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