blob: 9d808d0e0cd9ccf896c0ab20b571cfebc7188a79 [file] [log] [blame]
Aneesh Kumar K.V53c06f42011-01-10 13:51:47 -06001/*
2 * linux/fs/9p/vfs_inode_dotl.c
3 *
4 * This file contains vfs inode ops for the 9P2000.L protocol.
5 *
6 * Copyright (C) 2004 by Eric Van Hensbergen <ericvh@gmail.com>
7 * Copyright (C) 2002 by Ron Minnich <rminnich@lanl.gov>
8 *
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License version 2
11 * as published by the Free Software Foundation.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to:
20 * Free Software Foundation
21 * 51 Franklin Street, Fifth Floor
22 * Boston, MA 02111-1301 USA
23 *
24 */
25
26#include <linux/module.h>
27#include <linux/errno.h>
28#include <linux/fs.h>
29#include <linux/file.h>
30#include <linux/pagemap.h>
31#include <linux/stat.h>
32#include <linux/string.h>
33#include <linux/inet.h>
34#include <linux/namei.h>
35#include <linux/idr.h>
36#include <linux/sched.h>
37#include <linux/slab.h>
38#include <linux/xattr.h>
39#include <linux/posix_acl.h>
40#include <net/9p/9p.h>
41#include <net/9p/client.h>
42
43#include "v9fs.h"
44#include "v9fs_vfs.h"
45#include "fid.h"
46#include "cache.h"
47#include "xattr.h"
48#include "acl.h"
49
50static int
51v9fs_vfs_mknod_dotl(struct inode *dir, struct dentry *dentry, int omode,
52 dev_t rdev);
53
54/**
55 * v9fs_get_fsgid_for_create - Helper function to get the gid for creating a
56 * new file system object. This checks the S_ISGID to determine the owning
57 * group of the new file system object.
58 */
59
60static gid_t v9fs_get_fsgid_for_create(struct inode *dir_inode)
61{
62 BUG_ON(dir_inode == NULL);
63
64 if (dir_inode->i_mode & S_ISGID) {
65 /* set_gid bit is set.*/
66 return dir_inode->i_gid;
67 }
68 return current_fsgid();
69}
70
71/**
72 * v9fs_dentry_from_dir_inode - helper function to get the dentry from
73 * dir inode.
74 *
75 */
76
77static struct dentry *v9fs_dentry_from_dir_inode(struct inode *inode)
78{
79 struct dentry *dentry;
80
81 spin_lock(&inode->i_lock);
82 /* Directory should have only one entry. */
83 BUG_ON(S_ISDIR(inode->i_mode) && !list_is_singular(&inode->i_dentry));
84 dentry = list_entry(inode->i_dentry.next, struct dentry, d_alias);
85 spin_unlock(&inode->i_lock);
86 return dentry;
87}
88
Aneesh Kumar K.V5ffc0cb2011-02-28 17:04:01 +053089static struct inode *v9fs_qid_iget_dotl(struct super_block *sb,
90 struct p9_qid *qid,
91 struct p9_fid *fid,
92 struct p9_stat_dotl *st)
93{
94 int retval;
95 unsigned long i_ino;
96 struct inode *inode;
97 struct v9fs_session_info *v9ses = sb->s_fs_info;
98
99 i_ino = v9fs_qid2ino(qid);
100 inode = iget_locked(sb, i_ino);
101 if (!inode)
102 return ERR_PTR(-ENOMEM);
103 if (!(inode->i_state & I_NEW))
104 return inode;
105 /*
106 * initialize the inode with the stat info
107 * FIXME!! we may need support for stale inodes
108 * later.
109 */
110 retval = v9fs_init_inode(v9ses, inode, st->st_mode);
111 if (retval)
112 goto error;
113
114 v9fs_stat2inode_dotl(st, inode);
115#ifdef CONFIG_9P_FSCACHE
Aneesh Kumar K.Va78ce052011-02-28 17:04:02 +0530116 v9fs_fscache_set_key(inode, &st->qid);
Aneesh Kumar K.V5ffc0cb2011-02-28 17:04:01 +0530117 v9fs_cache_inode_get_cookie(inode);
118#endif
119 retval = v9fs_get_acl(inode, fid);
120 if (retval)
121 goto error;
122
123 unlock_new_inode(inode);
124 return inode;
125error:
126 unlock_new_inode(inode);
127 iput(inode);
128 return ERR_PTR(retval);
129
130}
131
Aneesh Kumar K.V53c06f42011-01-10 13:51:47 -0600132struct inode *
Aneesh Kumar K.Va78ce052011-02-28 17:04:02 +0530133v9fs_inode_from_fid_dotl(struct v9fs_session_info *v9ses, struct p9_fid *fid,
134 struct super_block *sb)
Aneesh Kumar K.V53c06f42011-01-10 13:51:47 -0600135{
Aneesh Kumar K.V53c06f42011-01-10 13:51:47 -0600136 struct p9_stat_dotl *st;
Aneesh Kumar K.V5ffc0cb2011-02-28 17:04:01 +0530137 struct inode *inode = NULL;
Aneesh Kumar K.V53c06f42011-01-10 13:51:47 -0600138
139 st = p9_client_getattr_dotl(fid, P9_STATS_BASIC);
140 if (IS_ERR(st))
141 return ERR_CAST(st);
142
Aneesh Kumar K.V5ffc0cb2011-02-28 17:04:01 +0530143 inode = v9fs_qid_iget_dotl(sb, &st->qid, fid, st);
Aneesh Kumar K.V53c06f42011-01-10 13:51:47 -0600144 kfree(st);
Aneesh Kumar K.V5ffc0cb2011-02-28 17:04:01 +0530145 return inode;
Aneesh Kumar K.V53c06f42011-01-10 13:51:47 -0600146}
147
148/**
149 * v9fs_vfs_create_dotl - VFS hook to create files for 9P2000.L protocol.
150 * @dir: directory inode that is being created
151 * @dentry: dentry that is being deleted
152 * @mode: create permissions
153 * @nd: path information
154 *
155 */
156
157static int
158v9fs_vfs_create_dotl(struct inode *dir, struct dentry *dentry, int omode,
159 struct nameidata *nd)
160{
161 int err = 0;
Aneesh Kumar K.V53c06f42011-01-10 13:51:47 -0600162 gid_t gid;
163 int flags;
164 mode_t mode;
Aneesh Kumar K.V6b39f6d2011-02-28 17:04:03 +0530165 char *name = NULL;
Aneesh Kumar K.V53c06f42011-01-10 13:51:47 -0600166 struct file *filp;
167 struct p9_qid qid;
168 struct inode *inode;
Aneesh Kumar K.V6b39f6d2011-02-28 17:04:03 +0530169 struct p9_fid *fid = NULL;
170 struct v9fs_inode *v9inode;
171 struct p9_fid *dfid, *ofid, *inode_fid;
172 struct v9fs_session_info *v9ses;
Aneesh Kumar K.V53c06f42011-01-10 13:51:47 -0600173 struct posix_acl *pacl = NULL, *dacl = NULL;
174
175 v9ses = v9fs_inode2v9ses(dir);
Al Virodd7dd5562011-06-25 21:17:17 -0400176 if (nd)
Al Viro8a5e9292011-06-25 19:15:54 -0400177 flags = nd->intent.open.flags;
Aneesh Kumar K.V53c06f42011-01-10 13:51:47 -0600178 else {
179 /*
180 * create call without LOOKUP_OPEN is due
181 * to mknod of regular files. So use mknod
182 * operation.
183 */
184 return v9fs_vfs_mknod_dotl(dir, dentry, omode, 0);
185 }
186
187 name = (char *) dentry->d_name.name;
188 P9_DPRINTK(P9_DEBUG_VFS, "v9fs_vfs_create_dotl: name:%s flags:0x%x "
189 "mode:0x%x\n", name, flags, omode);
190
191 dfid = v9fs_fid_lookup(dentry->d_parent);
192 if (IS_ERR(dfid)) {
193 err = PTR_ERR(dfid);
194 P9_DPRINTK(P9_DEBUG_VFS, "fid lookup failed %d\n", err);
195 return err;
196 }
197
198 /* clone a fid to use for creation */
199 ofid = p9_client_walk(dfid, 0, NULL, 1);
200 if (IS_ERR(ofid)) {
201 err = PTR_ERR(ofid);
202 P9_DPRINTK(P9_DEBUG_VFS, "p9_client_walk failed %d\n", err);
203 return err;
204 }
205
206 gid = v9fs_get_fsgid_for_create(dir);
207
208 mode = omode;
209 /* Update mode based on ACL value */
210 err = v9fs_acl_mode(dir, &mode, &dacl, &pacl);
211 if (err) {
212 P9_DPRINTK(P9_DEBUG_VFS,
213 "Failed to get acl values in creat %d\n", err);
214 goto error;
215 }
216 err = p9_client_create_dotl(ofid, name, flags, mode, gid, &qid);
217 if (err < 0) {
218 P9_DPRINTK(P9_DEBUG_VFS,
219 "p9_client_open_dotl failed in creat %d\n",
220 err);
221 goto error;
222 }
Aneesh Kumar K.Vd28c61f2011-02-28 17:04:08 +0530223 v9fs_invalidate_inode_attr(dir);
Aneesh Kumar K.V53c06f42011-01-10 13:51:47 -0600224
Aneesh Kumar K.Vaf7542f2011-01-10 14:22:21 -0600225 /* instantiate inode and assign the unopened fid to the dentry */
226 fid = p9_client_walk(dfid, 1, &name, 1);
227 if (IS_ERR(fid)) {
228 err = PTR_ERR(fid);
Eric Van Hensbergenc25a61f2011-01-11 09:49:03 -0600229 P9_DPRINTK(P9_DEBUG_VFS, "p9_client_walk failed %d\n", err);
Aneesh Kumar K.V53c06f42011-01-10 13:51:47 -0600230 fid = NULL;
Aneesh Kumar K.Vaf7542f2011-01-10 14:22:21 -0600231 goto error;
Aneesh Kumar K.V53c06f42011-01-10 13:51:47 -0600232 }
Aneesh Kumar K.Va78ce052011-02-28 17:04:02 +0530233 inode = v9fs_get_inode_from_fid(v9ses, fid, dir->i_sb);
Aneesh Kumar K.Vaf7542f2011-01-10 14:22:21 -0600234 if (IS_ERR(inode)) {
235 err = PTR_ERR(inode);
236 P9_DPRINTK(P9_DEBUG_VFS, "inode creation failed %d\n", err);
237 goto error;
238 }
Aneesh Kumar K.Vaf7542f2011-01-10 14:22:21 -0600239 d_instantiate(dentry, inode);
240 err = v9fs_fid_add(dentry, fid);
241 if (err < 0)
242 goto error;
243
Aneesh Kumar K.V53c06f42011-01-10 13:51:47 -0600244 /* Now set the ACL based on the default value */
Al Viro1ec95bf2011-07-23 02:28:13 -0400245 v9fs_set_create_acl(dentry, &dacl, &pacl);
Aneesh Kumar K.V6b39f6d2011-02-28 17:04:03 +0530246
247 v9inode = V9FS_I(inode);
Aneesh Kumar K.V5a7e0a82011-03-08 16:39:46 +0530248 mutex_lock(&v9inode->v_mutex);
Aneesh Kumar K.V7add6972011-03-08 16:39:49 +0530249 if (v9ses->cache && !v9inode->writeback_fid &&
250 ((flags & O_ACCMODE) != O_RDONLY)) {
Aneesh Kumar K.V3cf387d2011-02-28 17:03:57 +0530251 /*
Aneesh Kumar K.V6b39f6d2011-02-28 17:04:03 +0530252 * clone a fid and add it to writeback_fid
Aneesh Kumar K.V3cf387d2011-02-28 17:03:57 +0530253 * we do it during open time instead of
254 * page dirty time via write_begin/page_mkwrite
255 * because we want write after unlink usecase
256 * to work.
257 */
258 inode_fid = v9fs_writeback_fid(dentry);
259 if (IS_ERR(inode_fid)) {
260 err = PTR_ERR(inode_fid);
Aneesh Kumar K.V5a7e0a82011-03-08 16:39:46 +0530261 mutex_unlock(&v9inode->v_mutex);
Aneesh Kumar K.V398c4f02011-05-20 18:55:51 +0000262 goto err_clunk_old_fid;
Aneesh Kumar K.V3cf387d2011-02-28 17:03:57 +0530263 }
Aneesh Kumar K.V6b39f6d2011-02-28 17:04:03 +0530264 v9inode->writeback_fid = (void *) inode_fid;
Aneesh Kumar K.V3cf387d2011-02-28 17:03:57 +0530265 }
Aneesh Kumar K.V5a7e0a82011-03-08 16:39:46 +0530266 mutex_unlock(&v9inode->v_mutex);
Aneesh Kumar K.Vaf7542f2011-01-10 14:22:21 -0600267 /* Since we are opening a file, assign the open fid to the file */
268 filp = lookup_instantiate_filp(nd, dentry, generic_file_open);
269 if (IS_ERR(filp)) {
Aneesh Kumar K.V398c4f02011-05-20 18:55:51 +0000270 err = PTR_ERR(filp);
271 goto err_clunk_old_fid;
Aneesh Kumar K.Vaf7542f2011-01-10 14:22:21 -0600272 }
273 filp->private_data = ofid;
Aneesh Kumar K.V46848de2011-02-28 17:03:55 +0530274#ifdef CONFIG_9P_FSCACHE
275 if (v9ses->cache)
276 v9fs_cache_inode_set_cookie(inode, filp);
277#endif
Aneesh Kumar K.V53c06f42011-01-10 13:51:47 -0600278 return 0;
279
280error:
Aneesh Kumar K.V53c06f42011-01-10 13:51:47 -0600281 if (fid)
282 p9_client_clunk(fid);
Aneesh Kumar K.V398c4f02011-05-20 18:55:51 +0000283err_clunk_old_fid:
284 if (ofid)
285 p9_client_clunk(ofid);
Al Viro1ec95bf2011-07-23 02:28:13 -0400286 v9fs_set_create_acl(NULL, &dacl, &pacl);
Aneesh Kumar K.V53c06f42011-01-10 13:51:47 -0600287 return err;
288}
289
290/**
291 * v9fs_vfs_mkdir_dotl - VFS mkdir hook to create a directory
292 * @dir: inode that is being unlinked
293 * @dentry: dentry that is being unlinked
294 * @mode: mode for new directory
295 *
296 */
297
298static int v9fs_vfs_mkdir_dotl(struct inode *dir,
299 struct dentry *dentry, int omode)
300{
301 int err;
302 struct v9fs_session_info *v9ses;
303 struct p9_fid *fid = NULL, *dfid = NULL;
304 gid_t gid;
305 char *name;
306 mode_t mode;
307 struct inode *inode;
308 struct p9_qid qid;
309 struct dentry *dir_dentry;
310 struct posix_acl *dacl = NULL, *pacl = NULL;
311
312 P9_DPRINTK(P9_DEBUG_VFS, "name %s\n", dentry->d_name.name);
313 err = 0;
314 v9ses = v9fs_inode2v9ses(dir);
315
316 omode |= S_IFDIR;
317 if (dir->i_mode & S_ISGID)
318 omode |= S_ISGID;
319
320 dir_dentry = v9fs_dentry_from_dir_inode(dir);
321 dfid = v9fs_fid_lookup(dir_dentry);
322 if (IS_ERR(dfid)) {
323 err = PTR_ERR(dfid);
324 P9_DPRINTK(P9_DEBUG_VFS, "fid lookup failed %d\n", err);
325 dfid = NULL;
326 goto error;
327 }
328
329 gid = v9fs_get_fsgid_for_create(dir);
330 mode = omode;
331 /* Update mode based on ACL value */
332 err = v9fs_acl_mode(dir, &mode, &dacl, &pacl);
333 if (err) {
334 P9_DPRINTK(P9_DEBUG_VFS,
335 "Failed to get acl values in mkdir %d\n", err);
336 goto error;
337 }
338 name = (char *) dentry->d_name.name;
339 err = p9_client_mkdir_dotl(dfid, name, mode, gid, &qid);
340 if (err < 0)
341 goto error;
342
343 /* instantiate inode and assign the unopened fid to the dentry */
344 if (v9ses->cache == CACHE_LOOSE || v9ses->cache == CACHE_FSCACHE) {
345 fid = p9_client_walk(dfid, 1, &name, 1);
346 if (IS_ERR(fid)) {
347 err = PTR_ERR(fid);
348 P9_DPRINTK(P9_DEBUG_VFS, "p9_client_walk failed %d\n",
349 err);
350 fid = NULL;
351 goto error;
352 }
353
Aneesh Kumar K.Va78ce052011-02-28 17:04:02 +0530354 inode = v9fs_get_inode_from_fid(v9ses, fid, dir->i_sb);
Aneesh Kumar K.V53c06f42011-01-10 13:51:47 -0600355 if (IS_ERR(inode)) {
356 err = PTR_ERR(inode);
357 P9_DPRINTK(P9_DEBUG_VFS, "inode creation failed %d\n",
358 err);
359 goto error;
360 }
Aneesh Kumar K.V53c06f42011-01-10 13:51:47 -0600361 d_instantiate(dentry, inode);
362 err = v9fs_fid_add(dentry, fid);
363 if (err < 0)
364 goto error;
365 fid = NULL;
366 } else {
367 /*
368 * Not in cached mode. No need to populate
369 * inode with stat. We need to get an inode
370 * so that we can set the acl with dentry
371 */
372 inode = v9fs_get_inode(dir->i_sb, mode);
373 if (IS_ERR(inode)) {
374 err = PTR_ERR(inode);
375 goto error;
376 }
Aneesh Kumar K.V53c06f42011-01-10 13:51:47 -0600377 d_instantiate(dentry, inode);
378 }
379 /* Now set the ACL based on the default value */
Al Viro1ec95bf2011-07-23 02:28:13 -0400380 v9fs_set_create_acl(dentry, &dacl, &pacl);
Aneesh Kumar K.Vb271ec42011-02-28 17:04:05 +0530381 inc_nlink(dir);
Aneesh Kumar K.Vd28c61f2011-02-28 17:04:08 +0530382 v9fs_invalidate_inode_attr(dir);
Aneesh Kumar K.V53c06f42011-01-10 13:51:47 -0600383error:
384 if (fid)
385 p9_client_clunk(fid);
Al Viro1ec95bf2011-07-23 02:28:13 -0400386 v9fs_set_create_acl(NULL, &dacl, &pacl);
Aneesh Kumar K.V53c06f42011-01-10 13:51:47 -0600387 return err;
388}
389
390static int
391v9fs_vfs_getattr_dotl(struct vfsmount *mnt, struct dentry *dentry,
392 struct kstat *stat)
393{
394 int err;
395 struct v9fs_session_info *v9ses;
396 struct p9_fid *fid;
397 struct p9_stat_dotl *st;
398
399 P9_DPRINTK(P9_DEBUG_VFS, "dentry: %p\n", dentry);
400 err = -EPERM;
Aneesh Kumar K.V42869c82011-03-08 16:39:50 +0530401 v9ses = v9fs_dentry2v9ses(dentry);
Aneesh Kumar K.Va1211902011-02-28 17:04:01 +0530402 if (v9ses->cache == CACHE_LOOSE || v9ses->cache == CACHE_FSCACHE) {
403 generic_fillattr(dentry->d_inode, stat);
404 return 0;
405 }
Aneesh Kumar K.V53c06f42011-01-10 13:51:47 -0600406 fid = v9fs_fid_lookup(dentry);
407 if (IS_ERR(fid))
408 return PTR_ERR(fid);
409
410 /* Ask for all the fields in stat structure. Server will return
411 * whatever it supports
412 */
413
414 st = p9_client_getattr_dotl(fid, P9_STATS_ALL);
415 if (IS_ERR(st))
416 return PTR_ERR(st);
417
418 v9fs_stat2inode_dotl(st, dentry->d_inode);
419 generic_fillattr(dentry->d_inode, stat);
420 /* Change block size to what the server returned */
421 stat->blksize = st->st_blksize;
422
423 kfree(st);
424 return 0;
425}
426
427/**
428 * v9fs_vfs_setattr_dotl - set file metadata
429 * @dentry: file whose metadata to set
430 * @iattr: metadata assignment structure
431 *
432 */
433
434int v9fs_vfs_setattr_dotl(struct dentry *dentry, struct iattr *iattr)
435{
436 int retval;
437 struct v9fs_session_info *v9ses;
438 struct p9_fid *fid;
439 struct p9_iattr_dotl p9attr;
440
441 P9_DPRINTK(P9_DEBUG_VFS, "\n");
442
443 retval = inode_change_ok(dentry->d_inode, iattr);
444 if (retval)
445 return retval;
446
447 p9attr.valid = iattr->ia_valid;
448 p9attr.mode = iattr->ia_mode;
449 p9attr.uid = iattr->ia_uid;
450 p9attr.gid = iattr->ia_gid;
451 p9attr.size = iattr->ia_size;
452 p9attr.atime_sec = iattr->ia_atime.tv_sec;
453 p9attr.atime_nsec = iattr->ia_atime.tv_nsec;
454 p9attr.mtime_sec = iattr->ia_mtime.tv_sec;
455 p9attr.mtime_nsec = iattr->ia_mtime.tv_nsec;
456
457 retval = -EPERM;
Aneesh Kumar K.V42869c82011-03-08 16:39:50 +0530458 v9ses = v9fs_dentry2v9ses(dentry);
Aneesh Kumar K.V53c06f42011-01-10 13:51:47 -0600459 fid = v9fs_fid_lookup(dentry);
460 if (IS_ERR(fid))
461 return PTR_ERR(fid);
462
Aneesh Kumar K.V3dc54362011-02-28 17:04:11 +0530463 /* Write all dirty data */
464 if (S_ISREG(dentry->d_inode->i_mode))
465 filemap_write_and_wait(dentry->d_inode->i_mapping);
466
Aneesh Kumar K.Vf10fc502011-02-28 17:04:10 +0530467 retval = p9_client_setattr(fid, &p9attr);
468 if (retval < 0)
469 return retval;
Aneesh Kumar K.V53c06f42011-01-10 13:51:47 -0600470
Aneesh Kumar K.V059c1382011-03-08 16:39:48 +0530471 if ((iattr->ia_valid & ATTR_SIZE) &&
472 iattr->ia_size != i_size_read(dentry->d_inode))
473 truncate_setsize(dentry->d_inode, iattr->ia_size);
474
475 v9fs_invalidate_inode_attr(dentry->d_inode);
Aneesh Kumar K.V53c06f42011-01-10 13:51:47 -0600476 setattr_copy(dentry->d_inode, iattr);
477 mark_inode_dirty(dentry->d_inode);
478 if (iattr->ia_valid & ATTR_MODE) {
479 /* We also want to update ACL when we update mode bits */
480 retval = v9fs_acl_chmod(dentry);
481 if (retval < 0)
482 return retval;
483 }
484 return 0;
485}
486
487/**
488 * v9fs_stat2inode_dotl - populate an inode structure with stat info
489 * @stat: stat structure
490 * @inode: inode to populate
491 * @sb: superblock of filesystem
492 *
493 */
494
495void
496v9fs_stat2inode_dotl(struct p9_stat_dotl *stat, struct inode *inode)
497{
Aneesh Kumar K.Vb3cbea02011-02-28 17:04:06 +0530498 struct v9fs_inode *v9inode = V9FS_I(inode);
Aneesh Kumar K.V53c06f42011-01-10 13:51:47 -0600499
500 if ((stat->st_result_mask & P9_STATS_BASIC) == P9_STATS_BASIC) {
501 inode->i_atime.tv_sec = stat->st_atime_sec;
502 inode->i_atime.tv_nsec = stat->st_atime_nsec;
503 inode->i_mtime.tv_sec = stat->st_mtime_sec;
504 inode->i_mtime.tv_nsec = stat->st_mtime_nsec;
505 inode->i_ctime.tv_sec = stat->st_ctime_sec;
506 inode->i_ctime.tv_nsec = stat->st_ctime_nsec;
507 inode->i_uid = stat->st_uid;
508 inode->i_gid = stat->st_gid;
509 inode->i_nlink = stat->st_nlink;
510 inode->i_mode = stat->st_mode;
511 inode->i_rdev = new_decode_dev(stat->st_rdev);
512
513 if ((S_ISBLK(inode->i_mode)) || (S_ISCHR(inode->i_mode)))
514 init_special_inode(inode, inode->i_mode, inode->i_rdev);
515
516 i_size_write(inode, stat->st_size);
517 inode->i_blocks = stat->st_blocks;
518 } else {
519 if (stat->st_result_mask & P9_STATS_ATIME) {
520 inode->i_atime.tv_sec = stat->st_atime_sec;
521 inode->i_atime.tv_nsec = stat->st_atime_nsec;
522 }
523 if (stat->st_result_mask & P9_STATS_MTIME) {
524 inode->i_mtime.tv_sec = stat->st_mtime_sec;
525 inode->i_mtime.tv_nsec = stat->st_mtime_nsec;
526 }
527 if (stat->st_result_mask & P9_STATS_CTIME) {
528 inode->i_ctime.tv_sec = stat->st_ctime_sec;
529 inode->i_ctime.tv_nsec = stat->st_ctime_nsec;
530 }
531 if (stat->st_result_mask & P9_STATS_UID)
532 inode->i_uid = stat->st_uid;
533 if (stat->st_result_mask & P9_STATS_GID)
534 inode->i_gid = stat->st_gid;
535 if (stat->st_result_mask & P9_STATS_NLINK)
536 inode->i_nlink = stat->st_nlink;
537 if (stat->st_result_mask & P9_STATS_MODE) {
538 inode->i_mode = stat->st_mode;
539 if ((S_ISBLK(inode->i_mode)) ||
540 (S_ISCHR(inode->i_mode)))
541 init_special_inode(inode, inode->i_mode,
542 inode->i_rdev);
543 }
544 if (stat->st_result_mask & P9_STATS_RDEV)
545 inode->i_rdev = new_decode_dev(stat->st_rdev);
546 if (stat->st_result_mask & P9_STATS_SIZE)
547 i_size_write(inode, stat->st_size);
548 if (stat->st_result_mask & P9_STATS_BLOCKS)
549 inode->i_blocks = stat->st_blocks;
550 }
551 if (stat->st_result_mask & P9_STATS_GEN)
552 inode->i_generation = stat->st_gen;
553
554 /* Currently we don't support P9_STATS_BTIME and P9_STATS_DATA_VERSION
555 * because the inode structure does not have fields for them.
556 */
Aneesh Kumar K.Vb3cbea02011-02-28 17:04:06 +0530557 v9inode->cache_validity &= ~V9FS_INO_INVALID_ATTR;
Aneesh Kumar K.V53c06f42011-01-10 13:51:47 -0600558}
559
560static int
561v9fs_vfs_symlink_dotl(struct inode *dir, struct dentry *dentry,
562 const char *symname)
563{
Aneesh Kumar K.V53c06f42011-01-10 13:51:47 -0600564 int err;
565 gid_t gid;
Aneesh Kumar K.Vd28c61f2011-02-28 17:04:08 +0530566 char *name;
567 struct p9_qid qid;
568 struct inode *inode;
569 struct p9_fid *dfid;
570 struct p9_fid *fid = NULL;
571 struct v9fs_session_info *v9ses;
Aneesh Kumar K.V53c06f42011-01-10 13:51:47 -0600572
573 name = (char *) dentry->d_name.name;
574 P9_DPRINTK(P9_DEBUG_VFS, "v9fs_vfs_symlink_dotl : %lu,%s,%s\n",
575 dir->i_ino, name, symname);
576 v9ses = v9fs_inode2v9ses(dir);
577
578 dfid = v9fs_fid_lookup(dentry->d_parent);
579 if (IS_ERR(dfid)) {
580 err = PTR_ERR(dfid);
581 P9_DPRINTK(P9_DEBUG_VFS, "fid lookup failed %d\n", err);
582 return err;
583 }
584
585 gid = v9fs_get_fsgid_for_create(dir);
586
587 /* Server doesn't alter fid on TSYMLINK. Hence no need to clone it. */
588 err = p9_client_symlink(dfid, name, (char *)symname, gid, &qid);
589
590 if (err < 0) {
591 P9_DPRINTK(P9_DEBUG_VFS, "p9_client_symlink failed %d\n", err);
592 goto error;
593 }
594
Aneesh Kumar K.Vd28c61f2011-02-28 17:04:08 +0530595 v9fs_invalidate_inode_attr(dir);
Aneesh Kumar K.V53c06f42011-01-10 13:51:47 -0600596 if (v9ses->cache) {
597 /* Now walk from the parent so we can get an unopened fid. */
598 fid = p9_client_walk(dfid, 1, &name, 1);
599 if (IS_ERR(fid)) {
600 err = PTR_ERR(fid);
601 P9_DPRINTK(P9_DEBUG_VFS, "p9_client_walk failed %d\n",
602 err);
603 fid = NULL;
604 goto error;
605 }
606
607 /* instantiate inode and assign the unopened fid to dentry */
Aneesh Kumar K.Va78ce052011-02-28 17:04:02 +0530608 inode = v9fs_get_inode_from_fid(v9ses, fid, dir->i_sb);
Aneesh Kumar K.V53c06f42011-01-10 13:51:47 -0600609 if (IS_ERR(inode)) {
610 err = PTR_ERR(inode);
611 P9_DPRINTK(P9_DEBUG_VFS, "inode creation failed %d\n",
612 err);
613 goto error;
614 }
Aneesh Kumar K.V53c06f42011-01-10 13:51:47 -0600615 d_instantiate(dentry, inode);
616 err = v9fs_fid_add(dentry, fid);
617 if (err < 0)
618 goto error;
619 fid = NULL;
620 } else {
621 /* Not in cached mode. No need to populate inode with stat */
622 inode = v9fs_get_inode(dir->i_sb, S_IFLNK);
623 if (IS_ERR(inode)) {
624 err = PTR_ERR(inode);
625 goto error;
626 }
Aneesh Kumar K.V53c06f42011-01-10 13:51:47 -0600627 d_instantiate(dentry, inode);
628 }
629
630error:
631 if (fid)
632 p9_client_clunk(fid);
633
634 return err;
635}
636
637/**
638 * v9fs_vfs_link_dotl - create a hardlink for dotl
639 * @old_dentry: dentry for file to link to
640 * @dir: inode destination for new link
641 * @dentry: dentry for link
642 *
643 */
644
645static int
646v9fs_vfs_link_dotl(struct dentry *old_dentry, struct inode *dir,
647 struct dentry *dentry)
648{
649 int err;
Aneesh Kumar K.V53c06f42011-01-10 13:51:47 -0600650 char *name;
Aneesh Kumar K.V53c06f42011-01-10 13:51:47 -0600651 struct dentry *dir_dentry;
Aneesh Kumar K.Vd28c61f2011-02-28 17:04:08 +0530652 struct p9_fid *dfid, *oldfid;
653 struct v9fs_session_info *v9ses;
Aneesh Kumar K.V53c06f42011-01-10 13:51:47 -0600654
655 P9_DPRINTK(P9_DEBUG_VFS, "dir ino: %lu, old_name: %s, new_name: %s\n",
656 dir->i_ino, old_dentry->d_name.name,
657 dentry->d_name.name);
658
659 v9ses = v9fs_inode2v9ses(dir);
660 dir_dentry = v9fs_dentry_from_dir_inode(dir);
661 dfid = v9fs_fid_lookup(dir_dentry);
662 if (IS_ERR(dfid))
663 return PTR_ERR(dfid);
664
665 oldfid = v9fs_fid_lookup(old_dentry);
666 if (IS_ERR(oldfid))
667 return PTR_ERR(oldfid);
668
669 name = (char *) dentry->d_name.name;
670
671 err = p9_client_link(dfid, oldfid, (char *)dentry->d_name.name);
672
673 if (err < 0) {
674 P9_DPRINTK(P9_DEBUG_VFS, "p9_client_link failed %d\n", err);
675 return err;
676 }
677
Aneesh Kumar K.Vd28c61f2011-02-28 17:04:08 +0530678 v9fs_invalidate_inode_attr(dir);
Aneesh Kumar K.V53c06f42011-01-10 13:51:47 -0600679 if (v9ses->cache == CACHE_LOOSE || v9ses->cache == CACHE_FSCACHE) {
680 /* Get the latest stat info from server. */
681 struct p9_fid *fid;
Aneesh Kumar K.V53c06f42011-01-10 13:51:47 -0600682 fid = v9fs_fid_lookup(old_dentry);
683 if (IS_ERR(fid))
684 return PTR_ERR(fid);
685
Aneesh Kumar K.Vc06c0662011-02-28 17:04:09 +0530686 v9fs_refresh_inode_dotl(fid, old_dentry->d_inode);
Aneesh Kumar K.V53c06f42011-01-10 13:51:47 -0600687 }
Aneesh Kumar K.V20656a42011-02-28 17:03:55 +0530688 ihold(old_dentry->d_inode);
Aneesh Kumar K.V53c06f42011-01-10 13:51:47 -0600689 d_instantiate(dentry, old_dentry->d_inode);
690
691 return err;
692}
693
694/**
695 * v9fs_vfs_mknod_dotl - create a special file
696 * @dir: inode destination for new link
697 * @dentry: dentry for file
698 * @mode: mode for creation
699 * @rdev: device associated with special file
700 *
701 */
702static int
703v9fs_vfs_mknod_dotl(struct inode *dir, struct dentry *dentry, int omode,
704 dev_t rdev)
705{
706 int err;
Aneesh Kumar K.Vd28c61f2011-02-28 17:04:08 +0530707 gid_t gid;
Aneesh Kumar K.V53c06f42011-01-10 13:51:47 -0600708 char *name;
709 mode_t mode;
710 struct v9fs_session_info *v9ses;
711 struct p9_fid *fid = NULL, *dfid = NULL;
712 struct inode *inode;
Aneesh Kumar K.V53c06f42011-01-10 13:51:47 -0600713 struct p9_qid qid;
714 struct dentry *dir_dentry;
715 struct posix_acl *dacl = NULL, *pacl = NULL;
716
717 P9_DPRINTK(P9_DEBUG_VFS,
718 " %lu,%s mode: %x MAJOR: %u MINOR: %u\n", dir->i_ino,
719 dentry->d_name.name, omode, MAJOR(rdev), MINOR(rdev));
720
721 if (!new_valid_dev(rdev))
722 return -EINVAL;
723
724 v9ses = v9fs_inode2v9ses(dir);
725 dir_dentry = v9fs_dentry_from_dir_inode(dir);
726 dfid = v9fs_fid_lookup(dir_dentry);
727 if (IS_ERR(dfid)) {
728 err = PTR_ERR(dfid);
729 P9_DPRINTK(P9_DEBUG_VFS, "fid lookup failed %d\n", err);
730 dfid = NULL;
731 goto error;
732 }
733
734 gid = v9fs_get_fsgid_for_create(dir);
735 mode = omode;
736 /* Update mode based on ACL value */
737 err = v9fs_acl_mode(dir, &mode, &dacl, &pacl);
738 if (err) {
739 P9_DPRINTK(P9_DEBUG_VFS,
740 "Failed to get acl values in mknod %d\n", err);
741 goto error;
742 }
743 name = (char *) dentry->d_name.name;
744
745 err = p9_client_mknod_dotl(dfid, name, mode, rdev, gid, &qid);
746 if (err < 0)
747 goto error;
748
Aneesh Kumar K.Vd28c61f2011-02-28 17:04:08 +0530749 v9fs_invalidate_inode_attr(dir);
Aneesh Kumar K.V53c06f42011-01-10 13:51:47 -0600750 /* instantiate inode and assign the unopened fid to the dentry */
751 if (v9ses->cache == CACHE_LOOSE || v9ses->cache == CACHE_FSCACHE) {
752 fid = p9_client_walk(dfid, 1, &name, 1);
753 if (IS_ERR(fid)) {
754 err = PTR_ERR(fid);
755 P9_DPRINTK(P9_DEBUG_VFS, "p9_client_walk failed %d\n",
756 err);
757 fid = NULL;
758 goto error;
759 }
760
Aneesh Kumar K.Va78ce052011-02-28 17:04:02 +0530761 inode = v9fs_get_inode_from_fid(v9ses, fid, dir->i_sb);
Aneesh Kumar K.V53c06f42011-01-10 13:51:47 -0600762 if (IS_ERR(inode)) {
763 err = PTR_ERR(inode);
764 P9_DPRINTK(P9_DEBUG_VFS, "inode creation failed %d\n",
765 err);
766 goto error;
767 }
Aneesh Kumar K.V53c06f42011-01-10 13:51:47 -0600768 d_instantiate(dentry, inode);
769 err = v9fs_fid_add(dentry, fid);
770 if (err < 0)
771 goto error;
772 fid = NULL;
773 } else {
774 /*
775 * Not in cached mode. No need to populate inode with stat.
776 * socket syscall returns a fd, so we need instantiate
777 */
778 inode = v9fs_get_inode(dir->i_sb, mode);
779 if (IS_ERR(inode)) {
780 err = PTR_ERR(inode);
781 goto error;
782 }
Aneesh Kumar K.V53c06f42011-01-10 13:51:47 -0600783 d_instantiate(dentry, inode);
784 }
785 /* Now set the ACL based on the default value */
Al Viro1ec95bf2011-07-23 02:28:13 -0400786 v9fs_set_create_acl(dentry, &dacl, &pacl);
Aneesh Kumar K.V53c06f42011-01-10 13:51:47 -0600787error:
788 if (fid)
789 p9_client_clunk(fid);
Al Viro1ec95bf2011-07-23 02:28:13 -0400790 v9fs_set_create_acl(NULL, &dacl, &pacl);
Aneesh Kumar K.V53c06f42011-01-10 13:51:47 -0600791 return err;
792}
793
Aneesh Kumar K.V53c06f42011-01-10 13:51:47 -0600794/**
795 * v9fs_vfs_follow_link_dotl - follow a symlink path
796 * @dentry: dentry for symlink
797 * @nd: nameidata
798 *
799 */
800
801static void *
802v9fs_vfs_follow_link_dotl(struct dentry *dentry, struct nameidata *nd)
803{
M. Mohan Kumar31b6cea2011-01-08 07:28:46 +0530804 int retval;
805 struct p9_fid *fid;
Aneesh Kumar K.V53c06f42011-01-10 13:51:47 -0600806 char *link = __getname();
M. Mohan Kumar31b6cea2011-01-08 07:28:46 +0530807 char *target;
Aneesh Kumar K.V53c06f42011-01-10 13:51:47 -0600808
M. Mohan Kumar31b6cea2011-01-08 07:28:46 +0530809 P9_DPRINTK(P9_DEBUG_VFS, "%s\n", dentry->d_name.name);
Aneesh Kumar K.V53c06f42011-01-10 13:51:47 -0600810
M. Mohan Kumar31b6cea2011-01-08 07:28:46 +0530811 if (!link) {
Aneesh Kumar K.V53c06f42011-01-10 13:51:47 -0600812 link = ERR_PTR(-ENOMEM);
M. Mohan Kumar31b6cea2011-01-08 07:28:46 +0530813 goto ndset;
Aneesh Kumar K.V53c06f42011-01-10 13:51:47 -0600814 }
M. Mohan Kumar31b6cea2011-01-08 07:28:46 +0530815 fid = v9fs_fid_lookup(dentry);
816 if (IS_ERR(fid)) {
817 __putname(link);
Aneesh Kumar K.V936bb2d2011-03-24 23:04:41 +0530818 link = ERR_CAST(fid);
M. Mohan Kumar31b6cea2011-01-08 07:28:46 +0530819 goto ndset;
820 }
821 retval = p9_client_readlink(fid, &target);
822 if (!retval) {
823 strcpy(link, target);
824 kfree(target);
825 goto ndset;
826 }
827 __putname(link);
828 link = ERR_PTR(retval);
829ndset:
Aneesh Kumar K.V53c06f42011-01-10 13:51:47 -0600830 nd_set_link(nd, link);
Aneesh Kumar K.V53c06f42011-01-10 13:51:47 -0600831 return NULL;
832}
833
Aneesh Kumar K.Vb3cbea02011-02-28 17:04:06 +0530834int v9fs_refresh_inode_dotl(struct p9_fid *fid, struct inode *inode)
835{
836 loff_t i_size;
837 struct p9_stat_dotl *st;
838 struct v9fs_session_info *v9ses;
839
840 v9ses = v9fs_inode2v9ses(inode);
841 st = p9_client_getattr_dotl(fid, P9_STATS_ALL);
842 if (IS_ERR(st))
843 return PTR_ERR(st);
844
845 spin_lock(&inode->i_lock);
846 /*
847 * We don't want to refresh inode->i_size,
848 * because we may have cached data
849 */
850 i_size = inode->i_size;
851 v9fs_stat2inode_dotl(st, inode);
852 if (v9ses->cache)
853 inode->i_size = i_size;
854 spin_unlock(&inode->i_lock);
855 kfree(st);
856 return 0;
857}
858
Aneesh Kumar K.V53c06f42011-01-10 13:51:47 -0600859const struct inode_operations v9fs_dir_inode_operations_dotl = {
860 .create = v9fs_vfs_create_dotl,
861 .lookup = v9fs_vfs_lookup,
862 .link = v9fs_vfs_link_dotl,
863 .symlink = v9fs_vfs_symlink_dotl,
864 .unlink = v9fs_vfs_unlink,
865 .mkdir = v9fs_vfs_mkdir_dotl,
866 .rmdir = v9fs_vfs_rmdir,
867 .mknod = v9fs_vfs_mknod_dotl,
868 .rename = v9fs_vfs_rename,
869 .getattr = v9fs_vfs_getattr_dotl,
870 .setattr = v9fs_vfs_setattr_dotl,
871 .setxattr = generic_setxattr,
872 .getxattr = generic_getxattr,
873 .removexattr = generic_removexattr,
874 .listxattr = v9fs_listxattr,
Christoph Hellwig4e34e712011-07-23 17:37:31 +0200875 .get_acl = v9fs_iop_get_acl,
Aneesh Kumar K.V53c06f42011-01-10 13:51:47 -0600876};
877
878const struct inode_operations v9fs_file_inode_operations_dotl = {
879 .getattr = v9fs_vfs_getattr_dotl,
880 .setattr = v9fs_vfs_setattr_dotl,
881 .setxattr = generic_setxattr,
882 .getxattr = generic_getxattr,
883 .removexattr = generic_removexattr,
884 .listxattr = v9fs_listxattr,
Christoph Hellwig4e34e712011-07-23 17:37:31 +0200885 .get_acl = v9fs_iop_get_acl,
Aneesh Kumar K.V53c06f42011-01-10 13:51:47 -0600886};
887
888const struct inode_operations v9fs_symlink_inode_operations_dotl = {
M. Mohan Kumar31b6cea2011-01-08 07:28:46 +0530889 .readlink = generic_readlink,
Aneesh Kumar K.V53c06f42011-01-10 13:51:47 -0600890 .follow_link = v9fs_vfs_follow_link_dotl,
891 .put_link = v9fs_vfs_put_link,
892 .getattr = v9fs_vfs_getattr_dotl,
893 .setattr = v9fs_vfs_setattr_dotl,
894 .setxattr = generic_setxattr,
895 .getxattr = generic_getxattr,
896 .removexattr = generic_removexattr,
897 .listxattr = v9fs_listxattr,
898};