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