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