blob: 4cbfb714c6e437a91e260e9f9acd19a74dc8dafb [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 Ionkov16cce6d2006-03-25 03:07:26 -0800258v9fs_create(struct v9fs_session_info *v9ses, u32 pfid, char *name, u32 perm,
259 u8 mode, char *extension, 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 Ionkov731805b2006-03-07 21:55:42 -0800268 return -ENOSPC;
Eric Van Hensbergen2bad8472005-09-09 13:04:19 -0700269 }
270
Latchesar Ionkov6a3124a2006-03-02 02:54:30 -0800271 err = v9fs_t_walk(v9ses, pfid, fid, NULL, &fcall);
272 if (err < 0) {
Latchesar Ionkov531b1092006-01-08 01:05:00 -0800273 PRINT_FCALL_ERROR("clone error", fcall);
Latchesar Ionkov16cce6d2006-03-25 03:07:26 -0800274 goto put_fid;
Eric Van Hensbergen2bad8472005-09-09 13:04:19 -0700275 }
Eric Van Hensbergen2bad8472005-09-09 13:04:19 -0700276 kfree(fcall);
277
Latchesar Ionkov16cce6d2006-03-25 03:07:26 -0800278 err = v9fs_t_create(v9ses, fid, name, perm, mode, extension, &fcall);
Latchesar Ionkov6a3124a2006-03-02 02:54:30 -0800279 if (err < 0) {
Latchesar Ionkov531b1092006-01-08 01:05:00 -0800280 PRINT_FCALL_ERROR("create fails", fcall);
Latchesar Ionkov16cce6d2006-03-25 03:07:26 -0800281 goto clunk_fid;
Eric Van Hensbergen2bad8472005-09-09 13:04:19 -0700282 }
283
Latchesar Ionkov6a3124a2006-03-02 02:54:30 -0800284 if (iounit)
285 *iounit = fcall->params.rcreate.iounit;
286
287 if (qid)
288 *qid = fcall->params.rcreate.qid;
289
290 if (fidp)
291 *fidp = fid;
292
Eric Van Hensbergen2bad8472005-09-09 13:04:19 -0700293 kfree(fcall);
Eric Van Hensbergen2bad8472005-09-09 13:04:19 -0700294 return 0;
295
Latchesar Ionkov16cce6d2006-03-25 03:07:26 -0800296clunk_fid:
297 v9fs_t_clunk(v9ses, fid);
298 fid = V9FS_NOFID;
299
300put_fid:
Latchesar Ionkov6a3124a2006-03-02 02:54:30 -0800301 if (fid >= 0)
302 v9fs_put_idpool(fid, &v9ses->fidpool);
303
304 kfree(fcall);
305 return err;
306}
307
308static struct v9fs_fid*
309v9fs_clone_walk(struct v9fs_session_info *v9ses, u32 fid, struct dentry *dentry)
310{
311 int err;
312 u32 nfid;
313 struct v9fs_fid *ret;
314 struct v9fs_fcall *fcall;
315
316 nfid = v9fs_get_idpool(&v9ses->fidpool);
317 if (nfid < 0) {
318 eprintk(KERN_WARNING, "no free fids available\n");
Latchesar Ionkov731805b2006-03-07 21:55:42 -0800319 return ERR_PTR(-ENOSPC);
Latchesar Ionkov6a3124a2006-03-02 02:54:30 -0800320 }
321
322 err = v9fs_t_walk(v9ses, fid, nfid, (char *) dentry->d_name.name,
323 &fcall);
324
325 if (err < 0) {
326 PRINT_FCALL_ERROR("walk error", fcall);
327 v9fs_put_idpool(nfid, &v9ses->fidpool);
328 goto error;
329 }
330
Eric Van Hensbergen2bad8472005-09-09 13:04:19 -0700331 kfree(fcall);
Latchesar Ionkov3cf64292006-01-08 01:04:58 -0800332 fcall = NULL;
Latchesar Ionkov6a3124a2006-03-02 02:54:30 -0800333 ret = v9fs_fid_create(v9ses, nfid);
334 if (!ret) {
335 err = -ENOMEM;
336 goto clunk_fid;
337 }
Eric Van Hensbergen2bad8472005-09-09 13:04:19 -0700338
Latchesar Ionkov6a3124a2006-03-02 02:54:30 -0800339 err = v9fs_fid_insert(ret, dentry);
340 if (err < 0) {
341 v9fs_fid_destroy(ret);
342 goto clunk_fid;
Latchesar Ionkov0b8dd172005-09-27 21:45:24 -0700343 }
Latchesar Ionkov6a3124a2006-03-02 02:54:30 -0800344
345 return ret;
346
347clunk_fid:
348 v9fs_t_clunk(v9ses, nfid);
349
350error:
351 kfree(fcall);
352 return ERR_PTR(err);
353}
354
Latchesar Ionkov5174fda2006-03-25 03:07:25 -0800355static struct inode *
Latchesar Ionkov6a3124a2006-03-02 02:54:30 -0800356v9fs_inode_from_fid(struct v9fs_session_info *v9ses, u32 fid,
357 struct super_block *sb)
358{
359 int err, umode;
360 struct inode *ret;
361 struct v9fs_fcall *fcall;
362
363 ret = NULL;
364 err = v9fs_t_stat(v9ses, fid, &fcall);
365 if (err) {
366 PRINT_FCALL_ERROR("stat error", fcall);
367 goto error;
Eric Van Hensbergen2bad8472005-09-09 13:04:19 -0700368 }
Latchesar Ionkov6a3124a2006-03-02 02:54:30 -0800369
370 umode = p9mode2unixmode(v9ses, fcall->params.rstat.stat.mode);
371 ret = v9fs_get_inode(sb, umode);
372 if (IS_ERR(ret)) {
373 err = PTR_ERR(ret);
374 ret = NULL;
375 goto error;
376 }
377
378 v9fs_stat2inode(&fcall->params.rstat.stat, ret, sb);
379 kfree(fcall);
380 return ret;
381
382error:
383 kfree(fcall);
384 if (ret)
385 iput(ret);
386
387 return ERR_PTR(err);
Eric Van Hensbergen2bad8472005-09-09 13:04:19 -0700388}
389
390/**
391 * v9fs_remove - helper function to remove files and directories
Eric Van Hensbergen73c592b2005-09-09 13:04:26 -0700392 * @dir: directory inode that is being deleted
393 * @file: dentry that is being deleted
394 * @rmdir: removing a directory
Eric Van Hensbergen2bad8472005-09-09 13:04:19 -0700395 *
396 */
397
398static int v9fs_remove(struct inode *dir, struct dentry *file, int rmdir)
399{
400 struct v9fs_fcall *fcall = NULL;
401 struct super_block *sb = NULL;
402 struct v9fs_session_info *v9ses = NULL;
403 struct v9fs_fid *v9fid = NULL;
404 struct inode *file_inode = NULL;
405 int fid = -1;
406 int result = 0;
407
408 dprintk(DEBUG_VFS, "inode: %p dentry: %p rmdir: %d\n", dir, file,
409 rmdir);
410
411 file_inode = file->d_inode;
412 sb = file_inode->i_sb;
413 v9ses = v9fs_inode2v9ses(file_inode);
Latchesar Ionkov0b8dd172005-09-27 21:45:24 -0700414 v9fid = v9fs_fid_lookup(file);
Eric Van Hensbergen2bad8472005-09-09 13:04:19 -0700415
416 if (!v9fid) {
417 dprintk(DEBUG_ERROR,
418 "no v9fs_fid\n");
419 return -EBADF;
420 }
421
422 fid = v9fid->fid;
423 if (fid < 0) {
424 dprintk(DEBUG_ERROR, "inode #%lu, no fid!\n",
425 file_inode->i_ino);
426 return -EBADF;
427 }
428
429 result = v9fs_t_remove(v9ses, fid, &fcall);
Latchesar Ionkov531b1092006-01-08 01:05:00 -0800430 if (result < 0) {
431 PRINT_FCALL_ERROR("remove fails", fcall);
432 } else {
Eric Van Hensbergen2bad8472005-09-09 13:04:19 -0700433 v9fs_put_idpool(fid, &v9ses->fidpool);
434 v9fs_fid_destroy(v9fid);
435 }
436
437 kfree(fcall);
438 return result;
439}
440
Latchesar Ionkov6a3124a2006-03-02 02:54:30 -0800441static int
442v9fs_open_created(struct inode *inode, struct file *file)
443{
444 return 0;
445}
446
Eric Van Hensbergen2bad8472005-09-09 13:04:19 -0700447/**
448 * v9fs_vfs_create - VFS hook to create files
449 * @inode: directory inode that is being deleted
450 * @dentry: dentry that is being deleted
Latchesar Ionkov6a3124a2006-03-02 02:54:30 -0800451 * @mode: create permissions
Eric Van Hensbergen2bad8472005-09-09 13:04:19 -0700452 * @nd: path information
453 *
454 */
455
456static int
Latchesar Ionkov6a3124a2006-03-02 02:54:30 -0800457v9fs_vfs_create(struct inode *dir, struct dentry *dentry, int mode,
Eric Van Hensbergen2bad8472005-09-09 13:04:19 -0700458 struct nameidata *nd)
459{
Latchesar Ionkov6a3124a2006-03-02 02:54:30 -0800460 int err;
461 u32 fid, perm, iounit;
462 int flags;
463 struct v9fs_session_info *v9ses;
464 struct v9fs_fid *dfid, *vfid, *ffid;
465 struct inode *inode;
466 struct v9fs_qid qid;
467 struct file *filp;
468
469 inode = NULL;
470 vfid = NULL;
471 v9ses = v9fs_inode2v9ses(dir);
472 dfid = v9fs_fid_lookup(dentry->d_parent);
473 perm = unixmode2p9mode(v9ses, mode);
474
475 if (nd && nd->flags & LOOKUP_OPEN)
476 flags = nd->intent.open.flags - 1;
477 else
478 flags = O_RDWR;
479
480 err = v9fs_create(v9ses, dfid->fid, (char *) dentry->d_name.name,
Latchesar Ionkov16cce6d2006-03-25 03:07:26 -0800481 perm, v9fs_uflags2omode(flags), NULL, &fid, &qid, &iounit);
Latchesar Ionkov6a3124a2006-03-02 02:54:30 -0800482
483 if (err)
484 goto error;
485
486 vfid = v9fs_clone_walk(v9ses, dfid->fid, dentry);
487 if (IS_ERR(vfid)) {
488 err = PTR_ERR(vfid);
489 vfid = NULL;
490 goto error;
491 }
492
493 inode = v9fs_inode_from_fid(v9ses, vfid->fid, dir->i_sb);
494 if (IS_ERR(inode)) {
495 err = PTR_ERR(inode);
496 inode = NULL;
497 goto error;
498 }
499
500 dentry->d_op = &v9fs_dentry_operations;
501 d_instantiate(dentry, inode);
502
503 if (nd && nd->flags & LOOKUP_OPEN) {
504 ffid = v9fs_fid_create(v9ses, fid);
505 if (!ffid)
506 return -ENOMEM;
507
508 filp = lookup_instantiate_filp(nd, dentry, v9fs_open_created);
509 if (IS_ERR(filp)) {
510 v9fs_fid_destroy(ffid);
511 return PTR_ERR(filp);
512 }
513
514 ffid->rdir_pos = 0;
515 ffid->rdir_fcall = NULL;
516 ffid->fidopen = 1;
517 ffid->iounit = iounit;
518 ffid->filp = filp;
519 filp->private_data = ffid;
520 }
521
522 return 0;
523
524error:
525 if (vfid)
526 v9fs_fid_destroy(vfid);
527
528 if (inode)
529 iput(inode);
530
531 return err;
Eric Van Hensbergen2bad8472005-09-09 13:04:19 -0700532}
533
534/**
535 * v9fs_vfs_mkdir - VFS mkdir hook to create a directory
Eric Van Hensbergen73c592b2005-09-09 13:04:26 -0700536 * @inode: inode that is being unlinked
Eric Van Hensbergen2bad8472005-09-09 13:04:19 -0700537 * @dentry: dentry that is being unlinked
538 * @mode: mode for new directory
539 *
540 */
541
Latchesar Ionkov6a3124a2006-03-02 02:54:30 -0800542static int v9fs_vfs_mkdir(struct inode *dir, struct dentry *dentry, int mode)
Eric Van Hensbergen2bad8472005-09-09 13:04:19 -0700543{
Latchesar Ionkov6a3124a2006-03-02 02:54:30 -0800544 int err;
545 u32 fid, perm;
546 struct v9fs_session_info *v9ses;
547 struct v9fs_fid *dfid, *vfid;
548 struct inode *inode;
549
550 inode = NULL;
551 vfid = NULL;
552 v9ses = v9fs_inode2v9ses(dir);
553 dfid = v9fs_fid_lookup(dentry->d_parent);
554 perm = unixmode2p9mode(v9ses, mode | S_IFDIR);
555
556 err = v9fs_create(v9ses, dfid->fid, (char *) dentry->d_name.name,
Latchesar Ionkov16cce6d2006-03-25 03:07:26 -0800557 perm, V9FS_OREAD, NULL, &fid, NULL, NULL);
Latchesar Ionkov6a3124a2006-03-02 02:54:30 -0800558
559 if (err) {
560 dprintk(DEBUG_ERROR, "create error %d\n", err);
561 goto error;
562 }
563
564 err = v9fs_t_clunk(v9ses, fid);
565 if (err) {
566 dprintk(DEBUG_ERROR, "clunk error %d\n", err);
567 goto error;
568 }
569
570 vfid = v9fs_clone_walk(v9ses, dfid->fid, dentry);
571 if (IS_ERR(vfid)) {
572 err = PTR_ERR(vfid);
573 vfid = NULL;
574 goto error;
575 }
576
577 inode = v9fs_inode_from_fid(v9ses, vfid->fid, dir->i_sb);
578 if (IS_ERR(inode)) {
579 err = PTR_ERR(inode);
580 inode = NULL;
581 goto error;
582 }
583
584 dentry->d_op = &v9fs_dentry_operations;
585 d_instantiate(dentry, inode);
586 return 0;
587
588error:
589 if (vfid)
590 v9fs_fid_destroy(vfid);
591
592 return err;
Eric Van Hensbergen2bad8472005-09-09 13:04:19 -0700593}
594
595/**
596 * v9fs_vfs_lookup - VFS lookup hook to "walk" to a new inode
597 * @dir: inode that is being walked from
598 * @dentry: dentry that is being walked to?
599 * @nameidata: path data
600 *
601 */
602
603static struct dentry *v9fs_vfs_lookup(struct inode *dir, struct dentry *dentry,
604 struct nameidata *nameidata)
605{
606 struct super_block *sb;
607 struct v9fs_session_info *v9ses;
608 struct v9fs_fid *dirfid;
609 struct v9fs_fid *fid;
610 struct inode *inode;
611 struct v9fs_fcall *fcall = NULL;
Eric Van Hensbergen2bad8472005-09-09 13:04:19 -0700612 int dirfidnum = -1;
613 int newfid = -1;
614 int result = 0;
615
616 dprintk(DEBUG_VFS, "dir: %p dentry: (%s) %p nameidata: %p\n",
Latchesar Ionkov731805b2006-03-07 21:55:42 -0800617 dir, dentry->d_name.name, dentry, nameidata);
Eric Van Hensbergen2bad8472005-09-09 13:04:19 -0700618
619 sb = dir->i_sb;
620 v9ses = v9fs_inode2v9ses(dir);
Latchesar Ionkov5e7a99a2006-03-22 00:07:37 -0800621 dentry->d_op = &v9fs_dentry_operations;
Latchesar Ionkov0b8dd172005-09-27 21:45:24 -0700622 dirfid = v9fs_fid_lookup(dentry->d_parent);
Eric Van Hensbergen2bad8472005-09-09 13:04:19 -0700623
624 if (!dirfid) {
625 dprintk(DEBUG_ERROR, "no dirfid\n");
626 return ERR_PTR(-EINVAL);
627 }
628
629 dirfidnum = dirfid->fid;
630
631 if (dirfidnum < 0) {
632 dprintk(DEBUG_ERROR, "no dirfid for inode %p, #%lu\n",
633 dir, dir->i_ino);
634 return ERR_PTR(-EBADF);
635 }
636
637 newfid = v9fs_get_idpool(&v9ses->fidpool);
638 if (newfid < 0) {
639 eprintk(KERN_WARNING, "newfid fails!\n");
640 return ERR_PTR(-ENOSPC);
641 }
642
Latchesar Ionkov6a3124a2006-03-02 02:54:30 -0800643 result = v9fs_t_walk(v9ses, dirfidnum, newfid,
644 (char *)dentry->d_name.name, NULL);
Eric Van Hensbergen2bad8472005-09-09 13:04:19 -0700645 if (result < 0) {
646 v9fs_put_idpool(newfid, &v9ses->fidpool);
647 if (result == -ENOENT) {
648 d_add(dentry, NULL);
Latchesar Ionkov0b8dd172005-09-27 21:45:24 -0700649 dprintk(DEBUG_VFS,
Eric Van Hensbergen2bad8472005-09-09 13:04:19 -0700650 "Return negative dentry %p count %d\n",
651 dentry, atomic_read(&dentry->d_count));
652 return NULL;
653 }
654 dprintk(DEBUG_ERROR, "walk error:%d\n", result);
655 goto FreeFcall;
656 }
657
658 result = v9fs_t_stat(v9ses, newfid, &fcall);
659 if (result < 0) {
660 dprintk(DEBUG_ERROR, "stat error\n");
661 goto FreeFcall;
662 }
663
Latchesar Ionkov531b1092006-01-08 01:05:00 -0800664 inode = v9fs_get_inode(sb, p9mode2unixmode(v9ses,
665 fcall->params.rstat.stat.mode));
Eric Van Hensbergen2bad8472005-09-09 13:04:19 -0700666
667 if (IS_ERR(inode) && (PTR_ERR(inode) == -ENOSPC)) {
668 eprintk(KERN_WARNING, "inode alloc failes, returns %ld\n",
669 PTR_ERR(inode));
670
671 result = -ENOSPC;
672 goto FreeFcall;
673 }
674
Latchesar Ionkov531b1092006-01-08 01:05:00 -0800675 inode->i_ino = v9fs_qid2ino(&fcall->params.rstat.stat.qid);
Eric Van Hensbergen2bad8472005-09-09 13:04:19 -0700676
Latchesar Ionkov6a3124a2006-03-02 02:54:30 -0800677 fid = v9fs_fid_create(v9ses, newfid);
Eric Van Hensbergen2bad8472005-09-09 13:04:19 -0700678 if (fid == NULL) {
679 dprintk(DEBUG_ERROR, "couldn't insert\n");
680 result = -ENOMEM;
681 goto FreeFcall;
682 }
683
Latchesar Ionkov6a3124a2006-03-02 02:54:30 -0800684 result = v9fs_fid_insert(fid, dentry);
685 if (result < 0)
686 goto FreeFcall;
687
Latchesar Ionkov531b1092006-01-08 01:05:00 -0800688 fid->qid = fcall->params.rstat.stat.qid;
Latchesar Ionkov531b1092006-01-08 01:05:00 -0800689 v9fs_stat2inode(&fcall->params.rstat.stat, inode, inode->i_sb);
Eric Van Hensbergen2bad8472005-09-09 13:04:19 -0700690
691 d_add(dentry, inode);
692 kfree(fcall);
693
694 return NULL;
695
696 FreeFcall:
697 kfree(fcall);
698 return ERR_PTR(result);
699}
700
701/**
702 * v9fs_vfs_unlink - VFS unlink hook to delete an inode
703 * @i: inode that is being unlinked
Eric Van Hensbergen73c592b2005-09-09 13:04:26 -0700704 * @d: dentry that is being unlinked
Eric Van Hensbergen2bad8472005-09-09 13:04:19 -0700705 *
706 */
707
708static int v9fs_vfs_unlink(struct inode *i, struct dentry *d)
709{
710 return v9fs_remove(i, d, 0);
711}
712
713/**
714 * v9fs_vfs_rmdir - VFS unlink hook to delete a directory
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_rmdir(struct inode *i, struct dentry *d)
721{
722 return v9fs_remove(i, d, 1);
723}
724
725/**
726 * v9fs_vfs_rename - VFS hook to rename an inode
727 * @old_dir: old dir inode
728 * @old_dentry: old dentry
729 * @new_dir: new dir inode
730 * @new_dentry: new dentry
731 *
732 */
733
734static int
735v9fs_vfs_rename(struct inode *old_dir, struct dentry *old_dentry,
736 struct inode *new_dir, struct dentry *new_dentry)
737{
738 struct inode *old_inode = old_dentry->d_inode;
739 struct v9fs_session_info *v9ses = v9fs_inode2v9ses(old_inode);
Latchesar Ionkov0b8dd172005-09-27 21:45:24 -0700740 struct v9fs_fid *oldfid = v9fs_fid_lookup(old_dentry);
Eric Van Hensbergen2bad8472005-09-09 13:04:19 -0700741 struct v9fs_fid *olddirfid =
Latchesar Ionkov0b8dd172005-09-27 21:45:24 -0700742 v9fs_fid_lookup(old_dentry->d_parent);
Eric Van Hensbergen2bad8472005-09-09 13:04:19 -0700743 struct v9fs_fid *newdirfid =
Latchesar Ionkov0b8dd172005-09-27 21:45:24 -0700744 v9fs_fid_lookup(new_dentry->d_parent);
Latchesar Ionkov531b1092006-01-08 01:05:00 -0800745 struct v9fs_wstat wstat;
Eric Van Hensbergen2bad8472005-09-09 13:04:19 -0700746 struct v9fs_fcall *fcall = NULL;
747 int fid = -1;
748 int olddirfidnum = -1;
749 int newdirfidnum = -1;
750 int retval = 0;
751
752 dprintk(DEBUG_VFS, "\n");
753
754 if ((!oldfid) || (!olddirfid) || (!newdirfid)) {
755 dprintk(DEBUG_ERROR, "problem with arguments\n");
756 return -EBADF;
757 }
758
759 /* 9P can only handle file rename in the same directory */
760 if (memcmp(&olddirfid->qid, &newdirfid->qid, sizeof(newdirfid->qid))) {
761 dprintk(DEBUG_ERROR, "old dir and new dir are different\n");
762 retval = -EPERM;
763 goto FreeFcallnBail;
764 }
765
766 fid = oldfid->fid;
767 olddirfidnum = olddirfid->fid;
768 newdirfidnum = newdirfid->fid;
769
770 if (fid < 0) {
771 dprintk(DEBUG_ERROR, "no fid for old file #%lu\n",
772 old_inode->i_ino);
773 retval = -EBADF;
774 goto FreeFcallnBail;
775 }
776
Latchesar Ionkov531b1092006-01-08 01:05:00 -0800777 v9fs_blank_wstat(&wstat);
778 wstat.muid = v9ses->name;
779 wstat.name = (char *) new_dentry->d_name.name;
Eric Van Hensbergen2bad8472005-09-09 13:04:19 -0700780
Latchesar Ionkov531b1092006-01-08 01:05:00 -0800781 retval = v9fs_t_wstat(v9ses, fid, &wstat, &fcall);
Eric Van Hensbergen2bad8472005-09-09 13:04:19 -0700782
783 FreeFcallnBail:
Eric Van Hensbergen2bad8472005-09-09 13:04:19 -0700784 if (retval < 0)
Latchesar Ionkov531b1092006-01-08 01:05:00 -0800785 PRINT_FCALL_ERROR("wstat error", fcall);
Eric Van Hensbergen2bad8472005-09-09 13:04:19 -0700786
787 kfree(fcall);
788 return retval;
789}
790
791/**
Adrian Bunk943ffb52006-01-10 00:10:13 +0100792 * v9fs_vfs_getattr - retrieve file metadata
Eric Van Hensbergen2bad8472005-09-09 13:04:19 -0700793 * @mnt - mount information
794 * @dentry - file to get attributes on
795 * @stat - metadata structure to populate
796 *
797 */
798
799static int
800v9fs_vfs_getattr(struct vfsmount *mnt, struct dentry *dentry,
801 struct kstat *stat)
802{
803 struct v9fs_fcall *fcall = NULL;
804 struct v9fs_session_info *v9ses = v9fs_inode2v9ses(dentry->d_inode);
Latchesar Ionkov0b8dd172005-09-27 21:45:24 -0700805 struct v9fs_fid *fid = v9fs_fid_lookup(dentry);
Eric Van Hensbergen2bad8472005-09-09 13:04:19 -0700806 int err = -EPERM;
807
808 dprintk(DEBUG_VFS, "dentry: %p\n", dentry);
809 if (!fid) {
810 dprintk(DEBUG_ERROR,
811 "couldn't find fid associated with dentry\n");
812 return -EBADF;
813 }
814
815 err = v9fs_t_stat(v9ses, fid->fid, &fcall);
816
817 if (err < 0)
818 dprintk(DEBUG_ERROR, "stat error\n");
819 else {
Latchesar Ionkov531b1092006-01-08 01:05:00 -0800820 v9fs_stat2inode(&fcall->params.rstat.stat, dentry->d_inode,
Eric Van Hensbergen2bad8472005-09-09 13:04:19 -0700821 dentry->d_inode->i_sb);
822 generic_fillattr(dentry->d_inode, stat);
823 }
824
825 kfree(fcall);
826 return err;
827}
828
829/**
830 * v9fs_vfs_setattr - set file metadata
831 * @dentry: file whose metadata to set
832 * @iattr: metadata assignment structure
833 *
834 */
835
836static int v9fs_vfs_setattr(struct dentry *dentry, struct iattr *iattr)
837{
838 struct v9fs_session_info *v9ses = v9fs_inode2v9ses(dentry->d_inode);
Latchesar Ionkov0b8dd172005-09-27 21:45:24 -0700839 struct v9fs_fid *fid = v9fs_fid_lookup(dentry);
Eric Van Hensbergen2bad8472005-09-09 13:04:19 -0700840 struct v9fs_fcall *fcall = NULL;
Latchesar Ionkov531b1092006-01-08 01:05:00 -0800841 struct v9fs_wstat wstat;
Eric Van Hensbergen2bad8472005-09-09 13:04:19 -0700842 int res = -EPERM;
843
844 dprintk(DEBUG_VFS, "\n");
Eric Van Hensbergen73c592b2005-09-09 13:04:26 -0700845
Eric Van Hensbergen2bad8472005-09-09 13:04:19 -0700846 if (!fid) {
847 dprintk(DEBUG_ERROR,
848 "Couldn't find fid associated with dentry\n");
849 return -EBADF;
850 }
851
Latchesar Ionkov531b1092006-01-08 01:05:00 -0800852 v9fs_blank_wstat(&wstat);
Eric Van Hensbergen2bad8472005-09-09 13:04:19 -0700853 if (iattr->ia_valid & ATTR_MODE)
Latchesar Ionkov531b1092006-01-08 01:05:00 -0800854 wstat.mode = unixmode2p9mode(v9ses, iattr->ia_mode);
Eric Van Hensbergen2bad8472005-09-09 13:04:19 -0700855
856 if (iattr->ia_valid & ATTR_MTIME)
Latchesar Ionkov531b1092006-01-08 01:05:00 -0800857 wstat.mtime = iattr->ia_mtime.tv_sec;
Eric Van Hensbergen2bad8472005-09-09 13:04:19 -0700858
859 if (iattr->ia_valid & ATTR_ATIME)
Latchesar Ionkov531b1092006-01-08 01:05:00 -0800860 wstat.atime = iattr->ia_atime.tv_sec;
Eric Van Hensbergen2bad8472005-09-09 13:04:19 -0700861
862 if (iattr->ia_valid & ATTR_SIZE)
Latchesar Ionkov531b1092006-01-08 01:05:00 -0800863 wstat.length = iattr->ia_size;
Eric Van Hensbergen2bad8472005-09-09 13:04:19 -0700864
865 if (v9ses->extended) {
Latchesar Ionkov531b1092006-01-08 01:05:00 -0800866 if (iattr->ia_valid & ATTR_UID)
867 wstat.n_uid = iattr->ia_uid;
Eric Van Hensbergen2bad8472005-09-09 13:04:19 -0700868
Latchesar Ionkov531b1092006-01-08 01:05:00 -0800869 if (iattr->ia_valid & ATTR_GID)
870 wstat.n_gid = iattr->ia_gid;
Eric Van Hensbergen2bad8472005-09-09 13:04:19 -0700871 }
872
Latchesar Ionkov531b1092006-01-08 01:05:00 -0800873 res = v9fs_t_wstat(v9ses, fid->fid, &wstat, &fcall);
Eric Van Hensbergen2bad8472005-09-09 13:04:19 -0700874
875 if (res < 0)
Latchesar Ionkov531b1092006-01-08 01:05:00 -0800876 PRINT_FCALL_ERROR("wstat error", fcall);
Eric Van Hensbergen2bad8472005-09-09 13:04:19 -0700877
Eric Van Hensbergen2bad8472005-09-09 13:04:19 -0700878 kfree(fcall);
Eric Van Hensbergen2bad8472005-09-09 13:04:19 -0700879 if (res >= 0)
880 res = inode_setattr(dentry->d_inode, iattr);
881
882 return res;
883}
884
885/**
Latchesar Ionkov531b1092006-01-08 01:05:00 -0800886 * v9fs_stat2inode - populate an inode structure with mistat info
887 * @stat: Plan 9 metadata (mistat) structure
Eric Van Hensbergen2bad8472005-09-09 13:04:19 -0700888 * @inode: inode to populate
889 * @sb: superblock of filesystem
890 *
891 */
892
893void
Latchesar Ionkov531b1092006-01-08 01:05:00 -0800894v9fs_stat2inode(struct v9fs_stat *stat, struct inode *inode,
895 struct super_block *sb)
Eric Van Hensbergen2bad8472005-09-09 13:04:19 -0700896{
Latchesar Ionkov1dac06b2006-01-08 01:05:02 -0800897 int n;
Latchesar Ionkov531b1092006-01-08 01:05:00 -0800898 char ext[32];
Eric Van Hensbergen2bad8472005-09-09 13:04:19 -0700899 struct v9fs_session_info *v9ses = sb->s_fs_info;
900
901 inode->i_nlink = 1;
902
Latchesar Ionkov531b1092006-01-08 01:05:00 -0800903 inode->i_atime.tv_sec = stat->atime;
904 inode->i_mtime.tv_sec = stat->mtime;
905 inode->i_ctime.tv_sec = stat->mtime;
Eric Van Hensbergen2bad8472005-09-09 13:04:19 -0700906
Latchesar Ionkov531b1092006-01-08 01:05:00 -0800907 inode->i_uid = v9ses->uid;
908 inode->i_gid = v9ses->gid;
Eric Van Hensbergen2bad8472005-09-09 13:04:19 -0700909
910 if (v9ses->extended) {
Latchesar Ionkov531b1092006-01-08 01:05:00 -0800911 inode->i_uid = stat->n_uid;
912 inode->i_gid = stat->n_gid;
Eric Van Hensbergen2bad8472005-09-09 13:04:19 -0700913 }
914
Latchesar Ionkov531b1092006-01-08 01:05:00 -0800915 inode->i_mode = p9mode2unixmode(v9ses, stat->mode);
Eric Van Hensbergen2bad8472005-09-09 13:04:19 -0700916 if ((S_ISBLK(inode->i_mode)) || (S_ISCHR(inode->i_mode))) {
917 char type = 0;
918 int major = -1;
919 int minor = -1;
Latchesar Ionkov531b1092006-01-08 01:05:00 -0800920
Latchesar Ionkov1dac06b2006-01-08 01:05:02 -0800921 n = stat->extension.len;
922 if (n > sizeof(ext)-1)
923 n = sizeof(ext)-1;
924 memmove(ext, stat->extension.str, n);
925 ext[n] = 0;
Latchesar Ionkov531b1092006-01-08 01:05:00 -0800926 sscanf(ext, "%c %u %u", &type, &major, &minor);
Eric Van Hensbergen2bad8472005-09-09 13:04:19 -0700927 switch (type) {
928 case 'c':
929 inode->i_mode &= ~S_IFBLK;
930 inode->i_mode |= S_IFCHR;
931 break;
932 case 'b':
933 break;
934 default:
Latchesar Ionkov531b1092006-01-08 01:05:00 -0800935 dprintk(DEBUG_ERROR, "Unknown special type %c (%.*s)\n",
936 type, stat->extension.len, stat->extension.str);
Eric Van Hensbergen2bad8472005-09-09 13:04:19 -0700937 };
938 inode->i_rdev = MKDEV(major, minor);
939 } else
940 inode->i_rdev = 0;
941
Latchesar Ionkov531b1092006-01-08 01:05:00 -0800942 inode->i_size = stat->length;
Eric Van Hensbergen2bad8472005-09-09 13:04:19 -0700943
944 inode->i_blksize = sb->s_blocksize;
945 inode->i_blocks =
946 (inode->i_size + inode->i_blksize - 1) >> sb->s_blocksize_bits;
947}
948
949/**
950 * v9fs_qid2ino - convert qid into inode number
951 * @qid: qid to hash
952 *
953 * BUG: potential for inode number collisions?
954 */
955
956ino_t v9fs_qid2ino(struct v9fs_qid *qid)
957{
958 u64 path = qid->path + 2;
959 ino_t i = 0;
960
961 if (sizeof(ino_t) == sizeof(path))
962 memcpy(&i, &path, sizeof(ino_t));
963 else
964 i = (ino_t) (path ^ (path >> 32));
965
966 return i;
967}
968
969/**
Eric Van Hensbergen2bad8472005-09-09 13:04:19 -0700970 * v9fs_readlink - read a symlink's location (internal version)
971 * @dentry: dentry for symlink
Eric Van Hensbergen73c592b2005-09-09 13:04:26 -0700972 * @buffer: buffer to load symlink location into
Eric Van Hensbergen2bad8472005-09-09 13:04:19 -0700973 * @buflen: length of buffer
974 *
975 */
976
977static int v9fs_readlink(struct dentry *dentry, char *buffer, int buflen)
978{
979 int retval = -EPERM;
980
981 struct v9fs_fcall *fcall = NULL;
982 struct v9fs_session_info *v9ses = v9fs_inode2v9ses(dentry->d_inode);
Latchesar Ionkov0b8dd172005-09-27 21:45:24 -0700983 struct v9fs_fid *fid = v9fs_fid_lookup(dentry);
Eric Van Hensbergen2bad8472005-09-09 13:04:19 -0700984
985 if (!fid) {
986 dprintk(DEBUG_ERROR, "could not resolve fid from dentry\n");
987 retval = -EBADF;
988 goto FreeFcall;
989 }
990
991 if (!v9ses->extended) {
992 retval = -EBADF;
993 dprintk(DEBUG_ERROR, "not extended\n");
994 goto FreeFcall;
995 }
996
997 dprintk(DEBUG_VFS, " %s\n", dentry->d_name.name);
998 retval = v9fs_t_stat(v9ses, fid->fid, &fcall);
999
1000 if (retval < 0) {
1001 dprintk(DEBUG_ERROR, "stat error\n");
1002 goto FreeFcall;
1003 }
1004
1005 if (!fcall)
1006 return -EIO;
1007
Latchesar Ionkov531b1092006-01-08 01:05:00 -08001008 if (!(fcall->params.rstat.stat.mode & V9FS_DMSYMLINK)) {
Eric Van Hensbergen2bad8472005-09-09 13:04:19 -07001009 retval = -EINVAL;
1010 goto FreeFcall;
1011 }
1012
1013 /* copy extension buffer into buffer */
Latchesar Ionkov6a3124a2006-03-02 02:54:30 -08001014 if (fcall->params.rstat.stat.extension.len < buflen)
Latchesar Ionkov16cce6d2006-03-25 03:07:26 -08001015 buflen = fcall->params.rstat.stat.extension.len + 1;
Eric Van Hensbergen2bad8472005-09-09 13:04:19 -07001016
Latchesar Ionkov16cce6d2006-03-25 03:07:26 -08001017 memmove(buffer, fcall->params.rstat.stat.extension.str, buflen - 1);
Latchesar Ionkov531b1092006-01-08 01:05:00 -08001018 buffer[buflen-1] = 0;
Eric Van Hensbergen2bad8472005-09-09 13:04:19 -07001019
Latchesar Ionkov16cce6d2006-03-25 03:07:26 -08001020 dprintk(DEBUG_ERROR, "%s -> %.*s (%s)\n", dentry->d_name.name, fcall->params.rstat.stat.extension.len,
1021 fcall->params.rstat.stat.extension.str, buffer);
Eric Van Hensbergen2bad8472005-09-09 13:04:19 -07001022 retval = buflen;
1023
1024 FreeFcall:
1025 kfree(fcall);
1026
1027 return retval;
1028}
1029
1030/**
1031 * v9fs_vfs_readlink - read a symlink's location
1032 * @dentry: dentry for symlink
1033 * @buf: buffer to load symlink location into
1034 * @buflen: length of buffer
1035 *
1036 */
1037
1038static int v9fs_vfs_readlink(struct dentry *dentry, char __user * buffer,
1039 int buflen)
1040{
1041 int retval;
1042 int ret;
1043 char *link = __getname();
1044
Latchesar Ionkova1f9d8d2005-09-22 21:43:52 -07001045 if (buflen > PATH_MAX)
1046 buflen = PATH_MAX;
Eric Van Hensbergen2bad8472005-09-09 13:04:19 -07001047
1048 dprintk(DEBUG_VFS, " dentry: %s (%p)\n", dentry->d_iname, dentry);
1049
1050 retval = v9fs_readlink(dentry, link, buflen);
1051
1052 if (retval > 0) {
1053 if ((ret = copy_to_user(buffer, link, retval)) != 0) {
1054 dprintk(DEBUG_ERROR, "problem copying to user: %d\n",
1055 ret);
1056 retval = ret;
1057 }
1058 }
1059
Davi Arnautce44eeb2005-11-07 00:59:36 -08001060 __putname(link);
Eric Van Hensbergen2bad8472005-09-09 13:04:19 -07001061 return retval;
1062}
1063
1064/**
1065 * v9fs_vfs_follow_link - follow a symlink path
1066 * @dentry: dentry for symlink
1067 * @nd: nameidata
1068 *
1069 */
1070
1071static void *v9fs_vfs_follow_link(struct dentry *dentry, struct nameidata *nd)
1072{
1073 int len = 0;
1074 char *link = __getname();
1075
1076 dprintk(DEBUG_VFS, "%s n", dentry->d_name.name);
1077
1078 if (!link)
1079 link = ERR_PTR(-ENOMEM);
1080 else {
Latchesar Ionkov16cce6d2006-03-25 03:07:26 -08001081 len = v9fs_readlink(dentry, link, PATH_MAX);
Eric Van Hensbergen2bad8472005-09-09 13:04:19 -07001082
1083 if (len < 0) {
Davi Arnautce44eeb2005-11-07 00:59:36 -08001084 __putname(link);
Eric Van Hensbergen2bad8472005-09-09 13:04:19 -07001085 link = ERR_PTR(len);
1086 } else
1087 link[len] = 0;
1088 }
1089 nd_set_link(nd, link);
1090
1091 return NULL;
1092}
1093
1094/**
1095 * v9fs_vfs_put_link - release a symlink path
1096 * @dentry: dentry for symlink
1097 * @nd: nameidata
1098 *
1099 */
1100
1101static void v9fs_vfs_put_link(struct dentry *dentry, struct nameidata *nd, void *p)
1102{
1103 char *s = nd_get_link(nd);
1104
1105 dprintk(DEBUG_VFS, " %s %s\n", dentry->d_name.name, s);
1106 if (!IS_ERR(s))
Davi Arnautce44eeb2005-11-07 00:59:36 -08001107 __putname(s);
Eric Van Hensbergen2bad8472005-09-09 13:04:19 -07001108}
1109
Latchesar Ionkov531b1092006-01-08 01:05:00 -08001110static int v9fs_vfs_mkspecial(struct inode *dir, struct dentry *dentry,
1111 int mode, const char *extension)
1112{
Latchesar Ionkov6a3124a2006-03-02 02:54:30 -08001113 int err;
1114 u32 fid, perm;
Latchesar Ionkov531b1092006-01-08 01:05:00 -08001115 struct v9fs_session_info *v9ses;
Latchesar Ionkov6a3124a2006-03-02 02:54:30 -08001116 struct v9fs_fid *dfid, *vfid;
1117 struct inode *inode;
Latchesar Ionkov531b1092006-01-08 01:05:00 -08001118
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,
Latchesar Ionkov16cce6d2006-03-25 03:07:26 -08001131 perm, V9FS_OREAD, (char *) extension, &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
Latchesar Ionkov6a3124a2006-03-02 02:54:30 -08001154 dentry->d_op = &v9fs_dentry_operations;
1155 d_instantiate(dentry, inode);
1156 return 0;
1157
1158error:
Latchesar Ionkov6a3124a2006-03-02 02:54:30 -08001159 if (vfid)
1160 v9fs_fid_destroy(vfid);
1161
1162 if (inode)
1163 iput(inode);
1164
1165 return err;
1166
Latchesar Ionkov531b1092006-01-08 01:05:00 -08001167}
1168
1169/**
1170 * v9fs_vfs_symlink - helper function to create symlinks
1171 * @dir: directory inode containing symlink
1172 * @dentry: dentry for symlink
1173 * @symname: symlink data
1174 *
1175 * See 9P2000.u RFC for more information
1176 *
1177 */
1178
1179static int
1180v9fs_vfs_symlink(struct inode *dir, struct dentry *dentry, const char *symname)
1181{
1182 dprintk(DEBUG_VFS, " %lu,%s,%s\n", dir->i_ino, dentry->d_name.name,
1183 symname);
1184
1185 return v9fs_vfs_mkspecial(dir, dentry, S_IFLNK, symname);
1186}
1187
Eric Van Hensbergen2bad8472005-09-09 13:04:19 -07001188/**
1189 * v9fs_vfs_link - create a hardlink
1190 * @old_dentry: dentry for file to link to
1191 * @dir: inode destination for new link
Eric Van Hensbergen73c592b2005-09-09 13:04:26 -07001192 * @dentry: dentry for link
Eric Van Hensbergen2bad8472005-09-09 13:04:19 -07001193 *
1194 */
1195
1196/* XXX - lots of code dup'd from symlink and creates,
1197 * figure out a better reuse strategy
1198 */
1199
1200static int
1201v9fs_vfs_link(struct dentry *old_dentry, struct inode *dir,
1202 struct dentry *dentry)
1203{
Latchesar Ionkov531b1092006-01-08 01:05:00 -08001204 int retval;
1205 struct v9fs_fid *oldfid;
1206 char *name;
Eric Van Hensbergen2bad8472005-09-09 13:04:19 -07001207
1208 dprintk(DEBUG_VFS, " %lu,%s,%s\n", dir->i_ino, dentry->d_name.name,
1209 old_dentry->d_name.name);
1210
Latchesar Ionkov531b1092006-01-08 01:05:00 -08001211 oldfid = v9fs_fid_lookup(old_dentry);
1212 if (!oldfid) {
1213 dprintk(DEBUG_ERROR, "can't find oldfid\n");
1214 return -EPERM;
Eric Van Hensbergen2bad8472005-09-09 13:04:19 -07001215 }
1216
Latchesar Ionkov531b1092006-01-08 01:05:00 -08001217 name = __getname();
Latchesar Ionkov16cce6d2006-03-25 03:07:26 -08001218 sprintf(name, "%d\n", oldfid->fid);
Latchesar Ionkov531b1092006-01-08 01:05:00 -08001219 retval = v9fs_vfs_mkspecial(dir, dentry, V9FS_DMLINK, name);
1220 __putname(name);
Eric Van Hensbergen2bad8472005-09-09 13:04:19 -07001221
Eric Van Hensbergen2bad8472005-09-09 13:04:19 -07001222 return retval;
1223}
1224
1225/**
1226 * v9fs_vfs_mknod - create a special file
1227 * @dir: inode destination for new link
1228 * @dentry: dentry for file
1229 * @mode: mode for creation
1230 * @dev_t: device associated with special file
1231 *
1232 */
1233
1234static int
1235v9fs_vfs_mknod(struct inode *dir, struct dentry *dentry, int mode, dev_t rdev)
1236{
Latchesar Ionkov531b1092006-01-08 01:05:00 -08001237 int retval;
1238 char *name;
Eric Van Hensbergen2bad8472005-09-09 13:04:19 -07001239
1240 dprintk(DEBUG_VFS, " %lu,%s mode: %x MAJOR: %u MINOR: %u\n", dir->i_ino,
1241 dentry->d_name.name, mode, MAJOR(rdev), MINOR(rdev));
1242
Latchesar Ionkov531b1092006-01-08 01:05:00 -08001243 if (!new_valid_dev(rdev))
1244 return -EINVAL;
Eric Van Hensbergen73c592b2005-09-09 13:04:26 -07001245
Latchesar Ionkov531b1092006-01-08 01:05:00 -08001246 name = __getname();
Eugene Teoc0291a02006-03-25 03:07:27 -08001247 if (!name)
1248 return -ENOMEM;
Eric Van Hensbergen2bad8472005-09-09 13:04:19 -07001249 /* build extension */
1250 if (S_ISBLK(mode))
Latchesar Ionkov531b1092006-01-08 01:05:00 -08001251 sprintf(name, "b %u %u", MAJOR(rdev), MINOR(rdev));
Eric Van Hensbergen2bad8472005-09-09 13:04:19 -07001252 else if (S_ISCHR(mode))
Latchesar Ionkov531b1092006-01-08 01:05:00 -08001253 sprintf(name, "c %u %u", MAJOR(rdev), MINOR(rdev));
Eric Van Hensbergen73c592b2005-09-09 13:04:26 -07001254 else if (S_ISFIFO(mode))
Latchesar Ionkov531b1092006-01-08 01:05:00 -08001255 *name = 0;
Eric Van Hensbergen2bad8472005-09-09 13:04:19 -07001256 else {
Latchesar Ionkov531b1092006-01-08 01:05:00 -08001257 __putname(name);
1258 return -EINVAL;
Eric Van Hensbergen2bad8472005-09-09 13:04:19 -07001259 }
1260
Latchesar Ionkov531b1092006-01-08 01:05:00 -08001261 retval = v9fs_vfs_mkspecial(dir, dentry, mode, name);
1262 __putname(name);
Eric Van Hensbergen2bad8472005-09-09 13:04:19 -07001263
1264 return retval;
1265}
1266
Eric Van Hensbergenb5016112005-09-09 13:04:27 -07001267static struct inode_operations v9fs_dir_inode_operations_ext = {
Eric Van Hensbergen2bad8472005-09-09 13:04:19 -07001268 .create = v9fs_vfs_create,
1269 .lookup = v9fs_vfs_lookup,
1270 .symlink = v9fs_vfs_symlink,
1271 .link = v9fs_vfs_link,
1272 .unlink = v9fs_vfs_unlink,
1273 .mkdir = v9fs_vfs_mkdir,
1274 .rmdir = v9fs_vfs_rmdir,
1275 .mknod = v9fs_vfs_mknod,
1276 .rename = v9fs_vfs_rename,
1277 .readlink = v9fs_vfs_readlink,
1278 .getattr = v9fs_vfs_getattr,
1279 .setattr = v9fs_vfs_setattr,
1280};
1281
Eric Van Hensbergenb5016112005-09-09 13:04:27 -07001282static struct inode_operations v9fs_dir_inode_operations = {
1283 .create = v9fs_vfs_create,
1284 .lookup = v9fs_vfs_lookup,
1285 .unlink = v9fs_vfs_unlink,
1286 .mkdir = v9fs_vfs_mkdir,
1287 .rmdir = v9fs_vfs_rmdir,
1288 .mknod = v9fs_vfs_mknod,
1289 .rename = v9fs_vfs_rename,
1290 .getattr = v9fs_vfs_getattr,
1291 .setattr = v9fs_vfs_setattr,
1292};
1293
Eric Van Hensbergen2bad8472005-09-09 13:04:19 -07001294static struct inode_operations v9fs_file_inode_operations = {
1295 .getattr = v9fs_vfs_getattr,
1296 .setattr = v9fs_vfs_setattr,
1297};
1298
1299static struct inode_operations v9fs_symlink_inode_operations = {
1300 .readlink = v9fs_vfs_readlink,
1301 .follow_link = v9fs_vfs_follow_link,
1302 .put_link = v9fs_vfs_put_link,
1303 .getattr = v9fs_vfs_getattr,
1304 .setattr = v9fs_vfs_setattr,
1305};