2 * linux/fs/9p/vfs_inode_dotl.c
4 * This file contains vfs inode ops for the 9P2000.L protocol.
6 * Copyright (C) 2004 by Eric Van Hensbergen <ericvh@gmail.com>
7 * Copyright (C) 2002 by Ron Minnich <rminnich@lanl.gov>
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License version 2
11 * as published by the Free Software Foundation.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to:
20 * Free Software Foundation
21 * 51 Franklin Street, Fifth Floor
22 * Boston, MA 02111-1301 USA
26 #include <linux/module.h>
27 #include <linux/errno.h>
29 #include <linux/file.h>
30 #include <linux/pagemap.h>
31 #include <linux/stat.h>
32 #include <linux/string.h>
33 #include <linux/inet.h>
34 #include <linux/namei.h>
35 #include <linux/idr.h>
36 #include <linux/sched.h>
37 #include <linux/slab.h>
38 #include <linux/xattr.h>
39 #include <linux/posix_acl.h>
40 #include <net/9p/9p.h>
41 #include <net/9p/client.h>
51 v9fs_vfs_mknod_dotl(struct inode *dir, struct dentry *dentry, int omode,
55 * v9fs_get_fsgid_for_create - Helper function to get the gid for creating a
56 * new file system object. This checks the S_ISGID to determine the owning
57 * group of the new file system object.
60 static gid_t v9fs_get_fsgid_for_create(struct inode *dir_inode)
62 BUG_ON(dir_inode == NULL);
64 if (dir_inode->i_mode & S_ISGID) {
65 /* set_gid bit is set.*/
66 return dir_inode->i_gid;
68 return current_fsgid();
72 * v9fs_dentry_from_dir_inode - helper function to get the dentry from
77 static struct dentry *v9fs_dentry_from_dir_inode(struct inode *inode)
79 struct dentry *dentry;
81 spin_lock(&inode->i_lock);
82 /* Directory should have only one entry. */
83 BUG_ON(S_ISDIR(inode->i_mode) && !list_is_singular(&inode->i_dentry));
84 dentry = list_entry(inode->i_dentry.next, struct dentry, d_alias);
85 spin_unlock(&inode->i_lock);
89 static struct inode *v9fs_qid_iget_dotl(struct super_block *sb,
92 struct p9_stat_dotl *st)
97 struct v9fs_session_info *v9ses = sb->s_fs_info;
99 i_ino = v9fs_qid2ino(qid);
100 inode = iget_locked(sb, i_ino);
102 return ERR_PTR(-ENOMEM);
103 if (!(inode->i_state & I_NEW))
106 * initialize the inode with the stat info
107 * FIXME!! we may need support for stale inodes
110 retval = v9fs_init_inode(v9ses, inode, st->st_mode);
114 v9fs_stat2inode_dotl(st, inode);
115 #ifdef CONFIG_9P_FSCACHE
116 v9fs_fscache_set_key(inode, &st->qid);
117 v9fs_cache_inode_get_cookie(inode);
119 retval = v9fs_get_acl(inode, fid);
123 unlock_new_inode(inode);
126 unlock_new_inode(inode);
128 return ERR_PTR(retval);
133 v9fs_inode_from_fid_dotl(struct v9fs_session_info *v9ses, struct p9_fid *fid,
134 struct super_block *sb)
136 struct p9_stat_dotl *st;
137 struct inode *inode = NULL;
139 st = p9_client_getattr_dotl(fid, P9_STATS_BASIC);
143 inode = v9fs_qid_iget_dotl(sb, &st->qid, fid, st);
149 * v9fs_vfs_create_dotl - VFS hook to create files for 9P2000.L protocol.
150 * @dir: directory inode that is being created
151 * @dentry: dentry that is being deleted
152 * @mode: create permissions
153 * @nd: path information
158 v9fs_vfs_create_dotl(struct inode *dir, struct dentry *dentry, int omode,
159 struct nameidata *nd)
169 struct p9_fid *fid = NULL;
170 struct v9fs_inode *v9inode;
171 struct p9_fid *dfid, *ofid, *inode_fid;
172 struct v9fs_session_info *v9ses;
173 struct posix_acl *pacl = NULL, *dacl = NULL;
175 v9ses = v9fs_inode2v9ses(dir);
176 if (nd && nd->flags & LOOKUP_OPEN)
177 flags = nd->intent.open.flags - 1;
180 * create call without LOOKUP_OPEN is due
181 * to mknod of regular files. So use mknod
184 return v9fs_vfs_mknod_dotl(dir, dentry, omode, 0);
187 name = (char *) dentry->d_name.name;
188 P9_DPRINTK(P9_DEBUG_VFS, "v9fs_vfs_create_dotl: name:%s flags:0x%x "
189 "mode:0x%x\n", name, flags, omode);
191 dfid = v9fs_fid_lookup(dentry->d_parent);
194 P9_DPRINTK(P9_DEBUG_VFS, "fid lookup failed %d\n", err);
198 /* clone a fid to use for creation */
199 ofid = p9_client_walk(dfid, 0, NULL, 1);
202 P9_DPRINTK(P9_DEBUG_VFS, "p9_client_walk failed %d\n", err);
206 gid = v9fs_get_fsgid_for_create(dir);
209 /* Update mode based on ACL value */
210 err = v9fs_acl_mode(dir, &mode, &dacl, &pacl);
212 P9_DPRINTK(P9_DEBUG_VFS,
213 "Failed to get acl values in creat %d\n", err);
216 err = p9_client_create_dotl(ofid, name, flags, mode, gid, &qid);
218 P9_DPRINTK(P9_DEBUG_VFS,
219 "p9_client_open_dotl failed in creat %d\n",
224 /* instantiate inode and assign the unopened fid to the dentry */
225 fid = p9_client_walk(dfid, 1, &name, 1);
228 P9_DPRINTK(P9_DEBUG_VFS, "p9_client_walk failed %d\n", err);
232 inode = v9fs_get_inode_from_fid(v9ses, fid, dir->i_sb);
234 err = PTR_ERR(inode);
235 P9_DPRINTK(P9_DEBUG_VFS, "inode creation failed %d\n", err);
238 d_instantiate(dentry, inode);
239 err = v9fs_fid_add(dentry, fid);
243 /* Now set the ACL based on the default value */
244 v9fs_set_create_acl(dentry, dacl, pacl);
246 v9inode = V9FS_I(inode);
247 if (v9ses->cache && !v9inode->writeback_fid) {
249 * clone a fid and add it to writeback_fid
250 * we do it during open time instead of
251 * page dirty time via write_begin/page_mkwrite
252 * because we want write after unlink usecase
255 inode_fid = v9fs_writeback_fid(dentry);
256 if (IS_ERR(inode_fid)) {
257 err = PTR_ERR(inode_fid);
260 v9inode->writeback_fid = (void *) inode_fid;
262 /* Since we are opening a file, assign the open fid to the file */
263 filp = lookup_instantiate_filp(nd, dentry, generic_file_open);
265 p9_client_clunk(ofid);
266 return PTR_ERR(filp);
268 filp->private_data = ofid;
269 #ifdef CONFIG_9P_FSCACHE
271 v9fs_cache_inode_set_cookie(inode, filp);
277 p9_client_clunk(ofid);
279 p9_client_clunk(fid);
284 * v9fs_vfs_mkdir_dotl - VFS mkdir hook to create a directory
285 * @dir: inode that is being unlinked
286 * @dentry: dentry that is being unlinked
287 * @mode: mode for new directory
291 static int v9fs_vfs_mkdir_dotl(struct inode *dir,
292 struct dentry *dentry, int omode)
295 struct v9fs_session_info *v9ses;
296 struct p9_fid *fid = NULL, *dfid = NULL;
302 struct dentry *dir_dentry;
303 struct posix_acl *dacl = NULL, *pacl = NULL;
305 P9_DPRINTK(P9_DEBUG_VFS, "name %s\n", dentry->d_name.name);
307 v9ses = v9fs_inode2v9ses(dir);
310 if (dir->i_mode & S_ISGID)
313 dir_dentry = v9fs_dentry_from_dir_inode(dir);
314 dfid = v9fs_fid_lookup(dir_dentry);
317 P9_DPRINTK(P9_DEBUG_VFS, "fid lookup failed %d\n", err);
322 gid = v9fs_get_fsgid_for_create(dir);
324 /* Update mode based on ACL value */
325 err = v9fs_acl_mode(dir, &mode, &dacl, &pacl);
327 P9_DPRINTK(P9_DEBUG_VFS,
328 "Failed to get acl values in mkdir %d\n", err);
331 name = (char *) dentry->d_name.name;
332 err = p9_client_mkdir_dotl(dfid, name, mode, gid, &qid);
336 /* instantiate inode and assign the unopened fid to the dentry */
337 if (v9ses->cache == CACHE_LOOSE || v9ses->cache == CACHE_FSCACHE) {
338 fid = p9_client_walk(dfid, 1, &name, 1);
341 P9_DPRINTK(P9_DEBUG_VFS, "p9_client_walk failed %d\n",
347 inode = v9fs_get_inode_from_fid(v9ses, fid, dir->i_sb);
349 err = PTR_ERR(inode);
350 P9_DPRINTK(P9_DEBUG_VFS, "inode creation failed %d\n",
354 d_instantiate(dentry, inode);
355 err = v9fs_fid_add(dentry, fid);
361 * Not in cached mode. No need to populate
362 * inode with stat. We need to get an inode
363 * so that we can set the acl with dentry
365 inode = v9fs_get_inode(dir->i_sb, mode);
367 err = PTR_ERR(inode);
370 d_instantiate(dentry, inode);
372 /* Now set the ACL based on the default value */
373 v9fs_set_create_acl(dentry, dacl, pacl);
377 p9_client_clunk(fid);
382 v9fs_vfs_getattr_dotl(struct vfsmount *mnt, struct dentry *dentry,
386 struct v9fs_session_info *v9ses;
388 struct p9_stat_dotl *st;
390 P9_DPRINTK(P9_DEBUG_VFS, "dentry: %p\n", dentry);
392 v9ses = v9fs_inode2v9ses(dentry->d_inode);
393 if (v9ses->cache == CACHE_LOOSE || v9ses->cache == CACHE_FSCACHE) {
394 generic_fillattr(dentry->d_inode, stat);
397 fid = v9fs_fid_lookup(dentry);
401 /* Ask for all the fields in stat structure. Server will return
402 * whatever it supports
405 st = p9_client_getattr_dotl(fid, P9_STATS_ALL);
409 v9fs_stat2inode_dotl(st, dentry->d_inode);
410 generic_fillattr(dentry->d_inode, stat);
411 /* Change block size to what the server returned */
412 stat->blksize = st->st_blksize;
419 * v9fs_vfs_setattr_dotl - set file metadata
420 * @dentry: file whose metadata to set
421 * @iattr: metadata assignment structure
425 int v9fs_vfs_setattr_dotl(struct dentry *dentry, struct iattr *iattr)
428 struct v9fs_session_info *v9ses;
430 struct p9_iattr_dotl p9attr;
432 P9_DPRINTK(P9_DEBUG_VFS, "\n");
434 retval = inode_change_ok(dentry->d_inode, iattr);
438 p9attr.valid = iattr->ia_valid;
439 p9attr.mode = iattr->ia_mode;
440 p9attr.uid = iattr->ia_uid;
441 p9attr.gid = iattr->ia_gid;
442 p9attr.size = iattr->ia_size;
443 p9attr.atime_sec = iattr->ia_atime.tv_sec;
444 p9attr.atime_nsec = iattr->ia_atime.tv_nsec;
445 p9attr.mtime_sec = iattr->ia_mtime.tv_sec;
446 p9attr.mtime_nsec = iattr->ia_mtime.tv_nsec;
449 v9ses = v9fs_inode2v9ses(dentry->d_inode);
450 fid = v9fs_fid_lookup(dentry);
454 retval = p9_client_setattr(fid, &p9attr);
458 if ((iattr->ia_valid & ATTR_SIZE) &&
459 iattr->ia_size != i_size_read(dentry->d_inode)) {
460 retval = vmtruncate(dentry->d_inode, iattr->ia_size);
465 setattr_copy(dentry->d_inode, iattr);
466 mark_inode_dirty(dentry->d_inode);
467 if (iattr->ia_valid & ATTR_MODE) {
468 /* We also want to update ACL when we update mode bits */
469 retval = v9fs_acl_chmod(dentry);
477 * v9fs_stat2inode_dotl - populate an inode structure with stat info
478 * @stat: stat structure
479 * @inode: inode to populate
480 * @sb: superblock of filesystem
485 v9fs_stat2inode_dotl(struct p9_stat_dotl *stat, struct inode *inode)
488 if ((stat->st_result_mask & P9_STATS_BASIC) == P9_STATS_BASIC) {
489 inode->i_atime.tv_sec = stat->st_atime_sec;
490 inode->i_atime.tv_nsec = stat->st_atime_nsec;
491 inode->i_mtime.tv_sec = stat->st_mtime_sec;
492 inode->i_mtime.tv_nsec = stat->st_mtime_nsec;
493 inode->i_ctime.tv_sec = stat->st_ctime_sec;
494 inode->i_ctime.tv_nsec = stat->st_ctime_nsec;
495 inode->i_uid = stat->st_uid;
496 inode->i_gid = stat->st_gid;
497 inode->i_nlink = stat->st_nlink;
498 inode->i_mode = stat->st_mode;
499 inode->i_rdev = new_decode_dev(stat->st_rdev);
501 if ((S_ISBLK(inode->i_mode)) || (S_ISCHR(inode->i_mode)))
502 init_special_inode(inode, inode->i_mode, inode->i_rdev);
504 i_size_write(inode, stat->st_size);
505 inode->i_blocks = stat->st_blocks;
507 if (stat->st_result_mask & P9_STATS_ATIME) {
508 inode->i_atime.tv_sec = stat->st_atime_sec;
509 inode->i_atime.tv_nsec = stat->st_atime_nsec;
511 if (stat->st_result_mask & P9_STATS_MTIME) {
512 inode->i_mtime.tv_sec = stat->st_mtime_sec;
513 inode->i_mtime.tv_nsec = stat->st_mtime_nsec;
515 if (stat->st_result_mask & P9_STATS_CTIME) {
516 inode->i_ctime.tv_sec = stat->st_ctime_sec;
517 inode->i_ctime.tv_nsec = stat->st_ctime_nsec;
519 if (stat->st_result_mask & P9_STATS_UID)
520 inode->i_uid = stat->st_uid;
521 if (stat->st_result_mask & P9_STATS_GID)
522 inode->i_gid = stat->st_gid;
523 if (stat->st_result_mask & P9_STATS_NLINK)
524 inode->i_nlink = stat->st_nlink;
525 if (stat->st_result_mask & P9_STATS_MODE) {
526 inode->i_mode = stat->st_mode;
527 if ((S_ISBLK(inode->i_mode)) ||
528 (S_ISCHR(inode->i_mode)))
529 init_special_inode(inode, inode->i_mode,
532 if (stat->st_result_mask & P9_STATS_RDEV)
533 inode->i_rdev = new_decode_dev(stat->st_rdev);
534 if (stat->st_result_mask & P9_STATS_SIZE)
535 i_size_write(inode, stat->st_size);
536 if (stat->st_result_mask & P9_STATS_BLOCKS)
537 inode->i_blocks = stat->st_blocks;
539 if (stat->st_result_mask & P9_STATS_GEN)
540 inode->i_generation = stat->st_gen;
542 /* Currently we don't support P9_STATS_BTIME and P9_STATS_DATA_VERSION
543 * because the inode structure does not have fields for them.
548 v9fs_vfs_symlink_dotl(struct inode *dir, struct dentry *dentry,
551 struct v9fs_session_info *v9ses;
553 struct p9_fid *fid = NULL;
560 name = (char *) dentry->d_name.name;
561 P9_DPRINTK(P9_DEBUG_VFS, "v9fs_vfs_symlink_dotl : %lu,%s,%s\n",
562 dir->i_ino, name, symname);
563 v9ses = v9fs_inode2v9ses(dir);
565 dfid = v9fs_fid_lookup(dentry->d_parent);
568 P9_DPRINTK(P9_DEBUG_VFS, "fid lookup failed %d\n", err);
572 gid = v9fs_get_fsgid_for_create(dir);
574 /* Server doesn't alter fid on TSYMLINK. Hence no need to clone it. */
575 err = p9_client_symlink(dfid, name, (char *)symname, gid, &qid);
578 P9_DPRINTK(P9_DEBUG_VFS, "p9_client_symlink failed %d\n", err);
583 /* Now walk from the parent so we can get an unopened fid. */
584 fid = p9_client_walk(dfid, 1, &name, 1);
587 P9_DPRINTK(P9_DEBUG_VFS, "p9_client_walk failed %d\n",
593 /* instantiate inode and assign the unopened fid to dentry */
594 inode = v9fs_get_inode_from_fid(v9ses, fid, dir->i_sb);
596 err = PTR_ERR(inode);
597 P9_DPRINTK(P9_DEBUG_VFS, "inode creation failed %d\n",
601 d_instantiate(dentry, inode);
602 err = v9fs_fid_add(dentry, fid);
607 /* Not in cached mode. No need to populate inode with stat */
608 inode = v9fs_get_inode(dir->i_sb, S_IFLNK);
610 err = PTR_ERR(inode);
613 d_instantiate(dentry, inode);
618 p9_client_clunk(fid);
624 * v9fs_vfs_link_dotl - create a hardlink for dotl
625 * @old_dentry: dentry for file to link to
626 * @dir: inode destination for new link
627 * @dentry: dentry for link
632 v9fs_vfs_link_dotl(struct dentry *old_dentry, struct inode *dir,
633 struct dentry *dentry)
636 struct p9_fid *dfid, *oldfid;
638 struct v9fs_session_info *v9ses;
639 struct dentry *dir_dentry;
641 P9_DPRINTK(P9_DEBUG_VFS, "dir ino: %lu, old_name: %s, new_name: %s\n",
642 dir->i_ino, old_dentry->d_name.name,
643 dentry->d_name.name);
645 v9ses = v9fs_inode2v9ses(dir);
646 dir_dentry = v9fs_dentry_from_dir_inode(dir);
647 dfid = v9fs_fid_lookup(dir_dentry);
649 return PTR_ERR(dfid);
651 oldfid = v9fs_fid_lookup(old_dentry);
653 return PTR_ERR(oldfid);
655 name = (char *) dentry->d_name.name;
657 err = p9_client_link(dfid, oldfid, (char *)dentry->d_name.name);
660 P9_DPRINTK(P9_DEBUG_VFS, "p9_client_link failed %d\n", err);
664 if (v9ses->cache == CACHE_LOOSE || v9ses->cache == CACHE_FSCACHE) {
665 /* Get the latest stat info from server. */
667 struct p9_stat_dotl *st;
669 fid = v9fs_fid_lookup(old_dentry);
673 st = p9_client_getattr_dotl(fid, P9_STATS_BASIC);
677 v9fs_stat2inode_dotl(st, old_dentry->d_inode);
681 ihold(old_dentry->d_inode);
682 d_instantiate(dentry, old_dentry->d_inode);
688 * v9fs_vfs_mknod_dotl - create a special file
689 * @dir: inode destination for new link
690 * @dentry: dentry for file
691 * @mode: mode for creation
692 * @rdev: device associated with special file
696 v9fs_vfs_mknod_dotl(struct inode *dir, struct dentry *dentry, int omode,
702 struct v9fs_session_info *v9ses;
703 struct p9_fid *fid = NULL, *dfid = NULL;
707 struct dentry *dir_dentry;
708 struct posix_acl *dacl = NULL, *pacl = NULL;
710 P9_DPRINTK(P9_DEBUG_VFS,
711 " %lu,%s mode: %x MAJOR: %u MINOR: %u\n", dir->i_ino,
712 dentry->d_name.name, omode, MAJOR(rdev), MINOR(rdev));
714 if (!new_valid_dev(rdev))
717 v9ses = v9fs_inode2v9ses(dir);
718 dir_dentry = v9fs_dentry_from_dir_inode(dir);
719 dfid = v9fs_fid_lookup(dir_dentry);
722 P9_DPRINTK(P9_DEBUG_VFS, "fid lookup failed %d\n", err);
727 gid = v9fs_get_fsgid_for_create(dir);
729 /* Update mode based on ACL value */
730 err = v9fs_acl_mode(dir, &mode, &dacl, &pacl);
732 P9_DPRINTK(P9_DEBUG_VFS,
733 "Failed to get acl values in mknod %d\n", err);
736 name = (char *) dentry->d_name.name;
738 err = p9_client_mknod_dotl(dfid, name, mode, rdev, gid, &qid);
742 /* instantiate inode and assign the unopened fid to the dentry */
743 if (v9ses->cache == CACHE_LOOSE || v9ses->cache == CACHE_FSCACHE) {
744 fid = p9_client_walk(dfid, 1, &name, 1);
747 P9_DPRINTK(P9_DEBUG_VFS, "p9_client_walk failed %d\n",
753 inode = v9fs_get_inode_from_fid(v9ses, fid, dir->i_sb);
755 err = PTR_ERR(inode);
756 P9_DPRINTK(P9_DEBUG_VFS, "inode creation failed %d\n",
760 d_instantiate(dentry, inode);
761 err = v9fs_fid_add(dentry, fid);
767 * Not in cached mode. No need to populate inode with stat.
768 * socket syscall returns a fd, so we need instantiate
770 inode = v9fs_get_inode(dir->i_sb, mode);
772 err = PTR_ERR(inode);
775 d_instantiate(dentry, inode);
777 /* Now set the ACL based on the default value */
778 v9fs_set_create_acl(dentry, dacl, pacl);
781 p9_client_clunk(fid);
786 * v9fs_vfs_follow_link_dotl - follow a symlink path
787 * @dentry: dentry for symlink
793 v9fs_vfs_follow_link_dotl(struct dentry *dentry, struct nameidata *nd)
797 char *link = __getname();
800 P9_DPRINTK(P9_DEBUG_VFS, "%s\n", dentry->d_name.name);
803 link = ERR_PTR(-ENOMEM);
806 fid = v9fs_fid_lookup(dentry);
809 link = ERR_PTR(PTR_ERR(fid));
812 retval = p9_client_readlink(fid, &target);
814 strcpy(link, target);
819 link = ERR_PTR(retval);
821 nd_set_link(nd, link);
825 const struct inode_operations v9fs_dir_inode_operations_dotl = {
826 .create = v9fs_vfs_create_dotl,
827 .lookup = v9fs_vfs_lookup,
828 .link = v9fs_vfs_link_dotl,
829 .symlink = v9fs_vfs_symlink_dotl,
830 .unlink = v9fs_vfs_unlink,
831 .mkdir = v9fs_vfs_mkdir_dotl,
832 .rmdir = v9fs_vfs_rmdir,
833 .mknod = v9fs_vfs_mknod_dotl,
834 .rename = v9fs_vfs_rename,
835 .getattr = v9fs_vfs_getattr_dotl,
836 .setattr = v9fs_vfs_setattr_dotl,
837 .setxattr = generic_setxattr,
838 .getxattr = generic_getxattr,
839 .removexattr = generic_removexattr,
840 .listxattr = v9fs_listxattr,
841 .check_acl = v9fs_check_acl,
844 const struct inode_operations v9fs_file_inode_operations_dotl = {
845 .getattr = v9fs_vfs_getattr_dotl,
846 .setattr = v9fs_vfs_setattr_dotl,
847 .setxattr = generic_setxattr,
848 .getxattr = generic_getxattr,
849 .removexattr = generic_removexattr,
850 .listxattr = v9fs_listxattr,
851 .check_acl = v9fs_check_acl,
854 const struct inode_operations v9fs_symlink_inode_operations_dotl = {
855 .readlink = generic_readlink,
856 .follow_link = v9fs_vfs_follow_link_dotl,
857 .put_link = v9fs_vfs_put_link,
858 .getattr = v9fs_vfs_getattr_dotl,
859 .setattr = v9fs_vfs_setattr_dotl,
860 .setxattr = generic_setxattr,
861 .getxattr = generic_getxattr,
862 .removexattr = generic_removexattr,
863 .listxattr = v9fs_listxattr,