]> git.proxmox.com Git - mirror_ubuntu-jammy-kernel.git/blame - fs/nfsd/nfsproc.c
Merge tag 'rproc-v5.13' of git://git.kernel.org/pub/scm/linux/kernel/git/andersson...
[mirror_ubuntu-jammy-kernel.git] / fs / nfsd / nfsproc.c
CommitLineData
b2441318 1// SPDX-License-Identifier: GPL-2.0
1da177e4 2/*
1da177e4
LT
3 * Process version 2 NFS requests.
4 *
5 * Copyright (C) 1995-1997 Olaf Kirch <okir@monad.swb.de>
6 */
7
1da177e4 8#include <linux/namei.h>
1da177e4 9
9a74af21
BH
10#include "cache.h"
11#include "xdr.h"
0a3adade 12#include "vfs.h"
1da177e4 13
1da177e4
LT
14#define NFSDDBG_FACILITY NFSDDBG_PROC
15
7111c66e 16static __be32
a6beb732 17nfsd_proc_null(struct svc_rqst *rqstp)
1da177e4 18{
cc028a10 19 return rpc_success;
1da177e4
LT
20}
21
22/*
23 * Get a file's attributes
24 * N.B. After this call resp->fh needs an fh_put
25 */
7111c66e 26static __be32
a6beb732 27nfsd_proc_getattr(struct svc_rqst *rqstp)
1da177e4 28{
a6beb732
CH
29 struct nfsd_fhandle *argp = rqstp->rq_argp;
30 struct nfsd_attrstat *resp = rqstp->rq_resp;
f0af2210 31
1da177e4
LT
32 dprintk("nfsd: GETATTR %s\n", SVCFH_fmt(&argp->fh));
33
34 fh_copy(&resp->fh, &argp->fh);
f0af2210
CL
35 resp->status = fh_verify(rqstp, &resp->fh, 0,
36 NFSD_MAY_NOP | NFSD_MAY_BYPASS_GSS_ON_ROOT);
37 if (resp->status != nfs_ok)
38 goto out;
39 resp->status = fh_getattr(&resp->fh, &resp->stat);
40out:
cc028a10 41 return rpc_success;
1da177e4
LT
42}
43
44/*
45 * Set a file's attributes
46 * N.B. After this call resp->fh needs an fh_put
47 */
7111c66e 48static __be32
a6beb732 49nfsd_proc_setattr(struct svc_rqst *rqstp)
1da177e4 50{
a6beb732
CH
51 struct nfsd_sattrargs *argp = rqstp->rq_argp;
52 struct nfsd_attrstat *resp = rqstp->rq_resp;
cc265089
AG
53 struct iattr *iap = &argp->attrs;
54 struct svc_fh *fhp;
cc265089 55
1da177e4
LT
56 dprintk("nfsd: SETATTR %s, valid=%x, size=%ld\n",
57 SVCFH_fmt(&argp->fh),
58 argp->attrs.ia_valid, (long) argp->attrs.ia_size);
59
cc265089
AG
60 fhp = fh_copy(&resp->fh, &argp->fh);
61
62 /*
63 * NFSv2 does not differentiate between "set-[ac]time-to-now"
64 * which only requires access, and "set-[ac]time-to-X" which
65 * requires ownership.
66 * So if it looks like it might be "set both to the same time which
31051c85 67 * is close to now", and if setattr_prepare fails, then we
cc265089
AG
68 * convert to "set to now" instead of "set to explicit time"
69 *
31051c85 70 * We only call setattr_prepare as the last test as technically
cc265089
AG
71 * it is not an interface that we should be using.
72 */
73#define BOTH_TIME_SET (ATTR_ATIME_SET | ATTR_MTIME_SET)
74#define MAX_TOUCH_TIME_ERROR (30*60)
75 if ((iap->ia_valid & BOTH_TIME_SET) == BOTH_TIME_SET &&
76 iap->ia_mtime.tv_sec == iap->ia_atime.tv_sec) {
77 /*
78 * Looks probable.
79 *
80 * Now just make sure time is in the right ballpark.
81 * Solaris, at least, doesn't seem to care what the time
82 * request is. We require it be within 30 minutes of now.
83 */
b6356d42 84 time64_t delta = iap->ia_atime.tv_sec - ktime_get_real_seconds();
cc265089 85
f0af2210
CL
86 resp->status = fh_verify(rqstp, fhp, 0, NFSD_MAY_NOP);
87 if (resp->status != nfs_ok)
cc028a10 88 goto out;
cc265089
AG
89
90 if (delta < 0)
91 delta = -delta;
92 if (delta < MAX_TOUCH_TIME_ERROR &&
2f221d6f 93 setattr_prepare(&init_user_ns, fhp->fh_dentry, iap) != 0) {
cc265089
AG
94 /*
95 * Turn off ATTR_[AM]TIME_SET but leave ATTR_[AM]TIME.
96 * This will cause notify_change to set these times
97 * to "now"
98 */
99 iap->ia_valid &= ~BOTH_TIME_SET;
100 }
101 }
102
f0af2210
CL
103 resp->status = nfsd_setattr(rqstp, fhp, iap, 0, (time64_t)0);
104 if (resp->status != nfs_ok)
105 goto out;
106
107 resp->status = fh_getattr(&resp->fh, &resp->stat);
108out:
cc028a10 109 return rpc_success;
1da177e4
LT
110}
111
6b3dccd4
CL
112/* Obsolete, replaced by MNTPROC_MNT. */
113static __be32
114nfsd_proc_root(struct svc_rqst *rqstp)
115{
cc028a10 116 return rpc_success;
6b3dccd4
CL
117}
118
1da177e4
LT
119/*
120 * Look up a path name component
121 * Note: the dentry in the resp->fh may be negative if the file
122 * doesn't exist yet.
123 * N.B. After this call resp->fh needs an fh_put
124 */
7111c66e 125static __be32
a6beb732 126nfsd_proc_lookup(struct svc_rqst *rqstp)
1da177e4 127{
a6beb732
CH
128 struct nfsd_diropargs *argp = rqstp->rq_argp;
129 struct nfsd_diropres *resp = rqstp->rq_resp;
1da177e4
LT
130
131 dprintk("nfsd: LOOKUP %s %.*s\n",
132 SVCFH_fmt(&argp->fh), argp->len, argp->name);
133
134 fh_init(&resp->fh, NFS_FHSIZE);
f0af2210
CL
135 resp->status = nfsd_lookup(rqstp, &argp->fh, argp->name, argp->len,
136 &resp->fh);
1da177e4 137 fh_put(&argp->fh);
f0af2210
CL
138 if (resp->status != nfs_ok)
139 goto out;
140
141 resp->status = fh_getattr(&resp->fh, &resp->stat);
142out:
cc028a10 143 return rpc_success;
1da177e4
LT
144}
145
146/*
147 * Read a symlink.
148 */
7111c66e 149static __be32
a6beb732 150nfsd_proc_readlink(struct svc_rqst *rqstp)
1da177e4 151{
1fcbd1c9 152 struct nfsd_fhandle *argp = rqstp->rq_argp;
a6beb732 153 struct nfsd_readlinkres *resp = rqstp->rq_resp;
1da177e4
LT
154
155 dprintk("nfsd: READLINK %s\n", SVCFH_fmt(&argp->fh));
156
157 /* Read the symlink. */
158 resp->len = NFS_MAXPATHLEN;
d9014b0f
CL
159 resp->page = *(rqstp->rq_next_page++);
160 resp->status = nfsd_readlink(rqstp, &argp->fh,
161 page_address(resp->page), &resp->len);
1da177e4
LT
162
163 fh_put(&argp->fh);
cc028a10 164 return rpc_success;
1da177e4
LT
165}
166
167/*
168 * Read a portion of a file.
169 * N.B. After this call resp->fh needs an fh_put
170 */
7111c66e 171static __be32
a6beb732 172nfsd_proc_read(struct svc_rqst *rqstp)
1da177e4 173{
a6beb732
CH
174 struct nfsd_readargs *argp = rqstp->rq_argp;
175 struct nfsd_readres *resp = rqstp->rq_resp;
8c293ef9 176 unsigned int len;
83a63072 177 u32 eof;
8c293ef9 178 int v;
1da177e4
LT
179
180 dprintk("nfsd: READ %s %d bytes at %d\n",
181 SVCFH_fmt(&argp->fh),
182 argp->count, argp->offset);
183
8c293ef9
CL
184 argp->count = min_t(u32, argp->count, NFSSVC_MAXBLKSIZE_V2);
185
186 v = 0;
187 len = argp->count;
a6f8d9dc 188 resp->pages = rqstp->rq_next_page;
8c293ef9
CL
189 while (len > 0) {
190 struct page *page = *(rqstp->rq_next_page++);
191
192 rqstp->rq_vec[v].iov_base = page_address(page);
193 rqstp->rq_vec[v].iov_len = min_t(unsigned int, len, PAGE_SIZE);
194 len -= rqstp->rq_vec[v].iov_len;
195 v++;
196 }
197
1da177e4
LT
198 /* Obtain buffer pointer for payload. 19 is 1 word for
199 * status, 17 words for fattr, and 1 word for the byte count.
200 */
cd123012 201 svc_reserve_auth(rqstp, (19<<2) + argp->count + 4);
1da177e4
LT
202
203 resp->count = argp->count;
8c293ef9
CL
204 fh_copy(&resp->fh, &argp->fh);
205 resp->status = nfsd_read(rqstp, &resp->fh, argp->offset,
206 rqstp->rq_vec, v, &resp->count, &eof);
cc028a10
CL
207 if (resp->status == nfs_ok)
208 resp->status = fh_getattr(&resp->fh, &resp->stat);
209 else if (resp->status == nfserr_jukebox)
210 return rpc_drop_reply;
211 return rpc_success;
1da177e4
LT
212}
213
6b3dccd4
CL
214/* Reserved */
215static __be32
216nfsd_proc_writecache(struct svc_rqst *rqstp)
217{
cc028a10 218 return rpc_success;
6b3dccd4
CL
219}
220
1da177e4
LT
221/*
222 * Write data to a file
223 * N.B. After this call resp->fh needs an fh_put
224 */
7111c66e 225static __be32
a6beb732 226nfsd_proc_write(struct svc_rqst *rqstp)
1da177e4 227{
a6beb732
CH
228 struct nfsd_writeargs *argp = rqstp->rq_argp;
229 struct nfsd_attrstat *resp = rqstp->rq_resp;
31dec253 230 unsigned long cnt = argp->len;
8154ef27 231 unsigned int nvecs;
1da177e4
LT
232
233 dprintk("nfsd: WRITE %s %d bytes at %d\n",
234 SVCFH_fmt(&argp->fh),
235 argp->len, argp->offset);
236
3fd9557a
CL
237 nvecs = svc_fill_write_vector(rqstp, rqstp->rq_arg.pages,
238 &argp->first, cnt);
f0af2210
CL
239 if (!nvecs) {
240 resp->status = nfserr_io;
241 goto out;
242 }
243
244 resp->status = nfsd_write(rqstp, fh_copy(&resp->fh, &argp->fh),
245 argp->offset, rqstp->rq_vec, nvecs,
246 &cnt, NFS_DATA_SYNC, NULL);
cc028a10
CL
247 if (resp->status == nfs_ok)
248 resp->status = fh_getattr(&resp->fh, &resp->stat);
249 else if (resp->status == nfserr_jukebox)
250 return rpc_drop_reply;
f0af2210 251out:
cc028a10 252 return rpc_success;
1da177e4
LT
253}
254
255/*
256 * CREATE processing is complicated. The keyword here is `overloaded.'
257 * The parent directory is kept locked between the check for existence
258 * and the actual create() call in compliance with VFS protocols.
259 * N.B. After this call _both_ argp->fh and resp->fh need an fh_put
260 */
7111c66e 261static __be32
a6beb732 262nfsd_proc_create(struct svc_rqst *rqstp)
1da177e4 263{
a6beb732
CH
264 struct nfsd_createargs *argp = rqstp->rq_argp;
265 struct nfsd_diropres *resp = rqstp->rq_resp;
1da177e4
LT
266 svc_fh *dirfhp = &argp->fh;
267 svc_fh *newfhp = &resp->fh;
268 struct iattr *attr = &argp->attrs;
269 struct inode *inode;
270 struct dentry *dchild;
c4d987ba 271 int type, mode;
4a55c101 272 int hosterr;
1da177e4
LT
273 dev_t rdev = 0, wanted = new_decode_dev(attr->ia_size);
274
275 dprintk("nfsd: CREATE %s %.*s\n",
276 SVCFH_fmt(dirfhp), argp->len, argp->name);
277
278 /* First verify the parent file handle */
f0af2210
CL
279 resp->status = fh_verify(rqstp, dirfhp, S_IFDIR, NFSD_MAY_EXEC);
280 if (resp->status != nfs_ok)
1da177e4
LT
281 goto done; /* must fh_put dirfhp even on error */
282
8837abca 283 /* Check for NFSD_MAY_WRITE in nfsd_create if necessary */
1da177e4 284
f0af2210 285 resp->status = nfserr_exist;
1da177e4
LT
286 if (isdotent(argp->name, argp->len))
287 goto done;
4a55c101
JK
288 hosterr = fh_want_write(dirfhp);
289 if (hosterr) {
f0af2210 290 resp->status = nfserrno(hosterr);
4a55c101
JK
291 goto done;
292 }
293
7ed94296 294 fh_lock_nested(dirfhp, I_MUTEX_PARENT);
1da177e4
LT
295 dchild = lookup_one_len(argp->name, dirfhp->fh_dentry, argp->len);
296 if (IS_ERR(dchild)) {
f0af2210 297 resp->status = nfserrno(PTR_ERR(dchild));
1da177e4
LT
298 goto out_unlock;
299 }
300 fh_init(newfhp, NFS_FHSIZE);
f0af2210
CL
301 resp->status = fh_compose(newfhp, dirfhp->fh_export, dchild, dirfhp);
302 if (!resp->status && d_really_is_negative(dchild))
303 resp->status = nfserr_noent;
1da177e4 304 dput(dchild);
f0af2210
CL
305 if (resp->status) {
306 if (resp->status != nfserr_noent)
1da177e4
LT
307 goto out_unlock;
308 /*
309 * If the new file handle wasn't verified, we can't tell
310 * whether the file exists or not. Time to bail ...
311 */
f0af2210 312 resp->status = nfserr_acces;
1da177e4
LT
313 if (!newfhp->fh_dentry) {
314 printk(KERN_WARNING
315 "nfsd_proc_create: file handle not verified\n");
316 goto out_unlock;
317 }
318 }
319
2b0143b5 320 inode = d_inode(newfhp->fh_dentry);
1da177e4
LT
321
322 /* Unfudge the mode bits */
323 if (attr->ia_valid & ATTR_MODE) {
324 type = attr->ia_mode & S_IFMT;
325 mode = attr->ia_mode & ~S_IFMT;
326 if (!type) {
327 /* no type, so if target exists, assume same as that,
328 * else assume a file */
329 if (inode) {
330 type = inode->i_mode & S_IFMT;
331 switch(type) {
332 case S_IFCHR:
333 case S_IFBLK:
334 /* reserve rdev for later checking */
335 rdev = inode->i_rdev;
336 attr->ia_valid |= ATTR_SIZE;
337
df561f66 338 fallthrough;
1da177e4
LT
339 case S_IFIFO:
340 /* this is probably a permission check..
341 * at least IRIX implements perm checking on
342 * echo thing > device-special-file-or-pipe
343 * by doing a CREATE with type==0
344 */
f0af2210 345 resp->status = nfsd_permission(rqstp,
0ec757df 346 newfhp->fh_export,
1da177e4 347 newfhp->fh_dentry,
8837abca 348 NFSD_MAY_WRITE|NFSD_MAY_LOCAL_ACCESS);
f0af2210 349 if (resp->status && resp->status != nfserr_rofs)
1da177e4
LT
350 goto out_unlock;
351 }
352 } else
353 type = S_IFREG;
354 }
355 } else if (inode) {
356 type = inode->i_mode & S_IFMT;
357 mode = inode->i_mode & ~S_IFMT;
358 } else {
359 type = S_IFREG;
360 mode = 0; /* ??? */
361 }
362
363 attr->ia_valid |= ATTR_MODE;
364 attr->ia_mode = mode;
365
366 /* Special treatment for non-regular files according to the
367 * gospel of sun micro
368 */
369 if (type != S_IFREG) {
1da177e4
LT
370 if (type != S_IFBLK && type != S_IFCHR) {
371 rdev = 0;
372 } else if (type == S_IFCHR && !(attr->ia_valid & ATTR_SIZE)) {
373 /* If you think you've seen the worst, grok this. */
374 type = S_IFIFO;
375 } else {
376 /* Okay, char or block special */
1da177e4
LT
377 if (!rdev)
378 rdev = wanted;
379 }
380
381 /* we've used the SIZE information, so discard it */
382 attr->ia_valid &= ~ATTR_SIZE;
383
384 /* Make sure the type and device matches */
f0af2210 385 resp->status = nfserr_exist;
6e3e2c43 386 if (inode && inode_wrong_type(inode, type))
1da177e4
LT
387 goto out_unlock;
388 }
389
f0af2210 390 resp->status = nfs_ok;
1da177e4
LT
391 if (!inode) {
392 /* File doesn't exist. Create it and set attrs */
f0af2210
CL
393 resp->status = nfsd_create_locked(rqstp, dirfhp, argp->name,
394 argp->len, attr, type, rdev,
395 newfhp);
1da177e4
LT
396 } else if (type == S_IFREG) {
397 dprintk("nfsd: existing %s, valid=%x, size=%ld\n",
398 argp->name, attr->ia_valid, (long) attr->ia_size);
399 /* File already exists. We ignore all attributes except
400 * size, so that creat() behaves exactly like
401 * open(..., O_CREAT|O_TRUNC|O_WRONLY).
402 */
403 attr->ia_valid &= ATTR_SIZE;
404 if (attr->ia_valid)
f0af2210
CL
405 resp->status = nfsd_setattr(rqstp, newfhp, attr, 0,
406 (time64_t)0);
1da177e4
LT
407 }
408
409out_unlock:
410 /* We don't really need to unlock, as fh_put does it. */
411 fh_unlock(dirfhp);
4a55c101 412 fh_drop_write(dirfhp);
1da177e4
LT
413done:
414 fh_put(dirfhp);
f0af2210
CL
415 if (resp->status != nfs_ok)
416 goto out;
417 resp->status = fh_getattr(&resp->fh, &resp->stat);
418out:
cc028a10 419 return rpc_success;
1da177e4
LT
420}
421
7111c66e 422static __be32
a6beb732 423nfsd_proc_remove(struct svc_rqst *rqstp)
1da177e4 424{
a6beb732 425 struct nfsd_diropargs *argp = rqstp->rq_argp;
cc028a10 426 struct nfsd_stat *resp = rqstp->rq_resp;
1da177e4
LT
427
428 dprintk("nfsd: REMOVE %s %.*s\n", SVCFH_fmt(&argp->fh),
429 argp->len, argp->name);
430
431 /* Unlink. -SIFDIR means file must not be a directory */
cc028a10
CL
432 resp->status = nfsd_unlink(rqstp, &argp->fh, -S_IFDIR,
433 argp->name, argp->len);
1da177e4 434 fh_put(&argp->fh);
cc028a10 435 return rpc_success;
1da177e4
LT
436}
437
7111c66e 438static __be32
a6beb732 439nfsd_proc_rename(struct svc_rqst *rqstp)
1da177e4 440{
a6beb732 441 struct nfsd_renameargs *argp = rqstp->rq_argp;
cc028a10 442 struct nfsd_stat *resp = rqstp->rq_resp;
1da177e4
LT
443
444 dprintk("nfsd: RENAME %s %.*s -> \n",
445 SVCFH_fmt(&argp->ffh), argp->flen, argp->fname);
446 dprintk("nfsd: -> %s %.*s\n",
447 SVCFH_fmt(&argp->tfh), argp->tlen, argp->tname);
448
cc028a10
CL
449 resp->status = nfsd_rename(rqstp, &argp->ffh, argp->fname, argp->flen,
450 &argp->tfh, argp->tname, argp->tlen);
1da177e4
LT
451 fh_put(&argp->ffh);
452 fh_put(&argp->tfh);
cc028a10 453 return rpc_success;
1da177e4
LT
454}
455
7111c66e 456static __be32
a6beb732 457nfsd_proc_link(struct svc_rqst *rqstp)
1da177e4 458{
a6beb732 459 struct nfsd_linkargs *argp = rqstp->rq_argp;
cc028a10 460 struct nfsd_stat *resp = rqstp->rq_resp;
1da177e4
LT
461
462 dprintk("nfsd: LINK %s ->\n",
463 SVCFH_fmt(&argp->ffh));
464 dprintk("nfsd: %s %.*s\n",
465 SVCFH_fmt(&argp->tfh),
466 argp->tlen,
467 argp->tname);
468
cc028a10
CL
469 resp->status = nfsd_link(rqstp, &argp->tfh, argp->tname, argp->tlen,
470 &argp->ffh);
1da177e4
LT
471 fh_put(&argp->ffh);
472 fh_put(&argp->tfh);
cc028a10 473 return rpc_success;
1da177e4
LT
474}
475
7111c66e 476static __be32
a6beb732 477nfsd_proc_symlink(struct svc_rqst *rqstp)
1da177e4 478{
a6beb732 479 struct nfsd_symlinkargs *argp = rqstp->rq_argp;
cc028a10 480 struct nfsd_stat *resp = rqstp->rq_resp;
1da177e4 481 struct svc_fh newfh;
1da177e4 482
f0af2210 483 if (argp->tlen > NFS_MAXPATHLEN) {
cc028a10 484 resp->status = nfserr_nametoolong;
f0af2210
CL
485 goto out;
486 }
38a70315
CL
487
488 argp->tname = svc_fill_symlink_pathname(rqstp, &argp->first,
11b4d66e 489 page_address(rqstp->rq_arg.pages[0]),
38a70315 490 argp->tlen);
f0af2210 491 if (IS_ERR(argp->tname)) {
cc028a10 492 resp->status = nfserrno(PTR_ERR(argp->tname));
f0af2210
CL
493 goto out;
494 }
38a70315 495
1da177e4
LT
496 dprintk("nfsd: SYMLINK %s %.*s -> %.*s\n",
497 SVCFH_fmt(&argp->ffh), argp->flen, argp->fname,
498 argp->tlen, argp->tname);
499
500 fh_init(&newfh, NFS_FHSIZE);
cc028a10
CL
501 resp->status = nfsd_symlink(rqstp, &argp->ffh, argp->fname, argp->flen,
502 argp->tname, &newfh);
1da177e4 503
11b4d66e 504 kfree(argp->tname);
1da177e4
LT
505 fh_put(&argp->ffh);
506 fh_put(&newfh);
f0af2210 507out:
cc028a10 508 return rpc_success;
1da177e4
LT
509}
510
511/*
512 * Make directory. This operation is not idempotent.
513 * N.B. After this call resp->fh needs an fh_put
514 */
7111c66e 515static __be32
a6beb732 516nfsd_proc_mkdir(struct svc_rqst *rqstp)
1da177e4 517{
a6beb732
CH
518 struct nfsd_createargs *argp = rqstp->rq_argp;
519 struct nfsd_diropres *resp = rqstp->rq_resp;
1da177e4
LT
520
521 dprintk("nfsd: MKDIR %s %.*s\n", SVCFH_fmt(&argp->fh), argp->len, argp->name);
522
523 if (resp->fh.fh_dentry) {
524 printk(KERN_WARNING
525 "nfsd_proc_mkdir: response already verified??\n");
526 }
527
528 argp->attrs.ia_valid &= ~ATTR_SIZE;
529 fh_init(&resp->fh, NFS_FHSIZE);
f0af2210
CL
530 resp->status = nfsd_create(rqstp, &argp->fh, argp->name, argp->len,
531 &argp->attrs, S_IFDIR, 0, &resp->fh);
1da177e4 532 fh_put(&argp->fh);
f0af2210
CL
533 if (resp->status != nfs_ok)
534 goto out;
535
536 resp->status = fh_getattr(&resp->fh, &resp->stat);
537out:
cc028a10 538 return rpc_success;
1da177e4
LT
539}
540
541/*
542 * Remove a directory
543 */
7111c66e 544static __be32
a6beb732 545nfsd_proc_rmdir(struct svc_rqst *rqstp)
1da177e4 546{
a6beb732 547 struct nfsd_diropargs *argp = rqstp->rq_argp;
cc028a10 548 struct nfsd_stat *resp = rqstp->rq_resp;
1da177e4
LT
549
550 dprintk("nfsd: RMDIR %s %.*s\n", SVCFH_fmt(&argp->fh), argp->len, argp->name);
551
cc028a10
CL
552 resp->status = nfsd_unlink(rqstp, &argp->fh, S_IFDIR,
553 argp->name, argp->len);
1da177e4 554 fh_put(&argp->fh);
cc028a10 555 return rpc_success;
1da177e4
LT
556}
557
788cd46e
CL
558static void nfsd_init_dirlist_pages(struct svc_rqst *rqstp,
559 struct nfsd_readdirres *resp,
560 int count)
561{
f5dcccd6
CL
562 struct xdr_buf *buf = &resp->dirlist;
563 struct xdr_stream *xdr = &resp->xdr;
564
788cd46e
CL
565 count = min_t(u32, count, PAGE_SIZE);
566
f5dcccd6 567 memset(buf, 0, sizeof(*buf));
788cd46e 568
f5dcccd6
CL
569 /* Reserve room for the NULL ptr & eof flag (-2 words) */
570 buf->buflen = count - sizeof(__be32) * 2;
571 buf->pages = rqstp->rq_next_page;
788cd46e 572 rqstp->rq_next_page++;
f5dcccd6
CL
573
574 /* This is xdr_init_encode(), but it assumes that
575 * the head kvec has already been consumed. */
576 xdr_set_scratch_buffer(xdr, NULL, 0);
577 xdr->buf = buf;
578 xdr->page_ptr = buf->pages;
579 xdr->iov = NULL;
580 xdr->p = page_address(*buf->pages);
581 xdr->end = xdr->p + (PAGE_SIZE >> 2);
582 xdr->rqst = NULL;
788cd46e
CL
583}
584
1da177e4
LT
585/*
586 * Read a portion of a directory.
587 */
7111c66e 588static __be32
a6beb732 589nfsd_proc_readdir(struct svc_rqst *rqstp)
1da177e4 590{
a6beb732
CH
591 struct nfsd_readdirargs *argp = rqstp->rq_argp;
592 struct nfsd_readdirres *resp = rqstp->rq_resp;
1da177e4
LT
593 loff_t offset;
594
595 dprintk("nfsd: READDIR %s %d bytes at %d\n",
596 SVCFH_fmt(&argp->fh),
597 argp->count, argp->cookie);
598
788cd46e 599 nfsd_init_dirlist_pages(rqstp, resp, argp->count);
1da177e4 600
1da177e4 601 resp->common.err = nfs_ok;
f5dcccd6 602 resp->cookie_offset = 0;
1da177e4 603 offset = argp->cookie;
f0af2210 604 resp->status = nfsd_readdir(rqstp, &argp->fh, &offset,
8a2cf9f5 605 &resp->common, nfssvc_encode_entry);
d5253200 606 nfssvc_encode_nfscookie(resp, offset);
1da177e4
LT
607
608 fh_put(&argp->fh);
cc028a10 609 return rpc_success;
1da177e4
LT
610}
611
612/*
613 * Get file system info
614 */
7111c66e 615static __be32
a6beb732 616nfsd_proc_statfs(struct svc_rqst *rqstp)
1da177e4 617{
a6beb732
CH
618 struct nfsd_fhandle *argp = rqstp->rq_argp;
619 struct nfsd_statfsres *resp = rqstp->rq_resp;
1da177e4
LT
620
621 dprintk("nfsd: STATFS %s\n", SVCFH_fmt(&argp->fh));
622
f0af2210
CL
623 resp->status = nfsd_statfs(rqstp, &argp->fh, &resp->stats,
624 NFSD_MAY_BYPASS_GSS_ON_ROOT);
1da177e4 625 fh_put(&argp->fh);
cc028a10 626 return rpc_success;
1da177e4
LT
627}
628
629/*
630 * NFSv2 Server procedures.
631 * Only the results of non-idempotent operations are cached.
632 */
1da177e4 633
1da177e4
LT
634#define ST 1 /* status */
635#define FH 8 /* filehandle */
636#define AT 18 /* attributes */
637
860bda29 638static const struct svc_procedure nfsd_procedures2[18] = {
b9081d90 639 [NFSPROC_NULL] = {
a6beb732 640 .pc_func = nfsd_proc_null,
788f7183
CL
641 .pc_decode = nfssvc_decode_voidarg,
642 .pc_encode = nfssvc_encode_voidres,
643 .pc_argsize = sizeof(struct nfsd_voidargs),
644 .pc_ressize = sizeof(struct nfsd_voidres),
b9081d90 645 .pc_cachetype = RC_NOCACHE,
cc028a10 646 .pc_xdrressize = 0,
2289e87b 647 .pc_name = "NULL",
b9081d90
YZ
648 },
649 [NFSPROC_GETATTR] = {
a6beb732 650 .pc_func = nfsd_proc_getattr,
ebcd8e8b 651 .pc_decode = nfssvc_decode_fhandleargs,
92b54a4f 652 .pc_encode = nfssvc_encode_attrstatres,
1841b9b6 653 .pc_release = nfssvc_release_attrstat,
b9081d90
YZ
654 .pc_argsize = sizeof(struct nfsd_fhandle),
655 .pc_ressize = sizeof(struct nfsd_attrstat),
656 .pc_cachetype = RC_NOCACHE,
657 .pc_xdrressize = ST+AT,
2289e87b 658 .pc_name = "GETATTR",
b9081d90
YZ
659 },
660 [NFSPROC_SETATTR] = {
a6beb732 661 .pc_func = nfsd_proc_setattr,
026fec7e 662 .pc_decode = nfssvc_decode_sattrargs,
92b54a4f 663 .pc_encode = nfssvc_encode_attrstatres,
1841b9b6 664 .pc_release = nfssvc_release_attrstat,
b9081d90
YZ
665 .pc_argsize = sizeof(struct nfsd_sattrargs),
666 .pc_ressize = sizeof(struct nfsd_attrstat),
667 .pc_cachetype = RC_REPLBUFF,
668 .pc_xdrressize = ST+AT,
2289e87b 669 .pc_name = "SETATTR",
b9081d90
YZ
670 },
671 [NFSPROC_ROOT] = {
6b3dccd4 672 .pc_func = nfsd_proc_root,
788f7183
CL
673 .pc_decode = nfssvc_decode_voidarg,
674 .pc_encode = nfssvc_encode_voidres,
675 .pc_argsize = sizeof(struct nfsd_voidargs),
676 .pc_ressize = sizeof(struct nfsd_voidres),
b9081d90 677 .pc_cachetype = RC_NOCACHE,
cc028a10 678 .pc_xdrressize = 0,
2289e87b 679 .pc_name = "ROOT",
b9081d90
YZ
680 },
681 [NFSPROC_LOOKUP] = {
a6beb732 682 .pc_func = nfsd_proc_lookup,
026fec7e 683 .pc_decode = nfssvc_decode_diropargs,
63f8de37 684 .pc_encode = nfssvc_encode_diropres,
1841b9b6 685 .pc_release = nfssvc_release_diropres,
b9081d90
YZ
686 .pc_argsize = sizeof(struct nfsd_diropargs),
687 .pc_ressize = sizeof(struct nfsd_diropres),
688 .pc_cachetype = RC_NOCACHE,
689 .pc_xdrressize = ST+FH+AT,
2289e87b 690 .pc_name = "LOOKUP",
b9081d90
YZ
691 },
692 [NFSPROC_READLINK] = {
a6beb732 693 .pc_func = nfsd_proc_readlink,
1fcbd1c9 694 .pc_decode = nfssvc_decode_fhandleargs,
63f8de37 695 .pc_encode = nfssvc_encode_readlinkres,
1fcbd1c9 696 .pc_argsize = sizeof(struct nfsd_fhandle),
b9081d90
YZ
697 .pc_ressize = sizeof(struct nfsd_readlinkres),
698 .pc_cachetype = RC_NOCACHE,
699 .pc_xdrressize = ST+1+NFS_MAXPATHLEN/4,
2289e87b 700 .pc_name = "READLINK",
b9081d90
YZ
701 },
702 [NFSPROC_READ] = {
a6beb732 703 .pc_func = nfsd_proc_read,
026fec7e 704 .pc_decode = nfssvc_decode_readargs,
63f8de37 705 .pc_encode = nfssvc_encode_readres,
1841b9b6 706 .pc_release = nfssvc_release_readres,
b9081d90
YZ
707 .pc_argsize = sizeof(struct nfsd_readargs),
708 .pc_ressize = sizeof(struct nfsd_readres),
709 .pc_cachetype = RC_NOCACHE,
710 .pc_xdrressize = ST+AT+1+NFSSVC_MAXBLKSIZE_V2/4,
2289e87b 711 .pc_name = "READ",
b9081d90
YZ
712 },
713 [NFSPROC_WRITECACHE] = {
6b3dccd4 714 .pc_func = nfsd_proc_writecache,
788f7183
CL
715 .pc_decode = nfssvc_decode_voidarg,
716 .pc_encode = nfssvc_encode_voidres,
717 .pc_argsize = sizeof(struct nfsd_voidargs),
718 .pc_ressize = sizeof(struct nfsd_voidres),
b9081d90 719 .pc_cachetype = RC_NOCACHE,
cc028a10 720 .pc_xdrressize = 0,
2289e87b 721 .pc_name = "WRITECACHE",
b9081d90
YZ
722 },
723 [NFSPROC_WRITE] = {
a6beb732 724 .pc_func = nfsd_proc_write,
026fec7e 725 .pc_decode = nfssvc_decode_writeargs,
92b54a4f 726 .pc_encode = nfssvc_encode_attrstatres,
1841b9b6 727 .pc_release = nfssvc_release_attrstat,
b9081d90
YZ
728 .pc_argsize = sizeof(struct nfsd_writeargs),
729 .pc_ressize = sizeof(struct nfsd_attrstat),
730 .pc_cachetype = RC_REPLBUFF,
731 .pc_xdrressize = ST+AT,
2289e87b 732 .pc_name = "WRITE",
b9081d90
YZ
733 },
734 [NFSPROC_CREATE] = {
a6beb732 735 .pc_func = nfsd_proc_create,
026fec7e 736 .pc_decode = nfssvc_decode_createargs,
63f8de37 737 .pc_encode = nfssvc_encode_diropres,
1841b9b6 738 .pc_release = nfssvc_release_diropres,
b9081d90
YZ
739 .pc_argsize = sizeof(struct nfsd_createargs),
740 .pc_ressize = sizeof(struct nfsd_diropres),
741 .pc_cachetype = RC_REPLBUFF,
742 .pc_xdrressize = ST+FH+AT,
2289e87b 743 .pc_name = "CREATE",
b9081d90
YZ
744 },
745 [NFSPROC_REMOVE] = {
a6beb732 746 .pc_func = nfsd_proc_remove,
026fec7e 747 .pc_decode = nfssvc_decode_diropargs,
a887eaed 748 .pc_encode = nfssvc_encode_statres,
b9081d90 749 .pc_argsize = sizeof(struct nfsd_diropargs),
cc028a10 750 .pc_ressize = sizeof(struct nfsd_stat),
b9081d90
YZ
751 .pc_cachetype = RC_REPLSTAT,
752 .pc_xdrressize = ST,
2289e87b 753 .pc_name = "REMOVE",
b9081d90
YZ
754 },
755 [NFSPROC_RENAME] = {
a6beb732 756 .pc_func = nfsd_proc_rename,
026fec7e 757 .pc_decode = nfssvc_decode_renameargs,
a887eaed 758 .pc_encode = nfssvc_encode_statres,
b9081d90 759 .pc_argsize = sizeof(struct nfsd_renameargs),
cc028a10 760 .pc_ressize = sizeof(struct nfsd_stat),
b9081d90
YZ
761 .pc_cachetype = RC_REPLSTAT,
762 .pc_xdrressize = ST,
2289e87b 763 .pc_name = "RENAME",
b9081d90
YZ
764 },
765 [NFSPROC_LINK] = {
a6beb732 766 .pc_func = nfsd_proc_link,
026fec7e 767 .pc_decode = nfssvc_decode_linkargs,
a887eaed 768 .pc_encode = nfssvc_encode_statres,
b9081d90 769 .pc_argsize = sizeof(struct nfsd_linkargs),
cc028a10 770 .pc_ressize = sizeof(struct nfsd_stat),
b9081d90
YZ
771 .pc_cachetype = RC_REPLSTAT,
772 .pc_xdrressize = ST,
2289e87b 773 .pc_name = "LINK",
b9081d90
YZ
774 },
775 [NFSPROC_SYMLINK] = {
a6beb732 776 .pc_func = nfsd_proc_symlink,
026fec7e 777 .pc_decode = nfssvc_decode_symlinkargs,
a887eaed 778 .pc_encode = nfssvc_encode_statres,
b9081d90 779 .pc_argsize = sizeof(struct nfsd_symlinkargs),
cc028a10 780 .pc_ressize = sizeof(struct nfsd_stat),
b9081d90
YZ
781 .pc_cachetype = RC_REPLSTAT,
782 .pc_xdrressize = ST,
2289e87b 783 .pc_name = "SYMLINK",
b9081d90
YZ
784 },
785 [NFSPROC_MKDIR] = {
a6beb732 786 .pc_func = nfsd_proc_mkdir,
026fec7e 787 .pc_decode = nfssvc_decode_createargs,
63f8de37 788 .pc_encode = nfssvc_encode_diropres,
1841b9b6 789 .pc_release = nfssvc_release_diropres,
b9081d90
YZ
790 .pc_argsize = sizeof(struct nfsd_createargs),
791 .pc_ressize = sizeof(struct nfsd_diropres),
792 .pc_cachetype = RC_REPLBUFF,
793 .pc_xdrressize = ST+FH+AT,
2289e87b 794 .pc_name = "MKDIR",
b9081d90
YZ
795 },
796 [NFSPROC_RMDIR] = {
a6beb732 797 .pc_func = nfsd_proc_rmdir,
026fec7e 798 .pc_decode = nfssvc_decode_diropargs,
a887eaed 799 .pc_encode = nfssvc_encode_statres,
b9081d90 800 .pc_argsize = sizeof(struct nfsd_diropargs),
cc028a10 801 .pc_ressize = sizeof(struct nfsd_stat),
b9081d90
YZ
802 .pc_cachetype = RC_REPLSTAT,
803 .pc_xdrressize = ST,
2289e87b 804 .pc_name = "RMDIR",
b9081d90
YZ
805 },
806 [NFSPROC_READDIR] = {
a6beb732 807 .pc_func = nfsd_proc_readdir,
026fec7e 808 .pc_decode = nfssvc_decode_readdirargs,
63f8de37 809 .pc_encode = nfssvc_encode_readdirres,
b9081d90
YZ
810 .pc_argsize = sizeof(struct nfsd_readdirargs),
811 .pc_ressize = sizeof(struct nfsd_readdirres),
812 .pc_cachetype = RC_NOCACHE,
2289e87b 813 .pc_name = "READDIR",
b9081d90
YZ
814 },
815 [NFSPROC_STATFS] = {
a6beb732 816 .pc_func = nfsd_proc_statfs,
ebcd8e8b 817 .pc_decode = nfssvc_decode_fhandleargs,
63f8de37 818 .pc_encode = nfssvc_encode_statfsres,
b9081d90
YZ
819 .pc_argsize = sizeof(struct nfsd_fhandle),
820 .pc_ressize = sizeof(struct nfsd_statfsres),
821 .pc_cachetype = RC_NOCACHE,
822 .pc_xdrressize = ST+5,
2289e87b 823 .pc_name = "STATFS",
b9081d90 824 },
1da177e4
LT
825};
826
827
7fd38af9 828static unsigned int nfsd_count2[ARRAY_SIZE(nfsd_procedures2)];
e9679189
CH
829const struct svc_version nfsd_version2 = {
830 .vs_vers = 2,
831 .vs_nproc = 18,
832 .vs_proc = nfsd_procedures2,
833 .vs_count = nfsd_count2,
834 .vs_dispatch = nfsd_dispatch,
835 .vs_xdrsize = NFS2_SVC_XDRSIZE,
1da177e4
LT
836};
837
838/*
839 * Map errnos to NFS errnos.
840 */
63f10311 841__be32
1da177e4
LT
842nfserrno (int errno)
843{
844 static struct {
63f10311 845 __be32 nfserr;
1da177e4
LT
846 int syserr;
847 } nfs_errtbl[] = {
848 { nfs_ok, 0 },
849 { nfserr_perm, -EPERM },
850 { nfserr_noent, -ENOENT },
851 { nfserr_io, -EIO },
852 { nfserr_nxio, -ENXIO },
62814d6a 853 { nfserr_fbig, -E2BIG },
1da177e4
LT
854 { nfserr_acces, -EACCES },
855 { nfserr_exist, -EEXIST },
856 { nfserr_xdev, -EXDEV },
857 { nfserr_mlink, -EMLINK },
858 { nfserr_nodev, -ENODEV },
859 { nfserr_notdir, -ENOTDIR },
860 { nfserr_isdir, -EISDIR },
861 { nfserr_inval, -EINVAL },
862 { nfserr_fbig, -EFBIG },
863 { nfserr_nospc, -ENOSPC },
864 { nfserr_rofs, -EROFS },
865 { nfserr_mlink, -EMLINK },
866 { nfserr_nametoolong, -ENAMETOOLONG },
867 { nfserr_notempty, -ENOTEMPTY },
868#ifdef EDQUOT
869 { nfserr_dquot, -EDQUOT },
870#endif
871 { nfserr_stale, -ESTALE },
872 { nfserr_jukebox, -ETIMEDOUT },
599eb304 873 { nfserr_jukebox, -ERESTARTSYS },
062304a8
BF
874 { nfserr_jukebox, -EAGAIN },
875 { nfserr_jukebox, -EWOULDBLOCK },
3beb6cd1 876 { nfserr_jukebox, -ENOMEM },
1da177e4 877 { nfserr_io, -ETXTBSY },
a838cc49 878 { nfserr_notsupp, -EOPNOTSUPP },
b7aeda40 879 { nfserr_toosmall, -ETOOSMALL },
f39bde24 880 { nfserr_serverfault, -ESERVERFAULT },
b3fbfe0e 881 { nfserr_serverfault, -ENFILE },
42e61616 882 { nfserr_io, -EUCLEAN },
c952cd4e 883 { nfserr_perm, -ENOKEY },
1da177e4
LT
884 };
885 int i;
886
63f10311 887 for (i = 0; i < ARRAY_SIZE(nfs_errtbl); i++) {
1da177e4
LT
888 if (nfs_errtbl[i].syserr == errno)
889 return nfs_errtbl[i].nfserr;
890 }
ff30f08c 891 WARN_ONCE(1, "nfsd: non-standard errno: %d\n", errno);
1da177e4
LT
892 return nfserr_io;
893}
894