Add linux-next specific files for 20110716
[linux-2.6/next.git] / fs / 9p / vfs_inode_dotl.c
blobcaa63ef9006ffbc39dbdc0c620e57b2a30f11532
1 /*
2 * linux/fs/9p/vfs_inode_dotl.c
4 * This file contains vfs inode ops for the 9P2000.L protocol.
6 * Copyright (C) 2004 by Eric Van Hensbergen <ericvh@gmail.com>
7 * Copyright (C) 2002 by Ron Minnich <rminnich@lanl.gov>
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License version 2
11 * as published by the Free Software Foundation.
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.
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
26 #include <linux/module.h>
27 #include <linux/errno.h>
28 #include <linux/fs.h>
29 #include <linux/file.h>
30 #include <linux/pagemap.h>
31 #include <linux/stat.h>
32 #include <linux/string.h>
33 #include <linux/inet.h>
34 #include <linux/namei.h>
35 #include <linux/idr.h>
36 #include <linux/sched.h>
37 #include <linux/slab.h>
38 #include <linux/xattr.h>
39 #include <linux/posix_acl.h>
40 #include <net/9p/9p.h>
41 #include <net/9p/client.h>
43 #include "v9fs.h"
44 #include "v9fs_vfs.h"
45 #include "fid.h"
46 #include "cache.h"
47 #include "xattr.h"
48 #include "acl.h"
50 static int
51 v9fs_vfs_mknod_dotl(struct inode *dir, struct dentry *dentry, int omode,
52 dev_t rdev);
54 /**
55 * v9fs_get_fsgid_for_create - Helper function to get the gid for creating a
56 * new file system object. This checks the S_ISGID to determine the owning
57 * group of the new file system object.
60 static gid_t v9fs_get_fsgid_for_create(struct inode *dir_inode)
62 BUG_ON(dir_inode == NULL);
64 if (dir_inode->i_mode & S_ISGID) {
65 /* set_gid bit is set.*/
66 return dir_inode->i_gid;
68 return current_fsgid();
71 /**
72 * v9fs_dentry_from_dir_inode - helper function to get the dentry from
73 * dir inode.
77 static struct dentry *v9fs_dentry_from_dir_inode(struct inode *inode)
79 struct dentry *dentry;
81 spin_lock(&inode->i_lock);
82 /* Directory should have only one entry. */
83 BUG_ON(S_ISDIR(inode->i_mode) && !list_is_singular(&inode->i_dentry));
84 dentry = list_entry(inode->i_dentry.next, struct dentry, d_alias);
85 spin_unlock(&inode->i_lock);
86 return dentry;
89 static int v9fs_test_inode_dotl(struct inode *inode, void *data)
91 struct v9fs_inode *v9inode = V9FS_I(inode);
92 struct p9_stat_dotl *st = (struct p9_stat_dotl *)data;
94 /* don't match inode of different type */
95 if ((inode->i_mode & S_IFMT) != (st->st_mode & S_IFMT))
96 return 0;
98 if (inode->i_generation != st->st_gen)
99 return 0;
101 /* compare qid details */
102 if (memcmp(&v9inode->qid.version,
103 &st->qid.version, sizeof(v9inode->qid.version)))
104 return 0;
106 if (v9inode->qid.type != st->qid.type)
107 return 0;
108 return 1;
111 static int v9fs_set_inode_dotl(struct inode *inode, void *data)
113 struct v9fs_inode *v9inode = V9FS_I(inode);
114 struct p9_stat_dotl *st = (struct p9_stat_dotl *)data;
116 memcpy(&v9inode->qid, &st->qid, sizeof(st->qid));
117 inode->i_generation = st->st_gen;
118 return 0;
121 static struct inode *v9fs_qid_iget_dotl(struct super_block *sb,
122 struct p9_qid *qid,
123 struct p9_fid *fid,
124 struct p9_stat_dotl *st)
126 int retval;
127 unsigned long i_ino;
128 struct inode *inode;
129 struct v9fs_session_info *v9ses = sb->s_fs_info;
131 i_ino = v9fs_qid2ino(qid);
132 inode = iget5_locked(sb, i_ino, v9fs_test_inode_dotl,
133 v9fs_set_inode_dotl, st);
134 if (!inode)
135 return ERR_PTR(-ENOMEM);
136 if (!(inode->i_state & I_NEW))
137 return inode;
139 * initialize the inode with the stat info
140 * FIXME!! we may need support for stale inodes
141 * later.
143 inode->i_ino = i_ino;
144 retval = v9fs_init_inode(v9ses, inode, st->st_mode);
145 if (retval)
146 goto error;
148 v9fs_stat2inode_dotl(st, inode);
149 #ifdef CONFIG_9P_FSCACHE
150 v9fs_cache_inode_get_cookie(inode);
151 #endif
152 retval = v9fs_get_acl(inode, fid);
153 if (retval)
154 goto error;
156 unlock_new_inode(inode);
157 return inode;
158 error:
159 unlock_new_inode(inode);
160 iput(inode);
161 return ERR_PTR(retval);
165 struct inode *
166 v9fs_inode_from_fid_dotl(struct v9fs_session_info *v9ses, struct p9_fid *fid,
167 struct super_block *sb)
169 struct p9_stat_dotl *st;
170 struct inode *inode = NULL;
172 st = p9_client_getattr_dotl(fid, P9_STATS_BASIC | P9_STATS_GEN);
173 if (IS_ERR(st))
174 return ERR_CAST(st);
176 inode = v9fs_qid_iget_dotl(sb, &st->qid, fid, st);
177 kfree(st);
178 return inode;
182 * v9fs_vfs_create_dotl - VFS hook to create files for 9P2000.L protocol.
183 * @dir: directory inode that is being created
184 * @dentry: dentry that is being deleted
185 * @mode: create permissions
186 * @nd: path information
190 static int
191 v9fs_vfs_create_dotl(struct inode *dir, struct dentry *dentry, int omode,
192 struct nameidata *nd)
194 int err = 0;
195 gid_t gid;
196 int flags;
197 mode_t mode;
198 char *name = NULL;
199 struct file *filp;
200 struct p9_qid qid;
201 struct inode *inode;
202 struct p9_fid *fid = NULL;
203 struct v9fs_inode *v9inode;
204 struct p9_fid *dfid, *ofid, *inode_fid;
205 struct v9fs_session_info *v9ses;
206 struct posix_acl *pacl = NULL, *dacl = NULL;
208 v9ses = v9fs_inode2v9ses(dir);
209 if (nd && nd->flags & LOOKUP_OPEN)
210 flags = nd->intent.open.flags - 1;
211 else {
213 * create call without LOOKUP_OPEN is due
214 * to mknod of regular files. So use mknod
215 * operation.
217 return v9fs_vfs_mknod_dotl(dir, dentry, omode, 0);
220 name = (char *) dentry->d_name.name;
221 P9_DPRINTK(P9_DEBUG_VFS, "v9fs_vfs_create_dotl: name:%s flags:0x%x "
222 "mode:0x%x\n", name, flags, omode);
224 dfid = v9fs_fid_lookup(dentry->d_parent);
225 if (IS_ERR(dfid)) {
226 err = PTR_ERR(dfid);
227 P9_DPRINTK(P9_DEBUG_VFS, "fid lookup failed %d\n", err);
228 return err;
231 /* clone a fid to use for creation */
232 ofid = p9_client_walk(dfid, 0, NULL, 1);
233 if (IS_ERR(ofid)) {
234 err = PTR_ERR(ofid);
235 P9_DPRINTK(P9_DEBUG_VFS, "p9_client_walk failed %d\n", err);
236 return err;
239 gid = v9fs_get_fsgid_for_create(dir);
241 mode = omode;
242 /* Update mode based on ACL value */
243 err = v9fs_acl_mode(dir, &mode, &dacl, &pacl);
244 if (err) {
245 P9_DPRINTK(P9_DEBUG_VFS,
246 "Failed to get acl values in creat %d\n", err);
247 goto error;
249 err = p9_client_create_dotl(ofid, name, flags, mode, gid, &qid);
250 if (err < 0) {
251 P9_DPRINTK(P9_DEBUG_VFS,
252 "p9_client_open_dotl failed in creat %d\n",
253 err);
254 goto error;
256 v9fs_invalidate_inode_attr(dir);
258 /* instantiate inode and assign the unopened fid to the dentry */
259 fid = p9_client_walk(dfid, 1, &name, 1);
260 if (IS_ERR(fid)) {
261 err = PTR_ERR(fid);
262 P9_DPRINTK(P9_DEBUG_VFS, "p9_client_walk failed %d\n", err);
263 fid = NULL;
264 goto error;
266 inode = v9fs_get_inode_from_fid(v9ses, fid, dir->i_sb);
267 if (IS_ERR(inode)) {
268 err = PTR_ERR(inode);
269 P9_DPRINTK(P9_DEBUG_VFS, "inode creation failed %d\n", err);
270 goto error;
272 d_instantiate(dentry, inode);
273 err = v9fs_fid_add(dentry, fid);
274 if (err < 0)
275 goto error;
277 /* Now set the ACL based on the default value */
278 v9fs_set_create_acl(dentry, dacl, pacl);
280 v9inode = V9FS_I(inode);
281 mutex_lock(&v9inode->v_mutex);
282 if (v9ses->cache && !v9inode->writeback_fid &&
283 ((flags & O_ACCMODE) != O_RDONLY)) {
285 * clone a fid and add it to writeback_fid
286 * we do it during open time instead of
287 * page dirty time via write_begin/page_mkwrite
288 * because we want write after unlink usecase
289 * to work.
291 inode_fid = v9fs_writeback_fid(dentry);
292 if (IS_ERR(inode_fid)) {
293 err = PTR_ERR(inode_fid);
294 mutex_unlock(&v9inode->v_mutex);
295 goto err_clunk_old_fid;
297 v9inode->writeback_fid = (void *) inode_fid;
299 mutex_unlock(&v9inode->v_mutex);
300 /* Since we are opening a file, assign the open fid to the file */
301 filp = lookup_instantiate_filp(nd, dentry, generic_file_open);
302 if (IS_ERR(filp)) {
303 err = PTR_ERR(filp);
304 goto err_clunk_old_fid;
306 filp->private_data = ofid;
307 #ifdef CONFIG_9P_FSCACHE
308 if (v9ses->cache)
309 v9fs_cache_inode_set_cookie(inode, filp);
310 #endif
311 return 0;
313 error:
314 if (fid)
315 p9_client_clunk(fid);
316 err_clunk_old_fid:
317 if (ofid)
318 p9_client_clunk(ofid);
319 return err;
323 * v9fs_vfs_mkdir_dotl - VFS mkdir hook to create a directory
324 * @dir: inode that is being unlinked
325 * @dentry: dentry that is being unlinked
326 * @mode: mode for new directory
330 static int v9fs_vfs_mkdir_dotl(struct inode *dir,
331 struct dentry *dentry, int omode)
333 int err;
334 struct v9fs_session_info *v9ses;
335 struct p9_fid *fid = NULL, *dfid = NULL;
336 gid_t gid;
337 char *name;
338 mode_t mode;
339 struct inode *inode;
340 struct p9_qid qid;
341 struct dentry *dir_dentry;
342 struct posix_acl *dacl = NULL, *pacl = NULL;
344 P9_DPRINTK(P9_DEBUG_VFS, "name %s\n", dentry->d_name.name);
345 err = 0;
346 v9ses = v9fs_inode2v9ses(dir);
348 omode |= S_IFDIR;
349 if (dir->i_mode & S_ISGID)
350 omode |= S_ISGID;
352 dir_dentry = v9fs_dentry_from_dir_inode(dir);
353 dfid = v9fs_fid_lookup(dir_dentry);
354 if (IS_ERR(dfid)) {
355 err = PTR_ERR(dfid);
356 P9_DPRINTK(P9_DEBUG_VFS, "fid lookup failed %d\n", err);
357 dfid = NULL;
358 goto error;
361 gid = v9fs_get_fsgid_for_create(dir);
362 mode = omode;
363 /* Update mode based on ACL value */
364 err = v9fs_acl_mode(dir, &mode, &dacl, &pacl);
365 if (err) {
366 P9_DPRINTK(P9_DEBUG_VFS,
367 "Failed to get acl values in mkdir %d\n", err);
368 goto error;
370 name = (char *) dentry->d_name.name;
371 err = p9_client_mkdir_dotl(dfid, name, mode, gid, &qid);
372 if (err < 0)
373 goto error;
375 /* instantiate inode and assign the unopened fid to the dentry */
376 if (v9ses->cache == CACHE_LOOSE || v9ses->cache == CACHE_FSCACHE) {
377 fid = p9_client_walk(dfid, 1, &name, 1);
378 if (IS_ERR(fid)) {
379 err = PTR_ERR(fid);
380 P9_DPRINTK(P9_DEBUG_VFS, "p9_client_walk failed %d\n",
381 err);
382 fid = NULL;
383 goto error;
386 inode = v9fs_get_inode_from_fid(v9ses, fid, dir->i_sb);
387 if (IS_ERR(inode)) {
388 err = PTR_ERR(inode);
389 P9_DPRINTK(P9_DEBUG_VFS, "inode creation failed %d\n",
390 err);
391 goto error;
393 d_instantiate(dentry, inode);
394 err = v9fs_fid_add(dentry, fid);
395 if (err < 0)
396 goto error;
397 fid = NULL;
398 } else {
400 * Not in cached mode. No need to populate
401 * inode with stat. We need to get an inode
402 * so that we can set the acl with dentry
404 inode = v9fs_get_inode(dir->i_sb, mode);
405 if (IS_ERR(inode)) {
406 err = PTR_ERR(inode);
407 goto error;
409 d_instantiate(dentry, inode);
411 /* Now set the ACL based on the default value */
412 v9fs_set_create_acl(dentry, dacl, pacl);
413 inc_nlink(dir);
414 v9fs_invalidate_inode_attr(dir);
415 error:
416 if (fid)
417 p9_client_clunk(fid);
418 return err;
421 static int
422 v9fs_vfs_getattr_dotl(struct vfsmount *mnt, struct dentry *dentry,
423 struct kstat *stat)
425 int err;
426 struct v9fs_session_info *v9ses;
427 struct p9_fid *fid;
428 struct p9_stat_dotl *st;
430 P9_DPRINTK(P9_DEBUG_VFS, "dentry: %p\n", dentry);
431 err = -EPERM;
432 v9ses = v9fs_dentry2v9ses(dentry);
433 if (v9ses->cache == CACHE_LOOSE || v9ses->cache == CACHE_FSCACHE) {
434 generic_fillattr(dentry->d_inode, stat);
435 return 0;
437 fid = v9fs_fid_lookup(dentry);
438 if (IS_ERR(fid))
439 return PTR_ERR(fid);
441 /* Ask for all the fields in stat structure. Server will return
442 * whatever it supports
445 st = p9_client_getattr_dotl(fid, P9_STATS_ALL);
446 if (IS_ERR(st))
447 return PTR_ERR(st);
449 v9fs_stat2inode_dotl(st, dentry->d_inode);
450 generic_fillattr(dentry->d_inode, stat);
451 /* Change block size to what the server returned */
452 stat->blksize = st->st_blksize;
454 kfree(st);
455 return 0;
459 * v9fs_vfs_setattr_dotl - set file metadata
460 * @dentry: file whose metadata to set
461 * @iattr: metadata assignment structure
465 int v9fs_vfs_setattr_dotl(struct dentry *dentry, struct iattr *iattr)
467 int retval;
468 struct v9fs_session_info *v9ses;
469 struct p9_fid *fid;
470 struct p9_iattr_dotl p9attr;
472 P9_DPRINTK(P9_DEBUG_VFS, "\n");
474 retval = inode_change_ok(dentry->d_inode, iattr);
475 if (retval)
476 return retval;
478 p9attr.valid = iattr->ia_valid;
479 p9attr.mode = iattr->ia_mode;
480 p9attr.uid = iattr->ia_uid;
481 p9attr.gid = iattr->ia_gid;
482 p9attr.size = iattr->ia_size;
483 p9attr.atime_sec = iattr->ia_atime.tv_sec;
484 p9attr.atime_nsec = iattr->ia_atime.tv_nsec;
485 p9attr.mtime_sec = iattr->ia_mtime.tv_sec;
486 p9attr.mtime_nsec = iattr->ia_mtime.tv_nsec;
488 retval = -EPERM;
489 v9ses = v9fs_dentry2v9ses(dentry);
490 fid = v9fs_fid_lookup(dentry);
491 if (IS_ERR(fid))
492 return PTR_ERR(fid);
494 /* Write all dirty data */
495 if (S_ISREG(dentry->d_inode->i_mode))
496 filemap_write_and_wait(dentry->d_inode->i_mapping);
498 retval = p9_client_setattr(fid, &p9attr);
499 if (retval < 0)
500 return retval;
502 if ((iattr->ia_valid & ATTR_SIZE) &&
503 iattr->ia_size != i_size_read(dentry->d_inode))
504 truncate_setsize(dentry->d_inode, iattr->ia_size);
506 v9fs_invalidate_inode_attr(dentry->d_inode);
507 setattr_copy(dentry->d_inode, iattr);
508 mark_inode_dirty(dentry->d_inode);
509 if (iattr->ia_valid & ATTR_MODE) {
510 /* We also want to update ACL when we update mode bits */
511 retval = v9fs_acl_chmod(dentry);
512 if (retval < 0)
513 return retval;
515 return 0;
519 * v9fs_stat2inode_dotl - populate an inode structure with stat info
520 * @stat: stat structure
521 * @inode: inode to populate
522 * @sb: superblock of filesystem
526 void
527 v9fs_stat2inode_dotl(struct p9_stat_dotl *stat, struct inode *inode)
529 struct v9fs_inode *v9inode = V9FS_I(inode);
531 if ((stat->st_result_mask & P9_STATS_BASIC) == P9_STATS_BASIC) {
532 inode->i_atime.tv_sec = stat->st_atime_sec;
533 inode->i_atime.tv_nsec = stat->st_atime_nsec;
534 inode->i_mtime.tv_sec = stat->st_mtime_sec;
535 inode->i_mtime.tv_nsec = stat->st_mtime_nsec;
536 inode->i_ctime.tv_sec = stat->st_ctime_sec;
537 inode->i_ctime.tv_nsec = stat->st_ctime_nsec;
538 inode->i_uid = stat->st_uid;
539 inode->i_gid = stat->st_gid;
540 inode->i_nlink = stat->st_nlink;
541 inode->i_mode = stat->st_mode;
542 inode->i_rdev = new_decode_dev(stat->st_rdev);
544 if ((S_ISBLK(inode->i_mode)) || (S_ISCHR(inode->i_mode)))
545 init_special_inode(inode, inode->i_mode, inode->i_rdev);
547 i_size_write(inode, stat->st_size);
548 inode->i_blocks = stat->st_blocks;
549 } else {
550 if (stat->st_result_mask & P9_STATS_ATIME) {
551 inode->i_atime.tv_sec = stat->st_atime_sec;
552 inode->i_atime.tv_nsec = stat->st_atime_nsec;
554 if (stat->st_result_mask & P9_STATS_MTIME) {
555 inode->i_mtime.tv_sec = stat->st_mtime_sec;
556 inode->i_mtime.tv_nsec = stat->st_mtime_nsec;
558 if (stat->st_result_mask & P9_STATS_CTIME) {
559 inode->i_ctime.tv_sec = stat->st_ctime_sec;
560 inode->i_ctime.tv_nsec = stat->st_ctime_nsec;
562 if (stat->st_result_mask & P9_STATS_UID)
563 inode->i_uid = stat->st_uid;
564 if (stat->st_result_mask & P9_STATS_GID)
565 inode->i_gid = stat->st_gid;
566 if (stat->st_result_mask & P9_STATS_NLINK)
567 inode->i_nlink = stat->st_nlink;
568 if (stat->st_result_mask & P9_STATS_MODE) {
569 inode->i_mode = stat->st_mode;
570 if ((S_ISBLK(inode->i_mode)) ||
571 (S_ISCHR(inode->i_mode)))
572 init_special_inode(inode, inode->i_mode,
573 inode->i_rdev);
575 if (stat->st_result_mask & P9_STATS_RDEV)
576 inode->i_rdev = new_decode_dev(stat->st_rdev);
577 if (stat->st_result_mask & P9_STATS_SIZE)
578 i_size_write(inode, stat->st_size);
579 if (stat->st_result_mask & P9_STATS_BLOCKS)
580 inode->i_blocks = stat->st_blocks;
582 if (stat->st_result_mask & P9_STATS_GEN)
583 inode->i_generation = stat->st_gen;
585 /* Currently we don't support P9_STATS_BTIME and P9_STATS_DATA_VERSION
586 * because the inode structure does not have fields for them.
588 v9inode->cache_validity &= ~V9FS_INO_INVALID_ATTR;
591 static int
592 v9fs_vfs_symlink_dotl(struct inode *dir, struct dentry *dentry,
593 const char *symname)
595 int err;
596 gid_t gid;
597 char *name;
598 struct p9_qid qid;
599 struct inode *inode;
600 struct p9_fid *dfid;
601 struct p9_fid *fid = NULL;
602 struct v9fs_session_info *v9ses;
604 name = (char *) dentry->d_name.name;
605 P9_DPRINTK(P9_DEBUG_VFS, "v9fs_vfs_symlink_dotl : %lu,%s,%s\n",
606 dir->i_ino, name, symname);
607 v9ses = v9fs_inode2v9ses(dir);
609 dfid = v9fs_fid_lookup(dentry->d_parent);
610 if (IS_ERR(dfid)) {
611 err = PTR_ERR(dfid);
612 P9_DPRINTK(P9_DEBUG_VFS, "fid lookup failed %d\n", err);
613 return err;
616 gid = v9fs_get_fsgid_for_create(dir);
618 /* Server doesn't alter fid on TSYMLINK. Hence no need to clone it. */
619 err = p9_client_symlink(dfid, name, (char *)symname, gid, &qid);
621 if (err < 0) {
622 P9_DPRINTK(P9_DEBUG_VFS, "p9_client_symlink failed %d\n", err);
623 goto error;
626 v9fs_invalidate_inode_attr(dir);
627 if (v9ses->cache) {
628 /* Now walk from the parent so we can get an unopened fid. */
629 fid = p9_client_walk(dfid, 1, &name, 1);
630 if (IS_ERR(fid)) {
631 err = PTR_ERR(fid);
632 P9_DPRINTK(P9_DEBUG_VFS, "p9_client_walk failed %d\n",
633 err);
634 fid = NULL;
635 goto error;
638 /* instantiate inode and assign the unopened fid to dentry */
639 inode = v9fs_get_inode_from_fid(v9ses, fid, dir->i_sb);
640 if (IS_ERR(inode)) {
641 err = PTR_ERR(inode);
642 P9_DPRINTK(P9_DEBUG_VFS, "inode creation failed %d\n",
643 err);
644 goto error;
646 d_instantiate(dentry, inode);
647 err = v9fs_fid_add(dentry, fid);
648 if (err < 0)
649 goto error;
650 fid = NULL;
651 } else {
652 /* Not in cached mode. No need to populate inode with stat */
653 inode = v9fs_get_inode(dir->i_sb, S_IFLNK);
654 if (IS_ERR(inode)) {
655 err = PTR_ERR(inode);
656 goto error;
658 d_instantiate(dentry, inode);
661 error:
662 if (fid)
663 p9_client_clunk(fid);
665 return err;
669 * v9fs_vfs_link_dotl - create a hardlink for dotl
670 * @old_dentry: dentry for file to link to
671 * @dir: inode destination for new link
672 * @dentry: dentry for link
676 static int
677 v9fs_vfs_link_dotl(struct dentry *old_dentry, struct inode *dir,
678 struct dentry *dentry)
680 int err;
681 char *name;
682 struct dentry *dir_dentry;
683 struct p9_fid *dfid, *oldfid;
684 struct v9fs_session_info *v9ses;
686 P9_DPRINTK(P9_DEBUG_VFS, "dir ino: %lu, old_name: %s, new_name: %s\n",
687 dir->i_ino, old_dentry->d_name.name,
688 dentry->d_name.name);
690 v9ses = v9fs_inode2v9ses(dir);
691 dir_dentry = v9fs_dentry_from_dir_inode(dir);
692 dfid = v9fs_fid_lookup(dir_dentry);
693 if (IS_ERR(dfid))
694 return PTR_ERR(dfid);
696 oldfid = v9fs_fid_lookup(old_dentry);
697 if (IS_ERR(oldfid))
698 return PTR_ERR(oldfid);
700 name = (char *) dentry->d_name.name;
702 err = p9_client_link(dfid, oldfid, (char *)dentry->d_name.name);
704 if (err < 0) {
705 P9_DPRINTK(P9_DEBUG_VFS, "p9_client_link failed %d\n", err);
706 return err;
709 v9fs_invalidate_inode_attr(dir);
710 if (v9ses->cache == CACHE_LOOSE || v9ses->cache == CACHE_FSCACHE) {
711 /* Get the latest stat info from server. */
712 struct p9_fid *fid;
713 fid = v9fs_fid_lookup(old_dentry);
714 if (IS_ERR(fid))
715 return PTR_ERR(fid);
717 v9fs_refresh_inode_dotl(fid, old_dentry->d_inode);
719 ihold(old_dentry->d_inode);
720 d_instantiate(dentry, old_dentry->d_inode);
722 return err;
726 * v9fs_vfs_mknod_dotl - create a special file
727 * @dir: inode destination for new link
728 * @dentry: dentry for file
729 * @mode: mode for creation
730 * @rdev: device associated with special file
733 static int
734 v9fs_vfs_mknod_dotl(struct inode *dir, struct dentry *dentry, int omode,
735 dev_t rdev)
737 int err;
738 gid_t gid;
739 char *name;
740 mode_t mode;
741 struct v9fs_session_info *v9ses;
742 struct p9_fid *fid = NULL, *dfid = NULL;
743 struct inode *inode;
744 struct p9_qid qid;
745 struct dentry *dir_dentry;
746 struct posix_acl *dacl = NULL, *pacl = NULL;
748 P9_DPRINTK(P9_DEBUG_VFS,
749 " %lu,%s mode: %x MAJOR: %u MINOR: %u\n", dir->i_ino,
750 dentry->d_name.name, omode, MAJOR(rdev), MINOR(rdev));
752 if (!new_valid_dev(rdev))
753 return -EINVAL;
755 v9ses = v9fs_inode2v9ses(dir);
756 dir_dentry = v9fs_dentry_from_dir_inode(dir);
757 dfid = v9fs_fid_lookup(dir_dentry);
758 if (IS_ERR(dfid)) {
759 err = PTR_ERR(dfid);
760 P9_DPRINTK(P9_DEBUG_VFS, "fid lookup failed %d\n", err);
761 dfid = NULL;
762 goto error;
765 gid = v9fs_get_fsgid_for_create(dir);
766 mode = omode;
767 /* Update mode based on ACL value */
768 err = v9fs_acl_mode(dir, &mode, &dacl, &pacl);
769 if (err) {
770 P9_DPRINTK(P9_DEBUG_VFS,
771 "Failed to get acl values in mknod %d\n", err);
772 goto error;
774 name = (char *) dentry->d_name.name;
776 err = p9_client_mknod_dotl(dfid, name, mode, rdev, gid, &qid);
777 if (err < 0)
778 goto error;
780 v9fs_invalidate_inode_attr(dir);
781 /* instantiate inode and assign the unopened fid to the dentry */
782 if (v9ses->cache == CACHE_LOOSE || v9ses->cache == CACHE_FSCACHE) {
783 fid = p9_client_walk(dfid, 1, &name, 1);
784 if (IS_ERR(fid)) {
785 err = PTR_ERR(fid);
786 P9_DPRINTK(P9_DEBUG_VFS, "p9_client_walk failed %d\n",
787 err);
788 fid = NULL;
789 goto error;
792 inode = v9fs_get_inode_from_fid(v9ses, fid, dir->i_sb);
793 if (IS_ERR(inode)) {
794 err = PTR_ERR(inode);
795 P9_DPRINTK(P9_DEBUG_VFS, "inode creation failed %d\n",
796 err);
797 goto error;
799 d_instantiate(dentry, inode);
800 err = v9fs_fid_add(dentry, fid);
801 if (err < 0)
802 goto error;
803 fid = NULL;
804 } else {
806 * Not in cached mode. No need to populate inode with stat.
807 * socket syscall returns a fd, so we need instantiate
809 inode = v9fs_get_inode(dir->i_sb, mode);
810 if (IS_ERR(inode)) {
811 err = PTR_ERR(inode);
812 goto error;
814 d_instantiate(dentry, inode);
816 /* Now set the ACL based on the default value */
817 v9fs_set_create_acl(dentry, dacl, pacl);
818 error:
819 if (fid)
820 p9_client_clunk(fid);
821 return err;
825 * v9fs_vfs_follow_link_dotl - follow a symlink path
826 * @dentry: dentry for symlink
827 * @nd: nameidata
831 static void *
832 v9fs_vfs_follow_link_dotl(struct dentry *dentry, struct nameidata *nd)
834 int retval;
835 struct p9_fid *fid;
836 char *link = __getname();
837 char *target;
839 P9_DPRINTK(P9_DEBUG_VFS, "%s\n", dentry->d_name.name);
841 if (!link) {
842 link = ERR_PTR(-ENOMEM);
843 goto ndset;
845 fid = v9fs_fid_lookup(dentry);
846 if (IS_ERR(fid)) {
847 __putname(link);
848 link = ERR_CAST(fid);
849 goto ndset;
851 retval = p9_client_readlink(fid, &target);
852 if (!retval) {
853 strcpy(link, target);
854 kfree(target);
855 goto ndset;
857 __putname(link);
858 link = ERR_PTR(retval);
859 ndset:
860 nd_set_link(nd, link);
861 return NULL;
864 int v9fs_refresh_inode_dotl(struct p9_fid *fid, struct inode *inode)
866 loff_t i_size;
867 struct p9_stat_dotl *st;
868 struct v9fs_session_info *v9ses;
870 v9ses = v9fs_inode2v9ses(inode);
871 st = p9_client_getattr_dotl(fid, P9_STATS_ALL);
872 if (IS_ERR(st))
873 return PTR_ERR(st);
875 spin_lock(&inode->i_lock);
877 * We don't want to refresh inode->i_size,
878 * because we may have cached data
880 i_size = inode->i_size;
881 v9fs_stat2inode_dotl(st, inode);
882 if (v9ses->cache)
883 inode->i_size = i_size;
884 spin_unlock(&inode->i_lock);
885 kfree(st);
886 return 0;
889 const struct inode_operations v9fs_dir_inode_operations_dotl = {
890 .create = v9fs_vfs_create_dotl,
891 .lookup = v9fs_vfs_lookup,
892 .link = v9fs_vfs_link_dotl,
893 .symlink = v9fs_vfs_symlink_dotl,
894 .unlink = v9fs_vfs_unlink,
895 .mkdir = v9fs_vfs_mkdir_dotl,
896 .rmdir = v9fs_vfs_rmdir,
897 .mknod = v9fs_vfs_mknod_dotl,
898 .rename = v9fs_vfs_rename,
899 .getattr = v9fs_vfs_getattr_dotl,
900 .setattr = v9fs_vfs_setattr_dotl,
901 .setxattr = generic_setxattr,
902 .getxattr = generic_getxattr,
903 .removexattr = generic_removexattr,
904 .listxattr = v9fs_listxattr,
905 .check_acl = v9fs_check_acl,
908 const struct inode_operations v9fs_file_inode_operations_dotl = {
909 .getattr = v9fs_vfs_getattr_dotl,
910 .setattr = v9fs_vfs_setattr_dotl,
911 .setxattr = generic_setxattr,
912 .getxattr = generic_getxattr,
913 .removexattr = generic_removexattr,
914 .listxattr = v9fs_listxattr,
915 .check_acl = v9fs_check_acl,
918 const struct inode_operations v9fs_symlink_inode_operations_dotl = {
919 .readlink = generic_readlink,
920 .follow_link = v9fs_vfs_follow_link_dotl,
921 .put_link = v9fs_vfs_put_link,
922 .getattr = v9fs_vfs_getattr_dotl,
923 .setattr = v9fs_vfs_setattr_dotl,
924 .setxattr = generic_setxattr,
925 .getxattr = generic_getxattr,
926 .removexattr = generic_removexattr,
927 .listxattr = v9fs_listxattr,