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