9p: close ACL leaks
[platform/adaptation/renesas_rcar/renesas_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 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_fscache_set_key(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;
125 error:
126         unlock_new_inode(inode);
127         iput(inode);
128         return ERR_PTR(retval);
129
130 }
131
132 struct inode *
133 v9fs_inode_from_fid_dotl(struct v9fs_session_info *v9ses, struct p9_fid *fid,
134                          struct super_block *sb)
135 {
136         struct p9_stat_dotl *st;
137         struct inode *inode = NULL;
138
139         st = p9_client_getattr_dotl(fid, P9_STATS_BASIC);
140         if (IS_ERR(st))
141                 return ERR_CAST(st);
142
143         inode = v9fs_qid_iget_dotl(sb, &st->qid, fid, st);
144         kfree(st);
145         return inode;
146 }
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
157 static int
158 v9fs_vfs_create_dotl(struct inode *dir, struct dentry *dentry, int omode,
159                 struct nameidata *nd)
160 {
161         int err = 0;
162         gid_t gid;
163         int flags;
164         mode_t mode;
165         char *name = NULL;
166         struct file *filp;
167         struct p9_qid qid;
168         struct inode *inode;
169         struct p9_fid *fid = NULL;
170         struct v9fs_inode *v9inode;
171         struct p9_fid *dfid, *ofid, *inode_fid;
172         struct v9fs_session_info *v9ses;
173         struct posix_acl *pacl = NULL, *dacl = NULL;
174
175         v9ses = v9fs_inode2v9ses(dir);
176         if (nd)
177                 flags = nd->intent.open.flags;
178         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         }
223         v9fs_invalidate_inode_attr(dir);
224
225         /* 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);
229                 P9_DPRINTK(P9_DEBUG_VFS, "p9_client_walk failed %d\n", err);
230                 fid = NULL;
231                 goto error;
232         }
233         inode = v9fs_get_inode_from_fid(v9ses, fid, dir->i_sb);
234         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         }
239         d_instantiate(dentry, inode);
240         err = v9fs_fid_add(dentry, fid);
241         if (err < 0)
242                 goto error;
243
244         /* Now set the ACL based on the default value */
245         v9fs_set_create_acl(dentry, &dacl, &pacl);
246
247         v9inode = V9FS_I(inode);
248         mutex_lock(&v9inode->v_mutex);
249         if (v9ses->cache && !v9inode->writeback_fid &&
250             ((flags & O_ACCMODE) != O_RDONLY)) {
251                 /*
252                  * clone a fid and add it to writeback_fid
253                  * 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);
261                         mutex_unlock(&v9inode->v_mutex);
262                         goto err_clunk_old_fid;
263                 }
264                 v9inode->writeback_fid = (void *) inode_fid;
265         }
266         mutex_unlock(&v9inode->v_mutex);
267         /* 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)) {
270                 err = PTR_ERR(filp);
271                 goto err_clunk_old_fid;
272         }
273         filp->private_data = ofid;
274 #ifdef CONFIG_9P_FSCACHE
275         if (v9ses->cache)
276                 v9fs_cache_inode_set_cookie(inode, filp);
277 #endif
278         return 0;
279
280 error:
281         if (fid)
282                 p9_client_clunk(fid);
283 err_clunk_old_fid:
284         if (ofid)
285                 p9_client_clunk(ofid);
286         v9fs_set_create_acl(NULL, &dacl, &pacl);
287         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
298 static 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
354                 inode = v9fs_get_inode_from_fid(v9ses, fid, dir->i_sb);
355                 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                 }
361                 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                 }
377                 d_instantiate(dentry, inode);
378         }
379         /* Now set the ACL based on the default value */
380         v9fs_set_create_acl(dentry, &dacl, &pacl);
381         inc_nlink(dir);
382         v9fs_invalidate_inode_attr(dir);
383 error:
384         if (fid)
385                 p9_client_clunk(fid);
386         v9fs_set_create_acl(NULL, &dacl, &pacl);
387         return err;
388 }
389
390 static int
391 v9fs_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;
401         v9ses = v9fs_dentry2v9ses(dentry);
402         if (v9ses->cache == CACHE_LOOSE || v9ses->cache == CACHE_FSCACHE) {
403                 generic_fillattr(dentry->d_inode, stat);
404                 return 0;
405         }
406         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
434 int 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;
458         v9ses = v9fs_dentry2v9ses(dentry);
459         fid = v9fs_fid_lookup(dentry);
460         if (IS_ERR(fid))
461                 return PTR_ERR(fid);
462
463         /* Write all dirty data */
464         if (S_ISREG(dentry->d_inode->i_mode))
465                 filemap_write_and_wait(dentry->d_inode->i_mapping);
466
467         retval = p9_client_setattr(fid, &p9attr);
468         if (retval < 0)
469                 return retval;
470
471         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);
476         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
495 void
496 v9fs_stat2inode_dotl(struct p9_stat_dotl *stat, struct inode *inode)
497 {
498         struct v9fs_inode *v9inode = V9FS_I(inode);
499
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          */
557         v9inode->cache_validity &= ~V9FS_INO_INVALID_ATTR;
558 }
559
560 static int
561 v9fs_vfs_symlink_dotl(struct inode *dir, struct dentry *dentry,
562                 const char *symname)
563 {
564         int err;
565         gid_t gid;
566         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;
572
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
595         v9fs_invalidate_inode_attr(dir);
596         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 */
608                 inode = v9fs_get_inode_from_fid(v9ses, fid, dir->i_sb);
609                 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                 }
615                 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                 }
627                 d_instantiate(dentry, inode);
628         }
629
630 error:
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
645 static int
646 v9fs_vfs_link_dotl(struct dentry *old_dentry, struct inode *dir,
647                 struct dentry *dentry)
648 {
649         int err;
650         char *name;
651         struct dentry *dir_dentry;
652         struct p9_fid *dfid, *oldfid;
653         struct v9fs_session_info *v9ses;
654
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
678         v9fs_invalidate_inode_attr(dir);
679         if (v9ses->cache == CACHE_LOOSE || v9ses->cache == CACHE_FSCACHE) {
680                 /* Get the latest stat info from server. */
681                 struct p9_fid *fid;
682                 fid = v9fs_fid_lookup(old_dentry);
683                 if (IS_ERR(fid))
684                         return PTR_ERR(fid);
685
686                 v9fs_refresh_inode_dotl(fid, old_dentry->d_inode);
687         }
688         ihold(old_dentry->d_inode);
689         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  */
702 static int
703 v9fs_vfs_mknod_dotl(struct inode *dir, struct dentry *dentry, int omode,
704                 dev_t rdev)
705 {
706         int err;
707         gid_t gid;
708         char *name;
709         mode_t mode;
710         struct v9fs_session_info *v9ses;
711         struct p9_fid *fid = NULL, *dfid = NULL;
712         struct inode *inode;
713         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
749         v9fs_invalidate_inode_attr(dir);
750         /* 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
761                 inode = v9fs_get_inode_from_fid(v9ses, fid, dir->i_sb);
762                 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                 }
768                 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                 }
783                 d_instantiate(dentry, inode);
784         }
785         /* Now set the ACL based on the default value */
786         v9fs_set_create_acl(dentry, &dacl, &pacl);
787 error:
788         if (fid)
789                 p9_client_clunk(fid);
790         v9fs_set_create_acl(NULL, &dacl, &pacl);
791         return err;
792 }
793
794 /**
795  * v9fs_vfs_follow_link_dotl - follow a symlink path
796  * @dentry: dentry for symlink
797  * @nd: nameidata
798  *
799  */
800
801 static void *
802 v9fs_vfs_follow_link_dotl(struct dentry *dentry, struct nameidata *nd)
803 {
804         int retval;
805         struct p9_fid *fid;
806         char *link = __getname();
807         char *target;
808
809         P9_DPRINTK(P9_DEBUG_VFS, "%s\n", dentry->d_name.name);
810
811         if (!link) {
812                 link = ERR_PTR(-ENOMEM);
813                 goto ndset;
814         }
815         fid = v9fs_fid_lookup(dentry);
816         if (IS_ERR(fid)) {
817                 __putname(link);
818                 link = ERR_CAST(fid);
819                 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);
829 ndset:
830         nd_set_link(nd, link);
831         return NULL;
832 }
833
834 int 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
859 const 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,
875         .check_acl = v9fs_check_acl,
876 };
877
878 const 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,
885         .check_acl = v9fs_check_acl,
886 };
887
888 const struct inode_operations v9fs_symlink_inode_operations_dotl = {
889         .readlink = generic_readlink,
890         .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 };