Documentation: embargoed-hardware-issues.rst: Add myself for Power
[sfrench/cifs-2.6.git] / fs / 9p / vfs_inode.c
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * This file contains vfs inode ops for the 9P2000 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 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
10
11 #include <linux/module.h>
12 #include <linux/errno.h>
13 #include <linux/fs.h>
14 #include <linux/file.h>
15 #include <linux/pagemap.h>
16 #include <linux/stat.h>
17 #include <linux/string.h>
18 #include <linux/namei.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 const struct inode_operations v9fs_dir_inode_operations;
34 static const struct inode_operations v9fs_dir_inode_operations_dotu;
35 static const struct inode_operations v9fs_file_inode_operations;
36 static const struct inode_operations v9fs_symlink_inode_operations;
37
38 /**
39  * unixmode2p9mode - convert unix mode bits to plan 9
40  * @v9ses: v9fs session information
41  * @mode: mode to convert
42  *
43  */
44
45 static u32 unixmode2p9mode(struct v9fs_session_info *v9ses, umode_t mode)
46 {
47         int res;
48
49         res = mode & 0777;
50         if (S_ISDIR(mode))
51                 res |= P9_DMDIR;
52         if (v9fs_proto_dotu(v9ses)) {
53                 if (v9ses->nodev == 0) {
54                         if (S_ISSOCK(mode))
55                                 res |= P9_DMSOCKET;
56                         if (S_ISFIFO(mode))
57                                 res |= P9_DMNAMEDPIPE;
58                         if (S_ISBLK(mode))
59                                 res |= P9_DMDEVICE;
60                         if (S_ISCHR(mode))
61                                 res |= P9_DMDEVICE;
62                 }
63
64                 if ((mode & S_ISUID) == S_ISUID)
65                         res |= P9_DMSETUID;
66                 if ((mode & S_ISGID) == S_ISGID)
67                         res |= P9_DMSETGID;
68                 if ((mode & S_ISVTX) == S_ISVTX)
69                         res |= P9_DMSETVTX;
70         }
71         return res;
72 }
73
74 /**
75  * p9mode2perm- convert plan9 mode bits to unix permission bits
76  * @v9ses: v9fs session information
77  * @stat: p9_wstat from which mode need to be derived
78  *
79  */
80 static int p9mode2perm(struct v9fs_session_info *v9ses,
81                        struct p9_wstat *stat)
82 {
83         int res;
84         int mode = stat->mode;
85
86         res = mode & S_IALLUGO;
87         if (v9fs_proto_dotu(v9ses)) {
88                 if ((mode & P9_DMSETUID) == P9_DMSETUID)
89                         res |= S_ISUID;
90
91                 if ((mode & P9_DMSETGID) == P9_DMSETGID)
92                         res |= S_ISGID;
93
94                 if ((mode & P9_DMSETVTX) == P9_DMSETVTX)
95                         res |= S_ISVTX;
96         }
97         return res;
98 }
99
100 /**
101  * p9mode2unixmode- convert plan9 mode bits to unix mode bits
102  * @v9ses: v9fs session information
103  * @stat: p9_wstat from which mode need to be derived
104  * @rdev: major number, minor number in case of device files.
105  *
106  */
107 static umode_t p9mode2unixmode(struct v9fs_session_info *v9ses,
108                                struct p9_wstat *stat, dev_t *rdev)
109 {
110         int res, r;
111         u32 mode = stat->mode;
112
113         *rdev = 0;
114         res = p9mode2perm(v9ses, stat);
115
116         if ((mode & P9_DMDIR) == P9_DMDIR)
117                 res |= S_IFDIR;
118         else if ((mode & P9_DMSYMLINK) && (v9fs_proto_dotu(v9ses)))
119                 res |= S_IFLNK;
120         else if ((mode & P9_DMSOCKET) && (v9fs_proto_dotu(v9ses))
121                  && (v9ses->nodev == 0))
122                 res |= S_IFSOCK;
123         else if ((mode & P9_DMNAMEDPIPE) && (v9fs_proto_dotu(v9ses))
124                  && (v9ses->nodev == 0))
125                 res |= S_IFIFO;
126         else if ((mode & P9_DMDEVICE) && (v9fs_proto_dotu(v9ses))
127                  && (v9ses->nodev == 0)) {
128                 char type = 0;
129                 int major = -1, minor = -1;
130
131                 r = sscanf(stat->extension, "%c %i %i", &type, &major, &minor);
132                 if (r != 3) {
133                         p9_debug(P9_DEBUG_ERROR,
134                                  "invalid device string, umode will be bogus: %s\n",
135                                  stat->extension);
136                         return res;
137                 }
138                 switch (type) {
139                 case 'c':
140                         res |= S_IFCHR;
141                         break;
142                 case 'b':
143                         res |= S_IFBLK;
144                         break;
145                 default:
146                         p9_debug(P9_DEBUG_ERROR, "Unknown special type %c %s\n",
147                                  type, stat->extension);
148                 }
149                 *rdev = MKDEV(major, minor);
150         } else
151                 res |= S_IFREG;
152
153         return res;
154 }
155
156 /**
157  * v9fs_uflags2omode- convert posix open flags to plan 9 mode bits
158  * @uflags: flags to convert
159  * @extended: if .u extensions are active
160  */
161
162 int v9fs_uflags2omode(int uflags, int extended)
163 {
164         int ret;
165
166         switch (uflags&3) {
167         default:
168         case O_RDONLY:
169                 ret = P9_OREAD;
170                 break;
171
172         case O_WRONLY:
173                 ret = P9_OWRITE;
174                 break;
175
176         case O_RDWR:
177                 ret = P9_ORDWR;
178                 break;
179         }
180
181         if (extended) {
182                 if (uflags & O_EXCL)
183                         ret |= P9_OEXCL;
184
185                 if (uflags & O_APPEND)
186                         ret |= P9_OAPPEND;
187         }
188
189         return ret;
190 }
191
192 /**
193  * v9fs_blank_wstat - helper function to setup a 9P stat structure
194  * @wstat: structure to initialize
195  *
196  */
197
198 void
199 v9fs_blank_wstat(struct p9_wstat *wstat)
200 {
201         wstat->type = ~0;
202         wstat->dev = ~0;
203         wstat->qid.type = ~0;
204         wstat->qid.version = ~0;
205         *((long long *)&wstat->qid.path) = ~0;
206         wstat->mode = ~0;
207         wstat->atime = ~0;
208         wstat->mtime = ~0;
209         wstat->length = ~0;
210         wstat->name = NULL;
211         wstat->uid = NULL;
212         wstat->gid = NULL;
213         wstat->muid = NULL;
214         wstat->n_uid = INVALID_UID;
215         wstat->n_gid = INVALID_GID;
216         wstat->n_muid = INVALID_UID;
217         wstat->extension = NULL;
218 }
219
220 /**
221  * v9fs_alloc_inode - helper function to allocate an inode
222  * @sb: The superblock to allocate the inode from
223  */
224 struct inode *v9fs_alloc_inode(struct super_block *sb)
225 {
226         struct v9fs_inode *v9inode;
227
228         v9inode = alloc_inode_sb(sb, v9fs_inode_cache, GFP_KERNEL);
229         if (!v9inode)
230                 return NULL;
231         v9inode->cache_validity = 0;
232         mutex_init(&v9inode->v_mutex);
233         return &v9inode->netfs.inode;
234 }
235
236 /**
237  * v9fs_free_inode - destroy an inode
238  * @inode: The inode to be freed
239  */
240
241 void v9fs_free_inode(struct inode *inode)
242 {
243         kmem_cache_free(v9fs_inode_cache, V9FS_I(inode));
244 }
245
246 /*
247  * Set parameters for the netfs library
248  */
249 void v9fs_set_netfs_context(struct inode *inode)
250 {
251         struct v9fs_inode *v9inode = V9FS_I(inode);
252         netfs_inode_init(&v9inode->netfs, &v9fs_req_ops, true);
253 }
254
255 int v9fs_init_inode(struct v9fs_session_info *v9ses,
256                     struct inode *inode, struct p9_qid *qid, umode_t mode, dev_t rdev)
257 {
258         int err = 0;
259         struct v9fs_inode *v9inode = V9FS_I(inode);
260
261         memcpy(&v9inode->qid, qid, sizeof(struct p9_qid));
262
263         inode_init_owner(&nop_mnt_idmap, inode, NULL, mode);
264         inode->i_blocks = 0;
265         inode->i_rdev = rdev;
266         simple_inode_init_ts(inode);
267         inode->i_mapping->a_ops = &v9fs_addr_operations;
268         inode->i_private = NULL;
269
270         switch (mode & S_IFMT) {
271         case S_IFIFO:
272         case S_IFBLK:
273         case S_IFCHR:
274         case S_IFSOCK:
275                 if (v9fs_proto_dotl(v9ses)) {
276                         inode->i_op = &v9fs_file_inode_operations_dotl;
277                 } else if (v9fs_proto_dotu(v9ses)) {
278                         inode->i_op = &v9fs_file_inode_operations;
279                 } else {
280                         p9_debug(P9_DEBUG_ERROR,
281                                  "special files without extended mode\n");
282                         err = -EINVAL;
283                         goto error;
284                 }
285                 init_special_inode(inode, inode->i_mode, inode->i_rdev);
286                 break;
287         case S_IFREG:
288                 if (v9fs_proto_dotl(v9ses)) {
289                         inode->i_op = &v9fs_file_inode_operations_dotl;
290                         inode->i_fop = &v9fs_file_operations_dotl;
291                 } else {
292                         inode->i_op = &v9fs_file_inode_operations;
293                         inode->i_fop = &v9fs_file_operations;
294                 }
295
296                 break;
297         case S_IFLNK:
298                 if (!v9fs_proto_dotu(v9ses) && !v9fs_proto_dotl(v9ses)) {
299                         p9_debug(P9_DEBUG_ERROR,
300                                  "extended modes used with legacy protocol\n");
301                         err = -EINVAL;
302                         goto error;
303                 }
304
305                 if (v9fs_proto_dotl(v9ses))
306                         inode->i_op = &v9fs_symlink_inode_operations_dotl;
307                 else
308                         inode->i_op = &v9fs_symlink_inode_operations;
309
310                 break;
311         case S_IFDIR:
312                 inc_nlink(inode);
313                 if (v9fs_proto_dotl(v9ses))
314                         inode->i_op = &v9fs_dir_inode_operations_dotl;
315                 else if (v9fs_proto_dotu(v9ses))
316                         inode->i_op = &v9fs_dir_inode_operations_dotu;
317                 else
318                         inode->i_op = &v9fs_dir_inode_operations;
319
320                 if (v9fs_proto_dotl(v9ses))
321                         inode->i_fop = &v9fs_dir_operations_dotl;
322                 else
323                         inode->i_fop = &v9fs_dir_operations;
324
325                 break;
326         default:
327                 p9_debug(P9_DEBUG_ERROR, "BAD mode 0x%hx S_IFMT 0x%x\n",
328                          mode, mode & S_IFMT);
329                 err = -EINVAL;
330                 goto error;
331         }
332 error:
333         return err;
334
335 }
336
337 /**
338  * v9fs_evict_inode - Remove an inode from the inode cache
339  * @inode: inode to release
340  *
341  */
342 void v9fs_evict_inode(struct inode *inode)
343 {
344         struct v9fs_inode __maybe_unused *v9inode = V9FS_I(inode);
345         __le32 __maybe_unused version;
346
347         truncate_inode_pages_final(&inode->i_data);
348
349         version = cpu_to_le32(v9inode->qid.version);
350         netfs_clear_inode_writeback(inode, &version);
351
352         clear_inode(inode);
353         filemap_fdatawrite(&inode->i_data);
354
355 #ifdef CONFIG_9P_FSCACHE
356         fscache_relinquish_cookie(v9fs_inode_cookie(v9inode), false);
357 #endif
358 }
359
360 struct inode *v9fs_fid_iget(struct super_block *sb, struct p9_fid *fid)
361 {
362         dev_t rdev;
363         int retval;
364         umode_t umode;
365         struct inode *inode;
366         struct p9_wstat *st;
367         struct v9fs_session_info *v9ses = sb->s_fs_info;
368
369         inode = iget_locked(sb, QID2INO(&fid->qid));
370         if (unlikely(!inode))
371                 return ERR_PTR(-ENOMEM);
372         if (!(inode->i_state & I_NEW))
373                 return inode;
374
375         /*
376          * initialize the inode with the stat info
377          * FIXME!! we may need support for stale inodes
378          * later.
379          */
380         st = p9_client_stat(fid);
381         if (IS_ERR(st)) {
382                 retval = PTR_ERR(st);
383                 goto error;
384         }
385
386         umode = p9mode2unixmode(v9ses, st, &rdev);
387         retval = v9fs_init_inode(v9ses, inode, &fid->qid, umode, rdev);
388         v9fs_stat2inode(st, inode, sb, 0);
389         p9stat_free(st);
390         kfree(st);
391         if (retval)
392                 goto error;
393
394         v9fs_set_netfs_context(inode);
395         v9fs_cache_inode_get_cookie(inode);
396         unlock_new_inode(inode);
397         return inode;
398 error:
399         iget_failed(inode);
400         return ERR_PTR(retval);
401
402 }
403
404 /**
405  * v9fs_at_to_dotl_flags- convert Linux specific AT flags to
406  * plan 9 AT flag.
407  * @flags: flags to convert
408  */
409 static int v9fs_at_to_dotl_flags(int flags)
410 {
411         int rflags = 0;
412
413         if (flags & AT_REMOVEDIR)
414                 rflags |= P9_DOTL_AT_REMOVEDIR;
415
416         return rflags;
417 }
418
419 /**
420  * v9fs_dec_count - helper functon to drop i_nlink.
421  *
422  * If a directory had nlink <= 2 (including . and ..), then we should not drop
423  * the link count, which indicates the underlying exported fs doesn't maintain
424  * nlink accurately. e.g.
425  * - overlayfs sets nlink to 1 for merged dir
426  * - ext4 (with dir_nlink feature enabled) sets nlink to 1 if a dir has more
427  *   than EXT4_LINK_MAX (65000) links.
428  *
429  * @inode: inode whose nlink is being dropped
430  */
431 static void v9fs_dec_count(struct inode *inode)
432 {
433         if (!S_ISDIR(inode->i_mode) || inode->i_nlink > 2)
434                 drop_nlink(inode);
435 }
436
437 /**
438  * v9fs_remove - helper function to remove files and directories
439  * @dir: directory inode that is being deleted
440  * @dentry:  dentry that is being deleted
441  * @flags: removing a directory
442  *
443  */
444
445 static int v9fs_remove(struct inode *dir, struct dentry *dentry, int flags)
446 {
447         struct inode *inode;
448         int retval = -EOPNOTSUPP;
449         struct p9_fid *v9fid, *dfid;
450         struct v9fs_session_info *v9ses;
451
452         p9_debug(P9_DEBUG_VFS, "inode: %p dentry: %p rmdir: %x\n",
453                  dir, dentry, flags);
454
455         v9ses = v9fs_inode2v9ses(dir);
456         inode = d_inode(dentry);
457         dfid = v9fs_parent_fid(dentry);
458         if (IS_ERR(dfid)) {
459                 retval = PTR_ERR(dfid);
460                 p9_debug(P9_DEBUG_VFS, "fid lookup failed %d\n", retval);
461                 return retval;
462         }
463         if (v9fs_proto_dotl(v9ses))
464                 retval = p9_client_unlinkat(dfid, dentry->d_name.name,
465                                             v9fs_at_to_dotl_flags(flags));
466         p9_fid_put(dfid);
467         if (retval == -EOPNOTSUPP) {
468                 /* Try the one based on path */
469                 v9fid = v9fs_fid_clone(dentry);
470                 if (IS_ERR(v9fid))
471                         return PTR_ERR(v9fid);
472                 retval = p9_client_remove(v9fid);
473         }
474         if (!retval) {
475                 /*
476                  * directories on unlink should have zero
477                  * link count
478                  */
479                 if (flags & AT_REMOVEDIR) {
480                         clear_nlink(inode);
481                         v9fs_dec_count(dir);
482                 } else
483                         v9fs_dec_count(inode);
484
485                 v9fs_invalidate_inode_attr(inode);
486                 v9fs_invalidate_inode_attr(dir);
487
488                 /* invalidate all fids associated with dentry */
489                 /* NOTE: This will not include open fids */
490                 dentry->d_op->d_release(dentry);
491         }
492         return retval;
493 }
494
495 /**
496  * v9fs_create - Create a file
497  * @v9ses: session information
498  * @dir: directory that dentry is being created in
499  * @dentry:  dentry that is being created
500  * @extension: 9p2000.u extension string to support devices, etc.
501  * @perm: create permissions
502  * @mode: open mode
503  *
504  */
505 static struct p9_fid *
506 v9fs_create(struct v9fs_session_info *v9ses, struct inode *dir,
507                 struct dentry *dentry, char *extension, u32 perm, u8 mode)
508 {
509         int err;
510         const unsigned char *name;
511         struct p9_fid *dfid, *ofid = NULL, *fid = NULL;
512         struct inode *inode;
513
514         p9_debug(P9_DEBUG_VFS, "name %pd\n", dentry);
515
516         name = dentry->d_name.name;
517         dfid = v9fs_parent_fid(dentry);
518         if (IS_ERR(dfid)) {
519                 err = PTR_ERR(dfid);
520                 p9_debug(P9_DEBUG_VFS, "fid lookup failed %d\n", err);
521                 return ERR_PTR(err);
522         }
523
524         /* clone a fid to use for creation */
525         ofid = clone_fid(dfid);
526         if (IS_ERR(ofid)) {
527                 err = PTR_ERR(ofid);
528                 p9_debug(P9_DEBUG_VFS, "p9_client_walk failed %d\n", err);
529                 goto error;
530         }
531
532         err = p9_client_fcreate(ofid, name, perm, mode, extension);
533         if (err < 0) {
534                 p9_debug(P9_DEBUG_VFS, "p9_client_fcreate failed %d\n", err);
535                 goto error;
536         }
537
538         if (!(perm & P9_DMLINK)) {
539                 /* now walk from the parent so we can get unopened fid */
540                 fid = p9_client_walk(dfid, 1, &name, 1);
541                 if (IS_ERR(fid)) {
542                         err = PTR_ERR(fid);
543                         p9_debug(P9_DEBUG_VFS,
544                                    "p9_client_walk failed %d\n", err);
545                         goto error;
546                 }
547                 /*
548                  * instantiate inode and assign the unopened fid to the dentry
549                  */
550                 inode = v9fs_get_inode_from_fid(v9ses, fid, dir->i_sb);
551                 if (IS_ERR(inode)) {
552                         err = PTR_ERR(inode);
553                         p9_debug(P9_DEBUG_VFS,
554                                    "inode creation failed %d\n", err);
555                         goto error;
556                 }
557                 v9fs_fid_add(dentry, &fid);
558                 d_instantiate(dentry, inode);
559         }
560         p9_fid_put(dfid);
561         return ofid;
562 error:
563         p9_fid_put(dfid);
564         p9_fid_put(ofid);
565         p9_fid_put(fid);
566         return ERR_PTR(err);
567 }
568
569 /**
570  * v9fs_vfs_create - VFS hook to create a regular file
571  * @idmap: idmap of the mount
572  * @dir: The parent directory
573  * @dentry: The name of file to be created
574  * @mode: The UNIX file mode to set
575  * @excl: True if the file must not yet exist
576  *
577  * open(.., O_CREAT) is handled in v9fs_vfs_atomic_open().  This is only called
578  * for mknod(2).
579  *
580  */
581
582 static int
583 v9fs_vfs_create(struct mnt_idmap *idmap, struct inode *dir,
584                 struct dentry *dentry, umode_t mode, bool excl)
585 {
586         struct v9fs_session_info *v9ses = v9fs_inode2v9ses(dir);
587         u32 perm = unixmode2p9mode(v9ses, mode);
588         struct p9_fid *fid;
589
590         /* P9_OEXCL? */
591         fid = v9fs_create(v9ses, dir, dentry, NULL, perm, P9_ORDWR);
592         if (IS_ERR(fid))
593                 return PTR_ERR(fid);
594
595         v9fs_invalidate_inode_attr(dir);
596         p9_fid_put(fid);
597
598         return 0;
599 }
600
601 /**
602  * v9fs_vfs_mkdir - VFS mkdir hook to create a directory
603  * @idmap: idmap of the mount
604  * @dir:  inode that is being unlinked
605  * @dentry: dentry that is being unlinked
606  * @mode: mode for new directory
607  *
608  */
609
610 static int v9fs_vfs_mkdir(struct mnt_idmap *idmap, struct inode *dir,
611                           struct dentry *dentry, umode_t mode)
612 {
613         int err;
614         u32 perm;
615         struct p9_fid *fid;
616         struct v9fs_session_info *v9ses;
617
618         p9_debug(P9_DEBUG_VFS, "name %pd\n", dentry);
619         err = 0;
620         v9ses = v9fs_inode2v9ses(dir);
621         perm = unixmode2p9mode(v9ses, mode | S_IFDIR);
622         fid = v9fs_create(v9ses, dir, dentry, NULL, perm, P9_OREAD);
623         if (IS_ERR(fid)) {
624                 err = PTR_ERR(fid);
625                 fid = NULL;
626         } else {
627                 inc_nlink(dir);
628                 v9fs_invalidate_inode_attr(dir);
629         }
630
631         if (fid)
632                 p9_fid_put(fid);
633
634         return err;
635 }
636
637 /**
638  * v9fs_vfs_lookup - VFS lookup hook to "walk" to a new inode
639  * @dir:  inode that is being walked from
640  * @dentry: dentry that is being walked to?
641  * @flags: lookup flags (unused)
642  *
643  */
644
645 struct dentry *v9fs_vfs_lookup(struct inode *dir, struct dentry *dentry,
646                                       unsigned int flags)
647 {
648         struct dentry *res;
649         struct v9fs_session_info *v9ses;
650         struct p9_fid *dfid, *fid;
651         struct inode *inode;
652         const unsigned char *name;
653
654         p9_debug(P9_DEBUG_VFS, "dir: %p dentry: (%pd) %p flags: %x\n",
655                  dir, dentry, dentry, flags);
656
657         if (dentry->d_name.len > NAME_MAX)
658                 return ERR_PTR(-ENAMETOOLONG);
659
660         v9ses = v9fs_inode2v9ses(dir);
661         /* We can walk d_parent because we hold the dir->i_mutex */
662         dfid = v9fs_parent_fid(dentry);
663         if (IS_ERR(dfid))
664                 return ERR_CAST(dfid);
665
666         /*
667          * Make sure we don't use a wrong inode due to parallel
668          * unlink. For cached mode create calls request for new
669          * inode. But with cache disabled, lookup should do this.
670          */
671         name = dentry->d_name.name;
672         fid = p9_client_walk(dfid, 1, &name, 1);
673         p9_fid_put(dfid);
674         if (fid == ERR_PTR(-ENOENT))
675                 inode = NULL;
676         else if (IS_ERR(fid))
677                 inode = ERR_CAST(fid);
678         else
679                 inode = v9fs_get_inode_from_fid(v9ses, fid, dir->i_sb);
680         /*
681          * If we had a rename on the server and a parallel lookup
682          * for the new name, then make sure we instantiate with
683          * the new name. ie look up for a/b, while on server somebody
684          * moved b under k and client parallely did a lookup for
685          * k/b.
686          */
687         res = d_splice_alias(inode, dentry);
688         if (!IS_ERR(fid)) {
689                 if (!res)
690                         v9fs_fid_add(dentry, &fid);
691                 else if (!IS_ERR(res))
692                         v9fs_fid_add(res, &fid);
693                 else
694                         p9_fid_put(fid);
695         }
696         return res;
697 }
698
699 static int
700 v9fs_vfs_atomic_open(struct inode *dir, struct dentry *dentry,
701                      struct file *file, unsigned int flags, umode_t mode)
702 {
703         int err;
704         u32 perm;
705         struct v9fs_inode __maybe_unused *v9inode;
706         struct v9fs_session_info *v9ses;
707         struct p9_fid *fid;
708         struct dentry *res = NULL;
709         struct inode *inode;
710         int p9_omode;
711
712         if (d_in_lookup(dentry)) {
713                 res = v9fs_vfs_lookup(dir, dentry, 0);
714                 if (IS_ERR(res))
715                         return PTR_ERR(res);
716
717                 if (res)
718                         dentry = res;
719         }
720
721         /* Only creates */
722         if (!(flags & O_CREAT) || d_really_is_positive(dentry))
723                 return finish_no_open(file, res);
724
725         v9ses = v9fs_inode2v9ses(dir);
726         perm = unixmode2p9mode(v9ses, mode);
727         p9_omode = v9fs_uflags2omode(flags, v9fs_proto_dotu(v9ses));
728
729         if ((v9ses->cache & CACHE_WRITEBACK) && (p9_omode & P9_OWRITE)) {
730                 p9_omode = (p9_omode & ~P9_OWRITE) | P9_ORDWR;
731                 p9_debug(P9_DEBUG_CACHE,
732                         "write-only file with writeback enabled, creating w/ O_RDWR\n");
733         }
734         fid = v9fs_create(v9ses, dir, dentry, NULL, perm, p9_omode);
735         if (IS_ERR(fid)) {
736                 err = PTR_ERR(fid);
737                 goto error;
738         }
739
740         v9fs_invalidate_inode_attr(dir);
741         inode = d_inode(dentry);
742         v9inode = V9FS_I(inode);
743         err = finish_open(file, dentry, generic_file_open);
744         if (err)
745                 goto error;
746
747         file->private_data = fid;
748 #ifdef CONFIG_9P_FSCACHE
749         if (v9ses->cache & CACHE_FSCACHE)
750                 fscache_use_cookie(v9fs_inode_cookie(v9inode),
751                                    file->f_mode & FMODE_WRITE);
752 #endif
753
754         v9fs_fid_add_modes(fid, v9ses->flags, v9ses->cache, file->f_flags);
755         v9fs_open_fid_add(inode, &fid);
756
757         file->f_mode |= FMODE_CREATED;
758 out:
759         dput(res);
760         return err;
761
762 error:
763         p9_fid_put(fid);
764         goto out;
765 }
766
767 /**
768  * v9fs_vfs_unlink - VFS unlink hook to delete an inode
769  * @i:  inode that is being unlinked
770  * @d: dentry that is being unlinked
771  *
772  */
773
774 int v9fs_vfs_unlink(struct inode *i, struct dentry *d)
775 {
776         return v9fs_remove(i, d, 0);
777 }
778
779 /**
780  * v9fs_vfs_rmdir - VFS unlink hook to delete a directory
781  * @i:  inode that is being unlinked
782  * @d: dentry that is being unlinked
783  *
784  */
785
786 int v9fs_vfs_rmdir(struct inode *i, struct dentry *d)
787 {
788         return v9fs_remove(i, d, AT_REMOVEDIR);
789 }
790
791 /**
792  * v9fs_vfs_rename - VFS hook to rename an inode
793  * @idmap: The idmap of the mount
794  * @old_dir:  old dir inode
795  * @old_dentry: old dentry
796  * @new_dir: new dir inode
797  * @new_dentry: new dentry
798  * @flags: RENAME_* flags
799  *
800  */
801
802 int
803 v9fs_vfs_rename(struct mnt_idmap *idmap, struct inode *old_dir,
804                 struct dentry *old_dentry, struct inode *new_dir,
805                 struct dentry *new_dentry, unsigned int flags)
806 {
807         int retval;
808         struct inode *old_inode;
809         struct inode *new_inode;
810         struct v9fs_session_info *v9ses;
811         struct p9_fid *oldfid = NULL, *dfid = NULL;
812         struct p9_fid *olddirfid = NULL;
813         struct p9_fid *newdirfid = NULL;
814         struct p9_wstat wstat;
815
816         if (flags)
817                 return -EINVAL;
818
819         p9_debug(P9_DEBUG_VFS, "\n");
820         old_inode = d_inode(old_dentry);
821         new_inode = d_inode(new_dentry);
822         v9ses = v9fs_inode2v9ses(old_inode);
823         oldfid = v9fs_fid_lookup(old_dentry);
824         if (IS_ERR(oldfid))
825                 return PTR_ERR(oldfid);
826
827         dfid = v9fs_parent_fid(old_dentry);
828         olddirfid = clone_fid(dfid);
829         p9_fid_put(dfid);
830         dfid = NULL;
831
832         if (IS_ERR(olddirfid)) {
833                 retval = PTR_ERR(olddirfid);
834                 goto error;
835         }
836
837         dfid = v9fs_parent_fid(new_dentry);
838         newdirfid = clone_fid(dfid);
839         p9_fid_put(dfid);
840         dfid = NULL;
841
842         if (IS_ERR(newdirfid)) {
843                 retval = PTR_ERR(newdirfid);
844                 goto error;
845         }
846
847         down_write(&v9ses->rename_sem);
848         if (v9fs_proto_dotl(v9ses)) {
849                 retval = p9_client_renameat(olddirfid, old_dentry->d_name.name,
850                                             newdirfid, new_dentry->d_name.name);
851                 if (retval == -EOPNOTSUPP)
852                         retval = p9_client_rename(oldfid, newdirfid,
853                                                   new_dentry->d_name.name);
854                 if (retval != -EOPNOTSUPP)
855                         goto error_locked;
856         }
857         if (old_dentry->d_parent != new_dentry->d_parent) {
858                 /*
859                  * 9P .u can only handle file rename in the same directory
860                  */
861
862                 p9_debug(P9_DEBUG_ERROR, "old dir and new dir are different\n");
863                 retval = -EXDEV;
864                 goto error_locked;
865         }
866         v9fs_blank_wstat(&wstat);
867         wstat.muid = v9ses->uname;
868         wstat.name = new_dentry->d_name.name;
869         retval = p9_client_wstat(oldfid, &wstat);
870
871 error_locked:
872         if (!retval) {
873                 if (new_inode) {
874                         if (S_ISDIR(new_inode->i_mode))
875                                 clear_nlink(new_inode);
876                         else
877                                 v9fs_dec_count(new_inode);
878                 }
879                 if (S_ISDIR(old_inode->i_mode)) {
880                         if (!new_inode)
881                                 inc_nlink(new_dir);
882                         v9fs_dec_count(old_dir);
883                 }
884                 v9fs_invalidate_inode_attr(old_inode);
885                 v9fs_invalidate_inode_attr(old_dir);
886                 v9fs_invalidate_inode_attr(new_dir);
887
888                 /* successful rename */
889                 d_move(old_dentry, new_dentry);
890         }
891         up_write(&v9ses->rename_sem);
892
893 error:
894         p9_fid_put(newdirfid);
895         p9_fid_put(olddirfid);
896         p9_fid_put(oldfid);
897         return retval;
898 }
899
900 /**
901  * v9fs_vfs_getattr - retrieve file metadata
902  * @idmap: idmap of the mount
903  * @path: Object to query
904  * @stat: metadata structure to populate
905  * @request_mask: Mask of STATX_xxx flags indicating the caller's interests
906  * @flags: AT_STATX_xxx setting
907  *
908  */
909
910 static int
911 v9fs_vfs_getattr(struct mnt_idmap *idmap, const struct path *path,
912                  struct kstat *stat, u32 request_mask, unsigned int flags)
913 {
914         struct dentry *dentry = path->dentry;
915         struct inode *inode = d_inode(dentry);
916         struct v9fs_session_info *v9ses;
917         struct p9_fid *fid;
918         struct p9_wstat *st;
919
920         p9_debug(P9_DEBUG_VFS, "dentry: %p\n", dentry);
921         v9ses = v9fs_dentry2v9ses(dentry);
922         if (v9ses->cache & (CACHE_META|CACHE_LOOSE)) {
923                 generic_fillattr(&nop_mnt_idmap, request_mask, inode, stat);
924                 return 0;
925         } else if (v9ses->cache & CACHE_WRITEBACK) {
926                 if (S_ISREG(inode->i_mode)) {
927                         int retval = filemap_fdatawrite(inode->i_mapping);
928
929                         if (retval)
930                                 p9_debug(P9_DEBUG_ERROR,
931                                     "flushing writeback during getattr returned %d\n", retval);
932                 }
933         }
934         fid = v9fs_fid_lookup(dentry);
935         if (IS_ERR(fid))
936                 return PTR_ERR(fid);
937
938         st = p9_client_stat(fid);
939         p9_fid_put(fid);
940         if (IS_ERR(st))
941                 return PTR_ERR(st);
942
943         v9fs_stat2inode(st, d_inode(dentry), dentry->d_sb, 0);
944         generic_fillattr(&nop_mnt_idmap, request_mask, d_inode(dentry), stat);
945
946         p9stat_free(st);
947         kfree(st);
948         return 0;
949 }
950
951 /**
952  * v9fs_vfs_setattr - set file metadata
953  * @idmap: idmap of the mount
954  * @dentry: file whose metadata to set
955  * @iattr: metadata assignment structure
956  *
957  */
958
959 static int v9fs_vfs_setattr(struct mnt_idmap *idmap,
960                             struct dentry *dentry, struct iattr *iattr)
961 {
962         int retval, use_dentry = 0;
963         struct inode *inode = d_inode(dentry);
964         struct v9fs_session_info *v9ses;
965         struct p9_fid *fid = NULL;
966         struct p9_wstat wstat;
967
968         p9_debug(P9_DEBUG_VFS, "\n");
969         retval = setattr_prepare(&nop_mnt_idmap, dentry, iattr);
970         if (retval)
971                 return retval;
972
973         v9ses = v9fs_dentry2v9ses(dentry);
974         if (iattr->ia_valid & ATTR_FILE) {
975                 fid = iattr->ia_file->private_data;
976                 WARN_ON(!fid);
977         }
978         if (!fid) {
979                 fid = v9fs_fid_lookup(dentry);
980                 use_dentry = 1;
981         }
982         if (IS_ERR(fid))
983                 return PTR_ERR(fid);
984
985         v9fs_blank_wstat(&wstat);
986         if (iattr->ia_valid & ATTR_MODE)
987                 wstat.mode = unixmode2p9mode(v9ses, iattr->ia_mode);
988
989         if (iattr->ia_valid & ATTR_MTIME)
990                 wstat.mtime = iattr->ia_mtime.tv_sec;
991
992         if (iattr->ia_valid & ATTR_ATIME)
993                 wstat.atime = iattr->ia_atime.tv_sec;
994
995         if (iattr->ia_valid & ATTR_SIZE)
996                 wstat.length = iattr->ia_size;
997
998         if (v9fs_proto_dotu(v9ses)) {
999                 if (iattr->ia_valid & ATTR_UID)
1000                         wstat.n_uid = iattr->ia_uid;
1001
1002                 if (iattr->ia_valid & ATTR_GID)
1003                         wstat.n_gid = iattr->ia_gid;
1004         }
1005
1006         /* Write all dirty data */
1007         if (d_is_reg(dentry)) {
1008                 retval = filemap_fdatawrite(inode->i_mapping);
1009                 if (retval)
1010                         p9_debug(P9_DEBUG_ERROR,
1011                             "flushing writeback during setattr returned %d\n", retval);
1012         }
1013
1014         retval = p9_client_wstat(fid, &wstat);
1015
1016         if (use_dentry)
1017                 p9_fid_put(fid);
1018
1019         if (retval < 0)
1020                 return retval;
1021
1022         if ((iattr->ia_valid & ATTR_SIZE) &&
1023                  iattr->ia_size != i_size_read(inode)) {
1024                 truncate_setsize(inode, iattr->ia_size);
1025                 netfs_resize_file(netfs_inode(inode), iattr->ia_size, true);
1026
1027 #ifdef CONFIG_9P_FSCACHE
1028                 if (v9ses->cache & CACHE_FSCACHE) {
1029                         struct v9fs_inode *v9inode = V9FS_I(inode);
1030
1031                         fscache_resize_cookie(v9fs_inode_cookie(v9inode), iattr->ia_size);
1032                 }
1033 #endif
1034         }
1035
1036         v9fs_invalidate_inode_attr(inode);
1037
1038         setattr_copy(&nop_mnt_idmap, inode, iattr);
1039         mark_inode_dirty(inode);
1040         return 0;
1041 }
1042
1043 /**
1044  * v9fs_stat2inode - populate an inode structure with mistat info
1045  * @stat: Plan 9 metadata (mistat) structure
1046  * @inode: inode to populate
1047  * @sb: superblock of filesystem
1048  * @flags: control flags (e.g. V9FS_STAT2INODE_KEEP_ISIZE)
1049  *
1050  */
1051
1052 void
1053 v9fs_stat2inode(struct p9_wstat *stat, struct inode *inode,
1054                  struct super_block *sb, unsigned int flags)
1055 {
1056         umode_t mode;
1057         struct v9fs_session_info *v9ses = sb->s_fs_info;
1058         struct v9fs_inode *v9inode = V9FS_I(inode);
1059
1060         set_nlink(inode, 1);
1061
1062         inode_set_atime(inode, stat->atime, 0);
1063         inode_set_mtime(inode, stat->mtime, 0);
1064         inode_set_ctime(inode, stat->mtime, 0);
1065
1066         inode->i_uid = v9ses->dfltuid;
1067         inode->i_gid = v9ses->dfltgid;
1068
1069         if (v9fs_proto_dotu(v9ses)) {
1070                 inode->i_uid = stat->n_uid;
1071                 inode->i_gid = stat->n_gid;
1072         }
1073         if ((S_ISREG(inode->i_mode)) || (S_ISDIR(inode->i_mode))) {
1074                 if (v9fs_proto_dotu(v9ses)) {
1075                         unsigned int i_nlink;
1076                         /*
1077                          * Hadlink support got added later to the .u extension.
1078                          * So there can be a server out there that doesn't
1079                          * support this even with .u extension. That would
1080                          * just leave us with stat->extension being an empty
1081                          * string, though.
1082                          */
1083                         /* HARDLINKCOUNT %u */
1084                         if (sscanf(stat->extension,
1085                                    " HARDLINKCOUNT %u", &i_nlink) == 1)
1086                                 set_nlink(inode, i_nlink);
1087                 }
1088         }
1089         mode = p9mode2perm(v9ses, stat);
1090         mode |= inode->i_mode & ~S_IALLUGO;
1091         inode->i_mode = mode;
1092
1093         v9inode->netfs.remote_i_size = stat->length;
1094         if (!(flags & V9FS_STAT2INODE_KEEP_ISIZE))
1095                 v9fs_i_size_write(inode, stat->length);
1096         /* not real number of blocks, but 512 byte ones ... */
1097         inode->i_blocks = (stat->length + 512 - 1) >> 9;
1098         v9inode->cache_validity &= ~V9FS_INO_INVALID_ATTR;
1099 }
1100
1101 /**
1102  * v9fs_vfs_get_link - follow a symlink path
1103  * @dentry: dentry for symlink
1104  * @inode: inode for symlink
1105  * @done: delayed call for when we are done with the return value
1106  */
1107
1108 static const char *v9fs_vfs_get_link(struct dentry *dentry,
1109                                      struct inode *inode,
1110                                      struct delayed_call *done)
1111 {
1112         struct v9fs_session_info *v9ses;
1113         struct p9_fid *fid;
1114         struct p9_wstat *st;
1115         char *res;
1116
1117         if (!dentry)
1118                 return ERR_PTR(-ECHILD);
1119
1120         v9ses = v9fs_dentry2v9ses(dentry);
1121         if (!v9fs_proto_dotu(v9ses))
1122                 return ERR_PTR(-EBADF);
1123
1124         p9_debug(P9_DEBUG_VFS, "%pd\n", dentry);
1125         fid = v9fs_fid_lookup(dentry);
1126
1127         if (IS_ERR(fid))
1128                 return ERR_CAST(fid);
1129
1130         st = p9_client_stat(fid);
1131         p9_fid_put(fid);
1132         if (IS_ERR(st))
1133                 return ERR_CAST(st);
1134
1135         if (!(st->mode & P9_DMSYMLINK)) {
1136                 p9stat_free(st);
1137                 kfree(st);
1138                 return ERR_PTR(-EINVAL);
1139         }
1140         res = st->extension;
1141         st->extension = NULL;
1142         if (strlen(res) >= PATH_MAX)
1143                 res[PATH_MAX - 1] = '\0';
1144
1145         p9stat_free(st);
1146         kfree(st);
1147         set_delayed_call(done, kfree_link, res);
1148         return res;
1149 }
1150
1151 /**
1152  * v9fs_vfs_mkspecial - create a special file
1153  * @dir: inode to create special file in
1154  * @dentry: dentry to create
1155  * @perm: mode to create special file
1156  * @extension: 9p2000.u format extension string representing special file
1157  *
1158  */
1159
1160 static int v9fs_vfs_mkspecial(struct inode *dir, struct dentry *dentry,
1161         u32 perm, const char *extension)
1162 {
1163         struct p9_fid *fid;
1164         struct v9fs_session_info *v9ses;
1165
1166         v9ses = v9fs_inode2v9ses(dir);
1167         if (!v9fs_proto_dotu(v9ses)) {
1168                 p9_debug(P9_DEBUG_ERROR, "not extended\n");
1169                 return -EPERM;
1170         }
1171
1172         fid = v9fs_create(v9ses, dir, dentry, (char *) extension, perm,
1173                                                                 P9_OREAD);
1174         if (IS_ERR(fid))
1175                 return PTR_ERR(fid);
1176
1177         v9fs_invalidate_inode_attr(dir);
1178         p9_fid_put(fid);
1179         return 0;
1180 }
1181
1182 /**
1183  * v9fs_vfs_symlink - helper function to create symlinks
1184  * @idmap: idmap of the mount
1185  * @dir: directory inode containing symlink
1186  * @dentry: dentry for symlink
1187  * @symname: symlink data
1188  *
1189  * See Also: 9P2000.u RFC for more information
1190  *
1191  */
1192
1193 static int
1194 v9fs_vfs_symlink(struct mnt_idmap *idmap, struct inode *dir,
1195                  struct dentry *dentry, const char *symname)
1196 {
1197         p9_debug(P9_DEBUG_VFS, " %lu,%pd,%s\n",
1198                  dir->i_ino, dentry, symname);
1199
1200         return v9fs_vfs_mkspecial(dir, dentry, P9_DMSYMLINK, symname);
1201 }
1202
1203 #define U32_MAX_DIGITS 10
1204
1205 /**
1206  * v9fs_vfs_link - create a hardlink
1207  * @old_dentry: dentry for file to link to
1208  * @dir: inode destination for new link
1209  * @dentry: dentry for link
1210  *
1211  */
1212
1213 static int
1214 v9fs_vfs_link(struct dentry *old_dentry, struct inode *dir,
1215               struct dentry *dentry)
1216 {
1217         int retval;
1218         char name[1 + U32_MAX_DIGITS + 2]; /* sign + number + \n + \0 */
1219         struct p9_fid *oldfid;
1220
1221         p9_debug(P9_DEBUG_VFS, " %lu,%pd,%pd\n",
1222                  dir->i_ino, dentry, old_dentry);
1223
1224         oldfid = v9fs_fid_clone(old_dentry);
1225         if (IS_ERR(oldfid))
1226                 return PTR_ERR(oldfid);
1227
1228         sprintf(name, "%d\n", oldfid->fid);
1229         retval = v9fs_vfs_mkspecial(dir, dentry, P9_DMLINK, name);
1230         if (!retval) {
1231                 v9fs_refresh_inode(oldfid, d_inode(old_dentry));
1232                 v9fs_invalidate_inode_attr(dir);
1233         }
1234         p9_fid_put(oldfid);
1235         return retval;
1236 }
1237
1238 /**
1239  * v9fs_vfs_mknod - create a special file
1240  * @idmap: idmap of the mount
1241  * @dir: inode destination for new link
1242  * @dentry: dentry for file
1243  * @mode: mode for creation
1244  * @rdev: device associated with special file
1245  *
1246  */
1247
1248 static int
1249 v9fs_vfs_mknod(struct mnt_idmap *idmap, struct inode *dir,
1250                struct dentry *dentry, umode_t mode, dev_t rdev)
1251 {
1252         struct v9fs_session_info *v9ses = v9fs_inode2v9ses(dir);
1253         int retval;
1254         char name[2 + U32_MAX_DIGITS + 1 + U32_MAX_DIGITS + 1];
1255         u32 perm;
1256
1257         p9_debug(P9_DEBUG_VFS, " %lu,%pd mode: %x MAJOR: %u MINOR: %u\n",
1258                  dir->i_ino, dentry, mode,
1259                  MAJOR(rdev), MINOR(rdev));
1260
1261         /* build extension */
1262         if (S_ISBLK(mode))
1263                 sprintf(name, "b %u %u", MAJOR(rdev), MINOR(rdev));
1264         else if (S_ISCHR(mode))
1265                 sprintf(name, "c %u %u", MAJOR(rdev), MINOR(rdev));
1266         else
1267                 *name = 0;
1268
1269         perm = unixmode2p9mode(v9ses, mode);
1270         retval = v9fs_vfs_mkspecial(dir, dentry, perm, name);
1271
1272         return retval;
1273 }
1274
1275 int v9fs_refresh_inode(struct p9_fid *fid, struct inode *inode)
1276 {
1277         int umode;
1278         dev_t rdev;
1279         struct p9_wstat *st;
1280         struct v9fs_session_info *v9ses;
1281         unsigned int flags;
1282
1283         v9ses = v9fs_inode2v9ses(inode);
1284         st = p9_client_stat(fid);
1285         if (IS_ERR(st))
1286                 return PTR_ERR(st);
1287         /*
1288          * Don't update inode if the file type is different
1289          */
1290         umode = p9mode2unixmode(v9ses, st, &rdev);
1291         if (inode_wrong_type(inode, umode))
1292                 goto out;
1293
1294         /*
1295          * We don't want to refresh inode->i_size,
1296          * because we may have cached data
1297          */
1298         flags = (v9ses->cache & CACHE_LOOSE) ?
1299                 V9FS_STAT2INODE_KEEP_ISIZE : 0;
1300         v9fs_stat2inode(st, inode, inode->i_sb, flags);
1301 out:
1302         p9stat_free(st);
1303         kfree(st);
1304         return 0;
1305 }
1306
1307 static const struct inode_operations v9fs_dir_inode_operations_dotu = {
1308         .create = v9fs_vfs_create,
1309         .lookup = v9fs_vfs_lookup,
1310         .atomic_open = v9fs_vfs_atomic_open,
1311         .symlink = v9fs_vfs_symlink,
1312         .link = v9fs_vfs_link,
1313         .unlink = v9fs_vfs_unlink,
1314         .mkdir = v9fs_vfs_mkdir,
1315         .rmdir = v9fs_vfs_rmdir,
1316         .mknod = v9fs_vfs_mknod,
1317         .rename = v9fs_vfs_rename,
1318         .getattr = v9fs_vfs_getattr,
1319         .setattr = v9fs_vfs_setattr,
1320 };
1321
1322 static const struct inode_operations v9fs_dir_inode_operations = {
1323         .create = v9fs_vfs_create,
1324         .lookup = v9fs_vfs_lookup,
1325         .atomic_open = v9fs_vfs_atomic_open,
1326         .unlink = v9fs_vfs_unlink,
1327         .mkdir = v9fs_vfs_mkdir,
1328         .rmdir = v9fs_vfs_rmdir,
1329         .mknod = v9fs_vfs_mknod,
1330         .rename = v9fs_vfs_rename,
1331         .getattr = v9fs_vfs_getattr,
1332         .setattr = v9fs_vfs_setattr,
1333 };
1334
1335 static const struct inode_operations v9fs_file_inode_operations = {
1336         .getattr = v9fs_vfs_getattr,
1337         .setattr = v9fs_vfs_setattr,
1338 };
1339
1340 static const struct inode_operations v9fs_symlink_inode_operations = {
1341         .get_link = v9fs_vfs_get_link,
1342         .getattr = v9fs_vfs_getattr,
1343         .setattr = v9fs_vfs_setattr,
1344 };
1345