blob: 21a5f793c3d159953dc706e787a91eda89d15345 [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
Linus Torvalds1da177e2005-04-16 15:20:36 -070010#include <linux/namei.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070011
Boaz Harrosh9a74af22009-12-03 20:30:56 +020012#include "cache.h"
13#include "xdr.h"
J. Bruce Fields0a3adad2009-11-04 18:12:35 -050014#include "vfs.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070015
16typedef struct svc_rqst svc_rqst;
17typedef struct svc_buf svc_buf;
18
19#define NFSDDBG_FACILITY NFSDDBG_PROC
20
21
Al Viro7111c662006-10-19 23:28:45 -070022static __be32
Linus Torvalds1da177e2005-04-16 15:20:36 -070023nfsd_proc_null(struct svc_rqst *rqstp, void *argp, void *resp)
24{
25 return nfs_ok;
26}
27
Al Viroc4d987b2006-10-19 23:29:00 -070028static __be32
29nfsd_return_attrs(__be32 err, struct nfsd_attrstat *resp)
David Shaw846f2fc2006-01-18 17:43:51 -080030{
31 if (err) return err;
Jan Blunck54775492008-02-14 19:38:39 -080032 return nfserrno(vfs_getattr(resp->fh.fh_export->ex_path.mnt,
David Shaw846f2fc2006-01-18 17:43:51 -080033 resp->fh.fh_dentry,
34 &resp->stat));
35}
Al Viroc4d987b2006-10-19 23:29:00 -070036static __be32
37nfsd_return_dirop(__be32 err, struct nfsd_diropres *resp)
David Shaw846f2fc2006-01-18 17:43:51 -080038{
39 if (err) return err;
Jan Blunck54775492008-02-14 19:38:39 -080040 return nfserrno(vfs_getattr(resp->fh.fh_export->ex_path.mnt,
David Shaw846f2fc2006-01-18 17:43:51 -080041 resp->fh.fh_dentry,
42 &resp->stat));
43}
Linus Torvalds1da177e2005-04-16 15:20:36 -070044/*
45 * Get a file's attributes
46 * N.B. After this call resp->fh needs an fh_put
47 */
Al Viro7111c662006-10-19 23:28:45 -070048static __be32
Linus Torvalds1da177e2005-04-16 15:20:36 -070049nfsd_proc_getattr(struct svc_rqst *rqstp, struct nfsd_fhandle *argp,
50 struct nfsd_attrstat *resp)
51{
Al Viroc4d987b2006-10-19 23:29:00 -070052 __be32 nfserr;
Linus Torvalds1da177e2005-04-16 15:20:36 -070053 dprintk("nfsd: GETATTR %s\n", SVCFH_fmt(&argp->fh));
54
55 fh_copy(&resp->fh, &argp->fh);
J. Bruce Fields04716e62008-08-07 13:00:20 -040056 nfserr = fh_verify(rqstp, &resp->fh, 0,
57 NFSD_MAY_NOP | NFSD_MAY_BYPASS_GSS_ON_ROOT);
David Shaw846f2fc2006-01-18 17:43:51 -080058 return nfsd_return_attrs(nfserr, resp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070059}
60
61/*
62 * Set a file's attributes
63 * N.B. After this call resp->fh needs an fh_put
64 */
Al Viro7111c662006-10-19 23:28:45 -070065static __be32
Linus Torvalds1da177e2005-04-16 15:20:36 -070066nfsd_proc_setattr(struct svc_rqst *rqstp, struct nfsd_sattrargs *argp,
67 struct nfsd_attrstat *resp)
68{
Al Viroc4d987b2006-10-19 23:29:00 -070069 __be32 nfserr;
Linus Torvalds1da177e2005-04-16 15:20:36 -070070 dprintk("nfsd: SETATTR %s, valid=%x, size=%ld\n",
71 SVCFH_fmt(&argp->fh),
72 argp->attrs.ia_valid, (long) argp->attrs.ia_size);
73
74 fh_copy(&resp->fh, &argp->fh);
David Shaw846f2fc2006-01-18 17:43:51 -080075 nfserr = nfsd_setattr(rqstp, &resp->fh, &argp->attrs,0, (time_t)0);
76 return nfsd_return_attrs(nfserr, resp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070077}
78
79/*
80 * Look up a path name component
81 * Note: the dentry in the resp->fh may be negative if the file
82 * doesn't exist yet.
83 * N.B. After this call resp->fh needs an fh_put
84 */
Al Viro7111c662006-10-19 23:28:45 -070085static __be32
Linus Torvalds1da177e2005-04-16 15:20:36 -070086nfsd_proc_lookup(struct svc_rqst *rqstp, struct nfsd_diropargs *argp,
87 struct nfsd_diropres *resp)
88{
Al Viroc4d987b2006-10-19 23:29:00 -070089 __be32 nfserr;
Linus Torvalds1da177e2005-04-16 15:20:36 -070090
91 dprintk("nfsd: LOOKUP %s %.*s\n",
92 SVCFH_fmt(&argp->fh), argp->len, argp->name);
93
94 fh_init(&resp->fh, NFS_FHSIZE);
95 nfserr = nfsd_lookup(rqstp, &argp->fh, argp->name, argp->len,
96 &resp->fh);
97
98 fh_put(&argp->fh);
David Shaw846f2fc2006-01-18 17:43:51 -080099 return nfsd_return_dirop(nfserr, resp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700100}
101
102/*
103 * Read a symlink.
104 */
Al Viro7111c662006-10-19 23:28:45 -0700105static __be32
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106nfsd_proc_readlink(struct svc_rqst *rqstp, struct nfsd_readlinkargs *argp,
107 struct nfsd_readlinkres *resp)
108{
Al Viroc4d987b2006-10-19 23:29:00 -0700109 __be32 nfserr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700110
111 dprintk("nfsd: READLINK %s\n", SVCFH_fmt(&argp->fh));
112
113 /* Read the symlink. */
114 resp->len = NFS_MAXPATHLEN;
115 nfserr = nfsd_readlink(rqstp, &argp->fh, argp->buffer, &resp->len);
116
117 fh_put(&argp->fh);
118 return nfserr;
119}
120
121/*
122 * Read a portion of a file.
123 * N.B. After this call resp->fh needs an fh_put
124 */
Al Viro7111c662006-10-19 23:28:45 -0700125static __be32
Linus Torvalds1da177e2005-04-16 15:20:36 -0700126nfsd_proc_read(struct svc_rqst *rqstp, struct nfsd_readargs *argp,
127 struct nfsd_readres *resp)
128{
Al Viroc4d987b2006-10-19 23:29:00 -0700129 __be32 nfserr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700130
131 dprintk("nfsd: READ %s %d bytes at %d\n",
132 SVCFH_fmt(&argp->fh),
133 argp->count, argp->offset);
134
135 /* Obtain buffer pointer for payload. 19 is 1 word for
136 * status, 17 words for fattr, and 1 word for the byte count.
137 */
138
Greg Banks7adae482006-10-04 02:15:47 -0700139 if (NFSSVC_MAXBLKSIZE_V2 < argp->count) {
Chuck Leverad06e4b2007-02-12 00:53:32 -0800140 char buf[RPC_MAX_ADDRBUFLEN];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700141 printk(KERN_NOTICE
Chuck Leverad06e4b2007-02-12 00:53:32 -0800142 "oversized read request from %s (%d bytes)\n",
143 svc_print_addr(rqstp, buf, sizeof(buf)),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700144 argp->count);
Greg Banks7adae482006-10-04 02:15:47 -0700145 argp->count = NFSSVC_MAXBLKSIZE_V2;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700146 }
Jeff Laytoncd123012007-05-09 02:34:50 -0700147 svc_reserve_auth(rqstp, (19<<2) + argp->count + 4);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700148
149 resp->count = argp->count;
150 nfserr = nfsd_read(rqstp, fh_copy(&resp->fh, &argp->fh), NULL,
151 argp->offset,
NeilBrown3cc03b12006-10-04 02:15:47 -0700152 rqstp->rq_vec, argp->vlen,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700153 &resp->count);
154
David Shaw846f2fc2006-01-18 17:43:51 -0800155 if (nfserr) return nfserr;
Jan Blunck54775492008-02-14 19:38:39 -0800156 return nfserrno(vfs_getattr(resp->fh.fh_export->ex_path.mnt,
David Shaw846f2fc2006-01-18 17:43:51 -0800157 resp->fh.fh_dentry,
158 &resp->stat));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700159}
160
161/*
162 * Write data to a file
163 * N.B. After this call resp->fh needs an fh_put
164 */
Al Viro7111c662006-10-19 23:28:45 -0700165static __be32
Linus Torvalds1da177e2005-04-16 15:20:36 -0700166nfsd_proc_write(struct svc_rqst *rqstp, struct nfsd_writeargs *argp,
167 struct nfsd_attrstat *resp)
168{
Al Viroc4d987b2006-10-19 23:29:00 -0700169 __be32 nfserr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700170 int stable = 1;
David Shaw31dec252009-03-05 20:16:14 -0500171 unsigned long cnt = argp->len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172
173 dprintk("nfsd: WRITE %s %d bytes at %d\n",
174 SVCFH_fmt(&argp->fh),
175 argp->len, argp->offset);
176
177 nfserr = nfsd_write(rqstp, fh_copy(&resp->fh, &argp->fh), NULL,
178 argp->offset,
NeilBrown3cc03b12006-10-04 02:15:47 -0700179 rqstp->rq_vec, argp->vlen,
David Shaw31dec252009-03-05 20:16:14 -0500180 &cnt,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700181 &stable);
David Shaw846f2fc2006-01-18 17:43:51 -0800182 return nfsd_return_attrs(nfserr, resp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700183}
184
185/*
186 * CREATE processing is complicated. The keyword here is `overloaded.'
187 * The parent directory is kept locked between the check for existence
188 * and the actual create() call in compliance with VFS protocols.
189 * N.B. After this call _both_ argp->fh and resp->fh need an fh_put
190 */
Al Viro7111c662006-10-19 23:28:45 -0700191static __be32
Linus Torvalds1da177e2005-04-16 15:20:36 -0700192nfsd_proc_create(struct svc_rqst *rqstp, struct nfsd_createargs *argp,
193 struct nfsd_diropres *resp)
194{
195 svc_fh *dirfhp = &argp->fh;
196 svc_fh *newfhp = &resp->fh;
197 struct iattr *attr = &argp->attrs;
198 struct inode *inode;
199 struct dentry *dchild;
Al Viroc4d987b2006-10-19 23:29:00 -0700200 int type, mode;
201 __be32 nfserr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700202 dev_t rdev = 0, wanted = new_decode_dev(attr->ia_size);
203
204 dprintk("nfsd: CREATE %s %.*s\n",
205 SVCFH_fmt(dirfhp), argp->len, argp->name);
206
207 /* First verify the parent file handle */
Miklos Szeredi8837abc2008-06-16 13:20:29 +0200208 nfserr = fh_verify(rqstp, dirfhp, S_IFDIR, NFSD_MAY_EXEC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700209 if (nfserr)
210 goto done; /* must fh_put dirfhp even on error */
211
Miklos Szeredi8837abc2008-06-16 13:20:29 +0200212 /* Check for NFSD_MAY_WRITE in nfsd_create if necessary */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700213
214 nfserr = nfserr_acces;
215 if (!argp->len)
216 goto done;
217 nfserr = nfserr_exist;
218 if (isdotent(argp->name, argp->len))
219 goto done;
NeilBrown7ed94292006-10-04 02:15:43 -0700220 fh_lock_nested(dirfhp, I_MUTEX_PARENT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700221 dchild = lookup_one_len(argp->name, dirfhp->fh_dentry, argp->len);
222 if (IS_ERR(dchild)) {
223 nfserr = nfserrno(PTR_ERR(dchild));
224 goto out_unlock;
225 }
226 fh_init(newfhp, NFS_FHSIZE);
227 nfserr = fh_compose(newfhp, dirfhp->fh_export, dchild, dirfhp);
228 if (!nfserr && !dchild->d_inode)
229 nfserr = nfserr_noent;
230 dput(dchild);
231 if (nfserr) {
232 if (nfserr != nfserr_noent)
233 goto out_unlock;
234 /*
235 * If the new file handle wasn't verified, we can't tell
236 * whether the file exists or not. Time to bail ...
237 */
238 nfserr = nfserr_acces;
239 if (!newfhp->fh_dentry) {
240 printk(KERN_WARNING
241 "nfsd_proc_create: file handle not verified\n");
242 goto out_unlock;
243 }
244 }
245
246 inode = newfhp->fh_dentry->d_inode;
247
248 /* Unfudge the mode bits */
249 if (attr->ia_valid & ATTR_MODE) {
250 type = attr->ia_mode & S_IFMT;
251 mode = attr->ia_mode & ~S_IFMT;
252 if (!type) {
253 /* no type, so if target exists, assume same as that,
254 * else assume a file */
255 if (inode) {
256 type = inode->i_mode & S_IFMT;
257 switch(type) {
258 case S_IFCHR:
259 case S_IFBLK:
260 /* reserve rdev for later checking */
261 rdev = inode->i_rdev;
262 attr->ia_valid |= ATTR_SIZE;
263
264 /* FALLTHROUGH */
265 case S_IFIFO:
266 /* this is probably a permission check..
267 * at least IRIX implements perm checking on
268 * echo thing > device-special-file-or-pipe
269 * by doing a CREATE with type==0
270 */
J. Bruce Fields0ec757d2007-07-17 04:04:48 -0700271 nfserr = nfsd_permission(rqstp,
272 newfhp->fh_export,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700273 newfhp->fh_dentry,
Miklos Szeredi8837abc2008-06-16 13:20:29 +0200274 NFSD_MAY_WRITE|NFSD_MAY_LOCAL_ACCESS);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700275 if (nfserr && nfserr != nfserr_rofs)
276 goto out_unlock;
277 }
278 } else
279 type = S_IFREG;
280 }
281 } else if (inode) {
282 type = inode->i_mode & S_IFMT;
283 mode = inode->i_mode & ~S_IFMT;
284 } else {
285 type = S_IFREG;
286 mode = 0; /* ??? */
287 }
288
289 attr->ia_valid |= ATTR_MODE;
290 attr->ia_mode = mode;
291
292 /* Special treatment for non-regular files according to the
293 * gospel of sun micro
294 */
295 if (type != S_IFREG) {
296 int is_borc = 0;
297 if (type != S_IFBLK && type != S_IFCHR) {
298 rdev = 0;
299 } else if (type == S_IFCHR && !(attr->ia_valid & ATTR_SIZE)) {
300 /* If you think you've seen the worst, grok this. */
301 type = S_IFIFO;
302 } else {
303 /* Okay, char or block special */
304 is_borc = 1;
305 if (!rdev)
306 rdev = wanted;
307 }
308
309 /* we've used the SIZE information, so discard it */
310 attr->ia_valid &= ~ATTR_SIZE;
311
312 /* Make sure the type and device matches */
313 nfserr = nfserr_exist;
314 if (inode && type != (inode->i_mode & S_IFMT))
315 goto out_unlock;
316 }
317
318 nfserr = 0;
319 if (!inode) {
320 /* File doesn't exist. Create it and set attrs */
321 nfserr = nfsd_create(rqstp, dirfhp, argp->name, argp->len,
322 attr, type, rdev, newfhp);
323 } else if (type == S_IFREG) {
324 dprintk("nfsd: existing %s, valid=%x, size=%ld\n",
325 argp->name, attr->ia_valid, (long) attr->ia_size);
326 /* File already exists. We ignore all attributes except
327 * size, so that creat() behaves exactly like
328 * open(..., O_CREAT|O_TRUNC|O_WRONLY).
329 */
330 attr->ia_valid &= ATTR_SIZE;
331 if (attr->ia_valid)
332 nfserr = nfsd_setattr(rqstp, newfhp, attr, 0, (time_t)0);
333 }
334
335out_unlock:
336 /* We don't really need to unlock, as fh_put does it. */
337 fh_unlock(dirfhp);
338
339done:
340 fh_put(dirfhp);
David Shaw846f2fc2006-01-18 17:43:51 -0800341 return nfsd_return_dirop(nfserr, resp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700342}
343
Al Viro7111c662006-10-19 23:28:45 -0700344static __be32
Linus Torvalds1da177e2005-04-16 15:20:36 -0700345nfsd_proc_remove(struct svc_rqst *rqstp, struct nfsd_diropargs *argp,
346 void *resp)
347{
Al Viroc4d987b2006-10-19 23:29:00 -0700348 __be32 nfserr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700349
350 dprintk("nfsd: REMOVE %s %.*s\n", SVCFH_fmt(&argp->fh),
351 argp->len, argp->name);
352
353 /* Unlink. -SIFDIR means file must not be a directory */
354 nfserr = nfsd_unlink(rqstp, &argp->fh, -S_IFDIR, argp->name, argp->len);
355 fh_put(&argp->fh);
356 return nfserr;
357}
358
Al Viro7111c662006-10-19 23:28:45 -0700359static __be32
Linus Torvalds1da177e2005-04-16 15:20:36 -0700360nfsd_proc_rename(struct svc_rqst *rqstp, struct nfsd_renameargs *argp,
361 void *resp)
362{
Al Viroc4d987b2006-10-19 23:29:00 -0700363 __be32 nfserr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700364
365 dprintk("nfsd: RENAME %s %.*s -> \n",
366 SVCFH_fmt(&argp->ffh), argp->flen, argp->fname);
367 dprintk("nfsd: -> %s %.*s\n",
368 SVCFH_fmt(&argp->tfh), argp->tlen, argp->tname);
369
370 nfserr = nfsd_rename(rqstp, &argp->ffh, argp->fname, argp->flen,
371 &argp->tfh, argp->tname, argp->tlen);
372 fh_put(&argp->ffh);
373 fh_put(&argp->tfh);
374 return nfserr;
375}
376
Al Viro7111c662006-10-19 23:28:45 -0700377static __be32
Linus Torvalds1da177e2005-04-16 15:20:36 -0700378nfsd_proc_link(struct svc_rqst *rqstp, struct nfsd_linkargs *argp,
379 void *resp)
380{
Al Viroc4d987b2006-10-19 23:29:00 -0700381 __be32 nfserr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700382
383 dprintk("nfsd: LINK %s ->\n",
384 SVCFH_fmt(&argp->ffh));
385 dprintk("nfsd: %s %.*s\n",
386 SVCFH_fmt(&argp->tfh),
387 argp->tlen,
388 argp->tname);
389
390 nfserr = nfsd_link(rqstp, &argp->tfh, argp->tname, argp->tlen,
391 &argp->ffh);
392 fh_put(&argp->ffh);
393 fh_put(&argp->tfh);
394 return nfserr;
395}
396
Al Viro7111c662006-10-19 23:28:45 -0700397static __be32
Linus Torvalds1da177e2005-04-16 15:20:36 -0700398nfsd_proc_symlink(struct svc_rqst *rqstp, struct nfsd_symlinkargs *argp,
399 void *resp)
400{
401 struct svc_fh newfh;
Al Viroc4d987b2006-10-19 23:29:00 -0700402 __be32 nfserr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700403
404 dprintk("nfsd: SYMLINK %s %.*s -> %.*s\n",
405 SVCFH_fmt(&argp->ffh), argp->flen, argp->fname,
406 argp->tlen, argp->tname);
407
408 fh_init(&newfh, NFS_FHSIZE);
409 /*
410 * Create the link, look up new file and set attrs.
411 */
412 nfserr = nfsd_symlink(rqstp, &argp->ffh, argp->fname, argp->flen,
413 argp->tname, argp->tlen,
414 &newfh, &argp->attrs);
415
416
417 fh_put(&argp->ffh);
418 fh_put(&newfh);
419 return nfserr;
420}
421
422/*
423 * Make directory. This operation is not idempotent.
424 * N.B. After this call resp->fh needs an fh_put
425 */
Al Viro7111c662006-10-19 23:28:45 -0700426static __be32
Linus Torvalds1da177e2005-04-16 15:20:36 -0700427nfsd_proc_mkdir(struct svc_rqst *rqstp, struct nfsd_createargs *argp,
428 struct nfsd_diropres *resp)
429{
Al Viroc4d987b2006-10-19 23:29:00 -0700430 __be32 nfserr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700431
432 dprintk("nfsd: MKDIR %s %.*s\n", SVCFH_fmt(&argp->fh), argp->len, argp->name);
433
434 if (resp->fh.fh_dentry) {
435 printk(KERN_WARNING
436 "nfsd_proc_mkdir: response already verified??\n");
437 }
438
439 argp->attrs.ia_valid &= ~ATTR_SIZE;
440 fh_init(&resp->fh, NFS_FHSIZE);
441 nfserr = nfsd_create(rqstp, &argp->fh, argp->name, argp->len,
442 &argp->attrs, S_IFDIR, 0, &resp->fh);
443 fh_put(&argp->fh);
David Shaw846f2fc2006-01-18 17:43:51 -0800444 return nfsd_return_dirop(nfserr, resp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700445}
446
447/*
448 * Remove a directory
449 */
Al Viro7111c662006-10-19 23:28:45 -0700450static __be32
Linus Torvalds1da177e2005-04-16 15:20:36 -0700451nfsd_proc_rmdir(struct svc_rqst *rqstp, struct nfsd_diropargs *argp,
452 void *resp)
453{
Al Viroc4d987b2006-10-19 23:29:00 -0700454 __be32 nfserr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700455
456 dprintk("nfsd: RMDIR %s %.*s\n", SVCFH_fmt(&argp->fh), argp->len, argp->name);
457
458 nfserr = nfsd_unlink(rqstp, &argp->fh, S_IFDIR, argp->name, argp->len);
459 fh_put(&argp->fh);
460 return nfserr;
461}
462
463/*
464 * Read a portion of a directory.
465 */
Al Viro7111c662006-10-19 23:28:45 -0700466static __be32
Linus Torvalds1da177e2005-04-16 15:20:36 -0700467nfsd_proc_readdir(struct svc_rqst *rqstp, struct nfsd_readdirargs *argp,
468 struct nfsd_readdirres *resp)
469{
Al Viroc4d987b2006-10-19 23:29:00 -0700470 int count;
471 __be32 nfserr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700472 loff_t offset;
473
474 dprintk("nfsd: READDIR %s %d bytes at %d\n",
475 SVCFH_fmt(&argp->fh),
476 argp->count, argp->cookie);
477
478 /* Shrink to the client read size */
479 count = (argp->count >> 2) - 2;
480
481 /* Make sure we've room for the NULL ptr & eof flag */
482 count -= 2;
483 if (count < 0)
484 count = 0;
485
486 resp->buffer = argp->buffer;
487 resp->offset = NULL;
488 resp->buflen = count;
489 resp->common.err = nfs_ok;
490 /* Read directory and encode entries on the fly */
491 offset = argp->cookie;
492 nfserr = nfsd_readdir(rqstp, &argp->fh, &offset,
493 &resp->common, nfssvc_encode_entry);
494
495 resp->count = resp->buffer - argp->buffer;
496 if (resp->offset)
497 *resp->offset = htonl(offset);
498
499 fh_put(&argp->fh);
500 return nfserr;
501}
502
503/*
504 * Get file system info
505 */
Al Viro7111c662006-10-19 23:28:45 -0700506static __be32
Linus Torvalds1da177e2005-04-16 15:20:36 -0700507nfsd_proc_statfs(struct svc_rqst * rqstp, struct nfsd_fhandle *argp,
508 struct nfsd_statfsres *resp)
509{
Al Viroc4d987b2006-10-19 23:29:00 -0700510 __be32 nfserr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700511
512 dprintk("nfsd: STATFS %s\n", SVCFH_fmt(&argp->fh));
513
J. Bruce Fields04716e62008-08-07 13:00:20 -0400514 nfserr = nfsd_statfs(rqstp, &argp->fh, &resp->stats,
515 NFSD_MAY_BYPASS_GSS_ON_ROOT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700516 fh_put(&argp->fh);
517 return nfserr;
518}
519
520/*
521 * NFSv2 Server procedures.
522 * Only the results of non-idempotent operations are cached.
523 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700524struct nfsd_void { int dummy; };
525
Linus Torvalds1da177e2005-04-16 15:20:36 -0700526#define ST 1 /* status */
527#define FH 8 /* filehandle */
528#define AT 18 /* attributes */
529
530static struct svc_procedure nfsd_procedures2[18] = {
Yu Zhiguob9081d92009-06-09 17:33:34 +0800531 [NFSPROC_NULL] = {
532 .pc_func = (svc_procfunc) nfsd_proc_null,
533 .pc_decode = (kxdrproc_t) nfssvc_decode_void,
534 .pc_encode = (kxdrproc_t) nfssvc_encode_void,
535 .pc_argsize = sizeof(struct nfsd_void),
536 .pc_ressize = sizeof(struct nfsd_void),
537 .pc_cachetype = RC_NOCACHE,
538 .pc_xdrressize = ST,
539 },
540 [NFSPROC_GETATTR] = {
541 .pc_func = (svc_procfunc) nfsd_proc_getattr,
542 .pc_decode = (kxdrproc_t) nfssvc_decode_fhandle,
543 .pc_encode = (kxdrproc_t) nfssvc_encode_attrstat,
544 .pc_release = (kxdrproc_t) nfssvc_release_fhandle,
545 .pc_argsize = sizeof(struct nfsd_fhandle),
546 .pc_ressize = sizeof(struct nfsd_attrstat),
547 .pc_cachetype = RC_NOCACHE,
548 .pc_xdrressize = ST+AT,
549 },
550 [NFSPROC_SETATTR] = {
551 .pc_func = (svc_procfunc) nfsd_proc_setattr,
552 .pc_decode = (kxdrproc_t) nfssvc_decode_sattrargs,
553 .pc_encode = (kxdrproc_t) nfssvc_encode_attrstat,
554 .pc_release = (kxdrproc_t) nfssvc_release_fhandle,
555 .pc_argsize = sizeof(struct nfsd_sattrargs),
556 .pc_ressize = sizeof(struct nfsd_attrstat),
557 .pc_cachetype = RC_REPLBUFF,
558 .pc_xdrressize = ST+AT,
559 },
560 [NFSPROC_ROOT] = {
561 .pc_decode = (kxdrproc_t) nfssvc_decode_void,
562 .pc_encode = (kxdrproc_t) nfssvc_encode_void,
563 .pc_argsize = sizeof(struct nfsd_void),
564 .pc_ressize = sizeof(struct nfsd_void),
565 .pc_cachetype = RC_NOCACHE,
566 .pc_xdrressize = ST,
567 },
568 [NFSPROC_LOOKUP] = {
569 .pc_func = (svc_procfunc) nfsd_proc_lookup,
570 .pc_decode = (kxdrproc_t) nfssvc_decode_diropargs,
571 .pc_encode = (kxdrproc_t) nfssvc_encode_diropres,
572 .pc_release = (kxdrproc_t) nfssvc_release_fhandle,
573 .pc_argsize = sizeof(struct nfsd_diropargs),
574 .pc_ressize = sizeof(struct nfsd_diropres),
575 .pc_cachetype = RC_NOCACHE,
576 .pc_xdrressize = ST+FH+AT,
577 },
578 [NFSPROC_READLINK] = {
579 .pc_func = (svc_procfunc) nfsd_proc_readlink,
580 .pc_decode = (kxdrproc_t) nfssvc_decode_readlinkargs,
581 .pc_encode = (kxdrproc_t) nfssvc_encode_readlinkres,
582 .pc_argsize = sizeof(struct nfsd_readlinkargs),
583 .pc_ressize = sizeof(struct nfsd_readlinkres),
584 .pc_cachetype = RC_NOCACHE,
585 .pc_xdrressize = ST+1+NFS_MAXPATHLEN/4,
586 },
587 [NFSPROC_READ] = {
588 .pc_func = (svc_procfunc) nfsd_proc_read,
589 .pc_decode = (kxdrproc_t) nfssvc_decode_readargs,
590 .pc_encode = (kxdrproc_t) nfssvc_encode_readres,
591 .pc_release = (kxdrproc_t) nfssvc_release_fhandle,
592 .pc_argsize = sizeof(struct nfsd_readargs),
593 .pc_ressize = sizeof(struct nfsd_readres),
594 .pc_cachetype = RC_NOCACHE,
595 .pc_xdrressize = ST+AT+1+NFSSVC_MAXBLKSIZE_V2/4,
596 },
597 [NFSPROC_WRITECACHE] = {
598 .pc_decode = (kxdrproc_t) nfssvc_decode_void,
599 .pc_encode = (kxdrproc_t) nfssvc_encode_void,
600 .pc_argsize = sizeof(struct nfsd_void),
601 .pc_ressize = sizeof(struct nfsd_void),
602 .pc_cachetype = RC_NOCACHE,
603 .pc_xdrressize = ST,
604 },
605 [NFSPROC_WRITE] = {
606 .pc_func = (svc_procfunc) nfsd_proc_write,
607 .pc_decode = (kxdrproc_t) nfssvc_decode_writeargs,
608 .pc_encode = (kxdrproc_t) nfssvc_encode_attrstat,
609 .pc_release = (kxdrproc_t) nfssvc_release_fhandle,
610 .pc_argsize = sizeof(struct nfsd_writeargs),
611 .pc_ressize = sizeof(struct nfsd_attrstat),
612 .pc_cachetype = RC_REPLBUFF,
613 .pc_xdrressize = ST+AT,
614 },
615 [NFSPROC_CREATE] = {
616 .pc_func = (svc_procfunc) nfsd_proc_create,
617 .pc_decode = (kxdrproc_t) nfssvc_decode_createargs,
618 .pc_encode = (kxdrproc_t) nfssvc_encode_diropres,
619 .pc_release = (kxdrproc_t) nfssvc_release_fhandle,
620 .pc_argsize = sizeof(struct nfsd_createargs),
621 .pc_ressize = sizeof(struct nfsd_diropres),
622 .pc_cachetype = RC_REPLBUFF,
623 .pc_xdrressize = ST+FH+AT,
624 },
625 [NFSPROC_REMOVE] = {
626 .pc_func = (svc_procfunc) nfsd_proc_remove,
627 .pc_decode = (kxdrproc_t) nfssvc_decode_diropargs,
628 .pc_encode = (kxdrproc_t) nfssvc_encode_void,
629 .pc_argsize = sizeof(struct nfsd_diropargs),
630 .pc_ressize = sizeof(struct nfsd_void),
631 .pc_cachetype = RC_REPLSTAT,
632 .pc_xdrressize = ST,
633 },
634 [NFSPROC_RENAME] = {
635 .pc_func = (svc_procfunc) nfsd_proc_rename,
636 .pc_decode = (kxdrproc_t) nfssvc_decode_renameargs,
637 .pc_encode = (kxdrproc_t) nfssvc_encode_void,
638 .pc_argsize = sizeof(struct nfsd_renameargs),
639 .pc_ressize = sizeof(struct nfsd_void),
640 .pc_cachetype = RC_REPLSTAT,
641 .pc_xdrressize = ST,
642 },
643 [NFSPROC_LINK] = {
644 .pc_func = (svc_procfunc) nfsd_proc_link,
645 .pc_decode = (kxdrproc_t) nfssvc_decode_linkargs,
646 .pc_encode = (kxdrproc_t) nfssvc_encode_void,
647 .pc_argsize = sizeof(struct nfsd_linkargs),
648 .pc_ressize = sizeof(struct nfsd_void),
649 .pc_cachetype = RC_REPLSTAT,
650 .pc_xdrressize = ST,
651 },
652 [NFSPROC_SYMLINK] = {
653 .pc_func = (svc_procfunc) nfsd_proc_symlink,
654 .pc_decode = (kxdrproc_t) nfssvc_decode_symlinkargs,
655 .pc_encode = (kxdrproc_t) nfssvc_encode_void,
656 .pc_argsize = sizeof(struct nfsd_symlinkargs),
657 .pc_ressize = sizeof(struct nfsd_void),
658 .pc_cachetype = RC_REPLSTAT,
659 .pc_xdrressize = ST,
660 },
661 [NFSPROC_MKDIR] = {
662 .pc_func = (svc_procfunc) nfsd_proc_mkdir,
663 .pc_decode = (kxdrproc_t) nfssvc_decode_createargs,
664 .pc_encode = (kxdrproc_t) nfssvc_encode_diropres,
665 .pc_release = (kxdrproc_t) nfssvc_release_fhandle,
666 .pc_argsize = sizeof(struct nfsd_createargs),
667 .pc_ressize = sizeof(struct nfsd_diropres),
668 .pc_cachetype = RC_REPLBUFF,
669 .pc_xdrressize = ST+FH+AT,
670 },
671 [NFSPROC_RMDIR] = {
672 .pc_func = (svc_procfunc) nfsd_proc_rmdir,
673 .pc_decode = (kxdrproc_t) nfssvc_decode_diropargs,
674 .pc_encode = (kxdrproc_t) nfssvc_encode_void,
675 .pc_argsize = sizeof(struct nfsd_diropargs),
676 .pc_ressize = sizeof(struct nfsd_void),
677 .pc_cachetype = RC_REPLSTAT,
678 .pc_xdrressize = ST,
679 },
680 [NFSPROC_READDIR] = {
681 .pc_func = (svc_procfunc) nfsd_proc_readdir,
682 .pc_decode = (kxdrproc_t) nfssvc_decode_readdirargs,
683 .pc_encode = (kxdrproc_t) nfssvc_encode_readdirres,
684 .pc_argsize = sizeof(struct nfsd_readdirargs),
685 .pc_ressize = sizeof(struct nfsd_readdirres),
686 .pc_cachetype = RC_NOCACHE,
687 },
688 [NFSPROC_STATFS] = {
689 .pc_func = (svc_procfunc) nfsd_proc_statfs,
690 .pc_decode = (kxdrproc_t) nfssvc_decode_fhandle,
691 .pc_encode = (kxdrproc_t) nfssvc_encode_statfsres,
692 .pc_argsize = sizeof(struct nfsd_fhandle),
693 .pc_ressize = sizeof(struct nfsd_statfsres),
694 .pc_cachetype = RC_NOCACHE,
695 .pc_xdrressize = ST+5,
696 },
Linus Torvalds1da177e2005-04-16 15:20:36 -0700697};
698
699
700struct svc_version nfsd_version2 = {
701 .vs_vers = 2,
702 .vs_nproc = 18,
703 .vs_proc = nfsd_procedures2,
704 .vs_dispatch = nfsd_dispatch,
705 .vs_xdrsize = NFS2_SVC_XDRSIZE,
706};
707
708/*
709 * Map errnos to NFS errnos.
710 */
Al Viro63f103112006-10-19 23:28:54 -0700711__be32
Linus Torvalds1da177e2005-04-16 15:20:36 -0700712nfserrno (int errno)
713{
714 static struct {
Al Viro63f103112006-10-19 23:28:54 -0700715 __be32 nfserr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700716 int syserr;
717 } nfs_errtbl[] = {
718 { nfs_ok, 0 },
719 { nfserr_perm, -EPERM },
720 { nfserr_noent, -ENOENT },
721 { nfserr_io, -EIO },
722 { nfserr_nxio, -ENXIO },
723 { nfserr_acces, -EACCES },
724 { nfserr_exist, -EEXIST },
725 { nfserr_xdev, -EXDEV },
726 { nfserr_mlink, -EMLINK },
727 { nfserr_nodev, -ENODEV },
728 { nfserr_notdir, -ENOTDIR },
729 { nfserr_isdir, -EISDIR },
730 { nfserr_inval, -EINVAL },
731 { nfserr_fbig, -EFBIG },
732 { nfserr_nospc, -ENOSPC },
733 { nfserr_rofs, -EROFS },
734 { nfserr_mlink, -EMLINK },
735 { nfserr_nametoolong, -ENAMETOOLONG },
736 { nfserr_notempty, -ENOTEMPTY },
737#ifdef EDQUOT
738 { nfserr_dquot, -EDQUOT },
739#endif
740 { nfserr_stale, -ESTALE },
741 { nfserr_jukebox, -ETIMEDOUT },
NeilBrown599eb302008-06-19 10:11:09 +1000742 { nfserr_jukebox, -ERESTARTSYS },
Linus Torvalds1da177e2005-04-16 15:20:36 -0700743 { nfserr_dropit, -EAGAIN },
744 { nfserr_dropit, -ENOMEM },
745 { nfserr_badname, -ESRCH },
746 { nfserr_io, -ETXTBSY },
Andreas Gruenbachera838cc42005-06-22 17:16:24 +0000747 { nfserr_notsupp, -EOPNOTSUPP },
Dean Hildebrandb7aeda42008-12-15 19:40:15 +0200748 { nfserr_toosmall, -ETOOSMALL },
J. Bruce Fieldsf39bde22009-09-27 14:41:43 -0400749 { nfserr_serverfault, -ESERVERFAULT },
Linus Torvalds1da177e2005-04-16 15:20:36 -0700750 };
751 int i;
752
Al Viro63f103112006-10-19 23:28:54 -0700753 for (i = 0; i < ARRAY_SIZE(nfs_errtbl); i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700754 if (nfs_errtbl[i].syserr == errno)
755 return nfs_errtbl[i].nfserr;
756 }
757 printk (KERN_INFO "nfsd: non-standard errno: %d\n", errno);
758 return nfserr_io;
759}
760