2 * nfsproc2.c Process version 2 NFS requests.
3 * linux/fs/nfsd/nfs2proc.c
5 * Process version 2 NFS requests.
7 * Copyright (C) 1995-1997 Olaf Kirch <okir@monad.swb.de>
10 #include <linux/linkage.h>
11 #include <linux/sched.h>
12 #include <linux/errno.h>
13 #include <linux/locks.h>
15 #include <linux/stat.h>
16 #include <linux/fcntl.h>
17 #include <linux/net.h>
19 #include <linux/version.h>
20 #include <linux/unistd.h>
21 #include <linux/slab.h>
23 #include <linux/sunrpc/svc.h>
24 #include <linux/nfsd/nfsd.h>
25 #include <linux/nfsd/cache.h>
26 #include <linux/nfsd/xdr.h>
28 typedef struct svc_rqst svc_rqst;
29 typedef struct svc_buf svc_buf;
31 #define NFSDDBG_FACILITY NFSDDBG_PROC
35 svcbuf_reserve(struct svc_buf *buf, u32 **ptr, int *len, int nr)
38 *len = buf->buflen - buf->len - nr;
42 nfsd_proc_null(struct svc_rqst *rqstp, void *argp, void *resp)
48 * Get a file's attributes
49 * N.B. After this call resp->fh needs an fh_put
52 nfsd_proc_getattr(struct svc_rqst *rqstp, struct nfsd_fhandle *argp,
53 struct nfsd_attrstat *resp)
55 dprintk("nfsd: GETATTR %s\n", SVCFH_fmt(&argp->fh));
57 fh_copy(&resp->fh, &argp->fh);
58 return fh_verify(rqstp, &resp->fh, 0, MAY_NOP);
62 * Set a file's attributes
63 * N.B. After this call resp->fh needs an fh_put
66 nfsd_proc_setattr(struct svc_rqst *rqstp, struct nfsd_sattrargs *argp,
67 struct nfsd_attrstat *resp)
69 dprintk("nfsd: SETATTR %s, valid=%x, size=%ld\n",
71 argp->attrs.ia_valid, (long) argp->attrs.ia_size);
73 fh_copy(&resp->fh, &argp->fh);
74 return nfsd_setattr(rqstp, &resp->fh, &argp->attrs,0, (time_t)0);
78 * Look up a path name component
79 * Note: the dentry in the resp->fh may be negative if the file
81 * N.B. After this call resp->fh needs an fh_put
84 nfsd_proc_lookup(struct svc_rqst *rqstp, struct nfsd_diropargs *argp,
85 struct nfsd_diropres *resp)
89 dprintk("nfsd: LOOKUP %s %.*s\n",
90 SVCFH_fmt(&argp->fh), argp->len, argp->name);
92 fh_init(&resp->fh, NFS_FHSIZE);
93 nfserr = nfsd_lookup(rqstp, &argp->fh, argp->name, argp->len,
104 nfsd_proc_readlink(struct svc_rqst *rqstp, struct nfsd_fhandle *argp,
105 struct nfsd_readlinkres *resp)
110 dprintk("nfsd: READLINK %s\n", SVCFH_fmt(&argp->fh));
112 /* Reserve room for status and path length */
113 svcbuf_reserve(&rqstp->rq_resbuf, &path, &dummy, 2);
115 /* Read the symlink. */
116 resp->len = NFS_MAXPATHLEN;
117 nfserr = nfsd_readlink(rqstp, &argp->fh, (char *) path, &resp->len);
124 * Read a portion of a file.
125 * N.B. After this call resp->fh needs an fh_put
128 nfsd_proc_read(struct svc_rqst *rqstp, struct nfsd_readargs *argp,
129 struct nfsd_readres *resp)
134 dprintk("nfsd: READ %s %d bytes at %d\n",
135 SVCFH_fmt(&argp->fh),
136 argp->count, argp->offset);
138 /* Obtain buffer pointer for payload. 19 is 1 word for
139 * status, 17 words for fattr, and 1 word for the byte count.
141 svcbuf_reserve(&rqstp->rq_resbuf, &buffer, &avail, 19);
143 if ((avail << 2) < argp->count) {
145 "oversized read request from %08x:%d (%d bytes)\n",
146 ntohl(rqstp->rq_addr.sin_addr.s_addr),
147 ntohs(rqstp->rq_addr.sin_port),
149 argp->count = avail << 2;
151 svc_reserve(rqstp, (19<<2) + argp->count + 4);
153 resp->count = argp->count;
154 nfserr = nfsd_read(rqstp, fh_copy(&resp->fh, &argp->fh),
163 * Write data to a file
164 * N.B. After this call resp->fh needs an fh_put
167 nfsd_proc_write(struct svc_rqst *rqstp, struct nfsd_writeargs *argp,
168 struct nfsd_attrstat *resp)
173 dprintk("nfsd: WRITE %s %d bytes at %d\n",
174 SVCFH_fmt(&argp->fh),
175 argp->len, argp->offset);
177 nfserr = nfsd_write(rqstp, fh_copy(&resp->fh, &argp->fh),
186 * CREATE processing is complicated. The keyword here is `overloaded.'
187 * The parent directory is kept locked between the check for existence
188 * and the actual create() call in compliance with VFS protocols.
189 * N.B. After this call _both_ argp->fh and resp->fh need an fh_put
192 nfsd_proc_create(struct svc_rqst *rqstp, struct nfsd_createargs *argp,
193 struct nfsd_diropres *resp)
195 svc_fh *dirfhp = &argp->fh;
196 svc_fh *newfhp = &resp->fh;
197 struct iattr *attr = &argp->attrs;
199 struct dentry *dchild;
200 int nfserr, type, mode;
203 dprintk("nfsd: CREATE %s %.*s\n",
204 SVCFH_fmt(dirfhp), argp->len, argp->name);
206 /* First verify the parent file handle */
207 nfserr = fh_verify(rqstp, dirfhp, S_IFDIR, MAY_EXEC);
209 goto done; /* must fh_put dirfhp even on error */
211 /* Check for MAY_WRITE in nfsd_create if necessary */
213 nfserr = nfserr_acces;
216 nfserr = nfserr_exist;
217 if (isdotent(argp->name, argp->len))
220 dchild = lookup_one_len(argp->name, dirfhp->fh_dentry, argp->len);
221 if (IS_ERR(dchild)) {
222 nfserr = nfserrno(PTR_ERR(dchild));
225 fh_init(newfhp, NFS_FHSIZE);
226 nfserr = fh_compose(newfhp, dirfhp->fh_export, dchild, dirfhp);
227 if (!nfserr && !dchild->d_inode)
228 nfserr = nfserr_noent;
230 if (nfserr != nfserr_noent)
233 * If the new file handle wasn't verified, we can't tell
234 * whether the file exists or not. Time to bail ...
236 nfserr = nfserr_acces;
237 if (!newfhp->fh_dentry) {
239 "nfsd_proc_create: file handle not verified\n");
244 inode = newfhp->fh_dentry->d_inode;
246 /* Unfudge the mode bits */
247 if (attr->ia_valid & ATTR_MODE) {
248 type = attr->ia_mode & S_IFMT;
249 mode = attr->ia_mode & ~S_IFMT;
251 /* no type, so if target exists, assume same as that,
252 * else assume a file */
254 type = inode->i_mode & S_IFMT;
258 /* reserve rdev for later checking */
259 attr->ia_size = inode->i_rdev;
260 attr->ia_valid |= ATTR_SIZE;
264 /* this is probably a permission check..
265 * at least IRIX implements perm checking on
266 * echo thing > device-special-file-or-pipe
267 * by doing a CREATE with type==0
269 nfserr = nfsd_permission(newfhp->fh_export,
271 MAY_WRITE|_NFSD_IRIX_BOGOSITY);
272 if (nfserr && nfserr != nfserr_rofs)
279 type = inode->i_mode & S_IFMT;
280 mode = inode->i_mode & ~S_IFMT;
286 attr->ia_valid |= ATTR_MODE;
287 attr->ia_mode = mode;
289 /* Special treatment for non-regular files according to the
290 * gospel of sun micro
292 if (type != S_IFREG) {
294 u32 size = attr->ia_size;
297 if (type != S_IFBLK && type != S_IFCHR) {
299 } else if (type == S_IFCHR && !(attr->ia_valid & ATTR_SIZE)) {
300 /* If you think you've seen the worst, grok this. */
302 } else if (size != rdev) {
303 /* dev got truncated because of 16bit Linux dev_t */
304 nfserr = nfserr_inval;
307 /* Okay, char or block special */
311 /* we've used the SIZE information, so discard it */
312 attr->ia_valid &= ~ATTR_SIZE;
314 /* Make sure the type and device matches */
315 nfserr = nfserr_exist;
316 if (inode && (type != (inode->i_mode & S_IFMT) ||
317 (is_borc && inode->i_rdev != rdev)))
323 /* File doesn't exist. Create it and set attrs */
324 nfserr = nfsd_create(rqstp, dirfhp, argp->name, argp->len,
325 attr, type, rdev, newfhp);
326 } else if (type == S_IFREG) {
327 dprintk("nfsd: existing %s, valid=%x, size=%ld\n",
328 argp->name, attr->ia_valid, (long) attr->ia_size);
329 /* File already exists. We ignore all attributes except
330 * size, so that creat() behaves exactly like
331 * open(..., O_CREAT|O_TRUNC|O_WRONLY).
333 attr->ia_valid &= ATTR_SIZE;
335 nfserr = nfsd_setattr(rqstp, newfhp, attr, 0, (time_t)0);
339 /* We don't really need to unlock, as fh_put does it. */
348 nfsd_proc_remove(struct svc_rqst *rqstp, struct nfsd_diropargs *argp,
353 dprintk("nfsd: REMOVE %s %.*s\n", SVCFH_fmt(&argp->fh),
354 argp->len, argp->name);
356 /* Unlink. -SIFDIR means file must not be a directory */
357 nfserr = nfsd_unlink(rqstp, &argp->fh, -S_IFDIR, argp->name, argp->len);
363 nfsd_proc_rename(struct svc_rqst *rqstp, struct nfsd_renameargs *argp,
368 dprintk("nfsd: RENAME %s %.*s -> \n",
369 SVCFH_fmt(&argp->ffh), argp->flen, argp->fname);
370 dprintk("nfsd: -> %s %.*s\n",
371 SVCFH_fmt(&argp->tfh), argp->tlen, argp->tname);
373 nfserr = nfsd_rename(rqstp, &argp->ffh, argp->fname, argp->flen,
374 &argp->tfh, argp->tname, argp->tlen);
381 nfsd_proc_link(struct svc_rqst *rqstp, struct nfsd_linkargs *argp,
386 dprintk("nfsd: LINK %s ->\n",
387 SVCFH_fmt(&argp->ffh));
388 dprintk("nfsd: %s %.*s\n",
389 SVCFH_fmt(&argp->tfh),
393 nfserr = nfsd_link(rqstp, &argp->tfh, argp->tname, argp->tlen,
401 nfsd_proc_symlink(struct svc_rqst *rqstp, struct nfsd_symlinkargs *argp,
407 dprintk("nfsd: SYMLINK %s %.*s -> %.*s\n",
408 SVCFH_fmt(&argp->ffh), argp->flen, argp->fname,
409 argp->tlen, argp->tname);
411 fh_init(&newfh, NFS_FHSIZE);
413 * Create the link, look up new file and set attrs.
415 nfserr = nfsd_symlink(rqstp, &argp->ffh, argp->fname, argp->flen,
416 argp->tname, argp->tlen,
417 &newfh, &argp->attrs);
426 * Make directory. This operation is not idempotent.
427 * N.B. After this call resp->fh needs an fh_put
430 nfsd_proc_mkdir(struct svc_rqst *rqstp, struct nfsd_createargs *argp,
431 struct nfsd_diropres *resp)
435 dprintk("nfsd: MKDIR %s %.*s\n", SVCFH_fmt(&argp->fh), argp->len, argp->name);
437 if (resp->fh.fh_dentry) {
439 "nfsd_proc_mkdir: response already verified??\n");
442 argp->attrs.ia_valid &= ~ATTR_SIZE;
443 fh_init(&resp->fh, NFS_FHSIZE);
444 nfserr = nfsd_create(rqstp, &argp->fh, argp->name, argp->len,
445 &argp->attrs, S_IFDIR, 0, &resp->fh);
454 nfsd_proc_rmdir(struct svc_rqst *rqstp, struct nfsd_diropargs *argp,
459 dprintk("nfsd: RMDIR %s %.*s\n", SVCFH_fmt(&argp->fh), argp->len, argp->name);
461 nfserr = nfsd_unlink(rqstp, &argp->fh, S_IFDIR, argp->name, argp->len);
467 * Read a portion of a directory.
470 nfsd_proc_readdir(struct svc_rqst *rqstp, struct nfsd_readdirargs *argp,
471 struct nfsd_readdirres *resp)
476 dprintk("nfsd: READDIR %s %d bytes at %d\n",
477 SVCFH_fmt(&argp->fh),
478 argp->count, argp->cookie);
480 /* Reserve buffer space for status */
481 svcbuf_reserve(&rqstp->rq_resbuf, &buffer, &count, 1);
483 /* Shrink to the client read size */
484 if (count > (argp->count >> 2))
485 count = argp->count >> 2;
487 /* Make sure we've room for the NULL ptr & eof flag */
492 /* Read directory and encode entries on the fly */
493 nfserr = nfsd_readdir(rqstp, &argp->fh, (loff_t) argp->cookie,
495 buffer, &count, NULL);
503 * Get file system info
506 nfsd_proc_statfs(struct svc_rqst * rqstp, struct nfsd_fhandle *argp,
507 struct nfsd_statfsres *resp)
511 dprintk("nfsd: STATFS %s\n", SVCFH_fmt(&argp->fh));
513 nfserr = nfsd_statfs(rqstp, &argp->fh, &resp->stats);
519 * NFSv2 Server procedures.
520 * Only the results of non-idempotent operations are cached.
522 #define nfsd_proc_none NULL
523 #define nfssvc_release_none NULL
524 struct nfsd_void { int dummy; };
526 #define PROC(name, argt, rest, relt, cache, respsize) \
527 { (svc_procfunc) nfsd_proc_##name, \
528 (kxdrproc_t) nfssvc_decode_##argt, \
529 (kxdrproc_t) nfssvc_encode_##rest, \
530 (kxdrproc_t) nfssvc_release_##relt, \
531 sizeof(struct nfsd_##argt), \
532 sizeof(struct nfsd_##rest), \
538 #define ST 1 /* status */
539 #define FH 8 /* filehandle */
540 #define AT 18 /* attributes */
542 struct svc_procedure nfsd_procedures2[18] = {
543 PROC(null, void, void, none, RC_NOCACHE, ST),
544 PROC(getattr, fhandle, attrstat, fhandle, RC_NOCACHE, ST+AT),
545 PROC(setattr, sattrargs, attrstat, fhandle, RC_REPLBUFF, ST+AT),
546 PROC(none, void, void, none, RC_NOCACHE, ST),
547 PROC(lookup, diropargs, diropres, fhandle, RC_NOCACHE, ST+FH+AT),
548 PROC(readlink, fhandle, readlinkres, none, RC_NOCACHE, ST+1+NFS_MAXPATHLEN/4),
549 PROC(read, readargs, readres, fhandle, RC_NOCACHE, ST+AT+1+NFSSVC_MAXBLKSIZE),
550 PROC(none, void, void, none, RC_NOCACHE, ST),
551 PROC(write, writeargs, attrstat, fhandle, RC_REPLBUFF, ST+AT),
552 PROC(create, createargs, diropres, fhandle, RC_REPLBUFF, ST+FH+AT),
553 PROC(remove, diropargs, void, none, RC_REPLSTAT, ST),
554 PROC(rename, renameargs, void, none, RC_REPLSTAT, ST),
555 PROC(link, linkargs, void, none, RC_REPLSTAT, ST),
556 PROC(symlink, symlinkargs, void, none, RC_REPLSTAT, ST),
557 PROC(mkdir, createargs, diropres, fhandle, RC_REPLBUFF, ST+FH+AT),
558 PROC(rmdir, diropargs, void, none, RC_REPLSTAT, ST),
559 PROC(readdir, readdirargs, readdirres, none, RC_REPLBUFF, 0),
560 PROC(statfs, fhandle, statfsres, none, RC_NOCACHE, ST+5),
565 * Map errnos to NFS errnos.
575 { nfserr_perm, -EPERM },
576 { nfserr_noent, -ENOENT },
578 { nfserr_nxio, -ENXIO },
579 { nfserr_acces, -EACCES },
580 { nfserr_exist, -EEXIST },
581 { nfserr_xdev, -EXDEV },
582 { nfserr_mlink, -EMLINK },
583 { nfserr_nodev, -ENODEV },
584 { nfserr_notdir, -ENOTDIR },
585 { nfserr_isdir, -EISDIR },
586 { nfserr_inval, -EINVAL },
587 { nfserr_fbig, -EFBIG },
588 { nfserr_nospc, -ENOSPC },
589 { nfserr_rofs, -EROFS },
590 { nfserr_mlink, -EMLINK },
591 { nfserr_nametoolong, -ENAMETOOLONG },
592 { nfserr_notempty, -ENOTEMPTY },
594 { nfserr_dquot, -EDQUOT },
596 { nfserr_stale, -ESTALE },
597 { nfserr_dropit, -ENOMEM },
602 for (i = 0; nfs_errtbl[i].nfserr != -1; i++) {
603 if (nfs_errtbl[i].syserr == errno)
604 return nfs_errtbl[i].nfserr;
606 printk (KERN_INFO "nfsd: non-standard errno: %d\n", errno);