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