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 void nfsd4_put_session(struct nfsd4_session
*ses
)
102 atomic_dec(&ses
->se_ref
);
105 static bool is_session_dead(struct nfsd4_session
*ses
)
107 return ses
->se_flags
& NFS4_SESSION_DEAD
;
110 static __be32
mark_session_dead_locked(struct nfsd4_session
*ses
)
112 if (atomic_read(&ses
->se_ref
))
113 return nfserr_jukebox
;
114 ses
->se_flags
|= NFS4_SESSION_DEAD
;
118 static __be32
nfsd4_get_session_locked(struct nfsd4_session
*ses
)
120 if (is_session_dead(ses
))
121 return nfserr_badsession
;
122 atomic_inc(&ses
->se_ref
);
127 nfs4_unlock_state(void)
129 mutex_unlock(&client_mutex
);
132 static bool is_client_expired(struct nfs4_client
*clp
)
134 return clp
->cl_time
== 0;
137 static __be32
mark_client_expired_locked(struct nfs4_client
*clp
)
139 if (atomic_read(&clp
->cl_refcount
))
140 return nfserr_jukebox
;
145 static __be32
mark_client_expired(struct nfs4_client
*clp
)
147 struct nfsd_net
*nn
= net_generic(clp
->net
, nfsd_net_id
);
150 spin_lock(&nn
->client_lock
);
151 ret
= mark_client_expired_locked(clp
);
152 spin_unlock(&nn
->client_lock
);
156 static __be32
get_client_locked(struct nfs4_client
*clp
)
158 if (is_client_expired(clp
))
159 return nfserr_expired
;
160 atomic_inc(&clp
->cl_refcount
);
164 /* must be called under the client_lock */
166 renew_client_locked(struct nfs4_client
*clp
)
168 struct nfsd_net
*nn
= net_generic(clp
->net
, nfsd_net_id
);
170 if (is_client_expired(clp
)) {
172 printk("%s: client (clientid %08x/%08x) already expired\n",
174 clp
->cl_clientid
.cl_boot
,
175 clp
->cl_clientid
.cl_id
);
179 dprintk("renewing client (clientid %08x/%08x)\n",
180 clp
->cl_clientid
.cl_boot
,
181 clp
->cl_clientid
.cl_id
);
182 list_move_tail(&clp
->cl_lru
, &nn
->client_lru
);
183 clp
->cl_time
= get_seconds();
187 renew_client(struct nfs4_client
*clp
)
189 struct nfsd_net
*nn
= net_generic(clp
->net
, nfsd_net_id
);
191 spin_lock(&nn
->client_lock
);
192 renew_client_locked(clp
);
193 spin_unlock(&nn
->client_lock
);
196 static void put_client_renew_locked(struct nfs4_client
*clp
)
198 if (!atomic_dec_and_test(&clp
->cl_refcount
))
200 if (!is_client_expired(clp
))
201 renew_client_locked(clp
);
204 void put_client_renew(struct nfs4_client
*clp
)
206 struct nfsd_net
*nn
= net_generic(clp
->net
, nfsd_net_id
);
208 if (!atomic_dec_and_lock(&clp
->cl_refcount
, &nn
->client_lock
))
210 if (!is_client_expired(clp
))
211 renew_client_locked(clp
);
212 spin_unlock(&nn
->client_lock
);
217 opaque_hashval(const void *ptr
, int nbytes
)
219 unsigned char *cptr
= (unsigned char *) ptr
;
229 static void nfsd4_free_file(struct nfs4_file
*f
)
231 kmem_cache_free(file_slab
, f
);
235 put_nfs4_file(struct nfs4_file
*fi
)
237 if (atomic_dec_and_lock(&fi
->fi_ref
, &recall_lock
)) {
238 hlist_del(&fi
->fi_hash
);
239 spin_unlock(&recall_lock
);
246 get_nfs4_file(struct nfs4_file
*fi
)
248 atomic_inc(&fi
->fi_ref
);
251 static int num_delegations
;
252 unsigned long max_delegations
;
255 * Open owner state (share locks)
258 /* hash tables for lock and open owners */
259 #define OWNER_HASH_BITS 8
260 #define OWNER_HASH_SIZE (1 << OWNER_HASH_BITS)
261 #define OWNER_HASH_MASK (OWNER_HASH_SIZE - 1)
263 static unsigned int ownerstr_hashval(u32 clientid
, struct xdr_netobj
*ownername
)
267 ret
= opaque_hashval(ownername
->data
, ownername
->len
);
269 return ret
& OWNER_HASH_MASK
;
272 /* hash table for nfs4_file */
273 #define FILE_HASH_BITS 8
274 #define FILE_HASH_SIZE (1 << FILE_HASH_BITS)
276 static unsigned int file_hashval(struct inode
*ino
)
278 /* XXX: why are we hashing on inode pointer, anyway? */
279 return hash_ptr(ino
, FILE_HASH_BITS
);
282 static struct hlist_head file_hashtbl
[FILE_HASH_SIZE
];
284 static void __nfs4_file_get_access(struct nfs4_file
*fp
, int oflag
)
286 WARN_ON_ONCE(!(fp
->fi_fds
[oflag
] || fp
->fi_fds
[O_RDWR
]));
287 atomic_inc(&fp
->fi_access
[oflag
]);
290 static void nfs4_file_get_access(struct nfs4_file
*fp
, int oflag
)
292 if (oflag
== O_RDWR
) {
293 __nfs4_file_get_access(fp
, O_RDONLY
);
294 __nfs4_file_get_access(fp
, O_WRONLY
);
296 __nfs4_file_get_access(fp
, oflag
);
299 static void nfs4_file_put_fd(struct nfs4_file
*fp
, int oflag
)
301 if (fp
->fi_fds
[oflag
]) {
302 fput(fp
->fi_fds
[oflag
]);
303 fp
->fi_fds
[oflag
] = NULL
;
307 static void __nfs4_file_put_access(struct nfs4_file
*fp
, int oflag
)
309 if (atomic_dec_and_test(&fp
->fi_access
[oflag
])) {
310 nfs4_file_put_fd(fp
, oflag
);
311 if (atomic_read(&fp
->fi_access
[1 - oflag
]) == 0)
312 nfs4_file_put_fd(fp
, O_RDWR
);
316 static void nfs4_file_put_access(struct nfs4_file
*fp
, int oflag
)
318 if (oflag
== O_RDWR
) {
319 __nfs4_file_put_access(fp
, O_RDONLY
);
320 __nfs4_file_put_access(fp
, O_WRONLY
);
322 __nfs4_file_put_access(fp
, oflag
);
325 static struct nfs4_stid
*nfs4_alloc_stid(struct nfs4_client
*cl
, struct
328 struct idr
*stateids
= &cl
->cl_stateids
;
329 struct nfs4_stid
*stid
;
332 stid
= kmem_cache_alloc(slab
, GFP_KERNEL
);
336 new_id
= idr_alloc_cyclic(stateids
, stid
, 0, 0, GFP_KERNEL
);
339 stid
->sc_client
= cl
;
341 stid
->sc_stateid
.si_opaque
.so_id
= new_id
;
342 stid
->sc_stateid
.si_opaque
.so_clid
= cl
->cl_clientid
;
343 /* Will be incremented before return to client: */
344 stid
->sc_stateid
.si_generation
= 0;
347 * It shouldn't be a problem to reuse an opaque stateid value.
348 * I don't think it is for 4.1. But with 4.0 I worry that, for
349 * example, a stray write retransmission could be accepted by
350 * the server when it should have been rejected. Therefore,
351 * adopt a trick from the sctp code to attempt to maximize the
352 * amount of time until an id is reused, by ensuring they always
353 * "increase" (mod INT_MAX):
357 kmem_cache_free(slab
, stid
);
361 static struct nfs4_ol_stateid
* nfs4_alloc_stateid(struct nfs4_client
*clp
)
363 return openlockstateid(nfs4_alloc_stid(clp
, stateid_slab
));
366 static struct nfs4_delegation
*
367 alloc_init_deleg(struct nfs4_client
*clp
, struct nfs4_ol_stateid
*stp
, struct svc_fh
*current_fh
)
369 struct nfs4_delegation
*dp
;
370 struct nfs4_file
*fp
= stp
->st_file
;
372 dprintk("NFSD alloc_init_deleg\n");
373 if (fp
->fi_had_conflict
)
375 if (num_delegations
> max_delegations
)
377 dp
= delegstateid(nfs4_alloc_stid(clp
, deleg_slab
));
380 dp
->dl_stid
.sc_type
= NFS4_DELEG_STID
;
382 * delegation seqid's are never incremented. The 4.1 special
383 * meaning of seqid 0 isn't meaningful, really, but let's avoid
384 * 0 anyway just for consistency and use 1:
386 dp
->dl_stid
.sc_stateid
.si_generation
= 1;
388 INIT_LIST_HEAD(&dp
->dl_perfile
);
389 INIT_LIST_HEAD(&dp
->dl_perclnt
);
390 INIT_LIST_HEAD(&dp
->dl_recall_lru
);
393 dp
->dl_type
= NFS4_OPEN_DELEGATE_READ
;
394 fh_copy_shallow(&dp
->dl_fh
, ¤t_fh
->fh_handle
);
396 atomic_set(&dp
->dl_count
, 1);
397 nfsd4_init_callback(&dp
->dl_recall
);
401 static void remove_stid(struct nfs4_stid
*s
)
403 struct idr
*stateids
= &s
->sc_client
->cl_stateids
;
405 idr_remove(stateids
, s
->sc_stateid
.si_opaque
.so_id
);
409 nfs4_put_delegation(struct nfs4_delegation
*dp
)
411 if (atomic_dec_and_test(&dp
->dl_count
)) {
412 kmem_cache_free(deleg_slab
, dp
);
417 static void nfs4_put_deleg_lease(struct nfs4_file
*fp
)
419 if (atomic_dec_and_test(&fp
->fi_delegees
)) {
420 vfs_setlease(fp
->fi_deleg_file
, F_UNLCK
, &fp
->fi_lease
);
422 fput(fp
->fi_deleg_file
);
423 fp
->fi_deleg_file
= NULL
;
427 static void unhash_stid(struct nfs4_stid
*s
)
432 /* Called under the state lock. */
434 unhash_delegation(struct nfs4_delegation
*dp
)
436 list_del_init(&dp
->dl_perclnt
);
437 spin_lock(&recall_lock
);
438 list_del_init(&dp
->dl_perfile
);
439 list_del_init(&dp
->dl_recall_lru
);
440 spin_unlock(&recall_lock
);
441 nfs4_put_deleg_lease(dp
->dl_file
);
442 put_nfs4_file(dp
->dl_file
);
448 static void destroy_revoked_delegation(struct nfs4_delegation
*dp
)
450 list_del_init(&dp
->dl_recall_lru
);
451 remove_stid(&dp
->dl_stid
);
452 nfs4_put_delegation(dp
);
455 static void destroy_delegation(struct nfs4_delegation
*dp
)
457 unhash_delegation(dp
);
458 remove_stid(&dp
->dl_stid
);
459 nfs4_put_delegation(dp
);
462 static void revoke_delegation(struct nfs4_delegation
*dp
)
464 struct nfs4_client
*clp
= dp
->dl_stid
.sc_client
;
466 if (clp
->cl_minorversion
== 0)
467 destroy_delegation(dp
);
469 unhash_delegation(dp
);
470 dp
->dl_stid
.sc_type
= NFS4_REVOKED_DELEG_STID
;
471 list_add(&dp
->dl_recall_lru
, &clp
->cl_revoked
);
479 static unsigned int clientid_hashval(u32 id
)
481 return id
& CLIENT_HASH_MASK
;
484 static unsigned int clientstr_hashval(const char *name
)
486 return opaque_hashval(name
, 8) & CLIENT_HASH_MASK
;
490 * We store the NONE, READ, WRITE, and BOTH bits separately in the
491 * st_{access,deny}_bmap field of the stateid, in order to track not
492 * only what share bits are currently in force, but also what
493 * combinations of share bits previous opens have used. This allows us
494 * to enforce the recommendation of rfc 3530 14.2.19 that the server
495 * return an error if the client attempt to downgrade to a combination
496 * of share bits not explicable by closing some of its previous opens.
498 * XXX: This enforcement is actually incomplete, since we don't keep
499 * track of access/deny bit combinations; so, e.g., we allow:
501 * OPEN allow read, deny write
502 * OPEN allow both, deny none
503 * DOWNGRADE allow read, deny none
505 * which we should reject.
508 bmap_to_share_mode(unsigned long bmap
) {
510 unsigned int access
= 0;
512 for (i
= 1; i
< 4; i
++) {
513 if (test_bit(i
, &bmap
))
520 test_share(struct nfs4_ol_stateid
*stp
, struct nfsd4_open
*open
) {
521 unsigned int access
, deny
;
523 access
= bmap_to_share_mode(stp
->st_access_bmap
);
524 deny
= bmap_to_share_mode(stp
->st_deny_bmap
);
525 if ((access
& open
->op_share_deny
) || (deny
& open
->op_share_access
))
530 /* set share access for a given stateid */
532 set_access(u32 access
, struct nfs4_ol_stateid
*stp
)
534 __set_bit(access
, &stp
->st_access_bmap
);
537 /* clear share access for a given stateid */
539 clear_access(u32 access
, struct nfs4_ol_stateid
*stp
)
541 __clear_bit(access
, &stp
->st_access_bmap
);
544 /* test whether a given stateid has access */
546 test_access(u32 access
, struct nfs4_ol_stateid
*stp
)
548 return test_bit(access
, &stp
->st_access_bmap
);
551 /* set share deny for a given stateid */
553 set_deny(u32 access
, struct nfs4_ol_stateid
*stp
)
555 __set_bit(access
, &stp
->st_deny_bmap
);
558 /* clear share deny for a given stateid */
560 clear_deny(u32 access
, struct nfs4_ol_stateid
*stp
)
562 __clear_bit(access
, &stp
->st_deny_bmap
);
565 /* test whether a given stateid is denying specific access */
567 test_deny(u32 access
, struct nfs4_ol_stateid
*stp
)
569 return test_bit(access
, &stp
->st_deny_bmap
);
572 static int nfs4_access_to_omode(u32 access
)
574 switch (access
& NFS4_SHARE_ACCESS_BOTH
) {
575 case NFS4_SHARE_ACCESS_READ
:
577 case NFS4_SHARE_ACCESS_WRITE
:
579 case NFS4_SHARE_ACCESS_BOTH
:
586 /* release all access and file references for a given stateid */
588 release_all_access(struct nfs4_ol_stateid
*stp
)
592 for (i
= 1; i
< 4; i
++) {
593 if (test_access(i
, stp
))
594 nfs4_file_put_access(stp
->st_file
,
595 nfs4_access_to_omode(i
));
596 clear_access(i
, stp
);
600 static void unhash_generic_stateid(struct nfs4_ol_stateid
*stp
)
602 list_del(&stp
->st_perfile
);
603 list_del(&stp
->st_perstateowner
);
606 static void close_generic_stateid(struct nfs4_ol_stateid
*stp
)
608 release_all_access(stp
);
609 put_nfs4_file(stp
->st_file
);
613 static void free_generic_stateid(struct nfs4_ol_stateid
*stp
)
615 remove_stid(&stp
->st_stid
);
616 kmem_cache_free(stateid_slab
, stp
);
619 static void release_lock_stateid(struct nfs4_ol_stateid
*stp
)
623 unhash_generic_stateid(stp
);
624 unhash_stid(&stp
->st_stid
);
625 file
= find_any_file(stp
->st_file
);
627 locks_remove_posix(file
, (fl_owner_t
)lockowner(stp
->st_stateowner
));
628 close_generic_stateid(stp
);
629 free_generic_stateid(stp
);
632 static void unhash_lockowner(struct nfs4_lockowner
*lo
)
634 struct nfs4_ol_stateid
*stp
;
636 list_del(&lo
->lo_owner
.so_strhash
);
637 list_del(&lo
->lo_perstateid
);
638 list_del(&lo
->lo_owner_ino_hash
);
639 while (!list_empty(&lo
->lo_owner
.so_stateids
)) {
640 stp
= list_first_entry(&lo
->lo_owner
.so_stateids
,
641 struct nfs4_ol_stateid
, st_perstateowner
);
642 release_lock_stateid(stp
);
646 static void release_lockowner(struct nfs4_lockowner
*lo
)
648 unhash_lockowner(lo
);
649 nfs4_free_lockowner(lo
);
653 release_stateid_lockowners(struct nfs4_ol_stateid
*open_stp
)
655 struct nfs4_lockowner
*lo
;
657 while (!list_empty(&open_stp
->st_lockowners
)) {
658 lo
= list_entry(open_stp
->st_lockowners
.next
,
659 struct nfs4_lockowner
, lo_perstateid
);
660 release_lockowner(lo
);
664 static void unhash_open_stateid(struct nfs4_ol_stateid
*stp
)
666 unhash_generic_stateid(stp
);
667 release_stateid_lockowners(stp
);
668 close_generic_stateid(stp
);
671 static void release_open_stateid(struct nfs4_ol_stateid
*stp
)
673 unhash_open_stateid(stp
);
674 unhash_stid(&stp
->st_stid
);
675 free_generic_stateid(stp
);
678 static void unhash_openowner(struct nfs4_openowner
*oo
)
680 struct nfs4_ol_stateid
*stp
;
682 list_del(&oo
->oo_owner
.so_strhash
);
683 list_del(&oo
->oo_perclient
);
684 while (!list_empty(&oo
->oo_owner
.so_stateids
)) {
685 stp
= list_first_entry(&oo
->oo_owner
.so_stateids
,
686 struct nfs4_ol_stateid
, st_perstateowner
);
687 release_open_stateid(stp
);
691 static void release_last_closed_stateid(struct nfs4_openowner
*oo
)
693 struct nfs4_ol_stateid
*s
= oo
->oo_last_closed_stid
;
696 unhash_stid(&s
->st_stid
);
697 free_generic_stateid(s
);
698 oo
->oo_last_closed_stid
= NULL
;
702 static void release_openowner(struct nfs4_openowner
*oo
)
704 unhash_openowner(oo
);
705 list_del(&oo
->oo_close_lru
);
706 release_last_closed_stateid(oo
);
707 nfs4_free_openowner(oo
);
711 hash_sessionid(struct nfs4_sessionid
*sessionid
)
713 struct nfsd4_sessionid
*sid
= (struct nfsd4_sessionid
*)sessionid
;
715 return sid
->sequence
% SESSION_HASH_SIZE
;
720 dump_sessionid(const char *fn
, struct nfs4_sessionid
*sessionid
)
722 u32
*ptr
= (u32
*)(&sessionid
->data
[0]);
723 dprintk("%s: %u:%u:%u:%u\n", fn
, ptr
[0], ptr
[1], ptr
[2], ptr
[3]);
727 dump_sessionid(const char *fn
, struct nfs4_sessionid
*sessionid
)
733 * Bump the seqid on cstate->replay_owner, and clear replay_owner if it
734 * won't be used for replay.
736 void nfsd4_bump_seqid(struct nfsd4_compound_state
*cstate
, __be32 nfserr
)
738 struct nfs4_stateowner
*so
= cstate
->replay_owner
;
740 if (nfserr
== nfserr_replay_me
)
743 if (!seqid_mutating_err(ntohl(nfserr
))) {
744 cstate
->replay_owner
= NULL
;
749 if (so
->so_is_open_owner
)
750 release_last_closed_stateid(openowner(so
));
756 gen_sessionid(struct nfsd4_session
*ses
)
758 struct nfs4_client
*clp
= ses
->se_client
;
759 struct nfsd4_sessionid
*sid
;
761 sid
= (struct nfsd4_sessionid
*)ses
->se_sessionid
.data
;
762 sid
->clientid
= clp
->cl_clientid
;
763 sid
->sequence
= current_sessionid
++;
768 * The protocol defines ca_maxresponssize_cached to include the size of
769 * the rpc header, but all we need to cache is the data starting after
770 * the end of the initial SEQUENCE operation--the rest we regenerate
771 * each time. Therefore we can advertise a ca_maxresponssize_cached
772 * value that is the number of bytes in our cache plus a few additional
773 * bytes. In order to stay on the safe side, and not promise more than
774 * we can cache, those additional bytes must be the minimum possible: 24
775 * bytes of rpc header (xid through accept state, with AUTH_NULL
776 * verifier), 12 for the compound header (with zero-length tag), and 44
777 * for the SEQUENCE op response:
779 #define NFSD_MIN_HDR_SEQ_SZ (24 + 12 + 44)
782 free_session_slots(struct nfsd4_session
*ses
)
786 for (i
= 0; i
< ses
->se_fchannel
.maxreqs
; i
++)
787 kfree(ses
->se_slots
[i
]);
791 * We don't actually need to cache the rpc and session headers, so we
792 * can allocate a little less for each slot:
794 static inline u32
slot_bytes(struct nfsd4_channel_attrs
*ca
)
798 if (ca
->maxresp_cached
< NFSD_MIN_HDR_SEQ_SZ
)
801 size
= ca
->maxresp_cached
- NFSD_MIN_HDR_SEQ_SZ
;
802 return size
+ sizeof(struct nfsd4_slot
);
806 * XXX: If we run out of reserved DRC memory we could (up to a point)
807 * re-negotiate active sessions and reduce their slot usage to make
808 * room for new connections. For now we just fail the create session.
810 static u32
nfsd4_get_drc_mem(struct nfsd4_channel_attrs
*ca
)
812 u32 slotsize
= slot_bytes(ca
);
813 u32 num
= ca
->maxreqs
;
816 spin_lock(&nfsd_drc_lock
);
817 avail
= min((unsigned long)NFSD_MAX_MEM_PER_SESSION
,
818 nfsd_drc_max_mem
- nfsd_drc_mem_used
);
819 num
= min_t(int, num
, avail
/ slotsize
);
820 nfsd_drc_mem_used
+= num
* slotsize
;
821 spin_unlock(&nfsd_drc_lock
);
826 static void nfsd4_put_drc_mem(struct nfsd4_channel_attrs
*ca
)
828 int slotsize
= slot_bytes(ca
);
830 spin_lock(&nfsd_drc_lock
);
831 nfsd_drc_mem_used
-= slotsize
* ca
->maxreqs
;
832 spin_unlock(&nfsd_drc_lock
);
835 static struct nfsd4_session
*alloc_session(struct nfsd4_channel_attrs
*attrs
)
837 int numslots
= attrs
->maxreqs
;
838 int slotsize
= slot_bytes(attrs
);
839 struct nfsd4_session
*new;
842 BUILD_BUG_ON(NFSD_MAX_SLOTS_PER_SESSION
* sizeof(struct nfsd4_slot
*)
843 + sizeof(struct nfsd4_session
) > PAGE_SIZE
);
844 mem
= numslots
* sizeof(struct nfsd4_slot
*);
846 new = kzalloc(sizeof(*new) + mem
, GFP_KERNEL
);
849 /* allocate each struct nfsd4_slot and data cache in one piece */
850 for (i
= 0; i
< numslots
; i
++) {
851 new->se_slots
[i
] = kzalloc(slotsize
, GFP_KERNEL
);
852 if (!new->se_slots
[i
])
858 kfree(new->se_slots
[i
]);
863 static void free_conn(struct nfsd4_conn
*c
)
865 svc_xprt_put(c
->cn_xprt
);
869 static void nfsd4_conn_lost(struct svc_xpt_user
*u
)
871 struct nfsd4_conn
*c
= container_of(u
, struct nfsd4_conn
, cn_xpt_user
);
872 struct nfs4_client
*clp
= c
->cn_session
->se_client
;
874 spin_lock(&clp
->cl_lock
);
875 if (!list_empty(&c
->cn_persession
)) {
876 list_del(&c
->cn_persession
);
879 nfsd4_probe_callback(clp
);
880 spin_unlock(&clp
->cl_lock
);
883 static struct nfsd4_conn
*alloc_conn(struct svc_rqst
*rqstp
, u32 flags
)
885 struct nfsd4_conn
*conn
;
887 conn
= kmalloc(sizeof(struct nfsd4_conn
), GFP_KERNEL
);
890 svc_xprt_get(rqstp
->rq_xprt
);
891 conn
->cn_xprt
= rqstp
->rq_xprt
;
892 conn
->cn_flags
= flags
;
893 INIT_LIST_HEAD(&conn
->cn_xpt_user
.list
);
897 static void __nfsd4_hash_conn(struct nfsd4_conn
*conn
, struct nfsd4_session
*ses
)
899 conn
->cn_session
= ses
;
900 list_add(&conn
->cn_persession
, &ses
->se_conns
);
903 static void nfsd4_hash_conn(struct nfsd4_conn
*conn
, struct nfsd4_session
*ses
)
905 struct nfs4_client
*clp
= ses
->se_client
;
907 spin_lock(&clp
->cl_lock
);
908 __nfsd4_hash_conn(conn
, ses
);
909 spin_unlock(&clp
->cl_lock
);
912 static int nfsd4_register_conn(struct nfsd4_conn
*conn
)
914 conn
->cn_xpt_user
.callback
= nfsd4_conn_lost
;
915 return register_xpt_user(conn
->cn_xprt
, &conn
->cn_xpt_user
);
918 static void nfsd4_init_conn(struct svc_rqst
*rqstp
, struct nfsd4_conn
*conn
, struct nfsd4_session
*ses
)
922 nfsd4_hash_conn(conn
, ses
);
923 ret
= nfsd4_register_conn(conn
);
925 /* oops; xprt is already down: */
926 nfsd4_conn_lost(&conn
->cn_xpt_user
);
927 if (conn
->cn_flags
& NFS4_CDFC4_BACK
) {
928 /* callback channel may be back up */
929 nfsd4_probe_callback(ses
->se_client
);
933 static struct nfsd4_conn
*alloc_conn_from_crses(struct svc_rqst
*rqstp
, struct nfsd4_create_session
*cses
)
935 u32 dir
= NFS4_CDFC4_FORE
;
937 if (cses
->flags
& SESSION4_BACK_CHAN
)
938 dir
|= NFS4_CDFC4_BACK
;
939 return alloc_conn(rqstp
, dir
);
942 /* must be called under client_lock */
943 static void nfsd4_del_conns(struct nfsd4_session
*s
)
945 struct nfs4_client
*clp
= s
->se_client
;
946 struct nfsd4_conn
*c
;
948 spin_lock(&clp
->cl_lock
);
949 while (!list_empty(&s
->se_conns
)) {
950 c
= list_first_entry(&s
->se_conns
, struct nfsd4_conn
, cn_persession
);
951 list_del_init(&c
->cn_persession
);
952 spin_unlock(&clp
->cl_lock
);
954 unregister_xpt_user(c
->cn_xprt
, &c
->cn_xpt_user
);
957 spin_lock(&clp
->cl_lock
);
959 spin_unlock(&clp
->cl_lock
);
962 static void __free_session(struct nfsd4_session
*ses
)
964 free_session_slots(ses
);
968 static void free_session(struct nfsd4_session
*ses
)
970 struct nfsd_net
*nn
= net_generic(ses
->se_client
->net
, nfsd_net_id
);
972 lockdep_assert_held(&nn
->client_lock
);
973 nfsd4_del_conns(ses
);
974 nfsd4_put_drc_mem(&ses
->se_fchannel
);
978 static void init_session(struct svc_rqst
*rqstp
, struct nfsd4_session
*new, struct nfs4_client
*clp
, struct nfsd4_create_session
*cses
)
981 struct nfsd_net
*nn
= net_generic(SVC_NET(rqstp
), nfsd_net_id
);
983 new->se_client
= clp
;
986 INIT_LIST_HEAD(&new->se_conns
);
988 new->se_cb_seq_nr
= 1;
989 new->se_flags
= cses
->flags
;
990 new->se_cb_prog
= cses
->callback_prog
;
991 new->se_cb_sec
= cses
->cb_sec
;
992 atomic_set(&new->se_ref
, 0);
993 idx
= hash_sessionid(&new->se_sessionid
);
994 spin_lock(&nn
->client_lock
);
995 list_add(&new->se_hash
, &nn
->sessionid_hashtbl
[idx
]);
996 spin_lock(&clp
->cl_lock
);
997 list_add(&new->se_perclnt
, &clp
->cl_sessions
);
998 spin_unlock(&clp
->cl_lock
);
999 spin_unlock(&nn
->client_lock
);
1000 memcpy(&new->se_fchannel
, &cses
->fore_channel
,
1001 sizeof(struct nfsd4_channel_attrs
));
1002 if (cses
->flags
& SESSION4_BACK_CHAN
) {
1003 struct sockaddr
*sa
= svc_addr(rqstp
);
1005 * This is a little silly; with sessions there's no real
1006 * use for the callback address. Use the peer address
1007 * as a reasonable default for now, but consider fixing
1008 * the rpc client not to require an address in the
1011 rpc_copy_addr((struct sockaddr
*)&clp
->cl_cb_conn
.cb_addr
, sa
);
1012 clp
->cl_cb_conn
.cb_addrlen
= svc_addr_len(sa
);
1016 /* caller must hold client_lock */
1017 static struct nfsd4_session
*
1018 find_in_sessionid_hashtbl(struct nfs4_sessionid
*sessionid
, struct net
*net
)
1020 struct nfsd4_session
*elem
;
1022 struct nfsd_net
*nn
= net_generic(net
, nfsd_net_id
);
1024 dump_sessionid(__func__
, sessionid
);
1025 idx
= hash_sessionid(sessionid
);
1026 /* Search in the appropriate list */
1027 list_for_each_entry(elem
, &nn
->sessionid_hashtbl
[idx
], se_hash
) {
1028 if (!memcmp(elem
->se_sessionid
.data
, sessionid
->data
,
1029 NFS4_MAX_SESSIONID_LEN
)) {
1034 dprintk("%s: session not found\n", __func__
);
1038 /* caller must hold client_lock */
1040 unhash_session(struct nfsd4_session
*ses
)
1042 list_del(&ses
->se_hash
);
1043 spin_lock(&ses
->se_client
->cl_lock
);
1044 list_del(&ses
->se_perclnt
);
1045 spin_unlock(&ses
->se_client
->cl_lock
);
1048 /* SETCLIENTID and SETCLIENTID_CONFIRM Helper functions */
1050 STALE_CLIENTID(clientid_t
*clid
, struct nfsd_net
*nn
)
1052 if (clid
->cl_boot
== nn
->boot_time
)
1054 dprintk("NFSD stale clientid (%08x/%08x) boot_time %08lx\n",
1055 clid
->cl_boot
, clid
->cl_id
, nn
->boot_time
);
1060 * XXX Should we use a slab cache ?
1061 * This type of memory management is somewhat inefficient, but we use it
1062 * anyway since SETCLIENTID is not a common operation.
1064 static struct nfs4_client
*alloc_client(struct xdr_netobj name
)
1066 struct nfs4_client
*clp
;
1068 clp
= kzalloc(sizeof(struct nfs4_client
), GFP_KERNEL
);
1071 clp
->cl_name
.data
= kmemdup(name
.data
, name
.len
, GFP_KERNEL
);
1072 if (clp
->cl_name
.data
== NULL
) {
1076 clp
->cl_name
.len
= name
.len
;
1081 free_client(struct nfs4_client
*clp
)
1083 struct nfsd_net __maybe_unused
*nn
= net_generic(clp
->net
, nfsd_net_id
);
1085 lockdep_assert_held(&nn
->client_lock
);
1086 while (!list_empty(&clp
->cl_sessions
)) {
1087 struct nfsd4_session
*ses
;
1088 ses
= list_entry(clp
->cl_sessions
.next
, struct nfsd4_session
,
1090 list_del(&ses
->se_perclnt
);
1091 WARN_ON_ONCE(atomic_read(&ses
->se_ref
));
1094 free_svc_cred(&clp
->cl_cred
);
1095 kfree(clp
->cl_name
.data
);
1096 idr_destroy(&clp
->cl_stateids
);
1100 /* must be called under the client_lock */
1102 unhash_client_locked(struct nfs4_client
*clp
)
1104 struct nfsd4_session
*ses
;
1106 list_del(&clp
->cl_lru
);
1107 spin_lock(&clp
->cl_lock
);
1108 list_for_each_entry(ses
, &clp
->cl_sessions
, se_perclnt
)
1109 list_del_init(&ses
->se_hash
);
1110 spin_unlock(&clp
->cl_lock
);
1114 destroy_client(struct nfs4_client
*clp
)
1116 struct nfs4_openowner
*oo
;
1117 struct nfs4_delegation
*dp
;
1118 struct list_head reaplist
;
1119 struct nfsd_net
*nn
= net_generic(clp
->net
, nfsd_net_id
);
1121 INIT_LIST_HEAD(&reaplist
);
1122 spin_lock(&recall_lock
);
1123 while (!list_empty(&clp
->cl_delegations
)) {
1124 dp
= list_entry(clp
->cl_delegations
.next
, struct nfs4_delegation
, dl_perclnt
);
1125 list_del_init(&dp
->dl_perclnt
);
1126 list_move(&dp
->dl_recall_lru
, &reaplist
);
1128 spin_unlock(&recall_lock
);
1129 while (!list_empty(&reaplist
)) {
1130 dp
= list_entry(reaplist
.next
, struct nfs4_delegation
, dl_recall_lru
);
1131 destroy_delegation(dp
);
1133 while (!list_empty(&clp
->cl_openowners
)) {
1134 oo
= list_entry(clp
->cl_openowners
.next
, struct nfs4_openowner
, oo_perclient
);
1135 release_openowner(oo
);
1137 nfsd4_shutdown_callback(clp
);
1138 if (clp
->cl_cb_conn
.cb_xprt
)
1139 svc_xprt_put(clp
->cl_cb_conn
.cb_xprt
);
1140 list_del(&clp
->cl_idhash
);
1141 if (test_bit(NFSD4_CLIENT_CONFIRMED
, &clp
->cl_flags
))
1142 rb_erase(&clp
->cl_namenode
, &nn
->conf_name_tree
);
1144 rb_erase(&clp
->cl_namenode
, &nn
->unconf_name_tree
);
1145 spin_lock(&nn
->client_lock
);
1146 unhash_client_locked(clp
);
1147 WARN_ON_ONCE(atomic_read(&clp
->cl_refcount
));
1149 spin_unlock(&nn
->client_lock
);
1152 static void expire_client(struct nfs4_client
*clp
)
1154 nfsd4_client_record_remove(clp
);
1155 destroy_client(clp
);
1158 static void copy_verf(struct nfs4_client
*target
, nfs4_verifier
*source
)
1160 memcpy(target
->cl_verifier
.data
, source
->data
,
1161 sizeof(target
->cl_verifier
.data
));
1164 static void copy_clid(struct nfs4_client
*target
, struct nfs4_client
*source
)
1166 target
->cl_clientid
.cl_boot
= source
->cl_clientid
.cl_boot
;
1167 target
->cl_clientid
.cl_id
= source
->cl_clientid
.cl_id
;
1170 static int copy_cred(struct svc_cred
*target
, struct svc_cred
*source
)
1172 if (source
->cr_principal
) {
1173 target
->cr_principal
=
1174 kstrdup(source
->cr_principal
, GFP_KERNEL
);
1175 if (target
->cr_principal
== NULL
)
1178 target
->cr_principal
= NULL
;
1179 target
->cr_flavor
= source
->cr_flavor
;
1180 target
->cr_uid
= source
->cr_uid
;
1181 target
->cr_gid
= source
->cr_gid
;
1182 target
->cr_group_info
= source
->cr_group_info
;
1183 get_group_info(target
->cr_group_info
);
1184 target
->cr_gss_mech
= source
->cr_gss_mech
;
1185 if (source
->cr_gss_mech
)
1186 gss_mech_get(source
->cr_gss_mech
);
1191 compare_blob(const struct xdr_netobj
*o1
, const struct xdr_netobj
*o2
)
1195 res
= o1
->len
- o2
->len
;
1198 return (long long)memcmp(o1
->data
, o2
->data
, o1
->len
);
1201 static int same_name(const char *n1
, const char *n2
)
1203 return 0 == memcmp(n1
, n2
, HEXDIR_LEN
);
1207 same_verf(nfs4_verifier
*v1
, nfs4_verifier
*v2
)
1209 return 0 == memcmp(v1
->data
, v2
->data
, sizeof(v1
->data
));
1213 same_clid(clientid_t
*cl1
, clientid_t
*cl2
)
1215 return (cl1
->cl_boot
== cl2
->cl_boot
) && (cl1
->cl_id
== cl2
->cl_id
);
1218 static bool groups_equal(struct group_info
*g1
, struct group_info
*g2
)
1222 if (g1
->ngroups
!= g2
->ngroups
)
1224 for (i
=0; i
<g1
->ngroups
; i
++)
1225 if (!gid_eq(GROUP_AT(g1
, i
), GROUP_AT(g2
, i
)))
1231 * RFC 3530 language requires clid_inuse be returned when the
1232 * "principal" associated with a requests differs from that previously
1233 * used. We use uid, gid's, and gss principal string as our best
1234 * approximation. We also don't want to allow non-gss use of a client
1235 * established using gss: in theory cr_principal should catch that
1236 * change, but in practice cr_principal can be null even in the gss case
1237 * since gssd doesn't always pass down a principal string.
1239 static bool is_gss_cred(struct svc_cred
*cr
)
1241 /* Is cr_flavor one of the gss "pseudoflavors"?: */
1242 return (cr
->cr_flavor
> RPC_AUTH_MAXFLAVOR
);
1247 same_creds(struct svc_cred
*cr1
, struct svc_cred
*cr2
)
1249 if ((is_gss_cred(cr1
) != is_gss_cred(cr2
))
1250 || (!uid_eq(cr1
->cr_uid
, cr2
->cr_uid
))
1251 || (!gid_eq(cr1
->cr_gid
, cr2
->cr_gid
))
1252 || !groups_equal(cr1
->cr_group_info
, cr2
->cr_group_info
))
1254 if (cr1
->cr_principal
== cr2
->cr_principal
)
1256 if (!cr1
->cr_principal
|| !cr2
->cr_principal
)
1258 return 0 == strcmp(cr1
->cr_principal
, cr2
->cr_principal
);
1261 static bool svc_rqst_integrity_protected(struct svc_rqst
*rqstp
)
1263 struct svc_cred
*cr
= &rqstp
->rq_cred
;
1266 service
= gss_pseudoflavor_to_service(cr
->cr_gss_mech
, cr
->cr_flavor
);
1267 return service
== RPC_GSS_SVC_INTEGRITY
||
1268 service
== RPC_GSS_SVC_PRIVACY
;
1271 static bool mach_creds_match(struct nfs4_client
*cl
, struct svc_rqst
*rqstp
)
1273 struct svc_cred
*cr
= &rqstp
->rq_cred
;
1275 if (!cl
->cl_mach_cred
)
1277 if (cl
->cl_cred
.cr_gss_mech
!= cr
->cr_gss_mech
)
1279 if (!svc_rqst_integrity_protected(rqstp
))
1281 if (!cr
->cr_principal
)
1283 return 0 == strcmp(cl
->cl_cred
.cr_principal
, cr
->cr_principal
);
1286 static void gen_clid(struct nfs4_client
*clp
, struct nfsd_net
*nn
)
1288 static u32 current_clientid
= 1;
1290 clp
->cl_clientid
.cl_boot
= nn
->boot_time
;
1291 clp
->cl_clientid
.cl_id
= current_clientid
++;
1294 static void gen_confirm(struct nfs4_client
*clp
)
1299 verf
[0] = (__be32
)get_seconds();
1300 verf
[1] = (__be32
)i
++;
1301 memcpy(clp
->cl_confirm
.data
, verf
, sizeof(clp
->cl_confirm
.data
));
1304 static struct nfs4_stid
*find_stateid(struct nfs4_client
*cl
, stateid_t
*t
)
1306 struct nfs4_stid
*ret
;
1308 ret
= idr_find(&cl
->cl_stateids
, t
->si_opaque
.so_id
);
1309 if (!ret
|| !ret
->sc_type
)
1314 static struct nfs4_stid
*find_stateid_by_type(struct nfs4_client
*cl
, stateid_t
*t
, char typemask
)
1316 struct nfs4_stid
*s
;
1318 s
= find_stateid(cl
, t
);
1321 if (typemask
& s
->sc_type
)
1326 static struct nfs4_client
*create_client(struct xdr_netobj name
,
1327 struct svc_rqst
*rqstp
, nfs4_verifier
*verf
)
1329 struct nfs4_client
*clp
;
1330 struct sockaddr
*sa
= svc_addr(rqstp
);
1332 struct net
*net
= SVC_NET(rqstp
);
1333 struct nfsd_net
*nn
= net_generic(net
, nfsd_net_id
);
1335 clp
= alloc_client(name
);
1339 INIT_LIST_HEAD(&clp
->cl_sessions
);
1340 ret
= copy_cred(&clp
->cl_cred
, &rqstp
->rq_cred
);
1342 spin_lock(&nn
->client_lock
);
1344 spin_unlock(&nn
->client_lock
);
1347 idr_init(&clp
->cl_stateids
);
1348 atomic_set(&clp
->cl_refcount
, 0);
1349 clp
->cl_cb_state
= NFSD4_CB_UNKNOWN
;
1350 INIT_LIST_HEAD(&clp
->cl_idhash
);
1351 INIT_LIST_HEAD(&clp
->cl_openowners
);
1352 INIT_LIST_HEAD(&clp
->cl_delegations
);
1353 INIT_LIST_HEAD(&clp
->cl_lru
);
1354 INIT_LIST_HEAD(&clp
->cl_callbacks
);
1355 INIT_LIST_HEAD(&clp
->cl_revoked
);
1356 spin_lock_init(&clp
->cl_lock
);
1357 nfsd4_init_callback(&clp
->cl_cb_null
);
1358 clp
->cl_time
= get_seconds();
1359 clear_bit(0, &clp
->cl_cb_slot_busy
);
1360 rpc_init_wait_queue(&clp
->cl_cb_waitq
, "Backchannel slot table");
1361 copy_verf(clp
, verf
);
1362 rpc_copy_addr((struct sockaddr
*) &clp
->cl_addr
, sa
);
1364 clp
->cl_cb_session
= NULL
;
1370 add_clp_to_name_tree(struct nfs4_client
*new_clp
, struct rb_root
*root
)
1372 struct rb_node
**new = &(root
->rb_node
), *parent
= NULL
;
1373 struct nfs4_client
*clp
;
1376 clp
= rb_entry(*new, struct nfs4_client
, cl_namenode
);
1379 if (compare_blob(&clp
->cl_name
, &new_clp
->cl_name
) > 0)
1380 new = &((*new)->rb_left
);
1382 new = &((*new)->rb_right
);
1385 rb_link_node(&new_clp
->cl_namenode
, parent
, new);
1386 rb_insert_color(&new_clp
->cl_namenode
, root
);
1389 static struct nfs4_client
*
1390 find_clp_in_name_tree(struct xdr_netobj
*name
, struct rb_root
*root
)
1393 struct rb_node
*node
= root
->rb_node
;
1394 struct nfs4_client
*clp
;
1397 clp
= rb_entry(node
, struct nfs4_client
, cl_namenode
);
1398 cmp
= compare_blob(&clp
->cl_name
, name
);
1400 node
= node
->rb_left
;
1402 node
= node
->rb_right
;
1410 add_to_unconfirmed(struct nfs4_client
*clp
)
1412 unsigned int idhashval
;
1413 struct nfsd_net
*nn
= net_generic(clp
->net
, nfsd_net_id
);
1415 clear_bit(NFSD4_CLIENT_CONFIRMED
, &clp
->cl_flags
);
1416 add_clp_to_name_tree(clp
, &nn
->unconf_name_tree
);
1417 idhashval
= clientid_hashval(clp
->cl_clientid
.cl_id
);
1418 list_add(&clp
->cl_idhash
, &nn
->unconf_id_hashtbl
[idhashval
]);
1423 move_to_confirmed(struct nfs4_client
*clp
)
1425 unsigned int idhashval
= clientid_hashval(clp
->cl_clientid
.cl_id
);
1426 struct nfsd_net
*nn
= net_generic(clp
->net
, nfsd_net_id
);
1428 dprintk("NFSD: move_to_confirm nfs4_client %p\n", clp
);
1429 list_move(&clp
->cl_idhash
, &nn
->conf_id_hashtbl
[idhashval
]);
1430 rb_erase(&clp
->cl_namenode
, &nn
->unconf_name_tree
);
1431 add_clp_to_name_tree(clp
, &nn
->conf_name_tree
);
1432 set_bit(NFSD4_CLIENT_CONFIRMED
, &clp
->cl_flags
);
1436 static struct nfs4_client
*
1437 find_client_in_id_table(struct list_head
*tbl
, clientid_t
*clid
, bool sessions
)
1439 struct nfs4_client
*clp
;
1440 unsigned int idhashval
= clientid_hashval(clid
->cl_id
);
1442 list_for_each_entry(clp
, &tbl
[idhashval
], cl_idhash
) {
1443 if (same_clid(&clp
->cl_clientid
, clid
)) {
1444 if ((bool)clp
->cl_minorversion
!= sessions
)
1453 static struct nfs4_client
*
1454 find_confirmed_client(clientid_t
*clid
, bool sessions
, struct nfsd_net
*nn
)
1456 struct list_head
*tbl
= nn
->conf_id_hashtbl
;
1458 return find_client_in_id_table(tbl
, clid
, sessions
);
1461 static struct nfs4_client
*
1462 find_unconfirmed_client(clientid_t
*clid
, bool sessions
, struct nfsd_net
*nn
)
1464 struct list_head
*tbl
= nn
->unconf_id_hashtbl
;
1466 return find_client_in_id_table(tbl
, clid
, sessions
);
1469 static bool clp_used_exchangeid(struct nfs4_client
*clp
)
1471 return clp
->cl_exchange_flags
!= 0;
1474 static struct nfs4_client
*
1475 find_confirmed_client_by_name(struct xdr_netobj
*name
, struct nfsd_net
*nn
)
1477 return find_clp_in_name_tree(name
, &nn
->conf_name_tree
);
1480 static struct nfs4_client
*
1481 find_unconfirmed_client_by_name(struct xdr_netobj
*name
, struct nfsd_net
*nn
)
1483 return find_clp_in_name_tree(name
, &nn
->unconf_name_tree
);
1487 gen_callback(struct nfs4_client
*clp
, struct nfsd4_setclientid
*se
, struct svc_rqst
*rqstp
)
1489 struct nfs4_cb_conn
*conn
= &clp
->cl_cb_conn
;
1490 struct sockaddr
*sa
= svc_addr(rqstp
);
1491 u32 scopeid
= rpc_get_scope_id(sa
);
1492 unsigned short expected_family
;
1494 /* Currently, we only support tcp and tcp6 for the callback channel */
1495 if (se
->se_callback_netid_len
== 3 &&
1496 !memcmp(se
->se_callback_netid_val
, "tcp", 3))
1497 expected_family
= AF_INET
;
1498 else if (se
->se_callback_netid_len
== 4 &&
1499 !memcmp(se
->se_callback_netid_val
, "tcp6", 4))
1500 expected_family
= AF_INET6
;
1504 conn
->cb_addrlen
= rpc_uaddr2sockaddr(clp
->net
, se
->se_callback_addr_val
,
1505 se
->se_callback_addr_len
,
1506 (struct sockaddr
*)&conn
->cb_addr
,
1507 sizeof(conn
->cb_addr
));
1509 if (!conn
->cb_addrlen
|| conn
->cb_addr
.ss_family
!= expected_family
)
1512 if (conn
->cb_addr
.ss_family
== AF_INET6
)
1513 ((struct sockaddr_in6
*)&conn
->cb_addr
)->sin6_scope_id
= scopeid
;
1515 conn
->cb_prog
= se
->se_callback_prog
;
1516 conn
->cb_ident
= se
->se_callback_ident
;
1517 memcpy(&conn
->cb_saddr
, &rqstp
->rq_daddr
, rqstp
->rq_daddrlen
);
1520 conn
->cb_addr
.ss_family
= AF_UNSPEC
;
1521 conn
->cb_addrlen
= 0;
1522 dprintk(KERN_INFO
"NFSD: this client (clientid %08x/%08x) "
1523 "will not receive delegations\n",
1524 clp
->cl_clientid
.cl_boot
, clp
->cl_clientid
.cl_id
);
1530 * Cache a reply. nfsd4_check_drc_limit() has bounded the cache size.
1533 nfsd4_store_cache_entry(struct nfsd4_compoundres
*resp
)
1535 struct nfsd4_slot
*slot
= resp
->cstate
.slot
;
1538 dprintk("--> %s slot %p\n", __func__
, slot
);
1540 slot
->sl_opcnt
= resp
->opcnt
;
1541 slot
->sl_status
= resp
->cstate
.status
;
1543 slot
->sl_flags
|= NFSD4_SLOT_INITIALIZED
;
1544 if (nfsd4_not_cached(resp
)) {
1545 slot
->sl_datalen
= 0;
1548 slot
->sl_datalen
= (char *)resp
->p
- (char *)resp
->cstate
.datap
;
1549 base
= (char *)resp
->cstate
.datap
-
1550 (char *)resp
->xbuf
->head
[0].iov_base
;
1551 if (read_bytes_from_xdr_buf(resp
->xbuf
, base
, slot
->sl_data
,
1553 WARN("%s: sessions DRC could not cache compound\n", __func__
);
1558 * Encode the replay sequence operation from the slot values.
1559 * If cachethis is FALSE encode the uncached rep error on the next
1560 * operation which sets resp->p and increments resp->opcnt for
1561 * nfs4svc_encode_compoundres.
1565 nfsd4_enc_sequence_replay(struct nfsd4_compoundargs
*args
,
1566 struct nfsd4_compoundres
*resp
)
1568 struct nfsd4_op
*op
;
1569 struct nfsd4_slot
*slot
= resp
->cstate
.slot
;
1571 /* Encode the replayed sequence operation */
1572 op
= &args
->ops
[resp
->opcnt
- 1];
1573 nfsd4_encode_operation(resp
, op
);
1575 /* Return nfserr_retry_uncached_rep in next operation. */
1576 if (args
->opcnt
> 1 && !(slot
->sl_flags
& NFSD4_SLOT_CACHETHIS
)) {
1577 op
= &args
->ops
[resp
->opcnt
++];
1578 op
->status
= nfserr_retry_uncached_rep
;
1579 nfsd4_encode_operation(resp
, op
);
1585 * The sequence operation is not cached because we can use the slot and
1589 nfsd4_replay_cache_entry(struct nfsd4_compoundres
*resp
,
1590 struct nfsd4_sequence
*seq
)
1592 struct nfsd4_slot
*slot
= resp
->cstate
.slot
;
1595 dprintk("--> %s slot %p\n", __func__
, slot
);
1597 /* Either returns 0 or nfserr_retry_uncached */
1598 status
= nfsd4_enc_sequence_replay(resp
->rqstp
->rq_argp
, resp
);
1599 if (status
== nfserr_retry_uncached_rep
)
1602 /* The sequence operation has been encoded, cstate->datap set. */
1603 memcpy(resp
->cstate
.datap
, slot
->sl_data
, slot
->sl_datalen
);
1605 resp
->opcnt
= slot
->sl_opcnt
;
1606 resp
->p
= resp
->cstate
.datap
+ XDR_QUADLEN(slot
->sl_datalen
);
1607 status
= slot
->sl_status
;
1613 * Set the exchange_id flags returned by the server.
1616 nfsd4_set_ex_flags(struct nfs4_client
*new, struct nfsd4_exchange_id
*clid
)
1618 /* pNFS is not supported */
1619 new->cl_exchange_flags
|= EXCHGID4_FLAG_USE_NON_PNFS
;
1621 /* Referrals are supported, Migration is not. */
1622 new->cl_exchange_flags
|= EXCHGID4_FLAG_SUPP_MOVED_REFER
;
1624 /* set the wire flags to return to client. */
1625 clid
->flags
= new->cl_exchange_flags
;
1628 static bool client_has_state(struct nfs4_client
*clp
)
1631 * Note clp->cl_openowners check isn't quite right: there's no
1632 * need to count owners without stateid's.
1634 * Also note we should probably be using this in 4.0 case too.
1636 return !list_empty(&clp
->cl_openowners
)
1637 || !list_empty(&clp
->cl_delegations
)
1638 || !list_empty(&clp
->cl_sessions
);
1642 nfsd4_exchange_id(struct svc_rqst
*rqstp
,
1643 struct nfsd4_compound_state
*cstate
,
1644 struct nfsd4_exchange_id
*exid
)
1646 struct nfs4_client
*unconf
, *conf
, *new;
1648 char addr_str
[INET6_ADDRSTRLEN
];
1649 nfs4_verifier verf
= exid
->verifier
;
1650 struct sockaddr
*sa
= svc_addr(rqstp
);
1651 bool update
= exid
->flags
& EXCHGID4_FLAG_UPD_CONFIRMED_REC_A
;
1652 struct nfsd_net
*nn
= net_generic(SVC_NET(rqstp
), nfsd_net_id
);
1654 rpc_ntop(sa
, addr_str
, sizeof(addr_str
));
1655 dprintk("%s rqstp=%p exid=%p clname.len=%u clname.data=%p "
1656 "ip_addr=%s flags %x, spa_how %d\n",
1657 __func__
, rqstp
, exid
, exid
->clname
.len
, exid
->clname
.data
,
1658 addr_str
, exid
->flags
, exid
->spa_how
);
1660 if (exid
->flags
& ~EXCHGID4_FLAG_MASK_A
)
1661 return nfserr_inval
;
1663 switch (exid
->spa_how
) {
1665 if (!svc_rqst_integrity_protected(rqstp
))
1666 return nfserr_inval
;
1669 default: /* checked by xdr code */
1672 return nfserr_encr_alg_unsupp
;
1675 /* Cases below refer to rfc 5661 section 18.35.4: */
1677 conf
= find_confirmed_client_by_name(&exid
->clname
, nn
);
1679 bool creds_match
= same_creds(&conf
->cl_cred
, &rqstp
->rq_cred
);
1680 bool verfs_match
= same_verf(&verf
, &conf
->cl_verifier
);
1683 if (!clp_used_exchangeid(conf
)) { /* buggy client */
1684 status
= nfserr_inval
;
1687 if (!mach_creds_match(conf
, rqstp
)) {
1688 status
= nfserr_wrong_cred
;
1691 if (!creds_match
) { /* case 9 */
1692 status
= nfserr_perm
;
1695 if (!verfs_match
) { /* case 8 */
1696 status
= nfserr_not_same
;
1700 exid
->flags
|= EXCHGID4_FLAG_CONFIRMED_R
;
1704 if (!creds_match
) { /* case 3 */
1705 if (client_has_state(conf
)) {
1706 status
= nfserr_clid_inuse
;
1709 expire_client(conf
);
1712 if (verfs_match
) { /* case 2 */
1713 conf
->cl_exchange_flags
|= EXCHGID4_FLAG_CONFIRMED_R
;
1717 /* case 5, client reboot */
1721 if (update
) { /* case 7 */
1722 status
= nfserr_noent
;
1726 unconf
= find_unconfirmed_client_by_name(&exid
->clname
, nn
);
1727 if (unconf
) /* case 4, possible retry or client restart */
1728 expire_client(unconf
);
1730 /* case 1 (normal case) */
1732 new = create_client(exid
->clname
, rqstp
, &verf
);
1734 status
= nfserr_jukebox
;
1737 new->cl_minorversion
= cstate
->minorversion
;
1738 new->cl_mach_cred
= (exid
->spa_how
== SP4_MACH_CRED
);
1741 add_to_unconfirmed(new);
1743 exid
->clientid
.cl_boot
= new->cl_clientid
.cl_boot
;
1744 exid
->clientid
.cl_id
= new->cl_clientid
.cl_id
;
1746 exid
->seqid
= new->cl_cs_slot
.sl_seqid
+ 1;
1747 nfsd4_set_ex_flags(new, exid
);
1749 dprintk("nfsd4_exchange_id seqid %d flags %x\n",
1750 new->cl_cs_slot
.sl_seqid
, new->cl_exchange_flags
);
1754 nfs4_unlock_state();
1759 check_slot_seqid(u32 seqid
, u32 slot_seqid
, int slot_inuse
)
1761 dprintk("%s enter. seqid %d slot_seqid %d\n", __func__
, seqid
,
1764 /* The slot is in use, and no response has been sent. */
1766 if (seqid
== slot_seqid
)
1767 return nfserr_jukebox
;
1769 return nfserr_seq_misordered
;
1771 /* Note unsigned 32-bit arithmetic handles wraparound: */
1772 if (likely(seqid
== slot_seqid
+ 1))
1774 if (seqid
== slot_seqid
)
1775 return nfserr_replay_cache
;
1776 return nfserr_seq_misordered
;
1780 * Cache the create session result into the create session single DRC
1781 * slot cache by saving the xdr structure. sl_seqid has been set.
1782 * Do this for solo or embedded create session operations.
1785 nfsd4_cache_create_session(struct nfsd4_create_session
*cr_ses
,
1786 struct nfsd4_clid_slot
*slot
, __be32 nfserr
)
1788 slot
->sl_status
= nfserr
;
1789 memcpy(&slot
->sl_cr_ses
, cr_ses
, sizeof(*cr_ses
));
1793 nfsd4_replay_create_session(struct nfsd4_create_session
*cr_ses
,
1794 struct nfsd4_clid_slot
*slot
)
1796 memcpy(cr_ses
, &slot
->sl_cr_ses
, sizeof(*cr_ses
));
1797 return slot
->sl_status
;
1800 #define NFSD_MIN_REQ_HDR_SEQ_SZ ((\
1801 2 * 2 + /* credential,verifier: AUTH_NULL, length 0 */ \
1802 1 + /* MIN tag is length with zero, only length */ \
1803 3 + /* version, opcount, opcode */ \
1804 XDR_QUADLEN(NFS4_MAX_SESSIONID_LEN) + \
1805 /* seqid, slotID, slotID, cache */ \
1806 4 ) * sizeof(__be32))
1808 #define NFSD_MIN_RESP_HDR_SEQ_SZ ((\
1809 2 + /* verifier: AUTH_NULL, length 0 */\
1811 1 + /* MIN tag is length with zero, only length */ \
1812 3 + /* opcount, opcode, opstatus*/ \
1813 XDR_QUADLEN(NFS4_MAX_SESSIONID_LEN) + \
1814 /* seqid, slotID, slotID, slotID, status */ \
1815 5 ) * sizeof(__be32))
1817 static __be32
check_forechannel_attrs(struct nfsd4_channel_attrs
*ca
, struct nfsd_net
*nn
)
1819 u32 maxrpc
= nn
->nfsd_serv
->sv_max_mesg
;
1821 if (ca
->maxreq_sz
< NFSD_MIN_REQ_HDR_SEQ_SZ
)
1822 return nfserr_toosmall
;
1823 if (ca
->maxresp_sz
< NFSD_MIN_RESP_HDR_SEQ_SZ
)
1824 return nfserr_toosmall
;
1825 ca
->headerpadsz
= 0;
1826 ca
->maxreq_sz
= min_t(u32
, ca
->maxreq_sz
, maxrpc
);
1827 ca
->maxresp_sz
= min_t(u32
, ca
->maxresp_sz
, maxrpc
);
1828 ca
->maxops
= min_t(u32
, ca
->maxops
, NFSD_MAX_OPS_PER_COMPOUND
);
1829 ca
->maxresp_cached
= min_t(u32
, ca
->maxresp_cached
,
1830 NFSD_SLOT_CACHE_SIZE
+ NFSD_MIN_HDR_SEQ_SZ
);
1831 ca
->maxreqs
= min_t(u32
, ca
->maxreqs
, NFSD_MAX_SLOTS_PER_SESSION
);
1833 * Note decreasing slot size below client's request may make it
1834 * difficult for client to function correctly, whereas
1835 * decreasing the number of slots will (just?) affect
1836 * performance. When short on memory we therefore prefer to
1837 * decrease number of slots instead of their size. Clients that
1838 * request larger slots than they need will get poor results:
1840 ca
->maxreqs
= nfsd4_get_drc_mem(ca
);
1842 return nfserr_jukebox
;
1847 static __be32
check_backchannel_attrs(struct nfsd4_channel_attrs
*ca
)
1849 ca
->headerpadsz
= 0;
1852 * These RPC_MAX_HEADER macros are overkill, especially since we
1853 * don't even do gss on the backchannel yet. But this is still
1854 * less than 1k. Tighten up this estimate in the unlikely event
1855 * it turns out to be a problem for some client:
1857 if (ca
->maxreq_sz
< NFS4_enc_cb_recall_sz
+ RPC_MAX_HEADER_WITH_AUTH
)
1858 return nfserr_toosmall
;
1859 if (ca
->maxresp_sz
< NFS4_dec_cb_recall_sz
+ RPC_MAX_REPHEADER_WITH_AUTH
)
1860 return nfserr_toosmall
;
1861 ca
->maxresp_cached
= 0;
1863 return nfserr_toosmall
;
1868 static __be32
nfsd4_check_cb_sec(struct nfsd4_cb_sec
*cbs
)
1870 switch (cbs
->flavor
) {
1876 * GSS case: the spec doesn't allow us to return this
1877 * error. But it also doesn't allow us not to support
1879 * I'd rather this fail hard than return some error the
1880 * client might think it can already handle:
1882 return nfserr_encr_alg_unsupp
;
1887 nfsd4_create_session(struct svc_rqst
*rqstp
,
1888 struct nfsd4_compound_state
*cstate
,
1889 struct nfsd4_create_session
*cr_ses
)
1891 struct sockaddr
*sa
= svc_addr(rqstp
);
1892 struct nfs4_client
*conf
, *unconf
;
1893 struct nfsd4_session
*new;
1894 struct nfsd4_conn
*conn
;
1895 struct nfsd4_clid_slot
*cs_slot
= NULL
;
1897 struct nfsd_net
*nn
= net_generic(SVC_NET(rqstp
), nfsd_net_id
);
1899 if (cr_ses
->flags
& ~SESSION4_FLAG_MASK_A
)
1900 return nfserr_inval
;
1901 status
= nfsd4_check_cb_sec(&cr_ses
->cb_sec
);
1904 status
= check_forechannel_attrs(&cr_ses
->fore_channel
, nn
);
1907 status
= check_backchannel_attrs(&cr_ses
->back_channel
);
1910 status
= nfserr_jukebox
;
1911 new = alloc_session(&cr_ses
->fore_channel
);
1913 goto out_release_drc_mem
;
1914 conn
= alloc_conn_from_crses(rqstp
, cr_ses
);
1916 goto out_free_session
;
1919 unconf
= find_unconfirmed_client(&cr_ses
->clientid
, true, nn
);
1920 conf
= find_confirmed_client(&cr_ses
->clientid
, true, nn
);
1921 WARN_ON_ONCE(conf
&& unconf
);
1924 status
= nfserr_wrong_cred
;
1925 if (!mach_creds_match(conf
, rqstp
))
1927 cs_slot
= &conf
->cl_cs_slot
;
1928 status
= check_slot_seqid(cr_ses
->seqid
, cs_slot
->sl_seqid
, 0);
1929 if (status
== nfserr_replay_cache
) {
1930 status
= nfsd4_replay_create_session(cr_ses
, cs_slot
);
1932 } else if (cr_ses
->seqid
!= cs_slot
->sl_seqid
+ 1) {
1933 status
= nfserr_seq_misordered
;
1936 } else if (unconf
) {
1937 struct nfs4_client
*old
;
1938 if (!same_creds(&unconf
->cl_cred
, &rqstp
->rq_cred
) ||
1939 !rpc_cmp_addr(sa
, (struct sockaddr
*) &unconf
->cl_addr
)) {
1940 status
= nfserr_clid_inuse
;
1943 status
= nfserr_wrong_cred
;
1944 if (!mach_creds_match(unconf
, rqstp
))
1946 cs_slot
= &unconf
->cl_cs_slot
;
1947 status
= check_slot_seqid(cr_ses
->seqid
, cs_slot
->sl_seqid
, 0);
1949 /* an unconfirmed replay returns misordered */
1950 status
= nfserr_seq_misordered
;
1953 old
= find_confirmed_client_by_name(&unconf
->cl_name
, nn
);
1955 status
= mark_client_expired(old
);
1960 move_to_confirmed(unconf
);
1963 status
= nfserr_stale_clientid
;
1968 * We do not support RDMA or persistent sessions
1970 cr_ses
->flags
&= ~SESSION4_PERSIST
;
1971 cr_ses
->flags
&= ~SESSION4_RDMA
;
1973 init_session(rqstp
, new, conf
, cr_ses
);
1974 nfsd4_init_conn(rqstp
, conn
, new);
1976 memcpy(cr_ses
->sessionid
.data
, new->se_sessionid
.data
,
1977 NFS4_MAX_SESSIONID_LEN
);
1978 cs_slot
->sl_seqid
++;
1979 cr_ses
->seqid
= cs_slot
->sl_seqid
;
1981 /* cache solo and embedded create sessions under the state lock */
1982 nfsd4_cache_create_session(cr_ses
, cs_slot
, status
);
1983 nfs4_unlock_state();
1986 nfs4_unlock_state();
1989 __free_session(new);
1990 out_release_drc_mem
:
1991 nfsd4_put_drc_mem(&cr_ses
->fore_channel
);
1995 static __be32
nfsd4_map_bcts_dir(u32
*dir
)
1998 case NFS4_CDFC4_FORE
:
1999 case NFS4_CDFC4_BACK
:
2001 case NFS4_CDFC4_FORE_OR_BOTH
:
2002 case NFS4_CDFC4_BACK_OR_BOTH
:
2003 *dir
= NFS4_CDFC4_BOTH
;
2006 return nfserr_inval
;
2009 __be32
nfsd4_backchannel_ctl(struct svc_rqst
*rqstp
, struct nfsd4_compound_state
*cstate
, struct nfsd4_backchannel_ctl
*bc
)
2011 struct nfsd4_session
*session
= cstate
->session
;
2012 struct nfsd_net
*nn
= net_generic(SVC_NET(rqstp
), nfsd_net_id
);
2015 status
= nfsd4_check_cb_sec(&bc
->bc_cb_sec
);
2018 spin_lock(&nn
->client_lock
);
2019 session
->se_cb_prog
= bc
->bc_cb_program
;
2020 session
->se_cb_sec
= bc
->bc_cb_sec
;
2021 spin_unlock(&nn
->client_lock
);
2023 nfsd4_probe_callback(session
->se_client
);
2028 __be32
nfsd4_bind_conn_to_session(struct svc_rqst
*rqstp
,
2029 struct nfsd4_compound_state
*cstate
,
2030 struct nfsd4_bind_conn_to_session
*bcts
)
2033 struct nfsd4_conn
*conn
;
2034 struct nfsd4_session
*session
;
2035 struct nfsd_net
*nn
= net_generic(SVC_NET(rqstp
), nfsd_net_id
);
2037 if (!nfsd4_last_compound_op(rqstp
))
2038 return nfserr_not_only_op
;
2040 spin_lock(&nn
->client_lock
);
2041 session
= find_in_sessionid_hashtbl(&bcts
->sessionid
, SVC_NET(rqstp
));
2042 spin_unlock(&nn
->client_lock
);
2043 status
= nfserr_badsession
;
2046 status
= nfserr_wrong_cred
;
2047 if (!mach_creds_match(session
->se_client
, rqstp
))
2049 status
= nfsd4_map_bcts_dir(&bcts
->dir
);
2052 conn
= alloc_conn(rqstp
, bcts
->dir
);
2053 status
= nfserr_jukebox
;
2056 nfsd4_init_conn(rqstp
, conn
, session
);
2059 nfs4_unlock_state();
2063 static bool nfsd4_compound_in_session(struct nfsd4_session
*session
, struct nfs4_sessionid
*sid
)
2067 return !memcmp(sid
, &session
->se_sessionid
, sizeof(*sid
));
2071 nfsd4_destroy_session(struct svc_rqst
*r
,
2072 struct nfsd4_compound_state
*cstate
,
2073 struct nfsd4_destroy_session
*sessionid
)
2075 struct nfsd4_session
*ses
;
2077 struct nfsd_net
*nn
= net_generic(SVC_NET(r
), nfsd_net_id
);
2080 status
= nfserr_not_only_op
;
2081 if (nfsd4_compound_in_session(cstate
->session
, &sessionid
->sessionid
)) {
2082 if (!nfsd4_last_compound_op(r
))
2085 dump_sessionid(__func__
, &sessionid
->sessionid
);
2086 spin_lock(&nn
->client_lock
);
2087 ses
= find_in_sessionid_hashtbl(&sessionid
->sessionid
, SVC_NET(r
));
2088 status
= nfserr_badsession
;
2090 goto out_client_lock
;
2091 status
= nfserr_wrong_cred
;
2092 if (!mach_creds_match(ses
->se_client
, r
))
2093 goto out_client_lock
;
2094 status
= mark_session_dead_locked(ses
);
2096 goto out_client_lock
;
2097 unhash_session(ses
);
2098 spin_unlock(&nn
->client_lock
);
2100 nfsd4_probe_callback_sync(ses
->se_client
);
2102 spin_lock(&nn
->client_lock
);
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 lock_flocks*/
2722 dp
->dl_time
= get_seconds();
2724 nfsd4_cb_recall(dp
);
2727 /* Called from break_lease() with lock_flocks() 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
;
4598 for (flpp
= &inode
->i_flock
; *flpp
!= NULL
; flpp
= &(*flpp
)->fl_next
) {
4599 if ((*flpp
)->fl_owner
== (fl_owner_t
)lowner
) {
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
);