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/time.h>
12 #include <linux/errno.h>
14 #include <linux/stat.h>
15 #include <linux/fcntl.h>
16 #include <linux/net.h>
18 #include <linux/namei.h>
19 #include <linux/unistd.h>
20 #include <linux/slab.h>
22 #include <linux/sunrpc/svc.h>
23 #include <linux/nfsd/nfsd.h>
24 #include <linux/nfsd/cache.h>
25 #include <linux/nfsd/xdr.h>
27 typedef struct svc_rqst svc_rqst;
28 typedef struct svc_buf svc_buf;
30 #define NFSDDBG_FACILITY NFSDDBG_PROC
34 nfsd_proc_null(struct svc_rqst *rqstp, void *argp, void *resp)
40 nfsd_return_attrs(int err, struct nfsd_attrstat *resp)
43 return nfserrno(vfs_getattr(resp->fh.fh_export->ex_mnt,
48 nfsd_return_dirop(int err, struct nfsd_diropres *resp)
51 return nfserrno(vfs_getattr(resp->fh.fh_export->ex_mnt,
56 * Get a file's attributes
57 * N.B. After this call resp->fh needs an fh_put
60 nfsd_proc_getattr(struct svc_rqst *rqstp, struct nfsd_fhandle *argp,
61 struct nfsd_attrstat *resp)
64 dprintk("nfsd: GETATTR %s\n", SVCFH_fmt(&argp->fh));
66 fh_copy(&resp->fh, &argp->fh);
67 nfserr = fh_verify(rqstp, &resp->fh, 0, MAY_NOP);
68 return nfsd_return_attrs(nfserr, resp);
72 * Set a file's attributes
73 * N.B. After this call resp->fh needs an fh_put
76 nfsd_proc_setattr(struct svc_rqst *rqstp, struct nfsd_sattrargs *argp,
77 struct nfsd_attrstat *resp)
80 dprintk("nfsd: SETATTR %s, valid=%x, size=%ld\n",
82 argp->attrs.ia_valid, (long) argp->attrs.ia_size);
84 fh_copy(&resp->fh, &argp->fh);
85 nfserr = nfsd_setattr(rqstp, &resp->fh, &argp->attrs,0, (time_t)0);
86 return nfsd_return_attrs(nfserr, resp);
90 * Look up a path name component
91 * Note: the dentry in the resp->fh may be negative if the file
93 * N.B. After this call resp->fh needs an fh_put
96 nfsd_proc_lookup(struct svc_rqst *rqstp, struct nfsd_diropargs *argp,
97 struct nfsd_diropres *resp)
101 dprintk("nfsd: LOOKUP %s %.*s\n",
102 SVCFH_fmt(&argp->fh), argp->len, argp->name);
104 fh_init(&resp->fh, NFS_FHSIZE);
105 nfserr = nfsd_lookup(rqstp, &argp->fh, argp->name, argp->len,
109 return nfsd_return_dirop(nfserr, resp);
116 nfsd_proc_readlink(struct svc_rqst *rqstp, struct nfsd_readlinkargs *argp,
117 struct nfsd_readlinkres *resp)
121 dprintk("nfsd: READLINK %s\n", SVCFH_fmt(&argp->fh));
123 /* Read the symlink. */
124 resp->len = NFS_MAXPATHLEN;
125 nfserr = nfsd_readlink(rqstp, &argp->fh, argp->buffer, &resp->len);
132 * Read a portion of a file.
133 * N.B. After this call resp->fh needs an fh_put
136 nfsd_proc_read(struct svc_rqst *rqstp, struct nfsd_readargs *argp,
137 struct nfsd_readres *resp)
141 dprintk("nfsd: READ %s %d bytes at %d\n",
142 SVCFH_fmt(&argp->fh),
143 argp->count, argp->offset);
145 /* Obtain buffer pointer for payload. 19 is 1 word for
146 * status, 17 words for fattr, and 1 word for the byte count.
149 if (NFSSVC_MAXBLKSIZE < argp->count) {
151 "oversized read request from %u.%u.%u.%u:%d (%d bytes)\n",
152 NIPQUAD(rqstp->rq_addr.sin_addr.s_addr),
153 ntohs(rqstp->rq_addr.sin_port),
155 argp->count = NFSSVC_MAXBLKSIZE;
157 svc_reserve(rqstp, (19<<2) + argp->count + 4);
159 resp->count = argp->count;
160 nfserr = nfsd_read(rqstp, fh_copy(&resp->fh, &argp->fh), NULL,
162 argp->vec, argp->vlen,
165 if (nfserr) return nfserr;
166 return nfserrno(vfs_getattr(resp->fh.fh_export->ex_mnt,
172 * Write data to a file
173 * N.B. After this call resp->fh needs an fh_put
176 nfsd_proc_write(struct svc_rqst *rqstp, struct nfsd_writeargs *argp,
177 struct nfsd_attrstat *resp)
182 dprintk("nfsd: WRITE %s %d bytes at %d\n",
183 SVCFH_fmt(&argp->fh),
184 argp->len, argp->offset);
186 nfserr = nfsd_write(rqstp, fh_copy(&resp->fh, &argp->fh), NULL,
188 argp->vec, argp->vlen,
191 return nfsd_return_attrs(nfserr, resp);
195 * CREATE processing is complicated. The keyword here is `overloaded.'
196 * The parent directory is kept locked between the check for existence
197 * and the actual create() call in compliance with VFS protocols.
198 * N.B. After this call _both_ argp->fh and resp->fh need an fh_put
201 nfsd_proc_create(struct svc_rqst *rqstp, struct nfsd_createargs *argp,
202 struct nfsd_diropres *resp)
204 svc_fh *dirfhp = &argp->fh;
205 svc_fh *newfhp = &resp->fh;
206 struct iattr *attr = &argp->attrs;
208 struct dentry *dchild;
209 int nfserr, type, mode;
210 dev_t rdev = 0, wanted = new_decode_dev(attr->ia_size);
212 dprintk("nfsd: CREATE %s %.*s\n",
213 SVCFH_fmt(dirfhp), argp->len, argp->name);
215 /* First verify the parent file handle */
216 nfserr = fh_verify(rqstp, dirfhp, S_IFDIR, MAY_EXEC);
218 goto done; /* must fh_put dirfhp even on error */
220 /* Check for MAY_WRITE in nfsd_create if necessary */
222 nfserr = nfserr_acces;
225 nfserr = nfserr_exist;
226 if (isdotent(argp->name, argp->len))
229 dchild = lookup_one_len(argp->name, dirfhp->fh_dentry, argp->len);
230 if (IS_ERR(dchild)) {
231 nfserr = nfserrno(PTR_ERR(dchild));
234 fh_init(newfhp, NFS_FHSIZE);
235 nfserr = fh_compose(newfhp, dirfhp->fh_export, dchild, dirfhp);
236 if (!nfserr && !dchild->d_inode)
237 nfserr = nfserr_noent;
240 if (nfserr != nfserr_noent)
243 * If the new file handle wasn't verified, we can't tell
244 * whether the file exists or not. Time to bail ...
246 nfserr = nfserr_acces;
247 if (!newfhp->fh_dentry) {
249 "nfsd_proc_create: file handle not verified\n");
254 inode = newfhp->fh_dentry->d_inode;
256 /* Unfudge the mode bits */
257 if (attr->ia_valid & ATTR_MODE) {
258 type = attr->ia_mode & S_IFMT;
259 mode = attr->ia_mode & ~S_IFMT;
261 /* no type, so if target exists, assume same as that,
262 * else assume a file */
264 type = inode->i_mode & S_IFMT;
268 /* reserve rdev for later checking */
269 rdev = inode->i_rdev;
270 attr->ia_valid |= ATTR_SIZE;
274 /* this is probably a permission check..
275 * at least IRIX implements perm checking on
276 * echo thing > device-special-file-or-pipe
277 * by doing a CREATE with type==0
279 nfserr = nfsd_permission(newfhp->fh_export,
281 MAY_WRITE|MAY_LOCAL_ACCESS);
282 if (nfserr && nfserr != nfserr_rofs)
289 type = inode->i_mode & S_IFMT;
290 mode = inode->i_mode & ~S_IFMT;
296 attr->ia_valid |= ATTR_MODE;
297 attr->ia_mode = mode;
299 /* Special treatment for non-regular files according to the
300 * gospel of sun micro
302 if (type != S_IFREG) {
304 if (type != S_IFBLK && type != S_IFCHR) {
306 } else if (type == S_IFCHR && !(attr->ia_valid & ATTR_SIZE)) {
307 /* If you think you've seen the worst, grok this. */
310 /* Okay, char or block special */
316 /* we've used the SIZE information, so discard it */
317 attr->ia_valid &= ~ATTR_SIZE;
319 /* Make sure the type and device matches */
320 nfserr = nfserr_exist;
321 if (inode && type != (inode->i_mode & S_IFMT))
327 /* File doesn't exist. Create it and set attrs */
328 nfserr = nfsd_create(rqstp, dirfhp, argp->name, argp->len,
329 attr, type, rdev, newfhp);
330 } else if (type == S_IFREG) {
331 dprintk("nfsd: existing %s, valid=%x, size=%ld\n",
332 argp->name, attr->ia_valid, (long) attr->ia_size);
333 /* File already exists. We ignore all attributes except
334 * size, so that creat() behaves exactly like
335 * open(..., O_CREAT|O_TRUNC|O_WRONLY).
337 attr->ia_valid &= ATTR_SIZE;
339 nfserr = nfsd_setattr(rqstp, newfhp, attr, 0, (time_t)0);
343 /* We don't really need to unlock, as fh_put does it. */
348 return nfsd_return_dirop(nfserr, resp);
352 nfsd_proc_remove(struct svc_rqst *rqstp, struct nfsd_diropargs *argp,
357 dprintk("nfsd: REMOVE %s %.*s\n", SVCFH_fmt(&argp->fh),
358 argp->len, argp->name);
360 /* Unlink. -SIFDIR means file must not be a directory */
361 nfserr = nfsd_unlink(rqstp, &argp->fh, -S_IFDIR, argp->name, argp->len);
367 nfsd_proc_rename(struct svc_rqst *rqstp, struct nfsd_renameargs *argp,
372 dprintk("nfsd: RENAME %s %.*s -> \n",
373 SVCFH_fmt(&argp->ffh), argp->flen, argp->fname);
374 dprintk("nfsd: -> %s %.*s\n",
375 SVCFH_fmt(&argp->tfh), argp->tlen, argp->tname);
377 nfserr = nfsd_rename(rqstp, &argp->ffh, argp->fname, argp->flen,
378 &argp->tfh, argp->tname, argp->tlen);
385 nfsd_proc_link(struct svc_rqst *rqstp, struct nfsd_linkargs *argp,
390 dprintk("nfsd: LINK %s ->\n",
391 SVCFH_fmt(&argp->ffh));
392 dprintk("nfsd: %s %.*s\n",
393 SVCFH_fmt(&argp->tfh),
397 nfserr = nfsd_link(rqstp, &argp->tfh, argp->tname, argp->tlen,
405 nfsd_proc_symlink(struct svc_rqst *rqstp, struct nfsd_symlinkargs *argp,
411 dprintk("nfsd: SYMLINK %s %.*s -> %.*s\n",
412 SVCFH_fmt(&argp->ffh), argp->flen, argp->fname,
413 argp->tlen, argp->tname);
415 fh_init(&newfh, NFS_FHSIZE);
417 * Create the link, look up new file and set attrs.
419 nfserr = nfsd_symlink(rqstp, &argp->ffh, argp->fname, argp->flen,
420 argp->tname, argp->tlen,
421 &newfh, &argp->attrs);
430 * Make directory. This operation is not idempotent.
431 * N.B. After this call resp->fh needs an fh_put
434 nfsd_proc_mkdir(struct svc_rqst *rqstp, struct nfsd_createargs *argp,
435 struct nfsd_diropres *resp)
439 dprintk("nfsd: MKDIR %s %.*s\n", SVCFH_fmt(&argp->fh), argp->len, argp->name);
441 if (resp->fh.fh_dentry) {
443 "nfsd_proc_mkdir: response already verified??\n");
446 argp->attrs.ia_valid &= ~ATTR_SIZE;
447 fh_init(&resp->fh, NFS_FHSIZE);
448 nfserr = nfsd_create(rqstp, &argp->fh, argp->name, argp->len,
449 &argp->attrs, S_IFDIR, 0, &resp->fh);
451 return nfsd_return_dirop(nfserr, resp);
458 nfsd_proc_rmdir(struct svc_rqst *rqstp, struct nfsd_diropargs *argp,
463 dprintk("nfsd: RMDIR %s %.*s\n", SVCFH_fmt(&argp->fh), argp->len, argp->name);
465 nfserr = nfsd_unlink(rqstp, &argp->fh, S_IFDIR, argp->name, argp->len);
471 * Read a portion of a directory.
474 nfsd_proc_readdir(struct svc_rqst *rqstp, struct nfsd_readdirargs *argp,
475 struct nfsd_readdirres *resp)
480 dprintk("nfsd: READDIR %s %d bytes at %d\n",
481 SVCFH_fmt(&argp->fh),
482 argp->count, argp->cookie);
484 /* Shrink to the client read size */
485 count = (argp->count >> 2) - 2;
487 /* Make sure we've room for the NULL ptr & eof flag */
492 resp->buffer = argp->buffer;
494 resp->buflen = count;
495 resp->common.err = nfs_ok;
496 /* Read directory and encode entries on the fly */
497 offset = argp->cookie;
498 nfserr = nfsd_readdir(rqstp, &argp->fh, &offset,
499 &resp->common, nfssvc_encode_entry);
501 resp->count = resp->buffer - argp->buffer;
503 *resp->offset = htonl(offset);
510 * Get file system info
513 nfsd_proc_statfs(struct svc_rqst * rqstp, struct nfsd_fhandle *argp,
514 struct nfsd_statfsres *resp)
518 dprintk("nfsd: STATFS %s\n", SVCFH_fmt(&argp->fh));
520 nfserr = nfsd_statfs(rqstp, &argp->fh, &resp->stats);
526 * NFSv2 Server procedures.
527 * Only the results of non-idempotent operations are cached.
529 #define nfsd_proc_none NULL
530 #define nfssvc_release_none NULL
531 struct nfsd_void { int dummy; };
533 #define PROC(name, argt, rest, relt, cache, respsize) \
534 { (svc_procfunc) nfsd_proc_##name, \
535 (kxdrproc_t) nfssvc_decode_##argt, \
536 (kxdrproc_t) nfssvc_encode_##rest, \
537 (kxdrproc_t) nfssvc_release_##relt, \
538 sizeof(struct nfsd_##argt), \
539 sizeof(struct nfsd_##rest), \
545 #define ST 1 /* status */
546 #define FH 8 /* filehandle */
547 #define AT 18 /* attributes */
549 static struct svc_procedure nfsd_procedures2[18] = {
550 PROC(null, void, void, none, RC_NOCACHE, ST),
551 PROC(getattr, fhandle, attrstat, fhandle, RC_NOCACHE, ST+AT),
552 PROC(setattr, sattrargs, attrstat, fhandle, RC_REPLBUFF, ST+AT),
553 PROC(none, void, void, none, RC_NOCACHE, ST),
554 PROC(lookup, diropargs, diropres, fhandle, RC_NOCACHE, ST+FH+AT),
555 PROC(readlink, readlinkargs, readlinkres, none, RC_NOCACHE, ST+1+NFS_MAXPATHLEN/4),
556 PROC(read, readargs, readres, fhandle, RC_NOCACHE, ST+AT+1+NFSSVC_MAXBLKSIZE/4),
557 PROC(none, void, void, none, RC_NOCACHE, ST),
558 PROC(write, writeargs, attrstat, fhandle, RC_REPLBUFF, ST+AT),
559 PROC(create, createargs, diropres, fhandle, RC_REPLBUFF, ST+FH+AT),
560 PROC(remove, diropargs, void, none, RC_REPLSTAT, ST),
561 PROC(rename, renameargs, void, none, RC_REPLSTAT, ST),
562 PROC(link, linkargs, void, none, RC_REPLSTAT, ST),
563 PROC(symlink, symlinkargs, void, none, RC_REPLSTAT, ST),
564 PROC(mkdir, createargs, diropres, fhandle, RC_REPLBUFF, ST+FH+AT),
565 PROC(rmdir, diropargs, void, none, RC_REPLSTAT, ST),
566 PROC(readdir, readdirargs, readdirres, none, RC_NOCACHE, 0),
567 PROC(statfs, fhandle, statfsres, none, RC_NOCACHE, ST+5),
571 struct svc_version nfsd_version2 = {
574 .vs_proc = nfsd_procedures2,
575 .vs_dispatch = nfsd_dispatch,
576 .vs_xdrsize = NFS2_SVC_XDRSIZE,
580 * Map errnos to NFS errnos.
590 { nfserr_perm, -EPERM },
591 { nfserr_noent, -ENOENT },
593 { nfserr_nxio, -ENXIO },
594 { nfserr_acces, -EACCES },
595 { nfserr_exist, -EEXIST },
596 { nfserr_xdev, -EXDEV },
597 { nfserr_mlink, -EMLINK },
598 { nfserr_nodev, -ENODEV },
599 { nfserr_notdir, -ENOTDIR },
600 { nfserr_isdir, -EISDIR },
601 { nfserr_inval, -EINVAL },
602 { nfserr_fbig, -EFBIG },
603 { nfserr_nospc, -ENOSPC },
604 { nfserr_rofs, -EROFS },
605 { nfserr_mlink, -EMLINK },
606 { nfserr_nametoolong, -ENAMETOOLONG },
607 { nfserr_notempty, -ENOTEMPTY },
609 { nfserr_dquot, -EDQUOT },
611 { nfserr_stale, -ESTALE },
612 { nfserr_jukebox, -ETIMEDOUT },
613 { nfserr_dropit, -EAGAIN },
614 { nfserr_dropit, -ENOMEM },
615 { nfserr_badname, -ESRCH },
616 { nfserr_io, -ETXTBSY },
617 { nfserr_notsupp, -EOPNOTSUPP },
622 for (i = 0; nfs_errtbl[i].nfserr != -1; i++) {
623 if (nfs_errtbl[i].syserr == errno)
624 return nfs_errtbl[i].nfserr;
626 printk (KERN_INFO "nfsd: non-standard errno: %d\n", errno);