~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~

TOMOYO Linux Cross Reference
Linux/fs/nfsd/vfs.c

Version: ~ [ linux-5.3-rc5 ] ~ [ linux-5.2.9 ] ~ [ linux-5.1.21 ] ~ [ linux-5.0.21 ] ~ [ linux-4.20.17 ] ~ [ linux-4.19.67 ] ~ [ linux-4.18.20 ] ~ [ linux-4.17.19 ] ~ [ linux-4.16.18 ] ~ [ linux-4.15.18 ] ~ [ linux-4.14.139 ] ~ [ linux-4.13.16 ] ~ [ linux-4.12.14 ] ~ [ linux-4.11.12 ] ~ [ linux-4.10.17 ] ~ [ linux-4.9.189 ] ~ [ linux-4.8.17 ] ~ [ linux-4.7.10 ] ~ [ linux-4.6.7 ] ~ [ linux-4.5.7 ] ~ [ linux-4.4.189 ] ~ [ linux-4.3.6 ] ~ [ linux-4.2.8 ] ~ [ linux-4.1.52 ] ~ [ linux-4.0.9 ] ~ [ linux-3.19.8 ] ~ [ linux-3.18.140 ] ~ [ linux-3.17.8 ] ~ [ linux-3.16.72 ] ~ [ linux-3.15.10 ] ~ [ linux-3.14.79 ] ~ [ linux-3.13.11 ] ~ [ linux-3.12.74 ] ~ [ linux-3.11.10 ] ~ [ linux-3.10.108 ] ~ [ linux-3.9.11 ] ~ [ linux-3.8.13 ] ~ [ linux-3.7.10 ] ~ [ linux-3.6.11 ] ~ [ linux-3.5.7 ] ~ [ linux-3.4.113 ] ~ [ linux-3.3.8 ] ~ [ linux-3.2.102 ] ~ [ linux-3.1.10 ] ~ [ linux-3.0.101 ] ~ [ linux-2.6.32.71 ] ~ [ linux-2.6.0 ] ~ [ linux-2.4.37.11 ] ~ [ unix-v6-master ] ~ [ ccs-tools-1.8.5 ] ~ [ policy-sample ] ~
Architecture: ~ [ i386 ] ~ [ alpha ] ~ [ m68k ] ~ [ mips ] ~ [ ppc ] ~ [ sparc ] ~ [ sparc64 ] ~

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

~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~

kernel.org | git.kernel.org | LWN.net | Project Home | Wiki (Japanese) | Wiki (English) | SVN repository | Mail admin

Linux® is a registered trademark of Linus Torvalds in the United States and other countries.
TOMOYO® is a registered trademark of NTT DATA CORPORATION.

osdn.jp