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