fs/9p: Use truncate_setsize instead of vmtruncate
[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 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 && nd->flags & LOOKUP_OPEN)
177                 flags = nd->intent.open.flags - 1;
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                 /*
251                  * clone a fid and add it to writeback_fid
252                  * we do it during open time instead of
253                  * page dirty time via write_begin/page_mkwrite
254                  * because we want write after unlink usecase
255                  * to work.
256                  */
257                 inode_fid = v9fs_writeback_fid(dentry);
258                 if (IS_ERR(inode_fid)) {
259                         err = PTR_ERR(inode_fid);
260                         mutex_unlock(&v9inode->v_mutex);
261                         goto error;
262                 }
263                 v9inode->writeback_fid = (void *) inode_fid;
264         }
265         mutex_unlock(&v9inode->v_mutex);
266         /* Since we are opening a file, assign the open fid to the file */
267         filp = lookup_instantiate_filp(nd, dentry, generic_file_open);
268         if (IS_ERR(filp)) {
269                 p9_client_clunk(ofid);
270                 return PTR_ERR(filp);
271         }
272         filp->private_data = ofid;
273 #ifdef CONFIG_9P_FSCACHE
274         if (v9ses->cache)
275                 v9fs_cache_inode_set_cookie(inode, filp);
276 #endif
277         return 0;
278
279 error:
280         if (ofid)
281                 p9_client_clunk(ofid);
282         if (fid)
283                 p9_client_clunk(fid);
284         return err;
285 }
286
287 /**
288  * v9fs_vfs_mkdir_dotl - VFS mkdir hook to create a directory
289  * @dir:  inode that is being unlinked
290  * @dentry: dentry that is being unlinked
291  * @mode: mode for new directory
292  *
293  */
294
295 static int v9fs_vfs_mkdir_dotl(struct inode *dir,
296                                struct dentry *dentry, int omode)
297 {
298         int err;
299         struct v9fs_session_info *v9ses;
300         struct p9_fid *fid = NULL, *dfid = NULL;
301         gid_t gid;
302         char *name;
303         mode_t mode;
304         struct inode *inode;
305         struct p9_qid qid;
306         struct dentry *dir_dentry;
307         struct posix_acl *dacl = NULL, *pacl = NULL;
308
309         P9_DPRINTK(P9_DEBUG_VFS, "name %s\n", dentry->d_name.name);
310         err = 0;
311         v9ses = v9fs_inode2v9ses(dir);
312
313         omode |= S_IFDIR;
314         if (dir->i_mode & S_ISGID)
315                 omode |= S_ISGID;
316
317         dir_dentry = v9fs_dentry_from_dir_inode(dir);
318         dfid = v9fs_fid_lookup(dir_dentry);
319         if (IS_ERR(dfid)) {
320                 err = PTR_ERR(dfid);
321                 P9_DPRINTK(P9_DEBUG_VFS, "fid lookup failed %d\n", err);
322                 dfid = NULL;
323                 goto error;
324         }
325
326         gid = v9fs_get_fsgid_for_create(dir);
327         mode = omode;
328         /* Update mode based on ACL value */
329         err = v9fs_acl_mode(dir, &mode, &dacl, &pacl);
330         if (err) {
331                 P9_DPRINTK(P9_DEBUG_VFS,
332                            "Failed to get acl values in mkdir %d\n", err);
333                 goto error;
334         }
335         name = (char *) dentry->d_name.name;
336         err = p9_client_mkdir_dotl(dfid, name, mode, gid, &qid);
337         if (err < 0)
338                 goto error;
339
340         /* instantiate inode and assign the unopened fid to the dentry */
341         if (v9ses->cache == CACHE_LOOSE || v9ses->cache == CACHE_FSCACHE) {
342                 fid = p9_client_walk(dfid, 1, &name, 1);
343                 if (IS_ERR(fid)) {
344                         err = PTR_ERR(fid);
345                         P9_DPRINTK(P9_DEBUG_VFS, "p9_client_walk failed %d\n",
346                                 err);
347                         fid = NULL;
348                         goto error;
349                 }
350
351                 inode = v9fs_get_inode_from_fid(v9ses, fid, dir->i_sb);
352                 if (IS_ERR(inode)) {
353                         err = PTR_ERR(inode);
354                         P9_DPRINTK(P9_DEBUG_VFS, "inode creation failed %d\n",
355                                 err);
356                         goto error;
357                 }
358                 d_instantiate(dentry, inode);
359                 err = v9fs_fid_add(dentry, fid);
360                 if (err < 0)
361                         goto error;
362                 fid = NULL;
363         } else {
364                 /*
365                  * Not in cached mode. No need to populate
366                  * inode with stat. We need to get an inode
367                  * so that we can set the acl with dentry
368                  */
369                 inode = v9fs_get_inode(dir->i_sb, mode);
370                 if (IS_ERR(inode)) {
371                         err = PTR_ERR(inode);
372                         goto error;
373                 }
374                 d_instantiate(dentry, inode);
375         }
376         /* Now set the ACL based on the default value */
377         v9fs_set_create_acl(dentry, dacl, pacl);
378         inc_nlink(dir);
379         v9fs_invalidate_inode_attr(dir);
380 error:
381         if (fid)
382                 p9_client_clunk(fid);
383         return err;
384 }
385
386 static int
387 v9fs_vfs_getattr_dotl(struct vfsmount *mnt, struct dentry *dentry,
388                  struct kstat *stat)
389 {
390         int err;
391         struct v9fs_session_info *v9ses;
392         struct p9_fid *fid;
393         struct p9_stat_dotl *st;
394
395         P9_DPRINTK(P9_DEBUG_VFS, "dentry: %p\n", dentry);
396         err = -EPERM;
397         v9ses = v9fs_inode2v9ses(dentry->d_inode);
398         if (v9ses->cache == CACHE_LOOSE || v9ses->cache == CACHE_FSCACHE) {
399                 generic_fillattr(dentry->d_inode, stat);
400                 return 0;
401         }
402         fid = v9fs_fid_lookup(dentry);
403         if (IS_ERR(fid))
404                 return PTR_ERR(fid);
405
406         /* Ask for all the fields in stat structure. Server will return
407          * whatever it supports
408          */
409
410         st = p9_client_getattr_dotl(fid, P9_STATS_ALL);
411         if (IS_ERR(st))
412                 return PTR_ERR(st);
413
414         v9fs_stat2inode_dotl(st, dentry->d_inode);
415         generic_fillattr(dentry->d_inode, stat);
416         /* Change block size to what the server returned */
417         stat->blksize = st->st_blksize;
418
419         kfree(st);
420         return 0;
421 }
422
423 /**
424  * v9fs_vfs_setattr_dotl - set file metadata
425  * @dentry: file whose metadata to set
426  * @iattr: metadata assignment structure
427  *
428  */
429
430 int v9fs_vfs_setattr_dotl(struct dentry *dentry, struct iattr *iattr)
431 {
432         int retval;
433         struct v9fs_session_info *v9ses;
434         struct p9_fid *fid;
435         struct p9_iattr_dotl p9attr;
436
437         P9_DPRINTK(P9_DEBUG_VFS, "\n");
438
439         retval = inode_change_ok(dentry->d_inode, iattr);
440         if (retval)
441                 return retval;
442
443         p9attr.valid = iattr->ia_valid;
444         p9attr.mode = iattr->ia_mode;
445         p9attr.uid = iattr->ia_uid;
446         p9attr.gid = iattr->ia_gid;
447         p9attr.size = iattr->ia_size;
448         p9attr.atime_sec = iattr->ia_atime.tv_sec;
449         p9attr.atime_nsec = iattr->ia_atime.tv_nsec;
450         p9attr.mtime_sec = iattr->ia_mtime.tv_sec;
451         p9attr.mtime_nsec = iattr->ia_mtime.tv_nsec;
452
453         retval = -EPERM;
454         v9ses = v9fs_inode2v9ses(dentry->d_inode);
455         fid = v9fs_fid_lookup(dentry);
456         if (IS_ERR(fid))
457                 return PTR_ERR(fid);
458
459         /* Write all dirty data */
460         if (S_ISREG(dentry->d_inode->i_mode))
461                 filemap_write_and_wait(dentry->d_inode->i_mapping);
462
463         retval = p9_client_setattr(fid, &p9attr);
464         if (retval < 0)
465                 return retval;
466
467         if ((iattr->ia_valid & ATTR_SIZE) &&
468             iattr->ia_size != i_size_read(dentry->d_inode))
469                 truncate_setsize(dentry->d_inode, iattr->ia_size);
470
471         v9fs_invalidate_inode_attr(dentry->d_inode);
472         setattr_copy(dentry->d_inode, iattr);
473         mark_inode_dirty(dentry->d_inode);
474         if (iattr->ia_valid & ATTR_MODE) {
475                 /* We also want to update ACL when we update mode bits */
476                 retval = v9fs_acl_chmod(dentry);
477                 if (retval < 0)
478                         return retval;
479         }
480         return 0;
481 }
482
483 /**
484  * v9fs_stat2inode_dotl - populate an inode structure with stat info
485  * @stat: stat structure
486  * @inode: inode to populate
487  * @sb: superblock of filesystem
488  *
489  */
490
491 void
492 v9fs_stat2inode_dotl(struct p9_stat_dotl *stat, struct inode *inode)
493 {
494         struct v9fs_inode *v9inode = V9FS_I(inode);
495
496         if ((stat->st_result_mask & P9_STATS_BASIC) == P9_STATS_BASIC) {
497                 inode->i_atime.tv_sec = stat->st_atime_sec;
498                 inode->i_atime.tv_nsec = stat->st_atime_nsec;
499                 inode->i_mtime.tv_sec = stat->st_mtime_sec;
500                 inode->i_mtime.tv_nsec = stat->st_mtime_nsec;
501                 inode->i_ctime.tv_sec = stat->st_ctime_sec;
502                 inode->i_ctime.tv_nsec = stat->st_ctime_nsec;
503                 inode->i_uid = stat->st_uid;
504                 inode->i_gid = stat->st_gid;
505                 inode->i_nlink = stat->st_nlink;
506                 inode->i_mode = stat->st_mode;
507                 inode->i_rdev = new_decode_dev(stat->st_rdev);
508
509                 if ((S_ISBLK(inode->i_mode)) || (S_ISCHR(inode->i_mode)))
510                         init_special_inode(inode, inode->i_mode, inode->i_rdev);
511
512                 i_size_write(inode, stat->st_size);
513                 inode->i_blocks = stat->st_blocks;
514         } else {
515                 if (stat->st_result_mask & P9_STATS_ATIME) {
516                         inode->i_atime.tv_sec = stat->st_atime_sec;
517                         inode->i_atime.tv_nsec = stat->st_atime_nsec;
518                 }
519                 if (stat->st_result_mask & P9_STATS_MTIME) {
520                         inode->i_mtime.tv_sec = stat->st_mtime_sec;
521                         inode->i_mtime.tv_nsec = stat->st_mtime_nsec;
522                 }
523                 if (stat->st_result_mask & P9_STATS_CTIME) {
524                         inode->i_ctime.tv_sec = stat->st_ctime_sec;
525                         inode->i_ctime.tv_nsec = stat->st_ctime_nsec;
526                 }
527                 if (stat->st_result_mask & P9_STATS_UID)
528                         inode->i_uid = stat->st_uid;
529                 if (stat->st_result_mask & P9_STATS_GID)
530                         inode->i_gid = stat->st_gid;
531                 if (stat->st_result_mask & P9_STATS_NLINK)
532                         inode->i_nlink = stat->st_nlink;
533                 if (stat->st_result_mask & P9_STATS_MODE) {
534                         inode->i_mode = stat->st_mode;
535                         if ((S_ISBLK(inode->i_mode)) ||
536                                                 (S_ISCHR(inode->i_mode)))
537                                 init_special_inode(inode, inode->i_mode,
538                                                                 inode->i_rdev);
539                 }
540                 if (stat->st_result_mask & P9_STATS_RDEV)
541                         inode->i_rdev = new_decode_dev(stat->st_rdev);
542                 if (stat->st_result_mask & P9_STATS_SIZE)
543                         i_size_write(inode, stat->st_size);
544                 if (stat->st_result_mask & P9_STATS_BLOCKS)
545                         inode->i_blocks = stat->st_blocks;
546         }
547         if (stat->st_result_mask & P9_STATS_GEN)
548                         inode->i_generation = stat->st_gen;
549
550         /* Currently we don't support P9_STATS_BTIME and P9_STATS_DATA_VERSION
551          * because the inode structure does not have fields for them.
552          */
553         v9inode->cache_validity &= ~V9FS_INO_INVALID_ATTR;
554 }
555
556 static int
557 v9fs_vfs_symlink_dotl(struct inode *dir, struct dentry *dentry,
558                 const char *symname)
559 {
560         int err;
561         gid_t gid;
562         char *name;
563         struct p9_qid qid;
564         struct inode *inode;
565         struct p9_fid *dfid;
566         struct p9_fid *fid = NULL;
567         struct v9fs_session_info *v9ses;
568
569         name = (char *) dentry->d_name.name;
570         P9_DPRINTK(P9_DEBUG_VFS, "v9fs_vfs_symlink_dotl : %lu,%s,%s\n",
571                         dir->i_ino, name, symname);
572         v9ses = v9fs_inode2v9ses(dir);
573
574         dfid = v9fs_fid_lookup(dentry->d_parent);
575         if (IS_ERR(dfid)) {
576                 err = PTR_ERR(dfid);
577                 P9_DPRINTK(P9_DEBUG_VFS, "fid lookup failed %d\n", err);
578                 return err;
579         }
580
581         gid = v9fs_get_fsgid_for_create(dir);
582
583         /* Server doesn't alter fid on TSYMLINK. Hence no need to clone it. */
584         err = p9_client_symlink(dfid, name, (char *)symname, gid, &qid);
585
586         if (err < 0) {
587                 P9_DPRINTK(P9_DEBUG_VFS, "p9_client_symlink failed %d\n", err);
588                 goto error;
589         }
590
591         v9fs_invalidate_inode_attr(dir);
592         if (v9ses->cache) {
593                 /* Now walk from the parent so we can get an unopened fid. */
594                 fid = p9_client_walk(dfid, 1, &name, 1);
595                 if (IS_ERR(fid)) {
596                         err = PTR_ERR(fid);
597                         P9_DPRINTK(P9_DEBUG_VFS, "p9_client_walk failed %d\n",
598                                         err);
599                         fid = NULL;
600                         goto error;
601                 }
602
603                 /* instantiate inode and assign the unopened fid to dentry */
604                 inode = v9fs_get_inode_from_fid(v9ses, fid, dir->i_sb);
605                 if (IS_ERR(inode)) {
606                         err = PTR_ERR(inode);
607                         P9_DPRINTK(P9_DEBUG_VFS, "inode creation failed %d\n",
608                                         err);
609                         goto error;
610                 }
611                 d_instantiate(dentry, inode);
612                 err = v9fs_fid_add(dentry, fid);
613                 if (err < 0)
614                         goto error;
615                 fid = NULL;
616         } else {
617                 /* Not in cached mode. No need to populate inode with stat */
618                 inode = v9fs_get_inode(dir->i_sb, S_IFLNK);
619                 if (IS_ERR(inode)) {
620                         err = PTR_ERR(inode);
621                         goto error;
622                 }
623                 d_instantiate(dentry, inode);
624         }
625
626 error:
627         if (fid)
628                 p9_client_clunk(fid);
629
630         return err;
631 }
632
633 /**
634  * v9fs_vfs_link_dotl - create a hardlink for dotl
635  * @old_dentry: dentry for file to link to
636  * @dir: inode destination for new link
637  * @dentry: dentry for link
638  *
639  */
640
641 static int
642 v9fs_vfs_link_dotl(struct dentry *old_dentry, struct inode *dir,
643                 struct dentry *dentry)
644 {
645         int err;
646         char *name;
647         struct dentry *dir_dentry;
648         struct p9_fid *dfid, *oldfid;
649         struct v9fs_session_info *v9ses;
650
651         P9_DPRINTK(P9_DEBUG_VFS, "dir ino: %lu, old_name: %s, new_name: %s\n",
652                         dir->i_ino, old_dentry->d_name.name,
653                         dentry->d_name.name);
654
655         v9ses = v9fs_inode2v9ses(dir);
656         dir_dentry = v9fs_dentry_from_dir_inode(dir);
657         dfid = v9fs_fid_lookup(dir_dentry);
658         if (IS_ERR(dfid))
659                 return PTR_ERR(dfid);
660
661         oldfid = v9fs_fid_lookup(old_dentry);
662         if (IS_ERR(oldfid))
663                 return PTR_ERR(oldfid);
664
665         name = (char *) dentry->d_name.name;
666
667         err = p9_client_link(dfid, oldfid, (char *)dentry->d_name.name);
668
669         if (err < 0) {
670                 P9_DPRINTK(P9_DEBUG_VFS, "p9_client_link failed %d\n", err);
671                 return err;
672         }
673
674         v9fs_invalidate_inode_attr(dir);
675         if (v9ses->cache == CACHE_LOOSE || v9ses->cache == CACHE_FSCACHE) {
676                 /* Get the latest stat info from server. */
677                 struct p9_fid *fid;
678                 fid = v9fs_fid_lookup(old_dentry);
679                 if (IS_ERR(fid))
680                         return PTR_ERR(fid);
681
682                 v9fs_refresh_inode_dotl(fid, old_dentry->d_inode);
683         }
684         ihold(old_dentry->d_inode);
685         d_instantiate(dentry, old_dentry->d_inode);
686
687         return err;
688 }
689
690 /**
691  * v9fs_vfs_mknod_dotl - create a special file
692  * @dir: inode destination for new link
693  * @dentry: dentry for file
694  * @mode: mode for creation
695  * @rdev: device associated with special file
696  *
697  */
698 static int
699 v9fs_vfs_mknod_dotl(struct inode *dir, struct dentry *dentry, int omode,
700                 dev_t rdev)
701 {
702         int err;
703         gid_t gid;
704         char *name;
705         mode_t mode;
706         struct v9fs_session_info *v9ses;
707         struct p9_fid *fid = NULL, *dfid = NULL;
708         struct inode *inode;
709         struct p9_qid qid;
710         struct dentry *dir_dentry;
711         struct posix_acl *dacl = NULL, *pacl = NULL;
712
713         P9_DPRINTK(P9_DEBUG_VFS,
714                 " %lu,%s mode: %x MAJOR: %u MINOR: %u\n", dir->i_ino,
715                 dentry->d_name.name, omode, MAJOR(rdev), MINOR(rdev));
716
717         if (!new_valid_dev(rdev))
718                 return -EINVAL;
719
720         v9ses = v9fs_inode2v9ses(dir);
721         dir_dentry = v9fs_dentry_from_dir_inode(dir);
722         dfid = v9fs_fid_lookup(dir_dentry);
723         if (IS_ERR(dfid)) {
724                 err = PTR_ERR(dfid);
725                 P9_DPRINTK(P9_DEBUG_VFS, "fid lookup failed %d\n", err);
726                 dfid = NULL;
727                 goto error;
728         }
729
730         gid = v9fs_get_fsgid_for_create(dir);
731         mode = omode;
732         /* Update mode based on ACL value */
733         err = v9fs_acl_mode(dir, &mode, &dacl, &pacl);
734         if (err) {
735                 P9_DPRINTK(P9_DEBUG_VFS,
736                            "Failed to get acl values in mknod %d\n", err);
737                 goto error;
738         }
739         name = (char *) dentry->d_name.name;
740
741         err = p9_client_mknod_dotl(dfid, name, mode, rdev, gid, &qid);
742         if (err < 0)
743                 goto error;
744
745         v9fs_invalidate_inode_attr(dir);
746         /* instantiate inode and assign the unopened fid to the dentry */
747         if (v9ses->cache == CACHE_LOOSE || v9ses->cache == CACHE_FSCACHE) {
748                 fid = p9_client_walk(dfid, 1, &name, 1);
749                 if (IS_ERR(fid)) {
750                         err = PTR_ERR(fid);
751                         P9_DPRINTK(P9_DEBUG_VFS, "p9_client_walk failed %d\n",
752                                 err);
753                         fid = NULL;
754                         goto error;
755                 }
756
757                 inode = v9fs_get_inode_from_fid(v9ses, fid, dir->i_sb);
758                 if (IS_ERR(inode)) {
759                         err = PTR_ERR(inode);
760                         P9_DPRINTK(P9_DEBUG_VFS, "inode creation failed %d\n",
761                                 err);
762                         goto error;
763                 }
764                 d_instantiate(dentry, inode);
765                 err = v9fs_fid_add(dentry, fid);
766                 if (err < 0)
767                         goto error;
768                 fid = NULL;
769         } else {
770                 /*
771                  * Not in cached mode. No need to populate inode with stat.
772                  * socket syscall returns a fd, so we need instantiate
773                  */
774                 inode = v9fs_get_inode(dir->i_sb, mode);
775                 if (IS_ERR(inode)) {
776                         err = PTR_ERR(inode);
777                         goto error;
778                 }
779                 d_instantiate(dentry, inode);
780         }
781         /* Now set the ACL based on the default value */
782         v9fs_set_create_acl(dentry, dacl, pacl);
783 error:
784         if (fid)
785                 p9_client_clunk(fid);
786         return err;
787 }
788
789 /**
790  * v9fs_vfs_follow_link_dotl - follow a symlink path
791  * @dentry: dentry for symlink
792  * @nd: nameidata
793  *
794  */
795
796 static void *
797 v9fs_vfs_follow_link_dotl(struct dentry *dentry, struct nameidata *nd)
798 {
799         int retval;
800         struct p9_fid *fid;
801         char *link = __getname();
802         char *target;
803
804         P9_DPRINTK(P9_DEBUG_VFS, "%s\n", dentry->d_name.name);
805
806         if (!link) {
807                 link = ERR_PTR(-ENOMEM);
808                 goto ndset;
809         }
810         fid = v9fs_fid_lookup(dentry);
811         if (IS_ERR(fid)) {
812                 __putname(link);
813                 link = ERR_PTR(PTR_ERR(fid));
814                 goto ndset;
815         }
816         retval = p9_client_readlink(fid, &target);
817         if (!retval) {
818                 strcpy(link, target);
819                 kfree(target);
820                 goto ndset;
821         }
822         __putname(link);
823         link = ERR_PTR(retval);
824 ndset:
825         nd_set_link(nd, link);
826         return NULL;
827 }
828
829 int v9fs_refresh_inode_dotl(struct p9_fid *fid, struct inode *inode)
830 {
831         loff_t i_size;
832         struct p9_stat_dotl *st;
833         struct v9fs_session_info *v9ses;
834
835         v9ses = v9fs_inode2v9ses(inode);
836         st = p9_client_getattr_dotl(fid, P9_STATS_ALL);
837         if (IS_ERR(st))
838                 return PTR_ERR(st);
839
840         spin_lock(&inode->i_lock);
841         /*
842          * We don't want to refresh inode->i_size,
843          * because we may have cached data
844          */
845         i_size = inode->i_size;
846         v9fs_stat2inode_dotl(st, inode);
847         if (v9ses->cache)
848                 inode->i_size = i_size;
849         spin_unlock(&inode->i_lock);
850         kfree(st);
851         return 0;
852 }
853
854 const struct inode_operations v9fs_dir_inode_operations_dotl = {
855         .create = v9fs_vfs_create_dotl,
856         .lookup = v9fs_vfs_lookup,
857         .link = v9fs_vfs_link_dotl,
858         .symlink = v9fs_vfs_symlink_dotl,
859         .unlink = v9fs_vfs_unlink,
860         .mkdir = v9fs_vfs_mkdir_dotl,
861         .rmdir = v9fs_vfs_rmdir,
862         .mknod = v9fs_vfs_mknod_dotl,
863         .rename = v9fs_vfs_rename,
864         .getattr = v9fs_vfs_getattr_dotl,
865         .setattr = v9fs_vfs_setattr_dotl,
866         .setxattr = generic_setxattr,
867         .getxattr = generic_getxattr,
868         .removexattr = generic_removexattr,
869         .listxattr = v9fs_listxattr,
870         .check_acl = v9fs_check_acl,
871 };
872
873 const struct inode_operations v9fs_file_inode_operations_dotl = {
874         .getattr = v9fs_vfs_getattr_dotl,
875         .setattr = v9fs_vfs_setattr_dotl,
876         .setxattr = generic_setxattr,
877         .getxattr = generic_getxattr,
878         .removexattr = generic_removexattr,
879         .listxattr = v9fs_listxattr,
880         .check_acl = v9fs_check_acl,
881 };
882
883 const struct inode_operations v9fs_symlink_inode_operations_dotl = {
884         .readlink = generic_readlink,
885         .follow_link = v9fs_vfs_follow_link_dotl,
886         .put_link = v9fs_vfs_put_link,
887         .getattr = v9fs_vfs_getattr_dotl,
888         .setattr = v9fs_vfs_setattr_dotl,
889         .setxattr = generic_setxattr,
890         .getxattr = generic_getxattr,
891         .removexattr = generic_removexattr,
892         .listxattr = v9fs_listxattr,
893 };