]> bbs.cooldavid.org Git - net-next-2.6.git/blame - fs/9p/vfs_inode.c
fs/9p: Prevent parallel rename when doing fid_lookup
[net-next-2.6.git] / fs / 9p / vfs_inode.c
CommitLineData
2bad8471
EVH
1/*
2 * linux/fs/9p/vfs_inode.c
3 *
73c592b9 4 * This file contains vfs inode ops for the 9P2000 protocol.
2bad8471
EVH
5 *
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
42e8c509
EVH
10 * it under the terms of the GNU General Public License version 2
11 * as published by the Free Software Foundation.
2bad8471
EVH
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to:
20 * Free Software Foundation
21 * 51 Franklin Street, Fifth Floor
22 * Boston, MA 02111-1301 USA
23 *
24 */
25
26#include <linux/module.h>
27#include <linux/errno.h>
28#include <linux/fs.h>
29#include <linux/file.h>
30#include <linux/pagemap.h>
31#include <linux/stat.h>
32#include <linux/string.h>
2bad8471
EVH
33#include <linux/inet.h>
34#include <linux/namei.h>
35#include <linux/idr.h>
e8edc6e0 36#include <linux/sched.h>
5a0e3ad6 37#include <linux/slab.h>
ebf46264 38#include <linux/xattr.h>
bd238fb4
LI
39#include <net/9p/9p.h>
40#include <net/9p/client.h>
2bad8471 41
2bad8471 42#include "v9fs.h"
2bad8471 43#include "v9fs_vfs.h"
2bad8471 44#include "fid.h"
60e78d2c 45#include "cache.h"
ebf46264 46#include "xattr.h"
2bad8471 47
754661f1 48static const struct inode_operations v9fs_dir_inode_operations;
9b6533c9
SK
49static const struct inode_operations v9fs_dir_inode_operations_dotu;
50static const struct inode_operations v9fs_dir_inode_operations_dotl;
754661f1 51static const struct inode_operations v9fs_file_inode_operations;
9b6533c9 52static const struct inode_operations v9fs_file_inode_operations_dotl;
754661f1 53static const struct inode_operations v9fs_symlink_inode_operations;
9b6533c9 54static const struct inode_operations v9fs_symlink_inode_operations_dotl;
2bad8471
EVH
55
56/**
57 * unixmode2p9mode - convert unix mode bits to plan 9
58 * @v9ses: v9fs session information
59 * @mode: mode to convert
60 *
61 */
62
73c592b9 63static int unixmode2p9mode(struct v9fs_session_info *v9ses, int mode)
2bad8471
EVH
64{
65 int res;
66 res = mode & 0777;
67 if (S_ISDIR(mode))
bd238fb4 68 res |= P9_DMDIR;
dd6102fb 69 if (v9fs_proto_dotu(v9ses)) {
2bad8471 70 if (S_ISLNK(mode))
bd238fb4 71 res |= P9_DMSYMLINK;
2bad8471
EVH
72 if (v9ses->nodev == 0) {
73 if (S_ISSOCK(mode))
bd238fb4 74 res |= P9_DMSOCKET;
2bad8471 75 if (S_ISFIFO(mode))
bd238fb4 76 res |= P9_DMNAMEDPIPE;
2bad8471 77 if (S_ISBLK(mode))
bd238fb4 78 res |= P9_DMDEVICE;
2bad8471 79 if (S_ISCHR(mode))
bd238fb4 80 res |= P9_DMDEVICE;
2bad8471
EVH
81 }
82
83 if ((mode & S_ISUID) == S_ISUID)
bd238fb4 84 res |= P9_DMSETUID;
2bad8471 85 if ((mode & S_ISGID) == S_ISGID)
bd238fb4 86 res |= P9_DMSETGID;
d199d652
AL
87 if ((mode & S_ISVTX) == S_ISVTX)
88 res |= P9_DMSETVTX;
bd238fb4
LI
89 if ((mode & P9_DMLINK))
90 res |= P9_DMLINK;
2bad8471
EVH
91 }
92
93 return res;
94}
95
96/**
97 * p9mode2unixmode- convert plan9 mode bits to unix mode bits
98 * @v9ses: v9fs session information
99 * @mode: mode to convert
100 *
101 */
102
73c592b9 103static int p9mode2unixmode(struct v9fs_session_info *v9ses, int mode)
2bad8471
EVH
104{
105 int res;
106
107 res = mode & 0777;
108
bd238fb4 109 if ((mode & P9_DMDIR) == P9_DMDIR)
2bad8471 110 res |= S_IFDIR;
dd6102fb 111 else if ((mode & P9_DMSYMLINK) && (v9fs_proto_dotu(v9ses)))
2bad8471 112 res |= S_IFLNK;
dd6102fb 113 else if ((mode & P9_DMSOCKET) && (v9fs_proto_dotu(v9ses))
2bad8471
EVH
114 && (v9ses->nodev == 0))
115 res |= S_IFSOCK;
dd6102fb 116 else if ((mode & P9_DMNAMEDPIPE) && (v9fs_proto_dotu(v9ses))
2bad8471
EVH
117 && (v9ses->nodev == 0))
118 res |= S_IFIFO;
dd6102fb 119 else if ((mode & P9_DMDEVICE) && (v9fs_proto_dotu(v9ses))
2bad8471
EVH
120 && (v9ses->nodev == 0))
121 res |= S_IFBLK;
122 else
123 res |= S_IFREG;
124
dd6102fb 125 if (v9fs_proto_dotu(v9ses)) {
bd238fb4 126 if ((mode & P9_DMSETUID) == P9_DMSETUID)
2bad8471
EVH
127 res |= S_ISUID;
128
bd238fb4 129 if ((mode & P9_DMSETGID) == P9_DMSETGID)
2bad8471 130 res |= S_ISGID;
d199d652
AL
131
132 if ((mode & P9_DMSETVTX) == P9_DMSETVTX)
133 res |= S_ISVTX;
2bad8471
EVH
134 }
135
136 return res;
137}
138
ee443996
EVH
139/**
140 * v9fs_uflags2omode- convert posix open flags to plan 9 mode bits
141 * @uflags: flags to convert
2e4bef41 142 * @extended: if .u extensions are active
ee443996
EVH
143 */
144
2e4bef41 145int v9fs_uflags2omode(int uflags, int extended)
6a3124a3
LI
146{
147 int ret;
148
149 ret = 0;
150 switch (uflags&3) {
151 default:
152 case O_RDONLY:
bd238fb4 153 ret = P9_OREAD;
6a3124a3
LI
154 break;
155
156 case O_WRONLY:
bd238fb4 157 ret = P9_OWRITE;
6a3124a3
LI
158 break;
159
160 case O_RDWR:
bd238fb4 161 ret = P9_ORDWR;
6a3124a3
LI
162 break;
163 }
164
6a3124a3 165 if (uflags & O_TRUNC)
bd238fb4 166 ret |= P9_OTRUNC;
6a3124a3 167
2e4bef41
EVH
168 if (extended) {
169 if (uflags & O_EXCL)
170 ret |= P9_OEXCL;
171
172 if (uflags & O_APPEND)
173 ret |= P9_OAPPEND;
174 }
6a3124a3
LI
175
176 return ret;
177}
178
2bad8471 179/**
531b1094 180 * v9fs_blank_wstat - helper function to setup a 9P stat structure
531b1094 181 * @wstat: structure to initialize
2bad8471
EVH
182 *
183 */
184
7a4439c4 185void
bd238fb4 186v9fs_blank_wstat(struct p9_wstat *wstat)
2bad8471 187{
531b1094
LI
188 wstat->type = ~0;
189 wstat->dev = ~0;
190 wstat->qid.type = ~0;
191 wstat->qid.version = ~0;
192 *((long long *)&wstat->qid.path) = ~0;
193 wstat->mode = ~0;
194 wstat->atime = ~0;
195 wstat->mtime = ~0;
196 wstat->length = ~0;
197 wstat->name = NULL;
198 wstat->uid = NULL;
199 wstat->gid = NULL;
200 wstat->muid = NULL;
201 wstat->n_uid = ~0;
202 wstat->n_gid = ~0;
203 wstat->n_muid = ~0;
204 wstat->extension = NULL;
2bad8471
EVH
205}
206
60e78d2c
AK
207#ifdef CONFIG_9P_FSCACHE
208/**
209 * v9fs_alloc_inode - helper function to allocate an inode
210 * This callback is executed before setting up the inode so that we
211 * can associate a vcookie with each inode.
212 *
213 */
214
215struct inode *v9fs_alloc_inode(struct super_block *sb)
216{
217 struct v9fs_cookie *vcookie;
218 vcookie = (struct v9fs_cookie *)kmem_cache_alloc(vcookie_cache,
219 GFP_KERNEL);
220 if (!vcookie)
221 return NULL;
222
223 vcookie->fscache = NULL;
224 vcookie->qid = NULL;
225 spin_lock_init(&vcookie->lock);
226 return &vcookie->inode;
227}
228
229/**
230 * v9fs_destroy_inode - destroy an inode
231 *
232 */
233
234void v9fs_destroy_inode(struct inode *inode)
235{
236 kmem_cache_free(vcookie_cache, v9fs_inode2cookie(inode));
237}
238#endif
239
09d34ee5
EVH
240/**
241 * v9fs_get_fsgid_for_create - Helper function to get the gid for creating a
242 * new file system object. This checks the S_ISGID to determine the owning
243 * group of the new file system object.
244 */
245
246static gid_t v9fs_get_fsgid_for_create(struct inode *dir_inode)
247{
248 BUG_ON(dir_inode == NULL);
249
250 if (dir_inode->i_mode & S_ISGID) {
251 /* set_gid bit is set.*/
252 return dir_inode->i_gid;
253 }
254 return current_fsgid();
255}
256
257/**
258 * v9fs_dentry_from_dir_inode - helper function to get the dentry from
259 * dir inode.
260 *
261 */
262
263struct dentry *v9fs_dentry_from_dir_inode(struct inode *inode)
264{
265 struct dentry *dentry;
266
267 spin_lock(&dcache_lock);
268 /* Directory should have only one entry. */
269 BUG_ON(S_ISDIR(inode->i_mode) && !list_is_singular(&inode->i_dentry));
270 dentry = list_entry(inode->i_dentry.next, struct dentry, d_alias);
271 spin_unlock(&dcache_lock);
272 return dentry;
273}
274
2bad8471
EVH
275/**
276 * v9fs_get_inode - helper function to setup an inode
277 * @sb: superblock
278 * @mode: mode to setup inode with
279 *
280 */
281
282struct inode *v9fs_get_inode(struct super_block *sb, int mode)
283{
2bb54115 284 int err;
6a3124a3 285 struct inode *inode;
b501611a 286 struct v9fs_session_info *v9ses = sb->s_fs_info;
2bad8471 287
bd238fb4 288 P9_DPRINTK(P9_DEBUG_VFS, "super block: %p mode: %o\n", sb, mode);
2bad8471
EVH
289
290 inode = new_inode(sb);
2bb54115
AK
291 if (!inode) {
292 P9_EPRINTK(KERN_WARNING, "Problem allocating inode\n");
48559b4c 293 return ERR_PTR(-ENOMEM);
2bb54115
AK
294 }
295
217f206d 296 inode_init_owner(inode, NULL, mode);
2bb54115
AK
297 inode->i_blocks = 0;
298 inode->i_rdev = 0;
299 inode->i_atime = inode->i_mtime = inode->i_ctime = CURRENT_TIME;
300 inode->i_mapping->a_ops = &v9fs_addr_operations;
301
302 switch (mode & S_IFMT) {
303 case S_IFIFO:
304 case S_IFBLK:
305 case S_IFCHR:
306 case S_IFSOCK:
4b43516a
MK
307 if (v9fs_proto_dotl(v9ses)) {
308 inode->i_op = &v9fs_file_inode_operations_dotl;
309 inode->i_fop = &v9fs_file_operations_dotl;
310 } else if (v9fs_proto_dotu(v9ses)) {
311 inode->i_op = &v9fs_file_inode_operations;
312 inode->i_fop = &v9fs_file_operations;
313 } else {
bd238fb4 314 P9_DPRINTK(P9_DEBUG_ERROR,
2bb54115
AK
315 "special files without extended mode\n");
316 err = -EINVAL;
317 goto error;
2bad8471 318 }
2bb54115
AK
319 init_special_inode(inode, inode->i_mode, inode->i_rdev);
320 break;
321 case S_IFREG:
9b6533c9
SK
322 if (v9fs_proto_dotl(v9ses)) {
323 inode->i_op = &v9fs_file_inode_operations_dotl;
324 inode->i_fop = &v9fs_file_operations_dotl;
325 } else {
326 inode->i_op = &v9fs_file_inode_operations;
327 inode->i_fop = &v9fs_file_operations;
328 }
329
2bb54115 330 break;
9b6533c9 331
2bb54115 332 case S_IFLNK:
9b6533c9
SK
333 if (!v9fs_proto_dotu(v9ses) && !v9fs_proto_dotl(v9ses)) {
334 P9_DPRINTK(P9_DEBUG_ERROR, "extended modes used with "
335 "legacy protocol.\n");
2bb54115
AK
336 err = -EINVAL;
337 goto error;
338 }
9b6533c9
SK
339
340 if (v9fs_proto_dotl(v9ses))
341 inode->i_op = &v9fs_symlink_inode_operations_dotl;
342 else
343 inode->i_op = &v9fs_symlink_inode_operations;
344
2bb54115
AK
345 break;
346 case S_IFDIR:
347 inc_nlink(inode);
9b6533c9
SK
348 if (v9fs_proto_dotl(v9ses))
349 inode->i_op = &v9fs_dir_inode_operations_dotl;
350 else if (v9fs_proto_dotu(v9ses))
351 inode->i_op = &v9fs_dir_inode_operations_dotu;
2bb54115
AK
352 else
353 inode->i_op = &v9fs_dir_inode_operations;
9b6533c9
SK
354
355 if (v9fs_proto_dotl(v9ses))
356 inode->i_fop = &v9fs_dir_operations_dotl;
357 else
358 inode->i_fop = &v9fs_dir_operations;
359
2bb54115
AK
360 break;
361 default:
362 P9_DPRINTK(P9_DEBUG_ERROR, "BAD mode 0x%x S_IFMT 0x%x\n",
363 mode, mode & S_IFMT);
364 err = -EINVAL;
365 goto error;
2bad8471 366 }
2bb54115 367
2bad8471 368 return inode;
2bb54115
AK
369
370error:
371 iput(inode);
372 return ERR_PTR(err);
2bad8471
EVH
373}
374
bd238fb4 375/*
6a3124a3
LI
376static struct v9fs_fid*
377v9fs_clone_walk(struct v9fs_session_info *v9ses, u32 fid, struct dentry *dentry)
378{
379 int err;
736c4b85 380 int nfid;
6a3124a3
LI
381 struct v9fs_fid *ret;
382 struct v9fs_fcall *fcall;
383
384 nfid = v9fs_get_idpool(&v9ses->fidpool);
385 if (nfid < 0) {
0b8dd177 386 eprintk(KERN_WARNING, "no free fids available\n");
731805b4 387 return ERR_PTR(-ENOSPC);
0b8dd177
LI
388 }
389
6a3124a3
LI
390 err = v9fs_t_walk(v9ses, fid, nfid, (char *) dentry->d_name.name,
391 &fcall);
392
393 if (err < 0) {
41e5a6ac
LI
394 if (fcall && fcall->id == RWALK)
395 goto clunk_fid;
396
6a3124a3
LI
397 PRINT_FCALL_ERROR("walk error", fcall);
398 v9fs_put_idpool(nfid, &v9ses->fidpool);
399 goto error;
0b8dd177 400 }
6a3124a3 401
3cf6429a
LI
402 kfree(fcall);
403 fcall = NULL;
6a3124a3
LI
404 ret = v9fs_fid_create(v9ses, nfid);
405 if (!ret) {
406 err = -ENOMEM;
407 goto clunk_fid;
408 }
0b8dd177 409
6a3124a3
LI
410 err = v9fs_fid_insert(ret, dentry);
411 if (err < 0) {
412 v9fs_fid_destroy(ret);
413 goto clunk_fid;
0b8dd177 414 }
2bad8471 415
6a3124a3 416 return ret;
2bad8471 417
6a3124a3
LI
418clunk_fid:
419 v9fs_t_clunk(v9ses, nfid);
2bad8471 420
6a3124a3
LI
421error:
422 kfree(fcall);
423 return ERR_PTR(err);
424}
bd238fb4 425*/
2bad8471 426
60e78d2c
AK
427
428/**
429 * v9fs_clear_inode - release an inode
430 * @inode: inode to release
431 *
432 */
433void v9fs_clear_inode(struct inode *inode)
434{
435 filemap_fdatawrite(inode->i_mapping);
436
437#ifdef CONFIG_9P_FSCACHE
438 v9fs_cache_inode_put_cookie(inode);
439#endif
440}
441
5174fdab 442static struct inode *
f0853122 443v9fs_inode(struct v9fs_session_info *v9ses, struct p9_fid *fid,
6a3124a3
LI
444 struct super_block *sb)
445{
446 int err, umode;
f0853122 447 struct inode *ret = NULL;
51a87c55 448 struct p9_wstat *st;
531b1094 449
bd238fb4 450 st = p9_client_stat(fid);
02bc3567
AK
451 if (IS_ERR(st))
452 return ERR_CAST(st);
2bad8471 453
bd238fb4 454 umode = p9mode2unixmode(v9ses, st->mode);
6a3124a3
LI
455 ret = v9fs_get_inode(sb, umode);
456 if (IS_ERR(ret)) {
457 err = PTR_ERR(ret);
6a3124a3
LI
458 goto error;
459 }
2bad8471 460
bd238fb4
LI
461 v9fs_stat2inode(st, ret, sb);
462 ret->i_ino = v9fs_qid2ino(&st->qid);
60e78d2c
AK
463
464#ifdef CONFIG_9P_FSCACHE
465 v9fs_vcookie_set_qid(ret, &st->qid);
466 v9fs_cache_inode_get_cookie(ret);
467#endif
02bc3567 468 p9stat_free(st);
bd238fb4 469 kfree(st);
6a3124a3 470 return ret;
6a3124a3 471error:
02bc3567 472 p9stat_free(st);
bd238fb4 473 kfree(st);
6a3124a3 474 return ERR_PTR(err);
2bad8471
EVH
475}
476
f0853122
SK
477static struct inode *
478v9fs_inode_dotl(struct v9fs_session_info *v9ses, struct p9_fid *fid,
479 struct super_block *sb)
480{
481 struct inode *ret = NULL;
482 int err;
483 struct p9_stat_dotl *st;
484
485 st = p9_client_getattr_dotl(fid, P9_STATS_BASIC);
486 if (IS_ERR(st))
487 return ERR_CAST(st);
488
489 ret = v9fs_get_inode(sb, st->st_mode);
490 if (IS_ERR(ret)) {
491 err = PTR_ERR(ret);
492 goto error;
493 }
494
495 v9fs_stat2inode_dotl(st, ret);
496 ret->i_ino = v9fs_qid2ino(&st->qid);
497#ifdef CONFIG_9P_FSCACHE
498 v9fs_vcookie_set_qid(ret, &st->qid);
499 v9fs_cache_inode_get_cookie(ret);
500#endif
501 kfree(st);
502 return ret;
503error:
504 kfree(st);
505 return ERR_PTR(err);
506}
507
508/**
509 * v9fs_inode_from_fid - Helper routine to populate an inode by
510 * issuing a attribute request
511 * @v9ses: session information
512 * @fid: fid to issue attribute request for
513 * @sb: superblock on which to create inode
514 *
515 */
516static inline struct inode *
517v9fs_inode_from_fid(struct v9fs_session_info *v9ses, struct p9_fid *fid,
518 struct super_block *sb)
519{
520 if (v9fs_proto_dotl(v9ses))
521 return v9fs_inode_dotl(v9ses, fid, sb);
522 else
523 return v9fs_inode(v9ses, fid, sb);
524}
525
2bad8471
EVH
526/**
527 * v9fs_remove - helper function to remove files and directories
73c592b9
EVH
528 * @dir: directory inode that is being deleted
529 * @file: dentry that is being deleted
530 * @rmdir: removing a directory
2bad8471
EVH
531 *
532 */
533
534static int v9fs_remove(struct inode *dir, struct dentry *file, int rmdir)
535{
d994f405 536 int retval;
bd238fb4 537 struct inode *file_inode;
bd238fb4 538 struct p9_fid *v9fid;
2bad8471 539
bd238fb4 540 P9_DPRINTK(P9_DEBUG_VFS, "inode: %p dentry: %p rmdir: %d\n", dir, file,
2bad8471
EVH
541 rmdir);
542
543 file_inode = file->d_inode;
c959df9f 544 v9fid = v9fs_fid_clone(file);
ba17674f 545 if (IS_ERR(v9fid))
da977b2c 546 return PTR_ERR(v9fid);
2bad8471 547
d994f405
AK
548 retval = p9_client_remove(v9fid);
549 if (!retval)
550 drop_nlink(file_inode);
551 return retval;
2bad8471
EVH
552}
553
6a3124a3
LI
554static int
555v9fs_open_created(struct inode *inode, struct file *file)
556{
557 return 0;
558}
559
bd238fb4 560
2bad8471 561/**
bd238fb4 562 * v9fs_create - Create a file
ee443996
EVH
563 * @v9ses: session information
564 * @dir: directory that dentry is being created in
bd238fb4 565 * @dentry: dentry that is being created
0e15597e 566 * @extension: 9p2000.u extension string to support devices, etc.
bd238fb4
LI
567 * @perm: create permissions
568 * @mode: open mode
2bad8471
EVH
569 *
570 */
bd238fb4
LI
571static struct p9_fid *
572v9fs_create(struct v9fs_session_info *v9ses, struct inode *dir,
573 struct dentry *dentry, char *extension, u32 perm, u8 mode)
2bad8471 574{
6a3124a3 575 int err;
bd238fb4
LI
576 char *name;
577 struct p9_fid *dfid, *ofid, *fid;
6a3124a3 578 struct inode *inode;
6a3124a3 579
51a87c55
EVH
580 P9_DPRINTK(P9_DEBUG_VFS, "name %s\n", dentry->d_name.name);
581
bd238fb4
LI
582 err = 0;
583 ofid = NULL;
584 fid = NULL;
585 name = (char *) dentry->d_name.name;
6d27e64d 586 dfid = v9fs_fid_lookup(dentry->d_parent);
ba17674f 587 if (IS_ERR(dfid)) {
da977b2c 588 err = PTR_ERR(dfid);
6d27e64d
VJ
589 P9_DPRINTK(P9_DEBUG_VFS, "fid lookup failed %d\n", err);
590 return ERR_PTR(err);
da977b2c 591 }
6a3124a3 592
bd238fb4
LI
593 /* clone a fid to use for creation */
594 ofid = p9_client_walk(dfid, 0, NULL, 1);
595 if (IS_ERR(ofid)) {
596 err = PTR_ERR(ofid);
51a87c55 597 P9_DPRINTK(P9_DEBUG_VFS, "p9_client_walk failed %d\n", err);
6d27e64d 598 return ERR_PTR(err);
bd238fb4 599 }
6a3124a3 600
bd238fb4 601 err = p9_client_fcreate(ofid, name, perm, mode, extension);
51a87c55
EVH
602 if (err < 0) {
603 P9_DPRINTK(P9_DEBUG_VFS, "p9_client_fcreate failed %d\n", err);
bd238fb4 604 goto error;
51a87c55 605 }
6a3124a3 606
bd238fb4 607 /* now walk from the parent so we can get unopened fid */
6d27e64d 608 fid = p9_client_walk(dfid, 1, &name, 1);
bd238fb4
LI
609 if (IS_ERR(fid)) {
610 err = PTR_ERR(fid);
51a87c55 611 P9_DPRINTK(P9_DEBUG_VFS, "p9_client_walk failed %d\n", err);
bd238fb4 612 fid = NULL;
6a3124a3 613 goto error;
6d27e64d 614 }
6a3124a3 615
bd238fb4
LI
616 /* instantiate inode and assign the unopened fid to the dentry */
617 inode = v9fs_inode_from_fid(v9ses, fid, dir->i_sb);
6a3124a3
LI
618 if (IS_ERR(inode)) {
619 err = PTR_ERR(inode);
51a87c55 620 P9_DPRINTK(P9_DEBUG_VFS, "inode creation failed %d\n", err);
6a3124a3
LI
621 goto error;
622 }
623
ba17674f 624 if (v9ses->cache)
e03abc0c
EVH
625 dentry->d_op = &v9fs_cached_dentry_operations;
626 else
627 dentry->d_op = &v9fs_dentry_operations;
bd238fb4 628
6a3124a3 629 d_instantiate(dentry, inode);
50fb6d2b
AK
630 err = v9fs_fid_add(dentry, fid);
631 if (err < 0)
632 goto error;
633
bd238fb4 634 return ofid;
6a3124a3 635
bd238fb4 636error:
bd238fb4
LI
637 if (ofid)
638 p9_client_clunk(ofid);
639
640 if (fid)
641 p9_client_clunk(fid);
642
643 return ERR_PTR(err);
644}
645
5643135a
VJJ
646/**
647 * v9fs_vfs_create_dotl - VFS hook to create files for 9P2000.L protocol.
648 * @dir: directory inode that is being created
649 * @dentry: dentry that is being deleted
650 * @mode: create permissions
651 * @nd: path information
652 *
653 */
654
655static int
656v9fs_vfs_create_dotl(struct inode *dir, struct dentry *dentry, int mode,
657 struct nameidata *nd)
658{
659 int err = 0;
660 char *name = NULL;
661 gid_t gid;
662 int flags;
663 struct v9fs_session_info *v9ses;
664 struct p9_fid *fid = NULL;
665 struct p9_fid *dfid, *ofid;
666 struct file *filp;
667 struct p9_qid qid;
668 struct inode *inode;
669
670 v9ses = v9fs_inode2v9ses(dir);
671 if (nd && nd->flags & LOOKUP_OPEN)
672 flags = nd->intent.open.flags - 1;
673 else
674 flags = O_RDWR;
675
676 name = (char *) dentry->d_name.name;
677 P9_DPRINTK(P9_DEBUG_VFS, "v9fs_vfs_create_dotl: name:%s flags:0x%x "
678 "mode:0x%x\n", name, flags, mode);
679
680 dfid = v9fs_fid_lookup(dentry->d_parent);
681 if (IS_ERR(dfid)) {
682 err = PTR_ERR(dfid);
683 P9_DPRINTK(P9_DEBUG_VFS, "fid lookup failed %d\n", err);
684 return err;
685 }
686
687 /* clone a fid to use for creation */
688 ofid = p9_client_walk(dfid, 0, NULL, 1);
689 if (IS_ERR(ofid)) {
690 err = PTR_ERR(ofid);
691 P9_DPRINTK(P9_DEBUG_VFS, "p9_client_walk failed %d\n", err);
692 return err;
693 }
694
695 gid = v9fs_get_fsgid_for_create(dir);
696 err = p9_client_create_dotl(ofid, name, flags, mode, gid, &qid);
697 if (err < 0) {
698 P9_DPRINTK(P9_DEBUG_VFS,
699 "p9_client_open_dotl failed in creat %d\n",
700 err);
701 goto error;
702 }
703
704 /* No need to populate the inode if we are not opening the file AND
705 * not in cached mode.
706 */
707 if (!v9ses->cache && !(nd && nd->flags & LOOKUP_OPEN)) {
708 /* Not in cached mode. No need to populate inode with stat */
709 dentry->d_op = &v9fs_dentry_operations;
710 p9_client_clunk(ofid);
711 d_instantiate(dentry, NULL);
712 return 0;
713 }
714
715 /* Now walk from the parent so we can get an unopened fid. */
716 fid = p9_client_walk(dfid, 1, &name, 1);
717 if (IS_ERR(fid)) {
718 err = PTR_ERR(fid);
719 P9_DPRINTK(P9_DEBUG_VFS, "p9_client_walk failed %d\n", err);
720 fid = NULL;
721 goto error;
722 }
723
724 /* instantiate inode and assign the unopened fid to dentry */
725 inode = v9fs_inode_from_fid(v9ses, fid, dir->i_sb);
726 if (IS_ERR(inode)) {
727 err = PTR_ERR(inode);
728 P9_DPRINTK(P9_DEBUG_VFS, "inode creation failed %d\n", err);
729 goto error;
730 }
731 dentry->d_op = &v9fs_cached_dentry_operations;
732 d_instantiate(dentry, inode);
733 err = v9fs_fid_add(dentry, fid);
734 if (err < 0)
735 goto error;
736
737 /* if we are opening a file, assign the open fid to the file */
738 if (nd && nd->flags & LOOKUP_OPEN) {
739 filp = lookup_instantiate_filp(nd, dentry, v9fs_open_created);
740 if (IS_ERR(filp)) {
741 p9_client_clunk(ofid);
742 return PTR_ERR(filp);
743 }
744 filp->private_data = ofid;
745 } else
746 p9_client_clunk(ofid);
747
748 return 0;
749
750error:
751 if (ofid)
752 p9_client_clunk(ofid);
753 if (fid)
754 p9_client_clunk(fid);
755 return err;
756}
757
bd238fb4
LI
758/**
759 * v9fs_vfs_create - VFS hook to create files
ee443996 760 * @dir: directory inode that is being created
bd238fb4
LI
761 * @dentry: dentry that is being deleted
762 * @mode: create permissions
763 * @nd: path information
764 *
765 */
6a3124a3 766
bd238fb4
LI
767static int
768v9fs_vfs_create(struct inode *dir, struct dentry *dentry, int mode,
769 struct nameidata *nd)
770{
771 int err;
772 u32 perm;
773 int flags;
774 struct v9fs_session_info *v9ses;
775 struct p9_fid *fid;
776 struct file *filp;
777
778 err = 0;
779 fid = NULL;
780 v9ses = v9fs_inode2v9ses(dir);
781 perm = unixmode2p9mode(v9ses, mode);
782 if (nd && nd->flags & LOOKUP_OPEN)
783 flags = nd->intent.open.flags - 1;
784 else
785 flags = O_RDWR;
786
787 fid = v9fs_create(v9ses, dir, dentry, NULL, perm,
dd6102fb
SK
788 v9fs_uflags2omode(flags,
789 v9fs_proto_dotu(v9ses)));
bd238fb4
LI
790 if (IS_ERR(fid)) {
791 err = PTR_ERR(fid);
792 fid = NULL;
793 goto error;
794 }
795
796 /* if we are opening a file, assign the open fid to the file */
797 if (nd && nd->flags & LOOKUP_OPEN) {
6a3124a3
LI
798 filp = lookup_instantiate_filp(nd, dentry, v9fs_open_created);
799 if (IS_ERR(filp)) {
bd238fb4
LI
800 err = PTR_ERR(filp);
801 goto error;
6a3124a3
LI
802 }
803
bd238fb4
LI
804 filp->private_data = fid;
805 } else
806 p9_client_clunk(fid);
6a3124a3
LI
807
808 return 0;
809
810error:
bd238fb4
LI
811 if (fid)
812 p9_client_clunk(fid);
6a3124a3 813
6a3124a3 814 return err;
2bad8471
EVH
815}
816
817/**
818 * v9fs_vfs_mkdir - VFS mkdir hook to create a directory
ee443996 819 * @dir: inode that is being unlinked
2bad8471
EVH
820 * @dentry: dentry that is being unlinked
821 * @mode: mode for new directory
822 *
823 */
824
6a3124a3 825static int v9fs_vfs_mkdir(struct inode *dir, struct dentry *dentry, int mode)
2bad8471 826{
6a3124a3 827 int err;
bd238fb4 828 u32 perm;
6a3124a3 829 struct v9fs_session_info *v9ses;
bd238fb4 830 struct p9_fid *fid;
6a3124a3 831
bd238fb4
LI
832 P9_DPRINTK(P9_DEBUG_VFS, "name %s\n", dentry->d_name.name);
833 err = 0;
6a3124a3 834 v9ses = v9fs_inode2v9ses(dir);
6a3124a3 835 perm = unixmode2p9mode(v9ses, mode | S_IFDIR);
bd238fb4
LI
836 fid = v9fs_create(v9ses, dir, dentry, NULL, perm, P9_OREAD);
837 if (IS_ERR(fid)) {
838 err = PTR_ERR(fid);
839 fid = NULL;
6a3124a3
LI
840 }
841
bd238fb4
LI
842 if (fid)
843 p9_client_clunk(fid);
6a3124a3 844
6a3124a3 845 return err;
2bad8471
EVH
846}
847
01a622bd
MK
848
849/**
850 * v9fs_vfs_mkdir_dotl - VFS mkdir hook to create a directory
851 * @dir: inode that is being unlinked
852 * @dentry: dentry that is being unlinked
853 * @mode: mode for new directory
854 *
855 */
856
857static int v9fs_vfs_mkdir_dotl(struct inode *dir, struct dentry *dentry,
858 int mode)
859{
860 int err;
861 struct v9fs_session_info *v9ses;
862 struct p9_fid *fid = NULL, *dfid = NULL;
863 gid_t gid;
864 char *name;
865 struct inode *inode;
866 struct p9_qid qid;
867 struct dentry *dir_dentry;
868
869 P9_DPRINTK(P9_DEBUG_VFS, "name %s\n", dentry->d_name.name);
870 err = 0;
871 v9ses = v9fs_inode2v9ses(dir);
872
873 mode |= S_IFDIR;
874 dir_dentry = v9fs_dentry_from_dir_inode(dir);
875 dfid = v9fs_fid_lookup(dir_dentry);
876 if (IS_ERR(dfid)) {
877 err = PTR_ERR(dfid);
878 P9_DPRINTK(P9_DEBUG_VFS, "fid lookup failed %d\n", err);
879 dfid = NULL;
880 goto error;
881 }
882
883 gid = v9fs_get_fsgid_for_create(dir);
884 if (gid < 0) {
885 P9_DPRINTK(P9_DEBUG_VFS, "v9fs_get_fsgid_for_create failed\n");
886 goto error;
887 }
888
889 name = (char *) dentry->d_name.name;
890 err = p9_client_mkdir_dotl(dfid, name, mode, gid, &qid);
891 if (err < 0)
892 goto error;
893
894 /* instantiate inode and assign the unopened fid to the dentry */
895 if (v9ses->cache == CACHE_LOOSE || v9ses->cache == CACHE_FSCACHE) {
896 fid = p9_client_walk(dfid, 1, &name, 1);
897 if (IS_ERR(fid)) {
898 err = PTR_ERR(fid);
899 P9_DPRINTK(P9_DEBUG_VFS, "p9_client_walk failed %d\n",
900 err);
901 fid = NULL;
902 goto error;
903 }
904
905 inode = v9fs_inode_from_fid(v9ses, fid, dir->i_sb);
906 if (IS_ERR(inode)) {
907 err = PTR_ERR(inode);
908 P9_DPRINTK(P9_DEBUG_VFS, "inode creation failed %d\n",
909 err);
910 goto error;
911 }
912 dentry->d_op = &v9fs_cached_dentry_operations;
913 d_instantiate(dentry, inode);
914 err = v9fs_fid_add(dentry, fid);
915 if (err < 0)
916 goto error;
917 fid = NULL;
918 }
919error:
920 if (fid)
921 p9_client_clunk(fid);
922 return err;
923}
924
2bad8471
EVH
925/**
926 * v9fs_vfs_lookup - VFS lookup hook to "walk" to a new inode
927 * @dir: inode that is being walked from
928 * @dentry: dentry that is being walked to?
929 * @nameidata: path data
930 *
931 */
932
933static struct dentry *v9fs_vfs_lookup(struct inode *dir, struct dentry *dentry,
934 struct nameidata *nameidata)
935{
936 struct super_block *sb;
937 struct v9fs_session_info *v9ses;
bd238fb4 938 struct p9_fid *dfid, *fid;
2bad8471 939 struct inode *inode;
bd238fb4 940 char *name;
2bad8471
EVH
941 int result = 0;
942
bd238fb4 943 P9_DPRINTK(P9_DEBUG_VFS, "dir: %p dentry: (%s) %p nameidata: %p\n",
731805b4 944 dir, dentry->d_name.name, dentry, nameidata);
2bad8471 945
11e9b49b
SK
946 if (dentry->d_name.len > NAME_MAX)
947 return ERR_PTR(-ENAMETOOLONG);
948
2bad8471
EVH
949 sb = dir->i_sb;
950 v9ses = v9fs_inode2v9ses(dir);
a534c8d1 951 /* We can walk d_parent because we hold the dir->i_mutex */
bd238fb4
LI
952 dfid = v9fs_fid_lookup(dentry->d_parent);
953 if (IS_ERR(dfid))
e231c2ee 954 return ERR_CAST(dfid);
bd238fb4
LI
955
956 name = (char *) dentry->d_name.name;
957 fid = p9_client_walk(dfid, 1, &name, 1);
958 if (IS_ERR(fid)) {
959 result = PTR_ERR(fid);
2bad8471 960 if (result == -ENOENT) {
85e0df24
AK
961 inode = NULL;
962 goto inst_out;
2bad8471 963 }
2bad8471 964
bd238fb4 965 return ERR_PTR(result);
2bad8471
EVH
966 }
967
bd238fb4
LI
968 inode = v9fs_inode_from_fid(v9ses, fid, dir->i_sb);
969 if (IS_ERR(inode)) {
970 result = PTR_ERR(inode);
971 inode = NULL;
972 goto error;
2bad8471
EVH
973 }
974
bd238fb4 975 result = v9fs_fid_add(dentry, fid);
6a3124a3 976 if (result < 0)
bd238fb4 977 goto error;
6a3124a3 978
85e0df24
AK
979inst_out:
980 if (v9ses->cache)
e03abc0c
EVH
981 dentry->d_op = &v9fs_cached_dentry_operations;
982 else
983 dentry->d_op = &v9fs_dentry_operations;
2bad8471
EVH
984
985 d_add(dentry, inode);
2bad8471
EVH
986 return NULL;
987
bd238fb4 988error:
62aa528e 989 p9_client_clunk(fid);
da977b2c 990
2bad8471
EVH
991 return ERR_PTR(result);
992}
993
994/**
995 * v9fs_vfs_unlink - VFS unlink hook to delete an inode
996 * @i: inode that is being unlinked
73c592b9 997 * @d: dentry that is being unlinked
2bad8471
EVH
998 *
999 */
1000
1001static int v9fs_vfs_unlink(struct inode *i, struct dentry *d)
1002{
1003 return v9fs_remove(i, d, 0);
1004}
1005
1006/**
1007 * v9fs_vfs_rmdir - VFS unlink hook to delete a directory
1008 * @i: inode that is being unlinked
73c592b9 1009 * @d: dentry that is being unlinked
2bad8471
EVH
1010 *
1011 */
1012
1013static int v9fs_vfs_rmdir(struct inode *i, struct dentry *d)
1014{
1015 return v9fs_remove(i, d, 1);
1016}
1017
1018/**
1019 * v9fs_vfs_rename - VFS hook to rename an inode
1020 * @old_dir: old dir inode
1021 * @old_dentry: old dentry
1022 * @new_dir: new dir inode
1023 * @new_dentry: new dentry
1024 *
1025 */
1026
1027static int
1028v9fs_vfs_rename(struct inode *old_dir, struct dentry *old_dentry,
1029 struct inode *new_dir, struct dentry *new_dentry)
1030{
bd238fb4
LI
1031 struct inode *old_inode;
1032 struct v9fs_session_info *v9ses;
1033 struct p9_fid *oldfid;
1034 struct p9_fid *olddirfid;
1035 struct p9_fid *newdirfid;
1036 struct p9_wstat wstat;
1037 int retval;
2bad8471 1038
bd238fb4
LI
1039 P9_DPRINTK(P9_DEBUG_VFS, "\n");
1040 retval = 0;
1041 old_inode = old_dentry->d_inode;
1042 v9ses = v9fs_inode2v9ses(old_inode);
1043 oldfid = v9fs_fid_lookup(old_dentry);
ba17674f 1044 if (IS_ERR(oldfid))
da977b2c
EVH
1045 return PTR_ERR(oldfid);
1046
1047 olddirfid = v9fs_fid_clone(old_dentry->d_parent);
ba17674f 1048 if (IS_ERR(olddirfid)) {
da977b2c 1049 retval = PTR_ERR(olddirfid);
bd238fb4 1050 goto done;
da977b2c
EVH
1051 }
1052
1053 newdirfid = v9fs_fid_clone(new_dentry->d_parent);
ba17674f 1054 if (IS_ERR(newdirfid)) {
da977b2c 1055 retval = PTR_ERR(newdirfid);
bd238fb4 1056 goto clunk_olddir;
2bad8471
EVH
1057 }
1058
a534c8d1 1059 down_write(&v9ses->rename_sem);
4681dbda
SK
1060 if (v9fs_proto_dotl(v9ses)) {
1061 retval = p9_client_rename(oldfid, newdirfid,
1062 (char *) new_dentry->d_name.name);
1063 if (retval != -ENOSYS)
1064 goto clunk_newdir;
1065 }
a534c8d1
AK
1066 if (old_dentry->d_parent != new_dentry->d_parent) {
1067 /*
1068 * 9P .u can only handle file rename in the same directory
1069 */
4681dbda 1070
bd238fb4
LI
1071 P9_DPRINTK(P9_DEBUG_ERROR,
1072 "old dir and new dir are different\n");
621997cd 1073 retval = -EXDEV;
bd238fb4 1074 goto clunk_newdir;
2bad8471 1075 }
531b1094 1076 v9fs_blank_wstat(&wstat);
ba17674f 1077 wstat.muid = v9ses->uname;
531b1094 1078 wstat.name = (char *) new_dentry->d_name.name;
bd238fb4 1079 retval = p9_client_wstat(oldfid, &wstat);
2bad8471 1080
bd238fb4 1081clunk_newdir:
a534c8d1
AK
1082 if (!retval)
1083 /* successful rename */
1084 d_move(old_dentry, new_dentry);
1085 up_write(&v9ses->rename_sem);
22150c4f 1086 p9_client_clunk(newdirfid);
2bad8471 1087
bd238fb4 1088clunk_olddir:
22150c4f 1089 p9_client_clunk(olddirfid);
da977b2c 1090
bd238fb4 1091done:
2bad8471
EVH
1092 return retval;
1093}
1094
1095/**
943ffb58 1096 * v9fs_vfs_getattr - retrieve file metadata
ee443996
EVH
1097 * @mnt: mount information
1098 * @dentry: file to get attributes on
1099 * @stat: metadata structure to populate
2bad8471
EVH
1100 *
1101 */
1102
1103static int
1104v9fs_vfs_getattr(struct vfsmount *mnt, struct dentry *dentry,
1105 struct kstat *stat)
1106{
bd238fb4
LI
1107 int err;
1108 struct v9fs_session_info *v9ses;
1109 struct p9_fid *fid;
51a87c55 1110 struct p9_wstat *st;
bd238fb4
LI
1111
1112 P9_DPRINTK(P9_DEBUG_VFS, "dentry: %p\n", dentry);
1113 err = -EPERM;
1114 v9ses = v9fs_inode2v9ses(dentry->d_inode);
60e78d2c 1115 if (v9ses->cache == CACHE_LOOSE || v9ses->cache == CACHE_FSCACHE)
9523a841
EVH
1116 return simple_getattr(mnt, dentry, stat);
1117
bd238fb4
LI
1118 fid = v9fs_fid_lookup(dentry);
1119 if (IS_ERR(fid))
da977b2c 1120 return PTR_ERR(fid);
2bad8471 1121
bd238fb4
LI
1122 st = p9_client_stat(fid);
1123 if (IS_ERR(st))
1124 return PTR_ERR(st);
2bad8471 1125
bd238fb4 1126 v9fs_stat2inode(st, dentry->d_inode, dentry->d_inode->i_sb);
2bad8471 1127 generic_fillattr(dentry->d_inode, stat);
2bad8471 1128
bd238fb4
LI
1129 kfree(st);
1130 return 0;
2bad8471
EVH
1131}
1132
f0853122
SK
1133static int
1134v9fs_vfs_getattr_dotl(struct vfsmount *mnt, struct dentry *dentry,
1135 struct kstat *stat)
1136{
1137 int err;
1138 struct v9fs_session_info *v9ses;
1139 struct p9_fid *fid;
1140 struct p9_stat_dotl *st;
1141
1142 P9_DPRINTK(P9_DEBUG_VFS, "dentry: %p\n", dentry);
1143 err = -EPERM;
1144 v9ses = v9fs_inode2v9ses(dentry->d_inode);
1145 if (v9ses->cache == CACHE_LOOSE || v9ses->cache == CACHE_FSCACHE)
1146 return simple_getattr(mnt, dentry, stat);
1147
1148 fid = v9fs_fid_lookup(dentry);
1149 if (IS_ERR(fid))
1150 return PTR_ERR(fid);
1151
1152 /* Ask for all the fields in stat structure. Server will return
1153 * whatever it supports
1154 */
1155
1156 st = p9_client_getattr_dotl(fid, P9_STATS_ALL);
1157 if (IS_ERR(st))
1158 return PTR_ERR(st);
1159
1160 v9fs_stat2inode_dotl(st, dentry->d_inode);
1161 generic_fillattr(dentry->d_inode, stat);
1162 /* Change block size to what the server returned */
1163 stat->blksize = st->st_blksize;
1164
1165 kfree(st);
1166 return 0;
1167}
1168
2bad8471
EVH
1169/**
1170 * v9fs_vfs_setattr - set file metadata
1171 * @dentry: file whose metadata to set
1172 * @iattr: metadata assignment structure
1173 *
1174 */
1175
1176static int v9fs_vfs_setattr(struct dentry *dentry, struct iattr *iattr)
1177{
bd238fb4
LI
1178 int retval;
1179 struct v9fs_session_info *v9ses;
1180 struct p9_fid *fid;
1181 struct p9_wstat wstat;
2bad8471 1182
bd238fb4
LI
1183 P9_DPRINTK(P9_DEBUG_VFS, "\n");
1184 retval = -EPERM;
1185 v9ses = v9fs_inode2v9ses(dentry->d_inode);
1186 fid = v9fs_fid_lookup(dentry);
da977b2c
EVH
1187 if(IS_ERR(fid))
1188 return PTR_ERR(fid);
2bad8471 1189
531b1094 1190 v9fs_blank_wstat(&wstat);
2bad8471 1191 if (iattr->ia_valid & ATTR_MODE)
531b1094 1192 wstat.mode = unixmode2p9mode(v9ses, iattr->ia_mode);
2bad8471
EVH
1193
1194 if (iattr->ia_valid & ATTR_MTIME)
531b1094 1195 wstat.mtime = iattr->ia_mtime.tv_sec;
2bad8471
EVH
1196
1197 if (iattr->ia_valid & ATTR_ATIME)
531b1094 1198 wstat.atime = iattr->ia_atime.tv_sec;
2bad8471
EVH
1199
1200 if (iattr->ia_valid & ATTR_SIZE)
531b1094 1201 wstat.length = iattr->ia_size;
2bad8471 1202
dd6102fb 1203 if (v9fs_proto_dotu(v9ses)) {
531b1094
LI
1204 if (iattr->ia_valid & ATTR_UID)
1205 wstat.n_uid = iattr->ia_uid;
2bad8471 1206
531b1094
LI
1207 if (iattr->ia_valid & ATTR_GID)
1208 wstat.n_gid = iattr->ia_gid;
2bad8471
EVH
1209 }
1210
bd238fb4
LI
1211 retval = p9_client_wstat(fid, &wstat);
1212 if (retval >= 0)
1213 retval = inode_setattr(dentry->d_inode, iattr);
2bad8471 1214
bd238fb4 1215 return retval;
2bad8471
EVH
1216}
1217
87d7845a
SK
1218/**
1219 * v9fs_vfs_setattr_dotl - set file metadata
1220 * @dentry: file whose metadata to set
1221 * @iattr: metadata assignment structure
1222 *
1223 */
1224
1225static int v9fs_vfs_setattr_dotl(struct dentry *dentry, struct iattr *iattr)
1226{
1227 int retval;
1228 struct v9fs_session_info *v9ses;
1229 struct p9_fid *fid;
1230 struct p9_iattr_dotl p9attr;
1231
1232 P9_DPRINTK(P9_DEBUG_VFS, "\n");
1233
1234 retval = inode_change_ok(dentry->d_inode, iattr);
1235 if (retval)
1236 return retval;
1237
1238 p9attr.valid = iattr->ia_valid;
1239 p9attr.mode = iattr->ia_mode;
1240 p9attr.uid = iattr->ia_uid;
1241 p9attr.gid = iattr->ia_gid;
1242 p9attr.size = iattr->ia_size;
1243 p9attr.atime_sec = iattr->ia_atime.tv_sec;
1244 p9attr.atime_nsec = iattr->ia_atime.tv_nsec;
1245 p9attr.mtime_sec = iattr->ia_mtime.tv_sec;
1246 p9attr.mtime_nsec = iattr->ia_mtime.tv_nsec;
1247
1248 retval = -EPERM;
1249 v9ses = v9fs_inode2v9ses(dentry->d_inode);
1250 fid = v9fs_fid_lookup(dentry);
1251 if (IS_ERR(fid))
1252 return PTR_ERR(fid);
1253
1254 retval = p9_client_setattr(fid, &p9attr);
1255 if (retval >= 0)
1256 retval = inode_setattr(dentry->d_inode, iattr);
1257
1258 return retval;
1259}
1260
2bad8471 1261/**
531b1094
LI
1262 * v9fs_stat2inode - populate an inode structure with mistat info
1263 * @stat: Plan 9 metadata (mistat) structure
2bad8471
EVH
1264 * @inode: inode to populate
1265 * @sb: superblock of filesystem
1266 *
1267 */
1268
1269void
51a87c55 1270v9fs_stat2inode(struct p9_wstat *stat, struct inode *inode,
531b1094 1271 struct super_block *sb)
2bad8471 1272{
531b1094 1273 char ext[32];
5717144a
AK
1274 char tag_name[14];
1275 unsigned int i_nlink;
2bad8471
EVH
1276 struct v9fs_session_info *v9ses = sb->s_fs_info;
1277
1278 inode->i_nlink = 1;
1279
531b1094
LI
1280 inode->i_atime.tv_sec = stat->atime;
1281 inode->i_mtime.tv_sec = stat->mtime;
1282 inode->i_ctime.tv_sec = stat->mtime;
2bad8471 1283
bd32b82d
LI
1284 inode->i_uid = v9ses->dfltuid;
1285 inode->i_gid = v9ses->dfltgid;
2bad8471 1286
dd6102fb 1287 if (v9fs_proto_dotu(v9ses)) {
531b1094
LI
1288 inode->i_uid = stat->n_uid;
1289 inode->i_gid = stat->n_gid;
2bad8471 1290 }
5717144a
AK
1291 if ((S_ISREG(inode->i_mode)) || (S_ISDIR(inode->i_mode))) {
1292 if (v9fs_proto_dotu(v9ses) && (stat->extension[0] != '\0')) {
1293 /*
1294 * Hadlink support got added later to
1295 * to the .u extension. So there can be
1296 * server out there that doesn't support
1297 * this even with .u extension. So check
1298 * for non NULL stat->extension
1299 */
1300 strncpy(ext, stat->extension, sizeof(ext));
1301 /* HARDLINKCOUNT %u */
1302 sscanf(ext, "%13s %u", tag_name, &i_nlink);
1303 if (!strncmp(tag_name, "HARDLINKCOUNT", 13))
1304 inode->i_nlink = i_nlink;
1305 }
1306 }
531b1094 1307 inode->i_mode = p9mode2unixmode(v9ses, stat->mode);
2bad8471
EVH
1308 if ((S_ISBLK(inode->i_mode)) || (S_ISCHR(inode->i_mode))) {
1309 char type = 0;
1310 int major = -1;
1311 int minor = -1;
531b1094 1312
51a87c55 1313 strncpy(ext, stat->extension, sizeof(ext));
531b1094 1314 sscanf(ext, "%c %u %u", &type, &major, &minor);
2bad8471
EVH
1315 switch (type) {
1316 case 'c':
1317 inode->i_mode &= ~S_IFBLK;
1318 inode->i_mode |= S_IFCHR;
1319 break;
1320 case 'b':
1321 break;
1322 default:
bd238fb4 1323 P9_DPRINTK(P9_DEBUG_ERROR,
51a87c55
EVH
1324 "Unknown special type %c %s\n", type,
1325 stat->extension);
2bad8471
EVH
1326 };
1327 inode->i_rdev = MKDEV(major, minor);
57c7b4e6 1328 init_special_inode(inode, inode->i_mode, inode->i_rdev);
2bad8471
EVH
1329 } else
1330 inode->i_rdev = 0;
1331
7549ae3e 1332 i_size_write(inode, stat->length);
2bad8471 1333
bd238fb4 1334 /* not real number of blocks, but 512 byte ones ... */
7549ae3e 1335 inode->i_blocks = (i_size_read(inode) + 512 - 1) >> 9;
2bad8471
EVH
1336}
1337
f0853122
SK
1338/**
1339 * v9fs_stat2inode_dotl - populate an inode structure with stat info
1340 * @stat: stat structure
1341 * @inode: inode to populate
1342 * @sb: superblock of filesystem
1343 *
1344 */
1345
1346void
1347v9fs_stat2inode_dotl(struct p9_stat_dotl *stat, struct inode *inode)
1348{
1349
1350 if ((stat->st_result_mask & P9_STATS_BASIC) == P9_STATS_BASIC) {
1351 inode->i_atime.tv_sec = stat->st_atime_sec;
1352 inode->i_atime.tv_nsec = stat->st_atime_nsec;
1353 inode->i_mtime.tv_sec = stat->st_mtime_sec;
1354 inode->i_mtime.tv_nsec = stat->st_mtime_nsec;
1355 inode->i_ctime.tv_sec = stat->st_ctime_sec;
1356 inode->i_ctime.tv_nsec = stat->st_ctime_nsec;
1357 inode->i_uid = stat->st_uid;
1358 inode->i_gid = stat->st_gid;
1359 inode->i_nlink = stat->st_nlink;
1360 inode->i_mode = stat->st_mode;
1361 inode->i_rdev = new_decode_dev(stat->st_rdev);
1362
1363 if ((S_ISBLK(inode->i_mode)) || (S_ISCHR(inode->i_mode)))
1364 init_special_inode(inode, inode->i_mode, inode->i_rdev);
1365
1366 i_size_write(inode, stat->st_size);
1367 inode->i_blocks = stat->st_blocks;
1368 } else {
1369 if (stat->st_result_mask & P9_STATS_ATIME) {
1370 inode->i_atime.tv_sec = stat->st_atime_sec;
1371 inode->i_atime.tv_nsec = stat->st_atime_nsec;
1372 }
1373 if (stat->st_result_mask & P9_STATS_MTIME) {
1374 inode->i_mtime.tv_sec = stat->st_mtime_sec;
1375 inode->i_mtime.tv_nsec = stat->st_mtime_nsec;
1376 }
1377 if (stat->st_result_mask & P9_STATS_CTIME) {
1378 inode->i_ctime.tv_sec = stat->st_ctime_sec;
1379 inode->i_ctime.tv_nsec = stat->st_ctime_nsec;
1380 }
1381 if (stat->st_result_mask & P9_STATS_UID)
1382 inode->i_uid = stat->st_uid;
1383 if (stat->st_result_mask & P9_STATS_GID)
1384 inode->i_gid = stat->st_gid;
1385 if (stat->st_result_mask & P9_STATS_NLINK)
1386 inode->i_nlink = stat->st_nlink;
1387 if (stat->st_result_mask & P9_STATS_MODE) {
1388 inode->i_mode = stat->st_mode;
1389 if ((S_ISBLK(inode->i_mode)) ||
1390 (S_ISCHR(inode->i_mode)))
1391 init_special_inode(inode, inode->i_mode,
1392 inode->i_rdev);
1393 }
1394 if (stat->st_result_mask & P9_STATS_RDEV)
1395 inode->i_rdev = new_decode_dev(stat->st_rdev);
1396 if (stat->st_result_mask & P9_STATS_SIZE)
1397 i_size_write(inode, stat->st_size);
1398 if (stat->st_result_mask & P9_STATS_BLOCKS)
1399 inode->i_blocks = stat->st_blocks;
1400 }
1401 if (stat->st_result_mask & P9_STATS_GEN)
1402 inode->i_generation = stat->st_gen;
1403
1404 /* Currently we don't support P9_STATS_BTIME and P9_STATS_DATA_VERSION
1405 * because the inode structure does not have fields for them.
1406 */
1407}
1408
2bad8471
EVH
1409/**
1410 * v9fs_qid2ino - convert qid into inode number
1411 * @qid: qid to hash
1412 *
1413 * BUG: potential for inode number collisions?
1414 */
1415
bd238fb4 1416ino_t v9fs_qid2ino(struct p9_qid *qid)
2bad8471
EVH
1417{
1418 u64 path = qid->path + 2;
1419 ino_t i = 0;
1420
1421 if (sizeof(ino_t) == sizeof(path))
1422 memcpy(&i, &path, sizeof(ino_t));
1423 else
1424 i = (ino_t) (path ^ (path >> 32));
1425
1426 return i;
1427}
1428
2bad8471
EVH
1429/**
1430 * v9fs_readlink - read a symlink's location (internal version)
1431 * @dentry: dentry for symlink
73c592b9 1432 * @buffer: buffer to load symlink location into
2bad8471
EVH
1433 * @buflen: length of buffer
1434 *
1435 */
1436
1437static int v9fs_readlink(struct dentry *dentry, char *buffer, int buflen)
1438{
bd238fb4 1439 int retval;
2bad8471 1440
bd238fb4
LI
1441 struct v9fs_session_info *v9ses;
1442 struct p9_fid *fid;
51a87c55 1443 struct p9_wstat *st;
2bad8471 1444
bd238fb4
LI
1445 P9_DPRINTK(P9_DEBUG_VFS, " %s\n", dentry->d_name.name);
1446 retval = -EPERM;
1447 v9ses = v9fs_inode2v9ses(dentry->d_inode);
1448 fid = v9fs_fid_lookup(dentry);
ba17674f 1449 if (IS_ERR(fid))
da977b2c 1450 return PTR_ERR(fid);
2bad8471 1451
50cc42ff 1452 if (!v9fs_proto_dotu(v9ses) && !v9fs_proto_dotl(v9ses))
bd238fb4 1453 return -EBADF;
2bad8471 1454
bd238fb4
LI
1455 st = p9_client_stat(fid);
1456 if (IS_ERR(st))
1457 return PTR_ERR(st);
2bad8471 1458
bd238fb4 1459 if (!(st->mode & P9_DMSYMLINK)) {
2bad8471 1460 retval = -EINVAL;
bd238fb4 1461 goto done;
2bad8471
EVH
1462 }
1463
1464 /* copy extension buffer into buffer */
51a87c55 1465 strncpy(buffer, st->extension, buflen);
2bad8471 1466
bd238fb4 1467 P9_DPRINTK(P9_DEBUG_VFS,
51a87c55 1468 "%s -> %s (%s)\n", dentry->d_name.name, st->extension, buffer);
2bad8471 1469
2511cd0b 1470 retval = strnlen(buffer, buflen);
bd238fb4
LI
1471done:
1472 kfree(st);
2bad8471
EVH
1473 return retval;
1474}
1475
2bad8471
EVH
1476/**
1477 * v9fs_vfs_follow_link - follow a symlink path
1478 * @dentry: dentry for symlink
1479 * @nd: nameidata
1480 *
1481 */
1482
1483static void *v9fs_vfs_follow_link(struct dentry *dentry, struct nameidata *nd)
1484{
1485 int len = 0;
1486 char *link = __getname();
1487
bd238fb4 1488 P9_DPRINTK(P9_DEBUG_VFS, "%s n", dentry->d_name.name);
2bad8471
EVH
1489
1490 if (!link)
1491 link = ERR_PTR(-ENOMEM);
1492 else {
16cce6d2 1493 len = v9fs_readlink(dentry, link, PATH_MAX);
2bad8471
EVH
1494
1495 if (len < 0) {
ce44eeb6 1496 __putname(link);
2bad8471
EVH
1497 link = ERR_PTR(len);
1498 } else
2511cd0b 1499 link[min(len, PATH_MAX-1)] = 0;
2bad8471
EVH
1500 }
1501 nd_set_link(nd, link);
1502
1503 return NULL;
1504}
1505
1506/**
1507 * v9fs_vfs_put_link - release a symlink path
1508 * @dentry: dentry for symlink
1509 * @nd: nameidata
ee443996 1510 * @p: unused
2bad8471
EVH
1511 *
1512 */
1513
ee443996
EVH
1514static void
1515v9fs_vfs_put_link(struct dentry *dentry, struct nameidata *nd, void *p)
2bad8471
EVH
1516{
1517 char *s = nd_get_link(nd);
1518
6ff23207
DG
1519 P9_DPRINTK(P9_DEBUG_VFS, " %s %s\n", dentry->d_name.name,
1520 IS_ERR(s) ? "<error>" : s);
2bad8471 1521 if (!IS_ERR(s))
ce44eeb6 1522 __putname(s);
2bad8471
EVH
1523}
1524
ee443996
EVH
1525/**
1526 * v9fs_vfs_mkspecial - create a special file
1527 * @dir: inode to create special file in
1528 * @dentry: dentry to create
1529 * @mode: mode to create special file
1530 * @extension: 9p2000.u format extension string representing special file
1531 *
1532 */
1533
531b1094
LI
1534static int v9fs_vfs_mkspecial(struct inode *dir, struct dentry *dentry,
1535 int mode, const char *extension)
2bad8471 1536{
bd238fb4 1537 u32 perm;
531b1094 1538 struct v9fs_session_info *v9ses;
bd238fb4 1539 struct p9_fid *fid;
2bad8471 1540
6a3124a3 1541 v9ses = v9fs_inode2v9ses(dir);
dd6102fb 1542 if (!v9fs_proto_dotu(v9ses)) {
bd238fb4 1543 P9_DPRINTK(P9_DEBUG_ERROR, "not extended\n");
6a3124a3 1544 return -EPERM;
2bad8471
EVH
1545 }
1546
da977b2c 1547 perm = unixmode2p9mode(v9ses, mode);
bd238fb4
LI
1548 fid = v9fs_create(v9ses, dir, dentry, (char *) extension, perm,
1549 P9_OREAD);
1550 if (IS_ERR(fid))
1551 return PTR_ERR(fid);
da977b2c 1552
bd238fb4 1553 p9_client_clunk(fid);
6a3124a3 1554 return 0;
531b1094
LI
1555}
1556
50cc42ff
VJJ
1557/**
1558 * v9fs_vfs_symlink_dotl - helper function to create symlinks
1559 * @dir: directory inode containing symlink
1560 * @dentry: dentry for symlink
1561 * @symname: symlink data
1562 *
1563 * See Also: 9P2000.L RFC for more information
1564 *
1565 */
1566
1567static int
1568v9fs_vfs_symlink_dotl(struct inode *dir, struct dentry *dentry,
1569 const char *symname)
1570{
1571 struct v9fs_session_info *v9ses;
1572 struct p9_fid *dfid;
1573 struct p9_fid *fid = NULL;
1574 struct inode *inode;
1575 struct p9_qid qid;
1576 char *name;
1577 int err;
1578 gid_t gid;
1579
1580 name = (char *) dentry->d_name.name;
1581 P9_DPRINTK(P9_DEBUG_VFS, "v9fs_vfs_symlink_dotl : %lu,%s,%s\n",
1582 dir->i_ino, name, symname);
1583 v9ses = v9fs_inode2v9ses(dir);
1584
1585 dfid = v9fs_fid_lookup(dentry->d_parent);
1586 if (IS_ERR(dfid)) {
1587 err = PTR_ERR(dfid);
1588 P9_DPRINTK(P9_DEBUG_VFS, "fid lookup failed %d\n", err);
1589 return err;
1590 }
1591
1592 gid = v9fs_get_fsgid_for_create(dir);
1593
1594 if (gid < 0) {
1595 P9_DPRINTK(P9_DEBUG_VFS, "v9fs_get_egid failed %d\n", gid);
1596 goto error;
1597 }
1598
1599 /* Server doesn't alter fid on TSYMLINK. Hence no need to clone it. */
1600 err = p9_client_symlink(dfid, name, (char *)symname, gid, &qid);
1601
1602 if (err < 0) {
1603 P9_DPRINTK(P9_DEBUG_VFS, "p9_client_symlink failed %d\n", err);
1604 goto error;
1605 }
1606
1607 if (v9ses->cache) {
1608 /* Now walk from the parent so we can get an unopened fid. */
1609 fid = p9_client_walk(dfid, 1, &name, 1);
1610 if (IS_ERR(fid)) {
1611 err = PTR_ERR(fid);
1612 P9_DPRINTK(P9_DEBUG_VFS, "p9_client_walk failed %d\n",
1613 err);
1614 fid = NULL;
1615 goto error;
1616 }
1617
1618 /* instantiate inode and assign the unopened fid to dentry */
1619 inode = v9fs_inode_from_fid(v9ses, fid, dir->i_sb);
1620 if (IS_ERR(inode)) {
1621 err = PTR_ERR(inode);
1622 P9_DPRINTK(P9_DEBUG_VFS, "inode creation failed %d\n",
1623 err);
1624 goto error;
1625 }
1626 dentry->d_op = &v9fs_cached_dentry_operations;
1627 d_instantiate(dentry, inode);
1628 err = v9fs_fid_add(dentry, fid);
1629 if (err < 0)
1630 goto error;
1631 fid = NULL;
1632 } else {
1633 /* Not in cached mode. No need to populate inode with stat */
1634 inode = v9fs_get_inode(dir->i_sb, S_IFLNK);
1635 if (IS_ERR(inode)) {
1636 err = PTR_ERR(inode);
1637 goto error;
1638 }
1639 dentry->d_op = &v9fs_dentry_operations;
1640 d_instantiate(dentry, inode);
1641 }
1642
1643error:
1644 if (fid)
1645 p9_client_clunk(fid);
1646
1647 return err;
1648}
1649
531b1094
LI
1650/**
1651 * v9fs_vfs_symlink - helper function to create symlinks
1652 * @dir: directory inode containing symlink
1653 * @dentry: dentry for symlink
1654 * @symname: symlink data
1655 *
ee443996 1656 * See Also: 9P2000.u RFC for more information
531b1094
LI
1657 *
1658 */
1659
1660static int
1661v9fs_vfs_symlink(struct inode *dir, struct dentry *dentry, const char *symname)
1662{
bd238fb4
LI
1663 P9_DPRINTK(P9_DEBUG_VFS, " %lu,%s,%s\n", dir->i_ino,
1664 dentry->d_name.name, symname);
531b1094
LI
1665
1666 return v9fs_vfs_mkspecial(dir, dentry, S_IFLNK, symname);
1667}
1668
1669/**
1670 * v9fs_vfs_link - create a hardlink
1671 * @old_dentry: dentry for file to link to
1672 * @dir: inode destination for new link
1673 * @dentry: dentry for link
1674 *
1675 */
1676
531b1094
LI
1677static int
1678v9fs_vfs_link(struct dentry *old_dentry, struct inode *dir,
1679 struct dentry *dentry)
1680{
1681 int retval;
bd238fb4 1682 struct p9_fid *oldfid;
531b1094
LI
1683 char *name;
1684
bd238fb4
LI
1685 P9_DPRINTK(P9_DEBUG_VFS,
1686 " %lu,%s,%s\n", dir->i_ino, dentry->d_name.name,
531b1094
LI
1687 old_dentry->d_name.name);
1688
da977b2c 1689 oldfid = v9fs_fid_clone(old_dentry);
ba17674f 1690 if (IS_ERR(oldfid))
da977b2c 1691 return PTR_ERR(oldfid);
531b1094
LI
1692
1693 name = __getname();
da977b2c
EVH
1694 if (unlikely(!name)) {
1695 retval = -ENOMEM;
1696 goto clunk_fid;
1697 }
0710d36a 1698
16cce6d2 1699 sprintf(name, "%d\n", oldfid->fid);
bd238fb4 1700 retval = v9fs_vfs_mkspecial(dir, dentry, P9_DMLINK, name);
531b1094 1701 __putname(name);
2bad8471 1702
da977b2c 1703clunk_fid:
bd238fb4 1704 p9_client_clunk(oldfid);
2bad8471
EVH
1705 return retval;
1706}
1707
09d34ee5
EVH
1708/**
1709 * v9fs_vfs_link_dotl - create a hardlink for dotl
1710 * @old_dentry: dentry for file to link to
1711 * @dir: inode destination for new link
1712 * @dentry: dentry for link
1713 *
1714 */
1715
1716static int
1717v9fs_vfs_link_dotl(struct dentry *old_dentry, struct inode *dir,
1718 struct dentry *dentry)
1719{
1720 int err;
1721 struct p9_fid *dfid, *oldfid;
1722 char *name;
1723 struct v9fs_session_info *v9ses;
1724 struct dentry *dir_dentry;
1725
1726 P9_DPRINTK(P9_DEBUG_VFS, "dir ino: %lu, old_name: %s, new_name: %s\n",
1727 dir->i_ino, old_dentry->d_name.name,
1728 dentry->d_name.name);
1729
1730 v9ses = v9fs_inode2v9ses(dir);
1731 dir_dentry = v9fs_dentry_from_dir_inode(dir);
1732 dfid = v9fs_fid_lookup(dir_dentry);
1733 if (IS_ERR(dfid))
1734 return PTR_ERR(dfid);
1735
1736 oldfid = v9fs_fid_lookup(old_dentry);
1737 if (IS_ERR(oldfid))
1738 return PTR_ERR(oldfid);
1739
1740 name = (char *) dentry->d_name.name;
1741
1742 err = p9_client_link(dfid, oldfid, (char *)dentry->d_name.name);
1743
1744 if (err < 0) {
1745 P9_DPRINTK(P9_DEBUG_VFS, "p9_client_link failed %d\n", err);
1746 return err;
1747 }
1748
1749 if (v9ses->cache == CACHE_LOOSE || v9ses->cache == CACHE_FSCACHE) {
1750 /* Get the latest stat info from server. */
1751 struct p9_fid *fid;
1752 struct p9_stat_dotl *st;
1753
1754 fid = v9fs_fid_lookup(old_dentry);
1755 if (IS_ERR(fid))
1756 return PTR_ERR(fid);
1757
1758 st = p9_client_getattr_dotl(fid, P9_STATS_BASIC);
1759 if (IS_ERR(st))
1760 return PTR_ERR(st);
1761
1762 v9fs_stat2inode_dotl(st, old_dentry->d_inode);
1763
1764 kfree(st);
1765 } else {
1766 /* Caching disabled. No need to get upto date stat info.
1767 * This dentry will be released immediately. So, just i_count++
1768 */
1769 atomic_inc(&old_dentry->d_inode->i_count);
1770 }
1771
1772 dentry->d_op = old_dentry->d_op;
1773 d_instantiate(dentry, old_dentry->d_inode);
1774
1775 return err;
1776}
1777
2bad8471
EVH
1778/**
1779 * v9fs_vfs_mknod - create a special file
1780 * @dir: inode destination for new link
1781 * @dentry: dentry for file
1782 * @mode: mode for creation
ee443996 1783 * @rdev: device associated with special file
2bad8471
EVH
1784 *
1785 */
1786
1787static int
1788v9fs_vfs_mknod(struct inode *dir, struct dentry *dentry, int mode, dev_t rdev)
1789{
531b1094
LI
1790 int retval;
1791 char *name;
2bad8471 1792
bd238fb4
LI
1793 P9_DPRINTK(P9_DEBUG_VFS,
1794 " %lu,%s mode: %x MAJOR: %u MINOR: %u\n", dir->i_ino,
2bad8471
EVH
1795 dentry->d_name.name, mode, MAJOR(rdev), MINOR(rdev));
1796
531b1094
LI
1797 if (!new_valid_dev(rdev))
1798 return -EINVAL;
2bad8471 1799
531b1094 1800 name = __getname();
c0291a05
ET
1801 if (!name)
1802 return -ENOMEM;
2bad8471
EVH
1803 /* build extension */
1804 if (S_ISBLK(mode))
531b1094 1805 sprintf(name, "b %u %u", MAJOR(rdev), MINOR(rdev));
2bad8471 1806 else if (S_ISCHR(mode))
531b1094 1807 sprintf(name, "c %u %u", MAJOR(rdev), MINOR(rdev));
73c592b9 1808 else if (S_ISFIFO(mode))
531b1094 1809 *name = 0;
75cc5c9b
VJ
1810 else if (S_ISSOCK(mode))
1811 *name = 0;
2bad8471 1812 else {
531b1094
LI
1813 __putname(name);
1814 return -EINVAL;
2bad8471
EVH
1815 }
1816
531b1094
LI
1817 retval = v9fs_vfs_mkspecial(dir, dentry, mode, name);
1818 __putname(name);
2bad8471
EVH
1819
1820 return retval;
1821}
1822
4b43516a
MK
1823/**
1824 * v9fs_vfs_mknod_dotl - create a special file
1825 * @dir: inode destination for new link
1826 * @dentry: dentry for file
1827 * @mode: mode for creation
1828 * @rdev: device associated with special file
1829 *
1830 */
1831static int
1832v9fs_vfs_mknod_dotl(struct inode *dir, struct dentry *dentry, int mode,
1833 dev_t rdev)
1834{
1835 int err;
1836 char *name;
1837 struct v9fs_session_info *v9ses;
1838 struct p9_fid *fid = NULL, *dfid = NULL;
1839 struct inode *inode;
1840 gid_t gid;
1841 struct p9_qid qid;
01a622bd 1842 struct dentry *dir_dentry;
4b43516a
MK
1843
1844 P9_DPRINTK(P9_DEBUG_VFS,
1845 " %lu,%s mode: %x MAJOR: %u MINOR: %u\n", dir->i_ino,
1846 dentry->d_name.name, mode, MAJOR(rdev), MINOR(rdev));
1847
1848 if (!new_valid_dev(rdev))
1849 return -EINVAL;
1850
1851 v9ses = v9fs_inode2v9ses(dir);
1852 dir_dentry = v9fs_dentry_from_dir_inode(dir);
01a622bd 1853 dfid = v9fs_fid_lookup(dir_dentry);
4b43516a
MK
1854 if (IS_ERR(dfid)) {
1855 err = PTR_ERR(dfid);
1856 P9_DPRINTK(P9_DEBUG_VFS, "fid lookup failed %d\n", err);
1857 dfid = NULL;
1858 goto error;
1859 }
1860
1861 gid = v9fs_get_fsgid_for_create(dir);
1862 if (gid < 0) {
1863 P9_DPRINTK(P9_DEBUG_VFS, "v9fs_get_fsgid_for_create failed\n");
1864 goto error;
1865 }
1866
1867 name = (char *) dentry->d_name.name;
1868
1869 err = p9_client_mknod_dotl(dfid, name, mode, rdev, gid, &qid);
1870 if (err < 0)
1871 goto error;
1872
1873 /* instantiate inode and assign the unopened fid to the dentry */
1874 if (v9ses->cache == CACHE_LOOSE || v9ses->cache == CACHE_FSCACHE) {
1875 fid = p9_client_walk(dfid, 1, &name, 1);
1876 if (IS_ERR(fid)) {
1877 err = PTR_ERR(fid);
1878 P9_DPRINTK(P9_DEBUG_VFS, "p9_client_walk failed %d\n",
1879 err);
1880 fid = NULL;
1881 goto error;
1882 }
1883
1884 inode = v9fs_inode_from_fid(v9ses, fid, dir->i_sb);
1885 if (IS_ERR(inode)) {
1886 err = PTR_ERR(inode);
1887 P9_DPRINTK(P9_DEBUG_VFS, "inode creation failed %d\n",
1888 err);
1889 goto error;
1890 }
1891 dentry->d_op = &v9fs_cached_dentry_operations;
1892 d_instantiate(dentry, inode);
1893 err = v9fs_fid_add(dentry, fid);
1894 if (err < 0)
1895 goto error;
1896 fid = NULL;
1897 } else {
1898 /*
1899 * Not in cached mode. No need to populate inode with stat.
1900 * socket syscall returns a fd, so we need instantiate
1901 */
1902 inode = v9fs_get_inode(dir->i_sb, mode);
1903 if (IS_ERR(inode)) {
1904 err = PTR_ERR(inode);
1905 goto error;
1906 }
1907 dentry->d_op = &v9fs_dentry_operations;
1908 d_instantiate(dentry, inode);
1909 }
1910
1911error:
1912 if (fid)
1913 p9_client_clunk(fid);
1914 return err;
1915}
1916
9b6533c9
SK
1917static const struct inode_operations v9fs_dir_inode_operations_dotu = {
1918 .create = v9fs_vfs_create,
1919 .lookup = v9fs_vfs_lookup,
1920 .symlink = v9fs_vfs_symlink,
50cc42ff 1921 .link = v9fs_vfs_link,
9b6533c9
SK
1922 .unlink = v9fs_vfs_unlink,
1923 .mkdir = v9fs_vfs_mkdir,
1924 .rmdir = v9fs_vfs_rmdir,
4b43516a 1925 .mknod = v9fs_vfs_mknod_dotl,
9b6533c9
SK
1926 .rename = v9fs_vfs_rename,
1927 .getattr = v9fs_vfs_getattr,
1928 .setattr = v9fs_vfs_setattr,
1929};
1930
1931static const struct inode_operations v9fs_dir_inode_operations_dotl = {
5643135a 1932 .create = v9fs_vfs_create_dotl,
2bad8471 1933 .lookup = v9fs_vfs_lookup,
50cc42ff
VJJ
1934 .link = v9fs_vfs_link_dotl,
1935 .symlink = v9fs_vfs_symlink_dotl,
2bad8471 1936 .unlink = v9fs_vfs_unlink,
01a622bd 1937 .mkdir = v9fs_vfs_mkdir_dotl,
2bad8471 1938 .rmdir = v9fs_vfs_rmdir,
4b43516a 1939 .mknod = v9fs_vfs_mknod_dotl,
2bad8471 1940 .rename = v9fs_vfs_rename,
f0853122 1941 .getattr = v9fs_vfs_getattr_dotl,
87d7845a 1942 .setattr = v9fs_vfs_setattr_dotl,
ebf46264
AK
1943 .setxattr = generic_setxattr,
1944 .getxattr = generic_getxattr,
1945 .removexattr = generic_removexattr,
1946 .listxattr = v9fs_listxattr,
1947
2bad8471
EVH
1948};
1949
754661f1 1950static const struct inode_operations v9fs_dir_inode_operations = {
b501611a
EVH
1951 .create = v9fs_vfs_create,
1952 .lookup = v9fs_vfs_lookup,
1953 .unlink = v9fs_vfs_unlink,
1954 .mkdir = v9fs_vfs_mkdir,
1955 .rmdir = v9fs_vfs_rmdir,
1956 .mknod = v9fs_vfs_mknod,
1957 .rename = v9fs_vfs_rename,
1958 .getattr = v9fs_vfs_getattr,
1959 .setattr = v9fs_vfs_setattr,
1960};
1961
754661f1 1962static const struct inode_operations v9fs_file_inode_operations = {
2bad8471
EVH
1963 .getattr = v9fs_vfs_getattr,
1964 .setattr = v9fs_vfs_setattr,
1965};
1966
9b6533c9 1967static const struct inode_operations v9fs_file_inode_operations_dotl = {
f0853122 1968 .getattr = v9fs_vfs_getattr_dotl,
87d7845a 1969 .setattr = v9fs_vfs_setattr_dotl,
ebf46264
AK
1970 .setxattr = generic_setxattr,
1971 .getxattr = generic_getxattr,
1972 .removexattr = generic_removexattr,
1973 .listxattr = v9fs_listxattr,
9b6533c9
SK
1974};
1975
754661f1 1976static const struct inode_operations v9fs_symlink_inode_operations = {
204f2f0e 1977 .readlink = generic_readlink,
2bad8471
EVH
1978 .follow_link = v9fs_vfs_follow_link,
1979 .put_link = v9fs_vfs_put_link,
1980 .getattr = v9fs_vfs_getattr,
1981 .setattr = v9fs_vfs_setattr,
1982};
9b6533c9
SK
1983
1984static const struct inode_operations v9fs_symlink_inode_operations_dotl = {
1985 .readlink = generic_readlink,
1986 .follow_link = v9fs_vfs_follow_link,
1987 .put_link = v9fs_vfs_put_link,
f0853122 1988 .getattr = v9fs_vfs_getattr_dotl,
87d7845a 1989 .setattr = v9fs_vfs_setattr_dotl,
ebf46264
AK
1990 .setxattr = generic_setxattr,
1991 .getxattr = generic_getxattr,
1992 .removexattr = generic_removexattr,
1993 .listxattr = v9fs_listxattr,
9b6533c9 1994};