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