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, struct p9_qid *qid, umode_t mode, dev_t rdev)
259 struct v9fs_inode *v9inode = V9FS_I(inode);
261 memcpy(&v9inode->qid, qid, sizeof(struct p9_qid));
263 inode_init_owner(&nop_mnt_idmap, inode, NULL, mode);
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;
270 switch (mode & S_IFMT) {
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;
280 p9_debug(P9_DEBUG_ERROR,
281 "special files without extended mode\n");
285 init_special_inode(inode, inode->i_mode, inode->i_rdev);
288 if (v9fs_proto_dotl(v9ses)) {
289 inode->i_op = &v9fs_file_inode_operations_dotl;
290 inode->i_fop = &v9fs_file_operations_dotl;
292 inode->i_op = &v9fs_file_inode_operations;
293 inode->i_fop = &v9fs_file_operations;
298 if (!v9fs_proto_dotu(v9ses) && !v9fs_proto_dotl(v9ses)) {
299 p9_debug(P9_DEBUG_ERROR,
300 "extended modes used with legacy protocol\n");
305 if (v9fs_proto_dotl(v9ses))
306 inode->i_op = &v9fs_symlink_inode_operations_dotl;
308 inode->i_op = &v9fs_symlink_inode_operations;
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;
318 inode->i_op = &v9fs_dir_inode_operations;
320 if (v9fs_proto_dotl(v9ses))
321 inode->i_fop = &v9fs_dir_operations_dotl;
323 inode->i_fop = &v9fs_dir_operations;
327 p9_debug(P9_DEBUG_ERROR, "BAD mode 0x%hx S_IFMT 0x%x\n",
328 mode, mode & S_IFMT);
338 * v9fs_evict_inode - Remove an inode from the inode cache
339 * @inode: inode to release
342 void v9fs_evict_inode(struct inode *inode)
344 struct v9fs_inode __maybe_unused *v9inode = V9FS_I(inode);
345 __le32 __maybe_unused version;
347 truncate_inode_pages_final(&inode->i_data);
349 version = cpu_to_le32(v9inode->qid.version);
350 netfs_clear_inode_writeback(inode, &version);
353 filemap_fdatawrite(&inode->i_data);
355 #ifdef CONFIG_9P_FSCACHE
356 fscache_relinquish_cookie(v9fs_inode_cookie(v9inode), false);
360 struct inode *v9fs_fid_iget(struct super_block *sb, struct p9_fid *fid)
367 struct v9fs_session_info *v9ses = sb->s_fs_info;
369 inode = iget_locked(sb, QID2INO(&fid->qid));
370 if (unlikely(!inode))
371 return ERR_PTR(-ENOMEM);
372 if (!(inode->i_state & I_NEW))
376 * initialize the inode with the stat info
377 * FIXME!! we may need support for stale inodes
380 st = p9_client_stat(fid);
382 retval = PTR_ERR(st);
386 umode = p9mode2unixmode(v9ses, st, &rdev);
387 retval = v9fs_init_inode(v9ses, inode, &fid->qid, umode, rdev);
388 v9fs_stat2inode(st, inode, sb, 0);
394 v9fs_set_netfs_context(inode);
395 v9fs_cache_inode_get_cookie(inode);
396 unlock_new_inode(inode);
400 return ERR_PTR(retval);
405 * v9fs_at_to_dotl_flags- convert Linux specific AT flags to
407 * @flags: flags to convert
409 static int v9fs_at_to_dotl_flags(int flags)
413 if (flags & AT_REMOVEDIR)
414 rflags |= P9_DOTL_AT_REMOVEDIR;
420 * v9fs_dec_count - helper functon to drop i_nlink.
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.
429 * @inode: inode whose nlink is being dropped
431 static void v9fs_dec_count(struct inode *inode)
433 if (!S_ISDIR(inode->i_mode) || inode->i_nlink > 2)
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
445 static int v9fs_remove(struct inode *dir, struct dentry *dentry, int flags)
448 int retval = -EOPNOTSUPP;
449 struct p9_fid *v9fid, *dfid;
450 struct v9fs_session_info *v9ses;
452 p9_debug(P9_DEBUG_VFS, "inode: %p dentry: %p rmdir: %x\n",
455 v9ses = v9fs_inode2v9ses(dir);
456 inode = d_inode(dentry);
457 dfid = v9fs_parent_fid(dentry);
459 retval = PTR_ERR(dfid);
460 p9_debug(P9_DEBUG_VFS, "fid lookup failed %d\n", retval);
463 if (v9fs_proto_dotl(v9ses))
464 retval = p9_client_unlinkat(dfid, dentry->d_name.name,
465 v9fs_at_to_dotl_flags(flags));
467 if (retval == -EOPNOTSUPP) {
468 /* Try the one based on path */
469 v9fid = v9fs_fid_clone(dentry);
471 return PTR_ERR(v9fid);
472 retval = p9_client_remove(v9fid);
476 * directories on unlink should have zero
479 if (flags & AT_REMOVEDIR) {
483 v9fs_dec_count(inode);
485 v9fs_invalidate_inode_attr(inode);
486 v9fs_invalidate_inode_attr(dir);
488 /* invalidate all fids associated with dentry */
489 /* NOTE: This will not include open fids */
490 dentry->d_op->d_release(dentry);
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
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)
510 const unsigned char *name;
511 struct p9_fid *dfid, *ofid = NULL, *fid = NULL;
514 p9_debug(P9_DEBUG_VFS, "name %pd\n", dentry);
516 name = dentry->d_name.name;
517 dfid = v9fs_parent_fid(dentry);
520 p9_debug(P9_DEBUG_VFS, "fid lookup failed %d\n", err);
524 /* clone a fid to use for creation */
525 ofid = clone_fid(dfid);
528 p9_debug(P9_DEBUG_VFS, "p9_client_walk failed %d\n", err);
532 err = p9_client_fcreate(ofid, name, perm, mode, extension);
534 p9_debug(P9_DEBUG_VFS, "p9_client_fcreate failed %d\n", err);
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);
543 p9_debug(P9_DEBUG_VFS,
544 "p9_client_walk failed %d\n", err);
548 * instantiate inode and assign the unopened fid to the dentry
550 inode = v9fs_get_inode_from_fid(v9ses, fid, dir->i_sb);
552 err = PTR_ERR(inode);
553 p9_debug(P9_DEBUG_VFS,
554 "inode creation failed %d\n", err);
557 v9fs_fid_add(dentry, &fid);
558 d_instantiate(dentry, inode);
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
577 * open(.., O_CREAT) is handled in v9fs_vfs_atomic_open(). This is only called
583 v9fs_vfs_create(struct mnt_idmap *idmap, struct inode *dir,
584 struct dentry *dentry, umode_t mode, bool excl)
586 struct v9fs_session_info *v9ses = v9fs_inode2v9ses(dir);
587 u32 perm = unixmode2p9mode(v9ses, mode);
591 fid = v9fs_create(v9ses, dir, dentry, NULL, perm, P9_ORDWR);
595 v9fs_invalidate_inode_attr(dir);
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
610 static int v9fs_vfs_mkdir(struct mnt_idmap *idmap, struct inode *dir,
611 struct dentry *dentry, umode_t mode)
616 struct v9fs_session_info *v9ses;
618 p9_debug(P9_DEBUG_VFS, "name %pd\n", dentry);
620 v9ses = v9fs_inode2v9ses(dir);
621 perm = unixmode2p9mode(v9ses, mode | S_IFDIR);
622 fid = v9fs_create(v9ses, dir, dentry, NULL, perm, P9_OREAD);
628 v9fs_invalidate_inode_attr(dir);
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)
645 struct dentry *v9fs_vfs_lookup(struct inode *dir, struct dentry *dentry,
649 struct v9fs_session_info *v9ses;
650 struct p9_fid *dfid, *fid;
652 const unsigned char *name;
654 p9_debug(P9_DEBUG_VFS, "dir: %p dentry: (%pd) %p flags: %x\n",
655 dir, dentry, dentry, flags);
657 if (dentry->d_name.len > NAME_MAX)
658 return ERR_PTR(-ENAMETOOLONG);
660 v9ses = v9fs_inode2v9ses(dir);
661 /* We can walk d_parent because we hold the dir->i_mutex */
662 dfid = v9fs_parent_fid(dentry);
664 return ERR_CAST(dfid);
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.
671 name = dentry->d_name.name;
672 fid = p9_client_walk(dfid, 1, &name, 1);
674 if (fid == ERR_PTR(-ENOENT))
676 else if (IS_ERR(fid))
677 inode = ERR_CAST(fid);
679 inode = v9fs_get_inode_from_fid(v9ses, fid, dir->i_sb);
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
687 res = d_splice_alias(inode, dentry);
690 v9fs_fid_add(dentry, &fid);
691 else if (!IS_ERR(res))
692 v9fs_fid_add(res, &fid);
700 v9fs_vfs_atomic_open(struct inode *dir, struct dentry *dentry,
701 struct file *file, unsigned int flags, umode_t mode)
705 struct v9fs_inode __maybe_unused *v9inode;
706 struct v9fs_session_info *v9ses;
708 struct dentry *res = NULL;
712 if (d_in_lookup(dentry)) {
713 res = v9fs_vfs_lookup(dir, dentry, 0);
722 if (!(flags & O_CREAT) || d_really_is_positive(dentry))
723 return finish_no_open(file, res);
725 v9ses = v9fs_inode2v9ses(dir);
726 perm = unixmode2p9mode(v9ses, mode);
727 p9_omode = v9fs_uflags2omode(flags, v9fs_proto_dotu(v9ses));
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");
734 fid = v9fs_create(v9ses, dir, dentry, NULL, perm, p9_omode);
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);
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);
754 v9fs_fid_add_modes(fid, v9ses->flags, v9ses->cache, file->f_flags);
755 v9fs_open_fid_add(inode, &fid);
757 file->f_mode |= FMODE_CREATED;
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
774 int v9fs_vfs_unlink(struct inode *i, struct dentry *d)
776 return v9fs_remove(i, d, 0);
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
786 int v9fs_vfs_rmdir(struct inode *i, struct dentry *d)
788 return v9fs_remove(i, d, AT_REMOVEDIR);
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
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)
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;
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);
825 return PTR_ERR(oldfid);
827 dfid = v9fs_parent_fid(old_dentry);
828 olddirfid = clone_fid(dfid);
832 if (IS_ERR(olddirfid)) {
833 retval = PTR_ERR(olddirfid);
837 dfid = v9fs_parent_fid(new_dentry);
838 newdirfid = clone_fid(dfid);
842 if (IS_ERR(newdirfid)) {
843 retval = PTR_ERR(newdirfid);
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)
857 if (old_dentry->d_parent != new_dentry->d_parent) {
859 * 9P .u can only handle file rename in the same directory
862 p9_debug(P9_DEBUG_ERROR, "old dir and new dir are different\n");
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);
874 if (S_ISDIR(new_inode->i_mode))
875 clear_nlink(new_inode);
877 v9fs_dec_count(new_inode);
879 if (S_ISDIR(old_inode->i_mode)) {
882 v9fs_dec_count(old_dir);
884 v9fs_invalidate_inode_attr(old_inode);
885 v9fs_invalidate_inode_attr(old_dir);
886 v9fs_invalidate_inode_attr(new_dir);
888 /* successful rename */
889 d_move(old_dentry, new_dentry);
891 up_write(&v9ses->rename_sem);
894 p9_fid_put(newdirfid);
895 p9_fid_put(olddirfid);
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
911 v9fs_vfs_getattr(struct mnt_idmap *idmap, const struct path *path,
912 struct kstat *stat, u32 request_mask, unsigned int flags)
914 struct dentry *dentry = path->dentry;
915 struct inode *inode = d_inode(dentry);
916 struct v9fs_session_info *v9ses;
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);
925 } else if (v9ses->cache & CACHE_WRITEBACK) {
926 if (S_ISREG(inode->i_mode)) {
927 int retval = filemap_fdatawrite(inode->i_mapping);
930 p9_debug(P9_DEBUG_ERROR,
931 "flushing writeback during getattr returned %d\n", retval);
934 fid = v9fs_fid_lookup(dentry);
938 st = p9_client_stat(fid);
943 v9fs_stat2inode(st, d_inode(dentry), dentry->d_sb, 0);
944 generic_fillattr(&nop_mnt_idmap, request_mask, d_inode(dentry), stat);
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
959 static int v9fs_vfs_setattr(struct mnt_idmap *idmap,
960 struct dentry *dentry, struct iattr *iattr)
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;
968 p9_debug(P9_DEBUG_VFS, "\n");
969 retval = setattr_prepare(&nop_mnt_idmap, dentry, iattr);
973 v9ses = v9fs_dentry2v9ses(dentry);
974 if (iattr->ia_valid & ATTR_FILE) {
975 fid = iattr->ia_file->private_data;
979 fid = v9fs_fid_lookup(dentry);
985 v9fs_blank_wstat(&wstat);
986 if (iattr->ia_valid & ATTR_MODE)
987 wstat.mode = unixmode2p9mode(v9ses, iattr->ia_mode);
989 if (iattr->ia_valid & ATTR_MTIME)
990 wstat.mtime = iattr->ia_mtime.tv_sec;
992 if (iattr->ia_valid & ATTR_ATIME)
993 wstat.atime = iattr->ia_atime.tv_sec;
995 if (iattr->ia_valid & ATTR_SIZE)
996 wstat.length = iattr->ia_size;
998 if (v9fs_proto_dotu(v9ses)) {
999 if (iattr->ia_valid & ATTR_UID)
1000 wstat.n_uid = iattr->ia_uid;
1002 if (iattr->ia_valid & ATTR_GID)
1003 wstat.n_gid = iattr->ia_gid;
1006 /* Write all dirty data */
1007 if (d_is_reg(dentry)) {
1008 retval = filemap_fdatawrite(inode->i_mapping);
1010 p9_debug(P9_DEBUG_ERROR,
1011 "flushing writeback during setattr returned %d\n", retval);
1014 retval = p9_client_wstat(fid, &wstat);
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);
1027 #ifdef CONFIG_9P_FSCACHE
1028 if (v9ses->cache & CACHE_FSCACHE) {
1029 struct v9fs_inode *v9inode = V9FS_I(inode);
1031 fscache_resize_cookie(v9fs_inode_cookie(v9inode), iattr->ia_size);
1036 v9fs_invalidate_inode_attr(inode);
1038 setattr_copy(&nop_mnt_idmap, inode, iattr);
1039 mark_inode_dirty(inode);
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)
1053 v9fs_stat2inode(struct p9_wstat *stat, struct inode *inode,
1054 struct super_block *sb, unsigned int flags)
1057 struct v9fs_session_info *v9ses = sb->s_fs_info;
1058 struct v9fs_inode *v9inode = V9FS_I(inode);
1060 set_nlink(inode, 1);
1062 inode_set_atime(inode, stat->atime, 0);
1063 inode_set_mtime(inode, stat->mtime, 0);
1064 inode_set_ctime(inode, stat->mtime, 0);
1066 inode->i_uid = v9ses->dfltuid;
1067 inode->i_gid = v9ses->dfltgid;
1069 if (v9fs_proto_dotu(v9ses)) {
1070 inode->i_uid = stat->n_uid;
1071 inode->i_gid = stat->n_gid;
1073 if ((S_ISREG(inode->i_mode)) || (S_ISDIR(inode->i_mode))) {
1074 if (v9fs_proto_dotu(v9ses)) {
1075 unsigned int i_nlink;
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
1083 /* HARDLINKCOUNT %u */
1084 if (sscanf(stat->extension,
1085 " HARDLINKCOUNT %u", &i_nlink) == 1)
1086 set_nlink(inode, i_nlink);
1089 mode = p9mode2perm(v9ses, stat);
1090 mode |= inode->i_mode & ~S_IALLUGO;
1091 inode->i_mode = mode;
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;
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
1108 static const char *v9fs_vfs_get_link(struct dentry *dentry,
1109 struct inode *inode,
1110 struct delayed_call *done)
1112 struct v9fs_session_info *v9ses;
1114 struct p9_wstat *st;
1118 return ERR_PTR(-ECHILD);
1120 v9ses = v9fs_dentry2v9ses(dentry);
1121 if (!v9fs_proto_dotu(v9ses))
1122 return ERR_PTR(-EBADF);
1124 p9_debug(P9_DEBUG_VFS, "%pd\n", dentry);
1125 fid = v9fs_fid_lookup(dentry);
1128 return ERR_CAST(fid);
1130 st = p9_client_stat(fid);
1133 return ERR_CAST(st);
1135 if (!(st->mode & P9_DMSYMLINK)) {
1138 return ERR_PTR(-EINVAL);
1140 res = st->extension;
1141 st->extension = NULL;
1142 if (strlen(res) >= PATH_MAX)
1143 res[PATH_MAX - 1] = '\0';
1147 set_delayed_call(done, kfree_link, res);
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
1160 static int v9fs_vfs_mkspecial(struct inode *dir, struct dentry *dentry,
1161 u32 perm, const char *extension)
1164 struct v9fs_session_info *v9ses;
1166 v9ses = v9fs_inode2v9ses(dir);
1167 if (!v9fs_proto_dotu(v9ses)) {
1168 p9_debug(P9_DEBUG_ERROR, "not extended\n");
1172 fid = v9fs_create(v9ses, dir, dentry, (char *) extension, perm,
1175 return PTR_ERR(fid);
1177 v9fs_invalidate_inode_attr(dir);
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
1189 * See Also: 9P2000.u RFC for more information
1194 v9fs_vfs_symlink(struct mnt_idmap *idmap, struct inode *dir,
1195 struct dentry *dentry, const char *symname)
1197 p9_debug(P9_DEBUG_VFS, " %lu,%pd,%s\n",
1198 dir->i_ino, dentry, symname);
1200 return v9fs_vfs_mkspecial(dir, dentry, P9_DMSYMLINK, symname);
1203 #define U32_MAX_DIGITS 10
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
1214 v9fs_vfs_link(struct dentry *old_dentry, struct inode *dir,
1215 struct dentry *dentry)
1218 char name[1 + U32_MAX_DIGITS + 2]; /* sign + number + \n + \0 */
1219 struct p9_fid *oldfid;
1221 p9_debug(P9_DEBUG_VFS, " %lu,%pd,%pd\n",
1222 dir->i_ino, dentry, old_dentry);
1224 oldfid = v9fs_fid_clone(old_dentry);
1226 return PTR_ERR(oldfid);
1228 sprintf(name, "%d\n", oldfid->fid);
1229 retval = v9fs_vfs_mkspecial(dir, dentry, P9_DMLINK, name);
1231 v9fs_refresh_inode(oldfid, d_inode(old_dentry));
1232 v9fs_invalidate_inode_attr(dir);
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
1249 v9fs_vfs_mknod(struct mnt_idmap *idmap, struct inode *dir,
1250 struct dentry *dentry, umode_t mode, dev_t rdev)
1252 struct v9fs_session_info *v9ses = v9fs_inode2v9ses(dir);
1254 char name[2 + U32_MAX_DIGITS + 1 + U32_MAX_DIGITS + 1];
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));
1261 /* build extension */
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));
1269 perm = unixmode2p9mode(v9ses, mode);
1270 retval = v9fs_vfs_mkspecial(dir, dentry, perm, name);
1275 int v9fs_refresh_inode(struct p9_fid *fid, struct inode *inode)
1279 struct p9_wstat *st;
1280 struct v9fs_session_info *v9ses;
1283 v9ses = v9fs_inode2v9ses(inode);
1284 st = p9_client_stat(fid);
1288 * Don't update inode if the file type is different
1290 umode = p9mode2unixmode(v9ses, st, &rdev);
1291 if (inode_wrong_type(inode, umode))
1295 * We don't want to refresh inode->i_size,
1296 * because we may have cached data
1298 flags = (v9ses->cache & CACHE_LOOSE) ?
1299 V9FS_STAT2INODE_KEEP_ISIZE : 0;
1300 v9fs_stat2inode(st, inode, inode->i_sb, flags);
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,
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,
1335 static const struct inode_operations v9fs_file_inode_operations = {
1336 .getattr = v9fs_vfs_getattr,
1337 .setattr = v9fs_vfs_setattr,
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,