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