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