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