[NETFILTER]: nf_conntrack: add helper function for expectation initialization
[hh.org.git] / fs / nfsd / nfsproc.c
blobec983b777680ded981c59dbb65c81ebb0c5637dc
1 /*
2 * nfsproc2.c Process version 2 NFS requests.
3 * linux/fs/nfsd/nfs2proc.c
4 *
5 * Process version 2 NFS requests.
7 * Copyright (C) 1995-1997 Olaf Kirch <okir@monad.swb.de>
8 */
10 #include <linux/linkage.h>
11 #include <linux/time.h>
12 #include <linux/errno.h>
13 #include <linux/fs.h>
14 #include <linux/stat.h>
15 #include <linux/fcntl.h>
16 #include <linux/net.h>
17 #include <linux/in.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
33 static __be32
34 nfsd_proc_null(struct svc_rqst *rqstp, void *argp, void *resp)
36 return nfs_ok;
39 static __be32
40 nfsd_return_attrs(__be32 err, struct nfsd_attrstat *resp)
42 if (err) return err;
43 return nfserrno(vfs_getattr(resp->fh.fh_export->ex_mnt,
44 resp->fh.fh_dentry,
45 &resp->stat));
47 static __be32
48 nfsd_return_dirop(__be32 err, struct nfsd_diropres *resp)
50 if (err) return err;
51 return nfserrno(vfs_getattr(resp->fh.fh_export->ex_mnt,
52 resp->fh.fh_dentry,
53 &resp->stat));
56 * Get a file's attributes
57 * N.B. After this call resp->fh needs an fh_put
59 static __be32
60 nfsd_proc_getattr(struct svc_rqst *rqstp, struct nfsd_fhandle *argp,
61 struct nfsd_attrstat *resp)
63 __be32 nfserr;
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
75 static __be32
76 nfsd_proc_setattr(struct svc_rqst *rqstp, struct nfsd_sattrargs *argp,
77 struct nfsd_attrstat *resp)
79 __be32 nfserr;
80 dprintk("nfsd: SETATTR %s, valid=%x, size=%ld\n",
81 SVCFH_fmt(&argp->fh),
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
92 * doesn't exist yet.
93 * N.B. After this call resp->fh needs an fh_put
95 static __be32
96 nfsd_proc_lookup(struct svc_rqst *rqstp, struct nfsd_diropargs *argp,
97 struct nfsd_diropres *resp)
99 __be32 nfserr;
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,
106 &resp->fh);
108 fh_put(&argp->fh);
109 return nfsd_return_dirop(nfserr, resp);
113 * Read a symlink.
115 static __be32
116 nfsd_proc_readlink(struct svc_rqst *rqstp, struct nfsd_readlinkargs *argp,
117 struct nfsd_readlinkres *resp)
119 __be32 nfserr;
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);
127 fh_put(&argp->fh);
128 return nfserr;
132 * Read a portion of a file.
133 * N.B. After this call resp->fh needs an fh_put
135 static __be32
136 nfsd_proc_read(struct svc_rqst *rqstp, struct nfsd_readargs *argp,
137 struct nfsd_readres *resp)
139 __be32 nfserr;
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) {
150 printk(KERN_NOTICE
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),
154 argp->count);
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,
161 argp->offset,
162 rqstp->rq_vec, argp->vlen,
163 &resp->count);
165 if (nfserr) return nfserr;
166 return nfserrno(vfs_getattr(resp->fh.fh_export->ex_mnt,
167 resp->fh.fh_dentry,
168 &resp->stat));
172 * Write data to a file
173 * N.B. After this call resp->fh needs an fh_put
175 static __be32
176 nfsd_proc_write(struct svc_rqst *rqstp, struct nfsd_writeargs *argp,
177 struct nfsd_attrstat *resp)
179 __be32 nfserr;
180 int stable = 1;
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,
187 argp->offset,
188 rqstp->rq_vec, argp->vlen,
189 argp->len,
190 &stable);
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
200 static __be32
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;
207 struct inode *inode;
208 struct dentry *dchild;
209 int type, mode;
210 __be32 nfserr;
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);
218 if (nfserr)
219 goto done; /* must fh_put dirfhp even on error */
221 /* Check for MAY_WRITE in nfsd_create if necessary */
223 nfserr = nfserr_acces;
224 if (!argp->len)
225 goto done;
226 nfserr = nfserr_exist;
227 if (isdotent(argp->name, argp->len))
228 goto done;
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));
233 goto out_unlock;
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;
239 dput(dchild);
240 if (nfserr) {
241 if (nfserr != nfserr_noent)
242 goto out_unlock;
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) {
249 printk(KERN_WARNING
250 "nfsd_proc_create: file handle not verified\n");
251 goto out_unlock;
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;
261 if (!type) {
262 /* no type, so if target exists, assume same as that,
263 * else assume a file */
264 if (inode) {
265 type = inode->i_mode & S_IFMT;
266 switch(type) {
267 case S_IFCHR:
268 case S_IFBLK:
269 /* reserve rdev for later checking */
270 rdev = inode->i_rdev;
271 attr->ia_valid |= ATTR_SIZE;
273 /* FALLTHROUGH */
274 case S_IFIFO:
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,
281 newfhp->fh_dentry,
282 MAY_WRITE|MAY_LOCAL_ACCESS);
283 if (nfserr && nfserr != nfserr_rofs)
284 goto out_unlock;
286 } else
287 type = S_IFREG;
289 } else if (inode) {
290 type = inode->i_mode & S_IFMT;
291 mode = inode->i_mode & ~S_IFMT;
292 } else {
293 type = S_IFREG;
294 mode = 0; /* ??? */
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) {
304 int is_borc = 0;
305 if (type != S_IFBLK && type != S_IFCHR) {
306 rdev = 0;
307 } else if (type == S_IFCHR && !(attr->ia_valid & ATTR_SIZE)) {
308 /* If you think you've seen the worst, grok this. */
309 type = S_IFIFO;
310 } else {
311 /* Okay, char or block special */
312 is_borc = 1;
313 if (!rdev)
314 rdev = wanted;
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))
323 goto out_unlock;
326 nfserr = 0;
327 if (!inode) {
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;
339 if (attr->ia_valid)
340 nfserr = nfsd_setattr(rqstp, newfhp, attr, 0, (time_t)0);
343 out_unlock:
344 /* We don't really need to unlock, as fh_put does it. */
345 fh_unlock(dirfhp);
347 done:
348 fh_put(dirfhp);
349 return nfsd_return_dirop(nfserr, resp);
352 static __be32
353 nfsd_proc_remove(struct svc_rqst *rqstp, struct nfsd_diropargs *argp,
354 void *resp)
356 __be32 nfserr;
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);
363 fh_put(&argp->fh);
364 return nfserr;
367 static __be32
368 nfsd_proc_rename(struct svc_rqst *rqstp, struct nfsd_renameargs *argp,
369 void *resp)
371 __be32 nfserr;
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);
380 fh_put(&argp->ffh);
381 fh_put(&argp->tfh);
382 return nfserr;
385 static __be32
386 nfsd_proc_link(struct svc_rqst *rqstp, struct nfsd_linkargs *argp,
387 void *resp)
389 __be32 nfserr;
391 dprintk("nfsd: LINK %s ->\n",
392 SVCFH_fmt(&argp->ffh));
393 dprintk("nfsd: %s %.*s\n",
394 SVCFH_fmt(&argp->tfh),
395 argp->tlen,
396 argp->tname);
398 nfserr = nfsd_link(rqstp, &argp->tfh, argp->tname, argp->tlen,
399 &argp->ffh);
400 fh_put(&argp->ffh);
401 fh_put(&argp->tfh);
402 return nfserr;
405 static __be32
406 nfsd_proc_symlink(struct svc_rqst *rqstp, struct nfsd_symlinkargs *argp,
407 void *resp)
409 struct svc_fh newfh;
410 __be32 nfserr;
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);
425 fh_put(&argp->ffh);
426 fh_put(&newfh);
427 return nfserr;
431 * Make directory. This operation is not idempotent.
432 * N.B. After this call resp->fh needs an fh_put
434 static __be32
435 nfsd_proc_mkdir(struct svc_rqst *rqstp, struct nfsd_createargs *argp,
436 struct nfsd_diropres *resp)
438 __be32 nfserr;
440 dprintk("nfsd: MKDIR %s %.*s\n", SVCFH_fmt(&argp->fh), argp->len, argp->name);
442 if (resp->fh.fh_dentry) {
443 printk(KERN_WARNING
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);
451 fh_put(&argp->fh);
452 return nfsd_return_dirop(nfserr, resp);
456 * Remove a directory
458 static __be32
459 nfsd_proc_rmdir(struct svc_rqst *rqstp, struct nfsd_diropargs *argp,
460 void *resp)
462 __be32 nfserr;
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);
467 fh_put(&argp->fh);
468 return nfserr;
472 * Read a portion of a directory.
474 static __be32
475 nfsd_proc_readdir(struct svc_rqst *rqstp, struct nfsd_readdirargs *argp,
476 struct nfsd_readdirres *resp)
478 int count;
479 __be32 nfserr;
480 loff_t offset;
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 */
490 count -= 2;
491 if (count < 0)
492 count = 0;
494 resp->buffer = argp->buffer;
495 resp->offset = NULL;
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;
504 if (resp->offset)
505 *resp->offset = htonl(offset);
507 fh_put(&argp->fh);
508 return nfserr;
512 * Get file system info
514 static __be32
515 nfsd_proc_statfs(struct svc_rqst * rqstp, struct nfsd_fhandle *argp,
516 struct nfsd_statfsres *resp)
518 __be32 nfserr;
520 dprintk("nfsd: STATFS %s\n", SVCFH_fmt(&argp->fh));
522 nfserr = nfsd_statfs(rqstp, &argp->fh, &resp->stats);
523 fh_put(&argp->fh);
524 return nfserr;
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), \
542 0, \
543 cache, \
544 respsize, \
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 = {
574 .vs_vers = 2,
575 .vs_nproc = 18,
576 .vs_proc = nfsd_procedures2,
577 .vs_dispatch = nfsd_dispatch,
578 .vs_xdrsize = NFS2_SVC_XDRSIZE,
582 * Map errnos to NFS errnos.
584 __be32
585 nfserrno (int errno)
587 static struct {
588 __be32 nfserr;
589 int syserr;
590 } nfs_errtbl[] = {
591 { nfs_ok, 0 },
592 { nfserr_perm, -EPERM },
593 { nfserr_noent, -ENOENT },
594 { nfserr_io, -EIO },
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 },
610 #ifdef EDQUOT
611 { nfserr_dquot, -EDQUOT },
612 #endif
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 },
621 int i;
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);
628 return nfserr_io;