]> git.proxmox.com Git - mirror_ubuntu-bionic-kernel.git/blob - fs/nfsd/nfs4state.c
nfsd: Do not let nfs4_file pin the struct inode
[mirror_ubuntu-bionic-kernel.git] / fs / nfsd / nfs4state.c
1 /*
2 * Copyright (c) 2001 The Regents of the University of Michigan.
3 * All rights reserved.
4 *
5 * Kendrick Smith <kmsmith@umich.edu>
6 * Andy Adamson <kandros@umich.edu>
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
11 *
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the distribution.
17 * 3. Neither the name of the University nor the names of its
18 * contributors may be used to endorse or promote products derived
19 * from this software without specific prior written permission.
20 *
21 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED
22 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
23 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
24 * DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
26 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
27 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
28 * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
29 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
30 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
31 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32 *
33 */
34
35 #include <linux/file.h>
36 #include <linux/fs.h>
37 #include <linux/slab.h>
38 #include <linux/namei.h>
39 #include <linux/swap.h>
40 #include <linux/pagemap.h>
41 #include <linux/ratelimit.h>
42 #include <linux/sunrpc/svcauth_gss.h>
43 #include <linux/sunrpc/addr.h>
44 #include <linux/hash.h>
45 #include "xdr4.h"
46 #include "xdr4cb.h"
47 #include "vfs.h"
48 #include "current_stateid.h"
49
50 #include "netns.h"
51
52 #define NFSDDBG_FACILITY NFSDDBG_PROC
53
54 #define all_ones {{~0,~0},~0}
55 static const stateid_t one_stateid = {
56 .si_generation = ~0,
57 .si_opaque = all_ones,
58 };
59 static const stateid_t zero_stateid = {
60 /* all fields zero */
61 };
62 static const stateid_t currentstateid = {
63 .si_generation = 1,
64 };
65
66 static u64 current_sessionid = 1;
67
68 #define ZERO_STATEID(stateid) (!memcmp((stateid), &zero_stateid, sizeof(stateid_t)))
69 #define ONE_STATEID(stateid) (!memcmp((stateid), &one_stateid, sizeof(stateid_t)))
70 #define CURRENT_STATEID(stateid) (!memcmp((stateid), &currentstateid, sizeof(stateid_t)))
71
72 /* forward declarations */
73 static bool check_for_locks(struct nfs4_file *fp, struct nfs4_lockowner *lowner);
74
75 /* Locking: */
76
77 /* Currently used for almost all code touching nfsv4 state: */
78 static DEFINE_MUTEX(client_mutex);
79
80 /*
81 * Currently used for the del_recall_lru and file hash table. In an
82 * effort to decrease the scope of the client_mutex, this spinlock may
83 * eventually cover more:
84 */
85 static DEFINE_SPINLOCK(state_lock);
86
87 static struct kmem_cache *openowner_slab;
88 static struct kmem_cache *lockowner_slab;
89 static struct kmem_cache *file_slab;
90 static struct kmem_cache *stateid_slab;
91 static struct kmem_cache *deleg_slab;
92
93 void
94 nfs4_lock_state(void)
95 {
96 mutex_lock(&client_mutex);
97 }
98
99 static void free_session(struct nfsd4_session *);
100
101 static bool is_session_dead(struct nfsd4_session *ses)
102 {
103 return ses->se_flags & NFS4_SESSION_DEAD;
104 }
105
106 static __be32 mark_session_dead_locked(struct nfsd4_session *ses, int ref_held_by_me)
107 {
108 if (atomic_read(&ses->se_ref) > ref_held_by_me)
109 return nfserr_jukebox;
110 ses->se_flags |= NFS4_SESSION_DEAD;
111 return nfs_ok;
112 }
113
114 void
115 nfs4_unlock_state(void)
116 {
117 mutex_unlock(&client_mutex);
118 }
119
120 static bool is_client_expired(struct nfs4_client *clp)
121 {
122 return clp->cl_time == 0;
123 }
124
125 static __be32 mark_client_expired_locked(struct nfs4_client *clp)
126 {
127 if (atomic_read(&clp->cl_refcount))
128 return nfserr_jukebox;
129 clp->cl_time = 0;
130 return nfs_ok;
131 }
132
133 static __be32 mark_client_expired(struct nfs4_client *clp)
134 {
135 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
136 __be32 ret;
137
138 spin_lock(&nn->client_lock);
139 ret = mark_client_expired_locked(clp);
140 spin_unlock(&nn->client_lock);
141 return ret;
142 }
143
144 static __be32 get_client_locked(struct nfs4_client *clp)
145 {
146 if (is_client_expired(clp))
147 return nfserr_expired;
148 atomic_inc(&clp->cl_refcount);
149 return nfs_ok;
150 }
151
152 /* must be called under the client_lock */
153 static inline void
154 renew_client_locked(struct nfs4_client *clp)
155 {
156 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
157
158 if (is_client_expired(clp)) {
159 WARN_ON(1);
160 printk("%s: client (clientid %08x/%08x) already expired\n",
161 __func__,
162 clp->cl_clientid.cl_boot,
163 clp->cl_clientid.cl_id);
164 return;
165 }
166
167 dprintk("renewing client (clientid %08x/%08x)\n",
168 clp->cl_clientid.cl_boot,
169 clp->cl_clientid.cl_id);
170 list_move_tail(&clp->cl_lru, &nn->client_lru);
171 clp->cl_time = get_seconds();
172 }
173
174 static inline void
175 renew_client(struct nfs4_client *clp)
176 {
177 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
178
179 spin_lock(&nn->client_lock);
180 renew_client_locked(clp);
181 spin_unlock(&nn->client_lock);
182 }
183
184 static void put_client_renew_locked(struct nfs4_client *clp)
185 {
186 if (!atomic_dec_and_test(&clp->cl_refcount))
187 return;
188 if (!is_client_expired(clp))
189 renew_client_locked(clp);
190 }
191
192 static void put_client_renew(struct nfs4_client *clp)
193 {
194 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
195
196 if (!atomic_dec_and_lock(&clp->cl_refcount, &nn->client_lock))
197 return;
198 if (!is_client_expired(clp))
199 renew_client_locked(clp);
200 spin_unlock(&nn->client_lock);
201 }
202
203 static __be32 nfsd4_get_session_locked(struct nfsd4_session *ses)
204 {
205 __be32 status;
206
207 if (is_session_dead(ses))
208 return nfserr_badsession;
209 status = get_client_locked(ses->se_client);
210 if (status)
211 return status;
212 atomic_inc(&ses->se_ref);
213 return nfs_ok;
214 }
215
216 static void nfsd4_put_session_locked(struct nfsd4_session *ses)
217 {
218 struct nfs4_client *clp = ses->se_client;
219
220 if (atomic_dec_and_test(&ses->se_ref) && is_session_dead(ses))
221 free_session(ses);
222 put_client_renew_locked(clp);
223 }
224
225 static void nfsd4_put_session(struct nfsd4_session *ses)
226 {
227 struct nfs4_client *clp = ses->se_client;
228 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
229
230 spin_lock(&nn->client_lock);
231 nfsd4_put_session_locked(ses);
232 spin_unlock(&nn->client_lock);
233 }
234
235
236 static inline u32
237 opaque_hashval(const void *ptr, int nbytes)
238 {
239 unsigned char *cptr = (unsigned char *) ptr;
240
241 u32 x = 0;
242 while (nbytes--) {
243 x *= 37;
244 x += *cptr++;
245 }
246 return x;
247 }
248
249 static void nfsd4_free_file(struct nfs4_file *f)
250 {
251 kmem_cache_free(file_slab, f);
252 }
253
254 static inline void
255 put_nfs4_file(struct nfs4_file *fi)
256 {
257 might_lock(&state_lock);
258
259 if (atomic_dec_and_lock(&fi->fi_ref, &state_lock)) {
260 hlist_del(&fi->fi_hash);
261 spin_unlock(&state_lock);
262 nfsd4_free_file(fi);
263 }
264 }
265
266 static inline void
267 get_nfs4_file(struct nfs4_file *fi)
268 {
269 atomic_inc(&fi->fi_ref);
270 }
271
272 static struct file *
273 __nfs4_get_fd(struct nfs4_file *f, int oflag)
274 {
275 if (f->fi_fds[oflag])
276 return get_file(f->fi_fds[oflag]);
277 return NULL;
278 }
279
280 static struct file *
281 find_writeable_file_locked(struct nfs4_file *f)
282 {
283 struct file *ret;
284
285 lockdep_assert_held(&f->fi_lock);
286
287 ret = __nfs4_get_fd(f, O_WRONLY);
288 if (!ret)
289 ret = __nfs4_get_fd(f, O_RDWR);
290 return ret;
291 }
292
293 static struct file *
294 find_writeable_file(struct nfs4_file *f)
295 {
296 struct file *ret;
297
298 spin_lock(&f->fi_lock);
299 ret = find_writeable_file_locked(f);
300 spin_unlock(&f->fi_lock);
301
302 return ret;
303 }
304
305 static struct file *find_readable_file_locked(struct nfs4_file *f)
306 {
307 struct file *ret;
308
309 lockdep_assert_held(&f->fi_lock);
310
311 ret = __nfs4_get_fd(f, O_RDONLY);
312 if (!ret)
313 ret = __nfs4_get_fd(f, O_RDWR);
314 return ret;
315 }
316
317 static struct file *
318 find_readable_file(struct nfs4_file *f)
319 {
320 struct file *ret;
321
322 spin_lock(&f->fi_lock);
323 ret = find_readable_file_locked(f);
324 spin_unlock(&f->fi_lock);
325
326 return ret;
327 }
328
329 static struct file *
330 find_any_file(struct nfs4_file *f)
331 {
332 struct file *ret;
333
334 spin_lock(&f->fi_lock);
335 ret = __nfs4_get_fd(f, O_RDWR);
336 if (!ret) {
337 ret = __nfs4_get_fd(f, O_WRONLY);
338 if (!ret)
339 ret = __nfs4_get_fd(f, O_RDONLY);
340 }
341 spin_unlock(&f->fi_lock);
342 return ret;
343 }
344
345 static int num_delegations;
346 unsigned long max_delegations;
347
348 /*
349 * Open owner state (share locks)
350 */
351
352 /* hash tables for lock and open owners */
353 #define OWNER_HASH_BITS 8
354 #define OWNER_HASH_SIZE (1 << OWNER_HASH_BITS)
355 #define OWNER_HASH_MASK (OWNER_HASH_SIZE - 1)
356
357 static unsigned int ownerstr_hashval(u32 clientid, struct xdr_netobj *ownername)
358 {
359 unsigned int ret;
360
361 ret = opaque_hashval(ownername->data, ownername->len);
362 ret += clientid;
363 return ret & OWNER_HASH_MASK;
364 }
365
366 /* hash table for nfs4_file */
367 #define FILE_HASH_BITS 8
368 #define FILE_HASH_SIZE (1 << FILE_HASH_BITS)
369
370 static unsigned int nfsd_fh_hashval(struct knfsd_fh *fh)
371 {
372 return jhash2(fh->fh_base.fh_pad, XDR_QUADLEN(fh->fh_size), 0);
373 }
374
375 static unsigned int file_hashval(struct knfsd_fh *fh)
376 {
377 return nfsd_fh_hashval(fh) & (FILE_HASH_SIZE - 1);
378 }
379
380 static bool nfsd_fh_match(struct knfsd_fh *fh1, struct knfsd_fh *fh2)
381 {
382 return fh1->fh_size == fh2->fh_size &&
383 !memcmp(fh1->fh_base.fh_pad,
384 fh2->fh_base.fh_pad,
385 fh1->fh_size);
386 }
387
388 static struct hlist_head file_hashtbl[FILE_HASH_SIZE];
389
390 static void
391 __nfs4_file_get_access(struct nfs4_file *fp, u32 access)
392 {
393 lockdep_assert_held(&fp->fi_lock);
394
395 if (access & NFS4_SHARE_ACCESS_WRITE)
396 atomic_inc(&fp->fi_access[O_WRONLY]);
397 if (access & NFS4_SHARE_ACCESS_READ)
398 atomic_inc(&fp->fi_access[O_RDONLY]);
399 }
400
401 static __be32
402 nfs4_file_get_access(struct nfs4_file *fp, u32 access)
403 {
404 lockdep_assert_held(&fp->fi_lock);
405
406 /* Does this access mode make sense? */
407 if (access & ~NFS4_SHARE_ACCESS_BOTH)
408 return nfserr_inval;
409
410 /* Does it conflict with a deny mode already set? */
411 if ((access & fp->fi_share_deny) != 0)
412 return nfserr_share_denied;
413
414 __nfs4_file_get_access(fp, access);
415 return nfs_ok;
416 }
417
418 static __be32 nfs4_file_check_deny(struct nfs4_file *fp, u32 deny)
419 {
420 /* Common case is that there is no deny mode. */
421 if (deny) {
422 /* Does this deny mode make sense? */
423 if (deny & ~NFS4_SHARE_DENY_BOTH)
424 return nfserr_inval;
425
426 if ((deny & NFS4_SHARE_DENY_READ) &&
427 atomic_read(&fp->fi_access[O_RDONLY]))
428 return nfserr_share_denied;
429
430 if ((deny & NFS4_SHARE_DENY_WRITE) &&
431 atomic_read(&fp->fi_access[O_WRONLY]))
432 return nfserr_share_denied;
433 }
434 return nfs_ok;
435 }
436
437 static void __nfs4_file_put_access(struct nfs4_file *fp, int oflag)
438 {
439 might_lock(&fp->fi_lock);
440
441 if (atomic_dec_and_lock(&fp->fi_access[oflag], &fp->fi_lock)) {
442 struct file *f1 = NULL;
443 struct file *f2 = NULL;
444
445 swap(f1, fp->fi_fds[oflag]);
446 if (atomic_read(&fp->fi_access[1 - oflag]) == 0)
447 swap(f2, fp->fi_fds[O_RDWR]);
448 spin_unlock(&fp->fi_lock);
449 if (f1)
450 fput(f1);
451 if (f2)
452 fput(f2);
453 }
454 }
455
456 static void nfs4_file_put_access(struct nfs4_file *fp, u32 access)
457 {
458 WARN_ON_ONCE(access & ~NFS4_SHARE_ACCESS_BOTH);
459
460 if (access & NFS4_SHARE_ACCESS_WRITE)
461 __nfs4_file_put_access(fp, O_WRONLY);
462 if (access & NFS4_SHARE_ACCESS_READ)
463 __nfs4_file_put_access(fp, O_RDONLY);
464 }
465
466 static struct nfs4_stid *nfs4_alloc_stid(struct nfs4_client *cl, struct
467 kmem_cache *slab)
468 {
469 struct idr *stateids = &cl->cl_stateids;
470 struct nfs4_stid *stid;
471 int new_id;
472
473 stid = kmem_cache_alloc(slab, GFP_KERNEL);
474 if (!stid)
475 return NULL;
476
477 new_id = idr_alloc_cyclic(stateids, stid, 0, 0, GFP_KERNEL);
478 if (new_id < 0)
479 goto out_free;
480 stid->sc_client = cl;
481 stid->sc_type = 0;
482 stid->sc_stateid.si_opaque.so_id = new_id;
483 stid->sc_stateid.si_opaque.so_clid = cl->cl_clientid;
484 /* Will be incremented before return to client: */
485 stid->sc_stateid.si_generation = 0;
486 atomic_set(&stid->sc_count, 1);
487
488 /*
489 * It shouldn't be a problem to reuse an opaque stateid value.
490 * I don't think it is for 4.1. But with 4.0 I worry that, for
491 * example, a stray write retransmission could be accepted by
492 * the server when it should have been rejected. Therefore,
493 * adopt a trick from the sctp code to attempt to maximize the
494 * amount of time until an id is reused, by ensuring they always
495 * "increase" (mod INT_MAX):
496 */
497 return stid;
498 out_free:
499 kmem_cache_free(slab, stid);
500 return NULL;
501 }
502
503 static struct nfs4_ol_stateid * nfs4_alloc_stateid(struct nfs4_client *clp)
504 {
505 return openlockstateid(nfs4_alloc_stid(clp, stateid_slab));
506 }
507
508 /*
509 * When we recall a delegation, we should be careful not to hand it
510 * out again straight away.
511 * To ensure this we keep a pair of bloom filters ('new' and 'old')
512 * in which the filehandles of recalled delegations are "stored".
513 * If a filehandle appear in either filter, a delegation is blocked.
514 * When a delegation is recalled, the filehandle is stored in the "new"
515 * filter.
516 * Every 30 seconds we swap the filters and clear the "new" one,
517 * unless both are empty of course.
518 *
519 * Each filter is 256 bits. We hash the filehandle to 32bit and use the
520 * low 3 bytes as hash-table indices.
521 *
522 * 'state_lock', which is always held when block_delegations() is called,
523 * is used to manage concurrent access. Testing does not need the lock
524 * except when swapping the two filters.
525 */
526 static struct bloom_pair {
527 int entries, old_entries;
528 time_t swap_time;
529 int new; /* index into 'set' */
530 DECLARE_BITMAP(set[2], 256);
531 } blocked_delegations;
532
533 static int delegation_blocked(struct knfsd_fh *fh)
534 {
535 u32 hash;
536 struct bloom_pair *bd = &blocked_delegations;
537
538 if (bd->entries == 0)
539 return 0;
540 if (seconds_since_boot() - bd->swap_time > 30) {
541 spin_lock(&state_lock);
542 if (seconds_since_boot() - bd->swap_time > 30) {
543 bd->entries -= bd->old_entries;
544 bd->old_entries = bd->entries;
545 memset(bd->set[bd->new], 0,
546 sizeof(bd->set[0]));
547 bd->new = 1-bd->new;
548 bd->swap_time = seconds_since_boot();
549 }
550 spin_unlock(&state_lock);
551 }
552 hash = arch_fast_hash(&fh->fh_base, fh->fh_size, 0);
553 if (test_bit(hash&255, bd->set[0]) &&
554 test_bit((hash>>8)&255, bd->set[0]) &&
555 test_bit((hash>>16)&255, bd->set[0]))
556 return 1;
557
558 if (test_bit(hash&255, bd->set[1]) &&
559 test_bit((hash>>8)&255, bd->set[1]) &&
560 test_bit((hash>>16)&255, bd->set[1]))
561 return 1;
562
563 return 0;
564 }
565
566 static void block_delegations(struct knfsd_fh *fh)
567 {
568 u32 hash;
569 struct bloom_pair *bd = &blocked_delegations;
570
571 lockdep_assert_held(&state_lock);
572
573 hash = arch_fast_hash(&fh->fh_base, fh->fh_size, 0);
574
575 __set_bit(hash&255, bd->set[bd->new]);
576 __set_bit((hash>>8)&255, bd->set[bd->new]);
577 __set_bit((hash>>16)&255, bd->set[bd->new]);
578 if (bd->entries == 0)
579 bd->swap_time = seconds_since_boot();
580 bd->entries += 1;
581 }
582
583 static struct nfs4_delegation *
584 alloc_init_deleg(struct nfs4_client *clp, struct nfs4_ol_stateid *stp, struct svc_fh *current_fh)
585 {
586 struct nfs4_delegation *dp;
587
588 dprintk("NFSD alloc_init_deleg\n");
589 if (num_delegations > max_delegations)
590 return NULL;
591 if (delegation_blocked(&current_fh->fh_handle))
592 return NULL;
593 dp = delegstateid(nfs4_alloc_stid(clp, deleg_slab));
594 if (dp == NULL)
595 return dp;
596 /*
597 * delegation seqid's are never incremented. The 4.1 special
598 * meaning of seqid 0 isn't meaningful, really, but let's avoid
599 * 0 anyway just for consistency and use 1:
600 */
601 dp->dl_stid.sc_stateid.si_generation = 1;
602 num_delegations++;
603 INIT_LIST_HEAD(&dp->dl_perfile);
604 INIT_LIST_HEAD(&dp->dl_perclnt);
605 INIT_LIST_HEAD(&dp->dl_recall_lru);
606 dp->dl_file = NULL;
607 dp->dl_type = NFS4_OPEN_DELEGATE_READ;
608 fh_copy_shallow(&dp->dl_fh, &current_fh->fh_handle);
609 dp->dl_time = 0;
610 INIT_WORK(&dp->dl_recall.cb_work, nfsd4_run_cb_recall);
611 return dp;
612 }
613
614 static void remove_stid(struct nfs4_stid *s)
615 {
616 struct idr *stateids = &s->sc_client->cl_stateids;
617
618 idr_remove(stateids, s->sc_stateid.si_opaque.so_id);
619 }
620
621 static void nfs4_free_stid(struct kmem_cache *slab, struct nfs4_stid *s)
622 {
623 kmem_cache_free(slab, s);
624 }
625
626 void
627 nfs4_put_delegation(struct nfs4_delegation *dp)
628 {
629 if (atomic_dec_and_test(&dp->dl_stid.sc_count)) {
630 remove_stid(&dp->dl_stid);
631 nfs4_free_stid(deleg_slab, &dp->dl_stid);
632 num_delegations--;
633 }
634 }
635
636 static void nfs4_put_deleg_lease(struct nfs4_file *fp)
637 {
638 lockdep_assert_held(&state_lock);
639
640 if (!fp->fi_lease)
641 return;
642 if (atomic_dec_and_test(&fp->fi_delegees)) {
643 vfs_setlease(fp->fi_deleg_file, F_UNLCK, &fp->fi_lease);
644 fp->fi_lease = NULL;
645 fput(fp->fi_deleg_file);
646 fp->fi_deleg_file = NULL;
647 }
648 }
649
650 static void unhash_stid(struct nfs4_stid *s)
651 {
652 s->sc_type = 0;
653 }
654
655 static void
656 hash_delegation_locked(struct nfs4_delegation *dp, struct nfs4_file *fp)
657 {
658 lockdep_assert_held(&state_lock);
659 lockdep_assert_held(&fp->fi_lock);
660
661 dp->dl_stid.sc_type = NFS4_DELEG_STID;
662 list_add(&dp->dl_perfile, &fp->fi_delegations);
663 list_add(&dp->dl_perclnt, &dp->dl_stid.sc_client->cl_delegations);
664 }
665
666 /* Called under the state lock. */
667 static void
668 unhash_delegation(struct nfs4_delegation *dp)
669 {
670 struct nfs4_file *fp = dp->dl_file;
671
672 spin_lock(&state_lock);
673 dp->dl_stid.sc_type = NFS4_CLOSED_DELEG_STID;
674 /* Ensure that deleg break won't try to requeue it */
675 ++dp->dl_time;
676 spin_lock(&fp->fi_lock);
677 list_del_init(&dp->dl_perclnt);
678 list_del_init(&dp->dl_recall_lru);
679 list_del_init(&dp->dl_perfile);
680 spin_unlock(&fp->fi_lock);
681 if (fp) {
682 nfs4_put_deleg_lease(fp);
683 dp->dl_file = NULL;
684 }
685 spin_unlock(&state_lock);
686 if (fp)
687 put_nfs4_file(fp);
688 }
689
690 static void destroy_revoked_delegation(struct nfs4_delegation *dp)
691 {
692 list_del_init(&dp->dl_recall_lru);
693 nfs4_put_delegation(dp);
694 }
695
696 static void destroy_delegation(struct nfs4_delegation *dp)
697 {
698 unhash_delegation(dp);
699 nfs4_put_delegation(dp);
700 }
701
702 static void revoke_delegation(struct nfs4_delegation *dp)
703 {
704 struct nfs4_client *clp = dp->dl_stid.sc_client;
705
706 if (clp->cl_minorversion == 0)
707 destroy_delegation(dp);
708 else {
709 unhash_delegation(dp);
710 dp->dl_stid.sc_type = NFS4_REVOKED_DELEG_STID;
711 list_add(&dp->dl_recall_lru, &clp->cl_revoked);
712 }
713 }
714
715 /*
716 * SETCLIENTID state
717 */
718
719 static unsigned int clientid_hashval(u32 id)
720 {
721 return id & CLIENT_HASH_MASK;
722 }
723
724 static unsigned int clientstr_hashval(const char *name)
725 {
726 return opaque_hashval(name, 8) & CLIENT_HASH_MASK;
727 }
728
729 /*
730 * We store the NONE, READ, WRITE, and BOTH bits separately in the
731 * st_{access,deny}_bmap field of the stateid, in order to track not
732 * only what share bits are currently in force, but also what
733 * combinations of share bits previous opens have used. This allows us
734 * to enforce the recommendation of rfc 3530 14.2.19 that the server
735 * return an error if the client attempt to downgrade to a combination
736 * of share bits not explicable by closing some of its previous opens.
737 *
738 * XXX: This enforcement is actually incomplete, since we don't keep
739 * track of access/deny bit combinations; so, e.g., we allow:
740 *
741 * OPEN allow read, deny write
742 * OPEN allow both, deny none
743 * DOWNGRADE allow read, deny none
744 *
745 * which we should reject.
746 */
747 static unsigned int
748 bmap_to_share_mode(unsigned long bmap) {
749 int i;
750 unsigned int access = 0;
751
752 for (i = 1; i < 4; i++) {
753 if (test_bit(i, &bmap))
754 access |= i;
755 }
756 return access;
757 }
758
759 /* set share access for a given stateid */
760 static inline void
761 set_access(u32 access, struct nfs4_ol_stateid *stp)
762 {
763 unsigned char mask = 1 << access;
764
765 WARN_ON_ONCE(access > NFS4_SHARE_ACCESS_BOTH);
766 stp->st_access_bmap |= mask;
767 }
768
769 /* clear share access for a given stateid */
770 static inline void
771 clear_access(u32 access, struct nfs4_ol_stateid *stp)
772 {
773 unsigned char mask = 1 << access;
774
775 WARN_ON_ONCE(access > NFS4_SHARE_ACCESS_BOTH);
776 stp->st_access_bmap &= ~mask;
777 }
778
779 /* test whether a given stateid has access */
780 static inline bool
781 test_access(u32 access, struct nfs4_ol_stateid *stp)
782 {
783 unsigned char mask = 1 << access;
784
785 return (bool)(stp->st_access_bmap & mask);
786 }
787
788 /* set share deny for a given stateid */
789 static inline void
790 set_deny(u32 deny, struct nfs4_ol_stateid *stp)
791 {
792 unsigned char mask = 1 << deny;
793
794 WARN_ON_ONCE(deny > NFS4_SHARE_DENY_BOTH);
795 stp->st_deny_bmap |= mask;
796 }
797
798 /* clear share deny for a given stateid */
799 static inline void
800 clear_deny(u32 deny, struct nfs4_ol_stateid *stp)
801 {
802 unsigned char mask = 1 << deny;
803
804 WARN_ON_ONCE(deny > NFS4_SHARE_DENY_BOTH);
805 stp->st_deny_bmap &= ~mask;
806 }
807
808 /* test whether a given stateid is denying specific access */
809 static inline bool
810 test_deny(u32 deny, struct nfs4_ol_stateid *stp)
811 {
812 unsigned char mask = 1 << deny;
813
814 return (bool)(stp->st_deny_bmap & mask);
815 }
816
817 static int nfs4_access_to_omode(u32 access)
818 {
819 switch (access & NFS4_SHARE_ACCESS_BOTH) {
820 case NFS4_SHARE_ACCESS_READ:
821 return O_RDONLY;
822 case NFS4_SHARE_ACCESS_WRITE:
823 return O_WRONLY;
824 case NFS4_SHARE_ACCESS_BOTH:
825 return O_RDWR;
826 }
827 WARN_ON_ONCE(1);
828 return O_RDONLY;
829 }
830
831 /*
832 * A stateid that had a deny mode associated with it is being released
833 * or downgraded. Recalculate the deny mode on the file.
834 */
835 static void
836 recalculate_deny_mode(struct nfs4_file *fp)
837 {
838 struct nfs4_ol_stateid *stp;
839
840 spin_lock(&fp->fi_lock);
841 fp->fi_share_deny = 0;
842 list_for_each_entry(stp, &fp->fi_stateids, st_perfile)
843 fp->fi_share_deny |= bmap_to_share_mode(stp->st_deny_bmap);
844 spin_unlock(&fp->fi_lock);
845 }
846
847 static void
848 reset_union_bmap_deny(u32 deny, struct nfs4_ol_stateid *stp)
849 {
850 int i;
851 bool change = false;
852
853 for (i = 1; i < 4; i++) {
854 if ((i & deny) != i) {
855 change = true;
856 clear_deny(i, stp);
857 }
858 }
859
860 /* Recalculate per-file deny mode if there was a change */
861 if (change)
862 recalculate_deny_mode(stp->st_file);
863 }
864
865 /* release all access and file references for a given stateid */
866 static void
867 release_all_access(struct nfs4_ol_stateid *stp)
868 {
869 int i;
870 struct nfs4_file *fp = stp->st_file;
871
872 if (fp && stp->st_deny_bmap != 0)
873 recalculate_deny_mode(fp);
874
875 for (i = 1; i < 4; i++) {
876 if (test_access(i, stp))
877 nfs4_file_put_access(stp->st_file, i);
878 clear_access(i, stp);
879 }
880 }
881
882 static void unhash_generic_stateid(struct nfs4_ol_stateid *stp)
883 {
884 struct nfs4_file *fp = stp->st_file;
885
886 spin_lock(&fp->fi_lock);
887 list_del(&stp->st_perfile);
888 spin_unlock(&fp->fi_lock);
889 list_del(&stp->st_perstateowner);
890 }
891
892 static void close_generic_stateid(struct nfs4_ol_stateid *stp)
893 {
894 release_all_access(stp);
895 put_nfs4_file(stp->st_file);
896 stp->st_file = NULL;
897 }
898
899 static void free_generic_stateid(struct nfs4_ol_stateid *stp)
900 {
901 remove_stid(&stp->st_stid);
902 nfs4_free_stid(stateid_slab, &stp->st_stid);
903 }
904
905 static void __release_lock_stateid(struct nfs4_ol_stateid *stp)
906 {
907 struct file *file;
908
909 list_del(&stp->st_locks);
910 unhash_generic_stateid(stp);
911 unhash_stid(&stp->st_stid);
912 file = find_any_file(stp->st_file);
913 if (file)
914 filp_close(file, (fl_owner_t)lockowner(stp->st_stateowner));
915 close_generic_stateid(stp);
916 free_generic_stateid(stp);
917 }
918
919 static void unhash_lockowner(struct nfs4_lockowner *lo)
920 {
921 struct nfs4_ol_stateid *stp;
922
923 list_del(&lo->lo_owner.so_strhash);
924 while (!list_empty(&lo->lo_owner.so_stateids)) {
925 stp = list_first_entry(&lo->lo_owner.so_stateids,
926 struct nfs4_ol_stateid, st_perstateowner);
927 __release_lock_stateid(stp);
928 }
929 }
930
931 static void nfs4_free_lockowner(struct nfs4_lockowner *lo)
932 {
933 kfree(lo->lo_owner.so_owner.data);
934 kmem_cache_free(lockowner_slab, lo);
935 }
936
937 static void release_lockowner(struct nfs4_lockowner *lo)
938 {
939 unhash_lockowner(lo);
940 nfs4_free_lockowner(lo);
941 }
942
943 static void release_lockowner_if_empty(struct nfs4_lockowner *lo)
944 {
945 if (list_empty(&lo->lo_owner.so_stateids))
946 release_lockowner(lo);
947 }
948
949 static void release_lock_stateid(struct nfs4_ol_stateid *stp)
950 {
951 struct nfs4_lockowner *lo;
952
953 lo = lockowner(stp->st_stateowner);
954 __release_lock_stateid(stp);
955 release_lockowner_if_empty(lo);
956 }
957
958 static void release_open_stateid_locks(struct nfs4_ol_stateid *open_stp)
959 {
960 struct nfs4_ol_stateid *stp;
961
962 while (!list_empty(&open_stp->st_locks)) {
963 stp = list_entry(open_stp->st_locks.next,
964 struct nfs4_ol_stateid, st_locks);
965 release_lock_stateid(stp);
966 }
967 }
968
969 static void unhash_open_stateid(struct nfs4_ol_stateid *stp)
970 {
971 unhash_generic_stateid(stp);
972 release_open_stateid_locks(stp);
973 close_generic_stateid(stp);
974 }
975
976 static void release_open_stateid(struct nfs4_ol_stateid *stp)
977 {
978 unhash_open_stateid(stp);
979 free_generic_stateid(stp);
980 }
981
982 static void unhash_openowner(struct nfs4_openowner *oo)
983 {
984 struct nfs4_ol_stateid *stp;
985
986 list_del(&oo->oo_owner.so_strhash);
987 list_del(&oo->oo_perclient);
988 while (!list_empty(&oo->oo_owner.so_stateids)) {
989 stp = list_first_entry(&oo->oo_owner.so_stateids,
990 struct nfs4_ol_stateid, st_perstateowner);
991 release_open_stateid(stp);
992 }
993 }
994
995 static void release_last_closed_stateid(struct nfs4_openowner *oo)
996 {
997 struct nfs4_ol_stateid *s = oo->oo_last_closed_stid;
998
999 if (s) {
1000 free_generic_stateid(s);
1001 oo->oo_last_closed_stid = NULL;
1002 }
1003 }
1004
1005 static void nfs4_free_openowner(struct nfs4_openowner *oo)
1006 {
1007 kfree(oo->oo_owner.so_owner.data);
1008 kmem_cache_free(openowner_slab, oo);
1009 }
1010
1011 static void release_openowner(struct nfs4_openowner *oo)
1012 {
1013 unhash_openowner(oo);
1014 list_del(&oo->oo_close_lru);
1015 release_last_closed_stateid(oo);
1016 nfs4_free_openowner(oo);
1017 }
1018
1019 static inline int
1020 hash_sessionid(struct nfs4_sessionid *sessionid)
1021 {
1022 struct nfsd4_sessionid *sid = (struct nfsd4_sessionid *)sessionid;
1023
1024 return sid->sequence % SESSION_HASH_SIZE;
1025 }
1026
1027 #ifdef NFSD_DEBUG
1028 static inline void
1029 dump_sessionid(const char *fn, struct nfs4_sessionid *sessionid)
1030 {
1031 u32 *ptr = (u32 *)(&sessionid->data[0]);
1032 dprintk("%s: %u:%u:%u:%u\n", fn, ptr[0], ptr[1], ptr[2], ptr[3]);
1033 }
1034 #else
1035 static inline void
1036 dump_sessionid(const char *fn, struct nfs4_sessionid *sessionid)
1037 {
1038 }
1039 #endif
1040
1041 /*
1042 * Bump the seqid on cstate->replay_owner, and clear replay_owner if it
1043 * won't be used for replay.
1044 */
1045 void nfsd4_bump_seqid(struct nfsd4_compound_state *cstate, __be32 nfserr)
1046 {
1047 struct nfs4_stateowner *so = cstate->replay_owner;
1048
1049 if (nfserr == nfserr_replay_me)
1050 return;
1051
1052 if (!seqid_mutating_err(ntohl(nfserr))) {
1053 cstate->replay_owner = NULL;
1054 return;
1055 }
1056 if (!so)
1057 return;
1058 if (so->so_is_open_owner)
1059 release_last_closed_stateid(openowner(so));
1060 so->so_seqid++;
1061 return;
1062 }
1063
1064 static void
1065 gen_sessionid(struct nfsd4_session *ses)
1066 {
1067 struct nfs4_client *clp = ses->se_client;
1068 struct nfsd4_sessionid *sid;
1069
1070 sid = (struct nfsd4_sessionid *)ses->se_sessionid.data;
1071 sid->clientid = clp->cl_clientid;
1072 sid->sequence = current_sessionid++;
1073 sid->reserved = 0;
1074 }
1075
1076 /*
1077 * The protocol defines ca_maxresponssize_cached to include the size of
1078 * the rpc header, but all we need to cache is the data starting after
1079 * the end of the initial SEQUENCE operation--the rest we regenerate
1080 * each time. Therefore we can advertise a ca_maxresponssize_cached
1081 * value that is the number of bytes in our cache plus a few additional
1082 * bytes. In order to stay on the safe side, and not promise more than
1083 * we can cache, those additional bytes must be the minimum possible: 24
1084 * bytes of rpc header (xid through accept state, with AUTH_NULL
1085 * verifier), 12 for the compound header (with zero-length tag), and 44
1086 * for the SEQUENCE op response:
1087 */
1088 #define NFSD_MIN_HDR_SEQ_SZ (24 + 12 + 44)
1089
1090 static void
1091 free_session_slots(struct nfsd4_session *ses)
1092 {
1093 int i;
1094
1095 for (i = 0; i < ses->se_fchannel.maxreqs; i++)
1096 kfree(ses->se_slots[i]);
1097 }
1098
1099 /*
1100 * We don't actually need to cache the rpc and session headers, so we
1101 * can allocate a little less for each slot:
1102 */
1103 static inline u32 slot_bytes(struct nfsd4_channel_attrs *ca)
1104 {
1105 u32 size;
1106
1107 if (ca->maxresp_cached < NFSD_MIN_HDR_SEQ_SZ)
1108 size = 0;
1109 else
1110 size = ca->maxresp_cached - NFSD_MIN_HDR_SEQ_SZ;
1111 return size + sizeof(struct nfsd4_slot);
1112 }
1113
1114 /*
1115 * XXX: If we run out of reserved DRC memory we could (up to a point)
1116 * re-negotiate active sessions and reduce their slot usage to make
1117 * room for new connections. For now we just fail the create session.
1118 */
1119 static u32 nfsd4_get_drc_mem(struct nfsd4_channel_attrs *ca)
1120 {
1121 u32 slotsize = slot_bytes(ca);
1122 u32 num = ca->maxreqs;
1123 int avail;
1124
1125 spin_lock(&nfsd_drc_lock);
1126 avail = min((unsigned long)NFSD_MAX_MEM_PER_SESSION,
1127 nfsd_drc_max_mem - nfsd_drc_mem_used);
1128 num = min_t(int, num, avail / slotsize);
1129 nfsd_drc_mem_used += num * slotsize;
1130 spin_unlock(&nfsd_drc_lock);
1131
1132 return num;
1133 }
1134
1135 static void nfsd4_put_drc_mem(struct nfsd4_channel_attrs *ca)
1136 {
1137 int slotsize = slot_bytes(ca);
1138
1139 spin_lock(&nfsd_drc_lock);
1140 nfsd_drc_mem_used -= slotsize * ca->maxreqs;
1141 spin_unlock(&nfsd_drc_lock);
1142 }
1143
1144 static struct nfsd4_session *alloc_session(struct nfsd4_channel_attrs *fattrs,
1145 struct nfsd4_channel_attrs *battrs)
1146 {
1147 int numslots = fattrs->maxreqs;
1148 int slotsize = slot_bytes(fattrs);
1149 struct nfsd4_session *new;
1150 int mem, i;
1151
1152 BUILD_BUG_ON(NFSD_MAX_SLOTS_PER_SESSION * sizeof(struct nfsd4_slot *)
1153 + sizeof(struct nfsd4_session) > PAGE_SIZE);
1154 mem = numslots * sizeof(struct nfsd4_slot *);
1155
1156 new = kzalloc(sizeof(*new) + mem, GFP_KERNEL);
1157 if (!new)
1158 return NULL;
1159 /* allocate each struct nfsd4_slot and data cache in one piece */
1160 for (i = 0; i < numslots; i++) {
1161 new->se_slots[i] = kzalloc(slotsize, GFP_KERNEL);
1162 if (!new->se_slots[i])
1163 goto out_free;
1164 }
1165
1166 memcpy(&new->se_fchannel, fattrs, sizeof(struct nfsd4_channel_attrs));
1167 memcpy(&new->se_bchannel, battrs, sizeof(struct nfsd4_channel_attrs));
1168
1169 return new;
1170 out_free:
1171 while (i--)
1172 kfree(new->se_slots[i]);
1173 kfree(new);
1174 return NULL;
1175 }
1176
1177 static void free_conn(struct nfsd4_conn *c)
1178 {
1179 svc_xprt_put(c->cn_xprt);
1180 kfree(c);
1181 }
1182
1183 static void nfsd4_conn_lost(struct svc_xpt_user *u)
1184 {
1185 struct nfsd4_conn *c = container_of(u, struct nfsd4_conn, cn_xpt_user);
1186 struct nfs4_client *clp = c->cn_session->se_client;
1187
1188 spin_lock(&clp->cl_lock);
1189 if (!list_empty(&c->cn_persession)) {
1190 list_del(&c->cn_persession);
1191 free_conn(c);
1192 }
1193 nfsd4_probe_callback(clp);
1194 spin_unlock(&clp->cl_lock);
1195 }
1196
1197 static struct nfsd4_conn *alloc_conn(struct svc_rqst *rqstp, u32 flags)
1198 {
1199 struct nfsd4_conn *conn;
1200
1201 conn = kmalloc(sizeof(struct nfsd4_conn), GFP_KERNEL);
1202 if (!conn)
1203 return NULL;
1204 svc_xprt_get(rqstp->rq_xprt);
1205 conn->cn_xprt = rqstp->rq_xprt;
1206 conn->cn_flags = flags;
1207 INIT_LIST_HEAD(&conn->cn_xpt_user.list);
1208 return conn;
1209 }
1210
1211 static void __nfsd4_hash_conn(struct nfsd4_conn *conn, struct nfsd4_session *ses)
1212 {
1213 conn->cn_session = ses;
1214 list_add(&conn->cn_persession, &ses->se_conns);
1215 }
1216
1217 static void nfsd4_hash_conn(struct nfsd4_conn *conn, struct nfsd4_session *ses)
1218 {
1219 struct nfs4_client *clp = ses->se_client;
1220
1221 spin_lock(&clp->cl_lock);
1222 __nfsd4_hash_conn(conn, ses);
1223 spin_unlock(&clp->cl_lock);
1224 }
1225
1226 static int nfsd4_register_conn(struct nfsd4_conn *conn)
1227 {
1228 conn->cn_xpt_user.callback = nfsd4_conn_lost;
1229 return register_xpt_user(conn->cn_xprt, &conn->cn_xpt_user);
1230 }
1231
1232 static void nfsd4_init_conn(struct svc_rqst *rqstp, struct nfsd4_conn *conn, struct nfsd4_session *ses)
1233 {
1234 int ret;
1235
1236 nfsd4_hash_conn(conn, ses);
1237 ret = nfsd4_register_conn(conn);
1238 if (ret)
1239 /* oops; xprt is already down: */
1240 nfsd4_conn_lost(&conn->cn_xpt_user);
1241 /* We may have gained or lost a callback channel: */
1242 nfsd4_probe_callback_sync(ses->se_client);
1243 }
1244
1245 static struct nfsd4_conn *alloc_conn_from_crses(struct svc_rqst *rqstp, struct nfsd4_create_session *cses)
1246 {
1247 u32 dir = NFS4_CDFC4_FORE;
1248
1249 if (cses->flags & SESSION4_BACK_CHAN)
1250 dir |= NFS4_CDFC4_BACK;
1251 return alloc_conn(rqstp, dir);
1252 }
1253
1254 /* must be called under client_lock */
1255 static void nfsd4_del_conns(struct nfsd4_session *s)
1256 {
1257 struct nfs4_client *clp = s->se_client;
1258 struct nfsd4_conn *c;
1259
1260 spin_lock(&clp->cl_lock);
1261 while (!list_empty(&s->se_conns)) {
1262 c = list_first_entry(&s->se_conns, struct nfsd4_conn, cn_persession);
1263 list_del_init(&c->cn_persession);
1264 spin_unlock(&clp->cl_lock);
1265
1266 unregister_xpt_user(c->cn_xprt, &c->cn_xpt_user);
1267 free_conn(c);
1268
1269 spin_lock(&clp->cl_lock);
1270 }
1271 spin_unlock(&clp->cl_lock);
1272 }
1273
1274 static void __free_session(struct nfsd4_session *ses)
1275 {
1276 free_session_slots(ses);
1277 kfree(ses);
1278 }
1279
1280 static void free_session(struct nfsd4_session *ses)
1281 {
1282 struct nfsd_net *nn = net_generic(ses->se_client->net, nfsd_net_id);
1283
1284 lockdep_assert_held(&nn->client_lock);
1285 nfsd4_del_conns(ses);
1286 nfsd4_put_drc_mem(&ses->se_fchannel);
1287 __free_session(ses);
1288 }
1289
1290 static void init_session(struct svc_rqst *rqstp, struct nfsd4_session *new, struct nfs4_client *clp, struct nfsd4_create_session *cses)
1291 {
1292 int idx;
1293 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
1294
1295 new->se_client = clp;
1296 gen_sessionid(new);
1297
1298 INIT_LIST_HEAD(&new->se_conns);
1299
1300 new->se_cb_seq_nr = 1;
1301 new->se_flags = cses->flags;
1302 new->se_cb_prog = cses->callback_prog;
1303 new->se_cb_sec = cses->cb_sec;
1304 atomic_set(&new->se_ref, 0);
1305 idx = hash_sessionid(&new->se_sessionid);
1306 spin_lock(&nn->client_lock);
1307 list_add(&new->se_hash, &nn->sessionid_hashtbl[idx]);
1308 spin_lock(&clp->cl_lock);
1309 list_add(&new->se_perclnt, &clp->cl_sessions);
1310 spin_unlock(&clp->cl_lock);
1311 spin_unlock(&nn->client_lock);
1312
1313 if (cses->flags & SESSION4_BACK_CHAN) {
1314 struct sockaddr *sa = svc_addr(rqstp);
1315 /*
1316 * This is a little silly; with sessions there's no real
1317 * use for the callback address. Use the peer address
1318 * as a reasonable default for now, but consider fixing
1319 * the rpc client not to require an address in the
1320 * future:
1321 */
1322 rpc_copy_addr((struct sockaddr *)&clp->cl_cb_conn.cb_addr, sa);
1323 clp->cl_cb_conn.cb_addrlen = svc_addr_len(sa);
1324 }
1325 }
1326
1327 /* caller must hold client_lock */
1328 static struct nfsd4_session *
1329 __find_in_sessionid_hashtbl(struct nfs4_sessionid *sessionid, struct net *net)
1330 {
1331 struct nfsd4_session *elem;
1332 int idx;
1333 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
1334
1335 dump_sessionid(__func__, sessionid);
1336 idx = hash_sessionid(sessionid);
1337 /* Search in the appropriate list */
1338 list_for_each_entry(elem, &nn->sessionid_hashtbl[idx], se_hash) {
1339 if (!memcmp(elem->se_sessionid.data, sessionid->data,
1340 NFS4_MAX_SESSIONID_LEN)) {
1341 return elem;
1342 }
1343 }
1344
1345 dprintk("%s: session not found\n", __func__);
1346 return NULL;
1347 }
1348
1349 static struct nfsd4_session *
1350 find_in_sessionid_hashtbl(struct nfs4_sessionid *sessionid, struct net *net,
1351 __be32 *ret)
1352 {
1353 struct nfsd4_session *session;
1354 __be32 status = nfserr_badsession;
1355
1356 session = __find_in_sessionid_hashtbl(sessionid, net);
1357 if (!session)
1358 goto out;
1359 status = nfsd4_get_session_locked(session);
1360 if (status)
1361 session = NULL;
1362 out:
1363 *ret = status;
1364 return session;
1365 }
1366
1367 /* caller must hold client_lock */
1368 static void
1369 unhash_session(struct nfsd4_session *ses)
1370 {
1371 list_del(&ses->se_hash);
1372 spin_lock(&ses->se_client->cl_lock);
1373 list_del(&ses->se_perclnt);
1374 spin_unlock(&ses->se_client->cl_lock);
1375 }
1376
1377 /* SETCLIENTID and SETCLIENTID_CONFIRM Helper functions */
1378 static int
1379 STALE_CLIENTID(clientid_t *clid, struct nfsd_net *nn)
1380 {
1381 if (clid->cl_boot == nn->boot_time)
1382 return 0;
1383 dprintk("NFSD stale clientid (%08x/%08x) boot_time %08lx\n",
1384 clid->cl_boot, clid->cl_id, nn->boot_time);
1385 return 1;
1386 }
1387
1388 /*
1389 * XXX Should we use a slab cache ?
1390 * This type of memory management is somewhat inefficient, but we use it
1391 * anyway since SETCLIENTID is not a common operation.
1392 */
1393 static struct nfs4_client *alloc_client(struct xdr_netobj name)
1394 {
1395 struct nfs4_client *clp;
1396
1397 clp = kzalloc(sizeof(struct nfs4_client), GFP_KERNEL);
1398 if (clp == NULL)
1399 return NULL;
1400 clp->cl_name.data = kmemdup(name.data, name.len, GFP_KERNEL);
1401 if (clp->cl_name.data == NULL) {
1402 kfree(clp);
1403 return NULL;
1404 }
1405 clp->cl_name.len = name.len;
1406 INIT_LIST_HEAD(&clp->cl_sessions);
1407 idr_init(&clp->cl_stateids);
1408 atomic_set(&clp->cl_refcount, 0);
1409 clp->cl_cb_state = NFSD4_CB_UNKNOWN;
1410 INIT_LIST_HEAD(&clp->cl_idhash);
1411 INIT_LIST_HEAD(&clp->cl_openowners);
1412 INIT_LIST_HEAD(&clp->cl_delegations);
1413 INIT_LIST_HEAD(&clp->cl_lru);
1414 INIT_LIST_HEAD(&clp->cl_callbacks);
1415 INIT_LIST_HEAD(&clp->cl_revoked);
1416 spin_lock_init(&clp->cl_lock);
1417 rpc_init_wait_queue(&clp->cl_cb_waitq, "Backchannel slot table");
1418 return clp;
1419 }
1420
1421 static void
1422 free_client(struct nfs4_client *clp)
1423 {
1424 struct nfsd_net __maybe_unused *nn = net_generic(clp->net, nfsd_net_id);
1425
1426 lockdep_assert_held(&nn->client_lock);
1427 while (!list_empty(&clp->cl_sessions)) {
1428 struct nfsd4_session *ses;
1429 ses = list_entry(clp->cl_sessions.next, struct nfsd4_session,
1430 se_perclnt);
1431 list_del(&ses->se_perclnt);
1432 WARN_ON_ONCE(atomic_read(&ses->se_ref));
1433 free_session(ses);
1434 }
1435 rpc_destroy_wait_queue(&clp->cl_cb_waitq);
1436 free_svc_cred(&clp->cl_cred);
1437 kfree(clp->cl_name.data);
1438 idr_destroy(&clp->cl_stateids);
1439 kfree(clp);
1440 }
1441
1442 /* must be called under the client_lock */
1443 static inline void
1444 unhash_client_locked(struct nfs4_client *clp)
1445 {
1446 struct nfsd4_session *ses;
1447
1448 list_del(&clp->cl_lru);
1449 spin_lock(&clp->cl_lock);
1450 list_for_each_entry(ses, &clp->cl_sessions, se_perclnt)
1451 list_del_init(&ses->se_hash);
1452 spin_unlock(&clp->cl_lock);
1453 }
1454
1455 static void
1456 destroy_client(struct nfs4_client *clp)
1457 {
1458 struct nfs4_openowner *oo;
1459 struct nfs4_delegation *dp;
1460 struct list_head reaplist;
1461 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
1462
1463 INIT_LIST_HEAD(&reaplist);
1464 spin_lock(&state_lock);
1465 while (!list_empty(&clp->cl_delegations)) {
1466 dp = list_entry(clp->cl_delegations.next, struct nfs4_delegation, dl_perclnt);
1467 list_del_init(&dp->dl_perclnt);
1468 /* Ensure that deleg break won't try to requeue it */
1469 ++dp->dl_time;
1470 list_move(&dp->dl_recall_lru, &reaplist);
1471 }
1472 spin_unlock(&state_lock);
1473 while (!list_empty(&reaplist)) {
1474 dp = list_entry(reaplist.next, struct nfs4_delegation, dl_recall_lru);
1475 destroy_delegation(dp);
1476 }
1477 list_splice_init(&clp->cl_revoked, &reaplist);
1478 while (!list_empty(&reaplist)) {
1479 dp = list_entry(reaplist.next, struct nfs4_delegation, dl_recall_lru);
1480 destroy_revoked_delegation(dp);
1481 }
1482 while (!list_empty(&clp->cl_openowners)) {
1483 oo = list_entry(clp->cl_openowners.next, struct nfs4_openowner, oo_perclient);
1484 release_openowner(oo);
1485 }
1486 nfsd4_shutdown_callback(clp);
1487 if (clp->cl_cb_conn.cb_xprt)
1488 svc_xprt_put(clp->cl_cb_conn.cb_xprt);
1489 list_del(&clp->cl_idhash);
1490 if (test_bit(NFSD4_CLIENT_CONFIRMED, &clp->cl_flags))
1491 rb_erase(&clp->cl_namenode, &nn->conf_name_tree);
1492 else
1493 rb_erase(&clp->cl_namenode, &nn->unconf_name_tree);
1494 spin_lock(&nn->client_lock);
1495 unhash_client_locked(clp);
1496 WARN_ON_ONCE(atomic_read(&clp->cl_refcount));
1497 free_client(clp);
1498 spin_unlock(&nn->client_lock);
1499 }
1500
1501 static void expire_client(struct nfs4_client *clp)
1502 {
1503 nfsd4_client_record_remove(clp);
1504 destroy_client(clp);
1505 }
1506
1507 static void copy_verf(struct nfs4_client *target, nfs4_verifier *source)
1508 {
1509 memcpy(target->cl_verifier.data, source->data,
1510 sizeof(target->cl_verifier.data));
1511 }
1512
1513 static void copy_clid(struct nfs4_client *target, struct nfs4_client *source)
1514 {
1515 target->cl_clientid.cl_boot = source->cl_clientid.cl_boot;
1516 target->cl_clientid.cl_id = source->cl_clientid.cl_id;
1517 }
1518
1519 static int copy_cred(struct svc_cred *target, struct svc_cred *source)
1520 {
1521 if (source->cr_principal) {
1522 target->cr_principal =
1523 kstrdup(source->cr_principal, GFP_KERNEL);
1524 if (target->cr_principal == NULL)
1525 return -ENOMEM;
1526 } else
1527 target->cr_principal = NULL;
1528 target->cr_flavor = source->cr_flavor;
1529 target->cr_uid = source->cr_uid;
1530 target->cr_gid = source->cr_gid;
1531 target->cr_group_info = source->cr_group_info;
1532 get_group_info(target->cr_group_info);
1533 target->cr_gss_mech = source->cr_gss_mech;
1534 if (source->cr_gss_mech)
1535 gss_mech_get(source->cr_gss_mech);
1536 return 0;
1537 }
1538
1539 static long long
1540 compare_blob(const struct xdr_netobj *o1, const struct xdr_netobj *o2)
1541 {
1542 long long res;
1543
1544 res = o1->len - o2->len;
1545 if (res)
1546 return res;
1547 return (long long)memcmp(o1->data, o2->data, o1->len);
1548 }
1549
1550 static int same_name(const char *n1, const char *n2)
1551 {
1552 return 0 == memcmp(n1, n2, HEXDIR_LEN);
1553 }
1554
1555 static int
1556 same_verf(nfs4_verifier *v1, nfs4_verifier *v2)
1557 {
1558 return 0 == memcmp(v1->data, v2->data, sizeof(v1->data));
1559 }
1560
1561 static int
1562 same_clid(clientid_t *cl1, clientid_t *cl2)
1563 {
1564 return (cl1->cl_boot == cl2->cl_boot) && (cl1->cl_id == cl2->cl_id);
1565 }
1566
1567 static bool groups_equal(struct group_info *g1, struct group_info *g2)
1568 {
1569 int i;
1570
1571 if (g1->ngroups != g2->ngroups)
1572 return false;
1573 for (i=0; i<g1->ngroups; i++)
1574 if (!gid_eq(GROUP_AT(g1, i), GROUP_AT(g2, i)))
1575 return false;
1576 return true;
1577 }
1578
1579 /*
1580 * RFC 3530 language requires clid_inuse be returned when the
1581 * "principal" associated with a requests differs from that previously
1582 * used. We use uid, gid's, and gss principal string as our best
1583 * approximation. We also don't want to allow non-gss use of a client
1584 * established using gss: in theory cr_principal should catch that
1585 * change, but in practice cr_principal can be null even in the gss case
1586 * since gssd doesn't always pass down a principal string.
1587 */
1588 static bool is_gss_cred(struct svc_cred *cr)
1589 {
1590 /* Is cr_flavor one of the gss "pseudoflavors"?: */
1591 return (cr->cr_flavor > RPC_AUTH_MAXFLAVOR);
1592 }
1593
1594
1595 static bool
1596 same_creds(struct svc_cred *cr1, struct svc_cred *cr2)
1597 {
1598 if ((is_gss_cred(cr1) != is_gss_cred(cr2))
1599 || (!uid_eq(cr1->cr_uid, cr2->cr_uid))
1600 || (!gid_eq(cr1->cr_gid, cr2->cr_gid))
1601 || !groups_equal(cr1->cr_group_info, cr2->cr_group_info))
1602 return false;
1603 if (cr1->cr_principal == cr2->cr_principal)
1604 return true;
1605 if (!cr1->cr_principal || !cr2->cr_principal)
1606 return false;
1607 return 0 == strcmp(cr1->cr_principal, cr2->cr_principal);
1608 }
1609
1610 static bool svc_rqst_integrity_protected(struct svc_rqst *rqstp)
1611 {
1612 struct svc_cred *cr = &rqstp->rq_cred;
1613 u32 service;
1614
1615 if (!cr->cr_gss_mech)
1616 return false;
1617 service = gss_pseudoflavor_to_service(cr->cr_gss_mech, cr->cr_flavor);
1618 return service == RPC_GSS_SVC_INTEGRITY ||
1619 service == RPC_GSS_SVC_PRIVACY;
1620 }
1621
1622 static bool mach_creds_match(struct nfs4_client *cl, struct svc_rqst *rqstp)
1623 {
1624 struct svc_cred *cr = &rqstp->rq_cred;
1625
1626 if (!cl->cl_mach_cred)
1627 return true;
1628 if (cl->cl_cred.cr_gss_mech != cr->cr_gss_mech)
1629 return false;
1630 if (!svc_rqst_integrity_protected(rqstp))
1631 return false;
1632 if (!cr->cr_principal)
1633 return false;
1634 return 0 == strcmp(cl->cl_cred.cr_principal, cr->cr_principal);
1635 }
1636
1637 static void gen_clid(struct nfs4_client *clp, struct nfsd_net *nn)
1638 {
1639 static u32 current_clientid = 1;
1640
1641 clp->cl_clientid.cl_boot = nn->boot_time;
1642 clp->cl_clientid.cl_id = current_clientid++;
1643 }
1644
1645 static void gen_confirm(struct nfs4_client *clp)
1646 {
1647 __be32 verf[2];
1648 static u32 i;
1649
1650 /*
1651 * This is opaque to client, so no need to byte-swap. Use
1652 * __force to keep sparse happy
1653 */
1654 verf[0] = (__force __be32)get_seconds();
1655 verf[1] = (__force __be32)i++;
1656 memcpy(clp->cl_confirm.data, verf, sizeof(clp->cl_confirm.data));
1657 }
1658
1659 static struct nfs4_stid *find_stateid(struct nfs4_client *cl, stateid_t *t)
1660 {
1661 struct nfs4_stid *ret;
1662
1663 ret = idr_find(&cl->cl_stateids, t->si_opaque.so_id);
1664 if (!ret || !ret->sc_type)
1665 return NULL;
1666 return ret;
1667 }
1668
1669 static struct nfs4_stid *find_stateid_by_type(struct nfs4_client *cl, stateid_t *t, char typemask)
1670 {
1671 struct nfs4_stid *s;
1672
1673 s = find_stateid(cl, t);
1674 if (!s)
1675 return NULL;
1676 if (typemask & s->sc_type)
1677 return s;
1678 return NULL;
1679 }
1680
1681 static struct nfs4_client *create_client(struct xdr_netobj name,
1682 struct svc_rqst *rqstp, nfs4_verifier *verf)
1683 {
1684 struct nfs4_client *clp;
1685 struct sockaddr *sa = svc_addr(rqstp);
1686 int ret;
1687 struct net *net = SVC_NET(rqstp);
1688 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
1689
1690 clp = alloc_client(name);
1691 if (clp == NULL)
1692 return NULL;
1693
1694 ret = copy_cred(&clp->cl_cred, &rqstp->rq_cred);
1695 if (ret) {
1696 spin_lock(&nn->client_lock);
1697 free_client(clp);
1698 spin_unlock(&nn->client_lock);
1699 return NULL;
1700 }
1701 INIT_WORK(&clp->cl_cb_null.cb_work, nfsd4_run_cb_null);
1702 clp->cl_time = get_seconds();
1703 clear_bit(0, &clp->cl_cb_slot_busy);
1704 copy_verf(clp, verf);
1705 rpc_copy_addr((struct sockaddr *) &clp->cl_addr, sa);
1706 gen_confirm(clp);
1707 clp->cl_cb_session = NULL;
1708 clp->net = net;
1709 return clp;
1710 }
1711
1712 static void
1713 add_clp_to_name_tree(struct nfs4_client *new_clp, struct rb_root *root)
1714 {
1715 struct rb_node **new = &(root->rb_node), *parent = NULL;
1716 struct nfs4_client *clp;
1717
1718 while (*new) {
1719 clp = rb_entry(*new, struct nfs4_client, cl_namenode);
1720 parent = *new;
1721
1722 if (compare_blob(&clp->cl_name, &new_clp->cl_name) > 0)
1723 new = &((*new)->rb_left);
1724 else
1725 new = &((*new)->rb_right);
1726 }
1727
1728 rb_link_node(&new_clp->cl_namenode, parent, new);
1729 rb_insert_color(&new_clp->cl_namenode, root);
1730 }
1731
1732 static struct nfs4_client *
1733 find_clp_in_name_tree(struct xdr_netobj *name, struct rb_root *root)
1734 {
1735 long long cmp;
1736 struct rb_node *node = root->rb_node;
1737 struct nfs4_client *clp;
1738
1739 while (node) {
1740 clp = rb_entry(node, struct nfs4_client, cl_namenode);
1741 cmp = compare_blob(&clp->cl_name, name);
1742 if (cmp > 0)
1743 node = node->rb_left;
1744 else if (cmp < 0)
1745 node = node->rb_right;
1746 else
1747 return clp;
1748 }
1749 return NULL;
1750 }
1751
1752 static void
1753 add_to_unconfirmed(struct nfs4_client *clp)
1754 {
1755 unsigned int idhashval;
1756 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
1757
1758 clear_bit(NFSD4_CLIENT_CONFIRMED, &clp->cl_flags);
1759 add_clp_to_name_tree(clp, &nn->unconf_name_tree);
1760 idhashval = clientid_hashval(clp->cl_clientid.cl_id);
1761 list_add(&clp->cl_idhash, &nn->unconf_id_hashtbl[idhashval]);
1762 renew_client(clp);
1763 }
1764
1765 static void
1766 move_to_confirmed(struct nfs4_client *clp)
1767 {
1768 unsigned int idhashval = clientid_hashval(clp->cl_clientid.cl_id);
1769 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
1770
1771 dprintk("NFSD: move_to_confirm nfs4_client %p\n", clp);
1772 list_move(&clp->cl_idhash, &nn->conf_id_hashtbl[idhashval]);
1773 rb_erase(&clp->cl_namenode, &nn->unconf_name_tree);
1774 add_clp_to_name_tree(clp, &nn->conf_name_tree);
1775 set_bit(NFSD4_CLIENT_CONFIRMED, &clp->cl_flags);
1776 renew_client(clp);
1777 }
1778
1779 static struct nfs4_client *
1780 find_client_in_id_table(struct list_head *tbl, clientid_t *clid, bool sessions)
1781 {
1782 struct nfs4_client *clp;
1783 unsigned int idhashval = clientid_hashval(clid->cl_id);
1784
1785 list_for_each_entry(clp, &tbl[idhashval], cl_idhash) {
1786 if (same_clid(&clp->cl_clientid, clid)) {
1787 if ((bool)clp->cl_minorversion != sessions)
1788 return NULL;
1789 renew_client(clp);
1790 return clp;
1791 }
1792 }
1793 return NULL;
1794 }
1795
1796 static struct nfs4_client *
1797 find_confirmed_client(clientid_t *clid, bool sessions, struct nfsd_net *nn)
1798 {
1799 struct list_head *tbl = nn->conf_id_hashtbl;
1800
1801 return find_client_in_id_table(tbl, clid, sessions);
1802 }
1803
1804 static struct nfs4_client *
1805 find_unconfirmed_client(clientid_t *clid, bool sessions, struct nfsd_net *nn)
1806 {
1807 struct list_head *tbl = nn->unconf_id_hashtbl;
1808
1809 return find_client_in_id_table(tbl, clid, sessions);
1810 }
1811
1812 static bool clp_used_exchangeid(struct nfs4_client *clp)
1813 {
1814 return clp->cl_exchange_flags != 0;
1815 }
1816
1817 static struct nfs4_client *
1818 find_confirmed_client_by_name(struct xdr_netobj *name, struct nfsd_net *nn)
1819 {
1820 return find_clp_in_name_tree(name, &nn->conf_name_tree);
1821 }
1822
1823 static struct nfs4_client *
1824 find_unconfirmed_client_by_name(struct xdr_netobj *name, struct nfsd_net *nn)
1825 {
1826 return find_clp_in_name_tree(name, &nn->unconf_name_tree);
1827 }
1828
1829 static void
1830 gen_callback(struct nfs4_client *clp, struct nfsd4_setclientid *se, struct svc_rqst *rqstp)
1831 {
1832 struct nfs4_cb_conn *conn = &clp->cl_cb_conn;
1833 struct sockaddr *sa = svc_addr(rqstp);
1834 u32 scopeid = rpc_get_scope_id(sa);
1835 unsigned short expected_family;
1836
1837 /* Currently, we only support tcp and tcp6 for the callback channel */
1838 if (se->se_callback_netid_len == 3 &&
1839 !memcmp(se->se_callback_netid_val, "tcp", 3))
1840 expected_family = AF_INET;
1841 else if (se->se_callback_netid_len == 4 &&
1842 !memcmp(se->se_callback_netid_val, "tcp6", 4))
1843 expected_family = AF_INET6;
1844 else
1845 goto out_err;
1846
1847 conn->cb_addrlen = rpc_uaddr2sockaddr(clp->net, se->se_callback_addr_val,
1848 se->se_callback_addr_len,
1849 (struct sockaddr *)&conn->cb_addr,
1850 sizeof(conn->cb_addr));
1851
1852 if (!conn->cb_addrlen || conn->cb_addr.ss_family != expected_family)
1853 goto out_err;
1854
1855 if (conn->cb_addr.ss_family == AF_INET6)
1856 ((struct sockaddr_in6 *)&conn->cb_addr)->sin6_scope_id = scopeid;
1857
1858 conn->cb_prog = se->se_callback_prog;
1859 conn->cb_ident = se->se_callback_ident;
1860 memcpy(&conn->cb_saddr, &rqstp->rq_daddr, rqstp->rq_daddrlen);
1861 return;
1862 out_err:
1863 conn->cb_addr.ss_family = AF_UNSPEC;
1864 conn->cb_addrlen = 0;
1865 dprintk(KERN_INFO "NFSD: this client (clientid %08x/%08x) "
1866 "will not receive delegations\n",
1867 clp->cl_clientid.cl_boot, clp->cl_clientid.cl_id);
1868
1869 return;
1870 }
1871
1872 /*
1873 * Cache a reply. nfsd4_check_resp_size() has bounded the cache size.
1874 */
1875 static void
1876 nfsd4_store_cache_entry(struct nfsd4_compoundres *resp)
1877 {
1878 struct xdr_buf *buf = resp->xdr.buf;
1879 struct nfsd4_slot *slot = resp->cstate.slot;
1880 unsigned int base;
1881
1882 dprintk("--> %s slot %p\n", __func__, slot);
1883
1884 slot->sl_opcnt = resp->opcnt;
1885 slot->sl_status = resp->cstate.status;
1886
1887 slot->sl_flags |= NFSD4_SLOT_INITIALIZED;
1888 if (nfsd4_not_cached(resp)) {
1889 slot->sl_datalen = 0;
1890 return;
1891 }
1892 base = resp->cstate.data_offset;
1893 slot->sl_datalen = buf->len - base;
1894 if (read_bytes_from_xdr_buf(buf, base, slot->sl_data, slot->sl_datalen))
1895 WARN("%s: sessions DRC could not cache compound\n", __func__);
1896 return;
1897 }
1898
1899 /*
1900 * Encode the replay sequence operation from the slot values.
1901 * If cachethis is FALSE encode the uncached rep error on the next
1902 * operation which sets resp->p and increments resp->opcnt for
1903 * nfs4svc_encode_compoundres.
1904 *
1905 */
1906 static __be32
1907 nfsd4_enc_sequence_replay(struct nfsd4_compoundargs *args,
1908 struct nfsd4_compoundres *resp)
1909 {
1910 struct nfsd4_op *op;
1911 struct nfsd4_slot *slot = resp->cstate.slot;
1912
1913 /* Encode the replayed sequence operation */
1914 op = &args->ops[resp->opcnt - 1];
1915 nfsd4_encode_operation(resp, op);
1916
1917 /* Return nfserr_retry_uncached_rep in next operation. */
1918 if (args->opcnt > 1 && !(slot->sl_flags & NFSD4_SLOT_CACHETHIS)) {
1919 op = &args->ops[resp->opcnt++];
1920 op->status = nfserr_retry_uncached_rep;
1921 nfsd4_encode_operation(resp, op);
1922 }
1923 return op->status;
1924 }
1925
1926 /*
1927 * The sequence operation is not cached because we can use the slot and
1928 * session values.
1929 */
1930 static __be32
1931 nfsd4_replay_cache_entry(struct nfsd4_compoundres *resp,
1932 struct nfsd4_sequence *seq)
1933 {
1934 struct nfsd4_slot *slot = resp->cstate.slot;
1935 struct xdr_stream *xdr = &resp->xdr;
1936 __be32 *p;
1937 __be32 status;
1938
1939 dprintk("--> %s slot %p\n", __func__, slot);
1940
1941 status = nfsd4_enc_sequence_replay(resp->rqstp->rq_argp, resp);
1942 if (status)
1943 return status;
1944
1945 p = xdr_reserve_space(xdr, slot->sl_datalen);
1946 if (!p) {
1947 WARN_ON_ONCE(1);
1948 return nfserr_serverfault;
1949 }
1950 xdr_encode_opaque_fixed(p, slot->sl_data, slot->sl_datalen);
1951 xdr_commit_encode(xdr);
1952
1953 resp->opcnt = slot->sl_opcnt;
1954 return slot->sl_status;
1955 }
1956
1957 /*
1958 * Set the exchange_id flags returned by the server.
1959 */
1960 static void
1961 nfsd4_set_ex_flags(struct nfs4_client *new, struct nfsd4_exchange_id *clid)
1962 {
1963 /* pNFS is not supported */
1964 new->cl_exchange_flags |= EXCHGID4_FLAG_USE_NON_PNFS;
1965
1966 /* Referrals are supported, Migration is not. */
1967 new->cl_exchange_flags |= EXCHGID4_FLAG_SUPP_MOVED_REFER;
1968
1969 /* set the wire flags to return to client. */
1970 clid->flags = new->cl_exchange_flags;
1971 }
1972
1973 static bool client_has_state(struct nfs4_client *clp)
1974 {
1975 /*
1976 * Note clp->cl_openowners check isn't quite right: there's no
1977 * need to count owners without stateid's.
1978 *
1979 * Also note we should probably be using this in 4.0 case too.
1980 */
1981 return !list_empty(&clp->cl_openowners)
1982 || !list_empty(&clp->cl_delegations)
1983 || !list_empty(&clp->cl_sessions);
1984 }
1985
1986 __be32
1987 nfsd4_exchange_id(struct svc_rqst *rqstp,
1988 struct nfsd4_compound_state *cstate,
1989 struct nfsd4_exchange_id *exid)
1990 {
1991 struct nfs4_client *unconf, *conf, *new;
1992 __be32 status;
1993 char addr_str[INET6_ADDRSTRLEN];
1994 nfs4_verifier verf = exid->verifier;
1995 struct sockaddr *sa = svc_addr(rqstp);
1996 bool update = exid->flags & EXCHGID4_FLAG_UPD_CONFIRMED_REC_A;
1997 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
1998
1999 rpc_ntop(sa, addr_str, sizeof(addr_str));
2000 dprintk("%s rqstp=%p exid=%p clname.len=%u clname.data=%p "
2001 "ip_addr=%s flags %x, spa_how %d\n",
2002 __func__, rqstp, exid, exid->clname.len, exid->clname.data,
2003 addr_str, exid->flags, exid->spa_how);
2004
2005 if (exid->flags & ~EXCHGID4_FLAG_MASK_A)
2006 return nfserr_inval;
2007
2008 switch (exid->spa_how) {
2009 case SP4_MACH_CRED:
2010 if (!svc_rqst_integrity_protected(rqstp))
2011 return nfserr_inval;
2012 case SP4_NONE:
2013 break;
2014 default: /* checked by xdr code */
2015 WARN_ON_ONCE(1);
2016 case SP4_SSV:
2017 return nfserr_encr_alg_unsupp;
2018 }
2019
2020 /* Cases below refer to rfc 5661 section 18.35.4: */
2021 nfs4_lock_state();
2022 conf = find_confirmed_client_by_name(&exid->clname, nn);
2023 if (conf) {
2024 bool creds_match = same_creds(&conf->cl_cred, &rqstp->rq_cred);
2025 bool verfs_match = same_verf(&verf, &conf->cl_verifier);
2026
2027 if (update) {
2028 if (!clp_used_exchangeid(conf)) { /* buggy client */
2029 status = nfserr_inval;
2030 goto out;
2031 }
2032 if (!mach_creds_match(conf, rqstp)) {
2033 status = nfserr_wrong_cred;
2034 goto out;
2035 }
2036 if (!creds_match) { /* case 9 */
2037 status = nfserr_perm;
2038 goto out;
2039 }
2040 if (!verfs_match) { /* case 8 */
2041 status = nfserr_not_same;
2042 goto out;
2043 }
2044 /* case 6 */
2045 exid->flags |= EXCHGID4_FLAG_CONFIRMED_R;
2046 new = conf;
2047 goto out_copy;
2048 }
2049 if (!creds_match) { /* case 3 */
2050 if (client_has_state(conf)) {
2051 status = nfserr_clid_inuse;
2052 goto out;
2053 }
2054 expire_client(conf);
2055 goto out_new;
2056 }
2057 if (verfs_match) { /* case 2 */
2058 conf->cl_exchange_flags |= EXCHGID4_FLAG_CONFIRMED_R;
2059 new = conf;
2060 goto out_copy;
2061 }
2062 /* case 5, client reboot */
2063 goto out_new;
2064 }
2065
2066 if (update) { /* case 7 */
2067 status = nfserr_noent;
2068 goto out;
2069 }
2070
2071 unconf = find_unconfirmed_client_by_name(&exid->clname, nn);
2072 if (unconf) /* case 4, possible retry or client restart */
2073 expire_client(unconf);
2074
2075 /* case 1 (normal case) */
2076 out_new:
2077 new = create_client(exid->clname, rqstp, &verf);
2078 if (new == NULL) {
2079 status = nfserr_jukebox;
2080 goto out;
2081 }
2082 new->cl_minorversion = cstate->minorversion;
2083 new->cl_mach_cred = (exid->spa_how == SP4_MACH_CRED);
2084
2085 gen_clid(new, nn);
2086 add_to_unconfirmed(new);
2087 out_copy:
2088 exid->clientid.cl_boot = new->cl_clientid.cl_boot;
2089 exid->clientid.cl_id = new->cl_clientid.cl_id;
2090
2091 exid->seqid = new->cl_cs_slot.sl_seqid + 1;
2092 nfsd4_set_ex_flags(new, exid);
2093
2094 dprintk("nfsd4_exchange_id seqid %d flags %x\n",
2095 new->cl_cs_slot.sl_seqid, new->cl_exchange_flags);
2096 status = nfs_ok;
2097
2098 out:
2099 nfs4_unlock_state();
2100 return status;
2101 }
2102
2103 static __be32
2104 check_slot_seqid(u32 seqid, u32 slot_seqid, int slot_inuse)
2105 {
2106 dprintk("%s enter. seqid %d slot_seqid %d\n", __func__, seqid,
2107 slot_seqid);
2108
2109 /* The slot is in use, and no response has been sent. */
2110 if (slot_inuse) {
2111 if (seqid == slot_seqid)
2112 return nfserr_jukebox;
2113 else
2114 return nfserr_seq_misordered;
2115 }
2116 /* Note unsigned 32-bit arithmetic handles wraparound: */
2117 if (likely(seqid == slot_seqid + 1))
2118 return nfs_ok;
2119 if (seqid == slot_seqid)
2120 return nfserr_replay_cache;
2121 return nfserr_seq_misordered;
2122 }
2123
2124 /*
2125 * Cache the create session result into the create session single DRC
2126 * slot cache by saving the xdr structure. sl_seqid has been set.
2127 * Do this for solo or embedded create session operations.
2128 */
2129 static void
2130 nfsd4_cache_create_session(struct nfsd4_create_session *cr_ses,
2131 struct nfsd4_clid_slot *slot, __be32 nfserr)
2132 {
2133 slot->sl_status = nfserr;
2134 memcpy(&slot->sl_cr_ses, cr_ses, sizeof(*cr_ses));
2135 }
2136
2137 static __be32
2138 nfsd4_replay_create_session(struct nfsd4_create_session *cr_ses,
2139 struct nfsd4_clid_slot *slot)
2140 {
2141 memcpy(cr_ses, &slot->sl_cr_ses, sizeof(*cr_ses));
2142 return slot->sl_status;
2143 }
2144
2145 #define NFSD_MIN_REQ_HDR_SEQ_SZ ((\
2146 2 * 2 + /* credential,verifier: AUTH_NULL, length 0 */ \
2147 1 + /* MIN tag is length with zero, only length */ \
2148 3 + /* version, opcount, opcode */ \
2149 XDR_QUADLEN(NFS4_MAX_SESSIONID_LEN) + \
2150 /* seqid, slotID, slotID, cache */ \
2151 4 ) * sizeof(__be32))
2152
2153 #define NFSD_MIN_RESP_HDR_SEQ_SZ ((\
2154 2 + /* verifier: AUTH_NULL, length 0 */\
2155 1 + /* status */ \
2156 1 + /* MIN tag is length with zero, only length */ \
2157 3 + /* opcount, opcode, opstatus*/ \
2158 XDR_QUADLEN(NFS4_MAX_SESSIONID_LEN) + \
2159 /* seqid, slotID, slotID, slotID, status */ \
2160 5 ) * sizeof(__be32))
2161
2162 static __be32 check_forechannel_attrs(struct nfsd4_channel_attrs *ca, struct nfsd_net *nn)
2163 {
2164 u32 maxrpc = nn->nfsd_serv->sv_max_mesg;
2165
2166 if (ca->maxreq_sz < NFSD_MIN_REQ_HDR_SEQ_SZ)
2167 return nfserr_toosmall;
2168 if (ca->maxresp_sz < NFSD_MIN_RESP_HDR_SEQ_SZ)
2169 return nfserr_toosmall;
2170 ca->headerpadsz = 0;
2171 ca->maxreq_sz = min_t(u32, ca->maxreq_sz, maxrpc);
2172 ca->maxresp_sz = min_t(u32, ca->maxresp_sz, maxrpc);
2173 ca->maxops = min_t(u32, ca->maxops, NFSD_MAX_OPS_PER_COMPOUND);
2174 ca->maxresp_cached = min_t(u32, ca->maxresp_cached,
2175 NFSD_SLOT_CACHE_SIZE + NFSD_MIN_HDR_SEQ_SZ);
2176 ca->maxreqs = min_t(u32, ca->maxreqs, NFSD_MAX_SLOTS_PER_SESSION);
2177 /*
2178 * Note decreasing slot size below client's request may make it
2179 * difficult for client to function correctly, whereas
2180 * decreasing the number of slots will (just?) affect
2181 * performance. When short on memory we therefore prefer to
2182 * decrease number of slots instead of their size. Clients that
2183 * request larger slots than they need will get poor results:
2184 */
2185 ca->maxreqs = nfsd4_get_drc_mem(ca);
2186 if (!ca->maxreqs)
2187 return nfserr_jukebox;
2188
2189 return nfs_ok;
2190 }
2191
2192 #define NFSD_CB_MAX_REQ_SZ ((NFS4_enc_cb_recall_sz + \
2193 RPC_MAX_HEADER_WITH_AUTH) * sizeof(__be32))
2194 #define NFSD_CB_MAX_RESP_SZ ((NFS4_dec_cb_recall_sz + \
2195 RPC_MAX_REPHEADER_WITH_AUTH) * sizeof(__be32))
2196
2197 static __be32 check_backchannel_attrs(struct nfsd4_channel_attrs *ca)
2198 {
2199 ca->headerpadsz = 0;
2200
2201 /*
2202 * These RPC_MAX_HEADER macros are overkill, especially since we
2203 * don't even do gss on the backchannel yet. But this is still
2204 * less than 1k. Tighten up this estimate in the unlikely event
2205 * it turns out to be a problem for some client:
2206 */
2207 if (ca->maxreq_sz < NFSD_CB_MAX_REQ_SZ)
2208 return nfserr_toosmall;
2209 if (ca->maxresp_sz < NFSD_CB_MAX_RESP_SZ)
2210 return nfserr_toosmall;
2211 ca->maxresp_cached = 0;
2212 if (ca->maxops < 2)
2213 return nfserr_toosmall;
2214
2215 return nfs_ok;
2216 }
2217
2218 static __be32 nfsd4_check_cb_sec(struct nfsd4_cb_sec *cbs)
2219 {
2220 switch (cbs->flavor) {
2221 case RPC_AUTH_NULL:
2222 case RPC_AUTH_UNIX:
2223 return nfs_ok;
2224 default:
2225 /*
2226 * GSS case: the spec doesn't allow us to return this
2227 * error. But it also doesn't allow us not to support
2228 * GSS.
2229 * I'd rather this fail hard than return some error the
2230 * client might think it can already handle:
2231 */
2232 return nfserr_encr_alg_unsupp;
2233 }
2234 }
2235
2236 __be32
2237 nfsd4_create_session(struct svc_rqst *rqstp,
2238 struct nfsd4_compound_state *cstate,
2239 struct nfsd4_create_session *cr_ses)
2240 {
2241 struct sockaddr *sa = svc_addr(rqstp);
2242 struct nfs4_client *conf, *unconf;
2243 struct nfsd4_session *new;
2244 struct nfsd4_conn *conn;
2245 struct nfsd4_clid_slot *cs_slot = NULL;
2246 __be32 status = 0;
2247 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
2248
2249 if (cr_ses->flags & ~SESSION4_FLAG_MASK_A)
2250 return nfserr_inval;
2251 status = nfsd4_check_cb_sec(&cr_ses->cb_sec);
2252 if (status)
2253 return status;
2254 status = check_forechannel_attrs(&cr_ses->fore_channel, nn);
2255 if (status)
2256 return status;
2257 status = check_backchannel_attrs(&cr_ses->back_channel);
2258 if (status)
2259 goto out_release_drc_mem;
2260 status = nfserr_jukebox;
2261 new = alloc_session(&cr_ses->fore_channel, &cr_ses->back_channel);
2262 if (!new)
2263 goto out_release_drc_mem;
2264 conn = alloc_conn_from_crses(rqstp, cr_ses);
2265 if (!conn)
2266 goto out_free_session;
2267
2268 nfs4_lock_state();
2269 unconf = find_unconfirmed_client(&cr_ses->clientid, true, nn);
2270 conf = find_confirmed_client(&cr_ses->clientid, true, nn);
2271 WARN_ON_ONCE(conf && unconf);
2272
2273 if (conf) {
2274 status = nfserr_wrong_cred;
2275 if (!mach_creds_match(conf, rqstp))
2276 goto out_free_conn;
2277 cs_slot = &conf->cl_cs_slot;
2278 status = check_slot_seqid(cr_ses->seqid, cs_slot->sl_seqid, 0);
2279 if (status == nfserr_replay_cache) {
2280 status = nfsd4_replay_create_session(cr_ses, cs_slot);
2281 goto out_free_conn;
2282 } else if (cr_ses->seqid != cs_slot->sl_seqid + 1) {
2283 status = nfserr_seq_misordered;
2284 goto out_free_conn;
2285 }
2286 } else if (unconf) {
2287 struct nfs4_client *old;
2288 if (!same_creds(&unconf->cl_cred, &rqstp->rq_cred) ||
2289 !rpc_cmp_addr(sa, (struct sockaddr *) &unconf->cl_addr)) {
2290 status = nfserr_clid_inuse;
2291 goto out_free_conn;
2292 }
2293 status = nfserr_wrong_cred;
2294 if (!mach_creds_match(unconf, rqstp))
2295 goto out_free_conn;
2296 cs_slot = &unconf->cl_cs_slot;
2297 status = check_slot_seqid(cr_ses->seqid, cs_slot->sl_seqid, 0);
2298 if (status) {
2299 /* an unconfirmed replay returns misordered */
2300 status = nfserr_seq_misordered;
2301 goto out_free_conn;
2302 }
2303 old = find_confirmed_client_by_name(&unconf->cl_name, nn);
2304 if (old) {
2305 status = mark_client_expired(old);
2306 if (status)
2307 goto out_free_conn;
2308 expire_client(old);
2309 }
2310 move_to_confirmed(unconf);
2311 conf = unconf;
2312 } else {
2313 status = nfserr_stale_clientid;
2314 goto out_free_conn;
2315 }
2316 status = nfs_ok;
2317 /*
2318 * We do not support RDMA or persistent sessions
2319 */
2320 cr_ses->flags &= ~SESSION4_PERSIST;
2321 cr_ses->flags &= ~SESSION4_RDMA;
2322
2323 init_session(rqstp, new, conf, cr_ses);
2324 nfsd4_init_conn(rqstp, conn, new);
2325
2326 memcpy(cr_ses->sessionid.data, new->se_sessionid.data,
2327 NFS4_MAX_SESSIONID_LEN);
2328 cs_slot->sl_seqid++;
2329 cr_ses->seqid = cs_slot->sl_seqid;
2330
2331 /* cache solo and embedded create sessions under the state lock */
2332 nfsd4_cache_create_session(cr_ses, cs_slot, status);
2333 nfs4_unlock_state();
2334 return status;
2335 out_free_conn:
2336 nfs4_unlock_state();
2337 free_conn(conn);
2338 out_free_session:
2339 __free_session(new);
2340 out_release_drc_mem:
2341 nfsd4_put_drc_mem(&cr_ses->fore_channel);
2342 return status;
2343 }
2344
2345 static __be32 nfsd4_map_bcts_dir(u32 *dir)
2346 {
2347 switch (*dir) {
2348 case NFS4_CDFC4_FORE:
2349 case NFS4_CDFC4_BACK:
2350 return nfs_ok;
2351 case NFS4_CDFC4_FORE_OR_BOTH:
2352 case NFS4_CDFC4_BACK_OR_BOTH:
2353 *dir = NFS4_CDFC4_BOTH;
2354 return nfs_ok;
2355 };
2356 return nfserr_inval;
2357 }
2358
2359 __be32 nfsd4_backchannel_ctl(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate, struct nfsd4_backchannel_ctl *bc)
2360 {
2361 struct nfsd4_session *session = cstate->session;
2362 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
2363 __be32 status;
2364
2365 status = nfsd4_check_cb_sec(&bc->bc_cb_sec);
2366 if (status)
2367 return status;
2368 spin_lock(&nn->client_lock);
2369 session->se_cb_prog = bc->bc_cb_program;
2370 session->se_cb_sec = bc->bc_cb_sec;
2371 spin_unlock(&nn->client_lock);
2372
2373 nfsd4_probe_callback(session->se_client);
2374
2375 return nfs_ok;
2376 }
2377
2378 __be32 nfsd4_bind_conn_to_session(struct svc_rqst *rqstp,
2379 struct nfsd4_compound_state *cstate,
2380 struct nfsd4_bind_conn_to_session *bcts)
2381 {
2382 __be32 status;
2383 struct nfsd4_conn *conn;
2384 struct nfsd4_session *session;
2385 struct net *net = SVC_NET(rqstp);
2386 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
2387
2388 if (!nfsd4_last_compound_op(rqstp))
2389 return nfserr_not_only_op;
2390 nfs4_lock_state();
2391 spin_lock(&nn->client_lock);
2392 session = find_in_sessionid_hashtbl(&bcts->sessionid, net, &status);
2393 spin_unlock(&nn->client_lock);
2394 if (!session)
2395 goto out_no_session;
2396 status = nfserr_wrong_cred;
2397 if (!mach_creds_match(session->se_client, rqstp))
2398 goto out;
2399 status = nfsd4_map_bcts_dir(&bcts->dir);
2400 if (status)
2401 goto out;
2402 conn = alloc_conn(rqstp, bcts->dir);
2403 status = nfserr_jukebox;
2404 if (!conn)
2405 goto out;
2406 nfsd4_init_conn(rqstp, conn, session);
2407 status = nfs_ok;
2408 out:
2409 nfsd4_put_session(session);
2410 out_no_session:
2411 nfs4_unlock_state();
2412 return status;
2413 }
2414
2415 static bool nfsd4_compound_in_session(struct nfsd4_session *session, struct nfs4_sessionid *sid)
2416 {
2417 if (!session)
2418 return 0;
2419 return !memcmp(sid, &session->se_sessionid, sizeof(*sid));
2420 }
2421
2422 __be32
2423 nfsd4_destroy_session(struct svc_rqst *r,
2424 struct nfsd4_compound_state *cstate,
2425 struct nfsd4_destroy_session *sessionid)
2426 {
2427 struct nfsd4_session *ses;
2428 __be32 status;
2429 int ref_held_by_me = 0;
2430 struct net *net = SVC_NET(r);
2431 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
2432
2433 nfs4_lock_state();
2434 status = nfserr_not_only_op;
2435 if (nfsd4_compound_in_session(cstate->session, &sessionid->sessionid)) {
2436 if (!nfsd4_last_compound_op(r))
2437 goto out;
2438 ref_held_by_me++;
2439 }
2440 dump_sessionid(__func__, &sessionid->sessionid);
2441 spin_lock(&nn->client_lock);
2442 ses = find_in_sessionid_hashtbl(&sessionid->sessionid, net, &status);
2443 if (!ses)
2444 goto out_client_lock;
2445 status = nfserr_wrong_cred;
2446 if (!mach_creds_match(ses->se_client, r))
2447 goto out_put_session;
2448 status = mark_session_dead_locked(ses, 1 + ref_held_by_me);
2449 if (status)
2450 goto out_put_session;
2451 unhash_session(ses);
2452 spin_unlock(&nn->client_lock);
2453
2454 nfsd4_probe_callback_sync(ses->se_client);
2455
2456 spin_lock(&nn->client_lock);
2457 status = nfs_ok;
2458 out_put_session:
2459 nfsd4_put_session_locked(ses);
2460 out_client_lock:
2461 spin_unlock(&nn->client_lock);
2462 out:
2463 nfs4_unlock_state();
2464 return status;
2465 }
2466
2467 static struct nfsd4_conn *__nfsd4_find_conn(struct svc_xprt *xpt, struct nfsd4_session *s)
2468 {
2469 struct nfsd4_conn *c;
2470
2471 list_for_each_entry(c, &s->se_conns, cn_persession) {
2472 if (c->cn_xprt == xpt) {
2473 return c;
2474 }
2475 }
2476 return NULL;
2477 }
2478
2479 static __be32 nfsd4_sequence_check_conn(struct nfsd4_conn *new, struct nfsd4_session *ses)
2480 {
2481 struct nfs4_client *clp = ses->se_client;
2482 struct nfsd4_conn *c;
2483 __be32 status = nfs_ok;
2484 int ret;
2485
2486 spin_lock(&clp->cl_lock);
2487 c = __nfsd4_find_conn(new->cn_xprt, ses);
2488 if (c)
2489 goto out_free;
2490 status = nfserr_conn_not_bound_to_session;
2491 if (clp->cl_mach_cred)
2492 goto out_free;
2493 __nfsd4_hash_conn(new, ses);
2494 spin_unlock(&clp->cl_lock);
2495 ret = nfsd4_register_conn(new);
2496 if (ret)
2497 /* oops; xprt is already down: */
2498 nfsd4_conn_lost(&new->cn_xpt_user);
2499 return nfs_ok;
2500 out_free:
2501 spin_unlock(&clp->cl_lock);
2502 free_conn(new);
2503 return status;
2504 }
2505
2506 static bool nfsd4_session_too_many_ops(struct svc_rqst *rqstp, struct nfsd4_session *session)
2507 {
2508 struct nfsd4_compoundargs *args = rqstp->rq_argp;
2509
2510 return args->opcnt > session->se_fchannel.maxops;
2511 }
2512
2513 static bool nfsd4_request_too_big(struct svc_rqst *rqstp,
2514 struct nfsd4_session *session)
2515 {
2516 struct xdr_buf *xb = &rqstp->rq_arg;
2517
2518 return xb->len > session->se_fchannel.maxreq_sz;
2519 }
2520
2521 __be32
2522 nfsd4_sequence(struct svc_rqst *rqstp,
2523 struct nfsd4_compound_state *cstate,
2524 struct nfsd4_sequence *seq)
2525 {
2526 struct nfsd4_compoundres *resp = rqstp->rq_resp;
2527 struct xdr_stream *xdr = &resp->xdr;
2528 struct nfsd4_session *session;
2529 struct nfs4_client *clp;
2530 struct nfsd4_slot *slot;
2531 struct nfsd4_conn *conn;
2532 __be32 status;
2533 int buflen;
2534 struct net *net = SVC_NET(rqstp);
2535 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
2536
2537 if (resp->opcnt != 1)
2538 return nfserr_sequence_pos;
2539
2540 /*
2541 * Will be either used or freed by nfsd4_sequence_check_conn
2542 * below.
2543 */
2544 conn = alloc_conn(rqstp, NFS4_CDFC4_FORE);
2545 if (!conn)
2546 return nfserr_jukebox;
2547
2548 spin_lock(&nn->client_lock);
2549 session = find_in_sessionid_hashtbl(&seq->sessionid, net, &status);
2550 if (!session)
2551 goto out_no_session;
2552 clp = session->se_client;
2553
2554 status = nfserr_too_many_ops;
2555 if (nfsd4_session_too_many_ops(rqstp, session))
2556 goto out_put_session;
2557
2558 status = nfserr_req_too_big;
2559 if (nfsd4_request_too_big(rqstp, session))
2560 goto out_put_session;
2561
2562 status = nfserr_badslot;
2563 if (seq->slotid >= session->se_fchannel.maxreqs)
2564 goto out_put_session;
2565
2566 slot = session->se_slots[seq->slotid];
2567 dprintk("%s: slotid %d\n", __func__, seq->slotid);
2568
2569 /* We do not negotiate the number of slots yet, so set the
2570 * maxslots to the session maxreqs which is used to encode
2571 * sr_highest_slotid and the sr_target_slot id to maxslots */
2572 seq->maxslots = session->se_fchannel.maxreqs;
2573
2574 status = check_slot_seqid(seq->seqid, slot->sl_seqid,
2575 slot->sl_flags & NFSD4_SLOT_INUSE);
2576 if (status == nfserr_replay_cache) {
2577 status = nfserr_seq_misordered;
2578 if (!(slot->sl_flags & NFSD4_SLOT_INITIALIZED))
2579 goto out_put_session;
2580 cstate->slot = slot;
2581 cstate->session = session;
2582 cstate->clp = clp;
2583 /* Return the cached reply status and set cstate->status
2584 * for nfsd4_proc_compound processing */
2585 status = nfsd4_replay_cache_entry(resp, seq);
2586 cstate->status = nfserr_replay_cache;
2587 goto out;
2588 }
2589 if (status)
2590 goto out_put_session;
2591
2592 status = nfsd4_sequence_check_conn(conn, session);
2593 conn = NULL;
2594 if (status)
2595 goto out_put_session;
2596
2597 buflen = (seq->cachethis) ?
2598 session->se_fchannel.maxresp_cached :
2599 session->se_fchannel.maxresp_sz;
2600 status = (seq->cachethis) ? nfserr_rep_too_big_to_cache :
2601 nfserr_rep_too_big;
2602 if (xdr_restrict_buflen(xdr, buflen - rqstp->rq_auth_slack))
2603 goto out_put_session;
2604 svc_reserve(rqstp, buflen);
2605
2606 status = nfs_ok;
2607 /* Success! bump slot seqid */
2608 slot->sl_seqid = seq->seqid;
2609 slot->sl_flags |= NFSD4_SLOT_INUSE;
2610 if (seq->cachethis)
2611 slot->sl_flags |= NFSD4_SLOT_CACHETHIS;
2612 else
2613 slot->sl_flags &= ~NFSD4_SLOT_CACHETHIS;
2614
2615 cstate->slot = slot;
2616 cstate->session = session;
2617 cstate->clp = clp;
2618
2619 out:
2620 switch (clp->cl_cb_state) {
2621 case NFSD4_CB_DOWN:
2622 seq->status_flags = SEQ4_STATUS_CB_PATH_DOWN;
2623 break;
2624 case NFSD4_CB_FAULT:
2625 seq->status_flags = SEQ4_STATUS_BACKCHANNEL_FAULT;
2626 break;
2627 default:
2628 seq->status_flags = 0;
2629 }
2630 if (!list_empty(&clp->cl_revoked))
2631 seq->status_flags |= SEQ4_STATUS_RECALLABLE_STATE_REVOKED;
2632 out_no_session:
2633 if (conn)
2634 free_conn(conn);
2635 spin_unlock(&nn->client_lock);
2636 return status;
2637 out_put_session:
2638 nfsd4_put_session_locked(session);
2639 goto out_no_session;
2640 }
2641
2642 void
2643 nfsd4_sequence_done(struct nfsd4_compoundres *resp)
2644 {
2645 struct nfsd4_compound_state *cs = &resp->cstate;
2646
2647 if (nfsd4_has_session(cs)) {
2648 if (cs->status != nfserr_replay_cache) {
2649 nfsd4_store_cache_entry(resp);
2650 cs->slot->sl_flags &= ~NFSD4_SLOT_INUSE;
2651 }
2652 /* Drop session reference that was taken in nfsd4_sequence() */
2653 nfsd4_put_session(cs->session);
2654 } else if (cs->clp)
2655 put_client_renew(cs->clp);
2656 }
2657
2658 __be32
2659 nfsd4_destroy_clientid(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate, struct nfsd4_destroy_clientid *dc)
2660 {
2661 struct nfs4_client *conf, *unconf, *clp;
2662 __be32 status = 0;
2663 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
2664
2665 nfs4_lock_state();
2666 unconf = find_unconfirmed_client(&dc->clientid, true, nn);
2667 conf = find_confirmed_client(&dc->clientid, true, nn);
2668 WARN_ON_ONCE(conf && unconf);
2669
2670 if (conf) {
2671 clp = conf;
2672
2673 if (client_has_state(conf)) {
2674 status = nfserr_clientid_busy;
2675 goto out;
2676 }
2677 } else if (unconf)
2678 clp = unconf;
2679 else {
2680 status = nfserr_stale_clientid;
2681 goto out;
2682 }
2683 if (!mach_creds_match(clp, rqstp)) {
2684 status = nfserr_wrong_cred;
2685 goto out;
2686 }
2687 expire_client(clp);
2688 out:
2689 nfs4_unlock_state();
2690 return status;
2691 }
2692
2693 __be32
2694 nfsd4_reclaim_complete(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate, struct nfsd4_reclaim_complete *rc)
2695 {
2696 __be32 status = 0;
2697
2698 if (rc->rca_one_fs) {
2699 if (!cstate->current_fh.fh_dentry)
2700 return nfserr_nofilehandle;
2701 /*
2702 * We don't take advantage of the rca_one_fs case.
2703 * That's OK, it's optional, we can safely ignore it.
2704 */
2705 return nfs_ok;
2706 }
2707
2708 nfs4_lock_state();
2709 status = nfserr_complete_already;
2710 if (test_and_set_bit(NFSD4_CLIENT_RECLAIM_COMPLETE,
2711 &cstate->session->se_client->cl_flags))
2712 goto out;
2713
2714 status = nfserr_stale_clientid;
2715 if (is_client_expired(cstate->session->se_client))
2716 /*
2717 * The following error isn't really legal.
2718 * But we only get here if the client just explicitly
2719 * destroyed the client. Surely it no longer cares what
2720 * error it gets back on an operation for the dead
2721 * client.
2722 */
2723 goto out;
2724
2725 status = nfs_ok;
2726 nfsd4_client_record_create(cstate->session->se_client);
2727 out:
2728 nfs4_unlock_state();
2729 return status;
2730 }
2731
2732 __be32
2733 nfsd4_setclientid(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
2734 struct nfsd4_setclientid *setclid)
2735 {
2736 struct xdr_netobj clname = setclid->se_name;
2737 nfs4_verifier clverifier = setclid->se_verf;
2738 struct nfs4_client *conf, *unconf, *new;
2739 __be32 status;
2740 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
2741
2742 /* Cases below refer to rfc 3530 section 14.2.33: */
2743 nfs4_lock_state();
2744 conf = find_confirmed_client_by_name(&clname, nn);
2745 if (conf) {
2746 /* case 0: */
2747 status = nfserr_clid_inuse;
2748 if (clp_used_exchangeid(conf))
2749 goto out;
2750 if (!same_creds(&conf->cl_cred, &rqstp->rq_cred)) {
2751 char addr_str[INET6_ADDRSTRLEN];
2752 rpc_ntop((struct sockaddr *) &conf->cl_addr, addr_str,
2753 sizeof(addr_str));
2754 dprintk("NFSD: setclientid: string in use by client "
2755 "at %s\n", addr_str);
2756 goto out;
2757 }
2758 }
2759 unconf = find_unconfirmed_client_by_name(&clname, nn);
2760 if (unconf)
2761 expire_client(unconf);
2762 status = nfserr_jukebox;
2763 new = create_client(clname, rqstp, &clverifier);
2764 if (new == NULL)
2765 goto out;
2766 if (conf && same_verf(&conf->cl_verifier, &clverifier))
2767 /* case 1: probable callback update */
2768 copy_clid(new, conf);
2769 else /* case 4 (new client) or cases 2, 3 (client reboot): */
2770 gen_clid(new, nn);
2771 new->cl_minorversion = 0;
2772 gen_callback(new, setclid, rqstp);
2773 add_to_unconfirmed(new);
2774 setclid->se_clientid.cl_boot = new->cl_clientid.cl_boot;
2775 setclid->se_clientid.cl_id = new->cl_clientid.cl_id;
2776 memcpy(setclid->se_confirm.data, new->cl_confirm.data, sizeof(setclid->se_confirm.data));
2777 status = nfs_ok;
2778 out:
2779 nfs4_unlock_state();
2780 return status;
2781 }
2782
2783
2784 __be32
2785 nfsd4_setclientid_confirm(struct svc_rqst *rqstp,
2786 struct nfsd4_compound_state *cstate,
2787 struct nfsd4_setclientid_confirm *setclientid_confirm)
2788 {
2789 struct nfs4_client *conf, *unconf;
2790 nfs4_verifier confirm = setclientid_confirm->sc_confirm;
2791 clientid_t * clid = &setclientid_confirm->sc_clientid;
2792 __be32 status;
2793 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
2794
2795 if (STALE_CLIENTID(clid, nn))
2796 return nfserr_stale_clientid;
2797 nfs4_lock_state();
2798
2799 conf = find_confirmed_client(clid, false, nn);
2800 unconf = find_unconfirmed_client(clid, false, nn);
2801 /*
2802 * We try hard to give out unique clientid's, so if we get an
2803 * attempt to confirm the same clientid with a different cred,
2804 * there's a bug somewhere. Let's charitably assume it's our
2805 * bug.
2806 */
2807 status = nfserr_serverfault;
2808 if (unconf && !same_creds(&unconf->cl_cred, &rqstp->rq_cred))
2809 goto out;
2810 if (conf && !same_creds(&conf->cl_cred, &rqstp->rq_cred))
2811 goto out;
2812 /* cases below refer to rfc 3530 section 14.2.34: */
2813 if (!unconf || !same_verf(&confirm, &unconf->cl_confirm)) {
2814 if (conf && !unconf) /* case 2: probable retransmit */
2815 status = nfs_ok;
2816 else /* case 4: client hasn't noticed we rebooted yet? */
2817 status = nfserr_stale_clientid;
2818 goto out;
2819 }
2820 status = nfs_ok;
2821 if (conf) { /* case 1: callback update */
2822 nfsd4_change_callback(conf, &unconf->cl_cb_conn);
2823 nfsd4_probe_callback(conf);
2824 expire_client(unconf);
2825 } else { /* case 3: normal case; new or rebooted client */
2826 conf = find_confirmed_client_by_name(&unconf->cl_name, nn);
2827 if (conf) {
2828 status = mark_client_expired(conf);
2829 if (status)
2830 goto out;
2831 expire_client(conf);
2832 }
2833 move_to_confirmed(unconf);
2834 nfsd4_probe_callback(unconf);
2835 }
2836 out:
2837 nfs4_unlock_state();
2838 return status;
2839 }
2840
2841 static struct nfs4_file *nfsd4_alloc_file(void)
2842 {
2843 return kmem_cache_alloc(file_slab, GFP_KERNEL);
2844 }
2845
2846 /* OPEN Share state helper functions */
2847 static void nfsd4_init_file(struct nfs4_file *fp, struct knfsd_fh *fh)
2848 {
2849 unsigned int hashval = file_hashval(fh);
2850
2851 lockdep_assert_held(&state_lock);
2852
2853 atomic_set(&fp->fi_ref, 1);
2854 spin_lock_init(&fp->fi_lock);
2855 INIT_LIST_HEAD(&fp->fi_stateids);
2856 INIT_LIST_HEAD(&fp->fi_delegations);
2857 fh_copy_shallow(&fp->fi_fhandle, fh);
2858 fp->fi_had_conflict = false;
2859 fp->fi_lease = NULL;
2860 fp->fi_share_deny = 0;
2861 memset(fp->fi_fds, 0, sizeof(fp->fi_fds));
2862 memset(fp->fi_access, 0, sizeof(fp->fi_access));
2863 hlist_add_head(&fp->fi_hash, &file_hashtbl[hashval]);
2864 }
2865
2866 void
2867 nfsd4_free_slabs(void)
2868 {
2869 kmem_cache_destroy(openowner_slab);
2870 kmem_cache_destroy(lockowner_slab);
2871 kmem_cache_destroy(file_slab);
2872 kmem_cache_destroy(stateid_slab);
2873 kmem_cache_destroy(deleg_slab);
2874 }
2875
2876 int
2877 nfsd4_init_slabs(void)
2878 {
2879 openowner_slab = kmem_cache_create("nfsd4_openowners",
2880 sizeof(struct nfs4_openowner), 0, 0, NULL);
2881 if (openowner_slab == NULL)
2882 goto out;
2883 lockowner_slab = kmem_cache_create("nfsd4_lockowners",
2884 sizeof(struct nfs4_lockowner), 0, 0, NULL);
2885 if (lockowner_slab == NULL)
2886 goto out_free_openowner_slab;
2887 file_slab = kmem_cache_create("nfsd4_files",
2888 sizeof(struct nfs4_file), 0, 0, NULL);
2889 if (file_slab == NULL)
2890 goto out_free_lockowner_slab;
2891 stateid_slab = kmem_cache_create("nfsd4_stateids",
2892 sizeof(struct nfs4_ol_stateid), 0, 0, NULL);
2893 if (stateid_slab == NULL)
2894 goto out_free_file_slab;
2895 deleg_slab = kmem_cache_create("nfsd4_delegations",
2896 sizeof(struct nfs4_delegation), 0, 0, NULL);
2897 if (deleg_slab == NULL)
2898 goto out_free_stateid_slab;
2899 return 0;
2900
2901 out_free_stateid_slab:
2902 kmem_cache_destroy(stateid_slab);
2903 out_free_file_slab:
2904 kmem_cache_destroy(file_slab);
2905 out_free_lockowner_slab:
2906 kmem_cache_destroy(lockowner_slab);
2907 out_free_openowner_slab:
2908 kmem_cache_destroy(openowner_slab);
2909 out:
2910 dprintk("nfsd4: out of memory while initializing nfsv4\n");
2911 return -ENOMEM;
2912 }
2913
2914 static void init_nfs4_replay(struct nfs4_replay *rp)
2915 {
2916 rp->rp_status = nfserr_serverfault;
2917 rp->rp_buflen = 0;
2918 rp->rp_buf = rp->rp_ibuf;
2919 }
2920
2921 static inline void *alloc_stateowner(struct kmem_cache *slab, struct xdr_netobj *owner, struct nfs4_client *clp)
2922 {
2923 struct nfs4_stateowner *sop;
2924
2925 sop = kmem_cache_alloc(slab, GFP_KERNEL);
2926 if (!sop)
2927 return NULL;
2928
2929 sop->so_owner.data = kmemdup(owner->data, owner->len, GFP_KERNEL);
2930 if (!sop->so_owner.data) {
2931 kmem_cache_free(slab, sop);
2932 return NULL;
2933 }
2934 sop->so_owner.len = owner->len;
2935
2936 INIT_LIST_HEAD(&sop->so_stateids);
2937 sop->so_client = clp;
2938 init_nfs4_replay(&sop->so_replay);
2939 return sop;
2940 }
2941
2942 static void hash_openowner(struct nfs4_openowner *oo, struct nfs4_client *clp, unsigned int strhashval)
2943 {
2944 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
2945
2946 list_add(&oo->oo_owner.so_strhash, &nn->ownerstr_hashtbl[strhashval]);
2947 list_add(&oo->oo_perclient, &clp->cl_openowners);
2948 }
2949
2950 static struct nfs4_openowner *
2951 alloc_init_open_stateowner(unsigned int strhashval, struct nfsd4_open *open,
2952 struct nfsd4_compound_state *cstate)
2953 {
2954 struct nfs4_client *clp = cstate->clp;
2955 struct nfs4_openowner *oo;
2956
2957 oo = alloc_stateowner(openowner_slab, &open->op_owner, clp);
2958 if (!oo)
2959 return NULL;
2960 oo->oo_owner.so_is_open_owner = 1;
2961 oo->oo_owner.so_seqid = open->op_seqid;
2962 oo->oo_flags = NFS4_OO_NEW;
2963 if (nfsd4_has_session(cstate))
2964 oo->oo_flags |= NFS4_OO_CONFIRMED;
2965 oo->oo_time = 0;
2966 oo->oo_last_closed_stid = NULL;
2967 INIT_LIST_HEAD(&oo->oo_close_lru);
2968 hash_openowner(oo, clp, strhashval);
2969 return oo;
2970 }
2971
2972 static void init_open_stateid(struct nfs4_ol_stateid *stp, struct nfs4_file *fp, struct nfsd4_open *open) {
2973 struct nfs4_openowner *oo = open->op_openowner;
2974
2975 stp->st_stid.sc_type = NFS4_OPEN_STID;
2976 INIT_LIST_HEAD(&stp->st_locks);
2977 list_add(&stp->st_perstateowner, &oo->oo_owner.so_stateids);
2978 stp->st_stateowner = &oo->oo_owner;
2979 get_nfs4_file(fp);
2980 stp->st_file = fp;
2981 stp->st_access_bmap = 0;
2982 stp->st_deny_bmap = 0;
2983 stp->st_openstp = NULL;
2984 spin_lock(&fp->fi_lock);
2985 list_add(&stp->st_perfile, &fp->fi_stateids);
2986 spin_unlock(&fp->fi_lock);
2987 }
2988
2989 static void
2990 move_to_close_lru(struct nfs4_openowner *oo, struct net *net)
2991 {
2992 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
2993
2994 dprintk("NFSD: move_to_close_lru nfs4_openowner %p\n", oo);
2995
2996 list_move_tail(&oo->oo_close_lru, &nn->close_lru);
2997 oo->oo_time = get_seconds();
2998 }
2999
3000 static int
3001 same_owner_str(struct nfs4_stateowner *sop, struct xdr_netobj *owner,
3002 clientid_t *clid)
3003 {
3004 return (sop->so_owner.len == owner->len) &&
3005 0 == memcmp(sop->so_owner.data, owner->data, owner->len) &&
3006 (sop->so_client->cl_clientid.cl_id == clid->cl_id);
3007 }
3008
3009 static struct nfs4_openowner *
3010 find_openstateowner_str(unsigned int hashval, struct nfsd4_open *open,
3011 bool sessions, struct nfsd_net *nn)
3012 {
3013 struct nfs4_stateowner *so;
3014 struct nfs4_openowner *oo;
3015 struct nfs4_client *clp;
3016
3017 list_for_each_entry(so, &nn->ownerstr_hashtbl[hashval], so_strhash) {
3018 if (!so->so_is_open_owner)
3019 continue;
3020 if (same_owner_str(so, &open->op_owner, &open->op_clientid)) {
3021 oo = openowner(so);
3022 clp = oo->oo_owner.so_client;
3023 if ((bool)clp->cl_minorversion != sessions)
3024 return NULL;
3025 renew_client(oo->oo_owner.so_client);
3026 return oo;
3027 }
3028 }
3029 return NULL;
3030 }
3031
3032 /* search file_hashtbl[] for file */
3033 static struct nfs4_file *
3034 find_file_locked(struct knfsd_fh *fh)
3035 {
3036 unsigned int hashval = file_hashval(fh);
3037 struct nfs4_file *fp;
3038
3039 lockdep_assert_held(&state_lock);
3040
3041 hlist_for_each_entry(fp, &file_hashtbl[hashval], fi_hash) {
3042 if (nfsd_fh_match(&fp->fi_fhandle, fh)) {
3043 get_nfs4_file(fp);
3044 return fp;
3045 }
3046 }
3047 return NULL;
3048 }
3049
3050 static struct nfs4_file *
3051 find_file(struct knfsd_fh *fh)
3052 {
3053 struct nfs4_file *fp;
3054
3055 spin_lock(&state_lock);
3056 fp = find_file_locked(fh);
3057 spin_unlock(&state_lock);
3058 return fp;
3059 }
3060
3061 static struct nfs4_file *
3062 find_or_add_file(struct nfs4_file *new, struct knfsd_fh *fh)
3063 {
3064 struct nfs4_file *fp;
3065
3066 spin_lock(&state_lock);
3067 fp = find_file_locked(fh);
3068 if (fp == NULL) {
3069 nfsd4_init_file(new, fh);
3070 fp = new;
3071 }
3072 spin_unlock(&state_lock);
3073
3074 return fp;
3075 }
3076
3077 /*
3078 * Called to check deny when READ with all zero stateid or
3079 * WRITE with all zero or all one stateid
3080 */
3081 static __be32
3082 nfs4_share_conflict(struct svc_fh *current_fh, unsigned int deny_type)
3083 {
3084 struct nfs4_file *fp;
3085 __be32 ret = nfs_ok;
3086
3087 fp = find_file(&current_fh->fh_handle);
3088 if (!fp)
3089 return ret;
3090 /* Check for conflicting share reservations */
3091 spin_lock(&fp->fi_lock);
3092 if (fp->fi_share_deny & deny_type)
3093 ret = nfserr_locked;
3094 spin_unlock(&fp->fi_lock);
3095 put_nfs4_file(fp);
3096 return ret;
3097 }
3098
3099 void nfsd4_prepare_cb_recall(struct nfs4_delegation *dp)
3100 {
3101 struct nfs4_client *clp = dp->dl_stid.sc_client;
3102 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
3103
3104 /*
3105 * We can't do this in nfsd_break_deleg_cb because it is
3106 * already holding inode->i_lock
3107 */
3108 spin_lock(&state_lock);
3109 block_delegations(&dp->dl_fh);
3110 /*
3111 * If the dl_time != 0, then we know that it has already been
3112 * queued for a lease break. Don't queue it again.
3113 */
3114 if (dp->dl_time == 0) {
3115 dp->dl_time = get_seconds();
3116 list_add_tail(&dp->dl_recall_lru, &nn->del_recall_lru);
3117 }
3118 spin_unlock(&state_lock);
3119 }
3120
3121 static void nfsd_break_one_deleg(struct nfs4_delegation *dp)
3122 {
3123 /*
3124 * We're assuming the state code never drops its reference
3125 * without first removing the lease. Since we're in this lease
3126 * callback (and since the lease code is serialized by the kernel
3127 * lock) we know the server hasn't removed the lease yet, we know
3128 * it's safe to take a reference.
3129 */
3130 atomic_inc(&dp->dl_stid.sc_count);
3131 nfsd4_cb_recall(dp);
3132 }
3133
3134 /* Called from break_lease() with i_lock held. */
3135 static void nfsd_break_deleg_cb(struct file_lock *fl)
3136 {
3137 struct nfs4_file *fp = (struct nfs4_file *)fl->fl_owner;
3138 struct nfs4_delegation *dp;
3139
3140 if (!fp) {
3141 WARN(1, "(%p)->fl_owner NULL\n", fl);
3142 return;
3143 }
3144 if (fp->fi_had_conflict) {
3145 WARN(1, "duplicate break on %p\n", fp);
3146 return;
3147 }
3148 /*
3149 * We don't want the locks code to timeout the lease for us;
3150 * we'll remove it ourself if a delegation isn't returned
3151 * in time:
3152 */
3153 fl->fl_break_time = 0;
3154
3155 spin_lock(&fp->fi_lock);
3156 fp->fi_had_conflict = true;
3157 /*
3158 * If there are no delegations on the list, then we can't count on this
3159 * lease ever being cleaned up. Set the fl_break_time to jiffies so that
3160 * time_out_leases will do it ASAP. The fact that fi_had_conflict is now
3161 * true should keep any new delegations from being hashed.
3162 */
3163 if (list_empty(&fp->fi_delegations))
3164 fl->fl_break_time = jiffies;
3165 else
3166 list_for_each_entry(dp, &fp->fi_delegations, dl_perfile)
3167 nfsd_break_one_deleg(dp);
3168 spin_unlock(&fp->fi_lock);
3169 }
3170
3171 static
3172 int nfsd_change_deleg_cb(struct file_lock **onlist, int arg)
3173 {
3174 if (arg & F_UNLCK)
3175 return lease_modify(onlist, arg);
3176 else
3177 return -EAGAIN;
3178 }
3179
3180 static const struct lock_manager_operations nfsd_lease_mng_ops = {
3181 .lm_break = nfsd_break_deleg_cb,
3182 .lm_change = nfsd_change_deleg_cb,
3183 };
3184
3185 static __be32 nfsd4_check_seqid(struct nfsd4_compound_state *cstate, struct nfs4_stateowner *so, u32 seqid)
3186 {
3187 if (nfsd4_has_session(cstate))
3188 return nfs_ok;
3189 if (seqid == so->so_seqid - 1)
3190 return nfserr_replay_me;
3191 if (seqid == so->so_seqid)
3192 return nfs_ok;
3193 return nfserr_bad_seqid;
3194 }
3195
3196 static __be32 lookup_clientid(clientid_t *clid,
3197 struct nfsd4_compound_state *cstate,
3198 struct nfsd_net *nn)
3199 {
3200 struct nfs4_client *found;
3201
3202 if (cstate->clp) {
3203 found = cstate->clp;
3204 if (!same_clid(&found->cl_clientid, clid))
3205 return nfserr_stale_clientid;
3206 return nfs_ok;
3207 }
3208
3209 if (STALE_CLIENTID(clid, nn))
3210 return nfserr_stale_clientid;
3211
3212 /*
3213 * For v4.1+ we get the client in the SEQUENCE op. If we don't have one
3214 * cached already then we know this is for is for v4.0 and "sessions"
3215 * will be false.
3216 */
3217 WARN_ON_ONCE(cstate->session);
3218 found = find_confirmed_client(clid, false, nn);
3219 if (!found)
3220 return nfserr_expired;
3221
3222 /* Cache the nfs4_client in cstate! */
3223 cstate->clp = found;
3224 atomic_inc(&found->cl_refcount);
3225 return nfs_ok;
3226 }
3227
3228 __be32
3229 nfsd4_process_open1(struct nfsd4_compound_state *cstate,
3230 struct nfsd4_open *open, struct nfsd_net *nn)
3231 {
3232 clientid_t *clientid = &open->op_clientid;
3233 struct nfs4_client *clp = NULL;
3234 unsigned int strhashval;
3235 struct nfs4_openowner *oo = NULL;
3236 __be32 status;
3237
3238 if (STALE_CLIENTID(&open->op_clientid, nn))
3239 return nfserr_stale_clientid;
3240 /*
3241 * In case we need it later, after we've already created the
3242 * file and don't want to risk a further failure:
3243 */
3244 open->op_file = nfsd4_alloc_file();
3245 if (open->op_file == NULL)
3246 return nfserr_jukebox;
3247
3248 status = lookup_clientid(clientid, cstate, nn);
3249 if (status)
3250 return status;
3251 clp = cstate->clp;
3252
3253 strhashval = ownerstr_hashval(clientid->cl_id, &open->op_owner);
3254 oo = find_openstateowner_str(strhashval, open, cstate->minorversion, nn);
3255 open->op_openowner = oo;
3256 if (!oo) {
3257 goto new_owner;
3258 }
3259 if (!(oo->oo_flags & NFS4_OO_CONFIRMED)) {
3260 /* Replace unconfirmed owners without checking for replay. */
3261 release_openowner(oo);
3262 open->op_openowner = NULL;
3263 goto new_owner;
3264 }
3265 status = nfsd4_check_seqid(cstate, &oo->oo_owner, open->op_seqid);
3266 if (status)
3267 return status;
3268 goto alloc_stateid;
3269 new_owner:
3270 oo = alloc_init_open_stateowner(strhashval, open, cstate);
3271 if (oo == NULL)
3272 return nfserr_jukebox;
3273 open->op_openowner = oo;
3274 alloc_stateid:
3275 open->op_stp = nfs4_alloc_stateid(clp);
3276 if (!open->op_stp)
3277 return nfserr_jukebox;
3278 return nfs_ok;
3279 }
3280
3281 static inline __be32
3282 nfs4_check_delegmode(struct nfs4_delegation *dp, int flags)
3283 {
3284 if ((flags & WR_STATE) && (dp->dl_type == NFS4_OPEN_DELEGATE_READ))
3285 return nfserr_openmode;
3286 else
3287 return nfs_ok;
3288 }
3289
3290 static int share_access_to_flags(u32 share_access)
3291 {
3292 return share_access == NFS4_SHARE_ACCESS_READ ? RD_STATE : WR_STATE;
3293 }
3294
3295 static struct nfs4_delegation *find_deleg_stateid(struct nfs4_client *cl, stateid_t *s)
3296 {
3297 struct nfs4_stid *ret;
3298
3299 ret = find_stateid_by_type(cl, s, NFS4_DELEG_STID);
3300 if (!ret)
3301 return NULL;
3302 return delegstateid(ret);
3303 }
3304
3305 static bool nfsd4_is_deleg_cur(struct nfsd4_open *open)
3306 {
3307 return open->op_claim_type == NFS4_OPEN_CLAIM_DELEGATE_CUR ||
3308 open->op_claim_type == NFS4_OPEN_CLAIM_DELEG_CUR_FH;
3309 }
3310
3311 static __be32
3312 nfs4_check_deleg(struct nfs4_client *cl, struct nfsd4_open *open,
3313 struct nfs4_delegation **dp)
3314 {
3315 int flags;
3316 __be32 status = nfserr_bad_stateid;
3317
3318 *dp = find_deleg_stateid(cl, &open->op_delegate_stateid);
3319 if (*dp == NULL)
3320 goto out;
3321 flags = share_access_to_flags(open->op_share_access);
3322 status = nfs4_check_delegmode(*dp, flags);
3323 if (status)
3324 *dp = NULL;
3325 out:
3326 if (!nfsd4_is_deleg_cur(open))
3327 return nfs_ok;
3328 if (status)
3329 return status;
3330 open->op_openowner->oo_flags |= NFS4_OO_CONFIRMED;
3331 return nfs_ok;
3332 }
3333
3334 static struct nfs4_ol_stateid *
3335 nfsd4_find_existing_open(struct nfs4_file *fp, struct nfsd4_open *open)
3336 {
3337 struct nfs4_ol_stateid *local, *ret = NULL;
3338 struct nfs4_openowner *oo = open->op_openowner;
3339
3340 spin_lock(&fp->fi_lock);
3341 list_for_each_entry(local, &fp->fi_stateids, st_perfile) {
3342 /* ignore lock owners */
3343 if (local->st_stateowner->so_is_open_owner == 0)
3344 continue;
3345 if (local->st_stateowner == &oo->oo_owner) {
3346 ret = local;
3347 break;
3348 }
3349 }
3350 spin_unlock(&fp->fi_lock);
3351 return ret;
3352 }
3353
3354 static inline int nfs4_access_to_access(u32 nfs4_access)
3355 {
3356 int flags = 0;
3357
3358 if (nfs4_access & NFS4_SHARE_ACCESS_READ)
3359 flags |= NFSD_MAY_READ;
3360 if (nfs4_access & NFS4_SHARE_ACCESS_WRITE)
3361 flags |= NFSD_MAY_WRITE;
3362 return flags;
3363 }
3364
3365 static inline __be32
3366 nfsd4_truncate(struct svc_rqst *rqstp, struct svc_fh *fh,
3367 struct nfsd4_open *open)
3368 {
3369 struct iattr iattr = {
3370 .ia_valid = ATTR_SIZE,
3371 .ia_size = 0,
3372 };
3373 if (!open->op_truncate)
3374 return 0;
3375 if (!(open->op_share_access & NFS4_SHARE_ACCESS_WRITE))
3376 return nfserr_inval;
3377 return nfsd_setattr(rqstp, fh, &iattr, 0, (time_t)0);
3378 }
3379
3380 static __be32 nfs4_get_vfs_file(struct svc_rqst *rqstp, struct nfs4_file *fp,
3381 struct svc_fh *cur_fh, struct nfs4_ol_stateid *stp,
3382 struct nfsd4_open *open)
3383 {
3384 struct file *filp = NULL;
3385 __be32 status;
3386 int oflag = nfs4_access_to_omode(open->op_share_access);
3387 int access = nfs4_access_to_access(open->op_share_access);
3388 unsigned char old_access_bmap, old_deny_bmap;
3389
3390 spin_lock(&fp->fi_lock);
3391
3392 /*
3393 * Are we trying to set a deny mode that would conflict with
3394 * current access?
3395 */
3396 status = nfs4_file_check_deny(fp, open->op_share_deny);
3397 if (status != nfs_ok) {
3398 spin_unlock(&fp->fi_lock);
3399 goto out;
3400 }
3401
3402 /* set access to the file */
3403 status = nfs4_file_get_access(fp, open->op_share_access);
3404 if (status != nfs_ok) {
3405 spin_unlock(&fp->fi_lock);
3406 goto out;
3407 }
3408
3409 /* Set access bits in stateid */
3410 old_access_bmap = stp->st_access_bmap;
3411 set_access(open->op_share_access, stp);
3412
3413 /* Set new deny mask */
3414 old_deny_bmap = stp->st_deny_bmap;
3415 set_deny(open->op_share_deny, stp);
3416 fp->fi_share_deny |= (open->op_share_deny & NFS4_SHARE_DENY_BOTH);
3417
3418 if (!fp->fi_fds[oflag]) {
3419 spin_unlock(&fp->fi_lock);
3420 status = nfsd_open(rqstp, cur_fh, S_IFREG, access, &filp);
3421 if (status)
3422 goto out_put_access;
3423 spin_lock(&fp->fi_lock);
3424 if (!fp->fi_fds[oflag]) {
3425 fp->fi_fds[oflag] = filp;
3426 filp = NULL;
3427 }
3428 }
3429 spin_unlock(&fp->fi_lock);
3430 if (filp)
3431 fput(filp);
3432
3433 status = nfsd4_truncate(rqstp, cur_fh, open);
3434 if (status)
3435 goto out_put_access;
3436 out:
3437 return status;
3438 out_put_access:
3439 stp->st_access_bmap = old_access_bmap;
3440 nfs4_file_put_access(fp, open->op_share_access);
3441 reset_union_bmap_deny(bmap_to_share_mode(old_deny_bmap), stp);
3442 goto out;
3443 }
3444
3445 static __be32
3446 nfs4_upgrade_open(struct svc_rqst *rqstp, struct nfs4_file *fp, struct svc_fh *cur_fh, struct nfs4_ol_stateid *stp, struct nfsd4_open *open)
3447 {
3448 __be32 status;
3449 unsigned char old_deny_bmap;
3450
3451 if (!test_access(open->op_share_access, stp))
3452 return nfs4_get_vfs_file(rqstp, fp, cur_fh, stp, open);
3453
3454 /* test and set deny mode */
3455 spin_lock(&fp->fi_lock);
3456 status = nfs4_file_check_deny(fp, open->op_share_deny);
3457 if (status == nfs_ok) {
3458 old_deny_bmap = stp->st_deny_bmap;
3459 set_deny(open->op_share_deny, stp);
3460 fp->fi_share_deny |=
3461 (open->op_share_deny & NFS4_SHARE_DENY_BOTH);
3462 }
3463 spin_unlock(&fp->fi_lock);
3464
3465 if (status != nfs_ok)
3466 return status;
3467
3468 status = nfsd4_truncate(rqstp, cur_fh, open);
3469 if (status != nfs_ok)
3470 reset_union_bmap_deny(old_deny_bmap, stp);
3471 return status;
3472 }
3473
3474 static void
3475 nfs4_set_claim_prev(struct nfsd4_open *open, bool has_session)
3476 {
3477 open->op_openowner->oo_flags |= NFS4_OO_CONFIRMED;
3478 }
3479
3480 /* Should we give out recallable state?: */
3481 static bool nfsd4_cb_channel_good(struct nfs4_client *clp)
3482 {
3483 if (clp->cl_cb_state == NFSD4_CB_UP)
3484 return true;
3485 /*
3486 * In the sessions case, since we don't have to establish a
3487 * separate connection for callbacks, we assume it's OK
3488 * until we hear otherwise:
3489 */
3490 return clp->cl_minorversion && clp->cl_cb_state == NFSD4_CB_UNKNOWN;
3491 }
3492
3493 static struct file_lock *nfs4_alloc_init_lease(struct nfs4_file *fp, int flag)
3494 {
3495 struct file_lock *fl;
3496
3497 fl = locks_alloc_lock();
3498 if (!fl)
3499 return NULL;
3500 locks_init_lock(fl);
3501 fl->fl_lmops = &nfsd_lease_mng_ops;
3502 fl->fl_flags = FL_DELEG;
3503 fl->fl_type = flag == NFS4_OPEN_DELEGATE_READ? F_RDLCK: F_WRLCK;
3504 fl->fl_end = OFFSET_MAX;
3505 fl->fl_owner = (fl_owner_t)fp;
3506 fl->fl_pid = current->tgid;
3507 return fl;
3508 }
3509
3510 static int nfs4_setlease(struct nfs4_delegation *dp)
3511 {
3512 struct nfs4_file *fp = dp->dl_file;
3513 struct file_lock *fl;
3514 struct file *filp;
3515 int status = 0;
3516
3517 fl = nfs4_alloc_init_lease(fp, NFS4_OPEN_DELEGATE_READ);
3518 if (!fl)
3519 return -ENOMEM;
3520 filp = find_readable_file(fp);
3521 if (!filp) {
3522 /* We should always have a readable file here */
3523 WARN_ON_ONCE(1);
3524 return -EBADF;
3525 }
3526 fl->fl_file = filp;
3527 status = vfs_setlease(filp, fl->fl_type, &fl);
3528 if (status) {
3529 locks_free_lock(fl);
3530 goto out_fput;
3531 }
3532 spin_lock(&state_lock);
3533 spin_lock(&fp->fi_lock);
3534 /* Did the lease get broken before we took the lock? */
3535 status = -EAGAIN;
3536 if (fp->fi_had_conflict)
3537 goto out_unlock;
3538 /* Race breaker */
3539 if (fp->fi_lease) {
3540 status = 0;
3541 atomic_inc(&fp->fi_delegees);
3542 hash_delegation_locked(dp, fp);
3543 goto out_unlock;
3544 }
3545 fp->fi_lease = fl;
3546 fp->fi_deleg_file = filp;
3547 atomic_set(&fp->fi_delegees, 1);
3548 hash_delegation_locked(dp, fp);
3549 spin_unlock(&fp->fi_lock);
3550 spin_unlock(&state_lock);
3551 return 0;
3552 out_unlock:
3553 spin_unlock(&fp->fi_lock);
3554 spin_unlock(&state_lock);
3555 out_fput:
3556 fput(filp);
3557 return status;
3558 }
3559
3560 static int nfs4_set_delegation(struct nfs4_delegation *dp, struct nfs4_file *fp)
3561 {
3562 int status = 0;
3563
3564 if (fp->fi_had_conflict)
3565 return -EAGAIN;
3566 get_nfs4_file(fp);
3567 spin_lock(&state_lock);
3568 spin_lock(&fp->fi_lock);
3569 dp->dl_file = fp;
3570 if (!fp->fi_lease) {
3571 spin_unlock(&fp->fi_lock);
3572 spin_unlock(&state_lock);
3573 return nfs4_setlease(dp);
3574 }
3575 atomic_inc(&fp->fi_delegees);
3576 if (fp->fi_had_conflict) {
3577 status = -EAGAIN;
3578 goto out_unlock;
3579 }
3580 hash_delegation_locked(dp, fp);
3581 out_unlock:
3582 spin_unlock(&fp->fi_lock);
3583 spin_unlock(&state_lock);
3584 return status;
3585 }
3586
3587 static void nfsd4_open_deleg_none_ext(struct nfsd4_open *open, int status)
3588 {
3589 open->op_delegate_type = NFS4_OPEN_DELEGATE_NONE_EXT;
3590 if (status == -EAGAIN)
3591 open->op_why_no_deleg = WND4_CONTENTION;
3592 else {
3593 open->op_why_no_deleg = WND4_RESOURCE;
3594 switch (open->op_deleg_want) {
3595 case NFS4_SHARE_WANT_READ_DELEG:
3596 case NFS4_SHARE_WANT_WRITE_DELEG:
3597 case NFS4_SHARE_WANT_ANY_DELEG:
3598 break;
3599 case NFS4_SHARE_WANT_CANCEL:
3600 open->op_why_no_deleg = WND4_CANCELLED;
3601 break;
3602 case NFS4_SHARE_WANT_NO_DELEG:
3603 WARN_ON_ONCE(1);
3604 }
3605 }
3606 }
3607
3608 /*
3609 * Attempt to hand out a delegation.
3610 *
3611 * Note we don't support write delegations, and won't until the vfs has
3612 * proper support for them.
3613 */
3614 static void
3615 nfs4_open_delegation(struct net *net, struct svc_fh *fh,
3616 struct nfsd4_open *open, struct nfs4_ol_stateid *stp)
3617 {
3618 struct nfs4_delegation *dp;
3619 struct nfs4_openowner *oo = container_of(stp->st_stateowner, struct nfs4_openowner, oo_owner);
3620 int cb_up;
3621 int status = 0;
3622
3623 cb_up = nfsd4_cb_channel_good(oo->oo_owner.so_client);
3624 open->op_recall = 0;
3625 switch (open->op_claim_type) {
3626 case NFS4_OPEN_CLAIM_PREVIOUS:
3627 if (!cb_up)
3628 open->op_recall = 1;
3629 if (open->op_delegate_type != NFS4_OPEN_DELEGATE_READ)
3630 goto out_no_deleg;
3631 break;
3632 case NFS4_OPEN_CLAIM_NULL:
3633 case NFS4_OPEN_CLAIM_FH:
3634 /*
3635 * Let's not give out any delegations till everyone's
3636 * had the chance to reclaim theirs....
3637 */
3638 if (locks_in_grace(net))
3639 goto out_no_deleg;
3640 if (!cb_up || !(oo->oo_flags & NFS4_OO_CONFIRMED))
3641 goto out_no_deleg;
3642 /*
3643 * Also, if the file was opened for write or
3644 * create, there's a good chance the client's
3645 * about to write to it, resulting in an
3646 * immediate recall (since we don't support
3647 * write delegations):
3648 */
3649 if (open->op_share_access & NFS4_SHARE_ACCESS_WRITE)
3650 goto out_no_deleg;
3651 if (open->op_create == NFS4_OPEN_CREATE)
3652 goto out_no_deleg;
3653 break;
3654 default:
3655 goto out_no_deleg;
3656 }
3657 dp = alloc_init_deleg(oo->oo_owner.so_client, stp, fh);
3658 if (dp == NULL)
3659 goto out_no_deleg;
3660 status = nfs4_set_delegation(dp, stp->st_file);
3661 if (status)
3662 goto out_free;
3663
3664 memcpy(&open->op_delegate_stateid, &dp->dl_stid.sc_stateid, sizeof(dp->dl_stid.sc_stateid));
3665
3666 dprintk("NFSD: delegation stateid=" STATEID_FMT "\n",
3667 STATEID_VAL(&dp->dl_stid.sc_stateid));
3668 open->op_delegate_type = NFS4_OPEN_DELEGATE_READ;
3669 return;
3670 out_free:
3671 destroy_delegation(dp);
3672 out_no_deleg:
3673 open->op_delegate_type = NFS4_OPEN_DELEGATE_NONE;
3674 if (open->op_claim_type == NFS4_OPEN_CLAIM_PREVIOUS &&
3675 open->op_delegate_type != NFS4_OPEN_DELEGATE_NONE) {
3676 dprintk("NFSD: WARNING: refusing delegation reclaim\n");
3677 open->op_recall = 1;
3678 }
3679
3680 /* 4.1 client asking for a delegation? */
3681 if (open->op_deleg_want)
3682 nfsd4_open_deleg_none_ext(open, status);
3683 return;
3684 }
3685
3686 static void nfsd4_deleg_xgrade_none_ext(struct nfsd4_open *open,
3687 struct nfs4_delegation *dp)
3688 {
3689 if (open->op_deleg_want == NFS4_SHARE_WANT_READ_DELEG &&
3690 dp->dl_type == NFS4_OPEN_DELEGATE_WRITE) {
3691 open->op_delegate_type = NFS4_OPEN_DELEGATE_NONE_EXT;
3692 open->op_why_no_deleg = WND4_NOT_SUPP_DOWNGRADE;
3693 } else if (open->op_deleg_want == NFS4_SHARE_WANT_WRITE_DELEG &&
3694 dp->dl_type == NFS4_OPEN_DELEGATE_WRITE) {
3695 open->op_delegate_type = NFS4_OPEN_DELEGATE_NONE_EXT;
3696 open->op_why_no_deleg = WND4_NOT_SUPP_UPGRADE;
3697 }
3698 /* Otherwise the client must be confused wanting a delegation
3699 * it already has, therefore we don't return
3700 * NFS4_OPEN_DELEGATE_NONE_EXT and reason.
3701 */
3702 }
3703
3704 /*
3705 * called with nfs4_lock_state() held.
3706 */
3707 __be32
3708 nfsd4_process_open2(struct svc_rqst *rqstp, struct svc_fh *current_fh, struct nfsd4_open *open)
3709 {
3710 struct nfsd4_compoundres *resp = rqstp->rq_resp;
3711 struct nfs4_client *cl = open->op_openowner->oo_owner.so_client;
3712 struct nfs4_file *fp = NULL;
3713 struct nfs4_ol_stateid *stp = NULL;
3714 struct nfs4_delegation *dp = NULL;
3715 __be32 status;
3716
3717 /*
3718 * Lookup file; if found, lookup stateid and check open request,
3719 * and check for delegations in the process of being recalled.
3720 * If not found, create the nfs4_file struct
3721 */
3722 fp = find_or_add_file(open->op_file, &current_fh->fh_handle);
3723 if (fp != open->op_file) {
3724 status = nfs4_check_deleg(cl, open, &dp);
3725 if (status)
3726 goto out;
3727 stp = nfsd4_find_existing_open(fp, open);
3728 } else {
3729 open->op_file = NULL;
3730 status = nfserr_bad_stateid;
3731 if (nfsd4_is_deleg_cur(open))
3732 goto out;
3733 status = nfserr_jukebox;
3734 }
3735
3736 /*
3737 * OPEN the file, or upgrade an existing OPEN.
3738 * If truncate fails, the OPEN fails.
3739 */
3740 if (stp) {
3741 /* Stateid was found, this is an OPEN upgrade */
3742 status = nfs4_upgrade_open(rqstp, fp, current_fh, stp, open);
3743 if (status)
3744 goto out;
3745 } else {
3746 stp = open->op_stp;
3747 open->op_stp = NULL;
3748 init_open_stateid(stp, fp, open);
3749 status = nfs4_get_vfs_file(rqstp, fp, current_fh, stp, open);
3750 if (status) {
3751 release_open_stateid(stp);
3752 goto out;
3753 }
3754 }
3755 update_stateid(&stp->st_stid.sc_stateid);
3756 memcpy(&open->op_stateid, &stp->st_stid.sc_stateid, sizeof(stateid_t));
3757
3758 if (nfsd4_has_session(&resp->cstate)) {
3759 if (open->op_deleg_want & NFS4_SHARE_WANT_NO_DELEG) {
3760 open->op_delegate_type = NFS4_OPEN_DELEGATE_NONE_EXT;
3761 open->op_why_no_deleg = WND4_NOT_WANTED;
3762 goto nodeleg;
3763 }
3764 }
3765
3766 /*
3767 * Attempt to hand out a delegation. No error return, because the
3768 * OPEN succeeds even if we fail.
3769 */
3770 nfs4_open_delegation(SVC_NET(rqstp), current_fh, open, stp);
3771 nodeleg:
3772 status = nfs_ok;
3773
3774 dprintk("%s: stateid=" STATEID_FMT "\n", __func__,
3775 STATEID_VAL(&stp->st_stid.sc_stateid));
3776 out:
3777 /* 4.1 client trying to upgrade/downgrade delegation? */
3778 if (open->op_delegate_type == NFS4_OPEN_DELEGATE_NONE && dp &&
3779 open->op_deleg_want)
3780 nfsd4_deleg_xgrade_none_ext(open, dp);
3781
3782 if (fp)
3783 put_nfs4_file(fp);
3784 if (status == 0 && open->op_claim_type == NFS4_OPEN_CLAIM_PREVIOUS)
3785 nfs4_set_claim_prev(open, nfsd4_has_session(&resp->cstate));
3786 /*
3787 * To finish the open response, we just need to set the rflags.
3788 */
3789 open->op_rflags = NFS4_OPEN_RESULT_LOCKTYPE_POSIX;
3790 if (!(open->op_openowner->oo_flags & NFS4_OO_CONFIRMED) &&
3791 !nfsd4_has_session(&resp->cstate))
3792 open->op_rflags |= NFS4_OPEN_RESULT_CONFIRM;
3793
3794 return status;
3795 }
3796
3797 void nfsd4_cleanup_open_state(struct nfsd4_open *open, __be32 status)
3798 {
3799 if (open->op_openowner) {
3800 struct nfs4_openowner *oo = open->op_openowner;
3801
3802 if (!list_empty(&oo->oo_owner.so_stateids))
3803 list_del_init(&oo->oo_close_lru);
3804 if (oo->oo_flags & NFS4_OO_NEW) {
3805 if (status) {
3806 release_openowner(oo);
3807 open->op_openowner = NULL;
3808 } else
3809 oo->oo_flags &= ~NFS4_OO_NEW;
3810 }
3811 }
3812 if (open->op_file)
3813 nfsd4_free_file(open->op_file);
3814 if (open->op_stp)
3815 free_generic_stateid(open->op_stp);
3816 }
3817
3818 __be32
3819 nfsd4_renew(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
3820 clientid_t *clid)
3821 {
3822 struct nfs4_client *clp;
3823 __be32 status;
3824 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
3825
3826 nfs4_lock_state();
3827 dprintk("process_renew(%08x/%08x): starting\n",
3828 clid->cl_boot, clid->cl_id);
3829 status = lookup_clientid(clid, cstate, nn);
3830 if (status)
3831 goto out;
3832 clp = cstate->clp;
3833 status = nfserr_cb_path_down;
3834 if (!list_empty(&clp->cl_delegations)
3835 && clp->cl_cb_state != NFSD4_CB_UP)
3836 goto out;
3837 status = nfs_ok;
3838 out:
3839 nfs4_unlock_state();
3840 return status;
3841 }
3842
3843 static void
3844 nfsd4_end_grace(struct nfsd_net *nn)
3845 {
3846 /* do nothing if grace period already ended */
3847 if (nn->grace_ended)
3848 return;
3849
3850 dprintk("NFSD: end of grace period\n");
3851 nn->grace_ended = true;
3852 nfsd4_record_grace_done(nn, nn->boot_time);
3853 locks_end_grace(&nn->nfsd4_manager);
3854 /*
3855 * Now that every NFSv4 client has had the chance to recover and
3856 * to see the (possibly new, possibly shorter) lease time, we
3857 * can safely set the next grace time to the current lease time:
3858 */
3859 nn->nfsd4_grace = nn->nfsd4_lease;
3860 }
3861
3862 static time_t
3863 nfs4_laundromat(struct nfsd_net *nn)
3864 {
3865 struct nfs4_client *clp;
3866 struct nfs4_openowner *oo;
3867 struct nfs4_delegation *dp;
3868 struct list_head *pos, *next, reaplist;
3869 time_t cutoff = get_seconds() - nn->nfsd4_lease;
3870 time_t t, new_timeo = nn->nfsd4_lease;
3871
3872 nfs4_lock_state();
3873
3874 dprintk("NFSD: laundromat service - starting\n");
3875 nfsd4_end_grace(nn);
3876 INIT_LIST_HEAD(&reaplist);
3877 spin_lock(&nn->client_lock);
3878 list_for_each_safe(pos, next, &nn->client_lru) {
3879 clp = list_entry(pos, struct nfs4_client, cl_lru);
3880 if (time_after((unsigned long)clp->cl_time, (unsigned long)cutoff)) {
3881 t = clp->cl_time - cutoff;
3882 new_timeo = min(new_timeo, t);
3883 break;
3884 }
3885 if (mark_client_expired_locked(clp)) {
3886 dprintk("NFSD: client in use (clientid %08x)\n",
3887 clp->cl_clientid.cl_id);
3888 continue;
3889 }
3890 list_move(&clp->cl_lru, &reaplist);
3891 }
3892 spin_unlock(&nn->client_lock);
3893 list_for_each_safe(pos, next, &reaplist) {
3894 clp = list_entry(pos, struct nfs4_client, cl_lru);
3895 dprintk("NFSD: purging unused client (clientid %08x)\n",
3896 clp->cl_clientid.cl_id);
3897 expire_client(clp);
3898 }
3899 spin_lock(&state_lock);
3900 list_for_each_safe(pos, next, &nn->del_recall_lru) {
3901 dp = list_entry (pos, struct nfs4_delegation, dl_recall_lru);
3902 if (net_generic(dp->dl_stid.sc_client->net, nfsd_net_id) != nn)
3903 continue;
3904 if (time_after((unsigned long)dp->dl_time, (unsigned long)cutoff)) {
3905 t = dp->dl_time - cutoff;
3906 new_timeo = min(new_timeo, t);
3907 break;
3908 }
3909 list_move(&dp->dl_recall_lru, &reaplist);
3910 }
3911 spin_unlock(&state_lock);
3912 list_for_each_safe(pos, next, &reaplist) {
3913 dp = list_entry (pos, struct nfs4_delegation, dl_recall_lru);
3914 revoke_delegation(dp);
3915 }
3916 list_for_each_safe(pos, next, &nn->close_lru) {
3917 oo = container_of(pos, struct nfs4_openowner, oo_close_lru);
3918 if (time_after((unsigned long)oo->oo_time, (unsigned long)cutoff)) {
3919 t = oo->oo_time - cutoff;
3920 new_timeo = min(new_timeo, t);
3921 break;
3922 }
3923 release_openowner(oo);
3924 }
3925 new_timeo = max_t(time_t, new_timeo, NFSD_LAUNDROMAT_MINTIMEOUT);
3926 nfs4_unlock_state();
3927 return new_timeo;
3928 }
3929
3930 static struct workqueue_struct *laundry_wq;
3931 static void laundromat_main(struct work_struct *);
3932
3933 static void
3934 laundromat_main(struct work_struct *laundry)
3935 {
3936 time_t t;
3937 struct delayed_work *dwork = container_of(laundry, struct delayed_work,
3938 work);
3939 struct nfsd_net *nn = container_of(dwork, struct nfsd_net,
3940 laundromat_work);
3941
3942 t = nfs4_laundromat(nn);
3943 dprintk("NFSD: laundromat_main - sleeping for %ld seconds\n", t);
3944 queue_delayed_work(laundry_wq, &nn->laundromat_work, t*HZ);
3945 }
3946
3947 static inline __be32 nfs4_check_fh(struct svc_fh *fhp, struct nfs4_ol_stateid *stp)
3948 {
3949 if (!nfsd_fh_match(&fhp->fh_handle, &stp->st_file->fi_fhandle))
3950 return nfserr_bad_stateid;
3951 return nfs_ok;
3952 }
3953
3954 static inline int
3955 access_permit_read(struct nfs4_ol_stateid *stp)
3956 {
3957 return test_access(NFS4_SHARE_ACCESS_READ, stp) ||
3958 test_access(NFS4_SHARE_ACCESS_BOTH, stp) ||
3959 test_access(NFS4_SHARE_ACCESS_WRITE, stp);
3960 }
3961
3962 static inline int
3963 access_permit_write(struct nfs4_ol_stateid *stp)
3964 {
3965 return test_access(NFS4_SHARE_ACCESS_WRITE, stp) ||
3966 test_access(NFS4_SHARE_ACCESS_BOTH, stp);
3967 }
3968
3969 static
3970 __be32 nfs4_check_openmode(struct nfs4_ol_stateid *stp, int flags)
3971 {
3972 __be32 status = nfserr_openmode;
3973
3974 /* For lock stateid's, we test the parent open, not the lock: */
3975 if (stp->st_openstp)
3976 stp = stp->st_openstp;
3977 if ((flags & WR_STATE) && !access_permit_write(stp))
3978 goto out;
3979 if ((flags & RD_STATE) && !access_permit_read(stp))
3980 goto out;
3981 status = nfs_ok;
3982 out:
3983 return status;
3984 }
3985
3986 static inline __be32
3987 check_special_stateids(struct net *net, svc_fh *current_fh, stateid_t *stateid, int flags)
3988 {
3989 if (ONE_STATEID(stateid) && (flags & RD_STATE))
3990 return nfs_ok;
3991 else if (locks_in_grace(net)) {
3992 /* Answer in remaining cases depends on existence of
3993 * conflicting state; so we must wait out the grace period. */
3994 return nfserr_grace;
3995 } else if (flags & WR_STATE)
3996 return nfs4_share_conflict(current_fh,
3997 NFS4_SHARE_DENY_WRITE);
3998 else /* (flags & RD_STATE) && ZERO_STATEID(stateid) */
3999 return nfs4_share_conflict(current_fh,
4000 NFS4_SHARE_DENY_READ);
4001 }
4002
4003 /*
4004 * Allow READ/WRITE during grace period on recovered state only for files
4005 * that are not able to provide mandatory locking.
4006 */
4007 static inline int
4008 grace_disallows_io(struct net *net, struct inode *inode)
4009 {
4010 return locks_in_grace(net) && mandatory_lock(inode);
4011 }
4012
4013 /* Returns true iff a is later than b: */
4014 static bool stateid_generation_after(stateid_t *a, stateid_t *b)
4015 {
4016 return (s32)(a->si_generation - b->si_generation) > 0;
4017 }
4018
4019 static __be32 check_stateid_generation(stateid_t *in, stateid_t *ref, bool has_session)
4020 {
4021 /*
4022 * When sessions are used the stateid generation number is ignored
4023 * when it is zero.
4024 */
4025 if (has_session && in->si_generation == 0)
4026 return nfs_ok;
4027
4028 if (in->si_generation == ref->si_generation)
4029 return nfs_ok;
4030
4031 /* If the client sends us a stateid from the future, it's buggy: */
4032 if (stateid_generation_after(in, ref))
4033 return nfserr_bad_stateid;
4034 /*
4035 * However, we could see a stateid from the past, even from a
4036 * non-buggy client. For example, if the client sends a lock
4037 * while some IO is outstanding, the lock may bump si_generation
4038 * while the IO is still in flight. The client could avoid that
4039 * situation by waiting for responses on all the IO requests,
4040 * but better performance may result in retrying IO that
4041 * receives an old_stateid error if requests are rarely
4042 * reordered in flight:
4043 */
4044 return nfserr_old_stateid;
4045 }
4046
4047 static __be32 nfsd4_validate_stateid(struct nfs4_client *cl, stateid_t *stateid)
4048 {
4049 struct nfs4_stid *s;
4050 struct nfs4_ol_stateid *ols;
4051 __be32 status;
4052
4053 if (ZERO_STATEID(stateid) || ONE_STATEID(stateid))
4054 return nfserr_bad_stateid;
4055 /* Client debugging aid. */
4056 if (!same_clid(&stateid->si_opaque.so_clid, &cl->cl_clientid)) {
4057 char addr_str[INET6_ADDRSTRLEN];
4058 rpc_ntop((struct sockaddr *)&cl->cl_addr, addr_str,
4059 sizeof(addr_str));
4060 pr_warn_ratelimited("NFSD: client %s testing state ID "
4061 "with incorrect client ID\n", addr_str);
4062 return nfserr_bad_stateid;
4063 }
4064 s = find_stateid(cl, stateid);
4065 if (!s)
4066 return nfserr_bad_stateid;
4067 status = check_stateid_generation(stateid, &s->sc_stateid, 1);
4068 if (status)
4069 return status;
4070 switch (s->sc_type) {
4071 case NFS4_DELEG_STID:
4072 return nfs_ok;
4073 case NFS4_REVOKED_DELEG_STID:
4074 return nfserr_deleg_revoked;
4075 case NFS4_OPEN_STID:
4076 case NFS4_LOCK_STID:
4077 ols = openlockstateid(s);
4078 if (ols->st_stateowner->so_is_open_owner
4079 && !(openowner(ols->st_stateowner)->oo_flags
4080 & NFS4_OO_CONFIRMED))
4081 return nfserr_bad_stateid;
4082 return nfs_ok;
4083 default:
4084 printk("unknown stateid type %x\n", s->sc_type);
4085 /* Fallthrough */
4086 case NFS4_CLOSED_STID:
4087 case NFS4_CLOSED_DELEG_STID:
4088 return nfserr_bad_stateid;
4089 }
4090 }
4091
4092 static __be32
4093 nfsd4_lookup_stateid(struct nfsd4_compound_state *cstate,
4094 stateid_t *stateid, unsigned char typemask,
4095 struct nfs4_stid **s, struct nfsd_net *nn)
4096 {
4097 __be32 status;
4098
4099 if (ZERO_STATEID(stateid) || ONE_STATEID(stateid))
4100 return nfserr_bad_stateid;
4101 status = lookup_clientid(&stateid->si_opaque.so_clid, cstate, nn);
4102 if (status == nfserr_stale_clientid) {
4103 if (cstate->session)
4104 return nfserr_bad_stateid;
4105 return nfserr_stale_stateid;
4106 }
4107 if (status)
4108 return status;
4109 *s = find_stateid_by_type(cstate->clp, stateid, typemask);
4110 if (!*s)
4111 return nfserr_bad_stateid;
4112 return nfs_ok;
4113 }
4114
4115 /*
4116 * Checks for stateid operations
4117 */
4118 __be32
4119 nfs4_preprocess_stateid_op(struct net *net, struct nfsd4_compound_state *cstate,
4120 stateid_t *stateid, int flags, struct file **filpp)
4121 {
4122 struct nfs4_stid *s;
4123 struct nfs4_ol_stateid *stp = NULL;
4124 struct nfs4_delegation *dp = NULL;
4125 struct svc_fh *current_fh = &cstate->current_fh;
4126 struct inode *ino = current_fh->fh_dentry->d_inode;
4127 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
4128 struct file *file = NULL;
4129 __be32 status;
4130
4131 if (filpp)
4132 *filpp = NULL;
4133
4134 if (grace_disallows_io(net, ino))
4135 return nfserr_grace;
4136
4137 if (ZERO_STATEID(stateid) || ONE_STATEID(stateid))
4138 return check_special_stateids(net, current_fh, stateid, flags);
4139
4140 nfs4_lock_state();
4141
4142 status = nfsd4_lookup_stateid(cstate, stateid,
4143 NFS4_DELEG_STID|NFS4_OPEN_STID|NFS4_LOCK_STID,
4144 &s, nn);
4145 if (status)
4146 goto out;
4147 status = check_stateid_generation(stateid, &s->sc_stateid, nfsd4_has_session(cstate));
4148 if (status)
4149 goto out;
4150 switch (s->sc_type) {
4151 case NFS4_DELEG_STID:
4152 dp = delegstateid(s);
4153 status = nfs4_check_delegmode(dp, flags);
4154 if (status)
4155 goto out;
4156 if (filpp) {
4157 file = dp->dl_file->fi_deleg_file;
4158 if (!file) {
4159 WARN_ON_ONCE(1);
4160 status = nfserr_serverfault;
4161 goto out;
4162 }
4163 get_file(file);
4164 }
4165 break;
4166 case NFS4_OPEN_STID:
4167 case NFS4_LOCK_STID:
4168 stp = openlockstateid(s);
4169 status = nfs4_check_fh(current_fh, stp);
4170 if (status)
4171 goto out;
4172 if (stp->st_stateowner->so_is_open_owner
4173 && !(openowner(stp->st_stateowner)->oo_flags & NFS4_OO_CONFIRMED))
4174 goto out;
4175 status = nfs4_check_openmode(stp, flags);
4176 if (status)
4177 goto out;
4178 if (filpp) {
4179 if (flags & RD_STATE)
4180 file = find_readable_file(stp->st_file);
4181 else
4182 file = find_writeable_file(stp->st_file);
4183 }
4184 break;
4185 default:
4186 status = nfserr_bad_stateid;
4187 goto out;
4188 }
4189 status = nfs_ok;
4190 if (file)
4191 *filpp = file;
4192 out:
4193 nfs4_unlock_state();
4194 return status;
4195 }
4196
4197 static __be32
4198 nfsd4_free_lock_stateid(struct nfs4_ol_stateid *stp)
4199 {
4200 struct nfs4_lockowner *lo = lockowner(stp->st_stateowner);
4201
4202 if (check_for_locks(stp->st_file, lo))
4203 return nfserr_locks_held;
4204 release_lockowner_if_empty(lo);
4205 return nfs_ok;
4206 }
4207
4208 /*
4209 * Test if the stateid is valid
4210 */
4211 __be32
4212 nfsd4_test_stateid(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
4213 struct nfsd4_test_stateid *test_stateid)
4214 {
4215 struct nfsd4_test_stateid_id *stateid;
4216 struct nfs4_client *cl = cstate->session->se_client;
4217
4218 nfs4_lock_state();
4219 list_for_each_entry(stateid, &test_stateid->ts_stateid_list, ts_id_list)
4220 stateid->ts_id_status =
4221 nfsd4_validate_stateid(cl, &stateid->ts_id_stateid);
4222 nfs4_unlock_state();
4223
4224 return nfs_ok;
4225 }
4226
4227 __be32
4228 nfsd4_free_stateid(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
4229 struct nfsd4_free_stateid *free_stateid)
4230 {
4231 stateid_t *stateid = &free_stateid->fr_stateid;
4232 struct nfs4_stid *s;
4233 struct nfs4_delegation *dp;
4234 struct nfs4_client *cl = cstate->session->se_client;
4235 __be32 ret = nfserr_bad_stateid;
4236
4237 nfs4_lock_state();
4238 s = find_stateid(cl, stateid);
4239 if (!s)
4240 goto out;
4241 switch (s->sc_type) {
4242 case NFS4_DELEG_STID:
4243 ret = nfserr_locks_held;
4244 goto out;
4245 case NFS4_OPEN_STID:
4246 case NFS4_LOCK_STID:
4247 ret = check_stateid_generation(stateid, &s->sc_stateid, 1);
4248 if (ret)
4249 goto out;
4250 if (s->sc_type == NFS4_LOCK_STID)
4251 ret = nfsd4_free_lock_stateid(openlockstateid(s));
4252 else
4253 ret = nfserr_locks_held;
4254 break;
4255 case NFS4_REVOKED_DELEG_STID:
4256 dp = delegstateid(s);
4257 destroy_revoked_delegation(dp);
4258 ret = nfs_ok;
4259 break;
4260 default:
4261 ret = nfserr_bad_stateid;
4262 }
4263 out:
4264 nfs4_unlock_state();
4265 return ret;
4266 }
4267
4268 static inline int
4269 setlkflg (int type)
4270 {
4271 return (type == NFS4_READW_LT || type == NFS4_READ_LT) ?
4272 RD_STATE : WR_STATE;
4273 }
4274
4275 static __be32 nfs4_seqid_op_checks(struct nfsd4_compound_state *cstate, stateid_t *stateid, u32 seqid, struct nfs4_ol_stateid *stp)
4276 {
4277 struct svc_fh *current_fh = &cstate->current_fh;
4278 struct nfs4_stateowner *sop = stp->st_stateowner;
4279 __be32 status;
4280
4281 status = nfsd4_check_seqid(cstate, sop, seqid);
4282 if (status)
4283 return status;
4284 if (stp->st_stid.sc_type == NFS4_CLOSED_STID
4285 || stp->st_stid.sc_type == NFS4_REVOKED_DELEG_STID)
4286 /*
4287 * "Closed" stateid's exist *only* to return
4288 * nfserr_replay_me from the previous step, and
4289 * revoked delegations are kept only for free_stateid.
4290 */
4291 return nfserr_bad_stateid;
4292 status = check_stateid_generation(stateid, &stp->st_stid.sc_stateid, nfsd4_has_session(cstate));
4293 if (status)
4294 return status;
4295 return nfs4_check_fh(current_fh, stp);
4296 }
4297
4298 /*
4299 * Checks for sequence id mutating operations.
4300 */
4301 static __be32
4302 nfs4_preprocess_seqid_op(struct nfsd4_compound_state *cstate, u32 seqid,
4303 stateid_t *stateid, char typemask,
4304 struct nfs4_ol_stateid **stpp,
4305 struct nfsd_net *nn)
4306 {
4307 __be32 status;
4308 struct nfs4_stid *s;
4309 struct nfs4_ol_stateid *stp = NULL;
4310
4311 dprintk("NFSD: %s: seqid=%d stateid = " STATEID_FMT "\n", __func__,
4312 seqid, STATEID_VAL(stateid));
4313
4314 *stpp = NULL;
4315 status = nfsd4_lookup_stateid(cstate, stateid, typemask, &s, nn);
4316 if (status)
4317 return status;
4318 stp = openlockstateid(s);
4319 if (!nfsd4_has_session(cstate))
4320 cstate->replay_owner = stp->st_stateowner;
4321
4322 status = nfs4_seqid_op_checks(cstate, stateid, seqid, stp);
4323 if (!status)
4324 *stpp = stp;
4325 return status;
4326 }
4327
4328 static __be32 nfs4_preprocess_confirmed_seqid_op(struct nfsd4_compound_state *cstate, u32 seqid,
4329 stateid_t *stateid, struct nfs4_ol_stateid **stpp, struct nfsd_net *nn)
4330 {
4331 __be32 status;
4332 struct nfs4_openowner *oo;
4333
4334 status = nfs4_preprocess_seqid_op(cstate, seqid, stateid,
4335 NFS4_OPEN_STID, stpp, nn);
4336 if (status)
4337 return status;
4338 oo = openowner((*stpp)->st_stateowner);
4339 if (!(oo->oo_flags & NFS4_OO_CONFIRMED))
4340 return nfserr_bad_stateid;
4341 return nfs_ok;
4342 }
4343
4344 __be32
4345 nfsd4_open_confirm(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
4346 struct nfsd4_open_confirm *oc)
4347 {
4348 __be32 status;
4349 struct nfs4_openowner *oo;
4350 struct nfs4_ol_stateid *stp;
4351 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
4352
4353 dprintk("NFSD: nfsd4_open_confirm on file %pd\n",
4354 cstate->current_fh.fh_dentry);
4355
4356 status = fh_verify(rqstp, &cstate->current_fh, S_IFREG, 0);
4357 if (status)
4358 return status;
4359
4360 nfs4_lock_state();
4361
4362 status = nfs4_preprocess_seqid_op(cstate,
4363 oc->oc_seqid, &oc->oc_req_stateid,
4364 NFS4_OPEN_STID, &stp, nn);
4365 if (status)
4366 goto out;
4367 oo = openowner(stp->st_stateowner);
4368 status = nfserr_bad_stateid;
4369 if (oo->oo_flags & NFS4_OO_CONFIRMED)
4370 goto out;
4371 oo->oo_flags |= NFS4_OO_CONFIRMED;
4372 update_stateid(&stp->st_stid.sc_stateid);
4373 memcpy(&oc->oc_resp_stateid, &stp->st_stid.sc_stateid, sizeof(stateid_t));
4374 dprintk("NFSD: %s: success, seqid=%d stateid=" STATEID_FMT "\n",
4375 __func__, oc->oc_seqid, STATEID_VAL(&stp->st_stid.sc_stateid));
4376
4377 nfsd4_client_record_create(oo->oo_owner.so_client);
4378 status = nfs_ok;
4379 out:
4380 nfsd4_bump_seqid(cstate, status);
4381 if (!cstate->replay_owner)
4382 nfs4_unlock_state();
4383 return status;
4384 }
4385
4386 static inline void nfs4_stateid_downgrade_bit(struct nfs4_ol_stateid *stp, u32 access)
4387 {
4388 if (!test_access(access, stp))
4389 return;
4390 nfs4_file_put_access(stp->st_file, access);
4391 clear_access(access, stp);
4392 }
4393
4394 static inline void nfs4_stateid_downgrade(struct nfs4_ol_stateid *stp, u32 to_access)
4395 {
4396 switch (to_access) {
4397 case NFS4_SHARE_ACCESS_READ:
4398 nfs4_stateid_downgrade_bit(stp, NFS4_SHARE_ACCESS_WRITE);
4399 nfs4_stateid_downgrade_bit(stp, NFS4_SHARE_ACCESS_BOTH);
4400 break;
4401 case NFS4_SHARE_ACCESS_WRITE:
4402 nfs4_stateid_downgrade_bit(stp, NFS4_SHARE_ACCESS_READ);
4403 nfs4_stateid_downgrade_bit(stp, NFS4_SHARE_ACCESS_BOTH);
4404 break;
4405 case NFS4_SHARE_ACCESS_BOTH:
4406 break;
4407 default:
4408 WARN_ON_ONCE(1);
4409 }
4410 }
4411
4412 __be32
4413 nfsd4_open_downgrade(struct svc_rqst *rqstp,
4414 struct nfsd4_compound_state *cstate,
4415 struct nfsd4_open_downgrade *od)
4416 {
4417 __be32 status;
4418 struct nfs4_ol_stateid *stp;
4419 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
4420
4421 dprintk("NFSD: nfsd4_open_downgrade on file %pd\n",
4422 cstate->current_fh.fh_dentry);
4423
4424 /* We don't yet support WANT bits: */
4425 if (od->od_deleg_want)
4426 dprintk("NFSD: %s: od_deleg_want=0x%x ignored\n", __func__,
4427 od->od_deleg_want);
4428
4429 nfs4_lock_state();
4430 status = nfs4_preprocess_confirmed_seqid_op(cstate, od->od_seqid,
4431 &od->od_stateid, &stp, nn);
4432 if (status)
4433 goto out;
4434 status = nfserr_inval;
4435 if (!test_access(od->od_share_access, stp)) {
4436 dprintk("NFSD: access not a subset of current bitmap: 0x%hhx, input access=%08x\n",
4437 stp->st_access_bmap, od->od_share_access);
4438 goto out;
4439 }
4440 if (!test_deny(od->od_share_deny, stp)) {
4441 dprintk("NFSD: deny not a subset of current bitmap: 0x%hhx, input deny=%08x\n",
4442 stp->st_deny_bmap, od->od_share_deny);
4443 goto out;
4444 }
4445 nfs4_stateid_downgrade(stp, od->od_share_access);
4446
4447 reset_union_bmap_deny(od->od_share_deny, stp);
4448
4449 update_stateid(&stp->st_stid.sc_stateid);
4450 memcpy(&od->od_stateid, &stp->st_stid.sc_stateid, sizeof(stateid_t));
4451 status = nfs_ok;
4452 out:
4453 nfsd4_bump_seqid(cstate, status);
4454 if (!cstate->replay_owner)
4455 nfs4_unlock_state();
4456 return status;
4457 }
4458
4459 static void nfsd4_close_open_stateid(struct nfs4_ol_stateid *s)
4460 {
4461 struct nfs4_client *clp = s->st_stid.sc_client;
4462 struct nfs4_openowner *oo = openowner(s->st_stateowner);
4463
4464 s->st_stid.sc_type = NFS4_CLOSED_STID;
4465 unhash_open_stateid(s);
4466
4467 if (clp->cl_minorversion) {
4468 free_generic_stateid(s);
4469 if (list_empty(&oo->oo_owner.so_stateids))
4470 release_openowner(oo);
4471 } else {
4472 oo->oo_last_closed_stid = s;
4473 /*
4474 * In the 4.0 case we need to keep the owners around a
4475 * little while to handle CLOSE replay.
4476 */
4477 if (list_empty(&oo->oo_owner.so_stateids))
4478 move_to_close_lru(oo, clp->net);
4479 }
4480 }
4481
4482 /*
4483 * nfs4_unlock_state() called after encode
4484 */
4485 __be32
4486 nfsd4_close(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
4487 struct nfsd4_close *close)
4488 {
4489 __be32 status;
4490 struct nfs4_ol_stateid *stp;
4491 struct net *net = SVC_NET(rqstp);
4492 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
4493
4494 dprintk("NFSD: nfsd4_close on file %pd\n",
4495 cstate->current_fh.fh_dentry);
4496
4497 nfs4_lock_state();
4498 status = nfs4_preprocess_seqid_op(cstate, close->cl_seqid,
4499 &close->cl_stateid,
4500 NFS4_OPEN_STID|NFS4_CLOSED_STID,
4501 &stp, nn);
4502 nfsd4_bump_seqid(cstate, status);
4503 if (status)
4504 goto out;
4505 update_stateid(&stp->st_stid.sc_stateid);
4506 memcpy(&close->cl_stateid, &stp->st_stid.sc_stateid, sizeof(stateid_t));
4507
4508 nfsd4_close_open_stateid(stp);
4509 out:
4510 if (!cstate->replay_owner)
4511 nfs4_unlock_state();
4512 return status;
4513 }
4514
4515 __be32
4516 nfsd4_delegreturn(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
4517 struct nfsd4_delegreturn *dr)
4518 {
4519 struct nfs4_delegation *dp;
4520 stateid_t *stateid = &dr->dr_stateid;
4521 struct nfs4_stid *s;
4522 __be32 status;
4523 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
4524
4525 if ((status = fh_verify(rqstp, &cstate->current_fh, S_IFREG, 0)))
4526 return status;
4527
4528 nfs4_lock_state();
4529 status = nfsd4_lookup_stateid(cstate, stateid, NFS4_DELEG_STID, &s, nn);
4530 if (status)
4531 goto out;
4532 dp = delegstateid(s);
4533 status = check_stateid_generation(stateid, &dp->dl_stid.sc_stateid, nfsd4_has_session(cstate));
4534 if (status)
4535 goto out;
4536
4537 destroy_delegation(dp);
4538 out:
4539 nfs4_unlock_state();
4540
4541 return status;
4542 }
4543
4544
4545 #define LOFF_OVERFLOW(start, len) ((u64)(len) > ~(u64)(start))
4546
4547 static inline u64
4548 end_offset(u64 start, u64 len)
4549 {
4550 u64 end;
4551
4552 end = start + len;
4553 return end >= start ? end: NFS4_MAX_UINT64;
4554 }
4555
4556 /* last octet in a range */
4557 static inline u64
4558 last_byte_offset(u64 start, u64 len)
4559 {
4560 u64 end;
4561
4562 WARN_ON_ONCE(!len);
4563 end = start + len;
4564 return end > start ? end - 1: NFS4_MAX_UINT64;
4565 }
4566
4567 /*
4568 * TODO: Linux file offsets are _signed_ 64-bit quantities, which means that
4569 * we can't properly handle lock requests that go beyond the (2^63 - 1)-th
4570 * byte, because of sign extension problems. Since NFSv4 calls for 64-bit
4571 * locking, this prevents us from being completely protocol-compliant. The
4572 * real solution to this problem is to start using unsigned file offsets in
4573 * the VFS, but this is a very deep change!
4574 */
4575 static inline void
4576 nfs4_transform_lock_offset(struct file_lock *lock)
4577 {
4578 if (lock->fl_start < 0)
4579 lock->fl_start = OFFSET_MAX;
4580 if (lock->fl_end < 0)
4581 lock->fl_end = OFFSET_MAX;
4582 }
4583
4584 /* Hack!: For now, we're defining this just so we can use a pointer to it
4585 * as a unique cookie to identify our (NFSv4's) posix locks. */
4586 static const struct lock_manager_operations nfsd_posix_mng_ops = {
4587 };
4588
4589 static inline void
4590 nfs4_set_lock_denied(struct file_lock *fl, struct nfsd4_lock_denied *deny)
4591 {
4592 struct nfs4_lockowner *lo;
4593
4594 if (fl->fl_lmops == &nfsd_posix_mng_ops) {
4595 lo = (struct nfs4_lockowner *) fl->fl_owner;
4596 deny->ld_owner.data = kmemdup(lo->lo_owner.so_owner.data,
4597 lo->lo_owner.so_owner.len, GFP_KERNEL);
4598 if (!deny->ld_owner.data)
4599 /* We just don't care that much */
4600 goto nevermind;
4601 deny->ld_owner.len = lo->lo_owner.so_owner.len;
4602 deny->ld_clientid = lo->lo_owner.so_client->cl_clientid;
4603 } else {
4604 nevermind:
4605 deny->ld_owner.len = 0;
4606 deny->ld_owner.data = NULL;
4607 deny->ld_clientid.cl_boot = 0;
4608 deny->ld_clientid.cl_id = 0;
4609 }
4610 deny->ld_start = fl->fl_start;
4611 deny->ld_length = NFS4_MAX_UINT64;
4612 if (fl->fl_end != NFS4_MAX_UINT64)
4613 deny->ld_length = fl->fl_end - fl->fl_start + 1;
4614 deny->ld_type = NFS4_READ_LT;
4615 if (fl->fl_type != F_RDLCK)
4616 deny->ld_type = NFS4_WRITE_LT;
4617 }
4618
4619 static struct nfs4_lockowner *
4620 find_lockowner_str(clientid_t *clid, struct xdr_netobj *owner,
4621 struct nfsd_net *nn)
4622 {
4623 unsigned int strhashval = ownerstr_hashval(clid->cl_id, owner);
4624 struct nfs4_stateowner *so;
4625
4626 list_for_each_entry(so, &nn->ownerstr_hashtbl[strhashval], so_strhash) {
4627 if (so->so_is_open_owner)
4628 continue;
4629 if (!same_owner_str(so, owner, clid))
4630 continue;
4631 return lockowner(so);
4632 }
4633 return NULL;
4634 }
4635
4636 /*
4637 * Alloc a lock owner structure.
4638 * Called in nfsd4_lock - therefore, OPEN and OPEN_CONFIRM (if needed) has
4639 * occurred.
4640 *
4641 * strhashval = ownerstr_hashval
4642 */
4643 static struct nfs4_lockowner *
4644 alloc_init_lock_stateowner(unsigned int strhashval, struct nfs4_client *clp, struct nfs4_ol_stateid *open_stp, struct nfsd4_lock *lock) {
4645 struct nfs4_lockowner *lo;
4646 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
4647
4648 lo = alloc_stateowner(lockowner_slab, &lock->lk_new_owner, clp);
4649 if (!lo)
4650 return NULL;
4651 INIT_LIST_HEAD(&lo->lo_owner.so_stateids);
4652 lo->lo_owner.so_is_open_owner = 0;
4653 /* It is the openowner seqid that will be incremented in encode in the
4654 * case of new lockowners; so increment the lock seqid manually: */
4655 lo->lo_owner.so_seqid = lock->lk_new_lock_seqid + 1;
4656 list_add(&lo->lo_owner.so_strhash, &nn->ownerstr_hashtbl[strhashval]);
4657 return lo;
4658 }
4659
4660 static struct nfs4_ol_stateid *
4661 alloc_init_lock_stateid(struct nfs4_lockowner *lo, struct nfs4_file *fp,
4662 struct inode *inode,
4663 struct nfs4_ol_stateid *open_stp)
4664 {
4665 struct nfs4_ol_stateid *stp;
4666 struct nfs4_client *clp = lo->lo_owner.so_client;
4667
4668 stp = nfs4_alloc_stateid(clp);
4669 if (stp == NULL)
4670 return NULL;
4671 stp->st_stid.sc_type = NFS4_LOCK_STID;
4672 list_add(&stp->st_perstateowner, &lo->lo_owner.so_stateids);
4673 stp->st_stateowner = &lo->lo_owner;
4674 get_nfs4_file(fp);
4675 stp->st_file = fp;
4676 stp->st_access_bmap = 0;
4677 stp->st_deny_bmap = open_stp->st_deny_bmap;
4678 stp->st_openstp = open_stp;
4679 list_add(&stp->st_locks, &open_stp->st_locks);
4680 spin_lock(&fp->fi_lock);
4681 list_add(&stp->st_perfile, &fp->fi_stateids);
4682 spin_unlock(&fp->fi_lock);
4683 return stp;
4684 }
4685
4686 static struct nfs4_ol_stateid *
4687 find_lock_stateid(struct nfs4_lockowner *lo, struct nfs4_file *fp)
4688 {
4689 struct nfs4_ol_stateid *lst;
4690
4691 list_for_each_entry(lst, &lo->lo_owner.so_stateids, st_perstateowner) {
4692 if (lst->st_file == fp)
4693 return lst;
4694 }
4695 return NULL;
4696 }
4697
4698
4699 static int
4700 check_lock_length(u64 offset, u64 length)
4701 {
4702 return ((length == 0) || ((length != NFS4_MAX_UINT64) &&
4703 LOFF_OVERFLOW(offset, length)));
4704 }
4705
4706 static void get_lock_access(struct nfs4_ol_stateid *lock_stp, u32 access)
4707 {
4708 struct nfs4_file *fp = lock_stp->st_file;
4709
4710 lockdep_assert_held(&fp->fi_lock);
4711
4712 if (test_access(access, lock_stp))
4713 return;
4714 __nfs4_file_get_access(fp, access);
4715 set_access(access, lock_stp);
4716 }
4717
4718 static __be32 lookup_or_create_lock_state(struct nfsd4_compound_state *cstate, struct nfs4_ol_stateid *ost, struct nfsd4_lock *lock, struct nfs4_ol_stateid **lst, bool *new)
4719 {
4720 struct nfs4_file *fi = ost->st_file;
4721 struct nfs4_openowner *oo = openowner(ost->st_stateowner);
4722 struct nfs4_client *cl = oo->oo_owner.so_client;
4723 struct inode *inode = cstate->current_fh.fh_dentry->d_inode;
4724 struct nfs4_lockowner *lo;
4725 unsigned int strhashval;
4726 struct nfsd_net *nn = net_generic(cl->net, nfsd_net_id);
4727
4728 lo = find_lockowner_str(&cl->cl_clientid, &lock->v.new.owner, nn);
4729 if (!lo) {
4730 strhashval = ownerstr_hashval(cl->cl_clientid.cl_id,
4731 &lock->v.new.owner);
4732 lo = alloc_init_lock_stateowner(strhashval, cl, ost, lock);
4733 if (lo == NULL)
4734 return nfserr_jukebox;
4735 } else {
4736 /* with an existing lockowner, seqids must be the same */
4737 if (!cstate->minorversion &&
4738 lock->lk_new_lock_seqid != lo->lo_owner.so_seqid)
4739 return nfserr_bad_seqid;
4740 }
4741
4742 *lst = find_lock_stateid(lo, fi);
4743 if (*lst == NULL) {
4744 *lst = alloc_init_lock_stateid(lo, fi, inode, ost);
4745 if (*lst == NULL) {
4746 release_lockowner_if_empty(lo);
4747 return nfserr_jukebox;
4748 }
4749 *new = true;
4750 }
4751 return nfs_ok;
4752 }
4753
4754 /*
4755 * LOCK operation
4756 */
4757 __be32
4758 nfsd4_lock(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
4759 struct nfsd4_lock *lock)
4760 {
4761 struct nfs4_openowner *open_sop = NULL;
4762 struct nfs4_lockowner *lock_sop = NULL;
4763 struct nfs4_ol_stateid *lock_stp;
4764 struct nfs4_file *fp;
4765 struct file *filp = NULL;
4766 struct file_lock *file_lock = NULL;
4767 struct file_lock *conflock = NULL;
4768 __be32 status = 0;
4769 bool new_state = false;
4770 int lkflg;
4771 int err;
4772 struct net *net = SVC_NET(rqstp);
4773 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
4774
4775 dprintk("NFSD: nfsd4_lock: start=%Ld length=%Ld\n",
4776 (long long) lock->lk_offset,
4777 (long long) lock->lk_length);
4778
4779 if (check_lock_length(lock->lk_offset, lock->lk_length))
4780 return nfserr_inval;
4781
4782 if ((status = fh_verify(rqstp, &cstate->current_fh,
4783 S_IFREG, NFSD_MAY_LOCK))) {
4784 dprintk("NFSD: nfsd4_lock: permission denied!\n");
4785 return status;
4786 }
4787
4788 nfs4_lock_state();
4789
4790 if (lock->lk_is_new) {
4791 struct nfs4_ol_stateid *open_stp = NULL;
4792
4793 if (nfsd4_has_session(cstate))
4794 /* See rfc 5661 18.10.3: given clientid is ignored: */
4795 memcpy(&lock->v.new.clientid,
4796 &cstate->session->se_client->cl_clientid,
4797 sizeof(clientid_t));
4798
4799 status = nfserr_stale_clientid;
4800 if (STALE_CLIENTID(&lock->lk_new_clientid, nn))
4801 goto out;
4802
4803 /* validate and update open stateid and open seqid */
4804 status = nfs4_preprocess_confirmed_seqid_op(cstate,
4805 lock->lk_new_open_seqid,
4806 &lock->lk_new_open_stateid,
4807 &open_stp, nn);
4808 if (status)
4809 goto out;
4810 open_sop = openowner(open_stp->st_stateowner);
4811 status = nfserr_bad_stateid;
4812 if (!same_clid(&open_sop->oo_owner.so_client->cl_clientid,
4813 &lock->v.new.clientid))
4814 goto out;
4815 status = lookup_or_create_lock_state(cstate, open_stp, lock,
4816 &lock_stp, &new_state);
4817 } else
4818 status = nfs4_preprocess_seqid_op(cstate,
4819 lock->lk_old_lock_seqid,
4820 &lock->lk_old_lock_stateid,
4821 NFS4_LOCK_STID, &lock_stp, nn);
4822 if (status)
4823 goto out;
4824 lock_sop = lockowner(lock_stp->st_stateowner);
4825
4826 lkflg = setlkflg(lock->lk_type);
4827 status = nfs4_check_openmode(lock_stp, lkflg);
4828 if (status)
4829 goto out;
4830
4831 status = nfserr_grace;
4832 if (locks_in_grace(net) && !lock->lk_reclaim)
4833 goto out;
4834 status = nfserr_no_grace;
4835 if (!locks_in_grace(net) && lock->lk_reclaim)
4836 goto out;
4837
4838 file_lock = locks_alloc_lock();
4839 if (!file_lock) {
4840 dprintk("NFSD: %s: unable to allocate lock!\n", __func__);
4841 status = nfserr_jukebox;
4842 goto out;
4843 }
4844
4845 fp = lock_stp->st_file;
4846 locks_init_lock(file_lock);
4847 switch (lock->lk_type) {
4848 case NFS4_READ_LT:
4849 case NFS4_READW_LT:
4850 spin_lock(&fp->fi_lock);
4851 filp = find_readable_file_locked(fp);
4852 if (filp)
4853 get_lock_access(lock_stp, NFS4_SHARE_ACCESS_READ);
4854 spin_unlock(&fp->fi_lock);
4855 file_lock->fl_type = F_RDLCK;
4856 break;
4857 case NFS4_WRITE_LT:
4858 case NFS4_WRITEW_LT:
4859 spin_lock(&fp->fi_lock);
4860 filp = find_writeable_file_locked(fp);
4861 if (filp)
4862 get_lock_access(lock_stp, NFS4_SHARE_ACCESS_WRITE);
4863 spin_unlock(&fp->fi_lock);
4864 file_lock->fl_type = F_WRLCK;
4865 break;
4866 default:
4867 status = nfserr_inval;
4868 goto out;
4869 }
4870 if (!filp) {
4871 status = nfserr_openmode;
4872 goto out;
4873 }
4874 file_lock->fl_owner = (fl_owner_t)lock_sop;
4875 file_lock->fl_pid = current->tgid;
4876 file_lock->fl_file = filp;
4877 file_lock->fl_flags = FL_POSIX;
4878 file_lock->fl_lmops = &nfsd_posix_mng_ops;
4879 file_lock->fl_start = lock->lk_offset;
4880 file_lock->fl_end = last_byte_offset(lock->lk_offset, lock->lk_length);
4881 nfs4_transform_lock_offset(file_lock);
4882
4883 conflock = locks_alloc_lock();
4884 if (!conflock) {
4885 dprintk("NFSD: %s: unable to allocate lock!\n", __func__);
4886 status = nfserr_jukebox;
4887 goto out;
4888 }
4889
4890 err = vfs_lock_file(filp, F_SETLK, file_lock, conflock);
4891 switch (-err) {
4892 case 0: /* success! */
4893 update_stateid(&lock_stp->st_stid.sc_stateid);
4894 memcpy(&lock->lk_resp_stateid, &lock_stp->st_stid.sc_stateid,
4895 sizeof(stateid_t));
4896 status = 0;
4897 break;
4898 case (EAGAIN): /* conflock holds conflicting lock */
4899 status = nfserr_denied;
4900 dprintk("NFSD: nfsd4_lock: conflicting lock found!\n");
4901 nfs4_set_lock_denied(conflock, &lock->lk_denied);
4902 break;
4903 case (EDEADLK):
4904 status = nfserr_deadlock;
4905 break;
4906 default:
4907 dprintk("NFSD: nfsd4_lock: vfs_lock_file() failed! status %d\n",err);
4908 status = nfserrno(err);
4909 break;
4910 }
4911 out:
4912 if (filp)
4913 fput(filp);
4914 if (status && new_state)
4915 release_lock_stateid(lock_stp);
4916 nfsd4_bump_seqid(cstate, status);
4917 if (!cstate->replay_owner)
4918 nfs4_unlock_state();
4919 if (file_lock)
4920 locks_free_lock(file_lock);
4921 if (conflock)
4922 locks_free_lock(conflock);
4923 return status;
4924 }
4925
4926 /*
4927 * The NFSv4 spec allows a client to do a LOCKT without holding an OPEN,
4928 * so we do a temporary open here just to get an open file to pass to
4929 * vfs_test_lock. (Arguably perhaps test_lock should be done with an
4930 * inode operation.)
4931 */
4932 static __be32 nfsd_test_lock(struct svc_rqst *rqstp, struct svc_fh *fhp, struct file_lock *lock)
4933 {
4934 struct file *file;
4935 __be32 err = nfsd_open(rqstp, fhp, S_IFREG, NFSD_MAY_READ, &file);
4936 if (!err) {
4937 err = nfserrno(vfs_test_lock(file, lock));
4938 nfsd_close(file);
4939 }
4940 return err;
4941 }
4942
4943 /*
4944 * LOCKT operation
4945 */
4946 __be32
4947 nfsd4_lockt(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
4948 struct nfsd4_lockt *lockt)
4949 {
4950 struct file_lock *file_lock = NULL;
4951 struct nfs4_lockowner *lo;
4952 __be32 status;
4953 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
4954
4955 if (locks_in_grace(SVC_NET(rqstp)))
4956 return nfserr_grace;
4957
4958 if (check_lock_length(lockt->lt_offset, lockt->lt_length))
4959 return nfserr_inval;
4960
4961 nfs4_lock_state();
4962
4963 if (!nfsd4_has_session(cstate)) {
4964 status = lookup_clientid(&lockt->lt_clientid, cstate, nn);
4965 if (status)
4966 goto out;
4967 }
4968
4969 if ((status = fh_verify(rqstp, &cstate->current_fh, S_IFREG, 0)))
4970 goto out;
4971
4972 file_lock = locks_alloc_lock();
4973 if (!file_lock) {
4974 dprintk("NFSD: %s: unable to allocate lock!\n", __func__);
4975 status = nfserr_jukebox;
4976 goto out;
4977 }
4978 locks_init_lock(file_lock);
4979 switch (lockt->lt_type) {
4980 case NFS4_READ_LT:
4981 case NFS4_READW_LT:
4982 file_lock->fl_type = F_RDLCK;
4983 break;
4984 case NFS4_WRITE_LT:
4985 case NFS4_WRITEW_LT:
4986 file_lock->fl_type = F_WRLCK;
4987 break;
4988 default:
4989 dprintk("NFSD: nfs4_lockt: bad lock type!\n");
4990 status = nfserr_inval;
4991 goto out;
4992 }
4993
4994 lo = find_lockowner_str(&lockt->lt_clientid, &lockt->lt_owner, nn);
4995 if (lo)
4996 file_lock->fl_owner = (fl_owner_t)lo;
4997 file_lock->fl_pid = current->tgid;
4998 file_lock->fl_flags = FL_POSIX;
4999
5000 file_lock->fl_start = lockt->lt_offset;
5001 file_lock->fl_end = last_byte_offset(lockt->lt_offset, lockt->lt_length);
5002
5003 nfs4_transform_lock_offset(file_lock);
5004
5005 status = nfsd_test_lock(rqstp, &cstate->current_fh, file_lock);
5006 if (status)
5007 goto out;
5008
5009 if (file_lock->fl_type != F_UNLCK) {
5010 status = nfserr_denied;
5011 nfs4_set_lock_denied(file_lock, &lockt->lt_denied);
5012 }
5013 out:
5014 nfs4_unlock_state();
5015 if (file_lock)
5016 locks_free_lock(file_lock);
5017 return status;
5018 }
5019
5020 __be32
5021 nfsd4_locku(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
5022 struct nfsd4_locku *locku)
5023 {
5024 struct nfs4_ol_stateid *stp;
5025 struct file *filp = NULL;
5026 struct file_lock *file_lock = NULL;
5027 __be32 status;
5028 int err;
5029 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
5030
5031 dprintk("NFSD: nfsd4_locku: start=%Ld length=%Ld\n",
5032 (long long) locku->lu_offset,
5033 (long long) locku->lu_length);
5034
5035 if (check_lock_length(locku->lu_offset, locku->lu_length))
5036 return nfserr_inval;
5037
5038 nfs4_lock_state();
5039
5040 status = nfs4_preprocess_seqid_op(cstate, locku->lu_seqid,
5041 &locku->lu_stateid, NFS4_LOCK_STID,
5042 &stp, nn);
5043 if (status)
5044 goto out;
5045 filp = find_any_file(stp->st_file);
5046 if (!filp) {
5047 status = nfserr_lock_range;
5048 goto out;
5049 }
5050 file_lock = locks_alloc_lock();
5051 if (!file_lock) {
5052 dprintk("NFSD: %s: unable to allocate lock!\n", __func__);
5053 status = nfserr_jukebox;
5054 goto fput;
5055 }
5056 locks_init_lock(file_lock);
5057 file_lock->fl_type = F_UNLCK;
5058 file_lock->fl_owner = (fl_owner_t)lockowner(stp->st_stateowner);
5059 file_lock->fl_pid = current->tgid;
5060 file_lock->fl_file = filp;
5061 file_lock->fl_flags = FL_POSIX;
5062 file_lock->fl_lmops = &nfsd_posix_mng_ops;
5063 file_lock->fl_start = locku->lu_offset;
5064
5065 file_lock->fl_end = last_byte_offset(locku->lu_offset,
5066 locku->lu_length);
5067 nfs4_transform_lock_offset(file_lock);
5068
5069 err = vfs_lock_file(filp, F_SETLK, file_lock, NULL);
5070 if (err) {
5071 dprintk("NFSD: nfs4_locku: vfs_lock_file failed!\n");
5072 goto out_nfserr;
5073 }
5074 update_stateid(&stp->st_stid.sc_stateid);
5075 memcpy(&locku->lu_stateid, &stp->st_stid.sc_stateid, sizeof(stateid_t));
5076 fput:
5077 fput(filp);
5078 out:
5079 nfsd4_bump_seqid(cstate, status);
5080 if (!cstate->replay_owner)
5081 nfs4_unlock_state();
5082 if (file_lock)
5083 locks_free_lock(file_lock);
5084 return status;
5085
5086 out_nfserr:
5087 status = nfserrno(err);
5088 goto fput;
5089 }
5090
5091 /*
5092 * returns
5093 * true: locks held by lockowner
5094 * false: no locks held by lockowner
5095 */
5096 static bool
5097 check_for_locks(struct nfs4_file *fp, struct nfs4_lockowner *lowner)
5098 {
5099 struct file_lock **flpp;
5100 int status = false;
5101 struct file *filp = find_any_file(fp);
5102 struct inode *inode;
5103
5104 if (!filp) {
5105 /* Any valid lock stateid should have some sort of access */
5106 WARN_ON_ONCE(1);
5107 return status;
5108 }
5109
5110 inode = file_inode(filp);
5111
5112 spin_lock(&inode->i_lock);
5113 for (flpp = &inode->i_flock; *flpp != NULL; flpp = &(*flpp)->fl_next) {
5114 if ((*flpp)->fl_owner == (fl_owner_t)lowner) {
5115 status = true;
5116 break;
5117 }
5118 }
5119 spin_unlock(&inode->i_lock);
5120 fput(filp);
5121 return status;
5122 }
5123
5124 __be32
5125 nfsd4_release_lockowner(struct svc_rqst *rqstp,
5126 struct nfsd4_compound_state *cstate,
5127 struct nfsd4_release_lockowner *rlockowner)
5128 {
5129 clientid_t *clid = &rlockowner->rl_clientid;
5130 struct nfs4_stateowner *sop = NULL, *tmp;
5131 struct nfs4_lockowner *lo;
5132 struct nfs4_ol_stateid *stp;
5133 struct xdr_netobj *owner = &rlockowner->rl_owner;
5134 unsigned int hashval = ownerstr_hashval(clid->cl_id, owner);
5135 __be32 status;
5136 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
5137
5138 dprintk("nfsd4_release_lockowner clientid: (%08x/%08x):\n",
5139 clid->cl_boot, clid->cl_id);
5140
5141 nfs4_lock_state();
5142
5143 status = lookup_clientid(clid, cstate, nn);
5144 if (status)
5145 goto out;
5146
5147 status = nfserr_locks_held;
5148
5149 /* Find the matching lock stateowner */
5150 list_for_each_entry(tmp, &nn->ownerstr_hashtbl[hashval], so_strhash) {
5151 if (tmp->so_is_open_owner)
5152 continue;
5153 if (same_owner_str(tmp, owner, clid)) {
5154 sop = tmp;
5155 break;
5156 }
5157 }
5158
5159 /* No matching owner found, maybe a replay? Just declare victory... */
5160 if (!sop) {
5161 status = nfs_ok;
5162 goto out;
5163 }
5164
5165 lo = lockowner(sop);
5166 /* see if there are still any locks associated with it */
5167 list_for_each_entry(stp, &sop->so_stateids, st_perstateowner) {
5168 if (check_for_locks(stp->st_file, lo))
5169 goto out;
5170 }
5171
5172 status = nfs_ok;
5173 release_lockowner(lo);
5174 out:
5175 nfs4_unlock_state();
5176 return status;
5177 }
5178
5179 static inline struct nfs4_client_reclaim *
5180 alloc_reclaim(void)
5181 {
5182 return kmalloc(sizeof(struct nfs4_client_reclaim), GFP_KERNEL);
5183 }
5184
5185 bool
5186 nfs4_has_reclaimed_state(const char *name, struct nfsd_net *nn)
5187 {
5188 struct nfs4_client_reclaim *crp;
5189
5190 crp = nfsd4_find_reclaim_client(name, nn);
5191 return (crp && crp->cr_clp);
5192 }
5193
5194 /*
5195 * failure => all reset bets are off, nfserr_no_grace...
5196 */
5197 struct nfs4_client_reclaim *
5198 nfs4_client_to_reclaim(const char *name, struct nfsd_net *nn)
5199 {
5200 unsigned int strhashval;
5201 struct nfs4_client_reclaim *crp;
5202
5203 dprintk("NFSD nfs4_client_to_reclaim NAME: %.*s\n", HEXDIR_LEN, name);
5204 crp = alloc_reclaim();
5205 if (crp) {
5206 strhashval = clientstr_hashval(name);
5207 INIT_LIST_HEAD(&crp->cr_strhash);
5208 list_add(&crp->cr_strhash, &nn->reclaim_str_hashtbl[strhashval]);
5209 memcpy(crp->cr_recdir, name, HEXDIR_LEN);
5210 crp->cr_clp = NULL;
5211 nn->reclaim_str_hashtbl_size++;
5212 }
5213 return crp;
5214 }
5215
5216 void
5217 nfs4_remove_reclaim_record(struct nfs4_client_reclaim *crp, struct nfsd_net *nn)
5218 {
5219 list_del(&crp->cr_strhash);
5220 kfree(crp);
5221 nn->reclaim_str_hashtbl_size--;
5222 }
5223
5224 void
5225 nfs4_release_reclaim(struct nfsd_net *nn)
5226 {
5227 struct nfs4_client_reclaim *crp = NULL;
5228 int i;
5229
5230 for (i = 0; i < CLIENT_HASH_SIZE; i++) {
5231 while (!list_empty(&nn->reclaim_str_hashtbl[i])) {
5232 crp = list_entry(nn->reclaim_str_hashtbl[i].next,
5233 struct nfs4_client_reclaim, cr_strhash);
5234 nfs4_remove_reclaim_record(crp, nn);
5235 }
5236 }
5237 WARN_ON_ONCE(nn->reclaim_str_hashtbl_size);
5238 }
5239
5240 /*
5241 * called from OPEN, CLAIM_PREVIOUS with a new clientid. */
5242 struct nfs4_client_reclaim *
5243 nfsd4_find_reclaim_client(const char *recdir, struct nfsd_net *nn)
5244 {
5245 unsigned int strhashval;
5246 struct nfs4_client_reclaim *crp = NULL;
5247
5248 dprintk("NFSD: nfs4_find_reclaim_client for recdir %s\n", recdir);
5249
5250 strhashval = clientstr_hashval(recdir);
5251 list_for_each_entry(crp, &nn->reclaim_str_hashtbl[strhashval], cr_strhash) {
5252 if (same_name(crp->cr_recdir, recdir)) {
5253 return crp;
5254 }
5255 }
5256 return NULL;
5257 }
5258
5259 /*
5260 * Called from OPEN. Look for clientid in reclaim list.
5261 */
5262 __be32
5263 nfs4_check_open_reclaim(clientid_t *clid,
5264 struct nfsd4_compound_state *cstate,
5265 struct nfsd_net *nn)
5266 {
5267 __be32 status;
5268
5269 /* find clientid in conf_id_hashtbl */
5270 status = lookup_clientid(clid, cstate, nn);
5271 if (status)
5272 return nfserr_reclaim_bad;
5273
5274 if (nfsd4_client_record_check(cstate->clp))
5275 return nfserr_reclaim_bad;
5276
5277 return nfs_ok;
5278 }
5279
5280 #ifdef CONFIG_NFSD_FAULT_INJECTION
5281
5282 u64 nfsd_forget_client(struct nfs4_client *clp, u64 max)
5283 {
5284 if (mark_client_expired(clp))
5285 return 0;
5286 expire_client(clp);
5287 return 1;
5288 }
5289
5290 u64 nfsd_print_client(struct nfs4_client *clp, u64 num)
5291 {
5292 char buf[INET6_ADDRSTRLEN];
5293 rpc_ntop((struct sockaddr *)&clp->cl_addr, buf, sizeof(buf));
5294 printk(KERN_INFO "NFS Client: %s\n", buf);
5295 return 1;
5296 }
5297
5298 static void nfsd_print_count(struct nfs4_client *clp, unsigned int count,
5299 const char *type)
5300 {
5301 char buf[INET6_ADDRSTRLEN];
5302 rpc_ntop((struct sockaddr *)&clp->cl_addr, buf, sizeof(buf));
5303 printk(KERN_INFO "NFS Client: %s has %u %s\n", buf, count, type);
5304 }
5305
5306 static u64 nfsd_foreach_client_lock(struct nfs4_client *clp, u64 max,
5307 void (*func)(struct nfs4_ol_stateid *))
5308 {
5309 struct nfs4_openowner *oop;
5310 struct nfs4_ol_stateid *stp, *st_next;
5311 struct nfs4_ol_stateid *lst, *lst_next;
5312 u64 count = 0;
5313
5314 list_for_each_entry(oop, &clp->cl_openowners, oo_perclient) {
5315 list_for_each_entry_safe(stp, st_next,
5316 &oop->oo_owner.so_stateids, st_perstateowner) {
5317 list_for_each_entry_safe(lst, lst_next,
5318 &stp->st_locks, st_locks) {
5319 if (func)
5320 func(lst);
5321 if (++count == max)
5322 return count;
5323 }
5324 }
5325 }
5326
5327 return count;
5328 }
5329
5330 u64 nfsd_forget_client_locks(struct nfs4_client *clp, u64 max)
5331 {
5332 return nfsd_foreach_client_lock(clp, max, release_lock_stateid);
5333 }
5334
5335 u64 nfsd_print_client_locks(struct nfs4_client *clp, u64 max)
5336 {
5337 u64 count = nfsd_foreach_client_lock(clp, max, NULL);
5338 nfsd_print_count(clp, count, "locked files");
5339 return count;
5340 }
5341
5342 static u64 nfsd_foreach_client_open(struct nfs4_client *clp, u64 max, void (*func)(struct nfs4_openowner *))
5343 {
5344 struct nfs4_openowner *oop, *next;
5345 u64 count = 0;
5346
5347 list_for_each_entry_safe(oop, next, &clp->cl_openowners, oo_perclient) {
5348 if (func)
5349 func(oop);
5350 if (++count == max)
5351 break;
5352 }
5353
5354 return count;
5355 }
5356
5357 u64 nfsd_forget_client_openowners(struct nfs4_client *clp, u64 max)
5358 {
5359 return nfsd_foreach_client_open(clp, max, release_openowner);
5360 }
5361
5362 u64 nfsd_print_client_openowners(struct nfs4_client *clp, u64 max)
5363 {
5364 u64 count = nfsd_foreach_client_open(clp, max, NULL);
5365 nfsd_print_count(clp, count, "open files");
5366 return count;
5367 }
5368
5369 static u64 nfsd_find_all_delegations(struct nfs4_client *clp, u64 max,
5370 struct list_head *victims)
5371 {
5372 struct nfs4_delegation *dp, *next;
5373 u64 count = 0;
5374
5375 lockdep_assert_held(&state_lock);
5376 list_for_each_entry_safe(dp, next, &clp->cl_delegations, dl_perclnt) {
5377 if (victims) {
5378 /*
5379 * It's not safe to mess with delegations that have a
5380 * non-zero dl_time. They might have already been broken
5381 * and could be processed by the laundromat outside of
5382 * the state_lock. Just leave them be.
5383 */
5384 if (dp->dl_time != 0)
5385 continue;
5386
5387 /*
5388 * Increment dl_time to ensure that delegation breaks
5389 * don't monkey with it now that we are.
5390 */
5391 ++dp->dl_time;
5392 list_move(&dp->dl_recall_lru, victims);
5393 }
5394 if (++count == max)
5395 break;
5396 }
5397 return count;
5398 }
5399
5400 u64 nfsd_forget_client_delegations(struct nfs4_client *clp, u64 max)
5401 {
5402 struct nfs4_delegation *dp, *next;
5403 LIST_HEAD(victims);
5404 u64 count;
5405
5406 spin_lock(&state_lock);
5407 count = nfsd_find_all_delegations(clp, max, &victims);
5408 spin_unlock(&state_lock);
5409
5410 list_for_each_entry_safe(dp, next, &victims, dl_recall_lru)
5411 revoke_delegation(dp);
5412
5413 return count;
5414 }
5415
5416 u64 nfsd_recall_client_delegations(struct nfs4_client *clp, u64 max)
5417 {
5418 struct nfs4_delegation *dp;
5419 LIST_HEAD(victims);
5420 u64 count;
5421
5422 spin_lock(&state_lock);
5423 count = nfsd_find_all_delegations(clp, max, &victims);
5424 while (!list_empty(&victims)) {
5425 dp = list_first_entry(&victims, struct nfs4_delegation,
5426 dl_recall_lru);
5427 list_del_init(&dp->dl_recall_lru);
5428 dp->dl_time = 0;
5429 nfsd_break_one_deleg(dp);
5430 }
5431 spin_unlock(&state_lock);
5432
5433 return count;
5434 }
5435
5436 u64 nfsd_print_client_delegations(struct nfs4_client *clp, u64 max)
5437 {
5438 u64 count = 0;
5439
5440 spin_lock(&state_lock);
5441 count = nfsd_find_all_delegations(clp, max, NULL);
5442 spin_unlock(&state_lock);
5443
5444 nfsd_print_count(clp, count, "delegations");
5445 return count;
5446 }
5447
5448 u64 nfsd_for_n_state(u64 max, u64 (*func)(struct nfs4_client *, u64))
5449 {
5450 struct nfs4_client *clp, *next;
5451 u64 count = 0;
5452 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns, nfsd_net_id);
5453
5454 if (!nfsd_netns_ready(nn))
5455 return 0;
5456
5457 list_for_each_entry_safe(clp, next, &nn->client_lru, cl_lru) {
5458 count += func(clp, max - count);
5459 if ((max != 0) && (count >= max))
5460 break;
5461 }
5462
5463 return count;
5464 }
5465
5466 struct nfs4_client *nfsd_find_client(struct sockaddr_storage *addr, size_t addr_size)
5467 {
5468 struct nfs4_client *clp;
5469 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns, nfsd_net_id);
5470
5471 if (!nfsd_netns_ready(nn))
5472 return NULL;
5473
5474 list_for_each_entry(clp, &nn->client_lru, cl_lru) {
5475 if (memcmp(&clp->cl_addr, addr, addr_size) == 0)
5476 return clp;
5477 }
5478 return NULL;
5479 }
5480
5481 #endif /* CONFIG_NFSD_FAULT_INJECTION */
5482
5483 /*
5484 * Since the lifetime of a delegation isn't limited to that of an open, a
5485 * client may quite reasonably hang on to a delegation as long as it has
5486 * the inode cached. This becomes an obvious problem the first time a
5487 * client's inode cache approaches the size of the server's total memory.
5488 *
5489 * For now we avoid this problem by imposing a hard limit on the number
5490 * of delegations, which varies according to the server's memory size.
5491 */
5492 static void
5493 set_max_delegations(void)
5494 {
5495 /*
5496 * Allow at most 4 delegations per megabyte of RAM. Quick
5497 * estimates suggest that in the worst case (where every delegation
5498 * is for a different inode), a delegation could take about 1.5K,
5499 * giving a worst case usage of about 6% of memory.
5500 */
5501 max_delegations = nr_free_buffer_pages() >> (20 - 2 - PAGE_SHIFT);
5502 }
5503
5504 static int nfs4_state_create_net(struct net *net)
5505 {
5506 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
5507 int i;
5508
5509 nn->conf_id_hashtbl = kmalloc(sizeof(struct list_head) *
5510 CLIENT_HASH_SIZE, GFP_KERNEL);
5511 if (!nn->conf_id_hashtbl)
5512 goto err;
5513 nn->unconf_id_hashtbl = kmalloc(sizeof(struct list_head) *
5514 CLIENT_HASH_SIZE, GFP_KERNEL);
5515 if (!nn->unconf_id_hashtbl)
5516 goto err_unconf_id;
5517 nn->ownerstr_hashtbl = kmalloc(sizeof(struct list_head) *
5518 OWNER_HASH_SIZE, GFP_KERNEL);
5519 if (!nn->ownerstr_hashtbl)
5520 goto err_ownerstr;
5521 nn->sessionid_hashtbl = kmalloc(sizeof(struct list_head) *
5522 SESSION_HASH_SIZE, GFP_KERNEL);
5523 if (!nn->sessionid_hashtbl)
5524 goto err_sessionid;
5525
5526 for (i = 0; i < CLIENT_HASH_SIZE; i++) {
5527 INIT_LIST_HEAD(&nn->conf_id_hashtbl[i]);
5528 INIT_LIST_HEAD(&nn->unconf_id_hashtbl[i]);
5529 }
5530 for (i = 0; i < OWNER_HASH_SIZE; i++)
5531 INIT_LIST_HEAD(&nn->ownerstr_hashtbl[i]);
5532 for (i = 0; i < SESSION_HASH_SIZE; i++)
5533 INIT_LIST_HEAD(&nn->sessionid_hashtbl[i]);
5534 nn->conf_name_tree = RB_ROOT;
5535 nn->unconf_name_tree = RB_ROOT;
5536 INIT_LIST_HEAD(&nn->client_lru);
5537 INIT_LIST_HEAD(&nn->close_lru);
5538 INIT_LIST_HEAD(&nn->del_recall_lru);
5539 spin_lock_init(&nn->client_lock);
5540
5541 INIT_DELAYED_WORK(&nn->laundromat_work, laundromat_main);
5542 get_net(net);
5543
5544 return 0;
5545
5546 err_sessionid:
5547 kfree(nn->ownerstr_hashtbl);
5548 err_ownerstr:
5549 kfree(nn->unconf_id_hashtbl);
5550 err_unconf_id:
5551 kfree(nn->conf_id_hashtbl);
5552 err:
5553 return -ENOMEM;
5554 }
5555
5556 static void
5557 nfs4_state_destroy_net(struct net *net)
5558 {
5559 int i;
5560 struct nfs4_client *clp = NULL;
5561 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
5562
5563 for (i = 0; i < CLIENT_HASH_SIZE; i++) {
5564 while (!list_empty(&nn->conf_id_hashtbl[i])) {
5565 clp = list_entry(nn->conf_id_hashtbl[i].next, struct nfs4_client, cl_idhash);
5566 destroy_client(clp);
5567 }
5568 }
5569
5570 for (i = 0; i < CLIENT_HASH_SIZE; i++) {
5571 while (!list_empty(&nn->unconf_id_hashtbl[i])) {
5572 clp = list_entry(nn->unconf_id_hashtbl[i].next, struct nfs4_client, cl_idhash);
5573 destroy_client(clp);
5574 }
5575 }
5576
5577 kfree(nn->sessionid_hashtbl);
5578 kfree(nn->ownerstr_hashtbl);
5579 kfree(nn->unconf_id_hashtbl);
5580 kfree(nn->conf_id_hashtbl);
5581 put_net(net);
5582 }
5583
5584 int
5585 nfs4_state_start_net(struct net *net)
5586 {
5587 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
5588 int ret;
5589
5590 ret = nfs4_state_create_net(net);
5591 if (ret)
5592 return ret;
5593 nfsd4_client_tracking_init(net);
5594 nn->boot_time = get_seconds();
5595 locks_start_grace(net, &nn->nfsd4_manager);
5596 nn->grace_ended = false;
5597 printk(KERN_INFO "NFSD: starting %ld-second grace period (net %p)\n",
5598 nn->nfsd4_grace, net);
5599 queue_delayed_work(laundry_wq, &nn->laundromat_work, nn->nfsd4_grace * HZ);
5600 return 0;
5601 }
5602
5603 /* initialization to perform when the nfsd service is started: */
5604
5605 int
5606 nfs4_state_start(void)
5607 {
5608 int ret;
5609
5610 ret = set_callback_cred();
5611 if (ret)
5612 return -ENOMEM;
5613 laundry_wq = create_singlethread_workqueue("nfsd4");
5614 if (laundry_wq == NULL) {
5615 ret = -ENOMEM;
5616 goto out_recovery;
5617 }
5618 ret = nfsd4_create_callback_queue();
5619 if (ret)
5620 goto out_free_laundry;
5621
5622 set_max_delegations();
5623
5624 return 0;
5625
5626 out_free_laundry:
5627 destroy_workqueue(laundry_wq);
5628 out_recovery:
5629 return ret;
5630 }
5631
5632 void
5633 nfs4_state_shutdown_net(struct net *net)
5634 {
5635 struct nfs4_delegation *dp = NULL;
5636 struct list_head *pos, *next, reaplist;
5637 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
5638
5639 cancel_delayed_work_sync(&nn->laundromat_work);
5640 locks_end_grace(&nn->nfsd4_manager);
5641
5642 nfs4_lock_state();
5643 INIT_LIST_HEAD(&reaplist);
5644 spin_lock(&state_lock);
5645 list_for_each_safe(pos, next, &nn->del_recall_lru) {
5646 dp = list_entry (pos, struct nfs4_delegation, dl_recall_lru);
5647 list_move(&dp->dl_recall_lru, &reaplist);
5648 }
5649 spin_unlock(&state_lock);
5650 list_for_each_safe(pos, next, &reaplist) {
5651 dp = list_entry (pos, struct nfs4_delegation, dl_recall_lru);
5652 destroy_delegation(dp);
5653 }
5654
5655 nfsd4_client_tracking_exit(net);
5656 nfs4_state_destroy_net(net);
5657 nfs4_unlock_state();
5658 }
5659
5660 void
5661 nfs4_state_shutdown(void)
5662 {
5663 destroy_workqueue(laundry_wq);
5664 nfsd4_destroy_callback_queue();
5665 }
5666
5667 static void
5668 get_stateid(struct nfsd4_compound_state *cstate, stateid_t *stateid)
5669 {
5670 if (HAS_STATE_ID(cstate, CURRENT_STATE_ID_FLAG) && CURRENT_STATEID(stateid))
5671 memcpy(stateid, &cstate->current_stateid, sizeof(stateid_t));
5672 }
5673
5674 static void
5675 put_stateid(struct nfsd4_compound_state *cstate, stateid_t *stateid)
5676 {
5677 if (cstate->minorversion) {
5678 memcpy(&cstate->current_stateid, stateid, sizeof(stateid_t));
5679 SET_STATE_ID(cstate, CURRENT_STATE_ID_FLAG);
5680 }
5681 }
5682
5683 void
5684 clear_current_stateid(struct nfsd4_compound_state *cstate)
5685 {
5686 CLEAR_STATE_ID(cstate, CURRENT_STATE_ID_FLAG);
5687 }
5688
5689 /*
5690 * functions to set current state id
5691 */
5692 void
5693 nfsd4_set_opendowngradestateid(struct nfsd4_compound_state *cstate, struct nfsd4_open_downgrade *odp)
5694 {
5695 put_stateid(cstate, &odp->od_stateid);
5696 }
5697
5698 void
5699 nfsd4_set_openstateid(struct nfsd4_compound_state *cstate, struct nfsd4_open *open)
5700 {
5701 put_stateid(cstate, &open->op_stateid);
5702 }
5703
5704 void
5705 nfsd4_set_closestateid(struct nfsd4_compound_state *cstate, struct nfsd4_close *close)
5706 {
5707 put_stateid(cstate, &close->cl_stateid);
5708 }
5709
5710 void
5711 nfsd4_set_lockstateid(struct nfsd4_compound_state *cstate, struct nfsd4_lock *lock)
5712 {
5713 put_stateid(cstate, &lock->lk_resp_stateid);
5714 }
5715
5716 /*
5717 * functions to consume current state id
5718 */
5719
5720 void
5721 nfsd4_get_opendowngradestateid(struct nfsd4_compound_state *cstate, struct nfsd4_open_downgrade *odp)
5722 {
5723 get_stateid(cstate, &odp->od_stateid);
5724 }
5725
5726 void
5727 nfsd4_get_delegreturnstateid(struct nfsd4_compound_state *cstate, struct nfsd4_delegreturn *drp)
5728 {
5729 get_stateid(cstate, &drp->dr_stateid);
5730 }
5731
5732 void
5733 nfsd4_get_freestateid(struct nfsd4_compound_state *cstate, struct nfsd4_free_stateid *fsp)
5734 {
5735 get_stateid(cstate, &fsp->fr_stateid);
5736 }
5737
5738 void
5739 nfsd4_get_setattrstateid(struct nfsd4_compound_state *cstate, struct nfsd4_setattr *setattr)
5740 {
5741 get_stateid(cstate, &setattr->sa_stateid);
5742 }
5743
5744 void
5745 nfsd4_get_closestateid(struct nfsd4_compound_state *cstate, struct nfsd4_close *close)
5746 {
5747 get_stateid(cstate, &close->cl_stateid);
5748 }
5749
5750 void
5751 nfsd4_get_lockustateid(struct nfsd4_compound_state *cstate, struct nfsd4_locku *locku)
5752 {
5753 get_stateid(cstate, &locku->lu_stateid);
5754 }
5755
5756 void
5757 nfsd4_get_readstateid(struct nfsd4_compound_state *cstate, struct nfsd4_read *read)
5758 {
5759 get_stateid(cstate, &read->rd_stateid);
5760 }
5761
5762 void
5763 nfsd4_get_writestateid(struct nfsd4_compound_state *cstate, struct nfsd4_write *write)
5764 {
5765 get_stateid(cstate, &write->wr_stateid);
5766 }