Merge remote-tracking branch 'moduleh/module.h-split'
[linux-2.6/next.git] / fs / 9p / vfs_inode.c
blobe3c03db3c788149b83cd629468d782783f99e15b
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 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>
28 #include <linux/fs.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>
43 #include "v9fs.h"
44 #include "v9fs_vfs.h"
45 #include "fid.h"
46 #include "cache.h"
47 #include "xattr.h"
48 #include "acl.h"
50 static const struct inode_operations v9fs_dir_inode_operations;
51 static const struct inode_operations v9fs_dir_inode_operations_dotu;
52 static const struct inode_operations v9fs_file_inode_operations;
53 static const struct inode_operations v9fs_symlink_inode_operations;
55 /**
56 * unixmode2p9mode - convert unix mode bits to plan 9
57 * @v9ses: v9fs session information
58 * @mode: mode to convert
62 static int unixmode2p9mode(struct v9fs_session_info *v9ses, int mode)
64 int res;
65 res = mode & 0777;
66 if (S_ISDIR(mode))
67 res |= P9_DMDIR;
68 if (v9fs_proto_dotu(v9ses)) {
69 if (S_ISLNK(mode))
70 res |= P9_DMSYMLINK;
71 if (v9ses->nodev == 0) {
72 if (S_ISSOCK(mode))
73 res |= P9_DMSOCKET;
74 if (S_ISFIFO(mode))
75 res |= P9_DMNAMEDPIPE;
76 if (S_ISBLK(mode))
77 res |= P9_DMDEVICE;
78 if (S_ISCHR(mode))
79 res |= P9_DMDEVICE;
82 if ((mode & S_ISUID) == S_ISUID)
83 res |= P9_DMSETUID;
84 if ((mode & S_ISGID) == S_ISGID)
85 res |= P9_DMSETGID;
86 if ((mode & S_ISVTX) == S_ISVTX)
87 res |= P9_DMSETVTX;
88 if ((mode & P9_DMLINK))
89 res |= P9_DMLINK;
92 return res;
95 /**
96 * p9mode2unixmode- convert plan9 mode bits to unix mode bits
97 * @v9ses: v9fs session information
98 * @stat: p9_wstat from which mode need to be derived
99 * @rdev: major number, minor number in case of device files.
102 static int p9mode2unixmode(struct v9fs_session_info *v9ses,
103 struct p9_wstat *stat, dev_t *rdev)
105 int res;
106 int mode = stat->mode;
108 res = mode & S_IALLUGO;
109 *rdev = 0;
111 if ((mode & P9_DMDIR) == P9_DMDIR)
112 res |= S_IFDIR;
113 else if ((mode & P9_DMSYMLINK) && (v9fs_proto_dotu(v9ses)))
114 res |= S_IFLNK;
115 else if ((mode & P9_DMSOCKET) && (v9fs_proto_dotu(v9ses))
116 && (v9ses->nodev == 0))
117 res |= S_IFSOCK;
118 else if ((mode & P9_DMNAMEDPIPE) && (v9fs_proto_dotu(v9ses))
119 && (v9ses->nodev == 0))
120 res |= S_IFIFO;
121 else if ((mode & P9_DMDEVICE) && (v9fs_proto_dotu(v9ses))
122 && (v9ses->nodev == 0)) {
123 char type = 0, ext[32];
124 int major = -1, minor = -1;
126 strncpy(ext, stat->extension, sizeof(ext));
127 sscanf(ext, "%c %u %u", &type, &major, &minor);
128 switch (type) {
129 case 'c':
130 res |= S_IFCHR;
131 break;
132 case 'b':
133 res |= S_IFBLK;
134 break;
135 default:
136 P9_DPRINTK(P9_DEBUG_ERROR,
137 "Unknown special type %c %s\n", type,
138 stat->extension);
140 *rdev = MKDEV(major, minor);
141 } else
142 res |= S_IFREG;
144 if (v9fs_proto_dotu(v9ses)) {
145 if ((mode & P9_DMSETUID) == P9_DMSETUID)
146 res |= S_ISUID;
148 if ((mode & P9_DMSETGID) == P9_DMSETGID)
149 res |= S_ISGID;
151 if ((mode & P9_DMSETVTX) == P9_DMSETVTX)
152 res |= S_ISVTX;
154 return res;
158 * v9fs_uflags2omode- convert posix open flags to plan 9 mode bits
159 * @uflags: flags to convert
160 * @extended: if .u extensions are active
163 int v9fs_uflags2omode(int uflags, int extended)
165 int ret;
167 ret = 0;
168 switch (uflags&3) {
169 default:
170 case O_RDONLY:
171 ret = P9_OREAD;
172 break;
174 case O_WRONLY:
175 ret = P9_OWRITE;
176 break;
178 case O_RDWR:
179 ret = P9_ORDWR;
180 break;
183 if (uflags & O_TRUNC)
184 ret |= P9_OTRUNC;
186 if (extended) {
187 if (uflags & O_EXCL)
188 ret |= P9_OEXCL;
190 if (uflags & O_APPEND)
191 ret |= P9_OAPPEND;
194 return ret;
198 * v9fs_blank_wstat - helper function to setup a 9P stat structure
199 * @wstat: structure to initialize
203 void
204 v9fs_blank_wstat(struct p9_wstat *wstat)
206 wstat->type = ~0;
207 wstat->dev = ~0;
208 wstat->qid.type = ~0;
209 wstat->qid.version = ~0;
210 *((long long *)&wstat->qid.path) = ~0;
211 wstat->mode = ~0;
212 wstat->atime = ~0;
213 wstat->mtime = ~0;
214 wstat->length = ~0;
215 wstat->name = NULL;
216 wstat->uid = NULL;
217 wstat->gid = NULL;
218 wstat->muid = NULL;
219 wstat->n_uid = ~0;
220 wstat->n_gid = ~0;
221 wstat->n_muid = ~0;
222 wstat->extension = NULL;
226 * v9fs_alloc_inode - helper function to allocate an inode
229 struct inode *v9fs_alloc_inode(struct super_block *sb)
231 struct v9fs_inode *v9inode;
232 v9inode = (struct v9fs_inode *)kmem_cache_alloc(v9fs_inode_cache,
233 GFP_KERNEL);
234 if (!v9inode)
235 return NULL;
236 #ifdef CONFIG_9P_FSCACHE
237 v9inode->fscache = NULL;
238 spin_lock_init(&v9inode->fscache_lock);
239 #endif
240 v9inode->writeback_fid = NULL;
241 v9inode->cache_validity = 0;
242 mutex_init(&v9inode->v_mutex);
243 return &v9inode->vfs_inode;
247 * v9fs_destroy_inode - destroy an inode
251 static void v9fs_i_callback(struct rcu_head *head)
253 struct inode *inode = container_of(head, struct inode, i_rcu);
254 INIT_LIST_HEAD(&inode->i_dentry);
255 kmem_cache_free(v9fs_inode_cache, V9FS_I(inode));
258 void v9fs_destroy_inode(struct inode *inode)
260 call_rcu(&inode->i_rcu, v9fs_i_callback);
263 int v9fs_init_inode(struct v9fs_session_info *v9ses,
264 struct inode *inode, int mode, dev_t rdev)
266 int err = 0;
268 inode_init_owner(inode, NULL, mode);
269 inode->i_blocks = 0;
270 inode->i_rdev = rdev;
271 inode->i_atime = inode->i_mtime = inode->i_ctime = CURRENT_TIME;
272 inode->i_mapping->a_ops = &v9fs_addr_operations;
274 switch (mode & S_IFMT) {
275 case S_IFIFO:
276 case S_IFBLK:
277 case S_IFCHR:
278 case S_IFSOCK:
279 if (v9fs_proto_dotl(v9ses)) {
280 inode->i_op = &v9fs_file_inode_operations_dotl;
281 inode->i_fop = &v9fs_file_operations_dotl;
282 } else if (v9fs_proto_dotu(v9ses)) {
283 inode->i_op = &v9fs_file_inode_operations;
284 inode->i_fop = &v9fs_file_operations;
285 } else {
286 P9_DPRINTK(P9_DEBUG_ERROR,
287 "special files without extended mode\n");
288 err = -EINVAL;
289 goto error;
291 init_special_inode(inode, inode->i_mode, inode->i_rdev);
292 break;
293 case S_IFREG:
294 if (v9fs_proto_dotl(v9ses)) {
295 inode->i_op = &v9fs_file_inode_operations_dotl;
296 if (v9ses->cache)
297 inode->i_fop =
298 &v9fs_cached_file_operations_dotl;
299 else
300 inode->i_fop = &v9fs_file_operations_dotl;
301 } else {
302 inode->i_op = &v9fs_file_inode_operations;
303 if (v9ses->cache)
304 inode->i_fop = &v9fs_cached_file_operations;
305 else
306 inode->i_fop = &v9fs_file_operations;
309 break;
310 case S_IFLNK:
311 if (!v9fs_proto_dotu(v9ses) && !v9fs_proto_dotl(v9ses)) {
312 P9_DPRINTK(P9_DEBUG_ERROR, "extended modes used with "
313 "legacy protocol.\n");
314 err = -EINVAL;
315 goto error;
318 if (v9fs_proto_dotl(v9ses))
319 inode->i_op = &v9fs_symlink_inode_operations_dotl;
320 else
321 inode->i_op = &v9fs_symlink_inode_operations;
323 break;
324 case S_IFDIR:
325 inc_nlink(inode);
326 if (v9fs_proto_dotl(v9ses))
327 inode->i_op = &v9fs_dir_inode_operations_dotl;
328 else if (v9fs_proto_dotu(v9ses))
329 inode->i_op = &v9fs_dir_inode_operations_dotu;
330 else
331 inode->i_op = &v9fs_dir_inode_operations;
333 if (v9fs_proto_dotl(v9ses))
334 inode->i_fop = &v9fs_dir_operations_dotl;
335 else
336 inode->i_fop = &v9fs_dir_operations;
338 break;
339 default:
340 P9_DPRINTK(P9_DEBUG_ERROR, "BAD mode 0x%x S_IFMT 0x%x\n",
341 mode, mode & S_IFMT);
342 err = -EINVAL;
343 goto error;
345 error:
346 return err;
351 * v9fs_get_inode - helper function to setup an inode
352 * @sb: superblock
353 * @mode: mode to setup inode with
357 struct inode *v9fs_get_inode(struct super_block *sb, int mode, dev_t rdev)
359 int err;
360 struct inode *inode;
361 struct v9fs_session_info *v9ses = sb->s_fs_info;
363 P9_DPRINTK(P9_DEBUG_VFS, "super block: %p mode: %o\n", sb, mode);
365 inode = new_inode(sb);
366 if (!inode) {
367 P9_EPRINTK(KERN_WARNING, "Problem allocating inode\n");
368 return ERR_PTR(-ENOMEM);
370 err = v9fs_init_inode(v9ses, inode, mode, rdev);
371 if (err) {
372 iput(inode);
373 return ERR_PTR(err);
375 return inode;
379 static struct v9fs_fid*
380 v9fs_clone_walk(struct v9fs_session_info *v9ses, u32 fid, struct dentry *dentry)
382 int err;
383 int nfid;
384 struct v9fs_fid *ret;
385 struct v9fs_fcall *fcall;
387 nfid = v9fs_get_idpool(&v9ses->fidpool);
388 if (nfid < 0) {
389 eprintk(KERN_WARNING, "no free fids available\n");
390 return ERR_PTR(-ENOSPC);
393 err = v9fs_t_walk(v9ses, fid, nfid, (char *) dentry->d_name.name,
394 &fcall);
396 if (err < 0) {
397 if (fcall && fcall->id == RWALK)
398 goto clunk_fid;
400 PRINT_FCALL_ERROR("walk error", fcall);
401 v9fs_put_idpool(nfid, &v9ses->fidpool);
402 goto error;
405 kfree(fcall);
406 fcall = NULL;
407 ret = v9fs_fid_create(v9ses, nfid);
408 if (!ret) {
409 err = -ENOMEM;
410 goto clunk_fid;
413 err = v9fs_fid_insert(ret, dentry);
414 if (err < 0) {
415 v9fs_fid_destroy(ret);
416 goto clunk_fid;
419 return ret;
421 clunk_fid:
422 v9fs_t_clunk(v9ses, nfid);
424 error:
425 kfree(fcall);
426 return ERR_PTR(err);
432 * v9fs_clear_inode - release an inode
433 * @inode: inode to release
436 void v9fs_evict_inode(struct inode *inode)
438 struct v9fs_inode *v9inode = V9FS_I(inode);
440 truncate_inode_pages(inode->i_mapping, 0);
441 end_writeback(inode);
442 filemap_fdatawrite(inode->i_mapping);
444 #ifdef CONFIG_9P_FSCACHE
445 v9fs_cache_inode_put_cookie(inode);
446 #endif
447 /* clunk the fid stashed in writeback_fid */
448 if (v9inode->writeback_fid) {
449 p9_client_clunk(v9inode->writeback_fid);
450 v9inode->writeback_fid = NULL;
454 static int v9fs_test_inode(struct inode *inode, void *data)
456 int umode;
457 dev_t rdev;
458 struct v9fs_inode *v9inode = V9FS_I(inode);
459 struct p9_wstat *st = (struct p9_wstat *)data;
460 struct v9fs_session_info *v9ses = v9fs_inode2v9ses(inode);
462 umode = p9mode2unixmode(v9ses, st, &rdev);
463 /* don't match inode of different type */
464 if ((inode->i_mode & S_IFMT) != (umode & S_IFMT))
465 return 0;
467 /* compare qid details */
468 if (memcmp(&v9inode->qid.version,
469 &st->qid.version, sizeof(v9inode->qid.version)))
470 return 0;
472 if (v9inode->qid.type != st->qid.type)
473 return 0;
474 return 1;
477 static int v9fs_test_new_inode(struct inode *inode, void *data)
479 return 0;
482 static int v9fs_set_inode(struct inode *inode, void *data)
484 struct v9fs_inode *v9inode = V9FS_I(inode);
485 struct p9_wstat *st = (struct p9_wstat *)data;
487 memcpy(&v9inode->qid, &st->qid, sizeof(st->qid));
488 return 0;
491 static struct inode *v9fs_qid_iget(struct super_block *sb,
492 struct p9_qid *qid,
493 struct p9_wstat *st,
494 int new)
496 dev_t rdev;
497 int retval, umode;
498 unsigned long i_ino;
499 struct inode *inode;
500 struct v9fs_session_info *v9ses = sb->s_fs_info;
501 int (*test)(struct inode *, void *);
503 if (new)
504 test = v9fs_test_new_inode;
505 else
506 test = v9fs_test_inode;
508 i_ino = v9fs_qid2ino(qid);
509 inode = iget5_locked(sb, i_ino, test, v9fs_set_inode, st);
510 if (!inode)
511 return ERR_PTR(-ENOMEM);
512 if (!(inode->i_state & I_NEW))
513 return inode;
515 * initialize the inode with the stat info
516 * FIXME!! we may need support for stale inodes
517 * later.
519 inode->i_ino = i_ino;
520 umode = p9mode2unixmode(v9ses, st, &rdev);
521 retval = v9fs_init_inode(v9ses, inode, umode, rdev);
522 if (retval)
523 goto error;
525 v9fs_stat2inode(st, inode, sb);
526 #ifdef CONFIG_9P_FSCACHE
527 v9fs_cache_inode_get_cookie(inode);
528 #endif
529 unlock_new_inode(inode);
530 return inode;
531 error:
532 unlock_new_inode(inode);
533 iput(inode);
534 return ERR_PTR(retval);
538 struct inode *
539 v9fs_inode_from_fid(struct v9fs_session_info *v9ses, struct p9_fid *fid,
540 struct super_block *sb, int new)
542 struct p9_wstat *st;
543 struct inode *inode = NULL;
545 st = p9_client_stat(fid);
546 if (IS_ERR(st))
547 return ERR_CAST(st);
549 inode = v9fs_qid_iget(sb, &st->qid, st, new);
550 p9stat_free(st);
551 kfree(st);
552 return inode;
556 * v9fs_at_to_dotl_flags- convert Linux specific AT flags to
557 * plan 9 AT flag.
558 * @flags: flags to convert
560 static int v9fs_at_to_dotl_flags(int flags)
562 int rflags = 0;
563 if (flags & AT_REMOVEDIR)
564 rflags |= P9_DOTL_AT_REMOVEDIR;
565 return rflags;
569 * v9fs_remove - helper function to remove files and directories
570 * @dir: directory inode that is being deleted
571 * @dentry: dentry that is being deleted
572 * @rmdir: removing a directory
576 static int v9fs_remove(struct inode *dir, struct dentry *dentry, int flags)
578 struct inode *inode;
579 int retval = -EOPNOTSUPP;
580 struct p9_fid *v9fid, *dfid;
581 struct v9fs_session_info *v9ses;
583 P9_DPRINTK(P9_DEBUG_VFS, "inode: %p dentry: %p rmdir: %x\n",
584 dir, dentry, flags);
586 v9ses = v9fs_inode2v9ses(dir);
587 inode = dentry->d_inode;
588 dfid = v9fs_fid_lookup(dentry->d_parent);
589 if (IS_ERR(dfid)) {
590 retval = PTR_ERR(dfid);
591 P9_DPRINTK(P9_DEBUG_VFS, "fid lookup failed %d\n", retval);
592 return retval;
594 if (v9fs_proto_dotl(v9ses))
595 retval = p9_client_unlinkat(dfid, dentry->d_name.name,
596 v9fs_at_to_dotl_flags(flags));
597 if (retval == -EOPNOTSUPP) {
598 /* Try the one based on path */
599 v9fid = v9fs_fid_clone(dentry);
600 if (IS_ERR(v9fid))
601 return PTR_ERR(v9fid);
602 retval = p9_client_remove(v9fid);
604 if (!retval) {
606 * directories on unlink should have zero
607 * link count
609 if (flags & AT_REMOVEDIR) {
610 clear_nlink(inode);
611 drop_nlink(dir);
612 } else
613 drop_nlink(inode);
615 v9fs_invalidate_inode_attr(inode);
616 v9fs_invalidate_inode_attr(dir);
618 return retval;
622 * v9fs_create - Create a file
623 * @v9ses: session information
624 * @dir: directory that dentry is being created in
625 * @dentry: dentry that is being created
626 * @extension: 9p2000.u extension string to support devices, etc.
627 * @perm: create permissions
628 * @mode: open mode
631 static struct p9_fid *
632 v9fs_create(struct v9fs_session_info *v9ses, struct inode *dir,
633 struct dentry *dentry, char *extension, u32 perm, u8 mode)
635 int err;
636 char *name;
637 struct p9_fid *dfid, *ofid, *fid;
638 struct inode *inode;
640 P9_DPRINTK(P9_DEBUG_VFS, "name %s\n", dentry->d_name.name);
642 err = 0;
643 ofid = NULL;
644 fid = NULL;
645 name = (char *) dentry->d_name.name;
646 dfid = v9fs_fid_lookup(dentry->d_parent);
647 if (IS_ERR(dfid)) {
648 err = PTR_ERR(dfid);
649 P9_DPRINTK(P9_DEBUG_VFS, "fid lookup failed %d\n", err);
650 return ERR_PTR(err);
653 /* clone a fid to use for creation */
654 ofid = p9_client_walk(dfid, 0, NULL, 1);
655 if (IS_ERR(ofid)) {
656 err = PTR_ERR(ofid);
657 P9_DPRINTK(P9_DEBUG_VFS, "p9_client_walk failed %d\n", err);
658 return ERR_PTR(err);
661 err = p9_client_fcreate(ofid, name, perm, mode, extension);
662 if (err < 0) {
663 P9_DPRINTK(P9_DEBUG_VFS, "p9_client_fcreate failed %d\n", err);
664 goto error;
667 /* now walk from the parent so we can get unopened fid */
668 fid = p9_client_walk(dfid, 1, &name, 1);
669 if (IS_ERR(fid)) {
670 err = PTR_ERR(fid);
671 P9_DPRINTK(P9_DEBUG_VFS, "p9_client_walk failed %d\n", err);
672 fid = NULL;
673 goto error;
676 /* instantiate inode and assign the unopened fid to the dentry */
677 inode = v9fs_get_new_inode_from_fid(v9ses, fid, dir->i_sb);
678 if (IS_ERR(inode)) {
679 err = PTR_ERR(inode);
680 P9_DPRINTK(P9_DEBUG_VFS, "inode creation failed %d\n", err);
681 goto error;
683 err = v9fs_fid_add(dentry, fid);
684 if (err < 0)
685 goto error;
686 d_instantiate(dentry, inode);
687 return ofid;
688 error:
689 if (ofid)
690 p9_client_clunk(ofid);
692 if (fid)
693 p9_client_clunk(fid);
695 return ERR_PTR(err);
699 * v9fs_vfs_create - VFS hook to create files
700 * @dir: directory inode that is being created
701 * @dentry: dentry that is being deleted
702 * @mode: create permissions
703 * @nd: path information
707 static int
708 v9fs_vfs_create(struct inode *dir, struct dentry *dentry, int mode,
709 struct nameidata *nd)
711 int err;
712 u32 perm;
713 int flags;
714 struct file *filp;
715 struct v9fs_inode *v9inode;
716 struct v9fs_session_info *v9ses;
717 struct p9_fid *fid, *inode_fid;
719 err = 0;
720 fid = NULL;
721 v9ses = v9fs_inode2v9ses(dir);
722 perm = unixmode2p9mode(v9ses, mode);
723 if (nd)
724 flags = nd->intent.open.flags;
725 else
726 flags = O_RDWR;
728 fid = v9fs_create(v9ses, dir, dentry, NULL, perm,
729 v9fs_uflags2omode(flags,
730 v9fs_proto_dotu(v9ses)));
731 if (IS_ERR(fid)) {
732 err = PTR_ERR(fid);
733 fid = NULL;
734 goto error;
737 v9fs_invalidate_inode_attr(dir);
738 /* if we are opening a file, assign the open fid to the file */
739 if (nd) {
740 v9inode = V9FS_I(dentry->d_inode);
741 mutex_lock(&v9inode->v_mutex);
742 if (v9ses->cache && !v9inode->writeback_fid &&
743 ((flags & O_ACCMODE) != O_RDONLY)) {
745 * clone a fid and add it to writeback_fid
746 * we do it during open time instead of
747 * page dirty time via write_begin/page_mkwrite
748 * because we want write after unlink usecase
749 * to work.
751 inode_fid = v9fs_writeback_fid(dentry);
752 if (IS_ERR(inode_fid)) {
753 err = PTR_ERR(inode_fid);
754 mutex_unlock(&v9inode->v_mutex);
755 goto error;
757 v9inode->writeback_fid = (void *) inode_fid;
759 mutex_unlock(&v9inode->v_mutex);
760 filp = lookup_instantiate_filp(nd, dentry, generic_file_open);
761 if (IS_ERR(filp)) {
762 err = PTR_ERR(filp);
763 goto error;
766 filp->private_data = fid;
767 #ifdef CONFIG_9P_FSCACHE
768 if (v9ses->cache)
769 v9fs_cache_inode_set_cookie(dentry->d_inode, filp);
770 #endif
771 } else
772 p9_client_clunk(fid);
774 return 0;
776 error:
777 if (fid)
778 p9_client_clunk(fid);
780 return err;
784 * v9fs_vfs_mkdir - VFS mkdir hook to create a directory
785 * @dir: inode that is being unlinked
786 * @dentry: dentry that is being unlinked
787 * @mode: mode for new directory
791 static int v9fs_vfs_mkdir(struct inode *dir, struct dentry *dentry, int mode)
793 int err;
794 u32 perm;
795 struct p9_fid *fid;
796 struct v9fs_session_info *v9ses;
798 P9_DPRINTK(P9_DEBUG_VFS, "name %s\n", dentry->d_name.name);
799 err = 0;
800 v9ses = v9fs_inode2v9ses(dir);
801 perm = unixmode2p9mode(v9ses, mode | S_IFDIR);
802 fid = v9fs_create(v9ses, dir, dentry, NULL, perm, P9_OREAD);
803 if (IS_ERR(fid)) {
804 err = PTR_ERR(fid);
805 fid = NULL;
806 } else {
807 inc_nlink(dir);
808 v9fs_invalidate_inode_attr(dir);
811 if (fid)
812 p9_client_clunk(fid);
814 return err;
818 * v9fs_vfs_lookup - VFS lookup hook to "walk" to a new inode
819 * @dir: inode that is being walked from
820 * @dentry: dentry that is being walked to?
821 * @nameidata: path data
825 struct dentry *v9fs_vfs_lookup(struct inode *dir, struct dentry *dentry,
826 struct nameidata *nameidata)
828 struct dentry *res;
829 struct super_block *sb;
830 struct v9fs_session_info *v9ses;
831 struct p9_fid *dfid, *fid;
832 struct inode *inode;
833 char *name;
834 int result = 0;
836 P9_DPRINTK(P9_DEBUG_VFS, "dir: %p dentry: (%s) %p nameidata: %p\n",
837 dir, dentry->d_name.name, dentry, nameidata);
839 if (dentry->d_name.len > NAME_MAX)
840 return ERR_PTR(-ENAMETOOLONG);
842 sb = dir->i_sb;
843 v9ses = v9fs_inode2v9ses(dir);
844 /* We can walk d_parent because we hold the dir->i_mutex */
845 dfid = v9fs_fid_lookup(dentry->d_parent);
846 if (IS_ERR(dfid))
847 return ERR_CAST(dfid);
849 name = (char *) dentry->d_name.name;
850 fid = p9_client_walk(dfid, 1, &name, 1);
851 if (IS_ERR(fid)) {
852 result = PTR_ERR(fid);
853 if (result == -ENOENT) {
854 inode = NULL;
855 goto inst_out;
858 return ERR_PTR(result);
861 * Make sure we don't use a wrong inode due to parallel
862 * unlink. For cached mode create calls request for new
863 * inode. But with cache disabled, lookup should do this.
865 if (v9ses->cache)
866 inode = v9fs_get_inode_from_fid(v9ses, fid, dir->i_sb);
867 else
868 inode = v9fs_get_new_inode_from_fid(v9ses, fid, dir->i_sb);
869 if (IS_ERR(inode)) {
870 result = PTR_ERR(inode);
871 inode = NULL;
872 goto error;
874 result = v9fs_fid_add(dentry, fid);
875 if (result < 0)
876 goto error_iput;
877 inst_out:
879 * If we had a rename on the server and a parallel lookup
880 * for the new name, then make sure we instantiate with
881 * the new name. ie look up for a/b, while on server somebody
882 * moved b under k and client parallely did a lookup for
883 * k/b.
885 res = d_materialise_unique(dentry, inode);
886 if (!IS_ERR(res))
887 return res;
888 result = PTR_ERR(res);
889 error_iput:
890 iput(inode);
891 error:
892 p9_client_clunk(fid);
894 return ERR_PTR(result);
898 * v9fs_vfs_unlink - VFS unlink hook to delete an inode
899 * @i: inode that is being unlinked
900 * @d: dentry that is being unlinked
904 int v9fs_vfs_unlink(struct inode *i, struct dentry *d)
906 return v9fs_remove(i, d, 0);
910 * v9fs_vfs_rmdir - VFS unlink hook to delete a directory
911 * @i: inode that is being unlinked
912 * @d: dentry that is being unlinked
916 int v9fs_vfs_rmdir(struct inode *i, struct dentry *d)
918 return v9fs_remove(i, d, AT_REMOVEDIR);
922 * v9fs_vfs_rename - VFS hook to rename an inode
923 * @old_dir: old dir inode
924 * @old_dentry: old dentry
925 * @new_dir: new dir inode
926 * @new_dentry: new dentry
931 v9fs_vfs_rename(struct inode *old_dir, struct dentry *old_dentry,
932 struct inode *new_dir, struct dentry *new_dentry)
934 int retval;
935 struct inode *old_inode;
936 struct inode *new_inode;
937 struct v9fs_session_info *v9ses;
938 struct p9_fid *oldfid;
939 struct p9_fid *olddirfid;
940 struct p9_fid *newdirfid;
941 struct p9_wstat wstat;
943 P9_DPRINTK(P9_DEBUG_VFS, "\n");
944 retval = 0;
945 old_inode = old_dentry->d_inode;
946 new_inode = new_dentry->d_inode;
947 v9ses = v9fs_inode2v9ses(old_inode);
948 oldfid = v9fs_fid_lookup(old_dentry);
949 if (IS_ERR(oldfid))
950 return PTR_ERR(oldfid);
952 olddirfid = v9fs_fid_clone(old_dentry->d_parent);
953 if (IS_ERR(olddirfid)) {
954 retval = PTR_ERR(olddirfid);
955 goto done;
958 newdirfid = v9fs_fid_clone(new_dentry->d_parent);
959 if (IS_ERR(newdirfid)) {
960 retval = PTR_ERR(newdirfid);
961 goto clunk_olddir;
964 down_write(&v9ses->rename_sem);
965 if (v9fs_proto_dotl(v9ses)) {
966 retval = p9_client_renameat(olddirfid, old_dentry->d_name.name,
967 newdirfid, new_dentry->d_name.name);
968 if (retval == -EOPNOTSUPP)
969 retval = p9_client_rename(oldfid, newdirfid,
970 new_dentry->d_name.name);
971 if (retval != -EOPNOTSUPP)
972 goto clunk_newdir;
974 if (old_dentry->d_parent != new_dentry->d_parent) {
976 * 9P .u can only handle file rename in the same directory
979 P9_DPRINTK(P9_DEBUG_ERROR,
980 "old dir and new dir are different\n");
981 retval = -EXDEV;
982 goto clunk_newdir;
984 v9fs_blank_wstat(&wstat);
985 wstat.muid = v9ses->uname;
986 wstat.name = (char *) new_dentry->d_name.name;
987 retval = p9_client_wstat(oldfid, &wstat);
989 clunk_newdir:
990 if (!retval) {
991 if (new_inode) {
992 if (S_ISDIR(new_inode->i_mode))
993 clear_nlink(new_inode);
994 else
995 drop_nlink(new_inode);
997 if (S_ISDIR(old_inode->i_mode)) {
998 if (!new_inode)
999 inc_nlink(new_dir);
1000 drop_nlink(old_dir);
1002 v9fs_invalidate_inode_attr(old_inode);
1003 v9fs_invalidate_inode_attr(old_dir);
1004 v9fs_invalidate_inode_attr(new_dir);
1006 /* successful rename */
1007 d_move(old_dentry, new_dentry);
1009 up_write(&v9ses->rename_sem);
1010 p9_client_clunk(newdirfid);
1012 clunk_olddir:
1013 p9_client_clunk(olddirfid);
1015 done:
1016 return retval;
1020 * v9fs_vfs_getattr - retrieve file metadata
1021 * @mnt: mount information
1022 * @dentry: file to get attributes on
1023 * @stat: metadata structure to populate
1027 static int
1028 v9fs_vfs_getattr(struct vfsmount *mnt, struct dentry *dentry,
1029 struct kstat *stat)
1031 int err;
1032 struct v9fs_session_info *v9ses;
1033 struct p9_fid *fid;
1034 struct p9_wstat *st;
1036 P9_DPRINTK(P9_DEBUG_VFS, "dentry: %p\n", dentry);
1037 err = -EPERM;
1038 v9ses = v9fs_dentry2v9ses(dentry);
1039 if (v9ses->cache == CACHE_LOOSE || v9ses->cache == CACHE_FSCACHE) {
1040 generic_fillattr(dentry->d_inode, stat);
1041 return 0;
1043 fid = v9fs_fid_lookup(dentry);
1044 if (IS_ERR(fid))
1045 return PTR_ERR(fid);
1047 st = p9_client_stat(fid);
1048 if (IS_ERR(st))
1049 return PTR_ERR(st);
1051 v9fs_stat2inode(st, dentry->d_inode, dentry->d_inode->i_sb);
1052 generic_fillattr(dentry->d_inode, stat);
1054 p9stat_free(st);
1055 kfree(st);
1056 return 0;
1060 * v9fs_vfs_setattr - set file metadata
1061 * @dentry: file whose metadata to set
1062 * @iattr: metadata assignment structure
1066 static int v9fs_vfs_setattr(struct dentry *dentry, struct iattr *iattr)
1068 int retval;
1069 struct v9fs_session_info *v9ses;
1070 struct p9_fid *fid;
1071 struct p9_wstat wstat;
1073 P9_DPRINTK(P9_DEBUG_VFS, "\n");
1074 retval = inode_change_ok(dentry->d_inode, iattr);
1075 if (retval)
1076 return retval;
1078 retval = -EPERM;
1079 v9ses = v9fs_dentry2v9ses(dentry);
1080 fid = v9fs_fid_lookup(dentry);
1081 if(IS_ERR(fid))
1082 return PTR_ERR(fid);
1084 v9fs_blank_wstat(&wstat);
1085 if (iattr->ia_valid & ATTR_MODE)
1086 wstat.mode = unixmode2p9mode(v9ses, iattr->ia_mode);
1088 if (iattr->ia_valid & ATTR_MTIME)
1089 wstat.mtime = iattr->ia_mtime.tv_sec;
1091 if (iattr->ia_valid & ATTR_ATIME)
1092 wstat.atime = iattr->ia_atime.tv_sec;
1094 if (iattr->ia_valid & ATTR_SIZE)
1095 wstat.length = iattr->ia_size;
1097 if (v9fs_proto_dotu(v9ses)) {
1098 if (iattr->ia_valid & ATTR_UID)
1099 wstat.n_uid = iattr->ia_uid;
1101 if (iattr->ia_valid & ATTR_GID)
1102 wstat.n_gid = iattr->ia_gid;
1105 /* Write all dirty data */
1106 if (S_ISREG(dentry->d_inode->i_mode))
1107 filemap_write_and_wait(dentry->d_inode->i_mapping);
1109 retval = p9_client_wstat(fid, &wstat);
1110 if (retval < 0)
1111 return retval;
1113 if ((iattr->ia_valid & ATTR_SIZE) &&
1114 iattr->ia_size != i_size_read(dentry->d_inode))
1115 truncate_setsize(dentry->d_inode, iattr->ia_size);
1117 v9fs_invalidate_inode_attr(dentry->d_inode);
1119 setattr_copy(dentry->d_inode, iattr);
1120 mark_inode_dirty(dentry->d_inode);
1121 return 0;
1125 * v9fs_stat2inode - populate an inode structure with mistat info
1126 * @stat: Plan 9 metadata (mistat) structure
1127 * @inode: inode to populate
1128 * @sb: superblock of filesystem
1132 void
1133 v9fs_stat2inode(struct p9_wstat *stat, struct inode *inode,
1134 struct super_block *sb)
1136 mode_t mode;
1137 char ext[32];
1138 char tag_name[14];
1139 unsigned int i_nlink;
1140 struct v9fs_session_info *v9ses = sb->s_fs_info;
1141 struct v9fs_inode *v9inode = V9FS_I(inode);
1143 inode->i_nlink = 1;
1145 inode->i_atime.tv_sec = stat->atime;
1146 inode->i_mtime.tv_sec = stat->mtime;
1147 inode->i_ctime.tv_sec = stat->mtime;
1149 inode->i_uid = v9ses->dfltuid;
1150 inode->i_gid = v9ses->dfltgid;
1152 if (v9fs_proto_dotu(v9ses)) {
1153 inode->i_uid = stat->n_uid;
1154 inode->i_gid = stat->n_gid;
1156 if ((S_ISREG(inode->i_mode)) || (S_ISDIR(inode->i_mode))) {
1157 if (v9fs_proto_dotu(v9ses) && (stat->extension[0] != '\0')) {
1159 * Hadlink support got added later to
1160 * to the .u extension. So there can be
1161 * server out there that doesn't support
1162 * this even with .u extension. So check
1163 * for non NULL stat->extension
1165 strncpy(ext, stat->extension, sizeof(ext));
1166 /* HARDLINKCOUNT %u */
1167 sscanf(ext, "%13s %u", tag_name, &i_nlink);
1168 if (!strncmp(tag_name, "HARDLINKCOUNT", 13))
1169 inode->i_nlink = i_nlink;
1172 mode = stat->mode & S_IALLUGO;
1173 mode |= inode->i_mode & ~S_IALLUGO;
1174 inode->i_mode = mode;
1175 i_size_write(inode, stat->length);
1177 /* not real number of blocks, but 512 byte ones ... */
1178 inode->i_blocks = (i_size_read(inode) + 512 - 1) >> 9;
1179 v9inode->cache_validity &= ~V9FS_INO_INVALID_ATTR;
1183 * v9fs_qid2ino - convert qid into inode number
1184 * @qid: qid to hash
1186 * BUG: potential for inode number collisions?
1189 ino_t v9fs_qid2ino(struct p9_qid *qid)
1191 u64 path = qid->path + 2;
1192 ino_t i = 0;
1194 if (sizeof(ino_t) == sizeof(path))
1195 memcpy(&i, &path, sizeof(ino_t));
1196 else
1197 i = (ino_t) (path ^ (path >> 32));
1199 return i;
1203 * v9fs_readlink - read a symlink's location (internal version)
1204 * @dentry: dentry for symlink
1205 * @buffer: buffer to load symlink location into
1206 * @buflen: length of buffer
1210 static int v9fs_readlink(struct dentry *dentry, char *buffer, int buflen)
1212 int retval;
1214 struct v9fs_session_info *v9ses;
1215 struct p9_fid *fid;
1216 struct p9_wstat *st;
1218 P9_DPRINTK(P9_DEBUG_VFS, " %s\n", dentry->d_name.name);
1219 retval = -EPERM;
1220 v9ses = v9fs_dentry2v9ses(dentry);
1221 fid = v9fs_fid_lookup(dentry);
1222 if (IS_ERR(fid))
1223 return PTR_ERR(fid);
1225 if (!v9fs_proto_dotu(v9ses))
1226 return -EBADF;
1228 st = p9_client_stat(fid);
1229 if (IS_ERR(st))
1230 return PTR_ERR(st);
1232 if (!(st->mode & P9_DMSYMLINK)) {
1233 retval = -EINVAL;
1234 goto done;
1237 /* copy extension buffer into buffer */
1238 strncpy(buffer, st->extension, buflen);
1240 P9_DPRINTK(P9_DEBUG_VFS,
1241 "%s -> %s (%s)\n", dentry->d_name.name, st->extension, buffer);
1243 retval = strnlen(buffer, buflen);
1244 done:
1245 p9stat_free(st);
1246 kfree(st);
1247 return retval;
1251 * v9fs_vfs_follow_link - follow a symlink path
1252 * @dentry: dentry for symlink
1253 * @nd: nameidata
1257 static void *v9fs_vfs_follow_link(struct dentry *dentry, struct nameidata *nd)
1259 int len = 0;
1260 char *link = __getname();
1262 P9_DPRINTK(P9_DEBUG_VFS, "%s n", dentry->d_name.name);
1264 if (!link)
1265 link = ERR_PTR(-ENOMEM);
1266 else {
1267 len = v9fs_readlink(dentry, link, PATH_MAX);
1269 if (len < 0) {
1270 __putname(link);
1271 link = ERR_PTR(len);
1272 } else
1273 link[min(len, PATH_MAX-1)] = 0;
1275 nd_set_link(nd, link);
1277 return NULL;
1281 * v9fs_vfs_put_link - release a symlink path
1282 * @dentry: dentry for symlink
1283 * @nd: nameidata
1284 * @p: unused
1288 void
1289 v9fs_vfs_put_link(struct dentry *dentry, struct nameidata *nd, void *p)
1291 char *s = nd_get_link(nd);
1293 P9_DPRINTK(P9_DEBUG_VFS, " %s %s\n", dentry->d_name.name,
1294 IS_ERR(s) ? "<error>" : s);
1295 if (!IS_ERR(s))
1296 __putname(s);
1300 * v9fs_vfs_mkspecial - create a special file
1301 * @dir: inode to create special file in
1302 * @dentry: dentry to create
1303 * @mode: mode to create special file
1304 * @extension: 9p2000.u format extension string representing special file
1308 static int v9fs_vfs_mkspecial(struct inode *dir, struct dentry *dentry,
1309 int mode, const char *extension)
1311 u32 perm;
1312 struct p9_fid *fid;
1313 struct v9fs_session_info *v9ses;
1315 v9ses = v9fs_inode2v9ses(dir);
1316 if (!v9fs_proto_dotu(v9ses)) {
1317 P9_DPRINTK(P9_DEBUG_ERROR, "not extended\n");
1318 return -EPERM;
1321 perm = unixmode2p9mode(v9ses, mode);
1322 fid = v9fs_create(v9ses, dir, dentry, (char *) extension, perm,
1323 P9_OREAD);
1324 if (IS_ERR(fid))
1325 return PTR_ERR(fid);
1327 v9fs_invalidate_inode_attr(dir);
1328 p9_client_clunk(fid);
1329 return 0;
1333 * v9fs_vfs_symlink - helper function to create symlinks
1334 * @dir: directory inode containing symlink
1335 * @dentry: dentry for symlink
1336 * @symname: symlink data
1338 * See Also: 9P2000.u RFC for more information
1342 static int
1343 v9fs_vfs_symlink(struct inode *dir, struct dentry *dentry, const char *symname)
1345 P9_DPRINTK(P9_DEBUG_VFS, " %lu,%s,%s\n", dir->i_ino,
1346 dentry->d_name.name, symname);
1348 return v9fs_vfs_mkspecial(dir, dentry, S_IFLNK, symname);
1352 * v9fs_vfs_link - create a hardlink
1353 * @old_dentry: dentry for file to link to
1354 * @dir: inode destination for new link
1355 * @dentry: dentry for link
1359 static int
1360 v9fs_vfs_link(struct dentry *old_dentry, struct inode *dir,
1361 struct dentry *dentry)
1363 int retval;
1364 char *name;
1365 struct p9_fid *oldfid;
1367 P9_DPRINTK(P9_DEBUG_VFS,
1368 " %lu,%s,%s\n", dir->i_ino, dentry->d_name.name,
1369 old_dentry->d_name.name);
1371 oldfid = v9fs_fid_clone(old_dentry);
1372 if (IS_ERR(oldfid))
1373 return PTR_ERR(oldfid);
1375 name = __getname();
1376 if (unlikely(!name)) {
1377 retval = -ENOMEM;
1378 goto clunk_fid;
1381 sprintf(name, "%d\n", oldfid->fid);
1382 retval = v9fs_vfs_mkspecial(dir, dentry, P9_DMLINK, name);
1383 __putname(name);
1384 if (!retval) {
1385 v9fs_refresh_inode(oldfid, old_dentry->d_inode);
1386 v9fs_invalidate_inode_attr(dir);
1388 clunk_fid:
1389 p9_client_clunk(oldfid);
1390 return retval;
1394 * v9fs_vfs_mknod - create a special file
1395 * @dir: inode destination for new link
1396 * @dentry: dentry for file
1397 * @mode: mode for creation
1398 * @rdev: device associated with special file
1402 static int
1403 v9fs_vfs_mknod(struct inode *dir, struct dentry *dentry, int mode, dev_t rdev)
1405 int retval;
1406 char *name;
1408 P9_DPRINTK(P9_DEBUG_VFS,
1409 " %lu,%s mode: %x MAJOR: %u MINOR: %u\n", dir->i_ino,
1410 dentry->d_name.name, mode, MAJOR(rdev), MINOR(rdev));
1412 if (!new_valid_dev(rdev))
1413 return -EINVAL;
1415 name = __getname();
1416 if (!name)
1417 return -ENOMEM;
1418 /* build extension */
1419 if (S_ISBLK(mode))
1420 sprintf(name, "b %u %u", MAJOR(rdev), MINOR(rdev));
1421 else if (S_ISCHR(mode))
1422 sprintf(name, "c %u %u", MAJOR(rdev), MINOR(rdev));
1423 else if (S_ISFIFO(mode))
1424 *name = 0;
1425 else if (S_ISSOCK(mode))
1426 *name = 0;
1427 else {
1428 __putname(name);
1429 return -EINVAL;
1432 retval = v9fs_vfs_mkspecial(dir, dentry, mode, name);
1433 __putname(name);
1435 return retval;
1438 int v9fs_refresh_inode(struct p9_fid *fid, struct inode *inode)
1440 int umode;
1441 dev_t rdev;
1442 loff_t i_size;
1443 struct p9_wstat *st;
1444 struct v9fs_session_info *v9ses;
1446 v9ses = v9fs_inode2v9ses(inode);
1447 st = p9_client_stat(fid);
1448 if (IS_ERR(st))
1449 return PTR_ERR(st);
1451 * Don't update inode if the file type is different
1453 umode = p9mode2unixmode(v9ses, st, &rdev);
1454 if ((inode->i_mode & S_IFMT) != (umode & S_IFMT))
1455 goto out;
1457 spin_lock(&inode->i_lock);
1459 * We don't want to refresh inode->i_size,
1460 * because we may have cached data
1462 i_size = inode->i_size;
1463 v9fs_stat2inode(st, inode, inode->i_sb);
1464 if (v9ses->cache)
1465 inode->i_size = i_size;
1466 spin_unlock(&inode->i_lock);
1467 out:
1468 p9stat_free(st);
1469 kfree(st);
1470 return 0;
1473 static const struct inode_operations v9fs_dir_inode_operations_dotu = {
1474 .create = v9fs_vfs_create,
1475 .lookup = v9fs_vfs_lookup,
1476 .symlink = v9fs_vfs_symlink,
1477 .link = v9fs_vfs_link,
1478 .unlink = v9fs_vfs_unlink,
1479 .mkdir = v9fs_vfs_mkdir,
1480 .rmdir = v9fs_vfs_rmdir,
1481 .mknod = v9fs_vfs_mknod,
1482 .rename = v9fs_vfs_rename,
1483 .getattr = v9fs_vfs_getattr,
1484 .setattr = v9fs_vfs_setattr,
1487 static const struct inode_operations v9fs_dir_inode_operations = {
1488 .create = v9fs_vfs_create,
1489 .lookup = v9fs_vfs_lookup,
1490 .unlink = v9fs_vfs_unlink,
1491 .mkdir = v9fs_vfs_mkdir,
1492 .rmdir = v9fs_vfs_rmdir,
1493 .mknod = v9fs_vfs_mknod,
1494 .rename = v9fs_vfs_rename,
1495 .getattr = v9fs_vfs_getattr,
1496 .setattr = v9fs_vfs_setattr,
1499 static const struct inode_operations v9fs_file_inode_operations = {
1500 .getattr = v9fs_vfs_getattr,
1501 .setattr = v9fs_vfs_setattr,
1504 static const struct inode_operations v9fs_symlink_inode_operations = {
1505 .readlink = generic_readlink,
1506 .follow_link = v9fs_vfs_follow_link,
1507 .put_link = v9fs_vfs_put_link,
1508 .getattr = v9fs_vfs_getattr,
1509 .setattr = v9fs_vfs_setattr,