1 // SPDX-License-Identifier: GPL-2.0-only
3 * linux/fs/9p/vfs_inode.c
5 * This file contains vfs inode ops for the 9P2000 protocol.
7 * Copyright (C) 2004 by Eric Van Hensbergen <ericvh@gmail.com>
8 * Copyright (C) 2002 by Ron Minnich <rminnich@lanl.gov>
11 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
13 #include <linux/module.h>
14 #include <linux/errno.h>
16 #include <linux/file.h>
17 #include <linux/pagemap.h>
18 #include <linux/stat.h>
19 #include <linux/string.h>
20 #include <linux/inet.h>
21 #include <linux/namei.h>
22 #include <linux/idr.h>
23 #include <linux/sched.h>
24 #include <linux/slab.h>
25 #include <linux/xattr.h>
26 #include <linux/posix_acl.h>
27 #include <net/9p/9p.h>
28 #include <net/9p/client.h>
37 static const struct inode_operations v9fs_dir_inode_operations;
38 static const struct inode_operations v9fs_dir_inode_operations_dotu;
39 static const struct inode_operations v9fs_file_inode_operations;
40 static const struct inode_operations v9fs_symlink_inode_operations;
43 * unixmode2p9mode - convert unix mode bits to plan 9
44 * @v9ses: v9fs session information
45 * @mode: mode to convert
49 static u32 unixmode2p9mode(struct v9fs_session_info *v9ses, umode_t mode)
55 if (v9fs_proto_dotu(v9ses)) {
56 if (v9ses->nodev == 0) {
60 res |= P9_DMNAMEDPIPE;
67 if ((mode & S_ISUID) == S_ISUID)
69 if ((mode & S_ISGID) == S_ISGID)
71 if ((mode & S_ISVTX) == S_ISVTX)
78 * p9mode2perm- convert plan9 mode bits to unix permission bits
79 * @v9ses: v9fs session information
80 * @stat: p9_wstat from which mode need to be derived
83 static int p9mode2perm(struct v9fs_session_info *v9ses,
84 struct p9_wstat *stat)
87 int mode = stat->mode;
89 res = mode & S_IALLUGO;
90 if (v9fs_proto_dotu(v9ses)) {
91 if ((mode & P9_DMSETUID) == P9_DMSETUID)
94 if ((mode & P9_DMSETGID) == P9_DMSETGID)
97 if ((mode & P9_DMSETVTX) == P9_DMSETVTX)
104 * p9mode2unixmode- convert plan9 mode bits to unix mode bits
105 * @v9ses: v9fs session information
106 * @stat: p9_wstat from which mode need to be derived
107 * @rdev: major number, minor number in case of device files.
110 static umode_t p9mode2unixmode(struct v9fs_session_info *v9ses,
111 struct p9_wstat *stat, dev_t *rdev)
114 u32 mode = stat->mode;
117 res = p9mode2perm(v9ses, stat);
119 if ((mode & P9_DMDIR) == P9_DMDIR)
121 else if ((mode & P9_DMSYMLINK) && (v9fs_proto_dotu(v9ses)))
123 else if ((mode & P9_DMSOCKET) && (v9fs_proto_dotu(v9ses))
124 && (v9ses->nodev == 0))
126 else if ((mode & P9_DMNAMEDPIPE) && (v9fs_proto_dotu(v9ses))
127 && (v9ses->nodev == 0))
129 else if ((mode & P9_DMDEVICE) && (v9fs_proto_dotu(v9ses))
130 && (v9ses->nodev == 0)) {
131 char type = 0, ext[32];
132 int major = -1, minor = -1;
134 strlcpy(ext, stat->extension, sizeof(ext));
135 sscanf(ext, "%c %i %i", &type, &major, &minor);
144 p9_debug(P9_DEBUG_ERROR, "Unknown special type %c %s\n",
145 type, stat->extension);
147 *rdev = MKDEV(major, minor);
155 * v9fs_uflags2omode- convert posix open flags to plan 9 mode bits
156 * @uflags: flags to convert
157 * @extended: if .u extensions are active
160 int v9fs_uflags2omode(int uflags, int extended)
184 if (uflags & O_APPEND)
192 * v9fs_blank_wstat - helper function to setup a 9P stat structure
193 * @wstat: structure to initialize
198 v9fs_blank_wstat(struct p9_wstat *wstat)
202 wstat->qid.type = ~0;
203 wstat->qid.version = ~0;
204 *((long long *)&wstat->qid.path) = ~0;
213 wstat->n_uid = INVALID_UID;
214 wstat->n_gid = INVALID_GID;
215 wstat->n_muid = INVALID_UID;
216 wstat->extension = NULL;
220 * v9fs_alloc_inode - helper function to allocate an inode
223 struct inode *v9fs_alloc_inode(struct super_block *sb)
225 struct v9fs_inode *v9inode;
226 v9inode = (struct v9fs_inode *)kmem_cache_alloc(v9fs_inode_cache,
230 #ifdef CONFIG_9P_FSCACHE
231 v9inode->fscache = NULL;
232 mutex_init(&v9inode->fscache_lock);
234 v9inode->writeback_fid = NULL;
235 v9inode->cache_validity = 0;
236 mutex_init(&v9inode->v_mutex);
237 return &v9inode->vfs_inode;
241 * v9fs_free_inode - destroy an inode
245 void v9fs_free_inode(struct inode *inode)
247 kmem_cache_free(v9fs_inode_cache, V9FS_I(inode));
250 int v9fs_init_inode(struct v9fs_session_info *v9ses,
251 struct inode *inode, umode_t mode, dev_t rdev)
255 inode_init_owner(inode, NULL, mode);
257 inode->i_rdev = rdev;
258 inode->i_atime = inode->i_mtime = inode->i_ctime = current_time(inode);
259 inode->i_mapping->a_ops = &v9fs_addr_operations;
261 switch (mode & S_IFMT) {
266 if (v9fs_proto_dotl(v9ses)) {
267 inode->i_op = &v9fs_file_inode_operations_dotl;
268 } else if (v9fs_proto_dotu(v9ses)) {
269 inode->i_op = &v9fs_file_inode_operations;
271 p9_debug(P9_DEBUG_ERROR,
272 "special files without extended mode\n");
276 init_special_inode(inode, inode->i_mode, inode->i_rdev);
279 if (v9fs_proto_dotl(v9ses)) {
280 inode->i_op = &v9fs_file_inode_operations_dotl;
281 if (v9ses->cache == CACHE_LOOSE ||
282 v9ses->cache == CACHE_FSCACHE)
284 &v9fs_cached_file_operations_dotl;
285 else if (v9ses->cache == CACHE_MMAP)
286 inode->i_fop = &v9fs_mmap_file_operations_dotl;
288 inode->i_fop = &v9fs_file_operations_dotl;
290 inode->i_op = &v9fs_file_inode_operations;
291 if (v9ses->cache == CACHE_LOOSE ||
292 v9ses->cache == CACHE_FSCACHE)
294 &v9fs_cached_file_operations;
295 else if (v9ses->cache == CACHE_MMAP)
296 inode->i_fop = &v9fs_mmap_file_operations;
298 inode->i_fop = &v9fs_file_operations;
303 if (!v9fs_proto_dotu(v9ses) && !v9fs_proto_dotl(v9ses)) {
304 p9_debug(P9_DEBUG_ERROR,
305 "extended modes used with legacy protocol\n");
310 if (v9fs_proto_dotl(v9ses))
311 inode->i_op = &v9fs_symlink_inode_operations_dotl;
313 inode->i_op = &v9fs_symlink_inode_operations;
318 if (v9fs_proto_dotl(v9ses))
319 inode->i_op = &v9fs_dir_inode_operations_dotl;
320 else if (v9fs_proto_dotu(v9ses))
321 inode->i_op = &v9fs_dir_inode_operations_dotu;
323 inode->i_op = &v9fs_dir_inode_operations;
325 if (v9fs_proto_dotl(v9ses))
326 inode->i_fop = &v9fs_dir_operations_dotl;
328 inode->i_fop = &v9fs_dir_operations;
332 p9_debug(P9_DEBUG_ERROR, "BAD mode 0x%hx S_IFMT 0x%x\n",
333 mode, mode & S_IFMT);
343 * v9fs_get_inode - helper function to setup an inode
345 * @mode: mode to setup inode with
349 struct inode *v9fs_get_inode(struct super_block *sb, umode_t mode, dev_t rdev)
353 struct v9fs_session_info *v9ses = sb->s_fs_info;
355 p9_debug(P9_DEBUG_VFS, "super block: %p mode: %ho\n", sb, mode);
357 inode = new_inode(sb);
359 pr_warn("%s (%d): Problem allocating inode\n",
360 __func__, task_pid_nr(current));
361 return ERR_PTR(-ENOMEM);
363 err = v9fs_init_inode(v9ses, inode, mode, rdev);
372 static struct v9fs_fid*
373 v9fs_clone_walk(struct v9fs_session_info *v9ses, u32 fid, struct dentry *dentry)
377 struct v9fs_fid *ret;
378 struct v9fs_fcall *fcall;
380 nfid = v9fs_get_idpool(&v9ses->fidpool);
382 eprintk(KERN_WARNING, "no free fids available\n");
383 return ERR_PTR(-ENOSPC);
386 err = v9fs_t_walk(v9ses, fid, nfid, (char *) dentry->d_name.name,
390 if (fcall && fcall->id == RWALK)
393 PRINT_FCALL_ERROR("walk error", fcall);
394 v9fs_put_idpool(nfid, &v9ses->fidpool);
400 ret = v9fs_fid_create(v9ses, nfid);
406 err = v9fs_fid_insert(ret, dentry);
408 v9fs_fid_destroy(ret);
415 v9fs_t_clunk(v9ses, nfid);
425 * v9fs_clear_inode - release an inode
426 * @inode: inode to release
429 void v9fs_evict_inode(struct inode *inode)
431 struct v9fs_inode *v9inode = V9FS_I(inode);
433 truncate_inode_pages_final(&inode->i_data);
435 filemap_fdatawrite(&inode->i_data);
437 v9fs_cache_inode_put_cookie(inode);
438 /* clunk the fid stashed in writeback_fid */
439 if (v9inode->writeback_fid) {
440 p9_client_clunk(v9inode->writeback_fid);
441 v9inode->writeback_fid = NULL;
445 static int v9fs_test_inode(struct inode *inode, void *data)
449 struct v9fs_inode *v9inode = V9FS_I(inode);
450 struct p9_wstat *st = (struct p9_wstat *)data;
451 struct v9fs_session_info *v9ses = v9fs_inode2v9ses(inode);
453 umode = p9mode2unixmode(v9ses, st, &rdev);
454 /* don't match inode of different type */
455 if ((inode->i_mode & S_IFMT) != (umode & S_IFMT))
458 /* compare qid details */
459 if (memcmp(&v9inode->qid.version,
460 &st->qid.version, sizeof(v9inode->qid.version)))
463 if (v9inode->qid.type != st->qid.type)
466 if (v9inode->qid.path != st->qid.path)
471 static int v9fs_test_new_inode(struct inode *inode, void *data)
476 static int v9fs_set_inode(struct inode *inode, void *data)
478 struct v9fs_inode *v9inode = V9FS_I(inode);
479 struct p9_wstat *st = (struct p9_wstat *)data;
481 memcpy(&v9inode->qid, &st->qid, sizeof(st->qid));
485 static struct inode *v9fs_qid_iget(struct super_block *sb,
495 struct v9fs_session_info *v9ses = sb->s_fs_info;
496 int (*test)(struct inode *, void *);
499 test = v9fs_test_new_inode;
501 test = v9fs_test_inode;
503 i_ino = v9fs_qid2ino(qid);
504 inode = iget5_locked(sb, i_ino, test, v9fs_set_inode, st);
506 return ERR_PTR(-ENOMEM);
507 if (!(inode->i_state & I_NEW))
510 * initialize the inode with the stat info
511 * FIXME!! we may need support for stale inodes
514 inode->i_ino = i_ino;
515 umode = p9mode2unixmode(v9ses, st, &rdev);
516 retval = v9fs_init_inode(v9ses, inode, umode, rdev);
520 v9fs_stat2inode(st, inode, sb, 0);
521 v9fs_cache_inode_get_cookie(inode);
522 unlock_new_inode(inode);
526 return ERR_PTR(retval);
531 v9fs_inode_from_fid(struct v9fs_session_info *v9ses, struct p9_fid *fid,
532 struct super_block *sb, int new)
535 struct inode *inode = NULL;
537 st = p9_client_stat(fid);
541 inode = v9fs_qid_iget(sb, &st->qid, st, new);
548 * v9fs_at_to_dotl_flags- convert Linux specific AT flags to
550 * @flags: flags to convert
552 static int v9fs_at_to_dotl_flags(int flags)
555 if (flags & AT_REMOVEDIR)
556 rflags |= P9_DOTL_AT_REMOVEDIR;
561 * v9fs_dec_count - helper functon to drop i_nlink.
563 * If a directory had nlink <= 2 (including . and ..), then we should not drop
564 * the link count, which indicates the underlying exported fs doesn't maintain
565 * nlink accurately. e.g.
566 * - overlayfs sets nlink to 1 for merged dir
567 * - ext4 (with dir_nlink feature enabled) sets nlink to 1 if a dir has more
568 * than EXT4_LINK_MAX (65000) links.
570 * @inode: inode whose nlink is being dropped
572 static void v9fs_dec_count(struct inode *inode)
574 if (!S_ISDIR(inode->i_mode) || inode->i_nlink > 2)
579 * v9fs_remove - helper function to remove files and directories
580 * @dir: directory inode that is being deleted
581 * @dentry: dentry that is being deleted
582 * @flags: removing a directory
586 static int v9fs_remove(struct inode *dir, struct dentry *dentry, int flags)
589 int retval = -EOPNOTSUPP;
590 struct p9_fid *v9fid, *dfid;
591 struct v9fs_session_info *v9ses;
593 p9_debug(P9_DEBUG_VFS, "inode: %p dentry: %p rmdir: %x\n",
596 v9ses = v9fs_inode2v9ses(dir);
597 inode = d_inode(dentry);
598 dfid = v9fs_parent_fid(dentry);
600 retval = PTR_ERR(dfid);
601 p9_debug(P9_DEBUG_VFS, "fid lookup failed %d\n", retval);
604 if (v9fs_proto_dotl(v9ses))
605 retval = p9_client_unlinkat(dfid, dentry->d_name.name,
606 v9fs_at_to_dotl_flags(flags));
607 if (retval == -EOPNOTSUPP) {
608 /* Try the one based on path */
609 v9fid = v9fs_fid_clone(dentry);
611 return PTR_ERR(v9fid);
612 retval = p9_client_remove(v9fid);
616 * directories on unlink should have zero
619 if (flags & AT_REMOVEDIR) {
623 v9fs_dec_count(inode);
625 v9fs_invalidate_inode_attr(inode);
626 v9fs_invalidate_inode_attr(dir);
632 * v9fs_create - Create a file
633 * @v9ses: session information
634 * @dir: directory that dentry is being created in
635 * @dentry: dentry that is being created
636 * @extension: 9p2000.u extension string to support devices, etc.
637 * @perm: create permissions
641 static struct p9_fid *
642 v9fs_create(struct v9fs_session_info *v9ses, struct inode *dir,
643 struct dentry *dentry, char *extension, u32 perm, u8 mode)
646 const unsigned char *name;
647 struct p9_fid *dfid, *ofid, *fid;
650 p9_debug(P9_DEBUG_VFS, "name %pd\n", dentry);
655 name = dentry->d_name.name;
656 dfid = v9fs_parent_fid(dentry);
659 p9_debug(P9_DEBUG_VFS, "fid lookup failed %d\n", err);
663 /* clone a fid to use for creation */
664 ofid = clone_fid(dfid);
667 p9_debug(P9_DEBUG_VFS, "p9_client_walk failed %d\n", err);
671 err = p9_client_fcreate(ofid, name, perm, mode, extension);
673 p9_debug(P9_DEBUG_VFS, "p9_client_fcreate failed %d\n", err);
677 if (!(perm & P9_DMLINK)) {
678 /* now walk from the parent so we can get unopened fid */
679 fid = p9_client_walk(dfid, 1, &name, 1);
682 p9_debug(P9_DEBUG_VFS,
683 "p9_client_walk failed %d\n", err);
688 * instantiate inode and assign the unopened fid to the dentry
690 inode = v9fs_get_new_inode_from_fid(v9ses, fid, dir->i_sb);
692 err = PTR_ERR(inode);
693 p9_debug(P9_DEBUG_VFS,
694 "inode creation failed %d\n", err);
697 v9fs_fid_add(dentry, fid);
698 d_instantiate(dentry, inode);
703 p9_client_clunk(ofid);
706 p9_client_clunk(fid);
712 * v9fs_vfs_create - VFS hook to create a regular file
714 * open(.., O_CREAT) is handled in v9fs_vfs_atomic_open(). This is only called
717 * @dir: directory inode that is being created
718 * @dentry: dentry that is being deleted
719 * @mode: create permissions
724 v9fs_vfs_create(struct inode *dir, struct dentry *dentry, umode_t mode,
727 struct v9fs_session_info *v9ses = v9fs_inode2v9ses(dir);
728 u32 perm = unixmode2p9mode(v9ses, mode);
732 fid = v9fs_create(v9ses, dir, dentry, NULL, perm, P9_ORDWR);
736 v9fs_invalidate_inode_attr(dir);
737 p9_client_clunk(fid);
743 * v9fs_vfs_mkdir - VFS mkdir hook to create a directory
744 * @dir: inode that is being unlinked
745 * @dentry: dentry that is being unlinked
746 * @mode: mode for new directory
750 static int v9fs_vfs_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode)
755 struct v9fs_session_info *v9ses;
757 p9_debug(P9_DEBUG_VFS, "name %pd\n", dentry);
759 v9ses = v9fs_inode2v9ses(dir);
760 perm = unixmode2p9mode(v9ses, mode | S_IFDIR);
761 fid = v9fs_create(v9ses, dir, dentry, NULL, perm, P9_OREAD);
767 v9fs_invalidate_inode_attr(dir);
771 p9_client_clunk(fid);
777 * v9fs_vfs_lookup - VFS lookup hook to "walk" to a new inode
778 * @dir: inode that is being walked from
779 * @dentry: dentry that is being walked to?
780 * @flags: lookup flags (unused)
784 struct dentry *v9fs_vfs_lookup(struct inode *dir, struct dentry *dentry,
788 struct v9fs_session_info *v9ses;
789 struct p9_fid *dfid, *fid;
791 const unsigned char *name;
793 p9_debug(P9_DEBUG_VFS, "dir: %p dentry: (%pd) %p flags: %x\n",
794 dir, dentry, dentry, flags);
796 if (dentry->d_name.len > NAME_MAX)
797 return ERR_PTR(-ENAMETOOLONG);
799 v9ses = v9fs_inode2v9ses(dir);
800 /* We can walk d_parent because we hold the dir->i_mutex */
801 dfid = v9fs_parent_fid(dentry);
803 return ERR_CAST(dfid);
806 * Make sure we don't use a wrong inode due to parallel
807 * unlink. For cached mode create calls request for new
808 * inode. But with cache disabled, lookup should do this.
810 name = dentry->d_name.name;
811 fid = p9_client_walk(dfid, 1, &name, 1);
812 if (fid == ERR_PTR(-ENOENT))
814 else if (IS_ERR(fid))
815 inode = ERR_CAST(fid);
816 else if (v9ses->cache == CACHE_LOOSE || v9ses->cache == CACHE_FSCACHE)
817 inode = v9fs_get_inode_from_fid(v9ses, fid, dir->i_sb);
819 inode = v9fs_get_new_inode_from_fid(v9ses, fid, dir->i_sb);
821 * If we had a rename on the server and a parallel lookup
822 * for the new name, then make sure we instantiate with
823 * the new name. ie look up for a/b, while on server somebody
824 * moved b under k and client parallely did a lookup for
827 res = d_splice_alias(inode, dentry);
830 v9fs_fid_add(dentry, fid);
831 else if (!IS_ERR(res))
832 v9fs_fid_add(res, fid);
834 p9_client_clunk(fid);
840 v9fs_vfs_atomic_open(struct inode *dir, struct dentry *dentry,
841 struct file *file, unsigned flags, umode_t mode)
845 struct v9fs_inode *v9inode;
846 struct v9fs_session_info *v9ses;
847 struct p9_fid *fid, *inode_fid;
848 struct dentry *res = NULL;
850 if (d_in_lookup(dentry)) {
851 res = v9fs_vfs_lookup(dir, dentry, 0);
860 if (!(flags & O_CREAT) || d_really_is_positive(dentry))
861 return finish_no_open(file, res);
865 v9ses = v9fs_inode2v9ses(dir);
866 perm = unixmode2p9mode(v9ses, mode);
867 fid = v9fs_create(v9ses, dir, dentry, NULL, perm,
868 v9fs_uflags2omode(flags,
869 v9fs_proto_dotu(v9ses)));
876 v9fs_invalidate_inode_attr(dir);
877 v9inode = V9FS_I(d_inode(dentry));
878 mutex_lock(&v9inode->v_mutex);
879 if ((v9ses->cache == CACHE_LOOSE || v9ses->cache == CACHE_FSCACHE) &&
880 !v9inode->writeback_fid &&
881 ((flags & O_ACCMODE) != O_RDONLY)) {
883 * clone a fid and add it to writeback_fid
884 * we do it during open time instead of
885 * page dirty time via write_begin/page_mkwrite
886 * because we want write after unlink usecase
889 inode_fid = v9fs_writeback_fid(dentry);
890 if (IS_ERR(inode_fid)) {
891 err = PTR_ERR(inode_fid);
892 mutex_unlock(&v9inode->v_mutex);
895 v9inode->writeback_fid = (void *) inode_fid;
897 mutex_unlock(&v9inode->v_mutex);
898 err = finish_open(file, dentry, generic_file_open);
902 file->private_data = fid;
903 if (v9ses->cache == CACHE_LOOSE || v9ses->cache == CACHE_FSCACHE)
904 v9fs_cache_inode_set_cookie(d_inode(dentry), file);
906 file->f_mode |= FMODE_CREATED;
913 p9_client_clunk(fid);
918 * v9fs_vfs_unlink - VFS unlink hook to delete an inode
919 * @i: inode that is being unlinked
920 * @d: dentry that is being unlinked
924 int v9fs_vfs_unlink(struct inode *i, struct dentry *d)
926 return v9fs_remove(i, d, 0);
930 * v9fs_vfs_rmdir - VFS unlink hook to delete a directory
931 * @i: inode that is being unlinked
932 * @d: dentry that is being unlinked
936 int v9fs_vfs_rmdir(struct inode *i, struct dentry *d)
938 return v9fs_remove(i, d, AT_REMOVEDIR);
942 * v9fs_vfs_rename - VFS hook to rename an inode
943 * @old_dir: old dir inode
944 * @old_dentry: old dentry
945 * @new_dir: new dir inode
946 * @new_dentry: new dentry
951 v9fs_vfs_rename(struct inode *old_dir, struct dentry *old_dentry,
952 struct inode *new_dir, struct dentry *new_dentry,
956 struct inode *old_inode;
957 struct inode *new_inode;
958 struct v9fs_session_info *v9ses;
959 struct p9_fid *oldfid;
960 struct p9_fid *olddirfid;
961 struct p9_fid *newdirfid;
962 struct p9_wstat wstat;
967 p9_debug(P9_DEBUG_VFS, "\n");
969 old_inode = d_inode(old_dentry);
970 new_inode = d_inode(new_dentry);
971 v9ses = v9fs_inode2v9ses(old_inode);
972 oldfid = v9fs_fid_lookup(old_dentry);
974 return PTR_ERR(oldfid);
976 olddirfid = clone_fid(v9fs_parent_fid(old_dentry));
977 if (IS_ERR(olddirfid)) {
978 retval = PTR_ERR(olddirfid);
982 newdirfid = clone_fid(v9fs_parent_fid(new_dentry));
983 if (IS_ERR(newdirfid)) {
984 retval = PTR_ERR(newdirfid);
988 down_write(&v9ses->rename_sem);
989 if (v9fs_proto_dotl(v9ses)) {
990 retval = p9_client_renameat(olddirfid, old_dentry->d_name.name,
991 newdirfid, new_dentry->d_name.name);
992 if (retval == -EOPNOTSUPP)
993 retval = p9_client_rename(oldfid, newdirfid,
994 new_dentry->d_name.name);
995 if (retval != -EOPNOTSUPP)
998 if (old_dentry->d_parent != new_dentry->d_parent) {
1000 * 9P .u can only handle file rename in the same directory
1003 p9_debug(P9_DEBUG_ERROR, "old dir and new dir are different\n");
1007 v9fs_blank_wstat(&wstat);
1008 wstat.muid = v9ses->uname;
1009 wstat.name = new_dentry->d_name.name;
1010 retval = p9_client_wstat(oldfid, &wstat);
1015 if (S_ISDIR(new_inode->i_mode))
1016 clear_nlink(new_inode);
1018 v9fs_dec_count(new_inode);
1020 if (S_ISDIR(old_inode->i_mode)) {
1023 v9fs_dec_count(old_dir);
1025 v9fs_invalidate_inode_attr(old_inode);
1026 v9fs_invalidate_inode_attr(old_dir);
1027 v9fs_invalidate_inode_attr(new_dir);
1029 /* successful rename */
1030 d_move(old_dentry, new_dentry);
1032 up_write(&v9ses->rename_sem);
1033 p9_client_clunk(newdirfid);
1036 p9_client_clunk(olddirfid);
1043 * v9fs_vfs_getattr - retrieve file metadata
1044 * @path: Object to query
1045 * @stat: metadata structure to populate
1046 * @request_mask: Mask of STATX_xxx flags indicating the caller's interests
1047 * @flags: AT_STATX_xxx setting
1052 v9fs_vfs_getattr(const struct path *path, struct kstat *stat,
1053 u32 request_mask, unsigned int flags)
1055 struct dentry *dentry = path->dentry;
1056 struct v9fs_session_info *v9ses;
1058 struct p9_wstat *st;
1060 p9_debug(P9_DEBUG_VFS, "dentry: %p\n", dentry);
1061 v9ses = v9fs_dentry2v9ses(dentry);
1062 if (v9ses->cache == CACHE_LOOSE || v9ses->cache == CACHE_FSCACHE) {
1063 generic_fillattr(d_inode(dentry), stat);
1066 fid = v9fs_fid_lookup(dentry);
1068 return PTR_ERR(fid);
1070 st = p9_client_stat(fid);
1074 v9fs_stat2inode(st, d_inode(dentry), dentry->d_sb, 0);
1075 generic_fillattr(d_inode(dentry), stat);
1083 * v9fs_vfs_setattr - set file metadata
1084 * @dentry: file whose metadata to set
1085 * @iattr: metadata assignment structure
1089 static int v9fs_vfs_setattr(struct dentry *dentry, struct iattr *iattr)
1092 struct v9fs_session_info *v9ses;
1094 struct p9_wstat wstat;
1096 p9_debug(P9_DEBUG_VFS, "\n");
1097 retval = setattr_prepare(dentry, iattr);
1102 v9ses = v9fs_dentry2v9ses(dentry);
1103 fid = v9fs_fid_lookup(dentry);
1105 return PTR_ERR(fid);
1107 v9fs_blank_wstat(&wstat);
1108 if (iattr->ia_valid & ATTR_MODE)
1109 wstat.mode = unixmode2p9mode(v9ses, iattr->ia_mode);
1111 if (iattr->ia_valid & ATTR_MTIME)
1112 wstat.mtime = iattr->ia_mtime.tv_sec;
1114 if (iattr->ia_valid & ATTR_ATIME)
1115 wstat.atime = iattr->ia_atime.tv_sec;
1117 if (iattr->ia_valid & ATTR_SIZE)
1118 wstat.length = iattr->ia_size;
1120 if (v9fs_proto_dotu(v9ses)) {
1121 if (iattr->ia_valid & ATTR_UID)
1122 wstat.n_uid = iattr->ia_uid;
1124 if (iattr->ia_valid & ATTR_GID)
1125 wstat.n_gid = iattr->ia_gid;
1128 /* Write all dirty data */
1129 if (d_is_reg(dentry))
1130 filemap_write_and_wait(d_inode(dentry)->i_mapping);
1132 retval = p9_client_wstat(fid, &wstat);
1136 if ((iattr->ia_valid & ATTR_SIZE) &&
1137 iattr->ia_size != i_size_read(d_inode(dentry)))
1138 truncate_setsize(d_inode(dentry), iattr->ia_size);
1140 v9fs_invalidate_inode_attr(d_inode(dentry));
1142 setattr_copy(d_inode(dentry), iattr);
1143 mark_inode_dirty(d_inode(dentry));
1148 * v9fs_stat2inode - populate an inode structure with mistat info
1149 * @stat: Plan 9 metadata (mistat) structure
1150 * @inode: inode to populate
1151 * @sb: superblock of filesystem
1152 * @flags: control flags (e.g. V9FS_STAT2INODE_KEEP_ISIZE)
1157 v9fs_stat2inode(struct p9_wstat *stat, struct inode *inode,
1158 struct super_block *sb, unsigned int flags)
1163 unsigned int i_nlink;
1164 struct v9fs_session_info *v9ses = sb->s_fs_info;
1165 struct v9fs_inode *v9inode = V9FS_I(inode);
1167 set_nlink(inode, 1);
1169 inode->i_atime.tv_sec = stat->atime;
1170 inode->i_mtime.tv_sec = stat->mtime;
1171 inode->i_ctime.tv_sec = stat->mtime;
1173 inode->i_uid = v9ses->dfltuid;
1174 inode->i_gid = v9ses->dfltgid;
1176 if (v9fs_proto_dotu(v9ses)) {
1177 inode->i_uid = stat->n_uid;
1178 inode->i_gid = stat->n_gid;
1180 if ((S_ISREG(inode->i_mode)) || (S_ISDIR(inode->i_mode))) {
1181 if (v9fs_proto_dotu(v9ses) && (stat->extension[0] != '\0')) {
1183 * Hadlink support got added later to
1184 * to the .u extension. So there can be
1185 * server out there that doesn't support
1186 * this even with .u extension. So check
1187 * for non NULL stat->extension
1189 strlcpy(ext, stat->extension, sizeof(ext));
1190 /* HARDLINKCOUNT %u */
1191 sscanf(ext, "%13s %u", tag_name, &i_nlink);
1192 if (!strncmp(tag_name, "HARDLINKCOUNT", 13))
1193 set_nlink(inode, i_nlink);
1196 mode = p9mode2perm(v9ses, stat);
1197 mode |= inode->i_mode & ~S_IALLUGO;
1198 inode->i_mode = mode;
1200 if (!(flags & V9FS_STAT2INODE_KEEP_ISIZE))
1201 v9fs_i_size_write(inode, stat->length);
1202 /* not real number of blocks, but 512 byte ones ... */
1203 inode->i_blocks = (stat->length + 512 - 1) >> 9;
1204 v9inode->cache_validity &= ~V9FS_INO_INVALID_ATTR;
1208 * v9fs_qid2ino - convert qid into inode number
1211 * BUG: potential for inode number collisions?
1214 ino_t v9fs_qid2ino(struct p9_qid *qid)
1216 u64 path = qid->path + 2;
1219 if (sizeof(ino_t) == sizeof(path))
1220 memcpy(&i, &path, sizeof(ino_t));
1222 i = (ino_t) (path ^ (path >> 32));
1228 * v9fs_vfs_get_link - follow a symlink path
1229 * @dentry: dentry for symlink
1230 * @inode: inode for symlink
1231 * @done: delayed call for when we are done with the return value
1234 static const char *v9fs_vfs_get_link(struct dentry *dentry,
1235 struct inode *inode,
1236 struct delayed_call *done)
1238 struct v9fs_session_info *v9ses;
1240 struct p9_wstat *st;
1244 return ERR_PTR(-ECHILD);
1246 v9ses = v9fs_dentry2v9ses(dentry);
1247 fid = v9fs_fid_lookup(dentry);
1248 p9_debug(P9_DEBUG_VFS, "%pd\n", dentry);
1251 return ERR_CAST(fid);
1253 if (!v9fs_proto_dotu(v9ses))
1254 return ERR_PTR(-EBADF);
1256 st = p9_client_stat(fid);
1258 return ERR_CAST(st);
1260 if (!(st->mode & P9_DMSYMLINK)) {
1263 return ERR_PTR(-EINVAL);
1265 res = st->extension;
1266 st->extension = NULL;
1267 if (strlen(res) >= PATH_MAX)
1268 res[PATH_MAX - 1] = '\0';
1272 set_delayed_call(done, kfree_link, res);
1277 * v9fs_vfs_mkspecial - create a special file
1278 * @dir: inode to create special file in
1279 * @dentry: dentry to create
1280 * @perm: mode to create special file
1281 * @extension: 9p2000.u format extension string representing special file
1285 static int v9fs_vfs_mkspecial(struct inode *dir, struct dentry *dentry,
1286 u32 perm, const char *extension)
1289 struct v9fs_session_info *v9ses;
1291 v9ses = v9fs_inode2v9ses(dir);
1292 if (!v9fs_proto_dotu(v9ses)) {
1293 p9_debug(P9_DEBUG_ERROR, "not extended\n");
1297 fid = v9fs_create(v9ses, dir, dentry, (char *) extension, perm,
1300 return PTR_ERR(fid);
1302 v9fs_invalidate_inode_attr(dir);
1303 p9_client_clunk(fid);
1308 * v9fs_vfs_symlink - helper function to create symlinks
1309 * @dir: directory inode containing symlink
1310 * @dentry: dentry for symlink
1311 * @symname: symlink data
1313 * See Also: 9P2000.u RFC for more information
1318 v9fs_vfs_symlink(struct inode *dir, struct dentry *dentry, const char *symname)
1320 p9_debug(P9_DEBUG_VFS, " %lu,%pd,%s\n",
1321 dir->i_ino, dentry, symname);
1323 return v9fs_vfs_mkspecial(dir, dentry, P9_DMSYMLINK, symname);
1326 #define U32_MAX_DIGITS 10
1329 * v9fs_vfs_link - create a hardlink
1330 * @old_dentry: dentry for file to link to
1331 * @dir: inode destination for new link
1332 * @dentry: dentry for link
1337 v9fs_vfs_link(struct dentry *old_dentry, struct inode *dir,
1338 struct dentry *dentry)
1341 char name[1 + U32_MAX_DIGITS + 2]; /* sign + number + \n + \0 */
1342 struct p9_fid *oldfid;
1344 p9_debug(P9_DEBUG_VFS, " %lu,%pd,%pd\n",
1345 dir->i_ino, dentry, old_dentry);
1347 oldfid = v9fs_fid_clone(old_dentry);
1349 return PTR_ERR(oldfid);
1351 sprintf(name, "%d\n", oldfid->fid);
1352 retval = v9fs_vfs_mkspecial(dir, dentry, P9_DMLINK, name);
1354 v9fs_refresh_inode(oldfid, d_inode(old_dentry));
1355 v9fs_invalidate_inode_attr(dir);
1357 p9_client_clunk(oldfid);
1362 * v9fs_vfs_mknod - create a special file
1363 * @dir: inode destination for new link
1364 * @dentry: dentry for file
1365 * @mode: mode for creation
1366 * @rdev: device associated with special file
1371 v9fs_vfs_mknod(struct inode *dir, struct dentry *dentry, umode_t mode, dev_t rdev)
1373 struct v9fs_session_info *v9ses = v9fs_inode2v9ses(dir);
1375 char name[2 + U32_MAX_DIGITS + 1 + U32_MAX_DIGITS + 1];
1378 p9_debug(P9_DEBUG_VFS, " %lu,%pd mode: %hx MAJOR: %u MINOR: %u\n",
1379 dir->i_ino, dentry, mode,
1380 MAJOR(rdev), MINOR(rdev));
1382 /* build extension */
1384 sprintf(name, "b %u %u", MAJOR(rdev), MINOR(rdev));
1385 else if (S_ISCHR(mode))
1386 sprintf(name, "c %u %u", MAJOR(rdev), MINOR(rdev));
1390 perm = unixmode2p9mode(v9ses, mode);
1391 retval = v9fs_vfs_mkspecial(dir, dentry, perm, name);
1396 int v9fs_refresh_inode(struct p9_fid *fid, struct inode *inode)
1400 struct p9_wstat *st;
1401 struct v9fs_session_info *v9ses;
1404 v9ses = v9fs_inode2v9ses(inode);
1405 st = p9_client_stat(fid);
1409 * Don't update inode if the file type is different
1411 umode = p9mode2unixmode(v9ses, st, &rdev);
1412 if ((inode->i_mode & S_IFMT) != (umode & S_IFMT))
1416 * We don't want to refresh inode->i_size,
1417 * because we may have cached data
1419 flags = (v9ses->cache == CACHE_LOOSE || v9ses->cache == CACHE_FSCACHE) ?
1420 V9FS_STAT2INODE_KEEP_ISIZE : 0;
1421 v9fs_stat2inode(st, inode, inode->i_sb, flags);
1428 static const struct inode_operations v9fs_dir_inode_operations_dotu = {
1429 .create = v9fs_vfs_create,
1430 .lookup = v9fs_vfs_lookup,
1431 .atomic_open = v9fs_vfs_atomic_open,
1432 .symlink = v9fs_vfs_symlink,
1433 .link = v9fs_vfs_link,
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_dir_inode_operations = {
1444 .create = v9fs_vfs_create,
1445 .lookup = v9fs_vfs_lookup,
1446 .atomic_open = v9fs_vfs_atomic_open,
1447 .unlink = v9fs_vfs_unlink,
1448 .mkdir = v9fs_vfs_mkdir,
1449 .rmdir = v9fs_vfs_rmdir,
1450 .mknod = v9fs_vfs_mknod,
1451 .rename = v9fs_vfs_rename,
1452 .getattr = v9fs_vfs_getattr,
1453 .setattr = v9fs_vfs_setattr,
1456 static const struct inode_operations v9fs_file_inode_operations = {
1457 .getattr = v9fs_vfs_getattr,
1458 .setattr = v9fs_vfs_setattr,
1461 static const struct inode_operations v9fs_symlink_inode_operations = {
1462 .get_link = v9fs_vfs_get_link,
1463 .getattr = v9fs_vfs_getattr,
1464 .setattr = v9fs_vfs_setattr,