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