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