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