blob: edd41d918e6cf67d08b09271662db109f62e6c38 [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 },
Aneesh Kumar K.Vf88657c2011-08-03 19:55:32 +0530189 { O_APPEND, P9_DOTL_APPEND },
190 { O_NONBLOCK, P9_DOTL_NONBLOCK },
191 { O_DSYNC, P9_DOTL_DSYNC },
192 { FASYNC, P9_DOTL_FASYNC },
193 { O_DIRECT, P9_DOTL_DIRECT },
194 { O_LARGEFILE, P9_DOTL_LARGEFILE },
195 { O_DIRECTORY, P9_DOTL_DIRECTORY },
196 { O_NOFOLLOW, P9_DOTL_NOFOLLOW },
197 { O_NOATIME, P9_DOTL_NOATIME },
198 { O_CLOEXEC, P9_DOTL_CLOEXEC },
199 { O_SYNC, P9_DOTL_SYNC},
200 };
201 for (i = 0; i < ARRAY_SIZE(dotl_oflag_map); i++) {
202 if (flags & dotl_oflag_map[i].open_flag)
203 rflags |= dotl_oflag_map[i].dotl_flag;
204 }
205 return rflags;
206}
207
208/**
209 * v9fs_open_to_dotl_flags- convert Linux specific open flags to
210 * plan 9 open flag.
211 * @flags: flags to convert
212 */
213int v9fs_open_to_dotl_flags(int flags)
214{
215 int rflags = 0;
216
217 /*
218 * We have same bits for P9_DOTL_READONLY, P9_DOTL_WRONLY
219 * and P9_DOTL_NOACCESS
220 */
221 rflags |= flags & O_ACCMODE;
222 rflags |= v9fs_mapped_dotl_flags(flags);
223
224 return rflags;
225}
226
Aneesh Kumar K.V53c06f42011-01-10 13:51:47 -0600227/**
228 * v9fs_vfs_create_dotl - VFS hook to create files for 9P2000.L protocol.
229 * @dir: directory inode that is being created
230 * @dentry: dentry that is being deleted
231 * @mode: create permissions
Aneesh Kumar K.V53c06f42011-01-10 13:51:47 -0600232 *
233 */
234
235static int
Al Viro4acdaf22011-07-26 01:42:34 -0400236v9fs_vfs_create_dotl(struct inode *dir, struct dentry *dentry, umode_t omode,
Al Viroebfc3b42012-06-10 18:05:36 -0400237 bool excl)
Aneesh Kumar K.V53c06f42011-01-10 13:51:47 -0600238{
Miklos Szeredie43ae792012-06-05 15:10:26 +0200239 return v9fs_vfs_mknod_dotl(dir, dentry, omode, 0);
240}
241
Al Virod9585272012-06-22 12:39:14 +0400242static int
Miklos Szeredie43ae792012-06-05 15:10:26 +0200243v9fs_vfs_atomic_open_dotl(struct inode *dir, struct dentry *dentry,
Al Viro30d90492012-06-22 12:40:19 +0400244 struct file *file, unsigned flags, umode_t omode,
Al Viro47237682012-06-10 05:01:45 -0400245 int *opened)
Miklos Szeredie43ae792012-06-05 15:10:26 +0200246{
Aneesh Kumar K.V53c06f42011-01-10 13:51:47 -0600247 int err = 0;
Aneesh Kumar K.V53c06f42011-01-10 13:51:47 -0600248 gid_t gid;
Al Virod3fb6122011-07-23 18:37:50 -0400249 umode_t mode;
Aneesh Kumar K.V6b39f6d2011-02-28 17:04:03 +0530250 char *name = NULL;
Aneesh Kumar K.V53c06f42011-01-10 13:51:47 -0600251 struct p9_qid qid;
252 struct inode *inode;
Aneesh Kumar K.V6b39f6d2011-02-28 17:04:03 +0530253 struct p9_fid *fid = NULL;
254 struct v9fs_inode *v9inode;
255 struct p9_fid *dfid, *ofid, *inode_fid;
256 struct v9fs_session_info *v9ses;
Aneesh Kumar K.V53c06f42011-01-10 13:51:47 -0600257 struct posix_acl *pacl = NULL, *dacl = NULL;
Miklos Szeredie43ae792012-06-05 15:10:26 +0200258 struct dentry *res = NULL;
259
260 if (d_unhashed(dentry)) {
Al Viro00cd8dd2012-06-10 17:13:09 -0400261 res = v9fs_vfs_lookup(dir, dentry, 0);
Miklos Szeredie43ae792012-06-05 15:10:26 +0200262 if (IS_ERR(res))
Al Virod9585272012-06-22 12:39:14 +0400263 return PTR_ERR(res);
Miklos Szeredie43ae792012-06-05 15:10:26 +0200264
265 if (res)
266 dentry = res;
267 }
268
269 /* Only creates */
Al Viroe45198a2012-06-10 06:48:09 -0400270 if (!(flags & O_CREAT) || dentry->d_inode)
271 return finish_no_open(file, res);
Aneesh Kumar K.V53c06f42011-01-10 13:51:47 -0600272
273 v9ses = v9fs_inode2v9ses(dir);
Aneesh Kumar K.V53c06f42011-01-10 13:51:47 -0600274
275 name = (char *) dentry->d_name.name;
Linus Torvalds609eac12012-01-10 15:09:01 -0800276 p9_debug(P9_DEBUG_VFS, "name:%s flags:0x%x mode:0x%hx\n",
Joe Perches5d385152011-11-28 10:40:46 -0800277 name, flags, omode);
Aneesh Kumar K.V53c06f42011-01-10 13:51:47 -0600278
279 dfid = v9fs_fid_lookup(dentry->d_parent);
280 if (IS_ERR(dfid)) {
281 err = PTR_ERR(dfid);
Joe Perches5d385152011-11-28 10:40:46 -0800282 p9_debug(P9_DEBUG_VFS, "fid lookup failed %d\n", err);
Al Virod9585272012-06-22 12:39:14 +0400283 goto out;
Aneesh Kumar K.V53c06f42011-01-10 13:51:47 -0600284 }
285
286 /* clone a fid to use for creation */
287 ofid = p9_client_walk(dfid, 0, NULL, 1);
288 if (IS_ERR(ofid)) {
289 err = PTR_ERR(ofid);
Joe Perches5d385152011-11-28 10:40:46 -0800290 p9_debug(P9_DEBUG_VFS, "p9_client_walk failed %d\n", err);
Al Virod9585272012-06-22 12:39:14 +0400291 goto out;
Aneesh Kumar K.V53c06f42011-01-10 13:51:47 -0600292 }
293
294 gid = v9fs_get_fsgid_for_create(dir);
295
296 mode = omode;
297 /* Update mode based on ACL value */
298 err = v9fs_acl_mode(dir, &mode, &dacl, &pacl);
299 if (err) {
Joe Perches5d385152011-11-28 10:40:46 -0800300 p9_debug(P9_DEBUG_VFS, "Failed to get acl values in creat %d\n",
301 err);
Aneesh Kumar K.V53c06f42011-01-10 13:51:47 -0600302 goto error;
303 }
Aneesh Kumar K.Vf88657c2011-08-03 19:55:32 +0530304 err = p9_client_create_dotl(ofid, name, v9fs_open_to_dotl_flags(flags),
305 mode, gid, &qid);
Aneesh Kumar K.V53c06f42011-01-10 13:51:47 -0600306 if (err < 0) {
Joe Perches5d385152011-11-28 10:40:46 -0800307 p9_debug(P9_DEBUG_VFS, "p9_client_open_dotl failed in creat %d\n",
308 err);
Aneesh Kumar K.V53c06f42011-01-10 13:51:47 -0600309 goto error;
310 }
Aneesh Kumar K.Vd28c61f2011-02-28 17:04:08 +0530311 v9fs_invalidate_inode_attr(dir);
Aneesh Kumar K.V53c06f42011-01-10 13:51:47 -0600312
Aneesh Kumar K.Vaf7542f2011-01-10 14:22:21 -0600313 /* instantiate inode and assign the unopened fid to the dentry */
314 fid = p9_client_walk(dfid, 1, &name, 1);
315 if (IS_ERR(fid)) {
316 err = PTR_ERR(fid);
Joe Perches5d385152011-11-28 10:40:46 -0800317 p9_debug(P9_DEBUG_VFS, "p9_client_walk failed %d\n", err);
Aneesh Kumar K.V53c06f42011-01-10 13:51:47 -0600318 fid = NULL;
Aneesh Kumar K.Vaf7542f2011-01-10 14:22:21 -0600319 goto error;
Aneesh Kumar K.V53c06f42011-01-10 13:51:47 -0600320 }
Aneesh Kumar K.Ved80fcf2011-07-06 16:32:31 +0530321 inode = v9fs_get_new_inode_from_fid(v9ses, fid, dir->i_sb);
Aneesh Kumar K.Vaf7542f2011-01-10 14:22:21 -0600322 if (IS_ERR(inode)) {
323 err = PTR_ERR(inode);
Joe Perches5d385152011-11-28 10:40:46 -0800324 p9_debug(P9_DEBUG_VFS, "inode creation failed %d\n", err);
Aneesh Kumar K.Vaf7542f2011-01-10 14:22:21 -0600325 goto error;
326 }
Aneesh Kumar K.Vaf7542f2011-01-10 14:22:21 -0600327 err = v9fs_fid_add(dentry, fid);
328 if (err < 0)
329 goto error;
Aneesh Kumar K.V5441ae52011-07-25 18:06:32 +0000330 d_instantiate(dentry, inode);
Aneesh Kumar K.Vaf7542f2011-01-10 14:22:21 -0600331
Aneesh Kumar K.V53c06f42011-01-10 13:51:47 -0600332 /* Now set the ACL based on the default value */
Al Viro1ec95bf2011-07-23 02:28:13 -0400333 v9fs_set_create_acl(dentry, &dacl, &pacl);
Aneesh Kumar K.V6b39f6d2011-02-28 17:04:03 +0530334
335 v9inode = V9FS_I(inode);
Aneesh Kumar K.V5a7e0a82011-03-08 16:39:46 +0530336 mutex_lock(&v9inode->v_mutex);
Aneesh Kumar K.V7add6972011-03-08 16:39:49 +0530337 if (v9ses->cache && !v9inode->writeback_fid &&
338 ((flags & O_ACCMODE) != O_RDONLY)) {
Aneesh Kumar K.V3cf387d2011-02-28 17:03:57 +0530339 /*
Aneesh Kumar K.V6b39f6d2011-02-28 17:04:03 +0530340 * clone a fid and add it to writeback_fid
Aneesh Kumar K.V3cf387d2011-02-28 17:03:57 +0530341 * we do it during open time instead of
342 * page dirty time via write_begin/page_mkwrite
343 * because we want write after unlink usecase
344 * to work.
345 */
346 inode_fid = v9fs_writeback_fid(dentry);
347 if (IS_ERR(inode_fid)) {
348 err = PTR_ERR(inode_fid);
Aneesh Kumar K.V5a7e0a82011-03-08 16:39:46 +0530349 mutex_unlock(&v9inode->v_mutex);
Aneesh Kumar K.V398c4f02011-05-20 18:55:51 +0000350 goto err_clunk_old_fid;
Aneesh Kumar K.V3cf387d2011-02-28 17:03:57 +0530351 }
Aneesh Kumar K.V6b39f6d2011-02-28 17:04:03 +0530352 v9inode->writeback_fid = (void *) inode_fid;
Aneesh Kumar K.V3cf387d2011-02-28 17:03:57 +0530353 }
Aneesh Kumar K.V5a7e0a82011-03-08 16:39:46 +0530354 mutex_unlock(&v9inode->v_mutex);
Aneesh Kumar K.Vaf7542f2011-01-10 14:22:21 -0600355 /* Since we are opening a file, assign the open fid to the file */
Al Viro30d90492012-06-22 12:40:19 +0400356 err = finish_open(file, dentry, generic_file_open, opened);
357 if (err)
Aneesh Kumar K.V398c4f02011-05-20 18:55:51 +0000358 goto err_clunk_old_fid;
Al Viro30d90492012-06-22 12:40:19 +0400359 file->private_data = ofid;
Aneesh Kumar K.V46848de2011-02-28 17:03:55 +0530360#ifdef CONFIG_9P_FSCACHE
361 if (v9ses->cache)
Al Viro30d90492012-06-22 12:40:19 +0400362 v9fs_cache_inode_set_cookie(inode, file);
Aneesh Kumar K.V46848de2011-02-28 17:03:55 +0530363#endif
Al Viro47237682012-06-10 05:01:45 -0400364 *opened |= FILE_CREATED;
Miklos Szeredie43ae792012-06-05 15:10:26 +0200365out:
366 dput(res);
Al Virod9585272012-06-22 12:39:14 +0400367 return err;
Aneesh Kumar K.V53c06f42011-01-10 13:51:47 -0600368
369error:
Aneesh Kumar K.V53c06f42011-01-10 13:51:47 -0600370 if (fid)
371 p9_client_clunk(fid);
Aneesh Kumar K.V398c4f02011-05-20 18:55:51 +0000372err_clunk_old_fid:
373 if (ofid)
374 p9_client_clunk(ofid);
Al Viro1ec95bf2011-07-23 02:28:13 -0400375 v9fs_set_create_acl(NULL, &dacl, &pacl);
Miklos Szeredie43ae792012-06-05 15:10:26 +0200376 goto out;
Aneesh Kumar K.V53c06f42011-01-10 13:51:47 -0600377}
378
379/**
380 * v9fs_vfs_mkdir_dotl - VFS mkdir hook to create a directory
381 * @dir: inode that is being unlinked
382 * @dentry: dentry that is being unlinked
383 * @mode: mode for new directory
384 *
385 */
386
387static int v9fs_vfs_mkdir_dotl(struct inode *dir,
Al Viro18bb1db2011-07-26 01:41:39 -0400388 struct dentry *dentry, umode_t omode)
Aneesh Kumar K.V53c06f42011-01-10 13:51:47 -0600389{
390 int err;
391 struct v9fs_session_info *v9ses;
392 struct p9_fid *fid = NULL, *dfid = NULL;
393 gid_t gid;
394 char *name;
Al Virod3fb6122011-07-23 18:37:50 -0400395 umode_t mode;
Aneesh Kumar K.V53c06f42011-01-10 13:51:47 -0600396 struct inode *inode;
397 struct p9_qid qid;
398 struct dentry *dir_dentry;
399 struct posix_acl *dacl = NULL, *pacl = NULL;
400
Joe Perches5d385152011-11-28 10:40:46 -0800401 p9_debug(P9_DEBUG_VFS, "name %s\n", dentry->d_name.name);
Aneesh Kumar K.V53c06f42011-01-10 13:51:47 -0600402 err = 0;
403 v9ses = v9fs_inode2v9ses(dir);
404
405 omode |= S_IFDIR;
406 if (dir->i_mode & S_ISGID)
407 omode |= S_ISGID;
408
Al Viroaf569592012-04-02 20:02:53 -0400409 dir_dentry = dentry->d_parent;
Aneesh Kumar K.V53c06f42011-01-10 13:51:47 -0600410 dfid = v9fs_fid_lookup(dir_dentry);
411 if (IS_ERR(dfid)) {
412 err = PTR_ERR(dfid);
Joe Perches5d385152011-11-28 10:40:46 -0800413 p9_debug(P9_DEBUG_VFS, "fid lookup failed %d\n", err);
Aneesh Kumar K.V53c06f42011-01-10 13:51:47 -0600414 dfid = NULL;
415 goto error;
416 }
417
418 gid = v9fs_get_fsgid_for_create(dir);
419 mode = omode;
420 /* Update mode based on ACL value */
421 err = v9fs_acl_mode(dir, &mode, &dacl, &pacl);
422 if (err) {
Joe Perches5d385152011-11-28 10:40:46 -0800423 p9_debug(P9_DEBUG_VFS, "Failed to get acl values in mkdir %d\n",
424 err);
Aneesh Kumar K.V53c06f42011-01-10 13:51:47 -0600425 goto error;
426 }
427 name = (char *) dentry->d_name.name;
428 err = p9_client_mkdir_dotl(dfid, name, mode, gid, &qid);
429 if (err < 0)
430 goto error;
431
432 /* instantiate inode and assign the unopened fid to the dentry */
433 if (v9ses->cache == CACHE_LOOSE || v9ses->cache == CACHE_FSCACHE) {
434 fid = p9_client_walk(dfid, 1, &name, 1);
435 if (IS_ERR(fid)) {
436 err = PTR_ERR(fid);
Joe Perches5d385152011-11-28 10:40:46 -0800437 p9_debug(P9_DEBUG_VFS, "p9_client_walk failed %d\n",
438 err);
Aneesh Kumar K.V53c06f42011-01-10 13:51:47 -0600439 fid = NULL;
440 goto error;
441 }
442
Aneesh Kumar K.Ved80fcf2011-07-06 16:32:31 +0530443 inode = v9fs_get_new_inode_from_fid(v9ses, fid, dir->i_sb);
Aneesh Kumar K.V53c06f42011-01-10 13:51:47 -0600444 if (IS_ERR(inode)) {
445 err = PTR_ERR(inode);
Joe Perches5d385152011-11-28 10:40:46 -0800446 p9_debug(P9_DEBUG_VFS, "inode creation failed %d\n",
447 err);
Aneesh Kumar K.V53c06f42011-01-10 13:51:47 -0600448 goto error;
449 }
Aneesh Kumar K.V53c06f42011-01-10 13:51:47 -0600450 err = v9fs_fid_add(dentry, fid);
451 if (err < 0)
452 goto error;
Aneesh Kumar K.V5441ae52011-07-25 18:06:32 +0000453 d_instantiate(dentry, inode);
Aneesh Kumar K.V53c06f42011-01-10 13:51:47 -0600454 fid = NULL;
455 } else {
456 /*
457 * Not in cached mode. No need to populate
458 * inode with stat. We need to get an inode
459 * so that we can set the acl with dentry
460 */
Aneesh Kumar K.V45089142011-07-25 18:06:33 +0000461 inode = v9fs_get_inode(dir->i_sb, mode, 0);
Aneesh Kumar K.V53c06f42011-01-10 13:51:47 -0600462 if (IS_ERR(inode)) {
463 err = PTR_ERR(inode);
464 goto error;
465 }
Aneesh Kumar K.V53c06f42011-01-10 13:51:47 -0600466 d_instantiate(dentry, inode);
467 }
468 /* Now set the ACL based on the default value */
Al Viro1ec95bf2011-07-23 02:28:13 -0400469 v9fs_set_create_acl(dentry, &dacl, &pacl);
Aneesh Kumar K.Vb271ec42011-02-28 17:04:05 +0530470 inc_nlink(dir);
Aneesh Kumar K.Vd28c61f2011-02-28 17:04:08 +0530471 v9fs_invalidate_inode_attr(dir);
Aneesh Kumar K.V53c06f42011-01-10 13:51:47 -0600472error:
473 if (fid)
474 p9_client_clunk(fid);
Al Viro1ec95bf2011-07-23 02:28:13 -0400475 v9fs_set_create_acl(NULL, &dacl, &pacl);
Aneesh Kumar K.V53c06f42011-01-10 13:51:47 -0600476 return err;
477}
478
479static int
480v9fs_vfs_getattr_dotl(struct vfsmount *mnt, struct dentry *dentry,
481 struct kstat *stat)
482{
483 int err;
484 struct v9fs_session_info *v9ses;
485 struct p9_fid *fid;
486 struct p9_stat_dotl *st;
487
Joe Perches5d385152011-11-28 10:40:46 -0800488 p9_debug(P9_DEBUG_VFS, "dentry: %p\n", dentry);
Aneesh Kumar K.V53c06f42011-01-10 13:51:47 -0600489 err = -EPERM;
Aneesh Kumar K.V42869c82011-03-08 16:39:50 +0530490 v9ses = v9fs_dentry2v9ses(dentry);
Aneesh Kumar K.Va1211902011-02-28 17:04:01 +0530491 if (v9ses->cache == CACHE_LOOSE || v9ses->cache == CACHE_FSCACHE) {
492 generic_fillattr(dentry->d_inode, stat);
493 return 0;
494 }
Aneesh Kumar K.V53c06f42011-01-10 13:51:47 -0600495 fid = v9fs_fid_lookup(dentry);
496 if (IS_ERR(fid))
497 return PTR_ERR(fid);
498
499 /* Ask for all the fields in stat structure. Server will return
500 * whatever it supports
501 */
502
503 st = p9_client_getattr_dotl(fid, P9_STATS_ALL);
504 if (IS_ERR(st))
505 return PTR_ERR(st);
506
507 v9fs_stat2inode_dotl(st, dentry->d_inode);
508 generic_fillattr(dentry->d_inode, stat);
509 /* Change block size to what the server returned */
510 stat->blksize = st->st_blksize;
511
512 kfree(st);
513 return 0;
514}
515
Aneesh Kumar K.Vf7666192011-12-18 23:03:13 +0530516/*
517 * Attribute flags.
518 */
519#define P9_ATTR_MODE (1 << 0)
520#define P9_ATTR_UID (1 << 1)
521#define P9_ATTR_GID (1 << 2)
522#define P9_ATTR_SIZE (1 << 3)
523#define P9_ATTR_ATIME (1 << 4)
524#define P9_ATTR_MTIME (1 << 5)
525#define P9_ATTR_CTIME (1 << 6)
526#define P9_ATTR_ATIME_SET (1 << 7)
527#define P9_ATTR_MTIME_SET (1 << 8)
528
529struct dotl_iattr_map {
530 int iattr_valid;
531 int p9_iattr_valid;
532};
533
534static int v9fs_mapped_iattr_valid(int iattr_valid)
535{
536 int i;
537 int p9_iattr_valid = 0;
538 struct dotl_iattr_map dotl_iattr_map[] = {
539 { ATTR_MODE, P9_ATTR_MODE },
540 { ATTR_UID, P9_ATTR_UID },
541 { ATTR_GID, P9_ATTR_GID },
542 { ATTR_SIZE, P9_ATTR_SIZE },
543 { ATTR_ATIME, P9_ATTR_ATIME },
544 { ATTR_MTIME, P9_ATTR_MTIME },
545 { ATTR_CTIME, P9_ATTR_CTIME },
546 { ATTR_ATIME_SET, P9_ATTR_ATIME_SET },
547 { ATTR_MTIME_SET, P9_ATTR_MTIME_SET },
548 };
549 for (i = 0; i < ARRAY_SIZE(dotl_iattr_map); i++) {
550 if (iattr_valid & dotl_iattr_map[i].iattr_valid)
551 p9_iattr_valid |= dotl_iattr_map[i].p9_iattr_valid;
552 }
553 return p9_iattr_valid;
554}
555
Aneesh Kumar K.V53c06f42011-01-10 13:51:47 -0600556/**
557 * v9fs_vfs_setattr_dotl - set file metadata
558 * @dentry: file whose metadata to set
559 * @iattr: metadata assignment structure
560 *
561 */
562
563int v9fs_vfs_setattr_dotl(struct dentry *dentry, struct iattr *iattr)
564{
565 int retval;
566 struct v9fs_session_info *v9ses;
567 struct p9_fid *fid;
568 struct p9_iattr_dotl p9attr;
569
Joe Perches5d385152011-11-28 10:40:46 -0800570 p9_debug(P9_DEBUG_VFS, "\n");
Aneesh Kumar K.V53c06f42011-01-10 13:51:47 -0600571
572 retval = inode_change_ok(dentry->d_inode, iattr);
573 if (retval)
574 return retval;
575
Aneesh Kumar K.Vf7666192011-12-18 23:03:13 +0530576 p9attr.valid = v9fs_mapped_iattr_valid(iattr->ia_valid);
Aneesh Kumar K.V53c06f42011-01-10 13:51:47 -0600577 p9attr.mode = iattr->ia_mode;
578 p9attr.uid = iattr->ia_uid;
579 p9attr.gid = iattr->ia_gid;
580 p9attr.size = iattr->ia_size;
581 p9attr.atime_sec = iattr->ia_atime.tv_sec;
582 p9attr.atime_nsec = iattr->ia_atime.tv_nsec;
583 p9attr.mtime_sec = iattr->ia_mtime.tv_sec;
584 p9attr.mtime_nsec = iattr->ia_mtime.tv_nsec;
585
586 retval = -EPERM;
Aneesh Kumar K.V42869c82011-03-08 16:39:50 +0530587 v9ses = v9fs_dentry2v9ses(dentry);
Aneesh Kumar K.V53c06f42011-01-10 13:51:47 -0600588 fid = v9fs_fid_lookup(dentry);
589 if (IS_ERR(fid))
590 return PTR_ERR(fid);
591
Aneesh Kumar K.V3dc54362011-02-28 17:04:11 +0530592 /* Write all dirty data */
593 if (S_ISREG(dentry->d_inode->i_mode))
594 filemap_write_and_wait(dentry->d_inode->i_mapping);
595
Aneesh Kumar K.Vf10fc502011-02-28 17:04:10 +0530596 retval = p9_client_setattr(fid, &p9attr);
597 if (retval < 0)
598 return retval;
Aneesh Kumar K.V53c06f42011-01-10 13:51:47 -0600599
Aneesh Kumar K.V059c1382011-03-08 16:39:48 +0530600 if ((iattr->ia_valid & ATTR_SIZE) &&
601 iattr->ia_size != i_size_read(dentry->d_inode))
602 truncate_setsize(dentry->d_inode, iattr->ia_size);
603
604 v9fs_invalidate_inode_attr(dentry->d_inode);
Aneesh Kumar K.V53c06f42011-01-10 13:51:47 -0600605 setattr_copy(dentry->d_inode, iattr);
606 mark_inode_dirty(dentry->d_inode);
607 if (iattr->ia_valid & ATTR_MODE) {
608 /* We also want to update ACL when we update mode bits */
609 retval = v9fs_acl_chmod(dentry);
610 if (retval < 0)
611 return retval;
612 }
613 return 0;
614}
615
616/**
617 * v9fs_stat2inode_dotl - populate an inode structure with stat info
618 * @stat: stat structure
619 * @inode: inode to populate
620 * @sb: superblock of filesystem
621 *
622 */
623
624void
625v9fs_stat2inode_dotl(struct p9_stat_dotl *stat, struct inode *inode)
626{
Al Viro3eda0de2011-07-26 02:53:22 -0400627 umode_t mode;
Aneesh Kumar K.Vb3cbea02011-02-28 17:04:06 +0530628 struct v9fs_inode *v9inode = V9FS_I(inode);
Aneesh Kumar K.V53c06f42011-01-10 13:51:47 -0600629
630 if ((stat->st_result_mask & P9_STATS_BASIC) == P9_STATS_BASIC) {
631 inode->i_atime.tv_sec = stat->st_atime_sec;
632 inode->i_atime.tv_nsec = stat->st_atime_nsec;
633 inode->i_mtime.tv_sec = stat->st_mtime_sec;
634 inode->i_mtime.tv_nsec = stat->st_mtime_nsec;
635 inode->i_ctime.tv_sec = stat->st_ctime_sec;
636 inode->i_ctime.tv_nsec = stat->st_ctime_nsec;
637 inode->i_uid = stat->st_uid;
638 inode->i_gid = stat->st_gid;
Miklos Szeredibfe86842011-10-28 14:13:29 +0200639 set_nlink(inode, stat->st_nlink);
Aneesh Kumar K.V53c06f42011-01-10 13:51:47 -0600640
Aneesh Kumar K.V45089142011-07-25 18:06:33 +0000641 mode = stat->st_mode & S_IALLUGO;
642 mode |= inode->i_mode & ~S_IALLUGO;
643 inode->i_mode = mode;
Aneesh Kumar K.V53c06f42011-01-10 13:51:47 -0600644
645 i_size_write(inode, stat->st_size);
646 inode->i_blocks = stat->st_blocks;
647 } else {
648 if (stat->st_result_mask & P9_STATS_ATIME) {
649 inode->i_atime.tv_sec = stat->st_atime_sec;
650 inode->i_atime.tv_nsec = stat->st_atime_nsec;
651 }
652 if (stat->st_result_mask & P9_STATS_MTIME) {
653 inode->i_mtime.tv_sec = stat->st_mtime_sec;
654 inode->i_mtime.tv_nsec = stat->st_mtime_nsec;
655 }
656 if (stat->st_result_mask & P9_STATS_CTIME) {
657 inode->i_ctime.tv_sec = stat->st_ctime_sec;
658 inode->i_ctime.tv_nsec = stat->st_ctime_nsec;
659 }
660 if (stat->st_result_mask & P9_STATS_UID)
661 inode->i_uid = stat->st_uid;
662 if (stat->st_result_mask & P9_STATS_GID)
663 inode->i_gid = stat->st_gid;
664 if (stat->st_result_mask & P9_STATS_NLINK)
Miklos Szeredibfe86842011-10-28 14:13:29 +0200665 set_nlink(inode, stat->st_nlink);
Aneesh Kumar K.V53c06f42011-01-10 13:51:47 -0600666 if (stat->st_result_mask & P9_STATS_MODE) {
667 inode->i_mode = stat->st_mode;
668 if ((S_ISBLK(inode->i_mode)) ||
669 (S_ISCHR(inode->i_mode)))
670 init_special_inode(inode, inode->i_mode,
671 inode->i_rdev);
672 }
673 if (stat->st_result_mask & P9_STATS_RDEV)
674 inode->i_rdev = new_decode_dev(stat->st_rdev);
675 if (stat->st_result_mask & P9_STATS_SIZE)
676 i_size_write(inode, stat->st_size);
677 if (stat->st_result_mask & P9_STATS_BLOCKS)
678 inode->i_blocks = stat->st_blocks;
679 }
680 if (stat->st_result_mask & P9_STATS_GEN)
Aneesh Kumar K.Vfd2421f2011-07-11 16:40:59 +0000681 inode->i_generation = stat->st_gen;
Aneesh Kumar K.V53c06f42011-01-10 13:51:47 -0600682
683 /* Currently we don't support P9_STATS_BTIME and P9_STATS_DATA_VERSION
684 * because the inode structure does not have fields for them.
685 */
Aneesh Kumar K.Vb3cbea02011-02-28 17:04:06 +0530686 v9inode->cache_validity &= ~V9FS_INO_INVALID_ATTR;
Aneesh Kumar K.V53c06f42011-01-10 13:51:47 -0600687}
688
689static int
690v9fs_vfs_symlink_dotl(struct inode *dir, struct dentry *dentry,
691 const char *symname)
692{
Aneesh Kumar K.V53c06f42011-01-10 13:51:47 -0600693 int err;
694 gid_t gid;
Aneesh Kumar K.Vd28c61f2011-02-28 17:04:08 +0530695 char *name;
696 struct p9_qid qid;
697 struct inode *inode;
698 struct p9_fid *dfid;
699 struct p9_fid *fid = NULL;
700 struct v9fs_session_info *v9ses;
Aneesh Kumar K.V53c06f42011-01-10 13:51:47 -0600701
702 name = (char *) dentry->d_name.name;
Joe Perches5d385152011-11-28 10:40:46 -0800703 p9_debug(P9_DEBUG_VFS, "%lu,%s,%s\n", dir->i_ino, name, symname);
Aneesh Kumar K.V53c06f42011-01-10 13:51:47 -0600704 v9ses = v9fs_inode2v9ses(dir);
705
706 dfid = v9fs_fid_lookup(dentry->d_parent);
707 if (IS_ERR(dfid)) {
708 err = PTR_ERR(dfid);
Joe Perches5d385152011-11-28 10:40:46 -0800709 p9_debug(P9_DEBUG_VFS, "fid lookup failed %d\n", err);
Aneesh Kumar K.V53c06f42011-01-10 13:51:47 -0600710 return err;
711 }
712
713 gid = v9fs_get_fsgid_for_create(dir);
714
715 /* Server doesn't alter fid on TSYMLINK. Hence no need to clone it. */
716 err = p9_client_symlink(dfid, name, (char *)symname, gid, &qid);
717
718 if (err < 0) {
Joe Perches5d385152011-11-28 10:40:46 -0800719 p9_debug(P9_DEBUG_VFS, "p9_client_symlink failed %d\n", err);
Aneesh Kumar K.V53c06f42011-01-10 13:51:47 -0600720 goto error;
721 }
722
Aneesh Kumar K.Vd28c61f2011-02-28 17:04:08 +0530723 v9fs_invalidate_inode_attr(dir);
Aneesh Kumar K.V53c06f42011-01-10 13:51:47 -0600724 if (v9ses->cache) {
725 /* Now walk from the parent so we can get an unopened fid. */
726 fid = p9_client_walk(dfid, 1, &name, 1);
727 if (IS_ERR(fid)) {
728 err = PTR_ERR(fid);
Joe Perches5d385152011-11-28 10:40:46 -0800729 p9_debug(P9_DEBUG_VFS, "p9_client_walk failed %d\n",
730 err);
Aneesh Kumar K.V53c06f42011-01-10 13:51:47 -0600731 fid = NULL;
732 goto error;
733 }
734
735 /* instantiate inode and assign the unopened fid to dentry */
Aneesh Kumar K.Ved80fcf2011-07-06 16:32:31 +0530736 inode = v9fs_get_new_inode_from_fid(v9ses, fid, dir->i_sb);
Aneesh Kumar K.V53c06f42011-01-10 13:51:47 -0600737 if (IS_ERR(inode)) {
738 err = PTR_ERR(inode);
Joe Perches5d385152011-11-28 10:40:46 -0800739 p9_debug(P9_DEBUG_VFS, "inode creation failed %d\n",
740 err);
Aneesh Kumar K.V53c06f42011-01-10 13:51:47 -0600741 goto error;
742 }
Aneesh Kumar K.V53c06f42011-01-10 13:51:47 -0600743 err = v9fs_fid_add(dentry, fid);
744 if (err < 0)
745 goto error;
Aneesh Kumar K.V5441ae52011-07-25 18:06:32 +0000746 d_instantiate(dentry, inode);
Aneesh Kumar K.V53c06f42011-01-10 13:51:47 -0600747 fid = NULL;
748 } else {
749 /* Not in cached mode. No need to populate inode with stat */
Aneesh Kumar K.V45089142011-07-25 18:06:33 +0000750 inode = v9fs_get_inode(dir->i_sb, S_IFLNK, 0);
Aneesh Kumar K.V53c06f42011-01-10 13:51:47 -0600751 if (IS_ERR(inode)) {
752 err = PTR_ERR(inode);
753 goto error;
754 }
Aneesh Kumar K.V53c06f42011-01-10 13:51:47 -0600755 d_instantiate(dentry, inode);
756 }
757
758error:
759 if (fid)
760 p9_client_clunk(fid);
761
762 return err;
763}
764
765/**
766 * v9fs_vfs_link_dotl - create a hardlink for dotl
767 * @old_dentry: dentry for file to link to
768 * @dir: inode destination for new link
769 * @dentry: dentry for link
770 *
771 */
772
773static int
774v9fs_vfs_link_dotl(struct dentry *old_dentry, struct inode *dir,
775 struct dentry *dentry)
776{
777 int err;
Aneesh Kumar K.V53c06f42011-01-10 13:51:47 -0600778 char *name;
Aneesh Kumar K.V53c06f42011-01-10 13:51:47 -0600779 struct dentry *dir_dentry;
Aneesh Kumar K.Vd28c61f2011-02-28 17:04:08 +0530780 struct p9_fid *dfid, *oldfid;
781 struct v9fs_session_info *v9ses;
Aneesh Kumar K.V53c06f42011-01-10 13:51:47 -0600782
Joe Perches5d385152011-11-28 10:40:46 -0800783 p9_debug(P9_DEBUG_VFS, "dir ino: %lu, old_name: %s, new_name: %s\n",
784 dir->i_ino, old_dentry->d_name.name, dentry->d_name.name);
Aneesh Kumar K.V53c06f42011-01-10 13:51:47 -0600785
786 v9ses = v9fs_inode2v9ses(dir);
Al Viroaf569592012-04-02 20:02:53 -0400787 dir_dentry = dentry->d_parent;
Aneesh Kumar K.V53c06f42011-01-10 13:51:47 -0600788 dfid = v9fs_fid_lookup(dir_dentry);
789 if (IS_ERR(dfid))
790 return PTR_ERR(dfid);
791
792 oldfid = v9fs_fid_lookup(old_dentry);
793 if (IS_ERR(oldfid))
794 return PTR_ERR(oldfid);
795
796 name = (char *) dentry->d_name.name;
797
798 err = p9_client_link(dfid, oldfid, (char *)dentry->d_name.name);
799
800 if (err < 0) {
Joe Perches5d385152011-11-28 10:40:46 -0800801 p9_debug(P9_DEBUG_VFS, "p9_client_link failed %d\n", err);
Aneesh Kumar K.V53c06f42011-01-10 13:51:47 -0600802 return err;
803 }
804
Aneesh Kumar K.Vd28c61f2011-02-28 17:04:08 +0530805 v9fs_invalidate_inode_attr(dir);
Aneesh Kumar K.V53c06f42011-01-10 13:51:47 -0600806 if (v9ses->cache == CACHE_LOOSE || v9ses->cache == CACHE_FSCACHE) {
807 /* Get the latest stat info from server. */
808 struct p9_fid *fid;
Aneesh Kumar K.V53c06f42011-01-10 13:51:47 -0600809 fid = v9fs_fid_lookup(old_dentry);
810 if (IS_ERR(fid))
811 return PTR_ERR(fid);
812
Aneesh Kumar K.Vc06c0662011-02-28 17:04:09 +0530813 v9fs_refresh_inode_dotl(fid, old_dentry->d_inode);
Aneesh Kumar K.V53c06f42011-01-10 13:51:47 -0600814 }
Aneesh Kumar K.V20656a42011-02-28 17:03:55 +0530815 ihold(old_dentry->d_inode);
Aneesh Kumar K.V53c06f42011-01-10 13:51:47 -0600816 d_instantiate(dentry, old_dentry->d_inode);
817
818 return err;
819}
820
821/**
822 * v9fs_vfs_mknod_dotl - create a special file
823 * @dir: inode destination for new link
824 * @dentry: dentry for file
825 * @mode: mode for creation
826 * @rdev: device associated with special file
827 *
828 */
829static int
Al Viro1a67aaf2011-07-26 01:52:52 -0400830v9fs_vfs_mknod_dotl(struct inode *dir, struct dentry *dentry, umode_t omode,
Aneesh Kumar K.V53c06f42011-01-10 13:51:47 -0600831 dev_t rdev)
832{
833 int err;
Aneesh Kumar K.Vd28c61f2011-02-28 17:04:08 +0530834 gid_t gid;
Aneesh Kumar K.V53c06f42011-01-10 13:51:47 -0600835 char *name;
Al Virod3fb6122011-07-23 18:37:50 -0400836 umode_t mode;
Aneesh Kumar K.V53c06f42011-01-10 13:51:47 -0600837 struct v9fs_session_info *v9ses;
838 struct p9_fid *fid = NULL, *dfid = NULL;
839 struct inode *inode;
Aneesh Kumar K.V53c06f42011-01-10 13:51:47 -0600840 struct p9_qid qid;
841 struct dentry *dir_dentry;
842 struct posix_acl *dacl = NULL, *pacl = NULL;
843
Linus Torvalds609eac12012-01-10 15:09:01 -0800844 p9_debug(P9_DEBUG_VFS, " %lu,%s mode: %hx MAJOR: %u MINOR: %u\n",
Joe Perches5d385152011-11-28 10:40:46 -0800845 dir->i_ino, dentry->d_name.name, omode,
846 MAJOR(rdev), MINOR(rdev));
Aneesh Kumar K.V53c06f42011-01-10 13:51:47 -0600847
848 if (!new_valid_dev(rdev))
849 return -EINVAL;
850
851 v9ses = v9fs_inode2v9ses(dir);
Al Viroaf569592012-04-02 20:02:53 -0400852 dir_dentry = dentry->d_parent;
Aneesh Kumar K.V53c06f42011-01-10 13:51:47 -0600853 dfid = v9fs_fid_lookup(dir_dentry);
854 if (IS_ERR(dfid)) {
855 err = PTR_ERR(dfid);
Joe Perches5d385152011-11-28 10:40:46 -0800856 p9_debug(P9_DEBUG_VFS, "fid lookup failed %d\n", err);
Aneesh Kumar K.V53c06f42011-01-10 13:51:47 -0600857 dfid = NULL;
858 goto error;
859 }
860
861 gid = v9fs_get_fsgid_for_create(dir);
862 mode = omode;
863 /* Update mode based on ACL value */
864 err = v9fs_acl_mode(dir, &mode, &dacl, &pacl);
865 if (err) {
Joe Perches5d385152011-11-28 10:40:46 -0800866 p9_debug(P9_DEBUG_VFS, "Failed to get acl values in mknod %d\n",
867 err);
Aneesh Kumar K.V53c06f42011-01-10 13:51:47 -0600868 goto error;
869 }
870 name = (char *) dentry->d_name.name;
871
872 err = p9_client_mknod_dotl(dfid, name, mode, rdev, gid, &qid);
873 if (err < 0)
874 goto error;
875
Aneesh Kumar K.Vd28c61f2011-02-28 17:04:08 +0530876 v9fs_invalidate_inode_attr(dir);
Aneesh Kumar K.V53c06f42011-01-10 13:51:47 -0600877 /* instantiate inode and assign the unopened fid to the dentry */
878 if (v9ses->cache == CACHE_LOOSE || v9ses->cache == CACHE_FSCACHE) {
879 fid = p9_client_walk(dfid, 1, &name, 1);
880 if (IS_ERR(fid)) {
881 err = PTR_ERR(fid);
Joe Perches5d385152011-11-28 10:40:46 -0800882 p9_debug(P9_DEBUG_VFS, "p9_client_walk failed %d\n",
883 err);
Aneesh Kumar K.V53c06f42011-01-10 13:51:47 -0600884 fid = NULL;
885 goto error;
886 }
887
Aneesh Kumar K.Ved80fcf2011-07-06 16:32:31 +0530888 inode = v9fs_get_new_inode_from_fid(v9ses, fid, dir->i_sb);
Aneesh Kumar K.V53c06f42011-01-10 13:51:47 -0600889 if (IS_ERR(inode)) {
890 err = PTR_ERR(inode);
Joe Perches5d385152011-11-28 10:40:46 -0800891 p9_debug(P9_DEBUG_VFS, "inode creation failed %d\n",
892 err);
Aneesh Kumar K.V53c06f42011-01-10 13:51:47 -0600893 goto error;
894 }
Aneesh Kumar K.V53c06f42011-01-10 13:51:47 -0600895 err = v9fs_fid_add(dentry, fid);
896 if (err < 0)
897 goto error;
Aneesh Kumar K.V5441ae52011-07-25 18:06:32 +0000898 d_instantiate(dentry, inode);
Aneesh Kumar K.V53c06f42011-01-10 13:51:47 -0600899 fid = NULL;
900 } else {
901 /*
902 * Not in cached mode. No need to populate inode with stat.
903 * socket syscall returns a fd, so we need instantiate
904 */
Aneesh Kumar K.V45089142011-07-25 18:06:33 +0000905 inode = v9fs_get_inode(dir->i_sb, mode, rdev);
Aneesh Kumar K.V53c06f42011-01-10 13:51:47 -0600906 if (IS_ERR(inode)) {
907 err = PTR_ERR(inode);
908 goto error;
909 }
Aneesh Kumar K.V53c06f42011-01-10 13:51:47 -0600910 d_instantiate(dentry, inode);
911 }
912 /* Now set the ACL based on the default value */
Al Viro1ec95bf2011-07-23 02:28:13 -0400913 v9fs_set_create_acl(dentry, &dacl, &pacl);
Aneesh Kumar K.V53c06f42011-01-10 13:51:47 -0600914error:
915 if (fid)
916 p9_client_clunk(fid);
Al Viro1ec95bf2011-07-23 02:28:13 -0400917 v9fs_set_create_acl(NULL, &dacl, &pacl);
Aneesh Kumar K.V53c06f42011-01-10 13:51:47 -0600918 return err;
919}
920
Aneesh Kumar K.V53c06f42011-01-10 13:51:47 -0600921/**
922 * v9fs_vfs_follow_link_dotl - follow a symlink path
923 * @dentry: dentry for symlink
924 * @nd: nameidata
925 *
926 */
927
928static void *
929v9fs_vfs_follow_link_dotl(struct dentry *dentry, struct nameidata *nd)
930{
M. Mohan Kumar31b6cea2011-01-08 07:28:46 +0530931 int retval;
932 struct p9_fid *fid;
Aneesh Kumar K.V53c06f42011-01-10 13:51:47 -0600933 char *link = __getname();
M. Mohan Kumar31b6cea2011-01-08 07:28:46 +0530934 char *target;
Aneesh Kumar K.V53c06f42011-01-10 13:51:47 -0600935
Joe Perches5d385152011-11-28 10:40:46 -0800936 p9_debug(P9_DEBUG_VFS, "%s\n", dentry->d_name.name);
Aneesh Kumar K.V53c06f42011-01-10 13:51:47 -0600937
M. Mohan Kumar31b6cea2011-01-08 07:28:46 +0530938 if (!link) {
Aneesh Kumar K.V53c06f42011-01-10 13:51:47 -0600939 link = ERR_PTR(-ENOMEM);
M. Mohan Kumar31b6cea2011-01-08 07:28:46 +0530940 goto ndset;
Aneesh Kumar K.V53c06f42011-01-10 13:51:47 -0600941 }
M. Mohan Kumar31b6cea2011-01-08 07:28:46 +0530942 fid = v9fs_fid_lookup(dentry);
943 if (IS_ERR(fid)) {
944 __putname(link);
Aneesh Kumar K.V936bb2d2011-03-24 23:04:41 +0530945 link = ERR_CAST(fid);
M. Mohan Kumar31b6cea2011-01-08 07:28:46 +0530946 goto ndset;
947 }
948 retval = p9_client_readlink(fid, &target);
949 if (!retval) {
950 strcpy(link, target);
951 kfree(target);
952 goto ndset;
953 }
954 __putname(link);
955 link = ERR_PTR(retval);
956ndset:
Aneesh Kumar K.V53c06f42011-01-10 13:51:47 -0600957 nd_set_link(nd, link);
Aneesh Kumar K.V53c06f42011-01-10 13:51:47 -0600958 return NULL;
959}
960
Aneesh Kumar K.Vb3cbea02011-02-28 17:04:06 +0530961int v9fs_refresh_inode_dotl(struct p9_fid *fid, struct inode *inode)
962{
963 loff_t i_size;
964 struct p9_stat_dotl *st;
965 struct v9fs_session_info *v9ses;
966
967 v9ses = v9fs_inode2v9ses(inode);
968 st = p9_client_getattr_dotl(fid, P9_STATS_ALL);
969 if (IS_ERR(st))
970 return PTR_ERR(st);
Aneesh Kumar K.V45089142011-07-25 18:06:33 +0000971 /*
972 * Don't update inode if the file type is different
973 */
974 if ((inode->i_mode & S_IFMT) != (st->st_mode & S_IFMT))
975 goto out;
Aneesh Kumar K.Vb3cbea02011-02-28 17:04:06 +0530976
977 spin_lock(&inode->i_lock);
978 /*
979 * We don't want to refresh inode->i_size,
980 * because we may have cached data
981 */
982 i_size = inode->i_size;
983 v9fs_stat2inode_dotl(st, inode);
984 if (v9ses->cache)
985 inode->i_size = i_size;
986 spin_unlock(&inode->i_lock);
Aneesh Kumar K.V45089142011-07-25 18:06:33 +0000987out:
Aneesh Kumar K.Vb3cbea02011-02-28 17:04:06 +0530988 kfree(st);
989 return 0;
990}
991
Aneesh Kumar K.V53c06f42011-01-10 13:51:47 -0600992const struct inode_operations v9fs_dir_inode_operations_dotl = {
993 .create = v9fs_vfs_create_dotl,
Miklos Szeredie43ae792012-06-05 15:10:26 +0200994 .atomic_open = v9fs_vfs_atomic_open_dotl,
Aneesh Kumar K.V53c06f42011-01-10 13:51:47 -0600995 .lookup = v9fs_vfs_lookup,
996 .link = v9fs_vfs_link_dotl,
997 .symlink = v9fs_vfs_symlink_dotl,
998 .unlink = v9fs_vfs_unlink,
999 .mkdir = v9fs_vfs_mkdir_dotl,
1000 .rmdir = v9fs_vfs_rmdir,
1001 .mknod = v9fs_vfs_mknod_dotl,
1002 .rename = v9fs_vfs_rename,
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_file_inode_operations_dotl = {
1013 .getattr = v9fs_vfs_getattr_dotl,
1014 .setattr = v9fs_vfs_setattr_dotl,
1015 .setxattr = generic_setxattr,
1016 .getxattr = generic_getxattr,
1017 .removexattr = generic_removexattr,
1018 .listxattr = v9fs_listxattr,
Christoph Hellwig4e34e712011-07-23 17:37:31 +02001019 .get_acl = v9fs_iop_get_acl,
Aneesh Kumar K.V53c06f42011-01-10 13:51:47 -06001020};
1021
1022const struct inode_operations v9fs_symlink_inode_operations_dotl = {
M. Mohan Kumar31b6cea2011-01-08 07:28:46 +05301023 .readlink = generic_readlink,
Aneesh Kumar K.V53c06f42011-01-10 13:51:47 -06001024 .follow_link = v9fs_vfs_follow_link_dotl,
1025 .put_link = v9fs_vfs_put_link,
1026 .getattr = v9fs_vfs_getattr_dotl,
1027 .setattr = v9fs_vfs_setattr_dotl,
1028 .setxattr = generic_setxattr,
1029 .getxattr = generic_getxattr,
1030 .removexattr = generic_removexattr,
1031 .listxattr = v9fs_listxattr,
1032};