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
= kmem_cache_alloc(v9fs_inode_cache
, GFP_KERNEL
);
229 #ifdef CONFIG_9P_FSCACHE
230 v9inode
->fscache
= NULL
;
231 mutex_init(&v9inode
->fscache_lock
);
233 v9inode
->writeback_fid
= NULL
;
234 v9inode
->cache_validity
= 0;
235 mutex_init(&v9inode
->v_mutex
);
236 return &v9inode
->vfs_inode
;
240 * v9fs_free_inode - destroy an inode
244 void v9fs_free_inode(struct inode
*inode
)
246 kmem_cache_free(v9fs_inode_cache
, V9FS_I(inode
));
249 int v9fs_init_inode(struct v9fs_session_info
*v9ses
,
250 struct inode
*inode
, umode_t mode
, dev_t rdev
)
254 inode_init_owner(inode
, NULL
, mode
);
256 inode
->i_rdev
= rdev
;
257 inode
->i_atime
= inode
->i_mtime
= inode
->i_ctime
= current_time(inode
);
258 inode
->i_mapping
->a_ops
= &v9fs_addr_operations
;
259 inode
->i_private
= NULL
;
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 * v9fs_clear_inode - release an inode
373 * @inode: inode to release
376 void v9fs_evict_inode(struct inode
*inode
)
378 struct v9fs_inode
*v9inode
= V9FS_I(inode
);
380 truncate_inode_pages_final(&inode
->i_data
);
382 filemap_fdatawrite(&inode
->i_data
);
384 v9fs_cache_inode_put_cookie(inode
);
385 /* clunk the fid stashed in writeback_fid */
386 if (v9inode
->writeback_fid
) {
387 p9_client_clunk(v9inode
->writeback_fid
);
388 v9inode
->writeback_fid
= NULL
;
392 static int v9fs_test_inode(struct inode
*inode
, void *data
)
396 struct v9fs_inode
*v9inode
= V9FS_I(inode
);
397 struct p9_wstat
*st
= (struct p9_wstat
*)data
;
398 struct v9fs_session_info
*v9ses
= v9fs_inode2v9ses(inode
);
400 umode
= p9mode2unixmode(v9ses
, st
, &rdev
);
401 /* don't match inode of different type */
402 if ((inode
->i_mode
& S_IFMT
) != (umode
& S_IFMT
))
405 /* compare qid details */
406 if (memcmp(&v9inode
->qid
.version
,
407 &st
->qid
.version
, sizeof(v9inode
->qid
.version
)))
410 if (v9inode
->qid
.type
!= st
->qid
.type
)
413 if (v9inode
->qid
.path
!= st
->qid
.path
)
418 static int v9fs_test_new_inode(struct inode
*inode
, void *data
)
423 static int v9fs_set_inode(struct inode
*inode
, void *data
)
425 struct v9fs_inode
*v9inode
= V9FS_I(inode
);
426 struct p9_wstat
*st
= (struct p9_wstat
*)data
;
428 memcpy(&v9inode
->qid
, &st
->qid
, sizeof(st
->qid
));
432 static struct inode
*v9fs_qid_iget(struct super_block
*sb
,
442 struct v9fs_session_info
*v9ses
= sb
->s_fs_info
;
443 int (*test
)(struct inode
*, void *);
446 test
= v9fs_test_new_inode
;
448 test
= v9fs_test_inode
;
450 i_ino
= v9fs_qid2ino(qid
);
451 inode
= iget5_locked(sb
, i_ino
, test
, v9fs_set_inode
, st
);
453 return ERR_PTR(-ENOMEM
);
454 if (!(inode
->i_state
& I_NEW
))
457 * initialize the inode with the stat info
458 * FIXME!! we may need support for stale inodes
461 inode
->i_ino
= i_ino
;
462 umode
= p9mode2unixmode(v9ses
, st
, &rdev
);
463 retval
= v9fs_init_inode(v9ses
, inode
, umode
, rdev
);
467 v9fs_stat2inode(st
, inode
, sb
, 0);
468 v9fs_cache_inode_get_cookie(inode
);
469 unlock_new_inode(inode
);
473 return ERR_PTR(retval
);
478 v9fs_inode_from_fid(struct v9fs_session_info
*v9ses
, struct p9_fid
*fid
,
479 struct super_block
*sb
, int new)
482 struct inode
*inode
= NULL
;
484 st
= p9_client_stat(fid
);
488 inode
= v9fs_qid_iget(sb
, &st
->qid
, st
, new);
495 * v9fs_at_to_dotl_flags- convert Linux specific AT flags to
497 * @flags: flags to convert
499 static int v9fs_at_to_dotl_flags(int flags
)
502 if (flags
& AT_REMOVEDIR
)
503 rflags
|= P9_DOTL_AT_REMOVEDIR
;
508 * v9fs_dec_count - helper functon to drop i_nlink.
510 * If a directory had nlink <= 2 (including . and ..), then we should not drop
511 * the link count, which indicates the underlying exported fs doesn't maintain
512 * nlink accurately. e.g.
513 * - overlayfs sets nlink to 1 for merged dir
514 * - ext4 (with dir_nlink feature enabled) sets nlink to 1 if a dir has more
515 * than EXT4_LINK_MAX (65000) links.
517 * @inode: inode whose nlink is being dropped
519 static void v9fs_dec_count(struct inode
*inode
)
521 if (!S_ISDIR(inode
->i_mode
) || inode
->i_nlink
> 2)
526 * v9fs_remove - helper function to remove files and directories
527 * @dir: directory inode that is being deleted
528 * @dentry: dentry that is being deleted
529 * @flags: removing a directory
533 static int v9fs_remove(struct inode
*dir
, struct dentry
*dentry
, int flags
)
536 int retval
= -EOPNOTSUPP
;
537 struct p9_fid
*v9fid
, *dfid
;
538 struct v9fs_session_info
*v9ses
;
540 p9_debug(P9_DEBUG_VFS
, "inode: %p dentry: %p rmdir: %x\n",
543 v9ses
= v9fs_inode2v9ses(dir
);
544 inode
= d_inode(dentry
);
545 dfid
= v9fs_parent_fid(dentry
);
547 retval
= PTR_ERR(dfid
);
548 p9_debug(P9_DEBUG_VFS
, "fid lookup failed %d\n", retval
);
551 if (v9fs_proto_dotl(v9ses
))
552 retval
= p9_client_unlinkat(dfid
, dentry
->d_name
.name
,
553 v9fs_at_to_dotl_flags(flags
));
554 p9_client_clunk(dfid
);
555 if (retval
== -EOPNOTSUPP
) {
556 /* Try the one based on path */
557 v9fid
= v9fs_fid_clone(dentry
);
559 return PTR_ERR(v9fid
);
560 retval
= p9_client_remove(v9fid
);
564 * directories on unlink should have zero
567 if (flags
& AT_REMOVEDIR
) {
571 v9fs_dec_count(inode
);
573 v9fs_invalidate_inode_attr(inode
);
574 v9fs_invalidate_inode_attr(dir
);
576 /* invalidate all fids associated with dentry */
577 /* NOTE: This will not include open fids */
578 dentry
->d_op
->d_release(dentry
);
584 * v9fs_create - Create a file
585 * @v9ses: session information
586 * @dir: directory that dentry is being created in
587 * @dentry: dentry that is being created
588 * @extension: 9p2000.u extension string to support devices, etc.
589 * @perm: create permissions
593 static struct p9_fid
*
594 v9fs_create(struct v9fs_session_info
*v9ses
, struct inode
*dir
,
595 struct dentry
*dentry
, char *extension
, u32 perm
, u8 mode
)
598 const unsigned char *name
;
599 struct p9_fid
*dfid
, *ofid
= NULL
, *fid
= NULL
;
602 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
);
618 p9_client_clunk(dfid
);
622 err
= p9_client_fcreate(ofid
, name
, perm
, mode
, extension
);
624 p9_debug(P9_DEBUG_VFS
, "p9_client_fcreate failed %d\n", err
);
625 p9_client_clunk(dfid
);
629 if (!(perm
& P9_DMLINK
)) {
630 /* now walk from the parent so we can get unopened fid */
631 fid
= p9_client_walk(dfid
, 1, &name
, 1);
634 p9_debug(P9_DEBUG_VFS
,
635 "p9_client_walk failed %d\n", err
);
637 p9_client_clunk(dfid
);
641 * instantiate inode and assign the unopened fid to the dentry
643 inode
= v9fs_get_new_inode_from_fid(v9ses
, fid
, dir
->i_sb
);
645 err
= PTR_ERR(inode
);
646 p9_debug(P9_DEBUG_VFS
,
647 "inode creation failed %d\n", err
);
648 p9_client_clunk(dfid
);
651 v9fs_fid_add(dentry
, fid
);
652 d_instantiate(dentry
, inode
);
654 p9_client_clunk(dfid
);
658 p9_client_clunk(ofid
);
661 p9_client_clunk(fid
);
667 * v9fs_vfs_create - VFS hook to create a regular file
669 * open(.., O_CREAT) is handled in v9fs_vfs_atomic_open(). This is only called
672 * @dir: directory inode that is being created
673 * @dentry: dentry that is being deleted
674 * @mode: create permissions
679 v9fs_vfs_create(struct inode
*dir
, struct dentry
*dentry
, umode_t mode
,
682 struct v9fs_session_info
*v9ses
= v9fs_inode2v9ses(dir
);
683 u32 perm
= unixmode2p9mode(v9ses
, mode
);
687 fid
= v9fs_create(v9ses
, dir
, dentry
, NULL
, perm
, P9_ORDWR
);
691 v9fs_invalidate_inode_attr(dir
);
692 p9_client_clunk(fid
);
698 * v9fs_vfs_mkdir - VFS mkdir hook to create a directory
699 * @dir: inode that is being unlinked
700 * @dentry: dentry that is being unlinked
701 * @mode: mode for new directory
705 static int v9fs_vfs_mkdir(struct inode
*dir
, struct dentry
*dentry
, umode_t mode
)
710 struct v9fs_session_info
*v9ses
;
712 p9_debug(P9_DEBUG_VFS
, "name %pd\n", dentry
);
714 v9ses
= v9fs_inode2v9ses(dir
);
715 perm
= unixmode2p9mode(v9ses
, mode
| S_IFDIR
);
716 fid
= v9fs_create(v9ses
, dir
, dentry
, NULL
, perm
, P9_OREAD
);
722 v9fs_invalidate_inode_attr(dir
);
726 p9_client_clunk(fid
);
732 * v9fs_vfs_lookup - VFS lookup hook to "walk" to a new inode
733 * @dir: inode that is being walked from
734 * @dentry: dentry that is being walked to?
735 * @flags: lookup flags (unused)
739 struct dentry
*v9fs_vfs_lookup(struct inode
*dir
, struct dentry
*dentry
,
743 struct v9fs_session_info
*v9ses
;
744 struct p9_fid
*dfid
, *fid
;
746 const unsigned char *name
;
748 p9_debug(P9_DEBUG_VFS
, "dir: %p dentry: (%pd) %p flags: %x\n",
749 dir
, dentry
, dentry
, flags
);
751 if (dentry
->d_name
.len
> NAME_MAX
)
752 return ERR_PTR(-ENAMETOOLONG
);
754 v9ses
= v9fs_inode2v9ses(dir
);
755 /* We can walk d_parent because we hold the dir->i_mutex */
756 dfid
= v9fs_parent_fid(dentry
);
758 return ERR_CAST(dfid
);
761 * Make sure we don't use a wrong inode due to parallel
762 * unlink. For cached mode create calls request for new
763 * inode. But with cache disabled, lookup should do this.
765 name
= dentry
->d_name
.name
;
766 fid
= p9_client_walk(dfid
, 1, &name
, 1);
767 p9_client_clunk(dfid
);
768 if (fid
== ERR_PTR(-ENOENT
))
770 else if (IS_ERR(fid
))
771 inode
= ERR_CAST(fid
);
772 else if (v9ses
->cache
== CACHE_LOOSE
|| v9ses
->cache
== CACHE_FSCACHE
)
773 inode
= v9fs_get_inode_from_fid(v9ses
, fid
, dir
->i_sb
);
775 inode
= v9fs_get_new_inode_from_fid(v9ses
, fid
, dir
->i_sb
);
777 * If we had a rename on the server and a parallel lookup
778 * for the new name, then make sure we instantiate with
779 * the new name. ie look up for a/b, while on server somebody
780 * moved b under k and client parallely did a lookup for
783 res
= d_splice_alias(inode
, dentry
);
786 v9fs_fid_add(dentry
, fid
);
787 else if (!IS_ERR(res
))
788 v9fs_fid_add(res
, fid
);
790 p9_client_clunk(fid
);
796 v9fs_vfs_atomic_open(struct inode
*dir
, struct dentry
*dentry
,
797 struct file
*file
, unsigned flags
, umode_t mode
)
801 struct v9fs_inode
*v9inode
;
802 struct v9fs_session_info
*v9ses
;
803 struct p9_fid
*fid
, *inode_fid
;
804 struct dentry
*res
= NULL
;
807 if (d_in_lookup(dentry
)) {
808 res
= v9fs_vfs_lookup(dir
, dentry
, 0);
817 if (!(flags
& O_CREAT
) || d_really_is_positive(dentry
))
818 return finish_no_open(file
, res
);
822 v9ses
= v9fs_inode2v9ses(dir
);
823 perm
= unixmode2p9mode(v9ses
, mode
);
824 fid
= v9fs_create(v9ses
, dir
, dentry
, NULL
, perm
,
825 v9fs_uflags2omode(flags
,
826 v9fs_proto_dotu(v9ses
)));
833 v9fs_invalidate_inode_attr(dir
);
834 inode
= d_inode(dentry
);
835 v9inode
= V9FS_I(inode
);
836 mutex_lock(&v9inode
->v_mutex
);
837 if ((v9ses
->cache
== CACHE_LOOSE
|| v9ses
->cache
== CACHE_FSCACHE
) &&
838 !v9inode
->writeback_fid
&&
839 ((flags
& O_ACCMODE
) != O_RDONLY
)) {
841 * clone a fid and add it to writeback_fid
842 * we do it during open time instead of
843 * page dirty time via write_begin/page_mkwrite
844 * because we want write after unlink usecase
847 inode_fid
= v9fs_writeback_fid(dentry
);
848 if (IS_ERR(inode_fid
)) {
849 err
= PTR_ERR(inode_fid
);
850 mutex_unlock(&v9inode
->v_mutex
);
853 v9inode
->writeback_fid
= (void *) inode_fid
;
855 mutex_unlock(&v9inode
->v_mutex
);
856 err
= finish_open(file
, dentry
, generic_file_open
);
860 file
->private_data
= fid
;
861 if (v9ses
->cache
== CACHE_LOOSE
|| v9ses
->cache
== CACHE_FSCACHE
)
862 v9fs_cache_inode_set_cookie(d_inode(dentry
), file
);
863 v9fs_open_fid_add(inode
, fid
);
865 file
->f_mode
|= FMODE_CREATED
;
872 p9_client_clunk(fid
);
877 * v9fs_vfs_unlink - VFS unlink hook to delete an inode
878 * @i: inode that is being unlinked
879 * @d: dentry that is being unlinked
883 int v9fs_vfs_unlink(struct inode
*i
, struct dentry
*d
)
885 return v9fs_remove(i
, d
, 0);
889 * v9fs_vfs_rmdir - VFS unlink hook to delete a directory
890 * @i: inode that is being unlinked
891 * @d: dentry that is being unlinked
895 int v9fs_vfs_rmdir(struct inode
*i
, struct dentry
*d
)
897 return v9fs_remove(i
, d
, AT_REMOVEDIR
);
901 * v9fs_vfs_rename - VFS hook to rename an inode
902 * @old_dir: old dir inode
903 * @old_dentry: old dentry
904 * @new_dir: new dir inode
905 * @new_dentry: new dentry
910 v9fs_vfs_rename(struct inode
*old_dir
, struct dentry
*old_dentry
,
911 struct inode
*new_dir
, struct dentry
*new_dentry
,
915 struct inode
*old_inode
;
916 struct inode
*new_inode
;
917 struct v9fs_session_info
*v9ses
;
918 struct p9_fid
*oldfid
, *dfid
;
919 struct p9_fid
*olddirfid
;
920 struct p9_fid
*newdirfid
;
921 struct p9_wstat wstat
;
926 p9_debug(P9_DEBUG_VFS
, "\n");
928 old_inode
= d_inode(old_dentry
);
929 new_inode
= d_inode(new_dentry
);
930 v9ses
= v9fs_inode2v9ses(old_inode
);
931 oldfid
= v9fs_fid_lookup(old_dentry
);
933 return PTR_ERR(oldfid
);
935 dfid
= v9fs_parent_fid(old_dentry
);
936 olddirfid
= clone_fid(dfid
);
937 if (dfid
&& !IS_ERR(dfid
))
938 p9_client_clunk(dfid
);
940 if (IS_ERR(olddirfid
)) {
941 retval
= PTR_ERR(olddirfid
);
945 dfid
= v9fs_parent_fid(new_dentry
);
946 newdirfid
= clone_fid(dfid
);
947 p9_client_clunk(dfid
);
949 if (IS_ERR(newdirfid
)) {
950 retval
= PTR_ERR(newdirfid
);
954 down_write(&v9ses
->rename_sem
);
955 if (v9fs_proto_dotl(v9ses
)) {
956 retval
= p9_client_renameat(olddirfid
, old_dentry
->d_name
.name
,
957 newdirfid
, new_dentry
->d_name
.name
);
958 if (retval
== -EOPNOTSUPP
)
959 retval
= p9_client_rename(oldfid
, newdirfid
,
960 new_dentry
->d_name
.name
);
961 if (retval
!= -EOPNOTSUPP
)
964 if (old_dentry
->d_parent
!= new_dentry
->d_parent
) {
966 * 9P .u can only handle file rename in the same directory
969 p9_debug(P9_DEBUG_ERROR
, "old dir and new dir are different\n");
973 v9fs_blank_wstat(&wstat
);
974 wstat
.muid
= v9ses
->uname
;
975 wstat
.name
= new_dentry
->d_name
.name
;
976 retval
= p9_client_wstat(oldfid
, &wstat
);
981 if (S_ISDIR(new_inode
->i_mode
))
982 clear_nlink(new_inode
);
984 v9fs_dec_count(new_inode
);
986 if (S_ISDIR(old_inode
->i_mode
)) {
989 v9fs_dec_count(old_dir
);
991 v9fs_invalidate_inode_attr(old_inode
);
992 v9fs_invalidate_inode_attr(old_dir
);
993 v9fs_invalidate_inode_attr(new_dir
);
995 /* successful rename */
996 d_move(old_dentry
, new_dentry
);
998 up_write(&v9ses
->rename_sem
);
999 p9_client_clunk(newdirfid
);
1002 p9_client_clunk(olddirfid
);
1005 p9_client_clunk(oldfid
);
1010 * v9fs_vfs_getattr - retrieve file metadata
1011 * @path: Object to query
1012 * @stat: metadata structure to populate
1013 * @request_mask: Mask of STATX_xxx flags indicating the caller's interests
1014 * @flags: AT_STATX_xxx setting
1019 v9fs_vfs_getattr(const struct path
*path
, struct kstat
*stat
,
1020 u32 request_mask
, unsigned int flags
)
1022 struct dentry
*dentry
= path
->dentry
;
1023 struct v9fs_session_info
*v9ses
;
1025 struct p9_wstat
*st
;
1027 p9_debug(P9_DEBUG_VFS
, "dentry: %p\n", dentry
);
1028 v9ses
= v9fs_dentry2v9ses(dentry
);
1029 if (v9ses
->cache
== CACHE_LOOSE
|| v9ses
->cache
== CACHE_FSCACHE
) {
1030 generic_fillattr(d_inode(dentry
), stat
);
1033 fid
= v9fs_fid_lookup(dentry
);
1035 return PTR_ERR(fid
);
1037 st
= p9_client_stat(fid
);
1038 p9_client_clunk(fid
);
1042 v9fs_stat2inode(st
, d_inode(dentry
), dentry
->d_sb
, 0);
1043 generic_fillattr(d_inode(dentry
), stat
);
1051 * v9fs_vfs_setattr - set file metadata
1052 * @dentry: file whose metadata to set
1053 * @iattr: metadata assignment structure
1057 static int v9fs_vfs_setattr(struct dentry
*dentry
, struct iattr
*iattr
)
1059 int retval
, use_dentry
= 0;
1060 struct v9fs_session_info
*v9ses
;
1061 struct p9_fid
*fid
= NULL
;
1062 struct p9_wstat wstat
;
1064 p9_debug(P9_DEBUG_VFS
, "\n");
1065 retval
= setattr_prepare(dentry
, iattr
);
1070 v9ses
= v9fs_dentry2v9ses(dentry
);
1071 if (iattr
->ia_valid
& ATTR_FILE
) {
1072 fid
= iattr
->ia_file
->private_data
;
1076 fid
= v9fs_fid_lookup(dentry
);
1080 return PTR_ERR(fid
);
1082 v9fs_blank_wstat(&wstat
);
1083 if (iattr
->ia_valid
& ATTR_MODE
)
1084 wstat
.mode
= unixmode2p9mode(v9ses
, iattr
->ia_mode
);
1086 if (iattr
->ia_valid
& ATTR_MTIME
)
1087 wstat
.mtime
= iattr
->ia_mtime
.tv_sec
;
1089 if (iattr
->ia_valid
& ATTR_ATIME
)
1090 wstat
.atime
= iattr
->ia_atime
.tv_sec
;
1092 if (iattr
->ia_valid
& ATTR_SIZE
)
1093 wstat
.length
= iattr
->ia_size
;
1095 if (v9fs_proto_dotu(v9ses
)) {
1096 if (iattr
->ia_valid
& ATTR_UID
)
1097 wstat
.n_uid
= iattr
->ia_uid
;
1099 if (iattr
->ia_valid
& ATTR_GID
)
1100 wstat
.n_gid
= iattr
->ia_gid
;
1103 /* Write all dirty data */
1104 if (d_is_reg(dentry
))
1105 filemap_write_and_wait(d_inode(dentry
)->i_mapping
);
1107 retval
= p9_client_wstat(fid
, &wstat
);
1110 p9_client_clunk(fid
);
1115 if ((iattr
->ia_valid
& ATTR_SIZE
) &&
1116 iattr
->ia_size
!= i_size_read(d_inode(dentry
)))
1117 truncate_setsize(d_inode(dentry
), iattr
->ia_size
);
1119 v9fs_invalidate_inode_attr(d_inode(dentry
));
1121 setattr_copy(d_inode(dentry
), iattr
);
1122 mark_inode_dirty(d_inode(dentry
));
1127 * v9fs_stat2inode - populate an inode structure with mistat info
1128 * @stat: Plan 9 metadata (mistat) structure
1129 * @inode: inode to populate
1130 * @sb: superblock of filesystem
1131 * @flags: control flags (e.g. V9FS_STAT2INODE_KEEP_ISIZE)
1136 v9fs_stat2inode(struct p9_wstat
*stat
, struct inode
*inode
,
1137 struct super_block
*sb
, unsigned int flags
)
1142 unsigned int i_nlink
;
1143 struct v9fs_session_info
*v9ses
= sb
->s_fs_info
;
1144 struct v9fs_inode
*v9inode
= V9FS_I(inode
);
1146 set_nlink(inode
, 1);
1148 inode
->i_atime
.tv_sec
= stat
->atime
;
1149 inode
->i_mtime
.tv_sec
= stat
->mtime
;
1150 inode
->i_ctime
.tv_sec
= stat
->mtime
;
1152 inode
->i_uid
= v9ses
->dfltuid
;
1153 inode
->i_gid
= v9ses
->dfltgid
;
1155 if (v9fs_proto_dotu(v9ses
)) {
1156 inode
->i_uid
= stat
->n_uid
;
1157 inode
->i_gid
= stat
->n_gid
;
1159 if ((S_ISREG(inode
->i_mode
)) || (S_ISDIR(inode
->i_mode
))) {
1160 if (v9fs_proto_dotu(v9ses
) && (stat
->extension
[0] != '\0')) {
1162 * Hadlink support got added later to
1163 * to the .u extension. So there can be
1164 * server out there that doesn't support
1165 * this even with .u extension. So check
1166 * for non NULL stat->extension
1168 strlcpy(ext
, stat
->extension
, sizeof(ext
));
1169 /* HARDLINKCOUNT %u */
1170 sscanf(ext
, "%13s %u", tag_name
, &i_nlink
);
1171 if (!strncmp(tag_name
, "HARDLINKCOUNT", 13))
1172 set_nlink(inode
, i_nlink
);
1175 mode
= p9mode2perm(v9ses
, stat
);
1176 mode
|= inode
->i_mode
& ~S_IALLUGO
;
1177 inode
->i_mode
= mode
;
1179 if (!(flags
& V9FS_STAT2INODE_KEEP_ISIZE
))
1180 v9fs_i_size_write(inode
, stat
->length
);
1181 /* not real number of blocks, but 512 byte ones ... */
1182 inode
->i_blocks
= (stat
->length
+ 512 - 1) >> 9;
1183 v9inode
->cache_validity
&= ~V9FS_INO_INVALID_ATTR
;
1187 * v9fs_qid2ino - convert qid into inode number
1190 * BUG: potential for inode number collisions?
1193 ino_t
v9fs_qid2ino(struct p9_qid
*qid
)
1195 u64 path
= qid
->path
+ 2;
1198 if (sizeof(ino_t
) == sizeof(path
))
1199 memcpy(&i
, &path
, sizeof(ino_t
));
1201 i
= (ino_t
) (path
^ (path
>> 32));
1207 * v9fs_vfs_get_link - follow a symlink path
1208 * @dentry: dentry for symlink
1209 * @inode: inode for symlink
1210 * @done: delayed call for when we are done with the return value
1213 static const char *v9fs_vfs_get_link(struct dentry
*dentry
,
1214 struct inode
*inode
,
1215 struct delayed_call
*done
)
1217 struct v9fs_session_info
*v9ses
;
1219 struct p9_wstat
*st
;
1223 return ERR_PTR(-ECHILD
);
1225 v9ses
= v9fs_dentry2v9ses(dentry
);
1226 fid
= v9fs_fid_lookup(dentry
);
1227 p9_debug(P9_DEBUG_VFS
, "%pd\n", dentry
);
1230 return ERR_CAST(fid
);
1232 if (!v9fs_proto_dotu(v9ses
))
1233 return ERR_PTR(-EBADF
);
1235 st
= p9_client_stat(fid
);
1236 p9_client_clunk(fid
);
1238 return ERR_CAST(st
);
1240 if (!(st
->mode
& P9_DMSYMLINK
)) {
1243 return ERR_PTR(-EINVAL
);
1245 res
= st
->extension
;
1246 st
->extension
= NULL
;
1247 if (strlen(res
) >= PATH_MAX
)
1248 res
[PATH_MAX
- 1] = '\0';
1252 set_delayed_call(done
, kfree_link
, res
);
1257 * v9fs_vfs_mkspecial - create a special file
1258 * @dir: inode to create special file in
1259 * @dentry: dentry to create
1260 * @perm: mode to create special file
1261 * @extension: 9p2000.u format extension string representing special file
1265 static int v9fs_vfs_mkspecial(struct inode
*dir
, struct dentry
*dentry
,
1266 u32 perm
, const char *extension
)
1269 struct v9fs_session_info
*v9ses
;
1271 v9ses
= v9fs_inode2v9ses(dir
);
1272 if (!v9fs_proto_dotu(v9ses
)) {
1273 p9_debug(P9_DEBUG_ERROR
, "not extended\n");
1277 fid
= v9fs_create(v9ses
, dir
, dentry
, (char *) extension
, perm
,
1280 return PTR_ERR(fid
);
1282 v9fs_invalidate_inode_attr(dir
);
1283 p9_client_clunk(fid
);
1288 * v9fs_vfs_symlink - helper function to create symlinks
1289 * @dir: directory inode containing symlink
1290 * @dentry: dentry for symlink
1291 * @symname: symlink data
1293 * See Also: 9P2000.u RFC for more information
1298 v9fs_vfs_symlink(struct inode
*dir
, struct dentry
*dentry
, const char *symname
)
1300 p9_debug(P9_DEBUG_VFS
, " %lu,%pd,%s\n",
1301 dir
->i_ino
, dentry
, symname
);
1303 return v9fs_vfs_mkspecial(dir
, dentry
, P9_DMSYMLINK
, symname
);
1306 #define U32_MAX_DIGITS 10
1309 * v9fs_vfs_link - create a hardlink
1310 * @old_dentry: dentry for file to link to
1311 * @dir: inode destination for new link
1312 * @dentry: dentry for link
1317 v9fs_vfs_link(struct dentry
*old_dentry
, struct inode
*dir
,
1318 struct dentry
*dentry
)
1321 char name
[1 + U32_MAX_DIGITS
+ 2]; /* sign + number + \n + \0 */
1322 struct p9_fid
*oldfid
;
1324 p9_debug(P9_DEBUG_VFS
, " %lu,%pd,%pd\n",
1325 dir
->i_ino
, dentry
, old_dentry
);
1327 oldfid
= v9fs_fid_clone(old_dentry
);
1329 return PTR_ERR(oldfid
);
1331 sprintf(name
, "%d\n", oldfid
->fid
);
1332 retval
= v9fs_vfs_mkspecial(dir
, dentry
, P9_DMLINK
, name
);
1334 v9fs_refresh_inode(oldfid
, d_inode(old_dentry
));
1335 v9fs_invalidate_inode_attr(dir
);
1337 p9_client_clunk(oldfid
);
1342 * v9fs_vfs_mknod - create a special file
1343 * @dir: inode destination for new link
1344 * @dentry: dentry for file
1345 * @mode: mode for creation
1346 * @rdev: device associated with special file
1351 v9fs_vfs_mknod(struct inode
*dir
, struct dentry
*dentry
, umode_t mode
, dev_t rdev
)
1353 struct v9fs_session_info
*v9ses
= v9fs_inode2v9ses(dir
);
1355 char name
[2 + U32_MAX_DIGITS
+ 1 + U32_MAX_DIGITS
+ 1];
1358 p9_debug(P9_DEBUG_VFS
, " %lu,%pd mode: %hx MAJOR: %u MINOR: %u\n",
1359 dir
->i_ino
, dentry
, mode
,
1360 MAJOR(rdev
), MINOR(rdev
));
1362 /* build extension */
1364 sprintf(name
, "b %u %u", MAJOR(rdev
), MINOR(rdev
));
1365 else if (S_ISCHR(mode
))
1366 sprintf(name
, "c %u %u", MAJOR(rdev
), MINOR(rdev
));
1370 perm
= unixmode2p9mode(v9ses
, mode
);
1371 retval
= v9fs_vfs_mkspecial(dir
, dentry
, perm
, name
);
1376 int v9fs_refresh_inode(struct p9_fid
*fid
, struct inode
*inode
)
1380 struct p9_wstat
*st
;
1381 struct v9fs_session_info
*v9ses
;
1384 v9ses
= v9fs_inode2v9ses(inode
);
1385 st
= p9_client_stat(fid
);
1389 * Don't update inode if the file type is different
1391 umode
= p9mode2unixmode(v9ses
, st
, &rdev
);
1392 if ((inode
->i_mode
& S_IFMT
) != (umode
& S_IFMT
))
1396 * We don't want to refresh inode->i_size,
1397 * because we may have cached data
1399 flags
= (v9ses
->cache
== CACHE_LOOSE
|| v9ses
->cache
== CACHE_FSCACHE
) ?
1400 V9FS_STAT2INODE_KEEP_ISIZE
: 0;
1401 v9fs_stat2inode(st
, inode
, inode
->i_sb
, flags
);
1408 static const struct inode_operations v9fs_dir_inode_operations_dotu
= {
1409 .create
= v9fs_vfs_create
,
1410 .lookup
= v9fs_vfs_lookup
,
1411 .atomic_open
= v9fs_vfs_atomic_open
,
1412 .symlink
= v9fs_vfs_symlink
,
1413 .link
= v9fs_vfs_link
,
1414 .unlink
= v9fs_vfs_unlink
,
1415 .mkdir
= v9fs_vfs_mkdir
,
1416 .rmdir
= v9fs_vfs_rmdir
,
1417 .mknod
= v9fs_vfs_mknod
,
1418 .rename
= v9fs_vfs_rename
,
1419 .getattr
= v9fs_vfs_getattr
,
1420 .setattr
= v9fs_vfs_setattr
,
1423 static const struct inode_operations v9fs_dir_inode_operations
= {
1424 .create
= v9fs_vfs_create
,
1425 .lookup
= v9fs_vfs_lookup
,
1426 .atomic_open
= v9fs_vfs_atomic_open
,
1427 .unlink
= v9fs_vfs_unlink
,
1428 .mkdir
= v9fs_vfs_mkdir
,
1429 .rmdir
= v9fs_vfs_rmdir
,
1430 .mknod
= v9fs_vfs_mknod
,
1431 .rename
= v9fs_vfs_rename
,
1432 .getattr
= v9fs_vfs_getattr
,
1433 .setattr
= v9fs_vfs_setattr
,
1436 static const struct inode_operations v9fs_file_inode_operations
= {
1437 .getattr
= v9fs_vfs_getattr
,
1438 .setattr
= v9fs_vfs_setattr
,
1441 static const struct inode_operations v9fs_symlink_inode_operations
= {
1442 .get_link
= v9fs_vfs_get_link
,
1443 .getattr
= v9fs_vfs_getattr
,
1444 .setattr
= v9fs_vfs_setattr
,