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