blob: 6f62320eda89c951121e8e90389c1319e4b318ee [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
116 v9fs_vcookie_set_qid(inode, &st->qid);
117 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 *
133v9fs_inode_dotl(struct v9fs_session_info *v9ses, struct p9_fid *fid,
134 struct super_block *sb)
135{
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;
162 char *name = NULL;
163 gid_t gid;
164 int flags;
165 mode_t mode;
166 struct v9fs_session_info *v9ses;
167 struct p9_fid *fid = NULL;
Aneesh Kumar K.V3cf387d2011-02-28 17:03:57 +0530168 struct p9_fid *dfid, *ofid, *inode_fid;
Aneesh Kumar K.V53c06f42011-01-10 13:51:47 -0600169 struct file *filp;
170 struct p9_qid qid;
171 struct inode *inode;
172 struct posix_acl *pacl = NULL, *dacl = NULL;
173
174 v9ses = v9fs_inode2v9ses(dir);
175 if (nd && nd->flags & LOOKUP_OPEN)
176 flags = nd->intent.open.flags - 1;
177 else {
178 /*
179 * create call without LOOKUP_OPEN is due
180 * to mknod of regular files. So use mknod
181 * operation.
182 */
183 return v9fs_vfs_mknod_dotl(dir, dentry, omode, 0);
184 }
185
186 name = (char *) dentry->d_name.name;
187 P9_DPRINTK(P9_DEBUG_VFS, "v9fs_vfs_create_dotl: name:%s flags:0x%x "
188 "mode:0x%x\n", name, flags, omode);
189
190 dfid = v9fs_fid_lookup(dentry->d_parent);
191 if (IS_ERR(dfid)) {
192 err = PTR_ERR(dfid);
193 P9_DPRINTK(P9_DEBUG_VFS, "fid lookup failed %d\n", err);
194 return err;
195 }
196
197 /* clone a fid to use for creation */
198 ofid = p9_client_walk(dfid, 0, NULL, 1);
199 if (IS_ERR(ofid)) {
200 err = PTR_ERR(ofid);
201 P9_DPRINTK(P9_DEBUG_VFS, "p9_client_walk failed %d\n", err);
202 return err;
203 }
204
205 gid = v9fs_get_fsgid_for_create(dir);
206
207 mode = omode;
208 /* Update mode based on ACL value */
209 err = v9fs_acl_mode(dir, &mode, &dacl, &pacl);
210 if (err) {
211 P9_DPRINTK(P9_DEBUG_VFS,
212 "Failed to get acl values in creat %d\n", err);
213 goto error;
214 }
215 err = p9_client_create_dotl(ofid, name, flags, mode, gid, &qid);
216 if (err < 0) {
217 P9_DPRINTK(P9_DEBUG_VFS,
218 "p9_client_open_dotl failed in creat %d\n",
219 err);
220 goto error;
221 }
Aneesh Kumar K.V53c06f42011-01-10 13:51:47 -0600222
Aneesh Kumar K.Vaf7542f2011-01-10 14:22:21 -0600223 /* instantiate inode and assign the unopened fid to the dentry */
224 fid = p9_client_walk(dfid, 1, &name, 1);
225 if (IS_ERR(fid)) {
226 err = PTR_ERR(fid);
Eric Van Hensbergenc25a61f2011-01-11 09:49:03 -0600227 P9_DPRINTK(P9_DEBUG_VFS, "p9_client_walk failed %d\n", err);
Aneesh Kumar K.V53c06f42011-01-10 13:51:47 -0600228 fid = NULL;
Aneesh Kumar K.Vaf7542f2011-01-10 14:22:21 -0600229 goto error;
Aneesh Kumar K.V53c06f42011-01-10 13:51:47 -0600230 }
Aneesh Kumar K.Vaf7542f2011-01-10 14:22:21 -0600231 inode = v9fs_inode_from_fid(v9ses, fid, dir->i_sb);
232 if (IS_ERR(inode)) {
233 err = PTR_ERR(inode);
234 P9_DPRINTK(P9_DEBUG_VFS, "inode creation failed %d\n", err);
235 goto error;
236 }
Aneesh Kumar K.Vaf7542f2011-01-10 14:22:21 -0600237 d_instantiate(dentry, inode);
238 err = v9fs_fid_add(dentry, fid);
239 if (err < 0)
240 goto error;
241
Aneesh Kumar K.V53c06f42011-01-10 13:51:47 -0600242 /* Now set the ACL based on the default value */
243 v9fs_set_create_acl(dentry, dacl, pacl);
Aneesh Kumar K.V3cf387d2011-02-28 17:03:57 +0530244 if (v9ses->cache && !inode->i_private) {
245 /*
246 * clone a fid and add it to inode->i_private
247 * we do it during open time instead of
248 * page dirty time via write_begin/page_mkwrite
249 * because we want write after unlink usecase
250 * to work.
251 */
252 inode_fid = v9fs_writeback_fid(dentry);
253 if (IS_ERR(inode_fid)) {
254 err = PTR_ERR(inode_fid);
255 goto error;
256 }
257 inode->i_private = (void *) inode_fid;
258 }
Aneesh Kumar K.Vaf7542f2011-01-10 14:22:21 -0600259 /* Since we are opening a file, assign the open fid to the file */
260 filp = lookup_instantiate_filp(nd, dentry, generic_file_open);
261 if (IS_ERR(filp)) {
Aneesh Kumar K.V53c06f42011-01-10 13:51:47 -0600262 p9_client_clunk(ofid);
Aneesh Kumar K.Vaf7542f2011-01-10 14:22:21 -0600263 return PTR_ERR(filp);
264 }
265 filp->private_data = ofid;
Aneesh Kumar K.V46848de2011-02-28 17:03:55 +0530266#ifdef CONFIG_9P_FSCACHE
267 if (v9ses->cache)
268 v9fs_cache_inode_set_cookie(inode, filp);
269#endif
Aneesh Kumar K.V53c06f42011-01-10 13:51:47 -0600270 return 0;
271
272error:
273 if (ofid)
274 p9_client_clunk(ofid);
275 if (fid)
276 p9_client_clunk(fid);
277 return err;
278}
279
280/**
281 * v9fs_vfs_mkdir_dotl - VFS mkdir hook to create a directory
282 * @dir: inode that is being unlinked
283 * @dentry: dentry that is being unlinked
284 * @mode: mode for new directory
285 *
286 */
287
288static int v9fs_vfs_mkdir_dotl(struct inode *dir,
289 struct dentry *dentry, int omode)
290{
291 int err;
292 struct v9fs_session_info *v9ses;
293 struct p9_fid *fid = NULL, *dfid = NULL;
294 gid_t gid;
295 char *name;
296 mode_t mode;
297 struct inode *inode;
298 struct p9_qid qid;
299 struct dentry *dir_dentry;
300 struct posix_acl *dacl = NULL, *pacl = NULL;
301
302 P9_DPRINTK(P9_DEBUG_VFS, "name %s\n", dentry->d_name.name);
303 err = 0;
304 v9ses = v9fs_inode2v9ses(dir);
305
306 omode |= S_IFDIR;
307 if (dir->i_mode & S_ISGID)
308 omode |= S_ISGID;
309
310 dir_dentry = v9fs_dentry_from_dir_inode(dir);
311 dfid = v9fs_fid_lookup(dir_dentry);
312 if (IS_ERR(dfid)) {
313 err = PTR_ERR(dfid);
314 P9_DPRINTK(P9_DEBUG_VFS, "fid lookup failed %d\n", err);
315 dfid = NULL;
316 goto error;
317 }
318
319 gid = v9fs_get_fsgid_for_create(dir);
320 mode = omode;
321 /* Update mode based on ACL value */
322 err = v9fs_acl_mode(dir, &mode, &dacl, &pacl);
323 if (err) {
324 P9_DPRINTK(P9_DEBUG_VFS,
325 "Failed to get acl values in mkdir %d\n", err);
326 goto error;
327 }
328 name = (char *) dentry->d_name.name;
329 err = p9_client_mkdir_dotl(dfid, name, mode, gid, &qid);
330 if (err < 0)
331 goto error;
332
333 /* instantiate inode and assign the unopened fid to the dentry */
334 if (v9ses->cache == CACHE_LOOSE || v9ses->cache == CACHE_FSCACHE) {
335 fid = p9_client_walk(dfid, 1, &name, 1);
336 if (IS_ERR(fid)) {
337 err = PTR_ERR(fid);
338 P9_DPRINTK(P9_DEBUG_VFS, "p9_client_walk failed %d\n",
339 err);
340 fid = NULL;
341 goto error;
342 }
343
344 inode = v9fs_inode_from_fid(v9ses, fid, dir->i_sb);
345 if (IS_ERR(inode)) {
346 err = PTR_ERR(inode);
347 P9_DPRINTK(P9_DEBUG_VFS, "inode creation failed %d\n",
348 err);
349 goto error;
350 }
Aneesh Kumar K.V53c06f42011-01-10 13:51:47 -0600351 d_instantiate(dentry, inode);
352 err = v9fs_fid_add(dentry, fid);
353 if (err < 0)
354 goto error;
355 fid = NULL;
356 } else {
357 /*
358 * Not in cached mode. No need to populate
359 * inode with stat. We need to get an inode
360 * so that we can set the acl with dentry
361 */
362 inode = v9fs_get_inode(dir->i_sb, mode);
363 if (IS_ERR(inode)) {
364 err = PTR_ERR(inode);
365 goto error;
366 }
Aneesh Kumar K.V53c06f42011-01-10 13:51:47 -0600367 d_instantiate(dentry, inode);
368 }
369 /* Now set the ACL based on the default value */
370 v9fs_set_create_acl(dentry, dacl, pacl);
371
372error:
373 if (fid)
374 p9_client_clunk(fid);
375 return err;
376}
377
378static int
379v9fs_vfs_getattr_dotl(struct vfsmount *mnt, struct dentry *dentry,
380 struct kstat *stat)
381{
382 int err;
383 struct v9fs_session_info *v9ses;
384 struct p9_fid *fid;
385 struct p9_stat_dotl *st;
386
387 P9_DPRINTK(P9_DEBUG_VFS, "dentry: %p\n", dentry);
388 err = -EPERM;
389 v9ses = v9fs_inode2v9ses(dentry->d_inode);
390 if (v9ses->cache == CACHE_LOOSE || v9ses->cache == CACHE_FSCACHE)
391 return simple_getattr(mnt, dentry, stat);
392
393 fid = v9fs_fid_lookup(dentry);
394 if (IS_ERR(fid))
395 return PTR_ERR(fid);
396
397 /* Ask for all the fields in stat structure. Server will return
398 * whatever it supports
399 */
400
401 st = p9_client_getattr_dotl(fid, P9_STATS_ALL);
402 if (IS_ERR(st))
403 return PTR_ERR(st);
404
405 v9fs_stat2inode_dotl(st, dentry->d_inode);
406 generic_fillattr(dentry->d_inode, stat);
407 /* Change block size to what the server returned */
408 stat->blksize = st->st_blksize;
409
410 kfree(st);
411 return 0;
412}
413
414/**
415 * v9fs_vfs_setattr_dotl - set file metadata
416 * @dentry: file whose metadata to set
417 * @iattr: metadata assignment structure
418 *
419 */
420
421int v9fs_vfs_setattr_dotl(struct dentry *dentry, struct iattr *iattr)
422{
423 int retval;
424 struct v9fs_session_info *v9ses;
425 struct p9_fid *fid;
426 struct p9_iattr_dotl p9attr;
427
428 P9_DPRINTK(P9_DEBUG_VFS, "\n");
429
430 retval = inode_change_ok(dentry->d_inode, iattr);
431 if (retval)
432 return retval;
433
434 p9attr.valid = iattr->ia_valid;
435 p9attr.mode = iattr->ia_mode;
436 p9attr.uid = iattr->ia_uid;
437 p9attr.gid = iattr->ia_gid;
438 p9attr.size = iattr->ia_size;
439 p9attr.atime_sec = iattr->ia_atime.tv_sec;
440 p9attr.atime_nsec = iattr->ia_atime.tv_nsec;
441 p9attr.mtime_sec = iattr->ia_mtime.tv_sec;
442 p9attr.mtime_nsec = iattr->ia_mtime.tv_nsec;
443
444 retval = -EPERM;
445 v9ses = v9fs_inode2v9ses(dentry->d_inode);
446 fid = v9fs_fid_lookup(dentry);
447 if (IS_ERR(fid))
448 return PTR_ERR(fid);
449
450 retval = p9_client_setattr(fid, &p9attr);
451 if (retval < 0)
452 return retval;
453
454 if ((iattr->ia_valid & ATTR_SIZE) &&
455 iattr->ia_size != i_size_read(dentry->d_inode)) {
456 retval = vmtruncate(dentry->d_inode, iattr->ia_size);
457 if (retval)
458 return retval;
459 }
460
461 setattr_copy(dentry->d_inode, iattr);
462 mark_inode_dirty(dentry->d_inode);
463 if (iattr->ia_valid & ATTR_MODE) {
464 /* We also want to update ACL when we update mode bits */
465 retval = v9fs_acl_chmod(dentry);
466 if (retval < 0)
467 return retval;
468 }
469 return 0;
470}
471
472/**
473 * v9fs_stat2inode_dotl - populate an inode structure with stat info
474 * @stat: stat structure
475 * @inode: inode to populate
476 * @sb: superblock of filesystem
477 *
478 */
479
480void
481v9fs_stat2inode_dotl(struct p9_stat_dotl *stat, struct inode *inode)
482{
483
484 if ((stat->st_result_mask & P9_STATS_BASIC) == P9_STATS_BASIC) {
485 inode->i_atime.tv_sec = stat->st_atime_sec;
486 inode->i_atime.tv_nsec = stat->st_atime_nsec;
487 inode->i_mtime.tv_sec = stat->st_mtime_sec;
488 inode->i_mtime.tv_nsec = stat->st_mtime_nsec;
489 inode->i_ctime.tv_sec = stat->st_ctime_sec;
490 inode->i_ctime.tv_nsec = stat->st_ctime_nsec;
491 inode->i_uid = stat->st_uid;
492 inode->i_gid = stat->st_gid;
493 inode->i_nlink = stat->st_nlink;
494 inode->i_mode = stat->st_mode;
495 inode->i_rdev = new_decode_dev(stat->st_rdev);
496
497 if ((S_ISBLK(inode->i_mode)) || (S_ISCHR(inode->i_mode)))
498 init_special_inode(inode, inode->i_mode, inode->i_rdev);
499
500 i_size_write(inode, stat->st_size);
501 inode->i_blocks = stat->st_blocks;
502 } else {
503 if (stat->st_result_mask & P9_STATS_ATIME) {
504 inode->i_atime.tv_sec = stat->st_atime_sec;
505 inode->i_atime.tv_nsec = stat->st_atime_nsec;
506 }
507 if (stat->st_result_mask & P9_STATS_MTIME) {
508 inode->i_mtime.tv_sec = stat->st_mtime_sec;
509 inode->i_mtime.tv_nsec = stat->st_mtime_nsec;
510 }
511 if (stat->st_result_mask & P9_STATS_CTIME) {
512 inode->i_ctime.tv_sec = stat->st_ctime_sec;
513 inode->i_ctime.tv_nsec = stat->st_ctime_nsec;
514 }
515 if (stat->st_result_mask & P9_STATS_UID)
516 inode->i_uid = stat->st_uid;
517 if (stat->st_result_mask & P9_STATS_GID)
518 inode->i_gid = stat->st_gid;
519 if (stat->st_result_mask & P9_STATS_NLINK)
520 inode->i_nlink = stat->st_nlink;
521 if (stat->st_result_mask & P9_STATS_MODE) {
522 inode->i_mode = stat->st_mode;
523 if ((S_ISBLK(inode->i_mode)) ||
524 (S_ISCHR(inode->i_mode)))
525 init_special_inode(inode, inode->i_mode,
526 inode->i_rdev);
527 }
528 if (stat->st_result_mask & P9_STATS_RDEV)
529 inode->i_rdev = new_decode_dev(stat->st_rdev);
530 if (stat->st_result_mask & P9_STATS_SIZE)
531 i_size_write(inode, stat->st_size);
532 if (stat->st_result_mask & P9_STATS_BLOCKS)
533 inode->i_blocks = stat->st_blocks;
534 }
535 if (stat->st_result_mask & P9_STATS_GEN)
536 inode->i_generation = stat->st_gen;
537
538 /* Currently we don't support P9_STATS_BTIME and P9_STATS_DATA_VERSION
539 * because the inode structure does not have fields for them.
540 */
541}
542
543static int
544v9fs_vfs_symlink_dotl(struct inode *dir, struct dentry *dentry,
545 const char *symname)
546{
547 struct v9fs_session_info *v9ses;
548 struct p9_fid *dfid;
549 struct p9_fid *fid = NULL;
550 struct inode *inode;
551 struct p9_qid qid;
552 char *name;
553 int err;
554 gid_t gid;
555
556 name = (char *) dentry->d_name.name;
557 P9_DPRINTK(P9_DEBUG_VFS, "v9fs_vfs_symlink_dotl : %lu,%s,%s\n",
558 dir->i_ino, name, symname);
559 v9ses = v9fs_inode2v9ses(dir);
560
561 dfid = v9fs_fid_lookup(dentry->d_parent);
562 if (IS_ERR(dfid)) {
563 err = PTR_ERR(dfid);
564 P9_DPRINTK(P9_DEBUG_VFS, "fid lookup failed %d\n", err);
565 return err;
566 }
567
568 gid = v9fs_get_fsgid_for_create(dir);
569
570 /* Server doesn't alter fid on TSYMLINK. Hence no need to clone it. */
571 err = p9_client_symlink(dfid, name, (char *)symname, gid, &qid);
572
573 if (err < 0) {
574 P9_DPRINTK(P9_DEBUG_VFS, "p9_client_symlink failed %d\n", err);
575 goto error;
576 }
577
578 if (v9ses->cache) {
579 /* Now walk from the parent so we can get an unopened fid. */
580 fid = p9_client_walk(dfid, 1, &name, 1);
581 if (IS_ERR(fid)) {
582 err = PTR_ERR(fid);
583 P9_DPRINTK(P9_DEBUG_VFS, "p9_client_walk failed %d\n",
584 err);
585 fid = NULL;
586 goto error;
587 }
588
589 /* instantiate inode and assign the unopened fid to dentry */
590 inode = v9fs_inode_from_fid(v9ses, fid, dir->i_sb);
591 if (IS_ERR(inode)) {
592 err = PTR_ERR(inode);
593 P9_DPRINTK(P9_DEBUG_VFS, "inode creation failed %d\n",
594 err);
595 goto error;
596 }
Aneesh Kumar K.V53c06f42011-01-10 13:51:47 -0600597 d_instantiate(dentry, inode);
598 err = v9fs_fid_add(dentry, fid);
599 if (err < 0)
600 goto error;
601 fid = NULL;
602 } else {
603 /* Not in cached mode. No need to populate inode with stat */
604 inode = v9fs_get_inode(dir->i_sb, S_IFLNK);
605 if (IS_ERR(inode)) {
606 err = PTR_ERR(inode);
607 goto error;
608 }
Aneesh Kumar K.V53c06f42011-01-10 13:51:47 -0600609 d_instantiate(dentry, inode);
610 }
611
612error:
613 if (fid)
614 p9_client_clunk(fid);
615
616 return err;
617}
618
619/**
620 * v9fs_vfs_link_dotl - create a hardlink for dotl
621 * @old_dentry: dentry for file to link to
622 * @dir: inode destination for new link
623 * @dentry: dentry for link
624 *
625 */
626
627static int
628v9fs_vfs_link_dotl(struct dentry *old_dentry, struct inode *dir,
629 struct dentry *dentry)
630{
631 int err;
632 struct p9_fid *dfid, *oldfid;
633 char *name;
634 struct v9fs_session_info *v9ses;
635 struct dentry *dir_dentry;
636
637 P9_DPRINTK(P9_DEBUG_VFS, "dir ino: %lu, old_name: %s, new_name: %s\n",
638 dir->i_ino, old_dentry->d_name.name,
639 dentry->d_name.name);
640
641 v9ses = v9fs_inode2v9ses(dir);
642 dir_dentry = v9fs_dentry_from_dir_inode(dir);
643 dfid = v9fs_fid_lookup(dir_dentry);
644 if (IS_ERR(dfid))
645 return PTR_ERR(dfid);
646
647 oldfid = v9fs_fid_lookup(old_dentry);
648 if (IS_ERR(oldfid))
649 return PTR_ERR(oldfid);
650
651 name = (char *) dentry->d_name.name;
652
653 err = p9_client_link(dfid, oldfid, (char *)dentry->d_name.name);
654
655 if (err < 0) {
656 P9_DPRINTK(P9_DEBUG_VFS, "p9_client_link failed %d\n", err);
657 return err;
658 }
659
660 if (v9ses->cache == CACHE_LOOSE || v9ses->cache == CACHE_FSCACHE) {
661 /* Get the latest stat info from server. */
662 struct p9_fid *fid;
663 struct p9_stat_dotl *st;
664
665 fid = v9fs_fid_lookup(old_dentry);
666 if (IS_ERR(fid))
667 return PTR_ERR(fid);
668
669 st = p9_client_getattr_dotl(fid, P9_STATS_BASIC);
670 if (IS_ERR(st))
671 return PTR_ERR(st);
672
673 v9fs_stat2inode_dotl(st, old_dentry->d_inode);
674
675 kfree(st);
Aneesh Kumar K.V53c06f42011-01-10 13:51:47 -0600676 }
Aneesh Kumar K.V20656a42011-02-28 17:03:55 +0530677 ihold(old_dentry->d_inode);
Aneesh Kumar K.V53c06f42011-01-10 13:51:47 -0600678 d_instantiate(dentry, old_dentry->d_inode);
679
680 return err;
681}
682
683/**
684 * v9fs_vfs_mknod_dotl - create a special file
685 * @dir: inode destination for new link
686 * @dentry: dentry for file
687 * @mode: mode for creation
688 * @rdev: device associated with special file
689 *
690 */
691static int
692v9fs_vfs_mknod_dotl(struct inode *dir, struct dentry *dentry, int omode,
693 dev_t rdev)
694{
695 int err;
696 char *name;
697 mode_t mode;
698 struct v9fs_session_info *v9ses;
699 struct p9_fid *fid = NULL, *dfid = NULL;
700 struct inode *inode;
701 gid_t gid;
702 struct p9_qid qid;
703 struct dentry *dir_dentry;
704 struct posix_acl *dacl = NULL, *pacl = NULL;
705
706 P9_DPRINTK(P9_DEBUG_VFS,
707 " %lu,%s mode: %x MAJOR: %u MINOR: %u\n", dir->i_ino,
708 dentry->d_name.name, omode, MAJOR(rdev), MINOR(rdev));
709
710 if (!new_valid_dev(rdev))
711 return -EINVAL;
712
713 v9ses = v9fs_inode2v9ses(dir);
714 dir_dentry = v9fs_dentry_from_dir_inode(dir);
715 dfid = v9fs_fid_lookup(dir_dentry);
716 if (IS_ERR(dfid)) {
717 err = PTR_ERR(dfid);
718 P9_DPRINTK(P9_DEBUG_VFS, "fid lookup failed %d\n", err);
719 dfid = NULL;
720 goto error;
721 }
722
723 gid = v9fs_get_fsgid_for_create(dir);
724 mode = omode;
725 /* Update mode based on ACL value */
726 err = v9fs_acl_mode(dir, &mode, &dacl, &pacl);
727 if (err) {
728 P9_DPRINTK(P9_DEBUG_VFS,
729 "Failed to get acl values in mknod %d\n", err);
730 goto error;
731 }
732 name = (char *) dentry->d_name.name;
733
734 err = p9_client_mknod_dotl(dfid, name, mode, rdev, gid, &qid);
735 if (err < 0)
736 goto error;
737
738 /* instantiate inode and assign the unopened fid to the dentry */
739 if (v9ses->cache == CACHE_LOOSE || v9ses->cache == CACHE_FSCACHE) {
740 fid = p9_client_walk(dfid, 1, &name, 1);
741 if (IS_ERR(fid)) {
742 err = PTR_ERR(fid);
743 P9_DPRINTK(P9_DEBUG_VFS, "p9_client_walk failed %d\n",
744 err);
745 fid = NULL;
746 goto error;
747 }
748
749 inode = v9fs_inode_from_fid(v9ses, fid, dir->i_sb);
750 if (IS_ERR(inode)) {
751 err = PTR_ERR(inode);
752 P9_DPRINTK(P9_DEBUG_VFS, "inode creation failed %d\n",
753 err);
754 goto error;
755 }
Aneesh Kumar K.V53c06f42011-01-10 13:51:47 -0600756 d_instantiate(dentry, inode);
757 err = v9fs_fid_add(dentry, fid);
758 if (err < 0)
759 goto error;
760 fid = NULL;
761 } else {
762 /*
763 * Not in cached mode. No need to populate inode with stat.
764 * socket syscall returns a fd, so we need instantiate
765 */
766 inode = v9fs_get_inode(dir->i_sb, mode);
767 if (IS_ERR(inode)) {
768 err = PTR_ERR(inode);
769 goto error;
770 }
Aneesh Kumar K.V53c06f42011-01-10 13:51:47 -0600771 d_instantiate(dentry, inode);
772 }
773 /* Now set the ACL based on the default value */
774 v9fs_set_create_acl(dentry, dacl, pacl);
775error:
776 if (fid)
777 p9_client_clunk(fid);
778 return err;
779}
780
Aneesh Kumar K.V53c06f42011-01-10 13:51:47 -0600781/**
782 * v9fs_vfs_follow_link_dotl - follow a symlink path
783 * @dentry: dentry for symlink
784 * @nd: nameidata
785 *
786 */
787
788static void *
789v9fs_vfs_follow_link_dotl(struct dentry *dentry, struct nameidata *nd)
790{
M. Mohan Kumar31b6cea2011-01-08 07:28:46 +0530791 int retval;
792 struct p9_fid *fid;
Aneesh Kumar K.V53c06f42011-01-10 13:51:47 -0600793 char *link = __getname();
M. Mohan Kumar31b6cea2011-01-08 07:28:46 +0530794 char *target;
Aneesh Kumar K.V53c06f42011-01-10 13:51:47 -0600795
M. Mohan Kumar31b6cea2011-01-08 07:28:46 +0530796 P9_DPRINTK(P9_DEBUG_VFS, "%s\n", dentry->d_name.name);
Aneesh Kumar K.V53c06f42011-01-10 13:51:47 -0600797
M. Mohan Kumar31b6cea2011-01-08 07:28:46 +0530798 if (!link) {
Aneesh Kumar K.V53c06f42011-01-10 13:51:47 -0600799 link = ERR_PTR(-ENOMEM);
M. Mohan Kumar31b6cea2011-01-08 07:28:46 +0530800 goto ndset;
Aneesh Kumar K.V53c06f42011-01-10 13:51:47 -0600801 }
M. Mohan Kumar31b6cea2011-01-08 07:28:46 +0530802 fid = v9fs_fid_lookup(dentry);
803 if (IS_ERR(fid)) {
804 __putname(link);
805 link = ERR_PTR(PTR_ERR(fid));
806 goto ndset;
807 }
808 retval = p9_client_readlink(fid, &target);
809 if (!retval) {
810 strcpy(link, target);
811 kfree(target);
812 goto ndset;
813 }
814 __putname(link);
815 link = ERR_PTR(retval);
816ndset:
Aneesh Kumar K.V53c06f42011-01-10 13:51:47 -0600817 nd_set_link(nd, link);
Aneesh Kumar K.V53c06f42011-01-10 13:51:47 -0600818 return NULL;
819}
820
821const struct inode_operations v9fs_dir_inode_operations_dotl = {
822 .create = v9fs_vfs_create_dotl,
823 .lookup = v9fs_vfs_lookup,
824 .link = v9fs_vfs_link_dotl,
825 .symlink = v9fs_vfs_symlink_dotl,
826 .unlink = v9fs_vfs_unlink,
827 .mkdir = v9fs_vfs_mkdir_dotl,
828 .rmdir = v9fs_vfs_rmdir,
829 .mknod = v9fs_vfs_mknod_dotl,
830 .rename = v9fs_vfs_rename,
831 .getattr = v9fs_vfs_getattr_dotl,
832 .setattr = v9fs_vfs_setattr_dotl,
833 .setxattr = generic_setxattr,
834 .getxattr = generic_getxattr,
835 .removexattr = generic_removexattr,
836 .listxattr = v9fs_listxattr,
837 .check_acl = v9fs_check_acl,
838};
839
840const struct inode_operations v9fs_file_inode_operations_dotl = {
841 .getattr = v9fs_vfs_getattr_dotl,
842 .setattr = v9fs_vfs_setattr_dotl,
843 .setxattr = generic_setxattr,
844 .getxattr = generic_getxattr,
845 .removexattr = generic_removexattr,
846 .listxattr = v9fs_listxattr,
847 .check_acl = v9fs_check_acl,
848};
849
850const struct inode_operations v9fs_symlink_inode_operations_dotl = {
M. Mohan Kumar31b6cea2011-01-08 07:28:46 +0530851 .readlink = generic_readlink,
Aneesh Kumar K.V53c06f42011-01-10 13:51:47 -0600852 .follow_link = v9fs_vfs_follow_link_dotl,
853 .put_link = v9fs_vfs_put_link,
854 .getattr = v9fs_vfs_getattr_dotl,
855 .setattr = v9fs_vfs_setattr_dotl,
856 .setxattr = generic_setxattr,
857 .getxattr = generic_getxattr,
858 .removexattr = generic_removexattr,
859 .listxattr = v9fs_listxattr,
860};