blob: 91f552454c7656470eecd32a6d00513e5b54d81f [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
128/**
Latchesar Ionkov531b1092006-01-08 01:05:00 -0800129 * v9fs_blank_wstat - helper function to setup a 9P stat structure
Eric Van Hensbergen2bad8472005-09-09 13:04:19 -0700130 * @v9ses: 9P session info (for determining extended mode)
Latchesar Ionkov531b1092006-01-08 01:05:00 -0800131 * @wstat: structure to initialize
Eric Van Hensbergen2bad8472005-09-09 13:04:19 -0700132 *
133 */
134
Eric Van Hensbergen73c592b2005-09-09 13:04:26 -0700135static void
Latchesar Ionkov531b1092006-01-08 01:05:00 -0800136v9fs_blank_wstat(struct v9fs_wstat *wstat)
Eric Van Hensbergen2bad8472005-09-09 13:04:19 -0700137{
Latchesar Ionkov531b1092006-01-08 01:05:00 -0800138 wstat->type = ~0;
139 wstat->dev = ~0;
140 wstat->qid.type = ~0;
141 wstat->qid.version = ~0;
142 *((long long *)&wstat->qid.path) = ~0;
143 wstat->mode = ~0;
144 wstat->atime = ~0;
145 wstat->mtime = ~0;
146 wstat->length = ~0;
147 wstat->name = NULL;
148 wstat->uid = NULL;
149 wstat->gid = NULL;
150 wstat->muid = NULL;
151 wstat->n_uid = ~0;
152 wstat->n_gid = ~0;
153 wstat->n_muid = ~0;
154 wstat->extension = NULL;
Eric Van Hensbergen2bad8472005-09-09 13:04:19 -0700155}
156
157/**
158 * v9fs_get_inode - helper function to setup an inode
159 * @sb: superblock
160 * @mode: mode to setup inode with
161 *
162 */
163
164struct inode *v9fs_get_inode(struct super_block *sb, int mode)
165{
166 struct inode *inode = NULL;
Eric Van Hensbergenb5016112005-09-09 13:04:27 -0700167 struct v9fs_session_info *v9ses = sb->s_fs_info;
Eric Van Hensbergen2bad8472005-09-09 13:04:19 -0700168
169 dprintk(DEBUG_VFS, "super block: %p mode: %o\n", sb, mode);
170
171 inode = new_inode(sb);
172 if (inode) {
173 inode->i_mode = mode;
174 inode->i_uid = current->fsuid;
175 inode->i_gid = current->fsgid;
176 inode->i_blksize = sb->s_blocksize;
177 inode->i_blocks = 0;
178 inode->i_rdev = 0;
179 inode->i_atime = inode->i_mtime = inode->i_ctime = CURRENT_TIME;
Eric Van Hensbergen147b31c2006-01-18 17:43:02 -0800180 inode->i_mapping->a_ops = &v9fs_addr_operations;
Eric Van Hensbergen2bad8472005-09-09 13:04:19 -0700181
182 switch (mode & S_IFMT) {
183 case S_IFIFO:
184 case S_IFBLK:
185 case S_IFCHR:
186 case S_IFSOCK:
Eric Van Hensbergenb5016112005-09-09 13:04:27 -0700187 if(!v9ses->extended) {
188 dprintk(DEBUG_ERROR, "special files without extended mode\n");
189 return ERR_PTR(-EINVAL);
190 }
Eric Van Hensbergen5d58bec2005-09-09 13:04:27 -0700191 init_special_inode(inode, inode->i_mode,
192 inode->i_rdev);
193 break;
Eric Van Hensbergen2bad8472005-09-09 13:04:19 -0700194 case S_IFREG:
195 inode->i_op = &v9fs_file_inode_operations;
196 inode->i_fop = &v9fs_file_operations;
197 break;
Eric Van Hensbergenb5016112005-09-09 13:04:27 -0700198 case S_IFLNK:
199 if(!v9ses->extended) {
200 dprintk(DEBUG_ERROR, "extended modes used w/o 9P2000.u\n");
201 return ERR_PTR(-EINVAL);
202 }
203 inode->i_op = &v9fs_symlink_inode_operations;
204 break;
Eric Van Hensbergen2bad8472005-09-09 13:04:19 -0700205 case S_IFDIR:
206 inode->i_nlink++;
Eric Van Hensbergenb5016112005-09-09 13:04:27 -0700207 if(v9ses->extended)
208 inode->i_op = &v9fs_dir_inode_operations_ext;
209 else
210 inode->i_op = &v9fs_dir_inode_operations;
Eric Van Hensbergen2bad8472005-09-09 13:04:19 -0700211 inode->i_fop = &v9fs_dir_operations;
212 break;
Eric Van Hensbergen2bad8472005-09-09 13:04:19 -0700213 default:
214 dprintk(DEBUG_ERROR, "BAD mode 0x%x S_IFMT 0x%x\n",
215 mode, mode & S_IFMT);
216 return ERR_PTR(-EINVAL);
217 }
218 } else {
219 eprintk(KERN_WARNING, "Problem allocating inode\n");
220 return ERR_PTR(-ENOMEM);
221 }
222 return inode;
223}
224
225/**
226 * v9fs_create - helper function to create files and directories
227 * @dir: directory inode file is being created in
228 * @file_dentry: dentry file is being created in
229 * @perm: permissions file is being created with
Eric Van Hensbergen73c592b2005-09-09 13:04:26 -0700230 * @open_mode: resulting open mode for file
Eric Van Hensbergen2bad8472005-09-09 13:04:19 -0700231 *
232 */
233
234static int
235v9fs_create(struct inode *dir,
236 struct dentry *file_dentry,
237 unsigned int perm, unsigned int open_mode)
238{
239 struct v9fs_session_info *v9ses = v9fs_inode2v9ses(dir);
240 struct super_block *sb = dir->i_sb;
241 struct v9fs_fid *dirfid =
Latchesar Ionkov0b8dd172005-09-27 21:45:24 -0700242 v9fs_fid_lookup(file_dentry->d_parent);
Eric Van Hensbergen2bad8472005-09-09 13:04:19 -0700243 struct v9fs_fid *fid = NULL;
244 struct inode *file_inode = NULL;
245 struct v9fs_fcall *fcall = NULL;
246 struct v9fs_qid qid;
Eric Van Hensbergen2bad8472005-09-09 13:04:19 -0700247 int dirfidnum = -1;
248 long newfid = -1;
249 int result = 0;
250 unsigned int iounit = 0;
Latchesar Ionkov0b8dd172005-09-27 21:45:24 -0700251 int wfidno = -1;
Latchesar Ionkov3cf64292006-01-08 01:04:58 -0800252 int err;
Eric Van Hensbergen2bad8472005-09-09 13:04:19 -0700253
254 perm = unixmode2p9mode(v9ses, perm);
255
256 dprintk(DEBUG_VFS, "dir: %p dentry: %p perm: %o mode: %o\n", dir,
257 file_dentry, perm, open_mode);
258
259 if (!dirfid)
260 return -EBADF;
261
262 dirfidnum = dirfid->fid;
263 if (dirfidnum < 0) {
264 dprintk(DEBUG_ERROR, "No fid for the directory #%lu\n",
265 dir->i_ino);
266 return -EBADF;
267 }
268
269 if (file_dentry->d_inode) {
270 dprintk(DEBUG_ERROR,
271 "Odd. There is an inode for dir %lu, name :%s:\n",
272 dir->i_ino, file_dentry->d_name.name);
273 return -EEXIST;
274 }
275
276 newfid = v9fs_get_idpool(&v9ses->fidpool);
277 if (newfid < 0) {
278 eprintk(KERN_WARNING, "no free fids available\n");
279 return -ENOSPC;
280 }
281
282 result = v9fs_t_walk(v9ses, dirfidnum, newfid, NULL, &fcall);
283 if (result < 0) {
Latchesar Ionkov531b1092006-01-08 01:05:00 -0800284 PRINT_FCALL_ERROR("clone error", fcall);
Eric Van Hensbergen2bad8472005-09-09 13:04:19 -0700285 v9fs_put_idpool(newfid, &v9ses->fidpool);
Latchesar Ionkov0b8dd172005-09-27 21:45:24 -0700286 newfid = -1;
Eric Van Hensbergen2bad8472005-09-09 13:04:19 -0700287 goto CleanUpFid;
288 }
289
290 kfree(fcall);
Latchesar Ionkov3cf64292006-01-08 01:04:58 -0800291 fcall = NULL;
Eric Van Hensbergen2bad8472005-09-09 13:04:19 -0700292
293 result = v9fs_t_create(v9ses, newfid, (char *)file_dentry->d_name.name,
294 perm, open_mode, &fcall);
295 if (result < 0) {
Latchesar Ionkov531b1092006-01-08 01:05:00 -0800296 PRINT_FCALL_ERROR("create fails", fcall);
Eric Van Hensbergen2bad8472005-09-09 13:04:19 -0700297 goto CleanUpFid;
298 }
299
300 iounit = fcall->params.rcreate.iounit;
301 qid = fcall->params.rcreate.qid;
302 kfree(fcall);
Latchesar Ionkov3cf64292006-01-08 01:04:58 -0800303 fcall = NULL;
Eric Van Hensbergen2bad8472005-09-09 13:04:19 -0700304
Latchesar Ionkov3cf64292006-01-08 01:04:58 -0800305 if (!(perm&V9FS_DMDIR)) {
306 fid = v9fs_fid_create(file_dentry, v9ses, newfid, 1);
307 dprintk(DEBUG_VFS, "fid %p %d\n", fid, fid->fidcreate);
308 if (!fid) {
309 result = -ENOMEM;
310 goto CleanUpFid;
311 }
312
313 fid->qid = qid;
314 fid->iounit = iounit;
315 } else {
316 err = v9fs_t_clunk(v9ses, newfid);
Latchesar Ionkovd8da0972006-01-08 01:04:59 -0800317 newfid = -1;
Latchesar Ionkov3cf64292006-01-08 01:04:58 -0800318 if (err < 0)
319 dprintk(DEBUG_ERROR, "clunk for mkdir failed: %d\n", err);
Eric Van Hensbergen2bad8472005-09-09 13:04:19 -0700320 }
321
Latchesar Ionkov0b8dd172005-09-27 21:45:24 -0700322 /* walk to the newly created file and put the fid in the dentry */
323 wfidno = v9fs_get_idpool(&v9ses->fidpool);
Latchesar Ionkovd8da0972006-01-08 01:04:59 -0800324 if (wfidno < 0) {
Latchesar Ionkov0b8dd172005-09-27 21:45:24 -0700325 eprintk(KERN_WARNING, "no free fids available\n");
326 return -ENOSPC;
327 }
328
329 result = v9fs_t_walk(v9ses, dirfidnum, wfidno,
Latchesar Ionkov3cf64292006-01-08 01:04:58 -0800330 (char *) file_dentry->d_name.name, &fcall);
Latchesar Ionkov0b8dd172005-09-27 21:45:24 -0700331 if (result < 0) {
Latchesar Ionkov531b1092006-01-08 01:05:00 -0800332 PRINT_FCALL_ERROR("clone error", fcall);
Latchesar Ionkov0b8dd172005-09-27 21:45:24 -0700333 v9fs_put_idpool(wfidno, &v9ses->fidpool);
334 wfidno = -1;
335 goto CleanUpFid;
336 }
Latchesar Ionkov3cf64292006-01-08 01:04:58 -0800337 kfree(fcall);
338 fcall = NULL;
Latchesar Ionkov0b8dd172005-09-27 21:45:24 -0700339
340 if (!v9fs_fid_create(file_dentry, v9ses, wfidno, 0)) {
Latchesar Ionkov3cf64292006-01-08 01:04:58 -0800341 v9fs_put_idpool(wfidno, &v9ses->fidpool);
Latchesar Ionkov0b8dd172005-09-27 21:45:24 -0700342
343 goto CleanUpFid;
344 }
Eric Van Hensbergen2bad8472005-09-09 13:04:19 -0700345
346 if ((perm & V9FS_DMSYMLINK) || (perm & V9FS_DMLINK) ||
347 (perm & V9FS_DMNAMEDPIPE) || (perm & V9FS_DMSOCKET) ||
348 (perm & V9FS_DMDEVICE))
349 return 0;
350
Latchesar Ionkovd8da0972006-01-08 01:04:59 -0800351 result = v9fs_t_stat(v9ses, wfidno, &fcall);
Eric Van Hensbergen2bad8472005-09-09 13:04:19 -0700352 if (result < 0) {
Latchesar Ionkov531b1092006-01-08 01:05:00 -0800353 PRINT_FCALL_ERROR("stat error", fcall);
Eric Van Hensbergen2bad8472005-09-09 13:04:19 -0700354 goto CleanUpFid;
355 }
356
Eric Van Hensbergen2bad8472005-09-09 13:04:19 -0700357
Latchesar Ionkov531b1092006-01-08 01:05:00 -0800358 file_inode = v9fs_get_inode(sb,
359 p9mode2unixmode(v9ses, fcall->params.rstat.stat.mode));
360
Eric Van Hensbergen2bad8472005-09-09 13:04:19 -0700361 if ((!file_inode) || IS_ERR(file_inode)) {
362 dprintk(DEBUG_ERROR, "create inode failed\n");
363 result = -EBADF;
364 goto CleanUpFid;
365 }
366
Latchesar Ionkov531b1092006-01-08 01:05:00 -0800367 v9fs_stat2inode(&fcall->params.rstat.stat, file_inode, sb);
Eric Van Hensbergen2bad8472005-09-09 13:04:19 -0700368 kfree(fcall);
Latchesar Ionkova93a1172005-11-28 13:44:05 -0800369 fcall = NULL;
370 file_dentry->d_op = &v9fs_dentry_operations;
Eric Van Hensbergen2bad8472005-09-09 13:04:19 -0700371 d_instantiate(file_dentry, file_inode);
372
Eric Van Hensbergen2bad8472005-09-09 13:04:19 -0700373 return 0;
374
375 CleanUpFid:
376 kfree(fcall);
Latchesar Ionkov3cf64292006-01-08 01:04:58 -0800377 fcall = NULL;
Eric Van Hensbergen2bad8472005-09-09 13:04:19 -0700378
Latchesar Ionkov0b8dd172005-09-27 21:45:24 -0700379 if (newfid >= 0) {
Latchesar Ionkov3cf64292006-01-08 01:04:58 -0800380 err = v9fs_t_clunk(v9ses, newfid);
381 if (err < 0)
382 dprintk(DEBUG_ERROR, "clunk failed: %d\n", err);
Latchesar Ionkov0b8dd172005-09-27 21:45:24 -0700383 }
384 if (wfidno >= 0) {
Latchesar Ionkov3cf64292006-01-08 01:04:58 -0800385 err = v9fs_t_clunk(v9ses, wfidno);
386 if (err < 0)
387 dprintk(DEBUG_ERROR, "clunk failed: %d\n", err);
Eric Van Hensbergen2bad8472005-09-09 13:04:19 -0700388 }
389 return result;
390}
391
392/**
393 * v9fs_remove - helper function to remove files and directories
Eric Van Hensbergen73c592b2005-09-09 13:04:26 -0700394 * @dir: directory inode that is being deleted
395 * @file: dentry that is being deleted
396 * @rmdir: removing a directory
Eric Van Hensbergen2bad8472005-09-09 13:04:19 -0700397 *
398 */
399
400static int v9fs_remove(struct inode *dir, struct dentry *file, int rmdir)
401{
402 struct v9fs_fcall *fcall = NULL;
403 struct super_block *sb = NULL;
404 struct v9fs_session_info *v9ses = NULL;
405 struct v9fs_fid *v9fid = NULL;
406 struct inode *file_inode = NULL;
407 int fid = -1;
408 int result = 0;
409
410 dprintk(DEBUG_VFS, "inode: %p dentry: %p rmdir: %d\n", dir, file,
411 rmdir);
412
413 file_inode = file->d_inode;
414 sb = file_inode->i_sb;
415 v9ses = v9fs_inode2v9ses(file_inode);
Latchesar Ionkov0b8dd172005-09-27 21:45:24 -0700416 v9fid = v9fs_fid_lookup(file);
Eric Van Hensbergen2bad8472005-09-09 13:04:19 -0700417
418 if (!v9fid) {
419 dprintk(DEBUG_ERROR,
420 "no v9fs_fid\n");
421 return -EBADF;
422 }
423
424 fid = v9fid->fid;
425 if (fid < 0) {
426 dprintk(DEBUG_ERROR, "inode #%lu, no fid!\n",
427 file_inode->i_ino);
428 return -EBADF;
429 }
430
431 result = v9fs_t_remove(v9ses, fid, &fcall);
Latchesar Ionkov531b1092006-01-08 01:05:00 -0800432 if (result < 0) {
433 PRINT_FCALL_ERROR("remove fails", fcall);
434 } else {
Eric Van Hensbergen2bad8472005-09-09 13:04:19 -0700435 v9fs_put_idpool(fid, &v9ses->fidpool);
436 v9fs_fid_destroy(v9fid);
437 }
438
439 kfree(fcall);
440 return result;
441}
442
443/**
444 * v9fs_vfs_create - VFS hook to create files
445 * @inode: directory inode that is being deleted
446 * @dentry: dentry that is being deleted
447 * @perm: create permissions
448 * @nd: path information
449 *
450 */
451
452static int
453v9fs_vfs_create(struct inode *inode, struct dentry *dentry, int perm,
454 struct nameidata *nd)
455{
456 return v9fs_create(inode, dentry, perm, O_RDWR);
457}
458
459/**
460 * v9fs_vfs_mkdir - VFS mkdir hook to create a directory
Eric Van Hensbergen73c592b2005-09-09 13:04:26 -0700461 * @inode: inode that is being unlinked
Eric Van Hensbergen2bad8472005-09-09 13:04:19 -0700462 * @dentry: dentry that is being unlinked
463 * @mode: mode for new directory
464 *
465 */
466
467static int v9fs_vfs_mkdir(struct inode *inode, struct dentry *dentry, int mode)
468{
469 return v9fs_create(inode, dentry, mode | S_IFDIR, O_RDONLY);
470}
471
472/**
473 * v9fs_vfs_lookup - VFS lookup hook to "walk" to a new inode
474 * @dir: inode that is being walked from
475 * @dentry: dentry that is being walked to?
476 * @nameidata: path data
477 *
478 */
479
480static struct dentry *v9fs_vfs_lookup(struct inode *dir, struct dentry *dentry,
481 struct nameidata *nameidata)
482{
483 struct super_block *sb;
484 struct v9fs_session_info *v9ses;
485 struct v9fs_fid *dirfid;
486 struct v9fs_fid *fid;
487 struct inode *inode;
488 struct v9fs_fcall *fcall = NULL;
Eric Van Hensbergen2bad8472005-09-09 13:04:19 -0700489 int dirfidnum = -1;
490 int newfid = -1;
491 int result = 0;
492
493 dprintk(DEBUG_VFS, "dir: %p dentry: (%s) %p nameidata: %p\n",
494 dir, dentry->d_iname, dentry, nameidata);
495
496 sb = dir->i_sb;
497 v9ses = v9fs_inode2v9ses(dir);
Latchesar Ionkov0b8dd172005-09-27 21:45:24 -0700498 dirfid = v9fs_fid_lookup(dentry->d_parent);
Eric Van Hensbergen2bad8472005-09-09 13:04:19 -0700499
500 if (!dirfid) {
501 dprintk(DEBUG_ERROR, "no dirfid\n");
502 return ERR_PTR(-EINVAL);
503 }
504
505 dirfidnum = dirfid->fid;
506
507 if (dirfidnum < 0) {
508 dprintk(DEBUG_ERROR, "no dirfid for inode %p, #%lu\n",
509 dir, dir->i_ino);
510 return ERR_PTR(-EBADF);
511 }
512
513 newfid = v9fs_get_idpool(&v9ses->fidpool);
514 if (newfid < 0) {
515 eprintk(KERN_WARNING, "newfid fails!\n");
516 return ERR_PTR(-ENOSPC);
517 }
518
519 result =
520 v9fs_t_walk(v9ses, dirfidnum, newfid, (char *)dentry->d_name.name,
521 NULL);
522 if (result < 0) {
523 v9fs_put_idpool(newfid, &v9ses->fidpool);
524 if (result == -ENOENT) {
525 d_add(dentry, NULL);
Latchesar Ionkov0b8dd172005-09-27 21:45:24 -0700526 dprintk(DEBUG_VFS,
Eric Van Hensbergen2bad8472005-09-09 13:04:19 -0700527 "Return negative dentry %p count %d\n",
528 dentry, atomic_read(&dentry->d_count));
529 return NULL;
530 }
531 dprintk(DEBUG_ERROR, "walk error:%d\n", result);
532 goto FreeFcall;
533 }
534
535 result = v9fs_t_stat(v9ses, newfid, &fcall);
536 if (result < 0) {
537 dprintk(DEBUG_ERROR, "stat error\n");
538 goto FreeFcall;
539 }
540
Latchesar Ionkov531b1092006-01-08 01:05:00 -0800541 inode = v9fs_get_inode(sb, p9mode2unixmode(v9ses,
542 fcall->params.rstat.stat.mode));
Eric Van Hensbergen2bad8472005-09-09 13:04:19 -0700543
544 if (IS_ERR(inode) && (PTR_ERR(inode) == -ENOSPC)) {
545 eprintk(KERN_WARNING, "inode alloc failes, returns %ld\n",
546 PTR_ERR(inode));
547
548 result = -ENOSPC;
549 goto FreeFcall;
550 }
551
Latchesar Ionkov531b1092006-01-08 01:05:00 -0800552 inode->i_ino = v9fs_qid2ino(&fcall->params.rstat.stat.qid);
Eric Van Hensbergen2bad8472005-09-09 13:04:19 -0700553
Latchesar Ionkov0b8dd172005-09-27 21:45:24 -0700554 fid = v9fs_fid_create(dentry, v9ses, newfid, 0);
Eric Van Hensbergen2bad8472005-09-09 13:04:19 -0700555 if (fid == NULL) {
556 dprintk(DEBUG_ERROR, "couldn't insert\n");
557 result = -ENOMEM;
558 goto FreeFcall;
559 }
560
Latchesar Ionkov531b1092006-01-08 01:05:00 -0800561 fid->qid = fcall->params.rstat.stat.qid;
Eric Van Hensbergen2bad8472005-09-09 13:04:19 -0700562
563 dentry->d_op = &v9fs_dentry_operations;
Latchesar Ionkov531b1092006-01-08 01:05:00 -0800564 v9fs_stat2inode(&fcall->params.rstat.stat, inode, inode->i_sb);
Eric Van Hensbergen2bad8472005-09-09 13:04:19 -0700565
566 d_add(dentry, inode);
567 kfree(fcall);
568
569 return NULL;
570
571 FreeFcall:
572 kfree(fcall);
573 return ERR_PTR(result);
574}
575
576/**
577 * v9fs_vfs_unlink - VFS unlink hook to delete an inode
578 * @i: inode that is being unlinked
Eric Van Hensbergen73c592b2005-09-09 13:04:26 -0700579 * @d: dentry that is being unlinked
Eric Van Hensbergen2bad8472005-09-09 13:04:19 -0700580 *
581 */
582
583static int v9fs_vfs_unlink(struct inode *i, struct dentry *d)
584{
585 return v9fs_remove(i, d, 0);
586}
587
588/**
589 * v9fs_vfs_rmdir - VFS unlink hook to delete a directory
590 * @i: inode that is being unlinked
Eric Van Hensbergen73c592b2005-09-09 13:04:26 -0700591 * @d: dentry that is being unlinked
Eric Van Hensbergen2bad8472005-09-09 13:04:19 -0700592 *
593 */
594
595static int v9fs_vfs_rmdir(struct inode *i, struct dentry *d)
596{
597 return v9fs_remove(i, d, 1);
598}
599
600/**
601 * v9fs_vfs_rename - VFS hook to rename an inode
602 * @old_dir: old dir inode
603 * @old_dentry: old dentry
604 * @new_dir: new dir inode
605 * @new_dentry: new dentry
606 *
607 */
608
609static int
610v9fs_vfs_rename(struct inode *old_dir, struct dentry *old_dentry,
611 struct inode *new_dir, struct dentry *new_dentry)
612{
613 struct inode *old_inode = old_dentry->d_inode;
614 struct v9fs_session_info *v9ses = v9fs_inode2v9ses(old_inode);
Latchesar Ionkov0b8dd172005-09-27 21:45:24 -0700615 struct v9fs_fid *oldfid = v9fs_fid_lookup(old_dentry);
Eric Van Hensbergen2bad8472005-09-09 13:04:19 -0700616 struct v9fs_fid *olddirfid =
Latchesar Ionkov0b8dd172005-09-27 21:45:24 -0700617 v9fs_fid_lookup(old_dentry->d_parent);
Eric Van Hensbergen2bad8472005-09-09 13:04:19 -0700618 struct v9fs_fid *newdirfid =
Latchesar Ionkov0b8dd172005-09-27 21:45:24 -0700619 v9fs_fid_lookup(new_dentry->d_parent);
Latchesar Ionkov531b1092006-01-08 01:05:00 -0800620 struct v9fs_wstat wstat;
Eric Van Hensbergen2bad8472005-09-09 13:04:19 -0700621 struct v9fs_fcall *fcall = NULL;
622 int fid = -1;
623 int olddirfidnum = -1;
624 int newdirfidnum = -1;
625 int retval = 0;
626
627 dprintk(DEBUG_VFS, "\n");
628
629 if ((!oldfid) || (!olddirfid) || (!newdirfid)) {
630 dprintk(DEBUG_ERROR, "problem with arguments\n");
631 return -EBADF;
632 }
633
634 /* 9P can only handle file rename in the same directory */
635 if (memcmp(&olddirfid->qid, &newdirfid->qid, sizeof(newdirfid->qid))) {
636 dprintk(DEBUG_ERROR, "old dir and new dir are different\n");
637 retval = -EPERM;
638 goto FreeFcallnBail;
639 }
640
641 fid = oldfid->fid;
642 olddirfidnum = olddirfid->fid;
643 newdirfidnum = newdirfid->fid;
644
645 if (fid < 0) {
646 dprintk(DEBUG_ERROR, "no fid for old file #%lu\n",
647 old_inode->i_ino);
648 retval = -EBADF;
649 goto FreeFcallnBail;
650 }
651
Latchesar Ionkov531b1092006-01-08 01:05:00 -0800652 v9fs_blank_wstat(&wstat);
653 wstat.muid = v9ses->name;
654 wstat.name = (char *) new_dentry->d_name.name;
Eric Van Hensbergen2bad8472005-09-09 13:04:19 -0700655
Latchesar Ionkov531b1092006-01-08 01:05:00 -0800656 retval = v9fs_t_wstat(v9ses, fid, &wstat, &fcall);
Eric Van Hensbergen2bad8472005-09-09 13:04:19 -0700657
658 FreeFcallnBail:
Eric Van Hensbergen2bad8472005-09-09 13:04:19 -0700659 if (retval < 0)
Latchesar Ionkov531b1092006-01-08 01:05:00 -0800660 PRINT_FCALL_ERROR("wstat error", fcall);
Eric Van Hensbergen2bad8472005-09-09 13:04:19 -0700661
662 kfree(fcall);
663 return retval;
664}
665
666/**
Adrian Bunk943ffb52006-01-10 00:10:13 +0100667 * v9fs_vfs_getattr - retrieve file metadata
Eric Van Hensbergen2bad8472005-09-09 13:04:19 -0700668 * @mnt - mount information
669 * @dentry - file to get attributes on
670 * @stat - metadata structure to populate
671 *
672 */
673
674static int
675v9fs_vfs_getattr(struct vfsmount *mnt, struct dentry *dentry,
676 struct kstat *stat)
677{
678 struct v9fs_fcall *fcall = NULL;
679 struct v9fs_session_info *v9ses = v9fs_inode2v9ses(dentry->d_inode);
Latchesar Ionkov0b8dd172005-09-27 21:45:24 -0700680 struct v9fs_fid *fid = v9fs_fid_lookup(dentry);
Eric Van Hensbergen2bad8472005-09-09 13:04:19 -0700681 int err = -EPERM;
682
683 dprintk(DEBUG_VFS, "dentry: %p\n", dentry);
684 if (!fid) {
685 dprintk(DEBUG_ERROR,
686 "couldn't find fid associated with dentry\n");
687 return -EBADF;
688 }
689
690 err = v9fs_t_stat(v9ses, fid->fid, &fcall);
691
692 if (err < 0)
693 dprintk(DEBUG_ERROR, "stat error\n");
694 else {
Latchesar Ionkov531b1092006-01-08 01:05:00 -0800695 v9fs_stat2inode(&fcall->params.rstat.stat, dentry->d_inode,
Eric Van Hensbergen2bad8472005-09-09 13:04:19 -0700696 dentry->d_inode->i_sb);
697 generic_fillattr(dentry->d_inode, stat);
698 }
699
700 kfree(fcall);
701 return err;
702}
703
704/**
705 * v9fs_vfs_setattr - set file metadata
706 * @dentry: file whose metadata to set
707 * @iattr: metadata assignment structure
708 *
709 */
710
711static int v9fs_vfs_setattr(struct dentry *dentry, struct iattr *iattr)
712{
713 struct v9fs_session_info *v9ses = v9fs_inode2v9ses(dentry->d_inode);
Latchesar Ionkov0b8dd172005-09-27 21:45:24 -0700714 struct v9fs_fid *fid = v9fs_fid_lookup(dentry);
Eric Van Hensbergen2bad8472005-09-09 13:04:19 -0700715 struct v9fs_fcall *fcall = NULL;
Latchesar Ionkov531b1092006-01-08 01:05:00 -0800716 struct v9fs_wstat wstat;
Eric Van Hensbergen2bad8472005-09-09 13:04:19 -0700717 int res = -EPERM;
718
719 dprintk(DEBUG_VFS, "\n");
Eric Van Hensbergen73c592b2005-09-09 13:04:26 -0700720
Eric Van Hensbergen2bad8472005-09-09 13:04:19 -0700721 if (!fid) {
722 dprintk(DEBUG_ERROR,
723 "Couldn't find fid associated with dentry\n");
724 return -EBADF;
725 }
726
Latchesar Ionkov531b1092006-01-08 01:05:00 -0800727 v9fs_blank_wstat(&wstat);
Eric Van Hensbergen2bad8472005-09-09 13:04:19 -0700728 if (iattr->ia_valid & ATTR_MODE)
Latchesar Ionkov531b1092006-01-08 01:05:00 -0800729 wstat.mode = unixmode2p9mode(v9ses, iattr->ia_mode);
Eric Van Hensbergen2bad8472005-09-09 13:04:19 -0700730
731 if (iattr->ia_valid & ATTR_MTIME)
Latchesar Ionkov531b1092006-01-08 01:05:00 -0800732 wstat.mtime = iattr->ia_mtime.tv_sec;
Eric Van Hensbergen2bad8472005-09-09 13:04:19 -0700733
734 if (iattr->ia_valid & ATTR_ATIME)
Latchesar Ionkov531b1092006-01-08 01:05:00 -0800735 wstat.atime = iattr->ia_atime.tv_sec;
Eric Van Hensbergen2bad8472005-09-09 13:04:19 -0700736
737 if (iattr->ia_valid & ATTR_SIZE)
Latchesar Ionkov531b1092006-01-08 01:05:00 -0800738 wstat.length = iattr->ia_size;
Eric Van Hensbergen2bad8472005-09-09 13:04:19 -0700739
740 if (v9ses->extended) {
Latchesar Ionkov531b1092006-01-08 01:05:00 -0800741 if (iattr->ia_valid & ATTR_UID)
742 wstat.n_uid = iattr->ia_uid;
Eric Van Hensbergen2bad8472005-09-09 13:04:19 -0700743
Latchesar Ionkov531b1092006-01-08 01:05:00 -0800744 if (iattr->ia_valid & ATTR_GID)
745 wstat.n_gid = iattr->ia_gid;
Eric Van Hensbergen2bad8472005-09-09 13:04:19 -0700746 }
747
Latchesar Ionkov531b1092006-01-08 01:05:00 -0800748 res = v9fs_t_wstat(v9ses, fid->fid, &wstat, &fcall);
Eric Van Hensbergen2bad8472005-09-09 13:04:19 -0700749
750 if (res < 0)
Latchesar Ionkov531b1092006-01-08 01:05:00 -0800751 PRINT_FCALL_ERROR("wstat error", fcall);
Eric Van Hensbergen2bad8472005-09-09 13:04:19 -0700752
Eric Van Hensbergen2bad8472005-09-09 13:04:19 -0700753 kfree(fcall);
Eric Van Hensbergen2bad8472005-09-09 13:04:19 -0700754 if (res >= 0)
755 res = inode_setattr(dentry->d_inode, iattr);
756
757 return res;
758}
759
760/**
Latchesar Ionkov531b1092006-01-08 01:05:00 -0800761 * v9fs_stat2inode - populate an inode structure with mistat info
762 * @stat: Plan 9 metadata (mistat) structure
Eric Van Hensbergen2bad8472005-09-09 13:04:19 -0700763 * @inode: inode to populate
764 * @sb: superblock of filesystem
765 *
766 */
767
768void
Latchesar Ionkov531b1092006-01-08 01:05:00 -0800769v9fs_stat2inode(struct v9fs_stat *stat, struct inode *inode,
770 struct super_block *sb)
Eric Van Hensbergen2bad8472005-09-09 13:04:19 -0700771{
Latchesar Ionkov1dac06b2006-01-08 01:05:02 -0800772 int n;
Latchesar Ionkov531b1092006-01-08 01:05:00 -0800773 char ext[32];
Eric Van Hensbergen2bad8472005-09-09 13:04:19 -0700774 struct v9fs_session_info *v9ses = sb->s_fs_info;
775
776 inode->i_nlink = 1;
777
Latchesar Ionkov531b1092006-01-08 01:05:00 -0800778 inode->i_atime.tv_sec = stat->atime;
779 inode->i_mtime.tv_sec = stat->mtime;
780 inode->i_ctime.tv_sec = stat->mtime;
Eric Van Hensbergen2bad8472005-09-09 13:04:19 -0700781
Latchesar Ionkov531b1092006-01-08 01:05:00 -0800782 inode->i_uid = v9ses->uid;
783 inode->i_gid = v9ses->gid;
Eric Van Hensbergen2bad8472005-09-09 13:04:19 -0700784
785 if (v9ses->extended) {
Latchesar Ionkov531b1092006-01-08 01:05:00 -0800786 inode->i_uid = stat->n_uid;
787 inode->i_gid = stat->n_gid;
Eric Van Hensbergen2bad8472005-09-09 13:04:19 -0700788 }
789
Latchesar Ionkov531b1092006-01-08 01:05:00 -0800790 inode->i_mode = p9mode2unixmode(v9ses, stat->mode);
Eric Van Hensbergen2bad8472005-09-09 13:04:19 -0700791 if ((S_ISBLK(inode->i_mode)) || (S_ISCHR(inode->i_mode))) {
792 char type = 0;
793 int major = -1;
794 int minor = -1;
Latchesar Ionkov531b1092006-01-08 01:05:00 -0800795
Latchesar Ionkov1dac06b2006-01-08 01:05:02 -0800796 n = stat->extension.len;
797 if (n > sizeof(ext)-1)
798 n = sizeof(ext)-1;
799 memmove(ext, stat->extension.str, n);
800 ext[n] = 0;
Latchesar Ionkov531b1092006-01-08 01:05:00 -0800801 sscanf(ext, "%c %u %u", &type, &major, &minor);
Eric Van Hensbergen2bad8472005-09-09 13:04:19 -0700802 switch (type) {
803 case 'c':
804 inode->i_mode &= ~S_IFBLK;
805 inode->i_mode |= S_IFCHR;
806 break;
807 case 'b':
808 break;
809 default:
Latchesar Ionkov531b1092006-01-08 01:05:00 -0800810 dprintk(DEBUG_ERROR, "Unknown special type %c (%.*s)\n",
811 type, stat->extension.len, stat->extension.str);
Eric Van Hensbergen2bad8472005-09-09 13:04:19 -0700812 };
813 inode->i_rdev = MKDEV(major, minor);
814 } else
815 inode->i_rdev = 0;
816
Latchesar Ionkov531b1092006-01-08 01:05:00 -0800817 inode->i_size = stat->length;
Eric Van Hensbergen2bad8472005-09-09 13:04:19 -0700818
819 inode->i_blksize = sb->s_blocksize;
820 inode->i_blocks =
821 (inode->i_size + inode->i_blksize - 1) >> sb->s_blocksize_bits;
822}
823
824/**
825 * v9fs_qid2ino - convert qid into inode number
826 * @qid: qid to hash
827 *
828 * BUG: potential for inode number collisions?
829 */
830
831ino_t v9fs_qid2ino(struct v9fs_qid *qid)
832{
833 u64 path = qid->path + 2;
834 ino_t i = 0;
835
836 if (sizeof(ino_t) == sizeof(path))
837 memcpy(&i, &path, sizeof(ino_t));
838 else
839 i = (ino_t) (path ^ (path >> 32));
840
841 return i;
842}
843
844/**
Eric Van Hensbergen2bad8472005-09-09 13:04:19 -0700845 * v9fs_readlink - read a symlink's location (internal version)
846 * @dentry: dentry for symlink
Eric Van Hensbergen73c592b2005-09-09 13:04:26 -0700847 * @buffer: buffer to load symlink location into
Eric Van Hensbergen2bad8472005-09-09 13:04:19 -0700848 * @buflen: length of buffer
849 *
850 */
851
852static int v9fs_readlink(struct dentry *dentry, char *buffer, int buflen)
853{
854 int retval = -EPERM;
855
856 struct v9fs_fcall *fcall = NULL;
857 struct v9fs_session_info *v9ses = v9fs_inode2v9ses(dentry->d_inode);
Latchesar Ionkov0b8dd172005-09-27 21:45:24 -0700858 struct v9fs_fid *fid = v9fs_fid_lookup(dentry);
Eric Van Hensbergen2bad8472005-09-09 13:04:19 -0700859
860 if (!fid) {
861 dprintk(DEBUG_ERROR, "could not resolve fid from dentry\n");
862 retval = -EBADF;
863 goto FreeFcall;
864 }
865
866 if (!v9ses->extended) {
867 retval = -EBADF;
868 dprintk(DEBUG_ERROR, "not extended\n");
869 goto FreeFcall;
870 }
871
872 dprintk(DEBUG_VFS, " %s\n", dentry->d_name.name);
873 retval = v9fs_t_stat(v9ses, fid->fid, &fcall);
874
875 if (retval < 0) {
876 dprintk(DEBUG_ERROR, "stat error\n");
877 goto FreeFcall;
878 }
879
880 if (!fcall)
881 return -EIO;
882
Latchesar Ionkov531b1092006-01-08 01:05:00 -0800883 if (!(fcall->params.rstat.stat.mode & V9FS_DMSYMLINK)) {
Eric Van Hensbergen2bad8472005-09-09 13:04:19 -0700884 retval = -EINVAL;
885 goto FreeFcall;
886 }
887
888 /* copy extension buffer into buffer */
Latchesar Ionkov531b1092006-01-08 01:05:00 -0800889 if (fcall->params.rstat.stat.extension.len < buflen)
890 buflen = fcall->params.rstat.stat.extension.len;
Eric Van Hensbergen2bad8472005-09-09 13:04:19 -0700891
Latchesar Ionkov531b1092006-01-08 01:05:00 -0800892 memcpy(buffer, fcall->params.rstat.stat.extension.str, buflen - 1);
893 buffer[buflen-1] = 0;
Eric Van Hensbergen2bad8472005-09-09 13:04:19 -0700894
895 retval = buflen;
896
897 FreeFcall:
898 kfree(fcall);
899
900 return retval;
901}
902
903/**
904 * v9fs_vfs_readlink - read a symlink's location
905 * @dentry: dentry for symlink
906 * @buf: buffer to load symlink location into
907 * @buflen: length of buffer
908 *
909 */
910
911static int v9fs_vfs_readlink(struct dentry *dentry, char __user * buffer,
912 int buflen)
913{
914 int retval;
915 int ret;
916 char *link = __getname();
917
Latchesar Ionkova1f9d8d2005-09-22 21:43:52 -0700918 if (buflen > PATH_MAX)
919 buflen = PATH_MAX;
Eric Van Hensbergen2bad8472005-09-09 13:04:19 -0700920
921 dprintk(DEBUG_VFS, " dentry: %s (%p)\n", dentry->d_iname, dentry);
922
923 retval = v9fs_readlink(dentry, link, buflen);
924
925 if (retval > 0) {
926 if ((ret = copy_to_user(buffer, link, retval)) != 0) {
927 dprintk(DEBUG_ERROR, "problem copying to user: %d\n",
928 ret);
929 retval = ret;
930 }
931 }
932
Davi Arnautce44eeb2005-11-07 00:59:36 -0800933 __putname(link);
Eric Van Hensbergen2bad8472005-09-09 13:04:19 -0700934 return retval;
935}
936
937/**
938 * v9fs_vfs_follow_link - follow a symlink path
939 * @dentry: dentry for symlink
940 * @nd: nameidata
941 *
942 */
943
944static void *v9fs_vfs_follow_link(struct dentry *dentry, struct nameidata *nd)
945{
946 int len = 0;
947 char *link = __getname();
948
949 dprintk(DEBUG_VFS, "%s n", dentry->d_name.name);
950
951 if (!link)
952 link = ERR_PTR(-ENOMEM);
953 else {
954 len = v9fs_readlink(dentry, link, strlen(link));
955
956 if (len < 0) {
Davi Arnautce44eeb2005-11-07 00:59:36 -0800957 __putname(link);
Eric Van Hensbergen2bad8472005-09-09 13:04:19 -0700958 link = ERR_PTR(len);
959 } else
960 link[len] = 0;
961 }
962 nd_set_link(nd, link);
963
964 return NULL;
965}
966
967/**
968 * v9fs_vfs_put_link - release a symlink path
969 * @dentry: dentry for symlink
970 * @nd: nameidata
971 *
972 */
973
974static void v9fs_vfs_put_link(struct dentry *dentry, struct nameidata *nd, void *p)
975{
976 char *s = nd_get_link(nd);
977
978 dprintk(DEBUG_VFS, " %s %s\n", dentry->d_name.name, s);
979 if (!IS_ERR(s))
Davi Arnautce44eeb2005-11-07 00:59:36 -0800980 __putname(s);
Eric Van Hensbergen2bad8472005-09-09 13:04:19 -0700981}
982
Latchesar Ionkov531b1092006-01-08 01:05:00 -0800983static int v9fs_vfs_mkspecial(struct inode *dir, struct dentry *dentry,
984 int mode, const char *extension)
985{
986 int err, retval;
987 struct v9fs_session_info *v9ses;
988 struct v9fs_fcall *fcall;
989 struct v9fs_fid *fid;
990 struct v9fs_wstat wstat;
991
992 v9ses = v9fs_inode2v9ses(dir);
993 retval = -EPERM;
994 fcall = NULL;
995
996 if (!v9ses->extended) {
997 dprintk(DEBUG_ERROR, "not extended\n");
998 goto free_mem;
999 }
1000
1001 /* issue a create */
1002 retval = v9fs_create(dir, dentry, mode, 0);
1003 if (retval != 0)
1004 goto free_mem;
1005
1006 fid = v9fs_fid_get_created(dentry);
1007 if (!fid) {
1008 dprintk(DEBUG_ERROR, "couldn't resolve fid from dentry\n");
1009 goto free_mem;
1010 }
1011
1012 /* issue a Twstat */
1013 v9fs_blank_wstat(&wstat);
1014 wstat.muid = v9ses->name;
1015 wstat.extension = (char *) extension;
1016 retval = v9fs_t_wstat(v9ses, fid->fid, &wstat, &fcall);
1017 if (retval < 0) {
1018 PRINT_FCALL_ERROR("wstat error", fcall);
1019 goto free_mem;
1020 }
1021
1022 err = v9fs_t_clunk(v9ses, fid->fid);
1023 if (err < 0) {
1024 dprintk(DEBUG_ERROR, "clunk failed: %d\n", err);
1025 goto free_mem;
1026 }
1027
1028 d_drop(dentry); /* FID - will this also clunk? */
1029
1030free_mem:
1031 kfree(fcall);
1032 return retval;
1033}
1034
1035/**
1036 * v9fs_vfs_symlink - helper function to create symlinks
1037 * @dir: directory inode containing symlink
1038 * @dentry: dentry for symlink
1039 * @symname: symlink data
1040 *
1041 * See 9P2000.u RFC for more information
1042 *
1043 */
1044
1045static int
1046v9fs_vfs_symlink(struct inode *dir, struct dentry *dentry, const char *symname)
1047{
1048 dprintk(DEBUG_VFS, " %lu,%s,%s\n", dir->i_ino, dentry->d_name.name,
1049 symname);
1050
1051 return v9fs_vfs_mkspecial(dir, dentry, S_IFLNK, symname);
1052}
1053
Eric Van Hensbergen2bad8472005-09-09 13:04:19 -07001054/**
1055 * v9fs_vfs_link - create a hardlink
1056 * @old_dentry: dentry for file to link to
1057 * @dir: inode destination for new link
Eric Van Hensbergen73c592b2005-09-09 13:04:26 -07001058 * @dentry: dentry for link
Eric Van Hensbergen2bad8472005-09-09 13:04:19 -07001059 *
1060 */
1061
1062/* XXX - lots of code dup'd from symlink and creates,
1063 * figure out a better reuse strategy
1064 */
1065
1066static int
1067v9fs_vfs_link(struct dentry *old_dentry, struct inode *dir,
1068 struct dentry *dentry)
1069{
Latchesar Ionkov531b1092006-01-08 01:05:00 -08001070 int retval;
1071 struct v9fs_fid *oldfid;
1072 char *name;
Eric Van Hensbergen2bad8472005-09-09 13:04:19 -07001073
1074 dprintk(DEBUG_VFS, " %lu,%s,%s\n", dir->i_ino, dentry->d_name.name,
1075 old_dentry->d_name.name);
1076
Latchesar Ionkov531b1092006-01-08 01:05:00 -08001077 oldfid = v9fs_fid_lookup(old_dentry);
1078 if (!oldfid) {
1079 dprintk(DEBUG_ERROR, "can't find oldfid\n");
1080 return -EPERM;
Eric Van Hensbergen2bad8472005-09-09 13:04:19 -07001081 }
1082
Latchesar Ionkov531b1092006-01-08 01:05:00 -08001083 name = __getname();
1084 sprintf(name, "hardlink(%d)\n", oldfid->fid);
1085 retval = v9fs_vfs_mkspecial(dir, dentry, V9FS_DMLINK, name);
1086 __putname(name);
Eric Van Hensbergen2bad8472005-09-09 13:04:19 -07001087
Eric Van Hensbergen2bad8472005-09-09 13:04:19 -07001088 return retval;
1089}
1090
1091/**
1092 * v9fs_vfs_mknod - create a special file
1093 * @dir: inode destination for new link
1094 * @dentry: dentry for file
1095 * @mode: mode for creation
1096 * @dev_t: device associated with special file
1097 *
1098 */
1099
1100static int
1101v9fs_vfs_mknod(struct inode *dir, struct dentry *dentry, int mode, dev_t rdev)
1102{
Latchesar Ionkov531b1092006-01-08 01:05:00 -08001103 int retval;
1104 char *name;
Eric Van Hensbergen2bad8472005-09-09 13:04:19 -07001105
1106 dprintk(DEBUG_VFS, " %lu,%s mode: %x MAJOR: %u MINOR: %u\n", dir->i_ino,
1107 dentry->d_name.name, mode, MAJOR(rdev), MINOR(rdev));
1108
Latchesar Ionkov531b1092006-01-08 01:05:00 -08001109 if (!new_valid_dev(rdev))
1110 return -EINVAL;
Eric Van Hensbergen73c592b2005-09-09 13:04:26 -07001111
Latchesar Ionkov531b1092006-01-08 01:05:00 -08001112 name = __getname();
Eric Van Hensbergen2bad8472005-09-09 13:04:19 -07001113 /* build extension */
1114 if (S_ISBLK(mode))
Latchesar Ionkov531b1092006-01-08 01:05:00 -08001115 sprintf(name, "b %u %u", MAJOR(rdev), MINOR(rdev));
Eric Van Hensbergen2bad8472005-09-09 13:04:19 -07001116 else if (S_ISCHR(mode))
Latchesar Ionkov531b1092006-01-08 01:05:00 -08001117 sprintf(name, "c %u %u", MAJOR(rdev), MINOR(rdev));
Eric Van Hensbergen73c592b2005-09-09 13:04:26 -07001118 else if (S_ISFIFO(mode))
Latchesar Ionkov531b1092006-01-08 01:05:00 -08001119 *name = 0;
Eric Van Hensbergen2bad8472005-09-09 13:04:19 -07001120 else {
Latchesar Ionkov531b1092006-01-08 01:05:00 -08001121 __putname(name);
1122 return -EINVAL;
Eric Van Hensbergen2bad8472005-09-09 13:04:19 -07001123 }
1124
Latchesar Ionkov531b1092006-01-08 01:05:00 -08001125 retval = v9fs_vfs_mkspecial(dir, dentry, mode, name);
1126 __putname(name);
Eric Van Hensbergen2bad8472005-09-09 13:04:19 -07001127
1128 return retval;
1129}
1130
Eric Van Hensbergenb5016112005-09-09 13:04:27 -07001131static struct inode_operations v9fs_dir_inode_operations_ext = {
Eric Van Hensbergen2bad8472005-09-09 13:04:19 -07001132 .create = v9fs_vfs_create,
1133 .lookup = v9fs_vfs_lookup,
1134 .symlink = v9fs_vfs_symlink,
1135 .link = v9fs_vfs_link,
1136 .unlink = v9fs_vfs_unlink,
1137 .mkdir = v9fs_vfs_mkdir,
1138 .rmdir = v9fs_vfs_rmdir,
1139 .mknod = v9fs_vfs_mknod,
1140 .rename = v9fs_vfs_rename,
1141 .readlink = v9fs_vfs_readlink,
1142 .getattr = v9fs_vfs_getattr,
1143 .setattr = v9fs_vfs_setattr,
1144};
1145
Eric Van Hensbergenb5016112005-09-09 13:04:27 -07001146static struct inode_operations v9fs_dir_inode_operations = {
1147 .create = v9fs_vfs_create,
1148 .lookup = v9fs_vfs_lookup,
1149 .unlink = v9fs_vfs_unlink,
1150 .mkdir = v9fs_vfs_mkdir,
1151 .rmdir = v9fs_vfs_rmdir,
1152 .mknod = v9fs_vfs_mknod,
1153 .rename = v9fs_vfs_rename,
1154 .getattr = v9fs_vfs_getattr,
1155 .setattr = v9fs_vfs_setattr,
1156};
1157
Eric Van Hensbergen2bad8472005-09-09 13:04:19 -07001158static struct inode_operations v9fs_file_inode_operations = {
1159 .getattr = v9fs_vfs_getattr,
1160 .setattr = v9fs_vfs_setattr,
1161};
1162
1163static struct inode_operations v9fs_symlink_inode_operations = {
1164 .readlink = v9fs_vfs_readlink,
1165 .follow_link = v9fs_vfs_follow_link,
1166 .put_link = v9fs_vfs_put_link,
1167 .getattr = v9fs_vfs_getattr,
1168 .setattr = v9fs_vfs_setattr,
1169};