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>
46 #include "current_stateid.h"
50 #define NFSDDBG_FACILITY NFSDDBG_PROC
52 #define all_ones {{~0,~0},~0}
53 static const stateid_t one_stateid
= {
55 .si_opaque
= all_ones
,
57 static const stateid_t zero_stateid
= {
60 static const stateid_t currentstateid
= {
64 static u64 current_sessionid
= 1;
66 #define ZERO_STATEID(stateid) (!memcmp((stateid), &zero_stateid, sizeof(stateid_t)))
67 #define ONE_STATEID(stateid) (!memcmp((stateid), &one_stateid, sizeof(stateid_t)))
68 #define CURRENT_STATEID(stateid) (!memcmp((stateid), ¤tstateid, sizeof(stateid_t)))
70 /* forward declarations */
71 static int check_for_locks(struct nfs4_file
*filp
, struct nfs4_lockowner
*lowner
);
75 /* Currently used for almost all code touching nfsv4 state: */
76 static DEFINE_MUTEX(client_mutex
);
79 * Currently used for the del_recall_lru and file hash table. In an
80 * effort to decrease the scope of the client_mutex, this spinlock may
81 * eventually cover more:
83 static DEFINE_SPINLOCK(recall_lock
);
85 static struct kmem_cache
*openowner_slab
= NULL
;
86 static struct kmem_cache
*lockowner_slab
= NULL
;
87 static struct kmem_cache
*file_slab
= NULL
;
88 static struct kmem_cache
*stateid_slab
= NULL
;
89 static struct kmem_cache
*deleg_slab
= NULL
;
94 mutex_lock(&client_mutex
);
97 static void free_session(struct kref
*);
99 /* Must be called under the client_lock */
100 static void nfsd4_put_session_locked(struct nfsd4_session
*ses
)
102 kref_put(&ses
->se_ref
, free_session
);
105 static void nfsd4_get_session(struct nfsd4_session
*ses
)
107 kref_get(&ses
->se_ref
);
111 nfs4_unlock_state(void)
113 mutex_unlock(&client_mutex
);
117 opaque_hashval(const void *ptr
, int nbytes
)
119 unsigned char *cptr
= (unsigned char *) ptr
;
129 static struct list_head del_recall_lru
;
131 static void nfsd4_free_file(struct nfs4_file
*f
)
133 kmem_cache_free(file_slab
, f
);
137 put_nfs4_file(struct nfs4_file
*fi
)
139 if (atomic_dec_and_lock(&fi
->fi_ref
, &recall_lock
)) {
140 list_del(&fi
->fi_hash
);
141 spin_unlock(&recall_lock
);
148 get_nfs4_file(struct nfs4_file
*fi
)
150 atomic_inc(&fi
->fi_ref
);
153 static int num_delegations
;
154 unsigned long max_delegations
;
157 * Open owner state (share locks)
160 /* hash tables for lock and open owners */
161 #define OWNER_HASH_BITS 8
162 #define OWNER_HASH_SIZE (1 << OWNER_HASH_BITS)
163 #define OWNER_HASH_MASK (OWNER_HASH_SIZE - 1)
165 static unsigned int ownerstr_hashval(u32 clientid
, struct xdr_netobj
*ownername
)
169 ret
= opaque_hashval(ownername
->data
, ownername
->len
);
171 return ret
& OWNER_HASH_MASK
;
174 /* hash table for nfs4_file */
175 #define FILE_HASH_BITS 8
176 #define FILE_HASH_SIZE (1 << FILE_HASH_BITS)
178 static unsigned int file_hashval(struct inode
*ino
)
180 /* XXX: why are we hashing on inode pointer, anyway? */
181 return hash_ptr(ino
, FILE_HASH_BITS
);
184 static struct list_head file_hashtbl
[FILE_HASH_SIZE
];
186 static void __nfs4_file_get_access(struct nfs4_file
*fp
, int oflag
)
188 WARN_ON_ONCE(!(fp
->fi_fds
[oflag
] || fp
->fi_fds
[O_RDWR
]));
189 atomic_inc(&fp
->fi_access
[oflag
]);
192 static void nfs4_file_get_access(struct nfs4_file
*fp
, int oflag
)
194 if (oflag
== O_RDWR
) {
195 __nfs4_file_get_access(fp
, O_RDONLY
);
196 __nfs4_file_get_access(fp
, O_WRONLY
);
198 __nfs4_file_get_access(fp
, oflag
);
201 static void nfs4_file_put_fd(struct nfs4_file
*fp
, int oflag
)
203 if (fp
->fi_fds
[oflag
]) {
204 fput(fp
->fi_fds
[oflag
]);
205 fp
->fi_fds
[oflag
] = NULL
;
209 static void __nfs4_file_put_access(struct nfs4_file
*fp
, int oflag
)
211 if (atomic_dec_and_test(&fp
->fi_access
[oflag
])) {
212 nfs4_file_put_fd(fp
, oflag
);
214 * It's also safe to get rid of the RDWR open *if*
215 * we no longer have need of the other kind of access
216 * or if we already have the other kind of open:
218 if (fp
->fi_fds
[1-oflag
]
219 || atomic_read(&fp
->fi_access
[1 - oflag
]) == 0)
220 nfs4_file_put_fd(fp
, O_RDWR
);
224 static void nfs4_file_put_access(struct nfs4_file
*fp
, int oflag
)
226 if (oflag
== O_RDWR
) {
227 __nfs4_file_put_access(fp
, O_RDONLY
);
228 __nfs4_file_put_access(fp
, O_WRONLY
);
230 __nfs4_file_put_access(fp
, oflag
);
233 static struct nfs4_stid
*nfs4_alloc_stid(struct nfs4_client
*cl
, struct
236 struct idr
*stateids
= &cl
->cl_stateids
;
237 struct nfs4_stid
*stid
;
240 stid
= kmem_cache_alloc(slab
, GFP_KERNEL
);
244 new_id
= idr_alloc_cyclic(stateids
, stid
, 0, 0, GFP_KERNEL
);
247 stid
->sc_client
= cl
;
249 stid
->sc_stateid
.si_opaque
.so_id
= new_id
;
250 stid
->sc_stateid
.si_opaque
.so_clid
= cl
->cl_clientid
;
251 /* Will be incremented before return to client: */
252 stid
->sc_stateid
.si_generation
= 0;
255 * It shouldn't be a problem to reuse an opaque stateid value.
256 * I don't think it is for 4.1. But with 4.0 I worry that, for
257 * example, a stray write retransmission could be accepted by
258 * the server when it should have been rejected. Therefore,
259 * adopt a trick from the sctp code to attempt to maximize the
260 * amount of time until an id is reused, by ensuring they always
261 * "increase" (mod INT_MAX):
269 static struct nfs4_ol_stateid
* nfs4_alloc_stateid(struct nfs4_client
*clp
)
271 return openlockstateid(nfs4_alloc_stid(clp
, stateid_slab
));
274 static struct nfs4_delegation
*
275 alloc_init_deleg(struct nfs4_client
*clp
, struct nfs4_ol_stateid
*stp
, struct svc_fh
*current_fh
, u32 type
)
277 struct nfs4_delegation
*dp
;
278 struct nfs4_file
*fp
= stp
->st_file
;
280 dprintk("NFSD alloc_init_deleg\n");
282 * Major work on the lease subsystem (for example, to support
283 * calbacks on stat) will be required before we can support
284 * write delegations properly.
286 if (type
!= NFS4_OPEN_DELEGATE_READ
)
288 if (fp
->fi_had_conflict
)
290 if (num_delegations
> max_delegations
)
292 dp
= delegstateid(nfs4_alloc_stid(clp
, deleg_slab
));
295 dp
->dl_stid
.sc_type
= NFS4_DELEG_STID
;
297 * delegation seqid's are never incremented. The 4.1 special
298 * meaning of seqid 0 isn't meaningful, really, but let's avoid
299 * 0 anyway just for consistency and use 1:
301 dp
->dl_stid
.sc_stateid
.si_generation
= 1;
303 INIT_LIST_HEAD(&dp
->dl_perfile
);
304 INIT_LIST_HEAD(&dp
->dl_perclnt
);
305 INIT_LIST_HEAD(&dp
->dl_recall_lru
);
309 fh_copy_shallow(&dp
->dl_fh
, ¤t_fh
->fh_handle
);
311 atomic_set(&dp
->dl_count
, 1);
312 nfsd4_init_callback(&dp
->dl_recall
);
316 static void free_stid(struct nfs4_stid
*s
, struct kmem_cache
*slab
)
318 struct idr
*stateids
= &s
->sc_client
->cl_stateids
;
320 idr_remove(stateids
, s
->sc_stateid
.si_opaque
.so_id
);
321 kmem_cache_free(slab
, s
);
325 nfs4_put_delegation(struct nfs4_delegation
*dp
)
327 if (atomic_dec_and_test(&dp
->dl_count
)) {
328 dprintk("NFSD: freeing dp %p\n",dp
);
329 put_nfs4_file(dp
->dl_file
);
330 free_stid(&dp
->dl_stid
, deleg_slab
);
335 static void nfs4_put_deleg_lease(struct nfs4_file
*fp
)
337 if (atomic_dec_and_test(&fp
->fi_delegees
)) {
338 vfs_setlease(fp
->fi_deleg_file
, F_UNLCK
, &fp
->fi_lease
);
340 fput(fp
->fi_deleg_file
);
341 fp
->fi_deleg_file
= NULL
;
345 static void unhash_stid(struct nfs4_stid
*s
)
350 /* Called under the state lock. */
352 unhash_delegation(struct nfs4_delegation
*dp
)
354 unhash_stid(&dp
->dl_stid
);
355 list_del_init(&dp
->dl_perclnt
);
356 spin_lock(&recall_lock
);
357 list_del_init(&dp
->dl_perfile
);
358 list_del_init(&dp
->dl_recall_lru
);
359 spin_unlock(&recall_lock
);
360 nfs4_put_deleg_lease(dp
->dl_file
);
361 nfs4_put_delegation(dp
);
368 static unsigned int clientid_hashval(u32 id
)
370 return id
& CLIENT_HASH_MASK
;
373 static unsigned int clientstr_hashval(const char *name
)
375 return opaque_hashval(name
, 8) & CLIENT_HASH_MASK
;
379 * We store the NONE, READ, WRITE, and BOTH bits separately in the
380 * st_{access,deny}_bmap field of the stateid, in order to track not
381 * only what share bits are currently in force, but also what
382 * combinations of share bits previous opens have used. This allows us
383 * to enforce the recommendation of rfc 3530 14.2.19 that the server
384 * return an error if the client attempt to downgrade to a combination
385 * of share bits not explicable by closing some of its previous opens.
387 * XXX: This enforcement is actually incomplete, since we don't keep
388 * track of access/deny bit combinations; so, e.g., we allow:
390 * OPEN allow read, deny write
391 * OPEN allow both, deny none
392 * DOWNGRADE allow read, deny none
394 * which we should reject.
397 bmap_to_share_mode(unsigned long bmap
) {
399 unsigned int access
= 0;
401 for (i
= 1; i
< 4; i
++) {
402 if (test_bit(i
, &bmap
))
409 test_share(struct nfs4_ol_stateid
*stp
, struct nfsd4_open
*open
) {
410 unsigned int access
, deny
;
412 access
= bmap_to_share_mode(stp
->st_access_bmap
);
413 deny
= bmap_to_share_mode(stp
->st_deny_bmap
);
414 if ((access
& open
->op_share_deny
) || (deny
& open
->op_share_access
))
419 /* set share access for a given stateid */
421 set_access(u32 access
, struct nfs4_ol_stateid
*stp
)
423 __set_bit(access
, &stp
->st_access_bmap
);
426 /* clear share access for a given stateid */
428 clear_access(u32 access
, struct nfs4_ol_stateid
*stp
)
430 __clear_bit(access
, &stp
->st_access_bmap
);
433 /* test whether a given stateid has access */
435 test_access(u32 access
, struct nfs4_ol_stateid
*stp
)
437 return test_bit(access
, &stp
->st_access_bmap
);
440 /* set share deny for a given stateid */
442 set_deny(u32 access
, struct nfs4_ol_stateid
*stp
)
444 __set_bit(access
, &stp
->st_deny_bmap
);
447 /* clear share deny for a given stateid */
449 clear_deny(u32 access
, struct nfs4_ol_stateid
*stp
)
451 __clear_bit(access
, &stp
->st_deny_bmap
);
454 /* test whether a given stateid is denying specific access */
456 test_deny(u32 access
, struct nfs4_ol_stateid
*stp
)
458 return test_bit(access
, &stp
->st_deny_bmap
);
461 static int nfs4_access_to_omode(u32 access
)
463 switch (access
& NFS4_SHARE_ACCESS_BOTH
) {
464 case NFS4_SHARE_ACCESS_READ
:
466 case NFS4_SHARE_ACCESS_WRITE
:
468 case NFS4_SHARE_ACCESS_BOTH
:
475 /* release all access and file references for a given stateid */
477 release_all_access(struct nfs4_ol_stateid
*stp
)
481 for (i
= 1; i
< 4; i
++) {
482 if (test_access(i
, stp
))
483 nfs4_file_put_access(stp
->st_file
,
484 nfs4_access_to_omode(i
));
485 clear_access(i
, stp
);
489 static void unhash_generic_stateid(struct nfs4_ol_stateid
*stp
)
491 list_del(&stp
->st_perfile
);
492 list_del(&stp
->st_perstateowner
);
495 static void close_generic_stateid(struct nfs4_ol_stateid
*stp
)
497 release_all_access(stp
);
498 put_nfs4_file(stp
->st_file
);
502 static void free_generic_stateid(struct nfs4_ol_stateid
*stp
)
504 free_stid(&stp
->st_stid
, stateid_slab
);
507 static void release_lock_stateid(struct nfs4_ol_stateid
*stp
)
511 unhash_generic_stateid(stp
);
512 unhash_stid(&stp
->st_stid
);
513 file
= find_any_file(stp
->st_file
);
515 locks_remove_posix(file
, (fl_owner_t
)lockowner(stp
->st_stateowner
));
516 close_generic_stateid(stp
);
517 free_generic_stateid(stp
);
520 static void unhash_lockowner(struct nfs4_lockowner
*lo
)
522 struct nfs4_ol_stateid
*stp
;
524 list_del(&lo
->lo_owner
.so_strhash
);
525 list_del(&lo
->lo_perstateid
);
526 list_del(&lo
->lo_owner_ino_hash
);
527 while (!list_empty(&lo
->lo_owner
.so_stateids
)) {
528 stp
= list_first_entry(&lo
->lo_owner
.so_stateids
,
529 struct nfs4_ol_stateid
, st_perstateowner
);
530 release_lock_stateid(stp
);
534 static void release_lockowner(struct nfs4_lockowner
*lo
)
536 unhash_lockowner(lo
);
537 nfs4_free_lockowner(lo
);
541 release_stateid_lockowners(struct nfs4_ol_stateid
*open_stp
)
543 struct nfs4_lockowner
*lo
;
545 while (!list_empty(&open_stp
->st_lockowners
)) {
546 lo
= list_entry(open_stp
->st_lockowners
.next
,
547 struct nfs4_lockowner
, lo_perstateid
);
548 release_lockowner(lo
);
552 static void unhash_open_stateid(struct nfs4_ol_stateid
*stp
)
554 unhash_generic_stateid(stp
);
555 release_stateid_lockowners(stp
);
556 close_generic_stateid(stp
);
559 static void release_open_stateid(struct nfs4_ol_stateid
*stp
)
561 unhash_open_stateid(stp
);
562 unhash_stid(&stp
->st_stid
);
563 free_generic_stateid(stp
);
566 static void unhash_openowner(struct nfs4_openowner
*oo
)
568 struct nfs4_ol_stateid
*stp
;
570 list_del(&oo
->oo_owner
.so_strhash
);
571 list_del(&oo
->oo_perclient
);
572 while (!list_empty(&oo
->oo_owner
.so_stateids
)) {
573 stp
= list_first_entry(&oo
->oo_owner
.so_stateids
,
574 struct nfs4_ol_stateid
, st_perstateowner
);
575 release_open_stateid(stp
);
579 static void release_last_closed_stateid(struct nfs4_openowner
*oo
)
581 struct nfs4_ol_stateid
*s
= oo
->oo_last_closed_stid
;
584 unhash_stid(&s
->st_stid
);
585 free_generic_stateid(s
);
586 oo
->oo_last_closed_stid
= NULL
;
590 static void release_openowner(struct nfs4_openowner
*oo
)
592 unhash_openowner(oo
);
593 list_del(&oo
->oo_close_lru
);
594 release_last_closed_stateid(oo
);
595 nfs4_free_openowner(oo
);
599 hash_sessionid(struct nfs4_sessionid
*sessionid
)
601 struct nfsd4_sessionid
*sid
= (struct nfsd4_sessionid
*)sessionid
;
603 return sid
->sequence
% SESSION_HASH_SIZE
;
608 dump_sessionid(const char *fn
, struct nfs4_sessionid
*sessionid
)
610 u32
*ptr
= (u32
*)(&sessionid
->data
[0]);
611 dprintk("%s: %u:%u:%u:%u\n", fn
, ptr
[0], ptr
[1], ptr
[2], ptr
[3]);
615 dump_sessionid(const char *fn
, struct nfs4_sessionid
*sessionid
)
622 gen_sessionid(struct nfsd4_session
*ses
)
624 struct nfs4_client
*clp
= ses
->se_client
;
625 struct nfsd4_sessionid
*sid
;
627 sid
= (struct nfsd4_sessionid
*)ses
->se_sessionid
.data
;
628 sid
->clientid
= clp
->cl_clientid
;
629 sid
->sequence
= current_sessionid
++;
634 * The protocol defines ca_maxresponssize_cached to include the size of
635 * the rpc header, but all we need to cache is the data starting after
636 * the end of the initial SEQUENCE operation--the rest we regenerate
637 * each time. Therefore we can advertise a ca_maxresponssize_cached
638 * value that is the number of bytes in our cache plus a few additional
639 * bytes. In order to stay on the safe side, and not promise more than
640 * we can cache, those additional bytes must be the minimum possible: 24
641 * bytes of rpc header (xid through accept state, with AUTH_NULL
642 * verifier), 12 for the compound header (with zero-length tag), and 44
643 * for the SEQUENCE op response:
645 #define NFSD_MIN_HDR_SEQ_SZ (24 + 12 + 44)
648 free_session_slots(struct nfsd4_session
*ses
)
652 for (i
= 0; i
< ses
->se_fchannel
.maxreqs
; i
++)
653 kfree(ses
->se_slots
[i
]);
657 * We don't actually need to cache the rpc and session headers, so we
658 * can allocate a little less for each slot:
660 static inline int slot_bytes(struct nfsd4_channel_attrs
*ca
)
662 return ca
->maxresp_cached
- NFSD_MIN_HDR_SEQ_SZ
;
665 static int nfsd4_sanitize_slot_size(u32 size
)
667 size
-= NFSD_MIN_HDR_SEQ_SZ
; /* We don't cache the rpc header */
668 size
= min_t(u32
, size
, NFSD_SLOT_CACHE_SIZE
);
674 * XXX: If we run out of reserved DRC memory we could (up to a point)
675 * re-negotiate active sessions and reduce their slot usage to make
676 * room for new connections. For now we just fail the create session.
678 static int nfsd4_get_drc_mem(int slotsize
, u32 num
)
682 num
= min_t(u32
, num
, NFSD_MAX_SLOTS_PER_SESSION
);
684 spin_lock(&nfsd_drc_lock
);
685 avail
= min((unsigned long)NFSD_MAX_MEM_PER_SESSION
,
686 nfsd_drc_max_mem
- nfsd_drc_mem_used
);
687 num
= min_t(int, num
, avail
/ slotsize
);
688 nfsd_drc_mem_used
+= num
* slotsize
;
689 spin_unlock(&nfsd_drc_lock
);
694 static void nfsd4_put_drc_mem(int slotsize
, int num
)
696 spin_lock(&nfsd_drc_lock
);
697 nfsd_drc_mem_used
-= slotsize
* num
;
698 spin_unlock(&nfsd_drc_lock
);
701 static struct nfsd4_session
*__alloc_session(int slotsize
, int numslots
)
703 struct nfsd4_session
*new;
706 BUILD_BUG_ON(NFSD_MAX_SLOTS_PER_SESSION
* sizeof(struct nfsd4_slot
*)
707 + sizeof(struct nfsd4_session
) > PAGE_SIZE
);
708 mem
= numslots
* sizeof(struct nfsd4_slot
*);
710 new = kzalloc(sizeof(*new) + mem
, GFP_KERNEL
);
713 /* allocate each struct nfsd4_slot and data cache in one piece */
714 for (i
= 0; i
< numslots
; i
++) {
715 mem
= sizeof(struct nfsd4_slot
) + slotsize
;
716 new->se_slots
[i
] = kzalloc(mem
, GFP_KERNEL
);
717 if (!new->se_slots
[i
])
723 kfree(new->se_slots
[i
]);
728 static void init_forechannel_attrs(struct nfsd4_channel_attrs
*new,
729 struct nfsd4_channel_attrs
*req
,
730 int numslots
, int slotsize
,
733 u32 maxrpc
= nn
->nfsd_serv
->sv_max_mesg
;
735 new->maxreqs
= numslots
;
736 new->maxresp_cached
= min_t(u32
, req
->maxresp_cached
,
737 slotsize
+ NFSD_MIN_HDR_SEQ_SZ
);
738 new->maxreq_sz
= min_t(u32
, req
->maxreq_sz
, maxrpc
);
739 new->maxresp_sz
= min_t(u32
, req
->maxresp_sz
, maxrpc
);
740 new->maxops
= min_t(u32
, req
->maxops
, NFSD_MAX_OPS_PER_COMPOUND
);
743 static void free_conn(struct nfsd4_conn
*c
)
745 svc_xprt_put(c
->cn_xprt
);
749 static void nfsd4_conn_lost(struct svc_xpt_user
*u
)
751 struct nfsd4_conn
*c
= container_of(u
, struct nfsd4_conn
, cn_xpt_user
);
752 struct nfs4_client
*clp
= c
->cn_session
->se_client
;
754 spin_lock(&clp
->cl_lock
);
755 if (!list_empty(&c
->cn_persession
)) {
756 list_del(&c
->cn_persession
);
759 spin_unlock(&clp
->cl_lock
);
760 nfsd4_probe_callback(clp
);
763 static struct nfsd4_conn
*alloc_conn(struct svc_rqst
*rqstp
, u32 flags
)
765 struct nfsd4_conn
*conn
;
767 conn
= kmalloc(sizeof(struct nfsd4_conn
), GFP_KERNEL
);
770 svc_xprt_get(rqstp
->rq_xprt
);
771 conn
->cn_xprt
= rqstp
->rq_xprt
;
772 conn
->cn_flags
= flags
;
773 INIT_LIST_HEAD(&conn
->cn_xpt_user
.list
);
777 static void __nfsd4_hash_conn(struct nfsd4_conn
*conn
, struct nfsd4_session
*ses
)
779 conn
->cn_session
= ses
;
780 list_add(&conn
->cn_persession
, &ses
->se_conns
);
783 static void nfsd4_hash_conn(struct nfsd4_conn
*conn
, struct nfsd4_session
*ses
)
785 struct nfs4_client
*clp
= ses
->se_client
;
787 spin_lock(&clp
->cl_lock
);
788 __nfsd4_hash_conn(conn
, ses
);
789 spin_unlock(&clp
->cl_lock
);
792 static int nfsd4_register_conn(struct nfsd4_conn
*conn
)
794 conn
->cn_xpt_user
.callback
= nfsd4_conn_lost
;
795 return register_xpt_user(conn
->cn_xprt
, &conn
->cn_xpt_user
);
798 static void nfsd4_init_conn(struct svc_rqst
*rqstp
, struct nfsd4_conn
*conn
, struct nfsd4_session
*ses
)
802 nfsd4_hash_conn(conn
, ses
);
803 ret
= nfsd4_register_conn(conn
);
805 /* oops; xprt is already down: */
806 nfsd4_conn_lost(&conn
->cn_xpt_user
);
807 if (conn
->cn_flags
& NFS4_CDFC4_BACK
) {
808 /* callback channel may be back up */
809 nfsd4_probe_callback(ses
->se_client
);
813 static struct nfsd4_conn
*alloc_conn_from_crses(struct svc_rqst
*rqstp
, struct nfsd4_create_session
*cses
)
815 u32 dir
= NFS4_CDFC4_FORE
;
817 if (cses
->flags
& SESSION4_BACK_CHAN
)
818 dir
|= NFS4_CDFC4_BACK
;
819 return alloc_conn(rqstp
, dir
);
822 /* must be called under client_lock */
823 static void nfsd4_del_conns(struct nfsd4_session
*s
)
825 struct nfs4_client
*clp
= s
->se_client
;
826 struct nfsd4_conn
*c
;
828 spin_lock(&clp
->cl_lock
);
829 while (!list_empty(&s
->se_conns
)) {
830 c
= list_first_entry(&s
->se_conns
, struct nfsd4_conn
, cn_persession
);
831 list_del_init(&c
->cn_persession
);
832 spin_unlock(&clp
->cl_lock
);
834 unregister_xpt_user(c
->cn_xprt
, &c
->cn_xpt_user
);
837 spin_lock(&clp
->cl_lock
);
839 spin_unlock(&clp
->cl_lock
);
842 static void __free_session(struct nfsd4_session
*ses
)
844 nfsd4_put_drc_mem(slot_bytes(&ses
->se_fchannel
), ses
->se_fchannel
.maxreqs
);
845 free_session_slots(ses
);
849 static void free_session(struct kref
*kref
)
851 struct nfsd4_session
*ses
;
854 ses
= container_of(kref
, struct nfsd4_session
, se_ref
);
855 nn
= net_generic(ses
->se_client
->net
, nfsd_net_id
);
857 lockdep_assert_held(&nn
->client_lock
);
858 nfsd4_del_conns(ses
);
862 void nfsd4_put_session(struct nfsd4_session
*ses
)
864 struct nfsd_net
*nn
= net_generic(ses
->se_client
->net
, nfsd_net_id
);
866 spin_lock(&nn
->client_lock
);
867 nfsd4_put_session_locked(ses
);
868 spin_unlock(&nn
->client_lock
);
871 static struct nfsd4_session
*alloc_session(struct nfsd4_channel_attrs
*fchan
,
874 struct nfsd4_session
*new;
875 int numslots
, slotsize
;
877 * Note decreasing slot size below client's request may
878 * make it difficult for client to function correctly, whereas
879 * decreasing the number of slots will (just?) affect
880 * performance. When short on memory we therefore prefer to
881 * decrease number of slots instead of their size.
883 slotsize
= nfsd4_sanitize_slot_size(fchan
->maxresp_cached
);
884 numslots
= nfsd4_get_drc_mem(slotsize
, fchan
->maxreqs
);
888 new = __alloc_session(slotsize
, numslots
);
890 nfsd4_put_drc_mem(slotsize
, numslots
);
893 init_forechannel_attrs(&new->se_fchannel
, fchan
, numslots
, slotsize
, nn
);
897 static void init_session(struct svc_rqst
*rqstp
, struct nfsd4_session
*new, struct nfs4_client
*clp
, struct nfsd4_create_session
*cses
)
900 struct nfsd_net
*nn
= net_generic(SVC_NET(rqstp
), nfsd_net_id
);
902 new->se_client
= clp
;
905 INIT_LIST_HEAD(&new->se_conns
);
907 new->se_cb_seq_nr
= 1;
908 new->se_flags
= cses
->flags
;
909 new->se_cb_prog
= cses
->callback_prog
;
910 new->se_cb_sec
= cses
->cb_sec
;
911 kref_init(&new->se_ref
);
912 idx
= hash_sessionid(&new->se_sessionid
);
913 spin_lock(&nn
->client_lock
);
914 list_add(&new->se_hash
, &nn
->sessionid_hashtbl
[idx
]);
915 spin_lock(&clp
->cl_lock
);
916 list_add(&new->se_perclnt
, &clp
->cl_sessions
);
917 spin_unlock(&clp
->cl_lock
);
918 spin_unlock(&nn
->client_lock
);
920 if (cses
->flags
& SESSION4_BACK_CHAN
) {
921 struct sockaddr
*sa
= svc_addr(rqstp
);
923 * This is a little silly; with sessions there's no real
924 * use for the callback address. Use the peer address
925 * as a reasonable default for now, but consider fixing
926 * the rpc client not to require an address in the
929 rpc_copy_addr((struct sockaddr
*)&clp
->cl_cb_conn
.cb_addr
, sa
);
930 clp
->cl_cb_conn
.cb_addrlen
= svc_addr_len(sa
);
934 /* caller must hold client_lock */
935 static struct nfsd4_session
*
936 find_in_sessionid_hashtbl(struct nfs4_sessionid
*sessionid
, struct net
*net
)
938 struct nfsd4_session
*elem
;
940 struct nfsd_net
*nn
= net_generic(net
, nfsd_net_id
);
942 dump_sessionid(__func__
, sessionid
);
943 idx
= hash_sessionid(sessionid
);
944 /* Search in the appropriate list */
945 list_for_each_entry(elem
, &nn
->sessionid_hashtbl
[idx
], se_hash
) {
946 if (!memcmp(elem
->se_sessionid
.data
, sessionid
->data
,
947 NFS4_MAX_SESSIONID_LEN
)) {
952 dprintk("%s: session not found\n", __func__
);
956 /* caller must hold client_lock */
958 unhash_session(struct nfsd4_session
*ses
)
960 list_del(&ses
->se_hash
);
961 spin_lock(&ses
->se_client
->cl_lock
);
962 list_del(&ses
->se_perclnt
);
963 spin_unlock(&ses
->se_client
->cl_lock
);
966 /* must be called under the client_lock */
968 renew_client_locked(struct nfs4_client
*clp
)
970 struct nfsd_net
*nn
= net_generic(clp
->net
, nfsd_net_id
);
972 if (is_client_expired(clp
)) {
974 printk("%s: client (clientid %08x/%08x) already expired\n",
976 clp
->cl_clientid
.cl_boot
,
977 clp
->cl_clientid
.cl_id
);
981 dprintk("renewing client (clientid %08x/%08x)\n",
982 clp
->cl_clientid
.cl_boot
,
983 clp
->cl_clientid
.cl_id
);
984 list_move_tail(&clp
->cl_lru
, &nn
->client_lru
);
985 clp
->cl_time
= get_seconds();
989 renew_client(struct nfs4_client
*clp
)
991 struct nfsd_net
*nn
= net_generic(clp
->net
, nfsd_net_id
);
993 spin_lock(&nn
->client_lock
);
994 renew_client_locked(clp
);
995 spin_unlock(&nn
->client_lock
);
998 /* SETCLIENTID and SETCLIENTID_CONFIRM Helper functions */
1000 STALE_CLIENTID(clientid_t
*clid
, struct nfsd_net
*nn
)
1002 if (clid
->cl_boot
== nn
->boot_time
)
1004 dprintk("NFSD stale clientid (%08x/%08x) boot_time %08lx\n",
1005 clid
->cl_boot
, clid
->cl_id
, nn
->boot_time
);
1010 * XXX Should we use a slab cache ?
1011 * This type of memory management is somewhat inefficient, but we use it
1012 * anyway since SETCLIENTID is not a common operation.
1014 static struct nfs4_client
*alloc_client(struct xdr_netobj name
)
1016 struct nfs4_client
*clp
;
1018 clp
= kzalloc(sizeof(struct nfs4_client
), GFP_KERNEL
);
1021 clp
->cl_name
.data
= kmemdup(name
.data
, name
.len
, GFP_KERNEL
);
1022 if (clp
->cl_name
.data
== NULL
) {
1026 clp
->cl_name
.len
= name
.len
;
1031 free_client(struct nfs4_client
*clp
)
1033 struct nfsd_net __maybe_unused
*nn
= net_generic(clp
->net
, nfsd_net_id
);
1035 lockdep_assert_held(&nn
->client_lock
);
1036 while (!list_empty(&clp
->cl_sessions
)) {
1037 struct nfsd4_session
*ses
;
1038 ses
= list_entry(clp
->cl_sessions
.next
, struct nfsd4_session
,
1040 list_del(&ses
->se_perclnt
);
1041 nfsd4_put_session_locked(ses
);
1043 free_svc_cred(&clp
->cl_cred
);
1044 kfree(clp
->cl_name
.data
);
1045 idr_destroy(&clp
->cl_stateids
);
1050 release_session_client(struct nfsd4_session
*session
)
1052 struct nfs4_client
*clp
= session
->se_client
;
1053 struct nfsd_net
*nn
= net_generic(clp
->net
, nfsd_net_id
);
1055 if (!atomic_dec_and_lock(&clp
->cl_refcount
, &nn
->client_lock
))
1057 if (is_client_expired(clp
)) {
1059 session
->se_client
= NULL
;
1061 renew_client_locked(clp
);
1062 spin_unlock(&nn
->client_lock
);
1065 /* must be called under the client_lock */
1067 unhash_client_locked(struct nfs4_client
*clp
)
1069 struct nfsd4_session
*ses
;
1071 mark_client_expired(clp
);
1072 list_del(&clp
->cl_lru
);
1073 spin_lock(&clp
->cl_lock
);
1074 list_for_each_entry(ses
, &clp
->cl_sessions
, se_perclnt
)
1075 list_del_init(&ses
->se_hash
);
1076 spin_unlock(&clp
->cl_lock
);
1080 destroy_client(struct nfs4_client
*clp
)
1082 struct nfs4_openowner
*oo
;
1083 struct nfs4_delegation
*dp
;
1084 struct list_head reaplist
;
1085 struct nfsd_net
*nn
= net_generic(clp
->net
, nfsd_net_id
);
1087 INIT_LIST_HEAD(&reaplist
);
1088 spin_lock(&recall_lock
);
1089 while (!list_empty(&clp
->cl_delegations
)) {
1090 dp
= list_entry(clp
->cl_delegations
.next
, struct nfs4_delegation
, dl_perclnt
);
1091 list_del_init(&dp
->dl_perclnt
);
1092 list_move(&dp
->dl_recall_lru
, &reaplist
);
1094 spin_unlock(&recall_lock
);
1095 while (!list_empty(&reaplist
)) {
1096 dp
= list_entry(reaplist
.next
, struct nfs4_delegation
, dl_recall_lru
);
1097 unhash_delegation(dp
);
1099 while (!list_empty(&clp
->cl_openowners
)) {
1100 oo
= list_entry(clp
->cl_openowners
.next
, struct nfs4_openowner
, oo_perclient
);
1101 release_openowner(oo
);
1103 nfsd4_shutdown_callback(clp
);
1104 if (clp
->cl_cb_conn
.cb_xprt
)
1105 svc_xprt_put(clp
->cl_cb_conn
.cb_xprt
);
1106 list_del(&clp
->cl_idhash
);
1107 if (test_bit(NFSD4_CLIENT_CONFIRMED
, &clp
->cl_flags
))
1108 rb_erase(&clp
->cl_namenode
, &nn
->conf_name_tree
);
1110 rb_erase(&clp
->cl_namenode
, &nn
->unconf_name_tree
);
1111 spin_lock(&nn
->client_lock
);
1112 unhash_client_locked(clp
);
1113 if (atomic_read(&clp
->cl_refcount
) == 0)
1115 spin_unlock(&nn
->client_lock
);
1118 static void expire_client(struct nfs4_client
*clp
)
1120 nfsd4_client_record_remove(clp
);
1121 destroy_client(clp
);
1124 static void copy_verf(struct nfs4_client
*target
, nfs4_verifier
*source
)
1126 memcpy(target
->cl_verifier
.data
, source
->data
,
1127 sizeof(target
->cl_verifier
.data
));
1130 static void copy_clid(struct nfs4_client
*target
, struct nfs4_client
*source
)
1132 target
->cl_clientid
.cl_boot
= source
->cl_clientid
.cl_boot
;
1133 target
->cl_clientid
.cl_id
= source
->cl_clientid
.cl_id
;
1136 static int copy_cred(struct svc_cred
*target
, struct svc_cred
*source
)
1138 if (source
->cr_principal
) {
1139 target
->cr_principal
=
1140 kstrdup(source
->cr_principal
, GFP_KERNEL
);
1141 if (target
->cr_principal
== NULL
)
1144 target
->cr_principal
= NULL
;
1145 target
->cr_flavor
= source
->cr_flavor
;
1146 target
->cr_uid
= source
->cr_uid
;
1147 target
->cr_gid
= source
->cr_gid
;
1148 target
->cr_group_info
= source
->cr_group_info
;
1149 get_group_info(target
->cr_group_info
);
1154 compare_blob(const struct xdr_netobj
*o1
, const struct xdr_netobj
*o2
)
1158 res
= o1
->len
- o2
->len
;
1161 return (long long)memcmp(o1
->data
, o2
->data
, o1
->len
);
1164 static int same_name(const char *n1
, const char *n2
)
1166 return 0 == memcmp(n1
, n2
, HEXDIR_LEN
);
1170 same_verf(nfs4_verifier
*v1
, nfs4_verifier
*v2
)
1172 return 0 == memcmp(v1
->data
, v2
->data
, sizeof(v1
->data
));
1176 same_clid(clientid_t
*cl1
, clientid_t
*cl2
)
1178 return (cl1
->cl_boot
== cl2
->cl_boot
) && (cl1
->cl_id
== cl2
->cl_id
);
1181 static bool groups_equal(struct group_info
*g1
, struct group_info
*g2
)
1185 if (g1
->ngroups
!= g2
->ngroups
)
1187 for (i
=0; i
<g1
->ngroups
; i
++)
1188 if (!gid_eq(GROUP_AT(g1
, i
), GROUP_AT(g2
, i
)))
1194 * RFC 3530 language requires clid_inuse be returned when the
1195 * "principal" associated with a requests differs from that previously
1196 * used. We use uid, gid's, and gss principal string as our best
1197 * approximation. We also don't want to allow non-gss use of a client
1198 * established using gss: in theory cr_principal should catch that
1199 * change, but in practice cr_principal can be null even in the gss case
1200 * since gssd doesn't always pass down a principal string.
1202 static bool is_gss_cred(struct svc_cred
*cr
)
1204 /* Is cr_flavor one of the gss "pseudoflavors"?: */
1205 return (cr
->cr_flavor
> RPC_AUTH_MAXFLAVOR
);
1210 same_creds(struct svc_cred
*cr1
, struct svc_cred
*cr2
)
1212 if ((is_gss_cred(cr1
) != is_gss_cred(cr2
))
1213 || (!uid_eq(cr1
->cr_uid
, cr2
->cr_uid
))
1214 || (!gid_eq(cr1
->cr_gid
, cr2
->cr_gid
))
1215 || !groups_equal(cr1
->cr_group_info
, cr2
->cr_group_info
))
1217 if (cr1
->cr_principal
== cr2
->cr_principal
)
1219 if (!cr1
->cr_principal
|| !cr2
->cr_principal
)
1221 return 0 == strcmp(cr1
->cr_principal
, cr2
->cr_principal
);
1224 static void gen_clid(struct nfs4_client
*clp
, struct nfsd_net
*nn
)
1226 static u32 current_clientid
= 1;
1228 clp
->cl_clientid
.cl_boot
= nn
->boot_time
;
1229 clp
->cl_clientid
.cl_id
= current_clientid
++;
1232 static void gen_confirm(struct nfs4_client
*clp
)
1237 verf
[0] = (__be32
)get_seconds();
1238 verf
[1] = (__be32
)i
++;
1239 memcpy(clp
->cl_confirm
.data
, verf
, sizeof(clp
->cl_confirm
.data
));
1242 static struct nfs4_stid
*find_stateid(struct nfs4_client
*cl
, stateid_t
*t
)
1244 struct nfs4_stid
*ret
;
1246 ret
= idr_find(&cl
->cl_stateids
, t
->si_opaque
.so_id
);
1247 if (!ret
|| !ret
->sc_type
)
1252 static struct nfs4_stid
*find_stateid_by_type(struct nfs4_client
*cl
, stateid_t
*t
, char typemask
)
1254 struct nfs4_stid
*s
;
1256 s
= find_stateid(cl
, t
);
1259 if (typemask
& s
->sc_type
)
1264 static struct nfs4_client
*create_client(struct xdr_netobj name
,
1265 struct svc_rqst
*rqstp
, nfs4_verifier
*verf
)
1267 struct nfs4_client
*clp
;
1268 struct sockaddr
*sa
= svc_addr(rqstp
);
1270 struct net
*net
= SVC_NET(rqstp
);
1271 struct nfsd_net
*nn
= net_generic(net
, nfsd_net_id
);
1273 clp
= alloc_client(name
);
1277 INIT_LIST_HEAD(&clp
->cl_sessions
);
1278 ret
= copy_cred(&clp
->cl_cred
, &rqstp
->rq_cred
);
1280 spin_lock(&nn
->client_lock
);
1282 spin_unlock(&nn
->client_lock
);
1285 idr_init(&clp
->cl_stateids
);
1286 atomic_set(&clp
->cl_refcount
, 0);
1287 clp
->cl_cb_state
= NFSD4_CB_UNKNOWN
;
1288 INIT_LIST_HEAD(&clp
->cl_idhash
);
1289 INIT_LIST_HEAD(&clp
->cl_openowners
);
1290 INIT_LIST_HEAD(&clp
->cl_delegations
);
1291 INIT_LIST_HEAD(&clp
->cl_lru
);
1292 INIT_LIST_HEAD(&clp
->cl_callbacks
);
1293 spin_lock_init(&clp
->cl_lock
);
1294 nfsd4_init_callback(&clp
->cl_cb_null
);
1295 clp
->cl_time
= get_seconds();
1296 clear_bit(0, &clp
->cl_cb_slot_busy
);
1297 rpc_init_wait_queue(&clp
->cl_cb_waitq
, "Backchannel slot table");
1298 copy_verf(clp
, verf
);
1299 rpc_copy_addr((struct sockaddr
*) &clp
->cl_addr
, sa
);
1301 clp
->cl_cb_session
= NULL
;
1307 add_clp_to_name_tree(struct nfs4_client
*new_clp
, struct rb_root
*root
)
1309 struct rb_node
**new = &(root
->rb_node
), *parent
= NULL
;
1310 struct nfs4_client
*clp
;
1313 clp
= rb_entry(*new, struct nfs4_client
, cl_namenode
);
1316 if (compare_blob(&clp
->cl_name
, &new_clp
->cl_name
) > 0)
1317 new = &((*new)->rb_left
);
1319 new = &((*new)->rb_right
);
1322 rb_link_node(&new_clp
->cl_namenode
, parent
, new);
1323 rb_insert_color(&new_clp
->cl_namenode
, root
);
1326 static struct nfs4_client
*
1327 find_clp_in_name_tree(struct xdr_netobj
*name
, struct rb_root
*root
)
1330 struct rb_node
*node
= root
->rb_node
;
1331 struct nfs4_client
*clp
;
1334 clp
= rb_entry(node
, struct nfs4_client
, cl_namenode
);
1335 cmp
= compare_blob(&clp
->cl_name
, name
);
1337 node
= node
->rb_left
;
1339 node
= node
->rb_right
;
1347 add_to_unconfirmed(struct nfs4_client
*clp
)
1349 unsigned int idhashval
;
1350 struct nfsd_net
*nn
= net_generic(clp
->net
, nfsd_net_id
);
1352 clear_bit(NFSD4_CLIENT_CONFIRMED
, &clp
->cl_flags
);
1353 add_clp_to_name_tree(clp
, &nn
->unconf_name_tree
);
1354 idhashval
= clientid_hashval(clp
->cl_clientid
.cl_id
);
1355 list_add(&clp
->cl_idhash
, &nn
->unconf_id_hashtbl
[idhashval
]);
1360 move_to_confirmed(struct nfs4_client
*clp
)
1362 unsigned int idhashval
= clientid_hashval(clp
->cl_clientid
.cl_id
);
1363 struct nfsd_net
*nn
= net_generic(clp
->net
, nfsd_net_id
);
1365 dprintk("NFSD: move_to_confirm nfs4_client %p\n", clp
);
1366 list_move(&clp
->cl_idhash
, &nn
->conf_id_hashtbl
[idhashval
]);
1367 rb_erase(&clp
->cl_namenode
, &nn
->unconf_name_tree
);
1368 add_clp_to_name_tree(clp
, &nn
->conf_name_tree
);
1369 set_bit(NFSD4_CLIENT_CONFIRMED
, &clp
->cl_flags
);
1373 static struct nfs4_client
*
1374 find_confirmed_client(clientid_t
*clid
, bool sessions
, struct nfsd_net
*nn
)
1376 struct nfs4_client
*clp
;
1377 unsigned int idhashval
= clientid_hashval(clid
->cl_id
);
1379 list_for_each_entry(clp
, &nn
->conf_id_hashtbl
[idhashval
], cl_idhash
) {
1380 if (same_clid(&clp
->cl_clientid
, clid
)) {
1381 if ((bool)clp
->cl_minorversion
!= sessions
)
1390 static struct nfs4_client
*
1391 find_unconfirmed_client(clientid_t
*clid
, bool sessions
, struct nfsd_net
*nn
)
1393 struct nfs4_client
*clp
;
1394 unsigned int idhashval
= clientid_hashval(clid
->cl_id
);
1396 list_for_each_entry(clp
, &nn
->unconf_id_hashtbl
[idhashval
], cl_idhash
) {
1397 if (same_clid(&clp
->cl_clientid
, clid
)) {
1398 if ((bool)clp
->cl_minorversion
!= sessions
)
1406 static bool clp_used_exchangeid(struct nfs4_client
*clp
)
1408 return clp
->cl_exchange_flags
!= 0;
1411 static struct nfs4_client
*
1412 find_confirmed_client_by_name(struct xdr_netobj
*name
, struct nfsd_net
*nn
)
1414 return find_clp_in_name_tree(name
, &nn
->conf_name_tree
);
1417 static struct nfs4_client
*
1418 find_unconfirmed_client_by_name(struct xdr_netobj
*name
, struct nfsd_net
*nn
)
1420 return find_clp_in_name_tree(name
, &nn
->unconf_name_tree
);
1424 gen_callback(struct nfs4_client
*clp
, struct nfsd4_setclientid
*se
, struct svc_rqst
*rqstp
)
1426 struct nfs4_cb_conn
*conn
= &clp
->cl_cb_conn
;
1427 struct sockaddr
*sa
= svc_addr(rqstp
);
1428 u32 scopeid
= rpc_get_scope_id(sa
);
1429 unsigned short expected_family
;
1431 /* Currently, we only support tcp and tcp6 for the callback channel */
1432 if (se
->se_callback_netid_len
== 3 &&
1433 !memcmp(se
->se_callback_netid_val
, "tcp", 3))
1434 expected_family
= AF_INET
;
1435 else if (se
->se_callback_netid_len
== 4 &&
1436 !memcmp(se
->se_callback_netid_val
, "tcp6", 4))
1437 expected_family
= AF_INET6
;
1441 conn
->cb_addrlen
= rpc_uaddr2sockaddr(clp
->net
, se
->se_callback_addr_val
,
1442 se
->se_callback_addr_len
,
1443 (struct sockaddr
*)&conn
->cb_addr
,
1444 sizeof(conn
->cb_addr
));
1446 if (!conn
->cb_addrlen
|| conn
->cb_addr
.ss_family
!= expected_family
)
1449 if (conn
->cb_addr
.ss_family
== AF_INET6
)
1450 ((struct sockaddr_in6
*)&conn
->cb_addr
)->sin6_scope_id
= scopeid
;
1452 conn
->cb_prog
= se
->se_callback_prog
;
1453 conn
->cb_ident
= se
->se_callback_ident
;
1454 memcpy(&conn
->cb_saddr
, &rqstp
->rq_daddr
, rqstp
->rq_daddrlen
);
1457 conn
->cb_addr
.ss_family
= AF_UNSPEC
;
1458 conn
->cb_addrlen
= 0;
1459 dprintk(KERN_INFO
"NFSD: this client (clientid %08x/%08x) "
1460 "will not receive delegations\n",
1461 clp
->cl_clientid
.cl_boot
, clp
->cl_clientid
.cl_id
);
1467 * Cache a reply. nfsd4_check_drc_limit() has bounded the cache size.
1470 nfsd4_store_cache_entry(struct nfsd4_compoundres
*resp
)
1472 struct nfsd4_slot
*slot
= resp
->cstate
.slot
;
1475 dprintk("--> %s slot %p\n", __func__
, slot
);
1477 slot
->sl_opcnt
= resp
->opcnt
;
1478 slot
->sl_status
= resp
->cstate
.status
;
1480 slot
->sl_flags
|= NFSD4_SLOT_INITIALIZED
;
1481 if (nfsd4_not_cached(resp
)) {
1482 slot
->sl_datalen
= 0;
1485 slot
->sl_datalen
= (char *)resp
->p
- (char *)resp
->cstate
.datap
;
1486 base
= (char *)resp
->cstate
.datap
-
1487 (char *)resp
->xbuf
->head
[0].iov_base
;
1488 if (read_bytes_from_xdr_buf(resp
->xbuf
, base
, slot
->sl_data
,
1490 WARN("%s: sessions DRC could not cache compound\n", __func__
);
1495 * Encode the replay sequence operation from the slot values.
1496 * If cachethis is FALSE encode the uncached rep error on the next
1497 * operation which sets resp->p and increments resp->opcnt for
1498 * nfs4svc_encode_compoundres.
1502 nfsd4_enc_sequence_replay(struct nfsd4_compoundargs
*args
,
1503 struct nfsd4_compoundres
*resp
)
1505 struct nfsd4_op
*op
;
1506 struct nfsd4_slot
*slot
= resp
->cstate
.slot
;
1508 /* Encode the replayed sequence operation */
1509 op
= &args
->ops
[resp
->opcnt
- 1];
1510 nfsd4_encode_operation(resp
, op
);
1512 /* Return nfserr_retry_uncached_rep in next operation. */
1513 if (args
->opcnt
> 1 && !(slot
->sl_flags
& NFSD4_SLOT_CACHETHIS
)) {
1514 op
= &args
->ops
[resp
->opcnt
++];
1515 op
->status
= nfserr_retry_uncached_rep
;
1516 nfsd4_encode_operation(resp
, op
);
1522 * The sequence operation is not cached because we can use the slot and
1526 nfsd4_replay_cache_entry(struct nfsd4_compoundres
*resp
,
1527 struct nfsd4_sequence
*seq
)
1529 struct nfsd4_slot
*slot
= resp
->cstate
.slot
;
1532 dprintk("--> %s slot %p\n", __func__
, slot
);
1534 /* Either returns 0 or nfserr_retry_uncached */
1535 status
= nfsd4_enc_sequence_replay(resp
->rqstp
->rq_argp
, resp
);
1536 if (status
== nfserr_retry_uncached_rep
)
1539 /* The sequence operation has been encoded, cstate->datap set. */
1540 memcpy(resp
->cstate
.datap
, slot
->sl_data
, slot
->sl_datalen
);
1542 resp
->opcnt
= slot
->sl_opcnt
;
1543 resp
->p
= resp
->cstate
.datap
+ XDR_QUADLEN(slot
->sl_datalen
);
1544 status
= slot
->sl_status
;
1550 * Set the exchange_id flags returned by the server.
1553 nfsd4_set_ex_flags(struct nfs4_client
*new, struct nfsd4_exchange_id
*clid
)
1555 /* pNFS is not supported */
1556 new->cl_exchange_flags
|= EXCHGID4_FLAG_USE_NON_PNFS
;
1558 /* Referrals are supported, Migration is not. */
1559 new->cl_exchange_flags
|= EXCHGID4_FLAG_SUPP_MOVED_REFER
;
1561 /* set the wire flags to return to client. */
1562 clid
->flags
= new->cl_exchange_flags
;
1565 static bool client_has_state(struct nfs4_client
*clp
)
1568 * Note clp->cl_openowners check isn't quite right: there's no
1569 * need to count owners without stateid's.
1571 * Also note we should probably be using this in 4.0 case too.
1573 return !list_empty(&clp
->cl_openowners
)
1574 || !list_empty(&clp
->cl_delegations
)
1575 || !list_empty(&clp
->cl_sessions
);
1579 nfsd4_exchange_id(struct svc_rqst
*rqstp
,
1580 struct nfsd4_compound_state
*cstate
,
1581 struct nfsd4_exchange_id
*exid
)
1583 struct nfs4_client
*unconf
, *conf
, *new;
1585 char addr_str
[INET6_ADDRSTRLEN
];
1586 nfs4_verifier verf
= exid
->verifier
;
1587 struct sockaddr
*sa
= svc_addr(rqstp
);
1588 bool update
= exid
->flags
& EXCHGID4_FLAG_UPD_CONFIRMED_REC_A
;
1589 struct nfsd_net
*nn
= net_generic(SVC_NET(rqstp
), nfsd_net_id
);
1591 rpc_ntop(sa
, addr_str
, sizeof(addr_str
));
1592 dprintk("%s rqstp=%p exid=%p clname.len=%u clname.data=%p "
1593 "ip_addr=%s flags %x, spa_how %d\n",
1594 __func__
, rqstp
, exid
, exid
->clname
.len
, exid
->clname
.data
,
1595 addr_str
, exid
->flags
, exid
->spa_how
);
1597 if (exid
->flags
& ~EXCHGID4_FLAG_MASK_A
)
1598 return nfserr_inval
;
1600 /* Currently only support SP4_NONE */
1601 switch (exid
->spa_how
) {
1604 default: /* checked by xdr code */
1608 return nfserr_serverfault
; /* no excuse :-/ */
1611 /* Cases below refer to rfc 5661 section 18.35.4: */
1613 conf
= find_confirmed_client_by_name(&exid
->clname
, nn
);
1615 bool creds_match
= same_creds(&conf
->cl_cred
, &rqstp
->rq_cred
);
1616 bool verfs_match
= same_verf(&verf
, &conf
->cl_verifier
);
1619 if (!clp_used_exchangeid(conf
)) { /* buggy client */
1620 status
= nfserr_inval
;
1623 if (!creds_match
) { /* case 9 */
1624 status
= nfserr_perm
;
1627 if (!verfs_match
) { /* case 8 */
1628 status
= nfserr_not_same
;
1632 exid
->flags
|= EXCHGID4_FLAG_CONFIRMED_R
;
1636 if (!creds_match
) { /* case 3 */
1637 if (client_has_state(conf
)) {
1638 status
= nfserr_clid_inuse
;
1641 expire_client(conf
);
1644 if (verfs_match
) { /* case 2 */
1645 conf
->cl_exchange_flags
|= EXCHGID4_FLAG_CONFIRMED_R
;
1649 /* case 5, client reboot */
1653 if (update
) { /* case 7 */
1654 status
= nfserr_noent
;
1658 unconf
= find_unconfirmed_client_by_name(&exid
->clname
, nn
);
1659 if (unconf
) /* case 4, possible retry or client restart */
1660 expire_client(unconf
);
1662 /* case 1 (normal case) */
1664 new = create_client(exid
->clname
, rqstp
, &verf
);
1666 status
= nfserr_jukebox
;
1669 new->cl_minorversion
= 1;
1672 add_to_unconfirmed(new);
1674 exid
->clientid
.cl_boot
= new->cl_clientid
.cl_boot
;
1675 exid
->clientid
.cl_id
= new->cl_clientid
.cl_id
;
1677 exid
->seqid
= new->cl_cs_slot
.sl_seqid
+ 1;
1678 nfsd4_set_ex_flags(new, exid
);
1680 dprintk("nfsd4_exchange_id seqid %d flags %x\n",
1681 new->cl_cs_slot
.sl_seqid
, new->cl_exchange_flags
);
1685 nfs4_unlock_state();
1690 check_slot_seqid(u32 seqid
, u32 slot_seqid
, int slot_inuse
)
1692 dprintk("%s enter. seqid %d slot_seqid %d\n", __func__
, seqid
,
1695 /* The slot is in use, and no response has been sent. */
1697 if (seqid
== slot_seqid
)
1698 return nfserr_jukebox
;
1700 return nfserr_seq_misordered
;
1702 /* Note unsigned 32-bit arithmetic handles wraparound: */
1703 if (likely(seqid
== slot_seqid
+ 1))
1705 if (seqid
== slot_seqid
)
1706 return nfserr_replay_cache
;
1707 return nfserr_seq_misordered
;
1711 * Cache the create session result into the create session single DRC
1712 * slot cache by saving the xdr structure. sl_seqid has been set.
1713 * Do this for solo or embedded create session operations.
1716 nfsd4_cache_create_session(struct nfsd4_create_session
*cr_ses
,
1717 struct nfsd4_clid_slot
*slot
, __be32 nfserr
)
1719 slot
->sl_status
= nfserr
;
1720 memcpy(&slot
->sl_cr_ses
, cr_ses
, sizeof(*cr_ses
));
1724 nfsd4_replay_create_session(struct nfsd4_create_session
*cr_ses
,
1725 struct nfsd4_clid_slot
*slot
)
1727 memcpy(cr_ses
, &slot
->sl_cr_ses
, sizeof(*cr_ses
));
1728 return slot
->sl_status
;
1731 #define NFSD_MIN_REQ_HDR_SEQ_SZ ((\
1732 2 * 2 + /* credential,verifier: AUTH_NULL, length 0 */ \
1733 1 + /* MIN tag is length with zero, only length */ \
1734 3 + /* version, opcount, opcode */ \
1735 XDR_QUADLEN(NFS4_MAX_SESSIONID_LEN) + \
1736 /* seqid, slotID, slotID, cache */ \
1737 4 ) * sizeof(__be32))
1739 #define NFSD_MIN_RESP_HDR_SEQ_SZ ((\
1740 2 + /* verifier: AUTH_NULL, length 0 */\
1742 1 + /* MIN tag is length with zero, only length */ \
1743 3 + /* opcount, opcode, opstatus*/ \
1744 XDR_QUADLEN(NFS4_MAX_SESSIONID_LEN) + \
1745 /* seqid, slotID, slotID, slotID, status */ \
1746 5 ) * sizeof(__be32))
1748 static bool check_forechannel_attrs(struct nfsd4_channel_attrs fchannel
)
1750 return fchannel
.maxreq_sz
< NFSD_MIN_REQ_HDR_SEQ_SZ
1751 || fchannel
.maxresp_sz
< NFSD_MIN_RESP_HDR_SEQ_SZ
;
1755 nfsd4_create_session(struct svc_rqst
*rqstp
,
1756 struct nfsd4_compound_state
*cstate
,
1757 struct nfsd4_create_session
*cr_ses
)
1759 struct sockaddr
*sa
= svc_addr(rqstp
);
1760 struct nfs4_client
*conf
, *unconf
;
1761 struct nfsd4_session
*new;
1762 struct nfsd4_conn
*conn
;
1763 struct nfsd4_clid_slot
*cs_slot
= NULL
;
1765 struct nfsd_net
*nn
= net_generic(SVC_NET(rqstp
), nfsd_net_id
);
1767 if (cr_ses
->flags
& ~SESSION4_FLAG_MASK_A
)
1768 return nfserr_inval
;
1769 if (check_forechannel_attrs(cr_ses
->fore_channel
))
1770 return nfserr_toosmall
;
1771 new = alloc_session(&cr_ses
->fore_channel
, nn
);
1773 return nfserr_jukebox
;
1774 status
= nfserr_jukebox
;
1775 conn
= alloc_conn_from_crses(rqstp
, cr_ses
);
1777 goto out_free_session
;
1780 unconf
= find_unconfirmed_client(&cr_ses
->clientid
, true, nn
);
1781 conf
= find_confirmed_client(&cr_ses
->clientid
, true, nn
);
1784 cs_slot
= &conf
->cl_cs_slot
;
1785 status
= check_slot_seqid(cr_ses
->seqid
, cs_slot
->sl_seqid
, 0);
1786 if (status
== nfserr_replay_cache
) {
1787 status
= nfsd4_replay_create_session(cr_ses
, cs_slot
);
1789 } else if (cr_ses
->seqid
!= cs_slot
->sl_seqid
+ 1) {
1790 status
= nfserr_seq_misordered
;
1793 } else if (unconf
) {
1794 struct nfs4_client
*old
;
1795 if (!same_creds(&unconf
->cl_cred
, &rqstp
->rq_cred
) ||
1796 !rpc_cmp_addr(sa
, (struct sockaddr
*) &unconf
->cl_addr
)) {
1797 status
= nfserr_clid_inuse
;
1800 cs_slot
= &unconf
->cl_cs_slot
;
1801 status
= check_slot_seqid(cr_ses
->seqid
, cs_slot
->sl_seqid
, 0);
1803 /* an unconfirmed replay returns misordered */
1804 status
= nfserr_seq_misordered
;
1807 old
= find_confirmed_client_by_name(&unconf
->cl_name
, nn
);
1810 move_to_confirmed(unconf
);
1813 status
= nfserr_stale_clientid
;
1818 * We do not support RDMA or persistent sessions
1820 cr_ses
->flags
&= ~SESSION4_PERSIST
;
1821 cr_ses
->flags
&= ~SESSION4_RDMA
;
1823 init_session(rqstp
, new, conf
, cr_ses
);
1824 nfsd4_init_conn(rqstp
, conn
, new);
1826 memcpy(cr_ses
->sessionid
.data
, new->se_sessionid
.data
,
1827 NFS4_MAX_SESSIONID_LEN
);
1828 memcpy(&cr_ses
->fore_channel
, &new->se_fchannel
,
1829 sizeof(struct nfsd4_channel_attrs
));
1830 cs_slot
->sl_seqid
++;
1831 cr_ses
->seqid
= cs_slot
->sl_seqid
;
1833 /* cache solo and embedded create sessions under the state lock */
1834 nfsd4_cache_create_session(cr_ses
, cs_slot
, status
);
1835 nfs4_unlock_state();
1837 dprintk("%s returns %d\n", __func__
, ntohl(status
));
1840 nfs4_unlock_state();
1843 __free_session(new);
1847 static __be32
nfsd4_map_bcts_dir(u32
*dir
)
1850 case NFS4_CDFC4_FORE
:
1851 case NFS4_CDFC4_BACK
:
1853 case NFS4_CDFC4_FORE_OR_BOTH
:
1854 case NFS4_CDFC4_BACK_OR_BOTH
:
1855 *dir
= NFS4_CDFC4_BOTH
;
1858 return nfserr_inval
;
1861 __be32
nfsd4_backchannel_ctl(struct svc_rqst
*rqstp
, struct nfsd4_compound_state
*cstate
, struct nfsd4_backchannel_ctl
*bc
)
1863 struct nfsd4_session
*session
= cstate
->session
;
1864 struct nfsd_net
*nn
= net_generic(SVC_NET(rqstp
), nfsd_net_id
);
1866 spin_lock(&nn
->client_lock
);
1867 session
->se_cb_prog
= bc
->bc_cb_program
;
1868 session
->se_cb_sec
= bc
->bc_cb_sec
;
1869 spin_unlock(&nn
->client_lock
);
1871 nfsd4_probe_callback(session
->se_client
);
1876 __be32
nfsd4_bind_conn_to_session(struct svc_rqst
*rqstp
,
1877 struct nfsd4_compound_state
*cstate
,
1878 struct nfsd4_bind_conn_to_session
*bcts
)
1881 struct nfsd4_conn
*conn
;
1882 struct nfsd_net
*nn
= net_generic(SVC_NET(rqstp
), nfsd_net_id
);
1884 if (!nfsd4_last_compound_op(rqstp
))
1885 return nfserr_not_only_op
;
1886 spin_lock(&nn
->client_lock
);
1887 cstate
->session
= find_in_sessionid_hashtbl(&bcts
->sessionid
, SVC_NET(rqstp
));
1888 /* Sorta weird: we only need the refcnt'ing because new_conn acquires
1889 * client_lock iself: */
1890 if (cstate
->session
) {
1891 nfsd4_get_session(cstate
->session
);
1892 atomic_inc(&cstate
->session
->se_client
->cl_refcount
);
1894 spin_unlock(&nn
->client_lock
);
1895 if (!cstate
->session
)
1896 return nfserr_badsession
;
1898 status
= nfsd4_map_bcts_dir(&bcts
->dir
);
1901 conn
= alloc_conn(rqstp
, bcts
->dir
);
1903 return nfserr_jukebox
;
1904 nfsd4_init_conn(rqstp
, conn
, cstate
->session
);
1908 static bool nfsd4_compound_in_session(struct nfsd4_session
*session
, struct nfs4_sessionid
*sid
)
1912 return !memcmp(sid
, &session
->se_sessionid
, sizeof(*sid
));
1916 nfsd4_destroy_session(struct svc_rqst
*r
,
1917 struct nfsd4_compound_state
*cstate
,
1918 struct nfsd4_destroy_session
*sessionid
)
1920 struct nfsd4_session
*ses
;
1921 __be32 status
= nfserr_badsession
;
1922 struct nfsd_net
*nn
= net_generic(SVC_NET(r
), nfsd_net_id
);
1925 * - The confirmed nfs4_client->cl_sessionid holds destroyed sessinid
1926 * - Should we return nfserr_back_chan_busy if waiting for
1927 * callbacks on to-be-destroyed session?
1928 * - Do we need to clear any callback info from previous session?
1931 if (nfsd4_compound_in_session(cstate
->session
, &sessionid
->sessionid
)) {
1932 if (!nfsd4_last_compound_op(r
))
1933 return nfserr_not_only_op
;
1935 dump_sessionid(__func__
, &sessionid
->sessionid
);
1936 spin_lock(&nn
->client_lock
);
1937 ses
= find_in_sessionid_hashtbl(&sessionid
->sessionid
, SVC_NET(r
));
1939 spin_unlock(&nn
->client_lock
);
1943 unhash_session(ses
);
1944 spin_unlock(&nn
->client_lock
);
1947 nfsd4_probe_callback_sync(ses
->se_client
);
1948 nfs4_unlock_state();
1950 spin_lock(&nn
->client_lock
);
1951 nfsd4_del_conns(ses
);
1952 nfsd4_put_session_locked(ses
);
1953 spin_unlock(&nn
->client_lock
);
1956 dprintk("%s returns %d\n", __func__
, ntohl(status
));
1960 static struct nfsd4_conn
*__nfsd4_find_conn(struct svc_xprt
*xpt
, struct nfsd4_session
*s
)
1962 struct nfsd4_conn
*c
;
1964 list_for_each_entry(c
, &s
->se_conns
, cn_persession
) {
1965 if (c
->cn_xprt
== xpt
) {
1972 static void nfsd4_sequence_check_conn(struct nfsd4_conn
*new, struct nfsd4_session
*ses
)
1974 struct nfs4_client
*clp
= ses
->se_client
;
1975 struct nfsd4_conn
*c
;
1978 spin_lock(&clp
->cl_lock
);
1979 c
= __nfsd4_find_conn(new->cn_xprt
, ses
);
1981 spin_unlock(&clp
->cl_lock
);
1985 __nfsd4_hash_conn(new, ses
);
1986 spin_unlock(&clp
->cl_lock
);
1987 ret
= nfsd4_register_conn(new);
1989 /* oops; xprt is already down: */
1990 nfsd4_conn_lost(&new->cn_xpt_user
);
1994 static bool nfsd4_session_too_many_ops(struct svc_rqst
*rqstp
, struct nfsd4_session
*session
)
1996 struct nfsd4_compoundargs
*args
= rqstp
->rq_argp
;
1998 return args
->opcnt
> session
->se_fchannel
.maxops
;
2001 static bool nfsd4_request_too_big(struct svc_rqst
*rqstp
,
2002 struct nfsd4_session
*session
)
2004 struct xdr_buf
*xb
= &rqstp
->rq_arg
;
2006 return xb
->len
> session
->se_fchannel
.maxreq_sz
;
2010 nfsd4_sequence(struct svc_rqst
*rqstp
,
2011 struct nfsd4_compound_state
*cstate
,
2012 struct nfsd4_sequence
*seq
)
2014 struct nfsd4_compoundres
*resp
= rqstp
->rq_resp
;
2015 struct nfsd4_session
*session
;
2016 struct nfsd4_slot
*slot
;
2017 struct nfsd4_conn
*conn
;
2019 struct nfsd_net
*nn
= net_generic(SVC_NET(rqstp
), nfsd_net_id
);
2021 if (resp
->opcnt
!= 1)
2022 return nfserr_sequence_pos
;
2025 * Will be either used or freed by nfsd4_sequence_check_conn
2028 conn
= alloc_conn(rqstp
, NFS4_CDFC4_FORE
);
2030 return nfserr_jukebox
;
2032 spin_lock(&nn
->client_lock
);
2033 status
= nfserr_badsession
;
2034 session
= find_in_sessionid_hashtbl(&seq
->sessionid
, SVC_NET(rqstp
));
2038 status
= nfserr_too_many_ops
;
2039 if (nfsd4_session_too_many_ops(rqstp
, session
))
2042 status
= nfserr_req_too_big
;
2043 if (nfsd4_request_too_big(rqstp
, session
))
2046 status
= nfserr_badslot
;
2047 if (seq
->slotid
>= session
->se_fchannel
.maxreqs
)
2050 slot
= session
->se_slots
[seq
->slotid
];
2051 dprintk("%s: slotid %d\n", __func__
, seq
->slotid
);
2053 /* We do not negotiate the number of slots yet, so set the
2054 * maxslots to the session maxreqs which is used to encode
2055 * sr_highest_slotid and the sr_target_slot id to maxslots */
2056 seq
->maxslots
= session
->se_fchannel
.maxreqs
;
2058 status
= check_slot_seqid(seq
->seqid
, slot
->sl_seqid
,
2059 slot
->sl_flags
& NFSD4_SLOT_INUSE
);
2060 if (status
== nfserr_replay_cache
) {
2061 status
= nfserr_seq_misordered
;
2062 if (!(slot
->sl_flags
& NFSD4_SLOT_INITIALIZED
))
2064 cstate
->slot
= slot
;
2065 cstate
->session
= session
;
2066 /* Return the cached reply status and set cstate->status
2067 * for nfsd4_proc_compound processing */
2068 status
= nfsd4_replay_cache_entry(resp
, seq
);
2069 cstate
->status
= nfserr_replay_cache
;
2075 nfsd4_sequence_check_conn(conn
, session
);
2078 /* Success! bump slot seqid */
2079 slot
->sl_seqid
= seq
->seqid
;
2080 slot
->sl_flags
|= NFSD4_SLOT_INUSE
;
2082 slot
->sl_flags
|= NFSD4_SLOT_CACHETHIS
;
2084 slot
->sl_flags
&= ~NFSD4_SLOT_CACHETHIS
;
2086 cstate
->slot
= slot
;
2087 cstate
->session
= session
;
2090 /* Hold a session reference until done processing the compound. */
2091 if (cstate
->session
) {
2092 struct nfs4_client
*clp
= session
->se_client
;
2094 nfsd4_get_session(cstate
->session
);
2095 atomic_inc(&clp
->cl_refcount
);
2096 switch (clp
->cl_cb_state
) {
2098 seq
->status_flags
= SEQ4_STATUS_CB_PATH_DOWN
;
2100 case NFSD4_CB_FAULT
:
2101 seq
->status_flags
= SEQ4_STATUS_BACKCHANNEL_FAULT
;
2104 seq
->status_flags
= 0;
2108 spin_unlock(&nn
->client_lock
);
2109 dprintk("%s: return %d\n", __func__
, ntohl(status
));
2114 nfsd4_destroy_clientid(struct svc_rqst
*rqstp
, struct nfsd4_compound_state
*cstate
, struct nfsd4_destroy_clientid
*dc
)
2116 struct nfs4_client
*conf
, *unconf
, *clp
;
2118 struct nfsd_net
*nn
= net_generic(SVC_NET(rqstp
), nfsd_net_id
);
2121 unconf
= find_unconfirmed_client(&dc
->clientid
, true, nn
);
2122 conf
= find_confirmed_client(&dc
->clientid
, true, nn
);
2127 if (!is_client_expired(conf
) && client_has_state(conf
)) {
2128 status
= nfserr_clientid_busy
;
2132 /* rfc5661 18.50.3 */
2133 if (cstate
->session
&& conf
== cstate
->session
->se_client
) {
2134 status
= nfserr_clientid_busy
;
2140 status
= nfserr_stale_clientid
;
2146 nfs4_unlock_state();
2147 dprintk("%s return %d\n", __func__
, ntohl(status
));
2152 nfsd4_reclaim_complete(struct svc_rqst
*rqstp
, struct nfsd4_compound_state
*cstate
, struct nfsd4_reclaim_complete
*rc
)
2156 if (rc
->rca_one_fs
) {
2157 if (!cstate
->current_fh
.fh_dentry
)
2158 return nfserr_nofilehandle
;
2160 * We don't take advantage of the rca_one_fs case.
2161 * That's OK, it's optional, we can safely ignore it.
2167 status
= nfserr_complete_already
;
2168 if (test_and_set_bit(NFSD4_CLIENT_RECLAIM_COMPLETE
,
2169 &cstate
->session
->se_client
->cl_flags
))
2172 status
= nfserr_stale_clientid
;
2173 if (is_client_expired(cstate
->session
->se_client
))
2175 * The following error isn't really legal.
2176 * But we only get here if the client just explicitly
2177 * destroyed the client. Surely it no longer cares what
2178 * error it gets back on an operation for the dead
2184 nfsd4_client_record_create(cstate
->session
->se_client
);
2186 nfs4_unlock_state();
2191 nfsd4_setclientid(struct svc_rqst
*rqstp
, struct nfsd4_compound_state
*cstate
,
2192 struct nfsd4_setclientid
*setclid
)
2194 struct xdr_netobj clname
= setclid
->se_name
;
2195 nfs4_verifier clverifier
= setclid
->se_verf
;
2196 struct nfs4_client
*conf
, *unconf
, *new;
2198 struct nfsd_net
*nn
= net_generic(SVC_NET(rqstp
), nfsd_net_id
);
2200 /* Cases below refer to rfc 3530 section 14.2.33: */
2202 conf
= find_confirmed_client_by_name(&clname
, nn
);
2205 status
= nfserr_clid_inuse
;
2206 if (clp_used_exchangeid(conf
))
2208 if (!same_creds(&conf
->cl_cred
, &rqstp
->rq_cred
)) {
2209 char addr_str
[INET6_ADDRSTRLEN
];
2210 rpc_ntop((struct sockaddr
*) &conf
->cl_addr
, addr_str
,
2212 dprintk("NFSD: setclientid: string in use by client "
2213 "at %s\n", addr_str
);
2217 unconf
= find_unconfirmed_client_by_name(&clname
, nn
);
2219 expire_client(unconf
);
2220 status
= nfserr_jukebox
;
2221 new = create_client(clname
, rqstp
, &clverifier
);
2224 if (conf
&& same_verf(&conf
->cl_verifier
, &clverifier
))
2225 /* case 1: probable callback update */
2226 copy_clid(new, conf
);
2227 else /* case 4 (new client) or cases 2, 3 (client reboot): */
2229 new->cl_minorversion
= 0;
2230 gen_callback(new, setclid
, rqstp
);
2231 add_to_unconfirmed(new);
2232 setclid
->se_clientid
.cl_boot
= new->cl_clientid
.cl_boot
;
2233 setclid
->se_clientid
.cl_id
= new->cl_clientid
.cl_id
;
2234 memcpy(setclid
->se_confirm
.data
, new->cl_confirm
.data
, sizeof(setclid
->se_confirm
.data
));
2237 nfs4_unlock_state();
2243 nfsd4_setclientid_confirm(struct svc_rqst
*rqstp
,
2244 struct nfsd4_compound_state
*cstate
,
2245 struct nfsd4_setclientid_confirm
*setclientid_confirm
)
2247 struct nfs4_client
*conf
, *unconf
;
2248 nfs4_verifier confirm
= setclientid_confirm
->sc_confirm
;
2249 clientid_t
* clid
= &setclientid_confirm
->sc_clientid
;
2251 struct nfsd_net
*nn
= net_generic(SVC_NET(rqstp
), nfsd_net_id
);
2253 if (STALE_CLIENTID(clid
, nn
))
2254 return nfserr_stale_clientid
;
2257 conf
= find_confirmed_client(clid
, false, nn
);
2258 unconf
= find_unconfirmed_client(clid
, false, nn
);
2260 * We try hard to give out unique clientid's, so if we get an
2261 * attempt to confirm the same clientid with a different cred,
2262 * there's a bug somewhere. Let's charitably assume it's our
2265 status
= nfserr_serverfault
;
2266 if (unconf
&& !same_creds(&unconf
->cl_cred
, &rqstp
->rq_cred
))
2268 if (conf
&& !same_creds(&conf
->cl_cred
, &rqstp
->rq_cred
))
2270 /* cases below refer to rfc 3530 section 14.2.34: */
2271 if (!unconf
|| !same_verf(&confirm
, &unconf
->cl_confirm
)) {
2272 if (conf
&& !unconf
) /* case 2: probable retransmit */
2274 else /* case 4: client hasn't noticed we rebooted yet? */
2275 status
= nfserr_stale_clientid
;
2279 if (conf
) { /* case 1: callback update */
2280 nfsd4_change_callback(conf
, &unconf
->cl_cb_conn
);
2281 nfsd4_probe_callback(conf
);
2282 expire_client(unconf
);
2283 } else { /* case 3: normal case; new or rebooted client */
2284 conf
= find_confirmed_client_by_name(&unconf
->cl_name
, nn
);
2286 expire_client(conf
);
2287 move_to_confirmed(unconf
);
2288 nfsd4_probe_callback(unconf
);
2291 nfs4_unlock_state();
2295 static struct nfs4_file
*nfsd4_alloc_file(void)
2297 return kmem_cache_alloc(file_slab
, GFP_KERNEL
);
2300 /* OPEN Share state helper functions */
2301 static void nfsd4_init_file(struct nfs4_file
*fp
, struct inode
*ino
)
2303 unsigned int hashval
= file_hashval(ino
);
2305 atomic_set(&fp
->fi_ref
, 1);
2306 INIT_LIST_HEAD(&fp
->fi_hash
);
2307 INIT_LIST_HEAD(&fp
->fi_stateids
);
2308 INIT_LIST_HEAD(&fp
->fi_delegations
);
2309 fp
->fi_inode
= igrab(ino
);
2310 fp
->fi_had_conflict
= false;
2311 fp
->fi_lease
= NULL
;
2312 memset(fp
->fi_fds
, 0, sizeof(fp
->fi_fds
));
2313 memset(fp
->fi_access
, 0, sizeof(fp
->fi_access
));
2314 spin_lock(&recall_lock
);
2315 list_add(&fp
->fi_hash
, &file_hashtbl
[hashval
]);
2316 spin_unlock(&recall_lock
);
2320 nfsd4_free_slab(struct kmem_cache
**slab
)
2324 kmem_cache_destroy(*slab
);
2329 nfsd4_free_slabs(void)
2331 nfsd4_free_slab(&openowner_slab
);
2332 nfsd4_free_slab(&lockowner_slab
);
2333 nfsd4_free_slab(&file_slab
);
2334 nfsd4_free_slab(&stateid_slab
);
2335 nfsd4_free_slab(&deleg_slab
);
2339 nfsd4_init_slabs(void)
2341 openowner_slab
= kmem_cache_create("nfsd4_openowners",
2342 sizeof(struct nfs4_openowner
), 0, 0, NULL
);
2343 if (openowner_slab
== NULL
)
2345 lockowner_slab
= kmem_cache_create("nfsd4_lockowners",
2346 sizeof(struct nfs4_lockowner
), 0, 0, NULL
);
2347 if (lockowner_slab
== NULL
)
2349 file_slab
= kmem_cache_create("nfsd4_files",
2350 sizeof(struct nfs4_file
), 0, 0, NULL
);
2351 if (file_slab
== NULL
)
2353 stateid_slab
= kmem_cache_create("nfsd4_stateids",
2354 sizeof(struct nfs4_ol_stateid
), 0, 0, NULL
);
2355 if (stateid_slab
== NULL
)
2357 deleg_slab
= kmem_cache_create("nfsd4_delegations",
2358 sizeof(struct nfs4_delegation
), 0, 0, NULL
);
2359 if (deleg_slab
== NULL
)
2364 dprintk("nfsd4: out of memory while initializing nfsv4\n");
2368 void nfs4_free_openowner(struct nfs4_openowner
*oo
)
2370 kfree(oo
->oo_owner
.so_owner
.data
);
2371 kmem_cache_free(openowner_slab
, oo
);
2374 void nfs4_free_lockowner(struct nfs4_lockowner
*lo
)
2376 kfree(lo
->lo_owner
.so_owner
.data
);
2377 kmem_cache_free(lockowner_slab
, lo
);
2380 static void init_nfs4_replay(struct nfs4_replay
*rp
)
2382 rp
->rp_status
= nfserr_serverfault
;
2384 rp
->rp_buf
= rp
->rp_ibuf
;
2387 static inline void *alloc_stateowner(struct kmem_cache
*slab
, struct xdr_netobj
*owner
, struct nfs4_client
*clp
)
2389 struct nfs4_stateowner
*sop
;
2391 sop
= kmem_cache_alloc(slab
, GFP_KERNEL
);
2395 sop
->so_owner
.data
= kmemdup(owner
->data
, owner
->len
, GFP_KERNEL
);
2396 if (!sop
->so_owner
.data
) {
2397 kmem_cache_free(slab
, sop
);
2400 sop
->so_owner
.len
= owner
->len
;
2402 INIT_LIST_HEAD(&sop
->so_stateids
);
2403 sop
->so_client
= clp
;
2404 init_nfs4_replay(&sop
->so_replay
);
2408 static void hash_openowner(struct nfs4_openowner
*oo
, struct nfs4_client
*clp
, unsigned int strhashval
)
2410 struct nfsd_net
*nn
= net_generic(clp
->net
, nfsd_net_id
);
2412 list_add(&oo
->oo_owner
.so_strhash
, &nn
->ownerstr_hashtbl
[strhashval
]);
2413 list_add(&oo
->oo_perclient
, &clp
->cl_openowners
);
2416 static struct nfs4_openowner
*
2417 alloc_init_open_stateowner(unsigned int strhashval
, struct nfs4_client
*clp
, struct nfsd4_open
*open
) {
2418 struct nfs4_openowner
*oo
;
2420 oo
= alloc_stateowner(openowner_slab
, &open
->op_owner
, clp
);
2423 oo
->oo_owner
.so_is_open_owner
= 1;
2424 oo
->oo_owner
.so_seqid
= open
->op_seqid
;
2425 oo
->oo_flags
= NFS4_OO_NEW
;
2427 oo
->oo_last_closed_stid
= NULL
;
2428 INIT_LIST_HEAD(&oo
->oo_close_lru
);
2429 hash_openowner(oo
, clp
, strhashval
);
2433 static void init_open_stateid(struct nfs4_ol_stateid
*stp
, struct nfs4_file
*fp
, struct nfsd4_open
*open
) {
2434 struct nfs4_openowner
*oo
= open
->op_openowner
;
2436 stp
->st_stid
.sc_type
= NFS4_OPEN_STID
;
2437 INIT_LIST_HEAD(&stp
->st_lockowners
);
2438 list_add(&stp
->st_perstateowner
, &oo
->oo_owner
.so_stateids
);
2439 list_add(&stp
->st_perfile
, &fp
->fi_stateids
);
2440 stp
->st_stateowner
= &oo
->oo_owner
;
2443 stp
->st_access_bmap
= 0;
2444 stp
->st_deny_bmap
= 0;
2445 set_access(open
->op_share_access
, stp
);
2446 set_deny(open
->op_share_deny
, stp
);
2447 stp
->st_openstp
= NULL
;
2451 move_to_close_lru(struct nfs4_openowner
*oo
, struct net
*net
)
2453 struct nfsd_net
*nn
= net_generic(net
, nfsd_net_id
);
2455 dprintk("NFSD: move_to_close_lru nfs4_openowner %p\n", oo
);
2457 list_move_tail(&oo
->oo_close_lru
, &nn
->close_lru
);
2458 oo
->oo_time
= get_seconds();
2462 same_owner_str(struct nfs4_stateowner
*sop
, struct xdr_netobj
*owner
,
2465 return (sop
->so_owner
.len
== owner
->len
) &&
2466 0 == memcmp(sop
->so_owner
.data
, owner
->data
, owner
->len
) &&
2467 (sop
->so_client
->cl_clientid
.cl_id
== clid
->cl_id
);
2470 static struct nfs4_openowner
*
2471 find_openstateowner_str(unsigned int hashval
, struct nfsd4_open
*open
,
2472 bool sessions
, struct nfsd_net
*nn
)
2474 struct nfs4_stateowner
*so
;
2475 struct nfs4_openowner
*oo
;
2476 struct nfs4_client
*clp
;
2478 list_for_each_entry(so
, &nn
->ownerstr_hashtbl
[hashval
], so_strhash
) {
2479 if (!so
->so_is_open_owner
)
2481 if (same_owner_str(so
, &open
->op_owner
, &open
->op_clientid
)) {
2483 clp
= oo
->oo_owner
.so_client
;
2484 if ((bool)clp
->cl_minorversion
!= sessions
)
2486 renew_client(oo
->oo_owner
.so_client
);
2493 /* search file_hashtbl[] for file */
2494 static struct nfs4_file
*
2495 find_file(struct inode
*ino
)
2497 unsigned int hashval
= file_hashval(ino
);
2498 struct nfs4_file
*fp
;
2500 spin_lock(&recall_lock
);
2501 list_for_each_entry(fp
, &file_hashtbl
[hashval
], fi_hash
) {
2502 if (fp
->fi_inode
== ino
) {
2504 spin_unlock(&recall_lock
);
2508 spin_unlock(&recall_lock
);
2513 * Called to check deny when READ with all zero stateid or
2514 * WRITE with all zero or all one stateid
2517 nfs4_share_conflict(struct svc_fh
*current_fh
, unsigned int deny_type
)
2519 struct inode
*ino
= current_fh
->fh_dentry
->d_inode
;
2520 struct nfs4_file
*fp
;
2521 struct nfs4_ol_stateid
*stp
;
2524 dprintk("NFSD: nfs4_share_conflict\n");
2526 fp
= find_file(ino
);
2529 ret
= nfserr_locked
;
2530 /* Search for conflicting share reservations */
2531 list_for_each_entry(stp
, &fp
->fi_stateids
, st_perfile
) {
2532 if (test_deny(deny_type
, stp
) ||
2533 test_deny(NFS4_SHARE_DENY_BOTH
, stp
))
2542 static void nfsd_break_one_deleg(struct nfs4_delegation
*dp
)
2544 /* We're assuming the state code never drops its reference
2545 * without first removing the lease. Since we're in this lease
2546 * callback (and since the lease code is serialized by the kernel
2547 * lock) we know the server hasn't removed the lease yet, we know
2548 * it's safe to take a reference: */
2549 atomic_inc(&dp
->dl_count
);
2551 list_add_tail(&dp
->dl_recall_lru
, &del_recall_lru
);
2553 /* only place dl_time is set. protected by lock_flocks*/
2554 dp
->dl_time
= get_seconds();
2556 nfsd4_cb_recall(dp
);
2559 /* Called from break_lease() with lock_flocks() held. */
2560 static void nfsd_break_deleg_cb(struct file_lock
*fl
)
2562 struct nfs4_file
*fp
= (struct nfs4_file
*)fl
->fl_owner
;
2563 struct nfs4_delegation
*dp
;
2566 WARN(1, "(%p)->fl_owner NULL\n", fl
);
2569 if (fp
->fi_had_conflict
) {
2570 WARN(1, "duplicate break on %p\n", fp
);
2574 * We don't want the locks code to timeout the lease for us;
2575 * we'll remove it ourself if a delegation isn't returned
2578 fl
->fl_break_time
= 0;
2580 spin_lock(&recall_lock
);
2581 fp
->fi_had_conflict
= true;
2582 list_for_each_entry(dp
, &fp
->fi_delegations
, dl_perfile
)
2583 nfsd_break_one_deleg(dp
);
2584 spin_unlock(&recall_lock
);
2588 int nfsd_change_deleg_cb(struct file_lock
**onlist
, int arg
)
2591 return lease_modify(onlist
, arg
);
2596 static const struct lock_manager_operations nfsd_lease_mng_ops
= {
2597 .lm_break
= nfsd_break_deleg_cb
,
2598 .lm_change
= nfsd_change_deleg_cb
,
2601 static __be32
nfsd4_check_seqid(struct nfsd4_compound_state
*cstate
, struct nfs4_stateowner
*so
, u32 seqid
)
2603 if (nfsd4_has_session(cstate
))
2605 if (seqid
== so
->so_seqid
- 1)
2606 return nfserr_replay_me
;
2607 if (seqid
== so
->so_seqid
)
2609 return nfserr_bad_seqid
;
2613 nfsd4_process_open1(struct nfsd4_compound_state
*cstate
,
2614 struct nfsd4_open
*open
, struct nfsd_net
*nn
)
2616 clientid_t
*clientid
= &open
->op_clientid
;
2617 struct nfs4_client
*clp
= NULL
;
2618 unsigned int strhashval
;
2619 struct nfs4_openowner
*oo
= NULL
;
2622 if (STALE_CLIENTID(&open
->op_clientid
, nn
))
2623 return nfserr_stale_clientid
;
2625 * In case we need it later, after we've already created the
2626 * file and don't want to risk a further failure:
2628 open
->op_file
= nfsd4_alloc_file();
2629 if (open
->op_file
== NULL
)
2630 return nfserr_jukebox
;
2632 strhashval
= ownerstr_hashval(clientid
->cl_id
, &open
->op_owner
);
2633 oo
= find_openstateowner_str(strhashval
, open
, cstate
->minorversion
, nn
);
2634 open
->op_openowner
= oo
;
2636 clp
= find_confirmed_client(clientid
, cstate
->minorversion
,
2639 return nfserr_expired
;
2642 if (!(oo
->oo_flags
& NFS4_OO_CONFIRMED
)) {
2643 /* Replace unconfirmed owners without checking for replay. */
2644 clp
= oo
->oo_owner
.so_client
;
2645 release_openowner(oo
);
2646 open
->op_openowner
= NULL
;
2649 status
= nfsd4_check_seqid(cstate
, &oo
->oo_owner
, open
->op_seqid
);
2652 clp
= oo
->oo_owner
.so_client
;
2655 oo
= alloc_init_open_stateowner(strhashval
, clp
, open
);
2657 return nfserr_jukebox
;
2658 open
->op_openowner
= oo
;
2660 open
->op_stp
= nfs4_alloc_stateid(clp
);
2662 return nfserr_jukebox
;
2666 static inline __be32
2667 nfs4_check_delegmode(struct nfs4_delegation
*dp
, int flags
)
2669 if ((flags
& WR_STATE
) && (dp
->dl_type
== NFS4_OPEN_DELEGATE_READ
))
2670 return nfserr_openmode
;
2675 static int share_access_to_flags(u32 share_access
)
2677 return share_access
== NFS4_SHARE_ACCESS_READ
? RD_STATE
: WR_STATE
;
2680 static struct nfs4_delegation
*find_deleg_stateid(struct nfs4_client
*cl
, stateid_t
*s
)
2682 struct nfs4_stid
*ret
;
2684 ret
= find_stateid_by_type(cl
, s
, NFS4_DELEG_STID
);
2687 return delegstateid(ret
);
2690 static bool nfsd4_is_deleg_cur(struct nfsd4_open
*open
)
2692 return open
->op_claim_type
== NFS4_OPEN_CLAIM_DELEGATE_CUR
||
2693 open
->op_claim_type
== NFS4_OPEN_CLAIM_DELEG_CUR_FH
;
2697 nfs4_check_deleg(struct nfs4_client
*cl
, struct nfs4_file
*fp
, struct nfsd4_open
*open
,
2698 struct nfs4_delegation
**dp
)
2701 __be32 status
= nfserr_bad_stateid
;
2703 *dp
= find_deleg_stateid(cl
, &open
->op_delegate_stateid
);
2706 flags
= share_access_to_flags(open
->op_share_access
);
2707 status
= nfs4_check_delegmode(*dp
, flags
);
2711 if (!nfsd4_is_deleg_cur(open
))
2715 open
->op_openowner
->oo_flags
|= NFS4_OO_CONFIRMED
;
2720 nfs4_check_open(struct nfs4_file
*fp
, struct nfsd4_open
*open
, struct nfs4_ol_stateid
**stpp
)
2722 struct nfs4_ol_stateid
*local
;
2723 struct nfs4_openowner
*oo
= open
->op_openowner
;
2725 list_for_each_entry(local
, &fp
->fi_stateids
, st_perfile
) {
2726 /* ignore lock owners */
2727 if (local
->st_stateowner
->so_is_open_owner
== 0)
2729 /* remember if we have seen this open owner */
2730 if (local
->st_stateowner
== &oo
->oo_owner
)
2732 /* check for conflicting share reservations */
2733 if (!test_share(local
, open
))
2734 return nfserr_share_denied
;
2739 static inline int nfs4_access_to_access(u32 nfs4_access
)
2743 if (nfs4_access
& NFS4_SHARE_ACCESS_READ
)
2744 flags
|= NFSD_MAY_READ
;
2745 if (nfs4_access
& NFS4_SHARE_ACCESS_WRITE
)
2746 flags
|= NFSD_MAY_WRITE
;
2750 static __be32
nfs4_get_vfs_file(struct svc_rqst
*rqstp
, struct nfs4_file
*fp
,
2751 struct svc_fh
*cur_fh
, struct nfsd4_open
*open
)
2754 int oflag
= nfs4_access_to_omode(open
->op_share_access
);
2755 int access
= nfs4_access_to_access(open
->op_share_access
);
2757 if (!fp
->fi_fds
[oflag
]) {
2758 status
= nfsd_open(rqstp
, cur_fh
, S_IFREG
, access
,
2759 &fp
->fi_fds
[oflag
]);
2763 nfs4_file_get_access(fp
, oflag
);
2768 static inline __be32
2769 nfsd4_truncate(struct svc_rqst
*rqstp
, struct svc_fh
*fh
,
2770 struct nfsd4_open
*open
)
2772 struct iattr iattr
= {
2773 .ia_valid
= ATTR_SIZE
,
2776 if (!open
->op_truncate
)
2778 if (!(open
->op_share_access
& NFS4_SHARE_ACCESS_WRITE
))
2779 return nfserr_inval
;
2780 return nfsd_setattr(rqstp
, fh
, &iattr
, 0, (time_t)0);
2784 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
)
2786 u32 op_share_access
= open
->op_share_access
;
2790 new_access
= !test_access(op_share_access
, stp
);
2792 status
= nfs4_get_vfs_file(rqstp
, fp
, cur_fh
, open
);
2796 status
= nfsd4_truncate(rqstp
, cur_fh
, open
);
2799 int oflag
= nfs4_access_to_omode(op_share_access
);
2800 nfs4_file_put_access(fp
, oflag
);
2804 /* remember the open */
2805 set_access(op_share_access
, stp
);
2806 set_deny(open
->op_share_deny
, stp
);
2813 nfs4_set_claim_prev(struct nfsd4_open
*open
, bool has_session
)
2815 open
->op_openowner
->oo_flags
|= NFS4_OO_CONFIRMED
;
2818 /* Should we give out recallable state?: */
2819 static bool nfsd4_cb_channel_good(struct nfs4_client
*clp
)
2821 if (clp
->cl_cb_state
== NFSD4_CB_UP
)
2824 * In the sessions case, since we don't have to establish a
2825 * separate connection for callbacks, we assume it's OK
2826 * until we hear otherwise:
2828 return clp
->cl_minorversion
&& clp
->cl_cb_state
== NFSD4_CB_UNKNOWN
;
2831 static struct file_lock
*nfs4_alloc_init_lease(struct nfs4_delegation
*dp
, int flag
)
2833 struct file_lock
*fl
;
2835 fl
= locks_alloc_lock();
2838 locks_init_lock(fl
);
2839 fl
->fl_lmops
= &nfsd_lease_mng_ops
;
2840 fl
->fl_flags
= FL_LEASE
;
2841 fl
->fl_type
= flag
== NFS4_OPEN_DELEGATE_READ
? F_RDLCK
: F_WRLCK
;
2842 fl
->fl_end
= OFFSET_MAX
;
2843 fl
->fl_owner
= (fl_owner_t
)(dp
->dl_file
);
2844 fl
->fl_pid
= current
->tgid
;
2848 static int nfs4_setlease(struct nfs4_delegation
*dp
, int flag
)
2850 struct nfs4_file
*fp
= dp
->dl_file
;
2851 struct file_lock
*fl
;
2854 fl
= nfs4_alloc_init_lease(dp
, flag
);
2857 fl
->fl_file
= find_readable_file(fp
);
2858 list_add(&dp
->dl_perclnt
, &dp
->dl_stid
.sc_client
->cl_delegations
);
2859 status
= vfs_setlease(fl
->fl_file
, fl
->fl_type
, &fl
);
2861 list_del_init(&dp
->dl_perclnt
);
2862 locks_free_lock(fl
);
2866 fp
->fi_deleg_file
= get_file(fl
->fl_file
);
2867 atomic_set(&fp
->fi_delegees
, 1);
2868 list_add(&dp
->dl_perfile
, &fp
->fi_delegations
);
2872 static int nfs4_set_delegation(struct nfs4_delegation
*dp
, int flag
)
2874 struct nfs4_file
*fp
= dp
->dl_file
;
2877 return nfs4_setlease(dp
, flag
);
2878 spin_lock(&recall_lock
);
2879 if (fp
->fi_had_conflict
) {
2880 spin_unlock(&recall_lock
);
2883 atomic_inc(&fp
->fi_delegees
);
2884 list_add(&dp
->dl_perfile
, &fp
->fi_delegations
);
2885 spin_unlock(&recall_lock
);
2886 list_add(&dp
->dl_perclnt
, &dp
->dl_stid
.sc_client
->cl_delegations
);
2890 static void nfsd4_open_deleg_none_ext(struct nfsd4_open
*open
, int status
)
2892 open
->op_delegate_type
= NFS4_OPEN_DELEGATE_NONE_EXT
;
2893 if (status
== -EAGAIN
)
2894 open
->op_why_no_deleg
= WND4_CONTENTION
;
2896 open
->op_why_no_deleg
= WND4_RESOURCE
;
2897 switch (open
->op_deleg_want
) {
2898 case NFS4_SHARE_WANT_READ_DELEG
:
2899 case NFS4_SHARE_WANT_WRITE_DELEG
:
2900 case NFS4_SHARE_WANT_ANY_DELEG
:
2902 case NFS4_SHARE_WANT_CANCEL
:
2903 open
->op_why_no_deleg
= WND4_CANCELLED
;
2905 case NFS4_SHARE_WANT_NO_DELEG
:
2912 * Attempt to hand out a delegation.
2915 nfs4_open_delegation(struct net
*net
, struct svc_fh
*fh
,
2916 struct nfsd4_open
*open
, struct nfs4_ol_stateid
*stp
)
2918 struct nfs4_delegation
*dp
;
2919 struct nfs4_openowner
*oo
= container_of(stp
->st_stateowner
, struct nfs4_openowner
, oo_owner
);
2921 int status
= 0, flag
= 0;
2923 cb_up
= nfsd4_cb_channel_good(oo
->oo_owner
.so_client
);
2924 flag
= NFS4_OPEN_DELEGATE_NONE
;
2925 open
->op_recall
= 0;
2926 switch (open
->op_claim_type
) {
2927 case NFS4_OPEN_CLAIM_PREVIOUS
:
2929 open
->op_recall
= 1;
2930 flag
= open
->op_delegate_type
;
2931 if (flag
== NFS4_OPEN_DELEGATE_NONE
)
2934 case NFS4_OPEN_CLAIM_NULL
:
2935 /* Let's not give out any delegations till everyone's
2936 * had the chance to reclaim theirs.... */
2937 if (locks_in_grace(net
))
2939 if (!cb_up
|| !(oo
->oo_flags
& NFS4_OO_CONFIRMED
))
2941 if (open
->op_share_access
& NFS4_SHARE_ACCESS_WRITE
)
2942 flag
= NFS4_OPEN_DELEGATE_WRITE
;
2944 flag
= NFS4_OPEN_DELEGATE_READ
;
2950 dp
= alloc_init_deleg(oo
->oo_owner
.so_client
, stp
, fh
, flag
);
2953 status
= nfs4_set_delegation(dp
, flag
);
2957 memcpy(&open
->op_delegate_stateid
, &dp
->dl_stid
.sc_stateid
, sizeof(dp
->dl_stid
.sc_stateid
));
2959 dprintk("NFSD: delegation stateid=" STATEID_FMT
"\n",
2960 STATEID_VAL(&dp
->dl_stid
.sc_stateid
));
2962 open
->op_delegate_type
= flag
;
2963 if (flag
== NFS4_OPEN_DELEGATE_NONE
) {
2964 if (open
->op_claim_type
== NFS4_OPEN_CLAIM_PREVIOUS
&&
2965 open
->op_delegate_type
!= NFS4_OPEN_DELEGATE_NONE
)
2966 dprintk("NFSD: WARNING: refusing delegation reclaim\n");
2968 /* 4.1 client asking for a delegation? */
2969 if (open
->op_deleg_want
)
2970 nfsd4_open_deleg_none_ext(open
, status
);
2974 unhash_stid(&dp
->dl_stid
);
2975 nfs4_put_delegation(dp
);
2977 flag
= NFS4_OPEN_DELEGATE_NONE
;
2981 static void nfsd4_deleg_xgrade_none_ext(struct nfsd4_open
*open
,
2982 struct nfs4_delegation
*dp
)
2984 if (open
->op_deleg_want
== NFS4_SHARE_WANT_READ_DELEG
&&
2985 dp
->dl_type
== NFS4_OPEN_DELEGATE_WRITE
) {
2986 open
->op_delegate_type
= NFS4_OPEN_DELEGATE_NONE_EXT
;
2987 open
->op_why_no_deleg
= WND4_NOT_SUPP_DOWNGRADE
;
2988 } else if (open
->op_deleg_want
== NFS4_SHARE_WANT_WRITE_DELEG
&&
2989 dp
->dl_type
== NFS4_OPEN_DELEGATE_WRITE
) {
2990 open
->op_delegate_type
= NFS4_OPEN_DELEGATE_NONE_EXT
;
2991 open
->op_why_no_deleg
= WND4_NOT_SUPP_UPGRADE
;
2993 /* Otherwise the client must be confused wanting a delegation
2994 * it already has, therefore we don't return
2995 * NFS4_OPEN_DELEGATE_NONE_EXT and reason.
3000 * called with nfs4_lock_state() held.
3003 nfsd4_process_open2(struct svc_rqst
*rqstp
, struct svc_fh
*current_fh
, struct nfsd4_open
*open
)
3005 struct nfsd4_compoundres
*resp
= rqstp
->rq_resp
;
3006 struct nfs4_client
*cl
= open
->op_openowner
->oo_owner
.so_client
;
3007 struct nfs4_file
*fp
= NULL
;
3008 struct inode
*ino
= current_fh
->fh_dentry
->d_inode
;
3009 struct nfs4_ol_stateid
*stp
= NULL
;
3010 struct nfs4_delegation
*dp
= NULL
;
3014 * Lookup file; if found, lookup stateid and check open request,
3015 * and check for delegations in the process of being recalled.
3016 * If not found, create the nfs4_file struct
3018 fp
= find_file(ino
);
3020 if ((status
= nfs4_check_open(fp
, open
, &stp
)))
3022 status
= nfs4_check_deleg(cl
, fp
, open
, &dp
);
3026 status
= nfserr_bad_stateid
;
3027 if (nfsd4_is_deleg_cur(open
))
3029 status
= nfserr_jukebox
;
3031 open
->op_file
= NULL
;
3032 nfsd4_init_file(fp
, ino
);
3036 * OPEN the file, or upgrade an existing OPEN.
3037 * If truncate fails, the OPEN fails.
3040 /* Stateid was found, this is an OPEN upgrade */
3041 status
= nfs4_upgrade_open(rqstp
, fp
, current_fh
, stp
, open
);
3045 status
= nfs4_get_vfs_file(rqstp
, fp
, current_fh
, open
);
3048 status
= nfsd4_truncate(rqstp
, current_fh
, open
);
3052 open
->op_stp
= NULL
;
3053 init_open_stateid(stp
, fp
, open
);
3055 update_stateid(&stp
->st_stid
.sc_stateid
);
3056 memcpy(&open
->op_stateid
, &stp
->st_stid
.sc_stateid
, sizeof(stateid_t
));
3058 if (nfsd4_has_session(&resp
->cstate
)) {
3059 open
->op_openowner
->oo_flags
|= NFS4_OO_CONFIRMED
;
3061 if (open
->op_deleg_want
& NFS4_SHARE_WANT_NO_DELEG
) {
3062 open
->op_delegate_type
= NFS4_OPEN_DELEGATE_NONE_EXT
;
3063 open
->op_why_no_deleg
= WND4_NOT_WANTED
;
3069 * Attempt to hand out a delegation. No error return, because the
3070 * OPEN succeeds even if we fail.
3072 nfs4_open_delegation(SVC_NET(rqstp
), current_fh
, open
, stp
);
3076 dprintk("%s: stateid=" STATEID_FMT
"\n", __func__
,
3077 STATEID_VAL(&stp
->st_stid
.sc_stateid
));
3079 /* 4.1 client trying to upgrade/downgrade delegation? */
3080 if (open
->op_delegate_type
== NFS4_OPEN_DELEGATE_NONE
&& dp
&&
3081 open
->op_deleg_want
)
3082 nfsd4_deleg_xgrade_none_ext(open
, dp
);
3086 if (status
== 0 && open
->op_claim_type
== NFS4_OPEN_CLAIM_PREVIOUS
)
3087 nfs4_set_claim_prev(open
, nfsd4_has_session(&resp
->cstate
));
3089 * To finish the open response, we just need to set the rflags.
3091 open
->op_rflags
= NFS4_OPEN_RESULT_LOCKTYPE_POSIX
;
3092 if (!(open
->op_openowner
->oo_flags
& NFS4_OO_CONFIRMED
) &&
3093 !nfsd4_has_session(&resp
->cstate
))
3094 open
->op_rflags
|= NFS4_OPEN_RESULT_CONFIRM
;
3099 void nfsd4_cleanup_open_state(struct nfsd4_open
*open
, __be32 status
)
3101 if (open
->op_openowner
) {
3102 struct nfs4_openowner
*oo
= open
->op_openowner
;
3104 if (!list_empty(&oo
->oo_owner
.so_stateids
))
3105 list_del_init(&oo
->oo_close_lru
);
3106 if (oo
->oo_flags
& NFS4_OO_NEW
) {
3108 release_openowner(oo
);
3109 open
->op_openowner
= NULL
;
3111 oo
->oo_flags
&= ~NFS4_OO_NEW
;
3115 nfsd4_free_file(open
->op_file
);
3117 free_generic_stateid(open
->op_stp
);
3120 static __be32
lookup_clientid(clientid_t
*clid
, bool session
, struct nfsd_net
*nn
, struct nfs4_client
**clp
)
3122 struct nfs4_client
*found
;
3124 if (STALE_CLIENTID(clid
, nn
))
3125 return nfserr_stale_clientid
;
3126 found
= find_confirmed_client(clid
, session
, nn
);
3129 return found
? nfs_ok
: nfserr_expired
;
3133 nfsd4_renew(struct svc_rqst
*rqstp
, struct nfsd4_compound_state
*cstate
,
3136 struct nfs4_client
*clp
;
3138 struct nfsd_net
*nn
= net_generic(SVC_NET(rqstp
), nfsd_net_id
);
3141 dprintk("process_renew(%08x/%08x): starting\n",
3142 clid
->cl_boot
, clid
->cl_id
);
3143 status
= lookup_clientid(clid
, cstate
->minorversion
, nn
, &clp
);
3146 status
= nfserr_cb_path_down
;
3147 if (!list_empty(&clp
->cl_delegations
)
3148 && clp
->cl_cb_state
!= NFSD4_CB_UP
)
3152 nfs4_unlock_state();
3157 nfsd4_end_grace(struct nfsd_net
*nn
)
3159 /* do nothing if grace period already ended */
3160 if (nn
->grace_ended
)
3163 dprintk("NFSD: end of grace period\n");
3164 nn
->grace_ended
= true;
3165 nfsd4_record_grace_done(nn
, nn
->boot_time
);
3166 locks_end_grace(&nn
->nfsd4_manager
);
3168 * Now that every NFSv4 client has had the chance to recover and
3169 * to see the (possibly new, possibly shorter) lease time, we
3170 * can safely set the next grace time to the current lease time:
3172 nn
->nfsd4_grace
= nn
->nfsd4_lease
;
3176 nfs4_laundromat(struct nfsd_net
*nn
)
3178 struct nfs4_client
*clp
;
3179 struct nfs4_openowner
*oo
;
3180 struct nfs4_delegation
*dp
;
3181 struct list_head
*pos
, *next
, reaplist
;
3182 time_t cutoff
= get_seconds() - nn
->nfsd4_lease
;
3183 time_t t
, clientid_val
= nn
->nfsd4_lease
;
3184 time_t u
, test_val
= nn
->nfsd4_lease
;
3188 dprintk("NFSD: laundromat service - starting\n");
3189 nfsd4_end_grace(nn
);
3190 INIT_LIST_HEAD(&reaplist
);
3191 spin_lock(&nn
->client_lock
);
3192 list_for_each_safe(pos
, next
, &nn
->client_lru
) {
3193 clp
= list_entry(pos
, struct nfs4_client
, cl_lru
);
3194 if (time_after((unsigned long)clp
->cl_time
, (unsigned long)cutoff
)) {
3195 t
= clp
->cl_time
- cutoff
;
3196 if (clientid_val
> t
)
3200 if (atomic_read(&clp
->cl_refcount
)) {
3201 dprintk("NFSD: client in use (clientid %08x)\n",
3202 clp
->cl_clientid
.cl_id
);
3205 unhash_client_locked(clp
);
3206 list_add(&clp
->cl_lru
, &reaplist
);
3208 spin_unlock(&nn
->client_lock
);
3209 list_for_each_safe(pos
, next
, &reaplist
) {
3210 clp
= list_entry(pos
, struct nfs4_client
, cl_lru
);
3211 dprintk("NFSD: purging unused client (clientid %08x)\n",
3212 clp
->cl_clientid
.cl_id
);
3215 spin_lock(&recall_lock
);
3216 list_for_each_safe(pos
, next
, &del_recall_lru
) {
3217 dp
= list_entry (pos
, struct nfs4_delegation
, dl_recall_lru
);
3218 if (net_generic(dp
->dl_stid
.sc_client
->net
, nfsd_net_id
) != nn
)
3220 if (time_after((unsigned long)dp
->dl_time
, (unsigned long)cutoff
)) {
3221 u
= dp
->dl_time
- cutoff
;
3226 list_move(&dp
->dl_recall_lru
, &reaplist
);
3228 spin_unlock(&recall_lock
);
3229 list_for_each_safe(pos
, next
, &reaplist
) {
3230 dp
= list_entry (pos
, struct nfs4_delegation
, dl_recall_lru
);
3231 unhash_delegation(dp
);
3233 test_val
= nn
->nfsd4_lease
;
3234 list_for_each_safe(pos
, next
, &nn
->close_lru
) {
3235 oo
= container_of(pos
, struct nfs4_openowner
, oo_close_lru
);
3236 if (time_after((unsigned long)oo
->oo_time
, (unsigned long)cutoff
)) {
3237 u
= oo
->oo_time
- cutoff
;
3242 release_openowner(oo
);
3244 if (clientid_val
< NFSD_LAUNDROMAT_MINTIMEOUT
)
3245 clientid_val
= NFSD_LAUNDROMAT_MINTIMEOUT
;
3246 nfs4_unlock_state();
3247 return clientid_val
;
3250 static struct workqueue_struct
*laundry_wq
;
3251 static void laundromat_main(struct work_struct
*);
3254 laundromat_main(struct work_struct
*laundry
)
3257 struct delayed_work
*dwork
= container_of(laundry
, struct delayed_work
,
3259 struct nfsd_net
*nn
= container_of(dwork
, struct nfsd_net
,
3262 t
= nfs4_laundromat(nn
);
3263 dprintk("NFSD: laundromat_main - sleeping for %ld seconds\n", t
);
3264 queue_delayed_work(laundry_wq
, &nn
->laundromat_work
, t
*HZ
);
3267 static inline __be32
nfs4_check_fh(struct svc_fh
*fhp
, struct nfs4_ol_stateid
*stp
)
3269 if (fhp
->fh_dentry
->d_inode
!= stp
->st_file
->fi_inode
)
3270 return nfserr_bad_stateid
;
3275 STALE_STATEID(stateid_t
*stateid
, struct nfsd_net
*nn
)
3277 if (stateid
->si_opaque
.so_clid
.cl_boot
== nn
->boot_time
)
3279 dprintk("NFSD: stale stateid " STATEID_FMT
"!\n",
3280 STATEID_VAL(stateid
));
3285 access_permit_read(struct nfs4_ol_stateid
*stp
)
3287 return test_access(NFS4_SHARE_ACCESS_READ
, stp
) ||
3288 test_access(NFS4_SHARE_ACCESS_BOTH
, stp
) ||
3289 test_access(NFS4_SHARE_ACCESS_WRITE
, stp
);
3293 access_permit_write(struct nfs4_ol_stateid
*stp
)
3295 return test_access(NFS4_SHARE_ACCESS_WRITE
, stp
) ||
3296 test_access(NFS4_SHARE_ACCESS_BOTH
, stp
);
3300 __be32
nfs4_check_openmode(struct nfs4_ol_stateid
*stp
, int flags
)
3302 __be32 status
= nfserr_openmode
;
3304 /* For lock stateid's, we test the parent open, not the lock: */
3305 if (stp
->st_openstp
)
3306 stp
= stp
->st_openstp
;
3307 if ((flags
& WR_STATE
) && !access_permit_write(stp
))
3309 if ((flags
& RD_STATE
) && !access_permit_read(stp
))
3316 static inline __be32
3317 check_special_stateids(struct net
*net
, svc_fh
*current_fh
, stateid_t
*stateid
, int flags
)
3319 if (ONE_STATEID(stateid
) && (flags
& RD_STATE
))
3321 else if (locks_in_grace(net
)) {
3322 /* Answer in remaining cases depends on existence of
3323 * conflicting state; so we must wait out the grace period. */
3324 return nfserr_grace
;
3325 } else if (flags
& WR_STATE
)
3326 return nfs4_share_conflict(current_fh
,
3327 NFS4_SHARE_DENY_WRITE
);
3328 else /* (flags & RD_STATE) && ZERO_STATEID(stateid) */
3329 return nfs4_share_conflict(current_fh
,
3330 NFS4_SHARE_DENY_READ
);
3334 * Allow READ/WRITE during grace period on recovered state only for files
3335 * that are not able to provide mandatory locking.
3338 grace_disallows_io(struct net
*net
, struct inode
*inode
)
3340 return locks_in_grace(net
) && mandatory_lock(inode
);
3343 /* Returns true iff a is later than b: */
3344 static bool stateid_generation_after(stateid_t
*a
, stateid_t
*b
)
3346 return (s32
)a
->si_generation
- (s32
)b
->si_generation
> 0;
3349 static __be32
check_stateid_generation(stateid_t
*in
, stateid_t
*ref
, bool has_session
)
3352 * When sessions are used the stateid generation number is ignored
3355 if (has_session
&& in
->si_generation
== 0)
3358 if (in
->si_generation
== ref
->si_generation
)
3361 /* If the client sends us a stateid from the future, it's buggy: */
3362 if (stateid_generation_after(in
, ref
))
3363 return nfserr_bad_stateid
;
3365 * However, we could see a stateid from the past, even from a
3366 * non-buggy client. For example, if the client sends a lock
3367 * while some IO is outstanding, the lock may bump si_generation
3368 * while the IO is still in flight. The client could avoid that
3369 * situation by waiting for responses on all the IO requests,
3370 * but better performance may result in retrying IO that
3371 * receives an old_stateid error if requests are rarely
3372 * reordered in flight:
3374 return nfserr_old_stateid
;
3377 static __be32
nfsd4_validate_stateid(struct nfs4_client
*cl
, stateid_t
*stateid
)
3379 struct nfs4_stid
*s
;
3380 struct nfs4_ol_stateid
*ols
;
3383 if (ZERO_STATEID(stateid
) || ONE_STATEID(stateid
))
3384 return nfserr_bad_stateid
;
3385 /* Client debugging aid. */
3386 if (!same_clid(&stateid
->si_opaque
.so_clid
, &cl
->cl_clientid
)) {
3387 char addr_str
[INET6_ADDRSTRLEN
];
3388 rpc_ntop((struct sockaddr
*)&cl
->cl_addr
, addr_str
,
3390 pr_warn_ratelimited("NFSD: client %s testing state ID "
3391 "with incorrect client ID\n", addr_str
);
3392 return nfserr_bad_stateid
;
3394 s
= find_stateid(cl
, stateid
);
3396 return nfserr_bad_stateid
;
3397 status
= check_stateid_generation(stateid
, &s
->sc_stateid
, 1);
3400 if (!(s
->sc_type
& (NFS4_OPEN_STID
| NFS4_LOCK_STID
)))
3402 ols
= openlockstateid(s
);
3403 if (ols
->st_stateowner
->so_is_open_owner
3404 && !(openowner(ols
->st_stateowner
)->oo_flags
& NFS4_OO_CONFIRMED
))
3405 return nfserr_bad_stateid
;
3409 static __be32
nfsd4_lookup_stateid(stateid_t
*stateid
, unsigned char typemask
,
3410 struct nfs4_stid
**s
, bool sessions
,
3411 struct nfsd_net
*nn
)
3413 struct nfs4_client
*cl
;
3415 if (ZERO_STATEID(stateid
) || ONE_STATEID(stateid
))
3416 return nfserr_bad_stateid
;
3417 if (STALE_STATEID(stateid
, nn
))
3418 return nfserr_stale_stateid
;
3419 cl
= find_confirmed_client(&stateid
->si_opaque
.so_clid
, sessions
, nn
);
3421 return nfserr_expired
;
3422 *s
= find_stateid_by_type(cl
, stateid
, typemask
);
3424 return nfserr_bad_stateid
;
3430 * Checks for stateid operations
3433 nfs4_preprocess_stateid_op(struct net
*net
, struct nfsd4_compound_state
*cstate
,
3434 stateid_t
*stateid
, int flags
, struct file
**filpp
)
3436 struct nfs4_stid
*s
;
3437 struct nfs4_ol_stateid
*stp
= NULL
;
3438 struct nfs4_delegation
*dp
= NULL
;
3439 struct svc_fh
*current_fh
= &cstate
->current_fh
;
3440 struct inode
*ino
= current_fh
->fh_dentry
->d_inode
;
3441 struct nfsd_net
*nn
= net_generic(net
, nfsd_net_id
);
3447 if (grace_disallows_io(net
, ino
))
3448 return nfserr_grace
;
3450 if (ZERO_STATEID(stateid
) || ONE_STATEID(stateid
))
3451 return check_special_stateids(net
, current_fh
, stateid
, flags
);
3453 status
= nfsd4_lookup_stateid(stateid
, NFS4_DELEG_STID
|NFS4_OPEN_STID
|NFS4_LOCK_STID
,
3454 &s
, cstate
->minorversion
, nn
);
3457 status
= check_stateid_generation(stateid
, &s
->sc_stateid
, nfsd4_has_session(cstate
));
3460 switch (s
->sc_type
) {
3461 case NFS4_DELEG_STID
:
3462 dp
= delegstateid(s
);
3463 status
= nfs4_check_delegmode(dp
, flags
);
3467 *filpp
= dp
->dl_file
->fi_deleg_file
;
3470 status
= nfserr_serverfault
;
3475 case NFS4_OPEN_STID
:
3476 case NFS4_LOCK_STID
:
3477 stp
= openlockstateid(s
);
3478 status
= nfs4_check_fh(current_fh
, stp
);
3481 if (stp
->st_stateowner
->so_is_open_owner
3482 && !(openowner(stp
->st_stateowner
)->oo_flags
& NFS4_OO_CONFIRMED
))
3484 status
= nfs4_check_openmode(stp
, flags
);
3488 if (flags
& RD_STATE
)
3489 *filpp
= find_readable_file(stp
->st_file
);
3491 *filpp
= find_writeable_file(stp
->st_file
);
3495 return nfserr_bad_stateid
;
3503 nfsd4_free_lock_stateid(struct nfs4_ol_stateid
*stp
)
3505 if (check_for_locks(stp
->st_file
, lockowner(stp
->st_stateowner
)))
3506 return nfserr_locks_held
;
3507 release_lock_stateid(stp
);
3512 * Test if the stateid is valid
3515 nfsd4_test_stateid(struct svc_rqst
*rqstp
, struct nfsd4_compound_state
*cstate
,
3516 struct nfsd4_test_stateid
*test_stateid
)
3518 struct nfsd4_test_stateid_id
*stateid
;
3519 struct nfs4_client
*cl
= cstate
->session
->se_client
;
3522 list_for_each_entry(stateid
, &test_stateid
->ts_stateid_list
, ts_id_list
)
3523 stateid
->ts_id_status
=
3524 nfsd4_validate_stateid(cl
, &stateid
->ts_id_stateid
);
3525 nfs4_unlock_state();
3531 nfsd4_free_stateid(struct svc_rqst
*rqstp
, struct nfsd4_compound_state
*cstate
,
3532 struct nfsd4_free_stateid
*free_stateid
)
3534 stateid_t
*stateid
= &free_stateid
->fr_stateid
;
3535 struct nfs4_stid
*s
;
3536 struct nfs4_client
*cl
= cstate
->session
->se_client
;
3537 __be32 ret
= nfserr_bad_stateid
;
3540 s
= find_stateid(cl
, stateid
);
3543 switch (s
->sc_type
) {
3544 case NFS4_DELEG_STID
:
3545 ret
= nfserr_locks_held
;
3547 case NFS4_OPEN_STID
:
3548 case NFS4_LOCK_STID
:
3549 ret
= check_stateid_generation(stateid
, &s
->sc_stateid
, 1);
3552 if (s
->sc_type
== NFS4_LOCK_STID
)
3553 ret
= nfsd4_free_lock_stateid(openlockstateid(s
));
3555 ret
= nfserr_locks_held
;
3558 ret
= nfserr_bad_stateid
;
3561 nfs4_unlock_state();
3568 return (type
== NFS4_READW_LT
|| type
== NFS4_READ_LT
) ?
3569 RD_STATE
: WR_STATE
;
3572 static __be32
nfs4_seqid_op_checks(struct nfsd4_compound_state
*cstate
, stateid_t
*stateid
, u32 seqid
, struct nfs4_ol_stateid
*stp
)
3574 struct svc_fh
*current_fh
= &cstate
->current_fh
;
3575 struct nfs4_stateowner
*sop
= stp
->st_stateowner
;
3578 status
= nfsd4_check_seqid(cstate
, sop
, seqid
);
3581 if (stp
->st_stid
.sc_type
== NFS4_CLOSED_STID
)
3583 * "Closed" stateid's exist *only* to return
3584 * nfserr_replay_me from the previous step.
3586 return nfserr_bad_stateid
;
3587 status
= check_stateid_generation(stateid
, &stp
->st_stid
.sc_stateid
, nfsd4_has_session(cstate
));
3590 return nfs4_check_fh(current_fh
, stp
);
3594 * Checks for sequence id mutating operations.
3597 nfs4_preprocess_seqid_op(struct nfsd4_compound_state
*cstate
, u32 seqid
,
3598 stateid_t
*stateid
, char typemask
,
3599 struct nfs4_ol_stateid
**stpp
,
3600 struct nfsd_net
*nn
)
3603 struct nfs4_stid
*s
;
3605 dprintk("NFSD: %s: seqid=%d stateid = " STATEID_FMT
"\n", __func__
,
3606 seqid
, STATEID_VAL(stateid
));
3609 status
= nfsd4_lookup_stateid(stateid
, typemask
, &s
,
3610 cstate
->minorversion
, nn
);
3613 *stpp
= openlockstateid(s
);
3614 cstate
->replay_owner
= (*stpp
)->st_stateowner
;
3616 return nfs4_seqid_op_checks(cstate
, stateid
, seqid
, *stpp
);
3619 static __be32
nfs4_preprocess_confirmed_seqid_op(struct nfsd4_compound_state
*cstate
, u32 seqid
,
3620 stateid_t
*stateid
, struct nfs4_ol_stateid
**stpp
, struct nfsd_net
*nn
)
3623 struct nfs4_openowner
*oo
;
3625 status
= nfs4_preprocess_seqid_op(cstate
, seqid
, stateid
,
3626 NFS4_OPEN_STID
, stpp
, nn
);
3629 oo
= openowner((*stpp
)->st_stateowner
);
3630 if (!(oo
->oo_flags
& NFS4_OO_CONFIRMED
))
3631 return nfserr_bad_stateid
;
3636 nfsd4_open_confirm(struct svc_rqst
*rqstp
, struct nfsd4_compound_state
*cstate
,
3637 struct nfsd4_open_confirm
*oc
)
3640 struct nfs4_openowner
*oo
;
3641 struct nfs4_ol_stateid
*stp
;
3642 struct nfsd_net
*nn
= net_generic(SVC_NET(rqstp
), nfsd_net_id
);
3644 dprintk("NFSD: nfsd4_open_confirm on file %.*s\n",
3645 (int)cstate
->current_fh
.fh_dentry
->d_name
.len
,
3646 cstate
->current_fh
.fh_dentry
->d_name
.name
);
3648 status
= fh_verify(rqstp
, &cstate
->current_fh
, S_IFREG
, 0);
3654 status
= nfs4_preprocess_seqid_op(cstate
,
3655 oc
->oc_seqid
, &oc
->oc_req_stateid
,
3656 NFS4_OPEN_STID
, &stp
, nn
);
3659 oo
= openowner(stp
->st_stateowner
);
3660 status
= nfserr_bad_stateid
;
3661 if (oo
->oo_flags
& NFS4_OO_CONFIRMED
)
3663 oo
->oo_flags
|= NFS4_OO_CONFIRMED
;
3664 update_stateid(&stp
->st_stid
.sc_stateid
);
3665 memcpy(&oc
->oc_resp_stateid
, &stp
->st_stid
.sc_stateid
, sizeof(stateid_t
));
3666 dprintk("NFSD: %s: success, seqid=%d stateid=" STATEID_FMT
"\n",
3667 __func__
, oc
->oc_seqid
, STATEID_VAL(&stp
->st_stid
.sc_stateid
));
3669 nfsd4_client_record_create(oo
->oo_owner
.so_client
);
3672 if (!cstate
->replay_owner
)
3673 nfs4_unlock_state();
3677 static inline void nfs4_stateid_downgrade_bit(struct nfs4_ol_stateid
*stp
, u32 access
)
3679 if (!test_access(access
, stp
))
3681 nfs4_file_put_access(stp
->st_file
, nfs4_access_to_omode(access
));
3682 clear_access(access
, stp
);
3685 static inline void nfs4_stateid_downgrade(struct nfs4_ol_stateid
*stp
, u32 to_access
)
3687 switch (to_access
) {
3688 case NFS4_SHARE_ACCESS_READ
:
3689 nfs4_stateid_downgrade_bit(stp
, NFS4_SHARE_ACCESS_WRITE
);
3690 nfs4_stateid_downgrade_bit(stp
, NFS4_SHARE_ACCESS_BOTH
);
3692 case NFS4_SHARE_ACCESS_WRITE
:
3693 nfs4_stateid_downgrade_bit(stp
, NFS4_SHARE_ACCESS_READ
);
3694 nfs4_stateid_downgrade_bit(stp
, NFS4_SHARE_ACCESS_BOTH
);
3696 case NFS4_SHARE_ACCESS_BOTH
:
3704 reset_union_bmap_deny(unsigned long deny
, struct nfs4_ol_stateid
*stp
)
3707 for (i
= 0; i
< 4; i
++) {
3708 if ((i
& deny
) != i
)
3714 nfsd4_open_downgrade(struct svc_rqst
*rqstp
,
3715 struct nfsd4_compound_state
*cstate
,
3716 struct nfsd4_open_downgrade
*od
)
3719 struct nfs4_ol_stateid
*stp
;
3720 struct nfsd_net
*nn
= net_generic(SVC_NET(rqstp
), nfsd_net_id
);
3722 dprintk("NFSD: nfsd4_open_downgrade on file %.*s\n",
3723 (int)cstate
->current_fh
.fh_dentry
->d_name
.len
,
3724 cstate
->current_fh
.fh_dentry
->d_name
.name
);
3726 /* We don't yet support WANT bits: */
3727 if (od
->od_deleg_want
)
3728 dprintk("NFSD: %s: od_deleg_want=0x%x ignored\n", __func__
,
3732 status
= nfs4_preprocess_confirmed_seqid_op(cstate
, od
->od_seqid
,
3733 &od
->od_stateid
, &stp
, nn
);
3736 status
= nfserr_inval
;
3737 if (!test_access(od
->od_share_access
, stp
)) {
3738 dprintk("NFSD: access not a subset current bitmap: 0x%lx, input access=%08x\n",
3739 stp
->st_access_bmap
, od
->od_share_access
);
3742 if (!test_deny(od
->od_share_deny
, stp
)) {
3743 dprintk("NFSD:deny not a subset current bitmap: 0x%lx, input deny=%08x\n",
3744 stp
->st_deny_bmap
, od
->od_share_deny
);
3747 nfs4_stateid_downgrade(stp
, od
->od_share_access
);
3749 reset_union_bmap_deny(od
->od_share_deny
, stp
);
3751 update_stateid(&stp
->st_stid
.sc_stateid
);
3752 memcpy(&od
->od_stateid
, &stp
->st_stid
.sc_stateid
, sizeof(stateid_t
));
3755 if (!cstate
->replay_owner
)
3756 nfs4_unlock_state();
3760 void nfsd4_purge_closed_stateid(struct nfs4_stateowner
*so
)
3762 struct nfs4_openowner
*oo
;
3763 struct nfs4_ol_stateid
*s
;
3765 if (!so
->so_is_open_owner
)
3768 s
= oo
->oo_last_closed_stid
;
3771 if (!(oo
->oo_flags
& NFS4_OO_PURGE_CLOSE
)) {
3772 /* Release the last_closed_stid on the next seqid bump: */
3773 oo
->oo_flags
|= NFS4_OO_PURGE_CLOSE
;
3776 oo
->oo_flags
&= ~NFS4_OO_PURGE_CLOSE
;
3777 release_last_closed_stateid(oo
);
3780 static void nfsd4_close_open_stateid(struct nfs4_ol_stateid
*s
)
3782 unhash_open_stateid(s
);
3783 s
->st_stid
.sc_type
= NFS4_CLOSED_STID
;
3787 * nfs4_unlock_state() called after encode
3790 nfsd4_close(struct svc_rqst
*rqstp
, struct nfsd4_compound_state
*cstate
,
3791 struct nfsd4_close
*close
)
3794 struct nfs4_openowner
*oo
;
3795 struct nfs4_ol_stateid
*stp
;
3796 struct net
*net
= SVC_NET(rqstp
);
3797 struct nfsd_net
*nn
= net_generic(net
, nfsd_net_id
);
3799 dprintk("NFSD: nfsd4_close on file %.*s\n",
3800 (int)cstate
->current_fh
.fh_dentry
->d_name
.len
,
3801 cstate
->current_fh
.fh_dentry
->d_name
.name
);
3804 status
= nfs4_preprocess_seqid_op(cstate
, close
->cl_seqid
,
3806 NFS4_OPEN_STID
|NFS4_CLOSED_STID
,
3810 oo
= openowner(stp
->st_stateowner
);
3812 update_stateid(&stp
->st_stid
.sc_stateid
);
3813 memcpy(&close
->cl_stateid
, &stp
->st_stid
.sc_stateid
, sizeof(stateid_t
));
3815 nfsd4_close_open_stateid(stp
);
3816 release_last_closed_stateid(oo
);
3817 oo
->oo_last_closed_stid
= stp
;
3819 if (list_empty(&oo
->oo_owner
.so_stateids
)) {
3820 if (cstate
->minorversion
) {
3821 release_openowner(oo
);
3822 cstate
->replay_owner
= NULL
;
3825 * In the 4.0 case we need to keep the owners around a
3826 * little while to handle CLOSE replay.
3828 if (list_empty(&oo
->oo_owner
.so_stateids
))
3829 move_to_close_lru(oo
, SVC_NET(rqstp
));
3833 if (!cstate
->replay_owner
)
3834 nfs4_unlock_state();
3839 nfsd4_delegreturn(struct svc_rqst
*rqstp
, struct nfsd4_compound_state
*cstate
,
3840 struct nfsd4_delegreturn
*dr
)
3842 struct nfs4_delegation
*dp
;
3843 stateid_t
*stateid
= &dr
->dr_stateid
;
3844 struct nfs4_stid
*s
;
3846 struct nfsd_net
*nn
= net_generic(SVC_NET(rqstp
), nfsd_net_id
);
3848 if ((status
= fh_verify(rqstp
, &cstate
->current_fh
, S_IFREG
, 0)))
3852 status
= nfsd4_lookup_stateid(stateid
, NFS4_DELEG_STID
, &s
,
3853 cstate
->minorversion
, nn
);
3856 dp
= delegstateid(s
);
3857 status
= check_stateid_generation(stateid
, &dp
->dl_stid
.sc_stateid
, nfsd4_has_session(cstate
));
3861 unhash_delegation(dp
);
3863 nfs4_unlock_state();
3869 #define LOFF_OVERFLOW(start, len) ((u64)(len) > ~(u64)(start))
3871 #define LOCKOWNER_INO_HASH_MASK (LOCKOWNER_INO_HASH_SIZE - 1)
3874 end_offset(u64 start
, u64 len
)
3879 return end
>= start
? end
: NFS4_MAX_UINT64
;
3882 /* last octet in a range */
3884 last_byte_offset(u64 start
, u64 len
)
3890 return end
> start
? end
- 1: NFS4_MAX_UINT64
;
3893 static unsigned int lockowner_ino_hashval(struct inode
*inode
, u32 cl_id
, struct xdr_netobj
*ownername
)
3895 return (file_hashval(inode
) + cl_id
3896 + opaque_hashval(ownername
->data
, ownername
->len
))
3897 & LOCKOWNER_INO_HASH_MASK
;
3901 * TODO: Linux file offsets are _signed_ 64-bit quantities, which means that
3902 * we can't properly handle lock requests that go beyond the (2^63 - 1)-th
3903 * byte, because of sign extension problems. Since NFSv4 calls for 64-bit
3904 * locking, this prevents us from being completely protocol-compliant. The
3905 * real solution to this problem is to start using unsigned file offsets in
3906 * the VFS, but this is a very deep change!
3909 nfs4_transform_lock_offset(struct file_lock
*lock
)
3911 if (lock
->fl_start
< 0)
3912 lock
->fl_start
= OFFSET_MAX
;
3913 if (lock
->fl_end
< 0)
3914 lock
->fl_end
= OFFSET_MAX
;
3917 /* Hack!: For now, we're defining this just so we can use a pointer to it
3918 * as a unique cookie to identify our (NFSv4's) posix locks. */
3919 static const struct lock_manager_operations nfsd_posix_mng_ops
= {
3923 nfs4_set_lock_denied(struct file_lock
*fl
, struct nfsd4_lock_denied
*deny
)
3925 struct nfs4_lockowner
*lo
;
3927 if (fl
->fl_lmops
== &nfsd_posix_mng_ops
) {
3928 lo
= (struct nfs4_lockowner
*) fl
->fl_owner
;
3929 deny
->ld_owner
.data
= kmemdup(lo
->lo_owner
.so_owner
.data
,
3930 lo
->lo_owner
.so_owner
.len
, GFP_KERNEL
);
3931 if (!deny
->ld_owner
.data
)
3932 /* We just don't care that much */
3934 deny
->ld_owner
.len
= lo
->lo_owner
.so_owner
.len
;
3935 deny
->ld_clientid
= lo
->lo_owner
.so_client
->cl_clientid
;
3938 deny
->ld_owner
.len
= 0;
3939 deny
->ld_owner
.data
= NULL
;
3940 deny
->ld_clientid
.cl_boot
= 0;
3941 deny
->ld_clientid
.cl_id
= 0;
3943 deny
->ld_start
= fl
->fl_start
;
3944 deny
->ld_length
= NFS4_MAX_UINT64
;
3945 if (fl
->fl_end
!= NFS4_MAX_UINT64
)
3946 deny
->ld_length
= fl
->fl_end
- fl
->fl_start
+ 1;
3947 deny
->ld_type
= NFS4_READ_LT
;
3948 if (fl
->fl_type
!= F_RDLCK
)
3949 deny
->ld_type
= NFS4_WRITE_LT
;
3952 static bool same_lockowner_ino(struct nfs4_lockowner
*lo
, struct inode
*inode
, clientid_t
*clid
, struct xdr_netobj
*owner
)
3954 struct nfs4_ol_stateid
*lst
;
3956 if (!same_owner_str(&lo
->lo_owner
, owner
, clid
))
3958 lst
= list_first_entry(&lo
->lo_owner
.so_stateids
,
3959 struct nfs4_ol_stateid
, st_perstateowner
);
3960 return lst
->st_file
->fi_inode
== inode
;
3963 static struct nfs4_lockowner
*
3964 find_lockowner_str(struct inode
*inode
, clientid_t
*clid
,
3965 struct xdr_netobj
*owner
, struct nfsd_net
*nn
)
3967 unsigned int hashval
= lockowner_ino_hashval(inode
, clid
->cl_id
, owner
);
3968 struct nfs4_lockowner
*lo
;
3970 list_for_each_entry(lo
, &nn
->lockowner_ino_hashtbl
[hashval
], lo_owner_ino_hash
) {
3971 if (same_lockowner_ino(lo
, inode
, clid
, owner
))
3977 static void hash_lockowner(struct nfs4_lockowner
*lo
, unsigned int strhashval
, struct nfs4_client
*clp
, struct nfs4_ol_stateid
*open_stp
)
3979 struct inode
*inode
= open_stp
->st_file
->fi_inode
;
3980 unsigned int inohash
= lockowner_ino_hashval(inode
,
3981 clp
->cl_clientid
.cl_id
, &lo
->lo_owner
.so_owner
);
3982 struct nfsd_net
*nn
= net_generic(clp
->net
, nfsd_net_id
);
3984 list_add(&lo
->lo_owner
.so_strhash
, &nn
->ownerstr_hashtbl
[strhashval
]);
3985 list_add(&lo
->lo_owner_ino_hash
, &nn
->lockowner_ino_hashtbl
[inohash
]);
3986 list_add(&lo
->lo_perstateid
, &open_stp
->st_lockowners
);
3990 * Alloc a lock owner structure.
3991 * Called in nfsd4_lock - therefore, OPEN and OPEN_CONFIRM (if needed) has
3994 * strhashval = ownerstr_hashval
3997 static struct nfs4_lockowner
*
3998 alloc_init_lock_stateowner(unsigned int strhashval
, struct nfs4_client
*clp
, struct nfs4_ol_stateid
*open_stp
, struct nfsd4_lock
*lock
) {
3999 struct nfs4_lockowner
*lo
;
4001 lo
= alloc_stateowner(lockowner_slab
, &lock
->lk_new_owner
, clp
);
4004 INIT_LIST_HEAD(&lo
->lo_owner
.so_stateids
);
4005 lo
->lo_owner
.so_is_open_owner
= 0;
4006 /* It is the openowner seqid that will be incremented in encode in the
4007 * case of new lockowners; so increment the lock seqid manually: */
4008 lo
->lo_owner
.so_seqid
= lock
->lk_new_lock_seqid
+ 1;
4009 hash_lockowner(lo
, strhashval
, clp
, open_stp
);
4013 static struct nfs4_ol_stateid
*
4014 alloc_init_lock_stateid(struct nfs4_lockowner
*lo
, struct nfs4_file
*fp
, struct nfs4_ol_stateid
*open_stp
)
4016 struct nfs4_ol_stateid
*stp
;
4017 struct nfs4_client
*clp
= lo
->lo_owner
.so_client
;
4019 stp
= nfs4_alloc_stateid(clp
);
4022 stp
->st_stid
.sc_type
= NFS4_LOCK_STID
;
4023 list_add(&stp
->st_perfile
, &fp
->fi_stateids
);
4024 list_add(&stp
->st_perstateowner
, &lo
->lo_owner
.so_stateids
);
4025 stp
->st_stateowner
= &lo
->lo_owner
;
4028 stp
->st_access_bmap
= 0;
4029 stp
->st_deny_bmap
= open_stp
->st_deny_bmap
;
4030 stp
->st_openstp
= open_stp
;
4035 check_lock_length(u64 offset
, u64 length
)
4037 return ((length
== 0) || ((length
!= NFS4_MAX_UINT64
) &&
4038 LOFF_OVERFLOW(offset
, length
)));
4041 static void get_lock_access(struct nfs4_ol_stateid
*lock_stp
, u32 access
)
4043 struct nfs4_file
*fp
= lock_stp
->st_file
;
4044 int oflag
= nfs4_access_to_omode(access
);
4046 if (test_access(access
, lock_stp
))
4048 nfs4_file_get_access(fp
, oflag
);
4049 set_access(access
, lock_stp
);
4052 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)
4054 struct nfs4_file
*fi
= ost
->st_file
;
4055 struct nfs4_openowner
*oo
= openowner(ost
->st_stateowner
);
4056 struct nfs4_client
*cl
= oo
->oo_owner
.so_client
;
4057 struct nfs4_lockowner
*lo
;
4058 unsigned int strhashval
;
4059 struct nfsd_net
*nn
= net_generic(cl
->net
, nfsd_net_id
);
4061 lo
= find_lockowner_str(fi
->fi_inode
, &cl
->cl_clientid
,
4062 &lock
->v
.new.owner
, nn
);
4064 if (!cstate
->minorversion
)
4065 return nfserr_bad_seqid
;
4066 /* XXX: a lockowner always has exactly one stateid: */
4067 *lst
= list_first_entry(&lo
->lo_owner
.so_stateids
,
4068 struct nfs4_ol_stateid
, st_perstateowner
);
4071 strhashval
= ownerstr_hashval(cl
->cl_clientid
.cl_id
,
4072 &lock
->v
.new.owner
);
4073 lo
= alloc_init_lock_stateowner(strhashval
, cl
, ost
, lock
);
4075 return nfserr_jukebox
;
4076 *lst
= alloc_init_lock_stateid(lo
, fi
, ost
);
4078 release_lockowner(lo
);
4079 return nfserr_jukebox
;
4089 nfsd4_lock(struct svc_rqst
*rqstp
, struct nfsd4_compound_state
*cstate
,
4090 struct nfsd4_lock
*lock
)
4092 struct nfs4_openowner
*open_sop
= NULL
;
4093 struct nfs4_lockowner
*lock_sop
= NULL
;
4094 struct nfs4_ol_stateid
*lock_stp
;
4095 struct file
*filp
= NULL
;
4096 struct file_lock
*file_lock
= NULL
;
4097 struct file_lock
*conflock
= NULL
;
4099 bool new_state
= false;
4102 struct net
*net
= SVC_NET(rqstp
);
4103 struct nfsd_net
*nn
= net_generic(net
, nfsd_net_id
);
4105 dprintk("NFSD: nfsd4_lock: start=%Ld length=%Ld\n",
4106 (long long) lock
->lk_offset
,
4107 (long long) lock
->lk_length
);
4109 if (check_lock_length(lock
->lk_offset
, lock
->lk_length
))
4110 return nfserr_inval
;
4112 if ((status
= fh_verify(rqstp
, &cstate
->current_fh
,
4113 S_IFREG
, NFSD_MAY_LOCK
))) {
4114 dprintk("NFSD: nfsd4_lock: permission denied!\n");
4120 if (lock
->lk_is_new
) {
4121 struct nfs4_ol_stateid
*open_stp
= NULL
;
4123 if (nfsd4_has_session(cstate
))
4124 /* See rfc 5661 18.10.3: given clientid is ignored: */
4125 memcpy(&lock
->v
.new.clientid
,
4126 &cstate
->session
->se_client
->cl_clientid
,
4127 sizeof(clientid_t
));
4129 status
= nfserr_stale_clientid
;
4130 if (STALE_CLIENTID(&lock
->lk_new_clientid
, nn
))
4133 /* validate and update open stateid and open seqid */
4134 status
= nfs4_preprocess_confirmed_seqid_op(cstate
,
4135 lock
->lk_new_open_seqid
,
4136 &lock
->lk_new_open_stateid
,
4140 open_sop
= openowner(open_stp
->st_stateowner
);
4141 status
= nfserr_bad_stateid
;
4142 if (!same_clid(&open_sop
->oo_owner
.so_client
->cl_clientid
,
4143 &lock
->v
.new.clientid
))
4145 status
= lookup_or_create_lock_state(cstate
, open_stp
, lock
,
4146 &lock_stp
, &new_state
);
4148 status
= nfs4_preprocess_seqid_op(cstate
,
4149 lock
->lk_old_lock_seqid
,
4150 &lock
->lk_old_lock_stateid
,
4151 NFS4_LOCK_STID
, &lock_stp
, nn
);
4154 lock_sop
= lockowner(lock_stp
->st_stateowner
);
4156 lkflg
= setlkflg(lock
->lk_type
);
4157 status
= nfs4_check_openmode(lock_stp
, lkflg
);
4161 status
= nfserr_grace
;
4162 if (locks_in_grace(net
) && !lock
->lk_reclaim
)
4164 status
= nfserr_no_grace
;
4165 if (!locks_in_grace(net
) && lock
->lk_reclaim
)
4168 file_lock
= locks_alloc_lock();
4170 dprintk("NFSD: %s: unable to allocate lock!\n", __func__
);
4171 status
= nfserr_jukebox
;
4175 locks_init_lock(file_lock
);
4176 switch (lock
->lk_type
) {
4179 filp
= find_readable_file(lock_stp
->st_file
);
4181 get_lock_access(lock_stp
, NFS4_SHARE_ACCESS_READ
);
4182 file_lock
->fl_type
= F_RDLCK
;
4185 case NFS4_WRITEW_LT
:
4186 filp
= find_writeable_file(lock_stp
->st_file
);
4188 get_lock_access(lock_stp
, NFS4_SHARE_ACCESS_WRITE
);
4189 file_lock
->fl_type
= F_WRLCK
;
4192 status
= nfserr_inval
;
4196 status
= nfserr_openmode
;
4199 file_lock
->fl_owner
= (fl_owner_t
)lock_sop
;
4200 file_lock
->fl_pid
= current
->tgid
;
4201 file_lock
->fl_file
= filp
;
4202 file_lock
->fl_flags
= FL_POSIX
;
4203 file_lock
->fl_lmops
= &nfsd_posix_mng_ops
;
4204 file_lock
->fl_start
= lock
->lk_offset
;
4205 file_lock
->fl_end
= last_byte_offset(lock
->lk_offset
, lock
->lk_length
);
4206 nfs4_transform_lock_offset(file_lock
);
4208 conflock
= locks_alloc_lock();
4210 dprintk("NFSD: %s: unable to allocate lock!\n", __func__
);
4211 status
= nfserr_jukebox
;
4215 err
= vfs_lock_file(filp
, F_SETLK
, file_lock
, conflock
);
4217 case 0: /* success! */
4218 update_stateid(&lock_stp
->st_stid
.sc_stateid
);
4219 memcpy(&lock
->lk_resp_stateid
, &lock_stp
->st_stid
.sc_stateid
,
4223 case (EAGAIN
): /* conflock holds conflicting lock */
4224 status
= nfserr_denied
;
4225 dprintk("NFSD: nfsd4_lock: conflicting lock found!\n");
4226 nfs4_set_lock_denied(conflock
, &lock
->lk_denied
);
4229 status
= nfserr_deadlock
;
4232 dprintk("NFSD: nfsd4_lock: vfs_lock_file() failed! status %d\n",err
);
4233 status
= nfserrno(err
);
4237 if (status
&& new_state
)
4238 release_lockowner(lock_sop
);
4239 if (!cstate
->replay_owner
)
4240 nfs4_unlock_state();
4242 locks_free_lock(file_lock
);
4244 locks_free_lock(conflock
);
4249 * The NFSv4 spec allows a client to do a LOCKT without holding an OPEN,
4250 * so we do a temporary open here just to get an open file to pass to
4251 * vfs_test_lock. (Arguably perhaps test_lock should be done with an
4254 static __be32
nfsd_test_lock(struct svc_rqst
*rqstp
, struct svc_fh
*fhp
, struct file_lock
*lock
)
4257 __be32 err
= nfsd_open(rqstp
, fhp
, S_IFREG
, NFSD_MAY_READ
, &file
);
4259 err
= nfserrno(vfs_test_lock(file
, lock
));
4269 nfsd4_lockt(struct svc_rqst
*rqstp
, struct nfsd4_compound_state
*cstate
,
4270 struct nfsd4_lockt
*lockt
)
4272 struct inode
*inode
;
4273 struct file_lock
*file_lock
= NULL
;
4274 struct nfs4_lockowner
*lo
;
4276 struct nfsd_net
*nn
= net_generic(SVC_NET(rqstp
), nfsd_net_id
);
4278 if (locks_in_grace(SVC_NET(rqstp
)))
4279 return nfserr_grace
;
4281 if (check_lock_length(lockt
->lt_offset
, lockt
->lt_length
))
4282 return nfserr_inval
;
4286 if (!nfsd4_has_session(cstate
)) {
4287 status
= lookup_clientid(&lockt
->lt_clientid
, false, nn
, NULL
);
4292 if ((status
= fh_verify(rqstp
, &cstate
->current_fh
, S_IFREG
, 0)))
4295 inode
= cstate
->current_fh
.fh_dentry
->d_inode
;
4296 file_lock
= locks_alloc_lock();
4298 dprintk("NFSD: %s: unable to allocate lock!\n", __func__
);
4299 status
= nfserr_jukebox
;
4302 locks_init_lock(file_lock
);
4303 switch (lockt
->lt_type
) {
4306 file_lock
->fl_type
= F_RDLCK
;
4309 case NFS4_WRITEW_LT
:
4310 file_lock
->fl_type
= F_WRLCK
;
4313 dprintk("NFSD: nfs4_lockt: bad lock type!\n");
4314 status
= nfserr_inval
;
4318 lo
= find_lockowner_str(inode
, &lockt
->lt_clientid
, &lockt
->lt_owner
, nn
);
4320 file_lock
->fl_owner
= (fl_owner_t
)lo
;
4321 file_lock
->fl_pid
= current
->tgid
;
4322 file_lock
->fl_flags
= FL_POSIX
;
4324 file_lock
->fl_start
= lockt
->lt_offset
;
4325 file_lock
->fl_end
= last_byte_offset(lockt
->lt_offset
, lockt
->lt_length
);
4327 nfs4_transform_lock_offset(file_lock
);
4329 status
= nfsd_test_lock(rqstp
, &cstate
->current_fh
, file_lock
);
4333 if (file_lock
->fl_type
!= F_UNLCK
) {
4334 status
= nfserr_denied
;
4335 nfs4_set_lock_denied(file_lock
, &lockt
->lt_denied
);
4338 nfs4_unlock_state();
4340 locks_free_lock(file_lock
);
4345 nfsd4_locku(struct svc_rqst
*rqstp
, struct nfsd4_compound_state
*cstate
,
4346 struct nfsd4_locku
*locku
)
4348 struct nfs4_ol_stateid
*stp
;
4349 struct file
*filp
= NULL
;
4350 struct file_lock
*file_lock
= NULL
;
4353 struct nfsd_net
*nn
= net_generic(SVC_NET(rqstp
), nfsd_net_id
);
4355 dprintk("NFSD: nfsd4_locku: start=%Ld length=%Ld\n",
4356 (long long) locku
->lu_offset
,
4357 (long long) locku
->lu_length
);
4359 if (check_lock_length(locku
->lu_offset
, locku
->lu_length
))
4360 return nfserr_inval
;
4364 status
= nfs4_preprocess_seqid_op(cstate
, locku
->lu_seqid
,
4365 &locku
->lu_stateid
, NFS4_LOCK_STID
,
4369 filp
= find_any_file(stp
->st_file
);
4371 status
= nfserr_lock_range
;
4374 file_lock
= locks_alloc_lock();
4376 dprintk("NFSD: %s: unable to allocate lock!\n", __func__
);
4377 status
= nfserr_jukebox
;
4380 locks_init_lock(file_lock
);
4381 file_lock
->fl_type
= F_UNLCK
;
4382 file_lock
->fl_owner
= (fl_owner_t
)lockowner(stp
->st_stateowner
);
4383 file_lock
->fl_pid
= current
->tgid
;
4384 file_lock
->fl_file
= filp
;
4385 file_lock
->fl_flags
= FL_POSIX
;
4386 file_lock
->fl_lmops
= &nfsd_posix_mng_ops
;
4387 file_lock
->fl_start
= locku
->lu_offset
;
4389 file_lock
->fl_end
= last_byte_offset(locku
->lu_offset
,
4391 nfs4_transform_lock_offset(file_lock
);
4394 * Try to unlock the file in the VFS.
4396 err
= vfs_lock_file(filp
, F_SETLK
, file_lock
, NULL
);
4398 dprintk("NFSD: nfs4_locku: vfs_lock_file failed!\n");
4402 * OK, unlock succeeded; the only thing left to do is update the stateid.
4404 update_stateid(&stp
->st_stid
.sc_stateid
);
4405 memcpy(&locku
->lu_stateid
, &stp
->st_stid
.sc_stateid
, sizeof(stateid_t
));
4408 if (!cstate
->replay_owner
)
4409 nfs4_unlock_state();
4411 locks_free_lock(file_lock
);
4415 status
= nfserrno(err
);
4421 * 1: locks held by lockowner
4422 * 0: no locks held by lockowner
4425 check_for_locks(struct nfs4_file
*filp
, struct nfs4_lockowner
*lowner
)
4427 struct file_lock
**flpp
;
4428 struct inode
*inode
= filp
->fi_inode
;
4432 for (flpp
= &inode
->i_flock
; *flpp
!= NULL
; flpp
= &(*flpp
)->fl_next
) {
4433 if ((*flpp
)->fl_owner
== (fl_owner_t
)lowner
) {
4444 nfsd4_release_lockowner(struct svc_rqst
*rqstp
,
4445 struct nfsd4_compound_state
*cstate
,
4446 struct nfsd4_release_lockowner
*rlockowner
)
4448 clientid_t
*clid
= &rlockowner
->rl_clientid
;
4449 struct nfs4_stateowner
*sop
;
4450 struct nfs4_lockowner
*lo
;
4451 struct nfs4_ol_stateid
*stp
;
4452 struct xdr_netobj
*owner
= &rlockowner
->rl_owner
;
4453 struct list_head matches
;
4454 unsigned int hashval
= ownerstr_hashval(clid
->cl_id
, owner
);
4456 struct nfsd_net
*nn
= net_generic(SVC_NET(rqstp
), nfsd_net_id
);
4458 dprintk("nfsd4_release_lockowner clientid: (%08x/%08x):\n",
4459 clid
->cl_boot
, clid
->cl_id
);
4463 status
= lookup_clientid(clid
, cstate
->minorversion
, nn
, NULL
);
4467 status
= nfserr_locks_held
;
4468 INIT_LIST_HEAD(&matches
);
4470 list_for_each_entry(sop
, &nn
->ownerstr_hashtbl
[hashval
], so_strhash
) {
4471 if (sop
->so_is_open_owner
)
4473 if (!same_owner_str(sop
, owner
, clid
))
4475 list_for_each_entry(stp
, &sop
->so_stateids
,
4477 lo
= lockowner(sop
);
4478 if (check_for_locks(stp
->st_file
, lo
))
4480 list_add(&lo
->lo_list
, &matches
);
4483 /* Clients probably won't expect us to return with some (but not all)
4484 * of the lockowner state released; so don't release any until all
4485 * have been checked. */
4487 while (!list_empty(&matches
)) {
4488 lo
= list_entry(matches
.next
, struct nfs4_lockowner
,
4490 /* unhash_stateowner deletes so_perclient only
4491 * for openowners. */
4492 list_del(&lo
->lo_list
);
4493 release_lockowner(lo
);
4496 nfs4_unlock_state();
4500 static inline struct nfs4_client_reclaim
*
4503 return kmalloc(sizeof(struct nfs4_client_reclaim
), GFP_KERNEL
);
4507 nfs4_has_reclaimed_state(const char *name
, struct nfsd_net
*nn
)
4509 struct nfs4_client_reclaim
*crp
;
4511 crp
= nfsd4_find_reclaim_client(name
, nn
);
4512 return (crp
&& crp
->cr_clp
);
4516 * failure => all reset bets are off, nfserr_no_grace...
4518 struct nfs4_client_reclaim
*
4519 nfs4_client_to_reclaim(const char *name
, struct nfsd_net
*nn
)
4521 unsigned int strhashval
;
4522 struct nfs4_client_reclaim
*crp
;
4524 dprintk("NFSD nfs4_client_to_reclaim NAME: %.*s\n", HEXDIR_LEN
, name
);
4525 crp
= alloc_reclaim();
4527 strhashval
= clientstr_hashval(name
);
4528 INIT_LIST_HEAD(&crp
->cr_strhash
);
4529 list_add(&crp
->cr_strhash
, &nn
->reclaim_str_hashtbl
[strhashval
]);
4530 memcpy(crp
->cr_recdir
, name
, HEXDIR_LEN
);
4532 nn
->reclaim_str_hashtbl_size
++;
4538 nfs4_remove_reclaim_record(struct nfs4_client_reclaim
*crp
, struct nfsd_net
*nn
)
4540 list_del(&crp
->cr_strhash
);
4542 nn
->reclaim_str_hashtbl_size
--;
4546 nfs4_release_reclaim(struct nfsd_net
*nn
)
4548 struct nfs4_client_reclaim
*crp
= NULL
;
4551 for (i
= 0; i
< CLIENT_HASH_SIZE
; i
++) {
4552 while (!list_empty(&nn
->reclaim_str_hashtbl
[i
])) {
4553 crp
= list_entry(nn
->reclaim_str_hashtbl
[i
].next
,
4554 struct nfs4_client_reclaim
, cr_strhash
);
4555 nfs4_remove_reclaim_record(crp
, nn
);
4558 WARN_ON_ONCE(nn
->reclaim_str_hashtbl_size
);
4562 * called from OPEN, CLAIM_PREVIOUS with a new clientid. */
4563 struct nfs4_client_reclaim
*
4564 nfsd4_find_reclaim_client(const char *recdir
, struct nfsd_net
*nn
)
4566 unsigned int strhashval
;
4567 struct nfs4_client_reclaim
*crp
= NULL
;
4569 dprintk("NFSD: nfs4_find_reclaim_client for recdir %s\n", recdir
);
4571 strhashval
= clientstr_hashval(recdir
);
4572 list_for_each_entry(crp
, &nn
->reclaim_str_hashtbl
[strhashval
], cr_strhash
) {
4573 if (same_name(crp
->cr_recdir
, recdir
)) {
4581 * Called from OPEN. Look for clientid in reclaim list.
4584 nfs4_check_open_reclaim(clientid_t
*clid
, bool sessions
, struct nfsd_net
*nn
)
4586 struct nfs4_client
*clp
;
4588 /* find clientid in conf_id_hashtbl */
4589 clp
= find_confirmed_client(clid
, sessions
, nn
);
4591 return nfserr_reclaim_bad
;
4593 return nfsd4_client_record_check(clp
) ? nfserr_reclaim_bad
: nfs_ok
;
4596 #ifdef CONFIG_NFSD_FAULT_INJECTION
4598 u64
nfsd_forget_client(struct nfs4_client
*clp
, u64 max
)
4604 u64
nfsd_print_client(struct nfs4_client
*clp
, u64 num
)
4606 char buf
[INET6_ADDRSTRLEN
];
4607 rpc_ntop((struct sockaddr
*)&clp
->cl_addr
, buf
, sizeof(buf
));
4608 printk(KERN_INFO
"NFS Client: %s\n", buf
);
4612 static void nfsd_print_count(struct nfs4_client
*clp
, unsigned int count
,
4615 char buf
[INET6_ADDRSTRLEN
];
4616 rpc_ntop((struct sockaddr
*)&clp
->cl_addr
, buf
, sizeof(buf
));
4617 printk(KERN_INFO
"NFS Client: %s has %u %s\n", buf
, count
, type
);
4620 static u64
nfsd_foreach_client_lock(struct nfs4_client
*clp
, u64 max
, void (*func
)(struct nfs4_lockowner
*))
4622 struct nfs4_openowner
*oop
;
4623 struct nfs4_lockowner
*lop
, *lo_next
;
4624 struct nfs4_ol_stateid
*stp
, *st_next
;
4627 list_for_each_entry(oop
, &clp
->cl_openowners
, oo_perclient
) {
4628 list_for_each_entry_safe(stp
, st_next
, &oop
->oo_owner
.so_stateids
, st_perstateowner
) {
4629 list_for_each_entry_safe(lop
, lo_next
, &stp
->st_lockowners
, lo_perstateid
) {
4641 u64
nfsd_forget_client_locks(struct nfs4_client
*clp
, u64 max
)
4643 return nfsd_foreach_client_lock(clp
, max
, release_lockowner
);
4646 u64
nfsd_print_client_locks(struct nfs4_client
*clp
, u64 max
)
4648 u64 count
= nfsd_foreach_client_lock(clp
, max
, NULL
);
4649 nfsd_print_count(clp
, count
, "locked files");
4653 static u64
nfsd_foreach_client_open(struct nfs4_client
*clp
, u64 max
, void (*func
)(struct nfs4_openowner
*))
4655 struct nfs4_openowner
*oop
, *next
;
4658 list_for_each_entry_safe(oop
, next
, &clp
->cl_openowners
, oo_perclient
) {
4668 u64
nfsd_forget_client_openowners(struct nfs4_client
*clp
, u64 max
)
4670 return nfsd_foreach_client_open(clp
, max
, release_openowner
);
4673 u64
nfsd_print_client_openowners(struct nfs4_client
*clp
, u64 max
)
4675 u64 count
= nfsd_foreach_client_open(clp
, max
, NULL
);
4676 nfsd_print_count(clp
, count
, "open files");
4680 static u64
nfsd_find_all_delegations(struct nfs4_client
*clp
, u64 max
,
4681 struct list_head
*victims
)
4683 struct nfs4_delegation
*dp
, *next
;
4686 list_for_each_entry_safe(dp
, next
, &clp
->cl_delegations
, dl_perclnt
) {
4688 list_move(&dp
->dl_recall_lru
, victims
);
4695 u64
nfsd_forget_client_delegations(struct nfs4_client
*clp
, u64 max
)
4697 struct nfs4_delegation
*dp
, *next
;
4701 spin_lock(&recall_lock
);
4702 count
= nfsd_find_all_delegations(clp
, max
, &victims
);
4703 spin_unlock(&recall_lock
);
4705 list_for_each_entry_safe(dp
, next
, &victims
, dl_recall_lru
)
4706 unhash_delegation(dp
);
4711 u64
nfsd_recall_client_delegations(struct nfs4_client
*clp
, u64 max
)
4713 struct nfs4_delegation
*dp
, *next
;
4717 spin_lock(&recall_lock
);
4718 count
= nfsd_find_all_delegations(clp
, max
, &victims
);
4719 list_for_each_entry_safe(dp
, next
, &victims
, dl_recall_lru
)
4720 nfsd_break_one_deleg(dp
);
4721 spin_unlock(&recall_lock
);
4726 u64
nfsd_print_client_delegations(struct nfs4_client
*clp
, u64 max
)
4730 spin_lock(&recall_lock
);
4731 count
= nfsd_find_all_delegations(clp
, max
, NULL
);
4732 spin_unlock(&recall_lock
);
4734 nfsd_print_count(clp
, count
, "delegations");
4738 u64
nfsd_for_n_state(u64 max
, u64 (*func
)(struct nfs4_client
*, u64
))
4740 struct nfs4_client
*clp
, *next
;
4742 struct nfsd_net
*nn
= net_generic(current
->nsproxy
->net_ns
, nfsd_net_id
);
4744 if (!nfsd_netns_ready(nn
))
4747 list_for_each_entry_safe(clp
, next
, &nn
->client_lru
, cl_lru
) {
4748 count
+= func(clp
, max
- count
);
4749 if ((max
!= 0) && (count
>= max
))
4756 struct nfs4_client
*nfsd_find_client(struct sockaddr_storage
*addr
, size_t addr_size
)
4758 struct nfs4_client
*clp
;
4759 struct nfsd_net
*nn
= net_generic(current
->nsproxy
->net_ns
, nfsd_net_id
);
4761 if (!nfsd_netns_ready(nn
))
4764 list_for_each_entry(clp
, &nn
->client_lru
, cl_lru
) {
4765 if (memcmp(&clp
->cl_addr
, addr
, addr_size
) == 0)
4771 #endif /* CONFIG_NFSD_FAULT_INJECTION */
4773 /* initialization to perform at module load time: */
4776 nfs4_state_init(void)
4780 for (i
= 0; i
< FILE_HASH_SIZE
; i
++) {
4781 INIT_LIST_HEAD(&file_hashtbl
[i
]);
4783 INIT_LIST_HEAD(&del_recall_lru
);
4787 * Since the lifetime of a delegation isn't limited to that of an open, a
4788 * client may quite reasonably hang on to a delegation as long as it has
4789 * the inode cached. This becomes an obvious problem the first time a
4790 * client's inode cache approaches the size of the server's total memory.
4792 * For now we avoid this problem by imposing a hard limit on the number
4793 * of delegations, which varies according to the server's memory size.
4796 set_max_delegations(void)
4799 * Allow at most 4 delegations per megabyte of RAM. Quick
4800 * estimates suggest that in the worst case (where every delegation
4801 * is for a different inode), a delegation could take about 1.5K,
4802 * giving a worst case usage of about 6% of memory.
4804 max_delegations
= nr_free_buffer_pages() >> (20 - 2 - PAGE_SHIFT
);
4807 static int nfs4_state_create_net(struct net
*net
)
4809 struct nfsd_net
*nn
= net_generic(net
, nfsd_net_id
);
4812 nn
->conf_id_hashtbl
= kmalloc(sizeof(struct list_head
) *
4813 CLIENT_HASH_SIZE
, GFP_KERNEL
);
4814 if (!nn
->conf_id_hashtbl
)
4816 nn
->unconf_id_hashtbl
= kmalloc(sizeof(struct list_head
) *
4817 CLIENT_HASH_SIZE
, GFP_KERNEL
);
4818 if (!nn
->unconf_id_hashtbl
)
4820 nn
->ownerstr_hashtbl
= kmalloc(sizeof(struct list_head
) *
4821 OWNER_HASH_SIZE
, GFP_KERNEL
);
4822 if (!nn
->ownerstr_hashtbl
)
4824 nn
->lockowner_ino_hashtbl
= kmalloc(sizeof(struct list_head
) *
4825 LOCKOWNER_INO_HASH_SIZE
, GFP_KERNEL
);
4826 if (!nn
->lockowner_ino_hashtbl
)
4827 goto err_lockowner_ino
;
4828 nn
->sessionid_hashtbl
= kmalloc(sizeof(struct list_head
) *
4829 SESSION_HASH_SIZE
, GFP_KERNEL
);
4830 if (!nn
->sessionid_hashtbl
)
4833 for (i
= 0; i
< CLIENT_HASH_SIZE
; i
++) {
4834 INIT_LIST_HEAD(&nn
->conf_id_hashtbl
[i
]);
4835 INIT_LIST_HEAD(&nn
->unconf_id_hashtbl
[i
]);
4837 for (i
= 0; i
< OWNER_HASH_SIZE
; i
++)
4838 INIT_LIST_HEAD(&nn
->ownerstr_hashtbl
[i
]);
4839 for (i
= 0; i
< LOCKOWNER_INO_HASH_SIZE
; i
++)
4840 INIT_LIST_HEAD(&nn
->lockowner_ino_hashtbl
[i
]);
4841 for (i
= 0; i
< SESSION_HASH_SIZE
; i
++)
4842 INIT_LIST_HEAD(&nn
->sessionid_hashtbl
[i
]);
4843 nn
->conf_name_tree
= RB_ROOT
;
4844 nn
->unconf_name_tree
= RB_ROOT
;
4845 INIT_LIST_HEAD(&nn
->client_lru
);
4846 INIT_LIST_HEAD(&nn
->close_lru
);
4847 spin_lock_init(&nn
->client_lock
);
4849 INIT_DELAYED_WORK(&nn
->laundromat_work
, laundromat_main
);
4855 kfree(nn
->lockowner_ino_hashtbl
);
4857 kfree(nn
->ownerstr_hashtbl
);
4859 kfree(nn
->unconf_id_hashtbl
);
4861 kfree(nn
->conf_id_hashtbl
);
4867 nfs4_state_destroy_net(struct net
*net
)
4870 struct nfs4_client
*clp
= NULL
;
4871 struct nfsd_net
*nn
= net_generic(net
, nfsd_net_id
);
4872 struct rb_node
*node
, *tmp
;
4874 for (i
= 0; i
< CLIENT_HASH_SIZE
; i
++) {
4875 while (!list_empty(&nn
->conf_id_hashtbl
[i
])) {
4876 clp
= list_entry(nn
->conf_id_hashtbl
[i
].next
, struct nfs4_client
, cl_idhash
);
4877 destroy_client(clp
);
4881 node
= rb_first(&nn
->unconf_name_tree
);
4882 while (node
!= NULL
) {
4884 node
= rb_next(tmp
);
4885 clp
= rb_entry(tmp
, struct nfs4_client
, cl_namenode
);
4886 rb_erase(tmp
, &nn
->unconf_name_tree
);
4887 destroy_client(clp
);
4890 kfree(nn
->sessionid_hashtbl
);
4891 kfree(nn
->lockowner_ino_hashtbl
);
4892 kfree(nn
->ownerstr_hashtbl
);
4893 kfree(nn
->unconf_id_hashtbl
);
4894 kfree(nn
->conf_id_hashtbl
);
4899 nfs4_state_start_net(struct net
*net
)
4901 struct nfsd_net
*nn
= net_generic(net
, nfsd_net_id
);
4904 ret
= nfs4_state_create_net(net
);
4907 nfsd4_client_tracking_init(net
);
4908 nn
->boot_time
= get_seconds();
4909 locks_start_grace(net
, &nn
->nfsd4_manager
);
4910 nn
->grace_ended
= false;
4911 printk(KERN_INFO
"NFSD: starting %ld-second grace period (net %p)\n",
4912 nn
->nfsd4_grace
, net
);
4913 queue_delayed_work(laundry_wq
, &nn
->laundromat_work
, nn
->nfsd4_grace
* HZ
);
4917 /* initialization to perform when the nfsd service is started: */
4920 nfs4_state_start(void)
4924 ret
= set_callback_cred();
4927 laundry_wq
= create_singlethread_workqueue("nfsd4");
4928 if (laundry_wq
== NULL
) {
4932 ret
= nfsd4_create_callback_queue();
4934 goto out_free_laundry
;
4936 set_max_delegations();
4941 destroy_workqueue(laundry_wq
);
4946 /* should be called with the state lock held */
4948 nfs4_state_shutdown_net(struct net
*net
)
4950 struct nfs4_delegation
*dp
= NULL
;
4951 struct list_head
*pos
, *next
, reaplist
;
4952 struct nfsd_net
*nn
= net_generic(net
, nfsd_net_id
);
4954 cancel_delayed_work_sync(&nn
->laundromat_work
);
4955 locks_end_grace(&nn
->nfsd4_manager
);
4957 INIT_LIST_HEAD(&reaplist
);
4958 spin_lock(&recall_lock
);
4959 list_for_each_safe(pos
, next
, &del_recall_lru
) {
4960 dp
= list_entry (pos
, struct nfs4_delegation
, dl_recall_lru
);
4961 if (dp
->dl_stid
.sc_client
->net
!= net
)
4963 list_move(&dp
->dl_recall_lru
, &reaplist
);
4965 spin_unlock(&recall_lock
);
4966 list_for_each_safe(pos
, next
, &reaplist
) {
4967 dp
= list_entry (pos
, struct nfs4_delegation
, dl_recall_lru
);
4968 unhash_delegation(dp
);
4971 nfsd4_client_tracking_exit(net
);
4972 nfs4_state_destroy_net(net
);
4976 nfs4_state_shutdown(void)
4978 destroy_workqueue(laundry_wq
);
4979 nfsd4_destroy_callback_queue();
4983 get_stateid(struct nfsd4_compound_state
*cstate
, stateid_t
*stateid
)
4985 if (HAS_STATE_ID(cstate
, CURRENT_STATE_ID_FLAG
) && CURRENT_STATEID(stateid
))
4986 memcpy(stateid
, &cstate
->current_stateid
, sizeof(stateid_t
));
4990 put_stateid(struct nfsd4_compound_state
*cstate
, stateid_t
*stateid
)
4992 if (cstate
->minorversion
) {
4993 memcpy(&cstate
->current_stateid
, stateid
, sizeof(stateid_t
));
4994 SET_STATE_ID(cstate
, CURRENT_STATE_ID_FLAG
);
4999 clear_current_stateid(struct nfsd4_compound_state
*cstate
)
5001 CLEAR_STATE_ID(cstate
, CURRENT_STATE_ID_FLAG
);
5005 * functions to set current state id
5008 nfsd4_set_opendowngradestateid(struct nfsd4_compound_state
*cstate
, struct nfsd4_open_downgrade
*odp
)
5010 put_stateid(cstate
, &odp
->od_stateid
);
5014 nfsd4_set_openstateid(struct nfsd4_compound_state
*cstate
, struct nfsd4_open
*open
)
5016 put_stateid(cstate
, &open
->op_stateid
);
5020 nfsd4_set_closestateid(struct nfsd4_compound_state
*cstate
, struct nfsd4_close
*close
)
5022 put_stateid(cstate
, &close
->cl_stateid
);
5026 nfsd4_set_lockstateid(struct nfsd4_compound_state
*cstate
, struct nfsd4_lock
*lock
)
5028 put_stateid(cstate
, &lock
->lk_resp_stateid
);
5032 * functions to consume current state id
5036 nfsd4_get_opendowngradestateid(struct nfsd4_compound_state
*cstate
, struct nfsd4_open_downgrade
*odp
)
5038 get_stateid(cstate
, &odp
->od_stateid
);
5042 nfsd4_get_delegreturnstateid(struct nfsd4_compound_state
*cstate
, struct nfsd4_delegreturn
*drp
)
5044 get_stateid(cstate
, &drp
->dr_stateid
);
5048 nfsd4_get_freestateid(struct nfsd4_compound_state
*cstate
, struct nfsd4_free_stateid
*fsp
)
5050 get_stateid(cstate
, &fsp
->fr_stateid
);
5054 nfsd4_get_setattrstateid(struct nfsd4_compound_state
*cstate
, struct nfsd4_setattr
*setattr
)
5056 get_stateid(cstate
, &setattr
->sa_stateid
);
5060 nfsd4_get_closestateid(struct nfsd4_compound_state
*cstate
, struct nfsd4_close
*close
)
5062 get_stateid(cstate
, &close
->cl_stateid
);
5066 nfsd4_get_lockustateid(struct nfsd4_compound_state
*cstate
, struct nfsd4_locku
*locku
)
5068 get_stateid(cstate
, &locku
->lu_stateid
);
5072 nfsd4_get_readstateid(struct nfsd4_compound_state
*cstate
, struct nfsd4_read
*read
)
5074 get_stateid(cstate
, &read
->rd_stateid
);
5078 nfsd4_get_writestateid(struct nfsd4_compound_state
*cstate
, struct nfsd4_write
*write
)
5080 get_stateid(cstate
, &write
->wr_stateid
);