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