blob: dce729d428699c7653f18cdb0b6f5ff51ee40a7a [file] [log] [blame]
Eric Van Hensbergen2bad8472005-09-09 13:04:19 -07001/*
2 * linux/fs/9p/vfs_inode.c
3 *
Eric Van Hensbergen73c592b2005-09-09 13:04:26 -07004 * This file contains vfs inode ops for the 9P2000 protocol.
Eric Van Hensbergen2bad8472005-09-09 13:04:19 -07005 *
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"
Eric Van Hensbergen2bad8472005-09-09 13:04:19 -070043#include "fid.h"
44
45static struct inode_operations v9fs_dir_inode_operations;
Eric Van Hensbergenb5016112005-09-09 13:04:27 -070046static struct inode_operations v9fs_dir_inode_operations_ext;
Eric Van Hensbergen2bad8472005-09-09 13:04:19 -070047static 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
Eric Van Hensbergen73c592b2005-09-09 13:04:26 -070057static int unixmode2p9mode(struct v9fs_session_info *v9ses, int mode)
Eric Van Hensbergen2bad8472005-09-09 13:04:19 -070058{
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
Eric Van Hensbergen73c592b2005-09-09 13:04:26 -070095static int p9mode2unixmode(struct v9fs_session_info *v9ses, int mode)
Eric Van Hensbergen2bad8472005-09-09 13:04:19 -070096{
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
Latchesar Ionkov6a3124a2006-03-02 02:54:30 -0800128int v9fs_uflags2omode(int uflags)
129{
130 int ret;
131
132 ret = 0;
133 switch (uflags&3) {
134 default:
135 case O_RDONLY:
136 ret = V9FS_OREAD;
137 break;
138
139 case O_WRONLY:
140 ret = V9FS_OWRITE;
141 break;
142
143 case O_RDWR:
144 ret = V9FS_ORDWR;
145 break;
146 }
147
148 if (uflags & O_EXCL)
149 ret |= V9FS_OEXCL;
150
151 if (uflags & O_TRUNC)
152 ret |= V9FS_OTRUNC;
153
154 if (uflags & O_APPEND)
155 ret |= V9FS_OAPPEND;
156
157 return ret;
158}
159
Eric Van Hensbergen2bad8472005-09-09 13:04:19 -0700160/**
Latchesar Ionkov531b1092006-01-08 01:05:00 -0800161 * v9fs_blank_wstat - helper function to setup a 9P stat structure
Eric Van Hensbergen2bad8472005-09-09 13:04:19 -0700162 * @v9ses: 9P session info (for determining extended mode)
Latchesar Ionkov531b1092006-01-08 01:05:00 -0800163 * @wstat: structure to initialize
Eric Van Hensbergen2bad8472005-09-09 13:04:19 -0700164 *
165 */
166
Eric Van Hensbergen73c592b2005-09-09 13:04:26 -0700167static void
Latchesar Ionkov531b1092006-01-08 01:05:00 -0800168v9fs_blank_wstat(struct v9fs_wstat *wstat)
Eric Van Hensbergen2bad8472005-09-09 13:04:19 -0700169{
Latchesar Ionkov531b1092006-01-08 01:05:00 -0800170 wstat->type = ~0;
171 wstat->dev = ~0;
172 wstat->qid.type = ~0;
173 wstat->qid.version = ~0;
174 *((long long *)&wstat->qid.path) = ~0;
175 wstat->mode = ~0;
176 wstat->atime = ~0;
177 wstat->mtime = ~0;
178 wstat->length = ~0;
179 wstat->name = NULL;
180 wstat->uid = NULL;
181 wstat->gid = NULL;
182 wstat->muid = NULL;
183 wstat->n_uid = ~0;
184 wstat->n_gid = ~0;
185 wstat->n_muid = ~0;
186 wstat->extension = NULL;
Eric Van Hensbergen2bad8472005-09-09 13:04:19 -0700187}
188
189/**
190 * v9fs_get_inode - helper function to setup an inode
191 * @sb: superblock
192 * @mode: mode to setup inode with
193 *
194 */
195
196struct inode *v9fs_get_inode(struct super_block *sb, int mode)
197{
Latchesar Ionkov6a3124a2006-03-02 02:54:30 -0800198 struct inode *inode;
Eric Van Hensbergenb5016112005-09-09 13:04:27 -0700199 struct v9fs_session_info *v9ses = sb->s_fs_info;
Eric Van Hensbergen2bad8472005-09-09 13:04:19 -0700200
201 dprintk(DEBUG_VFS, "super block: %p mode: %o\n", sb, mode);
202
203 inode = new_inode(sb);
204 if (inode) {
205 inode->i_mode = mode;
206 inode->i_uid = current->fsuid;
207 inode->i_gid = current->fsgid;
208 inode->i_blksize = sb->s_blocksize;
209 inode->i_blocks = 0;
210 inode->i_rdev = 0;
211 inode->i_atime = inode->i_mtime = inode->i_ctime = CURRENT_TIME;
Eric Van Hensbergen147b31c2006-01-18 17:43:02 -0800212 inode->i_mapping->a_ops = &v9fs_addr_operations;
Eric Van Hensbergen2bad8472005-09-09 13:04:19 -0700213
214 switch (mode & S_IFMT) {
215 case S_IFIFO:
216 case S_IFBLK:
217 case S_IFCHR:
218 case S_IFSOCK:
Eric Van Hensbergenb5016112005-09-09 13:04:27 -0700219 if(!v9ses->extended) {
220 dprintk(DEBUG_ERROR, "special files without extended mode\n");
221 return ERR_PTR(-EINVAL);
222 }
Eric Van Hensbergen5d58bec2005-09-09 13:04:27 -0700223 init_special_inode(inode, inode->i_mode,
224 inode->i_rdev);
225 break;
Eric Van Hensbergen2bad8472005-09-09 13:04:19 -0700226 case S_IFREG:
227 inode->i_op = &v9fs_file_inode_operations;
228 inode->i_fop = &v9fs_file_operations;
229 break;
Eric Van Hensbergenb5016112005-09-09 13:04:27 -0700230 case S_IFLNK:
231 if(!v9ses->extended) {
232 dprintk(DEBUG_ERROR, "extended modes used w/o 9P2000.u\n");
233 return ERR_PTR(-EINVAL);
234 }
235 inode->i_op = &v9fs_symlink_inode_operations;
236 break;
Eric Van Hensbergen2bad8472005-09-09 13:04:19 -0700237 case S_IFDIR:
238 inode->i_nlink++;
Eric Van Hensbergenb5016112005-09-09 13:04:27 -0700239 if(v9ses->extended)
240 inode->i_op = &v9fs_dir_inode_operations_ext;
241 else
242 inode->i_op = &v9fs_dir_inode_operations;
Eric Van Hensbergen2bad8472005-09-09 13:04:19 -0700243 inode->i_fop = &v9fs_dir_operations;
244 break;
Eric Van Hensbergen2bad8472005-09-09 13:04:19 -0700245 default:
246 dprintk(DEBUG_ERROR, "BAD mode 0x%x S_IFMT 0x%x\n",
247 mode, mode & S_IFMT);
248 return ERR_PTR(-EINVAL);
249 }
250 } else {
251 eprintk(KERN_WARNING, "Problem allocating inode\n");
252 return ERR_PTR(-ENOMEM);
253 }
254 return inode;
255}
256
Eric Van Hensbergen2bad8472005-09-09 13:04:19 -0700257static int
Latchesar Ionkov6a3124a2006-03-02 02:54:30 -0800258v9fs_create(struct v9fs_session_info *v9ses, u32 pfid, char *name,
259 u32 perm, u8 mode, u32 *fidp, struct v9fs_qid *qid, u32 *iounit)
Eric Van Hensbergen2bad8472005-09-09 13:04:19 -0700260{
Latchesar Ionkov6a3124a2006-03-02 02:54:30 -0800261 u32 fid;
Latchesar Ionkov3cf64292006-01-08 01:04:58 -0800262 int err;
Latchesar Ionkov6a3124a2006-03-02 02:54:30 -0800263 struct v9fs_fcall *fcall;
Eric Van Hensbergen2bad8472005-09-09 13:04:19 -0700264
Latchesar Ionkov6a3124a2006-03-02 02:54:30 -0800265 fid = v9fs_get_idpool(&v9ses->fidpool);
266 if (fid < 0) {
Eric Van Hensbergen2bad8472005-09-09 13:04:19 -0700267 eprintk(KERN_WARNING, "no free fids available\n");
Latchesar Ionkov6a3124a2006-03-02 02:54:30 -0800268 err = -ENOSPC;
269 goto error;
Eric Van Hensbergen2bad8472005-09-09 13:04:19 -0700270 }
271
Latchesar Ionkov6a3124a2006-03-02 02:54:30 -0800272 err = v9fs_t_walk(v9ses, pfid, fid, NULL, &fcall);
273 if (err < 0) {
Latchesar Ionkov531b1092006-01-08 01:05:00 -0800274 PRINT_FCALL_ERROR("clone error", fcall);
Latchesar Ionkov6a3124a2006-03-02 02:54:30 -0800275 goto error;
Eric Van Hensbergen2bad8472005-09-09 13:04:19 -0700276 }
Eric Van Hensbergen2bad8472005-09-09 13:04:19 -0700277 kfree(fcall);
278
Latchesar Ionkov6a3124a2006-03-02 02:54:30 -0800279 err = v9fs_t_create(v9ses, fid, name, perm, mode, &fcall);
280 if (err < 0) {
Latchesar Ionkov531b1092006-01-08 01:05:00 -0800281 PRINT_FCALL_ERROR("create fails", fcall);
Latchesar Ionkov6a3124a2006-03-02 02:54:30 -0800282 goto error;
Eric Van Hensbergen2bad8472005-09-09 13:04:19 -0700283 }
284
Latchesar Ionkov6a3124a2006-03-02 02:54:30 -0800285 if (iounit)
286 *iounit = fcall->params.rcreate.iounit;
287
288 if (qid)
289 *qid = fcall->params.rcreate.qid;
290
291 if (fidp)
292 *fidp = fid;
293
Eric Van Hensbergen2bad8472005-09-09 13:04:19 -0700294 kfree(fcall);
Eric Van Hensbergen2bad8472005-09-09 13:04:19 -0700295 return 0;
296
Latchesar Ionkov6a3124a2006-03-02 02:54:30 -0800297error:
298 if (fid >= 0)
299 v9fs_put_idpool(fid, &v9ses->fidpool);
300
301 kfree(fcall);
302 return err;
303}
304
305static struct v9fs_fid*
306v9fs_clone_walk(struct v9fs_session_info *v9ses, u32 fid, struct dentry *dentry)
307{
308 int err;
309 u32 nfid;
310 struct v9fs_fid *ret;
311 struct v9fs_fcall *fcall;
312
313 nfid = v9fs_get_idpool(&v9ses->fidpool);
314 if (nfid < 0) {
315 eprintk(KERN_WARNING, "no free fids available\n");
316 err = -ENOSPC;
317 goto error;
318 }
319
320 err = v9fs_t_walk(v9ses, fid, nfid, (char *) dentry->d_name.name,
321 &fcall);
322
323 if (err < 0) {
324 PRINT_FCALL_ERROR("walk error", fcall);
325 v9fs_put_idpool(nfid, &v9ses->fidpool);
326 goto error;
327 }
328
Eric Van Hensbergen2bad8472005-09-09 13:04:19 -0700329 kfree(fcall);
Latchesar Ionkov3cf64292006-01-08 01:04:58 -0800330 fcall = NULL;
Latchesar Ionkov6a3124a2006-03-02 02:54:30 -0800331 ret = v9fs_fid_create(v9ses, nfid);
332 if (!ret) {
333 err = -ENOMEM;
334 goto clunk_fid;
335 }
Eric Van Hensbergen2bad8472005-09-09 13:04:19 -0700336
Latchesar Ionkov6a3124a2006-03-02 02:54:30 -0800337 err = v9fs_fid_insert(ret, dentry);
338 if (err < 0) {
339 v9fs_fid_destroy(ret);
340 goto clunk_fid;
Latchesar Ionkov0b8dd172005-09-27 21:45:24 -0700341 }
Latchesar Ionkov6a3124a2006-03-02 02:54:30 -0800342
343 return ret;
344
345clunk_fid:
346 v9fs_t_clunk(v9ses, nfid);
347
348error:
349 kfree(fcall);
350 return ERR_PTR(err);
351}
352
353struct inode *
354v9fs_inode_from_fid(struct v9fs_session_info *v9ses, u32 fid,
355 struct super_block *sb)
356{
357 int err, umode;
358 struct inode *ret;
359 struct v9fs_fcall *fcall;
360
361 ret = NULL;
362 err = v9fs_t_stat(v9ses, fid, &fcall);
363 if (err) {
364 PRINT_FCALL_ERROR("stat error", fcall);
365 goto error;
Eric Van Hensbergen2bad8472005-09-09 13:04:19 -0700366 }
Latchesar Ionkov6a3124a2006-03-02 02:54:30 -0800367
368 umode = p9mode2unixmode(v9ses, fcall->params.rstat.stat.mode);
369 ret = v9fs_get_inode(sb, umode);
370 if (IS_ERR(ret)) {
371 err = PTR_ERR(ret);
372 ret = NULL;
373 goto error;
374 }
375
376 v9fs_stat2inode(&fcall->params.rstat.stat, ret, sb);
377 kfree(fcall);
378 return ret;
379
380error:
381 kfree(fcall);
382 if (ret)
383 iput(ret);
384
385 return ERR_PTR(err);
Eric Van Hensbergen2bad8472005-09-09 13:04:19 -0700386}
387
388/**
389 * v9fs_remove - helper function to remove files and directories
Eric Van Hensbergen73c592b2005-09-09 13:04:26 -0700390 * @dir: directory inode that is being deleted
391 * @file: dentry that is being deleted
392 * @rmdir: removing a directory
Eric Van Hensbergen2bad8472005-09-09 13:04:19 -0700393 *
394 */
395
396static int v9fs_remove(struct inode *dir, struct dentry *file, int rmdir)
397{
398 struct v9fs_fcall *fcall = NULL;
399 struct super_block *sb = NULL;
400 struct v9fs_session_info *v9ses = NULL;
401 struct v9fs_fid *v9fid = NULL;
402 struct inode *file_inode = NULL;
403 int fid = -1;
404 int result = 0;
405
406 dprintk(DEBUG_VFS, "inode: %p dentry: %p rmdir: %d\n", dir, file,
407 rmdir);
408
409 file_inode = file->d_inode;
410 sb = file_inode->i_sb;
411 v9ses = v9fs_inode2v9ses(file_inode);
Latchesar Ionkov0b8dd172005-09-27 21:45:24 -0700412 v9fid = v9fs_fid_lookup(file);
Eric Van Hensbergen2bad8472005-09-09 13:04:19 -0700413
414 if (!v9fid) {
415 dprintk(DEBUG_ERROR,
416 "no v9fs_fid\n");
417 return -EBADF;
418 }
419
420 fid = v9fid->fid;
421 if (fid < 0) {
422 dprintk(DEBUG_ERROR, "inode #%lu, no fid!\n",
423 file_inode->i_ino);
424 return -EBADF;
425 }
426
427 result = v9fs_t_remove(v9ses, fid, &fcall);
Latchesar Ionkov531b1092006-01-08 01:05:00 -0800428 if (result < 0) {
429 PRINT_FCALL_ERROR("remove fails", fcall);
430 } else {
Eric Van Hensbergen2bad8472005-09-09 13:04:19 -0700431 v9fs_put_idpool(fid, &v9ses->fidpool);
432 v9fs_fid_destroy(v9fid);
433 }
434
435 kfree(fcall);
436 return result;
437}
438
Latchesar Ionkov6a3124a2006-03-02 02:54:30 -0800439static int
440v9fs_open_created(struct inode *inode, struct file *file)
441{
442 return 0;
443}
444
Eric Van Hensbergen2bad8472005-09-09 13:04:19 -0700445/**
446 * v9fs_vfs_create - VFS hook to create files
447 * @inode: directory inode that is being deleted
448 * @dentry: dentry that is being deleted
Latchesar Ionkov6a3124a2006-03-02 02:54:30 -0800449 * @mode: create permissions
Eric Van Hensbergen2bad8472005-09-09 13:04:19 -0700450 * @nd: path information
451 *
452 */
453
454static int
Latchesar Ionkov6a3124a2006-03-02 02:54:30 -0800455v9fs_vfs_create(struct inode *dir, struct dentry *dentry, int mode,
Eric Van Hensbergen2bad8472005-09-09 13:04:19 -0700456 struct nameidata *nd)
457{
Latchesar Ionkov6a3124a2006-03-02 02:54:30 -0800458 int err;
459 u32 fid, perm, iounit;
460 int flags;
461 struct v9fs_session_info *v9ses;
462 struct v9fs_fid *dfid, *vfid, *ffid;
463 struct inode *inode;
464 struct v9fs_qid qid;
465 struct file *filp;
466
467 inode = NULL;
468 vfid = NULL;
469 v9ses = v9fs_inode2v9ses(dir);
470 dfid = v9fs_fid_lookup(dentry->d_parent);
471 perm = unixmode2p9mode(v9ses, mode);
472
473 if (nd && nd->flags & LOOKUP_OPEN)
474 flags = nd->intent.open.flags - 1;
475 else
476 flags = O_RDWR;
477
478 err = v9fs_create(v9ses, dfid->fid, (char *) dentry->d_name.name,
479 perm, v9fs_uflags2omode(flags), &fid, &qid, &iounit);
480
481 if (err)
482 goto error;
483
484 vfid = v9fs_clone_walk(v9ses, dfid->fid, dentry);
485 if (IS_ERR(vfid)) {
486 err = PTR_ERR(vfid);
487 vfid = NULL;
488 goto error;
489 }
490
491 inode = v9fs_inode_from_fid(v9ses, vfid->fid, dir->i_sb);
492 if (IS_ERR(inode)) {
493 err = PTR_ERR(inode);
494 inode = NULL;
495 goto error;
496 }
497
498 dentry->d_op = &v9fs_dentry_operations;
499 d_instantiate(dentry, inode);
500
501 if (nd && nd->flags & LOOKUP_OPEN) {
502 ffid = v9fs_fid_create(v9ses, fid);
503 if (!ffid)
504 return -ENOMEM;
505
506 filp = lookup_instantiate_filp(nd, dentry, v9fs_open_created);
507 if (IS_ERR(filp)) {
508 v9fs_fid_destroy(ffid);
509 return PTR_ERR(filp);
510 }
511
512 ffid->rdir_pos = 0;
513 ffid->rdir_fcall = NULL;
514 ffid->fidopen = 1;
515 ffid->iounit = iounit;
516 ffid->filp = filp;
517 filp->private_data = ffid;
518 }
519
520 return 0;
521
522error:
523 if (vfid)
524 v9fs_fid_destroy(vfid);
525
526 if (inode)
527 iput(inode);
528
529 return err;
Eric Van Hensbergen2bad8472005-09-09 13:04:19 -0700530}
531
532/**
533 * v9fs_vfs_mkdir - VFS mkdir hook to create a directory
Eric Van Hensbergen73c592b2005-09-09 13:04:26 -0700534 * @inode: inode that is being unlinked
Eric Van Hensbergen2bad8472005-09-09 13:04:19 -0700535 * @dentry: dentry that is being unlinked
536 * @mode: mode for new directory
537 *
538 */
539
Latchesar Ionkov6a3124a2006-03-02 02:54:30 -0800540static int v9fs_vfs_mkdir(struct inode *dir, struct dentry *dentry, int mode)
Eric Van Hensbergen2bad8472005-09-09 13:04:19 -0700541{
Latchesar Ionkov6a3124a2006-03-02 02:54:30 -0800542 int err;
543 u32 fid, perm;
544 struct v9fs_session_info *v9ses;
545 struct v9fs_fid *dfid, *vfid;
546 struct inode *inode;
547
548 inode = NULL;
549 vfid = NULL;
550 v9ses = v9fs_inode2v9ses(dir);
551 dfid = v9fs_fid_lookup(dentry->d_parent);
552 perm = unixmode2p9mode(v9ses, mode | S_IFDIR);
553
554 err = v9fs_create(v9ses, dfid->fid, (char *) dentry->d_name.name,
555 perm, V9FS_OREAD, &fid, NULL, NULL);
556
557 if (err) {
558 dprintk(DEBUG_ERROR, "create error %d\n", err);
559 goto error;
560 }
561
562 err = v9fs_t_clunk(v9ses, fid);
563 if (err) {
564 dprintk(DEBUG_ERROR, "clunk error %d\n", err);
565 goto error;
566 }
567
568 vfid = v9fs_clone_walk(v9ses, dfid->fid, dentry);
569 if (IS_ERR(vfid)) {
570 err = PTR_ERR(vfid);
571 vfid = NULL;
572 goto error;
573 }
574
575 inode = v9fs_inode_from_fid(v9ses, vfid->fid, dir->i_sb);
576 if (IS_ERR(inode)) {
577 err = PTR_ERR(inode);
578 inode = NULL;
579 goto error;
580 }
581
582 dentry->d_op = &v9fs_dentry_operations;
583 d_instantiate(dentry, inode);
584 return 0;
585
586error:
587 if (vfid)
588 v9fs_fid_destroy(vfid);
589
590 return err;
Eric Van Hensbergen2bad8472005-09-09 13:04:19 -0700591}
592
593/**
594 * v9fs_vfs_lookup - VFS lookup hook to "walk" to a new inode
595 * @dir: inode that is being walked from
596 * @dentry: dentry that is being walked to?
597 * @nameidata: path data
598 *
599 */
600
601static struct dentry *v9fs_vfs_lookup(struct inode *dir, struct dentry *dentry,
602 struct nameidata *nameidata)
603{
604 struct super_block *sb;
605 struct v9fs_session_info *v9ses;
606 struct v9fs_fid *dirfid;
607 struct v9fs_fid *fid;
608 struct inode *inode;
609 struct v9fs_fcall *fcall = NULL;
Eric Van Hensbergen2bad8472005-09-09 13:04:19 -0700610 int dirfidnum = -1;
611 int newfid = -1;
612 int result = 0;
613
614 dprintk(DEBUG_VFS, "dir: %p dentry: (%s) %p nameidata: %p\n",
615 dir, dentry->d_iname, dentry, nameidata);
616
617 sb = dir->i_sb;
618 v9ses = v9fs_inode2v9ses(dir);
Latchesar Ionkov0b8dd172005-09-27 21:45:24 -0700619 dirfid = v9fs_fid_lookup(dentry->d_parent);
Eric Van Hensbergen2bad8472005-09-09 13:04:19 -0700620
621 if (!dirfid) {
622 dprintk(DEBUG_ERROR, "no dirfid\n");
623 return ERR_PTR(-EINVAL);
624 }
625
626 dirfidnum = dirfid->fid;
627
628 if (dirfidnum < 0) {
629 dprintk(DEBUG_ERROR, "no dirfid for inode %p, #%lu\n",
630 dir, dir->i_ino);
631 return ERR_PTR(-EBADF);
632 }
633
634 newfid = v9fs_get_idpool(&v9ses->fidpool);
635 if (newfid < 0) {
636 eprintk(KERN_WARNING, "newfid fails!\n");
637 return ERR_PTR(-ENOSPC);
638 }
639
Latchesar Ionkov6a3124a2006-03-02 02:54:30 -0800640 result = v9fs_t_walk(v9ses, dirfidnum, newfid,
641 (char *)dentry->d_name.name, NULL);
Eric Van Hensbergen2bad8472005-09-09 13:04:19 -0700642 if (result < 0) {
643 v9fs_put_idpool(newfid, &v9ses->fidpool);
644 if (result == -ENOENT) {
645 d_add(dentry, NULL);
Latchesar Ionkov0b8dd172005-09-27 21:45:24 -0700646 dprintk(DEBUG_VFS,
Eric Van Hensbergen2bad8472005-09-09 13:04:19 -0700647 "Return negative dentry %p count %d\n",
648 dentry, atomic_read(&dentry->d_count));
649 return NULL;
650 }
651 dprintk(DEBUG_ERROR, "walk error:%d\n", result);
652 goto FreeFcall;
653 }
654
655 result = v9fs_t_stat(v9ses, newfid, &fcall);
656 if (result < 0) {
657 dprintk(DEBUG_ERROR, "stat error\n");
658 goto FreeFcall;
659 }
660
Latchesar Ionkov531b1092006-01-08 01:05:00 -0800661 inode = v9fs_get_inode(sb, p9mode2unixmode(v9ses,
662 fcall->params.rstat.stat.mode));
Eric Van Hensbergen2bad8472005-09-09 13:04:19 -0700663
664 if (IS_ERR(inode) && (PTR_ERR(inode) == -ENOSPC)) {
665 eprintk(KERN_WARNING, "inode alloc failes, returns %ld\n",
666 PTR_ERR(inode));
667
668 result = -ENOSPC;
669 goto FreeFcall;
670 }
671
Latchesar Ionkov531b1092006-01-08 01:05:00 -0800672 inode->i_ino = v9fs_qid2ino(&fcall->params.rstat.stat.qid);
Eric Van Hensbergen2bad8472005-09-09 13:04:19 -0700673
Latchesar Ionkov6a3124a2006-03-02 02:54:30 -0800674 fid = v9fs_fid_create(v9ses, newfid);
Eric Van Hensbergen2bad8472005-09-09 13:04:19 -0700675 if (fid == NULL) {
676 dprintk(DEBUG_ERROR, "couldn't insert\n");
677 result = -ENOMEM;
678 goto FreeFcall;
679 }
680
Latchesar Ionkov6a3124a2006-03-02 02:54:30 -0800681 result = v9fs_fid_insert(fid, dentry);
682 if (result < 0)
683 goto FreeFcall;
684
Latchesar Ionkov531b1092006-01-08 01:05:00 -0800685 fid->qid = fcall->params.rstat.stat.qid;
Eric Van Hensbergen2bad8472005-09-09 13:04:19 -0700686
687 dentry->d_op = &v9fs_dentry_operations;
Latchesar Ionkov531b1092006-01-08 01:05:00 -0800688 v9fs_stat2inode(&fcall->params.rstat.stat, inode, inode->i_sb);
Eric Van Hensbergen2bad8472005-09-09 13:04:19 -0700689
690 d_add(dentry, inode);
691 kfree(fcall);
692
693 return NULL;
694
695 FreeFcall:
696 kfree(fcall);
697 return ERR_PTR(result);
698}
699
700/**
701 * v9fs_vfs_unlink - VFS unlink hook to delete an inode
702 * @i: inode that is being unlinked
Eric Van Hensbergen73c592b2005-09-09 13:04:26 -0700703 * @d: dentry that is being unlinked
Eric Van Hensbergen2bad8472005-09-09 13:04:19 -0700704 *
705 */
706
707static int v9fs_vfs_unlink(struct inode *i, struct dentry *d)
708{
709 return v9fs_remove(i, d, 0);
710}
711
712/**
713 * v9fs_vfs_rmdir - VFS unlink hook to delete a directory
714 * @i: inode that is being unlinked
Eric Van Hensbergen73c592b2005-09-09 13:04:26 -0700715 * @d: dentry that is being unlinked
Eric Van Hensbergen2bad8472005-09-09 13:04:19 -0700716 *
717 */
718
719static int v9fs_vfs_rmdir(struct inode *i, struct dentry *d)
720{
721 return v9fs_remove(i, d, 1);
722}
723
724/**
725 * v9fs_vfs_rename - VFS hook to rename an inode
726 * @old_dir: old dir inode
727 * @old_dentry: old dentry
728 * @new_dir: new dir inode
729 * @new_dentry: new dentry
730 *
731 */
732
733static int
734v9fs_vfs_rename(struct inode *old_dir, struct dentry *old_dentry,
735 struct inode *new_dir, struct dentry *new_dentry)
736{
737 struct inode *old_inode = old_dentry->d_inode;
738 struct v9fs_session_info *v9ses = v9fs_inode2v9ses(old_inode);
Latchesar Ionkov0b8dd172005-09-27 21:45:24 -0700739 struct v9fs_fid *oldfid = v9fs_fid_lookup(old_dentry);
Eric Van Hensbergen2bad8472005-09-09 13:04:19 -0700740 struct v9fs_fid *olddirfid =
Latchesar Ionkov0b8dd172005-09-27 21:45:24 -0700741 v9fs_fid_lookup(old_dentry->d_parent);
Eric Van Hensbergen2bad8472005-09-09 13:04:19 -0700742 struct v9fs_fid *newdirfid =
Latchesar Ionkov0b8dd172005-09-27 21:45:24 -0700743 v9fs_fid_lookup(new_dentry->d_parent);
Latchesar Ionkov531b1092006-01-08 01:05:00 -0800744 struct v9fs_wstat wstat;
Eric Van Hensbergen2bad8472005-09-09 13:04:19 -0700745 struct v9fs_fcall *fcall = NULL;
746 int fid = -1;
747 int olddirfidnum = -1;
748 int newdirfidnum = -1;
749 int retval = 0;
750
751 dprintk(DEBUG_VFS, "\n");
752
753 if ((!oldfid) || (!olddirfid) || (!newdirfid)) {
754 dprintk(DEBUG_ERROR, "problem with arguments\n");
755 return -EBADF;
756 }
757
758 /* 9P can only handle file rename in the same directory */
759 if (memcmp(&olddirfid->qid, &newdirfid->qid, sizeof(newdirfid->qid))) {
760 dprintk(DEBUG_ERROR, "old dir and new dir are different\n");
761 retval = -EPERM;
762 goto FreeFcallnBail;
763 }
764
765 fid = oldfid->fid;
766 olddirfidnum = olddirfid->fid;
767 newdirfidnum = newdirfid->fid;
768
769 if (fid < 0) {
770 dprintk(DEBUG_ERROR, "no fid for old file #%lu\n",
771 old_inode->i_ino);
772 retval = -EBADF;
773 goto FreeFcallnBail;
774 }
775
Latchesar Ionkov531b1092006-01-08 01:05:00 -0800776 v9fs_blank_wstat(&wstat);
777 wstat.muid = v9ses->name;
778 wstat.name = (char *) new_dentry->d_name.name;
Eric Van Hensbergen2bad8472005-09-09 13:04:19 -0700779
Latchesar Ionkov531b1092006-01-08 01:05:00 -0800780 retval = v9fs_t_wstat(v9ses, fid, &wstat, &fcall);
Eric Van Hensbergen2bad8472005-09-09 13:04:19 -0700781
782 FreeFcallnBail:
Eric Van Hensbergen2bad8472005-09-09 13:04:19 -0700783 if (retval < 0)
Latchesar Ionkov531b1092006-01-08 01:05:00 -0800784 PRINT_FCALL_ERROR("wstat error", fcall);
Eric Van Hensbergen2bad8472005-09-09 13:04:19 -0700785
786 kfree(fcall);
787 return retval;
788}
789
790/**
Adrian Bunk943ffb52006-01-10 00:10:13 +0100791 * v9fs_vfs_getattr - retrieve file metadata
Eric Van Hensbergen2bad8472005-09-09 13:04:19 -0700792 * @mnt - mount information
793 * @dentry - file to get attributes on
794 * @stat - metadata structure to populate
795 *
796 */
797
798static int
799v9fs_vfs_getattr(struct vfsmount *mnt, struct dentry *dentry,
800 struct kstat *stat)
801{
802 struct v9fs_fcall *fcall = NULL;
803 struct v9fs_session_info *v9ses = v9fs_inode2v9ses(dentry->d_inode);
Latchesar Ionkov0b8dd172005-09-27 21:45:24 -0700804 struct v9fs_fid *fid = v9fs_fid_lookup(dentry);
Eric Van Hensbergen2bad8472005-09-09 13:04:19 -0700805 int err = -EPERM;
806
807 dprintk(DEBUG_VFS, "dentry: %p\n", dentry);
808 if (!fid) {
809 dprintk(DEBUG_ERROR,
810 "couldn't find fid associated with dentry\n");
811 return -EBADF;
812 }
813
814 err = v9fs_t_stat(v9ses, fid->fid, &fcall);
815
816 if (err < 0)
817 dprintk(DEBUG_ERROR, "stat error\n");
818 else {
Latchesar Ionkov531b1092006-01-08 01:05:00 -0800819 v9fs_stat2inode(&fcall->params.rstat.stat, dentry->d_inode,
Eric Van Hensbergen2bad8472005-09-09 13:04:19 -0700820 dentry->d_inode->i_sb);
821 generic_fillattr(dentry->d_inode, stat);
822 }
823
824 kfree(fcall);
825 return err;
826}
827
828/**
829 * v9fs_vfs_setattr - set file metadata
830 * @dentry: file whose metadata to set
831 * @iattr: metadata assignment structure
832 *
833 */
834
835static int v9fs_vfs_setattr(struct dentry *dentry, struct iattr *iattr)
836{
837 struct v9fs_session_info *v9ses = v9fs_inode2v9ses(dentry->d_inode);
Latchesar Ionkov0b8dd172005-09-27 21:45:24 -0700838 struct v9fs_fid *fid = v9fs_fid_lookup(dentry);
Eric Van Hensbergen2bad8472005-09-09 13:04:19 -0700839 struct v9fs_fcall *fcall = NULL;
Latchesar Ionkov531b1092006-01-08 01:05:00 -0800840 struct v9fs_wstat wstat;
Eric Van Hensbergen2bad8472005-09-09 13:04:19 -0700841 int res = -EPERM;
842
843 dprintk(DEBUG_VFS, "\n");
Eric Van Hensbergen73c592b2005-09-09 13:04:26 -0700844
Eric Van Hensbergen2bad8472005-09-09 13:04:19 -0700845 if (!fid) {
846 dprintk(DEBUG_ERROR,
847 "Couldn't find fid associated with dentry\n");
848 return -EBADF;
849 }
850
Latchesar Ionkov531b1092006-01-08 01:05:00 -0800851 v9fs_blank_wstat(&wstat);
Eric Van Hensbergen2bad8472005-09-09 13:04:19 -0700852 if (iattr->ia_valid & ATTR_MODE)
Latchesar Ionkov531b1092006-01-08 01:05:00 -0800853 wstat.mode = unixmode2p9mode(v9ses, iattr->ia_mode);
Eric Van Hensbergen2bad8472005-09-09 13:04:19 -0700854
855 if (iattr->ia_valid & ATTR_MTIME)
Latchesar Ionkov531b1092006-01-08 01:05:00 -0800856 wstat.mtime = iattr->ia_mtime.tv_sec;
Eric Van Hensbergen2bad8472005-09-09 13:04:19 -0700857
858 if (iattr->ia_valid & ATTR_ATIME)
Latchesar Ionkov531b1092006-01-08 01:05:00 -0800859 wstat.atime = iattr->ia_atime.tv_sec;
Eric Van Hensbergen2bad8472005-09-09 13:04:19 -0700860
861 if (iattr->ia_valid & ATTR_SIZE)
Latchesar Ionkov531b1092006-01-08 01:05:00 -0800862 wstat.length = iattr->ia_size;
Eric Van Hensbergen2bad8472005-09-09 13:04:19 -0700863
864 if (v9ses->extended) {
Latchesar Ionkov531b1092006-01-08 01:05:00 -0800865 if (iattr->ia_valid & ATTR_UID)
866 wstat.n_uid = iattr->ia_uid;
Eric Van Hensbergen2bad8472005-09-09 13:04:19 -0700867
Latchesar Ionkov531b1092006-01-08 01:05:00 -0800868 if (iattr->ia_valid & ATTR_GID)
869 wstat.n_gid = iattr->ia_gid;
Eric Van Hensbergen2bad8472005-09-09 13:04:19 -0700870 }
871
Latchesar Ionkov531b1092006-01-08 01:05:00 -0800872 res = v9fs_t_wstat(v9ses, fid->fid, &wstat, &fcall);
Eric Van Hensbergen2bad8472005-09-09 13:04:19 -0700873
874 if (res < 0)
Latchesar Ionkov531b1092006-01-08 01:05:00 -0800875 PRINT_FCALL_ERROR("wstat error", fcall);
Eric Van Hensbergen2bad8472005-09-09 13:04:19 -0700876
Eric Van Hensbergen2bad8472005-09-09 13:04:19 -0700877 kfree(fcall);
Eric Van Hensbergen2bad8472005-09-09 13:04:19 -0700878 if (res >= 0)
879 res = inode_setattr(dentry->d_inode, iattr);
880
881 return res;
882}
883
884/**
Latchesar Ionkov531b1092006-01-08 01:05:00 -0800885 * v9fs_stat2inode - populate an inode structure with mistat info
886 * @stat: Plan 9 metadata (mistat) structure
Eric Van Hensbergen2bad8472005-09-09 13:04:19 -0700887 * @inode: inode to populate
888 * @sb: superblock of filesystem
889 *
890 */
891
892void
Latchesar Ionkov531b1092006-01-08 01:05:00 -0800893v9fs_stat2inode(struct v9fs_stat *stat, struct inode *inode,
894 struct super_block *sb)
Eric Van Hensbergen2bad8472005-09-09 13:04:19 -0700895{
Latchesar Ionkov1dac06b2006-01-08 01:05:02 -0800896 int n;
Latchesar Ionkov531b1092006-01-08 01:05:00 -0800897 char ext[32];
Eric Van Hensbergen2bad8472005-09-09 13:04:19 -0700898 struct v9fs_session_info *v9ses = sb->s_fs_info;
899
900 inode->i_nlink = 1;
901
Latchesar Ionkov531b1092006-01-08 01:05:00 -0800902 inode->i_atime.tv_sec = stat->atime;
903 inode->i_mtime.tv_sec = stat->mtime;
904 inode->i_ctime.tv_sec = stat->mtime;
Eric Van Hensbergen2bad8472005-09-09 13:04:19 -0700905
Latchesar Ionkov531b1092006-01-08 01:05:00 -0800906 inode->i_uid = v9ses->uid;
907 inode->i_gid = v9ses->gid;
Eric Van Hensbergen2bad8472005-09-09 13:04:19 -0700908
909 if (v9ses->extended) {
Latchesar Ionkov531b1092006-01-08 01:05:00 -0800910 inode->i_uid = stat->n_uid;
911 inode->i_gid = stat->n_gid;
Eric Van Hensbergen2bad8472005-09-09 13:04:19 -0700912 }
913
Latchesar Ionkov531b1092006-01-08 01:05:00 -0800914 inode->i_mode = p9mode2unixmode(v9ses, stat->mode);
Eric Van Hensbergen2bad8472005-09-09 13:04:19 -0700915 if ((S_ISBLK(inode->i_mode)) || (S_ISCHR(inode->i_mode))) {
916 char type = 0;
917 int major = -1;
918 int minor = -1;
Latchesar Ionkov531b1092006-01-08 01:05:00 -0800919
Latchesar Ionkov1dac06b2006-01-08 01:05:02 -0800920 n = stat->extension.len;
921 if (n > sizeof(ext)-1)
922 n = sizeof(ext)-1;
923 memmove(ext, stat->extension.str, n);
924 ext[n] = 0;
Latchesar Ionkov531b1092006-01-08 01:05:00 -0800925 sscanf(ext, "%c %u %u", &type, &major, &minor);
Eric Van Hensbergen2bad8472005-09-09 13:04:19 -0700926 switch (type) {
927 case 'c':
928 inode->i_mode &= ~S_IFBLK;
929 inode->i_mode |= S_IFCHR;
930 break;
931 case 'b':
932 break;
933 default:
Latchesar Ionkov531b1092006-01-08 01:05:00 -0800934 dprintk(DEBUG_ERROR, "Unknown special type %c (%.*s)\n",
935 type, stat->extension.len, stat->extension.str);
Eric Van Hensbergen2bad8472005-09-09 13:04:19 -0700936 };
937 inode->i_rdev = MKDEV(major, minor);
938 } else
939 inode->i_rdev = 0;
940
Latchesar Ionkov531b1092006-01-08 01:05:00 -0800941 inode->i_size = stat->length;
Eric Van Hensbergen2bad8472005-09-09 13:04:19 -0700942
943 inode->i_blksize = sb->s_blocksize;
944 inode->i_blocks =
945 (inode->i_size + inode->i_blksize - 1) >> sb->s_blocksize_bits;
946}
947
948/**
949 * v9fs_qid2ino - convert qid into inode number
950 * @qid: qid to hash
951 *
952 * BUG: potential for inode number collisions?
953 */
954
955ino_t v9fs_qid2ino(struct v9fs_qid *qid)
956{
957 u64 path = qid->path + 2;
958 ino_t i = 0;
959
960 if (sizeof(ino_t) == sizeof(path))
961 memcpy(&i, &path, sizeof(ino_t));
962 else
963 i = (ino_t) (path ^ (path >> 32));
964
965 return i;
966}
967
968/**
Eric Van Hensbergen2bad8472005-09-09 13:04:19 -0700969 * v9fs_readlink - read a symlink's location (internal version)
970 * @dentry: dentry for symlink
Eric Van Hensbergen73c592b2005-09-09 13:04:26 -0700971 * @buffer: buffer to load symlink location into
Eric Van Hensbergen2bad8472005-09-09 13:04:19 -0700972 * @buflen: length of buffer
973 *
974 */
975
976static int v9fs_readlink(struct dentry *dentry, char *buffer, int buflen)
977{
978 int retval = -EPERM;
979
980 struct v9fs_fcall *fcall = NULL;
981 struct v9fs_session_info *v9ses = v9fs_inode2v9ses(dentry->d_inode);
Latchesar Ionkov0b8dd172005-09-27 21:45:24 -0700982 struct v9fs_fid *fid = v9fs_fid_lookup(dentry);
Eric Van Hensbergen2bad8472005-09-09 13:04:19 -0700983
984 if (!fid) {
985 dprintk(DEBUG_ERROR, "could not resolve fid from dentry\n");
986 retval = -EBADF;
987 goto FreeFcall;
988 }
989
990 if (!v9ses->extended) {
991 retval = -EBADF;
992 dprintk(DEBUG_ERROR, "not extended\n");
993 goto FreeFcall;
994 }
995
996 dprintk(DEBUG_VFS, " %s\n", dentry->d_name.name);
997 retval = v9fs_t_stat(v9ses, fid->fid, &fcall);
998
999 if (retval < 0) {
1000 dprintk(DEBUG_ERROR, "stat error\n");
1001 goto FreeFcall;
1002 }
1003
1004 if (!fcall)
1005 return -EIO;
1006
Latchesar Ionkov531b1092006-01-08 01:05:00 -08001007 if (!(fcall->params.rstat.stat.mode & V9FS_DMSYMLINK)) {
Eric Van Hensbergen2bad8472005-09-09 13:04:19 -07001008 retval = -EINVAL;
1009 goto FreeFcall;
1010 }
1011
1012 /* copy extension buffer into buffer */
Latchesar Ionkov6a3124a2006-03-02 02:54:30 -08001013 if (fcall->params.rstat.stat.extension.len < buflen)
1014 buflen = fcall->params.rstat.stat.extension.len;
Eric Van Hensbergen2bad8472005-09-09 13:04:19 -07001015
Latchesar Ionkov531b1092006-01-08 01:05:00 -08001016 memcpy(buffer, fcall->params.rstat.stat.extension.str, buflen - 1);
1017 buffer[buflen-1] = 0;
Eric Van Hensbergen2bad8472005-09-09 13:04:19 -07001018
1019 retval = buflen;
1020
1021 FreeFcall:
1022 kfree(fcall);
1023
1024 return retval;
1025}
1026
1027/**
1028 * v9fs_vfs_readlink - read a symlink's location
1029 * @dentry: dentry for symlink
1030 * @buf: buffer to load symlink location into
1031 * @buflen: length of buffer
1032 *
1033 */
1034
1035static int v9fs_vfs_readlink(struct dentry *dentry, char __user * buffer,
1036 int buflen)
1037{
1038 int retval;
1039 int ret;
1040 char *link = __getname();
1041
Latchesar Ionkova1f9d8d2005-09-22 21:43:52 -07001042 if (buflen > PATH_MAX)
1043 buflen = PATH_MAX;
Eric Van Hensbergen2bad8472005-09-09 13:04:19 -07001044
1045 dprintk(DEBUG_VFS, " dentry: %s (%p)\n", dentry->d_iname, dentry);
1046
1047 retval = v9fs_readlink(dentry, link, buflen);
1048
1049 if (retval > 0) {
1050 if ((ret = copy_to_user(buffer, link, retval)) != 0) {
1051 dprintk(DEBUG_ERROR, "problem copying to user: %d\n",
1052 ret);
1053 retval = ret;
1054 }
1055 }
1056
Davi Arnautce44eeb2005-11-07 00:59:36 -08001057 __putname(link);
Eric Van Hensbergen2bad8472005-09-09 13:04:19 -07001058 return retval;
1059}
1060
1061/**
1062 * v9fs_vfs_follow_link - follow a symlink path
1063 * @dentry: dentry for symlink
1064 * @nd: nameidata
1065 *
1066 */
1067
1068static void *v9fs_vfs_follow_link(struct dentry *dentry, struct nameidata *nd)
1069{
1070 int len = 0;
1071 char *link = __getname();
1072
1073 dprintk(DEBUG_VFS, "%s n", dentry->d_name.name);
1074
1075 if (!link)
1076 link = ERR_PTR(-ENOMEM);
1077 else {
Latchesar Ionkov6a3124a2006-03-02 02:54:30 -08001078 len = v9fs_readlink(dentry, link, strlen(link));
Eric Van Hensbergen2bad8472005-09-09 13:04:19 -07001079
1080 if (len < 0) {
Davi Arnautce44eeb2005-11-07 00:59:36 -08001081 __putname(link);
Eric Van Hensbergen2bad8472005-09-09 13:04:19 -07001082 link = ERR_PTR(len);
1083 } else
1084 link[len] = 0;
1085 }
1086 nd_set_link(nd, link);
1087
1088 return NULL;
1089}
1090
1091/**
1092 * v9fs_vfs_put_link - release a symlink path
1093 * @dentry: dentry for symlink
1094 * @nd: nameidata
1095 *
1096 */
1097
1098static void v9fs_vfs_put_link(struct dentry *dentry, struct nameidata *nd, void *p)
1099{
1100 char *s = nd_get_link(nd);
1101
1102 dprintk(DEBUG_VFS, " %s %s\n", dentry->d_name.name, s);
1103 if (!IS_ERR(s))
Davi Arnautce44eeb2005-11-07 00:59:36 -08001104 __putname(s);
Eric Van Hensbergen2bad8472005-09-09 13:04:19 -07001105}
1106
Latchesar Ionkov531b1092006-01-08 01:05:00 -08001107static int v9fs_vfs_mkspecial(struct inode *dir, struct dentry *dentry,
1108 int mode, const char *extension)
1109{
Latchesar Ionkov6a3124a2006-03-02 02:54:30 -08001110 int err;
1111 u32 fid, perm;
Latchesar Ionkov531b1092006-01-08 01:05:00 -08001112 struct v9fs_session_info *v9ses;
Latchesar Ionkov6a3124a2006-03-02 02:54:30 -08001113 struct v9fs_fid *dfid, *vfid;
1114 struct inode *inode;
Latchesar Ionkov531b1092006-01-08 01:05:00 -08001115 struct v9fs_fcall *fcall;
Latchesar Ionkov531b1092006-01-08 01:05:00 -08001116 struct v9fs_wstat wstat;
1117
Latchesar Ionkov531b1092006-01-08 01:05:00 -08001118 fcall = NULL;
Latchesar Ionkov6a3124a2006-03-02 02:54:30 -08001119 inode = NULL;
1120 vfid = NULL;
1121 v9ses = v9fs_inode2v9ses(dir);
1122 dfid = v9fs_fid_lookup(dentry->d_parent);
1123 perm = unixmode2p9mode(v9ses, mode);
Latchesar Ionkov531b1092006-01-08 01:05:00 -08001124
1125 if (!v9ses->extended) {
1126 dprintk(DEBUG_ERROR, "not extended\n");
Latchesar Ionkov6a3124a2006-03-02 02:54:30 -08001127 return -EPERM;
Latchesar Ionkov531b1092006-01-08 01:05:00 -08001128 }
1129
Latchesar Ionkov6a3124a2006-03-02 02:54:30 -08001130 err = v9fs_create(v9ses, dfid->fid, (char *) dentry->d_name.name,
1131 perm, V9FS_OREAD, &fid, NULL, NULL);
Latchesar Ionkov531b1092006-01-08 01:05:00 -08001132
Latchesar Ionkov6a3124a2006-03-02 02:54:30 -08001133 if (err)
1134 goto error;
1135
1136 err = v9fs_t_clunk(v9ses, fid);
1137 if (err)
1138 goto error;
1139
1140 vfid = v9fs_clone_walk(v9ses, dfid->fid, dentry);
1141 if (IS_ERR(vfid)) {
1142 err = PTR_ERR(vfid);
1143 vfid = NULL;
1144 goto error;
1145 }
1146
1147 inode = v9fs_inode_from_fid(v9ses, vfid->fid, dir->i_sb);
1148 if (IS_ERR(inode)) {
1149 err = PTR_ERR(inode);
1150 inode = NULL;
1151 goto error;
Latchesar Ionkov531b1092006-01-08 01:05:00 -08001152 }
1153
1154 /* issue a Twstat */
1155 v9fs_blank_wstat(&wstat);
1156 wstat.muid = v9ses->name;
1157 wstat.extension = (char *) extension;
Latchesar Ionkov6a3124a2006-03-02 02:54:30 -08001158 err = v9fs_t_wstat(v9ses, vfid->fid, &wstat, &fcall);
Latchesar Ionkov531b1092006-01-08 01:05:00 -08001159 if (err < 0) {
Latchesar Ionkov6a3124a2006-03-02 02:54:30 -08001160 PRINT_FCALL_ERROR("wstat error", fcall);
1161 goto error;
Latchesar Ionkov531b1092006-01-08 01:05:00 -08001162 }
1163
Latchesar Ionkov531b1092006-01-08 01:05:00 -08001164 kfree(fcall);
Latchesar Ionkov6a3124a2006-03-02 02:54:30 -08001165 dentry->d_op = &v9fs_dentry_operations;
1166 d_instantiate(dentry, inode);
1167 return 0;
1168
1169error:
1170 kfree(fcall);
1171 if (vfid)
1172 v9fs_fid_destroy(vfid);
1173
1174 if (inode)
1175 iput(inode);
1176
1177 return err;
1178
Latchesar Ionkov531b1092006-01-08 01:05:00 -08001179}
1180
1181/**
1182 * v9fs_vfs_symlink - helper function to create symlinks
1183 * @dir: directory inode containing symlink
1184 * @dentry: dentry for symlink
1185 * @symname: symlink data
1186 *
1187 * See 9P2000.u RFC for more information
1188 *
1189 */
1190
1191static int
1192v9fs_vfs_symlink(struct inode *dir, struct dentry *dentry, const char *symname)
1193{
1194 dprintk(DEBUG_VFS, " %lu,%s,%s\n", dir->i_ino, dentry->d_name.name,
1195 symname);
1196
1197 return v9fs_vfs_mkspecial(dir, dentry, S_IFLNK, symname);
1198}
1199
Eric Van Hensbergen2bad8472005-09-09 13:04:19 -07001200/**
1201 * v9fs_vfs_link - create a hardlink
1202 * @old_dentry: dentry for file to link to
1203 * @dir: inode destination for new link
Eric Van Hensbergen73c592b2005-09-09 13:04:26 -07001204 * @dentry: dentry for link
Eric Van Hensbergen2bad8472005-09-09 13:04:19 -07001205 *
1206 */
1207
1208/* XXX - lots of code dup'd from symlink and creates,
1209 * figure out a better reuse strategy
1210 */
1211
1212static int
1213v9fs_vfs_link(struct dentry *old_dentry, struct inode *dir,
1214 struct dentry *dentry)
1215{
Latchesar Ionkov531b1092006-01-08 01:05:00 -08001216 int retval;
1217 struct v9fs_fid *oldfid;
1218 char *name;
Eric Van Hensbergen2bad8472005-09-09 13:04:19 -07001219
1220 dprintk(DEBUG_VFS, " %lu,%s,%s\n", dir->i_ino, dentry->d_name.name,
1221 old_dentry->d_name.name);
1222
Latchesar Ionkov531b1092006-01-08 01:05:00 -08001223 oldfid = v9fs_fid_lookup(old_dentry);
1224 if (!oldfid) {
1225 dprintk(DEBUG_ERROR, "can't find oldfid\n");
1226 return -EPERM;
Eric Van Hensbergen2bad8472005-09-09 13:04:19 -07001227 }
1228
Latchesar Ionkov531b1092006-01-08 01:05:00 -08001229 name = __getname();
1230 sprintf(name, "hardlink(%d)\n", oldfid->fid);
1231 retval = v9fs_vfs_mkspecial(dir, dentry, V9FS_DMLINK, name);
1232 __putname(name);
Eric Van Hensbergen2bad8472005-09-09 13:04:19 -07001233
Eric Van Hensbergen2bad8472005-09-09 13:04:19 -07001234 return retval;
1235}
1236
1237/**
1238 * v9fs_vfs_mknod - create a special file
1239 * @dir: inode destination for new link
1240 * @dentry: dentry for file
1241 * @mode: mode for creation
1242 * @dev_t: device associated with special file
1243 *
1244 */
1245
1246static int
1247v9fs_vfs_mknod(struct inode *dir, struct dentry *dentry, int mode, dev_t rdev)
1248{
Latchesar Ionkov531b1092006-01-08 01:05:00 -08001249 int retval;
1250 char *name;
Eric Van Hensbergen2bad8472005-09-09 13:04:19 -07001251
1252 dprintk(DEBUG_VFS, " %lu,%s mode: %x MAJOR: %u MINOR: %u\n", dir->i_ino,
1253 dentry->d_name.name, mode, MAJOR(rdev), MINOR(rdev));
1254
Latchesar Ionkov531b1092006-01-08 01:05:00 -08001255 if (!new_valid_dev(rdev))
1256 return -EINVAL;
Eric Van Hensbergen73c592b2005-09-09 13:04:26 -07001257
Latchesar Ionkov531b1092006-01-08 01:05:00 -08001258 name = __getname();
Eric Van Hensbergen2bad8472005-09-09 13:04:19 -07001259 /* build extension */
1260 if (S_ISBLK(mode))
Latchesar Ionkov531b1092006-01-08 01:05:00 -08001261 sprintf(name, "b %u %u", MAJOR(rdev), MINOR(rdev));
Eric Van Hensbergen2bad8472005-09-09 13:04:19 -07001262 else if (S_ISCHR(mode))
Latchesar Ionkov531b1092006-01-08 01:05:00 -08001263 sprintf(name, "c %u %u", MAJOR(rdev), MINOR(rdev));
Eric Van Hensbergen73c592b2005-09-09 13:04:26 -07001264 else if (S_ISFIFO(mode))
Latchesar Ionkov531b1092006-01-08 01:05:00 -08001265 *name = 0;
Eric Van Hensbergen2bad8472005-09-09 13:04:19 -07001266 else {
Latchesar Ionkov531b1092006-01-08 01:05:00 -08001267 __putname(name);
1268 return -EINVAL;
Eric Van Hensbergen2bad8472005-09-09 13:04:19 -07001269 }
1270
Latchesar Ionkov531b1092006-01-08 01:05:00 -08001271 retval = v9fs_vfs_mkspecial(dir, dentry, mode, name);
1272 __putname(name);
Eric Van Hensbergen2bad8472005-09-09 13:04:19 -07001273
1274 return retval;
1275}
1276
Eric Van Hensbergenb5016112005-09-09 13:04:27 -07001277static struct inode_operations v9fs_dir_inode_operations_ext = {
Eric Van Hensbergen2bad8472005-09-09 13:04:19 -07001278 .create = v9fs_vfs_create,
1279 .lookup = v9fs_vfs_lookup,
1280 .symlink = v9fs_vfs_symlink,
1281 .link = v9fs_vfs_link,
1282 .unlink = v9fs_vfs_unlink,
1283 .mkdir = v9fs_vfs_mkdir,
1284 .rmdir = v9fs_vfs_rmdir,
1285 .mknod = v9fs_vfs_mknod,
1286 .rename = v9fs_vfs_rename,
1287 .readlink = v9fs_vfs_readlink,
1288 .getattr = v9fs_vfs_getattr,
1289 .setattr = v9fs_vfs_setattr,
1290};
1291
Eric Van Hensbergenb5016112005-09-09 13:04:27 -07001292static struct inode_operations v9fs_dir_inode_operations = {
1293 .create = v9fs_vfs_create,
1294 .lookup = v9fs_vfs_lookup,
1295 .unlink = v9fs_vfs_unlink,
1296 .mkdir = v9fs_vfs_mkdir,
1297 .rmdir = v9fs_vfs_rmdir,
1298 .mknod = v9fs_vfs_mknod,
1299 .rename = v9fs_vfs_rename,
1300 .getattr = v9fs_vfs_getattr,
1301 .setattr = v9fs_vfs_setattr,
1302};
1303
Eric Van Hensbergen2bad8472005-09-09 13:04:19 -07001304static struct inode_operations v9fs_file_inode_operations = {
1305 .getattr = v9fs_vfs_getattr,
1306 .setattr = v9fs_vfs_setattr,
1307};
1308
1309static struct inode_operations v9fs_symlink_inode_operations = {
1310 .readlink = v9fs_vfs_readlink,
1311 .follow_link = v9fs_vfs_follow_link,
1312 .put_link = v9fs_vfs_put_link,
1313 .getattr = v9fs_vfs_getattr,
1314 .setattr = v9fs_vfs_setattr,
1315};