netfilter: ipv6: move POSTROUTING invocation before fragmentation
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / fs / 9p / vfs_inode.c
blob5fe45d692c9f32d47751209930c3cc4196705671
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 <net/9p/9p.h>
38 #include <net/9p/client.h>
40 #include "v9fs.h"
41 #include "v9fs_vfs.h"
42 #include "fid.h"
43 #include "cache.h"
45 static const struct inode_operations v9fs_dir_inode_operations;
46 static const struct inode_operations v9fs_dir_inode_operations_ext;
47 static const struct inode_operations v9fs_file_inode_operations;
48 static const 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 |= P9_DMDIR;
63 if (v9fs_proto_dotu(v9ses)) {
64 if (S_ISLNK(mode))
65 res |= P9_DMSYMLINK;
66 if (v9ses->nodev == 0) {
67 if (S_ISSOCK(mode))
68 res |= P9_DMSOCKET;
69 if (S_ISFIFO(mode))
70 res |= P9_DMNAMEDPIPE;
71 if (S_ISBLK(mode))
72 res |= P9_DMDEVICE;
73 if (S_ISCHR(mode))
74 res |= P9_DMDEVICE;
77 if ((mode & S_ISUID) == S_ISUID)
78 res |= P9_DMSETUID;
79 if ((mode & S_ISGID) == S_ISGID)
80 res |= P9_DMSETGID;
81 if ((mode & S_ISVTX) == S_ISVTX)
82 res |= P9_DMSETVTX;
83 if ((mode & P9_DMLINK))
84 res |= P9_DMLINK;
87 return res;
90 /**
91 * p9mode2unixmode- convert plan9 mode bits to unix mode bits
92 * @v9ses: v9fs session information
93 * @mode: mode to convert
97 static int p9mode2unixmode(struct v9fs_session_info *v9ses, int mode)
99 int res;
101 res = mode & 0777;
103 if ((mode & P9_DMDIR) == P9_DMDIR)
104 res |= S_IFDIR;
105 else if ((mode & P9_DMSYMLINK) && (v9fs_proto_dotu(v9ses)))
106 res |= S_IFLNK;
107 else if ((mode & P9_DMSOCKET) && (v9fs_proto_dotu(v9ses))
108 && (v9ses->nodev == 0))
109 res |= S_IFSOCK;
110 else if ((mode & P9_DMNAMEDPIPE) && (v9fs_proto_dotu(v9ses))
111 && (v9ses->nodev == 0))
112 res |= S_IFIFO;
113 else if ((mode & P9_DMDEVICE) && (v9fs_proto_dotu(v9ses))
114 && (v9ses->nodev == 0))
115 res |= S_IFBLK;
116 else
117 res |= S_IFREG;
119 if (v9fs_proto_dotu(v9ses)) {
120 if ((mode & P9_DMSETUID) == P9_DMSETUID)
121 res |= S_ISUID;
123 if ((mode & P9_DMSETGID) == P9_DMSETGID)
124 res |= S_ISGID;
126 if ((mode & P9_DMSETVTX) == P9_DMSETVTX)
127 res |= S_ISVTX;
130 return res;
134 * v9fs_uflags2omode- convert posix open flags to plan 9 mode bits
135 * @uflags: flags to convert
136 * @extended: if .u extensions are active
139 int v9fs_uflags2omode(int uflags, int extended)
141 int ret;
143 ret = 0;
144 switch (uflags&3) {
145 default:
146 case O_RDONLY:
147 ret = P9_OREAD;
148 break;
150 case O_WRONLY:
151 ret = P9_OWRITE;
152 break;
154 case O_RDWR:
155 ret = P9_ORDWR;
156 break;
159 if (uflags & O_TRUNC)
160 ret |= P9_OTRUNC;
162 if (extended) {
163 if (uflags & O_EXCL)
164 ret |= P9_OEXCL;
166 if (uflags & O_APPEND)
167 ret |= P9_OAPPEND;
170 return ret;
174 * v9fs_blank_wstat - helper function to setup a 9P stat structure
175 * @wstat: structure to initialize
179 void
180 v9fs_blank_wstat(struct p9_wstat *wstat)
182 wstat->type = ~0;
183 wstat->dev = ~0;
184 wstat->qid.type = ~0;
185 wstat->qid.version = ~0;
186 *((long long *)&wstat->qid.path) = ~0;
187 wstat->mode = ~0;
188 wstat->atime = ~0;
189 wstat->mtime = ~0;
190 wstat->length = ~0;
191 wstat->name = NULL;
192 wstat->uid = NULL;
193 wstat->gid = NULL;
194 wstat->muid = NULL;
195 wstat->n_uid = ~0;
196 wstat->n_gid = ~0;
197 wstat->n_muid = ~0;
198 wstat->extension = NULL;
201 #ifdef CONFIG_9P_FSCACHE
203 * v9fs_alloc_inode - helper function to allocate an inode
204 * This callback is executed before setting up the inode so that we
205 * can associate a vcookie with each inode.
209 struct inode *v9fs_alloc_inode(struct super_block *sb)
211 struct v9fs_cookie *vcookie;
212 vcookie = (struct v9fs_cookie *)kmem_cache_alloc(vcookie_cache,
213 GFP_KERNEL);
214 if (!vcookie)
215 return NULL;
217 vcookie->fscache = NULL;
218 vcookie->qid = NULL;
219 spin_lock_init(&vcookie->lock);
220 return &vcookie->inode;
224 * v9fs_destroy_inode - destroy an inode
228 void v9fs_destroy_inode(struct inode *inode)
230 kmem_cache_free(vcookie_cache, v9fs_inode2cookie(inode));
232 #endif
235 * v9fs_get_inode - helper function to setup an inode
236 * @sb: superblock
237 * @mode: mode to setup inode with
241 struct inode *v9fs_get_inode(struct super_block *sb, int mode)
243 int err;
244 struct inode *inode;
245 struct v9fs_session_info *v9ses = sb->s_fs_info;
247 P9_DPRINTK(P9_DEBUG_VFS, "super block: %p mode: %o\n", sb, mode);
249 inode = new_inode(sb);
250 if (!inode) {
251 P9_EPRINTK(KERN_WARNING, "Problem allocating inode\n");
252 return ERR_PTR(-ENOMEM);
255 inode->i_mode = mode;
256 inode->i_uid = current_fsuid();
257 inode->i_gid = current_fsgid();
258 inode->i_blocks = 0;
259 inode->i_rdev = 0;
260 inode->i_atime = inode->i_mtime = inode->i_ctime = CURRENT_TIME;
261 inode->i_mapping->a_ops = &v9fs_addr_operations;
263 switch (mode & S_IFMT) {
264 case S_IFIFO:
265 case S_IFBLK:
266 case S_IFCHR:
267 case S_IFSOCK:
268 if (!v9fs_proto_dotu(v9ses)) {
269 P9_DPRINTK(P9_DEBUG_ERROR,
270 "special files without extended mode\n");
271 err = -EINVAL;
272 goto error;
274 init_special_inode(inode, inode->i_mode, inode->i_rdev);
275 break;
276 case S_IFREG:
277 inode->i_op = &v9fs_file_inode_operations;
278 inode->i_fop = &v9fs_file_operations;
279 break;
280 case S_IFLNK:
281 if (!v9fs_proto_dotu(v9ses)) {
282 P9_DPRINTK(P9_DEBUG_ERROR,
283 "extended modes used w/o 9P2000.u\n");
284 err = -EINVAL;
285 goto error;
287 inode->i_op = &v9fs_symlink_inode_operations;
288 break;
289 case S_IFDIR:
290 inc_nlink(inode);
291 if (v9fs_proto_dotu(v9ses))
292 inode->i_op = &v9fs_dir_inode_operations_ext;
293 else
294 inode->i_op = &v9fs_dir_inode_operations;
295 inode->i_fop = &v9fs_dir_operations;
296 break;
297 default:
298 P9_DPRINTK(P9_DEBUG_ERROR, "BAD mode 0x%x S_IFMT 0x%x\n",
299 mode, mode & S_IFMT);
300 err = -EINVAL;
301 goto error;
304 return inode;
306 error:
307 iput(inode);
308 return ERR_PTR(err);
312 static struct v9fs_fid*
313 v9fs_clone_walk(struct v9fs_session_info *v9ses, u32 fid, struct dentry *dentry)
315 int err;
316 int nfid;
317 struct v9fs_fid *ret;
318 struct v9fs_fcall *fcall;
320 nfid = v9fs_get_idpool(&v9ses->fidpool);
321 if (nfid < 0) {
322 eprintk(KERN_WARNING, "no free fids available\n");
323 return ERR_PTR(-ENOSPC);
326 err = v9fs_t_walk(v9ses, fid, nfid, (char *) dentry->d_name.name,
327 &fcall);
329 if (err < 0) {
330 if (fcall && fcall->id == RWALK)
331 goto clunk_fid;
333 PRINT_FCALL_ERROR("walk error", fcall);
334 v9fs_put_idpool(nfid, &v9ses->fidpool);
335 goto error;
338 kfree(fcall);
339 fcall = NULL;
340 ret = v9fs_fid_create(v9ses, nfid);
341 if (!ret) {
342 err = -ENOMEM;
343 goto clunk_fid;
346 err = v9fs_fid_insert(ret, dentry);
347 if (err < 0) {
348 v9fs_fid_destroy(ret);
349 goto clunk_fid;
352 return ret;
354 clunk_fid:
355 v9fs_t_clunk(v9ses, nfid);
357 error:
358 kfree(fcall);
359 return ERR_PTR(err);
365 * v9fs_clear_inode - release an inode
366 * @inode: inode to release
369 void v9fs_clear_inode(struct inode *inode)
371 filemap_fdatawrite(inode->i_mapping);
373 #ifdef CONFIG_9P_FSCACHE
374 v9fs_cache_inode_put_cookie(inode);
375 #endif
379 * v9fs_inode_from_fid - populate an inode by issuing a attribute request
380 * @v9ses: session information
381 * @fid: fid to issue attribute request for
382 * @sb: superblock on which to create inode
386 static struct inode *
387 v9fs_inode_from_fid(struct v9fs_session_info *v9ses, struct p9_fid *fid,
388 struct super_block *sb)
390 int err, umode;
391 struct inode *ret;
392 struct p9_wstat *st;
394 ret = NULL;
395 st = p9_client_stat(fid);
396 if (IS_ERR(st))
397 return ERR_CAST(st);
399 umode = p9mode2unixmode(v9ses, st->mode);
400 ret = v9fs_get_inode(sb, umode);
401 if (IS_ERR(ret)) {
402 err = PTR_ERR(ret);
403 goto error;
406 v9fs_stat2inode(st, ret, sb);
407 ret->i_ino = v9fs_qid2ino(&st->qid);
409 #ifdef CONFIG_9P_FSCACHE
410 v9fs_vcookie_set_qid(ret, &st->qid);
411 v9fs_cache_inode_get_cookie(ret);
412 #endif
413 p9stat_free(st);
414 kfree(st);
416 return ret;
418 error:
419 p9stat_free(st);
420 kfree(st);
421 return ERR_PTR(err);
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 inode *file_inode;
435 struct v9fs_session_info *v9ses;
436 struct p9_fid *v9fid;
438 P9_DPRINTK(P9_DEBUG_VFS, "inode: %p dentry: %p rmdir: %d\n", dir, file,
439 rmdir);
441 file_inode = file->d_inode;
442 v9ses = v9fs_inode2v9ses(file_inode);
443 v9fid = v9fs_fid_clone(file);
444 if (IS_ERR(v9fid))
445 return PTR_ERR(v9fid);
447 return p9_client_remove(v9fid);
450 static int
451 v9fs_open_created(struct inode *inode, struct file *file)
453 return 0;
458 * v9fs_create - Create a file
459 * @v9ses: session information
460 * @dir: directory that dentry is being created in
461 * @dentry: dentry that is being created
462 * @extension: 9p2000.u extension string to support devices, etc.
463 * @perm: create permissions
464 * @mode: open mode
467 static struct p9_fid *
468 v9fs_create(struct v9fs_session_info *v9ses, struct inode *dir,
469 struct dentry *dentry, char *extension, u32 perm, u8 mode)
471 int err;
472 char *name;
473 struct p9_fid *dfid, *ofid, *fid;
474 struct inode *inode;
476 P9_DPRINTK(P9_DEBUG_VFS, "name %s\n", dentry->d_name.name);
478 err = 0;
479 ofid = NULL;
480 fid = NULL;
481 name = (char *) dentry->d_name.name;
482 dfid = v9fs_fid_clone(dentry->d_parent);
483 if (IS_ERR(dfid)) {
484 err = PTR_ERR(dfid);
485 P9_DPRINTK(P9_DEBUG_VFS, "fid clone failed %d\n", err);
486 dfid = NULL;
487 goto error;
490 /* clone a fid to use for creation */
491 ofid = p9_client_walk(dfid, 0, NULL, 1);
492 if (IS_ERR(ofid)) {
493 err = PTR_ERR(ofid);
494 P9_DPRINTK(P9_DEBUG_VFS, "p9_client_walk failed %d\n", err);
495 ofid = NULL;
496 goto error;
499 err = p9_client_fcreate(ofid, name, perm, mode, extension);
500 if (err < 0) {
501 P9_DPRINTK(P9_DEBUG_VFS, "p9_client_fcreate failed %d\n", err);
502 goto error;
505 /* now walk from the parent so we can get unopened fid */
506 fid = p9_client_walk(dfid, 1, &name, 0);
507 if (IS_ERR(fid)) {
508 err = PTR_ERR(fid);
509 P9_DPRINTK(P9_DEBUG_VFS, "p9_client_walk failed %d\n", err);
510 fid = NULL;
511 goto error;
512 } else
513 dfid = NULL;
515 /* instantiate inode and assign the unopened fid to the dentry */
516 inode = v9fs_inode_from_fid(v9ses, fid, dir->i_sb);
517 if (IS_ERR(inode)) {
518 err = PTR_ERR(inode);
519 P9_DPRINTK(P9_DEBUG_VFS, "inode creation failed %d\n", err);
520 goto error;
523 if (v9ses->cache)
524 dentry->d_op = &v9fs_cached_dentry_operations;
525 else
526 dentry->d_op = &v9fs_dentry_operations;
528 d_instantiate(dentry, inode);
529 err = v9fs_fid_add(dentry, fid);
530 if (err < 0)
531 goto error;
533 return ofid;
535 error:
536 if (dfid)
537 p9_client_clunk(dfid);
539 if (ofid)
540 p9_client_clunk(ofid);
542 if (fid)
543 p9_client_clunk(fid);
545 return ERR_PTR(err);
549 * v9fs_vfs_create - VFS hook to create files
550 * @dir: directory inode that is being created
551 * @dentry: dentry that is being deleted
552 * @mode: create permissions
553 * @nd: path information
557 static int
558 v9fs_vfs_create(struct inode *dir, struct dentry *dentry, int mode,
559 struct nameidata *nd)
561 int err;
562 u32 perm;
563 int flags;
564 struct v9fs_session_info *v9ses;
565 struct p9_fid *fid;
566 struct file *filp;
568 err = 0;
569 fid = NULL;
570 v9ses = v9fs_inode2v9ses(dir);
571 perm = unixmode2p9mode(v9ses, mode);
572 if (nd && nd->flags & LOOKUP_OPEN)
573 flags = nd->intent.open.flags - 1;
574 else
575 flags = O_RDWR;
577 fid = v9fs_create(v9ses, dir, dentry, NULL, perm,
578 v9fs_uflags2omode(flags,
579 v9fs_proto_dotu(v9ses)));
580 if (IS_ERR(fid)) {
581 err = PTR_ERR(fid);
582 fid = NULL;
583 goto error;
586 /* if we are opening a file, assign the open fid to the file */
587 if (nd && nd->flags & LOOKUP_OPEN) {
588 filp = lookup_instantiate_filp(nd, dentry, v9fs_open_created);
589 if (IS_ERR(filp)) {
590 err = PTR_ERR(filp);
591 goto error;
594 filp->private_data = fid;
595 } else
596 p9_client_clunk(fid);
598 return 0;
600 error:
601 if (fid)
602 p9_client_clunk(fid);
604 return err;
608 * v9fs_vfs_mkdir - VFS mkdir hook to create a directory
609 * @dir: inode that is being unlinked
610 * @dentry: dentry that is being unlinked
611 * @mode: mode for new directory
615 static int v9fs_vfs_mkdir(struct inode *dir, struct dentry *dentry, int mode)
617 int err;
618 u32 perm;
619 struct v9fs_session_info *v9ses;
620 struct p9_fid *fid;
622 P9_DPRINTK(P9_DEBUG_VFS, "name %s\n", dentry->d_name.name);
623 err = 0;
624 v9ses = v9fs_inode2v9ses(dir);
625 perm = unixmode2p9mode(v9ses, mode | S_IFDIR);
626 fid = v9fs_create(v9ses, dir, dentry, NULL, perm, P9_OREAD);
627 if (IS_ERR(fid)) {
628 err = PTR_ERR(fid);
629 fid = NULL;
632 if (fid)
633 p9_client_clunk(fid);
635 return err;
639 * v9fs_vfs_lookup - VFS lookup hook to "walk" to a new inode
640 * @dir: inode that is being walked from
641 * @dentry: dentry that is being walked to?
642 * @nameidata: path data
646 static struct dentry *v9fs_vfs_lookup(struct inode *dir, struct dentry *dentry,
647 struct nameidata *nameidata)
649 struct super_block *sb;
650 struct v9fs_session_info *v9ses;
651 struct p9_fid *dfid, *fid;
652 struct inode *inode;
653 char *name;
654 int result = 0;
656 P9_DPRINTK(P9_DEBUG_VFS, "dir: %p dentry: (%s) %p nameidata: %p\n",
657 dir, dentry->d_name.name, dentry, nameidata);
659 sb = dir->i_sb;
660 v9ses = v9fs_inode2v9ses(dir);
661 dfid = v9fs_fid_lookup(dentry->d_parent);
662 if (IS_ERR(dfid))
663 return ERR_CAST(dfid);
665 name = (char *) dentry->d_name.name;
666 fid = p9_client_walk(dfid, 1, &name, 1);
667 if (IS_ERR(fid)) {
668 result = PTR_ERR(fid);
669 if (result == -ENOENT) {
670 d_add(dentry, NULL);
671 return NULL;
674 return ERR_PTR(result);
677 inode = v9fs_inode_from_fid(v9ses, fid, dir->i_sb);
678 if (IS_ERR(inode)) {
679 result = PTR_ERR(inode);
680 inode = NULL;
681 goto error;
684 result = v9fs_fid_add(dentry, fid);
685 if (result < 0)
686 goto error;
688 if ((fid->qid.version) && (v9ses->cache))
689 dentry->d_op = &v9fs_cached_dentry_operations;
690 else
691 dentry->d_op = &v9fs_dentry_operations;
693 d_add(dentry, inode);
694 return NULL;
696 error:
697 p9_client_clunk(fid);
699 return ERR_PTR(result);
703 * v9fs_vfs_unlink - VFS unlink hook to delete an inode
704 * @i: inode that is being unlinked
705 * @d: dentry that is being unlinked
709 static int v9fs_vfs_unlink(struct inode *i, struct dentry *d)
711 return v9fs_remove(i, d, 0);
715 * v9fs_vfs_rmdir - VFS unlink hook to delete a directory
716 * @i: inode that is being unlinked
717 * @d: dentry that is being unlinked
721 static int v9fs_vfs_rmdir(struct inode *i, struct dentry *d)
723 return v9fs_remove(i, d, 1);
727 * v9fs_vfs_rename - VFS hook to rename an inode
728 * @old_dir: old dir inode
729 * @old_dentry: old dentry
730 * @new_dir: new dir inode
731 * @new_dentry: new dentry
735 static int
736 v9fs_vfs_rename(struct inode *old_dir, struct dentry *old_dentry,
737 struct inode *new_dir, struct dentry *new_dentry)
739 struct inode *old_inode;
740 struct v9fs_session_info *v9ses;
741 struct p9_fid *oldfid;
742 struct p9_fid *olddirfid;
743 struct p9_fid *newdirfid;
744 struct p9_wstat wstat;
745 int retval;
747 P9_DPRINTK(P9_DEBUG_VFS, "\n");
748 retval = 0;
749 old_inode = old_dentry->d_inode;
750 v9ses = v9fs_inode2v9ses(old_inode);
751 oldfid = v9fs_fid_lookup(old_dentry);
752 if (IS_ERR(oldfid))
753 return PTR_ERR(oldfid);
755 olddirfid = v9fs_fid_clone(old_dentry->d_parent);
756 if (IS_ERR(olddirfid)) {
757 retval = PTR_ERR(olddirfid);
758 goto done;
761 newdirfid = v9fs_fid_clone(new_dentry->d_parent);
762 if (IS_ERR(newdirfid)) {
763 retval = PTR_ERR(newdirfid);
764 goto clunk_olddir;
767 /* 9P can only handle file rename in the same directory */
768 if (memcmp(&olddirfid->qid, &newdirfid->qid, sizeof(newdirfid->qid))) {
769 P9_DPRINTK(P9_DEBUG_ERROR,
770 "old dir and new dir are different\n");
771 retval = -EXDEV;
772 goto clunk_newdir;
775 v9fs_blank_wstat(&wstat);
776 wstat.muid = v9ses->uname;
777 wstat.name = (char *) new_dentry->d_name.name;
778 retval = p9_client_wstat(oldfid, &wstat);
780 clunk_newdir:
781 p9_client_clunk(newdirfid);
783 clunk_olddir:
784 p9_client_clunk(olddirfid);
786 done:
787 return retval;
791 * v9fs_vfs_getattr - retrieve file metadata
792 * @mnt: mount information
793 * @dentry: file to get attributes on
794 * @stat: metadata structure to populate
798 static int
799 v9fs_vfs_getattr(struct vfsmount *mnt, struct dentry *dentry,
800 struct kstat *stat)
802 int err;
803 struct v9fs_session_info *v9ses;
804 struct p9_fid *fid;
805 struct p9_wstat *st;
807 P9_DPRINTK(P9_DEBUG_VFS, "dentry: %p\n", dentry);
808 err = -EPERM;
809 v9ses = v9fs_inode2v9ses(dentry->d_inode);
810 if (v9ses->cache == CACHE_LOOSE || v9ses->cache == CACHE_FSCACHE)
811 return simple_getattr(mnt, dentry, stat);
813 fid = v9fs_fid_lookup(dentry);
814 if (IS_ERR(fid))
815 return PTR_ERR(fid);
817 st = p9_client_stat(fid);
818 if (IS_ERR(st))
819 return PTR_ERR(st);
821 v9fs_stat2inode(st, dentry->d_inode, dentry->d_inode->i_sb);
822 generic_fillattr(dentry->d_inode, stat);
824 kfree(st);
825 return 0;
829 * v9fs_vfs_setattr - set file metadata
830 * @dentry: file whose metadata to set
831 * @iattr: metadata assignment structure
835 static int v9fs_vfs_setattr(struct dentry *dentry, struct iattr *iattr)
837 int retval;
838 struct v9fs_session_info *v9ses;
839 struct p9_fid *fid;
840 struct p9_wstat wstat;
842 P9_DPRINTK(P9_DEBUG_VFS, "\n");
843 retval = -EPERM;
844 v9ses = v9fs_inode2v9ses(dentry->d_inode);
845 fid = v9fs_fid_lookup(dentry);
846 if(IS_ERR(fid))
847 return PTR_ERR(fid);
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 (v9fs_proto_dotu(v9ses)) {
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 retval = p9_client_wstat(fid, &wstat);
871 if (retval >= 0)
872 retval = inode_setattr(dentry->d_inode, iattr);
874 return retval;
878 * v9fs_stat2inode - populate an inode structure with mistat info
879 * @stat: Plan 9 metadata (mistat) structure
880 * @inode: inode to populate
881 * @sb: superblock of filesystem
885 void
886 v9fs_stat2inode(struct p9_wstat *stat, struct inode *inode,
887 struct super_block *sb)
889 char ext[32];
890 char tag_name[14];
891 unsigned int i_nlink;
892 struct v9fs_session_info *v9ses = sb->s_fs_info;
894 inode->i_nlink = 1;
896 inode->i_atime.tv_sec = stat->atime;
897 inode->i_mtime.tv_sec = stat->mtime;
898 inode->i_ctime.tv_sec = stat->mtime;
900 inode->i_uid = v9ses->dfltuid;
901 inode->i_gid = v9ses->dfltgid;
903 if (v9fs_proto_dotu(v9ses)) {
904 inode->i_uid = stat->n_uid;
905 inode->i_gid = stat->n_gid;
907 if ((S_ISREG(inode->i_mode)) || (S_ISDIR(inode->i_mode))) {
908 if (v9fs_proto_dotu(v9ses) && (stat->extension[0] != '\0')) {
910 * Hadlink support got added later to
911 * to the .u extension. So there can be
912 * server out there that doesn't support
913 * this even with .u extension. So check
914 * for non NULL stat->extension
916 strncpy(ext, stat->extension, sizeof(ext));
917 /* HARDLINKCOUNT %u */
918 sscanf(ext, "%13s %u", tag_name, &i_nlink);
919 if (!strncmp(tag_name, "HARDLINKCOUNT", 13))
920 inode->i_nlink = i_nlink;
923 inode->i_mode = p9mode2unixmode(v9ses, stat->mode);
924 if ((S_ISBLK(inode->i_mode)) || (S_ISCHR(inode->i_mode))) {
925 char type = 0;
926 int major = -1;
927 int minor = -1;
929 strncpy(ext, stat->extension, sizeof(ext));
930 sscanf(ext, "%c %u %u", &type, &major, &minor);
931 switch (type) {
932 case 'c':
933 inode->i_mode &= ~S_IFBLK;
934 inode->i_mode |= S_IFCHR;
935 break;
936 case 'b':
937 break;
938 default:
939 P9_DPRINTK(P9_DEBUG_ERROR,
940 "Unknown special type %c %s\n", type,
941 stat->extension);
943 inode->i_rdev = MKDEV(major, minor);
944 init_special_inode(inode, inode->i_mode, inode->i_rdev);
945 } else
946 inode->i_rdev = 0;
948 i_size_write(inode, stat->length);
950 /* not real number of blocks, but 512 byte ones ... */
951 inode->i_blocks = (i_size_read(inode) + 512 - 1) >> 9;
955 * v9fs_qid2ino - convert qid into inode number
956 * @qid: qid to hash
958 * BUG: potential for inode number collisions?
961 ino_t v9fs_qid2ino(struct p9_qid *qid)
963 u64 path = qid->path + 2;
964 ino_t i = 0;
966 if (sizeof(ino_t) == sizeof(path))
967 memcpy(&i, &path, sizeof(ino_t));
968 else
969 i = (ino_t) (path ^ (path >> 32));
971 return i;
975 * v9fs_readlink - read a symlink's location (internal version)
976 * @dentry: dentry for symlink
977 * @buffer: buffer to load symlink location into
978 * @buflen: length of buffer
982 static int v9fs_readlink(struct dentry *dentry, char *buffer, int buflen)
984 int retval;
986 struct v9fs_session_info *v9ses;
987 struct p9_fid *fid;
988 struct p9_wstat *st;
990 P9_DPRINTK(P9_DEBUG_VFS, " %s\n", dentry->d_name.name);
991 retval = -EPERM;
992 v9ses = v9fs_inode2v9ses(dentry->d_inode);
993 fid = v9fs_fid_lookup(dentry);
994 if (IS_ERR(fid))
995 return PTR_ERR(fid);
997 if (!v9fs_proto_dotu(v9ses))
998 return -EBADF;
1000 st = p9_client_stat(fid);
1001 if (IS_ERR(st))
1002 return PTR_ERR(st);
1004 if (!(st->mode & P9_DMSYMLINK)) {
1005 retval = -EINVAL;
1006 goto done;
1009 /* copy extension buffer into buffer */
1010 strncpy(buffer, st->extension, buflen);
1012 P9_DPRINTK(P9_DEBUG_VFS,
1013 "%s -> %s (%s)\n", dentry->d_name.name, st->extension, buffer);
1015 retval = strnlen(buffer, buflen);
1016 done:
1017 kfree(st);
1018 return retval;
1022 * v9fs_vfs_follow_link - follow a symlink path
1023 * @dentry: dentry for symlink
1024 * @nd: nameidata
1028 static void *v9fs_vfs_follow_link(struct dentry *dentry, struct nameidata *nd)
1030 int len = 0;
1031 char *link = __getname();
1033 P9_DPRINTK(P9_DEBUG_VFS, "%s n", dentry->d_name.name);
1035 if (!link)
1036 link = ERR_PTR(-ENOMEM);
1037 else {
1038 len = v9fs_readlink(dentry, link, PATH_MAX);
1040 if (len < 0) {
1041 __putname(link);
1042 link = ERR_PTR(len);
1043 } else
1044 link[min(len, PATH_MAX-1)] = 0;
1046 nd_set_link(nd, link);
1048 return NULL;
1052 * v9fs_vfs_put_link - release a symlink path
1053 * @dentry: dentry for symlink
1054 * @nd: nameidata
1055 * @p: unused
1059 static void
1060 v9fs_vfs_put_link(struct dentry *dentry, struct nameidata *nd, void *p)
1062 char *s = nd_get_link(nd);
1064 P9_DPRINTK(P9_DEBUG_VFS, " %s %s\n", dentry->d_name.name,
1065 IS_ERR(s) ? "<error>" : s);
1066 if (!IS_ERR(s))
1067 __putname(s);
1071 * v9fs_vfs_mkspecial - create a special file
1072 * @dir: inode to create special file in
1073 * @dentry: dentry to create
1074 * @mode: mode to create special file
1075 * @extension: 9p2000.u format extension string representing special file
1079 static int v9fs_vfs_mkspecial(struct inode *dir, struct dentry *dentry,
1080 int mode, const char *extension)
1082 u32 perm;
1083 struct v9fs_session_info *v9ses;
1084 struct p9_fid *fid;
1086 v9ses = v9fs_inode2v9ses(dir);
1087 if (!v9fs_proto_dotu(v9ses)) {
1088 P9_DPRINTK(P9_DEBUG_ERROR, "not extended\n");
1089 return -EPERM;
1092 perm = unixmode2p9mode(v9ses, mode);
1093 fid = v9fs_create(v9ses, dir, dentry, (char *) extension, perm,
1094 P9_OREAD);
1095 if (IS_ERR(fid))
1096 return PTR_ERR(fid);
1098 p9_client_clunk(fid);
1099 return 0;
1103 * v9fs_vfs_symlink - helper function to create symlinks
1104 * @dir: directory inode containing symlink
1105 * @dentry: dentry for symlink
1106 * @symname: symlink data
1108 * See Also: 9P2000.u RFC for more information
1112 static int
1113 v9fs_vfs_symlink(struct inode *dir, struct dentry *dentry, const char *symname)
1115 P9_DPRINTK(P9_DEBUG_VFS, " %lu,%s,%s\n", dir->i_ino,
1116 dentry->d_name.name, symname);
1118 return v9fs_vfs_mkspecial(dir, dentry, S_IFLNK, symname);
1122 * v9fs_vfs_link - create a hardlink
1123 * @old_dentry: dentry for file to link to
1124 * @dir: inode destination for new link
1125 * @dentry: dentry for link
1129 static int
1130 v9fs_vfs_link(struct dentry *old_dentry, struct inode *dir,
1131 struct dentry *dentry)
1133 int retval;
1134 struct p9_fid *oldfid;
1135 char *name;
1137 P9_DPRINTK(P9_DEBUG_VFS,
1138 " %lu,%s,%s\n", dir->i_ino, dentry->d_name.name,
1139 old_dentry->d_name.name);
1141 oldfid = v9fs_fid_clone(old_dentry);
1142 if (IS_ERR(oldfid))
1143 return PTR_ERR(oldfid);
1145 name = __getname();
1146 if (unlikely(!name)) {
1147 retval = -ENOMEM;
1148 goto clunk_fid;
1151 sprintf(name, "%d\n", oldfid->fid);
1152 retval = v9fs_vfs_mkspecial(dir, dentry, P9_DMLINK, name);
1153 __putname(name);
1155 clunk_fid:
1156 p9_client_clunk(oldfid);
1157 return retval;
1161 * v9fs_vfs_mknod - create a special file
1162 * @dir: inode destination for new link
1163 * @dentry: dentry for file
1164 * @mode: mode for creation
1165 * @rdev: device associated with special file
1169 static int
1170 v9fs_vfs_mknod(struct inode *dir, struct dentry *dentry, int mode, dev_t rdev)
1172 int retval;
1173 char *name;
1175 P9_DPRINTK(P9_DEBUG_VFS,
1176 " %lu,%s mode: %x MAJOR: %u MINOR: %u\n", dir->i_ino,
1177 dentry->d_name.name, mode, MAJOR(rdev), MINOR(rdev));
1179 if (!new_valid_dev(rdev))
1180 return -EINVAL;
1182 name = __getname();
1183 if (!name)
1184 return -ENOMEM;
1185 /* build extension */
1186 if (S_ISBLK(mode))
1187 sprintf(name, "b %u %u", MAJOR(rdev), MINOR(rdev));
1188 else if (S_ISCHR(mode))
1189 sprintf(name, "c %u %u", MAJOR(rdev), MINOR(rdev));
1190 else if (S_ISFIFO(mode))
1191 *name = 0;
1192 else {
1193 __putname(name);
1194 return -EINVAL;
1197 retval = v9fs_vfs_mkspecial(dir, dentry, mode, name);
1198 __putname(name);
1200 return retval;
1203 static const struct inode_operations v9fs_dir_inode_operations_ext = {
1204 .create = v9fs_vfs_create,
1205 .lookup = v9fs_vfs_lookup,
1206 .symlink = v9fs_vfs_symlink,
1207 .link = v9fs_vfs_link,
1208 .unlink = v9fs_vfs_unlink,
1209 .mkdir = v9fs_vfs_mkdir,
1210 .rmdir = v9fs_vfs_rmdir,
1211 .mknod = v9fs_vfs_mknod,
1212 .rename = v9fs_vfs_rename,
1213 .getattr = v9fs_vfs_getattr,
1214 .setattr = v9fs_vfs_setattr,
1217 static const struct inode_operations v9fs_dir_inode_operations = {
1218 .create = v9fs_vfs_create,
1219 .lookup = v9fs_vfs_lookup,
1220 .unlink = v9fs_vfs_unlink,
1221 .mkdir = v9fs_vfs_mkdir,
1222 .rmdir = v9fs_vfs_rmdir,
1223 .mknod = v9fs_vfs_mknod,
1224 .rename = v9fs_vfs_rename,
1225 .getattr = v9fs_vfs_getattr,
1226 .setattr = v9fs_vfs_setattr,
1229 static const struct inode_operations v9fs_file_inode_operations = {
1230 .getattr = v9fs_vfs_getattr,
1231 .setattr = v9fs_vfs_setattr,
1234 static const struct inode_operations v9fs_symlink_inode_operations = {
1235 .readlink = generic_readlink,
1236 .follow_link = v9fs_vfs_follow_link,
1237 .put_link = v9fs_vfs_put_link,
1238 .getattr = v9fs_vfs_getattr,
1239 .setattr = v9fs_vfs_setattr,