aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--fs/9p/vfs_inode.c1371
1 files changed, 1371 insertions, 0 deletions
diff --git a/fs/9p/vfs_inode.c b/fs/9p/vfs_inode.c
new file mode 100644
index 000000000000..ef78af7ef04b
--- /dev/null
+++ b/fs/9p/vfs_inode.c
@@ -0,0 +1,1371 @@
1/*
2 * linux/fs/9p/vfs_inode.c
3 *
4 * This file contians 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 as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
13 *
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to:
21 * Free Software Foundation
22 * 51 Franklin Street, Fifth Floor
23 * Boston, MA 02111-1301 USA
24 *
25 */
26
27#include <linux/module.h>
28#include <linux/errno.h>
29#include <linux/fs.h>
30#include <linux/file.h>
31#include <linux/pagemap.h>
32#include <linux/stat.h>
33#include <linux/string.h>
34#include <linux/smp_lock.h>
35#include <linux/inet.h>
36#include <linux/namei.h>
37#include <linux/idr.h>
38
39#include "debug.h"
40#include "v9fs.h"
41#include "9p.h"
42#include "v9fs_vfs.h"
43#include "conv.h"
44#include "fid.h"
45
46static struct inode_operations v9fs_dir_inode_operations;
47static struct inode_operations v9fs_file_inode_operations;
48static struct inode_operations v9fs_symlink_inode_operations;
49
50/**
51 * unixmode2p9mode - convert unix mode bits to plan 9
52 * @v9ses: v9fs session information
53 * @mode: mode to convert
54 *
55 */
56
57static inline int unixmode2p9mode(struct v9fs_session_info *v9ses, int mode)
58{
59 int res;
60 res = mode & 0777;
61 if (S_ISDIR(mode))
62 res |= V9FS_DMDIR;
63 if (v9ses->extended) {
64 if (S_ISLNK(mode))
65 res |= V9FS_DMSYMLINK;
66 if (v9ses->nodev == 0) {
67 if (S_ISSOCK(mode))
68 res |= V9FS_DMSOCKET;
69 if (S_ISFIFO(mode))
70 res |= V9FS_DMNAMEDPIPE;
71 if (S_ISBLK(mode))
72 res |= V9FS_DMDEVICE;
73 if (S_ISCHR(mode))
74 res |= V9FS_DMDEVICE;
75 }
76
77 if ((mode & S_ISUID) == S_ISUID)
78 res |= V9FS_DMSETUID;
79 if ((mode & S_ISGID) == S_ISGID)
80 res |= V9FS_DMSETGID;
81 if ((mode & V9FS_DMLINK))
82 res |= V9FS_DMLINK;
83 }
84
85 return res;
86}
87
88/**
89 * p9mode2unixmode- convert plan9 mode bits to unix mode bits
90 * @v9ses: v9fs session information
91 * @mode: mode to convert
92 *
93 */
94
95static inline int p9mode2unixmode(struct v9fs_session_info *v9ses, int mode)
96{
97 int res;
98
99 res = mode & 0777;
100
101 if ((mode & V9FS_DMDIR) == V9FS_DMDIR)
102 res |= S_IFDIR;
103 else if ((mode & V9FS_DMSYMLINK) && (v9ses->extended))
104 res |= S_IFLNK;
105 else if ((mode & V9FS_DMSOCKET) && (v9ses->extended)
106 && (v9ses->nodev == 0))
107 res |= S_IFSOCK;
108 else if ((mode & V9FS_DMNAMEDPIPE) && (v9ses->extended)
109 && (v9ses->nodev == 0))
110 res |= S_IFIFO;
111 else if ((mode & V9FS_DMDEVICE) && (v9ses->extended)
112 && (v9ses->nodev == 0))
113 res |= S_IFBLK;
114 else
115 res |= S_IFREG;
116
117 if (v9ses->extended) {
118 if ((mode & V9FS_DMSETUID) == V9FS_DMSETUID)
119 res |= S_ISUID;
120
121 if ((mode & V9FS_DMSETGID) == V9FS_DMSETGID)
122 res |= S_ISGID;
123 }
124
125 return res;
126}
127
128/**
129 * v9fs_blank_mistat - helper function to setup a 9P stat structure
130 * @v9ses: 9P session info (for determining extended mode)
131 * @mistat: structure to initialize
132 *
133 */
134
135static inline void
136v9fs_blank_mistat(struct v9fs_session_info *v9ses, struct v9fs_stat *mistat)
137{
138 mistat->type = ~0;
139 mistat->dev = ~0;
140 mistat->qid.type = ~0;
141 mistat->qid.version = ~0;
142 *((long long *)&mistat->qid.path) = ~0;
143 mistat->mode = ~0;
144 mistat->atime = ~0;
145 mistat->mtime = ~0;
146 mistat->length = ~0;
147 mistat->name = mistat->data;
148 mistat->uid = mistat->data;
149 mistat->gid = mistat->data;
150 mistat->muid = mistat->data;
151 if (v9ses->extended) {
152 mistat->n_uid = ~0;
153 mistat->n_gid = ~0;
154 mistat->n_muid = ~0;
155 mistat->extension = mistat->data;
156 }
157 *mistat->data = 0;
158}
159
160/**
161 * v9fs_mistat2unix - convert mistat to unix stat
162 * @mistat: Plan 9 metadata (mistat) structure
163 * @stat: unix metadata (stat) structure to populate
164 * @sb: superblock
165 *
166 */
167
168static void
169v9fs_mistat2unix(struct v9fs_stat *mistat, struct stat *buf,
170 struct super_block *sb)
171{
172 struct v9fs_session_info *v9ses = sb ? sb->s_fs_info : NULL;
173
174 buf->st_nlink = 1;
175
176 buf->st_atime = mistat->atime;
177 buf->st_mtime = mistat->mtime;
178 buf->st_ctime = mistat->mtime;
179
180 if (v9ses && v9ses->extended) {
181 /* TODO: string to uid mapping via user-space daemon */
182 buf->st_uid = mistat->n_uid;
183 buf->st_gid = mistat->n_gid;
184
185 sscanf(mistat->uid, "%x", (unsigned int *)&buf->st_uid);
186 sscanf(mistat->gid, "%x", (unsigned int *)&buf->st_gid);
187 } else {
188 buf->st_uid = v9ses->uid;
189 buf->st_gid = v9ses->gid;
190 }
191
192 buf->st_uid = (unsigned short)-1;
193 buf->st_gid = (unsigned short)-1;
194
195 if (v9ses && v9ses->extended) {
196 if (mistat->n_uid != -1)
197 sscanf(mistat->uid, "%x", (unsigned int *)&buf->st_uid);
198
199 if (mistat->n_gid != -1)
200 sscanf(mistat->gid, "%x", (unsigned int *)&buf->st_gid);
201 }
202
203 if (buf->st_uid == (unsigned short)-1)
204 buf->st_uid = v9ses->uid;
205 if (buf->st_gid == (unsigned short)-1)
206 buf->st_gid = v9ses->gid;
207
208 buf->st_mode = p9mode2unixmode(v9ses, mistat->mode);
209 if ((S_ISBLK(buf->st_mode)) || (S_ISCHR(buf->st_mode))) {
210 char type = 0;
211 int major = -1;
212 int minor = -1;
213 sscanf(mistat->extension, "%c %u %u", &type, &major, &minor);
214 switch (type) {
215 case 'c':
216 buf->st_mode &= ~S_IFBLK;
217 buf->st_mode |= S_IFCHR;
218 break;
219 case 'b':
220 break;
221 default:
222 dprintk(DEBUG_ERROR, "Unknown special type %c (%s)\n",
223 type, mistat->extension);
224 };
225 buf->st_rdev = MKDEV(major, minor);
226 } else
227 buf->st_rdev = 0;
228
229 buf->st_size = mistat->length;
230
231 buf->st_blksize = sb->s_blocksize;
232 buf->st_blocks =
233 (buf->st_size + buf->st_blksize - 1) >> sb->s_blocksize_bits;
234}
235
236/**
237 * v9fs_get_inode - helper function to setup an inode
238 * @sb: superblock
239 * @mode: mode to setup inode with
240 *
241 */
242
243struct inode *v9fs_get_inode(struct super_block *sb, int mode)
244{
245 struct inode *inode = NULL;
246
247 dprintk(DEBUG_VFS, "super block: %p mode: %o\n", sb, mode);
248
249 inode = new_inode(sb);
250 if (inode) {
251 inode->i_mode = mode;
252 inode->i_uid = current->fsuid;
253 inode->i_gid = current->fsgid;
254 inode->i_blksize = sb->s_blocksize;
255 inode->i_blocks = 0;
256 inode->i_rdev = 0;
257 inode->i_atime = inode->i_mtime = inode->i_ctime = CURRENT_TIME;
258
259 switch (mode & S_IFMT) {
260 case S_IFIFO:
261 case S_IFBLK:
262 case S_IFCHR:
263 case S_IFSOCK:
264 case S_IFREG:
265 inode->i_op = &v9fs_file_inode_operations;
266 inode->i_fop = &v9fs_file_operations;
267 break;
268 case S_IFDIR:
269 inode->i_nlink++;
270 inode->i_op = &v9fs_dir_inode_operations;
271 inode->i_fop = &v9fs_dir_operations;
272 break;
273 case S_IFLNK:
274 inode->i_op = &v9fs_symlink_inode_operations;
275 break;
276 default:
277 dprintk(DEBUG_ERROR, "BAD mode 0x%x S_IFMT 0x%x\n",
278 mode, mode & S_IFMT);
279 return ERR_PTR(-EINVAL);
280 }
281 } else {
282 eprintk(KERN_WARNING, "Problem allocating inode\n");
283 return ERR_PTR(-ENOMEM);
284 }
285 return inode;
286}
287
288/**
289 * v9fs_create - helper function to create files and directories
290 * @dir: directory inode file is being created in
291 * @file_dentry: dentry file is being created in
292 * @perm: permissions file is being created with
293 * @open_mode: resulting open mode for file ???
294 *
295 */
296
297static int
298v9fs_create(struct inode *dir,
299 struct dentry *file_dentry,
300 unsigned int perm, unsigned int open_mode)
301{
302 struct v9fs_session_info *v9ses = v9fs_inode2v9ses(dir);
303 struct super_block *sb = dir->i_sb;
304 struct v9fs_fid *dirfid =
305 v9fs_fid_lookup(file_dentry->d_parent, FID_WALK);
306 struct v9fs_fid *fid = NULL;
307 struct inode *file_inode = NULL;
308 struct v9fs_fcall *fcall = NULL;
309 struct v9fs_qid qid;
310 struct stat newstat;
311 int dirfidnum = -1;
312 long newfid = -1;
313 int result = 0;
314 unsigned int iounit = 0;
315
316 perm = unixmode2p9mode(v9ses, perm);
317
318 dprintk(DEBUG_VFS, "dir: %p dentry: %p perm: %o mode: %o\n", dir,
319 file_dentry, perm, open_mode);
320
321 if (!dirfid)
322 return -EBADF;
323
324 dirfidnum = dirfid->fid;
325 if (dirfidnum < 0) {
326 dprintk(DEBUG_ERROR, "No fid for the directory #%lu\n",
327 dir->i_ino);
328 return -EBADF;
329 }
330
331 if (file_dentry->d_inode) {
332 dprintk(DEBUG_ERROR,
333 "Odd. There is an inode for dir %lu, name :%s:\n",
334 dir->i_ino, file_dentry->d_name.name);
335 return -EEXIST;
336 }
337
338 newfid = v9fs_get_idpool(&v9ses->fidpool);
339 if (newfid < 0) {
340 eprintk(KERN_WARNING, "no free fids available\n");
341 return -ENOSPC;
342 }
343
344 result = v9fs_t_walk(v9ses, dirfidnum, newfid, NULL, &fcall);
345 if (result < 0) {
346 dprintk(DEBUG_ERROR, "clone error: %s\n", FCALL_ERROR(fcall));
347 v9fs_put_idpool(newfid, &v9ses->fidpool);
348 newfid = 0;
349 goto CleanUpFid;
350 }
351
352 kfree(fcall);
353
354 result = v9fs_t_create(v9ses, newfid, (char *)file_dentry->d_name.name,
355 perm, open_mode, &fcall);
356 if (result < 0) {
357 dprintk(DEBUG_ERROR, "create fails: %s(%d)\n",
358 FCALL_ERROR(fcall), result);
359
360 goto CleanUpFid;
361 }
362
363 iounit = fcall->params.rcreate.iounit;
364 qid = fcall->params.rcreate.qid;
365 kfree(fcall);
366
367 fid = v9fs_fid_create(file_dentry);
368 if (!fid) {
369 result = -ENOMEM;
370 goto CleanUpFid;
371 }
372
373 fid->fid = newfid;
374 fid->fidopen = 0;
375 fid->fidcreate = 1;
376 fid->qid = qid;
377 fid->iounit = iounit;
378 fid->rdir_pos = 0;
379 fid->rdir_fcall = NULL;
380 fid->v9ses = v9ses;
381
382 if ((perm & V9FS_DMSYMLINK) || (perm & V9FS_DMLINK) ||
383 (perm & V9FS_DMNAMEDPIPE) || (perm & V9FS_DMSOCKET) ||
384 (perm & V9FS_DMDEVICE))
385 return 0;
386
387 result = v9fs_t_stat(v9ses, newfid, &fcall);
388 if (result < 0) {
389 dprintk(DEBUG_ERROR, "stat error: %s(%d)\n", FCALL_ERROR(fcall),
390 result);
391 goto CleanUpFid;
392 }
393
394 v9fs_mistat2unix(fcall->params.rstat.stat, &newstat, sb);
395
396 file_inode = v9fs_get_inode(sb, newstat.st_mode);
397 if ((!file_inode) || IS_ERR(file_inode)) {
398 dprintk(DEBUG_ERROR, "create inode failed\n");
399 result = -EBADF;
400 goto CleanUpFid;
401 }
402
403 v9fs_mistat2inode(fcall->params.rstat.stat, file_inode, sb);
404 kfree(fcall);
405 d_instantiate(file_dentry, file_inode);
406
407 if (perm & V9FS_DMDIR) {
408 if (v9fs_t_clunk(v9ses, newfid, &fcall))
409 dprintk(DEBUG_ERROR, "clunk for mkdir failed: %s\n",
410 FCALL_ERROR(fcall));
411
412 v9fs_put_idpool(newfid, &v9ses->fidpool);
413 kfree(fcall);
414 fid->fidopen = 0;
415 fid->fidcreate = 0;
416 d_drop(file_dentry);
417 }
418
419 return 0;
420
421 CleanUpFid:
422 kfree(fcall);
423
424 if (newfid) {
425 if (v9fs_t_clunk(v9ses, newfid, &fcall))
426 dprintk(DEBUG_ERROR, "clunk failed: %s\n",
427 FCALL_ERROR(fcall));
428
429 v9fs_put_idpool(newfid, &v9ses->fidpool);
430 kfree(fcall);
431 }
432 return result;
433}
434
435/**
436 * v9fs_remove - helper function to remove files and directories
437 * @inode: directory inode that is being deleted
438 * @dentry: dentry that is being deleted
439 * @rmdir: where we are a file or a directory
440 *
441 */
442
443static int v9fs_remove(struct inode *dir, struct dentry *file, int rmdir)
444{
445 struct v9fs_fcall *fcall = NULL;
446 struct super_block *sb = NULL;
447 struct v9fs_session_info *v9ses = NULL;
448 struct v9fs_fid *v9fid = NULL;
449 struct inode *file_inode = NULL;
450 int fid = -1;
451 int result = 0;
452
453 dprintk(DEBUG_VFS, "inode: %p dentry: %p rmdir: %d\n", dir, file,
454 rmdir);
455
456 file_inode = file->d_inode;
457 sb = file_inode->i_sb;
458 v9ses = v9fs_inode2v9ses(file_inode);
459 v9fid = v9fs_fid_lookup(file, FID_OP);
460
461 if (!v9fid) {
462 dprintk(DEBUG_ERROR,
463 "no v9fs_fid\n");
464 return -EBADF;
465 }
466
467 fid = v9fid->fid;
468 if (fid < 0) {
469 dprintk(DEBUG_ERROR, "inode #%lu, no fid!\n",
470 file_inode->i_ino);
471 return -EBADF;
472 }
473
474 result = v9fs_t_remove(v9ses, fid, &fcall);
475 if (result < 0)
476 dprintk(DEBUG_ERROR, "remove of file fails: %s(%d)\n",
477 FCALL_ERROR(fcall), result);
478 else {
479 v9fs_put_idpool(fid, &v9ses->fidpool);
480 v9fs_fid_destroy(v9fid);
481 }
482
483 kfree(fcall);
484 return result;
485}
486
487/**
488 * v9fs_vfs_create - VFS hook to create files
489 * @inode: directory inode that is being deleted
490 * @dentry: dentry that is being deleted
491 * @perm: create permissions
492 * @nd: path information
493 *
494 */
495
496static int
497v9fs_vfs_create(struct inode *inode, struct dentry *dentry, int perm,
498 struct nameidata *nd)
499{
500 return v9fs_create(inode, dentry, perm, O_RDWR);
501}
502
503/**
504 * v9fs_vfs_mkdir - VFS mkdir hook to create a directory
505 * @i: inode that is being unlinked
506 * @dentry: dentry that is being unlinked
507 * @mode: mode for new directory
508 *
509 */
510
511static int v9fs_vfs_mkdir(struct inode *inode, struct dentry *dentry, int mode)
512{
513 return v9fs_create(inode, dentry, mode | S_IFDIR, O_RDONLY);
514}
515
516/**
517 * v9fs_vfs_lookup - VFS lookup hook to "walk" to a new inode
518 * @dir: inode that is being walked from
519 * @dentry: dentry that is being walked to?
520 * @nameidata: path data
521 *
522 */
523
524static struct dentry *v9fs_vfs_lookup(struct inode *dir, struct dentry *dentry,
525 struct nameidata *nameidata)
526{
527 struct super_block *sb;
528 struct v9fs_session_info *v9ses;
529 struct v9fs_fid *dirfid;
530 struct v9fs_fid *fid;
531 struct inode *inode;
532 struct v9fs_fcall *fcall = NULL;
533 struct stat newstat;
534 int dirfidnum = -1;
535 int newfid = -1;
536 int result = 0;
537
538 dprintk(DEBUG_VFS, "dir: %p dentry: (%s) %p nameidata: %p\n",
539 dir, dentry->d_iname, dentry, nameidata);
540
541 sb = dir->i_sb;
542 v9ses = v9fs_inode2v9ses(dir);
543 dirfid = v9fs_fid_lookup(dentry->d_parent, FID_WALK);
544
545 if (!dirfid) {
546 dprintk(DEBUG_ERROR, "no dirfid\n");
547 return ERR_PTR(-EINVAL);
548 }
549
550 dirfidnum = dirfid->fid;
551
552 if (dirfidnum < 0) {
553 dprintk(DEBUG_ERROR, "no dirfid for inode %p, #%lu\n",
554 dir, dir->i_ino);
555 return ERR_PTR(-EBADF);
556 }
557
558 newfid = v9fs_get_idpool(&v9ses->fidpool);
559 if (newfid < 0) {
560 eprintk(KERN_WARNING, "newfid fails!\n");
561 return ERR_PTR(-ENOSPC);
562 }
563
564 result =
565 v9fs_t_walk(v9ses, dirfidnum, newfid, (char *)dentry->d_name.name,
566 NULL);
567 if (result < 0) {
568 v9fs_put_idpool(newfid, &v9ses->fidpool);
569 if (result == -ENOENT) {
570 d_add(dentry, NULL);
571 dprintk(DEBUG_ERROR,
572 "Return negative dentry %p count %d\n",
573 dentry, atomic_read(&dentry->d_count));
574 return NULL;
575 }
576 dprintk(DEBUG_ERROR, "walk error:%d\n", result);
577 goto FreeFcall;
578 }
579
580 result = v9fs_t_stat(v9ses, newfid, &fcall);
581 if (result < 0) {
582 dprintk(DEBUG_ERROR, "stat error\n");
583 goto FreeFcall;
584 }
585
586 v9fs_mistat2unix(fcall->params.rstat.stat, &newstat, sb);
587 inode = v9fs_get_inode(sb, newstat.st_mode);
588
589 if (IS_ERR(inode) && (PTR_ERR(inode) == -ENOSPC)) {
590 eprintk(KERN_WARNING, "inode alloc failes, returns %ld\n",
591 PTR_ERR(inode));
592
593 result = -ENOSPC;
594 goto FreeFcall;
595 }
596
597 inode->i_ino = v9fs_qid2ino(&fcall->params.rstat.stat->qid);
598
599 fid = v9fs_fid_create(dentry);
600 if (fid == NULL) {
601 dprintk(DEBUG_ERROR, "couldn't insert\n");
602 result = -ENOMEM;
603 goto FreeFcall;
604 }
605
606 fid->fid = newfid;
607 fid->fidopen = 0;
608 fid->v9ses = v9ses;
609 fid->qid = fcall->params.rstat.stat->qid;
610
611 dentry->d_op = &v9fs_dentry_operations;
612 v9fs_mistat2inode(fcall->params.rstat.stat, inode, inode->i_sb);
613
614 d_add(dentry, inode);
615 kfree(fcall);
616
617 return NULL;
618
619 FreeFcall:
620 kfree(fcall);
621 return ERR_PTR(result);
622}
623
624/**
625 * v9fs_vfs_unlink - VFS unlink hook to delete an inode
626 * @i: inode that is being unlinked
627 * @dentry: dentry that is being unlinked
628 *
629 */
630
631static int v9fs_vfs_unlink(struct inode *i, struct dentry *d)
632{
633 return v9fs_remove(i, d, 0);
634}
635
636/**
637 * v9fs_vfs_rmdir - VFS unlink hook to delete a directory
638 * @i: inode that is being unlinked
639 * @dentry: dentry that is being unlinked
640 *
641 */
642
643static int v9fs_vfs_rmdir(struct inode *i, struct dentry *d)
644{
645 return v9fs_remove(i, d, 1);
646}
647
648/**
649 * v9fs_vfs_rename - VFS hook to rename an inode
650 * @old_dir: old dir inode
651 * @old_dentry: old dentry
652 * @new_dir: new dir inode
653 * @new_dentry: new dentry
654 *
655 */
656
657static int
658v9fs_vfs_rename(struct inode *old_dir, struct dentry *old_dentry,
659 struct inode *new_dir, struct dentry *new_dentry)
660{
661 struct inode *old_inode = old_dentry->d_inode;
662 struct v9fs_session_info *v9ses = v9fs_inode2v9ses(old_inode);
663 struct v9fs_fid *oldfid = v9fs_fid_lookup(old_dentry, FID_WALK);
664 struct v9fs_fid *olddirfid =
665 v9fs_fid_lookup(old_dentry->d_parent, FID_WALK);
666 struct v9fs_fid *newdirfid =
667 v9fs_fid_lookup(new_dentry->d_parent, FID_WALK);
668 struct v9fs_stat *mistat = kmalloc(v9ses->maxdata, GFP_KERNEL);
669 struct v9fs_fcall *fcall = NULL;
670 int fid = -1;
671 int olddirfidnum = -1;
672 int newdirfidnum = -1;
673 int retval = 0;
674
675 dprintk(DEBUG_VFS, "\n");
676
677 if ((!oldfid) || (!olddirfid) || (!newdirfid)) {
678 dprintk(DEBUG_ERROR, "problem with arguments\n");
679 return -EBADF;
680 }
681
682 /* 9P can only handle file rename in the same directory */
683 if (memcmp(&olddirfid->qid, &newdirfid->qid, sizeof(newdirfid->qid))) {
684 dprintk(DEBUG_ERROR, "old dir and new dir are different\n");
685 retval = -EPERM;
686 goto FreeFcallnBail;
687 }
688
689 fid = oldfid->fid;
690 olddirfidnum = olddirfid->fid;
691 newdirfidnum = newdirfid->fid;
692
693 if (fid < 0) {
694 dprintk(DEBUG_ERROR, "no fid for old file #%lu\n",
695 old_inode->i_ino);
696 retval = -EBADF;
697 goto FreeFcallnBail;
698 }
699
700 v9fs_blank_mistat(v9ses, mistat);
701
702 strcpy(mistat->data + 1, v9ses->name);
703 mistat->name = mistat->data + 1 + strlen(v9ses->name);
704
705 if (new_dentry->d_name.len >
706 (v9ses->maxdata - strlen(v9ses->name) - sizeof(struct v9fs_stat))) {
707 dprintk(DEBUG_ERROR, "new name too long\n");
708 goto FreeFcallnBail;
709 }
710
711 strcpy(mistat->name, new_dentry->d_name.name);
712 retval = v9fs_t_wstat(v9ses, fid, mistat, &fcall);
713
714 FreeFcallnBail:
715 kfree(mistat);
716
717 if (retval < 0)
718 dprintk(DEBUG_ERROR, "v9fs_t_wstat error: %s\n",
719 FCALL_ERROR(fcall));
720
721 kfree(fcall);
722 return retval;
723}
724
725/**
726 * v9fs_vfs_getattr - retreive file metadata
727 * @mnt - mount information
728 * @dentry - file to get attributes on
729 * @stat - metadata structure to populate
730 *
731 */
732
733static int
734v9fs_vfs_getattr(struct vfsmount *mnt, struct dentry *dentry,
735 struct kstat *stat)
736{
737 struct v9fs_fcall *fcall = NULL;
738 struct v9fs_session_info *v9ses = v9fs_inode2v9ses(dentry->d_inode);
739 struct v9fs_fid *fid = v9fs_fid_lookup(dentry, FID_OP);
740 int err = -EPERM;
741
742 dprintk(DEBUG_VFS, "dentry: %p\n", dentry);
743 if (!fid) {
744 dprintk(DEBUG_ERROR,
745 "couldn't find fid associated with dentry\n");
746 return -EBADF;
747 }
748
749 err = v9fs_t_stat(v9ses, fid->fid, &fcall);
750
751 if (err < 0)
752 dprintk(DEBUG_ERROR, "stat error\n");
753 else {
754 v9fs_mistat2inode(fcall->params.rstat.stat, dentry->d_inode,
755 dentry->d_inode->i_sb);
756 generic_fillattr(dentry->d_inode, stat);
757 }
758
759 kfree(fcall);
760 return err;
761}
762
763/**
764 * v9fs_vfs_setattr - set file metadata
765 * @dentry: file whose metadata to set
766 * @iattr: metadata assignment structure
767 *
768 */
769
770static int v9fs_vfs_setattr(struct dentry *dentry, struct iattr *iattr)
771{
772 struct v9fs_session_info *v9ses = v9fs_inode2v9ses(dentry->d_inode);
773 struct v9fs_fid *fid = v9fs_fid_lookup(dentry, FID_OP);
774 struct v9fs_stat *mistat = kmalloc(v9ses->maxdata, GFP_KERNEL);
775 struct v9fs_fcall *fcall = NULL;
776 int res = -EPERM;
777
778 dprintk(DEBUG_VFS, "\n");
779 if (!fid) {
780 dprintk(DEBUG_ERROR,
781 "Couldn't find fid associated with dentry\n");
782 return -EBADF;
783 }
784
785 if (!mistat)
786 return -ENOMEM;
787
788 v9fs_blank_mistat(v9ses, mistat);
789 if (iattr->ia_valid & ATTR_MODE)
790 mistat->mode = unixmode2p9mode(v9ses, iattr->ia_mode);
791
792 if (iattr->ia_valid & ATTR_MTIME)
793 mistat->mtime = iattr->ia_mtime.tv_sec;
794
795 if (iattr->ia_valid & ATTR_ATIME)
796 mistat->atime = iattr->ia_atime.tv_sec;
797
798 if (iattr->ia_valid & ATTR_SIZE)
799 mistat->length = iattr->ia_size;
800
801 if (v9ses->extended) {
802 char *uid = kmalloc(strlen(mistat->uid), GFP_KERNEL);
803 char *gid = kmalloc(strlen(mistat->gid), GFP_KERNEL);
804 char *muid = kmalloc(strlen(mistat->muid), GFP_KERNEL);
805 char *name = kmalloc(strlen(mistat->name), GFP_KERNEL);
806 char *extension = kmalloc(strlen(mistat->extension),
807 GFP_KERNEL);
808
809 if ((!uid) || (!gid) || (!muid) || (!name) || (!extension)) {
810 kfree(uid);
811 kfree(gid);
812 kfree(muid);
813 kfree(name);
814 kfree(extension);
815
816 return -ENOMEM;
817 }
818
819 strcpy(uid, mistat->uid);
820 strcpy(gid, mistat->gid);
821 strcpy(muid, mistat->muid);
822 strcpy(name, mistat->name);
823 strcpy(extension, mistat->extension);
824
825 if (iattr->ia_valid & ATTR_UID) {
826 if (strlen(uid) != 8) {
827 dprintk(DEBUG_ERROR, "uid strlen is %u not 8\n",
828 (unsigned int)strlen(uid));
829 sprintf(uid, "%08x", iattr->ia_uid);
830 } else {
831 kfree(uid);
832 uid = kmalloc(9, GFP_KERNEL);
833 }
834
835 sprintf(uid, "%08x", iattr->ia_uid);
836 mistat->n_uid = iattr->ia_uid;
837 }
838
839 if (iattr->ia_valid & ATTR_GID) {
840 if (strlen(gid) != 8)
841 dprintk(DEBUG_ERROR, "gid strlen is %u not 8\n",
842 (unsigned int)strlen(gid));
843 else {
844 kfree(gid);
845 gid = kmalloc(9, GFP_KERNEL);
846 }
847
848 sprintf(gid, "%08x", iattr->ia_gid);
849 mistat->n_gid = iattr->ia_gid;
850 }
851
852 mistat->uid = mistat->data;
853 strcpy(mistat->uid, uid);
854 mistat->gid = mistat->data + strlen(uid) + 1;
855 strcpy(mistat->gid, gid);
856 mistat->muid = mistat->gid + strlen(gid) + 1;
857 strcpy(mistat->muid, muid);
858 mistat->name = mistat->muid + strlen(muid) + 1;
859 strcpy(mistat->name, name);
860 mistat->extension = mistat->name + strlen(name) + 1;
861 strcpy(mistat->extension, extension);
862
863 kfree(uid);
864 kfree(gid);
865 kfree(muid);
866 kfree(name);
867 kfree(extension);
868 }
869
870 res = v9fs_t_wstat(v9ses, fid->fid, mistat, &fcall);
871
872 if (res < 0)
873 dprintk(DEBUG_ERROR, "wstat error: %s\n", FCALL_ERROR(fcall));
874
875 kfree(mistat);
876 kfree(fcall);
877
878 if (res >= 0)
879 res = inode_setattr(dentry->d_inode, iattr);
880
881 return res;
882}
883
884/**
885 * v9fs_mistat2inode - populate an inode structure with mistat info
886 * @mistat: Plan 9 metadata (mistat) structure
887 * @inode: inode to populate
888 * @sb: superblock of filesystem
889 *
890 */
891
892void
893v9fs_mistat2inode(struct v9fs_stat *mistat, struct inode *inode,
894 struct super_block *sb)
895{
896 struct v9fs_session_info *v9ses = sb->s_fs_info;
897
898 inode->i_nlink = 1;
899
900 inode->i_atime.tv_sec = mistat->atime;
901 inode->i_mtime.tv_sec = mistat->mtime;
902 inode->i_ctime.tv_sec = mistat->mtime;
903
904 inode->i_uid = -1;
905 inode->i_gid = -1;
906
907 if (v9ses->extended) {
908 /* TODO: string to uid mapping via user-space daemon */
909 inode->i_uid = mistat->n_uid;
910 inode->i_gid = mistat->n_gid;
911
912 if (mistat->n_uid == -1)
913 sscanf(mistat->uid, "%x", &inode->i_uid);
914
915 if (mistat->n_gid == -1)
916 sscanf(mistat->gid, "%x", &inode->i_gid);
917 }
918
919 if (inode->i_uid == -1)
920 inode->i_uid = v9ses->uid;
921 if (inode->i_gid == -1)
922 inode->i_gid = v9ses->gid;
923
924 inode->i_mode = p9mode2unixmode(v9ses, mistat->mode);
925 if ((S_ISBLK(inode->i_mode)) || (S_ISCHR(inode->i_mode))) {
926 char type = 0;
927 int major = -1;
928 int minor = -1;
929 sscanf(mistat->extension, "%c %u %u", &type, &major, &minor);
930 switch (type) {
931 case 'c':
932 inode->i_mode &= ~S_IFBLK;
933 inode->i_mode |= S_IFCHR;
934 break;
935 case 'b':
936 break;
937 default:
938 dprintk(DEBUG_ERROR, "Unknown special type %c (%s)\n",
939 type, mistat->extension);
940 };
941 inode->i_rdev = MKDEV(major, minor);
942 } else
943 inode->i_rdev = 0;
944
945 inode->i_size = mistat->length;
946
947 inode->i_blksize = sb->s_blocksize;
948 inode->i_blocks =
949 (inode->i_size + inode->i_blksize - 1) >> sb->s_blocksize_bits;
950}
951
952/**
953 * v9fs_qid2ino - convert qid into inode number
954 * @qid: qid to hash
955 *
956 * BUG: potential for inode number collisions?
957 */
958
959ino_t v9fs_qid2ino(struct v9fs_qid *qid)
960{
961 u64 path = qid->path + 2;
962 ino_t i = 0;
963
964 if (sizeof(ino_t) == sizeof(path))
965 memcpy(&i, &path, sizeof(ino_t));
966 else
967 i = (ino_t) (path ^ (path >> 32));
968
969 return i;
970}
971
972/**
973 * v9fs_vfs_symlink - helper function to create symlinks
974 * @dir: directory inode containing symlink
975 * @dentry: dentry for symlink
976 * @symname: symlink data
977 *
978 * See 9P2000.u RFC for more information
979 *
980 */
981
982static int
983v9fs_vfs_symlink(struct inode *dir, struct dentry *dentry, const char *symname)
984{
985 int retval = -EPERM;
986 struct v9fs_fid *newfid;
987 struct v9fs_session_info *v9ses = v9fs_inode2v9ses(dir);
988 struct super_block *sb = dir ? dir->i_sb : NULL;
989 struct v9fs_fcall *fcall = NULL;
990 struct v9fs_stat *mistat = kmalloc(v9ses->maxdata, GFP_KERNEL);
991
992 dprintk(DEBUG_VFS, " %lu,%s,%s\n", dir->i_ino, dentry->d_name.name,
993 symname);
994
995 if ((!dentry) || (!sb) || (!v9ses)) {
996 dprintk(DEBUG_ERROR, "problem with arguments\n");
997 return -EBADF;
998 }
999
1000 if (!v9ses->extended) {
1001 dprintk(DEBUG_ERROR, "not extended\n");
1002 goto FreeFcall;
1003 }
1004
1005 /* issue a create */
1006 retval = v9fs_create(dir, dentry, S_IFLNK, 0);
1007 if (retval != 0)
1008 goto FreeFcall;
1009
1010 newfid = v9fs_fid_lookup(dentry, FID_OP);
1011
1012 /* issue a twstat */
1013 v9fs_blank_mistat(v9ses, mistat);
1014 strcpy(mistat->data + 1, symname);
1015 mistat->extension = mistat->data + 1;
1016 retval = v9fs_t_wstat(v9ses, newfid->fid, mistat, &fcall);
1017 if (retval < 0) {
1018 dprintk(DEBUG_ERROR, "v9fs_t_wstat error: %s\n",
1019 FCALL_ERROR(fcall));
1020 goto FreeFcall;
1021 }
1022
1023 kfree(fcall);
1024
1025 if (v9fs_t_clunk(v9ses, newfid->fid, &fcall)) {
1026 dprintk(DEBUG_ERROR, "clunk for symlink failed: %s\n",
1027 FCALL_ERROR(fcall));
1028 goto FreeFcall;
1029 }
1030
1031 d_drop(dentry); /* FID - will this also clunk? */
1032
1033 FreeFcall:
1034 kfree(mistat);
1035 kfree(fcall);
1036
1037 return retval;
1038}
1039
1040/**
1041 * v9fs_readlink - read a symlink's location (internal version)
1042 * @dentry: dentry for symlink
1043 * @buf: buffer to load symlink location into
1044 * @buflen: length of buffer
1045 *
1046 */
1047
1048static int v9fs_readlink(struct dentry *dentry, char *buffer, int buflen)
1049{
1050 int retval = -EPERM;
1051
1052 struct v9fs_fcall *fcall = NULL;
1053 struct v9fs_session_info *v9ses = v9fs_inode2v9ses(dentry->d_inode);
1054 struct v9fs_fid *fid = v9fs_fid_lookup(dentry, FID_OP);
1055
1056 if (!fid) {
1057 dprintk(DEBUG_ERROR, "could not resolve fid from dentry\n");
1058 retval = -EBADF;
1059 goto FreeFcall;
1060 }
1061
1062 if (!v9ses->extended) {
1063 retval = -EBADF;
1064 dprintk(DEBUG_ERROR, "not extended\n");
1065 goto FreeFcall;
1066 }
1067
1068 dprintk(DEBUG_VFS, " %s\n", dentry->d_name.name);
1069 retval = v9fs_t_stat(v9ses, fid->fid, &fcall);
1070
1071 if (retval < 0) {
1072 dprintk(DEBUG_ERROR, "stat error\n");
1073 goto FreeFcall;
1074 }
1075
1076 if (!fcall)
1077 return -EIO;
1078
1079 if (!(fcall->params.rstat.stat->mode & V9FS_DMSYMLINK)) {
1080 retval = -EINVAL;
1081 goto FreeFcall;
1082 }
1083
1084 /* copy extension buffer into buffer */
1085 if (strlen(fcall->params.rstat.stat->extension) < buflen)
1086 buflen = strlen(fcall->params.rstat.stat->extension);
1087
1088 memcpy(buffer, fcall->params.rstat.stat->extension, buflen + 1);
1089
1090 retval = buflen;
1091
1092 FreeFcall:
1093 kfree(fcall);
1094
1095 return retval;
1096}
1097
1098/**
1099 * v9fs_vfs_readlink - read a symlink's location
1100 * @dentry: dentry for symlink
1101 * @buf: buffer to load symlink location into
1102 * @buflen: length of buffer
1103 *
1104 */
1105
1106static int v9fs_vfs_readlink(struct dentry *dentry, char __user * buffer,
1107 int buflen)
1108{
1109 int retval;
1110 int ret;
1111 char *link = __getname();
1112
1113 if (strlen(link) < buflen)
1114 buflen = strlen(link);
1115
1116 dprintk(DEBUG_VFS, " dentry: %s (%p)\n", dentry->d_iname, dentry);
1117
1118 retval = v9fs_readlink(dentry, link, buflen);
1119
1120 if (retval > 0) {
1121 if ((ret = copy_to_user(buffer, link, retval)) != 0) {
1122 dprintk(DEBUG_ERROR, "problem copying to user: %d\n",
1123 ret);
1124 retval = ret;
1125 }
1126 }
1127
1128 putname(link);
1129 return retval;
1130}
1131
1132/**
1133 * v9fs_vfs_follow_link - follow a symlink path
1134 * @dentry: dentry for symlink
1135 * @nd: nameidata
1136 *
1137 */
1138
1139static void *v9fs_vfs_follow_link(struct dentry *dentry, struct nameidata *nd)
1140{
1141 int len = 0;
1142 char *link = __getname();
1143
1144 dprintk(DEBUG_VFS, "%s n", dentry->d_name.name);
1145
1146 if (!link)
1147 link = ERR_PTR(-ENOMEM);
1148 else {
1149 len = v9fs_readlink(dentry, link, strlen(link));
1150
1151 if (len < 0) {
1152 putname(link);
1153 link = ERR_PTR(len);
1154 } else
1155 link[len] = 0;
1156 }
1157 nd_set_link(nd, link);
1158
1159 return NULL;
1160}
1161
1162/**
1163 * v9fs_vfs_put_link - release a symlink path
1164 * @dentry: dentry for symlink
1165 * @nd: nameidata
1166 *
1167 */
1168
1169static void v9fs_vfs_put_link(struct dentry *dentry, struct nameidata *nd, void *p)
1170{
1171 char *s = nd_get_link(nd);
1172
1173 dprintk(DEBUG_VFS, " %s %s\n", dentry->d_name.name, s);
1174 if (!IS_ERR(s))
1175 putname(s);
1176}
1177
1178/**
1179 * v9fs_vfs_link - create a hardlink
1180 * @old_dentry: dentry for file to link to
1181 * @dir: inode destination for new link
1182 * @new_dentry: dentry for link
1183 *
1184 */
1185
1186/* XXX - lots of code dup'd from symlink and creates,
1187 * figure out a better reuse strategy
1188 */
1189
1190static int
1191v9fs_vfs_link(struct dentry *old_dentry, struct inode *dir,
1192 struct dentry *dentry)
1193{
1194 int retval = -EPERM;
1195 struct v9fs_session_info *v9ses = v9fs_inode2v9ses(dir);
1196 struct v9fs_fcall *fcall = NULL;
1197 struct v9fs_stat *mistat = kmalloc(v9ses->maxdata, GFP_KERNEL);
1198 struct v9fs_fid *oldfid = v9fs_fid_lookup(old_dentry, FID_OP);
1199 struct v9fs_fid *newfid = NULL;
1200 char *symname = __getname();
1201
1202 dprintk(DEBUG_VFS, " %lu,%s,%s\n", dir->i_ino, dentry->d_name.name,
1203 old_dentry->d_name.name);
1204
1205 if (!v9ses->extended) {
1206 dprintk(DEBUG_ERROR, "not extended\n");
1207 goto FreeMem;
1208 }
1209
1210 /* get fid of old_dentry */
1211 sprintf(symname, "hardlink(%d)\n", oldfid->fid);
1212
1213 /* issue a create */
1214 retval = v9fs_create(dir, dentry, V9FS_DMLINK, 0);
1215 if (retval != 0)
1216 goto FreeMem;
1217
1218 newfid = v9fs_fid_lookup(dentry, FID_OP);
1219 if (!newfid) {
1220 dprintk(DEBUG_ERROR, "couldn't resolve fid from dentry\n");
1221 goto FreeMem;
1222 }
1223
1224 /* issue a twstat */
1225 v9fs_blank_mistat(v9ses, mistat);
1226 strcpy(mistat->data + 1, symname);
1227 mistat->extension = mistat->data + 1;
1228 retval = v9fs_t_wstat(v9ses, newfid->fid, mistat, &fcall);
1229 if (retval < 0) {
1230 dprintk(DEBUG_ERROR, "v9fs_t_wstat error: %s\n",
1231 FCALL_ERROR(fcall));
1232 goto FreeMem;
1233 }
1234
1235 kfree(fcall);
1236
1237 if (v9fs_t_clunk(v9ses, newfid->fid, &fcall)) {
1238 dprintk(DEBUG_ERROR, "clunk for symlink failed: %s\n",
1239 FCALL_ERROR(fcall));
1240 goto FreeMem;
1241 }
1242
1243 d_drop(dentry); /* FID - will this also clunk? */
1244
1245 kfree(fcall);
1246 fcall = NULL;
1247
1248 FreeMem:
1249 kfree(mistat);
1250 kfree(fcall);
1251 putname(symname);
1252 return retval;
1253}
1254
1255/**
1256 * v9fs_vfs_mknod - create a special file
1257 * @dir: inode destination for new link
1258 * @dentry: dentry for file
1259 * @mode: mode for creation
1260 * @dev_t: device associated with special file
1261 *
1262 */
1263
1264static int
1265v9fs_vfs_mknod(struct inode *dir, struct dentry *dentry, int mode, dev_t rdev)
1266{
1267 int retval = -EPERM;
1268 struct v9fs_fid *newfid;
1269 struct v9fs_session_info *v9ses = v9fs_inode2v9ses(dir);
1270 struct v9fs_fcall *fcall = NULL;
1271 struct v9fs_stat *mistat = kmalloc(v9ses->maxdata, GFP_KERNEL);
1272 char *symname = __getname();
1273
1274 dprintk(DEBUG_VFS, " %lu,%s mode: %x MAJOR: %u MINOR: %u\n", dir->i_ino,
1275 dentry->d_name.name, mode, MAJOR(rdev), MINOR(rdev));
1276
1277 if (!new_valid_dev(rdev)) {
1278 retval = -EINVAL;
1279 goto FreeMem;
1280 }
1281
1282 if (!v9ses->extended) {
1283 dprintk(DEBUG_ERROR, "not extended\n");
1284 goto FreeMem;
1285 }
1286
1287 /* issue a create */
1288 retval = v9fs_create(dir, dentry, mode, 0);
1289
1290 if (retval != 0)
1291 goto FreeMem;
1292
1293 newfid = v9fs_fid_lookup(dentry, FID_OP);
1294 if (!newfid) {
1295 dprintk(DEBUG_ERROR, "coudn't resove fid from dentry\n");
1296 retval = -EINVAL;
1297 goto FreeMem;
1298 }
1299
1300 /* build extension */
1301 if (S_ISBLK(mode))
1302 sprintf(symname, "b %u %u", MAJOR(rdev), MINOR(rdev));
1303 else if (S_ISCHR(mode))
1304 sprintf(symname, "c %u %u", MAJOR(rdev), MINOR(rdev));
1305 else if (S_ISFIFO(mode)) ; /* DO NOTHING */
1306 else {
1307 retval = -EINVAL;
1308 goto FreeMem;
1309 }
1310
1311 if (!S_ISFIFO(mode)) {
1312 /* issue a twstat */
1313 v9fs_blank_mistat(v9ses, mistat);
1314 strcpy(mistat->data + 1, symname);
1315 mistat->extension = mistat->data + 1;
1316 retval = v9fs_t_wstat(v9ses, newfid->fid, mistat, &fcall);
1317 if (retval < 0) {
1318 dprintk(DEBUG_ERROR, "v9fs_t_wstat error: %s\n",
1319 FCALL_ERROR(fcall));
1320 goto FreeMem;
1321 }
1322
1323 kfree(fcall);
1324 }
1325
1326 /* need to update dcache so we show up */
1327 kfree(fcall);
1328
1329 if (v9fs_t_clunk(v9ses, newfid->fid, &fcall)) {
1330 dprintk(DEBUG_ERROR, "clunk for symlink failed: %s\n",
1331 FCALL_ERROR(fcall));
1332 goto FreeMem;
1333 }
1334
1335 d_drop(dentry); /* FID - will this also clunk? */
1336
1337 FreeMem:
1338 kfree(mistat);
1339 kfree(fcall);
1340 putname(symname);
1341
1342 return retval;
1343}
1344
1345static struct inode_operations v9fs_dir_inode_operations = {
1346 .create = v9fs_vfs_create,
1347 .lookup = v9fs_vfs_lookup,
1348 .symlink = v9fs_vfs_symlink,
1349 .link = v9fs_vfs_link,
1350 .unlink = v9fs_vfs_unlink,
1351 .mkdir = v9fs_vfs_mkdir,
1352 .rmdir = v9fs_vfs_rmdir,
1353 .mknod = v9fs_vfs_mknod,
1354 .rename = v9fs_vfs_rename,
1355 .readlink = v9fs_vfs_readlink,
1356 .getattr = v9fs_vfs_getattr,
1357 .setattr = v9fs_vfs_setattr,
1358};
1359
1360static struct inode_operations v9fs_file_inode_operations = {
1361 .getattr = v9fs_vfs_getattr,
1362 .setattr = v9fs_vfs_setattr,
1363};
1364
1365static struct inode_operations v9fs_symlink_inode_operations = {
1366 .readlink = v9fs_vfs_readlink,
1367 .follow_link = v9fs_vfs_follow_link,
1368 .put_link = v9fs_vfs_put_link,
1369 .getattr = v9fs_vfs_getattr,
1370 .setattr = v9fs_vfs_setattr,
1371};