2 * linux/fs/9p/vfs_inode.c
4 * This file contains 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 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 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 * @buf: 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 buf->st_uid = (unsigned short)-1;
181 buf->st_gid = (unsigned short)-1;
183 if (v9ses && v9ses->extended) {
184 /* TODO: string to uid mapping via user-space daemon */
185 if (mistat->n_uid != -1)
186 sscanf(mistat->uid, "%x", (unsigned int *)&buf->st_uid);
188 if (mistat->n_gid != -1)
189 sscanf(mistat->gid, "%x", (unsigned int *)&buf->st_gid);
192 if (buf->st_uid == (unsigned short)-1)
193 buf->st_uid = v9ses->uid;
194 if (buf->st_gid == (unsigned short)-1)
195 buf->st_gid = v9ses->gid;
197 buf->st_mode = p9mode2unixmode(v9ses, mistat->mode);
198 if ((S_ISBLK(buf->st_mode)) || (S_ISCHR(buf->st_mode))) {
202 sscanf(mistat->extension, "%c %u %u", &type, &major, &minor);
205 buf->st_mode &= ~S_IFBLK;
206 buf->st_mode |= S_IFCHR;
211 dprintk(DEBUG_ERROR, "Unknown special type %c (%s)\n",
212 type, mistat->extension);
214 buf->st_rdev = MKDEV(major, minor);
218 buf->st_size = mistat->length;
220 buf->st_blksize = sb->s_blocksize;
222 (buf->st_size + buf->st_blksize - 1) >> sb->s_blocksize_bits;
226 * v9fs_get_inode - helper function to setup an inode
228 * @mode: mode to setup inode with
232 struct inode *v9fs_get_inode(struct super_block *sb, int mode)
234 struct inode *inode = NULL;
236 dprintk(DEBUG_VFS, "super block: %p mode: %o\n", sb, mode);
238 inode = new_inode(sb);
240 inode->i_mode = mode;
241 inode->i_uid = current->fsuid;
242 inode->i_gid = current->fsgid;
243 inode->i_blksize = sb->s_blocksize;
246 inode->i_atime = inode->i_mtime = inode->i_ctime = CURRENT_TIME;
248 switch (mode & S_IFMT) {
254 inode->i_op = &v9fs_file_inode_operations;
255 inode->i_fop = &v9fs_file_operations;
259 inode->i_op = &v9fs_dir_inode_operations;
260 inode->i_fop = &v9fs_dir_operations;
263 inode->i_op = &v9fs_symlink_inode_operations;
266 dprintk(DEBUG_ERROR, "BAD mode 0x%x S_IFMT 0x%x\n",
267 mode, mode & S_IFMT);
268 return ERR_PTR(-EINVAL);
271 eprintk(KERN_WARNING, "Problem allocating inode\n");
272 return ERR_PTR(-ENOMEM);
278 * v9fs_create - helper function to create files and directories
279 * @dir: directory inode file is being created in
280 * @file_dentry: dentry file is being created in
281 * @perm: permissions file is being created with
282 * @open_mode: resulting open mode for file
287 v9fs_create(struct inode *dir,
288 struct dentry *file_dentry,
289 unsigned int perm, unsigned int open_mode)
291 struct v9fs_session_info *v9ses = v9fs_inode2v9ses(dir);
292 struct super_block *sb = dir->i_sb;
293 struct v9fs_fid *dirfid =
294 v9fs_fid_lookup(file_dentry->d_parent, FID_WALK);
295 struct v9fs_fid *fid = NULL;
296 struct inode *file_inode = NULL;
297 struct v9fs_fcall *fcall = NULL;
303 unsigned int iounit = 0;
305 perm = unixmode2p9mode(v9ses, perm);
307 dprintk(DEBUG_VFS, "dir: %p dentry: %p perm: %o mode: %o\n", dir,
308 file_dentry, perm, open_mode);
313 dirfidnum = dirfid->fid;
315 dprintk(DEBUG_ERROR, "No fid for the directory #%lu\n",
320 if (file_dentry->d_inode) {
322 "Odd. There is an inode for dir %lu, name :%s:\n",
323 dir->i_ino, file_dentry->d_name.name);
327 newfid = v9fs_get_idpool(&v9ses->fidpool);
329 eprintk(KERN_WARNING, "no free fids available\n");
333 result = v9fs_t_walk(v9ses, dirfidnum, newfid, NULL, &fcall);
335 dprintk(DEBUG_ERROR, "clone error: %s\n", FCALL_ERROR(fcall));
336 v9fs_put_idpool(newfid, &v9ses->fidpool);
343 result = v9fs_t_create(v9ses, newfid, (char *)file_dentry->d_name.name,
344 perm, open_mode, &fcall);
346 dprintk(DEBUG_ERROR, "create fails: %s(%d)\n",
347 FCALL_ERROR(fcall), result);
352 iounit = fcall->params.rcreate.iounit;
353 qid = fcall->params.rcreate.qid;
356 fid = v9fs_fid_create(file_dentry);
366 fid->iounit = iounit;
368 fid->rdir_fcall = NULL;
371 if ((perm & V9FS_DMSYMLINK) || (perm & V9FS_DMLINK) ||
372 (perm & V9FS_DMNAMEDPIPE) || (perm & V9FS_DMSOCKET) ||
373 (perm & V9FS_DMDEVICE))
376 result = v9fs_t_stat(v9ses, newfid, &fcall);
378 dprintk(DEBUG_ERROR, "stat error: %s(%d)\n", FCALL_ERROR(fcall),
383 v9fs_mistat2unix(fcall->params.rstat.stat, &newstat, sb);
385 file_inode = v9fs_get_inode(sb, newstat.st_mode);
386 if ((!file_inode) || IS_ERR(file_inode)) {
387 dprintk(DEBUG_ERROR, "create inode failed\n");
392 v9fs_mistat2inode(fcall->params.rstat.stat, file_inode, sb);
394 d_instantiate(file_dentry, file_inode);
396 if (perm & V9FS_DMDIR) {
397 if (v9fs_t_clunk(v9ses, newfid, &fcall))
398 dprintk(DEBUG_ERROR, "clunk for mkdir failed: %s\n",
401 v9fs_put_idpool(newfid, &v9ses->fidpool);
414 if (v9fs_t_clunk(v9ses, newfid, &fcall))
415 dprintk(DEBUG_ERROR, "clunk failed: %s\n",
418 v9fs_put_idpool(newfid, &v9ses->fidpool);
425 * v9fs_remove - helper function to remove files and directories
426 * @dir: directory inode that is being deleted
427 * @file: dentry that is being deleted
428 * @rmdir: removing a directory
432 static int v9fs_remove(struct inode *dir, struct dentry *file, int rmdir)
434 struct v9fs_fcall *fcall = NULL;
435 struct super_block *sb = NULL;
436 struct v9fs_session_info *v9ses = NULL;
437 struct v9fs_fid *v9fid = NULL;
438 struct inode *file_inode = NULL;
442 dprintk(DEBUG_VFS, "inode: %p dentry: %p rmdir: %d\n", dir, file,
445 file_inode = file->d_inode;
446 sb = file_inode->i_sb;
447 v9ses = v9fs_inode2v9ses(file_inode);
448 v9fid = v9fs_fid_lookup(file, FID_OP);
458 dprintk(DEBUG_ERROR, "inode #%lu, no fid!\n",
463 result = v9fs_t_remove(v9ses, fid, &fcall);
465 dprintk(DEBUG_ERROR, "remove of file fails: %s(%d)\n",
466 FCALL_ERROR(fcall), result);
468 v9fs_put_idpool(fid, &v9ses->fidpool);
469 v9fs_fid_destroy(v9fid);
477 * v9fs_vfs_create - VFS hook to create files
478 * @inode: directory inode that is being deleted
479 * @dentry: dentry that is being deleted
480 * @perm: create permissions
481 * @nd: path information
486 v9fs_vfs_create(struct inode *inode, struct dentry *dentry, int perm,
487 struct nameidata *nd)
489 return v9fs_create(inode, dentry, perm, O_RDWR);
493 * v9fs_vfs_mkdir - VFS mkdir hook to create a directory
494 * @inode: inode that is being unlinked
495 * @dentry: dentry that is being unlinked
496 * @mode: mode for new directory
500 static int v9fs_vfs_mkdir(struct inode *inode, struct dentry *dentry, int mode)
502 return v9fs_create(inode, dentry, mode | S_IFDIR, O_RDONLY);
506 * v9fs_vfs_lookup - VFS lookup hook to "walk" to a new inode
507 * @dir: inode that is being walked from
508 * @dentry: dentry that is being walked to?
509 * @nameidata: path data
513 static struct dentry *v9fs_vfs_lookup(struct inode *dir, struct dentry *dentry,
514 struct nameidata *nameidata)
516 struct super_block *sb;
517 struct v9fs_session_info *v9ses;
518 struct v9fs_fid *dirfid;
519 struct v9fs_fid *fid;
521 struct v9fs_fcall *fcall = NULL;
527 dprintk(DEBUG_VFS, "dir: %p dentry: (%s) %p nameidata: %p\n",
528 dir, dentry->d_iname, dentry, nameidata);
531 v9ses = v9fs_inode2v9ses(dir);
532 dirfid = v9fs_fid_lookup(dentry->d_parent, FID_WALK);
535 dprintk(DEBUG_ERROR, "no dirfid\n");
536 return ERR_PTR(-EINVAL);
539 dirfidnum = dirfid->fid;
542 dprintk(DEBUG_ERROR, "no dirfid for inode %p, #%lu\n",
544 return ERR_PTR(-EBADF);
547 newfid = v9fs_get_idpool(&v9ses->fidpool);
549 eprintk(KERN_WARNING, "newfid fails!\n");
550 return ERR_PTR(-ENOSPC);
554 v9fs_t_walk(v9ses, dirfidnum, newfid, (char *)dentry->d_name.name,
557 v9fs_put_idpool(newfid, &v9ses->fidpool);
558 if (result == -ENOENT) {
561 "Return negative dentry %p count %d\n",
562 dentry, atomic_read(&dentry->d_count));
565 dprintk(DEBUG_ERROR, "walk error:%d\n", result);
569 result = v9fs_t_stat(v9ses, newfid, &fcall);
571 dprintk(DEBUG_ERROR, "stat error\n");
575 v9fs_mistat2unix(fcall->params.rstat.stat, &newstat, sb);
576 inode = v9fs_get_inode(sb, newstat.st_mode);
578 if (IS_ERR(inode) && (PTR_ERR(inode) == -ENOSPC)) {
579 eprintk(KERN_WARNING, "inode alloc failes, returns %ld\n",
586 inode->i_ino = v9fs_qid2ino(&fcall->params.rstat.stat->qid);
588 fid = v9fs_fid_create(dentry);
590 dprintk(DEBUG_ERROR, "couldn't insert\n");
598 fid->qid = fcall->params.rstat.stat->qid;
600 dentry->d_op = &v9fs_dentry_operations;
601 v9fs_mistat2inode(fcall->params.rstat.stat, inode, inode->i_sb);
603 d_add(dentry, inode);
610 return ERR_PTR(result);
614 * v9fs_vfs_unlink - VFS unlink hook to delete an inode
615 * @i: inode that is being unlinked
616 * @d: dentry that is being unlinked
620 static int v9fs_vfs_unlink(struct inode *i, struct dentry *d)
622 return v9fs_remove(i, d, 0);
626 * v9fs_vfs_rmdir - VFS unlink hook to delete a directory
627 * @i: inode that is being unlinked
628 * @d: dentry that is being unlinked
632 static int v9fs_vfs_rmdir(struct inode *i, struct dentry *d)
634 return v9fs_remove(i, d, 1);
638 * v9fs_vfs_rename - VFS hook to rename an inode
639 * @old_dir: old dir inode
640 * @old_dentry: old dentry
641 * @new_dir: new dir inode
642 * @new_dentry: new dentry
647 v9fs_vfs_rename(struct inode *old_dir, struct dentry *old_dentry,
648 struct inode *new_dir, struct dentry *new_dentry)
650 struct inode *old_inode = old_dentry->d_inode;
651 struct v9fs_session_info *v9ses = v9fs_inode2v9ses(old_inode);
652 struct v9fs_fid *oldfid = v9fs_fid_lookup(old_dentry, FID_WALK);
653 struct v9fs_fid *olddirfid =
654 v9fs_fid_lookup(old_dentry->d_parent, FID_WALK);
655 struct v9fs_fid *newdirfid =
656 v9fs_fid_lookup(new_dentry->d_parent, FID_WALK);
657 struct v9fs_stat *mistat = kmalloc(v9ses->maxdata, GFP_KERNEL);
658 struct v9fs_fcall *fcall = NULL;
660 int olddirfidnum = -1;
661 int newdirfidnum = -1;
664 dprintk(DEBUG_VFS, "\n");
669 if ((!oldfid) || (!olddirfid) || (!newdirfid)) {
670 dprintk(DEBUG_ERROR, "problem with arguments\n");
674 /* 9P can only handle file rename in the same directory */
675 if (memcmp(&olddirfid->qid, &newdirfid->qid, sizeof(newdirfid->qid))) {
676 dprintk(DEBUG_ERROR, "old dir and new dir are different\n");
682 olddirfidnum = olddirfid->fid;
683 newdirfidnum = newdirfid->fid;
686 dprintk(DEBUG_ERROR, "no fid for old file #%lu\n",
692 v9fs_blank_mistat(v9ses, mistat);
694 strcpy(mistat->data + 1, v9ses->name);
695 mistat->name = mistat->data + 1 + strlen(v9ses->name);
697 if (new_dentry->d_name.len >
698 (v9ses->maxdata - strlen(v9ses->name) - sizeof(struct v9fs_stat))) {
699 dprintk(DEBUG_ERROR, "new name too long\n");
703 strcpy(mistat->name, new_dentry->d_name.name);
704 retval = v9fs_t_wstat(v9ses, fid, mistat, &fcall);
710 dprintk(DEBUG_ERROR, "v9fs_t_wstat error: %s\n",
718 * v9fs_vfs_getattr - retreive file metadata
719 * @mnt - mount information
720 * @dentry - file to get attributes on
721 * @stat - metadata structure to populate
726 v9fs_vfs_getattr(struct vfsmount *mnt, struct dentry *dentry,
729 struct v9fs_fcall *fcall = NULL;
730 struct v9fs_session_info *v9ses = v9fs_inode2v9ses(dentry->d_inode);
731 struct v9fs_fid *fid = v9fs_fid_lookup(dentry, FID_OP);
734 dprintk(DEBUG_VFS, "dentry: %p\n", dentry);
737 "couldn't find fid associated with dentry\n");
741 err = v9fs_t_stat(v9ses, fid->fid, &fcall);
744 dprintk(DEBUG_ERROR, "stat error\n");
746 v9fs_mistat2inode(fcall->params.rstat.stat, dentry->d_inode,
747 dentry->d_inode->i_sb);
748 generic_fillattr(dentry->d_inode, stat);
756 * v9fs_vfs_setattr - set file metadata
757 * @dentry: file whose metadata to set
758 * @iattr: metadata assignment structure
762 static int v9fs_vfs_setattr(struct dentry *dentry, struct iattr *iattr)
764 struct v9fs_session_info *v9ses = v9fs_inode2v9ses(dentry->d_inode);
765 struct v9fs_fid *fid = v9fs_fid_lookup(dentry, FID_OP);
766 struct v9fs_fcall *fcall = NULL;
767 struct v9fs_stat *mistat = kmalloc(v9ses->maxdata, GFP_KERNEL);
770 dprintk(DEBUG_VFS, "\n");
777 "Couldn't find fid associated with dentry\n");
781 v9fs_blank_mistat(v9ses, mistat);
782 if (iattr->ia_valid & ATTR_MODE)
783 mistat->mode = unixmode2p9mode(v9ses, iattr->ia_mode);
785 if (iattr->ia_valid & ATTR_MTIME)
786 mistat->mtime = iattr->ia_mtime.tv_sec;
788 if (iattr->ia_valid & ATTR_ATIME)
789 mistat->atime = iattr->ia_atime.tv_sec;
791 if (iattr->ia_valid & ATTR_SIZE)
792 mistat->length = iattr->ia_size;
794 if (v9ses->extended) {
795 char *ptr = mistat->data+1;
797 if (iattr->ia_valid & ATTR_UID) {
799 ptr += 1+sprintf(ptr, "%08x", iattr->ia_uid);
800 mistat->n_uid = iattr->ia_uid;
803 if (iattr->ia_valid & ATTR_GID) {
805 ptr += 1+sprintf(ptr, "%08x", iattr->ia_gid);
806 mistat->n_gid = iattr->ia_gid;
810 res = v9fs_t_wstat(v9ses, fid->fid, mistat, &fcall);
813 dprintk(DEBUG_ERROR, "wstat error: %s\n", FCALL_ERROR(fcall));
819 res = inode_setattr(dentry->d_inode, iattr);
825 * v9fs_mistat2inode - populate an inode structure with mistat info
826 * @mistat: Plan 9 metadata (mistat) structure
827 * @inode: inode to populate
828 * @sb: superblock of filesystem
833 v9fs_mistat2inode(struct v9fs_stat *mistat, struct inode *inode,
834 struct super_block *sb)
836 struct v9fs_session_info *v9ses = sb->s_fs_info;
840 inode->i_atime.tv_sec = mistat->atime;
841 inode->i_mtime.tv_sec = mistat->mtime;
842 inode->i_ctime.tv_sec = mistat->mtime;
847 if (v9ses->extended) {
848 /* TODO: string to uid mapping via user-space daemon */
849 inode->i_uid = mistat->n_uid;
850 inode->i_gid = mistat->n_gid;
852 if (mistat->n_uid == -1)
853 sscanf(mistat->uid, "%x", &inode->i_uid);
855 if (mistat->n_gid == -1)
856 sscanf(mistat->gid, "%x", &inode->i_gid);
859 if (inode->i_uid == -1)
860 inode->i_uid = v9ses->uid;
861 if (inode->i_gid == -1)
862 inode->i_gid = v9ses->gid;
864 inode->i_mode = p9mode2unixmode(v9ses, mistat->mode);
865 if ((S_ISBLK(inode->i_mode)) || (S_ISCHR(inode->i_mode))) {
869 sscanf(mistat->extension, "%c %u %u", &type, &major, &minor);
872 inode->i_mode &= ~S_IFBLK;
873 inode->i_mode |= S_IFCHR;
878 dprintk(DEBUG_ERROR, "Unknown special type %c (%s)\n",
879 type, mistat->extension);
881 inode->i_rdev = MKDEV(major, minor);
885 inode->i_size = mistat->length;
887 inode->i_blksize = sb->s_blocksize;
889 (inode->i_size + inode->i_blksize - 1) >> sb->s_blocksize_bits;
893 * v9fs_qid2ino - convert qid into inode number
896 * BUG: potential for inode number collisions?
899 ino_t v9fs_qid2ino(struct v9fs_qid *qid)
901 u64 path = qid->path + 2;
904 if (sizeof(ino_t) == sizeof(path))
905 memcpy(&i, &path, sizeof(ino_t));
907 i = (ino_t) (path ^ (path >> 32));
913 * v9fs_vfs_symlink - helper function to create symlinks
914 * @dir: directory inode containing symlink
915 * @dentry: dentry for symlink
916 * @symname: symlink data
918 * See 9P2000.u RFC for more information
923 v9fs_vfs_symlink(struct inode *dir, struct dentry *dentry, const char *symname)
926 struct v9fs_fid *newfid;
927 struct v9fs_session_info *v9ses = v9fs_inode2v9ses(dir);
928 struct v9fs_fcall *fcall = NULL;
929 struct v9fs_stat *mistat = kmalloc(v9ses->maxdata, GFP_KERNEL);
931 dprintk(DEBUG_VFS, " %lu,%s,%s\n", dir->i_ino, dentry->d_name.name,
937 if (!v9ses->extended) {
938 dprintk(DEBUG_ERROR, "not extended\n");
943 retval = v9fs_create(dir, dentry, S_IFLNK, 0);
947 newfid = v9fs_fid_lookup(dentry, FID_OP);
950 v9fs_blank_mistat(v9ses, mistat);
951 strcpy(mistat->data + 1, symname);
952 mistat->extension = mistat->data + 1;
953 retval = v9fs_t_wstat(v9ses, newfid->fid, mistat, &fcall);
955 dprintk(DEBUG_ERROR, "v9fs_t_wstat error: %s\n",
962 if (v9fs_t_clunk(v9ses, newfid->fid, &fcall)) {
963 dprintk(DEBUG_ERROR, "clunk for symlink failed: %s\n",
968 d_drop(dentry); /* FID - will this also clunk? */
978 * v9fs_readlink - read a symlink's location (internal version)
979 * @dentry: dentry for symlink
980 * @buffer: buffer to load symlink location into
981 * @buflen: length of buffer
985 static int v9fs_readlink(struct dentry *dentry, char *buffer, int buflen)
989 struct v9fs_fcall *fcall = NULL;
990 struct v9fs_session_info *v9ses = v9fs_inode2v9ses(dentry->d_inode);
991 struct v9fs_fid *fid = v9fs_fid_lookup(dentry, FID_OP);
994 dprintk(DEBUG_ERROR, "could not resolve fid from dentry\n");
999 if (!v9ses->extended) {
1001 dprintk(DEBUG_ERROR, "not extended\n");
1005 dprintk(DEBUG_VFS, " %s\n", dentry->d_name.name);
1006 retval = v9fs_t_stat(v9ses, fid->fid, &fcall);
1009 dprintk(DEBUG_ERROR, "stat error\n");
1016 if (!(fcall->params.rstat.stat->mode & V9FS_DMSYMLINK)) {
1021 /* copy extension buffer into buffer */
1022 if (strlen(fcall->params.rstat.stat->extension) < buflen)
1023 buflen = strlen(fcall->params.rstat.stat->extension);
1025 memcpy(buffer, fcall->params.rstat.stat->extension, buflen + 1);
1036 * v9fs_vfs_readlink - read a symlink's location
1037 * @dentry: dentry for symlink
1038 * @buf: buffer to load symlink location into
1039 * @buflen: length of buffer
1043 static int v9fs_vfs_readlink(struct dentry *dentry, char __user * buffer,
1048 char *link = __getname();
1050 if (strlen(link) < buflen)
1051 buflen = strlen(link);
1053 dprintk(DEBUG_VFS, " dentry: %s (%p)\n", dentry->d_iname, dentry);
1055 retval = v9fs_readlink(dentry, link, buflen);
1058 if ((ret = copy_to_user(buffer, link, retval)) != 0) {
1059 dprintk(DEBUG_ERROR, "problem copying to user: %d\n",
1070 * v9fs_vfs_follow_link - follow a symlink path
1071 * @dentry: dentry for symlink
1076 static void *v9fs_vfs_follow_link(struct dentry *dentry, struct nameidata *nd)
1079 char *link = __getname();
1081 dprintk(DEBUG_VFS, "%s n", dentry->d_name.name);
1084 link = ERR_PTR(-ENOMEM);
1086 len = v9fs_readlink(dentry, link, strlen(link));
1090 link = ERR_PTR(len);
1094 nd_set_link(nd, link);
1100 * v9fs_vfs_put_link - release a symlink path
1101 * @dentry: dentry for symlink
1106 static void v9fs_vfs_put_link(struct dentry *dentry, struct nameidata *nd, void *p)
1108 char *s = nd_get_link(nd);
1110 dprintk(DEBUG_VFS, " %s %s\n", dentry->d_name.name, s);
1116 * v9fs_vfs_link - create a hardlink
1117 * @old_dentry: dentry for file to link to
1118 * @dir: inode destination for new link
1119 * @dentry: dentry for link
1123 /* XXX - lots of code dup'd from symlink and creates,
1124 * figure out a better reuse strategy
1128 v9fs_vfs_link(struct dentry *old_dentry, struct inode *dir,
1129 struct dentry *dentry)
1131 int retval = -EPERM;
1132 struct v9fs_session_info *v9ses = v9fs_inode2v9ses(dir);
1133 struct v9fs_fcall *fcall = NULL;
1134 struct v9fs_stat *mistat = kmalloc(v9ses->maxdata, GFP_KERNEL);
1135 struct v9fs_fid *oldfid = v9fs_fid_lookup(old_dentry, FID_OP);
1136 struct v9fs_fid *newfid = NULL;
1137 char *symname = __getname();
1139 dprintk(DEBUG_VFS, " %lu,%s,%s\n", dir->i_ino, dentry->d_name.name,
1140 old_dentry->d_name.name);
1142 if (!v9ses->extended) {
1143 dprintk(DEBUG_ERROR, "not extended\n");
1147 /* get fid of old_dentry */
1148 sprintf(symname, "hardlink(%d)\n", oldfid->fid);
1150 /* issue a create */
1151 retval = v9fs_create(dir, dentry, V9FS_DMLINK, 0);
1155 newfid = v9fs_fid_lookup(dentry, FID_OP);
1157 dprintk(DEBUG_ERROR, "couldn't resolve fid from dentry\n");
1161 /* issue a twstat */
1162 v9fs_blank_mistat(v9ses, mistat);
1163 strcpy(mistat->data + 1, symname);
1164 mistat->extension = mistat->data + 1;
1165 retval = v9fs_t_wstat(v9ses, newfid->fid, mistat, &fcall);
1167 dprintk(DEBUG_ERROR, "v9fs_t_wstat error: %s\n",
1168 FCALL_ERROR(fcall));
1174 if (v9fs_t_clunk(v9ses, newfid->fid, &fcall)) {
1175 dprintk(DEBUG_ERROR, "clunk for symlink failed: %s\n",
1176 FCALL_ERROR(fcall));
1180 d_drop(dentry); /* FID - will this also clunk? */
1193 * v9fs_vfs_mknod - create a special file
1194 * @dir: inode destination for new link
1195 * @dentry: dentry for file
1196 * @mode: mode for creation
1197 * @dev_t: device associated with special file
1202 v9fs_vfs_mknod(struct inode *dir, struct dentry *dentry, int mode, dev_t rdev)
1204 int retval = -EPERM;
1205 struct v9fs_fid *newfid;
1206 struct v9fs_session_info *v9ses = v9fs_inode2v9ses(dir);
1207 struct v9fs_fcall *fcall = NULL;
1208 struct v9fs_stat *mistat = kmalloc(v9ses->maxdata, GFP_KERNEL);
1209 char *symname = __getname();
1211 dprintk(DEBUG_VFS, " %lu,%s mode: %x MAJOR: %u MINOR: %u\n", dir->i_ino,
1212 dentry->d_name.name, mode, MAJOR(rdev), MINOR(rdev));
1217 if (!new_valid_dev(rdev)) {
1222 if (!v9ses->extended) {
1223 dprintk(DEBUG_ERROR, "not extended\n");
1227 /* issue a create */
1228 retval = v9fs_create(dir, dentry, mode, 0);
1233 newfid = v9fs_fid_lookup(dentry, FID_OP);
1235 dprintk(DEBUG_ERROR, "coudn't resove fid from dentry\n");
1240 /* build extension */
1242 sprintf(symname, "b %u %u", MAJOR(rdev), MINOR(rdev));
1243 else if (S_ISCHR(mode))
1244 sprintf(symname, "c %u %u", MAJOR(rdev), MINOR(rdev));
1245 else if (S_ISFIFO(mode))
1252 if (!S_ISFIFO(mode)) {
1253 /* issue a twstat */
1254 v9fs_blank_mistat(v9ses, mistat);
1255 strcpy(mistat->data + 1, symname);
1256 mistat->extension = mistat->data + 1;
1257 retval = v9fs_t_wstat(v9ses, newfid->fid, mistat, &fcall);
1259 dprintk(DEBUG_ERROR, "v9fs_t_wstat error: %s\n",
1260 FCALL_ERROR(fcall));
1265 /* need to update dcache so we show up */
1268 if (v9fs_t_clunk(v9ses, newfid->fid, &fcall)) {
1269 dprintk(DEBUG_ERROR, "clunk for symlink failed: %s\n",
1270 FCALL_ERROR(fcall));
1274 d_drop(dentry); /* FID - will this also clunk? */
1284 static struct inode_operations v9fs_dir_inode_operations = {
1285 .create = v9fs_vfs_create,
1286 .lookup = v9fs_vfs_lookup,
1287 .symlink = v9fs_vfs_symlink,
1288 .link = v9fs_vfs_link,
1289 .unlink = v9fs_vfs_unlink,
1290 .mkdir = v9fs_vfs_mkdir,
1291 .rmdir = v9fs_vfs_rmdir,
1292 .mknod = v9fs_vfs_mknod,
1293 .rename = v9fs_vfs_rename,
1294 .readlink = v9fs_vfs_readlink,
1295 .getattr = v9fs_vfs_getattr,
1296 .setattr = v9fs_vfs_setattr,
1299 static struct inode_operations v9fs_file_inode_operations = {
1300 .getattr = v9fs_vfs_getattr,
1301 .setattr = v9fs_vfs_setattr,
1304 static struct inode_operations v9fs_symlink_inode_operations = {
1305 .readlink = v9fs_vfs_readlink,
1306 .follow_link = v9fs_vfs_follow_link,
1307 .put_link = v9fs_vfs_put_link,
1308 .getattr = v9fs_vfs_getattr,
1309 .setattr = v9fs_vfs_setattr,