2 * linux/fs/9p/vfs_inode.c
4 * This file contians vfs inode ops for the 9P2000 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 as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to:
21 * Free Software Foundation
22 * 51 Franklin Street, Fifth Floor
23 * Boston, MA 02111-1301 USA
27 #include <linux/module.h>
28 #include <linux/errno.h>
30 #include <linux/file.h>
31 #include <linux/pagemap.h>
32 #include <linux/stat.h>
33 #include <linux/string.h>
34 #include <linux/smp_lock.h>
35 #include <linux/inet.h>
36 #include <linux/namei.h>
37 #include <linux/idr.h>
46 static struct inode_operations v9fs_dir_inode_operations;
47 static struct inode_operations v9fs_file_inode_operations;
48 static struct inode_operations v9fs_symlink_inode_operations;
51 * unixmode2p9mode - convert unix mode bits to plan 9
52 * @v9ses: v9fs session information
53 * @mode: mode to convert
57 static inline int unixmode2p9mode(struct v9fs_session_info *v9ses, int mode)
63 if (v9ses->extended) {
65 res |= V9FS_DMSYMLINK;
66 if (v9ses->nodev == 0) {
70 res |= V9FS_DMNAMEDPIPE;
77 if ((mode & S_ISUID) == S_ISUID)
79 if ((mode & S_ISGID) == S_ISGID)
81 if ((mode & V9FS_DMLINK))
89 * p9mode2unixmode- convert plan9 mode bits to unix mode bits
90 * @v9ses: v9fs session information
91 * @mode: mode to convert
95 static inline int p9mode2unixmode(struct v9fs_session_info *v9ses, int mode)
101 if ((mode & V9FS_DMDIR) == V9FS_DMDIR)
103 else if ((mode & V9FS_DMSYMLINK) && (v9ses->extended))
105 else if ((mode & V9FS_DMSOCKET) && (v9ses->extended)
106 && (v9ses->nodev == 0))
108 else if ((mode & V9FS_DMNAMEDPIPE) && (v9ses->extended)
109 && (v9ses->nodev == 0))
111 else if ((mode & V9FS_DMDEVICE) && (v9ses->extended)
112 && (v9ses->nodev == 0))
117 if (v9ses->extended) {
118 if ((mode & V9FS_DMSETUID) == V9FS_DMSETUID)
121 if ((mode & V9FS_DMSETGID) == V9FS_DMSETGID)
129 * v9fs_blank_mistat - helper function to setup a 9P stat structure
130 * @v9ses: 9P session info (for determining extended mode)
131 * @mistat: structure to initialize
136 v9fs_blank_mistat(struct v9fs_session_info *v9ses, struct v9fs_stat *mistat)
140 mistat->qid.type = ~0;
141 mistat->qid.version = ~0;
142 *((long long *)&mistat->qid.path) = ~0;
147 mistat->name = mistat->data;
148 mistat->uid = mistat->data;
149 mistat->gid = mistat->data;
150 mistat->muid = mistat->data;
151 if (v9ses->extended) {
155 mistat->extension = mistat->data;
161 * v9fs_mistat2unix - convert mistat to unix stat
162 * @mistat: Plan 9 metadata (mistat) structure
163 * @stat: unix metadata (stat) structure to populate
169 v9fs_mistat2unix(struct v9fs_stat *mistat, struct stat *buf,
170 struct super_block *sb)
172 struct v9fs_session_info *v9ses = sb ? sb->s_fs_info : NULL;
176 buf->st_atime = mistat->atime;
177 buf->st_mtime = mistat->mtime;
178 buf->st_ctime = mistat->mtime;
180 if (v9ses && v9ses->extended) {
181 /* TODO: string to uid mapping via user-space daemon */
182 buf->st_uid = mistat->n_uid;
183 buf->st_gid = mistat->n_gid;
185 sscanf(mistat->uid, "%x", (unsigned int *)&buf->st_uid);
186 sscanf(mistat->gid, "%x", (unsigned int *)&buf->st_gid);
188 buf->st_uid = v9ses->uid;
189 buf->st_gid = v9ses->gid;
192 buf->st_uid = (unsigned short)-1;
193 buf->st_gid = (unsigned short)-1;
195 if (v9ses && v9ses->extended) {
196 if (mistat->n_uid != -1)
197 sscanf(mistat->uid, "%x", (unsigned int *)&buf->st_uid);
199 if (mistat->n_gid != -1)
200 sscanf(mistat->gid, "%x", (unsigned int *)&buf->st_gid);
203 if (buf->st_uid == (unsigned short)-1)
204 buf->st_uid = v9ses->uid;
205 if (buf->st_gid == (unsigned short)-1)
206 buf->st_gid = v9ses->gid;
208 buf->st_mode = p9mode2unixmode(v9ses, mistat->mode);
209 if ((S_ISBLK(buf->st_mode)) || (S_ISCHR(buf->st_mode))) {
213 sscanf(mistat->extension, "%c %u %u", &type, &major, &minor);
216 buf->st_mode &= ~S_IFBLK;
217 buf->st_mode |= S_IFCHR;
222 dprintk(DEBUG_ERROR, "Unknown special type %c (%s)\n",
223 type, mistat->extension);
225 buf->st_rdev = MKDEV(major, minor);
229 buf->st_size = mistat->length;
231 buf->st_blksize = sb->s_blocksize;
233 (buf->st_size + buf->st_blksize - 1) >> sb->s_blocksize_bits;
237 * v9fs_get_inode - helper function to setup an inode
239 * @mode: mode to setup inode with
243 struct inode *v9fs_get_inode(struct super_block *sb, int mode)
245 struct inode *inode = NULL;
247 dprintk(DEBUG_VFS, "super block: %p mode: %o\n", sb, mode);
249 inode = new_inode(sb);
251 inode->i_mode = mode;
252 inode->i_uid = current->fsuid;
253 inode->i_gid = current->fsgid;
254 inode->i_blksize = sb->s_blocksize;
257 inode->i_atime = inode->i_mtime = inode->i_ctime = CURRENT_TIME;
259 switch (mode & S_IFMT) {
265 inode->i_op = &v9fs_file_inode_operations;
266 inode->i_fop = &v9fs_file_operations;
270 inode->i_op = &v9fs_dir_inode_operations;
271 inode->i_fop = &v9fs_dir_operations;
274 inode->i_op = &v9fs_symlink_inode_operations;
277 dprintk(DEBUG_ERROR, "BAD mode 0x%x S_IFMT 0x%x\n",
278 mode, mode & S_IFMT);
279 return ERR_PTR(-EINVAL);
282 eprintk(KERN_WARNING, "Problem allocating inode\n");
283 return ERR_PTR(-ENOMEM);
289 * v9fs_create - helper function to create files and directories
290 * @dir: directory inode file is being created in
291 * @file_dentry: dentry file is being created in
292 * @perm: permissions file is being created with
293 * @open_mode: resulting open mode for file ???
298 v9fs_create(struct inode *dir,
299 struct dentry *file_dentry,
300 unsigned int perm, unsigned int open_mode)
302 struct v9fs_session_info *v9ses = v9fs_inode2v9ses(dir);
303 struct super_block *sb = dir->i_sb;
304 struct v9fs_fid *dirfid =
305 v9fs_fid_lookup(file_dentry->d_parent, FID_WALK);
306 struct v9fs_fid *fid = NULL;
307 struct inode *file_inode = NULL;
308 struct v9fs_fcall *fcall = NULL;
314 unsigned int iounit = 0;
316 perm = unixmode2p9mode(v9ses, perm);
318 dprintk(DEBUG_VFS, "dir: %p dentry: %p perm: %o mode: %o\n", dir,
319 file_dentry, perm, open_mode);
324 dirfidnum = dirfid->fid;
326 dprintk(DEBUG_ERROR, "No fid for the directory #%lu\n",
331 if (file_dentry->d_inode) {
333 "Odd. There is an inode for dir %lu, name :%s:\n",
334 dir->i_ino, file_dentry->d_name.name);
338 newfid = v9fs_get_idpool(&v9ses->fidpool);
340 eprintk(KERN_WARNING, "no free fids available\n");
344 result = v9fs_t_walk(v9ses, dirfidnum, newfid, NULL, &fcall);
346 dprintk(DEBUG_ERROR, "clone error: %s\n", FCALL_ERROR(fcall));
347 v9fs_put_idpool(newfid, &v9ses->fidpool);
354 result = v9fs_t_create(v9ses, newfid, (char *)file_dentry->d_name.name,
355 perm, open_mode, &fcall);
357 dprintk(DEBUG_ERROR, "create fails: %s(%d)\n",
358 FCALL_ERROR(fcall), result);
363 iounit = fcall->params.rcreate.iounit;
364 qid = fcall->params.rcreate.qid;
367 fid = v9fs_fid_create(file_dentry);
377 fid->iounit = iounit;
379 fid->rdir_fcall = NULL;
382 if ((perm & V9FS_DMSYMLINK) || (perm & V9FS_DMLINK) ||
383 (perm & V9FS_DMNAMEDPIPE) || (perm & V9FS_DMSOCKET) ||
384 (perm & V9FS_DMDEVICE))
387 result = v9fs_t_stat(v9ses, newfid, &fcall);
389 dprintk(DEBUG_ERROR, "stat error: %s(%d)\n", FCALL_ERROR(fcall),
394 v9fs_mistat2unix(fcall->params.rstat.stat, &newstat, sb);
396 file_inode = v9fs_get_inode(sb, newstat.st_mode);
397 if ((!file_inode) || IS_ERR(file_inode)) {
398 dprintk(DEBUG_ERROR, "create inode failed\n");
403 v9fs_mistat2inode(fcall->params.rstat.stat, file_inode, sb);
405 d_instantiate(file_dentry, file_inode);
407 if (perm & V9FS_DMDIR) {
408 if (v9fs_t_clunk(v9ses, newfid, &fcall))
409 dprintk(DEBUG_ERROR, "clunk for mkdir failed: %s\n",
412 v9fs_put_idpool(newfid, &v9ses->fidpool);
425 if (v9fs_t_clunk(v9ses, newfid, &fcall))
426 dprintk(DEBUG_ERROR, "clunk failed: %s\n",
429 v9fs_put_idpool(newfid, &v9ses->fidpool);
436 * v9fs_remove - helper function to remove files and directories
437 * @inode: directory inode that is being deleted
438 * @dentry: dentry that is being deleted
439 * @rmdir: where we are a file or a directory
443 static int v9fs_remove(struct inode *dir, struct dentry *file, int rmdir)
445 struct v9fs_fcall *fcall = NULL;
446 struct super_block *sb = NULL;
447 struct v9fs_session_info *v9ses = NULL;
448 struct v9fs_fid *v9fid = NULL;
449 struct inode *file_inode = NULL;
453 dprintk(DEBUG_VFS, "inode: %p dentry: %p rmdir: %d\n", dir, file,
456 file_inode = file->d_inode;
457 sb = file_inode->i_sb;
458 v9ses = v9fs_inode2v9ses(file_inode);
459 v9fid = v9fs_fid_lookup(file, FID_OP);
469 dprintk(DEBUG_ERROR, "inode #%lu, no fid!\n",
474 result = v9fs_t_remove(v9ses, fid, &fcall);
476 dprintk(DEBUG_ERROR, "remove of file fails: %s(%d)\n",
477 FCALL_ERROR(fcall), result);
479 v9fs_put_idpool(fid, &v9ses->fidpool);
480 v9fs_fid_destroy(v9fid);
488 * v9fs_vfs_create - VFS hook to create files
489 * @inode: directory inode that is being deleted
490 * @dentry: dentry that is being deleted
491 * @perm: create permissions
492 * @nd: path information
497 v9fs_vfs_create(struct inode *inode, struct dentry *dentry, int perm,
498 struct nameidata *nd)
500 return v9fs_create(inode, dentry, perm, O_RDWR);
504 * v9fs_vfs_mkdir - VFS mkdir hook to create a directory
505 * @i: inode that is being unlinked
506 * @dentry: dentry that is being unlinked
507 * @mode: mode for new directory
511 static int v9fs_vfs_mkdir(struct inode *inode, struct dentry *dentry, int mode)
513 return v9fs_create(inode, dentry, mode | S_IFDIR, O_RDONLY);
517 * v9fs_vfs_lookup - VFS lookup hook to "walk" to a new inode
518 * @dir: inode that is being walked from
519 * @dentry: dentry that is being walked to?
520 * @nameidata: path data
524 static struct dentry *v9fs_vfs_lookup(struct inode *dir, struct dentry *dentry,
525 struct nameidata *nameidata)
527 struct super_block *sb;
528 struct v9fs_session_info *v9ses;
529 struct v9fs_fid *dirfid;
530 struct v9fs_fid *fid;
532 struct v9fs_fcall *fcall = NULL;
538 dprintk(DEBUG_VFS, "dir: %p dentry: (%s) %p nameidata: %p\n",
539 dir, dentry->d_iname, dentry, nameidata);
542 v9ses = v9fs_inode2v9ses(dir);
543 dirfid = v9fs_fid_lookup(dentry->d_parent, FID_WALK);
546 dprintk(DEBUG_ERROR, "no dirfid\n");
547 return ERR_PTR(-EINVAL);
550 dirfidnum = dirfid->fid;
553 dprintk(DEBUG_ERROR, "no dirfid for inode %p, #%lu\n",
555 return ERR_PTR(-EBADF);
558 newfid = v9fs_get_idpool(&v9ses->fidpool);
560 eprintk(KERN_WARNING, "newfid fails!\n");
561 return ERR_PTR(-ENOSPC);
565 v9fs_t_walk(v9ses, dirfidnum, newfid, (char *)dentry->d_name.name,
568 v9fs_put_idpool(newfid, &v9ses->fidpool);
569 if (result == -ENOENT) {
572 "Return negative dentry %p count %d\n",
573 dentry, atomic_read(&dentry->d_count));
576 dprintk(DEBUG_ERROR, "walk error:%d\n", result);
580 result = v9fs_t_stat(v9ses, newfid, &fcall);
582 dprintk(DEBUG_ERROR, "stat error\n");
586 v9fs_mistat2unix(fcall->params.rstat.stat, &newstat, sb);
587 inode = v9fs_get_inode(sb, newstat.st_mode);
589 if (IS_ERR(inode) && (PTR_ERR(inode) == -ENOSPC)) {
590 eprintk(KERN_WARNING, "inode alloc failes, returns %ld\n",
597 inode->i_ino = v9fs_qid2ino(&fcall->params.rstat.stat->qid);
599 fid = v9fs_fid_create(dentry);
601 dprintk(DEBUG_ERROR, "couldn't insert\n");
609 fid->qid = fcall->params.rstat.stat->qid;
611 dentry->d_op = &v9fs_dentry_operations;
612 v9fs_mistat2inode(fcall->params.rstat.stat, inode, inode->i_sb);
614 d_add(dentry, inode);
621 return ERR_PTR(result);
625 * v9fs_vfs_unlink - VFS unlink hook to delete an inode
626 * @i: inode that is being unlinked
627 * @dentry: dentry that is being unlinked
631 static int v9fs_vfs_unlink(struct inode *i, struct dentry *d)
633 return v9fs_remove(i, d, 0);
637 * v9fs_vfs_rmdir - VFS unlink hook to delete a directory
638 * @i: inode that is being unlinked
639 * @dentry: dentry that is being unlinked
643 static int v9fs_vfs_rmdir(struct inode *i, struct dentry *d)
645 return v9fs_remove(i, d, 1);
649 * v9fs_vfs_rename - VFS hook to rename an inode
650 * @old_dir: old dir inode
651 * @old_dentry: old dentry
652 * @new_dir: new dir inode
653 * @new_dentry: new dentry
658 v9fs_vfs_rename(struct inode *old_dir, struct dentry *old_dentry,
659 struct inode *new_dir, struct dentry *new_dentry)
661 struct inode *old_inode = old_dentry->d_inode;
662 struct v9fs_session_info *v9ses = v9fs_inode2v9ses(old_inode);
663 struct v9fs_fid *oldfid = v9fs_fid_lookup(old_dentry, FID_WALK);
664 struct v9fs_fid *olddirfid =
665 v9fs_fid_lookup(old_dentry->d_parent, FID_WALK);
666 struct v9fs_fid *newdirfid =
667 v9fs_fid_lookup(new_dentry->d_parent, FID_WALK);
668 struct v9fs_stat *mistat = kmalloc(v9ses->maxdata, GFP_KERNEL);
669 struct v9fs_fcall *fcall = NULL;
671 int olddirfidnum = -1;
672 int newdirfidnum = -1;
675 dprintk(DEBUG_VFS, "\n");
677 if ((!oldfid) || (!olddirfid) || (!newdirfid)) {
678 dprintk(DEBUG_ERROR, "problem with arguments\n");
682 /* 9P can only handle file rename in the same directory */
683 if (memcmp(&olddirfid->qid, &newdirfid->qid, sizeof(newdirfid->qid))) {
684 dprintk(DEBUG_ERROR, "old dir and new dir are different\n");
690 olddirfidnum = olddirfid->fid;
691 newdirfidnum = newdirfid->fid;
694 dprintk(DEBUG_ERROR, "no fid for old file #%lu\n",
700 v9fs_blank_mistat(v9ses, mistat);
702 strcpy(mistat->data + 1, v9ses->name);
703 mistat->name = mistat->data + 1 + strlen(v9ses->name);
705 if (new_dentry->d_name.len >
706 (v9ses->maxdata - strlen(v9ses->name) - sizeof(struct v9fs_stat))) {
707 dprintk(DEBUG_ERROR, "new name too long\n");
711 strcpy(mistat->name, new_dentry->d_name.name);
712 retval = v9fs_t_wstat(v9ses, fid, mistat, &fcall);
718 dprintk(DEBUG_ERROR, "v9fs_t_wstat error: %s\n",
726 * v9fs_vfs_getattr - retreive file metadata
727 * @mnt - mount information
728 * @dentry - file to get attributes on
729 * @stat - metadata structure to populate
734 v9fs_vfs_getattr(struct vfsmount *mnt, struct dentry *dentry,
737 struct v9fs_fcall *fcall = NULL;
738 struct v9fs_session_info *v9ses = v9fs_inode2v9ses(dentry->d_inode);
739 struct v9fs_fid *fid = v9fs_fid_lookup(dentry, FID_OP);
742 dprintk(DEBUG_VFS, "dentry: %p\n", dentry);
745 "couldn't find fid associated with dentry\n");
749 err = v9fs_t_stat(v9ses, fid->fid, &fcall);
752 dprintk(DEBUG_ERROR, "stat error\n");
754 v9fs_mistat2inode(fcall->params.rstat.stat, dentry->d_inode,
755 dentry->d_inode->i_sb);
756 generic_fillattr(dentry->d_inode, stat);
764 * v9fs_vfs_setattr - set file metadata
765 * @dentry: file whose metadata to set
766 * @iattr: metadata assignment structure
770 static int v9fs_vfs_setattr(struct dentry *dentry, struct iattr *iattr)
772 struct v9fs_session_info *v9ses = v9fs_inode2v9ses(dentry->d_inode);
773 struct v9fs_fid *fid = v9fs_fid_lookup(dentry, FID_OP);
774 struct v9fs_stat *mistat = kmalloc(v9ses->maxdata, GFP_KERNEL);
775 struct v9fs_fcall *fcall = NULL;
778 dprintk(DEBUG_VFS, "\n");
781 "Couldn't find fid associated with dentry\n");
788 v9fs_blank_mistat(v9ses, mistat);
789 if (iattr->ia_valid & ATTR_MODE)
790 mistat->mode = unixmode2p9mode(v9ses, iattr->ia_mode);
792 if (iattr->ia_valid & ATTR_MTIME)
793 mistat->mtime = iattr->ia_mtime.tv_sec;
795 if (iattr->ia_valid & ATTR_ATIME)
796 mistat->atime = iattr->ia_atime.tv_sec;
798 if (iattr->ia_valid & ATTR_SIZE)
799 mistat->length = iattr->ia_size;
801 if (v9ses->extended) {
802 char *uid = kmalloc(strlen(mistat->uid), GFP_KERNEL);
803 char *gid = kmalloc(strlen(mistat->gid), GFP_KERNEL);
804 char *muid = kmalloc(strlen(mistat->muid), GFP_KERNEL);
805 char *name = kmalloc(strlen(mistat->name), GFP_KERNEL);
806 char *extension = kmalloc(strlen(mistat->extension),
809 if ((!uid) || (!gid) || (!muid) || (!name) || (!extension)) {
819 strcpy(uid, mistat->uid);
820 strcpy(gid, mistat->gid);
821 strcpy(muid, mistat->muid);
822 strcpy(name, mistat->name);
823 strcpy(extension, mistat->extension);
825 if (iattr->ia_valid & ATTR_UID) {
826 if (strlen(uid) != 8) {
827 dprintk(DEBUG_ERROR, "uid strlen is %u not 8\n",
828 (unsigned int)strlen(uid));
829 sprintf(uid, "%08x", iattr->ia_uid);
832 uid = kmalloc(9, GFP_KERNEL);
835 sprintf(uid, "%08x", iattr->ia_uid);
836 mistat->n_uid = iattr->ia_uid;
839 if (iattr->ia_valid & ATTR_GID) {
840 if (strlen(gid) != 8)
841 dprintk(DEBUG_ERROR, "gid strlen is %u not 8\n",
842 (unsigned int)strlen(gid));
845 gid = kmalloc(9, GFP_KERNEL);
848 sprintf(gid, "%08x", iattr->ia_gid);
849 mistat->n_gid = iattr->ia_gid;
852 mistat->uid = mistat->data;
853 strcpy(mistat->uid, uid);
854 mistat->gid = mistat->data + strlen(uid) + 1;
855 strcpy(mistat->gid, gid);
856 mistat->muid = mistat->gid + strlen(gid) + 1;
857 strcpy(mistat->muid, muid);
858 mistat->name = mistat->muid + strlen(muid) + 1;
859 strcpy(mistat->name, name);
860 mistat->extension = mistat->name + strlen(name) + 1;
861 strcpy(mistat->extension, extension);
870 res = v9fs_t_wstat(v9ses, fid->fid, mistat, &fcall);
873 dprintk(DEBUG_ERROR, "wstat error: %s\n", FCALL_ERROR(fcall));
879 res = inode_setattr(dentry->d_inode, iattr);
885 * v9fs_mistat2inode - populate an inode structure with mistat info
886 * @mistat: Plan 9 metadata (mistat) structure
887 * @inode: inode to populate
888 * @sb: superblock of filesystem
893 v9fs_mistat2inode(struct v9fs_stat *mistat, struct inode *inode,
894 struct super_block *sb)
896 struct v9fs_session_info *v9ses = sb->s_fs_info;
900 inode->i_atime.tv_sec = mistat->atime;
901 inode->i_mtime.tv_sec = mistat->mtime;
902 inode->i_ctime.tv_sec = mistat->mtime;
907 if (v9ses->extended) {
908 /* TODO: string to uid mapping via user-space daemon */
909 inode->i_uid = mistat->n_uid;
910 inode->i_gid = mistat->n_gid;
912 if (mistat->n_uid == -1)
913 sscanf(mistat->uid, "%x", &inode->i_uid);
915 if (mistat->n_gid == -1)
916 sscanf(mistat->gid, "%x", &inode->i_gid);
919 if (inode->i_uid == -1)
920 inode->i_uid = v9ses->uid;
921 if (inode->i_gid == -1)
922 inode->i_gid = v9ses->gid;
924 inode->i_mode = p9mode2unixmode(v9ses, mistat->mode);
925 if ((S_ISBLK(inode->i_mode)) || (S_ISCHR(inode->i_mode))) {
929 sscanf(mistat->extension, "%c %u %u", &type, &major, &minor);
932 inode->i_mode &= ~S_IFBLK;
933 inode->i_mode |= S_IFCHR;
938 dprintk(DEBUG_ERROR, "Unknown special type %c (%s)\n",
939 type, mistat->extension);
941 inode->i_rdev = MKDEV(major, minor);
945 inode->i_size = mistat->length;
947 inode->i_blksize = sb->s_blocksize;
949 (inode->i_size + inode->i_blksize - 1) >> sb->s_blocksize_bits;
953 * v9fs_qid2ino - convert qid into inode number
956 * BUG: potential for inode number collisions?
959 ino_t v9fs_qid2ino(struct v9fs_qid *qid)
961 u64 path = qid->path + 2;
964 if (sizeof(ino_t) == sizeof(path))
965 memcpy(&i, &path, sizeof(ino_t));
967 i = (ino_t) (path ^ (path >> 32));
973 * v9fs_vfs_symlink - helper function to create symlinks
974 * @dir: directory inode containing symlink
975 * @dentry: dentry for symlink
976 * @symname: symlink data
978 * See 9P2000.u RFC for more information
983 v9fs_vfs_symlink(struct inode *dir, struct dentry *dentry, const char *symname)
986 struct v9fs_fid *newfid;
987 struct v9fs_session_info *v9ses = v9fs_inode2v9ses(dir);
988 struct super_block *sb = dir ? dir->i_sb : NULL;
989 struct v9fs_fcall *fcall = NULL;
990 struct v9fs_stat *mistat = kmalloc(v9ses->maxdata, GFP_KERNEL);
992 dprintk(DEBUG_VFS, " %lu,%s,%s\n", dir->i_ino, dentry->d_name.name,
995 if ((!dentry) || (!sb) || (!v9ses)) {
996 dprintk(DEBUG_ERROR, "problem with arguments\n");
1000 if (!v9ses->extended) {
1001 dprintk(DEBUG_ERROR, "not extended\n");
1005 /* issue a create */
1006 retval = v9fs_create(dir, dentry, S_IFLNK, 0);
1010 newfid = v9fs_fid_lookup(dentry, FID_OP);
1012 /* issue a twstat */
1013 v9fs_blank_mistat(v9ses, mistat);
1014 strcpy(mistat->data + 1, symname);
1015 mistat->extension = mistat->data + 1;
1016 retval = v9fs_t_wstat(v9ses, newfid->fid, mistat, &fcall);
1018 dprintk(DEBUG_ERROR, "v9fs_t_wstat error: %s\n",
1019 FCALL_ERROR(fcall));
1025 if (v9fs_t_clunk(v9ses, newfid->fid, &fcall)) {
1026 dprintk(DEBUG_ERROR, "clunk for symlink failed: %s\n",
1027 FCALL_ERROR(fcall));
1031 d_drop(dentry); /* FID - will this also clunk? */
1041 * v9fs_readlink - read a symlink's location (internal version)
1042 * @dentry: dentry for symlink
1043 * @buf: buffer to load symlink location into
1044 * @buflen: length of buffer
1048 static int v9fs_readlink(struct dentry *dentry, char *buffer, int buflen)
1050 int retval = -EPERM;
1052 struct v9fs_fcall *fcall = NULL;
1053 struct v9fs_session_info *v9ses = v9fs_inode2v9ses(dentry->d_inode);
1054 struct v9fs_fid *fid = v9fs_fid_lookup(dentry, FID_OP);
1057 dprintk(DEBUG_ERROR, "could not resolve fid from dentry\n");
1062 if (!v9ses->extended) {
1064 dprintk(DEBUG_ERROR, "not extended\n");
1068 dprintk(DEBUG_VFS, " %s\n", dentry->d_name.name);
1069 retval = v9fs_t_stat(v9ses, fid->fid, &fcall);
1072 dprintk(DEBUG_ERROR, "stat error\n");
1079 if (!(fcall->params.rstat.stat->mode & V9FS_DMSYMLINK)) {
1084 /* copy extension buffer into buffer */
1085 if (strlen(fcall->params.rstat.stat->extension) < buflen)
1086 buflen = strlen(fcall->params.rstat.stat->extension);
1088 memcpy(buffer, fcall->params.rstat.stat->extension, buflen + 1);
1099 * v9fs_vfs_readlink - read a symlink's location
1100 * @dentry: dentry for symlink
1101 * @buf: buffer to load symlink location into
1102 * @buflen: length of buffer
1106 static int v9fs_vfs_readlink(struct dentry *dentry, char __user * buffer,
1111 char *link = __getname();
1113 if (strlen(link) < buflen)
1114 buflen = strlen(link);
1116 dprintk(DEBUG_VFS, " dentry: %s (%p)\n", dentry->d_iname, dentry);
1118 retval = v9fs_readlink(dentry, link, buflen);
1121 if ((ret = copy_to_user(buffer, link, retval)) != 0) {
1122 dprintk(DEBUG_ERROR, "problem copying to user: %d\n",
1133 * v9fs_vfs_follow_link - follow a symlink path
1134 * @dentry: dentry for symlink
1139 static void *v9fs_vfs_follow_link(struct dentry *dentry, struct nameidata *nd)
1142 char *link = __getname();
1144 dprintk(DEBUG_VFS, "%s n", dentry->d_name.name);
1147 link = ERR_PTR(-ENOMEM);
1149 len = v9fs_readlink(dentry, link, strlen(link));
1153 link = ERR_PTR(len);
1157 nd_set_link(nd, link);
1163 * v9fs_vfs_put_link - release a symlink path
1164 * @dentry: dentry for symlink
1169 static void v9fs_vfs_put_link(struct dentry *dentry, struct nameidata *nd, void *p)
1171 char *s = nd_get_link(nd);
1173 dprintk(DEBUG_VFS, " %s %s\n", dentry->d_name.name, s);
1179 * v9fs_vfs_link - create a hardlink
1180 * @old_dentry: dentry for file to link to
1181 * @dir: inode destination for new link
1182 * @new_dentry: dentry for link
1186 /* XXX - lots of code dup'd from symlink and creates,
1187 * figure out a better reuse strategy
1191 v9fs_vfs_link(struct dentry *old_dentry, struct inode *dir,
1192 struct dentry *dentry)
1194 int retval = -EPERM;
1195 struct v9fs_session_info *v9ses = v9fs_inode2v9ses(dir);
1196 struct v9fs_fcall *fcall = NULL;
1197 struct v9fs_stat *mistat = kmalloc(v9ses->maxdata, GFP_KERNEL);
1198 struct v9fs_fid *oldfid = v9fs_fid_lookup(old_dentry, FID_OP);
1199 struct v9fs_fid *newfid = NULL;
1200 char *symname = __getname();
1202 dprintk(DEBUG_VFS, " %lu,%s,%s\n", dir->i_ino, dentry->d_name.name,
1203 old_dentry->d_name.name);
1205 if (!v9ses->extended) {
1206 dprintk(DEBUG_ERROR, "not extended\n");
1210 /* get fid of old_dentry */
1211 sprintf(symname, "hardlink(%d)\n", oldfid->fid);
1213 /* issue a create */
1214 retval = v9fs_create(dir, dentry, V9FS_DMLINK, 0);
1218 newfid = v9fs_fid_lookup(dentry, FID_OP);
1220 dprintk(DEBUG_ERROR, "couldn't resolve fid from dentry\n");
1224 /* issue a twstat */
1225 v9fs_blank_mistat(v9ses, mistat);
1226 strcpy(mistat->data + 1, symname);
1227 mistat->extension = mistat->data + 1;
1228 retval = v9fs_t_wstat(v9ses, newfid->fid, mistat, &fcall);
1230 dprintk(DEBUG_ERROR, "v9fs_t_wstat error: %s\n",
1231 FCALL_ERROR(fcall));
1237 if (v9fs_t_clunk(v9ses, newfid->fid, &fcall)) {
1238 dprintk(DEBUG_ERROR, "clunk for symlink failed: %s\n",
1239 FCALL_ERROR(fcall));
1243 d_drop(dentry); /* FID - will this also clunk? */
1256 * v9fs_vfs_mknod - create a special file
1257 * @dir: inode destination for new link
1258 * @dentry: dentry for file
1259 * @mode: mode for creation
1260 * @dev_t: device associated with special file
1265 v9fs_vfs_mknod(struct inode *dir, struct dentry *dentry, int mode, dev_t rdev)
1267 int retval = -EPERM;
1268 struct v9fs_fid *newfid;
1269 struct v9fs_session_info *v9ses = v9fs_inode2v9ses(dir);
1270 struct v9fs_fcall *fcall = NULL;
1271 struct v9fs_stat *mistat = kmalloc(v9ses->maxdata, GFP_KERNEL);
1272 char *symname = __getname();
1274 dprintk(DEBUG_VFS, " %lu,%s mode: %x MAJOR: %u MINOR: %u\n", dir->i_ino,
1275 dentry->d_name.name, mode, MAJOR(rdev), MINOR(rdev));
1277 if (!new_valid_dev(rdev)) {
1282 if (!v9ses->extended) {
1283 dprintk(DEBUG_ERROR, "not extended\n");
1287 /* issue a create */
1288 retval = v9fs_create(dir, dentry, mode, 0);
1293 newfid = v9fs_fid_lookup(dentry, FID_OP);
1295 dprintk(DEBUG_ERROR, "coudn't resove fid from dentry\n");
1300 /* build extension */
1302 sprintf(symname, "b %u %u", MAJOR(rdev), MINOR(rdev));
1303 else if (S_ISCHR(mode))
1304 sprintf(symname, "c %u %u", MAJOR(rdev), MINOR(rdev));
1305 else if (S_ISFIFO(mode)) ; /* DO NOTHING */
1311 if (!S_ISFIFO(mode)) {
1312 /* issue a twstat */
1313 v9fs_blank_mistat(v9ses, mistat);
1314 strcpy(mistat->data + 1, symname);
1315 mistat->extension = mistat->data + 1;
1316 retval = v9fs_t_wstat(v9ses, newfid->fid, mistat, &fcall);
1318 dprintk(DEBUG_ERROR, "v9fs_t_wstat error: %s\n",
1319 FCALL_ERROR(fcall));
1326 /* need to update dcache so we show up */
1329 if (v9fs_t_clunk(v9ses, newfid->fid, &fcall)) {
1330 dprintk(DEBUG_ERROR, "clunk for symlink failed: %s\n",
1331 FCALL_ERROR(fcall));
1335 d_drop(dentry); /* FID - will this also clunk? */
1345 static struct inode_operations v9fs_dir_inode_operations = {
1346 .create = v9fs_vfs_create,
1347 .lookup = v9fs_vfs_lookup,
1348 .symlink = v9fs_vfs_symlink,
1349 .link = v9fs_vfs_link,
1350 .unlink = v9fs_vfs_unlink,
1351 .mkdir = v9fs_vfs_mkdir,
1352 .rmdir = v9fs_vfs_rmdir,
1353 .mknod = v9fs_vfs_mknod,
1354 .rename = v9fs_vfs_rename,
1355 .readlink = v9fs_vfs_readlink,
1356 .getattr = v9fs_vfs_getattr,
1357 .setattr = v9fs_vfs_setattr,
1360 static struct inode_operations v9fs_file_inode_operations = {
1361 .getattr = v9fs_vfs_getattr,
1362 .setattr = v9fs_vfs_setattr,
1365 static struct inode_operations v9fs_symlink_inode_operations = {
1366 .readlink = v9fs_vfs_readlink,
1367 .follow_link = v9fs_vfs_follow_link,
1368 .put_link = v9fs_vfs_put_link,
1369 .getattr = v9fs_vfs_getattr,
1370 .setattr = v9fs_vfs_setattr,