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>
47 #include "current_stateid.h"
51 #define NFSDDBG_FACILITY NFSDDBG_PROC
53 #define all_ones {{~0,~0},~0}
54 static const stateid_t one_stateid
= {
56 .si_opaque
= all_ones
,
58 static const stateid_t zero_stateid
= {
61 static const stateid_t currentstateid
= {
65 static u64 current_sessionid
= 1;
67 #define ZERO_STATEID(stateid) (!memcmp((stateid), &zero_stateid, sizeof(stateid_t)))
68 #define ONE_STATEID(stateid) (!memcmp((stateid), &one_stateid, sizeof(stateid_t)))
69 #define CURRENT_STATEID(stateid) (!memcmp((stateid), ¤tstateid, sizeof(stateid_t)))
71 /* forward declarations */
72 static int check_for_locks(struct nfs4_file
*filp
, struct nfs4_lockowner
*lowner
);
76 /* Currently used for almost all code touching nfsv4 state: */
77 static DEFINE_MUTEX(client_mutex
);
80 * Currently used for the del_recall_lru and file hash table. In an
81 * effort to decrease the scope of the client_mutex, this spinlock may
82 * eventually cover more:
84 static DEFINE_SPINLOCK(recall_lock
);
86 static struct kmem_cache
*openowner_slab
= NULL
;
87 static struct kmem_cache
*lockowner_slab
= NULL
;
88 static struct kmem_cache
*file_slab
= NULL
;
89 static struct kmem_cache
*stateid_slab
= NULL
;
90 static struct kmem_cache
*deleg_slab
= NULL
;
95 mutex_lock(&client_mutex
);
98 static void free_session(struct nfsd4_session
*);
100 static bool is_session_dead(struct nfsd4_session
*ses
)
102 return ses
->se_flags
& NFS4_SESSION_DEAD
;
105 void nfsd4_put_session(struct nfsd4_session
*ses
)
107 if (atomic_dec_and_test(&ses
->se_ref
) && is_session_dead(ses
))
111 static __be32
mark_session_dead_locked(struct nfsd4_session
*ses
, int ref_held_by_me
)
113 if (atomic_read(&ses
->se_ref
) > ref_held_by_me
)
114 return nfserr_jukebox
;
115 ses
->se_flags
|= NFS4_SESSION_DEAD
;
119 static __be32
nfsd4_get_session_locked(struct nfsd4_session
*ses
)
121 if (is_session_dead(ses
))
122 return nfserr_badsession
;
123 atomic_inc(&ses
->se_ref
);
128 nfs4_unlock_state(void)
130 mutex_unlock(&client_mutex
);
133 static bool is_client_expired(struct nfs4_client
*clp
)
135 return clp
->cl_time
== 0;
138 static __be32
mark_client_expired_locked(struct nfs4_client
*clp
)
140 if (atomic_read(&clp
->cl_refcount
))
141 return nfserr_jukebox
;
146 static __be32
mark_client_expired(struct nfs4_client
*clp
)
148 struct nfsd_net
*nn
= net_generic(clp
->net
, nfsd_net_id
);
151 spin_lock(&nn
->client_lock
);
152 ret
= mark_client_expired_locked(clp
);
153 spin_unlock(&nn
->client_lock
);
157 static __be32
get_client_locked(struct nfs4_client
*clp
)
159 if (is_client_expired(clp
))
160 return nfserr_expired
;
161 atomic_inc(&clp
->cl_refcount
);
165 /* must be called under the client_lock */
167 renew_client_locked(struct nfs4_client
*clp
)
169 struct nfsd_net
*nn
= net_generic(clp
->net
, nfsd_net_id
);
171 if (is_client_expired(clp
)) {
173 printk("%s: client (clientid %08x/%08x) already expired\n",
175 clp
->cl_clientid
.cl_boot
,
176 clp
->cl_clientid
.cl_id
);
180 dprintk("renewing client (clientid %08x/%08x)\n",
181 clp
->cl_clientid
.cl_boot
,
182 clp
->cl_clientid
.cl_id
);
183 list_move_tail(&clp
->cl_lru
, &nn
->client_lru
);
184 clp
->cl_time
= get_seconds();
188 renew_client(struct nfs4_client
*clp
)
190 struct nfsd_net
*nn
= net_generic(clp
->net
, nfsd_net_id
);
192 spin_lock(&nn
->client_lock
);
193 renew_client_locked(clp
);
194 spin_unlock(&nn
->client_lock
);
197 static void put_client_renew_locked(struct nfs4_client
*clp
)
199 if (!atomic_dec_and_test(&clp
->cl_refcount
))
201 if (!is_client_expired(clp
))
202 renew_client_locked(clp
);
205 void put_client_renew(struct nfs4_client
*clp
)
207 struct nfsd_net
*nn
= net_generic(clp
->net
, nfsd_net_id
);
209 if (!atomic_dec_and_lock(&clp
->cl_refcount
, &nn
->client_lock
))
211 if (!is_client_expired(clp
))
212 renew_client_locked(clp
);
213 spin_unlock(&nn
->client_lock
);
218 opaque_hashval(const void *ptr
, int nbytes
)
220 unsigned char *cptr
= (unsigned char *) ptr
;
230 static void nfsd4_free_file(struct nfs4_file
*f
)
232 kmem_cache_free(file_slab
, f
);
236 put_nfs4_file(struct nfs4_file
*fi
)
238 if (atomic_dec_and_lock(&fi
->fi_ref
, &recall_lock
)) {
239 hlist_del(&fi
->fi_hash
);
240 spin_unlock(&recall_lock
);
247 get_nfs4_file(struct nfs4_file
*fi
)
249 atomic_inc(&fi
->fi_ref
);
252 static int num_delegations
;
253 unsigned long max_delegations
;
256 * Open owner state (share locks)
259 /* hash tables for lock and open owners */
260 #define OWNER_HASH_BITS 8
261 #define OWNER_HASH_SIZE (1 << OWNER_HASH_BITS)
262 #define OWNER_HASH_MASK (OWNER_HASH_SIZE - 1)
264 static unsigned int ownerstr_hashval(u32 clientid
, struct xdr_netobj
*ownername
)
268 ret
= opaque_hashval(ownername
->data
, ownername
->len
);
270 return ret
& OWNER_HASH_MASK
;
273 /* hash table for nfs4_file */
274 #define FILE_HASH_BITS 8
275 #define FILE_HASH_SIZE (1 << FILE_HASH_BITS)
277 static unsigned int file_hashval(struct inode
*ino
)
279 /* XXX: why are we hashing on inode pointer, anyway? */
280 return hash_ptr(ino
, FILE_HASH_BITS
);
283 static struct hlist_head file_hashtbl
[FILE_HASH_SIZE
];
285 static void nfs4_file_get_access(struct nfs4_file
*fp
, int oflag
)
287 WARN_ON_ONCE(!fp
->fi_fds
[oflag
]);
288 if (oflag
== O_RDWR
) {
289 atomic_inc(&fp
->fi_access
[O_RDONLY
]);
290 atomic_inc(&fp
->fi_access
[O_WRONLY
]);
292 atomic_inc(&fp
->fi_access
[oflag
]);
295 static void nfs4_file_put_fd(struct nfs4_file
*fp
, int oflag
)
297 if (fp
->fi_fds
[oflag
]) {
298 fput(fp
->fi_fds
[oflag
]);
299 fp
->fi_fds
[oflag
] = NULL
;
303 static void __nfs4_file_put_access(struct nfs4_file
*fp
, int oflag
)
305 if (atomic_dec_and_test(&fp
->fi_access
[oflag
])) {
306 nfs4_file_put_fd(fp
, oflag
);
307 if (atomic_read(&fp
->fi_access
[1 - oflag
]) == 0)
308 nfs4_file_put_fd(fp
, O_RDWR
);
312 static void nfs4_file_put_access(struct nfs4_file
*fp
, int oflag
)
314 if (oflag
== O_RDWR
) {
315 __nfs4_file_put_access(fp
, O_RDONLY
);
316 __nfs4_file_put_access(fp
, O_WRONLY
);
318 __nfs4_file_put_access(fp
, oflag
);
321 static struct nfs4_stid
*nfs4_alloc_stid(struct nfs4_client
*cl
, struct
324 struct idr
*stateids
= &cl
->cl_stateids
;
325 struct nfs4_stid
*stid
;
328 stid
= kmem_cache_alloc(slab
, GFP_KERNEL
);
332 new_id
= idr_alloc_cyclic(stateids
, stid
, 0, 0, GFP_KERNEL
);
335 stid
->sc_client
= cl
;
337 stid
->sc_stateid
.si_opaque
.so_id
= new_id
;
338 stid
->sc_stateid
.si_opaque
.so_clid
= cl
->cl_clientid
;
339 /* Will be incremented before return to client: */
340 stid
->sc_stateid
.si_generation
= 0;
343 * It shouldn't be a problem to reuse an opaque stateid value.
344 * I don't think it is for 4.1. But with 4.0 I worry that, for
345 * example, a stray write retransmission could be accepted by
346 * the server when it should have been rejected. Therefore,
347 * adopt a trick from the sctp code to attempt to maximize the
348 * amount of time until an id is reused, by ensuring they always
349 * "increase" (mod INT_MAX):
353 kmem_cache_free(slab
, stid
);
357 static struct nfs4_ol_stateid
* nfs4_alloc_stateid(struct nfs4_client
*clp
)
359 return openlockstateid(nfs4_alloc_stid(clp
, stateid_slab
));
362 static struct nfs4_delegation
*
363 alloc_init_deleg(struct nfs4_client
*clp
, struct nfs4_ol_stateid
*stp
, struct svc_fh
*current_fh
)
365 struct nfs4_delegation
*dp
;
366 struct nfs4_file
*fp
= stp
->st_file
;
368 dprintk("NFSD alloc_init_deleg\n");
369 if (fp
->fi_had_conflict
)
371 if (num_delegations
> max_delegations
)
373 dp
= delegstateid(nfs4_alloc_stid(clp
, deleg_slab
));
376 dp
->dl_stid
.sc_type
= NFS4_DELEG_STID
;
378 * delegation seqid's are never incremented. The 4.1 special
379 * meaning of seqid 0 isn't meaningful, really, but let's avoid
380 * 0 anyway just for consistency and use 1:
382 dp
->dl_stid
.sc_stateid
.si_generation
= 1;
384 INIT_LIST_HEAD(&dp
->dl_perfile
);
385 INIT_LIST_HEAD(&dp
->dl_perclnt
);
386 INIT_LIST_HEAD(&dp
->dl_recall_lru
);
389 dp
->dl_type
= NFS4_OPEN_DELEGATE_READ
;
390 fh_copy_shallow(&dp
->dl_fh
, ¤t_fh
->fh_handle
);
392 atomic_set(&dp
->dl_count
, 1);
393 nfsd4_init_callback(&dp
->dl_recall
);
397 static void remove_stid(struct nfs4_stid
*s
)
399 struct idr
*stateids
= &s
->sc_client
->cl_stateids
;
401 idr_remove(stateids
, s
->sc_stateid
.si_opaque
.so_id
);
405 nfs4_put_delegation(struct nfs4_delegation
*dp
)
407 if (atomic_dec_and_test(&dp
->dl_count
)) {
408 kmem_cache_free(deleg_slab
, dp
);
413 static void nfs4_put_deleg_lease(struct nfs4_file
*fp
)
415 if (atomic_dec_and_test(&fp
->fi_delegees
)) {
416 vfs_setlease(fp
->fi_deleg_file
, F_UNLCK
, &fp
->fi_lease
);
418 fput(fp
->fi_deleg_file
);
419 fp
->fi_deleg_file
= NULL
;
423 static void unhash_stid(struct nfs4_stid
*s
)
428 /* Called under the state lock. */
430 unhash_delegation(struct nfs4_delegation
*dp
)
432 list_del_init(&dp
->dl_perclnt
);
433 spin_lock(&recall_lock
);
434 list_del_init(&dp
->dl_perfile
);
435 list_del_init(&dp
->dl_recall_lru
);
436 spin_unlock(&recall_lock
);
437 nfs4_put_deleg_lease(dp
->dl_file
);
438 put_nfs4_file(dp
->dl_file
);
444 static void destroy_revoked_delegation(struct nfs4_delegation
*dp
)
446 list_del_init(&dp
->dl_recall_lru
);
447 remove_stid(&dp
->dl_stid
);
448 nfs4_put_delegation(dp
);
451 static void destroy_delegation(struct nfs4_delegation
*dp
)
453 unhash_delegation(dp
);
454 remove_stid(&dp
->dl_stid
);
455 nfs4_put_delegation(dp
);
458 static void revoke_delegation(struct nfs4_delegation
*dp
)
460 struct nfs4_client
*clp
= dp
->dl_stid
.sc_client
;
462 if (clp
->cl_minorversion
== 0)
463 destroy_delegation(dp
);
465 unhash_delegation(dp
);
466 dp
->dl_stid
.sc_type
= NFS4_REVOKED_DELEG_STID
;
467 list_add(&dp
->dl_recall_lru
, &clp
->cl_revoked
);
475 static unsigned int clientid_hashval(u32 id
)
477 return id
& CLIENT_HASH_MASK
;
480 static unsigned int clientstr_hashval(const char *name
)
482 return opaque_hashval(name
, 8) & CLIENT_HASH_MASK
;
486 * We store the NONE, READ, WRITE, and BOTH bits separately in the
487 * st_{access,deny}_bmap field of the stateid, in order to track not
488 * only what share bits are currently in force, but also what
489 * combinations of share bits previous opens have used. This allows us
490 * to enforce the recommendation of rfc 3530 14.2.19 that the server
491 * return an error if the client attempt to downgrade to a combination
492 * of share bits not explicable by closing some of its previous opens.
494 * XXX: This enforcement is actually incomplete, since we don't keep
495 * track of access/deny bit combinations; so, e.g., we allow:
497 * OPEN allow read, deny write
498 * OPEN allow both, deny none
499 * DOWNGRADE allow read, deny none
501 * which we should reject.
504 bmap_to_share_mode(unsigned long bmap
) {
506 unsigned int access
= 0;
508 for (i
= 1; i
< 4; i
++) {
509 if (test_bit(i
, &bmap
))
516 test_share(struct nfs4_ol_stateid
*stp
, struct nfsd4_open
*open
) {
517 unsigned int access
, deny
;
519 access
= bmap_to_share_mode(stp
->st_access_bmap
);
520 deny
= bmap_to_share_mode(stp
->st_deny_bmap
);
521 if ((access
& open
->op_share_deny
) || (deny
& open
->op_share_access
))
526 /* set share access for a given stateid */
528 set_access(u32 access
, struct nfs4_ol_stateid
*stp
)
530 __set_bit(access
, &stp
->st_access_bmap
);
533 /* clear share access for a given stateid */
535 clear_access(u32 access
, struct nfs4_ol_stateid
*stp
)
537 __clear_bit(access
, &stp
->st_access_bmap
);
540 /* test whether a given stateid has access */
542 test_access(u32 access
, struct nfs4_ol_stateid
*stp
)
544 return test_bit(access
, &stp
->st_access_bmap
);
547 /* set share deny for a given stateid */
549 set_deny(u32 access
, struct nfs4_ol_stateid
*stp
)
551 __set_bit(access
, &stp
->st_deny_bmap
);
554 /* clear share deny for a given stateid */
556 clear_deny(u32 access
, struct nfs4_ol_stateid
*stp
)
558 __clear_bit(access
, &stp
->st_deny_bmap
);
561 /* test whether a given stateid is denying specific access */
563 test_deny(u32 access
, struct nfs4_ol_stateid
*stp
)
565 return test_bit(access
, &stp
->st_deny_bmap
);
568 static int nfs4_access_to_omode(u32 access
)
570 switch (access
& NFS4_SHARE_ACCESS_BOTH
) {
571 case NFS4_SHARE_ACCESS_READ
:
573 case NFS4_SHARE_ACCESS_WRITE
:
575 case NFS4_SHARE_ACCESS_BOTH
:
582 /* release all access and file references for a given stateid */
584 release_all_access(struct nfs4_ol_stateid
*stp
)
588 for (i
= 1; i
< 4; i
++) {
589 if (test_access(i
, stp
))
590 nfs4_file_put_access(stp
->st_file
,
591 nfs4_access_to_omode(i
));
592 clear_access(i
, stp
);
596 static void unhash_generic_stateid(struct nfs4_ol_stateid
*stp
)
598 list_del(&stp
->st_perfile
);
599 list_del(&stp
->st_perstateowner
);
602 static void close_generic_stateid(struct nfs4_ol_stateid
*stp
)
604 release_all_access(stp
);
605 put_nfs4_file(stp
->st_file
);
609 static void free_generic_stateid(struct nfs4_ol_stateid
*stp
)
611 remove_stid(&stp
->st_stid
);
612 kmem_cache_free(stateid_slab
, stp
);
615 static void release_lock_stateid(struct nfs4_ol_stateid
*stp
)
619 unhash_generic_stateid(stp
);
620 unhash_stid(&stp
->st_stid
);
621 file
= find_any_file(stp
->st_file
);
623 locks_remove_posix(file
, (fl_owner_t
)lockowner(stp
->st_stateowner
));
624 close_generic_stateid(stp
);
625 free_generic_stateid(stp
);
628 static void unhash_lockowner(struct nfs4_lockowner
*lo
)
630 struct nfs4_ol_stateid
*stp
;
632 list_del(&lo
->lo_owner
.so_strhash
);
633 list_del(&lo
->lo_perstateid
);
634 list_del(&lo
->lo_owner_ino_hash
);
635 while (!list_empty(&lo
->lo_owner
.so_stateids
)) {
636 stp
= list_first_entry(&lo
->lo_owner
.so_stateids
,
637 struct nfs4_ol_stateid
, st_perstateowner
);
638 release_lock_stateid(stp
);
642 static void release_lockowner(struct nfs4_lockowner
*lo
)
644 unhash_lockowner(lo
);
645 nfs4_free_lockowner(lo
);
649 release_stateid_lockowners(struct nfs4_ol_stateid
*open_stp
)
651 struct nfs4_lockowner
*lo
;
653 while (!list_empty(&open_stp
->st_lockowners
)) {
654 lo
= list_entry(open_stp
->st_lockowners
.next
,
655 struct nfs4_lockowner
, lo_perstateid
);
656 release_lockowner(lo
);
660 static void unhash_open_stateid(struct nfs4_ol_stateid
*stp
)
662 unhash_generic_stateid(stp
);
663 release_stateid_lockowners(stp
);
664 close_generic_stateid(stp
);
667 static void release_open_stateid(struct nfs4_ol_stateid
*stp
)
669 unhash_open_stateid(stp
);
670 unhash_stid(&stp
->st_stid
);
671 free_generic_stateid(stp
);
674 static void unhash_openowner(struct nfs4_openowner
*oo
)
676 struct nfs4_ol_stateid
*stp
;
678 list_del(&oo
->oo_owner
.so_strhash
);
679 list_del(&oo
->oo_perclient
);
680 while (!list_empty(&oo
->oo_owner
.so_stateids
)) {
681 stp
= list_first_entry(&oo
->oo_owner
.so_stateids
,
682 struct nfs4_ol_stateid
, st_perstateowner
);
683 release_open_stateid(stp
);
687 static void release_last_closed_stateid(struct nfs4_openowner
*oo
)
689 struct nfs4_ol_stateid
*s
= oo
->oo_last_closed_stid
;
692 unhash_stid(&s
->st_stid
);
693 free_generic_stateid(s
);
694 oo
->oo_last_closed_stid
= NULL
;
698 static void release_openowner(struct nfs4_openowner
*oo
)
700 unhash_openowner(oo
);
701 list_del(&oo
->oo_close_lru
);
702 release_last_closed_stateid(oo
);
703 nfs4_free_openowner(oo
);
707 hash_sessionid(struct nfs4_sessionid
*sessionid
)
709 struct nfsd4_sessionid
*sid
= (struct nfsd4_sessionid
*)sessionid
;
711 return sid
->sequence
% SESSION_HASH_SIZE
;
716 dump_sessionid(const char *fn
, struct nfs4_sessionid
*sessionid
)
718 u32
*ptr
= (u32
*)(&sessionid
->data
[0]);
719 dprintk("%s: %u:%u:%u:%u\n", fn
, ptr
[0], ptr
[1], ptr
[2], ptr
[3]);
723 dump_sessionid(const char *fn
, struct nfs4_sessionid
*sessionid
)
729 * Bump the seqid on cstate->replay_owner, and clear replay_owner if it
730 * won't be used for replay.
732 void nfsd4_bump_seqid(struct nfsd4_compound_state
*cstate
, __be32 nfserr
)
734 struct nfs4_stateowner
*so
= cstate
->replay_owner
;
736 if (nfserr
== nfserr_replay_me
)
739 if (!seqid_mutating_err(ntohl(nfserr
))) {
740 cstate
->replay_owner
= NULL
;
745 if (so
->so_is_open_owner
)
746 release_last_closed_stateid(openowner(so
));
752 gen_sessionid(struct nfsd4_session
*ses
)
754 struct nfs4_client
*clp
= ses
->se_client
;
755 struct nfsd4_sessionid
*sid
;
757 sid
= (struct nfsd4_sessionid
*)ses
->se_sessionid
.data
;
758 sid
->clientid
= clp
->cl_clientid
;
759 sid
->sequence
= current_sessionid
++;
764 * The protocol defines ca_maxresponssize_cached to include the size of
765 * the rpc header, but all we need to cache is the data starting after
766 * the end of the initial SEQUENCE operation--the rest we regenerate
767 * each time. Therefore we can advertise a ca_maxresponssize_cached
768 * value that is the number of bytes in our cache plus a few additional
769 * bytes. In order to stay on the safe side, and not promise more than
770 * we can cache, those additional bytes must be the minimum possible: 24
771 * bytes of rpc header (xid through accept state, with AUTH_NULL
772 * verifier), 12 for the compound header (with zero-length tag), and 44
773 * for the SEQUENCE op response:
775 #define NFSD_MIN_HDR_SEQ_SZ (24 + 12 + 44)
778 free_session_slots(struct nfsd4_session
*ses
)
782 for (i
= 0; i
< ses
->se_fchannel
.maxreqs
; i
++)
783 kfree(ses
->se_slots
[i
]);
787 * We don't actually need to cache the rpc and session headers, so we
788 * can allocate a little less for each slot:
790 static inline u32
slot_bytes(struct nfsd4_channel_attrs
*ca
)
794 if (ca
->maxresp_cached
< NFSD_MIN_HDR_SEQ_SZ
)
797 size
= ca
->maxresp_cached
- NFSD_MIN_HDR_SEQ_SZ
;
798 return size
+ sizeof(struct nfsd4_slot
);
802 * XXX: If we run out of reserved DRC memory we could (up to a point)
803 * re-negotiate active sessions and reduce their slot usage to make
804 * room for new connections. For now we just fail the create session.
806 static u32
nfsd4_get_drc_mem(struct nfsd4_channel_attrs
*ca
)
808 u32 slotsize
= slot_bytes(ca
);
809 u32 num
= ca
->maxreqs
;
812 spin_lock(&nfsd_drc_lock
);
813 avail
= min((unsigned long)NFSD_MAX_MEM_PER_SESSION
,
814 nfsd_drc_max_mem
- nfsd_drc_mem_used
);
815 num
= min_t(int, num
, avail
/ slotsize
);
816 nfsd_drc_mem_used
+= num
* slotsize
;
817 spin_unlock(&nfsd_drc_lock
);
822 static void nfsd4_put_drc_mem(struct nfsd4_channel_attrs
*ca
)
824 int slotsize
= slot_bytes(ca
);
826 spin_lock(&nfsd_drc_lock
);
827 nfsd_drc_mem_used
-= slotsize
* ca
->maxreqs
;
828 spin_unlock(&nfsd_drc_lock
);
831 static struct nfsd4_session
*alloc_session(struct nfsd4_channel_attrs
*attrs
)
833 int numslots
= attrs
->maxreqs
;
834 int slotsize
= slot_bytes(attrs
);
835 struct nfsd4_session
*new;
838 BUILD_BUG_ON(NFSD_MAX_SLOTS_PER_SESSION
* sizeof(struct nfsd4_slot
*)
839 + sizeof(struct nfsd4_session
) > PAGE_SIZE
);
840 mem
= numslots
* sizeof(struct nfsd4_slot
*);
842 new = kzalloc(sizeof(*new) + mem
, GFP_KERNEL
);
845 /* allocate each struct nfsd4_slot and data cache in one piece */
846 for (i
= 0; i
< numslots
; i
++) {
847 new->se_slots
[i
] = kzalloc(slotsize
, GFP_KERNEL
);
848 if (!new->se_slots
[i
])
854 kfree(new->se_slots
[i
]);
859 static void free_conn(struct nfsd4_conn
*c
)
861 svc_xprt_put(c
->cn_xprt
);
865 static void nfsd4_conn_lost(struct svc_xpt_user
*u
)
867 struct nfsd4_conn
*c
= container_of(u
, struct nfsd4_conn
, cn_xpt_user
);
868 struct nfs4_client
*clp
= c
->cn_session
->se_client
;
870 spin_lock(&clp
->cl_lock
);
871 if (!list_empty(&c
->cn_persession
)) {
872 list_del(&c
->cn_persession
);
875 nfsd4_probe_callback(clp
);
876 spin_unlock(&clp
->cl_lock
);
879 static struct nfsd4_conn
*alloc_conn(struct svc_rqst
*rqstp
, u32 flags
)
881 struct nfsd4_conn
*conn
;
883 conn
= kmalloc(sizeof(struct nfsd4_conn
), GFP_KERNEL
);
886 svc_xprt_get(rqstp
->rq_xprt
);
887 conn
->cn_xprt
= rqstp
->rq_xprt
;
888 conn
->cn_flags
= flags
;
889 INIT_LIST_HEAD(&conn
->cn_xpt_user
.list
);
893 static void __nfsd4_hash_conn(struct nfsd4_conn
*conn
, struct nfsd4_session
*ses
)
895 conn
->cn_session
= ses
;
896 list_add(&conn
->cn_persession
, &ses
->se_conns
);
899 static void nfsd4_hash_conn(struct nfsd4_conn
*conn
, struct nfsd4_session
*ses
)
901 struct nfs4_client
*clp
= ses
->se_client
;
903 spin_lock(&clp
->cl_lock
);
904 __nfsd4_hash_conn(conn
, ses
);
905 spin_unlock(&clp
->cl_lock
);
908 static int nfsd4_register_conn(struct nfsd4_conn
*conn
)
910 conn
->cn_xpt_user
.callback
= nfsd4_conn_lost
;
911 return register_xpt_user(conn
->cn_xprt
, &conn
->cn_xpt_user
);
914 static void nfsd4_init_conn(struct svc_rqst
*rqstp
, struct nfsd4_conn
*conn
, struct nfsd4_session
*ses
)
918 nfsd4_hash_conn(conn
, ses
);
919 ret
= nfsd4_register_conn(conn
);
921 /* oops; xprt is already down: */
922 nfsd4_conn_lost(&conn
->cn_xpt_user
);
923 if (conn
->cn_flags
& NFS4_CDFC4_BACK
) {
924 /* callback channel may be back up */
925 nfsd4_probe_callback(ses
->se_client
);
929 static struct nfsd4_conn
*alloc_conn_from_crses(struct svc_rqst
*rqstp
, struct nfsd4_create_session
*cses
)
931 u32 dir
= NFS4_CDFC4_FORE
;
933 if (cses
->flags
& SESSION4_BACK_CHAN
)
934 dir
|= NFS4_CDFC4_BACK
;
935 return alloc_conn(rqstp
, dir
);
938 /* must be called under client_lock */
939 static void nfsd4_del_conns(struct nfsd4_session
*s
)
941 struct nfs4_client
*clp
= s
->se_client
;
942 struct nfsd4_conn
*c
;
944 spin_lock(&clp
->cl_lock
);
945 while (!list_empty(&s
->se_conns
)) {
946 c
= list_first_entry(&s
->se_conns
, struct nfsd4_conn
, cn_persession
);
947 list_del_init(&c
->cn_persession
);
948 spin_unlock(&clp
->cl_lock
);
950 unregister_xpt_user(c
->cn_xprt
, &c
->cn_xpt_user
);
953 spin_lock(&clp
->cl_lock
);
955 spin_unlock(&clp
->cl_lock
);
958 static void __free_session(struct nfsd4_session
*ses
)
960 free_session_slots(ses
);
964 static void free_session(struct nfsd4_session
*ses
)
966 struct nfsd_net
*nn
= net_generic(ses
->se_client
->net
, nfsd_net_id
);
968 lockdep_assert_held(&nn
->client_lock
);
969 nfsd4_del_conns(ses
);
970 nfsd4_put_drc_mem(&ses
->se_fchannel
);
974 static void init_session(struct svc_rqst
*rqstp
, struct nfsd4_session
*new, struct nfs4_client
*clp
, struct nfsd4_create_session
*cses
)
977 struct nfsd_net
*nn
= net_generic(SVC_NET(rqstp
), nfsd_net_id
);
979 new->se_client
= clp
;
982 INIT_LIST_HEAD(&new->se_conns
);
984 new->se_cb_seq_nr
= 1;
985 new->se_flags
= cses
->flags
;
986 new->se_cb_prog
= cses
->callback_prog
;
987 new->se_cb_sec
= cses
->cb_sec
;
988 atomic_set(&new->se_ref
, 0);
989 idx
= hash_sessionid(&new->se_sessionid
);
990 spin_lock(&nn
->client_lock
);
991 list_add(&new->se_hash
, &nn
->sessionid_hashtbl
[idx
]);
992 spin_lock(&clp
->cl_lock
);
993 list_add(&new->se_perclnt
, &clp
->cl_sessions
);
994 spin_unlock(&clp
->cl_lock
);
995 spin_unlock(&nn
->client_lock
);
996 memcpy(&new->se_fchannel
, &cses
->fore_channel
,
997 sizeof(struct nfsd4_channel_attrs
));
998 if (cses
->flags
& SESSION4_BACK_CHAN
) {
999 struct sockaddr
*sa
= svc_addr(rqstp
);
1001 * This is a little silly; with sessions there's no real
1002 * use for the callback address. Use the peer address
1003 * as a reasonable default for now, but consider fixing
1004 * the rpc client not to require an address in the
1007 rpc_copy_addr((struct sockaddr
*)&clp
->cl_cb_conn
.cb_addr
, sa
);
1008 clp
->cl_cb_conn
.cb_addrlen
= svc_addr_len(sa
);
1012 /* caller must hold client_lock */
1013 static struct nfsd4_session
*
1014 find_in_sessionid_hashtbl(struct nfs4_sessionid
*sessionid
, struct net
*net
)
1016 struct nfsd4_session
*elem
;
1018 struct nfsd_net
*nn
= net_generic(net
, nfsd_net_id
);
1020 dump_sessionid(__func__
, sessionid
);
1021 idx
= hash_sessionid(sessionid
);
1022 /* Search in the appropriate list */
1023 list_for_each_entry(elem
, &nn
->sessionid_hashtbl
[idx
], se_hash
) {
1024 if (!memcmp(elem
->se_sessionid
.data
, sessionid
->data
,
1025 NFS4_MAX_SESSIONID_LEN
)) {
1030 dprintk("%s: session not found\n", __func__
);
1034 /* caller must hold client_lock */
1036 unhash_session(struct nfsd4_session
*ses
)
1038 list_del(&ses
->se_hash
);
1039 spin_lock(&ses
->se_client
->cl_lock
);
1040 list_del(&ses
->se_perclnt
);
1041 spin_unlock(&ses
->se_client
->cl_lock
);
1044 /* SETCLIENTID and SETCLIENTID_CONFIRM Helper functions */
1046 STALE_CLIENTID(clientid_t
*clid
, struct nfsd_net
*nn
)
1048 if (clid
->cl_boot
== nn
->boot_time
)
1050 dprintk("NFSD stale clientid (%08x/%08x) boot_time %08lx\n",
1051 clid
->cl_boot
, clid
->cl_id
, nn
->boot_time
);
1056 * XXX Should we use a slab cache ?
1057 * This type of memory management is somewhat inefficient, but we use it
1058 * anyway since SETCLIENTID is not a common operation.
1060 static struct nfs4_client
*alloc_client(struct xdr_netobj name
)
1062 struct nfs4_client
*clp
;
1064 clp
= kzalloc(sizeof(struct nfs4_client
), GFP_KERNEL
);
1067 clp
->cl_name
.data
= kmemdup(name
.data
, name
.len
, GFP_KERNEL
);
1068 if (clp
->cl_name
.data
== NULL
) {
1072 clp
->cl_name
.len
= name
.len
;
1077 free_client(struct nfs4_client
*clp
)
1079 struct nfsd_net __maybe_unused
*nn
= net_generic(clp
->net
, nfsd_net_id
);
1081 lockdep_assert_held(&nn
->client_lock
);
1082 while (!list_empty(&clp
->cl_sessions
)) {
1083 struct nfsd4_session
*ses
;
1084 ses
= list_entry(clp
->cl_sessions
.next
, struct nfsd4_session
,
1086 list_del(&ses
->se_perclnt
);
1087 WARN_ON_ONCE(atomic_read(&ses
->se_ref
));
1090 free_svc_cred(&clp
->cl_cred
);
1091 kfree(clp
->cl_name
.data
);
1092 idr_destroy(&clp
->cl_stateids
);
1096 /* must be called under the client_lock */
1098 unhash_client_locked(struct nfs4_client
*clp
)
1100 struct nfsd4_session
*ses
;
1102 list_del(&clp
->cl_lru
);
1103 spin_lock(&clp
->cl_lock
);
1104 list_for_each_entry(ses
, &clp
->cl_sessions
, se_perclnt
)
1105 list_del_init(&ses
->se_hash
);
1106 spin_unlock(&clp
->cl_lock
);
1110 destroy_client(struct nfs4_client
*clp
)
1112 struct nfs4_openowner
*oo
;
1113 struct nfs4_delegation
*dp
;
1114 struct list_head reaplist
;
1115 struct nfsd_net
*nn
= net_generic(clp
->net
, nfsd_net_id
);
1117 INIT_LIST_HEAD(&reaplist
);
1118 spin_lock(&recall_lock
);
1119 while (!list_empty(&clp
->cl_delegations
)) {
1120 dp
= list_entry(clp
->cl_delegations
.next
, struct nfs4_delegation
, dl_perclnt
);
1121 list_del_init(&dp
->dl_perclnt
);
1122 list_move(&dp
->dl_recall_lru
, &reaplist
);
1124 spin_unlock(&recall_lock
);
1125 while (!list_empty(&reaplist
)) {
1126 dp
= list_entry(reaplist
.next
, struct nfs4_delegation
, dl_recall_lru
);
1127 destroy_delegation(dp
);
1129 while (!list_empty(&clp
->cl_openowners
)) {
1130 oo
= list_entry(clp
->cl_openowners
.next
, struct nfs4_openowner
, oo_perclient
);
1131 release_openowner(oo
);
1133 nfsd4_shutdown_callback(clp
);
1134 if (clp
->cl_cb_conn
.cb_xprt
)
1135 svc_xprt_put(clp
->cl_cb_conn
.cb_xprt
);
1136 list_del(&clp
->cl_idhash
);
1137 if (test_bit(NFSD4_CLIENT_CONFIRMED
, &clp
->cl_flags
))
1138 rb_erase(&clp
->cl_namenode
, &nn
->conf_name_tree
);
1140 rb_erase(&clp
->cl_namenode
, &nn
->unconf_name_tree
);
1141 spin_lock(&nn
->client_lock
);
1142 unhash_client_locked(clp
);
1143 WARN_ON_ONCE(atomic_read(&clp
->cl_refcount
));
1145 spin_unlock(&nn
->client_lock
);
1148 static void expire_client(struct nfs4_client
*clp
)
1150 nfsd4_client_record_remove(clp
);
1151 destroy_client(clp
);
1154 static void copy_verf(struct nfs4_client
*target
, nfs4_verifier
*source
)
1156 memcpy(target
->cl_verifier
.data
, source
->data
,
1157 sizeof(target
->cl_verifier
.data
));
1160 static void copy_clid(struct nfs4_client
*target
, struct nfs4_client
*source
)
1162 target
->cl_clientid
.cl_boot
= source
->cl_clientid
.cl_boot
;
1163 target
->cl_clientid
.cl_id
= source
->cl_clientid
.cl_id
;
1166 static int copy_cred(struct svc_cred
*target
, struct svc_cred
*source
)
1168 if (source
->cr_principal
) {
1169 target
->cr_principal
=
1170 kstrdup(source
->cr_principal
, GFP_KERNEL
);
1171 if (target
->cr_principal
== NULL
)
1174 target
->cr_principal
= NULL
;
1175 target
->cr_flavor
= source
->cr_flavor
;
1176 target
->cr_uid
= source
->cr_uid
;
1177 target
->cr_gid
= source
->cr_gid
;
1178 target
->cr_group_info
= source
->cr_group_info
;
1179 get_group_info(target
->cr_group_info
);
1180 target
->cr_gss_mech
= source
->cr_gss_mech
;
1181 if (source
->cr_gss_mech
)
1182 gss_mech_get(source
->cr_gss_mech
);
1187 compare_blob(const struct xdr_netobj
*o1
, const struct xdr_netobj
*o2
)
1191 res
= o1
->len
- o2
->len
;
1194 return (long long)memcmp(o1
->data
, o2
->data
, o1
->len
);
1197 static int same_name(const char *n1
, const char *n2
)
1199 return 0 == memcmp(n1
, n2
, HEXDIR_LEN
);
1203 same_verf(nfs4_verifier
*v1
, nfs4_verifier
*v2
)
1205 return 0 == memcmp(v1
->data
, v2
->data
, sizeof(v1
->data
));
1209 same_clid(clientid_t
*cl1
, clientid_t
*cl2
)
1211 return (cl1
->cl_boot
== cl2
->cl_boot
) && (cl1
->cl_id
== cl2
->cl_id
);
1214 static bool groups_equal(struct group_info
*g1
, struct group_info
*g2
)
1218 if (g1
->ngroups
!= g2
->ngroups
)
1220 for (i
=0; i
<g1
->ngroups
; i
++)
1221 if (!gid_eq(GROUP_AT(g1
, i
), GROUP_AT(g2
, i
)))
1227 * RFC 3530 language requires clid_inuse be returned when the
1228 * "principal" associated with a requests differs from that previously
1229 * used. We use uid, gid's, and gss principal string as our best
1230 * approximation. We also don't want to allow non-gss use of a client
1231 * established using gss: in theory cr_principal should catch that
1232 * change, but in practice cr_principal can be null even in the gss case
1233 * since gssd doesn't always pass down a principal string.
1235 static bool is_gss_cred(struct svc_cred
*cr
)
1237 /* Is cr_flavor one of the gss "pseudoflavors"?: */
1238 return (cr
->cr_flavor
> RPC_AUTH_MAXFLAVOR
);
1243 same_creds(struct svc_cred
*cr1
, struct svc_cred
*cr2
)
1245 if ((is_gss_cred(cr1
) != is_gss_cred(cr2
))
1246 || (!uid_eq(cr1
->cr_uid
, cr2
->cr_uid
))
1247 || (!gid_eq(cr1
->cr_gid
, cr2
->cr_gid
))
1248 || !groups_equal(cr1
->cr_group_info
, cr2
->cr_group_info
))
1250 if (cr1
->cr_principal
== cr2
->cr_principal
)
1252 if (!cr1
->cr_principal
|| !cr2
->cr_principal
)
1254 return 0 == strcmp(cr1
->cr_principal
, cr2
->cr_principal
);
1257 static bool svc_rqst_integrity_protected(struct svc_rqst
*rqstp
)
1259 struct svc_cred
*cr
= &rqstp
->rq_cred
;
1262 service
= gss_pseudoflavor_to_service(cr
->cr_gss_mech
, cr
->cr_flavor
);
1263 return service
== RPC_GSS_SVC_INTEGRITY
||
1264 service
== RPC_GSS_SVC_PRIVACY
;
1267 static bool mach_creds_match(struct nfs4_client
*cl
, struct svc_rqst
*rqstp
)
1269 struct svc_cred
*cr
= &rqstp
->rq_cred
;
1271 if (!cl
->cl_mach_cred
)
1273 if (cl
->cl_cred
.cr_gss_mech
!= cr
->cr_gss_mech
)
1275 if (!svc_rqst_integrity_protected(rqstp
))
1277 if (!cr
->cr_principal
)
1279 return 0 == strcmp(cl
->cl_cred
.cr_principal
, cr
->cr_principal
);
1282 static void gen_clid(struct nfs4_client
*clp
, struct nfsd_net
*nn
)
1284 static u32 current_clientid
= 1;
1286 clp
->cl_clientid
.cl_boot
= nn
->boot_time
;
1287 clp
->cl_clientid
.cl_id
= current_clientid
++;
1290 static void gen_confirm(struct nfs4_client
*clp
)
1295 verf
[0] = (__be32
)get_seconds();
1296 verf
[1] = (__be32
)i
++;
1297 memcpy(clp
->cl_confirm
.data
, verf
, sizeof(clp
->cl_confirm
.data
));
1300 static struct nfs4_stid
*find_stateid(struct nfs4_client
*cl
, stateid_t
*t
)
1302 struct nfs4_stid
*ret
;
1304 ret
= idr_find(&cl
->cl_stateids
, t
->si_opaque
.so_id
);
1305 if (!ret
|| !ret
->sc_type
)
1310 static struct nfs4_stid
*find_stateid_by_type(struct nfs4_client
*cl
, stateid_t
*t
, char typemask
)
1312 struct nfs4_stid
*s
;
1314 s
= find_stateid(cl
, t
);
1317 if (typemask
& s
->sc_type
)
1322 static struct nfs4_client
*create_client(struct xdr_netobj name
,
1323 struct svc_rqst
*rqstp
, nfs4_verifier
*verf
)
1325 struct nfs4_client
*clp
;
1326 struct sockaddr
*sa
= svc_addr(rqstp
);
1328 struct net
*net
= SVC_NET(rqstp
);
1329 struct nfsd_net
*nn
= net_generic(net
, nfsd_net_id
);
1331 clp
= alloc_client(name
);
1335 INIT_LIST_HEAD(&clp
->cl_sessions
);
1336 ret
= copy_cred(&clp
->cl_cred
, &rqstp
->rq_cred
);
1338 spin_lock(&nn
->client_lock
);
1340 spin_unlock(&nn
->client_lock
);
1343 idr_init(&clp
->cl_stateids
);
1344 atomic_set(&clp
->cl_refcount
, 0);
1345 clp
->cl_cb_state
= NFSD4_CB_UNKNOWN
;
1346 INIT_LIST_HEAD(&clp
->cl_idhash
);
1347 INIT_LIST_HEAD(&clp
->cl_openowners
);
1348 INIT_LIST_HEAD(&clp
->cl_delegations
);
1349 INIT_LIST_HEAD(&clp
->cl_lru
);
1350 INIT_LIST_HEAD(&clp
->cl_callbacks
);
1351 INIT_LIST_HEAD(&clp
->cl_revoked
);
1352 spin_lock_init(&clp
->cl_lock
);
1353 nfsd4_init_callback(&clp
->cl_cb_null
);
1354 clp
->cl_time
= get_seconds();
1355 clear_bit(0, &clp
->cl_cb_slot_busy
);
1356 rpc_init_wait_queue(&clp
->cl_cb_waitq
, "Backchannel slot table");
1357 copy_verf(clp
, verf
);
1358 rpc_copy_addr((struct sockaddr
*) &clp
->cl_addr
, sa
);
1360 clp
->cl_cb_session
= NULL
;
1366 add_clp_to_name_tree(struct nfs4_client
*new_clp
, struct rb_root
*root
)
1368 struct rb_node
**new = &(root
->rb_node
), *parent
= NULL
;
1369 struct nfs4_client
*clp
;
1372 clp
= rb_entry(*new, struct nfs4_client
, cl_namenode
);
1375 if (compare_blob(&clp
->cl_name
, &new_clp
->cl_name
) > 0)
1376 new = &((*new)->rb_left
);
1378 new = &((*new)->rb_right
);
1381 rb_link_node(&new_clp
->cl_namenode
, parent
, new);
1382 rb_insert_color(&new_clp
->cl_namenode
, root
);
1385 static struct nfs4_client
*
1386 find_clp_in_name_tree(struct xdr_netobj
*name
, struct rb_root
*root
)
1389 struct rb_node
*node
= root
->rb_node
;
1390 struct nfs4_client
*clp
;
1393 clp
= rb_entry(node
, struct nfs4_client
, cl_namenode
);
1394 cmp
= compare_blob(&clp
->cl_name
, name
);
1396 node
= node
->rb_left
;
1398 node
= node
->rb_right
;
1406 add_to_unconfirmed(struct nfs4_client
*clp
)
1408 unsigned int idhashval
;
1409 struct nfsd_net
*nn
= net_generic(clp
->net
, nfsd_net_id
);
1411 clear_bit(NFSD4_CLIENT_CONFIRMED
, &clp
->cl_flags
);
1412 add_clp_to_name_tree(clp
, &nn
->unconf_name_tree
);
1413 idhashval
= clientid_hashval(clp
->cl_clientid
.cl_id
);
1414 list_add(&clp
->cl_idhash
, &nn
->unconf_id_hashtbl
[idhashval
]);
1419 move_to_confirmed(struct nfs4_client
*clp
)
1421 unsigned int idhashval
= clientid_hashval(clp
->cl_clientid
.cl_id
);
1422 struct nfsd_net
*nn
= net_generic(clp
->net
, nfsd_net_id
);
1424 dprintk("NFSD: move_to_confirm nfs4_client %p\n", clp
);
1425 list_move(&clp
->cl_idhash
, &nn
->conf_id_hashtbl
[idhashval
]);
1426 rb_erase(&clp
->cl_namenode
, &nn
->unconf_name_tree
);
1427 add_clp_to_name_tree(clp
, &nn
->conf_name_tree
);
1428 set_bit(NFSD4_CLIENT_CONFIRMED
, &clp
->cl_flags
);
1432 static struct nfs4_client
*
1433 find_client_in_id_table(struct list_head
*tbl
, clientid_t
*clid
, bool sessions
)
1435 struct nfs4_client
*clp
;
1436 unsigned int idhashval
= clientid_hashval(clid
->cl_id
);
1438 list_for_each_entry(clp
, &tbl
[idhashval
], cl_idhash
) {
1439 if (same_clid(&clp
->cl_clientid
, clid
)) {
1440 if ((bool)clp
->cl_minorversion
!= sessions
)
1449 static struct nfs4_client
*
1450 find_confirmed_client(clientid_t
*clid
, bool sessions
, struct nfsd_net
*nn
)
1452 struct list_head
*tbl
= nn
->conf_id_hashtbl
;
1454 return find_client_in_id_table(tbl
, clid
, sessions
);
1457 static struct nfs4_client
*
1458 find_unconfirmed_client(clientid_t
*clid
, bool sessions
, struct nfsd_net
*nn
)
1460 struct list_head
*tbl
= nn
->unconf_id_hashtbl
;
1462 return find_client_in_id_table(tbl
, clid
, sessions
);
1465 static bool clp_used_exchangeid(struct nfs4_client
*clp
)
1467 return clp
->cl_exchange_flags
!= 0;
1470 static struct nfs4_client
*
1471 find_confirmed_client_by_name(struct xdr_netobj
*name
, struct nfsd_net
*nn
)
1473 return find_clp_in_name_tree(name
, &nn
->conf_name_tree
);
1476 static struct nfs4_client
*
1477 find_unconfirmed_client_by_name(struct xdr_netobj
*name
, struct nfsd_net
*nn
)
1479 return find_clp_in_name_tree(name
, &nn
->unconf_name_tree
);
1483 gen_callback(struct nfs4_client
*clp
, struct nfsd4_setclientid
*se
, struct svc_rqst
*rqstp
)
1485 struct nfs4_cb_conn
*conn
= &clp
->cl_cb_conn
;
1486 struct sockaddr
*sa
= svc_addr(rqstp
);
1487 u32 scopeid
= rpc_get_scope_id(sa
);
1488 unsigned short expected_family
;
1490 /* Currently, we only support tcp and tcp6 for the callback channel */
1491 if (se
->se_callback_netid_len
== 3 &&
1492 !memcmp(se
->se_callback_netid_val
, "tcp", 3))
1493 expected_family
= AF_INET
;
1494 else if (se
->se_callback_netid_len
== 4 &&
1495 !memcmp(se
->se_callback_netid_val
, "tcp6", 4))
1496 expected_family
= AF_INET6
;
1500 conn
->cb_addrlen
= rpc_uaddr2sockaddr(clp
->net
, se
->se_callback_addr_val
,
1501 se
->se_callback_addr_len
,
1502 (struct sockaddr
*)&conn
->cb_addr
,
1503 sizeof(conn
->cb_addr
));
1505 if (!conn
->cb_addrlen
|| conn
->cb_addr
.ss_family
!= expected_family
)
1508 if (conn
->cb_addr
.ss_family
== AF_INET6
)
1509 ((struct sockaddr_in6
*)&conn
->cb_addr
)->sin6_scope_id
= scopeid
;
1511 conn
->cb_prog
= se
->se_callback_prog
;
1512 conn
->cb_ident
= se
->se_callback_ident
;
1513 memcpy(&conn
->cb_saddr
, &rqstp
->rq_daddr
, rqstp
->rq_daddrlen
);
1516 conn
->cb_addr
.ss_family
= AF_UNSPEC
;
1517 conn
->cb_addrlen
= 0;
1518 dprintk(KERN_INFO
"NFSD: this client (clientid %08x/%08x) "
1519 "will not receive delegations\n",
1520 clp
->cl_clientid
.cl_boot
, clp
->cl_clientid
.cl_id
);
1526 * Cache a reply. nfsd4_check_drc_limit() has bounded the cache size.
1529 nfsd4_store_cache_entry(struct nfsd4_compoundres
*resp
)
1531 struct nfsd4_slot
*slot
= resp
->cstate
.slot
;
1534 dprintk("--> %s slot %p\n", __func__
, slot
);
1536 slot
->sl_opcnt
= resp
->opcnt
;
1537 slot
->sl_status
= resp
->cstate
.status
;
1539 slot
->sl_flags
|= NFSD4_SLOT_INITIALIZED
;
1540 if (nfsd4_not_cached(resp
)) {
1541 slot
->sl_datalen
= 0;
1544 slot
->sl_datalen
= (char *)resp
->p
- (char *)resp
->cstate
.datap
;
1545 base
= (char *)resp
->cstate
.datap
-
1546 (char *)resp
->xbuf
->head
[0].iov_base
;
1547 if (read_bytes_from_xdr_buf(resp
->xbuf
, base
, slot
->sl_data
,
1549 WARN("%s: sessions DRC could not cache compound\n", __func__
);
1554 * Encode the replay sequence operation from the slot values.
1555 * If cachethis is FALSE encode the uncached rep error on the next
1556 * operation which sets resp->p and increments resp->opcnt for
1557 * nfs4svc_encode_compoundres.
1561 nfsd4_enc_sequence_replay(struct nfsd4_compoundargs
*args
,
1562 struct nfsd4_compoundres
*resp
)
1564 struct nfsd4_op
*op
;
1565 struct nfsd4_slot
*slot
= resp
->cstate
.slot
;
1567 /* Encode the replayed sequence operation */
1568 op
= &args
->ops
[resp
->opcnt
- 1];
1569 nfsd4_encode_operation(resp
, op
);
1571 /* Return nfserr_retry_uncached_rep in next operation. */
1572 if (args
->opcnt
> 1 && !(slot
->sl_flags
& NFSD4_SLOT_CACHETHIS
)) {
1573 op
= &args
->ops
[resp
->opcnt
++];
1574 op
->status
= nfserr_retry_uncached_rep
;
1575 nfsd4_encode_operation(resp
, op
);
1581 * The sequence operation is not cached because we can use the slot and
1585 nfsd4_replay_cache_entry(struct nfsd4_compoundres
*resp
,
1586 struct nfsd4_sequence
*seq
)
1588 struct nfsd4_slot
*slot
= resp
->cstate
.slot
;
1591 dprintk("--> %s slot %p\n", __func__
, slot
);
1593 /* Either returns 0 or nfserr_retry_uncached */
1594 status
= nfsd4_enc_sequence_replay(resp
->rqstp
->rq_argp
, resp
);
1595 if (status
== nfserr_retry_uncached_rep
)
1598 /* The sequence operation has been encoded, cstate->datap set. */
1599 memcpy(resp
->cstate
.datap
, slot
->sl_data
, slot
->sl_datalen
);
1601 resp
->opcnt
= slot
->sl_opcnt
;
1602 resp
->p
= resp
->cstate
.datap
+ XDR_QUADLEN(slot
->sl_datalen
);
1603 status
= slot
->sl_status
;
1609 * Set the exchange_id flags returned by the server.
1612 nfsd4_set_ex_flags(struct nfs4_client
*new, struct nfsd4_exchange_id
*clid
)
1614 /* pNFS is not supported */
1615 new->cl_exchange_flags
|= EXCHGID4_FLAG_USE_NON_PNFS
;
1617 /* Referrals are supported, Migration is not. */
1618 new->cl_exchange_flags
|= EXCHGID4_FLAG_SUPP_MOVED_REFER
;
1620 /* set the wire flags to return to client. */
1621 clid
->flags
= new->cl_exchange_flags
;
1624 static bool client_has_state(struct nfs4_client
*clp
)
1627 * Note clp->cl_openowners check isn't quite right: there's no
1628 * need to count owners without stateid's.
1630 * Also note we should probably be using this in 4.0 case too.
1632 return !list_empty(&clp
->cl_openowners
)
1633 || !list_empty(&clp
->cl_delegations
)
1634 || !list_empty(&clp
->cl_sessions
);
1638 nfsd4_exchange_id(struct svc_rqst
*rqstp
,
1639 struct nfsd4_compound_state
*cstate
,
1640 struct nfsd4_exchange_id
*exid
)
1642 struct nfs4_client
*unconf
, *conf
, *new;
1644 char addr_str
[INET6_ADDRSTRLEN
];
1645 nfs4_verifier verf
= exid
->verifier
;
1646 struct sockaddr
*sa
= svc_addr(rqstp
);
1647 bool update
= exid
->flags
& EXCHGID4_FLAG_UPD_CONFIRMED_REC_A
;
1648 struct nfsd_net
*nn
= net_generic(SVC_NET(rqstp
), nfsd_net_id
);
1650 rpc_ntop(sa
, addr_str
, sizeof(addr_str
));
1651 dprintk("%s rqstp=%p exid=%p clname.len=%u clname.data=%p "
1652 "ip_addr=%s flags %x, spa_how %d\n",
1653 __func__
, rqstp
, exid
, exid
->clname
.len
, exid
->clname
.data
,
1654 addr_str
, exid
->flags
, exid
->spa_how
);
1656 if (exid
->flags
& ~EXCHGID4_FLAG_MASK_A
)
1657 return nfserr_inval
;
1659 switch (exid
->spa_how
) {
1661 if (!svc_rqst_integrity_protected(rqstp
))
1662 return nfserr_inval
;
1665 default: /* checked by xdr code */
1668 return nfserr_encr_alg_unsupp
;
1671 /* Cases below refer to rfc 5661 section 18.35.4: */
1673 conf
= find_confirmed_client_by_name(&exid
->clname
, nn
);
1675 bool creds_match
= same_creds(&conf
->cl_cred
, &rqstp
->rq_cred
);
1676 bool verfs_match
= same_verf(&verf
, &conf
->cl_verifier
);
1679 if (!clp_used_exchangeid(conf
)) { /* buggy client */
1680 status
= nfserr_inval
;
1683 if (!mach_creds_match(conf
, rqstp
)) {
1684 status
= nfserr_wrong_cred
;
1687 if (!creds_match
) { /* case 9 */
1688 status
= nfserr_perm
;
1691 if (!verfs_match
) { /* case 8 */
1692 status
= nfserr_not_same
;
1696 exid
->flags
|= EXCHGID4_FLAG_CONFIRMED_R
;
1700 if (!creds_match
) { /* case 3 */
1701 if (client_has_state(conf
)) {
1702 status
= nfserr_clid_inuse
;
1705 expire_client(conf
);
1708 if (verfs_match
) { /* case 2 */
1709 conf
->cl_exchange_flags
|= EXCHGID4_FLAG_CONFIRMED_R
;
1713 /* case 5, client reboot */
1717 if (update
) { /* case 7 */
1718 status
= nfserr_noent
;
1722 unconf
= find_unconfirmed_client_by_name(&exid
->clname
, nn
);
1723 if (unconf
) /* case 4, possible retry or client restart */
1724 expire_client(unconf
);
1726 /* case 1 (normal case) */
1728 new = create_client(exid
->clname
, rqstp
, &verf
);
1730 status
= nfserr_jukebox
;
1733 new->cl_minorversion
= cstate
->minorversion
;
1734 new->cl_mach_cred
= (exid
->spa_how
== SP4_MACH_CRED
);
1737 add_to_unconfirmed(new);
1739 exid
->clientid
.cl_boot
= new->cl_clientid
.cl_boot
;
1740 exid
->clientid
.cl_id
= new->cl_clientid
.cl_id
;
1742 exid
->seqid
= new->cl_cs_slot
.sl_seqid
+ 1;
1743 nfsd4_set_ex_flags(new, exid
);
1745 dprintk("nfsd4_exchange_id seqid %d flags %x\n",
1746 new->cl_cs_slot
.sl_seqid
, new->cl_exchange_flags
);
1750 nfs4_unlock_state();
1755 check_slot_seqid(u32 seqid
, u32 slot_seqid
, int slot_inuse
)
1757 dprintk("%s enter. seqid %d slot_seqid %d\n", __func__
, seqid
,
1760 /* The slot is in use, and no response has been sent. */
1762 if (seqid
== slot_seqid
)
1763 return nfserr_jukebox
;
1765 return nfserr_seq_misordered
;
1767 /* Note unsigned 32-bit arithmetic handles wraparound: */
1768 if (likely(seqid
== slot_seqid
+ 1))
1770 if (seqid
== slot_seqid
)
1771 return nfserr_replay_cache
;
1772 return nfserr_seq_misordered
;
1776 * Cache the create session result into the create session single DRC
1777 * slot cache by saving the xdr structure. sl_seqid has been set.
1778 * Do this for solo or embedded create session operations.
1781 nfsd4_cache_create_session(struct nfsd4_create_session
*cr_ses
,
1782 struct nfsd4_clid_slot
*slot
, __be32 nfserr
)
1784 slot
->sl_status
= nfserr
;
1785 memcpy(&slot
->sl_cr_ses
, cr_ses
, sizeof(*cr_ses
));
1789 nfsd4_replay_create_session(struct nfsd4_create_session
*cr_ses
,
1790 struct nfsd4_clid_slot
*slot
)
1792 memcpy(cr_ses
, &slot
->sl_cr_ses
, sizeof(*cr_ses
));
1793 return slot
->sl_status
;
1796 #define NFSD_MIN_REQ_HDR_SEQ_SZ ((\
1797 2 * 2 + /* credential,verifier: AUTH_NULL, length 0 */ \
1798 1 + /* MIN tag is length with zero, only length */ \
1799 3 + /* version, opcount, opcode */ \
1800 XDR_QUADLEN(NFS4_MAX_SESSIONID_LEN) + \
1801 /* seqid, slotID, slotID, cache */ \
1802 4 ) * sizeof(__be32))
1804 #define NFSD_MIN_RESP_HDR_SEQ_SZ ((\
1805 2 + /* verifier: AUTH_NULL, length 0 */\
1807 1 + /* MIN tag is length with zero, only length */ \
1808 3 + /* opcount, opcode, opstatus*/ \
1809 XDR_QUADLEN(NFS4_MAX_SESSIONID_LEN) + \
1810 /* seqid, slotID, slotID, slotID, status */ \
1811 5 ) * sizeof(__be32))
1813 static __be32
check_forechannel_attrs(struct nfsd4_channel_attrs
*ca
, struct nfsd_net
*nn
)
1815 u32 maxrpc
= nn
->nfsd_serv
->sv_max_mesg
;
1817 if (ca
->maxreq_sz
< NFSD_MIN_REQ_HDR_SEQ_SZ
)
1818 return nfserr_toosmall
;
1819 if (ca
->maxresp_sz
< NFSD_MIN_RESP_HDR_SEQ_SZ
)
1820 return nfserr_toosmall
;
1821 ca
->headerpadsz
= 0;
1822 ca
->maxreq_sz
= min_t(u32
, ca
->maxreq_sz
, maxrpc
);
1823 ca
->maxresp_sz
= min_t(u32
, ca
->maxresp_sz
, maxrpc
);
1824 ca
->maxops
= min_t(u32
, ca
->maxops
, NFSD_MAX_OPS_PER_COMPOUND
);
1825 ca
->maxresp_cached
= min_t(u32
, ca
->maxresp_cached
,
1826 NFSD_SLOT_CACHE_SIZE
+ NFSD_MIN_HDR_SEQ_SZ
);
1827 ca
->maxreqs
= min_t(u32
, ca
->maxreqs
, NFSD_MAX_SLOTS_PER_SESSION
);
1829 * Note decreasing slot size below client's request may make it
1830 * difficult for client to function correctly, whereas
1831 * decreasing the number of slots will (just?) affect
1832 * performance. When short on memory we therefore prefer to
1833 * decrease number of slots instead of their size. Clients that
1834 * request larger slots than they need will get poor results:
1836 ca
->maxreqs
= nfsd4_get_drc_mem(ca
);
1838 return nfserr_jukebox
;
1843 static __be32
check_backchannel_attrs(struct nfsd4_channel_attrs
*ca
)
1845 ca
->headerpadsz
= 0;
1848 * These RPC_MAX_HEADER macros are overkill, especially since we
1849 * don't even do gss on the backchannel yet. But this is still
1850 * less than 1k. Tighten up this estimate in the unlikely event
1851 * it turns out to be a problem for some client:
1853 if (ca
->maxreq_sz
< NFS4_enc_cb_recall_sz
+ RPC_MAX_HEADER_WITH_AUTH
)
1854 return nfserr_toosmall
;
1855 if (ca
->maxresp_sz
< NFS4_dec_cb_recall_sz
+ RPC_MAX_REPHEADER_WITH_AUTH
)
1856 return nfserr_toosmall
;
1857 ca
->maxresp_cached
= 0;
1859 return nfserr_toosmall
;
1864 static __be32
nfsd4_check_cb_sec(struct nfsd4_cb_sec
*cbs
)
1866 switch (cbs
->flavor
) {
1872 * GSS case: the spec doesn't allow us to return this
1873 * error. But it also doesn't allow us not to support
1875 * I'd rather this fail hard than return some error the
1876 * client might think it can already handle:
1878 return nfserr_encr_alg_unsupp
;
1883 nfsd4_create_session(struct svc_rqst
*rqstp
,
1884 struct nfsd4_compound_state
*cstate
,
1885 struct nfsd4_create_session
*cr_ses
)
1887 struct sockaddr
*sa
= svc_addr(rqstp
);
1888 struct nfs4_client
*conf
, *unconf
;
1889 struct nfsd4_session
*new;
1890 struct nfsd4_conn
*conn
;
1891 struct nfsd4_clid_slot
*cs_slot
= NULL
;
1893 struct nfsd_net
*nn
= net_generic(SVC_NET(rqstp
), nfsd_net_id
);
1895 if (cr_ses
->flags
& ~SESSION4_FLAG_MASK_A
)
1896 return nfserr_inval
;
1897 status
= nfsd4_check_cb_sec(&cr_ses
->cb_sec
);
1900 status
= check_forechannel_attrs(&cr_ses
->fore_channel
, nn
);
1903 status
= check_backchannel_attrs(&cr_ses
->back_channel
);
1906 status
= nfserr_jukebox
;
1907 new = alloc_session(&cr_ses
->fore_channel
);
1909 goto out_release_drc_mem
;
1910 conn
= alloc_conn_from_crses(rqstp
, cr_ses
);
1912 goto out_free_session
;
1915 unconf
= find_unconfirmed_client(&cr_ses
->clientid
, true, nn
);
1916 conf
= find_confirmed_client(&cr_ses
->clientid
, true, nn
);
1917 WARN_ON_ONCE(conf
&& unconf
);
1920 status
= nfserr_wrong_cred
;
1921 if (!mach_creds_match(conf
, rqstp
))
1923 cs_slot
= &conf
->cl_cs_slot
;
1924 status
= check_slot_seqid(cr_ses
->seqid
, cs_slot
->sl_seqid
, 0);
1925 if (status
== nfserr_replay_cache
) {
1926 status
= nfsd4_replay_create_session(cr_ses
, cs_slot
);
1928 } else if (cr_ses
->seqid
!= cs_slot
->sl_seqid
+ 1) {
1929 status
= nfserr_seq_misordered
;
1932 } else if (unconf
) {
1933 struct nfs4_client
*old
;
1934 if (!same_creds(&unconf
->cl_cred
, &rqstp
->rq_cred
) ||
1935 !rpc_cmp_addr(sa
, (struct sockaddr
*) &unconf
->cl_addr
)) {
1936 status
= nfserr_clid_inuse
;
1939 status
= nfserr_wrong_cred
;
1940 if (!mach_creds_match(unconf
, rqstp
))
1942 cs_slot
= &unconf
->cl_cs_slot
;
1943 status
= check_slot_seqid(cr_ses
->seqid
, cs_slot
->sl_seqid
, 0);
1945 /* an unconfirmed replay returns misordered */
1946 status
= nfserr_seq_misordered
;
1949 old
= find_confirmed_client_by_name(&unconf
->cl_name
, nn
);
1951 status
= mark_client_expired(old
);
1956 move_to_confirmed(unconf
);
1959 status
= nfserr_stale_clientid
;
1964 * We do not support RDMA or persistent sessions
1966 cr_ses
->flags
&= ~SESSION4_PERSIST
;
1967 cr_ses
->flags
&= ~SESSION4_RDMA
;
1969 init_session(rqstp
, new, conf
, cr_ses
);
1970 nfsd4_init_conn(rqstp
, conn
, new);
1972 memcpy(cr_ses
->sessionid
.data
, new->se_sessionid
.data
,
1973 NFS4_MAX_SESSIONID_LEN
);
1974 cs_slot
->sl_seqid
++;
1975 cr_ses
->seqid
= cs_slot
->sl_seqid
;
1977 /* cache solo and embedded create sessions under the state lock */
1978 nfsd4_cache_create_session(cr_ses
, cs_slot
, status
);
1979 nfs4_unlock_state();
1982 nfs4_unlock_state();
1985 __free_session(new);
1986 out_release_drc_mem
:
1987 nfsd4_put_drc_mem(&cr_ses
->fore_channel
);
1991 static __be32
nfsd4_map_bcts_dir(u32
*dir
)
1994 case NFS4_CDFC4_FORE
:
1995 case NFS4_CDFC4_BACK
:
1997 case NFS4_CDFC4_FORE_OR_BOTH
:
1998 case NFS4_CDFC4_BACK_OR_BOTH
:
1999 *dir
= NFS4_CDFC4_BOTH
;
2002 return nfserr_inval
;
2005 __be32
nfsd4_backchannel_ctl(struct svc_rqst
*rqstp
, struct nfsd4_compound_state
*cstate
, struct nfsd4_backchannel_ctl
*bc
)
2007 struct nfsd4_session
*session
= cstate
->session
;
2008 struct nfsd_net
*nn
= net_generic(SVC_NET(rqstp
), nfsd_net_id
);
2011 status
= nfsd4_check_cb_sec(&bc
->bc_cb_sec
);
2014 spin_lock(&nn
->client_lock
);
2015 session
->se_cb_prog
= bc
->bc_cb_program
;
2016 session
->se_cb_sec
= bc
->bc_cb_sec
;
2017 spin_unlock(&nn
->client_lock
);
2019 nfsd4_probe_callback(session
->se_client
);
2024 __be32
nfsd4_bind_conn_to_session(struct svc_rqst
*rqstp
,
2025 struct nfsd4_compound_state
*cstate
,
2026 struct nfsd4_bind_conn_to_session
*bcts
)
2029 struct nfsd4_conn
*conn
;
2030 struct nfsd4_session
*session
;
2031 struct nfsd_net
*nn
= net_generic(SVC_NET(rqstp
), nfsd_net_id
);
2033 if (!nfsd4_last_compound_op(rqstp
))
2034 return nfserr_not_only_op
;
2036 spin_lock(&nn
->client_lock
);
2037 session
= find_in_sessionid_hashtbl(&bcts
->sessionid
, SVC_NET(rqstp
));
2038 spin_unlock(&nn
->client_lock
);
2039 status
= nfserr_badsession
;
2042 status
= nfserr_wrong_cred
;
2043 if (!mach_creds_match(session
->se_client
, rqstp
))
2045 status
= nfsd4_map_bcts_dir(&bcts
->dir
);
2048 conn
= alloc_conn(rqstp
, bcts
->dir
);
2049 status
= nfserr_jukebox
;
2052 nfsd4_init_conn(rqstp
, conn
, session
);
2055 nfs4_unlock_state();
2059 static bool nfsd4_compound_in_session(struct nfsd4_session
*session
, struct nfs4_sessionid
*sid
)
2063 return !memcmp(sid
, &session
->se_sessionid
, sizeof(*sid
));
2067 nfsd4_destroy_session(struct svc_rqst
*r
,
2068 struct nfsd4_compound_state
*cstate
,
2069 struct nfsd4_destroy_session
*sessionid
)
2071 struct nfsd4_session
*ses
;
2073 int ref_held_by_me
= 0;
2074 struct nfsd_net
*nn
= net_generic(SVC_NET(r
), nfsd_net_id
);
2077 status
= nfserr_not_only_op
;
2078 if (nfsd4_compound_in_session(cstate
->session
, &sessionid
->sessionid
)) {
2079 if (!nfsd4_last_compound_op(r
))
2083 dump_sessionid(__func__
, &sessionid
->sessionid
);
2084 spin_lock(&nn
->client_lock
);
2085 ses
= find_in_sessionid_hashtbl(&sessionid
->sessionid
, SVC_NET(r
));
2086 status
= nfserr_badsession
;
2088 goto out_client_lock
;
2089 status
= nfserr_wrong_cred
;
2090 if (!mach_creds_match(ses
->se_client
, r
))
2091 goto out_client_lock
;
2092 nfsd4_get_session_locked(ses
);
2093 status
= mark_session_dead_locked(ses
, 1 + ref_held_by_me
);
2095 goto out_put_session
;
2096 unhash_session(ses
);
2097 spin_unlock(&nn
->client_lock
);
2099 nfsd4_probe_callback_sync(ses
->se_client
);
2101 spin_lock(&nn
->client_lock
);
2104 nfsd4_put_session(ses
);
2106 spin_unlock(&nn
->client_lock
);
2108 nfs4_unlock_state();
2112 static struct nfsd4_conn
*__nfsd4_find_conn(struct svc_xprt
*xpt
, struct nfsd4_session
*s
)
2114 struct nfsd4_conn
*c
;
2116 list_for_each_entry(c
, &s
->se_conns
, cn_persession
) {
2117 if (c
->cn_xprt
== xpt
) {
2124 static __be32
nfsd4_sequence_check_conn(struct nfsd4_conn
*new, struct nfsd4_session
*ses
)
2126 struct nfs4_client
*clp
= ses
->se_client
;
2127 struct nfsd4_conn
*c
;
2128 __be32 status
= nfs_ok
;
2131 spin_lock(&clp
->cl_lock
);
2132 c
= __nfsd4_find_conn(new->cn_xprt
, ses
);
2135 status
= nfserr_conn_not_bound_to_session
;
2136 if (clp
->cl_mach_cred
)
2138 __nfsd4_hash_conn(new, ses
);
2139 spin_unlock(&clp
->cl_lock
);
2140 ret
= nfsd4_register_conn(new);
2142 /* oops; xprt is already down: */
2143 nfsd4_conn_lost(&new->cn_xpt_user
);
2146 spin_unlock(&clp
->cl_lock
);
2151 static bool nfsd4_session_too_many_ops(struct svc_rqst
*rqstp
, struct nfsd4_session
*session
)
2153 struct nfsd4_compoundargs
*args
= rqstp
->rq_argp
;
2155 return args
->opcnt
> session
->se_fchannel
.maxops
;
2158 static bool nfsd4_request_too_big(struct svc_rqst
*rqstp
,
2159 struct nfsd4_session
*session
)
2161 struct xdr_buf
*xb
= &rqstp
->rq_arg
;
2163 return xb
->len
> session
->se_fchannel
.maxreq_sz
;
2167 nfsd4_sequence(struct svc_rqst
*rqstp
,
2168 struct nfsd4_compound_state
*cstate
,
2169 struct nfsd4_sequence
*seq
)
2171 struct nfsd4_compoundres
*resp
= rqstp
->rq_resp
;
2172 struct nfsd4_session
*session
;
2173 struct nfs4_client
*clp
;
2174 struct nfsd4_slot
*slot
;
2175 struct nfsd4_conn
*conn
;
2177 struct nfsd_net
*nn
= net_generic(SVC_NET(rqstp
), nfsd_net_id
);
2179 if (resp
->opcnt
!= 1)
2180 return nfserr_sequence_pos
;
2183 * Will be either used or freed by nfsd4_sequence_check_conn
2186 conn
= alloc_conn(rqstp
, NFS4_CDFC4_FORE
);
2188 return nfserr_jukebox
;
2190 spin_lock(&nn
->client_lock
);
2191 status
= nfserr_badsession
;
2192 session
= find_in_sessionid_hashtbl(&seq
->sessionid
, SVC_NET(rqstp
));
2194 goto out_no_session
;
2195 clp
= session
->se_client
;
2196 status
= get_client_locked(clp
);
2198 goto out_no_session
;
2199 status
= nfsd4_get_session_locked(session
);
2201 goto out_put_client
;
2203 status
= nfserr_too_many_ops
;
2204 if (nfsd4_session_too_many_ops(rqstp
, session
))
2205 goto out_put_session
;
2207 status
= nfserr_req_too_big
;
2208 if (nfsd4_request_too_big(rqstp
, session
))
2209 goto out_put_session
;
2211 status
= nfserr_badslot
;
2212 if (seq
->slotid
>= session
->se_fchannel
.maxreqs
)
2213 goto out_put_session
;
2215 slot
= session
->se_slots
[seq
->slotid
];
2216 dprintk("%s: slotid %d\n", __func__
, seq
->slotid
);
2218 /* We do not negotiate the number of slots yet, so set the
2219 * maxslots to the session maxreqs which is used to encode
2220 * sr_highest_slotid and the sr_target_slot id to maxslots */
2221 seq
->maxslots
= session
->se_fchannel
.maxreqs
;
2223 status
= check_slot_seqid(seq
->seqid
, slot
->sl_seqid
,
2224 slot
->sl_flags
& NFSD4_SLOT_INUSE
);
2225 if (status
== nfserr_replay_cache
) {
2226 status
= nfserr_seq_misordered
;
2227 if (!(slot
->sl_flags
& NFSD4_SLOT_INITIALIZED
))
2228 goto out_put_session
;
2229 cstate
->slot
= slot
;
2230 cstate
->session
= session
;
2231 /* Return the cached reply status and set cstate->status
2232 * for nfsd4_proc_compound processing */
2233 status
= nfsd4_replay_cache_entry(resp
, seq
);
2234 cstate
->status
= nfserr_replay_cache
;
2238 goto out_put_session
;
2240 status
= nfsd4_sequence_check_conn(conn
, session
);
2243 goto out_put_session
;
2245 /* Success! bump slot seqid */
2246 slot
->sl_seqid
= seq
->seqid
;
2247 slot
->sl_flags
|= NFSD4_SLOT_INUSE
;
2249 slot
->sl_flags
|= NFSD4_SLOT_CACHETHIS
;
2251 slot
->sl_flags
&= ~NFSD4_SLOT_CACHETHIS
;
2253 cstate
->slot
= slot
;
2254 cstate
->session
= session
;
2257 switch (clp
->cl_cb_state
) {
2259 seq
->status_flags
= SEQ4_STATUS_CB_PATH_DOWN
;
2261 case NFSD4_CB_FAULT
:
2262 seq
->status_flags
= SEQ4_STATUS_BACKCHANNEL_FAULT
;
2265 seq
->status_flags
= 0;
2267 if (!list_empty(&clp
->cl_revoked
))
2268 seq
->status_flags
|= SEQ4_STATUS_RECALLABLE_STATE_REVOKED
;
2271 spin_unlock(&nn
->client_lock
);
2274 nfsd4_put_session(session
);
2276 put_client_renew_locked(clp
);
2277 goto out_no_session
;
2281 nfsd4_destroy_clientid(struct svc_rqst
*rqstp
, struct nfsd4_compound_state
*cstate
, struct nfsd4_destroy_clientid
*dc
)
2283 struct nfs4_client
*conf
, *unconf
, *clp
;
2285 struct nfsd_net
*nn
= net_generic(SVC_NET(rqstp
), nfsd_net_id
);
2288 unconf
= find_unconfirmed_client(&dc
->clientid
, true, nn
);
2289 conf
= find_confirmed_client(&dc
->clientid
, true, nn
);
2290 WARN_ON_ONCE(conf
&& unconf
);
2295 if (client_has_state(conf
)) {
2296 status
= nfserr_clientid_busy
;
2302 status
= nfserr_stale_clientid
;
2305 if (!mach_creds_match(clp
, rqstp
)) {
2306 status
= nfserr_wrong_cred
;
2311 nfs4_unlock_state();
2316 nfsd4_reclaim_complete(struct svc_rqst
*rqstp
, struct nfsd4_compound_state
*cstate
, struct nfsd4_reclaim_complete
*rc
)
2320 if (rc
->rca_one_fs
) {
2321 if (!cstate
->current_fh
.fh_dentry
)
2322 return nfserr_nofilehandle
;
2324 * We don't take advantage of the rca_one_fs case.
2325 * That's OK, it's optional, we can safely ignore it.
2331 status
= nfserr_complete_already
;
2332 if (test_and_set_bit(NFSD4_CLIENT_RECLAIM_COMPLETE
,
2333 &cstate
->session
->se_client
->cl_flags
))
2336 status
= nfserr_stale_clientid
;
2337 if (is_client_expired(cstate
->session
->se_client
))
2339 * The following error isn't really legal.
2340 * But we only get here if the client just explicitly
2341 * destroyed the client. Surely it no longer cares what
2342 * error it gets back on an operation for the dead
2348 nfsd4_client_record_create(cstate
->session
->se_client
);
2350 nfs4_unlock_state();
2355 nfsd4_setclientid(struct svc_rqst
*rqstp
, struct nfsd4_compound_state
*cstate
,
2356 struct nfsd4_setclientid
*setclid
)
2358 struct xdr_netobj clname
= setclid
->se_name
;
2359 nfs4_verifier clverifier
= setclid
->se_verf
;
2360 struct nfs4_client
*conf
, *unconf
, *new;
2362 struct nfsd_net
*nn
= net_generic(SVC_NET(rqstp
), nfsd_net_id
);
2364 /* Cases below refer to rfc 3530 section 14.2.33: */
2366 conf
= find_confirmed_client_by_name(&clname
, nn
);
2369 status
= nfserr_clid_inuse
;
2370 if (clp_used_exchangeid(conf
))
2372 if (!same_creds(&conf
->cl_cred
, &rqstp
->rq_cred
)) {
2373 char addr_str
[INET6_ADDRSTRLEN
];
2374 rpc_ntop((struct sockaddr
*) &conf
->cl_addr
, addr_str
,
2376 dprintk("NFSD: setclientid: string in use by client "
2377 "at %s\n", addr_str
);
2381 unconf
= find_unconfirmed_client_by_name(&clname
, nn
);
2383 expire_client(unconf
);
2384 status
= nfserr_jukebox
;
2385 new = create_client(clname
, rqstp
, &clverifier
);
2388 if (conf
&& same_verf(&conf
->cl_verifier
, &clverifier
))
2389 /* case 1: probable callback update */
2390 copy_clid(new, conf
);
2391 else /* case 4 (new client) or cases 2, 3 (client reboot): */
2393 new->cl_minorversion
= 0;
2394 gen_callback(new, setclid
, rqstp
);
2395 add_to_unconfirmed(new);
2396 setclid
->se_clientid
.cl_boot
= new->cl_clientid
.cl_boot
;
2397 setclid
->se_clientid
.cl_id
= new->cl_clientid
.cl_id
;
2398 memcpy(setclid
->se_confirm
.data
, new->cl_confirm
.data
, sizeof(setclid
->se_confirm
.data
));
2401 nfs4_unlock_state();
2407 nfsd4_setclientid_confirm(struct svc_rqst
*rqstp
,
2408 struct nfsd4_compound_state
*cstate
,
2409 struct nfsd4_setclientid_confirm
*setclientid_confirm
)
2411 struct nfs4_client
*conf
, *unconf
;
2412 nfs4_verifier confirm
= setclientid_confirm
->sc_confirm
;
2413 clientid_t
* clid
= &setclientid_confirm
->sc_clientid
;
2415 struct nfsd_net
*nn
= net_generic(SVC_NET(rqstp
), nfsd_net_id
);
2417 if (STALE_CLIENTID(clid
, nn
))
2418 return nfserr_stale_clientid
;
2421 conf
= find_confirmed_client(clid
, false, nn
);
2422 unconf
= find_unconfirmed_client(clid
, false, nn
);
2424 * We try hard to give out unique clientid's, so if we get an
2425 * attempt to confirm the same clientid with a different cred,
2426 * there's a bug somewhere. Let's charitably assume it's our
2429 status
= nfserr_serverfault
;
2430 if (unconf
&& !same_creds(&unconf
->cl_cred
, &rqstp
->rq_cred
))
2432 if (conf
&& !same_creds(&conf
->cl_cred
, &rqstp
->rq_cred
))
2434 /* cases below refer to rfc 3530 section 14.2.34: */
2435 if (!unconf
|| !same_verf(&confirm
, &unconf
->cl_confirm
)) {
2436 if (conf
&& !unconf
) /* case 2: probable retransmit */
2438 else /* case 4: client hasn't noticed we rebooted yet? */
2439 status
= nfserr_stale_clientid
;
2443 if (conf
) { /* case 1: callback update */
2444 nfsd4_change_callback(conf
, &unconf
->cl_cb_conn
);
2445 nfsd4_probe_callback(conf
);
2446 expire_client(unconf
);
2447 } else { /* case 3: normal case; new or rebooted client */
2448 conf
= find_confirmed_client_by_name(&unconf
->cl_name
, nn
);
2450 status
= mark_client_expired(conf
);
2453 expire_client(conf
);
2455 move_to_confirmed(unconf
);
2456 nfsd4_probe_callback(unconf
);
2459 nfs4_unlock_state();
2463 static struct nfs4_file
*nfsd4_alloc_file(void)
2465 return kmem_cache_alloc(file_slab
, GFP_KERNEL
);
2468 /* OPEN Share state helper functions */
2469 static void nfsd4_init_file(struct nfs4_file
*fp
, struct inode
*ino
)
2471 unsigned int hashval
= file_hashval(ino
);
2473 atomic_set(&fp
->fi_ref
, 1);
2474 INIT_LIST_HEAD(&fp
->fi_stateids
);
2475 INIT_LIST_HEAD(&fp
->fi_delegations
);
2476 fp
->fi_inode
= igrab(ino
);
2477 fp
->fi_had_conflict
= false;
2478 fp
->fi_lease
= NULL
;
2479 memset(fp
->fi_fds
, 0, sizeof(fp
->fi_fds
));
2480 memset(fp
->fi_access
, 0, sizeof(fp
->fi_access
));
2481 spin_lock(&recall_lock
);
2482 hlist_add_head(&fp
->fi_hash
, &file_hashtbl
[hashval
]);
2483 spin_unlock(&recall_lock
);
2487 nfsd4_free_slab(struct kmem_cache
**slab
)
2491 kmem_cache_destroy(*slab
);
2496 nfsd4_free_slabs(void)
2498 nfsd4_free_slab(&openowner_slab
);
2499 nfsd4_free_slab(&lockowner_slab
);
2500 nfsd4_free_slab(&file_slab
);
2501 nfsd4_free_slab(&stateid_slab
);
2502 nfsd4_free_slab(&deleg_slab
);
2506 nfsd4_init_slabs(void)
2508 openowner_slab
= kmem_cache_create("nfsd4_openowners",
2509 sizeof(struct nfs4_openowner
), 0, 0, NULL
);
2510 if (openowner_slab
== NULL
)
2512 lockowner_slab
= kmem_cache_create("nfsd4_lockowners",
2513 sizeof(struct nfs4_lockowner
), 0, 0, NULL
);
2514 if (lockowner_slab
== NULL
)
2516 file_slab
= kmem_cache_create("nfsd4_files",
2517 sizeof(struct nfs4_file
), 0, 0, NULL
);
2518 if (file_slab
== NULL
)
2520 stateid_slab
= kmem_cache_create("nfsd4_stateids",
2521 sizeof(struct nfs4_ol_stateid
), 0, 0, NULL
);
2522 if (stateid_slab
== NULL
)
2524 deleg_slab
= kmem_cache_create("nfsd4_delegations",
2525 sizeof(struct nfs4_delegation
), 0, 0, NULL
);
2526 if (deleg_slab
== NULL
)
2531 dprintk("nfsd4: out of memory while initializing nfsv4\n");
2535 void nfs4_free_openowner(struct nfs4_openowner
*oo
)
2537 kfree(oo
->oo_owner
.so_owner
.data
);
2538 kmem_cache_free(openowner_slab
, oo
);
2541 void nfs4_free_lockowner(struct nfs4_lockowner
*lo
)
2543 kfree(lo
->lo_owner
.so_owner
.data
);
2544 kmem_cache_free(lockowner_slab
, lo
);
2547 static void init_nfs4_replay(struct nfs4_replay
*rp
)
2549 rp
->rp_status
= nfserr_serverfault
;
2551 rp
->rp_buf
= rp
->rp_ibuf
;
2554 static inline void *alloc_stateowner(struct kmem_cache
*slab
, struct xdr_netobj
*owner
, struct nfs4_client
*clp
)
2556 struct nfs4_stateowner
*sop
;
2558 sop
= kmem_cache_alloc(slab
, GFP_KERNEL
);
2562 sop
->so_owner
.data
= kmemdup(owner
->data
, owner
->len
, GFP_KERNEL
);
2563 if (!sop
->so_owner
.data
) {
2564 kmem_cache_free(slab
, sop
);
2567 sop
->so_owner
.len
= owner
->len
;
2569 INIT_LIST_HEAD(&sop
->so_stateids
);
2570 sop
->so_client
= clp
;
2571 init_nfs4_replay(&sop
->so_replay
);
2575 static void hash_openowner(struct nfs4_openowner
*oo
, struct nfs4_client
*clp
, unsigned int strhashval
)
2577 struct nfsd_net
*nn
= net_generic(clp
->net
, nfsd_net_id
);
2579 list_add(&oo
->oo_owner
.so_strhash
, &nn
->ownerstr_hashtbl
[strhashval
]);
2580 list_add(&oo
->oo_perclient
, &clp
->cl_openowners
);
2583 static struct nfs4_openowner
*
2584 alloc_init_open_stateowner(unsigned int strhashval
, struct nfs4_client
*clp
, struct nfsd4_open
*open
) {
2585 struct nfs4_openowner
*oo
;
2587 oo
= alloc_stateowner(openowner_slab
, &open
->op_owner
, clp
);
2590 oo
->oo_owner
.so_is_open_owner
= 1;
2591 oo
->oo_owner
.so_seqid
= open
->op_seqid
;
2592 oo
->oo_flags
= NFS4_OO_NEW
;
2594 oo
->oo_last_closed_stid
= NULL
;
2595 INIT_LIST_HEAD(&oo
->oo_close_lru
);
2596 hash_openowner(oo
, clp
, strhashval
);
2600 static void init_open_stateid(struct nfs4_ol_stateid
*stp
, struct nfs4_file
*fp
, struct nfsd4_open
*open
) {
2601 struct nfs4_openowner
*oo
= open
->op_openowner
;
2603 stp
->st_stid
.sc_type
= NFS4_OPEN_STID
;
2604 INIT_LIST_HEAD(&stp
->st_lockowners
);
2605 list_add(&stp
->st_perstateowner
, &oo
->oo_owner
.so_stateids
);
2606 list_add(&stp
->st_perfile
, &fp
->fi_stateids
);
2607 stp
->st_stateowner
= &oo
->oo_owner
;
2610 stp
->st_access_bmap
= 0;
2611 stp
->st_deny_bmap
= 0;
2612 set_access(open
->op_share_access
, stp
);
2613 set_deny(open
->op_share_deny
, stp
);
2614 stp
->st_openstp
= NULL
;
2618 move_to_close_lru(struct nfs4_openowner
*oo
, struct net
*net
)
2620 struct nfsd_net
*nn
= net_generic(net
, nfsd_net_id
);
2622 dprintk("NFSD: move_to_close_lru nfs4_openowner %p\n", oo
);
2624 list_move_tail(&oo
->oo_close_lru
, &nn
->close_lru
);
2625 oo
->oo_time
= get_seconds();
2629 same_owner_str(struct nfs4_stateowner
*sop
, struct xdr_netobj
*owner
,
2632 return (sop
->so_owner
.len
== owner
->len
) &&
2633 0 == memcmp(sop
->so_owner
.data
, owner
->data
, owner
->len
) &&
2634 (sop
->so_client
->cl_clientid
.cl_id
== clid
->cl_id
);
2637 static struct nfs4_openowner
*
2638 find_openstateowner_str(unsigned int hashval
, struct nfsd4_open
*open
,
2639 bool sessions
, struct nfsd_net
*nn
)
2641 struct nfs4_stateowner
*so
;
2642 struct nfs4_openowner
*oo
;
2643 struct nfs4_client
*clp
;
2645 list_for_each_entry(so
, &nn
->ownerstr_hashtbl
[hashval
], so_strhash
) {
2646 if (!so
->so_is_open_owner
)
2648 if (same_owner_str(so
, &open
->op_owner
, &open
->op_clientid
)) {
2650 clp
= oo
->oo_owner
.so_client
;
2651 if ((bool)clp
->cl_minorversion
!= sessions
)
2653 renew_client(oo
->oo_owner
.so_client
);
2660 /* search file_hashtbl[] for file */
2661 static struct nfs4_file
*
2662 find_file(struct inode
*ino
)
2664 unsigned int hashval
= file_hashval(ino
);
2665 struct nfs4_file
*fp
;
2667 spin_lock(&recall_lock
);
2668 hlist_for_each_entry(fp
, &file_hashtbl
[hashval
], fi_hash
) {
2669 if (fp
->fi_inode
== ino
) {
2671 spin_unlock(&recall_lock
);
2675 spin_unlock(&recall_lock
);
2680 * Called to check deny when READ with all zero stateid or
2681 * WRITE with all zero or all one stateid
2684 nfs4_share_conflict(struct svc_fh
*current_fh
, unsigned int deny_type
)
2686 struct inode
*ino
= current_fh
->fh_dentry
->d_inode
;
2687 struct nfs4_file
*fp
;
2688 struct nfs4_ol_stateid
*stp
;
2691 fp
= find_file(ino
);
2694 ret
= nfserr_locked
;
2695 /* Search for conflicting share reservations */
2696 list_for_each_entry(stp
, &fp
->fi_stateids
, st_perfile
) {
2697 if (test_deny(deny_type
, stp
) ||
2698 test_deny(NFS4_SHARE_DENY_BOTH
, stp
))
2707 static void nfsd_break_one_deleg(struct nfs4_delegation
*dp
)
2709 struct nfs4_client
*clp
= dp
->dl_stid
.sc_client
;
2710 struct nfsd_net
*nn
= net_generic(clp
->net
, nfsd_net_id
);
2712 /* We're assuming the state code never drops its reference
2713 * without first removing the lease. Since we're in this lease
2714 * callback (and since the lease code is serialized by the kernel
2715 * lock) we know the server hasn't removed the lease yet, we know
2716 * it's safe to take a reference: */
2717 atomic_inc(&dp
->dl_count
);
2719 list_add_tail(&dp
->dl_recall_lru
, &nn
->del_recall_lru
);
2721 /* Only place dl_time is set; protected by i_lock: */
2722 dp
->dl_time
= get_seconds();
2724 nfsd4_cb_recall(dp
);
2727 /* Called from break_lease() with i_lock held. */
2728 static void nfsd_break_deleg_cb(struct file_lock
*fl
)
2730 struct nfs4_file
*fp
= (struct nfs4_file
*)fl
->fl_owner
;
2731 struct nfs4_delegation
*dp
;
2734 WARN(1, "(%p)->fl_owner NULL\n", fl
);
2737 if (fp
->fi_had_conflict
) {
2738 WARN(1, "duplicate break on %p\n", fp
);
2742 * We don't want the locks code to timeout the lease for us;
2743 * we'll remove it ourself if a delegation isn't returned
2746 fl
->fl_break_time
= 0;
2748 spin_lock(&recall_lock
);
2749 fp
->fi_had_conflict
= true;
2750 list_for_each_entry(dp
, &fp
->fi_delegations
, dl_perfile
)
2751 nfsd_break_one_deleg(dp
);
2752 spin_unlock(&recall_lock
);
2756 int nfsd_change_deleg_cb(struct file_lock
**onlist
, int arg
)
2759 return lease_modify(onlist
, arg
);
2764 static const struct lock_manager_operations nfsd_lease_mng_ops
= {
2765 .lm_break
= nfsd_break_deleg_cb
,
2766 .lm_change
= nfsd_change_deleg_cb
,
2769 static __be32
nfsd4_check_seqid(struct nfsd4_compound_state
*cstate
, struct nfs4_stateowner
*so
, u32 seqid
)
2771 if (nfsd4_has_session(cstate
))
2773 if (seqid
== so
->so_seqid
- 1)
2774 return nfserr_replay_me
;
2775 if (seqid
== so
->so_seqid
)
2777 return nfserr_bad_seqid
;
2781 nfsd4_process_open1(struct nfsd4_compound_state
*cstate
,
2782 struct nfsd4_open
*open
, struct nfsd_net
*nn
)
2784 clientid_t
*clientid
= &open
->op_clientid
;
2785 struct nfs4_client
*clp
= NULL
;
2786 unsigned int strhashval
;
2787 struct nfs4_openowner
*oo
= NULL
;
2790 if (STALE_CLIENTID(&open
->op_clientid
, nn
))
2791 return nfserr_stale_clientid
;
2793 * In case we need it later, after we've already created the
2794 * file and don't want to risk a further failure:
2796 open
->op_file
= nfsd4_alloc_file();
2797 if (open
->op_file
== NULL
)
2798 return nfserr_jukebox
;
2800 strhashval
= ownerstr_hashval(clientid
->cl_id
, &open
->op_owner
);
2801 oo
= find_openstateowner_str(strhashval
, open
, cstate
->minorversion
, nn
);
2802 open
->op_openowner
= oo
;
2804 clp
= find_confirmed_client(clientid
, cstate
->minorversion
,
2807 return nfserr_expired
;
2810 if (!(oo
->oo_flags
& NFS4_OO_CONFIRMED
)) {
2811 /* Replace unconfirmed owners without checking for replay. */
2812 clp
= oo
->oo_owner
.so_client
;
2813 release_openowner(oo
);
2814 open
->op_openowner
= NULL
;
2817 status
= nfsd4_check_seqid(cstate
, &oo
->oo_owner
, open
->op_seqid
);
2820 clp
= oo
->oo_owner
.so_client
;
2823 oo
= alloc_init_open_stateowner(strhashval
, clp
, open
);
2825 return nfserr_jukebox
;
2826 open
->op_openowner
= oo
;
2828 open
->op_stp
= nfs4_alloc_stateid(clp
);
2830 return nfserr_jukebox
;
2834 static inline __be32
2835 nfs4_check_delegmode(struct nfs4_delegation
*dp
, int flags
)
2837 if ((flags
& WR_STATE
) && (dp
->dl_type
== NFS4_OPEN_DELEGATE_READ
))
2838 return nfserr_openmode
;
2843 static int share_access_to_flags(u32 share_access
)
2845 return share_access
== NFS4_SHARE_ACCESS_READ
? RD_STATE
: WR_STATE
;
2848 static struct nfs4_delegation
*find_deleg_stateid(struct nfs4_client
*cl
, stateid_t
*s
)
2850 struct nfs4_stid
*ret
;
2852 ret
= find_stateid_by_type(cl
, s
, NFS4_DELEG_STID
);
2855 return delegstateid(ret
);
2858 static bool nfsd4_is_deleg_cur(struct nfsd4_open
*open
)
2860 return open
->op_claim_type
== NFS4_OPEN_CLAIM_DELEGATE_CUR
||
2861 open
->op_claim_type
== NFS4_OPEN_CLAIM_DELEG_CUR_FH
;
2865 nfs4_check_deleg(struct nfs4_client
*cl
, struct nfsd4_open
*open
,
2866 struct nfs4_delegation
**dp
)
2869 __be32 status
= nfserr_bad_stateid
;
2871 *dp
= find_deleg_stateid(cl
, &open
->op_delegate_stateid
);
2874 flags
= share_access_to_flags(open
->op_share_access
);
2875 status
= nfs4_check_delegmode(*dp
, flags
);
2879 if (!nfsd4_is_deleg_cur(open
))
2883 open
->op_openowner
->oo_flags
|= NFS4_OO_CONFIRMED
;
2888 nfs4_check_open(struct nfs4_file
*fp
, struct nfsd4_open
*open
, struct nfs4_ol_stateid
**stpp
)
2890 struct nfs4_ol_stateid
*local
;
2891 struct nfs4_openowner
*oo
= open
->op_openowner
;
2893 list_for_each_entry(local
, &fp
->fi_stateids
, st_perfile
) {
2894 /* ignore lock owners */
2895 if (local
->st_stateowner
->so_is_open_owner
== 0)
2897 /* remember if we have seen this open owner */
2898 if (local
->st_stateowner
== &oo
->oo_owner
)
2900 /* check for conflicting share reservations */
2901 if (!test_share(local
, open
))
2902 return nfserr_share_denied
;
2907 static inline int nfs4_access_to_access(u32 nfs4_access
)
2911 if (nfs4_access
& NFS4_SHARE_ACCESS_READ
)
2912 flags
|= NFSD_MAY_READ
;
2913 if (nfs4_access
& NFS4_SHARE_ACCESS_WRITE
)
2914 flags
|= NFSD_MAY_WRITE
;
2918 static __be32
nfs4_get_vfs_file(struct svc_rqst
*rqstp
, struct nfs4_file
*fp
,
2919 struct svc_fh
*cur_fh
, struct nfsd4_open
*open
)
2922 int oflag
= nfs4_access_to_omode(open
->op_share_access
);
2923 int access
= nfs4_access_to_access(open
->op_share_access
);
2925 if (!fp
->fi_fds
[oflag
]) {
2926 status
= nfsd_open(rqstp
, cur_fh
, S_IFREG
, access
,
2927 &fp
->fi_fds
[oflag
]);
2931 nfs4_file_get_access(fp
, oflag
);
2936 static inline __be32
2937 nfsd4_truncate(struct svc_rqst
*rqstp
, struct svc_fh
*fh
,
2938 struct nfsd4_open
*open
)
2940 struct iattr iattr
= {
2941 .ia_valid
= ATTR_SIZE
,
2944 if (!open
->op_truncate
)
2946 if (!(open
->op_share_access
& NFS4_SHARE_ACCESS_WRITE
))
2947 return nfserr_inval
;
2948 return nfsd_setattr(rqstp
, fh
, &iattr
, 0, (time_t)0);
2952 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
)
2954 u32 op_share_access
= open
->op_share_access
;
2958 new_access
= !test_access(op_share_access
, stp
);
2960 status
= nfs4_get_vfs_file(rqstp
, fp
, cur_fh
, open
);
2964 status
= nfsd4_truncate(rqstp
, cur_fh
, open
);
2967 int oflag
= nfs4_access_to_omode(op_share_access
);
2968 nfs4_file_put_access(fp
, oflag
);
2972 /* remember the open */
2973 set_access(op_share_access
, stp
);
2974 set_deny(open
->op_share_deny
, stp
);
2981 nfs4_set_claim_prev(struct nfsd4_open
*open
, bool has_session
)
2983 open
->op_openowner
->oo_flags
|= NFS4_OO_CONFIRMED
;
2986 /* Should we give out recallable state?: */
2987 static bool nfsd4_cb_channel_good(struct nfs4_client
*clp
)
2989 if (clp
->cl_cb_state
== NFSD4_CB_UP
)
2992 * In the sessions case, since we don't have to establish a
2993 * separate connection for callbacks, we assume it's OK
2994 * until we hear otherwise:
2996 return clp
->cl_minorversion
&& clp
->cl_cb_state
== NFSD4_CB_UNKNOWN
;
2999 static struct file_lock
*nfs4_alloc_init_lease(struct nfs4_delegation
*dp
, int flag
)
3001 struct file_lock
*fl
;
3003 fl
= locks_alloc_lock();
3006 locks_init_lock(fl
);
3007 fl
->fl_lmops
= &nfsd_lease_mng_ops
;
3008 fl
->fl_flags
= FL_LEASE
;
3009 fl
->fl_type
= flag
== NFS4_OPEN_DELEGATE_READ
? F_RDLCK
: F_WRLCK
;
3010 fl
->fl_end
= OFFSET_MAX
;
3011 fl
->fl_owner
= (fl_owner_t
)(dp
->dl_file
);
3012 fl
->fl_pid
= current
->tgid
;
3016 static int nfs4_setlease(struct nfs4_delegation
*dp
)
3018 struct nfs4_file
*fp
= dp
->dl_file
;
3019 struct file_lock
*fl
;
3022 fl
= nfs4_alloc_init_lease(dp
, NFS4_OPEN_DELEGATE_READ
);
3025 fl
->fl_file
= find_readable_file(fp
);
3026 list_add(&dp
->dl_perclnt
, &dp
->dl_stid
.sc_client
->cl_delegations
);
3027 status
= vfs_setlease(fl
->fl_file
, fl
->fl_type
, &fl
);
3029 list_del_init(&dp
->dl_perclnt
);
3030 locks_free_lock(fl
);
3034 fp
->fi_deleg_file
= get_file(fl
->fl_file
);
3035 atomic_set(&fp
->fi_delegees
, 1);
3036 list_add(&dp
->dl_perfile
, &fp
->fi_delegations
);
3040 static int nfs4_set_delegation(struct nfs4_delegation
*dp
)
3042 struct nfs4_file
*fp
= dp
->dl_file
;
3045 return nfs4_setlease(dp
);
3046 spin_lock(&recall_lock
);
3047 if (fp
->fi_had_conflict
) {
3048 spin_unlock(&recall_lock
);
3051 atomic_inc(&fp
->fi_delegees
);
3052 list_add(&dp
->dl_perfile
, &fp
->fi_delegations
);
3053 spin_unlock(&recall_lock
);
3054 list_add(&dp
->dl_perclnt
, &dp
->dl_stid
.sc_client
->cl_delegations
);
3058 static void nfsd4_open_deleg_none_ext(struct nfsd4_open
*open
, int status
)
3060 open
->op_delegate_type
= NFS4_OPEN_DELEGATE_NONE_EXT
;
3061 if (status
== -EAGAIN
)
3062 open
->op_why_no_deleg
= WND4_CONTENTION
;
3064 open
->op_why_no_deleg
= WND4_RESOURCE
;
3065 switch (open
->op_deleg_want
) {
3066 case NFS4_SHARE_WANT_READ_DELEG
:
3067 case NFS4_SHARE_WANT_WRITE_DELEG
:
3068 case NFS4_SHARE_WANT_ANY_DELEG
:
3070 case NFS4_SHARE_WANT_CANCEL
:
3071 open
->op_why_no_deleg
= WND4_CANCELLED
;
3073 case NFS4_SHARE_WANT_NO_DELEG
:
3080 * Attempt to hand out a delegation.
3082 * Note we don't support write delegations, and won't until the vfs has
3083 * proper support for them.
3086 nfs4_open_delegation(struct net
*net
, struct svc_fh
*fh
,
3087 struct nfsd4_open
*open
, struct nfs4_ol_stateid
*stp
)
3089 struct nfs4_delegation
*dp
;
3090 struct nfs4_openowner
*oo
= container_of(stp
->st_stateowner
, struct nfs4_openowner
, oo_owner
);
3094 cb_up
= nfsd4_cb_channel_good(oo
->oo_owner
.so_client
);
3095 open
->op_recall
= 0;
3096 switch (open
->op_claim_type
) {
3097 case NFS4_OPEN_CLAIM_PREVIOUS
:
3099 open
->op_recall
= 1;
3100 if (open
->op_delegate_type
!= NFS4_OPEN_DELEGATE_READ
)
3103 case NFS4_OPEN_CLAIM_NULL
:
3105 * Let's not give out any delegations till everyone's
3106 * had the chance to reclaim theirs....
3108 if (locks_in_grace(net
))
3110 if (!cb_up
|| !(oo
->oo_flags
& NFS4_OO_CONFIRMED
))
3113 * Also, if the file was opened for write or
3114 * create, there's a good chance the client's
3115 * about to write to it, resulting in an
3116 * immediate recall (since we don't support
3117 * write delegations):
3119 if (open
->op_share_access
& NFS4_SHARE_ACCESS_WRITE
)
3121 if (open
->op_create
== NFS4_OPEN_CREATE
)
3127 dp
= alloc_init_deleg(oo
->oo_owner
.so_client
, stp
, fh
);
3130 status
= nfs4_set_delegation(dp
);
3134 memcpy(&open
->op_delegate_stateid
, &dp
->dl_stid
.sc_stateid
, sizeof(dp
->dl_stid
.sc_stateid
));
3136 dprintk("NFSD: delegation stateid=" STATEID_FMT
"\n",
3137 STATEID_VAL(&dp
->dl_stid
.sc_stateid
));
3138 open
->op_delegate_type
= NFS4_OPEN_DELEGATE_READ
;
3141 unhash_stid(&dp
->dl_stid
);
3142 nfs4_put_delegation(dp
);
3144 open
->op_delegate_type
= NFS4_OPEN_DELEGATE_NONE
;
3145 if (open
->op_claim_type
== NFS4_OPEN_CLAIM_PREVIOUS
&&
3146 open
->op_delegate_type
!= NFS4_OPEN_DELEGATE_NONE
) {
3147 dprintk("NFSD: WARNING: refusing delegation reclaim\n");
3148 open
->op_recall
= 1;
3151 /* 4.1 client asking for a delegation? */
3152 if (open
->op_deleg_want
)
3153 nfsd4_open_deleg_none_ext(open
, status
);
3157 static void nfsd4_deleg_xgrade_none_ext(struct nfsd4_open
*open
,
3158 struct nfs4_delegation
*dp
)
3160 if (open
->op_deleg_want
== NFS4_SHARE_WANT_READ_DELEG
&&
3161 dp
->dl_type
== NFS4_OPEN_DELEGATE_WRITE
) {
3162 open
->op_delegate_type
= NFS4_OPEN_DELEGATE_NONE_EXT
;
3163 open
->op_why_no_deleg
= WND4_NOT_SUPP_DOWNGRADE
;
3164 } else if (open
->op_deleg_want
== NFS4_SHARE_WANT_WRITE_DELEG
&&
3165 dp
->dl_type
== NFS4_OPEN_DELEGATE_WRITE
) {
3166 open
->op_delegate_type
= NFS4_OPEN_DELEGATE_NONE_EXT
;
3167 open
->op_why_no_deleg
= WND4_NOT_SUPP_UPGRADE
;
3169 /* Otherwise the client must be confused wanting a delegation
3170 * it already has, therefore we don't return
3171 * NFS4_OPEN_DELEGATE_NONE_EXT and reason.
3176 * called with nfs4_lock_state() held.
3179 nfsd4_process_open2(struct svc_rqst
*rqstp
, struct svc_fh
*current_fh
, struct nfsd4_open
*open
)
3181 struct nfsd4_compoundres
*resp
= rqstp
->rq_resp
;
3182 struct nfs4_client
*cl
= open
->op_openowner
->oo_owner
.so_client
;
3183 struct nfs4_file
*fp
= NULL
;
3184 struct inode
*ino
= current_fh
->fh_dentry
->d_inode
;
3185 struct nfs4_ol_stateid
*stp
= NULL
;
3186 struct nfs4_delegation
*dp
= NULL
;
3190 * Lookup file; if found, lookup stateid and check open request,
3191 * and check for delegations in the process of being recalled.
3192 * If not found, create the nfs4_file struct
3194 fp
= find_file(ino
);
3196 if ((status
= nfs4_check_open(fp
, open
, &stp
)))
3198 status
= nfs4_check_deleg(cl
, open
, &dp
);
3202 status
= nfserr_bad_stateid
;
3203 if (nfsd4_is_deleg_cur(open
))
3205 status
= nfserr_jukebox
;
3207 open
->op_file
= NULL
;
3208 nfsd4_init_file(fp
, ino
);
3212 * OPEN the file, or upgrade an existing OPEN.
3213 * If truncate fails, the OPEN fails.
3216 /* Stateid was found, this is an OPEN upgrade */
3217 status
= nfs4_upgrade_open(rqstp
, fp
, current_fh
, stp
, open
);
3221 status
= nfs4_get_vfs_file(rqstp
, fp
, current_fh
, open
);
3224 status
= nfsd4_truncate(rqstp
, current_fh
, open
);
3228 open
->op_stp
= NULL
;
3229 init_open_stateid(stp
, fp
, open
);
3231 update_stateid(&stp
->st_stid
.sc_stateid
);
3232 memcpy(&open
->op_stateid
, &stp
->st_stid
.sc_stateid
, sizeof(stateid_t
));
3234 if (nfsd4_has_session(&resp
->cstate
)) {
3235 open
->op_openowner
->oo_flags
|= NFS4_OO_CONFIRMED
;
3237 if (open
->op_deleg_want
& NFS4_SHARE_WANT_NO_DELEG
) {
3238 open
->op_delegate_type
= NFS4_OPEN_DELEGATE_NONE_EXT
;
3239 open
->op_why_no_deleg
= WND4_NOT_WANTED
;
3245 * Attempt to hand out a delegation. No error return, because the
3246 * OPEN succeeds even if we fail.
3248 nfs4_open_delegation(SVC_NET(rqstp
), current_fh
, open
, stp
);
3252 dprintk("%s: stateid=" STATEID_FMT
"\n", __func__
,
3253 STATEID_VAL(&stp
->st_stid
.sc_stateid
));
3255 /* 4.1 client trying to upgrade/downgrade delegation? */
3256 if (open
->op_delegate_type
== NFS4_OPEN_DELEGATE_NONE
&& dp
&&
3257 open
->op_deleg_want
)
3258 nfsd4_deleg_xgrade_none_ext(open
, dp
);
3262 if (status
== 0 && open
->op_claim_type
== NFS4_OPEN_CLAIM_PREVIOUS
)
3263 nfs4_set_claim_prev(open
, nfsd4_has_session(&resp
->cstate
));
3265 * To finish the open response, we just need to set the rflags.
3267 open
->op_rflags
= NFS4_OPEN_RESULT_LOCKTYPE_POSIX
;
3268 if (!(open
->op_openowner
->oo_flags
& NFS4_OO_CONFIRMED
) &&
3269 !nfsd4_has_session(&resp
->cstate
))
3270 open
->op_rflags
|= NFS4_OPEN_RESULT_CONFIRM
;
3275 void nfsd4_cleanup_open_state(struct nfsd4_open
*open
, __be32 status
)
3277 if (open
->op_openowner
) {
3278 struct nfs4_openowner
*oo
= open
->op_openowner
;
3280 if (!list_empty(&oo
->oo_owner
.so_stateids
))
3281 list_del_init(&oo
->oo_close_lru
);
3282 if (oo
->oo_flags
& NFS4_OO_NEW
) {
3284 release_openowner(oo
);
3285 open
->op_openowner
= NULL
;
3287 oo
->oo_flags
&= ~NFS4_OO_NEW
;
3291 nfsd4_free_file(open
->op_file
);
3293 free_generic_stateid(open
->op_stp
);
3296 static __be32
lookup_clientid(clientid_t
*clid
, bool session
, struct nfsd_net
*nn
, struct nfs4_client
**clp
)
3298 struct nfs4_client
*found
;
3300 if (STALE_CLIENTID(clid
, nn
))
3301 return nfserr_stale_clientid
;
3302 found
= find_confirmed_client(clid
, session
, nn
);
3305 return found
? nfs_ok
: nfserr_expired
;
3309 nfsd4_renew(struct svc_rqst
*rqstp
, struct nfsd4_compound_state
*cstate
,
3312 struct nfs4_client
*clp
;
3314 struct nfsd_net
*nn
= net_generic(SVC_NET(rqstp
), nfsd_net_id
);
3317 dprintk("process_renew(%08x/%08x): starting\n",
3318 clid
->cl_boot
, clid
->cl_id
);
3319 status
= lookup_clientid(clid
, cstate
->minorversion
, nn
, &clp
);
3322 status
= nfserr_cb_path_down
;
3323 if (!list_empty(&clp
->cl_delegations
)
3324 && clp
->cl_cb_state
!= NFSD4_CB_UP
)
3328 nfs4_unlock_state();
3333 nfsd4_end_grace(struct nfsd_net
*nn
)
3335 /* do nothing if grace period already ended */
3336 if (nn
->grace_ended
)
3339 dprintk("NFSD: end of grace period\n");
3340 nn
->grace_ended
= true;
3341 nfsd4_record_grace_done(nn
, nn
->boot_time
);
3342 locks_end_grace(&nn
->nfsd4_manager
);
3344 * Now that every NFSv4 client has had the chance to recover and
3345 * to see the (possibly new, possibly shorter) lease time, we
3346 * can safely set the next grace time to the current lease time:
3348 nn
->nfsd4_grace
= nn
->nfsd4_lease
;
3352 nfs4_laundromat(struct nfsd_net
*nn
)
3354 struct nfs4_client
*clp
;
3355 struct nfs4_openowner
*oo
;
3356 struct nfs4_delegation
*dp
;
3357 struct list_head
*pos
, *next
, reaplist
;
3358 time_t cutoff
= get_seconds() - nn
->nfsd4_lease
;
3359 time_t t
, clientid_val
= nn
->nfsd4_lease
;
3360 time_t u
, test_val
= nn
->nfsd4_lease
;
3364 dprintk("NFSD: laundromat service - starting\n");
3365 nfsd4_end_grace(nn
);
3366 INIT_LIST_HEAD(&reaplist
);
3367 spin_lock(&nn
->client_lock
);
3368 list_for_each_safe(pos
, next
, &nn
->client_lru
) {
3369 clp
= list_entry(pos
, struct nfs4_client
, cl_lru
);
3370 if (time_after((unsigned long)clp
->cl_time
, (unsigned long)cutoff
)) {
3371 t
= clp
->cl_time
- cutoff
;
3372 if (clientid_val
> t
)
3376 if (mark_client_expired_locked(clp
)) {
3377 dprintk("NFSD: client in use (clientid %08x)\n",
3378 clp
->cl_clientid
.cl_id
);
3381 list_move(&clp
->cl_lru
, &reaplist
);
3383 spin_unlock(&nn
->client_lock
);
3384 list_for_each_safe(pos
, next
, &reaplist
) {
3385 clp
= list_entry(pos
, struct nfs4_client
, cl_lru
);
3386 dprintk("NFSD: purging unused client (clientid %08x)\n",
3387 clp
->cl_clientid
.cl_id
);
3390 spin_lock(&recall_lock
);
3391 list_for_each_safe(pos
, next
, &nn
->del_recall_lru
) {
3392 dp
= list_entry (pos
, struct nfs4_delegation
, dl_recall_lru
);
3393 if (net_generic(dp
->dl_stid
.sc_client
->net
, nfsd_net_id
) != nn
)
3395 if (time_after((unsigned long)dp
->dl_time
, (unsigned long)cutoff
)) {
3396 u
= dp
->dl_time
- cutoff
;
3401 list_move(&dp
->dl_recall_lru
, &reaplist
);
3403 spin_unlock(&recall_lock
);
3404 list_for_each_safe(pos
, next
, &reaplist
) {
3405 dp
= list_entry (pos
, struct nfs4_delegation
, dl_recall_lru
);
3406 revoke_delegation(dp
);
3408 test_val
= nn
->nfsd4_lease
;
3409 list_for_each_safe(pos
, next
, &nn
->close_lru
) {
3410 oo
= container_of(pos
, struct nfs4_openowner
, oo_close_lru
);
3411 if (time_after((unsigned long)oo
->oo_time
, (unsigned long)cutoff
)) {
3412 u
= oo
->oo_time
- cutoff
;
3417 release_openowner(oo
);
3419 if (clientid_val
< NFSD_LAUNDROMAT_MINTIMEOUT
)
3420 clientid_val
= NFSD_LAUNDROMAT_MINTIMEOUT
;
3421 nfs4_unlock_state();
3422 return clientid_val
;
3425 static struct workqueue_struct
*laundry_wq
;
3426 static void laundromat_main(struct work_struct
*);
3429 laundromat_main(struct work_struct
*laundry
)
3432 struct delayed_work
*dwork
= container_of(laundry
, struct delayed_work
,
3434 struct nfsd_net
*nn
= container_of(dwork
, struct nfsd_net
,
3437 t
= nfs4_laundromat(nn
);
3438 dprintk("NFSD: laundromat_main - sleeping for %ld seconds\n", t
);
3439 queue_delayed_work(laundry_wq
, &nn
->laundromat_work
, t
*HZ
);
3442 static inline __be32
nfs4_check_fh(struct svc_fh
*fhp
, struct nfs4_ol_stateid
*stp
)
3444 if (fhp
->fh_dentry
->d_inode
!= stp
->st_file
->fi_inode
)
3445 return nfserr_bad_stateid
;
3450 access_permit_read(struct nfs4_ol_stateid
*stp
)
3452 return test_access(NFS4_SHARE_ACCESS_READ
, stp
) ||
3453 test_access(NFS4_SHARE_ACCESS_BOTH
, stp
) ||
3454 test_access(NFS4_SHARE_ACCESS_WRITE
, stp
);
3458 access_permit_write(struct nfs4_ol_stateid
*stp
)
3460 return test_access(NFS4_SHARE_ACCESS_WRITE
, stp
) ||
3461 test_access(NFS4_SHARE_ACCESS_BOTH
, stp
);
3465 __be32
nfs4_check_openmode(struct nfs4_ol_stateid
*stp
, int flags
)
3467 __be32 status
= nfserr_openmode
;
3469 /* For lock stateid's, we test the parent open, not the lock: */
3470 if (stp
->st_openstp
)
3471 stp
= stp
->st_openstp
;
3472 if ((flags
& WR_STATE
) && !access_permit_write(stp
))
3474 if ((flags
& RD_STATE
) && !access_permit_read(stp
))
3481 static inline __be32
3482 check_special_stateids(struct net
*net
, svc_fh
*current_fh
, stateid_t
*stateid
, int flags
)
3484 if (ONE_STATEID(stateid
) && (flags
& RD_STATE
))
3486 else if (locks_in_grace(net
)) {
3487 /* Answer in remaining cases depends on existence of
3488 * conflicting state; so we must wait out the grace period. */
3489 return nfserr_grace
;
3490 } else if (flags
& WR_STATE
)
3491 return nfs4_share_conflict(current_fh
,
3492 NFS4_SHARE_DENY_WRITE
);
3493 else /* (flags & RD_STATE) && ZERO_STATEID(stateid) */
3494 return nfs4_share_conflict(current_fh
,
3495 NFS4_SHARE_DENY_READ
);
3499 * Allow READ/WRITE during grace period on recovered state only for files
3500 * that are not able to provide mandatory locking.
3503 grace_disallows_io(struct net
*net
, struct inode
*inode
)
3505 return locks_in_grace(net
) && mandatory_lock(inode
);
3508 /* Returns true iff a is later than b: */
3509 static bool stateid_generation_after(stateid_t
*a
, stateid_t
*b
)
3511 return (s32
)(a
->si_generation
- b
->si_generation
) > 0;
3514 static __be32
check_stateid_generation(stateid_t
*in
, stateid_t
*ref
, bool has_session
)
3517 * When sessions are used the stateid generation number is ignored
3520 if (has_session
&& in
->si_generation
== 0)
3523 if (in
->si_generation
== ref
->si_generation
)
3526 /* If the client sends us a stateid from the future, it's buggy: */
3527 if (stateid_generation_after(in
, ref
))
3528 return nfserr_bad_stateid
;
3530 * However, we could see a stateid from the past, even from a
3531 * non-buggy client. For example, if the client sends a lock
3532 * while some IO is outstanding, the lock may bump si_generation
3533 * while the IO is still in flight. The client could avoid that
3534 * situation by waiting for responses on all the IO requests,
3535 * but better performance may result in retrying IO that
3536 * receives an old_stateid error if requests are rarely
3537 * reordered in flight:
3539 return nfserr_old_stateid
;
3542 static __be32
nfsd4_validate_stateid(struct nfs4_client
*cl
, stateid_t
*stateid
)
3544 struct nfs4_stid
*s
;
3545 struct nfs4_ol_stateid
*ols
;
3548 if (ZERO_STATEID(stateid
) || ONE_STATEID(stateid
))
3549 return nfserr_bad_stateid
;
3550 /* Client debugging aid. */
3551 if (!same_clid(&stateid
->si_opaque
.so_clid
, &cl
->cl_clientid
)) {
3552 char addr_str
[INET6_ADDRSTRLEN
];
3553 rpc_ntop((struct sockaddr
*)&cl
->cl_addr
, addr_str
,
3555 pr_warn_ratelimited("NFSD: client %s testing state ID "
3556 "with incorrect client ID\n", addr_str
);
3557 return nfserr_bad_stateid
;
3559 s
= find_stateid(cl
, stateid
);
3561 return nfserr_bad_stateid
;
3562 status
= check_stateid_generation(stateid
, &s
->sc_stateid
, 1);
3565 switch (s
->sc_type
) {
3566 case NFS4_DELEG_STID
:
3568 case NFS4_REVOKED_DELEG_STID
:
3569 return nfserr_deleg_revoked
;
3570 case NFS4_OPEN_STID
:
3571 case NFS4_LOCK_STID
:
3572 ols
= openlockstateid(s
);
3573 if (ols
->st_stateowner
->so_is_open_owner
3574 && !(openowner(ols
->st_stateowner
)->oo_flags
3575 & NFS4_OO_CONFIRMED
))
3576 return nfserr_bad_stateid
;
3579 printk("unknown stateid type %x\n", s
->sc_type
);
3580 case NFS4_CLOSED_STID
:
3581 return nfserr_bad_stateid
;
3585 static __be32
nfsd4_lookup_stateid(stateid_t
*stateid
, unsigned char typemask
,
3586 struct nfs4_stid
**s
, bool sessions
,
3587 struct nfsd_net
*nn
)
3589 struct nfs4_client
*cl
;
3592 if (ZERO_STATEID(stateid
) || ONE_STATEID(stateid
))
3593 return nfserr_bad_stateid
;
3594 status
= lookup_clientid(&stateid
->si_opaque
.so_clid
, sessions
,
3596 if (status
== nfserr_stale_clientid
)
3597 return nfserr_stale_stateid
;
3600 *s
= find_stateid_by_type(cl
, stateid
, typemask
);
3602 return nfserr_bad_stateid
;
3607 * Checks for stateid operations
3610 nfs4_preprocess_stateid_op(struct net
*net
, struct nfsd4_compound_state
*cstate
,
3611 stateid_t
*stateid
, int flags
, struct file
**filpp
)
3613 struct nfs4_stid
*s
;
3614 struct nfs4_ol_stateid
*stp
= NULL
;
3615 struct nfs4_delegation
*dp
= NULL
;
3616 struct svc_fh
*current_fh
= &cstate
->current_fh
;
3617 struct inode
*ino
= current_fh
->fh_dentry
->d_inode
;
3618 struct nfsd_net
*nn
= net_generic(net
, nfsd_net_id
);
3624 if (grace_disallows_io(net
, ino
))
3625 return nfserr_grace
;
3627 if (ZERO_STATEID(stateid
) || ONE_STATEID(stateid
))
3628 return check_special_stateids(net
, current_fh
, stateid
, flags
);
3630 status
= nfsd4_lookup_stateid(stateid
, NFS4_DELEG_STID
|NFS4_OPEN_STID
|NFS4_LOCK_STID
,
3631 &s
, cstate
->minorversion
, nn
);
3634 status
= check_stateid_generation(stateid
, &s
->sc_stateid
, nfsd4_has_session(cstate
));
3637 switch (s
->sc_type
) {
3638 case NFS4_DELEG_STID
:
3639 dp
= delegstateid(s
);
3640 status
= nfs4_check_delegmode(dp
, flags
);
3644 *filpp
= dp
->dl_file
->fi_deleg_file
;
3647 status
= nfserr_serverfault
;
3652 case NFS4_OPEN_STID
:
3653 case NFS4_LOCK_STID
:
3654 stp
= openlockstateid(s
);
3655 status
= nfs4_check_fh(current_fh
, stp
);
3658 if (stp
->st_stateowner
->so_is_open_owner
3659 && !(openowner(stp
->st_stateowner
)->oo_flags
& NFS4_OO_CONFIRMED
))
3661 status
= nfs4_check_openmode(stp
, flags
);
3665 if (flags
& RD_STATE
)
3666 *filpp
= find_readable_file(stp
->st_file
);
3668 *filpp
= find_writeable_file(stp
->st_file
);
3672 return nfserr_bad_stateid
;
3680 nfsd4_free_lock_stateid(struct nfs4_ol_stateid
*stp
)
3682 if (check_for_locks(stp
->st_file
, lockowner(stp
->st_stateowner
)))
3683 return nfserr_locks_held
;
3684 release_lock_stateid(stp
);
3689 * Test if the stateid is valid
3692 nfsd4_test_stateid(struct svc_rqst
*rqstp
, struct nfsd4_compound_state
*cstate
,
3693 struct nfsd4_test_stateid
*test_stateid
)
3695 struct nfsd4_test_stateid_id
*stateid
;
3696 struct nfs4_client
*cl
= cstate
->session
->se_client
;
3699 list_for_each_entry(stateid
, &test_stateid
->ts_stateid_list
, ts_id_list
)
3700 stateid
->ts_id_status
=
3701 nfsd4_validate_stateid(cl
, &stateid
->ts_id_stateid
);
3702 nfs4_unlock_state();
3708 nfsd4_free_stateid(struct svc_rqst
*rqstp
, struct nfsd4_compound_state
*cstate
,
3709 struct nfsd4_free_stateid
*free_stateid
)
3711 stateid_t
*stateid
= &free_stateid
->fr_stateid
;
3712 struct nfs4_stid
*s
;
3713 struct nfs4_delegation
*dp
;
3714 struct nfs4_client
*cl
= cstate
->session
->se_client
;
3715 __be32 ret
= nfserr_bad_stateid
;
3718 s
= find_stateid(cl
, stateid
);
3721 switch (s
->sc_type
) {
3722 case NFS4_DELEG_STID
:
3723 ret
= nfserr_locks_held
;
3725 case NFS4_OPEN_STID
:
3726 case NFS4_LOCK_STID
:
3727 ret
= check_stateid_generation(stateid
, &s
->sc_stateid
, 1);
3730 if (s
->sc_type
== NFS4_LOCK_STID
)
3731 ret
= nfsd4_free_lock_stateid(openlockstateid(s
));
3733 ret
= nfserr_locks_held
;
3735 case NFS4_REVOKED_DELEG_STID
:
3736 dp
= delegstateid(s
);
3737 destroy_revoked_delegation(dp
);
3741 ret
= nfserr_bad_stateid
;
3744 nfs4_unlock_state();
3751 return (type
== NFS4_READW_LT
|| type
== NFS4_READ_LT
) ?
3752 RD_STATE
: WR_STATE
;
3755 static __be32
nfs4_seqid_op_checks(struct nfsd4_compound_state
*cstate
, stateid_t
*stateid
, u32 seqid
, struct nfs4_ol_stateid
*stp
)
3757 struct svc_fh
*current_fh
= &cstate
->current_fh
;
3758 struct nfs4_stateowner
*sop
= stp
->st_stateowner
;
3761 status
= nfsd4_check_seqid(cstate
, sop
, seqid
);
3764 if (stp
->st_stid
.sc_type
== NFS4_CLOSED_STID
3765 || stp
->st_stid
.sc_type
== NFS4_REVOKED_DELEG_STID
)
3767 * "Closed" stateid's exist *only* to return
3768 * nfserr_replay_me from the previous step, and
3769 * revoked delegations are kept only for free_stateid.
3771 return nfserr_bad_stateid
;
3772 status
= check_stateid_generation(stateid
, &stp
->st_stid
.sc_stateid
, nfsd4_has_session(cstate
));
3775 return nfs4_check_fh(current_fh
, stp
);
3779 * Checks for sequence id mutating operations.
3782 nfs4_preprocess_seqid_op(struct nfsd4_compound_state
*cstate
, u32 seqid
,
3783 stateid_t
*stateid
, char typemask
,
3784 struct nfs4_ol_stateid
**stpp
,
3785 struct nfsd_net
*nn
)
3788 struct nfs4_stid
*s
;
3790 dprintk("NFSD: %s: seqid=%d stateid = " STATEID_FMT
"\n", __func__
,
3791 seqid
, STATEID_VAL(stateid
));
3794 status
= nfsd4_lookup_stateid(stateid
, typemask
, &s
,
3795 cstate
->minorversion
, nn
);
3798 *stpp
= openlockstateid(s
);
3799 if (!nfsd4_has_session(cstate
))
3800 cstate
->replay_owner
= (*stpp
)->st_stateowner
;
3802 return nfs4_seqid_op_checks(cstate
, stateid
, seqid
, *stpp
);
3805 static __be32
nfs4_preprocess_confirmed_seqid_op(struct nfsd4_compound_state
*cstate
, u32 seqid
,
3806 stateid_t
*stateid
, struct nfs4_ol_stateid
**stpp
, struct nfsd_net
*nn
)
3809 struct nfs4_openowner
*oo
;
3811 status
= nfs4_preprocess_seqid_op(cstate
, seqid
, stateid
,
3812 NFS4_OPEN_STID
, stpp
, nn
);
3815 oo
= openowner((*stpp
)->st_stateowner
);
3816 if (!(oo
->oo_flags
& NFS4_OO_CONFIRMED
))
3817 return nfserr_bad_stateid
;
3822 nfsd4_open_confirm(struct svc_rqst
*rqstp
, struct nfsd4_compound_state
*cstate
,
3823 struct nfsd4_open_confirm
*oc
)
3826 struct nfs4_openowner
*oo
;
3827 struct nfs4_ol_stateid
*stp
;
3828 struct nfsd_net
*nn
= net_generic(SVC_NET(rqstp
), nfsd_net_id
);
3830 dprintk("NFSD: nfsd4_open_confirm on file %.*s\n",
3831 (int)cstate
->current_fh
.fh_dentry
->d_name
.len
,
3832 cstate
->current_fh
.fh_dentry
->d_name
.name
);
3834 status
= fh_verify(rqstp
, &cstate
->current_fh
, S_IFREG
, 0);
3840 status
= nfs4_preprocess_seqid_op(cstate
,
3841 oc
->oc_seqid
, &oc
->oc_req_stateid
,
3842 NFS4_OPEN_STID
, &stp
, nn
);
3845 oo
= openowner(stp
->st_stateowner
);
3846 status
= nfserr_bad_stateid
;
3847 if (oo
->oo_flags
& NFS4_OO_CONFIRMED
)
3849 oo
->oo_flags
|= NFS4_OO_CONFIRMED
;
3850 update_stateid(&stp
->st_stid
.sc_stateid
);
3851 memcpy(&oc
->oc_resp_stateid
, &stp
->st_stid
.sc_stateid
, sizeof(stateid_t
));
3852 dprintk("NFSD: %s: success, seqid=%d stateid=" STATEID_FMT
"\n",
3853 __func__
, oc
->oc_seqid
, STATEID_VAL(&stp
->st_stid
.sc_stateid
));
3855 nfsd4_client_record_create(oo
->oo_owner
.so_client
);
3858 nfsd4_bump_seqid(cstate
, status
);
3859 if (!cstate
->replay_owner
)
3860 nfs4_unlock_state();
3864 static inline void nfs4_stateid_downgrade_bit(struct nfs4_ol_stateid
*stp
, u32 access
)
3866 if (!test_access(access
, stp
))
3868 nfs4_file_put_access(stp
->st_file
, nfs4_access_to_omode(access
));
3869 clear_access(access
, stp
);
3872 static inline void nfs4_stateid_downgrade(struct nfs4_ol_stateid
*stp
, u32 to_access
)
3874 switch (to_access
) {
3875 case NFS4_SHARE_ACCESS_READ
:
3876 nfs4_stateid_downgrade_bit(stp
, NFS4_SHARE_ACCESS_WRITE
);
3877 nfs4_stateid_downgrade_bit(stp
, NFS4_SHARE_ACCESS_BOTH
);
3879 case NFS4_SHARE_ACCESS_WRITE
:
3880 nfs4_stateid_downgrade_bit(stp
, NFS4_SHARE_ACCESS_READ
);
3881 nfs4_stateid_downgrade_bit(stp
, NFS4_SHARE_ACCESS_BOTH
);
3883 case NFS4_SHARE_ACCESS_BOTH
:
3891 reset_union_bmap_deny(unsigned long deny
, struct nfs4_ol_stateid
*stp
)
3894 for (i
= 0; i
< 4; i
++) {
3895 if ((i
& deny
) != i
)
3901 nfsd4_open_downgrade(struct svc_rqst
*rqstp
,
3902 struct nfsd4_compound_state
*cstate
,
3903 struct nfsd4_open_downgrade
*od
)
3906 struct nfs4_ol_stateid
*stp
;
3907 struct nfsd_net
*nn
= net_generic(SVC_NET(rqstp
), nfsd_net_id
);
3909 dprintk("NFSD: nfsd4_open_downgrade on file %.*s\n",
3910 (int)cstate
->current_fh
.fh_dentry
->d_name
.len
,
3911 cstate
->current_fh
.fh_dentry
->d_name
.name
);
3913 /* We don't yet support WANT bits: */
3914 if (od
->od_deleg_want
)
3915 dprintk("NFSD: %s: od_deleg_want=0x%x ignored\n", __func__
,
3919 status
= nfs4_preprocess_confirmed_seqid_op(cstate
, od
->od_seqid
,
3920 &od
->od_stateid
, &stp
, nn
);
3923 status
= nfserr_inval
;
3924 if (!test_access(od
->od_share_access
, stp
)) {
3925 dprintk("NFSD: access not a subset current bitmap: 0x%lx, input access=%08x\n",
3926 stp
->st_access_bmap
, od
->od_share_access
);
3929 if (!test_deny(od
->od_share_deny
, stp
)) {
3930 dprintk("NFSD:deny not a subset current bitmap: 0x%lx, input deny=%08x\n",
3931 stp
->st_deny_bmap
, od
->od_share_deny
);
3934 nfs4_stateid_downgrade(stp
, od
->od_share_access
);
3936 reset_union_bmap_deny(od
->od_share_deny
, stp
);
3938 update_stateid(&stp
->st_stid
.sc_stateid
);
3939 memcpy(&od
->od_stateid
, &stp
->st_stid
.sc_stateid
, sizeof(stateid_t
));
3942 nfsd4_bump_seqid(cstate
, status
);
3943 if (!cstate
->replay_owner
)
3944 nfs4_unlock_state();
3948 static void nfsd4_close_open_stateid(struct nfs4_ol_stateid
*s
)
3950 unhash_open_stateid(s
);
3951 s
->st_stid
.sc_type
= NFS4_CLOSED_STID
;
3955 * nfs4_unlock_state() called after encode
3958 nfsd4_close(struct svc_rqst
*rqstp
, struct nfsd4_compound_state
*cstate
,
3959 struct nfsd4_close
*close
)
3962 struct nfs4_openowner
*oo
;
3963 struct nfs4_ol_stateid
*stp
;
3964 struct net
*net
= SVC_NET(rqstp
);
3965 struct nfsd_net
*nn
= net_generic(net
, nfsd_net_id
);
3967 dprintk("NFSD: nfsd4_close on file %.*s\n",
3968 (int)cstate
->current_fh
.fh_dentry
->d_name
.len
,
3969 cstate
->current_fh
.fh_dentry
->d_name
.name
);
3972 status
= nfs4_preprocess_seqid_op(cstate
, close
->cl_seqid
,
3974 NFS4_OPEN_STID
|NFS4_CLOSED_STID
,
3976 nfsd4_bump_seqid(cstate
, status
);
3979 oo
= openowner(stp
->st_stateowner
);
3980 update_stateid(&stp
->st_stid
.sc_stateid
);
3981 memcpy(&close
->cl_stateid
, &stp
->st_stid
.sc_stateid
, sizeof(stateid_t
));
3983 nfsd4_close_open_stateid(stp
);
3985 if (cstate
->minorversion
) {
3986 unhash_stid(&stp
->st_stid
);
3987 free_generic_stateid(stp
);
3989 oo
->oo_last_closed_stid
= stp
;
3991 if (list_empty(&oo
->oo_owner
.so_stateids
)) {
3992 if (cstate
->minorversion
)
3993 release_openowner(oo
);
3996 * In the 4.0 case we need to keep the owners around a
3997 * little while to handle CLOSE replay.
3999 move_to_close_lru(oo
, SVC_NET(rqstp
));
4003 if (!cstate
->replay_owner
)
4004 nfs4_unlock_state();
4009 nfsd4_delegreturn(struct svc_rqst
*rqstp
, struct nfsd4_compound_state
*cstate
,
4010 struct nfsd4_delegreturn
*dr
)
4012 struct nfs4_delegation
*dp
;
4013 stateid_t
*stateid
= &dr
->dr_stateid
;
4014 struct nfs4_stid
*s
;
4016 struct nfsd_net
*nn
= net_generic(SVC_NET(rqstp
), nfsd_net_id
);
4018 if ((status
= fh_verify(rqstp
, &cstate
->current_fh
, S_IFREG
, 0)))
4022 status
= nfsd4_lookup_stateid(stateid
, NFS4_DELEG_STID
, &s
,
4023 cstate
->minorversion
, nn
);
4026 dp
= delegstateid(s
);
4027 status
= check_stateid_generation(stateid
, &dp
->dl_stid
.sc_stateid
, nfsd4_has_session(cstate
));
4031 destroy_delegation(dp
);
4033 nfs4_unlock_state();
4039 #define LOFF_OVERFLOW(start, len) ((u64)(len) > ~(u64)(start))
4041 #define LOCKOWNER_INO_HASH_MASK (LOCKOWNER_INO_HASH_SIZE - 1)
4044 end_offset(u64 start
, u64 len
)
4049 return end
>= start
? end
: NFS4_MAX_UINT64
;
4052 /* last octet in a range */
4054 last_byte_offset(u64 start
, u64 len
)
4060 return end
> start
? end
- 1: NFS4_MAX_UINT64
;
4063 static unsigned int lockowner_ino_hashval(struct inode
*inode
, u32 cl_id
, struct xdr_netobj
*ownername
)
4065 return (file_hashval(inode
) + cl_id
4066 + opaque_hashval(ownername
->data
, ownername
->len
))
4067 & LOCKOWNER_INO_HASH_MASK
;
4071 * TODO: Linux file offsets are _signed_ 64-bit quantities, which means that
4072 * we can't properly handle lock requests that go beyond the (2^63 - 1)-th
4073 * byte, because of sign extension problems. Since NFSv4 calls for 64-bit
4074 * locking, this prevents us from being completely protocol-compliant. The
4075 * real solution to this problem is to start using unsigned file offsets in
4076 * the VFS, but this is a very deep change!
4079 nfs4_transform_lock_offset(struct file_lock
*lock
)
4081 if (lock
->fl_start
< 0)
4082 lock
->fl_start
= OFFSET_MAX
;
4083 if (lock
->fl_end
< 0)
4084 lock
->fl_end
= OFFSET_MAX
;
4087 /* Hack!: For now, we're defining this just so we can use a pointer to it
4088 * as a unique cookie to identify our (NFSv4's) posix locks. */
4089 static const struct lock_manager_operations nfsd_posix_mng_ops
= {
4093 nfs4_set_lock_denied(struct file_lock
*fl
, struct nfsd4_lock_denied
*deny
)
4095 struct nfs4_lockowner
*lo
;
4097 if (fl
->fl_lmops
== &nfsd_posix_mng_ops
) {
4098 lo
= (struct nfs4_lockowner
*) fl
->fl_owner
;
4099 deny
->ld_owner
.data
= kmemdup(lo
->lo_owner
.so_owner
.data
,
4100 lo
->lo_owner
.so_owner
.len
, GFP_KERNEL
);
4101 if (!deny
->ld_owner
.data
)
4102 /* We just don't care that much */
4104 deny
->ld_owner
.len
= lo
->lo_owner
.so_owner
.len
;
4105 deny
->ld_clientid
= lo
->lo_owner
.so_client
->cl_clientid
;
4108 deny
->ld_owner
.len
= 0;
4109 deny
->ld_owner
.data
= NULL
;
4110 deny
->ld_clientid
.cl_boot
= 0;
4111 deny
->ld_clientid
.cl_id
= 0;
4113 deny
->ld_start
= fl
->fl_start
;
4114 deny
->ld_length
= NFS4_MAX_UINT64
;
4115 if (fl
->fl_end
!= NFS4_MAX_UINT64
)
4116 deny
->ld_length
= fl
->fl_end
- fl
->fl_start
+ 1;
4117 deny
->ld_type
= NFS4_READ_LT
;
4118 if (fl
->fl_type
!= F_RDLCK
)
4119 deny
->ld_type
= NFS4_WRITE_LT
;
4122 static bool same_lockowner_ino(struct nfs4_lockowner
*lo
, struct inode
*inode
, clientid_t
*clid
, struct xdr_netobj
*owner
)
4124 struct nfs4_ol_stateid
*lst
;
4126 if (!same_owner_str(&lo
->lo_owner
, owner
, clid
))
4128 lst
= list_first_entry(&lo
->lo_owner
.so_stateids
,
4129 struct nfs4_ol_stateid
, st_perstateowner
);
4130 return lst
->st_file
->fi_inode
== inode
;
4133 static struct nfs4_lockowner
*
4134 find_lockowner_str(struct inode
*inode
, clientid_t
*clid
,
4135 struct xdr_netobj
*owner
, struct nfsd_net
*nn
)
4137 unsigned int hashval
= lockowner_ino_hashval(inode
, clid
->cl_id
, owner
);
4138 struct nfs4_lockowner
*lo
;
4140 list_for_each_entry(lo
, &nn
->lockowner_ino_hashtbl
[hashval
], lo_owner_ino_hash
) {
4141 if (same_lockowner_ino(lo
, inode
, clid
, owner
))
4147 static void hash_lockowner(struct nfs4_lockowner
*lo
, unsigned int strhashval
, struct nfs4_client
*clp
, struct nfs4_ol_stateid
*open_stp
)
4149 struct inode
*inode
= open_stp
->st_file
->fi_inode
;
4150 unsigned int inohash
= lockowner_ino_hashval(inode
,
4151 clp
->cl_clientid
.cl_id
, &lo
->lo_owner
.so_owner
);
4152 struct nfsd_net
*nn
= net_generic(clp
->net
, nfsd_net_id
);
4154 list_add(&lo
->lo_owner
.so_strhash
, &nn
->ownerstr_hashtbl
[strhashval
]);
4155 list_add(&lo
->lo_owner_ino_hash
, &nn
->lockowner_ino_hashtbl
[inohash
]);
4156 list_add(&lo
->lo_perstateid
, &open_stp
->st_lockowners
);
4160 * Alloc a lock owner structure.
4161 * Called in nfsd4_lock - therefore, OPEN and OPEN_CONFIRM (if needed) has
4164 * strhashval = ownerstr_hashval
4167 static struct nfs4_lockowner
*
4168 alloc_init_lock_stateowner(unsigned int strhashval
, struct nfs4_client
*clp
, struct nfs4_ol_stateid
*open_stp
, struct nfsd4_lock
*lock
) {
4169 struct nfs4_lockowner
*lo
;
4171 lo
= alloc_stateowner(lockowner_slab
, &lock
->lk_new_owner
, clp
);
4174 INIT_LIST_HEAD(&lo
->lo_owner
.so_stateids
);
4175 lo
->lo_owner
.so_is_open_owner
= 0;
4176 /* It is the openowner seqid that will be incremented in encode in the
4177 * case of new lockowners; so increment the lock seqid manually: */
4178 lo
->lo_owner
.so_seqid
= lock
->lk_new_lock_seqid
+ 1;
4179 hash_lockowner(lo
, strhashval
, clp
, open_stp
);
4183 static struct nfs4_ol_stateid
*
4184 alloc_init_lock_stateid(struct nfs4_lockowner
*lo
, struct nfs4_file
*fp
, struct nfs4_ol_stateid
*open_stp
)
4186 struct nfs4_ol_stateid
*stp
;
4187 struct nfs4_client
*clp
= lo
->lo_owner
.so_client
;
4189 stp
= nfs4_alloc_stateid(clp
);
4192 stp
->st_stid
.sc_type
= NFS4_LOCK_STID
;
4193 list_add(&stp
->st_perfile
, &fp
->fi_stateids
);
4194 list_add(&stp
->st_perstateowner
, &lo
->lo_owner
.so_stateids
);
4195 stp
->st_stateowner
= &lo
->lo_owner
;
4198 stp
->st_access_bmap
= 0;
4199 stp
->st_deny_bmap
= open_stp
->st_deny_bmap
;
4200 stp
->st_openstp
= open_stp
;
4205 check_lock_length(u64 offset
, u64 length
)
4207 return ((length
== 0) || ((length
!= NFS4_MAX_UINT64
) &&
4208 LOFF_OVERFLOW(offset
, length
)));
4211 static void get_lock_access(struct nfs4_ol_stateid
*lock_stp
, u32 access
)
4213 struct nfs4_file
*fp
= lock_stp
->st_file
;
4214 int oflag
= nfs4_access_to_omode(access
);
4216 if (test_access(access
, lock_stp
))
4218 nfs4_file_get_access(fp
, oflag
);
4219 set_access(access
, lock_stp
);
4222 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)
4224 struct nfs4_file
*fi
= ost
->st_file
;
4225 struct nfs4_openowner
*oo
= openowner(ost
->st_stateowner
);
4226 struct nfs4_client
*cl
= oo
->oo_owner
.so_client
;
4227 struct nfs4_lockowner
*lo
;
4228 unsigned int strhashval
;
4229 struct nfsd_net
*nn
= net_generic(cl
->net
, nfsd_net_id
);
4231 lo
= find_lockowner_str(fi
->fi_inode
, &cl
->cl_clientid
,
4232 &lock
->v
.new.owner
, nn
);
4234 if (!cstate
->minorversion
)
4235 return nfserr_bad_seqid
;
4236 /* XXX: a lockowner always has exactly one stateid: */
4237 *lst
= list_first_entry(&lo
->lo_owner
.so_stateids
,
4238 struct nfs4_ol_stateid
, st_perstateowner
);
4241 strhashval
= ownerstr_hashval(cl
->cl_clientid
.cl_id
,
4242 &lock
->v
.new.owner
);
4243 lo
= alloc_init_lock_stateowner(strhashval
, cl
, ost
, lock
);
4245 return nfserr_jukebox
;
4246 *lst
= alloc_init_lock_stateid(lo
, fi
, ost
);
4248 release_lockowner(lo
);
4249 return nfserr_jukebox
;
4259 nfsd4_lock(struct svc_rqst
*rqstp
, struct nfsd4_compound_state
*cstate
,
4260 struct nfsd4_lock
*lock
)
4262 struct nfs4_openowner
*open_sop
= NULL
;
4263 struct nfs4_lockowner
*lock_sop
= NULL
;
4264 struct nfs4_ol_stateid
*lock_stp
;
4265 struct file
*filp
= NULL
;
4266 struct file_lock
*file_lock
= NULL
;
4267 struct file_lock
*conflock
= NULL
;
4269 bool new_state
= false;
4272 struct net
*net
= SVC_NET(rqstp
);
4273 struct nfsd_net
*nn
= net_generic(net
, nfsd_net_id
);
4275 dprintk("NFSD: nfsd4_lock: start=%Ld length=%Ld\n",
4276 (long long) lock
->lk_offset
,
4277 (long long) lock
->lk_length
);
4279 if (check_lock_length(lock
->lk_offset
, lock
->lk_length
))
4280 return nfserr_inval
;
4282 if ((status
= fh_verify(rqstp
, &cstate
->current_fh
,
4283 S_IFREG
, NFSD_MAY_LOCK
))) {
4284 dprintk("NFSD: nfsd4_lock: permission denied!\n");
4290 if (lock
->lk_is_new
) {
4291 struct nfs4_ol_stateid
*open_stp
= NULL
;
4293 if (nfsd4_has_session(cstate
))
4294 /* See rfc 5661 18.10.3: given clientid is ignored: */
4295 memcpy(&lock
->v
.new.clientid
,
4296 &cstate
->session
->se_client
->cl_clientid
,
4297 sizeof(clientid_t
));
4299 status
= nfserr_stale_clientid
;
4300 if (STALE_CLIENTID(&lock
->lk_new_clientid
, nn
))
4303 /* validate and update open stateid and open seqid */
4304 status
= nfs4_preprocess_confirmed_seqid_op(cstate
,
4305 lock
->lk_new_open_seqid
,
4306 &lock
->lk_new_open_stateid
,
4310 open_sop
= openowner(open_stp
->st_stateowner
);
4311 status
= nfserr_bad_stateid
;
4312 if (!same_clid(&open_sop
->oo_owner
.so_client
->cl_clientid
,
4313 &lock
->v
.new.clientid
))
4315 status
= lookup_or_create_lock_state(cstate
, open_stp
, lock
,
4316 &lock_stp
, &new_state
);
4318 status
= nfs4_preprocess_seqid_op(cstate
,
4319 lock
->lk_old_lock_seqid
,
4320 &lock
->lk_old_lock_stateid
,
4321 NFS4_LOCK_STID
, &lock_stp
, nn
);
4324 lock_sop
= lockowner(lock_stp
->st_stateowner
);
4326 lkflg
= setlkflg(lock
->lk_type
);
4327 status
= nfs4_check_openmode(lock_stp
, lkflg
);
4331 status
= nfserr_grace
;
4332 if (locks_in_grace(net
) && !lock
->lk_reclaim
)
4334 status
= nfserr_no_grace
;
4335 if (!locks_in_grace(net
) && lock
->lk_reclaim
)
4338 file_lock
= locks_alloc_lock();
4340 dprintk("NFSD: %s: unable to allocate lock!\n", __func__
);
4341 status
= nfserr_jukebox
;
4345 locks_init_lock(file_lock
);
4346 switch (lock
->lk_type
) {
4349 filp
= find_readable_file(lock_stp
->st_file
);
4351 get_lock_access(lock_stp
, NFS4_SHARE_ACCESS_READ
);
4352 file_lock
->fl_type
= F_RDLCK
;
4355 case NFS4_WRITEW_LT
:
4356 filp
= find_writeable_file(lock_stp
->st_file
);
4358 get_lock_access(lock_stp
, NFS4_SHARE_ACCESS_WRITE
);
4359 file_lock
->fl_type
= F_WRLCK
;
4362 status
= nfserr_inval
;
4366 status
= nfserr_openmode
;
4369 file_lock
->fl_owner
= (fl_owner_t
)lock_sop
;
4370 file_lock
->fl_pid
= current
->tgid
;
4371 file_lock
->fl_file
= filp
;
4372 file_lock
->fl_flags
= FL_POSIX
;
4373 file_lock
->fl_lmops
= &nfsd_posix_mng_ops
;
4374 file_lock
->fl_start
= lock
->lk_offset
;
4375 file_lock
->fl_end
= last_byte_offset(lock
->lk_offset
, lock
->lk_length
);
4376 nfs4_transform_lock_offset(file_lock
);
4378 conflock
= locks_alloc_lock();
4380 dprintk("NFSD: %s: unable to allocate lock!\n", __func__
);
4381 status
= nfserr_jukebox
;
4385 err
= vfs_lock_file(filp
, F_SETLK
, file_lock
, conflock
);
4387 case 0: /* success! */
4388 update_stateid(&lock_stp
->st_stid
.sc_stateid
);
4389 memcpy(&lock
->lk_resp_stateid
, &lock_stp
->st_stid
.sc_stateid
,
4393 case (EAGAIN
): /* conflock holds conflicting lock */
4394 status
= nfserr_denied
;
4395 dprintk("NFSD: nfsd4_lock: conflicting lock found!\n");
4396 nfs4_set_lock_denied(conflock
, &lock
->lk_denied
);
4399 status
= nfserr_deadlock
;
4402 dprintk("NFSD: nfsd4_lock: vfs_lock_file() failed! status %d\n",err
);
4403 status
= nfserrno(err
);
4407 if (status
&& new_state
)
4408 release_lockowner(lock_sop
);
4409 nfsd4_bump_seqid(cstate
, status
);
4410 if (!cstate
->replay_owner
)
4411 nfs4_unlock_state();
4413 locks_free_lock(file_lock
);
4415 locks_free_lock(conflock
);
4420 * The NFSv4 spec allows a client to do a LOCKT without holding an OPEN,
4421 * so we do a temporary open here just to get an open file to pass to
4422 * vfs_test_lock. (Arguably perhaps test_lock should be done with an
4425 static __be32
nfsd_test_lock(struct svc_rqst
*rqstp
, struct svc_fh
*fhp
, struct file_lock
*lock
)
4428 __be32 err
= nfsd_open(rqstp
, fhp
, S_IFREG
, NFSD_MAY_READ
, &file
);
4430 err
= nfserrno(vfs_test_lock(file
, lock
));
4440 nfsd4_lockt(struct svc_rqst
*rqstp
, struct nfsd4_compound_state
*cstate
,
4441 struct nfsd4_lockt
*lockt
)
4443 struct inode
*inode
;
4444 struct file_lock
*file_lock
= NULL
;
4445 struct nfs4_lockowner
*lo
;
4447 struct nfsd_net
*nn
= net_generic(SVC_NET(rqstp
), nfsd_net_id
);
4449 if (locks_in_grace(SVC_NET(rqstp
)))
4450 return nfserr_grace
;
4452 if (check_lock_length(lockt
->lt_offset
, lockt
->lt_length
))
4453 return nfserr_inval
;
4457 if (!nfsd4_has_session(cstate
)) {
4458 status
= lookup_clientid(&lockt
->lt_clientid
, false, nn
, NULL
);
4463 if ((status
= fh_verify(rqstp
, &cstate
->current_fh
, S_IFREG
, 0)))
4466 inode
= cstate
->current_fh
.fh_dentry
->d_inode
;
4467 file_lock
= locks_alloc_lock();
4469 dprintk("NFSD: %s: unable to allocate lock!\n", __func__
);
4470 status
= nfserr_jukebox
;
4473 locks_init_lock(file_lock
);
4474 switch (lockt
->lt_type
) {
4477 file_lock
->fl_type
= F_RDLCK
;
4480 case NFS4_WRITEW_LT
:
4481 file_lock
->fl_type
= F_WRLCK
;
4484 dprintk("NFSD: nfs4_lockt: bad lock type!\n");
4485 status
= nfserr_inval
;
4489 lo
= find_lockowner_str(inode
, &lockt
->lt_clientid
, &lockt
->lt_owner
, nn
);
4491 file_lock
->fl_owner
= (fl_owner_t
)lo
;
4492 file_lock
->fl_pid
= current
->tgid
;
4493 file_lock
->fl_flags
= FL_POSIX
;
4495 file_lock
->fl_start
= lockt
->lt_offset
;
4496 file_lock
->fl_end
= last_byte_offset(lockt
->lt_offset
, lockt
->lt_length
);
4498 nfs4_transform_lock_offset(file_lock
);
4500 status
= nfsd_test_lock(rqstp
, &cstate
->current_fh
, file_lock
);
4504 if (file_lock
->fl_type
!= F_UNLCK
) {
4505 status
= nfserr_denied
;
4506 nfs4_set_lock_denied(file_lock
, &lockt
->lt_denied
);
4509 nfs4_unlock_state();
4511 locks_free_lock(file_lock
);
4516 nfsd4_locku(struct svc_rqst
*rqstp
, struct nfsd4_compound_state
*cstate
,
4517 struct nfsd4_locku
*locku
)
4519 struct nfs4_ol_stateid
*stp
;
4520 struct file
*filp
= NULL
;
4521 struct file_lock
*file_lock
= NULL
;
4524 struct nfsd_net
*nn
= net_generic(SVC_NET(rqstp
), nfsd_net_id
);
4526 dprintk("NFSD: nfsd4_locku: start=%Ld length=%Ld\n",
4527 (long long) locku
->lu_offset
,
4528 (long long) locku
->lu_length
);
4530 if (check_lock_length(locku
->lu_offset
, locku
->lu_length
))
4531 return nfserr_inval
;
4535 status
= nfs4_preprocess_seqid_op(cstate
, locku
->lu_seqid
,
4536 &locku
->lu_stateid
, NFS4_LOCK_STID
,
4540 filp
= find_any_file(stp
->st_file
);
4542 status
= nfserr_lock_range
;
4545 file_lock
= locks_alloc_lock();
4547 dprintk("NFSD: %s: unable to allocate lock!\n", __func__
);
4548 status
= nfserr_jukebox
;
4551 locks_init_lock(file_lock
);
4552 file_lock
->fl_type
= F_UNLCK
;
4553 file_lock
->fl_owner
= (fl_owner_t
)lockowner(stp
->st_stateowner
);
4554 file_lock
->fl_pid
= current
->tgid
;
4555 file_lock
->fl_file
= filp
;
4556 file_lock
->fl_flags
= FL_POSIX
;
4557 file_lock
->fl_lmops
= &nfsd_posix_mng_ops
;
4558 file_lock
->fl_start
= locku
->lu_offset
;
4560 file_lock
->fl_end
= last_byte_offset(locku
->lu_offset
,
4562 nfs4_transform_lock_offset(file_lock
);
4564 err
= vfs_lock_file(filp
, F_SETLK
, file_lock
, NULL
);
4566 dprintk("NFSD: nfs4_locku: vfs_lock_file failed!\n");
4569 update_stateid(&stp
->st_stid
.sc_stateid
);
4570 memcpy(&locku
->lu_stateid
, &stp
->st_stid
.sc_stateid
, sizeof(stateid_t
));
4573 nfsd4_bump_seqid(cstate
, status
);
4574 if (!cstate
->replay_owner
)
4575 nfs4_unlock_state();
4577 locks_free_lock(file_lock
);
4581 status
= nfserrno(err
);
4587 * 1: locks held by lockowner
4588 * 0: no locks held by lockowner
4591 check_for_locks(struct nfs4_file
*filp
, struct nfs4_lockowner
*lowner
)
4593 struct file_lock
**flpp
;
4594 struct inode
*inode
= filp
->fi_inode
;
4597 spin_lock(&inode
->i_lock
);
4598 for (flpp
= &inode
->i_flock
; *flpp
!= NULL
; flpp
= &(*flpp
)->fl_next
) {
4599 if ((*flpp
)->fl_owner
== (fl_owner_t
)lowner
) {
4605 spin_unlock(&inode
->i_lock
);
4610 nfsd4_release_lockowner(struct svc_rqst
*rqstp
,
4611 struct nfsd4_compound_state
*cstate
,
4612 struct nfsd4_release_lockowner
*rlockowner
)
4614 clientid_t
*clid
= &rlockowner
->rl_clientid
;
4615 struct nfs4_stateowner
*sop
;
4616 struct nfs4_lockowner
*lo
;
4617 struct nfs4_ol_stateid
*stp
;
4618 struct xdr_netobj
*owner
= &rlockowner
->rl_owner
;
4619 struct list_head matches
;
4620 unsigned int hashval
= ownerstr_hashval(clid
->cl_id
, owner
);
4622 struct nfsd_net
*nn
= net_generic(SVC_NET(rqstp
), nfsd_net_id
);
4624 dprintk("nfsd4_release_lockowner clientid: (%08x/%08x):\n",
4625 clid
->cl_boot
, clid
->cl_id
);
4629 status
= lookup_clientid(clid
, cstate
->minorversion
, nn
, NULL
);
4633 status
= nfserr_locks_held
;
4634 INIT_LIST_HEAD(&matches
);
4636 list_for_each_entry(sop
, &nn
->ownerstr_hashtbl
[hashval
], so_strhash
) {
4637 if (sop
->so_is_open_owner
)
4639 if (!same_owner_str(sop
, owner
, clid
))
4641 list_for_each_entry(stp
, &sop
->so_stateids
,
4643 lo
= lockowner(sop
);
4644 if (check_for_locks(stp
->st_file
, lo
))
4646 list_add(&lo
->lo_list
, &matches
);
4649 /* Clients probably won't expect us to return with some (but not all)
4650 * of the lockowner state released; so don't release any until all
4651 * have been checked. */
4653 while (!list_empty(&matches
)) {
4654 lo
= list_entry(matches
.next
, struct nfs4_lockowner
,
4656 /* unhash_stateowner deletes so_perclient only
4657 * for openowners. */
4658 list_del(&lo
->lo_list
);
4659 release_lockowner(lo
);
4662 nfs4_unlock_state();
4666 static inline struct nfs4_client_reclaim
*
4669 return kmalloc(sizeof(struct nfs4_client_reclaim
), GFP_KERNEL
);
4673 nfs4_has_reclaimed_state(const char *name
, struct nfsd_net
*nn
)
4675 struct nfs4_client_reclaim
*crp
;
4677 crp
= nfsd4_find_reclaim_client(name
, nn
);
4678 return (crp
&& crp
->cr_clp
);
4682 * failure => all reset bets are off, nfserr_no_grace...
4684 struct nfs4_client_reclaim
*
4685 nfs4_client_to_reclaim(const char *name
, struct nfsd_net
*nn
)
4687 unsigned int strhashval
;
4688 struct nfs4_client_reclaim
*crp
;
4690 dprintk("NFSD nfs4_client_to_reclaim NAME: %.*s\n", HEXDIR_LEN
, name
);
4691 crp
= alloc_reclaim();
4693 strhashval
= clientstr_hashval(name
);
4694 INIT_LIST_HEAD(&crp
->cr_strhash
);
4695 list_add(&crp
->cr_strhash
, &nn
->reclaim_str_hashtbl
[strhashval
]);
4696 memcpy(crp
->cr_recdir
, name
, HEXDIR_LEN
);
4698 nn
->reclaim_str_hashtbl_size
++;
4704 nfs4_remove_reclaim_record(struct nfs4_client_reclaim
*crp
, struct nfsd_net
*nn
)
4706 list_del(&crp
->cr_strhash
);
4708 nn
->reclaim_str_hashtbl_size
--;
4712 nfs4_release_reclaim(struct nfsd_net
*nn
)
4714 struct nfs4_client_reclaim
*crp
= NULL
;
4717 for (i
= 0; i
< CLIENT_HASH_SIZE
; i
++) {
4718 while (!list_empty(&nn
->reclaim_str_hashtbl
[i
])) {
4719 crp
= list_entry(nn
->reclaim_str_hashtbl
[i
].next
,
4720 struct nfs4_client_reclaim
, cr_strhash
);
4721 nfs4_remove_reclaim_record(crp
, nn
);
4724 WARN_ON_ONCE(nn
->reclaim_str_hashtbl_size
);
4728 * called from OPEN, CLAIM_PREVIOUS with a new clientid. */
4729 struct nfs4_client_reclaim
*
4730 nfsd4_find_reclaim_client(const char *recdir
, struct nfsd_net
*nn
)
4732 unsigned int strhashval
;
4733 struct nfs4_client_reclaim
*crp
= NULL
;
4735 dprintk("NFSD: nfs4_find_reclaim_client for recdir %s\n", recdir
);
4737 strhashval
= clientstr_hashval(recdir
);
4738 list_for_each_entry(crp
, &nn
->reclaim_str_hashtbl
[strhashval
], cr_strhash
) {
4739 if (same_name(crp
->cr_recdir
, recdir
)) {
4747 * Called from OPEN. Look for clientid in reclaim list.
4750 nfs4_check_open_reclaim(clientid_t
*clid
, bool sessions
, struct nfsd_net
*nn
)
4752 struct nfs4_client
*clp
;
4754 /* find clientid in conf_id_hashtbl */
4755 clp
= find_confirmed_client(clid
, sessions
, nn
);
4757 return nfserr_reclaim_bad
;
4759 return nfsd4_client_record_check(clp
) ? nfserr_reclaim_bad
: nfs_ok
;
4762 #ifdef CONFIG_NFSD_FAULT_INJECTION
4764 u64
nfsd_forget_client(struct nfs4_client
*clp
, u64 max
)
4766 if (mark_client_expired(clp
))
4772 u64
nfsd_print_client(struct nfs4_client
*clp
, u64 num
)
4774 char buf
[INET6_ADDRSTRLEN
];
4775 rpc_ntop((struct sockaddr
*)&clp
->cl_addr
, buf
, sizeof(buf
));
4776 printk(KERN_INFO
"NFS Client: %s\n", buf
);
4780 static void nfsd_print_count(struct nfs4_client
*clp
, unsigned int count
,
4783 char buf
[INET6_ADDRSTRLEN
];
4784 rpc_ntop((struct sockaddr
*)&clp
->cl_addr
, buf
, sizeof(buf
));
4785 printk(KERN_INFO
"NFS Client: %s has %u %s\n", buf
, count
, type
);
4788 static u64
nfsd_foreach_client_lock(struct nfs4_client
*clp
, u64 max
, void (*func
)(struct nfs4_lockowner
*))
4790 struct nfs4_openowner
*oop
;
4791 struct nfs4_lockowner
*lop
, *lo_next
;
4792 struct nfs4_ol_stateid
*stp
, *st_next
;
4795 list_for_each_entry(oop
, &clp
->cl_openowners
, oo_perclient
) {
4796 list_for_each_entry_safe(stp
, st_next
, &oop
->oo_owner
.so_stateids
, st_perstateowner
) {
4797 list_for_each_entry_safe(lop
, lo_next
, &stp
->st_lockowners
, lo_perstateid
) {
4809 u64
nfsd_forget_client_locks(struct nfs4_client
*clp
, u64 max
)
4811 return nfsd_foreach_client_lock(clp
, max
, release_lockowner
);
4814 u64
nfsd_print_client_locks(struct nfs4_client
*clp
, u64 max
)
4816 u64 count
= nfsd_foreach_client_lock(clp
, max
, NULL
);
4817 nfsd_print_count(clp
, count
, "locked files");
4821 static u64
nfsd_foreach_client_open(struct nfs4_client
*clp
, u64 max
, void (*func
)(struct nfs4_openowner
*))
4823 struct nfs4_openowner
*oop
, *next
;
4826 list_for_each_entry_safe(oop
, next
, &clp
->cl_openowners
, oo_perclient
) {
4836 u64
nfsd_forget_client_openowners(struct nfs4_client
*clp
, u64 max
)
4838 return nfsd_foreach_client_open(clp
, max
, release_openowner
);
4841 u64
nfsd_print_client_openowners(struct nfs4_client
*clp
, u64 max
)
4843 u64 count
= nfsd_foreach_client_open(clp
, max
, NULL
);
4844 nfsd_print_count(clp
, count
, "open files");
4848 static u64
nfsd_find_all_delegations(struct nfs4_client
*clp
, u64 max
,
4849 struct list_head
*victims
)
4851 struct nfs4_delegation
*dp
, *next
;
4854 list_for_each_entry_safe(dp
, next
, &clp
->cl_delegations
, dl_perclnt
) {
4856 list_move(&dp
->dl_recall_lru
, victims
);
4863 u64
nfsd_forget_client_delegations(struct nfs4_client
*clp
, u64 max
)
4865 struct nfs4_delegation
*dp
, *next
;
4869 spin_lock(&recall_lock
);
4870 count
= nfsd_find_all_delegations(clp
, max
, &victims
);
4871 spin_unlock(&recall_lock
);
4873 list_for_each_entry_safe(dp
, next
, &victims
, dl_recall_lru
)
4874 revoke_delegation(dp
);
4879 u64
nfsd_recall_client_delegations(struct nfs4_client
*clp
, u64 max
)
4881 struct nfs4_delegation
*dp
, *next
;
4885 spin_lock(&recall_lock
);
4886 count
= nfsd_find_all_delegations(clp
, max
, &victims
);
4887 list_for_each_entry_safe(dp
, next
, &victims
, dl_recall_lru
)
4888 nfsd_break_one_deleg(dp
);
4889 spin_unlock(&recall_lock
);
4894 u64
nfsd_print_client_delegations(struct nfs4_client
*clp
, u64 max
)
4898 spin_lock(&recall_lock
);
4899 count
= nfsd_find_all_delegations(clp
, max
, NULL
);
4900 spin_unlock(&recall_lock
);
4902 nfsd_print_count(clp
, count
, "delegations");
4906 u64
nfsd_for_n_state(u64 max
, u64 (*func
)(struct nfs4_client
*, u64
))
4908 struct nfs4_client
*clp
, *next
;
4910 struct nfsd_net
*nn
= net_generic(current
->nsproxy
->net_ns
, nfsd_net_id
);
4912 if (!nfsd_netns_ready(nn
))
4915 list_for_each_entry_safe(clp
, next
, &nn
->client_lru
, cl_lru
) {
4916 count
+= func(clp
, max
- count
);
4917 if ((max
!= 0) && (count
>= max
))
4924 struct nfs4_client
*nfsd_find_client(struct sockaddr_storage
*addr
, size_t addr_size
)
4926 struct nfs4_client
*clp
;
4927 struct nfsd_net
*nn
= net_generic(current
->nsproxy
->net_ns
, nfsd_net_id
);
4929 if (!nfsd_netns_ready(nn
))
4932 list_for_each_entry(clp
, &nn
->client_lru
, cl_lru
) {
4933 if (memcmp(&clp
->cl_addr
, addr
, addr_size
) == 0)
4939 #endif /* CONFIG_NFSD_FAULT_INJECTION */
4941 /* initialization to perform at module load time: */
4944 nfs4_state_init(void)
4949 * Since the lifetime of a delegation isn't limited to that of an open, a
4950 * client may quite reasonably hang on to a delegation as long as it has
4951 * the inode cached. This becomes an obvious problem the first time a
4952 * client's inode cache approaches the size of the server's total memory.
4954 * For now we avoid this problem by imposing a hard limit on the number
4955 * of delegations, which varies according to the server's memory size.
4958 set_max_delegations(void)
4961 * Allow at most 4 delegations per megabyte of RAM. Quick
4962 * estimates suggest that in the worst case (where every delegation
4963 * is for a different inode), a delegation could take about 1.5K,
4964 * giving a worst case usage of about 6% of memory.
4966 max_delegations
= nr_free_buffer_pages() >> (20 - 2 - PAGE_SHIFT
);
4969 static int nfs4_state_create_net(struct net
*net
)
4971 struct nfsd_net
*nn
= net_generic(net
, nfsd_net_id
);
4974 nn
->conf_id_hashtbl
= kmalloc(sizeof(struct list_head
) *
4975 CLIENT_HASH_SIZE
, GFP_KERNEL
);
4976 if (!nn
->conf_id_hashtbl
)
4978 nn
->unconf_id_hashtbl
= kmalloc(sizeof(struct list_head
) *
4979 CLIENT_HASH_SIZE
, GFP_KERNEL
);
4980 if (!nn
->unconf_id_hashtbl
)
4982 nn
->ownerstr_hashtbl
= kmalloc(sizeof(struct list_head
) *
4983 OWNER_HASH_SIZE
, GFP_KERNEL
);
4984 if (!nn
->ownerstr_hashtbl
)
4986 nn
->lockowner_ino_hashtbl
= kmalloc(sizeof(struct list_head
) *
4987 LOCKOWNER_INO_HASH_SIZE
, GFP_KERNEL
);
4988 if (!nn
->lockowner_ino_hashtbl
)
4989 goto err_lockowner_ino
;
4990 nn
->sessionid_hashtbl
= kmalloc(sizeof(struct list_head
) *
4991 SESSION_HASH_SIZE
, GFP_KERNEL
);
4992 if (!nn
->sessionid_hashtbl
)
4995 for (i
= 0; i
< CLIENT_HASH_SIZE
; i
++) {
4996 INIT_LIST_HEAD(&nn
->conf_id_hashtbl
[i
]);
4997 INIT_LIST_HEAD(&nn
->unconf_id_hashtbl
[i
]);
4999 for (i
= 0; i
< OWNER_HASH_SIZE
; i
++)
5000 INIT_LIST_HEAD(&nn
->ownerstr_hashtbl
[i
]);
5001 for (i
= 0; i
< LOCKOWNER_INO_HASH_SIZE
; i
++)
5002 INIT_LIST_HEAD(&nn
->lockowner_ino_hashtbl
[i
]);
5003 for (i
= 0; i
< SESSION_HASH_SIZE
; i
++)
5004 INIT_LIST_HEAD(&nn
->sessionid_hashtbl
[i
]);
5005 nn
->conf_name_tree
= RB_ROOT
;
5006 nn
->unconf_name_tree
= RB_ROOT
;
5007 INIT_LIST_HEAD(&nn
->client_lru
);
5008 INIT_LIST_HEAD(&nn
->close_lru
);
5009 INIT_LIST_HEAD(&nn
->del_recall_lru
);
5010 spin_lock_init(&nn
->client_lock
);
5012 INIT_DELAYED_WORK(&nn
->laundromat_work
, laundromat_main
);
5018 kfree(nn
->lockowner_ino_hashtbl
);
5020 kfree(nn
->ownerstr_hashtbl
);
5022 kfree(nn
->unconf_id_hashtbl
);
5024 kfree(nn
->conf_id_hashtbl
);
5030 nfs4_state_destroy_net(struct net
*net
)
5033 struct nfs4_client
*clp
= NULL
;
5034 struct nfsd_net
*nn
= net_generic(net
, nfsd_net_id
);
5035 struct rb_node
*node
, *tmp
;
5037 for (i
= 0; i
< CLIENT_HASH_SIZE
; i
++) {
5038 while (!list_empty(&nn
->conf_id_hashtbl
[i
])) {
5039 clp
= list_entry(nn
->conf_id_hashtbl
[i
].next
, struct nfs4_client
, cl_idhash
);
5040 destroy_client(clp
);
5044 node
= rb_first(&nn
->unconf_name_tree
);
5045 while (node
!= NULL
) {
5047 node
= rb_next(tmp
);
5048 clp
= rb_entry(tmp
, struct nfs4_client
, cl_namenode
);
5049 rb_erase(tmp
, &nn
->unconf_name_tree
);
5050 destroy_client(clp
);
5053 kfree(nn
->sessionid_hashtbl
);
5054 kfree(nn
->lockowner_ino_hashtbl
);
5055 kfree(nn
->ownerstr_hashtbl
);
5056 kfree(nn
->unconf_id_hashtbl
);
5057 kfree(nn
->conf_id_hashtbl
);
5062 nfs4_state_start_net(struct net
*net
)
5064 struct nfsd_net
*nn
= net_generic(net
, nfsd_net_id
);
5067 ret
= nfs4_state_create_net(net
);
5070 nfsd4_client_tracking_init(net
);
5071 nn
->boot_time
= get_seconds();
5072 locks_start_grace(net
, &nn
->nfsd4_manager
);
5073 nn
->grace_ended
= false;
5074 printk(KERN_INFO
"NFSD: starting %ld-second grace period (net %p)\n",
5075 nn
->nfsd4_grace
, net
);
5076 queue_delayed_work(laundry_wq
, &nn
->laundromat_work
, nn
->nfsd4_grace
* HZ
);
5080 /* initialization to perform when the nfsd service is started: */
5083 nfs4_state_start(void)
5087 ret
= set_callback_cred();
5090 laundry_wq
= create_singlethread_workqueue("nfsd4");
5091 if (laundry_wq
== NULL
) {
5095 ret
= nfsd4_create_callback_queue();
5097 goto out_free_laundry
;
5099 set_max_delegations();
5104 destroy_workqueue(laundry_wq
);
5109 /* should be called with the state lock held */
5111 nfs4_state_shutdown_net(struct net
*net
)
5113 struct nfs4_delegation
*dp
= NULL
;
5114 struct list_head
*pos
, *next
, reaplist
;
5115 struct nfsd_net
*nn
= net_generic(net
, nfsd_net_id
);
5117 cancel_delayed_work_sync(&nn
->laundromat_work
);
5118 locks_end_grace(&nn
->nfsd4_manager
);
5120 INIT_LIST_HEAD(&reaplist
);
5121 spin_lock(&recall_lock
);
5122 list_for_each_safe(pos
, next
, &nn
->del_recall_lru
) {
5123 dp
= list_entry (pos
, struct nfs4_delegation
, dl_recall_lru
);
5124 list_move(&dp
->dl_recall_lru
, &reaplist
);
5126 spin_unlock(&recall_lock
);
5127 list_for_each_safe(pos
, next
, &reaplist
) {
5128 dp
= list_entry (pos
, struct nfs4_delegation
, dl_recall_lru
);
5129 destroy_delegation(dp
);
5132 nfsd4_client_tracking_exit(net
);
5133 nfs4_state_destroy_net(net
);
5137 nfs4_state_shutdown(void)
5139 destroy_workqueue(laundry_wq
);
5140 nfsd4_destroy_callback_queue();
5144 get_stateid(struct nfsd4_compound_state
*cstate
, stateid_t
*stateid
)
5146 if (HAS_STATE_ID(cstate
, CURRENT_STATE_ID_FLAG
) && CURRENT_STATEID(stateid
))
5147 memcpy(stateid
, &cstate
->current_stateid
, sizeof(stateid_t
));
5151 put_stateid(struct nfsd4_compound_state
*cstate
, stateid_t
*stateid
)
5153 if (cstate
->minorversion
) {
5154 memcpy(&cstate
->current_stateid
, stateid
, sizeof(stateid_t
));
5155 SET_STATE_ID(cstate
, CURRENT_STATE_ID_FLAG
);
5160 clear_current_stateid(struct nfsd4_compound_state
*cstate
)
5162 CLEAR_STATE_ID(cstate
, CURRENT_STATE_ID_FLAG
);
5166 * functions to set current state id
5169 nfsd4_set_opendowngradestateid(struct nfsd4_compound_state
*cstate
, struct nfsd4_open_downgrade
*odp
)
5171 put_stateid(cstate
, &odp
->od_stateid
);
5175 nfsd4_set_openstateid(struct nfsd4_compound_state
*cstate
, struct nfsd4_open
*open
)
5177 put_stateid(cstate
, &open
->op_stateid
);
5181 nfsd4_set_closestateid(struct nfsd4_compound_state
*cstate
, struct nfsd4_close
*close
)
5183 put_stateid(cstate
, &close
->cl_stateid
);
5187 nfsd4_set_lockstateid(struct nfsd4_compound_state
*cstate
, struct nfsd4_lock
*lock
)
5189 put_stateid(cstate
, &lock
->lk_resp_stateid
);
5193 * functions to consume current state id
5197 nfsd4_get_opendowngradestateid(struct nfsd4_compound_state
*cstate
, struct nfsd4_open_downgrade
*odp
)
5199 get_stateid(cstate
, &odp
->od_stateid
);
5203 nfsd4_get_delegreturnstateid(struct nfsd4_compound_state
*cstate
, struct nfsd4_delegreturn
*drp
)
5205 get_stateid(cstate
, &drp
->dr_stateid
);
5209 nfsd4_get_freestateid(struct nfsd4_compound_state
*cstate
, struct nfsd4_free_stateid
*fsp
)
5211 get_stateid(cstate
, &fsp
->fr_stateid
);
5215 nfsd4_get_setattrstateid(struct nfsd4_compound_state
*cstate
, struct nfsd4_setattr
*setattr
)
5217 get_stateid(cstate
, &setattr
->sa_stateid
);
5221 nfsd4_get_closestateid(struct nfsd4_compound_state
*cstate
, struct nfsd4_close
*close
)
5223 get_stateid(cstate
, &close
->cl_stateid
);
5227 nfsd4_get_lockustateid(struct nfsd4_compound_state
*cstate
, struct nfsd4_locku
*locku
)
5229 get_stateid(cstate
, &locku
->lu_stateid
);
5233 nfsd4_get_readstateid(struct nfsd4_compound_state
*cstate
, struct nfsd4_read
*read
)
5235 get_stateid(cstate
, &read
->rd_stateid
);
5239 nfsd4_get_writestateid(struct nfsd4_compound_state
*cstate
, struct nfsd4_write
*write
)
5241 get_stateid(cstate
, &write
->wr_stateid
);