[PATCH] USB: EHCI unlink tweaks
[linux-2.6/x86.git] / fs / 9p / vfs_inode.c
blob3ad8455f8577b290a869d20d7a66d4b66ba85de2
1 /*
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>
29 #include <linux/fs.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>
39 #include "debug.h"
40 #include "v9fs.h"
41 #include "9p.h"
42 #include "v9fs_vfs.h"
43 #include "fid.h"
45 static struct inode_operations v9fs_dir_inode_operations;
46 static struct inode_operations v9fs_dir_inode_operations_ext;
47 static struct inode_operations v9fs_file_inode_operations;
48 static struct inode_operations v9fs_symlink_inode_operations;
50 /**
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)
59 int res;
60 res = mode & 0777;
61 if (S_ISDIR(mode))
62 res |= V9FS_DMDIR;
63 if (v9ses->extended) {
64 if (S_ISLNK(mode))
65 res |= V9FS_DMSYMLINK;
66 if (v9ses->nodev == 0) {
67 if (S_ISSOCK(mode))
68 res |= V9FS_DMSOCKET;
69 if (S_ISFIFO(mode))
70 res |= V9FS_DMNAMEDPIPE;
71 if (S_ISBLK(mode))
72 res |= V9FS_DMDEVICE;
73 if (S_ISCHR(mode))
74 res |= V9FS_DMDEVICE;
77 if ((mode & S_ISUID) == S_ISUID)
78 res |= V9FS_DMSETUID;
79 if ((mode & S_ISGID) == S_ISGID)
80 res |= V9FS_DMSETGID;
81 if ((mode & V9FS_DMLINK))
82 res |= V9FS_DMLINK;
85 return res;
88 /**
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)
97 int res;
99 res = mode & 0777;
101 if ((mode & V9FS_DMDIR) == V9FS_DMDIR)
102 res |= S_IFDIR;
103 else if ((mode & V9FS_DMSYMLINK) && (v9ses->extended))
104 res |= S_IFLNK;
105 else if ((mode & V9FS_DMSOCKET) && (v9ses->extended)
106 && (v9ses->nodev == 0))
107 res |= S_IFSOCK;
108 else if ((mode & V9FS_DMNAMEDPIPE) && (v9ses->extended)
109 && (v9ses->nodev == 0))
110 res |= S_IFIFO;
111 else if ((mode & V9FS_DMDEVICE) && (v9ses->extended)
112 && (v9ses->nodev == 0))
113 res |= S_IFBLK;
114 else
115 res |= S_IFREG;
117 if (v9ses->extended) {
118 if ((mode & V9FS_DMSETUID) == V9FS_DMSETUID)
119 res |= S_ISUID;
121 if ((mode & V9FS_DMSETGID) == V9FS_DMSETGID)
122 res |= S_ISGID;
125 return res;
128 int v9fs_uflags2omode(int uflags)
130 int ret;
132 ret = 0;
133 switch (uflags&3) {
134 default:
135 case O_RDONLY:
136 ret = V9FS_OREAD;
137 break;
139 case O_WRONLY:
140 ret = V9FS_OWRITE;
141 break;
143 case O_RDWR:
144 ret = V9FS_ORDWR;
145 break;
148 if (uflags & O_EXCL)
149 ret |= V9FS_OEXCL;
151 if (uflags & O_TRUNC)
152 ret |= V9FS_OTRUNC;
154 if (uflags & O_APPEND)
155 ret |= V9FS_OAPPEND;
157 return ret;
161 * v9fs_blank_wstat - helper function to setup a 9P stat structure
162 * @v9ses: 9P session info (for determining extended mode)
163 * @wstat: structure to initialize
167 static void
168 v9fs_blank_wstat(struct v9fs_wstat *wstat)
170 wstat->type = ~0;
171 wstat->dev = ~0;
172 wstat->qid.type = ~0;
173 wstat->qid.version = ~0;
174 *((long long *)&wstat->qid.path) = ~0;
175 wstat->mode = ~0;
176 wstat->atime = ~0;
177 wstat->mtime = ~0;
178 wstat->length = ~0;
179 wstat->name = NULL;
180 wstat->uid = NULL;
181 wstat->gid = NULL;
182 wstat->muid = NULL;
183 wstat->n_uid = ~0;
184 wstat->n_gid = ~0;
185 wstat->n_muid = ~0;
186 wstat->extension = NULL;
190 * v9fs_get_inode - helper function to setup an inode
191 * @sb: superblock
192 * @mode: mode to setup inode with
196 struct inode *v9fs_get_inode(struct super_block *sb, int mode)
198 struct inode *inode;
199 struct v9fs_session_info *v9ses = sb->s_fs_info;
201 dprintk(DEBUG_VFS, "super block: %p mode: %o\n", sb, mode);
203 inode = new_inode(sb);
204 if (inode) {
205 inode->i_mode = mode;
206 inode->i_uid = current->fsuid;
207 inode->i_gid = current->fsgid;
208 inode->i_blksize = sb->s_blocksize;
209 inode->i_blocks = 0;
210 inode->i_rdev = 0;
211 inode->i_atime = inode->i_mtime = inode->i_ctime = CURRENT_TIME;
212 inode->i_mapping->a_ops = &v9fs_addr_operations;
214 switch (mode & S_IFMT) {
215 case S_IFIFO:
216 case S_IFBLK:
217 case S_IFCHR:
218 case S_IFSOCK:
219 if(!v9ses->extended) {
220 dprintk(DEBUG_ERROR, "special files without extended mode\n");
221 return ERR_PTR(-EINVAL);
223 init_special_inode(inode, inode->i_mode,
224 inode->i_rdev);
225 break;
226 case S_IFREG:
227 inode->i_op = &v9fs_file_inode_operations;
228 inode->i_fop = &v9fs_file_operations;
229 break;
230 case S_IFLNK:
231 if(!v9ses->extended) {
232 dprintk(DEBUG_ERROR, "extended modes used w/o 9P2000.u\n");
233 return ERR_PTR(-EINVAL);
235 inode->i_op = &v9fs_symlink_inode_operations;
236 break;
237 case S_IFDIR:
238 inode->i_nlink++;
239 if(v9ses->extended)
240 inode->i_op = &v9fs_dir_inode_operations_ext;
241 else
242 inode->i_op = &v9fs_dir_inode_operations;
243 inode->i_fop = &v9fs_dir_operations;
244 break;
245 default:
246 dprintk(DEBUG_ERROR, "BAD mode 0x%x S_IFMT 0x%x\n",
247 mode, mode & S_IFMT);
248 return ERR_PTR(-EINVAL);
250 } else {
251 eprintk(KERN_WARNING, "Problem allocating inode\n");
252 return ERR_PTR(-ENOMEM);
254 return inode;
257 static int
258 v9fs_create(struct v9fs_session_info *v9ses, u32 pfid, char *name,
259 u32 perm, u8 mode, u32 *fidp, struct v9fs_qid *qid, u32 *iounit)
261 u32 fid;
262 int err;
263 struct v9fs_fcall *fcall;
265 fid = v9fs_get_idpool(&v9ses->fidpool);
266 if (fid < 0) {
267 eprintk(KERN_WARNING, "no free fids available\n");
268 return -ENOSPC;
271 err = v9fs_t_walk(v9ses, pfid, fid, NULL, &fcall);
272 if (err < 0) {
273 PRINT_FCALL_ERROR("clone error", fcall);
274 goto error;
276 kfree(fcall);
278 err = v9fs_t_create(v9ses, fid, name, perm, mode, &fcall);
279 if (err < 0) {
280 PRINT_FCALL_ERROR("create fails", fcall);
281 goto error;
284 if (iounit)
285 *iounit = fcall->params.rcreate.iounit;
287 if (qid)
288 *qid = fcall->params.rcreate.qid;
290 if (fidp)
291 *fidp = fid;
293 kfree(fcall);
294 return 0;
296 error:
297 if (fid >= 0)
298 v9fs_put_idpool(fid, &v9ses->fidpool);
300 kfree(fcall);
301 return err;
304 static struct v9fs_fid*
305 v9fs_clone_walk(struct v9fs_session_info *v9ses, u32 fid, struct dentry *dentry)
307 int err;
308 u32 nfid;
309 struct v9fs_fid *ret;
310 struct v9fs_fcall *fcall;
312 nfid = v9fs_get_idpool(&v9ses->fidpool);
313 if (nfid < 0) {
314 eprintk(KERN_WARNING, "no free fids available\n");
315 return ERR_PTR(-ENOSPC);
318 err = v9fs_t_walk(v9ses, fid, nfid, (char *) dentry->d_name.name,
319 &fcall);
321 if (err < 0) {
322 PRINT_FCALL_ERROR("walk error", fcall);
323 v9fs_put_idpool(nfid, &v9ses->fidpool);
324 goto error;
327 kfree(fcall);
328 fcall = NULL;
329 ret = v9fs_fid_create(v9ses, nfid);
330 if (!ret) {
331 err = -ENOMEM;
332 goto clunk_fid;
335 err = v9fs_fid_insert(ret, dentry);
336 if (err < 0) {
337 v9fs_fid_destroy(ret);
338 goto clunk_fid;
341 return ret;
343 clunk_fid:
344 v9fs_t_clunk(v9ses, nfid);
346 error:
347 kfree(fcall);
348 return ERR_PTR(err);
351 struct inode *
352 v9fs_inode_from_fid(struct v9fs_session_info *v9ses, u32 fid,
353 struct super_block *sb)
355 int err, umode;
356 struct inode *ret;
357 struct v9fs_fcall *fcall;
359 ret = NULL;
360 err = v9fs_t_stat(v9ses, fid, &fcall);
361 if (err) {
362 PRINT_FCALL_ERROR("stat error", fcall);
363 goto error;
366 umode = p9mode2unixmode(v9ses, fcall->params.rstat.stat.mode);
367 ret = v9fs_get_inode(sb, umode);
368 if (IS_ERR(ret)) {
369 err = PTR_ERR(ret);
370 ret = NULL;
371 goto error;
374 v9fs_stat2inode(&fcall->params.rstat.stat, ret, sb);
375 kfree(fcall);
376 return ret;
378 error:
379 kfree(fcall);
380 if (ret)
381 iput(ret);
383 return ERR_PTR(err);
387 * v9fs_remove - helper function to remove files and directories
388 * @dir: directory inode that is being deleted
389 * @file: dentry that is being deleted
390 * @rmdir: removing a directory
394 static int v9fs_remove(struct inode *dir, struct dentry *file, int rmdir)
396 struct v9fs_fcall *fcall = NULL;
397 struct super_block *sb = NULL;
398 struct v9fs_session_info *v9ses = NULL;
399 struct v9fs_fid *v9fid = NULL;
400 struct inode *file_inode = NULL;
401 int fid = -1;
402 int result = 0;
404 dprintk(DEBUG_VFS, "inode: %p dentry: %p rmdir: %d\n", dir, file,
405 rmdir);
407 file_inode = file->d_inode;
408 sb = file_inode->i_sb;
409 v9ses = v9fs_inode2v9ses(file_inode);
410 v9fid = v9fs_fid_lookup(file);
412 if (!v9fid) {
413 dprintk(DEBUG_ERROR,
414 "no v9fs_fid\n");
415 return -EBADF;
418 fid = v9fid->fid;
419 if (fid < 0) {
420 dprintk(DEBUG_ERROR, "inode #%lu, no fid!\n",
421 file_inode->i_ino);
422 return -EBADF;
425 result = v9fs_t_remove(v9ses, fid, &fcall);
426 if (result < 0) {
427 PRINT_FCALL_ERROR("remove fails", fcall);
428 } else {
429 v9fs_put_idpool(fid, &v9ses->fidpool);
430 v9fs_fid_destroy(v9fid);
433 kfree(fcall);
434 return result;
437 static int
438 v9fs_open_created(struct inode *inode, struct file *file)
440 return 0;
444 * v9fs_vfs_create - VFS hook to create files
445 * @inode: directory inode that is being deleted
446 * @dentry: dentry that is being deleted
447 * @mode: create permissions
448 * @nd: path information
452 static int
453 v9fs_vfs_create(struct inode *dir, struct dentry *dentry, int mode,
454 struct nameidata *nd)
456 int err;
457 u32 fid, perm, iounit;
458 int flags;
459 struct v9fs_session_info *v9ses;
460 struct v9fs_fid *dfid, *vfid, *ffid;
461 struct inode *inode;
462 struct v9fs_qid qid;
463 struct file *filp;
465 inode = NULL;
466 vfid = NULL;
467 v9ses = v9fs_inode2v9ses(dir);
468 dfid = v9fs_fid_lookup(dentry->d_parent);
469 perm = unixmode2p9mode(v9ses, mode);
471 if (nd && nd->flags & LOOKUP_OPEN)
472 flags = nd->intent.open.flags - 1;
473 else
474 flags = O_RDWR;
476 err = v9fs_create(v9ses, dfid->fid, (char *) dentry->d_name.name,
477 perm, v9fs_uflags2omode(flags), &fid, &qid, &iounit);
479 if (err)
480 goto error;
482 vfid = v9fs_clone_walk(v9ses, dfid->fid, dentry);
483 if (IS_ERR(vfid)) {
484 err = PTR_ERR(vfid);
485 vfid = NULL;
486 goto error;
489 inode = v9fs_inode_from_fid(v9ses, vfid->fid, dir->i_sb);
490 if (IS_ERR(inode)) {
491 err = PTR_ERR(inode);
492 inode = NULL;
493 goto error;
496 dentry->d_op = &v9fs_dentry_operations;
497 d_instantiate(dentry, inode);
499 if (nd && nd->flags & LOOKUP_OPEN) {
500 ffid = v9fs_fid_create(v9ses, fid);
501 if (!ffid)
502 return -ENOMEM;
504 filp = lookup_instantiate_filp(nd, dentry, v9fs_open_created);
505 if (IS_ERR(filp)) {
506 v9fs_fid_destroy(ffid);
507 return PTR_ERR(filp);
510 ffid->rdir_pos = 0;
511 ffid->rdir_fcall = NULL;
512 ffid->fidopen = 1;
513 ffid->iounit = iounit;
514 ffid->filp = filp;
515 filp->private_data = ffid;
518 return 0;
520 error:
521 if (vfid)
522 v9fs_fid_destroy(vfid);
524 if (inode)
525 iput(inode);
527 return err;
531 * v9fs_vfs_mkdir - VFS mkdir hook to create a directory
532 * @inode: inode that is being unlinked
533 * @dentry: dentry that is being unlinked
534 * @mode: mode for new directory
538 static int v9fs_vfs_mkdir(struct inode *dir, struct dentry *dentry, int mode)
540 int err;
541 u32 fid, perm;
542 struct v9fs_session_info *v9ses;
543 struct v9fs_fid *dfid, *vfid;
544 struct inode *inode;
546 inode = NULL;
547 vfid = NULL;
548 v9ses = v9fs_inode2v9ses(dir);
549 dfid = v9fs_fid_lookup(dentry->d_parent);
550 perm = unixmode2p9mode(v9ses, mode | S_IFDIR);
552 err = v9fs_create(v9ses, dfid->fid, (char *) dentry->d_name.name,
553 perm, V9FS_OREAD, &fid, NULL, NULL);
555 if (err) {
556 dprintk(DEBUG_ERROR, "create error %d\n", err);
557 goto error;
560 err = v9fs_t_clunk(v9ses, fid);
561 if (err) {
562 dprintk(DEBUG_ERROR, "clunk error %d\n", err);
563 goto error;
566 vfid = v9fs_clone_walk(v9ses, dfid->fid, dentry);
567 if (IS_ERR(vfid)) {
568 err = PTR_ERR(vfid);
569 vfid = NULL;
570 goto error;
573 inode = v9fs_inode_from_fid(v9ses, vfid->fid, dir->i_sb);
574 if (IS_ERR(inode)) {
575 err = PTR_ERR(inode);
576 inode = NULL;
577 goto error;
580 dentry->d_op = &v9fs_dentry_operations;
581 d_instantiate(dentry, inode);
582 return 0;
584 error:
585 if (vfid)
586 v9fs_fid_destroy(vfid);
588 return err;
592 * v9fs_vfs_lookup - VFS lookup hook to "walk" to a new inode
593 * @dir: inode that is being walked from
594 * @dentry: dentry that is being walked to?
595 * @nameidata: path data
599 static struct dentry *v9fs_vfs_lookup(struct inode *dir, struct dentry *dentry,
600 struct nameidata *nameidata)
602 struct super_block *sb;
603 struct v9fs_session_info *v9ses;
604 struct v9fs_fid *dirfid;
605 struct v9fs_fid *fid;
606 struct inode *inode;
607 struct v9fs_fcall *fcall = NULL;
608 int dirfidnum = -1;
609 int newfid = -1;
610 int result = 0;
612 dprintk(DEBUG_VFS, "dir: %p dentry: (%s) %p nameidata: %p\n",
613 dir, dentry->d_name.name, dentry, nameidata);
615 sb = dir->i_sb;
616 v9ses = v9fs_inode2v9ses(dir);
617 dirfid = v9fs_fid_lookup(dentry->d_parent);
619 if (!dirfid) {
620 dprintk(DEBUG_ERROR, "no dirfid\n");
621 return ERR_PTR(-EINVAL);
624 dirfidnum = dirfid->fid;
626 if (dirfidnum < 0) {
627 dprintk(DEBUG_ERROR, "no dirfid for inode %p, #%lu\n",
628 dir, dir->i_ino);
629 return ERR_PTR(-EBADF);
632 newfid = v9fs_get_idpool(&v9ses->fidpool);
633 if (newfid < 0) {
634 eprintk(KERN_WARNING, "newfid fails!\n");
635 return ERR_PTR(-ENOSPC);
638 result = v9fs_t_walk(v9ses, dirfidnum, newfid,
639 (char *)dentry->d_name.name, NULL);
640 if (result < 0) {
641 v9fs_put_idpool(newfid, &v9ses->fidpool);
642 if (result == -ENOENT) {
643 d_add(dentry, NULL);
644 dprintk(DEBUG_VFS,
645 "Return negative dentry %p count %d\n",
646 dentry, atomic_read(&dentry->d_count));
647 return NULL;
649 dprintk(DEBUG_ERROR, "walk error:%d\n", result);
650 goto FreeFcall;
653 result = v9fs_t_stat(v9ses, newfid, &fcall);
654 if (result < 0) {
655 dprintk(DEBUG_ERROR, "stat error\n");
656 goto FreeFcall;
659 inode = v9fs_get_inode(sb, p9mode2unixmode(v9ses,
660 fcall->params.rstat.stat.mode));
662 if (IS_ERR(inode) && (PTR_ERR(inode) == -ENOSPC)) {
663 eprintk(KERN_WARNING, "inode alloc failes, returns %ld\n",
664 PTR_ERR(inode));
666 result = -ENOSPC;
667 goto FreeFcall;
670 inode->i_ino = v9fs_qid2ino(&fcall->params.rstat.stat.qid);
672 fid = v9fs_fid_create(v9ses, newfid);
673 if (fid == NULL) {
674 dprintk(DEBUG_ERROR, "couldn't insert\n");
675 result = -ENOMEM;
676 goto FreeFcall;
679 result = v9fs_fid_insert(fid, dentry);
680 if (result < 0)
681 goto FreeFcall;
683 fid->qid = fcall->params.rstat.stat.qid;
685 dentry->d_op = &v9fs_dentry_operations;
686 v9fs_stat2inode(&fcall->params.rstat.stat, inode, inode->i_sb);
688 d_add(dentry, inode);
689 kfree(fcall);
691 return NULL;
693 FreeFcall:
694 kfree(fcall);
695 return ERR_PTR(result);
699 * v9fs_vfs_unlink - VFS unlink hook to delete an inode
700 * @i: inode that is being unlinked
701 * @d: dentry that is being unlinked
705 static int v9fs_vfs_unlink(struct inode *i, struct dentry *d)
707 return v9fs_remove(i, d, 0);
711 * v9fs_vfs_rmdir - VFS unlink hook to delete a directory
712 * @i: inode that is being unlinked
713 * @d: dentry that is being unlinked
717 static int v9fs_vfs_rmdir(struct inode *i, struct dentry *d)
719 return v9fs_remove(i, d, 1);
723 * v9fs_vfs_rename - VFS hook to rename an inode
724 * @old_dir: old dir inode
725 * @old_dentry: old dentry
726 * @new_dir: new dir inode
727 * @new_dentry: new dentry
731 static int
732 v9fs_vfs_rename(struct inode *old_dir, struct dentry *old_dentry,
733 struct inode *new_dir, struct dentry *new_dentry)
735 struct inode *old_inode = old_dentry->d_inode;
736 struct v9fs_session_info *v9ses = v9fs_inode2v9ses(old_inode);
737 struct v9fs_fid *oldfid = v9fs_fid_lookup(old_dentry);
738 struct v9fs_fid *olddirfid =
739 v9fs_fid_lookup(old_dentry->d_parent);
740 struct v9fs_fid *newdirfid =
741 v9fs_fid_lookup(new_dentry->d_parent);
742 struct v9fs_wstat wstat;
743 struct v9fs_fcall *fcall = NULL;
744 int fid = -1;
745 int olddirfidnum = -1;
746 int newdirfidnum = -1;
747 int retval = 0;
749 dprintk(DEBUG_VFS, "\n");
751 if ((!oldfid) || (!olddirfid) || (!newdirfid)) {
752 dprintk(DEBUG_ERROR, "problem with arguments\n");
753 return -EBADF;
756 /* 9P can only handle file rename in the same directory */
757 if (memcmp(&olddirfid->qid, &newdirfid->qid, sizeof(newdirfid->qid))) {
758 dprintk(DEBUG_ERROR, "old dir and new dir are different\n");
759 retval = -EPERM;
760 goto FreeFcallnBail;
763 fid = oldfid->fid;
764 olddirfidnum = olddirfid->fid;
765 newdirfidnum = newdirfid->fid;
767 if (fid < 0) {
768 dprintk(DEBUG_ERROR, "no fid for old file #%lu\n",
769 old_inode->i_ino);
770 retval = -EBADF;
771 goto FreeFcallnBail;
774 v9fs_blank_wstat(&wstat);
775 wstat.muid = v9ses->name;
776 wstat.name = (char *) new_dentry->d_name.name;
778 retval = v9fs_t_wstat(v9ses, fid, &wstat, &fcall);
780 FreeFcallnBail:
781 if (retval < 0)
782 PRINT_FCALL_ERROR("wstat error", fcall);
784 kfree(fcall);
785 return retval;
789 * v9fs_vfs_getattr - retrieve file metadata
790 * @mnt - mount information
791 * @dentry - file to get attributes on
792 * @stat - metadata structure to populate
796 static int
797 v9fs_vfs_getattr(struct vfsmount *mnt, struct dentry *dentry,
798 struct kstat *stat)
800 struct v9fs_fcall *fcall = NULL;
801 struct v9fs_session_info *v9ses = v9fs_inode2v9ses(dentry->d_inode);
802 struct v9fs_fid *fid = v9fs_fid_lookup(dentry);
803 int err = -EPERM;
805 dprintk(DEBUG_VFS, "dentry: %p\n", dentry);
806 if (!fid) {
807 dprintk(DEBUG_ERROR,
808 "couldn't find fid associated with dentry\n");
809 return -EBADF;
812 err = v9fs_t_stat(v9ses, fid->fid, &fcall);
814 if (err < 0)
815 dprintk(DEBUG_ERROR, "stat error\n");
816 else {
817 v9fs_stat2inode(&fcall->params.rstat.stat, dentry->d_inode,
818 dentry->d_inode->i_sb);
819 generic_fillattr(dentry->d_inode, stat);
822 kfree(fcall);
823 return err;
827 * v9fs_vfs_setattr - set file metadata
828 * @dentry: file whose metadata to set
829 * @iattr: metadata assignment structure
833 static int v9fs_vfs_setattr(struct dentry *dentry, struct iattr *iattr)
835 struct v9fs_session_info *v9ses = v9fs_inode2v9ses(dentry->d_inode);
836 struct v9fs_fid *fid = v9fs_fid_lookup(dentry);
837 struct v9fs_fcall *fcall = NULL;
838 struct v9fs_wstat wstat;
839 int res = -EPERM;
841 dprintk(DEBUG_VFS, "\n");
843 if (!fid) {
844 dprintk(DEBUG_ERROR,
845 "Couldn't find fid associated with dentry\n");
846 return -EBADF;
849 v9fs_blank_wstat(&wstat);
850 if (iattr->ia_valid & ATTR_MODE)
851 wstat.mode = unixmode2p9mode(v9ses, iattr->ia_mode);
853 if (iattr->ia_valid & ATTR_MTIME)
854 wstat.mtime = iattr->ia_mtime.tv_sec;
856 if (iattr->ia_valid & ATTR_ATIME)
857 wstat.atime = iattr->ia_atime.tv_sec;
859 if (iattr->ia_valid & ATTR_SIZE)
860 wstat.length = iattr->ia_size;
862 if (v9ses->extended) {
863 if (iattr->ia_valid & ATTR_UID)
864 wstat.n_uid = iattr->ia_uid;
866 if (iattr->ia_valid & ATTR_GID)
867 wstat.n_gid = iattr->ia_gid;
870 res = v9fs_t_wstat(v9ses, fid->fid, &wstat, &fcall);
872 if (res < 0)
873 PRINT_FCALL_ERROR("wstat error", fcall);
875 kfree(fcall);
876 if (res >= 0)
877 res = inode_setattr(dentry->d_inode, iattr);
879 return res;
883 * v9fs_stat2inode - populate an inode structure with mistat info
884 * @stat: Plan 9 metadata (mistat) structure
885 * @inode: inode to populate
886 * @sb: superblock of filesystem
890 void
891 v9fs_stat2inode(struct v9fs_stat *stat, struct inode *inode,
892 struct super_block *sb)
894 int n;
895 char ext[32];
896 struct v9fs_session_info *v9ses = sb->s_fs_info;
898 inode->i_nlink = 1;
900 inode->i_atime.tv_sec = stat->atime;
901 inode->i_mtime.tv_sec = stat->mtime;
902 inode->i_ctime.tv_sec = stat->mtime;
904 inode->i_uid = v9ses->uid;
905 inode->i_gid = v9ses->gid;
907 if (v9ses->extended) {
908 inode->i_uid = stat->n_uid;
909 inode->i_gid = stat->n_gid;
912 inode->i_mode = p9mode2unixmode(v9ses, stat->mode);
913 if ((S_ISBLK(inode->i_mode)) || (S_ISCHR(inode->i_mode))) {
914 char type = 0;
915 int major = -1;
916 int minor = -1;
918 n = stat->extension.len;
919 if (n > sizeof(ext)-1)
920 n = sizeof(ext)-1;
921 memmove(ext, stat->extension.str, n);
922 ext[n] = 0;
923 sscanf(ext, "%c %u %u", &type, &major, &minor);
924 switch (type) {
925 case 'c':
926 inode->i_mode &= ~S_IFBLK;
927 inode->i_mode |= S_IFCHR;
928 break;
929 case 'b':
930 break;
931 default:
932 dprintk(DEBUG_ERROR, "Unknown special type %c (%.*s)\n",
933 type, stat->extension.len, stat->extension.str);
935 inode->i_rdev = MKDEV(major, minor);
936 } else
937 inode->i_rdev = 0;
939 inode->i_size = stat->length;
941 inode->i_blksize = sb->s_blocksize;
942 inode->i_blocks =
943 (inode->i_size + inode->i_blksize - 1) >> sb->s_blocksize_bits;
947 * v9fs_qid2ino - convert qid into inode number
948 * @qid: qid to hash
950 * BUG: potential for inode number collisions?
953 ino_t v9fs_qid2ino(struct v9fs_qid *qid)
955 u64 path = qid->path + 2;
956 ino_t i = 0;
958 if (sizeof(ino_t) == sizeof(path))
959 memcpy(&i, &path, sizeof(ino_t));
960 else
961 i = (ino_t) (path ^ (path >> 32));
963 return i;
967 * v9fs_readlink - read a symlink's location (internal version)
968 * @dentry: dentry for symlink
969 * @buffer: buffer to load symlink location into
970 * @buflen: length of buffer
974 static int v9fs_readlink(struct dentry *dentry, char *buffer, int buflen)
976 int retval = -EPERM;
978 struct v9fs_fcall *fcall = NULL;
979 struct v9fs_session_info *v9ses = v9fs_inode2v9ses(dentry->d_inode);
980 struct v9fs_fid *fid = v9fs_fid_lookup(dentry);
982 if (!fid) {
983 dprintk(DEBUG_ERROR, "could not resolve fid from dentry\n");
984 retval = -EBADF;
985 goto FreeFcall;
988 if (!v9ses->extended) {
989 retval = -EBADF;
990 dprintk(DEBUG_ERROR, "not extended\n");
991 goto FreeFcall;
994 dprintk(DEBUG_VFS, " %s\n", dentry->d_name.name);
995 retval = v9fs_t_stat(v9ses, fid->fid, &fcall);
997 if (retval < 0) {
998 dprintk(DEBUG_ERROR, "stat error\n");
999 goto FreeFcall;
1002 if (!fcall)
1003 return -EIO;
1005 if (!(fcall->params.rstat.stat.mode & V9FS_DMSYMLINK)) {
1006 retval = -EINVAL;
1007 goto FreeFcall;
1010 /* copy extension buffer into buffer */
1011 if (fcall->params.rstat.stat.extension.len < buflen)
1012 buflen = fcall->params.rstat.stat.extension.len;
1014 memcpy(buffer, fcall->params.rstat.stat.extension.str, buflen - 1);
1015 buffer[buflen-1] = 0;
1017 retval = buflen;
1019 FreeFcall:
1020 kfree(fcall);
1022 return retval;
1026 * v9fs_vfs_readlink - read a symlink's location
1027 * @dentry: dentry for symlink
1028 * @buf: buffer to load symlink location into
1029 * @buflen: length of buffer
1033 static int v9fs_vfs_readlink(struct dentry *dentry, char __user * buffer,
1034 int buflen)
1036 int retval;
1037 int ret;
1038 char *link = __getname();
1040 if (buflen > PATH_MAX)
1041 buflen = PATH_MAX;
1043 dprintk(DEBUG_VFS, " dentry: %s (%p)\n", dentry->d_iname, dentry);
1045 retval = v9fs_readlink(dentry, link, buflen);
1047 if (retval > 0) {
1048 if ((ret = copy_to_user(buffer, link, retval)) != 0) {
1049 dprintk(DEBUG_ERROR, "problem copying to user: %d\n",
1050 ret);
1051 retval = ret;
1055 __putname(link);
1056 return retval;
1060 * v9fs_vfs_follow_link - follow a symlink path
1061 * @dentry: dentry for symlink
1062 * @nd: nameidata
1066 static void *v9fs_vfs_follow_link(struct dentry *dentry, struct nameidata *nd)
1068 int len = 0;
1069 char *link = __getname();
1071 dprintk(DEBUG_VFS, "%s n", dentry->d_name.name);
1073 if (!link)
1074 link = ERR_PTR(-ENOMEM);
1075 else {
1076 len = v9fs_readlink(dentry, link, strlen(link));
1078 if (len < 0) {
1079 __putname(link);
1080 link = ERR_PTR(len);
1081 } else
1082 link[len] = 0;
1084 nd_set_link(nd, link);
1086 return NULL;
1090 * v9fs_vfs_put_link - release a symlink path
1091 * @dentry: dentry for symlink
1092 * @nd: nameidata
1096 static void v9fs_vfs_put_link(struct dentry *dentry, struct nameidata *nd, void *p)
1098 char *s = nd_get_link(nd);
1100 dprintk(DEBUG_VFS, " %s %s\n", dentry->d_name.name, s);
1101 if (!IS_ERR(s))
1102 __putname(s);
1105 static int v9fs_vfs_mkspecial(struct inode *dir, struct dentry *dentry,
1106 int mode, const char *extension)
1108 int err;
1109 u32 fid, perm;
1110 struct v9fs_session_info *v9ses;
1111 struct v9fs_fid *dfid, *vfid;
1112 struct inode *inode;
1113 struct v9fs_fcall *fcall;
1114 struct v9fs_wstat wstat;
1116 fcall = NULL;
1117 inode = NULL;
1118 vfid = NULL;
1119 v9ses = v9fs_inode2v9ses(dir);
1120 dfid = v9fs_fid_lookup(dentry->d_parent);
1121 perm = unixmode2p9mode(v9ses, mode);
1123 if (!v9ses->extended) {
1124 dprintk(DEBUG_ERROR, "not extended\n");
1125 return -EPERM;
1128 err = v9fs_create(v9ses, dfid->fid, (char *) dentry->d_name.name,
1129 perm, V9FS_OREAD, &fid, NULL, NULL);
1131 if (err)
1132 goto error;
1134 err = v9fs_t_clunk(v9ses, fid);
1135 if (err)
1136 goto error;
1138 vfid = v9fs_clone_walk(v9ses, dfid->fid, dentry);
1139 if (IS_ERR(vfid)) {
1140 err = PTR_ERR(vfid);
1141 vfid = NULL;
1142 goto error;
1145 inode = v9fs_inode_from_fid(v9ses, vfid->fid, dir->i_sb);
1146 if (IS_ERR(inode)) {
1147 err = PTR_ERR(inode);
1148 inode = NULL;
1149 goto error;
1152 /* issue a Twstat */
1153 v9fs_blank_wstat(&wstat);
1154 wstat.muid = v9ses->name;
1155 wstat.extension = (char *) extension;
1156 err = v9fs_t_wstat(v9ses, vfid->fid, &wstat, &fcall);
1157 if (err < 0) {
1158 PRINT_FCALL_ERROR("wstat error", fcall);
1159 goto error;
1162 kfree(fcall);
1163 dentry->d_op = &v9fs_dentry_operations;
1164 d_instantiate(dentry, inode);
1165 return 0;
1167 error:
1168 kfree(fcall);
1169 if (vfid)
1170 v9fs_fid_destroy(vfid);
1172 if (inode)
1173 iput(inode);
1175 return err;
1180 * v9fs_vfs_symlink - helper function to create symlinks
1181 * @dir: directory inode containing symlink
1182 * @dentry: dentry for symlink
1183 * @symname: symlink data
1185 * See 9P2000.u RFC for more information
1189 static int
1190 v9fs_vfs_symlink(struct inode *dir, struct dentry *dentry, const char *symname)
1192 dprintk(DEBUG_VFS, " %lu,%s,%s\n", dir->i_ino, dentry->d_name.name,
1193 symname);
1195 return v9fs_vfs_mkspecial(dir, dentry, S_IFLNK, symname);
1199 * v9fs_vfs_link - create a hardlink
1200 * @old_dentry: dentry for file to link to
1201 * @dir: inode destination for new link
1202 * @dentry: dentry for link
1206 /* XXX - lots of code dup'd from symlink and creates,
1207 * figure out a better reuse strategy
1210 static int
1211 v9fs_vfs_link(struct dentry *old_dentry, struct inode *dir,
1212 struct dentry *dentry)
1214 int retval;
1215 struct v9fs_fid *oldfid;
1216 char *name;
1218 dprintk(DEBUG_VFS, " %lu,%s,%s\n", dir->i_ino, dentry->d_name.name,
1219 old_dentry->d_name.name);
1221 oldfid = v9fs_fid_lookup(old_dentry);
1222 if (!oldfid) {
1223 dprintk(DEBUG_ERROR, "can't find oldfid\n");
1224 return -EPERM;
1227 name = __getname();
1228 sprintf(name, "hardlink(%d)\n", oldfid->fid);
1229 retval = v9fs_vfs_mkspecial(dir, dentry, V9FS_DMLINK, name);
1230 __putname(name);
1232 return retval;
1236 * v9fs_vfs_mknod - create a special file
1237 * @dir: inode destination for new link
1238 * @dentry: dentry for file
1239 * @mode: mode for creation
1240 * @dev_t: device associated with special file
1244 static int
1245 v9fs_vfs_mknod(struct inode *dir, struct dentry *dentry, int mode, dev_t rdev)
1247 int retval;
1248 char *name;
1250 dprintk(DEBUG_VFS, " %lu,%s mode: %x MAJOR: %u MINOR: %u\n", dir->i_ino,
1251 dentry->d_name.name, mode, MAJOR(rdev), MINOR(rdev));
1253 if (!new_valid_dev(rdev))
1254 return -EINVAL;
1256 name = __getname();
1257 /* build extension */
1258 if (S_ISBLK(mode))
1259 sprintf(name, "b %u %u", MAJOR(rdev), MINOR(rdev));
1260 else if (S_ISCHR(mode))
1261 sprintf(name, "c %u %u", MAJOR(rdev), MINOR(rdev));
1262 else if (S_ISFIFO(mode))
1263 *name = 0;
1264 else {
1265 __putname(name);
1266 return -EINVAL;
1269 retval = v9fs_vfs_mkspecial(dir, dentry, mode, name);
1270 __putname(name);
1272 return retval;
1275 static struct inode_operations v9fs_dir_inode_operations_ext = {
1276 .create = v9fs_vfs_create,
1277 .lookup = v9fs_vfs_lookup,
1278 .symlink = v9fs_vfs_symlink,
1279 .link = v9fs_vfs_link,
1280 .unlink = v9fs_vfs_unlink,
1281 .mkdir = v9fs_vfs_mkdir,
1282 .rmdir = v9fs_vfs_rmdir,
1283 .mknod = v9fs_vfs_mknod,
1284 .rename = v9fs_vfs_rename,
1285 .readlink = v9fs_vfs_readlink,
1286 .getattr = v9fs_vfs_getattr,
1287 .setattr = v9fs_vfs_setattr,
1290 static struct inode_operations v9fs_dir_inode_operations = {
1291 .create = v9fs_vfs_create,
1292 .lookup = v9fs_vfs_lookup,
1293 .unlink = v9fs_vfs_unlink,
1294 .mkdir = v9fs_vfs_mkdir,
1295 .rmdir = v9fs_vfs_rmdir,
1296 .mknod = v9fs_vfs_mknod,
1297 .rename = v9fs_vfs_rename,
1298 .getattr = v9fs_vfs_getattr,
1299 .setattr = v9fs_vfs_setattr,
1302 static struct inode_operations v9fs_file_inode_operations = {
1303 .getattr = v9fs_vfs_getattr,
1304 .setattr = v9fs_vfs_setattr,
1307 static struct inode_operations v9fs_symlink_inode_operations = {
1308 .readlink = v9fs_vfs_readlink,
1309 .follow_link = v9fs_vfs_follow_link,
1310 .put_link = v9fs_vfs_put_link,
1311 .getattr = v9fs_vfs_getattr,
1312 .setattr = v9fs_vfs_setattr,