xref: /linux/fs/9p/vfs_inode.c (revision 6ca80638b90cec66547011ee1ef79e534589989a)
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/namei.h>
19 #include <linux/sched.h>
20 #include <linux/slab.h>
21 #include <linux/xattr.h>
22 #include <linux/posix_acl.h>
23 #include <net/9p/9p.h>
24 #include <net/9p/client.h>
25 
26 #include "v9fs.h"
27 #include "v9fs_vfs.h"
28 #include "fid.h"
29 #include "cache.h"
30 #include "xattr.h"
31 #include "acl.h"
32 
33 static const struct inode_operations v9fs_dir_inode_operations;
34 static const struct inode_operations v9fs_dir_inode_operations_dotu;
35 static const struct inode_operations v9fs_file_inode_operations;
36 static const struct inode_operations v9fs_symlink_inode_operations;
37 
38 /**
39  * unixmode2p9mode - convert unix mode bits to plan 9
40  * @v9ses: v9fs session information
41  * @mode: mode to convert
42  *
43  */
44 
45 static u32 unixmode2p9mode(struct v9fs_session_info *v9ses, umode_t mode)
46 {
47 	int res;
48 
49 	res = mode & 0777;
50 	if (S_ISDIR(mode))
51 		res |= P9_DMDIR;
52 	if (v9fs_proto_dotu(v9ses)) {
53 		if (v9ses->nodev == 0) {
54 			if (S_ISSOCK(mode))
55 				res |= P9_DMSOCKET;
56 			if (S_ISFIFO(mode))
57 				res |= P9_DMNAMEDPIPE;
58 			if (S_ISBLK(mode))
59 				res |= P9_DMDEVICE;
60 			if (S_ISCHR(mode))
61 				res |= P9_DMDEVICE;
62 		}
63 
64 		if ((mode & S_ISUID) == S_ISUID)
65 			res |= P9_DMSETUID;
66 		if ((mode & S_ISGID) == S_ISGID)
67 			res |= P9_DMSETGID;
68 		if ((mode & S_ISVTX) == S_ISVTX)
69 			res |= P9_DMSETVTX;
70 	}
71 	return res;
72 }
73 
74 /**
75  * p9mode2perm- convert plan9 mode bits to unix permission bits
76  * @v9ses: v9fs session information
77  * @stat: p9_wstat from which mode need to be derived
78  *
79  */
80 static int p9mode2perm(struct v9fs_session_info *v9ses,
81 		       struct p9_wstat *stat)
82 {
83 	int res;
84 	int mode = stat->mode;
85 
86 	res = mode & S_IALLUGO;
87 	if (v9fs_proto_dotu(v9ses)) {
88 		if ((mode & P9_DMSETUID) == P9_DMSETUID)
89 			res |= S_ISUID;
90 
91 		if ((mode & P9_DMSETGID) == P9_DMSETGID)
92 			res |= S_ISGID;
93 
94 		if ((mode & P9_DMSETVTX) == P9_DMSETVTX)
95 			res |= S_ISVTX;
96 	}
97 	return res;
98 }
99 
100 /**
101  * p9mode2unixmode- convert plan9 mode bits to unix mode bits
102  * @v9ses: v9fs session information
103  * @stat: p9_wstat from which mode need to be derived
104  * @rdev: major number, minor number in case of device files.
105  *
106  */
107 static umode_t p9mode2unixmode(struct v9fs_session_info *v9ses,
108 			       struct p9_wstat *stat, dev_t *rdev)
109 {
110 	int res, r;
111 	u32 mode = stat->mode;
112 
113 	*rdev = 0;
114 	res = p9mode2perm(v9ses, stat);
115 
116 	if ((mode & P9_DMDIR) == P9_DMDIR)
117 		res |= S_IFDIR;
118 	else if ((mode & P9_DMSYMLINK) && (v9fs_proto_dotu(v9ses)))
119 		res |= S_IFLNK;
120 	else if ((mode & P9_DMSOCKET) && (v9fs_proto_dotu(v9ses))
121 		 && (v9ses->nodev == 0))
122 		res |= S_IFSOCK;
123 	else if ((mode & P9_DMNAMEDPIPE) && (v9fs_proto_dotu(v9ses))
124 		 && (v9ses->nodev == 0))
125 		res |= S_IFIFO;
126 	else if ((mode & P9_DMDEVICE) && (v9fs_proto_dotu(v9ses))
127 		 && (v9ses->nodev == 0)) {
128 		char type = 0;
129 		int major = -1, minor = -1;
130 
131 		r = sscanf(stat->extension, "%c %i %i", &type, &major, &minor);
132 		if (r != 3) {
133 			p9_debug(P9_DEBUG_ERROR,
134 				 "invalid device string, umode will be bogus: %s\n",
135 				 stat->extension);
136 			return res;
137 		}
138 		switch (type) {
139 		case 'c':
140 			res |= S_IFCHR;
141 			break;
142 		case 'b':
143 			res |= S_IFBLK;
144 			break;
145 		default:
146 			p9_debug(P9_DEBUG_ERROR, "Unknown special type %c %s\n",
147 				 type, stat->extension);
148 		}
149 		*rdev = MKDEV(major, minor);
150 	} else
151 		res |= S_IFREG;
152 
153 	return res;
154 }
155 
156 /**
157  * v9fs_uflags2omode- convert posix open flags to plan 9 mode bits
158  * @uflags: flags to convert
159  * @extended: if .u extensions are active
160  */
161 
162 int v9fs_uflags2omode(int uflags, int extended)
163 {
164 	int ret;
165 
166 	switch (uflags&3) {
167 	default:
168 	case O_RDONLY:
169 		ret = P9_OREAD;
170 		break;
171 
172 	case O_WRONLY:
173 		ret = P9_OWRITE;
174 		break;
175 
176 	case O_RDWR:
177 		ret = P9_ORDWR;
178 		break;
179 	}
180 
181 	if (extended) {
182 		if (uflags & O_EXCL)
183 			ret |= P9_OEXCL;
184 
185 		if (uflags & O_APPEND)
186 			ret |= P9_OAPPEND;
187 	}
188 
189 	return ret;
190 }
191 
192 /**
193  * v9fs_blank_wstat - helper function to setup a 9P stat structure
194  * @wstat: structure to initialize
195  *
196  */
197 
198 void
199 v9fs_blank_wstat(struct p9_wstat *wstat)
200 {
201 	wstat->type = ~0;
202 	wstat->dev = ~0;
203 	wstat->qid.type = ~0;
204 	wstat->qid.version = ~0;
205 	*((long long *)&wstat->qid.path) = ~0;
206 	wstat->mode = ~0;
207 	wstat->atime = ~0;
208 	wstat->mtime = ~0;
209 	wstat->length = ~0;
210 	wstat->name = NULL;
211 	wstat->uid = NULL;
212 	wstat->gid = NULL;
213 	wstat->muid = NULL;
214 	wstat->n_uid = INVALID_UID;
215 	wstat->n_gid = INVALID_GID;
216 	wstat->n_muid = INVALID_UID;
217 	wstat->extension = NULL;
218 }
219 
220 /**
221  * v9fs_alloc_inode - helper function to allocate an inode
222  * @sb: The superblock to allocate the inode from
223  */
224 struct inode *v9fs_alloc_inode(struct super_block *sb)
225 {
226 	struct v9fs_inode *v9inode;
227 
228 	v9inode = alloc_inode_sb(sb, v9fs_inode_cache, GFP_KERNEL);
229 	if (!v9inode)
230 		return NULL;
231 	v9inode->cache_validity = 0;
232 	mutex_init(&v9inode->v_mutex);
233 	return &v9inode->netfs.inode;
234 }
235 
236 /**
237  * v9fs_free_inode - destroy an inode
238  * @inode: The inode to be freed
239  */
240 
241 void v9fs_free_inode(struct inode *inode)
242 {
243 	kmem_cache_free(v9fs_inode_cache, V9FS_I(inode));
244 }
245 
246 /*
247  * Set parameters for the netfs library
248  */
249 static void v9fs_set_netfs_context(struct inode *inode)
250 {
251 	struct v9fs_inode *v9inode = V9FS_I(inode);
252 	netfs_inode_init(&v9inode->netfs, &v9fs_req_ops);
253 }
254 
255 int v9fs_init_inode(struct v9fs_session_info *v9ses,
256 		    struct inode *inode, umode_t mode, dev_t rdev)
257 {
258 	int err = 0;
259 
260 	inode_init_owner(&nop_mnt_idmap, inode, NULL, mode);
261 	inode->i_blocks = 0;
262 	inode->i_rdev = rdev;
263 	inode->i_atime = inode->i_mtime = inode_set_ctime_current(inode);
264 	inode->i_mapping->a_ops = &v9fs_addr_operations;
265 	inode->i_private = NULL;
266 
267 	switch (mode & S_IFMT) {
268 	case S_IFIFO:
269 	case S_IFBLK:
270 	case S_IFCHR:
271 	case S_IFSOCK:
272 		if (v9fs_proto_dotl(v9ses)) {
273 			inode->i_op = &v9fs_file_inode_operations_dotl;
274 		} else if (v9fs_proto_dotu(v9ses)) {
275 			inode->i_op = &v9fs_file_inode_operations;
276 		} else {
277 			p9_debug(P9_DEBUG_ERROR,
278 				 "special files without extended mode\n");
279 			err = -EINVAL;
280 			goto error;
281 		}
282 		init_special_inode(inode, inode->i_mode, inode->i_rdev);
283 		break;
284 	case S_IFREG:
285 		if (v9fs_proto_dotl(v9ses)) {
286 			inode->i_op = &v9fs_file_inode_operations_dotl;
287 			inode->i_fop = &v9fs_file_operations_dotl;
288 		} else {
289 			inode->i_op = &v9fs_file_inode_operations;
290 			inode->i_fop = &v9fs_file_operations;
291 		}
292 
293 		break;
294 	case S_IFLNK:
295 		if (!v9fs_proto_dotu(v9ses) && !v9fs_proto_dotl(v9ses)) {
296 			p9_debug(P9_DEBUG_ERROR,
297 				 "extended modes used with legacy protocol\n");
298 			err = -EINVAL;
299 			goto error;
300 		}
301 
302 		if (v9fs_proto_dotl(v9ses))
303 			inode->i_op = &v9fs_symlink_inode_operations_dotl;
304 		else
305 			inode->i_op = &v9fs_symlink_inode_operations;
306 
307 		break;
308 	case S_IFDIR:
309 		inc_nlink(inode);
310 		if (v9fs_proto_dotl(v9ses))
311 			inode->i_op = &v9fs_dir_inode_operations_dotl;
312 		else if (v9fs_proto_dotu(v9ses))
313 			inode->i_op = &v9fs_dir_inode_operations_dotu;
314 		else
315 			inode->i_op = &v9fs_dir_inode_operations;
316 
317 		if (v9fs_proto_dotl(v9ses))
318 			inode->i_fop = &v9fs_dir_operations_dotl;
319 		else
320 			inode->i_fop = &v9fs_dir_operations;
321 
322 		break;
323 	default:
324 		p9_debug(P9_DEBUG_ERROR, "BAD mode 0x%hx S_IFMT 0x%x\n",
325 			 mode, mode & S_IFMT);
326 		err = -EINVAL;
327 		goto error;
328 	}
329 
330 	v9fs_set_netfs_context(inode);
331 error:
332 	return err;
333 
334 }
335 
336 /**
337  * v9fs_get_inode - helper function to setup an inode
338  * @sb: superblock
339  * @mode: mode to setup inode with
340  * @rdev: The device numbers to set
341  */
342 
343 struct inode *v9fs_get_inode(struct super_block *sb, umode_t mode, dev_t rdev)
344 {
345 	int err;
346 	struct inode *inode;
347 	struct v9fs_session_info *v9ses = sb->s_fs_info;
348 
349 	p9_debug(P9_DEBUG_VFS, "super block: %p mode: %ho\n", sb, mode);
350 
351 	inode = new_inode(sb);
352 	if (!inode) {
353 		pr_warn("%s (%d): Problem allocating inode\n",
354 			__func__, task_pid_nr(current));
355 		return ERR_PTR(-ENOMEM);
356 	}
357 	err = v9fs_init_inode(v9ses, inode, mode, rdev);
358 	if (err) {
359 		iput(inode);
360 		return ERR_PTR(err);
361 	}
362 	return inode;
363 }
364 
365 /**
366  * v9fs_evict_inode - Remove an inode from the inode cache
367  * @inode: inode to release
368  *
369  */
370 void v9fs_evict_inode(struct inode *inode)
371 {
372 	struct v9fs_inode __maybe_unused *v9inode = V9FS_I(inode);
373 	__le32 __maybe_unused version;
374 
375 	truncate_inode_pages_final(&inode->i_data);
376 
377 #ifdef CONFIG_9P_FSCACHE
378 	version = cpu_to_le32(v9inode->qid.version);
379 	fscache_clear_inode_writeback(v9fs_inode_cookie(v9inode), inode,
380 				      &version);
381 #endif
382 
383 	clear_inode(inode);
384 	filemap_fdatawrite(&inode->i_data);
385 
386 #ifdef CONFIG_9P_FSCACHE
387 	fscache_relinquish_cookie(v9fs_inode_cookie(v9inode), false);
388 #endif
389 }
390 
391 static int v9fs_test_inode(struct inode *inode, void *data)
392 {
393 	int umode;
394 	dev_t rdev;
395 	struct v9fs_inode *v9inode = V9FS_I(inode);
396 	struct p9_wstat *st = (struct p9_wstat *)data;
397 	struct v9fs_session_info *v9ses = v9fs_inode2v9ses(inode);
398 
399 	umode = p9mode2unixmode(v9ses, st, &rdev);
400 	/* don't match inode of different type */
401 	if (inode_wrong_type(inode, umode))
402 		return 0;
403 
404 	/* compare qid details */
405 	if (memcmp(&v9inode->qid.version,
406 		   &st->qid.version, sizeof(v9inode->qid.version)))
407 		return 0;
408 
409 	if (v9inode->qid.type != st->qid.type)
410 		return 0;
411 
412 	if (v9inode->qid.path != st->qid.path)
413 		return 0;
414 	return 1;
415 }
416 
417 static int v9fs_test_new_inode(struct inode *inode, void *data)
418 {
419 	return 0;
420 }
421 
422 static int v9fs_set_inode(struct inode *inode,  void *data)
423 {
424 	struct v9fs_inode *v9inode = V9FS_I(inode);
425 	struct p9_wstat *st = (struct p9_wstat *)data;
426 
427 	memcpy(&v9inode->qid, &st->qid, sizeof(st->qid));
428 	return 0;
429 }
430 
431 static struct inode *v9fs_qid_iget(struct super_block *sb,
432 				   struct p9_qid *qid,
433 				   struct p9_wstat *st,
434 				   int new)
435 {
436 	dev_t rdev;
437 	int retval;
438 	umode_t umode;
439 	unsigned long i_ino;
440 	struct inode *inode;
441 	struct v9fs_session_info *v9ses = sb->s_fs_info;
442 	int (*test)(struct inode *inode, void *data);
443 
444 	if (new)
445 		test = v9fs_test_new_inode;
446 	else
447 		test = v9fs_test_inode;
448 
449 	i_ino = v9fs_qid2ino(qid);
450 	inode = iget5_locked(sb, i_ino, test, v9fs_set_inode, st);
451 	if (!inode)
452 		return ERR_PTR(-ENOMEM);
453 	if (!(inode->i_state & I_NEW))
454 		return inode;
455 	/*
456 	 * initialize the inode with the stat info
457 	 * FIXME!! we may need support for stale inodes
458 	 * later.
459 	 */
460 	inode->i_ino = i_ino;
461 	umode = p9mode2unixmode(v9ses, st, &rdev);
462 	retval = v9fs_init_inode(v9ses, inode, umode, rdev);
463 	if (retval)
464 		goto error;
465 
466 	v9fs_stat2inode(st, inode, sb, 0);
467 	v9fs_cache_inode_get_cookie(inode);
468 	unlock_new_inode(inode);
469 	return inode;
470 error:
471 	iget_failed(inode);
472 	return ERR_PTR(retval);
473 
474 }
475 
476 struct inode *
477 v9fs_inode_from_fid(struct v9fs_session_info *v9ses, struct p9_fid *fid,
478 		    struct super_block *sb, int new)
479 {
480 	struct p9_wstat *st;
481 	struct inode *inode = NULL;
482 
483 	st = p9_client_stat(fid);
484 	if (IS_ERR(st))
485 		return ERR_CAST(st);
486 
487 	inode = v9fs_qid_iget(sb, &st->qid, st, new);
488 	p9stat_free(st);
489 	kfree(st);
490 	return inode;
491 }
492 
493 /**
494  * v9fs_at_to_dotl_flags- convert Linux specific AT flags to
495  * plan 9 AT flag.
496  * @flags: flags to convert
497  */
498 static int v9fs_at_to_dotl_flags(int flags)
499 {
500 	int rflags = 0;
501 
502 	if (flags & AT_REMOVEDIR)
503 		rflags |= P9_DOTL_AT_REMOVEDIR;
504 
505 	return rflags;
506 }
507 
508 /**
509  * v9fs_dec_count - helper functon to drop i_nlink.
510  *
511  * If a directory had nlink <= 2 (including . and ..), then we should not drop
512  * the link count, which indicates the underlying exported fs doesn't maintain
513  * nlink accurately. e.g.
514  * - overlayfs sets nlink to 1 for merged dir
515  * - ext4 (with dir_nlink feature enabled) sets nlink to 1 if a dir has more
516  *   than EXT4_LINK_MAX (65000) links.
517  *
518  * @inode: inode whose nlink is being dropped
519  */
520 static void v9fs_dec_count(struct inode *inode)
521 {
522 	if (!S_ISDIR(inode->i_mode) || inode->i_nlink > 2)
523 		drop_nlink(inode);
524 }
525 
526 /**
527  * v9fs_remove - helper function to remove files and directories
528  * @dir: directory inode that is being deleted
529  * @dentry:  dentry that is being deleted
530  * @flags: removing a directory
531  *
532  */
533 
534 static int v9fs_remove(struct inode *dir, struct dentry *dentry, int flags)
535 {
536 	struct inode *inode;
537 	int retval = -EOPNOTSUPP;
538 	struct p9_fid *v9fid, *dfid;
539 	struct v9fs_session_info *v9ses;
540 
541 	p9_debug(P9_DEBUG_VFS, "inode: %p dentry: %p rmdir: %x\n",
542 		 dir, dentry, flags);
543 
544 	v9ses = v9fs_inode2v9ses(dir);
545 	inode = d_inode(dentry);
546 	dfid = v9fs_parent_fid(dentry);
547 	if (IS_ERR(dfid)) {
548 		retval = PTR_ERR(dfid);
549 		p9_debug(P9_DEBUG_VFS, "fid lookup failed %d\n", retval);
550 		return retval;
551 	}
552 	if (v9fs_proto_dotl(v9ses))
553 		retval = p9_client_unlinkat(dfid, dentry->d_name.name,
554 					    v9fs_at_to_dotl_flags(flags));
555 	p9_fid_put(dfid);
556 	if (retval == -EOPNOTSUPP) {
557 		/* Try the one based on path */
558 		v9fid = v9fs_fid_clone(dentry);
559 		if (IS_ERR(v9fid))
560 			return PTR_ERR(v9fid);
561 		retval = p9_client_remove(v9fid);
562 	}
563 	if (!retval) {
564 		/*
565 		 * directories on unlink should have zero
566 		 * link count
567 		 */
568 		if (flags & AT_REMOVEDIR) {
569 			clear_nlink(inode);
570 			v9fs_dec_count(dir);
571 		} else
572 			v9fs_dec_count(inode);
573 
574 		v9fs_invalidate_inode_attr(inode);
575 		v9fs_invalidate_inode_attr(dir);
576 
577 		/* invalidate all fids associated with dentry */
578 		/* NOTE: This will not include open fids */
579 		dentry->d_op->d_release(dentry);
580 	}
581 	return retval;
582 }
583 
584 /**
585  * v9fs_create - Create a file
586  * @v9ses: session information
587  * @dir: directory that dentry is being created in
588  * @dentry:  dentry that is being created
589  * @extension: 9p2000.u extension string to support devices, etc.
590  * @perm: create permissions
591  * @mode: open mode
592  *
593  */
594 static struct p9_fid *
595 v9fs_create(struct v9fs_session_info *v9ses, struct inode *dir,
596 		struct dentry *dentry, char *extension, u32 perm, u8 mode)
597 {
598 	int err;
599 	const unsigned char *name;
600 	struct p9_fid *dfid, *ofid = NULL, *fid = NULL;
601 	struct inode *inode;
602 
603 	p9_debug(P9_DEBUG_VFS, "name %pd\n", dentry);
604 
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 		goto error;
619 	}
620 
621 	err = p9_client_fcreate(ofid, name, perm, mode, extension);
622 	if (err < 0) {
623 		p9_debug(P9_DEBUG_VFS, "p9_client_fcreate failed %d\n", err);
624 		goto error;
625 	}
626 
627 	if (!(perm & P9_DMLINK)) {
628 		/* now walk from the parent so we can get unopened fid */
629 		fid = p9_client_walk(dfid, 1, &name, 1);
630 		if (IS_ERR(fid)) {
631 			err = PTR_ERR(fid);
632 			p9_debug(P9_DEBUG_VFS,
633 				   "p9_client_walk failed %d\n", err);
634 			goto error;
635 		}
636 		/*
637 		 * instantiate inode and assign the unopened fid to the dentry
638 		 */
639 		inode = v9fs_get_new_inode_from_fid(v9ses, fid, dir->i_sb);
640 		if (IS_ERR(inode)) {
641 			err = PTR_ERR(inode);
642 			p9_debug(P9_DEBUG_VFS,
643 				   "inode creation failed %d\n", err);
644 			goto error;
645 		}
646 		v9fs_fid_add(dentry, &fid);
647 		d_instantiate(dentry, inode);
648 	}
649 	p9_fid_put(dfid);
650 	return ofid;
651 error:
652 	p9_fid_put(dfid);
653 	p9_fid_put(ofid);
654 	p9_fid_put(fid);
655 	return ERR_PTR(err);
656 }
657 
658 /**
659  * v9fs_vfs_create - VFS hook to create a regular file
660  * @idmap: idmap of the mount
661  * @dir: The parent directory
662  * @dentry: The name of file to be created
663  * @mode: The UNIX file mode to set
664  * @excl: True if the file must not yet exist
665  *
666  * open(.., O_CREAT) is handled in v9fs_vfs_atomic_open().  This is only called
667  * for mknod(2).
668  *
669  */
670 
671 static int
672 v9fs_vfs_create(struct mnt_idmap *idmap, struct inode *dir,
673 		struct dentry *dentry, umode_t mode, bool excl)
674 {
675 	struct v9fs_session_info *v9ses = v9fs_inode2v9ses(dir);
676 	u32 perm = unixmode2p9mode(v9ses, mode);
677 	struct p9_fid *fid;
678 
679 	/* P9_OEXCL? */
680 	fid = v9fs_create(v9ses, dir, dentry, NULL, perm, P9_ORDWR);
681 	if (IS_ERR(fid))
682 		return PTR_ERR(fid);
683 
684 	v9fs_invalidate_inode_attr(dir);
685 	p9_fid_put(fid);
686 
687 	return 0;
688 }
689 
690 /**
691  * v9fs_vfs_mkdir - VFS mkdir hook to create a directory
692  * @idmap: idmap of the mount
693  * @dir:  inode that is being unlinked
694  * @dentry: dentry that is being unlinked
695  * @mode: mode for new directory
696  *
697  */
698 
699 static int v9fs_vfs_mkdir(struct mnt_idmap *idmap, struct inode *dir,
700 			  struct dentry *dentry, umode_t mode)
701 {
702 	int err;
703 	u32 perm;
704 	struct p9_fid *fid;
705 	struct v9fs_session_info *v9ses;
706 
707 	p9_debug(P9_DEBUG_VFS, "name %pd\n", dentry);
708 	err = 0;
709 	v9ses = v9fs_inode2v9ses(dir);
710 	perm = unixmode2p9mode(v9ses, mode | S_IFDIR);
711 	fid = v9fs_create(v9ses, dir, dentry, NULL, perm, P9_OREAD);
712 	if (IS_ERR(fid)) {
713 		err = PTR_ERR(fid);
714 		fid = NULL;
715 	} else {
716 		inc_nlink(dir);
717 		v9fs_invalidate_inode_attr(dir);
718 	}
719 
720 	if (fid)
721 		p9_fid_put(fid);
722 
723 	return err;
724 }
725 
726 /**
727  * v9fs_vfs_lookup - VFS lookup hook to "walk" to a new inode
728  * @dir:  inode that is being walked from
729  * @dentry: dentry that is being walked to?
730  * @flags: lookup flags (unused)
731  *
732  */
733 
734 struct dentry *v9fs_vfs_lookup(struct inode *dir, struct dentry *dentry,
735 				      unsigned int flags)
736 {
737 	struct dentry *res;
738 	struct v9fs_session_info *v9ses;
739 	struct p9_fid *dfid, *fid;
740 	struct inode *inode;
741 	const unsigned char *name;
742 
743 	p9_debug(P9_DEBUG_VFS, "dir: %p dentry: (%pd) %p flags: %x\n",
744 		 dir, dentry, dentry, flags);
745 
746 	if (dentry->d_name.len > NAME_MAX)
747 		return ERR_PTR(-ENAMETOOLONG);
748 
749 	v9ses = v9fs_inode2v9ses(dir);
750 	/* We can walk d_parent because we hold the dir->i_mutex */
751 	dfid = v9fs_parent_fid(dentry);
752 	if (IS_ERR(dfid))
753 		return ERR_CAST(dfid);
754 
755 	/*
756 	 * Make sure we don't use a wrong inode due to parallel
757 	 * unlink. For cached mode create calls request for new
758 	 * inode. But with cache disabled, lookup should do this.
759 	 */
760 	name = dentry->d_name.name;
761 	fid = p9_client_walk(dfid, 1, &name, 1);
762 	p9_fid_put(dfid);
763 	if (fid == ERR_PTR(-ENOENT))
764 		inode = NULL;
765 	else if (IS_ERR(fid))
766 		inode = ERR_CAST(fid);
767 	else if (v9ses->cache & (CACHE_META|CACHE_LOOSE))
768 		inode = v9fs_get_inode_from_fid(v9ses, fid, dir->i_sb);
769 	else
770 		inode = v9fs_get_new_inode_from_fid(v9ses, fid, dir->i_sb);
771 	/*
772 	 * If we had a rename on the server and a parallel lookup
773 	 * for the new name, then make sure we instantiate with
774 	 * the new name. ie look up for a/b, while on server somebody
775 	 * moved b under k and client parallely did a lookup for
776 	 * k/b.
777 	 */
778 	res = d_splice_alias(inode, dentry);
779 	if (!IS_ERR(fid)) {
780 		if (!res)
781 			v9fs_fid_add(dentry, &fid);
782 		else if (!IS_ERR(res))
783 			v9fs_fid_add(res, &fid);
784 		else
785 			p9_fid_put(fid);
786 	}
787 	return res;
788 }
789 
790 static int
791 v9fs_vfs_atomic_open(struct inode *dir, struct dentry *dentry,
792 		     struct file *file, unsigned int flags, umode_t mode)
793 {
794 	int err;
795 	u32 perm;
796 	struct v9fs_inode __maybe_unused *v9inode;
797 	struct v9fs_session_info *v9ses;
798 	struct p9_fid *fid;
799 	struct dentry *res = NULL;
800 	struct inode *inode;
801 	int p9_omode;
802 
803 	if (d_in_lookup(dentry)) {
804 		res = v9fs_vfs_lookup(dir, dentry, 0);
805 		if (IS_ERR(res))
806 			return PTR_ERR(res);
807 
808 		if (res)
809 			dentry = res;
810 	}
811 
812 	/* Only creates */
813 	if (!(flags & O_CREAT) || d_really_is_positive(dentry))
814 		return finish_no_open(file, res);
815 
816 	v9ses = v9fs_inode2v9ses(dir);
817 	perm = unixmode2p9mode(v9ses, mode);
818 	p9_omode = v9fs_uflags2omode(flags, v9fs_proto_dotu(v9ses));
819 
820 	if ((v9ses->cache & CACHE_WRITEBACK) && (p9_omode & P9_OWRITE)) {
821 		p9_omode = (p9_omode & ~P9_OWRITE) | P9_ORDWR;
822 		p9_debug(P9_DEBUG_CACHE,
823 			"write-only file with writeback enabled, creating w/ O_RDWR\n");
824 	}
825 	fid = v9fs_create(v9ses, dir, dentry, NULL, perm, p9_omode);
826 	if (IS_ERR(fid)) {
827 		err = PTR_ERR(fid);
828 		goto error;
829 	}
830 
831 	v9fs_invalidate_inode_attr(dir);
832 	inode = d_inode(dentry);
833 	v9inode = V9FS_I(inode);
834 	err = finish_open(file, dentry, generic_file_open);
835 	if (err)
836 		goto error;
837 
838 	file->private_data = fid;
839 #ifdef CONFIG_9P_FSCACHE
840 	if (v9ses->cache & CACHE_FSCACHE)
841 		fscache_use_cookie(v9fs_inode_cookie(v9inode),
842 				   file->f_mode & FMODE_WRITE);
843 #endif
844 
845 	v9fs_fid_add_modes(fid, v9ses->flags, v9ses->cache, file->f_flags);
846 	v9fs_open_fid_add(inode, &fid);
847 
848 	file->f_mode |= FMODE_CREATED;
849 out:
850 	dput(res);
851 	return err;
852 
853 error:
854 	p9_fid_put(fid);
855 	goto out;
856 }
857 
858 /**
859  * v9fs_vfs_unlink - VFS unlink hook to delete an inode
860  * @i:  inode that is being unlinked
861  * @d: dentry that is being unlinked
862  *
863  */
864 
865 int v9fs_vfs_unlink(struct inode *i, struct dentry *d)
866 {
867 	return v9fs_remove(i, d, 0);
868 }
869 
870 /**
871  * v9fs_vfs_rmdir - VFS unlink hook to delete a directory
872  * @i:  inode that is being unlinked
873  * @d: dentry that is being unlinked
874  *
875  */
876 
877 int v9fs_vfs_rmdir(struct inode *i, struct dentry *d)
878 {
879 	return v9fs_remove(i, d, AT_REMOVEDIR);
880 }
881 
882 /**
883  * v9fs_vfs_rename - VFS hook to rename an inode
884  * @idmap: The idmap of the mount
885  * @old_dir:  old dir inode
886  * @old_dentry: old dentry
887  * @new_dir: new dir inode
888  * @new_dentry: new dentry
889  * @flags: RENAME_* flags
890  *
891  */
892 
893 int
894 v9fs_vfs_rename(struct mnt_idmap *idmap, struct inode *old_dir,
895 		struct dentry *old_dentry, struct inode *new_dir,
896 		struct dentry *new_dentry, unsigned int flags)
897 {
898 	int retval;
899 	struct inode *old_inode;
900 	struct inode *new_inode;
901 	struct v9fs_session_info *v9ses;
902 	struct p9_fid *oldfid = NULL, *dfid = NULL;
903 	struct p9_fid *olddirfid = NULL;
904 	struct p9_fid *newdirfid = NULL;
905 	struct p9_wstat wstat;
906 
907 	if (flags)
908 		return -EINVAL;
909 
910 	p9_debug(P9_DEBUG_VFS, "\n");
911 	old_inode = d_inode(old_dentry);
912 	new_inode = d_inode(new_dentry);
913 	v9ses = v9fs_inode2v9ses(old_inode);
914 	oldfid = v9fs_fid_lookup(old_dentry);
915 	if (IS_ERR(oldfid))
916 		return PTR_ERR(oldfid);
917 
918 	dfid = v9fs_parent_fid(old_dentry);
919 	olddirfid = clone_fid(dfid);
920 	p9_fid_put(dfid);
921 	dfid = NULL;
922 
923 	if (IS_ERR(olddirfid)) {
924 		retval = PTR_ERR(olddirfid);
925 		goto error;
926 	}
927 
928 	dfid = v9fs_parent_fid(new_dentry);
929 	newdirfid = clone_fid(dfid);
930 	p9_fid_put(dfid);
931 	dfid = NULL;
932 
933 	if (IS_ERR(newdirfid)) {
934 		retval = PTR_ERR(newdirfid);
935 		goto error;
936 	}
937 
938 	down_write(&v9ses->rename_sem);
939 	if (v9fs_proto_dotl(v9ses)) {
940 		retval = p9_client_renameat(olddirfid, old_dentry->d_name.name,
941 					    newdirfid, new_dentry->d_name.name);
942 		if (retval == -EOPNOTSUPP)
943 			retval = p9_client_rename(oldfid, newdirfid,
944 						  new_dentry->d_name.name);
945 		if (retval != -EOPNOTSUPP)
946 			goto error_locked;
947 	}
948 	if (old_dentry->d_parent != new_dentry->d_parent) {
949 		/*
950 		 * 9P .u can only handle file rename in the same directory
951 		 */
952 
953 		p9_debug(P9_DEBUG_ERROR, "old dir and new dir are different\n");
954 		retval = -EXDEV;
955 		goto error_locked;
956 	}
957 	v9fs_blank_wstat(&wstat);
958 	wstat.muid = v9ses->uname;
959 	wstat.name = new_dentry->d_name.name;
960 	retval = p9_client_wstat(oldfid, &wstat);
961 
962 error_locked:
963 	if (!retval) {
964 		if (new_inode) {
965 			if (S_ISDIR(new_inode->i_mode))
966 				clear_nlink(new_inode);
967 			else
968 				v9fs_dec_count(new_inode);
969 		}
970 		if (S_ISDIR(old_inode->i_mode)) {
971 			if (!new_inode)
972 				inc_nlink(new_dir);
973 			v9fs_dec_count(old_dir);
974 		}
975 		v9fs_invalidate_inode_attr(old_inode);
976 		v9fs_invalidate_inode_attr(old_dir);
977 		v9fs_invalidate_inode_attr(new_dir);
978 
979 		/* successful rename */
980 		d_move(old_dentry, new_dentry);
981 	}
982 	up_write(&v9ses->rename_sem);
983 
984 error:
985 	p9_fid_put(newdirfid);
986 	p9_fid_put(olddirfid);
987 	p9_fid_put(oldfid);
988 	return retval;
989 }
990 
991 /**
992  * v9fs_vfs_getattr - retrieve file metadata
993  * @idmap: idmap of the mount
994  * @path: Object to query
995  * @stat: metadata structure to populate
996  * @request_mask: Mask of STATX_xxx flags indicating the caller's interests
997  * @flags: AT_STATX_xxx setting
998  *
999  */
1000 
1001 static int
1002 v9fs_vfs_getattr(struct mnt_idmap *idmap, const struct path *path,
1003 		 struct kstat *stat, u32 request_mask, unsigned int flags)
1004 {
1005 	struct dentry *dentry = path->dentry;
1006 	struct inode *inode = d_inode(dentry);
1007 	struct v9fs_session_info *v9ses;
1008 	struct p9_fid *fid;
1009 	struct p9_wstat *st;
1010 
1011 	p9_debug(P9_DEBUG_VFS, "dentry: %p\n", dentry);
1012 	v9ses = v9fs_dentry2v9ses(dentry);
1013 	if (v9ses->cache & (CACHE_META|CACHE_LOOSE)) {
1014 		generic_fillattr(&nop_mnt_idmap, request_mask, inode, stat);
1015 		return 0;
1016 	} else if (v9ses->cache & CACHE_WRITEBACK) {
1017 		if (S_ISREG(inode->i_mode)) {
1018 			int retval = filemap_fdatawrite(inode->i_mapping);
1019 
1020 			if (retval)
1021 				p9_debug(P9_DEBUG_ERROR,
1022 				    "flushing writeback during getattr returned %d\n", retval);
1023 		}
1024 	}
1025 	fid = v9fs_fid_lookup(dentry);
1026 	if (IS_ERR(fid))
1027 		return PTR_ERR(fid);
1028 
1029 	st = p9_client_stat(fid);
1030 	p9_fid_put(fid);
1031 	if (IS_ERR(st))
1032 		return PTR_ERR(st);
1033 
1034 	v9fs_stat2inode(st, d_inode(dentry), dentry->d_sb, 0);
1035 	generic_fillattr(&nop_mnt_idmap, request_mask, d_inode(dentry), stat);
1036 
1037 	p9stat_free(st);
1038 	kfree(st);
1039 	return 0;
1040 }
1041 
1042 /**
1043  * v9fs_vfs_setattr - set file metadata
1044  * @idmap: idmap of the mount
1045  * @dentry: file whose metadata to set
1046  * @iattr: metadata assignment structure
1047  *
1048  */
1049 
1050 static int v9fs_vfs_setattr(struct mnt_idmap *idmap,
1051 			    struct dentry *dentry, struct iattr *iattr)
1052 {
1053 	int retval, use_dentry = 0;
1054 	struct inode *inode = d_inode(dentry);
1055 	struct v9fs_session_info *v9ses;
1056 	struct p9_fid *fid = NULL;
1057 	struct p9_wstat wstat;
1058 
1059 	p9_debug(P9_DEBUG_VFS, "\n");
1060 	retval = setattr_prepare(&nop_mnt_idmap, dentry, iattr);
1061 	if (retval)
1062 		return retval;
1063 
1064 	v9ses = v9fs_dentry2v9ses(dentry);
1065 	if (iattr->ia_valid & ATTR_FILE) {
1066 		fid = iattr->ia_file->private_data;
1067 		WARN_ON(!fid);
1068 	}
1069 	if (!fid) {
1070 		fid = v9fs_fid_lookup(dentry);
1071 		use_dentry = 1;
1072 	}
1073 	if (IS_ERR(fid))
1074 		return PTR_ERR(fid);
1075 
1076 	v9fs_blank_wstat(&wstat);
1077 	if (iattr->ia_valid & ATTR_MODE)
1078 		wstat.mode = unixmode2p9mode(v9ses, iattr->ia_mode);
1079 
1080 	if (iattr->ia_valid & ATTR_MTIME)
1081 		wstat.mtime = iattr->ia_mtime.tv_sec;
1082 
1083 	if (iattr->ia_valid & ATTR_ATIME)
1084 		wstat.atime = iattr->ia_atime.tv_sec;
1085 
1086 	if (iattr->ia_valid & ATTR_SIZE)
1087 		wstat.length = iattr->ia_size;
1088 
1089 	if (v9fs_proto_dotu(v9ses)) {
1090 		if (iattr->ia_valid & ATTR_UID)
1091 			wstat.n_uid = iattr->ia_uid;
1092 
1093 		if (iattr->ia_valid & ATTR_GID)
1094 			wstat.n_gid = iattr->ia_gid;
1095 	}
1096 
1097 	/* Write all dirty data */
1098 	if (d_is_reg(dentry)) {
1099 		retval = filemap_fdatawrite(inode->i_mapping);
1100 		if (retval)
1101 			p9_debug(P9_DEBUG_ERROR,
1102 			    "flushing writeback during setattr returned %d\n", retval);
1103 	}
1104 
1105 	retval = p9_client_wstat(fid, &wstat);
1106 
1107 	if (use_dentry)
1108 		p9_fid_put(fid);
1109 
1110 	if (retval < 0)
1111 		return retval;
1112 
1113 	if ((iattr->ia_valid & ATTR_SIZE) &&
1114 		 iattr->ia_size != i_size_read(inode)) {
1115 		truncate_setsize(inode, iattr->ia_size);
1116 		truncate_pagecache(inode, iattr->ia_size);
1117 
1118 #ifdef CONFIG_9P_FSCACHE
1119 		if (v9ses->cache & CACHE_FSCACHE) {
1120 			struct v9fs_inode *v9inode = V9FS_I(inode);
1121 
1122 			fscache_resize_cookie(v9fs_inode_cookie(v9inode), iattr->ia_size);
1123 		}
1124 #endif
1125 	}
1126 
1127 	v9fs_invalidate_inode_attr(inode);
1128 
1129 	setattr_copy(&nop_mnt_idmap, inode, iattr);
1130 	mark_inode_dirty(inode);
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_set_ctime(inode, stat->mtime, 0);
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 	if (!v9fs_proto_dotu(v9ses))
1232 		return ERR_PTR(-EBADF);
1233 
1234 	p9_debug(P9_DEBUG_VFS, "%pd\n", dentry);
1235 	fid = v9fs_fid_lookup(dentry);
1236 
1237 	if (IS_ERR(fid))
1238 		return ERR_CAST(fid);
1239 
1240 	st = p9_client_stat(fid);
1241 	p9_fid_put(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_fid_put(fid);
1289 	return 0;
1290 }
1291 
1292 /**
1293  * v9fs_vfs_symlink - helper function to create symlinks
1294  * @idmap: idmap 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 mnt_idmap *idmap, 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_fid_put(oldfid);
1345 	return retval;
1346 }
1347 
1348 /**
1349  * v9fs_vfs_mknod - create a special file
1350  * @idmap: idmap 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 mnt_idmap *idmap, 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: %x 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) ?
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