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