blob: 8333db12caca56207a0142fcb9a94000f962ad69 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * fs/nfsd/nfs4proc.c
3 *
4 * Server-side procedures for NFSv4.
5 *
6 * Copyright (c) 2002 The Regents of the University of Michigan.
7 * All rights reserved.
8 *
9 * Kendrick Smith <kmsmith@umich.edu>
10 * Andy Adamson <andros@umich.edu>
11 *
12 * Redistribution and use in source and binary forms, with or without
13 * modification, are permitted provided that the following conditions
14 * are met:
15 *
16 * 1. Redistributions of source code must retain the above copyright
17 * notice, this list of conditions and the following disclaimer.
18 * 2. Redistributions in binary form must reproduce the above copyright
19 * notice, this list of conditions and the following disclaimer in the
20 * documentation and/or other materials provided with the distribution.
21 * 3. Neither the name of the University nor the names of its
22 * contributors may be used to endorse or promote products derived
23 * from this software without specific prior written permission.
24 *
25 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED
26 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
27 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
28 * DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
29 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
30 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
31 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
32 * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
33 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
34 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
35 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
36 *
37 * Note: some routines in this file are just trivial wrappers
38 * (e.g. nfsd4_lookup()) defined solely for the sake of consistent
39 * naming. Since all such routines have been declared "inline",
40 * there shouldn't be any associated overhead. At some point in
41 * the future, I might inline these "by hand" to clean up a
42 * little.
43 */
44
45#include <linux/param.h>
46#include <linux/major.h>
47#include <linux/slab.h>
NeilBrown7e06b7f2005-06-23 22:03:13 -070048#include <linux/file.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070049
50#include <linux/sunrpc/svc.h>
51#include <linux/nfsd/nfsd.h>
52#include <linux/nfsd/cache.h>
53#include <linux/nfs4.h>
54#include <linux/nfsd/state.h>
55#include <linux/nfsd/xdr4.h>
56#include <linux/nfs4_acl.h>
57
58#define NFSDDBG_FACILITY NFSDDBG_PROC
59
60static inline void
61fh_dup2(struct svc_fh *dst, struct svc_fh *src)
62{
63 fh_put(dst);
64 dget(src->fh_dentry);
65 if (src->fh_export)
66 cache_get(&src->fh_export->h);
67 *dst = *src;
68}
69
70static int
71do_open_permission(struct svc_rqst *rqstp, struct svc_fh *current_fh, struct nfsd4_open *open)
72{
73 int accmode, status;
74
75 if (open->op_truncate &&
76 !(open->op_share_access & NFS4_SHARE_ACCESS_WRITE))
77 return nfserr_inval;
78
79 accmode = MAY_NOP;
80 if (open->op_share_access & NFS4_SHARE_ACCESS_READ)
81 accmode = MAY_READ;
82 if (open->op_share_deny & NFS4_SHARE_ACCESS_WRITE)
83 accmode |= (MAY_WRITE | MAY_TRUNC);
84 accmode |= MAY_OWNER_OVERRIDE;
85
86 status = fh_verify(rqstp, current_fh, S_IFREG, accmode);
87
88 return status;
89}
90
91static int
92do_open_lookup(struct svc_rqst *rqstp, struct svc_fh *current_fh, struct nfsd4_open *open)
93{
94 struct svc_fh resfh;
95 int status;
96
97 fh_init(&resfh, NFS4_FHSIZE);
98 open->op_truncate = 0;
99
100 if (open->op_create) {
101 /*
102 * Note: create modes (UNCHECKED,GUARDED...) are the same
103 * in NFSv4 as in v3.
104 */
105 status = nfsd_create_v3(rqstp, current_fh, open->op_fname.data,
106 open->op_fname.len, &open->op_iattr,
107 &resfh, open->op_createmode,
108 (u32 *)open->op_verf.data, &open->op_truncate);
109 }
110 else {
111 status = nfsd_lookup(rqstp, current_fh,
112 open->op_fname.data, open->op_fname.len, &resfh);
113 fh_unlock(current_fh);
114 }
115
116 if (!status) {
117 set_change_info(&open->op_cinfo, current_fh);
118
119 /* set reply cache */
120 fh_dup2(current_fh, &resfh);
121 open->op_stateowner->so_replay.rp_openfh_len =
122 resfh.fh_handle.fh_size;
123 memcpy(open->op_stateowner->so_replay.rp_openfh,
124 &resfh.fh_handle.fh_base,
125 resfh.fh_handle.fh_size);
126
127 status = do_open_permission(rqstp, current_fh, open);
128 }
129
130 fh_put(&resfh);
131 return status;
132}
133
134static int
135do_open_fhandle(struct svc_rqst *rqstp, struct svc_fh *current_fh, struct nfsd4_open *open)
136{
137 int status;
138
139 /* Only reclaims from previously confirmed clients are valid */
140 if ((status = nfs4_check_open_reclaim(&open->op_clientid)))
141 return status;
142
143 /* We don't know the target directory, and therefore can not
144 * set the change info
145 */
146
147 memset(&open->op_cinfo, 0, sizeof(struct nfsd4_change_info));
148
149 /* set replay cache */
150 open->op_stateowner->so_replay.rp_openfh_len = current_fh->fh_handle.fh_size;
151 memcpy(open->op_stateowner->so_replay.rp_openfh,
152 &current_fh->fh_handle.fh_base,
153 current_fh->fh_handle.fh_size);
154
155 open->op_truncate = (open->op_iattr.ia_valid & ATTR_SIZE) &&
156 (open->op_iattr.ia_size == 0);
157
158 status = do_open_permission(rqstp, current_fh, open);
159
160 return status;
161}
162
163
164static inline int
Neil Brownf2327d92005-09-13 01:25:37 -0700165nfsd4_open(struct svc_rqst *rqstp, struct svc_fh *current_fh, struct nfsd4_open *open, struct nfs4_stateowner **replay_owner)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700166{
167 int status;
168 dprintk("NFSD: nfsd4_open filename %.*s op_stateowner %p\n",
169 (int)open->op_fname.len, open->op_fname.data,
170 open->op_stateowner);
171
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172 /* This check required by spec. */
173 if (open->op_create && open->op_claim_type != NFS4_OPEN_CLAIM_NULL)
174 return nfserr_inval;
175
176 nfs4_lock_state();
177
178 /* check seqid for replay. set nfs4_owner */
179 status = nfsd4_process_open1(open);
180 if (status == NFSERR_REPLAY_ME) {
181 struct nfs4_replay *rp = &open->op_stateowner->so_replay;
182 fh_put(current_fh);
183 current_fh->fh_handle.fh_size = rp->rp_openfh_len;
184 memcpy(&current_fh->fh_handle.fh_base, rp->rp_openfh,
185 rp->rp_openfh_len);
186 status = fh_verify(rqstp, current_fh, 0, MAY_NOP);
187 if (status)
188 dprintk("nfsd4_open: replay failed"
189 " restoring previous filehandle\n");
190 else
191 status = NFSERR_REPLAY_ME;
192 }
193 if (status)
194 goto out;
J. Bruce Fieldsfb553c02006-01-18 17:43:36 -0800195
196 /* Openowner is now set, so sequence id will get bumped. Now we need
197 * these checks before we do any creates: */
J. Bruce Fieldscbd0d512006-02-07 12:58:32 -0800198 status = nfserr_grace;
J. Bruce Fieldsfb553c02006-01-18 17:43:36 -0800199 if (nfs4_in_grace() && open->op_claim_type != NFS4_OPEN_CLAIM_PREVIOUS)
J. Bruce Fieldscbd0d512006-02-07 12:58:32 -0800200 goto out;
201 status = nfserr_no_grace;
J. Bruce Fieldsfb553c02006-01-18 17:43:36 -0800202 if (!nfs4_in_grace() && open->op_claim_type == NFS4_OPEN_CLAIM_PREVIOUS)
J. Bruce Fieldscbd0d512006-02-07 12:58:32 -0800203 goto out;
J. Bruce Fieldsfb553c02006-01-18 17:43:36 -0800204
Linus Torvalds1da177e2005-04-16 15:20:36 -0700205 switch (open->op_claim_type) {
NeilBrown0dd3c192005-06-23 22:02:56 -0700206 case NFS4_OPEN_CLAIM_DELEGATE_CUR:
207 status = nfserr_inval;
208 if (open->op_create)
209 goto out;
210 /* fall through */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700211 case NFS4_OPEN_CLAIM_NULL:
212 /*
213 * (1) set CURRENT_FH to the file being opened,
214 * creating it if necessary, (2) set open->op_cinfo,
215 * (3) set open->op_truncate if the file is to be
216 * truncated after opening, (4) do permission checking.
217 */
218 status = do_open_lookup(rqstp, current_fh, open);
219 if (status)
220 goto out;
221 break;
222 case NFS4_OPEN_CLAIM_PREVIOUS:
J. Bruce Fieldsa5258252006-01-18 17:43:30 -0800223 open->op_stateowner->so_confirmed = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700224 /*
225 * The CURRENT_FH is already set to the file being
226 * opened. (1) set open->op_cinfo, (2) set
227 * open->op_truncate if the file is to be truncated
228 * after opening, (3) do permission checking.
229 */
230 status = do_open_fhandle(rqstp, current_fh, open);
231 if (status)
232 goto out;
233 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700234 case NFS4_OPEN_CLAIM_DELEGATE_PREV:
J. Bruce Fieldsa5258252006-01-18 17:43:30 -0800235 open->op_stateowner->so_confirmed = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700236 printk("NFSD: unsupported OPEN claim type %d\n",
237 open->op_claim_type);
238 status = nfserr_notsupp;
239 goto out;
240 default:
241 printk("NFSD: Invalid OPEN claim type %d\n",
242 open->op_claim_type);
243 status = nfserr_inval;
244 goto out;
245 }
246 /*
247 * nfsd4_process_open2() does the actual opening of the file. If
248 * successful, it (1) truncates the file if open->op_truncate was
249 * set, (2) sets open->op_stateid, (3) sets open->op_delegation.
250 */
251 status = nfsd4_process_open2(rqstp, current_fh, open);
252out:
Neil Brownf2327d92005-09-13 01:25:37 -0700253 if (open->op_stateowner) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700254 nfs4_get_stateowner(open->op_stateowner);
Neil Brownf2327d92005-09-13 01:25:37 -0700255 *replay_owner = open->op_stateowner;
256 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700257 nfs4_unlock_state();
258 return status;
259}
260
261/*
262 * filehandle-manipulating ops.
263 */
264static inline int
265nfsd4_getfh(struct svc_fh *current_fh, struct svc_fh **getfh)
266{
267 if (!current_fh->fh_dentry)
268 return nfserr_nofilehandle;
269
270 *getfh = current_fh;
271 return nfs_ok;
272}
273
274static inline int
275nfsd4_putfh(struct svc_rqst *rqstp, struct svc_fh *current_fh, struct nfsd4_putfh *putfh)
276{
277 fh_put(current_fh);
278 current_fh->fh_handle.fh_size = putfh->pf_fhlen;
279 memcpy(&current_fh->fh_handle.fh_base, putfh->pf_fhval, putfh->pf_fhlen);
280 return fh_verify(rqstp, current_fh, 0, MAY_NOP);
281}
282
283static inline int
284nfsd4_putrootfh(struct svc_rqst *rqstp, struct svc_fh *current_fh)
285{
286 int status;
287
288 fh_put(current_fh);
289 status = exp_pseudoroot(rqstp->rq_client, current_fh,
290 &rqstp->rq_chandle);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700291 return status;
292}
293
294static inline int
295nfsd4_restorefh(struct svc_fh *current_fh, struct svc_fh *save_fh)
296{
297 if (!save_fh->fh_dentry)
298 return nfserr_restorefh;
299
300 fh_dup2(current_fh, save_fh);
301 return nfs_ok;
302}
303
304static inline int
305nfsd4_savefh(struct svc_fh *current_fh, struct svc_fh *save_fh)
306{
307 if (!current_fh->fh_dentry)
308 return nfserr_nofilehandle;
309
310 fh_dup2(save_fh, current_fh);
311 return nfs_ok;
312}
313
314/*
315 * misc nfsv4 ops
316 */
317static inline int
318nfsd4_access(struct svc_rqst *rqstp, struct svc_fh *current_fh, struct nfsd4_access *access)
319{
320 if (access->ac_req_access & ~NFS3_ACCESS_FULL)
321 return nfserr_inval;
322
323 access->ac_resp_access = access->ac_req_access;
324 return nfsd_access(rqstp, current_fh, &access->ac_resp_access, &access->ac_supported);
325}
326
327static inline int
328nfsd4_commit(struct svc_rqst *rqstp, struct svc_fh *current_fh, struct nfsd4_commit *commit)
329{
330 int status;
331
332 u32 *p = (u32 *)commit->co_verf.data;
333 *p++ = nfssvc_boot.tv_sec;
334 *p++ = nfssvc_boot.tv_usec;
335
336 status = nfsd_commit(rqstp, current_fh, commit->co_offset, commit->co_count);
337 if (status == nfserr_symlink)
338 status = nfserr_inval;
339 return status;
340}
341
342static int
343nfsd4_create(struct svc_rqst *rqstp, struct svc_fh *current_fh, struct nfsd4_create *create)
344{
345 struct svc_fh resfh;
346 int status;
347 dev_t rdev;
348
349 fh_init(&resfh, NFS4_FHSIZE);
350
351 status = fh_verify(rqstp, current_fh, S_IFDIR, MAY_CREATE);
352 if (status == nfserr_symlink)
353 status = nfserr_notdir;
354 if (status)
355 return status;
356
357 switch (create->cr_type) {
358 case NF4LNK:
359 /* ugh! we have to null-terminate the linktext, or
360 * vfs_symlink() will choke. it is always safe to
361 * null-terminate by brute force, since at worst we
362 * will overwrite the first byte of the create namelen
363 * in the XDR buffer, which has already been extracted
364 * during XDR decode.
365 */
366 create->cr_linkname[create->cr_linklen] = 0;
367
368 status = nfsd_symlink(rqstp, current_fh, create->cr_name,
369 create->cr_namelen, create->cr_linkname,
370 create->cr_linklen, &resfh, &create->cr_iattr);
371 break;
372
373 case NF4BLK:
374 rdev = MKDEV(create->cr_specdata1, create->cr_specdata2);
375 if (MAJOR(rdev) != create->cr_specdata1 ||
376 MINOR(rdev) != create->cr_specdata2)
377 return nfserr_inval;
378 status = nfsd_create(rqstp, current_fh, create->cr_name,
379 create->cr_namelen, &create->cr_iattr,
380 S_IFBLK, rdev, &resfh);
381 break;
382
383 case NF4CHR:
384 rdev = MKDEV(create->cr_specdata1, create->cr_specdata2);
385 if (MAJOR(rdev) != create->cr_specdata1 ||
386 MINOR(rdev) != create->cr_specdata2)
387 return nfserr_inval;
388 status = nfsd_create(rqstp, current_fh, create->cr_name,
389 create->cr_namelen, &create->cr_iattr,
390 S_IFCHR, rdev, &resfh);
391 break;
392
393 case NF4SOCK:
394 status = nfsd_create(rqstp, current_fh, create->cr_name,
395 create->cr_namelen, &create->cr_iattr,
396 S_IFSOCK, 0, &resfh);
397 break;
398
399 case NF4FIFO:
400 status = nfsd_create(rqstp, current_fh, create->cr_name,
401 create->cr_namelen, &create->cr_iattr,
402 S_IFIFO, 0, &resfh);
403 break;
404
405 case NF4DIR:
406 create->cr_iattr.ia_valid &= ~ATTR_SIZE;
407 status = nfsd_create(rqstp, current_fh, create->cr_name,
408 create->cr_namelen, &create->cr_iattr,
409 S_IFDIR, 0, &resfh);
410 break;
411
412 default:
413 status = nfserr_badtype;
414 }
415
416 if (!status) {
417 fh_unlock(current_fh);
418 set_change_info(&create->cr_cinfo, current_fh);
419 fh_dup2(current_fh, &resfh);
420 }
421
422 fh_put(&resfh);
423 return status;
424}
425
426static inline int
427nfsd4_getattr(struct svc_rqst *rqstp, struct svc_fh *current_fh, struct nfsd4_getattr *getattr)
428{
429 int status;
430
431 status = fh_verify(rqstp, current_fh, 0, MAY_NOP);
432 if (status)
433 return status;
434
435 if (getattr->ga_bmval[1] & NFSD_WRITEONLY_ATTRS_WORD1)
436 return nfserr_inval;
437
438 getattr->ga_bmval[0] &= NFSD_SUPPORTED_ATTRS_WORD0;
439 getattr->ga_bmval[1] &= NFSD_SUPPORTED_ATTRS_WORD1;
440
441 getattr->ga_fhp = current_fh;
442 return nfs_ok;
443}
444
445static inline int
446nfsd4_link(struct svc_rqst *rqstp, struct svc_fh *current_fh,
447 struct svc_fh *save_fh, struct nfsd4_link *link)
448{
449 int status = nfserr_nofilehandle;
450
451 if (!save_fh->fh_dentry)
452 return status;
453 status = nfsd_link(rqstp, current_fh, link->li_name, link->li_namelen, save_fh);
454 if (!status)
455 set_change_info(&link->li_cinfo, current_fh);
456 return status;
457}
458
459static int
460nfsd4_lookupp(struct svc_rqst *rqstp, struct svc_fh *current_fh)
461{
462 struct svc_fh tmp_fh;
463 int ret;
464
465 fh_init(&tmp_fh, NFS4_FHSIZE);
466 if((ret = exp_pseudoroot(rqstp->rq_client, &tmp_fh,
467 &rqstp->rq_chandle)) != 0)
468 return ret;
469 if (tmp_fh.fh_dentry == current_fh->fh_dentry) {
470 fh_put(&tmp_fh);
471 return nfserr_noent;
472 }
473 fh_put(&tmp_fh);
474 return nfsd_lookup(rqstp, current_fh, "..", 2, current_fh);
475}
476
477static inline int
478nfsd4_lookup(struct svc_rqst *rqstp, struct svc_fh *current_fh, struct nfsd4_lookup *lookup)
479{
480 return nfsd_lookup(rqstp, current_fh, lookup->lo_name, lookup->lo_len, current_fh);
481}
482
483static inline int
484nfsd4_read(struct svc_rqst *rqstp, struct svc_fh *current_fh, struct nfsd4_read *read)
485{
486 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700487
488 /* no need to check permission - this will be done in nfsd_read() */
489
NeilBrown7e06b7f2005-06-23 22:03:13 -0700490 read->rd_filp = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700491 if (read->rd_offset >= OFFSET_MAX)
492 return nfserr_inval;
493
494 nfs4_lock_state();
495 /* check stateid */
496 if ((status = nfs4_preprocess_stateid_op(current_fh, &read->rd_stateid,
NeilBrown7e06b7f2005-06-23 22:03:13 -0700497 CHECK_FH | RD_STATE, &read->rd_filp))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700498 dprintk("NFSD: nfsd4_read: couldn't process stateid!\n");
499 goto out;
500 }
NeilBrown7e06b7f2005-06-23 22:03:13 -0700501 if (read->rd_filp)
502 get_file(read->rd_filp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700503 status = nfs_ok;
504out:
505 nfs4_unlock_state();
506 read->rd_rqstp = rqstp;
507 read->rd_fhp = current_fh;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700508 return status;
509}
510
511static inline int
512nfsd4_readdir(struct svc_rqst *rqstp, struct svc_fh *current_fh, struct nfsd4_readdir *readdir)
513{
514 u64 cookie = readdir->rd_cookie;
515 static const nfs4_verifier zeroverf;
516
517 /* no need to check permission - this will be done in nfsd_readdir() */
518
519 if (readdir->rd_bmval[1] & NFSD_WRITEONLY_ATTRS_WORD1)
520 return nfserr_inval;
521
522 readdir->rd_bmval[0] &= NFSD_SUPPORTED_ATTRS_WORD0;
523 readdir->rd_bmval[1] &= NFSD_SUPPORTED_ATTRS_WORD1;
524
525 if ((cookie > ~(u32)0) || (cookie == 1) || (cookie == 2) ||
526 (cookie == 0 && memcmp(readdir->rd_verf.data, zeroverf.data, NFS4_VERIFIER_SIZE)))
527 return nfserr_bad_cookie;
528
529 readdir->rd_rqstp = rqstp;
530 readdir->rd_fhp = current_fh;
531 return nfs_ok;
532}
533
534static inline int
535nfsd4_readlink(struct svc_rqst *rqstp, struct svc_fh *current_fh, struct nfsd4_readlink *readlink)
536{
537 readlink->rl_rqstp = rqstp;
538 readlink->rl_fhp = current_fh;
539 return nfs_ok;
540}
541
542static inline int
543nfsd4_remove(struct svc_rqst *rqstp, struct svc_fh *current_fh, struct nfsd4_remove *remove)
544{
545 int status;
546
NeilBrownc815afc2005-06-23 22:03:00 -0700547 if (nfs4_in_grace())
548 return nfserr_grace;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700549 status = nfsd_unlink(rqstp, current_fh, 0, remove->rm_name, remove->rm_namelen);
550 if (status == nfserr_symlink)
551 return nfserr_notdir;
552 if (!status) {
553 fh_unlock(current_fh);
554 set_change_info(&remove->rm_cinfo, current_fh);
555 }
556 return status;
557}
558
559static inline int
560nfsd4_rename(struct svc_rqst *rqstp, struct svc_fh *current_fh,
561 struct svc_fh *save_fh, struct nfsd4_rename *rename)
562{
563 int status = nfserr_nofilehandle;
564
565 if (!save_fh->fh_dentry)
566 return status;
NeilBrownc815afc2005-06-23 22:03:00 -0700567 if (nfs4_in_grace() && !(save_fh->fh_export->ex_flags
568 & NFSEXP_NOSUBTREECHECK))
569 return nfserr_grace;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700570 status = nfsd_rename(rqstp, save_fh, rename->rn_sname,
571 rename->rn_snamelen, current_fh,
572 rename->rn_tname, rename->rn_tnamelen);
573
574 /* the underlying filesystem returns different error's than required
575 * by NFSv4. both save_fh and current_fh have been verified.. */
576 if (status == nfserr_isdir)
577 status = nfserr_exist;
578 else if ((status == nfserr_notdir) &&
579 (S_ISDIR(save_fh->fh_dentry->d_inode->i_mode) &&
580 S_ISDIR(current_fh->fh_dentry->d_inode->i_mode)))
581 status = nfserr_exist;
582 else if (status == nfserr_symlink)
583 status = nfserr_notdir;
584
585 if (!status) {
586 set_change_info(&rename->rn_sinfo, current_fh);
587 set_change_info(&rename->rn_tinfo, save_fh);
588 }
589 return status;
590}
591
592static inline int
593nfsd4_setattr(struct svc_rqst *rqstp, struct svc_fh *current_fh, struct nfsd4_setattr *setattr)
594{
595 int status = nfs_ok;
596
Linus Torvalds1da177e2005-04-16 15:20:36 -0700597 if (setattr->sa_iattr.ia_valid & ATTR_SIZE) {
598 nfs4_lock_state();
J. Bruce Fields375c5542006-01-18 17:43:33 -0800599 status = nfs4_preprocess_stateid_op(current_fh,
600 &setattr->sa_stateid, CHECK_FH | WR_STATE, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700601 nfs4_unlock_state();
J. Bruce Fields375c5542006-01-18 17:43:33 -0800602 if (status) {
Greg Banks3e3b4802006-10-02 02:17:41 -0700603 dprintk("NFSD: nfsd4_setattr: couldn't process stateid!\n");
J. Bruce Fields375c5542006-01-18 17:43:33 -0800604 return status;
605 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700606 }
607 status = nfs_ok;
608 if (setattr->sa_acl != NULL)
609 status = nfsd4_set_nfs4_acl(rqstp, current_fh, setattr->sa_acl);
610 if (status)
J. Bruce Fields375c5542006-01-18 17:43:33 -0800611 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700612 status = nfsd_setattr(rqstp, current_fh, &setattr->sa_iattr,
613 0, (time_t)0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700614 return status;
615}
616
617static inline int
618nfsd4_write(struct svc_rqst *rqstp, struct svc_fh *current_fh, struct nfsd4_write *write)
619{
620 stateid_t *stateid = &write->wr_stateid;
621 struct file *filp = NULL;
622 u32 *p;
623 int status = nfs_ok;
624
625 /* no need to check permission - this will be done in nfsd_write() */
626
627 if (write->wr_offset >= OFFSET_MAX)
628 return nfserr_inval;
629
630 nfs4_lock_state();
J. Bruce Fields375c5542006-01-18 17:43:33 -0800631 status = nfs4_preprocess_stateid_op(current_fh, stateid,
632 CHECK_FH | WR_STATE, &filp);
NeilBrown7e06b7f2005-06-23 22:03:13 -0700633 if (filp)
634 get_file(filp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700635 nfs4_unlock_state();
636
J. Bruce Fields375c5542006-01-18 17:43:33 -0800637 if (status) {
638 dprintk("NFSD: nfsd4_write: couldn't process stateid!\n");
639 return status;
640 }
641
Linus Torvalds1da177e2005-04-16 15:20:36 -0700642 write->wr_bytes_written = write->wr_buflen;
643 write->wr_how_written = write->wr_stable_how;
644 p = (u32 *)write->wr_verifier.data;
645 *p++ = nfssvc_boot.tv_sec;
646 *p++ = nfssvc_boot.tv_usec;
647
648 status = nfsd_write(rqstp, current_fh, filp, write->wr_offset,
NeilBrown3cc03b12006-10-04 02:15:47 -0700649 rqstp->rq_vec, write->wr_vlen, write->wr_buflen,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700650 &write->wr_how_written);
NeilBrown7e06b7f2005-06-23 22:03:13 -0700651 if (filp)
652 fput(filp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700653
654 if (status == nfserr_symlink)
655 status = nfserr_inval;
656 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700657}
658
659/* This routine never returns NFS_OK! If there are no other errors, it
660 * will return NFSERR_SAME or NFSERR_NOT_SAME depending on whether the
661 * attributes matched. VERIFY is implemented by mapping NFSERR_SAME
662 * to NFS_OK after the call; NVERIFY by mapping NFSERR_NOT_SAME to NFS_OK.
663 */
664static int
665nfsd4_verify(struct svc_rqst *rqstp, struct svc_fh *current_fh, struct nfsd4_verify *verify)
666{
667 u32 *buf, *p;
668 int count;
669 int status;
670
671 status = fh_verify(rqstp, current_fh, 0, MAY_NOP);
672 if (status)
673 return status;
674
675 if ((verify->ve_bmval[0] & ~NFSD_SUPPORTED_ATTRS_WORD0)
676 || (verify->ve_bmval[1] & ~NFSD_SUPPORTED_ATTRS_WORD1))
677 return nfserr_attrnotsupp;
678 if ((verify->ve_bmval[0] & FATTR4_WORD0_RDATTR_ERROR)
679 || (verify->ve_bmval[1] & NFSD_WRITEONLY_ATTRS_WORD1))
680 return nfserr_inval;
681 if (verify->ve_attrlen & 3)
682 return nfserr_inval;
683
684 /* count in words:
685 * bitmap_len(1) + bitmap(2) + attr_len(1) = 4
686 */
687 count = 4 + (verify->ve_attrlen >> 2);
688 buf = kmalloc(count << 2, GFP_KERNEL);
689 if (!buf)
690 return nfserr_resource;
691
692 status = nfsd4_encode_fattr(current_fh, current_fh->fh_export,
693 current_fh->fh_dentry, buf,
694 &count, verify->ve_bmval,
695 rqstp);
696
697 /* this means that nfsd4_encode_fattr() ran out of space */
698 if (status == nfserr_resource && count == 0)
699 status = nfserr_not_same;
700 if (status)
701 goto out_kfree;
702
703 p = buf + 3;
704 status = nfserr_not_same;
705 if (ntohl(*p++) != verify->ve_attrlen)
706 goto out_kfree;
707 if (!memcmp(p, verify->ve_attrval, verify->ve_attrlen))
708 status = nfserr_same;
709
710out_kfree:
711 kfree(buf);
712 return status;
713}
714
715/*
716 * NULL call.
717 */
718static int
719nfsd4_proc_null(struct svc_rqst *rqstp, void *argp, void *resp)
720{
721 return nfs_ok;
722}
723
Shankar Anande2b20952006-07-10 04:45:44 -0700724static inline void nfsd4_increment_op_stats(u32 opnum)
725{
726 if (opnum >= FIRST_NFS4_OP && opnum <= LAST_NFS4_OP)
727 nfsdstats.nfs4_opcount[opnum]++;
728}
729
Linus Torvalds1da177e2005-04-16 15:20:36 -0700730
731/*
732 * COMPOUND call.
733 */
734static int
735nfsd4_proc_compound(struct svc_rqst *rqstp,
736 struct nfsd4_compoundargs *args,
737 struct nfsd4_compoundres *resp)
738{
739 struct nfsd4_op *op;
740 struct svc_fh *current_fh = NULL;
741 struct svc_fh *save_fh = NULL;
742 struct nfs4_stateowner *replay_owner = NULL;
743 int slack_space; /* in words, not bytes! */
744 int status;
745
746 status = nfserr_resource;
747 current_fh = kmalloc(sizeof(*current_fh), GFP_KERNEL);
748 if (current_fh == NULL)
749 goto out;
750 fh_init(current_fh, NFS4_FHSIZE);
751 save_fh = kmalloc(sizeof(*save_fh), GFP_KERNEL);
752 if (save_fh == NULL)
753 goto out;
754 fh_init(save_fh, NFS4_FHSIZE);
755
756 resp->xbuf = &rqstp->rq_res;
757 resp->p = rqstp->rq_res.head[0].iov_base + rqstp->rq_res.head[0].iov_len;
758 resp->tagp = resp->p;
759 /* reserve space for: taglen, tag, and opcnt */
760 resp->p += 2 + XDR_QUADLEN(args->taglen);
761 resp->end = rqstp->rq_res.head[0].iov_base + PAGE_SIZE;
762 resp->taglen = args->taglen;
763 resp->tag = args->tag;
764 resp->opcnt = 0;
765 resp->rqstp = rqstp;
766
767 /*
768 * According to RFC3010, this takes precedence over all other errors.
769 */
770 status = nfserr_minor_vers_mismatch;
771 if (args->minorversion > NFSD_SUPPORTED_MINOR_VERSION)
772 goto out;
773
774 status = nfs_ok;
775 while (!status && resp->opcnt < args->opcnt) {
776 op = &args->ops[resp->opcnt++];
777
J. Bruce Fieldsfd445272006-01-18 17:43:23 -0800778 dprintk("nfsv4 compound op #%d: %d\n", resp->opcnt, op->opnum);
779
Linus Torvalds1da177e2005-04-16 15:20:36 -0700780 /*
781 * The XDR decode routines may have pre-set op->status;
782 * for example, if there is a miscellaneous XDR error
783 * it will be set to nfserr_bad_xdr.
784 */
785 if (op->status)
786 goto encode_op;
787
788 /* We must be able to encode a successful response to
789 * this operation, with enough room left over to encode a
790 * failed response to the next operation. If we don't
791 * have enough room, fail with ERR_RESOURCE.
792 */
793/* FIXME - is slack_space *really* words, or bytes??? - neilb */
794 slack_space = (char *)resp->end - (char *)resp->p;
795 if (slack_space < COMPOUND_SLACK_SPACE + COMPOUND_ERR_SLACK_SPACE) {
796 BUG_ON(slack_space < COMPOUND_ERR_SLACK_SPACE);
797 op->status = nfserr_resource;
798 goto encode_op;
799 }
800
801 /* All operations except RENEW, SETCLIENTID, RESTOREFH
802 * SETCLIENTID_CONFIRM, PUTFH and PUTROOTFH
803 * require a valid current filehandle
Linus Torvalds1da177e2005-04-16 15:20:36 -0700804 */
J.Bruce Fields42ca0992006-10-04 02:16:20 -0700805 if (!current_fh->fh_dentry) {
806 if (!((op->opnum == OP_PUTFH) ||
807 (op->opnum == OP_PUTROOTFH) ||
808 (op->opnum == OP_SETCLIENTID) ||
809 (op->opnum == OP_SETCLIENTID_CONFIRM) ||
810 (op->opnum == OP_RENEW) ||
811 (op->opnum == OP_RESTOREFH) ||
812 (op->opnum == OP_RELEASE_LOCKOWNER))) {
813 op->status = nfserr_nofilehandle;
814 goto encode_op;
815 }
816 }
817 /* Check must be done at start of each operation, except
818 * for GETATTR and ops not listed as returning NFS4ERR_MOVED
819 */
820 else if (current_fh->fh_export->ex_fslocs.migrated &&
821 !((op->opnum == OP_GETATTR) ||
822 (op->opnum == OP_PUTROOTFH) ||
823 (op->opnum == OP_PUTPUBFH) ||
824 (op->opnum == OP_RENEW) ||
825 (op->opnum == OP_SETCLIENTID) ||
826 (op->opnum == OP_RELEASE_LOCKOWNER))) {
827 op->status = nfserr_moved;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700828 goto encode_op;
829 }
830 switch (op->opnum) {
831 case OP_ACCESS:
832 op->status = nfsd4_access(rqstp, current_fh, &op->u.access);
833 break;
834 case OP_CLOSE:
Neil Brownf2327d92005-09-13 01:25:37 -0700835 op->status = nfsd4_close(rqstp, current_fh, &op->u.close, &replay_owner);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700836 break;
837 case OP_COMMIT:
838 op->status = nfsd4_commit(rqstp, current_fh, &op->u.commit);
839 break;
840 case OP_CREATE:
841 op->status = nfsd4_create(rqstp, current_fh, &op->u.create);
842 break;
843 case OP_DELEGRETURN:
844 op->status = nfsd4_delegreturn(rqstp, current_fh, &op->u.delegreturn);
845 break;
846 case OP_GETATTR:
847 op->status = nfsd4_getattr(rqstp, current_fh, &op->u.getattr);
848 break;
849 case OP_GETFH:
850 op->status = nfsd4_getfh(current_fh, &op->u.getfh);
851 break;
852 case OP_LINK:
853 op->status = nfsd4_link(rqstp, current_fh, save_fh, &op->u.link);
854 break;
855 case OP_LOCK:
Neil Brownf2327d92005-09-13 01:25:37 -0700856 op->status = nfsd4_lock(rqstp, current_fh, &op->u.lock, &replay_owner);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700857 break;
858 case OP_LOCKT:
859 op->status = nfsd4_lockt(rqstp, current_fh, &op->u.lockt);
860 break;
861 case OP_LOCKU:
Neil Brownf2327d92005-09-13 01:25:37 -0700862 op->status = nfsd4_locku(rqstp, current_fh, &op->u.locku, &replay_owner);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700863 break;
864 case OP_LOOKUP:
865 op->status = nfsd4_lookup(rqstp, current_fh, &op->u.lookup);
866 break;
867 case OP_LOOKUPP:
868 op->status = nfsd4_lookupp(rqstp, current_fh);
869 break;
870 case OP_NVERIFY:
871 op->status = nfsd4_verify(rqstp, current_fh, &op->u.nverify);
872 if (op->status == nfserr_not_same)
873 op->status = nfs_ok;
874 break;
875 case OP_OPEN:
Neil Brownf2327d92005-09-13 01:25:37 -0700876 op->status = nfsd4_open(rqstp, current_fh, &op->u.open, &replay_owner);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700877 break;
878 case OP_OPEN_CONFIRM:
Neil Brownf2327d92005-09-13 01:25:37 -0700879 op->status = nfsd4_open_confirm(rqstp, current_fh, &op->u.open_confirm, &replay_owner);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700880 break;
881 case OP_OPEN_DOWNGRADE:
Neil Brownf2327d92005-09-13 01:25:37 -0700882 op->status = nfsd4_open_downgrade(rqstp, current_fh, &op->u.open_downgrade, &replay_owner);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700883 break;
884 case OP_PUTFH:
885 op->status = nfsd4_putfh(rqstp, current_fh, &op->u.putfh);
886 break;
887 case OP_PUTROOTFH:
888 op->status = nfsd4_putrootfh(rqstp, current_fh);
889 break;
890 case OP_READ:
891 op->status = nfsd4_read(rqstp, current_fh, &op->u.read);
892 break;
893 case OP_READDIR:
894 op->status = nfsd4_readdir(rqstp, current_fh, &op->u.readdir);
895 break;
896 case OP_READLINK:
897 op->status = nfsd4_readlink(rqstp, current_fh, &op->u.readlink);
898 break;
899 case OP_REMOVE:
900 op->status = nfsd4_remove(rqstp, current_fh, &op->u.remove);
901 break;
902 case OP_RENAME:
903 op->status = nfsd4_rename(rqstp, current_fh, save_fh, &op->u.rename);
904 break;
905 case OP_RENEW:
906 op->status = nfsd4_renew(&op->u.renew);
907 break;
908 case OP_RESTOREFH:
909 op->status = nfsd4_restorefh(current_fh, save_fh);
910 break;
911 case OP_SAVEFH:
912 op->status = nfsd4_savefh(current_fh, save_fh);
913 break;
914 case OP_SETATTR:
915 op->status = nfsd4_setattr(rqstp, current_fh, &op->u.setattr);
916 break;
917 case OP_SETCLIENTID:
918 op->status = nfsd4_setclientid(rqstp, &op->u.setclientid);
919 break;
920 case OP_SETCLIENTID_CONFIRM:
921 op->status = nfsd4_setclientid_confirm(rqstp, &op->u.setclientid_confirm);
922 break;
923 case OP_VERIFY:
924 op->status = nfsd4_verify(rqstp, current_fh, &op->u.verify);
925 if (op->status == nfserr_same)
926 op->status = nfs_ok;
927 break;
928 case OP_WRITE:
929 op->status = nfsd4_write(rqstp, current_fh, &op->u.write);
930 break;
931 case OP_RELEASE_LOCKOWNER:
932 op->status = nfsd4_release_lockowner(rqstp, &op->u.release_lockowner);
933 break;
934 default:
935 BUG_ON(op->status == nfs_ok);
936 break;
937 }
938
939encode_op:
940 if (op->status == NFSERR_REPLAY_ME) {
941 op->replay = &replay_owner->so_replay;
942 nfsd4_encode_replay(resp, op);
943 status = op->status = op->replay->rp_status;
944 } else {
945 nfsd4_encode_operation(resp, op);
946 status = op->status;
947 }
948 if (replay_owner && (replay_owner != (void *)(-1))) {
949 nfs4_put_stateowner(replay_owner);
950 replay_owner = NULL;
951 }
NeilBrown7e06b7f2005-06-23 22:03:13 -0700952 /* XXX Ugh, we need to get rid of this kind of special case: */
953 if (op->opnum == OP_READ && op->u.read.rd_filp)
954 fput(op->u.read.rd_filp);
Shankar Anande2b20952006-07-10 04:45:44 -0700955
956 nfsd4_increment_op_stats(op->opnum);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700957 }
958
959out:
960 nfsd4_release_compoundargs(args);
961 if (current_fh)
962 fh_put(current_fh);
963 kfree(current_fh);
964 if (save_fh)
965 fh_put(save_fh);
966 kfree(save_fh);
967 return status;
968}
969
970#define nfs4svc_decode_voidargs NULL
971#define nfs4svc_release_void NULL
972#define nfsd4_voidres nfsd4_voidargs
973#define nfs4svc_release_compound NULL
974struct nfsd4_voidargs { int dummy; };
975
976#define PROC(name, argt, rest, relt, cache, respsize) \
977 { (svc_procfunc) nfsd4_proc_##name, \
978 (kxdrproc_t) nfs4svc_decode_##argt##args, \
979 (kxdrproc_t) nfs4svc_encode_##rest##res, \
980 (kxdrproc_t) nfs4svc_release_##relt, \
981 sizeof(struct nfsd4_##argt##args), \
982 sizeof(struct nfsd4_##rest##res), \
983 0, \
984 cache, \
985 respsize, \
986 }
987
988/*
989 * TODO: At the present time, the NFSv4 server does not do XID caching
990 * of requests. Implementing XID caching would not be a serious problem,
991 * although it would require a mild change in interfaces since one
992 * doesn't know whether an NFSv4 request is idempotent until after the
993 * XDR decode. However, XID caching totally confuses pynfs (Peter
994 * Astrand's regression testsuite for NFSv4 servers), which reuses
995 * XID's liberally, so I've left it unimplemented until pynfs generates
996 * better XID's.
997 */
998static struct svc_procedure nfsd_procedures4[2] = {
999 PROC(null, void, void, void, RC_NOCACHE, 1),
NeilBrown7775f4c2006-04-10 22:55:20 -07001000 PROC(compound, compound, compound, compound, RC_NOCACHE, NFSD_BUFSIZE/4)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001001};
1002
1003struct svc_version nfsd_version4 = {
1004 .vs_vers = 4,
1005 .vs_nproc = 2,
1006 .vs_proc = nfsd_procedures4,
1007 .vs_dispatch = nfsd_dispatch,
1008 .vs_xdrsize = NFS4_SVC_XDRSIZE,
1009};
1010
1011/*
1012 * Local variables:
1013 * c-basic-offset: 8
1014 * End:
1015 */