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