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