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)
166 struct v9fs_session_info *v9ses;
167 struct p9_fid *fid = NULL;
168 struct p9_fid *dfid, *ofid, *inode_fid;
172 struct posix_acl *pacl = NULL, *dacl = NULL;
174 v9ses = v9fs_inode2v9ses(dir);
175 if (nd && nd->flags & LOOKUP_OPEN)
176 flags = nd->intent.open.flags - 1;
179 * create call without LOOKUP_OPEN is due
180 * to mknod of regular files. So use mknod
183 return v9fs_vfs_mknod_dotl(dir, dentry, omode, 0);
186 name = (char *) dentry->d_name.name;
187 P9_DPRINTK(P9_DEBUG_VFS, "v9fs_vfs_create_dotl: name:%s flags:0x%x "
188 "mode:0x%x\n", name, flags, omode);
190 dfid = v9fs_fid_lookup(dentry->d_parent);
193 P9_DPRINTK(P9_DEBUG_VFS, "fid lookup failed %d\n", err);
197 /* clone a fid to use for creation */
198 ofid = p9_client_walk(dfid, 0, NULL, 1);
201 P9_DPRINTK(P9_DEBUG_VFS, "p9_client_walk failed %d\n", err);
205 gid = v9fs_get_fsgid_for_create(dir);
208 /* Update mode based on ACL value */
209 err = v9fs_acl_mode(dir, &mode, &dacl, &pacl);
211 P9_DPRINTK(P9_DEBUG_VFS,
212 "Failed to get acl values in creat %d\n", err);
215 err = p9_client_create_dotl(ofid, name, flags, mode, gid, &qid);
217 P9_DPRINTK(P9_DEBUG_VFS,
218 "p9_client_open_dotl failed in creat %d\n",
223 /* instantiate inode and assign the unopened fid to the dentry */
224 fid = p9_client_walk(dfid, 1, &name, 1);
227 P9_DPRINTK(P9_DEBUG_VFS, "p9_client_walk failed %d\n", err);
231 inode = v9fs_get_inode_from_fid(v9ses, fid, dir->i_sb);
233 err = PTR_ERR(inode);
234 P9_DPRINTK(P9_DEBUG_VFS, "inode creation failed %d\n", err);
237 d_instantiate(dentry, inode);
238 err = v9fs_fid_add(dentry, fid);
242 /* Now set the ACL based on the default value */
243 v9fs_set_create_acl(dentry, dacl, pacl);
244 if (v9ses->cache && !inode->i_private) {
246 * clone a fid and add it to inode->i_private
247 * we do it during open time instead of
248 * page dirty time via write_begin/page_mkwrite
249 * because we want write after unlink usecase
252 inode_fid = v9fs_writeback_fid(dentry);
253 if (IS_ERR(inode_fid)) {
254 err = PTR_ERR(inode_fid);
257 inode->i_private = (void *) inode_fid;
259 /* Since we are opening a file, assign the open fid to the file */
260 filp = lookup_instantiate_filp(nd, dentry, generic_file_open);
262 p9_client_clunk(ofid);
263 return PTR_ERR(filp);
265 filp->private_data = ofid;
266 #ifdef CONFIG_9P_FSCACHE
268 v9fs_cache_inode_set_cookie(inode, filp);
274 p9_client_clunk(ofid);
276 p9_client_clunk(fid);
281 * v9fs_vfs_mkdir_dotl - VFS mkdir hook to create a directory
282 * @dir: inode that is being unlinked
283 * @dentry: dentry that is being unlinked
284 * @mode: mode for new directory
288 static int v9fs_vfs_mkdir_dotl(struct inode *dir,
289 struct dentry *dentry, int omode)
292 struct v9fs_session_info *v9ses;
293 struct p9_fid *fid = NULL, *dfid = NULL;
299 struct dentry *dir_dentry;
300 struct posix_acl *dacl = NULL, *pacl = NULL;
302 P9_DPRINTK(P9_DEBUG_VFS, "name %s\n", dentry->d_name.name);
304 v9ses = v9fs_inode2v9ses(dir);
307 if (dir->i_mode & S_ISGID)
310 dir_dentry = v9fs_dentry_from_dir_inode(dir);
311 dfid = v9fs_fid_lookup(dir_dentry);
314 P9_DPRINTK(P9_DEBUG_VFS, "fid lookup failed %d\n", err);
319 gid = v9fs_get_fsgid_for_create(dir);
321 /* Update mode based on ACL value */
322 err = v9fs_acl_mode(dir, &mode, &dacl, &pacl);
324 P9_DPRINTK(P9_DEBUG_VFS,
325 "Failed to get acl values in mkdir %d\n", err);
328 name = (char *) dentry->d_name.name;
329 err = p9_client_mkdir_dotl(dfid, name, mode, gid, &qid);
333 /* instantiate inode and assign the unopened fid to the dentry */
334 if (v9ses->cache == CACHE_LOOSE || v9ses->cache == CACHE_FSCACHE) {
335 fid = p9_client_walk(dfid, 1, &name, 1);
338 P9_DPRINTK(P9_DEBUG_VFS, "p9_client_walk failed %d\n",
344 inode = v9fs_get_inode_from_fid(v9ses, fid, dir->i_sb);
346 err = PTR_ERR(inode);
347 P9_DPRINTK(P9_DEBUG_VFS, "inode creation failed %d\n",
351 d_instantiate(dentry, inode);
352 err = v9fs_fid_add(dentry, fid);
358 * Not in cached mode. No need to populate
359 * inode with stat. We need to get an inode
360 * so that we can set the acl with dentry
362 inode = v9fs_get_inode(dir->i_sb, mode);
364 err = PTR_ERR(inode);
367 d_instantiate(dentry, inode);
369 /* Now set the ACL based on the default value */
370 v9fs_set_create_acl(dentry, dacl, pacl);
374 p9_client_clunk(fid);
379 v9fs_vfs_getattr_dotl(struct vfsmount *mnt, struct dentry *dentry,
383 struct v9fs_session_info *v9ses;
385 struct p9_stat_dotl *st;
387 P9_DPRINTK(P9_DEBUG_VFS, "dentry: %p\n", dentry);
389 v9ses = v9fs_inode2v9ses(dentry->d_inode);
390 if (v9ses->cache == CACHE_LOOSE || v9ses->cache == CACHE_FSCACHE) {
391 generic_fillattr(dentry->d_inode, stat);
394 fid = v9fs_fid_lookup(dentry);
398 /* Ask for all the fields in stat structure. Server will return
399 * whatever it supports
402 st = p9_client_getattr_dotl(fid, P9_STATS_ALL);
406 v9fs_stat2inode_dotl(st, dentry->d_inode);
407 generic_fillattr(dentry->d_inode, stat);
408 /* Change block size to what the server returned */
409 stat->blksize = st->st_blksize;
416 * v9fs_vfs_setattr_dotl - set file metadata
417 * @dentry: file whose metadata to set
418 * @iattr: metadata assignment structure
422 int v9fs_vfs_setattr_dotl(struct dentry *dentry, struct iattr *iattr)
425 struct v9fs_session_info *v9ses;
427 struct p9_iattr_dotl p9attr;
429 P9_DPRINTK(P9_DEBUG_VFS, "\n");
431 retval = inode_change_ok(dentry->d_inode, iattr);
435 p9attr.valid = iattr->ia_valid;
436 p9attr.mode = iattr->ia_mode;
437 p9attr.uid = iattr->ia_uid;
438 p9attr.gid = iattr->ia_gid;
439 p9attr.size = iattr->ia_size;
440 p9attr.atime_sec = iattr->ia_atime.tv_sec;
441 p9attr.atime_nsec = iattr->ia_atime.tv_nsec;
442 p9attr.mtime_sec = iattr->ia_mtime.tv_sec;
443 p9attr.mtime_nsec = iattr->ia_mtime.tv_nsec;
446 v9ses = v9fs_inode2v9ses(dentry->d_inode);
447 fid = v9fs_fid_lookup(dentry);
451 retval = p9_client_setattr(fid, &p9attr);
455 if ((iattr->ia_valid & ATTR_SIZE) &&
456 iattr->ia_size != i_size_read(dentry->d_inode)) {
457 retval = vmtruncate(dentry->d_inode, iattr->ia_size);
462 setattr_copy(dentry->d_inode, iattr);
463 mark_inode_dirty(dentry->d_inode);
464 if (iattr->ia_valid & ATTR_MODE) {
465 /* We also want to update ACL when we update mode bits */
466 retval = v9fs_acl_chmod(dentry);
474 * v9fs_stat2inode_dotl - populate an inode structure with stat info
475 * @stat: stat structure
476 * @inode: inode to populate
477 * @sb: superblock of filesystem
482 v9fs_stat2inode_dotl(struct p9_stat_dotl *stat, struct inode *inode)
485 if ((stat->st_result_mask & P9_STATS_BASIC) == P9_STATS_BASIC) {
486 inode->i_atime.tv_sec = stat->st_atime_sec;
487 inode->i_atime.tv_nsec = stat->st_atime_nsec;
488 inode->i_mtime.tv_sec = stat->st_mtime_sec;
489 inode->i_mtime.tv_nsec = stat->st_mtime_nsec;
490 inode->i_ctime.tv_sec = stat->st_ctime_sec;
491 inode->i_ctime.tv_nsec = stat->st_ctime_nsec;
492 inode->i_uid = stat->st_uid;
493 inode->i_gid = stat->st_gid;
494 inode->i_nlink = stat->st_nlink;
495 inode->i_mode = stat->st_mode;
496 inode->i_rdev = new_decode_dev(stat->st_rdev);
498 if ((S_ISBLK(inode->i_mode)) || (S_ISCHR(inode->i_mode)))
499 init_special_inode(inode, inode->i_mode, inode->i_rdev);
501 i_size_write(inode, stat->st_size);
502 inode->i_blocks = stat->st_blocks;
504 if (stat->st_result_mask & P9_STATS_ATIME) {
505 inode->i_atime.tv_sec = stat->st_atime_sec;
506 inode->i_atime.tv_nsec = stat->st_atime_nsec;
508 if (stat->st_result_mask & P9_STATS_MTIME) {
509 inode->i_mtime.tv_sec = stat->st_mtime_sec;
510 inode->i_mtime.tv_nsec = stat->st_mtime_nsec;
512 if (stat->st_result_mask & P9_STATS_CTIME) {
513 inode->i_ctime.tv_sec = stat->st_ctime_sec;
514 inode->i_ctime.tv_nsec = stat->st_ctime_nsec;
516 if (stat->st_result_mask & P9_STATS_UID)
517 inode->i_uid = stat->st_uid;
518 if (stat->st_result_mask & P9_STATS_GID)
519 inode->i_gid = stat->st_gid;
520 if (stat->st_result_mask & P9_STATS_NLINK)
521 inode->i_nlink = stat->st_nlink;
522 if (stat->st_result_mask & P9_STATS_MODE) {
523 inode->i_mode = stat->st_mode;
524 if ((S_ISBLK(inode->i_mode)) ||
525 (S_ISCHR(inode->i_mode)))
526 init_special_inode(inode, inode->i_mode,
529 if (stat->st_result_mask & P9_STATS_RDEV)
530 inode->i_rdev = new_decode_dev(stat->st_rdev);
531 if (stat->st_result_mask & P9_STATS_SIZE)
532 i_size_write(inode, stat->st_size);
533 if (stat->st_result_mask & P9_STATS_BLOCKS)
534 inode->i_blocks = stat->st_blocks;
536 if (stat->st_result_mask & P9_STATS_GEN)
537 inode->i_generation = stat->st_gen;
539 /* Currently we don't support P9_STATS_BTIME and P9_STATS_DATA_VERSION
540 * because the inode structure does not have fields for them.
545 v9fs_vfs_symlink_dotl(struct inode *dir, struct dentry *dentry,
548 struct v9fs_session_info *v9ses;
550 struct p9_fid *fid = NULL;
557 name = (char *) dentry->d_name.name;
558 P9_DPRINTK(P9_DEBUG_VFS, "v9fs_vfs_symlink_dotl : %lu,%s,%s\n",
559 dir->i_ino, name, symname);
560 v9ses = v9fs_inode2v9ses(dir);
562 dfid = v9fs_fid_lookup(dentry->d_parent);
565 P9_DPRINTK(P9_DEBUG_VFS, "fid lookup failed %d\n", err);
569 gid = v9fs_get_fsgid_for_create(dir);
571 /* Server doesn't alter fid on TSYMLINK. Hence no need to clone it. */
572 err = p9_client_symlink(dfid, name, (char *)symname, gid, &qid);
575 P9_DPRINTK(P9_DEBUG_VFS, "p9_client_symlink failed %d\n", err);
580 /* Now walk from the parent so we can get an unopened fid. */
581 fid = p9_client_walk(dfid, 1, &name, 1);
584 P9_DPRINTK(P9_DEBUG_VFS, "p9_client_walk failed %d\n",
590 /* instantiate inode and assign the unopened fid to dentry */
591 inode = v9fs_get_inode_from_fid(v9ses, fid, dir->i_sb);
593 err = PTR_ERR(inode);
594 P9_DPRINTK(P9_DEBUG_VFS, "inode creation failed %d\n",
598 d_instantiate(dentry, inode);
599 err = v9fs_fid_add(dentry, fid);
604 /* Not in cached mode. No need to populate inode with stat */
605 inode = v9fs_get_inode(dir->i_sb, S_IFLNK);
607 err = PTR_ERR(inode);
610 d_instantiate(dentry, inode);
615 p9_client_clunk(fid);
621 * v9fs_vfs_link_dotl - create a hardlink for dotl
622 * @old_dentry: dentry for file to link to
623 * @dir: inode destination for new link
624 * @dentry: dentry for link
629 v9fs_vfs_link_dotl(struct dentry *old_dentry, struct inode *dir,
630 struct dentry *dentry)
633 struct p9_fid *dfid, *oldfid;
635 struct v9fs_session_info *v9ses;
636 struct dentry *dir_dentry;
638 P9_DPRINTK(P9_DEBUG_VFS, "dir ino: %lu, old_name: %s, new_name: %s\n",
639 dir->i_ino, old_dentry->d_name.name,
640 dentry->d_name.name);
642 v9ses = v9fs_inode2v9ses(dir);
643 dir_dentry = v9fs_dentry_from_dir_inode(dir);
644 dfid = v9fs_fid_lookup(dir_dentry);
646 return PTR_ERR(dfid);
648 oldfid = v9fs_fid_lookup(old_dentry);
650 return PTR_ERR(oldfid);
652 name = (char *) dentry->d_name.name;
654 err = p9_client_link(dfid, oldfid, (char *)dentry->d_name.name);
657 P9_DPRINTK(P9_DEBUG_VFS, "p9_client_link failed %d\n", err);
661 if (v9ses->cache == CACHE_LOOSE || v9ses->cache == CACHE_FSCACHE) {
662 /* Get the latest stat info from server. */
664 struct p9_stat_dotl *st;
666 fid = v9fs_fid_lookup(old_dentry);
670 st = p9_client_getattr_dotl(fid, P9_STATS_BASIC);
674 v9fs_stat2inode_dotl(st, old_dentry->d_inode);
678 ihold(old_dentry->d_inode);
679 d_instantiate(dentry, old_dentry->d_inode);
685 * v9fs_vfs_mknod_dotl - create a special file
686 * @dir: inode destination for new link
687 * @dentry: dentry for file
688 * @mode: mode for creation
689 * @rdev: device associated with special file
693 v9fs_vfs_mknod_dotl(struct inode *dir, struct dentry *dentry, int omode,
699 struct v9fs_session_info *v9ses;
700 struct p9_fid *fid = NULL, *dfid = NULL;
704 struct dentry *dir_dentry;
705 struct posix_acl *dacl = NULL, *pacl = NULL;
707 P9_DPRINTK(P9_DEBUG_VFS,
708 " %lu,%s mode: %x MAJOR: %u MINOR: %u\n", dir->i_ino,
709 dentry->d_name.name, omode, MAJOR(rdev), MINOR(rdev));
711 if (!new_valid_dev(rdev))
714 v9ses = v9fs_inode2v9ses(dir);
715 dir_dentry = v9fs_dentry_from_dir_inode(dir);
716 dfid = v9fs_fid_lookup(dir_dentry);
719 P9_DPRINTK(P9_DEBUG_VFS, "fid lookup failed %d\n", err);
724 gid = v9fs_get_fsgid_for_create(dir);
726 /* Update mode based on ACL value */
727 err = v9fs_acl_mode(dir, &mode, &dacl, &pacl);
729 P9_DPRINTK(P9_DEBUG_VFS,
730 "Failed to get acl values in mknod %d\n", err);
733 name = (char *) dentry->d_name.name;
735 err = p9_client_mknod_dotl(dfid, name, mode, rdev, gid, &qid);
739 /* instantiate inode and assign the unopened fid to the dentry */
740 if (v9ses->cache == CACHE_LOOSE || v9ses->cache == CACHE_FSCACHE) {
741 fid = p9_client_walk(dfid, 1, &name, 1);
744 P9_DPRINTK(P9_DEBUG_VFS, "p9_client_walk failed %d\n",
750 inode = v9fs_get_inode_from_fid(v9ses, fid, dir->i_sb);
752 err = PTR_ERR(inode);
753 P9_DPRINTK(P9_DEBUG_VFS, "inode creation failed %d\n",
757 d_instantiate(dentry, inode);
758 err = v9fs_fid_add(dentry, fid);
764 * Not in cached mode. No need to populate inode with stat.
765 * socket syscall returns a fd, so we need instantiate
767 inode = v9fs_get_inode(dir->i_sb, mode);
769 err = PTR_ERR(inode);
772 d_instantiate(dentry, inode);
774 /* Now set the ACL based on the default value */
775 v9fs_set_create_acl(dentry, dacl, pacl);
778 p9_client_clunk(fid);
783 * v9fs_vfs_follow_link_dotl - follow a symlink path
784 * @dentry: dentry for symlink
790 v9fs_vfs_follow_link_dotl(struct dentry *dentry, struct nameidata *nd)
794 char *link = __getname();
797 P9_DPRINTK(P9_DEBUG_VFS, "%s\n", dentry->d_name.name);
800 link = ERR_PTR(-ENOMEM);
803 fid = v9fs_fid_lookup(dentry);
806 link = ERR_PTR(PTR_ERR(fid));
809 retval = p9_client_readlink(fid, &target);
811 strcpy(link, target);
816 link = ERR_PTR(retval);
818 nd_set_link(nd, link);
822 const struct inode_operations v9fs_dir_inode_operations_dotl = {
823 .create = v9fs_vfs_create_dotl,
824 .lookup = v9fs_vfs_lookup,
825 .link = v9fs_vfs_link_dotl,
826 .symlink = v9fs_vfs_symlink_dotl,
827 .unlink = v9fs_vfs_unlink,
828 .mkdir = v9fs_vfs_mkdir_dotl,
829 .rmdir = v9fs_vfs_rmdir,
830 .mknod = v9fs_vfs_mknod_dotl,
831 .rename = v9fs_vfs_rename,
832 .getattr = v9fs_vfs_getattr_dotl,
833 .setattr = v9fs_vfs_setattr_dotl,
834 .setxattr = generic_setxattr,
835 .getxattr = generic_getxattr,
836 .removexattr = generic_removexattr,
837 .listxattr = v9fs_listxattr,
838 .check_acl = v9fs_check_acl,
841 const struct inode_operations v9fs_file_inode_operations_dotl = {
842 .getattr = v9fs_vfs_getattr_dotl,
843 .setattr = v9fs_vfs_setattr_dotl,
844 .setxattr = generic_setxattr,
845 .getxattr = generic_getxattr,
846 .removexattr = generic_removexattr,
847 .listxattr = v9fs_listxattr,
848 .check_acl = v9fs_check_acl,
851 const struct inode_operations v9fs_symlink_inode_operations_dotl = {
852 .readlink = generic_readlink,
853 .follow_link = v9fs_vfs_follow_link_dotl,
854 .put_link = v9fs_vfs_put_link,
855 .getattr = v9fs_vfs_getattr_dotl,
856 .setattr = v9fs_vfs_setattr_dotl,
857 .setxattr = generic_setxattr,
858 .getxattr = generic_getxattr,
859 .removexattr = generic_removexattr,
860 .listxattr = v9fs_listxattr,