1 // SPDX-License-Identifier: GPL-2.0-only
3 * This file contains vfs inode ops for the 9P2000 protocol.
5 * Copyright (C) 2004 by Eric Van Hensbergen <ericvh@gmail.com>
6 * Copyright (C) 2002 by Ron Minnich <rminnich@lanl.gov>
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)
185 if (uflags & O_APPEND)
193 * v9fs_blank_wstat - helper function to setup a 9P stat structure
194 * @wstat: structure to initialize
199 v9fs_blank_wstat(struct p9_wstat *wstat)
203 wstat->qid.type = ~0;
204 wstat->qid.version = ~0;
205 *((long long *)&wstat->qid.path) = ~0;
214 wstat->n_uid = INVALID_UID;
215 wstat->n_gid = INVALID_GID;
216 wstat->n_muid = INVALID_UID;
217 wstat->extension = NULL;
221 * v9fs_alloc_inode - helper function to allocate an inode
222 * @sb: The superblock to allocate the inode from
224 struct inode *v9fs_alloc_inode(struct super_block *sb)
226 struct v9fs_inode *v9inode;
228 v9inode = alloc_inode_sb(sb, v9fs_inode_cache, GFP_KERNEL);
231 v9inode->cache_validity = 0;
232 mutex_init(&v9inode->v_mutex);
233 return &v9inode->netfs.inode;
237 * v9fs_free_inode - destroy an inode
238 * @inode: The inode to be freed
241 void v9fs_free_inode(struct inode *inode)
243 kmem_cache_free(v9fs_inode_cache, V9FS_I(inode));
247 * Set parameters for the netfs library
249 static void v9fs_set_netfs_context(struct inode *inode)
251 struct v9fs_inode *v9inode = V9FS_I(inode);
252 netfs_inode_init(&v9inode->netfs, &v9fs_req_ops);
255 int v9fs_init_inode(struct v9fs_session_info *v9ses,
256 struct inode *inode, umode_t mode, dev_t rdev)
260 inode_init_owner(&nop_mnt_idmap, inode, NULL, mode);
262 inode->i_rdev = rdev;
263 inode->i_atime = inode->i_mtime = inode_set_ctime_current(inode);
264 inode->i_mapping->a_ops = &v9fs_addr_operations;
265 inode->i_private = NULL;
267 switch (mode & S_IFMT) {
272 if (v9fs_proto_dotl(v9ses)) {
273 inode->i_op = &v9fs_file_inode_operations_dotl;
274 } else if (v9fs_proto_dotu(v9ses)) {
275 inode->i_op = &v9fs_file_inode_operations;
277 p9_debug(P9_DEBUG_ERROR,
278 "special files without extended mode\n");
282 init_special_inode(inode, inode->i_mode, inode->i_rdev);
285 if (v9fs_proto_dotl(v9ses)) {
286 inode->i_op = &v9fs_file_inode_operations_dotl;
287 inode->i_fop = &v9fs_file_operations_dotl;
289 inode->i_op = &v9fs_file_inode_operations;
290 inode->i_fop = &v9fs_file_operations;
295 if (!v9fs_proto_dotu(v9ses) && !v9fs_proto_dotl(v9ses)) {
296 p9_debug(P9_DEBUG_ERROR,
297 "extended modes used with legacy protocol\n");
302 if (v9fs_proto_dotl(v9ses))
303 inode->i_op = &v9fs_symlink_inode_operations_dotl;
305 inode->i_op = &v9fs_symlink_inode_operations;
310 if (v9fs_proto_dotl(v9ses))
311 inode->i_op = &v9fs_dir_inode_operations_dotl;
312 else if (v9fs_proto_dotu(v9ses))
313 inode->i_op = &v9fs_dir_inode_operations_dotu;
315 inode->i_op = &v9fs_dir_inode_operations;
317 if (v9fs_proto_dotl(v9ses))
318 inode->i_fop = &v9fs_dir_operations_dotl;
320 inode->i_fop = &v9fs_dir_operations;
324 p9_debug(P9_DEBUG_ERROR, "BAD mode 0x%hx S_IFMT 0x%x\n",
325 mode, mode & S_IFMT);
330 v9fs_set_netfs_context(inode);
337 * v9fs_get_inode - helper function to setup an inode
339 * @mode: mode to setup inode with
340 * @rdev: The device numbers to set
343 struct inode *v9fs_get_inode(struct super_block *sb, umode_t mode, dev_t rdev)
347 struct v9fs_session_info *v9ses = sb->s_fs_info;
349 p9_debug(P9_DEBUG_VFS, "super block: %p mode: %ho\n", sb, mode);
351 inode = new_inode(sb);
353 pr_warn("%s (%d): Problem allocating inode\n",
354 __func__, task_pid_nr(current));
355 return ERR_PTR(-ENOMEM);
357 err = v9fs_init_inode(v9ses, inode, mode, rdev);
366 * v9fs_evict_inode - Remove an inode from the inode cache
367 * @inode: inode to release
370 void v9fs_evict_inode(struct inode *inode)
372 struct v9fs_inode __maybe_unused *v9inode = V9FS_I(inode);
373 __le32 __maybe_unused version;
375 truncate_inode_pages_final(&inode->i_data);
377 #ifdef CONFIG_9P_FSCACHE
378 version = cpu_to_le32(v9inode->qid.version);
379 fscache_clear_inode_writeback(v9fs_inode_cookie(v9inode), inode,
384 filemap_fdatawrite(&inode->i_data);
386 #ifdef CONFIG_9P_FSCACHE
387 fscache_relinquish_cookie(v9fs_inode_cookie(v9inode), false);
391 static int v9fs_test_inode(struct inode *inode, void *data)
395 struct v9fs_inode *v9inode = V9FS_I(inode);
396 struct p9_wstat *st = (struct p9_wstat *)data;
397 struct v9fs_session_info *v9ses = v9fs_inode2v9ses(inode);
399 umode = p9mode2unixmode(v9ses, st, &rdev);
400 /* don't match inode of different type */
401 if (inode_wrong_type(inode, umode))
404 /* compare qid details */
405 if (memcmp(&v9inode->qid.version,
406 &st->qid.version, sizeof(v9inode->qid.version)))
409 if (v9inode->qid.type != st->qid.type)
412 if (v9inode->qid.path != st->qid.path)
417 static int v9fs_test_new_inode(struct inode *inode, void *data)
422 static int v9fs_set_inode(struct inode *inode, void *data)
424 struct v9fs_inode *v9inode = V9FS_I(inode);
425 struct p9_wstat *st = (struct p9_wstat *)data;
427 memcpy(&v9inode->qid, &st->qid, sizeof(st->qid));
431 static struct inode *v9fs_qid_iget(struct super_block *sb,
441 struct v9fs_session_info *v9ses = sb->s_fs_info;
442 int (*test)(struct inode *inode, void *data);
445 test = v9fs_test_new_inode;
447 test = v9fs_test_inode;
449 i_ino = v9fs_qid2ino(qid);
450 inode = iget5_locked(sb, i_ino, test, v9fs_set_inode, st);
452 return ERR_PTR(-ENOMEM);
453 if (!(inode->i_state & I_NEW))
456 * initialize the inode with the stat info
457 * FIXME!! we may need support for stale inodes
460 inode->i_ino = i_ino;
461 umode = p9mode2unixmode(v9ses, st, &rdev);
462 retval = v9fs_init_inode(v9ses, inode, umode, rdev);
466 v9fs_stat2inode(st, inode, sb, 0);
467 v9fs_cache_inode_get_cookie(inode);
468 unlock_new_inode(inode);
472 return ERR_PTR(retval);
477 v9fs_inode_from_fid(struct v9fs_session_info *v9ses, struct p9_fid *fid,
478 struct super_block *sb, int new)
481 struct inode *inode = NULL;
483 st = p9_client_stat(fid);
487 inode = v9fs_qid_iget(sb, &st->qid, st, new);
494 * v9fs_at_to_dotl_flags- convert Linux specific AT flags to
496 * @flags: flags to convert
498 static int v9fs_at_to_dotl_flags(int flags)
502 if (flags & AT_REMOVEDIR)
503 rflags |= P9_DOTL_AT_REMOVEDIR;
509 * v9fs_dec_count - helper functon to drop i_nlink.
511 * If a directory had nlink <= 2 (including . and ..), then we should not drop
512 * the link count, which indicates the underlying exported fs doesn't maintain
513 * nlink accurately. e.g.
514 * - overlayfs sets nlink to 1 for merged dir
515 * - ext4 (with dir_nlink feature enabled) sets nlink to 1 if a dir has more
516 * than EXT4_LINK_MAX (65000) links.
518 * @inode: inode whose nlink is being dropped
520 static void v9fs_dec_count(struct inode *inode)
522 if (!S_ISDIR(inode->i_mode) || inode->i_nlink > 2)
527 * v9fs_remove - helper function to remove files and directories
528 * @dir: directory inode that is being deleted
529 * @dentry: dentry that is being deleted
530 * @flags: removing a directory
534 static int v9fs_remove(struct inode *dir, struct dentry *dentry, int flags)
537 int retval = -EOPNOTSUPP;
538 struct p9_fid *v9fid, *dfid;
539 struct v9fs_session_info *v9ses;
541 p9_debug(P9_DEBUG_VFS, "inode: %p dentry: %p rmdir: %x\n",
544 v9ses = v9fs_inode2v9ses(dir);
545 inode = d_inode(dentry);
546 dfid = v9fs_parent_fid(dentry);
548 retval = PTR_ERR(dfid);
549 p9_debug(P9_DEBUG_VFS, "fid lookup failed %d\n", retval);
552 if (v9fs_proto_dotl(v9ses))
553 retval = p9_client_unlinkat(dfid, dentry->d_name.name,
554 v9fs_at_to_dotl_flags(flags));
556 if (retval == -EOPNOTSUPP) {
557 /* Try the one based on path */
558 v9fid = v9fs_fid_clone(dentry);
560 return PTR_ERR(v9fid);
561 retval = p9_client_remove(v9fid);
565 * directories on unlink should have zero
568 if (flags & AT_REMOVEDIR) {
572 v9fs_dec_count(inode);
574 v9fs_invalidate_inode_attr(inode);
575 v9fs_invalidate_inode_attr(dir);
577 /* invalidate all fids associated with dentry */
578 /* NOTE: This will not include open fids */
579 dentry->d_op->d_release(dentry);
585 * v9fs_create - Create a file
586 * @v9ses: session information
587 * @dir: directory that dentry is being created in
588 * @dentry: dentry that is being created
589 * @extension: 9p2000.u extension string to support devices, etc.
590 * @perm: create permissions
594 static struct p9_fid *
595 v9fs_create(struct v9fs_session_info *v9ses, struct inode *dir,
596 struct dentry *dentry, char *extension, u32 perm, u8 mode)
599 const unsigned char *name;
600 struct p9_fid *dfid, *ofid = NULL, *fid = NULL;
603 p9_debug(P9_DEBUG_VFS, "name %pd\n", dentry);
605 name = dentry->d_name.name;
606 dfid = v9fs_parent_fid(dentry);
609 p9_debug(P9_DEBUG_VFS, "fid lookup failed %d\n", err);
613 /* clone a fid to use for creation */
614 ofid = clone_fid(dfid);
617 p9_debug(P9_DEBUG_VFS, "p9_client_walk failed %d\n", err);
621 err = p9_client_fcreate(ofid, name, perm, mode, extension);
623 p9_debug(P9_DEBUG_VFS, "p9_client_fcreate failed %d\n", err);
627 if (!(perm & P9_DMLINK)) {
628 /* now walk from the parent so we can get unopened fid */
629 fid = p9_client_walk(dfid, 1, &name, 1);
632 p9_debug(P9_DEBUG_VFS,
633 "p9_client_walk failed %d\n", err);
637 * instantiate inode and assign the unopened fid to the dentry
639 inode = v9fs_get_new_inode_from_fid(v9ses, fid, dir->i_sb);
641 err = PTR_ERR(inode);
642 p9_debug(P9_DEBUG_VFS,
643 "inode creation failed %d\n", err);
646 v9fs_fid_add(dentry, &fid);
647 d_instantiate(dentry, inode);
659 * v9fs_vfs_create - VFS hook to create a regular file
660 * @idmap: idmap of the mount
661 * @dir: The parent directory
662 * @dentry: The name of file to be created
663 * @mode: The UNIX file mode to set
664 * @excl: True if the file must not yet exist
666 * open(.., O_CREAT) is handled in v9fs_vfs_atomic_open(). This is only called
672 v9fs_vfs_create(struct mnt_idmap *idmap, struct inode *dir,
673 struct dentry *dentry, umode_t mode, bool excl)
675 struct v9fs_session_info *v9ses = v9fs_inode2v9ses(dir);
676 u32 perm = unixmode2p9mode(v9ses, mode);
680 fid = v9fs_create(v9ses, dir, dentry, NULL, perm, P9_ORDWR);
684 v9fs_invalidate_inode_attr(dir);
691 * v9fs_vfs_mkdir - VFS mkdir hook to create a directory
692 * @idmap: idmap of the mount
693 * @dir: inode that is being unlinked
694 * @dentry: dentry that is being unlinked
695 * @mode: mode for new directory
699 static int v9fs_vfs_mkdir(struct mnt_idmap *idmap, struct inode *dir,
700 struct dentry *dentry, umode_t mode)
705 struct v9fs_session_info *v9ses;
707 p9_debug(P9_DEBUG_VFS, "name %pd\n", dentry);
709 v9ses = v9fs_inode2v9ses(dir);
710 perm = unixmode2p9mode(v9ses, mode | S_IFDIR);
711 fid = v9fs_create(v9ses, dir, dentry, NULL, perm, P9_OREAD);
717 v9fs_invalidate_inode_attr(dir);
727 * v9fs_vfs_lookup - VFS lookup hook to "walk" to a new inode
728 * @dir: inode that is being walked from
729 * @dentry: dentry that is being walked to?
730 * @flags: lookup flags (unused)
734 struct dentry *v9fs_vfs_lookup(struct inode *dir, struct dentry *dentry,
738 struct v9fs_session_info *v9ses;
739 struct p9_fid *dfid, *fid;
741 const unsigned char *name;
743 p9_debug(P9_DEBUG_VFS, "dir: %p dentry: (%pd) %p flags: %x\n",
744 dir, dentry, dentry, flags);
746 if (dentry->d_name.len > NAME_MAX)
747 return ERR_PTR(-ENAMETOOLONG);
749 v9ses = v9fs_inode2v9ses(dir);
750 /* We can walk d_parent because we hold the dir->i_mutex */
751 dfid = v9fs_parent_fid(dentry);
753 return ERR_CAST(dfid);
756 * Make sure we don't use a wrong inode due to parallel
757 * unlink. For cached mode create calls request for new
758 * inode. But with cache disabled, lookup should do this.
760 name = dentry->d_name.name;
761 fid = p9_client_walk(dfid, 1, &name, 1);
763 if (fid == ERR_PTR(-ENOENT))
765 else if (IS_ERR(fid))
766 inode = ERR_CAST(fid);
767 else if (v9ses->cache & (CACHE_META|CACHE_LOOSE))
768 inode = v9fs_get_inode_from_fid(v9ses, fid, dir->i_sb);
770 inode = v9fs_get_new_inode_from_fid(v9ses, fid, dir->i_sb);
772 * If we had a rename on the server and a parallel lookup
773 * for the new name, then make sure we instantiate with
774 * the new name. ie look up for a/b, while on server somebody
775 * moved b under k and client parallely did a lookup for
778 res = d_splice_alias(inode, dentry);
781 v9fs_fid_add(dentry, &fid);
782 else if (!IS_ERR(res))
783 v9fs_fid_add(res, &fid);
791 v9fs_vfs_atomic_open(struct inode *dir, struct dentry *dentry,
792 struct file *file, unsigned int flags, umode_t mode)
796 struct v9fs_inode __maybe_unused *v9inode;
797 struct v9fs_session_info *v9ses;
799 struct dentry *res = NULL;
803 if (d_in_lookup(dentry)) {
804 res = v9fs_vfs_lookup(dir, dentry, 0);
813 if (!(flags & O_CREAT) || d_really_is_positive(dentry))
814 return finish_no_open(file, res);
816 v9ses = v9fs_inode2v9ses(dir);
817 perm = unixmode2p9mode(v9ses, mode);
818 p9_omode = v9fs_uflags2omode(flags, v9fs_proto_dotu(v9ses));
820 if ((v9ses->cache & CACHE_WRITEBACK) && (p9_omode & P9_OWRITE)) {
821 p9_omode = (p9_omode & ~P9_OWRITE) | P9_ORDWR;
822 p9_debug(P9_DEBUG_CACHE,
823 "write-only file with writeback enabled, creating w/ O_RDWR\n");
825 fid = v9fs_create(v9ses, dir, dentry, NULL, perm, p9_omode);
831 v9fs_invalidate_inode_attr(dir);
832 inode = d_inode(dentry);
833 v9inode = V9FS_I(inode);
834 err = finish_open(file, dentry, generic_file_open);
838 file->private_data = fid;
839 #ifdef CONFIG_9P_FSCACHE
840 if (v9ses->cache & CACHE_FSCACHE)
841 fscache_use_cookie(v9fs_inode_cookie(v9inode),
842 file->f_mode & FMODE_WRITE);
845 v9fs_fid_add_modes(fid, v9ses->flags, v9ses->cache, file->f_flags);
846 v9fs_open_fid_add(inode, &fid);
848 file->f_mode |= FMODE_CREATED;
859 * v9fs_vfs_unlink - VFS unlink hook to delete an inode
860 * @i: inode that is being unlinked
861 * @d: dentry that is being unlinked
865 int v9fs_vfs_unlink(struct inode *i, struct dentry *d)
867 return v9fs_remove(i, d, 0);
871 * v9fs_vfs_rmdir - VFS unlink hook to delete a directory
872 * @i: inode that is being unlinked
873 * @d: dentry that is being unlinked
877 int v9fs_vfs_rmdir(struct inode *i, struct dentry *d)
879 return v9fs_remove(i, d, AT_REMOVEDIR);
883 * v9fs_vfs_rename - VFS hook to rename an inode
884 * @idmap: The idmap of the mount
885 * @old_dir: old dir inode
886 * @old_dentry: old dentry
887 * @new_dir: new dir inode
888 * @new_dentry: new dentry
889 * @flags: RENAME_* flags
894 v9fs_vfs_rename(struct mnt_idmap *idmap, struct inode *old_dir,
895 struct dentry *old_dentry, struct inode *new_dir,
896 struct dentry *new_dentry, unsigned int flags)
899 struct inode *old_inode;
900 struct inode *new_inode;
901 struct v9fs_session_info *v9ses;
902 struct p9_fid *oldfid = NULL, *dfid = NULL;
903 struct p9_fid *olddirfid = NULL;
904 struct p9_fid *newdirfid = NULL;
905 struct p9_wstat wstat;
910 p9_debug(P9_DEBUG_VFS, "\n");
911 old_inode = d_inode(old_dentry);
912 new_inode = d_inode(new_dentry);
913 v9ses = v9fs_inode2v9ses(old_inode);
914 oldfid = v9fs_fid_lookup(old_dentry);
916 return PTR_ERR(oldfid);
918 dfid = v9fs_parent_fid(old_dentry);
919 olddirfid = clone_fid(dfid);
923 if (IS_ERR(olddirfid)) {
924 retval = PTR_ERR(olddirfid);
928 dfid = v9fs_parent_fid(new_dentry);
929 newdirfid = clone_fid(dfid);
933 if (IS_ERR(newdirfid)) {
934 retval = PTR_ERR(newdirfid);
938 down_write(&v9ses->rename_sem);
939 if (v9fs_proto_dotl(v9ses)) {
940 retval = p9_client_renameat(olddirfid, old_dentry->d_name.name,
941 newdirfid, new_dentry->d_name.name);
942 if (retval == -EOPNOTSUPP)
943 retval = p9_client_rename(oldfid, newdirfid,
944 new_dentry->d_name.name);
945 if (retval != -EOPNOTSUPP)
948 if (old_dentry->d_parent != new_dentry->d_parent) {
950 * 9P .u can only handle file rename in the same directory
953 p9_debug(P9_DEBUG_ERROR, "old dir and new dir are different\n");
957 v9fs_blank_wstat(&wstat);
958 wstat.muid = v9ses->uname;
959 wstat.name = new_dentry->d_name.name;
960 retval = p9_client_wstat(oldfid, &wstat);
965 if (S_ISDIR(new_inode->i_mode))
966 clear_nlink(new_inode);
968 v9fs_dec_count(new_inode);
970 if (S_ISDIR(old_inode->i_mode)) {
973 v9fs_dec_count(old_dir);
975 v9fs_invalidate_inode_attr(old_inode);
976 v9fs_invalidate_inode_attr(old_dir);
977 v9fs_invalidate_inode_attr(new_dir);
979 /* successful rename */
980 d_move(old_dentry, new_dentry);
982 up_write(&v9ses->rename_sem);
985 p9_fid_put(newdirfid);
986 p9_fid_put(olddirfid);
992 * v9fs_vfs_getattr - retrieve file metadata
993 * @idmap: idmap of the mount
994 * @path: Object to query
995 * @stat: metadata structure to populate
996 * @request_mask: Mask of STATX_xxx flags indicating the caller's interests
997 * @flags: AT_STATX_xxx setting
1002 v9fs_vfs_getattr(struct mnt_idmap *idmap, const struct path *path,
1003 struct kstat *stat, u32 request_mask, unsigned int flags)
1005 struct dentry *dentry = path->dentry;
1006 struct inode *inode = d_inode(dentry);
1007 struct v9fs_session_info *v9ses;
1009 struct p9_wstat *st;
1011 p9_debug(P9_DEBUG_VFS, "dentry: %p\n", dentry);
1012 v9ses = v9fs_dentry2v9ses(dentry);
1013 if (v9ses->cache & (CACHE_META|CACHE_LOOSE)) {
1014 generic_fillattr(&nop_mnt_idmap, request_mask, inode, stat);
1016 } else if (v9ses->cache & CACHE_WRITEBACK) {
1017 if (S_ISREG(inode->i_mode)) {
1018 int retval = filemap_fdatawrite(inode->i_mapping);
1021 p9_debug(P9_DEBUG_ERROR,
1022 "flushing writeback during getattr returned %d\n", retval);
1025 fid = v9fs_fid_lookup(dentry);
1027 return PTR_ERR(fid);
1029 st = p9_client_stat(fid);
1034 v9fs_stat2inode(st, d_inode(dentry), dentry->d_sb, 0);
1035 generic_fillattr(&nop_mnt_idmap, request_mask, d_inode(dentry), stat);
1043 * v9fs_vfs_setattr - set file metadata
1044 * @idmap: idmap of the mount
1045 * @dentry: file whose metadata to set
1046 * @iattr: metadata assignment structure
1050 static int v9fs_vfs_setattr(struct mnt_idmap *idmap,
1051 struct dentry *dentry, struct iattr *iattr)
1053 int retval, use_dentry = 0;
1054 struct inode *inode = d_inode(dentry);
1055 struct v9fs_session_info *v9ses;
1056 struct p9_fid *fid = NULL;
1057 struct p9_wstat wstat;
1059 p9_debug(P9_DEBUG_VFS, "\n");
1060 retval = setattr_prepare(&nop_mnt_idmap, dentry, iattr);
1064 v9ses = v9fs_dentry2v9ses(dentry);
1065 if (iattr->ia_valid & ATTR_FILE) {
1066 fid = iattr->ia_file->private_data;
1070 fid = v9fs_fid_lookup(dentry);
1074 return PTR_ERR(fid);
1076 v9fs_blank_wstat(&wstat);
1077 if (iattr->ia_valid & ATTR_MODE)
1078 wstat.mode = unixmode2p9mode(v9ses, iattr->ia_mode);
1080 if (iattr->ia_valid & ATTR_MTIME)
1081 wstat.mtime = iattr->ia_mtime.tv_sec;
1083 if (iattr->ia_valid & ATTR_ATIME)
1084 wstat.atime = iattr->ia_atime.tv_sec;
1086 if (iattr->ia_valid & ATTR_SIZE)
1087 wstat.length = iattr->ia_size;
1089 if (v9fs_proto_dotu(v9ses)) {
1090 if (iattr->ia_valid & ATTR_UID)
1091 wstat.n_uid = iattr->ia_uid;
1093 if (iattr->ia_valid & ATTR_GID)
1094 wstat.n_gid = iattr->ia_gid;
1097 /* Write all dirty data */
1098 if (d_is_reg(dentry)) {
1099 retval = filemap_fdatawrite(inode->i_mapping);
1101 p9_debug(P9_DEBUG_ERROR,
1102 "flushing writeback during setattr returned %d\n", retval);
1105 retval = p9_client_wstat(fid, &wstat);
1113 if ((iattr->ia_valid & ATTR_SIZE) &&
1114 iattr->ia_size != i_size_read(inode)) {
1115 truncate_setsize(inode, iattr->ia_size);
1116 truncate_pagecache(inode, iattr->ia_size);
1118 #ifdef CONFIG_9P_FSCACHE
1119 if (v9ses->cache & CACHE_FSCACHE) {
1120 struct v9fs_inode *v9inode = V9FS_I(inode);
1122 fscache_resize_cookie(v9fs_inode_cookie(v9inode), iattr->ia_size);
1127 v9fs_invalidate_inode_attr(inode);
1129 setattr_copy(&nop_mnt_idmap, inode, iattr);
1130 mark_inode_dirty(inode);
1135 * v9fs_stat2inode - populate an inode structure with mistat info
1136 * @stat: Plan 9 metadata (mistat) structure
1137 * @inode: inode to populate
1138 * @sb: superblock of filesystem
1139 * @flags: control flags (e.g. V9FS_STAT2INODE_KEEP_ISIZE)
1144 v9fs_stat2inode(struct p9_wstat *stat, struct inode *inode,
1145 struct super_block *sb, unsigned int flags)
1148 struct v9fs_session_info *v9ses = sb->s_fs_info;
1149 struct v9fs_inode *v9inode = V9FS_I(inode);
1151 set_nlink(inode, 1);
1153 inode->i_atime.tv_sec = stat->atime;
1154 inode->i_mtime.tv_sec = stat->mtime;
1155 inode_set_ctime(inode, stat->mtime, 0);
1157 inode->i_uid = v9ses->dfltuid;
1158 inode->i_gid = v9ses->dfltgid;
1160 if (v9fs_proto_dotu(v9ses)) {
1161 inode->i_uid = stat->n_uid;
1162 inode->i_gid = stat->n_gid;
1164 if ((S_ISREG(inode->i_mode)) || (S_ISDIR(inode->i_mode))) {
1165 if (v9fs_proto_dotu(v9ses)) {
1166 unsigned int i_nlink;
1168 * Hadlink support got added later to the .u extension.
1169 * So there can be a server out there that doesn't
1170 * support this even with .u extension. That would
1171 * just leave us with stat->extension being an empty
1174 /* HARDLINKCOUNT %u */
1175 if (sscanf(stat->extension,
1176 " HARDLINKCOUNT %u", &i_nlink) == 1)
1177 set_nlink(inode, i_nlink);
1180 mode = p9mode2perm(v9ses, stat);
1181 mode |= inode->i_mode & ~S_IALLUGO;
1182 inode->i_mode = mode;
1184 if (!(flags & V9FS_STAT2INODE_KEEP_ISIZE))
1185 v9fs_i_size_write(inode, stat->length);
1186 /* not real number of blocks, but 512 byte ones ... */
1187 inode->i_blocks = (stat->length + 512 - 1) >> 9;
1188 v9inode->cache_validity &= ~V9FS_INO_INVALID_ATTR;
1192 * v9fs_qid2ino - convert qid into inode number
1195 * BUG: potential for inode number collisions?
1198 ino_t v9fs_qid2ino(struct p9_qid *qid)
1200 u64 path = qid->path + 2;
1203 if (sizeof(ino_t) == sizeof(path))
1204 memcpy(&i, &path, sizeof(ino_t));
1206 i = (ino_t) (path ^ (path >> 32));
1212 * v9fs_vfs_get_link - follow a symlink path
1213 * @dentry: dentry for symlink
1214 * @inode: inode for symlink
1215 * @done: delayed call for when we are done with the return value
1218 static const char *v9fs_vfs_get_link(struct dentry *dentry,
1219 struct inode *inode,
1220 struct delayed_call *done)
1222 struct v9fs_session_info *v9ses;
1224 struct p9_wstat *st;
1228 return ERR_PTR(-ECHILD);
1230 v9ses = v9fs_dentry2v9ses(dentry);
1231 if (!v9fs_proto_dotu(v9ses))
1232 return ERR_PTR(-EBADF);
1234 p9_debug(P9_DEBUG_VFS, "%pd\n", dentry);
1235 fid = v9fs_fid_lookup(dentry);
1238 return ERR_CAST(fid);
1240 st = p9_client_stat(fid);
1243 return ERR_CAST(st);
1245 if (!(st->mode & P9_DMSYMLINK)) {
1248 return ERR_PTR(-EINVAL);
1250 res = st->extension;
1251 st->extension = NULL;
1252 if (strlen(res) >= PATH_MAX)
1253 res[PATH_MAX - 1] = '\0';
1257 set_delayed_call(done, kfree_link, res);
1262 * v9fs_vfs_mkspecial - create a special file
1263 * @dir: inode to create special file in
1264 * @dentry: dentry to create
1265 * @perm: mode to create special file
1266 * @extension: 9p2000.u format extension string representing special file
1270 static int v9fs_vfs_mkspecial(struct inode *dir, struct dentry *dentry,
1271 u32 perm, const char *extension)
1274 struct v9fs_session_info *v9ses;
1276 v9ses = v9fs_inode2v9ses(dir);
1277 if (!v9fs_proto_dotu(v9ses)) {
1278 p9_debug(P9_DEBUG_ERROR, "not extended\n");
1282 fid = v9fs_create(v9ses, dir, dentry, (char *) extension, perm,
1285 return PTR_ERR(fid);
1287 v9fs_invalidate_inode_attr(dir);
1293 * v9fs_vfs_symlink - helper function to create symlinks
1294 * @idmap: idmap of the mount
1295 * @dir: directory inode containing symlink
1296 * @dentry: dentry for symlink
1297 * @symname: symlink data
1299 * See Also: 9P2000.u RFC for more information
1304 v9fs_vfs_symlink(struct mnt_idmap *idmap, struct inode *dir,
1305 struct dentry *dentry, const char *symname)
1307 p9_debug(P9_DEBUG_VFS, " %lu,%pd,%s\n",
1308 dir->i_ino, dentry, symname);
1310 return v9fs_vfs_mkspecial(dir, dentry, P9_DMSYMLINK, symname);
1313 #define U32_MAX_DIGITS 10
1316 * v9fs_vfs_link - create a hardlink
1317 * @old_dentry: dentry for file to link to
1318 * @dir: inode destination for new link
1319 * @dentry: dentry for link
1324 v9fs_vfs_link(struct dentry *old_dentry, struct inode *dir,
1325 struct dentry *dentry)
1328 char name[1 + U32_MAX_DIGITS + 2]; /* sign + number + \n + \0 */
1329 struct p9_fid *oldfid;
1331 p9_debug(P9_DEBUG_VFS, " %lu,%pd,%pd\n",
1332 dir->i_ino, dentry, old_dentry);
1334 oldfid = v9fs_fid_clone(old_dentry);
1336 return PTR_ERR(oldfid);
1338 sprintf(name, "%d\n", oldfid->fid);
1339 retval = v9fs_vfs_mkspecial(dir, dentry, P9_DMLINK, name);
1341 v9fs_refresh_inode(oldfid, d_inode(old_dentry));
1342 v9fs_invalidate_inode_attr(dir);
1349 * v9fs_vfs_mknod - create a special file
1350 * @idmap: idmap of the mount
1351 * @dir: inode destination for new link
1352 * @dentry: dentry for file
1353 * @mode: mode for creation
1354 * @rdev: device associated with special file
1359 v9fs_vfs_mknod(struct mnt_idmap *idmap, struct inode *dir,
1360 struct dentry *dentry, umode_t mode, dev_t rdev)
1362 struct v9fs_session_info *v9ses = v9fs_inode2v9ses(dir);
1364 char name[2 + U32_MAX_DIGITS + 1 + U32_MAX_DIGITS + 1];
1367 p9_debug(P9_DEBUG_VFS, " %lu,%pd mode: %x MAJOR: %u MINOR: %u\n",
1368 dir->i_ino, dentry, mode,
1369 MAJOR(rdev), MINOR(rdev));
1371 /* build extension */
1373 sprintf(name, "b %u %u", MAJOR(rdev), MINOR(rdev));
1374 else if (S_ISCHR(mode))
1375 sprintf(name, "c %u %u", MAJOR(rdev), MINOR(rdev));
1379 perm = unixmode2p9mode(v9ses, mode);
1380 retval = v9fs_vfs_mkspecial(dir, dentry, perm, name);
1385 int v9fs_refresh_inode(struct p9_fid *fid, struct inode *inode)
1389 struct p9_wstat *st;
1390 struct v9fs_session_info *v9ses;
1393 v9ses = v9fs_inode2v9ses(inode);
1394 st = p9_client_stat(fid);
1398 * Don't update inode if the file type is different
1400 umode = p9mode2unixmode(v9ses, st, &rdev);
1401 if (inode_wrong_type(inode, umode))
1405 * We don't want to refresh inode->i_size,
1406 * because we may have cached data
1408 flags = (v9ses->cache & CACHE_LOOSE) ?
1409 V9FS_STAT2INODE_KEEP_ISIZE : 0;
1410 v9fs_stat2inode(st, inode, inode->i_sb, flags);
1417 static const struct inode_operations v9fs_dir_inode_operations_dotu = {
1418 .create = v9fs_vfs_create,
1419 .lookup = v9fs_vfs_lookup,
1420 .atomic_open = v9fs_vfs_atomic_open,
1421 .symlink = v9fs_vfs_symlink,
1422 .link = v9fs_vfs_link,
1423 .unlink = v9fs_vfs_unlink,
1424 .mkdir = v9fs_vfs_mkdir,
1425 .rmdir = v9fs_vfs_rmdir,
1426 .mknod = v9fs_vfs_mknod,
1427 .rename = v9fs_vfs_rename,
1428 .getattr = v9fs_vfs_getattr,
1429 .setattr = v9fs_vfs_setattr,
1432 static const struct inode_operations v9fs_dir_inode_operations = {
1433 .create = v9fs_vfs_create,
1434 .lookup = v9fs_vfs_lookup,
1435 .atomic_open = v9fs_vfs_atomic_open,
1436 .unlink = v9fs_vfs_unlink,
1437 .mkdir = v9fs_vfs_mkdir,
1438 .rmdir = v9fs_vfs_rmdir,
1439 .mknod = v9fs_vfs_mknod,
1440 .rename = v9fs_vfs_rename,
1441 .getattr = v9fs_vfs_getattr,
1442 .setattr = v9fs_vfs_setattr,
1445 static const struct inode_operations v9fs_file_inode_operations = {
1446 .getattr = v9fs_vfs_getattr,
1447 .setattr = v9fs_vfs_setattr,
1450 static const struct inode_operations v9fs_symlink_inode_operations = {
1451 .get_link = v9fs_vfs_get_link,
1452 .getattr = v9fs_vfs_getattr,
1453 .setattr = v9fs_vfs_setattr,