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 static __be32
mark_session_dead_locked(struct nfsd4_session
*ses
, int ref_held_by_me
)
108 if (atomic_read(&ses
->se_ref
) > ref_held_by_me
)
109 return nfserr_jukebox
;
110 ses
->se_flags
|= NFS4_SESSION_DEAD
;
115 nfs4_unlock_state(void)
117 mutex_unlock(&client_mutex
);
120 static bool is_client_expired(struct nfs4_client
*clp
)
122 return clp
->cl_time
== 0;
125 static __be32
mark_client_expired_locked(struct nfs4_client
*clp
)
127 if (atomic_read(&clp
->cl_refcount
))
128 return nfserr_jukebox
;
133 static __be32
mark_client_expired(struct nfs4_client
*clp
)
135 struct nfsd_net
*nn
= net_generic(clp
->net
, nfsd_net_id
);
138 spin_lock(&nn
->client_lock
);
139 ret
= mark_client_expired_locked(clp
);
140 spin_unlock(&nn
->client_lock
);
144 static __be32
get_client_locked(struct nfs4_client
*clp
)
146 if (is_client_expired(clp
))
147 return nfserr_expired
;
148 atomic_inc(&clp
->cl_refcount
);
152 /* must be called under the client_lock */
154 renew_client_locked(struct nfs4_client
*clp
)
156 struct nfsd_net
*nn
= net_generic(clp
->net
, nfsd_net_id
);
158 if (is_client_expired(clp
)) {
160 printk("%s: client (clientid %08x/%08x) already expired\n",
162 clp
->cl_clientid
.cl_boot
,
163 clp
->cl_clientid
.cl_id
);
167 dprintk("renewing client (clientid %08x/%08x)\n",
168 clp
->cl_clientid
.cl_boot
,
169 clp
->cl_clientid
.cl_id
);
170 list_move_tail(&clp
->cl_lru
, &nn
->client_lru
);
171 clp
->cl_time
= get_seconds();
175 renew_client(struct nfs4_client
*clp
)
177 struct nfsd_net
*nn
= net_generic(clp
->net
, nfsd_net_id
);
179 spin_lock(&nn
->client_lock
);
180 renew_client_locked(clp
);
181 spin_unlock(&nn
->client_lock
);
184 static void put_client_renew_locked(struct nfs4_client
*clp
)
186 if (!atomic_dec_and_test(&clp
->cl_refcount
))
188 if (!is_client_expired(clp
))
189 renew_client_locked(clp
);
192 static void put_client_renew(struct nfs4_client
*clp
)
194 struct nfsd_net
*nn
= net_generic(clp
->net
, nfsd_net_id
);
196 if (!atomic_dec_and_lock(&clp
->cl_refcount
, &nn
->client_lock
))
198 if (!is_client_expired(clp
))
199 renew_client_locked(clp
);
200 spin_unlock(&nn
->client_lock
);
203 static __be32
nfsd4_get_session_locked(struct nfsd4_session
*ses
)
207 if (is_session_dead(ses
))
208 return nfserr_badsession
;
209 status
= get_client_locked(ses
->se_client
);
212 atomic_inc(&ses
->se_ref
);
216 static void nfsd4_put_session_locked(struct nfsd4_session
*ses
)
218 struct nfs4_client
*clp
= ses
->se_client
;
220 if (atomic_dec_and_test(&ses
->se_ref
) && is_session_dead(ses
))
222 put_client_renew_locked(clp
);
225 static void nfsd4_put_session(struct nfsd4_session
*ses
)
227 struct nfs4_client
*clp
= ses
->se_client
;
228 struct nfsd_net
*nn
= net_generic(clp
->net
, nfsd_net_id
);
230 spin_lock(&nn
->client_lock
);
231 nfsd4_put_session_locked(ses
);
232 spin_unlock(&nn
->client_lock
);
237 opaque_hashval(const void *ptr
, int nbytes
)
239 unsigned char *cptr
= (unsigned char *) ptr
;
249 static void nfsd4_free_file(struct nfs4_file
*f
)
251 kmem_cache_free(file_slab
, f
);
255 put_nfs4_file(struct nfs4_file
*fi
)
257 might_lock(&state_lock
);
259 if (atomic_dec_and_lock(&fi
->fi_ref
, &state_lock
)) {
260 hlist_del(&fi
->fi_hash
);
261 spin_unlock(&state_lock
);
268 get_nfs4_file(struct nfs4_file
*fi
)
270 atomic_inc(&fi
->fi_ref
);
274 __nfs4_get_fd(struct nfs4_file
*f
, int oflag
)
276 if (f
->fi_fds
[oflag
])
277 return get_file(f
->fi_fds
[oflag
]);
282 find_writeable_file_locked(struct nfs4_file
*f
)
286 lockdep_assert_held(&f
->fi_lock
);
288 ret
= __nfs4_get_fd(f
, O_WRONLY
);
290 ret
= __nfs4_get_fd(f
, O_RDWR
);
295 find_writeable_file(struct nfs4_file
*f
)
299 spin_lock(&f
->fi_lock
);
300 ret
= find_writeable_file_locked(f
);
301 spin_unlock(&f
->fi_lock
);
306 static struct file
*find_readable_file_locked(struct nfs4_file
*f
)
310 lockdep_assert_held(&f
->fi_lock
);
312 ret
= __nfs4_get_fd(f
, O_RDONLY
);
314 ret
= __nfs4_get_fd(f
, O_RDWR
);
319 find_readable_file(struct nfs4_file
*f
)
323 spin_lock(&f
->fi_lock
);
324 ret
= find_readable_file_locked(f
);
325 spin_unlock(&f
->fi_lock
);
331 find_any_file(struct nfs4_file
*f
)
335 spin_lock(&f
->fi_lock
);
336 ret
= __nfs4_get_fd(f
, O_RDWR
);
338 ret
= __nfs4_get_fd(f
, O_WRONLY
);
340 ret
= __nfs4_get_fd(f
, O_RDONLY
);
342 spin_unlock(&f
->fi_lock
);
346 static int num_delegations
;
347 unsigned long max_delegations
;
350 * Open owner state (share locks)
353 /* hash tables for lock and open owners */
354 #define OWNER_HASH_BITS 8
355 #define OWNER_HASH_SIZE (1 << OWNER_HASH_BITS)
356 #define OWNER_HASH_MASK (OWNER_HASH_SIZE - 1)
358 static unsigned int ownerstr_hashval(u32 clientid
, struct xdr_netobj
*ownername
)
362 ret
= opaque_hashval(ownername
->data
, ownername
->len
);
364 return ret
& OWNER_HASH_MASK
;
367 /* hash table for nfs4_file */
368 #define FILE_HASH_BITS 8
369 #define FILE_HASH_SIZE (1 << FILE_HASH_BITS)
371 static unsigned int nfsd_fh_hashval(struct knfsd_fh
*fh
)
373 return jhash2(fh
->fh_base
.fh_pad
, XDR_QUADLEN(fh
->fh_size
), 0);
376 static unsigned int file_hashval(struct knfsd_fh
*fh
)
378 return nfsd_fh_hashval(fh
) & (FILE_HASH_SIZE
- 1);
381 static bool nfsd_fh_match(struct knfsd_fh
*fh1
, struct knfsd_fh
*fh2
)
383 return fh1
->fh_size
== fh2
->fh_size
&&
384 !memcmp(fh1
->fh_base
.fh_pad
,
389 static struct hlist_head file_hashtbl
[FILE_HASH_SIZE
];
392 __nfs4_file_get_access(struct nfs4_file
*fp
, u32 access
)
394 lockdep_assert_held(&fp
->fi_lock
);
396 if (access
& NFS4_SHARE_ACCESS_WRITE
)
397 atomic_inc(&fp
->fi_access
[O_WRONLY
]);
398 if (access
& NFS4_SHARE_ACCESS_READ
)
399 atomic_inc(&fp
->fi_access
[O_RDONLY
]);
403 nfs4_file_get_access(struct nfs4_file
*fp
, u32 access
)
405 lockdep_assert_held(&fp
->fi_lock
);
407 /* Does this access mode make sense? */
408 if (access
& ~NFS4_SHARE_ACCESS_BOTH
)
411 /* Does it conflict with a deny mode already set? */
412 if ((access
& fp
->fi_share_deny
) != 0)
413 return nfserr_share_denied
;
415 __nfs4_file_get_access(fp
, access
);
419 static __be32
nfs4_file_check_deny(struct nfs4_file
*fp
, u32 deny
)
421 /* Common case is that there is no deny mode. */
423 /* Does this deny mode make sense? */
424 if (deny
& ~NFS4_SHARE_DENY_BOTH
)
427 if ((deny
& NFS4_SHARE_DENY_READ
) &&
428 atomic_read(&fp
->fi_access
[O_RDONLY
]))
429 return nfserr_share_denied
;
431 if ((deny
& NFS4_SHARE_DENY_WRITE
) &&
432 atomic_read(&fp
->fi_access
[O_WRONLY
]))
433 return nfserr_share_denied
;
438 static void __nfs4_file_put_access(struct nfs4_file
*fp
, int oflag
)
440 might_lock(&fp
->fi_lock
);
442 if (atomic_dec_and_lock(&fp
->fi_access
[oflag
], &fp
->fi_lock
)) {
443 struct file
*f1
= NULL
;
444 struct file
*f2
= NULL
;
446 swap(f1
, fp
->fi_fds
[oflag
]);
447 if (atomic_read(&fp
->fi_access
[1 - oflag
]) == 0)
448 swap(f2
, fp
->fi_fds
[O_RDWR
]);
449 spin_unlock(&fp
->fi_lock
);
457 static void nfs4_file_put_access(struct nfs4_file
*fp
, u32 access
)
459 WARN_ON_ONCE(access
& ~NFS4_SHARE_ACCESS_BOTH
);
461 if (access
& NFS4_SHARE_ACCESS_WRITE
)
462 __nfs4_file_put_access(fp
, O_WRONLY
);
463 if (access
& NFS4_SHARE_ACCESS_READ
)
464 __nfs4_file_put_access(fp
, O_RDONLY
);
467 static struct nfs4_stid
*nfs4_alloc_stid(struct nfs4_client
*cl
, struct
470 struct idr
*stateids
= &cl
->cl_stateids
;
471 struct nfs4_stid
*stid
;
474 stid
= kmem_cache_alloc(slab
, GFP_KERNEL
);
478 new_id
= idr_alloc_cyclic(stateids
, stid
, 0, 0, GFP_KERNEL
);
481 stid
->sc_client
= cl
;
483 stid
->sc_stateid
.si_opaque
.so_id
= new_id
;
484 stid
->sc_stateid
.si_opaque
.so_clid
= cl
->cl_clientid
;
485 /* Will be incremented before return to client: */
486 stid
->sc_stateid
.si_generation
= 0;
487 atomic_set(&stid
->sc_count
, 1);
490 * It shouldn't be a problem to reuse an opaque stateid value.
491 * I don't think it is for 4.1. But with 4.0 I worry that, for
492 * example, a stray write retransmission could be accepted by
493 * the server when it should have been rejected. Therefore,
494 * adopt a trick from the sctp code to attempt to maximize the
495 * amount of time until an id is reused, by ensuring they always
496 * "increase" (mod INT_MAX):
500 kmem_cache_free(slab
, stid
);
504 static struct nfs4_ol_stateid
* nfs4_alloc_stateid(struct nfs4_client
*clp
)
506 return openlockstateid(nfs4_alloc_stid(clp
, stateid_slab
));
510 * When we recall a delegation, we should be careful not to hand it
511 * out again straight away.
512 * To ensure this we keep a pair of bloom filters ('new' and 'old')
513 * in which the filehandles of recalled delegations are "stored".
514 * If a filehandle appear in either filter, a delegation is blocked.
515 * When a delegation is recalled, the filehandle is stored in the "new"
517 * Every 30 seconds we swap the filters and clear the "new" one,
518 * unless both are empty of course.
520 * Each filter is 256 bits. We hash the filehandle to 32bit and use the
521 * low 3 bytes as hash-table indices.
523 * 'state_lock', which is always held when block_delegations() is called,
524 * is used to manage concurrent access. Testing does not need the lock
525 * except when swapping the two filters.
527 static struct bloom_pair
{
528 int entries
, old_entries
;
530 int new; /* index into 'set' */
531 DECLARE_BITMAP(set
[2], 256);
532 } blocked_delegations
;
534 static int delegation_blocked(struct knfsd_fh
*fh
)
537 struct bloom_pair
*bd
= &blocked_delegations
;
539 if (bd
->entries
== 0)
541 if (seconds_since_boot() - bd
->swap_time
> 30) {
542 spin_lock(&state_lock
);
543 if (seconds_since_boot() - bd
->swap_time
> 30) {
544 bd
->entries
-= bd
->old_entries
;
545 bd
->old_entries
= bd
->entries
;
546 memset(bd
->set
[bd
->new], 0,
549 bd
->swap_time
= seconds_since_boot();
551 spin_unlock(&state_lock
);
553 hash
= arch_fast_hash(&fh
->fh_base
, fh
->fh_size
, 0);
554 if (test_bit(hash
&255, bd
->set
[0]) &&
555 test_bit((hash
>>8)&255, bd
->set
[0]) &&
556 test_bit((hash
>>16)&255, bd
->set
[0]))
559 if (test_bit(hash
&255, bd
->set
[1]) &&
560 test_bit((hash
>>8)&255, bd
->set
[1]) &&
561 test_bit((hash
>>16)&255, bd
->set
[1]))
567 static void block_delegations(struct knfsd_fh
*fh
)
570 struct bloom_pair
*bd
= &blocked_delegations
;
572 lockdep_assert_held(&state_lock
);
574 hash
= arch_fast_hash(&fh
->fh_base
, fh
->fh_size
, 0);
576 __set_bit(hash
&255, bd
->set
[bd
->new]);
577 __set_bit((hash
>>8)&255, bd
->set
[bd
->new]);
578 __set_bit((hash
>>16)&255, bd
->set
[bd
->new]);
579 if (bd
->entries
== 0)
580 bd
->swap_time
= seconds_since_boot();
584 static struct nfs4_delegation
*
585 alloc_init_deleg(struct nfs4_client
*clp
, struct nfs4_ol_stateid
*stp
, struct svc_fh
*current_fh
)
587 struct nfs4_delegation
*dp
;
589 dprintk("NFSD alloc_init_deleg\n");
590 if (num_delegations
> max_delegations
)
592 if (delegation_blocked(¤t_fh
->fh_handle
))
594 dp
= delegstateid(nfs4_alloc_stid(clp
, deleg_slab
));
598 * delegation seqid's are never incremented. The 4.1 special
599 * meaning of seqid 0 isn't meaningful, really, but let's avoid
600 * 0 anyway just for consistency and use 1:
602 dp
->dl_stid
.sc_stateid
.si_generation
= 1;
604 INIT_LIST_HEAD(&dp
->dl_perfile
);
605 INIT_LIST_HEAD(&dp
->dl_perclnt
);
606 INIT_LIST_HEAD(&dp
->dl_recall_lru
);
608 dp
->dl_type
= NFS4_OPEN_DELEGATE_READ
;
609 fh_copy_shallow(&dp
->dl_fh
, ¤t_fh
->fh_handle
);
611 INIT_WORK(&dp
->dl_recall
.cb_work
, nfsd4_run_cb_recall
);
615 static void remove_stid(struct nfs4_stid
*s
)
617 struct idr
*stateids
= &s
->sc_client
->cl_stateids
;
619 idr_remove(stateids
, s
->sc_stateid
.si_opaque
.so_id
);
622 static void nfs4_free_stid(struct kmem_cache
*slab
, struct nfs4_stid
*s
)
624 kmem_cache_free(slab
, s
);
628 nfs4_put_delegation(struct nfs4_delegation
*dp
)
630 if (atomic_dec_and_test(&dp
->dl_stid
.sc_count
)) {
631 remove_stid(&dp
->dl_stid
);
632 nfs4_free_stid(deleg_slab
, &dp
->dl_stid
);
637 static void nfs4_put_deleg_lease(struct nfs4_file
*fp
)
639 lockdep_assert_held(&state_lock
);
643 if (atomic_dec_and_test(&fp
->fi_delegees
)) {
644 vfs_setlease(fp
->fi_deleg_file
, F_UNLCK
, &fp
->fi_lease
);
646 fput(fp
->fi_deleg_file
);
647 fp
->fi_deleg_file
= NULL
;
651 static void unhash_stid(struct nfs4_stid
*s
)
657 hash_delegation_locked(struct nfs4_delegation
*dp
, struct nfs4_file
*fp
)
659 lockdep_assert_held(&state_lock
);
660 lockdep_assert_held(&fp
->fi_lock
);
662 dp
->dl_stid
.sc_type
= NFS4_DELEG_STID
;
663 list_add(&dp
->dl_perfile
, &fp
->fi_delegations
);
664 list_add(&dp
->dl_perclnt
, &dp
->dl_stid
.sc_client
->cl_delegations
);
667 /* Called under the state lock. */
669 unhash_delegation(struct nfs4_delegation
*dp
)
671 struct nfs4_file
*fp
= dp
->dl_file
;
673 spin_lock(&state_lock
);
674 dp
->dl_stid
.sc_type
= NFS4_CLOSED_DELEG_STID
;
675 /* Ensure that deleg break won't try to requeue it */
677 spin_lock(&fp
->fi_lock
);
678 list_del_init(&dp
->dl_perclnt
);
679 list_del_init(&dp
->dl_recall_lru
);
680 list_del_init(&dp
->dl_perfile
);
681 spin_unlock(&fp
->fi_lock
);
683 nfs4_put_deleg_lease(fp
);
686 spin_unlock(&state_lock
);
691 static void destroy_revoked_delegation(struct nfs4_delegation
*dp
)
693 list_del_init(&dp
->dl_recall_lru
);
694 nfs4_put_delegation(dp
);
697 static void destroy_delegation(struct nfs4_delegation
*dp
)
699 unhash_delegation(dp
);
700 nfs4_put_delegation(dp
);
703 static void revoke_delegation(struct nfs4_delegation
*dp
)
705 struct nfs4_client
*clp
= dp
->dl_stid
.sc_client
;
707 if (clp
->cl_minorversion
== 0)
708 destroy_delegation(dp
);
710 unhash_delegation(dp
);
711 dp
->dl_stid
.sc_type
= NFS4_REVOKED_DELEG_STID
;
712 list_add(&dp
->dl_recall_lru
, &clp
->cl_revoked
);
720 static unsigned int clientid_hashval(u32 id
)
722 return id
& CLIENT_HASH_MASK
;
725 static unsigned int clientstr_hashval(const char *name
)
727 return opaque_hashval(name
, 8) & CLIENT_HASH_MASK
;
731 * We store the NONE, READ, WRITE, and BOTH bits separately in the
732 * st_{access,deny}_bmap field of the stateid, in order to track not
733 * only what share bits are currently in force, but also what
734 * combinations of share bits previous opens have used. This allows us
735 * to enforce the recommendation of rfc 3530 14.2.19 that the server
736 * return an error if the client attempt to downgrade to a combination
737 * of share bits not explicable by closing some of its previous opens.
739 * XXX: This enforcement is actually incomplete, since we don't keep
740 * track of access/deny bit combinations; so, e.g., we allow:
742 * OPEN allow read, deny write
743 * OPEN allow both, deny none
744 * DOWNGRADE allow read, deny none
746 * which we should reject.
749 bmap_to_share_mode(unsigned long bmap
) {
751 unsigned int access
= 0;
753 for (i
= 1; i
< 4; i
++) {
754 if (test_bit(i
, &bmap
))
760 /* set share access for a given stateid */
762 set_access(u32 access
, struct nfs4_ol_stateid
*stp
)
764 unsigned char mask
= 1 << access
;
766 WARN_ON_ONCE(access
> NFS4_SHARE_ACCESS_BOTH
);
767 stp
->st_access_bmap
|= mask
;
770 /* clear share access for a given stateid */
772 clear_access(u32 access
, struct nfs4_ol_stateid
*stp
)
774 unsigned char mask
= 1 << access
;
776 WARN_ON_ONCE(access
> NFS4_SHARE_ACCESS_BOTH
);
777 stp
->st_access_bmap
&= ~mask
;
780 /* test whether a given stateid has access */
782 test_access(u32 access
, struct nfs4_ol_stateid
*stp
)
784 unsigned char mask
= 1 << access
;
786 return (bool)(stp
->st_access_bmap
& mask
);
789 /* set share deny for a given stateid */
791 set_deny(u32 deny
, struct nfs4_ol_stateid
*stp
)
793 unsigned char mask
= 1 << deny
;
795 WARN_ON_ONCE(deny
> NFS4_SHARE_DENY_BOTH
);
796 stp
->st_deny_bmap
|= mask
;
799 /* clear share deny for a given stateid */
801 clear_deny(u32 deny
, struct nfs4_ol_stateid
*stp
)
803 unsigned char mask
= 1 << deny
;
805 WARN_ON_ONCE(deny
> NFS4_SHARE_DENY_BOTH
);
806 stp
->st_deny_bmap
&= ~mask
;
809 /* test whether a given stateid is denying specific access */
811 test_deny(u32 deny
, struct nfs4_ol_stateid
*stp
)
813 unsigned char mask
= 1 << deny
;
815 return (bool)(stp
->st_deny_bmap
& mask
);
818 static int nfs4_access_to_omode(u32 access
)
820 switch (access
& NFS4_SHARE_ACCESS_BOTH
) {
821 case NFS4_SHARE_ACCESS_READ
:
823 case NFS4_SHARE_ACCESS_WRITE
:
825 case NFS4_SHARE_ACCESS_BOTH
:
833 * A stateid that had a deny mode associated with it is being released
834 * or downgraded. Recalculate the deny mode on the file.
837 recalculate_deny_mode(struct nfs4_file
*fp
)
839 struct nfs4_ol_stateid
*stp
;
841 spin_lock(&fp
->fi_lock
);
842 fp
->fi_share_deny
= 0;
843 list_for_each_entry(stp
, &fp
->fi_stateids
, st_perfile
)
844 fp
->fi_share_deny
|= bmap_to_share_mode(stp
->st_deny_bmap
);
845 spin_unlock(&fp
->fi_lock
);
849 reset_union_bmap_deny(u32 deny
, struct nfs4_ol_stateid
*stp
)
854 for (i
= 1; i
< 4; i
++) {
855 if ((i
& deny
) != i
) {
861 /* Recalculate per-file deny mode if there was a change */
863 recalculate_deny_mode(stp
->st_file
);
866 /* release all access and file references for a given stateid */
868 release_all_access(struct nfs4_ol_stateid
*stp
)
871 struct nfs4_file
*fp
= stp
->st_file
;
873 if (fp
&& stp
->st_deny_bmap
!= 0)
874 recalculate_deny_mode(fp
);
876 for (i
= 1; i
< 4; i
++) {
877 if (test_access(i
, stp
))
878 nfs4_file_put_access(stp
->st_file
, i
);
879 clear_access(i
, stp
);
883 static void unhash_generic_stateid(struct nfs4_ol_stateid
*stp
)
885 struct nfs4_file
*fp
= stp
->st_file
;
887 spin_lock(&fp
->fi_lock
);
888 list_del(&stp
->st_perfile
);
889 spin_unlock(&fp
->fi_lock
);
890 list_del(&stp
->st_perstateowner
);
893 static void close_generic_stateid(struct nfs4_ol_stateid
*stp
)
895 release_all_access(stp
);
896 put_nfs4_file(stp
->st_file
);
900 static void free_generic_stateid(struct nfs4_ol_stateid
*stp
)
902 remove_stid(&stp
->st_stid
);
903 nfs4_free_stid(stateid_slab
, &stp
->st_stid
);
906 static void __release_lock_stateid(struct nfs4_ol_stateid
*stp
)
910 list_del(&stp
->st_locks
);
911 unhash_generic_stateid(stp
);
912 unhash_stid(&stp
->st_stid
);
913 file
= find_any_file(stp
->st_file
);
915 filp_close(file
, (fl_owner_t
)lockowner(stp
->st_stateowner
));
916 close_generic_stateid(stp
);
917 free_generic_stateid(stp
);
920 static void unhash_lockowner(struct nfs4_lockowner
*lo
)
922 struct nfs4_ol_stateid
*stp
;
924 list_del(&lo
->lo_owner
.so_strhash
);
925 while (!list_empty(&lo
->lo_owner
.so_stateids
)) {
926 stp
= list_first_entry(&lo
->lo_owner
.so_stateids
,
927 struct nfs4_ol_stateid
, st_perstateowner
);
928 __release_lock_stateid(stp
);
932 static void nfs4_free_lockowner(struct nfs4_lockowner
*lo
)
934 kfree(lo
->lo_owner
.so_owner
.data
);
935 kmem_cache_free(lockowner_slab
, lo
);
938 static void release_lockowner(struct nfs4_lockowner
*lo
)
940 unhash_lockowner(lo
);
941 nfs4_free_lockowner(lo
);
944 static void release_lockowner_if_empty(struct nfs4_lockowner
*lo
)
946 if (list_empty(&lo
->lo_owner
.so_stateids
))
947 release_lockowner(lo
);
950 static void release_lock_stateid(struct nfs4_ol_stateid
*stp
)
952 struct nfs4_lockowner
*lo
;
954 lo
= lockowner(stp
->st_stateowner
);
955 __release_lock_stateid(stp
);
956 release_lockowner_if_empty(lo
);
959 static void release_open_stateid_locks(struct nfs4_ol_stateid
*open_stp
)
961 struct nfs4_ol_stateid
*stp
;
963 while (!list_empty(&open_stp
->st_locks
)) {
964 stp
= list_entry(open_stp
->st_locks
.next
,
965 struct nfs4_ol_stateid
, st_locks
);
966 release_lock_stateid(stp
);
970 static void unhash_open_stateid(struct nfs4_ol_stateid
*stp
)
972 unhash_generic_stateid(stp
);
973 release_open_stateid_locks(stp
);
974 close_generic_stateid(stp
);
977 static void release_open_stateid(struct nfs4_ol_stateid
*stp
)
979 unhash_open_stateid(stp
);
980 free_generic_stateid(stp
);
983 static void unhash_openowner(struct nfs4_openowner
*oo
)
985 struct nfs4_ol_stateid
*stp
;
987 list_del(&oo
->oo_owner
.so_strhash
);
988 list_del(&oo
->oo_perclient
);
989 while (!list_empty(&oo
->oo_owner
.so_stateids
)) {
990 stp
= list_first_entry(&oo
->oo_owner
.so_stateids
,
991 struct nfs4_ol_stateid
, st_perstateowner
);
992 release_open_stateid(stp
);
996 static void release_last_closed_stateid(struct nfs4_openowner
*oo
)
998 struct nfs4_ol_stateid
*s
= oo
->oo_last_closed_stid
;
1001 free_generic_stateid(s
);
1002 oo
->oo_last_closed_stid
= NULL
;
1006 static void nfs4_free_openowner(struct nfs4_openowner
*oo
)
1008 kfree(oo
->oo_owner
.so_owner
.data
);
1009 kmem_cache_free(openowner_slab
, oo
);
1012 static void release_openowner(struct nfs4_openowner
*oo
)
1014 unhash_openowner(oo
);
1015 list_del(&oo
->oo_close_lru
);
1016 release_last_closed_stateid(oo
);
1017 nfs4_free_openowner(oo
);
1021 hash_sessionid(struct nfs4_sessionid
*sessionid
)
1023 struct nfsd4_sessionid
*sid
= (struct nfsd4_sessionid
*)sessionid
;
1025 return sid
->sequence
% SESSION_HASH_SIZE
;
1030 dump_sessionid(const char *fn
, struct nfs4_sessionid
*sessionid
)
1032 u32
*ptr
= (u32
*)(&sessionid
->data
[0]);
1033 dprintk("%s: %u:%u:%u:%u\n", fn
, ptr
[0], ptr
[1], ptr
[2], ptr
[3]);
1037 dump_sessionid(const char *fn
, struct nfs4_sessionid
*sessionid
)
1043 * Bump the seqid on cstate->replay_owner, and clear replay_owner if it
1044 * won't be used for replay.
1046 void nfsd4_bump_seqid(struct nfsd4_compound_state
*cstate
, __be32 nfserr
)
1048 struct nfs4_stateowner
*so
= cstate
->replay_owner
;
1050 if (nfserr
== nfserr_replay_me
)
1053 if (!seqid_mutating_err(ntohl(nfserr
))) {
1054 cstate
->replay_owner
= NULL
;
1059 if (so
->so_is_open_owner
)
1060 release_last_closed_stateid(openowner(so
));
1066 gen_sessionid(struct nfsd4_session
*ses
)
1068 struct nfs4_client
*clp
= ses
->se_client
;
1069 struct nfsd4_sessionid
*sid
;
1071 sid
= (struct nfsd4_sessionid
*)ses
->se_sessionid
.data
;
1072 sid
->clientid
= clp
->cl_clientid
;
1073 sid
->sequence
= current_sessionid
++;
1078 * The protocol defines ca_maxresponssize_cached to include the size of
1079 * the rpc header, but all we need to cache is the data starting after
1080 * the end of the initial SEQUENCE operation--the rest we regenerate
1081 * each time. Therefore we can advertise a ca_maxresponssize_cached
1082 * value that is the number of bytes in our cache plus a few additional
1083 * bytes. In order to stay on the safe side, and not promise more than
1084 * we can cache, those additional bytes must be the minimum possible: 24
1085 * bytes of rpc header (xid through accept state, with AUTH_NULL
1086 * verifier), 12 for the compound header (with zero-length tag), and 44
1087 * for the SEQUENCE op response:
1089 #define NFSD_MIN_HDR_SEQ_SZ (24 + 12 + 44)
1092 free_session_slots(struct nfsd4_session
*ses
)
1096 for (i
= 0; i
< ses
->se_fchannel
.maxreqs
; i
++)
1097 kfree(ses
->se_slots
[i
]);
1101 * We don't actually need to cache the rpc and session headers, so we
1102 * can allocate a little less for each slot:
1104 static inline u32
slot_bytes(struct nfsd4_channel_attrs
*ca
)
1108 if (ca
->maxresp_cached
< NFSD_MIN_HDR_SEQ_SZ
)
1111 size
= ca
->maxresp_cached
- NFSD_MIN_HDR_SEQ_SZ
;
1112 return size
+ sizeof(struct nfsd4_slot
);
1116 * XXX: If we run out of reserved DRC memory we could (up to a point)
1117 * re-negotiate active sessions and reduce their slot usage to make
1118 * room for new connections. For now we just fail the create session.
1120 static u32
nfsd4_get_drc_mem(struct nfsd4_channel_attrs
*ca
)
1122 u32 slotsize
= slot_bytes(ca
);
1123 u32 num
= ca
->maxreqs
;
1126 spin_lock(&nfsd_drc_lock
);
1127 avail
= min((unsigned long)NFSD_MAX_MEM_PER_SESSION
,
1128 nfsd_drc_max_mem
- nfsd_drc_mem_used
);
1129 num
= min_t(int, num
, avail
/ slotsize
);
1130 nfsd_drc_mem_used
+= num
* slotsize
;
1131 spin_unlock(&nfsd_drc_lock
);
1136 static void nfsd4_put_drc_mem(struct nfsd4_channel_attrs
*ca
)
1138 int slotsize
= slot_bytes(ca
);
1140 spin_lock(&nfsd_drc_lock
);
1141 nfsd_drc_mem_used
-= slotsize
* ca
->maxreqs
;
1142 spin_unlock(&nfsd_drc_lock
);
1145 static struct nfsd4_session
*alloc_session(struct nfsd4_channel_attrs
*fattrs
,
1146 struct nfsd4_channel_attrs
*battrs
)
1148 int numslots
= fattrs
->maxreqs
;
1149 int slotsize
= slot_bytes(fattrs
);
1150 struct nfsd4_session
*new;
1153 BUILD_BUG_ON(NFSD_MAX_SLOTS_PER_SESSION
* sizeof(struct nfsd4_slot
*)
1154 + sizeof(struct nfsd4_session
) > PAGE_SIZE
);
1155 mem
= numslots
* sizeof(struct nfsd4_slot
*);
1157 new = kzalloc(sizeof(*new) + mem
, GFP_KERNEL
);
1160 /* allocate each struct nfsd4_slot and data cache in one piece */
1161 for (i
= 0; i
< numslots
; i
++) {
1162 new->se_slots
[i
] = kzalloc(slotsize
, GFP_KERNEL
);
1163 if (!new->se_slots
[i
])
1167 memcpy(&new->se_fchannel
, fattrs
, sizeof(struct nfsd4_channel_attrs
));
1168 memcpy(&new->se_bchannel
, battrs
, sizeof(struct nfsd4_channel_attrs
));
1173 kfree(new->se_slots
[i
]);
1178 static void free_conn(struct nfsd4_conn
*c
)
1180 svc_xprt_put(c
->cn_xprt
);
1184 static void nfsd4_conn_lost(struct svc_xpt_user
*u
)
1186 struct nfsd4_conn
*c
= container_of(u
, struct nfsd4_conn
, cn_xpt_user
);
1187 struct nfs4_client
*clp
= c
->cn_session
->se_client
;
1189 spin_lock(&clp
->cl_lock
);
1190 if (!list_empty(&c
->cn_persession
)) {
1191 list_del(&c
->cn_persession
);
1194 nfsd4_probe_callback(clp
);
1195 spin_unlock(&clp
->cl_lock
);
1198 static struct nfsd4_conn
*alloc_conn(struct svc_rqst
*rqstp
, u32 flags
)
1200 struct nfsd4_conn
*conn
;
1202 conn
= kmalloc(sizeof(struct nfsd4_conn
), GFP_KERNEL
);
1205 svc_xprt_get(rqstp
->rq_xprt
);
1206 conn
->cn_xprt
= rqstp
->rq_xprt
;
1207 conn
->cn_flags
= flags
;
1208 INIT_LIST_HEAD(&conn
->cn_xpt_user
.list
);
1212 static void __nfsd4_hash_conn(struct nfsd4_conn
*conn
, struct nfsd4_session
*ses
)
1214 conn
->cn_session
= ses
;
1215 list_add(&conn
->cn_persession
, &ses
->se_conns
);
1218 static void nfsd4_hash_conn(struct nfsd4_conn
*conn
, struct nfsd4_session
*ses
)
1220 struct nfs4_client
*clp
= ses
->se_client
;
1222 spin_lock(&clp
->cl_lock
);
1223 __nfsd4_hash_conn(conn
, ses
);
1224 spin_unlock(&clp
->cl_lock
);
1227 static int nfsd4_register_conn(struct nfsd4_conn
*conn
)
1229 conn
->cn_xpt_user
.callback
= nfsd4_conn_lost
;
1230 return register_xpt_user(conn
->cn_xprt
, &conn
->cn_xpt_user
);
1233 static void nfsd4_init_conn(struct svc_rqst
*rqstp
, struct nfsd4_conn
*conn
, struct nfsd4_session
*ses
)
1237 nfsd4_hash_conn(conn
, ses
);
1238 ret
= nfsd4_register_conn(conn
);
1240 /* oops; xprt is already down: */
1241 nfsd4_conn_lost(&conn
->cn_xpt_user
);
1242 /* We may have gained or lost a callback channel: */
1243 nfsd4_probe_callback_sync(ses
->se_client
);
1246 static struct nfsd4_conn
*alloc_conn_from_crses(struct svc_rqst
*rqstp
, struct nfsd4_create_session
*cses
)
1248 u32 dir
= NFS4_CDFC4_FORE
;
1250 if (cses
->flags
& SESSION4_BACK_CHAN
)
1251 dir
|= NFS4_CDFC4_BACK
;
1252 return alloc_conn(rqstp
, dir
);
1255 /* must be called under client_lock */
1256 static void nfsd4_del_conns(struct nfsd4_session
*s
)
1258 struct nfs4_client
*clp
= s
->se_client
;
1259 struct nfsd4_conn
*c
;
1261 spin_lock(&clp
->cl_lock
);
1262 while (!list_empty(&s
->se_conns
)) {
1263 c
= list_first_entry(&s
->se_conns
, struct nfsd4_conn
, cn_persession
);
1264 list_del_init(&c
->cn_persession
);
1265 spin_unlock(&clp
->cl_lock
);
1267 unregister_xpt_user(c
->cn_xprt
, &c
->cn_xpt_user
);
1270 spin_lock(&clp
->cl_lock
);
1272 spin_unlock(&clp
->cl_lock
);
1275 static void __free_session(struct nfsd4_session
*ses
)
1277 free_session_slots(ses
);
1281 static void free_session(struct nfsd4_session
*ses
)
1283 struct nfsd_net
*nn
= net_generic(ses
->se_client
->net
, nfsd_net_id
);
1285 lockdep_assert_held(&nn
->client_lock
);
1286 nfsd4_del_conns(ses
);
1287 nfsd4_put_drc_mem(&ses
->se_fchannel
);
1288 __free_session(ses
);
1291 static void init_session(struct svc_rqst
*rqstp
, struct nfsd4_session
*new, struct nfs4_client
*clp
, struct nfsd4_create_session
*cses
)
1294 struct nfsd_net
*nn
= net_generic(SVC_NET(rqstp
), nfsd_net_id
);
1296 new->se_client
= clp
;
1299 INIT_LIST_HEAD(&new->se_conns
);
1301 new->se_cb_seq_nr
= 1;
1302 new->se_flags
= cses
->flags
;
1303 new->se_cb_prog
= cses
->callback_prog
;
1304 new->se_cb_sec
= cses
->cb_sec
;
1305 atomic_set(&new->se_ref
, 0);
1306 idx
= hash_sessionid(&new->se_sessionid
);
1307 spin_lock(&nn
->client_lock
);
1308 list_add(&new->se_hash
, &nn
->sessionid_hashtbl
[idx
]);
1309 spin_lock(&clp
->cl_lock
);
1310 list_add(&new->se_perclnt
, &clp
->cl_sessions
);
1311 spin_unlock(&clp
->cl_lock
);
1312 spin_unlock(&nn
->client_lock
);
1314 if (cses
->flags
& SESSION4_BACK_CHAN
) {
1315 struct sockaddr
*sa
= svc_addr(rqstp
);
1317 * This is a little silly; with sessions there's no real
1318 * use for the callback address. Use the peer address
1319 * as a reasonable default for now, but consider fixing
1320 * the rpc client not to require an address in the
1323 rpc_copy_addr((struct sockaddr
*)&clp
->cl_cb_conn
.cb_addr
, sa
);
1324 clp
->cl_cb_conn
.cb_addrlen
= svc_addr_len(sa
);
1328 /* caller must hold client_lock */
1329 static struct nfsd4_session
*
1330 __find_in_sessionid_hashtbl(struct nfs4_sessionid
*sessionid
, struct net
*net
)
1332 struct nfsd4_session
*elem
;
1334 struct nfsd_net
*nn
= net_generic(net
, nfsd_net_id
);
1336 dump_sessionid(__func__
, sessionid
);
1337 idx
= hash_sessionid(sessionid
);
1338 /* Search in the appropriate list */
1339 list_for_each_entry(elem
, &nn
->sessionid_hashtbl
[idx
], se_hash
) {
1340 if (!memcmp(elem
->se_sessionid
.data
, sessionid
->data
,
1341 NFS4_MAX_SESSIONID_LEN
)) {
1346 dprintk("%s: session not found\n", __func__
);
1350 static struct nfsd4_session
*
1351 find_in_sessionid_hashtbl(struct nfs4_sessionid
*sessionid
, struct net
*net
,
1354 struct nfsd4_session
*session
;
1355 __be32 status
= nfserr_badsession
;
1357 session
= __find_in_sessionid_hashtbl(sessionid
, net
);
1360 status
= nfsd4_get_session_locked(session
);
1368 /* caller must hold client_lock */
1370 unhash_session(struct nfsd4_session
*ses
)
1372 list_del(&ses
->se_hash
);
1373 spin_lock(&ses
->se_client
->cl_lock
);
1374 list_del(&ses
->se_perclnt
);
1375 spin_unlock(&ses
->se_client
->cl_lock
);
1378 /* SETCLIENTID and SETCLIENTID_CONFIRM Helper functions */
1380 STALE_CLIENTID(clientid_t
*clid
, struct nfsd_net
*nn
)
1382 if (clid
->cl_boot
== nn
->boot_time
)
1384 dprintk("NFSD stale clientid (%08x/%08x) boot_time %08lx\n",
1385 clid
->cl_boot
, clid
->cl_id
, nn
->boot_time
);
1390 * XXX Should we use a slab cache ?
1391 * This type of memory management is somewhat inefficient, but we use it
1392 * anyway since SETCLIENTID is not a common operation.
1394 static struct nfs4_client
*alloc_client(struct xdr_netobj name
)
1396 struct nfs4_client
*clp
;
1398 clp
= kzalloc(sizeof(struct nfs4_client
), GFP_KERNEL
);
1401 clp
->cl_name
.data
= kmemdup(name
.data
, name
.len
, GFP_KERNEL
);
1402 if (clp
->cl_name
.data
== NULL
) {
1406 clp
->cl_name
.len
= name
.len
;
1407 INIT_LIST_HEAD(&clp
->cl_sessions
);
1408 idr_init(&clp
->cl_stateids
);
1409 atomic_set(&clp
->cl_refcount
, 0);
1410 clp
->cl_cb_state
= NFSD4_CB_UNKNOWN
;
1411 INIT_LIST_HEAD(&clp
->cl_idhash
);
1412 INIT_LIST_HEAD(&clp
->cl_openowners
);
1413 INIT_LIST_HEAD(&clp
->cl_delegations
);
1414 INIT_LIST_HEAD(&clp
->cl_lru
);
1415 INIT_LIST_HEAD(&clp
->cl_callbacks
);
1416 INIT_LIST_HEAD(&clp
->cl_revoked
);
1417 spin_lock_init(&clp
->cl_lock
);
1418 rpc_init_wait_queue(&clp
->cl_cb_waitq
, "Backchannel slot table");
1423 free_client(struct nfs4_client
*clp
)
1425 struct nfsd_net __maybe_unused
*nn
= net_generic(clp
->net
, nfsd_net_id
);
1427 lockdep_assert_held(&nn
->client_lock
);
1428 while (!list_empty(&clp
->cl_sessions
)) {
1429 struct nfsd4_session
*ses
;
1430 ses
= list_entry(clp
->cl_sessions
.next
, struct nfsd4_session
,
1432 list_del(&ses
->se_perclnt
);
1433 WARN_ON_ONCE(atomic_read(&ses
->se_ref
));
1436 rpc_destroy_wait_queue(&clp
->cl_cb_waitq
);
1437 free_svc_cred(&clp
->cl_cred
);
1438 kfree(clp
->cl_name
.data
);
1439 idr_destroy(&clp
->cl_stateids
);
1443 /* must be called under the client_lock */
1445 unhash_client_locked(struct nfs4_client
*clp
)
1447 struct nfsd4_session
*ses
;
1449 list_del(&clp
->cl_lru
);
1450 spin_lock(&clp
->cl_lock
);
1451 list_for_each_entry(ses
, &clp
->cl_sessions
, se_perclnt
)
1452 list_del_init(&ses
->se_hash
);
1453 spin_unlock(&clp
->cl_lock
);
1457 destroy_client(struct nfs4_client
*clp
)
1459 struct nfs4_openowner
*oo
;
1460 struct nfs4_delegation
*dp
;
1461 struct list_head reaplist
;
1462 struct nfsd_net
*nn
= net_generic(clp
->net
, nfsd_net_id
);
1464 INIT_LIST_HEAD(&reaplist
);
1465 spin_lock(&state_lock
);
1466 while (!list_empty(&clp
->cl_delegations
)) {
1467 dp
= list_entry(clp
->cl_delegations
.next
, struct nfs4_delegation
, dl_perclnt
);
1468 list_del_init(&dp
->dl_perclnt
);
1469 /* Ensure that deleg break won't try to requeue it */
1471 list_move(&dp
->dl_recall_lru
, &reaplist
);
1473 spin_unlock(&state_lock
);
1474 while (!list_empty(&reaplist
)) {
1475 dp
= list_entry(reaplist
.next
, struct nfs4_delegation
, dl_recall_lru
);
1476 destroy_delegation(dp
);
1478 list_splice_init(&clp
->cl_revoked
, &reaplist
);
1479 while (!list_empty(&reaplist
)) {
1480 dp
= list_entry(reaplist
.next
, struct nfs4_delegation
, dl_recall_lru
);
1481 destroy_revoked_delegation(dp
);
1483 while (!list_empty(&clp
->cl_openowners
)) {
1484 oo
= list_entry(clp
->cl_openowners
.next
, struct nfs4_openowner
, oo_perclient
);
1485 release_openowner(oo
);
1487 nfsd4_shutdown_callback(clp
);
1488 if (clp
->cl_cb_conn
.cb_xprt
)
1489 svc_xprt_put(clp
->cl_cb_conn
.cb_xprt
);
1490 list_del(&clp
->cl_idhash
);
1491 if (test_bit(NFSD4_CLIENT_CONFIRMED
, &clp
->cl_flags
))
1492 rb_erase(&clp
->cl_namenode
, &nn
->conf_name_tree
);
1494 rb_erase(&clp
->cl_namenode
, &nn
->unconf_name_tree
);
1495 spin_lock(&nn
->client_lock
);
1496 unhash_client_locked(clp
);
1497 WARN_ON_ONCE(atomic_read(&clp
->cl_refcount
));
1499 spin_unlock(&nn
->client_lock
);
1502 static void expire_client(struct nfs4_client
*clp
)
1504 nfsd4_client_record_remove(clp
);
1505 destroy_client(clp
);
1508 static void copy_verf(struct nfs4_client
*target
, nfs4_verifier
*source
)
1510 memcpy(target
->cl_verifier
.data
, source
->data
,
1511 sizeof(target
->cl_verifier
.data
));
1514 static void copy_clid(struct nfs4_client
*target
, struct nfs4_client
*source
)
1516 target
->cl_clientid
.cl_boot
= source
->cl_clientid
.cl_boot
;
1517 target
->cl_clientid
.cl_id
= source
->cl_clientid
.cl_id
;
1520 static int copy_cred(struct svc_cred
*target
, struct svc_cred
*source
)
1522 if (source
->cr_principal
) {
1523 target
->cr_principal
=
1524 kstrdup(source
->cr_principal
, GFP_KERNEL
);
1525 if (target
->cr_principal
== NULL
)
1528 target
->cr_principal
= NULL
;
1529 target
->cr_flavor
= source
->cr_flavor
;
1530 target
->cr_uid
= source
->cr_uid
;
1531 target
->cr_gid
= source
->cr_gid
;
1532 target
->cr_group_info
= source
->cr_group_info
;
1533 get_group_info(target
->cr_group_info
);
1534 target
->cr_gss_mech
= source
->cr_gss_mech
;
1535 if (source
->cr_gss_mech
)
1536 gss_mech_get(source
->cr_gss_mech
);
1541 compare_blob(const struct xdr_netobj
*o1
, const struct xdr_netobj
*o2
)
1545 res
= o1
->len
- o2
->len
;
1548 return (long long)memcmp(o1
->data
, o2
->data
, o1
->len
);
1551 static int same_name(const char *n1
, const char *n2
)
1553 return 0 == memcmp(n1
, n2
, HEXDIR_LEN
);
1557 same_verf(nfs4_verifier
*v1
, nfs4_verifier
*v2
)
1559 return 0 == memcmp(v1
->data
, v2
->data
, sizeof(v1
->data
));
1563 same_clid(clientid_t
*cl1
, clientid_t
*cl2
)
1565 return (cl1
->cl_boot
== cl2
->cl_boot
) && (cl1
->cl_id
== cl2
->cl_id
);
1568 static bool groups_equal(struct group_info
*g1
, struct group_info
*g2
)
1572 if (g1
->ngroups
!= g2
->ngroups
)
1574 for (i
=0; i
<g1
->ngroups
; i
++)
1575 if (!gid_eq(GROUP_AT(g1
, i
), GROUP_AT(g2
, i
)))
1581 * RFC 3530 language requires clid_inuse be returned when the
1582 * "principal" associated with a requests differs from that previously
1583 * used. We use uid, gid's, and gss principal string as our best
1584 * approximation. We also don't want to allow non-gss use of a client
1585 * established using gss: in theory cr_principal should catch that
1586 * change, but in practice cr_principal can be null even in the gss case
1587 * since gssd doesn't always pass down a principal string.
1589 static bool is_gss_cred(struct svc_cred
*cr
)
1591 /* Is cr_flavor one of the gss "pseudoflavors"?: */
1592 return (cr
->cr_flavor
> RPC_AUTH_MAXFLAVOR
);
1597 same_creds(struct svc_cred
*cr1
, struct svc_cred
*cr2
)
1599 if ((is_gss_cred(cr1
) != is_gss_cred(cr2
))
1600 || (!uid_eq(cr1
->cr_uid
, cr2
->cr_uid
))
1601 || (!gid_eq(cr1
->cr_gid
, cr2
->cr_gid
))
1602 || !groups_equal(cr1
->cr_group_info
, cr2
->cr_group_info
))
1604 if (cr1
->cr_principal
== cr2
->cr_principal
)
1606 if (!cr1
->cr_principal
|| !cr2
->cr_principal
)
1608 return 0 == strcmp(cr1
->cr_principal
, cr2
->cr_principal
);
1611 static bool svc_rqst_integrity_protected(struct svc_rqst
*rqstp
)
1613 struct svc_cred
*cr
= &rqstp
->rq_cred
;
1616 if (!cr
->cr_gss_mech
)
1618 service
= gss_pseudoflavor_to_service(cr
->cr_gss_mech
, cr
->cr_flavor
);
1619 return service
== RPC_GSS_SVC_INTEGRITY
||
1620 service
== RPC_GSS_SVC_PRIVACY
;
1623 static bool mach_creds_match(struct nfs4_client
*cl
, struct svc_rqst
*rqstp
)
1625 struct svc_cred
*cr
= &rqstp
->rq_cred
;
1627 if (!cl
->cl_mach_cred
)
1629 if (cl
->cl_cred
.cr_gss_mech
!= cr
->cr_gss_mech
)
1631 if (!svc_rqst_integrity_protected(rqstp
))
1633 if (!cr
->cr_principal
)
1635 return 0 == strcmp(cl
->cl_cred
.cr_principal
, cr
->cr_principal
);
1638 static void gen_clid(struct nfs4_client
*clp
, struct nfsd_net
*nn
)
1640 static u32 current_clientid
= 1;
1642 clp
->cl_clientid
.cl_boot
= nn
->boot_time
;
1643 clp
->cl_clientid
.cl_id
= current_clientid
++;
1646 static void gen_confirm(struct nfs4_client
*clp
)
1652 * This is opaque to client, so no need to byte-swap. Use
1653 * __force to keep sparse happy
1655 verf
[0] = (__force __be32
)get_seconds();
1656 verf
[1] = (__force __be32
)i
++;
1657 memcpy(clp
->cl_confirm
.data
, verf
, sizeof(clp
->cl_confirm
.data
));
1660 static struct nfs4_stid
*find_stateid(struct nfs4_client
*cl
, stateid_t
*t
)
1662 struct nfs4_stid
*ret
;
1664 ret
= idr_find(&cl
->cl_stateids
, t
->si_opaque
.so_id
);
1665 if (!ret
|| !ret
->sc_type
)
1670 static struct nfs4_stid
*find_stateid_by_type(struct nfs4_client
*cl
, stateid_t
*t
, char typemask
)
1672 struct nfs4_stid
*s
;
1674 s
= find_stateid(cl
, t
);
1677 if (typemask
& s
->sc_type
)
1682 static struct nfs4_client
*create_client(struct xdr_netobj name
,
1683 struct svc_rqst
*rqstp
, nfs4_verifier
*verf
)
1685 struct nfs4_client
*clp
;
1686 struct sockaddr
*sa
= svc_addr(rqstp
);
1688 struct net
*net
= SVC_NET(rqstp
);
1689 struct nfsd_net
*nn
= net_generic(net
, nfsd_net_id
);
1691 clp
= alloc_client(name
);
1695 ret
= copy_cred(&clp
->cl_cred
, &rqstp
->rq_cred
);
1697 spin_lock(&nn
->client_lock
);
1699 spin_unlock(&nn
->client_lock
);
1702 INIT_WORK(&clp
->cl_cb_null
.cb_work
, nfsd4_run_cb_null
);
1703 clp
->cl_time
= get_seconds();
1704 clear_bit(0, &clp
->cl_cb_slot_busy
);
1705 copy_verf(clp
, verf
);
1706 rpc_copy_addr((struct sockaddr
*) &clp
->cl_addr
, sa
);
1708 clp
->cl_cb_session
= NULL
;
1714 add_clp_to_name_tree(struct nfs4_client
*new_clp
, struct rb_root
*root
)
1716 struct rb_node
**new = &(root
->rb_node
), *parent
= NULL
;
1717 struct nfs4_client
*clp
;
1720 clp
= rb_entry(*new, struct nfs4_client
, cl_namenode
);
1723 if (compare_blob(&clp
->cl_name
, &new_clp
->cl_name
) > 0)
1724 new = &((*new)->rb_left
);
1726 new = &((*new)->rb_right
);
1729 rb_link_node(&new_clp
->cl_namenode
, parent
, new);
1730 rb_insert_color(&new_clp
->cl_namenode
, root
);
1733 static struct nfs4_client
*
1734 find_clp_in_name_tree(struct xdr_netobj
*name
, struct rb_root
*root
)
1737 struct rb_node
*node
= root
->rb_node
;
1738 struct nfs4_client
*clp
;
1741 clp
= rb_entry(node
, struct nfs4_client
, cl_namenode
);
1742 cmp
= compare_blob(&clp
->cl_name
, name
);
1744 node
= node
->rb_left
;
1746 node
= node
->rb_right
;
1754 add_to_unconfirmed(struct nfs4_client
*clp
)
1756 unsigned int idhashval
;
1757 struct nfsd_net
*nn
= net_generic(clp
->net
, nfsd_net_id
);
1759 clear_bit(NFSD4_CLIENT_CONFIRMED
, &clp
->cl_flags
);
1760 add_clp_to_name_tree(clp
, &nn
->unconf_name_tree
);
1761 idhashval
= clientid_hashval(clp
->cl_clientid
.cl_id
);
1762 list_add(&clp
->cl_idhash
, &nn
->unconf_id_hashtbl
[idhashval
]);
1767 move_to_confirmed(struct nfs4_client
*clp
)
1769 unsigned int idhashval
= clientid_hashval(clp
->cl_clientid
.cl_id
);
1770 struct nfsd_net
*nn
= net_generic(clp
->net
, nfsd_net_id
);
1772 dprintk("NFSD: move_to_confirm nfs4_client %p\n", clp
);
1773 list_move(&clp
->cl_idhash
, &nn
->conf_id_hashtbl
[idhashval
]);
1774 rb_erase(&clp
->cl_namenode
, &nn
->unconf_name_tree
);
1775 add_clp_to_name_tree(clp
, &nn
->conf_name_tree
);
1776 set_bit(NFSD4_CLIENT_CONFIRMED
, &clp
->cl_flags
);
1780 static struct nfs4_client
*
1781 find_client_in_id_table(struct list_head
*tbl
, clientid_t
*clid
, bool sessions
)
1783 struct nfs4_client
*clp
;
1784 unsigned int idhashval
= clientid_hashval(clid
->cl_id
);
1786 list_for_each_entry(clp
, &tbl
[idhashval
], cl_idhash
) {
1787 if (same_clid(&clp
->cl_clientid
, clid
)) {
1788 if ((bool)clp
->cl_minorversion
!= sessions
)
1797 static struct nfs4_client
*
1798 find_confirmed_client(clientid_t
*clid
, bool sessions
, struct nfsd_net
*nn
)
1800 struct list_head
*tbl
= nn
->conf_id_hashtbl
;
1802 return find_client_in_id_table(tbl
, clid
, sessions
);
1805 static struct nfs4_client
*
1806 find_unconfirmed_client(clientid_t
*clid
, bool sessions
, struct nfsd_net
*nn
)
1808 struct list_head
*tbl
= nn
->unconf_id_hashtbl
;
1810 return find_client_in_id_table(tbl
, clid
, sessions
);
1813 static bool clp_used_exchangeid(struct nfs4_client
*clp
)
1815 return clp
->cl_exchange_flags
!= 0;
1818 static struct nfs4_client
*
1819 find_confirmed_client_by_name(struct xdr_netobj
*name
, struct nfsd_net
*nn
)
1821 return find_clp_in_name_tree(name
, &nn
->conf_name_tree
);
1824 static struct nfs4_client
*
1825 find_unconfirmed_client_by_name(struct xdr_netobj
*name
, struct nfsd_net
*nn
)
1827 return find_clp_in_name_tree(name
, &nn
->unconf_name_tree
);
1831 gen_callback(struct nfs4_client
*clp
, struct nfsd4_setclientid
*se
, struct svc_rqst
*rqstp
)
1833 struct nfs4_cb_conn
*conn
= &clp
->cl_cb_conn
;
1834 struct sockaddr
*sa
= svc_addr(rqstp
);
1835 u32 scopeid
= rpc_get_scope_id(sa
);
1836 unsigned short expected_family
;
1838 /* Currently, we only support tcp and tcp6 for the callback channel */
1839 if (se
->se_callback_netid_len
== 3 &&
1840 !memcmp(se
->se_callback_netid_val
, "tcp", 3))
1841 expected_family
= AF_INET
;
1842 else if (se
->se_callback_netid_len
== 4 &&
1843 !memcmp(se
->se_callback_netid_val
, "tcp6", 4))
1844 expected_family
= AF_INET6
;
1848 conn
->cb_addrlen
= rpc_uaddr2sockaddr(clp
->net
, se
->se_callback_addr_val
,
1849 se
->se_callback_addr_len
,
1850 (struct sockaddr
*)&conn
->cb_addr
,
1851 sizeof(conn
->cb_addr
));
1853 if (!conn
->cb_addrlen
|| conn
->cb_addr
.ss_family
!= expected_family
)
1856 if (conn
->cb_addr
.ss_family
== AF_INET6
)
1857 ((struct sockaddr_in6
*)&conn
->cb_addr
)->sin6_scope_id
= scopeid
;
1859 conn
->cb_prog
= se
->se_callback_prog
;
1860 conn
->cb_ident
= se
->se_callback_ident
;
1861 memcpy(&conn
->cb_saddr
, &rqstp
->rq_daddr
, rqstp
->rq_daddrlen
);
1864 conn
->cb_addr
.ss_family
= AF_UNSPEC
;
1865 conn
->cb_addrlen
= 0;
1866 dprintk(KERN_INFO
"NFSD: this client (clientid %08x/%08x) "
1867 "will not receive delegations\n",
1868 clp
->cl_clientid
.cl_boot
, clp
->cl_clientid
.cl_id
);
1874 * Cache a reply. nfsd4_check_resp_size() has bounded the cache size.
1877 nfsd4_store_cache_entry(struct nfsd4_compoundres
*resp
)
1879 struct xdr_buf
*buf
= resp
->xdr
.buf
;
1880 struct nfsd4_slot
*slot
= resp
->cstate
.slot
;
1883 dprintk("--> %s slot %p\n", __func__
, slot
);
1885 slot
->sl_opcnt
= resp
->opcnt
;
1886 slot
->sl_status
= resp
->cstate
.status
;
1888 slot
->sl_flags
|= NFSD4_SLOT_INITIALIZED
;
1889 if (nfsd4_not_cached(resp
)) {
1890 slot
->sl_datalen
= 0;
1893 base
= resp
->cstate
.data_offset
;
1894 slot
->sl_datalen
= buf
->len
- base
;
1895 if (read_bytes_from_xdr_buf(buf
, base
, slot
->sl_data
, slot
->sl_datalen
))
1896 WARN("%s: sessions DRC could not cache compound\n", __func__
);
1901 * Encode the replay sequence operation from the slot values.
1902 * If cachethis is FALSE encode the uncached rep error on the next
1903 * operation which sets resp->p and increments resp->opcnt for
1904 * nfs4svc_encode_compoundres.
1908 nfsd4_enc_sequence_replay(struct nfsd4_compoundargs
*args
,
1909 struct nfsd4_compoundres
*resp
)
1911 struct nfsd4_op
*op
;
1912 struct nfsd4_slot
*slot
= resp
->cstate
.slot
;
1914 /* Encode the replayed sequence operation */
1915 op
= &args
->ops
[resp
->opcnt
- 1];
1916 nfsd4_encode_operation(resp
, op
);
1918 /* Return nfserr_retry_uncached_rep in next operation. */
1919 if (args
->opcnt
> 1 && !(slot
->sl_flags
& NFSD4_SLOT_CACHETHIS
)) {
1920 op
= &args
->ops
[resp
->opcnt
++];
1921 op
->status
= nfserr_retry_uncached_rep
;
1922 nfsd4_encode_operation(resp
, op
);
1928 * The sequence operation is not cached because we can use the slot and
1932 nfsd4_replay_cache_entry(struct nfsd4_compoundres
*resp
,
1933 struct nfsd4_sequence
*seq
)
1935 struct nfsd4_slot
*slot
= resp
->cstate
.slot
;
1936 struct xdr_stream
*xdr
= &resp
->xdr
;
1940 dprintk("--> %s slot %p\n", __func__
, slot
);
1942 status
= nfsd4_enc_sequence_replay(resp
->rqstp
->rq_argp
, resp
);
1946 p
= xdr_reserve_space(xdr
, slot
->sl_datalen
);
1949 return nfserr_serverfault
;
1951 xdr_encode_opaque_fixed(p
, slot
->sl_data
, slot
->sl_datalen
);
1952 xdr_commit_encode(xdr
);
1954 resp
->opcnt
= slot
->sl_opcnt
;
1955 return slot
->sl_status
;
1959 * Set the exchange_id flags returned by the server.
1962 nfsd4_set_ex_flags(struct nfs4_client
*new, struct nfsd4_exchange_id
*clid
)
1964 /* pNFS is not supported */
1965 new->cl_exchange_flags
|= EXCHGID4_FLAG_USE_NON_PNFS
;
1967 /* Referrals are supported, Migration is not. */
1968 new->cl_exchange_flags
|= EXCHGID4_FLAG_SUPP_MOVED_REFER
;
1970 /* set the wire flags to return to client. */
1971 clid
->flags
= new->cl_exchange_flags
;
1974 static bool client_has_state(struct nfs4_client
*clp
)
1977 * Note clp->cl_openowners check isn't quite right: there's no
1978 * need to count owners without stateid's.
1980 * Also note we should probably be using this in 4.0 case too.
1982 return !list_empty(&clp
->cl_openowners
)
1983 || !list_empty(&clp
->cl_delegations
)
1984 || !list_empty(&clp
->cl_sessions
);
1988 nfsd4_exchange_id(struct svc_rqst
*rqstp
,
1989 struct nfsd4_compound_state
*cstate
,
1990 struct nfsd4_exchange_id
*exid
)
1992 struct nfs4_client
*unconf
, *conf
, *new;
1994 char addr_str
[INET6_ADDRSTRLEN
];
1995 nfs4_verifier verf
= exid
->verifier
;
1996 struct sockaddr
*sa
= svc_addr(rqstp
);
1997 bool update
= exid
->flags
& EXCHGID4_FLAG_UPD_CONFIRMED_REC_A
;
1998 struct nfsd_net
*nn
= net_generic(SVC_NET(rqstp
), nfsd_net_id
);
2000 rpc_ntop(sa
, addr_str
, sizeof(addr_str
));
2001 dprintk("%s rqstp=%p exid=%p clname.len=%u clname.data=%p "
2002 "ip_addr=%s flags %x, spa_how %d\n",
2003 __func__
, rqstp
, exid
, exid
->clname
.len
, exid
->clname
.data
,
2004 addr_str
, exid
->flags
, exid
->spa_how
);
2006 if (exid
->flags
& ~EXCHGID4_FLAG_MASK_A
)
2007 return nfserr_inval
;
2009 switch (exid
->spa_how
) {
2011 if (!svc_rqst_integrity_protected(rqstp
))
2012 return nfserr_inval
;
2015 default: /* checked by xdr code */
2018 return nfserr_encr_alg_unsupp
;
2021 /* Cases below refer to rfc 5661 section 18.35.4: */
2023 conf
= find_confirmed_client_by_name(&exid
->clname
, nn
);
2025 bool creds_match
= same_creds(&conf
->cl_cred
, &rqstp
->rq_cred
);
2026 bool verfs_match
= same_verf(&verf
, &conf
->cl_verifier
);
2029 if (!clp_used_exchangeid(conf
)) { /* buggy client */
2030 status
= nfserr_inval
;
2033 if (!mach_creds_match(conf
, rqstp
)) {
2034 status
= nfserr_wrong_cred
;
2037 if (!creds_match
) { /* case 9 */
2038 status
= nfserr_perm
;
2041 if (!verfs_match
) { /* case 8 */
2042 status
= nfserr_not_same
;
2046 exid
->flags
|= EXCHGID4_FLAG_CONFIRMED_R
;
2050 if (!creds_match
) { /* case 3 */
2051 if (client_has_state(conf
)) {
2052 status
= nfserr_clid_inuse
;
2055 expire_client(conf
);
2058 if (verfs_match
) { /* case 2 */
2059 conf
->cl_exchange_flags
|= EXCHGID4_FLAG_CONFIRMED_R
;
2063 /* case 5, client reboot */
2067 if (update
) { /* case 7 */
2068 status
= nfserr_noent
;
2072 unconf
= find_unconfirmed_client_by_name(&exid
->clname
, nn
);
2073 if (unconf
) /* case 4, possible retry or client restart */
2074 expire_client(unconf
);
2076 /* case 1 (normal case) */
2078 new = create_client(exid
->clname
, rqstp
, &verf
);
2080 status
= nfserr_jukebox
;
2083 new->cl_minorversion
= cstate
->minorversion
;
2084 new->cl_mach_cred
= (exid
->spa_how
== SP4_MACH_CRED
);
2087 add_to_unconfirmed(new);
2089 exid
->clientid
.cl_boot
= new->cl_clientid
.cl_boot
;
2090 exid
->clientid
.cl_id
= new->cl_clientid
.cl_id
;
2092 exid
->seqid
= new->cl_cs_slot
.sl_seqid
+ 1;
2093 nfsd4_set_ex_flags(new, exid
);
2095 dprintk("nfsd4_exchange_id seqid %d flags %x\n",
2096 new->cl_cs_slot
.sl_seqid
, new->cl_exchange_flags
);
2100 nfs4_unlock_state();
2105 check_slot_seqid(u32 seqid
, u32 slot_seqid
, int slot_inuse
)
2107 dprintk("%s enter. seqid %d slot_seqid %d\n", __func__
, seqid
,
2110 /* The slot is in use, and no response has been sent. */
2112 if (seqid
== slot_seqid
)
2113 return nfserr_jukebox
;
2115 return nfserr_seq_misordered
;
2117 /* Note unsigned 32-bit arithmetic handles wraparound: */
2118 if (likely(seqid
== slot_seqid
+ 1))
2120 if (seqid
== slot_seqid
)
2121 return nfserr_replay_cache
;
2122 return nfserr_seq_misordered
;
2126 * Cache the create session result into the create session single DRC
2127 * slot cache by saving the xdr structure. sl_seqid has been set.
2128 * Do this for solo or embedded create session operations.
2131 nfsd4_cache_create_session(struct nfsd4_create_session
*cr_ses
,
2132 struct nfsd4_clid_slot
*slot
, __be32 nfserr
)
2134 slot
->sl_status
= nfserr
;
2135 memcpy(&slot
->sl_cr_ses
, cr_ses
, sizeof(*cr_ses
));
2139 nfsd4_replay_create_session(struct nfsd4_create_session
*cr_ses
,
2140 struct nfsd4_clid_slot
*slot
)
2142 memcpy(cr_ses
, &slot
->sl_cr_ses
, sizeof(*cr_ses
));
2143 return slot
->sl_status
;
2146 #define NFSD_MIN_REQ_HDR_SEQ_SZ ((\
2147 2 * 2 + /* credential,verifier: AUTH_NULL, length 0 */ \
2148 1 + /* MIN tag is length with zero, only length */ \
2149 3 + /* version, opcount, opcode */ \
2150 XDR_QUADLEN(NFS4_MAX_SESSIONID_LEN) + \
2151 /* seqid, slotID, slotID, cache */ \
2152 4 ) * sizeof(__be32))
2154 #define NFSD_MIN_RESP_HDR_SEQ_SZ ((\
2155 2 + /* verifier: AUTH_NULL, length 0 */\
2157 1 + /* MIN tag is length with zero, only length */ \
2158 3 + /* opcount, opcode, opstatus*/ \
2159 XDR_QUADLEN(NFS4_MAX_SESSIONID_LEN) + \
2160 /* seqid, slotID, slotID, slotID, status */ \
2161 5 ) * sizeof(__be32))
2163 static __be32
check_forechannel_attrs(struct nfsd4_channel_attrs
*ca
, struct nfsd_net
*nn
)
2165 u32 maxrpc
= nn
->nfsd_serv
->sv_max_mesg
;
2167 if (ca
->maxreq_sz
< NFSD_MIN_REQ_HDR_SEQ_SZ
)
2168 return nfserr_toosmall
;
2169 if (ca
->maxresp_sz
< NFSD_MIN_RESP_HDR_SEQ_SZ
)
2170 return nfserr_toosmall
;
2171 ca
->headerpadsz
= 0;
2172 ca
->maxreq_sz
= min_t(u32
, ca
->maxreq_sz
, maxrpc
);
2173 ca
->maxresp_sz
= min_t(u32
, ca
->maxresp_sz
, maxrpc
);
2174 ca
->maxops
= min_t(u32
, ca
->maxops
, NFSD_MAX_OPS_PER_COMPOUND
);
2175 ca
->maxresp_cached
= min_t(u32
, ca
->maxresp_cached
,
2176 NFSD_SLOT_CACHE_SIZE
+ NFSD_MIN_HDR_SEQ_SZ
);
2177 ca
->maxreqs
= min_t(u32
, ca
->maxreqs
, NFSD_MAX_SLOTS_PER_SESSION
);
2179 * Note decreasing slot size below client's request may make it
2180 * difficult for client to function correctly, whereas
2181 * decreasing the number of slots will (just?) affect
2182 * performance. When short on memory we therefore prefer to
2183 * decrease number of slots instead of their size. Clients that
2184 * request larger slots than they need will get poor results:
2186 ca
->maxreqs
= nfsd4_get_drc_mem(ca
);
2188 return nfserr_jukebox
;
2193 #define NFSD_CB_MAX_REQ_SZ ((NFS4_enc_cb_recall_sz + \
2194 RPC_MAX_HEADER_WITH_AUTH) * sizeof(__be32))
2195 #define NFSD_CB_MAX_RESP_SZ ((NFS4_dec_cb_recall_sz + \
2196 RPC_MAX_REPHEADER_WITH_AUTH) * sizeof(__be32))
2198 static __be32
check_backchannel_attrs(struct nfsd4_channel_attrs
*ca
)
2200 ca
->headerpadsz
= 0;
2203 * These RPC_MAX_HEADER macros are overkill, especially since we
2204 * don't even do gss on the backchannel yet. But this is still
2205 * less than 1k. Tighten up this estimate in the unlikely event
2206 * it turns out to be a problem for some client:
2208 if (ca
->maxreq_sz
< NFSD_CB_MAX_REQ_SZ
)
2209 return nfserr_toosmall
;
2210 if (ca
->maxresp_sz
< NFSD_CB_MAX_RESP_SZ
)
2211 return nfserr_toosmall
;
2212 ca
->maxresp_cached
= 0;
2214 return nfserr_toosmall
;
2219 static __be32
nfsd4_check_cb_sec(struct nfsd4_cb_sec
*cbs
)
2221 switch (cbs
->flavor
) {
2227 * GSS case: the spec doesn't allow us to return this
2228 * error. But it also doesn't allow us not to support
2230 * I'd rather this fail hard than return some error the
2231 * client might think it can already handle:
2233 return nfserr_encr_alg_unsupp
;
2238 nfsd4_create_session(struct svc_rqst
*rqstp
,
2239 struct nfsd4_compound_state
*cstate
,
2240 struct nfsd4_create_session
*cr_ses
)
2242 struct sockaddr
*sa
= svc_addr(rqstp
);
2243 struct nfs4_client
*conf
, *unconf
;
2244 struct nfsd4_session
*new;
2245 struct nfsd4_conn
*conn
;
2246 struct nfsd4_clid_slot
*cs_slot
= NULL
;
2248 struct nfsd_net
*nn
= net_generic(SVC_NET(rqstp
), nfsd_net_id
);
2250 if (cr_ses
->flags
& ~SESSION4_FLAG_MASK_A
)
2251 return nfserr_inval
;
2252 status
= nfsd4_check_cb_sec(&cr_ses
->cb_sec
);
2255 status
= check_forechannel_attrs(&cr_ses
->fore_channel
, nn
);
2258 status
= check_backchannel_attrs(&cr_ses
->back_channel
);
2260 goto out_release_drc_mem
;
2261 status
= nfserr_jukebox
;
2262 new = alloc_session(&cr_ses
->fore_channel
, &cr_ses
->back_channel
);
2264 goto out_release_drc_mem
;
2265 conn
= alloc_conn_from_crses(rqstp
, cr_ses
);
2267 goto out_free_session
;
2270 unconf
= find_unconfirmed_client(&cr_ses
->clientid
, true, nn
);
2271 conf
= find_confirmed_client(&cr_ses
->clientid
, true, nn
);
2272 WARN_ON_ONCE(conf
&& unconf
);
2275 status
= nfserr_wrong_cred
;
2276 if (!mach_creds_match(conf
, rqstp
))
2278 cs_slot
= &conf
->cl_cs_slot
;
2279 status
= check_slot_seqid(cr_ses
->seqid
, cs_slot
->sl_seqid
, 0);
2280 if (status
== nfserr_replay_cache
) {
2281 status
= nfsd4_replay_create_session(cr_ses
, cs_slot
);
2283 } else if (cr_ses
->seqid
!= cs_slot
->sl_seqid
+ 1) {
2284 status
= nfserr_seq_misordered
;
2287 } else if (unconf
) {
2288 struct nfs4_client
*old
;
2289 if (!same_creds(&unconf
->cl_cred
, &rqstp
->rq_cred
) ||
2290 !rpc_cmp_addr(sa
, (struct sockaddr
*) &unconf
->cl_addr
)) {
2291 status
= nfserr_clid_inuse
;
2294 status
= nfserr_wrong_cred
;
2295 if (!mach_creds_match(unconf
, rqstp
))
2297 cs_slot
= &unconf
->cl_cs_slot
;
2298 status
= check_slot_seqid(cr_ses
->seqid
, cs_slot
->sl_seqid
, 0);
2300 /* an unconfirmed replay returns misordered */
2301 status
= nfserr_seq_misordered
;
2304 old
= find_confirmed_client_by_name(&unconf
->cl_name
, nn
);
2306 status
= mark_client_expired(old
);
2311 move_to_confirmed(unconf
);
2314 status
= nfserr_stale_clientid
;
2319 * We do not support RDMA or persistent sessions
2321 cr_ses
->flags
&= ~SESSION4_PERSIST
;
2322 cr_ses
->flags
&= ~SESSION4_RDMA
;
2324 init_session(rqstp
, new, conf
, cr_ses
);
2325 nfsd4_init_conn(rqstp
, conn
, new);
2327 memcpy(cr_ses
->sessionid
.data
, new->se_sessionid
.data
,
2328 NFS4_MAX_SESSIONID_LEN
);
2329 cs_slot
->sl_seqid
++;
2330 cr_ses
->seqid
= cs_slot
->sl_seqid
;
2332 /* cache solo and embedded create sessions under the state lock */
2333 nfsd4_cache_create_session(cr_ses
, cs_slot
, status
);
2334 nfs4_unlock_state();
2337 nfs4_unlock_state();
2340 __free_session(new);
2341 out_release_drc_mem
:
2342 nfsd4_put_drc_mem(&cr_ses
->fore_channel
);
2346 static __be32
nfsd4_map_bcts_dir(u32
*dir
)
2349 case NFS4_CDFC4_FORE
:
2350 case NFS4_CDFC4_BACK
:
2352 case NFS4_CDFC4_FORE_OR_BOTH
:
2353 case NFS4_CDFC4_BACK_OR_BOTH
:
2354 *dir
= NFS4_CDFC4_BOTH
;
2357 return nfserr_inval
;
2360 __be32
nfsd4_backchannel_ctl(struct svc_rqst
*rqstp
, struct nfsd4_compound_state
*cstate
, struct nfsd4_backchannel_ctl
*bc
)
2362 struct nfsd4_session
*session
= cstate
->session
;
2363 struct nfsd_net
*nn
= net_generic(SVC_NET(rqstp
), nfsd_net_id
);
2366 status
= nfsd4_check_cb_sec(&bc
->bc_cb_sec
);
2369 spin_lock(&nn
->client_lock
);
2370 session
->se_cb_prog
= bc
->bc_cb_program
;
2371 session
->se_cb_sec
= bc
->bc_cb_sec
;
2372 spin_unlock(&nn
->client_lock
);
2374 nfsd4_probe_callback(session
->se_client
);
2379 __be32
nfsd4_bind_conn_to_session(struct svc_rqst
*rqstp
,
2380 struct nfsd4_compound_state
*cstate
,
2381 struct nfsd4_bind_conn_to_session
*bcts
)
2384 struct nfsd4_conn
*conn
;
2385 struct nfsd4_session
*session
;
2386 struct net
*net
= SVC_NET(rqstp
);
2387 struct nfsd_net
*nn
= net_generic(net
, nfsd_net_id
);
2389 if (!nfsd4_last_compound_op(rqstp
))
2390 return nfserr_not_only_op
;
2392 spin_lock(&nn
->client_lock
);
2393 session
= find_in_sessionid_hashtbl(&bcts
->sessionid
, net
, &status
);
2394 spin_unlock(&nn
->client_lock
);
2396 goto out_no_session
;
2397 status
= nfserr_wrong_cred
;
2398 if (!mach_creds_match(session
->se_client
, rqstp
))
2400 status
= nfsd4_map_bcts_dir(&bcts
->dir
);
2403 conn
= alloc_conn(rqstp
, bcts
->dir
);
2404 status
= nfserr_jukebox
;
2407 nfsd4_init_conn(rqstp
, conn
, session
);
2410 nfsd4_put_session(session
);
2412 nfs4_unlock_state();
2416 static bool nfsd4_compound_in_session(struct nfsd4_session
*session
, struct nfs4_sessionid
*sid
)
2420 return !memcmp(sid
, &session
->se_sessionid
, sizeof(*sid
));
2424 nfsd4_destroy_session(struct svc_rqst
*r
,
2425 struct nfsd4_compound_state
*cstate
,
2426 struct nfsd4_destroy_session
*sessionid
)
2428 struct nfsd4_session
*ses
;
2430 int ref_held_by_me
= 0;
2431 struct net
*net
= SVC_NET(r
);
2432 struct nfsd_net
*nn
= net_generic(net
, nfsd_net_id
);
2435 status
= nfserr_not_only_op
;
2436 if (nfsd4_compound_in_session(cstate
->session
, &sessionid
->sessionid
)) {
2437 if (!nfsd4_last_compound_op(r
))
2441 dump_sessionid(__func__
, &sessionid
->sessionid
);
2442 spin_lock(&nn
->client_lock
);
2443 ses
= find_in_sessionid_hashtbl(&sessionid
->sessionid
, net
, &status
);
2445 goto out_client_lock
;
2446 status
= nfserr_wrong_cred
;
2447 if (!mach_creds_match(ses
->se_client
, r
))
2448 goto out_put_session
;
2449 status
= mark_session_dead_locked(ses
, 1 + ref_held_by_me
);
2451 goto out_put_session
;
2452 unhash_session(ses
);
2453 spin_unlock(&nn
->client_lock
);
2455 nfsd4_probe_callback_sync(ses
->se_client
);
2457 spin_lock(&nn
->client_lock
);
2460 nfsd4_put_session_locked(ses
);
2462 spin_unlock(&nn
->client_lock
);
2464 nfs4_unlock_state();
2468 static struct nfsd4_conn
*__nfsd4_find_conn(struct svc_xprt
*xpt
, struct nfsd4_session
*s
)
2470 struct nfsd4_conn
*c
;
2472 list_for_each_entry(c
, &s
->se_conns
, cn_persession
) {
2473 if (c
->cn_xprt
== xpt
) {
2480 static __be32
nfsd4_sequence_check_conn(struct nfsd4_conn
*new, struct nfsd4_session
*ses
)
2482 struct nfs4_client
*clp
= ses
->se_client
;
2483 struct nfsd4_conn
*c
;
2484 __be32 status
= nfs_ok
;
2487 spin_lock(&clp
->cl_lock
);
2488 c
= __nfsd4_find_conn(new->cn_xprt
, ses
);
2491 status
= nfserr_conn_not_bound_to_session
;
2492 if (clp
->cl_mach_cred
)
2494 __nfsd4_hash_conn(new, ses
);
2495 spin_unlock(&clp
->cl_lock
);
2496 ret
= nfsd4_register_conn(new);
2498 /* oops; xprt is already down: */
2499 nfsd4_conn_lost(&new->cn_xpt_user
);
2502 spin_unlock(&clp
->cl_lock
);
2507 static bool nfsd4_session_too_many_ops(struct svc_rqst
*rqstp
, struct nfsd4_session
*session
)
2509 struct nfsd4_compoundargs
*args
= rqstp
->rq_argp
;
2511 return args
->opcnt
> session
->se_fchannel
.maxops
;
2514 static bool nfsd4_request_too_big(struct svc_rqst
*rqstp
,
2515 struct nfsd4_session
*session
)
2517 struct xdr_buf
*xb
= &rqstp
->rq_arg
;
2519 return xb
->len
> session
->se_fchannel
.maxreq_sz
;
2523 nfsd4_sequence(struct svc_rqst
*rqstp
,
2524 struct nfsd4_compound_state
*cstate
,
2525 struct nfsd4_sequence
*seq
)
2527 struct nfsd4_compoundres
*resp
= rqstp
->rq_resp
;
2528 struct xdr_stream
*xdr
= &resp
->xdr
;
2529 struct nfsd4_session
*session
;
2530 struct nfs4_client
*clp
;
2531 struct nfsd4_slot
*slot
;
2532 struct nfsd4_conn
*conn
;
2535 struct net
*net
= SVC_NET(rqstp
);
2536 struct nfsd_net
*nn
= net_generic(net
, nfsd_net_id
);
2538 if (resp
->opcnt
!= 1)
2539 return nfserr_sequence_pos
;
2542 * Will be either used or freed by nfsd4_sequence_check_conn
2545 conn
= alloc_conn(rqstp
, NFS4_CDFC4_FORE
);
2547 return nfserr_jukebox
;
2549 spin_lock(&nn
->client_lock
);
2550 session
= find_in_sessionid_hashtbl(&seq
->sessionid
, net
, &status
);
2552 goto out_no_session
;
2553 clp
= session
->se_client
;
2555 status
= nfserr_too_many_ops
;
2556 if (nfsd4_session_too_many_ops(rqstp
, session
))
2557 goto out_put_session
;
2559 status
= nfserr_req_too_big
;
2560 if (nfsd4_request_too_big(rqstp
, session
))
2561 goto out_put_session
;
2563 status
= nfserr_badslot
;
2564 if (seq
->slotid
>= session
->se_fchannel
.maxreqs
)
2565 goto out_put_session
;
2567 slot
= session
->se_slots
[seq
->slotid
];
2568 dprintk("%s: slotid %d\n", __func__
, seq
->slotid
);
2570 /* We do not negotiate the number of slots yet, so set the
2571 * maxslots to the session maxreqs which is used to encode
2572 * sr_highest_slotid and the sr_target_slot id to maxslots */
2573 seq
->maxslots
= session
->se_fchannel
.maxreqs
;
2575 status
= check_slot_seqid(seq
->seqid
, slot
->sl_seqid
,
2576 slot
->sl_flags
& NFSD4_SLOT_INUSE
);
2577 if (status
== nfserr_replay_cache
) {
2578 status
= nfserr_seq_misordered
;
2579 if (!(slot
->sl_flags
& NFSD4_SLOT_INITIALIZED
))
2580 goto out_put_session
;
2581 cstate
->slot
= slot
;
2582 cstate
->session
= session
;
2584 /* Return the cached reply status and set cstate->status
2585 * for nfsd4_proc_compound processing */
2586 status
= nfsd4_replay_cache_entry(resp
, seq
);
2587 cstate
->status
= nfserr_replay_cache
;
2591 goto out_put_session
;
2593 status
= nfsd4_sequence_check_conn(conn
, session
);
2596 goto out_put_session
;
2598 buflen
= (seq
->cachethis
) ?
2599 session
->se_fchannel
.maxresp_cached
:
2600 session
->se_fchannel
.maxresp_sz
;
2601 status
= (seq
->cachethis
) ? nfserr_rep_too_big_to_cache
:
2603 if (xdr_restrict_buflen(xdr
, buflen
- rqstp
->rq_auth_slack
))
2604 goto out_put_session
;
2605 svc_reserve(rqstp
, buflen
);
2608 /* Success! bump slot seqid */
2609 slot
->sl_seqid
= seq
->seqid
;
2610 slot
->sl_flags
|= NFSD4_SLOT_INUSE
;
2612 slot
->sl_flags
|= NFSD4_SLOT_CACHETHIS
;
2614 slot
->sl_flags
&= ~NFSD4_SLOT_CACHETHIS
;
2616 cstate
->slot
= slot
;
2617 cstate
->session
= session
;
2621 switch (clp
->cl_cb_state
) {
2623 seq
->status_flags
= SEQ4_STATUS_CB_PATH_DOWN
;
2625 case NFSD4_CB_FAULT
:
2626 seq
->status_flags
= SEQ4_STATUS_BACKCHANNEL_FAULT
;
2629 seq
->status_flags
= 0;
2631 if (!list_empty(&clp
->cl_revoked
))
2632 seq
->status_flags
|= SEQ4_STATUS_RECALLABLE_STATE_REVOKED
;
2636 spin_unlock(&nn
->client_lock
);
2639 nfsd4_put_session_locked(session
);
2640 goto out_no_session
;
2644 nfsd4_sequence_done(struct nfsd4_compoundres
*resp
)
2646 struct nfsd4_compound_state
*cs
= &resp
->cstate
;
2648 if (nfsd4_has_session(cs
)) {
2649 if (cs
->status
!= nfserr_replay_cache
) {
2650 nfsd4_store_cache_entry(resp
);
2651 cs
->slot
->sl_flags
&= ~NFSD4_SLOT_INUSE
;
2653 /* Drop session reference that was taken in nfsd4_sequence() */
2654 nfsd4_put_session(cs
->session
);
2656 put_client_renew(cs
->clp
);
2660 nfsd4_destroy_clientid(struct svc_rqst
*rqstp
, struct nfsd4_compound_state
*cstate
, struct nfsd4_destroy_clientid
*dc
)
2662 struct nfs4_client
*conf
, *unconf
, *clp
;
2664 struct nfsd_net
*nn
= net_generic(SVC_NET(rqstp
), nfsd_net_id
);
2667 unconf
= find_unconfirmed_client(&dc
->clientid
, true, nn
);
2668 conf
= find_confirmed_client(&dc
->clientid
, true, nn
);
2669 WARN_ON_ONCE(conf
&& unconf
);
2674 if (client_has_state(conf
)) {
2675 status
= nfserr_clientid_busy
;
2681 status
= nfserr_stale_clientid
;
2684 if (!mach_creds_match(clp
, rqstp
)) {
2685 status
= nfserr_wrong_cred
;
2690 nfs4_unlock_state();
2695 nfsd4_reclaim_complete(struct svc_rqst
*rqstp
, struct nfsd4_compound_state
*cstate
, struct nfsd4_reclaim_complete
*rc
)
2699 if (rc
->rca_one_fs
) {
2700 if (!cstate
->current_fh
.fh_dentry
)
2701 return nfserr_nofilehandle
;
2703 * We don't take advantage of the rca_one_fs case.
2704 * That's OK, it's optional, we can safely ignore it.
2710 status
= nfserr_complete_already
;
2711 if (test_and_set_bit(NFSD4_CLIENT_RECLAIM_COMPLETE
,
2712 &cstate
->session
->se_client
->cl_flags
))
2715 status
= nfserr_stale_clientid
;
2716 if (is_client_expired(cstate
->session
->se_client
))
2718 * The following error isn't really legal.
2719 * But we only get here if the client just explicitly
2720 * destroyed the client. Surely it no longer cares what
2721 * error it gets back on an operation for the dead
2727 nfsd4_client_record_create(cstate
->session
->se_client
);
2729 nfs4_unlock_state();
2734 nfsd4_setclientid(struct svc_rqst
*rqstp
, struct nfsd4_compound_state
*cstate
,
2735 struct nfsd4_setclientid
*setclid
)
2737 struct xdr_netobj clname
= setclid
->se_name
;
2738 nfs4_verifier clverifier
= setclid
->se_verf
;
2739 struct nfs4_client
*conf
, *unconf
, *new;
2741 struct nfsd_net
*nn
= net_generic(SVC_NET(rqstp
), nfsd_net_id
);
2743 /* Cases below refer to rfc 3530 section 14.2.33: */
2745 conf
= find_confirmed_client_by_name(&clname
, nn
);
2748 status
= nfserr_clid_inuse
;
2749 if (clp_used_exchangeid(conf
))
2751 if (!same_creds(&conf
->cl_cred
, &rqstp
->rq_cred
)) {
2752 char addr_str
[INET6_ADDRSTRLEN
];
2753 rpc_ntop((struct sockaddr
*) &conf
->cl_addr
, addr_str
,
2755 dprintk("NFSD: setclientid: string in use by client "
2756 "at %s\n", addr_str
);
2760 unconf
= find_unconfirmed_client_by_name(&clname
, nn
);
2762 expire_client(unconf
);
2763 status
= nfserr_jukebox
;
2764 new = create_client(clname
, rqstp
, &clverifier
);
2767 if (conf
&& same_verf(&conf
->cl_verifier
, &clverifier
))
2768 /* case 1: probable callback update */
2769 copy_clid(new, conf
);
2770 else /* case 4 (new client) or cases 2, 3 (client reboot): */
2772 new->cl_minorversion
= 0;
2773 gen_callback(new, setclid
, rqstp
);
2774 add_to_unconfirmed(new);
2775 setclid
->se_clientid
.cl_boot
= new->cl_clientid
.cl_boot
;
2776 setclid
->se_clientid
.cl_id
= new->cl_clientid
.cl_id
;
2777 memcpy(setclid
->se_confirm
.data
, new->cl_confirm
.data
, sizeof(setclid
->se_confirm
.data
));
2780 nfs4_unlock_state();
2786 nfsd4_setclientid_confirm(struct svc_rqst
*rqstp
,
2787 struct nfsd4_compound_state
*cstate
,
2788 struct nfsd4_setclientid_confirm
*setclientid_confirm
)
2790 struct nfs4_client
*conf
, *unconf
;
2791 nfs4_verifier confirm
= setclientid_confirm
->sc_confirm
;
2792 clientid_t
* clid
= &setclientid_confirm
->sc_clientid
;
2794 struct nfsd_net
*nn
= net_generic(SVC_NET(rqstp
), nfsd_net_id
);
2796 if (STALE_CLIENTID(clid
, nn
))
2797 return nfserr_stale_clientid
;
2800 conf
= find_confirmed_client(clid
, false, nn
);
2801 unconf
= find_unconfirmed_client(clid
, false, nn
);
2803 * We try hard to give out unique clientid's, so if we get an
2804 * attempt to confirm the same clientid with a different cred,
2805 * there's a bug somewhere. Let's charitably assume it's our
2808 status
= nfserr_serverfault
;
2809 if (unconf
&& !same_creds(&unconf
->cl_cred
, &rqstp
->rq_cred
))
2811 if (conf
&& !same_creds(&conf
->cl_cred
, &rqstp
->rq_cred
))
2813 /* cases below refer to rfc 3530 section 14.2.34: */
2814 if (!unconf
|| !same_verf(&confirm
, &unconf
->cl_confirm
)) {
2815 if (conf
&& !unconf
) /* case 2: probable retransmit */
2817 else /* case 4: client hasn't noticed we rebooted yet? */
2818 status
= nfserr_stale_clientid
;
2822 if (conf
) { /* case 1: callback update */
2823 nfsd4_change_callback(conf
, &unconf
->cl_cb_conn
);
2824 nfsd4_probe_callback(conf
);
2825 expire_client(unconf
);
2826 } else { /* case 3: normal case; new or rebooted client */
2827 conf
= find_confirmed_client_by_name(&unconf
->cl_name
, nn
);
2829 status
= mark_client_expired(conf
);
2832 expire_client(conf
);
2834 move_to_confirmed(unconf
);
2835 nfsd4_probe_callback(unconf
);
2838 nfs4_unlock_state();
2842 static struct nfs4_file
*nfsd4_alloc_file(void)
2844 return kmem_cache_alloc(file_slab
, GFP_KERNEL
);
2847 /* OPEN Share state helper functions */
2848 static void nfsd4_init_file(struct nfs4_file
*fp
, struct inode
*ino
,
2849 struct knfsd_fh
*fh
)
2851 unsigned int hashval
= file_hashval(fh
);
2853 lockdep_assert_held(&state_lock
);
2855 atomic_set(&fp
->fi_ref
, 1);
2856 spin_lock_init(&fp
->fi_lock
);
2857 INIT_LIST_HEAD(&fp
->fi_stateids
);
2858 INIT_LIST_HEAD(&fp
->fi_delegations
);
2861 fh_copy_shallow(&fp
->fi_fhandle
, fh
);
2862 fp
->fi_had_conflict
= false;
2863 fp
->fi_lease
= NULL
;
2864 fp
->fi_share_deny
= 0;
2865 memset(fp
->fi_fds
, 0, sizeof(fp
->fi_fds
));
2866 memset(fp
->fi_access
, 0, sizeof(fp
->fi_access
));
2867 hlist_add_head(&fp
->fi_hash
, &file_hashtbl
[hashval
]);
2871 nfsd4_free_slabs(void)
2873 kmem_cache_destroy(openowner_slab
);
2874 kmem_cache_destroy(lockowner_slab
);
2875 kmem_cache_destroy(file_slab
);
2876 kmem_cache_destroy(stateid_slab
);
2877 kmem_cache_destroy(deleg_slab
);
2881 nfsd4_init_slabs(void)
2883 openowner_slab
= kmem_cache_create("nfsd4_openowners",
2884 sizeof(struct nfs4_openowner
), 0, 0, NULL
);
2885 if (openowner_slab
== NULL
)
2887 lockowner_slab
= kmem_cache_create("nfsd4_lockowners",
2888 sizeof(struct nfs4_lockowner
), 0, 0, NULL
);
2889 if (lockowner_slab
== NULL
)
2890 goto out_free_openowner_slab
;
2891 file_slab
= kmem_cache_create("nfsd4_files",
2892 sizeof(struct nfs4_file
), 0, 0, NULL
);
2893 if (file_slab
== NULL
)
2894 goto out_free_lockowner_slab
;
2895 stateid_slab
= kmem_cache_create("nfsd4_stateids",
2896 sizeof(struct nfs4_ol_stateid
), 0, 0, NULL
);
2897 if (stateid_slab
== NULL
)
2898 goto out_free_file_slab
;
2899 deleg_slab
= kmem_cache_create("nfsd4_delegations",
2900 sizeof(struct nfs4_delegation
), 0, 0, NULL
);
2901 if (deleg_slab
== NULL
)
2902 goto out_free_stateid_slab
;
2905 out_free_stateid_slab
:
2906 kmem_cache_destroy(stateid_slab
);
2908 kmem_cache_destroy(file_slab
);
2909 out_free_lockowner_slab
:
2910 kmem_cache_destroy(lockowner_slab
);
2911 out_free_openowner_slab
:
2912 kmem_cache_destroy(openowner_slab
);
2914 dprintk("nfsd4: out of memory while initializing nfsv4\n");
2918 static void init_nfs4_replay(struct nfs4_replay
*rp
)
2920 rp
->rp_status
= nfserr_serverfault
;
2922 rp
->rp_buf
= rp
->rp_ibuf
;
2925 static inline void *alloc_stateowner(struct kmem_cache
*slab
, struct xdr_netobj
*owner
, struct nfs4_client
*clp
)
2927 struct nfs4_stateowner
*sop
;
2929 sop
= kmem_cache_alloc(slab
, GFP_KERNEL
);
2933 sop
->so_owner
.data
= kmemdup(owner
->data
, owner
->len
, GFP_KERNEL
);
2934 if (!sop
->so_owner
.data
) {
2935 kmem_cache_free(slab
, sop
);
2938 sop
->so_owner
.len
= owner
->len
;
2940 INIT_LIST_HEAD(&sop
->so_stateids
);
2941 sop
->so_client
= clp
;
2942 init_nfs4_replay(&sop
->so_replay
);
2946 static void hash_openowner(struct nfs4_openowner
*oo
, struct nfs4_client
*clp
, unsigned int strhashval
)
2948 struct nfsd_net
*nn
= net_generic(clp
->net
, nfsd_net_id
);
2950 list_add(&oo
->oo_owner
.so_strhash
, &nn
->ownerstr_hashtbl
[strhashval
]);
2951 list_add(&oo
->oo_perclient
, &clp
->cl_openowners
);
2954 static struct nfs4_openowner
*
2955 alloc_init_open_stateowner(unsigned int strhashval
, struct nfsd4_open
*open
,
2956 struct nfsd4_compound_state
*cstate
)
2958 struct nfs4_client
*clp
= cstate
->clp
;
2959 struct nfs4_openowner
*oo
;
2961 oo
= alloc_stateowner(openowner_slab
, &open
->op_owner
, clp
);
2964 oo
->oo_owner
.so_is_open_owner
= 1;
2965 oo
->oo_owner
.so_seqid
= open
->op_seqid
;
2966 oo
->oo_flags
= NFS4_OO_NEW
;
2967 if (nfsd4_has_session(cstate
))
2968 oo
->oo_flags
|= NFS4_OO_CONFIRMED
;
2970 oo
->oo_last_closed_stid
= NULL
;
2971 INIT_LIST_HEAD(&oo
->oo_close_lru
);
2972 hash_openowner(oo
, clp
, strhashval
);
2976 static void init_open_stateid(struct nfs4_ol_stateid
*stp
, struct nfs4_file
*fp
, struct nfsd4_open
*open
) {
2977 struct nfs4_openowner
*oo
= open
->op_openowner
;
2979 stp
->st_stid
.sc_type
= NFS4_OPEN_STID
;
2980 INIT_LIST_HEAD(&stp
->st_locks
);
2981 list_add(&stp
->st_perstateowner
, &oo
->oo_owner
.so_stateids
);
2982 stp
->st_stateowner
= &oo
->oo_owner
;
2985 stp
->st_access_bmap
= 0;
2986 stp
->st_deny_bmap
= 0;
2987 stp
->st_openstp
= NULL
;
2988 spin_lock(&fp
->fi_lock
);
2989 list_add(&stp
->st_perfile
, &fp
->fi_stateids
);
2990 spin_unlock(&fp
->fi_lock
);
2994 move_to_close_lru(struct nfs4_openowner
*oo
, struct net
*net
)
2996 struct nfsd_net
*nn
= net_generic(net
, nfsd_net_id
);
2998 dprintk("NFSD: move_to_close_lru nfs4_openowner %p\n", oo
);
3000 list_move_tail(&oo
->oo_close_lru
, &nn
->close_lru
);
3001 oo
->oo_time
= get_seconds();
3005 same_owner_str(struct nfs4_stateowner
*sop
, struct xdr_netobj
*owner
,
3008 return (sop
->so_owner
.len
== owner
->len
) &&
3009 0 == memcmp(sop
->so_owner
.data
, owner
->data
, owner
->len
) &&
3010 (sop
->so_client
->cl_clientid
.cl_id
== clid
->cl_id
);
3013 static struct nfs4_openowner
*
3014 find_openstateowner_str(unsigned int hashval
, struct nfsd4_open
*open
,
3015 bool sessions
, struct nfsd_net
*nn
)
3017 struct nfs4_stateowner
*so
;
3018 struct nfs4_openowner
*oo
;
3019 struct nfs4_client
*clp
;
3021 list_for_each_entry(so
, &nn
->ownerstr_hashtbl
[hashval
], so_strhash
) {
3022 if (!so
->so_is_open_owner
)
3024 if (same_owner_str(so
, &open
->op_owner
, &open
->op_clientid
)) {
3026 clp
= oo
->oo_owner
.so_client
;
3027 if ((bool)clp
->cl_minorversion
!= sessions
)
3029 renew_client(oo
->oo_owner
.so_client
);
3036 /* search file_hashtbl[] for file */
3037 static struct nfs4_file
*
3038 find_file_locked(struct knfsd_fh
*fh
)
3040 unsigned int hashval
= file_hashval(fh
);
3041 struct nfs4_file
*fp
;
3043 lockdep_assert_held(&state_lock
);
3045 hlist_for_each_entry(fp
, &file_hashtbl
[hashval
], fi_hash
) {
3046 if (nfsd_fh_match(&fp
->fi_fhandle
, fh
)) {
3054 static struct nfs4_file
*
3055 find_file(struct knfsd_fh
*fh
)
3057 struct nfs4_file
*fp
;
3059 spin_lock(&state_lock
);
3060 fp
= find_file_locked(fh
);
3061 spin_unlock(&state_lock
);
3065 static struct nfs4_file
*
3066 find_or_add_file(struct inode
*ino
, struct nfs4_file
*new, struct knfsd_fh
*fh
)
3068 struct nfs4_file
*fp
;
3070 spin_lock(&state_lock
);
3071 fp
= find_file_locked(fh
);
3073 nfsd4_init_file(new, ino
, fh
);
3076 spin_unlock(&state_lock
);
3082 * Called to check deny when READ with all zero stateid or
3083 * WRITE with all zero or all one stateid
3086 nfs4_share_conflict(struct svc_fh
*current_fh
, unsigned int deny_type
)
3088 struct nfs4_file
*fp
;
3089 __be32 ret
= nfs_ok
;
3091 fp
= find_file(¤t_fh
->fh_handle
);
3094 /* Check for conflicting share reservations */
3095 spin_lock(&fp
->fi_lock
);
3096 if (fp
->fi_share_deny
& deny_type
)
3097 ret
= nfserr_locked
;
3098 spin_unlock(&fp
->fi_lock
);
3103 void nfsd4_prepare_cb_recall(struct nfs4_delegation
*dp
)
3105 struct nfs4_client
*clp
= dp
->dl_stid
.sc_client
;
3106 struct nfsd_net
*nn
= net_generic(clp
->net
, nfsd_net_id
);
3109 * We can't do this in nfsd_break_deleg_cb because it is
3110 * already holding inode->i_lock
3112 spin_lock(&state_lock
);
3113 block_delegations(&dp
->dl_fh
);
3115 * If the dl_time != 0, then we know that it has already been
3116 * queued for a lease break. Don't queue it again.
3118 if (dp
->dl_time
== 0) {
3119 dp
->dl_time
= get_seconds();
3120 list_add_tail(&dp
->dl_recall_lru
, &nn
->del_recall_lru
);
3122 spin_unlock(&state_lock
);
3125 static void nfsd_break_one_deleg(struct nfs4_delegation
*dp
)
3128 * We're assuming the state code never drops its reference
3129 * without first removing the lease. Since we're in this lease
3130 * callback (and since the lease code is serialized by the kernel
3131 * lock) we know the server hasn't removed the lease yet, we know
3132 * it's safe to take a reference.
3134 atomic_inc(&dp
->dl_stid
.sc_count
);
3135 nfsd4_cb_recall(dp
);
3138 /* Called from break_lease() with i_lock held. */
3139 static void nfsd_break_deleg_cb(struct file_lock
*fl
)
3141 struct nfs4_file
*fp
= (struct nfs4_file
*)fl
->fl_owner
;
3142 struct nfs4_delegation
*dp
;
3145 WARN(1, "(%p)->fl_owner NULL\n", fl
);
3148 if (fp
->fi_had_conflict
) {
3149 WARN(1, "duplicate break on %p\n", fp
);
3153 * We don't want the locks code to timeout the lease for us;
3154 * we'll remove it ourself if a delegation isn't returned
3157 fl
->fl_break_time
= 0;
3159 spin_lock(&fp
->fi_lock
);
3160 fp
->fi_had_conflict
= true;
3162 * If there are no delegations on the list, then we can't count on this
3163 * lease ever being cleaned up. Set the fl_break_time to jiffies so that
3164 * time_out_leases will do it ASAP. The fact that fi_had_conflict is now
3165 * true should keep any new delegations from being hashed.
3167 if (list_empty(&fp
->fi_delegations
))
3168 fl
->fl_break_time
= jiffies
;
3170 list_for_each_entry(dp
, &fp
->fi_delegations
, dl_perfile
)
3171 nfsd_break_one_deleg(dp
);
3172 spin_unlock(&fp
->fi_lock
);
3176 int nfsd_change_deleg_cb(struct file_lock
**onlist
, int arg
)
3179 return lease_modify(onlist
, arg
);
3184 static const struct lock_manager_operations nfsd_lease_mng_ops
= {
3185 .lm_break
= nfsd_break_deleg_cb
,
3186 .lm_change
= nfsd_change_deleg_cb
,
3189 static __be32
nfsd4_check_seqid(struct nfsd4_compound_state
*cstate
, struct nfs4_stateowner
*so
, u32 seqid
)
3191 if (nfsd4_has_session(cstate
))
3193 if (seqid
== so
->so_seqid
- 1)
3194 return nfserr_replay_me
;
3195 if (seqid
== so
->so_seqid
)
3197 return nfserr_bad_seqid
;
3200 static __be32
lookup_clientid(clientid_t
*clid
,
3201 struct nfsd4_compound_state
*cstate
,
3202 struct nfsd_net
*nn
)
3204 struct nfs4_client
*found
;
3207 found
= cstate
->clp
;
3208 if (!same_clid(&found
->cl_clientid
, clid
))
3209 return nfserr_stale_clientid
;
3213 if (STALE_CLIENTID(clid
, nn
))
3214 return nfserr_stale_clientid
;
3217 * For v4.1+ we get the client in the SEQUENCE op. If we don't have one
3218 * cached already then we know this is for is for v4.0 and "sessions"
3221 WARN_ON_ONCE(cstate
->session
);
3222 found
= find_confirmed_client(clid
, false, nn
);
3224 return nfserr_expired
;
3226 /* Cache the nfs4_client in cstate! */
3227 cstate
->clp
= found
;
3228 atomic_inc(&found
->cl_refcount
);
3233 nfsd4_process_open1(struct nfsd4_compound_state
*cstate
,
3234 struct nfsd4_open
*open
, struct nfsd_net
*nn
)
3236 clientid_t
*clientid
= &open
->op_clientid
;
3237 struct nfs4_client
*clp
= NULL
;
3238 unsigned int strhashval
;
3239 struct nfs4_openowner
*oo
= NULL
;
3242 if (STALE_CLIENTID(&open
->op_clientid
, nn
))
3243 return nfserr_stale_clientid
;
3245 * In case we need it later, after we've already created the
3246 * file and don't want to risk a further failure:
3248 open
->op_file
= nfsd4_alloc_file();
3249 if (open
->op_file
== NULL
)
3250 return nfserr_jukebox
;
3252 status
= lookup_clientid(clientid
, cstate
, nn
);
3257 strhashval
= ownerstr_hashval(clientid
->cl_id
, &open
->op_owner
);
3258 oo
= find_openstateowner_str(strhashval
, open
, cstate
->minorversion
, nn
);
3259 open
->op_openowner
= oo
;
3263 if (!(oo
->oo_flags
& NFS4_OO_CONFIRMED
)) {
3264 /* Replace unconfirmed owners without checking for replay. */
3265 release_openowner(oo
);
3266 open
->op_openowner
= NULL
;
3269 status
= nfsd4_check_seqid(cstate
, &oo
->oo_owner
, open
->op_seqid
);
3274 oo
= alloc_init_open_stateowner(strhashval
, open
, cstate
);
3276 return nfserr_jukebox
;
3277 open
->op_openowner
= oo
;
3279 open
->op_stp
= nfs4_alloc_stateid(clp
);
3281 return nfserr_jukebox
;
3285 static inline __be32
3286 nfs4_check_delegmode(struct nfs4_delegation
*dp
, int flags
)
3288 if ((flags
& WR_STATE
) && (dp
->dl_type
== NFS4_OPEN_DELEGATE_READ
))
3289 return nfserr_openmode
;
3294 static int share_access_to_flags(u32 share_access
)
3296 return share_access
== NFS4_SHARE_ACCESS_READ
? RD_STATE
: WR_STATE
;
3299 static struct nfs4_delegation
*find_deleg_stateid(struct nfs4_client
*cl
, stateid_t
*s
)
3301 struct nfs4_stid
*ret
;
3303 ret
= find_stateid_by_type(cl
, s
, NFS4_DELEG_STID
);
3306 return delegstateid(ret
);
3309 static bool nfsd4_is_deleg_cur(struct nfsd4_open
*open
)
3311 return open
->op_claim_type
== NFS4_OPEN_CLAIM_DELEGATE_CUR
||
3312 open
->op_claim_type
== NFS4_OPEN_CLAIM_DELEG_CUR_FH
;
3316 nfs4_check_deleg(struct nfs4_client
*cl
, struct nfsd4_open
*open
,
3317 struct nfs4_delegation
**dp
)
3320 __be32 status
= nfserr_bad_stateid
;
3322 *dp
= find_deleg_stateid(cl
, &open
->op_delegate_stateid
);
3325 flags
= share_access_to_flags(open
->op_share_access
);
3326 status
= nfs4_check_delegmode(*dp
, flags
);
3330 if (!nfsd4_is_deleg_cur(open
))
3334 open
->op_openowner
->oo_flags
|= NFS4_OO_CONFIRMED
;
3338 static struct nfs4_ol_stateid
*
3339 nfsd4_find_existing_open(struct nfs4_file
*fp
, struct nfsd4_open
*open
)
3341 struct nfs4_ol_stateid
*local
, *ret
= NULL
;
3342 struct nfs4_openowner
*oo
= open
->op_openowner
;
3344 spin_lock(&fp
->fi_lock
);
3345 list_for_each_entry(local
, &fp
->fi_stateids
, st_perfile
) {
3346 /* ignore lock owners */
3347 if (local
->st_stateowner
->so_is_open_owner
== 0)
3349 if (local
->st_stateowner
== &oo
->oo_owner
) {
3354 spin_unlock(&fp
->fi_lock
);
3358 static inline int nfs4_access_to_access(u32 nfs4_access
)
3362 if (nfs4_access
& NFS4_SHARE_ACCESS_READ
)
3363 flags
|= NFSD_MAY_READ
;
3364 if (nfs4_access
& NFS4_SHARE_ACCESS_WRITE
)
3365 flags
|= NFSD_MAY_WRITE
;
3369 static inline __be32
3370 nfsd4_truncate(struct svc_rqst
*rqstp
, struct svc_fh
*fh
,
3371 struct nfsd4_open
*open
)
3373 struct iattr iattr
= {
3374 .ia_valid
= ATTR_SIZE
,
3377 if (!open
->op_truncate
)
3379 if (!(open
->op_share_access
& NFS4_SHARE_ACCESS_WRITE
))
3380 return nfserr_inval
;
3381 return nfsd_setattr(rqstp
, fh
, &iattr
, 0, (time_t)0);
3384 static __be32
nfs4_get_vfs_file(struct svc_rqst
*rqstp
, struct nfs4_file
*fp
,
3385 struct svc_fh
*cur_fh
, struct nfs4_ol_stateid
*stp
,
3386 struct nfsd4_open
*open
)
3388 struct file
*filp
= NULL
;
3390 int oflag
= nfs4_access_to_omode(open
->op_share_access
);
3391 int access
= nfs4_access_to_access(open
->op_share_access
);
3392 unsigned char old_access_bmap
, old_deny_bmap
;
3394 spin_lock(&fp
->fi_lock
);
3397 * Are we trying to set a deny mode that would conflict with
3400 status
= nfs4_file_check_deny(fp
, open
->op_share_deny
);
3401 if (status
!= nfs_ok
) {
3402 spin_unlock(&fp
->fi_lock
);
3406 /* set access to the file */
3407 status
= nfs4_file_get_access(fp
, open
->op_share_access
);
3408 if (status
!= nfs_ok
) {
3409 spin_unlock(&fp
->fi_lock
);
3413 /* Set access bits in stateid */
3414 old_access_bmap
= stp
->st_access_bmap
;
3415 set_access(open
->op_share_access
, stp
);
3417 /* Set new deny mask */
3418 old_deny_bmap
= stp
->st_deny_bmap
;
3419 set_deny(open
->op_share_deny
, stp
);
3420 fp
->fi_share_deny
|= (open
->op_share_deny
& NFS4_SHARE_DENY_BOTH
);
3422 if (!fp
->fi_fds
[oflag
]) {
3423 spin_unlock(&fp
->fi_lock
);
3424 status
= nfsd_open(rqstp
, cur_fh
, S_IFREG
, access
, &filp
);
3426 goto out_put_access
;
3427 spin_lock(&fp
->fi_lock
);
3428 if (!fp
->fi_fds
[oflag
]) {
3429 fp
->fi_fds
[oflag
] = filp
;
3433 spin_unlock(&fp
->fi_lock
);
3437 status
= nfsd4_truncate(rqstp
, cur_fh
, open
);
3439 goto out_put_access
;
3443 stp
->st_access_bmap
= old_access_bmap
;
3444 nfs4_file_put_access(fp
, open
->op_share_access
);
3445 reset_union_bmap_deny(bmap_to_share_mode(old_deny_bmap
), stp
);
3450 nfs4_upgrade_open(struct svc_rqst
*rqstp
, struct nfs4_file
*fp
, struct svc_fh
*cur_fh
, struct nfs4_ol_stateid
*stp
, struct nfsd4_open
*open
)
3453 unsigned char old_deny_bmap
;
3455 if (!test_access(open
->op_share_access
, stp
))
3456 return nfs4_get_vfs_file(rqstp
, fp
, cur_fh
, stp
, open
);
3458 /* test and set deny mode */
3459 spin_lock(&fp
->fi_lock
);
3460 status
= nfs4_file_check_deny(fp
, open
->op_share_deny
);
3461 if (status
== nfs_ok
) {
3462 old_deny_bmap
= stp
->st_deny_bmap
;
3463 set_deny(open
->op_share_deny
, stp
);
3464 fp
->fi_share_deny
|=
3465 (open
->op_share_deny
& NFS4_SHARE_DENY_BOTH
);
3467 spin_unlock(&fp
->fi_lock
);
3469 if (status
!= nfs_ok
)
3472 status
= nfsd4_truncate(rqstp
, cur_fh
, open
);
3473 if (status
!= nfs_ok
)
3474 reset_union_bmap_deny(old_deny_bmap
, stp
);
3479 nfs4_set_claim_prev(struct nfsd4_open
*open
, bool has_session
)
3481 open
->op_openowner
->oo_flags
|= NFS4_OO_CONFIRMED
;
3484 /* Should we give out recallable state?: */
3485 static bool nfsd4_cb_channel_good(struct nfs4_client
*clp
)
3487 if (clp
->cl_cb_state
== NFSD4_CB_UP
)
3490 * In the sessions case, since we don't have to establish a
3491 * separate connection for callbacks, we assume it's OK
3492 * until we hear otherwise:
3494 return clp
->cl_minorversion
&& clp
->cl_cb_state
== NFSD4_CB_UNKNOWN
;
3497 static struct file_lock
*nfs4_alloc_init_lease(struct nfs4_file
*fp
, int flag
)
3499 struct file_lock
*fl
;
3501 fl
= locks_alloc_lock();
3504 locks_init_lock(fl
);
3505 fl
->fl_lmops
= &nfsd_lease_mng_ops
;
3506 fl
->fl_flags
= FL_DELEG
;
3507 fl
->fl_type
= flag
== NFS4_OPEN_DELEGATE_READ
? F_RDLCK
: F_WRLCK
;
3508 fl
->fl_end
= OFFSET_MAX
;
3509 fl
->fl_owner
= (fl_owner_t
)fp
;
3510 fl
->fl_pid
= current
->tgid
;
3514 static int nfs4_setlease(struct nfs4_delegation
*dp
)
3516 struct nfs4_file
*fp
= dp
->dl_file
;
3517 struct file_lock
*fl
;
3521 fl
= nfs4_alloc_init_lease(fp
, NFS4_OPEN_DELEGATE_READ
);
3524 filp
= find_readable_file(fp
);
3526 /* We should always have a readable file here */
3531 status
= vfs_setlease(filp
, fl
->fl_type
, &fl
);
3533 locks_free_lock(fl
);
3536 spin_lock(&state_lock
);
3537 spin_lock(&fp
->fi_lock
);
3538 /* Did the lease get broken before we took the lock? */
3540 if (fp
->fi_had_conflict
)
3545 atomic_inc(&fp
->fi_delegees
);
3546 hash_delegation_locked(dp
, fp
);
3550 fp
->fi_deleg_file
= filp
;
3551 atomic_set(&fp
->fi_delegees
, 1);
3552 hash_delegation_locked(dp
, fp
);
3553 spin_unlock(&fp
->fi_lock
);
3554 spin_unlock(&state_lock
);
3557 spin_unlock(&fp
->fi_lock
);
3558 spin_unlock(&state_lock
);
3564 static int nfs4_set_delegation(struct nfs4_delegation
*dp
, struct nfs4_file
*fp
)
3568 if (fp
->fi_had_conflict
)
3571 spin_lock(&state_lock
);
3572 spin_lock(&fp
->fi_lock
);
3574 if (!fp
->fi_lease
) {
3575 spin_unlock(&fp
->fi_lock
);
3576 spin_unlock(&state_lock
);
3577 return nfs4_setlease(dp
);
3579 atomic_inc(&fp
->fi_delegees
);
3580 if (fp
->fi_had_conflict
) {
3584 hash_delegation_locked(dp
, fp
);
3586 spin_unlock(&fp
->fi_lock
);
3587 spin_unlock(&state_lock
);
3591 static void nfsd4_open_deleg_none_ext(struct nfsd4_open
*open
, int status
)
3593 open
->op_delegate_type
= NFS4_OPEN_DELEGATE_NONE_EXT
;
3594 if (status
== -EAGAIN
)
3595 open
->op_why_no_deleg
= WND4_CONTENTION
;
3597 open
->op_why_no_deleg
= WND4_RESOURCE
;
3598 switch (open
->op_deleg_want
) {
3599 case NFS4_SHARE_WANT_READ_DELEG
:
3600 case NFS4_SHARE_WANT_WRITE_DELEG
:
3601 case NFS4_SHARE_WANT_ANY_DELEG
:
3603 case NFS4_SHARE_WANT_CANCEL
:
3604 open
->op_why_no_deleg
= WND4_CANCELLED
;
3606 case NFS4_SHARE_WANT_NO_DELEG
:
3613 * Attempt to hand out a delegation.
3615 * Note we don't support write delegations, and won't until the vfs has
3616 * proper support for them.
3619 nfs4_open_delegation(struct net
*net
, struct svc_fh
*fh
,
3620 struct nfsd4_open
*open
, struct nfs4_ol_stateid
*stp
)
3622 struct nfs4_delegation
*dp
;
3623 struct nfs4_openowner
*oo
= container_of(stp
->st_stateowner
, struct nfs4_openowner
, oo_owner
);
3627 cb_up
= nfsd4_cb_channel_good(oo
->oo_owner
.so_client
);
3628 open
->op_recall
= 0;
3629 switch (open
->op_claim_type
) {
3630 case NFS4_OPEN_CLAIM_PREVIOUS
:
3632 open
->op_recall
= 1;
3633 if (open
->op_delegate_type
!= NFS4_OPEN_DELEGATE_READ
)
3636 case NFS4_OPEN_CLAIM_NULL
:
3637 case NFS4_OPEN_CLAIM_FH
:
3639 * Let's not give out any delegations till everyone's
3640 * had the chance to reclaim theirs....
3642 if (locks_in_grace(net
))
3644 if (!cb_up
|| !(oo
->oo_flags
& NFS4_OO_CONFIRMED
))
3647 * Also, if the file was opened for write or
3648 * create, there's a good chance the client's
3649 * about to write to it, resulting in an
3650 * immediate recall (since we don't support
3651 * write delegations):
3653 if (open
->op_share_access
& NFS4_SHARE_ACCESS_WRITE
)
3655 if (open
->op_create
== NFS4_OPEN_CREATE
)
3661 dp
= alloc_init_deleg(oo
->oo_owner
.so_client
, stp
, fh
);
3664 status
= nfs4_set_delegation(dp
, stp
->st_file
);
3668 memcpy(&open
->op_delegate_stateid
, &dp
->dl_stid
.sc_stateid
, sizeof(dp
->dl_stid
.sc_stateid
));
3670 dprintk("NFSD: delegation stateid=" STATEID_FMT
"\n",
3671 STATEID_VAL(&dp
->dl_stid
.sc_stateid
));
3672 open
->op_delegate_type
= NFS4_OPEN_DELEGATE_READ
;
3675 destroy_delegation(dp
);
3677 open
->op_delegate_type
= NFS4_OPEN_DELEGATE_NONE
;
3678 if (open
->op_claim_type
== NFS4_OPEN_CLAIM_PREVIOUS
&&
3679 open
->op_delegate_type
!= NFS4_OPEN_DELEGATE_NONE
) {
3680 dprintk("NFSD: WARNING: refusing delegation reclaim\n");
3681 open
->op_recall
= 1;
3684 /* 4.1 client asking for a delegation? */
3685 if (open
->op_deleg_want
)
3686 nfsd4_open_deleg_none_ext(open
, status
);
3690 static void nfsd4_deleg_xgrade_none_ext(struct nfsd4_open
*open
,
3691 struct nfs4_delegation
*dp
)
3693 if (open
->op_deleg_want
== NFS4_SHARE_WANT_READ_DELEG
&&
3694 dp
->dl_type
== NFS4_OPEN_DELEGATE_WRITE
) {
3695 open
->op_delegate_type
= NFS4_OPEN_DELEGATE_NONE_EXT
;
3696 open
->op_why_no_deleg
= WND4_NOT_SUPP_DOWNGRADE
;
3697 } else if (open
->op_deleg_want
== NFS4_SHARE_WANT_WRITE_DELEG
&&
3698 dp
->dl_type
== NFS4_OPEN_DELEGATE_WRITE
) {
3699 open
->op_delegate_type
= NFS4_OPEN_DELEGATE_NONE_EXT
;
3700 open
->op_why_no_deleg
= WND4_NOT_SUPP_UPGRADE
;
3702 /* Otherwise the client must be confused wanting a delegation
3703 * it already has, therefore we don't return
3704 * NFS4_OPEN_DELEGATE_NONE_EXT and reason.
3709 * called with nfs4_lock_state() held.
3712 nfsd4_process_open2(struct svc_rqst
*rqstp
, struct svc_fh
*current_fh
, struct nfsd4_open
*open
)
3714 struct nfsd4_compoundres
*resp
= rqstp
->rq_resp
;
3715 struct nfs4_client
*cl
= open
->op_openowner
->oo_owner
.so_client
;
3716 struct nfs4_file
*fp
= NULL
;
3717 struct inode
*ino
= current_fh
->fh_dentry
->d_inode
;
3718 struct nfs4_ol_stateid
*stp
= NULL
;
3719 struct nfs4_delegation
*dp
= NULL
;
3723 * Lookup file; if found, lookup stateid and check open request,
3724 * and check for delegations in the process of being recalled.
3725 * If not found, create the nfs4_file struct
3727 fp
= find_or_add_file(ino
, open
->op_file
, ¤t_fh
->fh_handle
);
3728 if (fp
!= open
->op_file
) {
3729 status
= nfs4_check_deleg(cl
, open
, &dp
);
3732 stp
= nfsd4_find_existing_open(fp
, open
);
3734 open
->op_file
= NULL
;
3735 status
= nfserr_bad_stateid
;
3736 if (nfsd4_is_deleg_cur(open
))
3738 status
= nfserr_jukebox
;
3742 * OPEN the file, or upgrade an existing OPEN.
3743 * If truncate fails, the OPEN fails.
3746 /* Stateid was found, this is an OPEN upgrade */
3747 status
= nfs4_upgrade_open(rqstp
, fp
, current_fh
, stp
, open
);
3752 open
->op_stp
= NULL
;
3753 init_open_stateid(stp
, fp
, open
);
3754 status
= nfs4_get_vfs_file(rqstp
, fp
, current_fh
, stp
, open
);
3756 release_open_stateid(stp
);
3760 update_stateid(&stp
->st_stid
.sc_stateid
);
3761 memcpy(&open
->op_stateid
, &stp
->st_stid
.sc_stateid
, sizeof(stateid_t
));
3763 if (nfsd4_has_session(&resp
->cstate
)) {
3764 if (open
->op_deleg_want
& NFS4_SHARE_WANT_NO_DELEG
) {
3765 open
->op_delegate_type
= NFS4_OPEN_DELEGATE_NONE_EXT
;
3766 open
->op_why_no_deleg
= WND4_NOT_WANTED
;
3772 * Attempt to hand out a delegation. No error return, because the
3773 * OPEN succeeds even if we fail.
3775 nfs4_open_delegation(SVC_NET(rqstp
), current_fh
, open
, stp
);
3779 dprintk("%s: stateid=" STATEID_FMT
"\n", __func__
,
3780 STATEID_VAL(&stp
->st_stid
.sc_stateid
));
3782 /* 4.1 client trying to upgrade/downgrade delegation? */
3783 if (open
->op_delegate_type
== NFS4_OPEN_DELEGATE_NONE
&& dp
&&
3784 open
->op_deleg_want
)
3785 nfsd4_deleg_xgrade_none_ext(open
, dp
);
3789 if (status
== 0 && open
->op_claim_type
== NFS4_OPEN_CLAIM_PREVIOUS
)
3790 nfs4_set_claim_prev(open
, nfsd4_has_session(&resp
->cstate
));
3792 * To finish the open response, we just need to set the rflags.
3794 open
->op_rflags
= NFS4_OPEN_RESULT_LOCKTYPE_POSIX
;
3795 if (!(open
->op_openowner
->oo_flags
& NFS4_OO_CONFIRMED
) &&
3796 !nfsd4_has_session(&resp
->cstate
))
3797 open
->op_rflags
|= NFS4_OPEN_RESULT_CONFIRM
;
3802 void nfsd4_cleanup_open_state(struct nfsd4_open
*open
, __be32 status
)
3804 if (open
->op_openowner
) {
3805 struct nfs4_openowner
*oo
= open
->op_openowner
;
3807 if (!list_empty(&oo
->oo_owner
.so_stateids
))
3808 list_del_init(&oo
->oo_close_lru
);
3809 if (oo
->oo_flags
& NFS4_OO_NEW
) {
3811 release_openowner(oo
);
3812 open
->op_openowner
= NULL
;
3814 oo
->oo_flags
&= ~NFS4_OO_NEW
;
3818 nfsd4_free_file(open
->op_file
);
3820 free_generic_stateid(open
->op_stp
);
3824 nfsd4_renew(struct svc_rqst
*rqstp
, struct nfsd4_compound_state
*cstate
,
3827 struct nfs4_client
*clp
;
3829 struct nfsd_net
*nn
= net_generic(SVC_NET(rqstp
), nfsd_net_id
);
3832 dprintk("process_renew(%08x/%08x): starting\n",
3833 clid
->cl_boot
, clid
->cl_id
);
3834 status
= lookup_clientid(clid
, cstate
, nn
);
3838 status
= nfserr_cb_path_down
;
3839 if (!list_empty(&clp
->cl_delegations
)
3840 && clp
->cl_cb_state
!= NFSD4_CB_UP
)
3844 nfs4_unlock_state();
3849 nfsd4_end_grace(struct nfsd_net
*nn
)
3851 /* do nothing if grace period already ended */
3852 if (nn
->grace_ended
)
3855 dprintk("NFSD: end of grace period\n");
3856 nn
->grace_ended
= true;
3857 nfsd4_record_grace_done(nn
, nn
->boot_time
);
3858 locks_end_grace(&nn
->nfsd4_manager
);
3860 * Now that every NFSv4 client has had the chance to recover and
3861 * to see the (possibly new, possibly shorter) lease time, we
3862 * can safely set the next grace time to the current lease time:
3864 nn
->nfsd4_grace
= nn
->nfsd4_lease
;
3868 nfs4_laundromat(struct nfsd_net
*nn
)
3870 struct nfs4_client
*clp
;
3871 struct nfs4_openowner
*oo
;
3872 struct nfs4_delegation
*dp
;
3873 struct list_head
*pos
, *next
, reaplist
;
3874 time_t cutoff
= get_seconds() - nn
->nfsd4_lease
;
3875 time_t t
, new_timeo
= nn
->nfsd4_lease
;
3879 dprintk("NFSD: laundromat service - starting\n");
3880 nfsd4_end_grace(nn
);
3881 INIT_LIST_HEAD(&reaplist
);
3882 spin_lock(&nn
->client_lock
);
3883 list_for_each_safe(pos
, next
, &nn
->client_lru
) {
3884 clp
= list_entry(pos
, struct nfs4_client
, cl_lru
);
3885 if (time_after((unsigned long)clp
->cl_time
, (unsigned long)cutoff
)) {
3886 t
= clp
->cl_time
- cutoff
;
3887 new_timeo
= min(new_timeo
, t
);
3890 if (mark_client_expired_locked(clp
)) {
3891 dprintk("NFSD: client in use (clientid %08x)\n",
3892 clp
->cl_clientid
.cl_id
);
3895 list_move(&clp
->cl_lru
, &reaplist
);
3897 spin_unlock(&nn
->client_lock
);
3898 list_for_each_safe(pos
, next
, &reaplist
) {
3899 clp
= list_entry(pos
, struct nfs4_client
, cl_lru
);
3900 dprintk("NFSD: purging unused client (clientid %08x)\n",
3901 clp
->cl_clientid
.cl_id
);
3904 spin_lock(&state_lock
);
3905 list_for_each_safe(pos
, next
, &nn
->del_recall_lru
) {
3906 dp
= list_entry (pos
, struct nfs4_delegation
, dl_recall_lru
);
3907 if (net_generic(dp
->dl_stid
.sc_client
->net
, nfsd_net_id
) != nn
)
3909 if (time_after((unsigned long)dp
->dl_time
, (unsigned long)cutoff
)) {
3910 t
= dp
->dl_time
- cutoff
;
3911 new_timeo
= min(new_timeo
, t
);
3914 list_move(&dp
->dl_recall_lru
, &reaplist
);
3916 spin_unlock(&state_lock
);
3917 list_for_each_safe(pos
, next
, &reaplist
) {
3918 dp
= list_entry (pos
, struct nfs4_delegation
, dl_recall_lru
);
3919 revoke_delegation(dp
);
3921 list_for_each_safe(pos
, next
, &nn
->close_lru
) {
3922 oo
= container_of(pos
, struct nfs4_openowner
, oo_close_lru
);
3923 if (time_after((unsigned long)oo
->oo_time
, (unsigned long)cutoff
)) {
3924 t
= oo
->oo_time
- cutoff
;
3925 new_timeo
= min(new_timeo
, t
);
3928 release_openowner(oo
);
3930 new_timeo
= max_t(time_t, new_timeo
, NFSD_LAUNDROMAT_MINTIMEOUT
);
3931 nfs4_unlock_state();
3935 static struct workqueue_struct
*laundry_wq
;
3936 static void laundromat_main(struct work_struct
*);
3939 laundromat_main(struct work_struct
*laundry
)
3942 struct delayed_work
*dwork
= container_of(laundry
, struct delayed_work
,
3944 struct nfsd_net
*nn
= container_of(dwork
, struct nfsd_net
,
3947 t
= nfs4_laundromat(nn
);
3948 dprintk("NFSD: laundromat_main - sleeping for %ld seconds\n", t
);
3949 queue_delayed_work(laundry_wq
, &nn
->laundromat_work
, t
*HZ
);
3952 static inline __be32
nfs4_check_fh(struct svc_fh
*fhp
, struct nfs4_ol_stateid
*stp
)
3954 if (!nfsd_fh_match(&fhp
->fh_handle
, &stp
->st_file
->fi_fhandle
))
3955 return nfserr_bad_stateid
;
3960 access_permit_read(struct nfs4_ol_stateid
*stp
)
3962 return test_access(NFS4_SHARE_ACCESS_READ
, stp
) ||
3963 test_access(NFS4_SHARE_ACCESS_BOTH
, stp
) ||
3964 test_access(NFS4_SHARE_ACCESS_WRITE
, stp
);
3968 access_permit_write(struct nfs4_ol_stateid
*stp
)
3970 return test_access(NFS4_SHARE_ACCESS_WRITE
, stp
) ||
3971 test_access(NFS4_SHARE_ACCESS_BOTH
, stp
);
3975 __be32
nfs4_check_openmode(struct nfs4_ol_stateid
*stp
, int flags
)
3977 __be32 status
= nfserr_openmode
;
3979 /* For lock stateid's, we test the parent open, not the lock: */
3980 if (stp
->st_openstp
)
3981 stp
= stp
->st_openstp
;
3982 if ((flags
& WR_STATE
) && !access_permit_write(stp
))
3984 if ((flags
& RD_STATE
) && !access_permit_read(stp
))
3991 static inline __be32
3992 check_special_stateids(struct net
*net
, svc_fh
*current_fh
, stateid_t
*stateid
, int flags
)
3994 if (ONE_STATEID(stateid
) && (flags
& RD_STATE
))
3996 else if (locks_in_grace(net
)) {
3997 /* Answer in remaining cases depends on existence of
3998 * conflicting state; so we must wait out the grace period. */
3999 return nfserr_grace
;
4000 } else if (flags
& WR_STATE
)
4001 return nfs4_share_conflict(current_fh
,
4002 NFS4_SHARE_DENY_WRITE
);
4003 else /* (flags & RD_STATE) && ZERO_STATEID(stateid) */
4004 return nfs4_share_conflict(current_fh
,
4005 NFS4_SHARE_DENY_READ
);
4009 * Allow READ/WRITE during grace period on recovered state only for files
4010 * that are not able to provide mandatory locking.
4013 grace_disallows_io(struct net
*net
, struct inode
*inode
)
4015 return locks_in_grace(net
) && mandatory_lock(inode
);
4018 /* Returns true iff a is later than b: */
4019 static bool stateid_generation_after(stateid_t
*a
, stateid_t
*b
)
4021 return (s32
)(a
->si_generation
- b
->si_generation
) > 0;
4024 static __be32
check_stateid_generation(stateid_t
*in
, stateid_t
*ref
, bool has_session
)
4027 * When sessions are used the stateid generation number is ignored
4030 if (has_session
&& in
->si_generation
== 0)
4033 if (in
->si_generation
== ref
->si_generation
)
4036 /* If the client sends us a stateid from the future, it's buggy: */
4037 if (stateid_generation_after(in
, ref
))
4038 return nfserr_bad_stateid
;
4040 * However, we could see a stateid from the past, even from a
4041 * non-buggy client. For example, if the client sends a lock
4042 * while some IO is outstanding, the lock may bump si_generation
4043 * while the IO is still in flight. The client could avoid that
4044 * situation by waiting for responses on all the IO requests,
4045 * but better performance may result in retrying IO that
4046 * receives an old_stateid error if requests are rarely
4047 * reordered in flight:
4049 return nfserr_old_stateid
;
4052 static __be32
nfsd4_validate_stateid(struct nfs4_client
*cl
, stateid_t
*stateid
)
4054 struct nfs4_stid
*s
;
4055 struct nfs4_ol_stateid
*ols
;
4058 if (ZERO_STATEID(stateid
) || ONE_STATEID(stateid
))
4059 return nfserr_bad_stateid
;
4060 /* Client debugging aid. */
4061 if (!same_clid(&stateid
->si_opaque
.so_clid
, &cl
->cl_clientid
)) {
4062 char addr_str
[INET6_ADDRSTRLEN
];
4063 rpc_ntop((struct sockaddr
*)&cl
->cl_addr
, addr_str
,
4065 pr_warn_ratelimited("NFSD: client %s testing state ID "
4066 "with incorrect client ID\n", addr_str
);
4067 return nfserr_bad_stateid
;
4069 s
= find_stateid(cl
, stateid
);
4071 return nfserr_bad_stateid
;
4072 status
= check_stateid_generation(stateid
, &s
->sc_stateid
, 1);
4075 switch (s
->sc_type
) {
4076 case NFS4_DELEG_STID
:
4078 case NFS4_REVOKED_DELEG_STID
:
4079 return nfserr_deleg_revoked
;
4080 case NFS4_OPEN_STID
:
4081 case NFS4_LOCK_STID
:
4082 ols
= openlockstateid(s
);
4083 if (ols
->st_stateowner
->so_is_open_owner
4084 && !(openowner(ols
->st_stateowner
)->oo_flags
4085 & NFS4_OO_CONFIRMED
))
4086 return nfserr_bad_stateid
;
4089 printk("unknown stateid type %x\n", s
->sc_type
);
4091 case NFS4_CLOSED_STID
:
4092 case NFS4_CLOSED_DELEG_STID
:
4093 return nfserr_bad_stateid
;
4098 nfsd4_lookup_stateid(struct nfsd4_compound_state
*cstate
,
4099 stateid_t
*stateid
, unsigned char typemask
,
4100 struct nfs4_stid
**s
, struct nfsd_net
*nn
)
4104 if (ZERO_STATEID(stateid
) || ONE_STATEID(stateid
))
4105 return nfserr_bad_stateid
;
4106 status
= lookup_clientid(&stateid
->si_opaque
.so_clid
, cstate
, nn
);
4107 if (status
== nfserr_stale_clientid
) {
4108 if (cstate
->session
)
4109 return nfserr_bad_stateid
;
4110 return nfserr_stale_stateid
;
4114 *s
= find_stateid_by_type(cstate
->clp
, stateid
, typemask
);
4116 return nfserr_bad_stateid
;
4121 * Checks for stateid operations
4124 nfs4_preprocess_stateid_op(struct net
*net
, struct nfsd4_compound_state
*cstate
,
4125 stateid_t
*stateid
, int flags
, struct file
**filpp
)
4127 struct nfs4_stid
*s
;
4128 struct nfs4_ol_stateid
*stp
= NULL
;
4129 struct nfs4_delegation
*dp
= NULL
;
4130 struct svc_fh
*current_fh
= &cstate
->current_fh
;
4131 struct inode
*ino
= current_fh
->fh_dentry
->d_inode
;
4132 struct nfsd_net
*nn
= net_generic(net
, nfsd_net_id
);
4133 struct file
*file
= NULL
;
4139 if (grace_disallows_io(net
, ino
))
4140 return nfserr_grace
;
4142 if (ZERO_STATEID(stateid
) || ONE_STATEID(stateid
))
4143 return check_special_stateids(net
, current_fh
, stateid
, flags
);
4147 status
= nfsd4_lookup_stateid(cstate
, stateid
,
4148 NFS4_DELEG_STID
|NFS4_OPEN_STID
|NFS4_LOCK_STID
,
4152 status
= check_stateid_generation(stateid
, &s
->sc_stateid
, nfsd4_has_session(cstate
));
4155 switch (s
->sc_type
) {
4156 case NFS4_DELEG_STID
:
4157 dp
= delegstateid(s
);
4158 status
= nfs4_check_delegmode(dp
, flags
);
4162 file
= dp
->dl_file
->fi_deleg_file
;
4165 status
= nfserr_serverfault
;
4171 case NFS4_OPEN_STID
:
4172 case NFS4_LOCK_STID
:
4173 stp
= openlockstateid(s
);
4174 status
= nfs4_check_fh(current_fh
, stp
);
4177 if (stp
->st_stateowner
->so_is_open_owner
4178 && !(openowner(stp
->st_stateowner
)->oo_flags
& NFS4_OO_CONFIRMED
))
4180 status
= nfs4_check_openmode(stp
, flags
);
4184 if (flags
& RD_STATE
)
4185 file
= find_readable_file(stp
->st_file
);
4187 file
= find_writeable_file(stp
->st_file
);
4191 status
= nfserr_bad_stateid
;
4198 nfs4_unlock_state();
4203 nfsd4_free_lock_stateid(struct nfs4_ol_stateid
*stp
)
4205 struct nfs4_lockowner
*lo
= lockowner(stp
->st_stateowner
);
4207 if (check_for_locks(stp
->st_file
, lo
))
4208 return nfserr_locks_held
;
4209 release_lockowner_if_empty(lo
);
4214 * Test if the stateid is valid
4217 nfsd4_test_stateid(struct svc_rqst
*rqstp
, struct nfsd4_compound_state
*cstate
,
4218 struct nfsd4_test_stateid
*test_stateid
)
4220 struct nfsd4_test_stateid_id
*stateid
;
4221 struct nfs4_client
*cl
= cstate
->session
->se_client
;
4224 list_for_each_entry(stateid
, &test_stateid
->ts_stateid_list
, ts_id_list
)
4225 stateid
->ts_id_status
=
4226 nfsd4_validate_stateid(cl
, &stateid
->ts_id_stateid
);
4227 nfs4_unlock_state();
4233 nfsd4_free_stateid(struct svc_rqst
*rqstp
, struct nfsd4_compound_state
*cstate
,
4234 struct nfsd4_free_stateid
*free_stateid
)
4236 stateid_t
*stateid
= &free_stateid
->fr_stateid
;
4237 struct nfs4_stid
*s
;
4238 struct nfs4_delegation
*dp
;
4239 struct nfs4_client
*cl
= cstate
->session
->se_client
;
4240 __be32 ret
= nfserr_bad_stateid
;
4243 s
= find_stateid(cl
, stateid
);
4246 switch (s
->sc_type
) {
4247 case NFS4_DELEG_STID
:
4248 ret
= nfserr_locks_held
;
4250 case NFS4_OPEN_STID
:
4251 case NFS4_LOCK_STID
:
4252 ret
= check_stateid_generation(stateid
, &s
->sc_stateid
, 1);
4255 if (s
->sc_type
== NFS4_LOCK_STID
)
4256 ret
= nfsd4_free_lock_stateid(openlockstateid(s
));
4258 ret
= nfserr_locks_held
;
4260 case NFS4_REVOKED_DELEG_STID
:
4261 dp
= delegstateid(s
);
4262 destroy_revoked_delegation(dp
);
4266 ret
= nfserr_bad_stateid
;
4269 nfs4_unlock_state();
4276 return (type
== NFS4_READW_LT
|| type
== NFS4_READ_LT
) ?
4277 RD_STATE
: WR_STATE
;
4280 static __be32
nfs4_seqid_op_checks(struct nfsd4_compound_state
*cstate
, stateid_t
*stateid
, u32 seqid
, struct nfs4_ol_stateid
*stp
)
4282 struct svc_fh
*current_fh
= &cstate
->current_fh
;
4283 struct nfs4_stateowner
*sop
= stp
->st_stateowner
;
4286 status
= nfsd4_check_seqid(cstate
, sop
, seqid
);
4289 if (stp
->st_stid
.sc_type
== NFS4_CLOSED_STID
4290 || stp
->st_stid
.sc_type
== NFS4_REVOKED_DELEG_STID
)
4292 * "Closed" stateid's exist *only* to return
4293 * nfserr_replay_me from the previous step, and
4294 * revoked delegations are kept only for free_stateid.
4296 return nfserr_bad_stateid
;
4297 status
= check_stateid_generation(stateid
, &stp
->st_stid
.sc_stateid
, nfsd4_has_session(cstate
));
4300 return nfs4_check_fh(current_fh
, stp
);
4304 * Checks for sequence id mutating operations.
4307 nfs4_preprocess_seqid_op(struct nfsd4_compound_state
*cstate
, u32 seqid
,
4308 stateid_t
*stateid
, char typemask
,
4309 struct nfs4_ol_stateid
**stpp
,
4310 struct nfsd_net
*nn
)
4313 struct nfs4_stid
*s
;
4314 struct nfs4_ol_stateid
*stp
= NULL
;
4316 dprintk("NFSD: %s: seqid=%d stateid = " STATEID_FMT
"\n", __func__
,
4317 seqid
, STATEID_VAL(stateid
));
4320 status
= nfsd4_lookup_stateid(cstate
, stateid
, typemask
, &s
, nn
);
4323 stp
= openlockstateid(s
);
4324 if (!nfsd4_has_session(cstate
))
4325 cstate
->replay_owner
= stp
->st_stateowner
;
4327 status
= nfs4_seqid_op_checks(cstate
, stateid
, seqid
, stp
);
4333 static __be32
nfs4_preprocess_confirmed_seqid_op(struct nfsd4_compound_state
*cstate
, u32 seqid
,
4334 stateid_t
*stateid
, struct nfs4_ol_stateid
**stpp
, struct nfsd_net
*nn
)
4337 struct nfs4_openowner
*oo
;
4339 status
= nfs4_preprocess_seqid_op(cstate
, seqid
, stateid
,
4340 NFS4_OPEN_STID
, stpp
, nn
);
4343 oo
= openowner((*stpp
)->st_stateowner
);
4344 if (!(oo
->oo_flags
& NFS4_OO_CONFIRMED
))
4345 return nfserr_bad_stateid
;
4350 nfsd4_open_confirm(struct svc_rqst
*rqstp
, struct nfsd4_compound_state
*cstate
,
4351 struct nfsd4_open_confirm
*oc
)
4354 struct nfs4_openowner
*oo
;
4355 struct nfs4_ol_stateid
*stp
;
4356 struct nfsd_net
*nn
= net_generic(SVC_NET(rqstp
), nfsd_net_id
);
4358 dprintk("NFSD: nfsd4_open_confirm on file %pd\n",
4359 cstate
->current_fh
.fh_dentry
);
4361 status
= fh_verify(rqstp
, &cstate
->current_fh
, S_IFREG
, 0);
4367 status
= nfs4_preprocess_seqid_op(cstate
,
4368 oc
->oc_seqid
, &oc
->oc_req_stateid
,
4369 NFS4_OPEN_STID
, &stp
, nn
);
4372 oo
= openowner(stp
->st_stateowner
);
4373 status
= nfserr_bad_stateid
;
4374 if (oo
->oo_flags
& NFS4_OO_CONFIRMED
)
4376 oo
->oo_flags
|= NFS4_OO_CONFIRMED
;
4377 update_stateid(&stp
->st_stid
.sc_stateid
);
4378 memcpy(&oc
->oc_resp_stateid
, &stp
->st_stid
.sc_stateid
, sizeof(stateid_t
));
4379 dprintk("NFSD: %s: success, seqid=%d stateid=" STATEID_FMT
"\n",
4380 __func__
, oc
->oc_seqid
, STATEID_VAL(&stp
->st_stid
.sc_stateid
));
4382 nfsd4_client_record_create(oo
->oo_owner
.so_client
);
4385 nfsd4_bump_seqid(cstate
, status
);
4386 if (!cstate
->replay_owner
)
4387 nfs4_unlock_state();
4391 static inline void nfs4_stateid_downgrade_bit(struct nfs4_ol_stateid
*stp
, u32 access
)
4393 if (!test_access(access
, stp
))
4395 nfs4_file_put_access(stp
->st_file
, access
);
4396 clear_access(access
, stp
);
4399 static inline void nfs4_stateid_downgrade(struct nfs4_ol_stateid
*stp
, u32 to_access
)
4401 switch (to_access
) {
4402 case NFS4_SHARE_ACCESS_READ
:
4403 nfs4_stateid_downgrade_bit(stp
, NFS4_SHARE_ACCESS_WRITE
);
4404 nfs4_stateid_downgrade_bit(stp
, NFS4_SHARE_ACCESS_BOTH
);
4406 case NFS4_SHARE_ACCESS_WRITE
:
4407 nfs4_stateid_downgrade_bit(stp
, NFS4_SHARE_ACCESS_READ
);
4408 nfs4_stateid_downgrade_bit(stp
, NFS4_SHARE_ACCESS_BOTH
);
4410 case NFS4_SHARE_ACCESS_BOTH
:
4418 nfsd4_open_downgrade(struct svc_rqst
*rqstp
,
4419 struct nfsd4_compound_state
*cstate
,
4420 struct nfsd4_open_downgrade
*od
)
4423 struct nfs4_ol_stateid
*stp
;
4424 struct nfsd_net
*nn
= net_generic(SVC_NET(rqstp
), nfsd_net_id
);
4426 dprintk("NFSD: nfsd4_open_downgrade on file %pd\n",
4427 cstate
->current_fh
.fh_dentry
);
4429 /* We don't yet support WANT bits: */
4430 if (od
->od_deleg_want
)
4431 dprintk("NFSD: %s: od_deleg_want=0x%x ignored\n", __func__
,
4435 status
= nfs4_preprocess_confirmed_seqid_op(cstate
, od
->od_seqid
,
4436 &od
->od_stateid
, &stp
, nn
);
4439 status
= nfserr_inval
;
4440 if (!test_access(od
->od_share_access
, stp
)) {
4441 dprintk("NFSD: access not a subset of current bitmap: 0x%hhx, input access=%08x\n",
4442 stp
->st_access_bmap
, od
->od_share_access
);
4445 if (!test_deny(od
->od_share_deny
, stp
)) {
4446 dprintk("NFSD: deny not a subset of current bitmap: 0x%hhx, input deny=%08x\n",
4447 stp
->st_deny_bmap
, od
->od_share_deny
);
4450 nfs4_stateid_downgrade(stp
, od
->od_share_access
);
4452 reset_union_bmap_deny(od
->od_share_deny
, stp
);
4454 update_stateid(&stp
->st_stid
.sc_stateid
);
4455 memcpy(&od
->od_stateid
, &stp
->st_stid
.sc_stateid
, sizeof(stateid_t
));
4458 nfsd4_bump_seqid(cstate
, status
);
4459 if (!cstate
->replay_owner
)
4460 nfs4_unlock_state();
4464 static void nfsd4_close_open_stateid(struct nfs4_ol_stateid
*s
)
4466 struct nfs4_client
*clp
= s
->st_stid
.sc_client
;
4467 struct nfs4_openowner
*oo
= openowner(s
->st_stateowner
);
4469 s
->st_stid
.sc_type
= NFS4_CLOSED_STID
;
4470 unhash_open_stateid(s
);
4472 if (clp
->cl_minorversion
) {
4473 free_generic_stateid(s
);
4474 if (list_empty(&oo
->oo_owner
.so_stateids
))
4475 release_openowner(oo
);
4477 oo
->oo_last_closed_stid
= s
;
4479 * In the 4.0 case we need to keep the owners around a
4480 * little while to handle CLOSE replay.
4482 if (list_empty(&oo
->oo_owner
.so_stateids
))
4483 move_to_close_lru(oo
, clp
->net
);
4488 * nfs4_unlock_state() called after encode
4491 nfsd4_close(struct svc_rqst
*rqstp
, struct nfsd4_compound_state
*cstate
,
4492 struct nfsd4_close
*close
)
4495 struct nfs4_ol_stateid
*stp
;
4496 struct net
*net
= SVC_NET(rqstp
);
4497 struct nfsd_net
*nn
= net_generic(net
, nfsd_net_id
);
4499 dprintk("NFSD: nfsd4_close on file %pd\n",
4500 cstate
->current_fh
.fh_dentry
);
4503 status
= nfs4_preprocess_seqid_op(cstate
, close
->cl_seqid
,
4505 NFS4_OPEN_STID
|NFS4_CLOSED_STID
,
4507 nfsd4_bump_seqid(cstate
, status
);
4510 update_stateid(&stp
->st_stid
.sc_stateid
);
4511 memcpy(&close
->cl_stateid
, &stp
->st_stid
.sc_stateid
, sizeof(stateid_t
));
4513 nfsd4_close_open_stateid(stp
);
4515 if (!cstate
->replay_owner
)
4516 nfs4_unlock_state();
4521 nfsd4_delegreturn(struct svc_rqst
*rqstp
, struct nfsd4_compound_state
*cstate
,
4522 struct nfsd4_delegreturn
*dr
)
4524 struct nfs4_delegation
*dp
;
4525 stateid_t
*stateid
= &dr
->dr_stateid
;
4526 struct nfs4_stid
*s
;
4528 struct nfsd_net
*nn
= net_generic(SVC_NET(rqstp
), nfsd_net_id
);
4530 if ((status
= fh_verify(rqstp
, &cstate
->current_fh
, S_IFREG
, 0)))
4534 status
= nfsd4_lookup_stateid(cstate
, stateid
, NFS4_DELEG_STID
, &s
, nn
);
4537 dp
= delegstateid(s
);
4538 status
= check_stateid_generation(stateid
, &dp
->dl_stid
.sc_stateid
, nfsd4_has_session(cstate
));
4542 destroy_delegation(dp
);
4544 nfs4_unlock_state();
4550 #define LOFF_OVERFLOW(start, len) ((u64)(len) > ~(u64)(start))
4553 end_offset(u64 start
, u64 len
)
4558 return end
>= start
? end
: NFS4_MAX_UINT64
;
4561 /* last octet in a range */
4563 last_byte_offset(u64 start
, u64 len
)
4569 return end
> start
? end
- 1: NFS4_MAX_UINT64
;
4573 * TODO: Linux file offsets are _signed_ 64-bit quantities, which means that
4574 * we can't properly handle lock requests that go beyond the (2^63 - 1)-th
4575 * byte, because of sign extension problems. Since NFSv4 calls for 64-bit
4576 * locking, this prevents us from being completely protocol-compliant. The
4577 * real solution to this problem is to start using unsigned file offsets in
4578 * the VFS, but this is a very deep change!
4581 nfs4_transform_lock_offset(struct file_lock
*lock
)
4583 if (lock
->fl_start
< 0)
4584 lock
->fl_start
= OFFSET_MAX
;
4585 if (lock
->fl_end
< 0)
4586 lock
->fl_end
= OFFSET_MAX
;
4589 /* Hack!: For now, we're defining this just so we can use a pointer to it
4590 * as a unique cookie to identify our (NFSv4's) posix locks. */
4591 static const struct lock_manager_operations nfsd_posix_mng_ops
= {
4595 nfs4_set_lock_denied(struct file_lock
*fl
, struct nfsd4_lock_denied
*deny
)
4597 struct nfs4_lockowner
*lo
;
4599 if (fl
->fl_lmops
== &nfsd_posix_mng_ops
) {
4600 lo
= (struct nfs4_lockowner
*) fl
->fl_owner
;
4601 deny
->ld_owner
.data
= kmemdup(lo
->lo_owner
.so_owner
.data
,
4602 lo
->lo_owner
.so_owner
.len
, GFP_KERNEL
);
4603 if (!deny
->ld_owner
.data
)
4604 /* We just don't care that much */
4606 deny
->ld_owner
.len
= lo
->lo_owner
.so_owner
.len
;
4607 deny
->ld_clientid
= lo
->lo_owner
.so_client
->cl_clientid
;
4610 deny
->ld_owner
.len
= 0;
4611 deny
->ld_owner
.data
= NULL
;
4612 deny
->ld_clientid
.cl_boot
= 0;
4613 deny
->ld_clientid
.cl_id
= 0;
4615 deny
->ld_start
= fl
->fl_start
;
4616 deny
->ld_length
= NFS4_MAX_UINT64
;
4617 if (fl
->fl_end
!= NFS4_MAX_UINT64
)
4618 deny
->ld_length
= fl
->fl_end
- fl
->fl_start
+ 1;
4619 deny
->ld_type
= NFS4_READ_LT
;
4620 if (fl
->fl_type
!= F_RDLCK
)
4621 deny
->ld_type
= NFS4_WRITE_LT
;
4624 static struct nfs4_lockowner
*
4625 find_lockowner_str(clientid_t
*clid
, struct xdr_netobj
*owner
,
4626 struct nfsd_net
*nn
)
4628 unsigned int strhashval
= ownerstr_hashval(clid
->cl_id
, owner
);
4629 struct nfs4_stateowner
*so
;
4631 list_for_each_entry(so
, &nn
->ownerstr_hashtbl
[strhashval
], so_strhash
) {
4632 if (so
->so_is_open_owner
)
4634 if (!same_owner_str(so
, owner
, clid
))
4636 return lockowner(so
);
4642 * Alloc a lock owner structure.
4643 * Called in nfsd4_lock - therefore, OPEN and OPEN_CONFIRM (if needed) has
4646 * strhashval = ownerstr_hashval
4648 static struct nfs4_lockowner
*
4649 alloc_init_lock_stateowner(unsigned int strhashval
, struct nfs4_client
*clp
, struct nfs4_ol_stateid
*open_stp
, struct nfsd4_lock
*lock
) {
4650 struct nfs4_lockowner
*lo
;
4651 struct nfsd_net
*nn
= net_generic(clp
->net
, nfsd_net_id
);
4653 lo
= alloc_stateowner(lockowner_slab
, &lock
->lk_new_owner
, clp
);
4656 INIT_LIST_HEAD(&lo
->lo_owner
.so_stateids
);
4657 lo
->lo_owner
.so_is_open_owner
= 0;
4658 /* It is the openowner seqid that will be incremented in encode in the
4659 * case of new lockowners; so increment the lock seqid manually: */
4660 lo
->lo_owner
.so_seqid
= lock
->lk_new_lock_seqid
+ 1;
4661 list_add(&lo
->lo_owner
.so_strhash
, &nn
->ownerstr_hashtbl
[strhashval
]);
4665 static struct nfs4_ol_stateid
*
4666 alloc_init_lock_stateid(struct nfs4_lockowner
*lo
, struct nfs4_file
*fp
, struct nfs4_ol_stateid
*open_stp
)
4668 struct nfs4_ol_stateid
*stp
;
4669 struct nfs4_client
*clp
= lo
->lo_owner
.so_client
;
4671 stp
= nfs4_alloc_stateid(clp
);
4674 stp
->st_stid
.sc_type
= NFS4_LOCK_STID
;
4675 list_add(&stp
->st_perstateowner
, &lo
->lo_owner
.so_stateids
);
4676 stp
->st_stateowner
= &lo
->lo_owner
;
4679 stp
->st_access_bmap
= 0;
4680 stp
->st_deny_bmap
= open_stp
->st_deny_bmap
;
4681 stp
->st_openstp
= open_stp
;
4682 list_add(&stp
->st_locks
, &open_stp
->st_locks
);
4683 spin_lock(&fp
->fi_lock
);
4684 list_add(&stp
->st_perfile
, &fp
->fi_stateids
);
4685 spin_unlock(&fp
->fi_lock
);
4689 static struct nfs4_ol_stateid
*
4690 find_lock_stateid(struct nfs4_lockowner
*lo
, struct nfs4_file
*fp
)
4692 struct nfs4_ol_stateid
*lst
;
4694 list_for_each_entry(lst
, &lo
->lo_owner
.so_stateids
, st_perstateowner
) {
4695 if (lst
->st_file
== fp
)
4703 check_lock_length(u64 offset
, u64 length
)
4705 return ((length
== 0) || ((length
!= NFS4_MAX_UINT64
) &&
4706 LOFF_OVERFLOW(offset
, length
)));
4709 static void get_lock_access(struct nfs4_ol_stateid
*lock_stp
, u32 access
)
4711 struct nfs4_file
*fp
= lock_stp
->st_file
;
4713 lockdep_assert_held(&fp
->fi_lock
);
4715 if (test_access(access
, lock_stp
))
4717 __nfs4_file_get_access(fp
, access
);
4718 set_access(access
, lock_stp
);
4721 static __be32
lookup_or_create_lock_state(struct nfsd4_compound_state
*cstate
, struct nfs4_ol_stateid
*ost
, struct nfsd4_lock
*lock
, struct nfs4_ol_stateid
**lst
, bool *new)
4723 struct nfs4_file
*fi
= ost
->st_file
;
4724 struct nfs4_openowner
*oo
= openowner(ost
->st_stateowner
);
4725 struct nfs4_client
*cl
= oo
->oo_owner
.so_client
;
4726 struct nfs4_lockowner
*lo
;
4727 unsigned int strhashval
;
4728 struct nfsd_net
*nn
= net_generic(cl
->net
, nfsd_net_id
);
4730 lo
= find_lockowner_str(&cl
->cl_clientid
, &lock
->v
.new.owner
, nn
);
4732 strhashval
= ownerstr_hashval(cl
->cl_clientid
.cl_id
,
4733 &lock
->v
.new.owner
);
4734 lo
= alloc_init_lock_stateowner(strhashval
, cl
, ost
, lock
);
4736 return nfserr_jukebox
;
4738 /* with an existing lockowner, seqids must be the same */
4739 if (!cstate
->minorversion
&&
4740 lock
->lk_new_lock_seqid
!= lo
->lo_owner
.so_seqid
)
4741 return nfserr_bad_seqid
;
4744 *lst
= find_lock_stateid(lo
, fi
);
4746 *lst
= alloc_init_lock_stateid(lo
, fi
, ost
);
4748 release_lockowner_if_empty(lo
);
4749 return nfserr_jukebox
;
4760 nfsd4_lock(struct svc_rqst
*rqstp
, struct nfsd4_compound_state
*cstate
,
4761 struct nfsd4_lock
*lock
)
4763 struct nfs4_openowner
*open_sop
= NULL
;
4764 struct nfs4_lockowner
*lock_sop
= NULL
;
4765 struct nfs4_ol_stateid
*lock_stp
;
4766 struct nfs4_file
*fp
;
4767 struct file
*filp
= NULL
;
4768 struct file_lock
*file_lock
= NULL
;
4769 struct file_lock
*conflock
= NULL
;
4771 bool new_state
= false;
4774 struct net
*net
= SVC_NET(rqstp
);
4775 struct nfsd_net
*nn
= net_generic(net
, nfsd_net_id
);
4777 dprintk("NFSD: nfsd4_lock: start=%Ld length=%Ld\n",
4778 (long long) lock
->lk_offset
,
4779 (long long) lock
->lk_length
);
4781 if (check_lock_length(lock
->lk_offset
, lock
->lk_length
))
4782 return nfserr_inval
;
4784 if ((status
= fh_verify(rqstp
, &cstate
->current_fh
,
4785 S_IFREG
, NFSD_MAY_LOCK
))) {
4786 dprintk("NFSD: nfsd4_lock: permission denied!\n");
4792 if (lock
->lk_is_new
) {
4793 struct nfs4_ol_stateid
*open_stp
= NULL
;
4795 if (nfsd4_has_session(cstate
))
4796 /* See rfc 5661 18.10.3: given clientid is ignored: */
4797 memcpy(&lock
->v
.new.clientid
,
4798 &cstate
->session
->se_client
->cl_clientid
,
4799 sizeof(clientid_t
));
4801 status
= nfserr_stale_clientid
;
4802 if (STALE_CLIENTID(&lock
->lk_new_clientid
, nn
))
4805 /* validate and update open stateid and open seqid */
4806 status
= nfs4_preprocess_confirmed_seqid_op(cstate
,
4807 lock
->lk_new_open_seqid
,
4808 &lock
->lk_new_open_stateid
,
4812 open_sop
= openowner(open_stp
->st_stateowner
);
4813 status
= nfserr_bad_stateid
;
4814 if (!same_clid(&open_sop
->oo_owner
.so_client
->cl_clientid
,
4815 &lock
->v
.new.clientid
))
4817 status
= lookup_or_create_lock_state(cstate
, open_stp
, lock
,
4818 &lock_stp
, &new_state
);
4820 status
= nfs4_preprocess_seqid_op(cstate
,
4821 lock
->lk_old_lock_seqid
,
4822 &lock
->lk_old_lock_stateid
,
4823 NFS4_LOCK_STID
, &lock_stp
, nn
);
4826 lock_sop
= lockowner(lock_stp
->st_stateowner
);
4828 lkflg
= setlkflg(lock
->lk_type
);
4829 status
= nfs4_check_openmode(lock_stp
, lkflg
);
4833 status
= nfserr_grace
;
4834 if (locks_in_grace(net
) && !lock
->lk_reclaim
)
4836 status
= nfserr_no_grace
;
4837 if (!locks_in_grace(net
) && lock
->lk_reclaim
)
4840 file_lock
= locks_alloc_lock();
4842 dprintk("NFSD: %s: unable to allocate lock!\n", __func__
);
4843 status
= nfserr_jukebox
;
4847 fp
= lock_stp
->st_file
;
4848 locks_init_lock(file_lock
);
4849 switch (lock
->lk_type
) {
4852 spin_lock(&fp
->fi_lock
);
4853 filp
= find_readable_file_locked(fp
);
4855 get_lock_access(lock_stp
, NFS4_SHARE_ACCESS_READ
);
4856 spin_unlock(&fp
->fi_lock
);
4857 file_lock
->fl_type
= F_RDLCK
;
4860 case NFS4_WRITEW_LT
:
4861 spin_lock(&fp
->fi_lock
);
4862 filp
= find_writeable_file_locked(fp
);
4864 get_lock_access(lock_stp
, NFS4_SHARE_ACCESS_WRITE
);
4865 spin_unlock(&fp
->fi_lock
);
4866 file_lock
->fl_type
= F_WRLCK
;
4869 status
= nfserr_inval
;
4873 status
= nfserr_openmode
;
4876 file_lock
->fl_owner
= (fl_owner_t
)lock_sop
;
4877 file_lock
->fl_pid
= current
->tgid
;
4878 file_lock
->fl_file
= filp
;
4879 file_lock
->fl_flags
= FL_POSIX
;
4880 file_lock
->fl_lmops
= &nfsd_posix_mng_ops
;
4881 file_lock
->fl_start
= lock
->lk_offset
;
4882 file_lock
->fl_end
= last_byte_offset(lock
->lk_offset
, lock
->lk_length
);
4883 nfs4_transform_lock_offset(file_lock
);
4885 conflock
= locks_alloc_lock();
4887 dprintk("NFSD: %s: unable to allocate lock!\n", __func__
);
4888 status
= nfserr_jukebox
;
4892 err
= vfs_lock_file(filp
, F_SETLK
, file_lock
, conflock
);
4894 case 0: /* success! */
4895 update_stateid(&lock_stp
->st_stid
.sc_stateid
);
4896 memcpy(&lock
->lk_resp_stateid
, &lock_stp
->st_stid
.sc_stateid
,
4900 case (EAGAIN
): /* conflock holds conflicting lock */
4901 status
= nfserr_denied
;
4902 dprintk("NFSD: nfsd4_lock: conflicting lock found!\n");
4903 nfs4_set_lock_denied(conflock
, &lock
->lk_denied
);
4906 status
= nfserr_deadlock
;
4909 dprintk("NFSD: nfsd4_lock: vfs_lock_file() failed! status %d\n",err
);
4910 status
= nfserrno(err
);
4916 if (status
&& new_state
)
4917 release_lock_stateid(lock_stp
);
4918 nfsd4_bump_seqid(cstate
, status
);
4919 if (!cstate
->replay_owner
)
4920 nfs4_unlock_state();
4922 locks_free_lock(file_lock
);
4924 locks_free_lock(conflock
);
4929 * The NFSv4 spec allows a client to do a LOCKT without holding an OPEN,
4930 * so we do a temporary open here just to get an open file to pass to
4931 * vfs_test_lock. (Arguably perhaps test_lock should be done with an
4934 static __be32
nfsd_test_lock(struct svc_rqst
*rqstp
, struct svc_fh
*fhp
, struct file_lock
*lock
)
4937 __be32 err
= nfsd_open(rqstp
, fhp
, S_IFREG
, NFSD_MAY_READ
, &file
);
4939 err
= nfserrno(vfs_test_lock(file
, lock
));
4949 nfsd4_lockt(struct svc_rqst
*rqstp
, struct nfsd4_compound_state
*cstate
,
4950 struct nfsd4_lockt
*lockt
)
4952 struct file_lock
*file_lock
= NULL
;
4953 struct nfs4_lockowner
*lo
;
4955 struct nfsd_net
*nn
= net_generic(SVC_NET(rqstp
), nfsd_net_id
);
4957 if (locks_in_grace(SVC_NET(rqstp
)))
4958 return nfserr_grace
;
4960 if (check_lock_length(lockt
->lt_offset
, lockt
->lt_length
))
4961 return nfserr_inval
;
4965 if (!nfsd4_has_session(cstate
)) {
4966 status
= lookup_clientid(&lockt
->lt_clientid
, cstate
, nn
);
4971 if ((status
= fh_verify(rqstp
, &cstate
->current_fh
, S_IFREG
, 0)))
4974 file_lock
= locks_alloc_lock();
4976 dprintk("NFSD: %s: unable to allocate lock!\n", __func__
);
4977 status
= nfserr_jukebox
;
4980 locks_init_lock(file_lock
);
4981 switch (lockt
->lt_type
) {
4984 file_lock
->fl_type
= F_RDLCK
;
4987 case NFS4_WRITEW_LT
:
4988 file_lock
->fl_type
= F_WRLCK
;
4991 dprintk("NFSD: nfs4_lockt: bad lock type!\n");
4992 status
= nfserr_inval
;
4996 lo
= find_lockowner_str(&lockt
->lt_clientid
, &lockt
->lt_owner
, nn
);
4998 file_lock
->fl_owner
= (fl_owner_t
)lo
;
4999 file_lock
->fl_pid
= current
->tgid
;
5000 file_lock
->fl_flags
= FL_POSIX
;
5002 file_lock
->fl_start
= lockt
->lt_offset
;
5003 file_lock
->fl_end
= last_byte_offset(lockt
->lt_offset
, lockt
->lt_length
);
5005 nfs4_transform_lock_offset(file_lock
);
5007 status
= nfsd_test_lock(rqstp
, &cstate
->current_fh
, file_lock
);
5011 if (file_lock
->fl_type
!= F_UNLCK
) {
5012 status
= nfserr_denied
;
5013 nfs4_set_lock_denied(file_lock
, &lockt
->lt_denied
);
5016 nfs4_unlock_state();
5018 locks_free_lock(file_lock
);
5023 nfsd4_locku(struct svc_rqst
*rqstp
, struct nfsd4_compound_state
*cstate
,
5024 struct nfsd4_locku
*locku
)
5026 struct nfs4_ol_stateid
*stp
;
5027 struct file
*filp
= NULL
;
5028 struct file_lock
*file_lock
= NULL
;
5031 struct nfsd_net
*nn
= net_generic(SVC_NET(rqstp
), nfsd_net_id
);
5033 dprintk("NFSD: nfsd4_locku: start=%Ld length=%Ld\n",
5034 (long long) locku
->lu_offset
,
5035 (long long) locku
->lu_length
);
5037 if (check_lock_length(locku
->lu_offset
, locku
->lu_length
))
5038 return nfserr_inval
;
5042 status
= nfs4_preprocess_seqid_op(cstate
, locku
->lu_seqid
,
5043 &locku
->lu_stateid
, NFS4_LOCK_STID
,
5047 filp
= find_any_file(stp
->st_file
);
5049 status
= nfserr_lock_range
;
5052 file_lock
= locks_alloc_lock();
5054 dprintk("NFSD: %s: unable to allocate lock!\n", __func__
);
5055 status
= nfserr_jukebox
;
5058 locks_init_lock(file_lock
);
5059 file_lock
->fl_type
= F_UNLCK
;
5060 file_lock
->fl_owner
= (fl_owner_t
)lockowner(stp
->st_stateowner
);
5061 file_lock
->fl_pid
= current
->tgid
;
5062 file_lock
->fl_file
= filp
;
5063 file_lock
->fl_flags
= FL_POSIX
;
5064 file_lock
->fl_lmops
= &nfsd_posix_mng_ops
;
5065 file_lock
->fl_start
= locku
->lu_offset
;
5067 file_lock
->fl_end
= last_byte_offset(locku
->lu_offset
,
5069 nfs4_transform_lock_offset(file_lock
);
5071 err
= vfs_lock_file(filp
, F_SETLK
, file_lock
, NULL
);
5073 dprintk("NFSD: nfs4_locku: vfs_lock_file failed!\n");
5076 update_stateid(&stp
->st_stid
.sc_stateid
);
5077 memcpy(&locku
->lu_stateid
, &stp
->st_stid
.sc_stateid
, sizeof(stateid_t
));
5081 nfsd4_bump_seqid(cstate
, status
);
5082 if (!cstate
->replay_owner
)
5083 nfs4_unlock_state();
5085 locks_free_lock(file_lock
);
5089 status
= nfserrno(err
);
5095 * 1: locks held by lockowner
5096 * 0: no locks held by lockowner
5099 check_for_locks(struct nfs4_file
*filp
, struct nfs4_lockowner
*lowner
)
5101 struct file_lock
**flpp
;
5102 struct inode
*inode
= filp
->fi_inode
;
5105 spin_lock(&inode
->i_lock
);
5106 for (flpp
= &inode
->i_flock
; *flpp
!= NULL
; flpp
= &(*flpp
)->fl_next
) {
5107 if ((*flpp
)->fl_owner
== (fl_owner_t
)lowner
) {
5113 spin_unlock(&inode
->i_lock
);
5118 nfsd4_release_lockowner(struct svc_rqst
*rqstp
,
5119 struct nfsd4_compound_state
*cstate
,
5120 struct nfsd4_release_lockowner
*rlockowner
)
5122 clientid_t
*clid
= &rlockowner
->rl_clientid
;
5123 struct nfs4_stateowner
*sop
= NULL
, *tmp
;
5124 struct nfs4_lockowner
*lo
;
5125 struct nfs4_ol_stateid
*stp
;
5126 struct xdr_netobj
*owner
= &rlockowner
->rl_owner
;
5127 unsigned int hashval
= ownerstr_hashval(clid
->cl_id
, owner
);
5129 struct nfsd_net
*nn
= net_generic(SVC_NET(rqstp
), nfsd_net_id
);
5131 dprintk("nfsd4_release_lockowner clientid: (%08x/%08x):\n",
5132 clid
->cl_boot
, clid
->cl_id
);
5136 status
= lookup_clientid(clid
, cstate
, nn
);
5140 status
= nfserr_locks_held
;
5142 /* Find the matching lock stateowner */
5143 list_for_each_entry(tmp
, &nn
->ownerstr_hashtbl
[hashval
], so_strhash
) {
5144 if (tmp
->so_is_open_owner
)
5146 if (same_owner_str(tmp
, owner
, clid
)) {
5152 /* No matching owner found, maybe a replay? Just declare victory... */
5158 lo
= lockowner(sop
);
5159 /* see if there are still any locks associated with it */
5160 list_for_each_entry(stp
, &sop
->so_stateids
, st_perstateowner
) {
5161 if (check_for_locks(stp
->st_file
, lo
))
5166 release_lockowner(lo
);
5168 nfs4_unlock_state();
5172 static inline struct nfs4_client_reclaim
*
5175 return kmalloc(sizeof(struct nfs4_client_reclaim
), GFP_KERNEL
);
5179 nfs4_has_reclaimed_state(const char *name
, struct nfsd_net
*nn
)
5181 struct nfs4_client_reclaim
*crp
;
5183 crp
= nfsd4_find_reclaim_client(name
, nn
);
5184 return (crp
&& crp
->cr_clp
);
5188 * failure => all reset bets are off, nfserr_no_grace...
5190 struct nfs4_client_reclaim
*
5191 nfs4_client_to_reclaim(const char *name
, struct nfsd_net
*nn
)
5193 unsigned int strhashval
;
5194 struct nfs4_client_reclaim
*crp
;
5196 dprintk("NFSD nfs4_client_to_reclaim NAME: %.*s\n", HEXDIR_LEN
, name
);
5197 crp
= alloc_reclaim();
5199 strhashval
= clientstr_hashval(name
);
5200 INIT_LIST_HEAD(&crp
->cr_strhash
);
5201 list_add(&crp
->cr_strhash
, &nn
->reclaim_str_hashtbl
[strhashval
]);
5202 memcpy(crp
->cr_recdir
, name
, HEXDIR_LEN
);
5204 nn
->reclaim_str_hashtbl_size
++;
5210 nfs4_remove_reclaim_record(struct nfs4_client_reclaim
*crp
, struct nfsd_net
*nn
)
5212 list_del(&crp
->cr_strhash
);
5214 nn
->reclaim_str_hashtbl_size
--;
5218 nfs4_release_reclaim(struct nfsd_net
*nn
)
5220 struct nfs4_client_reclaim
*crp
= NULL
;
5223 for (i
= 0; i
< CLIENT_HASH_SIZE
; i
++) {
5224 while (!list_empty(&nn
->reclaim_str_hashtbl
[i
])) {
5225 crp
= list_entry(nn
->reclaim_str_hashtbl
[i
].next
,
5226 struct nfs4_client_reclaim
, cr_strhash
);
5227 nfs4_remove_reclaim_record(crp
, nn
);
5230 WARN_ON_ONCE(nn
->reclaim_str_hashtbl_size
);
5234 * called from OPEN, CLAIM_PREVIOUS with a new clientid. */
5235 struct nfs4_client_reclaim
*
5236 nfsd4_find_reclaim_client(const char *recdir
, struct nfsd_net
*nn
)
5238 unsigned int strhashval
;
5239 struct nfs4_client_reclaim
*crp
= NULL
;
5241 dprintk("NFSD: nfs4_find_reclaim_client for recdir %s\n", recdir
);
5243 strhashval
= clientstr_hashval(recdir
);
5244 list_for_each_entry(crp
, &nn
->reclaim_str_hashtbl
[strhashval
], cr_strhash
) {
5245 if (same_name(crp
->cr_recdir
, recdir
)) {
5253 * Called from OPEN. Look for clientid in reclaim list.
5256 nfs4_check_open_reclaim(clientid_t
*clid
,
5257 struct nfsd4_compound_state
*cstate
,
5258 struct nfsd_net
*nn
)
5262 /* find clientid in conf_id_hashtbl */
5263 status
= lookup_clientid(clid
, cstate
, nn
);
5265 return nfserr_reclaim_bad
;
5267 if (nfsd4_client_record_check(cstate
->clp
))
5268 return nfserr_reclaim_bad
;
5273 #ifdef CONFIG_NFSD_FAULT_INJECTION
5275 u64
nfsd_forget_client(struct nfs4_client
*clp
, u64 max
)
5277 if (mark_client_expired(clp
))
5283 u64
nfsd_print_client(struct nfs4_client
*clp
, u64 num
)
5285 char buf
[INET6_ADDRSTRLEN
];
5286 rpc_ntop((struct sockaddr
*)&clp
->cl_addr
, buf
, sizeof(buf
));
5287 printk(KERN_INFO
"NFS Client: %s\n", buf
);
5291 static void nfsd_print_count(struct nfs4_client
*clp
, unsigned int count
,
5294 char buf
[INET6_ADDRSTRLEN
];
5295 rpc_ntop((struct sockaddr
*)&clp
->cl_addr
, buf
, sizeof(buf
));
5296 printk(KERN_INFO
"NFS Client: %s has %u %s\n", buf
, count
, type
);
5299 static u64
nfsd_foreach_client_lock(struct nfs4_client
*clp
, u64 max
,
5300 void (*func
)(struct nfs4_ol_stateid
*))
5302 struct nfs4_openowner
*oop
;
5303 struct nfs4_ol_stateid
*stp
, *st_next
;
5304 struct nfs4_ol_stateid
*lst
, *lst_next
;
5307 list_for_each_entry(oop
, &clp
->cl_openowners
, oo_perclient
) {
5308 list_for_each_entry_safe(stp
, st_next
,
5309 &oop
->oo_owner
.so_stateids
, st_perstateowner
) {
5310 list_for_each_entry_safe(lst
, lst_next
,
5311 &stp
->st_locks
, st_locks
) {
5323 u64
nfsd_forget_client_locks(struct nfs4_client
*clp
, u64 max
)
5325 return nfsd_foreach_client_lock(clp
, max
, release_lock_stateid
);
5328 u64
nfsd_print_client_locks(struct nfs4_client
*clp
, u64 max
)
5330 u64 count
= nfsd_foreach_client_lock(clp
, max
, NULL
);
5331 nfsd_print_count(clp
, count
, "locked files");
5335 static u64
nfsd_foreach_client_open(struct nfs4_client
*clp
, u64 max
, void (*func
)(struct nfs4_openowner
*))
5337 struct nfs4_openowner
*oop
, *next
;
5340 list_for_each_entry_safe(oop
, next
, &clp
->cl_openowners
, oo_perclient
) {
5350 u64
nfsd_forget_client_openowners(struct nfs4_client
*clp
, u64 max
)
5352 return nfsd_foreach_client_open(clp
, max
, release_openowner
);
5355 u64
nfsd_print_client_openowners(struct nfs4_client
*clp
, u64 max
)
5357 u64 count
= nfsd_foreach_client_open(clp
, max
, NULL
);
5358 nfsd_print_count(clp
, count
, "open files");
5362 static u64
nfsd_find_all_delegations(struct nfs4_client
*clp
, u64 max
,
5363 struct list_head
*victims
)
5365 struct nfs4_delegation
*dp
, *next
;
5368 lockdep_assert_held(&state_lock
);
5369 list_for_each_entry_safe(dp
, next
, &clp
->cl_delegations
, dl_perclnt
) {
5372 * It's not safe to mess with delegations that have a
5373 * non-zero dl_time. They might have already been broken
5374 * and could be processed by the laundromat outside of
5375 * the state_lock. Just leave them be.
5377 if (dp
->dl_time
!= 0)
5381 * Increment dl_time to ensure that delegation breaks
5382 * don't monkey with it now that we are.
5385 list_move(&dp
->dl_recall_lru
, victims
);
5393 u64
nfsd_forget_client_delegations(struct nfs4_client
*clp
, u64 max
)
5395 struct nfs4_delegation
*dp
, *next
;
5399 spin_lock(&state_lock
);
5400 count
= nfsd_find_all_delegations(clp
, max
, &victims
);
5401 spin_unlock(&state_lock
);
5403 list_for_each_entry_safe(dp
, next
, &victims
, dl_recall_lru
)
5404 revoke_delegation(dp
);
5409 u64
nfsd_recall_client_delegations(struct nfs4_client
*clp
, u64 max
)
5411 struct nfs4_delegation
*dp
;
5415 spin_lock(&state_lock
);
5416 count
= nfsd_find_all_delegations(clp
, max
, &victims
);
5417 while (!list_empty(&victims
)) {
5418 dp
= list_first_entry(&victims
, struct nfs4_delegation
,
5420 list_del_init(&dp
->dl_recall_lru
);
5422 nfsd_break_one_deleg(dp
);
5424 spin_unlock(&state_lock
);
5429 u64
nfsd_print_client_delegations(struct nfs4_client
*clp
, u64 max
)
5433 spin_lock(&state_lock
);
5434 count
= nfsd_find_all_delegations(clp
, max
, NULL
);
5435 spin_unlock(&state_lock
);
5437 nfsd_print_count(clp
, count
, "delegations");
5441 u64
nfsd_for_n_state(u64 max
, u64 (*func
)(struct nfs4_client
*, u64
))
5443 struct nfs4_client
*clp
, *next
;
5445 struct nfsd_net
*nn
= net_generic(current
->nsproxy
->net_ns
, nfsd_net_id
);
5447 if (!nfsd_netns_ready(nn
))
5450 list_for_each_entry_safe(clp
, next
, &nn
->client_lru
, cl_lru
) {
5451 count
+= func(clp
, max
- count
);
5452 if ((max
!= 0) && (count
>= max
))
5459 struct nfs4_client
*nfsd_find_client(struct sockaddr_storage
*addr
, size_t addr_size
)
5461 struct nfs4_client
*clp
;
5462 struct nfsd_net
*nn
= net_generic(current
->nsproxy
->net_ns
, nfsd_net_id
);
5464 if (!nfsd_netns_ready(nn
))
5467 list_for_each_entry(clp
, &nn
->client_lru
, cl_lru
) {
5468 if (memcmp(&clp
->cl_addr
, addr
, addr_size
) == 0)
5474 #endif /* CONFIG_NFSD_FAULT_INJECTION */
5477 * Since the lifetime of a delegation isn't limited to that of an open, a
5478 * client may quite reasonably hang on to a delegation as long as it has
5479 * the inode cached. This becomes an obvious problem the first time a
5480 * client's inode cache approaches the size of the server's total memory.
5482 * For now we avoid this problem by imposing a hard limit on the number
5483 * of delegations, which varies according to the server's memory size.
5486 set_max_delegations(void)
5489 * Allow at most 4 delegations per megabyte of RAM. Quick
5490 * estimates suggest that in the worst case (where every delegation
5491 * is for a different inode), a delegation could take about 1.5K,
5492 * giving a worst case usage of about 6% of memory.
5494 max_delegations
= nr_free_buffer_pages() >> (20 - 2 - PAGE_SHIFT
);
5497 static int nfs4_state_create_net(struct net
*net
)
5499 struct nfsd_net
*nn
= net_generic(net
, nfsd_net_id
);
5502 nn
->conf_id_hashtbl
= kmalloc(sizeof(struct list_head
) *
5503 CLIENT_HASH_SIZE
, GFP_KERNEL
);
5504 if (!nn
->conf_id_hashtbl
)
5506 nn
->unconf_id_hashtbl
= kmalloc(sizeof(struct list_head
) *
5507 CLIENT_HASH_SIZE
, GFP_KERNEL
);
5508 if (!nn
->unconf_id_hashtbl
)
5510 nn
->ownerstr_hashtbl
= kmalloc(sizeof(struct list_head
) *
5511 OWNER_HASH_SIZE
, GFP_KERNEL
);
5512 if (!nn
->ownerstr_hashtbl
)
5514 nn
->sessionid_hashtbl
= kmalloc(sizeof(struct list_head
) *
5515 SESSION_HASH_SIZE
, GFP_KERNEL
);
5516 if (!nn
->sessionid_hashtbl
)
5519 for (i
= 0; i
< CLIENT_HASH_SIZE
; i
++) {
5520 INIT_LIST_HEAD(&nn
->conf_id_hashtbl
[i
]);
5521 INIT_LIST_HEAD(&nn
->unconf_id_hashtbl
[i
]);
5523 for (i
= 0; i
< OWNER_HASH_SIZE
; i
++)
5524 INIT_LIST_HEAD(&nn
->ownerstr_hashtbl
[i
]);
5525 for (i
= 0; i
< SESSION_HASH_SIZE
; i
++)
5526 INIT_LIST_HEAD(&nn
->sessionid_hashtbl
[i
]);
5527 nn
->conf_name_tree
= RB_ROOT
;
5528 nn
->unconf_name_tree
= RB_ROOT
;
5529 INIT_LIST_HEAD(&nn
->client_lru
);
5530 INIT_LIST_HEAD(&nn
->close_lru
);
5531 INIT_LIST_HEAD(&nn
->del_recall_lru
);
5532 spin_lock_init(&nn
->client_lock
);
5534 INIT_DELAYED_WORK(&nn
->laundromat_work
, laundromat_main
);
5540 kfree(nn
->ownerstr_hashtbl
);
5542 kfree(nn
->unconf_id_hashtbl
);
5544 kfree(nn
->conf_id_hashtbl
);
5550 nfs4_state_destroy_net(struct net
*net
)
5553 struct nfs4_client
*clp
= NULL
;
5554 struct nfsd_net
*nn
= net_generic(net
, nfsd_net_id
);
5556 for (i
= 0; i
< CLIENT_HASH_SIZE
; i
++) {
5557 while (!list_empty(&nn
->conf_id_hashtbl
[i
])) {
5558 clp
= list_entry(nn
->conf_id_hashtbl
[i
].next
, struct nfs4_client
, cl_idhash
);
5559 destroy_client(clp
);
5563 for (i
= 0; i
< CLIENT_HASH_SIZE
; i
++) {
5564 while (!list_empty(&nn
->unconf_id_hashtbl
[i
])) {
5565 clp
= list_entry(nn
->unconf_id_hashtbl
[i
].next
, struct nfs4_client
, cl_idhash
);
5566 destroy_client(clp
);
5570 kfree(nn
->sessionid_hashtbl
);
5571 kfree(nn
->ownerstr_hashtbl
);
5572 kfree(nn
->unconf_id_hashtbl
);
5573 kfree(nn
->conf_id_hashtbl
);
5578 nfs4_state_start_net(struct net
*net
)
5580 struct nfsd_net
*nn
= net_generic(net
, nfsd_net_id
);
5583 ret
= nfs4_state_create_net(net
);
5586 nfsd4_client_tracking_init(net
);
5587 nn
->boot_time
= get_seconds();
5588 locks_start_grace(net
, &nn
->nfsd4_manager
);
5589 nn
->grace_ended
= false;
5590 printk(KERN_INFO
"NFSD: starting %ld-second grace period (net %p)\n",
5591 nn
->nfsd4_grace
, net
);
5592 queue_delayed_work(laundry_wq
, &nn
->laundromat_work
, nn
->nfsd4_grace
* HZ
);
5596 /* initialization to perform when the nfsd service is started: */
5599 nfs4_state_start(void)
5603 ret
= set_callback_cred();
5606 laundry_wq
= create_singlethread_workqueue("nfsd4");
5607 if (laundry_wq
== NULL
) {
5611 ret
= nfsd4_create_callback_queue();
5613 goto out_free_laundry
;
5615 set_max_delegations();
5620 destroy_workqueue(laundry_wq
);
5626 nfs4_state_shutdown_net(struct net
*net
)
5628 struct nfs4_delegation
*dp
= NULL
;
5629 struct list_head
*pos
, *next
, reaplist
;
5630 struct nfsd_net
*nn
= net_generic(net
, nfsd_net_id
);
5632 cancel_delayed_work_sync(&nn
->laundromat_work
);
5633 locks_end_grace(&nn
->nfsd4_manager
);
5636 INIT_LIST_HEAD(&reaplist
);
5637 spin_lock(&state_lock
);
5638 list_for_each_safe(pos
, next
, &nn
->del_recall_lru
) {
5639 dp
= list_entry (pos
, struct nfs4_delegation
, dl_recall_lru
);
5640 list_move(&dp
->dl_recall_lru
, &reaplist
);
5642 spin_unlock(&state_lock
);
5643 list_for_each_safe(pos
, next
, &reaplist
) {
5644 dp
= list_entry (pos
, struct nfs4_delegation
, dl_recall_lru
);
5645 destroy_delegation(dp
);
5648 nfsd4_client_tracking_exit(net
);
5649 nfs4_state_destroy_net(net
);
5650 nfs4_unlock_state();
5654 nfs4_state_shutdown(void)
5656 destroy_workqueue(laundry_wq
);
5657 nfsd4_destroy_callback_queue();
5661 get_stateid(struct nfsd4_compound_state
*cstate
, stateid_t
*stateid
)
5663 if (HAS_STATE_ID(cstate
, CURRENT_STATE_ID_FLAG
) && CURRENT_STATEID(stateid
))
5664 memcpy(stateid
, &cstate
->current_stateid
, sizeof(stateid_t
));
5668 put_stateid(struct nfsd4_compound_state
*cstate
, stateid_t
*stateid
)
5670 if (cstate
->minorversion
) {
5671 memcpy(&cstate
->current_stateid
, stateid
, sizeof(stateid_t
));
5672 SET_STATE_ID(cstate
, CURRENT_STATE_ID_FLAG
);
5677 clear_current_stateid(struct nfsd4_compound_state
*cstate
)
5679 CLEAR_STATE_ID(cstate
, CURRENT_STATE_ID_FLAG
);
5683 * functions to set current state id
5686 nfsd4_set_opendowngradestateid(struct nfsd4_compound_state
*cstate
, struct nfsd4_open_downgrade
*odp
)
5688 put_stateid(cstate
, &odp
->od_stateid
);
5692 nfsd4_set_openstateid(struct nfsd4_compound_state
*cstate
, struct nfsd4_open
*open
)
5694 put_stateid(cstate
, &open
->op_stateid
);
5698 nfsd4_set_closestateid(struct nfsd4_compound_state
*cstate
, struct nfsd4_close
*close
)
5700 put_stateid(cstate
, &close
->cl_stateid
);
5704 nfsd4_set_lockstateid(struct nfsd4_compound_state
*cstate
, struct nfsd4_lock
*lock
)
5706 put_stateid(cstate
, &lock
->lk_resp_stateid
);
5710 * functions to consume current state id
5714 nfsd4_get_opendowngradestateid(struct nfsd4_compound_state
*cstate
, struct nfsd4_open_downgrade
*odp
)
5716 get_stateid(cstate
, &odp
->od_stateid
);
5720 nfsd4_get_delegreturnstateid(struct nfsd4_compound_state
*cstate
, struct nfsd4_delegreturn
*drp
)
5722 get_stateid(cstate
, &drp
->dr_stateid
);
5726 nfsd4_get_freestateid(struct nfsd4_compound_state
*cstate
, struct nfsd4_free_stateid
*fsp
)
5728 get_stateid(cstate
, &fsp
->fr_stateid
);
5732 nfsd4_get_setattrstateid(struct nfsd4_compound_state
*cstate
, struct nfsd4_setattr
*setattr
)
5734 get_stateid(cstate
, &setattr
->sa_stateid
);
5738 nfsd4_get_closestateid(struct nfsd4_compound_state
*cstate
, struct nfsd4_close
*close
)
5740 get_stateid(cstate
, &close
->cl_stateid
);
5744 nfsd4_get_lockustateid(struct nfsd4_compound_state
*cstate
, struct nfsd4_locku
*locku
)
5746 get_stateid(cstate
, &locku
->lu_stateid
);
5750 nfsd4_get_readstateid(struct nfsd4_compound_state
*cstate
, struct nfsd4_read
*read
)
5752 get_stateid(cstate
, &read
->rd_stateid
);
5756 nfsd4_get_writestateid(struct nfsd4_compound_state
*cstate
, struct nfsd4_write
*write
)
5758 get_stateid(cstate
, &write
->wr_stateid
);