blob: dc9e7de38202db6262b75991a46673f8cafb7985 [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.Vfd2421f2011-07-11 16:40:59 +000089static int v9fs_test_inode_dotl(struct inode *inode, void *data)
90{
91 struct v9fs_inode *v9inode = V9FS_I(inode);
92 struct p9_stat_dotl *st = (struct p9_stat_dotl *)data;
93
94 /* don't match inode of different type */
95 if ((inode->i_mode & S_IFMT) != (st->st_mode & S_IFMT))
96 return 0;
97
98 if (inode->i_generation != st->st_gen)
99 return 0;
100
101 /* compare qid details */
102 if (memcmp(&v9inode->qid.version,
103 &st->qid.version, sizeof(v9inode->qid.version)))
104 return 0;
105
106 if (v9inode->qid.type != st->qid.type)
107 return 0;
108 return 1;
109}
110
111static int v9fs_set_inode_dotl(struct inode *inode, void *data)
112{
113 struct v9fs_inode *v9inode = V9FS_I(inode);
114 struct p9_stat_dotl *st = (struct p9_stat_dotl *)data;
115
116 memcpy(&v9inode->qid, &st->qid, sizeof(st->qid));
117 inode->i_generation = st->st_gen;
118 return 0;
119}
120
Aneesh Kumar K.V5ffc0cb2011-02-28 17:04:01 +0530121static 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)
125{
126 int retval;
127 unsigned long i_ino;
128 struct inode *inode;
129 struct v9fs_session_info *v9ses = sb->s_fs_info;
130
131 i_ino = v9fs_qid2ino(qid);
Aneesh Kumar K.Vfd2421f2011-07-11 16:40:59 +0000132 inode = iget5_locked(sb, i_ino, v9fs_test_inode_dotl,
133 v9fs_set_inode_dotl, st);
Aneesh Kumar K.V5ffc0cb2011-02-28 17:04:01 +0530134 if (!inode)
135 return ERR_PTR(-ENOMEM);
136 if (!(inode->i_state & I_NEW))
137 return inode;
138 /*
139 * initialize the inode with the stat info
140 * FIXME!! we may need support for stale inodes
141 * later.
142 */
Aneesh Kumar K.Vfd2421f2011-07-11 16:40:59 +0000143 inode->i_ino = i_ino;
Aneesh Kumar K.V5ffc0cb2011-02-28 17:04:01 +0530144 retval = v9fs_init_inode(v9ses, inode, st->st_mode);
145 if (retval)
146 goto error;
147
148 v9fs_stat2inode_dotl(st, inode);
149#ifdef CONFIG_9P_FSCACHE
Aneesh Kumar K.V5ffc0cb2011-02-28 17:04:01 +0530150 v9fs_cache_inode_get_cookie(inode);
151#endif
152 retval = v9fs_get_acl(inode, fid);
153 if (retval)
154 goto error;
155
156 unlock_new_inode(inode);
157 return inode;
158error:
159 unlock_new_inode(inode);
160 iput(inode);
161 return ERR_PTR(retval);
162
163}
164
Aneesh Kumar K.V53c06f42011-01-10 13:51:47 -0600165struct inode *
Aneesh Kumar K.Va78ce052011-02-28 17:04:02 +0530166v9fs_inode_from_fid_dotl(struct v9fs_session_info *v9ses, struct p9_fid *fid,
167 struct super_block *sb)
Aneesh Kumar K.V53c06f42011-01-10 13:51:47 -0600168{
Aneesh Kumar K.V53c06f42011-01-10 13:51:47 -0600169 struct p9_stat_dotl *st;
Aneesh Kumar K.V5ffc0cb2011-02-28 17:04:01 +0530170 struct inode *inode = NULL;
Aneesh Kumar K.V53c06f42011-01-10 13:51:47 -0600171
Aneesh Kumar K.Vfd2421f2011-07-11 16:40:59 +0000172 st = p9_client_getattr_dotl(fid, P9_STATS_BASIC | P9_STATS_GEN);
Aneesh Kumar K.V53c06f42011-01-10 13:51:47 -0600173 if (IS_ERR(st))
174 return ERR_CAST(st);
175
Aneesh Kumar K.V5ffc0cb2011-02-28 17:04:01 +0530176 inode = v9fs_qid_iget_dotl(sb, &st->qid, fid, st);
Aneesh Kumar K.V53c06f42011-01-10 13:51:47 -0600177 kfree(st);
Aneesh Kumar K.V5ffc0cb2011-02-28 17:04:01 +0530178 return inode;
Aneesh Kumar K.V53c06f42011-01-10 13:51:47 -0600179}
180
181/**
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
187 *
188 */
189
190static int
191v9fs_vfs_create_dotl(struct inode *dir, struct dentry *dentry, int omode,
192 struct nameidata *nd)
193{
194 int err = 0;
Aneesh Kumar K.V53c06f42011-01-10 13:51:47 -0600195 gid_t gid;
196 int flags;
197 mode_t mode;
Aneesh Kumar K.V6b39f6d2011-02-28 17:04:03 +0530198 char *name = NULL;
Aneesh Kumar K.V53c06f42011-01-10 13:51:47 -0600199 struct file *filp;
200 struct p9_qid qid;
201 struct inode *inode;
Aneesh Kumar K.V6b39f6d2011-02-28 17:04:03 +0530202 struct p9_fid *fid = NULL;
203 struct v9fs_inode *v9inode;
204 struct p9_fid *dfid, *ofid, *inode_fid;
205 struct v9fs_session_info *v9ses;
Aneesh Kumar K.V53c06f42011-01-10 13:51:47 -0600206 struct posix_acl *pacl = NULL, *dacl = NULL;
207
208 v9ses = v9fs_inode2v9ses(dir);
Al Virodd7dd552011-06-25 21:17:17 -0400209 if (nd)
Al Viro8a5e9292011-06-25 19:15:54 -0400210 flags = nd->intent.open.flags;
Aneesh Kumar K.V53c06f42011-01-10 13:51:47 -0600211 else {
212 /*
213 * create call without LOOKUP_OPEN is due
214 * to mknod of regular files. So use mknod
215 * operation.
216 */
217 return v9fs_vfs_mknod_dotl(dir, dentry, omode, 0);
218 }
219
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);
223
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;
229 }
230
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;
237 }
238
239 gid = v9fs_get_fsgid_for_create(dir);
240
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;
248 }
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;
255 }
Aneesh Kumar K.Vd28c61f2011-02-28 17:04:08 +0530256 v9fs_invalidate_inode_attr(dir);
Aneesh Kumar K.V53c06f42011-01-10 13:51:47 -0600257
Aneesh Kumar K.Vaf7542f2011-01-10 14:22:21 -0600258 /* 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);
Eric Van Hensbergenc25a61f2011-01-11 09:49:03 -0600262 P9_DPRINTK(P9_DEBUG_VFS, "p9_client_walk failed %d\n", err);
Aneesh Kumar K.V53c06f42011-01-10 13:51:47 -0600263 fid = NULL;
Aneesh Kumar K.Vaf7542f2011-01-10 14:22:21 -0600264 goto error;
Aneesh Kumar K.V53c06f42011-01-10 13:51:47 -0600265 }
Aneesh Kumar K.Va78ce052011-02-28 17:04:02 +0530266 inode = v9fs_get_inode_from_fid(v9ses, fid, dir->i_sb);
Aneesh Kumar K.Vaf7542f2011-01-10 14:22:21 -0600267 if (IS_ERR(inode)) {
268 err = PTR_ERR(inode);
269 P9_DPRINTK(P9_DEBUG_VFS, "inode creation failed %d\n", err);
270 goto error;
271 }
Aneesh Kumar K.Vaf7542f2011-01-10 14:22:21 -0600272 d_instantiate(dentry, inode);
273 err = v9fs_fid_add(dentry, fid);
274 if (err < 0)
275 goto error;
276
Aneesh Kumar K.V53c06f42011-01-10 13:51:47 -0600277 /* Now set the ACL based on the default value */
278 v9fs_set_create_acl(dentry, dacl, pacl);
Aneesh Kumar K.V6b39f6d2011-02-28 17:04:03 +0530279
280 v9inode = V9FS_I(inode);
Aneesh Kumar K.V5a7e0a82011-03-08 16:39:46 +0530281 mutex_lock(&v9inode->v_mutex);
Aneesh Kumar K.V7add6972011-03-08 16:39:49 +0530282 if (v9ses->cache && !v9inode->writeback_fid &&
283 ((flags & O_ACCMODE) != O_RDONLY)) {
Aneesh Kumar K.V3cf387d2011-02-28 17:03:57 +0530284 /*
Aneesh Kumar K.V6b39f6d2011-02-28 17:04:03 +0530285 * clone a fid and add it to writeback_fid
Aneesh Kumar K.V3cf387d2011-02-28 17:03:57 +0530286 * 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.
290 */
291 inode_fid = v9fs_writeback_fid(dentry);
292 if (IS_ERR(inode_fid)) {
293 err = PTR_ERR(inode_fid);
Aneesh Kumar K.V5a7e0a82011-03-08 16:39:46 +0530294 mutex_unlock(&v9inode->v_mutex);
Aneesh Kumar K.V398c4f02011-05-20 18:55:51 +0000295 goto err_clunk_old_fid;
Aneesh Kumar K.V3cf387d2011-02-28 17:03:57 +0530296 }
Aneesh Kumar K.V6b39f6d2011-02-28 17:04:03 +0530297 v9inode->writeback_fid = (void *) inode_fid;
Aneesh Kumar K.V3cf387d2011-02-28 17:03:57 +0530298 }
Aneesh Kumar K.V5a7e0a82011-03-08 16:39:46 +0530299 mutex_unlock(&v9inode->v_mutex);
Aneesh Kumar K.Vaf7542f2011-01-10 14:22:21 -0600300 /* 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)) {
Aneesh Kumar K.V398c4f02011-05-20 18:55:51 +0000303 err = PTR_ERR(filp);
304 goto err_clunk_old_fid;
Aneesh Kumar K.Vaf7542f2011-01-10 14:22:21 -0600305 }
306 filp->private_data = ofid;
Aneesh Kumar K.V46848de2011-02-28 17:03:55 +0530307#ifdef CONFIG_9P_FSCACHE
308 if (v9ses->cache)
309 v9fs_cache_inode_set_cookie(inode, filp);
310#endif
Aneesh Kumar K.V53c06f42011-01-10 13:51:47 -0600311 return 0;
312
313error:
Aneesh Kumar K.V53c06f42011-01-10 13:51:47 -0600314 if (fid)
315 p9_client_clunk(fid);
Aneesh Kumar K.V398c4f02011-05-20 18:55:51 +0000316err_clunk_old_fid:
317 if (ofid)
318 p9_client_clunk(ofid);
Aneesh Kumar K.V53c06f42011-01-10 13:51:47 -0600319 return err;
320}
321
322/**
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
327 *
328 */
329
330static int v9fs_vfs_mkdir_dotl(struct inode *dir,
331 struct dentry *dentry, int omode)
332{
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;
343
344 P9_DPRINTK(P9_DEBUG_VFS, "name %s\n", dentry->d_name.name);
345 err = 0;
346 v9ses = v9fs_inode2v9ses(dir);
347
348 omode |= S_IFDIR;
349 if (dir->i_mode & S_ISGID)
350 omode |= S_ISGID;
351
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;
359 }
360
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;
369 }
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;
374
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;
384 }
385
Aneesh Kumar K.Va78ce052011-02-28 17:04:02 +0530386 inode = v9fs_get_inode_from_fid(v9ses, fid, dir->i_sb);
Aneesh Kumar K.V53c06f42011-01-10 13:51:47 -0600387 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;
392 }
Aneesh Kumar K.V53c06f42011-01-10 13:51:47 -0600393 d_instantiate(dentry, inode);
394 err = v9fs_fid_add(dentry, fid);
395 if (err < 0)
396 goto error;
397 fid = NULL;
398 } else {
399 /*
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
403 */
404 inode = v9fs_get_inode(dir->i_sb, mode);
405 if (IS_ERR(inode)) {
406 err = PTR_ERR(inode);
407 goto error;
408 }
Aneesh Kumar K.V53c06f42011-01-10 13:51:47 -0600409 d_instantiate(dentry, inode);
410 }
411 /* Now set the ACL based on the default value */
412 v9fs_set_create_acl(dentry, dacl, pacl);
Aneesh Kumar K.Vb271ec42011-02-28 17:04:05 +0530413 inc_nlink(dir);
Aneesh Kumar K.Vd28c61f2011-02-28 17:04:08 +0530414 v9fs_invalidate_inode_attr(dir);
Aneesh Kumar K.V53c06f42011-01-10 13:51:47 -0600415error:
416 if (fid)
417 p9_client_clunk(fid);
418 return err;
419}
420
421static int
422v9fs_vfs_getattr_dotl(struct vfsmount *mnt, struct dentry *dentry,
423 struct kstat *stat)
424{
425 int err;
426 struct v9fs_session_info *v9ses;
427 struct p9_fid *fid;
428 struct p9_stat_dotl *st;
429
430 P9_DPRINTK(P9_DEBUG_VFS, "dentry: %p\n", dentry);
431 err = -EPERM;
Aneesh Kumar K.V42869c82011-03-08 16:39:50 +0530432 v9ses = v9fs_dentry2v9ses(dentry);
Aneesh Kumar K.Va1211902011-02-28 17:04:01 +0530433 if (v9ses->cache == CACHE_LOOSE || v9ses->cache == CACHE_FSCACHE) {
434 generic_fillattr(dentry->d_inode, stat);
435 return 0;
436 }
Aneesh Kumar K.V53c06f42011-01-10 13:51:47 -0600437 fid = v9fs_fid_lookup(dentry);
438 if (IS_ERR(fid))
439 return PTR_ERR(fid);
440
441 /* Ask for all the fields in stat structure. Server will return
442 * whatever it supports
443 */
444
445 st = p9_client_getattr_dotl(fid, P9_STATS_ALL);
446 if (IS_ERR(st))
447 return PTR_ERR(st);
448
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;
453
454 kfree(st);
455 return 0;
456}
457
458/**
459 * v9fs_vfs_setattr_dotl - set file metadata
460 * @dentry: file whose metadata to set
461 * @iattr: metadata assignment structure
462 *
463 */
464
465int v9fs_vfs_setattr_dotl(struct dentry *dentry, struct iattr *iattr)
466{
467 int retval;
468 struct v9fs_session_info *v9ses;
469 struct p9_fid *fid;
470 struct p9_iattr_dotl p9attr;
471
472 P9_DPRINTK(P9_DEBUG_VFS, "\n");
473
474 retval = inode_change_ok(dentry->d_inode, iattr);
475 if (retval)
476 return retval;
477
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;
487
488 retval = -EPERM;
Aneesh Kumar K.V42869c82011-03-08 16:39:50 +0530489 v9ses = v9fs_dentry2v9ses(dentry);
Aneesh Kumar K.V53c06f42011-01-10 13:51:47 -0600490 fid = v9fs_fid_lookup(dentry);
491 if (IS_ERR(fid))
492 return PTR_ERR(fid);
493
Aneesh Kumar K.V3dc54362011-02-28 17:04:11 +0530494 /* Write all dirty data */
495 if (S_ISREG(dentry->d_inode->i_mode))
496 filemap_write_and_wait(dentry->d_inode->i_mapping);
497
Aneesh Kumar K.Vf10fc502011-02-28 17:04:10 +0530498 retval = p9_client_setattr(fid, &p9attr);
499 if (retval < 0)
500 return retval;
Aneesh Kumar K.V53c06f42011-01-10 13:51:47 -0600501
Aneesh Kumar K.V059c1382011-03-08 16:39:48 +0530502 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);
505
506 v9fs_invalidate_inode_attr(dentry->d_inode);
Aneesh Kumar K.V53c06f42011-01-10 13:51:47 -0600507 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;
514 }
515 return 0;
516}
517
518/**
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
523 *
524 */
525
526void
527v9fs_stat2inode_dotl(struct p9_stat_dotl *stat, struct inode *inode)
528{
Aneesh Kumar K.Vb3cbea02011-02-28 17:04:06 +0530529 struct v9fs_inode *v9inode = V9FS_I(inode);
Aneesh Kumar K.V53c06f42011-01-10 13:51:47 -0600530
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);
543
544 if ((S_ISBLK(inode->i_mode)) || (S_ISCHR(inode->i_mode)))
545 init_special_inode(inode, inode->i_mode, inode->i_rdev);
546
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;
553 }
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;
557 }
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;
561 }
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);
574 }
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;
581 }
582 if (stat->st_result_mask & P9_STATS_GEN)
Aneesh Kumar K.Vfd2421f2011-07-11 16:40:59 +0000583 inode->i_generation = stat->st_gen;
Aneesh Kumar K.V53c06f42011-01-10 13:51:47 -0600584
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.
587 */
Aneesh Kumar K.Vb3cbea02011-02-28 17:04:06 +0530588 v9inode->cache_validity &= ~V9FS_INO_INVALID_ATTR;
Aneesh Kumar K.V53c06f42011-01-10 13:51:47 -0600589}
590
591static int
592v9fs_vfs_symlink_dotl(struct inode *dir, struct dentry *dentry,
593 const char *symname)
594{
Aneesh Kumar K.V53c06f42011-01-10 13:51:47 -0600595 int err;
596 gid_t gid;
Aneesh Kumar K.Vd28c61f2011-02-28 17:04:08 +0530597 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;
Aneesh Kumar K.V53c06f42011-01-10 13:51:47 -0600603
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);
608
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;
614 }
615
616 gid = v9fs_get_fsgid_for_create(dir);
617
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);
620
621 if (err < 0) {
622 P9_DPRINTK(P9_DEBUG_VFS, "p9_client_symlink failed %d\n", err);
623 goto error;
624 }
625
Aneesh Kumar K.Vd28c61f2011-02-28 17:04:08 +0530626 v9fs_invalidate_inode_attr(dir);
Aneesh Kumar K.V53c06f42011-01-10 13:51:47 -0600627 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;
636 }
637
638 /* instantiate inode and assign the unopened fid to dentry */
Aneesh Kumar K.Va78ce052011-02-28 17:04:02 +0530639 inode = v9fs_get_inode_from_fid(v9ses, fid, dir->i_sb);
Aneesh Kumar K.V53c06f42011-01-10 13:51:47 -0600640 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;
645 }
Aneesh Kumar K.V53c06f42011-01-10 13:51:47 -0600646 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;
657 }
Aneesh Kumar K.V53c06f42011-01-10 13:51:47 -0600658 d_instantiate(dentry, inode);
659 }
660
661error:
662 if (fid)
663 p9_client_clunk(fid);
664
665 return err;
666}
667
668/**
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
673 *
674 */
675
676static int
677v9fs_vfs_link_dotl(struct dentry *old_dentry, struct inode *dir,
678 struct dentry *dentry)
679{
680 int err;
Aneesh Kumar K.V53c06f42011-01-10 13:51:47 -0600681 char *name;
Aneesh Kumar K.V53c06f42011-01-10 13:51:47 -0600682 struct dentry *dir_dentry;
Aneesh Kumar K.Vd28c61f2011-02-28 17:04:08 +0530683 struct p9_fid *dfid, *oldfid;
684 struct v9fs_session_info *v9ses;
Aneesh Kumar K.V53c06f42011-01-10 13:51:47 -0600685
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);
689
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);
695
696 oldfid = v9fs_fid_lookup(old_dentry);
697 if (IS_ERR(oldfid))
698 return PTR_ERR(oldfid);
699
700 name = (char *) dentry->d_name.name;
701
702 err = p9_client_link(dfid, oldfid, (char *)dentry->d_name.name);
703
704 if (err < 0) {
705 P9_DPRINTK(P9_DEBUG_VFS, "p9_client_link failed %d\n", err);
706 return err;
707 }
708
Aneesh Kumar K.Vd28c61f2011-02-28 17:04:08 +0530709 v9fs_invalidate_inode_attr(dir);
Aneesh Kumar K.V53c06f42011-01-10 13:51:47 -0600710 if (v9ses->cache == CACHE_LOOSE || v9ses->cache == CACHE_FSCACHE) {
711 /* Get the latest stat info from server. */
712 struct p9_fid *fid;
Aneesh Kumar K.V53c06f42011-01-10 13:51:47 -0600713 fid = v9fs_fid_lookup(old_dentry);
714 if (IS_ERR(fid))
715 return PTR_ERR(fid);
716
Aneesh Kumar K.Vc06c0662011-02-28 17:04:09 +0530717 v9fs_refresh_inode_dotl(fid, old_dentry->d_inode);
Aneesh Kumar K.V53c06f42011-01-10 13:51:47 -0600718 }
Aneesh Kumar K.V20656a42011-02-28 17:03:55 +0530719 ihold(old_dentry->d_inode);
Aneesh Kumar K.V53c06f42011-01-10 13:51:47 -0600720 d_instantiate(dentry, old_dentry->d_inode);
721
722 return err;
723}
724
725/**
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
731 *
732 */
733static int
734v9fs_vfs_mknod_dotl(struct inode *dir, struct dentry *dentry, int omode,
735 dev_t rdev)
736{
737 int err;
Aneesh Kumar K.Vd28c61f2011-02-28 17:04:08 +0530738 gid_t gid;
Aneesh Kumar K.V53c06f42011-01-10 13:51:47 -0600739 char *name;
740 mode_t mode;
741 struct v9fs_session_info *v9ses;
742 struct p9_fid *fid = NULL, *dfid = NULL;
743 struct inode *inode;
Aneesh Kumar K.V53c06f42011-01-10 13:51:47 -0600744 struct p9_qid qid;
745 struct dentry *dir_dentry;
746 struct posix_acl *dacl = NULL, *pacl = NULL;
747
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));
751
752 if (!new_valid_dev(rdev))
753 return -EINVAL;
754
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;
763 }
764
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;
773 }
774 name = (char *) dentry->d_name.name;
775
776 err = p9_client_mknod_dotl(dfid, name, mode, rdev, gid, &qid);
777 if (err < 0)
778 goto error;
779
Aneesh Kumar K.Vd28c61f2011-02-28 17:04:08 +0530780 v9fs_invalidate_inode_attr(dir);
Aneesh Kumar K.V53c06f42011-01-10 13:51:47 -0600781 /* 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;
790 }
791
Aneesh Kumar K.Va78ce052011-02-28 17:04:02 +0530792 inode = v9fs_get_inode_from_fid(v9ses, fid, dir->i_sb);
Aneesh Kumar K.V53c06f42011-01-10 13:51:47 -0600793 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;
798 }
Aneesh Kumar K.V53c06f42011-01-10 13:51:47 -0600799 d_instantiate(dentry, inode);
800 err = v9fs_fid_add(dentry, fid);
801 if (err < 0)
802 goto error;
803 fid = NULL;
804 } else {
805 /*
806 * Not in cached mode. No need to populate inode with stat.
807 * socket syscall returns a fd, so we need instantiate
808 */
809 inode = v9fs_get_inode(dir->i_sb, mode);
810 if (IS_ERR(inode)) {
811 err = PTR_ERR(inode);
812 goto error;
813 }
Aneesh Kumar K.V53c06f42011-01-10 13:51:47 -0600814 d_instantiate(dentry, inode);
815 }
816 /* Now set the ACL based on the default value */
817 v9fs_set_create_acl(dentry, dacl, pacl);
818error:
819 if (fid)
820 p9_client_clunk(fid);
821 return err;
822}
823
Aneesh Kumar K.V53c06f42011-01-10 13:51:47 -0600824/**
825 * v9fs_vfs_follow_link_dotl - follow a symlink path
826 * @dentry: dentry for symlink
827 * @nd: nameidata
828 *
829 */
830
831static void *
832v9fs_vfs_follow_link_dotl(struct dentry *dentry, struct nameidata *nd)
833{
M. Mohan Kumar31b6cea2011-01-08 07:28:46 +0530834 int retval;
835 struct p9_fid *fid;
Aneesh Kumar K.V53c06f42011-01-10 13:51:47 -0600836 char *link = __getname();
M. Mohan Kumar31b6cea2011-01-08 07:28:46 +0530837 char *target;
Aneesh Kumar K.V53c06f42011-01-10 13:51:47 -0600838
M. Mohan Kumar31b6cea2011-01-08 07:28:46 +0530839 P9_DPRINTK(P9_DEBUG_VFS, "%s\n", dentry->d_name.name);
Aneesh Kumar K.V53c06f42011-01-10 13:51:47 -0600840
M. Mohan Kumar31b6cea2011-01-08 07:28:46 +0530841 if (!link) {
Aneesh Kumar K.V53c06f42011-01-10 13:51:47 -0600842 link = ERR_PTR(-ENOMEM);
M. Mohan Kumar31b6cea2011-01-08 07:28:46 +0530843 goto ndset;
Aneesh Kumar K.V53c06f42011-01-10 13:51:47 -0600844 }
M. Mohan Kumar31b6cea2011-01-08 07:28:46 +0530845 fid = v9fs_fid_lookup(dentry);
846 if (IS_ERR(fid)) {
847 __putname(link);
Aneesh Kumar K.V936bb2d2011-03-24 23:04:41 +0530848 link = ERR_CAST(fid);
M. Mohan Kumar31b6cea2011-01-08 07:28:46 +0530849 goto ndset;
850 }
851 retval = p9_client_readlink(fid, &target);
852 if (!retval) {
853 strcpy(link, target);
854 kfree(target);
855 goto ndset;
856 }
857 __putname(link);
858 link = ERR_PTR(retval);
859ndset:
Aneesh Kumar K.V53c06f42011-01-10 13:51:47 -0600860 nd_set_link(nd, link);
Aneesh Kumar K.V53c06f42011-01-10 13:51:47 -0600861 return NULL;
862}
863
Aneesh Kumar K.Vb3cbea02011-02-28 17:04:06 +0530864int v9fs_refresh_inode_dotl(struct p9_fid *fid, struct inode *inode)
865{
866 loff_t i_size;
867 struct p9_stat_dotl *st;
868 struct v9fs_session_info *v9ses;
869
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);
874
875 spin_lock(&inode->i_lock);
876 /*
877 * We don't want to refresh inode->i_size,
878 * because we may have cached data
879 */
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;
887}
888
Aneesh Kumar K.V53c06f42011-01-10 13:51:47 -0600889const 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,
906};
907
908const 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,
916};
917
918const struct inode_operations v9fs_symlink_inode_operations_dotl = {
M. Mohan Kumar31b6cea2011-01-08 07:28:46 +0530919 .readlink = generic_readlink,
Aneesh Kumar K.V53c06f42011-01-10 13:51:47 -0600920 .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,
928};