1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3 * This file contains vfs inode ops for the 9P2000 protocol.
4 *
5 * Copyright (C) 2004 by Eric Van Hensbergen <ericvh@gmail.com>
6 * Copyright (C) 2002 by Ron Minnich <rminnich@lanl.gov>
7 */
8
9 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
10
11 #include <linux/module.h>
12 #include <linux/errno.h>
13 #include <linux/fs.h>
14 #include <linux/file.h>
15 #include <linux/pagemap.h>
16 #include <linux/stat.h>
17 #include <linux/string.h>
18 #include <linux/namei.h>
19 #include <linux/sched.h>
20 #include <linux/slab.h>
21 #include <linux/xattr.h>
22 #include <linux/posix_acl.h>
23 #include <net/9p/9p.h>
24 #include <net/9p/client.h>
25
26 #include "v9fs.h"
27 #include "v9fs_vfs.h"
28 #include "fid.h"
29 #include "cache.h"
30 #include "xattr.h"
31 #include "acl.h"
32
33 static const struct inode_operations v9fs_dir_inode_operations;
34 static const struct inode_operations v9fs_dir_inode_operations_dotu;
35 static const struct inode_operations v9fs_file_inode_operations;
36 static const struct inode_operations v9fs_symlink_inode_operations;
37
38 /**
39 * unixmode2p9mode - convert unix mode bits to plan 9
40 * @v9ses: v9fs session information
41 * @mode: mode to convert
42 *
43 */
44
unixmode2p9mode(struct v9fs_session_info * v9ses,umode_t mode)45 static u32 unixmode2p9mode(struct v9fs_session_info *v9ses, umode_t mode)
46 {
47 int res;
48
49 res = mode & 0777;
50 if (S_ISDIR(mode))
51 res |= P9_DMDIR;
52 if (v9fs_proto_dotu(v9ses)) {
53 if (v9ses->nodev == 0) {
54 if (S_ISSOCK(mode))
55 res |= P9_DMSOCKET;
56 if (S_ISFIFO(mode))
57 res |= P9_DMNAMEDPIPE;
58 if (S_ISBLK(mode))
59 res |= P9_DMDEVICE;
60 if (S_ISCHR(mode))
61 res |= P9_DMDEVICE;
62 }
63
64 if ((mode & S_ISUID) == S_ISUID)
65 res |= P9_DMSETUID;
66 if ((mode & S_ISGID) == S_ISGID)
67 res |= P9_DMSETGID;
68 if ((mode & S_ISVTX) == S_ISVTX)
69 res |= P9_DMSETVTX;
70 }
71 return res;
72 }
73
74 /**
75 * p9mode2perm- convert plan9 mode bits to unix permission bits
76 * @v9ses: v9fs session information
77 * @stat: p9_wstat from which mode need to be derived
78 *
79 */
p9mode2perm(struct v9fs_session_info * v9ses,struct p9_wstat * stat)80 static int p9mode2perm(struct v9fs_session_info *v9ses,
81 struct p9_wstat *stat)
82 {
83 int res;
84 int mode = stat->mode;
85
86 res = mode & 0777; /* S_IRWXUGO */
87 if (v9fs_proto_dotu(v9ses)) {
88 if ((mode & P9_DMSETUID) == P9_DMSETUID)
89 res |= S_ISUID;
90
91 if ((mode & P9_DMSETGID) == P9_DMSETGID)
92 res |= S_ISGID;
93
94 if ((mode & P9_DMSETVTX) == P9_DMSETVTX)
95 res |= S_ISVTX;
96 }
97 return res;
98 }
99
100 /**
101 * p9mode2unixmode- convert plan9 mode bits to unix mode bits
102 * @v9ses: v9fs session information
103 * @stat: p9_wstat from which mode need to be derived
104 * @rdev: major number, minor number in case of device files.
105 *
106 */
p9mode2unixmode(struct v9fs_session_info * v9ses,struct p9_wstat * stat,dev_t * rdev)107 static umode_t p9mode2unixmode(struct v9fs_session_info *v9ses,
108 struct p9_wstat *stat, dev_t *rdev)
109 {
110 int res, r;
111 u32 mode = stat->mode;
112
113 *rdev = 0;
114 res = p9mode2perm(v9ses, stat);
115
116 if ((mode & P9_DMDIR) == P9_DMDIR)
117 res |= S_IFDIR;
118 else if ((mode & P9_DMSYMLINK) && (v9fs_proto_dotu(v9ses)))
119 res |= S_IFLNK;
120 else if ((mode & P9_DMSOCKET) && (v9fs_proto_dotu(v9ses))
121 && (v9ses->nodev == 0))
122 res |= S_IFSOCK;
123 else if ((mode & P9_DMNAMEDPIPE) && (v9fs_proto_dotu(v9ses))
124 && (v9ses->nodev == 0))
125 res |= S_IFIFO;
126 else if ((mode & P9_DMDEVICE) && (v9fs_proto_dotu(v9ses))
127 && (v9ses->nodev == 0)) {
128 char type = 0;
129 int major = -1, minor = -1;
130
131 r = sscanf(stat->extension, "%c %i %i", &type, &major, &minor);
132 if (r != 3) {
133 p9_debug(P9_DEBUG_ERROR,
134 "invalid device string, umode will be bogus: %s\n",
135 stat->extension);
136 return res;
137 }
138 switch (type) {
139 case 'c':
140 res |= S_IFCHR;
141 break;
142 case 'b':
143 res |= S_IFBLK;
144 break;
145 default:
146 p9_debug(P9_DEBUG_ERROR, "Unknown special type %c %s\n",
147 type, stat->extension);
148 }
149 *rdev = MKDEV(major, minor);
150 } else
151 res |= S_IFREG;
152
153 return res;
154 }
155
156 /**
157 * v9fs_uflags2omode- convert posix open flags to plan 9 mode bits
158 * @uflags: flags to convert
159 * @extended: if .u extensions are active
160 */
161
v9fs_uflags2omode(int uflags,int extended)162 int v9fs_uflags2omode(int uflags, int extended)
163 {
164 int ret;
165
166 switch (uflags&3) {
167 default:
168 case O_RDONLY:
169 ret = P9_OREAD;
170 break;
171
172 case O_WRONLY:
173 ret = P9_OWRITE;
174 break;
175
176 case O_RDWR:
177 ret = P9_ORDWR;
178 break;
179 }
180
181 if (uflags & O_TRUNC)
182 ret |= P9_OTRUNC;
183
184 if (extended) {
185 if (uflags & O_EXCL)
186 ret |= P9_OEXCL;
187
188 if (uflags & O_APPEND)
189 ret |= P9_OAPPEND;
190 }
191
192 return ret;
193 }
194
195 /**
196 * v9fs_blank_wstat - helper function to setup a 9P stat structure
197 * @wstat: structure to initialize
198 *
199 */
200
201 void
v9fs_blank_wstat(struct p9_wstat * wstat)202 v9fs_blank_wstat(struct p9_wstat *wstat)
203 {
204 wstat->type = ~0;
205 wstat->dev = ~0;
206 wstat->qid.type = ~0;
207 wstat->qid.version = ~0;
208 *((long long *)&wstat->qid.path) = ~0;
209 wstat->mode = ~0;
210 wstat->atime = ~0;
211 wstat->mtime = ~0;
212 wstat->length = ~0;
213 wstat->name = NULL;
214 wstat->uid = NULL;
215 wstat->gid = NULL;
216 wstat->muid = NULL;
217 wstat->n_uid = INVALID_UID;
218 wstat->n_gid = INVALID_GID;
219 wstat->n_muid = INVALID_UID;
220 wstat->extension = NULL;
221 }
222
223 /**
224 * v9fs_alloc_inode - helper function to allocate an inode
225 * @sb: The superblock to allocate the inode from
226 */
v9fs_alloc_inode(struct super_block * sb)227 struct inode *v9fs_alloc_inode(struct super_block *sb)
228 {
229 struct v9fs_inode *v9inode;
230
231 v9inode = alloc_inode_sb(sb, v9fs_inode_cache, GFP_KERNEL);
232 if (!v9inode)
233 return NULL;
234 v9inode->cache_validity = 0;
235 mutex_init(&v9inode->v_mutex);
236 return &v9inode->netfs.inode;
237 }
238
239 /**
240 * v9fs_free_inode - destroy an inode
241 * @inode: The inode to be freed
242 */
243
v9fs_free_inode(struct inode * inode)244 void v9fs_free_inode(struct inode *inode)
245 {
246 kmem_cache_free(v9fs_inode_cache, V9FS_I(inode));
247 }
248
249 /*
250 * Set parameters for the netfs library
251 */
v9fs_set_netfs_context(struct inode * inode)252 void v9fs_set_netfs_context(struct inode *inode)
253 {
254 struct v9fs_inode *v9inode = V9FS_I(inode);
255 netfs_inode_init(&v9inode->netfs, &v9fs_req_ops);
256 }
257
v9fs_init_inode(struct v9fs_session_info * v9ses,struct inode * inode,umode_t mode,dev_t rdev)258 int v9fs_init_inode(struct v9fs_session_info *v9ses,
259 struct inode *inode, umode_t mode, dev_t rdev)
260 {
261 int err = 0;
262
263 inode_init_owner(&nop_mnt_idmap, inode, NULL, mode);
264 inode->i_blocks = 0;
265 inode->i_rdev = rdev;
266 inode->i_atime = inode->i_mtime = inode_set_ctime_current(inode);
267 inode->i_mapping->a_ops = &v9fs_addr_operations;
268 inode->i_private = NULL;
269
270 switch (mode & S_IFMT) {
271 case S_IFIFO:
272 case S_IFBLK:
273 case S_IFCHR:
274 case S_IFSOCK:
275 if (v9fs_proto_dotl(v9ses)) {
276 inode->i_op = &v9fs_file_inode_operations_dotl;
277 } else if (v9fs_proto_dotu(v9ses)) {
278 inode->i_op = &v9fs_file_inode_operations;
279 } else {
280 p9_debug(P9_DEBUG_ERROR,
281 "special files without extended mode\n");
282 err = -EINVAL;
283 goto error;
284 }
285 init_special_inode(inode, inode->i_mode, inode->i_rdev);
286 break;
287 case S_IFREG:
288 if (v9fs_proto_dotl(v9ses)) {
289 inode->i_op = &v9fs_file_inode_operations_dotl;
290 inode->i_fop = &v9fs_file_operations_dotl;
291 } else {
292 inode->i_op = &v9fs_file_inode_operations;
293 inode->i_fop = &v9fs_file_operations;
294 }
295
296 break;
297 case S_IFLNK:
298 if (!v9fs_proto_dotu(v9ses) && !v9fs_proto_dotl(v9ses)) {
299 p9_debug(P9_DEBUG_ERROR,
300 "extended modes used with legacy protocol\n");
301 err = -EINVAL;
302 goto error;
303 }
304
305 if (v9fs_proto_dotl(v9ses))
306 inode->i_op = &v9fs_symlink_inode_operations_dotl;
307 else
308 inode->i_op = &v9fs_symlink_inode_operations;
309
310 break;
311 case S_IFDIR:
312 inc_nlink(inode);
313 if (v9fs_proto_dotl(v9ses))
314 inode->i_op = &v9fs_dir_inode_operations_dotl;
315 else if (v9fs_proto_dotu(v9ses))
316 inode->i_op = &v9fs_dir_inode_operations_dotu;
317 else
318 inode->i_op = &v9fs_dir_inode_operations;
319
320 if (v9fs_proto_dotl(v9ses))
321 inode->i_fop = &v9fs_dir_operations_dotl;
322 else
323 inode->i_fop = &v9fs_dir_operations;
324
325 break;
326 default:
327 p9_debug(P9_DEBUG_ERROR, "BAD mode 0x%hx S_IFMT 0x%x\n",
328 mode, mode & S_IFMT);
329 err = -EINVAL;
330 goto error;
331 }
332 error:
333 return err;
334
335 }
336
337 /**
338 * v9fs_get_inode - helper function to setup an inode
339 * @sb: superblock
340 * @mode: mode to setup inode with
341 * @rdev: The device numbers to set
342 */
343
v9fs_get_inode(struct super_block * sb,umode_t mode,dev_t rdev)344 struct inode *v9fs_get_inode(struct super_block *sb, umode_t mode, dev_t rdev)
345 {
346 int err;
347 struct inode *inode;
348 struct v9fs_session_info *v9ses = sb->s_fs_info;
349
350 p9_debug(P9_DEBUG_VFS, "super block: %p mode: %ho\n", sb, mode);
351
352 inode = new_inode(sb);
353 if (!inode) {
354 pr_warn("%s (%d): Problem allocating inode\n",
355 __func__, task_pid_nr(current));
356 return ERR_PTR(-ENOMEM);
357 }
358 err = v9fs_init_inode(v9ses, inode, mode, rdev);
359 if (err) {
360 iput(inode);
361 return ERR_PTR(err);
362 }
363 v9fs_set_netfs_context(inode);
364 return inode;
365 }
366
367 /**
368 * v9fs_evict_inode - Remove an inode from the inode cache
369 * @inode: inode to release
370 *
371 */
v9fs_evict_inode(struct inode * inode)372 void v9fs_evict_inode(struct inode *inode)
373 {
374 struct v9fs_inode __maybe_unused *v9inode = V9FS_I(inode);
375 __le32 __maybe_unused version;
376
377 truncate_inode_pages_final(&inode->i_data);
378
379 #ifdef CONFIG_9P_FSCACHE
380 version = cpu_to_le32(v9inode->qid.version);
381 fscache_clear_inode_writeback(v9fs_inode_cookie(v9inode), inode,
382 &version);
383 #endif
384
385 clear_inode(inode);
386 filemap_fdatawrite(&inode->i_data);
387
388 #ifdef CONFIG_9P_FSCACHE
389 fscache_relinquish_cookie(v9fs_inode_cookie(v9inode), false);
390 #endif
391 }
392
v9fs_test_inode(struct inode * inode,void * data)393 static int v9fs_test_inode(struct inode *inode, void *data)
394 {
395 int umode;
396 dev_t rdev;
397 struct v9fs_inode *v9inode = V9FS_I(inode);
398 struct p9_wstat *st = (struct p9_wstat *)data;
399 struct v9fs_session_info *v9ses = v9fs_inode2v9ses(inode);
400
401 umode = p9mode2unixmode(v9ses, st, &rdev);
402 /* don't match inode of different type */
403 if (inode_wrong_type(inode, umode))
404 return 0;
405
406 /* compare qid details */
407 if (memcmp(&v9inode->qid.version,
408 &st->qid.version, sizeof(v9inode->qid.version)))
409 return 0;
410
411 if (v9inode->qid.type != st->qid.type)
412 return 0;
413
414 if (v9inode->qid.path != st->qid.path)
415 return 0;
416 return 1;
417 }
418
v9fs_test_new_inode(struct inode * inode,void * data)419 static int v9fs_test_new_inode(struct inode *inode, void *data)
420 {
421 return 0;
422 }
423
v9fs_set_inode(struct inode * inode,void * data)424 static int v9fs_set_inode(struct inode *inode, void *data)
425 {
426 struct v9fs_inode *v9inode = V9FS_I(inode);
427 struct p9_wstat *st = (struct p9_wstat *)data;
428
429 memcpy(&v9inode->qid, &st->qid, sizeof(st->qid));
430 return 0;
431 }
432
v9fs_qid_iget(struct super_block * sb,struct p9_qid * qid,struct p9_wstat * st,int new)433 static struct inode *v9fs_qid_iget(struct super_block *sb,
434 struct p9_qid *qid,
435 struct p9_wstat *st,
436 int new)
437 {
438 dev_t rdev;
439 int retval;
440 umode_t umode;
441 unsigned long i_ino;
442 struct inode *inode;
443 struct v9fs_session_info *v9ses = sb->s_fs_info;
444 int (*test)(struct inode *inode, void *data);
445
446 if (new)
447 test = v9fs_test_new_inode;
448 else
449 test = v9fs_test_inode;
450
451 i_ino = v9fs_qid2ino(qid);
452 inode = iget5_locked(sb, i_ino, test, v9fs_set_inode, st);
453 if (!inode)
454 return ERR_PTR(-ENOMEM);
455 if (!(inode->i_state & I_NEW))
456 return inode;
457 /*
458 * initialize the inode with the stat info
459 * FIXME!! we may need support for stale inodes
460 * later.
461 */
462 inode->i_ino = i_ino;
463 umode = p9mode2unixmode(v9ses, st, &rdev);
464 retval = v9fs_init_inode(v9ses, inode, umode, rdev);
465 if (retval)
466 goto error;
467
468 v9fs_stat2inode(st, inode, sb, 0);
469 v9fs_set_netfs_context(inode);
470 v9fs_cache_inode_get_cookie(inode);
471 unlock_new_inode(inode);
472 return inode;
473 error:
474 iget_failed(inode);
475 return ERR_PTR(retval);
476
477 }
478
479 struct inode *
v9fs_inode_from_fid(struct v9fs_session_info * v9ses,struct p9_fid * fid,struct super_block * sb,int new)480 v9fs_inode_from_fid(struct v9fs_session_info *v9ses, struct p9_fid *fid,
481 struct super_block *sb, int new)
482 {
483 struct p9_wstat *st;
484 struct inode *inode = NULL;
485
486 st = p9_client_stat(fid);
487 if (IS_ERR(st))
488 return ERR_CAST(st);
489
490 inode = v9fs_qid_iget(sb, &st->qid, st, new);
491 p9stat_free(st);
492 kfree(st);
493 return inode;
494 }
495
496 /**
497 * v9fs_at_to_dotl_flags- convert Linux specific AT flags to
498 * plan 9 AT flag.
499 * @flags: flags to convert
500 */
v9fs_at_to_dotl_flags(int flags)501 static int v9fs_at_to_dotl_flags(int flags)
502 {
503 int rflags = 0;
504
505 if (flags & AT_REMOVEDIR)
506 rflags |= P9_DOTL_AT_REMOVEDIR;
507
508 return rflags;
509 }
510
511 /**
512 * v9fs_dec_count - helper functon to drop i_nlink.
513 *
514 * If a directory had nlink <= 2 (including . and ..), then we should not drop
515 * the link count, which indicates the underlying exported fs doesn't maintain
516 * nlink accurately. e.g.
517 * - overlayfs sets nlink to 1 for merged dir
518 * - ext4 (with dir_nlink feature enabled) sets nlink to 1 if a dir has more
519 * than EXT4_LINK_MAX (65000) links.
520 *
521 * @inode: inode whose nlink is being dropped
522 */
v9fs_dec_count(struct inode * inode)523 static void v9fs_dec_count(struct inode *inode)
524 {
525 if (!S_ISDIR(inode->i_mode) || inode->i_nlink > 2)
526 drop_nlink(inode);
527 }
528
529 /**
530 * v9fs_remove - helper function to remove files and directories
531 * @dir: directory inode that is being deleted
532 * @dentry: dentry that is being deleted
533 * @flags: removing a directory
534 *
535 */
536
v9fs_remove(struct inode * dir,struct dentry * dentry,int flags)537 static int v9fs_remove(struct inode *dir, struct dentry *dentry, int flags)
538 {
539 struct inode *inode;
540 int retval = -EOPNOTSUPP;
541 struct p9_fid *v9fid, *dfid;
542 struct v9fs_session_info *v9ses;
543
544 p9_debug(P9_DEBUG_VFS, "inode: %p dentry: %p rmdir: %x\n",
545 dir, dentry, flags);
546
547 v9ses = v9fs_inode2v9ses(dir);
548 inode = d_inode(dentry);
549 dfid = v9fs_parent_fid(dentry);
550 if (IS_ERR(dfid)) {
551 retval = PTR_ERR(dfid);
552 p9_debug(P9_DEBUG_VFS, "fid lookup failed %d\n", retval);
553 return retval;
554 }
555 if (v9fs_proto_dotl(v9ses))
556 retval = p9_client_unlinkat(dfid, dentry->d_name.name,
557 v9fs_at_to_dotl_flags(flags));
558 p9_fid_put(dfid);
559 if (retval == -EOPNOTSUPP) {
560 /* Try the one based on path */
561 v9fid = v9fs_fid_clone(dentry);
562 if (IS_ERR(v9fid))
563 return PTR_ERR(v9fid);
564 retval = p9_client_remove(v9fid);
565 }
566 if (!retval) {
567 /*
568 * directories on unlink should have zero
569 * link count
570 */
571 if (flags & AT_REMOVEDIR) {
572 clear_nlink(inode);
573 v9fs_dec_count(dir);
574 } else
575 v9fs_dec_count(inode);
576
577 v9fs_invalidate_inode_attr(inode);
578 v9fs_invalidate_inode_attr(dir);
579
580 /* invalidate all fids associated with dentry */
581 /* NOTE: This will not include open fids */
582 dentry->d_op->d_release(dentry);
583 }
584 return retval;
585 }
586
587 /**
588 * v9fs_create - Create a file
589 * @v9ses: session information
590 * @dir: directory that dentry is being created in
591 * @dentry: dentry that is being created
592 * @extension: 9p2000.u extension string to support devices, etc.
593 * @perm: create permissions
594 * @mode: open mode
595 *
596 */
597 static struct p9_fid *
v9fs_create(struct v9fs_session_info * v9ses,struct inode * dir,struct dentry * dentry,char * extension,u32 perm,u8 mode)598 v9fs_create(struct v9fs_session_info *v9ses, struct inode *dir,
599 struct dentry *dentry, char *extension, u32 perm, u8 mode)
600 {
601 int err;
602 const unsigned char *name;
603 struct p9_fid *dfid, *ofid = NULL, *fid = NULL;
604 struct inode *inode;
605
606 p9_debug(P9_DEBUG_VFS, "name %pd\n", dentry);
607
608 name = dentry->d_name.name;
609 dfid = v9fs_parent_fid(dentry);
610 if (IS_ERR(dfid)) {
611 err = PTR_ERR(dfid);
612 p9_debug(P9_DEBUG_VFS, "fid lookup failed %d\n", err);
613 return ERR_PTR(err);
614 }
615
616 /* clone a fid to use for creation */
617 ofid = clone_fid(dfid);
618 if (IS_ERR(ofid)) {
619 err = PTR_ERR(ofid);
620 p9_debug(P9_DEBUG_VFS, "p9_client_walk failed %d\n", err);
621 goto error;
622 }
623
624 err = p9_client_fcreate(ofid, name, perm, mode, extension);
625 if (err < 0) {
626 p9_debug(P9_DEBUG_VFS, "p9_client_fcreate failed %d\n", err);
627 goto error;
628 }
629
630 if (!(perm & P9_DMLINK)) {
631 /* now walk from the parent so we can get unopened fid */
632 fid = p9_client_walk(dfid, 1, &name, 1);
633 if (IS_ERR(fid)) {
634 err = PTR_ERR(fid);
635 p9_debug(P9_DEBUG_VFS,
636 "p9_client_walk failed %d\n", err);
637 goto error;
638 }
639 /*
640 * instantiate inode and assign the unopened fid to the dentry
641 */
642 inode = v9fs_get_new_inode_from_fid(v9ses, fid, dir->i_sb);
643 if (IS_ERR(inode)) {
644 err = PTR_ERR(inode);
645 p9_debug(P9_DEBUG_VFS,
646 "inode creation failed %d\n", err);
647 goto error;
648 }
649 v9fs_fid_add(dentry, &fid);
650 d_instantiate(dentry, inode);
651 }
652 p9_fid_put(dfid);
653 return ofid;
654 error:
655 p9_fid_put(dfid);
656 p9_fid_put(ofid);
657 p9_fid_put(fid);
658 return ERR_PTR(err);
659 }
660
661 /**
662 * v9fs_vfs_create - VFS hook to create a regular file
663 * @idmap: idmap of the mount
664 * @dir: The parent directory
665 * @dentry: The name of file to be created
666 * @mode: The UNIX file mode to set
667 * @excl: True if the file must not yet exist
668 *
669 * open(.., O_CREAT) is handled in v9fs_vfs_atomic_open(). This is only called
670 * for mknod(2).
671 *
672 */
673
674 static int
v9fs_vfs_create(struct mnt_idmap * idmap,struct inode * dir,struct dentry * dentry,umode_t mode,bool excl)675 v9fs_vfs_create(struct mnt_idmap *idmap, struct inode *dir,
676 struct dentry *dentry, umode_t mode, bool excl)
677 {
678 struct v9fs_session_info *v9ses = v9fs_inode2v9ses(dir);
679 u32 perm = unixmode2p9mode(v9ses, mode);
680 struct p9_fid *fid;
681
682 /* P9_OEXCL? */
683 fid = v9fs_create(v9ses, dir, dentry, NULL, perm, P9_ORDWR);
684 if (IS_ERR(fid))
685 return PTR_ERR(fid);
686
687 v9fs_invalidate_inode_attr(dir);
688 p9_fid_put(fid);
689
690 return 0;
691 }
692
693 /**
694 * v9fs_vfs_mkdir - VFS mkdir hook to create a directory
695 * @idmap: idmap of the mount
696 * @dir: inode that is being unlinked
697 * @dentry: dentry that is being unlinked
698 * @mode: mode for new directory
699 *
700 */
701
v9fs_vfs_mkdir(struct mnt_idmap * idmap,struct inode * dir,struct dentry * dentry,umode_t mode)702 static int v9fs_vfs_mkdir(struct mnt_idmap *idmap, struct inode *dir,
703 struct dentry *dentry, umode_t mode)
704 {
705 int err;
706 u32 perm;
707 struct p9_fid *fid;
708 struct v9fs_session_info *v9ses;
709
710 p9_debug(P9_DEBUG_VFS, "name %pd\n", dentry);
711 err = 0;
712 v9ses = v9fs_inode2v9ses(dir);
713 perm = unixmode2p9mode(v9ses, mode | S_IFDIR);
714 fid = v9fs_create(v9ses, dir, dentry, NULL, perm, P9_OREAD);
715 if (IS_ERR(fid)) {
716 err = PTR_ERR(fid);
717 fid = NULL;
718 } else {
719 inc_nlink(dir);
720 v9fs_invalidate_inode_attr(dir);
721 }
722
723 if (fid)
724 p9_fid_put(fid);
725
726 return err;
727 }
728
729 /**
730 * v9fs_vfs_lookup - VFS lookup hook to "walk" to a new inode
731 * @dir: inode that is being walked from
732 * @dentry: dentry that is being walked to?
733 * @flags: lookup flags (unused)
734 *
735 */
736
v9fs_vfs_lookup(struct inode * dir,struct dentry * dentry,unsigned int flags)737 struct dentry *v9fs_vfs_lookup(struct inode *dir, struct dentry *dentry,
738 unsigned int flags)
739 {
740 struct dentry *res;
741 struct v9fs_session_info *v9ses;
742 struct p9_fid *dfid, *fid;
743 struct inode *inode;
744 const unsigned char *name;
745
746 p9_debug(P9_DEBUG_VFS, "dir: %p dentry: (%pd) %p flags: %x\n",
747 dir, dentry, dentry, flags);
748
749 if (dentry->d_name.len > NAME_MAX)
750 return ERR_PTR(-ENAMETOOLONG);
751
752 v9ses = v9fs_inode2v9ses(dir);
753 /* We can walk d_parent because we hold the dir->i_mutex */
754 dfid = v9fs_parent_fid(dentry);
755 if (IS_ERR(dfid))
756 return ERR_CAST(dfid);
757
758 /*
759 * Make sure we don't use a wrong inode due to parallel
760 * unlink. For cached mode create calls request for new
761 * inode. But with cache disabled, lookup should do this.
762 */
763 name = dentry->d_name.name;
764 fid = p9_client_walk(dfid, 1, &name, 1);
765 p9_fid_put(dfid);
766 if (fid == ERR_PTR(-ENOENT))
767 inode = NULL;
768 else if (IS_ERR(fid))
769 inode = ERR_CAST(fid);
770 else if (v9ses->cache & (CACHE_META|CACHE_LOOSE))
771 inode = v9fs_get_inode_from_fid(v9ses, fid, dir->i_sb);
772 else
773 inode = v9fs_get_new_inode_from_fid(v9ses, fid, dir->i_sb);
774 /*
775 * If we had a rename on the server and a parallel lookup
776 * for the new name, then make sure we instantiate with
777 * the new name. ie look up for a/b, while on server somebody
778 * moved b under k and client parallely did a lookup for
779 * k/b.
780 */
781 res = d_splice_alias(inode, dentry);
782 if (!IS_ERR(fid)) {
783 if (!res)
784 v9fs_fid_add(dentry, &fid);
785 else if (!IS_ERR(res))
786 v9fs_fid_add(res, &fid);
787 else
788 p9_fid_put(fid);
789 }
790 return res;
791 }
792
793 static int
v9fs_vfs_atomic_open(struct inode * dir,struct dentry * dentry,struct file * file,unsigned int flags,umode_t mode)794 v9fs_vfs_atomic_open(struct inode *dir, struct dentry *dentry,
795 struct file *file, unsigned int flags, umode_t mode)
796 {
797 int err;
798 u32 perm;
799 struct v9fs_inode __maybe_unused *v9inode;
800 struct v9fs_session_info *v9ses;
801 struct p9_fid *fid;
802 struct dentry *res = NULL;
803 struct inode *inode;
804 int p9_omode;
805
806 if (d_in_lookup(dentry)) {
807 res = v9fs_vfs_lookup(dir, dentry, 0);
808 if (IS_ERR(res))
809 return PTR_ERR(res);
810
811 if (res)
812 dentry = res;
813 }
814
815 /* Only creates */
816 if (!(flags & O_CREAT) || d_really_is_positive(dentry))
817 return finish_no_open(file, res);
818
819 v9ses = v9fs_inode2v9ses(dir);
820 perm = unixmode2p9mode(v9ses, mode);
821 p9_omode = v9fs_uflags2omode(flags, v9fs_proto_dotu(v9ses));
822
823 if ((v9ses->cache & CACHE_WRITEBACK) && (p9_omode & P9_OWRITE)) {
824 p9_omode = (p9_omode & ~P9_OWRITE) | P9_ORDWR;
825 p9_debug(P9_DEBUG_CACHE,
826 "write-only file with writeback enabled, creating w/ O_RDWR\n");
827 }
828 fid = v9fs_create(v9ses, dir, dentry, NULL, perm, p9_omode);
829 if (IS_ERR(fid)) {
830 err = PTR_ERR(fid);
831 goto error;
832 }
833
834 v9fs_invalidate_inode_attr(dir);
835 inode = d_inode(dentry);
836 v9inode = V9FS_I(inode);
837 err = finish_open(file, dentry, generic_file_open);
838 if (err)
839 goto error;
840
841 file->private_data = fid;
842 #ifdef CONFIG_9P_FSCACHE
843 if (v9ses->cache & CACHE_FSCACHE)
844 fscache_use_cookie(v9fs_inode_cookie(v9inode),
845 file->f_mode & FMODE_WRITE);
846 #endif
847
848 v9fs_fid_add_modes(fid, v9ses->flags, v9ses->cache, file->f_flags);
849 v9fs_open_fid_add(inode, &fid);
850
851 file->f_mode |= FMODE_CREATED;
852 out:
853 dput(res);
854 return err;
855
856 error:
857 p9_fid_put(fid);
858 goto out;
859 }
860
861 /**
862 * v9fs_vfs_unlink - VFS unlink hook to delete an inode
863 * @i: inode that is being unlinked
864 * @d: dentry that is being unlinked
865 *
866 */
867
v9fs_vfs_unlink(struct inode * i,struct dentry * d)868 int v9fs_vfs_unlink(struct inode *i, struct dentry *d)
869 {
870 return v9fs_remove(i, d, 0);
871 }
872
873 /**
874 * v9fs_vfs_rmdir - VFS unlink hook to delete a directory
875 * @i: inode that is being unlinked
876 * @d: dentry that is being unlinked
877 *
878 */
879
v9fs_vfs_rmdir(struct inode * i,struct dentry * d)880 int v9fs_vfs_rmdir(struct inode *i, struct dentry *d)
881 {
882 return v9fs_remove(i, d, AT_REMOVEDIR);
883 }
884
885 /**
886 * v9fs_vfs_rename - VFS hook to rename an inode
887 * @idmap: The idmap of the mount
888 * @old_dir: old dir inode
889 * @old_dentry: old dentry
890 * @new_dir: new dir inode
891 * @new_dentry: new dentry
892 * @flags: RENAME_* flags
893 *
894 */
895
896 int
v9fs_vfs_rename(struct mnt_idmap * idmap,struct inode * old_dir,struct dentry * old_dentry,struct inode * new_dir,struct dentry * new_dentry,unsigned int flags)897 v9fs_vfs_rename(struct mnt_idmap *idmap, struct inode *old_dir,
898 struct dentry *old_dentry, struct inode *new_dir,
899 struct dentry *new_dentry, unsigned int flags)
900 {
901 int retval;
902 struct inode *old_inode;
903 struct inode *new_inode;
904 struct v9fs_session_info *v9ses;
905 struct p9_fid *oldfid = NULL, *dfid = NULL;
906 struct p9_fid *olddirfid = NULL;
907 struct p9_fid *newdirfid = NULL;
908 struct p9_wstat wstat;
909
910 if (flags)
911 return -EINVAL;
912
913 p9_debug(P9_DEBUG_VFS, "\n");
914 old_inode = d_inode(old_dentry);
915 new_inode = d_inode(new_dentry);
916 v9ses = v9fs_inode2v9ses(old_inode);
917 oldfid = v9fs_fid_lookup(old_dentry);
918 if (IS_ERR(oldfid))
919 return PTR_ERR(oldfid);
920
921 dfid = v9fs_parent_fid(old_dentry);
922 olddirfid = clone_fid(dfid);
923 p9_fid_put(dfid);
924 dfid = NULL;
925
926 if (IS_ERR(olddirfid)) {
927 retval = PTR_ERR(olddirfid);
928 goto error;
929 }
930
931 dfid = v9fs_parent_fid(new_dentry);
932 newdirfid = clone_fid(dfid);
933 p9_fid_put(dfid);
934 dfid = NULL;
935
936 if (IS_ERR(newdirfid)) {
937 retval = PTR_ERR(newdirfid);
938 goto error;
939 }
940
941 down_write(&v9ses->rename_sem);
942 if (v9fs_proto_dotl(v9ses)) {
943 retval = p9_client_renameat(olddirfid, old_dentry->d_name.name,
944 newdirfid, new_dentry->d_name.name);
945 if (retval == -EOPNOTSUPP)
946 retval = p9_client_rename(oldfid, newdirfid,
947 new_dentry->d_name.name);
948 if (retval != -EOPNOTSUPP)
949 goto error_locked;
950 }
951 if (old_dentry->d_parent != new_dentry->d_parent) {
952 /*
953 * 9P .u can only handle file rename in the same directory
954 */
955
956 p9_debug(P9_DEBUG_ERROR, "old dir and new dir are different\n");
957 retval = -EXDEV;
958 goto error_locked;
959 }
960 v9fs_blank_wstat(&wstat);
961 wstat.muid = v9ses->uname;
962 wstat.name = new_dentry->d_name.name;
963 retval = p9_client_wstat(oldfid, &wstat);
964
965 error_locked:
966 if (!retval) {
967 if (new_inode) {
968 if (S_ISDIR(new_inode->i_mode))
969 clear_nlink(new_inode);
970 else
971 v9fs_dec_count(new_inode);
972 }
973 if (S_ISDIR(old_inode->i_mode)) {
974 if (!new_inode)
975 inc_nlink(new_dir);
976 v9fs_dec_count(old_dir);
977 }
978 v9fs_invalidate_inode_attr(old_inode);
979 v9fs_invalidate_inode_attr(old_dir);
980 v9fs_invalidate_inode_attr(new_dir);
981
982 /* successful rename */
983 d_move(old_dentry, new_dentry);
984 }
985 up_write(&v9ses->rename_sem);
986
987 error:
988 p9_fid_put(newdirfid);
989 p9_fid_put(olddirfid);
990 p9_fid_put(oldfid);
991 return retval;
992 }
993
994 /**
995 * v9fs_vfs_getattr - retrieve file metadata
996 * @idmap: idmap of the mount
997 * @path: Object to query
998 * @stat: metadata structure to populate
999 * @request_mask: Mask of STATX_xxx flags indicating the caller's interests
1000 * @flags: AT_STATX_xxx setting
1001 *
1002 */
1003
1004 static int
v9fs_vfs_getattr(struct mnt_idmap * idmap,const struct path * path,struct kstat * stat,u32 request_mask,unsigned int flags)1005 v9fs_vfs_getattr(struct mnt_idmap *idmap, const struct path *path,
1006 struct kstat *stat, u32 request_mask, unsigned int flags)
1007 {
1008 struct dentry *dentry = path->dentry;
1009 struct inode *inode = d_inode(dentry);
1010 struct v9fs_session_info *v9ses;
1011 struct p9_fid *fid;
1012 struct p9_wstat *st;
1013
1014 p9_debug(P9_DEBUG_VFS, "dentry: %p\n", dentry);
1015 v9ses = v9fs_dentry2v9ses(dentry);
1016 if (v9ses->cache & (CACHE_META|CACHE_LOOSE)) {
1017 generic_fillattr(&nop_mnt_idmap, request_mask, inode, stat);
1018 return 0;
1019 } else if (v9ses->cache & CACHE_WRITEBACK) {
1020 if (S_ISREG(inode->i_mode)) {
1021 int retval = filemap_fdatawrite(inode->i_mapping);
1022
1023 if (retval)
1024 p9_debug(P9_DEBUG_ERROR,
1025 "flushing writeback during getattr returned %d\n", retval);
1026 }
1027 }
1028 fid = v9fs_fid_lookup(dentry);
1029 if (IS_ERR(fid))
1030 return PTR_ERR(fid);
1031
1032 st = p9_client_stat(fid);
1033 p9_fid_put(fid);
1034 if (IS_ERR(st))
1035 return PTR_ERR(st);
1036
1037 v9fs_stat2inode(st, d_inode(dentry), dentry->d_sb, 0);
1038 generic_fillattr(&nop_mnt_idmap, request_mask, d_inode(dentry), stat);
1039
1040 p9stat_free(st);
1041 kfree(st);
1042 return 0;
1043 }
1044
1045 /**
1046 * v9fs_vfs_setattr - set file metadata
1047 * @idmap: idmap of the mount
1048 * @dentry: file whose metadata to set
1049 * @iattr: metadata assignment structure
1050 *
1051 */
1052
v9fs_vfs_setattr(struct mnt_idmap * idmap,struct dentry * dentry,struct iattr * iattr)1053 static int v9fs_vfs_setattr(struct mnt_idmap *idmap,
1054 struct dentry *dentry, struct iattr *iattr)
1055 {
1056 int retval, use_dentry = 0;
1057 struct inode *inode = d_inode(dentry);
1058 struct v9fs_session_info *v9ses;
1059 struct p9_fid *fid = NULL;
1060 struct p9_wstat wstat;
1061
1062 p9_debug(P9_DEBUG_VFS, "\n");
1063 retval = setattr_prepare(&nop_mnt_idmap, dentry, iattr);
1064 if (retval)
1065 return retval;
1066
1067 v9ses = v9fs_dentry2v9ses(dentry);
1068 if (iattr->ia_valid & ATTR_FILE) {
1069 fid = iattr->ia_file->private_data;
1070 WARN_ON(!fid);
1071 }
1072 if (!fid) {
1073 fid = v9fs_fid_lookup(dentry);
1074 use_dentry = 1;
1075 }
1076 if (IS_ERR(fid))
1077 return PTR_ERR(fid);
1078
1079 v9fs_blank_wstat(&wstat);
1080 if (iattr->ia_valid & ATTR_MODE)
1081 wstat.mode = unixmode2p9mode(v9ses, iattr->ia_mode);
1082
1083 if (iattr->ia_valid & ATTR_MTIME)
1084 wstat.mtime = iattr->ia_mtime.tv_sec;
1085
1086 if (iattr->ia_valid & ATTR_ATIME)
1087 wstat.atime = iattr->ia_atime.tv_sec;
1088
1089 if (iattr->ia_valid & ATTR_SIZE)
1090 wstat.length = iattr->ia_size;
1091
1092 if (v9fs_proto_dotu(v9ses)) {
1093 if (iattr->ia_valid & ATTR_UID)
1094 wstat.n_uid = iattr->ia_uid;
1095
1096 if (iattr->ia_valid & ATTR_GID)
1097 wstat.n_gid = iattr->ia_gid;
1098 }
1099
1100 /* Write all dirty data */
1101 if (d_is_reg(dentry)) {
1102 retval = filemap_fdatawrite(inode->i_mapping);
1103 if (retval)
1104 p9_debug(P9_DEBUG_ERROR,
1105 "flushing writeback during setattr returned %d\n", retval);
1106 }
1107
1108 retval = p9_client_wstat(fid, &wstat);
1109
1110 if (use_dentry)
1111 p9_fid_put(fid);
1112
1113 if (retval < 0)
1114 return retval;
1115
1116 if ((iattr->ia_valid & ATTR_SIZE) &&
1117 iattr->ia_size != i_size_read(inode)) {
1118 truncate_setsize(inode, iattr->ia_size);
1119 truncate_pagecache(inode, iattr->ia_size);
1120
1121 #ifdef CONFIG_9P_FSCACHE
1122 if (v9ses->cache & CACHE_FSCACHE) {
1123 struct v9fs_inode *v9inode = V9FS_I(inode);
1124
1125 fscache_resize_cookie(v9fs_inode_cookie(v9inode), iattr->ia_size);
1126 }
1127 #endif
1128 }
1129
1130 v9fs_invalidate_inode_attr(inode);
1131
1132 setattr_copy(&nop_mnt_idmap, inode, iattr);
1133 mark_inode_dirty(inode);
1134 return 0;
1135 }
1136
1137 /**
1138 * v9fs_stat2inode - populate an inode structure with mistat info
1139 * @stat: Plan 9 metadata (mistat) structure
1140 * @inode: inode to populate
1141 * @sb: superblock of filesystem
1142 * @flags: control flags (e.g. V9FS_STAT2INODE_KEEP_ISIZE)
1143 *
1144 */
1145
1146 void
v9fs_stat2inode(struct p9_wstat * stat,struct inode * inode,struct super_block * sb,unsigned int flags)1147 v9fs_stat2inode(struct p9_wstat *stat, struct inode *inode,
1148 struct super_block *sb, unsigned int flags)
1149 {
1150 umode_t mode;
1151 struct v9fs_session_info *v9ses = sb->s_fs_info;
1152 struct v9fs_inode *v9inode = V9FS_I(inode);
1153
1154 set_nlink(inode, 1);
1155
1156 inode->i_atime.tv_sec = stat->atime;
1157 inode->i_mtime.tv_sec = stat->mtime;
1158 inode_set_ctime(inode, stat->mtime, 0);
1159
1160 inode->i_uid = v9ses->dfltuid;
1161 inode->i_gid = v9ses->dfltgid;
1162
1163 if (v9fs_proto_dotu(v9ses)) {
1164 inode->i_uid = stat->n_uid;
1165 inode->i_gid = stat->n_gid;
1166 }
1167 if ((S_ISREG(inode->i_mode)) || (S_ISDIR(inode->i_mode))) {
1168 if (v9fs_proto_dotu(v9ses)) {
1169 unsigned int i_nlink;
1170 /*
1171 * Hadlink support got added later to the .u extension.
1172 * So there can be a server out there that doesn't
1173 * support this even with .u extension. That would
1174 * just leave us with stat->extension being an empty
1175 * string, though.
1176 */
1177 /* HARDLINKCOUNT %u */
1178 if (sscanf(stat->extension,
1179 " HARDLINKCOUNT %u", &i_nlink) == 1)
1180 set_nlink(inode, i_nlink);
1181 }
1182 }
1183 mode = p9mode2perm(v9ses, stat);
1184 mode |= inode->i_mode & ~S_IALLUGO;
1185 inode->i_mode = mode;
1186
1187 if (!(flags & V9FS_STAT2INODE_KEEP_ISIZE))
1188 v9fs_i_size_write(inode, stat->length);
1189 /* not real number of blocks, but 512 byte ones ... */
1190 inode->i_blocks = (stat->length + 512 - 1) >> 9;
1191 v9inode->cache_validity &= ~V9FS_INO_INVALID_ATTR;
1192 }
1193
1194 /**
1195 * v9fs_qid2ino - convert qid into inode number
1196 * @qid: qid to hash
1197 *
1198 * BUG: potential for inode number collisions?
1199 */
1200
v9fs_qid2ino(struct p9_qid * qid)1201 ino_t v9fs_qid2ino(struct p9_qid *qid)
1202 {
1203 u64 path = qid->path + 2;
1204 ino_t i = 0;
1205
1206 if (sizeof(ino_t) == sizeof(path))
1207 memcpy(&i, &path, sizeof(ino_t));
1208 else
1209 i = (ino_t) (path ^ (path >> 32));
1210
1211 return i;
1212 }
1213
1214 /**
1215 * v9fs_vfs_get_link - follow a symlink path
1216 * @dentry: dentry for symlink
1217 * @inode: inode for symlink
1218 * @done: delayed call for when we are done with the return value
1219 */
1220
v9fs_vfs_get_link(struct dentry * dentry,struct inode * inode,struct delayed_call * done)1221 static const char *v9fs_vfs_get_link(struct dentry *dentry,
1222 struct inode *inode,
1223 struct delayed_call *done)
1224 {
1225 struct v9fs_session_info *v9ses;
1226 struct p9_fid *fid;
1227 struct p9_wstat *st;
1228 char *res;
1229
1230 if (!dentry)
1231 return ERR_PTR(-ECHILD);
1232
1233 v9ses = v9fs_dentry2v9ses(dentry);
1234 if (!v9fs_proto_dotu(v9ses))
1235 return ERR_PTR(-EBADF);
1236
1237 p9_debug(P9_DEBUG_VFS, "%pd\n", dentry);
1238 fid = v9fs_fid_lookup(dentry);
1239
1240 if (IS_ERR(fid))
1241 return ERR_CAST(fid);
1242
1243 st = p9_client_stat(fid);
1244 p9_fid_put(fid);
1245 if (IS_ERR(st))
1246 return ERR_CAST(st);
1247
1248 if (!(st->mode & P9_DMSYMLINK)) {
1249 p9stat_free(st);
1250 kfree(st);
1251 return ERR_PTR(-EINVAL);
1252 }
1253 res = st->extension;
1254 st->extension = NULL;
1255 if (strlen(res) >= PATH_MAX)
1256 res[PATH_MAX - 1] = '\0';
1257
1258 p9stat_free(st);
1259 kfree(st);
1260 set_delayed_call(done, kfree_link, res);
1261 return res;
1262 }
1263
1264 /**
1265 * v9fs_vfs_mkspecial - create a special file
1266 * @dir: inode to create special file in
1267 * @dentry: dentry to create
1268 * @perm: mode to create special file
1269 * @extension: 9p2000.u format extension string representing special file
1270 *
1271 */
1272
v9fs_vfs_mkspecial(struct inode * dir,struct dentry * dentry,u32 perm,const char * extension)1273 static int v9fs_vfs_mkspecial(struct inode *dir, struct dentry *dentry,
1274 u32 perm, const char *extension)
1275 {
1276 struct p9_fid *fid;
1277 struct v9fs_session_info *v9ses;
1278
1279 v9ses = v9fs_inode2v9ses(dir);
1280 if (!v9fs_proto_dotu(v9ses)) {
1281 p9_debug(P9_DEBUG_ERROR, "not extended\n");
1282 return -EPERM;
1283 }
1284
1285 fid = v9fs_create(v9ses, dir, dentry, (char *) extension, perm,
1286 P9_OREAD);
1287 if (IS_ERR(fid))
1288 return PTR_ERR(fid);
1289
1290 v9fs_invalidate_inode_attr(dir);
1291 p9_fid_put(fid);
1292 return 0;
1293 }
1294
1295 /**
1296 * v9fs_vfs_symlink - helper function to create symlinks
1297 * @idmap: idmap of the mount
1298 * @dir: directory inode containing symlink
1299 * @dentry: dentry for symlink
1300 * @symname: symlink data
1301 *
1302 * See Also: 9P2000.u RFC for more information
1303 *
1304 */
1305
1306 static int
v9fs_vfs_symlink(struct mnt_idmap * idmap,struct inode * dir,struct dentry * dentry,const char * symname)1307 v9fs_vfs_symlink(struct mnt_idmap *idmap, struct inode *dir,
1308 struct dentry *dentry, const char *symname)
1309 {
1310 p9_debug(P9_DEBUG_VFS, " %lu,%pd,%s\n",
1311 dir->i_ino, dentry, symname);
1312
1313 return v9fs_vfs_mkspecial(dir, dentry, P9_DMSYMLINK, symname);
1314 }
1315
1316 #define U32_MAX_DIGITS 10
1317
1318 /**
1319 * v9fs_vfs_link - create a hardlink
1320 * @old_dentry: dentry for file to link to
1321 * @dir: inode destination for new link
1322 * @dentry: dentry for link
1323 *
1324 */
1325
1326 static int
v9fs_vfs_link(struct dentry * old_dentry,struct inode * dir,struct dentry * dentry)1327 v9fs_vfs_link(struct dentry *old_dentry, struct inode *dir,
1328 struct dentry *dentry)
1329 {
1330 int retval;
1331 char name[1 + U32_MAX_DIGITS + 2]; /* sign + number + \n + \0 */
1332 struct p9_fid *oldfid;
1333
1334 p9_debug(P9_DEBUG_VFS, " %lu,%pd,%pd\n",
1335 dir->i_ino, dentry, old_dentry);
1336
1337 oldfid = v9fs_fid_clone(old_dentry);
1338 if (IS_ERR(oldfid))
1339 return PTR_ERR(oldfid);
1340
1341 sprintf(name, "%d\n", oldfid->fid);
1342 retval = v9fs_vfs_mkspecial(dir, dentry, P9_DMLINK, name);
1343 if (!retval) {
1344 v9fs_refresh_inode(oldfid, d_inode(old_dentry));
1345 v9fs_invalidate_inode_attr(dir);
1346 }
1347 p9_fid_put(oldfid);
1348 return retval;
1349 }
1350
1351 /**
1352 * v9fs_vfs_mknod - create a special file
1353 * @idmap: idmap of the mount
1354 * @dir: inode destination for new link
1355 * @dentry: dentry for file
1356 * @mode: mode for creation
1357 * @rdev: device associated with special file
1358 *
1359 */
1360
1361 static int
v9fs_vfs_mknod(struct mnt_idmap * idmap,struct inode * dir,struct dentry * dentry,umode_t mode,dev_t rdev)1362 v9fs_vfs_mknod(struct mnt_idmap *idmap, struct inode *dir,
1363 struct dentry *dentry, umode_t mode, dev_t rdev)
1364 {
1365 struct v9fs_session_info *v9ses = v9fs_inode2v9ses(dir);
1366 int retval;
1367 char name[2 + U32_MAX_DIGITS + 1 + U32_MAX_DIGITS + 1];
1368 u32 perm;
1369
1370 p9_debug(P9_DEBUG_VFS, " %lu,%pd mode: %x MAJOR: %u MINOR: %u\n",
1371 dir->i_ino, dentry, mode,
1372 MAJOR(rdev), MINOR(rdev));
1373
1374 /* build extension */
1375 if (S_ISBLK(mode))
1376 sprintf(name, "b %u %u", MAJOR(rdev), MINOR(rdev));
1377 else if (S_ISCHR(mode))
1378 sprintf(name, "c %u %u", MAJOR(rdev), MINOR(rdev));
1379 else
1380 *name = 0;
1381
1382 perm = unixmode2p9mode(v9ses, mode);
1383 retval = v9fs_vfs_mkspecial(dir, dentry, perm, name);
1384
1385 return retval;
1386 }
1387
v9fs_refresh_inode(struct p9_fid * fid,struct inode * inode)1388 int v9fs_refresh_inode(struct p9_fid *fid, struct inode *inode)
1389 {
1390 int umode;
1391 dev_t rdev;
1392 struct p9_wstat *st;
1393 struct v9fs_session_info *v9ses;
1394 unsigned int flags;
1395
1396 v9ses = v9fs_inode2v9ses(inode);
1397 st = p9_client_stat(fid);
1398 if (IS_ERR(st))
1399 return PTR_ERR(st);
1400 /*
1401 * Don't update inode if the file type is different
1402 */
1403 umode = p9mode2unixmode(v9ses, st, &rdev);
1404 if (inode_wrong_type(inode, umode))
1405 goto out;
1406
1407 /*
1408 * We don't want to refresh inode->i_size,
1409 * because we may have cached data
1410 */
1411 flags = (v9ses->cache & CACHE_LOOSE) ?
1412 V9FS_STAT2INODE_KEEP_ISIZE : 0;
1413 v9fs_stat2inode(st, inode, inode->i_sb, flags);
1414 out:
1415 p9stat_free(st);
1416 kfree(st);
1417 return 0;
1418 }
1419
1420 static const struct inode_operations v9fs_dir_inode_operations_dotu = {
1421 .create = v9fs_vfs_create,
1422 .lookup = v9fs_vfs_lookup,
1423 .atomic_open = v9fs_vfs_atomic_open,
1424 .symlink = v9fs_vfs_symlink,
1425 .link = v9fs_vfs_link,
1426 .unlink = v9fs_vfs_unlink,
1427 .mkdir = v9fs_vfs_mkdir,
1428 .rmdir = v9fs_vfs_rmdir,
1429 .mknod = v9fs_vfs_mknod,
1430 .rename = v9fs_vfs_rename,
1431 .getattr = v9fs_vfs_getattr,
1432 .setattr = v9fs_vfs_setattr,
1433 };
1434
1435 static const struct inode_operations v9fs_dir_inode_operations = {
1436 .create = v9fs_vfs_create,
1437 .lookup = v9fs_vfs_lookup,
1438 .atomic_open = v9fs_vfs_atomic_open,
1439 .unlink = v9fs_vfs_unlink,
1440 .mkdir = v9fs_vfs_mkdir,
1441 .rmdir = v9fs_vfs_rmdir,
1442 .mknod = v9fs_vfs_mknod,
1443 .rename = v9fs_vfs_rename,
1444 .getattr = v9fs_vfs_getattr,
1445 .setattr = v9fs_vfs_setattr,
1446 };
1447
1448 static const struct inode_operations v9fs_file_inode_operations = {
1449 .getattr = v9fs_vfs_getattr,
1450 .setattr = v9fs_vfs_setattr,
1451 };
1452
1453 static const struct inode_operations v9fs_symlink_inode_operations = {
1454 .get_link = v9fs_vfs_get_link,
1455 .getattr = v9fs_vfs_getattr,
1456 .setattr = v9fs_vfs_setattr,
1457 };
1458
1459