fs/9p: Add fid to inode in cached mode
[deliverable/linux.git] / fs / 9p / vfs_inode.c
1 /*
2 * linux/fs/9p/vfs_inode.c
3 *
4 * This file contains vfs inode ops for the 9P2000 protocol.
5 *
6 * Copyright (C) 2004 by Eric Van Hensbergen <ericvh@gmail.com>
7 * Copyright (C) 2002 by Ron Minnich <rminnich@lanl.gov>
8 *
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.
12 *
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.
17 *
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
23 *
24 */
25
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>
42
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"
49
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;
54
55 /**
56 * unixmode2p9mode - convert unix mode bits to plan 9
57 * @v9ses: v9fs session information
58 * @mode: mode to convert
59 *
60 */
61
62 static int unixmode2p9mode(struct v9fs_session_info *v9ses, int mode)
63 {
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;
80 }
81
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;
90 }
91
92 return res;
93 }
94
95 /**
96 * p9mode2unixmode- convert plan9 mode bits to unix mode bits
97 * @v9ses: v9fs session information
98 * @mode: mode to convert
99 *
100 */
101
102 static int p9mode2unixmode(struct v9fs_session_info *v9ses, int mode)
103 {
104 int res;
105
106 res = mode & 0777;
107
108 if ((mode & P9_DMDIR) == P9_DMDIR)
109 res |= S_IFDIR;
110 else if ((mode & P9_DMSYMLINK) && (v9fs_proto_dotu(v9ses)))
111 res |= S_IFLNK;
112 else if ((mode & P9_DMSOCKET) && (v9fs_proto_dotu(v9ses))
113 && (v9ses->nodev == 0))
114 res |= S_IFSOCK;
115 else if ((mode & P9_DMNAMEDPIPE) && (v9fs_proto_dotu(v9ses))
116 && (v9ses->nodev == 0))
117 res |= S_IFIFO;
118 else if ((mode & P9_DMDEVICE) && (v9fs_proto_dotu(v9ses))
119 && (v9ses->nodev == 0))
120 res |= S_IFBLK;
121 else
122 res |= S_IFREG;
123
124 if (v9fs_proto_dotu(v9ses)) {
125 if ((mode & P9_DMSETUID) == P9_DMSETUID)
126 res |= S_ISUID;
127
128 if ((mode & P9_DMSETGID) == P9_DMSETGID)
129 res |= S_ISGID;
130
131 if ((mode & P9_DMSETVTX) == P9_DMSETVTX)
132 res |= S_ISVTX;
133 }
134
135 return res;
136 }
137
138 /**
139 * v9fs_uflags2omode- convert posix open flags to plan 9 mode bits
140 * @uflags: flags to convert
141 * @extended: if .u extensions are active
142 */
143
144 int v9fs_uflags2omode(int uflags, int extended)
145 {
146 int ret;
147
148 ret = 0;
149 switch (uflags&3) {
150 default:
151 case O_RDONLY:
152 ret = P9_OREAD;
153 break;
154
155 case O_WRONLY:
156 ret = P9_OWRITE;
157 break;
158
159 case O_RDWR:
160 ret = P9_ORDWR;
161 break;
162 }
163
164 if (uflags & O_TRUNC)
165 ret |= P9_OTRUNC;
166
167 if (extended) {
168 if (uflags & O_EXCL)
169 ret |= P9_OEXCL;
170
171 if (uflags & O_APPEND)
172 ret |= P9_OAPPEND;
173 }
174
175 return ret;
176 }
177
178 /**
179 * v9fs_blank_wstat - helper function to setup a 9P stat structure
180 * @wstat: structure to initialize
181 *
182 */
183
184 void
185 v9fs_blank_wstat(struct p9_wstat *wstat)
186 {
187 wstat->type = ~0;
188 wstat->dev = ~0;
189 wstat->qid.type = ~0;
190 wstat->qid.version = ~0;
191 *((long long *)&wstat->qid.path) = ~0;
192 wstat->mode = ~0;
193 wstat->atime = ~0;
194 wstat->mtime = ~0;
195 wstat->length = ~0;
196 wstat->name = NULL;
197 wstat->uid = NULL;
198 wstat->gid = NULL;
199 wstat->muid = NULL;
200 wstat->n_uid = ~0;
201 wstat->n_gid = ~0;
202 wstat->n_muid = ~0;
203 wstat->extension = NULL;
204 }
205
206 #ifdef CONFIG_9P_FSCACHE
207 /**
208 * v9fs_alloc_inode - helper function to allocate an inode
209 * This callback is executed before setting up the inode so that we
210 * can associate a vcookie with each inode.
211 *
212 */
213
214 struct inode *v9fs_alloc_inode(struct super_block *sb)
215 {
216 struct v9fs_cookie *vcookie;
217 vcookie = (struct v9fs_cookie *)kmem_cache_alloc(vcookie_cache,
218 GFP_KERNEL);
219 if (!vcookie)
220 return NULL;
221
222 vcookie->fscache = NULL;
223 vcookie->qid = NULL;
224 spin_lock_init(&vcookie->lock);
225 return &vcookie->inode;
226 }
227
228 /**
229 * v9fs_destroy_inode - destroy an inode
230 *
231 */
232
233 static void v9fs_i_callback(struct rcu_head *head)
234 {
235 struct inode *inode = container_of(head, struct inode, i_rcu);
236 INIT_LIST_HEAD(&inode->i_dentry);
237 kmem_cache_free(vcookie_cache, v9fs_inode2cookie(inode));
238 }
239
240 void v9fs_destroy_inode(struct inode *inode)
241 {
242 call_rcu(&inode->i_rcu, v9fs_i_callback);
243 }
244 #endif
245
246 /**
247 * v9fs_get_inode - helper function to setup an inode
248 * @sb: superblock
249 * @mode: mode to setup inode with
250 *
251 */
252
253 struct inode *v9fs_get_inode(struct super_block *sb, int mode)
254 {
255 int err;
256 struct inode *inode;
257 struct v9fs_session_info *v9ses = sb->s_fs_info;
258
259 P9_DPRINTK(P9_DEBUG_VFS, "super block: %p mode: %o\n", sb, mode);
260
261 inode = new_inode(sb);
262 if (!inode) {
263 P9_EPRINTK(KERN_WARNING, "Problem allocating inode\n");
264 return ERR_PTR(-ENOMEM);
265 }
266
267 inode_init_owner(inode, NULL, mode);
268 inode->i_blocks = 0;
269 inode->i_rdev = 0;
270 inode->i_atime = inode->i_mtime = inode->i_ctime = CURRENT_TIME;
271 inode->i_mapping->a_ops = &v9fs_addr_operations;
272
273 switch (mode & S_IFMT) {
274 case S_IFIFO:
275 case S_IFBLK:
276 case S_IFCHR:
277 case S_IFSOCK:
278 if (v9fs_proto_dotl(v9ses)) {
279 inode->i_op = &v9fs_file_inode_operations_dotl;
280 inode->i_fop = &v9fs_file_operations_dotl;
281 } else if (v9fs_proto_dotu(v9ses)) {
282 inode->i_op = &v9fs_file_inode_operations;
283 inode->i_fop = &v9fs_file_operations;
284 } else {
285 P9_DPRINTK(P9_DEBUG_ERROR,
286 "special files without extended mode\n");
287 err = -EINVAL;
288 goto error;
289 }
290 init_special_inode(inode, inode->i_mode, inode->i_rdev);
291 break;
292 case S_IFREG:
293 if (v9fs_proto_dotl(v9ses)) {
294 inode->i_op = &v9fs_file_inode_operations_dotl;
295 if (v9ses->cache)
296 inode->i_fop =
297 &v9fs_cached_file_operations_dotl;
298 else
299 inode->i_fop = &v9fs_file_operations_dotl;
300 } else {
301 inode->i_op = &v9fs_file_inode_operations;
302 if (v9ses->cache)
303 inode->i_fop = &v9fs_cached_file_operations;
304 else
305 inode->i_fop = &v9fs_file_operations;
306 }
307
308 break;
309
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;
316 }
317
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;
322
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;
332
333 if (v9fs_proto_dotl(v9ses))
334 inode->i_fop = &v9fs_dir_operations_dotl;
335 else
336 inode->i_fop = &v9fs_dir_operations;
337
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;
344 }
345
346 return inode;
347
348 error:
349 iput(inode);
350 return ERR_PTR(err);
351 }
352
353 /*
354 static struct v9fs_fid*
355 v9fs_clone_walk(struct v9fs_session_info *v9ses, u32 fid, struct dentry *dentry)
356 {
357 int err;
358 int nfid;
359 struct v9fs_fid *ret;
360 struct v9fs_fcall *fcall;
361
362 nfid = v9fs_get_idpool(&v9ses->fidpool);
363 if (nfid < 0) {
364 eprintk(KERN_WARNING, "no free fids available\n");
365 return ERR_PTR(-ENOSPC);
366 }
367
368 err = v9fs_t_walk(v9ses, fid, nfid, (char *) dentry->d_name.name,
369 &fcall);
370
371 if (err < 0) {
372 if (fcall && fcall->id == RWALK)
373 goto clunk_fid;
374
375 PRINT_FCALL_ERROR("walk error", fcall);
376 v9fs_put_idpool(nfid, &v9ses->fidpool);
377 goto error;
378 }
379
380 kfree(fcall);
381 fcall = NULL;
382 ret = v9fs_fid_create(v9ses, nfid);
383 if (!ret) {
384 err = -ENOMEM;
385 goto clunk_fid;
386 }
387
388 err = v9fs_fid_insert(ret, dentry);
389 if (err < 0) {
390 v9fs_fid_destroy(ret);
391 goto clunk_fid;
392 }
393
394 return ret;
395
396 clunk_fid:
397 v9fs_t_clunk(v9ses, nfid);
398
399 error:
400 kfree(fcall);
401 return ERR_PTR(err);
402 }
403 */
404
405
406 /**
407 * v9fs_clear_inode - release an inode
408 * @inode: inode to release
409 *
410 */
411 void v9fs_evict_inode(struct inode *inode)
412 {
413 truncate_inode_pages(inode->i_mapping, 0);
414 end_writeback(inode);
415 filemap_fdatawrite(inode->i_mapping);
416
417 #ifdef CONFIG_9P_FSCACHE
418 v9fs_cache_inode_put_cookie(inode);
419 #endif
420 /* clunk the fid stashed in inode->i_private */
421 if (inode->i_private) {
422 p9_client_clunk((struct p9_fid *)inode->i_private);
423 inode->i_private = NULL;
424 }
425 }
426
427 struct inode *
428 v9fs_inode(struct v9fs_session_info *v9ses, struct p9_fid *fid,
429 struct super_block *sb)
430 {
431 int err, umode;
432 struct inode *ret = NULL;
433 struct p9_wstat *st;
434
435 st = p9_client_stat(fid);
436 if (IS_ERR(st))
437 return ERR_CAST(st);
438
439 umode = p9mode2unixmode(v9ses, st->mode);
440 ret = v9fs_get_inode(sb, umode);
441 if (IS_ERR(ret)) {
442 err = PTR_ERR(ret);
443 goto error;
444 }
445
446 v9fs_stat2inode(st, ret, sb);
447 ret->i_ino = v9fs_qid2ino(&st->qid);
448
449 #ifdef CONFIG_9P_FSCACHE
450 v9fs_vcookie_set_qid(ret, &st->qid);
451 v9fs_cache_inode_get_cookie(ret);
452 #endif
453 p9stat_free(st);
454 kfree(st);
455 return ret;
456 error:
457 p9stat_free(st);
458 kfree(st);
459 return ERR_PTR(err);
460 }
461
462 /**
463 * v9fs_remove - helper function to remove files and directories
464 * @dir: directory inode that is being deleted
465 * @file: dentry that is being deleted
466 * @rmdir: removing a directory
467 *
468 */
469
470 static int v9fs_remove(struct inode *dir, struct dentry *file, int rmdir)
471 {
472 int retval;
473 struct inode *file_inode;
474 struct p9_fid *v9fid;
475
476 P9_DPRINTK(P9_DEBUG_VFS, "inode: %p dentry: %p rmdir: %d\n", dir, file,
477 rmdir);
478
479 file_inode = file->d_inode;
480 v9fid = v9fs_fid_clone(file);
481 if (IS_ERR(v9fid))
482 return PTR_ERR(v9fid);
483
484 retval = p9_client_remove(v9fid);
485 if (!retval)
486 drop_nlink(file_inode);
487 return retval;
488 }
489
490 /**
491 * v9fs_create - Create a file
492 * @v9ses: session information
493 * @dir: directory that dentry is being created in
494 * @dentry: dentry that is being created
495 * @extension: 9p2000.u extension string to support devices, etc.
496 * @perm: create permissions
497 * @mode: open mode
498 *
499 */
500 static struct p9_fid *
501 v9fs_create(struct v9fs_session_info *v9ses, struct inode *dir,
502 struct dentry *dentry, char *extension, u32 perm, u8 mode)
503 {
504 int err;
505 char *name;
506 struct p9_fid *dfid, *ofid, *fid;
507 struct inode *inode;
508
509 P9_DPRINTK(P9_DEBUG_VFS, "name %s\n", dentry->d_name.name);
510
511 err = 0;
512 ofid = NULL;
513 fid = NULL;
514 name = (char *) dentry->d_name.name;
515 dfid = v9fs_fid_lookup(dentry->d_parent);
516 if (IS_ERR(dfid)) {
517 err = PTR_ERR(dfid);
518 P9_DPRINTK(P9_DEBUG_VFS, "fid lookup failed %d\n", err);
519 return ERR_PTR(err);
520 }
521
522 /* clone a fid to use for creation */
523 ofid = p9_client_walk(dfid, 0, NULL, 1);
524 if (IS_ERR(ofid)) {
525 err = PTR_ERR(ofid);
526 P9_DPRINTK(P9_DEBUG_VFS, "p9_client_walk failed %d\n", err);
527 return ERR_PTR(err);
528 }
529
530 err = p9_client_fcreate(ofid, name, perm, mode, extension);
531 if (err < 0) {
532 P9_DPRINTK(P9_DEBUG_VFS, "p9_client_fcreate failed %d\n", err);
533 goto error;
534 }
535
536 /* now walk from the parent so we can get unopened fid */
537 fid = p9_client_walk(dfid, 1, &name, 1);
538 if (IS_ERR(fid)) {
539 err = PTR_ERR(fid);
540 P9_DPRINTK(P9_DEBUG_VFS, "p9_client_walk failed %d\n", err);
541 fid = NULL;
542 goto error;
543 }
544
545 /* instantiate inode and assign the unopened fid to the dentry */
546 inode = v9fs_inode_from_fid(v9ses, fid, dir->i_sb);
547 if (IS_ERR(inode)) {
548 err = PTR_ERR(inode);
549 P9_DPRINTK(P9_DEBUG_VFS, "inode creation failed %d\n", err);
550 goto error;
551 }
552 d_instantiate(dentry, inode);
553 err = v9fs_fid_add(dentry, fid);
554 if (err < 0)
555 goto error;
556
557 return ofid;
558
559 error:
560 if (ofid)
561 p9_client_clunk(ofid);
562
563 if (fid)
564 p9_client_clunk(fid);
565
566 return ERR_PTR(err);
567 }
568
569 /**
570 * v9fs_vfs_create - VFS hook to create files
571 * @dir: directory inode that is being created
572 * @dentry: dentry that is being deleted
573 * @mode: create permissions
574 * @nd: path information
575 *
576 */
577
578 static int
579 v9fs_vfs_create(struct inode *dir, struct dentry *dentry, int mode,
580 struct nameidata *nd)
581 {
582 int err;
583 u32 perm;
584 int flags;
585 struct v9fs_session_info *v9ses;
586 struct p9_fid *fid, *inode_fid;
587 struct file *filp;
588
589 err = 0;
590 fid = NULL;
591 v9ses = v9fs_inode2v9ses(dir);
592 perm = unixmode2p9mode(v9ses, mode);
593 if (nd && nd->flags & LOOKUP_OPEN)
594 flags = nd->intent.open.flags - 1;
595 else
596 flags = O_RDWR;
597
598 fid = v9fs_create(v9ses, dir, dentry, NULL, perm,
599 v9fs_uflags2omode(flags,
600 v9fs_proto_dotu(v9ses)));
601 if (IS_ERR(fid)) {
602 err = PTR_ERR(fid);
603 fid = NULL;
604 goto error;
605 }
606
607 /* if we are opening a file, assign the open fid to the file */
608 if (nd && nd->flags & LOOKUP_OPEN) {
609 if (v9ses->cache && !dentry->d_inode->i_private) {
610 /*
611 * clone a fid and add it to inode->i_private
612 * we do it during open time instead of
613 * page dirty time via write_begin/page_mkwrite
614 * because we want write after unlink usecase
615 * to work.
616 */
617 inode_fid = v9fs_writeback_fid(dentry);
618 if (IS_ERR(inode_fid)) {
619 err = PTR_ERR(inode_fid);
620 goto error;
621 }
622 dentry->d_inode->i_private = (void *) inode_fid;
623 }
624 filp = lookup_instantiate_filp(nd, dentry, generic_file_open);
625 if (IS_ERR(filp)) {
626 err = PTR_ERR(filp);
627 goto error;
628 }
629
630 filp->private_data = fid;
631 #ifdef CONFIG_9P_FSCACHE
632 if (v9ses->cache)
633 v9fs_cache_inode_set_cookie(dentry->d_inode, filp);
634 #endif
635 } else
636 p9_client_clunk(fid);
637
638 return 0;
639
640 error:
641 if (fid)
642 p9_client_clunk(fid);
643
644 return err;
645 }
646
647 /**
648 * v9fs_vfs_mkdir - VFS mkdir hook to create a directory
649 * @dir: inode that is being unlinked
650 * @dentry: dentry that is being unlinked
651 * @mode: mode for new directory
652 *
653 */
654
655 static int v9fs_vfs_mkdir(struct inode *dir, struct dentry *dentry, int mode)
656 {
657 int err;
658 u32 perm;
659 struct v9fs_session_info *v9ses;
660 struct p9_fid *fid;
661
662 P9_DPRINTK(P9_DEBUG_VFS, "name %s\n", dentry->d_name.name);
663 err = 0;
664 v9ses = v9fs_inode2v9ses(dir);
665 perm = unixmode2p9mode(v9ses, mode | S_IFDIR);
666 fid = v9fs_create(v9ses, dir, dentry, NULL, perm, P9_OREAD);
667 if (IS_ERR(fid)) {
668 err = PTR_ERR(fid);
669 fid = NULL;
670 }
671
672 if (fid)
673 p9_client_clunk(fid);
674
675 return err;
676 }
677
678 /**
679 * v9fs_vfs_lookup - VFS lookup hook to "walk" to a new inode
680 * @dir: inode that is being walked from
681 * @dentry: dentry that is being walked to?
682 * @nameidata: path data
683 *
684 */
685
686 struct dentry *v9fs_vfs_lookup(struct inode *dir, struct dentry *dentry,
687 struct nameidata *nameidata)
688 {
689 struct super_block *sb;
690 struct v9fs_session_info *v9ses;
691 struct p9_fid *dfid, *fid;
692 struct inode *inode;
693 char *name;
694 int result = 0;
695
696 P9_DPRINTK(P9_DEBUG_VFS, "dir: %p dentry: (%s) %p nameidata: %p\n",
697 dir, dentry->d_name.name, dentry, nameidata);
698
699 if (dentry->d_name.len > NAME_MAX)
700 return ERR_PTR(-ENAMETOOLONG);
701
702 sb = dir->i_sb;
703 v9ses = v9fs_inode2v9ses(dir);
704 /* We can walk d_parent because we hold the dir->i_mutex */
705 dfid = v9fs_fid_lookup(dentry->d_parent);
706 if (IS_ERR(dfid))
707 return ERR_CAST(dfid);
708
709 name = (char *) dentry->d_name.name;
710 fid = p9_client_walk(dfid, 1, &name, 1);
711 if (IS_ERR(fid)) {
712 result = PTR_ERR(fid);
713 if (result == -ENOENT) {
714 inode = NULL;
715 goto inst_out;
716 }
717
718 return ERR_PTR(result);
719 }
720
721 inode = v9fs_inode_from_fid(v9ses, fid, dir->i_sb);
722 if (IS_ERR(inode)) {
723 result = PTR_ERR(inode);
724 inode = NULL;
725 goto error;
726 }
727
728 result = v9fs_fid_add(dentry, fid);
729 if (result < 0)
730 goto error_iput;
731
732 inst_out:
733 d_add(dentry, inode);
734 return NULL;
735
736 error_iput:
737 iput(inode);
738 error:
739 p9_client_clunk(fid);
740
741 return ERR_PTR(result);
742 }
743
744 /**
745 * v9fs_vfs_unlink - VFS unlink hook to delete an inode
746 * @i: inode that is being unlinked
747 * @d: dentry that is being unlinked
748 *
749 */
750
751 int v9fs_vfs_unlink(struct inode *i, struct dentry *d)
752 {
753 return v9fs_remove(i, d, 0);
754 }
755
756 /**
757 * v9fs_vfs_rmdir - VFS unlink hook to delete a directory
758 * @i: inode that is being unlinked
759 * @d: dentry that is being unlinked
760 *
761 */
762
763 int v9fs_vfs_rmdir(struct inode *i, struct dentry *d)
764 {
765 return v9fs_remove(i, d, 1);
766 }
767
768 /**
769 * v9fs_vfs_rename - VFS hook to rename an inode
770 * @old_dir: old dir inode
771 * @old_dentry: old dentry
772 * @new_dir: new dir inode
773 * @new_dentry: new dentry
774 *
775 */
776
777 int
778 v9fs_vfs_rename(struct inode *old_dir, struct dentry *old_dentry,
779 struct inode *new_dir, struct dentry *new_dentry)
780 {
781 struct inode *old_inode;
782 struct v9fs_session_info *v9ses;
783 struct p9_fid *oldfid;
784 struct p9_fid *olddirfid;
785 struct p9_fid *newdirfid;
786 struct p9_wstat wstat;
787 int retval;
788
789 P9_DPRINTK(P9_DEBUG_VFS, "\n");
790 retval = 0;
791 old_inode = old_dentry->d_inode;
792 v9ses = v9fs_inode2v9ses(old_inode);
793 oldfid = v9fs_fid_lookup(old_dentry);
794 if (IS_ERR(oldfid))
795 return PTR_ERR(oldfid);
796
797 olddirfid = v9fs_fid_clone(old_dentry->d_parent);
798 if (IS_ERR(olddirfid)) {
799 retval = PTR_ERR(olddirfid);
800 goto done;
801 }
802
803 newdirfid = v9fs_fid_clone(new_dentry->d_parent);
804 if (IS_ERR(newdirfid)) {
805 retval = PTR_ERR(newdirfid);
806 goto clunk_olddir;
807 }
808
809 down_write(&v9ses->rename_sem);
810 if (v9fs_proto_dotl(v9ses)) {
811 retval = p9_client_rename(oldfid, newdirfid,
812 (char *) new_dentry->d_name.name);
813 if (retval != -ENOSYS)
814 goto clunk_newdir;
815 }
816 if (old_dentry->d_parent != new_dentry->d_parent) {
817 /*
818 * 9P .u can only handle file rename in the same directory
819 */
820
821 P9_DPRINTK(P9_DEBUG_ERROR,
822 "old dir and new dir are different\n");
823 retval = -EXDEV;
824 goto clunk_newdir;
825 }
826 v9fs_blank_wstat(&wstat);
827 wstat.muid = v9ses->uname;
828 wstat.name = (char *) new_dentry->d_name.name;
829 retval = p9_client_wstat(oldfid, &wstat);
830
831 clunk_newdir:
832 if (!retval)
833 /* successful rename */
834 d_move(old_dentry, new_dentry);
835 up_write(&v9ses->rename_sem);
836 p9_client_clunk(newdirfid);
837
838 clunk_olddir:
839 p9_client_clunk(olddirfid);
840
841 done:
842 return retval;
843 }
844
845 /**
846 * v9fs_vfs_getattr - retrieve file metadata
847 * @mnt: mount information
848 * @dentry: file to get attributes on
849 * @stat: metadata structure to populate
850 *
851 */
852
853 static int
854 v9fs_vfs_getattr(struct vfsmount *mnt, struct dentry *dentry,
855 struct kstat *stat)
856 {
857 int err;
858 struct v9fs_session_info *v9ses;
859 struct p9_fid *fid;
860 struct p9_wstat *st;
861
862 P9_DPRINTK(P9_DEBUG_VFS, "dentry: %p\n", dentry);
863 err = -EPERM;
864 v9ses = v9fs_inode2v9ses(dentry->d_inode);
865 if (v9ses->cache == CACHE_LOOSE || v9ses->cache == CACHE_FSCACHE)
866 return simple_getattr(mnt, dentry, stat);
867
868 fid = v9fs_fid_lookup(dentry);
869 if (IS_ERR(fid))
870 return PTR_ERR(fid);
871
872 st = p9_client_stat(fid);
873 if (IS_ERR(st))
874 return PTR_ERR(st);
875
876 v9fs_stat2inode(st, dentry->d_inode, dentry->d_inode->i_sb);
877 generic_fillattr(dentry->d_inode, stat);
878
879 p9stat_free(st);
880 kfree(st);
881 return 0;
882 }
883
884 /**
885 * v9fs_vfs_setattr - set file metadata
886 * @dentry: file whose metadata to set
887 * @iattr: metadata assignment structure
888 *
889 */
890
891 static int v9fs_vfs_setattr(struct dentry *dentry, struct iattr *iattr)
892 {
893 int retval;
894 struct v9fs_session_info *v9ses;
895 struct p9_fid *fid;
896 struct p9_wstat wstat;
897
898 P9_DPRINTK(P9_DEBUG_VFS, "\n");
899 retval = -EPERM;
900 v9ses = v9fs_inode2v9ses(dentry->d_inode);
901 fid = v9fs_fid_lookup(dentry);
902 if(IS_ERR(fid))
903 return PTR_ERR(fid);
904
905 v9fs_blank_wstat(&wstat);
906 if (iattr->ia_valid & ATTR_MODE)
907 wstat.mode = unixmode2p9mode(v9ses, iattr->ia_mode);
908
909 if (iattr->ia_valid & ATTR_MTIME)
910 wstat.mtime = iattr->ia_mtime.tv_sec;
911
912 if (iattr->ia_valid & ATTR_ATIME)
913 wstat.atime = iattr->ia_atime.tv_sec;
914
915 if (iattr->ia_valid & ATTR_SIZE)
916 wstat.length = iattr->ia_size;
917
918 if (v9fs_proto_dotu(v9ses)) {
919 if (iattr->ia_valid & ATTR_UID)
920 wstat.n_uid = iattr->ia_uid;
921
922 if (iattr->ia_valid & ATTR_GID)
923 wstat.n_gid = iattr->ia_gid;
924 }
925
926 retval = p9_client_wstat(fid, &wstat);
927 if (retval < 0)
928 return retval;
929
930 if ((iattr->ia_valid & ATTR_SIZE) &&
931 iattr->ia_size != i_size_read(dentry->d_inode)) {
932 retval = vmtruncate(dentry->d_inode, iattr->ia_size);
933 if (retval)
934 return retval;
935 }
936
937 setattr_copy(dentry->d_inode, iattr);
938 mark_inode_dirty(dentry->d_inode);
939 return 0;
940 }
941
942 /**
943 * v9fs_stat2inode - populate an inode structure with mistat info
944 * @stat: Plan 9 metadata (mistat) structure
945 * @inode: inode to populate
946 * @sb: superblock of filesystem
947 *
948 */
949
950 void
951 v9fs_stat2inode(struct p9_wstat *stat, struct inode *inode,
952 struct super_block *sb)
953 {
954 char ext[32];
955 char tag_name[14];
956 unsigned int i_nlink;
957 struct v9fs_session_info *v9ses = sb->s_fs_info;
958
959 inode->i_nlink = 1;
960
961 inode->i_atime.tv_sec = stat->atime;
962 inode->i_mtime.tv_sec = stat->mtime;
963 inode->i_ctime.tv_sec = stat->mtime;
964
965 inode->i_uid = v9ses->dfltuid;
966 inode->i_gid = v9ses->dfltgid;
967
968 if (v9fs_proto_dotu(v9ses)) {
969 inode->i_uid = stat->n_uid;
970 inode->i_gid = stat->n_gid;
971 }
972 if ((S_ISREG(inode->i_mode)) || (S_ISDIR(inode->i_mode))) {
973 if (v9fs_proto_dotu(v9ses) && (stat->extension[0] != '\0')) {
974 /*
975 * Hadlink support got added later to
976 * to the .u extension. So there can be
977 * server out there that doesn't support
978 * this even with .u extension. So check
979 * for non NULL stat->extension
980 */
981 strncpy(ext, stat->extension, sizeof(ext));
982 /* HARDLINKCOUNT %u */
983 sscanf(ext, "%13s %u", tag_name, &i_nlink);
984 if (!strncmp(tag_name, "HARDLINKCOUNT", 13))
985 inode->i_nlink = i_nlink;
986 }
987 }
988 inode->i_mode = p9mode2unixmode(v9ses, stat->mode);
989 if ((S_ISBLK(inode->i_mode)) || (S_ISCHR(inode->i_mode))) {
990 char type = 0;
991 int major = -1;
992 int minor = -1;
993
994 strncpy(ext, stat->extension, sizeof(ext));
995 sscanf(ext, "%c %u %u", &type, &major, &minor);
996 switch (type) {
997 case 'c':
998 inode->i_mode &= ~S_IFBLK;
999 inode->i_mode |= S_IFCHR;
1000 break;
1001 case 'b':
1002 break;
1003 default:
1004 P9_DPRINTK(P9_DEBUG_ERROR,
1005 "Unknown special type %c %s\n", type,
1006 stat->extension);
1007 };
1008 inode->i_rdev = MKDEV(major, minor);
1009 init_special_inode(inode, inode->i_mode, inode->i_rdev);
1010 } else
1011 inode->i_rdev = 0;
1012
1013 i_size_write(inode, stat->length);
1014
1015 /* not real number of blocks, but 512 byte ones ... */
1016 inode->i_blocks = (i_size_read(inode) + 512 - 1) >> 9;
1017 }
1018
1019 /**
1020 * v9fs_qid2ino - convert qid into inode number
1021 * @qid: qid to hash
1022 *
1023 * BUG: potential for inode number collisions?
1024 */
1025
1026 ino_t v9fs_qid2ino(struct p9_qid *qid)
1027 {
1028 u64 path = qid->path + 2;
1029 ino_t i = 0;
1030
1031 if (sizeof(ino_t) == sizeof(path))
1032 memcpy(&i, &path, sizeof(ino_t));
1033 else
1034 i = (ino_t) (path ^ (path >> 32));
1035
1036 return i;
1037 }
1038
1039 /**
1040 * v9fs_readlink - read a symlink's location (internal version)
1041 * @dentry: dentry for symlink
1042 * @buffer: buffer to load symlink location into
1043 * @buflen: length of buffer
1044 *
1045 */
1046
1047 static int v9fs_readlink(struct dentry *dentry, char *buffer, int buflen)
1048 {
1049 int retval;
1050
1051 struct v9fs_session_info *v9ses;
1052 struct p9_fid *fid;
1053 struct p9_wstat *st;
1054
1055 P9_DPRINTK(P9_DEBUG_VFS, " %s\n", dentry->d_name.name);
1056 retval = -EPERM;
1057 v9ses = v9fs_inode2v9ses(dentry->d_inode);
1058 fid = v9fs_fid_lookup(dentry);
1059 if (IS_ERR(fid))
1060 return PTR_ERR(fid);
1061
1062 if (!v9fs_proto_dotu(v9ses))
1063 return -EBADF;
1064
1065 st = p9_client_stat(fid);
1066 if (IS_ERR(st))
1067 return PTR_ERR(st);
1068
1069 if (!(st->mode & P9_DMSYMLINK)) {
1070 retval = -EINVAL;
1071 goto done;
1072 }
1073
1074 /* copy extension buffer into buffer */
1075 strncpy(buffer, st->extension, buflen);
1076
1077 P9_DPRINTK(P9_DEBUG_VFS,
1078 "%s -> %s (%s)\n", dentry->d_name.name, st->extension, buffer);
1079
1080 retval = strnlen(buffer, buflen);
1081 done:
1082 p9stat_free(st);
1083 kfree(st);
1084 return retval;
1085 }
1086
1087 /**
1088 * v9fs_vfs_follow_link - follow a symlink path
1089 * @dentry: dentry for symlink
1090 * @nd: nameidata
1091 *
1092 */
1093
1094 static void *v9fs_vfs_follow_link(struct dentry *dentry, struct nameidata *nd)
1095 {
1096 int len = 0;
1097 char *link = __getname();
1098
1099 P9_DPRINTK(P9_DEBUG_VFS, "%s n", dentry->d_name.name);
1100
1101 if (!link)
1102 link = ERR_PTR(-ENOMEM);
1103 else {
1104 len = v9fs_readlink(dentry, link, PATH_MAX);
1105
1106 if (len < 0) {
1107 __putname(link);
1108 link = ERR_PTR(len);
1109 } else
1110 link[min(len, PATH_MAX-1)] = 0;
1111 }
1112 nd_set_link(nd, link);
1113
1114 return NULL;
1115 }
1116
1117 /**
1118 * v9fs_vfs_put_link - release a symlink path
1119 * @dentry: dentry for symlink
1120 * @nd: nameidata
1121 * @p: unused
1122 *
1123 */
1124
1125 void
1126 v9fs_vfs_put_link(struct dentry *dentry, struct nameidata *nd, void *p)
1127 {
1128 char *s = nd_get_link(nd);
1129
1130 P9_DPRINTK(P9_DEBUG_VFS, " %s %s\n", dentry->d_name.name,
1131 IS_ERR(s) ? "<error>" : s);
1132 if (!IS_ERR(s))
1133 __putname(s);
1134 }
1135
1136 /**
1137 * v9fs_vfs_mkspecial - create a special file
1138 * @dir: inode to create special file in
1139 * @dentry: dentry to create
1140 * @mode: mode to create special file
1141 * @extension: 9p2000.u format extension string representing special file
1142 *
1143 */
1144
1145 static int v9fs_vfs_mkspecial(struct inode *dir, struct dentry *dentry,
1146 int mode, const char *extension)
1147 {
1148 u32 perm;
1149 struct v9fs_session_info *v9ses;
1150 struct p9_fid *fid;
1151
1152 v9ses = v9fs_inode2v9ses(dir);
1153 if (!v9fs_proto_dotu(v9ses)) {
1154 P9_DPRINTK(P9_DEBUG_ERROR, "not extended\n");
1155 return -EPERM;
1156 }
1157
1158 perm = unixmode2p9mode(v9ses, mode);
1159 fid = v9fs_create(v9ses, dir, dentry, (char *) extension, perm,
1160 P9_OREAD);
1161 if (IS_ERR(fid))
1162 return PTR_ERR(fid);
1163
1164 p9_client_clunk(fid);
1165 return 0;
1166 }
1167
1168 /**
1169 * v9fs_vfs_symlink - helper function to create symlinks
1170 * @dir: directory inode containing symlink
1171 * @dentry: dentry for symlink
1172 * @symname: symlink data
1173 *
1174 * See Also: 9P2000.u RFC for more information
1175 *
1176 */
1177
1178 static int
1179 v9fs_vfs_symlink(struct inode *dir, struct dentry *dentry, const char *symname)
1180 {
1181 P9_DPRINTK(P9_DEBUG_VFS, " %lu,%s,%s\n", dir->i_ino,
1182 dentry->d_name.name, symname);
1183
1184 return v9fs_vfs_mkspecial(dir, dentry, S_IFLNK, symname);
1185 }
1186
1187 /**
1188 * v9fs_vfs_link - create a hardlink
1189 * @old_dentry: dentry for file to link to
1190 * @dir: inode destination for new link
1191 * @dentry: dentry for link
1192 *
1193 */
1194
1195 static int
1196 v9fs_vfs_link(struct dentry *old_dentry, struct inode *dir,
1197 struct dentry *dentry)
1198 {
1199 int retval;
1200 struct p9_fid *oldfid;
1201 char *name;
1202
1203 P9_DPRINTK(P9_DEBUG_VFS,
1204 " %lu,%s,%s\n", dir->i_ino, dentry->d_name.name,
1205 old_dentry->d_name.name);
1206
1207 oldfid = v9fs_fid_clone(old_dentry);
1208 if (IS_ERR(oldfid))
1209 return PTR_ERR(oldfid);
1210
1211 name = __getname();
1212 if (unlikely(!name)) {
1213 retval = -ENOMEM;
1214 goto clunk_fid;
1215 }
1216
1217 sprintf(name, "%d\n", oldfid->fid);
1218 retval = v9fs_vfs_mkspecial(dir, dentry, P9_DMLINK, name);
1219 __putname(name);
1220
1221 clunk_fid:
1222 p9_client_clunk(oldfid);
1223 return retval;
1224 }
1225
1226 /**
1227 * v9fs_vfs_mknod - create a special file
1228 * @dir: inode destination for new link
1229 * @dentry: dentry for file
1230 * @mode: mode for creation
1231 * @rdev: device associated with special file
1232 *
1233 */
1234
1235 static int
1236 v9fs_vfs_mknod(struct inode *dir, struct dentry *dentry, int mode, dev_t rdev)
1237 {
1238 int retval;
1239 char *name;
1240
1241 P9_DPRINTK(P9_DEBUG_VFS,
1242 " %lu,%s mode: %x MAJOR: %u MINOR: %u\n", dir->i_ino,
1243 dentry->d_name.name, mode, MAJOR(rdev), MINOR(rdev));
1244
1245 if (!new_valid_dev(rdev))
1246 return -EINVAL;
1247
1248 name = __getname();
1249 if (!name)
1250 return -ENOMEM;
1251 /* build extension */
1252 if (S_ISBLK(mode))
1253 sprintf(name, "b %u %u", MAJOR(rdev), MINOR(rdev));
1254 else if (S_ISCHR(mode))
1255 sprintf(name, "c %u %u", MAJOR(rdev), MINOR(rdev));
1256 else if (S_ISFIFO(mode))
1257 *name = 0;
1258 else if (S_ISSOCK(mode))
1259 *name = 0;
1260 else {
1261 __putname(name);
1262 return -EINVAL;
1263 }
1264
1265 retval = v9fs_vfs_mkspecial(dir, dentry, mode, name);
1266 __putname(name);
1267
1268 return retval;
1269 }
1270
1271 static const struct inode_operations v9fs_dir_inode_operations_dotu = {
1272 .create = v9fs_vfs_create,
1273 .lookup = v9fs_vfs_lookup,
1274 .symlink = v9fs_vfs_symlink,
1275 .link = v9fs_vfs_link,
1276 .unlink = v9fs_vfs_unlink,
1277 .mkdir = v9fs_vfs_mkdir,
1278 .rmdir = v9fs_vfs_rmdir,
1279 .mknod = v9fs_vfs_mknod,
1280 .rename = v9fs_vfs_rename,
1281 .getattr = v9fs_vfs_getattr,
1282 .setattr = v9fs_vfs_setattr,
1283 };
1284
1285 static const struct inode_operations v9fs_dir_inode_operations = {
1286 .create = v9fs_vfs_create,
1287 .lookup = v9fs_vfs_lookup,
1288 .unlink = v9fs_vfs_unlink,
1289 .mkdir = v9fs_vfs_mkdir,
1290 .rmdir = v9fs_vfs_rmdir,
1291 .mknod = v9fs_vfs_mknod,
1292 .rename = v9fs_vfs_rename,
1293 .getattr = v9fs_vfs_getattr,
1294 .setattr = v9fs_vfs_setattr,
1295 };
1296
1297 static const struct inode_operations v9fs_file_inode_operations = {
1298 .getattr = v9fs_vfs_getattr,
1299 .setattr = v9fs_vfs_setattr,
1300 };
1301
1302 static const struct inode_operations v9fs_symlink_inode_operations = {
1303 .readlink = generic_readlink,
1304 .follow_link = v9fs_vfs_follow_link,
1305 .put_link = v9fs_vfs_put_link,
1306 .getattr = v9fs_vfs_getattr,
1307 .setattr = v9fs_vfs_setattr,
1308 };
1309
This page took 0.134475 seconds and 5 git commands to generate.