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