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