[PATCH] knfsd: nfsd4: simplify filehandle check
[safe/jmp/linux-2.6] / fs / nfsd / nfs4proc.c
1 /*
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
38 #include <linux/param.h>
39 #include <linux/major.h>
40 #include <linux/slab.h>
41 #include <linux/file.h>
42
43 #include <linux/sunrpc/svc.h>
44 #include <linux/nfsd/nfsd.h>
45 #include <linux/nfsd/cache.h>
46 #include <linux/nfs4.h>
47 #include <linux/nfsd/state.h>
48 #include <linux/nfsd/xdr4.h>
49 #include <linux/nfs4_acl.h>
50
51 #define NFSDDBG_FACILITY                NFSDDBG_PROC
52
53 static inline void
54 fh_dup2(struct svc_fh *dst, struct svc_fh *src)
55 {
56         fh_put(dst);
57         dget(src->fh_dentry);
58         if (src->fh_export)
59                 cache_get(&src->fh_export->h);
60         *dst = *src;
61 }
62
63 static __be32
64 do_open_permission(struct svc_rqst *rqstp, struct svc_fh *current_fh, struct nfsd4_open *open, int accmode)
65 {
66         __be32 status;
67
68         if (open->op_truncate &&
69                 !(open->op_share_access & NFS4_SHARE_ACCESS_WRITE))
70                 return nfserr_inval;
71
72         if (open->op_share_access & NFS4_SHARE_ACCESS_READ)
73                 accmode |= MAY_READ;
74         if (open->op_share_access & NFS4_SHARE_ACCESS_WRITE)
75                 accmode |= (MAY_WRITE | MAY_TRUNC);
76         if (open->op_share_deny & NFS4_SHARE_DENY_WRITE)
77                 accmode |= MAY_WRITE;
78
79         status = fh_verify(rqstp, current_fh, S_IFREG, accmode);
80
81         return status;
82 }
83
84 static __be32
85 do_open_lookup(struct svc_rqst *rqstp, struct svc_fh *current_fh, struct nfsd4_open *open)
86 {
87         struct svc_fh resfh;
88         __be32 status;
89         int created = 0;
90
91         fh_init(&resfh, NFS4_FHSIZE);
92         open->op_truncate = 0;
93
94         if (open->op_create) {
95                 /*
96                  * Note: create modes (UNCHECKED,GUARDED...) are the same
97                  * in NFSv4 as in v3.
98                  */
99                 status = nfsd_create_v3(rqstp, current_fh, open->op_fname.data,
100                                         open->op_fname.len, &open->op_iattr,
101                                         &resfh, open->op_createmode,
102                                         (u32 *)open->op_verf.data, &open->op_truncate, &created);
103         } else {
104                 status = nfsd_lookup(rqstp, current_fh,
105                                      open->op_fname.data, open->op_fname.len, &resfh);
106                 fh_unlock(current_fh);
107         }
108         if (status)
109                 goto out;
110
111         set_change_info(&open->op_cinfo, current_fh);
112
113         /* set reply cache */
114         fh_dup2(current_fh, &resfh);
115         open->op_stateowner->so_replay.rp_openfh_len = resfh.fh_handle.fh_size;
116         memcpy(open->op_stateowner->so_replay.rp_openfh,
117                         &resfh.fh_handle.fh_base, resfh.fh_handle.fh_size);
118
119         if (!created)
120                 status = do_open_permission(rqstp, current_fh, open, MAY_NOP);
121
122 out:
123         fh_put(&resfh);
124         return status;
125 }
126
127 static __be32
128 do_open_fhandle(struct svc_rqst *rqstp, struct svc_fh *current_fh, struct nfsd4_open *open)
129 {
130         __be32 status;
131
132         /* Only reclaims from previously confirmed clients are valid */
133         if ((status = nfs4_check_open_reclaim(&open->op_clientid)))
134                 return status;
135
136         /* We don't know the target directory, and therefore can not
137         * set the change info
138         */
139
140         memset(&open->op_cinfo, 0, sizeof(struct nfsd4_change_info));
141
142         /* set replay cache */
143         open->op_stateowner->so_replay.rp_openfh_len = current_fh->fh_handle.fh_size;
144         memcpy(open->op_stateowner->so_replay.rp_openfh,
145                 &current_fh->fh_handle.fh_base,
146                 current_fh->fh_handle.fh_size);
147
148         open->op_truncate = (open->op_iattr.ia_valid & ATTR_SIZE) &&
149                 (open->op_iattr.ia_size == 0);
150
151         status = do_open_permission(rqstp, current_fh, open, MAY_OWNER_OVERRIDE);
152
153         return status;
154 }
155
156
157 static __be32
158 nfsd4_open(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
159            struct nfsd4_open *open)
160 {
161         __be32 status;
162         dprintk("NFSD: nfsd4_open filename %.*s op_stateowner %p\n",
163                 (int)open->op_fname.len, open->op_fname.data,
164                 open->op_stateowner);
165
166         /* This check required by spec. */
167         if (open->op_create && open->op_claim_type != NFS4_OPEN_CLAIM_NULL)
168                 return nfserr_inval;
169
170         nfs4_lock_state();
171
172         /* check seqid for replay. set nfs4_owner */
173         status = nfsd4_process_open1(open);
174         if (status == nfserr_replay_me) {
175                 struct nfs4_replay *rp = &open->op_stateowner->so_replay;
176                 fh_put(&cstate->current_fh);
177                 cstate->current_fh.fh_handle.fh_size = rp->rp_openfh_len;
178                 memcpy(&cstate->current_fh.fh_handle.fh_base, rp->rp_openfh,
179                                 rp->rp_openfh_len);
180                 status = fh_verify(rqstp, &cstate->current_fh, 0, MAY_NOP);
181                 if (status)
182                         dprintk("nfsd4_open: replay failed"
183                                 " restoring previous filehandle\n");
184                 else
185                         status = nfserr_replay_me;
186         }
187         if (status)
188                 goto out;
189
190         /* Openowner is now set, so sequence id will get bumped.  Now we need
191          * these checks before we do any creates: */
192         status = nfserr_grace;
193         if (nfs4_in_grace() && open->op_claim_type != NFS4_OPEN_CLAIM_PREVIOUS)
194                 goto out;
195         status = nfserr_no_grace;
196         if (!nfs4_in_grace() && open->op_claim_type == NFS4_OPEN_CLAIM_PREVIOUS)
197                 goto out;
198
199         switch (open->op_claim_type) {
200                 case NFS4_OPEN_CLAIM_DELEGATE_CUR:
201                         status = nfserr_inval;
202                         if (open->op_create)
203                                 goto out;
204                         /* fall through */
205                 case NFS4_OPEN_CLAIM_NULL:
206                         /*
207                          * (1) set CURRENT_FH to the file being opened,
208                          * creating it if necessary, (2) set open->op_cinfo,
209                          * (3) set open->op_truncate if the file is to be
210                          * truncated after opening, (4) do permission checking.
211                          */
212                         status = do_open_lookup(rqstp, &cstate->current_fh,
213                                                 open);
214                         if (status)
215                                 goto out;
216                         break;
217                 case NFS4_OPEN_CLAIM_PREVIOUS:
218                         open->op_stateowner->so_confirmed = 1;
219                         /*
220                          * The CURRENT_FH is already set to the file being
221                          * opened.  (1) set open->op_cinfo, (2) set
222                          * open->op_truncate if the file is to be truncated
223                          * after opening, (3) do permission checking.
224                         */
225                         status = do_open_fhandle(rqstp, &cstate->current_fh,
226                                                  open);
227                         if (status)
228                                 goto out;
229                         break;
230                 case NFS4_OPEN_CLAIM_DELEGATE_PREV:
231                         open->op_stateowner->so_confirmed = 1;
232                         printk("NFSD: unsupported OPEN claim type %d\n",
233                                 open->op_claim_type);
234                         status = nfserr_notsupp;
235                         goto out;
236                 default:
237                         printk("NFSD: Invalid OPEN claim type %d\n",
238                                 open->op_claim_type);
239                         status = nfserr_inval;
240                         goto out;
241         }
242         /*
243          * nfsd4_process_open2() does the actual opening of the file.  If
244          * successful, it (1) truncates the file if open->op_truncate was
245          * set, (2) sets open->op_stateid, (3) sets open->op_delegation.
246          */
247         status = nfsd4_process_open2(rqstp, &cstate->current_fh, open);
248 out:
249         if (open->op_stateowner) {
250                 nfs4_get_stateowner(open->op_stateowner);
251                 cstate->replay_owner = open->op_stateowner;
252         }
253         nfs4_unlock_state();
254         return status;
255 }
256
257 /*
258  * filehandle-manipulating ops.
259  */
260 static __be32
261 nfsd4_getfh(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
262             struct svc_fh **getfh)
263 {
264         if (!cstate->current_fh.fh_dentry)
265                 return nfserr_nofilehandle;
266
267         *getfh = &cstate->current_fh;
268         return nfs_ok;
269 }
270
271 static __be32
272 nfsd4_putfh(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
273             struct nfsd4_putfh *putfh)
274 {
275         fh_put(&cstate->current_fh);
276         cstate->current_fh.fh_handle.fh_size = putfh->pf_fhlen;
277         memcpy(&cstate->current_fh.fh_handle.fh_base, putfh->pf_fhval,
278                putfh->pf_fhlen);
279         return fh_verify(rqstp, &cstate->current_fh, 0, MAY_NOP);
280 }
281
282 static __be32
283 nfsd4_putrootfh(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
284                 void *arg)
285 {
286         __be32 status;
287
288         fh_put(&cstate->current_fh);
289         status = exp_pseudoroot(rqstp->rq_client, &cstate->current_fh,
290                               &rqstp->rq_chandle);
291         return status;
292 }
293
294 static __be32
295 nfsd4_restorefh(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
296                 void *arg)
297 {
298         if (!cstate->save_fh.fh_dentry)
299                 return nfserr_restorefh;
300
301         fh_dup2(&cstate->current_fh, &cstate->save_fh);
302         return nfs_ok;
303 }
304
305 static __be32
306 nfsd4_savefh(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
307              void *arg)
308 {
309         if (!cstate->current_fh.fh_dentry)
310                 return nfserr_nofilehandle;
311
312         fh_dup2(&cstate->save_fh, &cstate->current_fh);
313         return nfs_ok;
314 }
315
316 /*
317  * misc nfsv4 ops
318  */
319 static __be32
320 nfsd4_access(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
321              struct nfsd4_access *access)
322 {
323         if (access->ac_req_access & ~NFS3_ACCESS_FULL)
324                 return nfserr_inval;
325
326         access->ac_resp_access = access->ac_req_access;
327         return nfsd_access(rqstp, &cstate->current_fh, &access->ac_resp_access,
328                            &access->ac_supported);
329 }
330
331 static __be32
332 nfsd4_commit(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
333              struct nfsd4_commit *commit)
334 {
335         __be32 status;
336
337         u32 *p = (u32 *)commit->co_verf.data;
338         *p++ = nfssvc_boot.tv_sec;
339         *p++ = nfssvc_boot.tv_usec;
340
341         status = nfsd_commit(rqstp, &cstate->current_fh, commit->co_offset,
342                              commit->co_count);
343         if (status == nfserr_symlink)
344                 status = nfserr_inval;
345         return status;
346 }
347
348 static __be32
349 nfsd4_create(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
350              struct nfsd4_create *create)
351 {
352         struct svc_fh resfh;
353         __be32 status;
354         dev_t rdev;
355
356         fh_init(&resfh, NFS4_FHSIZE);
357
358         status = fh_verify(rqstp, &cstate->current_fh, S_IFDIR, MAY_CREATE);
359         if (status == nfserr_symlink)
360                 status = nfserr_notdir;
361         if (status)
362                 return status;
363
364         switch (create->cr_type) {
365         case NF4LNK:
366                 /* ugh! we have to null-terminate the linktext, or
367                  * vfs_symlink() will choke.  it is always safe to
368                  * null-terminate by brute force, since at worst we
369                  * will overwrite the first byte of the create namelen
370                  * in the XDR buffer, which has already been extracted
371                  * during XDR decode.
372                  */
373                 create->cr_linkname[create->cr_linklen] = 0;
374
375                 status = nfsd_symlink(rqstp, &cstate->current_fh,
376                                       create->cr_name, create->cr_namelen,
377                                       create->cr_linkname, create->cr_linklen,
378                                       &resfh, &create->cr_iattr);
379                 break;
380
381         case NF4BLK:
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, &cstate->current_fh,
387                                      create->cr_name, create->cr_namelen,
388                                      &create->cr_iattr, S_IFBLK, rdev, &resfh);
389                 break;
390
391         case NF4CHR:
392                 rdev = MKDEV(create->cr_specdata1, create->cr_specdata2);
393                 if (MAJOR(rdev) != create->cr_specdata1 ||
394                     MINOR(rdev) != create->cr_specdata2)
395                         return nfserr_inval;
396                 status = nfsd_create(rqstp, &cstate->current_fh,
397                                      create->cr_name, create->cr_namelen,
398                                      &create->cr_iattr,S_IFCHR, rdev, &resfh);
399                 break;
400
401         case NF4SOCK:
402                 status = nfsd_create(rqstp, &cstate->current_fh,
403                                      create->cr_name, create->cr_namelen,
404                                      &create->cr_iattr, S_IFSOCK, 0, &resfh);
405                 break;
406
407         case NF4FIFO:
408                 status = nfsd_create(rqstp, &cstate->current_fh,
409                                      create->cr_name, create->cr_namelen,
410                                      &create->cr_iattr, S_IFIFO, 0, &resfh);
411                 break;
412
413         case NF4DIR:
414                 create->cr_iattr.ia_valid &= ~ATTR_SIZE;
415                 status = nfsd_create(rqstp, &cstate->current_fh,
416                                      create->cr_name, create->cr_namelen,
417                                      &create->cr_iattr, S_IFDIR, 0, &resfh);
418                 break;
419
420         default:
421                 status = nfserr_badtype;
422         }
423
424         if (!status) {
425                 fh_unlock(&cstate->current_fh);
426                 set_change_info(&create->cr_cinfo, &cstate->current_fh);
427                 fh_dup2(&cstate->current_fh, &resfh);
428         }
429
430         fh_put(&resfh);
431         return status;
432 }
433
434 static __be32
435 nfsd4_getattr(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
436               struct nfsd4_getattr *getattr)
437 {
438         __be32 status;
439
440         status = fh_verify(rqstp, &cstate->current_fh, 0, MAY_NOP);
441         if (status)
442                 return status;
443
444         if (getattr->ga_bmval[1] & NFSD_WRITEONLY_ATTRS_WORD1)
445                 return nfserr_inval;
446
447         getattr->ga_bmval[0] &= NFSD_SUPPORTED_ATTRS_WORD0;
448         getattr->ga_bmval[1] &= NFSD_SUPPORTED_ATTRS_WORD1;
449
450         getattr->ga_fhp = &cstate->current_fh;
451         return nfs_ok;
452 }
453
454 static __be32
455 nfsd4_link(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
456            struct nfsd4_link *link)
457 {
458         __be32 status = nfserr_nofilehandle;
459
460         if (!cstate->save_fh.fh_dentry)
461                 return status;
462         status = nfsd_link(rqstp, &cstate->current_fh,
463                            link->li_name, link->li_namelen, &cstate->save_fh);
464         if (!status)
465                 set_change_info(&link->li_cinfo, &cstate->current_fh);
466         return status;
467 }
468
469 static __be32
470 nfsd4_lookupp(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
471               void *arg)
472 {
473         struct svc_fh tmp_fh;
474         __be32 ret;
475
476         fh_init(&tmp_fh, NFS4_FHSIZE);
477         if((ret = exp_pseudoroot(rqstp->rq_client, &tmp_fh,
478                               &rqstp->rq_chandle)) != 0)
479                 return ret;
480         if (tmp_fh.fh_dentry == cstate->current_fh.fh_dentry) {
481                 fh_put(&tmp_fh);
482                 return nfserr_noent;
483         }
484         fh_put(&tmp_fh);
485         return nfsd_lookup(rqstp, &cstate->current_fh,
486                            "..", 2, &cstate->current_fh);
487 }
488
489 static __be32
490 nfsd4_lookup(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
491              struct nfsd4_lookup *lookup)
492 {
493         return nfsd_lookup(rqstp, &cstate->current_fh,
494                            lookup->lo_name, lookup->lo_len,
495                            &cstate->current_fh);
496 }
497
498 static __be32
499 nfsd4_read(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
500            struct nfsd4_read *read)
501 {
502         __be32 status;
503
504         /* no need to check permission - this will be done in nfsd_read() */
505
506         read->rd_filp = NULL;
507         if (read->rd_offset >= OFFSET_MAX)
508                 return nfserr_inval;
509
510         nfs4_lock_state();
511         /* check stateid */
512         if ((status = nfs4_preprocess_stateid_op(&cstate->current_fh,
513                                 &read->rd_stateid,
514                                 CHECK_FH | RD_STATE, &read->rd_filp))) {
515                 dprintk("NFSD: nfsd4_read: couldn't process stateid!\n");
516                 goto out;
517         }
518         if (read->rd_filp)
519                 get_file(read->rd_filp);
520         status = nfs_ok;
521 out:
522         nfs4_unlock_state();
523         read->rd_rqstp = rqstp;
524         read->rd_fhp = &cstate->current_fh;
525         return status;
526 }
527
528 static __be32
529 nfsd4_readdir(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
530               struct nfsd4_readdir *readdir)
531 {
532         u64 cookie = readdir->rd_cookie;
533         static const nfs4_verifier zeroverf;
534
535         /* no need to check permission - this will be done in nfsd_readdir() */
536
537         if (readdir->rd_bmval[1] & NFSD_WRITEONLY_ATTRS_WORD1)
538                 return nfserr_inval;
539
540         readdir->rd_bmval[0] &= NFSD_SUPPORTED_ATTRS_WORD0;
541         readdir->rd_bmval[1] &= NFSD_SUPPORTED_ATTRS_WORD1;
542
543         if ((cookie > ~(u32)0) || (cookie == 1) || (cookie == 2) ||
544             (cookie == 0 && memcmp(readdir->rd_verf.data, zeroverf.data, NFS4_VERIFIER_SIZE)))
545                 return nfserr_bad_cookie;
546
547         readdir->rd_rqstp = rqstp;
548         readdir->rd_fhp = &cstate->current_fh;
549         return nfs_ok;
550 }
551
552 static __be32
553 nfsd4_readlink(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
554                struct nfsd4_readlink *readlink)
555 {
556         readlink->rl_rqstp = rqstp;
557         readlink->rl_fhp = &cstate->current_fh;
558         return nfs_ok;
559 }
560
561 static __be32
562 nfsd4_remove(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
563              struct nfsd4_remove *remove)
564 {
565         __be32 status;
566
567         if (nfs4_in_grace())
568                 return nfserr_grace;
569         status = nfsd_unlink(rqstp, &cstate->current_fh, 0,
570                              remove->rm_name, remove->rm_namelen);
571         if (status == nfserr_symlink)
572                 return nfserr_notdir;
573         if (!status) {
574                 fh_unlock(&cstate->current_fh);
575                 set_change_info(&remove->rm_cinfo, &cstate->current_fh);
576         }
577         return status;
578 }
579
580 static __be32
581 nfsd4_rename(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
582              struct nfsd4_rename *rename)
583 {
584         __be32 status = nfserr_nofilehandle;
585
586         if (!cstate->save_fh.fh_dentry)
587                 return status;
588         if (nfs4_in_grace() && !(cstate->save_fh.fh_export->ex_flags
589                                         & NFSEXP_NOSUBTREECHECK))
590                 return nfserr_grace;
591         status = nfsd_rename(rqstp, &cstate->save_fh, rename->rn_sname,
592                              rename->rn_snamelen, &cstate->current_fh,
593                              rename->rn_tname, rename->rn_tnamelen);
594
595         /* the underlying filesystem returns different error's than required
596          * by NFSv4. both save_fh and current_fh have been verified.. */
597         if (status == nfserr_isdir)
598                 status = nfserr_exist;
599         else if ((status == nfserr_notdir) &&
600                   (S_ISDIR(cstate->save_fh.fh_dentry->d_inode->i_mode) &&
601                    S_ISDIR(cstate->current_fh.fh_dentry->d_inode->i_mode)))
602                 status = nfserr_exist;
603         else if (status == nfserr_symlink)
604                 status = nfserr_notdir;
605
606         if (!status) {
607                 set_change_info(&rename->rn_sinfo, &cstate->current_fh);
608                 set_change_info(&rename->rn_tinfo, &cstate->save_fh);
609         }
610         return status;
611 }
612
613 static __be32
614 nfsd4_setattr(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
615               struct nfsd4_setattr *setattr)
616 {
617         __be32 status = nfs_ok;
618
619         if (setattr->sa_iattr.ia_valid & ATTR_SIZE) {
620                 nfs4_lock_state();
621                 status = nfs4_preprocess_stateid_op(&cstate->current_fh,
622                         &setattr->sa_stateid, CHECK_FH | WR_STATE, NULL);
623                 nfs4_unlock_state();
624                 if (status) {
625                         dprintk("NFSD: nfsd4_setattr: couldn't process stateid!\n");
626                         return status;
627                 }
628         }
629         status = nfs_ok;
630         if (setattr->sa_acl != NULL)
631                 status = nfsd4_set_nfs4_acl(rqstp, &cstate->current_fh,
632                                             setattr->sa_acl);
633         if (status)
634                 return status;
635         status = nfsd_setattr(rqstp, &cstate->current_fh, &setattr->sa_iattr,
636                                 0, (time_t)0);
637         return status;
638 }
639
640 static __be32
641 nfsd4_write(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
642             struct nfsd4_write *write)
643 {
644         stateid_t *stateid = &write->wr_stateid;
645         struct file *filp = NULL;
646         u32 *p;
647         __be32 status = nfs_ok;
648
649         /* no need to check permission - this will be done in nfsd_write() */
650
651         if (write->wr_offset >= OFFSET_MAX)
652                 return nfserr_inval;
653
654         nfs4_lock_state();
655         status = nfs4_preprocess_stateid_op(&cstate->current_fh, stateid,
656                                         CHECK_FH | WR_STATE, &filp);
657         if (filp)
658                 get_file(filp);
659         nfs4_unlock_state();
660
661         if (status) {
662                 dprintk("NFSD: nfsd4_write: couldn't process stateid!\n");
663                 return status;
664         }
665
666         write->wr_bytes_written = write->wr_buflen;
667         write->wr_how_written = write->wr_stable_how;
668         p = (u32 *)write->wr_verifier.data;
669         *p++ = nfssvc_boot.tv_sec;
670         *p++ = nfssvc_boot.tv_usec;
671
672         status =  nfsd_write(rqstp, &cstate->current_fh, filp,
673                              write->wr_offset, rqstp->rq_vec, write->wr_vlen,
674                              write->wr_buflen, &write->wr_how_written);
675         if (filp)
676                 fput(filp);
677
678         if (status == nfserr_symlink)
679                 status = nfserr_inval;
680         return status;
681 }
682
683 /* This routine never returns NFS_OK!  If there are no other errors, it
684  * will return NFSERR_SAME or NFSERR_NOT_SAME depending on whether the
685  * attributes matched.  VERIFY is implemented by mapping NFSERR_SAME
686  * to NFS_OK after the call; NVERIFY by mapping NFSERR_NOT_SAME to NFS_OK.
687  */
688 static __be32
689 _nfsd4_verify(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
690              struct nfsd4_verify *verify)
691 {
692         __be32 *buf, *p;
693         int count;
694         __be32 status;
695
696         status = fh_verify(rqstp, &cstate->current_fh, 0, MAY_NOP);
697         if (status)
698                 return status;
699
700         if ((verify->ve_bmval[0] & ~NFSD_SUPPORTED_ATTRS_WORD0)
701             || (verify->ve_bmval[1] & ~NFSD_SUPPORTED_ATTRS_WORD1))
702                 return nfserr_attrnotsupp;
703         if ((verify->ve_bmval[0] & FATTR4_WORD0_RDATTR_ERROR)
704             || (verify->ve_bmval[1] & NFSD_WRITEONLY_ATTRS_WORD1))
705                 return nfserr_inval;
706         if (verify->ve_attrlen & 3)
707                 return nfserr_inval;
708
709         /* count in words:
710          *   bitmap_len(1) + bitmap(2) + attr_len(1) = 4
711          */
712         count = 4 + (verify->ve_attrlen >> 2);
713         buf = kmalloc(count << 2, GFP_KERNEL);
714         if (!buf)
715                 return nfserr_resource;
716
717         status = nfsd4_encode_fattr(&cstate->current_fh,
718                                     cstate->current_fh.fh_export,
719                                     cstate->current_fh.fh_dentry, buf,
720                                     &count, verify->ve_bmval,
721                                     rqstp);
722
723         /* this means that nfsd4_encode_fattr() ran out of space */
724         if (status == nfserr_resource && count == 0)
725                 status = nfserr_not_same;
726         if (status)
727                 goto out_kfree;
728
729         p = buf + 3;
730         status = nfserr_not_same;
731         if (ntohl(*p++) != verify->ve_attrlen)
732                 goto out_kfree;
733         if (!memcmp(p, verify->ve_attrval, verify->ve_attrlen))
734                 status = nfserr_same;
735
736 out_kfree:
737         kfree(buf);
738         return status;
739 }
740
741 static __be32
742 nfsd4_nverify(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
743               struct nfsd4_verify *verify)
744 {
745         __be32 status;
746
747         status = _nfsd4_verify(rqstp, cstate, verify);
748         return status == nfserr_not_same ? nfs_ok : status;
749 }
750
751 static __be32
752 nfsd4_verify(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
753              struct nfsd4_verify *verify)
754 {
755         __be32 status;
756
757         status = _nfsd4_verify(rqstp, cstate, verify);
758         return status == nfserr_same ? nfs_ok : status;
759 }
760
761 /*
762  * NULL call.
763  */
764 static __be32
765 nfsd4_proc_null(struct svc_rqst *rqstp, void *argp, void *resp)
766 {
767         return nfs_ok;
768 }
769
770 static inline void nfsd4_increment_op_stats(u32 opnum)
771 {
772         if (opnum >= FIRST_NFS4_OP && opnum <= LAST_NFS4_OP)
773                 nfsdstats.nfs4_opcount[opnum]++;
774 }
775
776 static void cstate_free(struct nfsd4_compound_state *cstate)
777 {
778         if (cstate == NULL)
779                 return;
780         fh_put(&cstate->current_fh);
781         fh_put(&cstate->save_fh);
782         BUG_ON(cstate->replay_owner);
783         kfree(cstate);
784 }
785
786 static struct nfsd4_compound_state *cstate_alloc(void)
787 {
788         struct nfsd4_compound_state *cstate;
789
790         cstate = kmalloc(sizeof(struct nfsd4_compound_state), GFP_KERNEL);
791         if (cstate == NULL)
792                 return NULL;
793         fh_init(&cstate->current_fh, NFS4_FHSIZE);
794         fh_init(&cstate->save_fh, NFS4_FHSIZE);
795         cstate->replay_owner = NULL;
796         return cstate;
797 }
798
799 typedef __be32(*nfsd4op_func)(struct svc_rqst *, struct nfsd4_compound_state *,
800                               void *);
801
802 struct nfsd4_operation {
803         nfsd4op_func op_func;
804         u32 op_flags;
805 /* Most ops require a valid current filehandle; a few don't: */
806 #define ALLOWED_WITHOUT_FH 1
807 /* GETATTR and ops not listed as returning NFS4ERR_MOVED: */
808 #define ALLOWED_ON_ABSENT_FS 2
809 };
810
811 static struct nfsd4_operation nfsd4_ops[];
812
813 /*
814  * COMPOUND call.
815  */
816 static __be32
817 nfsd4_proc_compound(struct svc_rqst *rqstp,
818                     struct nfsd4_compoundargs *args,
819                     struct nfsd4_compoundres *resp)
820 {
821         struct nfsd4_op *op;
822         struct nfsd4_operation *opdesc;
823         struct nfsd4_compound_state *cstate = NULL;
824         int             slack_bytes;
825         __be32          status;
826
827         status = nfserr_resource;
828         cstate = cstate_alloc();
829         if (cstate == NULL)
830                 goto out;
831
832         resp->xbuf = &rqstp->rq_res;
833         resp->p = rqstp->rq_res.head[0].iov_base + rqstp->rq_res.head[0].iov_len;
834         resp->tagp = resp->p;
835         /* reserve space for: taglen, tag, and opcnt */
836         resp->p += 2 + XDR_QUADLEN(args->taglen);
837         resp->end = rqstp->rq_res.head[0].iov_base + PAGE_SIZE;
838         resp->taglen = args->taglen;
839         resp->tag = args->tag;
840         resp->opcnt = 0;
841         resp->rqstp = rqstp;
842
843         /*
844          * According to RFC3010, this takes precedence over all other errors.
845          */
846         status = nfserr_minor_vers_mismatch;
847         if (args->minorversion > NFSD_SUPPORTED_MINOR_VERSION)
848                 goto out;
849
850         status = nfs_ok;
851         while (!status && resp->opcnt < args->opcnt) {
852                 op = &args->ops[resp->opcnt++];
853
854                 dprintk("nfsv4 compound op #%d: %d\n", resp->opcnt, op->opnum);
855
856                 /*
857                  * The XDR decode routines may have pre-set op->status;
858                  * for example, if there is a miscellaneous XDR error
859                  * it will be set to nfserr_bad_xdr.
860                  */
861                 if (op->status)
862                         goto encode_op;
863
864                 /* We must be able to encode a successful response to
865                  * this operation, with enough room left over to encode a
866                  * failed response to the next operation.  If we don't
867                  * have enough room, fail with ERR_RESOURCE.
868                  */
869                 slack_bytes = (char *)resp->end - (char *)resp->p;
870                 if (slack_bytes < COMPOUND_SLACK_SPACE
871                                 + COMPOUND_ERR_SLACK_SPACE) {
872                         BUG_ON(slack_bytes < COMPOUND_ERR_SLACK_SPACE);
873                         op->status = nfserr_resource;
874                         goto encode_op;
875                 }
876
877                 opdesc = &nfsd4_ops[op->opnum];
878
879                 if (!cstate->current_fh.fh_dentry) {
880                         if (!(opdesc->op_flags & ALLOWED_WITHOUT_FH)) {
881                                 op->status = nfserr_nofilehandle;
882                                 goto encode_op;
883                         }
884                 } else if (cstate->current_fh.fh_export->ex_fslocs.migrated &&
885                           !(opdesc->op_flags & ALLOWED_ON_ABSENT_FS)) {
886                         op->status = nfserr_moved;
887                         goto encode_op;
888                 }
889
890                 if (opdesc->op_func)
891                         op->status = opdesc->op_func(rqstp, cstate, &op->u);
892                 else
893                         BUG_ON(op->status == nfs_ok);
894
895 encode_op:
896                 if (op->status == nfserr_replay_me) {
897                         op->replay = &cstate->replay_owner->so_replay;
898                         nfsd4_encode_replay(resp, op);
899                         status = op->status = op->replay->rp_status;
900                 } else {
901                         nfsd4_encode_operation(resp, op);
902                         status = op->status;
903                 }
904                 if (cstate->replay_owner) {
905                         nfs4_put_stateowner(cstate->replay_owner);
906                         cstate->replay_owner = NULL;
907                 }
908                 /* XXX Ugh, we need to get rid of this kind of special case: */
909                 if (op->opnum == OP_READ && op->u.read.rd_filp)
910                         fput(op->u.read.rd_filp);
911
912                 nfsd4_increment_op_stats(op->opnum);
913         }
914
915 out:
916         nfsd4_release_compoundargs(args);
917         cstate_free(cstate);
918         return status;
919 }
920
921 static struct nfsd4_operation nfsd4_ops[OP_RELEASE_LOCKOWNER+1] = {
922         [OP_ACCESS] = {
923                 .op_func = (nfsd4op_func)nfsd4_access,
924         },
925         [OP_CLOSE] = {
926                 .op_func = (nfsd4op_func)nfsd4_close,
927         },
928         [OP_COMMIT] = {
929                 .op_func = (nfsd4op_func)nfsd4_commit,
930         },
931         [OP_CREATE] = {
932                 .op_func = (nfsd4op_func)nfsd4_create,
933         },
934         [OP_DELEGRETURN] = {
935                 .op_func = (nfsd4op_func)nfsd4_delegreturn,
936         },
937         [OP_GETATTR] = {
938                 .op_func = (nfsd4op_func)nfsd4_getattr,
939                 .op_flags = ALLOWED_ON_ABSENT_FS,
940         },
941         [OP_GETFH] = {
942                 .op_func = (nfsd4op_func)nfsd4_getfh,
943         },
944         [OP_LINK] = {
945                 .op_func = (nfsd4op_func)nfsd4_link,
946         },
947         [OP_LOCK] = {
948                 .op_func = (nfsd4op_func)nfsd4_lock,
949         },
950         [OP_LOCKT] = {
951                 .op_func = (nfsd4op_func)nfsd4_lockt,
952         },
953         [OP_LOCKU] = {
954                 .op_func = (nfsd4op_func)nfsd4_locku,
955         },
956         [OP_LOOKUP] = {
957                 .op_func = (nfsd4op_func)nfsd4_lookup,
958         },
959         [OP_LOOKUPP] = {
960                 .op_func = (nfsd4op_func)nfsd4_lookupp,
961         },
962         [OP_NVERIFY] = {
963                 .op_func = (nfsd4op_func)nfsd4_nverify,
964         },
965         [OP_OPEN] = {
966                 .op_func = (nfsd4op_func)nfsd4_open,
967         },
968         [OP_OPEN_CONFIRM] = {
969                 .op_func = (nfsd4op_func)nfsd4_open_confirm,
970         },
971         [OP_OPEN_DOWNGRADE] = {
972                 .op_func = (nfsd4op_func)nfsd4_open_downgrade,
973         },
974         [OP_PUTFH] = {
975                 .op_func = (nfsd4op_func)nfsd4_putfh,
976                 .op_flags = ALLOWED_WITHOUT_FH | ALLOWED_ON_ABSENT_FS,
977         },
978         [OP_PUTPUBFH] = {
979                 /* unsupported; just for future reference: */
980                 .op_flags = ALLOWED_WITHOUT_FH | ALLOWED_ON_ABSENT_FS,
981         },
982         [OP_PUTROOTFH] = {
983                 .op_func = (nfsd4op_func)nfsd4_putrootfh,
984                 .op_flags = ALLOWED_WITHOUT_FH | ALLOWED_ON_ABSENT_FS,
985         },
986         [OP_READ] = {
987                 .op_func = (nfsd4op_func)nfsd4_read,
988         },
989         [OP_READDIR] = {
990                 .op_func = (nfsd4op_func)nfsd4_readdir,
991         },
992         [OP_READLINK] = {
993                 .op_func = (nfsd4op_func)nfsd4_readlink,
994         },
995         [OP_REMOVE] = {
996                 .op_func = (nfsd4op_func)nfsd4_remove,
997         },
998         [OP_RENAME] = {
999                 .op_func = (nfsd4op_func)nfsd4_rename,
1000         },
1001         [OP_RENEW] = {
1002                 .op_func = (nfsd4op_func)nfsd4_renew,
1003                 .op_flags = ALLOWED_WITHOUT_FH | ALLOWED_ON_ABSENT_FS,
1004         },
1005         [OP_RESTOREFH] = {
1006                 .op_func = (nfsd4op_func)nfsd4_restorefh,
1007                 .op_flags = ALLOWED_WITHOUT_FH | ALLOWED_ON_ABSENT_FS,
1008         },
1009         [OP_SAVEFH] = {
1010                 .op_func = (nfsd4op_func)nfsd4_savefh,
1011         },
1012         [OP_SETATTR] = {
1013                 .op_func = (nfsd4op_func)nfsd4_setattr,
1014         },
1015         [OP_SETCLIENTID] = {
1016                 .op_func = (nfsd4op_func)nfsd4_setclientid,
1017                 .op_flags = ALLOWED_WITHOUT_FH | ALLOWED_ON_ABSENT_FS,
1018         },
1019         [OP_SETCLIENTID_CONFIRM] = {
1020                 .op_func = (nfsd4op_func)nfsd4_setclientid_confirm,
1021                 .op_flags = ALLOWED_WITHOUT_FH | ALLOWED_ON_ABSENT_FS,
1022         },
1023         [OP_VERIFY] = {
1024                 .op_func = (nfsd4op_func)nfsd4_verify,
1025         },
1026         [OP_WRITE] = {
1027                 .op_func = (nfsd4op_func)nfsd4_write,
1028         },
1029         [OP_RELEASE_LOCKOWNER] = {
1030                 .op_func = (nfsd4op_func)nfsd4_release_lockowner,
1031                 .op_flags = ALLOWED_WITHOUT_FH | ALLOWED_ON_ABSENT_FS,
1032         },
1033 };
1034
1035 #define nfs4svc_decode_voidargs         NULL
1036 #define nfs4svc_release_void            NULL
1037 #define nfsd4_voidres                   nfsd4_voidargs
1038 #define nfs4svc_release_compound        NULL
1039 struct nfsd4_voidargs { int dummy; };
1040
1041 #define PROC(name, argt, rest, relt, cache, respsize)   \
1042  { (svc_procfunc) nfsd4_proc_##name,            \
1043    (kxdrproc_t) nfs4svc_decode_##argt##args,    \
1044    (kxdrproc_t) nfs4svc_encode_##rest##res,     \
1045    (kxdrproc_t) nfs4svc_release_##relt,         \
1046    sizeof(struct nfsd4_##argt##args),           \
1047    sizeof(struct nfsd4_##rest##res),            \
1048    0,                                           \
1049    cache,                                       \
1050    respsize,                                    \
1051  }
1052
1053 /*
1054  * TODO: At the present time, the NFSv4 server does not do XID caching
1055  * of requests.  Implementing XID caching would not be a serious problem,
1056  * although it would require a mild change in interfaces since one
1057  * doesn't know whether an NFSv4 request is idempotent until after the
1058  * XDR decode.  However, XID caching totally confuses pynfs (Peter
1059  * Astrand's regression testsuite for NFSv4 servers), which reuses
1060  * XID's liberally, so I've left it unimplemented until pynfs generates
1061  * better XID's.
1062  */
1063 static struct svc_procedure             nfsd_procedures4[2] = {
1064   PROC(null,     void,          void,           void,     RC_NOCACHE, 1),
1065   PROC(compound, compound,      compound,       compound, RC_NOCACHE, NFSD_BUFSIZE/4)
1066 };
1067
1068 struct svc_version      nfsd_version4 = {
1069                 .vs_vers        = 4,
1070                 .vs_nproc       = 2,
1071                 .vs_proc        = nfsd_procedures4,
1072                 .vs_dispatch    = nfsd_dispatch,
1073                 .vs_xdrsize     = NFS4_SVC_XDRSIZE,
1074 };
1075
1076 /*
1077  * Local variables:
1078  *  c-basic-offset: 8
1079  * End:
1080  */