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