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
= {
66 static const stateid_t close_stateid
= {
67 .si_generation
= 0xffffffffU
,
70 static u64 current_sessionid
= 1;
72 #define ZERO_STATEID(stateid) (!memcmp((stateid), &zero_stateid, sizeof(stateid_t)))
73 #define ONE_STATEID(stateid) (!memcmp((stateid), &one_stateid, sizeof(stateid_t)))
74 #define CURRENT_STATEID(stateid) (!memcmp((stateid), ¤tstateid, sizeof(stateid_t)))
75 #define CLOSE_STATEID(stateid) (!memcmp((stateid), &close_stateid, sizeof(stateid_t)))
77 /* forward declarations */
78 static bool check_for_locks(struct nfs4_file
*fp
, struct nfs4_lockowner
*lowner
);
79 static void nfs4_free_ol_stateid(struct nfs4_stid
*stid
);
84 * Currently used for the del_recall_lru and file hash table. In an
85 * effort to decrease the scope of the client_mutex, this spinlock may
86 * eventually cover more:
88 static DEFINE_SPINLOCK(state_lock
);
91 * A waitqueue for all in-progress 4.0 CLOSE operations that are waiting for
92 * the refcount on the open stateid to drop.
94 static DECLARE_WAIT_QUEUE_HEAD(close_wq
);
96 static struct kmem_cache
*openowner_slab
;
97 static struct kmem_cache
*lockowner_slab
;
98 static struct kmem_cache
*file_slab
;
99 static struct kmem_cache
*stateid_slab
;
100 static struct kmem_cache
*deleg_slab
;
101 static struct kmem_cache
*odstate_slab
;
103 static void free_session(struct nfsd4_session
*);
105 static struct nfsd4_callback_ops nfsd4_cb_recall_ops
;
107 static bool is_session_dead(struct nfsd4_session
*ses
)
109 return ses
->se_flags
& NFS4_SESSION_DEAD
;
112 static __be32
mark_session_dead_locked(struct nfsd4_session
*ses
, int ref_held_by_me
)
114 if (atomic_read(&ses
->se_ref
) > ref_held_by_me
)
115 return nfserr_jukebox
;
116 ses
->se_flags
|= NFS4_SESSION_DEAD
;
120 static bool is_client_expired(struct nfs4_client
*clp
)
122 return clp
->cl_time
== 0;
125 static __be32
get_client_locked(struct nfs4_client
*clp
)
127 struct nfsd_net
*nn
= net_generic(clp
->net
, nfsd_net_id
);
129 lockdep_assert_held(&nn
->client_lock
);
131 if (is_client_expired(clp
))
132 return nfserr_expired
;
133 atomic_inc(&clp
->cl_refcount
);
137 /* must be called under the client_lock */
139 renew_client_locked(struct nfs4_client
*clp
)
141 struct nfsd_net
*nn
= net_generic(clp
->net
, nfsd_net_id
);
143 if (is_client_expired(clp
)) {
145 printk("%s: client (clientid %08x/%08x) already expired\n",
147 clp
->cl_clientid
.cl_boot
,
148 clp
->cl_clientid
.cl_id
);
152 dprintk("renewing client (clientid %08x/%08x)\n",
153 clp
->cl_clientid
.cl_boot
,
154 clp
->cl_clientid
.cl_id
);
155 list_move_tail(&clp
->cl_lru
, &nn
->client_lru
);
156 clp
->cl_time
= get_seconds();
159 static void put_client_renew_locked(struct nfs4_client
*clp
)
161 struct nfsd_net
*nn
= net_generic(clp
->net
, nfsd_net_id
);
163 lockdep_assert_held(&nn
->client_lock
);
165 if (!atomic_dec_and_test(&clp
->cl_refcount
))
167 if (!is_client_expired(clp
))
168 renew_client_locked(clp
);
171 static void put_client_renew(struct nfs4_client
*clp
)
173 struct nfsd_net
*nn
= net_generic(clp
->net
, nfsd_net_id
);
175 if (!atomic_dec_and_lock(&clp
->cl_refcount
, &nn
->client_lock
))
177 if (!is_client_expired(clp
))
178 renew_client_locked(clp
);
179 spin_unlock(&nn
->client_lock
);
182 static __be32
nfsd4_get_session_locked(struct nfsd4_session
*ses
)
186 if (is_session_dead(ses
))
187 return nfserr_badsession
;
188 status
= get_client_locked(ses
->se_client
);
191 atomic_inc(&ses
->se_ref
);
195 static void nfsd4_put_session_locked(struct nfsd4_session
*ses
)
197 struct nfs4_client
*clp
= ses
->se_client
;
198 struct nfsd_net
*nn
= net_generic(clp
->net
, nfsd_net_id
);
200 lockdep_assert_held(&nn
->client_lock
);
202 if (atomic_dec_and_test(&ses
->se_ref
) && is_session_dead(ses
))
204 put_client_renew_locked(clp
);
207 static void nfsd4_put_session(struct nfsd4_session
*ses
)
209 struct nfs4_client
*clp
= ses
->se_client
;
210 struct nfsd_net
*nn
= net_generic(clp
->net
, nfsd_net_id
);
212 spin_lock(&nn
->client_lock
);
213 nfsd4_put_session_locked(ses
);
214 spin_unlock(&nn
->client_lock
);
217 static inline struct nfs4_stateowner
*
218 nfs4_get_stateowner(struct nfs4_stateowner
*sop
)
220 atomic_inc(&sop
->so_count
);
225 same_owner_str(struct nfs4_stateowner
*sop
, struct xdr_netobj
*owner
)
227 return (sop
->so_owner
.len
== owner
->len
) &&
228 0 == memcmp(sop
->so_owner
.data
, owner
->data
, owner
->len
);
231 static struct nfs4_openowner
*
232 find_openstateowner_str_locked(unsigned int hashval
, struct nfsd4_open
*open
,
233 struct nfs4_client
*clp
)
235 struct nfs4_stateowner
*so
;
237 lockdep_assert_held(&clp
->cl_lock
);
239 list_for_each_entry(so
, &clp
->cl_ownerstr_hashtbl
[hashval
],
241 if (!so
->so_is_open_owner
)
243 if (same_owner_str(so
, &open
->op_owner
))
244 return openowner(nfs4_get_stateowner(so
));
249 static struct nfs4_openowner
*
250 find_openstateowner_str(unsigned int hashval
, struct nfsd4_open
*open
,
251 struct nfs4_client
*clp
)
253 struct nfs4_openowner
*oo
;
255 spin_lock(&clp
->cl_lock
);
256 oo
= find_openstateowner_str_locked(hashval
, open
, clp
);
257 spin_unlock(&clp
->cl_lock
);
262 opaque_hashval(const void *ptr
, int nbytes
)
264 unsigned char *cptr
= (unsigned char *) ptr
;
274 static void nfsd4_free_file_rcu(struct rcu_head
*rcu
)
276 struct nfs4_file
*fp
= container_of(rcu
, struct nfs4_file
, fi_rcu
);
278 kmem_cache_free(file_slab
, fp
);
282 put_nfs4_file(struct nfs4_file
*fi
)
284 might_lock(&state_lock
);
286 if (atomic_dec_and_lock(&fi
->fi_ref
, &state_lock
)) {
287 hlist_del_rcu(&fi
->fi_hash
);
288 spin_unlock(&state_lock
);
289 WARN_ON_ONCE(!list_empty(&fi
->fi_clnt_odstate
));
290 WARN_ON_ONCE(!list_empty(&fi
->fi_delegations
));
291 call_rcu(&fi
->fi_rcu
, nfsd4_free_file_rcu
);
296 __nfs4_get_fd(struct nfs4_file
*f
, int oflag
)
298 if (f
->fi_fds
[oflag
])
299 return get_file(f
->fi_fds
[oflag
]);
304 find_writeable_file_locked(struct nfs4_file
*f
)
308 lockdep_assert_held(&f
->fi_lock
);
310 ret
= __nfs4_get_fd(f
, O_WRONLY
);
312 ret
= __nfs4_get_fd(f
, O_RDWR
);
317 find_writeable_file(struct nfs4_file
*f
)
321 spin_lock(&f
->fi_lock
);
322 ret
= find_writeable_file_locked(f
);
323 spin_unlock(&f
->fi_lock
);
328 static struct file
*find_readable_file_locked(struct nfs4_file
*f
)
332 lockdep_assert_held(&f
->fi_lock
);
334 ret
= __nfs4_get_fd(f
, O_RDONLY
);
336 ret
= __nfs4_get_fd(f
, O_RDWR
);
341 find_readable_file(struct nfs4_file
*f
)
345 spin_lock(&f
->fi_lock
);
346 ret
= find_readable_file_locked(f
);
347 spin_unlock(&f
->fi_lock
);
353 find_any_file(struct nfs4_file
*f
)
357 spin_lock(&f
->fi_lock
);
358 ret
= __nfs4_get_fd(f
, O_RDWR
);
360 ret
= __nfs4_get_fd(f
, O_WRONLY
);
362 ret
= __nfs4_get_fd(f
, O_RDONLY
);
364 spin_unlock(&f
->fi_lock
);
368 static atomic_long_t num_delegations
;
369 unsigned long max_delegations
;
372 * Open owner state (share locks)
375 /* hash tables for lock and open owners */
376 #define OWNER_HASH_BITS 8
377 #define OWNER_HASH_SIZE (1 << OWNER_HASH_BITS)
378 #define OWNER_HASH_MASK (OWNER_HASH_SIZE - 1)
380 static unsigned int ownerstr_hashval(struct xdr_netobj
*ownername
)
384 ret
= opaque_hashval(ownername
->data
, ownername
->len
);
385 return ret
& OWNER_HASH_MASK
;
388 /* hash table for nfs4_file */
389 #define FILE_HASH_BITS 8
390 #define FILE_HASH_SIZE (1 << FILE_HASH_BITS)
392 static unsigned int nfsd_fh_hashval(struct knfsd_fh
*fh
)
394 return jhash2(fh
->fh_base
.fh_pad
, XDR_QUADLEN(fh
->fh_size
), 0);
397 static unsigned int file_hashval(struct knfsd_fh
*fh
)
399 return nfsd_fh_hashval(fh
) & (FILE_HASH_SIZE
- 1);
402 static struct hlist_head file_hashtbl
[FILE_HASH_SIZE
];
405 __nfs4_file_get_access(struct nfs4_file
*fp
, u32 access
)
407 lockdep_assert_held(&fp
->fi_lock
);
409 if (access
& NFS4_SHARE_ACCESS_WRITE
)
410 atomic_inc(&fp
->fi_access
[O_WRONLY
]);
411 if (access
& NFS4_SHARE_ACCESS_READ
)
412 atomic_inc(&fp
->fi_access
[O_RDONLY
]);
416 nfs4_file_get_access(struct nfs4_file
*fp
, u32 access
)
418 lockdep_assert_held(&fp
->fi_lock
);
420 /* Does this access mode make sense? */
421 if (access
& ~NFS4_SHARE_ACCESS_BOTH
)
424 /* Does it conflict with a deny mode already set? */
425 if ((access
& fp
->fi_share_deny
) != 0)
426 return nfserr_share_denied
;
428 __nfs4_file_get_access(fp
, access
);
432 static __be32
nfs4_file_check_deny(struct nfs4_file
*fp
, u32 deny
)
434 /* Common case is that there is no deny mode. */
436 /* Does this deny mode make sense? */
437 if (deny
& ~NFS4_SHARE_DENY_BOTH
)
440 if ((deny
& NFS4_SHARE_DENY_READ
) &&
441 atomic_read(&fp
->fi_access
[O_RDONLY
]))
442 return nfserr_share_denied
;
444 if ((deny
& NFS4_SHARE_DENY_WRITE
) &&
445 atomic_read(&fp
->fi_access
[O_WRONLY
]))
446 return nfserr_share_denied
;
451 static void __nfs4_file_put_access(struct nfs4_file
*fp
, int oflag
)
453 might_lock(&fp
->fi_lock
);
455 if (atomic_dec_and_lock(&fp
->fi_access
[oflag
], &fp
->fi_lock
)) {
456 struct file
*f1
= NULL
;
457 struct file
*f2
= NULL
;
459 swap(f1
, fp
->fi_fds
[oflag
]);
460 if (atomic_read(&fp
->fi_access
[1 - oflag
]) == 0)
461 swap(f2
, fp
->fi_fds
[O_RDWR
]);
462 spin_unlock(&fp
->fi_lock
);
470 static void nfs4_file_put_access(struct nfs4_file
*fp
, u32 access
)
472 WARN_ON_ONCE(access
& ~NFS4_SHARE_ACCESS_BOTH
);
474 if (access
& NFS4_SHARE_ACCESS_WRITE
)
475 __nfs4_file_put_access(fp
, O_WRONLY
);
476 if (access
& NFS4_SHARE_ACCESS_READ
)
477 __nfs4_file_put_access(fp
, O_RDONLY
);
481 * Allocate a new open/delegation state counter. This is needed for
482 * pNFS for proper return on close semantics.
484 * Note that we only allocate it for pNFS-enabled exports, otherwise
485 * all pointers to struct nfs4_clnt_odstate are always NULL.
487 static struct nfs4_clnt_odstate
*
488 alloc_clnt_odstate(struct nfs4_client
*clp
)
490 struct nfs4_clnt_odstate
*co
;
492 co
= kmem_cache_zalloc(odstate_slab
, GFP_KERNEL
);
495 atomic_set(&co
->co_odcount
, 1);
501 hash_clnt_odstate_locked(struct nfs4_clnt_odstate
*co
)
503 struct nfs4_file
*fp
= co
->co_file
;
505 lockdep_assert_held(&fp
->fi_lock
);
506 list_add(&co
->co_perfile
, &fp
->fi_clnt_odstate
);
510 get_clnt_odstate(struct nfs4_clnt_odstate
*co
)
513 atomic_inc(&co
->co_odcount
);
517 put_clnt_odstate(struct nfs4_clnt_odstate
*co
)
519 struct nfs4_file
*fp
;
525 if (atomic_dec_and_lock(&co
->co_odcount
, &fp
->fi_lock
)) {
526 list_del(&co
->co_perfile
);
527 spin_unlock(&fp
->fi_lock
);
529 nfsd4_return_all_file_layouts(co
->co_client
, fp
);
530 kmem_cache_free(odstate_slab
, co
);
534 static struct nfs4_clnt_odstate
*
535 find_or_hash_clnt_odstate(struct nfs4_file
*fp
, struct nfs4_clnt_odstate
*new)
537 struct nfs4_clnt_odstate
*co
;
538 struct nfs4_client
*cl
;
545 spin_lock(&fp
->fi_lock
);
546 list_for_each_entry(co
, &fp
->fi_clnt_odstate
, co_perfile
) {
547 if (co
->co_client
== cl
) {
548 get_clnt_odstate(co
);
554 hash_clnt_odstate_locked(new);
556 spin_unlock(&fp
->fi_lock
);
560 struct nfs4_stid
*nfs4_alloc_stid(struct nfs4_client
*cl
, struct kmem_cache
*slab
,
561 void (*sc_free
)(struct nfs4_stid
*))
563 struct nfs4_stid
*stid
;
566 stid
= kmem_cache_zalloc(slab
, GFP_KERNEL
);
570 idr_preload(GFP_KERNEL
);
571 spin_lock(&cl
->cl_lock
);
572 new_id
= idr_alloc_cyclic(&cl
->cl_stateids
, stid
, 0, 0, GFP_NOWAIT
);
573 spin_unlock(&cl
->cl_lock
);
578 stid
->sc_free
= sc_free
;
579 stid
->sc_client
= cl
;
580 stid
->sc_stateid
.si_opaque
.so_id
= new_id
;
581 stid
->sc_stateid
.si_opaque
.so_clid
= cl
->cl_clientid
;
582 /* Will be incremented before return to client: */
583 atomic_set(&stid
->sc_count
, 1);
584 spin_lock_init(&stid
->sc_lock
);
587 * It shouldn't be a problem to reuse an opaque stateid value.
588 * I don't think it is for 4.1. But with 4.0 I worry that, for
589 * example, a stray write retransmission could be accepted by
590 * the server when it should have been rejected. Therefore,
591 * adopt a trick from the sctp code to attempt to maximize the
592 * amount of time until an id is reused, by ensuring they always
593 * "increase" (mod INT_MAX):
597 kmem_cache_free(slab
, stid
);
601 static struct nfs4_ol_stateid
* nfs4_alloc_open_stateid(struct nfs4_client
*clp
)
603 struct nfs4_stid
*stid
;
605 stid
= nfs4_alloc_stid(clp
, stateid_slab
, nfs4_free_ol_stateid
);
609 return openlockstateid(stid
);
612 static void nfs4_free_deleg(struct nfs4_stid
*stid
)
614 kmem_cache_free(deleg_slab
, stid
);
615 atomic_long_dec(&num_delegations
);
619 * When we recall a delegation, we should be careful not to hand it
620 * out again straight away.
621 * To ensure this we keep a pair of bloom filters ('new' and 'old')
622 * in which the filehandles of recalled delegations are "stored".
623 * If a filehandle appear in either filter, a delegation is blocked.
624 * When a delegation is recalled, the filehandle is stored in the "new"
626 * Every 30 seconds we swap the filters and clear the "new" one,
627 * unless both are empty of course.
629 * Each filter is 256 bits. We hash the filehandle to 32bit and use the
630 * low 3 bytes as hash-table indices.
632 * 'blocked_delegations_lock', which is always taken in block_delegations(),
633 * is used to manage concurrent access. Testing does not need the lock
634 * except when swapping the two filters.
636 static DEFINE_SPINLOCK(blocked_delegations_lock
);
637 static struct bloom_pair
{
638 int entries
, old_entries
;
640 int new; /* index into 'set' */
641 DECLARE_BITMAP(set
[2], 256);
642 } blocked_delegations
;
644 static int delegation_blocked(struct knfsd_fh
*fh
)
647 struct bloom_pair
*bd
= &blocked_delegations
;
649 if (bd
->entries
== 0)
651 if (seconds_since_boot() - bd
->swap_time
> 30) {
652 spin_lock(&blocked_delegations_lock
);
653 if (seconds_since_boot() - bd
->swap_time
> 30) {
654 bd
->entries
-= bd
->old_entries
;
655 bd
->old_entries
= bd
->entries
;
656 memset(bd
->set
[bd
->new], 0,
659 bd
->swap_time
= seconds_since_boot();
661 spin_unlock(&blocked_delegations_lock
);
663 hash
= jhash(&fh
->fh_base
, fh
->fh_size
, 0);
664 if (test_bit(hash
&255, bd
->set
[0]) &&
665 test_bit((hash
>>8)&255, bd
->set
[0]) &&
666 test_bit((hash
>>16)&255, bd
->set
[0]))
669 if (test_bit(hash
&255, bd
->set
[1]) &&
670 test_bit((hash
>>8)&255, bd
->set
[1]) &&
671 test_bit((hash
>>16)&255, bd
->set
[1]))
677 static void block_delegations(struct knfsd_fh
*fh
)
680 struct bloom_pair
*bd
= &blocked_delegations
;
682 hash
= jhash(&fh
->fh_base
, fh
->fh_size
, 0);
684 spin_lock(&blocked_delegations_lock
);
685 __set_bit(hash
&255, bd
->set
[bd
->new]);
686 __set_bit((hash
>>8)&255, bd
->set
[bd
->new]);
687 __set_bit((hash
>>16)&255, bd
->set
[bd
->new]);
688 if (bd
->entries
== 0)
689 bd
->swap_time
= seconds_since_boot();
691 spin_unlock(&blocked_delegations_lock
);
694 static struct nfs4_delegation
*
695 alloc_init_deleg(struct nfs4_client
*clp
, struct svc_fh
*current_fh
,
696 struct nfs4_clnt_odstate
*odstate
)
698 struct nfs4_delegation
*dp
;
701 dprintk("NFSD alloc_init_deleg\n");
702 n
= atomic_long_inc_return(&num_delegations
);
703 if (n
< 0 || n
> max_delegations
)
705 if (delegation_blocked(¤t_fh
->fh_handle
))
707 dp
= delegstateid(nfs4_alloc_stid(clp
, deleg_slab
, nfs4_free_deleg
));
712 * delegation seqid's are never incremented. The 4.1 special
713 * meaning of seqid 0 isn't meaningful, really, but let's avoid
714 * 0 anyway just for consistency and use 1:
716 dp
->dl_stid
.sc_stateid
.si_generation
= 1;
717 INIT_LIST_HEAD(&dp
->dl_perfile
);
718 INIT_LIST_HEAD(&dp
->dl_perclnt
);
719 INIT_LIST_HEAD(&dp
->dl_recall_lru
);
720 dp
->dl_clnt_odstate
= odstate
;
721 get_clnt_odstate(odstate
);
722 dp
->dl_type
= NFS4_OPEN_DELEGATE_READ
;
724 nfsd4_init_cb(&dp
->dl_recall
, dp
->dl_stid
.sc_client
,
725 &nfsd4_cb_recall_ops
, NFSPROC4_CLNT_CB_RECALL
);
728 atomic_long_dec(&num_delegations
);
733 nfs4_put_stid(struct nfs4_stid
*s
)
735 struct nfs4_file
*fp
= s
->sc_file
;
736 struct nfs4_client
*clp
= s
->sc_client
;
738 might_lock(&clp
->cl_lock
);
740 if (!atomic_dec_and_lock(&s
->sc_count
, &clp
->cl_lock
)) {
741 wake_up_all(&close_wq
);
744 idr_remove(&clp
->cl_stateids
, s
->sc_stateid
.si_opaque
.so_id
);
745 spin_unlock(&clp
->cl_lock
);
752 nfs4_inc_and_copy_stateid(stateid_t
*dst
, struct nfs4_stid
*stid
)
754 stateid_t
*src
= &stid
->sc_stateid
;
756 spin_lock(&stid
->sc_lock
);
757 if (unlikely(++src
->si_generation
== 0))
758 src
->si_generation
= 1;
759 memcpy(dst
, src
, sizeof(*dst
));
760 spin_unlock(&stid
->sc_lock
);
763 static void nfs4_put_deleg_lease(struct nfs4_file
*fp
)
765 struct file
*filp
= NULL
;
767 spin_lock(&fp
->fi_lock
);
768 if (fp
->fi_deleg_file
&& --fp
->fi_delegees
== 0)
769 swap(filp
, fp
->fi_deleg_file
);
770 spin_unlock(&fp
->fi_lock
);
773 vfs_setlease(filp
, F_UNLCK
, NULL
, (void **)&fp
);
778 void nfs4_unhash_stid(struct nfs4_stid
*s
)
784 * nfs4_get_existing_delegation - Discover if this delegation already exists
785 * @clp: a pointer to the nfs4_client we're granting a delegation to
786 * @fp: a pointer to the nfs4_file we're granting a delegation on
789 * On success: NULL if an existing delegation was not found.
791 * On error: -EAGAIN if one was previously granted to this nfs4_client
792 * for this nfs4_file.
797 nfs4_get_existing_delegation(struct nfs4_client
*clp
, struct nfs4_file
*fp
)
799 struct nfs4_delegation
*searchdp
= NULL
;
800 struct nfs4_client
*searchclp
= NULL
;
802 lockdep_assert_held(&state_lock
);
803 lockdep_assert_held(&fp
->fi_lock
);
805 list_for_each_entry(searchdp
, &fp
->fi_delegations
, dl_perfile
) {
806 searchclp
= searchdp
->dl_stid
.sc_client
;
807 if (clp
== searchclp
) {
815 * hash_delegation_locked - Add a delegation to the appropriate lists
816 * @dp: a pointer to the nfs4_delegation we are adding.
817 * @fp: a pointer to the nfs4_file we're granting a delegation on
820 * On success: NULL if the delegation was successfully hashed.
822 * On error: -EAGAIN if one was previously granted to this
823 * nfs4_client for this nfs4_file. Delegation is not hashed.
828 hash_delegation_locked(struct nfs4_delegation
*dp
, struct nfs4_file
*fp
)
831 struct nfs4_client
*clp
= dp
->dl_stid
.sc_client
;
833 lockdep_assert_held(&state_lock
);
834 lockdep_assert_held(&fp
->fi_lock
);
836 status
= nfs4_get_existing_delegation(clp
, fp
);
840 atomic_inc(&dp
->dl_stid
.sc_count
);
841 dp
->dl_stid
.sc_type
= NFS4_DELEG_STID
;
842 list_add(&dp
->dl_perfile
, &fp
->fi_delegations
);
843 list_add(&dp
->dl_perclnt
, &clp
->cl_delegations
);
848 unhash_delegation_locked(struct nfs4_delegation
*dp
)
850 struct nfs4_file
*fp
= dp
->dl_stid
.sc_file
;
852 lockdep_assert_held(&state_lock
);
854 if (list_empty(&dp
->dl_perfile
))
857 dp
->dl_stid
.sc_type
= NFS4_CLOSED_DELEG_STID
;
858 /* Ensure that deleg break won't try to requeue it */
860 spin_lock(&fp
->fi_lock
);
861 list_del_init(&dp
->dl_perclnt
);
862 list_del_init(&dp
->dl_recall_lru
);
863 list_del_init(&dp
->dl_perfile
);
864 spin_unlock(&fp
->fi_lock
);
868 static void destroy_delegation(struct nfs4_delegation
*dp
)
872 spin_lock(&state_lock
);
873 unhashed
= unhash_delegation_locked(dp
);
874 spin_unlock(&state_lock
);
876 put_clnt_odstate(dp
->dl_clnt_odstate
);
877 nfs4_put_deleg_lease(dp
->dl_stid
.sc_file
);
878 nfs4_put_stid(&dp
->dl_stid
);
882 static void revoke_delegation(struct nfs4_delegation
*dp
)
884 struct nfs4_client
*clp
= dp
->dl_stid
.sc_client
;
886 WARN_ON(!list_empty(&dp
->dl_recall_lru
));
888 put_clnt_odstate(dp
->dl_clnt_odstate
);
889 nfs4_put_deleg_lease(dp
->dl_stid
.sc_file
);
891 if (clp
->cl_minorversion
== 0)
892 nfs4_put_stid(&dp
->dl_stid
);
894 dp
->dl_stid
.sc_type
= NFS4_REVOKED_DELEG_STID
;
895 spin_lock(&clp
->cl_lock
);
896 list_add(&dp
->dl_recall_lru
, &clp
->cl_revoked
);
897 spin_unlock(&clp
->cl_lock
);
905 static unsigned int clientid_hashval(u32 id
)
907 return id
& CLIENT_HASH_MASK
;
910 static unsigned int clientstr_hashval(const char *name
)
912 return opaque_hashval(name
, 8) & CLIENT_HASH_MASK
;
916 * We store the NONE, READ, WRITE, and BOTH bits separately in the
917 * st_{access,deny}_bmap field of the stateid, in order to track not
918 * only what share bits are currently in force, but also what
919 * combinations of share bits previous opens have used. This allows us
920 * to enforce the recommendation of rfc 3530 14.2.19 that the server
921 * return an error if the client attempt to downgrade to a combination
922 * of share bits not explicable by closing some of its previous opens.
924 * XXX: This enforcement is actually incomplete, since we don't keep
925 * track of access/deny bit combinations; so, e.g., we allow:
927 * OPEN allow read, deny write
928 * OPEN allow both, deny none
929 * DOWNGRADE allow read, deny none
931 * which we should reject.
934 bmap_to_share_mode(unsigned long bmap
) {
936 unsigned int access
= 0;
938 for (i
= 1; i
< 4; i
++) {
939 if (test_bit(i
, &bmap
))
945 /* set share access for a given stateid */
947 set_access(u32 access
, struct nfs4_ol_stateid
*stp
)
949 unsigned char mask
= 1 << access
;
951 WARN_ON_ONCE(access
> NFS4_SHARE_ACCESS_BOTH
);
952 stp
->st_access_bmap
|= mask
;
955 /* clear share access for a given stateid */
957 clear_access(u32 access
, struct nfs4_ol_stateid
*stp
)
959 unsigned char mask
= 1 << access
;
961 WARN_ON_ONCE(access
> NFS4_SHARE_ACCESS_BOTH
);
962 stp
->st_access_bmap
&= ~mask
;
965 /* test whether a given stateid has access */
967 test_access(u32 access
, struct nfs4_ol_stateid
*stp
)
969 unsigned char mask
= 1 << access
;
971 return (bool)(stp
->st_access_bmap
& mask
);
974 /* set share deny for a given stateid */
976 set_deny(u32 deny
, struct nfs4_ol_stateid
*stp
)
978 unsigned char mask
= 1 << deny
;
980 WARN_ON_ONCE(deny
> NFS4_SHARE_DENY_BOTH
);
981 stp
->st_deny_bmap
|= mask
;
984 /* clear share deny for a given stateid */
986 clear_deny(u32 deny
, struct nfs4_ol_stateid
*stp
)
988 unsigned char mask
= 1 << deny
;
990 WARN_ON_ONCE(deny
> NFS4_SHARE_DENY_BOTH
);
991 stp
->st_deny_bmap
&= ~mask
;
994 /* test whether a given stateid is denying specific access */
996 test_deny(u32 deny
, struct nfs4_ol_stateid
*stp
)
998 unsigned char mask
= 1 << deny
;
1000 return (bool)(stp
->st_deny_bmap
& mask
);
1003 static int nfs4_access_to_omode(u32 access
)
1005 switch (access
& NFS4_SHARE_ACCESS_BOTH
) {
1006 case NFS4_SHARE_ACCESS_READ
:
1008 case NFS4_SHARE_ACCESS_WRITE
:
1010 case NFS4_SHARE_ACCESS_BOTH
:
1018 * A stateid that had a deny mode associated with it is being released
1019 * or downgraded. Recalculate the deny mode on the file.
1022 recalculate_deny_mode(struct nfs4_file
*fp
)
1024 struct nfs4_ol_stateid
*stp
;
1026 spin_lock(&fp
->fi_lock
);
1027 fp
->fi_share_deny
= 0;
1028 list_for_each_entry(stp
, &fp
->fi_stateids
, st_perfile
)
1029 fp
->fi_share_deny
|= bmap_to_share_mode(stp
->st_deny_bmap
);
1030 spin_unlock(&fp
->fi_lock
);
1034 reset_union_bmap_deny(u32 deny
, struct nfs4_ol_stateid
*stp
)
1037 bool change
= false;
1039 for (i
= 1; i
< 4; i
++) {
1040 if ((i
& deny
) != i
) {
1046 /* Recalculate per-file deny mode if there was a change */
1048 recalculate_deny_mode(stp
->st_stid
.sc_file
);
1051 /* release all access and file references for a given stateid */
1053 release_all_access(struct nfs4_ol_stateid
*stp
)
1056 struct nfs4_file
*fp
= stp
->st_stid
.sc_file
;
1058 if (fp
&& stp
->st_deny_bmap
!= 0)
1059 recalculate_deny_mode(fp
);
1061 for (i
= 1; i
< 4; i
++) {
1062 if (test_access(i
, stp
))
1063 nfs4_file_put_access(stp
->st_stid
.sc_file
, i
);
1064 clear_access(i
, stp
);
1068 static inline void nfs4_free_stateowner(struct nfs4_stateowner
*sop
)
1070 kfree(sop
->so_owner
.data
);
1071 sop
->so_ops
->so_free(sop
);
1074 static void nfs4_put_stateowner(struct nfs4_stateowner
*sop
)
1076 struct nfs4_client
*clp
= sop
->so_client
;
1078 might_lock(&clp
->cl_lock
);
1080 if (!atomic_dec_and_lock(&sop
->so_count
, &clp
->cl_lock
))
1082 sop
->so_ops
->so_unhash(sop
);
1083 spin_unlock(&clp
->cl_lock
);
1084 nfs4_free_stateowner(sop
);
1087 static bool unhash_ol_stateid(struct nfs4_ol_stateid
*stp
)
1089 struct nfs4_file
*fp
= stp
->st_stid
.sc_file
;
1091 lockdep_assert_held(&stp
->st_stateowner
->so_client
->cl_lock
);
1093 if (list_empty(&stp
->st_perfile
))
1096 spin_lock(&fp
->fi_lock
);
1097 list_del_init(&stp
->st_perfile
);
1098 spin_unlock(&fp
->fi_lock
);
1099 list_del(&stp
->st_perstateowner
);
1103 static void nfs4_free_ol_stateid(struct nfs4_stid
*stid
)
1105 struct nfs4_ol_stateid
*stp
= openlockstateid(stid
);
1107 put_clnt_odstate(stp
->st_clnt_odstate
);
1108 release_all_access(stp
);
1109 if (stp
->st_stateowner
)
1110 nfs4_put_stateowner(stp
->st_stateowner
);
1111 kmem_cache_free(stateid_slab
, stid
);
1114 static void nfs4_free_lock_stateid(struct nfs4_stid
*stid
)
1116 struct nfs4_ol_stateid
*stp
= openlockstateid(stid
);
1117 struct nfs4_lockowner
*lo
= lockowner(stp
->st_stateowner
);
1120 file
= find_any_file(stp
->st_stid
.sc_file
);
1122 filp_close(file
, (fl_owner_t
)lo
);
1123 nfs4_free_ol_stateid(stid
);
1127 * Put the persistent reference to an already unhashed generic stateid, while
1128 * holding the cl_lock. If it's the last reference, then put it onto the
1129 * reaplist for later destruction.
1131 static void put_ol_stateid_locked(struct nfs4_ol_stateid
*stp
,
1132 struct list_head
*reaplist
)
1134 struct nfs4_stid
*s
= &stp
->st_stid
;
1135 struct nfs4_client
*clp
= s
->sc_client
;
1137 lockdep_assert_held(&clp
->cl_lock
);
1139 WARN_ON_ONCE(!list_empty(&stp
->st_locks
));
1141 if (!atomic_dec_and_test(&s
->sc_count
)) {
1142 wake_up_all(&close_wq
);
1146 idr_remove(&clp
->cl_stateids
, s
->sc_stateid
.si_opaque
.so_id
);
1147 list_add(&stp
->st_locks
, reaplist
);
1150 static bool unhash_lock_stateid(struct nfs4_ol_stateid
*stp
)
1152 lockdep_assert_held(&stp
->st_stid
.sc_client
->cl_lock
);
1154 list_del_init(&stp
->st_locks
);
1155 nfs4_unhash_stid(&stp
->st_stid
);
1156 return unhash_ol_stateid(stp
);
1159 static void release_lock_stateid(struct nfs4_ol_stateid
*stp
)
1161 struct nfs4_client
*clp
= stp
->st_stid
.sc_client
;
1164 spin_lock(&clp
->cl_lock
);
1165 unhashed
= unhash_lock_stateid(stp
);
1166 spin_unlock(&clp
->cl_lock
);
1168 nfs4_put_stid(&stp
->st_stid
);
1171 static void unhash_lockowner_locked(struct nfs4_lockowner
*lo
)
1173 struct nfs4_client
*clp
= lo
->lo_owner
.so_client
;
1175 lockdep_assert_held(&clp
->cl_lock
);
1177 list_del_init(&lo
->lo_owner
.so_strhash
);
1181 * Free a list of generic stateids that were collected earlier after being
1185 free_ol_stateid_reaplist(struct list_head
*reaplist
)
1187 struct nfs4_ol_stateid
*stp
;
1188 struct nfs4_file
*fp
;
1192 while (!list_empty(reaplist
)) {
1193 stp
= list_first_entry(reaplist
, struct nfs4_ol_stateid
,
1195 list_del(&stp
->st_locks
);
1196 fp
= stp
->st_stid
.sc_file
;
1197 stp
->st_stid
.sc_free(&stp
->st_stid
);
1203 static void release_open_stateid_locks(struct nfs4_ol_stateid
*open_stp
,
1204 struct list_head
*reaplist
)
1206 struct nfs4_ol_stateid
*stp
;
1208 lockdep_assert_held(&open_stp
->st_stid
.sc_client
->cl_lock
);
1210 while (!list_empty(&open_stp
->st_locks
)) {
1211 stp
= list_entry(open_stp
->st_locks
.next
,
1212 struct nfs4_ol_stateid
, st_locks
);
1213 WARN_ON(!unhash_lock_stateid(stp
));
1214 put_ol_stateid_locked(stp
, reaplist
);
1218 static bool unhash_open_stateid(struct nfs4_ol_stateid
*stp
,
1219 struct list_head
*reaplist
)
1223 lockdep_assert_held(&stp
->st_stid
.sc_client
->cl_lock
);
1225 unhashed
= unhash_ol_stateid(stp
);
1226 release_open_stateid_locks(stp
, reaplist
);
1230 static void release_open_stateid(struct nfs4_ol_stateid
*stp
)
1232 LIST_HEAD(reaplist
);
1234 spin_lock(&stp
->st_stid
.sc_client
->cl_lock
);
1235 if (unhash_open_stateid(stp
, &reaplist
))
1236 put_ol_stateid_locked(stp
, &reaplist
);
1237 spin_unlock(&stp
->st_stid
.sc_client
->cl_lock
);
1238 free_ol_stateid_reaplist(&reaplist
);
1241 static void unhash_openowner_locked(struct nfs4_openowner
*oo
)
1243 struct nfs4_client
*clp
= oo
->oo_owner
.so_client
;
1245 lockdep_assert_held(&clp
->cl_lock
);
1247 list_del_init(&oo
->oo_owner
.so_strhash
);
1248 list_del_init(&oo
->oo_perclient
);
1251 static void release_last_closed_stateid(struct nfs4_openowner
*oo
)
1253 struct nfsd_net
*nn
= net_generic(oo
->oo_owner
.so_client
->net
,
1255 struct nfs4_ol_stateid
*s
;
1257 spin_lock(&nn
->client_lock
);
1258 s
= oo
->oo_last_closed_stid
;
1260 list_del_init(&oo
->oo_close_lru
);
1261 oo
->oo_last_closed_stid
= NULL
;
1263 spin_unlock(&nn
->client_lock
);
1265 nfs4_put_stid(&s
->st_stid
);
1268 static void release_openowner(struct nfs4_openowner
*oo
)
1270 struct nfs4_ol_stateid
*stp
;
1271 struct nfs4_client
*clp
= oo
->oo_owner
.so_client
;
1272 struct list_head reaplist
;
1274 INIT_LIST_HEAD(&reaplist
);
1276 spin_lock(&clp
->cl_lock
);
1277 unhash_openowner_locked(oo
);
1278 while (!list_empty(&oo
->oo_owner
.so_stateids
)) {
1279 stp
= list_first_entry(&oo
->oo_owner
.so_stateids
,
1280 struct nfs4_ol_stateid
, st_perstateowner
);
1281 if (unhash_open_stateid(stp
, &reaplist
))
1282 put_ol_stateid_locked(stp
, &reaplist
);
1284 spin_unlock(&clp
->cl_lock
);
1285 free_ol_stateid_reaplist(&reaplist
);
1286 release_last_closed_stateid(oo
);
1287 nfs4_put_stateowner(&oo
->oo_owner
);
1291 hash_sessionid(struct nfs4_sessionid
*sessionid
)
1293 struct nfsd4_sessionid
*sid
= (struct nfsd4_sessionid
*)sessionid
;
1295 return sid
->sequence
% SESSION_HASH_SIZE
;
1298 #ifdef CONFIG_SUNRPC_DEBUG
1300 dump_sessionid(const char *fn
, struct nfs4_sessionid
*sessionid
)
1302 u32
*ptr
= (u32
*)(&sessionid
->data
[0]);
1303 dprintk("%s: %u:%u:%u:%u\n", fn
, ptr
[0], ptr
[1], ptr
[2], ptr
[3]);
1307 dump_sessionid(const char *fn
, struct nfs4_sessionid
*sessionid
)
1313 * Bump the seqid on cstate->replay_owner, and clear replay_owner if it
1314 * won't be used for replay.
1316 void nfsd4_bump_seqid(struct nfsd4_compound_state
*cstate
, __be32 nfserr
)
1318 struct nfs4_stateowner
*so
= cstate
->replay_owner
;
1320 if (nfserr
== nfserr_replay_me
)
1323 if (!seqid_mutating_err(ntohl(nfserr
))) {
1324 nfsd4_cstate_clear_replay(cstate
);
1329 if (so
->so_is_open_owner
)
1330 release_last_closed_stateid(openowner(so
));
1336 gen_sessionid(struct nfsd4_session
*ses
)
1338 struct nfs4_client
*clp
= ses
->se_client
;
1339 struct nfsd4_sessionid
*sid
;
1341 sid
= (struct nfsd4_sessionid
*)ses
->se_sessionid
.data
;
1342 sid
->clientid
= clp
->cl_clientid
;
1343 sid
->sequence
= current_sessionid
++;
1348 * The protocol defines ca_maxresponssize_cached to include the size of
1349 * the rpc header, but all we need to cache is the data starting after
1350 * the end of the initial SEQUENCE operation--the rest we regenerate
1351 * each time. Therefore we can advertise a ca_maxresponssize_cached
1352 * value that is the number of bytes in our cache plus a few additional
1353 * bytes. In order to stay on the safe side, and not promise more than
1354 * we can cache, those additional bytes must be the minimum possible: 24
1355 * bytes of rpc header (xid through accept state, with AUTH_NULL
1356 * verifier), 12 for the compound header (with zero-length tag), and 44
1357 * for the SEQUENCE op response:
1359 #define NFSD_MIN_HDR_SEQ_SZ (24 + 12 + 44)
1362 free_session_slots(struct nfsd4_session
*ses
)
1366 for (i
= 0; i
< ses
->se_fchannel
.maxreqs
; i
++)
1367 kfree(ses
->se_slots
[i
]);
1371 * We don't actually need to cache the rpc and session headers, so we
1372 * can allocate a little less for each slot:
1374 static inline u32
slot_bytes(struct nfsd4_channel_attrs
*ca
)
1378 if (ca
->maxresp_cached
< NFSD_MIN_HDR_SEQ_SZ
)
1381 size
= ca
->maxresp_cached
- NFSD_MIN_HDR_SEQ_SZ
;
1382 return size
+ sizeof(struct nfsd4_slot
);
1386 * XXX: If we run out of reserved DRC memory we could (up to a point)
1387 * re-negotiate active sessions and reduce their slot usage to make
1388 * room for new connections. For now we just fail the create session.
1390 static u32
nfsd4_get_drc_mem(struct nfsd4_channel_attrs
*ca
)
1392 u32 slotsize
= slot_bytes(ca
);
1393 u32 num
= ca
->maxreqs
;
1396 spin_lock(&nfsd_drc_lock
);
1397 avail
= min((unsigned long)NFSD_MAX_MEM_PER_SESSION
,
1398 nfsd_drc_max_mem
- nfsd_drc_mem_used
);
1399 num
= min_t(int, num
, avail
/ slotsize
);
1400 nfsd_drc_mem_used
+= num
* slotsize
;
1401 spin_unlock(&nfsd_drc_lock
);
1406 static void nfsd4_put_drc_mem(struct nfsd4_channel_attrs
*ca
)
1408 int slotsize
= slot_bytes(ca
);
1410 spin_lock(&nfsd_drc_lock
);
1411 nfsd_drc_mem_used
-= slotsize
* ca
->maxreqs
;
1412 spin_unlock(&nfsd_drc_lock
);
1415 static struct nfsd4_session
*alloc_session(struct nfsd4_channel_attrs
*fattrs
,
1416 struct nfsd4_channel_attrs
*battrs
)
1418 int numslots
= fattrs
->maxreqs
;
1419 int slotsize
= slot_bytes(fattrs
);
1420 struct nfsd4_session
*new;
1423 BUILD_BUG_ON(NFSD_MAX_SLOTS_PER_SESSION
* sizeof(struct nfsd4_slot
*)
1424 + sizeof(struct nfsd4_session
) > PAGE_SIZE
);
1425 mem
= numslots
* sizeof(struct nfsd4_slot
*);
1427 new = kzalloc(sizeof(*new) + mem
, GFP_KERNEL
);
1430 /* allocate each struct nfsd4_slot and data cache in one piece */
1431 for (i
= 0; i
< numslots
; i
++) {
1432 new->se_slots
[i
] = kzalloc(slotsize
, GFP_KERNEL
);
1433 if (!new->se_slots
[i
])
1437 memcpy(&new->se_fchannel
, fattrs
, sizeof(struct nfsd4_channel_attrs
));
1438 memcpy(&new->se_bchannel
, battrs
, sizeof(struct nfsd4_channel_attrs
));
1443 kfree(new->se_slots
[i
]);
1448 static void free_conn(struct nfsd4_conn
*c
)
1450 svc_xprt_put(c
->cn_xprt
);
1454 static void nfsd4_conn_lost(struct svc_xpt_user
*u
)
1456 struct nfsd4_conn
*c
= container_of(u
, struct nfsd4_conn
, cn_xpt_user
);
1457 struct nfs4_client
*clp
= c
->cn_session
->se_client
;
1459 spin_lock(&clp
->cl_lock
);
1460 if (!list_empty(&c
->cn_persession
)) {
1461 list_del(&c
->cn_persession
);
1464 nfsd4_probe_callback(clp
);
1465 spin_unlock(&clp
->cl_lock
);
1468 static struct nfsd4_conn
*alloc_conn(struct svc_rqst
*rqstp
, u32 flags
)
1470 struct nfsd4_conn
*conn
;
1472 conn
= kmalloc(sizeof(struct nfsd4_conn
), GFP_KERNEL
);
1475 svc_xprt_get(rqstp
->rq_xprt
);
1476 conn
->cn_xprt
= rqstp
->rq_xprt
;
1477 conn
->cn_flags
= flags
;
1478 INIT_LIST_HEAD(&conn
->cn_xpt_user
.list
);
1482 static void __nfsd4_hash_conn(struct nfsd4_conn
*conn
, struct nfsd4_session
*ses
)
1484 conn
->cn_session
= ses
;
1485 list_add(&conn
->cn_persession
, &ses
->se_conns
);
1488 static void nfsd4_hash_conn(struct nfsd4_conn
*conn
, struct nfsd4_session
*ses
)
1490 struct nfs4_client
*clp
= ses
->se_client
;
1492 spin_lock(&clp
->cl_lock
);
1493 __nfsd4_hash_conn(conn
, ses
);
1494 spin_unlock(&clp
->cl_lock
);
1497 static int nfsd4_register_conn(struct nfsd4_conn
*conn
)
1499 conn
->cn_xpt_user
.callback
= nfsd4_conn_lost
;
1500 return register_xpt_user(conn
->cn_xprt
, &conn
->cn_xpt_user
);
1503 static void nfsd4_init_conn(struct svc_rqst
*rqstp
, struct nfsd4_conn
*conn
, struct nfsd4_session
*ses
)
1507 nfsd4_hash_conn(conn
, ses
);
1508 ret
= nfsd4_register_conn(conn
);
1510 /* oops; xprt is already down: */
1511 nfsd4_conn_lost(&conn
->cn_xpt_user
);
1512 /* We may have gained or lost a callback channel: */
1513 nfsd4_probe_callback_sync(ses
->se_client
);
1516 static struct nfsd4_conn
*alloc_conn_from_crses(struct svc_rqst
*rqstp
, struct nfsd4_create_session
*cses
)
1518 u32 dir
= NFS4_CDFC4_FORE
;
1520 if (cses
->flags
& SESSION4_BACK_CHAN
)
1521 dir
|= NFS4_CDFC4_BACK
;
1522 return alloc_conn(rqstp
, dir
);
1525 /* must be called under client_lock */
1526 static void nfsd4_del_conns(struct nfsd4_session
*s
)
1528 struct nfs4_client
*clp
= s
->se_client
;
1529 struct nfsd4_conn
*c
;
1531 spin_lock(&clp
->cl_lock
);
1532 while (!list_empty(&s
->se_conns
)) {
1533 c
= list_first_entry(&s
->se_conns
, struct nfsd4_conn
, cn_persession
);
1534 list_del_init(&c
->cn_persession
);
1535 spin_unlock(&clp
->cl_lock
);
1537 unregister_xpt_user(c
->cn_xprt
, &c
->cn_xpt_user
);
1540 spin_lock(&clp
->cl_lock
);
1542 spin_unlock(&clp
->cl_lock
);
1545 static void __free_session(struct nfsd4_session
*ses
)
1547 free_session_slots(ses
);
1551 static void free_session(struct nfsd4_session
*ses
)
1553 nfsd4_del_conns(ses
);
1554 nfsd4_put_drc_mem(&ses
->se_fchannel
);
1555 __free_session(ses
);
1558 static void init_session(struct svc_rqst
*rqstp
, struct nfsd4_session
*new, struct nfs4_client
*clp
, struct nfsd4_create_session
*cses
)
1561 struct nfsd_net
*nn
= net_generic(SVC_NET(rqstp
), nfsd_net_id
);
1563 new->se_client
= clp
;
1566 INIT_LIST_HEAD(&new->se_conns
);
1568 new->se_cb_seq_nr
= 1;
1569 new->se_flags
= cses
->flags
;
1570 new->se_cb_prog
= cses
->callback_prog
;
1571 new->se_cb_sec
= cses
->cb_sec
;
1572 atomic_set(&new->se_ref
, 0);
1573 idx
= hash_sessionid(&new->se_sessionid
);
1574 list_add(&new->se_hash
, &nn
->sessionid_hashtbl
[idx
]);
1575 spin_lock(&clp
->cl_lock
);
1576 list_add(&new->se_perclnt
, &clp
->cl_sessions
);
1577 spin_unlock(&clp
->cl_lock
);
1580 struct sockaddr
*sa
= svc_addr(rqstp
);
1582 * This is a little silly; with sessions there's no real
1583 * use for the callback address. Use the peer address
1584 * as a reasonable default for now, but consider fixing
1585 * the rpc client not to require an address in the
1588 rpc_copy_addr((struct sockaddr
*)&clp
->cl_cb_conn
.cb_addr
, sa
);
1589 clp
->cl_cb_conn
.cb_addrlen
= svc_addr_len(sa
);
1593 /* caller must hold client_lock */
1594 static struct nfsd4_session
*
1595 __find_in_sessionid_hashtbl(struct nfs4_sessionid
*sessionid
, struct net
*net
)
1597 struct nfsd4_session
*elem
;
1599 struct nfsd_net
*nn
= net_generic(net
, nfsd_net_id
);
1601 lockdep_assert_held(&nn
->client_lock
);
1603 dump_sessionid(__func__
, sessionid
);
1604 idx
= hash_sessionid(sessionid
);
1605 /* Search in the appropriate list */
1606 list_for_each_entry(elem
, &nn
->sessionid_hashtbl
[idx
], se_hash
) {
1607 if (!memcmp(elem
->se_sessionid
.data
, sessionid
->data
,
1608 NFS4_MAX_SESSIONID_LEN
)) {
1613 dprintk("%s: session not found\n", __func__
);
1617 static struct nfsd4_session
*
1618 find_in_sessionid_hashtbl(struct nfs4_sessionid
*sessionid
, struct net
*net
,
1621 struct nfsd4_session
*session
;
1622 __be32 status
= nfserr_badsession
;
1624 session
= __find_in_sessionid_hashtbl(sessionid
, net
);
1627 status
= nfsd4_get_session_locked(session
);
1635 /* caller must hold client_lock */
1637 unhash_session(struct nfsd4_session
*ses
)
1639 struct nfs4_client
*clp
= ses
->se_client
;
1640 struct nfsd_net
*nn
= net_generic(clp
->net
, nfsd_net_id
);
1642 lockdep_assert_held(&nn
->client_lock
);
1644 list_del(&ses
->se_hash
);
1645 spin_lock(&ses
->se_client
->cl_lock
);
1646 list_del(&ses
->se_perclnt
);
1647 spin_unlock(&ses
->se_client
->cl_lock
);
1650 /* SETCLIENTID and SETCLIENTID_CONFIRM Helper functions */
1652 STALE_CLIENTID(clientid_t
*clid
, struct nfsd_net
*nn
)
1655 * We're assuming the clid was not given out from a boot
1656 * precisely 2^32 (about 136 years) before this one. That seems
1657 * a safe assumption:
1659 if (clid
->cl_boot
== (u32
)nn
->boot_time
)
1661 dprintk("NFSD stale clientid (%08x/%08x) boot_time %08lx\n",
1662 clid
->cl_boot
, clid
->cl_id
, nn
->boot_time
);
1667 * XXX Should we use a slab cache ?
1668 * This type of memory management is somewhat inefficient, but we use it
1669 * anyway since SETCLIENTID is not a common operation.
1671 static struct nfs4_client
*alloc_client(struct xdr_netobj name
)
1673 struct nfs4_client
*clp
;
1676 clp
= kzalloc(sizeof(struct nfs4_client
), GFP_KERNEL
);
1679 clp
->cl_name
.data
= kmemdup(name
.data
, name
.len
, GFP_KERNEL
);
1680 if (clp
->cl_name
.data
== NULL
)
1682 clp
->cl_ownerstr_hashtbl
= kmalloc(sizeof(struct list_head
) *
1683 OWNER_HASH_SIZE
, GFP_KERNEL
);
1684 if (!clp
->cl_ownerstr_hashtbl
)
1685 goto err_no_hashtbl
;
1686 for (i
= 0; i
< OWNER_HASH_SIZE
; i
++)
1687 INIT_LIST_HEAD(&clp
->cl_ownerstr_hashtbl
[i
]);
1688 clp
->cl_name
.len
= name
.len
;
1689 INIT_LIST_HEAD(&clp
->cl_sessions
);
1690 idr_init(&clp
->cl_stateids
);
1691 atomic_set(&clp
->cl_refcount
, 0);
1692 clp
->cl_cb_state
= NFSD4_CB_UNKNOWN
;
1693 INIT_LIST_HEAD(&clp
->cl_idhash
);
1694 INIT_LIST_HEAD(&clp
->cl_openowners
);
1695 INIT_LIST_HEAD(&clp
->cl_delegations
);
1696 INIT_LIST_HEAD(&clp
->cl_lru
);
1697 INIT_LIST_HEAD(&clp
->cl_revoked
);
1698 #ifdef CONFIG_NFSD_PNFS
1699 INIT_LIST_HEAD(&clp
->cl_lo_states
);
1701 spin_lock_init(&clp
->cl_lock
);
1702 rpc_init_wait_queue(&clp
->cl_cb_waitq
, "Backchannel slot table");
1705 kfree(clp
->cl_name
.data
);
1712 free_client(struct nfs4_client
*clp
)
1714 while (!list_empty(&clp
->cl_sessions
)) {
1715 struct nfsd4_session
*ses
;
1716 ses
= list_entry(clp
->cl_sessions
.next
, struct nfsd4_session
,
1718 list_del(&ses
->se_perclnt
);
1719 WARN_ON_ONCE(atomic_read(&ses
->se_ref
));
1722 rpc_destroy_wait_queue(&clp
->cl_cb_waitq
);
1723 free_svc_cred(&clp
->cl_cred
);
1724 kfree(clp
->cl_ownerstr_hashtbl
);
1725 kfree(clp
->cl_name
.data
);
1726 idr_destroy(&clp
->cl_stateids
);
1730 /* must be called under the client_lock */
1732 unhash_client_locked(struct nfs4_client
*clp
)
1734 struct nfsd_net
*nn
= net_generic(clp
->net
, nfsd_net_id
);
1735 struct nfsd4_session
*ses
;
1737 lockdep_assert_held(&nn
->client_lock
);
1739 /* Mark the client as expired! */
1741 /* Make it invisible */
1742 if (!list_empty(&clp
->cl_idhash
)) {
1743 list_del_init(&clp
->cl_idhash
);
1744 if (test_bit(NFSD4_CLIENT_CONFIRMED
, &clp
->cl_flags
))
1745 rb_erase(&clp
->cl_namenode
, &nn
->conf_name_tree
);
1747 rb_erase(&clp
->cl_namenode
, &nn
->unconf_name_tree
);
1749 list_del_init(&clp
->cl_lru
);
1750 spin_lock(&clp
->cl_lock
);
1751 list_for_each_entry(ses
, &clp
->cl_sessions
, se_perclnt
)
1752 list_del_init(&ses
->se_hash
);
1753 spin_unlock(&clp
->cl_lock
);
1757 unhash_client(struct nfs4_client
*clp
)
1759 struct nfsd_net
*nn
= net_generic(clp
->net
, nfsd_net_id
);
1761 spin_lock(&nn
->client_lock
);
1762 unhash_client_locked(clp
);
1763 spin_unlock(&nn
->client_lock
);
1766 static __be32
mark_client_expired_locked(struct nfs4_client
*clp
)
1768 if (atomic_read(&clp
->cl_refcount
))
1769 return nfserr_jukebox
;
1770 unhash_client_locked(clp
);
1775 __destroy_client(struct nfs4_client
*clp
)
1777 struct nfs4_openowner
*oo
;
1778 struct nfs4_delegation
*dp
;
1779 struct list_head reaplist
;
1781 INIT_LIST_HEAD(&reaplist
);
1782 spin_lock(&state_lock
);
1783 while (!list_empty(&clp
->cl_delegations
)) {
1784 dp
= list_entry(clp
->cl_delegations
.next
, struct nfs4_delegation
, dl_perclnt
);
1785 WARN_ON(!unhash_delegation_locked(dp
));
1786 list_add(&dp
->dl_recall_lru
, &reaplist
);
1788 spin_unlock(&state_lock
);
1789 while (!list_empty(&reaplist
)) {
1790 dp
= list_entry(reaplist
.next
, struct nfs4_delegation
, dl_recall_lru
);
1791 list_del_init(&dp
->dl_recall_lru
);
1792 put_clnt_odstate(dp
->dl_clnt_odstate
);
1793 nfs4_put_deleg_lease(dp
->dl_stid
.sc_file
);
1794 nfs4_put_stid(&dp
->dl_stid
);
1796 while (!list_empty(&clp
->cl_revoked
)) {
1797 dp
= list_entry(clp
->cl_revoked
.next
, struct nfs4_delegation
, dl_recall_lru
);
1798 list_del_init(&dp
->dl_recall_lru
);
1799 nfs4_put_stid(&dp
->dl_stid
);
1801 while (!list_empty(&clp
->cl_openowners
)) {
1802 oo
= list_entry(clp
->cl_openowners
.next
, struct nfs4_openowner
, oo_perclient
);
1803 nfs4_get_stateowner(&oo
->oo_owner
);
1804 release_openowner(oo
);
1806 nfsd4_return_all_client_layouts(clp
);
1807 nfsd4_shutdown_callback(clp
);
1808 if (clp
->cl_cb_conn
.cb_xprt
)
1809 svc_xprt_put(clp
->cl_cb_conn
.cb_xprt
);
1814 destroy_client(struct nfs4_client
*clp
)
1817 __destroy_client(clp
);
1820 static void expire_client(struct nfs4_client
*clp
)
1823 nfsd4_client_record_remove(clp
);
1824 __destroy_client(clp
);
1827 static void copy_verf(struct nfs4_client
*target
, nfs4_verifier
*source
)
1829 memcpy(target
->cl_verifier
.data
, source
->data
,
1830 sizeof(target
->cl_verifier
.data
));
1833 static void copy_clid(struct nfs4_client
*target
, struct nfs4_client
*source
)
1835 target
->cl_clientid
.cl_boot
= source
->cl_clientid
.cl_boot
;
1836 target
->cl_clientid
.cl_id
= source
->cl_clientid
.cl_id
;
1839 static int copy_cred(struct svc_cred
*target
, struct svc_cred
*source
)
1841 if (source
->cr_principal
) {
1842 target
->cr_principal
=
1843 kstrdup(source
->cr_principal
, GFP_KERNEL
);
1844 if (target
->cr_principal
== NULL
)
1847 target
->cr_principal
= NULL
;
1848 target
->cr_flavor
= source
->cr_flavor
;
1849 target
->cr_uid
= source
->cr_uid
;
1850 target
->cr_gid
= source
->cr_gid
;
1851 target
->cr_group_info
= source
->cr_group_info
;
1852 get_group_info(target
->cr_group_info
);
1853 target
->cr_gss_mech
= source
->cr_gss_mech
;
1854 if (source
->cr_gss_mech
)
1855 gss_mech_get(source
->cr_gss_mech
);
1860 compare_blob(const struct xdr_netobj
*o1
, const struct xdr_netobj
*o2
)
1862 if (o1
->len
< o2
->len
)
1864 if (o1
->len
> o2
->len
)
1866 return memcmp(o1
->data
, o2
->data
, o1
->len
);
1869 static int same_name(const char *n1
, const char *n2
)
1871 return 0 == memcmp(n1
, n2
, HEXDIR_LEN
);
1875 same_verf(nfs4_verifier
*v1
, nfs4_verifier
*v2
)
1877 return 0 == memcmp(v1
->data
, v2
->data
, sizeof(v1
->data
));
1881 same_clid(clientid_t
*cl1
, clientid_t
*cl2
)
1883 return (cl1
->cl_boot
== cl2
->cl_boot
) && (cl1
->cl_id
== cl2
->cl_id
);
1886 static bool groups_equal(struct group_info
*g1
, struct group_info
*g2
)
1890 if (g1
->ngroups
!= g2
->ngroups
)
1892 for (i
=0; i
<g1
->ngroups
; i
++)
1893 if (!gid_eq(GROUP_AT(g1
, i
), GROUP_AT(g2
, i
)))
1899 * RFC 3530 language requires clid_inuse be returned when the
1900 * "principal" associated with a requests differs from that previously
1901 * used. We use uid, gid's, and gss principal string as our best
1902 * approximation. We also don't want to allow non-gss use of a client
1903 * established using gss: in theory cr_principal should catch that
1904 * change, but in practice cr_principal can be null even in the gss case
1905 * since gssd doesn't always pass down a principal string.
1907 static bool is_gss_cred(struct svc_cred
*cr
)
1909 /* Is cr_flavor one of the gss "pseudoflavors"?: */
1910 return (cr
->cr_flavor
> RPC_AUTH_MAXFLAVOR
);
1915 same_creds(struct svc_cred
*cr1
, struct svc_cred
*cr2
)
1917 if ((is_gss_cred(cr1
) != is_gss_cred(cr2
))
1918 || (!uid_eq(cr1
->cr_uid
, cr2
->cr_uid
))
1919 || (!gid_eq(cr1
->cr_gid
, cr2
->cr_gid
))
1920 || !groups_equal(cr1
->cr_group_info
, cr2
->cr_group_info
))
1922 if (cr1
->cr_principal
== cr2
->cr_principal
)
1924 if (!cr1
->cr_principal
|| !cr2
->cr_principal
)
1926 return 0 == strcmp(cr1
->cr_principal
, cr2
->cr_principal
);
1929 static bool svc_rqst_integrity_protected(struct svc_rqst
*rqstp
)
1931 struct svc_cred
*cr
= &rqstp
->rq_cred
;
1934 if (!cr
->cr_gss_mech
)
1936 service
= gss_pseudoflavor_to_service(cr
->cr_gss_mech
, cr
->cr_flavor
);
1937 return service
== RPC_GSS_SVC_INTEGRITY
||
1938 service
== RPC_GSS_SVC_PRIVACY
;
1941 static bool mach_creds_match(struct nfs4_client
*cl
, struct svc_rqst
*rqstp
)
1943 struct svc_cred
*cr
= &rqstp
->rq_cred
;
1945 if (!cl
->cl_mach_cred
)
1947 if (cl
->cl_cred
.cr_gss_mech
!= cr
->cr_gss_mech
)
1949 if (!svc_rqst_integrity_protected(rqstp
))
1951 if (!cr
->cr_principal
)
1953 return 0 == strcmp(cl
->cl_cred
.cr_principal
, cr
->cr_principal
);
1956 static void gen_confirm(struct nfs4_client
*clp
, struct nfsd_net
*nn
)
1961 * This is opaque to client, so no need to byte-swap. Use
1962 * __force to keep sparse happy
1964 verf
[0] = (__force __be32
)get_seconds();
1965 verf
[1] = (__force __be32
)nn
->clverifier_counter
++;
1966 memcpy(clp
->cl_confirm
.data
, verf
, sizeof(clp
->cl_confirm
.data
));
1969 static void gen_clid(struct nfs4_client
*clp
, struct nfsd_net
*nn
)
1971 clp
->cl_clientid
.cl_boot
= nn
->boot_time
;
1972 clp
->cl_clientid
.cl_id
= nn
->clientid_counter
++;
1973 gen_confirm(clp
, nn
);
1976 static struct nfs4_stid
*
1977 find_stateid_locked(struct nfs4_client
*cl
, stateid_t
*t
)
1979 struct nfs4_stid
*ret
;
1981 ret
= idr_find(&cl
->cl_stateids
, t
->si_opaque
.so_id
);
1982 if (!ret
|| !ret
->sc_type
)
1987 static struct nfs4_stid
*
1988 find_stateid_by_type(struct nfs4_client
*cl
, stateid_t
*t
, char typemask
)
1990 struct nfs4_stid
*s
;
1992 spin_lock(&cl
->cl_lock
);
1993 s
= find_stateid_locked(cl
, t
);
1995 if (typemask
& s
->sc_type
)
1996 atomic_inc(&s
->sc_count
);
2000 spin_unlock(&cl
->cl_lock
);
2004 static struct nfs4_client
*create_client(struct xdr_netobj name
,
2005 struct svc_rqst
*rqstp
, nfs4_verifier
*verf
)
2007 struct nfs4_client
*clp
;
2008 struct sockaddr
*sa
= svc_addr(rqstp
);
2010 struct net
*net
= SVC_NET(rqstp
);
2012 clp
= alloc_client(name
);
2016 ret
= copy_cred(&clp
->cl_cred
, &rqstp
->rq_cred
);
2021 nfsd4_init_cb(&clp
->cl_cb_null
, clp
, NULL
, NFSPROC4_CLNT_CB_NULL
);
2022 clp
->cl_time
= get_seconds();
2023 clear_bit(0, &clp
->cl_cb_slot_busy
);
2024 copy_verf(clp
, verf
);
2025 rpc_copy_addr((struct sockaddr
*) &clp
->cl_addr
, sa
);
2026 clp
->cl_cb_session
= NULL
;
2032 add_clp_to_name_tree(struct nfs4_client
*new_clp
, struct rb_root
*root
)
2034 struct rb_node
**new = &(root
->rb_node
), *parent
= NULL
;
2035 struct nfs4_client
*clp
;
2038 clp
= rb_entry(*new, struct nfs4_client
, cl_namenode
);
2041 if (compare_blob(&clp
->cl_name
, &new_clp
->cl_name
) > 0)
2042 new = &((*new)->rb_left
);
2044 new = &((*new)->rb_right
);
2047 rb_link_node(&new_clp
->cl_namenode
, parent
, new);
2048 rb_insert_color(&new_clp
->cl_namenode
, root
);
2051 static struct nfs4_client
*
2052 find_clp_in_name_tree(struct xdr_netobj
*name
, struct rb_root
*root
)
2055 struct rb_node
*node
= root
->rb_node
;
2056 struct nfs4_client
*clp
;
2059 clp
= rb_entry(node
, struct nfs4_client
, cl_namenode
);
2060 cmp
= compare_blob(&clp
->cl_name
, name
);
2062 node
= node
->rb_left
;
2064 node
= node
->rb_right
;
2072 add_to_unconfirmed(struct nfs4_client
*clp
)
2074 unsigned int idhashval
;
2075 struct nfsd_net
*nn
= net_generic(clp
->net
, nfsd_net_id
);
2077 lockdep_assert_held(&nn
->client_lock
);
2079 clear_bit(NFSD4_CLIENT_CONFIRMED
, &clp
->cl_flags
);
2080 add_clp_to_name_tree(clp
, &nn
->unconf_name_tree
);
2081 idhashval
= clientid_hashval(clp
->cl_clientid
.cl_id
);
2082 list_add(&clp
->cl_idhash
, &nn
->unconf_id_hashtbl
[idhashval
]);
2083 renew_client_locked(clp
);
2087 move_to_confirmed(struct nfs4_client
*clp
)
2089 unsigned int idhashval
= clientid_hashval(clp
->cl_clientid
.cl_id
);
2090 struct nfsd_net
*nn
= net_generic(clp
->net
, nfsd_net_id
);
2092 lockdep_assert_held(&nn
->client_lock
);
2094 dprintk("NFSD: move_to_confirm nfs4_client %p\n", clp
);
2095 list_move(&clp
->cl_idhash
, &nn
->conf_id_hashtbl
[idhashval
]);
2096 rb_erase(&clp
->cl_namenode
, &nn
->unconf_name_tree
);
2097 add_clp_to_name_tree(clp
, &nn
->conf_name_tree
);
2098 set_bit(NFSD4_CLIENT_CONFIRMED
, &clp
->cl_flags
);
2099 renew_client_locked(clp
);
2102 static struct nfs4_client
*
2103 find_client_in_id_table(struct list_head
*tbl
, clientid_t
*clid
, bool sessions
)
2105 struct nfs4_client
*clp
;
2106 unsigned int idhashval
= clientid_hashval(clid
->cl_id
);
2108 list_for_each_entry(clp
, &tbl
[idhashval
], cl_idhash
) {
2109 if (same_clid(&clp
->cl_clientid
, clid
)) {
2110 if ((bool)clp
->cl_minorversion
!= sessions
)
2112 renew_client_locked(clp
);
2119 static struct nfs4_client
*
2120 find_confirmed_client(clientid_t
*clid
, bool sessions
, struct nfsd_net
*nn
)
2122 struct list_head
*tbl
= nn
->conf_id_hashtbl
;
2124 lockdep_assert_held(&nn
->client_lock
);
2125 return find_client_in_id_table(tbl
, clid
, sessions
);
2128 static struct nfs4_client
*
2129 find_unconfirmed_client(clientid_t
*clid
, bool sessions
, struct nfsd_net
*nn
)
2131 struct list_head
*tbl
= nn
->unconf_id_hashtbl
;
2133 lockdep_assert_held(&nn
->client_lock
);
2134 return find_client_in_id_table(tbl
, clid
, sessions
);
2137 static bool clp_used_exchangeid(struct nfs4_client
*clp
)
2139 return clp
->cl_exchange_flags
!= 0;
2142 static struct nfs4_client
*
2143 find_confirmed_client_by_name(struct xdr_netobj
*name
, struct nfsd_net
*nn
)
2145 lockdep_assert_held(&nn
->client_lock
);
2146 return find_clp_in_name_tree(name
, &nn
->conf_name_tree
);
2149 static struct nfs4_client
*
2150 find_unconfirmed_client_by_name(struct xdr_netobj
*name
, struct nfsd_net
*nn
)
2152 lockdep_assert_held(&nn
->client_lock
);
2153 return find_clp_in_name_tree(name
, &nn
->unconf_name_tree
);
2157 gen_callback(struct nfs4_client
*clp
, struct nfsd4_setclientid
*se
, struct svc_rqst
*rqstp
)
2159 struct nfs4_cb_conn
*conn
= &clp
->cl_cb_conn
;
2160 struct sockaddr
*sa
= svc_addr(rqstp
);
2161 u32 scopeid
= rpc_get_scope_id(sa
);
2162 unsigned short expected_family
;
2164 /* Currently, we only support tcp and tcp6 for the callback channel */
2165 if (se
->se_callback_netid_len
== 3 &&
2166 !memcmp(se
->se_callback_netid_val
, "tcp", 3))
2167 expected_family
= AF_INET
;
2168 else if (se
->se_callback_netid_len
== 4 &&
2169 !memcmp(se
->se_callback_netid_val
, "tcp6", 4))
2170 expected_family
= AF_INET6
;
2174 conn
->cb_addrlen
= rpc_uaddr2sockaddr(clp
->net
, se
->se_callback_addr_val
,
2175 se
->se_callback_addr_len
,
2176 (struct sockaddr
*)&conn
->cb_addr
,
2177 sizeof(conn
->cb_addr
));
2179 if (!conn
->cb_addrlen
|| conn
->cb_addr
.ss_family
!= expected_family
)
2182 if (conn
->cb_addr
.ss_family
== AF_INET6
)
2183 ((struct sockaddr_in6
*)&conn
->cb_addr
)->sin6_scope_id
= scopeid
;
2185 conn
->cb_prog
= se
->se_callback_prog
;
2186 conn
->cb_ident
= se
->se_callback_ident
;
2187 memcpy(&conn
->cb_saddr
, &rqstp
->rq_daddr
, rqstp
->rq_daddrlen
);
2190 conn
->cb_addr
.ss_family
= AF_UNSPEC
;
2191 conn
->cb_addrlen
= 0;
2192 dprintk(KERN_INFO
"NFSD: this client (clientid %08x/%08x) "
2193 "will not receive delegations\n",
2194 clp
->cl_clientid
.cl_boot
, clp
->cl_clientid
.cl_id
);
2200 * Cache a reply. nfsd4_check_resp_size() has bounded the cache size.
2203 nfsd4_store_cache_entry(struct nfsd4_compoundres
*resp
)
2205 struct xdr_buf
*buf
= resp
->xdr
.buf
;
2206 struct nfsd4_slot
*slot
= resp
->cstate
.slot
;
2209 dprintk("--> %s slot %p\n", __func__
, slot
);
2211 slot
->sl_opcnt
= resp
->opcnt
;
2212 slot
->sl_status
= resp
->cstate
.status
;
2214 slot
->sl_flags
|= NFSD4_SLOT_INITIALIZED
;
2215 if (nfsd4_not_cached(resp
)) {
2216 slot
->sl_datalen
= 0;
2219 base
= resp
->cstate
.data_offset
;
2220 slot
->sl_datalen
= buf
->len
- base
;
2221 if (read_bytes_from_xdr_buf(buf
, base
, slot
->sl_data
, slot
->sl_datalen
))
2222 WARN("%s: sessions DRC could not cache compound\n", __func__
);
2227 * Encode the replay sequence operation from the slot values.
2228 * If cachethis is FALSE encode the uncached rep error on the next
2229 * operation which sets resp->p and increments resp->opcnt for
2230 * nfs4svc_encode_compoundres.
2234 nfsd4_enc_sequence_replay(struct nfsd4_compoundargs
*args
,
2235 struct nfsd4_compoundres
*resp
)
2237 struct nfsd4_op
*op
;
2238 struct nfsd4_slot
*slot
= resp
->cstate
.slot
;
2240 /* Encode the replayed sequence operation */
2241 op
= &args
->ops
[resp
->opcnt
- 1];
2242 nfsd4_encode_operation(resp
, op
);
2244 /* Return nfserr_retry_uncached_rep in next operation. */
2245 if (args
->opcnt
> 1 && !(slot
->sl_flags
& NFSD4_SLOT_CACHETHIS
)) {
2246 op
= &args
->ops
[resp
->opcnt
++];
2247 op
->status
= nfserr_retry_uncached_rep
;
2248 nfsd4_encode_operation(resp
, op
);
2254 * The sequence operation is not cached because we can use the slot and
2258 nfsd4_replay_cache_entry(struct nfsd4_compoundres
*resp
,
2259 struct nfsd4_sequence
*seq
)
2261 struct nfsd4_slot
*slot
= resp
->cstate
.slot
;
2262 struct xdr_stream
*xdr
= &resp
->xdr
;
2266 dprintk("--> %s slot %p\n", __func__
, slot
);
2268 status
= nfsd4_enc_sequence_replay(resp
->rqstp
->rq_argp
, resp
);
2272 p
= xdr_reserve_space(xdr
, slot
->sl_datalen
);
2275 return nfserr_serverfault
;
2277 xdr_encode_opaque_fixed(p
, slot
->sl_data
, slot
->sl_datalen
);
2278 xdr_commit_encode(xdr
);
2280 resp
->opcnt
= slot
->sl_opcnt
;
2281 return slot
->sl_status
;
2285 * Set the exchange_id flags returned by the server.
2288 nfsd4_set_ex_flags(struct nfs4_client
*new, struct nfsd4_exchange_id
*clid
)
2290 #ifdef CONFIG_NFSD_PNFS
2291 new->cl_exchange_flags
|= EXCHGID4_FLAG_USE_PNFS_MDS
;
2293 new->cl_exchange_flags
|= EXCHGID4_FLAG_USE_NON_PNFS
;
2296 /* Referrals are supported, Migration is not. */
2297 new->cl_exchange_flags
|= EXCHGID4_FLAG_SUPP_MOVED_REFER
;
2299 /* set the wire flags to return to client. */
2300 clid
->flags
= new->cl_exchange_flags
;
2303 static bool client_has_openowners(struct nfs4_client
*clp
)
2305 struct nfs4_openowner
*oo
;
2307 list_for_each_entry(oo
, &clp
->cl_openowners
, oo_perclient
) {
2308 if (!list_empty(&oo
->oo_owner
.so_stateids
))
2314 static bool client_has_state(struct nfs4_client
*clp
)
2316 return client_has_openowners(clp
)
2317 #ifdef CONFIG_NFSD_PNFS
2318 || !list_empty(&clp
->cl_lo_states
)
2320 || !list_empty(&clp
->cl_delegations
)
2321 || !list_empty(&clp
->cl_sessions
);
2325 nfsd4_exchange_id(struct svc_rqst
*rqstp
,
2326 struct nfsd4_compound_state
*cstate
,
2327 struct nfsd4_exchange_id
*exid
)
2329 struct nfs4_client
*conf
, *new;
2330 struct nfs4_client
*unconf
= NULL
;
2332 char addr_str
[INET6_ADDRSTRLEN
];
2333 nfs4_verifier verf
= exid
->verifier
;
2334 struct sockaddr
*sa
= svc_addr(rqstp
);
2335 bool update
= exid
->flags
& EXCHGID4_FLAG_UPD_CONFIRMED_REC_A
;
2336 struct nfsd_net
*nn
= net_generic(SVC_NET(rqstp
), nfsd_net_id
);
2338 rpc_ntop(sa
, addr_str
, sizeof(addr_str
));
2339 dprintk("%s rqstp=%p exid=%p clname.len=%u clname.data=%p "
2340 "ip_addr=%s flags %x, spa_how %d\n",
2341 __func__
, rqstp
, exid
, exid
->clname
.len
, exid
->clname
.data
,
2342 addr_str
, exid
->flags
, exid
->spa_how
);
2344 if (exid
->flags
& ~EXCHGID4_FLAG_MASK_A
)
2345 return nfserr_inval
;
2347 switch (exid
->spa_how
) {
2349 if (!svc_rqst_integrity_protected(rqstp
))
2350 return nfserr_inval
;
2353 default: /* checked by xdr code */
2356 return nfserr_encr_alg_unsupp
;
2359 new = create_client(exid
->clname
, rqstp
, &verf
);
2361 return nfserr_jukebox
;
2363 /* Cases below refer to rfc 5661 section 18.35.4: */
2364 spin_lock(&nn
->client_lock
);
2365 conf
= find_confirmed_client_by_name(&exid
->clname
, nn
);
2367 bool creds_match
= same_creds(&conf
->cl_cred
, &rqstp
->rq_cred
);
2368 bool verfs_match
= same_verf(&verf
, &conf
->cl_verifier
);
2371 if (!clp_used_exchangeid(conf
)) { /* buggy client */
2372 status
= nfserr_inval
;
2375 if (!mach_creds_match(conf
, rqstp
)) {
2376 status
= nfserr_wrong_cred
;
2379 if (!creds_match
) { /* case 9 */
2380 status
= nfserr_perm
;
2383 if (!verfs_match
) { /* case 8 */
2384 status
= nfserr_not_same
;
2388 exid
->flags
|= EXCHGID4_FLAG_CONFIRMED_R
;
2391 if (!creds_match
) { /* case 3 */
2392 if (client_has_state(conf
)) {
2393 status
= nfserr_clid_inuse
;
2398 if (verfs_match
) { /* case 2 */
2399 conf
->cl_exchange_flags
|= EXCHGID4_FLAG_CONFIRMED_R
;
2402 /* case 5, client reboot */
2407 if (update
) { /* case 7 */
2408 status
= nfserr_noent
;
2412 unconf
= find_unconfirmed_client_by_name(&exid
->clname
, nn
);
2413 if (unconf
) /* case 4, possible retry or client restart */
2414 unhash_client_locked(unconf
);
2416 /* case 1 (normal case) */
2419 status
= mark_client_expired_locked(conf
);
2423 new->cl_minorversion
= cstate
->minorversion
;
2424 new->cl_mach_cred
= (exid
->spa_how
== SP4_MACH_CRED
);
2427 add_to_unconfirmed(new);
2430 exid
->clientid
.cl_boot
= conf
->cl_clientid
.cl_boot
;
2431 exid
->clientid
.cl_id
= conf
->cl_clientid
.cl_id
;
2433 exid
->seqid
= conf
->cl_cs_slot
.sl_seqid
+ 1;
2434 nfsd4_set_ex_flags(conf
, exid
);
2436 dprintk("nfsd4_exchange_id seqid %d flags %x\n",
2437 conf
->cl_cs_slot
.sl_seqid
, conf
->cl_exchange_flags
);
2441 spin_unlock(&nn
->client_lock
);
2445 expire_client(unconf
);
2450 check_slot_seqid(u32 seqid
, u32 slot_seqid
, int slot_inuse
)
2452 dprintk("%s enter. seqid %d slot_seqid %d\n", __func__
, seqid
,
2455 /* The slot is in use, and no response has been sent. */
2457 if (seqid
== slot_seqid
)
2458 return nfserr_jukebox
;
2460 return nfserr_seq_misordered
;
2462 /* Note unsigned 32-bit arithmetic handles wraparound: */
2463 if (likely(seqid
== slot_seqid
+ 1))
2465 if (seqid
== slot_seqid
)
2466 return nfserr_replay_cache
;
2467 return nfserr_seq_misordered
;
2471 * Cache the create session result into the create session single DRC
2472 * slot cache by saving the xdr structure. sl_seqid has been set.
2473 * Do this for solo or embedded create session operations.
2476 nfsd4_cache_create_session(struct nfsd4_create_session
*cr_ses
,
2477 struct nfsd4_clid_slot
*slot
, __be32 nfserr
)
2479 slot
->sl_status
= nfserr
;
2480 memcpy(&slot
->sl_cr_ses
, cr_ses
, sizeof(*cr_ses
));
2484 nfsd4_replay_create_session(struct nfsd4_create_session
*cr_ses
,
2485 struct nfsd4_clid_slot
*slot
)
2487 memcpy(cr_ses
, &slot
->sl_cr_ses
, sizeof(*cr_ses
));
2488 return slot
->sl_status
;
2491 #define NFSD_MIN_REQ_HDR_SEQ_SZ ((\
2492 2 * 2 + /* credential,verifier: AUTH_NULL, length 0 */ \
2493 1 + /* MIN tag is length with zero, only length */ \
2494 3 + /* version, opcount, opcode */ \
2495 XDR_QUADLEN(NFS4_MAX_SESSIONID_LEN) + \
2496 /* seqid, slotID, slotID, cache */ \
2497 4 ) * sizeof(__be32))
2499 #define NFSD_MIN_RESP_HDR_SEQ_SZ ((\
2500 2 + /* verifier: AUTH_NULL, length 0 */\
2502 1 + /* MIN tag is length with zero, only length */ \
2503 3 + /* opcount, opcode, opstatus*/ \
2504 XDR_QUADLEN(NFS4_MAX_SESSIONID_LEN) + \
2505 /* seqid, slotID, slotID, slotID, status */ \
2506 5 ) * sizeof(__be32))
2508 static __be32
check_forechannel_attrs(struct nfsd4_channel_attrs
*ca
, struct nfsd_net
*nn
)
2510 u32 maxrpc
= nn
->nfsd_serv
->sv_max_mesg
;
2512 if (ca
->maxreq_sz
< NFSD_MIN_REQ_HDR_SEQ_SZ
)
2513 return nfserr_toosmall
;
2514 if (ca
->maxresp_sz
< NFSD_MIN_RESP_HDR_SEQ_SZ
)
2515 return nfserr_toosmall
;
2516 ca
->headerpadsz
= 0;
2517 ca
->maxreq_sz
= min_t(u32
, ca
->maxreq_sz
, maxrpc
);
2518 ca
->maxresp_sz
= min_t(u32
, ca
->maxresp_sz
, maxrpc
);
2519 ca
->maxops
= min_t(u32
, ca
->maxops
, NFSD_MAX_OPS_PER_COMPOUND
);
2520 ca
->maxresp_cached
= min_t(u32
, ca
->maxresp_cached
,
2521 NFSD_SLOT_CACHE_SIZE
+ NFSD_MIN_HDR_SEQ_SZ
);
2522 ca
->maxreqs
= min_t(u32
, ca
->maxreqs
, NFSD_MAX_SLOTS_PER_SESSION
);
2524 * Note decreasing slot size below client's request may make it
2525 * difficult for client to function correctly, whereas
2526 * decreasing the number of slots will (just?) affect
2527 * performance. When short on memory we therefore prefer to
2528 * decrease number of slots instead of their size. Clients that
2529 * request larger slots than they need will get poor results:
2531 ca
->maxreqs
= nfsd4_get_drc_mem(ca
);
2533 return nfserr_jukebox
;
2538 #define NFSD_CB_MAX_REQ_SZ ((NFS4_enc_cb_recall_sz + \
2539 RPC_MAX_HEADER_WITH_AUTH) * sizeof(__be32))
2540 #define NFSD_CB_MAX_RESP_SZ ((NFS4_dec_cb_recall_sz + \
2541 RPC_MAX_REPHEADER_WITH_AUTH) * sizeof(__be32))
2543 static __be32
check_backchannel_attrs(struct nfsd4_channel_attrs
*ca
)
2545 ca
->headerpadsz
= 0;
2548 * These RPC_MAX_HEADER macros are overkill, especially since we
2549 * don't even do gss on the backchannel yet. But this is still
2550 * less than 1k. Tighten up this estimate in the unlikely event
2551 * it turns out to be a problem for some client:
2553 if (ca
->maxreq_sz
< NFSD_CB_MAX_REQ_SZ
)
2554 return nfserr_toosmall
;
2555 if (ca
->maxresp_sz
< NFSD_CB_MAX_RESP_SZ
)
2556 return nfserr_toosmall
;
2557 ca
->maxresp_cached
= 0;
2559 return nfserr_toosmall
;
2564 static __be32
nfsd4_check_cb_sec(struct nfsd4_cb_sec
*cbs
)
2566 switch (cbs
->flavor
) {
2572 * GSS case: the spec doesn't allow us to return this
2573 * error. But it also doesn't allow us not to support
2575 * I'd rather this fail hard than return some error the
2576 * client might think it can already handle:
2578 return nfserr_encr_alg_unsupp
;
2583 nfsd4_create_session(struct svc_rqst
*rqstp
,
2584 struct nfsd4_compound_state
*cstate
,
2585 struct nfsd4_create_session
*cr_ses
)
2587 struct sockaddr
*sa
= svc_addr(rqstp
);
2588 struct nfs4_client
*conf
, *unconf
;
2589 struct nfs4_client
*old
= NULL
;
2590 struct nfsd4_session
*new;
2591 struct nfsd4_conn
*conn
;
2592 struct nfsd4_clid_slot
*cs_slot
= NULL
;
2594 struct nfsd_net
*nn
= net_generic(SVC_NET(rqstp
), nfsd_net_id
);
2596 if (cr_ses
->flags
& ~SESSION4_FLAG_MASK_A
)
2597 return nfserr_inval
;
2598 status
= nfsd4_check_cb_sec(&cr_ses
->cb_sec
);
2601 status
= check_forechannel_attrs(&cr_ses
->fore_channel
, nn
);
2604 status
= check_backchannel_attrs(&cr_ses
->back_channel
);
2606 goto out_release_drc_mem
;
2607 status
= nfserr_jukebox
;
2608 new = alloc_session(&cr_ses
->fore_channel
, &cr_ses
->back_channel
);
2610 goto out_release_drc_mem
;
2611 conn
= alloc_conn_from_crses(rqstp
, cr_ses
);
2613 goto out_free_session
;
2615 spin_lock(&nn
->client_lock
);
2616 unconf
= find_unconfirmed_client(&cr_ses
->clientid
, true, nn
);
2617 conf
= find_confirmed_client(&cr_ses
->clientid
, true, nn
);
2618 WARN_ON_ONCE(conf
&& unconf
);
2621 status
= nfserr_wrong_cred
;
2622 if (!mach_creds_match(conf
, rqstp
))
2624 cs_slot
= &conf
->cl_cs_slot
;
2625 status
= check_slot_seqid(cr_ses
->seqid
, cs_slot
->sl_seqid
, 0);
2627 if (status
== nfserr_replay_cache
)
2628 status
= nfsd4_replay_create_session(cr_ses
, cs_slot
);
2631 } else if (unconf
) {
2632 if (!same_creds(&unconf
->cl_cred
, &rqstp
->rq_cred
) ||
2633 !rpc_cmp_addr(sa
, (struct sockaddr
*) &unconf
->cl_addr
)) {
2634 status
= nfserr_clid_inuse
;
2637 status
= nfserr_wrong_cred
;
2638 if (!mach_creds_match(unconf
, rqstp
))
2640 cs_slot
= &unconf
->cl_cs_slot
;
2641 status
= check_slot_seqid(cr_ses
->seqid
, cs_slot
->sl_seqid
, 0);
2643 /* an unconfirmed replay returns misordered */
2644 status
= nfserr_seq_misordered
;
2647 old
= find_confirmed_client_by_name(&unconf
->cl_name
, nn
);
2649 status
= mark_client_expired_locked(old
);
2655 move_to_confirmed(unconf
);
2658 status
= nfserr_stale_clientid
;
2663 * We do not support RDMA or persistent sessions
2665 cr_ses
->flags
&= ~SESSION4_PERSIST
;
2666 cr_ses
->flags
&= ~SESSION4_RDMA
;
2668 init_session(rqstp
, new, conf
, cr_ses
);
2669 nfsd4_get_session_locked(new);
2671 memcpy(cr_ses
->sessionid
.data
, new->se_sessionid
.data
,
2672 NFS4_MAX_SESSIONID_LEN
);
2673 cs_slot
->sl_seqid
++;
2674 cr_ses
->seqid
= cs_slot
->sl_seqid
;
2676 /* cache solo and embedded create sessions under the client_lock */
2677 nfsd4_cache_create_session(cr_ses
, cs_slot
, status
);
2678 spin_unlock(&nn
->client_lock
);
2679 /* init connection and backchannel */
2680 nfsd4_init_conn(rqstp
, conn
, new);
2681 nfsd4_put_session(new);
2686 spin_unlock(&nn
->client_lock
);
2691 __free_session(new);
2692 out_release_drc_mem
:
2693 nfsd4_put_drc_mem(&cr_ses
->fore_channel
);
2697 static __be32
nfsd4_map_bcts_dir(u32
*dir
)
2700 case NFS4_CDFC4_FORE
:
2701 case NFS4_CDFC4_BACK
:
2703 case NFS4_CDFC4_FORE_OR_BOTH
:
2704 case NFS4_CDFC4_BACK_OR_BOTH
:
2705 *dir
= NFS4_CDFC4_BOTH
;
2708 return nfserr_inval
;
2711 __be32
nfsd4_backchannel_ctl(struct svc_rqst
*rqstp
, struct nfsd4_compound_state
*cstate
, struct nfsd4_backchannel_ctl
*bc
)
2713 struct nfsd4_session
*session
= cstate
->session
;
2714 struct nfsd_net
*nn
= net_generic(SVC_NET(rqstp
), nfsd_net_id
);
2717 status
= nfsd4_check_cb_sec(&bc
->bc_cb_sec
);
2720 spin_lock(&nn
->client_lock
);
2721 session
->se_cb_prog
= bc
->bc_cb_program
;
2722 session
->se_cb_sec
= bc
->bc_cb_sec
;
2723 spin_unlock(&nn
->client_lock
);
2725 nfsd4_probe_callback(session
->se_client
);
2730 __be32
nfsd4_bind_conn_to_session(struct svc_rqst
*rqstp
,
2731 struct nfsd4_compound_state
*cstate
,
2732 struct nfsd4_bind_conn_to_session
*bcts
)
2735 struct nfsd4_conn
*conn
;
2736 struct nfsd4_session
*session
;
2737 struct net
*net
= SVC_NET(rqstp
);
2738 struct nfsd_net
*nn
= net_generic(net
, nfsd_net_id
);
2740 if (!nfsd4_last_compound_op(rqstp
))
2741 return nfserr_not_only_op
;
2742 spin_lock(&nn
->client_lock
);
2743 session
= find_in_sessionid_hashtbl(&bcts
->sessionid
, net
, &status
);
2744 spin_unlock(&nn
->client_lock
);
2746 goto out_no_session
;
2747 status
= nfserr_wrong_cred
;
2748 if (!mach_creds_match(session
->se_client
, rqstp
))
2750 status
= nfsd4_map_bcts_dir(&bcts
->dir
);
2753 conn
= alloc_conn(rqstp
, bcts
->dir
);
2754 status
= nfserr_jukebox
;
2757 nfsd4_init_conn(rqstp
, conn
, session
);
2760 nfsd4_put_session(session
);
2765 static bool nfsd4_compound_in_session(struct nfsd4_session
*session
, struct nfs4_sessionid
*sid
)
2769 return !memcmp(sid
, &session
->se_sessionid
, sizeof(*sid
));
2773 nfsd4_destroy_session(struct svc_rqst
*r
,
2774 struct nfsd4_compound_state
*cstate
,
2775 struct nfsd4_destroy_session
*sessionid
)
2777 struct nfsd4_session
*ses
;
2779 int ref_held_by_me
= 0;
2780 struct net
*net
= SVC_NET(r
);
2781 struct nfsd_net
*nn
= net_generic(net
, nfsd_net_id
);
2783 status
= nfserr_not_only_op
;
2784 if (nfsd4_compound_in_session(cstate
->session
, &sessionid
->sessionid
)) {
2785 if (!nfsd4_last_compound_op(r
))
2789 dump_sessionid(__func__
, &sessionid
->sessionid
);
2790 spin_lock(&nn
->client_lock
);
2791 ses
= find_in_sessionid_hashtbl(&sessionid
->sessionid
, net
, &status
);
2793 goto out_client_lock
;
2794 status
= nfserr_wrong_cred
;
2795 if (!mach_creds_match(ses
->se_client
, r
))
2796 goto out_put_session
;
2797 status
= mark_session_dead_locked(ses
, 1 + ref_held_by_me
);
2799 goto out_put_session
;
2800 unhash_session(ses
);
2801 spin_unlock(&nn
->client_lock
);
2803 nfsd4_probe_callback_sync(ses
->se_client
);
2805 spin_lock(&nn
->client_lock
);
2808 nfsd4_put_session_locked(ses
);
2810 spin_unlock(&nn
->client_lock
);
2815 static struct nfsd4_conn
*__nfsd4_find_conn(struct svc_xprt
*xpt
, struct nfsd4_session
*s
)
2817 struct nfsd4_conn
*c
;
2819 list_for_each_entry(c
, &s
->se_conns
, cn_persession
) {
2820 if (c
->cn_xprt
== xpt
) {
2827 static __be32
nfsd4_sequence_check_conn(struct nfsd4_conn
*new, struct nfsd4_session
*ses
)
2829 struct nfs4_client
*clp
= ses
->se_client
;
2830 struct nfsd4_conn
*c
;
2831 __be32 status
= nfs_ok
;
2834 spin_lock(&clp
->cl_lock
);
2835 c
= __nfsd4_find_conn(new->cn_xprt
, ses
);
2838 status
= nfserr_conn_not_bound_to_session
;
2839 if (clp
->cl_mach_cred
)
2841 __nfsd4_hash_conn(new, ses
);
2842 spin_unlock(&clp
->cl_lock
);
2843 ret
= nfsd4_register_conn(new);
2845 /* oops; xprt is already down: */
2846 nfsd4_conn_lost(&new->cn_xpt_user
);
2849 spin_unlock(&clp
->cl_lock
);
2854 static bool nfsd4_session_too_many_ops(struct svc_rqst
*rqstp
, struct nfsd4_session
*session
)
2856 struct nfsd4_compoundargs
*args
= rqstp
->rq_argp
;
2858 return args
->opcnt
> session
->se_fchannel
.maxops
;
2861 static bool nfsd4_request_too_big(struct svc_rqst
*rqstp
,
2862 struct nfsd4_session
*session
)
2864 struct xdr_buf
*xb
= &rqstp
->rq_arg
;
2866 return xb
->len
> session
->se_fchannel
.maxreq_sz
;
2870 nfsd4_sequence(struct svc_rqst
*rqstp
,
2871 struct nfsd4_compound_state
*cstate
,
2872 struct nfsd4_sequence
*seq
)
2874 struct nfsd4_compoundres
*resp
= rqstp
->rq_resp
;
2875 struct xdr_stream
*xdr
= &resp
->xdr
;
2876 struct nfsd4_session
*session
;
2877 struct nfs4_client
*clp
;
2878 struct nfsd4_slot
*slot
;
2879 struct nfsd4_conn
*conn
;
2882 struct net
*net
= SVC_NET(rqstp
);
2883 struct nfsd_net
*nn
= net_generic(net
, nfsd_net_id
);
2885 if (resp
->opcnt
!= 1)
2886 return nfserr_sequence_pos
;
2889 * Will be either used or freed by nfsd4_sequence_check_conn
2892 conn
= alloc_conn(rqstp
, NFS4_CDFC4_FORE
);
2894 return nfserr_jukebox
;
2896 spin_lock(&nn
->client_lock
);
2897 session
= find_in_sessionid_hashtbl(&seq
->sessionid
, net
, &status
);
2899 goto out_no_session
;
2900 clp
= session
->se_client
;
2902 status
= nfserr_too_many_ops
;
2903 if (nfsd4_session_too_many_ops(rqstp
, session
))
2904 goto out_put_session
;
2906 status
= nfserr_req_too_big
;
2907 if (nfsd4_request_too_big(rqstp
, session
))
2908 goto out_put_session
;
2910 status
= nfserr_badslot
;
2911 if (seq
->slotid
>= session
->se_fchannel
.maxreqs
)
2912 goto out_put_session
;
2914 slot
= session
->se_slots
[seq
->slotid
];
2915 dprintk("%s: slotid %d\n", __func__
, seq
->slotid
);
2917 /* We do not negotiate the number of slots yet, so set the
2918 * maxslots to the session maxreqs which is used to encode
2919 * sr_highest_slotid and the sr_target_slot id to maxslots */
2920 seq
->maxslots
= session
->se_fchannel
.maxreqs
;
2922 status
= check_slot_seqid(seq
->seqid
, slot
->sl_seqid
,
2923 slot
->sl_flags
& NFSD4_SLOT_INUSE
);
2924 if (status
== nfserr_replay_cache
) {
2925 status
= nfserr_seq_misordered
;
2926 if (!(slot
->sl_flags
& NFSD4_SLOT_INITIALIZED
))
2927 goto out_put_session
;
2928 cstate
->slot
= slot
;
2929 cstate
->session
= session
;
2931 /* Return the cached reply status and set cstate->status
2932 * for nfsd4_proc_compound processing */
2933 status
= nfsd4_replay_cache_entry(resp
, seq
);
2934 cstate
->status
= nfserr_replay_cache
;
2938 goto out_put_session
;
2940 status
= nfsd4_sequence_check_conn(conn
, session
);
2943 goto out_put_session
;
2945 buflen
= (seq
->cachethis
) ?
2946 session
->se_fchannel
.maxresp_cached
:
2947 session
->se_fchannel
.maxresp_sz
;
2948 status
= (seq
->cachethis
) ? nfserr_rep_too_big_to_cache
:
2950 if (xdr_restrict_buflen(xdr
, buflen
- rqstp
->rq_auth_slack
))
2951 goto out_put_session
;
2952 svc_reserve(rqstp
, buflen
);
2955 /* Success! bump slot seqid */
2956 slot
->sl_seqid
= seq
->seqid
;
2957 slot
->sl_flags
|= NFSD4_SLOT_INUSE
;
2959 slot
->sl_flags
|= NFSD4_SLOT_CACHETHIS
;
2961 slot
->sl_flags
&= ~NFSD4_SLOT_CACHETHIS
;
2963 cstate
->slot
= slot
;
2964 cstate
->session
= session
;
2968 switch (clp
->cl_cb_state
) {
2970 seq
->status_flags
= SEQ4_STATUS_CB_PATH_DOWN
;
2972 case NFSD4_CB_FAULT
:
2973 seq
->status_flags
= SEQ4_STATUS_BACKCHANNEL_FAULT
;
2976 seq
->status_flags
= 0;
2978 if (!list_empty(&clp
->cl_revoked
))
2979 seq
->status_flags
|= SEQ4_STATUS_RECALLABLE_STATE_REVOKED
;
2983 spin_unlock(&nn
->client_lock
);
2986 nfsd4_put_session_locked(session
);
2987 goto out_no_session
;
2991 nfsd4_sequence_done(struct nfsd4_compoundres
*resp
)
2993 struct nfsd4_compound_state
*cs
= &resp
->cstate
;
2995 if (nfsd4_has_session(cs
)) {
2996 if (cs
->status
!= nfserr_replay_cache
) {
2997 nfsd4_store_cache_entry(resp
);
2998 cs
->slot
->sl_flags
&= ~NFSD4_SLOT_INUSE
;
3000 /* Drop session reference that was taken in nfsd4_sequence() */
3001 nfsd4_put_session(cs
->session
);
3003 put_client_renew(cs
->clp
);
3007 nfsd4_destroy_clientid(struct svc_rqst
*rqstp
, struct nfsd4_compound_state
*cstate
, struct nfsd4_destroy_clientid
*dc
)
3009 struct nfs4_client
*conf
, *unconf
;
3010 struct nfs4_client
*clp
= NULL
;
3012 struct nfsd_net
*nn
= net_generic(SVC_NET(rqstp
), nfsd_net_id
);
3014 spin_lock(&nn
->client_lock
);
3015 unconf
= find_unconfirmed_client(&dc
->clientid
, true, nn
);
3016 conf
= find_confirmed_client(&dc
->clientid
, true, nn
);
3017 WARN_ON_ONCE(conf
&& unconf
);
3020 if (client_has_state(conf
)) {
3021 status
= nfserr_clientid_busy
;
3024 status
= mark_client_expired_locked(conf
);
3031 status
= nfserr_stale_clientid
;
3034 if (!mach_creds_match(clp
, rqstp
)) {
3036 status
= nfserr_wrong_cred
;
3039 unhash_client_locked(clp
);
3041 spin_unlock(&nn
->client_lock
);
3048 nfsd4_reclaim_complete(struct svc_rqst
*rqstp
, struct nfsd4_compound_state
*cstate
, struct nfsd4_reclaim_complete
*rc
)
3052 if (rc
->rca_one_fs
) {
3053 if (!cstate
->current_fh
.fh_dentry
)
3054 return nfserr_nofilehandle
;
3056 * We don't take advantage of the rca_one_fs case.
3057 * That's OK, it's optional, we can safely ignore it.
3062 status
= nfserr_complete_already
;
3063 if (test_and_set_bit(NFSD4_CLIENT_RECLAIM_COMPLETE
,
3064 &cstate
->session
->se_client
->cl_flags
))
3067 status
= nfserr_stale_clientid
;
3068 if (is_client_expired(cstate
->session
->se_client
))
3070 * The following error isn't really legal.
3071 * But we only get here if the client just explicitly
3072 * destroyed the client. Surely it no longer cares what
3073 * error it gets back on an operation for the dead
3079 nfsd4_client_record_create(cstate
->session
->se_client
);
3085 nfsd4_setclientid(struct svc_rqst
*rqstp
, struct nfsd4_compound_state
*cstate
,
3086 struct nfsd4_setclientid
*setclid
)
3088 struct xdr_netobj clname
= setclid
->se_name
;
3089 nfs4_verifier clverifier
= setclid
->se_verf
;
3090 struct nfs4_client
*conf
, *new;
3091 struct nfs4_client
*unconf
= NULL
;
3093 struct nfsd_net
*nn
= net_generic(SVC_NET(rqstp
), nfsd_net_id
);
3095 new = create_client(clname
, rqstp
, &clverifier
);
3097 return nfserr_jukebox
;
3098 /* Cases below refer to rfc 3530 section 14.2.33: */
3099 spin_lock(&nn
->client_lock
);
3100 conf
= find_confirmed_client_by_name(&clname
, nn
);
3101 if (conf
&& client_has_state(conf
)) {
3103 status
= nfserr_clid_inuse
;
3104 if (clp_used_exchangeid(conf
))
3106 if (!same_creds(&conf
->cl_cred
, &rqstp
->rq_cred
)) {
3107 char addr_str
[INET6_ADDRSTRLEN
];
3108 rpc_ntop((struct sockaddr
*) &conf
->cl_addr
, addr_str
,
3110 dprintk("NFSD: setclientid: string in use by client "
3111 "at %s\n", addr_str
);
3115 unconf
= find_unconfirmed_client_by_name(&clname
, nn
);
3117 unhash_client_locked(unconf
);
3118 if (conf
&& same_verf(&conf
->cl_verifier
, &clverifier
)) {
3119 /* case 1: probable callback update */
3120 copy_clid(new, conf
);
3121 gen_confirm(new, nn
);
3122 } else /* case 4 (new client) or cases 2, 3 (client reboot): */
3124 new->cl_minorversion
= 0;
3125 gen_callback(new, setclid
, rqstp
);
3126 add_to_unconfirmed(new);
3127 setclid
->se_clientid
.cl_boot
= new->cl_clientid
.cl_boot
;
3128 setclid
->se_clientid
.cl_id
= new->cl_clientid
.cl_id
;
3129 memcpy(setclid
->se_confirm
.data
, new->cl_confirm
.data
, sizeof(setclid
->se_confirm
.data
));
3133 spin_unlock(&nn
->client_lock
);
3137 expire_client(unconf
);
3143 nfsd4_setclientid_confirm(struct svc_rqst
*rqstp
,
3144 struct nfsd4_compound_state
*cstate
,
3145 struct nfsd4_setclientid_confirm
*setclientid_confirm
)
3147 struct nfs4_client
*conf
, *unconf
;
3148 struct nfs4_client
*old
= NULL
;
3149 nfs4_verifier confirm
= setclientid_confirm
->sc_confirm
;
3150 clientid_t
* clid
= &setclientid_confirm
->sc_clientid
;
3152 struct nfsd_net
*nn
= net_generic(SVC_NET(rqstp
), nfsd_net_id
);
3154 if (STALE_CLIENTID(clid
, nn
))
3155 return nfserr_stale_clientid
;
3157 spin_lock(&nn
->client_lock
);
3158 conf
= find_confirmed_client(clid
, false, nn
);
3159 unconf
= find_unconfirmed_client(clid
, false, nn
);
3161 * We try hard to give out unique clientid's, so if we get an
3162 * attempt to confirm the same clientid with a different cred,
3163 * the client may be buggy; this should never happen.
3165 * Nevertheless, RFC 7530 recommends INUSE for this case:
3167 status
= nfserr_clid_inuse
;
3168 if (unconf
&& !same_creds(&unconf
->cl_cred
, &rqstp
->rq_cred
))
3170 if (conf
&& !same_creds(&conf
->cl_cred
, &rqstp
->rq_cred
))
3172 /* cases below refer to rfc 3530 section 14.2.34: */
3173 if (!unconf
|| !same_verf(&confirm
, &unconf
->cl_confirm
)) {
3174 if (conf
&& !unconf
) /* case 2: probable retransmit */
3176 else /* case 4: client hasn't noticed we rebooted yet? */
3177 status
= nfserr_stale_clientid
;
3181 if (conf
) { /* case 1: callback update */
3183 unhash_client_locked(old
);
3184 nfsd4_change_callback(conf
, &unconf
->cl_cb_conn
);
3185 } else { /* case 3: normal case; new or rebooted client */
3186 old
= find_confirmed_client_by_name(&unconf
->cl_name
, nn
);
3188 status
= nfserr_clid_inuse
;
3189 if (client_has_state(old
)
3190 && !same_creds(&unconf
->cl_cred
,
3193 status
= mark_client_expired_locked(old
);
3199 move_to_confirmed(unconf
);
3202 get_client_locked(conf
);
3203 spin_unlock(&nn
->client_lock
);
3204 nfsd4_probe_callback(conf
);
3205 spin_lock(&nn
->client_lock
);
3206 put_client_renew_locked(conf
);
3208 spin_unlock(&nn
->client_lock
);
3214 static struct nfs4_file
*nfsd4_alloc_file(void)
3216 return kmem_cache_alloc(file_slab
, GFP_KERNEL
);
3219 /* OPEN Share state helper functions */
3220 static void nfsd4_init_file(struct knfsd_fh
*fh
, unsigned int hashval
,
3221 struct nfs4_file
*fp
)
3223 lockdep_assert_held(&state_lock
);
3225 atomic_set(&fp
->fi_ref
, 1);
3226 spin_lock_init(&fp
->fi_lock
);
3227 INIT_LIST_HEAD(&fp
->fi_stateids
);
3228 INIT_LIST_HEAD(&fp
->fi_delegations
);
3229 INIT_LIST_HEAD(&fp
->fi_clnt_odstate
);
3230 fh_copy_shallow(&fp
->fi_fhandle
, fh
);
3231 fp
->fi_deleg_file
= NULL
;
3232 fp
->fi_had_conflict
= false;
3233 fp
->fi_share_deny
= 0;
3234 memset(fp
->fi_fds
, 0, sizeof(fp
->fi_fds
));
3235 memset(fp
->fi_access
, 0, sizeof(fp
->fi_access
));
3236 #ifdef CONFIG_NFSD_PNFS
3237 INIT_LIST_HEAD(&fp
->fi_lo_states
);
3238 atomic_set(&fp
->fi_lo_recalls
, 0);
3240 hlist_add_head_rcu(&fp
->fi_hash
, &file_hashtbl
[hashval
]);
3244 nfsd4_free_slabs(void)
3246 kmem_cache_destroy(odstate_slab
);
3247 kmem_cache_destroy(openowner_slab
);
3248 kmem_cache_destroy(lockowner_slab
);
3249 kmem_cache_destroy(file_slab
);
3250 kmem_cache_destroy(stateid_slab
);
3251 kmem_cache_destroy(deleg_slab
);
3255 nfsd4_init_slabs(void)
3257 openowner_slab
= kmem_cache_create("nfsd4_openowners",
3258 sizeof(struct nfs4_openowner
), 0, 0, NULL
);
3259 if (openowner_slab
== NULL
)
3261 lockowner_slab
= kmem_cache_create("nfsd4_lockowners",
3262 sizeof(struct nfs4_lockowner
), 0, 0, NULL
);
3263 if (lockowner_slab
== NULL
)
3264 goto out_free_openowner_slab
;
3265 file_slab
= kmem_cache_create("nfsd4_files",
3266 sizeof(struct nfs4_file
), 0, 0, NULL
);
3267 if (file_slab
== NULL
)
3268 goto out_free_lockowner_slab
;
3269 stateid_slab
= kmem_cache_create("nfsd4_stateids",
3270 sizeof(struct nfs4_ol_stateid
), 0, 0, NULL
);
3271 if (stateid_slab
== NULL
)
3272 goto out_free_file_slab
;
3273 deleg_slab
= kmem_cache_create("nfsd4_delegations",
3274 sizeof(struct nfs4_delegation
), 0, 0, NULL
);
3275 if (deleg_slab
== NULL
)
3276 goto out_free_stateid_slab
;
3277 odstate_slab
= kmem_cache_create("nfsd4_odstate",
3278 sizeof(struct nfs4_clnt_odstate
), 0, 0, NULL
);
3279 if (odstate_slab
== NULL
)
3280 goto out_free_deleg_slab
;
3283 out_free_deleg_slab
:
3284 kmem_cache_destroy(deleg_slab
);
3285 out_free_stateid_slab
:
3286 kmem_cache_destroy(stateid_slab
);
3288 kmem_cache_destroy(file_slab
);
3289 out_free_lockowner_slab
:
3290 kmem_cache_destroy(lockowner_slab
);
3291 out_free_openowner_slab
:
3292 kmem_cache_destroy(openowner_slab
);
3294 dprintk("nfsd4: out of memory while initializing nfsv4\n");
3298 static void init_nfs4_replay(struct nfs4_replay
*rp
)
3300 rp
->rp_status
= nfserr_serverfault
;
3302 rp
->rp_buf
= rp
->rp_ibuf
;
3303 mutex_init(&rp
->rp_mutex
);
3306 static void nfsd4_cstate_assign_replay(struct nfsd4_compound_state
*cstate
,
3307 struct nfs4_stateowner
*so
)
3309 if (!nfsd4_has_session(cstate
)) {
3310 mutex_lock(&so
->so_replay
.rp_mutex
);
3311 cstate
->replay_owner
= nfs4_get_stateowner(so
);
3315 void nfsd4_cstate_clear_replay(struct nfsd4_compound_state
*cstate
)
3317 struct nfs4_stateowner
*so
= cstate
->replay_owner
;
3320 cstate
->replay_owner
= NULL
;
3321 mutex_unlock(&so
->so_replay
.rp_mutex
);
3322 nfs4_put_stateowner(so
);
3326 static inline void *alloc_stateowner(struct kmem_cache
*slab
, struct xdr_netobj
*owner
, struct nfs4_client
*clp
)
3328 struct nfs4_stateowner
*sop
;
3330 sop
= kmem_cache_alloc(slab
, GFP_KERNEL
);
3334 sop
->so_owner
.data
= kmemdup(owner
->data
, owner
->len
, GFP_KERNEL
);
3335 if (!sop
->so_owner
.data
) {
3336 kmem_cache_free(slab
, sop
);
3339 sop
->so_owner
.len
= owner
->len
;
3341 INIT_LIST_HEAD(&sop
->so_stateids
);
3342 sop
->so_client
= clp
;
3343 init_nfs4_replay(&sop
->so_replay
);
3344 atomic_set(&sop
->so_count
, 1);
3348 static void hash_openowner(struct nfs4_openowner
*oo
, struct nfs4_client
*clp
, unsigned int strhashval
)
3350 lockdep_assert_held(&clp
->cl_lock
);
3352 list_add(&oo
->oo_owner
.so_strhash
,
3353 &clp
->cl_ownerstr_hashtbl
[strhashval
]);
3354 list_add(&oo
->oo_perclient
, &clp
->cl_openowners
);
3357 static void nfs4_unhash_openowner(struct nfs4_stateowner
*so
)
3359 unhash_openowner_locked(openowner(so
));
3362 static void nfs4_free_openowner(struct nfs4_stateowner
*so
)
3364 struct nfs4_openowner
*oo
= openowner(so
);
3366 kmem_cache_free(openowner_slab
, oo
);
3369 static const struct nfs4_stateowner_operations openowner_ops
= {
3370 .so_unhash
= nfs4_unhash_openowner
,
3371 .so_free
= nfs4_free_openowner
,
3374 static struct nfs4_ol_stateid
*
3375 nfsd4_find_existing_open(struct nfs4_file
*fp
, struct nfsd4_open
*open
)
3377 struct nfs4_ol_stateid
*local
, *ret
= NULL
;
3378 struct nfs4_openowner
*oo
= open
->op_openowner
;
3380 lockdep_assert_held(&fp
->fi_lock
);
3382 list_for_each_entry(local
, &fp
->fi_stateids
, st_perfile
) {
3383 /* ignore lock owners */
3384 if (local
->st_stateowner
->so_is_open_owner
== 0)
3386 if (local
->st_stateowner
!= &oo
->oo_owner
)
3388 if (local
->st_stid
.sc_type
== NFS4_OPEN_STID
) {
3390 atomic_inc(&ret
->st_stid
.sc_count
);
3398 nfsd4_verify_open_stid(struct nfs4_stid
*s
)
3400 __be32 ret
= nfs_ok
;
3402 switch (s
->sc_type
) {
3405 case NFS4_CLOSED_STID
:
3406 case NFS4_CLOSED_DELEG_STID
:
3407 ret
= nfserr_bad_stateid
;
3409 case NFS4_REVOKED_DELEG_STID
:
3410 ret
= nfserr_deleg_revoked
;
3415 /* Lock the stateid st_mutex, and deal with races with CLOSE */
3417 nfsd4_lock_ol_stateid(struct nfs4_ol_stateid
*stp
)
3421 mutex_lock(&stp
->st_mutex
);
3422 ret
= nfsd4_verify_open_stid(&stp
->st_stid
);
3424 mutex_unlock(&stp
->st_mutex
);
3428 static struct nfs4_ol_stateid
*
3429 nfsd4_find_and_lock_existing_open(struct nfs4_file
*fp
, struct nfsd4_open
*open
)
3431 struct nfs4_ol_stateid
*stp
;
3433 spin_lock(&fp
->fi_lock
);
3434 stp
= nfsd4_find_existing_open(fp
, open
);
3435 spin_unlock(&fp
->fi_lock
);
3436 if (!stp
|| nfsd4_lock_ol_stateid(stp
) == nfs_ok
)
3438 nfs4_put_stid(&stp
->st_stid
);
3443 static struct nfs4_openowner
*
3444 alloc_init_open_stateowner(unsigned int strhashval
, struct nfsd4_open
*open
,
3445 struct nfsd4_compound_state
*cstate
)
3447 struct nfs4_client
*clp
= cstate
->clp
;
3448 struct nfs4_openowner
*oo
, *ret
;
3450 oo
= alloc_stateowner(openowner_slab
, &open
->op_owner
, clp
);
3453 oo
->oo_owner
.so_ops
= &openowner_ops
;
3454 oo
->oo_owner
.so_is_open_owner
= 1;
3455 oo
->oo_owner
.so_seqid
= open
->op_seqid
;
3457 if (nfsd4_has_session(cstate
))
3458 oo
->oo_flags
|= NFS4_OO_CONFIRMED
;
3460 oo
->oo_last_closed_stid
= NULL
;
3461 INIT_LIST_HEAD(&oo
->oo_close_lru
);
3462 spin_lock(&clp
->cl_lock
);
3463 ret
= find_openstateowner_str_locked(strhashval
, open
, clp
);
3465 hash_openowner(oo
, clp
, strhashval
);
3468 nfs4_free_stateowner(&oo
->oo_owner
);
3470 spin_unlock(&clp
->cl_lock
);
3474 static struct nfs4_ol_stateid
*
3475 init_open_stateid(struct nfs4_file
*fp
, struct nfsd4_open
*open
)
3478 struct nfs4_openowner
*oo
= open
->op_openowner
;
3479 struct nfs4_ol_stateid
*retstp
= NULL
;
3480 struct nfs4_ol_stateid
*stp
;
3483 /* We are moving these outside of the spinlocks to avoid the warnings */
3484 mutex_init(&stp
->st_mutex
);
3485 mutex_lock(&stp
->st_mutex
);
3488 spin_lock(&oo
->oo_owner
.so_client
->cl_lock
);
3489 spin_lock(&fp
->fi_lock
);
3491 retstp
= nfsd4_find_existing_open(fp
, open
);
3495 open
->op_stp
= NULL
;
3496 atomic_inc(&stp
->st_stid
.sc_count
);
3497 stp
->st_stid
.sc_type
= NFS4_OPEN_STID
;
3498 INIT_LIST_HEAD(&stp
->st_locks
);
3499 stp
->st_stateowner
= nfs4_get_stateowner(&oo
->oo_owner
);
3501 stp
->st_stid
.sc_file
= fp
;
3502 stp
->st_access_bmap
= 0;
3503 stp
->st_deny_bmap
= 0;
3504 stp
->st_openstp
= NULL
;
3505 list_add(&stp
->st_perstateowner
, &oo
->oo_owner
.so_stateids
);
3506 list_add(&stp
->st_perfile
, &fp
->fi_stateids
);
3509 spin_unlock(&fp
->fi_lock
);
3510 spin_unlock(&oo
->oo_owner
.so_client
->cl_lock
);
3512 /* Handle races with CLOSE */
3513 if (nfsd4_lock_ol_stateid(retstp
) != nfs_ok
) {
3514 nfs4_put_stid(&retstp
->st_stid
);
3517 /* To keep mutex tracking happy */
3518 mutex_unlock(&stp
->st_mutex
);
3525 * In the 4.0 case we need to keep the owners around a little while to handle
3526 * CLOSE replay. We still do need to release any file access that is held by
3527 * them before returning however.
3530 move_to_close_lru(struct nfs4_ol_stateid
*s
, struct net
*net
)
3532 struct nfs4_ol_stateid
*last
;
3533 struct nfs4_openowner
*oo
= openowner(s
->st_stateowner
);
3534 struct nfsd_net
*nn
= net_generic(s
->st_stid
.sc_client
->net
,
3537 dprintk("NFSD: move_to_close_lru nfs4_openowner %p\n", oo
);
3540 * We know that we hold one reference via nfsd4_close, and another
3541 * "persistent" reference for the client. If the refcount is higher
3542 * than 2, then there are still calls in progress that are using this
3543 * stateid. We can't put the sc_file reference until they are finished.
3544 * Wait for the refcount to drop to 2. Since it has been unhashed,
3545 * there should be no danger of the refcount going back up again at
3548 wait_event(close_wq
, atomic_read(&s
->st_stid
.sc_count
) == 2);
3550 release_all_access(s
);
3551 if (s
->st_stid
.sc_file
) {
3552 put_nfs4_file(s
->st_stid
.sc_file
);
3553 s
->st_stid
.sc_file
= NULL
;
3556 spin_lock(&nn
->client_lock
);
3557 last
= oo
->oo_last_closed_stid
;
3558 oo
->oo_last_closed_stid
= s
;
3559 list_move_tail(&oo
->oo_close_lru
, &nn
->close_lru
);
3560 oo
->oo_time
= get_seconds();
3561 spin_unlock(&nn
->client_lock
);
3563 nfs4_put_stid(&last
->st_stid
);
3566 /* search file_hashtbl[] for file */
3567 static struct nfs4_file
*
3568 find_file_locked(struct knfsd_fh
*fh
, unsigned int hashval
)
3570 struct nfs4_file
*fp
;
3572 hlist_for_each_entry_rcu(fp
, &file_hashtbl
[hashval
], fi_hash
) {
3573 if (fh_match(&fp
->fi_fhandle
, fh
)) {
3574 if (atomic_inc_not_zero(&fp
->fi_ref
))
3582 find_file(struct knfsd_fh
*fh
)
3584 struct nfs4_file
*fp
;
3585 unsigned int hashval
= file_hashval(fh
);
3588 fp
= find_file_locked(fh
, hashval
);
3593 static struct nfs4_file
*
3594 find_or_add_file(struct nfs4_file
*new, struct knfsd_fh
*fh
)
3596 struct nfs4_file
*fp
;
3597 unsigned int hashval
= file_hashval(fh
);
3600 fp
= find_file_locked(fh
, hashval
);
3605 spin_lock(&state_lock
);
3606 fp
= find_file_locked(fh
, hashval
);
3607 if (likely(fp
== NULL
)) {
3608 nfsd4_init_file(fh
, hashval
, new);
3611 spin_unlock(&state_lock
);
3617 * Called to check deny when READ with all zero stateid or
3618 * WRITE with all zero or all one stateid
3621 nfs4_share_conflict(struct svc_fh
*current_fh
, unsigned int deny_type
)
3623 struct nfs4_file
*fp
;
3624 __be32 ret
= nfs_ok
;
3626 fp
= find_file(¤t_fh
->fh_handle
);
3629 /* Check for conflicting share reservations */
3630 spin_lock(&fp
->fi_lock
);
3631 if (fp
->fi_share_deny
& deny_type
)
3632 ret
= nfserr_locked
;
3633 spin_unlock(&fp
->fi_lock
);
3638 static void nfsd4_cb_recall_prepare(struct nfsd4_callback
*cb
)
3640 struct nfs4_delegation
*dp
= cb_to_delegation(cb
);
3641 struct nfsd_net
*nn
= net_generic(dp
->dl_stid
.sc_client
->net
,
3644 block_delegations(&dp
->dl_stid
.sc_file
->fi_fhandle
);
3647 * We can't do this in nfsd_break_deleg_cb because it is
3648 * already holding inode->i_lock.
3650 * If the dl_time != 0, then we know that it has already been
3651 * queued for a lease break. Don't queue it again.
3653 spin_lock(&state_lock
);
3654 if (dp
->dl_time
== 0) {
3655 dp
->dl_time
= get_seconds();
3656 list_add_tail(&dp
->dl_recall_lru
, &nn
->del_recall_lru
);
3658 spin_unlock(&state_lock
);
3661 static int nfsd4_cb_recall_done(struct nfsd4_callback
*cb
,
3662 struct rpc_task
*task
)
3664 struct nfs4_delegation
*dp
= cb_to_delegation(cb
);
3666 if (dp
->dl_stid
.sc_type
== NFS4_CLOSED_DELEG_STID
)
3669 switch (task
->tk_status
) {
3673 case -NFS4ERR_BAD_STATEID
:
3675 * Race: client probably got cb_recall before open reply
3676 * granting delegation.
3678 if (dp
->dl_retries
--) {
3679 rpc_delay(task
, 2 * HZ
);
3688 static void nfsd4_cb_recall_release(struct nfsd4_callback
*cb
)
3690 struct nfs4_delegation
*dp
= cb_to_delegation(cb
);
3692 nfs4_put_stid(&dp
->dl_stid
);
3695 static struct nfsd4_callback_ops nfsd4_cb_recall_ops
= {
3696 .prepare
= nfsd4_cb_recall_prepare
,
3697 .done
= nfsd4_cb_recall_done
,
3698 .release
= nfsd4_cb_recall_release
,
3701 static void nfsd_break_one_deleg(struct nfs4_delegation
*dp
)
3704 * We're assuming the state code never drops its reference
3705 * without first removing the lease. Since we're in this lease
3706 * callback (and since the lease code is serialized by the kernel
3707 * lock) we know the server hasn't removed the lease yet, we know
3708 * it's safe to take a reference.
3710 atomic_inc(&dp
->dl_stid
.sc_count
);
3711 nfsd4_run_cb(&dp
->dl_recall
);
3714 /* Called from break_lease() with i_lock held. */
3716 nfsd_break_deleg_cb(struct file_lock
*fl
)
3719 struct nfs4_file
*fp
= (struct nfs4_file
*)fl
->fl_owner
;
3720 struct nfs4_delegation
*dp
;
3723 WARN(1, "(%p)->fl_owner NULL\n", fl
);
3726 if (fp
->fi_had_conflict
) {
3727 WARN(1, "duplicate break on %p\n", fp
);
3731 * We don't want the locks code to timeout the lease for us;
3732 * we'll remove it ourself if a delegation isn't returned
3735 fl
->fl_break_time
= 0;
3737 spin_lock(&fp
->fi_lock
);
3738 fp
->fi_had_conflict
= true;
3740 * If there are no delegations on the list, then return true
3741 * so that the lease code will go ahead and delete it.
3743 if (list_empty(&fp
->fi_delegations
))
3746 list_for_each_entry(dp
, &fp
->fi_delegations
, dl_perfile
)
3747 nfsd_break_one_deleg(dp
);
3748 spin_unlock(&fp
->fi_lock
);
3753 nfsd_change_deleg_cb(struct file_lock
*onlist
, int arg
,
3754 struct list_head
*dispose
)
3757 return lease_modify(onlist
, arg
, dispose
);
3762 static const struct lock_manager_operations nfsd_lease_mng_ops
= {
3763 .lm_break
= nfsd_break_deleg_cb
,
3764 .lm_change
= nfsd_change_deleg_cb
,
3767 static __be32
nfsd4_check_seqid(struct nfsd4_compound_state
*cstate
, struct nfs4_stateowner
*so
, u32 seqid
)
3769 if (nfsd4_has_session(cstate
))
3771 if (seqid
== so
->so_seqid
- 1)
3772 return nfserr_replay_me
;
3773 if (seqid
== so
->so_seqid
)
3775 return nfserr_bad_seqid
;
3778 static __be32
lookup_clientid(clientid_t
*clid
,
3779 struct nfsd4_compound_state
*cstate
,
3780 struct nfsd_net
*nn
)
3782 struct nfs4_client
*found
;
3785 found
= cstate
->clp
;
3786 if (!same_clid(&found
->cl_clientid
, clid
))
3787 return nfserr_stale_clientid
;
3791 if (STALE_CLIENTID(clid
, nn
))
3792 return nfserr_stale_clientid
;
3795 * For v4.1+ we get the client in the SEQUENCE op. If we don't have one
3796 * cached already then we know this is for is for v4.0 and "sessions"
3799 WARN_ON_ONCE(cstate
->session
);
3800 spin_lock(&nn
->client_lock
);
3801 found
= find_confirmed_client(clid
, false, nn
);
3803 spin_unlock(&nn
->client_lock
);
3804 return nfserr_expired
;
3806 atomic_inc(&found
->cl_refcount
);
3807 spin_unlock(&nn
->client_lock
);
3809 /* Cache the nfs4_client in cstate! */
3810 cstate
->clp
= found
;
3815 nfsd4_process_open1(struct nfsd4_compound_state
*cstate
,
3816 struct nfsd4_open
*open
, struct nfsd_net
*nn
)
3818 clientid_t
*clientid
= &open
->op_clientid
;
3819 struct nfs4_client
*clp
= NULL
;
3820 unsigned int strhashval
;
3821 struct nfs4_openowner
*oo
= NULL
;
3824 if (STALE_CLIENTID(&open
->op_clientid
, nn
))
3825 return nfserr_stale_clientid
;
3827 * In case we need it later, after we've already created the
3828 * file and don't want to risk a further failure:
3830 open
->op_file
= nfsd4_alloc_file();
3831 if (open
->op_file
== NULL
)
3832 return nfserr_jukebox
;
3834 status
= lookup_clientid(clientid
, cstate
, nn
);
3839 strhashval
= ownerstr_hashval(&open
->op_owner
);
3840 oo
= find_openstateowner_str(strhashval
, open
, clp
);
3841 open
->op_openowner
= oo
;
3845 if (!(oo
->oo_flags
& NFS4_OO_CONFIRMED
)) {
3846 /* Replace unconfirmed owners without checking for replay. */
3847 release_openowner(oo
);
3848 open
->op_openowner
= NULL
;
3851 status
= nfsd4_check_seqid(cstate
, &oo
->oo_owner
, open
->op_seqid
);
3856 oo
= alloc_init_open_stateowner(strhashval
, open
, cstate
);
3858 return nfserr_jukebox
;
3859 open
->op_openowner
= oo
;
3861 open
->op_stp
= nfs4_alloc_open_stateid(clp
);
3863 return nfserr_jukebox
;
3865 if (nfsd4_has_session(cstate
) &&
3866 (cstate
->current_fh
.fh_export
->ex_flags
& NFSEXP_PNFS
)) {
3867 open
->op_odstate
= alloc_clnt_odstate(clp
);
3868 if (!open
->op_odstate
)
3869 return nfserr_jukebox
;
3875 static inline __be32
3876 nfs4_check_delegmode(struct nfs4_delegation
*dp
, int flags
)
3878 if ((flags
& WR_STATE
) && (dp
->dl_type
== NFS4_OPEN_DELEGATE_READ
))
3879 return nfserr_openmode
;
3884 static int share_access_to_flags(u32 share_access
)
3886 return share_access
== NFS4_SHARE_ACCESS_READ
? RD_STATE
: WR_STATE
;
3889 static struct nfs4_delegation
*find_deleg_stateid(struct nfs4_client
*cl
, stateid_t
*s
)
3891 struct nfs4_stid
*ret
;
3893 ret
= find_stateid_by_type(cl
, s
,
3894 NFS4_DELEG_STID
|NFS4_REVOKED_DELEG_STID
);
3897 return delegstateid(ret
);
3900 static bool nfsd4_is_deleg_cur(struct nfsd4_open
*open
)
3902 return open
->op_claim_type
== NFS4_OPEN_CLAIM_DELEGATE_CUR
||
3903 open
->op_claim_type
== NFS4_OPEN_CLAIM_DELEG_CUR_FH
;
3907 nfs4_check_deleg(struct nfs4_client
*cl
, struct nfsd4_open
*open
,
3908 struct nfs4_delegation
**dp
)
3911 __be32 status
= nfserr_bad_stateid
;
3912 struct nfs4_delegation
*deleg
;
3914 deleg
= find_deleg_stateid(cl
, &open
->op_delegate_stateid
);
3917 if (deleg
->dl_stid
.sc_type
== NFS4_REVOKED_DELEG_STID
) {
3918 nfs4_put_stid(&deleg
->dl_stid
);
3919 if (cl
->cl_minorversion
)
3920 status
= nfserr_deleg_revoked
;
3923 flags
= share_access_to_flags(open
->op_share_access
);
3924 status
= nfs4_check_delegmode(deleg
, flags
);
3926 nfs4_put_stid(&deleg
->dl_stid
);
3931 if (!nfsd4_is_deleg_cur(open
))
3935 open
->op_openowner
->oo_flags
|= NFS4_OO_CONFIRMED
;
3939 static inline int nfs4_access_to_access(u32 nfs4_access
)
3943 if (nfs4_access
& NFS4_SHARE_ACCESS_READ
)
3944 flags
|= NFSD_MAY_READ
;
3945 if (nfs4_access
& NFS4_SHARE_ACCESS_WRITE
)
3946 flags
|= NFSD_MAY_WRITE
;
3950 static inline __be32
3951 nfsd4_truncate(struct svc_rqst
*rqstp
, struct svc_fh
*fh
,
3952 struct nfsd4_open
*open
)
3954 struct iattr iattr
= {
3955 .ia_valid
= ATTR_SIZE
,
3958 if (!open
->op_truncate
)
3960 if (!(open
->op_share_access
& NFS4_SHARE_ACCESS_WRITE
))
3961 return nfserr_inval
;
3962 return nfsd_setattr(rqstp
, fh
, &iattr
, 0, (time_t)0);
3965 static __be32
nfs4_get_vfs_file(struct svc_rqst
*rqstp
, struct nfs4_file
*fp
,
3966 struct svc_fh
*cur_fh
, struct nfs4_ol_stateid
*stp
,
3967 struct nfsd4_open
*open
)
3969 struct file
*filp
= NULL
;
3971 int oflag
= nfs4_access_to_omode(open
->op_share_access
);
3972 int access
= nfs4_access_to_access(open
->op_share_access
);
3973 unsigned char old_access_bmap
, old_deny_bmap
;
3975 spin_lock(&fp
->fi_lock
);
3978 * Are we trying to set a deny mode that would conflict with
3981 status
= nfs4_file_check_deny(fp
, open
->op_share_deny
);
3982 if (status
!= nfs_ok
) {
3983 spin_unlock(&fp
->fi_lock
);
3987 /* set access to the file */
3988 status
= nfs4_file_get_access(fp
, open
->op_share_access
);
3989 if (status
!= nfs_ok
) {
3990 spin_unlock(&fp
->fi_lock
);
3994 /* Set access bits in stateid */
3995 old_access_bmap
= stp
->st_access_bmap
;
3996 set_access(open
->op_share_access
, stp
);
3998 /* Set new deny mask */
3999 old_deny_bmap
= stp
->st_deny_bmap
;
4000 set_deny(open
->op_share_deny
, stp
);
4001 fp
->fi_share_deny
|= (open
->op_share_deny
& NFS4_SHARE_DENY_BOTH
);
4003 if (!fp
->fi_fds
[oflag
]) {
4004 spin_unlock(&fp
->fi_lock
);
4005 status
= nfsd_open(rqstp
, cur_fh
, S_IFREG
, access
, &filp
);
4007 goto out_put_access
;
4008 spin_lock(&fp
->fi_lock
);
4009 if (!fp
->fi_fds
[oflag
]) {
4010 fp
->fi_fds
[oflag
] = filp
;
4014 spin_unlock(&fp
->fi_lock
);
4018 status
= nfsd4_truncate(rqstp
, cur_fh
, open
);
4020 goto out_put_access
;
4024 stp
->st_access_bmap
= old_access_bmap
;
4025 nfs4_file_put_access(fp
, open
->op_share_access
);
4026 reset_union_bmap_deny(bmap_to_share_mode(old_deny_bmap
), stp
);
4031 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
)
4034 unsigned char old_deny_bmap
= stp
->st_deny_bmap
;
4036 if (!test_access(open
->op_share_access
, stp
))
4037 return nfs4_get_vfs_file(rqstp
, fp
, cur_fh
, stp
, open
);
4039 /* test and set deny mode */
4040 spin_lock(&fp
->fi_lock
);
4041 status
= nfs4_file_check_deny(fp
, open
->op_share_deny
);
4042 if (status
== nfs_ok
) {
4043 set_deny(open
->op_share_deny
, stp
);
4044 fp
->fi_share_deny
|=
4045 (open
->op_share_deny
& NFS4_SHARE_DENY_BOTH
);
4047 spin_unlock(&fp
->fi_lock
);
4049 if (status
!= nfs_ok
)
4052 status
= nfsd4_truncate(rqstp
, cur_fh
, open
);
4053 if (status
!= nfs_ok
)
4054 reset_union_bmap_deny(old_deny_bmap
, stp
);
4058 /* Should we give out recallable state?: */
4059 static bool nfsd4_cb_channel_good(struct nfs4_client
*clp
)
4061 if (clp
->cl_cb_state
== NFSD4_CB_UP
)
4064 * In the sessions case, since we don't have to establish a
4065 * separate connection for callbacks, we assume it's OK
4066 * until we hear otherwise:
4068 return clp
->cl_minorversion
&& clp
->cl_cb_state
== NFSD4_CB_UNKNOWN
;
4071 static struct file_lock
*nfs4_alloc_init_lease(struct nfs4_file
*fp
, int flag
)
4073 struct file_lock
*fl
;
4075 fl
= locks_alloc_lock();
4078 fl
->fl_lmops
= &nfsd_lease_mng_ops
;
4079 fl
->fl_flags
= FL_DELEG
;
4080 fl
->fl_type
= flag
== NFS4_OPEN_DELEGATE_READ
? F_RDLCK
: F_WRLCK
;
4081 fl
->fl_end
= OFFSET_MAX
;
4082 fl
->fl_owner
= (fl_owner_t
)fp
;
4083 fl
->fl_pid
= current
->tgid
;
4088 * nfs4_setlease - Obtain a delegation by requesting lease from vfs layer
4089 * @dp: a pointer to the nfs4_delegation we're adding.
4092 * On success: Return code will be 0 on success.
4094 * On error: -EAGAIN if there was an existing delegation.
4095 * nonzero if there is an error in other cases.
4099 static int nfs4_setlease(struct nfs4_delegation
*dp
)
4101 struct nfs4_file
*fp
= dp
->dl_stid
.sc_file
;
4102 struct file_lock
*fl
;
4106 fl
= nfs4_alloc_init_lease(fp
, NFS4_OPEN_DELEGATE_READ
);
4109 filp
= find_readable_file(fp
);
4111 /* We should always have a readable file here */
4113 locks_free_lock(fl
);
4117 status
= vfs_setlease(filp
, fl
->fl_type
, &fl
, NULL
);
4119 locks_free_lock(fl
);
4122 spin_lock(&state_lock
);
4123 spin_lock(&fp
->fi_lock
);
4124 /* Did the lease get broken before we took the lock? */
4126 if (fp
->fi_had_conflict
)
4129 if (fp
->fi_deleg_file
) {
4130 status
= hash_delegation_locked(dp
, fp
);
4133 fp
->fi_deleg_file
= filp
;
4134 fp
->fi_delegees
= 0;
4135 status
= hash_delegation_locked(dp
, fp
);
4136 spin_unlock(&fp
->fi_lock
);
4137 spin_unlock(&state_lock
);
4139 /* Should never happen, this is a new fi_deleg_file */
4145 spin_unlock(&fp
->fi_lock
);
4146 spin_unlock(&state_lock
);
4152 static struct nfs4_delegation
*
4153 nfs4_set_delegation(struct nfs4_client
*clp
, struct svc_fh
*fh
,
4154 struct nfs4_file
*fp
, struct nfs4_clnt_odstate
*odstate
)
4157 struct nfs4_delegation
*dp
;
4159 if (fp
->fi_had_conflict
)
4160 return ERR_PTR(-EAGAIN
);
4162 spin_lock(&state_lock
);
4163 spin_lock(&fp
->fi_lock
);
4164 status
= nfs4_get_existing_delegation(clp
, fp
);
4165 spin_unlock(&fp
->fi_lock
);
4166 spin_unlock(&state_lock
);
4169 return ERR_PTR(status
);
4171 dp
= alloc_init_deleg(clp
, fh
, odstate
);
4173 return ERR_PTR(-ENOMEM
);
4176 spin_lock(&state_lock
);
4177 spin_lock(&fp
->fi_lock
);
4178 dp
->dl_stid
.sc_file
= fp
;
4179 if (!fp
->fi_deleg_file
) {
4180 spin_unlock(&fp
->fi_lock
);
4181 spin_unlock(&state_lock
);
4182 status
= nfs4_setlease(dp
);
4185 if (fp
->fi_had_conflict
) {
4189 status
= hash_delegation_locked(dp
, fp
);
4191 spin_unlock(&fp
->fi_lock
);
4192 spin_unlock(&state_lock
);
4195 put_clnt_odstate(dp
->dl_clnt_odstate
);
4196 nfs4_put_stid(&dp
->dl_stid
);
4197 return ERR_PTR(status
);
4202 static void nfsd4_open_deleg_none_ext(struct nfsd4_open
*open
, int status
)
4204 open
->op_delegate_type
= NFS4_OPEN_DELEGATE_NONE_EXT
;
4205 if (status
== -EAGAIN
)
4206 open
->op_why_no_deleg
= WND4_CONTENTION
;
4208 open
->op_why_no_deleg
= WND4_RESOURCE
;
4209 switch (open
->op_deleg_want
) {
4210 case NFS4_SHARE_WANT_READ_DELEG
:
4211 case NFS4_SHARE_WANT_WRITE_DELEG
:
4212 case NFS4_SHARE_WANT_ANY_DELEG
:
4214 case NFS4_SHARE_WANT_CANCEL
:
4215 open
->op_why_no_deleg
= WND4_CANCELLED
;
4217 case NFS4_SHARE_WANT_NO_DELEG
:
4224 * Attempt to hand out a delegation.
4226 * Note we don't support write delegations, and won't until the vfs has
4227 * proper support for them.
4230 nfs4_open_delegation(struct svc_fh
*fh
, struct nfsd4_open
*open
,
4231 struct nfs4_ol_stateid
*stp
)
4233 struct nfs4_delegation
*dp
;
4234 struct nfs4_openowner
*oo
= openowner(stp
->st_stateowner
);
4235 struct nfs4_client
*clp
= stp
->st_stid
.sc_client
;
4239 cb_up
= nfsd4_cb_channel_good(oo
->oo_owner
.so_client
);
4240 open
->op_recall
= 0;
4241 switch (open
->op_claim_type
) {
4242 case NFS4_OPEN_CLAIM_PREVIOUS
:
4244 open
->op_recall
= 1;
4245 if (open
->op_delegate_type
!= NFS4_OPEN_DELEGATE_READ
)
4248 case NFS4_OPEN_CLAIM_NULL
:
4249 case NFS4_OPEN_CLAIM_FH
:
4251 * Let's not give out any delegations till everyone's
4252 * had the chance to reclaim theirs, *and* until
4253 * NLM locks have all been reclaimed:
4255 if (locks_in_grace(clp
->net
))
4257 if (!cb_up
|| !(oo
->oo_flags
& NFS4_OO_CONFIRMED
))
4260 * Also, if the file was opened for write or
4261 * create, there's a good chance the client's
4262 * about to write to it, resulting in an
4263 * immediate recall (since we don't support
4264 * write delegations):
4266 if (open
->op_share_access
& NFS4_SHARE_ACCESS_WRITE
)
4268 if (open
->op_create
== NFS4_OPEN_CREATE
)
4274 dp
= nfs4_set_delegation(clp
, fh
, stp
->st_stid
.sc_file
, stp
->st_clnt_odstate
);
4278 memcpy(&open
->op_delegate_stateid
, &dp
->dl_stid
.sc_stateid
, sizeof(dp
->dl_stid
.sc_stateid
));
4280 dprintk("NFSD: delegation stateid=" STATEID_FMT
"\n",
4281 STATEID_VAL(&dp
->dl_stid
.sc_stateid
));
4282 open
->op_delegate_type
= NFS4_OPEN_DELEGATE_READ
;
4283 nfs4_put_stid(&dp
->dl_stid
);
4286 open
->op_delegate_type
= NFS4_OPEN_DELEGATE_NONE
;
4287 if (open
->op_claim_type
== NFS4_OPEN_CLAIM_PREVIOUS
&&
4288 open
->op_delegate_type
!= NFS4_OPEN_DELEGATE_NONE
) {
4289 dprintk("NFSD: WARNING: refusing delegation reclaim\n");
4290 open
->op_recall
= 1;
4293 /* 4.1 client asking for a delegation? */
4294 if (open
->op_deleg_want
)
4295 nfsd4_open_deleg_none_ext(open
, status
);
4299 static void nfsd4_deleg_xgrade_none_ext(struct nfsd4_open
*open
,
4300 struct nfs4_delegation
*dp
)
4302 if (open
->op_deleg_want
== NFS4_SHARE_WANT_READ_DELEG
&&
4303 dp
->dl_type
== NFS4_OPEN_DELEGATE_WRITE
) {
4304 open
->op_delegate_type
= NFS4_OPEN_DELEGATE_NONE_EXT
;
4305 open
->op_why_no_deleg
= WND4_NOT_SUPP_DOWNGRADE
;
4306 } else if (open
->op_deleg_want
== NFS4_SHARE_WANT_WRITE_DELEG
&&
4307 dp
->dl_type
== NFS4_OPEN_DELEGATE_WRITE
) {
4308 open
->op_delegate_type
= NFS4_OPEN_DELEGATE_NONE_EXT
;
4309 open
->op_why_no_deleg
= WND4_NOT_SUPP_UPGRADE
;
4311 /* Otherwise the client must be confused wanting a delegation
4312 * it already has, therefore we don't return
4313 * NFS4_OPEN_DELEGATE_NONE_EXT and reason.
4318 nfsd4_process_open2(struct svc_rqst
*rqstp
, struct svc_fh
*current_fh
, struct nfsd4_open
*open
)
4320 struct nfsd4_compoundres
*resp
= rqstp
->rq_resp
;
4321 struct nfs4_client
*cl
= open
->op_openowner
->oo_owner
.so_client
;
4322 struct nfs4_file
*fp
= NULL
;
4323 struct nfs4_ol_stateid
*stp
= NULL
;
4324 struct nfs4_delegation
*dp
= NULL
;
4326 bool new_stp
= false;
4329 * Lookup file; if found, lookup stateid and check open request,
4330 * and check for delegations in the process of being recalled.
4331 * If not found, create the nfs4_file struct
4333 fp
= find_or_add_file(open
->op_file
, ¤t_fh
->fh_handle
);
4334 if (fp
!= open
->op_file
) {
4335 status
= nfs4_check_deleg(cl
, open
, &dp
);
4338 stp
= nfsd4_find_and_lock_existing_open(fp
, open
);
4340 open
->op_file
= NULL
;
4341 status
= nfserr_bad_stateid
;
4342 if (nfsd4_is_deleg_cur(open
))
4347 stp
= init_open_stateid(fp
, open
);
4353 * OPEN the file, or upgrade an existing OPEN.
4354 * If truncate fails, the OPEN fails.
4356 * stp is already locked.
4359 /* Stateid was found, this is an OPEN upgrade */
4360 status
= nfs4_upgrade_open(rqstp
, fp
, current_fh
, stp
, open
);
4362 mutex_unlock(&stp
->st_mutex
);
4366 status
= nfs4_get_vfs_file(rqstp
, fp
, current_fh
, stp
, open
);
4368 stp
->st_stid
.sc_type
= NFS4_CLOSED_STID
;
4369 release_open_stateid(stp
);
4370 mutex_unlock(&stp
->st_mutex
);
4374 stp
->st_clnt_odstate
= find_or_hash_clnt_odstate(fp
,
4376 if (stp
->st_clnt_odstate
== open
->op_odstate
)
4377 open
->op_odstate
= NULL
;
4380 nfs4_inc_and_copy_stateid(&open
->op_stateid
, &stp
->st_stid
);
4381 mutex_unlock(&stp
->st_mutex
);
4383 if (nfsd4_has_session(&resp
->cstate
)) {
4384 if (open
->op_deleg_want
& NFS4_SHARE_WANT_NO_DELEG
) {
4385 open
->op_delegate_type
= NFS4_OPEN_DELEGATE_NONE_EXT
;
4386 open
->op_why_no_deleg
= WND4_NOT_WANTED
;
4392 * Attempt to hand out a delegation. No error return, because the
4393 * OPEN succeeds even if we fail.
4395 nfs4_open_delegation(current_fh
, open
, stp
);
4399 dprintk("%s: stateid=" STATEID_FMT
"\n", __func__
,
4400 STATEID_VAL(&stp
->st_stid
.sc_stateid
));
4402 /* 4.1 client trying to upgrade/downgrade delegation? */
4403 if (open
->op_delegate_type
== NFS4_OPEN_DELEGATE_NONE
&& dp
&&
4404 open
->op_deleg_want
)
4405 nfsd4_deleg_xgrade_none_ext(open
, dp
);
4409 if (status
== 0 && open
->op_claim_type
== NFS4_OPEN_CLAIM_PREVIOUS
)
4410 open
->op_openowner
->oo_flags
|= NFS4_OO_CONFIRMED
;
4412 * To finish the open response, we just need to set the rflags.
4414 open
->op_rflags
= NFS4_OPEN_RESULT_LOCKTYPE_POSIX
;
4415 if (!(open
->op_openowner
->oo_flags
& NFS4_OO_CONFIRMED
) &&
4416 !nfsd4_has_session(&resp
->cstate
))
4417 open
->op_rflags
|= NFS4_OPEN_RESULT_CONFIRM
;
4419 nfs4_put_stid(&dp
->dl_stid
);
4421 nfs4_put_stid(&stp
->st_stid
);
4426 void nfsd4_cleanup_open_state(struct nfsd4_compound_state
*cstate
,
4427 struct nfsd4_open
*open
)
4429 if (open
->op_openowner
) {
4430 struct nfs4_stateowner
*so
= &open
->op_openowner
->oo_owner
;
4432 nfsd4_cstate_assign_replay(cstate
, so
);
4433 nfs4_put_stateowner(so
);
4436 kmem_cache_free(file_slab
, open
->op_file
);
4438 nfs4_put_stid(&open
->op_stp
->st_stid
);
4439 if (open
->op_odstate
)
4440 kmem_cache_free(odstate_slab
, open
->op_odstate
);
4444 nfsd4_renew(struct svc_rqst
*rqstp
, struct nfsd4_compound_state
*cstate
,
4447 struct nfs4_client
*clp
;
4449 struct nfsd_net
*nn
= net_generic(SVC_NET(rqstp
), nfsd_net_id
);
4451 dprintk("process_renew(%08x/%08x): starting\n",
4452 clid
->cl_boot
, clid
->cl_id
);
4453 status
= lookup_clientid(clid
, cstate
, nn
);
4457 status
= nfserr_cb_path_down
;
4458 if (!list_empty(&clp
->cl_delegations
)
4459 && clp
->cl_cb_state
!= NFSD4_CB_UP
)
4467 nfsd4_end_grace(struct nfsd_net
*nn
)
4469 /* do nothing if grace period already ended */
4470 if (nn
->grace_ended
)
4473 dprintk("NFSD: end of grace period\n");
4474 nn
->grace_ended
= true;
4476 * If the server goes down again right now, an NFSv4
4477 * client will still be allowed to reclaim after it comes back up,
4478 * even if it hasn't yet had a chance to reclaim state this time.
4481 nfsd4_record_grace_done(nn
);
4483 * At this point, NFSv4 clients can still reclaim. But if the
4484 * server crashes, any that have not yet reclaimed will be out
4485 * of luck on the next boot.
4487 * (NFSv4.1+ clients are considered to have reclaimed once they
4488 * call RECLAIM_COMPLETE. NFSv4.0 clients are considered to
4489 * have reclaimed after their first OPEN.)
4491 locks_end_grace(&nn
->nfsd4_manager
);
4493 * At this point, and once lockd and/or any other containers
4494 * exit their grace period, further reclaims will fail and
4495 * regular locking can resume.
4500 nfs4_laundromat(struct nfsd_net
*nn
)
4502 struct nfs4_client
*clp
;
4503 struct nfs4_openowner
*oo
;
4504 struct nfs4_delegation
*dp
;
4505 struct nfs4_ol_stateid
*stp
;
4506 struct list_head
*pos
, *next
, reaplist
;
4507 time_t cutoff
= get_seconds() - nn
->nfsd4_lease
;
4508 time_t t
, new_timeo
= nn
->nfsd4_lease
;
4510 dprintk("NFSD: laundromat service - starting\n");
4511 nfsd4_end_grace(nn
);
4512 INIT_LIST_HEAD(&reaplist
);
4513 spin_lock(&nn
->client_lock
);
4514 list_for_each_safe(pos
, next
, &nn
->client_lru
) {
4515 clp
= list_entry(pos
, struct nfs4_client
, cl_lru
);
4516 if (time_after((unsigned long)clp
->cl_time
, (unsigned long)cutoff
)) {
4517 t
= clp
->cl_time
- cutoff
;
4518 new_timeo
= min(new_timeo
, t
);
4521 if (mark_client_expired_locked(clp
)) {
4522 dprintk("NFSD: client in use (clientid %08x)\n",
4523 clp
->cl_clientid
.cl_id
);
4526 list_add(&clp
->cl_lru
, &reaplist
);
4528 spin_unlock(&nn
->client_lock
);
4529 list_for_each_safe(pos
, next
, &reaplist
) {
4530 clp
= list_entry(pos
, struct nfs4_client
, cl_lru
);
4531 dprintk("NFSD: purging unused client (clientid %08x)\n",
4532 clp
->cl_clientid
.cl_id
);
4533 list_del_init(&clp
->cl_lru
);
4536 spin_lock(&state_lock
);
4537 list_for_each_safe(pos
, next
, &nn
->del_recall_lru
) {
4538 dp
= list_entry (pos
, struct nfs4_delegation
, dl_recall_lru
);
4539 if (time_after((unsigned long)dp
->dl_time
, (unsigned long)cutoff
)) {
4540 t
= dp
->dl_time
- cutoff
;
4541 new_timeo
= min(new_timeo
, t
);
4544 WARN_ON(!unhash_delegation_locked(dp
));
4545 list_add(&dp
->dl_recall_lru
, &reaplist
);
4547 spin_unlock(&state_lock
);
4548 while (!list_empty(&reaplist
)) {
4549 dp
= list_first_entry(&reaplist
, struct nfs4_delegation
,
4551 list_del_init(&dp
->dl_recall_lru
);
4552 revoke_delegation(dp
);
4555 spin_lock(&nn
->client_lock
);
4556 while (!list_empty(&nn
->close_lru
)) {
4557 oo
= list_first_entry(&nn
->close_lru
, struct nfs4_openowner
,
4559 if (time_after((unsigned long)oo
->oo_time
,
4560 (unsigned long)cutoff
)) {
4561 t
= oo
->oo_time
- cutoff
;
4562 new_timeo
= min(new_timeo
, t
);
4565 list_del_init(&oo
->oo_close_lru
);
4566 stp
= oo
->oo_last_closed_stid
;
4567 oo
->oo_last_closed_stid
= NULL
;
4568 spin_unlock(&nn
->client_lock
);
4569 nfs4_put_stid(&stp
->st_stid
);
4570 spin_lock(&nn
->client_lock
);
4572 spin_unlock(&nn
->client_lock
);
4574 new_timeo
= max_t(time_t, new_timeo
, NFSD_LAUNDROMAT_MINTIMEOUT
);
4578 static struct workqueue_struct
*laundry_wq
;
4579 static void laundromat_main(struct work_struct
*);
4582 laundromat_main(struct work_struct
*laundry
)
4585 struct delayed_work
*dwork
= container_of(laundry
, struct delayed_work
,
4587 struct nfsd_net
*nn
= container_of(dwork
, struct nfsd_net
,
4590 t
= nfs4_laundromat(nn
);
4591 dprintk("NFSD: laundromat_main - sleeping for %ld seconds\n", t
);
4592 queue_delayed_work(laundry_wq
, &nn
->laundromat_work
, t
*HZ
);
4595 static inline __be32
nfs4_check_fh(struct svc_fh
*fhp
, struct nfs4_stid
*stp
)
4597 if (!fh_match(&fhp
->fh_handle
, &stp
->sc_file
->fi_fhandle
))
4598 return nfserr_bad_stateid
;
4603 access_permit_read(struct nfs4_ol_stateid
*stp
)
4605 return test_access(NFS4_SHARE_ACCESS_READ
, stp
) ||
4606 test_access(NFS4_SHARE_ACCESS_BOTH
, stp
) ||
4607 test_access(NFS4_SHARE_ACCESS_WRITE
, stp
);
4611 access_permit_write(struct nfs4_ol_stateid
*stp
)
4613 return test_access(NFS4_SHARE_ACCESS_WRITE
, stp
) ||
4614 test_access(NFS4_SHARE_ACCESS_BOTH
, stp
);
4618 __be32
nfs4_check_openmode(struct nfs4_ol_stateid
*stp
, int flags
)
4620 __be32 status
= nfserr_openmode
;
4622 /* For lock stateid's, we test the parent open, not the lock: */
4623 if (stp
->st_openstp
)
4624 stp
= stp
->st_openstp
;
4625 if ((flags
& WR_STATE
) && !access_permit_write(stp
))
4627 if ((flags
& RD_STATE
) && !access_permit_read(stp
))
4634 static inline __be32
4635 check_special_stateids(struct net
*net
, svc_fh
*current_fh
, stateid_t
*stateid
, int flags
)
4637 if (ONE_STATEID(stateid
) && (flags
& RD_STATE
))
4639 else if (opens_in_grace(net
)) {
4640 /* Answer in remaining cases depends on existence of
4641 * conflicting state; so we must wait out the grace period. */
4642 return nfserr_grace
;
4643 } else if (flags
& WR_STATE
)
4644 return nfs4_share_conflict(current_fh
,
4645 NFS4_SHARE_DENY_WRITE
);
4646 else /* (flags & RD_STATE) && ZERO_STATEID(stateid) */
4647 return nfs4_share_conflict(current_fh
,
4648 NFS4_SHARE_DENY_READ
);
4652 * Allow READ/WRITE during grace period on recovered state only for files
4653 * that are not able to provide mandatory locking.
4656 grace_disallows_io(struct net
*net
, struct inode
*inode
)
4658 return opens_in_grace(net
) && mandatory_lock(inode
);
4661 /* Returns true iff a is later than b: */
4662 static bool stateid_generation_after(stateid_t
*a
, stateid_t
*b
)
4664 return (s32
)(a
->si_generation
- b
->si_generation
) > 0;
4667 static __be32
check_stateid_generation(stateid_t
*in
, stateid_t
*ref
, bool has_session
)
4670 * When sessions are used the stateid generation number is ignored
4673 if (has_session
&& in
->si_generation
== 0)
4676 if (in
->si_generation
== ref
->si_generation
)
4679 /* If the client sends us a stateid from the future, it's buggy: */
4680 if (stateid_generation_after(in
, ref
))
4681 return nfserr_bad_stateid
;
4683 * However, we could see a stateid from the past, even from a
4684 * non-buggy client. For example, if the client sends a lock
4685 * while some IO is outstanding, the lock may bump si_generation
4686 * while the IO is still in flight. The client could avoid that
4687 * situation by waiting for responses on all the IO requests,
4688 * but better performance may result in retrying IO that
4689 * receives an old_stateid error if requests are rarely
4690 * reordered in flight:
4692 return nfserr_old_stateid
;
4695 static __be32
nfsd4_check_openowner_confirmed(struct nfs4_ol_stateid
*ols
)
4697 if (ols
->st_stateowner
->so_is_open_owner
&&
4698 !(openowner(ols
->st_stateowner
)->oo_flags
& NFS4_OO_CONFIRMED
))
4699 return nfserr_bad_stateid
;
4703 static __be32
nfsd4_validate_stateid(struct nfs4_client
*cl
, stateid_t
*stateid
)
4705 struct nfs4_stid
*s
;
4706 __be32 status
= nfserr_bad_stateid
;
4708 if (ZERO_STATEID(stateid
) || ONE_STATEID(stateid
) ||
4709 CLOSE_STATEID(stateid
))
4711 /* Client debugging aid. */
4712 if (!same_clid(&stateid
->si_opaque
.so_clid
, &cl
->cl_clientid
)) {
4713 char addr_str
[INET6_ADDRSTRLEN
];
4714 rpc_ntop((struct sockaddr
*)&cl
->cl_addr
, addr_str
,
4716 pr_warn_ratelimited("NFSD: client %s testing state ID "
4717 "with incorrect client ID\n", addr_str
);
4720 spin_lock(&cl
->cl_lock
);
4721 s
= find_stateid_locked(cl
, stateid
);
4724 status
= check_stateid_generation(stateid
, &s
->sc_stateid
, 1);
4727 switch (s
->sc_type
) {
4728 case NFS4_DELEG_STID
:
4731 case NFS4_REVOKED_DELEG_STID
:
4732 status
= nfserr_deleg_revoked
;
4734 case NFS4_OPEN_STID
:
4735 case NFS4_LOCK_STID
:
4736 status
= nfsd4_check_openowner_confirmed(openlockstateid(s
));
4739 printk("unknown stateid type %x\n", s
->sc_type
);
4741 case NFS4_CLOSED_STID
:
4742 case NFS4_CLOSED_DELEG_STID
:
4743 status
= nfserr_bad_stateid
;
4746 spin_unlock(&cl
->cl_lock
);
4751 nfsd4_lookup_stateid(struct nfsd4_compound_state
*cstate
,
4752 stateid_t
*stateid
, unsigned char typemask
,
4753 struct nfs4_stid
**s
, struct nfsd_net
*nn
)
4756 bool return_revoked
= false;
4759 * only return revoked delegations if explicitly asked.
4760 * otherwise we report revoked or bad_stateid status.
4762 if (typemask
& NFS4_REVOKED_DELEG_STID
)
4763 return_revoked
= true;
4764 else if (typemask
& NFS4_DELEG_STID
)
4765 typemask
|= NFS4_REVOKED_DELEG_STID
;
4767 if (ZERO_STATEID(stateid
) || ONE_STATEID(stateid
) ||
4768 CLOSE_STATEID(stateid
))
4769 return nfserr_bad_stateid
;
4770 status
= lookup_clientid(&stateid
->si_opaque
.so_clid
, cstate
, nn
);
4771 if (status
== nfserr_stale_clientid
) {
4772 if (cstate
->session
)
4773 return nfserr_bad_stateid
;
4774 return nfserr_stale_stateid
;
4778 *s
= find_stateid_by_type(cstate
->clp
, stateid
, typemask
);
4780 return nfserr_bad_stateid
;
4781 if (((*s
)->sc_type
== NFS4_REVOKED_DELEG_STID
) && !return_revoked
) {
4783 if (cstate
->minorversion
)
4784 return nfserr_deleg_revoked
;
4785 return nfserr_bad_stateid
;
4790 static struct file
*
4791 nfs4_find_file(struct nfs4_stid
*s
, int flags
)
4796 switch (s
->sc_type
) {
4797 case NFS4_DELEG_STID
:
4798 if (WARN_ON_ONCE(!s
->sc_file
->fi_deleg_file
))
4800 return get_file(s
->sc_file
->fi_deleg_file
);
4801 case NFS4_OPEN_STID
:
4802 case NFS4_LOCK_STID
:
4803 if (flags
& RD_STATE
)
4804 return find_readable_file(s
->sc_file
);
4806 return find_writeable_file(s
->sc_file
);
4814 nfs4_check_olstateid(struct svc_fh
*fhp
, struct nfs4_ol_stateid
*ols
, int flags
)
4818 status
= nfsd4_check_openowner_confirmed(ols
);
4821 return nfs4_check_openmode(ols
, flags
);
4825 nfs4_check_file(struct svc_rqst
*rqstp
, struct svc_fh
*fhp
, struct nfs4_stid
*s
,
4826 struct file
**filpp
, bool *tmp_file
, int flags
)
4828 int acc
= (flags
& RD_STATE
) ? NFSD_MAY_READ
: NFSD_MAY_WRITE
;
4832 file
= nfs4_find_file(s
, flags
);
4834 status
= nfsd_permission(rqstp
, fhp
->fh_export
, fhp
->fh_dentry
,
4835 acc
| NFSD_MAY_OWNER_OVERRIDE
);
4843 status
= nfsd_open(rqstp
, fhp
, S_IFREG
, acc
, filpp
);
4855 * Checks for stateid operations
4858 nfs4_preprocess_stateid_op(struct svc_rqst
*rqstp
,
4859 struct nfsd4_compound_state
*cstate
, stateid_t
*stateid
,
4860 int flags
, struct file
**filpp
, bool *tmp_file
)
4862 struct svc_fh
*fhp
= &cstate
->current_fh
;
4863 struct inode
*ino
= d_inode(fhp
->fh_dentry
);
4864 struct net
*net
= SVC_NET(rqstp
);
4865 struct nfsd_net
*nn
= net_generic(net
, nfsd_net_id
);
4866 struct nfs4_stid
*s
= NULL
;
4874 if (grace_disallows_io(net
, ino
))
4875 return nfserr_grace
;
4877 if (ZERO_STATEID(stateid
) || ONE_STATEID(stateid
)) {
4878 status
= check_special_stateids(net
, fhp
, stateid
, flags
);
4882 status
= nfsd4_lookup_stateid(cstate
, stateid
,
4883 NFS4_DELEG_STID
|NFS4_OPEN_STID
|NFS4_LOCK_STID
,
4887 status
= check_stateid_generation(stateid
, &s
->sc_stateid
,
4888 nfsd4_has_session(cstate
));
4892 switch (s
->sc_type
) {
4893 case NFS4_DELEG_STID
:
4894 status
= nfs4_check_delegmode(delegstateid(s
), flags
);
4896 case NFS4_OPEN_STID
:
4897 case NFS4_LOCK_STID
:
4898 status
= nfs4_check_olstateid(fhp
, openlockstateid(s
), flags
);
4901 status
= nfserr_bad_stateid
;
4906 status
= nfs4_check_fh(fhp
, s
);
4909 if (!status
&& filpp
)
4910 status
= nfs4_check_file(rqstp
, fhp
, s
, filpp
, tmp_file
, flags
);
4918 * Test if the stateid is valid
4921 nfsd4_test_stateid(struct svc_rqst
*rqstp
, struct nfsd4_compound_state
*cstate
,
4922 struct nfsd4_test_stateid
*test_stateid
)
4924 struct nfsd4_test_stateid_id
*stateid
;
4925 struct nfs4_client
*cl
= cstate
->session
->se_client
;
4927 list_for_each_entry(stateid
, &test_stateid
->ts_stateid_list
, ts_id_list
)
4928 stateid
->ts_id_status
=
4929 nfsd4_validate_stateid(cl
, &stateid
->ts_id_stateid
);
4935 nfsd4_free_lock_stateid(stateid_t
*stateid
, struct nfs4_stid
*s
)
4937 struct nfs4_ol_stateid
*stp
= openlockstateid(s
);
4940 mutex_lock(&stp
->st_mutex
);
4942 ret
= check_stateid_generation(stateid
, &s
->sc_stateid
, 1);
4946 ret
= nfserr_locks_held
;
4947 if (check_for_locks(stp
->st_stid
.sc_file
,
4948 lockowner(stp
->st_stateowner
)))
4951 release_lock_stateid(stp
);
4955 mutex_unlock(&stp
->st_mutex
);
4961 nfsd4_free_stateid(struct svc_rqst
*rqstp
, struct nfsd4_compound_state
*cstate
,
4962 struct nfsd4_free_stateid
*free_stateid
)
4964 stateid_t
*stateid
= &free_stateid
->fr_stateid
;
4965 struct nfs4_stid
*s
;
4966 struct nfs4_delegation
*dp
;
4967 struct nfs4_client
*cl
= cstate
->session
->se_client
;
4968 __be32 ret
= nfserr_bad_stateid
;
4970 spin_lock(&cl
->cl_lock
);
4971 s
= find_stateid_locked(cl
, stateid
);
4974 switch (s
->sc_type
) {
4975 case NFS4_DELEG_STID
:
4976 ret
= nfserr_locks_held
;
4978 case NFS4_OPEN_STID
:
4979 ret
= check_stateid_generation(stateid
, &s
->sc_stateid
, 1);
4982 ret
= nfserr_locks_held
;
4984 case NFS4_LOCK_STID
:
4985 atomic_inc(&s
->sc_count
);
4986 spin_unlock(&cl
->cl_lock
);
4987 ret
= nfsd4_free_lock_stateid(stateid
, s
);
4989 case NFS4_REVOKED_DELEG_STID
:
4990 dp
= delegstateid(s
);
4991 list_del_init(&dp
->dl_recall_lru
);
4992 spin_unlock(&cl
->cl_lock
);
4996 /* Default falls through and returns nfserr_bad_stateid */
4999 spin_unlock(&cl
->cl_lock
);
5007 return (type
== NFS4_READW_LT
|| type
== NFS4_READ_LT
) ?
5008 RD_STATE
: WR_STATE
;
5011 static __be32
nfs4_seqid_op_checks(struct nfsd4_compound_state
*cstate
, stateid_t
*stateid
, u32 seqid
, struct nfs4_ol_stateid
*stp
)
5013 struct svc_fh
*current_fh
= &cstate
->current_fh
;
5014 struct nfs4_stateowner
*sop
= stp
->st_stateowner
;
5017 status
= nfsd4_check_seqid(cstate
, sop
, seqid
);
5020 status
= nfsd4_lock_ol_stateid(stp
);
5021 if (status
!= nfs_ok
)
5023 status
= check_stateid_generation(stateid
, &stp
->st_stid
.sc_stateid
, nfsd4_has_session(cstate
));
5024 if (status
== nfs_ok
)
5025 status
= nfs4_check_fh(current_fh
, &stp
->st_stid
);
5026 if (status
!= nfs_ok
)
5027 mutex_unlock(&stp
->st_mutex
);
5032 * Checks for sequence id mutating operations.
5035 nfs4_preprocess_seqid_op(struct nfsd4_compound_state
*cstate
, u32 seqid
,
5036 stateid_t
*stateid
, char typemask
,
5037 struct nfs4_ol_stateid
**stpp
,
5038 struct nfsd_net
*nn
)
5041 struct nfs4_stid
*s
;
5042 struct nfs4_ol_stateid
*stp
= NULL
;
5044 dprintk("NFSD: %s: seqid=%d stateid = " STATEID_FMT
"\n", __func__
,
5045 seqid
, STATEID_VAL(stateid
));
5048 status
= nfsd4_lookup_stateid(cstate
, stateid
, typemask
, &s
, nn
);
5051 stp
= openlockstateid(s
);
5052 nfsd4_cstate_assign_replay(cstate
, stp
->st_stateowner
);
5054 status
= nfs4_seqid_op_checks(cstate
, stateid
, seqid
, stp
);
5058 nfs4_put_stid(&stp
->st_stid
);
5062 static __be32
nfs4_preprocess_confirmed_seqid_op(struct nfsd4_compound_state
*cstate
, u32 seqid
,
5063 stateid_t
*stateid
, struct nfs4_ol_stateid
**stpp
, struct nfsd_net
*nn
)
5066 struct nfs4_openowner
*oo
;
5067 struct nfs4_ol_stateid
*stp
;
5069 status
= nfs4_preprocess_seqid_op(cstate
, seqid
, stateid
,
5070 NFS4_OPEN_STID
, &stp
, nn
);
5073 oo
= openowner(stp
->st_stateowner
);
5074 if (!(oo
->oo_flags
& NFS4_OO_CONFIRMED
)) {
5075 mutex_unlock(&stp
->st_mutex
);
5076 nfs4_put_stid(&stp
->st_stid
);
5077 return nfserr_bad_stateid
;
5084 nfsd4_open_confirm(struct svc_rqst
*rqstp
, struct nfsd4_compound_state
*cstate
,
5085 struct nfsd4_open_confirm
*oc
)
5088 struct nfs4_openowner
*oo
;
5089 struct nfs4_ol_stateid
*stp
;
5090 struct nfsd_net
*nn
= net_generic(SVC_NET(rqstp
), nfsd_net_id
);
5092 dprintk("NFSD: nfsd4_open_confirm on file %pd\n",
5093 cstate
->current_fh
.fh_dentry
);
5095 status
= fh_verify(rqstp
, &cstate
->current_fh
, S_IFREG
, 0);
5099 status
= nfs4_preprocess_seqid_op(cstate
,
5100 oc
->oc_seqid
, &oc
->oc_req_stateid
,
5101 NFS4_OPEN_STID
, &stp
, nn
);
5104 oo
= openowner(stp
->st_stateowner
);
5105 status
= nfserr_bad_stateid
;
5106 if (oo
->oo_flags
& NFS4_OO_CONFIRMED
) {
5107 mutex_unlock(&stp
->st_mutex
);
5110 oo
->oo_flags
|= NFS4_OO_CONFIRMED
;
5111 nfs4_inc_and_copy_stateid(&oc
->oc_resp_stateid
, &stp
->st_stid
);
5112 mutex_unlock(&stp
->st_mutex
);
5113 dprintk("NFSD: %s: success, seqid=%d stateid=" STATEID_FMT
"\n",
5114 __func__
, oc
->oc_seqid
, STATEID_VAL(&stp
->st_stid
.sc_stateid
));
5116 nfsd4_client_record_create(oo
->oo_owner
.so_client
);
5119 nfs4_put_stid(&stp
->st_stid
);
5121 nfsd4_bump_seqid(cstate
, status
);
5125 static inline void nfs4_stateid_downgrade_bit(struct nfs4_ol_stateid
*stp
, u32 access
)
5127 if (!test_access(access
, stp
))
5129 nfs4_file_put_access(stp
->st_stid
.sc_file
, access
);
5130 clear_access(access
, stp
);
5133 static inline void nfs4_stateid_downgrade(struct nfs4_ol_stateid
*stp
, u32 to_access
)
5135 switch (to_access
) {
5136 case NFS4_SHARE_ACCESS_READ
:
5137 nfs4_stateid_downgrade_bit(stp
, NFS4_SHARE_ACCESS_WRITE
);
5138 nfs4_stateid_downgrade_bit(stp
, NFS4_SHARE_ACCESS_BOTH
);
5140 case NFS4_SHARE_ACCESS_WRITE
:
5141 nfs4_stateid_downgrade_bit(stp
, NFS4_SHARE_ACCESS_READ
);
5142 nfs4_stateid_downgrade_bit(stp
, NFS4_SHARE_ACCESS_BOTH
);
5144 case NFS4_SHARE_ACCESS_BOTH
:
5152 nfsd4_open_downgrade(struct svc_rqst
*rqstp
,
5153 struct nfsd4_compound_state
*cstate
,
5154 struct nfsd4_open_downgrade
*od
)
5157 struct nfs4_ol_stateid
*stp
;
5158 struct nfsd_net
*nn
= net_generic(SVC_NET(rqstp
), nfsd_net_id
);
5160 dprintk("NFSD: nfsd4_open_downgrade on file %pd\n",
5161 cstate
->current_fh
.fh_dentry
);
5163 /* We don't yet support WANT bits: */
5164 if (od
->od_deleg_want
)
5165 dprintk("NFSD: %s: od_deleg_want=0x%x ignored\n", __func__
,
5168 status
= nfs4_preprocess_confirmed_seqid_op(cstate
, od
->od_seqid
,
5169 &od
->od_stateid
, &stp
, nn
);
5172 status
= nfserr_inval
;
5173 if (!test_access(od
->od_share_access
, stp
)) {
5174 dprintk("NFSD: access not a subset of current bitmap: 0x%hhx, input access=%08x\n",
5175 stp
->st_access_bmap
, od
->od_share_access
);
5178 if (!test_deny(od
->od_share_deny
, stp
)) {
5179 dprintk("NFSD: deny not a subset of current bitmap: 0x%hhx, input deny=%08x\n",
5180 stp
->st_deny_bmap
, od
->od_share_deny
);
5183 nfs4_stateid_downgrade(stp
, od
->od_share_access
);
5184 reset_union_bmap_deny(od
->od_share_deny
, stp
);
5185 nfs4_inc_and_copy_stateid(&od
->od_stateid
, &stp
->st_stid
);
5188 mutex_unlock(&stp
->st_mutex
);
5189 nfs4_put_stid(&stp
->st_stid
);
5191 nfsd4_bump_seqid(cstate
, status
);
5195 static void nfsd4_close_open_stateid(struct nfs4_ol_stateid
*s
)
5197 struct nfs4_client
*clp
= s
->st_stid
.sc_client
;
5199 LIST_HEAD(reaplist
);
5201 spin_lock(&clp
->cl_lock
);
5202 unhashed
= unhash_open_stateid(s
, &reaplist
);
5204 if (clp
->cl_minorversion
) {
5206 put_ol_stateid_locked(s
, &reaplist
);
5207 spin_unlock(&clp
->cl_lock
);
5208 free_ol_stateid_reaplist(&reaplist
);
5210 spin_unlock(&clp
->cl_lock
);
5211 free_ol_stateid_reaplist(&reaplist
);
5213 move_to_close_lru(s
, clp
->net
);
5218 * nfs4_unlock_state() called after encode
5221 nfsd4_close(struct svc_rqst
*rqstp
, struct nfsd4_compound_state
*cstate
,
5222 struct nfsd4_close
*close
)
5225 struct nfs4_ol_stateid
*stp
;
5226 struct net
*net
= SVC_NET(rqstp
);
5227 struct nfsd_net
*nn
= net_generic(net
, nfsd_net_id
);
5229 dprintk("NFSD: nfsd4_close on file %pd\n",
5230 cstate
->current_fh
.fh_dentry
);
5232 status
= nfs4_preprocess_seqid_op(cstate
, close
->cl_seqid
,
5234 NFS4_OPEN_STID
|NFS4_CLOSED_STID
,
5236 nfsd4_bump_seqid(cstate
, status
);
5240 stp
->st_stid
.sc_type
= NFS4_CLOSED_STID
;
5241 nfs4_inc_and_copy_stateid(&close
->cl_stateid
, &stp
->st_stid
);
5243 nfsd4_close_open_stateid(stp
);
5244 mutex_unlock(&stp
->st_mutex
);
5246 /* See RFC5661 sectionm 18.2.4 */
5247 if (stp
->st_stid
.sc_client
->cl_minorversion
)
5248 memcpy(&close
->cl_stateid
, &close_stateid
,
5249 sizeof(close
->cl_stateid
));
5251 /* put reference from nfs4_preprocess_seqid_op */
5252 nfs4_put_stid(&stp
->st_stid
);
5258 nfsd4_delegreturn(struct svc_rqst
*rqstp
, struct nfsd4_compound_state
*cstate
,
5259 struct nfsd4_delegreturn
*dr
)
5261 struct nfs4_delegation
*dp
;
5262 stateid_t
*stateid
= &dr
->dr_stateid
;
5263 struct nfs4_stid
*s
;
5265 struct nfsd_net
*nn
= net_generic(SVC_NET(rqstp
), nfsd_net_id
);
5267 if ((status
= fh_verify(rqstp
, &cstate
->current_fh
, S_IFREG
, 0)))
5270 status
= nfsd4_lookup_stateid(cstate
, stateid
, NFS4_DELEG_STID
, &s
, nn
);
5273 dp
= delegstateid(s
);
5274 status
= check_stateid_generation(stateid
, &dp
->dl_stid
.sc_stateid
, nfsd4_has_session(cstate
));
5278 destroy_delegation(dp
);
5280 nfs4_put_stid(&dp
->dl_stid
);
5286 end_offset(u64 start
, u64 len
)
5291 return end
>= start
? end
: NFS4_MAX_UINT64
;
5294 /* last octet in a range */
5296 last_byte_offset(u64 start
, u64 len
)
5302 return end
> start
? end
- 1: NFS4_MAX_UINT64
;
5306 * TODO: Linux file offsets are _signed_ 64-bit quantities, which means that
5307 * we can't properly handle lock requests that go beyond the (2^63 - 1)-th
5308 * byte, because of sign extension problems. Since NFSv4 calls for 64-bit
5309 * locking, this prevents us from being completely protocol-compliant. The
5310 * real solution to this problem is to start using unsigned file offsets in
5311 * the VFS, but this is a very deep change!
5314 nfs4_transform_lock_offset(struct file_lock
*lock
)
5316 if (lock
->fl_start
< 0)
5317 lock
->fl_start
= OFFSET_MAX
;
5318 if (lock
->fl_end
< 0)
5319 lock
->fl_end
= OFFSET_MAX
;
5323 nfsd4_fl_get_owner(fl_owner_t owner
)
5325 struct nfs4_lockowner
*lo
= (struct nfs4_lockowner
*)owner
;
5327 nfs4_get_stateowner(&lo
->lo_owner
);
5332 nfsd4_fl_put_owner(fl_owner_t owner
)
5334 struct nfs4_lockowner
*lo
= (struct nfs4_lockowner
*)owner
;
5337 nfs4_put_stateowner(&lo
->lo_owner
);
5340 static const struct lock_manager_operations nfsd_posix_mng_ops
= {
5341 .lm_get_owner
= nfsd4_fl_get_owner
,
5342 .lm_put_owner
= nfsd4_fl_put_owner
,
5346 nfs4_set_lock_denied(struct file_lock
*fl
, struct nfsd4_lock_denied
*deny
)
5348 struct nfs4_lockowner
*lo
;
5350 if (fl
->fl_lmops
== &nfsd_posix_mng_ops
) {
5351 lo
= (struct nfs4_lockowner
*) fl
->fl_owner
;
5352 deny
->ld_owner
.data
= kmemdup(lo
->lo_owner
.so_owner
.data
,
5353 lo
->lo_owner
.so_owner
.len
, GFP_KERNEL
);
5354 if (!deny
->ld_owner
.data
)
5355 /* We just don't care that much */
5357 deny
->ld_owner
.len
= lo
->lo_owner
.so_owner
.len
;
5358 deny
->ld_clientid
= lo
->lo_owner
.so_client
->cl_clientid
;
5361 deny
->ld_owner
.len
= 0;
5362 deny
->ld_owner
.data
= NULL
;
5363 deny
->ld_clientid
.cl_boot
= 0;
5364 deny
->ld_clientid
.cl_id
= 0;
5366 deny
->ld_start
= fl
->fl_start
;
5367 deny
->ld_length
= NFS4_MAX_UINT64
;
5368 if (fl
->fl_end
!= NFS4_MAX_UINT64
)
5369 deny
->ld_length
= fl
->fl_end
- fl
->fl_start
+ 1;
5370 deny
->ld_type
= NFS4_READ_LT
;
5371 if (fl
->fl_type
!= F_RDLCK
)
5372 deny
->ld_type
= NFS4_WRITE_LT
;
5375 static struct nfs4_lockowner
*
5376 find_lockowner_str_locked(struct nfs4_client
*clp
, struct xdr_netobj
*owner
)
5378 unsigned int strhashval
= ownerstr_hashval(owner
);
5379 struct nfs4_stateowner
*so
;
5381 lockdep_assert_held(&clp
->cl_lock
);
5383 list_for_each_entry(so
, &clp
->cl_ownerstr_hashtbl
[strhashval
],
5385 if (so
->so_is_open_owner
)
5387 if (same_owner_str(so
, owner
))
5388 return lockowner(nfs4_get_stateowner(so
));
5393 static struct nfs4_lockowner
*
5394 find_lockowner_str(struct nfs4_client
*clp
, struct xdr_netobj
*owner
)
5396 struct nfs4_lockowner
*lo
;
5398 spin_lock(&clp
->cl_lock
);
5399 lo
= find_lockowner_str_locked(clp
, owner
);
5400 spin_unlock(&clp
->cl_lock
);
5404 static void nfs4_unhash_lockowner(struct nfs4_stateowner
*sop
)
5406 unhash_lockowner_locked(lockowner(sop
));
5409 static void nfs4_free_lockowner(struct nfs4_stateowner
*sop
)
5411 struct nfs4_lockowner
*lo
= lockowner(sop
);
5413 kmem_cache_free(lockowner_slab
, lo
);
5416 static const struct nfs4_stateowner_operations lockowner_ops
= {
5417 .so_unhash
= nfs4_unhash_lockowner
,
5418 .so_free
= nfs4_free_lockowner
,
5422 * Alloc a lock owner structure.
5423 * Called in nfsd4_lock - therefore, OPEN and OPEN_CONFIRM (if needed) has
5426 * strhashval = ownerstr_hashval
5428 static struct nfs4_lockowner
*
5429 alloc_init_lock_stateowner(unsigned int strhashval
, struct nfs4_client
*clp
,
5430 struct nfs4_ol_stateid
*open_stp
,
5431 struct nfsd4_lock
*lock
)
5433 struct nfs4_lockowner
*lo
, *ret
;
5435 lo
= alloc_stateowner(lockowner_slab
, &lock
->lk_new_owner
, clp
);
5438 INIT_LIST_HEAD(&lo
->lo_owner
.so_stateids
);
5439 lo
->lo_owner
.so_is_open_owner
= 0;
5440 lo
->lo_owner
.so_seqid
= lock
->lk_new_lock_seqid
;
5441 lo
->lo_owner
.so_ops
= &lockowner_ops
;
5442 spin_lock(&clp
->cl_lock
);
5443 ret
= find_lockowner_str_locked(clp
, &lock
->lk_new_owner
);
5445 list_add(&lo
->lo_owner
.so_strhash
,
5446 &clp
->cl_ownerstr_hashtbl
[strhashval
]);
5449 nfs4_free_stateowner(&lo
->lo_owner
);
5451 spin_unlock(&clp
->cl_lock
);
5456 init_lock_stateid(struct nfs4_ol_stateid
*stp
, struct nfs4_lockowner
*lo
,
5457 struct nfs4_file
*fp
, struct inode
*inode
,
5458 struct nfs4_ol_stateid
*open_stp
)
5460 struct nfs4_client
*clp
= lo
->lo_owner
.so_client
;
5462 lockdep_assert_held(&clp
->cl_lock
);
5464 atomic_inc(&stp
->st_stid
.sc_count
);
5465 stp
->st_stid
.sc_type
= NFS4_LOCK_STID
;
5466 stp
->st_stateowner
= nfs4_get_stateowner(&lo
->lo_owner
);
5468 stp
->st_stid
.sc_file
= fp
;
5469 stp
->st_access_bmap
= 0;
5470 stp
->st_deny_bmap
= open_stp
->st_deny_bmap
;
5471 stp
->st_openstp
= open_stp
;
5472 mutex_init(&stp
->st_mutex
);
5473 list_add(&stp
->st_locks
, &open_stp
->st_locks
);
5474 list_add(&stp
->st_perstateowner
, &lo
->lo_owner
.so_stateids
);
5475 spin_lock(&fp
->fi_lock
);
5476 list_add(&stp
->st_perfile
, &fp
->fi_stateids
);
5477 spin_unlock(&fp
->fi_lock
);
5480 static struct nfs4_ol_stateid
*
5481 find_lock_stateid(struct nfs4_lockowner
*lo
, struct nfs4_file
*fp
)
5483 struct nfs4_ol_stateid
*lst
;
5484 struct nfs4_client
*clp
= lo
->lo_owner
.so_client
;
5486 lockdep_assert_held(&clp
->cl_lock
);
5488 list_for_each_entry(lst
, &lo
->lo_owner
.so_stateids
, st_perstateowner
) {
5489 if (lst
->st_stid
.sc_file
== fp
) {
5490 atomic_inc(&lst
->st_stid
.sc_count
);
5497 static struct nfs4_ol_stateid
*
5498 find_or_create_lock_stateid(struct nfs4_lockowner
*lo
, struct nfs4_file
*fi
,
5499 struct inode
*inode
, struct nfs4_ol_stateid
*ost
,
5502 struct nfs4_stid
*ns
= NULL
;
5503 struct nfs4_ol_stateid
*lst
;
5504 struct nfs4_openowner
*oo
= openowner(ost
->st_stateowner
);
5505 struct nfs4_client
*clp
= oo
->oo_owner
.so_client
;
5507 spin_lock(&clp
->cl_lock
);
5508 lst
= find_lock_stateid(lo
, fi
);
5510 spin_unlock(&clp
->cl_lock
);
5511 ns
= nfs4_alloc_stid(clp
, stateid_slab
, nfs4_free_lock_stateid
);
5515 spin_lock(&clp
->cl_lock
);
5516 lst
= find_lock_stateid(lo
, fi
);
5518 lst
= openlockstateid(ns
);
5519 init_lock_stateid(lst
, lo
, fi
, inode
, ost
);
5524 spin_unlock(&clp
->cl_lock
);
5531 check_lock_length(u64 offset
, u64 length
)
5533 return ((length
== 0) || ((length
!= NFS4_MAX_UINT64
) &&
5534 (length
> ~offset
)));
5537 static void get_lock_access(struct nfs4_ol_stateid
*lock_stp
, u32 access
)
5539 struct nfs4_file
*fp
= lock_stp
->st_stid
.sc_file
;
5541 lockdep_assert_held(&fp
->fi_lock
);
5543 if (test_access(access
, lock_stp
))
5545 __nfs4_file_get_access(fp
, access
);
5546 set_access(access
, lock_stp
);
5550 lookup_or_create_lock_state(struct nfsd4_compound_state
*cstate
,
5551 struct nfs4_ol_stateid
*ost
,
5552 struct nfsd4_lock
*lock
,
5553 struct nfs4_ol_stateid
**plst
, bool *new)
5556 struct nfs4_file
*fi
= ost
->st_stid
.sc_file
;
5557 struct nfs4_openowner
*oo
= openowner(ost
->st_stateowner
);
5558 struct nfs4_client
*cl
= oo
->oo_owner
.so_client
;
5559 struct inode
*inode
= d_inode(cstate
->current_fh
.fh_dentry
);
5560 struct nfs4_lockowner
*lo
;
5561 struct nfs4_ol_stateid
*lst
;
5562 unsigned int strhashval
;
5565 lo
= find_lockowner_str(cl
, &lock
->lk_new_owner
);
5567 strhashval
= ownerstr_hashval(&lock
->lk_new_owner
);
5568 lo
= alloc_init_lock_stateowner(strhashval
, cl
, ost
, lock
);
5570 return nfserr_jukebox
;
5572 /* with an existing lockowner, seqids must be the same */
5573 status
= nfserr_bad_seqid
;
5574 if (!cstate
->minorversion
&&
5575 lock
->lk_new_lock_seqid
!= lo
->lo_owner
.so_seqid
)
5580 lst
= find_or_create_lock_stateid(lo
, fi
, inode
, ost
, new);
5582 status
= nfserr_jukebox
;
5586 mutex_lock(&lst
->st_mutex
);
5588 /* See if it's still hashed to avoid race with FREE_STATEID */
5589 spin_lock(&cl
->cl_lock
);
5590 hashed
= !list_empty(&lst
->st_perfile
);
5591 spin_unlock(&cl
->cl_lock
);
5594 mutex_unlock(&lst
->st_mutex
);
5595 nfs4_put_stid(&lst
->st_stid
);
5601 nfs4_put_stateowner(&lo
->lo_owner
);
5609 nfsd4_lock(struct svc_rqst
*rqstp
, struct nfsd4_compound_state
*cstate
,
5610 struct nfsd4_lock
*lock
)
5612 struct nfs4_openowner
*open_sop
= NULL
;
5613 struct nfs4_lockowner
*lock_sop
= NULL
;
5614 struct nfs4_ol_stateid
*lock_stp
= NULL
;
5615 struct nfs4_ol_stateid
*open_stp
= NULL
;
5616 struct nfs4_file
*fp
;
5617 struct file
*filp
= NULL
;
5618 struct file_lock
*file_lock
= NULL
;
5619 struct file_lock
*conflock
= NULL
;
5624 struct net
*net
= SVC_NET(rqstp
);
5625 struct nfsd_net
*nn
= net_generic(net
, nfsd_net_id
);
5627 dprintk("NFSD: nfsd4_lock: start=%Ld length=%Ld\n",
5628 (long long) lock
->lk_offset
,
5629 (long long) lock
->lk_length
);
5631 if (check_lock_length(lock
->lk_offset
, lock
->lk_length
))
5632 return nfserr_inval
;
5634 if ((status
= fh_verify(rqstp
, &cstate
->current_fh
,
5635 S_IFREG
, NFSD_MAY_LOCK
))) {
5636 dprintk("NFSD: nfsd4_lock: permission denied!\n");
5640 if (lock
->lk_is_new
) {
5641 if (nfsd4_has_session(cstate
))
5642 /* See rfc 5661 18.10.3: given clientid is ignored: */
5643 memcpy(&lock
->lk_new_clientid
,
5644 &cstate
->session
->se_client
->cl_clientid
,
5645 sizeof(clientid_t
));
5647 status
= nfserr_stale_clientid
;
5648 if (STALE_CLIENTID(&lock
->lk_new_clientid
, nn
))
5651 /* validate and update open stateid and open seqid */
5652 status
= nfs4_preprocess_confirmed_seqid_op(cstate
,
5653 lock
->lk_new_open_seqid
,
5654 &lock
->lk_new_open_stateid
,
5658 mutex_unlock(&open_stp
->st_mutex
);
5659 open_sop
= openowner(open_stp
->st_stateowner
);
5660 status
= nfserr_bad_stateid
;
5661 if (!same_clid(&open_sop
->oo_owner
.so_client
->cl_clientid
,
5662 &lock
->lk_new_clientid
))
5664 status
= lookup_or_create_lock_state(cstate
, open_stp
, lock
,
5667 status
= nfs4_preprocess_seqid_op(cstate
,
5668 lock
->lk_old_lock_seqid
,
5669 &lock
->lk_old_lock_stateid
,
5670 NFS4_LOCK_STID
, &lock_stp
, nn
);
5674 lock_sop
= lockowner(lock_stp
->st_stateowner
);
5676 lkflg
= setlkflg(lock
->lk_type
);
5677 status
= nfs4_check_openmode(lock_stp
, lkflg
);
5681 status
= nfserr_grace
;
5682 if (locks_in_grace(net
) && !lock
->lk_reclaim
)
5684 status
= nfserr_no_grace
;
5685 if (!locks_in_grace(net
) && lock
->lk_reclaim
)
5688 file_lock
= locks_alloc_lock();
5690 dprintk("NFSD: %s: unable to allocate lock!\n", __func__
);
5691 status
= nfserr_jukebox
;
5695 fp
= lock_stp
->st_stid
.sc_file
;
5696 switch (lock
->lk_type
) {
5699 spin_lock(&fp
->fi_lock
);
5700 filp
= find_readable_file_locked(fp
);
5702 get_lock_access(lock_stp
, NFS4_SHARE_ACCESS_READ
);
5703 spin_unlock(&fp
->fi_lock
);
5704 file_lock
->fl_type
= F_RDLCK
;
5707 case NFS4_WRITEW_LT
:
5708 spin_lock(&fp
->fi_lock
);
5709 filp
= find_writeable_file_locked(fp
);
5711 get_lock_access(lock_stp
, NFS4_SHARE_ACCESS_WRITE
);
5712 spin_unlock(&fp
->fi_lock
);
5713 file_lock
->fl_type
= F_WRLCK
;
5716 status
= nfserr_inval
;
5720 status
= nfserr_openmode
;
5724 file_lock
->fl_owner
= (fl_owner_t
)lockowner(nfs4_get_stateowner(&lock_sop
->lo_owner
));
5725 file_lock
->fl_pid
= current
->tgid
;
5726 file_lock
->fl_file
= filp
;
5727 file_lock
->fl_flags
= FL_POSIX
;
5728 file_lock
->fl_lmops
= &nfsd_posix_mng_ops
;
5729 file_lock
->fl_start
= lock
->lk_offset
;
5730 file_lock
->fl_end
= last_byte_offset(lock
->lk_offset
, lock
->lk_length
);
5731 nfs4_transform_lock_offset(file_lock
);
5733 conflock
= locks_alloc_lock();
5735 dprintk("NFSD: %s: unable to allocate lock!\n", __func__
);
5736 status
= nfserr_jukebox
;
5740 err
= vfs_lock_file(filp
, F_SETLK
, file_lock
, conflock
);
5742 case 0: /* success! */
5743 nfs4_inc_and_copy_stateid(&lock
->lk_resp_stateid
, &lock_stp
->st_stid
);
5746 case (EAGAIN
): /* conflock holds conflicting lock */
5747 status
= nfserr_denied
;
5748 dprintk("NFSD: nfsd4_lock: conflicting lock found!\n");
5749 nfs4_set_lock_denied(conflock
, &lock
->lk_denied
);
5752 status
= nfserr_deadlock
;
5755 dprintk("NFSD: nfsd4_lock: vfs_lock_file() failed! status %d\n",err
);
5756 status
= nfserrno(err
);
5763 /* Bump seqid manually if the 4.0 replay owner is openowner */
5764 if (cstate
->replay_owner
&&
5765 cstate
->replay_owner
!= &lock_sop
->lo_owner
&&
5766 seqid_mutating_err(ntohl(status
)))
5767 lock_sop
->lo_owner
.so_seqid
++;
5769 mutex_unlock(&lock_stp
->st_mutex
);
5772 * If this is a new, never-before-used stateid, and we are
5773 * returning an error, then just go ahead and release it.
5776 release_lock_stateid(lock_stp
);
5778 nfs4_put_stid(&lock_stp
->st_stid
);
5781 nfs4_put_stid(&open_stp
->st_stid
);
5782 nfsd4_bump_seqid(cstate
, status
);
5784 locks_free_lock(file_lock
);
5786 locks_free_lock(conflock
);
5791 * The NFSv4 spec allows a client to do a LOCKT without holding an OPEN,
5792 * so we do a temporary open here just to get an open file to pass to
5793 * vfs_test_lock. (Arguably perhaps test_lock should be done with an
5796 static __be32
nfsd_test_lock(struct svc_rqst
*rqstp
, struct svc_fh
*fhp
, struct file_lock
*lock
)
5799 __be32 err
= nfsd_open(rqstp
, fhp
, S_IFREG
, NFSD_MAY_READ
, &file
);
5801 err
= nfserrno(vfs_test_lock(file
, lock
));
5811 nfsd4_lockt(struct svc_rqst
*rqstp
, struct nfsd4_compound_state
*cstate
,
5812 struct nfsd4_lockt
*lockt
)
5814 struct file_lock
*file_lock
= NULL
;
5815 struct nfs4_lockowner
*lo
= NULL
;
5817 struct nfsd_net
*nn
= net_generic(SVC_NET(rqstp
), nfsd_net_id
);
5819 if (locks_in_grace(SVC_NET(rqstp
)))
5820 return nfserr_grace
;
5822 if (check_lock_length(lockt
->lt_offset
, lockt
->lt_length
))
5823 return nfserr_inval
;
5825 if (!nfsd4_has_session(cstate
)) {
5826 status
= lookup_clientid(&lockt
->lt_clientid
, cstate
, nn
);
5831 if ((status
= fh_verify(rqstp
, &cstate
->current_fh
, S_IFREG
, 0)))
5834 file_lock
= locks_alloc_lock();
5836 dprintk("NFSD: %s: unable to allocate lock!\n", __func__
);
5837 status
= nfserr_jukebox
;
5841 switch (lockt
->lt_type
) {
5844 file_lock
->fl_type
= F_RDLCK
;
5847 case NFS4_WRITEW_LT
:
5848 file_lock
->fl_type
= F_WRLCK
;
5851 dprintk("NFSD: nfs4_lockt: bad lock type!\n");
5852 status
= nfserr_inval
;
5856 lo
= find_lockowner_str(cstate
->clp
, &lockt
->lt_owner
);
5858 file_lock
->fl_owner
= (fl_owner_t
)lo
;
5859 file_lock
->fl_pid
= current
->tgid
;
5860 file_lock
->fl_flags
= FL_POSIX
;
5862 file_lock
->fl_start
= lockt
->lt_offset
;
5863 file_lock
->fl_end
= last_byte_offset(lockt
->lt_offset
, lockt
->lt_length
);
5865 nfs4_transform_lock_offset(file_lock
);
5867 status
= nfsd_test_lock(rqstp
, &cstate
->current_fh
, file_lock
);
5871 if (file_lock
->fl_type
!= F_UNLCK
) {
5872 status
= nfserr_denied
;
5873 nfs4_set_lock_denied(file_lock
, &lockt
->lt_denied
);
5877 nfs4_put_stateowner(&lo
->lo_owner
);
5879 locks_free_lock(file_lock
);
5884 nfsd4_locku(struct svc_rqst
*rqstp
, struct nfsd4_compound_state
*cstate
,
5885 struct nfsd4_locku
*locku
)
5887 struct nfs4_ol_stateid
*stp
;
5888 struct file
*filp
= NULL
;
5889 struct file_lock
*file_lock
= NULL
;
5892 struct nfsd_net
*nn
= net_generic(SVC_NET(rqstp
), nfsd_net_id
);
5894 dprintk("NFSD: nfsd4_locku: start=%Ld length=%Ld\n",
5895 (long long) locku
->lu_offset
,
5896 (long long) locku
->lu_length
);
5898 if (check_lock_length(locku
->lu_offset
, locku
->lu_length
))
5899 return nfserr_inval
;
5901 status
= nfs4_preprocess_seqid_op(cstate
, locku
->lu_seqid
,
5902 &locku
->lu_stateid
, NFS4_LOCK_STID
,
5906 filp
= find_any_file(stp
->st_stid
.sc_file
);
5908 status
= nfserr_lock_range
;
5911 file_lock
= locks_alloc_lock();
5913 dprintk("NFSD: %s: unable to allocate lock!\n", __func__
);
5914 status
= nfserr_jukebox
;
5918 file_lock
->fl_type
= F_UNLCK
;
5919 file_lock
->fl_owner
= (fl_owner_t
)lockowner(nfs4_get_stateowner(stp
->st_stateowner
));
5920 file_lock
->fl_pid
= current
->tgid
;
5921 file_lock
->fl_file
= filp
;
5922 file_lock
->fl_flags
= FL_POSIX
;
5923 file_lock
->fl_lmops
= &nfsd_posix_mng_ops
;
5924 file_lock
->fl_start
= locku
->lu_offset
;
5926 file_lock
->fl_end
= last_byte_offset(locku
->lu_offset
,
5928 nfs4_transform_lock_offset(file_lock
);
5930 err
= vfs_lock_file(filp
, F_SETLK
, file_lock
, NULL
);
5932 dprintk("NFSD: nfs4_locku: vfs_lock_file failed!\n");
5935 nfs4_inc_and_copy_stateid(&locku
->lu_stateid
, &stp
->st_stid
);
5939 mutex_unlock(&stp
->st_mutex
);
5940 nfs4_put_stid(&stp
->st_stid
);
5942 nfsd4_bump_seqid(cstate
, status
);
5944 locks_free_lock(file_lock
);
5948 status
= nfserrno(err
);
5954 * true: locks held by lockowner
5955 * false: no locks held by lockowner
5958 check_for_locks(struct nfs4_file
*fp
, struct nfs4_lockowner
*lowner
)
5960 struct file_lock
*fl
;
5962 struct file
*filp
= find_any_file(fp
);
5963 struct inode
*inode
;
5964 struct file_lock_context
*flctx
;
5967 /* Any valid lock stateid should have some sort of access */
5972 inode
= file_inode(filp
);
5973 flctx
= inode
->i_flctx
;
5975 if (flctx
&& !list_empty_careful(&flctx
->flc_posix
)) {
5976 spin_lock(&flctx
->flc_lock
);
5977 list_for_each_entry(fl
, &flctx
->flc_posix
, fl_list
) {
5978 if (fl
->fl_owner
== (fl_owner_t
)lowner
) {
5983 spin_unlock(&flctx
->flc_lock
);
5990 nfsd4_release_lockowner(struct svc_rqst
*rqstp
,
5991 struct nfsd4_compound_state
*cstate
,
5992 struct nfsd4_release_lockowner
*rlockowner
)
5994 clientid_t
*clid
= &rlockowner
->rl_clientid
;
5995 struct nfs4_stateowner
*sop
;
5996 struct nfs4_lockowner
*lo
= NULL
;
5997 struct nfs4_ol_stateid
*stp
;
5998 struct xdr_netobj
*owner
= &rlockowner
->rl_owner
;
5999 unsigned int hashval
= ownerstr_hashval(owner
);
6001 struct nfsd_net
*nn
= net_generic(SVC_NET(rqstp
), nfsd_net_id
);
6002 struct nfs4_client
*clp
;
6003 LIST_HEAD (reaplist
);
6005 dprintk("nfsd4_release_lockowner clientid: (%08x/%08x):\n",
6006 clid
->cl_boot
, clid
->cl_id
);
6008 status
= lookup_clientid(clid
, cstate
, nn
);
6013 /* Find the matching lock stateowner */
6014 spin_lock(&clp
->cl_lock
);
6015 list_for_each_entry(sop
, &clp
->cl_ownerstr_hashtbl
[hashval
],
6018 if (sop
->so_is_open_owner
|| !same_owner_str(sop
, owner
))
6021 /* see if there are still any locks associated with it */
6022 lo
= lockowner(sop
);
6023 list_for_each_entry(stp
, &sop
->so_stateids
, st_perstateowner
) {
6024 if (check_for_locks(stp
->st_stid
.sc_file
, lo
)) {
6025 status
= nfserr_locks_held
;
6026 spin_unlock(&clp
->cl_lock
);
6031 nfs4_get_stateowner(sop
);
6035 spin_unlock(&clp
->cl_lock
);
6039 unhash_lockowner_locked(lo
);
6040 while (!list_empty(&lo
->lo_owner
.so_stateids
)) {
6041 stp
= list_first_entry(&lo
->lo_owner
.so_stateids
,
6042 struct nfs4_ol_stateid
,
6044 WARN_ON(!unhash_lock_stateid(stp
));
6045 put_ol_stateid_locked(stp
, &reaplist
);
6047 spin_unlock(&clp
->cl_lock
);
6048 free_ol_stateid_reaplist(&reaplist
);
6049 nfs4_put_stateowner(&lo
->lo_owner
);
6054 static inline struct nfs4_client_reclaim
*
6057 return kmalloc(sizeof(struct nfs4_client_reclaim
), GFP_KERNEL
);
6061 nfs4_has_reclaimed_state(const char *name
, struct nfsd_net
*nn
)
6063 struct nfs4_client_reclaim
*crp
;
6065 crp
= nfsd4_find_reclaim_client(name
, nn
);
6066 return (crp
&& crp
->cr_clp
);
6070 * failure => all reset bets are off, nfserr_no_grace...
6072 struct nfs4_client_reclaim
*
6073 nfs4_client_to_reclaim(const char *name
, struct nfsd_net
*nn
)
6075 unsigned int strhashval
;
6076 struct nfs4_client_reclaim
*crp
;
6078 dprintk("NFSD nfs4_client_to_reclaim NAME: %.*s\n", HEXDIR_LEN
, name
);
6079 crp
= alloc_reclaim();
6081 strhashval
= clientstr_hashval(name
);
6082 INIT_LIST_HEAD(&crp
->cr_strhash
);
6083 list_add(&crp
->cr_strhash
, &nn
->reclaim_str_hashtbl
[strhashval
]);
6084 memcpy(crp
->cr_recdir
, name
, HEXDIR_LEN
);
6086 nn
->reclaim_str_hashtbl_size
++;
6092 nfs4_remove_reclaim_record(struct nfs4_client_reclaim
*crp
, struct nfsd_net
*nn
)
6094 list_del(&crp
->cr_strhash
);
6096 nn
->reclaim_str_hashtbl_size
--;
6100 nfs4_release_reclaim(struct nfsd_net
*nn
)
6102 struct nfs4_client_reclaim
*crp
= NULL
;
6105 for (i
= 0; i
< CLIENT_HASH_SIZE
; i
++) {
6106 while (!list_empty(&nn
->reclaim_str_hashtbl
[i
])) {
6107 crp
= list_entry(nn
->reclaim_str_hashtbl
[i
].next
,
6108 struct nfs4_client_reclaim
, cr_strhash
);
6109 nfs4_remove_reclaim_record(crp
, nn
);
6112 WARN_ON_ONCE(nn
->reclaim_str_hashtbl_size
);
6116 * called from OPEN, CLAIM_PREVIOUS with a new clientid. */
6117 struct nfs4_client_reclaim
*
6118 nfsd4_find_reclaim_client(const char *recdir
, struct nfsd_net
*nn
)
6120 unsigned int strhashval
;
6121 struct nfs4_client_reclaim
*crp
= NULL
;
6123 dprintk("NFSD: nfs4_find_reclaim_client for recdir %s\n", recdir
);
6125 strhashval
= clientstr_hashval(recdir
);
6126 list_for_each_entry(crp
, &nn
->reclaim_str_hashtbl
[strhashval
], cr_strhash
) {
6127 if (same_name(crp
->cr_recdir
, recdir
)) {
6135 * Called from OPEN. Look for clientid in reclaim list.
6138 nfs4_check_open_reclaim(clientid_t
*clid
,
6139 struct nfsd4_compound_state
*cstate
,
6140 struct nfsd_net
*nn
)
6144 /* find clientid in conf_id_hashtbl */
6145 status
= lookup_clientid(clid
, cstate
, nn
);
6147 return nfserr_reclaim_bad
;
6149 if (test_bit(NFSD4_CLIENT_RECLAIM_COMPLETE
, &cstate
->clp
->cl_flags
))
6150 return nfserr_no_grace
;
6152 if (nfsd4_client_record_check(cstate
->clp
))
6153 return nfserr_reclaim_bad
;
6158 #ifdef CONFIG_NFSD_FAULT_INJECTION
6160 put_client(struct nfs4_client
*clp
)
6162 atomic_dec(&clp
->cl_refcount
);
6165 static struct nfs4_client
*
6166 nfsd_find_client(struct sockaddr_storage
*addr
, size_t addr_size
)
6168 struct nfs4_client
*clp
;
6169 struct nfsd_net
*nn
= net_generic(current
->nsproxy
->net_ns
,
6172 if (!nfsd_netns_ready(nn
))
6175 list_for_each_entry(clp
, &nn
->client_lru
, cl_lru
) {
6176 if (memcmp(&clp
->cl_addr
, addr
, addr_size
) == 0)
6183 nfsd_inject_print_clients(void)
6185 struct nfs4_client
*clp
;
6187 struct nfsd_net
*nn
= net_generic(current
->nsproxy
->net_ns
,
6189 char buf
[INET6_ADDRSTRLEN
];
6191 if (!nfsd_netns_ready(nn
))
6194 spin_lock(&nn
->client_lock
);
6195 list_for_each_entry(clp
, &nn
->client_lru
, cl_lru
) {
6196 rpc_ntop((struct sockaddr
*)&clp
->cl_addr
, buf
, sizeof(buf
));
6197 pr_info("NFS Client: %s\n", buf
);
6200 spin_unlock(&nn
->client_lock
);
6206 nfsd_inject_forget_client(struct sockaddr_storage
*addr
, size_t addr_size
)
6209 struct nfs4_client
*clp
;
6210 struct nfsd_net
*nn
= net_generic(current
->nsproxy
->net_ns
,
6213 if (!nfsd_netns_ready(nn
))
6216 spin_lock(&nn
->client_lock
);
6217 clp
= nfsd_find_client(addr
, addr_size
);
6219 if (mark_client_expired_locked(clp
) == nfs_ok
)
6224 spin_unlock(&nn
->client_lock
);
6233 nfsd_inject_forget_clients(u64 max
)
6236 struct nfs4_client
*clp
, *next
;
6237 struct nfsd_net
*nn
= net_generic(current
->nsproxy
->net_ns
,
6239 LIST_HEAD(reaplist
);
6241 if (!nfsd_netns_ready(nn
))
6244 spin_lock(&nn
->client_lock
);
6245 list_for_each_entry_safe(clp
, next
, &nn
->client_lru
, cl_lru
) {
6246 if (mark_client_expired_locked(clp
) == nfs_ok
) {
6247 list_add(&clp
->cl_lru
, &reaplist
);
6248 if (max
!= 0 && ++count
>= max
)
6252 spin_unlock(&nn
->client_lock
);
6254 list_for_each_entry_safe(clp
, next
, &reaplist
, cl_lru
)
6260 static void nfsd_print_count(struct nfs4_client
*clp
, unsigned int count
,
6263 char buf
[INET6_ADDRSTRLEN
];
6264 rpc_ntop((struct sockaddr
*)&clp
->cl_addr
, buf
, sizeof(buf
));
6265 printk(KERN_INFO
"NFS Client: %s has %u %s\n", buf
, count
, type
);
6269 nfsd_inject_add_lock_to_list(struct nfs4_ol_stateid
*lst
,
6270 struct list_head
*collect
)
6272 struct nfs4_client
*clp
= lst
->st_stid
.sc_client
;
6273 struct nfsd_net
*nn
= net_generic(current
->nsproxy
->net_ns
,
6279 lockdep_assert_held(&nn
->client_lock
);
6280 atomic_inc(&clp
->cl_refcount
);
6281 list_add(&lst
->st_locks
, collect
);
6284 static u64
nfsd_foreach_client_lock(struct nfs4_client
*clp
, u64 max
,
6285 struct list_head
*collect
,
6286 bool (*func
)(struct nfs4_ol_stateid
*))
6288 struct nfs4_openowner
*oop
;
6289 struct nfs4_ol_stateid
*stp
, *st_next
;
6290 struct nfs4_ol_stateid
*lst
, *lst_next
;
6293 spin_lock(&clp
->cl_lock
);
6294 list_for_each_entry(oop
, &clp
->cl_openowners
, oo_perclient
) {
6295 list_for_each_entry_safe(stp
, st_next
,
6296 &oop
->oo_owner
.so_stateids
, st_perstateowner
) {
6297 list_for_each_entry_safe(lst
, lst_next
,
6298 &stp
->st_locks
, st_locks
) {
6301 nfsd_inject_add_lock_to_list(lst
,
6306 * Despite the fact that these functions deal
6307 * with 64-bit integers for "count", we must
6308 * ensure that it doesn't blow up the
6309 * clp->cl_refcount. Throw a warning if we
6310 * start to approach INT_MAX here.
6312 WARN_ON_ONCE(count
== (INT_MAX
/ 2));
6319 spin_unlock(&clp
->cl_lock
);
6325 nfsd_collect_client_locks(struct nfs4_client
*clp
, struct list_head
*collect
,
6328 return nfsd_foreach_client_lock(clp
, max
, collect
, unhash_lock_stateid
);
6332 nfsd_print_client_locks(struct nfs4_client
*clp
)
6334 u64 count
= nfsd_foreach_client_lock(clp
, 0, NULL
, NULL
);
6335 nfsd_print_count(clp
, count
, "locked files");
6340 nfsd_inject_print_locks(void)
6342 struct nfs4_client
*clp
;
6344 struct nfsd_net
*nn
= net_generic(current
->nsproxy
->net_ns
,
6347 if (!nfsd_netns_ready(nn
))
6350 spin_lock(&nn
->client_lock
);
6351 list_for_each_entry(clp
, &nn
->client_lru
, cl_lru
)
6352 count
+= nfsd_print_client_locks(clp
);
6353 spin_unlock(&nn
->client_lock
);
6359 nfsd_reap_locks(struct list_head
*reaplist
)
6361 struct nfs4_client
*clp
;
6362 struct nfs4_ol_stateid
*stp
, *next
;
6364 list_for_each_entry_safe(stp
, next
, reaplist
, st_locks
) {
6365 list_del_init(&stp
->st_locks
);
6366 clp
= stp
->st_stid
.sc_client
;
6367 nfs4_put_stid(&stp
->st_stid
);
6373 nfsd_inject_forget_client_locks(struct sockaddr_storage
*addr
, size_t addr_size
)
6375 unsigned int count
= 0;
6376 struct nfs4_client
*clp
;
6377 struct nfsd_net
*nn
= net_generic(current
->nsproxy
->net_ns
,
6379 LIST_HEAD(reaplist
);
6381 if (!nfsd_netns_ready(nn
))
6384 spin_lock(&nn
->client_lock
);
6385 clp
= nfsd_find_client(addr
, addr_size
);
6387 count
= nfsd_collect_client_locks(clp
, &reaplist
, 0);
6388 spin_unlock(&nn
->client_lock
);
6389 nfsd_reap_locks(&reaplist
);
6394 nfsd_inject_forget_locks(u64 max
)
6397 struct nfs4_client
*clp
;
6398 struct nfsd_net
*nn
= net_generic(current
->nsproxy
->net_ns
,
6400 LIST_HEAD(reaplist
);
6402 if (!nfsd_netns_ready(nn
))
6405 spin_lock(&nn
->client_lock
);
6406 list_for_each_entry(clp
, &nn
->client_lru
, cl_lru
) {
6407 count
+= nfsd_collect_client_locks(clp
, &reaplist
, max
- count
);
6408 if (max
!= 0 && count
>= max
)
6411 spin_unlock(&nn
->client_lock
);
6412 nfsd_reap_locks(&reaplist
);
6417 nfsd_foreach_client_openowner(struct nfs4_client
*clp
, u64 max
,
6418 struct list_head
*collect
,
6419 void (*func
)(struct nfs4_openowner
*))
6421 struct nfs4_openowner
*oop
, *next
;
6422 struct nfsd_net
*nn
= net_generic(current
->nsproxy
->net_ns
,
6426 lockdep_assert_held(&nn
->client_lock
);
6428 spin_lock(&clp
->cl_lock
);
6429 list_for_each_entry_safe(oop
, next
, &clp
->cl_openowners
, oo_perclient
) {
6433 atomic_inc(&clp
->cl_refcount
);
6434 list_add(&oop
->oo_perclient
, collect
);
6439 * Despite the fact that these functions deal with
6440 * 64-bit integers for "count", we must ensure that
6441 * it doesn't blow up the clp->cl_refcount. Throw a
6442 * warning if we start to approach INT_MAX here.
6444 WARN_ON_ONCE(count
== (INT_MAX
/ 2));
6448 spin_unlock(&clp
->cl_lock
);
6454 nfsd_print_client_openowners(struct nfs4_client
*clp
)
6456 u64 count
= nfsd_foreach_client_openowner(clp
, 0, NULL
, NULL
);
6458 nfsd_print_count(clp
, count
, "openowners");
6463 nfsd_collect_client_openowners(struct nfs4_client
*clp
,
6464 struct list_head
*collect
, u64 max
)
6466 return nfsd_foreach_client_openowner(clp
, max
, collect
,
6467 unhash_openowner_locked
);
6471 nfsd_inject_print_openowners(void)
6473 struct nfs4_client
*clp
;
6475 struct nfsd_net
*nn
= net_generic(current
->nsproxy
->net_ns
,
6478 if (!nfsd_netns_ready(nn
))
6481 spin_lock(&nn
->client_lock
);
6482 list_for_each_entry(clp
, &nn
->client_lru
, cl_lru
)
6483 count
+= nfsd_print_client_openowners(clp
);
6484 spin_unlock(&nn
->client_lock
);
6490 nfsd_reap_openowners(struct list_head
*reaplist
)
6492 struct nfs4_client
*clp
;
6493 struct nfs4_openowner
*oop
, *next
;
6495 list_for_each_entry_safe(oop
, next
, reaplist
, oo_perclient
) {
6496 list_del_init(&oop
->oo_perclient
);
6497 clp
= oop
->oo_owner
.so_client
;
6498 release_openowner(oop
);
6504 nfsd_inject_forget_client_openowners(struct sockaddr_storage
*addr
,
6507 unsigned int count
= 0;
6508 struct nfs4_client
*clp
;
6509 struct nfsd_net
*nn
= net_generic(current
->nsproxy
->net_ns
,
6511 LIST_HEAD(reaplist
);
6513 if (!nfsd_netns_ready(nn
))
6516 spin_lock(&nn
->client_lock
);
6517 clp
= nfsd_find_client(addr
, addr_size
);
6519 count
= nfsd_collect_client_openowners(clp
, &reaplist
, 0);
6520 spin_unlock(&nn
->client_lock
);
6521 nfsd_reap_openowners(&reaplist
);
6526 nfsd_inject_forget_openowners(u64 max
)
6529 struct nfs4_client
*clp
;
6530 struct nfsd_net
*nn
= net_generic(current
->nsproxy
->net_ns
,
6532 LIST_HEAD(reaplist
);
6534 if (!nfsd_netns_ready(nn
))
6537 spin_lock(&nn
->client_lock
);
6538 list_for_each_entry(clp
, &nn
->client_lru
, cl_lru
) {
6539 count
+= nfsd_collect_client_openowners(clp
, &reaplist
,
6541 if (max
!= 0 && count
>= max
)
6544 spin_unlock(&nn
->client_lock
);
6545 nfsd_reap_openowners(&reaplist
);
6549 static u64
nfsd_find_all_delegations(struct nfs4_client
*clp
, u64 max
,
6550 struct list_head
*victims
)
6552 struct nfs4_delegation
*dp
, *next
;
6553 struct nfsd_net
*nn
= net_generic(current
->nsproxy
->net_ns
,
6557 lockdep_assert_held(&nn
->client_lock
);
6559 spin_lock(&state_lock
);
6560 list_for_each_entry_safe(dp
, next
, &clp
->cl_delegations
, dl_perclnt
) {
6563 * It's not safe to mess with delegations that have a
6564 * non-zero dl_time. They might have already been broken
6565 * and could be processed by the laundromat outside of
6566 * the state_lock. Just leave them be.
6568 if (dp
->dl_time
!= 0)
6571 atomic_inc(&clp
->cl_refcount
);
6572 WARN_ON(!unhash_delegation_locked(dp
));
6573 list_add(&dp
->dl_recall_lru
, victims
);
6577 * Despite the fact that these functions deal with
6578 * 64-bit integers for "count", we must ensure that
6579 * it doesn't blow up the clp->cl_refcount. Throw a
6580 * warning if we start to approach INT_MAX here.
6582 WARN_ON_ONCE(count
== (INT_MAX
/ 2));
6586 spin_unlock(&state_lock
);
6591 nfsd_print_client_delegations(struct nfs4_client
*clp
)
6593 u64 count
= nfsd_find_all_delegations(clp
, 0, NULL
);
6595 nfsd_print_count(clp
, count
, "delegations");
6600 nfsd_inject_print_delegations(void)
6602 struct nfs4_client
*clp
;
6604 struct nfsd_net
*nn
= net_generic(current
->nsproxy
->net_ns
,
6607 if (!nfsd_netns_ready(nn
))
6610 spin_lock(&nn
->client_lock
);
6611 list_for_each_entry(clp
, &nn
->client_lru
, cl_lru
)
6612 count
+= nfsd_print_client_delegations(clp
);
6613 spin_unlock(&nn
->client_lock
);
6619 nfsd_forget_delegations(struct list_head
*reaplist
)
6621 struct nfs4_client
*clp
;
6622 struct nfs4_delegation
*dp
, *next
;
6624 list_for_each_entry_safe(dp
, next
, reaplist
, dl_recall_lru
) {
6625 list_del_init(&dp
->dl_recall_lru
);
6626 clp
= dp
->dl_stid
.sc_client
;
6627 revoke_delegation(dp
);
6633 nfsd_inject_forget_client_delegations(struct sockaddr_storage
*addr
,
6637 struct nfs4_client
*clp
;
6638 struct nfsd_net
*nn
= net_generic(current
->nsproxy
->net_ns
,
6640 LIST_HEAD(reaplist
);
6642 if (!nfsd_netns_ready(nn
))
6645 spin_lock(&nn
->client_lock
);
6646 clp
= nfsd_find_client(addr
, addr_size
);
6648 count
= nfsd_find_all_delegations(clp
, 0, &reaplist
);
6649 spin_unlock(&nn
->client_lock
);
6651 nfsd_forget_delegations(&reaplist
);
6656 nfsd_inject_forget_delegations(u64 max
)
6659 struct nfs4_client
*clp
;
6660 struct nfsd_net
*nn
= net_generic(current
->nsproxy
->net_ns
,
6662 LIST_HEAD(reaplist
);
6664 if (!nfsd_netns_ready(nn
))
6667 spin_lock(&nn
->client_lock
);
6668 list_for_each_entry(clp
, &nn
->client_lru
, cl_lru
) {
6669 count
+= nfsd_find_all_delegations(clp
, max
- count
, &reaplist
);
6670 if (max
!= 0 && count
>= max
)
6673 spin_unlock(&nn
->client_lock
);
6674 nfsd_forget_delegations(&reaplist
);
6679 nfsd_recall_delegations(struct list_head
*reaplist
)
6681 struct nfs4_client
*clp
;
6682 struct nfs4_delegation
*dp
, *next
;
6684 list_for_each_entry_safe(dp
, next
, reaplist
, dl_recall_lru
) {
6685 list_del_init(&dp
->dl_recall_lru
);
6686 clp
= dp
->dl_stid
.sc_client
;
6688 * We skipped all entries that had a zero dl_time before,
6689 * so we can now reset the dl_time back to 0. If a delegation
6690 * break comes in now, then it won't make any difference since
6691 * we're recalling it either way.
6693 spin_lock(&state_lock
);
6695 spin_unlock(&state_lock
);
6696 nfsd_break_one_deleg(dp
);
6702 nfsd_inject_recall_client_delegations(struct sockaddr_storage
*addr
,
6706 struct nfs4_client
*clp
;
6707 struct nfsd_net
*nn
= net_generic(current
->nsproxy
->net_ns
,
6709 LIST_HEAD(reaplist
);
6711 if (!nfsd_netns_ready(nn
))
6714 spin_lock(&nn
->client_lock
);
6715 clp
= nfsd_find_client(addr
, addr_size
);
6717 count
= nfsd_find_all_delegations(clp
, 0, &reaplist
);
6718 spin_unlock(&nn
->client_lock
);
6720 nfsd_recall_delegations(&reaplist
);
6725 nfsd_inject_recall_delegations(u64 max
)
6728 struct nfs4_client
*clp
, *next
;
6729 struct nfsd_net
*nn
= net_generic(current
->nsproxy
->net_ns
,
6731 LIST_HEAD(reaplist
);
6733 if (!nfsd_netns_ready(nn
))
6736 spin_lock(&nn
->client_lock
);
6737 list_for_each_entry_safe(clp
, next
, &nn
->client_lru
, cl_lru
) {
6738 count
+= nfsd_find_all_delegations(clp
, max
- count
, &reaplist
);
6739 if (max
!= 0 && ++count
>= max
)
6742 spin_unlock(&nn
->client_lock
);
6743 nfsd_recall_delegations(&reaplist
);
6746 #endif /* CONFIG_NFSD_FAULT_INJECTION */
6749 * Since the lifetime of a delegation isn't limited to that of an open, a
6750 * client may quite reasonably hang on to a delegation as long as it has
6751 * the inode cached. This becomes an obvious problem the first time a
6752 * client's inode cache approaches the size of the server's total memory.
6754 * For now we avoid this problem by imposing a hard limit on the number
6755 * of delegations, which varies according to the server's memory size.
6758 set_max_delegations(void)
6761 * Allow at most 4 delegations per megabyte of RAM. Quick
6762 * estimates suggest that in the worst case (where every delegation
6763 * is for a different inode), a delegation could take about 1.5K,
6764 * giving a worst case usage of about 6% of memory.
6766 max_delegations
= nr_free_buffer_pages() >> (20 - 2 - PAGE_SHIFT
);
6769 static int nfs4_state_create_net(struct net
*net
)
6771 struct nfsd_net
*nn
= net_generic(net
, nfsd_net_id
);
6774 nn
->conf_id_hashtbl
= kmalloc(sizeof(struct list_head
) *
6775 CLIENT_HASH_SIZE
, GFP_KERNEL
);
6776 if (!nn
->conf_id_hashtbl
)
6778 nn
->unconf_id_hashtbl
= kmalloc(sizeof(struct list_head
) *
6779 CLIENT_HASH_SIZE
, GFP_KERNEL
);
6780 if (!nn
->unconf_id_hashtbl
)
6782 nn
->sessionid_hashtbl
= kmalloc(sizeof(struct list_head
) *
6783 SESSION_HASH_SIZE
, GFP_KERNEL
);
6784 if (!nn
->sessionid_hashtbl
)
6787 for (i
= 0; i
< CLIENT_HASH_SIZE
; i
++) {
6788 INIT_LIST_HEAD(&nn
->conf_id_hashtbl
[i
]);
6789 INIT_LIST_HEAD(&nn
->unconf_id_hashtbl
[i
]);
6791 for (i
= 0; i
< SESSION_HASH_SIZE
; i
++)
6792 INIT_LIST_HEAD(&nn
->sessionid_hashtbl
[i
]);
6793 nn
->conf_name_tree
= RB_ROOT
;
6794 nn
->unconf_name_tree
= RB_ROOT
;
6795 nn
->boot_time
= get_seconds();
6796 nn
->grace_ended
= false;
6797 nn
->nfsd4_manager
.block_opens
= true;
6798 INIT_LIST_HEAD(&nn
->nfsd4_manager
.list
);
6799 INIT_LIST_HEAD(&nn
->client_lru
);
6800 INIT_LIST_HEAD(&nn
->close_lru
);
6801 INIT_LIST_HEAD(&nn
->del_recall_lru
);
6802 spin_lock_init(&nn
->client_lock
);
6804 INIT_DELAYED_WORK(&nn
->laundromat_work
, laundromat_main
);
6810 kfree(nn
->unconf_id_hashtbl
);
6812 kfree(nn
->conf_id_hashtbl
);
6818 nfs4_state_destroy_net(struct net
*net
)
6821 struct nfs4_client
*clp
= NULL
;
6822 struct nfsd_net
*nn
= net_generic(net
, nfsd_net_id
);
6824 for (i
= 0; i
< CLIENT_HASH_SIZE
; i
++) {
6825 while (!list_empty(&nn
->conf_id_hashtbl
[i
])) {
6826 clp
= list_entry(nn
->conf_id_hashtbl
[i
].next
, struct nfs4_client
, cl_idhash
);
6827 destroy_client(clp
);
6831 for (i
= 0; i
< CLIENT_HASH_SIZE
; i
++) {
6832 while (!list_empty(&nn
->unconf_id_hashtbl
[i
])) {
6833 clp
= list_entry(nn
->unconf_id_hashtbl
[i
].next
, struct nfs4_client
, cl_idhash
);
6834 destroy_client(clp
);
6838 kfree(nn
->sessionid_hashtbl
);
6839 kfree(nn
->unconf_id_hashtbl
);
6840 kfree(nn
->conf_id_hashtbl
);
6845 nfs4_state_start_net(struct net
*net
)
6847 struct nfsd_net
*nn
= net_generic(net
, nfsd_net_id
);
6850 ret
= nfs4_state_create_net(net
);
6853 locks_start_grace(net
, &nn
->nfsd4_manager
);
6854 nfsd4_client_tracking_init(net
);
6855 printk(KERN_INFO
"NFSD: starting %ld-second grace period (net %p)\n",
6856 nn
->nfsd4_grace
, net
);
6857 queue_delayed_work(laundry_wq
, &nn
->laundromat_work
, nn
->nfsd4_grace
* HZ
);
6861 /* initialization to perform when the nfsd service is started: */
6864 nfs4_state_start(void)
6868 ret
= set_callback_cred();
6872 laundry_wq
= alloc_workqueue("%s", WQ_UNBOUND
, 0, "nfsd4");
6873 if (laundry_wq
== NULL
) {
6875 goto out_cleanup_cred
;
6877 ret
= nfsd4_create_callback_queue();
6879 goto out_free_laundry
;
6881 set_max_delegations();
6885 destroy_workqueue(laundry_wq
);
6887 cleanup_callback_cred();
6892 nfs4_state_shutdown_net(struct net
*net
)
6894 struct nfs4_delegation
*dp
= NULL
;
6895 struct list_head
*pos
, *next
, reaplist
;
6896 struct nfsd_net
*nn
= net_generic(net
, nfsd_net_id
);
6898 cancel_delayed_work_sync(&nn
->laundromat_work
);
6899 locks_end_grace(&nn
->nfsd4_manager
);
6901 INIT_LIST_HEAD(&reaplist
);
6902 spin_lock(&state_lock
);
6903 list_for_each_safe(pos
, next
, &nn
->del_recall_lru
) {
6904 dp
= list_entry (pos
, struct nfs4_delegation
, dl_recall_lru
);
6905 WARN_ON(!unhash_delegation_locked(dp
));
6906 list_add(&dp
->dl_recall_lru
, &reaplist
);
6908 spin_unlock(&state_lock
);
6909 list_for_each_safe(pos
, next
, &reaplist
) {
6910 dp
= list_entry (pos
, struct nfs4_delegation
, dl_recall_lru
);
6911 list_del_init(&dp
->dl_recall_lru
);
6912 put_clnt_odstate(dp
->dl_clnt_odstate
);
6913 nfs4_put_deleg_lease(dp
->dl_stid
.sc_file
);
6914 nfs4_put_stid(&dp
->dl_stid
);
6917 nfsd4_client_tracking_exit(net
);
6918 nfs4_state_destroy_net(net
);
6922 nfs4_state_shutdown(void)
6924 destroy_workqueue(laundry_wq
);
6925 nfsd4_destroy_callback_queue();
6926 cleanup_callback_cred();
6930 get_stateid(struct nfsd4_compound_state
*cstate
, stateid_t
*stateid
)
6932 if (HAS_STATE_ID(cstate
, CURRENT_STATE_ID_FLAG
) && CURRENT_STATEID(stateid
))
6933 memcpy(stateid
, &cstate
->current_stateid
, sizeof(stateid_t
));
6937 put_stateid(struct nfsd4_compound_state
*cstate
, stateid_t
*stateid
)
6939 if (cstate
->minorversion
) {
6940 memcpy(&cstate
->current_stateid
, stateid
, sizeof(stateid_t
));
6941 SET_STATE_ID(cstate
, CURRENT_STATE_ID_FLAG
);
6946 clear_current_stateid(struct nfsd4_compound_state
*cstate
)
6948 CLEAR_STATE_ID(cstate
, CURRENT_STATE_ID_FLAG
);
6952 * functions to set current state id
6955 nfsd4_set_opendowngradestateid(struct nfsd4_compound_state
*cstate
, struct nfsd4_open_downgrade
*odp
)
6957 put_stateid(cstate
, &odp
->od_stateid
);
6961 nfsd4_set_openstateid(struct nfsd4_compound_state
*cstate
, struct nfsd4_open
*open
)
6963 put_stateid(cstate
, &open
->op_stateid
);
6967 nfsd4_set_closestateid(struct nfsd4_compound_state
*cstate
, struct nfsd4_close
*close
)
6969 put_stateid(cstate
, &close
->cl_stateid
);
6973 nfsd4_set_lockstateid(struct nfsd4_compound_state
*cstate
, struct nfsd4_lock
*lock
)
6975 put_stateid(cstate
, &lock
->lk_resp_stateid
);
6979 * functions to consume current state id
6983 nfsd4_get_opendowngradestateid(struct nfsd4_compound_state
*cstate
, struct nfsd4_open_downgrade
*odp
)
6985 get_stateid(cstate
, &odp
->od_stateid
);
6989 nfsd4_get_delegreturnstateid(struct nfsd4_compound_state
*cstate
, struct nfsd4_delegreturn
*drp
)
6991 get_stateid(cstate
, &drp
->dr_stateid
);
6995 nfsd4_get_freestateid(struct nfsd4_compound_state
*cstate
, struct nfsd4_free_stateid
*fsp
)
6997 get_stateid(cstate
, &fsp
->fr_stateid
);
7001 nfsd4_get_setattrstateid(struct nfsd4_compound_state
*cstate
, struct nfsd4_setattr
*setattr
)
7003 get_stateid(cstate
, &setattr
->sa_stateid
);
7007 nfsd4_get_closestateid(struct nfsd4_compound_state
*cstate
, struct nfsd4_close
*close
)
7009 get_stateid(cstate
, &close
->cl_stateid
);
7013 nfsd4_get_lockustateid(struct nfsd4_compound_state
*cstate
, struct nfsd4_locku
*locku
)
7015 get_stateid(cstate
, &locku
->lu_stateid
);
7019 nfsd4_get_readstateid(struct nfsd4_compound_state
*cstate
, struct nfsd4_read
*read
)
7021 get_stateid(cstate
, &read
->rd_stateid
);
7025 nfsd4_get_writestateid(struct nfsd4_compound_state
*cstate
, struct nfsd4_write
*write
)
7027 get_stateid(cstate
, &write
->wr_stateid
);