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