blob: a3f2540cc4b22c30e5c9d85c0c5c76fee99ceb93 [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
Aneesh Kumar K.Ved80fcf2011-07-06 16:32:31 +0530111/* Always get a new inode */
112static int v9fs_test_new_inode_dotl(struct inode *inode, void *data)
113{
114 return 0;
115}
116
Aneesh Kumar K.Vfd2421f2011-07-11 16:40:59 +0000117static int v9fs_set_inode_dotl(struct inode *inode, void *data)
118{
119 struct v9fs_inode *v9inode = V9FS_I(inode);
120 struct p9_stat_dotl *st = (struct p9_stat_dotl *)data;
121
122 memcpy(&v9inode->qid, &st->qid, sizeof(st->qid));
123 inode->i_generation = st->st_gen;
124 return 0;
125}
126
Aneesh Kumar K.V5ffc0cb2011-02-28 17:04:01 +0530127static struct inode *v9fs_qid_iget_dotl(struct super_block *sb,
128 struct p9_qid *qid,
129 struct p9_fid *fid,
Aneesh Kumar K.Ved80fcf2011-07-06 16:32:31 +0530130 struct p9_stat_dotl *st,
131 int new)
Aneesh Kumar K.V5ffc0cb2011-02-28 17:04:01 +0530132{
133 int retval;
134 unsigned long i_ino;
135 struct inode *inode;
136 struct v9fs_session_info *v9ses = sb->s_fs_info;
Aneesh Kumar K.Ved80fcf2011-07-06 16:32:31 +0530137 int (*test)(struct inode *, void *);
138
139 if (new)
140 test = v9fs_test_new_inode_dotl;
141 else
142 test = v9fs_test_inode_dotl;
Aneesh Kumar K.V5ffc0cb2011-02-28 17:04:01 +0530143
144 i_ino = v9fs_qid2ino(qid);
Aneesh Kumar K.Ved80fcf2011-07-06 16:32:31 +0530145 inode = iget5_locked(sb, i_ino, test, v9fs_set_inode_dotl, st);
Aneesh Kumar K.V5ffc0cb2011-02-28 17:04:01 +0530146 if (!inode)
147 return ERR_PTR(-ENOMEM);
148 if (!(inode->i_state & I_NEW))
149 return inode;
150 /*
151 * initialize the inode with the stat info
152 * FIXME!! we may need support for stale inodes
153 * later.
154 */
Aneesh Kumar K.Vfd2421f2011-07-11 16:40:59 +0000155 inode->i_ino = i_ino;
Aneesh Kumar K.V45089142011-07-25 18:06:33 +0000156 retval = v9fs_init_inode(v9ses, inode,
157 st->st_mode, new_decode_dev(st->st_rdev));
Aneesh Kumar K.V5ffc0cb2011-02-28 17:04:01 +0530158 if (retval)
159 goto error;
160
161 v9fs_stat2inode_dotl(st, inode);
162#ifdef CONFIG_9P_FSCACHE
Aneesh Kumar K.V5ffc0cb2011-02-28 17:04:01 +0530163 v9fs_cache_inode_get_cookie(inode);
164#endif
165 retval = v9fs_get_acl(inode, fid);
166 if (retval)
167 goto error;
168
169 unlock_new_inode(inode);
170 return inode;
171error:
172 unlock_new_inode(inode);
173 iput(inode);
174 return ERR_PTR(retval);
175
176}
177
Aneesh Kumar K.V53c06f42011-01-10 13:51:47 -0600178struct inode *
Aneesh Kumar K.Va78ce052011-02-28 17:04:02 +0530179v9fs_inode_from_fid_dotl(struct v9fs_session_info *v9ses, struct p9_fid *fid,
Aneesh Kumar K.Ved80fcf2011-07-06 16:32:31 +0530180 struct super_block *sb, int new)
Aneesh Kumar K.V53c06f42011-01-10 13:51:47 -0600181{
Aneesh Kumar K.V53c06f42011-01-10 13:51:47 -0600182 struct p9_stat_dotl *st;
Aneesh Kumar K.V5ffc0cb2011-02-28 17:04:01 +0530183 struct inode *inode = NULL;
Aneesh Kumar K.V53c06f42011-01-10 13:51:47 -0600184
Aneesh Kumar K.Vfd2421f2011-07-11 16:40:59 +0000185 st = p9_client_getattr_dotl(fid, P9_STATS_BASIC | P9_STATS_GEN);
Aneesh Kumar K.V53c06f42011-01-10 13:51:47 -0600186 if (IS_ERR(st))
187 return ERR_CAST(st);
188
Aneesh Kumar K.Ved80fcf2011-07-06 16:32:31 +0530189 inode = v9fs_qid_iget_dotl(sb, &st->qid, fid, st, new);
Aneesh Kumar K.V53c06f42011-01-10 13:51:47 -0600190 kfree(st);
Aneesh Kumar K.V5ffc0cb2011-02-28 17:04:01 +0530191 return inode;
Aneesh Kumar K.V53c06f42011-01-10 13:51:47 -0600192}
193
194/**
195 * v9fs_vfs_create_dotl - VFS hook to create files for 9P2000.L protocol.
196 * @dir: directory inode that is being created
197 * @dentry: dentry that is being deleted
198 * @mode: create permissions
199 * @nd: path information
200 *
201 */
202
203static int
204v9fs_vfs_create_dotl(struct inode *dir, struct dentry *dentry, int omode,
205 struct nameidata *nd)
206{
207 int err = 0;
Aneesh Kumar K.V53c06f42011-01-10 13:51:47 -0600208 gid_t gid;
209 int flags;
Al Virod3fb6122011-07-23 18:37:50 -0400210 umode_t mode;
Aneesh Kumar K.V6b39f6d2011-02-28 17:04:03 +0530211 char *name = NULL;
Aneesh Kumar K.V53c06f42011-01-10 13:51:47 -0600212 struct file *filp;
213 struct p9_qid qid;
214 struct inode *inode;
Aneesh Kumar K.V6b39f6d2011-02-28 17:04:03 +0530215 struct p9_fid *fid = NULL;
216 struct v9fs_inode *v9inode;
217 struct p9_fid *dfid, *ofid, *inode_fid;
218 struct v9fs_session_info *v9ses;
Aneesh Kumar K.V53c06f42011-01-10 13:51:47 -0600219 struct posix_acl *pacl = NULL, *dacl = NULL;
220
221 v9ses = v9fs_inode2v9ses(dir);
Al Virodd7dd552011-06-25 21:17:17 -0400222 if (nd)
Al Viro8a5e9292011-06-25 19:15:54 -0400223 flags = nd->intent.open.flags;
Aneesh Kumar K.V53c06f42011-01-10 13:51:47 -0600224 else {
225 /*
226 * create call without LOOKUP_OPEN is due
227 * to mknod of regular files. So use mknod
228 * operation.
229 */
230 return v9fs_vfs_mknod_dotl(dir, dentry, omode, 0);
231 }
232
233 name = (char *) dentry->d_name.name;
234 P9_DPRINTK(P9_DEBUG_VFS, "v9fs_vfs_create_dotl: name:%s flags:0x%x "
235 "mode:0x%x\n", name, flags, omode);
236
237 dfid = v9fs_fid_lookup(dentry->d_parent);
238 if (IS_ERR(dfid)) {
239 err = PTR_ERR(dfid);
240 P9_DPRINTK(P9_DEBUG_VFS, "fid lookup failed %d\n", err);
241 return err;
242 }
243
244 /* clone a fid to use for creation */
245 ofid = p9_client_walk(dfid, 0, NULL, 1);
246 if (IS_ERR(ofid)) {
247 err = PTR_ERR(ofid);
248 P9_DPRINTK(P9_DEBUG_VFS, "p9_client_walk failed %d\n", err);
249 return err;
250 }
251
252 gid = v9fs_get_fsgid_for_create(dir);
253
254 mode = omode;
255 /* Update mode based on ACL value */
256 err = v9fs_acl_mode(dir, &mode, &dacl, &pacl);
257 if (err) {
258 P9_DPRINTK(P9_DEBUG_VFS,
259 "Failed to get acl values in creat %d\n", err);
260 goto error;
261 }
262 err = p9_client_create_dotl(ofid, name, flags, mode, gid, &qid);
263 if (err < 0) {
264 P9_DPRINTK(P9_DEBUG_VFS,
265 "p9_client_open_dotl failed in creat %d\n",
266 err);
267 goto error;
268 }
Aneesh Kumar K.Vd28c61f2011-02-28 17:04:08 +0530269 v9fs_invalidate_inode_attr(dir);
Aneesh Kumar K.V53c06f42011-01-10 13:51:47 -0600270
Aneesh Kumar K.Vaf7542f2011-01-10 14:22:21 -0600271 /* instantiate inode and assign the unopened fid to the dentry */
272 fid = p9_client_walk(dfid, 1, &name, 1);
273 if (IS_ERR(fid)) {
274 err = PTR_ERR(fid);
Eric Van Hensbergenc25a61f2011-01-11 09:49:03 -0600275 P9_DPRINTK(P9_DEBUG_VFS, "p9_client_walk failed %d\n", err);
Aneesh Kumar K.V53c06f42011-01-10 13:51:47 -0600276 fid = NULL;
Aneesh Kumar K.Vaf7542f2011-01-10 14:22:21 -0600277 goto error;
Aneesh Kumar K.V53c06f42011-01-10 13:51:47 -0600278 }
Aneesh Kumar K.Ved80fcf2011-07-06 16:32:31 +0530279 inode = v9fs_get_new_inode_from_fid(v9ses, fid, dir->i_sb);
Aneesh Kumar K.Vaf7542f2011-01-10 14:22:21 -0600280 if (IS_ERR(inode)) {
281 err = PTR_ERR(inode);
282 P9_DPRINTK(P9_DEBUG_VFS, "inode creation failed %d\n", err);
283 goto error;
284 }
Aneesh Kumar K.Vaf7542f2011-01-10 14:22:21 -0600285 err = v9fs_fid_add(dentry, fid);
286 if (err < 0)
287 goto error;
Aneesh Kumar K.V5441ae52011-07-25 18:06:32 +0000288 d_instantiate(dentry, inode);
Aneesh Kumar K.Vaf7542f2011-01-10 14:22:21 -0600289
Aneesh Kumar K.V53c06f42011-01-10 13:51:47 -0600290 /* Now set the ACL based on the default value */
Al Viro1ec95bf2011-07-23 02:28:13 -0400291 v9fs_set_create_acl(dentry, &dacl, &pacl);
Aneesh Kumar K.V6b39f6d2011-02-28 17:04:03 +0530292
293 v9inode = V9FS_I(inode);
Aneesh Kumar K.V5a7e0a82011-03-08 16:39:46 +0530294 mutex_lock(&v9inode->v_mutex);
Aneesh Kumar K.V7add6972011-03-08 16:39:49 +0530295 if (v9ses->cache && !v9inode->writeback_fid &&
296 ((flags & O_ACCMODE) != O_RDONLY)) {
Aneesh Kumar K.V3cf387d2011-02-28 17:03:57 +0530297 /*
Aneesh Kumar K.V6b39f6d2011-02-28 17:04:03 +0530298 * clone a fid and add it to writeback_fid
Aneesh Kumar K.V3cf387d2011-02-28 17:03:57 +0530299 * we do it during open time instead of
300 * page dirty time via write_begin/page_mkwrite
301 * because we want write after unlink usecase
302 * to work.
303 */
304 inode_fid = v9fs_writeback_fid(dentry);
305 if (IS_ERR(inode_fid)) {
306 err = PTR_ERR(inode_fid);
Aneesh Kumar K.V5a7e0a82011-03-08 16:39:46 +0530307 mutex_unlock(&v9inode->v_mutex);
Aneesh Kumar K.V398c4f02011-05-20 18:55:51 +0000308 goto err_clunk_old_fid;
Aneesh Kumar K.V3cf387d2011-02-28 17:03:57 +0530309 }
Aneesh Kumar K.V6b39f6d2011-02-28 17:04:03 +0530310 v9inode->writeback_fid = (void *) inode_fid;
Aneesh Kumar K.V3cf387d2011-02-28 17:03:57 +0530311 }
Aneesh Kumar K.V5a7e0a82011-03-08 16:39:46 +0530312 mutex_unlock(&v9inode->v_mutex);
Aneesh Kumar K.Vaf7542f2011-01-10 14:22:21 -0600313 /* Since we are opening a file, assign the open fid to the file */
314 filp = lookup_instantiate_filp(nd, dentry, generic_file_open);
315 if (IS_ERR(filp)) {
Aneesh Kumar K.V398c4f02011-05-20 18:55:51 +0000316 err = PTR_ERR(filp);
317 goto err_clunk_old_fid;
Aneesh Kumar K.Vaf7542f2011-01-10 14:22:21 -0600318 }
319 filp->private_data = ofid;
Aneesh Kumar K.V46848de2011-02-28 17:03:55 +0530320#ifdef CONFIG_9P_FSCACHE
321 if (v9ses->cache)
322 v9fs_cache_inode_set_cookie(inode, filp);
323#endif
Aneesh Kumar K.V53c06f42011-01-10 13:51:47 -0600324 return 0;
325
326error:
Aneesh Kumar K.V53c06f42011-01-10 13:51:47 -0600327 if (fid)
328 p9_client_clunk(fid);
Aneesh Kumar K.V398c4f02011-05-20 18:55:51 +0000329err_clunk_old_fid:
330 if (ofid)
331 p9_client_clunk(ofid);
Al Viro1ec95bf2011-07-23 02:28:13 -0400332 v9fs_set_create_acl(NULL, &dacl, &pacl);
Aneesh Kumar K.V53c06f42011-01-10 13:51:47 -0600333 return err;
334}
335
336/**
337 * v9fs_vfs_mkdir_dotl - VFS mkdir hook to create a directory
338 * @dir: inode that is being unlinked
339 * @dentry: dentry that is being unlinked
340 * @mode: mode for new directory
341 *
342 */
343
344static int v9fs_vfs_mkdir_dotl(struct inode *dir,
345 struct dentry *dentry, int omode)
346{
347 int err;
348 struct v9fs_session_info *v9ses;
349 struct p9_fid *fid = NULL, *dfid = NULL;
350 gid_t gid;
351 char *name;
Al Virod3fb6122011-07-23 18:37:50 -0400352 umode_t mode;
Aneesh Kumar K.V53c06f42011-01-10 13:51:47 -0600353 struct inode *inode;
354 struct p9_qid qid;
355 struct dentry *dir_dentry;
356 struct posix_acl *dacl = NULL, *pacl = NULL;
357
358 P9_DPRINTK(P9_DEBUG_VFS, "name %s\n", dentry->d_name.name);
359 err = 0;
360 v9ses = v9fs_inode2v9ses(dir);
361
362 omode |= S_IFDIR;
363 if (dir->i_mode & S_ISGID)
364 omode |= S_ISGID;
365
366 dir_dentry = v9fs_dentry_from_dir_inode(dir);
367 dfid = v9fs_fid_lookup(dir_dentry);
368 if (IS_ERR(dfid)) {
369 err = PTR_ERR(dfid);
370 P9_DPRINTK(P9_DEBUG_VFS, "fid lookup failed %d\n", err);
371 dfid = NULL;
372 goto error;
373 }
374
375 gid = v9fs_get_fsgid_for_create(dir);
376 mode = omode;
377 /* Update mode based on ACL value */
378 err = v9fs_acl_mode(dir, &mode, &dacl, &pacl);
379 if (err) {
380 P9_DPRINTK(P9_DEBUG_VFS,
381 "Failed to get acl values in mkdir %d\n", err);
382 goto error;
383 }
384 name = (char *) dentry->d_name.name;
385 err = p9_client_mkdir_dotl(dfid, name, mode, gid, &qid);
386 if (err < 0)
387 goto error;
388
389 /* instantiate inode and assign the unopened fid to the dentry */
390 if (v9ses->cache == CACHE_LOOSE || v9ses->cache == CACHE_FSCACHE) {
391 fid = p9_client_walk(dfid, 1, &name, 1);
392 if (IS_ERR(fid)) {
393 err = PTR_ERR(fid);
394 P9_DPRINTK(P9_DEBUG_VFS, "p9_client_walk failed %d\n",
395 err);
396 fid = NULL;
397 goto error;
398 }
399
Aneesh Kumar K.Ved80fcf2011-07-06 16:32:31 +0530400 inode = v9fs_get_new_inode_from_fid(v9ses, fid, dir->i_sb);
Aneesh Kumar K.V53c06f42011-01-10 13:51:47 -0600401 if (IS_ERR(inode)) {
402 err = PTR_ERR(inode);
403 P9_DPRINTK(P9_DEBUG_VFS, "inode creation failed %d\n",
404 err);
405 goto error;
406 }
Aneesh Kumar K.V53c06f42011-01-10 13:51:47 -0600407 err = v9fs_fid_add(dentry, fid);
408 if (err < 0)
409 goto error;
Aneesh Kumar K.V5441ae52011-07-25 18:06:32 +0000410 d_instantiate(dentry, inode);
Aneesh Kumar K.V53c06f42011-01-10 13:51:47 -0600411 fid = NULL;
412 } else {
413 /*
414 * Not in cached mode. No need to populate
415 * inode with stat. We need to get an inode
416 * so that we can set the acl with dentry
417 */
Aneesh Kumar K.V45089142011-07-25 18:06:33 +0000418 inode = v9fs_get_inode(dir->i_sb, mode, 0);
Aneesh Kumar K.V53c06f42011-01-10 13:51:47 -0600419 if (IS_ERR(inode)) {
420 err = PTR_ERR(inode);
421 goto error;
422 }
Aneesh Kumar K.V53c06f42011-01-10 13:51:47 -0600423 d_instantiate(dentry, inode);
424 }
425 /* Now set the ACL based on the default value */
Al Viro1ec95bf2011-07-23 02:28:13 -0400426 v9fs_set_create_acl(dentry, &dacl, &pacl);
Aneesh Kumar K.Vb271ec42011-02-28 17:04:05 +0530427 inc_nlink(dir);
Aneesh Kumar K.Vd28c61f2011-02-28 17:04:08 +0530428 v9fs_invalidate_inode_attr(dir);
Aneesh Kumar K.V53c06f42011-01-10 13:51:47 -0600429error:
430 if (fid)
431 p9_client_clunk(fid);
Al Viro1ec95bf2011-07-23 02:28:13 -0400432 v9fs_set_create_acl(NULL, &dacl, &pacl);
Aneesh Kumar K.V53c06f42011-01-10 13:51:47 -0600433 return err;
434}
435
436static int
437v9fs_vfs_getattr_dotl(struct vfsmount *mnt, struct dentry *dentry,
438 struct kstat *stat)
439{
440 int err;
441 struct v9fs_session_info *v9ses;
442 struct p9_fid *fid;
443 struct p9_stat_dotl *st;
444
445 P9_DPRINTK(P9_DEBUG_VFS, "dentry: %p\n", dentry);
446 err = -EPERM;
Aneesh Kumar K.V42869c82011-03-08 16:39:50 +0530447 v9ses = v9fs_dentry2v9ses(dentry);
Aneesh Kumar K.Va1211902011-02-28 17:04:01 +0530448 if (v9ses->cache == CACHE_LOOSE || v9ses->cache == CACHE_FSCACHE) {
449 generic_fillattr(dentry->d_inode, stat);
450 return 0;
451 }
Aneesh Kumar K.V53c06f42011-01-10 13:51:47 -0600452 fid = v9fs_fid_lookup(dentry);
453 if (IS_ERR(fid))
454 return PTR_ERR(fid);
455
456 /* Ask for all the fields in stat structure. Server will return
457 * whatever it supports
458 */
459
460 st = p9_client_getattr_dotl(fid, P9_STATS_ALL);
461 if (IS_ERR(st))
462 return PTR_ERR(st);
463
464 v9fs_stat2inode_dotl(st, dentry->d_inode);
465 generic_fillattr(dentry->d_inode, stat);
466 /* Change block size to what the server returned */
467 stat->blksize = st->st_blksize;
468
469 kfree(st);
470 return 0;
471}
472
473/**
474 * v9fs_vfs_setattr_dotl - set file metadata
475 * @dentry: file whose metadata to set
476 * @iattr: metadata assignment structure
477 *
478 */
479
480int v9fs_vfs_setattr_dotl(struct dentry *dentry, struct iattr *iattr)
481{
482 int retval;
483 struct v9fs_session_info *v9ses;
484 struct p9_fid *fid;
485 struct p9_iattr_dotl p9attr;
486
487 P9_DPRINTK(P9_DEBUG_VFS, "\n");
488
489 retval = inode_change_ok(dentry->d_inode, iattr);
490 if (retval)
491 return retval;
492
493 p9attr.valid = iattr->ia_valid;
494 p9attr.mode = iattr->ia_mode;
495 p9attr.uid = iattr->ia_uid;
496 p9attr.gid = iattr->ia_gid;
497 p9attr.size = iattr->ia_size;
498 p9attr.atime_sec = iattr->ia_atime.tv_sec;
499 p9attr.atime_nsec = iattr->ia_atime.tv_nsec;
500 p9attr.mtime_sec = iattr->ia_mtime.tv_sec;
501 p9attr.mtime_nsec = iattr->ia_mtime.tv_nsec;
502
503 retval = -EPERM;
Aneesh Kumar K.V42869c82011-03-08 16:39:50 +0530504 v9ses = v9fs_dentry2v9ses(dentry);
Aneesh Kumar K.V53c06f42011-01-10 13:51:47 -0600505 fid = v9fs_fid_lookup(dentry);
506 if (IS_ERR(fid))
507 return PTR_ERR(fid);
508
Aneesh Kumar K.V3dc54362011-02-28 17:04:11 +0530509 /* Write all dirty data */
510 if (S_ISREG(dentry->d_inode->i_mode))
511 filemap_write_and_wait(dentry->d_inode->i_mapping);
512
Aneesh Kumar K.Vf10fc502011-02-28 17:04:10 +0530513 retval = p9_client_setattr(fid, &p9attr);
514 if (retval < 0)
515 return retval;
Aneesh Kumar K.V53c06f42011-01-10 13:51:47 -0600516
Aneesh Kumar K.V059c1382011-03-08 16:39:48 +0530517 if ((iattr->ia_valid & ATTR_SIZE) &&
518 iattr->ia_size != i_size_read(dentry->d_inode))
519 truncate_setsize(dentry->d_inode, iattr->ia_size);
520
521 v9fs_invalidate_inode_attr(dentry->d_inode);
Aneesh Kumar K.V53c06f42011-01-10 13:51:47 -0600522 setattr_copy(dentry->d_inode, iattr);
523 mark_inode_dirty(dentry->d_inode);
524 if (iattr->ia_valid & ATTR_MODE) {
525 /* We also want to update ACL when we update mode bits */
526 retval = v9fs_acl_chmod(dentry);
527 if (retval < 0)
528 return retval;
529 }
530 return 0;
531}
532
533/**
534 * v9fs_stat2inode_dotl - populate an inode structure with stat info
535 * @stat: stat structure
536 * @inode: inode to populate
537 * @sb: superblock of filesystem
538 *
539 */
540
541void
542v9fs_stat2inode_dotl(struct p9_stat_dotl *stat, struct inode *inode)
543{
Aneesh Kumar K.V45089142011-07-25 18:06:33 +0000544 mode_t mode;
Aneesh Kumar K.Vb3cbea02011-02-28 17:04:06 +0530545 struct v9fs_inode *v9inode = V9FS_I(inode);
Aneesh Kumar K.V53c06f42011-01-10 13:51:47 -0600546
547 if ((stat->st_result_mask & P9_STATS_BASIC) == P9_STATS_BASIC) {
548 inode->i_atime.tv_sec = stat->st_atime_sec;
549 inode->i_atime.tv_nsec = stat->st_atime_nsec;
550 inode->i_mtime.tv_sec = stat->st_mtime_sec;
551 inode->i_mtime.tv_nsec = stat->st_mtime_nsec;
552 inode->i_ctime.tv_sec = stat->st_ctime_sec;
553 inode->i_ctime.tv_nsec = stat->st_ctime_nsec;
554 inode->i_uid = stat->st_uid;
555 inode->i_gid = stat->st_gid;
556 inode->i_nlink = stat->st_nlink;
Aneesh Kumar K.V53c06f42011-01-10 13:51:47 -0600557
Aneesh Kumar K.V45089142011-07-25 18:06:33 +0000558 mode = stat->st_mode & S_IALLUGO;
559 mode |= inode->i_mode & ~S_IALLUGO;
560 inode->i_mode = mode;
Aneesh Kumar K.V53c06f42011-01-10 13:51:47 -0600561
562 i_size_write(inode, stat->st_size);
563 inode->i_blocks = stat->st_blocks;
564 } else {
565 if (stat->st_result_mask & P9_STATS_ATIME) {
566 inode->i_atime.tv_sec = stat->st_atime_sec;
567 inode->i_atime.tv_nsec = stat->st_atime_nsec;
568 }
569 if (stat->st_result_mask & P9_STATS_MTIME) {
570 inode->i_mtime.tv_sec = stat->st_mtime_sec;
571 inode->i_mtime.tv_nsec = stat->st_mtime_nsec;
572 }
573 if (stat->st_result_mask & P9_STATS_CTIME) {
574 inode->i_ctime.tv_sec = stat->st_ctime_sec;
575 inode->i_ctime.tv_nsec = stat->st_ctime_nsec;
576 }
577 if (stat->st_result_mask & P9_STATS_UID)
578 inode->i_uid = stat->st_uid;
579 if (stat->st_result_mask & P9_STATS_GID)
580 inode->i_gid = stat->st_gid;
581 if (stat->st_result_mask & P9_STATS_NLINK)
582 inode->i_nlink = stat->st_nlink;
583 if (stat->st_result_mask & P9_STATS_MODE) {
584 inode->i_mode = stat->st_mode;
585 if ((S_ISBLK(inode->i_mode)) ||
586 (S_ISCHR(inode->i_mode)))
587 init_special_inode(inode, inode->i_mode,
588 inode->i_rdev);
589 }
590 if (stat->st_result_mask & P9_STATS_RDEV)
591 inode->i_rdev = new_decode_dev(stat->st_rdev);
592 if (stat->st_result_mask & P9_STATS_SIZE)
593 i_size_write(inode, stat->st_size);
594 if (stat->st_result_mask & P9_STATS_BLOCKS)
595 inode->i_blocks = stat->st_blocks;
596 }
597 if (stat->st_result_mask & P9_STATS_GEN)
Aneesh Kumar K.Vfd2421f2011-07-11 16:40:59 +0000598 inode->i_generation = stat->st_gen;
Aneesh Kumar K.V53c06f42011-01-10 13:51:47 -0600599
600 /* Currently we don't support P9_STATS_BTIME and P9_STATS_DATA_VERSION
601 * because the inode structure does not have fields for them.
602 */
Aneesh Kumar K.Vb3cbea02011-02-28 17:04:06 +0530603 v9inode->cache_validity &= ~V9FS_INO_INVALID_ATTR;
Aneesh Kumar K.V53c06f42011-01-10 13:51:47 -0600604}
605
606static int
607v9fs_vfs_symlink_dotl(struct inode *dir, struct dentry *dentry,
608 const char *symname)
609{
Aneesh Kumar K.V53c06f42011-01-10 13:51:47 -0600610 int err;
611 gid_t gid;
Aneesh Kumar K.Vd28c61f2011-02-28 17:04:08 +0530612 char *name;
613 struct p9_qid qid;
614 struct inode *inode;
615 struct p9_fid *dfid;
616 struct p9_fid *fid = NULL;
617 struct v9fs_session_info *v9ses;
Aneesh Kumar K.V53c06f42011-01-10 13:51:47 -0600618
619 name = (char *) dentry->d_name.name;
620 P9_DPRINTK(P9_DEBUG_VFS, "v9fs_vfs_symlink_dotl : %lu,%s,%s\n",
621 dir->i_ino, name, symname);
622 v9ses = v9fs_inode2v9ses(dir);
623
624 dfid = v9fs_fid_lookup(dentry->d_parent);
625 if (IS_ERR(dfid)) {
626 err = PTR_ERR(dfid);
627 P9_DPRINTK(P9_DEBUG_VFS, "fid lookup failed %d\n", err);
628 return err;
629 }
630
631 gid = v9fs_get_fsgid_for_create(dir);
632
633 /* Server doesn't alter fid on TSYMLINK. Hence no need to clone it. */
634 err = p9_client_symlink(dfid, name, (char *)symname, gid, &qid);
635
636 if (err < 0) {
637 P9_DPRINTK(P9_DEBUG_VFS, "p9_client_symlink failed %d\n", err);
638 goto error;
639 }
640
Aneesh Kumar K.Vd28c61f2011-02-28 17:04:08 +0530641 v9fs_invalidate_inode_attr(dir);
Aneesh Kumar K.V53c06f42011-01-10 13:51:47 -0600642 if (v9ses->cache) {
643 /* Now walk from the parent so we can get an unopened fid. */
644 fid = p9_client_walk(dfid, 1, &name, 1);
645 if (IS_ERR(fid)) {
646 err = PTR_ERR(fid);
647 P9_DPRINTK(P9_DEBUG_VFS, "p9_client_walk failed %d\n",
648 err);
649 fid = NULL;
650 goto error;
651 }
652
653 /* instantiate inode and assign the unopened fid to dentry */
Aneesh Kumar K.Ved80fcf2011-07-06 16:32:31 +0530654 inode = v9fs_get_new_inode_from_fid(v9ses, fid, dir->i_sb);
Aneesh Kumar K.V53c06f42011-01-10 13:51:47 -0600655 if (IS_ERR(inode)) {
656 err = PTR_ERR(inode);
657 P9_DPRINTK(P9_DEBUG_VFS, "inode creation failed %d\n",
658 err);
659 goto error;
660 }
Aneesh Kumar K.V53c06f42011-01-10 13:51:47 -0600661 err = v9fs_fid_add(dentry, fid);
662 if (err < 0)
663 goto error;
Aneesh Kumar K.V5441ae52011-07-25 18:06:32 +0000664 d_instantiate(dentry, inode);
Aneesh Kumar K.V53c06f42011-01-10 13:51:47 -0600665 fid = NULL;
666 } else {
667 /* Not in cached mode. No need to populate inode with stat */
Aneesh Kumar K.V45089142011-07-25 18:06:33 +0000668 inode = v9fs_get_inode(dir->i_sb, S_IFLNK, 0);
Aneesh Kumar K.V53c06f42011-01-10 13:51:47 -0600669 if (IS_ERR(inode)) {
670 err = PTR_ERR(inode);
671 goto error;
672 }
Aneesh Kumar K.V53c06f42011-01-10 13:51:47 -0600673 d_instantiate(dentry, inode);
674 }
675
676error:
677 if (fid)
678 p9_client_clunk(fid);
679
680 return err;
681}
682
683/**
684 * v9fs_vfs_link_dotl - create a hardlink for dotl
685 * @old_dentry: dentry for file to link to
686 * @dir: inode destination for new link
687 * @dentry: dentry for link
688 *
689 */
690
691static int
692v9fs_vfs_link_dotl(struct dentry *old_dentry, struct inode *dir,
693 struct dentry *dentry)
694{
695 int err;
Aneesh Kumar K.V53c06f42011-01-10 13:51:47 -0600696 char *name;
Aneesh Kumar K.V53c06f42011-01-10 13:51:47 -0600697 struct dentry *dir_dentry;
Aneesh Kumar K.Vd28c61f2011-02-28 17:04:08 +0530698 struct p9_fid *dfid, *oldfid;
699 struct v9fs_session_info *v9ses;
Aneesh Kumar K.V53c06f42011-01-10 13:51:47 -0600700
701 P9_DPRINTK(P9_DEBUG_VFS, "dir ino: %lu, old_name: %s, new_name: %s\n",
702 dir->i_ino, old_dentry->d_name.name,
703 dentry->d_name.name);
704
705 v9ses = v9fs_inode2v9ses(dir);
706 dir_dentry = v9fs_dentry_from_dir_inode(dir);
707 dfid = v9fs_fid_lookup(dir_dentry);
708 if (IS_ERR(dfid))
709 return PTR_ERR(dfid);
710
711 oldfid = v9fs_fid_lookup(old_dentry);
712 if (IS_ERR(oldfid))
713 return PTR_ERR(oldfid);
714
715 name = (char *) dentry->d_name.name;
716
717 err = p9_client_link(dfid, oldfid, (char *)dentry->d_name.name);
718
719 if (err < 0) {
720 P9_DPRINTK(P9_DEBUG_VFS, "p9_client_link failed %d\n", err);
721 return err;
722 }
723
Aneesh Kumar K.Vd28c61f2011-02-28 17:04:08 +0530724 v9fs_invalidate_inode_attr(dir);
Aneesh Kumar K.V53c06f42011-01-10 13:51:47 -0600725 if (v9ses->cache == CACHE_LOOSE || v9ses->cache == CACHE_FSCACHE) {
726 /* Get the latest stat info from server. */
727 struct p9_fid *fid;
Aneesh Kumar K.V53c06f42011-01-10 13:51:47 -0600728 fid = v9fs_fid_lookup(old_dentry);
729 if (IS_ERR(fid))
730 return PTR_ERR(fid);
731
Aneesh Kumar K.Vc06c0662011-02-28 17:04:09 +0530732 v9fs_refresh_inode_dotl(fid, old_dentry->d_inode);
Aneesh Kumar K.V53c06f42011-01-10 13:51:47 -0600733 }
Aneesh Kumar K.V20656a42011-02-28 17:03:55 +0530734 ihold(old_dentry->d_inode);
Aneesh Kumar K.V53c06f42011-01-10 13:51:47 -0600735 d_instantiate(dentry, old_dentry->d_inode);
736
737 return err;
738}
739
740/**
741 * v9fs_vfs_mknod_dotl - create a special file
742 * @dir: inode destination for new link
743 * @dentry: dentry for file
744 * @mode: mode for creation
745 * @rdev: device associated with special file
746 *
747 */
748static int
749v9fs_vfs_mknod_dotl(struct inode *dir, struct dentry *dentry, int omode,
750 dev_t rdev)
751{
752 int err;
Aneesh Kumar K.Vd28c61f2011-02-28 17:04:08 +0530753 gid_t gid;
Aneesh Kumar K.V53c06f42011-01-10 13:51:47 -0600754 char *name;
Al Virod3fb6122011-07-23 18:37:50 -0400755 umode_t mode;
Aneesh Kumar K.V53c06f42011-01-10 13:51:47 -0600756 struct v9fs_session_info *v9ses;
757 struct p9_fid *fid = NULL, *dfid = NULL;
758 struct inode *inode;
Aneesh Kumar K.V53c06f42011-01-10 13:51:47 -0600759 struct p9_qid qid;
760 struct dentry *dir_dentry;
761 struct posix_acl *dacl = NULL, *pacl = NULL;
762
763 P9_DPRINTK(P9_DEBUG_VFS,
764 " %lu,%s mode: %x MAJOR: %u MINOR: %u\n", dir->i_ino,
765 dentry->d_name.name, omode, MAJOR(rdev), MINOR(rdev));
766
767 if (!new_valid_dev(rdev))
768 return -EINVAL;
769
770 v9ses = v9fs_inode2v9ses(dir);
771 dir_dentry = v9fs_dentry_from_dir_inode(dir);
772 dfid = v9fs_fid_lookup(dir_dentry);
773 if (IS_ERR(dfid)) {
774 err = PTR_ERR(dfid);
775 P9_DPRINTK(P9_DEBUG_VFS, "fid lookup failed %d\n", err);
776 dfid = NULL;
777 goto error;
778 }
779
780 gid = v9fs_get_fsgid_for_create(dir);
781 mode = omode;
782 /* Update mode based on ACL value */
783 err = v9fs_acl_mode(dir, &mode, &dacl, &pacl);
784 if (err) {
785 P9_DPRINTK(P9_DEBUG_VFS,
786 "Failed to get acl values in mknod %d\n", err);
787 goto error;
788 }
789 name = (char *) dentry->d_name.name;
790
791 err = p9_client_mknod_dotl(dfid, name, mode, rdev, gid, &qid);
792 if (err < 0)
793 goto error;
794
Aneesh Kumar K.Vd28c61f2011-02-28 17:04:08 +0530795 v9fs_invalidate_inode_attr(dir);
Aneesh Kumar K.V53c06f42011-01-10 13:51:47 -0600796 /* instantiate inode and assign the unopened fid to the dentry */
797 if (v9ses->cache == CACHE_LOOSE || v9ses->cache == CACHE_FSCACHE) {
798 fid = p9_client_walk(dfid, 1, &name, 1);
799 if (IS_ERR(fid)) {
800 err = PTR_ERR(fid);
801 P9_DPRINTK(P9_DEBUG_VFS, "p9_client_walk failed %d\n",
802 err);
803 fid = NULL;
804 goto error;
805 }
806
Aneesh Kumar K.Ved80fcf2011-07-06 16:32:31 +0530807 inode = v9fs_get_new_inode_from_fid(v9ses, fid, dir->i_sb);
Aneesh Kumar K.V53c06f42011-01-10 13:51:47 -0600808 if (IS_ERR(inode)) {
809 err = PTR_ERR(inode);
810 P9_DPRINTK(P9_DEBUG_VFS, "inode creation failed %d\n",
811 err);
812 goto error;
813 }
Aneesh Kumar K.V53c06f42011-01-10 13:51:47 -0600814 err = v9fs_fid_add(dentry, fid);
815 if (err < 0)
816 goto error;
Aneesh Kumar K.V5441ae52011-07-25 18:06:32 +0000817 d_instantiate(dentry, inode);
Aneesh Kumar K.V53c06f42011-01-10 13:51:47 -0600818 fid = NULL;
819 } else {
820 /*
821 * Not in cached mode. No need to populate inode with stat.
822 * socket syscall returns a fd, so we need instantiate
823 */
Aneesh Kumar K.V45089142011-07-25 18:06:33 +0000824 inode = v9fs_get_inode(dir->i_sb, mode, rdev);
Aneesh Kumar K.V53c06f42011-01-10 13:51:47 -0600825 if (IS_ERR(inode)) {
826 err = PTR_ERR(inode);
827 goto error;
828 }
Aneesh Kumar K.V53c06f42011-01-10 13:51:47 -0600829 d_instantiate(dentry, inode);
830 }
831 /* Now set the ACL based on the default value */
Al Viro1ec95bf2011-07-23 02:28:13 -0400832 v9fs_set_create_acl(dentry, &dacl, &pacl);
Aneesh Kumar K.V53c06f42011-01-10 13:51:47 -0600833error:
834 if (fid)
835 p9_client_clunk(fid);
Al Viro1ec95bf2011-07-23 02:28:13 -0400836 v9fs_set_create_acl(NULL, &dacl, &pacl);
Aneesh Kumar K.V53c06f42011-01-10 13:51:47 -0600837 return err;
838}
839
Aneesh Kumar K.V53c06f42011-01-10 13:51:47 -0600840/**
841 * v9fs_vfs_follow_link_dotl - follow a symlink path
842 * @dentry: dentry for symlink
843 * @nd: nameidata
844 *
845 */
846
847static void *
848v9fs_vfs_follow_link_dotl(struct dentry *dentry, struct nameidata *nd)
849{
M. Mohan Kumar31b6cea2011-01-08 07:28:46 +0530850 int retval;
851 struct p9_fid *fid;
Aneesh Kumar K.V53c06f42011-01-10 13:51:47 -0600852 char *link = __getname();
M. Mohan Kumar31b6cea2011-01-08 07:28:46 +0530853 char *target;
Aneesh Kumar K.V53c06f42011-01-10 13:51:47 -0600854
M. Mohan Kumar31b6cea2011-01-08 07:28:46 +0530855 P9_DPRINTK(P9_DEBUG_VFS, "%s\n", dentry->d_name.name);
Aneesh Kumar K.V53c06f42011-01-10 13:51:47 -0600856
M. Mohan Kumar31b6cea2011-01-08 07:28:46 +0530857 if (!link) {
Aneesh Kumar K.V53c06f42011-01-10 13:51:47 -0600858 link = ERR_PTR(-ENOMEM);
M. Mohan Kumar31b6cea2011-01-08 07:28:46 +0530859 goto ndset;
Aneesh Kumar K.V53c06f42011-01-10 13:51:47 -0600860 }
M. Mohan Kumar31b6cea2011-01-08 07:28:46 +0530861 fid = v9fs_fid_lookup(dentry);
862 if (IS_ERR(fid)) {
863 __putname(link);
Aneesh Kumar K.V936bb2d2011-03-24 23:04:41 +0530864 link = ERR_CAST(fid);
M. Mohan Kumar31b6cea2011-01-08 07:28:46 +0530865 goto ndset;
866 }
867 retval = p9_client_readlink(fid, &target);
868 if (!retval) {
869 strcpy(link, target);
870 kfree(target);
871 goto ndset;
872 }
873 __putname(link);
874 link = ERR_PTR(retval);
875ndset:
Aneesh Kumar K.V53c06f42011-01-10 13:51:47 -0600876 nd_set_link(nd, link);
Aneesh Kumar K.V53c06f42011-01-10 13:51:47 -0600877 return NULL;
878}
879
Aneesh Kumar K.Vb3cbea02011-02-28 17:04:06 +0530880int v9fs_refresh_inode_dotl(struct p9_fid *fid, struct inode *inode)
881{
882 loff_t i_size;
883 struct p9_stat_dotl *st;
884 struct v9fs_session_info *v9ses;
885
886 v9ses = v9fs_inode2v9ses(inode);
887 st = p9_client_getattr_dotl(fid, P9_STATS_ALL);
888 if (IS_ERR(st))
889 return PTR_ERR(st);
Aneesh Kumar K.V45089142011-07-25 18:06:33 +0000890 /*
891 * Don't update inode if the file type is different
892 */
893 if ((inode->i_mode & S_IFMT) != (st->st_mode & S_IFMT))
894 goto out;
Aneesh Kumar K.Vb3cbea02011-02-28 17:04:06 +0530895
896 spin_lock(&inode->i_lock);
897 /*
898 * We don't want to refresh inode->i_size,
899 * because we may have cached data
900 */
901 i_size = inode->i_size;
902 v9fs_stat2inode_dotl(st, inode);
903 if (v9ses->cache)
904 inode->i_size = i_size;
905 spin_unlock(&inode->i_lock);
Aneesh Kumar K.V45089142011-07-25 18:06:33 +0000906out:
Aneesh Kumar K.Vb3cbea02011-02-28 17:04:06 +0530907 kfree(st);
908 return 0;
909}
910
Aneesh Kumar K.V53c06f42011-01-10 13:51:47 -0600911const struct inode_operations v9fs_dir_inode_operations_dotl = {
912 .create = v9fs_vfs_create_dotl,
913 .lookup = v9fs_vfs_lookup,
914 .link = v9fs_vfs_link_dotl,
915 .symlink = v9fs_vfs_symlink_dotl,
916 .unlink = v9fs_vfs_unlink,
917 .mkdir = v9fs_vfs_mkdir_dotl,
918 .rmdir = v9fs_vfs_rmdir,
919 .mknod = v9fs_vfs_mknod_dotl,
920 .rename = v9fs_vfs_rename,
921 .getattr = v9fs_vfs_getattr_dotl,
922 .setattr = v9fs_vfs_setattr_dotl,
923 .setxattr = generic_setxattr,
924 .getxattr = generic_getxattr,
925 .removexattr = generic_removexattr,
926 .listxattr = v9fs_listxattr,
Christoph Hellwig4e34e712011-07-23 17:37:31 +0200927 .get_acl = v9fs_iop_get_acl,
Aneesh Kumar K.V53c06f42011-01-10 13:51:47 -0600928};
929
930const struct inode_operations v9fs_file_inode_operations_dotl = {
931 .getattr = v9fs_vfs_getattr_dotl,
932 .setattr = v9fs_vfs_setattr_dotl,
933 .setxattr = generic_setxattr,
934 .getxattr = generic_getxattr,
935 .removexattr = generic_removexattr,
936 .listxattr = v9fs_listxattr,
Christoph Hellwig4e34e712011-07-23 17:37:31 +0200937 .get_acl = v9fs_iop_get_acl,
Aneesh Kumar K.V53c06f42011-01-10 13:51:47 -0600938};
939
940const struct inode_operations v9fs_symlink_inode_operations_dotl = {
M. Mohan Kumar31b6cea2011-01-08 07:28:46 +0530941 .readlink = generic_readlink,
Aneesh Kumar K.V53c06f42011-01-10 13:51:47 -0600942 .follow_link = v9fs_vfs_follow_link_dotl,
943 .put_link = v9fs_vfs_put_link,
944 .getattr = v9fs_vfs_getattr_dotl,
945 .setattr = v9fs_vfs_setattr_dotl,
946 .setxattr = generic_setxattr,
947 .getxattr = generic_getxattr,
948 .removexattr = generic_removexattr,
949 .listxattr = v9fs_listxattr,
950};