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