blob: e46bb5a82e1aff4a38d39bddf84b5a6cc5d9e2b6 [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 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 Ionkov6a3124a2006-03-02 02:54:30 -0800274 goto error;
Eric Van Hensbergen2bad8472005-09-09 13:04:19 -0700275 }
Eric Van Hensbergen2bad8472005-09-09 13:04:19 -0700276 kfree(fcall);
277
Latchesar Ionkov6a3124a2006-03-02 02:54:30 -0800278 err = v9fs_t_create(v9ses, fid, name, perm, mode, &fcall);
279 if (err < 0) {
Latchesar Ionkov531b1092006-01-08 01:05:00 -0800280 PRINT_FCALL_ERROR("create fails", fcall);
Latchesar Ionkov6a3124a2006-03-02 02:54:30 -0800281 goto error;
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 Ionkov6a3124a2006-03-02 02:54:30 -0800296error:
297 if (fid >= 0)
298 v9fs_put_idpool(fid, &v9ses->fidpool);
299
300 kfree(fcall);
301 return err;
302}
303
304static struct v9fs_fid*
305v9fs_clone_walk(struct v9fs_session_info *v9ses, u32 fid, struct dentry *dentry)
306{
307 int err;
308 u32 nfid;
309 struct v9fs_fid *ret;
310 struct v9fs_fcall *fcall;
311
312 nfid = v9fs_get_idpool(&v9ses->fidpool);
313 if (nfid < 0) {
314 eprintk(KERN_WARNING, "no free fids available\n");
Latchesar Ionkov731805b2006-03-07 21:55:42 -0800315 return ERR_PTR(-ENOSPC);
Latchesar Ionkov6a3124a2006-03-02 02:54:30 -0800316 }
317
318 err = v9fs_t_walk(v9ses, fid, nfid, (char *) dentry->d_name.name,
319 &fcall);
320
321 if (err < 0) {
322 PRINT_FCALL_ERROR("walk error", fcall);
323 v9fs_put_idpool(nfid, &v9ses->fidpool);
324 goto error;
325 }
326
Eric Van Hensbergen2bad8472005-09-09 13:04:19 -0700327 kfree(fcall);
Latchesar Ionkov3cf64292006-01-08 01:04:58 -0800328 fcall = NULL;
Latchesar Ionkov6a3124a2006-03-02 02:54:30 -0800329 ret = v9fs_fid_create(v9ses, nfid);
330 if (!ret) {
331 err = -ENOMEM;
332 goto clunk_fid;
333 }
Eric Van Hensbergen2bad8472005-09-09 13:04:19 -0700334
Latchesar Ionkov6a3124a2006-03-02 02:54:30 -0800335 err = v9fs_fid_insert(ret, dentry);
336 if (err < 0) {
337 v9fs_fid_destroy(ret);
338 goto clunk_fid;
Latchesar Ionkov0b8dd172005-09-27 21:45:24 -0700339 }
Latchesar Ionkov6a3124a2006-03-02 02:54:30 -0800340
341 return ret;
342
343clunk_fid:
344 v9fs_t_clunk(v9ses, nfid);
345
346error:
347 kfree(fcall);
348 return ERR_PTR(err);
349}
350
Latchesar Ionkov5174fda2006-03-25 03:07:25 -0800351static struct inode *
Latchesar Ionkov6a3124a2006-03-02 02:54:30 -0800352v9fs_inode_from_fid(struct v9fs_session_info *v9ses, u32 fid,
353 struct super_block *sb)
354{
355 int err, umode;
356 struct inode *ret;
357 struct v9fs_fcall *fcall;
358
359 ret = NULL;
360 err = v9fs_t_stat(v9ses, fid, &fcall);
361 if (err) {
362 PRINT_FCALL_ERROR("stat error", fcall);
363 goto error;
Eric Van Hensbergen2bad8472005-09-09 13:04:19 -0700364 }
Latchesar Ionkov6a3124a2006-03-02 02:54:30 -0800365
366 umode = p9mode2unixmode(v9ses, fcall->params.rstat.stat.mode);
367 ret = v9fs_get_inode(sb, umode);
368 if (IS_ERR(ret)) {
369 err = PTR_ERR(ret);
370 ret = NULL;
371 goto error;
372 }
373
374 v9fs_stat2inode(&fcall->params.rstat.stat, ret, sb);
375 kfree(fcall);
376 return ret;
377
378error:
379 kfree(fcall);
380 if (ret)
381 iput(ret);
382
383 return ERR_PTR(err);
Eric Van Hensbergen2bad8472005-09-09 13:04:19 -0700384}
385
386/**
387 * v9fs_remove - helper function to remove files and directories
Eric Van Hensbergen73c592b2005-09-09 13:04:26 -0700388 * @dir: directory inode that is being deleted
389 * @file: dentry that is being deleted
390 * @rmdir: removing a directory
Eric Van Hensbergen2bad8472005-09-09 13:04:19 -0700391 *
392 */
393
394static int v9fs_remove(struct inode *dir, struct dentry *file, int rmdir)
395{
396 struct v9fs_fcall *fcall = NULL;
397 struct super_block *sb = NULL;
398 struct v9fs_session_info *v9ses = NULL;
399 struct v9fs_fid *v9fid = NULL;
400 struct inode *file_inode = NULL;
401 int fid = -1;
402 int result = 0;
403
404 dprintk(DEBUG_VFS, "inode: %p dentry: %p rmdir: %d\n", dir, file,
405 rmdir);
406
407 file_inode = file->d_inode;
408 sb = file_inode->i_sb;
409 v9ses = v9fs_inode2v9ses(file_inode);
Latchesar Ionkov0b8dd172005-09-27 21:45:24 -0700410 v9fid = v9fs_fid_lookup(file);
Eric Van Hensbergen2bad8472005-09-09 13:04:19 -0700411
412 if (!v9fid) {
413 dprintk(DEBUG_ERROR,
414 "no v9fs_fid\n");
415 return -EBADF;
416 }
417
418 fid = v9fid->fid;
419 if (fid < 0) {
420 dprintk(DEBUG_ERROR, "inode #%lu, no fid!\n",
421 file_inode->i_ino);
422 return -EBADF;
423 }
424
425 result = v9fs_t_remove(v9ses, fid, &fcall);
Latchesar Ionkov531b1092006-01-08 01:05:00 -0800426 if (result < 0) {
427 PRINT_FCALL_ERROR("remove fails", fcall);
428 } else {
Eric Van Hensbergen2bad8472005-09-09 13:04:19 -0700429 v9fs_put_idpool(fid, &v9ses->fidpool);
430 v9fs_fid_destroy(v9fid);
431 }
432
433 kfree(fcall);
434 return result;
435}
436
Latchesar Ionkov6a3124a2006-03-02 02:54:30 -0800437static int
438v9fs_open_created(struct inode *inode, struct file *file)
439{
440 return 0;
441}
442
Eric Van Hensbergen2bad8472005-09-09 13:04:19 -0700443/**
444 * v9fs_vfs_create - VFS hook to create files
445 * @inode: directory inode that is being deleted
446 * @dentry: dentry that is being deleted
Latchesar Ionkov6a3124a2006-03-02 02:54:30 -0800447 * @mode: create permissions
Eric Van Hensbergen2bad8472005-09-09 13:04:19 -0700448 * @nd: path information
449 *
450 */
451
452static int
Latchesar Ionkov6a3124a2006-03-02 02:54:30 -0800453v9fs_vfs_create(struct inode *dir, struct dentry *dentry, int mode,
Eric Van Hensbergen2bad8472005-09-09 13:04:19 -0700454 struct nameidata *nd)
455{
Latchesar Ionkov6a3124a2006-03-02 02:54:30 -0800456 int err;
457 u32 fid, perm, iounit;
458 int flags;
459 struct v9fs_session_info *v9ses;
460 struct v9fs_fid *dfid, *vfid, *ffid;
461 struct inode *inode;
462 struct v9fs_qid qid;
463 struct file *filp;
464
465 inode = NULL;
466 vfid = NULL;
467 v9ses = v9fs_inode2v9ses(dir);
468 dfid = v9fs_fid_lookup(dentry->d_parent);
469 perm = unixmode2p9mode(v9ses, mode);
470
471 if (nd && nd->flags & LOOKUP_OPEN)
472 flags = nd->intent.open.flags - 1;
473 else
474 flags = O_RDWR;
475
476 err = v9fs_create(v9ses, dfid->fid, (char *) dentry->d_name.name,
477 perm, v9fs_uflags2omode(flags), &fid, &qid, &iounit);
478
479 if (err)
480 goto error;
481
482 vfid = v9fs_clone_walk(v9ses, dfid->fid, dentry);
483 if (IS_ERR(vfid)) {
484 err = PTR_ERR(vfid);
485 vfid = NULL;
486 goto error;
487 }
488
489 inode = v9fs_inode_from_fid(v9ses, vfid->fid, dir->i_sb);
490 if (IS_ERR(inode)) {
491 err = PTR_ERR(inode);
492 inode = NULL;
493 goto error;
494 }
495
496 dentry->d_op = &v9fs_dentry_operations;
497 d_instantiate(dentry, inode);
498
499 if (nd && nd->flags & LOOKUP_OPEN) {
500 ffid = v9fs_fid_create(v9ses, fid);
501 if (!ffid)
502 return -ENOMEM;
503
504 filp = lookup_instantiate_filp(nd, dentry, v9fs_open_created);
505 if (IS_ERR(filp)) {
506 v9fs_fid_destroy(ffid);
507 return PTR_ERR(filp);
508 }
509
510 ffid->rdir_pos = 0;
511 ffid->rdir_fcall = NULL;
512 ffid->fidopen = 1;
513 ffid->iounit = iounit;
514 ffid->filp = filp;
515 filp->private_data = ffid;
516 }
517
518 return 0;
519
520error:
521 if (vfid)
522 v9fs_fid_destroy(vfid);
523
524 if (inode)
525 iput(inode);
526
527 return err;
Eric Van Hensbergen2bad8472005-09-09 13:04:19 -0700528}
529
530/**
531 * v9fs_vfs_mkdir - VFS mkdir hook to create a directory
Eric Van Hensbergen73c592b2005-09-09 13:04:26 -0700532 * @inode: inode that is being unlinked
Eric Van Hensbergen2bad8472005-09-09 13:04:19 -0700533 * @dentry: dentry that is being unlinked
534 * @mode: mode for new directory
535 *
536 */
537
Latchesar Ionkov6a3124a2006-03-02 02:54:30 -0800538static int v9fs_vfs_mkdir(struct inode *dir, struct dentry *dentry, int mode)
Eric Van Hensbergen2bad8472005-09-09 13:04:19 -0700539{
Latchesar Ionkov6a3124a2006-03-02 02:54:30 -0800540 int err;
541 u32 fid, perm;
542 struct v9fs_session_info *v9ses;
543 struct v9fs_fid *dfid, *vfid;
544 struct inode *inode;
545
546 inode = NULL;
547 vfid = NULL;
548 v9ses = v9fs_inode2v9ses(dir);
549 dfid = v9fs_fid_lookup(dentry->d_parent);
550 perm = unixmode2p9mode(v9ses, mode | S_IFDIR);
551
552 err = v9fs_create(v9ses, dfid->fid, (char *) dentry->d_name.name,
553 perm, V9FS_OREAD, &fid, NULL, NULL);
554
555 if (err) {
556 dprintk(DEBUG_ERROR, "create error %d\n", err);
557 goto error;
558 }
559
560 err = v9fs_t_clunk(v9ses, fid);
561 if (err) {
562 dprintk(DEBUG_ERROR, "clunk error %d\n", err);
563 goto error;
564 }
565
566 vfid = v9fs_clone_walk(v9ses, dfid->fid, dentry);
567 if (IS_ERR(vfid)) {
568 err = PTR_ERR(vfid);
569 vfid = NULL;
570 goto error;
571 }
572
573 inode = v9fs_inode_from_fid(v9ses, vfid->fid, dir->i_sb);
574 if (IS_ERR(inode)) {
575 err = PTR_ERR(inode);
576 inode = NULL;
577 goto error;
578 }
579
580 dentry->d_op = &v9fs_dentry_operations;
581 d_instantiate(dentry, inode);
582 return 0;
583
584error:
585 if (vfid)
586 v9fs_fid_destroy(vfid);
587
588 return err;
Eric Van Hensbergen2bad8472005-09-09 13:04:19 -0700589}
590
591/**
592 * v9fs_vfs_lookup - VFS lookup hook to "walk" to a new inode
593 * @dir: inode that is being walked from
594 * @dentry: dentry that is being walked to?
595 * @nameidata: path data
596 *
597 */
598
599static struct dentry *v9fs_vfs_lookup(struct inode *dir, struct dentry *dentry,
600 struct nameidata *nameidata)
601{
602 struct super_block *sb;
603 struct v9fs_session_info *v9ses;
604 struct v9fs_fid *dirfid;
605 struct v9fs_fid *fid;
606 struct inode *inode;
607 struct v9fs_fcall *fcall = NULL;
Eric Van Hensbergen2bad8472005-09-09 13:04:19 -0700608 int dirfidnum = -1;
609 int newfid = -1;
610 int result = 0;
611
612 dprintk(DEBUG_VFS, "dir: %p dentry: (%s) %p nameidata: %p\n",
Latchesar Ionkov731805b2006-03-07 21:55:42 -0800613 dir, dentry->d_name.name, dentry, nameidata);
Eric Van Hensbergen2bad8472005-09-09 13:04:19 -0700614
615 sb = dir->i_sb;
616 v9ses = v9fs_inode2v9ses(dir);
Latchesar Ionkov5e7a99a2006-03-22 00:07:37 -0800617 dentry->d_op = &v9fs_dentry_operations;
Latchesar Ionkov0b8dd172005-09-27 21:45:24 -0700618 dirfid = v9fs_fid_lookup(dentry->d_parent);
Eric Van Hensbergen2bad8472005-09-09 13:04:19 -0700619
620 if (!dirfid) {
621 dprintk(DEBUG_ERROR, "no dirfid\n");
622 return ERR_PTR(-EINVAL);
623 }
624
625 dirfidnum = dirfid->fid;
626
627 if (dirfidnum < 0) {
628 dprintk(DEBUG_ERROR, "no dirfid for inode %p, #%lu\n",
629 dir, dir->i_ino);
630 return ERR_PTR(-EBADF);
631 }
632
633 newfid = v9fs_get_idpool(&v9ses->fidpool);
634 if (newfid < 0) {
635 eprintk(KERN_WARNING, "newfid fails!\n");
636 return ERR_PTR(-ENOSPC);
637 }
638
Latchesar Ionkov6a3124a2006-03-02 02:54:30 -0800639 result = v9fs_t_walk(v9ses, dirfidnum, newfid,
640 (char *)dentry->d_name.name, NULL);
Eric Van Hensbergen2bad8472005-09-09 13:04:19 -0700641 if (result < 0) {
642 v9fs_put_idpool(newfid, &v9ses->fidpool);
643 if (result == -ENOENT) {
644 d_add(dentry, NULL);
Latchesar Ionkov0b8dd172005-09-27 21:45:24 -0700645 dprintk(DEBUG_VFS,
Eric Van Hensbergen2bad8472005-09-09 13:04:19 -0700646 "Return negative dentry %p count %d\n",
647 dentry, atomic_read(&dentry->d_count));
648 return NULL;
649 }
650 dprintk(DEBUG_ERROR, "walk error:%d\n", result);
651 goto FreeFcall;
652 }
653
654 result = v9fs_t_stat(v9ses, newfid, &fcall);
655 if (result < 0) {
656 dprintk(DEBUG_ERROR, "stat error\n");
657 goto FreeFcall;
658 }
659
Latchesar Ionkov531b1092006-01-08 01:05:00 -0800660 inode = v9fs_get_inode(sb, p9mode2unixmode(v9ses,
661 fcall->params.rstat.stat.mode));
Eric Van Hensbergen2bad8472005-09-09 13:04:19 -0700662
663 if (IS_ERR(inode) && (PTR_ERR(inode) == -ENOSPC)) {
664 eprintk(KERN_WARNING, "inode alloc failes, returns %ld\n",
665 PTR_ERR(inode));
666
667 result = -ENOSPC;
668 goto FreeFcall;
669 }
670
Latchesar Ionkov531b1092006-01-08 01:05:00 -0800671 inode->i_ino = v9fs_qid2ino(&fcall->params.rstat.stat.qid);
Eric Van Hensbergen2bad8472005-09-09 13:04:19 -0700672
Latchesar Ionkov6a3124a2006-03-02 02:54:30 -0800673 fid = v9fs_fid_create(v9ses, newfid);
Eric Van Hensbergen2bad8472005-09-09 13:04:19 -0700674 if (fid == NULL) {
675 dprintk(DEBUG_ERROR, "couldn't insert\n");
676 result = -ENOMEM;
677 goto FreeFcall;
678 }
679
Latchesar Ionkov6a3124a2006-03-02 02:54:30 -0800680 result = v9fs_fid_insert(fid, dentry);
681 if (result < 0)
682 goto FreeFcall;
683
Latchesar Ionkov531b1092006-01-08 01:05:00 -0800684 fid->qid = fcall->params.rstat.stat.qid;
Latchesar Ionkov531b1092006-01-08 01:05:00 -0800685 v9fs_stat2inode(&fcall->params.rstat.stat, inode, inode->i_sb);
Eric Van Hensbergen2bad8472005-09-09 13:04:19 -0700686
687 d_add(dentry, inode);
688 kfree(fcall);
689
690 return NULL;
691
692 FreeFcall:
693 kfree(fcall);
694 return ERR_PTR(result);
695}
696
697/**
698 * v9fs_vfs_unlink - VFS unlink hook to delete an inode
699 * @i: inode that is being unlinked
Eric Van Hensbergen73c592b2005-09-09 13:04:26 -0700700 * @d: dentry that is being unlinked
Eric Van Hensbergen2bad8472005-09-09 13:04:19 -0700701 *
702 */
703
704static int v9fs_vfs_unlink(struct inode *i, struct dentry *d)
705{
706 return v9fs_remove(i, d, 0);
707}
708
709/**
710 * v9fs_vfs_rmdir - VFS unlink hook to delete a directory
711 * @i: inode that is being unlinked
Eric Van Hensbergen73c592b2005-09-09 13:04:26 -0700712 * @d: dentry that is being unlinked
Eric Van Hensbergen2bad8472005-09-09 13:04:19 -0700713 *
714 */
715
716static int v9fs_vfs_rmdir(struct inode *i, struct dentry *d)
717{
718 return v9fs_remove(i, d, 1);
719}
720
721/**
722 * v9fs_vfs_rename - VFS hook to rename an inode
723 * @old_dir: old dir inode
724 * @old_dentry: old dentry
725 * @new_dir: new dir inode
726 * @new_dentry: new dentry
727 *
728 */
729
730static int
731v9fs_vfs_rename(struct inode *old_dir, struct dentry *old_dentry,
732 struct inode *new_dir, struct dentry *new_dentry)
733{
734 struct inode *old_inode = old_dentry->d_inode;
735 struct v9fs_session_info *v9ses = v9fs_inode2v9ses(old_inode);
Latchesar Ionkov0b8dd172005-09-27 21:45:24 -0700736 struct v9fs_fid *oldfid = v9fs_fid_lookup(old_dentry);
Eric Van Hensbergen2bad8472005-09-09 13:04:19 -0700737 struct v9fs_fid *olddirfid =
Latchesar Ionkov0b8dd172005-09-27 21:45:24 -0700738 v9fs_fid_lookup(old_dentry->d_parent);
Eric Van Hensbergen2bad8472005-09-09 13:04:19 -0700739 struct v9fs_fid *newdirfid =
Latchesar Ionkov0b8dd172005-09-27 21:45:24 -0700740 v9fs_fid_lookup(new_dentry->d_parent);
Latchesar Ionkov531b1092006-01-08 01:05:00 -0800741 struct v9fs_wstat wstat;
Eric Van Hensbergen2bad8472005-09-09 13:04:19 -0700742 struct v9fs_fcall *fcall = NULL;
743 int fid = -1;
744 int olddirfidnum = -1;
745 int newdirfidnum = -1;
746 int retval = 0;
747
748 dprintk(DEBUG_VFS, "\n");
749
750 if ((!oldfid) || (!olddirfid) || (!newdirfid)) {
751 dprintk(DEBUG_ERROR, "problem with arguments\n");
752 return -EBADF;
753 }
754
755 /* 9P can only handle file rename in the same directory */
756 if (memcmp(&olddirfid->qid, &newdirfid->qid, sizeof(newdirfid->qid))) {
757 dprintk(DEBUG_ERROR, "old dir and new dir are different\n");
758 retval = -EPERM;
759 goto FreeFcallnBail;
760 }
761
762 fid = oldfid->fid;
763 olddirfidnum = olddirfid->fid;
764 newdirfidnum = newdirfid->fid;
765
766 if (fid < 0) {
767 dprintk(DEBUG_ERROR, "no fid for old file #%lu\n",
768 old_inode->i_ino);
769 retval = -EBADF;
770 goto FreeFcallnBail;
771 }
772
Latchesar Ionkov531b1092006-01-08 01:05:00 -0800773 v9fs_blank_wstat(&wstat);
774 wstat.muid = v9ses->name;
775 wstat.name = (char *) new_dentry->d_name.name;
Eric Van Hensbergen2bad8472005-09-09 13:04:19 -0700776
Latchesar Ionkov531b1092006-01-08 01:05:00 -0800777 retval = v9fs_t_wstat(v9ses, fid, &wstat, &fcall);
Eric Van Hensbergen2bad8472005-09-09 13:04:19 -0700778
779 FreeFcallnBail:
Eric Van Hensbergen2bad8472005-09-09 13:04:19 -0700780 if (retval < 0)
Latchesar Ionkov531b1092006-01-08 01:05:00 -0800781 PRINT_FCALL_ERROR("wstat error", fcall);
Eric Van Hensbergen2bad8472005-09-09 13:04:19 -0700782
783 kfree(fcall);
784 return retval;
785}
786
787/**
Adrian Bunk943ffb52006-01-10 00:10:13 +0100788 * v9fs_vfs_getattr - retrieve file metadata
Eric Van Hensbergen2bad8472005-09-09 13:04:19 -0700789 * @mnt - mount information
790 * @dentry - file to get attributes on
791 * @stat - metadata structure to populate
792 *
793 */
794
795static int
796v9fs_vfs_getattr(struct vfsmount *mnt, struct dentry *dentry,
797 struct kstat *stat)
798{
799 struct v9fs_fcall *fcall = NULL;
800 struct v9fs_session_info *v9ses = v9fs_inode2v9ses(dentry->d_inode);
Latchesar Ionkov0b8dd172005-09-27 21:45:24 -0700801 struct v9fs_fid *fid = v9fs_fid_lookup(dentry);
Eric Van Hensbergen2bad8472005-09-09 13:04:19 -0700802 int err = -EPERM;
803
804 dprintk(DEBUG_VFS, "dentry: %p\n", dentry);
805 if (!fid) {
806 dprintk(DEBUG_ERROR,
807 "couldn't find fid associated with dentry\n");
808 return -EBADF;
809 }
810
811 err = v9fs_t_stat(v9ses, fid->fid, &fcall);
812
813 if (err < 0)
814 dprintk(DEBUG_ERROR, "stat error\n");
815 else {
Latchesar Ionkov531b1092006-01-08 01:05:00 -0800816 v9fs_stat2inode(&fcall->params.rstat.stat, dentry->d_inode,
Eric Van Hensbergen2bad8472005-09-09 13:04:19 -0700817 dentry->d_inode->i_sb);
818 generic_fillattr(dentry->d_inode, stat);
819 }
820
821 kfree(fcall);
822 return err;
823}
824
825/**
826 * v9fs_vfs_setattr - set file metadata
827 * @dentry: file whose metadata to set
828 * @iattr: metadata assignment structure
829 *
830 */
831
832static int v9fs_vfs_setattr(struct dentry *dentry, struct iattr *iattr)
833{
834 struct v9fs_session_info *v9ses = v9fs_inode2v9ses(dentry->d_inode);
Latchesar Ionkov0b8dd172005-09-27 21:45:24 -0700835 struct v9fs_fid *fid = v9fs_fid_lookup(dentry);
Eric Van Hensbergen2bad8472005-09-09 13:04:19 -0700836 struct v9fs_fcall *fcall = NULL;
Latchesar Ionkov531b1092006-01-08 01:05:00 -0800837 struct v9fs_wstat wstat;
Eric Van Hensbergen2bad8472005-09-09 13:04:19 -0700838 int res = -EPERM;
839
840 dprintk(DEBUG_VFS, "\n");
Eric Van Hensbergen73c592b2005-09-09 13:04:26 -0700841
Eric Van Hensbergen2bad8472005-09-09 13:04:19 -0700842 if (!fid) {
843 dprintk(DEBUG_ERROR,
844 "Couldn't find fid associated with dentry\n");
845 return -EBADF;
846 }
847
Latchesar Ionkov531b1092006-01-08 01:05:00 -0800848 v9fs_blank_wstat(&wstat);
Eric Van Hensbergen2bad8472005-09-09 13:04:19 -0700849 if (iattr->ia_valid & ATTR_MODE)
Latchesar Ionkov531b1092006-01-08 01:05:00 -0800850 wstat.mode = unixmode2p9mode(v9ses, iattr->ia_mode);
Eric Van Hensbergen2bad8472005-09-09 13:04:19 -0700851
852 if (iattr->ia_valid & ATTR_MTIME)
Latchesar Ionkov531b1092006-01-08 01:05:00 -0800853 wstat.mtime = iattr->ia_mtime.tv_sec;
Eric Van Hensbergen2bad8472005-09-09 13:04:19 -0700854
855 if (iattr->ia_valid & ATTR_ATIME)
Latchesar Ionkov531b1092006-01-08 01:05:00 -0800856 wstat.atime = iattr->ia_atime.tv_sec;
Eric Van Hensbergen2bad8472005-09-09 13:04:19 -0700857
858 if (iattr->ia_valid & ATTR_SIZE)
Latchesar Ionkov531b1092006-01-08 01:05:00 -0800859 wstat.length = iattr->ia_size;
Eric Van Hensbergen2bad8472005-09-09 13:04:19 -0700860
861 if (v9ses->extended) {
Latchesar Ionkov531b1092006-01-08 01:05:00 -0800862 if (iattr->ia_valid & ATTR_UID)
863 wstat.n_uid = iattr->ia_uid;
Eric Van Hensbergen2bad8472005-09-09 13:04:19 -0700864
Latchesar Ionkov531b1092006-01-08 01:05:00 -0800865 if (iattr->ia_valid & ATTR_GID)
866 wstat.n_gid = iattr->ia_gid;
Eric Van Hensbergen2bad8472005-09-09 13:04:19 -0700867 }
868
Latchesar Ionkov531b1092006-01-08 01:05:00 -0800869 res = v9fs_t_wstat(v9ses, fid->fid, &wstat, &fcall);
Eric Van Hensbergen2bad8472005-09-09 13:04:19 -0700870
871 if (res < 0)
Latchesar Ionkov531b1092006-01-08 01:05:00 -0800872 PRINT_FCALL_ERROR("wstat error", fcall);
Eric Van Hensbergen2bad8472005-09-09 13:04:19 -0700873
Eric Van Hensbergen2bad8472005-09-09 13:04:19 -0700874 kfree(fcall);
Eric Van Hensbergen2bad8472005-09-09 13:04:19 -0700875 if (res >= 0)
876 res = inode_setattr(dentry->d_inode, iattr);
877
878 return res;
879}
880
881/**
Latchesar Ionkov531b1092006-01-08 01:05:00 -0800882 * v9fs_stat2inode - populate an inode structure with mistat info
883 * @stat: Plan 9 metadata (mistat) structure
Eric Van Hensbergen2bad8472005-09-09 13:04:19 -0700884 * @inode: inode to populate
885 * @sb: superblock of filesystem
886 *
887 */
888
889void
Latchesar Ionkov531b1092006-01-08 01:05:00 -0800890v9fs_stat2inode(struct v9fs_stat *stat, struct inode *inode,
891 struct super_block *sb)
Eric Van Hensbergen2bad8472005-09-09 13:04:19 -0700892{
Latchesar Ionkov1dac06b2006-01-08 01:05:02 -0800893 int n;
Latchesar Ionkov531b1092006-01-08 01:05:00 -0800894 char ext[32];
Eric Van Hensbergen2bad8472005-09-09 13:04:19 -0700895 struct v9fs_session_info *v9ses = sb->s_fs_info;
896
897 inode->i_nlink = 1;
898
Latchesar Ionkov531b1092006-01-08 01:05:00 -0800899 inode->i_atime.tv_sec = stat->atime;
900 inode->i_mtime.tv_sec = stat->mtime;
901 inode->i_ctime.tv_sec = stat->mtime;
Eric Van Hensbergen2bad8472005-09-09 13:04:19 -0700902
Latchesar Ionkov531b1092006-01-08 01:05:00 -0800903 inode->i_uid = v9ses->uid;
904 inode->i_gid = v9ses->gid;
Eric Van Hensbergen2bad8472005-09-09 13:04:19 -0700905
906 if (v9ses->extended) {
Latchesar Ionkov531b1092006-01-08 01:05:00 -0800907 inode->i_uid = stat->n_uid;
908 inode->i_gid = stat->n_gid;
Eric Van Hensbergen2bad8472005-09-09 13:04:19 -0700909 }
910
Latchesar Ionkov531b1092006-01-08 01:05:00 -0800911 inode->i_mode = p9mode2unixmode(v9ses, stat->mode);
Eric Van Hensbergen2bad8472005-09-09 13:04:19 -0700912 if ((S_ISBLK(inode->i_mode)) || (S_ISCHR(inode->i_mode))) {
913 char type = 0;
914 int major = -1;
915 int minor = -1;
Latchesar Ionkov531b1092006-01-08 01:05:00 -0800916
Latchesar Ionkov1dac06b2006-01-08 01:05:02 -0800917 n = stat->extension.len;
918 if (n > sizeof(ext)-1)
919 n = sizeof(ext)-1;
920 memmove(ext, stat->extension.str, n);
921 ext[n] = 0;
Latchesar Ionkov531b1092006-01-08 01:05:00 -0800922 sscanf(ext, "%c %u %u", &type, &major, &minor);
Eric Van Hensbergen2bad8472005-09-09 13:04:19 -0700923 switch (type) {
924 case 'c':
925 inode->i_mode &= ~S_IFBLK;
926 inode->i_mode |= S_IFCHR;
927 break;
928 case 'b':
929 break;
930 default:
Latchesar Ionkov531b1092006-01-08 01:05:00 -0800931 dprintk(DEBUG_ERROR, "Unknown special type %c (%.*s)\n",
932 type, stat->extension.len, stat->extension.str);
Eric Van Hensbergen2bad8472005-09-09 13:04:19 -0700933 };
934 inode->i_rdev = MKDEV(major, minor);
935 } else
936 inode->i_rdev = 0;
937
Latchesar Ionkov531b1092006-01-08 01:05:00 -0800938 inode->i_size = stat->length;
Eric Van Hensbergen2bad8472005-09-09 13:04:19 -0700939
940 inode->i_blksize = sb->s_blocksize;
941 inode->i_blocks =
942 (inode->i_size + inode->i_blksize - 1) >> sb->s_blocksize_bits;
943}
944
945/**
946 * v9fs_qid2ino - convert qid into inode number
947 * @qid: qid to hash
948 *
949 * BUG: potential for inode number collisions?
950 */
951
952ino_t v9fs_qid2ino(struct v9fs_qid *qid)
953{
954 u64 path = qid->path + 2;
955 ino_t i = 0;
956
957 if (sizeof(ino_t) == sizeof(path))
958 memcpy(&i, &path, sizeof(ino_t));
959 else
960 i = (ino_t) (path ^ (path >> 32));
961
962 return i;
963}
964
965/**
Eric Van Hensbergen2bad8472005-09-09 13:04:19 -0700966 * v9fs_readlink - read a symlink's location (internal version)
967 * @dentry: dentry for symlink
Eric Van Hensbergen73c592b2005-09-09 13:04:26 -0700968 * @buffer: buffer to load symlink location into
Eric Van Hensbergen2bad8472005-09-09 13:04:19 -0700969 * @buflen: length of buffer
970 *
971 */
972
973static int v9fs_readlink(struct dentry *dentry, char *buffer, int buflen)
974{
975 int retval = -EPERM;
976
977 struct v9fs_fcall *fcall = NULL;
978 struct v9fs_session_info *v9ses = v9fs_inode2v9ses(dentry->d_inode);
Latchesar Ionkov0b8dd172005-09-27 21:45:24 -0700979 struct v9fs_fid *fid = v9fs_fid_lookup(dentry);
Eric Van Hensbergen2bad8472005-09-09 13:04:19 -0700980
981 if (!fid) {
982 dprintk(DEBUG_ERROR, "could not resolve fid from dentry\n");
983 retval = -EBADF;
984 goto FreeFcall;
985 }
986
987 if (!v9ses->extended) {
988 retval = -EBADF;
989 dprintk(DEBUG_ERROR, "not extended\n");
990 goto FreeFcall;
991 }
992
993 dprintk(DEBUG_VFS, " %s\n", dentry->d_name.name);
994 retval = v9fs_t_stat(v9ses, fid->fid, &fcall);
995
996 if (retval < 0) {
997 dprintk(DEBUG_ERROR, "stat error\n");
998 goto FreeFcall;
999 }
1000
1001 if (!fcall)
1002 return -EIO;
1003
Latchesar Ionkov531b1092006-01-08 01:05:00 -08001004 if (!(fcall->params.rstat.stat.mode & V9FS_DMSYMLINK)) {
Eric Van Hensbergen2bad8472005-09-09 13:04:19 -07001005 retval = -EINVAL;
1006 goto FreeFcall;
1007 }
1008
1009 /* copy extension buffer into buffer */
Latchesar Ionkov6a3124a2006-03-02 02:54:30 -08001010 if (fcall->params.rstat.stat.extension.len < buflen)
1011 buflen = fcall->params.rstat.stat.extension.len;
Eric Van Hensbergen2bad8472005-09-09 13:04:19 -07001012
Latchesar Ionkov531b1092006-01-08 01:05:00 -08001013 memcpy(buffer, fcall->params.rstat.stat.extension.str, buflen - 1);
1014 buffer[buflen-1] = 0;
Eric Van Hensbergen2bad8472005-09-09 13:04:19 -07001015
1016 retval = buflen;
1017
1018 FreeFcall:
1019 kfree(fcall);
1020
1021 return retval;
1022}
1023
1024/**
1025 * v9fs_vfs_readlink - read a symlink's location
1026 * @dentry: dentry for symlink
1027 * @buf: buffer to load symlink location into
1028 * @buflen: length of buffer
1029 *
1030 */
1031
1032static int v9fs_vfs_readlink(struct dentry *dentry, char __user * buffer,
1033 int buflen)
1034{
1035 int retval;
1036 int ret;
1037 char *link = __getname();
1038
Latchesar Ionkova1f9d8d2005-09-22 21:43:52 -07001039 if (buflen > PATH_MAX)
1040 buflen = PATH_MAX;
Eric Van Hensbergen2bad8472005-09-09 13:04:19 -07001041
1042 dprintk(DEBUG_VFS, " dentry: %s (%p)\n", dentry->d_iname, dentry);
1043
1044 retval = v9fs_readlink(dentry, link, buflen);
1045
1046 if (retval > 0) {
1047 if ((ret = copy_to_user(buffer, link, retval)) != 0) {
1048 dprintk(DEBUG_ERROR, "problem copying to user: %d\n",
1049 ret);
1050 retval = ret;
1051 }
1052 }
1053
Davi Arnautce44eeb2005-11-07 00:59:36 -08001054 __putname(link);
Eric Van Hensbergen2bad8472005-09-09 13:04:19 -07001055 return retval;
1056}
1057
1058/**
1059 * v9fs_vfs_follow_link - follow a symlink path
1060 * @dentry: dentry for symlink
1061 * @nd: nameidata
1062 *
1063 */
1064
1065static void *v9fs_vfs_follow_link(struct dentry *dentry, struct nameidata *nd)
1066{
1067 int len = 0;
1068 char *link = __getname();
1069
1070 dprintk(DEBUG_VFS, "%s n", dentry->d_name.name);
1071
1072 if (!link)
1073 link = ERR_PTR(-ENOMEM);
1074 else {
Latchesar Ionkov6a3124a2006-03-02 02:54:30 -08001075 len = v9fs_readlink(dentry, link, strlen(link));
Eric Van Hensbergen2bad8472005-09-09 13:04:19 -07001076
1077 if (len < 0) {
Davi Arnautce44eeb2005-11-07 00:59:36 -08001078 __putname(link);
Eric Van Hensbergen2bad8472005-09-09 13:04:19 -07001079 link = ERR_PTR(len);
1080 } else
1081 link[len] = 0;
1082 }
1083 nd_set_link(nd, link);
1084
1085 return NULL;
1086}
1087
1088/**
1089 * v9fs_vfs_put_link - release a symlink path
1090 * @dentry: dentry for symlink
1091 * @nd: nameidata
1092 *
1093 */
1094
1095static void v9fs_vfs_put_link(struct dentry *dentry, struct nameidata *nd, void *p)
1096{
1097 char *s = nd_get_link(nd);
1098
1099 dprintk(DEBUG_VFS, " %s %s\n", dentry->d_name.name, s);
1100 if (!IS_ERR(s))
Davi Arnautce44eeb2005-11-07 00:59:36 -08001101 __putname(s);
Eric Van Hensbergen2bad8472005-09-09 13:04:19 -07001102}
1103
Latchesar Ionkov531b1092006-01-08 01:05:00 -08001104static int v9fs_vfs_mkspecial(struct inode *dir, struct dentry *dentry,
1105 int mode, const char *extension)
1106{
Latchesar Ionkov6a3124a2006-03-02 02:54:30 -08001107 int err;
1108 u32 fid, perm;
Latchesar Ionkov531b1092006-01-08 01:05:00 -08001109 struct v9fs_session_info *v9ses;
Latchesar Ionkov6a3124a2006-03-02 02:54:30 -08001110 struct v9fs_fid *dfid, *vfid;
1111 struct inode *inode;
Latchesar Ionkov531b1092006-01-08 01:05:00 -08001112 struct v9fs_fcall *fcall;
Latchesar Ionkov531b1092006-01-08 01:05:00 -08001113 struct v9fs_wstat wstat;
1114
Latchesar Ionkov531b1092006-01-08 01:05:00 -08001115 fcall = NULL;
Latchesar Ionkov6a3124a2006-03-02 02:54:30 -08001116 inode = NULL;
1117 vfid = NULL;
1118 v9ses = v9fs_inode2v9ses(dir);
1119 dfid = v9fs_fid_lookup(dentry->d_parent);
1120 perm = unixmode2p9mode(v9ses, mode);
Latchesar Ionkov531b1092006-01-08 01:05:00 -08001121
1122 if (!v9ses->extended) {
1123 dprintk(DEBUG_ERROR, "not extended\n");
Latchesar Ionkov6a3124a2006-03-02 02:54:30 -08001124 return -EPERM;
Latchesar Ionkov531b1092006-01-08 01:05:00 -08001125 }
1126
Latchesar Ionkov6a3124a2006-03-02 02:54:30 -08001127 err = v9fs_create(v9ses, dfid->fid, (char *) dentry->d_name.name,
1128 perm, V9FS_OREAD, &fid, NULL, NULL);
Latchesar Ionkov531b1092006-01-08 01:05:00 -08001129
Latchesar Ionkov6a3124a2006-03-02 02:54:30 -08001130 if (err)
1131 goto error;
1132
1133 err = v9fs_t_clunk(v9ses, fid);
1134 if (err)
1135 goto error;
1136
1137 vfid = v9fs_clone_walk(v9ses, dfid->fid, dentry);
1138 if (IS_ERR(vfid)) {
1139 err = PTR_ERR(vfid);
1140 vfid = NULL;
1141 goto error;
1142 }
1143
1144 inode = v9fs_inode_from_fid(v9ses, vfid->fid, dir->i_sb);
1145 if (IS_ERR(inode)) {
1146 err = PTR_ERR(inode);
1147 inode = NULL;
1148 goto error;
Latchesar Ionkov531b1092006-01-08 01:05:00 -08001149 }
1150
1151 /* issue a Twstat */
1152 v9fs_blank_wstat(&wstat);
1153 wstat.muid = v9ses->name;
1154 wstat.extension = (char *) extension;
Latchesar Ionkov6a3124a2006-03-02 02:54:30 -08001155 err = v9fs_t_wstat(v9ses, vfid->fid, &wstat, &fcall);
Latchesar Ionkov531b1092006-01-08 01:05:00 -08001156 if (err < 0) {
Latchesar Ionkov6a3124a2006-03-02 02:54:30 -08001157 PRINT_FCALL_ERROR("wstat error", fcall);
1158 goto error;
Latchesar Ionkov531b1092006-01-08 01:05:00 -08001159 }
1160
Latchesar Ionkov531b1092006-01-08 01:05:00 -08001161 kfree(fcall);
Latchesar Ionkov6a3124a2006-03-02 02:54:30 -08001162 dentry->d_op = &v9fs_dentry_operations;
1163 d_instantiate(dentry, inode);
1164 return 0;
1165
1166error:
1167 kfree(fcall);
1168 if (vfid)
1169 v9fs_fid_destroy(vfid);
1170
1171 if (inode)
1172 iput(inode);
1173
1174 return err;
1175
Latchesar Ionkov531b1092006-01-08 01:05:00 -08001176}
1177
1178/**
1179 * v9fs_vfs_symlink - helper function to create symlinks
1180 * @dir: directory inode containing symlink
1181 * @dentry: dentry for symlink
1182 * @symname: symlink data
1183 *
1184 * See 9P2000.u RFC for more information
1185 *
1186 */
1187
1188static int
1189v9fs_vfs_symlink(struct inode *dir, struct dentry *dentry, const char *symname)
1190{
1191 dprintk(DEBUG_VFS, " %lu,%s,%s\n", dir->i_ino, dentry->d_name.name,
1192 symname);
1193
1194 return v9fs_vfs_mkspecial(dir, dentry, S_IFLNK, symname);
1195}
1196
Eric Van Hensbergen2bad8472005-09-09 13:04:19 -07001197/**
1198 * v9fs_vfs_link - create a hardlink
1199 * @old_dentry: dentry for file to link to
1200 * @dir: inode destination for new link
Eric Van Hensbergen73c592b2005-09-09 13:04:26 -07001201 * @dentry: dentry for link
Eric Van Hensbergen2bad8472005-09-09 13:04:19 -07001202 *
1203 */
1204
1205/* XXX - lots of code dup'd from symlink and creates,
1206 * figure out a better reuse strategy
1207 */
1208
1209static int
1210v9fs_vfs_link(struct dentry *old_dentry, struct inode *dir,
1211 struct dentry *dentry)
1212{
Latchesar Ionkov531b1092006-01-08 01:05:00 -08001213 int retval;
1214 struct v9fs_fid *oldfid;
1215 char *name;
Eric Van Hensbergen2bad8472005-09-09 13:04:19 -07001216
1217 dprintk(DEBUG_VFS, " %lu,%s,%s\n", dir->i_ino, dentry->d_name.name,
1218 old_dentry->d_name.name);
1219
Latchesar Ionkov531b1092006-01-08 01:05:00 -08001220 oldfid = v9fs_fid_lookup(old_dentry);
1221 if (!oldfid) {
1222 dprintk(DEBUG_ERROR, "can't find oldfid\n");
1223 return -EPERM;
Eric Van Hensbergen2bad8472005-09-09 13:04:19 -07001224 }
1225
Latchesar Ionkov531b1092006-01-08 01:05:00 -08001226 name = __getname();
1227 sprintf(name, "hardlink(%d)\n", oldfid->fid);
1228 retval = v9fs_vfs_mkspecial(dir, dentry, V9FS_DMLINK, name);
1229 __putname(name);
Eric Van Hensbergen2bad8472005-09-09 13:04:19 -07001230
Eric Van Hensbergen2bad8472005-09-09 13:04:19 -07001231 return retval;
1232}
1233
1234/**
1235 * v9fs_vfs_mknod - create a special file
1236 * @dir: inode destination for new link
1237 * @dentry: dentry for file
1238 * @mode: mode for creation
1239 * @dev_t: device associated with special file
1240 *
1241 */
1242
1243static int
1244v9fs_vfs_mknod(struct inode *dir, struct dentry *dentry, int mode, dev_t rdev)
1245{
Latchesar Ionkov531b1092006-01-08 01:05:00 -08001246 int retval;
1247 char *name;
Eric Van Hensbergen2bad8472005-09-09 13:04:19 -07001248
1249 dprintk(DEBUG_VFS, " %lu,%s mode: %x MAJOR: %u MINOR: %u\n", dir->i_ino,
1250 dentry->d_name.name, mode, MAJOR(rdev), MINOR(rdev));
1251
Latchesar Ionkov531b1092006-01-08 01:05:00 -08001252 if (!new_valid_dev(rdev))
1253 return -EINVAL;
Eric Van Hensbergen73c592b2005-09-09 13:04:26 -07001254
Latchesar Ionkov531b1092006-01-08 01:05:00 -08001255 name = __getname();
Eric Van Hensbergen2bad8472005-09-09 13:04:19 -07001256 /* build extension */
1257 if (S_ISBLK(mode))
Latchesar Ionkov531b1092006-01-08 01:05:00 -08001258 sprintf(name, "b %u %u", MAJOR(rdev), MINOR(rdev));
Eric Van Hensbergen2bad8472005-09-09 13:04:19 -07001259 else if (S_ISCHR(mode))
Latchesar Ionkov531b1092006-01-08 01:05:00 -08001260 sprintf(name, "c %u %u", MAJOR(rdev), MINOR(rdev));
Eric Van Hensbergen73c592b2005-09-09 13:04:26 -07001261 else if (S_ISFIFO(mode))
Latchesar Ionkov531b1092006-01-08 01:05:00 -08001262 *name = 0;
Eric Van Hensbergen2bad8472005-09-09 13:04:19 -07001263 else {
Latchesar Ionkov531b1092006-01-08 01:05:00 -08001264 __putname(name);
1265 return -EINVAL;
Eric Van Hensbergen2bad8472005-09-09 13:04:19 -07001266 }
1267
Latchesar Ionkov531b1092006-01-08 01:05:00 -08001268 retval = v9fs_vfs_mkspecial(dir, dentry, mode, name);
1269 __putname(name);
Eric Van Hensbergen2bad8472005-09-09 13:04:19 -07001270
1271 return retval;
1272}
1273
Eric Van Hensbergenb5016112005-09-09 13:04:27 -07001274static struct inode_operations v9fs_dir_inode_operations_ext = {
Eric Van Hensbergen2bad8472005-09-09 13:04:19 -07001275 .create = v9fs_vfs_create,
1276 .lookup = v9fs_vfs_lookup,
1277 .symlink = v9fs_vfs_symlink,
1278 .link = v9fs_vfs_link,
1279 .unlink = v9fs_vfs_unlink,
1280 .mkdir = v9fs_vfs_mkdir,
1281 .rmdir = v9fs_vfs_rmdir,
1282 .mknod = v9fs_vfs_mknod,
1283 .rename = v9fs_vfs_rename,
1284 .readlink = v9fs_vfs_readlink,
1285 .getattr = v9fs_vfs_getattr,
1286 .setattr = v9fs_vfs_setattr,
1287};
1288
Eric Van Hensbergenb5016112005-09-09 13:04:27 -07001289static struct inode_operations v9fs_dir_inode_operations = {
1290 .create = v9fs_vfs_create,
1291 .lookup = v9fs_vfs_lookup,
1292 .unlink = v9fs_vfs_unlink,
1293 .mkdir = v9fs_vfs_mkdir,
1294 .rmdir = v9fs_vfs_rmdir,
1295 .mknod = v9fs_vfs_mknod,
1296 .rename = v9fs_vfs_rename,
1297 .getattr = v9fs_vfs_getattr,
1298 .setattr = v9fs_vfs_setattr,
1299};
1300
Eric Van Hensbergen2bad8472005-09-09 13:04:19 -07001301static struct inode_operations v9fs_file_inode_operations = {
1302 .getattr = v9fs_vfs_getattr,
1303 .setattr = v9fs_vfs_setattr,
1304};
1305
1306static struct inode_operations v9fs_symlink_inode_operations = {
1307 .readlink = v9fs_vfs_readlink,
1308 .follow_link = v9fs_vfs_follow_link,
1309 .put_link = v9fs_vfs_put_link,
1310 .getattr = v9fs_vfs_getattr,
1311 .setattr = v9fs_vfs_setattr,
1312};