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