blob: 2cb87ba4b1c1fa76ab685d213ae6e0f33a53dcf8 [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
Eric Van Hensbergen42e8c502006-03-25 03:07:28 -080010 * it under the terms of the GNU General Public License version 2
11 * as published by the Free Software Foundation.
Eric Van Hensbergen2bad8472005-09-09 13:04:19 -070012 *
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/smp_lock.h>
34#include <linux/inet.h>
35#include <linux/namei.h>
36#include <linux/idr.h>
37
38#include "debug.h"
39#include "v9fs.h"
40#include "9p.h"
41#include "v9fs_vfs.h"
Eric Van Hensbergen2bad8472005-09-09 13:04:19 -070042#include "fid.h"
43
44static struct inode_operations v9fs_dir_inode_operations;
Eric Van Hensbergenb5016112005-09-09 13:04:27 -070045static struct inode_operations v9fs_dir_inode_operations_ext;
Eric Van Hensbergen2bad8472005-09-09 13:04:19 -070046static struct inode_operations v9fs_file_inode_operations;
47static struct inode_operations v9fs_symlink_inode_operations;
48
49/**
50 * unixmode2p9mode - convert unix mode bits to plan 9
51 * @v9ses: v9fs session information
52 * @mode: mode to convert
53 *
54 */
55
Eric Van Hensbergen73c592b2005-09-09 13:04:26 -070056static int unixmode2p9mode(struct v9fs_session_info *v9ses, int mode)
Eric Van Hensbergen2bad8472005-09-09 13:04:19 -070057{
58 int res;
59 res = mode & 0777;
60 if (S_ISDIR(mode))
61 res |= V9FS_DMDIR;
62 if (v9ses->extended) {
63 if (S_ISLNK(mode))
64 res |= V9FS_DMSYMLINK;
65 if (v9ses->nodev == 0) {
66 if (S_ISSOCK(mode))
67 res |= V9FS_DMSOCKET;
68 if (S_ISFIFO(mode))
69 res |= V9FS_DMNAMEDPIPE;
70 if (S_ISBLK(mode))
71 res |= V9FS_DMDEVICE;
72 if (S_ISCHR(mode))
73 res |= V9FS_DMDEVICE;
74 }
75
76 if ((mode & S_ISUID) == S_ISUID)
77 res |= V9FS_DMSETUID;
78 if ((mode & S_ISGID) == S_ISGID)
79 res |= V9FS_DMSETGID;
80 if ((mode & V9FS_DMLINK))
81 res |= V9FS_DMLINK;
82 }
83
84 return res;
85}
86
87/**
88 * p9mode2unixmode- convert plan9 mode bits to unix mode bits
89 * @v9ses: v9fs session information
90 * @mode: mode to convert
91 *
92 */
93
Eric Van Hensbergen73c592b2005-09-09 13:04:26 -070094static int p9mode2unixmode(struct v9fs_session_info *v9ses, int mode)
Eric Van Hensbergen2bad8472005-09-09 13:04:19 -070095{
96 int res;
97
98 res = mode & 0777;
99
100 if ((mode & V9FS_DMDIR) == V9FS_DMDIR)
101 res |= S_IFDIR;
102 else if ((mode & V9FS_DMSYMLINK) && (v9ses->extended))
103 res |= S_IFLNK;
104 else if ((mode & V9FS_DMSOCKET) && (v9ses->extended)
105 && (v9ses->nodev == 0))
106 res |= S_IFSOCK;
107 else if ((mode & V9FS_DMNAMEDPIPE) && (v9ses->extended)
108 && (v9ses->nodev == 0))
109 res |= S_IFIFO;
110 else if ((mode & V9FS_DMDEVICE) && (v9ses->extended)
111 && (v9ses->nodev == 0))
112 res |= S_IFBLK;
113 else
114 res |= S_IFREG;
115
116 if (v9ses->extended) {
117 if ((mode & V9FS_DMSETUID) == V9FS_DMSETUID)
118 res |= S_ISUID;
119
120 if ((mode & V9FS_DMSETGID) == V9FS_DMSETGID)
121 res |= S_ISGID;
122 }
123
124 return res;
125}
126
Latchesar Ionkov6a3124a2006-03-02 02:54:30 -0800127int v9fs_uflags2omode(int uflags)
128{
129 int ret;
130
131 ret = 0;
132 switch (uflags&3) {
133 default:
134 case O_RDONLY:
135 ret = V9FS_OREAD;
136 break;
137
138 case O_WRONLY:
139 ret = V9FS_OWRITE;
140 break;
141
142 case O_RDWR:
143 ret = V9FS_ORDWR;
144 break;
145 }
146
147 if (uflags & O_EXCL)
148 ret |= V9FS_OEXCL;
149
150 if (uflags & O_TRUNC)
151 ret |= V9FS_OTRUNC;
152
153 if (uflags & O_APPEND)
154 ret |= V9FS_OAPPEND;
155
156 return ret;
157}
158
Eric Van Hensbergen2bad8472005-09-09 13:04:19 -0700159/**
Latchesar Ionkov531b1092006-01-08 01:05:00 -0800160 * v9fs_blank_wstat - helper function to setup a 9P stat structure
Eric Van Hensbergen2bad8472005-09-09 13:04:19 -0700161 * @v9ses: 9P session info (for determining extended mode)
Latchesar Ionkov531b1092006-01-08 01:05:00 -0800162 * @wstat: structure to initialize
Eric Van Hensbergen2bad8472005-09-09 13:04:19 -0700163 *
164 */
165
Eric Van Hensbergen73c592b2005-09-09 13:04:26 -0700166static void
Latchesar Ionkov531b1092006-01-08 01:05:00 -0800167v9fs_blank_wstat(struct v9fs_wstat *wstat)
Eric Van Hensbergen2bad8472005-09-09 13:04:19 -0700168{
Latchesar Ionkov531b1092006-01-08 01:05:00 -0800169 wstat->type = ~0;
170 wstat->dev = ~0;
171 wstat->qid.type = ~0;
172 wstat->qid.version = ~0;
173 *((long long *)&wstat->qid.path) = ~0;
174 wstat->mode = ~0;
175 wstat->atime = ~0;
176 wstat->mtime = ~0;
177 wstat->length = ~0;
178 wstat->name = NULL;
179 wstat->uid = NULL;
180 wstat->gid = NULL;
181 wstat->muid = NULL;
182 wstat->n_uid = ~0;
183 wstat->n_gid = ~0;
184 wstat->n_muid = ~0;
185 wstat->extension = NULL;
Eric Van Hensbergen2bad8472005-09-09 13:04:19 -0700186}
187
188/**
189 * v9fs_get_inode - helper function to setup an inode
190 * @sb: superblock
191 * @mode: mode to setup inode with
192 *
193 */
194
195struct inode *v9fs_get_inode(struct super_block *sb, int mode)
196{
Latchesar Ionkov6a3124a2006-03-02 02:54:30 -0800197 struct inode *inode;
Eric Van Hensbergenb5016112005-09-09 13:04:27 -0700198 struct v9fs_session_info *v9ses = sb->s_fs_info;
Eric Van Hensbergen2bad8472005-09-09 13:04:19 -0700199
200 dprintk(DEBUG_VFS, "super block: %p mode: %o\n", sb, mode);
201
202 inode = new_inode(sb);
203 if (inode) {
204 inode->i_mode = mode;
205 inode->i_uid = current->fsuid;
206 inode->i_gid = current->fsgid;
207 inode->i_blksize = sb->s_blocksize;
208 inode->i_blocks = 0;
209 inode->i_rdev = 0;
210 inode->i_atime = inode->i_mtime = inode->i_ctime = CURRENT_TIME;
Eric Van Hensbergen147b31c2006-01-18 17:43:02 -0800211 inode->i_mapping->a_ops = &v9fs_addr_operations;
Eric Van Hensbergen2bad8472005-09-09 13:04:19 -0700212
213 switch (mode & S_IFMT) {
214 case S_IFIFO:
215 case S_IFBLK:
216 case S_IFCHR:
217 case S_IFSOCK:
Eric Van Hensbergenb5016112005-09-09 13:04:27 -0700218 if(!v9ses->extended) {
219 dprintk(DEBUG_ERROR, "special files without extended mode\n");
220 return ERR_PTR(-EINVAL);
221 }
Eric Van Hensbergen5d58bec2005-09-09 13:04:27 -0700222 init_special_inode(inode, inode->i_mode,
223 inode->i_rdev);
224 break;
Eric Van Hensbergen2bad8472005-09-09 13:04:19 -0700225 case S_IFREG:
226 inode->i_op = &v9fs_file_inode_operations;
227 inode->i_fop = &v9fs_file_operations;
228 break;
Eric Van Hensbergenb5016112005-09-09 13:04:27 -0700229 case S_IFLNK:
230 if(!v9ses->extended) {
231 dprintk(DEBUG_ERROR, "extended modes used w/o 9P2000.u\n");
232 return ERR_PTR(-EINVAL);
233 }
234 inode->i_op = &v9fs_symlink_inode_operations;
235 break;
Eric Van Hensbergen2bad8472005-09-09 13:04:19 -0700236 case S_IFDIR:
237 inode->i_nlink++;
Eric Van Hensbergenb5016112005-09-09 13:04:27 -0700238 if(v9ses->extended)
239 inode->i_op = &v9fs_dir_inode_operations_ext;
240 else
241 inode->i_op = &v9fs_dir_inode_operations;
Eric Van Hensbergen2bad8472005-09-09 13:04:19 -0700242 inode->i_fop = &v9fs_dir_operations;
243 break;
Eric Van Hensbergen2bad8472005-09-09 13:04:19 -0700244 default:
245 dprintk(DEBUG_ERROR, "BAD mode 0x%x S_IFMT 0x%x\n",
246 mode, mode & S_IFMT);
247 return ERR_PTR(-EINVAL);
248 }
249 } else {
250 eprintk(KERN_WARNING, "Problem allocating inode\n");
251 return ERR_PTR(-ENOMEM);
252 }
253 return inode;
254}
255
Eric Van Hensbergen2bad8472005-09-09 13:04:19 -0700256static int
Latchesar Ionkov16cce6d2006-03-25 03:07:26 -0800257v9fs_create(struct v9fs_session_info *v9ses, u32 pfid, char *name, u32 perm,
258 u8 mode, char *extension, u32 *fidp, struct v9fs_qid *qid, u32 *iounit)
Eric Van Hensbergen2bad8472005-09-09 13:04:19 -0700259{
Latchesar Ionkov6a3124a2006-03-02 02:54:30 -0800260 u32 fid;
Latchesar Ionkov3cf64292006-01-08 01:04:58 -0800261 int err;
Latchesar Ionkov6a3124a2006-03-02 02:54:30 -0800262 struct v9fs_fcall *fcall;
Eric Van Hensbergen2bad8472005-09-09 13:04:19 -0700263
Latchesar Ionkov6a3124a2006-03-02 02:54:30 -0800264 fid = v9fs_get_idpool(&v9ses->fidpool);
265 if (fid < 0) {
Eric Van Hensbergen2bad8472005-09-09 13:04:19 -0700266 eprintk(KERN_WARNING, "no free fids available\n");
Latchesar Ionkov731805b2006-03-07 21:55:42 -0800267 return -ENOSPC;
Eric Van Hensbergen2bad8472005-09-09 13:04:19 -0700268 }
269
Latchesar Ionkov6a3124a2006-03-02 02:54:30 -0800270 err = v9fs_t_walk(v9ses, pfid, fid, NULL, &fcall);
271 if (err < 0) {
Latchesar Ionkov531b1092006-01-08 01:05:00 -0800272 PRINT_FCALL_ERROR("clone error", fcall);
Latchesar Ionkov41e5a6a2006-05-15 09:44:21 -0700273 if (fcall && fcall->id == RWALK)
274 goto clunk_fid;
275 else
276 goto put_fid;
Eric Van Hensbergen2bad8472005-09-09 13:04:19 -0700277 }
Eric Van Hensbergen2bad8472005-09-09 13:04:19 -0700278 kfree(fcall);
279
Latchesar Ionkov16cce6d2006-03-25 03:07:26 -0800280 err = v9fs_t_create(v9ses, fid, name, perm, mode, extension, &fcall);
Latchesar Ionkov6a3124a2006-03-02 02:54:30 -0800281 if (err < 0) {
Latchesar Ionkov531b1092006-01-08 01:05:00 -0800282 PRINT_FCALL_ERROR("create fails", fcall);
Latchesar Ionkov16cce6d2006-03-25 03:07:26 -0800283 goto clunk_fid;
Eric Van Hensbergen2bad8472005-09-09 13:04:19 -0700284 }
285
Latchesar Ionkov6a3124a2006-03-02 02:54:30 -0800286 if (iounit)
287 *iounit = fcall->params.rcreate.iounit;
288
289 if (qid)
290 *qid = fcall->params.rcreate.qid;
291
292 if (fidp)
293 *fidp = fid;
294
Eric Van Hensbergen2bad8472005-09-09 13:04:19 -0700295 kfree(fcall);
Eric Van Hensbergen2bad8472005-09-09 13:04:19 -0700296 return 0;
297
Latchesar Ionkov16cce6d2006-03-25 03:07:26 -0800298clunk_fid:
299 v9fs_t_clunk(v9ses, fid);
300 fid = V9FS_NOFID;
301
302put_fid:
Latchesar Ionkov6a3124a2006-03-02 02:54:30 -0800303 if (fid >= 0)
304 v9fs_put_idpool(fid, &v9ses->fidpool);
305
306 kfree(fcall);
307 return err;
308}
309
310static struct v9fs_fid*
311v9fs_clone_walk(struct v9fs_session_info *v9ses, u32 fid, struct dentry *dentry)
312{
313 int err;
314 u32 nfid;
315 struct v9fs_fid *ret;
316 struct v9fs_fcall *fcall;
317
318 nfid = v9fs_get_idpool(&v9ses->fidpool);
319 if (nfid < 0) {
320 eprintk(KERN_WARNING, "no free fids available\n");
Latchesar Ionkov731805b2006-03-07 21:55:42 -0800321 return ERR_PTR(-ENOSPC);
Latchesar Ionkov6a3124a2006-03-02 02:54:30 -0800322 }
323
324 err = v9fs_t_walk(v9ses, fid, nfid, (char *) dentry->d_name.name,
325 &fcall);
326
327 if (err < 0) {
Latchesar Ionkov41e5a6a2006-05-15 09:44:21 -0700328 if (fcall && fcall->id == RWALK)
329 goto clunk_fid;
330
Latchesar Ionkov6a3124a2006-03-02 02:54:30 -0800331 PRINT_FCALL_ERROR("walk error", fcall);
332 v9fs_put_idpool(nfid, &v9ses->fidpool);
333 goto error;
334 }
335
Eric Van Hensbergen2bad8472005-09-09 13:04:19 -0700336 kfree(fcall);
Latchesar Ionkov3cf64292006-01-08 01:04:58 -0800337 fcall = NULL;
Latchesar Ionkov6a3124a2006-03-02 02:54:30 -0800338 ret = v9fs_fid_create(v9ses, nfid);
339 if (!ret) {
340 err = -ENOMEM;
341 goto clunk_fid;
342 }
Eric Van Hensbergen2bad8472005-09-09 13:04:19 -0700343
Latchesar Ionkov6a3124a2006-03-02 02:54:30 -0800344 err = v9fs_fid_insert(ret, dentry);
345 if (err < 0) {
346 v9fs_fid_destroy(ret);
347 goto clunk_fid;
Latchesar Ionkov0b8dd172005-09-27 21:45:24 -0700348 }
Latchesar Ionkov6a3124a2006-03-02 02:54:30 -0800349
350 return ret;
351
352clunk_fid:
353 v9fs_t_clunk(v9ses, nfid);
354
355error:
356 kfree(fcall);
357 return ERR_PTR(err);
358}
359
Latchesar Ionkov5174fda2006-03-25 03:07:25 -0800360static struct inode *
Latchesar Ionkov6a3124a2006-03-02 02:54:30 -0800361v9fs_inode_from_fid(struct v9fs_session_info *v9ses, u32 fid,
362 struct super_block *sb)
363{
364 int err, umode;
365 struct inode *ret;
366 struct v9fs_fcall *fcall;
367
368 ret = NULL;
369 err = v9fs_t_stat(v9ses, fid, &fcall);
370 if (err) {
371 PRINT_FCALL_ERROR("stat error", fcall);
372 goto error;
Eric Van Hensbergen2bad8472005-09-09 13:04:19 -0700373 }
Latchesar Ionkov6a3124a2006-03-02 02:54:30 -0800374
375 umode = p9mode2unixmode(v9ses, fcall->params.rstat.stat.mode);
376 ret = v9fs_get_inode(sb, umode);
377 if (IS_ERR(ret)) {
378 err = PTR_ERR(ret);
379 ret = NULL;
380 goto error;
381 }
382
383 v9fs_stat2inode(&fcall->params.rstat.stat, ret, sb);
384 kfree(fcall);
385 return ret;
386
387error:
388 kfree(fcall);
389 if (ret)
390 iput(ret);
391
392 return ERR_PTR(err);
Eric Van Hensbergen2bad8472005-09-09 13:04:19 -0700393}
394
395/**
396 * v9fs_remove - helper function to remove files and directories
Eric Van Hensbergen73c592b2005-09-09 13:04:26 -0700397 * @dir: directory inode that is being deleted
398 * @file: dentry that is being deleted
399 * @rmdir: removing a directory
Eric Van Hensbergen2bad8472005-09-09 13:04:19 -0700400 *
401 */
402
403static int v9fs_remove(struct inode *dir, struct dentry *file, int rmdir)
404{
405 struct v9fs_fcall *fcall = NULL;
406 struct super_block *sb = NULL;
407 struct v9fs_session_info *v9ses = NULL;
408 struct v9fs_fid *v9fid = NULL;
409 struct inode *file_inode = NULL;
410 int fid = -1;
411 int result = 0;
412
413 dprintk(DEBUG_VFS, "inode: %p dentry: %p rmdir: %d\n", dir, file,
414 rmdir);
415
416 file_inode = file->d_inode;
417 sb = file_inode->i_sb;
418 v9ses = v9fs_inode2v9ses(file_inode);
Latchesar Ionkov0b8dd172005-09-27 21:45:24 -0700419 v9fid = v9fs_fid_lookup(file);
Eric Van Hensbergen2bad8472005-09-09 13:04:19 -0700420
421 if (!v9fid) {
422 dprintk(DEBUG_ERROR,
423 "no v9fs_fid\n");
424 return -EBADF;
425 }
426
427 fid = v9fid->fid;
428 if (fid < 0) {
429 dprintk(DEBUG_ERROR, "inode #%lu, no fid!\n",
430 file_inode->i_ino);
431 return -EBADF;
432 }
433
434 result = v9fs_t_remove(v9ses, fid, &fcall);
Latchesar Ionkov531b1092006-01-08 01:05:00 -0800435 if (result < 0) {
436 PRINT_FCALL_ERROR("remove fails", fcall);
437 } else {
Eric Van Hensbergen2bad8472005-09-09 13:04:19 -0700438 v9fs_put_idpool(fid, &v9ses->fidpool);
439 v9fs_fid_destroy(v9fid);
440 }
441
442 kfree(fcall);
443 return result;
444}
445
Latchesar Ionkov6a3124a2006-03-02 02:54:30 -0800446static int
447v9fs_open_created(struct inode *inode, struct file *file)
448{
449 return 0;
450}
451
Eric Van Hensbergen2bad8472005-09-09 13:04:19 -0700452/**
453 * v9fs_vfs_create - VFS hook to create files
454 * @inode: directory inode that is being deleted
455 * @dentry: dentry that is being deleted
Latchesar Ionkov6a3124a2006-03-02 02:54:30 -0800456 * @mode: create permissions
Eric Van Hensbergen2bad8472005-09-09 13:04:19 -0700457 * @nd: path information
458 *
459 */
460
461static int
Latchesar Ionkov6a3124a2006-03-02 02:54:30 -0800462v9fs_vfs_create(struct inode *dir, struct dentry *dentry, int mode,
Eric Van Hensbergen2bad8472005-09-09 13:04:19 -0700463 struct nameidata *nd)
464{
Latchesar Ionkov6a3124a2006-03-02 02:54:30 -0800465 int err;
466 u32 fid, perm, iounit;
467 int flags;
468 struct v9fs_session_info *v9ses;
469 struct v9fs_fid *dfid, *vfid, *ffid;
470 struct inode *inode;
471 struct v9fs_qid qid;
472 struct file *filp;
473
474 inode = NULL;
475 vfid = NULL;
476 v9ses = v9fs_inode2v9ses(dir);
477 dfid = v9fs_fid_lookup(dentry->d_parent);
478 perm = unixmode2p9mode(v9ses, mode);
479
480 if (nd && nd->flags & LOOKUP_OPEN)
481 flags = nd->intent.open.flags - 1;
482 else
483 flags = O_RDWR;
484
485 err = v9fs_create(v9ses, dfid->fid, (char *) dentry->d_name.name,
Latchesar Ionkov16cce6d2006-03-25 03:07:26 -0800486 perm, v9fs_uflags2omode(flags), NULL, &fid, &qid, &iounit);
Latchesar Ionkov6a3124a2006-03-02 02:54:30 -0800487
488 if (err)
489 goto error;
490
491 vfid = v9fs_clone_walk(v9ses, dfid->fid, dentry);
492 if (IS_ERR(vfid)) {
493 err = PTR_ERR(vfid);
494 vfid = NULL;
495 goto error;
496 }
497
498 inode = v9fs_inode_from_fid(v9ses, vfid->fid, dir->i_sb);
499 if (IS_ERR(inode)) {
500 err = PTR_ERR(inode);
501 inode = NULL;
502 goto error;
503 }
504
505 dentry->d_op = &v9fs_dentry_operations;
506 d_instantiate(dentry, inode);
507
508 if (nd && nd->flags & LOOKUP_OPEN) {
509 ffid = v9fs_fid_create(v9ses, fid);
510 if (!ffid)
511 return -ENOMEM;
512
513 filp = lookup_instantiate_filp(nd, dentry, v9fs_open_created);
514 if (IS_ERR(filp)) {
515 v9fs_fid_destroy(ffid);
516 return PTR_ERR(filp);
517 }
518
519 ffid->rdir_pos = 0;
520 ffid->rdir_fcall = NULL;
521 ffid->fidopen = 1;
522 ffid->iounit = iounit;
523 ffid->filp = filp;
524 filp->private_data = ffid;
525 }
526
527 return 0;
528
529error:
530 if (vfid)
531 v9fs_fid_destroy(vfid);
532
533 if (inode)
534 iput(inode);
535
536 return err;
Eric Van Hensbergen2bad8472005-09-09 13:04:19 -0700537}
538
539/**
540 * v9fs_vfs_mkdir - VFS mkdir hook to create a directory
Eric Van Hensbergen73c592b2005-09-09 13:04:26 -0700541 * @inode: inode that is being unlinked
Eric Van Hensbergen2bad8472005-09-09 13:04:19 -0700542 * @dentry: dentry that is being unlinked
543 * @mode: mode for new directory
544 *
545 */
546
Latchesar Ionkov6a3124a2006-03-02 02:54:30 -0800547static int v9fs_vfs_mkdir(struct inode *dir, struct dentry *dentry, int mode)
Eric Van Hensbergen2bad8472005-09-09 13:04:19 -0700548{
Latchesar Ionkov6a3124a2006-03-02 02:54:30 -0800549 int err;
550 u32 fid, perm;
551 struct v9fs_session_info *v9ses;
552 struct v9fs_fid *dfid, *vfid;
553 struct inode *inode;
554
555 inode = NULL;
556 vfid = NULL;
557 v9ses = v9fs_inode2v9ses(dir);
558 dfid = v9fs_fid_lookup(dentry->d_parent);
559 perm = unixmode2p9mode(v9ses, mode | S_IFDIR);
560
561 err = v9fs_create(v9ses, dfid->fid, (char *) dentry->d_name.name,
Latchesar Ionkov16cce6d2006-03-25 03:07:26 -0800562 perm, V9FS_OREAD, NULL, &fid, NULL, NULL);
Latchesar Ionkov6a3124a2006-03-02 02:54:30 -0800563
564 if (err) {
565 dprintk(DEBUG_ERROR, "create error %d\n", err);
566 goto error;
567 }
568
569 err = v9fs_t_clunk(v9ses, fid);
570 if (err) {
571 dprintk(DEBUG_ERROR, "clunk error %d\n", err);
572 goto error;
573 }
574
575 vfid = v9fs_clone_walk(v9ses, dfid->fid, dentry);
576 if (IS_ERR(vfid)) {
577 err = PTR_ERR(vfid);
578 vfid = NULL;
579 goto error;
580 }
581
582 inode = v9fs_inode_from_fid(v9ses, vfid->fid, dir->i_sb);
583 if (IS_ERR(inode)) {
584 err = PTR_ERR(inode);
585 inode = NULL;
586 goto error;
587 }
588
589 dentry->d_op = &v9fs_dentry_operations;
590 d_instantiate(dentry, inode);
591 return 0;
592
593error:
594 if (vfid)
595 v9fs_fid_destroy(vfid);
596
597 return err;
Eric Van Hensbergen2bad8472005-09-09 13:04:19 -0700598}
599
600/**
601 * v9fs_vfs_lookup - VFS lookup hook to "walk" to a new inode
602 * @dir: inode that is being walked from
603 * @dentry: dentry that is being walked to?
604 * @nameidata: path data
605 *
606 */
607
608static struct dentry *v9fs_vfs_lookup(struct inode *dir, struct dentry *dentry,
609 struct nameidata *nameidata)
610{
611 struct super_block *sb;
612 struct v9fs_session_info *v9ses;
613 struct v9fs_fid *dirfid;
614 struct v9fs_fid *fid;
615 struct inode *inode;
616 struct v9fs_fcall *fcall = NULL;
Eric Van Hensbergen2bad8472005-09-09 13:04:19 -0700617 int dirfidnum = -1;
618 int newfid = -1;
619 int result = 0;
620
621 dprintk(DEBUG_VFS, "dir: %p dentry: (%s) %p nameidata: %p\n",
Latchesar Ionkov731805b2006-03-07 21:55:42 -0800622 dir, dentry->d_name.name, dentry, nameidata);
Eric Van Hensbergen2bad8472005-09-09 13:04:19 -0700623
624 sb = dir->i_sb;
625 v9ses = v9fs_inode2v9ses(dir);
Latchesar Ionkov5e7a99a2006-03-22 00:07:37 -0800626 dentry->d_op = &v9fs_dentry_operations;
Latchesar Ionkov0b8dd172005-09-27 21:45:24 -0700627 dirfid = v9fs_fid_lookup(dentry->d_parent);
Eric Van Hensbergen2bad8472005-09-09 13:04:19 -0700628
629 if (!dirfid) {
630 dprintk(DEBUG_ERROR, "no dirfid\n");
631 return ERR_PTR(-EINVAL);
632 }
633
634 dirfidnum = dirfid->fid;
635
636 if (dirfidnum < 0) {
637 dprintk(DEBUG_ERROR, "no dirfid for inode %p, #%lu\n",
638 dir, dir->i_ino);
639 return ERR_PTR(-EBADF);
640 }
641
642 newfid = v9fs_get_idpool(&v9ses->fidpool);
643 if (newfid < 0) {
644 eprintk(KERN_WARNING, "newfid fails!\n");
645 return ERR_PTR(-ENOSPC);
646 }
647
Latchesar Ionkov6a3124a2006-03-02 02:54:30 -0800648 result = v9fs_t_walk(v9ses, dirfidnum, newfid,
Latchesar Ionkov41e5a6a2006-05-15 09:44:21 -0700649 (char *)dentry->d_name.name, &fcall);
650
Eric Van Hensbergen2bad8472005-09-09 13:04:19 -0700651 if (result < 0) {
Latchesar Ionkov41e5a6a2006-05-15 09:44:21 -0700652 if (fcall && fcall->id == RWALK)
653 v9fs_t_clunk(v9ses, newfid);
654 else
655 v9fs_put_idpool(newfid, &v9ses->fidpool);
656
Eric Van Hensbergen2bad8472005-09-09 13:04:19 -0700657 if (result == -ENOENT) {
658 d_add(dentry, NULL);
Latchesar Ionkov0b8dd172005-09-27 21:45:24 -0700659 dprintk(DEBUG_VFS,
Eric Van Hensbergen2bad8472005-09-09 13:04:19 -0700660 "Return negative dentry %p count %d\n",
661 dentry, atomic_read(&dentry->d_count));
Latchesar Ionkov41e5a6a2006-05-15 09:44:21 -0700662 kfree(fcall);
Eric Van Hensbergen2bad8472005-09-09 13:04:19 -0700663 return NULL;
664 }
665 dprintk(DEBUG_ERROR, "walk error:%d\n", result);
666 goto FreeFcall;
667 }
Latchesar Ionkov41e5a6a2006-05-15 09:44:21 -0700668 kfree(fcall);
Eric Van Hensbergen2bad8472005-09-09 13:04:19 -0700669
670 result = v9fs_t_stat(v9ses, newfid, &fcall);
671 if (result < 0) {
672 dprintk(DEBUG_ERROR, "stat error\n");
673 goto FreeFcall;
674 }
675
Latchesar Ionkov531b1092006-01-08 01:05:00 -0800676 inode = v9fs_get_inode(sb, p9mode2unixmode(v9ses,
677 fcall->params.rstat.stat.mode));
Eric Van Hensbergen2bad8472005-09-09 13:04:19 -0700678
679 if (IS_ERR(inode) && (PTR_ERR(inode) == -ENOSPC)) {
680 eprintk(KERN_WARNING, "inode alloc failes, returns %ld\n",
681 PTR_ERR(inode));
682
683 result = -ENOSPC;
684 goto FreeFcall;
685 }
686
Latchesar Ionkov531b1092006-01-08 01:05:00 -0800687 inode->i_ino = v9fs_qid2ino(&fcall->params.rstat.stat.qid);
Eric Van Hensbergen2bad8472005-09-09 13:04:19 -0700688
Latchesar Ionkov6a3124a2006-03-02 02:54:30 -0800689 fid = v9fs_fid_create(v9ses, newfid);
Eric Van Hensbergen2bad8472005-09-09 13:04:19 -0700690 if (fid == NULL) {
691 dprintk(DEBUG_ERROR, "couldn't insert\n");
692 result = -ENOMEM;
693 goto FreeFcall;
694 }
695
Latchesar Ionkov6a3124a2006-03-02 02:54:30 -0800696 result = v9fs_fid_insert(fid, dentry);
697 if (result < 0)
698 goto FreeFcall;
699
Latchesar Ionkov531b1092006-01-08 01:05:00 -0800700 fid->qid = fcall->params.rstat.stat.qid;
Latchesar Ionkov531b1092006-01-08 01:05:00 -0800701 v9fs_stat2inode(&fcall->params.rstat.stat, inode, inode->i_sb);
Eric Van Hensbergen2bad8472005-09-09 13:04:19 -0700702
703 d_add(dentry, inode);
704 kfree(fcall);
705
706 return NULL;
707
708 FreeFcall:
709 kfree(fcall);
710 return ERR_PTR(result);
711}
712
713/**
714 * v9fs_vfs_unlink - VFS unlink hook to delete an inode
715 * @i: inode that is being unlinked
Eric Van Hensbergen73c592b2005-09-09 13:04:26 -0700716 * @d: dentry that is being unlinked
Eric Van Hensbergen2bad8472005-09-09 13:04:19 -0700717 *
718 */
719
720static int v9fs_vfs_unlink(struct inode *i, struct dentry *d)
721{
722 return v9fs_remove(i, d, 0);
723}
724
725/**
726 * v9fs_vfs_rmdir - VFS unlink hook to delete a directory
727 * @i: inode that is being unlinked
Eric Van Hensbergen73c592b2005-09-09 13:04:26 -0700728 * @d: dentry that is being unlinked
Eric Van Hensbergen2bad8472005-09-09 13:04:19 -0700729 *
730 */
731
732static int v9fs_vfs_rmdir(struct inode *i, struct dentry *d)
733{
734 return v9fs_remove(i, d, 1);
735}
736
737/**
738 * v9fs_vfs_rename - VFS hook to rename an inode
739 * @old_dir: old dir inode
740 * @old_dentry: old dentry
741 * @new_dir: new dir inode
742 * @new_dentry: new dentry
743 *
744 */
745
746static int
747v9fs_vfs_rename(struct inode *old_dir, struct dentry *old_dentry,
748 struct inode *new_dir, struct dentry *new_dentry)
749{
750 struct inode *old_inode = old_dentry->d_inode;
751 struct v9fs_session_info *v9ses = v9fs_inode2v9ses(old_inode);
Latchesar Ionkov0b8dd172005-09-27 21:45:24 -0700752 struct v9fs_fid *oldfid = v9fs_fid_lookup(old_dentry);
Eric Van Hensbergen2bad8472005-09-09 13:04:19 -0700753 struct v9fs_fid *olddirfid =
Latchesar Ionkov0b8dd172005-09-27 21:45:24 -0700754 v9fs_fid_lookup(old_dentry->d_parent);
Eric Van Hensbergen2bad8472005-09-09 13:04:19 -0700755 struct v9fs_fid *newdirfid =
Latchesar Ionkov0b8dd172005-09-27 21:45:24 -0700756 v9fs_fid_lookup(new_dentry->d_parent);
Latchesar Ionkov531b1092006-01-08 01:05:00 -0800757 struct v9fs_wstat wstat;
Eric Van Hensbergen2bad8472005-09-09 13:04:19 -0700758 struct v9fs_fcall *fcall = NULL;
759 int fid = -1;
760 int olddirfidnum = -1;
761 int newdirfidnum = -1;
762 int retval = 0;
763
764 dprintk(DEBUG_VFS, "\n");
765
766 if ((!oldfid) || (!olddirfid) || (!newdirfid)) {
767 dprintk(DEBUG_ERROR, "problem with arguments\n");
768 return -EBADF;
769 }
770
771 /* 9P can only handle file rename in the same directory */
772 if (memcmp(&olddirfid->qid, &newdirfid->qid, sizeof(newdirfid->qid))) {
773 dprintk(DEBUG_ERROR, "old dir and new dir are different\n");
774 retval = -EPERM;
775 goto FreeFcallnBail;
776 }
777
778 fid = oldfid->fid;
779 olddirfidnum = olddirfid->fid;
780 newdirfidnum = newdirfid->fid;
781
782 if (fid < 0) {
783 dprintk(DEBUG_ERROR, "no fid for old file #%lu\n",
784 old_inode->i_ino);
785 retval = -EBADF;
786 goto FreeFcallnBail;
787 }
788
Latchesar Ionkov531b1092006-01-08 01:05:00 -0800789 v9fs_blank_wstat(&wstat);
790 wstat.muid = v9ses->name;
791 wstat.name = (char *) new_dentry->d_name.name;
Eric Van Hensbergen2bad8472005-09-09 13:04:19 -0700792
Latchesar Ionkov531b1092006-01-08 01:05:00 -0800793 retval = v9fs_t_wstat(v9ses, fid, &wstat, &fcall);
Eric Van Hensbergen2bad8472005-09-09 13:04:19 -0700794
795 FreeFcallnBail:
Eric Van Hensbergen2bad8472005-09-09 13:04:19 -0700796 if (retval < 0)
Latchesar Ionkov531b1092006-01-08 01:05:00 -0800797 PRINT_FCALL_ERROR("wstat error", fcall);
Eric Van Hensbergen2bad8472005-09-09 13:04:19 -0700798
799 kfree(fcall);
800 return retval;
801}
802
803/**
Adrian Bunk943ffb52006-01-10 00:10:13 +0100804 * v9fs_vfs_getattr - retrieve file metadata
Eric Van Hensbergen2bad8472005-09-09 13:04:19 -0700805 * @mnt - mount information
806 * @dentry - file to get attributes on
807 * @stat - metadata structure to populate
808 *
809 */
810
811static int
812v9fs_vfs_getattr(struct vfsmount *mnt, struct dentry *dentry,
813 struct kstat *stat)
814{
815 struct v9fs_fcall *fcall = NULL;
816 struct v9fs_session_info *v9ses = v9fs_inode2v9ses(dentry->d_inode);
Latchesar Ionkov0b8dd172005-09-27 21:45:24 -0700817 struct v9fs_fid *fid = v9fs_fid_lookup(dentry);
Eric Van Hensbergen2bad8472005-09-09 13:04:19 -0700818 int err = -EPERM;
819
820 dprintk(DEBUG_VFS, "dentry: %p\n", dentry);
821 if (!fid) {
822 dprintk(DEBUG_ERROR,
823 "couldn't find fid associated with dentry\n");
824 return -EBADF;
825 }
826
827 err = v9fs_t_stat(v9ses, fid->fid, &fcall);
828
829 if (err < 0)
830 dprintk(DEBUG_ERROR, "stat error\n");
831 else {
Latchesar Ionkov531b1092006-01-08 01:05:00 -0800832 v9fs_stat2inode(&fcall->params.rstat.stat, dentry->d_inode,
Eric Van Hensbergen2bad8472005-09-09 13:04:19 -0700833 dentry->d_inode->i_sb);
834 generic_fillattr(dentry->d_inode, stat);
835 }
836
837 kfree(fcall);
838 return err;
839}
840
841/**
842 * v9fs_vfs_setattr - set file metadata
843 * @dentry: file whose metadata to set
844 * @iattr: metadata assignment structure
845 *
846 */
847
848static int v9fs_vfs_setattr(struct dentry *dentry, struct iattr *iattr)
849{
850 struct v9fs_session_info *v9ses = v9fs_inode2v9ses(dentry->d_inode);
Latchesar Ionkov0b8dd172005-09-27 21:45:24 -0700851 struct v9fs_fid *fid = v9fs_fid_lookup(dentry);
Eric Van Hensbergen2bad8472005-09-09 13:04:19 -0700852 struct v9fs_fcall *fcall = NULL;
Latchesar Ionkov531b1092006-01-08 01:05:00 -0800853 struct v9fs_wstat wstat;
Eric Van Hensbergen2bad8472005-09-09 13:04:19 -0700854 int res = -EPERM;
855
856 dprintk(DEBUG_VFS, "\n");
Eric Van Hensbergen73c592b2005-09-09 13:04:26 -0700857
Eric Van Hensbergen2bad8472005-09-09 13:04:19 -0700858 if (!fid) {
859 dprintk(DEBUG_ERROR,
860 "Couldn't find fid associated with dentry\n");
861 return -EBADF;
862 }
863
Latchesar Ionkov531b1092006-01-08 01:05:00 -0800864 v9fs_blank_wstat(&wstat);
Eric Van Hensbergen2bad8472005-09-09 13:04:19 -0700865 if (iattr->ia_valid & ATTR_MODE)
Latchesar Ionkov531b1092006-01-08 01:05:00 -0800866 wstat.mode = unixmode2p9mode(v9ses, iattr->ia_mode);
Eric Van Hensbergen2bad8472005-09-09 13:04:19 -0700867
868 if (iattr->ia_valid & ATTR_MTIME)
Latchesar Ionkov531b1092006-01-08 01:05:00 -0800869 wstat.mtime = iattr->ia_mtime.tv_sec;
Eric Van Hensbergen2bad8472005-09-09 13:04:19 -0700870
871 if (iattr->ia_valid & ATTR_ATIME)
Latchesar Ionkov531b1092006-01-08 01:05:00 -0800872 wstat.atime = iattr->ia_atime.tv_sec;
Eric Van Hensbergen2bad8472005-09-09 13:04:19 -0700873
874 if (iattr->ia_valid & ATTR_SIZE)
Latchesar Ionkov531b1092006-01-08 01:05:00 -0800875 wstat.length = iattr->ia_size;
Eric Van Hensbergen2bad8472005-09-09 13:04:19 -0700876
877 if (v9ses->extended) {
Latchesar Ionkov531b1092006-01-08 01:05:00 -0800878 if (iattr->ia_valid & ATTR_UID)
879 wstat.n_uid = iattr->ia_uid;
Eric Van Hensbergen2bad8472005-09-09 13:04:19 -0700880
Latchesar Ionkov531b1092006-01-08 01:05:00 -0800881 if (iattr->ia_valid & ATTR_GID)
882 wstat.n_gid = iattr->ia_gid;
Eric Van Hensbergen2bad8472005-09-09 13:04:19 -0700883 }
884
Latchesar Ionkov531b1092006-01-08 01:05:00 -0800885 res = v9fs_t_wstat(v9ses, fid->fid, &wstat, &fcall);
Eric Van Hensbergen2bad8472005-09-09 13:04:19 -0700886
887 if (res < 0)
Latchesar Ionkov531b1092006-01-08 01:05:00 -0800888 PRINT_FCALL_ERROR("wstat error", fcall);
Eric Van Hensbergen2bad8472005-09-09 13:04:19 -0700889
Eric Van Hensbergen2bad8472005-09-09 13:04:19 -0700890 kfree(fcall);
Eric Van Hensbergen2bad8472005-09-09 13:04:19 -0700891 if (res >= 0)
892 res = inode_setattr(dentry->d_inode, iattr);
893
894 return res;
895}
896
897/**
Latchesar Ionkov531b1092006-01-08 01:05:00 -0800898 * v9fs_stat2inode - populate an inode structure with mistat info
899 * @stat: Plan 9 metadata (mistat) structure
Eric Van Hensbergen2bad8472005-09-09 13:04:19 -0700900 * @inode: inode to populate
901 * @sb: superblock of filesystem
902 *
903 */
904
905void
Latchesar Ionkov531b1092006-01-08 01:05:00 -0800906v9fs_stat2inode(struct v9fs_stat *stat, struct inode *inode,
907 struct super_block *sb)
Eric Van Hensbergen2bad8472005-09-09 13:04:19 -0700908{
Latchesar Ionkov1dac06b2006-01-08 01:05:02 -0800909 int n;
Latchesar Ionkov531b1092006-01-08 01:05:00 -0800910 char ext[32];
Eric Van Hensbergen2bad8472005-09-09 13:04:19 -0700911 struct v9fs_session_info *v9ses = sb->s_fs_info;
912
913 inode->i_nlink = 1;
914
Latchesar Ionkov531b1092006-01-08 01:05:00 -0800915 inode->i_atime.tv_sec = stat->atime;
916 inode->i_mtime.tv_sec = stat->mtime;
917 inode->i_ctime.tv_sec = stat->mtime;
Eric Van Hensbergen2bad8472005-09-09 13:04:19 -0700918
Latchesar Ionkov531b1092006-01-08 01:05:00 -0800919 inode->i_uid = v9ses->uid;
920 inode->i_gid = v9ses->gid;
Eric Van Hensbergen2bad8472005-09-09 13:04:19 -0700921
922 if (v9ses->extended) {
Latchesar Ionkov531b1092006-01-08 01:05:00 -0800923 inode->i_uid = stat->n_uid;
924 inode->i_gid = stat->n_gid;
Eric Van Hensbergen2bad8472005-09-09 13:04:19 -0700925 }
926
Latchesar Ionkov531b1092006-01-08 01:05:00 -0800927 inode->i_mode = p9mode2unixmode(v9ses, stat->mode);
Eric Van Hensbergen2bad8472005-09-09 13:04:19 -0700928 if ((S_ISBLK(inode->i_mode)) || (S_ISCHR(inode->i_mode))) {
929 char type = 0;
930 int major = -1;
931 int minor = -1;
Latchesar Ionkov531b1092006-01-08 01:05:00 -0800932
Latchesar Ionkov1dac06b2006-01-08 01:05:02 -0800933 n = stat->extension.len;
934 if (n > sizeof(ext)-1)
935 n = sizeof(ext)-1;
936 memmove(ext, stat->extension.str, n);
937 ext[n] = 0;
Latchesar Ionkov531b1092006-01-08 01:05:00 -0800938 sscanf(ext, "%c %u %u", &type, &major, &minor);
Eric Van Hensbergen2bad8472005-09-09 13:04:19 -0700939 switch (type) {
940 case 'c':
941 inode->i_mode &= ~S_IFBLK;
942 inode->i_mode |= S_IFCHR;
943 break;
944 case 'b':
945 break;
946 default:
Latchesar Ionkov531b1092006-01-08 01:05:00 -0800947 dprintk(DEBUG_ERROR, "Unknown special type %c (%.*s)\n",
948 type, stat->extension.len, stat->extension.str);
Eric Van Hensbergen2bad8472005-09-09 13:04:19 -0700949 };
950 inode->i_rdev = MKDEV(major, minor);
951 } else
952 inode->i_rdev = 0;
953
Latchesar Ionkov531b1092006-01-08 01:05:00 -0800954 inode->i_size = stat->length;
Eric Van Hensbergen2bad8472005-09-09 13:04:19 -0700955
956 inode->i_blksize = sb->s_blocksize;
957 inode->i_blocks =
958 (inode->i_size + inode->i_blksize - 1) >> sb->s_blocksize_bits;
959}
960
961/**
962 * v9fs_qid2ino - convert qid into inode number
963 * @qid: qid to hash
964 *
965 * BUG: potential for inode number collisions?
966 */
967
968ino_t v9fs_qid2ino(struct v9fs_qid *qid)
969{
970 u64 path = qid->path + 2;
971 ino_t i = 0;
972
973 if (sizeof(ino_t) == sizeof(path))
974 memcpy(&i, &path, sizeof(ino_t));
975 else
976 i = (ino_t) (path ^ (path >> 32));
977
978 return i;
979}
980
981/**
Eric Van Hensbergen2bad8472005-09-09 13:04:19 -0700982 * v9fs_readlink - read a symlink's location (internal version)
983 * @dentry: dentry for symlink
Eric Van Hensbergen73c592b2005-09-09 13:04:26 -0700984 * @buffer: buffer to load symlink location into
Eric Van Hensbergen2bad8472005-09-09 13:04:19 -0700985 * @buflen: length of buffer
986 *
987 */
988
989static int v9fs_readlink(struct dentry *dentry, char *buffer, int buflen)
990{
991 int retval = -EPERM;
992
993 struct v9fs_fcall *fcall = NULL;
994 struct v9fs_session_info *v9ses = v9fs_inode2v9ses(dentry->d_inode);
Latchesar Ionkov0b8dd172005-09-27 21:45:24 -0700995 struct v9fs_fid *fid = v9fs_fid_lookup(dentry);
Eric Van Hensbergen2bad8472005-09-09 13:04:19 -0700996
997 if (!fid) {
998 dprintk(DEBUG_ERROR, "could not resolve fid from dentry\n");
999 retval = -EBADF;
1000 goto FreeFcall;
1001 }
1002
1003 if (!v9ses->extended) {
1004 retval = -EBADF;
1005 dprintk(DEBUG_ERROR, "not extended\n");
1006 goto FreeFcall;
1007 }
1008
1009 dprintk(DEBUG_VFS, " %s\n", dentry->d_name.name);
1010 retval = v9fs_t_stat(v9ses, fid->fid, &fcall);
1011
1012 if (retval < 0) {
1013 dprintk(DEBUG_ERROR, "stat error\n");
1014 goto FreeFcall;
1015 }
1016
1017 if (!fcall)
1018 return -EIO;
1019
Latchesar Ionkov531b1092006-01-08 01:05:00 -08001020 if (!(fcall->params.rstat.stat.mode & V9FS_DMSYMLINK)) {
Eric Van Hensbergen2bad8472005-09-09 13:04:19 -07001021 retval = -EINVAL;
1022 goto FreeFcall;
1023 }
1024
1025 /* copy extension buffer into buffer */
Latchesar Ionkov6a3124a2006-03-02 02:54:30 -08001026 if (fcall->params.rstat.stat.extension.len < buflen)
Latchesar Ionkov16cce6d2006-03-25 03:07:26 -08001027 buflen = fcall->params.rstat.stat.extension.len + 1;
Eric Van Hensbergen2bad8472005-09-09 13:04:19 -07001028
Latchesar Ionkov16cce6d2006-03-25 03:07:26 -08001029 memmove(buffer, fcall->params.rstat.stat.extension.str, buflen - 1);
Latchesar Ionkov531b1092006-01-08 01:05:00 -08001030 buffer[buflen-1] = 0;
Eric Van Hensbergen2bad8472005-09-09 13:04:19 -07001031
Latchesar Ionkov16cce6d2006-03-25 03:07:26 -08001032 dprintk(DEBUG_ERROR, "%s -> %.*s (%s)\n", dentry->d_name.name, fcall->params.rstat.stat.extension.len,
1033 fcall->params.rstat.stat.extension.str, buffer);
Eric Van Hensbergen2bad8472005-09-09 13:04:19 -07001034 retval = buflen;
1035
1036 FreeFcall:
1037 kfree(fcall);
1038
1039 return retval;
1040}
1041
1042/**
1043 * v9fs_vfs_readlink - read a symlink's location
1044 * @dentry: dentry for symlink
1045 * @buf: buffer to load symlink location into
1046 * @buflen: length of buffer
1047 *
1048 */
1049
1050static int v9fs_vfs_readlink(struct dentry *dentry, char __user * buffer,
1051 int buflen)
1052{
1053 int retval;
1054 int ret;
1055 char *link = __getname();
1056
Latchesar Ionkova1f9d8d2005-09-22 21:43:52 -07001057 if (buflen > PATH_MAX)
1058 buflen = PATH_MAX;
Eric Van Hensbergen2bad8472005-09-09 13:04:19 -07001059
1060 dprintk(DEBUG_VFS, " dentry: %s (%p)\n", dentry->d_iname, dentry);
1061
1062 retval = v9fs_readlink(dentry, link, buflen);
1063
1064 if (retval > 0) {
1065 if ((ret = copy_to_user(buffer, link, retval)) != 0) {
1066 dprintk(DEBUG_ERROR, "problem copying to user: %d\n",
1067 ret);
1068 retval = ret;
1069 }
1070 }
1071
Davi Arnautce44eeb2005-11-07 00:59:36 -08001072 __putname(link);
Eric Van Hensbergen2bad8472005-09-09 13:04:19 -07001073 return retval;
1074}
1075
1076/**
1077 * v9fs_vfs_follow_link - follow a symlink path
1078 * @dentry: dentry for symlink
1079 * @nd: nameidata
1080 *
1081 */
1082
1083static void *v9fs_vfs_follow_link(struct dentry *dentry, struct nameidata *nd)
1084{
1085 int len = 0;
1086 char *link = __getname();
1087
1088 dprintk(DEBUG_VFS, "%s n", dentry->d_name.name);
1089
1090 if (!link)
1091 link = ERR_PTR(-ENOMEM);
1092 else {
Latchesar Ionkov16cce6d2006-03-25 03:07:26 -08001093 len = v9fs_readlink(dentry, link, PATH_MAX);
Eric Van Hensbergen2bad8472005-09-09 13:04:19 -07001094
1095 if (len < 0) {
Davi Arnautce44eeb2005-11-07 00:59:36 -08001096 __putname(link);
Eric Van Hensbergen2bad8472005-09-09 13:04:19 -07001097 link = ERR_PTR(len);
1098 } else
1099 link[len] = 0;
1100 }
1101 nd_set_link(nd, link);
1102
1103 return NULL;
1104}
1105
1106/**
1107 * v9fs_vfs_put_link - release a symlink path
1108 * @dentry: dentry for symlink
1109 * @nd: nameidata
1110 *
1111 */
1112
1113static void v9fs_vfs_put_link(struct dentry *dentry, struct nameidata *nd, void *p)
1114{
1115 char *s = nd_get_link(nd);
1116
1117 dprintk(DEBUG_VFS, " %s %s\n", dentry->d_name.name, s);
1118 if (!IS_ERR(s))
Davi Arnautce44eeb2005-11-07 00:59:36 -08001119 __putname(s);
Eric Van Hensbergen2bad8472005-09-09 13:04:19 -07001120}
1121
Latchesar Ionkov531b1092006-01-08 01:05:00 -08001122static int v9fs_vfs_mkspecial(struct inode *dir, struct dentry *dentry,
1123 int mode, const char *extension)
1124{
Latchesar Ionkov6a3124a2006-03-02 02:54:30 -08001125 int err;
1126 u32 fid, perm;
Latchesar Ionkov531b1092006-01-08 01:05:00 -08001127 struct v9fs_session_info *v9ses;
Latchesar Ionkov6a3124a2006-03-02 02:54:30 -08001128 struct v9fs_fid *dfid, *vfid;
1129 struct inode *inode;
Latchesar Ionkov531b1092006-01-08 01:05:00 -08001130
Latchesar Ionkov6a3124a2006-03-02 02:54:30 -08001131 inode = NULL;
1132 vfid = NULL;
1133 v9ses = v9fs_inode2v9ses(dir);
1134 dfid = v9fs_fid_lookup(dentry->d_parent);
1135 perm = unixmode2p9mode(v9ses, mode);
Latchesar Ionkov531b1092006-01-08 01:05:00 -08001136
1137 if (!v9ses->extended) {
1138 dprintk(DEBUG_ERROR, "not extended\n");
Latchesar Ionkov6a3124a2006-03-02 02:54:30 -08001139 return -EPERM;
Latchesar Ionkov531b1092006-01-08 01:05:00 -08001140 }
1141
Latchesar Ionkov6a3124a2006-03-02 02:54:30 -08001142 err = v9fs_create(v9ses, dfid->fid, (char *) dentry->d_name.name,
Latchesar Ionkov16cce6d2006-03-25 03:07:26 -08001143 perm, V9FS_OREAD, (char *) extension, &fid, NULL, NULL);
Latchesar Ionkov531b1092006-01-08 01:05:00 -08001144
Latchesar Ionkov6a3124a2006-03-02 02:54:30 -08001145 if (err)
1146 goto error;
1147
1148 err = v9fs_t_clunk(v9ses, fid);
1149 if (err)
1150 goto error;
1151
1152 vfid = v9fs_clone_walk(v9ses, dfid->fid, dentry);
1153 if (IS_ERR(vfid)) {
1154 err = PTR_ERR(vfid);
1155 vfid = NULL;
1156 goto error;
1157 }
1158
1159 inode = v9fs_inode_from_fid(v9ses, vfid->fid, dir->i_sb);
1160 if (IS_ERR(inode)) {
1161 err = PTR_ERR(inode);
1162 inode = NULL;
1163 goto error;
Latchesar Ionkov531b1092006-01-08 01:05:00 -08001164 }
1165
Latchesar Ionkov6a3124a2006-03-02 02:54:30 -08001166 dentry->d_op = &v9fs_dentry_operations;
1167 d_instantiate(dentry, inode);
1168 return 0;
1169
1170error:
Latchesar Ionkov6a3124a2006-03-02 02:54:30 -08001171 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();
Latchesar Ionkov16cce6d2006-03-25 03:07:26 -08001230 sprintf(name, "%d\n", oldfid->fid);
Latchesar Ionkov531b1092006-01-08 01:05:00 -08001231 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();
Eugene Teoc0291a02006-03-25 03:07:27 -08001259 if (!name)
1260 return -ENOMEM;
Eric Van Hensbergen2bad8472005-09-09 13:04:19 -07001261 /* build extension */
1262 if (S_ISBLK(mode))
Latchesar Ionkov531b1092006-01-08 01:05:00 -08001263 sprintf(name, "b %u %u", MAJOR(rdev), MINOR(rdev));
Eric Van Hensbergen2bad8472005-09-09 13:04:19 -07001264 else if (S_ISCHR(mode))
Latchesar Ionkov531b1092006-01-08 01:05:00 -08001265 sprintf(name, "c %u %u", MAJOR(rdev), MINOR(rdev));
Eric Van Hensbergen73c592b2005-09-09 13:04:26 -07001266 else if (S_ISFIFO(mode))
Latchesar Ionkov531b1092006-01-08 01:05:00 -08001267 *name = 0;
Eric Van Hensbergen2bad8472005-09-09 13:04:19 -07001268 else {
Latchesar Ionkov531b1092006-01-08 01:05:00 -08001269 __putname(name);
1270 return -EINVAL;
Eric Van Hensbergen2bad8472005-09-09 13:04:19 -07001271 }
1272
Latchesar Ionkov531b1092006-01-08 01:05:00 -08001273 retval = v9fs_vfs_mkspecial(dir, dentry, mode, name);
1274 __putname(name);
Eric Van Hensbergen2bad8472005-09-09 13:04:19 -07001275
1276 return retval;
1277}
1278
Eric Van Hensbergenb5016112005-09-09 13:04:27 -07001279static struct inode_operations v9fs_dir_inode_operations_ext = {
Eric Van Hensbergen2bad8472005-09-09 13:04:19 -07001280 .create = v9fs_vfs_create,
1281 .lookup = v9fs_vfs_lookup,
1282 .symlink = v9fs_vfs_symlink,
1283 .link = v9fs_vfs_link,
1284 .unlink = v9fs_vfs_unlink,
1285 .mkdir = v9fs_vfs_mkdir,
1286 .rmdir = v9fs_vfs_rmdir,
1287 .mknod = v9fs_vfs_mknod,
1288 .rename = v9fs_vfs_rename,
1289 .readlink = v9fs_vfs_readlink,
1290 .getattr = v9fs_vfs_getattr,
1291 .setattr = v9fs_vfs_setattr,
1292};
1293
Eric Van Hensbergenb5016112005-09-09 13:04:27 -07001294static struct inode_operations v9fs_dir_inode_operations = {
1295 .create = v9fs_vfs_create,
1296 .lookup = v9fs_vfs_lookup,
1297 .unlink = v9fs_vfs_unlink,
1298 .mkdir = v9fs_vfs_mkdir,
1299 .rmdir = v9fs_vfs_rmdir,
1300 .mknod = v9fs_vfs_mknod,
1301 .rename = v9fs_vfs_rename,
1302 .getattr = v9fs_vfs_getattr,
1303 .setattr = v9fs_vfs_setattr,
1304};
1305
Eric Van Hensbergen2bad8472005-09-09 13:04:19 -07001306static struct inode_operations v9fs_file_inode_operations = {
1307 .getattr = v9fs_vfs_getattr,
1308 .setattr = v9fs_vfs_setattr,
1309};
1310
1311static struct inode_operations v9fs_symlink_inode_operations = {
1312 .readlink = v9fs_vfs_readlink,
1313 .follow_link = v9fs_vfs_follow_link,
1314 .put_link = v9fs_vfs_put_link,
1315 .getattr = v9fs_vfs_getattr,
1316 .setattr = v9fs_vfs_setattr,
1317};