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

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

Version: ~ [ linux-5.0-rc6 ] ~ [ linux-4.20.7 ] ~ [ linux-4.19.20 ] ~ [ linux-4.18.20 ] ~ [ linux-4.17.19 ] ~ [ linux-4.16.18 ] ~ [ linux-4.15.18 ] ~ [ linux-4.14.98 ] ~ [ linux-4.13.16 ] ~ [ linux-4.12.14 ] ~ [ linux-4.11.12 ] ~ [ linux-4.10.17 ] ~ [ linux-4.9.155 ] ~ [ linux-4.8.17 ] ~ [ linux-4.7.10 ] ~ [ linux-4.6.7 ] ~ [ linux-4.5.7 ] ~ [ linux-4.4.174 ] ~ [ linux-4.3.6 ] ~ [ linux-4.2.8 ] ~ [ linux-4.1.52 ] ~ [ linux-4.0.9 ] ~ [ linux-3.19.8 ] ~ [ linux-3.18.134 ] ~ [ linux-3.17.8 ] ~ [ linux-3.16.62 ] ~ [ 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.39.4 ] ~ [ linux-2.6.38.8 ] ~ [ linux-2.6.37.6 ] ~ [ linux-2.6.36.4 ] ~ [ linux-2.6.35.14 ] ~ [ linux-2.6.34.15 ] ~ [ linux-2.6.33.20 ] ~ [ 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 #define MSNFS   /* HACK HACK */
  2 /*
  3  * linux/fs/nfsd/vfs.c
  4  *
  5  * File operations used by nfsd. Some of these have been ripped from
  6  * other parts of the kernel because they weren't exported, others
  7  * are partial duplicates with added or changed functionality.
  8  *
  9  * Note that several functions dget() the dentry upon which they want
 10  * to act, most notably those that create directory entries. Response
 11  * dentry's are dput()'d if necessary in the release callback.
 12  * So if you notice code paths that apparently fail to dput() the
 13  * dentry, don't worry--they have been taken care of.
 14  *
 15  * Copyright (C) 1995-1999 Olaf Kirch <okir@monad.swb.de>
 16  * Zerocpy NFS support (C) 2002 Hirokazu Takahashi <taka@valinux.co.jp>
 17  */
 18 
 19 #include <linux/config.h>
 20 #include <linux/string.h>
 21 #include <linux/time.h>
 22 #include <linux/errno.h>
 23 #include <linux/fs.h>
 24 #include <linux/mount.h>
 25 #include <linux/major.h>
 26 #include <linux/ext2_fs.h>
 27 #include <linux/proc_fs.h>
 28 #include <linux/stat.h>
 29 #include <linux/fcntl.h>
 30 #include <linux/net.h>
 31 #include <linux/unistd.h>
 32 #include <linux/slab.h>
 33 #include <linux/pagemap.h>
 34 #include <linux/in.h>
 35 #include <linux/module.h>
 36 #include <linux/namei.h>
 37 #include <linux/vfs.h>
 38 #include <linux/sunrpc/svc.h>
 39 #include <linux/nfsd/nfsd.h>
 40 #ifdef CONFIG_NFSD_V3
 41 #include <linux/nfs3.h>
 42 #include <linux/nfsd/xdr3.h>
 43 #endif /* CONFIG_NFSD_V3 */
 44 #include <linux/nfsd/nfsfh.h>
 45 #include <linux/quotaops.h>
 46 
 47 #include <asm/uaccess.h>
 48 
 49 #define NFSDDBG_FACILITY                NFSDDBG_FILEOP
 50 #define NFSD_PARANOIA
 51 
 52 
 53 /* We must ignore files (but only files) which might have mandatory
 54  * locks on them because there is no way to know if the accesser has
 55  * the lock.
 56  */
 57 #define IS_ISMNDLK(i)   (S_ISREG((i)->i_mode) && MANDATORY_LOCK(i))
 58 
 59 /*
 60  * This is a cache of readahead params that help us choose the proper
 61  * readahead strategy. Initially, we set all readahead parameters to 0
 62  * and let the VFS handle things.
 63  * If you increase the number of cached files very much, you'll need to
 64  * add a hash table here.
 65  */
 66 struct raparms {
 67         struct raparms          *p_next;
 68         unsigned int            p_count;
 69         ino_t                   p_ino;
 70         dev_t                   p_dev;
 71         struct file_ra_state    p_ra;
 72 };
 73 
 74 static struct raparms *         raparml;
 75 static struct raparms *         raparm_cache;
 76 
 77 /* 
 78  * Called from nfsd_lookup and encode_dirent. Check if we have crossed 
 79  * a mount point.
 80  * Returns -EAGAIN leaving *dpp and *expp unchanged, 
 81  *  or nfs_ok having possibly changed *dpp and *expp
 82  */
 83 int
 84 nfsd_cross_mnt(struct svc_rqst *rqstp, struct dentry **dpp, 
 85                         struct svc_export **expp)
 86 {
 87         struct svc_export *exp = *expp, *exp2 = NULL;
 88         struct dentry *dentry = *dpp;
 89         struct vfsmount *mnt = mntget(exp->ex_mnt);
 90         struct dentry *mounts = dget(dentry);
 91         int err = nfs_ok;
 92 
 93         while (follow_down(&mnt,&mounts)&&d_mountpoint(mounts));
 94 
 95         exp2 = exp_get_by_name(exp->ex_client, mnt, mounts, &rqstp->rq_chandle);
 96         if (IS_ERR(exp2)) {
 97                 err = PTR_ERR(exp2);
 98                 dput(mounts);
 99                 mntput(mnt);
100                 goto out;
101         }
102         if (exp2 && ((exp->ex_flags & NFSEXP_CROSSMOUNT) || EX_NOHIDE(exp2))) {
103                 /* successfully crossed mount point */
104                 exp_put(exp);
105                 *expp = exp2;
106                 dput(dentry);
107                 *dpp = mounts;
108         } else {
109                 if (exp2) exp_put(exp2);
110                 dput(mounts);
111         }
112         mntput(mnt);
113 out:
114         return err;
115 }
116 
117 /*
118  * Look up one component of a pathname.
119  * N.B. After this call _both_ fhp and resfh need an fh_put
120  *
121  * If the lookup would cross a mountpoint, and the mounted filesystem
122  * is exported to the client with NFSEXP_NOHIDE, then the lookup is
123  * accepted as it stands and the mounted directory is
124  * returned. Otherwise the covered directory is returned.
125  * NOTE: this mountpoint crossing is not supported properly by all
126  *   clients and is explicitly disallowed for NFSv3
127  *      NeilBrown <neilb@cse.unsw.edu.au>
128  */
129 int
130 nfsd_lookup(struct svc_rqst *rqstp, struct svc_fh *fhp, const char *name,
131                                         int len, struct svc_fh *resfh)
132 {
133         struct svc_export       *exp;
134         struct dentry           *dparent;
135         struct dentry           *dentry;
136         int                     err;
137 
138         dprintk("nfsd: nfsd_lookup(fh %s, %.*s)\n", SVCFH_fmt(fhp), len,name);
139 
140         /* Obtain dentry and export. */
141         err = fh_verify(rqstp, fhp, S_IFDIR, MAY_EXEC);
142         if (err)
143                 goto out;
144 
145         dparent = fhp->fh_dentry;
146         exp  = fhp->fh_export;
147 
148         err = nfserr_acces;
149 
150         /* Lookup the name, but don't follow links */
151         if (isdotent(name, len)) {
152                 if (len==1)
153                         dentry = dget(dparent);
154                 else if (dparent != exp->ex_dentry) {
155                         dentry = dget_parent(dparent);
156                 } else  if (!EX_NOHIDE(exp))
157                         dentry = dget(dparent); /* .. == . just like at / */
158                 else {
159                         /* checking mountpoint crossing is very different when stepping up */
160                         struct svc_export *exp2 = NULL;
161                         struct dentry *dp;
162                         struct vfsmount *mnt = mntget(exp->ex_mnt);
163                         dentry = dget(dparent);
164                         while(follow_up(&mnt, &dentry))
165                                 ;
166                         dp = dget_parent(dentry);
167                         dput(dentry);
168                         dentry = dp;
169 
170                         exp2 = exp_parent(exp->ex_client, mnt, dentry,
171                                           &rqstp->rq_chandle);
172                         if (IS_ERR(exp2)) {
173                                 err = PTR_ERR(exp2);
174                                 dput(dentry);
175                                 mntput(mnt);
176                                 goto out;
177                         }
178                         if (!exp2) {
179                                 dput(dentry);
180                                 dentry = dget(dparent);
181                         } else {
182                                 exp_put(exp);
183                                 exp = exp2;
184                         }
185                         mntput(mnt);
186                 }
187         } else {
188                 fh_lock(fhp);
189                 dentry = lookup_one_len(name, dparent, len);
190                 err = PTR_ERR(dentry);
191                 if (IS_ERR(dentry))
192                         goto out_nfserr;
193                 /*
194                  * check if we have crossed a mount point ...
195                  */
196                 if (d_mountpoint(dentry)) {
197                         if ((err = nfsd_cross_mnt(rqstp, &dentry, &exp))) {
198                                 dput(dentry);
199                                 goto out_nfserr;
200                         }
201                 }
202         }
203         /*
204          * Note: we compose the file handle now, but as the
205          * dentry may be negative, it may need to be updated.
206          */
207         err = fh_compose(resfh, exp, dentry, fhp);
208         if (!err && !dentry->d_inode)
209                 err = nfserr_noent;
210 out:
211         return err;
212 
213 out_nfserr:
214         err = nfserrno(err);
215         goto out;
216 }
217 
218 /*
219  * Set various file attributes.
220  * N.B. After this call fhp needs an fh_put
221  */
222 int
223 nfsd_setattr(struct svc_rqst *rqstp, struct svc_fh *fhp, struct iattr *iap,
224              int check_guard, time_t guardtime)
225 {
226         struct dentry   *dentry;
227         struct inode    *inode;
228         int             accmode = MAY_SATTR;
229         int             ftype = 0;
230         int             imode;
231         int             err;
232         int             size_change = 0;
233 
234         if (iap->ia_valid & (ATTR_ATIME | ATTR_MTIME | ATTR_SIZE))
235                 accmode |= MAY_WRITE|MAY_OWNER_OVERRIDE;
236         if (iap->ia_valid & ATTR_SIZE)
237                 ftype = S_IFREG;
238 
239         /* Get inode */
240         err = fh_verify(rqstp, fhp, ftype, accmode);
241         if (err || !iap->ia_valid)
242                 goto out;
243 
244         dentry = fhp->fh_dentry;
245         inode = dentry->d_inode;
246 
247         /* NFSv2 does not differentiate between "set-[ac]time-to-now"
248          * which only requires access, and "set-[ac]time-to-X" which
249          * requires ownership.
250          * So if it looks like it might be "set both to the same time which
251          * is close to now", and if inode_change_ok fails, then we
252          * convert to "set to now" instead of "set to explicit time"
253          *
254          * We only call inode_change_ok as the last test as technically
255          * it is not an interface that we should be using.  It is only
256          * valid if the filesystem does not define it's own i_op->setattr.
257          */
258 #define BOTH_TIME_SET (ATTR_ATIME_SET | ATTR_MTIME_SET)
259 #define MAX_TOUCH_TIME_ERROR (30*60)
260         if ((iap->ia_valid & BOTH_TIME_SET) == BOTH_TIME_SET
261             && iap->ia_mtime.tv_sec == iap->ia_atime.tv_sec
262             ) {
263             /* Looks probable.  Now just make sure time is in the right ballpark.
264              * Solaris, at least, doesn't seem to care what the time request is.
265              * We require it be within 30 minutes of now.
266              */
267             time_t delta = iap->ia_atime.tv_sec - get_seconds();
268             if (delta<0) delta = -delta;
269             if (delta < MAX_TOUCH_TIME_ERROR &&
270                 inode_change_ok(inode, iap) != 0) {
271                 /* turn off ATTR_[AM]TIME_SET but leave ATTR_[AM]TIME
272                  * this will cause notify_change to set these times to "now"
273                  */
274                 iap->ia_valid &= ~BOTH_TIME_SET;
275             }
276         }
277             
278         /* The size case is special. It changes the file as well as the attributes.  */
279         if (iap->ia_valid & ATTR_SIZE) {
280                 if (iap->ia_size < inode->i_size) {
281                         err = nfsd_permission(fhp->fh_export, dentry, MAY_TRUNC|MAY_OWNER_OVERRIDE);
282                         if (err)
283                                 goto out;
284                 }
285 
286                 /*
287                  * If we are changing the size of the file, then
288                  * we need to break all leases.
289                  */
290                 err = break_lease(inode, FMODE_WRITE);
291                 if (err)
292                         goto out_nfserr;
293 
294                 err = get_write_access(inode);
295                 if (err)
296                         goto out_nfserr;
297 
298                 size_change = 1;
299                 err = locks_verify_truncate(inode, NULL, iap->ia_size);
300                 if (err) {
301                         put_write_access(inode);
302                         goto out_nfserr;
303                 }
304                 DQUOT_INIT(inode);
305         }
306 
307         imode = inode->i_mode;
308         if (iap->ia_valid & ATTR_MODE) {
309                 iap->ia_mode &= S_IALLUGO;
310                 imode = iap->ia_mode |= (imode & ~S_IALLUGO);
311         }
312 
313         /* Revoke setuid/setgid bit on chown/chgrp */
314         if ((iap->ia_valid & ATTR_UID) && iap->ia_uid != inode->i_uid)
315                 iap->ia_valid |= ATTR_KILL_SUID;
316         if ((iap->ia_valid & ATTR_GID) && iap->ia_gid != inode->i_gid)
317                 iap->ia_valid |= ATTR_KILL_SGID;
318 
319         /* Change the attributes. */
320 
321         iap->ia_valid |= ATTR_CTIME;
322 
323         err = nfserr_notsync;
324         if (!check_guard || guardtime == inode->i_ctime.tv_sec) {
325                 fh_lock(fhp);
326                 err = notify_change(dentry, iap);
327                 err = nfserrno(err);
328                 fh_unlock(fhp);
329         }
330         if (size_change)
331                 put_write_access(inode);
332         if (!err)
333                 if (EX_ISSYNC(fhp->fh_export))
334                         write_inode_now(inode, 1);
335 out:
336         return err;
337 
338 out_nfserr:
339         err = nfserrno(err);
340         goto out;
341 }
342 
343 #ifdef CONFIG_NFSD_V3
344 /*
345  * Check server access rights to a file system object
346  */
347 struct accessmap {
348         u32             access;
349         int             how;
350 };
351 static struct accessmap nfs3_regaccess[] = {
352     {   NFS3_ACCESS_READ,       MAY_READ                        },
353     {   NFS3_ACCESS_EXECUTE,    MAY_EXEC                        },
354     {   NFS3_ACCESS_MODIFY,     MAY_WRITE|MAY_TRUNC             },
355     {   NFS3_ACCESS_EXTEND,     MAY_WRITE                       },
356 
357     {   0,                      0                               }
358 };
359 
360 static struct accessmap nfs3_diraccess[] = {
361     {   NFS3_ACCESS_READ,       MAY_READ                        },
362     {   NFS3_ACCESS_LOOKUP,     MAY_EXEC                        },
363     {   NFS3_ACCESS_MODIFY,     MAY_EXEC|MAY_WRITE|MAY_TRUNC    },
364     {   NFS3_ACCESS_EXTEND,     MAY_EXEC|MAY_WRITE              },
365     {   NFS3_ACCESS_DELETE,     MAY_REMOVE                      },
366 
367     {   0,                      0                               }
368 };
369 
370 static struct accessmap nfs3_anyaccess[] = {
371         /* Some clients - Solaris 2.6 at least, make an access call
372          * to the server to check for access for things like /dev/null
373          * (which really, the server doesn't care about).  So
374          * We provide simple access checking for them, looking
375          * mainly at mode bits
376          */
377     {   NFS3_ACCESS_READ,       MAY_READ                        },
378     {   NFS3_ACCESS_EXECUTE,    MAY_EXEC                        },
379     {   NFS3_ACCESS_MODIFY,     MAY_WRITE                       },
380     {   NFS3_ACCESS_EXTEND,     MAY_WRITE                       },
381 
382     {   0,                      0                               }
383 };
384 
385 int
386 nfsd_access(struct svc_rqst *rqstp, struct svc_fh *fhp, u32 *access, u32 *supported)
387 {
388         struct accessmap        *map;
389         struct svc_export       *export;
390         struct dentry           *dentry;
391         u32                     query, result = 0, sresult = 0;
392         unsigned int            error;
393 
394         error = fh_verify(rqstp, fhp, 0, MAY_NOP);
395         if (error)
396                 goto out;
397 
398         export = fhp->fh_export;
399         dentry = fhp->fh_dentry;
400 
401         if (S_ISREG(dentry->d_inode->i_mode))
402                 map = nfs3_regaccess;
403         else if (S_ISDIR(dentry->d_inode->i_mode))
404                 map = nfs3_diraccess;
405         else
406                 map = nfs3_anyaccess;
407 
408 
409         query = *access;
410         for  (; map->access; map++) {
411                 if (map->access & query) {
412                         unsigned int err2;
413 
414                         sresult |= map->access;
415 
416                         err2 = nfsd_permission(export, dentry, map->how);
417                         switch (err2) {
418                         case nfs_ok:
419                                 result |= map->access;
420                                 break;
421                                 
422                         /* the following error codes just mean the access was not allowed,
423                          * rather than an error occurred */
424                         case nfserr_rofs:
425                         case nfserr_acces:
426                         case nfserr_perm:
427                                 /* simply don't "or" in the access bit. */
428                                 break;
429                         default:
430                                 error = err2;
431                                 goto out;
432                         }
433                 }
434         }
435         *access = result;
436         if (supported)
437                 *supported = sresult;
438 
439  out:
440         return error;
441 }
442 #endif /* CONFIG_NFSD_V3 */
443 
444 
445 
446 /*
447  * Open an existing file or directory.
448  * The access argument indicates the type of open (read/write/lock)
449  * N.B. After this call fhp needs an fh_put
450  */
451 int
452 nfsd_open(struct svc_rqst *rqstp, struct svc_fh *fhp, int type,
453                         int access, struct file *filp)
454 {
455         struct dentry   *dentry;
456         struct inode    *inode;
457         int             flags = O_RDONLY|O_LARGEFILE, err;
458 
459         /*
460          * If we get here, then the client has already done an "open",
461          * and (hopefully) checked permission - so allow OWNER_OVERRIDE
462          * in case a chmod has now revoked permission.
463          */
464         err = fh_verify(rqstp, fhp, type, access | MAY_OWNER_OVERRIDE);
465         if (err)
466                 goto out;
467 
468         dentry = fhp->fh_dentry;
469         inode = dentry->d_inode;
470 
471         /* Disallow access to files with the append-only bit set or
472          * with mandatory locking enabled
473          */
474         err = nfserr_perm;
475         if (IS_APPEND(inode) || IS_ISMNDLK(inode))
476                 goto out;
477         if (!inode->i_fop)
478                 goto out;
479 
480         /*
481          * Check to see if there are any leases on this file.
482          * This may block while leases are broken.
483          */
484         err = break_lease(inode, (access & MAY_WRITE) ? FMODE_WRITE : 0);
485         if (err)
486                 goto out_nfserr;
487 
488         if (access & MAY_WRITE) {
489                 err = get_write_access(inode);
490                 if (err)
491                         goto out_nfserr;
492 
493                 flags = O_WRONLY|O_LARGEFILE;
494 
495                 DQUOT_INIT(inode);
496         }
497 
498         err = open_private_file(filp, dentry, flags);
499         if (!err) {
500                 filp->f_vfsmnt = fhp->fh_export->ex_mnt;
501         } else if (access & MAY_WRITE)
502                 put_write_access(inode);
503 
504 out_nfserr:
505         if (err)
506                 err = nfserrno(err);
507 out:
508         return err;
509 }
510 
511 /*
512  * Close a file.
513  */
514 void
515 nfsd_close(struct file *filp)
516 {
517         struct dentry   *dentry = filp->f_dentry;
518         struct inode    *inode = dentry->d_inode;
519 
520         close_private_file(filp);
521         if (filp->f_mode & FMODE_WRITE)
522                 put_write_access(inode);
523 }
524 
525 /*
526  * Sync a file
527  * As this calls fsync (not fdatasync) there is no need for a write_inode
528  * after it.
529  */
530 inline void nfsd_dosync(struct file *filp, struct dentry *dp, 
531                         struct file_operations *fop)
532 {
533         struct inode *inode = dp->d_inode;
534         int (*fsync) (struct file *, struct dentry *, int);
535 
536         filemap_fdatawrite(inode->i_mapping);
537         if (fop && (fsync = fop->fsync))
538                 fsync(filp, dp, 0);
539         filemap_fdatawait(inode->i_mapping);
540 }
541         
542 
543 void
544 nfsd_sync(struct file *filp)
545 {
546         struct inode *inode = filp->f_dentry->d_inode;
547         dprintk("nfsd: sync file %s\n", filp->f_dentry->d_name.name);
548         down(&inode->i_sem);
549         nfsd_dosync(filp, filp->f_dentry, filp->f_op);
550         up(&inode->i_sem);
551 }
552 
553 void
554 nfsd_sync_dir(struct dentry *dp)
555 {
556         nfsd_dosync(NULL, dp, dp->d_inode->i_fop);
557 }
558 
559 /*
560  * Obtain the readahead parameters for the file
561  * specified by (dev, ino).
562  */
563 static spinlock_t ra_lock = SPIN_LOCK_UNLOCKED;
564 
565 static inline struct raparms *
566 nfsd_get_raparms(dev_t dev, ino_t ino)
567 {
568         struct raparms  *ra, **rap, **frap = NULL;
569         int depth = 0;
570 
571         spin_lock(&ra_lock);
572         for (rap = &raparm_cache; (ra = *rap); rap = &ra->p_next) {
573                 if (ra->p_ino == ino && ra->p_dev == dev)
574                         goto found;
575                 depth++;
576                 if (ra->p_count == 0)
577                         frap = rap;
578         }
579         depth = nfsdstats.ra_size*11/10;
580         if (!frap) {    
581                 spin_unlock(&ra_lock);
582                 return NULL;
583         }
584         rap = frap;
585         ra = *frap;
586         ra->p_dev = dev;
587         ra->p_ino = ino;
588         memset(&ra->p_ra, 0, sizeof(ra->p_ra));
589 found:
590         if (rap != &raparm_cache) {
591                 *rap = ra->p_next;
592                 ra->p_next   = raparm_cache;
593                 raparm_cache = ra;
594         }
595         ra->p_count++;
596         nfsdstats.ra_depth[depth*10/nfsdstats.ra_size]++;
597         spin_unlock(&ra_lock);
598         return ra;
599 }
600 
601 /*
602  * Grab and keep cached pages assosiated with a file in the svc_rqst
603  * so that they can be passed to the netowork sendmsg/sendpage routines
604  * directrly. They will be released after the sending has completed.
605  */
606 static int
607 nfsd_read_actor(read_descriptor_t *desc, struct page *page, unsigned long offset , unsigned long size)
608 {
609         unsigned long count = desc->count;
610         struct svc_rqst *rqstp = (struct svc_rqst *)desc->buf;
611 
612         if (size > count)
613                 size = count;
614 
615         if (rqstp->rq_res.page_len == 0) {
616                 get_page(page);
617                 rqstp->rq_respages[rqstp->rq_resused++] = page;
618                 rqstp->rq_res.page_base = offset;
619                 rqstp->rq_res.page_len = size;
620         } else if (page != rqstp->rq_respages[rqstp->rq_resused-1]) {
621                 get_page(page);
622                 rqstp->rq_respages[rqstp->rq_resused++] = page;
623                 rqstp->rq_res.page_len += size;
624         } else {
625                 rqstp->rq_res.page_len += size;
626         }
627 
628         desc->count = count - size;
629         desc->written += size;
630         return size;
631 }
632 
633 /*
634  * Read data from a file. count must contain the requested read count
635  * on entry. On return, *count contains the number of bytes actually read.
636  * N.B. After this call fhp needs an fh_put
637  */
638 int
639 nfsd_read(struct svc_rqst *rqstp, struct svc_fh *fhp, loff_t offset,
640           struct iovec *vec, int vlen, unsigned long *count)
641 {
642         struct raparms  *ra;
643         mm_segment_t    oldfs;
644         int             err;
645         struct file     file;
646         struct inode    *inode;
647 
648         err = nfsd_open(rqstp, fhp, S_IFREG, MAY_READ, &file);
649         if (err)
650                 goto out;
651         err = nfserr_perm;
652         inode = file.f_dentry->d_inode;
653 #ifdef MSNFS
654         if ((fhp->fh_export->ex_flags & NFSEXP_MSNFS) &&
655                 (!lock_may_read(inode, offset, *count)))
656                 goto out_close;
657 #endif
658 
659         /* Get readahead parameters */
660         ra = nfsd_get_raparms(inode->i_sb->s_dev, inode->i_ino);
661         if (ra)
662                 file.f_ra = ra->p_ra;
663 
664         if (file.f_op->sendfile) {
665                 svc_pushback_unused_pages(rqstp);
666                 err = file.f_op->sendfile(&file, &offset, *count,
667                                                  nfsd_read_actor, rqstp);
668         } else {
669                 oldfs = get_fs();
670                 set_fs(KERNEL_DS);
671                 err = vfs_readv(&file, vec, vlen, &offset);
672                 set_fs(oldfs);
673         }
674 
675         /* Write back readahead params */
676         if (ra)
677                 ra->p_ra = file.f_ra;
678 
679         if (err >= 0) {
680                 nfsdstats.io_read += err;
681                 *count = err;
682                 err = 0;
683         } else 
684                 err = nfserrno(err);
685 out_close:
686         nfsd_close(&file);
687 out:
688         return err;
689 }
690 
691 /*
692  * Write data to a file.
693  * The stable flag requests synchronous writes.
694  * N.B. After this call fhp needs an fh_put
695  */
696 int
697 nfsd_write(struct svc_rqst *rqstp, struct svc_fh *fhp, loff_t offset,
698                                 struct iovec *vec, int vlen,
699                                 unsigned long cnt, int *stablep)
700 {
701         struct svc_export       *exp;
702         struct file             file;
703         struct dentry           *dentry;
704         struct inode            *inode;
705         mm_segment_t            oldfs;
706         int                     err = 0;
707         int                     stable = *stablep;
708 
709         err = nfsd_open(rqstp, fhp, S_IFREG, MAY_WRITE, &file);
710         if (err)
711                 goto out;
712         if (!cnt)
713                 goto out_close;
714         err = nfserr_perm;
715 
716 #ifdef MSNFS
717         if ((fhp->fh_export->ex_flags & NFSEXP_MSNFS) &&
718                 (!lock_may_write(file.f_dentry->d_inode, offset, cnt)))
719                 goto out_close;
720 #endif
721 
722         dentry = file.f_dentry;
723         inode = dentry->d_inode;
724         exp   = fhp->fh_export;
725 
726         /*
727          * Request sync writes if
728          *  -   the sync export option has been set, or
729          *  -   the client requested O_SYNC behavior (NFSv3 feature).
730          *  -   The file system doesn't support fsync().
731          * When gathered writes have been configured for this volume,
732          * flushing the data to disk is handled separately below.
733          */
734 
735         if (file.f_op->fsync == 0) {/* COMMIT3 cannot work */
736                stable = 2;
737                *stablep = 2; /* FILE_SYNC */
738         }
739 
740         if (!EX_ISSYNC(exp))
741                 stable = 0;
742         if (stable && !EX_WGATHER(exp))
743                 file.f_flags |= O_SYNC;
744 
745         /* Write the data. */
746         oldfs = get_fs(); set_fs(KERNEL_DS);
747         err = vfs_writev(&file, vec, vlen, &offset);
748         if (err >= 0)
749                 nfsdstats.io_write += cnt;
750         set_fs(oldfs);
751 
752         /* clear setuid/setgid flag after write */
753         if (err >= 0 && (inode->i_mode & (S_ISUID | S_ISGID))) {
754                 struct iattr    ia;
755                 ia.ia_valid = ATTR_KILL_SUID | ATTR_KILL_SGID;
756 
757                 down(&inode->i_sem);
758                 notify_change(dentry, &ia);
759                 up(&inode->i_sem);
760         }
761 
762         if (err >= 0 && stable) {
763                 static ino_t    last_ino;
764                 static dev_t    last_dev;
765 
766                 /*
767                  * Gathered writes: If another process is currently
768                  * writing to the file, there's a high chance
769                  * this is another nfsd (triggered by a bulk write
770                  * from a client's biod). Rather than syncing the
771                  * file with each write request, we sleep for 10 msec.
772                  *
773                  * I don't know if this roughly approximates
774                  * C. Juszak's idea of gathered writes, but it's a
775                  * nice and simple solution (IMHO), and it seems to
776                  * work:-)
777                  */
778                 if (EX_WGATHER(exp)) {
779                         if (atomic_read(&inode->i_writecount) > 1
780                             || (last_ino == inode->i_ino && last_dev == inode->i_sb->s_dev)) {
781                                 dprintk("nfsd: write defer %d\n", current->pid);
782                                 set_current_state(TASK_UNINTERRUPTIBLE);
783                                 schedule_timeout((HZ+99)/100);
784                                 dprintk("nfsd: write resume %d\n", current->pid);
785                         }
786 
787                         if (inode->i_state & I_DIRTY) {
788                                 dprintk("nfsd: write sync %d\n", current->pid);
789                                 nfsd_sync(&file);
790                         }
791 #if 0
792                         wake_up(&inode->i_wait);
793 #endif
794                 }
795                 last_ino = inode->i_ino;
796                 last_dev = inode->i_sb->s_dev;
797         }
798 
799         dprintk("nfsd: write complete err=%d\n", err);
800         if (err >= 0)
801                 err = 0;
802         else 
803                 err = nfserrno(err);
804 out_close:
805         nfsd_close(&file);
806 out:
807         return err;
808 }
809 
810 
811 #ifdef CONFIG_NFSD_V3
812 /*
813  * Commit all pending writes to stable storage.
814  * Strictly speaking, we could sync just the indicated file region here,
815  * but there's currently no way we can ask the VFS to do so.
816  *
817  * Unfortunately we cannot lock the file to make sure we return full WCC
818  * data to the client, as locking happens lower down in the filesystem.
819  */
820 int
821 nfsd_commit(struct svc_rqst *rqstp, struct svc_fh *fhp,
822                off_t offset, unsigned long count)
823 {
824         struct file     file;
825         int             err;
826 
827         if ((u64)count > ~(u64)offset)
828                 return nfserr_inval;
829 
830         if ((err = nfsd_open(rqstp, fhp, S_IFREG, MAY_WRITE, &file)) != 0)
831                 return err;
832         if (EX_ISSYNC(fhp->fh_export)) {
833                 if (file.f_op && file.f_op->fsync) {
834                         nfsd_sync(&file);
835                 } else {
836                         err = nfserr_notsupp;
837                 }
838         }
839 
840         nfsd_close(&file);
841         return err;
842 }
843 #endif /* CONFIG_NFSD_V3 */
844 
845 /*
846  * Create a file (regular, directory, device, fifo); UNIX sockets 
847  * not yet implemented.
848  * If the response fh has been verified, the parent directory should
849  * already be locked. Note that the parent directory is left locked.
850  *
851  * N.B. Every call to nfsd_create needs an fh_put for _both_ fhp and resfhp
852  */
853 int
854 nfsd_create(struct svc_rqst *rqstp, struct svc_fh *fhp,
855                 char *fname, int flen, struct iattr *iap,
856                 int type, dev_t rdev, struct svc_fh *resfhp)
857 {
858         struct dentry   *dentry, *dchild;
859         struct inode    *dirp;
860         int             err;
861 
862         err = nfserr_perm;
863         if (!flen)
864                 goto out;
865         err = nfserr_exist;
866         if (isdotent(fname, flen))
867                 goto out;
868 
869         err = fh_verify(rqstp, fhp, S_IFDIR, MAY_CREATE);
870         if (err)
871                 goto out;
872 
873         dentry = fhp->fh_dentry;
874         dirp = dentry->d_inode;
875 
876         err = nfserr_notdir;
877         if(!dirp->i_op || !dirp->i_op->lookup)
878                 goto out;
879         /*
880          * Check whether the response file handle has been verified yet.
881          * If it has, the parent directory should already be locked.
882          */
883         if (!resfhp->fh_dentry) {
884                 /* called from nfsd_proc_mkdir, or possibly nfsd3_proc_create */
885                 fh_lock(fhp);
886                 dchild = lookup_one_len(fname, dentry, flen);
887                 err = PTR_ERR(dchild);
888                 if (IS_ERR(dchild))
889                         goto out_nfserr;
890                 err = fh_compose(resfhp, fhp->fh_export, dchild, fhp);
891                 if (err)
892                         goto out;
893         } else {
894                 /* called from nfsd_proc_create */
895                 dchild = resfhp->fh_dentry;
896                 if (!fhp->fh_locked) {
897                         /* not actually possible */
898                         printk(KERN_ERR
899                                 "nfsd_create: parent %s/%s not locked!\n",
900                                 dentry->d_parent->d_name.name,
901                                 dentry->d_name.name);
902                         err = -EIO;
903                         goto out;
904                 }
905         }
906         /*
907          * Make sure the child dentry is still negative ...
908          */
909         err = nfserr_exist;
910         if (dchild->d_inode) {
911                 dprintk("nfsd_create: dentry %s/%s not negative!\n",
912                         dentry->d_name.name, dchild->d_name.name);
913                 goto out; 
914         }
915 
916         if (!(iap->ia_valid & ATTR_MODE))
917                 iap->ia_mode = 0;
918         iap->ia_mode = (iap->ia_mode & S_IALLUGO) | type;
919 
920         /*
921          * Get the dir op function pointer.
922          */
923         err = nfserr_perm;
924         switch (type) {
925         case S_IFREG:
926                 err = vfs_create(dirp, dchild, iap->ia_mode, NULL);
927                 break;
928         case S_IFDIR:
929                 err = vfs_mkdir(dirp, dchild, iap->ia_mode);
930                 break;
931         case S_IFCHR:
932         case S_IFBLK:
933         case S_IFIFO:
934         case S_IFSOCK:
935                 err = vfs_mknod(dirp, dchild, iap->ia_mode, rdev);
936                 break;
937         default:
938                 printk("nfsd: bad file type %o in nfsd_create\n", type);
939                 err = -EINVAL;
940         }
941         if (err < 0)
942                 goto out_nfserr;
943 
944         if (EX_ISSYNC(fhp->fh_export)) {
945                 nfsd_sync_dir(dentry);
946                 write_inode_now(dchild->d_inode, 1);
947         }
948 
949 
950         /* Set file attributes. Mode has already been set and
951          * setting uid/gid works only for root. Irix appears to
952          * send along the gid when it tries to implement setgid
953          * directories via NFS.
954          */
955         err = 0;
956         if ((iap->ia_valid &= ~(ATTR_UID|ATTR_GID|ATTR_MODE)) != 0)
957                 err = nfsd_setattr(rqstp, resfhp, iap, 0, (time_t)0);
958         /*
959          * Update the file handle to get the new inode info.
960          */
961         if (!err)
962                 err = fh_update(resfhp);
963 out:
964         return err;
965 
966 out_nfserr:
967         err = nfserrno(err);
968         goto out;
969 }
970 
971 #ifdef CONFIG_NFSD_V3
972 /*
973  * NFSv3 version of nfsd_create
974  */
975 int
976 nfsd_create_v3(struct svc_rqst *rqstp, struct svc_fh *fhp,
977                 char *fname, int flen, struct iattr *iap,
978                 struct svc_fh *resfhp, int createmode, u32 *verifier,
979                 int *truncp)
980 {
981         struct dentry   *dentry, *dchild;
982         struct inode    *dirp;
983         int             err;
984         __u32           v_mtime=0, v_atime=0;
985         int             v_mode=0;
986 
987         err = nfserr_perm;
988         if (!flen)
989                 goto out;
990         err = nfserr_exist;
991         if (isdotent(fname, flen))
992                 goto out;
993         if (!(iap->ia_valid & ATTR_MODE))
994                 iap->ia_mode = 0;
995         err = fh_verify(rqstp, fhp, S_IFDIR, MAY_CREATE);
996         if (err)
997                 goto out;
998 
999         dentry = fhp->fh_dentry;
1000         dirp = dentry->d_inode;
1001 
1002         /* Get all the sanity checks out of the way before
1003          * we lock the parent. */
1004         err = nfserr_notdir;
1005         if(!dirp->i_op || !dirp->i_op->lookup)
1006                 goto out;
1007         fh_lock(fhp);
1008 
1009         /*
1010          * Compose the response file handle.
1011          */
1012         dchild = lookup_one_len(fname, dentry, flen);
1013         err = PTR_ERR(dchild);
1014         if (IS_ERR(dchild))
1015                 goto out_nfserr;
1016 
1017         err = fh_compose(resfhp, fhp->fh_export, dchild, fhp);
1018         if (err)
1019                 goto out;
1020 
1021         if (createmode == NFS3_CREATE_EXCLUSIVE) {
1022                 /* while the verifier would fit in mtime+atime,
1023                  * solaris7 gets confused (bugid 4218508) if these have
1024                  * the high bit set, so we use the mode as well
1025                  */
1026                 v_mtime = verifier[0]&0x7fffffff;
1027                 v_atime = verifier[1]&0x7fffffff;
1028                 v_mode  = S_IFREG
1029                         | ((verifier[0]&0x80000000) >> (32-7)) /* u+x */
1030                         | ((verifier[1]&0x80000000) >> (32-9)) /* u+r */
1031                         ;
1032         }
1033         
1034         if (dchild->d_inode) {
1035                 err = 0;
1036 
1037                 switch (createmode) {
1038                 case NFS3_CREATE_UNCHECKED:
1039                         if (! S_ISREG(dchild->d_inode->i_mode))
1040                                 err = nfserr_exist;
1041                         else if (truncp) {
1042                                 /* in nfsv4, we need to treat this case a little
1043                                  * differently.  we don't want to truncate the
1044                                  * file now; this would be wrong if the OPEN
1045                                  * fails for some other reason.  furthermore,
1046                                  * if the size is nonzero, we should ignore it
1047                                  * according to spec!
1048                                  */
1049                                 *truncp = (iap->ia_valid & ATTR_SIZE) && !iap->ia_size;
1050                         }
1051                         else {
1052                                 iap->ia_valid &= ATTR_SIZE;
1053                                 goto set_attr;
1054                         }
1055                         break;
1056                 case NFS3_CREATE_EXCLUSIVE:
1057                         if (   dchild->d_inode->i_mtime.tv_sec == v_mtime
1058                             && dchild->d_inode->i_atime.tv_sec == v_atime
1059                             && dchild->d_inode->i_mode  == v_mode
1060                             && dchild->d_inode->i_size  == 0 )
1061                                 break;
1062                          /* fallthru */
1063                 case NFS3_CREATE_GUARDED:
1064                         err = nfserr_exist;
1065                 }
1066                 goto out;
1067         }
1068 
1069         err = vfs_create(dirp, dchild, iap->ia_mode, NULL);
1070         if (err < 0)
1071                 goto out_nfserr;
1072 
1073         if (EX_ISSYNC(fhp->fh_export)) {
1074                 nfsd_sync_dir(dentry);
1075                 /* setattr will sync the child (or not) */
1076         }
1077 
1078         /*
1079          * Update the filehandle to get the new inode info.
1080          */
1081         err = fh_update(resfhp);
1082         if (err)
1083                 goto out;
1084 
1085         if (createmode == NFS3_CREATE_EXCLUSIVE) {
1086                 /* Cram the verifier into atime/mtime/mode */
1087                 iap->ia_valid = ATTR_MTIME|ATTR_ATIME
1088                         | ATTR_MTIME_SET|ATTR_ATIME_SET
1089                         | ATTR_MODE;
1090                 /* XXX someone who knows this better please fix it for nsec */ 
1091                 iap->ia_mtime.tv_sec = v_mtime;
1092                 iap->ia_atime.tv_sec = v_atime;
1093                 iap->ia_mtime.tv_nsec = 0;
1094                 iap->ia_atime.tv_nsec = 0;
1095                 iap->ia_mode  = v_mode;
1096         }
1097 
1098         /* Set file attributes.
1099          * Mode has already been set but we might need to reset it
1100          * for CREATE_EXCLUSIVE
1101          * Irix appears to send along the gid when it tries to
1102          * implement setgid directories via NFS. Clear out all that cruft.
1103          */
1104  set_attr:
1105         if ((iap->ia_valid &= ~(ATTR_UID|ATTR_GID)) != 0)
1106                 err = nfsd_setattr(rqstp, resfhp, iap, 0, (time_t)0);
1107 
1108  out:
1109         fh_unlock(fhp);
1110         return err;
1111  
1112  out_nfserr:
1113         err = nfserrno(err);
1114         goto out;
1115 }
1116 #endif /* CONFIG_NFSD_V3 */
1117 
1118 /*
1119  * Read a symlink. On entry, *lenp must contain the maximum path length that
1120  * fits into the buffer. On return, it contains the true length.
1121  * N.B. After this call fhp needs an fh_put
1122  */
1123 int
1124 nfsd_readlink(struct svc_rqst *rqstp, struct svc_fh *fhp, char *buf, int *lenp)
1125 {
1126         struct dentry   *dentry;
1127         struct inode    *inode;
1128         mm_segment_t    oldfs;
1129         int             err;
1130 
1131         err = fh_verify(rqstp, fhp, S_IFLNK, MAY_NOP);
1132         if (err)
1133                 goto out;
1134 
1135         dentry = fhp->fh_dentry;
1136         inode = dentry->d_inode;
1137 
1138         err = nfserr_inval;
1139         if (!inode->i_op || !inode->i_op->readlink)
1140                 goto out;
1141 
1142         update_atime(inode);
1143         /* N.B. Why does this call need a get_fs()??
1144          * Remove the set_fs and watch the fireworks:-) --okir
1145          */
1146 
1147         oldfs = get_fs(); set_fs(KERNEL_DS);
1148         err = inode->i_op->readlink(dentry, buf, *lenp);
1149         set_fs(oldfs);
1150 
1151         if (err < 0)
1152                 goto out_nfserr;
1153         *lenp = err;
1154         err = 0;
1155 out:
1156         return err;
1157 
1158 out_nfserr:
1159         err = nfserrno(err);
1160         goto out;
1161 }
1162 
1163 /*
1164  * Create a symlink and look up its inode
1165  * N.B. After this call _both_ fhp and resfhp need an fh_put
1166  */
1167 int
1168 nfsd_symlink(struct svc_rqst *rqstp, struct svc_fh *fhp,
1169                                 char *fname, int flen,
1170                                 char *path,  int plen,
1171                                 struct svc_fh *resfhp,
1172                                 struct iattr *iap)
1173 {
1174         struct dentry   *dentry, *dnew;
1175         int             err, cerr;
1176 
1177         err = nfserr_noent;
1178         if (!flen || !plen)
1179                 goto out;
1180         err = nfserr_exist;
1181         if (isdotent(fname, flen))
1182                 goto out;
1183 
1184         err = fh_verify(rqstp, fhp, S_IFDIR, MAY_CREATE);
1185         if (err)
1186                 goto out;
1187         fh_lock(fhp);
1188         dentry = fhp->fh_dentry;
1189         dnew = lookup_one_len(fname, dentry, flen);
1190         err = PTR_ERR(dnew);
1191         if (IS_ERR(dnew))
1192                 goto out_nfserr;
1193 
1194         if (unlikely(path[plen] != 0)) {
1195                 char *path_alloced = kmalloc(plen+1, GFP_KERNEL);
1196                 if (path_alloced == NULL)
1197                         err = -ENOMEM;
1198                 else {
1199                         strncpy(path_alloced, path, plen);
1200                         path_alloced[plen] = 0;
1201                         err = vfs_symlink(dentry->d_inode, dnew, path_alloced);
1202                         kfree(path_alloced);
1203                 }
1204         } else
1205                 err = vfs_symlink(dentry->d_inode, dnew, path);
1206 
1207         if (!err) {
1208                 if (EX_ISSYNC(fhp->fh_export))
1209                         nfsd_sync_dir(dentry);
1210                 if (iap) {
1211                         iap->ia_valid &= ATTR_MODE /* ~(ATTR_MODE|ATTR_UID|ATTR_GID)*/;
1212                         if (iap->ia_valid) {
1213                                 iap->ia_valid |= ATTR_CTIME;
1214                                 iap->ia_mode = (iap->ia_mode&S_IALLUGO)
1215                                         | S_IFLNK;
1216                                 err = notify_change(dnew, iap);
1217                                 if (err)
1218                                         err = nfserrno(err);
1219                                 else if (EX_ISSYNC(fhp->fh_export))
1220                                         write_inode_now(dentry->d_inode, 1);
1221                        }
1222                 }
1223         } else
1224                 err = nfserrno(err);
1225         fh_unlock(fhp);
1226 
1227         /* Compose the fh so the dentry will be freed ... */
1228         cerr = fh_compose(resfhp, fhp->fh_export, dnew, fhp);
1229         if (err==0) err = cerr;
1230 out:
1231         return err;
1232 
1233 out_nfserr:
1234         err = nfserrno(err);
1235         goto out;
1236 }
1237 
1238 /*
1239  * Create a hardlink
1240  * N.B. After this call _both_ ffhp and tfhp need an fh_put
1241  */
1242 int
1243 nfsd_link(struct svc_rqst *rqstp, struct svc_fh *ffhp,
1244                                 char *name, int len, struct svc_fh *tfhp)
1245 {
1246         struct dentry   *ddir, *dnew, *dold;
1247         struct inode    *dirp, *dest;
1248         int             err;
1249 
1250         err = fh_verify(rqstp, ffhp, S_IFDIR, MAY_CREATE);
1251         if (err)
1252                 goto out;
1253         err = fh_verify(rqstp, tfhp, -S_IFDIR, MAY_NOP);
1254         if (err)
1255                 goto out;
1256 
1257         err = nfserr_perm;
1258         if (!len)
1259                 goto out;
1260         err = nfserr_exist;
1261         if (isdotent(name, len))
1262                 goto out;
1263 
1264         fh_lock(ffhp);
1265         ddir = ffhp->fh_dentry;
1266         dirp = ddir->d_inode;
1267 
1268         dnew = lookup_one_len(name, ddir, len);
1269         err = PTR_ERR(dnew);
1270         if (IS_ERR(dnew))
1271                 goto out_nfserr;
1272 
1273         dold = tfhp->fh_dentry;
1274         dest = dold->d_inode;
1275 
1276         err = vfs_link(dold, dirp, dnew);
1277         if (!err) {
1278                 if (EX_ISSYNC(ffhp->fh_export)) {
1279                         nfsd_sync_dir(ddir);
1280                         write_inode_now(dest, 1);
1281                 }
1282         } else {
1283                 if (err == -EXDEV && rqstp->rq_vers == 2)
1284                         err = nfserr_acces;
1285                 else
1286                         err = nfserrno(err);
1287         }
1288 
1289         fh_unlock(ffhp);
1290         dput(dnew);
1291 out:
1292         return err;
1293 
1294 out_nfserr:
1295         err = nfserrno(err);
1296         goto out;
1297 }
1298 
1299 /*
1300  * Rename a file
1301  * N.B. After this call _both_ ffhp and tfhp need an fh_put
1302  */
1303 int
1304 nfsd_rename(struct svc_rqst *rqstp, struct svc_fh *ffhp, char *fname, int flen,
1305                             struct svc_fh *tfhp, char *tname, int tlen)
1306 {
1307         struct dentry   *fdentry, *tdentry, *odentry, *ndentry, *trap;
1308         struct inode    *fdir, *tdir;
1309         int             err;
1310 
1311         err = fh_verify(rqstp, ffhp, S_IFDIR, MAY_REMOVE);
1312         if (err)
1313                 goto out;
1314         err = fh_verify(rqstp, tfhp, S_IFDIR, MAY_CREATE);
1315         if (err)
1316                 goto out;
1317 
1318         fdentry = ffhp->fh_dentry;
1319         fdir = fdentry->d_inode;
1320 
1321         tdentry = tfhp->fh_dentry;
1322         tdir = tdentry->d_inode;
1323 
1324         err = (rqstp->rq_vers == 2) ? nfserr_acces : nfserr_xdev;
1325         if (fdir->i_sb != tdir->i_sb)
1326                 goto out;
1327 
1328         err = nfserr_perm;
1329         if (!flen || isdotent(fname, flen) || !tlen || isdotent(tname, tlen))
1330                 goto out;
1331 
1332         /* cannot use fh_lock as we need deadlock protective ordering
1333          * so do it by hand */
1334         trap = lock_rename(tdentry, fdentry);
1335         ffhp->fh_locked = tfhp->fh_locked = 1;
1336         fill_pre_wcc(ffhp);
1337         fill_pre_wcc(tfhp);
1338 
1339         odentry = lookup_one_len(fname, fdentry, flen);
1340         err = PTR_ERR(odentry);
1341         if (IS_ERR(odentry))
1342                 goto out_nfserr;
1343 
1344         err = -ENOENT;
1345         if (!odentry->d_inode)
1346                 goto out_dput_old;
1347         err = -EINVAL;
1348         if (odentry == trap)
1349                 goto out_dput_old;
1350 
1351         ndentry = lookup_one_len(tname, tdentry, tlen);
1352         err = PTR_ERR(ndentry);
1353         if (IS_ERR(ndentry))
1354                 goto out_dput_old;
1355         err = -ENOTEMPTY;
1356         if (ndentry == trap)
1357                 goto out_dput_new;
1358 
1359 #ifdef MSNFS
1360         if ((ffhp->fh_export->ex_flags & NFSEXP_MSNFS) &&
1361                 ((atomic_read(&odentry->d_count) > 1)
1362                  || (atomic_read(&ndentry->d_count) > 1))) {
1363                         err = nfserr_perm;
1364         } else
1365 #endif
1366         err = vfs_rename(fdir, odentry, tdir, ndentry);
1367         if (!err && EX_ISSYNC(tfhp->fh_export)) {
1368                 nfsd_sync_dir(tdentry);
1369                 nfsd_sync_dir(fdentry);
1370         }
1371 
1372  out_dput_new:
1373         dput(ndentry);
1374  out_dput_old:
1375         dput(odentry);
1376  out_nfserr:
1377         if (err)
1378                 err = nfserrno(err);
1379 
1380         /* we cannot reply on fh_unlock on the two filehandles,
1381          * as that would do the wrong thing if the two directories
1382          * were the same, so again we do it by hand
1383          */
1384         fill_post_wcc(ffhp);
1385         fill_post_wcc(tfhp);
1386         unlock_rename(tdentry, fdentry);
1387         ffhp->fh_locked = tfhp->fh_locked = 0;
1388 
1389 out:
1390         return err;
1391 }
1392 
1393 /*
1394  * Unlink a file or directory
1395  * N.B. After this call fhp needs an fh_put
1396  */
1397 int
1398 nfsd_unlink(struct svc_rqst *rqstp, struct svc_fh *fhp, int type,
1399                                 char *fname, int flen)
1400 {
1401         struct dentry   *dentry, *rdentry;
1402         struct inode    *dirp;
1403         int             err;
1404 
1405         err = nfserr_acces;
1406         if (!flen || isdotent(fname, flen))
1407                 goto out;
1408         err = fh_verify(rqstp, fhp, S_IFDIR, MAY_REMOVE);
1409         if (err)
1410                 goto out;
1411 
1412         fh_lock(fhp);
1413         dentry = fhp->fh_dentry;
1414         dirp = dentry->d_inode;
1415 
1416         rdentry = lookup_one_len(fname, dentry, flen);
1417         err = PTR_ERR(rdentry);
1418         if (IS_ERR(rdentry))
1419                 goto out_nfserr;
1420 
1421         if (!rdentry->d_inode) {
1422                 dput(rdentry);
1423                 err = nfserr_noent;
1424                 goto out;
1425         }
1426 
1427         if (!type)
1428                 type = rdentry->d_inode->i_mode & S_IFMT;
1429 
1430         if (type != S_IFDIR) { /* It's UNLINK */
1431 #ifdef MSNFS
1432                 if ((fhp->fh_export->ex_flags & NFSEXP_MSNFS) &&
1433                         (atomic_read(&rdentry->d_count) > 1)) {
1434                         err = nfserr_perm;
1435                 } else
1436 #endif
1437                 err = vfs_unlink(dirp, rdentry);
1438         } else { /* It's RMDIR */
1439                 err = vfs_rmdir(dirp, rdentry);
1440         }
1441 
1442         dput(rdentry);
1443 
1444         if (err)
1445                 goto out_nfserr;
1446         if (EX_ISSYNC(fhp->fh_export)) 
1447                 nfsd_sync_dir(dentry);
1448 
1449 out:
1450         return err;
1451 
1452 out_nfserr:
1453         err = nfserrno(err);
1454         goto out;
1455 }
1456 
1457 /*
1458  * Read entries from a directory.
1459  * The  NFSv3/4 verifier we ignore for now.
1460  */
1461 int
1462 nfsd_readdir(struct svc_rqst *rqstp, struct svc_fh *fhp, loff_t *offsetp, 
1463              struct readdir_cd *cdp, encode_dent_fn func)
1464 {
1465         int             err;
1466         struct file     file;
1467         loff_t          offset = *offsetp;
1468 
1469         err = nfsd_open(rqstp, fhp, S_IFDIR, MAY_READ, &file);
1470         if (err)
1471                 goto out;
1472         if (offset > ~(u32) 0)
1473                 goto out_close;
1474 
1475         file.f_pos = offset;
1476 
1477         /*
1478          * Read the directory entries. This silly loop is necessary because
1479          * readdir() is not guaranteed to fill up the entire buffer, but
1480          * may choose to do less.
1481          */
1482 
1483         do {
1484                 cdp->err = nfserr_eof; /* will be cleared on successful read */
1485                 err = vfs_readdir(&file, (filldir_t) func, cdp);
1486         } while (err >=0 && cdp->err == nfs_ok);
1487         if (err)
1488                 err = nfserrno(err);
1489         else
1490                 err = cdp->err;
1491         *offsetp = file.f_pos;
1492 
1493         if (err == nfserr_eof || err == nfserr_readdir_nospc)
1494                 err = nfs_ok; /* can still be found in ->err */
1495 out_close:
1496         nfsd_close(&file);
1497 out:
1498         return err;
1499 }
1500 
1501 /*
1502  * Get file system stats
1503  * N.B. After this call fhp needs an fh_put
1504  */
1505 int
1506 nfsd_statfs(struct svc_rqst *rqstp, struct svc_fh *fhp, struct kstatfs *stat)
1507 {
1508         int err = fh_verify(rqstp, fhp, 0, MAY_NOP);
1509         if (!err && vfs_statfs(fhp->fh_dentry->d_inode->i_sb,stat))
1510                 err = nfserr_io;
1511         return err;
1512 }
1513 
1514 /*
1515  * Check for a user's access permissions to this inode.
1516  */
1517 int
1518 nfsd_permission(struct svc_export *exp, struct dentry *dentry, int acc)
1519 {
1520         struct inode    *inode = dentry->d_inode;
1521         int             err;
1522 
1523         if (acc == MAY_NOP)
1524                 return 0;
1525 #if 0
1526         dprintk("nfsd: permission 0x%x%s%s%s%s%s%s%s mode 0%o%s%s%s\n",
1527                 acc,
1528                 (acc & MAY_READ)?       " read"  : "",
1529                 (acc & MAY_WRITE)?      " write" : "",
1530                 (acc & MAY_EXEC)?       " exec"  : "",
1531                 (acc & MAY_SATTR)?      " sattr" : "",
1532                 (acc & MAY_TRUNC)?      " trunc" : "",
1533                 (acc & MAY_LOCK)?       " lock"  : "",
1534                 (acc & MAY_OWNER_OVERRIDE)? " owneroverride" : "",
1535                 inode->i_mode,
1536                 IS_IMMUTABLE(inode)?    " immut" : "",
1537                 IS_APPEND(inode)?       " append" : "",
1538                 IS_RDONLY(inode)?       " ro" : "");
1539         dprintk("      owner %d/%d user %d/%d\n",
1540                 inode->i_uid, inode->i_gid, current->fsuid, current->fsgid);
1541 #endif
1542 
1543         /* Normally we reject any write/sattr etc access on a read-only file
1544          * system.  But if it is IRIX doing check on write-access for a 
1545          * device special file, we ignore rofs.
1546          */
1547         if (!(acc & MAY_LOCAL_ACCESS))
1548                 if (acc & (MAY_WRITE | MAY_SATTR | MAY_TRUNC)) {
1549                         if (EX_RDONLY(exp) || IS_RDONLY(inode))
1550                                 return nfserr_rofs;
1551                         if (/* (acc & MAY_WRITE) && */ IS_IMMUTABLE(inode))
1552                                 return nfserr_perm;
1553                 }
1554         if ((acc & MAY_TRUNC) && IS_APPEND(inode))
1555                 return nfserr_perm;
1556 
1557         if (acc & MAY_LOCK) {
1558                 /* If we cannot rely on authentication in NLM requests,
1559                  * just allow locks, otherwise require read permission, or
1560                  * ownership
1561                  */
1562                 if (exp->ex_flags & NFSEXP_NOAUTHNLM)
1563                         return 0;
1564                 else
1565                         acc = MAY_READ | MAY_OWNER_OVERRIDE;
1566         }
1567         /*
1568          * The file owner always gets access permission for accesses that
1569          * would normally be checked at open time. This is to make
1570          * file access work even when the client has done a fchmod(fd, 0).
1571          *
1572          * However, `cp foo bar' should fail nevertheless when bar is
1573          * readonly. A sensible way to do this might be to reject all
1574          * attempts to truncate a read-only file, because a creat() call
1575          * always implies file truncation.
1576          * ... but this isn't really fair.  A process may reasonably call
1577          * ftruncate on an open file descriptor on a file with perm 000.
1578          * We must trust the client to do permission checking - using "ACCESS"
1579          * with NFSv3.
1580          */
1581         if ((acc & MAY_OWNER_OVERRIDE) &&
1582             inode->i_uid == current->fsuid)
1583                 return 0;
1584 
1585         err = permission(inode, acc & (MAY_READ|MAY_WRITE|MAY_EXEC), NULL);
1586 
1587         /* Allow read access to binaries even when mode 111 */
1588         if (err == -EACCES && S_ISREG(inode->i_mode) &&
1589             acc == (MAY_READ | MAY_OWNER_OVERRIDE))
1590                 err = permission(inode, MAY_EXEC, NULL);
1591 
1592         return err? nfserrno(err) : 0;
1593 }
1594 
1595 void
1596 nfsd_racache_shutdown(void)
1597 {
1598         if (!raparm_cache)
1599                 return;
1600         dprintk("nfsd: freeing readahead buffers.\n");
1601         kfree(raparml);
1602         raparm_cache = raparml = NULL;
1603 }
1604 /*
1605  * Initialize readahead param cache
1606  */
1607 int
1608 nfsd_racache_init(int cache_size)
1609 {
1610         int     i;
1611 
1612         if (raparm_cache)
1613                 return 0;
1614         raparml = kmalloc(sizeof(struct raparms) * cache_size, GFP_KERNEL);
1615 
1616         if (raparml != NULL) {
1617                 dprintk("nfsd: allocating %d readahead buffers.\n",
1618                         cache_size);
1619                 memset(raparml, 0, sizeof(struct raparms) * cache_size);
1620                 for (i = 0; i < cache_size - 1; i++) {
1621                         raparml[i].p_next = raparml + i + 1;
1622                 }
1623                 raparm_cache = raparml;
1624         } else {
1625                 printk(KERN_WARNING
1626                        "nfsd: Could not allocate memory read-ahead cache.\n");
1627                 return -ENOMEM;
1628         }
1629         nfsdstats.ra_size = cache_size;
1630         return 0;
1631 }
1632 

~ [ 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