blob: a05d3376cc4669db17f6281b160fe7604ec02585 [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
J. Bruce Fieldsdc730e12006-10-17 00:10:13 -070071do_open_permission(struct svc_rqst *rqstp, struct svc_fh *current_fh, struct nfsd4_open *open, int accmode)
Linus Torvalds1da177e2005-04-16 15:20:36 -070072{
J. Bruce Fieldsdc730e12006-10-17 00:10:13 -070073 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -070074
75 if (open->op_truncate &&
76 !(open->op_share_access & NFS4_SHARE_ACCESS_WRITE))
77 return nfserr_inval;
78
Linus Torvalds1da177e2005-04-16 15:20:36 -070079 if (open->op_share_access & NFS4_SHARE_ACCESS_READ)
J. Bruce Fieldsdc730e12006-10-17 00:10:13 -070080 accmode |= MAY_READ;
Linus Torvalds1da177e2005-04-16 15:20:36 -070081 if (open->op_share_deny & NFS4_SHARE_ACCESS_WRITE)
82 accmode |= (MAY_WRITE | MAY_TRUNC);
Linus Torvalds1da177e2005-04-16 15:20:36 -070083
84 status = fh_verify(rqstp, current_fh, S_IFREG, accmode);
85
86 return status;
87}
88
89static int
90do_open_lookup(struct svc_rqst *rqstp, struct svc_fh *current_fh, struct nfsd4_open *open)
91{
92 struct svc_fh resfh;
93 int status;
94
95 fh_init(&resfh, NFS4_FHSIZE);
96 open->op_truncate = 0;
97
98 if (open->op_create) {
99 /*
100 * Note: create modes (UNCHECKED,GUARDED...) are the same
101 * in NFSv4 as in v3.
102 */
103 status = nfsd_create_v3(rqstp, current_fh, open->op_fname.data,
104 open->op_fname.len, &open->op_iattr,
105 &resfh, open->op_createmode,
106 (u32 *)open->op_verf.data, &open->op_truncate);
107 }
108 else {
109 status = nfsd_lookup(rqstp, current_fh,
110 open->op_fname.data, open->op_fname.len, &resfh);
111 fh_unlock(current_fh);
112 }
113
114 if (!status) {
115 set_change_info(&open->op_cinfo, current_fh);
116
117 /* set reply cache */
118 fh_dup2(current_fh, &resfh);
119 open->op_stateowner->so_replay.rp_openfh_len =
120 resfh.fh_handle.fh_size;
121 memcpy(open->op_stateowner->so_replay.rp_openfh,
122 &resfh.fh_handle.fh_base,
123 resfh.fh_handle.fh_size);
124
J. Bruce Fieldsdc730e12006-10-17 00:10:13 -0700125 status = do_open_permission(rqstp, current_fh, open, MAY_NOP);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700126 }
127
128 fh_put(&resfh);
129 return status;
130}
131
132static int
133do_open_fhandle(struct svc_rqst *rqstp, struct svc_fh *current_fh, struct nfsd4_open *open)
134{
135 int status;
136
137 /* Only reclaims from previously confirmed clients are valid */
138 if ((status = nfs4_check_open_reclaim(&open->op_clientid)))
139 return status;
140
141 /* We don't know the target directory, and therefore can not
142 * set the change info
143 */
144
145 memset(&open->op_cinfo, 0, sizeof(struct nfsd4_change_info));
146
147 /* set replay cache */
148 open->op_stateowner->so_replay.rp_openfh_len = current_fh->fh_handle.fh_size;
149 memcpy(open->op_stateowner->so_replay.rp_openfh,
150 &current_fh->fh_handle.fh_base,
151 current_fh->fh_handle.fh_size);
152
153 open->op_truncate = (open->op_iattr.ia_valid & ATTR_SIZE) &&
154 (open->op_iattr.ia_size == 0);
155
J. Bruce Fieldsdc730e12006-10-17 00:10:13 -0700156 status = do_open_permission(rqstp, current_fh, open, MAY_OWNER_OVERRIDE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700157
158 return status;
159}
160
161
162static inline int
Neil Brownf2327d92005-09-13 01:25:37 -0700163nfsd4_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 -0700164{
165 int status;
166 dprintk("NFSD: nfsd4_open filename %.*s op_stateowner %p\n",
167 (int)open->op_fname.len, open->op_fname.data,
168 open->op_stateowner);
169
Linus Torvalds1da177e2005-04-16 15:20:36 -0700170 /* This check required by spec. */
171 if (open->op_create && open->op_claim_type != NFS4_OPEN_CLAIM_NULL)
172 return nfserr_inval;
173
174 nfs4_lock_state();
175
176 /* check seqid for replay. set nfs4_owner */
177 status = nfsd4_process_open1(open);
178 if (status == NFSERR_REPLAY_ME) {
179 struct nfs4_replay *rp = &open->op_stateowner->so_replay;
180 fh_put(current_fh);
181 current_fh->fh_handle.fh_size = rp->rp_openfh_len;
182 memcpy(&current_fh->fh_handle.fh_base, rp->rp_openfh,
183 rp->rp_openfh_len);
184 status = fh_verify(rqstp, current_fh, 0, MAY_NOP);
185 if (status)
186 dprintk("nfsd4_open: replay failed"
187 " restoring previous filehandle\n");
188 else
189 status = NFSERR_REPLAY_ME;
190 }
191 if (status)
192 goto out;
J. Bruce Fieldsfb553c02006-01-18 17:43:36 -0800193
194 /* Openowner is now set, so sequence id will get bumped. Now we need
195 * these checks before we do any creates: */
J. Bruce Fieldscbd0d512006-02-07 12:58:32 -0800196 status = nfserr_grace;
J. Bruce Fieldsfb553c02006-01-18 17:43:36 -0800197 if (nfs4_in_grace() && open->op_claim_type != NFS4_OPEN_CLAIM_PREVIOUS)
J. Bruce Fieldscbd0d512006-02-07 12:58:32 -0800198 goto out;
199 status = nfserr_no_grace;
J. Bruce Fieldsfb553c02006-01-18 17:43:36 -0800200 if (!nfs4_in_grace() && open->op_claim_type == NFS4_OPEN_CLAIM_PREVIOUS)
J. Bruce Fieldscbd0d512006-02-07 12:58:32 -0800201 goto out;
J. Bruce Fieldsfb553c02006-01-18 17:43:36 -0800202
Linus Torvalds1da177e2005-04-16 15:20:36 -0700203 switch (open->op_claim_type) {
NeilBrown0dd3c192005-06-23 22:02:56 -0700204 case NFS4_OPEN_CLAIM_DELEGATE_CUR:
205 status = nfserr_inval;
206 if (open->op_create)
207 goto out;
208 /* fall through */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700209 case NFS4_OPEN_CLAIM_NULL:
210 /*
211 * (1) set CURRENT_FH to the file being opened,
212 * creating it if necessary, (2) set open->op_cinfo,
213 * (3) set open->op_truncate if the file is to be
214 * truncated after opening, (4) do permission checking.
215 */
216 status = do_open_lookup(rqstp, current_fh, open);
217 if (status)
218 goto out;
219 break;
220 case NFS4_OPEN_CLAIM_PREVIOUS:
J. Bruce Fieldsa5258252006-01-18 17:43:30 -0800221 open->op_stateowner->so_confirmed = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700222 /*
223 * The CURRENT_FH is already set to the file being
224 * opened. (1) set open->op_cinfo, (2) set
225 * open->op_truncate if the file is to be truncated
226 * after opening, (3) do permission checking.
227 */
228 status = do_open_fhandle(rqstp, current_fh, open);
229 if (status)
230 goto out;
231 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700232 case NFS4_OPEN_CLAIM_DELEGATE_PREV:
J. Bruce Fieldsa5258252006-01-18 17:43:30 -0800233 open->op_stateowner->so_confirmed = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700234 printk("NFSD: unsupported OPEN claim type %d\n",
235 open->op_claim_type);
236 status = nfserr_notsupp;
237 goto out;
238 default:
239 printk("NFSD: Invalid OPEN claim type %d\n",
240 open->op_claim_type);
241 status = nfserr_inval;
242 goto out;
243 }
244 /*
245 * nfsd4_process_open2() does the actual opening of the file. If
246 * successful, it (1) truncates the file if open->op_truncate was
247 * set, (2) sets open->op_stateid, (3) sets open->op_delegation.
248 */
249 status = nfsd4_process_open2(rqstp, current_fh, open);
250out:
Neil Brownf2327d92005-09-13 01:25:37 -0700251 if (open->op_stateowner) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700252 nfs4_get_stateowner(open->op_stateowner);
Neil Brownf2327d92005-09-13 01:25:37 -0700253 *replay_owner = open->op_stateowner;
254 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700255 nfs4_unlock_state();
256 return status;
257}
258
259/*
260 * filehandle-manipulating ops.
261 */
262static inline int
263nfsd4_getfh(struct svc_fh *current_fh, struct svc_fh **getfh)
264{
265 if (!current_fh->fh_dentry)
266 return nfserr_nofilehandle;
267
268 *getfh = current_fh;
269 return nfs_ok;
270}
271
272static inline int
273nfsd4_putfh(struct svc_rqst *rqstp, struct svc_fh *current_fh, struct nfsd4_putfh *putfh)
274{
275 fh_put(current_fh);
276 current_fh->fh_handle.fh_size = putfh->pf_fhlen;
277 memcpy(&current_fh->fh_handle.fh_base, putfh->pf_fhval, putfh->pf_fhlen);
278 return fh_verify(rqstp, current_fh, 0, MAY_NOP);
279}
280
281static inline int
282nfsd4_putrootfh(struct svc_rqst *rqstp, struct svc_fh *current_fh)
283{
284 int status;
285
286 fh_put(current_fh);
287 status = exp_pseudoroot(rqstp->rq_client, current_fh,
288 &rqstp->rq_chandle);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700289 return status;
290}
291
292static inline int
293nfsd4_restorefh(struct svc_fh *current_fh, struct svc_fh *save_fh)
294{
295 if (!save_fh->fh_dentry)
296 return nfserr_restorefh;
297
298 fh_dup2(current_fh, save_fh);
299 return nfs_ok;
300}
301
302static inline int
303nfsd4_savefh(struct svc_fh *current_fh, struct svc_fh *save_fh)
304{
305 if (!current_fh->fh_dentry)
306 return nfserr_nofilehandle;
307
308 fh_dup2(save_fh, current_fh);
309 return nfs_ok;
310}
311
312/*
313 * misc nfsv4 ops
314 */
315static inline int
316nfsd4_access(struct svc_rqst *rqstp, struct svc_fh *current_fh, struct nfsd4_access *access)
317{
318 if (access->ac_req_access & ~NFS3_ACCESS_FULL)
319 return nfserr_inval;
320
321 access->ac_resp_access = access->ac_req_access;
322 return nfsd_access(rqstp, current_fh, &access->ac_resp_access, &access->ac_supported);
323}
324
325static inline int
326nfsd4_commit(struct svc_rqst *rqstp, struct svc_fh *current_fh, struct nfsd4_commit *commit)
327{
328 int status;
329
330 u32 *p = (u32 *)commit->co_verf.data;
331 *p++ = nfssvc_boot.tv_sec;
332 *p++ = nfssvc_boot.tv_usec;
333
334 status = nfsd_commit(rqstp, current_fh, commit->co_offset, commit->co_count);
335 if (status == nfserr_symlink)
336 status = nfserr_inval;
337 return status;
338}
339
340static int
341nfsd4_create(struct svc_rqst *rqstp, struct svc_fh *current_fh, struct nfsd4_create *create)
342{
343 struct svc_fh resfh;
344 int status;
345 dev_t rdev;
346
347 fh_init(&resfh, NFS4_FHSIZE);
348
349 status = fh_verify(rqstp, current_fh, S_IFDIR, MAY_CREATE);
350 if (status == nfserr_symlink)
351 status = nfserr_notdir;
352 if (status)
353 return status;
354
355 switch (create->cr_type) {
356 case NF4LNK:
357 /* ugh! we have to null-terminate the linktext, or
358 * vfs_symlink() will choke. it is always safe to
359 * null-terminate by brute force, since at worst we
360 * will overwrite the first byte of the create namelen
361 * in the XDR buffer, which has already been extracted
362 * during XDR decode.
363 */
364 create->cr_linkname[create->cr_linklen] = 0;
365
366 status = nfsd_symlink(rqstp, current_fh, create->cr_name,
367 create->cr_namelen, create->cr_linkname,
368 create->cr_linklen, &resfh, &create->cr_iattr);
369 break;
370
371 case NF4BLK:
372 rdev = MKDEV(create->cr_specdata1, create->cr_specdata2);
373 if (MAJOR(rdev) != create->cr_specdata1 ||
374 MINOR(rdev) != create->cr_specdata2)
375 return nfserr_inval;
376 status = nfsd_create(rqstp, current_fh, create->cr_name,
377 create->cr_namelen, &create->cr_iattr,
378 S_IFBLK, rdev, &resfh);
379 break;
380
381 case NF4CHR:
382 rdev = MKDEV(create->cr_specdata1, create->cr_specdata2);
383 if (MAJOR(rdev) != create->cr_specdata1 ||
384 MINOR(rdev) != create->cr_specdata2)
385 return nfserr_inval;
386 status = nfsd_create(rqstp, current_fh, create->cr_name,
387 create->cr_namelen, &create->cr_iattr,
388 S_IFCHR, rdev, &resfh);
389 break;
390
391 case NF4SOCK:
392 status = nfsd_create(rqstp, current_fh, create->cr_name,
393 create->cr_namelen, &create->cr_iattr,
394 S_IFSOCK, 0, &resfh);
395 break;
396
397 case NF4FIFO:
398 status = nfsd_create(rqstp, current_fh, create->cr_name,
399 create->cr_namelen, &create->cr_iattr,
400 S_IFIFO, 0, &resfh);
401 break;
402
403 case NF4DIR:
404 create->cr_iattr.ia_valid &= ~ATTR_SIZE;
405 status = nfsd_create(rqstp, current_fh, create->cr_name,
406 create->cr_namelen, &create->cr_iattr,
407 S_IFDIR, 0, &resfh);
408 break;
409
410 default:
411 status = nfserr_badtype;
412 }
413
414 if (!status) {
415 fh_unlock(current_fh);
416 set_change_info(&create->cr_cinfo, current_fh);
417 fh_dup2(current_fh, &resfh);
418 }
419
420 fh_put(&resfh);
421 return status;
422}
423
424static inline int
425nfsd4_getattr(struct svc_rqst *rqstp, struct svc_fh *current_fh, struct nfsd4_getattr *getattr)
426{
427 int status;
428
429 status = fh_verify(rqstp, current_fh, 0, MAY_NOP);
430 if (status)
431 return status;
432
433 if (getattr->ga_bmval[1] & NFSD_WRITEONLY_ATTRS_WORD1)
434 return nfserr_inval;
435
436 getattr->ga_bmval[0] &= NFSD_SUPPORTED_ATTRS_WORD0;
437 getattr->ga_bmval[1] &= NFSD_SUPPORTED_ATTRS_WORD1;
438
439 getattr->ga_fhp = current_fh;
440 return nfs_ok;
441}
442
443static inline int
444nfsd4_link(struct svc_rqst *rqstp, struct svc_fh *current_fh,
445 struct svc_fh *save_fh, struct nfsd4_link *link)
446{
447 int status = nfserr_nofilehandle;
448
449 if (!save_fh->fh_dentry)
450 return status;
451 status = nfsd_link(rqstp, current_fh, link->li_name, link->li_namelen, save_fh);
452 if (!status)
453 set_change_info(&link->li_cinfo, current_fh);
454 return status;
455}
456
457static int
458nfsd4_lookupp(struct svc_rqst *rqstp, struct svc_fh *current_fh)
459{
460 struct svc_fh tmp_fh;
461 int ret;
462
463 fh_init(&tmp_fh, NFS4_FHSIZE);
464 if((ret = exp_pseudoroot(rqstp->rq_client, &tmp_fh,
465 &rqstp->rq_chandle)) != 0)
466 return ret;
467 if (tmp_fh.fh_dentry == current_fh->fh_dentry) {
468 fh_put(&tmp_fh);
469 return nfserr_noent;
470 }
471 fh_put(&tmp_fh);
472 return nfsd_lookup(rqstp, current_fh, "..", 2, current_fh);
473}
474
475static inline int
476nfsd4_lookup(struct svc_rqst *rqstp, struct svc_fh *current_fh, struct nfsd4_lookup *lookup)
477{
478 return nfsd_lookup(rqstp, current_fh, lookup->lo_name, lookup->lo_len, current_fh);
479}
480
481static inline int
482nfsd4_read(struct svc_rqst *rqstp, struct svc_fh *current_fh, struct nfsd4_read *read)
483{
484 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700485
486 /* no need to check permission - this will be done in nfsd_read() */
487
NeilBrown7e06b7f2005-06-23 22:03:13 -0700488 read->rd_filp = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700489 if (read->rd_offset >= OFFSET_MAX)
490 return nfserr_inval;
491
492 nfs4_lock_state();
493 /* check stateid */
494 if ((status = nfs4_preprocess_stateid_op(current_fh, &read->rd_stateid,
NeilBrown7e06b7f2005-06-23 22:03:13 -0700495 CHECK_FH | RD_STATE, &read->rd_filp))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700496 dprintk("NFSD: nfsd4_read: couldn't process stateid!\n");
497 goto out;
498 }
NeilBrown7e06b7f2005-06-23 22:03:13 -0700499 if (read->rd_filp)
500 get_file(read->rd_filp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700501 status = nfs_ok;
502out:
503 nfs4_unlock_state();
504 read->rd_rqstp = rqstp;
505 read->rd_fhp = current_fh;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700506 return status;
507}
508
509static inline int
510nfsd4_readdir(struct svc_rqst *rqstp, struct svc_fh *current_fh, struct nfsd4_readdir *readdir)
511{
512 u64 cookie = readdir->rd_cookie;
513 static const nfs4_verifier zeroverf;
514
515 /* no need to check permission - this will be done in nfsd_readdir() */
516
517 if (readdir->rd_bmval[1] & NFSD_WRITEONLY_ATTRS_WORD1)
518 return nfserr_inval;
519
520 readdir->rd_bmval[0] &= NFSD_SUPPORTED_ATTRS_WORD0;
521 readdir->rd_bmval[1] &= NFSD_SUPPORTED_ATTRS_WORD1;
522
523 if ((cookie > ~(u32)0) || (cookie == 1) || (cookie == 2) ||
524 (cookie == 0 && memcmp(readdir->rd_verf.data, zeroverf.data, NFS4_VERIFIER_SIZE)))
525 return nfserr_bad_cookie;
526
527 readdir->rd_rqstp = rqstp;
528 readdir->rd_fhp = current_fh;
529 return nfs_ok;
530}
531
532static inline int
533nfsd4_readlink(struct svc_rqst *rqstp, struct svc_fh *current_fh, struct nfsd4_readlink *readlink)
534{
535 readlink->rl_rqstp = rqstp;
536 readlink->rl_fhp = current_fh;
537 return nfs_ok;
538}
539
540static inline int
541nfsd4_remove(struct svc_rqst *rqstp, struct svc_fh *current_fh, struct nfsd4_remove *remove)
542{
543 int status;
544
NeilBrownc815afc2005-06-23 22:03:00 -0700545 if (nfs4_in_grace())
546 return nfserr_grace;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700547 status = nfsd_unlink(rqstp, current_fh, 0, remove->rm_name, remove->rm_namelen);
548 if (status == nfserr_symlink)
549 return nfserr_notdir;
550 if (!status) {
551 fh_unlock(current_fh);
552 set_change_info(&remove->rm_cinfo, current_fh);
553 }
554 return status;
555}
556
557static inline int
558nfsd4_rename(struct svc_rqst *rqstp, struct svc_fh *current_fh,
559 struct svc_fh *save_fh, struct nfsd4_rename *rename)
560{
561 int status = nfserr_nofilehandle;
562
563 if (!save_fh->fh_dentry)
564 return status;
NeilBrownc815afc2005-06-23 22:03:00 -0700565 if (nfs4_in_grace() && !(save_fh->fh_export->ex_flags
566 & NFSEXP_NOSUBTREECHECK))
567 return nfserr_grace;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700568 status = nfsd_rename(rqstp, save_fh, rename->rn_sname,
569 rename->rn_snamelen, current_fh,
570 rename->rn_tname, rename->rn_tnamelen);
571
572 /* the underlying filesystem returns different error's than required
573 * by NFSv4. both save_fh and current_fh have been verified.. */
574 if (status == nfserr_isdir)
575 status = nfserr_exist;
576 else if ((status == nfserr_notdir) &&
577 (S_ISDIR(save_fh->fh_dentry->d_inode->i_mode) &&
578 S_ISDIR(current_fh->fh_dentry->d_inode->i_mode)))
579 status = nfserr_exist;
580 else if (status == nfserr_symlink)
581 status = nfserr_notdir;
582
583 if (!status) {
584 set_change_info(&rename->rn_sinfo, current_fh);
585 set_change_info(&rename->rn_tinfo, save_fh);
586 }
587 return status;
588}
589
590static inline int
591nfsd4_setattr(struct svc_rqst *rqstp, struct svc_fh *current_fh, struct nfsd4_setattr *setattr)
592{
593 int status = nfs_ok;
594
Linus Torvalds1da177e2005-04-16 15:20:36 -0700595 if (setattr->sa_iattr.ia_valid & ATTR_SIZE) {
596 nfs4_lock_state();
J. Bruce Fields375c5542006-01-18 17:43:33 -0800597 status = nfs4_preprocess_stateid_op(current_fh,
598 &setattr->sa_stateid, CHECK_FH | WR_STATE, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700599 nfs4_unlock_state();
J. Bruce Fields375c5542006-01-18 17:43:33 -0800600 if (status) {
Greg Banks3e3b4802006-10-02 02:17:41 -0700601 dprintk("NFSD: nfsd4_setattr: couldn't process stateid!\n");
J. Bruce Fields375c5542006-01-18 17:43:33 -0800602 return status;
603 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700604 }
605 status = nfs_ok;
606 if (setattr->sa_acl != NULL)
607 status = nfsd4_set_nfs4_acl(rqstp, current_fh, setattr->sa_acl);
608 if (status)
J. Bruce Fields375c5542006-01-18 17:43:33 -0800609 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700610 status = nfsd_setattr(rqstp, current_fh, &setattr->sa_iattr,
611 0, (time_t)0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700612 return status;
613}
614
615static inline int
616nfsd4_write(struct svc_rqst *rqstp, struct svc_fh *current_fh, struct nfsd4_write *write)
617{
618 stateid_t *stateid = &write->wr_stateid;
619 struct file *filp = NULL;
620 u32 *p;
621 int status = nfs_ok;
622
623 /* no need to check permission - this will be done in nfsd_write() */
624
625 if (write->wr_offset >= OFFSET_MAX)
626 return nfserr_inval;
627
628 nfs4_lock_state();
J. Bruce Fields375c5542006-01-18 17:43:33 -0800629 status = nfs4_preprocess_stateid_op(current_fh, stateid,
630 CHECK_FH | WR_STATE, &filp);
NeilBrown7e06b7f2005-06-23 22:03:13 -0700631 if (filp)
632 get_file(filp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700633 nfs4_unlock_state();
634
J. Bruce Fields375c5542006-01-18 17:43:33 -0800635 if (status) {
636 dprintk("NFSD: nfsd4_write: couldn't process stateid!\n");
637 return status;
638 }
639
Linus Torvalds1da177e2005-04-16 15:20:36 -0700640 write->wr_bytes_written = write->wr_buflen;
641 write->wr_how_written = write->wr_stable_how;
642 p = (u32 *)write->wr_verifier.data;
643 *p++ = nfssvc_boot.tv_sec;
644 *p++ = nfssvc_boot.tv_usec;
645
646 status = nfsd_write(rqstp, current_fh, filp, write->wr_offset,
NeilBrown3cc03b12006-10-04 02:15:47 -0700647 rqstp->rq_vec, write->wr_vlen, write->wr_buflen,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700648 &write->wr_how_written);
NeilBrown7e06b7f2005-06-23 22:03:13 -0700649 if (filp)
650 fput(filp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700651
652 if (status == nfserr_symlink)
653 status = nfserr_inval;
654 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700655}
656
657/* This routine never returns NFS_OK! If there are no other errors, it
658 * will return NFSERR_SAME or NFSERR_NOT_SAME depending on whether the
659 * attributes matched. VERIFY is implemented by mapping NFSERR_SAME
660 * to NFS_OK after the call; NVERIFY by mapping NFSERR_NOT_SAME to NFS_OK.
661 */
662static int
663nfsd4_verify(struct svc_rqst *rqstp, struct svc_fh *current_fh, struct nfsd4_verify *verify)
664{
665 u32 *buf, *p;
666 int count;
667 int status;
668
669 status = fh_verify(rqstp, current_fh, 0, MAY_NOP);
670 if (status)
671 return status;
672
673 if ((verify->ve_bmval[0] & ~NFSD_SUPPORTED_ATTRS_WORD0)
674 || (verify->ve_bmval[1] & ~NFSD_SUPPORTED_ATTRS_WORD1))
675 return nfserr_attrnotsupp;
676 if ((verify->ve_bmval[0] & FATTR4_WORD0_RDATTR_ERROR)
677 || (verify->ve_bmval[1] & NFSD_WRITEONLY_ATTRS_WORD1))
678 return nfserr_inval;
679 if (verify->ve_attrlen & 3)
680 return nfserr_inval;
681
682 /* count in words:
683 * bitmap_len(1) + bitmap(2) + attr_len(1) = 4
684 */
685 count = 4 + (verify->ve_attrlen >> 2);
686 buf = kmalloc(count << 2, GFP_KERNEL);
687 if (!buf)
688 return nfserr_resource;
689
690 status = nfsd4_encode_fattr(current_fh, current_fh->fh_export,
691 current_fh->fh_dentry, buf,
692 &count, verify->ve_bmval,
693 rqstp);
694
695 /* this means that nfsd4_encode_fattr() ran out of space */
696 if (status == nfserr_resource && count == 0)
697 status = nfserr_not_same;
698 if (status)
699 goto out_kfree;
700
701 p = buf + 3;
702 status = nfserr_not_same;
703 if (ntohl(*p++) != verify->ve_attrlen)
704 goto out_kfree;
705 if (!memcmp(p, verify->ve_attrval, verify->ve_attrlen))
706 status = nfserr_same;
707
708out_kfree:
709 kfree(buf);
710 return status;
711}
712
713/*
714 * NULL call.
715 */
716static int
717nfsd4_proc_null(struct svc_rqst *rqstp, void *argp, void *resp)
718{
719 return nfs_ok;
720}
721
Shankar Anande2b20952006-07-10 04:45:44 -0700722static inline void nfsd4_increment_op_stats(u32 opnum)
723{
724 if (opnum >= FIRST_NFS4_OP && opnum <= LAST_NFS4_OP)
725 nfsdstats.nfs4_opcount[opnum]++;
726}
727
Linus Torvalds1da177e2005-04-16 15:20:36 -0700728
729/*
730 * COMPOUND call.
731 */
732static int
733nfsd4_proc_compound(struct svc_rqst *rqstp,
734 struct nfsd4_compoundargs *args,
735 struct nfsd4_compoundres *resp)
736{
737 struct nfsd4_op *op;
738 struct svc_fh *current_fh = NULL;
739 struct svc_fh *save_fh = NULL;
740 struct nfs4_stateowner *replay_owner = NULL;
741 int slack_space; /* in words, not bytes! */
742 int status;
743
744 status = nfserr_resource;
745 current_fh = kmalloc(sizeof(*current_fh), GFP_KERNEL);
746 if (current_fh == NULL)
747 goto out;
748 fh_init(current_fh, NFS4_FHSIZE);
749 save_fh = kmalloc(sizeof(*save_fh), GFP_KERNEL);
750 if (save_fh == NULL)
751 goto out;
752 fh_init(save_fh, NFS4_FHSIZE);
753
754 resp->xbuf = &rqstp->rq_res;
755 resp->p = rqstp->rq_res.head[0].iov_base + rqstp->rq_res.head[0].iov_len;
756 resp->tagp = resp->p;
757 /* reserve space for: taglen, tag, and opcnt */
758 resp->p += 2 + XDR_QUADLEN(args->taglen);
759 resp->end = rqstp->rq_res.head[0].iov_base + PAGE_SIZE;
760 resp->taglen = args->taglen;
761 resp->tag = args->tag;
762 resp->opcnt = 0;
763 resp->rqstp = rqstp;
764
765 /*
766 * According to RFC3010, this takes precedence over all other errors.
767 */
768 status = nfserr_minor_vers_mismatch;
769 if (args->minorversion > NFSD_SUPPORTED_MINOR_VERSION)
770 goto out;
771
772 status = nfs_ok;
773 while (!status && resp->opcnt < args->opcnt) {
774 op = &args->ops[resp->opcnt++];
775
J. Bruce Fieldsfd445272006-01-18 17:43:23 -0800776 dprintk("nfsv4 compound op #%d: %d\n", resp->opcnt, op->opnum);
777
Linus Torvalds1da177e2005-04-16 15:20:36 -0700778 /*
779 * The XDR decode routines may have pre-set op->status;
780 * for example, if there is a miscellaneous XDR error
781 * it will be set to nfserr_bad_xdr.
782 */
783 if (op->status)
784 goto encode_op;
785
786 /* We must be able to encode a successful response to
787 * this operation, with enough room left over to encode a
788 * failed response to the next operation. If we don't
789 * have enough room, fail with ERR_RESOURCE.
790 */
791/* FIXME - is slack_space *really* words, or bytes??? - neilb */
792 slack_space = (char *)resp->end - (char *)resp->p;
793 if (slack_space < COMPOUND_SLACK_SPACE + COMPOUND_ERR_SLACK_SPACE) {
794 BUG_ON(slack_space < COMPOUND_ERR_SLACK_SPACE);
795 op->status = nfserr_resource;
796 goto encode_op;
797 }
798
799 /* All operations except RENEW, SETCLIENTID, RESTOREFH
800 * SETCLIENTID_CONFIRM, PUTFH and PUTROOTFH
801 * require a valid current filehandle
Linus Torvalds1da177e2005-04-16 15:20:36 -0700802 */
J.Bruce Fields42ca0992006-10-04 02:16:20 -0700803 if (!current_fh->fh_dentry) {
804 if (!((op->opnum == OP_PUTFH) ||
805 (op->opnum == OP_PUTROOTFH) ||
806 (op->opnum == OP_SETCLIENTID) ||
807 (op->opnum == OP_SETCLIENTID_CONFIRM) ||
808 (op->opnum == OP_RENEW) ||
809 (op->opnum == OP_RESTOREFH) ||
810 (op->opnum == OP_RELEASE_LOCKOWNER))) {
811 op->status = nfserr_nofilehandle;
812 goto encode_op;
813 }
814 }
815 /* Check must be done at start of each operation, except
816 * for GETATTR and ops not listed as returning NFS4ERR_MOVED
817 */
818 else if (current_fh->fh_export->ex_fslocs.migrated &&
819 !((op->opnum == OP_GETATTR) ||
820 (op->opnum == OP_PUTROOTFH) ||
821 (op->opnum == OP_PUTPUBFH) ||
822 (op->opnum == OP_RENEW) ||
823 (op->opnum == OP_SETCLIENTID) ||
824 (op->opnum == OP_RELEASE_LOCKOWNER))) {
825 op->status = nfserr_moved;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700826 goto encode_op;
827 }
828 switch (op->opnum) {
829 case OP_ACCESS:
830 op->status = nfsd4_access(rqstp, current_fh, &op->u.access);
831 break;
832 case OP_CLOSE:
Neil Brownf2327d92005-09-13 01:25:37 -0700833 op->status = nfsd4_close(rqstp, current_fh, &op->u.close, &replay_owner);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700834 break;
835 case OP_COMMIT:
836 op->status = nfsd4_commit(rqstp, current_fh, &op->u.commit);
837 break;
838 case OP_CREATE:
839 op->status = nfsd4_create(rqstp, current_fh, &op->u.create);
840 break;
841 case OP_DELEGRETURN:
842 op->status = nfsd4_delegreturn(rqstp, current_fh, &op->u.delegreturn);
843 break;
844 case OP_GETATTR:
845 op->status = nfsd4_getattr(rqstp, current_fh, &op->u.getattr);
846 break;
847 case OP_GETFH:
848 op->status = nfsd4_getfh(current_fh, &op->u.getfh);
849 break;
850 case OP_LINK:
851 op->status = nfsd4_link(rqstp, current_fh, save_fh, &op->u.link);
852 break;
853 case OP_LOCK:
Neil Brownf2327d92005-09-13 01:25:37 -0700854 op->status = nfsd4_lock(rqstp, current_fh, &op->u.lock, &replay_owner);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700855 break;
856 case OP_LOCKT:
857 op->status = nfsd4_lockt(rqstp, current_fh, &op->u.lockt);
858 break;
859 case OP_LOCKU:
Neil Brownf2327d92005-09-13 01:25:37 -0700860 op->status = nfsd4_locku(rqstp, current_fh, &op->u.locku, &replay_owner);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700861 break;
862 case OP_LOOKUP:
863 op->status = nfsd4_lookup(rqstp, current_fh, &op->u.lookup);
864 break;
865 case OP_LOOKUPP:
866 op->status = nfsd4_lookupp(rqstp, current_fh);
867 break;
868 case OP_NVERIFY:
869 op->status = nfsd4_verify(rqstp, current_fh, &op->u.nverify);
870 if (op->status == nfserr_not_same)
871 op->status = nfs_ok;
872 break;
873 case OP_OPEN:
Neil Brownf2327d92005-09-13 01:25:37 -0700874 op->status = nfsd4_open(rqstp, current_fh, &op->u.open, &replay_owner);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700875 break;
876 case OP_OPEN_CONFIRM:
Neil Brownf2327d92005-09-13 01:25:37 -0700877 op->status = nfsd4_open_confirm(rqstp, current_fh, &op->u.open_confirm, &replay_owner);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700878 break;
879 case OP_OPEN_DOWNGRADE:
Neil Brownf2327d92005-09-13 01:25:37 -0700880 op->status = nfsd4_open_downgrade(rqstp, current_fh, &op->u.open_downgrade, &replay_owner);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700881 break;
882 case OP_PUTFH:
883 op->status = nfsd4_putfh(rqstp, current_fh, &op->u.putfh);
884 break;
885 case OP_PUTROOTFH:
886 op->status = nfsd4_putrootfh(rqstp, current_fh);
887 break;
888 case OP_READ:
889 op->status = nfsd4_read(rqstp, current_fh, &op->u.read);
890 break;
891 case OP_READDIR:
892 op->status = nfsd4_readdir(rqstp, current_fh, &op->u.readdir);
893 break;
894 case OP_READLINK:
895 op->status = nfsd4_readlink(rqstp, current_fh, &op->u.readlink);
896 break;
897 case OP_REMOVE:
898 op->status = nfsd4_remove(rqstp, current_fh, &op->u.remove);
899 break;
900 case OP_RENAME:
901 op->status = nfsd4_rename(rqstp, current_fh, save_fh, &op->u.rename);
902 break;
903 case OP_RENEW:
904 op->status = nfsd4_renew(&op->u.renew);
905 break;
906 case OP_RESTOREFH:
907 op->status = nfsd4_restorefh(current_fh, save_fh);
908 break;
909 case OP_SAVEFH:
910 op->status = nfsd4_savefh(current_fh, save_fh);
911 break;
912 case OP_SETATTR:
913 op->status = nfsd4_setattr(rqstp, current_fh, &op->u.setattr);
914 break;
915 case OP_SETCLIENTID:
916 op->status = nfsd4_setclientid(rqstp, &op->u.setclientid);
917 break;
918 case OP_SETCLIENTID_CONFIRM:
919 op->status = nfsd4_setclientid_confirm(rqstp, &op->u.setclientid_confirm);
920 break;
921 case OP_VERIFY:
922 op->status = nfsd4_verify(rqstp, current_fh, &op->u.verify);
923 if (op->status == nfserr_same)
924 op->status = nfs_ok;
925 break;
926 case OP_WRITE:
927 op->status = nfsd4_write(rqstp, current_fh, &op->u.write);
928 break;
929 case OP_RELEASE_LOCKOWNER:
930 op->status = nfsd4_release_lockowner(rqstp, &op->u.release_lockowner);
931 break;
932 default:
933 BUG_ON(op->status == nfs_ok);
934 break;
935 }
936
937encode_op:
938 if (op->status == NFSERR_REPLAY_ME) {
939 op->replay = &replay_owner->so_replay;
940 nfsd4_encode_replay(resp, op);
941 status = op->status = op->replay->rp_status;
942 } else {
943 nfsd4_encode_operation(resp, op);
944 status = op->status;
945 }
946 if (replay_owner && (replay_owner != (void *)(-1))) {
947 nfs4_put_stateowner(replay_owner);
948 replay_owner = NULL;
949 }
NeilBrown7e06b7f2005-06-23 22:03:13 -0700950 /* XXX Ugh, we need to get rid of this kind of special case: */
951 if (op->opnum == OP_READ && op->u.read.rd_filp)
952 fput(op->u.read.rd_filp);
Shankar Anande2b20952006-07-10 04:45:44 -0700953
954 nfsd4_increment_op_stats(op->opnum);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700955 }
956
957out:
958 nfsd4_release_compoundargs(args);
959 if (current_fh)
960 fh_put(current_fh);
961 kfree(current_fh);
962 if (save_fh)
963 fh_put(save_fh);
964 kfree(save_fh);
965 return status;
966}
967
968#define nfs4svc_decode_voidargs NULL
969#define nfs4svc_release_void NULL
970#define nfsd4_voidres nfsd4_voidargs
971#define nfs4svc_release_compound NULL
972struct nfsd4_voidargs { int dummy; };
973
974#define PROC(name, argt, rest, relt, cache, respsize) \
975 { (svc_procfunc) nfsd4_proc_##name, \
976 (kxdrproc_t) nfs4svc_decode_##argt##args, \
977 (kxdrproc_t) nfs4svc_encode_##rest##res, \
978 (kxdrproc_t) nfs4svc_release_##relt, \
979 sizeof(struct nfsd4_##argt##args), \
980 sizeof(struct nfsd4_##rest##res), \
981 0, \
982 cache, \
983 respsize, \
984 }
985
986/*
987 * TODO: At the present time, the NFSv4 server does not do XID caching
988 * of requests. Implementing XID caching would not be a serious problem,
989 * although it would require a mild change in interfaces since one
990 * doesn't know whether an NFSv4 request is idempotent until after the
991 * XDR decode. However, XID caching totally confuses pynfs (Peter
992 * Astrand's regression testsuite for NFSv4 servers), which reuses
993 * XID's liberally, so I've left it unimplemented until pynfs generates
994 * better XID's.
995 */
996static struct svc_procedure nfsd_procedures4[2] = {
997 PROC(null, void, void, void, RC_NOCACHE, 1),
NeilBrown7775f4c2006-04-10 22:55:20 -0700998 PROC(compound, compound, compound, compound, RC_NOCACHE, NFSD_BUFSIZE/4)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700999};
1000
1001struct svc_version nfsd_version4 = {
1002 .vs_vers = 4,
1003 .vs_nproc = 2,
1004 .vs_proc = nfsd_procedures4,
1005 .vs_dispatch = nfsd_dispatch,
1006 .vs_xdrsize = NFS4_SVC_XDRSIZE,
1007};
1008
1009/*
1010 * Local variables:
1011 * c-basic-offset: 8
1012 * End:
1013 */