[PATCH] nfsd4: nfs4state.c miscellaneous goto removals
[sfrench/cifs-2.6.git] / 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  * 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>
48 #include <linux/file.h>
49
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
60 static inline void
61 fh_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
70 static int
71 do_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
91 static int
92 do_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
134 static int
135 do_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
164 static inline int
165 nfsd4_open(struct svc_rqst *rqstp, struct svc_fh *current_fh, struct nfsd4_open *open, struct nfs4_stateowner **replay_owner)
166 {
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
172         /* 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;
195         switch (open->op_claim_type) {
196                 case NFS4_OPEN_CLAIM_DELEGATE_CUR:
197                         status = nfserr_inval;
198                         if (open->op_create)
199                                 goto out;
200                         /* fall through */
201                 case NFS4_OPEN_CLAIM_NULL:
202                         /*
203                          * (1) set CURRENT_FH to the file being opened,
204                          * creating it if necessary, (2) set open->op_cinfo,
205                          * (3) set open->op_truncate if the file is to be
206                          * truncated after opening, (4) do permission checking.
207                          */
208                         status = do_open_lookup(rqstp, current_fh, open);
209                         if (status)
210                                 goto out;
211                         break;
212                 case NFS4_OPEN_CLAIM_PREVIOUS:
213                         open->op_stateowner->so_confirmed = 1;
214                         /*
215                          * The CURRENT_FH is already set to the file being
216                          * opened.  (1) set open->op_cinfo, (2) set
217                          * open->op_truncate if the file is to be truncated
218                          * after opening, (3) do permission checking.
219                         */
220                         status = do_open_fhandle(rqstp, current_fh, open);
221                         if (status)
222                                 goto out;
223                         break;
224                 case NFS4_OPEN_CLAIM_DELEGATE_PREV:
225                         open->op_stateowner->so_confirmed = 1;
226                         printk("NFSD: unsupported OPEN claim type %d\n",
227                                 open->op_claim_type);
228                         status = nfserr_notsupp;
229                         goto out;
230                 default:
231                         printk("NFSD: Invalid OPEN claim type %d\n",
232                                 open->op_claim_type);
233                         status = nfserr_inval;
234                         goto out;
235         }
236         /*
237          * nfsd4_process_open2() does the actual opening of the file.  If
238          * successful, it (1) truncates the file if open->op_truncate was
239          * set, (2) sets open->op_stateid, (3) sets open->op_delegation.
240          */
241         status = nfsd4_process_open2(rqstp, current_fh, open);
242 out:
243         if (open->op_stateowner) {
244                 nfs4_get_stateowner(open->op_stateowner);
245                 *replay_owner = open->op_stateowner;
246         }
247         nfs4_unlock_state();
248         return status;
249 }
250
251 /*
252  * filehandle-manipulating ops.
253  */
254 static inline int
255 nfsd4_getfh(struct svc_fh *current_fh, struct svc_fh **getfh)
256 {
257         if (!current_fh->fh_dentry)
258                 return nfserr_nofilehandle;
259
260         *getfh = current_fh;
261         return nfs_ok;
262 }
263
264 static inline int
265 nfsd4_putfh(struct svc_rqst *rqstp, struct svc_fh *current_fh, struct nfsd4_putfh *putfh)
266 {
267         fh_put(current_fh);
268         current_fh->fh_handle.fh_size = putfh->pf_fhlen;
269         memcpy(&current_fh->fh_handle.fh_base, putfh->pf_fhval, putfh->pf_fhlen);
270         return fh_verify(rqstp, current_fh, 0, MAY_NOP);
271 }
272
273 static inline int
274 nfsd4_putrootfh(struct svc_rqst *rqstp, struct svc_fh *current_fh)
275 {
276         int status;
277
278         fh_put(current_fh);
279         status = exp_pseudoroot(rqstp->rq_client, current_fh,
280                               &rqstp->rq_chandle);
281         if (!status)
282                 status = nfserrno(nfsd_setuser(rqstp, current_fh->fh_export));
283         return status;
284 }
285
286 static inline int
287 nfsd4_restorefh(struct svc_fh *current_fh, struct svc_fh *save_fh)
288 {
289         if (!save_fh->fh_dentry)
290                 return nfserr_restorefh;
291
292         fh_dup2(current_fh, save_fh);
293         return nfs_ok;
294 }
295
296 static inline int
297 nfsd4_savefh(struct svc_fh *current_fh, struct svc_fh *save_fh)
298 {
299         if (!current_fh->fh_dentry)
300                 return nfserr_nofilehandle;
301
302         fh_dup2(save_fh, current_fh);
303         return nfs_ok;
304 }
305
306 /*
307  * misc nfsv4 ops
308  */
309 static inline int
310 nfsd4_access(struct svc_rqst *rqstp, struct svc_fh *current_fh, struct nfsd4_access *access)
311 {
312         if (access->ac_req_access & ~NFS3_ACCESS_FULL)
313                 return nfserr_inval;
314
315         access->ac_resp_access = access->ac_req_access;
316         return nfsd_access(rqstp, current_fh, &access->ac_resp_access, &access->ac_supported);
317 }
318
319 static inline int
320 nfsd4_commit(struct svc_rqst *rqstp, struct svc_fh *current_fh, struct nfsd4_commit *commit)
321 {
322         int status;
323
324         u32 *p = (u32 *)commit->co_verf.data;
325         *p++ = nfssvc_boot.tv_sec;
326         *p++ = nfssvc_boot.tv_usec;
327
328         status = nfsd_commit(rqstp, current_fh, commit->co_offset, commit->co_count);
329         if (status == nfserr_symlink)
330                 status = nfserr_inval;
331         return status;
332 }
333
334 static int
335 nfsd4_create(struct svc_rqst *rqstp, struct svc_fh *current_fh, struct nfsd4_create *create)
336 {
337         struct svc_fh resfh;
338         int status;
339         dev_t rdev;
340
341         fh_init(&resfh, NFS4_FHSIZE);
342
343         status = fh_verify(rqstp, current_fh, S_IFDIR, MAY_CREATE);
344         if (status == nfserr_symlink)
345                 status = nfserr_notdir;
346         if (status)
347                 return status;
348
349         switch (create->cr_type) {
350         case NF4LNK:
351                 /* ugh! we have to null-terminate the linktext, or
352                  * vfs_symlink() will choke.  it is always safe to
353                  * null-terminate by brute force, since at worst we
354                  * will overwrite the first byte of the create namelen
355                  * in the XDR buffer, which has already been extracted
356                  * during XDR decode.
357                  */
358                 create->cr_linkname[create->cr_linklen] = 0;
359
360                 status = nfsd_symlink(rqstp, current_fh, create->cr_name,
361                                       create->cr_namelen, create->cr_linkname,
362                                       create->cr_linklen, &resfh, &create->cr_iattr);
363                 break;
364
365         case NF4BLK:
366                 rdev = MKDEV(create->cr_specdata1, create->cr_specdata2);
367                 if (MAJOR(rdev) != create->cr_specdata1 ||
368                     MINOR(rdev) != create->cr_specdata2)
369                         return nfserr_inval;
370                 status = nfsd_create(rqstp, current_fh, create->cr_name,
371                                      create->cr_namelen, &create->cr_iattr,
372                                      S_IFBLK, rdev, &resfh);
373                 break;
374
375         case NF4CHR:
376                 rdev = MKDEV(create->cr_specdata1, create->cr_specdata2);
377                 if (MAJOR(rdev) != create->cr_specdata1 ||
378                     MINOR(rdev) != create->cr_specdata2)
379                         return nfserr_inval;
380                 status = nfsd_create(rqstp, current_fh, create->cr_name,
381                                      create->cr_namelen, &create->cr_iattr,
382                                      S_IFCHR, rdev, &resfh);
383                 break;
384
385         case NF4SOCK:
386                 status = nfsd_create(rqstp, current_fh, create->cr_name,
387                                      create->cr_namelen, &create->cr_iattr,
388                                      S_IFSOCK, 0, &resfh);
389                 break;
390
391         case NF4FIFO:
392                 status = nfsd_create(rqstp, current_fh, create->cr_name,
393                                      create->cr_namelen, &create->cr_iattr,
394                                      S_IFIFO, 0, &resfh);
395                 break;
396
397         case NF4DIR:
398                 create->cr_iattr.ia_valid &= ~ATTR_SIZE;
399                 status = nfsd_create(rqstp, current_fh, create->cr_name,
400                                      create->cr_namelen, &create->cr_iattr,
401                                      S_IFDIR, 0, &resfh);
402                 break;
403
404         default:
405                 status = nfserr_badtype;
406         }
407
408         if (!status) {
409                 fh_unlock(current_fh);
410                 set_change_info(&create->cr_cinfo, current_fh);
411                 fh_dup2(current_fh, &resfh);
412         }
413
414         fh_put(&resfh);
415         return status;
416 }
417
418 static inline int
419 nfsd4_getattr(struct svc_rqst *rqstp, struct svc_fh *current_fh, struct nfsd4_getattr *getattr)
420 {
421         int status;
422
423         status = fh_verify(rqstp, current_fh, 0, MAY_NOP);
424         if (status)
425                 return status;
426
427         if (getattr->ga_bmval[1] & NFSD_WRITEONLY_ATTRS_WORD1)
428                 return nfserr_inval;
429
430         getattr->ga_bmval[0] &= NFSD_SUPPORTED_ATTRS_WORD0;
431         getattr->ga_bmval[1] &= NFSD_SUPPORTED_ATTRS_WORD1;
432
433         getattr->ga_fhp = current_fh;
434         return nfs_ok;
435 }
436
437 static inline int
438 nfsd4_link(struct svc_rqst *rqstp, struct svc_fh *current_fh,
439            struct svc_fh *save_fh, struct nfsd4_link *link)
440 {
441         int status = nfserr_nofilehandle;
442
443         if (!save_fh->fh_dentry)
444                 return status;
445         status = nfsd_link(rqstp, current_fh, link->li_name, link->li_namelen, save_fh);
446         if (!status)
447                 set_change_info(&link->li_cinfo, current_fh);
448         return status;
449 }
450
451 static int
452 nfsd4_lookupp(struct svc_rqst *rqstp, struct svc_fh *current_fh)
453 {
454         struct svc_fh tmp_fh;
455         int ret;
456
457         fh_init(&tmp_fh, NFS4_FHSIZE);
458         if((ret = exp_pseudoroot(rqstp->rq_client, &tmp_fh,
459                               &rqstp->rq_chandle)) != 0)
460                 return ret;
461         if (tmp_fh.fh_dentry == current_fh->fh_dentry) {
462                 fh_put(&tmp_fh);
463                 return nfserr_noent;
464         }
465         fh_put(&tmp_fh);
466         return nfsd_lookup(rqstp, current_fh, "..", 2, current_fh);
467 }
468
469 static inline int
470 nfsd4_lookup(struct svc_rqst *rqstp, struct svc_fh *current_fh, struct nfsd4_lookup *lookup)
471 {
472         return nfsd_lookup(rqstp, current_fh, lookup->lo_name, lookup->lo_len, current_fh);
473 }
474
475 static inline int
476 nfsd4_read(struct svc_rqst *rqstp, struct svc_fh *current_fh, struct nfsd4_read *read)
477 {
478         int status;
479
480         /* no need to check permission - this will be done in nfsd_read() */
481
482         read->rd_filp = NULL;
483         if (read->rd_offset >= OFFSET_MAX)
484                 return nfserr_inval;
485
486         nfs4_lock_state();
487         /* check stateid */
488         if ((status = nfs4_preprocess_stateid_op(current_fh, &read->rd_stateid,
489                                 CHECK_FH | RD_STATE, &read->rd_filp))) {
490                 dprintk("NFSD: nfsd4_read: couldn't process stateid!\n");
491                 goto out;
492         }
493         if (read->rd_filp)
494                 get_file(read->rd_filp);
495         status = nfs_ok;
496 out:
497         nfs4_unlock_state();
498         read->rd_rqstp = rqstp;
499         read->rd_fhp = current_fh;
500         return status;
501 }
502
503 static inline int
504 nfsd4_readdir(struct svc_rqst *rqstp, struct svc_fh *current_fh, struct nfsd4_readdir *readdir)
505 {
506         u64 cookie = readdir->rd_cookie;
507         static const nfs4_verifier zeroverf;
508
509         /* no need to check permission - this will be done in nfsd_readdir() */
510
511         if (readdir->rd_bmval[1] & NFSD_WRITEONLY_ATTRS_WORD1)
512                 return nfserr_inval;
513
514         readdir->rd_bmval[0] &= NFSD_SUPPORTED_ATTRS_WORD0;
515         readdir->rd_bmval[1] &= NFSD_SUPPORTED_ATTRS_WORD1;
516
517         if ((cookie > ~(u32)0) || (cookie == 1) || (cookie == 2) ||
518             (cookie == 0 && memcmp(readdir->rd_verf.data, zeroverf.data, NFS4_VERIFIER_SIZE)))
519                 return nfserr_bad_cookie;
520
521         readdir->rd_rqstp = rqstp;
522         readdir->rd_fhp = current_fh;
523         return nfs_ok;
524 }
525
526 static inline int
527 nfsd4_readlink(struct svc_rqst *rqstp, struct svc_fh *current_fh, struct nfsd4_readlink *readlink)
528 {
529         readlink->rl_rqstp = rqstp;
530         readlink->rl_fhp = current_fh;
531         return nfs_ok;
532 }
533
534 static inline int
535 nfsd4_remove(struct svc_rqst *rqstp, struct svc_fh *current_fh, struct nfsd4_remove *remove)
536 {
537         int status;
538
539         if (nfs4_in_grace())
540                 return nfserr_grace;
541         status = nfsd_unlink(rqstp, current_fh, 0, remove->rm_name, remove->rm_namelen);
542         if (status == nfserr_symlink)
543                 return nfserr_notdir;
544         if (!status) {
545                 fh_unlock(current_fh);
546                 set_change_info(&remove->rm_cinfo, current_fh);
547         }
548         return status;
549 }
550
551 static inline int
552 nfsd4_rename(struct svc_rqst *rqstp, struct svc_fh *current_fh,
553              struct svc_fh *save_fh, struct nfsd4_rename *rename)
554 {
555         int status = nfserr_nofilehandle;
556
557         if (!save_fh->fh_dentry)
558                 return status;
559         if (nfs4_in_grace() && !(save_fh->fh_export->ex_flags
560                                         & NFSEXP_NOSUBTREECHECK))
561                 return nfserr_grace;
562         status = nfsd_rename(rqstp, save_fh, rename->rn_sname,
563                              rename->rn_snamelen, current_fh,
564                              rename->rn_tname, rename->rn_tnamelen);
565
566         /* the underlying filesystem returns different error's than required
567          * by NFSv4. both save_fh and current_fh have been verified.. */
568         if (status == nfserr_isdir)
569                 status = nfserr_exist;
570         else if ((status == nfserr_notdir) &&
571                   (S_ISDIR(save_fh->fh_dentry->d_inode->i_mode) &&
572                    S_ISDIR(current_fh->fh_dentry->d_inode->i_mode)))
573                 status = nfserr_exist;
574         else if (status == nfserr_symlink)
575                 status = nfserr_notdir;
576
577         if (!status) {
578                 set_change_info(&rename->rn_sinfo, current_fh);
579                 set_change_info(&rename->rn_tinfo, save_fh);
580         }
581         return status;
582 }
583
584 static inline int
585 nfsd4_setattr(struct svc_rqst *rqstp, struct svc_fh *current_fh, struct nfsd4_setattr *setattr)
586 {
587         int status = nfs_ok;
588
589         if (!current_fh->fh_dentry)
590                 return nfserr_nofilehandle;
591
592         status = nfs_ok;
593         if (setattr->sa_iattr.ia_valid & ATTR_SIZE) {
594                 nfs4_lock_state();
595                 status = nfs4_preprocess_stateid_op(current_fh,
596                         &setattr->sa_stateid, CHECK_FH | WR_STATE, NULL);
597                 nfs4_unlock_state();
598                 if (status) {
599                         dprintk("NFSD: nfsd4_setattr: couldn't process stateid!");
600                         return status;
601                 }
602         }
603         status = nfs_ok;
604         if (setattr->sa_acl != NULL)
605                 status = nfsd4_set_nfs4_acl(rqstp, current_fh, setattr->sa_acl);
606         if (status)
607                 return status;
608         status = nfsd_setattr(rqstp, current_fh, &setattr->sa_iattr,
609                                 0, (time_t)0);
610         return status;
611 }
612
613 static inline int
614 nfsd4_write(struct svc_rqst *rqstp, struct svc_fh *current_fh, struct nfsd4_write *write)
615 {
616         stateid_t *stateid = &write->wr_stateid;
617         struct file *filp = NULL;
618         u32 *p;
619         int status = nfs_ok;
620
621         /* no need to check permission - this will be done in nfsd_write() */
622
623         if (write->wr_offset >= OFFSET_MAX)
624                 return nfserr_inval;
625
626         nfs4_lock_state();
627         status = nfs4_preprocess_stateid_op(current_fh, stateid,
628                                         CHECK_FH | WR_STATE, &filp);
629         if (filp)
630                 get_file(filp);
631         nfs4_unlock_state();
632
633         if (status) {
634                 dprintk("NFSD: nfsd4_write: couldn't process stateid!\n");
635                 return status;
636         }
637
638         write->wr_bytes_written = write->wr_buflen;
639         write->wr_how_written = write->wr_stable_how;
640         p = (u32 *)write->wr_verifier.data;
641         *p++ = nfssvc_boot.tv_sec;
642         *p++ = nfssvc_boot.tv_usec;
643
644         status =  nfsd_write(rqstp, current_fh, filp, write->wr_offset,
645                         write->wr_vec, write->wr_vlen, write->wr_buflen,
646                         &write->wr_how_written);
647         if (filp)
648                 fput(filp);
649
650         if (status == nfserr_symlink)
651                 status = nfserr_inval;
652         return status;
653 }
654
655 /* This routine never returns NFS_OK!  If there are no other errors, it
656  * will return NFSERR_SAME or NFSERR_NOT_SAME depending on whether the
657  * attributes matched.  VERIFY is implemented by mapping NFSERR_SAME
658  * to NFS_OK after the call; NVERIFY by mapping NFSERR_NOT_SAME to NFS_OK.
659  */
660 static int
661 nfsd4_verify(struct svc_rqst *rqstp, struct svc_fh *current_fh, struct nfsd4_verify *verify)
662 {
663         u32 *buf, *p;
664         int count;
665         int status;
666
667         status = fh_verify(rqstp, current_fh, 0, MAY_NOP);
668         if (status)
669                 return status;
670
671         if ((verify->ve_bmval[0] & ~NFSD_SUPPORTED_ATTRS_WORD0)
672             || (verify->ve_bmval[1] & ~NFSD_SUPPORTED_ATTRS_WORD1))
673                 return nfserr_attrnotsupp;
674         if ((verify->ve_bmval[0] & FATTR4_WORD0_RDATTR_ERROR)
675             || (verify->ve_bmval[1] & NFSD_WRITEONLY_ATTRS_WORD1))
676                 return nfserr_inval;
677         if (verify->ve_attrlen & 3)
678                 return nfserr_inval;
679
680         /* count in words:
681          *   bitmap_len(1) + bitmap(2) + attr_len(1) = 4
682          */
683         count = 4 + (verify->ve_attrlen >> 2);
684         buf = kmalloc(count << 2, GFP_KERNEL);
685         if (!buf)
686                 return nfserr_resource;
687
688         status = nfsd4_encode_fattr(current_fh, current_fh->fh_export,
689                                     current_fh->fh_dentry, buf,
690                                     &count, verify->ve_bmval,
691                                     rqstp);
692
693         /* this means that nfsd4_encode_fattr() ran out of space */
694         if (status == nfserr_resource && count == 0)
695                 status = nfserr_not_same;
696         if (status)
697                 goto out_kfree;
698
699         p = buf + 3;
700         status = nfserr_not_same;
701         if (ntohl(*p++) != verify->ve_attrlen)
702                 goto out_kfree;
703         if (!memcmp(p, verify->ve_attrval, verify->ve_attrlen))
704                 status = nfserr_same;
705
706 out_kfree:
707         kfree(buf);
708         return status;
709 }
710
711 /*
712  * NULL call.
713  */
714 static int
715 nfsd4_proc_null(struct svc_rqst *rqstp, void *argp, void *resp)
716 {
717         return nfs_ok;
718 }
719
720
721 /*
722  * COMPOUND call.
723  */
724 static int
725 nfsd4_proc_compound(struct svc_rqst *rqstp,
726                     struct nfsd4_compoundargs *args,
727                     struct nfsd4_compoundres *resp)
728 {
729         struct nfsd4_op *op;
730         struct svc_fh   *current_fh = NULL;
731         struct svc_fh   *save_fh = NULL;
732         struct nfs4_stateowner *replay_owner = NULL;
733         int             slack_space;    /* in words, not bytes! */
734         int             status;
735
736         status = nfserr_resource;
737         current_fh = kmalloc(sizeof(*current_fh), GFP_KERNEL);
738         if (current_fh == NULL)
739                 goto out;
740         fh_init(current_fh, NFS4_FHSIZE);
741         save_fh = kmalloc(sizeof(*save_fh), GFP_KERNEL);
742         if (save_fh == NULL)
743                 goto out;
744         fh_init(save_fh, NFS4_FHSIZE);
745
746         resp->xbuf = &rqstp->rq_res;
747         resp->p = rqstp->rq_res.head[0].iov_base + rqstp->rq_res.head[0].iov_len;
748         resp->tagp = resp->p;
749         /* reserve space for: taglen, tag, and opcnt */
750         resp->p += 2 + XDR_QUADLEN(args->taglen);
751         resp->end = rqstp->rq_res.head[0].iov_base + PAGE_SIZE;
752         resp->taglen = args->taglen;
753         resp->tag = args->tag;
754         resp->opcnt = 0;
755         resp->rqstp = rqstp;
756
757         /*
758          * According to RFC3010, this takes precedence over all other errors.
759          */
760         status = nfserr_minor_vers_mismatch;
761         if (args->minorversion > NFSD_SUPPORTED_MINOR_VERSION)
762                 goto out;
763
764         status = nfs_ok;
765         while (!status && resp->opcnt < args->opcnt) {
766                 op = &args->ops[resp->opcnt++];
767
768                 dprintk("nfsv4 compound op #%d: %d\n", resp->opcnt, op->opnum);
769
770                 /*
771                  * The XDR decode routines may have pre-set op->status;
772                  * for example, if there is a miscellaneous XDR error
773                  * it will be set to nfserr_bad_xdr.
774                  */
775                 if (op->status)
776                         goto encode_op;
777
778                 /* We must be able to encode a successful response to
779                  * this operation, with enough room left over to encode a
780                  * failed response to the next operation.  If we don't
781                  * have enough room, fail with ERR_RESOURCE.
782                  */
783 /* FIXME - is slack_space *really* words, or bytes??? - neilb */
784                 slack_space = (char *)resp->end - (char *)resp->p;
785                 if (slack_space < COMPOUND_SLACK_SPACE + COMPOUND_ERR_SLACK_SPACE) {
786                         BUG_ON(slack_space < COMPOUND_ERR_SLACK_SPACE);
787                         op->status = nfserr_resource;
788                         goto encode_op;
789                 }
790
791                 /* All operations except RENEW, SETCLIENTID, RESTOREFH
792                 * SETCLIENTID_CONFIRM, PUTFH and PUTROOTFH
793                 * require a valid current filehandle
794                 *
795                 * SETATTR NOFILEHANDLE error handled in nfsd4_setattr
796                 * due to required returned bitmap argument
797                 */
798                 if ((!current_fh->fh_dentry) &&
799                    !((op->opnum == OP_PUTFH) || (op->opnum == OP_PUTROOTFH) ||
800                    (op->opnum == OP_SETCLIENTID) ||
801                    (op->opnum == OP_SETCLIENTID_CONFIRM) ||
802                    (op->opnum == OP_RENEW) || (op->opnum == OP_RESTOREFH) ||
803                    (op->opnum == OP_RELEASE_LOCKOWNER) ||
804                    (op->opnum == OP_SETATTR))) {
805                         op->status = nfserr_nofilehandle;
806                         goto encode_op;
807                 }
808                 switch (op->opnum) {
809                 case OP_ACCESS:
810                         op->status = nfsd4_access(rqstp, current_fh, &op->u.access);
811                         break;
812                 case OP_CLOSE:
813                         op->status = nfsd4_close(rqstp, current_fh, &op->u.close, &replay_owner);
814                         break;
815                 case OP_COMMIT:
816                         op->status = nfsd4_commit(rqstp, current_fh, &op->u.commit);
817                         break;
818                 case OP_CREATE:
819                         op->status = nfsd4_create(rqstp, current_fh, &op->u.create);
820                         break;
821                 case OP_DELEGRETURN:
822                         op->status = nfsd4_delegreturn(rqstp, current_fh, &op->u.delegreturn);
823                         break;
824                 case OP_GETATTR:
825                         op->status = nfsd4_getattr(rqstp, current_fh, &op->u.getattr);
826                         break;
827                 case OP_GETFH:
828                         op->status = nfsd4_getfh(current_fh, &op->u.getfh);
829                         break;
830                 case OP_LINK:
831                         op->status = nfsd4_link(rqstp, current_fh, save_fh, &op->u.link);
832                         break;
833                 case OP_LOCK:
834                         op->status = nfsd4_lock(rqstp, current_fh, &op->u.lock, &replay_owner);
835                         break;
836                 case OP_LOCKT:
837                         op->status = nfsd4_lockt(rqstp, current_fh, &op->u.lockt);
838                         break;
839                 case OP_LOCKU:
840                         op->status = nfsd4_locku(rqstp, current_fh, &op->u.locku, &replay_owner);
841                         break;
842                 case OP_LOOKUP:
843                         op->status = nfsd4_lookup(rqstp, current_fh, &op->u.lookup);
844                         break;
845                 case OP_LOOKUPP:
846                         op->status = nfsd4_lookupp(rqstp, current_fh);
847                         break;
848                 case OP_NVERIFY:
849                         op->status = nfsd4_verify(rqstp, current_fh, &op->u.nverify);
850                         if (op->status == nfserr_not_same)
851                                 op->status = nfs_ok;
852                         break;
853                 case OP_OPEN:
854                         op->status = nfsd4_open(rqstp, current_fh, &op->u.open, &replay_owner);
855                         break;
856                 case OP_OPEN_CONFIRM:
857                         op->status = nfsd4_open_confirm(rqstp, current_fh, &op->u.open_confirm, &replay_owner);
858                         break;
859                 case OP_OPEN_DOWNGRADE:
860                         op->status = nfsd4_open_downgrade(rqstp, current_fh, &op->u.open_downgrade, &replay_owner);
861                         break;
862                 case OP_PUTFH:
863                         op->status = nfsd4_putfh(rqstp, current_fh, &op->u.putfh);
864                         break;
865                 case OP_PUTROOTFH:
866                         op->status = nfsd4_putrootfh(rqstp, current_fh);
867                         break;
868                 case OP_READ:
869                         op->status = nfsd4_read(rqstp, current_fh, &op->u.read);
870                         break;
871                 case OP_READDIR:
872                         op->status = nfsd4_readdir(rqstp, current_fh, &op->u.readdir);
873                         break;
874                 case OP_READLINK:
875                         op->status = nfsd4_readlink(rqstp, current_fh, &op->u.readlink);
876                         break;
877                 case OP_REMOVE:
878                         op->status = nfsd4_remove(rqstp, current_fh, &op->u.remove);
879                         break;
880                 case OP_RENAME:
881                         op->status = nfsd4_rename(rqstp, current_fh, save_fh, &op->u.rename);
882                         break;
883                 case OP_RENEW:
884                         op->status = nfsd4_renew(&op->u.renew);
885                         break;
886                 case OP_RESTOREFH:
887                         op->status = nfsd4_restorefh(current_fh, save_fh);
888                         break;
889                 case OP_SAVEFH:
890                         op->status = nfsd4_savefh(current_fh, save_fh);
891                         break;
892                 case OP_SETATTR:
893                         op->status = nfsd4_setattr(rqstp, current_fh, &op->u.setattr);
894                         break;
895                 case OP_SETCLIENTID:
896                         op->status = nfsd4_setclientid(rqstp, &op->u.setclientid);
897                         break;
898                 case OP_SETCLIENTID_CONFIRM:
899                         op->status = nfsd4_setclientid_confirm(rqstp, &op->u.setclientid_confirm);
900                         break;
901                 case OP_VERIFY:
902                         op->status = nfsd4_verify(rqstp, current_fh, &op->u.verify);
903                         if (op->status == nfserr_same)
904                                 op->status = nfs_ok;
905                         break;
906                 case OP_WRITE:
907                         op->status = nfsd4_write(rqstp, current_fh, &op->u.write);
908                         break;
909                 case OP_RELEASE_LOCKOWNER:
910                         op->status = nfsd4_release_lockowner(rqstp, &op->u.release_lockowner);
911                         break;
912                 default:
913                         BUG_ON(op->status == nfs_ok);
914                         break;
915                 }
916
917 encode_op:
918                 if (op->status == NFSERR_REPLAY_ME) {
919                         op->replay = &replay_owner->so_replay;
920                         nfsd4_encode_replay(resp, op);
921                         status = op->status = op->replay->rp_status;
922                 } else {
923                         nfsd4_encode_operation(resp, op);
924                         status = op->status;
925                 }
926                 if (replay_owner && (replay_owner != (void *)(-1))) {
927                         nfs4_put_stateowner(replay_owner);
928                         replay_owner = NULL;
929                 }
930                 /* XXX Ugh, we need to get rid of this kind of special case: */
931                 if (op->opnum == OP_READ && op->u.read.rd_filp)
932                         fput(op->u.read.rd_filp);
933         }
934
935 out:
936         nfsd4_release_compoundargs(args);
937         if (current_fh)
938                 fh_put(current_fh);
939         kfree(current_fh);
940         if (save_fh)
941                 fh_put(save_fh);
942         kfree(save_fh);
943         return status;
944 }
945
946 #define nfs4svc_decode_voidargs         NULL
947 #define nfs4svc_release_void            NULL
948 #define nfsd4_voidres                   nfsd4_voidargs
949 #define nfs4svc_release_compound        NULL
950 struct nfsd4_voidargs { int dummy; };
951
952 #define PROC(name, argt, rest, relt, cache, respsize)   \
953  { (svc_procfunc) nfsd4_proc_##name,            \
954    (kxdrproc_t) nfs4svc_decode_##argt##args,    \
955    (kxdrproc_t) nfs4svc_encode_##rest##res,     \
956    (kxdrproc_t) nfs4svc_release_##relt,         \
957    sizeof(struct nfsd4_##argt##args),           \
958    sizeof(struct nfsd4_##rest##res),            \
959    0,                                           \
960    cache,                                       \
961    respsize,                                    \
962  }
963
964 /*
965  * TODO: At the present time, the NFSv4 server does not do XID caching
966  * of requests.  Implementing XID caching would not be a serious problem,
967  * although it would require a mild change in interfaces since one
968  * doesn't know whether an NFSv4 request is idempotent until after the
969  * XDR decode.  However, XID caching totally confuses pynfs (Peter
970  * Astrand's regression testsuite for NFSv4 servers), which reuses
971  * XID's liberally, so I've left it unimplemented until pynfs generates
972  * better XID's.
973  */
974 static struct svc_procedure             nfsd_procedures4[2] = {
975   PROC(null,     void,          void,           void,     RC_NOCACHE, 1),
976   PROC(compound, compound,      compound,       compound, RC_NOCACHE, NFSD_BUFSIZE)
977 };
978
979 struct svc_version      nfsd_version4 = {
980                 .vs_vers        = 4,
981                 .vs_nproc       = 2,
982                 .vs_proc        = nfsd_procedures4,
983                 .vs_dispatch    = nfsd_dispatch,
984                 .vs_xdrsize     = NFS4_SVC_XDRSIZE,
985 };
986
987 /*
988  * Local variables:
989  *  c-basic-offset: 8
990  * End:
991  */