Merge branches 'acpica', 'acpi-video' and 'acpi-fan'
[linux-2.6-block.git] / fs / nfsd / vfs.c
1 /*
2  * File operations used by nfsd. Some of these have been ripped from
3  * other parts of the kernel because they weren't exported, others
4  * are partial duplicates with added or changed functionality.
5  *
6  * Note that several functions dget() the dentry upon which they want
7  * to act, most notably those that create directory entries. Response
8  * dentry's are dput()'d if necessary in the release callback.
9  * So if you notice code paths that apparently fail to dput() the
10  * dentry, don't worry--they have been taken care of.
11  *
12  * Copyright (C) 1995-1999 Olaf Kirch <okir@monad.swb.de>
13  * Zerocpy NFS support (C) 2002 Hirokazu Takahashi <taka@valinux.co.jp>
14  */
15
16 #include <linux/fs.h>
17 #include <linux/file.h>
18 #include <linux/splice.h>
19 #include <linux/falloc.h>
20 #include <linux/fcntl.h>
21 #include <linux/namei.h>
22 #include <linux/delay.h>
23 #include <linux/fsnotify.h>
24 #include <linux/posix_acl_xattr.h>
25 #include <linux/xattr.h>
26 #include <linux/jhash.h>
27 #include <linux/ima.h>
28 #include <linux/slab.h>
29 #include <asm/uaccess.h>
30 #include <linux/exportfs.h>
31 #include <linux/writeback.h>
32 #include <linux/security.h>
33
34 #ifdef CONFIG_NFSD_V3
35 #include "xdr3.h"
36 #endif /* CONFIG_NFSD_V3 */
37
38 #ifdef CONFIG_NFSD_V4
39 #include "../internal.h"
40 #include "acl.h"
41 #include "idmap.h"
42 #endif /* CONFIG_NFSD_V4 */
43
44 #include "nfsd.h"
45 #include "vfs.h"
46
47 #define NFSDDBG_FACILITY                NFSDDBG_FILEOP
48
49
50 /*
51  * This is a cache of readahead params that help us choose the proper
52  * readahead strategy. Initially, we set all readahead parameters to 0
53  * and let the VFS handle things.
54  * If you increase the number of cached files very much, you'll need to
55  * add a hash table here.
56  */
57 struct raparms {
58         struct raparms          *p_next;
59         unsigned int            p_count;
60         ino_t                   p_ino;
61         dev_t                   p_dev;
62         int                     p_set;
63         struct file_ra_state    p_ra;
64         unsigned int            p_hindex;
65 };
66
67 struct raparm_hbucket {
68         struct raparms          *pb_head;
69         spinlock_t              pb_lock;
70 } ____cacheline_aligned_in_smp;
71
72 #define RAPARM_HASH_BITS        4
73 #define RAPARM_HASH_SIZE        (1<<RAPARM_HASH_BITS)
74 #define RAPARM_HASH_MASK        (RAPARM_HASH_SIZE-1)
75 static struct raparm_hbucket    raparm_hash[RAPARM_HASH_SIZE];
76
77 /* 
78  * Called from nfsd_lookup and encode_dirent. Check if we have crossed 
79  * a mount point.
80  * Returns -EAGAIN or -ETIMEDOUT leaving *dpp and *expp unchanged,
81  *  or nfs_ok having possibly changed *dpp and *expp
82  */
83 int
84 nfsd_cross_mnt(struct svc_rqst *rqstp, struct dentry **dpp, 
85                         struct svc_export **expp)
86 {
87         struct svc_export *exp = *expp, *exp2 = NULL;
88         struct dentry *dentry = *dpp;
89         struct path path = {.mnt = mntget(exp->ex_path.mnt),
90                             .dentry = dget(dentry)};
91         int err = 0;
92
93         err = follow_down(&path);
94         if (err < 0)
95                 goto out;
96
97         exp2 = rqst_exp_get_by_name(rqstp, &path);
98         if (IS_ERR(exp2)) {
99                 err = PTR_ERR(exp2);
100                 /*
101                  * We normally allow NFS clients to continue
102                  * "underneath" a mountpoint that is not exported.
103                  * The exception is V4ROOT, where no traversal is ever
104                  * allowed without an explicit export of the new
105                  * directory.
106                  */
107                 if (err == -ENOENT && !(exp->ex_flags & NFSEXP_V4ROOT))
108                         err = 0;
109                 path_put(&path);
110                 goto out;
111         }
112         if (nfsd_v4client(rqstp) ||
113                 (exp->ex_flags & NFSEXP_CROSSMOUNT) || EX_NOHIDE(exp2)) {
114                 /* successfully crossed mount point */
115                 /*
116                  * This is subtle: path.dentry is *not* on path.mnt
117                  * at this point.  The only reason we are safe is that
118                  * original mnt is pinned down by exp, so we should
119                  * put path *before* putting exp
120                  */
121                 *dpp = path.dentry;
122                 path.dentry = dentry;
123                 *expp = exp2;
124                 exp2 = exp;
125         }
126         path_put(&path);
127         exp_put(exp2);
128 out:
129         return err;
130 }
131
132 static void follow_to_parent(struct path *path)
133 {
134         struct dentry *dp;
135
136         while (path->dentry == path->mnt->mnt_root && follow_up(path))
137                 ;
138         dp = dget_parent(path->dentry);
139         dput(path->dentry);
140         path->dentry = dp;
141 }
142
143 static int nfsd_lookup_parent(struct svc_rqst *rqstp, struct dentry *dparent, struct svc_export **exp, struct dentry **dentryp)
144 {
145         struct svc_export *exp2;
146         struct path path = {.mnt = mntget((*exp)->ex_path.mnt),
147                             .dentry = dget(dparent)};
148
149         follow_to_parent(&path);
150
151         exp2 = rqst_exp_parent(rqstp, &path);
152         if (PTR_ERR(exp2) == -ENOENT) {
153                 *dentryp = dget(dparent);
154         } else if (IS_ERR(exp2)) {
155                 path_put(&path);
156                 return PTR_ERR(exp2);
157         } else {
158                 *dentryp = dget(path.dentry);
159                 exp_put(*exp);
160                 *exp = exp2;
161         }
162         path_put(&path);
163         return 0;
164 }
165
166 /*
167  * For nfsd purposes, we treat V4ROOT exports as though there was an
168  * export at *every* directory.
169  */
170 int nfsd_mountpoint(struct dentry *dentry, struct svc_export *exp)
171 {
172         if (d_mountpoint(dentry))
173                 return 1;
174         if (nfsd4_is_junction(dentry))
175                 return 1;
176         if (!(exp->ex_flags & NFSEXP_V4ROOT))
177                 return 0;
178         return d_inode(dentry) != NULL;
179 }
180
181 __be32
182 nfsd_lookup_dentry(struct svc_rqst *rqstp, struct svc_fh *fhp,
183                    const char *name, unsigned int len,
184                    struct svc_export **exp_ret, struct dentry **dentry_ret)
185 {
186         struct svc_export       *exp;
187         struct dentry           *dparent;
188         struct dentry           *dentry;
189         int                     host_err;
190
191         dprintk("nfsd: nfsd_lookup(fh %s, %.*s)\n", SVCFH_fmt(fhp), len,name);
192
193         dparent = fhp->fh_dentry;
194         exp = exp_get(fhp->fh_export);
195
196         /* Lookup the name, but don't follow links */
197         if (isdotent(name, len)) {
198                 if (len==1)
199                         dentry = dget(dparent);
200                 else if (dparent != exp->ex_path.dentry)
201                         dentry = dget_parent(dparent);
202                 else if (!EX_NOHIDE(exp) && !nfsd_v4client(rqstp))
203                         dentry = dget(dparent); /* .. == . just like at / */
204                 else {
205                         /* checking mountpoint crossing is very different when stepping up */
206                         host_err = nfsd_lookup_parent(rqstp, dparent, &exp, &dentry);
207                         if (host_err)
208                                 goto out_nfserr;
209                 }
210         } else {
211                 /*
212                  * In the nfsd4_open() case, this may be held across
213                  * subsequent open and delegation acquisition which may
214                  * need to take the child's i_mutex:
215                  */
216                 fh_lock_nested(fhp, I_MUTEX_PARENT);
217                 dentry = lookup_one_len(name, dparent, len);
218                 host_err = PTR_ERR(dentry);
219                 if (IS_ERR(dentry))
220                         goto out_nfserr;
221                 if (nfsd_mountpoint(dentry, exp)) {
222                         /*
223                          * We don't need the i_mutex after all.  It's
224                          * still possible we could open this (regular
225                          * files can be mountpoints too), but the
226                          * i_mutex is just there to prevent renames of
227                          * something that we might be about to delegate,
228                          * and a mountpoint won't be renamed:
229                          */
230                         fh_unlock(fhp);
231                         if ((host_err = nfsd_cross_mnt(rqstp, &dentry, &exp))) {
232                                 dput(dentry);
233                                 goto out_nfserr;
234                         }
235                 }
236         }
237         *dentry_ret = dentry;
238         *exp_ret = exp;
239         return 0;
240
241 out_nfserr:
242         exp_put(exp);
243         return nfserrno(host_err);
244 }
245
246 /*
247  * Look up one component of a pathname.
248  * N.B. After this call _both_ fhp and resfh need an fh_put
249  *
250  * If the lookup would cross a mountpoint, and the mounted filesystem
251  * is exported to the client with NFSEXP_NOHIDE, then the lookup is
252  * accepted as it stands and the mounted directory is
253  * returned. Otherwise the covered directory is returned.
254  * NOTE: this mountpoint crossing is not supported properly by all
255  *   clients and is explicitly disallowed for NFSv3
256  *      NeilBrown <neilb@cse.unsw.edu.au>
257  */
258 __be32
259 nfsd_lookup(struct svc_rqst *rqstp, struct svc_fh *fhp, const char *name,
260                                 unsigned int len, struct svc_fh *resfh)
261 {
262         struct svc_export       *exp;
263         struct dentry           *dentry;
264         __be32 err;
265
266         err = fh_verify(rqstp, fhp, S_IFDIR, NFSD_MAY_EXEC);
267         if (err)
268                 return err;
269         err = nfsd_lookup_dentry(rqstp, fhp, name, len, &exp, &dentry);
270         if (err)
271                 return err;
272         err = check_nfsd_access(exp, rqstp);
273         if (err)
274                 goto out;
275         /*
276          * Note: we compose the file handle now, but as the
277          * dentry may be negative, it may need to be updated.
278          */
279         err = fh_compose(resfh, exp, dentry, fhp);
280         if (!err && d_really_is_negative(dentry))
281                 err = nfserr_noent;
282 out:
283         dput(dentry);
284         exp_put(exp);
285         return err;
286 }
287
288 /*
289  * Commit metadata changes to stable storage.
290  */
291 static int
292 commit_metadata(struct svc_fh *fhp)
293 {
294         struct inode *inode = d_inode(fhp->fh_dentry);
295         const struct export_operations *export_ops = inode->i_sb->s_export_op;
296
297         if (!EX_ISSYNC(fhp->fh_export))
298                 return 0;
299
300         if (export_ops->commit_metadata)
301                 return export_ops->commit_metadata(inode);
302         return sync_inode_metadata(inode, 1);
303 }
304
305 /*
306  * Go over the attributes and take care of the small differences between
307  * NFS semantics and what Linux expects.
308  */
309 static void
310 nfsd_sanitize_attrs(struct inode *inode, struct iattr *iap)
311 {
312         /* sanitize the mode change */
313         if (iap->ia_valid & ATTR_MODE) {
314                 iap->ia_mode &= S_IALLUGO;
315                 iap->ia_mode |= (inode->i_mode & ~S_IALLUGO);
316         }
317
318         /* Revoke setuid/setgid on chown */
319         if (!S_ISDIR(inode->i_mode) &&
320             ((iap->ia_valid & ATTR_UID) || (iap->ia_valid & ATTR_GID))) {
321                 iap->ia_valid |= ATTR_KILL_PRIV;
322                 if (iap->ia_valid & ATTR_MODE) {
323                         /* we're setting mode too, just clear the s*id bits */
324                         iap->ia_mode &= ~S_ISUID;
325                         if (iap->ia_mode & S_IXGRP)
326                                 iap->ia_mode &= ~S_ISGID;
327                 } else {
328                         /* set ATTR_KILL_* bits and let VFS handle it */
329                         iap->ia_valid |= (ATTR_KILL_SUID | ATTR_KILL_SGID);
330                 }
331         }
332 }
333
334 static __be32
335 nfsd_get_write_access(struct svc_rqst *rqstp, struct svc_fh *fhp,
336                 struct iattr *iap)
337 {
338         struct inode *inode = d_inode(fhp->fh_dentry);
339         int host_err;
340
341         if (iap->ia_size < inode->i_size) {
342                 __be32 err;
343
344                 err = nfsd_permission(rqstp, fhp->fh_export, fhp->fh_dentry,
345                                 NFSD_MAY_TRUNC | NFSD_MAY_OWNER_OVERRIDE);
346                 if (err)
347                         return err;
348         }
349
350         host_err = get_write_access(inode);
351         if (host_err)
352                 goto out_nfserrno;
353
354         host_err = locks_verify_truncate(inode, NULL, iap->ia_size);
355         if (host_err)
356                 goto out_put_write_access;
357         return 0;
358
359 out_put_write_access:
360         put_write_access(inode);
361 out_nfserrno:
362         return nfserrno(host_err);
363 }
364
365 /*
366  * Set various file attributes.  After this call fhp needs an fh_put.
367  */
368 __be32
369 nfsd_setattr(struct svc_rqst *rqstp, struct svc_fh *fhp, struct iattr *iap,
370              int check_guard, time_t guardtime)
371 {
372         struct dentry   *dentry;
373         struct inode    *inode;
374         int             accmode = NFSD_MAY_SATTR;
375         umode_t         ftype = 0;
376         __be32          err;
377         int             host_err;
378         bool            get_write_count;
379         int             size_change = 0;
380
381         if (iap->ia_valid & (ATTR_ATIME | ATTR_MTIME | ATTR_SIZE))
382                 accmode |= NFSD_MAY_WRITE|NFSD_MAY_OWNER_OVERRIDE;
383         if (iap->ia_valid & ATTR_SIZE)
384                 ftype = S_IFREG;
385
386         /* Callers that do fh_verify should do the fh_want_write: */
387         get_write_count = !fhp->fh_dentry;
388
389         /* Get inode */
390         err = fh_verify(rqstp, fhp, ftype, accmode);
391         if (err)
392                 goto out;
393         if (get_write_count) {
394                 host_err = fh_want_write(fhp);
395                 if (host_err)
396                         return nfserrno(host_err);
397         }
398
399         dentry = fhp->fh_dentry;
400         inode = d_inode(dentry);
401
402         /* Ignore any mode updates on symlinks */
403         if (S_ISLNK(inode->i_mode))
404                 iap->ia_valid &= ~ATTR_MODE;
405
406         if (!iap->ia_valid)
407                 goto out;
408
409         nfsd_sanitize_attrs(inode, iap);
410
411         /*
412          * The size case is special, it changes the file in addition to the
413          * attributes.
414          */
415         if (iap->ia_valid & ATTR_SIZE) {
416                 err = nfsd_get_write_access(rqstp, fhp, iap);
417                 if (err)
418                         goto out;
419                 size_change = 1;
420
421                 /*
422                  * RFC5661, Section 18.30.4:
423                  *   Changing the size of a file with SETATTR indirectly
424                  *   changes the time_modify and change attributes.
425                  *
426                  * (and similar for the older RFCs)
427                  */
428                 if (iap->ia_size != i_size_read(inode))
429                         iap->ia_valid |= ATTR_MTIME;
430         }
431
432         iap->ia_valid |= ATTR_CTIME;
433
434         if (check_guard && guardtime != inode->i_ctime.tv_sec) {
435                 err = nfserr_notsync;
436                 goto out_put_write_access;
437         }
438
439         fh_lock(fhp);
440         host_err = notify_change(dentry, iap, NULL);
441         fh_unlock(fhp);
442         err = nfserrno(host_err);
443
444 out_put_write_access:
445         if (size_change)
446                 put_write_access(inode);
447         if (!err)
448                 err = nfserrno(commit_metadata(fhp));
449 out:
450         return err;
451 }
452
453 #if defined(CONFIG_NFSD_V4)
454 /*
455  * NFS junction information is stored in an extended attribute.
456  */
457 #define NFSD_JUNCTION_XATTR_NAME        XATTR_TRUSTED_PREFIX "junction.nfs"
458
459 /**
460  * nfsd4_is_junction - Test if an object could be an NFS junction
461  *
462  * @dentry: object to test
463  *
464  * Returns 1 if "dentry" appears to contain NFS junction information.
465  * Otherwise 0 is returned.
466  */
467 int nfsd4_is_junction(struct dentry *dentry)
468 {
469         struct inode *inode = d_inode(dentry);
470
471         if (inode == NULL)
472                 return 0;
473         if (inode->i_mode & S_IXUGO)
474                 return 0;
475         if (!(inode->i_mode & S_ISVTX))
476                 return 0;
477         if (vfs_getxattr(dentry, NFSD_JUNCTION_XATTR_NAME, NULL, 0) <= 0)
478                 return 0;
479         return 1;
480 }
481 #ifdef CONFIG_NFSD_V4_SECURITY_LABEL
482 __be32 nfsd4_set_nfs4_label(struct svc_rqst *rqstp, struct svc_fh *fhp,
483                 struct xdr_netobj *label)
484 {
485         __be32 error;
486         int host_error;
487         struct dentry *dentry;
488
489         error = fh_verify(rqstp, fhp, 0 /* S_IFREG */, NFSD_MAY_SATTR);
490         if (error)
491                 return error;
492
493         dentry = fhp->fh_dentry;
494
495         mutex_lock(&d_inode(dentry)->i_mutex);
496         host_error = security_inode_setsecctx(dentry, label->data, label->len);
497         mutex_unlock(&d_inode(dentry)->i_mutex);
498         return nfserrno(host_error);
499 }
500 #else
501 __be32 nfsd4_set_nfs4_label(struct svc_rqst *rqstp, struct svc_fh *fhp,
502                 struct xdr_netobj *label)
503 {
504         return nfserr_notsupp;
505 }
506 #endif
507
508 __be32 nfsd4_clone_file_range(struct file *src, u64 src_pos, struct file *dst,
509                 u64 dst_pos, u64 count)
510 {
511         return nfserrno(vfs_clone_file_range(src, src_pos, dst, dst_pos,
512                         count));
513 }
514
515 __be32 nfsd4_vfs_fallocate(struct svc_rqst *rqstp, struct svc_fh *fhp,
516                            struct file *file, loff_t offset, loff_t len,
517                            int flags)
518 {
519         int error;
520
521         if (!S_ISREG(file_inode(file)->i_mode))
522                 return nfserr_inval;
523
524         error = vfs_fallocate(file, flags, offset, len);
525         if (!error)
526                 error = commit_metadata(fhp);
527
528         return nfserrno(error);
529 }
530 #endif /* defined(CONFIG_NFSD_V4) */
531
532 #ifdef CONFIG_NFSD_V3
533 /*
534  * Check server access rights to a file system object
535  */
536 struct accessmap {
537         u32             access;
538         int             how;
539 };
540 static struct accessmap nfs3_regaccess[] = {
541     {   NFS3_ACCESS_READ,       NFSD_MAY_READ                   },
542     {   NFS3_ACCESS_EXECUTE,    NFSD_MAY_EXEC                   },
543     {   NFS3_ACCESS_MODIFY,     NFSD_MAY_WRITE|NFSD_MAY_TRUNC   },
544     {   NFS3_ACCESS_EXTEND,     NFSD_MAY_WRITE                  },
545
546     {   0,                      0                               }
547 };
548
549 static struct accessmap nfs3_diraccess[] = {
550     {   NFS3_ACCESS_READ,       NFSD_MAY_READ                   },
551     {   NFS3_ACCESS_LOOKUP,     NFSD_MAY_EXEC                   },
552     {   NFS3_ACCESS_MODIFY,     NFSD_MAY_EXEC|NFSD_MAY_WRITE|NFSD_MAY_TRUNC},
553     {   NFS3_ACCESS_EXTEND,     NFSD_MAY_EXEC|NFSD_MAY_WRITE    },
554     {   NFS3_ACCESS_DELETE,     NFSD_MAY_REMOVE                 },
555
556     {   0,                      0                               }
557 };
558
559 static struct accessmap nfs3_anyaccess[] = {
560         /* Some clients - Solaris 2.6 at least, make an access call
561          * to the server to check for access for things like /dev/null
562          * (which really, the server doesn't care about).  So
563          * We provide simple access checking for them, looking
564          * mainly at mode bits, and we make sure to ignore read-only
565          * filesystem checks
566          */
567     {   NFS3_ACCESS_READ,       NFSD_MAY_READ                   },
568     {   NFS3_ACCESS_EXECUTE,    NFSD_MAY_EXEC                   },
569     {   NFS3_ACCESS_MODIFY,     NFSD_MAY_WRITE|NFSD_MAY_LOCAL_ACCESS    },
570     {   NFS3_ACCESS_EXTEND,     NFSD_MAY_WRITE|NFSD_MAY_LOCAL_ACCESS    },
571
572     {   0,                      0                               }
573 };
574
575 __be32
576 nfsd_access(struct svc_rqst *rqstp, struct svc_fh *fhp, u32 *access, u32 *supported)
577 {
578         struct accessmap        *map;
579         struct svc_export       *export;
580         struct dentry           *dentry;
581         u32                     query, result = 0, sresult = 0;
582         __be32                  error;
583
584         error = fh_verify(rqstp, fhp, 0, NFSD_MAY_NOP);
585         if (error)
586                 goto out;
587
588         export = fhp->fh_export;
589         dentry = fhp->fh_dentry;
590
591         if (d_is_reg(dentry))
592                 map = nfs3_regaccess;
593         else if (d_is_dir(dentry))
594                 map = nfs3_diraccess;
595         else
596                 map = nfs3_anyaccess;
597
598
599         query = *access;
600         for  (; map->access; map++) {
601                 if (map->access & query) {
602                         __be32 err2;
603
604                         sresult |= map->access;
605
606                         err2 = nfsd_permission(rqstp, export, dentry, map->how);
607                         switch (err2) {
608                         case nfs_ok:
609                                 result |= map->access;
610                                 break;
611                                 
612                         /* the following error codes just mean the access was not allowed,
613                          * rather than an error occurred */
614                         case nfserr_rofs:
615                         case nfserr_acces:
616                         case nfserr_perm:
617                                 /* simply don't "or" in the access bit. */
618                                 break;
619                         default:
620                                 error = err2;
621                                 goto out;
622                         }
623                 }
624         }
625         *access = result;
626         if (supported)
627                 *supported = sresult;
628
629  out:
630         return error;
631 }
632 #endif /* CONFIG_NFSD_V3 */
633
634 static int nfsd_open_break_lease(struct inode *inode, int access)
635 {
636         unsigned int mode;
637
638         if (access & NFSD_MAY_NOT_BREAK_LEASE)
639                 return 0;
640         mode = (access & NFSD_MAY_WRITE) ? O_WRONLY : O_RDONLY;
641         return break_lease(inode, mode | O_NONBLOCK);
642 }
643
644 /*
645  * Open an existing file or directory.
646  * The may_flags argument indicates the type of open (read/write/lock)
647  * and additional flags.
648  * N.B. After this call fhp needs an fh_put
649  */
650 __be32
651 nfsd_open(struct svc_rqst *rqstp, struct svc_fh *fhp, umode_t type,
652                         int may_flags, struct file **filp)
653 {
654         struct path     path;
655         struct inode    *inode;
656         struct file     *file;
657         int             flags = O_RDONLY|O_LARGEFILE;
658         __be32          err;
659         int             host_err = 0;
660
661         validate_process_creds();
662
663         /*
664          * If we get here, then the client has already done an "open",
665          * and (hopefully) checked permission - so allow OWNER_OVERRIDE
666          * in case a chmod has now revoked permission.
667          *
668          * Arguably we should also allow the owner override for
669          * directories, but we never have and it doesn't seem to have
670          * caused anyone a problem.  If we were to change this, note
671          * also that our filldir callbacks would need a variant of
672          * lookup_one_len that doesn't check permissions.
673          */
674         if (type == S_IFREG)
675                 may_flags |= NFSD_MAY_OWNER_OVERRIDE;
676         err = fh_verify(rqstp, fhp, type, may_flags);
677         if (err)
678                 goto out;
679
680         path.mnt = fhp->fh_export->ex_path.mnt;
681         path.dentry = fhp->fh_dentry;
682         inode = d_inode(path.dentry);
683
684         /* Disallow write access to files with the append-only bit set
685          * or any access when mandatory locking enabled
686          */
687         err = nfserr_perm;
688         if (IS_APPEND(inode) && (may_flags & NFSD_MAY_WRITE))
689                 goto out;
690         /*
691          * We must ignore files (but only files) which might have mandatory
692          * locks on them because there is no way to know if the accesser has
693          * the lock.
694          */
695         if (S_ISREG((inode)->i_mode) && mandatory_lock(inode))
696                 goto out;
697
698         if (!inode->i_fop)
699                 goto out;
700
701         host_err = nfsd_open_break_lease(inode, may_flags);
702         if (host_err) /* NOMEM or WOULDBLOCK */
703                 goto out_nfserr;
704
705         if (may_flags & NFSD_MAY_WRITE) {
706                 if (may_flags & NFSD_MAY_READ)
707                         flags = O_RDWR|O_LARGEFILE;
708                 else
709                         flags = O_WRONLY|O_LARGEFILE;
710         }
711
712         file = dentry_open(&path, flags, current_cred());
713         if (IS_ERR(file)) {
714                 host_err = PTR_ERR(file);
715                 goto out_nfserr;
716         }
717
718         host_err = ima_file_check(file, may_flags, 0);
719         if (host_err) {
720                 fput(file);
721                 goto out_nfserr;
722         }
723
724         if (may_flags & NFSD_MAY_64BIT_COOKIE)
725                 file->f_mode |= FMODE_64BITHASH;
726         else
727                 file->f_mode |= FMODE_32BITHASH;
728
729         *filp = file;
730 out_nfserr:
731         err = nfserrno(host_err);
732 out:
733         validate_process_creds();
734         return err;
735 }
736
737 struct raparms *
738 nfsd_init_raparms(struct file *file)
739 {
740         struct inode *inode = file_inode(file);
741         dev_t dev = inode->i_sb->s_dev;
742         ino_t ino = inode->i_ino;
743         struct raparms  *ra, **rap, **frap = NULL;
744         int depth = 0;
745         unsigned int hash;
746         struct raparm_hbucket *rab;
747
748         hash = jhash_2words(dev, ino, 0xfeedbeef) & RAPARM_HASH_MASK;
749         rab = &raparm_hash[hash];
750
751         spin_lock(&rab->pb_lock);
752         for (rap = &rab->pb_head; (ra = *rap); rap = &ra->p_next) {
753                 if (ra->p_ino == ino && ra->p_dev == dev)
754                         goto found;
755                 depth++;
756                 if (ra->p_count == 0)
757                         frap = rap;
758         }
759         depth = nfsdstats.ra_size;
760         if (!frap) {    
761                 spin_unlock(&rab->pb_lock);
762                 return NULL;
763         }
764         rap = frap;
765         ra = *frap;
766         ra->p_dev = dev;
767         ra->p_ino = ino;
768         ra->p_set = 0;
769         ra->p_hindex = hash;
770 found:
771         if (rap != &rab->pb_head) {
772                 *rap = ra->p_next;
773                 ra->p_next   = rab->pb_head;
774                 rab->pb_head = ra;
775         }
776         ra->p_count++;
777         nfsdstats.ra_depth[depth*10/nfsdstats.ra_size]++;
778         spin_unlock(&rab->pb_lock);
779
780         if (ra->p_set)
781                 file->f_ra = ra->p_ra;
782         return ra;
783 }
784
785 void nfsd_put_raparams(struct file *file, struct raparms *ra)
786 {
787         struct raparm_hbucket *rab = &raparm_hash[ra->p_hindex];
788
789         spin_lock(&rab->pb_lock);
790         ra->p_ra = file->f_ra;
791         ra->p_set = 1;
792         ra->p_count--;
793         spin_unlock(&rab->pb_lock);
794 }
795
796 /*
797  * Grab and keep cached pages associated with a file in the svc_rqst
798  * so that they can be passed to the network sendmsg/sendpage routines
799  * directly. They will be released after the sending has completed.
800  */
801 static int
802 nfsd_splice_actor(struct pipe_inode_info *pipe, struct pipe_buffer *buf,
803                   struct splice_desc *sd)
804 {
805         struct svc_rqst *rqstp = sd->u.data;
806         struct page **pp = rqstp->rq_next_page;
807         struct page *page = buf->page;
808         size_t size;
809
810         size = sd->len;
811
812         if (rqstp->rq_res.page_len == 0) {
813                 get_page(page);
814                 put_page(*rqstp->rq_next_page);
815                 *(rqstp->rq_next_page++) = page;
816                 rqstp->rq_res.page_base = buf->offset;
817                 rqstp->rq_res.page_len = size;
818         } else if (page != pp[-1]) {
819                 get_page(page);
820                 if (*rqstp->rq_next_page)
821                         put_page(*rqstp->rq_next_page);
822                 *(rqstp->rq_next_page++) = page;
823                 rqstp->rq_res.page_len += size;
824         } else
825                 rqstp->rq_res.page_len += size;
826
827         return size;
828 }
829
830 static int nfsd_direct_splice_actor(struct pipe_inode_info *pipe,
831                                     struct splice_desc *sd)
832 {
833         return __splice_from_pipe(pipe, sd, nfsd_splice_actor);
834 }
835
836 static __be32
837 nfsd_finish_read(struct file *file, unsigned long *count, int host_err)
838 {
839         if (host_err >= 0) {
840                 nfsdstats.io_read += host_err;
841                 *count = host_err;
842                 fsnotify_access(file);
843                 return 0;
844         } else 
845                 return nfserrno(host_err);
846 }
847
848 __be32 nfsd_splice_read(struct svc_rqst *rqstp,
849                      struct file *file, loff_t offset, unsigned long *count)
850 {
851         struct splice_desc sd = {
852                 .len            = 0,
853                 .total_len      = *count,
854                 .pos            = offset,
855                 .u.data         = rqstp,
856         };
857         int host_err;
858
859         rqstp->rq_next_page = rqstp->rq_respages + 1;
860         host_err = splice_direct_to_actor(file, &sd, nfsd_direct_splice_actor);
861         return nfsd_finish_read(file, count, host_err);
862 }
863
864 __be32 nfsd_readv(struct file *file, loff_t offset, struct kvec *vec, int vlen,
865                 unsigned long *count)
866 {
867         mm_segment_t oldfs;
868         int host_err;
869
870         oldfs = get_fs();
871         set_fs(KERNEL_DS);
872         host_err = vfs_readv(file, (struct iovec __user *)vec, vlen, &offset);
873         set_fs(oldfs);
874         return nfsd_finish_read(file, count, host_err);
875 }
876
877 static __be32
878 nfsd_vfs_read(struct svc_rqst *rqstp, struct file *file,
879               loff_t offset, struct kvec *vec, int vlen, unsigned long *count)
880 {
881         if (file->f_op->splice_read && test_bit(RQ_SPLICE_OK, &rqstp->rq_flags))
882                 return nfsd_splice_read(rqstp, file, offset, count);
883         else
884                 return nfsd_readv(file, offset, vec, vlen, count);
885 }
886
887 /*
888  * Gathered writes: If another process is currently writing to the file,
889  * there's a high chance this is another nfsd (triggered by a bulk write
890  * from a client's biod). Rather than syncing the file with each write
891  * request, we sleep for 10 msec.
892  *
893  * I don't know if this roughly approximates C. Juszak's idea of
894  * gathered writes, but it's a nice and simple solution (IMHO), and it
895  * seems to work:-)
896  *
897  * Note: we do this only in the NFSv2 case, since v3 and higher have a
898  * better tool (separate unstable writes and commits) for solving this
899  * problem.
900  */
901 static int wait_for_concurrent_writes(struct file *file)
902 {
903         struct inode *inode = file_inode(file);
904         static ino_t last_ino;
905         static dev_t last_dev;
906         int err = 0;
907
908         if (atomic_read(&inode->i_writecount) > 1
909             || (last_ino == inode->i_ino && last_dev == inode->i_sb->s_dev)) {
910                 dprintk("nfsd: write defer %d\n", task_pid_nr(current));
911                 msleep(10);
912                 dprintk("nfsd: write resume %d\n", task_pid_nr(current));
913         }
914
915         if (inode->i_state & I_DIRTY) {
916                 dprintk("nfsd: write sync %d\n", task_pid_nr(current));
917                 err = vfs_fsync(file, 0);
918         }
919         last_ino = inode->i_ino;
920         last_dev = inode->i_sb->s_dev;
921         return err;
922 }
923
924 __be32
925 nfsd_vfs_write(struct svc_rqst *rqstp, struct svc_fh *fhp, struct file *file,
926                                 loff_t offset, struct kvec *vec, int vlen,
927                                 unsigned long *cnt, int *stablep)
928 {
929         struct svc_export       *exp;
930         struct inode            *inode;
931         mm_segment_t            oldfs;
932         __be32                  err = 0;
933         int                     host_err;
934         int                     stable = *stablep;
935         int                     use_wgather;
936         loff_t                  pos = offset;
937         loff_t                  end = LLONG_MAX;
938         unsigned int            pflags = current->flags;
939
940         if (test_bit(RQ_LOCAL, &rqstp->rq_flags))
941                 /*
942                  * We want less throttling in balance_dirty_pages()
943                  * and shrink_inactive_list() so that nfs to
944                  * localhost doesn't cause nfsd to lock up due to all
945                  * the client's dirty pages or its congested queue.
946                  */
947                 current->flags |= PF_LESS_THROTTLE;
948
949         inode = file_inode(file);
950         exp   = fhp->fh_export;
951
952         use_wgather = (rqstp->rq_vers == 2) && EX_WGATHER(exp);
953
954         if (!EX_ISSYNC(exp))
955                 stable = 0;
956
957         /* Write the data. */
958         oldfs = get_fs(); set_fs(KERNEL_DS);
959         host_err = vfs_writev(file, (struct iovec __user *)vec, vlen, &pos);
960         set_fs(oldfs);
961         if (host_err < 0)
962                 goto out_nfserr;
963         *cnt = host_err;
964         nfsdstats.io_write += host_err;
965         fsnotify_modify(file);
966
967         if (stable) {
968                 if (use_wgather) {
969                         host_err = wait_for_concurrent_writes(file);
970                 } else {
971                         if (*cnt)
972                                 end = offset + *cnt - 1;
973                         host_err = vfs_fsync_range(file, offset, end, 0);
974                 }
975         }
976
977 out_nfserr:
978         dprintk("nfsd: write complete host_err=%d\n", host_err);
979         if (host_err >= 0)
980                 err = 0;
981         else
982                 err = nfserrno(host_err);
983         if (test_bit(RQ_LOCAL, &rqstp->rq_flags))
984                 tsk_restore_flags(current, pflags, PF_LESS_THROTTLE);
985         return err;
986 }
987
988 /*
989  * Read data from a file. count must contain the requested read count
990  * on entry. On return, *count contains the number of bytes actually read.
991  * N.B. After this call fhp needs an fh_put
992  */
993 __be32 nfsd_read(struct svc_rqst *rqstp, struct svc_fh *fhp,
994         loff_t offset, struct kvec *vec, int vlen, unsigned long *count)
995 {
996         struct file *file;
997         struct raparms  *ra;
998         __be32 err;
999
1000         err = nfsd_open(rqstp, fhp, S_IFREG, NFSD_MAY_READ, &file);
1001         if (err)
1002                 return err;
1003
1004         ra = nfsd_init_raparms(file);
1005         err = nfsd_vfs_read(rqstp, file, offset, vec, vlen, count);
1006         if (ra)
1007                 nfsd_put_raparams(file, ra);
1008         fput(file);
1009
1010         return err;
1011 }
1012
1013 /*
1014  * Write data to a file.
1015  * The stable flag requests synchronous writes.
1016  * N.B. After this call fhp needs an fh_put
1017  */
1018 __be32
1019 nfsd_write(struct svc_rqst *rqstp, struct svc_fh *fhp, struct file *file,
1020                 loff_t offset, struct kvec *vec, int vlen, unsigned long *cnt,
1021                 int *stablep)
1022 {
1023         __be32                  err = 0;
1024
1025         if (file) {
1026                 err = nfsd_permission(rqstp, fhp->fh_export, fhp->fh_dentry,
1027                                 NFSD_MAY_WRITE|NFSD_MAY_OWNER_OVERRIDE);
1028                 if (err)
1029                         goto out;
1030                 err = nfsd_vfs_write(rqstp, fhp, file, offset, vec, vlen, cnt,
1031                                 stablep);
1032         } else {
1033                 err = nfsd_open(rqstp, fhp, S_IFREG, NFSD_MAY_WRITE, &file);
1034                 if (err)
1035                         goto out;
1036
1037                 if (cnt)
1038                         err = nfsd_vfs_write(rqstp, fhp, file, offset, vec, vlen,
1039                                              cnt, stablep);
1040                 fput(file);
1041         }
1042 out:
1043         return err;
1044 }
1045
1046 #ifdef CONFIG_NFSD_V3
1047 /*
1048  * Commit all pending writes to stable storage.
1049  *
1050  * Note: we only guarantee that data that lies within the range specified
1051  * by the 'offset' and 'count' parameters will be synced.
1052  *
1053  * Unfortunately we cannot lock the file to make sure we return full WCC
1054  * data to the client, as locking happens lower down in the filesystem.
1055  */
1056 __be32
1057 nfsd_commit(struct svc_rqst *rqstp, struct svc_fh *fhp,
1058                loff_t offset, unsigned long count)
1059 {
1060         struct file     *file;
1061         loff_t          end = LLONG_MAX;
1062         __be32          err = nfserr_inval;
1063
1064         if (offset < 0)
1065                 goto out;
1066         if (count != 0) {
1067                 end = offset + (loff_t)count - 1;
1068                 if (end < offset)
1069                         goto out;
1070         }
1071
1072         err = nfsd_open(rqstp, fhp, S_IFREG,
1073                         NFSD_MAY_WRITE|NFSD_MAY_NOT_BREAK_LEASE, &file);
1074         if (err)
1075                 goto out;
1076         if (EX_ISSYNC(fhp->fh_export)) {
1077                 int err2 = vfs_fsync_range(file, offset, end, 0);
1078
1079                 if (err2 != -EINVAL)
1080                         err = nfserrno(err2);
1081                 else
1082                         err = nfserr_notsupp;
1083         }
1084
1085         fput(file);
1086 out:
1087         return err;
1088 }
1089 #endif /* CONFIG_NFSD_V3 */
1090
1091 static __be32
1092 nfsd_create_setattr(struct svc_rqst *rqstp, struct svc_fh *resfhp,
1093                         struct iattr *iap)
1094 {
1095         /*
1096          * Mode has already been set earlier in create:
1097          */
1098         iap->ia_valid &= ~ATTR_MODE;
1099         /*
1100          * Setting uid/gid works only for root.  Irix appears to
1101          * send along the gid on create when it tries to implement
1102          * setgid directories via NFS:
1103          */
1104         if (!uid_eq(current_fsuid(), GLOBAL_ROOT_UID))
1105                 iap->ia_valid &= ~(ATTR_UID|ATTR_GID);
1106         if (iap->ia_valid)
1107                 return nfsd_setattr(rqstp, resfhp, iap, 0, (time_t)0);
1108         /* Callers expect file metadata to be committed here */
1109         return nfserrno(commit_metadata(resfhp));
1110 }
1111
1112 /* HPUX client sometimes creates a file in mode 000, and sets size to 0.
1113  * setting size to 0 may fail for some specific file systems by the permission
1114  * checking which requires WRITE permission but the mode is 000.
1115  * we ignore the resizing(to 0) on the just new created file, since the size is
1116  * 0 after file created.
1117  *
1118  * call this only after vfs_create() is called.
1119  * */
1120 static void
1121 nfsd_check_ignore_resizing(struct iattr *iap)
1122 {
1123         if ((iap->ia_valid & ATTR_SIZE) && (iap->ia_size == 0))
1124                 iap->ia_valid &= ~ATTR_SIZE;
1125 }
1126
1127 /*
1128  * Create a file (regular, directory, device, fifo); UNIX sockets 
1129  * not yet implemented.
1130  * If the response fh has been verified, the parent directory should
1131  * already be locked. Note that the parent directory is left locked.
1132  *
1133  * N.B. Every call to nfsd_create needs an fh_put for _both_ fhp and resfhp
1134  */
1135 __be32
1136 nfsd_create(struct svc_rqst *rqstp, struct svc_fh *fhp,
1137                 char *fname, int flen, struct iattr *iap,
1138                 int type, dev_t rdev, struct svc_fh *resfhp)
1139 {
1140         struct dentry   *dentry, *dchild = NULL;
1141         struct inode    *dirp;
1142         __be32          err;
1143         __be32          err2;
1144         int             host_err;
1145
1146         err = nfserr_perm;
1147         if (!flen)
1148                 goto out;
1149         err = nfserr_exist;
1150         if (isdotent(fname, flen))
1151                 goto out;
1152
1153         err = fh_verify(rqstp, fhp, S_IFDIR, NFSD_MAY_CREATE);
1154         if (err)
1155                 goto out;
1156
1157         dentry = fhp->fh_dentry;
1158         dirp = d_inode(dentry);
1159
1160         err = nfserr_notdir;
1161         if (!dirp->i_op->lookup)
1162                 goto out;
1163         /*
1164          * Check whether the response file handle has been verified yet.
1165          * If it has, the parent directory should already be locked.
1166          */
1167         if (!resfhp->fh_dentry) {
1168                 host_err = fh_want_write(fhp);
1169                 if (host_err)
1170                         goto out_nfserr;
1171
1172                 /* called from nfsd_proc_mkdir, or possibly nfsd3_proc_create */
1173                 fh_lock_nested(fhp, I_MUTEX_PARENT);
1174                 dchild = lookup_one_len(fname, dentry, flen);
1175                 host_err = PTR_ERR(dchild);
1176                 if (IS_ERR(dchild))
1177                         goto out_nfserr;
1178                 err = fh_compose(resfhp, fhp->fh_export, dchild, fhp);
1179                 if (err)
1180                         goto out;
1181         } else {
1182                 /* called from nfsd_proc_create */
1183                 dchild = dget(resfhp->fh_dentry);
1184                 if (!fhp->fh_locked) {
1185                         /* not actually possible */
1186                         printk(KERN_ERR
1187                                 "nfsd_create: parent %pd2 not locked!\n",
1188                                 dentry);
1189                         err = nfserr_io;
1190                         goto out;
1191                 }
1192         }
1193         /*
1194          * Make sure the child dentry is still negative ...
1195          */
1196         err = nfserr_exist;
1197         if (d_really_is_positive(dchild)) {
1198                 dprintk("nfsd_create: dentry %pd/%pd not negative!\n",
1199                         dentry, dchild);
1200                 goto out; 
1201         }
1202
1203         if (!(iap->ia_valid & ATTR_MODE))
1204                 iap->ia_mode = 0;
1205         iap->ia_mode = (iap->ia_mode & S_IALLUGO) | type;
1206
1207         err = nfserr_inval;
1208         if (!S_ISREG(type) && !S_ISDIR(type) && !special_file(type)) {
1209                 printk(KERN_WARNING "nfsd: bad file type %o in nfsd_create\n",
1210                        type);
1211                 goto out;
1212         }
1213
1214         /*
1215          * Get the dir op function pointer.
1216          */
1217         err = 0;
1218         host_err = 0;
1219         switch (type) {
1220         case S_IFREG:
1221                 host_err = vfs_create(dirp, dchild, iap->ia_mode, true);
1222                 if (!host_err)
1223                         nfsd_check_ignore_resizing(iap);
1224                 break;
1225         case S_IFDIR:
1226                 host_err = vfs_mkdir(dirp, dchild, iap->ia_mode);
1227                 break;
1228         case S_IFCHR:
1229         case S_IFBLK:
1230         case S_IFIFO:
1231         case S_IFSOCK:
1232                 host_err = vfs_mknod(dirp, dchild, iap->ia_mode, rdev);
1233                 break;
1234         }
1235         if (host_err < 0)
1236                 goto out_nfserr;
1237
1238         err = nfsd_create_setattr(rqstp, resfhp, iap);
1239
1240         /*
1241          * nfsd_create_setattr already committed the child.  Transactional
1242          * filesystems had a chance to commit changes for both parent and
1243          * child * simultaneously making the following commit_metadata a
1244          * noop.
1245          */
1246         err2 = nfserrno(commit_metadata(fhp));
1247         if (err2)
1248                 err = err2;
1249         /*
1250          * Update the file handle to get the new inode info.
1251          */
1252         if (!err)
1253                 err = fh_update(resfhp);
1254 out:
1255         if (dchild && !IS_ERR(dchild))
1256                 dput(dchild);
1257         return err;
1258
1259 out_nfserr:
1260         err = nfserrno(host_err);
1261         goto out;
1262 }
1263
1264 #ifdef CONFIG_NFSD_V3
1265
1266 /*
1267  * NFSv3 and NFSv4 version of nfsd_create
1268  */
1269 __be32
1270 do_nfsd_create(struct svc_rqst *rqstp, struct svc_fh *fhp,
1271                 char *fname, int flen, struct iattr *iap,
1272                 struct svc_fh *resfhp, int createmode, u32 *verifier,
1273                 bool *truncp, bool *created)
1274 {
1275         struct dentry   *dentry, *dchild = NULL;
1276         struct inode    *dirp;
1277         __be32          err;
1278         int             host_err;
1279         __u32           v_mtime=0, v_atime=0;
1280
1281         err = nfserr_perm;
1282         if (!flen)
1283                 goto out;
1284         err = nfserr_exist;
1285         if (isdotent(fname, flen))
1286                 goto out;
1287         if (!(iap->ia_valid & ATTR_MODE))
1288                 iap->ia_mode = 0;
1289         err = fh_verify(rqstp, fhp, S_IFDIR, NFSD_MAY_EXEC);
1290         if (err)
1291                 goto out;
1292
1293         dentry = fhp->fh_dentry;
1294         dirp = d_inode(dentry);
1295
1296         /* Get all the sanity checks out of the way before
1297          * we lock the parent. */
1298         err = nfserr_notdir;
1299         if (!dirp->i_op->lookup)
1300                 goto out;
1301
1302         host_err = fh_want_write(fhp);
1303         if (host_err)
1304                 goto out_nfserr;
1305
1306         fh_lock_nested(fhp, I_MUTEX_PARENT);
1307
1308         /*
1309          * Compose the response file handle.
1310          */
1311         dchild = lookup_one_len(fname, dentry, flen);
1312         host_err = PTR_ERR(dchild);
1313         if (IS_ERR(dchild))
1314                 goto out_nfserr;
1315
1316         /* If file doesn't exist, check for permissions to create one */
1317         if (d_really_is_negative(dchild)) {
1318                 err = fh_verify(rqstp, fhp, S_IFDIR, NFSD_MAY_CREATE);
1319                 if (err)
1320                         goto out;
1321         }
1322
1323         err = fh_compose(resfhp, fhp->fh_export, dchild, fhp);
1324         if (err)
1325                 goto out;
1326
1327         if (nfsd_create_is_exclusive(createmode)) {
1328                 /* solaris7 gets confused (bugid 4218508) if these have
1329                  * the high bit set, so just clear the high bits. If this is
1330                  * ever changed to use different attrs for storing the
1331                  * verifier, then do_open_lookup() will also need to be fixed
1332                  * accordingly.
1333                  */
1334                 v_mtime = verifier[0]&0x7fffffff;
1335                 v_atime = verifier[1]&0x7fffffff;
1336         }
1337         
1338         if (d_really_is_positive(dchild)) {
1339                 err = 0;
1340
1341                 switch (createmode) {
1342                 case NFS3_CREATE_UNCHECKED:
1343                         if (! d_is_reg(dchild))
1344                                 goto out;
1345                         else if (truncp) {
1346                                 /* in nfsv4, we need to treat this case a little
1347                                  * differently.  we don't want to truncate the
1348                                  * file now; this would be wrong if the OPEN
1349                                  * fails for some other reason.  furthermore,
1350                                  * if the size is nonzero, we should ignore it
1351                                  * according to spec!
1352                                  */
1353                                 *truncp = (iap->ia_valid & ATTR_SIZE) && !iap->ia_size;
1354                         }
1355                         else {
1356                                 iap->ia_valid &= ATTR_SIZE;
1357                                 goto set_attr;
1358                         }
1359                         break;
1360                 case NFS3_CREATE_EXCLUSIVE:
1361                         if (   d_inode(dchild)->i_mtime.tv_sec == v_mtime
1362                             && d_inode(dchild)->i_atime.tv_sec == v_atime
1363                             && d_inode(dchild)->i_size  == 0 ) {
1364                                 if (created)
1365                                         *created = 1;
1366                                 break;
1367                         }
1368                 case NFS4_CREATE_EXCLUSIVE4_1:
1369                         if (   d_inode(dchild)->i_mtime.tv_sec == v_mtime
1370                             && d_inode(dchild)->i_atime.tv_sec == v_atime
1371                             && d_inode(dchild)->i_size  == 0 ) {
1372                                 if (created)
1373                                         *created = 1;
1374                                 goto set_attr;
1375                         }
1376                          /* fallthru */
1377                 case NFS3_CREATE_GUARDED:
1378                         err = nfserr_exist;
1379                 }
1380                 fh_drop_write(fhp);
1381                 goto out;
1382         }
1383
1384         host_err = vfs_create(dirp, dchild, iap->ia_mode, true);
1385         if (host_err < 0) {
1386                 fh_drop_write(fhp);
1387                 goto out_nfserr;
1388         }
1389         if (created)
1390                 *created = 1;
1391
1392         nfsd_check_ignore_resizing(iap);
1393
1394         if (nfsd_create_is_exclusive(createmode)) {
1395                 /* Cram the verifier into atime/mtime */
1396                 iap->ia_valid = ATTR_MTIME|ATTR_ATIME
1397                         | ATTR_MTIME_SET|ATTR_ATIME_SET;
1398                 /* XXX someone who knows this better please fix it for nsec */ 
1399                 iap->ia_mtime.tv_sec = v_mtime;
1400                 iap->ia_atime.tv_sec = v_atime;
1401                 iap->ia_mtime.tv_nsec = 0;
1402                 iap->ia_atime.tv_nsec = 0;
1403         }
1404
1405  set_attr:
1406         err = nfsd_create_setattr(rqstp, resfhp, iap);
1407
1408         /*
1409          * nfsd_create_setattr already committed the child
1410          * (and possibly also the parent).
1411          */
1412         if (!err)
1413                 err = nfserrno(commit_metadata(fhp));
1414
1415         /*
1416          * Update the filehandle to get the new inode info.
1417          */
1418         if (!err)
1419                 err = fh_update(resfhp);
1420
1421  out:
1422         fh_unlock(fhp);
1423         if (dchild && !IS_ERR(dchild))
1424                 dput(dchild);
1425         fh_drop_write(fhp);
1426         return err;
1427  
1428  out_nfserr:
1429         err = nfserrno(host_err);
1430         goto out;
1431 }
1432 #endif /* CONFIG_NFSD_V3 */
1433
1434 /*
1435  * Read a symlink. On entry, *lenp must contain the maximum path length that
1436  * fits into the buffer. On return, it contains the true length.
1437  * N.B. After this call fhp needs an fh_put
1438  */
1439 __be32
1440 nfsd_readlink(struct svc_rqst *rqstp, struct svc_fh *fhp, char *buf, int *lenp)
1441 {
1442         struct inode    *inode;
1443         mm_segment_t    oldfs;
1444         __be32          err;
1445         int             host_err;
1446         struct path path;
1447
1448         err = fh_verify(rqstp, fhp, S_IFLNK, NFSD_MAY_NOP);
1449         if (err)
1450                 goto out;
1451
1452         path.mnt = fhp->fh_export->ex_path.mnt;
1453         path.dentry = fhp->fh_dentry;
1454         inode = d_inode(path.dentry);
1455
1456         err = nfserr_inval;
1457         if (!inode->i_op->readlink)
1458                 goto out;
1459
1460         touch_atime(&path);
1461         /* N.B. Why does this call need a get_fs()??
1462          * Remove the set_fs and watch the fireworks:-) --okir
1463          */
1464
1465         oldfs = get_fs(); set_fs(KERNEL_DS);
1466         host_err = inode->i_op->readlink(path.dentry, (char __user *)buf, *lenp);
1467         set_fs(oldfs);
1468
1469         if (host_err < 0)
1470                 goto out_nfserr;
1471         *lenp = host_err;
1472         err = 0;
1473 out:
1474         return err;
1475
1476 out_nfserr:
1477         err = nfserrno(host_err);
1478         goto out;
1479 }
1480
1481 /*
1482  * Create a symlink and look up its inode
1483  * N.B. After this call _both_ fhp and resfhp need an fh_put
1484  */
1485 __be32
1486 nfsd_symlink(struct svc_rqst *rqstp, struct svc_fh *fhp,
1487                                 char *fname, int flen,
1488                                 char *path,
1489                                 struct svc_fh *resfhp)
1490 {
1491         struct dentry   *dentry, *dnew;
1492         __be32          err, cerr;
1493         int             host_err;
1494
1495         err = nfserr_noent;
1496         if (!flen || path[0] == '\0')
1497                 goto out;
1498         err = nfserr_exist;
1499         if (isdotent(fname, flen))
1500                 goto out;
1501
1502         err = fh_verify(rqstp, fhp, S_IFDIR, NFSD_MAY_CREATE);
1503         if (err)
1504                 goto out;
1505
1506         host_err = fh_want_write(fhp);
1507         if (host_err)
1508                 goto out_nfserr;
1509
1510         fh_lock(fhp);
1511         dentry = fhp->fh_dentry;
1512         dnew = lookup_one_len(fname, dentry, flen);
1513         host_err = PTR_ERR(dnew);
1514         if (IS_ERR(dnew))
1515                 goto out_nfserr;
1516
1517         host_err = vfs_symlink(d_inode(dentry), dnew, path);
1518         err = nfserrno(host_err);
1519         if (!err)
1520                 err = nfserrno(commit_metadata(fhp));
1521         fh_unlock(fhp);
1522
1523         fh_drop_write(fhp);
1524
1525         cerr = fh_compose(resfhp, fhp->fh_export, dnew, fhp);
1526         dput(dnew);
1527         if (err==0) err = cerr;
1528 out:
1529         return err;
1530
1531 out_nfserr:
1532         err = nfserrno(host_err);
1533         goto out;
1534 }
1535
1536 /*
1537  * Create a hardlink
1538  * N.B. After this call _both_ ffhp and tfhp need an fh_put
1539  */
1540 __be32
1541 nfsd_link(struct svc_rqst *rqstp, struct svc_fh *ffhp,
1542                                 char *name, int len, struct svc_fh *tfhp)
1543 {
1544         struct dentry   *ddir, *dnew, *dold;
1545         struct inode    *dirp;
1546         __be32          err;
1547         int             host_err;
1548
1549         err = fh_verify(rqstp, ffhp, S_IFDIR, NFSD_MAY_CREATE);
1550         if (err)
1551                 goto out;
1552         err = fh_verify(rqstp, tfhp, 0, NFSD_MAY_NOP);
1553         if (err)
1554                 goto out;
1555         err = nfserr_isdir;
1556         if (d_is_dir(tfhp->fh_dentry))
1557                 goto out;
1558         err = nfserr_perm;
1559         if (!len)
1560                 goto out;
1561         err = nfserr_exist;
1562         if (isdotent(name, len))
1563                 goto out;
1564
1565         host_err = fh_want_write(tfhp);
1566         if (host_err) {
1567                 err = nfserrno(host_err);
1568                 goto out;
1569         }
1570
1571         fh_lock_nested(ffhp, I_MUTEX_PARENT);
1572         ddir = ffhp->fh_dentry;
1573         dirp = d_inode(ddir);
1574
1575         dnew = lookup_one_len(name, ddir, len);
1576         host_err = PTR_ERR(dnew);
1577         if (IS_ERR(dnew))
1578                 goto out_nfserr;
1579
1580         dold = tfhp->fh_dentry;
1581
1582         err = nfserr_noent;
1583         if (d_really_is_negative(dold))
1584                 goto out_dput;
1585         host_err = vfs_link(dold, dirp, dnew, NULL);
1586         if (!host_err) {
1587                 err = nfserrno(commit_metadata(ffhp));
1588                 if (!err)
1589                         err = nfserrno(commit_metadata(tfhp));
1590         } else {
1591                 if (host_err == -EXDEV && rqstp->rq_vers == 2)
1592                         err = nfserr_acces;
1593                 else
1594                         err = nfserrno(host_err);
1595         }
1596 out_dput:
1597         dput(dnew);
1598 out_unlock:
1599         fh_unlock(ffhp);
1600         fh_drop_write(tfhp);
1601 out:
1602         return err;
1603
1604 out_nfserr:
1605         err = nfserrno(host_err);
1606         goto out_unlock;
1607 }
1608
1609 /*
1610  * Rename a file
1611  * N.B. After this call _both_ ffhp and tfhp need an fh_put
1612  */
1613 __be32
1614 nfsd_rename(struct svc_rqst *rqstp, struct svc_fh *ffhp, char *fname, int flen,
1615                             struct svc_fh *tfhp, char *tname, int tlen)
1616 {
1617         struct dentry   *fdentry, *tdentry, *odentry, *ndentry, *trap;
1618         struct inode    *fdir, *tdir;
1619         __be32          err;
1620         int             host_err;
1621
1622         err = fh_verify(rqstp, ffhp, S_IFDIR, NFSD_MAY_REMOVE);
1623         if (err)
1624                 goto out;
1625         err = fh_verify(rqstp, tfhp, S_IFDIR, NFSD_MAY_CREATE);
1626         if (err)
1627                 goto out;
1628
1629         fdentry = ffhp->fh_dentry;
1630         fdir = d_inode(fdentry);
1631
1632         tdentry = tfhp->fh_dentry;
1633         tdir = d_inode(tdentry);
1634
1635         err = nfserr_perm;
1636         if (!flen || isdotent(fname, flen) || !tlen || isdotent(tname, tlen))
1637                 goto out;
1638
1639         host_err = fh_want_write(ffhp);
1640         if (host_err) {
1641                 err = nfserrno(host_err);
1642                 goto out;
1643         }
1644
1645         /* cannot use fh_lock as we need deadlock protective ordering
1646          * so do it by hand */
1647         trap = lock_rename(tdentry, fdentry);
1648         ffhp->fh_locked = tfhp->fh_locked = true;
1649         fill_pre_wcc(ffhp);
1650         fill_pre_wcc(tfhp);
1651
1652         odentry = lookup_one_len(fname, fdentry, flen);
1653         host_err = PTR_ERR(odentry);
1654         if (IS_ERR(odentry))
1655                 goto out_nfserr;
1656
1657         host_err = -ENOENT;
1658         if (d_really_is_negative(odentry))
1659                 goto out_dput_old;
1660         host_err = -EINVAL;
1661         if (odentry == trap)
1662                 goto out_dput_old;
1663
1664         ndentry = lookup_one_len(tname, tdentry, tlen);
1665         host_err = PTR_ERR(ndentry);
1666         if (IS_ERR(ndentry))
1667                 goto out_dput_old;
1668         host_err = -ENOTEMPTY;
1669         if (ndentry == trap)
1670                 goto out_dput_new;
1671
1672         host_err = -EXDEV;
1673         if (ffhp->fh_export->ex_path.mnt != tfhp->fh_export->ex_path.mnt)
1674                 goto out_dput_new;
1675         if (ffhp->fh_export->ex_path.dentry != tfhp->fh_export->ex_path.dentry)
1676                 goto out_dput_new;
1677
1678         host_err = vfs_rename(fdir, odentry, tdir, ndentry, NULL, 0);
1679         if (!host_err) {
1680                 host_err = commit_metadata(tfhp);
1681                 if (!host_err)
1682                         host_err = commit_metadata(ffhp);
1683         }
1684  out_dput_new:
1685         dput(ndentry);
1686  out_dput_old:
1687         dput(odentry);
1688  out_nfserr:
1689         err = nfserrno(host_err);
1690         /*
1691          * We cannot rely on fh_unlock on the two filehandles,
1692          * as that would do the wrong thing if the two directories
1693          * were the same, so again we do it by hand.
1694          */
1695         fill_post_wcc(ffhp);
1696         fill_post_wcc(tfhp);
1697         unlock_rename(tdentry, fdentry);
1698         ffhp->fh_locked = tfhp->fh_locked = false;
1699         fh_drop_write(ffhp);
1700
1701 out:
1702         return err;
1703 }
1704
1705 /*
1706  * Unlink a file or directory
1707  * N.B. After this call fhp needs an fh_put
1708  */
1709 __be32
1710 nfsd_unlink(struct svc_rqst *rqstp, struct svc_fh *fhp, int type,
1711                                 char *fname, int flen)
1712 {
1713         struct dentry   *dentry, *rdentry;
1714         struct inode    *dirp;
1715         __be32          err;
1716         int             host_err;
1717
1718         err = nfserr_acces;
1719         if (!flen || isdotent(fname, flen))
1720                 goto out;
1721         err = fh_verify(rqstp, fhp, S_IFDIR, NFSD_MAY_REMOVE);
1722         if (err)
1723                 goto out;
1724
1725         host_err = fh_want_write(fhp);
1726         if (host_err)
1727                 goto out_nfserr;
1728
1729         fh_lock_nested(fhp, I_MUTEX_PARENT);
1730         dentry = fhp->fh_dentry;
1731         dirp = d_inode(dentry);
1732
1733         rdentry = lookup_one_len(fname, dentry, flen);
1734         host_err = PTR_ERR(rdentry);
1735         if (IS_ERR(rdentry))
1736                 goto out_nfserr;
1737
1738         if (d_really_is_negative(rdentry)) {
1739                 dput(rdentry);
1740                 err = nfserr_noent;
1741                 goto out;
1742         }
1743
1744         if (!type)
1745                 type = d_inode(rdentry)->i_mode & S_IFMT;
1746
1747         if (type != S_IFDIR)
1748                 host_err = vfs_unlink(dirp, rdentry, NULL);
1749         else
1750                 host_err = vfs_rmdir(dirp, rdentry);
1751         if (!host_err)
1752                 host_err = commit_metadata(fhp);
1753         dput(rdentry);
1754
1755 out_nfserr:
1756         err = nfserrno(host_err);
1757 out:
1758         return err;
1759 }
1760
1761 /*
1762  * We do this buffering because we must not call back into the file
1763  * system's ->lookup() method from the filldir callback. That may well
1764  * deadlock a number of file systems.
1765  *
1766  * This is based heavily on the implementation of same in XFS.
1767  */
1768 struct buffered_dirent {
1769         u64             ino;
1770         loff_t          offset;
1771         int             namlen;
1772         unsigned int    d_type;
1773         char            name[];
1774 };
1775
1776 struct readdir_data {
1777         struct dir_context ctx;
1778         char            *dirent;
1779         size_t          used;
1780         int             full;
1781 };
1782
1783 static int nfsd_buffered_filldir(struct dir_context *ctx, const char *name,
1784                                  int namlen, loff_t offset, u64 ino,
1785                                  unsigned int d_type)
1786 {
1787         struct readdir_data *buf =
1788                 container_of(ctx, struct readdir_data, ctx);
1789         struct buffered_dirent *de = (void *)(buf->dirent + buf->used);
1790         unsigned int reclen;
1791
1792         reclen = ALIGN(sizeof(struct buffered_dirent) + namlen, sizeof(u64));
1793         if (buf->used + reclen > PAGE_SIZE) {
1794                 buf->full = 1;
1795                 return -EINVAL;
1796         }
1797
1798         de->namlen = namlen;
1799         de->offset = offset;
1800         de->ino = ino;
1801         de->d_type = d_type;
1802         memcpy(de->name, name, namlen);
1803         buf->used += reclen;
1804
1805         return 0;
1806 }
1807
1808 static __be32 nfsd_buffered_readdir(struct file *file, nfsd_filldir_t func,
1809                                     struct readdir_cd *cdp, loff_t *offsetp)
1810 {
1811         struct buffered_dirent *de;
1812         int host_err;
1813         int size;
1814         loff_t offset;
1815         struct readdir_data buf = {
1816                 .ctx.actor = nfsd_buffered_filldir,
1817                 .dirent = (void *)__get_free_page(GFP_KERNEL)
1818         };
1819
1820         if (!buf.dirent)
1821                 return nfserrno(-ENOMEM);
1822
1823         offset = *offsetp;
1824
1825         while (1) {
1826                 unsigned int reclen;
1827
1828                 cdp->err = nfserr_eof; /* will be cleared on successful read */
1829                 buf.used = 0;
1830                 buf.full = 0;
1831
1832                 host_err = iterate_dir(file, &buf.ctx);
1833                 if (buf.full)
1834                         host_err = 0;
1835
1836                 if (host_err < 0)
1837                         break;
1838
1839                 size = buf.used;
1840
1841                 if (!size)
1842                         break;
1843
1844                 de = (struct buffered_dirent *)buf.dirent;
1845                 while (size > 0) {
1846                         offset = de->offset;
1847
1848                         if (func(cdp, de->name, de->namlen, de->offset,
1849                                  de->ino, de->d_type))
1850                                 break;
1851
1852                         if (cdp->err != nfs_ok)
1853                                 break;
1854
1855                         reclen = ALIGN(sizeof(*de) + de->namlen,
1856                                        sizeof(u64));
1857                         size -= reclen;
1858                         de = (struct buffered_dirent *)((char *)de + reclen);
1859                 }
1860                 if (size > 0) /* We bailed out early */
1861                         break;
1862
1863                 offset = vfs_llseek(file, 0, SEEK_CUR);
1864         }
1865
1866         free_page((unsigned long)(buf.dirent));
1867
1868         if (host_err)
1869                 return nfserrno(host_err);
1870
1871         *offsetp = offset;
1872         return cdp->err;
1873 }
1874
1875 /*
1876  * Read entries from a directory.
1877  * The  NFSv3/4 verifier we ignore for now.
1878  */
1879 __be32
1880 nfsd_readdir(struct svc_rqst *rqstp, struct svc_fh *fhp, loff_t *offsetp, 
1881              struct readdir_cd *cdp, nfsd_filldir_t func)
1882 {
1883         __be32          err;
1884         struct file     *file;
1885         loff_t          offset = *offsetp;
1886         int             may_flags = NFSD_MAY_READ;
1887
1888         /* NFSv2 only supports 32 bit cookies */
1889         if (rqstp->rq_vers > 2)
1890                 may_flags |= NFSD_MAY_64BIT_COOKIE;
1891
1892         err = nfsd_open(rqstp, fhp, S_IFDIR, may_flags, &file);
1893         if (err)
1894                 goto out;
1895
1896         offset = vfs_llseek(file, offset, SEEK_SET);
1897         if (offset < 0) {
1898                 err = nfserrno((int)offset);
1899                 goto out_close;
1900         }
1901
1902         err = nfsd_buffered_readdir(file, func, cdp, offsetp);
1903
1904         if (err == nfserr_eof || err == nfserr_toosmall)
1905                 err = nfs_ok; /* can still be found in ->err */
1906 out_close:
1907         fput(file);
1908 out:
1909         return err;
1910 }
1911
1912 /*
1913  * Get file system stats
1914  * N.B. After this call fhp needs an fh_put
1915  */
1916 __be32
1917 nfsd_statfs(struct svc_rqst *rqstp, struct svc_fh *fhp, struct kstatfs *stat, int access)
1918 {
1919         __be32 err;
1920
1921         err = fh_verify(rqstp, fhp, 0, NFSD_MAY_NOP | access);
1922         if (!err) {
1923                 struct path path = {
1924                         .mnt    = fhp->fh_export->ex_path.mnt,
1925                         .dentry = fhp->fh_dentry,
1926                 };
1927                 if (vfs_statfs(&path, stat))
1928                         err = nfserr_io;
1929         }
1930         return err;
1931 }
1932
1933 static int exp_rdonly(struct svc_rqst *rqstp, struct svc_export *exp)
1934 {
1935         return nfsexp_flags(rqstp, exp) & NFSEXP_READONLY;
1936 }
1937
1938 /*
1939  * Check for a user's access permissions to this inode.
1940  */
1941 __be32
1942 nfsd_permission(struct svc_rqst *rqstp, struct svc_export *exp,
1943                                         struct dentry *dentry, int acc)
1944 {
1945         struct inode    *inode = d_inode(dentry);
1946         int             err;
1947
1948         if ((acc & NFSD_MAY_MASK) == NFSD_MAY_NOP)
1949                 return 0;
1950 #if 0
1951         dprintk("nfsd: permission 0x%x%s%s%s%s%s%s%s mode 0%o%s%s%s\n",
1952                 acc,
1953                 (acc & NFSD_MAY_READ)?  " read"  : "",
1954                 (acc & NFSD_MAY_WRITE)? " write" : "",
1955                 (acc & NFSD_MAY_EXEC)?  " exec"  : "",
1956                 (acc & NFSD_MAY_SATTR)? " sattr" : "",
1957                 (acc & NFSD_MAY_TRUNC)? " trunc" : "",
1958                 (acc & NFSD_MAY_LOCK)?  " lock"  : "",
1959                 (acc & NFSD_MAY_OWNER_OVERRIDE)? " owneroverride" : "",
1960                 inode->i_mode,
1961                 IS_IMMUTABLE(inode)?    " immut" : "",
1962                 IS_APPEND(inode)?       " append" : "",
1963                 __mnt_is_readonly(exp->ex_path.mnt)?    " ro" : "");
1964         dprintk("      owner %d/%d user %d/%d\n",
1965                 inode->i_uid, inode->i_gid, current_fsuid(), current_fsgid());
1966 #endif
1967
1968         /* Normally we reject any write/sattr etc access on a read-only file
1969          * system.  But if it is IRIX doing check on write-access for a 
1970          * device special file, we ignore rofs.
1971          */
1972         if (!(acc & NFSD_MAY_LOCAL_ACCESS))
1973                 if (acc & (NFSD_MAY_WRITE | NFSD_MAY_SATTR | NFSD_MAY_TRUNC)) {
1974                         if (exp_rdonly(rqstp, exp) ||
1975                             __mnt_is_readonly(exp->ex_path.mnt))
1976                                 return nfserr_rofs;
1977                         if (/* (acc & NFSD_MAY_WRITE) && */ IS_IMMUTABLE(inode))
1978                                 return nfserr_perm;
1979                 }
1980         if ((acc & NFSD_MAY_TRUNC) && IS_APPEND(inode))
1981                 return nfserr_perm;
1982
1983         if (acc & NFSD_MAY_LOCK) {
1984                 /* If we cannot rely on authentication in NLM requests,
1985                  * just allow locks, otherwise require read permission, or
1986                  * ownership
1987                  */
1988                 if (exp->ex_flags & NFSEXP_NOAUTHNLM)
1989                         return 0;
1990                 else
1991                         acc = NFSD_MAY_READ | NFSD_MAY_OWNER_OVERRIDE;
1992         }
1993         /*
1994          * The file owner always gets access permission for accesses that
1995          * would normally be checked at open time. This is to make
1996          * file access work even when the client has done a fchmod(fd, 0).
1997          *
1998          * However, `cp foo bar' should fail nevertheless when bar is
1999          * readonly. A sensible way to do this might be to reject all
2000          * attempts to truncate a read-only file, because a creat() call
2001          * always implies file truncation.
2002          * ... but this isn't really fair.  A process may reasonably call
2003          * ftruncate on an open file descriptor on a file with perm 000.
2004          * We must trust the client to do permission checking - using "ACCESS"
2005          * with NFSv3.
2006          */
2007         if ((acc & NFSD_MAY_OWNER_OVERRIDE) &&
2008             uid_eq(inode->i_uid, current_fsuid()))
2009                 return 0;
2010
2011         /* This assumes  NFSD_MAY_{READ,WRITE,EXEC} == MAY_{READ,WRITE,EXEC} */
2012         err = inode_permission(inode, acc & (MAY_READ|MAY_WRITE|MAY_EXEC));
2013
2014         /* Allow read access to binaries even when mode 111 */
2015         if (err == -EACCES && S_ISREG(inode->i_mode) &&
2016              (acc == (NFSD_MAY_READ | NFSD_MAY_OWNER_OVERRIDE) ||
2017               acc == (NFSD_MAY_READ | NFSD_MAY_READ_IF_EXEC)))
2018                 err = inode_permission(inode, MAY_EXEC);
2019
2020         return err? nfserrno(err) : 0;
2021 }
2022
2023 void
2024 nfsd_racache_shutdown(void)
2025 {
2026         struct raparms *raparm, *last_raparm;
2027         unsigned int i;
2028
2029         dprintk("nfsd: freeing readahead buffers.\n");
2030
2031         for (i = 0; i < RAPARM_HASH_SIZE; i++) {
2032                 raparm = raparm_hash[i].pb_head;
2033                 while(raparm) {
2034                         last_raparm = raparm;
2035                         raparm = raparm->p_next;
2036                         kfree(last_raparm);
2037                 }
2038                 raparm_hash[i].pb_head = NULL;
2039         }
2040 }
2041 /*
2042  * Initialize readahead param cache
2043  */
2044 int
2045 nfsd_racache_init(int cache_size)
2046 {
2047         int     i;
2048         int     j = 0;
2049         int     nperbucket;
2050         struct raparms **raparm = NULL;
2051
2052
2053         if (raparm_hash[0].pb_head)
2054                 return 0;
2055         nperbucket = DIV_ROUND_UP(cache_size, RAPARM_HASH_SIZE);
2056         nperbucket = max(2, nperbucket);
2057         cache_size = nperbucket * RAPARM_HASH_SIZE;
2058
2059         dprintk("nfsd: allocating %d readahead buffers.\n", cache_size);
2060
2061         for (i = 0; i < RAPARM_HASH_SIZE; i++) {
2062                 spin_lock_init(&raparm_hash[i].pb_lock);
2063
2064                 raparm = &raparm_hash[i].pb_head;
2065                 for (j = 0; j < nperbucket; j++) {
2066                         *raparm = kzalloc(sizeof(struct raparms), GFP_KERNEL);
2067                         if (!*raparm)
2068                                 goto out_nomem;
2069                         raparm = &(*raparm)->p_next;
2070                 }
2071                 *raparm = NULL;
2072         }
2073
2074         nfsdstats.ra_size = cache_size;
2075         return 0;
2076
2077 out_nomem:
2078         dprintk("nfsd: kmalloc failed, freeing readahead buffers\n");
2079         nfsd_racache_shutdown();
2080         return -ENOMEM;
2081 }