blob: 6c967e1ba37b774b5dd96855af818be26742b8f8 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * nfsproc2.c Process version 2 NFS requests.
3 * linux/fs/nfsd/nfs2proc.c
4 *
5 * Process version 2 NFS requests.
6 *
7 * Copyright (C) 1995-1997 Olaf Kirch <okir@monad.swb.de>
8 */
9
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>
21
Chuck Leverad06e4b2007-02-12 00:53:32 -080022#include <linux/sunrpc/clnt.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070023#include <linux/sunrpc/svc.h>
24#include <linux/nfsd/nfsd.h>
25#include <linux/nfsd/cache.h>
26#include <linux/nfsd/xdr.h>
J. Bruce Fields0a3adad2009-11-04 18:12:35 -050027#include "vfs.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070028
29typedef struct svc_rqst svc_rqst;
30typedef struct svc_buf svc_buf;
31
32#define NFSDDBG_FACILITY NFSDDBG_PROC
33
34
Al Viro7111c662006-10-19 23:28:45 -070035static __be32
Linus Torvalds1da177e2005-04-16 15:20:36 -070036nfsd_proc_null(struct svc_rqst *rqstp, void *argp, void *resp)
37{
38 return nfs_ok;
39}
40
Al Viroc4d987b2006-10-19 23:29:00 -070041static __be32
42nfsd_return_attrs(__be32 err, struct nfsd_attrstat *resp)
David Shaw846f2fc2006-01-18 17:43:51 -080043{
44 if (err) return err;
Jan Blunck54775492008-02-14 19:38:39 -080045 return nfserrno(vfs_getattr(resp->fh.fh_export->ex_path.mnt,
David Shaw846f2fc2006-01-18 17:43:51 -080046 resp->fh.fh_dentry,
47 &resp->stat));
48}
Al Viroc4d987b2006-10-19 23:29:00 -070049static __be32
50nfsd_return_dirop(__be32 err, struct nfsd_diropres *resp)
David Shaw846f2fc2006-01-18 17:43:51 -080051{
52 if (err) return err;
Jan Blunck54775492008-02-14 19:38:39 -080053 return nfserrno(vfs_getattr(resp->fh.fh_export->ex_path.mnt,
David Shaw846f2fc2006-01-18 17:43:51 -080054 resp->fh.fh_dentry,
55 &resp->stat));
56}
Linus Torvalds1da177e2005-04-16 15:20:36 -070057/*
58 * Get a file's attributes
59 * N.B. After this call resp->fh needs an fh_put
60 */
Al Viro7111c662006-10-19 23:28:45 -070061static __be32
Linus Torvalds1da177e2005-04-16 15:20:36 -070062nfsd_proc_getattr(struct svc_rqst *rqstp, struct nfsd_fhandle *argp,
63 struct nfsd_attrstat *resp)
64{
Al Viroc4d987b2006-10-19 23:29:00 -070065 __be32 nfserr;
Linus Torvalds1da177e2005-04-16 15:20:36 -070066 dprintk("nfsd: GETATTR %s\n", SVCFH_fmt(&argp->fh));
67
68 fh_copy(&resp->fh, &argp->fh);
J. Bruce Fields04716e62008-08-07 13:00:20 -040069 nfserr = fh_verify(rqstp, &resp->fh, 0,
70 NFSD_MAY_NOP | NFSD_MAY_BYPASS_GSS_ON_ROOT);
David Shaw846f2fc2006-01-18 17:43:51 -080071 return nfsd_return_attrs(nfserr, resp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070072}
73
74/*
75 * Set a file's attributes
76 * N.B. After this call resp->fh needs an fh_put
77 */
Al Viro7111c662006-10-19 23:28:45 -070078static __be32
Linus Torvalds1da177e2005-04-16 15:20:36 -070079nfsd_proc_setattr(struct svc_rqst *rqstp, struct nfsd_sattrargs *argp,
80 struct nfsd_attrstat *resp)
81{
Al Viroc4d987b2006-10-19 23:29:00 -070082 __be32 nfserr;
Linus Torvalds1da177e2005-04-16 15:20:36 -070083 dprintk("nfsd: SETATTR %s, valid=%x, size=%ld\n",
84 SVCFH_fmt(&argp->fh),
85 argp->attrs.ia_valid, (long) argp->attrs.ia_size);
86
87 fh_copy(&resp->fh, &argp->fh);
David Shaw846f2fc2006-01-18 17:43:51 -080088 nfserr = nfsd_setattr(rqstp, &resp->fh, &argp->attrs,0, (time_t)0);
89 return nfsd_return_attrs(nfserr, resp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070090}
91
92/*
93 * Look up a path name component
94 * Note: the dentry in the resp->fh may be negative if the file
95 * doesn't exist yet.
96 * N.B. After this call resp->fh needs an fh_put
97 */
Al Viro7111c662006-10-19 23:28:45 -070098static __be32
Linus Torvalds1da177e2005-04-16 15:20:36 -070099nfsd_proc_lookup(struct svc_rqst *rqstp, struct nfsd_diropargs *argp,
100 struct nfsd_diropres *resp)
101{
Al Viroc4d987b2006-10-19 23:29:00 -0700102 __be32 nfserr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700103
104 dprintk("nfsd: LOOKUP %s %.*s\n",
105 SVCFH_fmt(&argp->fh), argp->len, argp->name);
106
107 fh_init(&resp->fh, NFS_FHSIZE);
108 nfserr = nfsd_lookup(rqstp, &argp->fh, argp->name, argp->len,
109 &resp->fh);
110
111 fh_put(&argp->fh);
David Shaw846f2fc2006-01-18 17:43:51 -0800112 return nfsd_return_dirop(nfserr, resp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700113}
114
115/*
116 * Read a symlink.
117 */
Al Viro7111c662006-10-19 23:28:45 -0700118static __be32
Linus Torvalds1da177e2005-04-16 15:20:36 -0700119nfsd_proc_readlink(struct svc_rqst *rqstp, struct nfsd_readlinkargs *argp,
120 struct nfsd_readlinkres *resp)
121{
Al Viroc4d987b2006-10-19 23:29:00 -0700122 __be32 nfserr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700123
124 dprintk("nfsd: READLINK %s\n", SVCFH_fmt(&argp->fh));
125
126 /* Read the symlink. */
127 resp->len = NFS_MAXPATHLEN;
128 nfserr = nfsd_readlink(rqstp, &argp->fh, argp->buffer, &resp->len);
129
130 fh_put(&argp->fh);
131 return nfserr;
132}
133
134/*
135 * Read a portion of a file.
136 * N.B. After this call resp->fh needs an fh_put
137 */
Al Viro7111c662006-10-19 23:28:45 -0700138static __be32
Linus Torvalds1da177e2005-04-16 15:20:36 -0700139nfsd_proc_read(struct svc_rqst *rqstp, struct nfsd_readargs *argp,
140 struct nfsd_readres *resp)
141{
Al Viroc4d987b2006-10-19 23:29:00 -0700142 __be32 nfserr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700143
144 dprintk("nfsd: READ %s %d bytes at %d\n",
145 SVCFH_fmt(&argp->fh),
146 argp->count, argp->offset);
147
148 /* Obtain buffer pointer for payload. 19 is 1 word for
149 * status, 17 words for fattr, and 1 word for the byte count.
150 */
151
Greg Banks7adae482006-10-04 02:15:47 -0700152 if (NFSSVC_MAXBLKSIZE_V2 < argp->count) {
Chuck Leverad06e4b2007-02-12 00:53:32 -0800153 char buf[RPC_MAX_ADDRBUFLEN];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700154 printk(KERN_NOTICE
Chuck Leverad06e4b2007-02-12 00:53:32 -0800155 "oversized read request from %s (%d bytes)\n",
156 svc_print_addr(rqstp, buf, sizeof(buf)),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700157 argp->count);
Greg Banks7adae482006-10-04 02:15:47 -0700158 argp->count = NFSSVC_MAXBLKSIZE_V2;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700159 }
Jeff Laytoncd123012007-05-09 02:34:50 -0700160 svc_reserve_auth(rqstp, (19<<2) + argp->count + 4);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700161
162 resp->count = argp->count;
163 nfserr = nfsd_read(rqstp, fh_copy(&resp->fh, &argp->fh), NULL,
164 argp->offset,
NeilBrown3cc03b12006-10-04 02:15:47 -0700165 rqstp->rq_vec, argp->vlen,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700166 &resp->count);
167
David Shaw846f2fc2006-01-18 17:43:51 -0800168 if (nfserr) return nfserr;
Jan Blunck54775492008-02-14 19:38:39 -0800169 return nfserrno(vfs_getattr(resp->fh.fh_export->ex_path.mnt,
David Shaw846f2fc2006-01-18 17:43:51 -0800170 resp->fh.fh_dentry,
171 &resp->stat));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172}
173
174/*
175 * Write data to a file
176 * N.B. After this call resp->fh needs an fh_put
177 */
Al Viro7111c662006-10-19 23:28:45 -0700178static __be32
Linus Torvalds1da177e2005-04-16 15:20:36 -0700179nfsd_proc_write(struct svc_rqst *rqstp, struct nfsd_writeargs *argp,
180 struct nfsd_attrstat *resp)
181{
Al Viroc4d987b2006-10-19 23:29:00 -0700182 __be32 nfserr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700183 int stable = 1;
David Shaw31dec252009-03-05 20:16:14 -0500184 unsigned long cnt = argp->len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700185
186 dprintk("nfsd: WRITE %s %d bytes at %d\n",
187 SVCFH_fmt(&argp->fh),
188 argp->len, argp->offset);
189
190 nfserr = nfsd_write(rqstp, fh_copy(&resp->fh, &argp->fh), NULL,
191 argp->offset,
NeilBrown3cc03b12006-10-04 02:15:47 -0700192 rqstp->rq_vec, argp->vlen,
David Shaw31dec252009-03-05 20:16:14 -0500193 &cnt,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700194 &stable);
David Shaw846f2fc2006-01-18 17:43:51 -0800195 return nfsd_return_attrs(nfserr, resp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700196}
197
198/*
199 * CREATE processing is complicated. The keyword here is `overloaded.'
200 * The parent directory is kept locked between the check for existence
201 * and the actual create() call in compliance with VFS protocols.
202 * N.B. After this call _both_ argp->fh and resp->fh need an fh_put
203 */
Al Viro7111c662006-10-19 23:28:45 -0700204static __be32
Linus Torvalds1da177e2005-04-16 15:20:36 -0700205nfsd_proc_create(struct svc_rqst *rqstp, struct nfsd_createargs *argp,
206 struct nfsd_diropres *resp)
207{
208 svc_fh *dirfhp = &argp->fh;
209 svc_fh *newfhp = &resp->fh;
210 struct iattr *attr = &argp->attrs;
211 struct inode *inode;
212 struct dentry *dchild;
Al Viroc4d987b2006-10-19 23:29:00 -0700213 int type, mode;
214 __be32 nfserr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700215 dev_t rdev = 0, wanted = new_decode_dev(attr->ia_size);
216
217 dprintk("nfsd: CREATE %s %.*s\n",
218 SVCFH_fmt(dirfhp), argp->len, argp->name);
219
220 /* First verify the parent file handle */
Miklos Szeredi8837abc2008-06-16 13:20:29 +0200221 nfserr = fh_verify(rqstp, dirfhp, S_IFDIR, NFSD_MAY_EXEC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700222 if (nfserr)
223 goto done; /* must fh_put dirfhp even on error */
224
Miklos Szeredi8837abc2008-06-16 13:20:29 +0200225 /* Check for NFSD_MAY_WRITE in nfsd_create if necessary */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700226
227 nfserr = nfserr_acces;
228 if (!argp->len)
229 goto done;
230 nfserr = nfserr_exist;
231 if (isdotent(argp->name, argp->len))
232 goto done;
NeilBrown7ed94292006-10-04 02:15:43 -0700233 fh_lock_nested(dirfhp, I_MUTEX_PARENT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700234 dchild = lookup_one_len(argp->name, dirfhp->fh_dentry, argp->len);
235 if (IS_ERR(dchild)) {
236 nfserr = nfserrno(PTR_ERR(dchild));
237 goto out_unlock;
238 }
239 fh_init(newfhp, NFS_FHSIZE);
240 nfserr = fh_compose(newfhp, dirfhp->fh_export, dchild, dirfhp);
241 if (!nfserr && !dchild->d_inode)
242 nfserr = nfserr_noent;
243 dput(dchild);
244 if (nfserr) {
245 if (nfserr != nfserr_noent)
246 goto out_unlock;
247 /*
248 * If the new file handle wasn't verified, we can't tell
249 * whether the file exists or not. Time to bail ...
250 */
251 nfserr = nfserr_acces;
252 if (!newfhp->fh_dentry) {
253 printk(KERN_WARNING
254 "nfsd_proc_create: file handle not verified\n");
255 goto out_unlock;
256 }
257 }
258
259 inode = newfhp->fh_dentry->d_inode;
260
261 /* Unfudge the mode bits */
262 if (attr->ia_valid & ATTR_MODE) {
263 type = attr->ia_mode & S_IFMT;
264 mode = attr->ia_mode & ~S_IFMT;
265 if (!type) {
266 /* no type, so if target exists, assume same as that,
267 * else assume a file */
268 if (inode) {
269 type = inode->i_mode & S_IFMT;
270 switch(type) {
271 case S_IFCHR:
272 case S_IFBLK:
273 /* reserve rdev for later checking */
274 rdev = inode->i_rdev;
275 attr->ia_valid |= ATTR_SIZE;
276
277 /* FALLTHROUGH */
278 case S_IFIFO:
279 /* this is probably a permission check..
280 * at least IRIX implements perm checking on
281 * echo thing > device-special-file-or-pipe
282 * by doing a CREATE with type==0
283 */
J. Bruce Fields0ec757d2007-07-17 04:04:48 -0700284 nfserr = nfsd_permission(rqstp,
285 newfhp->fh_export,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700286 newfhp->fh_dentry,
Miklos Szeredi8837abc2008-06-16 13:20:29 +0200287 NFSD_MAY_WRITE|NFSD_MAY_LOCAL_ACCESS);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700288 if (nfserr && nfserr != nfserr_rofs)
289 goto out_unlock;
290 }
291 } else
292 type = S_IFREG;
293 }
294 } else if (inode) {
295 type = inode->i_mode & S_IFMT;
296 mode = inode->i_mode & ~S_IFMT;
297 } else {
298 type = S_IFREG;
299 mode = 0; /* ??? */
300 }
301
302 attr->ia_valid |= ATTR_MODE;
303 attr->ia_mode = mode;
304
305 /* Special treatment for non-regular files according to the
306 * gospel of sun micro
307 */
308 if (type != S_IFREG) {
309 int is_borc = 0;
310 if (type != S_IFBLK && type != S_IFCHR) {
311 rdev = 0;
312 } else if (type == S_IFCHR && !(attr->ia_valid & ATTR_SIZE)) {
313 /* If you think you've seen the worst, grok this. */
314 type = S_IFIFO;
315 } else {
316 /* Okay, char or block special */
317 is_borc = 1;
318 if (!rdev)
319 rdev = wanted;
320 }
321
322 /* we've used the SIZE information, so discard it */
323 attr->ia_valid &= ~ATTR_SIZE;
324
325 /* Make sure the type and device matches */
326 nfserr = nfserr_exist;
327 if (inode && type != (inode->i_mode & S_IFMT))
328 goto out_unlock;
329 }
330
331 nfserr = 0;
332 if (!inode) {
333 /* File doesn't exist. Create it and set attrs */
334 nfserr = nfsd_create(rqstp, dirfhp, argp->name, argp->len,
335 attr, type, rdev, newfhp);
336 } else if (type == S_IFREG) {
337 dprintk("nfsd: existing %s, valid=%x, size=%ld\n",
338 argp->name, attr->ia_valid, (long) attr->ia_size);
339 /* File already exists. We ignore all attributes except
340 * size, so that creat() behaves exactly like
341 * open(..., O_CREAT|O_TRUNC|O_WRONLY).
342 */
343 attr->ia_valid &= ATTR_SIZE;
344 if (attr->ia_valid)
345 nfserr = nfsd_setattr(rqstp, newfhp, attr, 0, (time_t)0);
346 }
347
348out_unlock:
349 /* We don't really need to unlock, as fh_put does it. */
350 fh_unlock(dirfhp);
351
352done:
353 fh_put(dirfhp);
David Shaw846f2fc2006-01-18 17:43:51 -0800354 return nfsd_return_dirop(nfserr, resp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700355}
356
Al Viro7111c662006-10-19 23:28:45 -0700357static __be32
Linus Torvalds1da177e2005-04-16 15:20:36 -0700358nfsd_proc_remove(struct svc_rqst *rqstp, struct nfsd_diropargs *argp,
359 void *resp)
360{
Al Viroc4d987b2006-10-19 23:29:00 -0700361 __be32 nfserr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700362
363 dprintk("nfsd: REMOVE %s %.*s\n", SVCFH_fmt(&argp->fh),
364 argp->len, argp->name);
365
366 /* Unlink. -SIFDIR means file must not be a directory */
367 nfserr = nfsd_unlink(rqstp, &argp->fh, -S_IFDIR, argp->name, argp->len);
368 fh_put(&argp->fh);
369 return nfserr;
370}
371
Al Viro7111c662006-10-19 23:28:45 -0700372static __be32
Linus Torvalds1da177e2005-04-16 15:20:36 -0700373nfsd_proc_rename(struct svc_rqst *rqstp, struct nfsd_renameargs *argp,
374 void *resp)
375{
Al Viroc4d987b2006-10-19 23:29:00 -0700376 __be32 nfserr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700377
378 dprintk("nfsd: RENAME %s %.*s -> \n",
379 SVCFH_fmt(&argp->ffh), argp->flen, argp->fname);
380 dprintk("nfsd: -> %s %.*s\n",
381 SVCFH_fmt(&argp->tfh), argp->tlen, argp->tname);
382
383 nfserr = nfsd_rename(rqstp, &argp->ffh, argp->fname, argp->flen,
384 &argp->tfh, argp->tname, argp->tlen);
385 fh_put(&argp->ffh);
386 fh_put(&argp->tfh);
387 return nfserr;
388}
389
Al Viro7111c662006-10-19 23:28:45 -0700390static __be32
Linus Torvalds1da177e2005-04-16 15:20:36 -0700391nfsd_proc_link(struct svc_rqst *rqstp, struct nfsd_linkargs *argp,
392 void *resp)
393{
Al Viroc4d987b2006-10-19 23:29:00 -0700394 __be32 nfserr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700395
396 dprintk("nfsd: LINK %s ->\n",
397 SVCFH_fmt(&argp->ffh));
398 dprintk("nfsd: %s %.*s\n",
399 SVCFH_fmt(&argp->tfh),
400 argp->tlen,
401 argp->tname);
402
403 nfserr = nfsd_link(rqstp, &argp->tfh, argp->tname, argp->tlen,
404 &argp->ffh);
405 fh_put(&argp->ffh);
406 fh_put(&argp->tfh);
407 return nfserr;
408}
409
Al Viro7111c662006-10-19 23:28:45 -0700410static __be32
Linus Torvalds1da177e2005-04-16 15:20:36 -0700411nfsd_proc_symlink(struct svc_rqst *rqstp, struct nfsd_symlinkargs *argp,
412 void *resp)
413{
414 struct svc_fh newfh;
Al Viroc4d987b2006-10-19 23:29:00 -0700415 __be32 nfserr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700416
417 dprintk("nfsd: SYMLINK %s %.*s -> %.*s\n",
418 SVCFH_fmt(&argp->ffh), argp->flen, argp->fname,
419 argp->tlen, argp->tname);
420
421 fh_init(&newfh, NFS_FHSIZE);
422 /*
423 * Create the link, look up new file and set attrs.
424 */
425 nfserr = nfsd_symlink(rqstp, &argp->ffh, argp->fname, argp->flen,
426 argp->tname, argp->tlen,
427 &newfh, &argp->attrs);
428
429
430 fh_put(&argp->ffh);
431 fh_put(&newfh);
432 return nfserr;
433}
434
435/*
436 * Make directory. This operation is not idempotent.
437 * N.B. After this call resp->fh needs an fh_put
438 */
Al Viro7111c662006-10-19 23:28:45 -0700439static __be32
Linus Torvalds1da177e2005-04-16 15:20:36 -0700440nfsd_proc_mkdir(struct svc_rqst *rqstp, struct nfsd_createargs *argp,
441 struct nfsd_diropres *resp)
442{
Al Viroc4d987b2006-10-19 23:29:00 -0700443 __be32 nfserr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700444
445 dprintk("nfsd: MKDIR %s %.*s\n", SVCFH_fmt(&argp->fh), argp->len, argp->name);
446
447 if (resp->fh.fh_dentry) {
448 printk(KERN_WARNING
449 "nfsd_proc_mkdir: response already verified??\n");
450 }
451
452 argp->attrs.ia_valid &= ~ATTR_SIZE;
453 fh_init(&resp->fh, NFS_FHSIZE);
454 nfserr = nfsd_create(rqstp, &argp->fh, argp->name, argp->len,
455 &argp->attrs, S_IFDIR, 0, &resp->fh);
456 fh_put(&argp->fh);
David Shaw846f2fc2006-01-18 17:43:51 -0800457 return nfsd_return_dirop(nfserr, resp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700458}
459
460/*
461 * Remove a directory
462 */
Al Viro7111c662006-10-19 23:28:45 -0700463static __be32
Linus Torvalds1da177e2005-04-16 15:20:36 -0700464nfsd_proc_rmdir(struct svc_rqst *rqstp, struct nfsd_diropargs *argp,
465 void *resp)
466{
Al Viroc4d987b2006-10-19 23:29:00 -0700467 __be32 nfserr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700468
469 dprintk("nfsd: RMDIR %s %.*s\n", SVCFH_fmt(&argp->fh), argp->len, argp->name);
470
471 nfserr = nfsd_unlink(rqstp, &argp->fh, S_IFDIR, argp->name, argp->len);
472 fh_put(&argp->fh);
473 return nfserr;
474}
475
476/*
477 * Read a portion of a directory.
478 */
Al Viro7111c662006-10-19 23:28:45 -0700479static __be32
Linus Torvalds1da177e2005-04-16 15:20:36 -0700480nfsd_proc_readdir(struct svc_rqst *rqstp, struct nfsd_readdirargs *argp,
481 struct nfsd_readdirres *resp)
482{
Al Viroc4d987b2006-10-19 23:29:00 -0700483 int count;
484 __be32 nfserr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700485 loff_t offset;
486
487 dprintk("nfsd: READDIR %s %d bytes at %d\n",
488 SVCFH_fmt(&argp->fh),
489 argp->count, argp->cookie);
490
491 /* Shrink to the client read size */
492 count = (argp->count >> 2) - 2;
493
494 /* Make sure we've room for the NULL ptr & eof flag */
495 count -= 2;
496 if (count < 0)
497 count = 0;
498
499 resp->buffer = argp->buffer;
500 resp->offset = NULL;
501 resp->buflen = count;
502 resp->common.err = nfs_ok;
503 /* Read directory and encode entries on the fly */
504 offset = argp->cookie;
505 nfserr = nfsd_readdir(rqstp, &argp->fh, &offset,
506 &resp->common, nfssvc_encode_entry);
507
508 resp->count = resp->buffer - argp->buffer;
509 if (resp->offset)
510 *resp->offset = htonl(offset);
511
512 fh_put(&argp->fh);
513 return nfserr;
514}
515
516/*
517 * Get file system info
518 */
Al Viro7111c662006-10-19 23:28:45 -0700519static __be32
Linus Torvalds1da177e2005-04-16 15:20:36 -0700520nfsd_proc_statfs(struct svc_rqst * rqstp, struct nfsd_fhandle *argp,
521 struct nfsd_statfsres *resp)
522{
Al Viroc4d987b2006-10-19 23:29:00 -0700523 __be32 nfserr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700524
525 dprintk("nfsd: STATFS %s\n", SVCFH_fmt(&argp->fh));
526
J. Bruce Fields04716e62008-08-07 13:00:20 -0400527 nfserr = nfsd_statfs(rqstp, &argp->fh, &resp->stats,
528 NFSD_MAY_BYPASS_GSS_ON_ROOT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700529 fh_put(&argp->fh);
530 return nfserr;
531}
532
533/*
534 * NFSv2 Server procedures.
535 * Only the results of non-idempotent operations are cached.
536 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700537struct nfsd_void { int dummy; };
538
Linus Torvalds1da177e2005-04-16 15:20:36 -0700539#define ST 1 /* status */
540#define FH 8 /* filehandle */
541#define AT 18 /* attributes */
542
543static struct svc_procedure nfsd_procedures2[18] = {
Yu Zhiguob9081d92009-06-09 17:33:34 +0800544 [NFSPROC_NULL] = {
545 .pc_func = (svc_procfunc) nfsd_proc_null,
546 .pc_decode = (kxdrproc_t) nfssvc_decode_void,
547 .pc_encode = (kxdrproc_t) nfssvc_encode_void,
548 .pc_argsize = sizeof(struct nfsd_void),
549 .pc_ressize = sizeof(struct nfsd_void),
550 .pc_cachetype = RC_NOCACHE,
551 .pc_xdrressize = ST,
552 },
553 [NFSPROC_GETATTR] = {
554 .pc_func = (svc_procfunc) nfsd_proc_getattr,
555 .pc_decode = (kxdrproc_t) nfssvc_decode_fhandle,
556 .pc_encode = (kxdrproc_t) nfssvc_encode_attrstat,
557 .pc_release = (kxdrproc_t) nfssvc_release_fhandle,
558 .pc_argsize = sizeof(struct nfsd_fhandle),
559 .pc_ressize = sizeof(struct nfsd_attrstat),
560 .pc_cachetype = RC_NOCACHE,
561 .pc_xdrressize = ST+AT,
562 },
563 [NFSPROC_SETATTR] = {
564 .pc_func = (svc_procfunc) nfsd_proc_setattr,
565 .pc_decode = (kxdrproc_t) nfssvc_decode_sattrargs,
566 .pc_encode = (kxdrproc_t) nfssvc_encode_attrstat,
567 .pc_release = (kxdrproc_t) nfssvc_release_fhandle,
568 .pc_argsize = sizeof(struct nfsd_sattrargs),
569 .pc_ressize = sizeof(struct nfsd_attrstat),
570 .pc_cachetype = RC_REPLBUFF,
571 .pc_xdrressize = ST+AT,
572 },
573 [NFSPROC_ROOT] = {
574 .pc_decode = (kxdrproc_t) nfssvc_decode_void,
575 .pc_encode = (kxdrproc_t) nfssvc_encode_void,
576 .pc_argsize = sizeof(struct nfsd_void),
577 .pc_ressize = sizeof(struct nfsd_void),
578 .pc_cachetype = RC_NOCACHE,
579 .pc_xdrressize = ST,
580 },
581 [NFSPROC_LOOKUP] = {
582 .pc_func = (svc_procfunc) nfsd_proc_lookup,
583 .pc_decode = (kxdrproc_t) nfssvc_decode_diropargs,
584 .pc_encode = (kxdrproc_t) nfssvc_encode_diropres,
585 .pc_release = (kxdrproc_t) nfssvc_release_fhandle,
586 .pc_argsize = sizeof(struct nfsd_diropargs),
587 .pc_ressize = sizeof(struct nfsd_diropres),
588 .pc_cachetype = RC_NOCACHE,
589 .pc_xdrressize = ST+FH+AT,
590 },
591 [NFSPROC_READLINK] = {
592 .pc_func = (svc_procfunc) nfsd_proc_readlink,
593 .pc_decode = (kxdrproc_t) nfssvc_decode_readlinkargs,
594 .pc_encode = (kxdrproc_t) nfssvc_encode_readlinkres,
595 .pc_argsize = sizeof(struct nfsd_readlinkargs),
596 .pc_ressize = sizeof(struct nfsd_readlinkres),
597 .pc_cachetype = RC_NOCACHE,
598 .pc_xdrressize = ST+1+NFS_MAXPATHLEN/4,
599 },
600 [NFSPROC_READ] = {
601 .pc_func = (svc_procfunc) nfsd_proc_read,
602 .pc_decode = (kxdrproc_t) nfssvc_decode_readargs,
603 .pc_encode = (kxdrproc_t) nfssvc_encode_readres,
604 .pc_release = (kxdrproc_t) nfssvc_release_fhandle,
605 .pc_argsize = sizeof(struct nfsd_readargs),
606 .pc_ressize = sizeof(struct nfsd_readres),
607 .pc_cachetype = RC_NOCACHE,
608 .pc_xdrressize = ST+AT+1+NFSSVC_MAXBLKSIZE_V2/4,
609 },
610 [NFSPROC_WRITECACHE] = {
611 .pc_decode = (kxdrproc_t) nfssvc_decode_void,
612 .pc_encode = (kxdrproc_t) nfssvc_encode_void,
613 .pc_argsize = sizeof(struct nfsd_void),
614 .pc_ressize = sizeof(struct nfsd_void),
615 .pc_cachetype = RC_NOCACHE,
616 .pc_xdrressize = ST,
617 },
618 [NFSPROC_WRITE] = {
619 .pc_func = (svc_procfunc) nfsd_proc_write,
620 .pc_decode = (kxdrproc_t) nfssvc_decode_writeargs,
621 .pc_encode = (kxdrproc_t) nfssvc_encode_attrstat,
622 .pc_release = (kxdrproc_t) nfssvc_release_fhandle,
623 .pc_argsize = sizeof(struct nfsd_writeargs),
624 .pc_ressize = sizeof(struct nfsd_attrstat),
625 .pc_cachetype = RC_REPLBUFF,
626 .pc_xdrressize = ST+AT,
627 },
628 [NFSPROC_CREATE] = {
629 .pc_func = (svc_procfunc) nfsd_proc_create,
630 .pc_decode = (kxdrproc_t) nfssvc_decode_createargs,
631 .pc_encode = (kxdrproc_t) nfssvc_encode_diropres,
632 .pc_release = (kxdrproc_t) nfssvc_release_fhandle,
633 .pc_argsize = sizeof(struct nfsd_createargs),
634 .pc_ressize = sizeof(struct nfsd_diropres),
635 .pc_cachetype = RC_REPLBUFF,
636 .pc_xdrressize = ST+FH+AT,
637 },
638 [NFSPROC_REMOVE] = {
639 .pc_func = (svc_procfunc) nfsd_proc_remove,
640 .pc_decode = (kxdrproc_t) nfssvc_decode_diropargs,
641 .pc_encode = (kxdrproc_t) nfssvc_encode_void,
642 .pc_argsize = sizeof(struct nfsd_diropargs),
643 .pc_ressize = sizeof(struct nfsd_void),
644 .pc_cachetype = RC_REPLSTAT,
645 .pc_xdrressize = ST,
646 },
647 [NFSPROC_RENAME] = {
648 .pc_func = (svc_procfunc) nfsd_proc_rename,
649 .pc_decode = (kxdrproc_t) nfssvc_decode_renameargs,
650 .pc_encode = (kxdrproc_t) nfssvc_encode_void,
651 .pc_argsize = sizeof(struct nfsd_renameargs),
652 .pc_ressize = sizeof(struct nfsd_void),
653 .pc_cachetype = RC_REPLSTAT,
654 .pc_xdrressize = ST,
655 },
656 [NFSPROC_LINK] = {
657 .pc_func = (svc_procfunc) nfsd_proc_link,
658 .pc_decode = (kxdrproc_t) nfssvc_decode_linkargs,
659 .pc_encode = (kxdrproc_t) nfssvc_encode_void,
660 .pc_argsize = sizeof(struct nfsd_linkargs),
661 .pc_ressize = sizeof(struct nfsd_void),
662 .pc_cachetype = RC_REPLSTAT,
663 .pc_xdrressize = ST,
664 },
665 [NFSPROC_SYMLINK] = {
666 .pc_func = (svc_procfunc) nfsd_proc_symlink,
667 .pc_decode = (kxdrproc_t) nfssvc_decode_symlinkargs,
668 .pc_encode = (kxdrproc_t) nfssvc_encode_void,
669 .pc_argsize = sizeof(struct nfsd_symlinkargs),
670 .pc_ressize = sizeof(struct nfsd_void),
671 .pc_cachetype = RC_REPLSTAT,
672 .pc_xdrressize = ST,
673 },
674 [NFSPROC_MKDIR] = {
675 .pc_func = (svc_procfunc) nfsd_proc_mkdir,
676 .pc_decode = (kxdrproc_t) nfssvc_decode_createargs,
677 .pc_encode = (kxdrproc_t) nfssvc_encode_diropres,
678 .pc_release = (kxdrproc_t) nfssvc_release_fhandle,
679 .pc_argsize = sizeof(struct nfsd_createargs),
680 .pc_ressize = sizeof(struct nfsd_diropres),
681 .pc_cachetype = RC_REPLBUFF,
682 .pc_xdrressize = ST+FH+AT,
683 },
684 [NFSPROC_RMDIR] = {
685 .pc_func = (svc_procfunc) nfsd_proc_rmdir,
686 .pc_decode = (kxdrproc_t) nfssvc_decode_diropargs,
687 .pc_encode = (kxdrproc_t) nfssvc_encode_void,
688 .pc_argsize = sizeof(struct nfsd_diropargs),
689 .pc_ressize = sizeof(struct nfsd_void),
690 .pc_cachetype = RC_REPLSTAT,
691 .pc_xdrressize = ST,
692 },
693 [NFSPROC_READDIR] = {
694 .pc_func = (svc_procfunc) nfsd_proc_readdir,
695 .pc_decode = (kxdrproc_t) nfssvc_decode_readdirargs,
696 .pc_encode = (kxdrproc_t) nfssvc_encode_readdirres,
697 .pc_argsize = sizeof(struct nfsd_readdirargs),
698 .pc_ressize = sizeof(struct nfsd_readdirres),
699 .pc_cachetype = RC_NOCACHE,
700 },
701 [NFSPROC_STATFS] = {
702 .pc_func = (svc_procfunc) nfsd_proc_statfs,
703 .pc_decode = (kxdrproc_t) nfssvc_decode_fhandle,
704 .pc_encode = (kxdrproc_t) nfssvc_encode_statfsres,
705 .pc_argsize = sizeof(struct nfsd_fhandle),
706 .pc_ressize = sizeof(struct nfsd_statfsres),
707 .pc_cachetype = RC_NOCACHE,
708 .pc_xdrressize = ST+5,
709 },
Linus Torvalds1da177e2005-04-16 15:20:36 -0700710};
711
712
713struct svc_version nfsd_version2 = {
714 .vs_vers = 2,
715 .vs_nproc = 18,
716 .vs_proc = nfsd_procedures2,
717 .vs_dispatch = nfsd_dispatch,
718 .vs_xdrsize = NFS2_SVC_XDRSIZE,
719};
720
721/*
722 * Map errnos to NFS errnos.
723 */
Al Viro63f103112006-10-19 23:28:54 -0700724__be32
Linus Torvalds1da177e2005-04-16 15:20:36 -0700725nfserrno (int errno)
726{
727 static struct {
Al Viro63f103112006-10-19 23:28:54 -0700728 __be32 nfserr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700729 int syserr;
730 } nfs_errtbl[] = {
731 { nfs_ok, 0 },
732 { nfserr_perm, -EPERM },
733 { nfserr_noent, -ENOENT },
734 { nfserr_io, -EIO },
735 { nfserr_nxio, -ENXIO },
736 { nfserr_acces, -EACCES },
737 { nfserr_exist, -EEXIST },
738 { nfserr_xdev, -EXDEV },
739 { nfserr_mlink, -EMLINK },
740 { nfserr_nodev, -ENODEV },
741 { nfserr_notdir, -ENOTDIR },
742 { nfserr_isdir, -EISDIR },
743 { nfserr_inval, -EINVAL },
744 { nfserr_fbig, -EFBIG },
745 { nfserr_nospc, -ENOSPC },
746 { nfserr_rofs, -EROFS },
747 { nfserr_mlink, -EMLINK },
748 { nfserr_nametoolong, -ENAMETOOLONG },
749 { nfserr_notempty, -ENOTEMPTY },
750#ifdef EDQUOT
751 { nfserr_dquot, -EDQUOT },
752#endif
753 { nfserr_stale, -ESTALE },
754 { nfserr_jukebox, -ETIMEDOUT },
NeilBrown599eb302008-06-19 10:11:09 +1000755 { nfserr_jukebox, -ERESTARTSYS },
Linus Torvalds1da177e2005-04-16 15:20:36 -0700756 { nfserr_dropit, -EAGAIN },
757 { nfserr_dropit, -ENOMEM },
758 { nfserr_badname, -ESRCH },
759 { nfserr_io, -ETXTBSY },
Andreas Gruenbachera838cc42005-06-22 17:16:24 +0000760 { nfserr_notsupp, -EOPNOTSUPP },
Dean Hildebrandb7aeda42008-12-15 19:40:15 +0200761 { nfserr_toosmall, -ETOOSMALL },
J. Bruce Fieldsf39bde22009-09-27 14:41:43 -0400762 { nfserr_serverfault, -ESERVERFAULT },
Linus Torvalds1da177e2005-04-16 15:20:36 -0700763 };
764 int i;
765
Al Viro63f103112006-10-19 23:28:54 -0700766 for (i = 0; i < ARRAY_SIZE(nfs_errtbl); i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700767 if (nfs_errtbl[i].syserr == errno)
768 return nfs_errtbl[i].nfserr;
769 }
770 printk (KERN_INFO "nfsd: non-standard errno: %d\n", errno);
771 return nfserr_io;
772}
773