1 // SPDX-License-Identifier: GPL-2.0-only
3 * This file contains vfs inode ops for the 9P2000 protocol.
9 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
11 #include <linux/module.h>
12 #include <linux/errno.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>
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;
39 * unixmode2p9mode - convert unix mode bits to plan 9
40 * @v9ses: v9fs session information
41 * @mode: mode to convert
45 static u32 unixmode2p9mode(struct v9fs_session_info *v9ses, umode_t mode)
52 if (v9fs_proto_dotu(v9ses)) {
53 if (v9ses->nodev == 0) {
57 res |= P9_DMNAMEDPIPE;
64 if ((mode & S_ISUID) == S_ISUID)
66 if ((mode & S_ISGID) == S_ISGID)
68 if ((mode & S_ISVTX) == S_ISVTX)
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
80 static int p9mode2perm(struct v9fs_session_info *v9ses,
81 struct p9_wstat *stat)
84 int mode = stat->mode;
86 res = mode & S_IALLUGO;
87 if (v9fs_proto_dotu(v9ses)) {
88 if ((mode & P9_DMSETUID) == P9_DMSETUID)
91 if ((mode & P9_DMSETGID) == P9_DMSETGID)
94 if ((mode & P9_DMSETVTX) == P9_DMSETVTX)
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.
107 static umode_t p9mode2unixmode(struct v9fs_session_info *v9ses,
108 struct p9_wstat *stat, dev_t *rdev)
111 u32 mode = stat->mode;
114 res = p9mode2perm(v9ses, stat);
116 if ((mode & P9_DMDIR) == P9_DMDIR)
118 else if ((mode & P9_DMSYMLINK) && (v9fs_proto_dotu(v9ses)))
120 else if ((mode & P9_DMSOCKET) && (v9fs_proto_dotu(v9ses))
121 && (v9ses->nodev == 0))
123 else if ((mode & P9_DMNAMEDPIPE) && (v9fs_proto_dotu(v9ses))
124 && (v9ses->nodev == 0))
126 else if ((mode & P9_DMDEVICE) && (v9fs_proto_dotu(v9ses))
127 && (v9ses->nodev == 0)) {
129 int major = -1, minor = -1;
131 r = sscanf(stat->extension, "%c %i %i", &type, &major, &minor);
133 p9_debug(P9_DEBUG_ERROR,
134 "invalid device string, umode will be bogus: %s\n",
146 p9_debug(P9_DEBUG_ERROR, "Unknown special type %c %s\n",
147 type, stat->extension);
149 *rdev = MKDEV(major, minor);
157 * v9fs_uflags2omode- convert posix open flags to plan 9 mode bits
158 * @uflags: flags to convert
159 * @extended: if .u extensions are active
162 int v9fs_uflags2omode(int uflags, int extended)
186 if (uflags & O_APPEND)
194 * v9fs_blank_wstat - helper function to setup a 9P stat structure
195 * @wstat: structure to initialize
200 v9fs_blank_wstat(struct p9_wstat *wstat)
204 wstat->qid.type = ~0;
205 wstat->qid.version = ~0;
206 *((long long *)&wstat->qid.path) = ~0;
215 wstat->n_uid = INVALID_UID;
216 wstat->n_gid = INVALID_GID;
217 wstat->n_muid = INVALID_UID;
218 wstat->extension = NULL;
222 * v9fs_alloc_inode - helper function to allocate an inode
223 * @sb: The superblock to allocate the inode from
225 struct inode *v9fs_alloc_inode(struct super_block *sb)
227 struct v9fs_inode *v9inode;
229 v9inode = alloc_inode_sb(sb, v9fs_inode_cache, GFP_KERNEL);
232 v9inode->cache_validity = 0;
233 mutex_init(&v9inode->v_mutex);
234 return &v9inode->netfs.inode;
238 * v9fs_free_inode - destroy an inode
239 * @inode: The inode to be freed
242 void v9fs_free_inode(struct inode *inode)
244 kmem_cache_free(v9fs_inode_cache, V9FS_I(inode));
248 * Set parameters for the netfs library
250 static void v9fs_set_netfs_context(struct inode *inode)
252 struct v9fs_inode *v9inode = V9FS_I(inode);
253 netfs_inode_init(&v9inode->netfs, &v9fs_req_ops);
256 int v9fs_init_inode(struct v9fs_session_info *v9ses,
257 struct inode *inode, umode_t mode, dev_t rdev)
261 inode_init_owner(&nop_mnt_idmap, inode, NULL, mode);
263 inode->i_rdev = rdev;
264 inode->i_atime = inode->i_mtime = inode->i_ctime = current_time(inode);
265 inode->i_mapping->a_ops = &v9fs_addr_operations;
266 inode->i_private = NULL;
268 switch (mode & S_IFMT) {
273 if (v9fs_proto_dotl(v9ses)) {
274 inode->i_op = &v9fs_file_inode_operations_dotl;
275 } else if (v9fs_proto_dotu(v9ses)) {
276 inode->i_op = &v9fs_file_inode_operations;
278 p9_debug(P9_DEBUG_ERROR,
279 "special files without extended mode\n");
283 init_special_inode(inode, inode->i_mode, inode->i_rdev);
286 if (v9fs_proto_dotl(v9ses)) {
287 inode->i_op = &v9fs_file_inode_operations_dotl;
288 inode->i_fop = &v9fs_file_operations_dotl;
290 inode->i_op = &v9fs_file_inode_operations;
291 inode->i_fop = &v9fs_file_operations;
296 if (!v9fs_proto_dotu(v9ses) && !v9fs_proto_dotl(v9ses)) {
297 p9_debug(P9_DEBUG_ERROR,
298 "extended modes used with legacy protocol\n");
303 if (v9fs_proto_dotl(v9ses))
304 inode->i_op = &v9fs_symlink_inode_operations_dotl;
306 inode->i_op = &v9fs_symlink_inode_operations;
311 if (v9fs_proto_dotl(v9ses))
312 inode->i_op = &v9fs_dir_inode_operations_dotl;
313 else if (v9fs_proto_dotu(v9ses))
314 inode->i_op = &v9fs_dir_inode_operations_dotu;
316 inode->i_op = &v9fs_dir_inode_operations;
318 if (v9fs_proto_dotl(v9ses))
319 inode->i_fop = &v9fs_dir_operations_dotl;
321 inode->i_fop = &v9fs_dir_operations;
325 p9_debug(P9_DEBUG_ERROR, "BAD mode 0x%hx S_IFMT 0x%x\n",
326 mode, mode & S_IFMT);
331 v9fs_set_netfs_context(inode);
338 * v9fs_get_inode - helper function to setup an inode
340 * @mode: mode to setup inode with
341 * @rdev: The device numbers to set
344 struct inode *v9fs_get_inode(struct super_block *sb, umode_t mode, dev_t rdev)
348 struct v9fs_session_info *v9ses = sb->s_fs_info;
350 p9_debug(P9_DEBUG_VFS, "super block: %p mode: %ho\n", sb, mode);
352 inode = new_inode(sb);
354 pr_warn("%s (%d): Problem allocating inode\n",
355 __func__, task_pid_nr(current));
356 return ERR_PTR(-ENOMEM);
358 err = v9fs_init_inode(v9ses, inode, mode, rdev);
367 * v9fs_evict_inode - Remove an inode from the inode cache
368 * @inode: inode to release
371 void v9fs_evict_inode(struct inode *inode)
373 struct v9fs_inode __maybe_unused *v9inode = V9FS_I(inode);
374 __le32 __maybe_unused version;
376 truncate_inode_pages_final(&inode->i_data);
378 #ifdef CONFIG_9P_FSCACHE
379 version = cpu_to_le32(v9inode->qid.version);
380 fscache_clear_inode_writeback(v9fs_inode_cookie(v9inode), inode,
385 filemap_fdatawrite(&inode->i_data);
387 #ifdef CONFIG_9P_FSCACHE
388 fscache_relinquish_cookie(v9fs_inode_cookie(v9inode), false);
392 static int v9fs_test_inode(struct inode *inode, void *data)
396 struct v9fs_inode *v9inode = V9FS_I(inode);
397 struct p9_wstat *st = (struct p9_wstat *)data;
398 struct v9fs_session_info *v9ses = v9fs_inode2v9ses(inode);
400 umode = p9mode2unixmode(v9ses, st, &rdev);
401 /* don't match inode of different type */
402 if (inode_wrong_type(inode, umode))
405 /* compare qid details */
406 if (memcmp(&v9inode->qid.version,
407 &st->qid.version, sizeof(v9inode->qid.version)))
410 if (v9inode->qid.type != st->qid.type)
413 if (v9inode->qid.path != st->qid.path)
418 static int v9fs_test_new_inode(struct inode *inode, void *data)
423 static int v9fs_set_inode(struct inode *inode, void *data)
425 struct v9fs_inode *v9inode = V9FS_I(inode);
426 struct p9_wstat *st = (struct p9_wstat *)data;
428 memcpy(&v9inode->qid, &st->qid, sizeof(st->qid));
432 static struct inode *v9fs_qid_iget(struct super_block *sb,
442 struct v9fs_session_info *v9ses = sb->s_fs_info;
443 int (*test)(struct inode *inode, void *data);
446 test = v9fs_test_new_inode;
448 test = v9fs_test_inode;
450 i_ino = v9fs_qid2ino(qid);
451 inode = iget5_locked(sb, i_ino, test, v9fs_set_inode, st);
453 return ERR_PTR(-ENOMEM);
454 if (!(inode->i_state & I_NEW))
457 * initialize the inode with the stat info
458 * FIXME!! we may need support for stale inodes
461 inode->i_ino = i_ino;
462 umode = p9mode2unixmode(v9ses, st, &rdev);
463 retval = v9fs_init_inode(v9ses, inode, umode, rdev);
467 v9fs_stat2inode(st, inode, sb, 0);
468 v9fs_cache_inode_get_cookie(inode);
469 unlock_new_inode(inode);
473 return ERR_PTR(retval);
478 v9fs_inode_from_fid(struct v9fs_session_info *v9ses, struct p9_fid *fid,
479 struct super_block *sb, int new)
482 struct inode *inode = NULL;
484 st = p9_client_stat(fid);
488 inode = v9fs_qid_iget(sb, &st->qid, st, new);
495 * v9fs_at_to_dotl_flags- convert Linux specific AT flags to
497 * @flags: flags to convert
499 static int v9fs_at_to_dotl_flags(int flags)
503 if (flags & AT_REMOVEDIR)
504 rflags |= P9_DOTL_AT_REMOVEDIR;
510 * v9fs_dec_count - helper functon to drop i_nlink.
512 * If a directory had nlink <= 2 (including . and ..), then we should not drop
513 * the link count, which indicates the underlying exported fs doesn't maintain
514 * nlink accurately. e.g.
515 * - overlayfs sets nlink to 1 for merged dir
516 * - ext4 (with dir_nlink feature enabled) sets nlink to 1 if a dir has more
517 * than EXT4_LINK_MAX (65000) links.
519 * @inode: inode whose nlink is being dropped
521 static void v9fs_dec_count(struct inode *inode)
523 if (!S_ISDIR(inode->i_mode) || inode->i_nlink > 2)
528 * v9fs_remove - helper function to remove files and directories
529 * @dir: directory inode that is being deleted
530 * @dentry: dentry that is being deleted
531 * @flags: removing a directory
535 static int v9fs_remove(struct inode *dir, struct dentry *dentry, int flags)
538 int retval = -EOPNOTSUPP;
539 struct p9_fid *v9fid, *dfid;
540 struct v9fs_session_info *v9ses;
542 p9_debug(P9_DEBUG_VFS, "inode: %p dentry: %p rmdir: %x\n",
545 v9ses = v9fs_inode2v9ses(dir);
546 inode = d_inode(dentry);
547 dfid = v9fs_parent_fid(dentry);
549 retval = PTR_ERR(dfid);
550 p9_debug(P9_DEBUG_VFS, "fid lookup failed %d\n", retval);
553 if (v9fs_proto_dotl(v9ses))
554 retval = p9_client_unlinkat(dfid, dentry->d_name.name,
555 v9fs_at_to_dotl_flags(flags));
557 if (retval == -EOPNOTSUPP) {
558 /* Try the one based on path */
559 v9fid = v9fs_fid_clone(dentry);
561 return PTR_ERR(v9fid);
562 retval = p9_client_remove(v9fid);
566 * directories on unlink should have zero
569 if (flags & AT_REMOVEDIR) {
573 v9fs_dec_count(inode);
575 v9fs_invalidate_inode_attr(inode);
576 v9fs_invalidate_inode_attr(dir);
578 /* invalidate all fids associated with dentry */
579 /* NOTE: This will not include open fids */
580 dentry->d_op->d_release(dentry);
586 * v9fs_create - Create a file
587 * @v9ses: session information
588 * @dir: directory that dentry is being created in
589 * @dentry: dentry that is being created
590 * @extension: 9p2000.u extension string to support devices, etc.
591 * @perm: create permissions
595 static struct p9_fid *
596 v9fs_create(struct v9fs_session_info *v9ses, struct inode *dir,
597 struct dentry *dentry, char *extension, u32 perm, u8 mode)
600 const unsigned char *name;
601 struct p9_fid *dfid, *ofid = NULL, *fid = NULL;
604 p9_debug(P9_DEBUG_VFS, "name %pd\n", dentry);
607 name = dentry->d_name.name;
608 dfid = v9fs_parent_fid(dentry);
611 p9_debug(P9_DEBUG_VFS, "fid lookup failed %d\n", err);
615 /* clone a fid to use for creation */
616 ofid = clone_fid(dfid);
619 p9_debug(P9_DEBUG_VFS, "p9_client_walk failed %d\n", err);
623 err = p9_client_fcreate(ofid, name, perm, mode, extension);
625 p9_debug(P9_DEBUG_VFS, "p9_client_fcreate failed %d\n", err);
629 if (!(perm & P9_DMLINK)) {
630 /* now walk from the parent so we can get unopened fid */
631 fid = p9_client_walk(dfid, 1, &name, 1);
634 p9_debug(P9_DEBUG_VFS,
635 "p9_client_walk failed %d\n", err);
639 * instantiate inode and assign the unopened fid to the dentry
641 inode = v9fs_get_new_inode_from_fid(v9ses, fid, dir->i_sb);
643 err = PTR_ERR(inode);
644 p9_debug(P9_DEBUG_VFS,
645 "inode creation failed %d\n", err);
648 v9fs_fid_add(dentry, &fid);
649 d_instantiate(dentry, inode);
661 * v9fs_vfs_create - VFS hook to create a regular file
662 * @idmap: idmap of the mount
663 * @dir: The parent directory
664 * @dentry: The name of file to be created
665 * @mode: The UNIX file mode to set
666 * @excl: True if the file must not yet exist
668 * open(.., O_CREAT) is handled in v9fs_vfs_atomic_open(). This is only called
674 v9fs_vfs_create(struct mnt_idmap *idmap, struct inode *dir,
675 struct dentry *dentry, umode_t mode, bool excl)
677 struct v9fs_session_info *v9ses = v9fs_inode2v9ses(dir);
678 u32 perm = unixmode2p9mode(v9ses, mode);
682 fid = v9fs_create(v9ses, dir, dentry, NULL, perm, P9_ORDWR);
686 v9fs_invalidate_inode_attr(dir);
693 * v9fs_vfs_mkdir - VFS mkdir hook to create a directory
694 * @idmap: idmap of the mount
695 * @dir: inode that is being unlinked
696 * @dentry: dentry that is being unlinked
697 * @mode: mode for new directory
701 static int v9fs_vfs_mkdir(struct mnt_idmap *idmap, struct inode *dir,
702 struct dentry *dentry, umode_t mode)
707 struct v9fs_session_info *v9ses;
709 p9_debug(P9_DEBUG_VFS, "name %pd\n", dentry);
711 v9ses = v9fs_inode2v9ses(dir);
712 perm = unixmode2p9mode(v9ses, mode | S_IFDIR);
713 fid = v9fs_create(v9ses, dir, dentry, NULL, perm, P9_OREAD);
719 v9fs_invalidate_inode_attr(dir);
729 * v9fs_vfs_lookup - VFS lookup hook to "walk" to a new inode
730 * @dir: inode that is being walked from
731 * @dentry: dentry that is being walked to?
732 * @flags: lookup flags (unused)
736 struct dentry *v9fs_vfs_lookup(struct inode *dir, struct dentry *dentry,
740 struct v9fs_session_info *v9ses;
741 struct p9_fid *dfid, *fid;
743 const unsigned char *name;
745 p9_debug(P9_DEBUG_VFS, "dir: %p dentry: (%pd) %p flags: %x\n",
746 dir, dentry, dentry, flags);
748 if (dentry->d_name.len > NAME_MAX)
749 return ERR_PTR(-ENAMETOOLONG);
751 v9ses = v9fs_inode2v9ses(dir);
752 /* We can walk d_parent because we hold the dir->i_mutex */
753 dfid = v9fs_parent_fid(dentry);
755 return ERR_CAST(dfid);
758 * Make sure we don't use a wrong inode due to parallel
759 * unlink. For cached mode create calls request for new
760 * inode. But with cache disabled, lookup should do this.
762 name = dentry->d_name.name;
763 fid = p9_client_walk(dfid, 1, &name, 1);
765 if (fid == ERR_PTR(-ENOENT))
767 else if (IS_ERR(fid))
768 inode = ERR_CAST(fid);
769 else if (v9ses->cache & (CACHE_META|CACHE_LOOSE))
770 inode = v9fs_get_inode_from_fid(v9ses, fid, dir->i_sb);
772 inode = v9fs_get_new_inode_from_fid(v9ses, fid, dir->i_sb);
774 * If we had a rename on the server and a parallel lookup
775 * for the new name, then make sure we instantiate with
776 * the new name. ie look up for a/b, while on server somebody
777 * moved b under k and client parallely did a lookup for
780 res = d_splice_alias(inode, dentry);
783 v9fs_fid_add(dentry, &fid);
784 else if (!IS_ERR(res))
785 v9fs_fid_add(res, &fid);
793 v9fs_vfs_atomic_open(struct inode *dir, struct dentry *dentry,
794 struct file *file, unsigned int flags, umode_t mode)
798 struct v9fs_inode __maybe_unused *v9inode;
799 struct v9fs_session_info *v9ses;
801 struct dentry *res = NULL;
805 if (d_in_lookup(dentry)) {
806 res = v9fs_vfs_lookup(dir, dentry, 0);
815 if (!(flags & O_CREAT) || d_really_is_positive(dentry))
816 return finish_no_open(file, res);
820 v9ses = v9fs_inode2v9ses(dir);
821 perm = unixmode2p9mode(v9ses, mode);
822 p9_omode = v9fs_uflags2omode(flags, v9fs_proto_dotu(v9ses));
824 if ((v9ses->cache & CACHE_WRITEBACK) && (p9_omode & P9_OWRITE)) {
825 p9_omode = (p9_omode & ~P9_OWRITE) | P9_ORDWR;
826 p9_debug(P9_DEBUG_CACHE,
827 "write-only file with writeback enabled, creating w/ O_RDWR\n");
829 fid = v9fs_create(v9ses, dir, dentry, NULL, perm, p9_omode);
835 v9fs_invalidate_inode_attr(dir);
836 inode = d_inode(dentry);
837 v9inode = V9FS_I(inode);
838 err = finish_open(file, dentry, generic_file_open);
842 file->private_data = fid;
843 #ifdef CONFIG_9P_FSCACHE
844 if (v9ses->cache & CACHE_FSCACHE)
845 fscache_use_cookie(v9fs_inode_cookie(v9inode),
846 file->f_mode & FMODE_WRITE);
849 v9fs_fid_add_modes(fid, v9ses->flags, v9ses->cache, file->f_flags);
850 v9fs_open_fid_add(inode, &fid);
852 file->f_mode |= FMODE_CREATED;
863 * v9fs_vfs_unlink - VFS unlink hook to delete an inode
864 * @i: inode that is being unlinked
865 * @d: dentry that is being unlinked
869 int v9fs_vfs_unlink(struct inode *i, struct dentry *d)
871 return v9fs_remove(i, d, 0);
875 * v9fs_vfs_rmdir - VFS unlink hook to delete a directory
876 * @i: inode that is being unlinked
877 * @d: dentry that is being unlinked
881 int v9fs_vfs_rmdir(struct inode *i, struct dentry *d)
883 return v9fs_remove(i, d, AT_REMOVEDIR);
887 * v9fs_vfs_rename - VFS hook to rename an inode
888 * @idmap: The idmap of the mount
889 * @old_dir: old dir inode
890 * @old_dentry: old dentry
891 * @new_dir: new dir inode
892 * @new_dentry: new dentry
893 * @flags: RENAME_* flags
898 v9fs_vfs_rename(struct mnt_idmap *idmap, struct inode *old_dir,
899 struct dentry *old_dentry, struct inode *new_dir,
900 struct dentry *new_dentry, unsigned int flags)
903 struct inode *old_inode;
904 struct inode *new_inode;
905 struct v9fs_session_info *v9ses;
906 struct p9_fid *oldfid = NULL, *dfid = NULL;
907 struct p9_fid *olddirfid = NULL;
908 struct p9_fid *newdirfid = NULL;
909 struct p9_wstat wstat;
914 p9_debug(P9_DEBUG_VFS, "\n");
916 old_inode = d_inode(old_dentry);
917 new_inode = d_inode(new_dentry);
918 v9ses = v9fs_inode2v9ses(old_inode);
919 oldfid = v9fs_fid_lookup(old_dentry);
921 return PTR_ERR(oldfid);
923 dfid = v9fs_parent_fid(old_dentry);
924 olddirfid = clone_fid(dfid);
928 if (IS_ERR(olddirfid)) {
929 retval = PTR_ERR(olddirfid);
933 dfid = v9fs_parent_fid(new_dentry);
934 newdirfid = clone_fid(dfid);
938 if (IS_ERR(newdirfid)) {
939 retval = PTR_ERR(newdirfid);
943 down_write(&v9ses->rename_sem);
944 if (v9fs_proto_dotl(v9ses)) {
945 retval = p9_client_renameat(olddirfid, old_dentry->d_name.name,
946 newdirfid, new_dentry->d_name.name);
947 if (retval == -EOPNOTSUPP)
948 retval = p9_client_rename(oldfid, newdirfid,
949 new_dentry->d_name.name);
950 if (retval != -EOPNOTSUPP)
953 if (old_dentry->d_parent != new_dentry->d_parent) {
955 * 9P .u can only handle file rename in the same directory
958 p9_debug(P9_DEBUG_ERROR, "old dir and new dir are different\n");
962 v9fs_blank_wstat(&wstat);
963 wstat.muid = v9ses->uname;
964 wstat.name = new_dentry->d_name.name;
965 retval = p9_client_wstat(oldfid, &wstat);
970 if (S_ISDIR(new_inode->i_mode))
971 clear_nlink(new_inode);
973 v9fs_dec_count(new_inode);
975 if (S_ISDIR(old_inode->i_mode)) {
978 v9fs_dec_count(old_dir);
980 v9fs_invalidate_inode_attr(old_inode);
981 v9fs_invalidate_inode_attr(old_dir);
982 v9fs_invalidate_inode_attr(new_dir);
984 /* successful rename */
985 d_move(old_dentry, new_dentry);
987 up_write(&v9ses->rename_sem);
990 p9_fid_put(newdirfid);
991 p9_fid_put(olddirfid);
997 * v9fs_vfs_getattr - retrieve file metadata
998 * @idmap: idmap of the mount
999 * @path: Object to query
1000 * @stat: metadata structure to populate
1001 * @request_mask: Mask of STATX_xxx flags indicating the caller's interests
1002 * @flags: AT_STATX_xxx setting
1007 v9fs_vfs_getattr(struct mnt_idmap *idmap, const struct path *path,
1008 struct kstat *stat, u32 request_mask, unsigned int flags)
1010 struct dentry *dentry = path->dentry;
1011 struct inode *inode = d_inode(dentry);
1012 struct v9fs_session_info *v9ses;
1014 struct p9_wstat *st;
1016 p9_debug(P9_DEBUG_VFS, "dentry: %p\n", dentry);
1017 v9ses = v9fs_dentry2v9ses(dentry);
1018 if (v9ses->cache & (CACHE_META|CACHE_LOOSE)) {
1019 generic_fillattr(&nop_mnt_idmap, inode, stat);
1021 } else if (v9ses->cache & CACHE_WRITEBACK) {
1022 if (S_ISREG(inode->i_mode)) {
1023 int retval = filemap_fdatawrite(inode->i_mapping);
1026 p9_debug(P9_DEBUG_ERROR,
1027 "flushing writeback during getattr returned %d\n", retval);
1030 fid = v9fs_fid_lookup(dentry);
1032 return PTR_ERR(fid);
1034 st = p9_client_stat(fid);
1039 v9fs_stat2inode(st, d_inode(dentry), dentry->d_sb, 0);
1040 generic_fillattr(&nop_mnt_idmap, d_inode(dentry), stat);
1048 * v9fs_vfs_setattr - set file metadata
1049 * @idmap: idmap of the mount
1050 * @dentry: file whose metadata to set
1051 * @iattr: metadata assignment structure
1055 static int v9fs_vfs_setattr(struct mnt_idmap *idmap,
1056 struct dentry *dentry, struct iattr *iattr)
1058 int retval, use_dentry = 0;
1059 struct inode *inode = d_inode(dentry);
1060 struct v9fs_session_info *v9ses;
1061 struct p9_fid *fid = NULL;
1062 struct p9_wstat wstat;
1064 p9_debug(P9_DEBUG_VFS, "\n");
1065 retval = setattr_prepare(&nop_mnt_idmap, dentry, iattr);
1070 v9ses = v9fs_dentry2v9ses(dentry);
1071 if (iattr->ia_valid & ATTR_FILE) {
1072 fid = iattr->ia_file->private_data;
1076 fid = v9fs_fid_lookup(dentry);
1080 return PTR_ERR(fid);
1082 v9fs_blank_wstat(&wstat);
1083 if (iattr->ia_valid & ATTR_MODE)
1084 wstat.mode = unixmode2p9mode(v9ses, iattr->ia_mode);
1086 if (iattr->ia_valid & ATTR_MTIME)
1087 wstat.mtime = iattr->ia_mtime.tv_sec;
1089 if (iattr->ia_valid & ATTR_ATIME)
1090 wstat.atime = iattr->ia_atime.tv_sec;
1092 if (iattr->ia_valid & ATTR_SIZE)
1093 wstat.length = iattr->ia_size;
1095 if (v9fs_proto_dotu(v9ses)) {
1096 if (iattr->ia_valid & ATTR_UID)
1097 wstat.n_uid = iattr->ia_uid;
1099 if (iattr->ia_valid & ATTR_GID)
1100 wstat.n_gid = iattr->ia_gid;
1103 /* Write all dirty data */
1104 if (d_is_reg(dentry)) {
1105 retval = filemap_fdatawrite(inode->i_mapping);
1107 p9_debug(P9_DEBUG_ERROR,
1108 "flushing writeback during setattr returned %d\n", retval);
1111 retval = p9_client_wstat(fid, &wstat);
1119 if ((iattr->ia_valid & ATTR_SIZE) &&
1120 iattr->ia_size != i_size_read(inode)) {
1121 truncate_setsize(inode, iattr->ia_size);
1122 truncate_pagecache(inode, iattr->ia_size);
1124 #ifdef CONFIG_9P_FSCACHE
1125 if (v9ses->cache & CACHE_FSCACHE) {
1126 struct v9fs_inode *v9inode = V9FS_I(inode);
1128 fscache_resize_cookie(v9fs_inode_cookie(v9inode), iattr->ia_size);
1133 v9fs_invalidate_inode_attr(inode);
1135 setattr_copy(&nop_mnt_idmap, inode, iattr);
1136 mark_inode_dirty(inode);
1141 * v9fs_stat2inode - populate an inode structure with mistat info
1142 * @stat: Plan 9 metadata (mistat) structure
1143 * @inode: inode to populate
1144 * @sb: superblock of filesystem
1145 * @flags: control flags (e.g. V9FS_STAT2INODE_KEEP_ISIZE)
1150 v9fs_stat2inode(struct p9_wstat *stat, struct inode *inode,
1151 struct super_block *sb, unsigned int flags)
1154 struct v9fs_session_info *v9ses = sb->s_fs_info;
1155 struct v9fs_inode *v9inode = V9FS_I(inode);
1157 set_nlink(inode, 1);
1159 inode->i_atime.tv_sec = stat->atime;
1160 inode->i_mtime.tv_sec = stat->mtime;
1161 inode->i_ctime.tv_sec = stat->mtime;
1163 inode->i_uid = v9ses->dfltuid;
1164 inode->i_gid = v9ses->dfltgid;
1166 if (v9fs_proto_dotu(v9ses)) {
1167 inode->i_uid = stat->n_uid;
1168 inode->i_gid = stat->n_gid;
1170 if ((S_ISREG(inode->i_mode)) || (S_ISDIR(inode->i_mode))) {
1171 if (v9fs_proto_dotu(v9ses)) {
1172 unsigned int i_nlink;
1174 * Hadlink support got added later to the .u extension.
1175 * So there can be a server out there that doesn't
1176 * support this even with .u extension. That would
1177 * just leave us with stat->extension being an empty
1180 /* HARDLINKCOUNT %u */
1181 if (sscanf(stat->extension,
1182 " HARDLINKCOUNT %u", &i_nlink) == 1)
1183 set_nlink(inode, i_nlink);
1186 mode = p9mode2perm(v9ses, stat);
1187 mode |= inode->i_mode & ~S_IALLUGO;
1188 inode->i_mode = mode;
1190 if (!(flags & V9FS_STAT2INODE_KEEP_ISIZE))
1191 v9fs_i_size_write(inode, stat->length);
1192 /* not real number of blocks, but 512 byte ones ... */
1193 inode->i_blocks = (stat->length + 512 - 1) >> 9;
1194 v9inode->cache_validity &= ~V9FS_INO_INVALID_ATTR;
1198 * v9fs_qid2ino - convert qid into inode number
1201 * BUG: potential for inode number collisions?
1204 ino_t v9fs_qid2ino(struct p9_qid *qid)
1206 u64 path = qid->path + 2;
1209 if (sizeof(ino_t) == sizeof(path))
1210 memcpy(&i, &path, sizeof(ino_t));
1212 i = (ino_t) (path ^ (path >> 32));
1218 * v9fs_vfs_get_link - follow a symlink path
1219 * @dentry: dentry for symlink
1220 * @inode: inode for symlink
1221 * @done: delayed call for when we are done with the return value
1224 static const char *v9fs_vfs_get_link(struct dentry *dentry,
1225 struct inode *inode,
1226 struct delayed_call *done)
1228 struct v9fs_session_info *v9ses;
1230 struct p9_wstat *st;
1234 return ERR_PTR(-ECHILD);
1236 v9ses = v9fs_dentry2v9ses(dentry);
1237 if (!v9fs_proto_dotu(v9ses))
1238 return ERR_PTR(-EBADF);
1240 p9_debug(P9_DEBUG_VFS, "%pd\n", dentry);
1241 fid = v9fs_fid_lookup(dentry);
1244 return ERR_CAST(fid);
1246 st = p9_client_stat(fid);
1249 return ERR_CAST(st);
1251 if (!(st->mode & P9_DMSYMLINK)) {
1254 return ERR_PTR(-EINVAL);
1256 res = st->extension;
1257 st->extension = NULL;
1258 if (strlen(res) >= PATH_MAX)
1259 res[PATH_MAX - 1] = '\0';
1263 set_delayed_call(done, kfree_link, res);
1268 * v9fs_vfs_mkspecial - create a special file
1269 * @dir: inode to create special file in
1270 * @dentry: dentry to create
1271 * @perm: mode to create special file
1272 * @extension: 9p2000.u format extension string representing special file
1276 static int v9fs_vfs_mkspecial(struct inode *dir, struct dentry *dentry,
1277 u32 perm, const char *extension)
1280 struct v9fs_session_info *v9ses;
1282 v9ses = v9fs_inode2v9ses(dir);
1283 if (!v9fs_proto_dotu(v9ses)) {
1284 p9_debug(P9_DEBUG_ERROR, "not extended\n");
1288 fid = v9fs_create(v9ses, dir, dentry, (char *) extension, perm,
1291 return PTR_ERR(fid);
1293 v9fs_invalidate_inode_attr(dir);
1299 * v9fs_vfs_symlink - helper function to create symlinks
1300 * @idmap: idmap of the mount
1301 * @dir: directory inode containing symlink
1302 * @dentry: dentry for symlink
1303 * @symname: symlink data
1305 * See Also: 9P2000.u RFC for more information
1310 v9fs_vfs_symlink(struct mnt_idmap *idmap, struct inode *dir,
1311 struct dentry *dentry, const char *symname)
1313 p9_debug(P9_DEBUG_VFS, " %lu,%pd,%s\n",
1314 dir->i_ino, dentry, symname);
1316 return v9fs_vfs_mkspecial(dir, dentry, P9_DMSYMLINK, symname);
1319 #define U32_MAX_DIGITS 10
1322 * v9fs_vfs_link - create a hardlink
1323 * @old_dentry: dentry for file to link to
1324 * @dir: inode destination for new link
1325 * @dentry: dentry for link
1330 v9fs_vfs_link(struct dentry *old_dentry, struct inode *dir,
1331 struct dentry *dentry)
1334 char name[1 + U32_MAX_DIGITS + 2]; /* sign + number + \n + \0 */
1335 struct p9_fid *oldfid;
1337 p9_debug(P9_DEBUG_VFS, " %lu,%pd,%pd\n",
1338 dir->i_ino, dentry, old_dentry);
1340 oldfid = v9fs_fid_clone(old_dentry);
1342 return PTR_ERR(oldfid);
1344 sprintf(name, "%d\n", oldfid->fid);
1345 retval = v9fs_vfs_mkspecial(dir, dentry, P9_DMLINK, name);
1347 v9fs_refresh_inode(oldfid, d_inode(old_dentry));
1348 v9fs_invalidate_inode_attr(dir);
1355 * v9fs_vfs_mknod - create a special file
1356 * @idmap: idmap of the mount
1357 * @dir: inode destination for new link
1358 * @dentry: dentry for file
1359 * @mode: mode for creation
1360 * @rdev: device associated with special file
1365 v9fs_vfs_mknod(struct mnt_idmap *idmap, struct inode *dir,
1366 struct dentry *dentry, umode_t mode, dev_t rdev)
1368 struct v9fs_session_info *v9ses = v9fs_inode2v9ses(dir);
1370 char name[2 + U32_MAX_DIGITS + 1 + U32_MAX_DIGITS + 1];
1373 p9_debug(P9_DEBUG_VFS, " %lu,%pd mode: %x MAJOR: %u MINOR: %u\n",
1374 dir->i_ino, dentry, mode,
1375 MAJOR(rdev), MINOR(rdev));
1377 /* build extension */
1379 sprintf(name, "b %u %u", MAJOR(rdev), MINOR(rdev));
1380 else if (S_ISCHR(mode))
1381 sprintf(name, "c %u %u", MAJOR(rdev), MINOR(rdev));
1385 perm = unixmode2p9mode(v9ses, mode);
1386 retval = v9fs_vfs_mkspecial(dir, dentry, perm, name);
1391 int v9fs_refresh_inode(struct p9_fid *fid, struct inode *inode)
1395 struct p9_wstat *st;
1396 struct v9fs_session_info *v9ses;
1399 v9ses = v9fs_inode2v9ses(inode);
1400 st = p9_client_stat(fid);
1404 * Don't update inode if the file type is different
1406 umode = p9mode2unixmode(v9ses, st, &rdev);
1407 if (inode_wrong_type(inode, umode))
1411 * We don't want to refresh inode->i_size,
1412 * because we may have cached data
1414 flags = (v9ses->cache & CACHE_LOOSE) ?
1415 V9FS_STAT2INODE_KEEP_ISIZE : 0;
1416 v9fs_stat2inode(st, inode, inode->i_sb, flags);
1423 static const struct inode_operations v9fs_dir_inode_operations_dotu = {
1424 .create = v9fs_vfs_create,
1425 .lookup = v9fs_vfs_lookup,
1426 .atomic_open = v9fs_vfs_atomic_open,
1427 .symlink = v9fs_vfs_symlink,
1428 .link = v9fs_vfs_link,
1429 .unlink = v9fs_vfs_unlink,
1430 .mkdir = v9fs_vfs_mkdir,
1431 .rmdir = v9fs_vfs_rmdir,
1432 .mknod = v9fs_vfs_mknod,
1433 .rename = v9fs_vfs_rename,
1434 .getattr = v9fs_vfs_getattr,
1435 .setattr = v9fs_vfs_setattr,
1438 static const struct inode_operations v9fs_dir_inode_operations = {
1439 .create = v9fs_vfs_create,
1440 .lookup = v9fs_vfs_lookup,
1441 .atomic_open = v9fs_vfs_atomic_open,
1442 .unlink = v9fs_vfs_unlink,
1443 .mkdir = v9fs_vfs_mkdir,
1444 .rmdir = v9fs_vfs_rmdir,
1445 .mknod = v9fs_vfs_mknod,
1446 .rename = v9fs_vfs_rename,
1447 .getattr = v9fs_vfs_getattr,
1448 .setattr = v9fs_vfs_setattr,
1451 static const struct inode_operations v9fs_file_inode_operations = {
1452 .getattr = v9fs_vfs_getattr,
1453 .setattr = v9fs_vfs_setattr,
1456 static const struct inode_operations v9fs_symlink_inode_operations = {
1457 .get_link = v9fs_vfs_get_link,
1458 .getattr = v9fs_vfs_getattr,
1459 .setattr = v9fs_vfs_setattr,