blob: e3dd2a1e2bfc18e47abae82bce7ee60238527c08 [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
Al Viro1a67aaf2011-07-26 01:52:52 -040051v9fs_vfs_mknod_dotl(struct inode *dir, struct dentry *dentry, umode_t omode,
Aneesh Kumar K.V53c06f42011-01-10 13:51:47 -060052 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
Aneesh Kumar K.Vfd2421f2011-07-11 16:40:59 +000071static int v9fs_test_inode_dotl(struct inode *inode, void *data)
72{
73 struct v9fs_inode *v9inode = V9FS_I(inode);
74 struct p9_stat_dotl *st = (struct p9_stat_dotl *)data;
75
76 /* don't match inode of different type */
77 if ((inode->i_mode & S_IFMT) != (st->st_mode & S_IFMT))
78 return 0;
79
80 if (inode->i_generation != st->st_gen)
81 return 0;
82
83 /* compare qid details */
84 if (memcmp(&v9inode->qid.version,
85 &st->qid.version, sizeof(v9inode->qid.version)))
86 return 0;
87
88 if (v9inode->qid.type != st->qid.type)
89 return 0;
90 return 1;
91}
92
Aneesh Kumar K.Ved80fcf2011-07-06 16:32:31 +053093/* Always get a new inode */
94static int v9fs_test_new_inode_dotl(struct inode *inode, void *data)
95{
96 return 0;
97}
98
Aneesh Kumar K.Vfd2421f2011-07-11 16:40:59 +000099static int v9fs_set_inode_dotl(struct inode *inode, void *data)
100{
101 struct v9fs_inode *v9inode = V9FS_I(inode);
102 struct p9_stat_dotl *st = (struct p9_stat_dotl *)data;
103
104 memcpy(&v9inode->qid, &st->qid, sizeof(st->qid));
105 inode->i_generation = st->st_gen;
106 return 0;
107}
108
Aneesh Kumar K.V5ffc0cb2011-02-28 17:04:01 +0530109static struct inode *v9fs_qid_iget_dotl(struct super_block *sb,
110 struct p9_qid *qid,
111 struct p9_fid *fid,
Aneesh Kumar K.Ved80fcf2011-07-06 16:32:31 +0530112 struct p9_stat_dotl *st,
113 int new)
Aneesh Kumar K.V5ffc0cb2011-02-28 17:04:01 +0530114{
115 int retval;
116 unsigned long i_ino;
117 struct inode *inode;
118 struct v9fs_session_info *v9ses = sb->s_fs_info;
Aneesh Kumar K.Ved80fcf2011-07-06 16:32:31 +0530119 int (*test)(struct inode *, void *);
120
121 if (new)
122 test = v9fs_test_new_inode_dotl;
123 else
124 test = v9fs_test_inode_dotl;
Aneesh Kumar K.V5ffc0cb2011-02-28 17:04:01 +0530125
126 i_ino = v9fs_qid2ino(qid);
Aneesh Kumar K.Ved80fcf2011-07-06 16:32:31 +0530127 inode = iget5_locked(sb, i_ino, test, v9fs_set_inode_dotl, st);
Aneesh Kumar K.V5ffc0cb2011-02-28 17:04:01 +0530128 if (!inode)
129 return ERR_PTR(-ENOMEM);
130 if (!(inode->i_state & I_NEW))
131 return inode;
132 /*
133 * initialize the inode with the stat info
134 * FIXME!! we may need support for stale inodes
135 * later.
136 */
Aneesh Kumar K.Vfd2421f2011-07-11 16:40:59 +0000137 inode->i_ino = i_ino;
Aneesh Kumar K.V45089142011-07-25 18:06:33 +0000138 retval = v9fs_init_inode(v9ses, inode,
139 st->st_mode, new_decode_dev(st->st_rdev));
Aneesh Kumar K.V5ffc0cb2011-02-28 17:04:01 +0530140 if (retval)
141 goto error;
142
143 v9fs_stat2inode_dotl(st, inode);
144#ifdef CONFIG_9P_FSCACHE
Aneesh Kumar K.V5ffc0cb2011-02-28 17:04:01 +0530145 v9fs_cache_inode_get_cookie(inode);
146#endif
147 retval = v9fs_get_acl(inode, fid);
148 if (retval)
149 goto error;
150
151 unlock_new_inode(inode);
152 return inode;
153error:
154 unlock_new_inode(inode);
155 iput(inode);
156 return ERR_PTR(retval);
157
158}
159
Aneesh Kumar K.V53c06f42011-01-10 13:51:47 -0600160struct inode *
Aneesh Kumar K.Va78ce052011-02-28 17:04:02 +0530161v9fs_inode_from_fid_dotl(struct v9fs_session_info *v9ses, struct p9_fid *fid,
Aneesh Kumar K.Ved80fcf2011-07-06 16:32:31 +0530162 struct super_block *sb, int new)
Aneesh Kumar K.V53c06f42011-01-10 13:51:47 -0600163{
Aneesh Kumar K.V53c06f42011-01-10 13:51:47 -0600164 struct p9_stat_dotl *st;
Aneesh Kumar K.V5ffc0cb2011-02-28 17:04:01 +0530165 struct inode *inode = NULL;
Aneesh Kumar K.V53c06f42011-01-10 13:51:47 -0600166
Aneesh Kumar K.Vfd2421f2011-07-11 16:40:59 +0000167 st = p9_client_getattr_dotl(fid, P9_STATS_BASIC | P9_STATS_GEN);
Aneesh Kumar K.V53c06f42011-01-10 13:51:47 -0600168 if (IS_ERR(st))
169 return ERR_CAST(st);
170
Aneesh Kumar K.Ved80fcf2011-07-06 16:32:31 +0530171 inode = v9fs_qid_iget_dotl(sb, &st->qid, fid, st, new);
Aneesh Kumar K.V53c06f42011-01-10 13:51:47 -0600172 kfree(st);
Aneesh Kumar K.V5ffc0cb2011-02-28 17:04:01 +0530173 return inode;
Aneesh Kumar K.V53c06f42011-01-10 13:51:47 -0600174}
175
Aneesh Kumar K.Vf88657c2011-08-03 19:55:32 +0530176struct dotl_openflag_map {
177 int open_flag;
178 int dotl_flag;
179};
180
181static int v9fs_mapped_dotl_flags(int flags)
182{
183 int i;
184 int rflags = 0;
185 struct dotl_openflag_map dotl_oflag_map[] = {
186 { O_CREAT, P9_DOTL_CREATE },
187 { O_EXCL, P9_DOTL_EXCL },
188 { O_NOCTTY, P9_DOTL_NOCTTY },
189 { O_TRUNC, P9_DOTL_TRUNC },
190 { O_APPEND, P9_DOTL_APPEND },
191 { O_NONBLOCK, P9_DOTL_NONBLOCK },
192 { O_DSYNC, P9_DOTL_DSYNC },
193 { FASYNC, P9_DOTL_FASYNC },
194 { O_DIRECT, P9_DOTL_DIRECT },
195 { O_LARGEFILE, P9_DOTL_LARGEFILE },
196 { O_DIRECTORY, P9_DOTL_DIRECTORY },
197 { O_NOFOLLOW, P9_DOTL_NOFOLLOW },
198 { O_NOATIME, P9_DOTL_NOATIME },
199 { O_CLOEXEC, P9_DOTL_CLOEXEC },
200 { O_SYNC, P9_DOTL_SYNC},
201 };
202 for (i = 0; i < ARRAY_SIZE(dotl_oflag_map); i++) {
203 if (flags & dotl_oflag_map[i].open_flag)
204 rflags |= dotl_oflag_map[i].dotl_flag;
205 }
206 return rflags;
207}
208
209/**
210 * v9fs_open_to_dotl_flags- convert Linux specific open flags to
211 * plan 9 open flag.
212 * @flags: flags to convert
213 */
214int v9fs_open_to_dotl_flags(int flags)
215{
216 int rflags = 0;
217
218 /*
219 * We have same bits for P9_DOTL_READONLY, P9_DOTL_WRONLY
220 * and P9_DOTL_NOACCESS
221 */
222 rflags |= flags & O_ACCMODE;
223 rflags |= v9fs_mapped_dotl_flags(flags);
224
225 return rflags;
226}
227
Aneesh Kumar K.V53c06f42011-01-10 13:51:47 -0600228/**
229 * v9fs_vfs_create_dotl - VFS hook to create files for 9P2000.L protocol.
230 * @dir: directory inode that is being created
231 * @dentry: dentry that is being deleted
232 * @mode: create permissions
233 * @nd: path information
234 *
235 */
236
237static int
Al Viro4acdaf22011-07-26 01:42:34 -0400238v9fs_vfs_create_dotl(struct inode *dir, struct dentry *dentry, umode_t omode,
Aneesh Kumar K.V53c06f42011-01-10 13:51:47 -0600239 struct nameidata *nd)
240{
241 int err = 0;
Aneesh Kumar K.V53c06f42011-01-10 13:51:47 -0600242 gid_t gid;
243 int flags;
Al Virod3fb6122011-07-23 18:37:50 -0400244 umode_t mode;
Aneesh Kumar K.V6b39f6d2011-02-28 17:04:03 +0530245 char *name = NULL;
Aneesh Kumar K.V53c06f42011-01-10 13:51:47 -0600246 struct file *filp;
247 struct p9_qid qid;
248 struct inode *inode;
Aneesh Kumar K.V6b39f6d2011-02-28 17:04:03 +0530249 struct p9_fid *fid = NULL;
250 struct v9fs_inode *v9inode;
251 struct p9_fid *dfid, *ofid, *inode_fid;
252 struct v9fs_session_info *v9ses;
Aneesh Kumar K.V53c06f42011-01-10 13:51:47 -0600253 struct posix_acl *pacl = NULL, *dacl = NULL;
254
255 v9ses = v9fs_inode2v9ses(dir);
Al Virodd7dd552011-06-25 21:17:17 -0400256 if (nd)
Al Viro8a5e9292011-06-25 19:15:54 -0400257 flags = nd->intent.open.flags;
Aneesh Kumar K.V53c06f42011-01-10 13:51:47 -0600258 else {
259 /*
260 * create call without LOOKUP_OPEN is due
261 * to mknod of regular files. So use mknod
262 * operation.
263 */
264 return v9fs_vfs_mknod_dotl(dir, dentry, omode, 0);
265 }
266
267 name = (char *) dentry->d_name.name;
Linus Torvalds609eac12012-01-10 15:09:01 -0800268 p9_debug(P9_DEBUG_VFS, "name:%s flags:0x%x mode:0x%hx\n",
Joe Perches5d385152011-11-28 10:40:46 -0800269 name, flags, omode);
Aneesh Kumar K.V53c06f42011-01-10 13:51:47 -0600270
271 dfid = v9fs_fid_lookup(dentry->d_parent);
272 if (IS_ERR(dfid)) {
273 err = PTR_ERR(dfid);
Joe Perches5d385152011-11-28 10:40:46 -0800274 p9_debug(P9_DEBUG_VFS, "fid lookup failed %d\n", err);
Aneesh Kumar K.V53c06f42011-01-10 13:51:47 -0600275 return err;
276 }
277
278 /* clone a fid to use for creation */
279 ofid = p9_client_walk(dfid, 0, NULL, 1);
280 if (IS_ERR(ofid)) {
281 err = PTR_ERR(ofid);
Joe Perches5d385152011-11-28 10:40:46 -0800282 p9_debug(P9_DEBUG_VFS, "p9_client_walk failed %d\n", err);
Aneesh Kumar K.V53c06f42011-01-10 13:51:47 -0600283 return err;
284 }
285
286 gid = v9fs_get_fsgid_for_create(dir);
287
288 mode = omode;
289 /* Update mode based on ACL value */
290 err = v9fs_acl_mode(dir, &mode, &dacl, &pacl);
291 if (err) {
Joe Perches5d385152011-11-28 10:40:46 -0800292 p9_debug(P9_DEBUG_VFS, "Failed to get acl values in creat %d\n",
293 err);
Aneesh Kumar K.V53c06f42011-01-10 13:51:47 -0600294 goto error;
295 }
Aneesh Kumar K.Vf88657c2011-08-03 19:55:32 +0530296 err = p9_client_create_dotl(ofid, name, v9fs_open_to_dotl_flags(flags),
297 mode, gid, &qid);
Aneesh Kumar K.V53c06f42011-01-10 13:51:47 -0600298 if (err < 0) {
Joe Perches5d385152011-11-28 10:40:46 -0800299 p9_debug(P9_DEBUG_VFS, "p9_client_open_dotl failed in creat %d\n",
300 err);
Aneesh Kumar K.V53c06f42011-01-10 13:51:47 -0600301 goto error;
302 }
Aneesh Kumar K.Vd28c61f2011-02-28 17:04:08 +0530303 v9fs_invalidate_inode_attr(dir);
Aneesh Kumar K.V53c06f42011-01-10 13:51:47 -0600304
Aneesh Kumar K.Vaf7542f2011-01-10 14:22:21 -0600305 /* instantiate inode and assign the unopened fid to the dentry */
306 fid = p9_client_walk(dfid, 1, &name, 1);
307 if (IS_ERR(fid)) {
308 err = PTR_ERR(fid);
Joe Perches5d385152011-11-28 10:40:46 -0800309 p9_debug(P9_DEBUG_VFS, "p9_client_walk failed %d\n", err);
Aneesh Kumar K.V53c06f42011-01-10 13:51:47 -0600310 fid = NULL;
Aneesh Kumar K.Vaf7542f2011-01-10 14:22:21 -0600311 goto error;
Aneesh Kumar K.V53c06f42011-01-10 13:51:47 -0600312 }
Aneesh Kumar K.Ved80fcf2011-07-06 16:32:31 +0530313 inode = v9fs_get_new_inode_from_fid(v9ses, fid, dir->i_sb);
Aneesh Kumar K.Vaf7542f2011-01-10 14:22:21 -0600314 if (IS_ERR(inode)) {
315 err = PTR_ERR(inode);
Joe Perches5d385152011-11-28 10:40:46 -0800316 p9_debug(P9_DEBUG_VFS, "inode creation failed %d\n", err);
Aneesh Kumar K.Vaf7542f2011-01-10 14:22:21 -0600317 goto error;
318 }
Aneesh Kumar K.Vaf7542f2011-01-10 14:22:21 -0600319 err = v9fs_fid_add(dentry, fid);
320 if (err < 0)
321 goto error;
Aneesh Kumar K.V5441ae52011-07-25 18:06:32 +0000322 d_instantiate(dentry, inode);
Aneesh Kumar K.Vaf7542f2011-01-10 14:22:21 -0600323
Aneesh Kumar K.V53c06f42011-01-10 13:51:47 -0600324 /* Now set the ACL based on the default value */
Al Viro1ec95bf2011-07-23 02:28:13 -0400325 v9fs_set_create_acl(dentry, &dacl, &pacl);
Aneesh Kumar K.V6b39f6d2011-02-28 17:04:03 +0530326
327 v9inode = V9FS_I(inode);
Aneesh Kumar K.V5a7e0a82011-03-08 16:39:46 +0530328 mutex_lock(&v9inode->v_mutex);
Aneesh Kumar K.V7add6972011-03-08 16:39:49 +0530329 if (v9ses->cache && !v9inode->writeback_fid &&
330 ((flags & O_ACCMODE) != O_RDONLY)) {
Aneesh Kumar K.V3cf387d2011-02-28 17:03:57 +0530331 /*
Aneesh Kumar K.V6b39f6d2011-02-28 17:04:03 +0530332 * clone a fid and add it to writeback_fid
Aneesh Kumar K.V3cf387d2011-02-28 17:03:57 +0530333 * we do it during open time instead of
334 * page dirty time via write_begin/page_mkwrite
335 * because we want write after unlink usecase
336 * to work.
337 */
338 inode_fid = v9fs_writeback_fid(dentry);
339 if (IS_ERR(inode_fid)) {
340 err = PTR_ERR(inode_fid);
Aneesh Kumar K.V5a7e0a82011-03-08 16:39:46 +0530341 mutex_unlock(&v9inode->v_mutex);
Aneesh Kumar K.V398c4f02011-05-20 18:55:51 +0000342 goto err_clunk_old_fid;
Aneesh Kumar K.V3cf387d2011-02-28 17:03:57 +0530343 }
Aneesh Kumar K.V6b39f6d2011-02-28 17:04:03 +0530344 v9inode->writeback_fid = (void *) inode_fid;
Aneesh Kumar K.V3cf387d2011-02-28 17:03:57 +0530345 }
Aneesh Kumar K.V5a7e0a82011-03-08 16:39:46 +0530346 mutex_unlock(&v9inode->v_mutex);
Aneesh Kumar K.Vaf7542f2011-01-10 14:22:21 -0600347 /* Since we are opening a file, assign the open fid to the file */
348 filp = lookup_instantiate_filp(nd, dentry, generic_file_open);
349 if (IS_ERR(filp)) {
Aneesh Kumar K.V398c4f02011-05-20 18:55:51 +0000350 err = PTR_ERR(filp);
351 goto err_clunk_old_fid;
Aneesh Kumar K.Vaf7542f2011-01-10 14:22:21 -0600352 }
353 filp->private_data = ofid;
Aneesh Kumar K.V46848de2011-02-28 17:03:55 +0530354#ifdef CONFIG_9P_FSCACHE
355 if (v9ses->cache)
356 v9fs_cache_inode_set_cookie(inode, filp);
357#endif
Aneesh Kumar K.V53c06f42011-01-10 13:51:47 -0600358 return 0;
359
360error:
Aneesh Kumar K.V53c06f42011-01-10 13:51:47 -0600361 if (fid)
362 p9_client_clunk(fid);
Aneesh Kumar K.V398c4f02011-05-20 18:55:51 +0000363err_clunk_old_fid:
364 if (ofid)
365 p9_client_clunk(ofid);
Al Viro1ec95bf2011-07-23 02:28:13 -0400366 v9fs_set_create_acl(NULL, &dacl, &pacl);
Aneesh Kumar K.V53c06f42011-01-10 13:51:47 -0600367 return err;
368}
369
370/**
371 * v9fs_vfs_mkdir_dotl - VFS mkdir hook to create a directory
372 * @dir: inode that is being unlinked
373 * @dentry: dentry that is being unlinked
374 * @mode: mode for new directory
375 *
376 */
377
378static int v9fs_vfs_mkdir_dotl(struct inode *dir,
Al Viro18bb1db2011-07-26 01:41:39 -0400379 struct dentry *dentry, umode_t omode)
Aneesh Kumar K.V53c06f42011-01-10 13:51:47 -0600380{
381 int err;
382 struct v9fs_session_info *v9ses;
383 struct p9_fid *fid = NULL, *dfid = NULL;
384 gid_t gid;
385 char *name;
Al Virod3fb6122011-07-23 18:37:50 -0400386 umode_t mode;
Aneesh Kumar K.V53c06f42011-01-10 13:51:47 -0600387 struct inode *inode;
388 struct p9_qid qid;
389 struct dentry *dir_dentry;
390 struct posix_acl *dacl = NULL, *pacl = NULL;
391
Joe Perches5d385152011-11-28 10:40:46 -0800392 p9_debug(P9_DEBUG_VFS, "name %s\n", dentry->d_name.name);
Aneesh Kumar K.V53c06f42011-01-10 13:51:47 -0600393 err = 0;
394 v9ses = v9fs_inode2v9ses(dir);
395
396 omode |= S_IFDIR;
397 if (dir->i_mode & S_ISGID)
398 omode |= S_ISGID;
399
Al Viroaf569592012-04-02 20:02:53 -0400400 dir_dentry = dentry->d_parent;
Aneesh Kumar K.V53c06f42011-01-10 13:51:47 -0600401 dfid = v9fs_fid_lookup(dir_dentry);
402 if (IS_ERR(dfid)) {
403 err = PTR_ERR(dfid);
Joe Perches5d385152011-11-28 10:40:46 -0800404 p9_debug(P9_DEBUG_VFS, "fid lookup failed %d\n", err);
Aneesh Kumar K.V53c06f42011-01-10 13:51:47 -0600405 dfid = NULL;
406 goto error;
407 }
408
409 gid = v9fs_get_fsgid_for_create(dir);
410 mode = omode;
411 /* Update mode based on ACL value */
412 err = v9fs_acl_mode(dir, &mode, &dacl, &pacl);
413 if (err) {
Joe Perches5d385152011-11-28 10:40:46 -0800414 p9_debug(P9_DEBUG_VFS, "Failed to get acl values in mkdir %d\n",
415 err);
Aneesh Kumar K.V53c06f42011-01-10 13:51:47 -0600416 goto error;
417 }
418 name = (char *) dentry->d_name.name;
419 err = p9_client_mkdir_dotl(dfid, name, mode, gid, &qid);
420 if (err < 0)
421 goto error;
422
423 /* instantiate inode and assign the unopened fid to the dentry */
424 if (v9ses->cache == CACHE_LOOSE || v9ses->cache == CACHE_FSCACHE) {
425 fid = p9_client_walk(dfid, 1, &name, 1);
426 if (IS_ERR(fid)) {
427 err = PTR_ERR(fid);
Joe Perches5d385152011-11-28 10:40:46 -0800428 p9_debug(P9_DEBUG_VFS, "p9_client_walk failed %d\n",
429 err);
Aneesh Kumar K.V53c06f42011-01-10 13:51:47 -0600430 fid = NULL;
431 goto error;
432 }
433
Aneesh Kumar K.Ved80fcf2011-07-06 16:32:31 +0530434 inode = v9fs_get_new_inode_from_fid(v9ses, fid, dir->i_sb);
Aneesh Kumar K.V53c06f42011-01-10 13:51:47 -0600435 if (IS_ERR(inode)) {
436 err = PTR_ERR(inode);
Joe Perches5d385152011-11-28 10:40:46 -0800437 p9_debug(P9_DEBUG_VFS, "inode creation failed %d\n",
438 err);
Aneesh Kumar K.V53c06f42011-01-10 13:51:47 -0600439 goto error;
440 }
Aneesh Kumar K.V53c06f42011-01-10 13:51:47 -0600441 err = v9fs_fid_add(dentry, fid);
442 if (err < 0)
443 goto error;
Aneesh Kumar K.V5441ae52011-07-25 18:06:32 +0000444 d_instantiate(dentry, inode);
Aneesh Kumar K.V53c06f42011-01-10 13:51:47 -0600445 fid = NULL;
446 } else {
447 /*
448 * Not in cached mode. No need to populate
449 * inode with stat. We need to get an inode
450 * so that we can set the acl with dentry
451 */
Aneesh Kumar K.V45089142011-07-25 18:06:33 +0000452 inode = v9fs_get_inode(dir->i_sb, mode, 0);
Aneesh Kumar K.V53c06f42011-01-10 13:51:47 -0600453 if (IS_ERR(inode)) {
454 err = PTR_ERR(inode);
455 goto error;
456 }
Aneesh Kumar K.V53c06f42011-01-10 13:51:47 -0600457 d_instantiate(dentry, inode);
458 }
459 /* Now set the ACL based on the default value */
Al Viro1ec95bf2011-07-23 02:28:13 -0400460 v9fs_set_create_acl(dentry, &dacl, &pacl);
Aneesh Kumar K.Vb271ec42011-02-28 17:04:05 +0530461 inc_nlink(dir);
Aneesh Kumar K.Vd28c61f2011-02-28 17:04:08 +0530462 v9fs_invalidate_inode_attr(dir);
Aneesh Kumar K.V53c06f42011-01-10 13:51:47 -0600463error:
464 if (fid)
465 p9_client_clunk(fid);
Al Viro1ec95bf2011-07-23 02:28:13 -0400466 v9fs_set_create_acl(NULL, &dacl, &pacl);
Aneesh Kumar K.V53c06f42011-01-10 13:51:47 -0600467 return err;
468}
469
470static int
471v9fs_vfs_getattr_dotl(struct vfsmount *mnt, struct dentry *dentry,
472 struct kstat *stat)
473{
474 int err;
475 struct v9fs_session_info *v9ses;
476 struct p9_fid *fid;
477 struct p9_stat_dotl *st;
478
Joe Perches5d385152011-11-28 10:40:46 -0800479 p9_debug(P9_DEBUG_VFS, "dentry: %p\n", dentry);
Aneesh Kumar K.V53c06f42011-01-10 13:51:47 -0600480 err = -EPERM;
Aneesh Kumar K.V42869c82011-03-08 16:39:50 +0530481 v9ses = v9fs_dentry2v9ses(dentry);
Aneesh Kumar K.Va1211902011-02-28 17:04:01 +0530482 if (v9ses->cache == CACHE_LOOSE || v9ses->cache == CACHE_FSCACHE) {
483 generic_fillattr(dentry->d_inode, stat);
484 return 0;
485 }
Aneesh Kumar K.V53c06f42011-01-10 13:51:47 -0600486 fid = v9fs_fid_lookup(dentry);
487 if (IS_ERR(fid))
488 return PTR_ERR(fid);
489
490 /* Ask for all the fields in stat structure. Server will return
491 * whatever it supports
492 */
493
494 st = p9_client_getattr_dotl(fid, P9_STATS_ALL);
495 if (IS_ERR(st))
496 return PTR_ERR(st);
497
498 v9fs_stat2inode_dotl(st, dentry->d_inode);
499 generic_fillattr(dentry->d_inode, stat);
500 /* Change block size to what the server returned */
501 stat->blksize = st->st_blksize;
502
503 kfree(st);
504 return 0;
505}
506
Aneesh Kumar K.Vf7666192011-12-18 23:03:13 +0530507/*
508 * Attribute flags.
509 */
510#define P9_ATTR_MODE (1 << 0)
511#define P9_ATTR_UID (1 << 1)
512#define P9_ATTR_GID (1 << 2)
513#define P9_ATTR_SIZE (1 << 3)
514#define P9_ATTR_ATIME (1 << 4)
515#define P9_ATTR_MTIME (1 << 5)
516#define P9_ATTR_CTIME (1 << 6)
517#define P9_ATTR_ATIME_SET (1 << 7)
518#define P9_ATTR_MTIME_SET (1 << 8)
519
520struct dotl_iattr_map {
521 int iattr_valid;
522 int p9_iattr_valid;
523};
524
525static int v9fs_mapped_iattr_valid(int iattr_valid)
526{
527 int i;
528 int p9_iattr_valid = 0;
529 struct dotl_iattr_map dotl_iattr_map[] = {
530 { ATTR_MODE, P9_ATTR_MODE },
531 { ATTR_UID, P9_ATTR_UID },
532 { ATTR_GID, P9_ATTR_GID },
533 { ATTR_SIZE, P9_ATTR_SIZE },
534 { ATTR_ATIME, P9_ATTR_ATIME },
535 { ATTR_MTIME, P9_ATTR_MTIME },
536 { ATTR_CTIME, P9_ATTR_CTIME },
537 { ATTR_ATIME_SET, P9_ATTR_ATIME_SET },
538 { ATTR_MTIME_SET, P9_ATTR_MTIME_SET },
539 };
540 for (i = 0; i < ARRAY_SIZE(dotl_iattr_map); i++) {
541 if (iattr_valid & dotl_iattr_map[i].iattr_valid)
542 p9_iattr_valid |= dotl_iattr_map[i].p9_iattr_valid;
543 }
544 return p9_iattr_valid;
545}
546
Aneesh Kumar K.V53c06f42011-01-10 13:51:47 -0600547/**
548 * v9fs_vfs_setattr_dotl - set file metadata
549 * @dentry: file whose metadata to set
550 * @iattr: metadata assignment structure
551 *
552 */
553
554int v9fs_vfs_setattr_dotl(struct dentry *dentry, struct iattr *iattr)
555{
556 int retval;
557 struct v9fs_session_info *v9ses;
558 struct p9_fid *fid;
559 struct p9_iattr_dotl p9attr;
560
Joe Perches5d385152011-11-28 10:40:46 -0800561 p9_debug(P9_DEBUG_VFS, "\n");
Aneesh Kumar K.V53c06f42011-01-10 13:51:47 -0600562
563 retval = inode_change_ok(dentry->d_inode, iattr);
564 if (retval)
565 return retval;
566
Aneesh Kumar K.Vf7666192011-12-18 23:03:13 +0530567 p9attr.valid = v9fs_mapped_iattr_valid(iattr->ia_valid);
Aneesh Kumar K.V53c06f42011-01-10 13:51:47 -0600568 p9attr.mode = iattr->ia_mode;
569 p9attr.uid = iattr->ia_uid;
570 p9attr.gid = iattr->ia_gid;
571 p9attr.size = iattr->ia_size;
572 p9attr.atime_sec = iattr->ia_atime.tv_sec;
573 p9attr.atime_nsec = iattr->ia_atime.tv_nsec;
574 p9attr.mtime_sec = iattr->ia_mtime.tv_sec;
575 p9attr.mtime_nsec = iattr->ia_mtime.tv_nsec;
576
577 retval = -EPERM;
Aneesh Kumar K.V42869c82011-03-08 16:39:50 +0530578 v9ses = v9fs_dentry2v9ses(dentry);
Aneesh Kumar K.V53c06f42011-01-10 13:51:47 -0600579 fid = v9fs_fid_lookup(dentry);
580 if (IS_ERR(fid))
581 return PTR_ERR(fid);
582
Aneesh Kumar K.V3dc54362011-02-28 17:04:11 +0530583 /* Write all dirty data */
584 if (S_ISREG(dentry->d_inode->i_mode))
585 filemap_write_and_wait(dentry->d_inode->i_mapping);
586
Aneesh Kumar K.Vf10fc502011-02-28 17:04:10 +0530587 retval = p9_client_setattr(fid, &p9attr);
588 if (retval < 0)
589 return retval;
Aneesh Kumar K.V53c06f42011-01-10 13:51:47 -0600590
Aneesh Kumar K.V059c1382011-03-08 16:39:48 +0530591 if ((iattr->ia_valid & ATTR_SIZE) &&
592 iattr->ia_size != i_size_read(dentry->d_inode))
593 truncate_setsize(dentry->d_inode, iattr->ia_size);
594
595 v9fs_invalidate_inode_attr(dentry->d_inode);
Aneesh Kumar K.V53c06f42011-01-10 13:51:47 -0600596 setattr_copy(dentry->d_inode, iattr);
597 mark_inode_dirty(dentry->d_inode);
598 if (iattr->ia_valid & ATTR_MODE) {
599 /* We also want to update ACL when we update mode bits */
600 retval = v9fs_acl_chmod(dentry);
601 if (retval < 0)
602 return retval;
603 }
604 return 0;
605}
606
607/**
608 * v9fs_stat2inode_dotl - populate an inode structure with stat info
609 * @stat: stat structure
610 * @inode: inode to populate
611 * @sb: superblock of filesystem
612 *
613 */
614
615void
616v9fs_stat2inode_dotl(struct p9_stat_dotl *stat, struct inode *inode)
617{
Al Viro3eda0de2011-07-26 02:53:22 -0400618 umode_t mode;
Aneesh Kumar K.Vb3cbea02011-02-28 17:04:06 +0530619 struct v9fs_inode *v9inode = V9FS_I(inode);
Aneesh Kumar K.V53c06f42011-01-10 13:51:47 -0600620
621 if ((stat->st_result_mask & P9_STATS_BASIC) == P9_STATS_BASIC) {
622 inode->i_atime.tv_sec = stat->st_atime_sec;
623 inode->i_atime.tv_nsec = stat->st_atime_nsec;
624 inode->i_mtime.tv_sec = stat->st_mtime_sec;
625 inode->i_mtime.tv_nsec = stat->st_mtime_nsec;
626 inode->i_ctime.tv_sec = stat->st_ctime_sec;
627 inode->i_ctime.tv_nsec = stat->st_ctime_nsec;
628 inode->i_uid = stat->st_uid;
629 inode->i_gid = stat->st_gid;
Miklos Szeredibfe86842011-10-28 14:13:29 +0200630 set_nlink(inode, stat->st_nlink);
Aneesh Kumar K.V53c06f42011-01-10 13:51:47 -0600631
Aneesh Kumar K.V45089142011-07-25 18:06:33 +0000632 mode = stat->st_mode & S_IALLUGO;
633 mode |= inode->i_mode & ~S_IALLUGO;
634 inode->i_mode = mode;
Aneesh Kumar K.V53c06f42011-01-10 13:51:47 -0600635
636 i_size_write(inode, stat->st_size);
637 inode->i_blocks = stat->st_blocks;
638 } else {
639 if (stat->st_result_mask & P9_STATS_ATIME) {
640 inode->i_atime.tv_sec = stat->st_atime_sec;
641 inode->i_atime.tv_nsec = stat->st_atime_nsec;
642 }
643 if (stat->st_result_mask & P9_STATS_MTIME) {
644 inode->i_mtime.tv_sec = stat->st_mtime_sec;
645 inode->i_mtime.tv_nsec = stat->st_mtime_nsec;
646 }
647 if (stat->st_result_mask & P9_STATS_CTIME) {
648 inode->i_ctime.tv_sec = stat->st_ctime_sec;
649 inode->i_ctime.tv_nsec = stat->st_ctime_nsec;
650 }
651 if (stat->st_result_mask & P9_STATS_UID)
652 inode->i_uid = stat->st_uid;
653 if (stat->st_result_mask & P9_STATS_GID)
654 inode->i_gid = stat->st_gid;
655 if (stat->st_result_mask & P9_STATS_NLINK)
Miklos Szeredibfe86842011-10-28 14:13:29 +0200656 set_nlink(inode, stat->st_nlink);
Aneesh Kumar K.V53c06f42011-01-10 13:51:47 -0600657 if (stat->st_result_mask & P9_STATS_MODE) {
658 inode->i_mode = stat->st_mode;
659 if ((S_ISBLK(inode->i_mode)) ||
660 (S_ISCHR(inode->i_mode)))
661 init_special_inode(inode, inode->i_mode,
662 inode->i_rdev);
663 }
664 if (stat->st_result_mask & P9_STATS_RDEV)
665 inode->i_rdev = new_decode_dev(stat->st_rdev);
666 if (stat->st_result_mask & P9_STATS_SIZE)
667 i_size_write(inode, stat->st_size);
668 if (stat->st_result_mask & P9_STATS_BLOCKS)
669 inode->i_blocks = stat->st_blocks;
670 }
671 if (stat->st_result_mask & P9_STATS_GEN)
Aneesh Kumar K.Vfd2421f2011-07-11 16:40:59 +0000672 inode->i_generation = stat->st_gen;
Aneesh Kumar K.V53c06f42011-01-10 13:51:47 -0600673
674 /* Currently we don't support P9_STATS_BTIME and P9_STATS_DATA_VERSION
675 * because the inode structure does not have fields for them.
676 */
Aneesh Kumar K.Vb3cbea02011-02-28 17:04:06 +0530677 v9inode->cache_validity &= ~V9FS_INO_INVALID_ATTR;
Aneesh Kumar K.V53c06f42011-01-10 13:51:47 -0600678}
679
680static int
681v9fs_vfs_symlink_dotl(struct inode *dir, struct dentry *dentry,
682 const char *symname)
683{
Aneesh Kumar K.V53c06f42011-01-10 13:51:47 -0600684 int err;
685 gid_t gid;
Aneesh Kumar K.Vd28c61f2011-02-28 17:04:08 +0530686 char *name;
687 struct p9_qid qid;
688 struct inode *inode;
689 struct p9_fid *dfid;
690 struct p9_fid *fid = NULL;
691 struct v9fs_session_info *v9ses;
Aneesh Kumar K.V53c06f42011-01-10 13:51:47 -0600692
693 name = (char *) dentry->d_name.name;
Joe Perches5d385152011-11-28 10:40:46 -0800694 p9_debug(P9_DEBUG_VFS, "%lu,%s,%s\n", dir->i_ino, name, symname);
Aneesh Kumar K.V53c06f42011-01-10 13:51:47 -0600695 v9ses = v9fs_inode2v9ses(dir);
696
697 dfid = v9fs_fid_lookup(dentry->d_parent);
698 if (IS_ERR(dfid)) {
699 err = PTR_ERR(dfid);
Joe Perches5d385152011-11-28 10:40:46 -0800700 p9_debug(P9_DEBUG_VFS, "fid lookup failed %d\n", err);
Aneesh Kumar K.V53c06f42011-01-10 13:51:47 -0600701 return err;
702 }
703
704 gid = v9fs_get_fsgid_for_create(dir);
705
706 /* Server doesn't alter fid on TSYMLINK. Hence no need to clone it. */
707 err = p9_client_symlink(dfid, name, (char *)symname, gid, &qid);
708
709 if (err < 0) {
Joe Perches5d385152011-11-28 10:40:46 -0800710 p9_debug(P9_DEBUG_VFS, "p9_client_symlink failed %d\n", err);
Aneesh Kumar K.V53c06f42011-01-10 13:51:47 -0600711 goto error;
712 }
713
Aneesh Kumar K.Vd28c61f2011-02-28 17:04:08 +0530714 v9fs_invalidate_inode_attr(dir);
Aneesh Kumar K.V53c06f42011-01-10 13:51:47 -0600715 if (v9ses->cache) {
716 /* Now walk from the parent so we can get an unopened fid. */
717 fid = p9_client_walk(dfid, 1, &name, 1);
718 if (IS_ERR(fid)) {
719 err = PTR_ERR(fid);
Joe Perches5d385152011-11-28 10:40:46 -0800720 p9_debug(P9_DEBUG_VFS, "p9_client_walk failed %d\n",
721 err);
Aneesh Kumar K.V53c06f42011-01-10 13:51:47 -0600722 fid = NULL;
723 goto error;
724 }
725
726 /* instantiate inode and assign the unopened fid to dentry */
Aneesh Kumar K.Ved80fcf2011-07-06 16:32:31 +0530727 inode = v9fs_get_new_inode_from_fid(v9ses, fid, dir->i_sb);
Aneesh Kumar K.V53c06f42011-01-10 13:51:47 -0600728 if (IS_ERR(inode)) {
729 err = PTR_ERR(inode);
Joe Perches5d385152011-11-28 10:40:46 -0800730 p9_debug(P9_DEBUG_VFS, "inode creation failed %d\n",
731 err);
Aneesh Kumar K.V53c06f42011-01-10 13:51:47 -0600732 goto error;
733 }
Aneesh Kumar K.V53c06f42011-01-10 13:51:47 -0600734 err = v9fs_fid_add(dentry, fid);
735 if (err < 0)
736 goto error;
Aneesh Kumar K.V5441ae52011-07-25 18:06:32 +0000737 d_instantiate(dentry, inode);
Aneesh Kumar K.V53c06f42011-01-10 13:51:47 -0600738 fid = NULL;
739 } else {
740 /* Not in cached mode. No need to populate inode with stat */
Aneesh Kumar K.V45089142011-07-25 18:06:33 +0000741 inode = v9fs_get_inode(dir->i_sb, S_IFLNK, 0);
Aneesh Kumar K.V53c06f42011-01-10 13:51:47 -0600742 if (IS_ERR(inode)) {
743 err = PTR_ERR(inode);
744 goto error;
745 }
Aneesh Kumar K.V53c06f42011-01-10 13:51:47 -0600746 d_instantiate(dentry, inode);
747 }
748
749error:
750 if (fid)
751 p9_client_clunk(fid);
752
753 return err;
754}
755
756/**
757 * v9fs_vfs_link_dotl - create a hardlink for dotl
758 * @old_dentry: dentry for file to link to
759 * @dir: inode destination for new link
760 * @dentry: dentry for link
761 *
762 */
763
764static int
765v9fs_vfs_link_dotl(struct dentry *old_dentry, struct inode *dir,
766 struct dentry *dentry)
767{
768 int err;
Aneesh Kumar K.V53c06f42011-01-10 13:51:47 -0600769 char *name;
Aneesh Kumar K.V53c06f42011-01-10 13:51:47 -0600770 struct dentry *dir_dentry;
Aneesh Kumar K.Vd28c61f2011-02-28 17:04:08 +0530771 struct p9_fid *dfid, *oldfid;
772 struct v9fs_session_info *v9ses;
Aneesh Kumar K.V53c06f42011-01-10 13:51:47 -0600773
Joe Perches5d385152011-11-28 10:40:46 -0800774 p9_debug(P9_DEBUG_VFS, "dir ino: %lu, old_name: %s, new_name: %s\n",
775 dir->i_ino, old_dentry->d_name.name, dentry->d_name.name);
Aneesh Kumar K.V53c06f42011-01-10 13:51:47 -0600776
777 v9ses = v9fs_inode2v9ses(dir);
Al Viroaf569592012-04-02 20:02:53 -0400778 dir_dentry = dentry->d_parent;
Aneesh Kumar K.V53c06f42011-01-10 13:51:47 -0600779 dfid = v9fs_fid_lookup(dir_dentry);
780 if (IS_ERR(dfid))
781 return PTR_ERR(dfid);
782
783 oldfid = v9fs_fid_lookup(old_dentry);
784 if (IS_ERR(oldfid))
785 return PTR_ERR(oldfid);
786
787 name = (char *) dentry->d_name.name;
788
789 err = p9_client_link(dfid, oldfid, (char *)dentry->d_name.name);
790
791 if (err < 0) {
Joe Perches5d385152011-11-28 10:40:46 -0800792 p9_debug(P9_DEBUG_VFS, "p9_client_link failed %d\n", err);
Aneesh Kumar K.V53c06f42011-01-10 13:51:47 -0600793 return err;
794 }
795
Aneesh Kumar K.Vd28c61f2011-02-28 17:04:08 +0530796 v9fs_invalidate_inode_attr(dir);
Aneesh Kumar K.V53c06f42011-01-10 13:51:47 -0600797 if (v9ses->cache == CACHE_LOOSE || v9ses->cache == CACHE_FSCACHE) {
798 /* Get the latest stat info from server. */
799 struct p9_fid *fid;
Aneesh Kumar K.V53c06f42011-01-10 13:51:47 -0600800 fid = v9fs_fid_lookup(old_dentry);
801 if (IS_ERR(fid))
802 return PTR_ERR(fid);
803
Aneesh Kumar K.Vc06c0662011-02-28 17:04:09 +0530804 v9fs_refresh_inode_dotl(fid, old_dentry->d_inode);
Aneesh Kumar K.V53c06f42011-01-10 13:51:47 -0600805 }
Aneesh Kumar K.V20656a42011-02-28 17:03:55 +0530806 ihold(old_dentry->d_inode);
Aneesh Kumar K.V53c06f42011-01-10 13:51:47 -0600807 d_instantiate(dentry, old_dentry->d_inode);
808
809 return err;
810}
811
812/**
813 * v9fs_vfs_mknod_dotl - create a special file
814 * @dir: inode destination for new link
815 * @dentry: dentry for file
816 * @mode: mode for creation
817 * @rdev: device associated with special file
818 *
819 */
820static int
Al Viro1a67aaf2011-07-26 01:52:52 -0400821v9fs_vfs_mknod_dotl(struct inode *dir, struct dentry *dentry, umode_t omode,
Aneesh Kumar K.V53c06f42011-01-10 13:51:47 -0600822 dev_t rdev)
823{
824 int err;
Aneesh Kumar K.Vd28c61f2011-02-28 17:04:08 +0530825 gid_t gid;
Aneesh Kumar K.V53c06f42011-01-10 13:51:47 -0600826 char *name;
Al Virod3fb6122011-07-23 18:37:50 -0400827 umode_t mode;
Aneesh Kumar K.V53c06f42011-01-10 13:51:47 -0600828 struct v9fs_session_info *v9ses;
829 struct p9_fid *fid = NULL, *dfid = NULL;
830 struct inode *inode;
Aneesh Kumar K.V53c06f42011-01-10 13:51:47 -0600831 struct p9_qid qid;
832 struct dentry *dir_dentry;
833 struct posix_acl *dacl = NULL, *pacl = NULL;
834
Linus Torvalds609eac12012-01-10 15:09:01 -0800835 p9_debug(P9_DEBUG_VFS, " %lu,%s mode: %hx MAJOR: %u MINOR: %u\n",
Joe Perches5d385152011-11-28 10:40:46 -0800836 dir->i_ino, dentry->d_name.name, omode,
837 MAJOR(rdev), MINOR(rdev));
Aneesh Kumar K.V53c06f42011-01-10 13:51:47 -0600838
839 if (!new_valid_dev(rdev))
840 return -EINVAL;
841
842 v9ses = v9fs_inode2v9ses(dir);
Al Viroaf569592012-04-02 20:02:53 -0400843 dir_dentry = dentry->d_parent;
Aneesh Kumar K.V53c06f42011-01-10 13:51:47 -0600844 dfid = v9fs_fid_lookup(dir_dentry);
845 if (IS_ERR(dfid)) {
846 err = PTR_ERR(dfid);
Joe Perches5d385152011-11-28 10:40:46 -0800847 p9_debug(P9_DEBUG_VFS, "fid lookup failed %d\n", err);
Aneesh Kumar K.V53c06f42011-01-10 13:51:47 -0600848 dfid = NULL;
849 goto error;
850 }
851
852 gid = v9fs_get_fsgid_for_create(dir);
853 mode = omode;
854 /* Update mode based on ACL value */
855 err = v9fs_acl_mode(dir, &mode, &dacl, &pacl);
856 if (err) {
Joe Perches5d385152011-11-28 10:40:46 -0800857 p9_debug(P9_DEBUG_VFS, "Failed to get acl values in mknod %d\n",
858 err);
Aneesh Kumar K.V53c06f42011-01-10 13:51:47 -0600859 goto error;
860 }
861 name = (char *) dentry->d_name.name;
862
863 err = p9_client_mknod_dotl(dfid, name, mode, rdev, gid, &qid);
864 if (err < 0)
865 goto error;
866
Aneesh Kumar K.Vd28c61f2011-02-28 17:04:08 +0530867 v9fs_invalidate_inode_attr(dir);
Aneesh Kumar K.V53c06f42011-01-10 13:51:47 -0600868 /* instantiate inode and assign the unopened fid to the dentry */
869 if (v9ses->cache == CACHE_LOOSE || v9ses->cache == CACHE_FSCACHE) {
870 fid = p9_client_walk(dfid, 1, &name, 1);
871 if (IS_ERR(fid)) {
872 err = PTR_ERR(fid);
Joe Perches5d385152011-11-28 10:40:46 -0800873 p9_debug(P9_DEBUG_VFS, "p9_client_walk failed %d\n",
874 err);
Aneesh Kumar K.V53c06f42011-01-10 13:51:47 -0600875 fid = NULL;
876 goto error;
877 }
878
Aneesh Kumar K.Ved80fcf2011-07-06 16:32:31 +0530879 inode = v9fs_get_new_inode_from_fid(v9ses, fid, dir->i_sb);
Aneesh Kumar K.V53c06f42011-01-10 13:51:47 -0600880 if (IS_ERR(inode)) {
881 err = PTR_ERR(inode);
Joe Perches5d385152011-11-28 10:40:46 -0800882 p9_debug(P9_DEBUG_VFS, "inode creation failed %d\n",
883 err);
Aneesh Kumar K.V53c06f42011-01-10 13:51:47 -0600884 goto error;
885 }
Aneesh Kumar K.V53c06f42011-01-10 13:51:47 -0600886 err = v9fs_fid_add(dentry, fid);
887 if (err < 0)
888 goto error;
Aneesh Kumar K.V5441ae52011-07-25 18:06:32 +0000889 d_instantiate(dentry, inode);
Aneesh Kumar K.V53c06f42011-01-10 13:51:47 -0600890 fid = NULL;
891 } else {
892 /*
893 * Not in cached mode. No need to populate inode with stat.
894 * socket syscall returns a fd, so we need instantiate
895 */
Aneesh Kumar K.V45089142011-07-25 18:06:33 +0000896 inode = v9fs_get_inode(dir->i_sb, mode, rdev);
Aneesh Kumar K.V53c06f42011-01-10 13:51:47 -0600897 if (IS_ERR(inode)) {
898 err = PTR_ERR(inode);
899 goto error;
900 }
Aneesh Kumar K.V53c06f42011-01-10 13:51:47 -0600901 d_instantiate(dentry, inode);
902 }
903 /* Now set the ACL based on the default value */
Al Viro1ec95bf2011-07-23 02:28:13 -0400904 v9fs_set_create_acl(dentry, &dacl, &pacl);
Aneesh Kumar K.V53c06f42011-01-10 13:51:47 -0600905error:
906 if (fid)
907 p9_client_clunk(fid);
Al Viro1ec95bf2011-07-23 02:28:13 -0400908 v9fs_set_create_acl(NULL, &dacl, &pacl);
Aneesh Kumar K.V53c06f42011-01-10 13:51:47 -0600909 return err;
910}
911
Aneesh Kumar K.V53c06f42011-01-10 13:51:47 -0600912/**
913 * v9fs_vfs_follow_link_dotl - follow a symlink path
914 * @dentry: dentry for symlink
915 * @nd: nameidata
916 *
917 */
918
919static void *
920v9fs_vfs_follow_link_dotl(struct dentry *dentry, struct nameidata *nd)
921{
M. Mohan Kumar31b6cea2011-01-08 07:28:46 +0530922 int retval;
923 struct p9_fid *fid;
Aneesh Kumar K.V53c06f42011-01-10 13:51:47 -0600924 char *link = __getname();
M. Mohan Kumar31b6cea2011-01-08 07:28:46 +0530925 char *target;
Aneesh Kumar K.V53c06f42011-01-10 13:51:47 -0600926
Joe Perches5d385152011-11-28 10:40:46 -0800927 p9_debug(P9_DEBUG_VFS, "%s\n", dentry->d_name.name);
Aneesh Kumar K.V53c06f42011-01-10 13:51:47 -0600928
M. Mohan Kumar31b6cea2011-01-08 07:28:46 +0530929 if (!link) {
Aneesh Kumar K.V53c06f42011-01-10 13:51:47 -0600930 link = ERR_PTR(-ENOMEM);
M. Mohan Kumar31b6cea2011-01-08 07:28:46 +0530931 goto ndset;
Aneesh Kumar K.V53c06f42011-01-10 13:51:47 -0600932 }
M. Mohan Kumar31b6cea2011-01-08 07:28:46 +0530933 fid = v9fs_fid_lookup(dentry);
934 if (IS_ERR(fid)) {
935 __putname(link);
Aneesh Kumar K.V936bb2d2011-03-24 23:04:41 +0530936 link = ERR_CAST(fid);
M. Mohan Kumar31b6cea2011-01-08 07:28:46 +0530937 goto ndset;
938 }
939 retval = p9_client_readlink(fid, &target);
940 if (!retval) {
941 strcpy(link, target);
942 kfree(target);
943 goto ndset;
944 }
945 __putname(link);
946 link = ERR_PTR(retval);
947ndset:
Aneesh Kumar K.V53c06f42011-01-10 13:51:47 -0600948 nd_set_link(nd, link);
Aneesh Kumar K.V53c06f42011-01-10 13:51:47 -0600949 return NULL;
950}
951
Aneesh Kumar K.Vb3cbea02011-02-28 17:04:06 +0530952int v9fs_refresh_inode_dotl(struct p9_fid *fid, struct inode *inode)
953{
954 loff_t i_size;
955 struct p9_stat_dotl *st;
956 struct v9fs_session_info *v9ses;
957
958 v9ses = v9fs_inode2v9ses(inode);
959 st = p9_client_getattr_dotl(fid, P9_STATS_ALL);
960 if (IS_ERR(st))
961 return PTR_ERR(st);
Aneesh Kumar K.V45089142011-07-25 18:06:33 +0000962 /*
963 * Don't update inode if the file type is different
964 */
965 if ((inode->i_mode & S_IFMT) != (st->st_mode & S_IFMT))
966 goto out;
Aneesh Kumar K.Vb3cbea02011-02-28 17:04:06 +0530967
968 spin_lock(&inode->i_lock);
969 /*
970 * We don't want to refresh inode->i_size,
971 * because we may have cached data
972 */
973 i_size = inode->i_size;
974 v9fs_stat2inode_dotl(st, inode);
975 if (v9ses->cache)
976 inode->i_size = i_size;
977 spin_unlock(&inode->i_lock);
Aneesh Kumar K.V45089142011-07-25 18:06:33 +0000978out:
Aneesh Kumar K.Vb3cbea02011-02-28 17:04:06 +0530979 kfree(st);
980 return 0;
981}
982
Aneesh Kumar K.V53c06f42011-01-10 13:51:47 -0600983const struct inode_operations v9fs_dir_inode_operations_dotl = {
984 .create = v9fs_vfs_create_dotl,
985 .lookup = v9fs_vfs_lookup,
986 .link = v9fs_vfs_link_dotl,
987 .symlink = v9fs_vfs_symlink_dotl,
988 .unlink = v9fs_vfs_unlink,
989 .mkdir = v9fs_vfs_mkdir_dotl,
990 .rmdir = v9fs_vfs_rmdir,
991 .mknod = v9fs_vfs_mknod_dotl,
992 .rename = v9fs_vfs_rename,
993 .getattr = v9fs_vfs_getattr_dotl,
994 .setattr = v9fs_vfs_setattr_dotl,
995 .setxattr = generic_setxattr,
996 .getxattr = generic_getxattr,
997 .removexattr = generic_removexattr,
998 .listxattr = v9fs_listxattr,
Christoph Hellwig4e34e712011-07-23 17:37:31 +0200999 .get_acl = v9fs_iop_get_acl,
Aneesh Kumar K.V53c06f42011-01-10 13:51:47 -06001000};
1001
1002const struct inode_operations v9fs_file_inode_operations_dotl = {
1003 .getattr = v9fs_vfs_getattr_dotl,
1004 .setattr = v9fs_vfs_setattr_dotl,
1005 .setxattr = generic_setxattr,
1006 .getxattr = generic_getxattr,
1007 .removexattr = generic_removexattr,
1008 .listxattr = v9fs_listxattr,
Christoph Hellwig4e34e712011-07-23 17:37:31 +02001009 .get_acl = v9fs_iop_get_acl,
Aneesh Kumar K.V53c06f42011-01-10 13:51:47 -06001010};
1011
1012const struct inode_operations v9fs_symlink_inode_operations_dotl = {
M. Mohan Kumar31b6cea2011-01-08 07:28:46 +05301013 .readlink = generic_readlink,
Aneesh Kumar K.V53c06f42011-01-10 13:51:47 -06001014 .follow_link = v9fs_vfs_follow_link_dotl,
1015 .put_link = v9fs_vfs_put_link,
1016 .getattr = v9fs_vfs_getattr_dotl,
1017 .setattr = v9fs_vfs_setattr_dotl,
1018 .setxattr = generic_setxattr,
1019 .getxattr = generic_getxattr,
1020 .removexattr = generic_removexattr,
1021 .listxattr = v9fs_listxattr,
1022};