2 * Copyright (c) 2001 The Regents of the University of Michigan.
5 * Kendrick Smith <kmsmith@umich.edu>
6 * Andy Adamson <kandros@umich.edu>
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
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.
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.
35 #include <linux/file.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>
48 #include "current_stateid.h"
52 #define NFSDDBG_FACILITY NFSDDBG_PROC
54 #define all_ones {{~0,~0},~0}
55 static const stateid_t one_stateid
= {
57 .si_opaque
= all_ones
,
59 static const stateid_t zero_stateid
= {
62 static const stateid_t currentstateid
= {
66 static u64 current_sessionid
= 1;
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), ¤tstateid, sizeof(stateid_t)))
72 /* forward declarations */
73 static int check_for_locks(struct nfs4_file
*filp
, struct nfs4_lockowner
*lowner
);
77 /* Currently used for almost all code touching nfsv4 state: */
78 static DEFINE_MUTEX(client_mutex
);
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:
85 static DEFINE_SPINLOCK(state_lock
);
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
;
96 mutex_lock(&client_mutex
);
99 static void free_session(struct nfsd4_session
*);
101 static bool is_session_dead(struct nfsd4_session
*ses
)
103 return ses
->se_flags
& NFS4_SESSION_DEAD
;
106 void nfsd4_put_session(struct nfsd4_session
*ses
)
108 if (atomic_dec_and_test(&ses
->se_ref
) && is_session_dead(ses
))
112 static __be32
mark_session_dead_locked(struct nfsd4_session
*ses
, int ref_held_by_me
)
114 if (atomic_read(&ses
->se_ref
) > ref_held_by_me
)
115 return nfserr_jukebox
;
116 ses
->se_flags
|= NFS4_SESSION_DEAD
;
120 static __be32
nfsd4_get_session_locked(struct nfsd4_session
*ses
)
122 if (is_session_dead(ses
))
123 return nfserr_badsession
;
124 atomic_inc(&ses
->se_ref
);
129 nfs4_unlock_state(void)
131 mutex_unlock(&client_mutex
);
134 static bool is_client_expired(struct nfs4_client
*clp
)
136 return clp
->cl_time
== 0;
139 static __be32
mark_client_expired_locked(struct nfs4_client
*clp
)
141 if (atomic_read(&clp
->cl_refcount
))
142 return nfserr_jukebox
;
147 static __be32
mark_client_expired(struct nfs4_client
*clp
)
149 struct nfsd_net
*nn
= net_generic(clp
->net
, nfsd_net_id
);
152 spin_lock(&nn
->client_lock
);
153 ret
= mark_client_expired_locked(clp
);
154 spin_unlock(&nn
->client_lock
);
158 static __be32
get_client_locked(struct nfs4_client
*clp
)
160 if (is_client_expired(clp
))
161 return nfserr_expired
;
162 atomic_inc(&clp
->cl_refcount
);
166 /* must be called under the client_lock */
168 renew_client_locked(struct nfs4_client
*clp
)
170 struct nfsd_net
*nn
= net_generic(clp
->net
, nfsd_net_id
);
172 if (is_client_expired(clp
)) {
174 printk("%s: client (clientid %08x/%08x) already expired\n",
176 clp
->cl_clientid
.cl_boot
,
177 clp
->cl_clientid
.cl_id
);
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();
189 renew_client(struct nfs4_client
*clp
)
191 struct nfsd_net
*nn
= net_generic(clp
->net
, nfsd_net_id
);
193 spin_lock(&nn
->client_lock
);
194 renew_client_locked(clp
);
195 spin_unlock(&nn
->client_lock
);
198 static void put_client_renew_locked(struct nfs4_client
*clp
)
200 if (!atomic_dec_and_test(&clp
->cl_refcount
))
202 if (!is_client_expired(clp
))
203 renew_client_locked(clp
);
207 opaque_hashval(const void *ptr
, int nbytes
)
209 unsigned char *cptr
= (unsigned char *) ptr
;
219 static void nfsd4_free_file(struct nfs4_file
*f
)
221 kmem_cache_free(file_slab
, f
);
225 put_nfs4_file(struct nfs4_file
*fi
)
227 if (atomic_dec_and_lock(&fi
->fi_ref
, &state_lock
)) {
228 hlist_del(&fi
->fi_hash
);
229 spin_unlock(&state_lock
);
236 get_nfs4_file(struct nfs4_file
*fi
)
238 atomic_inc(&fi
->fi_ref
);
241 static int num_delegations
;
242 unsigned long max_delegations
;
245 * Open owner state (share locks)
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)
253 static unsigned int ownerstr_hashval(u32 clientid
, struct xdr_netobj
*ownername
)
257 ret
= opaque_hashval(ownername
->data
, ownername
->len
);
259 return ret
& OWNER_HASH_MASK
;
262 /* hash table for nfs4_file */
263 #define FILE_HASH_BITS 8
264 #define FILE_HASH_SIZE (1 << FILE_HASH_BITS)
266 static unsigned int file_hashval(struct inode
*ino
)
268 /* XXX: why are we hashing on inode pointer, anyway? */
269 return hash_ptr(ino
, FILE_HASH_BITS
);
272 static struct hlist_head file_hashtbl
[FILE_HASH_SIZE
];
274 static void __nfs4_file_get_access(struct nfs4_file
*fp
, int oflag
)
276 WARN_ON_ONCE(!(fp
->fi_fds
[oflag
] || fp
->fi_fds
[O_RDWR
]));
277 atomic_inc(&fp
->fi_access
[oflag
]);
280 static void nfs4_file_get_access(struct nfs4_file
*fp
, int oflag
)
282 if (oflag
== O_RDWR
) {
283 __nfs4_file_get_access(fp
, O_RDONLY
);
284 __nfs4_file_get_access(fp
, O_WRONLY
);
286 __nfs4_file_get_access(fp
, oflag
);
289 static void nfs4_file_put_fd(struct nfs4_file
*fp
, int oflag
)
291 if (fp
->fi_fds
[oflag
]) {
292 fput(fp
->fi_fds
[oflag
]);
293 fp
->fi_fds
[oflag
] = NULL
;
297 static void __nfs4_file_put_access(struct nfs4_file
*fp
, int oflag
)
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
);
306 static void nfs4_file_put_access(struct nfs4_file
*fp
, int oflag
)
308 if (oflag
== O_RDWR
) {
309 __nfs4_file_put_access(fp
, O_RDONLY
);
310 __nfs4_file_put_access(fp
, O_WRONLY
);
312 __nfs4_file_put_access(fp
, oflag
);
315 static struct nfs4_stid
*nfs4_alloc_stid(struct nfs4_client
*cl
, struct
318 struct idr
*stateids
= &cl
->cl_stateids
;
319 struct nfs4_stid
*stid
;
322 stid
= kmem_cache_alloc(slab
, GFP_KERNEL
);
326 new_id
= idr_alloc_cyclic(stateids
, stid
, 0, 0, GFP_KERNEL
);
329 stid
->sc_client
= cl
;
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;
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):
347 kmem_cache_free(slab
, stid
);
351 static struct nfs4_ol_stateid
* nfs4_alloc_stateid(struct nfs4_client
*clp
)
353 return openlockstateid(nfs4_alloc_stid(clp
, stateid_slab
));
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"
364 * Every 30 seconds we swap the filters and clear the "new" one,
365 * unless both are empty of course.
367 * Each filter is 256 bits. We hash the filehandle to 32bit and use the
368 * low 3 bytes as hash-table indices.
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.
374 static struct bloom_pair
{
375 int entries
, old_entries
;
377 int new; /* index into 'set' */
378 DECLARE_BITMAP(set
[2], 256);
379 } blocked_delegations
;
381 static int delegation_blocked(struct knfsd_fh
*fh
)
384 struct bloom_pair
*bd
= &blocked_delegations
;
386 if (bd
->entries
== 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,
396 bd
->swap_time
= seconds_since_boot();
398 spin_unlock(&state_lock
);
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]))
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]))
414 static void block_delegations(struct knfsd_fh
*fh
)
417 struct bloom_pair
*bd
= &blocked_delegations
;
419 hash
= arch_fast_hash(&fh
->fh_base
, fh
->fh_size
, 0);
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();
429 static struct nfs4_delegation
*
430 alloc_init_deleg(struct nfs4_client
*clp
, struct nfs4_ol_stateid
*stp
, struct svc_fh
*current_fh
)
432 struct nfs4_delegation
*dp
;
434 dprintk("NFSD alloc_init_deleg\n");
435 if (num_delegations
> max_delegations
)
437 if (delegation_blocked(¤t_fh
->fh_handle
))
439 dp
= delegstateid(nfs4_alloc_stid(clp
, deleg_slab
));
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:
447 dp
->dl_stid
.sc_stateid
.si_generation
= 1;
449 INIT_LIST_HEAD(&dp
->dl_perfile
);
450 INIT_LIST_HEAD(&dp
->dl_perclnt
);
451 INIT_LIST_HEAD(&dp
->dl_recall_lru
);
453 dp
->dl_type
= NFS4_OPEN_DELEGATE_READ
;
454 fh_copy_shallow(&dp
->dl_fh
, ¤t_fh
->fh_handle
);
456 atomic_set(&dp
->dl_count
, 1);
457 nfsd4_init_callback(&dp
->dl_recall
);
461 static void remove_stid(struct nfs4_stid
*s
)
463 struct idr
*stateids
= &s
->sc_client
->cl_stateids
;
465 idr_remove(stateids
, s
->sc_stateid
.si_opaque
.so_id
);
468 static void nfs4_free_stid(struct kmem_cache
*slab
, struct nfs4_stid
*s
)
470 kmem_cache_free(slab
, s
);
474 nfs4_put_delegation(struct nfs4_delegation
*dp
)
476 if (atomic_dec_and_test(&dp
->dl_count
)) {
477 nfs4_free_stid(deleg_slab
, &dp
->dl_stid
);
482 static void nfs4_put_deleg_lease(struct nfs4_file
*fp
)
486 if (atomic_dec_and_test(&fp
->fi_delegees
)) {
487 vfs_setlease(fp
->fi_deleg_file
, F_UNLCK
, &fp
->fi_lease
);
489 fput(fp
->fi_deleg_file
);
490 fp
->fi_deleg_file
= NULL
;
494 static void unhash_stid(struct nfs4_stid
*s
)
500 hash_delegation_locked(struct nfs4_delegation
*dp
, struct nfs4_file
*fp
)
502 lockdep_assert_held(&state_lock
);
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
);
509 /* Called under the state lock. */
511 unhash_delegation(struct nfs4_delegation
*dp
)
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
);
519 nfs4_put_deleg_lease(dp
->dl_file
);
520 put_nfs4_file(dp
->dl_file
);
527 static void destroy_revoked_delegation(struct nfs4_delegation
*dp
)
529 list_del_init(&dp
->dl_recall_lru
);
530 remove_stid(&dp
->dl_stid
);
531 nfs4_put_delegation(dp
);
534 static void destroy_delegation(struct nfs4_delegation
*dp
)
536 unhash_delegation(dp
);
537 remove_stid(&dp
->dl_stid
);
538 nfs4_put_delegation(dp
);
541 static void revoke_delegation(struct nfs4_delegation
*dp
)
543 struct nfs4_client
*clp
= dp
->dl_stid
.sc_client
;
545 if (clp
->cl_minorversion
== 0)
546 destroy_delegation(dp
);
548 unhash_delegation(dp
);
549 dp
->dl_stid
.sc_type
= NFS4_REVOKED_DELEG_STID
;
550 list_add(&dp
->dl_recall_lru
, &clp
->cl_revoked
);
558 static unsigned int clientid_hashval(u32 id
)
560 return id
& CLIENT_HASH_MASK
;
563 static unsigned int clientstr_hashval(const char *name
)
565 return opaque_hashval(name
, 8) & CLIENT_HASH_MASK
;
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.
577 * XXX: This enforcement is actually incomplete, since we don't keep
578 * track of access/deny bit combinations; so, e.g., we allow:
580 * OPEN allow read, deny write
581 * OPEN allow both, deny none
582 * DOWNGRADE allow read, deny none
584 * which we should reject.
587 bmap_to_share_mode(unsigned long bmap
) {
589 unsigned int access
= 0;
591 for (i
= 1; i
< 4; i
++) {
592 if (test_bit(i
, &bmap
))
599 test_share(struct nfs4_ol_stateid
*stp
, struct nfsd4_open
*open
) {
600 unsigned int access
, deny
;
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
))
609 /* set share access for a given stateid */
611 set_access(u32 access
, struct nfs4_ol_stateid
*stp
)
613 __set_bit(access
, &stp
->st_access_bmap
);
616 /* clear share access for a given stateid */
618 clear_access(u32 access
, struct nfs4_ol_stateid
*stp
)
620 __clear_bit(access
, &stp
->st_access_bmap
);
623 /* test whether a given stateid has access */
625 test_access(u32 access
, struct nfs4_ol_stateid
*stp
)
627 return test_bit(access
, &stp
->st_access_bmap
);
630 /* set share deny for a given stateid */
632 set_deny(u32 access
, struct nfs4_ol_stateid
*stp
)
634 __set_bit(access
, &stp
->st_deny_bmap
);
637 /* clear share deny for a given stateid */
639 clear_deny(u32 access
, struct nfs4_ol_stateid
*stp
)
641 __clear_bit(access
, &stp
->st_deny_bmap
);
644 /* test whether a given stateid is denying specific access */
646 test_deny(u32 access
, struct nfs4_ol_stateid
*stp
)
648 return test_bit(access
, &stp
->st_deny_bmap
);
651 static int nfs4_access_to_omode(u32 access
)
653 switch (access
& NFS4_SHARE_ACCESS_BOTH
) {
654 case NFS4_SHARE_ACCESS_READ
:
656 case NFS4_SHARE_ACCESS_WRITE
:
658 case NFS4_SHARE_ACCESS_BOTH
:
665 /* release all access and file references for a given stateid */
667 release_all_access(struct nfs4_ol_stateid
*stp
)
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
);
679 static void unhash_generic_stateid(struct nfs4_ol_stateid
*stp
)
681 list_del(&stp
->st_perfile
);
682 list_del(&stp
->st_perstateowner
);
685 static void close_generic_stateid(struct nfs4_ol_stateid
*stp
)
687 release_all_access(stp
);
688 put_nfs4_file(stp
->st_file
);
692 static void free_generic_stateid(struct nfs4_ol_stateid
*stp
)
694 remove_stid(&stp
->st_stid
);
695 nfs4_free_stid(stateid_slab
, &stp
->st_stid
);
698 static void __release_lock_stateid(struct nfs4_ol_stateid
*stp
)
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
);
707 locks_remove_posix(file
, (fl_owner_t
)lockowner(stp
->st_stateowner
));
708 close_generic_stateid(stp
);
709 free_generic_stateid(stp
);
712 static void unhash_lockowner(struct nfs4_lockowner
*lo
)
714 struct nfs4_ol_stateid
*stp
;
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
);
725 static void nfs4_free_lockowner(struct nfs4_lockowner
*lo
)
727 kfree(lo
->lo_owner
.so_owner
.data
);
728 kmem_cache_free(lockowner_slab
, lo
);
731 static void release_lockowner(struct nfs4_lockowner
*lo
)
733 unhash_lockowner(lo
);
734 nfs4_free_lockowner(lo
);
737 static void release_lockowner_if_empty(struct nfs4_lockowner
*lo
)
739 if (list_empty(&lo
->lo_owner
.so_stateids
))
740 release_lockowner(lo
);
743 static void release_lock_stateid(struct nfs4_ol_stateid
*stp
)
745 struct nfs4_lockowner
*lo
;
747 lo
= lockowner(stp
->st_stateowner
);
748 __release_lock_stateid(stp
);
749 release_lockowner_if_empty(lo
);
752 static void release_open_stateid_locks(struct nfs4_ol_stateid
*open_stp
)
754 struct nfs4_ol_stateid
*stp
;
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
);
763 static void unhash_open_stateid(struct nfs4_ol_stateid
*stp
)
765 unhash_generic_stateid(stp
);
766 release_open_stateid_locks(stp
);
767 close_generic_stateid(stp
);
770 static void release_open_stateid(struct nfs4_ol_stateid
*stp
)
772 unhash_open_stateid(stp
);
773 free_generic_stateid(stp
);
776 static void unhash_openowner(struct nfs4_openowner
*oo
)
778 struct nfs4_ol_stateid
*stp
;
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
);
789 static void release_last_closed_stateid(struct nfs4_openowner
*oo
)
791 struct nfs4_ol_stateid
*s
= oo
->oo_last_closed_stid
;
794 free_generic_stateid(s
);
795 oo
->oo_last_closed_stid
= NULL
;
799 static void nfs4_free_openowner(struct nfs4_openowner
*oo
)
801 kfree(oo
->oo_owner
.so_owner
.data
);
802 kmem_cache_free(openowner_slab
, oo
);
805 static void release_openowner(struct nfs4_openowner
*oo
)
807 unhash_openowner(oo
);
808 list_del(&oo
->oo_close_lru
);
809 release_last_closed_stateid(oo
);
810 nfs4_free_openowner(oo
);
814 hash_sessionid(struct nfs4_sessionid
*sessionid
)
816 struct nfsd4_sessionid
*sid
= (struct nfsd4_sessionid
*)sessionid
;
818 return sid
->sequence
% SESSION_HASH_SIZE
;
823 dump_sessionid(const char *fn
, struct nfs4_sessionid
*sessionid
)
825 u32
*ptr
= (u32
*)(&sessionid
->data
[0]);
826 dprintk("%s: %u:%u:%u:%u\n", fn
, ptr
[0], ptr
[1], ptr
[2], ptr
[3]);
830 dump_sessionid(const char *fn
, struct nfs4_sessionid
*sessionid
)
836 * Bump the seqid on cstate->replay_owner, and clear replay_owner if it
837 * won't be used for replay.
839 void nfsd4_bump_seqid(struct nfsd4_compound_state
*cstate
, __be32 nfserr
)
841 struct nfs4_stateowner
*so
= cstate
->replay_owner
;
843 if (nfserr
== nfserr_replay_me
)
846 if (!seqid_mutating_err(ntohl(nfserr
))) {
847 cstate
->replay_owner
= NULL
;
852 if (so
->so_is_open_owner
)
853 release_last_closed_stateid(openowner(so
));
859 gen_sessionid(struct nfsd4_session
*ses
)
861 struct nfs4_client
*clp
= ses
->se_client
;
862 struct nfsd4_sessionid
*sid
;
864 sid
= (struct nfsd4_sessionid
*)ses
->se_sessionid
.data
;
865 sid
->clientid
= clp
->cl_clientid
;
866 sid
->sequence
= current_sessionid
++;
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:
882 #define NFSD_MIN_HDR_SEQ_SZ (24 + 12 + 44)
885 free_session_slots(struct nfsd4_session
*ses
)
889 for (i
= 0; i
< ses
->se_fchannel
.maxreqs
; i
++)
890 kfree(ses
->se_slots
[i
]);
894 * We don't actually need to cache the rpc and session headers, so we
895 * can allocate a little less for each slot:
897 static inline u32
slot_bytes(struct nfsd4_channel_attrs
*ca
)
901 if (ca
->maxresp_cached
< NFSD_MIN_HDR_SEQ_SZ
)
904 size
= ca
->maxresp_cached
- NFSD_MIN_HDR_SEQ_SZ
;
905 return size
+ sizeof(struct nfsd4_slot
);
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.
913 static u32
nfsd4_get_drc_mem(struct nfsd4_channel_attrs
*ca
)
915 u32 slotsize
= slot_bytes(ca
);
916 u32 num
= ca
->maxreqs
;
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
);
929 static void nfsd4_put_drc_mem(struct nfsd4_channel_attrs
*ca
)
931 int slotsize
= slot_bytes(ca
);
933 spin_lock(&nfsd_drc_lock
);
934 nfsd_drc_mem_used
-= slotsize
* ca
->maxreqs
;
935 spin_unlock(&nfsd_drc_lock
);
938 static struct nfsd4_session
*alloc_session(struct nfsd4_channel_attrs
*fattrs
,
939 struct nfsd4_channel_attrs
*battrs
)
941 int numslots
= fattrs
->maxreqs
;
942 int slotsize
= slot_bytes(fattrs
);
943 struct nfsd4_session
*new;
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
*);
950 new = kzalloc(sizeof(*new) + mem
, GFP_KERNEL
);
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
])
960 memcpy(&new->se_fchannel
, fattrs
, sizeof(struct nfsd4_channel_attrs
));
961 memcpy(&new->se_bchannel
, battrs
, sizeof(struct nfsd4_channel_attrs
));
966 kfree(new->se_slots
[i
]);
971 static void free_conn(struct nfsd4_conn
*c
)
973 svc_xprt_put(c
->cn_xprt
);
977 static void nfsd4_conn_lost(struct svc_xpt_user
*u
)
979 struct nfsd4_conn
*c
= container_of(u
, struct nfsd4_conn
, cn_xpt_user
);
980 struct nfs4_client
*clp
= c
->cn_session
->se_client
;
982 spin_lock(&clp
->cl_lock
);
983 if (!list_empty(&c
->cn_persession
)) {
984 list_del(&c
->cn_persession
);
987 nfsd4_probe_callback(clp
);
988 spin_unlock(&clp
->cl_lock
);
991 static struct nfsd4_conn
*alloc_conn(struct svc_rqst
*rqstp
, u32 flags
)
993 struct nfsd4_conn
*conn
;
995 conn
= kmalloc(sizeof(struct nfsd4_conn
), GFP_KERNEL
);
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
);
1005 static void __nfsd4_hash_conn(struct nfsd4_conn
*conn
, struct nfsd4_session
*ses
)
1007 conn
->cn_session
= ses
;
1008 list_add(&conn
->cn_persession
, &ses
->se_conns
);
1011 static void nfsd4_hash_conn(struct nfsd4_conn
*conn
, struct nfsd4_session
*ses
)
1013 struct nfs4_client
*clp
= ses
->se_client
;
1015 spin_lock(&clp
->cl_lock
);
1016 __nfsd4_hash_conn(conn
, ses
);
1017 spin_unlock(&clp
->cl_lock
);
1020 static int nfsd4_register_conn(struct nfsd4_conn
*conn
)
1022 conn
->cn_xpt_user
.callback
= nfsd4_conn_lost
;
1023 return register_xpt_user(conn
->cn_xprt
, &conn
->cn_xpt_user
);
1026 static void nfsd4_init_conn(struct svc_rqst
*rqstp
, struct nfsd4_conn
*conn
, struct nfsd4_session
*ses
)
1030 nfsd4_hash_conn(conn
, ses
);
1031 ret
= nfsd4_register_conn(conn
);
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
);
1041 static struct nfsd4_conn
*alloc_conn_from_crses(struct svc_rqst
*rqstp
, struct nfsd4_create_session
*cses
)
1043 u32 dir
= NFS4_CDFC4_FORE
;
1045 if (cses
->flags
& SESSION4_BACK_CHAN
)
1046 dir
|= NFS4_CDFC4_BACK
;
1047 return alloc_conn(rqstp
, dir
);
1050 /* must be called under client_lock */
1051 static void nfsd4_del_conns(struct nfsd4_session
*s
)
1053 struct nfs4_client
*clp
= s
->se_client
;
1054 struct nfsd4_conn
*c
;
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
);
1062 unregister_xpt_user(c
->cn_xprt
, &c
->cn_xpt_user
);
1065 spin_lock(&clp
->cl_lock
);
1067 spin_unlock(&clp
->cl_lock
);
1070 static void __free_session(struct nfsd4_session
*ses
)
1072 free_session_slots(ses
);
1076 static void free_session(struct nfsd4_session
*ses
)
1078 struct nfsd_net
*nn
= net_generic(ses
->se_client
->net
, nfsd_net_id
);
1080 lockdep_assert_held(&nn
->client_lock
);
1081 nfsd4_del_conns(ses
);
1082 nfsd4_put_drc_mem(&ses
->se_fchannel
);
1083 __free_session(ses
);
1086 static void init_session(struct svc_rqst
*rqstp
, struct nfsd4_session
*new, struct nfs4_client
*clp
, struct nfsd4_create_session
*cses
)
1089 struct nfsd_net
*nn
= net_generic(SVC_NET(rqstp
), nfsd_net_id
);
1091 new->se_client
= clp
;
1094 INIT_LIST_HEAD(&new->se_conns
);
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
);
1109 if (cses
->flags
& SESSION4_BACK_CHAN
) {
1110 struct sockaddr
*sa
= svc_addr(rqstp
);
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
1118 rpc_copy_addr((struct sockaddr
*)&clp
->cl_cb_conn
.cb_addr
, sa
);
1119 clp
->cl_cb_conn
.cb_addrlen
= svc_addr_len(sa
);
1123 /* caller must hold client_lock */
1124 static struct nfsd4_session
*
1125 find_in_sessionid_hashtbl(struct nfs4_sessionid
*sessionid
, struct net
*net
)
1127 struct nfsd4_session
*elem
;
1129 struct nfsd_net
*nn
= net_generic(net
, nfsd_net_id
);
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
)) {
1141 dprintk("%s: session not found\n", __func__
);
1145 /* caller must hold client_lock */
1147 unhash_session(struct nfsd4_session
*ses
)
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
);
1155 /* SETCLIENTID and SETCLIENTID_CONFIRM Helper functions */
1157 STALE_CLIENTID(clientid_t
*clid
, struct nfsd_net
*nn
)
1159 if (clid
->cl_boot
== nn
->boot_time
)
1161 dprintk("NFSD stale clientid (%08x/%08x) boot_time %08lx\n",
1162 clid
->cl_boot
, clid
->cl_id
, nn
->boot_time
);
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.
1171 static struct nfs4_client
*alloc_client(struct xdr_netobj name
)
1173 struct nfs4_client
*clp
;
1175 clp
= kzalloc(sizeof(struct nfs4_client
), GFP_KERNEL
);
1178 clp
->cl_name
.data
= kmemdup(name
.data
, name
.len
, GFP_KERNEL
);
1179 if (clp
->cl_name
.data
== NULL
) {
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");
1200 free_client(struct nfs4_client
*clp
)
1202 struct nfsd_net __maybe_unused
*nn
= net_generic(clp
->net
, nfsd_net_id
);
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
,
1209 list_del(&ses
->se_perclnt
);
1210 WARN_ON_ONCE(atomic_read(&ses
->se_ref
));
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
);
1220 /* must be called under the client_lock */
1222 unhash_client_locked(struct nfs4_client
*clp
)
1224 struct nfsd4_session
*ses
;
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
);
1234 destroy_client(struct nfs4_client
*clp
)
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
);
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
);
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
);
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
);
1258 while (!list_empty(&clp
->cl_openowners
)) {
1259 oo
= list_entry(clp
->cl_openowners
.next
, struct nfs4_openowner
, oo_perclient
);
1260 release_openowner(oo
);
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
);
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
));
1274 spin_unlock(&nn
->client_lock
);
1277 static void expire_client(struct nfs4_client
*clp
)
1279 nfsd4_client_record_remove(clp
);
1280 destroy_client(clp
);
1283 static void copy_verf(struct nfs4_client
*target
, nfs4_verifier
*source
)
1285 memcpy(target
->cl_verifier
.data
, source
->data
,
1286 sizeof(target
->cl_verifier
.data
));
1289 static void copy_clid(struct nfs4_client
*target
, struct nfs4_client
*source
)
1291 target
->cl_clientid
.cl_boot
= source
->cl_clientid
.cl_boot
;
1292 target
->cl_clientid
.cl_id
= source
->cl_clientid
.cl_id
;
1295 static int copy_cred(struct svc_cred
*target
, struct svc_cred
*source
)
1297 if (source
->cr_principal
) {
1298 target
->cr_principal
=
1299 kstrdup(source
->cr_principal
, GFP_KERNEL
);
1300 if (target
->cr_principal
== NULL
)
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
);
1316 compare_blob(const struct xdr_netobj
*o1
, const struct xdr_netobj
*o2
)
1320 res
= o1
->len
- o2
->len
;
1323 return (long long)memcmp(o1
->data
, o2
->data
, o1
->len
);
1326 static int same_name(const char *n1
, const char *n2
)
1328 return 0 == memcmp(n1
, n2
, HEXDIR_LEN
);
1332 same_verf(nfs4_verifier
*v1
, nfs4_verifier
*v2
)
1334 return 0 == memcmp(v1
->data
, v2
->data
, sizeof(v1
->data
));
1338 same_clid(clientid_t
*cl1
, clientid_t
*cl2
)
1340 return (cl1
->cl_boot
== cl2
->cl_boot
) && (cl1
->cl_id
== cl2
->cl_id
);
1343 static bool groups_equal(struct group_info
*g1
, struct group_info
*g2
)
1347 if (g1
->ngroups
!= g2
->ngroups
)
1349 for (i
=0; i
<g1
->ngroups
; i
++)
1350 if (!gid_eq(GROUP_AT(g1
, i
), GROUP_AT(g2
, i
)))
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.
1364 static bool is_gss_cred(struct svc_cred
*cr
)
1366 /* Is cr_flavor one of the gss "pseudoflavors"?: */
1367 return (cr
->cr_flavor
> RPC_AUTH_MAXFLAVOR
);
1372 same_creds(struct svc_cred
*cr1
, struct svc_cred
*cr2
)
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
))
1379 if (cr1
->cr_principal
== cr2
->cr_principal
)
1381 if (!cr1
->cr_principal
|| !cr2
->cr_principal
)
1383 return 0 == strcmp(cr1
->cr_principal
, cr2
->cr_principal
);
1386 static bool svc_rqst_integrity_protected(struct svc_rqst
*rqstp
)
1388 struct svc_cred
*cr
= &rqstp
->rq_cred
;
1391 if (!cr
->cr_gss_mech
)
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
;
1398 static bool mach_creds_match(struct nfs4_client
*cl
, struct svc_rqst
*rqstp
)
1400 struct svc_cred
*cr
= &rqstp
->rq_cred
;
1402 if (!cl
->cl_mach_cred
)
1404 if (cl
->cl_cred
.cr_gss_mech
!= cr
->cr_gss_mech
)
1406 if (!svc_rqst_integrity_protected(rqstp
))
1408 if (!cr
->cr_principal
)
1410 return 0 == strcmp(cl
->cl_cred
.cr_principal
, cr
->cr_principal
);
1413 static void gen_clid(struct nfs4_client
*clp
, struct nfsd_net
*nn
)
1415 static u32 current_clientid
= 1;
1417 clp
->cl_clientid
.cl_boot
= nn
->boot_time
;
1418 clp
->cl_clientid
.cl_id
= current_clientid
++;
1421 static void gen_confirm(struct nfs4_client
*clp
)
1427 * This is opaque to client, so no need to byte-swap. Use
1428 * __force to keep sparse happy
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
));
1435 static struct nfs4_stid
*find_stateid(struct nfs4_client
*cl
, stateid_t
*t
)
1437 struct nfs4_stid
*ret
;
1439 ret
= idr_find(&cl
->cl_stateids
, t
->si_opaque
.so_id
);
1440 if (!ret
|| !ret
->sc_type
)
1445 static struct nfs4_stid
*find_stateid_by_type(struct nfs4_client
*cl
, stateid_t
*t
, char typemask
)
1447 struct nfs4_stid
*s
;
1449 s
= find_stateid(cl
, t
);
1452 if (typemask
& s
->sc_type
)
1457 static struct nfs4_client
*create_client(struct xdr_netobj name
,
1458 struct svc_rqst
*rqstp
, nfs4_verifier
*verf
)
1460 struct nfs4_client
*clp
;
1461 struct sockaddr
*sa
= svc_addr(rqstp
);
1463 struct net
*net
= SVC_NET(rqstp
);
1464 struct nfsd_net
*nn
= net_generic(net
, nfsd_net_id
);
1466 clp
= alloc_client(name
);
1470 ret
= copy_cred(&clp
->cl_cred
, &rqstp
->rq_cred
);
1472 spin_lock(&nn
->client_lock
);
1474 spin_unlock(&nn
->client_lock
);
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
);
1483 clp
->cl_cb_session
= NULL
;
1489 add_clp_to_name_tree(struct nfs4_client
*new_clp
, struct rb_root
*root
)
1491 struct rb_node
**new = &(root
->rb_node
), *parent
= NULL
;
1492 struct nfs4_client
*clp
;
1495 clp
= rb_entry(*new, struct nfs4_client
, cl_namenode
);
1498 if (compare_blob(&clp
->cl_name
, &new_clp
->cl_name
) > 0)
1499 new = &((*new)->rb_left
);
1501 new = &((*new)->rb_right
);
1504 rb_link_node(&new_clp
->cl_namenode
, parent
, new);
1505 rb_insert_color(&new_clp
->cl_namenode
, root
);
1508 static struct nfs4_client
*
1509 find_clp_in_name_tree(struct xdr_netobj
*name
, struct rb_root
*root
)
1512 struct rb_node
*node
= root
->rb_node
;
1513 struct nfs4_client
*clp
;
1516 clp
= rb_entry(node
, struct nfs4_client
, cl_namenode
);
1517 cmp
= compare_blob(&clp
->cl_name
, name
);
1519 node
= node
->rb_left
;
1521 node
= node
->rb_right
;
1529 add_to_unconfirmed(struct nfs4_client
*clp
)
1531 unsigned int idhashval
;
1532 struct nfsd_net
*nn
= net_generic(clp
->net
, nfsd_net_id
);
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
]);
1542 move_to_confirmed(struct nfs4_client
*clp
)
1544 unsigned int idhashval
= clientid_hashval(clp
->cl_clientid
.cl_id
);
1545 struct nfsd_net
*nn
= net_generic(clp
->net
, nfsd_net_id
);
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
);
1555 static struct nfs4_client
*
1556 find_client_in_id_table(struct list_head
*tbl
, clientid_t
*clid
, bool sessions
)
1558 struct nfs4_client
*clp
;
1559 unsigned int idhashval
= clientid_hashval(clid
->cl_id
);
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
)
1572 static struct nfs4_client
*
1573 find_confirmed_client(clientid_t
*clid
, bool sessions
, struct nfsd_net
*nn
)
1575 struct list_head
*tbl
= nn
->conf_id_hashtbl
;
1577 return find_client_in_id_table(tbl
, clid
, sessions
);
1580 static struct nfs4_client
*
1581 find_unconfirmed_client(clientid_t
*clid
, bool sessions
, struct nfsd_net
*nn
)
1583 struct list_head
*tbl
= nn
->unconf_id_hashtbl
;
1585 return find_client_in_id_table(tbl
, clid
, sessions
);
1588 static bool clp_used_exchangeid(struct nfs4_client
*clp
)
1590 return clp
->cl_exchange_flags
!= 0;
1593 static struct nfs4_client
*
1594 find_confirmed_client_by_name(struct xdr_netobj
*name
, struct nfsd_net
*nn
)
1596 return find_clp_in_name_tree(name
, &nn
->conf_name_tree
);
1599 static struct nfs4_client
*
1600 find_unconfirmed_client_by_name(struct xdr_netobj
*name
, struct nfsd_net
*nn
)
1602 return find_clp_in_name_tree(name
, &nn
->unconf_name_tree
);
1606 gen_callback(struct nfs4_client
*clp
, struct nfsd4_setclientid
*se
, struct svc_rqst
*rqstp
)
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
;
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
;
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
));
1628 if (!conn
->cb_addrlen
|| conn
->cb_addr
.ss_family
!= expected_family
)
1631 if (conn
->cb_addr
.ss_family
== AF_INET6
)
1632 ((struct sockaddr_in6
*)&conn
->cb_addr
)->sin6_scope_id
= scopeid
;
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
);
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
);
1649 * Cache a reply. nfsd4_check_resp_size() has bounded the cache size.
1652 nfsd4_store_cache_entry(struct nfsd4_compoundres
*resp
)
1654 struct xdr_buf
*buf
= resp
->xdr
.buf
;
1655 struct nfsd4_slot
*slot
= resp
->cstate
.slot
;
1658 dprintk("--> %s slot %p\n", __func__
, slot
);
1660 slot
->sl_opcnt
= resp
->opcnt
;
1661 slot
->sl_status
= resp
->cstate
.status
;
1663 slot
->sl_flags
|= NFSD4_SLOT_INITIALIZED
;
1664 if (nfsd4_not_cached(resp
)) {
1665 slot
->sl_datalen
= 0;
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__
);
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.
1683 nfsd4_enc_sequence_replay(struct nfsd4_compoundargs
*args
,
1684 struct nfsd4_compoundres
*resp
)
1686 struct nfsd4_op
*op
;
1687 struct nfsd4_slot
*slot
= resp
->cstate
.slot
;
1689 /* Encode the replayed sequence operation */
1690 op
= &args
->ops
[resp
->opcnt
- 1];
1691 nfsd4_encode_operation(resp
, op
);
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
);
1703 * The sequence operation is not cached because we can use the slot and
1707 nfsd4_replay_cache_entry(struct nfsd4_compoundres
*resp
,
1708 struct nfsd4_sequence
*seq
)
1710 struct nfsd4_slot
*slot
= resp
->cstate
.slot
;
1711 struct xdr_stream
*xdr
= &resp
->xdr
;
1715 dprintk("--> %s slot %p\n", __func__
, slot
);
1717 status
= nfsd4_enc_sequence_replay(resp
->rqstp
->rq_argp
, resp
);
1721 p
= xdr_reserve_space(xdr
, slot
->sl_datalen
);
1724 return nfserr_serverfault
;
1726 xdr_encode_opaque_fixed(p
, slot
->sl_data
, slot
->sl_datalen
);
1727 xdr_commit_encode(xdr
);
1729 resp
->opcnt
= slot
->sl_opcnt
;
1730 return slot
->sl_status
;
1734 * Set the exchange_id flags returned by the server.
1737 nfsd4_set_ex_flags(struct nfs4_client
*new, struct nfsd4_exchange_id
*clid
)
1739 /* pNFS is not supported */
1740 new->cl_exchange_flags
|= EXCHGID4_FLAG_USE_NON_PNFS
;
1742 /* Referrals are supported, Migration is not. */
1743 new->cl_exchange_flags
|= EXCHGID4_FLAG_SUPP_MOVED_REFER
;
1745 /* set the wire flags to return to client. */
1746 clid
->flags
= new->cl_exchange_flags
;
1749 static bool client_has_state(struct nfs4_client
*clp
)
1752 * Note clp->cl_openowners check isn't quite right: there's no
1753 * need to count owners without stateid's.
1755 * Also note we should probably be using this in 4.0 case too.
1757 return !list_empty(&clp
->cl_openowners
)
1758 || !list_empty(&clp
->cl_delegations
)
1759 || !list_empty(&clp
->cl_sessions
);
1763 nfsd4_exchange_id(struct svc_rqst
*rqstp
,
1764 struct nfsd4_compound_state
*cstate
,
1765 struct nfsd4_exchange_id
*exid
)
1767 struct nfs4_client
*unconf
, *conf
, *new;
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
);
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
);
1781 if (exid
->flags
& ~EXCHGID4_FLAG_MASK_A
)
1782 return nfserr_inval
;
1784 switch (exid
->spa_how
) {
1786 if (!svc_rqst_integrity_protected(rqstp
))
1787 return nfserr_inval
;
1790 default: /* checked by xdr code */
1793 return nfserr_encr_alg_unsupp
;
1796 /* Cases below refer to rfc 5661 section 18.35.4: */
1798 conf
= find_confirmed_client_by_name(&exid
->clname
, nn
);
1800 bool creds_match
= same_creds(&conf
->cl_cred
, &rqstp
->rq_cred
);
1801 bool verfs_match
= same_verf(&verf
, &conf
->cl_verifier
);
1804 if (!clp_used_exchangeid(conf
)) { /* buggy client */
1805 status
= nfserr_inval
;
1808 if (!mach_creds_match(conf
, rqstp
)) {
1809 status
= nfserr_wrong_cred
;
1812 if (!creds_match
) { /* case 9 */
1813 status
= nfserr_perm
;
1816 if (!verfs_match
) { /* case 8 */
1817 status
= nfserr_not_same
;
1821 exid
->flags
|= EXCHGID4_FLAG_CONFIRMED_R
;
1825 if (!creds_match
) { /* case 3 */
1826 if (client_has_state(conf
)) {
1827 status
= nfserr_clid_inuse
;
1830 expire_client(conf
);
1833 if (verfs_match
) { /* case 2 */
1834 conf
->cl_exchange_flags
|= EXCHGID4_FLAG_CONFIRMED_R
;
1838 /* case 5, client reboot */
1842 if (update
) { /* case 7 */
1843 status
= nfserr_noent
;
1847 unconf
= find_unconfirmed_client_by_name(&exid
->clname
, nn
);
1848 if (unconf
) /* case 4, possible retry or client restart */
1849 expire_client(unconf
);
1851 /* case 1 (normal case) */
1853 new = create_client(exid
->clname
, rqstp
, &verf
);
1855 status
= nfserr_jukebox
;
1858 new->cl_minorversion
= cstate
->minorversion
;
1859 new->cl_mach_cred
= (exid
->spa_how
== SP4_MACH_CRED
);
1862 add_to_unconfirmed(new);
1864 exid
->clientid
.cl_boot
= new->cl_clientid
.cl_boot
;
1865 exid
->clientid
.cl_id
= new->cl_clientid
.cl_id
;
1867 exid
->seqid
= new->cl_cs_slot
.sl_seqid
+ 1;
1868 nfsd4_set_ex_flags(new, exid
);
1870 dprintk("nfsd4_exchange_id seqid %d flags %x\n",
1871 new->cl_cs_slot
.sl_seqid
, new->cl_exchange_flags
);
1875 nfs4_unlock_state();
1880 check_slot_seqid(u32 seqid
, u32 slot_seqid
, int slot_inuse
)
1882 dprintk("%s enter. seqid %d slot_seqid %d\n", __func__
, seqid
,
1885 /* The slot is in use, and no response has been sent. */
1887 if (seqid
== slot_seqid
)
1888 return nfserr_jukebox
;
1890 return nfserr_seq_misordered
;
1892 /* Note unsigned 32-bit arithmetic handles wraparound: */
1893 if (likely(seqid
== slot_seqid
+ 1))
1895 if (seqid
== slot_seqid
)
1896 return nfserr_replay_cache
;
1897 return nfserr_seq_misordered
;
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.
1906 nfsd4_cache_create_session(struct nfsd4_create_session
*cr_ses
,
1907 struct nfsd4_clid_slot
*slot
, __be32 nfserr
)
1909 slot
->sl_status
= nfserr
;
1910 memcpy(&slot
->sl_cr_ses
, cr_ses
, sizeof(*cr_ses
));
1914 nfsd4_replay_create_session(struct nfsd4_create_session
*cr_ses
,
1915 struct nfsd4_clid_slot
*slot
)
1917 memcpy(cr_ses
, &slot
->sl_cr_ses
, sizeof(*cr_ses
));
1918 return slot
->sl_status
;
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))
1929 #define NFSD_MIN_RESP_HDR_SEQ_SZ ((\
1930 2 + /* verifier: AUTH_NULL, length 0 */\
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))
1938 static __be32
check_forechannel_attrs(struct nfsd4_channel_attrs
*ca
, struct nfsd_net
*nn
)
1940 u32 maxrpc
= nn
->nfsd_serv
->sv_max_mesg
;
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
);
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:
1961 ca
->maxreqs
= nfsd4_get_drc_mem(ca
);
1963 return nfserr_jukebox
;
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))
1973 static __be32
check_backchannel_attrs(struct nfsd4_channel_attrs
*ca
)
1975 ca
->headerpadsz
= 0;
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:
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;
1989 return nfserr_toosmall
;
1994 static __be32
nfsd4_check_cb_sec(struct nfsd4_cb_sec
*cbs
)
1996 switch (cbs
->flavor
) {
2002 * GSS case: the spec doesn't allow us to return this
2003 * error. But it also doesn't allow us not to support
2005 * I'd rather this fail hard than return some error the
2006 * client might think it can already handle:
2008 return nfserr_encr_alg_unsupp
;
2013 nfsd4_create_session(struct svc_rqst
*rqstp
,
2014 struct nfsd4_compound_state
*cstate
,
2015 struct nfsd4_create_session
*cr_ses
)
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
;
2023 struct nfsd_net
*nn
= net_generic(SVC_NET(rqstp
), nfsd_net_id
);
2025 if (cr_ses
->flags
& ~SESSION4_FLAG_MASK_A
)
2026 return nfserr_inval
;
2027 status
= nfsd4_check_cb_sec(&cr_ses
->cb_sec
);
2030 status
= check_forechannel_attrs(&cr_ses
->fore_channel
, nn
);
2033 status
= check_backchannel_attrs(&cr_ses
->back_channel
);
2035 goto out_release_drc_mem
;
2036 status
= nfserr_jukebox
;
2037 new = alloc_session(&cr_ses
->fore_channel
, &cr_ses
->back_channel
);
2039 goto out_release_drc_mem
;
2040 conn
= alloc_conn_from_crses(rqstp
, cr_ses
);
2042 goto out_free_session
;
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
);
2050 status
= nfserr_wrong_cred
;
2051 if (!mach_creds_match(conf
, rqstp
))
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
);
2058 } else if (cr_ses
->seqid
!= cs_slot
->sl_seqid
+ 1) {
2059 status
= nfserr_seq_misordered
;
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
;
2069 status
= nfserr_wrong_cred
;
2070 if (!mach_creds_match(unconf
, rqstp
))
2072 cs_slot
= &unconf
->cl_cs_slot
;
2073 status
= check_slot_seqid(cr_ses
->seqid
, cs_slot
->sl_seqid
, 0);
2075 /* an unconfirmed replay returns misordered */
2076 status
= nfserr_seq_misordered
;
2079 old
= find_confirmed_client_by_name(&unconf
->cl_name
, nn
);
2081 status
= mark_client_expired(old
);
2086 move_to_confirmed(unconf
);
2089 status
= nfserr_stale_clientid
;
2094 * We do not support RDMA or persistent sessions
2096 cr_ses
->flags
&= ~SESSION4_PERSIST
;
2097 cr_ses
->flags
&= ~SESSION4_RDMA
;
2099 init_session(rqstp
, new, conf
, cr_ses
);
2100 nfsd4_init_conn(rqstp
, conn
, new);
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
;
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();
2112 nfs4_unlock_state();
2115 __free_session(new);
2116 out_release_drc_mem
:
2117 nfsd4_put_drc_mem(&cr_ses
->fore_channel
);
2121 static __be32
nfsd4_map_bcts_dir(u32
*dir
)
2124 case NFS4_CDFC4_FORE
:
2125 case NFS4_CDFC4_BACK
:
2127 case NFS4_CDFC4_FORE_OR_BOTH
:
2128 case NFS4_CDFC4_BACK_OR_BOTH
:
2129 *dir
= NFS4_CDFC4_BOTH
;
2132 return nfserr_inval
;
2135 __be32
nfsd4_backchannel_ctl(struct svc_rqst
*rqstp
, struct nfsd4_compound_state
*cstate
, struct nfsd4_backchannel_ctl
*bc
)
2137 struct nfsd4_session
*session
= cstate
->session
;
2138 struct nfsd_net
*nn
= net_generic(SVC_NET(rqstp
), nfsd_net_id
);
2141 status
= nfsd4_check_cb_sec(&bc
->bc_cb_sec
);
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
);
2149 nfsd4_probe_callback(session
->se_client
);
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
)
2159 struct nfsd4_conn
*conn
;
2160 struct nfsd4_session
*session
;
2161 struct nfsd_net
*nn
= net_generic(SVC_NET(rqstp
), nfsd_net_id
);
2163 if (!nfsd4_last_compound_op(rqstp
))
2164 return nfserr_not_only_op
;
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
;
2172 status
= nfserr_wrong_cred
;
2173 if (!mach_creds_match(session
->se_client
, rqstp
))
2175 status
= nfsd4_map_bcts_dir(&bcts
->dir
);
2178 conn
= alloc_conn(rqstp
, bcts
->dir
);
2179 status
= nfserr_jukebox
;
2182 nfsd4_init_conn(rqstp
, conn
, session
);
2185 nfs4_unlock_state();
2189 static bool nfsd4_compound_in_session(struct nfsd4_session
*session
, struct nfs4_sessionid
*sid
)
2193 return !memcmp(sid
, &session
->se_sessionid
, sizeof(*sid
));
2197 nfsd4_destroy_session(struct svc_rqst
*r
,
2198 struct nfsd4_compound_state
*cstate
,
2199 struct nfsd4_destroy_session
*sessionid
)
2201 struct nfsd4_session
*ses
;
2203 int ref_held_by_me
= 0;
2204 struct nfsd_net
*nn
= net_generic(SVC_NET(r
), nfsd_net_id
);
2207 status
= nfserr_not_only_op
;
2208 if (nfsd4_compound_in_session(cstate
->session
, &sessionid
->sessionid
)) {
2209 if (!nfsd4_last_compound_op(r
))
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
;
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
);
2225 goto out_put_session
;
2226 unhash_session(ses
);
2227 spin_unlock(&nn
->client_lock
);
2229 nfsd4_probe_callback_sync(ses
->se_client
);
2231 spin_lock(&nn
->client_lock
);
2234 nfsd4_put_session(ses
);
2236 spin_unlock(&nn
->client_lock
);
2238 nfs4_unlock_state();
2242 static struct nfsd4_conn
*__nfsd4_find_conn(struct svc_xprt
*xpt
, struct nfsd4_session
*s
)
2244 struct nfsd4_conn
*c
;
2246 list_for_each_entry(c
, &s
->se_conns
, cn_persession
) {
2247 if (c
->cn_xprt
== xpt
) {
2254 static __be32
nfsd4_sequence_check_conn(struct nfsd4_conn
*new, struct nfsd4_session
*ses
)
2256 struct nfs4_client
*clp
= ses
->se_client
;
2257 struct nfsd4_conn
*c
;
2258 __be32 status
= nfs_ok
;
2261 spin_lock(&clp
->cl_lock
);
2262 c
= __nfsd4_find_conn(new->cn_xprt
, ses
);
2265 status
= nfserr_conn_not_bound_to_session
;
2266 if (clp
->cl_mach_cred
)
2268 __nfsd4_hash_conn(new, ses
);
2269 spin_unlock(&clp
->cl_lock
);
2270 ret
= nfsd4_register_conn(new);
2272 /* oops; xprt is already down: */
2273 nfsd4_conn_lost(&new->cn_xpt_user
);
2276 spin_unlock(&clp
->cl_lock
);
2281 static bool nfsd4_session_too_many_ops(struct svc_rqst
*rqstp
, struct nfsd4_session
*session
)
2283 struct nfsd4_compoundargs
*args
= rqstp
->rq_argp
;
2285 return args
->opcnt
> session
->se_fchannel
.maxops
;
2288 static bool nfsd4_request_too_big(struct svc_rqst
*rqstp
,
2289 struct nfsd4_session
*session
)
2291 struct xdr_buf
*xb
= &rqstp
->rq_arg
;
2293 return xb
->len
> session
->se_fchannel
.maxreq_sz
;
2297 nfsd4_sequence(struct svc_rqst
*rqstp
,
2298 struct nfsd4_compound_state
*cstate
,
2299 struct nfsd4_sequence
*seq
)
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
;
2309 struct nfsd_net
*nn
= net_generic(SVC_NET(rqstp
), nfsd_net_id
);
2311 if (resp
->opcnt
!= 1)
2312 return nfserr_sequence_pos
;
2315 * Will be either used or freed by nfsd4_sequence_check_conn
2318 conn
= alloc_conn(rqstp
, NFS4_CDFC4_FORE
);
2320 return nfserr_jukebox
;
2322 spin_lock(&nn
->client_lock
);
2323 status
= nfserr_badsession
;
2324 session
= find_in_sessionid_hashtbl(&seq
->sessionid
, SVC_NET(rqstp
));
2326 goto out_no_session
;
2327 clp
= session
->se_client
;
2328 status
= get_client_locked(clp
);
2330 goto out_no_session
;
2331 status
= nfsd4_get_session_locked(session
);
2333 goto out_put_client
;
2335 status
= nfserr_too_many_ops
;
2336 if (nfsd4_session_too_many_ops(rqstp
, session
))
2337 goto out_put_session
;
2339 status
= nfserr_req_too_big
;
2340 if (nfsd4_request_too_big(rqstp
, session
))
2341 goto out_put_session
;
2343 status
= nfserr_badslot
;
2344 if (seq
->slotid
>= session
->se_fchannel
.maxreqs
)
2345 goto out_put_session
;
2347 slot
= session
->se_slots
[seq
->slotid
];
2348 dprintk("%s: slotid %d\n", __func__
, seq
->slotid
);
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
;
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
;
2370 goto out_put_session
;
2372 status
= nfsd4_sequence_check_conn(conn
, session
);
2375 goto out_put_session
;
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
:
2382 if (xdr_restrict_buflen(xdr
, buflen
- rqstp
->rq_auth_slack
))
2383 goto out_put_session
;
2384 svc_reserve(rqstp
, buflen
);
2387 /* Success! bump slot seqid */
2388 slot
->sl_seqid
= seq
->seqid
;
2389 slot
->sl_flags
|= NFSD4_SLOT_INUSE
;
2391 slot
->sl_flags
|= NFSD4_SLOT_CACHETHIS
;
2393 slot
->sl_flags
&= ~NFSD4_SLOT_CACHETHIS
;
2395 cstate
->slot
= slot
;
2396 cstate
->session
= session
;
2399 switch (clp
->cl_cb_state
) {
2401 seq
->status_flags
= SEQ4_STATUS_CB_PATH_DOWN
;
2403 case NFSD4_CB_FAULT
:
2404 seq
->status_flags
= SEQ4_STATUS_BACKCHANNEL_FAULT
;
2407 seq
->status_flags
= 0;
2409 if (!list_empty(&clp
->cl_revoked
))
2410 seq
->status_flags
|= SEQ4_STATUS_RECALLABLE_STATE_REVOKED
;
2414 spin_unlock(&nn
->client_lock
);
2417 nfsd4_put_session(session
);
2419 put_client_renew_locked(clp
);
2420 goto out_no_session
;
2424 nfsd4_sequence_done(struct nfsd4_compoundres
*resp
)
2426 struct nfsd4_compound_state
*cs
= &resp
->cstate
;
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
);
2432 if (cs
->status
!= nfserr_replay_cache
) {
2433 nfsd4_store_cache_entry(resp
);
2434 cs
->slot
->sl_flags
&= ~NFSD4_SLOT_INUSE
;
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
);
2445 nfsd4_destroy_clientid(struct svc_rqst
*rqstp
, struct nfsd4_compound_state
*cstate
, struct nfsd4_destroy_clientid
*dc
)
2447 struct nfs4_client
*conf
, *unconf
, *clp
;
2449 struct nfsd_net
*nn
= net_generic(SVC_NET(rqstp
), nfsd_net_id
);
2452 unconf
= find_unconfirmed_client(&dc
->clientid
, true, nn
);
2453 conf
= find_confirmed_client(&dc
->clientid
, true, nn
);
2454 WARN_ON_ONCE(conf
&& unconf
);
2459 if (client_has_state(conf
)) {
2460 status
= nfserr_clientid_busy
;
2466 status
= nfserr_stale_clientid
;
2469 if (!mach_creds_match(clp
, rqstp
)) {
2470 status
= nfserr_wrong_cred
;
2475 nfs4_unlock_state();
2480 nfsd4_reclaim_complete(struct svc_rqst
*rqstp
, struct nfsd4_compound_state
*cstate
, struct nfsd4_reclaim_complete
*rc
)
2484 if (rc
->rca_one_fs
) {
2485 if (!cstate
->current_fh
.fh_dentry
)
2486 return nfserr_nofilehandle
;
2488 * We don't take advantage of the rca_one_fs case.
2489 * That's OK, it's optional, we can safely ignore it.
2495 status
= nfserr_complete_already
;
2496 if (test_and_set_bit(NFSD4_CLIENT_RECLAIM_COMPLETE
,
2497 &cstate
->session
->se_client
->cl_flags
))
2500 status
= nfserr_stale_clientid
;
2501 if (is_client_expired(cstate
->session
->se_client
))
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
2512 nfsd4_client_record_create(cstate
->session
->se_client
);
2514 nfs4_unlock_state();
2519 nfsd4_setclientid(struct svc_rqst
*rqstp
, struct nfsd4_compound_state
*cstate
,
2520 struct nfsd4_setclientid
*setclid
)
2522 struct xdr_netobj clname
= setclid
->se_name
;
2523 nfs4_verifier clverifier
= setclid
->se_verf
;
2524 struct nfs4_client
*conf
, *unconf
, *new;
2526 struct nfsd_net
*nn
= net_generic(SVC_NET(rqstp
), nfsd_net_id
);
2528 /* Cases below refer to rfc 3530 section 14.2.33: */
2530 conf
= find_confirmed_client_by_name(&clname
, nn
);
2533 status
= nfserr_clid_inuse
;
2534 if (clp_used_exchangeid(conf
))
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
,
2540 dprintk("NFSD: setclientid: string in use by client "
2541 "at %s\n", addr_str
);
2545 unconf
= find_unconfirmed_client_by_name(&clname
, nn
);
2547 expire_client(unconf
);
2548 status
= nfserr_jukebox
;
2549 new = create_client(clname
, rqstp
, &clverifier
);
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): */
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
));
2565 nfs4_unlock_state();
2571 nfsd4_setclientid_confirm(struct svc_rqst
*rqstp
,
2572 struct nfsd4_compound_state
*cstate
,
2573 struct nfsd4_setclientid_confirm
*setclientid_confirm
)
2575 struct nfs4_client
*conf
, *unconf
;
2576 nfs4_verifier confirm
= setclientid_confirm
->sc_confirm
;
2577 clientid_t
* clid
= &setclientid_confirm
->sc_clientid
;
2579 struct nfsd_net
*nn
= net_generic(SVC_NET(rqstp
), nfsd_net_id
);
2581 if (STALE_CLIENTID(clid
, nn
))
2582 return nfserr_stale_clientid
;
2585 conf
= find_confirmed_client(clid
, false, nn
);
2586 unconf
= find_unconfirmed_client(clid
, false, nn
);
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
2593 status
= nfserr_serverfault
;
2594 if (unconf
&& !same_creds(&unconf
->cl_cred
, &rqstp
->rq_cred
))
2596 if (conf
&& !same_creds(&conf
->cl_cred
, &rqstp
->rq_cred
))
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 */
2602 else /* case 4: client hasn't noticed we rebooted yet? */
2603 status
= nfserr_stale_clientid
;
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
);
2614 status
= mark_client_expired(conf
);
2617 expire_client(conf
);
2619 move_to_confirmed(unconf
);
2620 nfsd4_probe_callback(unconf
);
2623 nfs4_unlock_state();
2627 static struct nfs4_file
*nfsd4_alloc_file(void)
2629 return kmem_cache_alloc(file_slab
, GFP_KERNEL
);
2632 /* OPEN Share state helper functions */
2633 static void nfsd4_init_file(struct nfs4_file
*fp
, struct inode
*ino
)
2635 unsigned int hashval
= file_hashval(ino
);
2637 lockdep_assert_held(&state_lock
);
2639 atomic_set(&fp
->fi_ref
, 1);
2640 INIT_LIST_HEAD(&fp
->fi_stateids
);
2641 INIT_LIST_HEAD(&fp
->fi_delegations
);
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
]);
2652 nfsd4_free_slabs(void)
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
);
2662 nfsd4_init_slabs(void)
2664 openowner_slab
= kmem_cache_create("nfsd4_openowners",
2665 sizeof(struct nfs4_openowner
), 0, 0, NULL
);
2666 if (openowner_slab
== NULL
)
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
;
2686 out_free_stateid_slab
:
2687 kmem_cache_destroy(stateid_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
);
2695 dprintk("nfsd4: out of memory while initializing nfsv4\n");
2699 static void init_nfs4_replay(struct nfs4_replay
*rp
)
2701 rp
->rp_status
= nfserr_serverfault
;
2703 rp
->rp_buf
= rp
->rp_ibuf
;
2706 static inline void *alloc_stateowner(struct kmem_cache
*slab
, struct xdr_netobj
*owner
, struct nfs4_client
*clp
)
2708 struct nfs4_stateowner
*sop
;
2710 sop
= kmem_cache_alloc(slab
, GFP_KERNEL
);
2714 sop
->so_owner
.data
= kmemdup(owner
->data
, owner
->len
, GFP_KERNEL
);
2715 if (!sop
->so_owner
.data
) {
2716 kmem_cache_free(slab
, sop
);
2719 sop
->so_owner
.len
= owner
->len
;
2721 INIT_LIST_HEAD(&sop
->so_stateids
);
2722 sop
->so_client
= clp
;
2723 init_nfs4_replay(&sop
->so_replay
);
2727 static void hash_openowner(struct nfs4_openowner
*oo
, struct nfs4_client
*clp
, unsigned int strhashval
)
2729 struct nfsd_net
*nn
= net_generic(clp
->net
, nfsd_net_id
);
2731 list_add(&oo
->oo_owner
.so_strhash
, &nn
->ownerstr_hashtbl
[strhashval
]);
2732 list_add(&oo
->oo_perclient
, &clp
->cl_openowners
);
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
)
2740 struct nfs4_openowner
*oo
;
2742 oo
= alloc_stateowner(openowner_slab
, &open
->op_owner
, clp
);
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
;
2751 oo
->oo_last_closed_stid
= NULL
;
2752 INIT_LIST_HEAD(&oo
->oo_close_lru
);
2753 hash_openowner(oo
, clp
, strhashval
);
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
;
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
;
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
;
2775 move_to_close_lru(struct nfs4_openowner
*oo
, struct net
*net
)
2777 struct nfsd_net
*nn
= net_generic(net
, nfsd_net_id
);
2779 dprintk("NFSD: move_to_close_lru nfs4_openowner %p\n", oo
);
2781 list_move_tail(&oo
->oo_close_lru
, &nn
->close_lru
);
2782 oo
->oo_time
= get_seconds();
2786 same_owner_str(struct nfs4_stateowner
*sop
, struct xdr_netobj
*owner
,
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
);
2794 static struct nfs4_openowner
*
2795 find_openstateowner_str(unsigned int hashval
, struct nfsd4_open
*open
,
2796 bool sessions
, struct nfsd_net
*nn
)
2798 struct nfs4_stateowner
*so
;
2799 struct nfs4_openowner
*oo
;
2800 struct nfs4_client
*clp
;
2802 list_for_each_entry(so
, &nn
->ownerstr_hashtbl
[hashval
], so_strhash
) {
2803 if (!so
->so_is_open_owner
)
2805 if (same_owner_str(so
, &open
->op_owner
, &open
->op_clientid
)) {
2807 clp
= oo
->oo_owner
.so_client
;
2808 if ((bool)clp
->cl_minorversion
!= sessions
)
2810 renew_client(oo
->oo_owner
.so_client
);
2817 /* search file_hashtbl[] for file */
2818 static struct nfs4_file
*
2819 find_file_locked(struct inode
*ino
)
2821 unsigned int hashval
= file_hashval(ino
);
2822 struct nfs4_file
*fp
;
2824 lockdep_assert_held(&state_lock
);
2826 hlist_for_each_entry(fp
, &file_hashtbl
[hashval
], fi_hash
) {
2827 if (fp
->fi_inode
== ino
) {
2835 static struct nfs4_file
*
2836 find_file(struct inode
*ino
)
2838 struct nfs4_file
*fp
;
2840 spin_lock(&state_lock
);
2841 fp
= find_file_locked(ino
);
2842 spin_unlock(&state_lock
);
2846 static struct nfs4_file
*
2847 find_or_add_file(struct inode
*ino
, struct nfs4_file
*new)
2849 struct nfs4_file
*fp
;
2851 spin_lock(&state_lock
);
2852 fp
= find_file_locked(ino
);
2854 nfsd4_init_file(new, ino
);
2857 spin_unlock(&state_lock
);
2863 * Called to check deny when READ with all zero stateid or
2864 * WRITE with all zero or all one stateid
2867 nfs4_share_conflict(struct svc_fh
*current_fh
, unsigned int deny_type
)
2869 struct inode
*ino
= current_fh
->fh_dentry
->d_inode
;
2870 struct nfs4_file
*fp
;
2871 struct nfs4_ol_stateid
*stp
;
2874 fp
= find_file(ino
);
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
))
2890 static void nfsd_break_one_deleg(struct nfs4_delegation
*dp
)
2892 struct nfs4_client
*clp
= dp
->dl_stid
.sc_client
;
2893 struct nfsd_net
*nn
= net_generic(clp
->net
, nfsd_net_id
);
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
);
2903 list_add_tail(&dp
->dl_recall_lru
, &nn
->del_recall_lru
);
2905 /* Only place dl_time is set; protected by i_lock: */
2906 dp
->dl_time
= get_seconds();
2908 block_delegations(&dp
->dl_fh
);
2910 nfsd4_cb_recall(dp
);
2913 /* Called from break_lease() with i_lock held. */
2914 static void nfsd_break_deleg_cb(struct file_lock
*fl
)
2916 struct nfs4_file
*fp
= (struct nfs4_file
*)fl
->fl_owner
;
2917 struct nfs4_delegation
*dp
;
2920 WARN(1, "(%p)->fl_owner NULL\n", fl
);
2923 if (fp
->fi_had_conflict
) {
2924 WARN(1, "duplicate break on %p\n", fp
);
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
2932 fl
->fl_break_time
= 0;
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
);
2942 int nfsd_change_deleg_cb(struct file_lock
**onlist
, int arg
)
2945 return lease_modify(onlist
, arg
);
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
,
2955 static __be32
nfsd4_check_seqid(struct nfsd4_compound_state
*cstate
, struct nfs4_stateowner
*so
, u32 seqid
)
2957 if (nfsd4_has_session(cstate
))
2959 if (seqid
== so
->so_seqid
- 1)
2960 return nfserr_replay_me
;
2961 if (seqid
== so
->so_seqid
)
2963 return nfserr_bad_seqid
;
2967 nfsd4_process_open1(struct nfsd4_compound_state
*cstate
,
2968 struct nfsd4_open
*open
, struct nfsd_net
*nn
)
2970 clientid_t
*clientid
= &open
->op_clientid
;
2971 struct nfs4_client
*clp
= NULL
;
2972 unsigned int strhashval
;
2973 struct nfs4_openowner
*oo
= NULL
;
2976 if (STALE_CLIENTID(&open
->op_clientid
, nn
))
2977 return nfserr_stale_clientid
;
2979 * In case we need it later, after we've already created the
2980 * file and don't want to risk a further failure:
2982 open
->op_file
= nfsd4_alloc_file();
2983 if (open
->op_file
== NULL
)
2984 return nfserr_jukebox
;
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
;
2990 clp
= find_confirmed_client(clientid
, cstate
->minorversion
,
2993 return nfserr_expired
;
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
;
3003 status
= nfsd4_check_seqid(cstate
, &oo
->oo_owner
, open
->op_seqid
);
3006 clp
= oo
->oo_owner
.so_client
;
3009 oo
= alloc_init_open_stateowner(strhashval
, clp
, open
, cstate
);
3011 return nfserr_jukebox
;
3012 open
->op_openowner
= oo
;
3014 open
->op_stp
= nfs4_alloc_stateid(clp
);
3016 return nfserr_jukebox
;
3020 static inline __be32
3021 nfs4_check_delegmode(struct nfs4_delegation
*dp
, int flags
)
3023 if ((flags
& WR_STATE
) && (dp
->dl_type
== NFS4_OPEN_DELEGATE_READ
))
3024 return nfserr_openmode
;
3029 static int share_access_to_flags(u32 share_access
)
3031 return share_access
== NFS4_SHARE_ACCESS_READ
? RD_STATE
: WR_STATE
;
3034 static struct nfs4_delegation
*find_deleg_stateid(struct nfs4_client
*cl
, stateid_t
*s
)
3036 struct nfs4_stid
*ret
;
3038 ret
= find_stateid_by_type(cl
, s
, NFS4_DELEG_STID
);
3041 return delegstateid(ret
);
3044 static bool nfsd4_is_deleg_cur(struct nfsd4_open
*open
)
3046 return open
->op_claim_type
== NFS4_OPEN_CLAIM_DELEGATE_CUR
||
3047 open
->op_claim_type
== NFS4_OPEN_CLAIM_DELEG_CUR_FH
;
3051 nfs4_check_deleg(struct nfs4_client
*cl
, struct nfsd4_open
*open
,
3052 struct nfs4_delegation
**dp
)
3055 __be32 status
= nfserr_bad_stateid
;
3057 *dp
= find_deleg_stateid(cl
, &open
->op_delegate_stateid
);
3060 flags
= share_access_to_flags(open
->op_share_access
);
3061 status
= nfs4_check_delegmode(*dp
, flags
);
3065 if (!nfsd4_is_deleg_cur(open
))
3069 open
->op_openowner
->oo_flags
|= NFS4_OO_CONFIRMED
;
3074 nfs4_check_open(struct nfs4_file
*fp
, struct nfsd4_open
*open
, struct nfs4_ol_stateid
**stpp
)
3076 struct nfs4_ol_stateid
*local
;
3077 struct nfs4_openowner
*oo
= open
->op_openowner
;
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)
3083 /* remember if we have seen this open owner */
3084 if (local
->st_stateowner
== &oo
->oo_owner
)
3086 /* check for conflicting share reservations */
3087 if (!test_share(local
, open
))
3088 return nfserr_share_denied
;
3093 static inline int nfs4_access_to_access(u32 nfs4_access
)
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
;
3104 static inline __be32
3105 nfsd4_truncate(struct svc_rqst
*rqstp
, struct svc_fh
*fh
,
3106 struct nfsd4_open
*open
)
3108 struct iattr iattr
= {
3109 .ia_valid
= ATTR_SIZE
,
3112 if (!open
->op_truncate
)
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);
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
)
3123 int oflag
= nfs4_access_to_omode(open
->op_share_access
);
3124 int access
= nfs4_access_to_access(open
->op_share_access
);
3126 if (!fp
->fi_fds
[oflag
]) {
3127 status
= nfsd_open(rqstp
, cur_fh
, S_IFREG
, access
,
3128 &fp
->fi_fds
[oflag
]);
3132 nfs4_file_get_access(fp
, oflag
);
3134 status
= nfsd4_truncate(rqstp
, cur_fh
, open
);
3136 goto out_put_access
;
3141 nfs4_file_put_access(fp
, oflag
);
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
)
3149 u32 op_share_access
= open
->op_share_access
;
3152 if (!test_access(op_share_access
, stp
))
3153 status
= nfs4_get_vfs_file(rqstp
, fp
, cur_fh
, open
);
3155 status
= nfsd4_truncate(rqstp
, cur_fh
, open
);
3160 /* remember the open */
3161 set_access(op_share_access
, stp
);
3162 set_deny(open
->op_share_deny
, stp
);
3168 nfs4_set_claim_prev(struct nfsd4_open
*open
, bool has_session
)
3170 open
->op_openowner
->oo_flags
|= NFS4_OO_CONFIRMED
;
3173 /* Should we give out recallable state?: */
3174 static bool nfsd4_cb_channel_good(struct nfs4_client
*clp
)
3176 if (clp
->cl_cb_state
== NFSD4_CB_UP
)
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:
3183 return clp
->cl_minorversion
&& clp
->cl_cb_state
== NFSD4_CB_UNKNOWN
;
3186 static struct file_lock
*nfs4_alloc_init_lease(struct nfs4_delegation
*dp
, int flag
)
3188 struct file_lock
*fl
;
3190 fl
= locks_alloc_lock();
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
;
3203 static int nfs4_setlease(struct nfs4_delegation
*dp
)
3205 struct nfs4_file
*fp
= dp
->dl_file
;
3206 struct file_lock
*fl
;
3209 fl
= nfs4_alloc_init_lease(dp
, NFS4_OPEN_DELEGATE_READ
);
3212 fl
->fl_file
= find_readable_file(fp
);
3213 status
= vfs_setlease(fl
->fl_file
, fl
->fl_type
, &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
);
3224 locks_free_lock(fl
);
3228 static int nfs4_set_delegation(struct nfs4_delegation
*dp
, struct nfs4_file
*fp
)
3230 if (fp
->fi_had_conflict
)
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
);
3242 hash_delegation_locked(dp
, fp
);
3243 spin_unlock(&state_lock
);
3247 static void nfsd4_open_deleg_none_ext(struct nfsd4_open
*open
, int status
)
3249 open
->op_delegate_type
= NFS4_OPEN_DELEGATE_NONE_EXT
;
3250 if (status
== -EAGAIN
)
3251 open
->op_why_no_deleg
= WND4_CONTENTION
;
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
:
3259 case NFS4_SHARE_WANT_CANCEL
:
3260 open
->op_why_no_deleg
= WND4_CANCELLED
;
3262 case NFS4_SHARE_WANT_NO_DELEG
:
3269 * Attempt to hand out a delegation.
3271 * Note we don't support write delegations, and won't until the vfs has
3272 * proper support for them.
3275 nfs4_open_delegation(struct net
*net
, struct svc_fh
*fh
,
3276 struct nfsd4_open
*open
, struct nfs4_ol_stateid
*stp
)
3278 struct nfs4_delegation
*dp
;
3279 struct nfs4_openowner
*oo
= container_of(stp
->st_stateowner
, struct nfs4_openowner
, oo_owner
);
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
:
3288 open
->op_recall
= 1;
3289 if (open
->op_delegate_type
!= NFS4_OPEN_DELEGATE_READ
)
3292 case NFS4_OPEN_CLAIM_NULL
:
3293 case NFS4_OPEN_CLAIM_FH
:
3295 * Let's not give out any delegations till everyone's
3296 * had the chance to reclaim theirs....
3298 if (locks_in_grace(net
))
3300 if (!cb_up
|| !(oo
->oo_flags
& NFS4_OO_CONFIRMED
))
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):
3309 if (open
->op_share_access
& NFS4_SHARE_ACCESS_WRITE
)
3311 if (open
->op_create
== NFS4_OPEN_CREATE
)
3317 dp
= alloc_init_deleg(oo
->oo_owner
.so_client
, stp
, fh
);
3320 status
= nfs4_set_delegation(dp
, stp
->st_file
);
3324 memcpy(&open
->op_delegate_stateid
, &dp
->dl_stid
.sc_stateid
, sizeof(dp
->dl_stid
.sc_stateid
));
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
;
3331 destroy_delegation(dp
);
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;
3340 /* 4.1 client asking for a delegation? */
3341 if (open
->op_deleg_want
)
3342 nfsd4_open_deleg_none_ext(open
, status
);
3346 static void nfsd4_deleg_xgrade_none_ext(struct nfsd4_open
*open
,
3347 struct nfs4_delegation
*dp
)
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
;
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.
3365 * called with nfs4_lock_state() held.
3368 nfsd4_process_open2(struct svc_rqst
*rqstp
, struct svc_fh
*current_fh
, struct nfsd4_open
*open
)
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
;
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
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
)))
3387 status
= nfs4_check_deleg(cl
, open
, &dp
);
3391 open
->op_file
= NULL
;
3392 status
= nfserr_bad_stateid
;
3393 if (nfsd4_is_deleg_cur(open
))
3395 status
= nfserr_jukebox
;
3399 * OPEN the file, or upgrade an existing OPEN.
3400 * If truncate fails, the OPEN fails.
3403 /* Stateid was found, this is an OPEN upgrade */
3404 status
= nfs4_upgrade_open(rqstp
, fp
, current_fh
, stp
, open
);
3408 status
= nfs4_get_vfs_file(rqstp
, fp
, current_fh
, open
);
3412 open
->op_stp
= NULL
;
3413 init_open_stateid(stp
, fp
, open
);
3415 update_stateid(&stp
->st_stid
.sc_stateid
);
3416 memcpy(&open
->op_stateid
, &stp
->st_stid
.sc_stateid
, sizeof(stateid_t
));
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
;
3427 * Attempt to hand out a delegation. No error return, because the
3428 * OPEN succeeds even if we fail.
3430 nfs4_open_delegation(SVC_NET(rqstp
), current_fh
, open
, stp
);
3434 dprintk("%s: stateid=" STATEID_FMT
"\n", __func__
,
3435 STATEID_VAL(&stp
->st_stid
.sc_stateid
));
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
);
3444 if (status
== 0 && open
->op_claim_type
== NFS4_OPEN_CLAIM_PREVIOUS
)
3445 nfs4_set_claim_prev(open
, nfsd4_has_session(&resp
->cstate
));
3447 * To finish the open response, we just need to set the rflags.
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
;
3457 void nfsd4_cleanup_open_state(struct nfsd4_open
*open
, __be32 status
)
3459 if (open
->op_openowner
) {
3460 struct nfs4_openowner
*oo
= open
->op_openowner
;
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
) {
3466 release_openowner(oo
);
3467 open
->op_openowner
= NULL
;
3469 oo
->oo_flags
&= ~NFS4_OO_NEW
;
3473 nfsd4_free_file(open
->op_file
);
3475 free_generic_stateid(open
->op_stp
);
3478 static __be32
lookup_clientid(clientid_t
*clid
, bool session
, struct nfsd_net
*nn
, struct nfs4_client
**clp
)
3480 struct nfs4_client
*found
;
3482 if (STALE_CLIENTID(clid
, nn
))
3483 return nfserr_stale_clientid
;
3484 found
= find_confirmed_client(clid
, session
, nn
);
3487 return found
? nfs_ok
: nfserr_expired
;
3491 nfsd4_renew(struct svc_rqst
*rqstp
, struct nfsd4_compound_state
*cstate
,
3494 struct nfs4_client
*clp
;
3496 struct nfsd_net
*nn
= net_generic(SVC_NET(rqstp
), nfsd_net_id
);
3499 dprintk("process_renew(%08x/%08x): starting\n",
3500 clid
->cl_boot
, clid
->cl_id
);
3501 status
= lookup_clientid(clid
, cstate
->minorversion
, nn
, &clp
);
3504 status
= nfserr_cb_path_down
;
3505 if (!list_empty(&clp
->cl_delegations
)
3506 && clp
->cl_cb_state
!= NFSD4_CB_UP
)
3510 nfs4_unlock_state();
3515 nfsd4_end_grace(struct nfsd_net
*nn
)
3517 /* do nothing if grace period already ended */
3518 if (nn
->grace_ended
)
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
);
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:
3530 nn
->nfsd4_grace
= nn
->nfsd4_lease
;
3534 nfs4_laundromat(struct nfsd_net
*nn
)
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
;
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
);
3556 if (mark_client_expired_locked(clp
)) {
3557 dprintk("NFSD: client in use (clientid %08x)\n",
3558 clp
->cl_clientid
.cl_id
);
3561 list_move(&clp
->cl_lru
, &reaplist
);
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
);
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
)
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
);
3580 list_move(&dp
->dl_recall_lru
, &reaplist
);
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
);
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
);
3594 release_openowner(oo
);
3596 new_timeo
= max_t(time_t, new_timeo
, NFSD_LAUNDROMAT_MINTIMEOUT
);
3597 nfs4_unlock_state();
3601 static struct workqueue_struct
*laundry_wq
;
3602 static void laundromat_main(struct work_struct
*);
3605 laundromat_main(struct work_struct
*laundry
)
3608 struct delayed_work
*dwork
= container_of(laundry
, struct delayed_work
,
3610 struct nfsd_net
*nn
= container_of(dwork
, struct nfsd_net
,
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
);
3618 static inline __be32
nfs4_check_fh(struct svc_fh
*fhp
, struct nfs4_ol_stateid
*stp
)
3620 if (fhp
->fh_dentry
->d_inode
!= stp
->st_file
->fi_inode
)
3621 return nfserr_bad_stateid
;
3626 access_permit_read(struct nfs4_ol_stateid
*stp
)
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
);
3634 access_permit_write(struct nfs4_ol_stateid
*stp
)
3636 return test_access(NFS4_SHARE_ACCESS_WRITE
, stp
) ||
3637 test_access(NFS4_SHARE_ACCESS_BOTH
, stp
);
3641 __be32
nfs4_check_openmode(struct nfs4_ol_stateid
*stp
, int flags
)
3643 __be32 status
= nfserr_openmode
;
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
))
3650 if ((flags
& RD_STATE
) && !access_permit_read(stp
))
3657 static inline __be32
3658 check_special_stateids(struct net
*net
, svc_fh
*current_fh
, stateid_t
*stateid
, int flags
)
3660 if (ONE_STATEID(stateid
) && (flags
& RD_STATE
))
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
);
3675 * Allow READ/WRITE during grace period on recovered state only for files
3676 * that are not able to provide mandatory locking.
3679 grace_disallows_io(struct net
*net
, struct inode
*inode
)
3681 return locks_in_grace(net
) && mandatory_lock(inode
);
3684 /* Returns true iff a is later than b: */
3685 static bool stateid_generation_after(stateid_t
*a
, stateid_t
*b
)
3687 return (s32
)(a
->si_generation
- b
->si_generation
) > 0;
3690 static __be32
check_stateid_generation(stateid_t
*in
, stateid_t
*ref
, bool has_session
)
3693 * When sessions are used the stateid generation number is ignored
3696 if (has_session
&& in
->si_generation
== 0)
3699 if (in
->si_generation
== ref
->si_generation
)
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
;
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:
3715 return nfserr_old_stateid
;
3718 static __be32
nfsd4_validate_stateid(struct nfs4_client
*cl
, stateid_t
*stateid
)
3720 struct nfs4_stid
*s
;
3721 struct nfs4_ol_stateid
*ols
;
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
,
3731 pr_warn_ratelimited("NFSD: client %s testing state ID "
3732 "with incorrect client ID\n", addr_str
);
3733 return nfserr_bad_stateid
;
3735 s
= find_stateid(cl
, stateid
);
3737 return nfserr_bad_stateid
;
3738 status
= check_stateid_generation(stateid
, &s
->sc_stateid
, 1);
3741 switch (s
->sc_type
) {
3742 case NFS4_DELEG_STID
:
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
;
3755 printk("unknown stateid type %x\n", s
->sc_type
);
3756 case NFS4_CLOSED_STID
:
3757 return nfserr_bad_stateid
;
3761 static __be32
nfsd4_lookup_stateid(stateid_t
*stateid
, unsigned char typemask
,
3762 struct nfs4_stid
**s
, bool sessions
,
3763 struct nfsd_net
*nn
)
3765 struct nfs4_client
*cl
;
3768 if (ZERO_STATEID(stateid
) || ONE_STATEID(stateid
))
3769 return nfserr_bad_stateid
;
3770 status
= lookup_clientid(&stateid
->si_opaque
.so_clid
, sessions
,
3772 if (status
== nfserr_stale_clientid
) {
3774 return nfserr_bad_stateid
;
3775 return nfserr_stale_stateid
;
3779 *s
= find_stateid_by_type(cl
, stateid
, typemask
);
3781 return nfserr_bad_stateid
;
3786 * Checks for stateid operations
3789 nfs4_preprocess_stateid_op(struct net
*net
, struct nfsd4_compound_state
*cstate
,
3790 stateid_t
*stateid
, int flags
, struct file
**filpp
)
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
;
3804 if (grace_disallows_io(net
, ino
))
3805 return nfserr_grace
;
3807 if (ZERO_STATEID(stateid
) || ONE_STATEID(stateid
))
3808 return check_special_stateids(net
, current_fh
, stateid
, flags
);
3812 status
= nfsd4_lookup_stateid(stateid
,
3813 NFS4_DELEG_STID
|NFS4_OPEN_STID
|NFS4_LOCK_STID
,
3814 &s
, cstate
->minorversion
, nn
);
3817 status
= check_stateid_generation(stateid
, &s
->sc_stateid
, nfsd4_has_session(cstate
));
3820 switch (s
->sc_type
) {
3821 case NFS4_DELEG_STID
:
3822 dp
= delegstateid(s
);
3823 status
= nfs4_check_delegmode(dp
, flags
);
3827 file
= dp
->dl_file
->fi_deleg_file
;
3830 status
= nfserr_serverfault
;
3835 case NFS4_OPEN_STID
:
3836 case NFS4_LOCK_STID
:
3837 stp
= openlockstateid(s
);
3838 status
= nfs4_check_fh(current_fh
, stp
);
3841 if (stp
->st_stateowner
->so_is_open_owner
3842 && !(openowner(stp
->st_stateowner
)->oo_flags
& NFS4_OO_CONFIRMED
))
3844 status
= nfs4_check_openmode(stp
, flags
);
3848 if (flags
& RD_STATE
)
3849 file
= find_readable_file(stp
->st_file
);
3851 file
= find_writeable_file(stp
->st_file
);
3855 status
= nfserr_bad_stateid
;
3860 *filpp
= get_file(file
);
3862 nfs4_unlock_state();
3867 nfsd4_free_lock_stateid(struct nfs4_ol_stateid
*stp
)
3869 struct nfs4_lockowner
*lo
= lockowner(stp
->st_stateowner
);
3871 if (check_for_locks(stp
->st_file
, lo
))
3872 return nfserr_locks_held
;
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:
3878 release_lockowner(lo
);
3883 * Test if the stateid is valid
3886 nfsd4_test_stateid(struct svc_rqst
*rqstp
, struct nfsd4_compound_state
*cstate
,
3887 struct nfsd4_test_stateid
*test_stateid
)
3889 struct nfsd4_test_stateid_id
*stateid
;
3890 struct nfs4_client
*cl
= cstate
->session
->se_client
;
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();
3902 nfsd4_free_stateid(struct svc_rqst
*rqstp
, struct nfsd4_compound_state
*cstate
,
3903 struct nfsd4_free_stateid
*free_stateid
)
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
;
3912 s
= find_stateid(cl
, stateid
);
3915 switch (s
->sc_type
) {
3916 case NFS4_DELEG_STID
:
3917 ret
= nfserr_locks_held
;
3919 case NFS4_OPEN_STID
:
3920 case NFS4_LOCK_STID
:
3921 ret
= check_stateid_generation(stateid
, &s
->sc_stateid
, 1);
3924 if (s
->sc_type
== NFS4_LOCK_STID
)
3925 ret
= nfsd4_free_lock_stateid(openlockstateid(s
));
3927 ret
= nfserr_locks_held
;
3929 case NFS4_REVOKED_DELEG_STID
:
3930 dp
= delegstateid(s
);
3931 destroy_revoked_delegation(dp
);
3935 ret
= nfserr_bad_stateid
;
3938 nfs4_unlock_state();
3945 return (type
== NFS4_READW_LT
|| type
== NFS4_READ_LT
) ?
3946 RD_STATE
: WR_STATE
;
3949 static __be32
nfs4_seqid_op_checks(struct nfsd4_compound_state
*cstate
, stateid_t
*stateid
, u32 seqid
, struct nfs4_ol_stateid
*stp
)
3951 struct svc_fh
*current_fh
= &cstate
->current_fh
;
3952 struct nfs4_stateowner
*sop
= stp
->st_stateowner
;
3955 status
= nfsd4_check_seqid(cstate
, sop
, seqid
);
3958 if (stp
->st_stid
.sc_type
== NFS4_CLOSED_STID
3959 || stp
->st_stid
.sc_type
== NFS4_REVOKED_DELEG_STID
)
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.
3965 return nfserr_bad_stateid
;
3966 status
= check_stateid_generation(stateid
, &stp
->st_stid
.sc_stateid
, nfsd4_has_session(cstate
));
3969 return nfs4_check_fh(current_fh
, stp
);
3973 * Checks for sequence id mutating operations.
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
)
3982 struct nfs4_stid
*s
;
3983 struct nfs4_ol_stateid
*stp
= NULL
;
3985 dprintk("NFSD: %s: seqid=%d stateid = " STATEID_FMT
"\n", __func__
,
3986 seqid
, STATEID_VAL(stateid
));
3989 status
= nfsd4_lookup_stateid(stateid
, typemask
, &s
,
3990 cstate
->minorversion
, nn
);
3993 stp
= openlockstateid(s
);
3994 if (!nfsd4_has_session(cstate
))
3995 cstate
->replay_owner
= stp
->st_stateowner
;
3997 status
= nfs4_seqid_op_checks(cstate
, stateid
, seqid
, stp
);
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
)
4007 struct nfs4_openowner
*oo
;
4009 status
= nfs4_preprocess_seqid_op(cstate
, seqid
, stateid
,
4010 NFS4_OPEN_STID
, stpp
, nn
);
4013 oo
= openowner((*stpp
)->st_stateowner
);
4014 if (!(oo
->oo_flags
& NFS4_OO_CONFIRMED
))
4015 return nfserr_bad_stateid
;
4020 nfsd4_open_confirm(struct svc_rqst
*rqstp
, struct nfsd4_compound_state
*cstate
,
4021 struct nfsd4_open_confirm
*oc
)
4024 struct nfs4_openowner
*oo
;
4025 struct nfs4_ol_stateid
*stp
;
4026 struct nfsd_net
*nn
= net_generic(SVC_NET(rqstp
), nfsd_net_id
);
4028 dprintk("NFSD: nfsd4_open_confirm on file %pd\n",
4029 cstate
->current_fh
.fh_dentry
);
4031 status
= fh_verify(rqstp
, &cstate
->current_fh
, S_IFREG
, 0);
4037 status
= nfs4_preprocess_seqid_op(cstate
,
4038 oc
->oc_seqid
, &oc
->oc_req_stateid
,
4039 NFS4_OPEN_STID
, &stp
, nn
);
4042 oo
= openowner(stp
->st_stateowner
);
4043 status
= nfserr_bad_stateid
;
4044 if (oo
->oo_flags
& NFS4_OO_CONFIRMED
)
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
));
4052 nfsd4_client_record_create(oo
->oo_owner
.so_client
);
4055 nfsd4_bump_seqid(cstate
, status
);
4056 if (!cstate
->replay_owner
)
4057 nfs4_unlock_state();
4061 static inline void nfs4_stateid_downgrade_bit(struct nfs4_ol_stateid
*stp
, u32 access
)
4063 if (!test_access(access
, stp
))
4065 nfs4_file_put_access(stp
->st_file
, nfs4_access_to_omode(access
));
4066 clear_access(access
, stp
);
4069 static inline void nfs4_stateid_downgrade(struct nfs4_ol_stateid
*stp
, u32 to_access
)
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
);
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
);
4080 case NFS4_SHARE_ACCESS_BOTH
:
4088 reset_union_bmap_deny(unsigned long deny
, struct nfs4_ol_stateid
*stp
)
4091 for (i
= 0; i
< 4; i
++) {
4092 if ((i
& deny
) != i
)
4098 nfsd4_open_downgrade(struct svc_rqst
*rqstp
,
4099 struct nfsd4_compound_state
*cstate
,
4100 struct nfsd4_open_downgrade
*od
)
4103 struct nfs4_ol_stateid
*stp
;
4104 struct nfsd_net
*nn
= net_generic(SVC_NET(rqstp
), nfsd_net_id
);
4106 dprintk("NFSD: nfsd4_open_downgrade on file %pd\n",
4107 cstate
->current_fh
.fh_dentry
);
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__
,
4115 status
= nfs4_preprocess_confirmed_seqid_op(cstate
, od
->od_seqid
,
4116 &od
->od_stateid
, &stp
, nn
);
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
);
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
);
4130 nfs4_stateid_downgrade(stp
, od
->od_share_access
);
4132 reset_union_bmap_deny(od
->od_share_deny
, stp
);
4134 update_stateid(&stp
->st_stid
.sc_stateid
);
4135 memcpy(&od
->od_stateid
, &stp
->st_stid
.sc_stateid
, sizeof(stateid_t
));
4138 nfsd4_bump_seqid(cstate
, status
);
4139 if (!cstate
->replay_owner
)
4140 nfs4_unlock_state();
4144 static void nfsd4_close_open_stateid(struct nfs4_ol_stateid
*s
)
4146 struct nfs4_client
*clp
= s
->st_stid
.sc_client
;
4147 struct nfs4_openowner
*oo
= openowner(s
->st_stateowner
);
4149 s
->st_stid
.sc_type
= NFS4_CLOSED_STID
;
4150 unhash_open_stateid(s
);
4152 if (clp
->cl_minorversion
) {
4153 free_generic_stateid(s
);
4154 if (list_empty(&oo
->oo_owner
.so_stateids
))
4155 release_openowner(oo
);
4157 oo
->oo_last_closed_stid
= s
;
4159 * In the 4.0 case we need to keep the owners around a
4160 * little while to handle CLOSE replay.
4162 if (list_empty(&oo
->oo_owner
.so_stateids
))
4163 move_to_close_lru(oo
, clp
->net
);
4168 * nfs4_unlock_state() called after encode
4171 nfsd4_close(struct svc_rqst
*rqstp
, struct nfsd4_compound_state
*cstate
,
4172 struct nfsd4_close
*close
)
4175 struct nfs4_ol_stateid
*stp
;
4176 struct net
*net
= SVC_NET(rqstp
);
4177 struct nfsd_net
*nn
= net_generic(net
, nfsd_net_id
);
4179 dprintk("NFSD: nfsd4_close on file %pd\n",
4180 cstate
->current_fh
.fh_dentry
);
4183 status
= nfs4_preprocess_seqid_op(cstate
, close
->cl_seqid
,
4185 NFS4_OPEN_STID
|NFS4_CLOSED_STID
,
4187 nfsd4_bump_seqid(cstate
, status
);
4190 update_stateid(&stp
->st_stid
.sc_stateid
);
4191 memcpy(&close
->cl_stateid
, &stp
->st_stid
.sc_stateid
, sizeof(stateid_t
));
4193 nfsd4_close_open_stateid(stp
);
4195 if (!cstate
->replay_owner
)
4196 nfs4_unlock_state();
4201 nfsd4_delegreturn(struct svc_rqst
*rqstp
, struct nfsd4_compound_state
*cstate
,
4202 struct nfsd4_delegreturn
*dr
)
4204 struct nfs4_delegation
*dp
;
4205 stateid_t
*stateid
= &dr
->dr_stateid
;
4206 struct nfs4_stid
*s
;
4208 struct nfsd_net
*nn
= net_generic(SVC_NET(rqstp
), nfsd_net_id
);
4210 if ((status
= fh_verify(rqstp
, &cstate
->current_fh
, S_IFREG
, 0)))
4214 status
= nfsd4_lookup_stateid(stateid
, NFS4_DELEG_STID
, &s
,
4215 cstate
->minorversion
, nn
);
4218 dp
= delegstateid(s
);
4219 status
= check_stateid_generation(stateid
, &dp
->dl_stid
.sc_stateid
, nfsd4_has_session(cstate
));
4223 destroy_delegation(dp
);
4225 nfs4_unlock_state();
4231 #define LOFF_OVERFLOW(start, len) ((u64)(len) > ~(u64)(start))
4233 #define LOCKOWNER_INO_HASH_MASK (LOCKOWNER_INO_HASH_SIZE - 1)
4236 end_offset(u64 start
, u64 len
)
4241 return end
>= start
? end
: NFS4_MAX_UINT64
;
4244 /* last octet in a range */
4246 last_byte_offset(u64 start
, u64 len
)
4252 return end
> start
? end
- 1: NFS4_MAX_UINT64
;
4255 static unsigned int lockowner_ino_hashval(struct inode
*inode
, u32 cl_id
, struct xdr_netobj
*ownername
)
4257 return (file_hashval(inode
) + cl_id
4258 + opaque_hashval(ownername
->data
, ownername
->len
))
4259 & LOCKOWNER_INO_HASH_MASK
;
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!
4271 nfs4_transform_lock_offset(struct file_lock
*lock
)
4273 if (lock
->fl_start
< 0)
4274 lock
->fl_start
= OFFSET_MAX
;
4275 if (lock
->fl_end
< 0)
4276 lock
->fl_end
= OFFSET_MAX
;
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
= {
4285 nfs4_set_lock_denied(struct file_lock
*fl
, struct nfsd4_lock_denied
*deny
)
4287 struct nfs4_lockowner
*lo
;
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 */
4296 deny
->ld_owner
.len
= lo
->lo_owner
.so_owner
.len
;
4297 deny
->ld_clientid
= lo
->lo_owner
.so_client
->cl_clientid
;
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;
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
;
4314 static bool same_lockowner_ino(struct nfs4_lockowner
*lo
, struct inode
*inode
, clientid_t
*clid
, struct xdr_netobj
*owner
)
4316 struct nfs4_ol_stateid
*lst
;
4318 if (!same_owner_str(&lo
->lo_owner
, owner
, clid
))
4320 if (list_empty(&lo
->lo_owner
.so_stateids
)) {
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
;
4329 static struct nfs4_lockowner
*
4330 find_lockowner_str(struct inode
*inode
, clientid_t
*clid
,
4331 struct xdr_netobj
*owner
, struct nfsd_net
*nn
)
4333 unsigned int hashval
= lockowner_ino_hashval(inode
, clid
->cl_id
, owner
);
4334 struct nfs4_lockowner
*lo
;
4336 list_for_each_entry(lo
, &nn
->lockowner_ino_hashtbl
[hashval
], lo_owner_ino_hash
) {
4337 if (same_lockowner_ino(lo
, inode
, clid
, owner
))
4343 static void hash_lockowner(struct nfs4_lockowner
*lo
, unsigned int strhashval
, struct nfs4_client
*clp
, struct nfs4_ol_stateid
*open_stp
)
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
);
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
]);
4355 * Alloc a lock owner structure.
4356 * Called in nfsd4_lock - therefore, OPEN and OPEN_CONFIRM (if needed) has
4359 * strhashval = ownerstr_hashval
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
;
4366 lo
= alloc_stateowner(lockowner_slab
, &lock
->lk_new_owner
, clp
);
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
);
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
)
4381 struct nfs4_ol_stateid
*stp
;
4382 struct nfs4_client
*clp
= lo
->lo_owner
.so_client
;
4384 stp
= nfs4_alloc_stateid(clp
);
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
;
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
);
4401 check_lock_length(u64 offset
, u64 length
)
4403 return ((length
== 0) || ((length
!= NFS4_MAX_UINT64
) &&
4404 LOFF_OVERFLOW(offset
, length
)));
4407 static void get_lock_access(struct nfs4_ol_stateid
*lock_stp
, u32 access
)
4409 struct nfs4_file
*fp
= lock_stp
->st_file
;
4410 int oflag
= nfs4_access_to_omode(access
);
4412 if (test_access(access
, lock_stp
))
4414 nfs4_file_get_access(fp
, oflag
);
4415 set_access(access
, lock_stp
);
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)
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
);
4427 lo
= find_lockowner_str(fi
->fi_inode
, &cl
->cl_clientid
,
4428 &lock
->v
.new.owner
, nn
);
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
);
4437 strhashval
= ownerstr_hashval(cl
->cl_clientid
.cl_id
,
4438 &lock
->v
.new.owner
);
4439 lo
= alloc_init_lock_stateowner(strhashval
, cl
, ost
, lock
);
4441 return nfserr_jukebox
;
4442 *lst
= alloc_init_lock_stateid(lo
, fi
, ost
);
4444 release_lockowner(lo
);
4445 return nfserr_jukebox
;
4455 nfsd4_lock(struct svc_rqst
*rqstp
, struct nfsd4_compound_state
*cstate
,
4456 struct nfsd4_lock
*lock
)
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
;
4465 bool new_state
= false;
4468 struct net
*net
= SVC_NET(rqstp
);
4469 struct nfsd_net
*nn
= net_generic(net
, nfsd_net_id
);
4471 dprintk("NFSD: nfsd4_lock: start=%Ld length=%Ld\n",
4472 (long long) lock
->lk_offset
,
4473 (long long) lock
->lk_length
);
4475 if (check_lock_length(lock
->lk_offset
, lock
->lk_length
))
4476 return nfserr_inval
;
4478 if ((status
= fh_verify(rqstp
, &cstate
->current_fh
,
4479 S_IFREG
, NFSD_MAY_LOCK
))) {
4480 dprintk("NFSD: nfsd4_lock: permission denied!\n");
4486 if (lock
->lk_is_new
) {
4487 struct nfs4_ol_stateid
*open_stp
= NULL
;
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
));
4495 status
= nfserr_stale_clientid
;
4496 if (STALE_CLIENTID(&lock
->lk_new_clientid
, nn
))
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
,
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
))
4511 status
= lookup_or_create_lock_state(cstate
, open_stp
, lock
,
4512 &lock_stp
, &new_state
);
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
);
4520 lock_sop
= lockowner(lock_stp
->st_stateowner
);
4522 lkflg
= setlkflg(lock
->lk_type
);
4523 status
= nfs4_check_openmode(lock_stp
, lkflg
);
4527 status
= nfserr_grace
;
4528 if (locks_in_grace(net
) && !lock
->lk_reclaim
)
4530 status
= nfserr_no_grace
;
4531 if (!locks_in_grace(net
) && lock
->lk_reclaim
)
4534 file_lock
= locks_alloc_lock();
4536 dprintk("NFSD: %s: unable to allocate lock!\n", __func__
);
4537 status
= nfserr_jukebox
;
4541 locks_init_lock(file_lock
);
4542 switch (lock
->lk_type
) {
4545 filp
= find_readable_file(lock_stp
->st_file
);
4547 get_lock_access(lock_stp
, NFS4_SHARE_ACCESS_READ
);
4548 file_lock
->fl_type
= F_RDLCK
;
4551 case NFS4_WRITEW_LT
:
4552 filp
= find_writeable_file(lock_stp
->st_file
);
4554 get_lock_access(lock_stp
, NFS4_SHARE_ACCESS_WRITE
);
4555 file_lock
->fl_type
= F_WRLCK
;
4558 status
= nfserr_inval
;
4562 status
= nfserr_openmode
;
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
);
4574 conflock
= locks_alloc_lock();
4576 dprintk("NFSD: %s: unable to allocate lock!\n", __func__
);
4577 status
= nfserr_jukebox
;
4581 err
= vfs_lock_file(filp
, F_SETLK
, file_lock
, conflock
);
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
,
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
);
4595 status
= nfserr_deadlock
;
4598 dprintk("NFSD: nfsd4_lock: vfs_lock_file() failed! status %d\n",err
);
4599 status
= nfserrno(err
);
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();
4609 locks_free_lock(file_lock
);
4611 locks_free_lock(conflock
);
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
4621 static __be32
nfsd_test_lock(struct svc_rqst
*rqstp
, struct svc_fh
*fhp
, struct file_lock
*lock
)
4624 __be32 err
= nfsd_open(rqstp
, fhp
, S_IFREG
, NFSD_MAY_READ
, &file
);
4626 err
= nfserrno(vfs_test_lock(file
, lock
));
4636 nfsd4_lockt(struct svc_rqst
*rqstp
, struct nfsd4_compound_state
*cstate
,
4637 struct nfsd4_lockt
*lockt
)
4639 struct inode
*inode
;
4640 struct file_lock
*file_lock
= NULL
;
4641 struct nfs4_lockowner
*lo
;
4643 struct nfsd_net
*nn
= net_generic(SVC_NET(rqstp
), nfsd_net_id
);
4645 if (locks_in_grace(SVC_NET(rqstp
)))
4646 return nfserr_grace
;
4648 if (check_lock_length(lockt
->lt_offset
, lockt
->lt_length
))
4649 return nfserr_inval
;
4653 if (!nfsd4_has_session(cstate
)) {
4654 status
= lookup_clientid(&lockt
->lt_clientid
, false, nn
, NULL
);
4659 if ((status
= fh_verify(rqstp
, &cstate
->current_fh
, S_IFREG
, 0)))
4662 inode
= cstate
->current_fh
.fh_dentry
->d_inode
;
4663 file_lock
= locks_alloc_lock();
4665 dprintk("NFSD: %s: unable to allocate lock!\n", __func__
);
4666 status
= nfserr_jukebox
;
4669 locks_init_lock(file_lock
);
4670 switch (lockt
->lt_type
) {
4673 file_lock
->fl_type
= F_RDLCK
;
4676 case NFS4_WRITEW_LT
:
4677 file_lock
->fl_type
= F_WRLCK
;
4680 dprintk("NFSD: nfs4_lockt: bad lock type!\n");
4681 status
= nfserr_inval
;
4685 lo
= find_lockowner_str(inode
, &lockt
->lt_clientid
, &lockt
->lt_owner
, nn
);
4687 file_lock
->fl_owner
= (fl_owner_t
)lo
;
4688 file_lock
->fl_pid
= current
->tgid
;
4689 file_lock
->fl_flags
= FL_POSIX
;
4691 file_lock
->fl_start
= lockt
->lt_offset
;
4692 file_lock
->fl_end
= last_byte_offset(lockt
->lt_offset
, lockt
->lt_length
);
4694 nfs4_transform_lock_offset(file_lock
);
4696 status
= nfsd_test_lock(rqstp
, &cstate
->current_fh
, file_lock
);
4700 if (file_lock
->fl_type
!= F_UNLCK
) {
4701 status
= nfserr_denied
;
4702 nfs4_set_lock_denied(file_lock
, &lockt
->lt_denied
);
4705 nfs4_unlock_state();
4707 locks_free_lock(file_lock
);
4712 nfsd4_locku(struct svc_rqst
*rqstp
, struct nfsd4_compound_state
*cstate
,
4713 struct nfsd4_locku
*locku
)
4715 struct nfs4_ol_stateid
*stp
;
4716 struct file
*filp
= NULL
;
4717 struct file_lock
*file_lock
= NULL
;
4720 struct nfsd_net
*nn
= net_generic(SVC_NET(rqstp
), nfsd_net_id
);
4722 dprintk("NFSD: nfsd4_locku: start=%Ld length=%Ld\n",
4723 (long long) locku
->lu_offset
,
4724 (long long) locku
->lu_length
);
4726 if (check_lock_length(locku
->lu_offset
, locku
->lu_length
))
4727 return nfserr_inval
;
4731 status
= nfs4_preprocess_seqid_op(cstate
, locku
->lu_seqid
,
4732 &locku
->lu_stateid
, NFS4_LOCK_STID
,
4736 filp
= find_any_file(stp
->st_file
);
4738 status
= nfserr_lock_range
;
4741 file_lock
= locks_alloc_lock();
4743 dprintk("NFSD: %s: unable to allocate lock!\n", __func__
);
4744 status
= nfserr_jukebox
;
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
;
4756 file_lock
->fl_end
= last_byte_offset(locku
->lu_offset
,
4758 nfs4_transform_lock_offset(file_lock
);
4760 err
= vfs_lock_file(filp
, F_SETLK
, file_lock
, NULL
);
4762 dprintk("NFSD: nfs4_locku: vfs_lock_file failed!\n");
4765 update_stateid(&stp
->st_stid
.sc_stateid
);
4766 memcpy(&locku
->lu_stateid
, &stp
->st_stid
.sc_stateid
, sizeof(stateid_t
));
4769 nfsd4_bump_seqid(cstate
, status
);
4770 if (!cstate
->replay_owner
)
4771 nfs4_unlock_state();
4773 locks_free_lock(file_lock
);
4777 status
= nfserrno(err
);
4783 * 1: locks held by lockowner
4784 * 0: no locks held by lockowner
4787 check_for_locks(struct nfs4_file
*filp
, struct nfs4_lockowner
*lowner
)
4789 struct file_lock
**flpp
;
4790 struct inode
*inode
= filp
->fi_inode
;
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
) {
4801 spin_unlock(&inode
->i_lock
);
4806 nfsd4_release_lockowner(struct svc_rqst
*rqstp
,
4807 struct nfsd4_compound_state
*cstate
,
4808 struct nfsd4_release_lockowner
*rlockowner
)
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
);
4818 struct nfsd_net
*nn
= net_generic(SVC_NET(rqstp
), nfsd_net_id
);
4820 dprintk("nfsd4_release_lockowner clientid: (%08x/%08x):\n",
4821 clid
->cl_boot
, clid
->cl_id
);
4825 status
= lookup_clientid(clid
, cstate
->minorversion
, nn
, NULL
);
4829 status
= nfserr_locks_held
;
4830 INIT_LIST_HEAD(&matches
);
4832 list_for_each_entry(sop
, &nn
->ownerstr_hashtbl
[hashval
], so_strhash
) {
4833 if (sop
->so_is_open_owner
)
4835 if (!same_owner_str(sop
, owner
, clid
))
4837 list_for_each_entry(stp
, &sop
->so_stateids
,
4839 lo
= lockowner(sop
);
4840 if (check_for_locks(stp
->st_file
, lo
))
4842 list_add(&lo
->lo_list
, &matches
);
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. */
4849 while (!list_empty(&matches
)) {
4850 lo
= list_entry(matches
.next
, struct nfs4_lockowner
,
4852 /* unhash_stateowner deletes so_perclient only
4853 * for openowners. */
4854 list_del(&lo
->lo_list
);
4855 release_lockowner(lo
);
4858 nfs4_unlock_state();
4862 static inline struct nfs4_client_reclaim
*
4865 return kmalloc(sizeof(struct nfs4_client_reclaim
), GFP_KERNEL
);
4869 nfs4_has_reclaimed_state(const char *name
, struct nfsd_net
*nn
)
4871 struct nfs4_client_reclaim
*crp
;
4873 crp
= nfsd4_find_reclaim_client(name
, nn
);
4874 return (crp
&& crp
->cr_clp
);
4878 * failure => all reset bets are off, nfserr_no_grace...
4880 struct nfs4_client_reclaim
*
4881 nfs4_client_to_reclaim(const char *name
, struct nfsd_net
*nn
)
4883 unsigned int strhashval
;
4884 struct nfs4_client_reclaim
*crp
;
4886 dprintk("NFSD nfs4_client_to_reclaim NAME: %.*s\n", HEXDIR_LEN
, name
);
4887 crp
= alloc_reclaim();
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
);
4894 nn
->reclaim_str_hashtbl_size
++;
4900 nfs4_remove_reclaim_record(struct nfs4_client_reclaim
*crp
, struct nfsd_net
*nn
)
4902 list_del(&crp
->cr_strhash
);
4904 nn
->reclaim_str_hashtbl_size
--;
4908 nfs4_release_reclaim(struct nfsd_net
*nn
)
4910 struct nfs4_client_reclaim
*crp
= NULL
;
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
);
4920 WARN_ON_ONCE(nn
->reclaim_str_hashtbl_size
);
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
)
4928 unsigned int strhashval
;
4929 struct nfs4_client_reclaim
*crp
= NULL
;
4931 dprintk("NFSD: nfs4_find_reclaim_client for recdir %s\n", recdir
);
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
)) {
4943 * Called from OPEN. Look for clientid in reclaim list.
4946 nfs4_check_open_reclaim(clientid_t
*clid
, bool sessions
, struct nfsd_net
*nn
)
4948 struct nfs4_client
*clp
;
4950 /* find clientid in conf_id_hashtbl */
4951 clp
= find_confirmed_client(clid
, sessions
, nn
);
4953 return nfserr_reclaim_bad
;
4955 return nfsd4_client_record_check(clp
) ? nfserr_reclaim_bad
: nfs_ok
;
4958 #ifdef CONFIG_NFSD_FAULT_INJECTION
4960 u64
nfsd_forget_client(struct nfs4_client
*clp
, u64 max
)
4962 if (mark_client_expired(clp
))
4968 u64
nfsd_print_client(struct nfs4_client
*clp
, u64 num
)
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
);
4976 static void nfsd_print_count(struct nfs4_client
*clp
, unsigned int count
,
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
);
4984 static u64
nfsd_foreach_client_lock(struct nfs4_client
*clp
, u64 max
,
4985 void (*func
)(struct nfs4_ol_stateid
*))
4987 struct nfs4_openowner
*oop
;
4988 struct nfs4_ol_stateid
*stp
, *st_next
;
4989 struct nfs4_ol_stateid
*lst
, *lst_next
;
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
) {
5008 u64
nfsd_forget_client_locks(struct nfs4_client
*clp
, u64 max
)
5010 return nfsd_foreach_client_lock(clp
, max
, release_lock_stateid
);
5013 u64
nfsd_print_client_locks(struct nfs4_client
*clp
, u64 max
)
5015 u64 count
= nfsd_foreach_client_lock(clp
, max
, NULL
);
5016 nfsd_print_count(clp
, count
, "locked files");
5020 static u64
nfsd_foreach_client_open(struct nfs4_client
*clp
, u64 max
, void (*func
)(struct nfs4_openowner
*))
5022 struct nfs4_openowner
*oop
, *next
;
5025 list_for_each_entry_safe(oop
, next
, &clp
->cl_openowners
, oo_perclient
) {
5035 u64
nfsd_forget_client_openowners(struct nfs4_client
*clp
, u64 max
)
5037 return nfsd_foreach_client_open(clp
, max
, release_openowner
);
5040 u64
nfsd_print_client_openowners(struct nfs4_client
*clp
, u64 max
)
5042 u64 count
= nfsd_foreach_client_open(clp
, max
, NULL
);
5043 nfsd_print_count(clp
, count
, "open files");
5047 static u64
nfsd_find_all_delegations(struct nfs4_client
*clp
, u64 max
,
5048 struct list_head
*victims
)
5050 struct nfs4_delegation
*dp
, *next
;
5053 lockdep_assert_held(&state_lock
);
5054 list_for_each_entry_safe(dp
, next
, &clp
->cl_delegations
, dl_perclnt
) {
5056 list_move(&dp
->dl_recall_lru
, victims
);
5063 u64
nfsd_forget_client_delegations(struct nfs4_client
*clp
, u64 max
)
5065 struct nfs4_delegation
*dp
, *next
;
5069 spin_lock(&state_lock
);
5070 count
= nfsd_find_all_delegations(clp
, max
, &victims
);
5071 spin_unlock(&state_lock
);
5073 list_for_each_entry_safe(dp
, next
, &victims
, dl_recall_lru
)
5074 revoke_delegation(dp
);
5079 u64
nfsd_recall_client_delegations(struct nfs4_client
*clp
, u64 max
)
5081 struct nfs4_delegation
*dp
, *next
;
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
);
5094 u64
nfsd_print_client_delegations(struct nfs4_client
*clp
, u64 max
)
5098 spin_lock(&state_lock
);
5099 count
= nfsd_find_all_delegations(clp
, max
, NULL
);
5100 spin_unlock(&state_lock
);
5102 nfsd_print_count(clp
, count
, "delegations");
5106 u64
nfsd_for_n_state(u64 max
, u64 (*func
)(struct nfs4_client
*, u64
))
5108 struct nfs4_client
*clp
, *next
;
5110 struct nfsd_net
*nn
= net_generic(current
->nsproxy
->net_ns
, nfsd_net_id
);
5112 if (!nfsd_netns_ready(nn
))
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
))
5124 struct nfs4_client
*nfsd_find_client(struct sockaddr_storage
*addr
, size_t addr_size
)
5126 struct nfs4_client
*clp
;
5127 struct nfsd_net
*nn
= net_generic(current
->nsproxy
->net_ns
, nfsd_net_id
);
5129 if (!nfsd_netns_ready(nn
))
5132 list_for_each_entry(clp
, &nn
->client_lru
, cl_lru
) {
5133 if (memcmp(&clp
->cl_addr
, addr
, addr_size
) == 0)
5139 #endif /* CONFIG_NFSD_FAULT_INJECTION */
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.
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.
5151 set_max_delegations(void)
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.
5159 max_delegations
= nr_free_buffer_pages() >> (20 - 2 - PAGE_SHIFT
);
5162 static int nfs4_state_create_net(struct net
*net
)
5164 struct nfsd_net
*nn
= net_generic(net
, nfsd_net_id
);
5167 nn
->conf_id_hashtbl
= kmalloc(sizeof(struct list_head
) *
5168 CLIENT_HASH_SIZE
, GFP_KERNEL
);
5169 if (!nn
->conf_id_hashtbl
)
5171 nn
->unconf_id_hashtbl
= kmalloc(sizeof(struct list_head
) *
5172 CLIENT_HASH_SIZE
, GFP_KERNEL
);
5173 if (!nn
->unconf_id_hashtbl
)
5175 nn
->ownerstr_hashtbl
= kmalloc(sizeof(struct list_head
) *
5176 OWNER_HASH_SIZE
, GFP_KERNEL
);
5177 if (!nn
->ownerstr_hashtbl
)
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
)
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
]);
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
);
5205 INIT_DELAYED_WORK(&nn
->laundromat_work
, laundromat_main
);
5211 kfree(nn
->lockowner_ino_hashtbl
);
5213 kfree(nn
->ownerstr_hashtbl
);
5215 kfree(nn
->unconf_id_hashtbl
);
5217 kfree(nn
->conf_id_hashtbl
);
5223 nfs4_state_destroy_net(struct net
*net
)
5226 struct nfs4_client
*clp
= NULL
;
5227 struct nfsd_net
*nn
= net_generic(net
, nfsd_net_id
);
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
);
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
);
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
);
5252 nfs4_state_start_net(struct net
*net
)
5254 struct nfsd_net
*nn
= net_generic(net
, nfsd_net_id
);
5257 ret
= nfs4_state_create_net(net
);
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
);
5270 /* initialization to perform when the nfsd service is started: */
5273 nfs4_state_start(void)
5277 ret
= set_callback_cred();
5280 laundry_wq
= create_singlethread_workqueue("nfsd4");
5281 if (laundry_wq
== NULL
) {
5285 ret
= nfsd4_create_callback_queue();
5287 goto out_free_laundry
;
5289 set_max_delegations();
5294 destroy_workqueue(laundry_wq
);
5300 nfs4_state_shutdown_net(struct net
*net
)
5302 struct nfs4_delegation
*dp
= NULL
;
5303 struct list_head
*pos
, *next
, reaplist
;
5304 struct nfsd_net
*nn
= net_generic(net
, nfsd_net_id
);
5306 cancel_delayed_work_sync(&nn
->laundromat_work
);
5307 locks_end_grace(&nn
->nfsd4_manager
);
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
);
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
);
5322 nfsd4_client_tracking_exit(net
);
5323 nfs4_state_destroy_net(net
);
5324 nfs4_unlock_state();
5328 nfs4_state_shutdown(void)
5330 destroy_workqueue(laundry_wq
);
5331 nfsd4_destroy_callback_queue();
5335 get_stateid(struct nfsd4_compound_state
*cstate
, stateid_t
*stateid
)
5337 if (HAS_STATE_ID(cstate
, CURRENT_STATE_ID_FLAG
) && CURRENT_STATEID(stateid
))
5338 memcpy(stateid
, &cstate
->current_stateid
, sizeof(stateid_t
));
5342 put_stateid(struct nfsd4_compound_state
*cstate
, stateid_t
*stateid
)
5344 if (cstate
->minorversion
) {
5345 memcpy(&cstate
->current_stateid
, stateid
, sizeof(stateid_t
));
5346 SET_STATE_ID(cstate
, CURRENT_STATE_ID_FLAG
);
5351 clear_current_stateid(struct nfsd4_compound_state
*cstate
)
5353 CLEAR_STATE_ID(cstate
, CURRENT_STATE_ID_FLAG
);
5357 * functions to set current state id
5360 nfsd4_set_opendowngradestateid(struct nfsd4_compound_state
*cstate
, struct nfsd4_open_downgrade
*odp
)
5362 put_stateid(cstate
, &odp
->od_stateid
);
5366 nfsd4_set_openstateid(struct nfsd4_compound_state
*cstate
, struct nfsd4_open
*open
)
5368 put_stateid(cstate
, &open
->op_stateid
);
5372 nfsd4_set_closestateid(struct nfsd4_compound_state
*cstate
, struct nfsd4_close
*close
)
5374 put_stateid(cstate
, &close
->cl_stateid
);
5378 nfsd4_set_lockstateid(struct nfsd4_compound_state
*cstate
, struct nfsd4_lock
*lock
)
5380 put_stateid(cstate
, &lock
->lk_resp_stateid
);
5384 * functions to consume current state id
5388 nfsd4_get_opendowngradestateid(struct nfsd4_compound_state
*cstate
, struct nfsd4_open_downgrade
*odp
)
5390 get_stateid(cstate
, &odp
->od_stateid
);
5394 nfsd4_get_delegreturnstateid(struct nfsd4_compound_state
*cstate
, struct nfsd4_delegreturn
*drp
)
5396 get_stateid(cstate
, &drp
->dr_stateid
);
5400 nfsd4_get_freestateid(struct nfsd4_compound_state
*cstate
, struct nfsd4_free_stateid
*fsp
)
5402 get_stateid(cstate
, &fsp
->fr_stateid
);
5406 nfsd4_get_setattrstateid(struct nfsd4_compound_state
*cstate
, struct nfsd4_setattr
*setattr
)
5408 get_stateid(cstate
, &setattr
->sa_stateid
);
5412 nfsd4_get_closestateid(struct nfsd4_compound_state
*cstate
, struct nfsd4_close
*close
)
5414 get_stateid(cstate
, &close
->cl_stateid
);
5418 nfsd4_get_lockustateid(struct nfsd4_compound_state
*cstate
, struct nfsd4_locku
*locku
)
5420 get_stateid(cstate
, &locku
->lu_stateid
);
5424 nfsd4_get_readstateid(struct nfsd4_compound_state
*cstate
, struct nfsd4_read
*read
)
5426 get_stateid(cstate
, &read
->rd_stateid
);
5430 nfsd4_get_writestateid(struct nfsd4_compound_state
*cstate
, struct nfsd4_write
*write
)
5432 get_stateid(cstate
, &write
->wr_stateid
);