* better
[mascara-docs.git] / i386 / linux-2.3.21 / fs / nfsd / nfsproc.c
blob7c3e24817b94eb088c432fa76431ef4ca6de82b1
1 /*
2 * nfsproc2.c Process version 2 NFS requests.
4 * Copyright (C) 1995 Olaf Kirch <okir@monad.swb.de>
5 */
7 #include <linux/linkage.h>
8 #include <linux/sched.h>
9 #include <linux/errno.h>
10 #include <linux/locks.h>
11 #include <linux/fs.h>
12 #include <linux/stat.h>
13 #include <linux/fcntl.h>
14 #include <linux/net.h>
15 #include <linux/in.h>
16 #include <linux/version.h>
17 #include <linux/unistd.h>
18 #include <linux/malloc.h>
20 #include <linux/sunrpc/svc.h>
21 #include <linux/nfsd/nfsd.h>
22 #include <linux/nfsd/cache.h>
23 #include <linux/nfsd/xdr.h>
25 typedef struct svc_rqst svc_rqst;
26 typedef struct svc_buf svc_buf;
28 #define NFSDDBG_FACILITY NFSDDBG_PROC
30 #define RETURN(st) return st
32 static void
33 svcbuf_reserve(struct svc_buf *buf, u32 **ptr, int *len, int nr)
35 *ptr = buf->buf + nr;
36 *len = buf->buflen - buf->len - nr;
39 static int
40 nfsd_proc_null(struct svc_rqst *rqstp, void *argp, void *resp)
42 RETURN(nfs_ok);
46 * Get a file's attributes
47 * N.B. After this call resp->fh needs an fh_put
49 static int
50 nfsd_proc_getattr(struct svc_rqst *rqstp, struct nfsd_fhandle *argp,
51 struct nfsd_attrstat *resp)
53 dprintk("nfsd: GETATTR %d/%d\n",
54 SVCFH_DEV(&argp->fh), SVCFH_INO(&argp->fh));
56 fh_copy(&resp->fh, &argp->fh);
57 RETURN(fh_verify(rqstp, &resp->fh, 0, MAY_NOP));
61 * Set a file's attributes
62 * N.B. After this call resp->fh needs an fh_put
64 static int
65 nfsd_proc_setattr(struct svc_rqst *rqstp, struct nfsd_sattrargs *argp,
66 struct nfsd_attrstat *resp)
68 dprintk("nfsd: SETATTR %d/%d, valid=%x, size=%ld\n",
69 SVCFH_DEV(&argp->fh), SVCFH_INO(&argp->fh),
70 argp->attrs.ia_valid, (long) argp->attrs.ia_size);
72 fh_copy(&resp->fh, &argp->fh);
73 RETURN(nfsd_setattr(rqstp, &resp->fh, &argp->attrs));
77 * Look up a path name component
78 * Note: the dentry in the resp->fh may be negative if the file
79 * doesn't exist yet.
80 * N.B. After this call resp->fh needs an fh_put
82 static int
83 nfsd_proc_lookup(struct svc_rqst *rqstp, struct nfsd_diropargs *argp,
84 struct nfsd_diropres *resp)
86 int nfserr;
88 dprintk("nfsd: LOOKUP %d/%d %s\n",
89 SVCFH_DEV(&argp->fh), SVCFH_INO(&argp->fh), argp->name);
91 nfserr = nfsd_lookup(rqstp, &argp->fh, argp->name, argp->len,
92 &resp->fh);
94 fh_put(&argp->fh);
95 RETURN(nfserr);
99 * Read a symlink.
101 static int
102 nfsd_proc_readlink(struct svc_rqst *rqstp, struct nfsd_fhandle *argp,
103 struct nfsd_readlinkres *resp)
105 u32 *path;
106 int dummy, nfserr;
108 dprintk("nfsd: READLINK %p\n", SVCFH_DENTRY(&argp->fh));
110 /* Reserve room for status and path length */
111 svcbuf_reserve(&rqstp->rq_resbuf, &path, &dummy, 2);
113 /* Read the symlink. */
114 resp->len = NFS_MAXPATHLEN;
115 nfserr = nfsd_readlink(rqstp, &argp->fh, (char *) path, &resp->len);
117 fh_put(&argp->fh);
118 RETURN(nfserr);
122 * Read a portion of a file.
123 * N.B. After this call resp->fh needs an fh_put
125 static int
126 nfsd_proc_read(struct svc_rqst *rqstp, struct nfsd_readargs *argp,
127 struct nfsd_readres *resp)
129 u32 * buffer;
130 int nfserr, avail;
132 dprintk("nfsd: READ %d/%d %d bytes at %d\n",
133 SVCFH_DEV(&argp->fh), SVCFH_INO(&argp->fh),
134 argp->count, argp->offset);
136 /* Obtain buffer pointer for payload. 19 is 1 word for
137 * status, 17 words for fattr, and 1 word for the byte count.
139 svcbuf_reserve(&rqstp->rq_resbuf, &buffer, &avail, 19);
141 if ((avail << 2) < argp->count) {
142 printk(KERN_NOTICE
143 "oversized read request from %08lx:%d (%d bytes)\n",
144 ntohl(rqstp->rq_addr.sin_addr.s_addr),
145 ntohs(rqstp->rq_addr.sin_port),
146 argp->count);
147 argp->count = avail;
150 resp->count = argp->count;
151 nfserr = nfsd_read(rqstp, fh_copy(&resp->fh, &argp->fh),
152 argp->offset,
153 (char *) buffer,
154 &resp->count);
156 RETURN(nfserr);
160 * Write data to a file
161 * N.B. After this call resp->fh needs an fh_put
163 static int
164 nfsd_proc_write(struct svc_rqst *rqstp, struct nfsd_writeargs *argp,
165 struct nfsd_attrstat *resp)
167 int nfserr;
169 dprintk("nfsd: WRITE %d/%d %d bytes at %d\n",
170 SVCFH_DEV(&argp->fh), SVCFH_INO(&argp->fh),
171 argp->len, argp->offset);
173 nfserr = nfsd_write(rqstp, fh_copy(&resp->fh, &argp->fh),
174 argp->offset,
175 argp->data,
176 argp->len,
178 RETURN(nfserr);
182 * CREATE processing is complicated. The keyword here is `overloaded.'
183 * The parent directory is kept locked between the check for existence
184 * and the actual create() call in compliance with VFS protocols.
185 * N.B. After this call _both_ argp->fh and resp->fh need an fh_put
187 static int
188 nfsd_proc_create(struct svc_rqst *rqstp, struct nfsd_createargs *argp,
189 struct nfsd_diropres *resp)
191 svc_fh *dirfhp = &argp->fh;
192 svc_fh *newfhp = &resp->fh;
193 struct iattr *attr = &argp->attrs;
194 struct inode *inode;
195 int nfserr, type, mode, rdonly = 0;
196 dev_t rdev = NODEV;
198 dprintk("nfsd: CREATE %d/%d %s\n",
199 SVCFH_DEV(dirfhp), SVCFH_INO(dirfhp), argp->name);
201 /* First verify the parent file handle */
202 nfserr = fh_verify(rqstp, dirfhp, S_IFDIR, MAY_EXEC);
203 if (nfserr)
204 goto done; /* must fh_put dirfhp even on error */
206 /* Check for MAY_WRITE separately. */
207 nfserr = nfsd_permission(dirfhp->fh_export, dirfhp->fh_dentry,
208 MAY_WRITE);
209 if (nfserr == nfserr_rofs) {
210 rdonly = 1; /* Non-fatal error for echo > /dev/null */
211 } else if (nfserr)
212 goto done;
215 * Do a lookup to verify the new file handle.
217 nfserr = nfsd_lookup(rqstp, dirfhp, argp->name, argp->len, newfhp);
218 if (nfserr) {
219 if (nfserr != nfserr_noent)
220 goto done;
222 * If the new file handle wasn't verified, we can't tell
223 * whether the file exists or not. Time to bail ...
225 nfserr = nfserr_acces;
226 if (!newfhp->fh_dverified) {
227 printk(KERN_WARNING
228 "nfsd_proc_create: file handle not verified\n");
229 goto done;
234 * Lock the parent directory and check for existence.
236 nfserr = fh_lock_parent(dirfhp, newfhp->fh_dentry);
237 if (nfserr)
238 goto done;
239 inode = newfhp->fh_dentry->d_inode;
241 /* Unfudge the mode bits */
242 if (attr->ia_valid & ATTR_MODE) {
243 type = attr->ia_mode & S_IFMT;
244 mode = attr->ia_mode & ~S_IFMT;
245 if (!type) /* HP weirdness */
246 type = S_IFREG;
247 } else if (inode) {
248 type = inode->i_mode & S_IFMT;
249 mode = inode->i_mode & ~S_IFMT;
250 } else {
251 type = S_IFREG;
252 mode = 0; /* ??? */
255 /* This is for "echo > /dev/null" a la SunOS. Argh. */
256 nfserr = nfserr_rofs;
257 if (rdonly && (!inode || type == S_IFREG))
258 goto out_unlock;
260 attr->ia_valid |= ATTR_MODE;
261 attr->ia_mode = type | mode;
263 /* Special treatment for non-regular files according to the
264 * gospel of sun micro
266 if (type != S_IFREG) {
267 int is_borc = 0;
268 u32 size = attr->ia_size;
270 rdev = (dev_t) size;
271 if (type != S_IFBLK && type != S_IFCHR) {
272 rdev = 0;
273 } else if (type == S_IFCHR && size == ~(u32) 0) {
274 /* If you think you've seen the worst, grok this. */
275 attr->ia_mode = S_IFIFO | mode;
276 type = S_IFIFO;
277 } else if (size != rdev) {
278 /* dev got truncated because of 16bit Linux dev_t */
279 nfserr = nfserr_io; /* or nfserr_inval? */
280 goto out_unlock;
281 } else {
282 /* Okay, char or block special */
283 is_borc = 1;
286 /* Make sure the type and device matches */
287 nfserr = nfserr_exist;
288 if (inode && (type != (inode->i_mode & S_IFMT) ||
289 (is_borc && inode->i_rdev != rdev)))
290 goto out_unlock;
293 nfserr = 0;
294 if (!inode) {
295 /* File doesn't exist. Create it and set attrs */
296 nfserr = nfsd_create(rqstp, dirfhp, argp->name, argp->len,
297 attr, type, rdev, newfhp);
298 } else if (type == S_IFREG) {
299 dprintk("nfsd: existing %s, valid=%x, size=%ld\n",
300 argp->name, attr->ia_valid, (long) attr->ia_size);
301 /* File already exists. We ignore all attributes except
302 * size, so that creat() behaves exactly like
303 * open(..., O_CREAT|O_TRUNC|O_WRONLY).
305 attr->ia_valid &= ATTR_SIZE;
306 if (attr->ia_valid)
307 nfserr = nfsd_setattr(rqstp, newfhp, attr);
310 out_unlock:
311 /* We don't really need to unlock, as fh_put does it. */
312 fh_unlock(dirfhp);
314 done:
315 fh_put(dirfhp);
316 RETURN(nfserr);
319 static int
320 nfsd_proc_remove(struct svc_rqst *rqstp, struct nfsd_diropargs *argp,
321 void *resp)
323 int nfserr;
325 dprintk("nfsd: REMOVE %p %s\n", SVCFH_DENTRY(&argp->fh), argp->name);
327 /* Unlink. -SIFDIR means file must not be a directory */
328 nfserr = nfsd_unlink(rqstp, &argp->fh, -S_IFDIR, argp->name, argp->len);
329 fh_put(&argp->fh);
330 RETURN(nfserr);
333 static int
334 nfsd_proc_rename(struct svc_rqst *rqstp, struct nfsd_renameargs *argp,
335 void *resp)
337 int nfserr;
339 dprintk("nfsd: RENAME %p %s -> %p %s\n",
340 SVCFH_DENTRY(&argp->ffh), argp->fname,
341 SVCFH_DENTRY(&argp->tfh), argp->tname);
343 nfserr = nfsd_rename(rqstp, &argp->ffh, argp->fname, argp->flen,
344 &argp->tfh, argp->tname, argp->tlen);
345 fh_put(&argp->ffh);
346 fh_put(&argp->tfh);
347 RETURN(nfserr);
350 static int
351 nfsd_proc_link(struct svc_rqst *rqstp, struct nfsd_linkargs *argp,
352 void *resp)
354 int nfserr;
356 dprintk("nfsd: LINK %p -> %p %s\n",
357 SVCFH_DENTRY(&argp->ffh),
358 SVCFH_DENTRY(&argp->tfh),
359 argp->tname);
361 nfserr = nfsd_link(rqstp, &argp->tfh, argp->tname, argp->tlen,
362 &argp->ffh);
363 fh_put(&argp->ffh);
364 fh_put(&argp->tfh);
365 RETURN(nfserr);
368 static int
369 nfsd_proc_symlink(struct svc_rqst *rqstp, struct nfsd_symlinkargs *argp,
370 void *resp)
372 struct svc_fh newfh;
373 int nfserr;
375 dprintk("nfsd: SYMLINK %p %s -> %s\n",
376 SVCFH_DENTRY(&argp->ffh), argp->fname, argp->tname);
378 memset(&newfh, 0, sizeof(struct svc_fh));
380 * Create the link, look up new file and set attrs.
382 nfserr = nfsd_symlink(rqstp, &argp->ffh, argp->fname, argp->flen,
383 argp->tname, argp->tlen,
384 &newfh);
385 if (!nfserr) {
386 argp->attrs.ia_valid &= ~ATTR_SIZE;
387 nfserr = nfsd_setattr(rqstp, &newfh, &argp->attrs);
390 fh_put(&argp->ffh);
391 fh_put(&newfh);
392 RETURN(nfserr);
396 * Make directory. This operation is not idempotent.
397 * N.B. After this call resp->fh needs an fh_put
399 static int
400 nfsd_proc_mkdir(struct svc_rqst *rqstp, struct nfsd_createargs *argp,
401 struct nfsd_diropres *resp)
403 int nfserr;
405 dprintk("nfsd: MKDIR %p %s\n", SVCFH_DENTRY(&argp->fh), argp->name);
407 if (resp->fh.fh_dverified) {
408 printk(KERN_WARNING
409 "nfsd_proc_mkdir: response already verified??\n");
412 argp->attrs.ia_valid &= ~ATTR_SIZE;
413 nfserr = nfsd_create(rqstp, &argp->fh, argp->name, argp->len,
414 &argp->attrs, S_IFDIR, 0, &resp->fh);
415 fh_put(&argp->fh);
416 RETURN(nfserr);
420 * Remove a directory
422 static int
423 nfsd_proc_rmdir(struct svc_rqst *rqstp, struct nfsd_diropargs *argp,
424 void *resp)
426 int nfserr;
428 dprintk("nfsd: RMDIR %p %s\n", SVCFH_DENTRY(&argp->fh), argp->name);
430 nfserr = nfsd_unlink(rqstp, &argp->fh, S_IFDIR, argp->name, argp->len);
431 fh_put(&argp->fh);
432 RETURN(nfserr);
436 * Read a portion of a directory.
438 static int
439 nfsd_proc_readdir(struct svc_rqst *rqstp, struct nfsd_readdirargs *argp,
440 struct nfsd_readdirres *resp)
442 u32 * buffer;
443 int nfserr, count;
445 dprintk("nfsd: READDIR %d/%d %d bytes at %d\n",
446 SVCFH_DEV(&argp->fh), SVCFH_INO(&argp->fh),
447 argp->count, argp->cookie);
449 /* Reserve buffer space for status */
450 svcbuf_reserve(&rqstp->rq_resbuf, &buffer, &count, 1);
452 /* Shrink to the client read size */
453 if (count > (argp->count >> 2))
454 count = argp->count >> 2;
456 /* Make sure we've room for the NULL ptr & eof flag */
457 count -= 2;
458 if (count < 0)
459 count = 0;
461 /* Read directory and encode entries on the fly */
462 nfserr = nfsd_readdir(rqstp, &argp->fh, (loff_t) argp->cookie,
463 nfssvc_encode_entry, buffer, &count);
464 resp->count = count;
466 fh_put(&argp->fh);
467 RETURN(nfserr);
471 * Get file system info
473 static int
474 nfsd_proc_statfs(struct svc_rqst * rqstp, struct nfsd_fhandle *argp,
475 struct nfsd_statfsres *resp)
477 int nfserr;
479 dprintk("nfsd: STATFS %p\n", SVCFH_DENTRY(&argp->fh));
481 nfserr = nfsd_statfs(rqstp, &argp->fh, &resp->stats);
482 fh_put(&argp->fh);
483 RETURN(nfserr);
487 * NFSv2 Server procedures.
488 * Only the results of non-idempotent operations are cached.
490 #define nfsd_proc_none NULL
491 #define nfssvc_release_none NULL
492 struct nfsd_void { int dummy; };
494 #define PROC(name, argt, rest, relt, cache) \
495 { (svc_procfunc) nfsd_proc_##name, \
496 (kxdrproc_t) nfssvc_decode_##argt, \
497 (kxdrproc_t) nfssvc_encode_##rest, \
498 (kxdrproc_t) nfssvc_release_##relt, \
499 sizeof(struct nfsd_##argt), \
500 sizeof(struct nfsd_##rest), \
501 0, \
502 cache \
504 struct svc_procedure nfsd_procedures2[18] = {
505 PROC(null, void, void, none, RC_NOCACHE),
506 PROC(getattr, fhandle, attrstat, fhandle, RC_NOCACHE),
507 PROC(setattr, sattrargs, attrstat, fhandle, RC_REPLBUFF),
508 PROC(none, void, void, none, RC_NOCACHE),
509 PROC(lookup, diropargs, diropres, fhandle, RC_NOCACHE),
510 PROC(readlink, fhandle, readlinkres, none, RC_NOCACHE),
511 PROC(read, readargs, readres, fhandle, RC_NOCACHE),
512 PROC(none, void, void, none, RC_NOCACHE),
513 PROC(write, writeargs, attrstat, fhandle, RC_REPLBUFF),
514 PROC(create, createargs, diropres, fhandle, RC_REPLBUFF),
515 PROC(remove, diropargs, void, none, RC_REPLSTAT),
516 PROC(rename, renameargs, void, none, RC_REPLSTAT),
517 PROC(link, linkargs, void, none, RC_REPLSTAT),
518 PROC(symlink, symlinkargs, void, none, RC_REPLSTAT),
519 PROC(mkdir, createargs, diropres, fhandle, RC_REPLBUFF),
520 PROC(rmdir, diropargs, void, none, RC_REPLSTAT),
521 PROC(readdir, readdirargs, readdirres, none, RC_REPLBUFF),
522 PROC(statfs, fhandle, statfsres, none, RC_NOCACHE),
527 * Map errnos to NFS errnos.
530 nfserrno (int errno)
532 static struct {
533 int nfserr;
534 int syserr;
535 } nfs_errtbl[] = {
536 { NFS_OK, 0 },
537 { NFSERR_PERM, EPERM },
538 { NFSERR_NOENT, ENOENT },
539 { NFSERR_IO, EIO },
540 { NFSERR_NXIO, ENXIO },
541 { NFSERR_ACCES, EACCES },
542 { NFSERR_EXIST, EEXIST },
543 { NFSERR_NODEV, ENODEV },
544 { NFSERR_NOTDIR, ENOTDIR },
545 { NFSERR_ISDIR, EISDIR },
546 { NFSERR_INVAL, EINVAL },
547 { NFSERR_FBIG, EFBIG },
548 { NFSERR_NOSPC, ENOSPC },
549 { NFSERR_ROFS, EROFS },
550 { NFSERR_NAMETOOLONG, ENAMETOOLONG },
551 { NFSERR_NOTEMPTY, ENOTEMPTY },
552 #ifdef EDQUOT
553 { NFSERR_DQUOT, EDQUOT },
554 #endif
555 { NFSERR_STALE, ESTALE },
556 { NFSERR_WFLUSH, EIO },
557 { -1, EIO }
559 int i;
561 for (i = 0; nfs_errtbl[i].nfserr != -1; i++) {
562 if (nfs_errtbl[i].syserr == errno)
563 return htonl (nfs_errtbl[i].nfserr);
565 printk (KERN_INFO "nfsd: non-standard errno: %d\n", errno);
566 return nfserr_io;
569 #if 0
570 static void
571 nfsd_dump(char *tag, u32 *buf, int len)
573 int i;
575 printk(KERN_NOTICE
576 "nfsd: %s (%d words)\n", tag, len);
578 for (i = 0; i < len && i < 32; i += 8)
579 printk(KERN_NOTICE
580 " %08lx %08lx %08lx %08lx"
581 " %08lx %08lx %08lx %08lx\n",
582 buf[i], buf[i+1], buf[i+2], buf[i+3],
583 buf[i+4], buf[i+5], buf[i+6], buf[i+7]);
585 #endif