blob: 5c04d66afb1d96cf6a4d2485a20190ec7d52fdba [file] [log] [blame]
Aneesh Kumar K.V53c06f42011-01-10 13:51:47 -06001/*
2 * linux/fs/9p/vfs_inode_dotl.c
3 *
4 * This file contains vfs inode ops for the 9P2000.L protocol.
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
10 * it under the terms of the GNU General Public License version 2
11 * as published by the Free Software Foundation.
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>
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>
42
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"
49
50static int
51v9fs_vfs_mknod_dotl(struct inode *dir, struct dentry *dentry, int omode,
52 dev_t rdev);
53
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.
58 */
59
60static gid_t v9fs_get_fsgid_for_create(struct inode *dir_inode)
61{
62 BUG_ON(dir_inode == NULL);
63
64 if (dir_inode->i_mode & S_ISGID) {
65 /* set_gid bit is set.*/
66 return dir_inode->i_gid;
67 }
68 return current_fsgid();
69}
70
71/**
72 * v9fs_dentry_from_dir_inode - helper function to get the dentry from
73 * dir inode.
74 *
75 */
76
77static struct dentry *v9fs_dentry_from_dir_inode(struct inode *inode)
78{
79 struct dentry *dentry;
80
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;
87}
88
Aneesh Kumar K.V5ffc0cb2011-02-28 17:04:01 +053089static struct inode *v9fs_qid_iget_dotl(struct super_block *sb,
90 struct p9_qid *qid,
91 struct p9_fid *fid,
92 struct p9_stat_dotl *st)
93{
94 int retval;
95 unsigned long i_ino;
96 struct inode *inode;
97 struct v9fs_session_info *v9ses = sb->s_fs_info;
98
99 i_ino = v9fs_qid2ino(qid);
100 inode = iget_locked(sb, i_ino);
101 if (!inode)
102 return ERR_PTR(-ENOMEM);
103 if (!(inode->i_state & I_NEW))
104 return inode;
105 /*
106 * initialize the inode with the stat info
107 * FIXME!! we may need support for stale inodes
108 * later.
109 */
110 retval = v9fs_init_inode(v9ses, inode, st->st_mode);
111 if (retval)
112 goto error;
113
114 v9fs_stat2inode_dotl(st, inode);
115#ifdef CONFIG_9P_FSCACHE
Aneesh Kumar K.Va78ce052011-02-28 17:04:02 +0530116 v9fs_fscache_set_key(inode, &st->qid);
Aneesh Kumar K.V5ffc0cb2011-02-28 17:04:01 +0530117 v9fs_cache_inode_get_cookie(inode);
118#endif
119 retval = v9fs_get_acl(inode, fid);
120 if (retval)
121 goto error;
122
123 unlock_new_inode(inode);
124 return inode;
125error:
126 unlock_new_inode(inode);
127 iput(inode);
128 return ERR_PTR(retval);
129
130}
131
Aneesh Kumar K.V53c06f42011-01-10 13:51:47 -0600132struct inode *
Aneesh Kumar K.Va78ce052011-02-28 17:04:02 +0530133v9fs_inode_from_fid_dotl(struct v9fs_session_info *v9ses, struct p9_fid *fid,
134 struct super_block *sb)
Aneesh Kumar K.V53c06f42011-01-10 13:51:47 -0600135{
Aneesh Kumar K.V53c06f42011-01-10 13:51:47 -0600136 struct p9_stat_dotl *st;
Aneesh Kumar K.V5ffc0cb2011-02-28 17:04:01 +0530137 struct inode *inode = NULL;
Aneesh Kumar K.V53c06f42011-01-10 13:51:47 -0600138
139 st = p9_client_getattr_dotl(fid, P9_STATS_BASIC);
140 if (IS_ERR(st))
141 return ERR_CAST(st);
142
Aneesh Kumar K.V5ffc0cb2011-02-28 17:04:01 +0530143 inode = v9fs_qid_iget_dotl(sb, &st->qid, fid, st);
Aneesh Kumar K.V53c06f42011-01-10 13:51:47 -0600144 kfree(st);
Aneesh Kumar K.V5ffc0cb2011-02-28 17:04:01 +0530145 return inode;
Aneesh Kumar K.V53c06f42011-01-10 13:51:47 -0600146}
147
148/**
149 * v9fs_vfs_create_dotl - VFS hook to create files for 9P2000.L protocol.
150 * @dir: directory inode that is being created
151 * @dentry: dentry that is being deleted
152 * @mode: create permissions
153 * @nd: path information
154 *
155 */
156
157static int
158v9fs_vfs_create_dotl(struct inode *dir, struct dentry *dentry, int omode,
159 struct nameidata *nd)
160{
161 int err = 0;
Aneesh Kumar K.V53c06f42011-01-10 13:51:47 -0600162 gid_t gid;
163 int flags;
164 mode_t mode;
Aneesh Kumar K.V6b39f6d2011-02-28 17:04:03 +0530165 char *name = NULL;
Aneesh Kumar K.V53c06f42011-01-10 13:51:47 -0600166 struct file *filp;
167 struct p9_qid qid;
168 struct inode *inode;
Aneesh Kumar K.V6b39f6d2011-02-28 17:04:03 +0530169 struct p9_fid *fid = NULL;
170 struct v9fs_inode *v9inode;
171 struct p9_fid *dfid, *ofid, *inode_fid;
172 struct v9fs_session_info *v9ses;
Aneesh Kumar K.V53c06f42011-01-10 13:51:47 -0600173 struct posix_acl *pacl = NULL, *dacl = NULL;
174
175 v9ses = v9fs_inode2v9ses(dir);
176 if (nd && nd->flags & LOOKUP_OPEN)
177 flags = nd->intent.open.flags - 1;
178 else {
179 /*
180 * create call without LOOKUP_OPEN is due
181 * to mknod of regular files. So use mknod
182 * operation.
183 */
184 return v9fs_vfs_mknod_dotl(dir, dentry, omode, 0);
185 }
186
187 name = (char *) dentry->d_name.name;
188 P9_DPRINTK(P9_DEBUG_VFS, "v9fs_vfs_create_dotl: name:%s flags:0x%x "
189 "mode:0x%x\n", name, flags, omode);
190
191 dfid = v9fs_fid_lookup(dentry->d_parent);
192 if (IS_ERR(dfid)) {
193 err = PTR_ERR(dfid);
194 P9_DPRINTK(P9_DEBUG_VFS, "fid lookup failed %d\n", err);
195 return err;
196 }
197
198 /* clone a fid to use for creation */
199 ofid = p9_client_walk(dfid, 0, NULL, 1);
200 if (IS_ERR(ofid)) {
201 err = PTR_ERR(ofid);
202 P9_DPRINTK(P9_DEBUG_VFS, "p9_client_walk failed %d\n", err);
203 return err;
204 }
205
206 gid = v9fs_get_fsgid_for_create(dir);
207
208 mode = omode;
209 /* Update mode based on ACL value */
210 err = v9fs_acl_mode(dir, &mode, &dacl, &pacl);
211 if (err) {
212 P9_DPRINTK(P9_DEBUG_VFS,
213 "Failed to get acl values in creat %d\n", err);
214 goto error;
215 }
216 err = p9_client_create_dotl(ofid, name, flags, mode, gid, &qid);
217 if (err < 0) {
218 P9_DPRINTK(P9_DEBUG_VFS,
219 "p9_client_open_dotl failed in creat %d\n",
220 err);
221 goto error;
222 }
Aneesh Kumar K.V53c06f42011-01-10 13:51:47 -0600223
Aneesh Kumar K.Vaf7542f2011-01-10 14:22:21 -0600224 /* instantiate inode and assign the unopened fid to the dentry */
225 fid = p9_client_walk(dfid, 1, &name, 1);
226 if (IS_ERR(fid)) {
227 err = PTR_ERR(fid);
Eric Van Hensbergenc25a61f2011-01-11 09:49:03 -0600228 P9_DPRINTK(P9_DEBUG_VFS, "p9_client_walk failed %d\n", err);
Aneesh Kumar K.V53c06f42011-01-10 13:51:47 -0600229 fid = NULL;
Aneesh Kumar K.Vaf7542f2011-01-10 14:22:21 -0600230 goto error;
Aneesh Kumar K.V53c06f42011-01-10 13:51:47 -0600231 }
Aneesh Kumar K.Va78ce052011-02-28 17:04:02 +0530232 inode = v9fs_get_inode_from_fid(v9ses, fid, dir->i_sb);
Aneesh Kumar K.Vaf7542f2011-01-10 14:22:21 -0600233 if (IS_ERR(inode)) {
234 err = PTR_ERR(inode);
235 P9_DPRINTK(P9_DEBUG_VFS, "inode creation failed %d\n", err);
236 goto error;
237 }
Aneesh Kumar K.Vaf7542f2011-01-10 14:22:21 -0600238 d_instantiate(dentry, inode);
239 err = v9fs_fid_add(dentry, fid);
240 if (err < 0)
241 goto error;
242
Aneesh Kumar K.V53c06f42011-01-10 13:51:47 -0600243 /* Now set the ACL based on the default value */
244 v9fs_set_create_acl(dentry, dacl, pacl);
Aneesh Kumar K.V6b39f6d2011-02-28 17:04:03 +0530245
246 v9inode = V9FS_I(inode);
247 if (v9ses->cache && !v9inode->writeback_fid) {
Aneesh Kumar K.V3cf387d2011-02-28 17:03:57 +0530248 /*
Aneesh Kumar K.V6b39f6d2011-02-28 17:04:03 +0530249 * clone a fid and add it to writeback_fid
Aneesh Kumar K.V3cf387d2011-02-28 17:03:57 +0530250 * we do it during open time instead of
251 * page dirty time via write_begin/page_mkwrite
252 * because we want write after unlink usecase
253 * to work.
254 */
255 inode_fid = v9fs_writeback_fid(dentry);
256 if (IS_ERR(inode_fid)) {
257 err = PTR_ERR(inode_fid);
258 goto error;
259 }
Aneesh Kumar K.V6b39f6d2011-02-28 17:04:03 +0530260 v9inode->writeback_fid = (void *) inode_fid;
Aneesh Kumar K.V3cf387d2011-02-28 17:03:57 +0530261 }
Aneesh Kumar K.Vaf7542f2011-01-10 14:22:21 -0600262 /* Since we are opening a file, assign the open fid to the file */
263 filp = lookup_instantiate_filp(nd, dentry, generic_file_open);
264 if (IS_ERR(filp)) {
Aneesh Kumar K.V53c06f42011-01-10 13:51:47 -0600265 p9_client_clunk(ofid);
Aneesh Kumar K.Vaf7542f2011-01-10 14:22:21 -0600266 return PTR_ERR(filp);
267 }
268 filp->private_data = ofid;
Aneesh Kumar K.V46848de2011-02-28 17:03:55 +0530269#ifdef CONFIG_9P_FSCACHE
270 if (v9ses->cache)
271 v9fs_cache_inode_set_cookie(inode, filp);
272#endif
Aneesh Kumar K.V53c06f42011-01-10 13:51:47 -0600273 return 0;
274
275error:
276 if (ofid)
277 p9_client_clunk(ofid);
278 if (fid)
279 p9_client_clunk(fid);
280 return err;
281}
282
283/**
284 * v9fs_vfs_mkdir_dotl - VFS mkdir hook to create a directory
285 * @dir: inode that is being unlinked
286 * @dentry: dentry that is being unlinked
287 * @mode: mode for new directory
288 *
289 */
290
291static int v9fs_vfs_mkdir_dotl(struct inode *dir,
292 struct dentry *dentry, int omode)
293{
294 int err;
295 struct v9fs_session_info *v9ses;
296 struct p9_fid *fid = NULL, *dfid = NULL;
297 gid_t gid;
298 char *name;
299 mode_t mode;
300 struct inode *inode;
301 struct p9_qid qid;
302 struct dentry *dir_dentry;
303 struct posix_acl *dacl = NULL, *pacl = NULL;
304
305 P9_DPRINTK(P9_DEBUG_VFS, "name %s\n", dentry->d_name.name);
306 err = 0;
307 v9ses = v9fs_inode2v9ses(dir);
308
309 omode |= S_IFDIR;
310 if (dir->i_mode & S_ISGID)
311 omode |= S_ISGID;
312
313 dir_dentry = v9fs_dentry_from_dir_inode(dir);
314 dfid = v9fs_fid_lookup(dir_dentry);
315 if (IS_ERR(dfid)) {
316 err = PTR_ERR(dfid);
317 P9_DPRINTK(P9_DEBUG_VFS, "fid lookup failed %d\n", err);
318 dfid = NULL;
319 goto error;
320 }
321
322 gid = v9fs_get_fsgid_for_create(dir);
323 mode = omode;
324 /* Update mode based on ACL value */
325 err = v9fs_acl_mode(dir, &mode, &dacl, &pacl);
326 if (err) {
327 P9_DPRINTK(P9_DEBUG_VFS,
328 "Failed to get acl values in mkdir %d\n", err);
329 goto error;
330 }
331 name = (char *) dentry->d_name.name;
332 err = p9_client_mkdir_dotl(dfid, name, mode, gid, &qid);
333 if (err < 0)
334 goto error;
335
336 /* instantiate inode and assign the unopened fid to the dentry */
337 if (v9ses->cache == CACHE_LOOSE || v9ses->cache == CACHE_FSCACHE) {
338 fid = p9_client_walk(dfid, 1, &name, 1);
339 if (IS_ERR(fid)) {
340 err = PTR_ERR(fid);
341 P9_DPRINTK(P9_DEBUG_VFS, "p9_client_walk failed %d\n",
342 err);
343 fid = NULL;
344 goto error;
345 }
346
Aneesh Kumar K.Va78ce052011-02-28 17:04:02 +0530347 inode = v9fs_get_inode_from_fid(v9ses, fid, dir->i_sb);
Aneesh Kumar K.V53c06f42011-01-10 13:51:47 -0600348 if (IS_ERR(inode)) {
349 err = PTR_ERR(inode);
350 P9_DPRINTK(P9_DEBUG_VFS, "inode creation failed %d\n",
351 err);
352 goto error;
353 }
Aneesh Kumar K.V53c06f42011-01-10 13:51:47 -0600354 d_instantiate(dentry, inode);
355 err = v9fs_fid_add(dentry, fid);
356 if (err < 0)
357 goto error;
358 fid = NULL;
359 } else {
360 /*
361 * Not in cached mode. No need to populate
362 * inode with stat. We need to get an inode
363 * so that we can set the acl with dentry
364 */
365 inode = v9fs_get_inode(dir->i_sb, mode);
366 if (IS_ERR(inode)) {
367 err = PTR_ERR(inode);
368 goto error;
369 }
Aneesh Kumar K.V53c06f42011-01-10 13:51:47 -0600370 d_instantiate(dentry, inode);
371 }
372 /* Now set the ACL based on the default value */
373 v9fs_set_create_acl(dentry, dacl, pacl);
Aneesh Kumar K.Vb271ec42011-02-28 17:04:05 +0530374 inc_nlink(dir);
Aneesh Kumar K.V53c06f42011-01-10 13:51:47 -0600375error:
376 if (fid)
377 p9_client_clunk(fid);
378 return err;
379}
380
381static int
382v9fs_vfs_getattr_dotl(struct vfsmount *mnt, struct dentry *dentry,
383 struct kstat *stat)
384{
385 int err;
386 struct v9fs_session_info *v9ses;
387 struct p9_fid *fid;
388 struct p9_stat_dotl *st;
389
390 P9_DPRINTK(P9_DEBUG_VFS, "dentry: %p\n", dentry);
391 err = -EPERM;
392 v9ses = v9fs_inode2v9ses(dentry->d_inode);
Aneesh Kumar K.Va1211902011-02-28 17:04:01 +0530393 if (v9ses->cache == CACHE_LOOSE || v9ses->cache == CACHE_FSCACHE) {
394 generic_fillattr(dentry->d_inode, stat);
395 return 0;
396 }
Aneesh Kumar K.V53c06f42011-01-10 13:51:47 -0600397 fid = v9fs_fid_lookup(dentry);
398 if (IS_ERR(fid))
399 return PTR_ERR(fid);
400
401 /* Ask for all the fields in stat structure. Server will return
402 * whatever it supports
403 */
404
405 st = p9_client_getattr_dotl(fid, P9_STATS_ALL);
406 if (IS_ERR(st))
407 return PTR_ERR(st);
408
409 v9fs_stat2inode_dotl(st, dentry->d_inode);
410 generic_fillattr(dentry->d_inode, stat);
411 /* Change block size to what the server returned */
412 stat->blksize = st->st_blksize;
413
414 kfree(st);
415 return 0;
416}
417
418/**
419 * v9fs_vfs_setattr_dotl - set file metadata
420 * @dentry: file whose metadata to set
421 * @iattr: metadata assignment structure
422 *
423 */
424
425int v9fs_vfs_setattr_dotl(struct dentry *dentry, struct iattr *iattr)
426{
427 int retval;
428 struct v9fs_session_info *v9ses;
429 struct p9_fid *fid;
430 struct p9_iattr_dotl p9attr;
431
432 P9_DPRINTK(P9_DEBUG_VFS, "\n");
433
434 retval = inode_change_ok(dentry->d_inode, iattr);
435 if (retval)
436 return retval;
437
438 p9attr.valid = iattr->ia_valid;
439 p9attr.mode = iattr->ia_mode;
440 p9attr.uid = iattr->ia_uid;
441 p9attr.gid = iattr->ia_gid;
442 p9attr.size = iattr->ia_size;
443 p9attr.atime_sec = iattr->ia_atime.tv_sec;
444 p9attr.atime_nsec = iattr->ia_atime.tv_nsec;
445 p9attr.mtime_sec = iattr->ia_mtime.tv_sec;
446 p9attr.mtime_nsec = iattr->ia_mtime.tv_nsec;
447
448 retval = -EPERM;
449 v9ses = v9fs_inode2v9ses(dentry->d_inode);
450 fid = v9fs_fid_lookup(dentry);
451 if (IS_ERR(fid))
452 return PTR_ERR(fid);
453
454 retval = p9_client_setattr(fid, &p9attr);
455 if (retval < 0)
456 return retval;
457
458 if ((iattr->ia_valid & ATTR_SIZE) &&
459 iattr->ia_size != i_size_read(dentry->d_inode)) {
460 retval = vmtruncate(dentry->d_inode, iattr->ia_size);
461 if (retval)
462 return retval;
463 }
464
465 setattr_copy(dentry->d_inode, iattr);
466 mark_inode_dirty(dentry->d_inode);
467 if (iattr->ia_valid & ATTR_MODE) {
468 /* We also want to update ACL when we update mode bits */
469 retval = v9fs_acl_chmod(dentry);
470 if (retval < 0)
471 return retval;
472 }
473 return 0;
474}
475
476/**
477 * v9fs_stat2inode_dotl - populate an inode structure with stat info
478 * @stat: stat structure
479 * @inode: inode to populate
480 * @sb: superblock of filesystem
481 *
482 */
483
484void
485v9fs_stat2inode_dotl(struct p9_stat_dotl *stat, struct inode *inode)
486{
Aneesh Kumar K.Vb3cbea02011-02-28 17:04:06 +0530487 struct v9fs_inode *v9inode = V9FS_I(inode);
Aneesh Kumar K.V53c06f42011-01-10 13:51:47 -0600488
489 if ((stat->st_result_mask & P9_STATS_BASIC) == P9_STATS_BASIC) {
490 inode->i_atime.tv_sec = stat->st_atime_sec;
491 inode->i_atime.tv_nsec = stat->st_atime_nsec;
492 inode->i_mtime.tv_sec = stat->st_mtime_sec;
493 inode->i_mtime.tv_nsec = stat->st_mtime_nsec;
494 inode->i_ctime.tv_sec = stat->st_ctime_sec;
495 inode->i_ctime.tv_nsec = stat->st_ctime_nsec;
496 inode->i_uid = stat->st_uid;
497 inode->i_gid = stat->st_gid;
498 inode->i_nlink = stat->st_nlink;
499 inode->i_mode = stat->st_mode;
500 inode->i_rdev = new_decode_dev(stat->st_rdev);
501
502 if ((S_ISBLK(inode->i_mode)) || (S_ISCHR(inode->i_mode)))
503 init_special_inode(inode, inode->i_mode, inode->i_rdev);
504
505 i_size_write(inode, stat->st_size);
506 inode->i_blocks = stat->st_blocks;
507 } else {
508 if (stat->st_result_mask & P9_STATS_ATIME) {
509 inode->i_atime.tv_sec = stat->st_atime_sec;
510 inode->i_atime.tv_nsec = stat->st_atime_nsec;
511 }
512 if (stat->st_result_mask & P9_STATS_MTIME) {
513 inode->i_mtime.tv_sec = stat->st_mtime_sec;
514 inode->i_mtime.tv_nsec = stat->st_mtime_nsec;
515 }
516 if (stat->st_result_mask & P9_STATS_CTIME) {
517 inode->i_ctime.tv_sec = stat->st_ctime_sec;
518 inode->i_ctime.tv_nsec = stat->st_ctime_nsec;
519 }
520 if (stat->st_result_mask & P9_STATS_UID)
521 inode->i_uid = stat->st_uid;
522 if (stat->st_result_mask & P9_STATS_GID)
523 inode->i_gid = stat->st_gid;
524 if (stat->st_result_mask & P9_STATS_NLINK)
525 inode->i_nlink = stat->st_nlink;
526 if (stat->st_result_mask & P9_STATS_MODE) {
527 inode->i_mode = stat->st_mode;
528 if ((S_ISBLK(inode->i_mode)) ||
529 (S_ISCHR(inode->i_mode)))
530 init_special_inode(inode, inode->i_mode,
531 inode->i_rdev);
532 }
533 if (stat->st_result_mask & P9_STATS_RDEV)
534 inode->i_rdev = new_decode_dev(stat->st_rdev);
535 if (stat->st_result_mask & P9_STATS_SIZE)
536 i_size_write(inode, stat->st_size);
537 if (stat->st_result_mask & P9_STATS_BLOCKS)
538 inode->i_blocks = stat->st_blocks;
539 }
540 if (stat->st_result_mask & P9_STATS_GEN)
541 inode->i_generation = stat->st_gen;
542
543 /* Currently we don't support P9_STATS_BTIME and P9_STATS_DATA_VERSION
544 * because the inode structure does not have fields for them.
545 */
Aneesh Kumar K.Vb3cbea02011-02-28 17:04:06 +0530546 v9inode->cache_validity &= ~V9FS_INO_INVALID_ATTR;
Aneesh Kumar K.V53c06f42011-01-10 13:51:47 -0600547}
548
549static int
550v9fs_vfs_symlink_dotl(struct inode *dir, struct dentry *dentry,
551 const char *symname)
552{
553 struct v9fs_session_info *v9ses;
554 struct p9_fid *dfid;
555 struct p9_fid *fid = NULL;
556 struct inode *inode;
557 struct p9_qid qid;
558 char *name;
559 int err;
560 gid_t gid;
561
562 name = (char *) dentry->d_name.name;
563 P9_DPRINTK(P9_DEBUG_VFS, "v9fs_vfs_symlink_dotl : %lu,%s,%s\n",
564 dir->i_ino, name, symname);
565 v9ses = v9fs_inode2v9ses(dir);
566
567 dfid = v9fs_fid_lookup(dentry->d_parent);
568 if (IS_ERR(dfid)) {
569 err = PTR_ERR(dfid);
570 P9_DPRINTK(P9_DEBUG_VFS, "fid lookup failed %d\n", err);
571 return err;
572 }
573
574 gid = v9fs_get_fsgid_for_create(dir);
575
576 /* Server doesn't alter fid on TSYMLINK. Hence no need to clone it. */
577 err = p9_client_symlink(dfid, name, (char *)symname, gid, &qid);
578
579 if (err < 0) {
580 P9_DPRINTK(P9_DEBUG_VFS, "p9_client_symlink failed %d\n", err);
581 goto error;
582 }
583
584 if (v9ses->cache) {
585 /* Now walk from the parent so we can get an unopened fid. */
586 fid = p9_client_walk(dfid, 1, &name, 1);
587 if (IS_ERR(fid)) {
588 err = PTR_ERR(fid);
589 P9_DPRINTK(P9_DEBUG_VFS, "p9_client_walk failed %d\n",
590 err);
591 fid = NULL;
592 goto error;
593 }
594
595 /* instantiate inode and assign the unopened fid to dentry */
Aneesh Kumar K.Va78ce052011-02-28 17:04:02 +0530596 inode = v9fs_get_inode_from_fid(v9ses, fid, dir->i_sb);
Aneesh Kumar K.V53c06f42011-01-10 13:51:47 -0600597 if (IS_ERR(inode)) {
598 err = PTR_ERR(inode);
599 P9_DPRINTK(P9_DEBUG_VFS, "inode creation failed %d\n",
600 err);
601 goto error;
602 }
Aneesh Kumar K.V53c06f42011-01-10 13:51:47 -0600603 d_instantiate(dentry, inode);
604 err = v9fs_fid_add(dentry, fid);
605 if (err < 0)
606 goto error;
607 fid = NULL;
608 } else {
609 /* Not in cached mode. No need to populate inode with stat */
610 inode = v9fs_get_inode(dir->i_sb, S_IFLNK);
611 if (IS_ERR(inode)) {
612 err = PTR_ERR(inode);
613 goto error;
614 }
Aneesh Kumar K.V53c06f42011-01-10 13:51:47 -0600615 d_instantiate(dentry, inode);
616 }
617
618error:
619 if (fid)
620 p9_client_clunk(fid);
621
622 return err;
623}
624
625/**
626 * v9fs_vfs_link_dotl - create a hardlink for dotl
627 * @old_dentry: dentry for file to link to
628 * @dir: inode destination for new link
629 * @dentry: dentry for link
630 *
631 */
632
633static int
634v9fs_vfs_link_dotl(struct dentry *old_dentry, struct inode *dir,
635 struct dentry *dentry)
636{
637 int err;
638 struct p9_fid *dfid, *oldfid;
639 char *name;
640 struct v9fs_session_info *v9ses;
641 struct dentry *dir_dentry;
642
643 P9_DPRINTK(P9_DEBUG_VFS, "dir ino: %lu, old_name: %s, new_name: %s\n",
644 dir->i_ino, old_dentry->d_name.name,
645 dentry->d_name.name);
646
647 v9ses = v9fs_inode2v9ses(dir);
648 dir_dentry = v9fs_dentry_from_dir_inode(dir);
649 dfid = v9fs_fid_lookup(dir_dentry);
650 if (IS_ERR(dfid))
651 return PTR_ERR(dfid);
652
653 oldfid = v9fs_fid_lookup(old_dentry);
654 if (IS_ERR(oldfid))
655 return PTR_ERR(oldfid);
656
657 name = (char *) dentry->d_name.name;
658
659 err = p9_client_link(dfid, oldfid, (char *)dentry->d_name.name);
660
661 if (err < 0) {
662 P9_DPRINTK(P9_DEBUG_VFS, "p9_client_link failed %d\n", err);
663 return err;
664 }
665
666 if (v9ses->cache == CACHE_LOOSE || v9ses->cache == CACHE_FSCACHE) {
667 /* Get the latest stat info from server. */
668 struct p9_fid *fid;
669 struct p9_stat_dotl *st;
670
671 fid = v9fs_fid_lookup(old_dentry);
672 if (IS_ERR(fid))
673 return PTR_ERR(fid);
674
675 st = p9_client_getattr_dotl(fid, P9_STATS_BASIC);
676 if (IS_ERR(st))
677 return PTR_ERR(st);
678
679 v9fs_stat2inode_dotl(st, old_dentry->d_inode);
680
681 kfree(st);
Aneesh Kumar K.V53c06f42011-01-10 13:51:47 -0600682 }
Aneesh Kumar K.V20656a42011-02-28 17:03:55 +0530683 ihold(old_dentry->d_inode);
Aneesh Kumar K.V53c06f42011-01-10 13:51:47 -0600684 d_instantiate(dentry, old_dentry->d_inode);
685
686 return err;
687}
688
689/**
690 * v9fs_vfs_mknod_dotl - create a special file
691 * @dir: inode destination for new link
692 * @dentry: dentry for file
693 * @mode: mode for creation
694 * @rdev: device associated with special file
695 *
696 */
697static int
698v9fs_vfs_mknod_dotl(struct inode *dir, struct dentry *dentry, int omode,
699 dev_t rdev)
700{
701 int err;
702 char *name;
703 mode_t mode;
704 struct v9fs_session_info *v9ses;
705 struct p9_fid *fid = NULL, *dfid = NULL;
706 struct inode *inode;
707 gid_t gid;
708 struct p9_qid qid;
709 struct dentry *dir_dentry;
710 struct posix_acl *dacl = NULL, *pacl = NULL;
711
712 P9_DPRINTK(P9_DEBUG_VFS,
713 " %lu,%s mode: %x MAJOR: %u MINOR: %u\n", dir->i_ino,
714 dentry->d_name.name, omode, MAJOR(rdev), MINOR(rdev));
715
716 if (!new_valid_dev(rdev))
717 return -EINVAL;
718
719 v9ses = v9fs_inode2v9ses(dir);
720 dir_dentry = v9fs_dentry_from_dir_inode(dir);
721 dfid = v9fs_fid_lookup(dir_dentry);
722 if (IS_ERR(dfid)) {
723 err = PTR_ERR(dfid);
724 P9_DPRINTK(P9_DEBUG_VFS, "fid lookup failed %d\n", err);
725 dfid = NULL;
726 goto error;
727 }
728
729 gid = v9fs_get_fsgid_for_create(dir);
730 mode = omode;
731 /* Update mode based on ACL value */
732 err = v9fs_acl_mode(dir, &mode, &dacl, &pacl);
733 if (err) {
734 P9_DPRINTK(P9_DEBUG_VFS,
735 "Failed to get acl values in mknod %d\n", err);
736 goto error;
737 }
738 name = (char *) dentry->d_name.name;
739
740 err = p9_client_mknod_dotl(dfid, name, mode, rdev, gid, &qid);
741 if (err < 0)
742 goto error;
743
744 /* instantiate inode and assign the unopened fid to the dentry */
745 if (v9ses->cache == CACHE_LOOSE || v9ses->cache == CACHE_FSCACHE) {
746 fid = p9_client_walk(dfid, 1, &name, 1);
747 if (IS_ERR(fid)) {
748 err = PTR_ERR(fid);
749 P9_DPRINTK(P9_DEBUG_VFS, "p9_client_walk failed %d\n",
750 err);
751 fid = NULL;
752 goto error;
753 }
754
Aneesh Kumar K.Va78ce052011-02-28 17:04:02 +0530755 inode = v9fs_get_inode_from_fid(v9ses, fid, dir->i_sb);
Aneesh Kumar K.V53c06f42011-01-10 13:51:47 -0600756 if (IS_ERR(inode)) {
757 err = PTR_ERR(inode);
758 P9_DPRINTK(P9_DEBUG_VFS, "inode creation failed %d\n",
759 err);
760 goto error;
761 }
Aneesh Kumar K.V53c06f42011-01-10 13:51:47 -0600762 d_instantiate(dentry, inode);
763 err = v9fs_fid_add(dentry, fid);
764 if (err < 0)
765 goto error;
766 fid = NULL;
767 } else {
768 /*
769 * Not in cached mode. No need to populate inode with stat.
770 * socket syscall returns a fd, so we need instantiate
771 */
772 inode = v9fs_get_inode(dir->i_sb, mode);
773 if (IS_ERR(inode)) {
774 err = PTR_ERR(inode);
775 goto error;
776 }
Aneesh Kumar K.V53c06f42011-01-10 13:51:47 -0600777 d_instantiate(dentry, inode);
778 }
779 /* Now set the ACL based on the default value */
780 v9fs_set_create_acl(dentry, dacl, pacl);
781error:
782 if (fid)
783 p9_client_clunk(fid);
784 return err;
785}
786
Aneesh Kumar K.V53c06f42011-01-10 13:51:47 -0600787/**
788 * v9fs_vfs_follow_link_dotl - follow a symlink path
789 * @dentry: dentry for symlink
790 * @nd: nameidata
791 *
792 */
793
794static void *
795v9fs_vfs_follow_link_dotl(struct dentry *dentry, struct nameidata *nd)
796{
M. Mohan Kumar31b6cea2011-01-08 07:28:46 +0530797 int retval;
798 struct p9_fid *fid;
Aneesh Kumar K.V53c06f42011-01-10 13:51:47 -0600799 char *link = __getname();
M. Mohan Kumar31b6cea2011-01-08 07:28:46 +0530800 char *target;
Aneesh Kumar K.V53c06f42011-01-10 13:51:47 -0600801
M. Mohan Kumar31b6cea2011-01-08 07:28:46 +0530802 P9_DPRINTK(P9_DEBUG_VFS, "%s\n", dentry->d_name.name);
Aneesh Kumar K.V53c06f42011-01-10 13:51:47 -0600803
M. Mohan Kumar31b6cea2011-01-08 07:28:46 +0530804 if (!link) {
Aneesh Kumar K.V53c06f42011-01-10 13:51:47 -0600805 link = ERR_PTR(-ENOMEM);
M. Mohan Kumar31b6cea2011-01-08 07:28:46 +0530806 goto ndset;
Aneesh Kumar K.V53c06f42011-01-10 13:51:47 -0600807 }
M. Mohan Kumar31b6cea2011-01-08 07:28:46 +0530808 fid = v9fs_fid_lookup(dentry);
809 if (IS_ERR(fid)) {
810 __putname(link);
811 link = ERR_PTR(PTR_ERR(fid));
812 goto ndset;
813 }
814 retval = p9_client_readlink(fid, &target);
815 if (!retval) {
816 strcpy(link, target);
817 kfree(target);
818 goto ndset;
819 }
820 __putname(link);
821 link = ERR_PTR(retval);
822ndset:
Aneesh Kumar K.V53c06f42011-01-10 13:51:47 -0600823 nd_set_link(nd, link);
Aneesh Kumar K.V53c06f42011-01-10 13:51:47 -0600824 return NULL;
825}
826
Aneesh Kumar K.Vb3cbea02011-02-28 17:04:06 +0530827int v9fs_refresh_inode_dotl(struct p9_fid *fid, struct inode *inode)
828{
829 loff_t i_size;
830 struct p9_stat_dotl *st;
831 struct v9fs_session_info *v9ses;
832
833 v9ses = v9fs_inode2v9ses(inode);
834 st = p9_client_getattr_dotl(fid, P9_STATS_ALL);
835 if (IS_ERR(st))
836 return PTR_ERR(st);
837
838 spin_lock(&inode->i_lock);
839 /*
840 * We don't want to refresh inode->i_size,
841 * because we may have cached data
842 */
843 i_size = inode->i_size;
844 v9fs_stat2inode_dotl(st, inode);
845 if (v9ses->cache)
846 inode->i_size = i_size;
847 spin_unlock(&inode->i_lock);
848 kfree(st);
849 return 0;
850}
851
Aneesh Kumar K.V53c06f42011-01-10 13:51:47 -0600852const struct inode_operations v9fs_dir_inode_operations_dotl = {
853 .create = v9fs_vfs_create_dotl,
854 .lookup = v9fs_vfs_lookup,
855 .link = v9fs_vfs_link_dotl,
856 .symlink = v9fs_vfs_symlink_dotl,
857 .unlink = v9fs_vfs_unlink,
858 .mkdir = v9fs_vfs_mkdir_dotl,
859 .rmdir = v9fs_vfs_rmdir,
860 .mknod = v9fs_vfs_mknod_dotl,
861 .rename = v9fs_vfs_rename,
862 .getattr = v9fs_vfs_getattr_dotl,
863 .setattr = v9fs_vfs_setattr_dotl,
864 .setxattr = generic_setxattr,
865 .getxattr = generic_getxattr,
866 .removexattr = generic_removexattr,
867 .listxattr = v9fs_listxattr,
868 .check_acl = v9fs_check_acl,
869};
870
871const struct inode_operations v9fs_file_inode_operations_dotl = {
872 .getattr = v9fs_vfs_getattr_dotl,
873 .setattr = v9fs_vfs_setattr_dotl,
874 .setxattr = generic_setxattr,
875 .getxattr = generic_getxattr,
876 .removexattr = generic_removexattr,
877 .listxattr = v9fs_listxattr,
878 .check_acl = v9fs_check_acl,
879};
880
881const struct inode_operations v9fs_symlink_inode_operations_dotl = {
M. Mohan Kumar31b6cea2011-01-08 07:28:46 +0530882 .readlink = generic_readlink,
Aneesh Kumar K.V53c06f42011-01-10 13:51:47 -0600883 .follow_link = v9fs_vfs_follow_link_dotl,
884 .put_link = v9fs_vfs_put_link,
885 .getattr = v9fs_vfs_getattr_dotl,
886 .setattr = v9fs_vfs_setattr_dotl,
887 .setxattr = generic_setxattr,
888 .getxattr = generic_getxattr,
889 .removexattr = generic_removexattr,
890 .listxattr = v9fs_listxattr,
891};