2 * Copyright (c) 2001 The Regents of the University of Michigan.
5 * Kendrick Smith <kmsmith@umich.edu>
6 * Andy Adamson <kandros@umich.edu>
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the distribution.
17 * 3. Neither the name of the University nor the names of its
18 * contributors may be used to endorse or promote products derived
19 * from this software without specific prior written permission.
21 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED
22 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
23 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
24 * DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
26 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
27 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
28 * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
29 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
30 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
31 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
35 #include <linux/file.h>
37 #include <linux/slab.h>
38 #include <linux/namei.h>
39 #include <linux/swap.h>
40 #include <linux/pagemap.h>
41 #include <linux/ratelimit.h>
42 #include <linux/sunrpc/svcauth_gss.h>
43 #include <linux/sunrpc/addr.h>
44 #include <linux/jhash.h>
48 #include "current_stateid.h"
53 #define NFSDDBG_FACILITY NFSDDBG_PROC
55 #define all_ones {{~0,~0},~0}
56 static const stateid_t one_stateid
= {
58 .si_opaque
= all_ones
,
60 static const stateid_t zero_stateid
= {
63 static const stateid_t currentstateid
= {
67 static u64 current_sessionid
= 1;
69 #define ZERO_STATEID(stateid) (!memcmp((stateid), &zero_stateid, sizeof(stateid_t)))
70 #define ONE_STATEID(stateid) (!memcmp((stateid), &one_stateid, sizeof(stateid_t)))
71 #define CURRENT_STATEID(stateid) (!memcmp((stateid), ¤tstateid, sizeof(stateid_t)))
73 /* forward declarations */
74 static bool check_for_locks(struct nfs4_file
*fp
, struct nfs4_lockowner
*lowner
);
75 static void nfs4_free_ol_stateid(struct nfs4_stid
*stid
);
80 * Currently used for the del_recall_lru and file hash table. In an
81 * effort to decrease the scope of the client_mutex, this spinlock may
82 * eventually cover more:
84 static DEFINE_SPINLOCK(state_lock
);
87 * A waitqueue for all in-progress 4.0 CLOSE operations that are waiting for
88 * the refcount on the open stateid to drop.
90 static DECLARE_WAIT_QUEUE_HEAD(close_wq
);
92 static struct kmem_cache
*openowner_slab
;
93 static struct kmem_cache
*lockowner_slab
;
94 static struct kmem_cache
*file_slab
;
95 static struct kmem_cache
*stateid_slab
;
96 static struct kmem_cache
*deleg_slab
;
97 static struct kmem_cache
*odstate_slab
;
99 static void free_session(struct nfsd4_session
*);
101 static struct nfsd4_callback_ops nfsd4_cb_recall_ops
;
103 static bool is_session_dead(struct nfsd4_session
*ses
)
105 return ses
->se_flags
& NFS4_SESSION_DEAD
;
108 static __be32
mark_session_dead_locked(struct nfsd4_session
*ses
, int ref_held_by_me
)
110 if (atomic_read(&ses
->se_ref
) > ref_held_by_me
)
111 return nfserr_jukebox
;
112 ses
->se_flags
|= NFS4_SESSION_DEAD
;
116 static bool is_client_expired(struct nfs4_client
*clp
)
118 return clp
->cl_time
== 0;
121 static __be32
get_client_locked(struct nfs4_client
*clp
)
123 struct nfsd_net
*nn
= net_generic(clp
->net
, nfsd_net_id
);
125 lockdep_assert_held(&nn
->client_lock
);
127 if (is_client_expired(clp
))
128 return nfserr_expired
;
129 atomic_inc(&clp
->cl_refcount
);
133 /* must be called under the client_lock */
135 renew_client_locked(struct nfs4_client
*clp
)
137 struct nfsd_net
*nn
= net_generic(clp
->net
, nfsd_net_id
);
139 if (is_client_expired(clp
)) {
141 printk("%s: client (clientid %08x/%08x) already expired\n",
143 clp
->cl_clientid
.cl_boot
,
144 clp
->cl_clientid
.cl_id
);
148 dprintk("renewing client (clientid %08x/%08x)\n",
149 clp
->cl_clientid
.cl_boot
,
150 clp
->cl_clientid
.cl_id
);
151 list_move_tail(&clp
->cl_lru
, &nn
->client_lru
);
152 clp
->cl_time
= get_seconds();
155 static void put_client_renew_locked(struct nfs4_client
*clp
)
157 struct nfsd_net
*nn
= net_generic(clp
->net
, nfsd_net_id
);
159 lockdep_assert_held(&nn
->client_lock
);
161 if (!atomic_dec_and_test(&clp
->cl_refcount
))
163 if (!is_client_expired(clp
))
164 renew_client_locked(clp
);
167 static void put_client_renew(struct nfs4_client
*clp
)
169 struct nfsd_net
*nn
= net_generic(clp
->net
, nfsd_net_id
);
171 if (!atomic_dec_and_lock(&clp
->cl_refcount
, &nn
->client_lock
))
173 if (!is_client_expired(clp
))
174 renew_client_locked(clp
);
175 spin_unlock(&nn
->client_lock
);
178 static __be32
nfsd4_get_session_locked(struct nfsd4_session
*ses
)
182 if (is_session_dead(ses
))
183 return nfserr_badsession
;
184 status
= get_client_locked(ses
->se_client
);
187 atomic_inc(&ses
->se_ref
);
191 static void nfsd4_put_session_locked(struct nfsd4_session
*ses
)
193 struct nfs4_client
*clp
= ses
->se_client
;
194 struct nfsd_net
*nn
= net_generic(clp
->net
, nfsd_net_id
);
196 lockdep_assert_held(&nn
->client_lock
);
198 if (atomic_dec_and_test(&ses
->se_ref
) && is_session_dead(ses
))
200 put_client_renew_locked(clp
);
203 static void nfsd4_put_session(struct nfsd4_session
*ses
)
205 struct nfs4_client
*clp
= ses
->se_client
;
206 struct nfsd_net
*nn
= net_generic(clp
->net
, nfsd_net_id
);
208 spin_lock(&nn
->client_lock
);
209 nfsd4_put_session_locked(ses
);
210 spin_unlock(&nn
->client_lock
);
213 static inline struct nfs4_stateowner
*
214 nfs4_get_stateowner(struct nfs4_stateowner
*sop
)
216 atomic_inc(&sop
->so_count
);
221 same_owner_str(struct nfs4_stateowner
*sop
, struct xdr_netobj
*owner
)
223 return (sop
->so_owner
.len
== owner
->len
) &&
224 0 == memcmp(sop
->so_owner
.data
, owner
->data
, owner
->len
);
227 static struct nfs4_openowner
*
228 find_openstateowner_str_locked(unsigned int hashval
, struct nfsd4_open
*open
,
229 struct nfs4_client
*clp
)
231 struct nfs4_stateowner
*so
;
233 lockdep_assert_held(&clp
->cl_lock
);
235 list_for_each_entry(so
, &clp
->cl_ownerstr_hashtbl
[hashval
],
237 if (!so
->so_is_open_owner
)
239 if (same_owner_str(so
, &open
->op_owner
))
240 return openowner(nfs4_get_stateowner(so
));
245 static struct nfs4_openowner
*
246 find_openstateowner_str(unsigned int hashval
, struct nfsd4_open
*open
,
247 struct nfs4_client
*clp
)
249 struct nfs4_openowner
*oo
;
251 spin_lock(&clp
->cl_lock
);
252 oo
= find_openstateowner_str_locked(hashval
, open
, clp
);
253 spin_unlock(&clp
->cl_lock
);
258 opaque_hashval(const void *ptr
, int nbytes
)
260 unsigned char *cptr
= (unsigned char *) ptr
;
270 static void nfsd4_free_file_rcu(struct rcu_head
*rcu
)
272 struct nfs4_file
*fp
= container_of(rcu
, struct nfs4_file
, fi_rcu
);
274 kmem_cache_free(file_slab
, fp
);
278 put_nfs4_file(struct nfs4_file
*fi
)
280 might_lock(&state_lock
);
282 if (atomic_dec_and_lock(&fi
->fi_ref
, &state_lock
)) {
283 hlist_del_rcu(&fi
->fi_hash
);
284 spin_unlock(&state_lock
);
285 WARN_ON_ONCE(!list_empty(&fi
->fi_clnt_odstate
));
286 WARN_ON_ONCE(!list_empty(&fi
->fi_delegations
));
287 call_rcu(&fi
->fi_rcu
, nfsd4_free_file_rcu
);
292 __nfs4_get_fd(struct nfs4_file
*f
, int oflag
)
294 if (f
->fi_fds
[oflag
])
295 return get_file(f
->fi_fds
[oflag
]);
300 find_writeable_file_locked(struct nfs4_file
*f
)
304 lockdep_assert_held(&f
->fi_lock
);
306 ret
= __nfs4_get_fd(f
, O_WRONLY
);
308 ret
= __nfs4_get_fd(f
, O_RDWR
);
313 find_writeable_file(struct nfs4_file
*f
)
317 spin_lock(&f
->fi_lock
);
318 ret
= find_writeable_file_locked(f
);
319 spin_unlock(&f
->fi_lock
);
324 static struct file
*find_readable_file_locked(struct nfs4_file
*f
)
328 lockdep_assert_held(&f
->fi_lock
);
330 ret
= __nfs4_get_fd(f
, O_RDONLY
);
332 ret
= __nfs4_get_fd(f
, O_RDWR
);
337 find_readable_file(struct nfs4_file
*f
)
341 spin_lock(&f
->fi_lock
);
342 ret
= find_readable_file_locked(f
);
343 spin_unlock(&f
->fi_lock
);
349 find_any_file(struct nfs4_file
*f
)
353 spin_lock(&f
->fi_lock
);
354 ret
= __nfs4_get_fd(f
, O_RDWR
);
356 ret
= __nfs4_get_fd(f
, O_WRONLY
);
358 ret
= __nfs4_get_fd(f
, O_RDONLY
);
360 spin_unlock(&f
->fi_lock
);
364 static atomic_long_t num_delegations
;
365 unsigned long max_delegations
;
368 * Open owner state (share locks)
371 /* hash tables for lock and open owners */
372 #define OWNER_HASH_BITS 8
373 #define OWNER_HASH_SIZE (1 << OWNER_HASH_BITS)
374 #define OWNER_HASH_MASK (OWNER_HASH_SIZE - 1)
376 static unsigned int ownerstr_hashval(struct xdr_netobj
*ownername
)
380 ret
= opaque_hashval(ownername
->data
, ownername
->len
);
381 return ret
& OWNER_HASH_MASK
;
384 /* hash table for nfs4_file */
385 #define FILE_HASH_BITS 8
386 #define FILE_HASH_SIZE (1 << FILE_HASH_BITS)
388 static unsigned int nfsd_fh_hashval(struct knfsd_fh
*fh
)
390 return jhash2(fh
->fh_base
.fh_pad
, XDR_QUADLEN(fh
->fh_size
), 0);
393 static unsigned int file_hashval(struct knfsd_fh
*fh
)
395 return nfsd_fh_hashval(fh
) & (FILE_HASH_SIZE
- 1);
398 static struct hlist_head file_hashtbl
[FILE_HASH_SIZE
];
401 __nfs4_file_get_access(struct nfs4_file
*fp
, u32 access
)
403 lockdep_assert_held(&fp
->fi_lock
);
405 if (access
& NFS4_SHARE_ACCESS_WRITE
)
406 atomic_inc(&fp
->fi_access
[O_WRONLY
]);
407 if (access
& NFS4_SHARE_ACCESS_READ
)
408 atomic_inc(&fp
->fi_access
[O_RDONLY
]);
412 nfs4_file_get_access(struct nfs4_file
*fp
, u32 access
)
414 lockdep_assert_held(&fp
->fi_lock
);
416 /* Does this access mode make sense? */
417 if (access
& ~NFS4_SHARE_ACCESS_BOTH
)
420 /* Does it conflict with a deny mode already set? */
421 if ((access
& fp
->fi_share_deny
) != 0)
422 return nfserr_share_denied
;
424 __nfs4_file_get_access(fp
, access
);
428 static __be32
nfs4_file_check_deny(struct nfs4_file
*fp
, u32 deny
)
430 /* Common case is that there is no deny mode. */
432 /* Does this deny mode make sense? */
433 if (deny
& ~NFS4_SHARE_DENY_BOTH
)
436 if ((deny
& NFS4_SHARE_DENY_READ
) &&
437 atomic_read(&fp
->fi_access
[O_RDONLY
]))
438 return nfserr_share_denied
;
440 if ((deny
& NFS4_SHARE_DENY_WRITE
) &&
441 atomic_read(&fp
->fi_access
[O_WRONLY
]))
442 return nfserr_share_denied
;
447 static void __nfs4_file_put_access(struct nfs4_file
*fp
, int oflag
)
449 might_lock(&fp
->fi_lock
);
451 if (atomic_dec_and_lock(&fp
->fi_access
[oflag
], &fp
->fi_lock
)) {
452 struct file
*f1
= NULL
;
453 struct file
*f2
= NULL
;
455 swap(f1
, fp
->fi_fds
[oflag
]);
456 if (atomic_read(&fp
->fi_access
[1 - oflag
]) == 0)
457 swap(f2
, fp
->fi_fds
[O_RDWR
]);
458 spin_unlock(&fp
->fi_lock
);
466 static void nfs4_file_put_access(struct nfs4_file
*fp
, u32 access
)
468 WARN_ON_ONCE(access
& ~NFS4_SHARE_ACCESS_BOTH
);
470 if (access
& NFS4_SHARE_ACCESS_WRITE
)
471 __nfs4_file_put_access(fp
, O_WRONLY
);
472 if (access
& NFS4_SHARE_ACCESS_READ
)
473 __nfs4_file_put_access(fp
, O_RDONLY
);
477 * Allocate a new open/delegation state counter. This is needed for
478 * pNFS for proper return on close semantics.
480 * Note that we only allocate it for pNFS-enabled exports, otherwise
481 * all pointers to struct nfs4_clnt_odstate are always NULL.
483 static struct nfs4_clnt_odstate
*
484 alloc_clnt_odstate(struct nfs4_client
*clp
)
486 struct nfs4_clnt_odstate
*co
;
488 co
= kmem_cache_zalloc(odstate_slab
, GFP_KERNEL
);
491 atomic_set(&co
->co_odcount
, 1);
497 hash_clnt_odstate_locked(struct nfs4_clnt_odstate
*co
)
499 struct nfs4_file
*fp
= co
->co_file
;
501 lockdep_assert_held(&fp
->fi_lock
);
502 list_add(&co
->co_perfile
, &fp
->fi_clnt_odstate
);
506 get_clnt_odstate(struct nfs4_clnt_odstate
*co
)
509 atomic_inc(&co
->co_odcount
);
513 put_clnt_odstate(struct nfs4_clnt_odstate
*co
)
515 struct nfs4_file
*fp
;
521 if (atomic_dec_and_lock(&co
->co_odcount
, &fp
->fi_lock
)) {
522 list_del(&co
->co_perfile
);
523 spin_unlock(&fp
->fi_lock
);
525 nfsd4_return_all_file_layouts(co
->co_client
, fp
);
526 kmem_cache_free(odstate_slab
, co
);
530 static struct nfs4_clnt_odstate
*
531 find_or_hash_clnt_odstate(struct nfs4_file
*fp
, struct nfs4_clnt_odstate
*new)
533 struct nfs4_clnt_odstate
*co
;
534 struct nfs4_client
*cl
;
541 spin_lock(&fp
->fi_lock
);
542 list_for_each_entry(co
, &fp
->fi_clnt_odstate
, co_perfile
) {
543 if (co
->co_client
== cl
) {
544 get_clnt_odstate(co
);
550 hash_clnt_odstate_locked(new);
552 spin_unlock(&fp
->fi_lock
);
556 struct nfs4_stid
*nfs4_alloc_stid(struct nfs4_client
*cl
,
557 struct kmem_cache
*slab
)
559 struct nfs4_stid
*stid
;
562 stid
= kmem_cache_zalloc(slab
, GFP_KERNEL
);
566 idr_preload(GFP_KERNEL
);
567 spin_lock(&cl
->cl_lock
);
568 new_id
= idr_alloc_cyclic(&cl
->cl_stateids
, stid
, 0, 0, GFP_NOWAIT
);
569 spin_unlock(&cl
->cl_lock
);
573 stid
->sc_client
= cl
;
574 stid
->sc_stateid
.si_opaque
.so_id
= new_id
;
575 stid
->sc_stateid
.si_opaque
.so_clid
= cl
->cl_clientid
;
576 /* Will be incremented before return to client: */
577 atomic_set(&stid
->sc_count
, 1);
580 * It shouldn't be a problem to reuse an opaque stateid value.
581 * I don't think it is for 4.1. But with 4.0 I worry that, for
582 * example, a stray write retransmission could be accepted by
583 * the server when it should have been rejected. Therefore,
584 * adopt a trick from the sctp code to attempt to maximize the
585 * amount of time until an id is reused, by ensuring they always
586 * "increase" (mod INT_MAX):
590 kmem_cache_free(slab
, stid
);
594 static struct nfs4_ol_stateid
* nfs4_alloc_open_stateid(struct nfs4_client
*clp
)
596 struct nfs4_stid
*stid
;
597 struct nfs4_ol_stateid
*stp
;
599 stid
= nfs4_alloc_stid(clp
, stateid_slab
);
603 stp
= openlockstateid(stid
);
604 stp
->st_stid
.sc_free
= nfs4_free_ol_stateid
;
608 static void nfs4_free_deleg(struct nfs4_stid
*stid
)
610 kmem_cache_free(deleg_slab
, stid
);
611 atomic_long_dec(&num_delegations
);
615 * When we recall a delegation, we should be careful not to hand it
616 * out again straight away.
617 * To ensure this we keep a pair of bloom filters ('new' and 'old')
618 * in which the filehandles of recalled delegations are "stored".
619 * If a filehandle appear in either filter, a delegation is blocked.
620 * When a delegation is recalled, the filehandle is stored in the "new"
622 * Every 30 seconds we swap the filters and clear the "new" one,
623 * unless both are empty of course.
625 * Each filter is 256 bits. We hash the filehandle to 32bit and use the
626 * low 3 bytes as hash-table indices.
628 * 'blocked_delegations_lock', which is always taken in block_delegations(),
629 * is used to manage concurrent access. Testing does not need the lock
630 * except when swapping the two filters.
632 static DEFINE_SPINLOCK(blocked_delegations_lock
);
633 static struct bloom_pair
{
634 int entries
, old_entries
;
636 int new; /* index into 'set' */
637 DECLARE_BITMAP(set
[2], 256);
638 } blocked_delegations
;
640 static int delegation_blocked(struct knfsd_fh
*fh
)
643 struct bloom_pair
*bd
= &blocked_delegations
;
645 if (bd
->entries
== 0)
647 if (seconds_since_boot() - bd
->swap_time
> 30) {
648 spin_lock(&blocked_delegations_lock
);
649 if (seconds_since_boot() - bd
->swap_time
> 30) {
650 bd
->entries
-= bd
->old_entries
;
651 bd
->old_entries
= bd
->entries
;
652 memset(bd
->set
[bd
->new], 0,
655 bd
->swap_time
= seconds_since_boot();
657 spin_unlock(&blocked_delegations_lock
);
659 hash
= jhash(&fh
->fh_base
, fh
->fh_size
, 0);
660 if (test_bit(hash
&255, bd
->set
[0]) &&
661 test_bit((hash
>>8)&255, bd
->set
[0]) &&
662 test_bit((hash
>>16)&255, bd
->set
[0]))
665 if (test_bit(hash
&255, bd
->set
[1]) &&
666 test_bit((hash
>>8)&255, bd
->set
[1]) &&
667 test_bit((hash
>>16)&255, bd
->set
[1]))
673 static void block_delegations(struct knfsd_fh
*fh
)
676 struct bloom_pair
*bd
= &blocked_delegations
;
678 hash
= jhash(&fh
->fh_base
, fh
->fh_size
, 0);
680 spin_lock(&blocked_delegations_lock
);
681 __set_bit(hash
&255, bd
->set
[bd
->new]);
682 __set_bit((hash
>>8)&255, bd
->set
[bd
->new]);
683 __set_bit((hash
>>16)&255, bd
->set
[bd
->new]);
684 if (bd
->entries
== 0)
685 bd
->swap_time
= seconds_since_boot();
687 spin_unlock(&blocked_delegations_lock
);
690 static struct nfs4_delegation
*
691 alloc_init_deleg(struct nfs4_client
*clp
, struct svc_fh
*current_fh
,
692 struct nfs4_clnt_odstate
*odstate
)
694 struct nfs4_delegation
*dp
;
697 dprintk("NFSD alloc_init_deleg\n");
698 n
= atomic_long_inc_return(&num_delegations
);
699 if (n
< 0 || n
> max_delegations
)
701 if (delegation_blocked(¤t_fh
->fh_handle
))
703 dp
= delegstateid(nfs4_alloc_stid(clp
, deleg_slab
));
707 dp
->dl_stid
.sc_free
= nfs4_free_deleg
;
709 * delegation seqid's are never incremented. The 4.1 special
710 * meaning of seqid 0 isn't meaningful, really, but let's avoid
711 * 0 anyway just for consistency and use 1:
713 dp
->dl_stid
.sc_stateid
.si_generation
= 1;
714 INIT_LIST_HEAD(&dp
->dl_perfile
);
715 INIT_LIST_HEAD(&dp
->dl_perclnt
);
716 INIT_LIST_HEAD(&dp
->dl_recall_lru
);
717 dp
->dl_clnt_odstate
= odstate
;
718 get_clnt_odstate(odstate
);
719 dp
->dl_type
= NFS4_OPEN_DELEGATE_READ
;
721 nfsd4_init_cb(&dp
->dl_recall
, dp
->dl_stid
.sc_client
,
722 &nfsd4_cb_recall_ops
, NFSPROC4_CLNT_CB_RECALL
);
725 atomic_long_dec(&num_delegations
);
730 nfs4_put_stid(struct nfs4_stid
*s
)
732 struct nfs4_file
*fp
= s
->sc_file
;
733 struct nfs4_client
*clp
= s
->sc_client
;
735 might_lock(&clp
->cl_lock
);
737 if (!atomic_dec_and_lock(&s
->sc_count
, &clp
->cl_lock
)) {
738 wake_up_all(&close_wq
);
741 idr_remove(&clp
->cl_stateids
, s
->sc_stateid
.si_opaque
.so_id
);
742 spin_unlock(&clp
->cl_lock
);
748 static void nfs4_put_deleg_lease(struct nfs4_file
*fp
)
750 struct file
*filp
= NULL
;
752 spin_lock(&fp
->fi_lock
);
753 if (fp
->fi_deleg_file
&& --fp
->fi_delegees
== 0)
754 swap(filp
, fp
->fi_deleg_file
);
755 spin_unlock(&fp
->fi_lock
);
758 vfs_setlease(filp
, F_UNLCK
, NULL
, (void **)&fp
);
763 void nfs4_unhash_stid(struct nfs4_stid
*s
)
769 hash_delegation_locked(struct nfs4_delegation
*dp
, struct nfs4_file
*fp
)
771 lockdep_assert_held(&state_lock
);
772 lockdep_assert_held(&fp
->fi_lock
);
774 atomic_inc(&dp
->dl_stid
.sc_count
);
775 dp
->dl_stid
.sc_type
= NFS4_DELEG_STID
;
776 list_add(&dp
->dl_perfile
, &fp
->fi_delegations
);
777 list_add(&dp
->dl_perclnt
, &dp
->dl_stid
.sc_client
->cl_delegations
);
781 unhash_delegation_locked(struct nfs4_delegation
*dp
)
783 struct nfs4_file
*fp
= dp
->dl_stid
.sc_file
;
785 lockdep_assert_held(&state_lock
);
787 if (list_empty(&dp
->dl_perfile
))
790 dp
->dl_stid
.sc_type
= NFS4_CLOSED_DELEG_STID
;
791 /* Ensure that deleg break won't try to requeue it */
793 spin_lock(&fp
->fi_lock
);
794 list_del_init(&dp
->dl_perclnt
);
795 list_del_init(&dp
->dl_recall_lru
);
796 list_del_init(&dp
->dl_perfile
);
797 spin_unlock(&fp
->fi_lock
);
801 static void destroy_delegation(struct nfs4_delegation
*dp
)
805 spin_lock(&state_lock
);
806 unhashed
= unhash_delegation_locked(dp
);
807 spin_unlock(&state_lock
);
809 put_clnt_odstate(dp
->dl_clnt_odstate
);
810 nfs4_put_deleg_lease(dp
->dl_stid
.sc_file
);
811 nfs4_put_stid(&dp
->dl_stid
);
815 static void revoke_delegation(struct nfs4_delegation
*dp
)
817 struct nfs4_client
*clp
= dp
->dl_stid
.sc_client
;
819 WARN_ON(!list_empty(&dp
->dl_recall_lru
));
821 put_clnt_odstate(dp
->dl_clnt_odstate
);
822 nfs4_put_deleg_lease(dp
->dl_stid
.sc_file
);
824 if (clp
->cl_minorversion
== 0)
825 nfs4_put_stid(&dp
->dl_stid
);
827 dp
->dl_stid
.sc_type
= NFS4_REVOKED_DELEG_STID
;
828 spin_lock(&clp
->cl_lock
);
829 list_add(&dp
->dl_recall_lru
, &clp
->cl_revoked
);
830 spin_unlock(&clp
->cl_lock
);
838 static unsigned int clientid_hashval(u32 id
)
840 return id
& CLIENT_HASH_MASK
;
843 static unsigned int clientstr_hashval(const char *name
)
845 return opaque_hashval(name
, 8) & CLIENT_HASH_MASK
;
849 * We store the NONE, READ, WRITE, and BOTH bits separately in the
850 * st_{access,deny}_bmap field of the stateid, in order to track not
851 * only what share bits are currently in force, but also what
852 * combinations of share bits previous opens have used. This allows us
853 * to enforce the recommendation of rfc 3530 14.2.19 that the server
854 * return an error if the client attempt to downgrade to a combination
855 * of share bits not explicable by closing some of its previous opens.
857 * XXX: This enforcement is actually incomplete, since we don't keep
858 * track of access/deny bit combinations; so, e.g., we allow:
860 * OPEN allow read, deny write
861 * OPEN allow both, deny none
862 * DOWNGRADE allow read, deny none
864 * which we should reject.
867 bmap_to_share_mode(unsigned long bmap
) {
869 unsigned int access
= 0;
871 for (i
= 1; i
< 4; i
++) {
872 if (test_bit(i
, &bmap
))
878 /* set share access for a given stateid */
880 set_access(u32 access
, struct nfs4_ol_stateid
*stp
)
882 unsigned char mask
= 1 << access
;
884 WARN_ON_ONCE(access
> NFS4_SHARE_ACCESS_BOTH
);
885 stp
->st_access_bmap
|= mask
;
888 /* clear share access for a given stateid */
890 clear_access(u32 access
, struct nfs4_ol_stateid
*stp
)
892 unsigned char mask
= 1 << access
;
894 WARN_ON_ONCE(access
> NFS4_SHARE_ACCESS_BOTH
);
895 stp
->st_access_bmap
&= ~mask
;
898 /* test whether a given stateid has access */
900 test_access(u32 access
, struct nfs4_ol_stateid
*stp
)
902 unsigned char mask
= 1 << access
;
904 return (bool)(stp
->st_access_bmap
& mask
);
907 /* set share deny for a given stateid */
909 set_deny(u32 deny
, struct nfs4_ol_stateid
*stp
)
911 unsigned char mask
= 1 << deny
;
913 WARN_ON_ONCE(deny
> NFS4_SHARE_DENY_BOTH
);
914 stp
->st_deny_bmap
|= mask
;
917 /* clear share deny for a given stateid */
919 clear_deny(u32 deny
, struct nfs4_ol_stateid
*stp
)
921 unsigned char mask
= 1 << deny
;
923 WARN_ON_ONCE(deny
> NFS4_SHARE_DENY_BOTH
);
924 stp
->st_deny_bmap
&= ~mask
;
927 /* test whether a given stateid is denying specific access */
929 test_deny(u32 deny
, struct nfs4_ol_stateid
*stp
)
931 unsigned char mask
= 1 << deny
;
933 return (bool)(stp
->st_deny_bmap
& mask
);
936 static int nfs4_access_to_omode(u32 access
)
938 switch (access
& NFS4_SHARE_ACCESS_BOTH
) {
939 case NFS4_SHARE_ACCESS_READ
:
941 case NFS4_SHARE_ACCESS_WRITE
:
943 case NFS4_SHARE_ACCESS_BOTH
:
951 * A stateid that had a deny mode associated with it is being released
952 * or downgraded. Recalculate the deny mode on the file.
955 recalculate_deny_mode(struct nfs4_file
*fp
)
957 struct nfs4_ol_stateid
*stp
;
959 spin_lock(&fp
->fi_lock
);
960 fp
->fi_share_deny
= 0;
961 list_for_each_entry(stp
, &fp
->fi_stateids
, st_perfile
)
962 fp
->fi_share_deny
|= bmap_to_share_mode(stp
->st_deny_bmap
);
963 spin_unlock(&fp
->fi_lock
);
967 reset_union_bmap_deny(u32 deny
, struct nfs4_ol_stateid
*stp
)
972 for (i
= 1; i
< 4; i
++) {
973 if ((i
& deny
) != i
) {
979 /* Recalculate per-file deny mode if there was a change */
981 recalculate_deny_mode(stp
->st_stid
.sc_file
);
984 /* release all access and file references for a given stateid */
986 release_all_access(struct nfs4_ol_stateid
*stp
)
989 struct nfs4_file
*fp
= stp
->st_stid
.sc_file
;
991 if (fp
&& stp
->st_deny_bmap
!= 0)
992 recalculate_deny_mode(fp
);
994 for (i
= 1; i
< 4; i
++) {
995 if (test_access(i
, stp
))
996 nfs4_file_put_access(stp
->st_stid
.sc_file
, i
);
997 clear_access(i
, stp
);
1001 static inline void nfs4_free_stateowner(struct nfs4_stateowner
*sop
)
1003 kfree(sop
->so_owner
.data
);
1004 sop
->so_ops
->so_free(sop
);
1007 static void nfs4_put_stateowner(struct nfs4_stateowner
*sop
)
1009 struct nfs4_client
*clp
= sop
->so_client
;
1011 might_lock(&clp
->cl_lock
);
1013 if (!atomic_dec_and_lock(&sop
->so_count
, &clp
->cl_lock
))
1015 sop
->so_ops
->so_unhash(sop
);
1016 spin_unlock(&clp
->cl_lock
);
1017 nfs4_free_stateowner(sop
);
1020 static bool unhash_ol_stateid(struct nfs4_ol_stateid
*stp
)
1022 struct nfs4_file
*fp
= stp
->st_stid
.sc_file
;
1024 lockdep_assert_held(&stp
->st_stateowner
->so_client
->cl_lock
);
1026 if (list_empty(&stp
->st_perfile
))
1029 spin_lock(&fp
->fi_lock
);
1030 list_del_init(&stp
->st_perfile
);
1031 spin_unlock(&fp
->fi_lock
);
1032 list_del(&stp
->st_perstateowner
);
1036 static void nfs4_free_ol_stateid(struct nfs4_stid
*stid
)
1038 struct nfs4_ol_stateid
*stp
= openlockstateid(stid
);
1040 put_clnt_odstate(stp
->st_clnt_odstate
);
1041 release_all_access(stp
);
1042 if (stp
->st_stateowner
)
1043 nfs4_put_stateowner(stp
->st_stateowner
);
1044 kmem_cache_free(stateid_slab
, stid
);
1047 static void nfs4_free_lock_stateid(struct nfs4_stid
*stid
)
1049 struct nfs4_ol_stateid
*stp
= openlockstateid(stid
);
1050 struct nfs4_lockowner
*lo
= lockowner(stp
->st_stateowner
);
1053 file
= find_any_file(stp
->st_stid
.sc_file
);
1055 filp_close(file
, (fl_owner_t
)lo
);
1056 nfs4_free_ol_stateid(stid
);
1060 * Put the persistent reference to an already unhashed generic stateid, while
1061 * holding the cl_lock. If it's the last reference, then put it onto the
1062 * reaplist for later destruction.
1064 static void put_ol_stateid_locked(struct nfs4_ol_stateid
*stp
,
1065 struct list_head
*reaplist
)
1067 struct nfs4_stid
*s
= &stp
->st_stid
;
1068 struct nfs4_client
*clp
= s
->sc_client
;
1070 lockdep_assert_held(&clp
->cl_lock
);
1072 WARN_ON_ONCE(!list_empty(&stp
->st_locks
));
1074 if (!atomic_dec_and_test(&s
->sc_count
)) {
1075 wake_up_all(&close_wq
);
1079 idr_remove(&clp
->cl_stateids
, s
->sc_stateid
.si_opaque
.so_id
);
1080 list_add(&stp
->st_locks
, reaplist
);
1083 static bool unhash_lock_stateid(struct nfs4_ol_stateid
*stp
)
1085 struct nfs4_openowner
*oo
= openowner(stp
->st_openstp
->st_stateowner
);
1087 lockdep_assert_held(&oo
->oo_owner
.so_client
->cl_lock
);
1089 list_del_init(&stp
->st_locks
);
1090 nfs4_unhash_stid(&stp
->st_stid
);
1091 return unhash_ol_stateid(stp
);
1094 static void release_lock_stateid(struct nfs4_ol_stateid
*stp
)
1096 struct nfs4_openowner
*oo
= openowner(stp
->st_openstp
->st_stateowner
);
1099 spin_lock(&oo
->oo_owner
.so_client
->cl_lock
);
1100 unhashed
= unhash_lock_stateid(stp
);
1101 spin_unlock(&oo
->oo_owner
.so_client
->cl_lock
);
1103 nfs4_put_stid(&stp
->st_stid
);
1106 static void unhash_lockowner_locked(struct nfs4_lockowner
*lo
)
1108 struct nfs4_client
*clp
= lo
->lo_owner
.so_client
;
1110 lockdep_assert_held(&clp
->cl_lock
);
1112 list_del_init(&lo
->lo_owner
.so_strhash
);
1116 * Free a list of generic stateids that were collected earlier after being
1120 free_ol_stateid_reaplist(struct list_head
*reaplist
)
1122 struct nfs4_ol_stateid
*stp
;
1123 struct nfs4_file
*fp
;
1127 while (!list_empty(reaplist
)) {
1128 stp
= list_first_entry(reaplist
, struct nfs4_ol_stateid
,
1130 list_del(&stp
->st_locks
);
1131 fp
= stp
->st_stid
.sc_file
;
1132 stp
->st_stid
.sc_free(&stp
->st_stid
);
1138 static void release_lockowner(struct nfs4_lockowner
*lo
)
1140 struct nfs4_client
*clp
= lo
->lo_owner
.so_client
;
1141 struct nfs4_ol_stateid
*stp
;
1142 struct list_head reaplist
;
1144 INIT_LIST_HEAD(&reaplist
);
1146 spin_lock(&clp
->cl_lock
);
1147 unhash_lockowner_locked(lo
);
1148 while (!list_empty(&lo
->lo_owner
.so_stateids
)) {
1149 stp
= list_first_entry(&lo
->lo_owner
.so_stateids
,
1150 struct nfs4_ol_stateid
, st_perstateowner
);
1151 WARN_ON(!unhash_lock_stateid(stp
));
1152 put_ol_stateid_locked(stp
, &reaplist
);
1154 spin_unlock(&clp
->cl_lock
);
1155 free_ol_stateid_reaplist(&reaplist
);
1156 nfs4_put_stateowner(&lo
->lo_owner
);
1159 static void release_open_stateid_locks(struct nfs4_ol_stateid
*open_stp
,
1160 struct list_head
*reaplist
)
1162 struct nfs4_ol_stateid
*stp
;
1164 lockdep_assert_held(&open_stp
->st_stid
.sc_client
->cl_lock
);
1166 while (!list_empty(&open_stp
->st_locks
)) {
1167 stp
= list_entry(open_stp
->st_locks
.next
,
1168 struct nfs4_ol_stateid
, st_locks
);
1169 WARN_ON(!unhash_lock_stateid(stp
));
1170 put_ol_stateid_locked(stp
, reaplist
);
1174 static bool unhash_open_stateid(struct nfs4_ol_stateid
*stp
,
1175 struct list_head
*reaplist
)
1179 lockdep_assert_held(&stp
->st_stid
.sc_client
->cl_lock
);
1181 unhashed
= unhash_ol_stateid(stp
);
1182 release_open_stateid_locks(stp
, reaplist
);
1186 static void release_open_stateid(struct nfs4_ol_stateid
*stp
)
1188 LIST_HEAD(reaplist
);
1190 spin_lock(&stp
->st_stid
.sc_client
->cl_lock
);
1191 if (unhash_open_stateid(stp
, &reaplist
))
1192 put_ol_stateid_locked(stp
, &reaplist
);
1193 spin_unlock(&stp
->st_stid
.sc_client
->cl_lock
);
1194 free_ol_stateid_reaplist(&reaplist
);
1197 static void unhash_openowner_locked(struct nfs4_openowner
*oo
)
1199 struct nfs4_client
*clp
= oo
->oo_owner
.so_client
;
1201 lockdep_assert_held(&clp
->cl_lock
);
1203 list_del_init(&oo
->oo_owner
.so_strhash
);
1204 list_del_init(&oo
->oo_perclient
);
1207 static void release_last_closed_stateid(struct nfs4_openowner
*oo
)
1209 struct nfsd_net
*nn
= net_generic(oo
->oo_owner
.so_client
->net
,
1211 struct nfs4_ol_stateid
*s
;
1213 spin_lock(&nn
->client_lock
);
1214 s
= oo
->oo_last_closed_stid
;
1216 list_del_init(&oo
->oo_close_lru
);
1217 oo
->oo_last_closed_stid
= NULL
;
1219 spin_unlock(&nn
->client_lock
);
1221 nfs4_put_stid(&s
->st_stid
);
1224 static void release_openowner(struct nfs4_openowner
*oo
)
1226 struct nfs4_ol_stateid
*stp
;
1227 struct nfs4_client
*clp
= oo
->oo_owner
.so_client
;
1228 struct list_head reaplist
;
1230 INIT_LIST_HEAD(&reaplist
);
1232 spin_lock(&clp
->cl_lock
);
1233 unhash_openowner_locked(oo
);
1234 while (!list_empty(&oo
->oo_owner
.so_stateids
)) {
1235 stp
= list_first_entry(&oo
->oo_owner
.so_stateids
,
1236 struct nfs4_ol_stateid
, st_perstateowner
);
1237 if (unhash_open_stateid(stp
, &reaplist
))
1238 put_ol_stateid_locked(stp
, &reaplist
);
1240 spin_unlock(&clp
->cl_lock
);
1241 free_ol_stateid_reaplist(&reaplist
);
1242 release_last_closed_stateid(oo
);
1243 nfs4_put_stateowner(&oo
->oo_owner
);
1247 hash_sessionid(struct nfs4_sessionid
*sessionid
)
1249 struct nfsd4_sessionid
*sid
= (struct nfsd4_sessionid
*)sessionid
;
1251 return sid
->sequence
% SESSION_HASH_SIZE
;
1254 #ifdef CONFIG_SUNRPC_DEBUG
1256 dump_sessionid(const char *fn
, struct nfs4_sessionid
*sessionid
)
1258 u32
*ptr
= (u32
*)(&sessionid
->data
[0]);
1259 dprintk("%s: %u:%u:%u:%u\n", fn
, ptr
[0], ptr
[1], ptr
[2], ptr
[3]);
1263 dump_sessionid(const char *fn
, struct nfs4_sessionid
*sessionid
)
1269 * Bump the seqid on cstate->replay_owner, and clear replay_owner if it
1270 * won't be used for replay.
1272 void nfsd4_bump_seqid(struct nfsd4_compound_state
*cstate
, __be32 nfserr
)
1274 struct nfs4_stateowner
*so
= cstate
->replay_owner
;
1276 if (nfserr
== nfserr_replay_me
)
1279 if (!seqid_mutating_err(ntohl(nfserr
))) {
1280 nfsd4_cstate_clear_replay(cstate
);
1285 if (so
->so_is_open_owner
)
1286 release_last_closed_stateid(openowner(so
));
1292 gen_sessionid(struct nfsd4_session
*ses
)
1294 struct nfs4_client
*clp
= ses
->se_client
;
1295 struct nfsd4_sessionid
*sid
;
1297 sid
= (struct nfsd4_sessionid
*)ses
->se_sessionid
.data
;
1298 sid
->clientid
= clp
->cl_clientid
;
1299 sid
->sequence
= current_sessionid
++;
1304 * The protocol defines ca_maxresponssize_cached to include the size of
1305 * the rpc header, but all we need to cache is the data starting after
1306 * the end of the initial SEQUENCE operation--the rest we regenerate
1307 * each time. Therefore we can advertise a ca_maxresponssize_cached
1308 * value that is the number of bytes in our cache plus a few additional
1309 * bytes. In order to stay on the safe side, and not promise more than
1310 * we can cache, those additional bytes must be the minimum possible: 24
1311 * bytes of rpc header (xid through accept state, with AUTH_NULL
1312 * verifier), 12 for the compound header (with zero-length tag), and 44
1313 * for the SEQUENCE op response:
1315 #define NFSD_MIN_HDR_SEQ_SZ (24 + 12 + 44)
1318 free_session_slots(struct nfsd4_session
*ses
)
1322 for (i
= 0; i
< ses
->se_fchannel
.maxreqs
; i
++)
1323 kfree(ses
->se_slots
[i
]);
1327 * We don't actually need to cache the rpc and session headers, so we
1328 * can allocate a little less for each slot:
1330 static inline u32
slot_bytes(struct nfsd4_channel_attrs
*ca
)
1334 if (ca
->maxresp_cached
< NFSD_MIN_HDR_SEQ_SZ
)
1337 size
= ca
->maxresp_cached
- NFSD_MIN_HDR_SEQ_SZ
;
1338 return size
+ sizeof(struct nfsd4_slot
);
1342 * XXX: If we run out of reserved DRC memory we could (up to a point)
1343 * re-negotiate active sessions and reduce their slot usage to make
1344 * room for new connections. For now we just fail the create session.
1346 static u32
nfsd4_get_drc_mem(struct nfsd4_channel_attrs
*ca
)
1348 u32 slotsize
= slot_bytes(ca
);
1349 u32 num
= ca
->maxreqs
;
1352 spin_lock(&nfsd_drc_lock
);
1353 avail
= min((unsigned long)NFSD_MAX_MEM_PER_SESSION
,
1354 nfsd_drc_max_mem
- nfsd_drc_mem_used
);
1355 num
= min_t(int, num
, avail
/ slotsize
);
1356 nfsd_drc_mem_used
+= num
* slotsize
;
1357 spin_unlock(&nfsd_drc_lock
);
1362 static void nfsd4_put_drc_mem(struct nfsd4_channel_attrs
*ca
)
1364 int slotsize
= slot_bytes(ca
);
1366 spin_lock(&nfsd_drc_lock
);
1367 nfsd_drc_mem_used
-= slotsize
* ca
->maxreqs
;
1368 spin_unlock(&nfsd_drc_lock
);
1371 static struct nfsd4_session
*alloc_session(struct nfsd4_channel_attrs
*fattrs
,
1372 struct nfsd4_channel_attrs
*battrs
)
1374 int numslots
= fattrs
->maxreqs
;
1375 int slotsize
= slot_bytes(fattrs
);
1376 struct nfsd4_session
*new;
1379 BUILD_BUG_ON(NFSD_MAX_SLOTS_PER_SESSION
* sizeof(struct nfsd4_slot
*)
1380 + sizeof(struct nfsd4_session
) > PAGE_SIZE
);
1381 mem
= numslots
* sizeof(struct nfsd4_slot
*);
1383 new = kzalloc(sizeof(*new) + mem
, GFP_KERNEL
);
1386 /* allocate each struct nfsd4_slot and data cache in one piece */
1387 for (i
= 0; i
< numslots
; i
++) {
1388 new->se_slots
[i
] = kzalloc(slotsize
, GFP_KERNEL
);
1389 if (!new->se_slots
[i
])
1393 memcpy(&new->se_fchannel
, fattrs
, sizeof(struct nfsd4_channel_attrs
));
1394 memcpy(&new->se_bchannel
, battrs
, sizeof(struct nfsd4_channel_attrs
));
1399 kfree(new->se_slots
[i
]);
1404 static void free_conn(struct nfsd4_conn
*c
)
1406 svc_xprt_put(c
->cn_xprt
);
1410 static void nfsd4_conn_lost(struct svc_xpt_user
*u
)
1412 struct nfsd4_conn
*c
= container_of(u
, struct nfsd4_conn
, cn_xpt_user
);
1413 struct nfs4_client
*clp
= c
->cn_session
->se_client
;
1415 spin_lock(&clp
->cl_lock
);
1416 if (!list_empty(&c
->cn_persession
)) {
1417 list_del(&c
->cn_persession
);
1420 nfsd4_probe_callback(clp
);
1421 spin_unlock(&clp
->cl_lock
);
1424 static struct nfsd4_conn
*alloc_conn(struct svc_rqst
*rqstp
, u32 flags
)
1426 struct nfsd4_conn
*conn
;
1428 conn
= kmalloc(sizeof(struct nfsd4_conn
), GFP_KERNEL
);
1431 svc_xprt_get(rqstp
->rq_xprt
);
1432 conn
->cn_xprt
= rqstp
->rq_xprt
;
1433 conn
->cn_flags
= flags
;
1434 INIT_LIST_HEAD(&conn
->cn_xpt_user
.list
);
1438 static void __nfsd4_hash_conn(struct nfsd4_conn
*conn
, struct nfsd4_session
*ses
)
1440 conn
->cn_session
= ses
;
1441 list_add(&conn
->cn_persession
, &ses
->se_conns
);
1444 static void nfsd4_hash_conn(struct nfsd4_conn
*conn
, struct nfsd4_session
*ses
)
1446 struct nfs4_client
*clp
= ses
->se_client
;
1448 spin_lock(&clp
->cl_lock
);
1449 __nfsd4_hash_conn(conn
, ses
);
1450 spin_unlock(&clp
->cl_lock
);
1453 static int nfsd4_register_conn(struct nfsd4_conn
*conn
)
1455 conn
->cn_xpt_user
.callback
= nfsd4_conn_lost
;
1456 return register_xpt_user(conn
->cn_xprt
, &conn
->cn_xpt_user
);
1459 static void nfsd4_init_conn(struct svc_rqst
*rqstp
, struct nfsd4_conn
*conn
, struct nfsd4_session
*ses
)
1463 nfsd4_hash_conn(conn
, ses
);
1464 ret
= nfsd4_register_conn(conn
);
1466 /* oops; xprt is already down: */
1467 nfsd4_conn_lost(&conn
->cn_xpt_user
);
1468 /* We may have gained or lost a callback channel: */
1469 nfsd4_probe_callback_sync(ses
->se_client
);
1472 static struct nfsd4_conn
*alloc_conn_from_crses(struct svc_rqst
*rqstp
, struct nfsd4_create_session
*cses
)
1474 u32 dir
= NFS4_CDFC4_FORE
;
1476 if (cses
->flags
& SESSION4_BACK_CHAN
)
1477 dir
|= NFS4_CDFC4_BACK
;
1478 return alloc_conn(rqstp
, dir
);
1481 /* must be called under client_lock */
1482 static void nfsd4_del_conns(struct nfsd4_session
*s
)
1484 struct nfs4_client
*clp
= s
->se_client
;
1485 struct nfsd4_conn
*c
;
1487 spin_lock(&clp
->cl_lock
);
1488 while (!list_empty(&s
->se_conns
)) {
1489 c
= list_first_entry(&s
->se_conns
, struct nfsd4_conn
, cn_persession
);
1490 list_del_init(&c
->cn_persession
);
1491 spin_unlock(&clp
->cl_lock
);
1493 unregister_xpt_user(c
->cn_xprt
, &c
->cn_xpt_user
);
1496 spin_lock(&clp
->cl_lock
);
1498 spin_unlock(&clp
->cl_lock
);
1501 static void __free_session(struct nfsd4_session
*ses
)
1503 free_session_slots(ses
);
1507 static void free_session(struct nfsd4_session
*ses
)
1509 nfsd4_del_conns(ses
);
1510 nfsd4_put_drc_mem(&ses
->se_fchannel
);
1511 __free_session(ses
);
1514 static void init_session(struct svc_rqst
*rqstp
, struct nfsd4_session
*new, struct nfs4_client
*clp
, struct nfsd4_create_session
*cses
)
1517 struct nfsd_net
*nn
= net_generic(SVC_NET(rqstp
), nfsd_net_id
);
1519 new->se_client
= clp
;
1522 INIT_LIST_HEAD(&new->se_conns
);
1524 new->se_cb_seq_nr
= 1;
1525 new->se_flags
= cses
->flags
;
1526 new->se_cb_prog
= cses
->callback_prog
;
1527 new->se_cb_sec
= cses
->cb_sec
;
1528 atomic_set(&new->se_ref
, 0);
1529 idx
= hash_sessionid(&new->se_sessionid
);
1530 list_add(&new->se_hash
, &nn
->sessionid_hashtbl
[idx
]);
1531 spin_lock(&clp
->cl_lock
);
1532 list_add(&new->se_perclnt
, &clp
->cl_sessions
);
1533 spin_unlock(&clp
->cl_lock
);
1536 struct sockaddr
*sa
= svc_addr(rqstp
);
1538 * This is a little silly; with sessions there's no real
1539 * use for the callback address. Use the peer address
1540 * as a reasonable default for now, but consider fixing
1541 * the rpc client not to require an address in the
1544 rpc_copy_addr((struct sockaddr
*)&clp
->cl_cb_conn
.cb_addr
, sa
);
1545 clp
->cl_cb_conn
.cb_addrlen
= svc_addr_len(sa
);
1549 /* caller must hold client_lock */
1550 static struct nfsd4_session
*
1551 __find_in_sessionid_hashtbl(struct nfs4_sessionid
*sessionid
, struct net
*net
)
1553 struct nfsd4_session
*elem
;
1555 struct nfsd_net
*nn
= net_generic(net
, nfsd_net_id
);
1557 lockdep_assert_held(&nn
->client_lock
);
1559 dump_sessionid(__func__
, sessionid
);
1560 idx
= hash_sessionid(sessionid
);
1561 /* Search in the appropriate list */
1562 list_for_each_entry(elem
, &nn
->sessionid_hashtbl
[idx
], se_hash
) {
1563 if (!memcmp(elem
->se_sessionid
.data
, sessionid
->data
,
1564 NFS4_MAX_SESSIONID_LEN
)) {
1569 dprintk("%s: session not found\n", __func__
);
1573 static struct nfsd4_session
*
1574 find_in_sessionid_hashtbl(struct nfs4_sessionid
*sessionid
, struct net
*net
,
1577 struct nfsd4_session
*session
;
1578 __be32 status
= nfserr_badsession
;
1580 session
= __find_in_sessionid_hashtbl(sessionid
, net
);
1583 status
= nfsd4_get_session_locked(session
);
1591 /* caller must hold client_lock */
1593 unhash_session(struct nfsd4_session
*ses
)
1595 struct nfs4_client
*clp
= ses
->se_client
;
1596 struct nfsd_net
*nn
= net_generic(clp
->net
, nfsd_net_id
);
1598 lockdep_assert_held(&nn
->client_lock
);
1600 list_del(&ses
->se_hash
);
1601 spin_lock(&ses
->se_client
->cl_lock
);
1602 list_del(&ses
->se_perclnt
);
1603 spin_unlock(&ses
->se_client
->cl_lock
);
1606 /* SETCLIENTID and SETCLIENTID_CONFIRM Helper functions */
1608 STALE_CLIENTID(clientid_t
*clid
, struct nfsd_net
*nn
)
1611 * We're assuming the clid was not given out from a boot
1612 * precisely 2^32 (about 136 years) before this one. That seems
1613 * a safe assumption:
1615 if (clid
->cl_boot
== (u32
)nn
->boot_time
)
1617 dprintk("NFSD stale clientid (%08x/%08x) boot_time %08lx\n",
1618 clid
->cl_boot
, clid
->cl_id
, nn
->boot_time
);
1623 * XXX Should we use a slab cache ?
1624 * This type of memory management is somewhat inefficient, but we use it
1625 * anyway since SETCLIENTID is not a common operation.
1627 static struct nfs4_client
*alloc_client(struct xdr_netobj name
)
1629 struct nfs4_client
*clp
;
1632 clp
= kzalloc(sizeof(struct nfs4_client
), GFP_KERNEL
);
1635 clp
->cl_name
.data
= kmemdup(name
.data
, name
.len
, GFP_KERNEL
);
1636 if (clp
->cl_name
.data
== NULL
)
1638 clp
->cl_ownerstr_hashtbl
= kmalloc(sizeof(struct list_head
) *
1639 OWNER_HASH_SIZE
, GFP_KERNEL
);
1640 if (!clp
->cl_ownerstr_hashtbl
)
1641 goto err_no_hashtbl
;
1642 for (i
= 0; i
< OWNER_HASH_SIZE
; i
++)
1643 INIT_LIST_HEAD(&clp
->cl_ownerstr_hashtbl
[i
]);
1644 clp
->cl_name
.len
= name
.len
;
1645 INIT_LIST_HEAD(&clp
->cl_sessions
);
1646 idr_init(&clp
->cl_stateids
);
1647 atomic_set(&clp
->cl_refcount
, 0);
1648 clp
->cl_cb_state
= NFSD4_CB_UNKNOWN
;
1649 INIT_LIST_HEAD(&clp
->cl_idhash
);
1650 INIT_LIST_HEAD(&clp
->cl_openowners
);
1651 INIT_LIST_HEAD(&clp
->cl_delegations
);
1652 INIT_LIST_HEAD(&clp
->cl_lru
);
1653 INIT_LIST_HEAD(&clp
->cl_revoked
);
1654 #ifdef CONFIG_NFSD_PNFS
1655 INIT_LIST_HEAD(&clp
->cl_lo_states
);
1657 spin_lock_init(&clp
->cl_lock
);
1658 rpc_init_wait_queue(&clp
->cl_cb_waitq
, "Backchannel slot table");
1661 kfree(clp
->cl_name
.data
);
1668 free_client(struct nfs4_client
*clp
)
1670 while (!list_empty(&clp
->cl_sessions
)) {
1671 struct nfsd4_session
*ses
;
1672 ses
= list_entry(clp
->cl_sessions
.next
, struct nfsd4_session
,
1674 list_del(&ses
->se_perclnt
);
1675 WARN_ON_ONCE(atomic_read(&ses
->se_ref
));
1678 rpc_destroy_wait_queue(&clp
->cl_cb_waitq
);
1679 free_svc_cred(&clp
->cl_cred
);
1680 kfree(clp
->cl_ownerstr_hashtbl
);
1681 kfree(clp
->cl_name
.data
);
1682 idr_destroy(&clp
->cl_stateids
);
1686 /* must be called under the client_lock */
1688 unhash_client_locked(struct nfs4_client
*clp
)
1690 struct nfsd_net
*nn
= net_generic(clp
->net
, nfsd_net_id
);
1691 struct nfsd4_session
*ses
;
1693 lockdep_assert_held(&nn
->client_lock
);
1695 /* Mark the client as expired! */
1697 /* Make it invisible */
1698 if (!list_empty(&clp
->cl_idhash
)) {
1699 list_del_init(&clp
->cl_idhash
);
1700 if (test_bit(NFSD4_CLIENT_CONFIRMED
, &clp
->cl_flags
))
1701 rb_erase(&clp
->cl_namenode
, &nn
->conf_name_tree
);
1703 rb_erase(&clp
->cl_namenode
, &nn
->unconf_name_tree
);
1705 list_del_init(&clp
->cl_lru
);
1706 spin_lock(&clp
->cl_lock
);
1707 list_for_each_entry(ses
, &clp
->cl_sessions
, se_perclnt
)
1708 list_del_init(&ses
->se_hash
);
1709 spin_unlock(&clp
->cl_lock
);
1713 unhash_client(struct nfs4_client
*clp
)
1715 struct nfsd_net
*nn
= net_generic(clp
->net
, nfsd_net_id
);
1717 spin_lock(&nn
->client_lock
);
1718 unhash_client_locked(clp
);
1719 spin_unlock(&nn
->client_lock
);
1722 static __be32
mark_client_expired_locked(struct nfs4_client
*clp
)
1724 if (atomic_read(&clp
->cl_refcount
))
1725 return nfserr_jukebox
;
1726 unhash_client_locked(clp
);
1731 __destroy_client(struct nfs4_client
*clp
)
1733 struct nfs4_openowner
*oo
;
1734 struct nfs4_delegation
*dp
;
1735 struct list_head reaplist
;
1737 INIT_LIST_HEAD(&reaplist
);
1738 spin_lock(&state_lock
);
1739 while (!list_empty(&clp
->cl_delegations
)) {
1740 dp
= list_entry(clp
->cl_delegations
.next
, struct nfs4_delegation
, dl_perclnt
);
1741 WARN_ON(!unhash_delegation_locked(dp
));
1742 list_add(&dp
->dl_recall_lru
, &reaplist
);
1744 spin_unlock(&state_lock
);
1745 while (!list_empty(&reaplist
)) {
1746 dp
= list_entry(reaplist
.next
, struct nfs4_delegation
, dl_recall_lru
);
1747 list_del_init(&dp
->dl_recall_lru
);
1748 put_clnt_odstate(dp
->dl_clnt_odstate
);
1749 nfs4_put_deleg_lease(dp
->dl_stid
.sc_file
);
1750 nfs4_put_stid(&dp
->dl_stid
);
1752 while (!list_empty(&clp
->cl_revoked
)) {
1753 dp
= list_entry(clp
->cl_revoked
.next
, struct nfs4_delegation
, dl_recall_lru
);
1754 list_del_init(&dp
->dl_recall_lru
);
1755 nfs4_put_stid(&dp
->dl_stid
);
1757 while (!list_empty(&clp
->cl_openowners
)) {
1758 oo
= list_entry(clp
->cl_openowners
.next
, struct nfs4_openowner
, oo_perclient
);
1759 nfs4_get_stateowner(&oo
->oo_owner
);
1760 release_openowner(oo
);
1762 nfsd4_return_all_client_layouts(clp
);
1763 nfsd4_shutdown_callback(clp
);
1764 if (clp
->cl_cb_conn
.cb_xprt
)
1765 svc_xprt_put(clp
->cl_cb_conn
.cb_xprt
);
1770 destroy_client(struct nfs4_client
*clp
)
1773 __destroy_client(clp
);
1776 static void expire_client(struct nfs4_client
*clp
)
1779 nfsd4_client_record_remove(clp
);
1780 __destroy_client(clp
);
1783 static void copy_verf(struct nfs4_client
*target
, nfs4_verifier
*source
)
1785 memcpy(target
->cl_verifier
.data
, source
->data
,
1786 sizeof(target
->cl_verifier
.data
));
1789 static void copy_clid(struct nfs4_client
*target
, struct nfs4_client
*source
)
1791 target
->cl_clientid
.cl_boot
= source
->cl_clientid
.cl_boot
;
1792 target
->cl_clientid
.cl_id
= source
->cl_clientid
.cl_id
;
1795 static int copy_cred(struct svc_cred
*target
, struct svc_cred
*source
)
1797 if (source
->cr_principal
) {
1798 target
->cr_principal
=
1799 kstrdup(source
->cr_principal
, GFP_KERNEL
);
1800 if (target
->cr_principal
== NULL
)
1803 target
->cr_principal
= NULL
;
1804 target
->cr_flavor
= source
->cr_flavor
;
1805 target
->cr_uid
= source
->cr_uid
;
1806 target
->cr_gid
= source
->cr_gid
;
1807 target
->cr_group_info
= source
->cr_group_info
;
1808 get_group_info(target
->cr_group_info
);
1809 target
->cr_gss_mech
= source
->cr_gss_mech
;
1810 if (source
->cr_gss_mech
)
1811 gss_mech_get(source
->cr_gss_mech
);
1816 compare_blob(const struct xdr_netobj
*o1
, const struct xdr_netobj
*o2
)
1818 if (o1
->len
< o2
->len
)
1820 if (o1
->len
> o2
->len
)
1822 return memcmp(o1
->data
, o2
->data
, o1
->len
);
1825 static int same_name(const char *n1
, const char *n2
)
1827 return 0 == memcmp(n1
, n2
, HEXDIR_LEN
);
1831 same_verf(nfs4_verifier
*v1
, nfs4_verifier
*v2
)
1833 return 0 == memcmp(v1
->data
, v2
->data
, sizeof(v1
->data
));
1837 same_clid(clientid_t
*cl1
, clientid_t
*cl2
)
1839 return (cl1
->cl_boot
== cl2
->cl_boot
) && (cl1
->cl_id
== cl2
->cl_id
);
1842 static bool groups_equal(struct group_info
*g1
, struct group_info
*g2
)
1846 if (g1
->ngroups
!= g2
->ngroups
)
1848 for (i
=0; i
<g1
->ngroups
; i
++)
1849 if (!gid_eq(GROUP_AT(g1
, i
), GROUP_AT(g2
, i
)))
1855 * RFC 3530 language requires clid_inuse be returned when the
1856 * "principal" associated with a requests differs from that previously
1857 * used. We use uid, gid's, and gss principal string as our best
1858 * approximation. We also don't want to allow non-gss use of a client
1859 * established using gss: in theory cr_principal should catch that
1860 * change, but in practice cr_principal can be null even in the gss case
1861 * since gssd doesn't always pass down a principal string.
1863 static bool is_gss_cred(struct svc_cred
*cr
)
1865 /* Is cr_flavor one of the gss "pseudoflavors"?: */
1866 return (cr
->cr_flavor
> RPC_AUTH_MAXFLAVOR
);
1871 same_creds(struct svc_cred
*cr1
, struct svc_cred
*cr2
)
1873 if ((is_gss_cred(cr1
) != is_gss_cred(cr2
))
1874 || (!uid_eq(cr1
->cr_uid
, cr2
->cr_uid
))
1875 || (!gid_eq(cr1
->cr_gid
, cr2
->cr_gid
))
1876 || !groups_equal(cr1
->cr_group_info
, cr2
->cr_group_info
))
1878 if (cr1
->cr_principal
== cr2
->cr_principal
)
1880 if (!cr1
->cr_principal
|| !cr2
->cr_principal
)
1882 return 0 == strcmp(cr1
->cr_principal
, cr2
->cr_principal
);
1885 static bool svc_rqst_integrity_protected(struct svc_rqst
*rqstp
)
1887 struct svc_cred
*cr
= &rqstp
->rq_cred
;
1890 if (!cr
->cr_gss_mech
)
1892 service
= gss_pseudoflavor_to_service(cr
->cr_gss_mech
, cr
->cr_flavor
);
1893 return service
== RPC_GSS_SVC_INTEGRITY
||
1894 service
== RPC_GSS_SVC_PRIVACY
;
1897 static bool mach_creds_match(struct nfs4_client
*cl
, struct svc_rqst
*rqstp
)
1899 struct svc_cred
*cr
= &rqstp
->rq_cred
;
1901 if (!cl
->cl_mach_cred
)
1903 if (cl
->cl_cred
.cr_gss_mech
!= cr
->cr_gss_mech
)
1905 if (!svc_rqst_integrity_protected(rqstp
))
1907 if (!cr
->cr_principal
)
1909 return 0 == strcmp(cl
->cl_cred
.cr_principal
, cr
->cr_principal
);
1912 static void gen_confirm(struct nfs4_client
*clp
, struct nfsd_net
*nn
)
1917 * This is opaque to client, so no need to byte-swap. Use
1918 * __force to keep sparse happy
1920 verf
[0] = (__force __be32
)get_seconds();
1921 verf
[1] = (__force __be32
)nn
->clverifier_counter
++;
1922 memcpy(clp
->cl_confirm
.data
, verf
, sizeof(clp
->cl_confirm
.data
));
1925 static void gen_clid(struct nfs4_client
*clp
, struct nfsd_net
*nn
)
1927 clp
->cl_clientid
.cl_boot
= nn
->boot_time
;
1928 clp
->cl_clientid
.cl_id
= nn
->clientid_counter
++;
1929 gen_confirm(clp
, nn
);
1932 static struct nfs4_stid
*
1933 find_stateid_locked(struct nfs4_client
*cl
, stateid_t
*t
)
1935 struct nfs4_stid
*ret
;
1937 ret
= idr_find(&cl
->cl_stateids
, t
->si_opaque
.so_id
);
1938 if (!ret
|| !ret
->sc_type
)
1943 static struct nfs4_stid
*
1944 find_stateid_by_type(struct nfs4_client
*cl
, stateid_t
*t
, char typemask
)
1946 struct nfs4_stid
*s
;
1948 spin_lock(&cl
->cl_lock
);
1949 s
= find_stateid_locked(cl
, t
);
1951 if (typemask
& s
->sc_type
)
1952 atomic_inc(&s
->sc_count
);
1956 spin_unlock(&cl
->cl_lock
);
1960 static struct nfs4_client
*create_client(struct xdr_netobj name
,
1961 struct svc_rqst
*rqstp
, nfs4_verifier
*verf
)
1963 struct nfs4_client
*clp
;
1964 struct sockaddr
*sa
= svc_addr(rqstp
);
1966 struct net
*net
= SVC_NET(rqstp
);
1968 clp
= alloc_client(name
);
1972 ret
= copy_cred(&clp
->cl_cred
, &rqstp
->rq_cred
);
1977 nfsd4_init_cb(&clp
->cl_cb_null
, clp
, NULL
, NFSPROC4_CLNT_CB_NULL
);
1978 clp
->cl_time
= get_seconds();
1979 clear_bit(0, &clp
->cl_cb_slot_busy
);
1980 copy_verf(clp
, verf
);
1981 rpc_copy_addr((struct sockaddr
*) &clp
->cl_addr
, sa
);
1982 clp
->cl_cb_session
= NULL
;
1988 add_clp_to_name_tree(struct nfs4_client
*new_clp
, struct rb_root
*root
)
1990 struct rb_node
**new = &(root
->rb_node
), *parent
= NULL
;
1991 struct nfs4_client
*clp
;
1994 clp
= rb_entry(*new, struct nfs4_client
, cl_namenode
);
1997 if (compare_blob(&clp
->cl_name
, &new_clp
->cl_name
) > 0)
1998 new = &((*new)->rb_left
);
2000 new = &((*new)->rb_right
);
2003 rb_link_node(&new_clp
->cl_namenode
, parent
, new);
2004 rb_insert_color(&new_clp
->cl_namenode
, root
);
2007 static struct nfs4_client
*
2008 find_clp_in_name_tree(struct xdr_netobj
*name
, struct rb_root
*root
)
2011 struct rb_node
*node
= root
->rb_node
;
2012 struct nfs4_client
*clp
;
2015 clp
= rb_entry(node
, struct nfs4_client
, cl_namenode
);
2016 cmp
= compare_blob(&clp
->cl_name
, name
);
2018 node
= node
->rb_left
;
2020 node
= node
->rb_right
;
2028 add_to_unconfirmed(struct nfs4_client
*clp
)
2030 unsigned int idhashval
;
2031 struct nfsd_net
*nn
= net_generic(clp
->net
, nfsd_net_id
);
2033 lockdep_assert_held(&nn
->client_lock
);
2035 clear_bit(NFSD4_CLIENT_CONFIRMED
, &clp
->cl_flags
);
2036 add_clp_to_name_tree(clp
, &nn
->unconf_name_tree
);
2037 idhashval
= clientid_hashval(clp
->cl_clientid
.cl_id
);
2038 list_add(&clp
->cl_idhash
, &nn
->unconf_id_hashtbl
[idhashval
]);
2039 renew_client_locked(clp
);
2043 move_to_confirmed(struct nfs4_client
*clp
)
2045 unsigned int idhashval
= clientid_hashval(clp
->cl_clientid
.cl_id
);
2046 struct nfsd_net
*nn
= net_generic(clp
->net
, nfsd_net_id
);
2048 lockdep_assert_held(&nn
->client_lock
);
2050 dprintk("NFSD: move_to_confirm nfs4_client %p\n", clp
);
2051 list_move(&clp
->cl_idhash
, &nn
->conf_id_hashtbl
[idhashval
]);
2052 rb_erase(&clp
->cl_namenode
, &nn
->unconf_name_tree
);
2053 add_clp_to_name_tree(clp
, &nn
->conf_name_tree
);
2054 set_bit(NFSD4_CLIENT_CONFIRMED
, &clp
->cl_flags
);
2055 renew_client_locked(clp
);
2058 static struct nfs4_client
*
2059 find_client_in_id_table(struct list_head
*tbl
, clientid_t
*clid
, bool sessions
)
2061 struct nfs4_client
*clp
;
2062 unsigned int idhashval
= clientid_hashval(clid
->cl_id
);
2064 list_for_each_entry(clp
, &tbl
[idhashval
], cl_idhash
) {
2065 if (same_clid(&clp
->cl_clientid
, clid
)) {
2066 if ((bool)clp
->cl_minorversion
!= sessions
)
2068 renew_client_locked(clp
);
2075 static struct nfs4_client
*
2076 find_confirmed_client(clientid_t
*clid
, bool sessions
, struct nfsd_net
*nn
)
2078 struct list_head
*tbl
= nn
->conf_id_hashtbl
;
2080 lockdep_assert_held(&nn
->client_lock
);
2081 return find_client_in_id_table(tbl
, clid
, sessions
);
2084 static struct nfs4_client
*
2085 find_unconfirmed_client(clientid_t
*clid
, bool sessions
, struct nfsd_net
*nn
)
2087 struct list_head
*tbl
= nn
->unconf_id_hashtbl
;
2089 lockdep_assert_held(&nn
->client_lock
);
2090 return find_client_in_id_table(tbl
, clid
, sessions
);
2093 static bool clp_used_exchangeid(struct nfs4_client
*clp
)
2095 return clp
->cl_exchange_flags
!= 0;
2098 static struct nfs4_client
*
2099 find_confirmed_client_by_name(struct xdr_netobj
*name
, struct nfsd_net
*nn
)
2101 lockdep_assert_held(&nn
->client_lock
);
2102 return find_clp_in_name_tree(name
, &nn
->conf_name_tree
);
2105 static struct nfs4_client
*
2106 find_unconfirmed_client_by_name(struct xdr_netobj
*name
, struct nfsd_net
*nn
)
2108 lockdep_assert_held(&nn
->client_lock
);
2109 return find_clp_in_name_tree(name
, &nn
->unconf_name_tree
);
2113 gen_callback(struct nfs4_client
*clp
, struct nfsd4_setclientid
*se
, struct svc_rqst
*rqstp
)
2115 struct nfs4_cb_conn
*conn
= &clp
->cl_cb_conn
;
2116 struct sockaddr
*sa
= svc_addr(rqstp
);
2117 u32 scopeid
= rpc_get_scope_id(sa
);
2118 unsigned short expected_family
;
2120 /* Currently, we only support tcp and tcp6 for the callback channel */
2121 if (se
->se_callback_netid_len
== 3 &&
2122 !memcmp(se
->se_callback_netid_val
, "tcp", 3))
2123 expected_family
= AF_INET
;
2124 else if (se
->se_callback_netid_len
== 4 &&
2125 !memcmp(se
->se_callback_netid_val
, "tcp6", 4))
2126 expected_family
= AF_INET6
;
2130 conn
->cb_addrlen
= rpc_uaddr2sockaddr(clp
->net
, se
->se_callback_addr_val
,
2131 se
->se_callback_addr_len
,
2132 (struct sockaddr
*)&conn
->cb_addr
,
2133 sizeof(conn
->cb_addr
));
2135 if (!conn
->cb_addrlen
|| conn
->cb_addr
.ss_family
!= expected_family
)
2138 if (conn
->cb_addr
.ss_family
== AF_INET6
)
2139 ((struct sockaddr_in6
*)&conn
->cb_addr
)->sin6_scope_id
= scopeid
;
2141 conn
->cb_prog
= se
->se_callback_prog
;
2142 conn
->cb_ident
= se
->se_callback_ident
;
2143 memcpy(&conn
->cb_saddr
, &rqstp
->rq_daddr
, rqstp
->rq_daddrlen
);
2146 conn
->cb_addr
.ss_family
= AF_UNSPEC
;
2147 conn
->cb_addrlen
= 0;
2148 dprintk(KERN_INFO
"NFSD: this client (clientid %08x/%08x) "
2149 "will not receive delegations\n",
2150 clp
->cl_clientid
.cl_boot
, clp
->cl_clientid
.cl_id
);
2156 * Cache a reply. nfsd4_check_resp_size() has bounded the cache size.
2159 nfsd4_store_cache_entry(struct nfsd4_compoundres
*resp
)
2161 struct xdr_buf
*buf
= resp
->xdr
.buf
;
2162 struct nfsd4_slot
*slot
= resp
->cstate
.slot
;
2165 dprintk("--> %s slot %p\n", __func__
, slot
);
2167 slot
->sl_opcnt
= resp
->opcnt
;
2168 slot
->sl_status
= resp
->cstate
.status
;
2170 slot
->sl_flags
|= NFSD4_SLOT_INITIALIZED
;
2171 if (nfsd4_not_cached(resp
)) {
2172 slot
->sl_datalen
= 0;
2175 base
= resp
->cstate
.data_offset
;
2176 slot
->sl_datalen
= buf
->len
- base
;
2177 if (read_bytes_from_xdr_buf(buf
, base
, slot
->sl_data
, slot
->sl_datalen
))
2178 WARN("%s: sessions DRC could not cache compound\n", __func__
);
2183 * Encode the replay sequence operation from the slot values.
2184 * If cachethis is FALSE encode the uncached rep error on the next
2185 * operation which sets resp->p and increments resp->opcnt for
2186 * nfs4svc_encode_compoundres.
2190 nfsd4_enc_sequence_replay(struct nfsd4_compoundargs
*args
,
2191 struct nfsd4_compoundres
*resp
)
2193 struct nfsd4_op
*op
;
2194 struct nfsd4_slot
*slot
= resp
->cstate
.slot
;
2196 /* Encode the replayed sequence operation */
2197 op
= &args
->ops
[resp
->opcnt
- 1];
2198 nfsd4_encode_operation(resp
, op
);
2200 /* Return nfserr_retry_uncached_rep in next operation. */
2201 if (args
->opcnt
> 1 && !(slot
->sl_flags
& NFSD4_SLOT_CACHETHIS
)) {
2202 op
= &args
->ops
[resp
->opcnt
++];
2203 op
->status
= nfserr_retry_uncached_rep
;
2204 nfsd4_encode_operation(resp
, op
);
2210 * The sequence operation is not cached because we can use the slot and
2214 nfsd4_replay_cache_entry(struct nfsd4_compoundres
*resp
,
2215 struct nfsd4_sequence
*seq
)
2217 struct nfsd4_slot
*slot
= resp
->cstate
.slot
;
2218 struct xdr_stream
*xdr
= &resp
->xdr
;
2222 dprintk("--> %s slot %p\n", __func__
, slot
);
2224 status
= nfsd4_enc_sequence_replay(resp
->rqstp
->rq_argp
, resp
);
2228 p
= xdr_reserve_space(xdr
, slot
->sl_datalen
);
2231 return nfserr_serverfault
;
2233 xdr_encode_opaque_fixed(p
, slot
->sl_data
, slot
->sl_datalen
);
2234 xdr_commit_encode(xdr
);
2236 resp
->opcnt
= slot
->sl_opcnt
;
2237 return slot
->sl_status
;
2241 * Set the exchange_id flags returned by the server.
2244 nfsd4_set_ex_flags(struct nfs4_client
*new, struct nfsd4_exchange_id
*clid
)
2246 #ifdef CONFIG_NFSD_PNFS
2247 new->cl_exchange_flags
|= EXCHGID4_FLAG_USE_PNFS_MDS
;
2249 new->cl_exchange_flags
|= EXCHGID4_FLAG_USE_NON_PNFS
;
2252 /* Referrals are supported, Migration is not. */
2253 new->cl_exchange_flags
|= EXCHGID4_FLAG_SUPP_MOVED_REFER
;
2255 /* set the wire flags to return to client. */
2256 clid
->flags
= new->cl_exchange_flags
;
2259 static bool client_has_state(struct nfs4_client
*clp
)
2262 * Note clp->cl_openowners check isn't quite right: there's no
2263 * need to count owners without stateid's.
2265 * Also note we should probably be using this in 4.0 case too.
2267 return !list_empty(&clp
->cl_openowners
)
2268 #ifdef CONFIG_NFSD_PNFS
2269 || !list_empty(&clp
->cl_lo_states
)
2271 || !list_empty(&clp
->cl_delegations
)
2272 || !list_empty(&clp
->cl_sessions
);
2276 nfsd4_exchange_id(struct svc_rqst
*rqstp
,
2277 struct nfsd4_compound_state
*cstate
,
2278 struct nfsd4_exchange_id
*exid
)
2280 struct nfs4_client
*conf
, *new;
2281 struct nfs4_client
*unconf
= NULL
;
2283 char addr_str
[INET6_ADDRSTRLEN
];
2284 nfs4_verifier verf
= exid
->verifier
;
2285 struct sockaddr
*sa
= svc_addr(rqstp
);
2286 bool update
= exid
->flags
& EXCHGID4_FLAG_UPD_CONFIRMED_REC_A
;
2287 struct nfsd_net
*nn
= net_generic(SVC_NET(rqstp
), nfsd_net_id
);
2289 rpc_ntop(sa
, addr_str
, sizeof(addr_str
));
2290 dprintk("%s rqstp=%p exid=%p clname.len=%u clname.data=%p "
2291 "ip_addr=%s flags %x, spa_how %d\n",
2292 __func__
, rqstp
, exid
, exid
->clname
.len
, exid
->clname
.data
,
2293 addr_str
, exid
->flags
, exid
->spa_how
);
2295 if (exid
->flags
& ~EXCHGID4_FLAG_MASK_A
)
2296 return nfserr_inval
;
2298 switch (exid
->spa_how
) {
2300 if (!svc_rqst_integrity_protected(rqstp
))
2301 return nfserr_inval
;
2304 default: /* checked by xdr code */
2307 return nfserr_encr_alg_unsupp
;
2310 new = create_client(exid
->clname
, rqstp
, &verf
);
2312 return nfserr_jukebox
;
2314 /* Cases below refer to rfc 5661 section 18.35.4: */
2315 spin_lock(&nn
->client_lock
);
2316 conf
= find_confirmed_client_by_name(&exid
->clname
, nn
);
2318 bool creds_match
= same_creds(&conf
->cl_cred
, &rqstp
->rq_cred
);
2319 bool verfs_match
= same_verf(&verf
, &conf
->cl_verifier
);
2322 if (!clp_used_exchangeid(conf
)) { /* buggy client */
2323 status
= nfserr_inval
;
2326 if (!mach_creds_match(conf
, rqstp
)) {
2327 status
= nfserr_wrong_cred
;
2330 if (!creds_match
) { /* case 9 */
2331 status
= nfserr_perm
;
2334 if (!verfs_match
) { /* case 8 */
2335 status
= nfserr_not_same
;
2339 exid
->flags
|= EXCHGID4_FLAG_CONFIRMED_R
;
2342 if (!creds_match
) { /* case 3 */
2343 if (client_has_state(conf
)) {
2344 status
= nfserr_clid_inuse
;
2349 if (verfs_match
) { /* case 2 */
2350 conf
->cl_exchange_flags
|= EXCHGID4_FLAG_CONFIRMED_R
;
2353 /* case 5, client reboot */
2358 if (update
) { /* case 7 */
2359 status
= nfserr_noent
;
2363 unconf
= find_unconfirmed_client_by_name(&exid
->clname
, nn
);
2364 if (unconf
) /* case 4, possible retry or client restart */
2365 unhash_client_locked(unconf
);
2367 /* case 1 (normal case) */
2370 status
= mark_client_expired_locked(conf
);
2374 new->cl_minorversion
= cstate
->minorversion
;
2375 new->cl_mach_cred
= (exid
->spa_how
== SP4_MACH_CRED
);
2378 add_to_unconfirmed(new);
2381 exid
->clientid
.cl_boot
= conf
->cl_clientid
.cl_boot
;
2382 exid
->clientid
.cl_id
= conf
->cl_clientid
.cl_id
;
2384 exid
->seqid
= conf
->cl_cs_slot
.sl_seqid
+ 1;
2385 nfsd4_set_ex_flags(conf
, exid
);
2387 dprintk("nfsd4_exchange_id seqid %d flags %x\n",
2388 conf
->cl_cs_slot
.sl_seqid
, conf
->cl_exchange_flags
);
2392 spin_unlock(&nn
->client_lock
);
2396 expire_client(unconf
);
2401 check_slot_seqid(u32 seqid
, u32 slot_seqid
, int slot_inuse
)
2403 dprintk("%s enter. seqid %d slot_seqid %d\n", __func__
, seqid
,
2406 /* The slot is in use, and no response has been sent. */
2408 if (seqid
== slot_seqid
)
2409 return nfserr_jukebox
;
2411 return nfserr_seq_misordered
;
2413 /* Note unsigned 32-bit arithmetic handles wraparound: */
2414 if (likely(seqid
== slot_seqid
+ 1))
2416 if (seqid
== slot_seqid
)
2417 return nfserr_replay_cache
;
2418 return nfserr_seq_misordered
;
2422 * Cache the create session result into the create session single DRC
2423 * slot cache by saving the xdr structure. sl_seqid has been set.
2424 * Do this for solo or embedded create session operations.
2427 nfsd4_cache_create_session(struct nfsd4_create_session
*cr_ses
,
2428 struct nfsd4_clid_slot
*slot
, __be32 nfserr
)
2430 slot
->sl_status
= nfserr
;
2431 memcpy(&slot
->sl_cr_ses
, cr_ses
, sizeof(*cr_ses
));
2435 nfsd4_replay_create_session(struct nfsd4_create_session
*cr_ses
,
2436 struct nfsd4_clid_slot
*slot
)
2438 memcpy(cr_ses
, &slot
->sl_cr_ses
, sizeof(*cr_ses
));
2439 return slot
->sl_status
;
2442 #define NFSD_MIN_REQ_HDR_SEQ_SZ ((\
2443 2 * 2 + /* credential,verifier: AUTH_NULL, length 0 */ \
2444 1 + /* MIN tag is length with zero, only length */ \
2445 3 + /* version, opcount, opcode */ \
2446 XDR_QUADLEN(NFS4_MAX_SESSIONID_LEN) + \
2447 /* seqid, slotID, slotID, cache */ \
2448 4 ) * sizeof(__be32))
2450 #define NFSD_MIN_RESP_HDR_SEQ_SZ ((\
2451 2 + /* verifier: AUTH_NULL, length 0 */\
2453 1 + /* MIN tag is length with zero, only length */ \
2454 3 + /* opcount, opcode, opstatus*/ \
2455 XDR_QUADLEN(NFS4_MAX_SESSIONID_LEN) + \
2456 /* seqid, slotID, slotID, slotID, status */ \
2457 5 ) * sizeof(__be32))
2459 static __be32
check_forechannel_attrs(struct nfsd4_channel_attrs
*ca
, struct nfsd_net
*nn
)
2461 u32 maxrpc
= nn
->nfsd_serv
->sv_max_mesg
;
2463 if (ca
->maxreq_sz
< NFSD_MIN_REQ_HDR_SEQ_SZ
)
2464 return nfserr_toosmall
;
2465 if (ca
->maxresp_sz
< NFSD_MIN_RESP_HDR_SEQ_SZ
)
2466 return nfserr_toosmall
;
2467 ca
->headerpadsz
= 0;
2468 ca
->maxreq_sz
= min_t(u32
, ca
->maxreq_sz
, maxrpc
);
2469 ca
->maxresp_sz
= min_t(u32
, ca
->maxresp_sz
, maxrpc
);
2470 ca
->maxops
= min_t(u32
, ca
->maxops
, NFSD_MAX_OPS_PER_COMPOUND
);
2471 ca
->maxresp_cached
= min_t(u32
, ca
->maxresp_cached
,
2472 NFSD_SLOT_CACHE_SIZE
+ NFSD_MIN_HDR_SEQ_SZ
);
2473 ca
->maxreqs
= min_t(u32
, ca
->maxreqs
, NFSD_MAX_SLOTS_PER_SESSION
);
2475 * Note decreasing slot size below client's request may make it
2476 * difficult for client to function correctly, whereas
2477 * decreasing the number of slots will (just?) affect
2478 * performance. When short on memory we therefore prefer to
2479 * decrease number of slots instead of their size. Clients that
2480 * request larger slots than they need will get poor results:
2482 ca
->maxreqs
= nfsd4_get_drc_mem(ca
);
2484 return nfserr_jukebox
;
2489 #define NFSD_CB_MAX_REQ_SZ ((NFS4_enc_cb_recall_sz + \
2490 RPC_MAX_HEADER_WITH_AUTH) * sizeof(__be32))
2491 #define NFSD_CB_MAX_RESP_SZ ((NFS4_dec_cb_recall_sz + \
2492 RPC_MAX_REPHEADER_WITH_AUTH) * sizeof(__be32))
2494 static __be32
check_backchannel_attrs(struct nfsd4_channel_attrs
*ca
)
2496 ca
->headerpadsz
= 0;
2499 * These RPC_MAX_HEADER macros are overkill, especially since we
2500 * don't even do gss on the backchannel yet. But this is still
2501 * less than 1k. Tighten up this estimate in the unlikely event
2502 * it turns out to be a problem for some client:
2504 if (ca
->maxreq_sz
< NFSD_CB_MAX_REQ_SZ
)
2505 return nfserr_toosmall
;
2506 if (ca
->maxresp_sz
< NFSD_CB_MAX_RESP_SZ
)
2507 return nfserr_toosmall
;
2508 ca
->maxresp_cached
= 0;
2510 return nfserr_toosmall
;
2515 static __be32
nfsd4_check_cb_sec(struct nfsd4_cb_sec
*cbs
)
2517 switch (cbs
->flavor
) {
2523 * GSS case: the spec doesn't allow us to return this
2524 * error. But it also doesn't allow us not to support
2526 * I'd rather this fail hard than return some error the
2527 * client might think it can already handle:
2529 return nfserr_encr_alg_unsupp
;
2534 nfsd4_create_session(struct svc_rqst
*rqstp
,
2535 struct nfsd4_compound_state
*cstate
,
2536 struct nfsd4_create_session
*cr_ses
)
2538 struct sockaddr
*sa
= svc_addr(rqstp
);
2539 struct nfs4_client
*conf
, *unconf
;
2540 struct nfs4_client
*old
= NULL
;
2541 struct nfsd4_session
*new;
2542 struct nfsd4_conn
*conn
;
2543 struct nfsd4_clid_slot
*cs_slot
= NULL
;
2545 struct nfsd_net
*nn
= net_generic(SVC_NET(rqstp
), nfsd_net_id
);
2547 if (cr_ses
->flags
& ~SESSION4_FLAG_MASK_A
)
2548 return nfserr_inval
;
2549 status
= nfsd4_check_cb_sec(&cr_ses
->cb_sec
);
2552 status
= check_forechannel_attrs(&cr_ses
->fore_channel
, nn
);
2555 status
= check_backchannel_attrs(&cr_ses
->back_channel
);
2557 goto out_release_drc_mem
;
2558 status
= nfserr_jukebox
;
2559 new = alloc_session(&cr_ses
->fore_channel
, &cr_ses
->back_channel
);
2561 goto out_release_drc_mem
;
2562 conn
= alloc_conn_from_crses(rqstp
, cr_ses
);
2564 goto out_free_session
;
2566 spin_lock(&nn
->client_lock
);
2567 unconf
= find_unconfirmed_client(&cr_ses
->clientid
, true, nn
);
2568 conf
= find_confirmed_client(&cr_ses
->clientid
, true, nn
);
2569 WARN_ON_ONCE(conf
&& unconf
);
2572 status
= nfserr_wrong_cred
;
2573 if (!mach_creds_match(conf
, rqstp
))
2575 cs_slot
= &conf
->cl_cs_slot
;
2576 status
= check_slot_seqid(cr_ses
->seqid
, cs_slot
->sl_seqid
, 0);
2578 if (status
== nfserr_replay_cache
)
2579 status
= nfsd4_replay_create_session(cr_ses
, cs_slot
);
2582 } else if (unconf
) {
2583 if (!same_creds(&unconf
->cl_cred
, &rqstp
->rq_cred
) ||
2584 !rpc_cmp_addr(sa
, (struct sockaddr
*) &unconf
->cl_addr
)) {
2585 status
= nfserr_clid_inuse
;
2588 status
= nfserr_wrong_cred
;
2589 if (!mach_creds_match(unconf
, rqstp
))
2591 cs_slot
= &unconf
->cl_cs_slot
;
2592 status
= check_slot_seqid(cr_ses
->seqid
, cs_slot
->sl_seqid
, 0);
2594 /* an unconfirmed replay returns misordered */
2595 status
= nfserr_seq_misordered
;
2598 old
= find_confirmed_client_by_name(&unconf
->cl_name
, nn
);
2600 status
= mark_client_expired_locked(old
);
2606 move_to_confirmed(unconf
);
2609 status
= nfserr_stale_clientid
;
2614 * We do not support RDMA or persistent sessions
2616 cr_ses
->flags
&= ~SESSION4_PERSIST
;
2617 cr_ses
->flags
&= ~SESSION4_RDMA
;
2619 init_session(rqstp
, new, conf
, cr_ses
);
2620 nfsd4_get_session_locked(new);
2622 memcpy(cr_ses
->sessionid
.data
, new->se_sessionid
.data
,
2623 NFS4_MAX_SESSIONID_LEN
);
2624 cs_slot
->sl_seqid
++;
2625 cr_ses
->seqid
= cs_slot
->sl_seqid
;
2627 /* cache solo and embedded create sessions under the client_lock */
2628 nfsd4_cache_create_session(cr_ses
, cs_slot
, status
);
2629 spin_unlock(&nn
->client_lock
);
2630 /* init connection and backchannel */
2631 nfsd4_init_conn(rqstp
, conn
, new);
2632 nfsd4_put_session(new);
2637 spin_unlock(&nn
->client_lock
);
2642 __free_session(new);
2643 out_release_drc_mem
:
2644 nfsd4_put_drc_mem(&cr_ses
->fore_channel
);
2648 static __be32
nfsd4_map_bcts_dir(u32
*dir
)
2651 case NFS4_CDFC4_FORE
:
2652 case NFS4_CDFC4_BACK
:
2654 case NFS4_CDFC4_FORE_OR_BOTH
:
2655 case NFS4_CDFC4_BACK_OR_BOTH
:
2656 *dir
= NFS4_CDFC4_BOTH
;
2659 return nfserr_inval
;
2662 __be32
nfsd4_backchannel_ctl(struct svc_rqst
*rqstp
, struct nfsd4_compound_state
*cstate
, struct nfsd4_backchannel_ctl
*bc
)
2664 struct nfsd4_session
*session
= cstate
->session
;
2665 struct nfsd_net
*nn
= net_generic(SVC_NET(rqstp
), nfsd_net_id
);
2668 status
= nfsd4_check_cb_sec(&bc
->bc_cb_sec
);
2671 spin_lock(&nn
->client_lock
);
2672 session
->se_cb_prog
= bc
->bc_cb_program
;
2673 session
->se_cb_sec
= bc
->bc_cb_sec
;
2674 spin_unlock(&nn
->client_lock
);
2676 nfsd4_probe_callback(session
->se_client
);
2681 __be32
nfsd4_bind_conn_to_session(struct svc_rqst
*rqstp
,
2682 struct nfsd4_compound_state
*cstate
,
2683 struct nfsd4_bind_conn_to_session
*bcts
)
2686 struct nfsd4_conn
*conn
;
2687 struct nfsd4_session
*session
;
2688 struct net
*net
= SVC_NET(rqstp
);
2689 struct nfsd_net
*nn
= net_generic(net
, nfsd_net_id
);
2691 if (!nfsd4_last_compound_op(rqstp
))
2692 return nfserr_not_only_op
;
2693 spin_lock(&nn
->client_lock
);
2694 session
= find_in_sessionid_hashtbl(&bcts
->sessionid
, net
, &status
);
2695 spin_unlock(&nn
->client_lock
);
2697 goto out_no_session
;
2698 status
= nfserr_wrong_cred
;
2699 if (!mach_creds_match(session
->se_client
, rqstp
))
2701 status
= nfsd4_map_bcts_dir(&bcts
->dir
);
2704 conn
= alloc_conn(rqstp
, bcts
->dir
);
2705 status
= nfserr_jukebox
;
2708 nfsd4_init_conn(rqstp
, conn
, session
);
2711 nfsd4_put_session(session
);
2716 static bool nfsd4_compound_in_session(struct nfsd4_session
*session
, struct nfs4_sessionid
*sid
)
2720 return !memcmp(sid
, &session
->se_sessionid
, sizeof(*sid
));
2724 nfsd4_destroy_session(struct svc_rqst
*r
,
2725 struct nfsd4_compound_state
*cstate
,
2726 struct nfsd4_destroy_session
*sessionid
)
2728 struct nfsd4_session
*ses
;
2730 int ref_held_by_me
= 0;
2731 struct net
*net
= SVC_NET(r
);
2732 struct nfsd_net
*nn
= net_generic(net
, nfsd_net_id
);
2734 status
= nfserr_not_only_op
;
2735 if (nfsd4_compound_in_session(cstate
->session
, &sessionid
->sessionid
)) {
2736 if (!nfsd4_last_compound_op(r
))
2740 dump_sessionid(__func__
, &sessionid
->sessionid
);
2741 spin_lock(&nn
->client_lock
);
2742 ses
= find_in_sessionid_hashtbl(&sessionid
->sessionid
, net
, &status
);
2744 goto out_client_lock
;
2745 status
= nfserr_wrong_cred
;
2746 if (!mach_creds_match(ses
->se_client
, r
))
2747 goto out_put_session
;
2748 status
= mark_session_dead_locked(ses
, 1 + ref_held_by_me
);
2750 goto out_put_session
;
2751 unhash_session(ses
);
2752 spin_unlock(&nn
->client_lock
);
2754 nfsd4_probe_callback_sync(ses
->se_client
);
2756 spin_lock(&nn
->client_lock
);
2759 nfsd4_put_session_locked(ses
);
2761 spin_unlock(&nn
->client_lock
);
2766 static struct nfsd4_conn
*__nfsd4_find_conn(struct svc_xprt
*xpt
, struct nfsd4_session
*s
)
2768 struct nfsd4_conn
*c
;
2770 list_for_each_entry(c
, &s
->se_conns
, cn_persession
) {
2771 if (c
->cn_xprt
== xpt
) {
2778 static __be32
nfsd4_sequence_check_conn(struct nfsd4_conn
*new, struct nfsd4_session
*ses
)
2780 struct nfs4_client
*clp
= ses
->se_client
;
2781 struct nfsd4_conn
*c
;
2782 __be32 status
= nfs_ok
;
2785 spin_lock(&clp
->cl_lock
);
2786 c
= __nfsd4_find_conn(new->cn_xprt
, ses
);
2789 status
= nfserr_conn_not_bound_to_session
;
2790 if (clp
->cl_mach_cred
)
2792 __nfsd4_hash_conn(new, ses
);
2793 spin_unlock(&clp
->cl_lock
);
2794 ret
= nfsd4_register_conn(new);
2796 /* oops; xprt is already down: */
2797 nfsd4_conn_lost(&new->cn_xpt_user
);
2800 spin_unlock(&clp
->cl_lock
);
2805 static bool nfsd4_session_too_many_ops(struct svc_rqst
*rqstp
, struct nfsd4_session
*session
)
2807 struct nfsd4_compoundargs
*args
= rqstp
->rq_argp
;
2809 return args
->opcnt
> session
->se_fchannel
.maxops
;
2812 static bool nfsd4_request_too_big(struct svc_rqst
*rqstp
,
2813 struct nfsd4_session
*session
)
2815 struct xdr_buf
*xb
= &rqstp
->rq_arg
;
2817 return xb
->len
> session
->se_fchannel
.maxreq_sz
;
2821 nfsd4_sequence(struct svc_rqst
*rqstp
,
2822 struct nfsd4_compound_state
*cstate
,
2823 struct nfsd4_sequence
*seq
)
2825 struct nfsd4_compoundres
*resp
= rqstp
->rq_resp
;
2826 struct xdr_stream
*xdr
= &resp
->xdr
;
2827 struct nfsd4_session
*session
;
2828 struct nfs4_client
*clp
;
2829 struct nfsd4_slot
*slot
;
2830 struct nfsd4_conn
*conn
;
2833 struct net
*net
= SVC_NET(rqstp
);
2834 struct nfsd_net
*nn
= net_generic(net
, nfsd_net_id
);
2836 if (resp
->opcnt
!= 1)
2837 return nfserr_sequence_pos
;
2840 * Will be either used or freed by nfsd4_sequence_check_conn
2843 conn
= alloc_conn(rqstp
, NFS4_CDFC4_FORE
);
2845 return nfserr_jukebox
;
2847 spin_lock(&nn
->client_lock
);
2848 session
= find_in_sessionid_hashtbl(&seq
->sessionid
, net
, &status
);
2850 goto out_no_session
;
2851 clp
= session
->se_client
;
2853 status
= nfserr_too_many_ops
;
2854 if (nfsd4_session_too_many_ops(rqstp
, session
))
2855 goto out_put_session
;
2857 status
= nfserr_req_too_big
;
2858 if (nfsd4_request_too_big(rqstp
, session
))
2859 goto out_put_session
;
2861 status
= nfserr_badslot
;
2862 if (seq
->slotid
>= session
->se_fchannel
.maxreqs
)
2863 goto out_put_session
;
2865 slot
= session
->se_slots
[seq
->slotid
];
2866 dprintk("%s: slotid %d\n", __func__
, seq
->slotid
);
2868 /* We do not negotiate the number of slots yet, so set the
2869 * maxslots to the session maxreqs which is used to encode
2870 * sr_highest_slotid and the sr_target_slot id to maxslots */
2871 seq
->maxslots
= session
->se_fchannel
.maxreqs
;
2873 status
= check_slot_seqid(seq
->seqid
, slot
->sl_seqid
,
2874 slot
->sl_flags
& NFSD4_SLOT_INUSE
);
2875 if (status
== nfserr_replay_cache
) {
2876 status
= nfserr_seq_misordered
;
2877 if (!(slot
->sl_flags
& NFSD4_SLOT_INITIALIZED
))
2878 goto out_put_session
;
2879 cstate
->slot
= slot
;
2880 cstate
->session
= session
;
2882 /* Return the cached reply status and set cstate->status
2883 * for nfsd4_proc_compound processing */
2884 status
= nfsd4_replay_cache_entry(resp
, seq
);
2885 cstate
->status
= nfserr_replay_cache
;
2889 goto out_put_session
;
2891 status
= nfsd4_sequence_check_conn(conn
, session
);
2894 goto out_put_session
;
2896 buflen
= (seq
->cachethis
) ?
2897 session
->se_fchannel
.maxresp_cached
:
2898 session
->se_fchannel
.maxresp_sz
;
2899 status
= (seq
->cachethis
) ? nfserr_rep_too_big_to_cache
:
2901 if (xdr_restrict_buflen(xdr
, buflen
- rqstp
->rq_auth_slack
))
2902 goto out_put_session
;
2903 svc_reserve(rqstp
, buflen
);
2906 /* Success! bump slot seqid */
2907 slot
->sl_seqid
= seq
->seqid
;
2908 slot
->sl_flags
|= NFSD4_SLOT_INUSE
;
2910 slot
->sl_flags
|= NFSD4_SLOT_CACHETHIS
;
2912 slot
->sl_flags
&= ~NFSD4_SLOT_CACHETHIS
;
2914 cstate
->slot
= slot
;
2915 cstate
->session
= session
;
2919 switch (clp
->cl_cb_state
) {
2921 seq
->status_flags
= SEQ4_STATUS_CB_PATH_DOWN
;
2923 case NFSD4_CB_FAULT
:
2924 seq
->status_flags
= SEQ4_STATUS_BACKCHANNEL_FAULT
;
2927 seq
->status_flags
= 0;
2929 if (!list_empty(&clp
->cl_revoked
))
2930 seq
->status_flags
|= SEQ4_STATUS_RECALLABLE_STATE_REVOKED
;
2934 spin_unlock(&nn
->client_lock
);
2937 nfsd4_put_session_locked(session
);
2938 goto out_no_session
;
2942 nfsd4_sequence_done(struct nfsd4_compoundres
*resp
)
2944 struct nfsd4_compound_state
*cs
= &resp
->cstate
;
2946 if (nfsd4_has_session(cs
)) {
2947 if (cs
->status
!= nfserr_replay_cache
) {
2948 nfsd4_store_cache_entry(resp
);
2949 cs
->slot
->sl_flags
&= ~NFSD4_SLOT_INUSE
;
2951 /* Drop session reference that was taken in nfsd4_sequence() */
2952 nfsd4_put_session(cs
->session
);
2954 put_client_renew(cs
->clp
);
2958 nfsd4_destroy_clientid(struct svc_rqst
*rqstp
, struct nfsd4_compound_state
*cstate
, struct nfsd4_destroy_clientid
*dc
)
2960 struct nfs4_client
*conf
, *unconf
;
2961 struct nfs4_client
*clp
= NULL
;
2963 struct nfsd_net
*nn
= net_generic(SVC_NET(rqstp
), nfsd_net_id
);
2965 spin_lock(&nn
->client_lock
);
2966 unconf
= find_unconfirmed_client(&dc
->clientid
, true, nn
);
2967 conf
= find_confirmed_client(&dc
->clientid
, true, nn
);
2968 WARN_ON_ONCE(conf
&& unconf
);
2971 if (client_has_state(conf
)) {
2972 status
= nfserr_clientid_busy
;
2975 status
= mark_client_expired_locked(conf
);
2982 status
= nfserr_stale_clientid
;
2985 if (!mach_creds_match(clp
, rqstp
)) {
2987 status
= nfserr_wrong_cred
;
2990 unhash_client_locked(clp
);
2992 spin_unlock(&nn
->client_lock
);
2999 nfsd4_reclaim_complete(struct svc_rqst
*rqstp
, struct nfsd4_compound_state
*cstate
, struct nfsd4_reclaim_complete
*rc
)
3003 if (rc
->rca_one_fs
) {
3004 if (!cstate
->current_fh
.fh_dentry
)
3005 return nfserr_nofilehandle
;
3007 * We don't take advantage of the rca_one_fs case.
3008 * That's OK, it's optional, we can safely ignore it.
3013 status
= nfserr_complete_already
;
3014 if (test_and_set_bit(NFSD4_CLIENT_RECLAIM_COMPLETE
,
3015 &cstate
->session
->se_client
->cl_flags
))
3018 status
= nfserr_stale_clientid
;
3019 if (is_client_expired(cstate
->session
->se_client
))
3021 * The following error isn't really legal.
3022 * But we only get here if the client just explicitly
3023 * destroyed the client. Surely it no longer cares what
3024 * error it gets back on an operation for the dead
3030 nfsd4_client_record_create(cstate
->session
->se_client
);
3036 nfsd4_setclientid(struct svc_rqst
*rqstp
, struct nfsd4_compound_state
*cstate
,
3037 struct nfsd4_setclientid
*setclid
)
3039 struct xdr_netobj clname
= setclid
->se_name
;
3040 nfs4_verifier clverifier
= setclid
->se_verf
;
3041 struct nfs4_client
*conf
, *new;
3042 struct nfs4_client
*unconf
= NULL
;
3044 struct nfsd_net
*nn
= net_generic(SVC_NET(rqstp
), nfsd_net_id
);
3046 new = create_client(clname
, rqstp
, &clverifier
);
3048 return nfserr_jukebox
;
3049 /* Cases below refer to rfc 3530 section 14.2.33: */
3050 spin_lock(&nn
->client_lock
);
3051 conf
= find_confirmed_client_by_name(&clname
, nn
);
3054 status
= nfserr_clid_inuse
;
3055 if (clp_used_exchangeid(conf
))
3057 if (!same_creds(&conf
->cl_cred
, &rqstp
->rq_cred
)) {
3058 char addr_str
[INET6_ADDRSTRLEN
];
3059 rpc_ntop((struct sockaddr
*) &conf
->cl_addr
, addr_str
,
3061 dprintk("NFSD: setclientid: string in use by client "
3062 "at %s\n", addr_str
);
3066 unconf
= find_unconfirmed_client_by_name(&clname
, nn
);
3068 unhash_client_locked(unconf
);
3069 if (conf
&& same_verf(&conf
->cl_verifier
, &clverifier
)) {
3070 /* case 1: probable callback update */
3071 copy_clid(new, conf
);
3072 gen_confirm(new, nn
);
3073 } else /* case 4 (new client) or cases 2, 3 (client reboot): */
3075 new->cl_minorversion
= 0;
3076 gen_callback(new, setclid
, rqstp
);
3077 add_to_unconfirmed(new);
3078 setclid
->se_clientid
.cl_boot
= new->cl_clientid
.cl_boot
;
3079 setclid
->se_clientid
.cl_id
= new->cl_clientid
.cl_id
;
3080 memcpy(setclid
->se_confirm
.data
, new->cl_confirm
.data
, sizeof(setclid
->se_confirm
.data
));
3084 spin_unlock(&nn
->client_lock
);
3088 expire_client(unconf
);
3094 nfsd4_setclientid_confirm(struct svc_rqst
*rqstp
,
3095 struct nfsd4_compound_state
*cstate
,
3096 struct nfsd4_setclientid_confirm
*setclientid_confirm
)
3098 struct nfs4_client
*conf
, *unconf
;
3099 struct nfs4_client
*old
= NULL
;
3100 nfs4_verifier confirm
= setclientid_confirm
->sc_confirm
;
3101 clientid_t
* clid
= &setclientid_confirm
->sc_clientid
;
3103 struct nfsd_net
*nn
= net_generic(SVC_NET(rqstp
), nfsd_net_id
);
3105 if (STALE_CLIENTID(clid
, nn
))
3106 return nfserr_stale_clientid
;
3108 spin_lock(&nn
->client_lock
);
3109 conf
= find_confirmed_client(clid
, false, nn
);
3110 unconf
= find_unconfirmed_client(clid
, false, nn
);
3112 * We try hard to give out unique clientid's, so if we get an
3113 * attempt to confirm the same clientid with a different cred,
3114 * the client may be buggy; this should never happen.
3116 * Nevertheless, RFC 7530 recommends INUSE for this case:
3118 status
= nfserr_clid_inuse
;
3119 if (unconf
&& !same_creds(&unconf
->cl_cred
, &rqstp
->rq_cred
))
3121 if (conf
&& !same_creds(&conf
->cl_cred
, &rqstp
->rq_cred
))
3123 /* cases below refer to rfc 3530 section 14.2.34: */
3124 if (!unconf
|| !same_verf(&confirm
, &unconf
->cl_confirm
)) {
3125 if (conf
&& !unconf
) /* case 2: probable retransmit */
3127 else /* case 4: client hasn't noticed we rebooted yet? */
3128 status
= nfserr_stale_clientid
;
3132 if (conf
) { /* case 1: callback update */
3134 unhash_client_locked(old
);
3135 nfsd4_change_callback(conf
, &unconf
->cl_cb_conn
);
3136 } else { /* case 3: normal case; new or rebooted client */
3137 old
= find_confirmed_client_by_name(&unconf
->cl_name
, nn
);
3139 status
= mark_client_expired_locked(old
);
3145 move_to_confirmed(unconf
);
3148 get_client_locked(conf
);
3149 spin_unlock(&nn
->client_lock
);
3150 nfsd4_probe_callback(conf
);
3151 spin_lock(&nn
->client_lock
);
3152 put_client_renew_locked(conf
);
3154 spin_unlock(&nn
->client_lock
);
3160 static struct nfs4_file
*nfsd4_alloc_file(void)
3162 return kmem_cache_alloc(file_slab
, GFP_KERNEL
);
3165 /* OPEN Share state helper functions */
3166 static void nfsd4_init_file(struct knfsd_fh
*fh
, unsigned int hashval
,
3167 struct nfs4_file
*fp
)
3169 lockdep_assert_held(&state_lock
);
3171 atomic_set(&fp
->fi_ref
, 1);
3172 spin_lock_init(&fp
->fi_lock
);
3173 INIT_LIST_HEAD(&fp
->fi_stateids
);
3174 INIT_LIST_HEAD(&fp
->fi_delegations
);
3175 INIT_LIST_HEAD(&fp
->fi_clnt_odstate
);
3176 fh_copy_shallow(&fp
->fi_fhandle
, fh
);
3177 fp
->fi_deleg_file
= NULL
;
3178 fp
->fi_had_conflict
= false;
3179 fp
->fi_share_deny
= 0;
3180 memset(fp
->fi_fds
, 0, sizeof(fp
->fi_fds
));
3181 memset(fp
->fi_access
, 0, sizeof(fp
->fi_access
));
3182 #ifdef CONFIG_NFSD_PNFS
3183 INIT_LIST_HEAD(&fp
->fi_lo_states
);
3184 atomic_set(&fp
->fi_lo_recalls
, 0);
3186 hlist_add_head_rcu(&fp
->fi_hash
, &file_hashtbl
[hashval
]);
3190 nfsd4_free_slabs(void)
3192 kmem_cache_destroy(odstate_slab
);
3193 kmem_cache_destroy(openowner_slab
);
3194 kmem_cache_destroy(lockowner_slab
);
3195 kmem_cache_destroy(file_slab
);
3196 kmem_cache_destroy(stateid_slab
);
3197 kmem_cache_destroy(deleg_slab
);
3201 nfsd4_init_slabs(void)
3203 openowner_slab
= kmem_cache_create("nfsd4_openowners",
3204 sizeof(struct nfs4_openowner
), 0, 0, NULL
);
3205 if (openowner_slab
== NULL
)
3207 lockowner_slab
= kmem_cache_create("nfsd4_lockowners",
3208 sizeof(struct nfs4_lockowner
), 0, 0, NULL
);
3209 if (lockowner_slab
== NULL
)
3210 goto out_free_openowner_slab
;
3211 file_slab
= kmem_cache_create("nfsd4_files",
3212 sizeof(struct nfs4_file
), 0, 0, NULL
);
3213 if (file_slab
== NULL
)
3214 goto out_free_lockowner_slab
;
3215 stateid_slab
= kmem_cache_create("nfsd4_stateids",
3216 sizeof(struct nfs4_ol_stateid
), 0, 0, NULL
);
3217 if (stateid_slab
== NULL
)
3218 goto out_free_file_slab
;
3219 deleg_slab
= kmem_cache_create("nfsd4_delegations",
3220 sizeof(struct nfs4_delegation
), 0, 0, NULL
);
3221 if (deleg_slab
== NULL
)
3222 goto out_free_stateid_slab
;
3223 odstate_slab
= kmem_cache_create("nfsd4_odstate",
3224 sizeof(struct nfs4_clnt_odstate
), 0, 0, NULL
);
3225 if (odstate_slab
== NULL
)
3226 goto out_free_deleg_slab
;
3229 out_free_deleg_slab
:
3230 kmem_cache_destroy(deleg_slab
);
3231 out_free_stateid_slab
:
3232 kmem_cache_destroy(stateid_slab
);
3234 kmem_cache_destroy(file_slab
);
3235 out_free_lockowner_slab
:
3236 kmem_cache_destroy(lockowner_slab
);
3237 out_free_openowner_slab
:
3238 kmem_cache_destroy(openowner_slab
);
3240 dprintk("nfsd4: out of memory while initializing nfsv4\n");
3244 static void init_nfs4_replay(struct nfs4_replay
*rp
)
3246 rp
->rp_status
= nfserr_serverfault
;
3248 rp
->rp_buf
= rp
->rp_ibuf
;
3249 mutex_init(&rp
->rp_mutex
);
3252 static void nfsd4_cstate_assign_replay(struct nfsd4_compound_state
*cstate
,
3253 struct nfs4_stateowner
*so
)
3255 if (!nfsd4_has_session(cstate
)) {
3256 mutex_lock(&so
->so_replay
.rp_mutex
);
3257 cstate
->replay_owner
= nfs4_get_stateowner(so
);
3261 void nfsd4_cstate_clear_replay(struct nfsd4_compound_state
*cstate
)
3263 struct nfs4_stateowner
*so
= cstate
->replay_owner
;
3266 cstate
->replay_owner
= NULL
;
3267 mutex_unlock(&so
->so_replay
.rp_mutex
);
3268 nfs4_put_stateowner(so
);
3272 static inline void *alloc_stateowner(struct kmem_cache
*slab
, struct xdr_netobj
*owner
, struct nfs4_client
*clp
)
3274 struct nfs4_stateowner
*sop
;
3276 sop
= kmem_cache_alloc(slab
, GFP_KERNEL
);
3280 sop
->so_owner
.data
= kmemdup(owner
->data
, owner
->len
, GFP_KERNEL
);
3281 if (!sop
->so_owner
.data
) {
3282 kmem_cache_free(slab
, sop
);
3285 sop
->so_owner
.len
= owner
->len
;
3287 INIT_LIST_HEAD(&sop
->so_stateids
);
3288 sop
->so_client
= clp
;
3289 init_nfs4_replay(&sop
->so_replay
);
3290 atomic_set(&sop
->so_count
, 1);
3294 static void hash_openowner(struct nfs4_openowner
*oo
, struct nfs4_client
*clp
, unsigned int strhashval
)
3296 lockdep_assert_held(&clp
->cl_lock
);
3298 list_add(&oo
->oo_owner
.so_strhash
,
3299 &clp
->cl_ownerstr_hashtbl
[strhashval
]);
3300 list_add(&oo
->oo_perclient
, &clp
->cl_openowners
);
3303 static void nfs4_unhash_openowner(struct nfs4_stateowner
*so
)
3305 unhash_openowner_locked(openowner(so
));
3308 static void nfs4_free_openowner(struct nfs4_stateowner
*so
)
3310 struct nfs4_openowner
*oo
= openowner(so
);
3312 kmem_cache_free(openowner_slab
, oo
);
3315 static const struct nfs4_stateowner_operations openowner_ops
= {
3316 .so_unhash
= nfs4_unhash_openowner
,
3317 .so_free
= nfs4_free_openowner
,
3320 static struct nfs4_openowner
*
3321 alloc_init_open_stateowner(unsigned int strhashval
, struct nfsd4_open
*open
,
3322 struct nfsd4_compound_state
*cstate
)
3324 struct nfs4_client
*clp
= cstate
->clp
;
3325 struct nfs4_openowner
*oo
, *ret
;
3327 oo
= alloc_stateowner(openowner_slab
, &open
->op_owner
, clp
);
3330 oo
->oo_owner
.so_ops
= &openowner_ops
;
3331 oo
->oo_owner
.so_is_open_owner
= 1;
3332 oo
->oo_owner
.so_seqid
= open
->op_seqid
;
3334 if (nfsd4_has_session(cstate
))
3335 oo
->oo_flags
|= NFS4_OO_CONFIRMED
;
3337 oo
->oo_last_closed_stid
= NULL
;
3338 INIT_LIST_HEAD(&oo
->oo_close_lru
);
3339 spin_lock(&clp
->cl_lock
);
3340 ret
= find_openstateowner_str_locked(strhashval
, open
, clp
);
3342 hash_openowner(oo
, clp
, strhashval
);
3345 nfs4_free_stateowner(&oo
->oo_owner
);
3347 spin_unlock(&clp
->cl_lock
);
3351 static void init_open_stateid(struct nfs4_ol_stateid
*stp
, struct nfs4_file
*fp
, struct nfsd4_open
*open
) {
3352 struct nfs4_openowner
*oo
= open
->op_openowner
;
3354 atomic_inc(&stp
->st_stid
.sc_count
);
3355 stp
->st_stid
.sc_type
= NFS4_OPEN_STID
;
3356 INIT_LIST_HEAD(&stp
->st_locks
);
3357 stp
->st_stateowner
= nfs4_get_stateowner(&oo
->oo_owner
);
3359 stp
->st_stid
.sc_file
= fp
;
3360 stp
->st_access_bmap
= 0;
3361 stp
->st_deny_bmap
= 0;
3362 stp
->st_openstp
= NULL
;
3363 spin_lock(&oo
->oo_owner
.so_client
->cl_lock
);
3364 list_add(&stp
->st_perstateowner
, &oo
->oo_owner
.so_stateids
);
3365 spin_lock(&fp
->fi_lock
);
3366 list_add(&stp
->st_perfile
, &fp
->fi_stateids
);
3367 spin_unlock(&fp
->fi_lock
);
3368 spin_unlock(&oo
->oo_owner
.so_client
->cl_lock
);
3372 * In the 4.0 case we need to keep the owners around a little while to handle
3373 * CLOSE replay. We still do need to release any file access that is held by
3374 * them before returning however.
3377 move_to_close_lru(struct nfs4_ol_stateid
*s
, struct net
*net
)
3379 struct nfs4_ol_stateid
*last
;
3380 struct nfs4_openowner
*oo
= openowner(s
->st_stateowner
);
3381 struct nfsd_net
*nn
= net_generic(s
->st_stid
.sc_client
->net
,
3384 dprintk("NFSD: move_to_close_lru nfs4_openowner %p\n", oo
);
3387 * We know that we hold one reference via nfsd4_close, and another
3388 * "persistent" reference for the client. If the refcount is higher
3389 * than 2, then there are still calls in progress that are using this
3390 * stateid. We can't put the sc_file reference until they are finished.
3391 * Wait for the refcount to drop to 2. Since it has been unhashed,
3392 * there should be no danger of the refcount going back up again at
3395 wait_event(close_wq
, atomic_read(&s
->st_stid
.sc_count
) == 2);
3397 release_all_access(s
);
3398 if (s
->st_stid
.sc_file
) {
3399 put_nfs4_file(s
->st_stid
.sc_file
);
3400 s
->st_stid
.sc_file
= NULL
;
3403 spin_lock(&nn
->client_lock
);
3404 last
= oo
->oo_last_closed_stid
;
3405 oo
->oo_last_closed_stid
= s
;
3406 list_move_tail(&oo
->oo_close_lru
, &nn
->close_lru
);
3407 oo
->oo_time
= get_seconds();
3408 spin_unlock(&nn
->client_lock
);
3410 nfs4_put_stid(&last
->st_stid
);
3413 /* search file_hashtbl[] for file */
3414 static struct nfs4_file
*
3415 find_file_locked(struct knfsd_fh
*fh
, unsigned int hashval
)
3417 struct nfs4_file
*fp
;
3419 hlist_for_each_entry_rcu(fp
, &file_hashtbl
[hashval
], fi_hash
) {
3420 if (fh_match(&fp
->fi_fhandle
, fh
)) {
3421 if (atomic_inc_not_zero(&fp
->fi_ref
))
3429 find_file(struct knfsd_fh
*fh
)
3431 struct nfs4_file
*fp
;
3432 unsigned int hashval
= file_hashval(fh
);
3435 fp
= find_file_locked(fh
, hashval
);
3440 static struct nfs4_file
*
3441 find_or_add_file(struct nfs4_file
*new, struct knfsd_fh
*fh
)
3443 struct nfs4_file
*fp
;
3444 unsigned int hashval
= file_hashval(fh
);
3447 fp
= find_file_locked(fh
, hashval
);
3452 spin_lock(&state_lock
);
3453 fp
= find_file_locked(fh
, hashval
);
3454 if (likely(fp
== NULL
)) {
3455 nfsd4_init_file(fh
, hashval
, new);
3458 spin_unlock(&state_lock
);
3464 * Called to check deny when READ with all zero stateid or
3465 * WRITE with all zero or all one stateid
3468 nfs4_share_conflict(struct svc_fh
*current_fh
, unsigned int deny_type
)
3470 struct nfs4_file
*fp
;
3471 __be32 ret
= nfs_ok
;
3473 fp
= find_file(¤t_fh
->fh_handle
);
3476 /* Check for conflicting share reservations */
3477 spin_lock(&fp
->fi_lock
);
3478 if (fp
->fi_share_deny
& deny_type
)
3479 ret
= nfserr_locked
;
3480 spin_unlock(&fp
->fi_lock
);
3485 static void nfsd4_cb_recall_prepare(struct nfsd4_callback
*cb
)
3487 struct nfs4_delegation
*dp
= cb_to_delegation(cb
);
3488 struct nfsd_net
*nn
= net_generic(dp
->dl_stid
.sc_client
->net
,
3491 block_delegations(&dp
->dl_stid
.sc_file
->fi_fhandle
);
3494 * We can't do this in nfsd_break_deleg_cb because it is
3495 * already holding inode->i_lock.
3497 * If the dl_time != 0, then we know that it has already been
3498 * queued for a lease break. Don't queue it again.
3500 spin_lock(&state_lock
);
3501 if (dp
->dl_time
== 0) {
3502 dp
->dl_time
= get_seconds();
3503 list_add_tail(&dp
->dl_recall_lru
, &nn
->del_recall_lru
);
3505 spin_unlock(&state_lock
);
3508 static int nfsd4_cb_recall_done(struct nfsd4_callback
*cb
,
3509 struct rpc_task
*task
)
3511 struct nfs4_delegation
*dp
= cb_to_delegation(cb
);
3513 if (dp
->dl_stid
.sc_type
== NFS4_CLOSED_DELEG_STID
)
3516 switch (task
->tk_status
) {
3520 case -NFS4ERR_BAD_STATEID
:
3522 * Race: client probably got cb_recall before open reply
3523 * granting delegation.
3525 if (dp
->dl_retries
--) {
3526 rpc_delay(task
, 2 * HZ
);
3535 static void nfsd4_cb_recall_release(struct nfsd4_callback
*cb
)
3537 struct nfs4_delegation
*dp
= cb_to_delegation(cb
);
3539 nfs4_put_stid(&dp
->dl_stid
);
3542 static struct nfsd4_callback_ops nfsd4_cb_recall_ops
= {
3543 .prepare
= nfsd4_cb_recall_prepare
,
3544 .done
= nfsd4_cb_recall_done
,
3545 .release
= nfsd4_cb_recall_release
,
3548 static void nfsd_break_one_deleg(struct nfs4_delegation
*dp
)
3551 * We're assuming the state code never drops its reference
3552 * without first removing the lease. Since we're in this lease
3553 * callback (and since the lease code is serialized by the kernel
3554 * lock) we know the server hasn't removed the lease yet, we know
3555 * it's safe to take a reference.
3557 atomic_inc(&dp
->dl_stid
.sc_count
);
3558 nfsd4_run_cb(&dp
->dl_recall
);
3561 /* Called from break_lease() with i_lock held. */
3563 nfsd_break_deleg_cb(struct file_lock
*fl
)
3566 struct nfs4_file
*fp
= (struct nfs4_file
*)fl
->fl_owner
;
3567 struct nfs4_delegation
*dp
;
3570 WARN(1, "(%p)->fl_owner NULL\n", fl
);
3573 if (fp
->fi_had_conflict
) {
3574 WARN(1, "duplicate break on %p\n", fp
);
3578 * We don't want the locks code to timeout the lease for us;
3579 * we'll remove it ourself if a delegation isn't returned
3582 fl
->fl_break_time
= 0;
3584 spin_lock(&fp
->fi_lock
);
3585 fp
->fi_had_conflict
= true;
3587 * If there are no delegations on the list, then return true
3588 * so that the lease code will go ahead and delete it.
3590 if (list_empty(&fp
->fi_delegations
))
3593 list_for_each_entry(dp
, &fp
->fi_delegations
, dl_perfile
)
3594 nfsd_break_one_deleg(dp
);
3595 spin_unlock(&fp
->fi_lock
);
3600 nfsd_change_deleg_cb(struct file_lock
*onlist
, int arg
,
3601 struct list_head
*dispose
)
3604 return lease_modify(onlist
, arg
, dispose
);
3609 static const struct lock_manager_operations nfsd_lease_mng_ops
= {
3610 .lm_break
= nfsd_break_deleg_cb
,
3611 .lm_change
= nfsd_change_deleg_cb
,
3614 static __be32
nfsd4_check_seqid(struct nfsd4_compound_state
*cstate
, struct nfs4_stateowner
*so
, u32 seqid
)
3616 if (nfsd4_has_session(cstate
))
3618 if (seqid
== so
->so_seqid
- 1)
3619 return nfserr_replay_me
;
3620 if (seqid
== so
->so_seqid
)
3622 return nfserr_bad_seqid
;
3625 static __be32
lookup_clientid(clientid_t
*clid
,
3626 struct nfsd4_compound_state
*cstate
,
3627 struct nfsd_net
*nn
)
3629 struct nfs4_client
*found
;
3632 found
= cstate
->clp
;
3633 if (!same_clid(&found
->cl_clientid
, clid
))
3634 return nfserr_stale_clientid
;
3638 if (STALE_CLIENTID(clid
, nn
))
3639 return nfserr_stale_clientid
;
3642 * For v4.1+ we get the client in the SEQUENCE op. If we don't have one
3643 * cached already then we know this is for is for v4.0 and "sessions"
3646 WARN_ON_ONCE(cstate
->session
);
3647 spin_lock(&nn
->client_lock
);
3648 found
= find_confirmed_client(clid
, false, nn
);
3650 spin_unlock(&nn
->client_lock
);
3651 return nfserr_expired
;
3653 atomic_inc(&found
->cl_refcount
);
3654 spin_unlock(&nn
->client_lock
);
3656 /* Cache the nfs4_client in cstate! */
3657 cstate
->clp
= found
;
3662 nfsd4_process_open1(struct nfsd4_compound_state
*cstate
,
3663 struct nfsd4_open
*open
, struct nfsd_net
*nn
)
3665 clientid_t
*clientid
= &open
->op_clientid
;
3666 struct nfs4_client
*clp
= NULL
;
3667 unsigned int strhashval
;
3668 struct nfs4_openowner
*oo
= NULL
;
3671 if (STALE_CLIENTID(&open
->op_clientid
, nn
))
3672 return nfserr_stale_clientid
;
3674 * In case we need it later, after we've already created the
3675 * file and don't want to risk a further failure:
3677 open
->op_file
= nfsd4_alloc_file();
3678 if (open
->op_file
== NULL
)
3679 return nfserr_jukebox
;
3681 status
= lookup_clientid(clientid
, cstate
, nn
);
3686 strhashval
= ownerstr_hashval(&open
->op_owner
);
3687 oo
= find_openstateowner_str(strhashval
, open
, clp
);
3688 open
->op_openowner
= oo
;
3692 if (!(oo
->oo_flags
& NFS4_OO_CONFIRMED
)) {
3693 /* Replace unconfirmed owners without checking for replay. */
3694 release_openowner(oo
);
3695 open
->op_openowner
= NULL
;
3698 status
= nfsd4_check_seqid(cstate
, &oo
->oo_owner
, open
->op_seqid
);
3703 oo
= alloc_init_open_stateowner(strhashval
, open
, cstate
);
3705 return nfserr_jukebox
;
3706 open
->op_openowner
= oo
;
3708 open
->op_stp
= nfs4_alloc_open_stateid(clp
);
3710 return nfserr_jukebox
;
3712 if (nfsd4_has_session(cstate
) &&
3713 (cstate
->current_fh
.fh_export
->ex_flags
& NFSEXP_PNFS
)) {
3714 open
->op_odstate
= alloc_clnt_odstate(clp
);
3715 if (!open
->op_odstate
)
3716 return nfserr_jukebox
;
3722 static inline __be32
3723 nfs4_check_delegmode(struct nfs4_delegation
*dp
, int flags
)
3725 if ((flags
& WR_STATE
) && (dp
->dl_type
== NFS4_OPEN_DELEGATE_READ
))
3726 return nfserr_openmode
;
3731 static int share_access_to_flags(u32 share_access
)
3733 return share_access
== NFS4_SHARE_ACCESS_READ
? RD_STATE
: WR_STATE
;
3736 static struct nfs4_delegation
*find_deleg_stateid(struct nfs4_client
*cl
, stateid_t
*s
)
3738 struct nfs4_stid
*ret
;
3740 ret
= find_stateid_by_type(cl
, s
, NFS4_DELEG_STID
);
3743 return delegstateid(ret
);
3746 static bool nfsd4_is_deleg_cur(struct nfsd4_open
*open
)
3748 return open
->op_claim_type
== NFS4_OPEN_CLAIM_DELEGATE_CUR
||
3749 open
->op_claim_type
== NFS4_OPEN_CLAIM_DELEG_CUR_FH
;
3753 nfs4_check_deleg(struct nfs4_client
*cl
, struct nfsd4_open
*open
,
3754 struct nfs4_delegation
**dp
)
3757 __be32 status
= nfserr_bad_stateid
;
3758 struct nfs4_delegation
*deleg
;
3760 deleg
= find_deleg_stateid(cl
, &open
->op_delegate_stateid
);
3763 flags
= share_access_to_flags(open
->op_share_access
);
3764 status
= nfs4_check_delegmode(deleg
, flags
);
3766 nfs4_put_stid(&deleg
->dl_stid
);
3771 if (!nfsd4_is_deleg_cur(open
))
3775 open
->op_openowner
->oo_flags
|= NFS4_OO_CONFIRMED
;
3779 static struct nfs4_ol_stateid
*
3780 nfsd4_find_existing_open(struct nfs4_file
*fp
, struct nfsd4_open
*open
)
3782 struct nfs4_ol_stateid
*local
, *ret
= NULL
;
3783 struct nfs4_openowner
*oo
= open
->op_openowner
;
3785 spin_lock(&fp
->fi_lock
);
3786 list_for_each_entry(local
, &fp
->fi_stateids
, st_perfile
) {
3787 /* ignore lock owners */
3788 if (local
->st_stateowner
->so_is_open_owner
== 0)
3790 if (local
->st_stateowner
== &oo
->oo_owner
) {
3792 atomic_inc(&ret
->st_stid
.sc_count
);
3796 spin_unlock(&fp
->fi_lock
);
3800 static inline int nfs4_access_to_access(u32 nfs4_access
)
3804 if (nfs4_access
& NFS4_SHARE_ACCESS_READ
)
3805 flags
|= NFSD_MAY_READ
;
3806 if (nfs4_access
& NFS4_SHARE_ACCESS_WRITE
)
3807 flags
|= NFSD_MAY_WRITE
;
3811 static inline __be32
3812 nfsd4_truncate(struct svc_rqst
*rqstp
, struct svc_fh
*fh
,
3813 struct nfsd4_open
*open
)
3815 struct iattr iattr
= {
3816 .ia_valid
= ATTR_SIZE
,
3819 if (!open
->op_truncate
)
3821 if (!(open
->op_share_access
& NFS4_SHARE_ACCESS_WRITE
))
3822 return nfserr_inval
;
3823 return nfsd_setattr(rqstp
, fh
, &iattr
, 0, (time_t)0);
3826 static __be32
nfs4_get_vfs_file(struct svc_rqst
*rqstp
, struct nfs4_file
*fp
,
3827 struct svc_fh
*cur_fh
, struct nfs4_ol_stateid
*stp
,
3828 struct nfsd4_open
*open
)
3830 struct file
*filp
= NULL
;
3832 int oflag
= nfs4_access_to_omode(open
->op_share_access
);
3833 int access
= nfs4_access_to_access(open
->op_share_access
);
3834 unsigned char old_access_bmap
, old_deny_bmap
;
3836 spin_lock(&fp
->fi_lock
);
3839 * Are we trying to set a deny mode that would conflict with
3842 status
= nfs4_file_check_deny(fp
, open
->op_share_deny
);
3843 if (status
!= nfs_ok
) {
3844 spin_unlock(&fp
->fi_lock
);
3848 /* set access to the file */
3849 status
= nfs4_file_get_access(fp
, open
->op_share_access
);
3850 if (status
!= nfs_ok
) {
3851 spin_unlock(&fp
->fi_lock
);
3855 /* Set access bits in stateid */
3856 old_access_bmap
= stp
->st_access_bmap
;
3857 set_access(open
->op_share_access
, stp
);
3859 /* Set new deny mask */
3860 old_deny_bmap
= stp
->st_deny_bmap
;
3861 set_deny(open
->op_share_deny
, stp
);
3862 fp
->fi_share_deny
|= (open
->op_share_deny
& NFS4_SHARE_DENY_BOTH
);
3864 if (!fp
->fi_fds
[oflag
]) {
3865 spin_unlock(&fp
->fi_lock
);
3866 status
= nfsd_open(rqstp
, cur_fh
, S_IFREG
, access
, &filp
);
3868 goto out_put_access
;
3869 spin_lock(&fp
->fi_lock
);
3870 if (!fp
->fi_fds
[oflag
]) {
3871 fp
->fi_fds
[oflag
] = filp
;
3875 spin_unlock(&fp
->fi_lock
);
3879 status
= nfsd4_truncate(rqstp
, cur_fh
, open
);
3881 goto out_put_access
;
3885 stp
->st_access_bmap
= old_access_bmap
;
3886 nfs4_file_put_access(fp
, open
->op_share_access
);
3887 reset_union_bmap_deny(bmap_to_share_mode(old_deny_bmap
), stp
);
3892 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
)
3895 unsigned char old_deny_bmap
= stp
->st_deny_bmap
;
3897 if (!test_access(open
->op_share_access
, stp
))
3898 return nfs4_get_vfs_file(rqstp
, fp
, cur_fh
, stp
, open
);
3900 /* test and set deny mode */
3901 spin_lock(&fp
->fi_lock
);
3902 status
= nfs4_file_check_deny(fp
, open
->op_share_deny
);
3903 if (status
== nfs_ok
) {
3904 set_deny(open
->op_share_deny
, stp
);
3905 fp
->fi_share_deny
|=
3906 (open
->op_share_deny
& NFS4_SHARE_DENY_BOTH
);
3908 spin_unlock(&fp
->fi_lock
);
3910 if (status
!= nfs_ok
)
3913 status
= nfsd4_truncate(rqstp
, cur_fh
, open
);
3914 if (status
!= nfs_ok
)
3915 reset_union_bmap_deny(old_deny_bmap
, stp
);
3919 /* Should we give out recallable state?: */
3920 static bool nfsd4_cb_channel_good(struct nfs4_client
*clp
)
3922 if (clp
->cl_cb_state
== NFSD4_CB_UP
)
3925 * In the sessions case, since we don't have to establish a
3926 * separate connection for callbacks, we assume it's OK
3927 * until we hear otherwise:
3929 return clp
->cl_minorversion
&& clp
->cl_cb_state
== NFSD4_CB_UNKNOWN
;
3932 static struct file_lock
*nfs4_alloc_init_lease(struct nfs4_file
*fp
, int flag
)
3934 struct file_lock
*fl
;
3936 fl
= locks_alloc_lock();
3939 fl
->fl_lmops
= &nfsd_lease_mng_ops
;
3940 fl
->fl_flags
= FL_DELEG
;
3941 fl
->fl_type
= flag
== NFS4_OPEN_DELEGATE_READ
? F_RDLCK
: F_WRLCK
;
3942 fl
->fl_end
= OFFSET_MAX
;
3943 fl
->fl_owner
= (fl_owner_t
)fp
;
3944 fl
->fl_pid
= current
->tgid
;
3948 static int nfs4_setlease(struct nfs4_delegation
*dp
)
3950 struct nfs4_file
*fp
= dp
->dl_stid
.sc_file
;
3951 struct file_lock
*fl
;
3955 fl
= nfs4_alloc_init_lease(fp
, NFS4_OPEN_DELEGATE_READ
);
3958 filp
= find_readable_file(fp
);
3960 /* We should always have a readable file here */
3962 locks_free_lock(fl
);
3966 status
= vfs_setlease(filp
, fl
->fl_type
, &fl
, NULL
);
3968 locks_free_lock(fl
);
3971 spin_lock(&state_lock
);
3972 spin_lock(&fp
->fi_lock
);
3973 /* Did the lease get broken before we took the lock? */
3975 if (fp
->fi_had_conflict
)
3978 if (fp
->fi_deleg_file
) {
3981 hash_delegation_locked(dp
, fp
);
3984 fp
->fi_deleg_file
= filp
;
3985 fp
->fi_delegees
= 1;
3986 hash_delegation_locked(dp
, fp
);
3987 spin_unlock(&fp
->fi_lock
);
3988 spin_unlock(&state_lock
);
3991 spin_unlock(&fp
->fi_lock
);
3992 spin_unlock(&state_lock
);
3998 static struct nfs4_delegation
*
3999 nfs4_set_delegation(struct nfs4_client
*clp
, struct svc_fh
*fh
,
4000 struct nfs4_file
*fp
, struct nfs4_clnt_odstate
*odstate
)
4003 struct nfs4_delegation
*dp
;
4005 if (fp
->fi_had_conflict
)
4006 return ERR_PTR(-EAGAIN
);
4008 dp
= alloc_init_deleg(clp
, fh
, odstate
);
4010 return ERR_PTR(-ENOMEM
);
4013 spin_lock(&state_lock
);
4014 spin_lock(&fp
->fi_lock
);
4015 dp
->dl_stid
.sc_file
= fp
;
4016 if (!fp
->fi_deleg_file
) {
4017 spin_unlock(&fp
->fi_lock
);
4018 spin_unlock(&state_lock
);
4019 status
= nfs4_setlease(dp
);
4022 if (fp
->fi_had_conflict
) {
4027 hash_delegation_locked(dp
, fp
);
4030 spin_unlock(&fp
->fi_lock
);
4031 spin_unlock(&state_lock
);
4034 put_clnt_odstate(dp
->dl_clnt_odstate
);
4035 nfs4_put_stid(&dp
->dl_stid
);
4036 return ERR_PTR(status
);
4041 static void nfsd4_open_deleg_none_ext(struct nfsd4_open
*open
, int status
)
4043 open
->op_delegate_type
= NFS4_OPEN_DELEGATE_NONE_EXT
;
4044 if (status
== -EAGAIN
)
4045 open
->op_why_no_deleg
= WND4_CONTENTION
;
4047 open
->op_why_no_deleg
= WND4_RESOURCE
;
4048 switch (open
->op_deleg_want
) {
4049 case NFS4_SHARE_WANT_READ_DELEG
:
4050 case NFS4_SHARE_WANT_WRITE_DELEG
:
4051 case NFS4_SHARE_WANT_ANY_DELEG
:
4053 case NFS4_SHARE_WANT_CANCEL
:
4054 open
->op_why_no_deleg
= WND4_CANCELLED
;
4056 case NFS4_SHARE_WANT_NO_DELEG
:
4063 * Attempt to hand out a delegation.
4065 * Note we don't support write delegations, and won't until the vfs has
4066 * proper support for them.
4069 nfs4_open_delegation(struct svc_fh
*fh
, struct nfsd4_open
*open
,
4070 struct nfs4_ol_stateid
*stp
)
4072 struct nfs4_delegation
*dp
;
4073 struct nfs4_openowner
*oo
= openowner(stp
->st_stateowner
);
4074 struct nfs4_client
*clp
= stp
->st_stid
.sc_client
;
4078 cb_up
= nfsd4_cb_channel_good(oo
->oo_owner
.so_client
);
4079 open
->op_recall
= 0;
4080 switch (open
->op_claim_type
) {
4081 case NFS4_OPEN_CLAIM_PREVIOUS
:
4083 open
->op_recall
= 1;
4084 if (open
->op_delegate_type
!= NFS4_OPEN_DELEGATE_READ
)
4087 case NFS4_OPEN_CLAIM_NULL
:
4088 case NFS4_OPEN_CLAIM_FH
:
4090 * Let's not give out any delegations till everyone's
4091 * had the chance to reclaim theirs, *and* until
4092 * NLM locks have all been reclaimed:
4094 if (locks_in_grace(clp
->net
))
4096 if (!cb_up
|| !(oo
->oo_flags
& NFS4_OO_CONFIRMED
))
4099 * Also, if the file was opened for write or
4100 * create, there's a good chance the client's
4101 * about to write to it, resulting in an
4102 * immediate recall (since we don't support
4103 * write delegations):
4105 if (open
->op_share_access
& NFS4_SHARE_ACCESS_WRITE
)
4107 if (open
->op_create
== NFS4_OPEN_CREATE
)
4113 dp
= nfs4_set_delegation(clp
, fh
, stp
->st_stid
.sc_file
, stp
->st_clnt_odstate
);
4117 memcpy(&open
->op_delegate_stateid
, &dp
->dl_stid
.sc_stateid
, sizeof(dp
->dl_stid
.sc_stateid
));
4119 dprintk("NFSD: delegation stateid=" STATEID_FMT
"\n",
4120 STATEID_VAL(&dp
->dl_stid
.sc_stateid
));
4121 open
->op_delegate_type
= NFS4_OPEN_DELEGATE_READ
;
4122 nfs4_put_stid(&dp
->dl_stid
);
4125 open
->op_delegate_type
= NFS4_OPEN_DELEGATE_NONE
;
4126 if (open
->op_claim_type
== NFS4_OPEN_CLAIM_PREVIOUS
&&
4127 open
->op_delegate_type
!= NFS4_OPEN_DELEGATE_NONE
) {
4128 dprintk("NFSD: WARNING: refusing delegation reclaim\n");
4129 open
->op_recall
= 1;
4132 /* 4.1 client asking for a delegation? */
4133 if (open
->op_deleg_want
)
4134 nfsd4_open_deleg_none_ext(open
, status
);
4138 static void nfsd4_deleg_xgrade_none_ext(struct nfsd4_open
*open
,
4139 struct nfs4_delegation
*dp
)
4141 if (open
->op_deleg_want
== NFS4_SHARE_WANT_READ_DELEG
&&
4142 dp
->dl_type
== NFS4_OPEN_DELEGATE_WRITE
) {
4143 open
->op_delegate_type
= NFS4_OPEN_DELEGATE_NONE_EXT
;
4144 open
->op_why_no_deleg
= WND4_NOT_SUPP_DOWNGRADE
;
4145 } else if (open
->op_deleg_want
== NFS4_SHARE_WANT_WRITE_DELEG
&&
4146 dp
->dl_type
== NFS4_OPEN_DELEGATE_WRITE
) {
4147 open
->op_delegate_type
= NFS4_OPEN_DELEGATE_NONE_EXT
;
4148 open
->op_why_no_deleg
= WND4_NOT_SUPP_UPGRADE
;
4150 /* Otherwise the client must be confused wanting a delegation
4151 * it already has, therefore we don't return
4152 * NFS4_OPEN_DELEGATE_NONE_EXT and reason.
4157 nfsd4_process_open2(struct svc_rqst
*rqstp
, struct svc_fh
*current_fh
, struct nfsd4_open
*open
)
4159 struct nfsd4_compoundres
*resp
= rqstp
->rq_resp
;
4160 struct nfs4_client
*cl
= open
->op_openowner
->oo_owner
.so_client
;
4161 struct nfs4_file
*fp
= NULL
;
4162 struct nfs4_ol_stateid
*stp
= NULL
;
4163 struct nfs4_delegation
*dp
= NULL
;
4167 * Lookup file; if found, lookup stateid and check open request,
4168 * and check for delegations in the process of being recalled.
4169 * If not found, create the nfs4_file struct
4171 fp
= find_or_add_file(open
->op_file
, ¤t_fh
->fh_handle
);
4172 if (fp
!= open
->op_file
) {
4173 status
= nfs4_check_deleg(cl
, open
, &dp
);
4176 stp
= nfsd4_find_existing_open(fp
, open
);
4178 open
->op_file
= NULL
;
4179 status
= nfserr_bad_stateid
;
4180 if (nfsd4_is_deleg_cur(open
))
4185 * OPEN the file, or upgrade an existing OPEN.
4186 * If truncate fails, the OPEN fails.
4189 /* Stateid was found, this is an OPEN upgrade */
4190 status
= nfs4_upgrade_open(rqstp
, fp
, current_fh
, stp
, open
);
4195 open
->op_stp
= NULL
;
4196 init_open_stateid(stp
, fp
, open
);
4197 status
= nfs4_get_vfs_file(rqstp
, fp
, current_fh
, stp
, open
);
4199 release_open_stateid(stp
);
4203 stp
->st_clnt_odstate
= find_or_hash_clnt_odstate(fp
,
4205 if (stp
->st_clnt_odstate
== open
->op_odstate
)
4206 open
->op_odstate
= NULL
;
4208 update_stateid(&stp
->st_stid
.sc_stateid
);
4209 memcpy(&open
->op_stateid
, &stp
->st_stid
.sc_stateid
, sizeof(stateid_t
));
4211 if (nfsd4_has_session(&resp
->cstate
)) {
4212 if (open
->op_deleg_want
& NFS4_SHARE_WANT_NO_DELEG
) {
4213 open
->op_delegate_type
= NFS4_OPEN_DELEGATE_NONE_EXT
;
4214 open
->op_why_no_deleg
= WND4_NOT_WANTED
;
4220 * Attempt to hand out a delegation. No error return, because the
4221 * OPEN succeeds even if we fail.
4223 nfs4_open_delegation(current_fh
, open
, stp
);
4227 dprintk("%s: stateid=" STATEID_FMT
"\n", __func__
,
4228 STATEID_VAL(&stp
->st_stid
.sc_stateid
));
4230 /* 4.1 client trying to upgrade/downgrade delegation? */
4231 if (open
->op_delegate_type
== NFS4_OPEN_DELEGATE_NONE
&& dp
&&
4232 open
->op_deleg_want
)
4233 nfsd4_deleg_xgrade_none_ext(open
, dp
);
4237 if (status
== 0 && open
->op_claim_type
== NFS4_OPEN_CLAIM_PREVIOUS
)
4238 open
->op_openowner
->oo_flags
|= NFS4_OO_CONFIRMED
;
4240 * To finish the open response, we just need to set the rflags.
4242 open
->op_rflags
= NFS4_OPEN_RESULT_LOCKTYPE_POSIX
;
4243 if (!(open
->op_openowner
->oo_flags
& NFS4_OO_CONFIRMED
) &&
4244 !nfsd4_has_session(&resp
->cstate
))
4245 open
->op_rflags
|= NFS4_OPEN_RESULT_CONFIRM
;
4247 nfs4_put_stid(&dp
->dl_stid
);
4249 nfs4_put_stid(&stp
->st_stid
);
4254 void nfsd4_cleanup_open_state(struct nfsd4_compound_state
*cstate
,
4255 struct nfsd4_open
*open
)
4257 if (open
->op_openowner
) {
4258 struct nfs4_stateowner
*so
= &open
->op_openowner
->oo_owner
;
4260 nfsd4_cstate_assign_replay(cstate
, so
);
4261 nfs4_put_stateowner(so
);
4264 kmem_cache_free(file_slab
, open
->op_file
);
4266 nfs4_put_stid(&open
->op_stp
->st_stid
);
4267 if (open
->op_odstate
)
4268 kmem_cache_free(odstate_slab
, open
->op_odstate
);
4272 nfsd4_renew(struct svc_rqst
*rqstp
, struct nfsd4_compound_state
*cstate
,
4275 struct nfs4_client
*clp
;
4277 struct nfsd_net
*nn
= net_generic(SVC_NET(rqstp
), nfsd_net_id
);
4279 dprintk("process_renew(%08x/%08x): starting\n",
4280 clid
->cl_boot
, clid
->cl_id
);
4281 status
= lookup_clientid(clid
, cstate
, nn
);
4285 status
= nfserr_cb_path_down
;
4286 if (!list_empty(&clp
->cl_delegations
)
4287 && clp
->cl_cb_state
!= NFSD4_CB_UP
)
4295 nfsd4_end_grace(struct nfsd_net
*nn
)
4297 /* do nothing if grace period already ended */
4298 if (nn
->grace_ended
)
4301 dprintk("NFSD: end of grace period\n");
4302 nn
->grace_ended
= true;
4304 * If the server goes down again right now, an NFSv4
4305 * client will still be allowed to reclaim after it comes back up,
4306 * even if it hasn't yet had a chance to reclaim state this time.
4309 nfsd4_record_grace_done(nn
);
4311 * At this point, NFSv4 clients can still reclaim. But if the
4312 * server crashes, any that have not yet reclaimed will be out
4313 * of luck on the next boot.
4315 * (NFSv4.1+ clients are considered to have reclaimed once they
4316 * call RECLAIM_COMPLETE. NFSv4.0 clients are considered to
4317 * have reclaimed after their first OPEN.)
4319 locks_end_grace(&nn
->nfsd4_manager
);
4321 * At this point, and once lockd and/or any other containers
4322 * exit their grace period, further reclaims will fail and
4323 * regular locking can resume.
4328 nfs4_laundromat(struct nfsd_net
*nn
)
4330 struct nfs4_client
*clp
;
4331 struct nfs4_openowner
*oo
;
4332 struct nfs4_delegation
*dp
;
4333 struct nfs4_ol_stateid
*stp
;
4334 struct list_head
*pos
, *next
, reaplist
;
4335 time_t cutoff
= get_seconds() - nn
->nfsd4_lease
;
4336 time_t t
, new_timeo
= nn
->nfsd4_lease
;
4338 dprintk("NFSD: laundromat service - starting\n");
4339 nfsd4_end_grace(nn
);
4340 INIT_LIST_HEAD(&reaplist
);
4341 spin_lock(&nn
->client_lock
);
4342 list_for_each_safe(pos
, next
, &nn
->client_lru
) {
4343 clp
= list_entry(pos
, struct nfs4_client
, cl_lru
);
4344 if (time_after((unsigned long)clp
->cl_time
, (unsigned long)cutoff
)) {
4345 t
= clp
->cl_time
- cutoff
;
4346 new_timeo
= min(new_timeo
, t
);
4349 if (mark_client_expired_locked(clp
)) {
4350 dprintk("NFSD: client in use (clientid %08x)\n",
4351 clp
->cl_clientid
.cl_id
);
4354 list_add(&clp
->cl_lru
, &reaplist
);
4356 spin_unlock(&nn
->client_lock
);
4357 list_for_each_safe(pos
, next
, &reaplist
) {
4358 clp
= list_entry(pos
, struct nfs4_client
, cl_lru
);
4359 dprintk("NFSD: purging unused client (clientid %08x)\n",
4360 clp
->cl_clientid
.cl_id
);
4361 list_del_init(&clp
->cl_lru
);
4364 spin_lock(&state_lock
);
4365 list_for_each_safe(pos
, next
, &nn
->del_recall_lru
) {
4366 dp
= list_entry (pos
, struct nfs4_delegation
, dl_recall_lru
);
4367 if (time_after((unsigned long)dp
->dl_time
, (unsigned long)cutoff
)) {
4368 t
= dp
->dl_time
- cutoff
;
4369 new_timeo
= min(new_timeo
, t
);
4372 WARN_ON(!unhash_delegation_locked(dp
));
4373 list_add(&dp
->dl_recall_lru
, &reaplist
);
4375 spin_unlock(&state_lock
);
4376 while (!list_empty(&reaplist
)) {
4377 dp
= list_first_entry(&reaplist
, struct nfs4_delegation
,
4379 list_del_init(&dp
->dl_recall_lru
);
4380 revoke_delegation(dp
);
4383 spin_lock(&nn
->client_lock
);
4384 while (!list_empty(&nn
->close_lru
)) {
4385 oo
= list_first_entry(&nn
->close_lru
, struct nfs4_openowner
,
4387 if (time_after((unsigned long)oo
->oo_time
,
4388 (unsigned long)cutoff
)) {
4389 t
= oo
->oo_time
- cutoff
;
4390 new_timeo
= min(new_timeo
, t
);
4393 list_del_init(&oo
->oo_close_lru
);
4394 stp
= oo
->oo_last_closed_stid
;
4395 oo
->oo_last_closed_stid
= NULL
;
4396 spin_unlock(&nn
->client_lock
);
4397 nfs4_put_stid(&stp
->st_stid
);
4398 spin_lock(&nn
->client_lock
);
4400 spin_unlock(&nn
->client_lock
);
4402 new_timeo
= max_t(time_t, new_timeo
, NFSD_LAUNDROMAT_MINTIMEOUT
);
4406 static struct workqueue_struct
*laundry_wq
;
4407 static void laundromat_main(struct work_struct
*);
4410 laundromat_main(struct work_struct
*laundry
)
4413 struct delayed_work
*dwork
= container_of(laundry
, struct delayed_work
,
4415 struct nfsd_net
*nn
= container_of(dwork
, struct nfsd_net
,
4418 t
= nfs4_laundromat(nn
);
4419 dprintk("NFSD: laundromat_main - sleeping for %ld seconds\n", t
);
4420 queue_delayed_work(laundry_wq
, &nn
->laundromat_work
, t
*HZ
);
4423 static inline __be32
nfs4_check_fh(struct svc_fh
*fhp
, struct nfs4_stid
*stp
)
4425 if (!fh_match(&fhp
->fh_handle
, &stp
->sc_file
->fi_fhandle
))
4426 return nfserr_bad_stateid
;
4431 access_permit_read(struct nfs4_ol_stateid
*stp
)
4433 return test_access(NFS4_SHARE_ACCESS_READ
, stp
) ||
4434 test_access(NFS4_SHARE_ACCESS_BOTH
, stp
) ||
4435 test_access(NFS4_SHARE_ACCESS_WRITE
, stp
);
4439 access_permit_write(struct nfs4_ol_stateid
*stp
)
4441 return test_access(NFS4_SHARE_ACCESS_WRITE
, stp
) ||
4442 test_access(NFS4_SHARE_ACCESS_BOTH
, stp
);
4446 __be32
nfs4_check_openmode(struct nfs4_ol_stateid
*stp
, int flags
)
4448 __be32 status
= nfserr_openmode
;
4450 /* For lock stateid's, we test the parent open, not the lock: */
4451 if (stp
->st_openstp
)
4452 stp
= stp
->st_openstp
;
4453 if ((flags
& WR_STATE
) && !access_permit_write(stp
))
4455 if ((flags
& RD_STATE
) && !access_permit_read(stp
))
4462 static inline __be32
4463 check_special_stateids(struct net
*net
, svc_fh
*current_fh
, stateid_t
*stateid
, int flags
)
4465 if (ONE_STATEID(stateid
) && (flags
& RD_STATE
))
4467 else if (opens_in_grace(net
)) {
4468 /* Answer in remaining cases depends on existence of
4469 * conflicting state; so we must wait out the grace period. */
4470 return nfserr_grace
;
4471 } else if (flags
& WR_STATE
)
4472 return nfs4_share_conflict(current_fh
,
4473 NFS4_SHARE_DENY_WRITE
);
4474 else /* (flags & RD_STATE) && ZERO_STATEID(stateid) */
4475 return nfs4_share_conflict(current_fh
,
4476 NFS4_SHARE_DENY_READ
);
4480 * Allow READ/WRITE during grace period on recovered state only for files
4481 * that are not able to provide mandatory locking.
4484 grace_disallows_io(struct net
*net
, struct inode
*inode
)
4486 return opens_in_grace(net
) && mandatory_lock(inode
);
4489 /* Returns true iff a is later than b: */
4490 static bool stateid_generation_after(stateid_t
*a
, stateid_t
*b
)
4492 return (s32
)(a
->si_generation
- b
->si_generation
) > 0;
4495 static __be32
check_stateid_generation(stateid_t
*in
, stateid_t
*ref
, bool has_session
)
4498 * When sessions are used the stateid generation number is ignored
4501 if (has_session
&& in
->si_generation
== 0)
4504 if (in
->si_generation
== ref
->si_generation
)
4507 /* If the client sends us a stateid from the future, it's buggy: */
4508 if (stateid_generation_after(in
, ref
))
4509 return nfserr_bad_stateid
;
4511 * However, we could see a stateid from the past, even from a
4512 * non-buggy client. For example, if the client sends a lock
4513 * while some IO is outstanding, the lock may bump si_generation
4514 * while the IO is still in flight. The client could avoid that
4515 * situation by waiting for responses on all the IO requests,
4516 * but better performance may result in retrying IO that
4517 * receives an old_stateid error if requests are rarely
4518 * reordered in flight:
4520 return nfserr_old_stateid
;
4523 static __be32
nfsd4_check_openowner_confirmed(struct nfs4_ol_stateid
*ols
)
4525 if (ols
->st_stateowner
->so_is_open_owner
&&
4526 !(openowner(ols
->st_stateowner
)->oo_flags
& NFS4_OO_CONFIRMED
))
4527 return nfserr_bad_stateid
;
4531 static __be32
nfsd4_validate_stateid(struct nfs4_client
*cl
, stateid_t
*stateid
)
4533 struct nfs4_stid
*s
;
4534 __be32 status
= nfserr_bad_stateid
;
4536 if (ZERO_STATEID(stateid
) || ONE_STATEID(stateid
))
4538 /* Client debugging aid. */
4539 if (!same_clid(&stateid
->si_opaque
.so_clid
, &cl
->cl_clientid
)) {
4540 char addr_str
[INET6_ADDRSTRLEN
];
4541 rpc_ntop((struct sockaddr
*)&cl
->cl_addr
, addr_str
,
4543 pr_warn_ratelimited("NFSD: client %s testing state ID "
4544 "with incorrect client ID\n", addr_str
);
4547 spin_lock(&cl
->cl_lock
);
4548 s
= find_stateid_locked(cl
, stateid
);
4551 status
= check_stateid_generation(stateid
, &s
->sc_stateid
, 1);
4554 switch (s
->sc_type
) {
4555 case NFS4_DELEG_STID
:
4558 case NFS4_REVOKED_DELEG_STID
:
4559 status
= nfserr_deleg_revoked
;
4561 case NFS4_OPEN_STID
:
4562 case NFS4_LOCK_STID
:
4563 status
= nfsd4_check_openowner_confirmed(openlockstateid(s
));
4566 printk("unknown stateid type %x\n", s
->sc_type
);
4568 case NFS4_CLOSED_STID
:
4569 case NFS4_CLOSED_DELEG_STID
:
4570 status
= nfserr_bad_stateid
;
4573 spin_unlock(&cl
->cl_lock
);
4578 nfsd4_lookup_stateid(struct nfsd4_compound_state
*cstate
,
4579 stateid_t
*stateid
, unsigned char typemask
,
4580 struct nfs4_stid
**s
, struct nfsd_net
*nn
)
4584 if (ZERO_STATEID(stateid
) || ONE_STATEID(stateid
))
4585 return nfserr_bad_stateid
;
4586 status
= lookup_clientid(&stateid
->si_opaque
.so_clid
, cstate
, nn
);
4587 if (status
== nfserr_stale_clientid
) {
4588 if (cstate
->session
)
4589 return nfserr_bad_stateid
;
4590 return nfserr_stale_stateid
;
4594 *s
= find_stateid_by_type(cstate
->clp
, stateid
, typemask
);
4596 return nfserr_bad_stateid
;
4600 static struct file
*
4601 nfs4_find_file(struct nfs4_stid
*s
, int flags
)
4606 switch (s
->sc_type
) {
4607 case NFS4_DELEG_STID
:
4608 if (WARN_ON_ONCE(!s
->sc_file
->fi_deleg_file
))
4610 return get_file(s
->sc_file
->fi_deleg_file
);
4611 case NFS4_OPEN_STID
:
4612 case NFS4_LOCK_STID
:
4613 if (flags
& RD_STATE
)
4614 return find_readable_file(s
->sc_file
);
4616 return find_writeable_file(s
->sc_file
);
4624 nfs4_check_olstateid(struct svc_fh
*fhp
, struct nfs4_ol_stateid
*ols
, int flags
)
4628 status
= nfsd4_check_openowner_confirmed(ols
);
4631 return nfs4_check_openmode(ols
, flags
);
4635 nfs4_check_file(struct svc_rqst
*rqstp
, struct svc_fh
*fhp
, struct nfs4_stid
*s
,
4636 struct file
**filpp
, bool *tmp_file
, int flags
)
4638 int acc
= (flags
& RD_STATE
) ? NFSD_MAY_READ
: NFSD_MAY_WRITE
;
4642 file
= nfs4_find_file(s
, flags
);
4644 status
= nfsd_permission(rqstp
, fhp
->fh_export
, fhp
->fh_dentry
,
4645 acc
| NFSD_MAY_OWNER_OVERRIDE
);
4653 status
= nfsd_open(rqstp
, fhp
, S_IFREG
, acc
, filpp
);
4665 * Checks for stateid operations
4668 nfs4_preprocess_stateid_op(struct svc_rqst
*rqstp
,
4669 struct nfsd4_compound_state
*cstate
, stateid_t
*stateid
,
4670 int flags
, struct file
**filpp
, bool *tmp_file
)
4672 struct svc_fh
*fhp
= &cstate
->current_fh
;
4673 struct inode
*ino
= d_inode(fhp
->fh_dentry
);
4674 struct net
*net
= SVC_NET(rqstp
);
4675 struct nfsd_net
*nn
= net_generic(net
, nfsd_net_id
);
4676 struct nfs4_stid
*s
= NULL
;
4684 if (grace_disallows_io(net
, ino
))
4685 return nfserr_grace
;
4687 if (ZERO_STATEID(stateid
) || ONE_STATEID(stateid
)) {
4688 status
= check_special_stateids(net
, fhp
, stateid
, flags
);
4692 status
= nfsd4_lookup_stateid(cstate
, stateid
,
4693 NFS4_DELEG_STID
|NFS4_OPEN_STID
|NFS4_LOCK_STID
,
4697 status
= check_stateid_generation(stateid
, &s
->sc_stateid
,
4698 nfsd4_has_session(cstate
));
4702 switch (s
->sc_type
) {
4703 case NFS4_DELEG_STID
:
4704 status
= nfs4_check_delegmode(delegstateid(s
), flags
);
4706 case NFS4_OPEN_STID
:
4707 case NFS4_LOCK_STID
:
4708 status
= nfs4_check_olstateid(fhp
, openlockstateid(s
), flags
);
4711 status
= nfserr_bad_stateid
;
4716 status
= nfs4_check_fh(fhp
, s
);
4719 if (!status
&& filpp
)
4720 status
= nfs4_check_file(rqstp
, fhp
, s
, filpp
, tmp_file
, flags
);
4728 * Test if the stateid is valid
4731 nfsd4_test_stateid(struct svc_rqst
*rqstp
, struct nfsd4_compound_state
*cstate
,
4732 struct nfsd4_test_stateid
*test_stateid
)
4734 struct nfsd4_test_stateid_id
*stateid
;
4735 struct nfs4_client
*cl
= cstate
->session
->se_client
;
4737 list_for_each_entry(stateid
, &test_stateid
->ts_stateid_list
, ts_id_list
)
4738 stateid
->ts_id_status
=
4739 nfsd4_validate_stateid(cl
, &stateid
->ts_id_stateid
);
4745 nfsd4_free_stateid(struct svc_rqst
*rqstp
, struct nfsd4_compound_state
*cstate
,
4746 struct nfsd4_free_stateid
*free_stateid
)
4748 stateid_t
*stateid
= &free_stateid
->fr_stateid
;
4749 struct nfs4_stid
*s
;
4750 struct nfs4_delegation
*dp
;
4751 struct nfs4_ol_stateid
*stp
;
4752 struct nfs4_client
*cl
= cstate
->session
->se_client
;
4753 __be32 ret
= nfserr_bad_stateid
;
4755 spin_lock(&cl
->cl_lock
);
4756 s
= find_stateid_locked(cl
, stateid
);
4759 switch (s
->sc_type
) {
4760 case NFS4_DELEG_STID
:
4761 ret
= nfserr_locks_held
;
4763 case NFS4_OPEN_STID
:
4764 ret
= check_stateid_generation(stateid
, &s
->sc_stateid
, 1);
4767 ret
= nfserr_locks_held
;
4769 case NFS4_LOCK_STID
:
4770 ret
= check_stateid_generation(stateid
, &s
->sc_stateid
, 1);
4773 stp
= openlockstateid(s
);
4774 ret
= nfserr_locks_held
;
4775 if (check_for_locks(stp
->st_stid
.sc_file
,
4776 lockowner(stp
->st_stateowner
)))
4778 WARN_ON(!unhash_lock_stateid(stp
));
4779 spin_unlock(&cl
->cl_lock
);
4783 case NFS4_REVOKED_DELEG_STID
:
4784 dp
= delegstateid(s
);
4785 list_del_init(&dp
->dl_recall_lru
);
4786 spin_unlock(&cl
->cl_lock
);
4790 /* Default falls through and returns nfserr_bad_stateid */
4793 spin_unlock(&cl
->cl_lock
);
4801 return (type
== NFS4_READW_LT
|| type
== NFS4_READ_LT
) ?
4802 RD_STATE
: WR_STATE
;
4805 static __be32
nfs4_seqid_op_checks(struct nfsd4_compound_state
*cstate
, stateid_t
*stateid
, u32 seqid
, struct nfs4_ol_stateid
*stp
)
4807 struct svc_fh
*current_fh
= &cstate
->current_fh
;
4808 struct nfs4_stateowner
*sop
= stp
->st_stateowner
;
4811 status
= nfsd4_check_seqid(cstate
, sop
, seqid
);
4814 if (stp
->st_stid
.sc_type
== NFS4_CLOSED_STID
4815 || stp
->st_stid
.sc_type
== NFS4_REVOKED_DELEG_STID
)
4817 * "Closed" stateid's exist *only* to return
4818 * nfserr_replay_me from the previous step, and
4819 * revoked delegations are kept only for free_stateid.
4821 return nfserr_bad_stateid
;
4822 status
= check_stateid_generation(stateid
, &stp
->st_stid
.sc_stateid
, nfsd4_has_session(cstate
));
4825 return nfs4_check_fh(current_fh
, &stp
->st_stid
);
4829 * Checks for sequence id mutating operations.
4832 nfs4_preprocess_seqid_op(struct nfsd4_compound_state
*cstate
, u32 seqid
,
4833 stateid_t
*stateid
, char typemask
,
4834 struct nfs4_ol_stateid
**stpp
,
4835 struct nfsd_net
*nn
)
4838 struct nfs4_stid
*s
;
4839 struct nfs4_ol_stateid
*stp
= NULL
;
4841 dprintk("NFSD: %s: seqid=%d stateid = " STATEID_FMT
"\n", __func__
,
4842 seqid
, STATEID_VAL(stateid
));
4845 status
= nfsd4_lookup_stateid(cstate
, stateid
, typemask
, &s
, nn
);
4848 stp
= openlockstateid(s
);
4849 nfsd4_cstate_assign_replay(cstate
, stp
->st_stateowner
);
4851 status
= nfs4_seqid_op_checks(cstate
, stateid
, seqid
, stp
);
4855 nfs4_put_stid(&stp
->st_stid
);
4859 static __be32
nfs4_preprocess_confirmed_seqid_op(struct nfsd4_compound_state
*cstate
, u32 seqid
,
4860 stateid_t
*stateid
, struct nfs4_ol_stateid
**stpp
, struct nfsd_net
*nn
)
4863 struct nfs4_openowner
*oo
;
4864 struct nfs4_ol_stateid
*stp
;
4866 status
= nfs4_preprocess_seqid_op(cstate
, seqid
, stateid
,
4867 NFS4_OPEN_STID
, &stp
, nn
);
4870 oo
= openowner(stp
->st_stateowner
);
4871 if (!(oo
->oo_flags
& NFS4_OO_CONFIRMED
)) {
4872 nfs4_put_stid(&stp
->st_stid
);
4873 return nfserr_bad_stateid
;
4880 nfsd4_open_confirm(struct svc_rqst
*rqstp
, struct nfsd4_compound_state
*cstate
,
4881 struct nfsd4_open_confirm
*oc
)
4884 struct nfs4_openowner
*oo
;
4885 struct nfs4_ol_stateid
*stp
;
4886 struct nfsd_net
*nn
= net_generic(SVC_NET(rqstp
), nfsd_net_id
);
4888 dprintk("NFSD: nfsd4_open_confirm on file %pd\n",
4889 cstate
->current_fh
.fh_dentry
);
4891 status
= fh_verify(rqstp
, &cstate
->current_fh
, S_IFREG
, 0);
4895 status
= nfs4_preprocess_seqid_op(cstate
,
4896 oc
->oc_seqid
, &oc
->oc_req_stateid
,
4897 NFS4_OPEN_STID
, &stp
, nn
);
4900 oo
= openowner(stp
->st_stateowner
);
4901 status
= nfserr_bad_stateid
;
4902 if (oo
->oo_flags
& NFS4_OO_CONFIRMED
)
4904 oo
->oo_flags
|= NFS4_OO_CONFIRMED
;
4905 update_stateid(&stp
->st_stid
.sc_stateid
);
4906 memcpy(&oc
->oc_resp_stateid
, &stp
->st_stid
.sc_stateid
, sizeof(stateid_t
));
4907 dprintk("NFSD: %s: success, seqid=%d stateid=" STATEID_FMT
"\n",
4908 __func__
, oc
->oc_seqid
, STATEID_VAL(&stp
->st_stid
.sc_stateid
));
4910 nfsd4_client_record_create(oo
->oo_owner
.so_client
);
4913 nfs4_put_stid(&stp
->st_stid
);
4915 nfsd4_bump_seqid(cstate
, status
);
4919 static inline void nfs4_stateid_downgrade_bit(struct nfs4_ol_stateid
*stp
, u32 access
)
4921 if (!test_access(access
, stp
))
4923 nfs4_file_put_access(stp
->st_stid
.sc_file
, access
);
4924 clear_access(access
, stp
);
4927 static inline void nfs4_stateid_downgrade(struct nfs4_ol_stateid
*stp
, u32 to_access
)
4929 switch (to_access
) {
4930 case NFS4_SHARE_ACCESS_READ
:
4931 nfs4_stateid_downgrade_bit(stp
, NFS4_SHARE_ACCESS_WRITE
);
4932 nfs4_stateid_downgrade_bit(stp
, NFS4_SHARE_ACCESS_BOTH
);
4934 case NFS4_SHARE_ACCESS_WRITE
:
4935 nfs4_stateid_downgrade_bit(stp
, NFS4_SHARE_ACCESS_READ
);
4936 nfs4_stateid_downgrade_bit(stp
, NFS4_SHARE_ACCESS_BOTH
);
4938 case NFS4_SHARE_ACCESS_BOTH
:
4946 nfsd4_open_downgrade(struct svc_rqst
*rqstp
,
4947 struct nfsd4_compound_state
*cstate
,
4948 struct nfsd4_open_downgrade
*od
)
4951 struct nfs4_ol_stateid
*stp
;
4952 struct nfsd_net
*nn
= net_generic(SVC_NET(rqstp
), nfsd_net_id
);
4954 dprintk("NFSD: nfsd4_open_downgrade on file %pd\n",
4955 cstate
->current_fh
.fh_dentry
);
4957 /* We don't yet support WANT bits: */
4958 if (od
->od_deleg_want
)
4959 dprintk("NFSD: %s: od_deleg_want=0x%x ignored\n", __func__
,
4962 status
= nfs4_preprocess_confirmed_seqid_op(cstate
, od
->od_seqid
,
4963 &od
->od_stateid
, &stp
, nn
);
4966 status
= nfserr_inval
;
4967 if (!test_access(od
->od_share_access
, stp
)) {
4968 dprintk("NFSD: access not a subset of current bitmap: 0x%hhx, input access=%08x\n",
4969 stp
->st_access_bmap
, od
->od_share_access
);
4972 if (!test_deny(od
->od_share_deny
, stp
)) {
4973 dprintk("NFSD: deny not a subset of current bitmap: 0x%hhx, input deny=%08x\n",
4974 stp
->st_deny_bmap
, od
->od_share_deny
);
4977 nfs4_stateid_downgrade(stp
, od
->od_share_access
);
4979 reset_union_bmap_deny(od
->od_share_deny
, stp
);
4981 update_stateid(&stp
->st_stid
.sc_stateid
);
4982 memcpy(&od
->od_stateid
, &stp
->st_stid
.sc_stateid
, sizeof(stateid_t
));
4985 nfs4_put_stid(&stp
->st_stid
);
4987 nfsd4_bump_seqid(cstate
, status
);
4991 static void nfsd4_close_open_stateid(struct nfs4_ol_stateid
*s
)
4993 struct nfs4_client
*clp
= s
->st_stid
.sc_client
;
4995 LIST_HEAD(reaplist
);
4997 s
->st_stid
.sc_type
= NFS4_CLOSED_STID
;
4998 spin_lock(&clp
->cl_lock
);
4999 unhashed
= unhash_open_stateid(s
, &reaplist
);
5001 if (clp
->cl_minorversion
) {
5003 put_ol_stateid_locked(s
, &reaplist
);
5004 spin_unlock(&clp
->cl_lock
);
5005 free_ol_stateid_reaplist(&reaplist
);
5007 spin_unlock(&clp
->cl_lock
);
5008 free_ol_stateid_reaplist(&reaplist
);
5010 move_to_close_lru(s
, clp
->net
);
5015 * nfs4_unlock_state() called after encode
5018 nfsd4_close(struct svc_rqst
*rqstp
, struct nfsd4_compound_state
*cstate
,
5019 struct nfsd4_close
*close
)
5022 struct nfs4_ol_stateid
*stp
;
5023 struct net
*net
= SVC_NET(rqstp
);
5024 struct nfsd_net
*nn
= net_generic(net
, nfsd_net_id
);
5026 dprintk("NFSD: nfsd4_close on file %pd\n",
5027 cstate
->current_fh
.fh_dentry
);
5029 status
= nfs4_preprocess_seqid_op(cstate
, close
->cl_seqid
,
5031 NFS4_OPEN_STID
|NFS4_CLOSED_STID
,
5033 nfsd4_bump_seqid(cstate
, status
);
5036 update_stateid(&stp
->st_stid
.sc_stateid
);
5037 memcpy(&close
->cl_stateid
, &stp
->st_stid
.sc_stateid
, sizeof(stateid_t
));
5039 nfsd4_close_open_stateid(stp
);
5041 /* put reference from nfs4_preprocess_seqid_op */
5042 nfs4_put_stid(&stp
->st_stid
);
5048 nfsd4_delegreturn(struct svc_rqst
*rqstp
, struct nfsd4_compound_state
*cstate
,
5049 struct nfsd4_delegreturn
*dr
)
5051 struct nfs4_delegation
*dp
;
5052 stateid_t
*stateid
= &dr
->dr_stateid
;
5053 struct nfs4_stid
*s
;
5055 struct nfsd_net
*nn
= net_generic(SVC_NET(rqstp
), nfsd_net_id
);
5057 if ((status
= fh_verify(rqstp
, &cstate
->current_fh
, S_IFREG
, 0)))
5060 status
= nfsd4_lookup_stateid(cstate
, stateid
, NFS4_DELEG_STID
, &s
, nn
);
5063 dp
= delegstateid(s
);
5064 status
= check_stateid_generation(stateid
, &dp
->dl_stid
.sc_stateid
, nfsd4_has_session(cstate
));
5068 destroy_delegation(dp
);
5070 nfs4_put_stid(&dp
->dl_stid
);
5076 end_offset(u64 start
, u64 len
)
5081 return end
>= start
? end
: NFS4_MAX_UINT64
;
5084 /* last octet in a range */
5086 last_byte_offset(u64 start
, u64 len
)
5092 return end
> start
? end
- 1: NFS4_MAX_UINT64
;
5096 * TODO: Linux file offsets are _signed_ 64-bit quantities, which means that
5097 * we can't properly handle lock requests that go beyond the (2^63 - 1)-th
5098 * byte, because of sign extension problems. Since NFSv4 calls for 64-bit
5099 * locking, this prevents us from being completely protocol-compliant. The
5100 * real solution to this problem is to start using unsigned file offsets in
5101 * the VFS, but this is a very deep change!
5104 nfs4_transform_lock_offset(struct file_lock
*lock
)
5106 if (lock
->fl_start
< 0)
5107 lock
->fl_start
= OFFSET_MAX
;
5108 if (lock
->fl_end
< 0)
5109 lock
->fl_end
= OFFSET_MAX
;
5113 nfsd4_fl_get_owner(fl_owner_t owner
)
5115 struct nfs4_lockowner
*lo
= (struct nfs4_lockowner
*)owner
;
5117 nfs4_get_stateowner(&lo
->lo_owner
);
5122 nfsd4_fl_put_owner(fl_owner_t owner
)
5124 struct nfs4_lockowner
*lo
= (struct nfs4_lockowner
*)owner
;
5127 nfs4_put_stateowner(&lo
->lo_owner
);
5130 static const struct lock_manager_operations nfsd_posix_mng_ops
= {
5131 .lm_get_owner
= nfsd4_fl_get_owner
,
5132 .lm_put_owner
= nfsd4_fl_put_owner
,
5136 nfs4_set_lock_denied(struct file_lock
*fl
, struct nfsd4_lock_denied
*deny
)
5138 struct nfs4_lockowner
*lo
;
5140 if (fl
->fl_lmops
== &nfsd_posix_mng_ops
) {
5141 lo
= (struct nfs4_lockowner
*) fl
->fl_owner
;
5142 deny
->ld_owner
.data
= kmemdup(lo
->lo_owner
.so_owner
.data
,
5143 lo
->lo_owner
.so_owner
.len
, GFP_KERNEL
);
5144 if (!deny
->ld_owner
.data
)
5145 /* We just don't care that much */
5147 deny
->ld_owner
.len
= lo
->lo_owner
.so_owner
.len
;
5148 deny
->ld_clientid
= lo
->lo_owner
.so_client
->cl_clientid
;
5151 deny
->ld_owner
.len
= 0;
5152 deny
->ld_owner
.data
= NULL
;
5153 deny
->ld_clientid
.cl_boot
= 0;
5154 deny
->ld_clientid
.cl_id
= 0;
5156 deny
->ld_start
= fl
->fl_start
;
5157 deny
->ld_length
= NFS4_MAX_UINT64
;
5158 if (fl
->fl_end
!= NFS4_MAX_UINT64
)
5159 deny
->ld_length
= fl
->fl_end
- fl
->fl_start
+ 1;
5160 deny
->ld_type
= NFS4_READ_LT
;
5161 if (fl
->fl_type
!= F_RDLCK
)
5162 deny
->ld_type
= NFS4_WRITE_LT
;
5165 static struct nfs4_lockowner
*
5166 find_lockowner_str_locked(struct nfs4_client
*clp
, struct xdr_netobj
*owner
)
5168 unsigned int strhashval
= ownerstr_hashval(owner
);
5169 struct nfs4_stateowner
*so
;
5171 lockdep_assert_held(&clp
->cl_lock
);
5173 list_for_each_entry(so
, &clp
->cl_ownerstr_hashtbl
[strhashval
],
5175 if (so
->so_is_open_owner
)
5177 if (same_owner_str(so
, owner
))
5178 return lockowner(nfs4_get_stateowner(so
));
5183 static struct nfs4_lockowner
*
5184 find_lockowner_str(struct nfs4_client
*clp
, struct xdr_netobj
*owner
)
5186 struct nfs4_lockowner
*lo
;
5188 spin_lock(&clp
->cl_lock
);
5189 lo
= find_lockowner_str_locked(clp
, owner
);
5190 spin_unlock(&clp
->cl_lock
);
5194 static void nfs4_unhash_lockowner(struct nfs4_stateowner
*sop
)
5196 unhash_lockowner_locked(lockowner(sop
));
5199 static void nfs4_free_lockowner(struct nfs4_stateowner
*sop
)
5201 struct nfs4_lockowner
*lo
= lockowner(sop
);
5203 kmem_cache_free(lockowner_slab
, lo
);
5206 static const struct nfs4_stateowner_operations lockowner_ops
= {
5207 .so_unhash
= nfs4_unhash_lockowner
,
5208 .so_free
= nfs4_free_lockowner
,
5212 * Alloc a lock owner structure.
5213 * Called in nfsd4_lock - therefore, OPEN and OPEN_CONFIRM (if needed) has
5216 * strhashval = ownerstr_hashval
5218 static struct nfs4_lockowner
*
5219 alloc_init_lock_stateowner(unsigned int strhashval
, struct nfs4_client
*clp
,
5220 struct nfs4_ol_stateid
*open_stp
,
5221 struct nfsd4_lock
*lock
)
5223 struct nfs4_lockowner
*lo
, *ret
;
5225 lo
= alloc_stateowner(lockowner_slab
, &lock
->lk_new_owner
, clp
);
5228 INIT_LIST_HEAD(&lo
->lo_owner
.so_stateids
);
5229 lo
->lo_owner
.so_is_open_owner
= 0;
5230 lo
->lo_owner
.so_seqid
= lock
->lk_new_lock_seqid
;
5231 lo
->lo_owner
.so_ops
= &lockowner_ops
;
5232 spin_lock(&clp
->cl_lock
);
5233 ret
= find_lockowner_str_locked(clp
, &lock
->lk_new_owner
);
5235 list_add(&lo
->lo_owner
.so_strhash
,
5236 &clp
->cl_ownerstr_hashtbl
[strhashval
]);
5239 nfs4_free_stateowner(&lo
->lo_owner
);
5241 spin_unlock(&clp
->cl_lock
);
5246 init_lock_stateid(struct nfs4_ol_stateid
*stp
, struct nfs4_lockowner
*lo
,
5247 struct nfs4_file
*fp
, struct inode
*inode
,
5248 struct nfs4_ol_stateid
*open_stp
)
5250 struct nfs4_client
*clp
= lo
->lo_owner
.so_client
;
5252 lockdep_assert_held(&clp
->cl_lock
);
5254 atomic_inc(&stp
->st_stid
.sc_count
);
5255 stp
->st_stid
.sc_type
= NFS4_LOCK_STID
;
5256 stp
->st_stateowner
= nfs4_get_stateowner(&lo
->lo_owner
);
5258 stp
->st_stid
.sc_file
= fp
;
5259 stp
->st_stid
.sc_free
= nfs4_free_lock_stateid
;
5260 stp
->st_access_bmap
= 0;
5261 stp
->st_deny_bmap
= open_stp
->st_deny_bmap
;
5262 stp
->st_openstp
= open_stp
;
5263 list_add(&stp
->st_locks
, &open_stp
->st_locks
);
5264 list_add(&stp
->st_perstateowner
, &lo
->lo_owner
.so_stateids
);
5265 spin_lock(&fp
->fi_lock
);
5266 list_add(&stp
->st_perfile
, &fp
->fi_stateids
);
5267 spin_unlock(&fp
->fi_lock
);
5270 static struct nfs4_ol_stateid
*
5271 find_lock_stateid(struct nfs4_lockowner
*lo
, struct nfs4_file
*fp
)
5273 struct nfs4_ol_stateid
*lst
;
5274 struct nfs4_client
*clp
= lo
->lo_owner
.so_client
;
5276 lockdep_assert_held(&clp
->cl_lock
);
5278 list_for_each_entry(lst
, &lo
->lo_owner
.so_stateids
, st_perstateowner
) {
5279 if (lst
->st_stid
.sc_file
== fp
) {
5280 atomic_inc(&lst
->st_stid
.sc_count
);
5287 static struct nfs4_ol_stateid
*
5288 find_or_create_lock_stateid(struct nfs4_lockowner
*lo
, struct nfs4_file
*fi
,
5289 struct inode
*inode
, struct nfs4_ol_stateid
*ost
,
5292 struct nfs4_stid
*ns
= NULL
;
5293 struct nfs4_ol_stateid
*lst
;
5294 struct nfs4_openowner
*oo
= openowner(ost
->st_stateowner
);
5295 struct nfs4_client
*clp
= oo
->oo_owner
.so_client
;
5297 spin_lock(&clp
->cl_lock
);
5298 lst
= find_lock_stateid(lo
, fi
);
5300 spin_unlock(&clp
->cl_lock
);
5301 ns
= nfs4_alloc_stid(clp
, stateid_slab
);
5305 spin_lock(&clp
->cl_lock
);
5306 lst
= find_lock_stateid(lo
, fi
);
5308 lst
= openlockstateid(ns
);
5309 init_lock_stateid(lst
, lo
, fi
, inode
, ost
);
5314 spin_unlock(&clp
->cl_lock
);
5321 check_lock_length(u64 offset
, u64 length
)
5323 return ((length
== 0) || ((length
!= NFS4_MAX_UINT64
) &&
5324 (length
> ~offset
)));
5327 static void get_lock_access(struct nfs4_ol_stateid
*lock_stp
, u32 access
)
5329 struct nfs4_file
*fp
= lock_stp
->st_stid
.sc_file
;
5331 lockdep_assert_held(&fp
->fi_lock
);
5333 if (test_access(access
, lock_stp
))
5335 __nfs4_file_get_access(fp
, access
);
5336 set_access(access
, lock_stp
);
5340 lookup_or_create_lock_state(struct nfsd4_compound_state
*cstate
,
5341 struct nfs4_ol_stateid
*ost
,
5342 struct nfsd4_lock
*lock
,
5343 struct nfs4_ol_stateid
**lst
, bool *new)
5346 struct nfs4_file
*fi
= ost
->st_stid
.sc_file
;
5347 struct nfs4_openowner
*oo
= openowner(ost
->st_stateowner
);
5348 struct nfs4_client
*cl
= oo
->oo_owner
.so_client
;
5349 struct inode
*inode
= d_inode(cstate
->current_fh
.fh_dentry
);
5350 struct nfs4_lockowner
*lo
;
5351 unsigned int strhashval
;
5353 lo
= find_lockowner_str(cl
, &lock
->lk_new_owner
);
5355 strhashval
= ownerstr_hashval(&lock
->lk_new_owner
);
5356 lo
= alloc_init_lock_stateowner(strhashval
, cl
, ost
, lock
);
5358 return nfserr_jukebox
;
5360 /* with an existing lockowner, seqids must be the same */
5361 status
= nfserr_bad_seqid
;
5362 if (!cstate
->minorversion
&&
5363 lock
->lk_new_lock_seqid
!= lo
->lo_owner
.so_seqid
)
5367 *lst
= find_or_create_lock_stateid(lo
, fi
, inode
, ost
, new);
5369 status
= nfserr_jukebox
;
5374 nfs4_put_stateowner(&lo
->lo_owner
);
5382 nfsd4_lock(struct svc_rqst
*rqstp
, struct nfsd4_compound_state
*cstate
,
5383 struct nfsd4_lock
*lock
)
5385 struct nfs4_openowner
*open_sop
= NULL
;
5386 struct nfs4_lockowner
*lock_sop
= NULL
;
5387 struct nfs4_ol_stateid
*lock_stp
= NULL
;
5388 struct nfs4_ol_stateid
*open_stp
= NULL
;
5389 struct nfs4_file
*fp
;
5390 struct file
*filp
= NULL
;
5391 struct file_lock
*file_lock
= NULL
;
5392 struct file_lock
*conflock
= NULL
;
5397 struct net
*net
= SVC_NET(rqstp
);
5398 struct nfsd_net
*nn
= net_generic(net
, nfsd_net_id
);
5400 dprintk("NFSD: nfsd4_lock: start=%Ld length=%Ld\n",
5401 (long long) lock
->lk_offset
,
5402 (long long) lock
->lk_length
);
5404 if (check_lock_length(lock
->lk_offset
, lock
->lk_length
))
5405 return nfserr_inval
;
5407 if ((status
= fh_verify(rqstp
, &cstate
->current_fh
,
5408 S_IFREG
, NFSD_MAY_LOCK
))) {
5409 dprintk("NFSD: nfsd4_lock: permission denied!\n");
5413 if (lock
->lk_is_new
) {
5414 if (nfsd4_has_session(cstate
))
5415 /* See rfc 5661 18.10.3: given clientid is ignored: */
5416 memcpy(&lock
->lk_new_clientid
,
5417 &cstate
->session
->se_client
->cl_clientid
,
5418 sizeof(clientid_t
));
5420 status
= nfserr_stale_clientid
;
5421 if (STALE_CLIENTID(&lock
->lk_new_clientid
, nn
))
5424 /* validate and update open stateid and open seqid */
5425 status
= nfs4_preprocess_confirmed_seqid_op(cstate
,
5426 lock
->lk_new_open_seqid
,
5427 &lock
->lk_new_open_stateid
,
5431 open_sop
= openowner(open_stp
->st_stateowner
);
5432 status
= nfserr_bad_stateid
;
5433 if (!same_clid(&open_sop
->oo_owner
.so_client
->cl_clientid
,
5434 &lock
->lk_new_clientid
))
5436 status
= lookup_or_create_lock_state(cstate
, open_stp
, lock
,
5439 status
= nfs4_preprocess_seqid_op(cstate
,
5440 lock
->lk_old_lock_seqid
,
5441 &lock
->lk_old_lock_stateid
,
5442 NFS4_LOCK_STID
, &lock_stp
, nn
);
5446 lock_sop
= lockowner(lock_stp
->st_stateowner
);
5448 lkflg
= setlkflg(lock
->lk_type
);
5449 status
= nfs4_check_openmode(lock_stp
, lkflg
);
5453 status
= nfserr_grace
;
5454 if (locks_in_grace(net
) && !lock
->lk_reclaim
)
5456 status
= nfserr_no_grace
;
5457 if (!locks_in_grace(net
) && lock
->lk_reclaim
)
5460 file_lock
= locks_alloc_lock();
5462 dprintk("NFSD: %s: unable to allocate lock!\n", __func__
);
5463 status
= nfserr_jukebox
;
5467 fp
= lock_stp
->st_stid
.sc_file
;
5468 switch (lock
->lk_type
) {
5471 spin_lock(&fp
->fi_lock
);
5472 filp
= find_readable_file_locked(fp
);
5474 get_lock_access(lock_stp
, NFS4_SHARE_ACCESS_READ
);
5475 spin_unlock(&fp
->fi_lock
);
5476 file_lock
->fl_type
= F_RDLCK
;
5479 case NFS4_WRITEW_LT
:
5480 spin_lock(&fp
->fi_lock
);
5481 filp
= find_writeable_file_locked(fp
);
5483 get_lock_access(lock_stp
, NFS4_SHARE_ACCESS_WRITE
);
5484 spin_unlock(&fp
->fi_lock
);
5485 file_lock
->fl_type
= F_WRLCK
;
5488 status
= nfserr_inval
;
5492 status
= nfserr_openmode
;
5496 file_lock
->fl_owner
= (fl_owner_t
)lockowner(nfs4_get_stateowner(&lock_sop
->lo_owner
));
5497 file_lock
->fl_pid
= current
->tgid
;
5498 file_lock
->fl_file
= filp
;
5499 file_lock
->fl_flags
= FL_POSIX
;
5500 file_lock
->fl_lmops
= &nfsd_posix_mng_ops
;
5501 file_lock
->fl_start
= lock
->lk_offset
;
5502 file_lock
->fl_end
= last_byte_offset(lock
->lk_offset
, lock
->lk_length
);
5503 nfs4_transform_lock_offset(file_lock
);
5505 conflock
= locks_alloc_lock();
5507 dprintk("NFSD: %s: unable to allocate lock!\n", __func__
);
5508 status
= nfserr_jukebox
;
5512 err
= vfs_lock_file(filp
, F_SETLK
, file_lock
, conflock
);
5514 case 0: /* success! */
5515 update_stateid(&lock_stp
->st_stid
.sc_stateid
);
5516 memcpy(&lock
->lk_resp_stateid
, &lock_stp
->st_stid
.sc_stateid
,
5520 case (EAGAIN
): /* conflock holds conflicting lock */
5521 status
= nfserr_denied
;
5522 dprintk("NFSD: nfsd4_lock: conflicting lock found!\n");
5523 nfs4_set_lock_denied(conflock
, &lock
->lk_denied
);
5526 status
= nfserr_deadlock
;
5529 dprintk("NFSD: nfsd4_lock: vfs_lock_file() failed! status %d\n",err
);
5530 status
= nfserrno(err
);
5537 /* Bump seqid manually if the 4.0 replay owner is openowner */
5538 if (cstate
->replay_owner
&&
5539 cstate
->replay_owner
!= &lock_sop
->lo_owner
&&
5540 seqid_mutating_err(ntohl(status
)))
5541 lock_sop
->lo_owner
.so_seqid
++;
5544 * If this is a new, never-before-used stateid, and we are
5545 * returning an error, then just go ahead and release it.
5548 release_lock_stateid(lock_stp
);
5550 nfs4_put_stid(&lock_stp
->st_stid
);
5553 nfs4_put_stid(&open_stp
->st_stid
);
5554 nfsd4_bump_seqid(cstate
, status
);
5556 locks_free_lock(file_lock
);
5558 locks_free_lock(conflock
);
5563 * The NFSv4 spec allows a client to do a LOCKT without holding an OPEN,
5564 * so we do a temporary open here just to get an open file to pass to
5565 * vfs_test_lock. (Arguably perhaps test_lock should be done with an
5568 static __be32
nfsd_test_lock(struct svc_rqst
*rqstp
, struct svc_fh
*fhp
, struct file_lock
*lock
)
5571 __be32 err
= nfsd_open(rqstp
, fhp
, S_IFREG
, NFSD_MAY_READ
, &file
);
5573 err
= nfserrno(vfs_test_lock(file
, lock
));
5583 nfsd4_lockt(struct svc_rqst
*rqstp
, struct nfsd4_compound_state
*cstate
,
5584 struct nfsd4_lockt
*lockt
)
5586 struct file_lock
*file_lock
= NULL
;
5587 struct nfs4_lockowner
*lo
= NULL
;
5589 struct nfsd_net
*nn
= net_generic(SVC_NET(rqstp
), nfsd_net_id
);
5591 if (locks_in_grace(SVC_NET(rqstp
)))
5592 return nfserr_grace
;
5594 if (check_lock_length(lockt
->lt_offset
, lockt
->lt_length
))
5595 return nfserr_inval
;
5597 if (!nfsd4_has_session(cstate
)) {
5598 status
= lookup_clientid(&lockt
->lt_clientid
, cstate
, nn
);
5603 if ((status
= fh_verify(rqstp
, &cstate
->current_fh
, S_IFREG
, 0)))
5606 file_lock
= locks_alloc_lock();
5608 dprintk("NFSD: %s: unable to allocate lock!\n", __func__
);
5609 status
= nfserr_jukebox
;
5613 switch (lockt
->lt_type
) {
5616 file_lock
->fl_type
= F_RDLCK
;
5619 case NFS4_WRITEW_LT
:
5620 file_lock
->fl_type
= F_WRLCK
;
5623 dprintk("NFSD: nfs4_lockt: bad lock type!\n");
5624 status
= nfserr_inval
;
5628 lo
= find_lockowner_str(cstate
->clp
, &lockt
->lt_owner
);
5630 file_lock
->fl_owner
= (fl_owner_t
)lo
;
5631 file_lock
->fl_pid
= current
->tgid
;
5632 file_lock
->fl_flags
= FL_POSIX
;
5634 file_lock
->fl_start
= lockt
->lt_offset
;
5635 file_lock
->fl_end
= last_byte_offset(lockt
->lt_offset
, lockt
->lt_length
);
5637 nfs4_transform_lock_offset(file_lock
);
5639 status
= nfsd_test_lock(rqstp
, &cstate
->current_fh
, file_lock
);
5643 if (file_lock
->fl_type
!= F_UNLCK
) {
5644 status
= nfserr_denied
;
5645 nfs4_set_lock_denied(file_lock
, &lockt
->lt_denied
);
5649 nfs4_put_stateowner(&lo
->lo_owner
);
5651 locks_free_lock(file_lock
);
5656 nfsd4_locku(struct svc_rqst
*rqstp
, struct nfsd4_compound_state
*cstate
,
5657 struct nfsd4_locku
*locku
)
5659 struct nfs4_ol_stateid
*stp
;
5660 struct file
*filp
= NULL
;
5661 struct file_lock
*file_lock
= NULL
;
5664 struct nfsd_net
*nn
= net_generic(SVC_NET(rqstp
), nfsd_net_id
);
5666 dprintk("NFSD: nfsd4_locku: start=%Ld length=%Ld\n",
5667 (long long) locku
->lu_offset
,
5668 (long long) locku
->lu_length
);
5670 if (check_lock_length(locku
->lu_offset
, locku
->lu_length
))
5671 return nfserr_inval
;
5673 status
= nfs4_preprocess_seqid_op(cstate
, locku
->lu_seqid
,
5674 &locku
->lu_stateid
, NFS4_LOCK_STID
,
5678 filp
= find_any_file(stp
->st_stid
.sc_file
);
5680 status
= nfserr_lock_range
;
5683 file_lock
= locks_alloc_lock();
5685 dprintk("NFSD: %s: unable to allocate lock!\n", __func__
);
5686 status
= nfserr_jukebox
;
5690 file_lock
->fl_type
= F_UNLCK
;
5691 file_lock
->fl_owner
= (fl_owner_t
)lockowner(nfs4_get_stateowner(stp
->st_stateowner
));
5692 file_lock
->fl_pid
= current
->tgid
;
5693 file_lock
->fl_file
= filp
;
5694 file_lock
->fl_flags
= FL_POSIX
;
5695 file_lock
->fl_lmops
= &nfsd_posix_mng_ops
;
5696 file_lock
->fl_start
= locku
->lu_offset
;
5698 file_lock
->fl_end
= last_byte_offset(locku
->lu_offset
,
5700 nfs4_transform_lock_offset(file_lock
);
5702 err
= vfs_lock_file(filp
, F_SETLK
, file_lock
, NULL
);
5704 dprintk("NFSD: nfs4_locku: vfs_lock_file failed!\n");
5707 update_stateid(&stp
->st_stid
.sc_stateid
);
5708 memcpy(&locku
->lu_stateid
, &stp
->st_stid
.sc_stateid
, sizeof(stateid_t
));
5712 nfs4_put_stid(&stp
->st_stid
);
5714 nfsd4_bump_seqid(cstate
, status
);
5716 locks_free_lock(file_lock
);
5720 status
= nfserrno(err
);
5726 * true: locks held by lockowner
5727 * false: no locks held by lockowner
5730 check_for_locks(struct nfs4_file
*fp
, struct nfs4_lockowner
*lowner
)
5732 struct file_lock
*fl
;
5734 struct file
*filp
= find_any_file(fp
);
5735 struct inode
*inode
;
5736 struct file_lock_context
*flctx
;
5739 /* Any valid lock stateid should have some sort of access */
5744 inode
= file_inode(filp
);
5745 flctx
= inode
->i_flctx
;
5747 if (flctx
&& !list_empty_careful(&flctx
->flc_posix
)) {
5748 spin_lock(&flctx
->flc_lock
);
5749 list_for_each_entry(fl
, &flctx
->flc_posix
, fl_list
) {
5750 if (fl
->fl_owner
== (fl_owner_t
)lowner
) {
5755 spin_unlock(&flctx
->flc_lock
);
5762 nfsd4_release_lockowner(struct svc_rqst
*rqstp
,
5763 struct nfsd4_compound_state
*cstate
,
5764 struct nfsd4_release_lockowner
*rlockowner
)
5766 clientid_t
*clid
= &rlockowner
->rl_clientid
;
5767 struct nfs4_stateowner
*sop
;
5768 struct nfs4_lockowner
*lo
= NULL
;
5769 struct nfs4_ol_stateid
*stp
;
5770 struct xdr_netobj
*owner
= &rlockowner
->rl_owner
;
5771 unsigned int hashval
= ownerstr_hashval(owner
);
5773 struct nfsd_net
*nn
= net_generic(SVC_NET(rqstp
), nfsd_net_id
);
5774 struct nfs4_client
*clp
;
5776 dprintk("nfsd4_release_lockowner clientid: (%08x/%08x):\n",
5777 clid
->cl_boot
, clid
->cl_id
);
5779 status
= lookup_clientid(clid
, cstate
, nn
);
5784 /* Find the matching lock stateowner */
5785 spin_lock(&clp
->cl_lock
);
5786 list_for_each_entry(sop
, &clp
->cl_ownerstr_hashtbl
[hashval
],
5789 if (sop
->so_is_open_owner
|| !same_owner_str(sop
, owner
))
5792 /* see if there are still any locks associated with it */
5793 lo
= lockowner(sop
);
5794 list_for_each_entry(stp
, &sop
->so_stateids
, st_perstateowner
) {
5795 if (check_for_locks(stp
->st_stid
.sc_file
, lo
)) {
5796 status
= nfserr_locks_held
;
5797 spin_unlock(&clp
->cl_lock
);
5802 nfs4_get_stateowner(sop
);
5805 spin_unlock(&clp
->cl_lock
);
5807 release_lockowner(lo
);
5811 static inline struct nfs4_client_reclaim
*
5814 return kmalloc(sizeof(struct nfs4_client_reclaim
), GFP_KERNEL
);
5818 nfs4_has_reclaimed_state(const char *name
, struct nfsd_net
*nn
)
5820 struct nfs4_client_reclaim
*crp
;
5822 crp
= nfsd4_find_reclaim_client(name
, nn
);
5823 return (crp
&& crp
->cr_clp
);
5827 * failure => all reset bets are off, nfserr_no_grace...
5829 struct nfs4_client_reclaim
*
5830 nfs4_client_to_reclaim(const char *name
, struct nfsd_net
*nn
)
5832 unsigned int strhashval
;
5833 struct nfs4_client_reclaim
*crp
;
5835 dprintk("NFSD nfs4_client_to_reclaim NAME: %.*s\n", HEXDIR_LEN
, name
);
5836 crp
= alloc_reclaim();
5838 strhashval
= clientstr_hashval(name
);
5839 INIT_LIST_HEAD(&crp
->cr_strhash
);
5840 list_add(&crp
->cr_strhash
, &nn
->reclaim_str_hashtbl
[strhashval
]);
5841 memcpy(crp
->cr_recdir
, name
, HEXDIR_LEN
);
5843 nn
->reclaim_str_hashtbl_size
++;
5849 nfs4_remove_reclaim_record(struct nfs4_client_reclaim
*crp
, struct nfsd_net
*nn
)
5851 list_del(&crp
->cr_strhash
);
5853 nn
->reclaim_str_hashtbl_size
--;
5857 nfs4_release_reclaim(struct nfsd_net
*nn
)
5859 struct nfs4_client_reclaim
*crp
= NULL
;
5862 for (i
= 0; i
< CLIENT_HASH_SIZE
; i
++) {
5863 while (!list_empty(&nn
->reclaim_str_hashtbl
[i
])) {
5864 crp
= list_entry(nn
->reclaim_str_hashtbl
[i
].next
,
5865 struct nfs4_client_reclaim
, cr_strhash
);
5866 nfs4_remove_reclaim_record(crp
, nn
);
5869 WARN_ON_ONCE(nn
->reclaim_str_hashtbl_size
);
5873 * called from OPEN, CLAIM_PREVIOUS with a new clientid. */
5874 struct nfs4_client_reclaim
*
5875 nfsd4_find_reclaim_client(const char *recdir
, struct nfsd_net
*nn
)
5877 unsigned int strhashval
;
5878 struct nfs4_client_reclaim
*crp
= NULL
;
5880 dprintk("NFSD: nfs4_find_reclaim_client for recdir %s\n", recdir
);
5882 strhashval
= clientstr_hashval(recdir
);
5883 list_for_each_entry(crp
, &nn
->reclaim_str_hashtbl
[strhashval
], cr_strhash
) {
5884 if (same_name(crp
->cr_recdir
, recdir
)) {
5892 * Called from OPEN. Look for clientid in reclaim list.
5895 nfs4_check_open_reclaim(clientid_t
*clid
,
5896 struct nfsd4_compound_state
*cstate
,
5897 struct nfsd_net
*nn
)
5901 /* find clientid in conf_id_hashtbl */
5902 status
= lookup_clientid(clid
, cstate
, nn
);
5904 return nfserr_reclaim_bad
;
5906 if (test_bit(NFSD4_CLIENT_RECLAIM_COMPLETE
, &cstate
->clp
->cl_flags
))
5907 return nfserr_no_grace
;
5909 if (nfsd4_client_record_check(cstate
->clp
))
5910 return nfserr_reclaim_bad
;
5915 #ifdef CONFIG_NFSD_FAULT_INJECTION
5917 put_client(struct nfs4_client
*clp
)
5919 atomic_dec(&clp
->cl_refcount
);
5922 static struct nfs4_client
*
5923 nfsd_find_client(struct sockaddr_storage
*addr
, size_t addr_size
)
5925 struct nfs4_client
*clp
;
5926 struct nfsd_net
*nn
= net_generic(current
->nsproxy
->net_ns
,
5929 if (!nfsd_netns_ready(nn
))
5932 list_for_each_entry(clp
, &nn
->client_lru
, cl_lru
) {
5933 if (memcmp(&clp
->cl_addr
, addr
, addr_size
) == 0)
5940 nfsd_inject_print_clients(void)
5942 struct nfs4_client
*clp
;
5944 struct nfsd_net
*nn
= net_generic(current
->nsproxy
->net_ns
,
5946 char buf
[INET6_ADDRSTRLEN
];
5948 if (!nfsd_netns_ready(nn
))
5951 spin_lock(&nn
->client_lock
);
5952 list_for_each_entry(clp
, &nn
->client_lru
, cl_lru
) {
5953 rpc_ntop((struct sockaddr
*)&clp
->cl_addr
, buf
, sizeof(buf
));
5954 pr_info("NFS Client: %s\n", buf
);
5957 spin_unlock(&nn
->client_lock
);
5963 nfsd_inject_forget_client(struct sockaddr_storage
*addr
, size_t addr_size
)
5966 struct nfs4_client
*clp
;
5967 struct nfsd_net
*nn
= net_generic(current
->nsproxy
->net_ns
,
5970 if (!nfsd_netns_ready(nn
))
5973 spin_lock(&nn
->client_lock
);
5974 clp
= nfsd_find_client(addr
, addr_size
);
5976 if (mark_client_expired_locked(clp
) == nfs_ok
)
5981 spin_unlock(&nn
->client_lock
);
5990 nfsd_inject_forget_clients(u64 max
)
5993 struct nfs4_client
*clp
, *next
;
5994 struct nfsd_net
*nn
= net_generic(current
->nsproxy
->net_ns
,
5996 LIST_HEAD(reaplist
);
5998 if (!nfsd_netns_ready(nn
))
6001 spin_lock(&nn
->client_lock
);
6002 list_for_each_entry_safe(clp
, next
, &nn
->client_lru
, cl_lru
) {
6003 if (mark_client_expired_locked(clp
) == nfs_ok
) {
6004 list_add(&clp
->cl_lru
, &reaplist
);
6005 if (max
!= 0 && ++count
>= max
)
6009 spin_unlock(&nn
->client_lock
);
6011 list_for_each_entry_safe(clp
, next
, &reaplist
, cl_lru
)
6017 static void nfsd_print_count(struct nfs4_client
*clp
, unsigned int count
,
6020 char buf
[INET6_ADDRSTRLEN
];
6021 rpc_ntop((struct sockaddr
*)&clp
->cl_addr
, buf
, sizeof(buf
));
6022 printk(KERN_INFO
"NFS Client: %s has %u %s\n", buf
, count
, type
);
6026 nfsd_inject_add_lock_to_list(struct nfs4_ol_stateid
*lst
,
6027 struct list_head
*collect
)
6029 struct nfs4_client
*clp
= lst
->st_stid
.sc_client
;
6030 struct nfsd_net
*nn
= net_generic(current
->nsproxy
->net_ns
,
6036 lockdep_assert_held(&nn
->client_lock
);
6037 atomic_inc(&clp
->cl_refcount
);
6038 list_add(&lst
->st_locks
, collect
);
6041 static u64
nfsd_foreach_client_lock(struct nfs4_client
*clp
, u64 max
,
6042 struct list_head
*collect
,
6043 bool (*func
)(struct nfs4_ol_stateid
*))
6045 struct nfs4_openowner
*oop
;
6046 struct nfs4_ol_stateid
*stp
, *st_next
;
6047 struct nfs4_ol_stateid
*lst
, *lst_next
;
6050 spin_lock(&clp
->cl_lock
);
6051 list_for_each_entry(oop
, &clp
->cl_openowners
, oo_perclient
) {
6052 list_for_each_entry_safe(stp
, st_next
,
6053 &oop
->oo_owner
.so_stateids
, st_perstateowner
) {
6054 list_for_each_entry_safe(lst
, lst_next
,
6055 &stp
->st_locks
, st_locks
) {
6058 nfsd_inject_add_lock_to_list(lst
,
6063 * Despite the fact that these functions deal
6064 * with 64-bit integers for "count", we must
6065 * ensure that it doesn't blow up the
6066 * clp->cl_refcount. Throw a warning if we
6067 * start to approach INT_MAX here.
6069 WARN_ON_ONCE(count
== (INT_MAX
/ 2));
6076 spin_unlock(&clp
->cl_lock
);
6082 nfsd_collect_client_locks(struct nfs4_client
*clp
, struct list_head
*collect
,
6085 return nfsd_foreach_client_lock(clp
, max
, collect
, unhash_lock_stateid
);
6089 nfsd_print_client_locks(struct nfs4_client
*clp
)
6091 u64 count
= nfsd_foreach_client_lock(clp
, 0, NULL
, NULL
);
6092 nfsd_print_count(clp
, count
, "locked files");
6097 nfsd_inject_print_locks(void)
6099 struct nfs4_client
*clp
;
6101 struct nfsd_net
*nn
= net_generic(current
->nsproxy
->net_ns
,
6104 if (!nfsd_netns_ready(nn
))
6107 spin_lock(&nn
->client_lock
);
6108 list_for_each_entry(clp
, &nn
->client_lru
, cl_lru
)
6109 count
+= nfsd_print_client_locks(clp
);
6110 spin_unlock(&nn
->client_lock
);
6116 nfsd_reap_locks(struct list_head
*reaplist
)
6118 struct nfs4_client
*clp
;
6119 struct nfs4_ol_stateid
*stp
, *next
;
6121 list_for_each_entry_safe(stp
, next
, reaplist
, st_locks
) {
6122 list_del_init(&stp
->st_locks
);
6123 clp
= stp
->st_stid
.sc_client
;
6124 nfs4_put_stid(&stp
->st_stid
);
6130 nfsd_inject_forget_client_locks(struct sockaddr_storage
*addr
, size_t addr_size
)
6132 unsigned int count
= 0;
6133 struct nfs4_client
*clp
;
6134 struct nfsd_net
*nn
= net_generic(current
->nsproxy
->net_ns
,
6136 LIST_HEAD(reaplist
);
6138 if (!nfsd_netns_ready(nn
))
6141 spin_lock(&nn
->client_lock
);
6142 clp
= nfsd_find_client(addr
, addr_size
);
6144 count
= nfsd_collect_client_locks(clp
, &reaplist
, 0);
6145 spin_unlock(&nn
->client_lock
);
6146 nfsd_reap_locks(&reaplist
);
6151 nfsd_inject_forget_locks(u64 max
)
6154 struct nfs4_client
*clp
;
6155 struct nfsd_net
*nn
= net_generic(current
->nsproxy
->net_ns
,
6157 LIST_HEAD(reaplist
);
6159 if (!nfsd_netns_ready(nn
))
6162 spin_lock(&nn
->client_lock
);
6163 list_for_each_entry(clp
, &nn
->client_lru
, cl_lru
) {
6164 count
+= nfsd_collect_client_locks(clp
, &reaplist
, max
- count
);
6165 if (max
!= 0 && count
>= max
)
6168 spin_unlock(&nn
->client_lock
);
6169 nfsd_reap_locks(&reaplist
);
6174 nfsd_foreach_client_openowner(struct nfs4_client
*clp
, u64 max
,
6175 struct list_head
*collect
,
6176 void (*func
)(struct nfs4_openowner
*))
6178 struct nfs4_openowner
*oop
, *next
;
6179 struct nfsd_net
*nn
= net_generic(current
->nsproxy
->net_ns
,
6183 lockdep_assert_held(&nn
->client_lock
);
6185 spin_lock(&clp
->cl_lock
);
6186 list_for_each_entry_safe(oop
, next
, &clp
->cl_openowners
, oo_perclient
) {
6190 atomic_inc(&clp
->cl_refcount
);
6191 list_add(&oop
->oo_perclient
, collect
);
6196 * Despite the fact that these functions deal with
6197 * 64-bit integers for "count", we must ensure that
6198 * it doesn't blow up the clp->cl_refcount. Throw a
6199 * warning if we start to approach INT_MAX here.
6201 WARN_ON_ONCE(count
== (INT_MAX
/ 2));
6205 spin_unlock(&clp
->cl_lock
);
6211 nfsd_print_client_openowners(struct nfs4_client
*clp
)
6213 u64 count
= nfsd_foreach_client_openowner(clp
, 0, NULL
, NULL
);
6215 nfsd_print_count(clp
, count
, "openowners");
6220 nfsd_collect_client_openowners(struct nfs4_client
*clp
,
6221 struct list_head
*collect
, u64 max
)
6223 return nfsd_foreach_client_openowner(clp
, max
, collect
,
6224 unhash_openowner_locked
);
6228 nfsd_inject_print_openowners(void)
6230 struct nfs4_client
*clp
;
6232 struct nfsd_net
*nn
= net_generic(current
->nsproxy
->net_ns
,
6235 if (!nfsd_netns_ready(nn
))
6238 spin_lock(&nn
->client_lock
);
6239 list_for_each_entry(clp
, &nn
->client_lru
, cl_lru
)
6240 count
+= nfsd_print_client_openowners(clp
);
6241 spin_unlock(&nn
->client_lock
);
6247 nfsd_reap_openowners(struct list_head
*reaplist
)
6249 struct nfs4_client
*clp
;
6250 struct nfs4_openowner
*oop
, *next
;
6252 list_for_each_entry_safe(oop
, next
, reaplist
, oo_perclient
) {
6253 list_del_init(&oop
->oo_perclient
);
6254 clp
= oop
->oo_owner
.so_client
;
6255 release_openowner(oop
);
6261 nfsd_inject_forget_client_openowners(struct sockaddr_storage
*addr
,
6264 unsigned int count
= 0;
6265 struct nfs4_client
*clp
;
6266 struct nfsd_net
*nn
= net_generic(current
->nsproxy
->net_ns
,
6268 LIST_HEAD(reaplist
);
6270 if (!nfsd_netns_ready(nn
))
6273 spin_lock(&nn
->client_lock
);
6274 clp
= nfsd_find_client(addr
, addr_size
);
6276 count
= nfsd_collect_client_openowners(clp
, &reaplist
, 0);
6277 spin_unlock(&nn
->client_lock
);
6278 nfsd_reap_openowners(&reaplist
);
6283 nfsd_inject_forget_openowners(u64 max
)
6286 struct nfs4_client
*clp
;
6287 struct nfsd_net
*nn
= net_generic(current
->nsproxy
->net_ns
,
6289 LIST_HEAD(reaplist
);
6291 if (!nfsd_netns_ready(nn
))
6294 spin_lock(&nn
->client_lock
);
6295 list_for_each_entry(clp
, &nn
->client_lru
, cl_lru
) {
6296 count
+= nfsd_collect_client_openowners(clp
, &reaplist
,
6298 if (max
!= 0 && count
>= max
)
6301 spin_unlock(&nn
->client_lock
);
6302 nfsd_reap_openowners(&reaplist
);
6306 static u64
nfsd_find_all_delegations(struct nfs4_client
*clp
, u64 max
,
6307 struct list_head
*victims
)
6309 struct nfs4_delegation
*dp
, *next
;
6310 struct nfsd_net
*nn
= net_generic(current
->nsproxy
->net_ns
,
6314 lockdep_assert_held(&nn
->client_lock
);
6316 spin_lock(&state_lock
);
6317 list_for_each_entry_safe(dp
, next
, &clp
->cl_delegations
, dl_perclnt
) {
6320 * It's not safe to mess with delegations that have a
6321 * non-zero dl_time. They might have already been broken
6322 * and could be processed by the laundromat outside of
6323 * the state_lock. Just leave them be.
6325 if (dp
->dl_time
!= 0)
6328 atomic_inc(&clp
->cl_refcount
);
6329 WARN_ON(!unhash_delegation_locked(dp
));
6330 list_add(&dp
->dl_recall_lru
, victims
);
6334 * Despite the fact that these functions deal with
6335 * 64-bit integers for "count", we must ensure that
6336 * it doesn't blow up the clp->cl_refcount. Throw a
6337 * warning if we start to approach INT_MAX here.
6339 WARN_ON_ONCE(count
== (INT_MAX
/ 2));
6343 spin_unlock(&state_lock
);
6348 nfsd_print_client_delegations(struct nfs4_client
*clp
)
6350 u64 count
= nfsd_find_all_delegations(clp
, 0, NULL
);
6352 nfsd_print_count(clp
, count
, "delegations");
6357 nfsd_inject_print_delegations(void)
6359 struct nfs4_client
*clp
;
6361 struct nfsd_net
*nn
= net_generic(current
->nsproxy
->net_ns
,
6364 if (!nfsd_netns_ready(nn
))
6367 spin_lock(&nn
->client_lock
);
6368 list_for_each_entry(clp
, &nn
->client_lru
, cl_lru
)
6369 count
+= nfsd_print_client_delegations(clp
);
6370 spin_unlock(&nn
->client_lock
);
6376 nfsd_forget_delegations(struct list_head
*reaplist
)
6378 struct nfs4_client
*clp
;
6379 struct nfs4_delegation
*dp
, *next
;
6381 list_for_each_entry_safe(dp
, next
, reaplist
, dl_recall_lru
) {
6382 list_del_init(&dp
->dl_recall_lru
);
6383 clp
= dp
->dl_stid
.sc_client
;
6384 revoke_delegation(dp
);
6390 nfsd_inject_forget_client_delegations(struct sockaddr_storage
*addr
,
6394 struct nfs4_client
*clp
;
6395 struct nfsd_net
*nn
= net_generic(current
->nsproxy
->net_ns
,
6397 LIST_HEAD(reaplist
);
6399 if (!nfsd_netns_ready(nn
))
6402 spin_lock(&nn
->client_lock
);
6403 clp
= nfsd_find_client(addr
, addr_size
);
6405 count
= nfsd_find_all_delegations(clp
, 0, &reaplist
);
6406 spin_unlock(&nn
->client_lock
);
6408 nfsd_forget_delegations(&reaplist
);
6413 nfsd_inject_forget_delegations(u64 max
)
6416 struct nfs4_client
*clp
;
6417 struct nfsd_net
*nn
= net_generic(current
->nsproxy
->net_ns
,
6419 LIST_HEAD(reaplist
);
6421 if (!nfsd_netns_ready(nn
))
6424 spin_lock(&nn
->client_lock
);
6425 list_for_each_entry(clp
, &nn
->client_lru
, cl_lru
) {
6426 count
+= nfsd_find_all_delegations(clp
, max
- count
, &reaplist
);
6427 if (max
!= 0 && count
>= max
)
6430 spin_unlock(&nn
->client_lock
);
6431 nfsd_forget_delegations(&reaplist
);
6436 nfsd_recall_delegations(struct list_head
*reaplist
)
6438 struct nfs4_client
*clp
;
6439 struct nfs4_delegation
*dp
, *next
;
6441 list_for_each_entry_safe(dp
, next
, reaplist
, dl_recall_lru
) {
6442 list_del_init(&dp
->dl_recall_lru
);
6443 clp
= dp
->dl_stid
.sc_client
;
6445 * We skipped all entries that had a zero dl_time before,
6446 * so we can now reset the dl_time back to 0. If a delegation
6447 * break comes in now, then it won't make any difference since
6448 * we're recalling it either way.
6450 spin_lock(&state_lock
);
6452 spin_unlock(&state_lock
);
6453 nfsd_break_one_deleg(dp
);
6459 nfsd_inject_recall_client_delegations(struct sockaddr_storage
*addr
,
6463 struct nfs4_client
*clp
;
6464 struct nfsd_net
*nn
= net_generic(current
->nsproxy
->net_ns
,
6466 LIST_HEAD(reaplist
);
6468 if (!nfsd_netns_ready(nn
))
6471 spin_lock(&nn
->client_lock
);
6472 clp
= nfsd_find_client(addr
, addr_size
);
6474 count
= nfsd_find_all_delegations(clp
, 0, &reaplist
);
6475 spin_unlock(&nn
->client_lock
);
6477 nfsd_recall_delegations(&reaplist
);
6482 nfsd_inject_recall_delegations(u64 max
)
6485 struct nfs4_client
*clp
, *next
;
6486 struct nfsd_net
*nn
= net_generic(current
->nsproxy
->net_ns
,
6488 LIST_HEAD(reaplist
);
6490 if (!nfsd_netns_ready(nn
))
6493 spin_lock(&nn
->client_lock
);
6494 list_for_each_entry_safe(clp
, next
, &nn
->client_lru
, cl_lru
) {
6495 count
+= nfsd_find_all_delegations(clp
, max
- count
, &reaplist
);
6496 if (max
!= 0 && ++count
>= max
)
6499 spin_unlock(&nn
->client_lock
);
6500 nfsd_recall_delegations(&reaplist
);
6503 #endif /* CONFIG_NFSD_FAULT_INJECTION */
6506 * Since the lifetime of a delegation isn't limited to that of an open, a
6507 * client may quite reasonably hang on to a delegation as long as it has
6508 * the inode cached. This becomes an obvious problem the first time a
6509 * client's inode cache approaches the size of the server's total memory.
6511 * For now we avoid this problem by imposing a hard limit on the number
6512 * of delegations, which varies according to the server's memory size.
6515 set_max_delegations(void)
6518 * Allow at most 4 delegations per megabyte of RAM. Quick
6519 * estimates suggest that in the worst case (where every delegation
6520 * is for a different inode), a delegation could take about 1.5K,
6521 * giving a worst case usage of about 6% of memory.
6523 max_delegations
= nr_free_buffer_pages() >> (20 - 2 - PAGE_SHIFT
);
6526 static int nfs4_state_create_net(struct net
*net
)
6528 struct nfsd_net
*nn
= net_generic(net
, nfsd_net_id
);
6531 nn
->conf_id_hashtbl
= kmalloc(sizeof(struct list_head
) *
6532 CLIENT_HASH_SIZE
, GFP_KERNEL
);
6533 if (!nn
->conf_id_hashtbl
)
6535 nn
->unconf_id_hashtbl
= kmalloc(sizeof(struct list_head
) *
6536 CLIENT_HASH_SIZE
, GFP_KERNEL
);
6537 if (!nn
->unconf_id_hashtbl
)
6539 nn
->sessionid_hashtbl
= kmalloc(sizeof(struct list_head
) *
6540 SESSION_HASH_SIZE
, GFP_KERNEL
);
6541 if (!nn
->sessionid_hashtbl
)
6544 for (i
= 0; i
< CLIENT_HASH_SIZE
; i
++) {
6545 INIT_LIST_HEAD(&nn
->conf_id_hashtbl
[i
]);
6546 INIT_LIST_HEAD(&nn
->unconf_id_hashtbl
[i
]);
6548 for (i
= 0; i
< SESSION_HASH_SIZE
; i
++)
6549 INIT_LIST_HEAD(&nn
->sessionid_hashtbl
[i
]);
6550 nn
->conf_name_tree
= RB_ROOT
;
6551 nn
->unconf_name_tree
= RB_ROOT
;
6552 INIT_LIST_HEAD(&nn
->client_lru
);
6553 INIT_LIST_HEAD(&nn
->close_lru
);
6554 INIT_LIST_HEAD(&nn
->del_recall_lru
);
6555 spin_lock_init(&nn
->client_lock
);
6557 INIT_DELAYED_WORK(&nn
->laundromat_work
, laundromat_main
);
6563 kfree(nn
->unconf_id_hashtbl
);
6565 kfree(nn
->conf_id_hashtbl
);
6571 nfs4_state_destroy_net(struct net
*net
)
6574 struct nfs4_client
*clp
= NULL
;
6575 struct nfsd_net
*nn
= net_generic(net
, nfsd_net_id
);
6577 for (i
= 0; i
< CLIENT_HASH_SIZE
; i
++) {
6578 while (!list_empty(&nn
->conf_id_hashtbl
[i
])) {
6579 clp
= list_entry(nn
->conf_id_hashtbl
[i
].next
, struct nfs4_client
, cl_idhash
);
6580 destroy_client(clp
);
6584 for (i
= 0; i
< CLIENT_HASH_SIZE
; i
++) {
6585 while (!list_empty(&nn
->unconf_id_hashtbl
[i
])) {
6586 clp
= list_entry(nn
->unconf_id_hashtbl
[i
].next
, struct nfs4_client
, cl_idhash
);
6587 destroy_client(clp
);
6591 kfree(nn
->sessionid_hashtbl
);
6592 kfree(nn
->unconf_id_hashtbl
);
6593 kfree(nn
->conf_id_hashtbl
);
6598 nfs4_state_start_net(struct net
*net
)
6600 struct nfsd_net
*nn
= net_generic(net
, nfsd_net_id
);
6603 ret
= nfs4_state_create_net(net
);
6606 nn
->boot_time
= get_seconds();
6607 nn
->grace_ended
= false;
6608 nn
->nfsd4_manager
.block_opens
= true;
6609 locks_start_grace(net
, &nn
->nfsd4_manager
);
6610 nfsd4_client_tracking_init(net
);
6611 printk(KERN_INFO
"NFSD: starting %ld-second grace period (net %p)\n",
6612 nn
->nfsd4_grace
, net
);
6613 queue_delayed_work(laundry_wq
, &nn
->laundromat_work
, nn
->nfsd4_grace
* HZ
);
6617 /* initialization to perform when the nfsd service is started: */
6620 nfs4_state_start(void)
6624 ret
= set_callback_cred();
6627 laundry_wq
= alloc_workqueue("%s", WQ_UNBOUND
, 0, "nfsd4");
6628 if (laundry_wq
== NULL
) {
6632 ret
= nfsd4_create_callback_queue();
6634 goto out_free_laundry
;
6636 set_max_delegations();
6641 destroy_workqueue(laundry_wq
);
6647 nfs4_state_shutdown_net(struct net
*net
)
6649 struct nfs4_delegation
*dp
= NULL
;
6650 struct list_head
*pos
, *next
, reaplist
;
6651 struct nfsd_net
*nn
= net_generic(net
, nfsd_net_id
);
6653 cancel_delayed_work_sync(&nn
->laundromat_work
);
6654 locks_end_grace(&nn
->nfsd4_manager
);
6656 INIT_LIST_HEAD(&reaplist
);
6657 spin_lock(&state_lock
);
6658 list_for_each_safe(pos
, next
, &nn
->del_recall_lru
) {
6659 dp
= list_entry (pos
, struct nfs4_delegation
, dl_recall_lru
);
6660 WARN_ON(!unhash_delegation_locked(dp
));
6661 list_add(&dp
->dl_recall_lru
, &reaplist
);
6663 spin_unlock(&state_lock
);
6664 list_for_each_safe(pos
, next
, &reaplist
) {
6665 dp
= list_entry (pos
, struct nfs4_delegation
, dl_recall_lru
);
6666 list_del_init(&dp
->dl_recall_lru
);
6667 put_clnt_odstate(dp
->dl_clnt_odstate
);
6668 nfs4_put_deleg_lease(dp
->dl_stid
.sc_file
);
6669 nfs4_put_stid(&dp
->dl_stid
);
6672 nfsd4_client_tracking_exit(net
);
6673 nfs4_state_destroy_net(net
);
6677 nfs4_state_shutdown(void)
6679 destroy_workqueue(laundry_wq
);
6680 nfsd4_destroy_callback_queue();
6684 get_stateid(struct nfsd4_compound_state
*cstate
, stateid_t
*stateid
)
6686 if (HAS_STATE_ID(cstate
, CURRENT_STATE_ID_FLAG
) && CURRENT_STATEID(stateid
))
6687 memcpy(stateid
, &cstate
->current_stateid
, sizeof(stateid_t
));
6691 put_stateid(struct nfsd4_compound_state
*cstate
, stateid_t
*stateid
)
6693 if (cstate
->minorversion
) {
6694 memcpy(&cstate
->current_stateid
, stateid
, sizeof(stateid_t
));
6695 SET_STATE_ID(cstate
, CURRENT_STATE_ID_FLAG
);
6700 clear_current_stateid(struct nfsd4_compound_state
*cstate
)
6702 CLEAR_STATE_ID(cstate
, CURRENT_STATE_ID_FLAG
);
6706 * functions to set current state id
6709 nfsd4_set_opendowngradestateid(struct nfsd4_compound_state
*cstate
, struct nfsd4_open_downgrade
*odp
)
6711 put_stateid(cstate
, &odp
->od_stateid
);
6715 nfsd4_set_openstateid(struct nfsd4_compound_state
*cstate
, struct nfsd4_open
*open
)
6717 put_stateid(cstate
, &open
->op_stateid
);
6721 nfsd4_set_closestateid(struct nfsd4_compound_state
*cstate
, struct nfsd4_close
*close
)
6723 put_stateid(cstate
, &close
->cl_stateid
);
6727 nfsd4_set_lockstateid(struct nfsd4_compound_state
*cstate
, struct nfsd4_lock
*lock
)
6729 put_stateid(cstate
, &lock
->lk_resp_stateid
);
6733 * functions to consume current state id
6737 nfsd4_get_opendowngradestateid(struct nfsd4_compound_state
*cstate
, struct nfsd4_open_downgrade
*odp
)
6739 get_stateid(cstate
, &odp
->od_stateid
);
6743 nfsd4_get_delegreturnstateid(struct nfsd4_compound_state
*cstate
, struct nfsd4_delegreturn
*drp
)
6745 get_stateid(cstate
, &drp
->dr_stateid
);
6749 nfsd4_get_freestateid(struct nfsd4_compound_state
*cstate
, struct nfsd4_free_stateid
*fsp
)
6751 get_stateid(cstate
, &fsp
->fr_stateid
);
6755 nfsd4_get_setattrstateid(struct nfsd4_compound_state
*cstate
, struct nfsd4_setattr
*setattr
)
6757 get_stateid(cstate
, &setattr
->sa_stateid
);
6761 nfsd4_get_closestateid(struct nfsd4_compound_state
*cstate
, struct nfsd4_close
*close
)
6763 get_stateid(cstate
, &close
->cl_stateid
);
6767 nfsd4_get_lockustateid(struct nfsd4_compound_state
*cstate
, struct nfsd4_locku
*locku
)
6769 get_stateid(cstate
, &locku
->lu_stateid
);
6773 nfsd4_get_readstateid(struct nfsd4_compound_state
*cstate
, struct nfsd4_read
*read
)
6775 get_stateid(cstate
, &read
->rd_stateid
);
6779 nfsd4_get_writestateid(struct nfsd4_compound_state
*cstate
, struct nfsd4_write
*write
)
6781 get_stateid(cstate
, &write
->wr_stateid
);