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 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, true);
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 simple_inode_init_ts(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);
335 * v9fs_get_inode - helper function to setup an inode
337 * @mode: mode to setup inode with
338 * @rdev: The device numbers to set
341 struct inode *v9fs_get_inode(struct super_block *sb, umode_t mode, dev_t rdev)
345 struct v9fs_session_info *v9ses = sb->s_fs_info;
347 p9_debug(P9_DEBUG_VFS, "super block: %p mode: %ho\n", sb, mode);
349 inode = new_inode(sb);
351 pr_warn("%s (%d): Problem allocating inode\n",
352 __func__, task_pid_nr(current));
353 return ERR_PTR(-ENOMEM);
355 err = v9fs_init_inode(v9ses, inode, mode, rdev);
360 v9fs_set_netfs_context(inode);
365 * v9fs_evict_inode - Remove an inode from the inode cache
366 * @inode: inode to release
369 void v9fs_evict_inode(struct inode *inode)
371 struct v9fs_inode __maybe_unused *v9inode = V9FS_I(inode);
372 __le32 __maybe_unused version;
374 truncate_inode_pages_final(&inode->i_data);
376 version = cpu_to_le32(v9inode->qid.version);
377 netfs_clear_inode_writeback(inode, &version);
380 filemap_fdatawrite(&inode->i_data);
382 #ifdef CONFIG_9P_FSCACHE
383 fscache_relinquish_cookie(v9fs_inode_cookie(v9inode), false);
387 static int v9fs_test_inode(struct inode *inode, void *data)
391 struct v9fs_inode *v9inode = V9FS_I(inode);
392 struct p9_wstat *st = (struct p9_wstat *)data;
393 struct v9fs_session_info *v9ses = v9fs_inode2v9ses(inode);
395 umode = p9mode2unixmode(v9ses, st, &rdev);
396 /* don't match inode of different type */
397 if (inode_wrong_type(inode, umode))
400 /* compare qid details */
401 if (memcmp(&v9inode->qid.version,
402 &st->qid.version, sizeof(v9inode->qid.version)))
405 if (v9inode->qid.type != st->qid.type)
408 if (v9inode->qid.path != st->qid.path)
413 static int v9fs_test_new_inode(struct inode *inode, void *data)
418 static int v9fs_set_inode(struct inode *inode, void *data)
420 struct v9fs_inode *v9inode = V9FS_I(inode);
421 struct p9_wstat *st = (struct p9_wstat *)data;
423 memcpy(&v9inode->qid, &st->qid, sizeof(st->qid));
427 static struct inode *v9fs_qid_iget(struct super_block *sb,
437 struct v9fs_session_info *v9ses = sb->s_fs_info;
438 int (*test)(struct inode *inode, void *data);
441 test = v9fs_test_new_inode;
443 test = v9fs_test_inode;
445 i_ino = v9fs_qid2ino(qid);
446 inode = iget5_locked(sb, i_ino, test, v9fs_set_inode, st);
448 return ERR_PTR(-ENOMEM);
449 if (!(inode->i_state & I_NEW))
452 * initialize the inode with the stat info
453 * FIXME!! we may need support for stale inodes
456 inode->i_ino = i_ino;
457 umode = p9mode2unixmode(v9ses, st, &rdev);
458 retval = v9fs_init_inode(v9ses, inode, umode, rdev);
462 v9fs_stat2inode(st, inode, sb, 0);
463 v9fs_set_netfs_context(inode);
464 v9fs_cache_inode_get_cookie(inode);
465 unlock_new_inode(inode);
469 return ERR_PTR(retval);
474 v9fs_inode_from_fid(struct v9fs_session_info *v9ses, struct p9_fid *fid,
475 struct super_block *sb, int new)
478 struct inode *inode = NULL;
480 st = p9_client_stat(fid);
484 inode = v9fs_qid_iget(sb, &st->qid, st, new);
491 * v9fs_at_to_dotl_flags- convert Linux specific AT flags to
493 * @flags: flags to convert
495 static int v9fs_at_to_dotl_flags(int flags)
499 if (flags & AT_REMOVEDIR)
500 rflags |= P9_DOTL_AT_REMOVEDIR;
506 * v9fs_dec_count - helper functon to drop i_nlink.
508 * If a directory had nlink <= 2 (including . and ..), then we should not drop
509 * the link count, which indicates the underlying exported fs doesn't maintain
510 * nlink accurately. e.g.
511 * - overlayfs sets nlink to 1 for merged dir
512 * - ext4 (with dir_nlink feature enabled) sets nlink to 1 if a dir has more
513 * than EXT4_LINK_MAX (65000) links.
515 * @inode: inode whose nlink is being dropped
517 static void v9fs_dec_count(struct inode *inode)
519 if (!S_ISDIR(inode->i_mode) || inode->i_nlink > 2)
524 * v9fs_remove - helper function to remove files and directories
525 * @dir: directory inode that is being deleted
526 * @dentry: dentry that is being deleted
527 * @flags: removing a directory
531 static int v9fs_remove(struct inode *dir, struct dentry *dentry, int flags)
534 int retval = -EOPNOTSUPP;
535 struct p9_fid *v9fid, *dfid;
536 struct v9fs_session_info *v9ses;
538 p9_debug(P9_DEBUG_VFS, "inode: %p dentry: %p rmdir: %x\n",
541 v9ses = v9fs_inode2v9ses(dir);
542 inode = d_inode(dentry);
543 dfid = v9fs_parent_fid(dentry);
545 retval = PTR_ERR(dfid);
546 p9_debug(P9_DEBUG_VFS, "fid lookup failed %d\n", retval);
549 if (v9fs_proto_dotl(v9ses))
550 retval = p9_client_unlinkat(dfid, dentry->d_name.name,
551 v9fs_at_to_dotl_flags(flags));
553 if (retval == -EOPNOTSUPP) {
554 /* Try the one based on path */
555 v9fid = v9fs_fid_clone(dentry);
557 return PTR_ERR(v9fid);
558 retval = p9_client_remove(v9fid);
562 * directories on unlink should have zero
565 if (flags & AT_REMOVEDIR) {
569 v9fs_dec_count(inode);
571 v9fs_invalidate_inode_attr(inode);
572 v9fs_invalidate_inode_attr(dir);
574 /* invalidate all fids associated with dentry */
575 /* NOTE: This will not include open fids */
576 dentry->d_op->d_release(dentry);
582 * v9fs_create - Create a file
583 * @v9ses: session information
584 * @dir: directory that dentry is being created in
585 * @dentry: dentry that is being created
586 * @extension: 9p2000.u extension string to support devices, etc.
587 * @perm: create permissions
591 static struct p9_fid *
592 v9fs_create(struct v9fs_session_info *v9ses, struct inode *dir,
593 struct dentry *dentry, char *extension, u32 perm, u8 mode)
596 const unsigned char *name;
597 struct p9_fid *dfid, *ofid = NULL, *fid = NULL;
600 p9_debug(P9_DEBUG_VFS, "name %pd\n", dentry);
602 name = dentry->d_name.name;
603 dfid = v9fs_parent_fid(dentry);
606 p9_debug(P9_DEBUG_VFS, "fid lookup failed %d\n", err);
610 /* clone a fid to use for creation */
611 ofid = clone_fid(dfid);
614 p9_debug(P9_DEBUG_VFS, "p9_client_walk failed %d\n", err);
618 err = p9_client_fcreate(ofid, name, perm, mode, extension);
620 p9_debug(P9_DEBUG_VFS, "p9_client_fcreate failed %d\n", err);
624 if (!(perm & P9_DMLINK)) {
625 /* now walk from the parent so we can get unopened fid */
626 fid = p9_client_walk(dfid, 1, &name, 1);
629 p9_debug(P9_DEBUG_VFS,
630 "p9_client_walk failed %d\n", err);
634 * instantiate inode and assign the unopened fid to the dentry
636 inode = v9fs_get_new_inode_from_fid(v9ses, fid, dir->i_sb);
638 err = PTR_ERR(inode);
639 p9_debug(P9_DEBUG_VFS,
640 "inode creation failed %d\n", err);
643 v9fs_fid_add(dentry, &fid);
644 d_instantiate(dentry, inode);
656 * v9fs_vfs_create - VFS hook to create a regular file
657 * @idmap: idmap of the mount
658 * @dir: The parent directory
659 * @dentry: The name of file to be created
660 * @mode: The UNIX file mode to set
661 * @excl: True if the file must not yet exist
663 * open(.., O_CREAT) is handled in v9fs_vfs_atomic_open(). This is only called
669 v9fs_vfs_create(struct mnt_idmap *idmap, struct inode *dir,
670 struct dentry *dentry, umode_t mode, bool excl)
672 struct v9fs_session_info *v9ses = v9fs_inode2v9ses(dir);
673 u32 perm = unixmode2p9mode(v9ses, mode);
677 fid = v9fs_create(v9ses, dir, dentry, NULL, perm, P9_ORDWR);
681 v9fs_invalidate_inode_attr(dir);
688 * v9fs_vfs_mkdir - VFS mkdir hook to create a directory
689 * @idmap: idmap of the mount
690 * @dir: inode that is being unlinked
691 * @dentry: dentry that is being unlinked
692 * @mode: mode for new directory
696 static int v9fs_vfs_mkdir(struct mnt_idmap *idmap, struct inode *dir,
697 struct dentry *dentry, umode_t mode)
702 struct v9fs_session_info *v9ses;
704 p9_debug(P9_DEBUG_VFS, "name %pd\n", dentry);
706 v9ses = v9fs_inode2v9ses(dir);
707 perm = unixmode2p9mode(v9ses, mode | S_IFDIR);
708 fid = v9fs_create(v9ses, dir, dentry, NULL, perm, P9_OREAD);
714 v9fs_invalidate_inode_attr(dir);
724 * v9fs_vfs_lookup - VFS lookup hook to "walk" to a new inode
725 * @dir: inode that is being walked from
726 * @dentry: dentry that is being walked to?
727 * @flags: lookup flags (unused)
731 struct dentry *v9fs_vfs_lookup(struct inode *dir, struct dentry *dentry,
735 struct v9fs_session_info *v9ses;
736 struct p9_fid *dfid, *fid;
738 const unsigned char *name;
740 p9_debug(P9_DEBUG_VFS, "dir: %p dentry: (%pd) %p flags: %x\n",
741 dir, dentry, dentry, flags);
743 if (dentry->d_name.len > NAME_MAX)
744 return ERR_PTR(-ENAMETOOLONG);
746 v9ses = v9fs_inode2v9ses(dir);
747 /* We can walk d_parent because we hold the dir->i_mutex */
748 dfid = v9fs_parent_fid(dentry);
750 return ERR_CAST(dfid);
753 * Make sure we don't use a wrong inode due to parallel
754 * unlink. For cached mode create calls request for new
755 * inode. But with cache disabled, lookup should do this.
757 name = dentry->d_name.name;
758 fid = p9_client_walk(dfid, 1, &name, 1);
760 if (fid == ERR_PTR(-ENOENT))
762 else if (IS_ERR(fid))
763 inode = ERR_CAST(fid);
764 else if (v9ses->cache & (CACHE_META|CACHE_LOOSE))
765 inode = v9fs_get_inode_from_fid(v9ses, fid, dir->i_sb);
767 inode = v9fs_get_new_inode_from_fid(v9ses, fid, dir->i_sb);
769 * If we had a rename on the server and a parallel lookup
770 * for the new name, then make sure we instantiate with
771 * the new name. ie look up for a/b, while on server somebody
772 * moved b under k and client parallely did a lookup for
775 res = d_splice_alias(inode, dentry);
778 v9fs_fid_add(dentry, &fid);
779 else if (!IS_ERR(res))
780 v9fs_fid_add(res, &fid);
788 v9fs_vfs_atomic_open(struct inode *dir, struct dentry *dentry,
789 struct file *file, unsigned int flags, umode_t mode)
793 struct v9fs_inode __maybe_unused *v9inode;
794 struct v9fs_session_info *v9ses;
796 struct dentry *res = NULL;
800 if (d_in_lookup(dentry)) {
801 res = v9fs_vfs_lookup(dir, dentry, 0);
810 if (!(flags & O_CREAT) || d_really_is_positive(dentry))
811 return finish_no_open(file, res);
813 v9ses = v9fs_inode2v9ses(dir);
814 perm = unixmode2p9mode(v9ses, mode);
815 p9_omode = v9fs_uflags2omode(flags, v9fs_proto_dotu(v9ses));
817 if ((v9ses->cache & CACHE_WRITEBACK) && (p9_omode & P9_OWRITE)) {
818 p9_omode = (p9_omode & ~P9_OWRITE) | P9_ORDWR;
819 p9_debug(P9_DEBUG_CACHE,
820 "write-only file with writeback enabled, creating w/ O_RDWR\n");
822 fid = v9fs_create(v9ses, dir, dentry, NULL, perm, p9_omode);
828 v9fs_invalidate_inode_attr(dir);
829 inode = d_inode(dentry);
830 v9inode = V9FS_I(inode);
831 err = finish_open(file, dentry, generic_file_open);
835 file->private_data = fid;
836 #ifdef CONFIG_9P_FSCACHE
837 if (v9ses->cache & CACHE_FSCACHE)
838 fscache_use_cookie(v9fs_inode_cookie(v9inode),
839 file->f_mode & FMODE_WRITE);
842 v9fs_fid_add_modes(fid, v9ses->flags, v9ses->cache, file->f_flags);
843 v9fs_open_fid_add(inode, &fid);
845 file->f_mode |= FMODE_CREATED;
856 * v9fs_vfs_unlink - VFS unlink hook to delete an inode
857 * @i: inode that is being unlinked
858 * @d: dentry that is being unlinked
862 int v9fs_vfs_unlink(struct inode *i, struct dentry *d)
864 return v9fs_remove(i, d, 0);
868 * v9fs_vfs_rmdir - VFS unlink hook to delete a directory
869 * @i: inode that is being unlinked
870 * @d: dentry that is being unlinked
874 int v9fs_vfs_rmdir(struct inode *i, struct dentry *d)
876 return v9fs_remove(i, d, AT_REMOVEDIR);
880 * v9fs_vfs_rename - VFS hook to rename an inode
881 * @idmap: The idmap of the mount
882 * @old_dir: old dir inode
883 * @old_dentry: old dentry
884 * @new_dir: new dir inode
885 * @new_dentry: new dentry
886 * @flags: RENAME_* flags
891 v9fs_vfs_rename(struct mnt_idmap *idmap, struct inode *old_dir,
892 struct dentry *old_dentry, struct inode *new_dir,
893 struct dentry *new_dentry, unsigned int flags)
896 struct inode *old_inode;
897 struct inode *new_inode;
898 struct v9fs_session_info *v9ses;
899 struct p9_fid *oldfid = NULL, *dfid = NULL;
900 struct p9_fid *olddirfid = NULL;
901 struct p9_fid *newdirfid = NULL;
902 struct p9_wstat wstat;
907 p9_debug(P9_DEBUG_VFS, "\n");
908 old_inode = d_inode(old_dentry);
909 new_inode = d_inode(new_dentry);
910 v9ses = v9fs_inode2v9ses(old_inode);
911 oldfid = v9fs_fid_lookup(old_dentry);
913 return PTR_ERR(oldfid);
915 dfid = v9fs_parent_fid(old_dentry);
916 olddirfid = clone_fid(dfid);
920 if (IS_ERR(olddirfid)) {
921 retval = PTR_ERR(olddirfid);
925 dfid = v9fs_parent_fid(new_dentry);
926 newdirfid = clone_fid(dfid);
930 if (IS_ERR(newdirfid)) {
931 retval = PTR_ERR(newdirfid);
935 down_write(&v9ses->rename_sem);
936 if (v9fs_proto_dotl(v9ses)) {
937 retval = p9_client_renameat(olddirfid, old_dentry->d_name.name,
938 newdirfid, new_dentry->d_name.name);
939 if (retval == -EOPNOTSUPP)
940 retval = p9_client_rename(oldfid, newdirfid,
941 new_dentry->d_name.name);
942 if (retval != -EOPNOTSUPP)
945 if (old_dentry->d_parent != new_dentry->d_parent) {
947 * 9P .u can only handle file rename in the same directory
950 p9_debug(P9_DEBUG_ERROR, "old dir and new dir are different\n");
954 v9fs_blank_wstat(&wstat);
955 wstat.muid = v9ses->uname;
956 wstat.name = new_dentry->d_name.name;
957 retval = p9_client_wstat(oldfid, &wstat);
962 if (S_ISDIR(new_inode->i_mode))
963 clear_nlink(new_inode);
965 v9fs_dec_count(new_inode);
967 if (S_ISDIR(old_inode->i_mode)) {
970 v9fs_dec_count(old_dir);
972 v9fs_invalidate_inode_attr(old_inode);
973 v9fs_invalidate_inode_attr(old_dir);
974 v9fs_invalidate_inode_attr(new_dir);
976 /* successful rename */
977 d_move(old_dentry, new_dentry);
979 up_write(&v9ses->rename_sem);
982 p9_fid_put(newdirfid);
983 p9_fid_put(olddirfid);
989 * v9fs_vfs_getattr - retrieve file metadata
990 * @idmap: idmap of the mount
991 * @path: Object to query
992 * @stat: metadata structure to populate
993 * @request_mask: Mask of STATX_xxx flags indicating the caller's interests
994 * @flags: AT_STATX_xxx setting
999 v9fs_vfs_getattr(struct mnt_idmap *idmap, const struct path *path,
1000 struct kstat *stat, u32 request_mask, unsigned int flags)
1002 struct dentry *dentry = path->dentry;
1003 struct inode *inode = d_inode(dentry);
1004 struct v9fs_session_info *v9ses;
1006 struct p9_wstat *st;
1008 p9_debug(P9_DEBUG_VFS, "dentry: %p\n", dentry);
1009 v9ses = v9fs_dentry2v9ses(dentry);
1010 if (v9ses->cache & (CACHE_META|CACHE_LOOSE)) {
1011 generic_fillattr(&nop_mnt_idmap, request_mask, inode, stat);
1013 } else if (v9ses->cache & CACHE_WRITEBACK) {
1014 if (S_ISREG(inode->i_mode)) {
1015 int retval = filemap_fdatawrite(inode->i_mapping);
1018 p9_debug(P9_DEBUG_ERROR,
1019 "flushing writeback during getattr returned %d\n", retval);
1022 fid = v9fs_fid_lookup(dentry);
1024 return PTR_ERR(fid);
1026 st = p9_client_stat(fid);
1031 v9fs_stat2inode(st, d_inode(dentry), dentry->d_sb, 0);
1032 generic_fillattr(&nop_mnt_idmap, request_mask, d_inode(dentry), stat);
1040 * v9fs_vfs_setattr - set file metadata
1041 * @idmap: idmap of the mount
1042 * @dentry: file whose metadata to set
1043 * @iattr: metadata assignment structure
1047 static int v9fs_vfs_setattr(struct mnt_idmap *idmap,
1048 struct dentry *dentry, struct iattr *iattr)
1050 int retval, use_dentry = 0;
1051 struct inode *inode = d_inode(dentry);
1052 struct v9fs_session_info *v9ses;
1053 struct p9_fid *fid = NULL;
1054 struct p9_wstat wstat;
1056 p9_debug(P9_DEBUG_VFS, "\n");
1057 retval = setattr_prepare(&nop_mnt_idmap, dentry, iattr);
1061 v9ses = v9fs_dentry2v9ses(dentry);
1062 if (iattr->ia_valid & ATTR_FILE) {
1063 fid = iattr->ia_file->private_data;
1067 fid = v9fs_fid_lookup(dentry);
1071 return PTR_ERR(fid);
1073 v9fs_blank_wstat(&wstat);
1074 if (iattr->ia_valid & ATTR_MODE)
1075 wstat.mode = unixmode2p9mode(v9ses, iattr->ia_mode);
1077 if (iattr->ia_valid & ATTR_MTIME)
1078 wstat.mtime = iattr->ia_mtime.tv_sec;
1080 if (iattr->ia_valid & ATTR_ATIME)
1081 wstat.atime = iattr->ia_atime.tv_sec;
1083 if (iattr->ia_valid & ATTR_SIZE)
1084 wstat.length = iattr->ia_size;
1086 if (v9fs_proto_dotu(v9ses)) {
1087 if (iattr->ia_valid & ATTR_UID)
1088 wstat.n_uid = iattr->ia_uid;
1090 if (iattr->ia_valid & ATTR_GID)
1091 wstat.n_gid = iattr->ia_gid;
1094 /* Write all dirty data */
1095 if (d_is_reg(dentry)) {
1096 retval = filemap_fdatawrite(inode->i_mapping);
1098 p9_debug(P9_DEBUG_ERROR,
1099 "flushing writeback during setattr returned %d\n", retval);
1102 retval = p9_client_wstat(fid, &wstat);
1110 if ((iattr->ia_valid & ATTR_SIZE) &&
1111 iattr->ia_size != i_size_read(inode)) {
1112 truncate_setsize(inode, iattr->ia_size);
1113 netfs_resize_file(netfs_inode(inode), iattr->ia_size, true);
1115 #ifdef CONFIG_9P_FSCACHE
1116 if (v9ses->cache & CACHE_FSCACHE) {
1117 struct v9fs_inode *v9inode = V9FS_I(inode);
1119 fscache_resize_cookie(v9fs_inode_cookie(v9inode), iattr->ia_size);
1124 v9fs_invalidate_inode_attr(inode);
1126 setattr_copy(&nop_mnt_idmap, inode, iattr);
1127 mark_inode_dirty(inode);
1132 * v9fs_stat2inode - populate an inode structure with mistat info
1133 * @stat: Plan 9 metadata (mistat) structure
1134 * @inode: inode to populate
1135 * @sb: superblock of filesystem
1136 * @flags: control flags (e.g. V9FS_STAT2INODE_KEEP_ISIZE)
1141 v9fs_stat2inode(struct p9_wstat *stat, struct inode *inode,
1142 struct super_block *sb, unsigned int flags)
1145 struct v9fs_session_info *v9ses = sb->s_fs_info;
1146 struct v9fs_inode *v9inode = V9FS_I(inode);
1148 set_nlink(inode, 1);
1150 inode_set_atime(inode, stat->atime, 0);
1151 inode_set_mtime(inode, stat->mtime, 0);
1152 inode_set_ctime(inode, stat->mtime, 0);
1154 inode->i_uid = v9ses->dfltuid;
1155 inode->i_gid = v9ses->dfltgid;
1157 if (v9fs_proto_dotu(v9ses)) {
1158 inode->i_uid = stat->n_uid;
1159 inode->i_gid = stat->n_gid;
1161 if ((S_ISREG(inode->i_mode)) || (S_ISDIR(inode->i_mode))) {
1162 if (v9fs_proto_dotu(v9ses)) {
1163 unsigned int i_nlink;
1165 * Hadlink support got added later to the .u extension.
1166 * So there can be a server out there that doesn't
1167 * support this even with .u extension. That would
1168 * just leave us with stat->extension being an empty
1171 /* HARDLINKCOUNT %u */
1172 if (sscanf(stat->extension,
1173 " HARDLINKCOUNT %u", &i_nlink) == 1)
1174 set_nlink(inode, i_nlink);
1177 mode = p9mode2perm(v9ses, stat);
1178 mode |= inode->i_mode & ~S_IALLUGO;
1179 inode->i_mode = mode;
1181 v9inode->netfs.remote_i_size = stat->length;
1182 if (!(flags & V9FS_STAT2INODE_KEEP_ISIZE))
1183 v9fs_i_size_write(inode, stat->length);
1184 /* not real number of blocks, but 512 byte ones ... */
1185 inode->i_blocks = (stat->length + 512 - 1) >> 9;
1186 v9inode->cache_validity &= ~V9FS_INO_INVALID_ATTR;
1190 * v9fs_qid2ino - convert qid into inode number
1193 * BUG: potential for inode number collisions?
1196 ino_t v9fs_qid2ino(struct p9_qid *qid)
1198 u64 path = qid->path + 2;
1201 if (sizeof(ino_t) == sizeof(path))
1202 memcpy(&i, &path, sizeof(ino_t));
1204 i = (ino_t) (path ^ (path >> 32));
1210 * v9fs_vfs_get_link - follow a symlink path
1211 * @dentry: dentry for symlink
1212 * @inode: inode for symlink
1213 * @done: delayed call for when we are done with the return value
1216 static const char *v9fs_vfs_get_link(struct dentry *dentry,
1217 struct inode *inode,
1218 struct delayed_call *done)
1220 struct v9fs_session_info *v9ses;
1222 struct p9_wstat *st;
1226 return ERR_PTR(-ECHILD);
1228 v9ses = v9fs_dentry2v9ses(dentry);
1229 if (!v9fs_proto_dotu(v9ses))
1230 return ERR_PTR(-EBADF);
1232 p9_debug(P9_DEBUG_VFS, "%pd\n", dentry);
1233 fid = v9fs_fid_lookup(dentry);
1236 return ERR_CAST(fid);
1238 st = p9_client_stat(fid);
1241 return ERR_CAST(st);
1243 if (!(st->mode & P9_DMSYMLINK)) {
1246 return ERR_PTR(-EINVAL);
1248 res = st->extension;
1249 st->extension = NULL;
1250 if (strlen(res) >= PATH_MAX)
1251 res[PATH_MAX - 1] = '\0';
1255 set_delayed_call(done, kfree_link, res);
1260 * v9fs_vfs_mkspecial - create a special file
1261 * @dir: inode to create special file in
1262 * @dentry: dentry to create
1263 * @perm: mode to create special file
1264 * @extension: 9p2000.u format extension string representing special file
1268 static int v9fs_vfs_mkspecial(struct inode *dir, struct dentry *dentry,
1269 u32 perm, const char *extension)
1272 struct v9fs_session_info *v9ses;
1274 v9ses = v9fs_inode2v9ses(dir);
1275 if (!v9fs_proto_dotu(v9ses)) {
1276 p9_debug(P9_DEBUG_ERROR, "not extended\n");
1280 fid = v9fs_create(v9ses, dir, dentry, (char *) extension, perm,
1283 return PTR_ERR(fid);
1285 v9fs_invalidate_inode_attr(dir);
1291 * v9fs_vfs_symlink - helper function to create symlinks
1292 * @idmap: idmap of the mount
1293 * @dir: directory inode containing symlink
1294 * @dentry: dentry for symlink
1295 * @symname: symlink data
1297 * See Also: 9P2000.u RFC for more information
1302 v9fs_vfs_symlink(struct mnt_idmap *idmap, struct inode *dir,
1303 struct dentry *dentry, const char *symname)
1305 p9_debug(P9_DEBUG_VFS, " %lu,%pd,%s\n",
1306 dir->i_ino, dentry, symname);
1308 return v9fs_vfs_mkspecial(dir, dentry, P9_DMSYMLINK, symname);
1311 #define U32_MAX_DIGITS 10
1314 * v9fs_vfs_link - create a hardlink
1315 * @old_dentry: dentry for file to link to
1316 * @dir: inode destination for new link
1317 * @dentry: dentry for link
1322 v9fs_vfs_link(struct dentry *old_dentry, struct inode *dir,
1323 struct dentry *dentry)
1326 char name[1 + U32_MAX_DIGITS + 2]; /* sign + number + \n + \0 */
1327 struct p9_fid *oldfid;
1329 p9_debug(P9_DEBUG_VFS, " %lu,%pd,%pd\n",
1330 dir->i_ino, dentry, old_dentry);
1332 oldfid = v9fs_fid_clone(old_dentry);
1334 return PTR_ERR(oldfid);
1336 sprintf(name, "%d\n", oldfid->fid);
1337 retval = v9fs_vfs_mkspecial(dir, dentry, P9_DMLINK, name);
1339 v9fs_refresh_inode(oldfid, d_inode(old_dentry));
1340 v9fs_invalidate_inode_attr(dir);
1347 * v9fs_vfs_mknod - create a special file
1348 * @idmap: idmap of the mount
1349 * @dir: inode destination for new link
1350 * @dentry: dentry for file
1351 * @mode: mode for creation
1352 * @rdev: device associated with special file
1357 v9fs_vfs_mknod(struct mnt_idmap *idmap, struct inode *dir,
1358 struct dentry *dentry, umode_t mode, dev_t rdev)
1360 struct v9fs_session_info *v9ses = v9fs_inode2v9ses(dir);
1362 char name[2 + U32_MAX_DIGITS + 1 + U32_MAX_DIGITS + 1];
1365 p9_debug(P9_DEBUG_VFS, " %lu,%pd mode: %x MAJOR: %u MINOR: %u\n",
1366 dir->i_ino, dentry, mode,
1367 MAJOR(rdev), MINOR(rdev));
1369 /* build extension */
1371 sprintf(name, "b %u %u", MAJOR(rdev), MINOR(rdev));
1372 else if (S_ISCHR(mode))
1373 sprintf(name, "c %u %u", MAJOR(rdev), MINOR(rdev));
1377 perm = unixmode2p9mode(v9ses, mode);
1378 retval = v9fs_vfs_mkspecial(dir, dentry, perm, name);
1383 int v9fs_refresh_inode(struct p9_fid *fid, struct inode *inode)
1387 struct p9_wstat *st;
1388 struct v9fs_session_info *v9ses;
1391 v9ses = v9fs_inode2v9ses(inode);
1392 st = p9_client_stat(fid);
1396 * Don't update inode if the file type is different
1398 umode = p9mode2unixmode(v9ses, st, &rdev);
1399 if (inode_wrong_type(inode, umode))
1403 * We don't want to refresh inode->i_size,
1404 * because we may have cached data
1406 flags = (v9ses->cache & CACHE_LOOSE) ?
1407 V9FS_STAT2INODE_KEEP_ISIZE : 0;
1408 v9fs_stat2inode(st, inode, inode->i_sb, flags);
1415 static const struct inode_operations v9fs_dir_inode_operations_dotu = {
1416 .create = v9fs_vfs_create,
1417 .lookup = v9fs_vfs_lookup,
1418 .atomic_open = v9fs_vfs_atomic_open,
1419 .symlink = v9fs_vfs_symlink,
1420 .link = v9fs_vfs_link,
1421 .unlink = v9fs_vfs_unlink,
1422 .mkdir = v9fs_vfs_mkdir,
1423 .rmdir = v9fs_vfs_rmdir,
1424 .mknod = v9fs_vfs_mknod,
1425 .rename = v9fs_vfs_rename,
1426 .getattr = v9fs_vfs_getattr,
1427 .setattr = v9fs_vfs_setattr,
1430 static const struct inode_operations v9fs_dir_inode_operations = {
1431 .create = v9fs_vfs_create,
1432 .lookup = v9fs_vfs_lookup,
1433 .atomic_open = v9fs_vfs_atomic_open,
1434 .unlink = v9fs_vfs_unlink,
1435 .mkdir = v9fs_vfs_mkdir,
1436 .rmdir = v9fs_vfs_rmdir,
1437 .mknod = v9fs_vfs_mknod,
1438 .rename = v9fs_vfs_rename,
1439 .getattr = v9fs_vfs_getattr,
1440 .setattr = v9fs_vfs_setattr,
1443 static const struct inode_operations v9fs_file_inode_operations = {
1444 .getattr = v9fs_vfs_getattr,
1445 .setattr = v9fs_vfs_setattr,
1448 static const struct inode_operations v9fs_symlink_inode_operations = {
1449 .get_link = v9fs_vfs_get_link,
1450 .getattr = v9fs_vfs_getattr,
1451 .setattr = v9fs_vfs_setattr,