Import 2.4.0-test3pre1
[davej-history.git] / fs / nfsd / vfs.c
blob79bceeb860c2e656bd1a233d29c3ded3f0a90e77
1 /*
2 * linux/fs/nfsd/vfs.c
4 * File operations used by nfsd. Some of these have been ripped from
5 * other parts of the kernel because they weren't in ksyms.c, others
6 * are partial duplicates with added or changed functionality.
8 * Note that several functions dget() the dentry upon which they want
9 * to act, most notably those that create directory entries. Response
10 * dentry's are dput()'d if necessary in the release callback.
11 * So if you notice code paths that apparently fail to dput() the
12 * dentry, don't worry--they have been taken care of.
14 * Copyright (C) 1995-1999 Olaf Kirch <okir@monad.swb.de>
17 #include <linux/config.h>
18 #include <linux/version.h>
19 #include <linux/string.h>
20 #include <linux/sched.h>
21 #include <linux/errno.h>
22 #include <linux/locks.h>
23 #include <linux/fs.h>
24 #include <linux/major.h>
25 #include <linux/ext2_fs.h>
26 #include <linux/proc_fs.h>
27 #include <linux/stat.h>
28 #include <linux/fcntl.h>
29 #include <linux/net.h>
30 #include <linux/unistd.h>
31 #include <linux/malloc.h>
32 #include <linux/in.h>
33 #define __NO_VERSION__
34 #include <linux/module.h>
36 #include <linux/sunrpc/svc.h>
37 #include <linux/nfsd/nfsd.h>
38 #ifdef CONFIG_NFSD_V3
39 #include <linux/nfs3.h>
40 #include <linux/nfsd/xdr3.h>
41 #endif /* CONFIG_NFSD_V3 */
42 #include <linux/nfsd/nfsfh.h>
43 #include <linux/quotaops.h>
45 #include <asm/uaccess.h>
47 #define NFSDDBG_FACILITY NFSDDBG_FILEOP
48 #define NFSD_PARANOIA
51 /* We must ignore files (but only files) which might have mandatory
52 * locks on them because there is no way to know if the accesser has
53 * the lock.
55 #define IS_ISMNDLK(i) (S_ISREG((i)->i_mode) && MANDATORY_LOCK(i))
58 * This is a cache of readahead params that help us choose the proper
59 * readahead strategy. Initially, we set all readahead parameters to 0
60 * and let the VFS handle things.
61 * If you increase the number of cached files very much, you'll need to
62 * add a hash table here.
64 struct raparms {
65 struct raparms *p_next;
66 unsigned int p_count;
67 ino_t p_ino;
68 dev_t p_dev;
69 unsigned long p_reada,
70 p_ramax,
71 p_raend,
72 p_ralen,
73 p_rawin;
76 static struct raparms * raparml = NULL;
77 static struct raparms * raparm_cache = NULL;
80 * Look up one component of a pathname.
81 * N.B. After this call _both_ fhp and resfh need an fh_put
83 * If the lookup would cross a mountpoint, and the mounted filesystem
84 * is exported to the client with NFSEXP_CROSSMNT, then the lookup is
85 * accepted as it stands and the mounted directory is
86 * returned. Otherwise the covered directory is returned.
87 * NOTE: this mountpoint crossing is not supported properly by all
88 * clients and is explicitly disallowed for NFSv3
89 * NeilBrown <neilb@cse.unsw.edu.au>
91 int
92 nfsd_lookup(struct svc_rqst *rqstp, struct svc_fh *fhp, const char *name,
93 int len, struct svc_fh *resfh)
95 struct svc_export *exp;
96 struct dentry *dparent;
97 struct dentry *dentry;
98 int err;
100 dprintk("nfsd: nfsd_lookup(fh %s, %s)\n", SVCFH_fmt(fhp), name);
102 /* Obtain dentry and export. */
103 err = fh_verify(rqstp, fhp, S_IFDIR, MAY_EXEC);
104 if (err)
105 goto out;
107 dparent = fhp->fh_dentry;
108 exp = fhp->fh_export;
110 err = nfserr_acces;
112 /* Lookup the name, but don't follow links */
113 if (strcmp(name, ".")==0) {
114 dentry = dget(dparent);
115 } else if (strcmp(name, "..")==0) {
116 /* checking mountpoint crossing is very different when stepping up */
117 if (dparent == exp->ex_dentry) {
118 if (!EX_CROSSMNT(exp))
119 dentry = dget(dparent); /* .. == . just like at / */
120 else
122 struct svc_export *exp2 = NULL;
123 struct dentry *dp;
124 struct vfsmount *mnt = mntget(exp->ex_mnt);
125 dentry = dget(dparent);
126 while(follow_up(&mnt, &dentry))
128 dp = dget(dentry->d_parent);
129 dput(dentry);
130 dentry = dp;
131 for ( ; exp2 == NULL && dp->d_parent != dp;
132 dp=dp->d_parent)
133 exp2 = exp_get(exp->ex_client, dp->d_inode->i_dev, dp->d_inode->i_ino);
134 if (exp2==NULL) {
135 dput(dentry);
136 dentry = dget(dparent);
137 } else {
138 exp = exp2;
140 mntput(mnt);
142 } else
143 dentry = dget(dparent->d_parent);
144 } else {
145 dentry = lookup_one(name, dparent);
146 err = PTR_ERR(dentry);
147 if (IS_ERR(dentry))
148 goto out_nfserr;
150 * check if we have crossed a mount point ...
152 if (d_mountpoint(dentry)) {
153 struct svc_export *exp2 = NULL;
154 struct vfsmount *mnt = mntget(exp->ex_mnt);
155 struct dentry *mounts = dget(dentry);
156 while (follow_down(&mnt,&mounts)&&d_mountpoint(mounts))
158 exp2 = exp_get(rqstp->rq_client,
159 mounts->d_inode->i_dev,
160 mounts->d_inode->i_ino);
161 if (exp2 && EX_CROSSMNT(exp2)) {
162 /* successfully crossed mount point */
163 exp = exp2;
164 dput(dentry);
165 dentry = mounts;
166 } else
167 dput(mounts);
168 mntput(mnt);
172 * Note: we compose the file handle now, but as the
173 * dentry may be negative, it may need to be updated.
175 err = fh_compose(resfh, exp, dentry);
176 if (!err && !dentry->d_inode)
177 err = nfserr_noent;
178 out:
179 return err;
181 out_nfserr:
182 err = nfserrno(err);
183 goto out;
187 * Set various file attributes.
188 * N.B. After this call fhp needs an fh_put
191 nfsd_setattr(struct svc_rqst *rqstp, struct svc_fh *fhp, struct iattr *iap)
193 struct dentry *dentry;
194 struct inode *inode;
195 int accmode = MAY_SATTR;
196 int ftype = 0;
197 int imode;
198 int err;
199 kernel_cap_t saved_cap = 0;
200 int size_change = 0;
202 if (iap->ia_valid & (ATTR_ATIME | ATTR_MTIME | ATTR_SIZE))
203 accmode |= MAY_WRITE|MAY_OWNER_OVERRIDE;
204 if (iap->ia_valid & ATTR_SIZE)
205 ftype = S_IFREG;
207 /* Get inode */
208 err = fh_verify(rqstp, fhp, ftype, accmode);
209 if (err || !iap->ia_valid)
210 goto out;
212 dentry = fhp->fh_dentry;
213 inode = dentry->d_inode;
215 err = inode_change_ok(inode, iap);
216 /* could be a "touch" (utimes) request where the user is not the owner but does
217 * have write permission. In this case the user should be allowed to set
218 * both times to the current time. We could just assume any such SETATTR
219 * is intended to set the times to "now", but we do a couple of simple tests
220 * to increase our confidence.
222 #define BOTH_TIME_SET (ATTR_ATIME_SET | ATTR_MTIME_SET)
223 #define MAX_TOUCH_TIME_ERROR (30*60)
224 if (err
225 && (iap->ia_valid & BOTH_TIME_SET) == BOTH_TIME_SET
226 && iap->ia_mtime == iap->ia_ctime
228 /* looks good. now just make sure time is in the right ballpark.
229 * solaris, at least, doesn't seem to care what the time request is
231 time_t delta = iap->ia_atime - CURRENT_TIME;
232 if (delta<0) delta = -delta;
233 if (delta < MAX_TOUCH_TIME_ERROR) {
234 /* turn off ATTR_[AM]TIME_SET but leave ATTR_[AM]TIME
235 * this will cause notify_change to set these times to "now"
237 iap->ia_valid &= ~BOTH_TIME_SET;
238 err = inode_change_ok(inode, iap);
242 if (err)
243 goto out_nfserr;
245 /* The size case is special. It changes the file as well as the attributes. */
246 if (iap->ia_valid & ATTR_SIZE) {
247 if (iap->ia_size < inode->i_size) {
248 err = nfsd_permission(fhp->fh_export, dentry, MAY_TRUNC|MAY_OWNER_OVERRIDE);
249 if (err)
250 goto out;
252 err = get_write_access(inode);
253 if (err)
254 goto out_nfserr;
256 err = locks_verify_truncate(inode, NULL, iap->ia_size);
257 if (err) {
258 put_write_access(inode);
259 goto out_nfserr;
261 DQUOT_INIT(inode);
264 imode = inode->i_mode;
265 if (iap->ia_valid & ATTR_MODE) {
266 iap->ia_mode &= S_IALLUGO;
267 imode = iap->ia_mode |= (imode & ~S_IALLUGO);
270 /* Revoke setuid/setgid bit on chown/chgrp */
271 if ((iap->ia_valid & ATTR_UID) && (imode & S_ISUID)
272 && iap->ia_uid != inode->i_uid) {
273 iap->ia_valid |= ATTR_MODE;
274 iap->ia_mode = imode &= ~S_ISUID;
276 if ((iap->ia_valid & ATTR_GID) && (imode & S_ISGID)
277 && iap->ia_gid != inode->i_gid) {
278 iap->ia_valid |= ATTR_MODE;
279 iap->ia_mode = imode &= ~S_ISGID;
282 /* Change the attributes. */
285 iap->ia_valid |= ATTR_CTIME;
286 if (current->fsuid != 0) {
287 saved_cap = current->cap_effective;
288 cap_clear(current->cap_effective);
290 #ifdef CONFIG_QUOTA
291 /* DQUOT_TRANSFER needs both ia_uid and ia_gid defined */
292 if (iap->ia_valid & (ATTR_UID|ATTR_GID)) {
293 if (! (iap->ia_valid & ATTR_UID))
294 iap->ia_uid = inode->i_uid;
295 if (! (iap->ia_valid & ATTR_GID))
296 iap->ia_gid = inode->i_gid;
297 iap->ia_valid |= ATTR_UID|ATTR_GID;
299 #endif /* CONFIG_QUOTA */
301 if (iap->ia_valid & ATTR_SIZE) {
302 fh_lock(fhp);
303 size_change = 1;
305 #ifdef CONFIG_QUOTA
306 if (iap->ia_valid & (ATTR_UID|ATTR_GID))
307 err = DQUOT_TRANSFER(dentry, iap);
308 else
309 #endif
310 err = notify_change(dentry, iap);
311 if (size_change) {
312 fh_unlock(fhp);
313 put_write_access(inode);
315 if (current->fsuid != 0)
316 current->cap_effective = saved_cap;
317 if (err)
318 goto out_nfserr;
319 if (EX_ISSYNC(fhp->fh_export))
320 write_inode_now(inode);
321 err = 0;
322 out:
323 return err;
325 out_nfserr:
326 err = nfserrno(err);
327 goto out;
330 #ifdef CONFIG_NFSD_V3
332 * Check server access rights to a file system object
334 struct accessmap {
335 u32 access;
336 int how;
338 static struct accessmap nfs3_regaccess[] = {
339 { NFS3_ACCESS_READ, MAY_READ },
340 { NFS3_ACCESS_EXECUTE, MAY_EXEC },
341 { NFS3_ACCESS_MODIFY, MAY_WRITE|MAY_TRUNC },
342 { NFS3_ACCESS_EXTEND, MAY_WRITE },
344 { 0, 0 }
347 static struct accessmap nfs3_diraccess[] = {
348 { NFS3_ACCESS_READ, MAY_READ },
349 { NFS3_ACCESS_LOOKUP, MAY_EXEC },
350 { NFS3_ACCESS_MODIFY, MAY_EXEC|MAY_WRITE|MAY_TRUNC },
351 { NFS3_ACCESS_EXTEND, MAY_EXEC|MAY_WRITE },
352 { NFS3_ACCESS_DELETE, MAY_REMOVE },
354 { 0, 0 }
357 static struct accessmap nfs3_anyaccess[] = {
358 /* XXX: should we try to cover read/write here for clients that
359 * rely on us to do their access checking for special files? */
361 { 0, 0 }
365 nfsd_access(struct svc_rqst *rqstp, struct svc_fh *fhp, u32 *access)
367 struct accessmap *map;
368 struct svc_export *export;
369 struct dentry *dentry;
370 u32 query, result = 0;
371 unsigned int error;
373 error = fh_verify(rqstp, fhp, 0, MAY_NOP);
374 if (error)
375 goto out;
377 export = fhp->fh_export;
378 dentry = fhp->fh_dentry;
380 if (S_ISREG(dentry->d_inode->i_mode))
381 map = nfs3_regaccess;
382 else if (S_ISDIR(dentry->d_inode->i_mode))
383 map = nfs3_diraccess;
384 else
385 map = nfs3_anyaccess;
388 query = *access;
389 for (; map->access; map++) {
390 if (map->access & query) {
391 unsigned int err2;
392 err2 = nfsd_permission(export, dentry, map->how);
393 switch (err2) {
394 case nfs_ok:
395 result |= map->access;
396 break;
398 /* the following error codes just mean the access was not allowed,
399 * rather than an error occurred */
400 case nfserr_rofs:
401 case nfserr_acces:
402 case nfserr_perm:
403 /* simply don't "or" in the access bit. */
404 break;
405 default:
406 error = err2;
407 goto out;
411 *access = result;
413 out:
414 return error;
416 #endif /* CONFIG_NFSD_V3 */
421 * Open an existing file or directory.
422 * The access argument indicates the type of open (read/write/lock)
423 * N.B. After this call fhp needs an fh_put
426 nfsd_open(struct svc_rqst *rqstp, struct svc_fh *fhp, int type,
427 int access, struct file *filp)
429 struct dentry *dentry;
430 struct inode *inode;
431 int err;
433 /* If we get here, then the client has already done an "open", and (hopefully)
434 * checked permission - so allow OWNER_OVERRIDE in case a chmod has now revoked
435 * permission */
436 err = fh_verify(rqstp, fhp, type, access | MAY_OWNER_OVERRIDE);
437 if (err)
438 goto out;
440 dentry = fhp->fh_dentry;
441 inode = dentry->d_inode;
443 /* Disallow access to files with the append-only bit set or
444 * with mandatory locking enabled
446 err = nfserr_perm;
447 if (IS_APPEND(inode) || IS_ISMNDLK(inode))
448 goto out;
449 if (!inode->i_fop)
450 goto out;
452 if ((access & MAY_WRITE) && (err = get_write_access(inode)) != 0)
453 goto out_nfserr;
455 memset(filp, 0, sizeof(*filp));
456 filp->f_op = fops_get(inode->i_fop);
457 atomic_set(&filp->f_count, 1);
458 filp->f_dentry = dentry;
459 if (access & MAY_WRITE) {
460 filp->f_flags = O_WRONLY;
461 filp->f_mode = FMODE_WRITE;
462 DQUOT_INIT(inode);
463 } else {
464 filp->f_flags = O_RDONLY;
465 filp->f_mode = FMODE_READ;
468 err = 0;
469 if (filp->f_op && filp->f_op->open) {
470 err = filp->f_op->open(inode, filp);
471 if (err) {
472 fops_put(filp->f_op);
473 if (access & MAY_WRITE)
474 put_write_access(inode);
476 /* I nearly added put_filp() call here, but this filp
477 * is really on callers stack frame. -DaveM
479 atomic_dec(&filp->f_count);
482 out_nfserr:
483 if (err)
484 err = nfserrno(err);
485 out:
486 return err;
490 * Close a file.
492 void
493 nfsd_close(struct file *filp)
495 struct dentry *dentry = filp->f_dentry;
496 struct inode *inode = dentry->d_inode;
498 if (filp->f_op && filp->f_op->release)
499 filp->f_op->release(inode, filp);
500 fops_put(filp->f_op);
501 if (filp->f_mode & FMODE_WRITE)
502 put_write_access(inode);
506 * Sync a file
507 * As this calls fsync (not fdatasync) there is no need for a write_inode
508 * after it.
510 void
511 nfsd_sync(struct file *filp)
513 dprintk("nfsd: sync file %s\n", filp->f_dentry->d_name.name);
514 down(&filp->f_dentry->d_inode->i_sem);
515 filp->f_op->fsync(filp, filp->f_dentry);
516 up(&filp->f_dentry->d_inode->i_sem);
519 void
520 nfsd_sync_dir(struct dentry *dp)
522 struct inode *inode = dp->d_inode;
523 int (*fsync) (struct file *, struct dentry *);
525 if (inode->i_fop && (fsync = inode->i_fop->fsync)) {
526 fsync(NULL, dp);
531 * Obtain the readahead parameters for the file
532 * specified by (dev, ino).
534 static inline struct raparms *
535 nfsd_get_raparms(dev_t dev, ino_t ino)
537 struct raparms *ra, **rap, **frap = NULL;
538 int depth = 0;
540 for (rap = &raparm_cache; (ra = *rap); rap = &ra->p_next) {
541 if (ra->p_ino == ino && ra->p_dev == dev)
542 goto found;
543 depth++;
544 if (ra->p_count == 0)
545 frap = rap;
547 depth = nfsdstats.ra_size*11/10;
548 if (!frap)
549 return NULL;
550 rap = frap;
551 ra = *frap;
552 memset(ra, 0, sizeof(*ra));
553 ra->p_dev = dev;
554 ra->p_ino = ino;
555 found:
556 if (rap != &raparm_cache) {
557 *rap = ra->p_next;
558 ra->p_next = raparm_cache;
559 raparm_cache = ra;
561 ra->p_count++;
562 nfsdstats.ra_depth[depth*10/nfsdstats.ra_size]++;
563 return ra;
567 * Read data from a file. count must contain the requested read count
568 * on entry. On return, *count contains the number of bytes actually read.
569 * N.B. After this call fhp needs an fh_put
572 nfsd_read(struct svc_rqst *rqstp, struct svc_fh *fhp, loff_t offset,
573 char *buf, unsigned long *count)
575 struct raparms *ra;
576 mm_segment_t oldfs;
577 int err;
578 struct file file;
580 err = nfsd_open(rqstp, fhp, S_IFREG, MAY_READ, &file);
581 if (err)
582 goto out;
583 err = nfserr_perm;
584 if (!file.f_op->read)
585 goto out_close;
587 /* Get readahead parameters */
588 ra = nfsd_get_raparms(fhp->fh_export->ex_dev, fhp->fh_dentry->d_inode->i_ino);
589 if (ra) {
590 file.f_reada = ra->p_reada;
591 file.f_ramax = ra->p_ramax;
592 file.f_raend = ra->p_raend;
593 file.f_ralen = ra->p_ralen;
594 file.f_rawin = ra->p_rawin;
596 file.f_pos = offset;
598 oldfs = get_fs(); set_fs(KERNEL_DS);
599 err = file.f_op->read(&file, buf, *count, &file.f_pos);
600 set_fs(oldfs);
602 /* Write back readahead params */
603 if (ra != NULL) {
604 dprintk("nfsd: raparms %ld %ld %ld %ld %ld\n",
605 file.f_reada, file.f_ramax, file.f_raend,
606 file.f_ralen, file.f_rawin);
607 ra->p_reada = file.f_reada;
608 ra->p_ramax = file.f_ramax;
609 ra->p_raend = file.f_raend;
610 ra->p_ralen = file.f_ralen;
611 ra->p_rawin = file.f_rawin;
612 ra->p_count -= 1;
615 if (err >= 0) {
616 nfsdstats.io_read += err;
617 *count = err;
618 err = 0;
619 } else
620 err = nfserrno(err);
621 out_close:
622 nfsd_close(&file);
623 out:
624 return err;
628 * Write data to a file.
629 * The stable flag requests synchronous writes.
630 * N.B. After this call fhp needs an fh_put
633 nfsd_write(struct svc_rqst *rqstp, struct svc_fh *fhp, loff_t offset,
634 char *buf, unsigned long cnt, int *stablep)
636 struct svc_export *exp;
637 struct file file;
638 struct dentry *dentry;
639 struct inode *inode;
640 mm_segment_t oldfs;
641 int err = 0;
642 int stable = *stablep;
643 #ifdef CONFIG_QUOTA
644 uid_t saved_euid;
645 #endif
647 err = nfsd_open(rqstp, fhp, S_IFREG, MAY_WRITE, &file);
648 if (err)
649 goto out;
650 if (!cnt)
651 goto out_close;
652 err = nfserr_perm;
653 if (!file.f_op->write)
654 goto out_close;
656 dentry = file.f_dentry;
657 inode = dentry->d_inode;
658 exp = fhp->fh_export;
661 * Request sync writes if
662 * - the sync export option has been set, or
663 * - the client requested O_SYNC behavior (NFSv3 feature).
664 * - The file system doesn't support fsync().
665 * When gathered writes have been configured for this volume,
666 * flushing the data to disk is handled separately below.
669 if (file.f_op->fsync == 0) {/* COMMIT3 cannot work */
670 stable = 2;
671 *stablep = 2; /* FILE_SYNC */
674 if (!EX_ISSYNC(exp))
675 stable = 0;
676 if (stable && !EX_WGATHER(exp))
677 file.f_flags |= O_SYNC;
679 file.f_pos = offset; /* set write offset */
681 /* Write the data. */
682 oldfs = get_fs(); set_fs(KERNEL_DS);
683 #ifdef CONFIG_QUOTA
684 /* This is for disk quota. */
685 saved_euid = current->euid;
686 current->euid = current->fsuid;
687 err = file.f_op->write(&file, buf, cnt, &file.f_pos);
688 current->euid = saved_euid;
689 #else
690 err = file.f_op->write(&file, buf, cnt, &file.f_pos);
691 #endif
692 if (err >= 0)
693 nfsdstats.io_write += cnt;
694 set_fs(oldfs);
696 /* clear setuid/setgid flag after write */
697 if (err >= 0 && (inode->i_mode & (S_ISUID | S_ISGID))) {
698 struct iattr ia;
699 kernel_cap_t saved_cap = 0;
701 ia.ia_valid = ATTR_MODE;
702 ia.ia_mode = inode->i_mode & ~(S_ISUID | S_ISGID);
703 if (current->fsuid != 0) {
704 saved_cap = current->cap_effective;
705 cap_clear(current->cap_effective);
707 notify_change(dentry, &ia);
708 if (current->fsuid != 0)
709 current->cap_effective = saved_cap;
712 if (err >= 0 && stable) {
713 static unsigned long last_ino = 0;
714 static kdev_t last_dev = NODEV;
717 * Gathered writes: If another process is currently
718 * writing to the file, there's a high chance
719 * this is another nfsd (triggered by a bulk write
720 * from a client's biod). Rather than syncing the
721 * file with each write request, we sleep for 10 msec.
723 * I don't know if this roughly approximates
724 * C. Juszak's idea of gathered writes, but it's a
725 * nice and simple solution (IMHO), and it seems to
726 * work:-)
728 if (EX_WGATHER(exp) && (atomic_read(&inode->i_writecount) > 1
729 || (last_ino == inode->i_ino && last_dev == inode->i_dev))) {
730 #if 0
731 interruptible_sleep_on_timeout(&inode->i_wait, 10 * HZ / 1000);
732 #else
733 dprintk("nfsd: write defer %d\n", current->pid);
734 /* FIXME: Olaf commented this out [gam3] */
735 set_current_state(TASK_UNINTERRUPTIBLE);
736 schedule_timeout((HZ+99)/100);
737 current->state = TASK_RUNNING;
738 dprintk("nfsd: write resume %d\n", current->pid);
739 #endif
742 if (inode->i_state & I_DIRTY) {
743 dprintk("nfsd: write sync %d\n", current->pid);
744 nfsd_sync(&file);
746 #if 0
747 wake_up(&inode->i_wait);
748 #endif
749 last_ino = inode->i_ino;
750 last_dev = inode->i_dev;
753 dprintk("nfsd: write complete err=%d\n", err);
754 if (err >= 0)
755 err = 0;
756 else
757 err = nfserrno(err);
758 out_close:
759 nfsd_close(&file);
760 out:
761 return err;
765 #ifdef CONFIG_NFSD_V3
767 * Commit all pending writes to stable storage.
768 * Strictly speaking, we could sync just the indicated file region here,
769 * but there's currently no way we can ask the VFS to do so.
771 * Unfortunately we cannot lock the file to make sure we return full WCC
772 * data to the client, as locking happens lower down in the filesystem.
775 nfsd_commit(struct svc_rqst *rqstp, struct svc_fh *fhp,
776 off_t offset, unsigned long count)
778 struct file file;
779 int err;
781 if ((err = nfsd_open(rqstp, fhp, S_IFREG, MAY_WRITE, &file)) != 0)
782 return err;
783 if (EX_ISSYNC(fhp->fh_export)) {
784 if (file.f_op && file.f_op->fsync) {
785 nfsd_sync(&file);
786 } else {
787 err = nfserr_notsupp;
791 nfsd_close(&file);
792 return err;
794 #endif /* CONFIG_NFSD_V3 */
797 * Create a file (regular, directory, device, fifo); UNIX sockets
798 * not yet implemented.
799 * If the response fh has been verified, the parent directory should
800 * already be locked. Note that the parent directory is left locked.
802 * N.B. Every call to nfsd_create needs an fh_put for _both_ fhp and resfhp
805 nfsd_create(struct svc_rqst *rqstp, struct svc_fh *fhp,
806 char *fname, int flen, struct iattr *iap,
807 int type, dev_t rdev, struct svc_fh *resfhp)
809 struct dentry *dentry, *dchild;
810 struct inode *dirp;
811 int err;
813 err = nfserr_perm;
814 if (!flen)
815 goto out;
816 err = nfserr_exist;
817 if (isdotent(fname, flen))
818 goto out;
820 err = fh_verify(rqstp, fhp, S_IFDIR, MAY_CREATE);
821 if (err)
822 goto out;
824 dentry = fhp->fh_dentry;
825 dirp = dentry->d_inode;
827 err = nfserr_notdir;
828 if(!dirp->i_op || !dirp->i_op->lookup)
829 goto out;
831 * Check whether the response file handle has been verified yet.
832 * If it has, the parent directory should already be locked.
834 if (!resfhp->fh_dentry) {
835 /* called from nfsd_proc_mkdir, or possibly nfsd3_proc_create */
836 fh_lock(fhp);
837 dchild = lookup_one(fname, dentry);
838 err = PTR_ERR(dchild);
839 if (IS_ERR(dchild))
840 goto out_nfserr;
841 err = fh_compose(resfhp, fhp->fh_export, dchild);
842 if (err)
843 goto out;
844 } else {
845 /* called from nfsd_proc_create */
846 dchild = resfhp->fh_dentry;
847 if (!fhp->fh_locked) {
848 /* not actually possible */
849 printk(KERN_ERR
850 "nfsd_create: parent %s/%s not locked!\n",
851 dentry->d_parent->d_name.name,
852 dentry->d_name.name);
853 err = -EIO;
854 goto out;
858 * Make sure the child dentry is still negative ...
860 err = nfserr_exist;
861 if (dchild->d_inode) {
862 dprintk("nfsd_create: dentry %s/%s not negative!\n",
863 dentry->d_name.name, dchild->d_name.name);
864 goto out;
867 if (!(iap->ia_valid & ATTR_MODE))
868 iap->ia_mode = 0;
869 iap->ia_mode = (iap->ia_mode & S_IALLUGO) | type;
872 * Get the dir op function pointer.
874 err = nfserr_perm;
875 switch (type) {
876 case S_IFREG:
877 err = vfs_create(dirp, dchild, iap->ia_mode);
878 break;
879 case S_IFDIR:
880 err = vfs_mkdir(dirp, dchild, iap->ia_mode);
881 break;
882 case S_IFCHR:
883 case S_IFBLK:
884 case S_IFIFO:
885 case S_IFSOCK:
886 err = vfs_mknod(dirp, dchild, iap->ia_mode, rdev);
887 break;
888 default:
889 printk("nfsd: bad file type %o in nfsd_create\n", type);
890 err = -EINVAL;
892 if (err < 0)
893 goto out_nfserr;
895 if (EX_ISSYNC(fhp->fh_export)) {
896 nfsd_sync_dir(dentry);
897 write_inode_now(dchild->d_inode);
901 /* Set file attributes. Mode has already been set and
902 * setting uid/gid works only for root. Irix appears to
903 * send along the gid when it tries to implement setgid
904 * directories via NFS.
906 err = 0;
907 if ((iap->ia_valid &= ~(ATTR_UID|ATTR_GID|ATTR_MODE)) != 0)
908 err = nfsd_setattr(rqstp, resfhp, iap);
910 * Update the file handle to get the new inode info.
912 if (!err)
913 err = fh_update(resfhp);
914 out:
915 return err;
917 out_nfserr:
918 err = nfserrno(err);
919 goto out;
922 #ifdef CONFIG_NFSD_V3
924 * NFSv3 version of nfsd_create
927 nfsd_create_v3(struct svc_rqst *rqstp, struct svc_fh *fhp,
928 char *fname, int flen, struct iattr *iap,
929 struct svc_fh *resfhp, int createmode, u32 *verifier)
931 struct dentry *dentry, *dchild;
932 struct inode *dirp;
933 int err;
934 __u32 v_mtime=0, v_atime=0;
935 int v_mode=0;
937 err = nfserr_perm;
938 if (!flen)
939 goto out;
940 err = nfserr_exist;
941 if (isdotent(fname, flen))
942 goto out;
943 if (!(iap->ia_valid & ATTR_MODE))
944 iap->ia_mode = 0;
945 err = fh_verify(rqstp, fhp, S_IFDIR, MAY_CREATE);
946 if (err)
947 goto out;
949 dentry = fhp->fh_dentry;
950 dirp = dentry->d_inode;
952 /* Get all the sanity checks out of the way before
953 * we lock the parent. */
954 err = nfserr_notdir;
955 if(!dirp->i_op || !dirp->i_op->lookup)
956 goto out;
957 fh_lock(fhp);
960 * Compose the response file handle.
962 dchild = lookup_one(fname, dentry);
963 err = PTR_ERR(dchild);
964 if (IS_ERR(dchild))
965 goto out_nfserr;
967 err = fh_compose(resfhp, fhp->fh_export, dchild);
968 if (err)
969 goto out;
971 if (createmode == NFS3_CREATE_EXCLUSIVE) {
972 /* while the verifier would fit in mtime+atime,
973 * solaris7 gets confused (bugid 4218508) if these have
974 * the high bit set, so we use the mode as well
976 v_mtime = verifier[0]&0x7fffffff;
977 v_atime = verifier[1]&0x7fffffff;
978 v_mode = S_IFREG
979 | ((verifier[0]&0x80000000) >> (32-7)) /* u+x */
980 | ((verifier[1]&0x80000000) >> (32-9)) /* u+r */
984 if (dchild->d_inode) {
985 err = 0;
987 switch (createmode) {
988 case NFS3_CREATE_UNCHECKED:
989 if (! S_ISREG(dchild->d_inode->i_mode))
990 err = nfserr_exist;
991 else {
992 iap->ia_valid &= ATTR_SIZE;
993 goto set_attr;
995 break;
996 case NFS3_CREATE_EXCLUSIVE:
997 if ( dchild->d_inode->i_mtime == v_mtime
998 && dchild->d_inode->i_atime == v_atime
999 && dchild->d_inode->i_mode == v_mode
1000 && dchild->d_inode->i_size == 0 )
1001 break;
1002 /* fallthru */
1003 case NFS3_CREATE_GUARDED:
1004 err = nfserr_exist;
1006 goto out;
1009 err = vfs_create(dirp, dchild, iap->ia_mode);
1010 if (err < 0)
1011 goto out_nfserr;
1013 if (EX_ISSYNC(fhp->fh_export)) {
1014 nfsd_sync_dir(dentry);
1015 /* setattr will sync the child (or not) */
1019 * Update the filehandle to get the new inode info.
1021 err = fh_update(resfhp);
1022 if (err)
1023 goto out;
1025 if (createmode == NFS3_CREATE_EXCLUSIVE) {
1026 /* Cram the verifier into atime/mtime/mode */
1027 iap->ia_valid = ATTR_MTIME|ATTR_ATIME
1028 | ATTR_MTIME_SET|ATTR_ATIME_SET
1029 | ATTR_MODE;
1030 iap->ia_mtime = v_mtime;
1031 iap->ia_atime = v_atime;
1032 iap->ia_mode = v_mode;
1035 /* Set file attributes.
1036 * Mode has already been set but we might need to reset it
1037 * for CREATE_EXCLUSIVE
1038 * Irix appears to send along the gid when it tries to
1039 * implement setgid directories via NFS. Clear out all that cruft.
1041 set_attr:
1042 if ((iap->ia_valid &= ~(ATTR_UID|ATTR_GID)) != 0)
1043 err = nfsd_setattr(rqstp, resfhp, iap);
1045 out:
1046 fh_unlock(fhp);
1047 return err;
1049 out_nfserr:
1050 err = nfserrno(err);
1051 goto out;
1053 #endif /* CONFIG_NFSD_V3 */
1056 * Read a symlink. On entry, *lenp must contain the maximum path length that
1057 * fits into the buffer. On return, it contains the true length.
1058 * N.B. After this call fhp needs an fh_put
1061 nfsd_readlink(struct svc_rqst *rqstp, struct svc_fh *fhp, char *buf, int *lenp)
1063 struct dentry *dentry;
1064 struct inode *inode;
1065 mm_segment_t oldfs;
1066 int err;
1068 err = fh_verify(rqstp, fhp, S_IFLNK, MAY_NOP);
1069 if (err)
1070 goto out;
1072 dentry = fhp->fh_dentry;
1073 inode = dentry->d_inode;
1075 err = nfserr_inval;
1076 if (!inode->i_op || !inode->i_op->readlink)
1077 goto out;
1079 UPDATE_ATIME(inode);
1080 /* N.B. Why does this call need a get_fs()??
1081 * Remove the set_fs and watch the fireworks:-) --okir
1084 oldfs = get_fs(); set_fs(KERNEL_DS);
1085 err = inode->i_op->readlink(dentry, buf, *lenp);
1086 set_fs(oldfs);
1088 if (err < 0)
1089 goto out_nfserr;
1090 *lenp = err;
1091 err = 0;
1092 out:
1093 return err;
1095 out_nfserr:
1096 err = nfserrno(err);
1097 goto out;
1101 * Create a symlink and look up its inode
1102 * N.B. After this call _both_ fhp and resfhp need an fh_put
1105 nfsd_symlink(struct svc_rqst *rqstp, struct svc_fh *fhp,
1106 char *fname, int flen,
1107 char *path, int plen,
1108 struct svc_fh *resfhp,
1109 struct iattr *iap)
1111 struct dentry *dentry, *dnew;
1112 int err, cerr;
1114 err = nfserr_noent;
1115 if (!flen || !plen)
1116 goto out;
1117 err = nfserr_exist;
1118 if (isdotent(fname, flen))
1119 goto out;
1121 err = fh_verify(rqstp, fhp, S_IFDIR, MAY_CREATE);
1122 if (err)
1123 goto out;
1124 fh_lock(fhp);
1125 dentry = fhp->fh_dentry;
1126 dnew = lookup_one(fname, dentry);
1127 err = PTR_ERR(dnew);
1128 if (IS_ERR(dnew))
1129 goto out_nfserr;
1131 err = vfs_symlink(dentry->d_inode, dnew, path);
1132 if (!err) {
1133 if (EX_ISSYNC(fhp->fh_export))
1134 nfsd_sync_dir(dentry);
1135 if (iap) {
1136 iap->ia_valid &= ATTR_MODE /* ~(ATTR_MODE|ATTR_UID|ATTR_GID)*/;
1137 if (iap->ia_valid) {
1138 iap->ia_valid |= ATTR_CTIME;
1139 iap->ia_mode = (iap->ia_mode&S_IALLUGO)
1140 | S_IFLNK;
1141 err = notify_change(dnew, iap);
1142 if (!err && EX_ISSYNC(fhp->fh_export))
1143 write_inode_now(dentry->d_inode);
1146 } else
1147 err = nfserrno(err);
1148 fh_unlock(fhp);
1150 /* Compose the fh so the dentry will be freed ... */
1151 cerr = fh_compose(resfhp, fhp->fh_export, dnew);
1152 if (err==0) err = cerr;
1153 out:
1154 return err;
1156 out_nfserr:
1157 err = nfserrno(err);
1158 goto out;
1162 * Create a hardlink
1163 * N.B. After this call _both_ ffhp and tfhp need an fh_put
1166 nfsd_link(struct svc_rqst *rqstp, struct svc_fh *ffhp,
1167 char *fname, int len, struct svc_fh *tfhp)
1169 struct dentry *ddir, *dnew, *dold;
1170 struct inode *dirp, *dest;
1171 int err;
1173 err = fh_verify(rqstp, ffhp, S_IFDIR, MAY_CREATE);
1174 if (err)
1175 goto out;
1176 err = fh_verify(rqstp, tfhp, -S_IFDIR, MAY_NOP);
1177 if (err)
1178 goto out;
1180 err = nfserr_perm;
1181 if (!len)
1182 goto out;
1183 err = nfserr_exist;
1184 if (isdotent(fname, len))
1185 goto out;
1187 fh_lock(ffhp);
1188 ddir = ffhp->fh_dentry;
1189 dirp = ddir->d_inode;
1191 dnew = lookup_one(fname, ddir);
1192 err = PTR_ERR(dnew);
1193 if (IS_ERR(dnew))
1194 goto out_nfserr;
1196 dold = tfhp->fh_dentry;
1197 dest = dold->d_inode;
1199 err = vfs_link(dold, dirp, dnew);
1200 if (!err) {
1201 if (EX_ISSYNC(ffhp->fh_export)) {
1202 nfsd_sync_dir(ddir);
1203 write_inode_now(dest);
1205 } else {
1206 if (err == -EXDEV && rqstp->rq_vers == 2)
1207 err = nfserr_acces;
1208 else
1209 err = nfserrno(err);
1212 fh_unlock(ffhp);
1213 dput(dnew);
1214 out:
1215 return err;
1217 out_nfserr:
1218 err = nfserrno(err);
1219 goto out;
1223 * Rename a file
1224 * N.B. After this call _both_ ffhp and tfhp need an fh_put
1227 nfsd_rename(struct svc_rqst *rqstp, struct svc_fh *ffhp, char *fname, int flen,
1228 struct svc_fh *tfhp, char *tname, int tlen)
1230 struct dentry *fdentry, *tdentry, *odentry, *ndentry;
1231 struct inode *fdir, *tdir;
1232 int err;
1234 err = fh_verify(rqstp, ffhp, S_IFDIR, MAY_REMOVE);
1235 if (err)
1236 goto out;
1237 err = fh_verify(rqstp, tfhp, S_IFDIR, MAY_CREATE);
1238 if (err)
1239 goto out;
1241 fdentry = ffhp->fh_dentry;
1242 fdir = fdentry->d_inode;
1244 tdentry = tfhp->fh_dentry;
1245 tdir = tdentry->d_inode;
1247 err = (rqstp->rq_vers == 2) ? nfserr_acces : nfserr_xdev;
1248 if (fdir->i_dev != tdir->i_dev)
1249 goto out;
1251 err = nfserr_perm;
1252 if (!flen || isdotent(fname, flen) || !tlen || isdotent(tname, tlen))
1253 goto out;
1255 /* cannot use fh_lock as we need deadlock protective ordering
1256 * so do it by hand */
1257 double_down(&tdir->i_sem, &fdir->i_sem);
1258 ffhp->fh_locked = tfhp->fh_locked = 1;
1259 fill_pre_wcc(ffhp);
1260 fill_pre_wcc(tfhp);
1262 odentry = lookup_one(fname, fdentry);
1263 err = PTR_ERR(odentry);
1264 if (IS_ERR(odentry))
1265 goto out_nfserr;
1267 err = -ENOENT;
1268 if (!odentry->d_inode)
1269 goto out_dput_old;
1271 ndentry = lookup_one(tname, tdentry);
1272 err = PTR_ERR(ndentry);
1273 if (IS_ERR(ndentry))
1274 goto out_dput_old;
1277 err = vfs_rename(fdir, odentry, tdir, ndentry);
1278 if (!err && EX_ISSYNC(tfhp->fh_export)) {
1279 nfsd_sync_dir(tdentry);
1280 nfsd_sync_dir(fdentry);
1282 dput(ndentry);
1284 out_dput_old:
1285 dput(odentry);
1286 out_nfserr:
1287 if (err)
1288 err = nfserrno(err);
1290 /* we cannot reply on fh_unlock on the two filehandles,
1291 * as that would do the wrong thing if the two directories
1292 * were the same, so again we do it by hand
1294 fill_post_wcc(ffhp);
1295 fill_post_wcc(tfhp);
1296 double_up(&tdir->i_sem, &fdir->i_sem);
1297 ffhp->fh_locked = tfhp->fh_locked = 0;
1299 out:
1300 return err;
1304 * Unlink a file or directory
1305 * N.B. After this call fhp needs an fh_put
1308 nfsd_unlink(struct svc_rqst *rqstp, struct svc_fh *fhp, int type,
1309 char *fname, int flen)
1311 struct dentry *dentry, *rdentry;
1312 struct inode *dirp;
1313 int err;
1315 err = nfserr_acces;
1316 if (!flen || isdotent(fname, flen))
1317 goto out;
1318 err = fh_verify(rqstp, fhp, S_IFDIR, MAY_REMOVE);
1319 if (err)
1320 goto out;
1322 fh_lock(fhp);
1323 dentry = fhp->fh_dentry;
1324 dirp = dentry->d_inode;
1326 rdentry = lookup_one(fname, dentry);
1327 err = PTR_ERR(rdentry);
1328 if (IS_ERR(rdentry))
1329 goto out_nfserr;
1331 if (!rdentry->d_inode) {
1332 dput(rdentry);
1333 err = nfserr_noent;
1334 goto out;
1337 if (type != S_IFDIR) { /* It's UNLINK */
1338 err = vfs_unlink(dirp, rdentry);
1339 } else { /* It's RMDIR */
1340 err = vfs_rmdir(dirp, rdentry);
1343 dput(rdentry);
1345 if (err)
1346 goto out_nfserr;
1347 if (EX_ISSYNC(fhp->fh_export))
1348 nfsd_sync_dir(dentry);
1350 out:
1351 return err;
1353 out_nfserr:
1354 err = nfserrno(err);
1355 goto out;
1359 * Read entries from a directory.
1360 * The verifier is an NFSv3 thing we ignore for now.
1363 nfsd_readdir(struct svc_rqst *rqstp, struct svc_fh *fhp, loff_t offset,
1364 encode_dent_fn func, u32 *buffer, int *countp, u32 *verf)
1366 struct inode *inode;
1367 u32 *p;
1368 int oldlen, eof, err;
1369 struct file file;
1370 struct readdir_cd cd;
1372 err = nfsd_open(rqstp, fhp, S_IFDIR, MAY_READ, &file);
1373 if (err)
1374 goto out;
1375 if (offset > ~(u32) 0)
1376 goto out_close;
1378 err = nfserr_notdir;
1379 if (!file.f_op->readdir)
1380 goto out_close;
1381 file.f_pos = offset;
1383 /* Set up the readdir context */
1384 memset(&cd, 0, sizeof(cd));
1385 cd.rqstp = rqstp;
1386 cd.buffer = buffer;
1387 cd.buflen = *countp; /* count of words */
1388 cd.dirfh = fhp;
1391 * Read the directory entries. This silly loop is necessary because
1392 * readdir() is not guaranteed to fill up the entire buffer, but
1393 * may choose to do less.
1395 inode = file.f_dentry->d_inode;
1396 down(&inode->i_sem);
1397 while (1) {
1398 oldlen = cd.buflen;
1401 dprintk("nfsd: f_op->readdir(%x/%ld @ %d) buflen = %d (%d)\n",
1402 file.f_inode->i_dev, file.f_inode->i_ino,
1403 (int) file.f_pos, (int) oldlen, (int) cd.buflen);
1405 err = file.f_op->readdir(&file, &cd, (filldir_t) func);
1406 if (err < 0)
1407 goto out_nfserr;
1408 if (oldlen == cd.buflen)
1409 break;
1410 if (cd.eob)
1411 break;
1413 up(&inode->i_sem);
1415 /* If we didn't fill the buffer completely, we're at EOF */
1416 eof = !cd.eob;
1418 if (cd.offset) {
1419 if (rqstp->rq_vers == 3)
1420 (void)xdr_encode_hyper(cd.offset, file.f_pos);
1421 else
1422 *cd.offset = htonl(file.f_pos);
1425 p = cd.buffer;
1426 *p++ = 0; /* no more entries */
1427 *p++ = htonl(eof); /* end of directory */
1428 *countp = (caddr_t) p - (caddr_t) buffer;
1430 dprintk("nfsd: readdir result %d bytes, eof %d offset %d\n",
1431 *countp, eof,
1432 cd.offset? ntohl(*cd.offset) : -1);
1433 err = 0;
1434 out_close:
1435 nfsd_close(&file);
1436 out:
1437 return err;
1439 out_nfserr:
1440 up(&inode->i_sem);
1441 err = nfserrno(err);
1442 goto out_close;
1446 * Get file system stats
1447 * N.B. After this call fhp needs an fh_put
1450 nfsd_statfs(struct svc_rqst *rqstp, struct svc_fh *fhp, struct statfs *stat)
1452 int err = fh_verify(rqstp, fhp, 0, MAY_NOP);
1453 if (!err && vfs_statfs(fhp->fh_dentry->d_inode->i_sb,stat))
1454 err = nfserr_io;
1455 return err;
1459 * Check for a user's access permissions to this inode.
1462 nfsd_permission(struct svc_export *exp, struct dentry *dentry, int acc)
1464 struct inode *inode = dentry->d_inode;
1465 int err;
1466 kernel_cap_t saved_cap = 0;
1468 if (acc == MAY_NOP)
1469 return 0;
1470 #if 0
1471 dprintk("nfsd: permission 0x%x%s%s%s%s%s%s%s mode 0%o%s%s%s\n",
1472 acc,
1473 (acc & MAY_READ)? " read" : "",
1474 (acc & MAY_WRITE)? " write" : "",
1475 (acc & MAY_EXEC)? " exec" : "",
1476 (acc & MAY_SATTR)? " sattr" : "",
1477 (acc & MAY_TRUNC)? " trunc" : "",
1478 (acc & MAY_LOCK)? " lock" : "",
1479 (acc & MAY_OWNER_OVERRIDE)? " owneroverride" : "",
1480 inode->i_mode,
1481 IS_IMMUTABLE(inode)? " immut" : "",
1482 IS_APPEND(inode)? " append" : "",
1483 IS_RDONLY(inode)? " ro" : "");
1484 dprintk(" owner %d/%d user %d/%d\n",
1485 inode->i_uid, inode->i_gid, current->fsuid, current->fsgid);
1486 #endif
1488 if (acc & (MAY_WRITE | MAY_SATTR | MAY_TRUNC)) {
1489 if (EX_RDONLY(exp) || IS_RDONLY(inode))
1490 return nfserr_rofs;
1491 if (/* (acc & MAY_WRITE) && */ IS_IMMUTABLE(inode))
1492 return nfserr_perm;
1494 if ((acc & MAY_TRUNC) && IS_APPEND(inode))
1495 return nfserr_perm;
1497 if (acc & MAY_LOCK) {
1498 /* If we cannot rely on authentication in NLM requests,
1499 * just allow locks, otherwise require read permission, or
1500 * ownership
1502 if (exp->ex_flags & NFSEXP_NOAUTHNLM)
1503 return 0;
1504 else
1505 acc = MAY_READ | MAY_OWNER_OVERRIDE;
1508 * The file owner always gets access permission for accesses that
1509 * would normally be checked at open time. This is to make
1510 * file access work even when the client has done a fchmod(fd, 0).
1512 * However, `cp foo bar' should fail nevertheless when bar is
1513 * readonly. A sensible way to do this might be to reject all
1514 * attempts to truncate a read-only file, because a creat() call
1515 * always implies file truncation.
1516 * ... but this isn't really fair. A process may reasonably call
1517 * ftruncate on an open file descriptor on a file with perm 000.
1518 * We must trust the client to do permission checking - using "ACCESS"
1519 * with NFSv3.
1521 if ((acc & MAY_OWNER_OVERRIDE) &&
1522 inode->i_uid == current->fsuid)
1523 return 0;
1525 if (current->fsuid != 0) {
1526 saved_cap = current->cap_effective;
1527 cap_clear(current->cap_effective);
1530 err = permission(inode, acc & (MAY_READ|MAY_WRITE|MAY_EXEC));
1532 /* Allow read access to binaries even when mode 111 */
1533 if (err == -EACCES && S_ISREG(inode->i_mode) && acc == MAY_READ)
1534 err = permission(inode, MAY_EXEC);
1536 if (current->fsuid != 0)
1537 current->cap_effective = saved_cap;
1539 return err? nfserrno(err) : 0;
1542 void
1543 nfsd_racache_shutdown(void)
1545 if (!raparm_cache)
1546 return;
1547 dprintk("nfsd: freeing readahead buffers.\n");
1548 kfree(raparml);
1549 raparm_cache = raparml = NULL;
1552 * Initialize readahead param cache
1555 nfsd_racache_init(int cache_size)
1557 int i;
1559 if (raparm_cache)
1560 return 0;
1561 raparml = kmalloc(sizeof(struct raparms) * cache_size, GFP_KERNEL);
1563 if (raparml != NULL) {
1564 dprintk("nfsd: allocating %d readahead buffers.\n",
1565 cache_size);
1566 memset(raparml, 0, sizeof(struct raparms) * cache_size);
1567 for (i = 0; i < cache_size - 1; i++) {
1568 raparml[i].p_next = raparml + i + 1;
1570 raparm_cache = raparml;
1571 } else {
1572 printk(KERN_WARNING
1573 "nfsd: Could not allocate memory read-ahead cache.\n");
1574 return -ENOMEM;
1576 nfsdstats.ra_size = cache_size;
1577 return 0;