a3f2540cc4b22c30e5c9d85c0c5c76fee99ceb93
[pandora-kernel.git] / fs / 9p / vfs_inode_dotl.c
1 /*
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
50 static int
51 v9fs_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
60 static 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
77 static 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
89 static int v9fs_test_inode_dotl(struct inode *inode, void *data)
90 {
91         struct v9fs_inode *v9inode = V9FS_I(inode);
92         struct p9_stat_dotl *st = (struct p9_stat_dotl *)data;
93
94         /* don't match inode of different type */
95         if ((inode->i_mode & S_IFMT) != (st->st_mode & S_IFMT))
96                 return 0;
97
98         if (inode->i_generation != st->st_gen)
99                 return 0;
100
101         /* compare qid details */
102         if (memcmp(&v9inode->qid.version,
103                    &st->qid.version, sizeof(v9inode->qid.version)))
104                 return 0;
105
106         if (v9inode->qid.type != st->qid.type)
107                 return 0;
108         return 1;
109 }
110
111 /* Always get a new inode */
112 static int v9fs_test_new_inode_dotl(struct inode *inode, void *data)
113 {
114         return 0;
115 }
116
117 static int v9fs_set_inode_dotl(struct inode *inode,  void *data)
118 {
119         struct v9fs_inode *v9inode = V9FS_I(inode);
120         struct p9_stat_dotl *st = (struct p9_stat_dotl *)data;
121
122         memcpy(&v9inode->qid, &st->qid, sizeof(st->qid));
123         inode->i_generation = st->st_gen;
124         return 0;
125 }
126
127 static struct inode *v9fs_qid_iget_dotl(struct super_block *sb,
128                                         struct p9_qid *qid,
129                                         struct p9_fid *fid,
130                                         struct p9_stat_dotl *st,
131                                         int new)
132 {
133         int retval;
134         unsigned long i_ino;
135         struct inode *inode;
136         struct v9fs_session_info *v9ses = sb->s_fs_info;
137         int (*test)(struct inode *, void *);
138
139         if (new)
140                 test = v9fs_test_new_inode_dotl;
141         else
142                 test = v9fs_test_inode_dotl;
143
144         i_ino = v9fs_qid2ino(qid);
145         inode = iget5_locked(sb, i_ino, test, v9fs_set_inode_dotl, st);
146         if (!inode)
147                 return ERR_PTR(-ENOMEM);
148         if (!(inode->i_state & I_NEW))
149                 return inode;
150         /*
151          * initialize the inode with the stat info
152          * FIXME!! we may need support for stale inodes
153          * later.
154          */
155         inode->i_ino = i_ino;
156         retval = v9fs_init_inode(v9ses, inode,
157                                  st->st_mode, new_decode_dev(st->st_rdev));
158         if (retval)
159                 goto error;
160
161         v9fs_stat2inode_dotl(st, inode);
162 #ifdef CONFIG_9P_FSCACHE
163         v9fs_cache_inode_get_cookie(inode);
164 #endif
165         retval = v9fs_get_acl(inode, fid);
166         if (retval)
167                 goto error;
168
169         unlock_new_inode(inode);
170         return inode;
171 error:
172         unlock_new_inode(inode);
173         iput(inode);
174         return ERR_PTR(retval);
175
176 }
177
178 struct inode *
179 v9fs_inode_from_fid_dotl(struct v9fs_session_info *v9ses, struct p9_fid *fid,
180                          struct super_block *sb, int new)
181 {
182         struct p9_stat_dotl *st;
183         struct inode *inode = NULL;
184
185         st = p9_client_getattr_dotl(fid, P9_STATS_BASIC | P9_STATS_GEN);
186         if (IS_ERR(st))
187                 return ERR_CAST(st);
188
189         inode = v9fs_qid_iget_dotl(sb, &st->qid, fid, st, new);
190         kfree(st);
191         return inode;
192 }
193
194 /**
195  * v9fs_vfs_create_dotl - VFS hook to create files for 9P2000.L protocol.
196  * @dir: directory inode that is being created
197  * @dentry:  dentry that is being deleted
198  * @mode: create permissions
199  * @nd: path information
200  *
201  */
202
203 static int
204 v9fs_vfs_create_dotl(struct inode *dir, struct dentry *dentry, int omode,
205                 struct nameidata *nd)
206 {
207         int err = 0;
208         gid_t gid;
209         int flags;
210         umode_t mode;
211         char *name = NULL;
212         struct file *filp;
213         struct p9_qid qid;
214         struct inode *inode;
215         struct p9_fid *fid = NULL;
216         struct v9fs_inode *v9inode;
217         struct p9_fid *dfid, *ofid, *inode_fid;
218         struct v9fs_session_info *v9ses;
219         struct posix_acl *pacl = NULL, *dacl = NULL;
220
221         v9ses = v9fs_inode2v9ses(dir);
222         if (nd)
223                 flags = nd->intent.open.flags;
224         else {
225                 /*
226                  * create call without LOOKUP_OPEN is due
227                  * to mknod of regular files. So use mknod
228                  * operation.
229                  */
230                 return v9fs_vfs_mknod_dotl(dir, dentry, omode, 0);
231         }
232
233         name = (char *) dentry->d_name.name;
234         P9_DPRINTK(P9_DEBUG_VFS, "v9fs_vfs_create_dotl: name:%s flags:0x%x "
235                         "mode:0x%x\n", name, flags, omode);
236
237         dfid = v9fs_fid_lookup(dentry->d_parent);
238         if (IS_ERR(dfid)) {
239                 err = PTR_ERR(dfid);
240                 P9_DPRINTK(P9_DEBUG_VFS, "fid lookup failed %d\n", err);
241                 return err;
242         }
243
244         /* clone a fid to use for creation */
245         ofid = p9_client_walk(dfid, 0, NULL, 1);
246         if (IS_ERR(ofid)) {
247                 err = PTR_ERR(ofid);
248                 P9_DPRINTK(P9_DEBUG_VFS, "p9_client_walk failed %d\n", err);
249                 return err;
250         }
251
252         gid = v9fs_get_fsgid_for_create(dir);
253
254         mode = omode;
255         /* Update mode based on ACL value */
256         err = v9fs_acl_mode(dir, &mode, &dacl, &pacl);
257         if (err) {
258                 P9_DPRINTK(P9_DEBUG_VFS,
259                            "Failed to get acl values in creat %d\n", err);
260                 goto error;
261         }
262         err = p9_client_create_dotl(ofid, name, flags, mode, gid, &qid);
263         if (err < 0) {
264                 P9_DPRINTK(P9_DEBUG_VFS,
265                                 "p9_client_open_dotl failed in creat %d\n",
266                                 err);
267                 goto error;
268         }
269         v9fs_invalidate_inode_attr(dir);
270
271         /* instantiate inode and assign the unopened fid to the dentry */
272         fid = p9_client_walk(dfid, 1, &name, 1);
273         if (IS_ERR(fid)) {
274                 err = PTR_ERR(fid);
275                 P9_DPRINTK(P9_DEBUG_VFS, "p9_client_walk failed %d\n", err);
276                 fid = NULL;
277                 goto error;
278         }
279         inode = v9fs_get_new_inode_from_fid(v9ses, fid, dir->i_sb);
280         if (IS_ERR(inode)) {
281                 err = PTR_ERR(inode);
282                 P9_DPRINTK(P9_DEBUG_VFS, "inode creation failed %d\n", err);
283                 goto error;
284         }
285         err = v9fs_fid_add(dentry, fid);
286         if (err < 0)
287                 goto error;
288         d_instantiate(dentry, inode);
289
290         /* Now set the ACL based on the default value */
291         v9fs_set_create_acl(dentry, &dacl, &pacl);
292
293         v9inode = V9FS_I(inode);
294         mutex_lock(&v9inode->v_mutex);
295         if (v9ses->cache && !v9inode->writeback_fid &&
296             ((flags & O_ACCMODE) != O_RDONLY)) {
297                 /*
298                  * clone a fid and add it to writeback_fid
299                  * we do it during open time instead of
300                  * page dirty time via write_begin/page_mkwrite
301                  * because we want write after unlink usecase
302                  * to work.
303                  */
304                 inode_fid = v9fs_writeback_fid(dentry);
305                 if (IS_ERR(inode_fid)) {
306                         err = PTR_ERR(inode_fid);
307                         mutex_unlock(&v9inode->v_mutex);
308                         goto err_clunk_old_fid;
309                 }
310                 v9inode->writeback_fid = (void *) inode_fid;
311         }
312         mutex_unlock(&v9inode->v_mutex);
313         /* Since we are opening a file, assign the open fid to the file */
314         filp = lookup_instantiate_filp(nd, dentry, generic_file_open);
315         if (IS_ERR(filp)) {
316                 err = PTR_ERR(filp);
317                 goto err_clunk_old_fid;
318         }
319         filp->private_data = ofid;
320 #ifdef CONFIG_9P_FSCACHE
321         if (v9ses->cache)
322                 v9fs_cache_inode_set_cookie(inode, filp);
323 #endif
324         return 0;
325
326 error:
327         if (fid)
328                 p9_client_clunk(fid);
329 err_clunk_old_fid:
330         if (ofid)
331                 p9_client_clunk(ofid);
332         v9fs_set_create_acl(NULL, &dacl, &pacl);
333         return err;
334 }
335
336 /**
337  * v9fs_vfs_mkdir_dotl - VFS mkdir hook to create a directory
338  * @dir:  inode that is being unlinked
339  * @dentry: dentry that is being unlinked
340  * @mode: mode for new directory
341  *
342  */
343
344 static int v9fs_vfs_mkdir_dotl(struct inode *dir,
345                                struct dentry *dentry, int omode)
346 {
347         int err;
348         struct v9fs_session_info *v9ses;
349         struct p9_fid *fid = NULL, *dfid = NULL;
350         gid_t gid;
351         char *name;
352         umode_t mode;
353         struct inode *inode;
354         struct p9_qid qid;
355         struct dentry *dir_dentry;
356         struct posix_acl *dacl = NULL, *pacl = NULL;
357
358         P9_DPRINTK(P9_DEBUG_VFS, "name %s\n", dentry->d_name.name);
359         err = 0;
360         v9ses = v9fs_inode2v9ses(dir);
361
362         omode |= S_IFDIR;
363         if (dir->i_mode & S_ISGID)
364                 omode |= S_ISGID;
365
366         dir_dentry = v9fs_dentry_from_dir_inode(dir);
367         dfid = v9fs_fid_lookup(dir_dentry);
368         if (IS_ERR(dfid)) {
369                 err = PTR_ERR(dfid);
370                 P9_DPRINTK(P9_DEBUG_VFS, "fid lookup failed %d\n", err);
371                 dfid = NULL;
372                 goto error;
373         }
374
375         gid = v9fs_get_fsgid_for_create(dir);
376         mode = omode;
377         /* Update mode based on ACL value */
378         err = v9fs_acl_mode(dir, &mode, &dacl, &pacl);
379         if (err) {
380                 P9_DPRINTK(P9_DEBUG_VFS,
381                            "Failed to get acl values in mkdir %d\n", err);
382                 goto error;
383         }
384         name = (char *) dentry->d_name.name;
385         err = p9_client_mkdir_dotl(dfid, name, mode, gid, &qid);
386         if (err < 0)
387                 goto error;
388
389         /* instantiate inode and assign the unopened fid to the dentry */
390         if (v9ses->cache == CACHE_LOOSE || v9ses->cache == CACHE_FSCACHE) {
391                 fid = p9_client_walk(dfid, 1, &name, 1);
392                 if (IS_ERR(fid)) {
393                         err = PTR_ERR(fid);
394                         P9_DPRINTK(P9_DEBUG_VFS, "p9_client_walk failed %d\n",
395                                 err);
396                         fid = NULL;
397                         goto error;
398                 }
399
400                 inode = v9fs_get_new_inode_from_fid(v9ses, fid, dir->i_sb);
401                 if (IS_ERR(inode)) {
402                         err = PTR_ERR(inode);
403                         P9_DPRINTK(P9_DEBUG_VFS, "inode creation failed %d\n",
404                                 err);
405                         goto error;
406                 }
407                 err = v9fs_fid_add(dentry, fid);
408                 if (err < 0)
409                         goto error;
410                 d_instantiate(dentry, inode);
411                 fid = NULL;
412         } else {
413                 /*
414                  * Not in cached mode. No need to populate
415                  * inode with stat. We need to get an inode
416                  * so that we can set the acl with dentry
417                  */
418                 inode = v9fs_get_inode(dir->i_sb, mode, 0);
419                 if (IS_ERR(inode)) {
420                         err = PTR_ERR(inode);
421                         goto error;
422                 }
423                 d_instantiate(dentry, inode);
424         }
425         /* Now set the ACL based on the default value */
426         v9fs_set_create_acl(dentry, &dacl, &pacl);
427         inc_nlink(dir);
428         v9fs_invalidate_inode_attr(dir);
429 error:
430         if (fid)
431                 p9_client_clunk(fid);
432         v9fs_set_create_acl(NULL, &dacl, &pacl);
433         return err;
434 }
435
436 static int
437 v9fs_vfs_getattr_dotl(struct vfsmount *mnt, struct dentry *dentry,
438                  struct kstat *stat)
439 {
440         int err;
441         struct v9fs_session_info *v9ses;
442         struct p9_fid *fid;
443         struct p9_stat_dotl *st;
444
445         P9_DPRINTK(P9_DEBUG_VFS, "dentry: %p\n", dentry);
446         err = -EPERM;
447         v9ses = v9fs_dentry2v9ses(dentry);
448         if (v9ses->cache == CACHE_LOOSE || v9ses->cache == CACHE_FSCACHE) {
449                 generic_fillattr(dentry->d_inode, stat);
450                 return 0;
451         }
452         fid = v9fs_fid_lookup(dentry);
453         if (IS_ERR(fid))
454                 return PTR_ERR(fid);
455
456         /* Ask for all the fields in stat structure. Server will return
457          * whatever it supports
458          */
459
460         st = p9_client_getattr_dotl(fid, P9_STATS_ALL);
461         if (IS_ERR(st))
462                 return PTR_ERR(st);
463
464         v9fs_stat2inode_dotl(st, dentry->d_inode);
465         generic_fillattr(dentry->d_inode, stat);
466         /* Change block size to what the server returned */
467         stat->blksize = st->st_blksize;
468
469         kfree(st);
470         return 0;
471 }
472
473 /**
474  * v9fs_vfs_setattr_dotl - set file metadata
475  * @dentry: file whose metadata to set
476  * @iattr: metadata assignment structure
477  *
478  */
479
480 int v9fs_vfs_setattr_dotl(struct dentry *dentry, struct iattr *iattr)
481 {
482         int retval;
483         struct v9fs_session_info *v9ses;
484         struct p9_fid *fid;
485         struct p9_iattr_dotl p9attr;
486
487         P9_DPRINTK(P9_DEBUG_VFS, "\n");
488
489         retval = inode_change_ok(dentry->d_inode, iattr);
490         if (retval)
491                 return retval;
492
493         p9attr.valid = iattr->ia_valid;
494         p9attr.mode = iattr->ia_mode;
495         p9attr.uid = iattr->ia_uid;
496         p9attr.gid = iattr->ia_gid;
497         p9attr.size = iattr->ia_size;
498         p9attr.atime_sec = iattr->ia_atime.tv_sec;
499         p9attr.atime_nsec = iattr->ia_atime.tv_nsec;
500         p9attr.mtime_sec = iattr->ia_mtime.tv_sec;
501         p9attr.mtime_nsec = iattr->ia_mtime.tv_nsec;
502
503         retval = -EPERM;
504         v9ses = v9fs_dentry2v9ses(dentry);
505         fid = v9fs_fid_lookup(dentry);
506         if (IS_ERR(fid))
507                 return PTR_ERR(fid);
508
509         /* Write all dirty data */
510         if (S_ISREG(dentry->d_inode->i_mode))
511                 filemap_write_and_wait(dentry->d_inode->i_mapping);
512
513         retval = p9_client_setattr(fid, &p9attr);
514         if (retval < 0)
515                 return retval;
516
517         if ((iattr->ia_valid & ATTR_SIZE) &&
518             iattr->ia_size != i_size_read(dentry->d_inode))
519                 truncate_setsize(dentry->d_inode, iattr->ia_size);
520
521         v9fs_invalidate_inode_attr(dentry->d_inode);
522         setattr_copy(dentry->d_inode, iattr);
523         mark_inode_dirty(dentry->d_inode);
524         if (iattr->ia_valid & ATTR_MODE) {
525                 /* We also want to update ACL when we update mode bits */
526                 retval = v9fs_acl_chmod(dentry);
527                 if (retval < 0)
528                         return retval;
529         }
530         return 0;
531 }
532
533 /**
534  * v9fs_stat2inode_dotl - populate an inode structure with stat info
535  * @stat: stat structure
536  * @inode: inode to populate
537  * @sb: superblock of filesystem
538  *
539  */
540
541 void
542 v9fs_stat2inode_dotl(struct p9_stat_dotl *stat, struct inode *inode)
543 {
544         mode_t mode;
545         struct v9fs_inode *v9inode = V9FS_I(inode);
546
547         if ((stat->st_result_mask & P9_STATS_BASIC) == P9_STATS_BASIC) {
548                 inode->i_atime.tv_sec = stat->st_atime_sec;
549                 inode->i_atime.tv_nsec = stat->st_atime_nsec;
550                 inode->i_mtime.tv_sec = stat->st_mtime_sec;
551                 inode->i_mtime.tv_nsec = stat->st_mtime_nsec;
552                 inode->i_ctime.tv_sec = stat->st_ctime_sec;
553                 inode->i_ctime.tv_nsec = stat->st_ctime_nsec;
554                 inode->i_uid = stat->st_uid;
555                 inode->i_gid = stat->st_gid;
556                 inode->i_nlink = stat->st_nlink;
557
558                 mode = stat->st_mode & S_IALLUGO;
559                 mode |= inode->i_mode & ~S_IALLUGO;
560                 inode->i_mode = mode;
561
562                 i_size_write(inode, stat->st_size);
563                 inode->i_blocks = stat->st_blocks;
564         } else {
565                 if (stat->st_result_mask & P9_STATS_ATIME) {
566                         inode->i_atime.tv_sec = stat->st_atime_sec;
567                         inode->i_atime.tv_nsec = stat->st_atime_nsec;
568                 }
569                 if (stat->st_result_mask & P9_STATS_MTIME) {
570                         inode->i_mtime.tv_sec = stat->st_mtime_sec;
571                         inode->i_mtime.tv_nsec = stat->st_mtime_nsec;
572                 }
573                 if (stat->st_result_mask & P9_STATS_CTIME) {
574                         inode->i_ctime.tv_sec = stat->st_ctime_sec;
575                         inode->i_ctime.tv_nsec = stat->st_ctime_nsec;
576                 }
577                 if (stat->st_result_mask & P9_STATS_UID)
578                         inode->i_uid = stat->st_uid;
579                 if (stat->st_result_mask & P9_STATS_GID)
580                         inode->i_gid = stat->st_gid;
581                 if (stat->st_result_mask & P9_STATS_NLINK)
582                         inode->i_nlink = stat->st_nlink;
583                 if (stat->st_result_mask & P9_STATS_MODE) {
584                         inode->i_mode = stat->st_mode;
585                         if ((S_ISBLK(inode->i_mode)) ||
586                                                 (S_ISCHR(inode->i_mode)))
587                                 init_special_inode(inode, inode->i_mode,
588                                                                 inode->i_rdev);
589                 }
590                 if (stat->st_result_mask & P9_STATS_RDEV)
591                         inode->i_rdev = new_decode_dev(stat->st_rdev);
592                 if (stat->st_result_mask & P9_STATS_SIZE)
593                         i_size_write(inode, stat->st_size);
594                 if (stat->st_result_mask & P9_STATS_BLOCKS)
595                         inode->i_blocks = stat->st_blocks;
596         }
597         if (stat->st_result_mask & P9_STATS_GEN)
598                 inode->i_generation = stat->st_gen;
599
600         /* Currently we don't support P9_STATS_BTIME and P9_STATS_DATA_VERSION
601          * because the inode structure does not have fields for them.
602          */
603         v9inode->cache_validity &= ~V9FS_INO_INVALID_ATTR;
604 }
605
606 static int
607 v9fs_vfs_symlink_dotl(struct inode *dir, struct dentry *dentry,
608                 const char *symname)
609 {
610         int err;
611         gid_t gid;
612         char *name;
613         struct p9_qid qid;
614         struct inode *inode;
615         struct p9_fid *dfid;
616         struct p9_fid *fid = NULL;
617         struct v9fs_session_info *v9ses;
618
619         name = (char *) dentry->d_name.name;
620         P9_DPRINTK(P9_DEBUG_VFS, "v9fs_vfs_symlink_dotl : %lu,%s,%s\n",
621                         dir->i_ino, name, symname);
622         v9ses = v9fs_inode2v9ses(dir);
623
624         dfid = v9fs_fid_lookup(dentry->d_parent);
625         if (IS_ERR(dfid)) {
626                 err = PTR_ERR(dfid);
627                 P9_DPRINTK(P9_DEBUG_VFS, "fid lookup failed %d\n", err);
628                 return err;
629         }
630
631         gid = v9fs_get_fsgid_for_create(dir);
632
633         /* Server doesn't alter fid on TSYMLINK. Hence no need to clone it. */
634         err = p9_client_symlink(dfid, name, (char *)symname, gid, &qid);
635
636         if (err < 0) {
637                 P9_DPRINTK(P9_DEBUG_VFS, "p9_client_symlink failed %d\n", err);
638                 goto error;
639         }
640
641         v9fs_invalidate_inode_attr(dir);
642         if (v9ses->cache) {
643                 /* Now walk from the parent so we can get an unopened fid. */
644                 fid = p9_client_walk(dfid, 1, &name, 1);
645                 if (IS_ERR(fid)) {
646                         err = PTR_ERR(fid);
647                         P9_DPRINTK(P9_DEBUG_VFS, "p9_client_walk failed %d\n",
648                                         err);
649                         fid = NULL;
650                         goto error;
651                 }
652
653                 /* instantiate inode and assign the unopened fid to dentry */
654                 inode = v9fs_get_new_inode_from_fid(v9ses, fid, dir->i_sb);
655                 if (IS_ERR(inode)) {
656                         err = PTR_ERR(inode);
657                         P9_DPRINTK(P9_DEBUG_VFS, "inode creation failed %d\n",
658                                         err);
659                         goto error;
660                 }
661                 err = v9fs_fid_add(dentry, fid);
662                 if (err < 0)
663                         goto error;
664                 d_instantiate(dentry, inode);
665                 fid = NULL;
666         } else {
667                 /* Not in cached mode. No need to populate inode with stat */
668                 inode = v9fs_get_inode(dir->i_sb, S_IFLNK, 0);
669                 if (IS_ERR(inode)) {
670                         err = PTR_ERR(inode);
671                         goto error;
672                 }
673                 d_instantiate(dentry, inode);
674         }
675
676 error:
677         if (fid)
678                 p9_client_clunk(fid);
679
680         return err;
681 }
682
683 /**
684  * v9fs_vfs_link_dotl - create a hardlink for dotl
685  * @old_dentry: dentry for file to link to
686  * @dir: inode destination for new link
687  * @dentry: dentry for link
688  *
689  */
690
691 static int
692 v9fs_vfs_link_dotl(struct dentry *old_dentry, struct inode *dir,
693                 struct dentry *dentry)
694 {
695         int err;
696         char *name;
697         struct dentry *dir_dentry;
698         struct p9_fid *dfid, *oldfid;
699         struct v9fs_session_info *v9ses;
700
701         P9_DPRINTK(P9_DEBUG_VFS, "dir ino: %lu, old_name: %s, new_name: %s\n",
702                         dir->i_ino, old_dentry->d_name.name,
703                         dentry->d_name.name);
704
705         v9ses = v9fs_inode2v9ses(dir);
706         dir_dentry = v9fs_dentry_from_dir_inode(dir);
707         dfid = v9fs_fid_lookup(dir_dentry);
708         if (IS_ERR(dfid))
709                 return PTR_ERR(dfid);
710
711         oldfid = v9fs_fid_lookup(old_dentry);
712         if (IS_ERR(oldfid))
713                 return PTR_ERR(oldfid);
714
715         name = (char *) dentry->d_name.name;
716
717         err = p9_client_link(dfid, oldfid, (char *)dentry->d_name.name);
718
719         if (err < 0) {
720                 P9_DPRINTK(P9_DEBUG_VFS, "p9_client_link failed %d\n", err);
721                 return err;
722         }
723
724         v9fs_invalidate_inode_attr(dir);
725         if (v9ses->cache == CACHE_LOOSE || v9ses->cache == CACHE_FSCACHE) {
726                 /* Get the latest stat info from server. */
727                 struct p9_fid *fid;
728                 fid = v9fs_fid_lookup(old_dentry);
729                 if (IS_ERR(fid))
730                         return PTR_ERR(fid);
731
732                 v9fs_refresh_inode_dotl(fid, old_dentry->d_inode);
733         }
734         ihold(old_dentry->d_inode);
735         d_instantiate(dentry, old_dentry->d_inode);
736
737         return err;
738 }
739
740 /**
741  * v9fs_vfs_mknod_dotl - create a special file
742  * @dir: inode destination for new link
743  * @dentry: dentry for file
744  * @mode: mode for creation
745  * @rdev: device associated with special file
746  *
747  */
748 static int
749 v9fs_vfs_mknod_dotl(struct inode *dir, struct dentry *dentry, int omode,
750                 dev_t rdev)
751 {
752         int err;
753         gid_t gid;
754         char *name;
755         umode_t mode;
756         struct v9fs_session_info *v9ses;
757         struct p9_fid *fid = NULL, *dfid = NULL;
758         struct inode *inode;
759         struct p9_qid qid;
760         struct dentry *dir_dentry;
761         struct posix_acl *dacl = NULL, *pacl = NULL;
762
763         P9_DPRINTK(P9_DEBUG_VFS,
764                 " %lu,%s mode: %x MAJOR: %u MINOR: %u\n", dir->i_ino,
765                 dentry->d_name.name, omode, MAJOR(rdev), MINOR(rdev));
766
767         if (!new_valid_dev(rdev))
768                 return -EINVAL;
769
770         v9ses = v9fs_inode2v9ses(dir);
771         dir_dentry = v9fs_dentry_from_dir_inode(dir);
772         dfid = v9fs_fid_lookup(dir_dentry);
773         if (IS_ERR(dfid)) {
774                 err = PTR_ERR(dfid);
775                 P9_DPRINTK(P9_DEBUG_VFS, "fid lookup failed %d\n", err);
776                 dfid = NULL;
777                 goto error;
778         }
779
780         gid = v9fs_get_fsgid_for_create(dir);
781         mode = omode;
782         /* Update mode based on ACL value */
783         err = v9fs_acl_mode(dir, &mode, &dacl, &pacl);
784         if (err) {
785                 P9_DPRINTK(P9_DEBUG_VFS,
786                            "Failed to get acl values in mknod %d\n", err);
787                 goto error;
788         }
789         name = (char *) dentry->d_name.name;
790
791         err = p9_client_mknod_dotl(dfid, name, mode, rdev, gid, &qid);
792         if (err < 0)
793                 goto error;
794
795         v9fs_invalidate_inode_attr(dir);
796         /* instantiate inode and assign the unopened fid to the dentry */
797         if (v9ses->cache == CACHE_LOOSE || v9ses->cache == CACHE_FSCACHE) {
798                 fid = p9_client_walk(dfid, 1, &name, 1);
799                 if (IS_ERR(fid)) {
800                         err = PTR_ERR(fid);
801                         P9_DPRINTK(P9_DEBUG_VFS, "p9_client_walk failed %d\n",
802                                 err);
803                         fid = NULL;
804                         goto error;
805                 }
806
807                 inode = v9fs_get_new_inode_from_fid(v9ses, fid, dir->i_sb);
808                 if (IS_ERR(inode)) {
809                         err = PTR_ERR(inode);
810                         P9_DPRINTK(P9_DEBUG_VFS, "inode creation failed %d\n",
811                                 err);
812                         goto error;
813                 }
814                 err = v9fs_fid_add(dentry, fid);
815                 if (err < 0)
816                         goto error;
817                 d_instantiate(dentry, inode);
818                 fid = NULL;
819         } else {
820                 /*
821                  * Not in cached mode. No need to populate inode with stat.
822                  * socket syscall returns a fd, so we need instantiate
823                  */
824                 inode = v9fs_get_inode(dir->i_sb, mode, rdev);
825                 if (IS_ERR(inode)) {
826                         err = PTR_ERR(inode);
827                         goto error;
828                 }
829                 d_instantiate(dentry, inode);
830         }
831         /* Now set the ACL based on the default value */
832         v9fs_set_create_acl(dentry, &dacl, &pacl);
833 error:
834         if (fid)
835                 p9_client_clunk(fid);
836         v9fs_set_create_acl(NULL, &dacl, &pacl);
837         return err;
838 }
839
840 /**
841  * v9fs_vfs_follow_link_dotl - follow a symlink path
842  * @dentry: dentry for symlink
843  * @nd: nameidata
844  *
845  */
846
847 static void *
848 v9fs_vfs_follow_link_dotl(struct dentry *dentry, struct nameidata *nd)
849 {
850         int retval;
851         struct p9_fid *fid;
852         char *link = __getname();
853         char *target;
854
855         P9_DPRINTK(P9_DEBUG_VFS, "%s\n", dentry->d_name.name);
856
857         if (!link) {
858                 link = ERR_PTR(-ENOMEM);
859                 goto ndset;
860         }
861         fid = v9fs_fid_lookup(dentry);
862         if (IS_ERR(fid)) {
863                 __putname(link);
864                 link = ERR_CAST(fid);
865                 goto ndset;
866         }
867         retval = p9_client_readlink(fid, &target);
868         if (!retval) {
869                 strcpy(link, target);
870                 kfree(target);
871                 goto ndset;
872         }
873         __putname(link);
874         link = ERR_PTR(retval);
875 ndset:
876         nd_set_link(nd, link);
877         return NULL;
878 }
879
880 int v9fs_refresh_inode_dotl(struct p9_fid *fid, struct inode *inode)
881 {
882         loff_t i_size;
883         struct p9_stat_dotl *st;
884         struct v9fs_session_info *v9ses;
885
886         v9ses = v9fs_inode2v9ses(inode);
887         st = p9_client_getattr_dotl(fid, P9_STATS_ALL);
888         if (IS_ERR(st))
889                 return PTR_ERR(st);
890         /*
891          * Don't update inode if the file type is different
892          */
893         if ((inode->i_mode & S_IFMT) != (st->st_mode & S_IFMT))
894                 goto out;
895
896         spin_lock(&inode->i_lock);
897         /*
898          * We don't want to refresh inode->i_size,
899          * because we may have cached data
900          */
901         i_size = inode->i_size;
902         v9fs_stat2inode_dotl(st, inode);
903         if (v9ses->cache)
904                 inode->i_size = i_size;
905         spin_unlock(&inode->i_lock);
906 out:
907         kfree(st);
908         return 0;
909 }
910
911 const struct inode_operations v9fs_dir_inode_operations_dotl = {
912         .create = v9fs_vfs_create_dotl,
913         .lookup = v9fs_vfs_lookup,
914         .link = v9fs_vfs_link_dotl,
915         .symlink = v9fs_vfs_symlink_dotl,
916         .unlink = v9fs_vfs_unlink,
917         .mkdir = v9fs_vfs_mkdir_dotl,
918         .rmdir = v9fs_vfs_rmdir,
919         .mknod = v9fs_vfs_mknod_dotl,
920         .rename = v9fs_vfs_rename,
921         .getattr = v9fs_vfs_getattr_dotl,
922         .setattr = v9fs_vfs_setattr_dotl,
923         .setxattr = generic_setxattr,
924         .getxattr = generic_getxattr,
925         .removexattr = generic_removexattr,
926         .listxattr = v9fs_listxattr,
927         .get_acl = v9fs_iop_get_acl,
928 };
929
930 const struct inode_operations v9fs_file_inode_operations_dotl = {
931         .getattr = v9fs_vfs_getattr_dotl,
932         .setattr = v9fs_vfs_setattr_dotl,
933         .setxattr = generic_setxattr,
934         .getxattr = generic_getxattr,
935         .removexattr = generic_removexattr,
936         .listxattr = v9fs_listxattr,
937         .get_acl = v9fs_iop_get_acl,
938 };
939
940 const struct inode_operations v9fs_symlink_inode_operations_dotl = {
941         .readlink = generic_readlink,
942         .follow_link = v9fs_vfs_follow_link_dotl,
943         .put_link = v9fs_vfs_put_link,
944         .getattr = v9fs_vfs_getattr_dotl,
945         .setattr = v9fs_vfs_setattr_dotl,
946         .setxattr = generic_setxattr,
947         .getxattr = generic_getxattr,
948         .removexattr = generic_removexattr,
949         .listxattr = v9fs_listxattr,
950 };