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/clnt.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 nfsd_proc_null(struct svc_rqst *rqstp, void *argp, void *resp)
41 nfsd_return_attrs(__be32 err, struct nfsd_attrstat *resp)
44 return nfserrno(vfs_getattr(resp->fh.fh_export->ex_path.mnt,
49 nfsd_return_dirop(__be32 err, struct nfsd_diropres *resp)
52 return nfserrno(vfs_getattr(resp->fh.fh_export->ex_path.mnt,
57 * Get a file's attributes
58 * N.B. After this call resp->fh needs an fh_put
61 nfsd_proc_getattr(struct svc_rqst *rqstp, struct nfsd_fhandle *argp,
62 struct nfsd_attrstat *resp)
65 dprintk("nfsd: GETATTR %s\n", SVCFH_fmt(&argp->fh));
67 fh_copy(&resp->fh, &argp->fh);
68 nfserr = fh_verify(rqstp, &resp->fh, 0,
69 NFSD_MAY_NOP | NFSD_MAY_BYPASS_GSS_ON_ROOT);
70 return nfsd_return_attrs(nfserr, resp);
74 * Set a file's attributes
75 * N.B. After this call resp->fh needs an fh_put
78 nfsd_proc_setattr(struct svc_rqst *rqstp, struct nfsd_sattrargs *argp,
79 struct nfsd_attrstat *resp)
82 dprintk("nfsd: SETATTR %s, valid=%x, size=%ld\n",
84 argp->attrs.ia_valid, (long) argp->attrs.ia_size);
86 fh_copy(&resp->fh, &argp->fh);
87 nfserr = nfsd_setattr(rqstp, &resp->fh, &argp->attrs,0, (time_t)0);
88 return nfsd_return_attrs(nfserr, resp);
92 * Look up a path name component
93 * Note: the dentry in the resp->fh may be negative if the file
95 * N.B. After this call resp->fh needs an fh_put
98 nfsd_proc_lookup(struct svc_rqst *rqstp, struct nfsd_diropargs *argp,
99 struct nfsd_diropres *resp)
103 dprintk("nfsd: LOOKUP %s %.*s\n",
104 SVCFH_fmt(&argp->fh), argp->len, argp->name);
106 fh_init(&resp->fh, NFS_FHSIZE);
107 nfserr = nfsd_lookup(rqstp, &argp->fh, argp->name, argp->len,
111 return nfsd_return_dirop(nfserr, resp);
118 nfsd_proc_readlink(struct svc_rqst *rqstp, struct nfsd_readlinkargs *argp,
119 struct nfsd_readlinkres *resp)
123 dprintk("nfsd: READLINK %s\n", SVCFH_fmt(&argp->fh));
125 /* Read the symlink. */
126 resp->len = NFS_MAXPATHLEN;
127 nfserr = nfsd_readlink(rqstp, &argp->fh, argp->buffer, &resp->len);
134 * Read a portion of a file.
135 * N.B. After this call resp->fh needs an fh_put
138 nfsd_proc_read(struct svc_rqst *rqstp, struct nfsd_readargs *argp,
139 struct nfsd_readres *resp)
143 dprintk("nfsd: READ %s %d bytes at %d\n",
144 SVCFH_fmt(&argp->fh),
145 argp->count, argp->offset);
147 /* Obtain buffer pointer for payload. 19 is 1 word for
148 * status, 17 words for fattr, and 1 word for the byte count.
151 if (NFSSVC_MAXBLKSIZE_V2 < argp->count) {
152 char buf[RPC_MAX_ADDRBUFLEN];
154 "oversized read request from %s (%d bytes)\n",
155 svc_print_addr(rqstp, buf, sizeof(buf)),
157 argp->count = NFSSVC_MAXBLKSIZE_V2;
159 svc_reserve_auth(rqstp, (19<<2) + argp->count + 4);
161 resp->count = argp->count;
162 nfserr = nfsd_read(rqstp, fh_copy(&resp->fh, &argp->fh), NULL,
164 rqstp->rq_vec, argp->vlen,
167 if (nfserr) return nfserr;
168 return nfserrno(vfs_getattr(resp->fh.fh_export->ex_path.mnt,
174 * Write data to a file
175 * N.B. After this call resp->fh needs an fh_put
178 nfsd_proc_write(struct svc_rqst *rqstp, struct nfsd_writeargs *argp,
179 struct nfsd_attrstat *resp)
183 unsigned long cnt = argp->len;
185 dprintk("nfsd: WRITE %s %d bytes at %d\n",
186 SVCFH_fmt(&argp->fh),
187 argp->len, argp->offset);
189 nfserr = nfsd_write(rqstp, fh_copy(&resp->fh, &argp->fh), NULL,
191 rqstp->rq_vec, argp->vlen,
194 return nfsd_return_attrs(nfserr, resp);
198 * CREATE processing is complicated. The keyword here is `overloaded.'
199 * The parent directory is kept locked between the check for existence
200 * and the actual create() call in compliance with VFS protocols.
201 * N.B. After this call _both_ argp->fh and resp->fh need an fh_put
204 nfsd_proc_create(struct svc_rqst *rqstp, struct nfsd_createargs *argp,
205 struct nfsd_diropres *resp)
207 svc_fh *dirfhp = &argp->fh;
208 svc_fh *newfhp = &resp->fh;
209 struct iattr *attr = &argp->attrs;
211 struct dentry *dchild;
214 dev_t rdev = 0, wanted = new_decode_dev(attr->ia_size);
216 dprintk("nfsd: CREATE %s %.*s\n",
217 SVCFH_fmt(dirfhp), argp->len, argp->name);
219 /* First verify the parent file handle */
220 nfserr = fh_verify(rqstp, dirfhp, S_IFDIR, NFSD_MAY_EXEC);
222 goto done; /* must fh_put dirfhp even on error */
224 /* Check for NFSD_MAY_WRITE in nfsd_create if necessary */
226 nfserr = nfserr_acces;
229 nfserr = nfserr_exist;
230 if (isdotent(argp->name, argp->len))
232 fh_lock_nested(dirfhp, I_MUTEX_PARENT);
233 dchild = lookup_one_len(argp->name, dirfhp->fh_dentry, argp->len);
234 if (IS_ERR(dchild)) {
235 nfserr = nfserrno(PTR_ERR(dchild));
238 fh_init(newfhp, NFS_FHSIZE);
239 nfserr = fh_compose(newfhp, dirfhp->fh_export, dchild, dirfhp);
240 if (!nfserr && !dchild->d_inode)
241 nfserr = nfserr_noent;
244 if (nfserr != nfserr_noent)
247 * If the new file handle wasn't verified, we can't tell
248 * whether the file exists or not. Time to bail ...
250 nfserr = nfserr_acces;
251 if (!newfhp->fh_dentry) {
253 "nfsd_proc_create: file handle not verified\n");
258 inode = newfhp->fh_dentry->d_inode;
260 /* Unfudge the mode bits */
261 if (attr->ia_valid & ATTR_MODE) {
262 type = attr->ia_mode & S_IFMT;
263 mode = attr->ia_mode & ~S_IFMT;
265 /* no type, so if target exists, assume same as that,
266 * else assume a file */
268 type = inode->i_mode & S_IFMT;
272 /* reserve rdev for later checking */
273 rdev = inode->i_rdev;
274 attr->ia_valid |= ATTR_SIZE;
278 /* this is probably a permission check..
279 * at least IRIX implements perm checking on
280 * echo thing > device-special-file-or-pipe
281 * by doing a CREATE with type==0
283 nfserr = nfsd_permission(rqstp,
286 NFSD_MAY_WRITE|NFSD_MAY_LOCAL_ACCESS);
287 if (nfserr && nfserr != nfserr_rofs)
294 type = inode->i_mode & S_IFMT;
295 mode = inode->i_mode & ~S_IFMT;
301 attr->ia_valid |= ATTR_MODE;
302 attr->ia_mode = mode;
304 /* Special treatment for non-regular files according to the
305 * gospel of sun micro
307 if (type != S_IFREG) {
309 if (type != S_IFBLK && type != S_IFCHR) {
311 } else if (type == S_IFCHR && !(attr->ia_valid & ATTR_SIZE)) {
312 /* If you think you've seen the worst, grok this. */
315 /* Okay, char or block special */
321 /* we've used the SIZE information, so discard it */
322 attr->ia_valid &= ~ATTR_SIZE;
324 /* Make sure the type and device matches */
325 nfserr = nfserr_exist;
326 if (inode && type != (inode->i_mode & S_IFMT))
332 /* File doesn't exist. Create it and set attrs */
333 nfserr = nfsd_create(rqstp, dirfhp, argp->name, argp->len,
334 attr, type, rdev, newfhp);
335 } else if (type == S_IFREG) {
336 dprintk("nfsd: existing %s, valid=%x, size=%ld\n",
337 argp->name, attr->ia_valid, (long) attr->ia_size);
338 /* File already exists. We ignore all attributes except
339 * size, so that creat() behaves exactly like
340 * open(..., O_CREAT|O_TRUNC|O_WRONLY).
342 attr->ia_valid &= ATTR_SIZE;
344 nfserr = nfsd_setattr(rqstp, newfhp, attr, 0, (time_t)0);
348 /* We don't really need to unlock, as fh_put does it. */
353 return nfsd_return_dirop(nfserr, resp);
357 nfsd_proc_remove(struct svc_rqst *rqstp, struct nfsd_diropargs *argp,
362 dprintk("nfsd: REMOVE %s %.*s\n", SVCFH_fmt(&argp->fh),
363 argp->len, argp->name);
365 /* Unlink. -SIFDIR means file must not be a directory */
366 nfserr = nfsd_unlink(rqstp, &argp->fh, -S_IFDIR, argp->name, argp->len);
372 nfsd_proc_rename(struct svc_rqst *rqstp, struct nfsd_renameargs *argp,
377 dprintk("nfsd: RENAME %s %.*s -> \n",
378 SVCFH_fmt(&argp->ffh), argp->flen, argp->fname);
379 dprintk("nfsd: -> %s %.*s\n",
380 SVCFH_fmt(&argp->tfh), argp->tlen, argp->tname);
382 nfserr = nfsd_rename(rqstp, &argp->ffh, argp->fname, argp->flen,
383 &argp->tfh, argp->tname, argp->tlen);
390 nfsd_proc_link(struct svc_rqst *rqstp, struct nfsd_linkargs *argp,
395 dprintk("nfsd: LINK %s ->\n",
396 SVCFH_fmt(&argp->ffh));
397 dprintk("nfsd: %s %.*s\n",
398 SVCFH_fmt(&argp->tfh),
402 nfserr = nfsd_link(rqstp, &argp->tfh, argp->tname, argp->tlen,
410 nfsd_proc_symlink(struct svc_rqst *rqstp, struct nfsd_symlinkargs *argp,
416 dprintk("nfsd: SYMLINK %s %.*s -> %.*s\n",
417 SVCFH_fmt(&argp->ffh), argp->flen, argp->fname,
418 argp->tlen, argp->tname);
420 fh_init(&newfh, NFS_FHSIZE);
422 * Create the link, look up new file and set attrs.
424 nfserr = nfsd_symlink(rqstp, &argp->ffh, argp->fname, argp->flen,
425 argp->tname, argp->tlen,
426 &newfh, &argp->attrs);
435 * Make directory. This operation is not idempotent.
436 * N.B. After this call resp->fh needs an fh_put
439 nfsd_proc_mkdir(struct svc_rqst *rqstp, struct nfsd_createargs *argp,
440 struct nfsd_diropres *resp)
444 dprintk("nfsd: MKDIR %s %.*s\n", SVCFH_fmt(&argp->fh), argp->len, argp->name);
446 if (resp->fh.fh_dentry) {
448 "nfsd_proc_mkdir: response already verified??\n");
451 argp->attrs.ia_valid &= ~ATTR_SIZE;
452 fh_init(&resp->fh, NFS_FHSIZE);
453 nfserr = nfsd_create(rqstp, &argp->fh, argp->name, argp->len,
454 &argp->attrs, S_IFDIR, 0, &resp->fh);
456 return nfsd_return_dirop(nfserr, resp);
463 nfsd_proc_rmdir(struct svc_rqst *rqstp, struct nfsd_diropargs *argp,
468 dprintk("nfsd: RMDIR %s %.*s\n", SVCFH_fmt(&argp->fh), argp->len, argp->name);
470 nfserr = nfsd_unlink(rqstp, &argp->fh, S_IFDIR, argp->name, argp->len);
476 * Read a portion of a directory.
479 nfsd_proc_readdir(struct svc_rqst *rqstp, struct nfsd_readdirargs *argp,
480 struct nfsd_readdirres *resp)
486 dprintk("nfsd: READDIR %s %d bytes at %d\n",
487 SVCFH_fmt(&argp->fh),
488 argp->count, argp->cookie);
490 /* Shrink to the client read size */
491 count = (argp->count >> 2) - 2;
493 /* Make sure we've room for the NULL ptr & eof flag */
498 resp->buffer = argp->buffer;
500 resp->buflen = count;
501 resp->common.err = nfs_ok;
502 /* Read directory and encode entries on the fly */
503 offset = argp->cookie;
504 nfserr = nfsd_readdir(rqstp, &argp->fh, &offset,
505 &resp->common, nfssvc_encode_entry);
507 resp->count = resp->buffer - argp->buffer;
509 *resp->offset = htonl(offset);
516 * Get file system info
519 nfsd_proc_statfs(struct svc_rqst * rqstp, struct nfsd_fhandle *argp,
520 struct nfsd_statfsres *resp)
524 dprintk("nfsd: STATFS %s\n", SVCFH_fmt(&argp->fh));
526 nfserr = nfsd_statfs(rqstp, &argp->fh, &resp->stats,
527 NFSD_MAY_BYPASS_GSS_ON_ROOT);
533 * NFSv2 Server procedures.
534 * Only the results of non-idempotent operations are cached.
536 #define nfsd_proc_none NULL
537 #define nfssvc_release_none NULL
538 struct nfsd_void { int dummy; };
540 #define PROC(name, argt, rest, relt, cache, respsize) \
541 { (svc_procfunc) nfsd_proc_##name, \
542 (kxdrproc_t) nfssvc_decode_##argt, \
543 (kxdrproc_t) nfssvc_encode_##rest, \
544 (kxdrproc_t) nfssvc_release_##relt, \
545 sizeof(struct nfsd_##argt), \
546 sizeof(struct nfsd_##rest), \
552 #define ST 1 /* status */
553 #define FH 8 /* filehandle */
554 #define AT 18 /* attributes */
556 static struct svc_procedure nfsd_procedures2[18] = {
557 PROC(null, void, void, none, RC_NOCACHE, ST),
558 PROC(getattr, fhandle, attrstat, fhandle, RC_NOCACHE, ST+AT),
559 PROC(setattr, sattrargs, attrstat, fhandle, RC_REPLBUFF, ST+AT),
560 PROC(none, void, void, none, RC_NOCACHE, ST),
561 PROC(lookup, diropargs, diropres, fhandle, RC_NOCACHE, ST+FH+AT),
562 PROC(readlink, readlinkargs, readlinkres, none, RC_NOCACHE, ST+1+NFS_MAXPATHLEN/4),
563 PROC(read, readargs, readres, fhandle, RC_NOCACHE, ST+AT+1+NFSSVC_MAXBLKSIZE_V2/4),
564 PROC(none, void, void, none, RC_NOCACHE, ST),
565 PROC(write, writeargs, attrstat, fhandle, RC_REPLBUFF, ST+AT),
566 PROC(create, createargs, diropres, fhandle, RC_REPLBUFF, ST+FH+AT),
567 PROC(remove, diropargs, void, none, RC_REPLSTAT, ST),
568 PROC(rename, renameargs, void, none, RC_REPLSTAT, ST),
569 PROC(link, linkargs, void, none, RC_REPLSTAT, ST),
570 PROC(symlink, symlinkargs, void, none, RC_REPLSTAT, ST),
571 PROC(mkdir, createargs, diropres, fhandle, RC_REPLBUFF, ST+FH+AT),
572 PROC(rmdir, diropargs, void, none, RC_REPLSTAT, ST),
573 PROC(readdir, readdirargs, readdirres, none, RC_NOCACHE, 0),
574 PROC(statfs, fhandle, statfsres, none, RC_NOCACHE, ST+5),
578 struct svc_version nfsd_version2 = {
581 .vs_proc = nfsd_procedures2,
582 .vs_dispatch = nfsd_dispatch,
583 .vs_xdrsize = NFS2_SVC_XDRSIZE,
587 * Map errnos to NFS errnos.
597 { nfserr_perm, -EPERM },
598 { nfserr_noent, -ENOENT },
600 { nfserr_nxio, -ENXIO },
601 { nfserr_acces, -EACCES },
602 { nfserr_exist, -EEXIST },
603 { nfserr_xdev, -EXDEV },
604 { nfserr_mlink, -EMLINK },
605 { nfserr_nodev, -ENODEV },
606 { nfserr_notdir, -ENOTDIR },
607 { nfserr_isdir, -EISDIR },
608 { nfserr_inval, -EINVAL },
609 { nfserr_fbig, -EFBIG },
610 { nfserr_nospc, -ENOSPC },
611 { nfserr_rofs, -EROFS },
612 { nfserr_mlink, -EMLINK },
613 { nfserr_nametoolong, -ENAMETOOLONG },
614 { nfserr_notempty, -ENOTEMPTY },
616 { nfserr_dquot, -EDQUOT },
618 { nfserr_stale, -ESTALE },
619 { nfserr_jukebox, -ETIMEDOUT },
620 { nfserr_jukebox, -ERESTARTSYS },
621 { nfserr_dropit, -EAGAIN },
622 { nfserr_dropit, -ENOMEM },
623 { nfserr_badname, -ESRCH },
624 { nfserr_io, -ETXTBSY },
625 { nfserr_notsupp, -EOPNOTSUPP },
626 { nfserr_toosmall, -ETOOSMALL },
630 for (i = 0; i < ARRAY_SIZE(nfs_errtbl); i++) {
631 if (nfs_errtbl[i].syserr == errno)
632 return nfs_errtbl[i].nfserr;
634 printk (KERN_INFO "nfsd: non-standard errno: %d\n", errno);