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(__be32 err, struct nfsd_attrstat *resp)
43 return nfserrno(vfs_getattr(resp->fh.fh_export->ex_mnt,
48 nfsd_return_dirop(__be32 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_V2 < 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_V2;
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 rqstp->rq_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 rqstp->rq_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;
211 dev_t rdev = 0, wanted = new_decode_dev(attr->ia_size);
213 dprintk("nfsd: CREATE %s %.*s\n",
214 SVCFH_fmt(dirfhp), argp->len, argp->name);
216 /* First verify the parent file handle */
217 nfserr = fh_verify(rqstp, dirfhp, S_IFDIR, MAY_EXEC);
219 goto done; /* must fh_put dirfhp even on error */
221 /* Check for MAY_WRITE in nfsd_create if necessary */
223 nfserr = nfserr_acces;
226 nfserr = nfserr_exist;
227 if (isdotent(argp->name, argp->len))
229 fh_lock_nested(dirfhp, I_MUTEX_PARENT);
230 dchild = lookup_one_len(argp->name, dirfhp->fh_dentry, argp->len);
231 if (IS_ERR(dchild)) {
232 nfserr = nfserrno(PTR_ERR(dchild));
235 fh_init(newfhp, NFS_FHSIZE);
236 nfserr = fh_compose(newfhp, dirfhp->fh_export, dchild, dirfhp);
237 if (!nfserr && !dchild->d_inode)
238 nfserr = nfserr_noent;
241 if (nfserr != nfserr_noent)
244 * If the new file handle wasn't verified, we can't tell
245 * whether the file exists or not. Time to bail ...
247 nfserr = nfserr_acces;
248 if (!newfhp->fh_dentry) {
250 "nfsd_proc_create: file handle not verified\n");
255 inode = newfhp->fh_dentry->d_inode;
257 /* Unfudge the mode bits */
258 if (attr->ia_valid & ATTR_MODE) {
259 type = attr->ia_mode & S_IFMT;
260 mode = attr->ia_mode & ~S_IFMT;
262 /* no type, so if target exists, assume same as that,
263 * else assume a file */
265 type = inode->i_mode & S_IFMT;
269 /* reserve rdev for later checking */
270 rdev = inode->i_rdev;
271 attr->ia_valid |= ATTR_SIZE;
275 /* this is probably a permission check..
276 * at least IRIX implements perm checking on
277 * echo thing > device-special-file-or-pipe
278 * by doing a CREATE with type==0
280 nfserr = nfsd_permission(newfhp->fh_export,
282 MAY_WRITE|MAY_LOCAL_ACCESS);
283 if (nfserr && nfserr != nfserr_rofs)
290 type = inode->i_mode & S_IFMT;
291 mode = inode->i_mode & ~S_IFMT;
297 attr->ia_valid |= ATTR_MODE;
298 attr->ia_mode = mode;
300 /* Special treatment for non-regular files according to the
301 * gospel of sun micro
303 if (type != S_IFREG) {
305 if (type != S_IFBLK && type != S_IFCHR) {
307 } else if (type == S_IFCHR && !(attr->ia_valid & ATTR_SIZE)) {
308 /* If you think you've seen the worst, grok this. */
311 /* Okay, char or block special */
317 /* we've used the SIZE information, so discard it */
318 attr->ia_valid &= ~ATTR_SIZE;
320 /* Make sure the type and device matches */
321 nfserr = nfserr_exist;
322 if (inode && type != (inode->i_mode & S_IFMT))
328 /* File doesn't exist. Create it and set attrs */
329 nfserr = nfsd_create(rqstp, dirfhp, argp->name, argp->len,
330 attr, type, rdev, newfhp);
331 } else if (type == S_IFREG) {
332 dprintk("nfsd: existing %s, valid=%x, size=%ld\n",
333 argp->name, attr->ia_valid, (long) attr->ia_size);
334 /* File already exists. We ignore all attributes except
335 * size, so that creat() behaves exactly like
336 * open(..., O_CREAT|O_TRUNC|O_WRONLY).
338 attr->ia_valid &= ATTR_SIZE;
340 nfserr = nfsd_setattr(rqstp, newfhp, attr, 0, (time_t)0);
344 /* We don't really need to unlock, as fh_put does it. */
349 return nfsd_return_dirop(nfserr, resp);
353 nfsd_proc_remove(struct svc_rqst *rqstp, struct nfsd_diropargs *argp,
358 dprintk("nfsd: REMOVE %s %.*s\n", SVCFH_fmt(&argp->fh),
359 argp->len, argp->name);
361 /* Unlink. -SIFDIR means file must not be a directory */
362 nfserr = nfsd_unlink(rqstp, &argp->fh, -S_IFDIR, argp->name, argp->len);
368 nfsd_proc_rename(struct svc_rqst *rqstp, struct nfsd_renameargs *argp,
373 dprintk("nfsd: RENAME %s %.*s -> \n",
374 SVCFH_fmt(&argp->ffh), argp->flen, argp->fname);
375 dprintk("nfsd: -> %s %.*s\n",
376 SVCFH_fmt(&argp->tfh), argp->tlen, argp->tname);
378 nfserr = nfsd_rename(rqstp, &argp->ffh, argp->fname, argp->flen,
379 &argp->tfh, argp->tname, argp->tlen);
386 nfsd_proc_link(struct svc_rqst *rqstp, struct nfsd_linkargs *argp,
391 dprintk("nfsd: LINK %s ->\n",
392 SVCFH_fmt(&argp->ffh));
393 dprintk("nfsd: %s %.*s\n",
394 SVCFH_fmt(&argp->tfh),
398 nfserr = nfsd_link(rqstp, &argp->tfh, argp->tname, argp->tlen,
406 nfsd_proc_symlink(struct svc_rqst *rqstp, struct nfsd_symlinkargs *argp,
412 dprintk("nfsd: SYMLINK %s %.*s -> %.*s\n",
413 SVCFH_fmt(&argp->ffh), argp->flen, argp->fname,
414 argp->tlen, argp->tname);
416 fh_init(&newfh, NFS_FHSIZE);
418 * Create the link, look up new file and set attrs.
420 nfserr = nfsd_symlink(rqstp, &argp->ffh, argp->fname, argp->flen,
421 argp->tname, argp->tlen,
422 &newfh, &argp->attrs);
431 * Make directory. This operation is not idempotent.
432 * N.B. After this call resp->fh needs an fh_put
435 nfsd_proc_mkdir(struct svc_rqst *rqstp, struct nfsd_createargs *argp,
436 struct nfsd_diropres *resp)
440 dprintk("nfsd: MKDIR %s %.*s\n", SVCFH_fmt(&argp->fh), argp->len, argp->name);
442 if (resp->fh.fh_dentry) {
444 "nfsd_proc_mkdir: response already verified??\n");
447 argp->attrs.ia_valid &= ~ATTR_SIZE;
448 fh_init(&resp->fh, NFS_FHSIZE);
449 nfserr = nfsd_create(rqstp, &argp->fh, argp->name, argp->len,
450 &argp->attrs, S_IFDIR, 0, &resp->fh);
452 return nfsd_return_dirop(nfserr, resp);
459 nfsd_proc_rmdir(struct svc_rqst *rqstp, struct nfsd_diropargs *argp,
464 dprintk("nfsd: RMDIR %s %.*s\n", SVCFH_fmt(&argp->fh), argp->len, argp->name);
466 nfserr = nfsd_unlink(rqstp, &argp->fh, S_IFDIR, argp->name, argp->len);
472 * Read a portion of a directory.
475 nfsd_proc_readdir(struct svc_rqst *rqstp, struct nfsd_readdirargs *argp,
476 struct nfsd_readdirres *resp)
482 dprintk("nfsd: READDIR %s %d bytes at %d\n",
483 SVCFH_fmt(&argp->fh),
484 argp->count, argp->cookie);
486 /* Shrink to the client read size */
487 count = (argp->count >> 2) - 2;
489 /* Make sure we've room for the NULL ptr & eof flag */
494 resp->buffer = argp->buffer;
496 resp->buflen = count;
497 resp->common.err = nfs_ok;
498 /* Read directory and encode entries on the fly */
499 offset = argp->cookie;
500 nfserr = nfsd_readdir(rqstp, &argp->fh, &offset,
501 &resp->common, nfssvc_encode_entry);
503 resp->count = resp->buffer - argp->buffer;
505 *resp->offset = htonl(offset);
512 * Get file system info
515 nfsd_proc_statfs(struct svc_rqst * rqstp, struct nfsd_fhandle *argp,
516 struct nfsd_statfsres *resp)
520 dprintk("nfsd: STATFS %s\n", SVCFH_fmt(&argp->fh));
522 nfserr = nfsd_statfs(rqstp, &argp->fh, &resp->stats);
528 * NFSv2 Server procedures.
529 * Only the results of non-idempotent operations are cached.
531 #define nfsd_proc_none NULL
532 #define nfssvc_release_none NULL
533 struct nfsd_void { int dummy; };
535 #define PROC(name, argt, rest, relt, cache, respsize) \
536 { (svc_procfunc) nfsd_proc_##name, \
537 (kxdrproc_t) nfssvc_decode_##argt, \
538 (kxdrproc_t) nfssvc_encode_##rest, \
539 (kxdrproc_t) nfssvc_release_##relt, \
540 sizeof(struct nfsd_##argt), \
541 sizeof(struct nfsd_##rest), \
547 #define ST 1 /* status */
548 #define FH 8 /* filehandle */
549 #define AT 18 /* attributes */
551 static struct svc_procedure nfsd_procedures2[18] = {
552 PROC(null, void, void, none, RC_NOCACHE, ST),
553 PROC(getattr, fhandle, attrstat, fhandle, RC_NOCACHE, ST+AT),
554 PROC(setattr, sattrargs, attrstat, fhandle, RC_REPLBUFF, ST+AT),
555 PROC(none, void, void, none, RC_NOCACHE, ST),
556 PROC(lookup, diropargs, diropres, fhandle, RC_NOCACHE, ST+FH+AT),
557 PROC(readlink, readlinkargs, readlinkres, none, RC_NOCACHE, ST+1+NFS_MAXPATHLEN/4),
558 PROC(read, readargs, readres, fhandle, RC_NOCACHE, ST+AT+1+NFSSVC_MAXBLKSIZE_V2/4),
559 PROC(none, void, void, none, RC_NOCACHE, ST),
560 PROC(write, writeargs, attrstat, fhandle, RC_REPLBUFF, ST+AT),
561 PROC(create, createargs, diropres, fhandle, RC_REPLBUFF, ST+FH+AT),
562 PROC(remove, diropargs, void, none, RC_REPLSTAT, ST),
563 PROC(rename, renameargs, void, none, RC_REPLSTAT, ST),
564 PROC(link, linkargs, void, none, RC_REPLSTAT, ST),
565 PROC(symlink, symlinkargs, void, none, RC_REPLSTAT, ST),
566 PROC(mkdir, createargs, diropres, fhandle, RC_REPLBUFF, ST+FH+AT),
567 PROC(rmdir, diropargs, void, none, RC_REPLSTAT, ST),
568 PROC(readdir, readdirargs, readdirres, none, RC_NOCACHE, 0),
569 PROC(statfs, fhandle, statfsres, none, RC_NOCACHE, ST+5),
573 struct svc_version nfsd_version2 = {
576 .vs_proc = nfsd_procedures2,
577 .vs_dispatch = nfsd_dispatch,
578 .vs_xdrsize = NFS2_SVC_XDRSIZE,
582 * Map errnos to NFS errnos.
592 { nfserr_perm, -EPERM },
593 { nfserr_noent, -ENOENT },
595 { nfserr_nxio, -ENXIO },
596 { nfserr_acces, -EACCES },
597 { nfserr_exist, -EEXIST },
598 { nfserr_xdev, -EXDEV },
599 { nfserr_mlink, -EMLINK },
600 { nfserr_nodev, -ENODEV },
601 { nfserr_notdir, -ENOTDIR },
602 { nfserr_isdir, -EISDIR },
603 { nfserr_inval, -EINVAL },
604 { nfserr_fbig, -EFBIG },
605 { nfserr_nospc, -ENOSPC },
606 { nfserr_rofs, -EROFS },
607 { nfserr_mlink, -EMLINK },
608 { nfserr_nametoolong, -ENAMETOOLONG },
609 { nfserr_notempty, -ENOTEMPTY },
611 { nfserr_dquot, -EDQUOT },
613 { nfserr_stale, -ESTALE },
614 { nfserr_jukebox, -ETIMEDOUT },
615 { nfserr_dropit, -EAGAIN },
616 { nfserr_dropit, -ENOMEM },
617 { nfserr_badname, -ESRCH },
618 { nfserr_io, -ETXTBSY },
619 { nfserr_notsupp, -EOPNOTSUPP },
623 for (i = 0; i < ARRAY_SIZE(nfs_errtbl); i++) {
624 if (nfs_errtbl[i].syserr == errno)
625 return nfs_errtbl[i].nfserr;
627 printk (KERN_INFO "nfsd: non-standard errno: %d\n", errno);