• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  *  linux/fs/9p/vfs_inode_dotl.c
4  *
5  * This file contains vfs inode ops for the 9P2000.L 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 #include <linux/module.h>
12 #include <linux/errno.h>
13 #include <linux/fs.h>
14 #include <linux/file.h>
15 #include <linux/pagemap.h>
16 #include <linux/stat.h>
17 #include <linux/string.h>
18 #include <linux/inet.h>
19 #include <linux/namei.h>
20 #include <linux/idr.h>
21 #include <linux/sched.h>
22 #include <linux/slab.h>
23 #include <linux/xattr.h>
24 #include <linux/posix_acl.h>
25 #include <net/9p/9p.h>
26 #include <net/9p/client.h>
27 
28 #include "v9fs.h"
29 #include "v9fs_vfs.h"
30 #include "fid.h"
31 #include "cache.h"
32 #include "xattr.h"
33 #include "acl.h"
34 
35 static int
36 v9fs_vfs_mknod_dotl(struct user_namespace *mnt_userns, struct inode *dir,
37 		    struct dentry *dentry, umode_t omode, dev_t rdev);
38 
39 /**
40  * v9fs_get_fsgid_for_create - Helper function to get the gid for a new object
41  * @dir_inode: The directory inode
42  *
43  * Helper function to get the gid for creating a
44  * new file system object. This checks the S_ISGID to determine the owning
45  * group of the new file system object.
46  */
47 
v9fs_get_fsgid_for_create(struct inode * dir_inode)48 static kgid_t v9fs_get_fsgid_for_create(struct inode *dir_inode)
49 {
50 	BUG_ON(dir_inode == NULL);
51 
52 	if (dir_inode->i_mode & S_ISGID) {
53 		/* set_gid bit is set.*/
54 		return dir_inode->i_gid;
55 	}
56 	return current_fsgid();
57 }
58 
v9fs_test_inode_dotl(struct inode * inode,void * data)59 static int v9fs_test_inode_dotl(struct inode *inode, void *data)
60 {
61 	struct v9fs_inode *v9inode = V9FS_I(inode);
62 	struct p9_stat_dotl *st = (struct p9_stat_dotl *)data;
63 
64 	/* don't match inode of different type */
65 	if (inode_wrong_type(inode, st->st_mode))
66 		return 0;
67 
68 	if (inode->i_generation != st->st_gen)
69 		return 0;
70 
71 	/* compare qid details */
72 	if (memcmp(&v9inode->qid.version,
73 		   &st->qid.version, sizeof(v9inode->qid.version)))
74 		return 0;
75 
76 	if (v9inode->qid.type != st->qid.type)
77 		return 0;
78 
79 	if (v9inode->qid.path != st->qid.path)
80 		return 0;
81 	return 1;
82 }
83 
84 /* Always get a new inode */
v9fs_test_new_inode_dotl(struct inode * inode,void * data)85 static int v9fs_test_new_inode_dotl(struct inode *inode, void *data)
86 {
87 	return 0;
88 }
89 
v9fs_set_inode_dotl(struct inode * inode,void * data)90 static int v9fs_set_inode_dotl(struct inode *inode,  void *data)
91 {
92 	struct v9fs_inode *v9inode = V9FS_I(inode);
93 	struct p9_stat_dotl *st = (struct p9_stat_dotl *)data;
94 
95 	memcpy(&v9inode->qid, &st->qid, sizeof(st->qid));
96 	inode->i_generation = st->st_gen;
97 	return 0;
98 }
99 
v9fs_qid_iget_dotl(struct super_block * sb,struct p9_qid * qid,struct p9_fid * fid,struct p9_stat_dotl * st,int new)100 static struct inode *v9fs_qid_iget_dotl(struct super_block *sb,
101 					struct p9_qid *qid,
102 					struct p9_fid *fid,
103 					struct p9_stat_dotl *st,
104 					int new)
105 {
106 	int retval;
107 	unsigned long i_ino;
108 	struct inode *inode;
109 	struct v9fs_session_info *v9ses = sb->s_fs_info;
110 	int (*test)(struct inode *inode, void *data);
111 
112 	if (new)
113 		test = v9fs_test_new_inode_dotl;
114 	else
115 		test = v9fs_test_inode_dotl;
116 
117 	i_ino = v9fs_qid2ino(qid);
118 	inode = iget5_locked(sb, i_ino, test, v9fs_set_inode_dotl, st);
119 	if (!inode)
120 		return ERR_PTR(-ENOMEM);
121 	if (!(inode->i_state & I_NEW))
122 		return inode;
123 	/*
124 	 * initialize the inode with the stat info
125 	 * FIXME!! we may need support for stale inodes
126 	 * later.
127 	 */
128 	inode->i_ino = i_ino;
129 	retval = v9fs_init_inode(v9ses, inode,
130 				 st->st_mode, new_decode_dev(st->st_rdev));
131 	if (retval)
132 		goto error;
133 
134 	v9fs_stat2inode_dotl(st, inode, 0);
135 	v9fs_cache_inode_get_cookie(inode);
136 	retval = v9fs_get_acl(inode, fid);
137 	if (retval)
138 		goto error;
139 
140 	unlock_new_inode(inode);
141 	return inode;
142 error:
143 	iget_failed(inode);
144 	return ERR_PTR(retval);
145 
146 }
147 
148 struct inode *
v9fs_inode_from_fid_dotl(struct v9fs_session_info * v9ses,struct p9_fid * fid,struct super_block * sb,int new)149 v9fs_inode_from_fid_dotl(struct v9fs_session_info *v9ses, struct p9_fid *fid,
150 			 struct super_block *sb, int new)
151 {
152 	struct p9_stat_dotl *st;
153 	struct inode *inode = NULL;
154 
155 	st = p9_client_getattr_dotl(fid, P9_STATS_BASIC | P9_STATS_GEN);
156 	if (IS_ERR(st))
157 		return ERR_CAST(st);
158 
159 	inode = v9fs_qid_iget_dotl(sb, &st->qid, fid, st, new);
160 	kfree(st);
161 	return inode;
162 }
163 
164 struct dotl_openflag_map {
165 	int open_flag;
166 	int dotl_flag;
167 };
168 
v9fs_mapped_dotl_flags(int flags)169 static int v9fs_mapped_dotl_flags(int flags)
170 {
171 	int i;
172 	int rflags = 0;
173 	struct dotl_openflag_map dotl_oflag_map[] = {
174 		{ O_CREAT,	P9_DOTL_CREATE },
175 		{ O_EXCL,	P9_DOTL_EXCL },
176 		{ O_NOCTTY,	P9_DOTL_NOCTTY },
177 		{ O_APPEND,	P9_DOTL_APPEND },
178 		{ O_NONBLOCK,	P9_DOTL_NONBLOCK },
179 		{ O_DSYNC,	P9_DOTL_DSYNC },
180 		{ FASYNC,	P9_DOTL_FASYNC },
181 		{ O_DIRECT,	P9_DOTL_DIRECT },
182 		{ O_LARGEFILE,	P9_DOTL_LARGEFILE },
183 		{ O_DIRECTORY,	P9_DOTL_DIRECTORY },
184 		{ O_NOFOLLOW,	P9_DOTL_NOFOLLOW },
185 		{ O_NOATIME,	P9_DOTL_NOATIME },
186 		{ O_CLOEXEC,	P9_DOTL_CLOEXEC },
187 		{ O_SYNC,	P9_DOTL_SYNC},
188 	};
189 	for (i = 0; i < ARRAY_SIZE(dotl_oflag_map); i++) {
190 		if (flags & dotl_oflag_map[i].open_flag)
191 			rflags |= dotl_oflag_map[i].dotl_flag;
192 	}
193 	return rflags;
194 }
195 
196 /**
197  * v9fs_open_to_dotl_flags- convert Linux specific open flags to
198  * plan 9 open flag.
199  * @flags: flags to convert
200  */
v9fs_open_to_dotl_flags(int flags)201 int v9fs_open_to_dotl_flags(int flags)
202 {
203 	int rflags = 0;
204 
205 	/*
206 	 * We have same bits for P9_DOTL_READONLY, P9_DOTL_WRONLY
207 	 * and P9_DOTL_NOACCESS
208 	 */
209 	rflags |= flags & O_ACCMODE;
210 	rflags |= v9fs_mapped_dotl_flags(flags);
211 
212 	return rflags;
213 }
214 
215 /**
216  * v9fs_vfs_create_dotl - VFS hook to create files for 9P2000.L protocol.
217  * @mnt_userns: The user namespace of the mount
218  * @dir: directory inode that is being created
219  * @dentry:  dentry that is being deleted
220  * @omode: create permissions
221  * @excl: True if the file must not yet exist
222  *
223  */
224 static int
v9fs_vfs_create_dotl(struct user_namespace * mnt_userns,struct inode * dir,struct dentry * dentry,umode_t omode,bool excl)225 v9fs_vfs_create_dotl(struct user_namespace *mnt_userns, struct inode *dir,
226 		     struct dentry *dentry, umode_t omode, bool excl)
227 {
228 	return v9fs_vfs_mknod_dotl(mnt_userns, dir, dentry, omode, 0);
229 }
230 
231 static int
v9fs_vfs_atomic_open_dotl(struct inode * dir,struct dentry * dentry,struct file * file,unsigned int flags,umode_t omode)232 v9fs_vfs_atomic_open_dotl(struct inode *dir, struct dentry *dentry,
233 			  struct file *file, unsigned int flags, umode_t omode)
234 {
235 	int err = 0;
236 	kgid_t gid;
237 	umode_t mode;
238 	const unsigned char *name = NULL;
239 	struct p9_qid qid;
240 	struct inode *inode;
241 	struct p9_fid *fid = NULL;
242 	struct v9fs_inode *v9inode;
243 	struct p9_fid *dfid, *ofid, *inode_fid;
244 	struct v9fs_session_info *v9ses;
245 	struct posix_acl *pacl = NULL, *dacl = NULL;
246 	struct dentry *res = NULL;
247 
248 	if (d_in_lookup(dentry)) {
249 		res = v9fs_vfs_lookup(dir, dentry, 0);
250 		if (IS_ERR(res))
251 			return PTR_ERR(res);
252 
253 		if (res)
254 			dentry = res;
255 	}
256 
257 	/* Only creates */
258 	if (!(flags & O_CREAT) || d_really_is_positive(dentry))
259 		return	finish_no_open(file, res);
260 
261 	v9ses = v9fs_inode2v9ses(dir);
262 
263 	name = dentry->d_name.name;
264 	p9_debug(P9_DEBUG_VFS, "name:%s flags:0x%x mode:0x%x\n",
265 		 name, flags, omode);
266 
267 	dfid = v9fs_parent_fid(dentry);
268 	if (IS_ERR(dfid)) {
269 		err = PTR_ERR(dfid);
270 		p9_debug(P9_DEBUG_VFS, "fid lookup failed %d\n", err);
271 		goto out;
272 	}
273 
274 	/* clone a fid to use for creation */
275 	ofid = clone_fid(dfid);
276 	if (IS_ERR(ofid)) {
277 		err = PTR_ERR(ofid);
278 		p9_debug(P9_DEBUG_VFS, "p9_client_walk failed %d\n", err);
279 		p9_client_clunk(dfid);
280 		goto out;
281 	}
282 
283 	gid = v9fs_get_fsgid_for_create(dir);
284 
285 	mode = omode;
286 	/* Update mode based on ACL value */
287 	err = v9fs_acl_mode(dir, &mode, &dacl, &pacl);
288 	if (err) {
289 		p9_debug(P9_DEBUG_VFS, "Failed to get acl values in creat %d\n",
290 			 err);
291 		p9_client_clunk(dfid);
292 		goto error;
293 	}
294 	err = p9_client_create_dotl(ofid, name, v9fs_open_to_dotl_flags(flags),
295 				    mode, gid, &qid);
296 	if (err < 0) {
297 		p9_debug(P9_DEBUG_VFS, "p9_client_open_dotl failed in creat %d\n",
298 			 err);
299 		p9_client_clunk(dfid);
300 		goto error;
301 	}
302 	v9fs_invalidate_inode_attr(dir);
303 
304 	/* instantiate inode and assign the unopened fid to the dentry */
305 	fid = p9_client_walk(dfid, 1, &name, 1);
306 	p9_client_clunk(dfid);
307 	if (IS_ERR(fid)) {
308 		err = PTR_ERR(fid);
309 		p9_debug(P9_DEBUG_VFS, "p9_client_walk failed %d\n", err);
310 		fid = NULL;
311 		goto error;
312 	}
313 	inode = v9fs_get_new_inode_from_fid(v9ses, fid, dir->i_sb);
314 	if (IS_ERR(inode)) {
315 		err = PTR_ERR(inode);
316 		p9_debug(P9_DEBUG_VFS, "inode creation failed %d\n", err);
317 		goto error;
318 	}
319 	/* Now set the ACL based on the default value */
320 	v9fs_set_create_acl(inode, fid, dacl, pacl);
321 
322 	v9fs_fid_add(dentry, fid);
323 	d_instantiate(dentry, inode);
324 
325 	v9inode = V9FS_I(inode);
326 	mutex_lock(&v9inode->v_mutex);
327 	if ((v9ses->cache == CACHE_LOOSE || v9ses->cache == CACHE_FSCACHE) &&
328 	    !v9inode->writeback_fid &&
329 	    ((flags & O_ACCMODE) != O_RDONLY)) {
330 		/*
331 		 * clone a fid and add it to writeback_fid
332 		 * we do it during open time instead of
333 		 * page dirty time via write_begin/page_mkwrite
334 		 * because we want write after unlink usecase
335 		 * to work.
336 		 */
337 		inode_fid = v9fs_writeback_fid(dentry);
338 		if (IS_ERR(inode_fid)) {
339 			err = PTR_ERR(inode_fid);
340 			mutex_unlock(&v9inode->v_mutex);
341 			goto err_clunk_old_fid;
342 		}
343 		v9inode->writeback_fid = (void *) inode_fid;
344 	}
345 	mutex_unlock(&v9inode->v_mutex);
346 	/* Since we are opening a file, assign the open fid to the file */
347 	err = finish_open(file, dentry, generic_file_open);
348 	if (err)
349 		goto err_clunk_old_fid;
350 	file->private_data = ofid;
351 	if (v9ses->cache == CACHE_LOOSE || v9ses->cache == CACHE_FSCACHE)
352 		v9fs_cache_inode_set_cookie(inode, file);
353 	v9fs_open_fid_add(inode, ofid);
354 	file->f_mode |= FMODE_CREATED;
355 out:
356 	v9fs_put_acl(dacl, pacl);
357 	dput(res);
358 	return err;
359 
360 error:
361 	if (fid)
362 		p9_client_clunk(fid);
363 err_clunk_old_fid:
364 	if (ofid)
365 		p9_client_clunk(ofid);
366 	goto out;
367 }
368 
369 /**
370  * v9fs_vfs_mkdir_dotl - VFS mkdir hook to create a directory
371  * @mnt_userns: The user namespace of the mount
372  * @dir:  inode that is being unlinked
373  * @dentry: dentry that is being unlinked
374  * @omode: mode for new directory
375  *
376  */
377 
v9fs_vfs_mkdir_dotl(struct user_namespace * mnt_userns,struct inode * dir,struct dentry * dentry,umode_t omode)378 static int v9fs_vfs_mkdir_dotl(struct user_namespace *mnt_userns,
379 			       struct inode *dir, struct dentry *dentry,
380 			       umode_t omode)
381 {
382 	int err;
383 	struct v9fs_session_info *v9ses;
384 	struct p9_fid *fid = NULL, *dfid = NULL;
385 	kgid_t gid;
386 	const unsigned char *name;
387 	umode_t mode;
388 	struct inode *inode;
389 	struct p9_qid qid;
390 	struct posix_acl *dacl = NULL, *pacl = NULL;
391 
392 	p9_debug(P9_DEBUG_VFS, "name %pd\n", dentry);
393 	err = 0;
394 	v9ses = v9fs_inode2v9ses(dir);
395 
396 	omode |= S_IFDIR;
397 	if (dir->i_mode & S_ISGID)
398 		omode |= S_ISGID;
399 
400 	dfid = v9fs_parent_fid(dentry);
401 	if (IS_ERR(dfid)) {
402 		err = PTR_ERR(dfid);
403 		p9_debug(P9_DEBUG_VFS, "fid lookup failed %d\n", err);
404 		dfid = NULL;
405 		goto error;
406 	}
407 
408 	gid = v9fs_get_fsgid_for_create(dir);
409 	mode = omode;
410 	/* Update mode based on ACL value */
411 	err = v9fs_acl_mode(dir, &mode, &dacl, &pacl);
412 	if (err) {
413 		p9_debug(P9_DEBUG_VFS, "Failed to get acl values in mkdir %d\n",
414 			 err);
415 		goto error;
416 	}
417 	name = dentry->d_name.name;
418 	err = p9_client_mkdir_dotl(dfid, name, mode, gid, &qid);
419 	if (err < 0)
420 		goto error;
421 	fid = p9_client_walk(dfid, 1, &name, 1);
422 	if (IS_ERR(fid)) {
423 		err = PTR_ERR(fid);
424 		p9_debug(P9_DEBUG_VFS, "p9_client_walk failed %d\n",
425 			 err);
426 		fid = NULL;
427 		goto error;
428 	}
429 
430 	/* instantiate inode and assign the unopened fid to the dentry */
431 	if (v9ses->cache == CACHE_LOOSE || v9ses->cache == CACHE_FSCACHE) {
432 		inode = v9fs_get_new_inode_from_fid(v9ses, fid, dir->i_sb);
433 		if (IS_ERR(inode)) {
434 			err = PTR_ERR(inode);
435 			p9_debug(P9_DEBUG_VFS, "inode creation failed %d\n",
436 				 err);
437 			goto error;
438 		}
439 		v9fs_fid_add(dentry, fid);
440 		v9fs_set_create_acl(inode, fid, dacl, pacl);
441 		d_instantiate(dentry, inode);
442 		fid = NULL;
443 		err = 0;
444 	} else {
445 		/*
446 		 * Not in cached mode. No need to populate
447 		 * inode with stat. We need to get an inode
448 		 * so that we can set the acl with dentry
449 		 */
450 		inode = v9fs_get_inode(dir->i_sb, mode, 0);
451 		if (IS_ERR(inode)) {
452 			err = PTR_ERR(inode);
453 			goto error;
454 		}
455 		v9fs_set_create_acl(inode, fid, dacl, pacl);
456 		d_instantiate(dentry, inode);
457 	}
458 	inc_nlink(dir);
459 	v9fs_invalidate_inode_attr(dir);
460 error:
461 	if (fid)
462 		p9_client_clunk(fid);
463 	v9fs_put_acl(dacl, pacl);
464 	p9_client_clunk(dfid);
465 	return err;
466 }
467 
468 static int
v9fs_vfs_getattr_dotl(struct user_namespace * mnt_userns,const struct path * path,struct kstat * stat,u32 request_mask,unsigned int flags)469 v9fs_vfs_getattr_dotl(struct user_namespace *mnt_userns,
470 		      const struct path *path, struct kstat *stat,
471 		      u32 request_mask, unsigned int flags)
472 {
473 	struct dentry *dentry = path->dentry;
474 	struct v9fs_session_info *v9ses;
475 	struct p9_fid *fid;
476 	struct p9_stat_dotl *st;
477 
478 	p9_debug(P9_DEBUG_VFS, "dentry: %p\n", dentry);
479 	v9ses = v9fs_dentry2v9ses(dentry);
480 	if (v9ses->cache == CACHE_LOOSE || v9ses->cache == CACHE_FSCACHE) {
481 		generic_fillattr(&init_user_ns, d_inode(dentry), stat);
482 		return 0;
483 	}
484 	fid = v9fs_fid_lookup(dentry);
485 	if (IS_ERR(fid))
486 		return PTR_ERR(fid);
487 
488 	/* Ask for all the fields in stat structure. Server will return
489 	 * whatever it supports
490 	 */
491 
492 	st = p9_client_getattr_dotl(fid, P9_STATS_ALL);
493 	p9_client_clunk(fid);
494 	if (IS_ERR(st))
495 		return PTR_ERR(st);
496 
497 	v9fs_stat2inode_dotl(st, d_inode(dentry), 0);
498 	generic_fillattr(&init_user_ns, d_inode(dentry), stat);
499 	/* Change block size to what the server returned */
500 	stat->blksize = st->st_blksize;
501 
502 	kfree(st);
503 	return 0;
504 }
505 
506 /*
507  * Attribute flags.
508  */
509 #define P9_ATTR_MODE		(1 << 0)
510 #define P9_ATTR_UID		(1 << 1)
511 #define P9_ATTR_GID		(1 << 2)
512 #define P9_ATTR_SIZE		(1 << 3)
513 #define P9_ATTR_ATIME		(1 << 4)
514 #define P9_ATTR_MTIME		(1 << 5)
515 #define P9_ATTR_CTIME		(1 << 6)
516 #define P9_ATTR_ATIME_SET	(1 << 7)
517 #define P9_ATTR_MTIME_SET	(1 << 8)
518 
519 struct dotl_iattr_map {
520 	int iattr_valid;
521 	int p9_iattr_valid;
522 };
523 
v9fs_mapped_iattr_valid(int iattr_valid)524 static int v9fs_mapped_iattr_valid(int iattr_valid)
525 {
526 	int i;
527 	int p9_iattr_valid = 0;
528 	struct dotl_iattr_map dotl_iattr_map[] = {
529 		{ ATTR_MODE,		P9_ATTR_MODE },
530 		{ ATTR_UID,		P9_ATTR_UID },
531 		{ ATTR_GID,		P9_ATTR_GID },
532 		{ ATTR_SIZE,		P9_ATTR_SIZE },
533 		{ ATTR_ATIME,		P9_ATTR_ATIME },
534 		{ ATTR_MTIME,		P9_ATTR_MTIME },
535 		{ ATTR_CTIME,		P9_ATTR_CTIME },
536 		{ ATTR_ATIME_SET,	P9_ATTR_ATIME_SET },
537 		{ ATTR_MTIME_SET,	P9_ATTR_MTIME_SET },
538 	};
539 	for (i = 0; i < ARRAY_SIZE(dotl_iattr_map); i++) {
540 		if (iattr_valid & dotl_iattr_map[i].iattr_valid)
541 			p9_iattr_valid |= dotl_iattr_map[i].p9_iattr_valid;
542 	}
543 	return p9_iattr_valid;
544 }
545 
546 /**
547  * v9fs_vfs_setattr_dotl - set file metadata
548  * @mnt_userns: The user namespace of the mount
549  * @dentry: file whose metadata to set
550  * @iattr: metadata assignment structure
551  *
552  */
553 
v9fs_vfs_setattr_dotl(struct user_namespace * mnt_userns,struct dentry * dentry,struct iattr * iattr)554 int v9fs_vfs_setattr_dotl(struct user_namespace *mnt_userns,
555 			  struct dentry *dentry, struct iattr *iattr)
556 {
557 	int retval, use_dentry = 0;
558 	struct p9_fid *fid = NULL;
559 	struct p9_iattr_dotl p9attr = {
560 		.uid = INVALID_UID,
561 		.gid = INVALID_GID,
562 	};
563 	struct inode *inode = d_inode(dentry);
564 
565 	p9_debug(P9_DEBUG_VFS, "\n");
566 
567 	retval = setattr_prepare(&init_user_ns, dentry, iattr);
568 	if (retval)
569 		return retval;
570 
571 	p9attr.valid = v9fs_mapped_iattr_valid(iattr->ia_valid);
572 	if (iattr->ia_valid & ATTR_MODE)
573 		p9attr.mode = iattr->ia_mode;
574 	if (iattr->ia_valid & ATTR_UID)
575 		p9attr.uid = iattr->ia_uid;
576 	if (iattr->ia_valid & ATTR_GID)
577 		p9attr.gid = iattr->ia_gid;
578 	if (iattr->ia_valid & ATTR_SIZE)
579 		p9attr.size = iattr->ia_size;
580 	if (iattr->ia_valid & ATTR_ATIME_SET) {
581 		p9attr.atime_sec = iattr->ia_atime.tv_sec;
582 		p9attr.atime_nsec = iattr->ia_atime.tv_nsec;
583 	}
584 	if (iattr->ia_valid & ATTR_MTIME_SET) {
585 		p9attr.mtime_sec = iattr->ia_mtime.tv_sec;
586 		p9attr.mtime_nsec = iattr->ia_mtime.tv_nsec;
587 	}
588 
589 	if (iattr->ia_valid & ATTR_FILE) {
590 		fid = iattr->ia_file->private_data;
591 		WARN_ON(!fid);
592 	}
593 	if (!fid) {
594 		fid = v9fs_fid_lookup(dentry);
595 		use_dentry = 1;
596 	}
597 	if (IS_ERR(fid))
598 		return PTR_ERR(fid);
599 
600 	/* Write all dirty data */
601 	if (S_ISREG(inode->i_mode))
602 		filemap_write_and_wait(inode->i_mapping);
603 
604 	retval = p9_client_setattr(fid, &p9attr);
605 	if (retval < 0) {
606 		if (use_dentry)
607 			p9_client_clunk(fid);
608 		return retval;
609 	}
610 
611 	if ((iattr->ia_valid & ATTR_SIZE) &&
612 	    iattr->ia_size != i_size_read(inode))
613 		truncate_setsize(inode, iattr->ia_size);
614 
615 	v9fs_invalidate_inode_attr(inode);
616 	setattr_copy(&init_user_ns, inode, iattr);
617 	mark_inode_dirty(inode);
618 	if (iattr->ia_valid & ATTR_MODE) {
619 		/* We also want to update ACL when we update mode bits */
620 		retval = v9fs_acl_chmod(inode, fid);
621 		if (retval < 0) {
622 			if (use_dentry)
623 				p9_client_clunk(fid);
624 			return retval;
625 		}
626 	}
627 	if (use_dentry)
628 		p9_client_clunk(fid);
629 
630 	return 0;
631 }
632 
633 /**
634  * v9fs_stat2inode_dotl - populate an inode structure with stat info
635  * @stat: stat structure
636  * @inode: inode to populate
637  * @flags: ctrl flags (e.g. V9FS_STAT2INODE_KEEP_ISIZE)
638  *
639  */
640 
641 void
v9fs_stat2inode_dotl(struct p9_stat_dotl * stat,struct inode * inode,unsigned int flags)642 v9fs_stat2inode_dotl(struct p9_stat_dotl *stat, struct inode *inode,
643 		      unsigned int flags)
644 {
645 	umode_t mode;
646 	struct v9fs_inode *v9inode = V9FS_I(inode);
647 
648 	if ((stat->st_result_mask & P9_STATS_BASIC) == P9_STATS_BASIC) {
649 		inode->i_atime.tv_sec = stat->st_atime_sec;
650 		inode->i_atime.tv_nsec = stat->st_atime_nsec;
651 		inode->i_mtime.tv_sec = stat->st_mtime_sec;
652 		inode->i_mtime.tv_nsec = stat->st_mtime_nsec;
653 		inode->i_ctime.tv_sec = stat->st_ctime_sec;
654 		inode->i_ctime.tv_nsec = stat->st_ctime_nsec;
655 		inode->i_uid = stat->st_uid;
656 		inode->i_gid = stat->st_gid;
657 		set_nlink(inode, stat->st_nlink);
658 
659 		mode = stat->st_mode & S_IALLUGO;
660 		mode |= inode->i_mode & ~S_IALLUGO;
661 		inode->i_mode = mode;
662 
663 		if (!(flags & V9FS_STAT2INODE_KEEP_ISIZE))
664 			v9fs_i_size_write(inode, stat->st_size);
665 		inode->i_blocks = stat->st_blocks;
666 	} else {
667 		if (stat->st_result_mask & P9_STATS_ATIME) {
668 			inode->i_atime.tv_sec = stat->st_atime_sec;
669 			inode->i_atime.tv_nsec = stat->st_atime_nsec;
670 		}
671 		if (stat->st_result_mask & P9_STATS_MTIME) {
672 			inode->i_mtime.tv_sec = stat->st_mtime_sec;
673 			inode->i_mtime.tv_nsec = stat->st_mtime_nsec;
674 		}
675 		if (stat->st_result_mask & P9_STATS_CTIME) {
676 			inode->i_ctime.tv_sec = stat->st_ctime_sec;
677 			inode->i_ctime.tv_nsec = stat->st_ctime_nsec;
678 		}
679 		if (stat->st_result_mask & P9_STATS_UID)
680 			inode->i_uid = stat->st_uid;
681 		if (stat->st_result_mask & P9_STATS_GID)
682 			inode->i_gid = stat->st_gid;
683 		if (stat->st_result_mask & P9_STATS_NLINK)
684 			set_nlink(inode, stat->st_nlink);
685 		if (stat->st_result_mask & P9_STATS_MODE) {
686 			mode = stat->st_mode & S_IALLUGO;
687 			mode |= inode->i_mode & ~S_IALLUGO;
688 			inode->i_mode = mode;
689 		}
690 		if (!(flags & V9FS_STAT2INODE_KEEP_ISIZE) &&
691 		    stat->st_result_mask & P9_STATS_SIZE)
692 			v9fs_i_size_write(inode, stat->st_size);
693 		if (stat->st_result_mask & P9_STATS_BLOCKS)
694 			inode->i_blocks = stat->st_blocks;
695 	}
696 	if (stat->st_result_mask & P9_STATS_GEN)
697 		inode->i_generation = stat->st_gen;
698 
699 	/* Currently we don't support P9_STATS_BTIME and P9_STATS_DATA_VERSION
700 	 * because the inode structure does not have fields for them.
701 	 */
702 	v9inode->cache_validity &= ~V9FS_INO_INVALID_ATTR;
703 }
704 
705 static int
v9fs_vfs_symlink_dotl(struct user_namespace * mnt_userns,struct inode * dir,struct dentry * dentry,const char * symname)706 v9fs_vfs_symlink_dotl(struct user_namespace *mnt_userns, struct inode *dir,
707 		      struct dentry *dentry, const char *symname)
708 {
709 	int err;
710 	kgid_t gid;
711 	const unsigned char *name;
712 	struct p9_qid qid;
713 	struct inode *inode;
714 	struct p9_fid *dfid;
715 	struct p9_fid *fid = NULL;
716 	struct v9fs_session_info *v9ses;
717 
718 	name = dentry->d_name.name;
719 	p9_debug(P9_DEBUG_VFS, "%lu,%s,%s\n", dir->i_ino, name, symname);
720 	v9ses = v9fs_inode2v9ses(dir);
721 
722 	dfid = v9fs_parent_fid(dentry);
723 	if (IS_ERR(dfid)) {
724 		err = PTR_ERR(dfid);
725 		p9_debug(P9_DEBUG_VFS, "fid lookup failed %d\n", err);
726 		return err;
727 	}
728 
729 	gid = v9fs_get_fsgid_for_create(dir);
730 
731 	/* Server doesn't alter fid on TSYMLINK. Hence no need to clone it. */
732 	err = p9_client_symlink(dfid, name, symname, gid, &qid);
733 
734 	if (err < 0) {
735 		p9_debug(P9_DEBUG_VFS, "p9_client_symlink failed %d\n", err);
736 		goto error;
737 	}
738 
739 	v9fs_invalidate_inode_attr(dir);
740 	if (v9ses->cache == CACHE_LOOSE || v9ses->cache == CACHE_FSCACHE) {
741 		/* Now walk from the parent so we can get an unopened fid. */
742 		fid = p9_client_walk(dfid, 1, &name, 1);
743 		if (IS_ERR(fid)) {
744 			err = PTR_ERR(fid);
745 			p9_debug(P9_DEBUG_VFS, "p9_client_walk failed %d\n",
746 				 err);
747 			fid = NULL;
748 			goto error;
749 		}
750 
751 		/* instantiate inode and assign the unopened fid to dentry */
752 		inode = v9fs_get_new_inode_from_fid(v9ses, fid, dir->i_sb);
753 		if (IS_ERR(inode)) {
754 			err = PTR_ERR(inode);
755 			p9_debug(P9_DEBUG_VFS, "inode creation failed %d\n",
756 				 err);
757 			goto error;
758 		}
759 		v9fs_fid_add(dentry, fid);
760 		d_instantiate(dentry, inode);
761 		fid = NULL;
762 		err = 0;
763 	} else {
764 		/* Not in cached mode. No need to populate inode with stat */
765 		inode = v9fs_get_inode(dir->i_sb, S_IFLNK, 0);
766 		if (IS_ERR(inode)) {
767 			err = PTR_ERR(inode);
768 			goto error;
769 		}
770 		d_instantiate(dentry, inode);
771 	}
772 
773 error:
774 	if (fid)
775 		p9_client_clunk(fid);
776 
777 	p9_client_clunk(dfid);
778 	return err;
779 }
780 
781 /**
782  * v9fs_vfs_link_dotl - create a hardlink for dotl
783  * @old_dentry: dentry for file to link to
784  * @dir: inode destination for new link
785  * @dentry: dentry for link
786  *
787  */
788 
789 static int
v9fs_vfs_link_dotl(struct dentry * old_dentry,struct inode * dir,struct dentry * dentry)790 v9fs_vfs_link_dotl(struct dentry *old_dentry, struct inode *dir,
791 		struct dentry *dentry)
792 {
793 	int err;
794 	struct p9_fid *dfid, *oldfid;
795 	struct v9fs_session_info *v9ses;
796 
797 	p9_debug(P9_DEBUG_VFS, "dir ino: %lu, old_name: %pd, new_name: %pd\n",
798 		 dir->i_ino, old_dentry, dentry);
799 
800 	v9ses = v9fs_inode2v9ses(dir);
801 	dfid = v9fs_parent_fid(dentry);
802 	if (IS_ERR(dfid))
803 		return PTR_ERR(dfid);
804 
805 	oldfid = v9fs_fid_lookup(old_dentry);
806 	if (IS_ERR(oldfid)) {
807 		p9_client_clunk(dfid);
808 		return PTR_ERR(oldfid);
809 	}
810 
811 	err = p9_client_link(dfid, oldfid, dentry->d_name.name);
812 
813 	p9_client_clunk(dfid);
814 	p9_client_clunk(oldfid);
815 	if (err < 0) {
816 		p9_debug(P9_DEBUG_VFS, "p9_client_link failed %d\n", err);
817 		return err;
818 	}
819 
820 	v9fs_invalidate_inode_attr(dir);
821 	if (v9ses->cache == CACHE_LOOSE || v9ses->cache == CACHE_FSCACHE) {
822 		/* Get the latest stat info from server. */
823 		struct p9_fid *fid;
824 
825 		fid = v9fs_fid_lookup(old_dentry);
826 		if (IS_ERR(fid))
827 			return PTR_ERR(fid);
828 
829 		v9fs_refresh_inode_dotl(fid, d_inode(old_dentry));
830 		p9_client_clunk(fid);
831 	}
832 	ihold(d_inode(old_dentry));
833 	d_instantiate(dentry, d_inode(old_dentry));
834 
835 	return err;
836 }
837 
838 /**
839  * v9fs_vfs_mknod_dotl - create a special file
840  * @mnt_userns: The user namespace of the mount
841  * @dir: inode destination for new link
842  * @dentry: dentry for file
843  * @omode: mode for creation
844  * @rdev: device associated with special file
845  *
846  */
847 static int
v9fs_vfs_mknod_dotl(struct user_namespace * mnt_userns,struct inode * dir,struct dentry * dentry,umode_t omode,dev_t rdev)848 v9fs_vfs_mknod_dotl(struct user_namespace *mnt_userns, struct inode *dir,
849 		    struct dentry *dentry, umode_t omode, dev_t rdev)
850 {
851 	int err;
852 	kgid_t gid;
853 	const unsigned char *name;
854 	umode_t mode;
855 	struct v9fs_session_info *v9ses;
856 	struct p9_fid *fid = NULL, *dfid = NULL;
857 	struct inode *inode;
858 	struct p9_qid qid;
859 	struct posix_acl *dacl = NULL, *pacl = NULL;
860 
861 	p9_debug(P9_DEBUG_VFS, " %lu,%pd mode: %x MAJOR: %u MINOR: %u\n",
862 		 dir->i_ino, dentry, omode,
863 		 MAJOR(rdev), MINOR(rdev));
864 
865 	v9ses = v9fs_inode2v9ses(dir);
866 	dfid = v9fs_parent_fid(dentry);
867 	if (IS_ERR(dfid)) {
868 		err = PTR_ERR(dfid);
869 		p9_debug(P9_DEBUG_VFS, "fid lookup failed %d\n", err);
870 		dfid = NULL;
871 		goto error;
872 	}
873 
874 	gid = v9fs_get_fsgid_for_create(dir);
875 	mode = omode;
876 	/* Update mode based on ACL value */
877 	err = v9fs_acl_mode(dir, &mode, &dacl, &pacl);
878 	if (err) {
879 		p9_debug(P9_DEBUG_VFS, "Failed to get acl values in mknod %d\n",
880 			 err);
881 		goto error;
882 	}
883 	name = dentry->d_name.name;
884 
885 	err = p9_client_mknod_dotl(dfid, name, mode, rdev, gid, &qid);
886 	if (err < 0)
887 		goto error;
888 
889 	v9fs_invalidate_inode_attr(dir);
890 	fid = p9_client_walk(dfid, 1, &name, 1);
891 	if (IS_ERR(fid)) {
892 		err = PTR_ERR(fid);
893 		p9_debug(P9_DEBUG_VFS, "p9_client_walk failed %d\n",
894 			 err);
895 		fid = NULL;
896 		goto error;
897 	}
898 
899 	/* instantiate inode and assign the unopened fid to the dentry */
900 	if (v9ses->cache == CACHE_LOOSE || v9ses->cache == CACHE_FSCACHE) {
901 		inode = v9fs_get_new_inode_from_fid(v9ses, fid, dir->i_sb);
902 		if (IS_ERR(inode)) {
903 			err = PTR_ERR(inode);
904 			p9_debug(P9_DEBUG_VFS, "inode creation failed %d\n",
905 				 err);
906 			goto error;
907 		}
908 		v9fs_set_create_acl(inode, fid, dacl, pacl);
909 		v9fs_fid_add(dentry, fid);
910 		d_instantiate(dentry, inode);
911 		fid = NULL;
912 		err = 0;
913 	} else {
914 		/*
915 		 * Not in cached mode. No need to populate inode with stat.
916 		 * socket syscall returns a fd, so we need instantiate
917 		 */
918 		inode = v9fs_get_inode(dir->i_sb, mode, rdev);
919 		if (IS_ERR(inode)) {
920 			err = PTR_ERR(inode);
921 			goto error;
922 		}
923 		v9fs_set_create_acl(inode, fid, dacl, pacl);
924 		d_instantiate(dentry, inode);
925 	}
926 error:
927 	if (fid)
928 		p9_client_clunk(fid);
929 	v9fs_put_acl(dacl, pacl);
930 	p9_client_clunk(dfid);
931 
932 	return err;
933 }
934 
935 /**
936  * v9fs_vfs_get_link_dotl - follow a symlink path
937  * @dentry: dentry for symlink
938  * @inode: inode for symlink
939  * @done: destructor for return value
940  */
941 
942 static const char *
v9fs_vfs_get_link_dotl(struct dentry * dentry,struct inode * inode,struct delayed_call * done)943 v9fs_vfs_get_link_dotl(struct dentry *dentry,
944 		       struct inode *inode,
945 		       struct delayed_call *done)
946 {
947 	struct p9_fid *fid;
948 	char *target;
949 	int retval;
950 
951 	if (!dentry)
952 		return ERR_PTR(-ECHILD);
953 
954 	p9_debug(P9_DEBUG_VFS, "%pd\n", dentry);
955 
956 	fid = v9fs_fid_lookup(dentry);
957 	if (IS_ERR(fid))
958 		return ERR_CAST(fid);
959 	retval = p9_client_readlink(fid, &target);
960 	p9_client_clunk(fid);
961 	if (retval)
962 		return ERR_PTR(retval);
963 	set_delayed_call(done, kfree_link, target);
964 	return target;
965 }
966 
v9fs_refresh_inode_dotl(struct p9_fid * fid,struct inode * inode)967 int v9fs_refresh_inode_dotl(struct p9_fid *fid, struct inode *inode)
968 {
969 	struct p9_stat_dotl *st;
970 	struct v9fs_session_info *v9ses;
971 	unsigned int flags;
972 
973 	v9ses = v9fs_inode2v9ses(inode);
974 	st = p9_client_getattr_dotl(fid, P9_STATS_ALL);
975 	if (IS_ERR(st))
976 		return PTR_ERR(st);
977 	/*
978 	 * Don't update inode if the file type is different
979 	 */
980 	if (inode_wrong_type(inode, st->st_mode))
981 		goto out;
982 
983 	/*
984 	 * We don't want to refresh inode->i_size,
985 	 * because we may have cached data
986 	 */
987 	flags = (v9ses->cache == CACHE_LOOSE || v9ses->cache == CACHE_FSCACHE) ?
988 		V9FS_STAT2INODE_KEEP_ISIZE : 0;
989 	v9fs_stat2inode_dotl(st, inode, flags);
990 out:
991 	kfree(st);
992 	return 0;
993 }
994 
995 const struct inode_operations v9fs_dir_inode_operations_dotl = {
996 	.create = v9fs_vfs_create_dotl,
997 	.atomic_open = v9fs_vfs_atomic_open_dotl,
998 	.lookup = v9fs_vfs_lookup,
999 	.link = v9fs_vfs_link_dotl,
1000 	.symlink = v9fs_vfs_symlink_dotl,
1001 	.unlink = v9fs_vfs_unlink,
1002 	.mkdir = v9fs_vfs_mkdir_dotl,
1003 	.rmdir = v9fs_vfs_rmdir,
1004 	.mknod = v9fs_vfs_mknod_dotl,
1005 	.rename = v9fs_vfs_rename,
1006 	.getattr = v9fs_vfs_getattr_dotl,
1007 	.setattr = v9fs_vfs_setattr_dotl,
1008 	.listxattr = v9fs_listxattr,
1009 	.get_acl = v9fs_iop_get_acl,
1010 };
1011 
1012 const struct inode_operations v9fs_file_inode_operations_dotl = {
1013 	.getattr = v9fs_vfs_getattr_dotl,
1014 	.setattr = v9fs_vfs_setattr_dotl,
1015 	.listxattr = v9fs_listxattr,
1016 	.get_acl = v9fs_iop_get_acl,
1017 };
1018 
1019 const struct inode_operations v9fs_symlink_inode_operations_dotl = {
1020 	.get_link = v9fs_vfs_get_link_dotl,
1021 	.getattr = v9fs_vfs_getattr_dotl,
1022 	.setattr = v9fs_vfs_setattr_dotl,
1023 	.listxattr = v9fs_listxattr,
1024 };
1025