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