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