xref: /openbmc/linux/fs/9p/vfs_inode.c (revision f6d73b12ca9fd3b1c29a6a725cd751b972c740cf)
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 
unixmode2p9mode(struct v9fs_session_info * v9ses,umode_t mode)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  */
p9mode2perm(struct v9fs_session_info * v9ses,struct p9_wstat * stat)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 & 0777; /* S_IRWXUGO */
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  */
p9mode2unixmode(struct v9fs_session_info * v9ses,struct p9_wstat * stat,dev_t * rdev)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 
v9fs_uflags2omode(int uflags,int extended)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 (uflags & O_TRUNC)
182 		ret |= P9_OTRUNC;
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
v9fs_blank_wstat(struct p9_wstat * wstat)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  */
v9fs_alloc_inode(struct super_block * sb)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->cache_validity = 0;
235 	mutex_init(&v9inode->v_mutex);
236 	return &v9inode->netfs.inode;
237 }
238 
239 /**
240  * v9fs_free_inode - destroy an inode
241  * @inode: The inode to be freed
242  */
243 
v9fs_free_inode(struct inode * inode)244 void v9fs_free_inode(struct inode *inode)
245 {
246 	kmem_cache_free(v9fs_inode_cache, V9FS_I(inode));
247 }
248 
249 /*
250  * Set parameters for the netfs library
251  */
v9fs_set_netfs_context(struct inode * inode)252 void v9fs_set_netfs_context(struct inode *inode)
253 {
254 	struct v9fs_inode *v9inode = V9FS_I(inode);
255 	netfs_inode_init(&v9inode->netfs, &v9fs_req_ops);
256 }
257 
v9fs_init_inode(struct v9fs_session_info * v9ses,struct inode * inode,umode_t mode,dev_t rdev)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(&nop_mnt_idmap, inode, NULL, mode);
264 	inode->i_blocks = 0;
265 	inode->i_rdev = rdev;
266 	inode->i_atime = inode->i_mtime = inode_set_ctime_current(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 			inode->i_fop = &v9fs_file_operations_dotl;
291 		} else {
292 			inode->i_op = &v9fs_file_inode_operations;
293 			inode->i_fop = &v9fs_file_operations;
294 		}
295 
296 		break;
297 	case S_IFLNK:
298 		if (!v9fs_proto_dotu(v9ses) && !v9fs_proto_dotl(v9ses)) {
299 			p9_debug(P9_DEBUG_ERROR,
300 				 "extended modes used with legacy protocol\n");
301 			err = -EINVAL;
302 			goto error;
303 		}
304 
305 		if (v9fs_proto_dotl(v9ses))
306 			inode->i_op = &v9fs_symlink_inode_operations_dotl;
307 		else
308 			inode->i_op = &v9fs_symlink_inode_operations;
309 
310 		break;
311 	case S_IFDIR:
312 		inc_nlink(inode);
313 		if (v9fs_proto_dotl(v9ses))
314 			inode->i_op = &v9fs_dir_inode_operations_dotl;
315 		else if (v9fs_proto_dotu(v9ses))
316 			inode->i_op = &v9fs_dir_inode_operations_dotu;
317 		else
318 			inode->i_op = &v9fs_dir_inode_operations;
319 
320 		if (v9fs_proto_dotl(v9ses))
321 			inode->i_fop = &v9fs_dir_operations_dotl;
322 		else
323 			inode->i_fop = &v9fs_dir_operations;
324 
325 		break;
326 	default:
327 		p9_debug(P9_DEBUG_ERROR, "BAD mode 0x%hx S_IFMT 0x%x\n",
328 			 mode, mode & S_IFMT);
329 		err = -EINVAL;
330 		goto error;
331 	}
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 
v9fs_get_inode(struct super_block * sb,umode_t mode,dev_t rdev)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 	v9fs_set_netfs_context(inode);
364 	return inode;
365 }
366 
367 /**
368  * v9fs_evict_inode - Remove an inode from the inode cache
369  * @inode: inode to release
370  *
371  */
v9fs_evict_inode(struct inode * inode)372 void v9fs_evict_inode(struct inode *inode)
373 {
374 	struct v9fs_inode __maybe_unused *v9inode = V9FS_I(inode);
375 	__le32 __maybe_unused version;
376 
377 	if (!is_bad_inode(inode)) {
378 		truncate_inode_pages_final(&inode->i_data);
379 
380 #ifdef CONFIG_9P_FSCACHE
381 		version = cpu_to_le32(v9inode->qid.version);
382 		fscache_clear_inode_writeback(v9fs_inode_cookie(v9inode), inode,
383 				      &version);
384 #endif
385 		clear_inode(inode);
386 		filemap_fdatawrite(&inode->i_data);
387 
388 #ifdef CONFIG_9P_FSCACHE
389 		if (v9fs_inode_cookie(v9inode))
390 			fscache_relinquish_cookie(v9fs_inode_cookie(v9inode), false);
391 #endif
392 	} else
393 		clear_inode(inode);
394 }
395 
v9fs_test_inode(struct inode * inode,void * data)396 static int v9fs_test_inode(struct inode *inode, void *data)
397 {
398 	int umode;
399 	dev_t rdev;
400 	struct v9fs_inode *v9inode = V9FS_I(inode);
401 	struct p9_wstat *st = (struct p9_wstat *)data;
402 	struct v9fs_session_info *v9ses = v9fs_inode2v9ses(inode);
403 
404 	umode = p9mode2unixmode(v9ses, st, &rdev);
405 	/* don't match inode of different type */
406 	if (inode_wrong_type(inode, umode))
407 		return 0;
408 
409 	/* compare qid details */
410 	if (memcmp(&v9inode->qid.version,
411 		   &st->qid.version, sizeof(v9inode->qid.version)))
412 		return 0;
413 
414 	if (v9inode->qid.type != st->qid.type)
415 		return 0;
416 
417 	if (v9inode->qid.path != st->qid.path)
418 		return 0;
419 	return 1;
420 }
421 
v9fs_test_new_inode(struct inode * inode,void * data)422 static int v9fs_test_new_inode(struct inode *inode, void *data)
423 {
424 	return 0;
425 }
426 
v9fs_set_inode(struct inode * inode,void * data)427 static int v9fs_set_inode(struct inode *inode,  void *data)
428 {
429 	struct v9fs_inode *v9inode = V9FS_I(inode);
430 	struct p9_wstat *st = (struct p9_wstat *)data;
431 
432 	memcpy(&v9inode->qid, &st->qid, sizeof(st->qid));
433 	return 0;
434 }
435 
v9fs_qid_iget(struct super_block * sb,struct p9_qid * qid,struct p9_wstat * st,int new)436 static struct inode *v9fs_qid_iget(struct super_block *sb,
437 				   struct p9_qid *qid,
438 				   struct p9_wstat *st,
439 				   int new)
440 {
441 	dev_t rdev;
442 	int retval;
443 	umode_t umode;
444 	unsigned long i_ino;
445 	struct inode *inode;
446 	struct v9fs_session_info *v9ses = sb->s_fs_info;
447 	int (*test)(struct inode *inode, void *data);
448 
449 	if (new)
450 		test = v9fs_test_new_inode;
451 	else
452 		test = v9fs_test_inode;
453 
454 	i_ino = v9fs_qid2ino(qid);
455 	inode = iget5_locked(sb, i_ino, test, v9fs_set_inode, st);
456 	if (!inode)
457 		return ERR_PTR(-ENOMEM);
458 	if (!(inode->i_state & I_NEW))
459 		return inode;
460 	/*
461 	 * initialize the inode with the stat info
462 	 * FIXME!! we may need support for stale inodes
463 	 * later.
464 	 */
465 	inode->i_ino = i_ino;
466 	umode = p9mode2unixmode(v9ses, st, &rdev);
467 	retval = v9fs_init_inode(v9ses, inode, umode, rdev);
468 	if (retval)
469 		goto error;
470 
471 	v9fs_stat2inode(st, inode, sb, 0);
472 	v9fs_set_netfs_context(inode);
473 	v9fs_cache_inode_get_cookie(inode);
474 	unlock_new_inode(inode);
475 	return inode;
476 error:
477 	iget_failed(inode);
478 	return ERR_PTR(retval);
479 
480 }
481 
482 struct inode *
v9fs_inode_from_fid(struct v9fs_session_info * v9ses,struct p9_fid * fid,struct super_block * sb,int new)483 v9fs_inode_from_fid(struct v9fs_session_info *v9ses, struct p9_fid *fid,
484 		    struct super_block *sb, int new)
485 {
486 	struct p9_wstat *st;
487 	struct inode *inode = NULL;
488 
489 	st = p9_client_stat(fid);
490 	if (IS_ERR(st))
491 		return ERR_CAST(st);
492 
493 	inode = v9fs_qid_iget(sb, &st->qid, st, new);
494 	p9stat_free(st);
495 	kfree(st);
496 	return inode;
497 }
498 
499 /**
500  * v9fs_at_to_dotl_flags- convert Linux specific AT flags to
501  * plan 9 AT flag.
502  * @flags: flags to convert
503  */
v9fs_at_to_dotl_flags(int flags)504 static int v9fs_at_to_dotl_flags(int flags)
505 {
506 	int rflags = 0;
507 
508 	if (flags & AT_REMOVEDIR)
509 		rflags |= P9_DOTL_AT_REMOVEDIR;
510 
511 	return rflags;
512 }
513 
514 /**
515  * v9fs_dec_count - helper functon to drop i_nlink.
516  *
517  * If a directory had nlink <= 2 (including . and ..), then we should not drop
518  * the link count, which indicates the underlying exported fs doesn't maintain
519  * nlink accurately. e.g.
520  * - overlayfs sets nlink to 1 for merged dir
521  * - ext4 (with dir_nlink feature enabled) sets nlink to 1 if a dir has more
522  *   than EXT4_LINK_MAX (65000) links.
523  *
524  * @inode: inode whose nlink is being dropped
525  */
v9fs_dec_count(struct inode * inode)526 static void v9fs_dec_count(struct inode *inode)
527 {
528 	if (!S_ISDIR(inode->i_mode) || inode->i_nlink > 2)
529 		drop_nlink(inode);
530 }
531 
532 /**
533  * v9fs_remove - helper function to remove files and directories
534  * @dir: directory inode that is being deleted
535  * @dentry:  dentry that is being deleted
536  * @flags: removing a directory
537  *
538  */
539 
v9fs_remove(struct inode * dir,struct dentry * dentry,int flags)540 static int v9fs_remove(struct inode *dir, struct dentry *dentry, int flags)
541 {
542 	struct inode *inode;
543 	int retval = -EOPNOTSUPP;
544 	struct p9_fid *v9fid, *dfid;
545 	struct v9fs_session_info *v9ses;
546 
547 	p9_debug(P9_DEBUG_VFS, "inode: %p dentry: %p rmdir: %x\n",
548 		 dir, dentry, flags);
549 
550 	v9ses = v9fs_inode2v9ses(dir);
551 	inode = d_inode(dentry);
552 	dfid = v9fs_parent_fid(dentry);
553 	if (IS_ERR(dfid)) {
554 		retval = PTR_ERR(dfid);
555 		p9_debug(P9_DEBUG_VFS, "fid lookup failed %d\n", retval);
556 		return retval;
557 	}
558 	if (v9fs_proto_dotl(v9ses))
559 		retval = p9_client_unlinkat(dfid, dentry->d_name.name,
560 					    v9fs_at_to_dotl_flags(flags));
561 	p9_fid_put(dfid);
562 	if (retval == -EOPNOTSUPP) {
563 		/* Try the one based on path */
564 		v9fid = v9fs_fid_clone(dentry);
565 		if (IS_ERR(v9fid))
566 			return PTR_ERR(v9fid);
567 		retval = p9_client_remove(v9fid);
568 	}
569 	if (!retval) {
570 		/*
571 		 * directories on unlink should have zero
572 		 * link count
573 		 */
574 		if (flags & AT_REMOVEDIR) {
575 			clear_nlink(inode);
576 			v9fs_dec_count(dir);
577 		} else
578 			v9fs_dec_count(inode);
579 
580 		v9fs_invalidate_inode_attr(inode);
581 		v9fs_invalidate_inode_attr(dir);
582 
583 		/* invalidate all fids associated with dentry */
584 		/* NOTE: This will not include open fids */
585 		dentry->d_op->d_release(dentry);
586 	}
587 	return retval;
588 }
589 
590 /**
591  * v9fs_create - Create a file
592  * @v9ses: session information
593  * @dir: directory that dentry is being created in
594  * @dentry:  dentry that is being created
595  * @extension: 9p2000.u extension string to support devices, etc.
596  * @perm: create permissions
597  * @mode: open mode
598  *
599  */
600 static struct p9_fid *
v9fs_create(struct v9fs_session_info * v9ses,struct inode * dir,struct dentry * dentry,char * extension,u32 perm,u8 mode)601 v9fs_create(struct v9fs_session_info *v9ses, struct inode *dir,
602 		struct dentry *dentry, char *extension, u32 perm, u8 mode)
603 {
604 	int err;
605 	const unsigned char *name;
606 	struct p9_fid *dfid, *ofid = NULL, *fid = NULL;
607 	struct inode *inode;
608 
609 	p9_debug(P9_DEBUG_VFS, "name %pd\n", dentry);
610 
611 	name = dentry->d_name.name;
612 	dfid = v9fs_parent_fid(dentry);
613 	if (IS_ERR(dfid)) {
614 		err = PTR_ERR(dfid);
615 		p9_debug(P9_DEBUG_VFS, "fid lookup failed %d\n", err);
616 		return ERR_PTR(err);
617 	}
618 
619 	/* clone a fid to use for creation */
620 	ofid = clone_fid(dfid);
621 	if (IS_ERR(ofid)) {
622 		err = PTR_ERR(ofid);
623 		p9_debug(P9_DEBUG_VFS, "p9_client_walk failed %d\n", err);
624 		goto error;
625 	}
626 
627 	err = p9_client_fcreate(ofid, name, perm, mode, extension);
628 	if (err < 0) {
629 		p9_debug(P9_DEBUG_VFS, "p9_client_fcreate failed %d\n", err);
630 		goto error;
631 	}
632 
633 	if (!(perm & P9_DMLINK)) {
634 		/* now walk from the parent so we can get unopened fid */
635 		fid = p9_client_walk(dfid, 1, &name, 1);
636 		if (IS_ERR(fid)) {
637 			err = PTR_ERR(fid);
638 			p9_debug(P9_DEBUG_VFS,
639 				   "p9_client_walk failed %d\n", err);
640 			goto error;
641 		}
642 		/*
643 		 * instantiate inode and assign the unopened fid to the dentry
644 		 */
645 		inode = v9fs_get_new_inode_from_fid(v9ses, fid, dir->i_sb);
646 		if (IS_ERR(inode)) {
647 			err = PTR_ERR(inode);
648 			p9_debug(P9_DEBUG_VFS,
649 				   "inode creation failed %d\n", err);
650 			goto error;
651 		}
652 		v9fs_fid_add(dentry, &fid);
653 		d_instantiate(dentry, inode);
654 	}
655 	p9_fid_put(dfid);
656 	return ofid;
657 error:
658 	p9_fid_put(dfid);
659 	p9_fid_put(ofid);
660 	p9_fid_put(fid);
661 	return ERR_PTR(err);
662 }
663 
664 /**
665  * v9fs_vfs_create - VFS hook to create a regular file
666  * @idmap: idmap of the mount
667  * @dir: The parent directory
668  * @dentry: The name of file to be created
669  * @mode: The UNIX file mode to set
670  * @excl: True if the file must not yet exist
671  *
672  * open(.., O_CREAT) is handled in v9fs_vfs_atomic_open().  This is only called
673  * for mknod(2).
674  *
675  */
676 
677 static int
v9fs_vfs_create(struct mnt_idmap * idmap,struct inode * dir,struct dentry * dentry,umode_t mode,bool excl)678 v9fs_vfs_create(struct mnt_idmap *idmap, struct inode *dir,
679 		struct dentry *dentry, umode_t mode, bool excl)
680 {
681 	struct v9fs_session_info *v9ses = v9fs_inode2v9ses(dir);
682 	u32 perm = unixmode2p9mode(v9ses, mode);
683 	struct p9_fid *fid;
684 
685 	/* P9_OEXCL? */
686 	fid = v9fs_create(v9ses, dir, dentry, NULL, perm, P9_ORDWR);
687 	if (IS_ERR(fid))
688 		return PTR_ERR(fid);
689 
690 	v9fs_invalidate_inode_attr(dir);
691 	p9_fid_put(fid);
692 
693 	return 0;
694 }
695 
696 /**
697  * v9fs_vfs_mkdir - VFS mkdir hook to create a directory
698  * @idmap: idmap of the mount
699  * @dir:  inode that is being unlinked
700  * @dentry: dentry that is being unlinked
701  * @mode: mode for new directory
702  *
703  */
704 
v9fs_vfs_mkdir(struct mnt_idmap * idmap,struct inode * dir,struct dentry * dentry,umode_t mode)705 static int v9fs_vfs_mkdir(struct mnt_idmap *idmap, struct inode *dir,
706 			  struct dentry *dentry, umode_t mode)
707 {
708 	int err;
709 	u32 perm;
710 	struct p9_fid *fid;
711 	struct v9fs_session_info *v9ses;
712 
713 	p9_debug(P9_DEBUG_VFS, "name %pd\n", dentry);
714 	err = 0;
715 	v9ses = v9fs_inode2v9ses(dir);
716 	perm = unixmode2p9mode(v9ses, mode | S_IFDIR);
717 	fid = v9fs_create(v9ses, dir, dentry, NULL, perm, P9_OREAD);
718 	if (IS_ERR(fid)) {
719 		err = PTR_ERR(fid);
720 		fid = NULL;
721 	} else {
722 		inc_nlink(dir);
723 		v9fs_invalidate_inode_attr(dir);
724 	}
725 
726 	if (fid)
727 		p9_fid_put(fid);
728 
729 	return err;
730 }
731 
732 /**
733  * v9fs_vfs_lookup - VFS lookup hook to "walk" to a new inode
734  * @dir:  inode that is being walked from
735  * @dentry: dentry that is being walked to?
736  * @flags: lookup flags (unused)
737  *
738  */
739 
v9fs_vfs_lookup(struct inode * dir,struct dentry * dentry,unsigned int flags)740 struct dentry *v9fs_vfs_lookup(struct inode *dir, struct dentry *dentry,
741 				      unsigned int flags)
742 {
743 	struct dentry *res;
744 	struct v9fs_session_info *v9ses;
745 	struct p9_fid *dfid, *fid;
746 	struct inode *inode;
747 	const unsigned char *name;
748 
749 	p9_debug(P9_DEBUG_VFS, "dir: %p dentry: (%pd) %p flags: %x\n",
750 		 dir, dentry, dentry, flags);
751 
752 	if (dentry->d_name.len > NAME_MAX)
753 		return ERR_PTR(-ENAMETOOLONG);
754 
755 	v9ses = v9fs_inode2v9ses(dir);
756 	/* We can walk d_parent because we hold the dir->i_mutex */
757 	dfid = v9fs_parent_fid(dentry);
758 	if (IS_ERR(dfid))
759 		return ERR_CAST(dfid);
760 
761 	/*
762 	 * Make sure we don't use a wrong inode due to parallel
763 	 * unlink. For cached mode create calls request for new
764 	 * inode. But with cache disabled, lookup should do this.
765 	 */
766 	name = dentry->d_name.name;
767 	fid = p9_client_walk(dfid, 1, &name, 1);
768 	p9_fid_put(dfid);
769 	if (fid == ERR_PTR(-ENOENT))
770 		inode = NULL;
771 	else if (IS_ERR(fid))
772 		inode = ERR_CAST(fid);
773 	else if (v9ses->cache & (CACHE_META|CACHE_LOOSE))
774 		inode = v9fs_get_inode_from_fid(v9ses, fid, dir->i_sb);
775 	else
776 		inode = v9fs_get_new_inode_from_fid(v9ses, fid, dir->i_sb);
777 	/*
778 	 * If we had a rename on the server and a parallel lookup
779 	 * for the new name, then make sure we instantiate with
780 	 * the new name. ie look up for a/b, while on server somebody
781 	 * moved b under k and client parallely did a lookup for
782 	 * k/b.
783 	 */
784 	res = d_splice_alias(inode, dentry);
785 	if (!IS_ERR(fid)) {
786 		if (!res)
787 			v9fs_fid_add(dentry, &fid);
788 		else if (!IS_ERR(res))
789 			v9fs_fid_add(res, &fid);
790 		else
791 			p9_fid_put(fid);
792 	}
793 	return res;
794 }
795 
796 static int
v9fs_vfs_atomic_open(struct inode * dir,struct dentry * dentry,struct file * file,unsigned int flags,umode_t mode)797 v9fs_vfs_atomic_open(struct inode *dir, struct dentry *dentry,
798 		     struct file *file, unsigned int flags, umode_t mode)
799 {
800 	int err;
801 	u32 perm;
802 	struct v9fs_inode __maybe_unused *v9inode;
803 	struct v9fs_session_info *v9ses;
804 	struct p9_fid *fid;
805 	struct dentry *res = NULL;
806 	struct inode *inode;
807 	int p9_omode;
808 
809 	if (d_in_lookup(dentry)) {
810 		res = v9fs_vfs_lookup(dir, dentry, 0);
811 		if (IS_ERR(res))
812 			return PTR_ERR(res);
813 
814 		if (res)
815 			dentry = res;
816 	}
817 
818 	/* Only creates */
819 	if (!(flags & O_CREAT) || d_really_is_positive(dentry))
820 		return finish_no_open(file, res);
821 
822 	v9ses = v9fs_inode2v9ses(dir);
823 	perm = unixmode2p9mode(v9ses, mode);
824 	p9_omode = v9fs_uflags2omode(flags, v9fs_proto_dotu(v9ses));
825 
826 	if ((v9ses->cache & CACHE_WRITEBACK) && (p9_omode & P9_OWRITE)) {
827 		p9_omode = (p9_omode & ~P9_OWRITE) | P9_ORDWR;
828 		p9_debug(P9_DEBUG_CACHE,
829 			"write-only file with writeback enabled, creating w/ O_RDWR\n");
830 	}
831 	fid = v9fs_create(v9ses, dir, dentry, NULL, perm, p9_omode);
832 	if (IS_ERR(fid)) {
833 		err = PTR_ERR(fid);
834 		goto error;
835 	}
836 
837 	v9fs_invalidate_inode_attr(dir);
838 	inode = d_inode(dentry);
839 	v9inode = V9FS_I(inode);
840 	err = finish_open(file, dentry, generic_file_open);
841 	if (err)
842 		goto error;
843 
844 	file->private_data = fid;
845 #ifdef CONFIG_9P_FSCACHE
846 	if (v9ses->cache & CACHE_FSCACHE)
847 		fscache_use_cookie(v9fs_inode_cookie(v9inode),
848 				   file->f_mode & FMODE_WRITE);
849 #endif
850 
851 	v9fs_fid_add_modes(fid, v9ses->flags, v9ses->cache, file->f_flags);
852 	v9fs_open_fid_add(inode, &fid);
853 
854 	file->f_mode |= FMODE_CREATED;
855 out:
856 	dput(res);
857 	return err;
858 
859 error:
860 	p9_fid_put(fid);
861 	goto out;
862 }
863 
864 /**
865  * v9fs_vfs_unlink - VFS unlink hook to delete an inode
866  * @i:  inode that is being unlinked
867  * @d: dentry that is being unlinked
868  *
869  */
870 
v9fs_vfs_unlink(struct inode * i,struct dentry * d)871 int v9fs_vfs_unlink(struct inode *i, struct dentry *d)
872 {
873 	return v9fs_remove(i, d, 0);
874 }
875 
876 /**
877  * v9fs_vfs_rmdir - VFS unlink hook to delete a directory
878  * @i:  inode that is being unlinked
879  * @d: dentry that is being unlinked
880  *
881  */
882 
v9fs_vfs_rmdir(struct inode * i,struct dentry * d)883 int v9fs_vfs_rmdir(struct inode *i, struct dentry *d)
884 {
885 	return v9fs_remove(i, d, AT_REMOVEDIR);
886 }
887 
888 /**
889  * v9fs_vfs_rename - VFS hook to rename an inode
890  * @idmap: The idmap of the mount
891  * @old_dir:  old dir inode
892  * @old_dentry: old dentry
893  * @new_dir: new dir inode
894  * @new_dentry: new dentry
895  * @flags: RENAME_* flags
896  *
897  */
898 
899 int
v9fs_vfs_rename(struct mnt_idmap * idmap,struct inode * old_dir,struct dentry * old_dentry,struct inode * new_dir,struct dentry * new_dentry,unsigned int flags)900 v9fs_vfs_rename(struct mnt_idmap *idmap, struct inode *old_dir,
901 		struct dentry *old_dentry, struct inode *new_dir,
902 		struct dentry *new_dentry, unsigned int flags)
903 {
904 	int retval;
905 	struct inode *old_inode;
906 	struct inode *new_inode;
907 	struct v9fs_session_info *v9ses;
908 	struct p9_fid *oldfid = NULL, *dfid = NULL;
909 	struct p9_fid *olddirfid = NULL;
910 	struct p9_fid *newdirfid = NULL;
911 	struct p9_wstat wstat;
912 
913 	if (flags)
914 		return -EINVAL;
915 
916 	p9_debug(P9_DEBUG_VFS, "\n");
917 	old_inode = d_inode(old_dentry);
918 	new_inode = d_inode(new_dentry);
919 	v9ses = v9fs_inode2v9ses(old_inode);
920 	oldfid = v9fs_fid_lookup(old_dentry);
921 	if (IS_ERR(oldfid))
922 		return PTR_ERR(oldfid);
923 
924 	dfid = v9fs_parent_fid(old_dentry);
925 	olddirfid = clone_fid(dfid);
926 	p9_fid_put(dfid);
927 	dfid = NULL;
928 
929 	if (IS_ERR(olddirfid)) {
930 		retval = PTR_ERR(olddirfid);
931 		goto error;
932 	}
933 
934 	dfid = v9fs_parent_fid(new_dentry);
935 	newdirfid = clone_fid(dfid);
936 	p9_fid_put(dfid);
937 	dfid = NULL;
938 
939 	if (IS_ERR(newdirfid)) {
940 		retval = PTR_ERR(newdirfid);
941 		goto error;
942 	}
943 
944 	down_write(&v9ses->rename_sem);
945 	if (v9fs_proto_dotl(v9ses)) {
946 		retval = p9_client_renameat(olddirfid, old_dentry->d_name.name,
947 					    newdirfid, new_dentry->d_name.name);
948 		if (retval == -EOPNOTSUPP)
949 			retval = p9_client_rename(oldfid, newdirfid,
950 						  new_dentry->d_name.name);
951 		if (retval != -EOPNOTSUPP)
952 			goto error_locked;
953 	}
954 	if (old_dentry->d_parent != new_dentry->d_parent) {
955 		/*
956 		 * 9P .u can only handle file rename in the same directory
957 		 */
958 
959 		p9_debug(P9_DEBUG_ERROR, "old dir and new dir are different\n");
960 		retval = -EXDEV;
961 		goto error_locked;
962 	}
963 	v9fs_blank_wstat(&wstat);
964 	wstat.muid = v9ses->uname;
965 	wstat.name = new_dentry->d_name.name;
966 	retval = p9_client_wstat(oldfid, &wstat);
967 
968 error_locked:
969 	if (!retval) {
970 		if (new_inode) {
971 			if (S_ISDIR(new_inode->i_mode))
972 				clear_nlink(new_inode);
973 			else
974 				v9fs_dec_count(new_inode);
975 		}
976 		if (S_ISDIR(old_inode->i_mode)) {
977 			if (!new_inode)
978 				inc_nlink(new_dir);
979 			v9fs_dec_count(old_dir);
980 		}
981 		v9fs_invalidate_inode_attr(old_inode);
982 		v9fs_invalidate_inode_attr(old_dir);
983 		v9fs_invalidate_inode_attr(new_dir);
984 
985 		/* successful rename */
986 		d_move(old_dentry, new_dentry);
987 	}
988 	up_write(&v9ses->rename_sem);
989 
990 error:
991 	p9_fid_put(newdirfid);
992 	p9_fid_put(olddirfid);
993 	p9_fid_put(oldfid);
994 	return retval;
995 }
996 
997 /**
998  * v9fs_vfs_getattr - retrieve file metadata
999  * @idmap: idmap of the mount
1000  * @path: Object to query
1001  * @stat: metadata structure to populate
1002  * @request_mask: Mask of STATX_xxx flags indicating the caller's interests
1003  * @flags: AT_STATX_xxx setting
1004  *
1005  */
1006 
1007 static int
v9fs_vfs_getattr(struct mnt_idmap * idmap,const struct path * path,struct kstat * stat,u32 request_mask,unsigned int flags)1008 v9fs_vfs_getattr(struct mnt_idmap *idmap, const struct path *path,
1009 		 struct kstat *stat, u32 request_mask, unsigned int flags)
1010 {
1011 	struct dentry *dentry = path->dentry;
1012 	struct inode *inode = d_inode(dentry);
1013 	struct v9fs_session_info *v9ses;
1014 	struct p9_fid *fid;
1015 	struct p9_wstat *st;
1016 
1017 	p9_debug(P9_DEBUG_VFS, "dentry: %p\n", dentry);
1018 	v9ses = v9fs_dentry2v9ses(dentry);
1019 	if (v9ses->cache & (CACHE_META|CACHE_LOOSE)) {
1020 		generic_fillattr(&nop_mnt_idmap, request_mask, inode, stat);
1021 		return 0;
1022 	} else if (v9ses->cache & CACHE_WRITEBACK) {
1023 		if (S_ISREG(inode->i_mode)) {
1024 			int retval = filemap_fdatawrite(inode->i_mapping);
1025 
1026 			if (retval)
1027 				p9_debug(P9_DEBUG_ERROR,
1028 				    "flushing writeback during getattr returned %d\n", retval);
1029 		}
1030 	}
1031 	fid = v9fs_fid_lookup(dentry);
1032 	if (IS_ERR(fid))
1033 		return PTR_ERR(fid);
1034 
1035 	st = p9_client_stat(fid);
1036 	p9_fid_put(fid);
1037 	if (IS_ERR(st))
1038 		return PTR_ERR(st);
1039 
1040 	v9fs_stat2inode(st, d_inode(dentry), dentry->d_sb, 0);
1041 	generic_fillattr(&nop_mnt_idmap, request_mask, d_inode(dentry), stat);
1042 
1043 	p9stat_free(st);
1044 	kfree(st);
1045 	return 0;
1046 }
1047 
1048 /**
1049  * v9fs_vfs_setattr - set file metadata
1050  * @idmap: idmap of the mount
1051  * @dentry: file whose metadata to set
1052  * @iattr: metadata assignment structure
1053  *
1054  */
1055 
v9fs_vfs_setattr(struct mnt_idmap * idmap,struct dentry * dentry,struct iattr * iattr)1056 static int v9fs_vfs_setattr(struct mnt_idmap *idmap,
1057 			    struct dentry *dentry, struct iattr *iattr)
1058 {
1059 	int retval, use_dentry = 0;
1060 	struct inode *inode = d_inode(dentry);
1061 	struct v9fs_session_info *v9ses;
1062 	struct p9_fid *fid = NULL;
1063 	struct p9_wstat wstat;
1064 
1065 	p9_debug(P9_DEBUG_VFS, "\n");
1066 	retval = setattr_prepare(&nop_mnt_idmap, dentry, iattr);
1067 	if (retval)
1068 		return retval;
1069 
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
v9fs_stat2inode(struct p9_wstat * stat,struct inode * inode,struct super_block * sb,unsigned int flags)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_set_ctime(inode, stat->mtime, 0);
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 
v9fs_qid2ino(struct p9_qid * qid)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 
v9fs_vfs_get_link(struct dentry * dentry,struct inode * inode,struct delayed_call * done)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 
v9fs_vfs_mkspecial(struct inode * dir,struct dentry * dentry,u32 perm,const char * extension)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
v9fs_vfs_symlink(struct mnt_idmap * idmap,struct inode * dir,struct dentry * dentry,const char * symname)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
v9fs_vfs_link(struct dentry * old_dentry,struct inode * dir,struct dentry * dentry)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
v9fs_vfs_mknod(struct mnt_idmap * idmap,struct inode * dir,struct dentry * dentry,umode_t mode,dev_t rdev)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 
v9fs_refresh_inode(struct p9_fid * fid,struct inode * inode)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