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"
52 #define NFSDDBG_FACILITY NFSDDBG_PROC
54 #define all_ones {{~0,~0},~0}
55 static const stateid_t one_stateid
= {
57 .si_opaque
= all_ones
,
59 static const stateid_t zero_stateid
= {
62 static const stateid_t currentstateid
= {
66 static u64 current_sessionid
= 1;
68 #define ZERO_STATEID(stateid) (!memcmp((stateid), &zero_stateid, sizeof(stateid_t)))
69 #define ONE_STATEID(stateid) (!memcmp((stateid), &one_stateid, sizeof(stateid_t)))
70 #define CURRENT_STATEID(stateid) (!memcmp((stateid), ¤tstateid, sizeof(stateid_t)))
72 /* forward declarations */
73 static bool check_for_locks(struct nfs4_file
*fp
, struct nfs4_lockowner
*lowner
);
74 static void nfs4_free_ol_stateid(struct nfs4_stid
*stid
);
79 * Currently used for the del_recall_lru and file hash table. In an
80 * effort to decrease the scope of the client_mutex, this spinlock may
81 * eventually cover more:
83 static DEFINE_SPINLOCK(state_lock
);
86 * A waitqueue for all in-progress 4.0 CLOSE operations that are waiting for
87 * the refcount on the open stateid to drop.
89 static DECLARE_WAIT_QUEUE_HEAD(close_wq
);
91 static struct kmem_cache
*openowner_slab
;
92 static struct kmem_cache
*lockowner_slab
;
93 static struct kmem_cache
*file_slab
;
94 static struct kmem_cache
*stateid_slab
;
95 static struct kmem_cache
*deleg_slab
;
97 static void free_session(struct nfsd4_session
*);
99 static struct nfsd4_callback_ops nfsd4_cb_recall_ops
;
101 static bool is_session_dead(struct nfsd4_session
*ses
)
103 return ses
->se_flags
& NFS4_SESSION_DEAD
;
106 static __be32
mark_session_dead_locked(struct nfsd4_session
*ses
, int ref_held_by_me
)
108 if (atomic_read(&ses
->se_ref
) > ref_held_by_me
)
109 return nfserr_jukebox
;
110 ses
->se_flags
|= NFS4_SESSION_DEAD
;
114 static bool is_client_expired(struct nfs4_client
*clp
)
116 return clp
->cl_time
== 0;
119 static __be32
get_client_locked(struct nfs4_client
*clp
)
121 struct nfsd_net
*nn
= net_generic(clp
->net
, nfsd_net_id
);
123 lockdep_assert_held(&nn
->client_lock
);
125 if (is_client_expired(clp
))
126 return nfserr_expired
;
127 atomic_inc(&clp
->cl_refcount
);
131 /* must be called under the client_lock */
133 renew_client_locked(struct nfs4_client
*clp
)
135 struct nfsd_net
*nn
= net_generic(clp
->net
, nfsd_net_id
);
137 if (is_client_expired(clp
)) {
139 printk("%s: client (clientid %08x/%08x) already expired\n",
141 clp
->cl_clientid
.cl_boot
,
142 clp
->cl_clientid
.cl_id
);
146 dprintk("renewing client (clientid %08x/%08x)\n",
147 clp
->cl_clientid
.cl_boot
,
148 clp
->cl_clientid
.cl_id
);
149 list_move_tail(&clp
->cl_lru
, &nn
->client_lru
);
150 clp
->cl_time
= get_seconds();
153 static void put_client_renew_locked(struct nfs4_client
*clp
)
155 struct nfsd_net
*nn
= net_generic(clp
->net
, nfsd_net_id
);
157 lockdep_assert_held(&nn
->client_lock
);
159 if (!atomic_dec_and_test(&clp
->cl_refcount
))
161 if (!is_client_expired(clp
))
162 renew_client_locked(clp
);
165 static void put_client_renew(struct nfs4_client
*clp
)
167 struct nfsd_net
*nn
= net_generic(clp
->net
, nfsd_net_id
);
169 if (!atomic_dec_and_lock(&clp
->cl_refcount
, &nn
->client_lock
))
171 if (!is_client_expired(clp
))
172 renew_client_locked(clp
);
173 spin_unlock(&nn
->client_lock
);
176 static __be32
nfsd4_get_session_locked(struct nfsd4_session
*ses
)
180 if (is_session_dead(ses
))
181 return nfserr_badsession
;
182 status
= get_client_locked(ses
->se_client
);
185 atomic_inc(&ses
->se_ref
);
189 static void nfsd4_put_session_locked(struct nfsd4_session
*ses
)
191 struct nfs4_client
*clp
= ses
->se_client
;
192 struct nfsd_net
*nn
= net_generic(clp
->net
, nfsd_net_id
);
194 lockdep_assert_held(&nn
->client_lock
);
196 if (atomic_dec_and_test(&ses
->se_ref
) && is_session_dead(ses
))
198 put_client_renew_locked(clp
);
201 static void nfsd4_put_session(struct nfsd4_session
*ses
)
203 struct nfs4_client
*clp
= ses
->se_client
;
204 struct nfsd_net
*nn
= net_generic(clp
->net
, nfsd_net_id
);
206 spin_lock(&nn
->client_lock
);
207 nfsd4_put_session_locked(ses
);
208 spin_unlock(&nn
->client_lock
);
211 static inline struct nfs4_stateowner
*
212 nfs4_get_stateowner(struct nfs4_stateowner
*sop
)
214 atomic_inc(&sop
->so_count
);
219 same_owner_str(struct nfs4_stateowner
*sop
, struct xdr_netobj
*owner
)
221 return (sop
->so_owner
.len
== owner
->len
) &&
222 0 == memcmp(sop
->so_owner
.data
, owner
->data
, owner
->len
);
225 static struct nfs4_openowner
*
226 find_openstateowner_str_locked(unsigned int hashval
, struct nfsd4_open
*open
,
227 struct nfs4_client
*clp
)
229 struct nfs4_stateowner
*so
;
231 lockdep_assert_held(&clp
->cl_lock
);
233 list_for_each_entry(so
, &clp
->cl_ownerstr_hashtbl
[hashval
],
235 if (!so
->so_is_open_owner
)
237 if (same_owner_str(so
, &open
->op_owner
))
238 return openowner(nfs4_get_stateowner(so
));
243 static struct nfs4_openowner
*
244 find_openstateowner_str(unsigned int hashval
, struct nfsd4_open
*open
,
245 struct nfs4_client
*clp
)
247 struct nfs4_openowner
*oo
;
249 spin_lock(&clp
->cl_lock
);
250 oo
= find_openstateowner_str_locked(hashval
, open
, clp
);
251 spin_unlock(&clp
->cl_lock
);
256 opaque_hashval(const void *ptr
, int nbytes
)
258 unsigned char *cptr
= (unsigned char *) ptr
;
268 static void nfsd4_free_file_rcu(struct rcu_head
*rcu
)
270 struct nfs4_file
*fp
= container_of(rcu
, struct nfs4_file
, fi_rcu
);
272 kmem_cache_free(file_slab
, fp
);
276 put_nfs4_file(struct nfs4_file
*fi
)
278 might_lock(&state_lock
);
280 if (atomic_dec_and_lock(&fi
->fi_ref
, &state_lock
)) {
281 hlist_del_rcu(&fi
->fi_hash
);
282 spin_unlock(&state_lock
);
283 WARN_ON_ONCE(!list_empty(&fi
->fi_delegations
));
284 call_rcu(&fi
->fi_rcu
, nfsd4_free_file_rcu
);
289 get_nfs4_file(struct nfs4_file
*fi
)
291 atomic_inc(&fi
->fi_ref
);
295 __nfs4_get_fd(struct nfs4_file
*f
, int oflag
)
297 if (f
->fi_fds
[oflag
])
298 return get_file(f
->fi_fds
[oflag
]);
303 find_writeable_file_locked(struct nfs4_file
*f
)
307 lockdep_assert_held(&f
->fi_lock
);
309 ret
= __nfs4_get_fd(f
, O_WRONLY
);
311 ret
= __nfs4_get_fd(f
, O_RDWR
);
316 find_writeable_file(struct nfs4_file
*f
)
320 spin_lock(&f
->fi_lock
);
321 ret
= find_writeable_file_locked(f
);
322 spin_unlock(&f
->fi_lock
);
327 static struct file
*find_readable_file_locked(struct nfs4_file
*f
)
331 lockdep_assert_held(&f
->fi_lock
);
333 ret
= __nfs4_get_fd(f
, O_RDONLY
);
335 ret
= __nfs4_get_fd(f
, O_RDWR
);
340 find_readable_file(struct nfs4_file
*f
)
344 spin_lock(&f
->fi_lock
);
345 ret
= find_readable_file_locked(f
);
346 spin_unlock(&f
->fi_lock
);
352 find_any_file(struct nfs4_file
*f
)
356 spin_lock(&f
->fi_lock
);
357 ret
= __nfs4_get_fd(f
, O_RDWR
);
359 ret
= __nfs4_get_fd(f
, O_WRONLY
);
361 ret
= __nfs4_get_fd(f
, O_RDONLY
);
363 spin_unlock(&f
->fi_lock
);
367 static atomic_long_t num_delegations
;
368 unsigned long max_delegations
;
371 * Open owner state (share locks)
374 /* hash tables for lock and open owners */
375 #define OWNER_HASH_BITS 8
376 #define OWNER_HASH_SIZE (1 << OWNER_HASH_BITS)
377 #define OWNER_HASH_MASK (OWNER_HASH_SIZE - 1)
379 static unsigned int ownerstr_hashval(struct xdr_netobj
*ownername
)
383 ret
= opaque_hashval(ownername
->data
, ownername
->len
);
384 return ret
& OWNER_HASH_MASK
;
387 /* hash table for nfs4_file */
388 #define FILE_HASH_BITS 8
389 #define FILE_HASH_SIZE (1 << FILE_HASH_BITS)
391 static unsigned int nfsd_fh_hashval(struct knfsd_fh
*fh
)
393 return jhash2(fh
->fh_base
.fh_pad
, XDR_QUADLEN(fh
->fh_size
), 0);
396 static unsigned int file_hashval(struct knfsd_fh
*fh
)
398 return nfsd_fh_hashval(fh
) & (FILE_HASH_SIZE
- 1);
401 static struct hlist_head file_hashtbl
[FILE_HASH_SIZE
];
404 __nfs4_file_get_access(struct nfs4_file
*fp
, u32 access
)
406 lockdep_assert_held(&fp
->fi_lock
);
408 if (access
& NFS4_SHARE_ACCESS_WRITE
)
409 atomic_inc(&fp
->fi_access
[O_WRONLY
]);
410 if (access
& NFS4_SHARE_ACCESS_READ
)
411 atomic_inc(&fp
->fi_access
[O_RDONLY
]);
415 nfs4_file_get_access(struct nfs4_file
*fp
, u32 access
)
417 lockdep_assert_held(&fp
->fi_lock
);
419 /* Does this access mode make sense? */
420 if (access
& ~NFS4_SHARE_ACCESS_BOTH
)
423 /* Does it conflict with a deny mode already set? */
424 if ((access
& fp
->fi_share_deny
) != 0)
425 return nfserr_share_denied
;
427 __nfs4_file_get_access(fp
, access
);
431 static __be32
nfs4_file_check_deny(struct nfs4_file
*fp
, u32 deny
)
433 /* Common case is that there is no deny mode. */
435 /* Does this deny mode make sense? */
436 if (deny
& ~NFS4_SHARE_DENY_BOTH
)
439 if ((deny
& NFS4_SHARE_DENY_READ
) &&
440 atomic_read(&fp
->fi_access
[O_RDONLY
]))
441 return nfserr_share_denied
;
443 if ((deny
& NFS4_SHARE_DENY_WRITE
) &&
444 atomic_read(&fp
->fi_access
[O_WRONLY
]))
445 return nfserr_share_denied
;
450 static void __nfs4_file_put_access(struct nfs4_file
*fp
, int oflag
)
452 might_lock(&fp
->fi_lock
);
454 if (atomic_dec_and_lock(&fp
->fi_access
[oflag
], &fp
->fi_lock
)) {
455 struct file
*f1
= NULL
;
456 struct file
*f2
= NULL
;
458 swap(f1
, fp
->fi_fds
[oflag
]);
459 if (atomic_read(&fp
->fi_access
[1 - oflag
]) == 0)
460 swap(f2
, fp
->fi_fds
[O_RDWR
]);
461 spin_unlock(&fp
->fi_lock
);
469 static void nfs4_file_put_access(struct nfs4_file
*fp
, u32 access
)
471 WARN_ON_ONCE(access
& ~NFS4_SHARE_ACCESS_BOTH
);
473 if (access
& NFS4_SHARE_ACCESS_WRITE
)
474 __nfs4_file_put_access(fp
, O_WRONLY
);
475 if (access
& NFS4_SHARE_ACCESS_READ
)
476 __nfs4_file_put_access(fp
, O_RDONLY
);
479 struct nfs4_stid
*nfs4_alloc_stid(struct nfs4_client
*cl
,
480 struct kmem_cache
*slab
)
482 struct nfs4_stid
*stid
;
485 stid
= kmem_cache_zalloc(slab
, GFP_KERNEL
);
489 idr_preload(GFP_KERNEL
);
490 spin_lock(&cl
->cl_lock
);
491 new_id
= idr_alloc_cyclic(&cl
->cl_stateids
, stid
, 0, 0, GFP_NOWAIT
);
492 spin_unlock(&cl
->cl_lock
);
496 stid
->sc_client
= cl
;
497 stid
->sc_stateid
.si_opaque
.so_id
= new_id
;
498 stid
->sc_stateid
.si_opaque
.so_clid
= cl
->cl_clientid
;
499 /* Will be incremented before return to client: */
500 atomic_set(&stid
->sc_count
, 1);
503 * It shouldn't be a problem to reuse an opaque stateid value.
504 * I don't think it is for 4.1. But with 4.0 I worry that, for
505 * example, a stray write retransmission could be accepted by
506 * the server when it should have been rejected. Therefore,
507 * adopt a trick from the sctp code to attempt to maximize the
508 * amount of time until an id is reused, by ensuring they always
509 * "increase" (mod INT_MAX):
513 kmem_cache_free(slab
, stid
);
517 static struct nfs4_ol_stateid
* nfs4_alloc_open_stateid(struct nfs4_client
*clp
)
519 struct nfs4_stid
*stid
;
520 struct nfs4_ol_stateid
*stp
;
522 stid
= nfs4_alloc_stid(clp
, stateid_slab
);
526 stp
= openlockstateid(stid
);
527 stp
->st_stid
.sc_free
= nfs4_free_ol_stateid
;
531 static void nfs4_free_deleg(struct nfs4_stid
*stid
)
533 kmem_cache_free(deleg_slab
, stid
);
534 atomic_long_dec(&num_delegations
);
538 * When we recall a delegation, we should be careful not to hand it
539 * out again straight away.
540 * To ensure this we keep a pair of bloom filters ('new' and 'old')
541 * in which the filehandles of recalled delegations are "stored".
542 * If a filehandle appear in either filter, a delegation is blocked.
543 * When a delegation is recalled, the filehandle is stored in the "new"
545 * Every 30 seconds we swap the filters and clear the "new" one,
546 * unless both are empty of course.
548 * Each filter is 256 bits. We hash the filehandle to 32bit and use the
549 * low 3 bytes as hash-table indices.
551 * 'blocked_delegations_lock', which is always taken in block_delegations(),
552 * is used to manage concurrent access. Testing does not need the lock
553 * except when swapping the two filters.
555 static DEFINE_SPINLOCK(blocked_delegations_lock
);
556 static struct bloom_pair
{
557 int entries
, old_entries
;
559 int new; /* index into 'set' */
560 DECLARE_BITMAP(set
[2], 256);
561 } blocked_delegations
;
563 static int delegation_blocked(struct knfsd_fh
*fh
)
566 struct bloom_pair
*bd
= &blocked_delegations
;
568 if (bd
->entries
== 0)
570 if (seconds_since_boot() - bd
->swap_time
> 30) {
571 spin_lock(&blocked_delegations_lock
);
572 if (seconds_since_boot() - bd
->swap_time
> 30) {
573 bd
->entries
-= bd
->old_entries
;
574 bd
->old_entries
= bd
->entries
;
575 memset(bd
->set
[bd
->new], 0,
578 bd
->swap_time
= seconds_since_boot();
580 spin_unlock(&blocked_delegations_lock
);
582 hash
= jhash(&fh
->fh_base
, fh
->fh_size
, 0);
583 if (test_bit(hash
&255, bd
->set
[0]) &&
584 test_bit((hash
>>8)&255, bd
->set
[0]) &&
585 test_bit((hash
>>16)&255, bd
->set
[0]))
588 if (test_bit(hash
&255, bd
->set
[1]) &&
589 test_bit((hash
>>8)&255, bd
->set
[1]) &&
590 test_bit((hash
>>16)&255, bd
->set
[1]))
596 static void block_delegations(struct knfsd_fh
*fh
)
599 struct bloom_pair
*bd
= &blocked_delegations
;
601 hash
= jhash(&fh
->fh_base
, fh
->fh_size
, 0);
603 spin_lock(&blocked_delegations_lock
);
604 __set_bit(hash
&255, bd
->set
[bd
->new]);
605 __set_bit((hash
>>8)&255, bd
->set
[bd
->new]);
606 __set_bit((hash
>>16)&255, bd
->set
[bd
->new]);
607 if (bd
->entries
== 0)
608 bd
->swap_time
= seconds_since_boot();
610 spin_unlock(&blocked_delegations_lock
);
613 static struct nfs4_delegation
*
614 alloc_init_deleg(struct nfs4_client
*clp
, struct svc_fh
*current_fh
)
616 struct nfs4_delegation
*dp
;
619 dprintk("NFSD alloc_init_deleg\n");
620 n
= atomic_long_inc_return(&num_delegations
);
621 if (n
< 0 || n
> max_delegations
)
623 if (delegation_blocked(¤t_fh
->fh_handle
))
625 dp
= delegstateid(nfs4_alloc_stid(clp
, deleg_slab
));
629 dp
->dl_stid
.sc_free
= nfs4_free_deleg
;
631 * delegation seqid's are never incremented. The 4.1 special
632 * meaning of seqid 0 isn't meaningful, really, but let's avoid
633 * 0 anyway just for consistency and use 1:
635 dp
->dl_stid
.sc_stateid
.si_generation
= 1;
636 INIT_LIST_HEAD(&dp
->dl_perfile
);
637 INIT_LIST_HEAD(&dp
->dl_perclnt
);
638 INIT_LIST_HEAD(&dp
->dl_recall_lru
);
639 dp
->dl_type
= NFS4_OPEN_DELEGATE_READ
;
641 nfsd4_init_cb(&dp
->dl_recall
, dp
->dl_stid
.sc_client
,
642 &nfsd4_cb_recall_ops
, NFSPROC4_CLNT_CB_RECALL
);
645 atomic_long_dec(&num_delegations
);
650 nfs4_put_stid(struct nfs4_stid
*s
)
652 struct nfs4_file
*fp
= s
->sc_file
;
653 struct nfs4_client
*clp
= s
->sc_client
;
655 might_lock(&clp
->cl_lock
);
657 if (!atomic_dec_and_lock(&s
->sc_count
, &clp
->cl_lock
)) {
658 wake_up_all(&close_wq
);
661 idr_remove(&clp
->cl_stateids
, s
->sc_stateid
.si_opaque
.so_id
);
662 spin_unlock(&clp
->cl_lock
);
668 static void nfs4_put_deleg_lease(struct nfs4_file
*fp
)
670 struct file
*filp
= NULL
;
672 spin_lock(&fp
->fi_lock
);
673 if (fp
->fi_deleg_file
&& --fp
->fi_delegees
== 0)
674 swap(filp
, fp
->fi_deleg_file
);
675 spin_unlock(&fp
->fi_lock
);
678 vfs_setlease(filp
, F_UNLCK
, NULL
, (void **)&fp
);
683 void nfs4_unhash_stid(struct nfs4_stid
*s
)
689 hash_delegation_locked(struct nfs4_delegation
*dp
, struct nfs4_file
*fp
)
691 lockdep_assert_held(&state_lock
);
692 lockdep_assert_held(&fp
->fi_lock
);
694 atomic_inc(&dp
->dl_stid
.sc_count
);
695 dp
->dl_stid
.sc_type
= NFS4_DELEG_STID
;
696 list_add(&dp
->dl_perfile
, &fp
->fi_delegations
);
697 list_add(&dp
->dl_perclnt
, &dp
->dl_stid
.sc_client
->cl_delegations
);
701 unhash_delegation_locked(struct nfs4_delegation
*dp
)
703 struct nfs4_file
*fp
= dp
->dl_stid
.sc_file
;
705 lockdep_assert_held(&state_lock
);
707 dp
->dl_stid
.sc_type
= NFS4_CLOSED_DELEG_STID
;
708 /* Ensure that deleg break won't try to requeue it */
710 spin_lock(&fp
->fi_lock
);
711 list_del_init(&dp
->dl_perclnt
);
712 list_del_init(&dp
->dl_recall_lru
);
713 list_del_init(&dp
->dl_perfile
);
714 spin_unlock(&fp
->fi_lock
);
717 static void destroy_delegation(struct nfs4_delegation
*dp
)
719 spin_lock(&state_lock
);
720 unhash_delegation_locked(dp
);
721 spin_unlock(&state_lock
);
722 nfs4_put_deleg_lease(dp
->dl_stid
.sc_file
);
723 nfs4_put_stid(&dp
->dl_stid
);
726 static void revoke_delegation(struct nfs4_delegation
*dp
)
728 struct nfs4_client
*clp
= dp
->dl_stid
.sc_client
;
730 WARN_ON(!list_empty(&dp
->dl_recall_lru
));
732 nfs4_put_deleg_lease(dp
->dl_stid
.sc_file
);
734 if (clp
->cl_minorversion
== 0)
735 nfs4_put_stid(&dp
->dl_stid
);
737 dp
->dl_stid
.sc_type
= NFS4_REVOKED_DELEG_STID
;
738 spin_lock(&clp
->cl_lock
);
739 list_add(&dp
->dl_recall_lru
, &clp
->cl_revoked
);
740 spin_unlock(&clp
->cl_lock
);
748 static unsigned int clientid_hashval(u32 id
)
750 return id
& CLIENT_HASH_MASK
;
753 static unsigned int clientstr_hashval(const char *name
)
755 return opaque_hashval(name
, 8) & CLIENT_HASH_MASK
;
759 * We store the NONE, READ, WRITE, and BOTH bits separately in the
760 * st_{access,deny}_bmap field of the stateid, in order to track not
761 * only what share bits are currently in force, but also what
762 * combinations of share bits previous opens have used. This allows us
763 * to enforce the recommendation of rfc 3530 14.2.19 that the server
764 * return an error if the client attempt to downgrade to a combination
765 * of share bits not explicable by closing some of its previous opens.
767 * XXX: This enforcement is actually incomplete, since we don't keep
768 * track of access/deny bit combinations; so, e.g., we allow:
770 * OPEN allow read, deny write
771 * OPEN allow both, deny none
772 * DOWNGRADE allow read, deny none
774 * which we should reject.
777 bmap_to_share_mode(unsigned long bmap
) {
779 unsigned int access
= 0;
781 for (i
= 1; i
< 4; i
++) {
782 if (test_bit(i
, &bmap
))
788 /* set share access for a given stateid */
790 set_access(u32 access
, struct nfs4_ol_stateid
*stp
)
792 unsigned char mask
= 1 << access
;
794 WARN_ON_ONCE(access
> NFS4_SHARE_ACCESS_BOTH
);
795 stp
->st_access_bmap
|= mask
;
798 /* clear share access for a given stateid */
800 clear_access(u32 access
, struct nfs4_ol_stateid
*stp
)
802 unsigned char mask
= 1 << access
;
804 WARN_ON_ONCE(access
> NFS4_SHARE_ACCESS_BOTH
);
805 stp
->st_access_bmap
&= ~mask
;
808 /* test whether a given stateid has access */
810 test_access(u32 access
, struct nfs4_ol_stateid
*stp
)
812 unsigned char mask
= 1 << access
;
814 return (bool)(stp
->st_access_bmap
& mask
);
817 /* set share deny for a given stateid */
819 set_deny(u32 deny
, struct nfs4_ol_stateid
*stp
)
821 unsigned char mask
= 1 << deny
;
823 WARN_ON_ONCE(deny
> NFS4_SHARE_DENY_BOTH
);
824 stp
->st_deny_bmap
|= mask
;
827 /* clear share deny for a given stateid */
829 clear_deny(u32 deny
, struct nfs4_ol_stateid
*stp
)
831 unsigned char mask
= 1 << deny
;
833 WARN_ON_ONCE(deny
> NFS4_SHARE_DENY_BOTH
);
834 stp
->st_deny_bmap
&= ~mask
;
837 /* test whether a given stateid is denying specific access */
839 test_deny(u32 deny
, struct nfs4_ol_stateid
*stp
)
841 unsigned char mask
= 1 << deny
;
843 return (bool)(stp
->st_deny_bmap
& mask
);
846 static int nfs4_access_to_omode(u32 access
)
848 switch (access
& NFS4_SHARE_ACCESS_BOTH
) {
849 case NFS4_SHARE_ACCESS_READ
:
851 case NFS4_SHARE_ACCESS_WRITE
:
853 case NFS4_SHARE_ACCESS_BOTH
:
861 * A stateid that had a deny mode associated with it is being released
862 * or downgraded. Recalculate the deny mode on the file.
865 recalculate_deny_mode(struct nfs4_file
*fp
)
867 struct nfs4_ol_stateid
*stp
;
869 spin_lock(&fp
->fi_lock
);
870 fp
->fi_share_deny
= 0;
871 list_for_each_entry(stp
, &fp
->fi_stateids
, st_perfile
)
872 fp
->fi_share_deny
|= bmap_to_share_mode(stp
->st_deny_bmap
);
873 spin_unlock(&fp
->fi_lock
);
877 reset_union_bmap_deny(u32 deny
, struct nfs4_ol_stateid
*stp
)
882 for (i
= 1; i
< 4; i
++) {
883 if ((i
& deny
) != i
) {
889 /* Recalculate per-file deny mode if there was a change */
891 recalculate_deny_mode(stp
->st_stid
.sc_file
);
894 /* release all access and file references for a given stateid */
896 release_all_access(struct nfs4_ol_stateid
*stp
)
899 struct nfs4_file
*fp
= stp
->st_stid
.sc_file
;
901 if (fp
&& stp
->st_deny_bmap
!= 0)
902 recalculate_deny_mode(fp
);
904 for (i
= 1; i
< 4; i
++) {
905 if (test_access(i
, stp
))
906 nfs4_file_put_access(stp
->st_stid
.sc_file
, i
);
907 clear_access(i
, stp
);
911 static void nfs4_put_stateowner(struct nfs4_stateowner
*sop
)
913 struct nfs4_client
*clp
= sop
->so_client
;
915 might_lock(&clp
->cl_lock
);
917 if (!atomic_dec_and_lock(&sop
->so_count
, &clp
->cl_lock
))
919 sop
->so_ops
->so_unhash(sop
);
920 spin_unlock(&clp
->cl_lock
);
921 kfree(sop
->so_owner
.data
);
922 sop
->so_ops
->so_free(sop
);
925 static void unhash_ol_stateid(struct nfs4_ol_stateid
*stp
)
927 struct nfs4_file
*fp
= stp
->st_stid
.sc_file
;
929 lockdep_assert_held(&stp
->st_stateowner
->so_client
->cl_lock
);
931 spin_lock(&fp
->fi_lock
);
932 list_del(&stp
->st_perfile
);
933 spin_unlock(&fp
->fi_lock
);
934 list_del(&stp
->st_perstateowner
);
937 static void nfs4_free_ol_stateid(struct nfs4_stid
*stid
)
939 struct nfs4_ol_stateid
*stp
= openlockstateid(stid
);
941 release_all_access(stp
);
942 if (stp
->st_stateowner
)
943 nfs4_put_stateowner(stp
->st_stateowner
);
944 kmem_cache_free(stateid_slab
, stid
);
947 static void nfs4_free_lock_stateid(struct nfs4_stid
*stid
)
949 struct nfs4_ol_stateid
*stp
= openlockstateid(stid
);
950 struct nfs4_lockowner
*lo
= lockowner(stp
->st_stateowner
);
953 file
= find_any_file(stp
->st_stid
.sc_file
);
955 filp_close(file
, (fl_owner_t
)lo
);
956 nfs4_free_ol_stateid(stid
);
960 * Put the persistent reference to an already unhashed generic stateid, while
961 * holding the cl_lock. If it's the last reference, then put it onto the
962 * reaplist for later destruction.
964 static void put_ol_stateid_locked(struct nfs4_ol_stateid
*stp
,
965 struct list_head
*reaplist
)
967 struct nfs4_stid
*s
= &stp
->st_stid
;
968 struct nfs4_client
*clp
= s
->sc_client
;
970 lockdep_assert_held(&clp
->cl_lock
);
972 WARN_ON_ONCE(!list_empty(&stp
->st_locks
));
974 if (!atomic_dec_and_test(&s
->sc_count
)) {
975 wake_up_all(&close_wq
);
979 idr_remove(&clp
->cl_stateids
, s
->sc_stateid
.si_opaque
.so_id
);
980 list_add(&stp
->st_locks
, reaplist
);
983 static void unhash_lock_stateid(struct nfs4_ol_stateid
*stp
)
985 struct nfs4_openowner
*oo
= openowner(stp
->st_openstp
->st_stateowner
);
987 lockdep_assert_held(&oo
->oo_owner
.so_client
->cl_lock
);
989 list_del_init(&stp
->st_locks
);
990 unhash_ol_stateid(stp
);
991 nfs4_unhash_stid(&stp
->st_stid
);
994 static void release_lock_stateid(struct nfs4_ol_stateid
*stp
)
996 struct nfs4_openowner
*oo
= openowner(stp
->st_openstp
->st_stateowner
);
998 spin_lock(&oo
->oo_owner
.so_client
->cl_lock
);
999 unhash_lock_stateid(stp
);
1000 spin_unlock(&oo
->oo_owner
.so_client
->cl_lock
);
1001 nfs4_put_stid(&stp
->st_stid
);
1004 static void unhash_lockowner_locked(struct nfs4_lockowner
*lo
)
1006 struct nfs4_client
*clp
= lo
->lo_owner
.so_client
;
1008 lockdep_assert_held(&clp
->cl_lock
);
1010 list_del_init(&lo
->lo_owner
.so_strhash
);
1014 * Free a list of generic stateids that were collected earlier after being
1018 free_ol_stateid_reaplist(struct list_head
*reaplist
)
1020 struct nfs4_ol_stateid
*stp
;
1021 struct nfs4_file
*fp
;
1025 while (!list_empty(reaplist
)) {
1026 stp
= list_first_entry(reaplist
, struct nfs4_ol_stateid
,
1028 list_del(&stp
->st_locks
);
1029 fp
= stp
->st_stid
.sc_file
;
1030 stp
->st_stid
.sc_free(&stp
->st_stid
);
1036 static void release_lockowner(struct nfs4_lockowner
*lo
)
1038 struct nfs4_client
*clp
= lo
->lo_owner
.so_client
;
1039 struct nfs4_ol_stateid
*stp
;
1040 struct list_head reaplist
;
1042 INIT_LIST_HEAD(&reaplist
);
1044 spin_lock(&clp
->cl_lock
);
1045 unhash_lockowner_locked(lo
);
1046 while (!list_empty(&lo
->lo_owner
.so_stateids
)) {
1047 stp
= list_first_entry(&lo
->lo_owner
.so_stateids
,
1048 struct nfs4_ol_stateid
, st_perstateowner
);
1049 unhash_lock_stateid(stp
);
1050 put_ol_stateid_locked(stp
, &reaplist
);
1052 spin_unlock(&clp
->cl_lock
);
1053 free_ol_stateid_reaplist(&reaplist
);
1054 nfs4_put_stateowner(&lo
->lo_owner
);
1057 static void release_open_stateid_locks(struct nfs4_ol_stateid
*open_stp
,
1058 struct list_head
*reaplist
)
1060 struct nfs4_ol_stateid
*stp
;
1062 while (!list_empty(&open_stp
->st_locks
)) {
1063 stp
= list_entry(open_stp
->st_locks
.next
,
1064 struct nfs4_ol_stateid
, st_locks
);
1065 unhash_lock_stateid(stp
);
1066 put_ol_stateid_locked(stp
, reaplist
);
1070 static void unhash_open_stateid(struct nfs4_ol_stateid
*stp
,
1071 struct list_head
*reaplist
)
1073 lockdep_assert_held(&stp
->st_stid
.sc_client
->cl_lock
);
1075 unhash_ol_stateid(stp
);
1076 release_open_stateid_locks(stp
, reaplist
);
1079 static void release_open_stateid(struct nfs4_ol_stateid
*stp
)
1081 LIST_HEAD(reaplist
);
1083 spin_lock(&stp
->st_stid
.sc_client
->cl_lock
);
1084 unhash_open_stateid(stp
, &reaplist
);
1085 put_ol_stateid_locked(stp
, &reaplist
);
1086 spin_unlock(&stp
->st_stid
.sc_client
->cl_lock
);
1087 free_ol_stateid_reaplist(&reaplist
);
1090 static void unhash_openowner_locked(struct nfs4_openowner
*oo
)
1092 struct nfs4_client
*clp
= oo
->oo_owner
.so_client
;
1094 lockdep_assert_held(&clp
->cl_lock
);
1096 list_del_init(&oo
->oo_owner
.so_strhash
);
1097 list_del_init(&oo
->oo_perclient
);
1100 static void release_last_closed_stateid(struct nfs4_openowner
*oo
)
1102 struct nfsd_net
*nn
= net_generic(oo
->oo_owner
.so_client
->net
,
1104 struct nfs4_ol_stateid
*s
;
1106 spin_lock(&nn
->client_lock
);
1107 s
= oo
->oo_last_closed_stid
;
1109 list_del_init(&oo
->oo_close_lru
);
1110 oo
->oo_last_closed_stid
= NULL
;
1112 spin_unlock(&nn
->client_lock
);
1114 nfs4_put_stid(&s
->st_stid
);
1117 static void release_openowner(struct nfs4_openowner
*oo
)
1119 struct nfs4_ol_stateid
*stp
;
1120 struct nfs4_client
*clp
= oo
->oo_owner
.so_client
;
1121 struct list_head reaplist
;
1123 INIT_LIST_HEAD(&reaplist
);
1125 spin_lock(&clp
->cl_lock
);
1126 unhash_openowner_locked(oo
);
1127 while (!list_empty(&oo
->oo_owner
.so_stateids
)) {
1128 stp
= list_first_entry(&oo
->oo_owner
.so_stateids
,
1129 struct nfs4_ol_stateid
, st_perstateowner
);
1130 unhash_open_stateid(stp
, &reaplist
);
1131 put_ol_stateid_locked(stp
, &reaplist
);
1133 spin_unlock(&clp
->cl_lock
);
1134 free_ol_stateid_reaplist(&reaplist
);
1135 release_last_closed_stateid(oo
);
1136 nfs4_put_stateowner(&oo
->oo_owner
);
1140 hash_sessionid(struct nfs4_sessionid
*sessionid
)
1142 struct nfsd4_sessionid
*sid
= (struct nfsd4_sessionid
*)sessionid
;
1144 return sid
->sequence
% SESSION_HASH_SIZE
;
1149 dump_sessionid(const char *fn
, struct nfs4_sessionid
*sessionid
)
1151 u32
*ptr
= (u32
*)(&sessionid
->data
[0]);
1152 dprintk("%s: %u:%u:%u:%u\n", fn
, ptr
[0], ptr
[1], ptr
[2], ptr
[3]);
1156 dump_sessionid(const char *fn
, struct nfs4_sessionid
*sessionid
)
1162 * Bump the seqid on cstate->replay_owner, and clear replay_owner if it
1163 * won't be used for replay.
1165 void nfsd4_bump_seqid(struct nfsd4_compound_state
*cstate
, __be32 nfserr
)
1167 struct nfs4_stateowner
*so
= cstate
->replay_owner
;
1169 if (nfserr
== nfserr_replay_me
)
1172 if (!seqid_mutating_err(ntohl(nfserr
))) {
1173 nfsd4_cstate_clear_replay(cstate
);
1178 if (so
->so_is_open_owner
)
1179 release_last_closed_stateid(openowner(so
));
1185 gen_sessionid(struct nfsd4_session
*ses
)
1187 struct nfs4_client
*clp
= ses
->se_client
;
1188 struct nfsd4_sessionid
*sid
;
1190 sid
= (struct nfsd4_sessionid
*)ses
->se_sessionid
.data
;
1191 sid
->clientid
= clp
->cl_clientid
;
1192 sid
->sequence
= current_sessionid
++;
1197 * The protocol defines ca_maxresponssize_cached to include the size of
1198 * the rpc header, but all we need to cache is the data starting after
1199 * the end of the initial SEQUENCE operation--the rest we regenerate
1200 * each time. Therefore we can advertise a ca_maxresponssize_cached
1201 * value that is the number of bytes in our cache plus a few additional
1202 * bytes. In order to stay on the safe side, and not promise more than
1203 * we can cache, those additional bytes must be the minimum possible: 24
1204 * bytes of rpc header (xid through accept state, with AUTH_NULL
1205 * verifier), 12 for the compound header (with zero-length tag), and 44
1206 * for the SEQUENCE op response:
1208 #define NFSD_MIN_HDR_SEQ_SZ (24 + 12 + 44)
1211 free_session_slots(struct nfsd4_session
*ses
)
1215 for (i
= 0; i
< ses
->se_fchannel
.maxreqs
; i
++)
1216 kfree(ses
->se_slots
[i
]);
1220 * We don't actually need to cache the rpc and session headers, so we
1221 * can allocate a little less for each slot:
1223 static inline u32
slot_bytes(struct nfsd4_channel_attrs
*ca
)
1227 if (ca
->maxresp_cached
< NFSD_MIN_HDR_SEQ_SZ
)
1230 size
= ca
->maxresp_cached
- NFSD_MIN_HDR_SEQ_SZ
;
1231 return size
+ sizeof(struct nfsd4_slot
);
1235 * XXX: If we run out of reserved DRC memory we could (up to a point)
1236 * re-negotiate active sessions and reduce their slot usage to make
1237 * room for new connections. For now we just fail the create session.
1239 static u32
nfsd4_get_drc_mem(struct nfsd4_channel_attrs
*ca
)
1241 u32 slotsize
= slot_bytes(ca
);
1242 u32 num
= ca
->maxreqs
;
1245 spin_lock(&nfsd_drc_lock
);
1246 avail
= min((unsigned long)NFSD_MAX_MEM_PER_SESSION
,
1247 nfsd_drc_max_mem
- nfsd_drc_mem_used
);
1248 num
= min_t(int, num
, avail
/ slotsize
);
1249 nfsd_drc_mem_used
+= num
* slotsize
;
1250 spin_unlock(&nfsd_drc_lock
);
1255 static void nfsd4_put_drc_mem(struct nfsd4_channel_attrs
*ca
)
1257 int slotsize
= slot_bytes(ca
);
1259 spin_lock(&nfsd_drc_lock
);
1260 nfsd_drc_mem_used
-= slotsize
* ca
->maxreqs
;
1261 spin_unlock(&nfsd_drc_lock
);
1264 static struct nfsd4_session
*alloc_session(struct nfsd4_channel_attrs
*fattrs
,
1265 struct nfsd4_channel_attrs
*battrs
)
1267 int numslots
= fattrs
->maxreqs
;
1268 int slotsize
= slot_bytes(fattrs
);
1269 struct nfsd4_session
*new;
1272 BUILD_BUG_ON(NFSD_MAX_SLOTS_PER_SESSION
* sizeof(struct nfsd4_slot
*)
1273 + sizeof(struct nfsd4_session
) > PAGE_SIZE
);
1274 mem
= numslots
* sizeof(struct nfsd4_slot
*);
1276 new = kzalloc(sizeof(*new) + mem
, GFP_KERNEL
);
1279 /* allocate each struct nfsd4_slot and data cache in one piece */
1280 for (i
= 0; i
< numslots
; i
++) {
1281 new->se_slots
[i
] = kzalloc(slotsize
, GFP_KERNEL
);
1282 if (!new->se_slots
[i
])
1286 memcpy(&new->se_fchannel
, fattrs
, sizeof(struct nfsd4_channel_attrs
));
1287 memcpy(&new->se_bchannel
, battrs
, sizeof(struct nfsd4_channel_attrs
));
1292 kfree(new->se_slots
[i
]);
1297 static void free_conn(struct nfsd4_conn
*c
)
1299 svc_xprt_put(c
->cn_xprt
);
1303 static void nfsd4_conn_lost(struct svc_xpt_user
*u
)
1305 struct nfsd4_conn
*c
= container_of(u
, struct nfsd4_conn
, cn_xpt_user
);
1306 struct nfs4_client
*clp
= c
->cn_session
->se_client
;
1308 spin_lock(&clp
->cl_lock
);
1309 if (!list_empty(&c
->cn_persession
)) {
1310 list_del(&c
->cn_persession
);
1313 nfsd4_probe_callback(clp
);
1314 spin_unlock(&clp
->cl_lock
);
1317 static struct nfsd4_conn
*alloc_conn(struct svc_rqst
*rqstp
, u32 flags
)
1319 struct nfsd4_conn
*conn
;
1321 conn
= kmalloc(sizeof(struct nfsd4_conn
), GFP_KERNEL
);
1324 svc_xprt_get(rqstp
->rq_xprt
);
1325 conn
->cn_xprt
= rqstp
->rq_xprt
;
1326 conn
->cn_flags
= flags
;
1327 INIT_LIST_HEAD(&conn
->cn_xpt_user
.list
);
1331 static void __nfsd4_hash_conn(struct nfsd4_conn
*conn
, struct nfsd4_session
*ses
)
1333 conn
->cn_session
= ses
;
1334 list_add(&conn
->cn_persession
, &ses
->se_conns
);
1337 static void nfsd4_hash_conn(struct nfsd4_conn
*conn
, struct nfsd4_session
*ses
)
1339 struct nfs4_client
*clp
= ses
->se_client
;
1341 spin_lock(&clp
->cl_lock
);
1342 __nfsd4_hash_conn(conn
, ses
);
1343 spin_unlock(&clp
->cl_lock
);
1346 static int nfsd4_register_conn(struct nfsd4_conn
*conn
)
1348 conn
->cn_xpt_user
.callback
= nfsd4_conn_lost
;
1349 return register_xpt_user(conn
->cn_xprt
, &conn
->cn_xpt_user
);
1352 static void nfsd4_init_conn(struct svc_rqst
*rqstp
, struct nfsd4_conn
*conn
, struct nfsd4_session
*ses
)
1356 nfsd4_hash_conn(conn
, ses
);
1357 ret
= nfsd4_register_conn(conn
);
1359 /* oops; xprt is already down: */
1360 nfsd4_conn_lost(&conn
->cn_xpt_user
);
1361 /* We may have gained or lost a callback channel: */
1362 nfsd4_probe_callback_sync(ses
->se_client
);
1365 static struct nfsd4_conn
*alloc_conn_from_crses(struct svc_rqst
*rqstp
, struct nfsd4_create_session
*cses
)
1367 u32 dir
= NFS4_CDFC4_FORE
;
1369 if (cses
->flags
& SESSION4_BACK_CHAN
)
1370 dir
|= NFS4_CDFC4_BACK
;
1371 return alloc_conn(rqstp
, dir
);
1374 /* must be called under client_lock */
1375 static void nfsd4_del_conns(struct nfsd4_session
*s
)
1377 struct nfs4_client
*clp
= s
->se_client
;
1378 struct nfsd4_conn
*c
;
1380 spin_lock(&clp
->cl_lock
);
1381 while (!list_empty(&s
->se_conns
)) {
1382 c
= list_first_entry(&s
->se_conns
, struct nfsd4_conn
, cn_persession
);
1383 list_del_init(&c
->cn_persession
);
1384 spin_unlock(&clp
->cl_lock
);
1386 unregister_xpt_user(c
->cn_xprt
, &c
->cn_xpt_user
);
1389 spin_lock(&clp
->cl_lock
);
1391 spin_unlock(&clp
->cl_lock
);
1394 static void __free_session(struct nfsd4_session
*ses
)
1396 free_session_slots(ses
);
1400 static void free_session(struct nfsd4_session
*ses
)
1402 nfsd4_del_conns(ses
);
1403 nfsd4_put_drc_mem(&ses
->se_fchannel
);
1404 __free_session(ses
);
1407 static void init_session(struct svc_rqst
*rqstp
, struct nfsd4_session
*new, struct nfs4_client
*clp
, struct nfsd4_create_session
*cses
)
1410 struct nfsd_net
*nn
= net_generic(SVC_NET(rqstp
), nfsd_net_id
);
1412 new->se_client
= clp
;
1415 INIT_LIST_HEAD(&new->se_conns
);
1417 new->se_cb_seq_nr
= 1;
1418 new->se_flags
= cses
->flags
;
1419 new->se_cb_prog
= cses
->callback_prog
;
1420 new->se_cb_sec
= cses
->cb_sec
;
1421 atomic_set(&new->se_ref
, 0);
1422 idx
= hash_sessionid(&new->se_sessionid
);
1423 list_add(&new->se_hash
, &nn
->sessionid_hashtbl
[idx
]);
1424 spin_lock(&clp
->cl_lock
);
1425 list_add(&new->se_perclnt
, &clp
->cl_sessions
);
1426 spin_unlock(&clp
->cl_lock
);
1429 struct sockaddr
*sa
= svc_addr(rqstp
);
1431 * This is a little silly; with sessions there's no real
1432 * use for the callback address. Use the peer address
1433 * as a reasonable default for now, but consider fixing
1434 * the rpc client not to require an address in the
1437 rpc_copy_addr((struct sockaddr
*)&clp
->cl_cb_conn
.cb_addr
, sa
);
1438 clp
->cl_cb_conn
.cb_addrlen
= svc_addr_len(sa
);
1442 /* caller must hold client_lock */
1443 static struct nfsd4_session
*
1444 __find_in_sessionid_hashtbl(struct nfs4_sessionid
*sessionid
, struct net
*net
)
1446 struct nfsd4_session
*elem
;
1448 struct nfsd_net
*nn
= net_generic(net
, nfsd_net_id
);
1450 lockdep_assert_held(&nn
->client_lock
);
1452 dump_sessionid(__func__
, sessionid
);
1453 idx
= hash_sessionid(sessionid
);
1454 /* Search in the appropriate list */
1455 list_for_each_entry(elem
, &nn
->sessionid_hashtbl
[idx
], se_hash
) {
1456 if (!memcmp(elem
->se_sessionid
.data
, sessionid
->data
,
1457 NFS4_MAX_SESSIONID_LEN
)) {
1462 dprintk("%s: session not found\n", __func__
);
1466 static struct nfsd4_session
*
1467 find_in_sessionid_hashtbl(struct nfs4_sessionid
*sessionid
, struct net
*net
,
1470 struct nfsd4_session
*session
;
1471 __be32 status
= nfserr_badsession
;
1473 session
= __find_in_sessionid_hashtbl(sessionid
, net
);
1476 status
= nfsd4_get_session_locked(session
);
1484 /* caller must hold client_lock */
1486 unhash_session(struct nfsd4_session
*ses
)
1488 struct nfs4_client
*clp
= ses
->se_client
;
1489 struct nfsd_net
*nn
= net_generic(clp
->net
, nfsd_net_id
);
1491 lockdep_assert_held(&nn
->client_lock
);
1493 list_del(&ses
->se_hash
);
1494 spin_lock(&ses
->se_client
->cl_lock
);
1495 list_del(&ses
->se_perclnt
);
1496 spin_unlock(&ses
->se_client
->cl_lock
);
1499 /* SETCLIENTID and SETCLIENTID_CONFIRM Helper functions */
1501 STALE_CLIENTID(clientid_t
*clid
, struct nfsd_net
*nn
)
1504 * We're assuming the clid was not given out from a boot
1505 * precisely 2^32 (about 136 years) before this one. That seems
1506 * a safe assumption:
1508 if (clid
->cl_boot
== (u32
)nn
->boot_time
)
1510 dprintk("NFSD stale clientid (%08x/%08x) boot_time %08lx\n",
1511 clid
->cl_boot
, clid
->cl_id
, nn
->boot_time
);
1516 * XXX Should we use a slab cache ?
1517 * This type of memory management is somewhat inefficient, but we use it
1518 * anyway since SETCLIENTID is not a common operation.
1520 static struct nfs4_client
*alloc_client(struct xdr_netobj name
)
1522 struct nfs4_client
*clp
;
1525 clp
= kzalloc(sizeof(struct nfs4_client
), GFP_KERNEL
);
1528 clp
->cl_name
.data
= kmemdup(name
.data
, name
.len
, GFP_KERNEL
);
1529 if (clp
->cl_name
.data
== NULL
)
1531 clp
->cl_ownerstr_hashtbl
= kmalloc(sizeof(struct list_head
) *
1532 OWNER_HASH_SIZE
, GFP_KERNEL
);
1533 if (!clp
->cl_ownerstr_hashtbl
)
1534 goto err_no_hashtbl
;
1535 for (i
= 0; i
< OWNER_HASH_SIZE
; i
++)
1536 INIT_LIST_HEAD(&clp
->cl_ownerstr_hashtbl
[i
]);
1537 clp
->cl_name
.len
= name
.len
;
1538 INIT_LIST_HEAD(&clp
->cl_sessions
);
1539 idr_init(&clp
->cl_stateids
);
1540 atomic_set(&clp
->cl_refcount
, 0);
1541 clp
->cl_cb_state
= NFSD4_CB_UNKNOWN
;
1542 INIT_LIST_HEAD(&clp
->cl_idhash
);
1543 INIT_LIST_HEAD(&clp
->cl_openowners
);
1544 INIT_LIST_HEAD(&clp
->cl_delegations
);
1545 INIT_LIST_HEAD(&clp
->cl_lru
);
1546 INIT_LIST_HEAD(&clp
->cl_callbacks
);
1547 INIT_LIST_HEAD(&clp
->cl_revoked
);
1548 spin_lock_init(&clp
->cl_lock
);
1549 rpc_init_wait_queue(&clp
->cl_cb_waitq
, "Backchannel slot table");
1552 kfree(clp
->cl_name
.data
);
1559 free_client(struct nfs4_client
*clp
)
1561 while (!list_empty(&clp
->cl_sessions
)) {
1562 struct nfsd4_session
*ses
;
1563 ses
= list_entry(clp
->cl_sessions
.next
, struct nfsd4_session
,
1565 list_del(&ses
->se_perclnt
);
1566 WARN_ON_ONCE(atomic_read(&ses
->se_ref
));
1569 rpc_destroy_wait_queue(&clp
->cl_cb_waitq
);
1570 free_svc_cred(&clp
->cl_cred
);
1571 kfree(clp
->cl_ownerstr_hashtbl
);
1572 kfree(clp
->cl_name
.data
);
1573 idr_destroy(&clp
->cl_stateids
);
1577 /* must be called under the client_lock */
1579 unhash_client_locked(struct nfs4_client
*clp
)
1581 struct nfsd_net
*nn
= net_generic(clp
->net
, nfsd_net_id
);
1582 struct nfsd4_session
*ses
;
1584 lockdep_assert_held(&nn
->client_lock
);
1586 /* Mark the client as expired! */
1588 /* Make it invisible */
1589 if (!list_empty(&clp
->cl_idhash
)) {
1590 list_del_init(&clp
->cl_idhash
);
1591 if (test_bit(NFSD4_CLIENT_CONFIRMED
, &clp
->cl_flags
))
1592 rb_erase(&clp
->cl_namenode
, &nn
->conf_name_tree
);
1594 rb_erase(&clp
->cl_namenode
, &nn
->unconf_name_tree
);
1596 list_del_init(&clp
->cl_lru
);
1597 spin_lock(&clp
->cl_lock
);
1598 list_for_each_entry(ses
, &clp
->cl_sessions
, se_perclnt
)
1599 list_del_init(&ses
->se_hash
);
1600 spin_unlock(&clp
->cl_lock
);
1604 unhash_client(struct nfs4_client
*clp
)
1606 struct nfsd_net
*nn
= net_generic(clp
->net
, nfsd_net_id
);
1608 spin_lock(&nn
->client_lock
);
1609 unhash_client_locked(clp
);
1610 spin_unlock(&nn
->client_lock
);
1613 static __be32
mark_client_expired_locked(struct nfs4_client
*clp
)
1615 if (atomic_read(&clp
->cl_refcount
))
1616 return nfserr_jukebox
;
1617 unhash_client_locked(clp
);
1622 __destroy_client(struct nfs4_client
*clp
)
1624 struct nfs4_openowner
*oo
;
1625 struct nfs4_delegation
*dp
;
1626 struct list_head reaplist
;
1628 INIT_LIST_HEAD(&reaplist
);
1629 spin_lock(&state_lock
);
1630 while (!list_empty(&clp
->cl_delegations
)) {
1631 dp
= list_entry(clp
->cl_delegations
.next
, struct nfs4_delegation
, dl_perclnt
);
1632 unhash_delegation_locked(dp
);
1633 list_add(&dp
->dl_recall_lru
, &reaplist
);
1635 spin_unlock(&state_lock
);
1636 while (!list_empty(&reaplist
)) {
1637 dp
= list_entry(reaplist
.next
, struct nfs4_delegation
, dl_recall_lru
);
1638 list_del_init(&dp
->dl_recall_lru
);
1639 nfs4_put_deleg_lease(dp
->dl_stid
.sc_file
);
1640 nfs4_put_stid(&dp
->dl_stid
);
1642 while (!list_empty(&clp
->cl_revoked
)) {
1643 dp
= list_entry(reaplist
.next
, struct nfs4_delegation
, dl_recall_lru
);
1644 list_del_init(&dp
->dl_recall_lru
);
1645 nfs4_put_stid(&dp
->dl_stid
);
1647 while (!list_empty(&clp
->cl_openowners
)) {
1648 oo
= list_entry(clp
->cl_openowners
.next
, struct nfs4_openowner
, oo_perclient
);
1649 nfs4_get_stateowner(&oo
->oo_owner
);
1650 release_openowner(oo
);
1652 nfsd4_shutdown_callback(clp
);
1653 if (clp
->cl_cb_conn
.cb_xprt
)
1654 svc_xprt_put(clp
->cl_cb_conn
.cb_xprt
);
1659 destroy_client(struct nfs4_client
*clp
)
1662 __destroy_client(clp
);
1665 static void expire_client(struct nfs4_client
*clp
)
1668 nfsd4_client_record_remove(clp
);
1669 __destroy_client(clp
);
1672 static void copy_verf(struct nfs4_client
*target
, nfs4_verifier
*source
)
1674 memcpy(target
->cl_verifier
.data
, source
->data
,
1675 sizeof(target
->cl_verifier
.data
));
1678 static void copy_clid(struct nfs4_client
*target
, struct nfs4_client
*source
)
1680 target
->cl_clientid
.cl_boot
= source
->cl_clientid
.cl_boot
;
1681 target
->cl_clientid
.cl_id
= source
->cl_clientid
.cl_id
;
1684 static int copy_cred(struct svc_cred
*target
, struct svc_cred
*source
)
1686 if (source
->cr_principal
) {
1687 target
->cr_principal
=
1688 kstrdup(source
->cr_principal
, GFP_KERNEL
);
1689 if (target
->cr_principal
== NULL
)
1692 target
->cr_principal
= NULL
;
1693 target
->cr_flavor
= source
->cr_flavor
;
1694 target
->cr_uid
= source
->cr_uid
;
1695 target
->cr_gid
= source
->cr_gid
;
1696 target
->cr_group_info
= source
->cr_group_info
;
1697 get_group_info(target
->cr_group_info
);
1698 target
->cr_gss_mech
= source
->cr_gss_mech
;
1699 if (source
->cr_gss_mech
)
1700 gss_mech_get(source
->cr_gss_mech
);
1705 compare_blob(const struct xdr_netobj
*o1
, const struct xdr_netobj
*o2
)
1707 if (o1
->len
< o2
->len
)
1709 if (o1
->len
> o2
->len
)
1711 return memcmp(o1
->data
, o2
->data
, o1
->len
);
1714 static int same_name(const char *n1
, const char *n2
)
1716 return 0 == memcmp(n1
, n2
, HEXDIR_LEN
);
1720 same_verf(nfs4_verifier
*v1
, nfs4_verifier
*v2
)
1722 return 0 == memcmp(v1
->data
, v2
->data
, sizeof(v1
->data
));
1726 same_clid(clientid_t
*cl1
, clientid_t
*cl2
)
1728 return (cl1
->cl_boot
== cl2
->cl_boot
) && (cl1
->cl_id
== cl2
->cl_id
);
1731 static bool groups_equal(struct group_info
*g1
, struct group_info
*g2
)
1735 if (g1
->ngroups
!= g2
->ngroups
)
1737 for (i
=0; i
<g1
->ngroups
; i
++)
1738 if (!gid_eq(GROUP_AT(g1
, i
), GROUP_AT(g2
, i
)))
1744 * RFC 3530 language requires clid_inuse be returned when the
1745 * "principal" associated with a requests differs from that previously
1746 * used. We use uid, gid's, and gss principal string as our best
1747 * approximation. We also don't want to allow non-gss use of a client
1748 * established using gss: in theory cr_principal should catch that
1749 * change, but in practice cr_principal can be null even in the gss case
1750 * since gssd doesn't always pass down a principal string.
1752 static bool is_gss_cred(struct svc_cred
*cr
)
1754 /* Is cr_flavor one of the gss "pseudoflavors"?: */
1755 return (cr
->cr_flavor
> RPC_AUTH_MAXFLAVOR
);
1760 same_creds(struct svc_cred
*cr1
, struct svc_cred
*cr2
)
1762 if ((is_gss_cred(cr1
) != is_gss_cred(cr2
))
1763 || (!uid_eq(cr1
->cr_uid
, cr2
->cr_uid
))
1764 || (!gid_eq(cr1
->cr_gid
, cr2
->cr_gid
))
1765 || !groups_equal(cr1
->cr_group_info
, cr2
->cr_group_info
))
1767 if (cr1
->cr_principal
== cr2
->cr_principal
)
1769 if (!cr1
->cr_principal
|| !cr2
->cr_principal
)
1771 return 0 == strcmp(cr1
->cr_principal
, cr2
->cr_principal
);
1774 static bool svc_rqst_integrity_protected(struct svc_rqst
*rqstp
)
1776 struct svc_cred
*cr
= &rqstp
->rq_cred
;
1779 if (!cr
->cr_gss_mech
)
1781 service
= gss_pseudoflavor_to_service(cr
->cr_gss_mech
, cr
->cr_flavor
);
1782 return service
== RPC_GSS_SVC_INTEGRITY
||
1783 service
== RPC_GSS_SVC_PRIVACY
;
1786 static bool mach_creds_match(struct nfs4_client
*cl
, struct svc_rqst
*rqstp
)
1788 struct svc_cred
*cr
= &rqstp
->rq_cred
;
1790 if (!cl
->cl_mach_cred
)
1792 if (cl
->cl_cred
.cr_gss_mech
!= cr
->cr_gss_mech
)
1794 if (!svc_rqst_integrity_protected(rqstp
))
1796 if (!cr
->cr_principal
)
1798 return 0 == strcmp(cl
->cl_cred
.cr_principal
, cr
->cr_principal
);
1801 static void gen_confirm(struct nfs4_client
*clp
, struct nfsd_net
*nn
)
1806 * This is opaque to client, so no need to byte-swap. Use
1807 * __force to keep sparse happy
1809 verf
[0] = (__force __be32
)get_seconds();
1810 verf
[1] = (__force __be32
)nn
->clientid_counter
;
1811 memcpy(clp
->cl_confirm
.data
, verf
, sizeof(clp
->cl_confirm
.data
));
1814 static void gen_clid(struct nfs4_client
*clp
, struct nfsd_net
*nn
)
1816 clp
->cl_clientid
.cl_boot
= nn
->boot_time
;
1817 clp
->cl_clientid
.cl_id
= nn
->clientid_counter
++;
1818 gen_confirm(clp
, nn
);
1821 static struct nfs4_stid
*
1822 find_stateid_locked(struct nfs4_client
*cl
, stateid_t
*t
)
1824 struct nfs4_stid
*ret
;
1826 ret
= idr_find(&cl
->cl_stateids
, t
->si_opaque
.so_id
);
1827 if (!ret
|| !ret
->sc_type
)
1832 static struct nfs4_stid
*
1833 find_stateid_by_type(struct nfs4_client
*cl
, stateid_t
*t
, char typemask
)
1835 struct nfs4_stid
*s
;
1837 spin_lock(&cl
->cl_lock
);
1838 s
= find_stateid_locked(cl
, t
);
1840 if (typemask
& s
->sc_type
)
1841 atomic_inc(&s
->sc_count
);
1845 spin_unlock(&cl
->cl_lock
);
1849 static struct nfs4_client
*create_client(struct xdr_netobj name
,
1850 struct svc_rqst
*rqstp
, nfs4_verifier
*verf
)
1852 struct nfs4_client
*clp
;
1853 struct sockaddr
*sa
= svc_addr(rqstp
);
1855 struct net
*net
= SVC_NET(rqstp
);
1857 clp
= alloc_client(name
);
1861 ret
= copy_cred(&clp
->cl_cred
, &rqstp
->rq_cred
);
1866 nfsd4_init_cb(&clp
->cl_cb_null
, clp
, NULL
, NFSPROC4_CLNT_CB_NULL
);
1867 clp
->cl_time
= get_seconds();
1868 clear_bit(0, &clp
->cl_cb_slot_busy
);
1869 copy_verf(clp
, verf
);
1870 rpc_copy_addr((struct sockaddr
*) &clp
->cl_addr
, sa
);
1871 clp
->cl_cb_session
= NULL
;
1877 add_clp_to_name_tree(struct nfs4_client
*new_clp
, struct rb_root
*root
)
1879 struct rb_node
**new = &(root
->rb_node
), *parent
= NULL
;
1880 struct nfs4_client
*clp
;
1883 clp
= rb_entry(*new, struct nfs4_client
, cl_namenode
);
1886 if (compare_blob(&clp
->cl_name
, &new_clp
->cl_name
) > 0)
1887 new = &((*new)->rb_left
);
1889 new = &((*new)->rb_right
);
1892 rb_link_node(&new_clp
->cl_namenode
, parent
, new);
1893 rb_insert_color(&new_clp
->cl_namenode
, root
);
1896 static struct nfs4_client
*
1897 find_clp_in_name_tree(struct xdr_netobj
*name
, struct rb_root
*root
)
1900 struct rb_node
*node
= root
->rb_node
;
1901 struct nfs4_client
*clp
;
1904 clp
= rb_entry(node
, struct nfs4_client
, cl_namenode
);
1905 cmp
= compare_blob(&clp
->cl_name
, name
);
1907 node
= node
->rb_left
;
1909 node
= node
->rb_right
;
1917 add_to_unconfirmed(struct nfs4_client
*clp
)
1919 unsigned int idhashval
;
1920 struct nfsd_net
*nn
= net_generic(clp
->net
, nfsd_net_id
);
1922 lockdep_assert_held(&nn
->client_lock
);
1924 clear_bit(NFSD4_CLIENT_CONFIRMED
, &clp
->cl_flags
);
1925 add_clp_to_name_tree(clp
, &nn
->unconf_name_tree
);
1926 idhashval
= clientid_hashval(clp
->cl_clientid
.cl_id
);
1927 list_add(&clp
->cl_idhash
, &nn
->unconf_id_hashtbl
[idhashval
]);
1928 renew_client_locked(clp
);
1932 move_to_confirmed(struct nfs4_client
*clp
)
1934 unsigned int idhashval
= clientid_hashval(clp
->cl_clientid
.cl_id
);
1935 struct nfsd_net
*nn
= net_generic(clp
->net
, nfsd_net_id
);
1937 lockdep_assert_held(&nn
->client_lock
);
1939 dprintk("NFSD: move_to_confirm nfs4_client %p\n", clp
);
1940 list_move(&clp
->cl_idhash
, &nn
->conf_id_hashtbl
[idhashval
]);
1941 rb_erase(&clp
->cl_namenode
, &nn
->unconf_name_tree
);
1942 add_clp_to_name_tree(clp
, &nn
->conf_name_tree
);
1943 set_bit(NFSD4_CLIENT_CONFIRMED
, &clp
->cl_flags
);
1944 renew_client_locked(clp
);
1947 static struct nfs4_client
*
1948 find_client_in_id_table(struct list_head
*tbl
, clientid_t
*clid
, bool sessions
)
1950 struct nfs4_client
*clp
;
1951 unsigned int idhashval
= clientid_hashval(clid
->cl_id
);
1953 list_for_each_entry(clp
, &tbl
[idhashval
], cl_idhash
) {
1954 if (same_clid(&clp
->cl_clientid
, clid
)) {
1955 if ((bool)clp
->cl_minorversion
!= sessions
)
1957 renew_client_locked(clp
);
1964 static struct nfs4_client
*
1965 find_confirmed_client(clientid_t
*clid
, bool sessions
, struct nfsd_net
*nn
)
1967 struct list_head
*tbl
= nn
->conf_id_hashtbl
;
1969 lockdep_assert_held(&nn
->client_lock
);
1970 return find_client_in_id_table(tbl
, clid
, sessions
);
1973 static struct nfs4_client
*
1974 find_unconfirmed_client(clientid_t
*clid
, bool sessions
, struct nfsd_net
*nn
)
1976 struct list_head
*tbl
= nn
->unconf_id_hashtbl
;
1978 lockdep_assert_held(&nn
->client_lock
);
1979 return find_client_in_id_table(tbl
, clid
, sessions
);
1982 static bool clp_used_exchangeid(struct nfs4_client
*clp
)
1984 return clp
->cl_exchange_flags
!= 0;
1987 static struct nfs4_client
*
1988 find_confirmed_client_by_name(struct xdr_netobj
*name
, struct nfsd_net
*nn
)
1990 lockdep_assert_held(&nn
->client_lock
);
1991 return find_clp_in_name_tree(name
, &nn
->conf_name_tree
);
1994 static struct nfs4_client
*
1995 find_unconfirmed_client_by_name(struct xdr_netobj
*name
, struct nfsd_net
*nn
)
1997 lockdep_assert_held(&nn
->client_lock
);
1998 return find_clp_in_name_tree(name
, &nn
->unconf_name_tree
);
2002 gen_callback(struct nfs4_client
*clp
, struct nfsd4_setclientid
*se
, struct svc_rqst
*rqstp
)
2004 struct nfs4_cb_conn
*conn
= &clp
->cl_cb_conn
;
2005 struct sockaddr
*sa
= svc_addr(rqstp
);
2006 u32 scopeid
= rpc_get_scope_id(sa
);
2007 unsigned short expected_family
;
2009 /* Currently, we only support tcp and tcp6 for the callback channel */
2010 if (se
->se_callback_netid_len
== 3 &&
2011 !memcmp(se
->se_callback_netid_val
, "tcp", 3))
2012 expected_family
= AF_INET
;
2013 else if (se
->se_callback_netid_len
== 4 &&
2014 !memcmp(se
->se_callback_netid_val
, "tcp6", 4))
2015 expected_family
= AF_INET6
;
2019 conn
->cb_addrlen
= rpc_uaddr2sockaddr(clp
->net
, se
->se_callback_addr_val
,
2020 se
->se_callback_addr_len
,
2021 (struct sockaddr
*)&conn
->cb_addr
,
2022 sizeof(conn
->cb_addr
));
2024 if (!conn
->cb_addrlen
|| conn
->cb_addr
.ss_family
!= expected_family
)
2027 if (conn
->cb_addr
.ss_family
== AF_INET6
)
2028 ((struct sockaddr_in6
*)&conn
->cb_addr
)->sin6_scope_id
= scopeid
;
2030 conn
->cb_prog
= se
->se_callback_prog
;
2031 conn
->cb_ident
= se
->se_callback_ident
;
2032 memcpy(&conn
->cb_saddr
, &rqstp
->rq_daddr
, rqstp
->rq_daddrlen
);
2035 conn
->cb_addr
.ss_family
= AF_UNSPEC
;
2036 conn
->cb_addrlen
= 0;
2037 dprintk(KERN_INFO
"NFSD: this client (clientid %08x/%08x) "
2038 "will not receive delegations\n",
2039 clp
->cl_clientid
.cl_boot
, clp
->cl_clientid
.cl_id
);
2045 * Cache a reply. nfsd4_check_resp_size() has bounded the cache size.
2048 nfsd4_store_cache_entry(struct nfsd4_compoundres
*resp
)
2050 struct xdr_buf
*buf
= resp
->xdr
.buf
;
2051 struct nfsd4_slot
*slot
= resp
->cstate
.slot
;
2054 dprintk("--> %s slot %p\n", __func__
, slot
);
2056 slot
->sl_opcnt
= resp
->opcnt
;
2057 slot
->sl_status
= resp
->cstate
.status
;
2059 slot
->sl_flags
|= NFSD4_SLOT_INITIALIZED
;
2060 if (nfsd4_not_cached(resp
)) {
2061 slot
->sl_datalen
= 0;
2064 base
= resp
->cstate
.data_offset
;
2065 slot
->sl_datalen
= buf
->len
- base
;
2066 if (read_bytes_from_xdr_buf(buf
, base
, slot
->sl_data
, slot
->sl_datalen
))
2067 WARN("%s: sessions DRC could not cache compound\n", __func__
);
2072 * Encode the replay sequence operation from the slot values.
2073 * If cachethis is FALSE encode the uncached rep error on the next
2074 * operation which sets resp->p and increments resp->opcnt for
2075 * nfs4svc_encode_compoundres.
2079 nfsd4_enc_sequence_replay(struct nfsd4_compoundargs
*args
,
2080 struct nfsd4_compoundres
*resp
)
2082 struct nfsd4_op
*op
;
2083 struct nfsd4_slot
*slot
= resp
->cstate
.slot
;
2085 /* Encode the replayed sequence operation */
2086 op
= &args
->ops
[resp
->opcnt
- 1];
2087 nfsd4_encode_operation(resp
, op
);
2089 /* Return nfserr_retry_uncached_rep in next operation. */
2090 if (args
->opcnt
> 1 && !(slot
->sl_flags
& NFSD4_SLOT_CACHETHIS
)) {
2091 op
= &args
->ops
[resp
->opcnt
++];
2092 op
->status
= nfserr_retry_uncached_rep
;
2093 nfsd4_encode_operation(resp
, op
);
2099 * The sequence operation is not cached because we can use the slot and
2103 nfsd4_replay_cache_entry(struct nfsd4_compoundres
*resp
,
2104 struct nfsd4_sequence
*seq
)
2106 struct nfsd4_slot
*slot
= resp
->cstate
.slot
;
2107 struct xdr_stream
*xdr
= &resp
->xdr
;
2111 dprintk("--> %s slot %p\n", __func__
, slot
);
2113 status
= nfsd4_enc_sequence_replay(resp
->rqstp
->rq_argp
, resp
);
2117 p
= xdr_reserve_space(xdr
, slot
->sl_datalen
);
2120 return nfserr_serverfault
;
2122 xdr_encode_opaque_fixed(p
, slot
->sl_data
, slot
->sl_datalen
);
2123 xdr_commit_encode(xdr
);
2125 resp
->opcnt
= slot
->sl_opcnt
;
2126 return slot
->sl_status
;
2130 * Set the exchange_id flags returned by the server.
2133 nfsd4_set_ex_flags(struct nfs4_client
*new, struct nfsd4_exchange_id
*clid
)
2135 /* pNFS is not supported */
2136 new->cl_exchange_flags
|= EXCHGID4_FLAG_USE_NON_PNFS
;
2138 /* Referrals are supported, Migration is not. */
2139 new->cl_exchange_flags
|= EXCHGID4_FLAG_SUPP_MOVED_REFER
;
2141 /* set the wire flags to return to client. */
2142 clid
->flags
= new->cl_exchange_flags
;
2145 static bool client_has_state(struct nfs4_client
*clp
)
2148 * Note clp->cl_openowners check isn't quite right: there's no
2149 * need to count owners without stateid's.
2151 * Also note we should probably be using this in 4.0 case too.
2153 return !list_empty(&clp
->cl_openowners
)
2154 || !list_empty(&clp
->cl_delegations
)
2155 || !list_empty(&clp
->cl_sessions
);
2159 nfsd4_exchange_id(struct svc_rqst
*rqstp
,
2160 struct nfsd4_compound_state
*cstate
,
2161 struct nfsd4_exchange_id
*exid
)
2163 struct nfs4_client
*conf
, *new;
2164 struct nfs4_client
*unconf
= NULL
;
2166 char addr_str
[INET6_ADDRSTRLEN
];
2167 nfs4_verifier verf
= exid
->verifier
;
2168 struct sockaddr
*sa
= svc_addr(rqstp
);
2169 bool update
= exid
->flags
& EXCHGID4_FLAG_UPD_CONFIRMED_REC_A
;
2170 struct nfsd_net
*nn
= net_generic(SVC_NET(rqstp
), nfsd_net_id
);
2172 rpc_ntop(sa
, addr_str
, sizeof(addr_str
));
2173 dprintk("%s rqstp=%p exid=%p clname.len=%u clname.data=%p "
2174 "ip_addr=%s flags %x, spa_how %d\n",
2175 __func__
, rqstp
, exid
, exid
->clname
.len
, exid
->clname
.data
,
2176 addr_str
, exid
->flags
, exid
->spa_how
);
2178 if (exid
->flags
& ~EXCHGID4_FLAG_MASK_A
)
2179 return nfserr_inval
;
2181 switch (exid
->spa_how
) {
2183 if (!svc_rqst_integrity_protected(rqstp
))
2184 return nfserr_inval
;
2187 default: /* checked by xdr code */
2190 return nfserr_encr_alg_unsupp
;
2193 new = create_client(exid
->clname
, rqstp
, &verf
);
2195 return nfserr_jukebox
;
2197 /* Cases below refer to rfc 5661 section 18.35.4: */
2198 spin_lock(&nn
->client_lock
);
2199 conf
= find_confirmed_client_by_name(&exid
->clname
, nn
);
2201 bool creds_match
= same_creds(&conf
->cl_cred
, &rqstp
->rq_cred
);
2202 bool verfs_match
= same_verf(&verf
, &conf
->cl_verifier
);
2205 if (!clp_used_exchangeid(conf
)) { /* buggy client */
2206 status
= nfserr_inval
;
2209 if (!mach_creds_match(conf
, rqstp
)) {
2210 status
= nfserr_wrong_cred
;
2213 if (!creds_match
) { /* case 9 */
2214 status
= nfserr_perm
;
2217 if (!verfs_match
) { /* case 8 */
2218 status
= nfserr_not_same
;
2222 exid
->flags
|= EXCHGID4_FLAG_CONFIRMED_R
;
2225 if (!creds_match
) { /* case 3 */
2226 if (client_has_state(conf
)) {
2227 status
= nfserr_clid_inuse
;
2232 if (verfs_match
) { /* case 2 */
2233 conf
->cl_exchange_flags
|= EXCHGID4_FLAG_CONFIRMED_R
;
2236 /* case 5, client reboot */
2241 if (update
) { /* case 7 */
2242 status
= nfserr_noent
;
2246 unconf
= find_unconfirmed_client_by_name(&exid
->clname
, nn
);
2247 if (unconf
) /* case 4, possible retry or client restart */
2248 unhash_client_locked(unconf
);
2250 /* case 1 (normal case) */
2253 status
= mark_client_expired_locked(conf
);
2257 new->cl_minorversion
= cstate
->minorversion
;
2258 new->cl_mach_cred
= (exid
->spa_how
== SP4_MACH_CRED
);
2261 add_to_unconfirmed(new);
2264 exid
->clientid
.cl_boot
= conf
->cl_clientid
.cl_boot
;
2265 exid
->clientid
.cl_id
= conf
->cl_clientid
.cl_id
;
2267 exid
->seqid
= conf
->cl_cs_slot
.sl_seqid
+ 1;
2268 nfsd4_set_ex_flags(conf
, exid
);
2270 dprintk("nfsd4_exchange_id seqid %d flags %x\n",
2271 conf
->cl_cs_slot
.sl_seqid
, conf
->cl_exchange_flags
);
2275 spin_unlock(&nn
->client_lock
);
2279 expire_client(unconf
);
2284 check_slot_seqid(u32 seqid
, u32 slot_seqid
, int slot_inuse
)
2286 dprintk("%s enter. seqid %d slot_seqid %d\n", __func__
, seqid
,
2289 /* The slot is in use, and no response has been sent. */
2291 if (seqid
== slot_seqid
)
2292 return nfserr_jukebox
;
2294 return nfserr_seq_misordered
;
2296 /* Note unsigned 32-bit arithmetic handles wraparound: */
2297 if (likely(seqid
== slot_seqid
+ 1))
2299 if (seqid
== slot_seqid
)
2300 return nfserr_replay_cache
;
2301 return nfserr_seq_misordered
;
2305 * Cache the create session result into the create session single DRC
2306 * slot cache by saving the xdr structure. sl_seqid has been set.
2307 * Do this for solo or embedded create session operations.
2310 nfsd4_cache_create_session(struct nfsd4_create_session
*cr_ses
,
2311 struct nfsd4_clid_slot
*slot
, __be32 nfserr
)
2313 slot
->sl_status
= nfserr
;
2314 memcpy(&slot
->sl_cr_ses
, cr_ses
, sizeof(*cr_ses
));
2318 nfsd4_replay_create_session(struct nfsd4_create_session
*cr_ses
,
2319 struct nfsd4_clid_slot
*slot
)
2321 memcpy(cr_ses
, &slot
->sl_cr_ses
, sizeof(*cr_ses
));
2322 return slot
->sl_status
;
2325 #define NFSD_MIN_REQ_HDR_SEQ_SZ ((\
2326 2 * 2 + /* credential,verifier: AUTH_NULL, length 0 */ \
2327 1 + /* MIN tag is length with zero, only length */ \
2328 3 + /* version, opcount, opcode */ \
2329 XDR_QUADLEN(NFS4_MAX_SESSIONID_LEN) + \
2330 /* seqid, slotID, slotID, cache */ \
2331 4 ) * sizeof(__be32))
2333 #define NFSD_MIN_RESP_HDR_SEQ_SZ ((\
2334 2 + /* verifier: AUTH_NULL, length 0 */\
2336 1 + /* MIN tag is length with zero, only length */ \
2337 3 + /* opcount, opcode, opstatus*/ \
2338 XDR_QUADLEN(NFS4_MAX_SESSIONID_LEN) + \
2339 /* seqid, slotID, slotID, slotID, status */ \
2340 5 ) * sizeof(__be32))
2342 static __be32
check_forechannel_attrs(struct nfsd4_channel_attrs
*ca
, struct nfsd_net
*nn
)
2344 u32 maxrpc
= nn
->nfsd_serv
->sv_max_mesg
;
2346 if (ca
->maxreq_sz
< NFSD_MIN_REQ_HDR_SEQ_SZ
)
2347 return nfserr_toosmall
;
2348 if (ca
->maxresp_sz
< NFSD_MIN_RESP_HDR_SEQ_SZ
)
2349 return nfserr_toosmall
;
2350 ca
->headerpadsz
= 0;
2351 ca
->maxreq_sz
= min_t(u32
, ca
->maxreq_sz
, maxrpc
);
2352 ca
->maxresp_sz
= min_t(u32
, ca
->maxresp_sz
, maxrpc
);
2353 ca
->maxops
= min_t(u32
, ca
->maxops
, NFSD_MAX_OPS_PER_COMPOUND
);
2354 ca
->maxresp_cached
= min_t(u32
, ca
->maxresp_cached
,
2355 NFSD_SLOT_CACHE_SIZE
+ NFSD_MIN_HDR_SEQ_SZ
);
2356 ca
->maxreqs
= min_t(u32
, ca
->maxreqs
, NFSD_MAX_SLOTS_PER_SESSION
);
2358 * Note decreasing slot size below client's request may make it
2359 * difficult for client to function correctly, whereas
2360 * decreasing the number of slots will (just?) affect
2361 * performance. When short on memory we therefore prefer to
2362 * decrease number of slots instead of their size. Clients that
2363 * request larger slots than they need will get poor results:
2365 ca
->maxreqs
= nfsd4_get_drc_mem(ca
);
2367 return nfserr_jukebox
;
2372 #define NFSD_CB_MAX_REQ_SZ ((NFS4_enc_cb_recall_sz + \
2373 RPC_MAX_HEADER_WITH_AUTH) * sizeof(__be32))
2374 #define NFSD_CB_MAX_RESP_SZ ((NFS4_dec_cb_recall_sz + \
2375 RPC_MAX_REPHEADER_WITH_AUTH) * sizeof(__be32))
2377 static __be32
check_backchannel_attrs(struct nfsd4_channel_attrs
*ca
)
2379 ca
->headerpadsz
= 0;
2382 * These RPC_MAX_HEADER macros are overkill, especially since we
2383 * don't even do gss on the backchannel yet. But this is still
2384 * less than 1k. Tighten up this estimate in the unlikely event
2385 * it turns out to be a problem for some client:
2387 if (ca
->maxreq_sz
< NFSD_CB_MAX_REQ_SZ
)
2388 return nfserr_toosmall
;
2389 if (ca
->maxresp_sz
< NFSD_CB_MAX_RESP_SZ
)
2390 return nfserr_toosmall
;
2391 ca
->maxresp_cached
= 0;
2393 return nfserr_toosmall
;
2398 static __be32
nfsd4_check_cb_sec(struct nfsd4_cb_sec
*cbs
)
2400 switch (cbs
->flavor
) {
2406 * GSS case: the spec doesn't allow us to return this
2407 * error. But it also doesn't allow us not to support
2409 * I'd rather this fail hard than return some error the
2410 * client might think it can already handle:
2412 return nfserr_encr_alg_unsupp
;
2417 nfsd4_create_session(struct svc_rqst
*rqstp
,
2418 struct nfsd4_compound_state
*cstate
,
2419 struct nfsd4_create_session
*cr_ses
)
2421 struct sockaddr
*sa
= svc_addr(rqstp
);
2422 struct nfs4_client
*conf
, *unconf
;
2423 struct nfs4_client
*old
= NULL
;
2424 struct nfsd4_session
*new;
2425 struct nfsd4_conn
*conn
;
2426 struct nfsd4_clid_slot
*cs_slot
= NULL
;
2428 struct nfsd_net
*nn
= net_generic(SVC_NET(rqstp
), nfsd_net_id
);
2430 if (cr_ses
->flags
& ~SESSION4_FLAG_MASK_A
)
2431 return nfserr_inval
;
2432 status
= nfsd4_check_cb_sec(&cr_ses
->cb_sec
);
2435 status
= check_forechannel_attrs(&cr_ses
->fore_channel
, nn
);
2438 status
= check_backchannel_attrs(&cr_ses
->back_channel
);
2440 goto out_release_drc_mem
;
2441 status
= nfserr_jukebox
;
2442 new = alloc_session(&cr_ses
->fore_channel
, &cr_ses
->back_channel
);
2444 goto out_release_drc_mem
;
2445 conn
= alloc_conn_from_crses(rqstp
, cr_ses
);
2447 goto out_free_session
;
2449 spin_lock(&nn
->client_lock
);
2450 unconf
= find_unconfirmed_client(&cr_ses
->clientid
, true, nn
);
2451 conf
= find_confirmed_client(&cr_ses
->clientid
, true, nn
);
2452 WARN_ON_ONCE(conf
&& unconf
);
2455 status
= nfserr_wrong_cred
;
2456 if (!mach_creds_match(conf
, rqstp
))
2458 cs_slot
= &conf
->cl_cs_slot
;
2459 status
= check_slot_seqid(cr_ses
->seqid
, cs_slot
->sl_seqid
, 0);
2460 if (status
== nfserr_replay_cache
) {
2461 status
= nfsd4_replay_create_session(cr_ses
, cs_slot
);
2463 } else if (cr_ses
->seqid
!= cs_slot
->sl_seqid
+ 1) {
2464 status
= nfserr_seq_misordered
;
2467 } else if (unconf
) {
2468 if (!same_creds(&unconf
->cl_cred
, &rqstp
->rq_cred
) ||
2469 !rpc_cmp_addr(sa
, (struct sockaddr
*) &unconf
->cl_addr
)) {
2470 status
= nfserr_clid_inuse
;
2473 status
= nfserr_wrong_cred
;
2474 if (!mach_creds_match(unconf
, rqstp
))
2476 cs_slot
= &unconf
->cl_cs_slot
;
2477 status
= check_slot_seqid(cr_ses
->seqid
, cs_slot
->sl_seqid
, 0);
2479 /* an unconfirmed replay returns misordered */
2480 status
= nfserr_seq_misordered
;
2483 old
= find_confirmed_client_by_name(&unconf
->cl_name
, nn
);
2485 status
= mark_client_expired_locked(old
);
2491 move_to_confirmed(unconf
);
2494 status
= nfserr_stale_clientid
;
2499 * We do not support RDMA or persistent sessions
2501 cr_ses
->flags
&= ~SESSION4_PERSIST
;
2502 cr_ses
->flags
&= ~SESSION4_RDMA
;
2504 init_session(rqstp
, new, conf
, cr_ses
);
2505 nfsd4_get_session_locked(new);
2507 memcpy(cr_ses
->sessionid
.data
, new->se_sessionid
.data
,
2508 NFS4_MAX_SESSIONID_LEN
);
2509 cs_slot
->sl_seqid
++;
2510 cr_ses
->seqid
= cs_slot
->sl_seqid
;
2512 /* cache solo and embedded create sessions under the client_lock */
2513 nfsd4_cache_create_session(cr_ses
, cs_slot
, status
);
2514 spin_unlock(&nn
->client_lock
);
2515 /* init connection and backchannel */
2516 nfsd4_init_conn(rqstp
, conn
, new);
2517 nfsd4_put_session(new);
2522 spin_unlock(&nn
->client_lock
);
2527 __free_session(new);
2528 out_release_drc_mem
:
2529 nfsd4_put_drc_mem(&cr_ses
->fore_channel
);
2533 static __be32
nfsd4_map_bcts_dir(u32
*dir
)
2536 case NFS4_CDFC4_FORE
:
2537 case NFS4_CDFC4_BACK
:
2539 case NFS4_CDFC4_FORE_OR_BOTH
:
2540 case NFS4_CDFC4_BACK_OR_BOTH
:
2541 *dir
= NFS4_CDFC4_BOTH
;
2544 return nfserr_inval
;
2547 __be32
nfsd4_backchannel_ctl(struct svc_rqst
*rqstp
, struct nfsd4_compound_state
*cstate
, struct nfsd4_backchannel_ctl
*bc
)
2549 struct nfsd4_session
*session
= cstate
->session
;
2550 struct nfsd_net
*nn
= net_generic(SVC_NET(rqstp
), nfsd_net_id
);
2553 status
= nfsd4_check_cb_sec(&bc
->bc_cb_sec
);
2556 spin_lock(&nn
->client_lock
);
2557 session
->se_cb_prog
= bc
->bc_cb_program
;
2558 session
->se_cb_sec
= bc
->bc_cb_sec
;
2559 spin_unlock(&nn
->client_lock
);
2561 nfsd4_probe_callback(session
->se_client
);
2566 __be32
nfsd4_bind_conn_to_session(struct svc_rqst
*rqstp
,
2567 struct nfsd4_compound_state
*cstate
,
2568 struct nfsd4_bind_conn_to_session
*bcts
)
2571 struct nfsd4_conn
*conn
;
2572 struct nfsd4_session
*session
;
2573 struct net
*net
= SVC_NET(rqstp
);
2574 struct nfsd_net
*nn
= net_generic(net
, nfsd_net_id
);
2576 if (!nfsd4_last_compound_op(rqstp
))
2577 return nfserr_not_only_op
;
2578 spin_lock(&nn
->client_lock
);
2579 session
= find_in_sessionid_hashtbl(&bcts
->sessionid
, net
, &status
);
2580 spin_unlock(&nn
->client_lock
);
2582 goto out_no_session
;
2583 status
= nfserr_wrong_cred
;
2584 if (!mach_creds_match(session
->se_client
, rqstp
))
2586 status
= nfsd4_map_bcts_dir(&bcts
->dir
);
2589 conn
= alloc_conn(rqstp
, bcts
->dir
);
2590 status
= nfserr_jukebox
;
2593 nfsd4_init_conn(rqstp
, conn
, session
);
2596 nfsd4_put_session(session
);
2601 static bool nfsd4_compound_in_session(struct nfsd4_session
*session
, struct nfs4_sessionid
*sid
)
2605 return !memcmp(sid
, &session
->se_sessionid
, sizeof(*sid
));
2609 nfsd4_destroy_session(struct svc_rqst
*r
,
2610 struct nfsd4_compound_state
*cstate
,
2611 struct nfsd4_destroy_session
*sessionid
)
2613 struct nfsd4_session
*ses
;
2615 int ref_held_by_me
= 0;
2616 struct net
*net
= SVC_NET(r
);
2617 struct nfsd_net
*nn
= net_generic(net
, nfsd_net_id
);
2619 status
= nfserr_not_only_op
;
2620 if (nfsd4_compound_in_session(cstate
->session
, &sessionid
->sessionid
)) {
2621 if (!nfsd4_last_compound_op(r
))
2625 dump_sessionid(__func__
, &sessionid
->sessionid
);
2626 spin_lock(&nn
->client_lock
);
2627 ses
= find_in_sessionid_hashtbl(&sessionid
->sessionid
, net
, &status
);
2629 goto out_client_lock
;
2630 status
= nfserr_wrong_cred
;
2631 if (!mach_creds_match(ses
->se_client
, r
))
2632 goto out_put_session
;
2633 status
= mark_session_dead_locked(ses
, 1 + ref_held_by_me
);
2635 goto out_put_session
;
2636 unhash_session(ses
);
2637 spin_unlock(&nn
->client_lock
);
2639 nfsd4_probe_callback_sync(ses
->se_client
);
2641 spin_lock(&nn
->client_lock
);
2644 nfsd4_put_session_locked(ses
);
2646 spin_unlock(&nn
->client_lock
);
2651 static struct nfsd4_conn
*__nfsd4_find_conn(struct svc_xprt
*xpt
, struct nfsd4_session
*s
)
2653 struct nfsd4_conn
*c
;
2655 list_for_each_entry(c
, &s
->se_conns
, cn_persession
) {
2656 if (c
->cn_xprt
== xpt
) {
2663 static __be32
nfsd4_sequence_check_conn(struct nfsd4_conn
*new, struct nfsd4_session
*ses
)
2665 struct nfs4_client
*clp
= ses
->se_client
;
2666 struct nfsd4_conn
*c
;
2667 __be32 status
= nfs_ok
;
2670 spin_lock(&clp
->cl_lock
);
2671 c
= __nfsd4_find_conn(new->cn_xprt
, ses
);
2674 status
= nfserr_conn_not_bound_to_session
;
2675 if (clp
->cl_mach_cred
)
2677 __nfsd4_hash_conn(new, ses
);
2678 spin_unlock(&clp
->cl_lock
);
2679 ret
= nfsd4_register_conn(new);
2681 /* oops; xprt is already down: */
2682 nfsd4_conn_lost(&new->cn_xpt_user
);
2685 spin_unlock(&clp
->cl_lock
);
2690 static bool nfsd4_session_too_many_ops(struct svc_rqst
*rqstp
, struct nfsd4_session
*session
)
2692 struct nfsd4_compoundargs
*args
= rqstp
->rq_argp
;
2694 return args
->opcnt
> session
->se_fchannel
.maxops
;
2697 static bool nfsd4_request_too_big(struct svc_rqst
*rqstp
,
2698 struct nfsd4_session
*session
)
2700 struct xdr_buf
*xb
= &rqstp
->rq_arg
;
2702 return xb
->len
> session
->se_fchannel
.maxreq_sz
;
2706 nfsd4_sequence(struct svc_rqst
*rqstp
,
2707 struct nfsd4_compound_state
*cstate
,
2708 struct nfsd4_sequence
*seq
)
2710 struct nfsd4_compoundres
*resp
= rqstp
->rq_resp
;
2711 struct xdr_stream
*xdr
= &resp
->xdr
;
2712 struct nfsd4_session
*session
;
2713 struct nfs4_client
*clp
;
2714 struct nfsd4_slot
*slot
;
2715 struct nfsd4_conn
*conn
;
2718 struct net
*net
= SVC_NET(rqstp
);
2719 struct nfsd_net
*nn
= net_generic(net
, nfsd_net_id
);
2721 if (resp
->opcnt
!= 1)
2722 return nfserr_sequence_pos
;
2725 * Will be either used or freed by nfsd4_sequence_check_conn
2728 conn
= alloc_conn(rqstp
, NFS4_CDFC4_FORE
);
2730 return nfserr_jukebox
;
2732 spin_lock(&nn
->client_lock
);
2733 session
= find_in_sessionid_hashtbl(&seq
->sessionid
, net
, &status
);
2735 goto out_no_session
;
2736 clp
= session
->se_client
;
2738 status
= nfserr_too_many_ops
;
2739 if (nfsd4_session_too_many_ops(rqstp
, session
))
2740 goto out_put_session
;
2742 status
= nfserr_req_too_big
;
2743 if (nfsd4_request_too_big(rqstp
, session
))
2744 goto out_put_session
;
2746 status
= nfserr_badslot
;
2747 if (seq
->slotid
>= session
->se_fchannel
.maxreqs
)
2748 goto out_put_session
;
2750 slot
= session
->se_slots
[seq
->slotid
];
2751 dprintk("%s: slotid %d\n", __func__
, seq
->slotid
);
2753 /* We do not negotiate the number of slots yet, so set the
2754 * maxslots to the session maxreqs which is used to encode
2755 * sr_highest_slotid and the sr_target_slot id to maxslots */
2756 seq
->maxslots
= session
->se_fchannel
.maxreqs
;
2758 status
= check_slot_seqid(seq
->seqid
, slot
->sl_seqid
,
2759 slot
->sl_flags
& NFSD4_SLOT_INUSE
);
2760 if (status
== nfserr_replay_cache
) {
2761 status
= nfserr_seq_misordered
;
2762 if (!(slot
->sl_flags
& NFSD4_SLOT_INITIALIZED
))
2763 goto out_put_session
;
2764 cstate
->slot
= slot
;
2765 cstate
->session
= session
;
2767 /* Return the cached reply status and set cstate->status
2768 * for nfsd4_proc_compound processing */
2769 status
= nfsd4_replay_cache_entry(resp
, seq
);
2770 cstate
->status
= nfserr_replay_cache
;
2774 goto out_put_session
;
2776 status
= nfsd4_sequence_check_conn(conn
, session
);
2779 goto out_put_session
;
2781 buflen
= (seq
->cachethis
) ?
2782 session
->se_fchannel
.maxresp_cached
:
2783 session
->se_fchannel
.maxresp_sz
;
2784 status
= (seq
->cachethis
) ? nfserr_rep_too_big_to_cache
:
2786 if (xdr_restrict_buflen(xdr
, buflen
- rqstp
->rq_auth_slack
))
2787 goto out_put_session
;
2788 svc_reserve(rqstp
, buflen
);
2791 /* Success! bump slot seqid */
2792 slot
->sl_seqid
= seq
->seqid
;
2793 slot
->sl_flags
|= NFSD4_SLOT_INUSE
;
2795 slot
->sl_flags
|= NFSD4_SLOT_CACHETHIS
;
2797 slot
->sl_flags
&= ~NFSD4_SLOT_CACHETHIS
;
2799 cstate
->slot
= slot
;
2800 cstate
->session
= session
;
2804 switch (clp
->cl_cb_state
) {
2806 seq
->status_flags
= SEQ4_STATUS_CB_PATH_DOWN
;
2808 case NFSD4_CB_FAULT
:
2809 seq
->status_flags
= SEQ4_STATUS_BACKCHANNEL_FAULT
;
2812 seq
->status_flags
= 0;
2814 if (!list_empty(&clp
->cl_revoked
))
2815 seq
->status_flags
|= SEQ4_STATUS_RECALLABLE_STATE_REVOKED
;
2819 spin_unlock(&nn
->client_lock
);
2822 nfsd4_put_session_locked(session
);
2823 goto out_no_session
;
2827 nfsd4_sequence_done(struct nfsd4_compoundres
*resp
)
2829 struct nfsd4_compound_state
*cs
= &resp
->cstate
;
2831 if (nfsd4_has_session(cs
)) {
2832 if (cs
->status
!= nfserr_replay_cache
) {
2833 nfsd4_store_cache_entry(resp
);
2834 cs
->slot
->sl_flags
&= ~NFSD4_SLOT_INUSE
;
2836 /* Drop session reference that was taken in nfsd4_sequence() */
2837 nfsd4_put_session(cs
->session
);
2839 put_client_renew(cs
->clp
);
2843 nfsd4_destroy_clientid(struct svc_rqst
*rqstp
, struct nfsd4_compound_state
*cstate
, struct nfsd4_destroy_clientid
*dc
)
2845 struct nfs4_client
*conf
, *unconf
;
2846 struct nfs4_client
*clp
= NULL
;
2848 struct nfsd_net
*nn
= net_generic(SVC_NET(rqstp
), nfsd_net_id
);
2850 spin_lock(&nn
->client_lock
);
2851 unconf
= find_unconfirmed_client(&dc
->clientid
, true, nn
);
2852 conf
= find_confirmed_client(&dc
->clientid
, true, nn
);
2853 WARN_ON_ONCE(conf
&& unconf
);
2856 if (client_has_state(conf
)) {
2857 status
= nfserr_clientid_busy
;
2860 status
= mark_client_expired_locked(conf
);
2867 status
= nfserr_stale_clientid
;
2870 if (!mach_creds_match(clp
, rqstp
)) {
2872 status
= nfserr_wrong_cred
;
2875 unhash_client_locked(clp
);
2877 spin_unlock(&nn
->client_lock
);
2884 nfsd4_reclaim_complete(struct svc_rqst
*rqstp
, struct nfsd4_compound_state
*cstate
, struct nfsd4_reclaim_complete
*rc
)
2888 if (rc
->rca_one_fs
) {
2889 if (!cstate
->current_fh
.fh_dentry
)
2890 return nfserr_nofilehandle
;
2892 * We don't take advantage of the rca_one_fs case.
2893 * That's OK, it's optional, we can safely ignore it.
2898 status
= nfserr_complete_already
;
2899 if (test_and_set_bit(NFSD4_CLIENT_RECLAIM_COMPLETE
,
2900 &cstate
->session
->se_client
->cl_flags
))
2903 status
= nfserr_stale_clientid
;
2904 if (is_client_expired(cstate
->session
->se_client
))
2906 * The following error isn't really legal.
2907 * But we only get here if the client just explicitly
2908 * destroyed the client. Surely it no longer cares what
2909 * error it gets back on an operation for the dead
2915 nfsd4_client_record_create(cstate
->session
->se_client
);
2921 nfsd4_setclientid(struct svc_rqst
*rqstp
, struct nfsd4_compound_state
*cstate
,
2922 struct nfsd4_setclientid
*setclid
)
2924 struct xdr_netobj clname
= setclid
->se_name
;
2925 nfs4_verifier clverifier
= setclid
->se_verf
;
2926 struct nfs4_client
*conf
, *new;
2927 struct nfs4_client
*unconf
= NULL
;
2929 struct nfsd_net
*nn
= net_generic(SVC_NET(rqstp
), nfsd_net_id
);
2931 new = create_client(clname
, rqstp
, &clverifier
);
2933 return nfserr_jukebox
;
2934 /* Cases below refer to rfc 3530 section 14.2.33: */
2935 spin_lock(&nn
->client_lock
);
2936 conf
= find_confirmed_client_by_name(&clname
, nn
);
2939 status
= nfserr_clid_inuse
;
2940 if (clp_used_exchangeid(conf
))
2942 if (!same_creds(&conf
->cl_cred
, &rqstp
->rq_cred
)) {
2943 char addr_str
[INET6_ADDRSTRLEN
];
2944 rpc_ntop((struct sockaddr
*) &conf
->cl_addr
, addr_str
,
2946 dprintk("NFSD: setclientid: string in use by client "
2947 "at %s\n", addr_str
);
2951 unconf
= find_unconfirmed_client_by_name(&clname
, nn
);
2953 unhash_client_locked(unconf
);
2954 if (conf
&& same_verf(&conf
->cl_verifier
, &clverifier
))
2955 /* case 1: probable callback update */
2956 copy_clid(new, conf
);
2957 else /* case 4 (new client) or cases 2, 3 (client reboot): */
2959 new->cl_minorversion
= 0;
2960 gen_callback(new, setclid
, rqstp
);
2961 add_to_unconfirmed(new);
2962 setclid
->se_clientid
.cl_boot
= new->cl_clientid
.cl_boot
;
2963 setclid
->se_clientid
.cl_id
= new->cl_clientid
.cl_id
;
2964 memcpy(setclid
->se_confirm
.data
, new->cl_confirm
.data
, sizeof(setclid
->se_confirm
.data
));
2968 spin_unlock(&nn
->client_lock
);
2972 expire_client(unconf
);
2978 nfsd4_setclientid_confirm(struct svc_rqst
*rqstp
,
2979 struct nfsd4_compound_state
*cstate
,
2980 struct nfsd4_setclientid_confirm
*setclientid_confirm
)
2982 struct nfs4_client
*conf
, *unconf
;
2983 struct nfs4_client
*old
= NULL
;
2984 nfs4_verifier confirm
= setclientid_confirm
->sc_confirm
;
2985 clientid_t
* clid
= &setclientid_confirm
->sc_clientid
;
2987 struct nfsd_net
*nn
= net_generic(SVC_NET(rqstp
), nfsd_net_id
);
2989 if (STALE_CLIENTID(clid
, nn
))
2990 return nfserr_stale_clientid
;
2992 spin_lock(&nn
->client_lock
);
2993 conf
= find_confirmed_client(clid
, false, nn
);
2994 unconf
= find_unconfirmed_client(clid
, false, nn
);
2996 * We try hard to give out unique clientid's, so if we get an
2997 * attempt to confirm the same clientid with a different cred,
2998 * there's a bug somewhere. Let's charitably assume it's our
3001 status
= nfserr_serverfault
;
3002 if (unconf
&& !same_creds(&unconf
->cl_cred
, &rqstp
->rq_cred
))
3004 if (conf
&& !same_creds(&conf
->cl_cred
, &rqstp
->rq_cred
))
3006 /* cases below refer to rfc 3530 section 14.2.34: */
3007 if (!unconf
|| !same_verf(&confirm
, &unconf
->cl_confirm
)) {
3008 if (conf
&& !unconf
) /* case 2: probable retransmit */
3010 else /* case 4: client hasn't noticed we rebooted yet? */
3011 status
= nfserr_stale_clientid
;
3015 if (conf
) { /* case 1: callback update */
3017 unhash_client_locked(old
);
3018 nfsd4_change_callback(conf
, &unconf
->cl_cb_conn
);
3019 } else { /* case 3: normal case; new or rebooted client */
3020 old
= find_confirmed_client_by_name(&unconf
->cl_name
, nn
);
3022 status
= mark_client_expired_locked(old
);
3028 move_to_confirmed(unconf
);
3031 get_client_locked(conf
);
3032 spin_unlock(&nn
->client_lock
);
3033 nfsd4_probe_callback(conf
);
3034 spin_lock(&nn
->client_lock
);
3035 put_client_renew_locked(conf
);
3037 spin_unlock(&nn
->client_lock
);
3043 static struct nfs4_file
*nfsd4_alloc_file(void)
3045 return kmem_cache_alloc(file_slab
, GFP_KERNEL
);
3048 /* OPEN Share state helper functions */
3049 static void nfsd4_init_file(struct knfsd_fh
*fh
, unsigned int hashval
,
3050 struct nfs4_file
*fp
)
3052 lockdep_assert_held(&state_lock
);
3054 atomic_set(&fp
->fi_ref
, 1);
3055 spin_lock_init(&fp
->fi_lock
);
3056 INIT_LIST_HEAD(&fp
->fi_stateids
);
3057 INIT_LIST_HEAD(&fp
->fi_delegations
);
3058 fh_copy_shallow(&fp
->fi_fhandle
, fh
);
3059 fp
->fi_deleg_file
= NULL
;
3060 fp
->fi_had_conflict
= false;
3061 fp
->fi_share_deny
= 0;
3062 memset(fp
->fi_fds
, 0, sizeof(fp
->fi_fds
));
3063 memset(fp
->fi_access
, 0, sizeof(fp
->fi_access
));
3064 hlist_add_head_rcu(&fp
->fi_hash
, &file_hashtbl
[hashval
]);
3068 nfsd4_free_slabs(void)
3070 kmem_cache_destroy(openowner_slab
);
3071 kmem_cache_destroy(lockowner_slab
);
3072 kmem_cache_destroy(file_slab
);
3073 kmem_cache_destroy(stateid_slab
);
3074 kmem_cache_destroy(deleg_slab
);
3078 nfsd4_init_slabs(void)
3080 openowner_slab
= kmem_cache_create("nfsd4_openowners",
3081 sizeof(struct nfs4_openowner
), 0, 0, NULL
);
3082 if (openowner_slab
== NULL
)
3084 lockowner_slab
= kmem_cache_create("nfsd4_lockowners",
3085 sizeof(struct nfs4_lockowner
), 0, 0, NULL
);
3086 if (lockowner_slab
== NULL
)
3087 goto out_free_openowner_slab
;
3088 file_slab
= kmem_cache_create("nfsd4_files",
3089 sizeof(struct nfs4_file
), 0, 0, NULL
);
3090 if (file_slab
== NULL
)
3091 goto out_free_lockowner_slab
;
3092 stateid_slab
= kmem_cache_create("nfsd4_stateids",
3093 sizeof(struct nfs4_ol_stateid
), 0, 0, NULL
);
3094 if (stateid_slab
== NULL
)
3095 goto out_free_file_slab
;
3096 deleg_slab
= kmem_cache_create("nfsd4_delegations",
3097 sizeof(struct nfs4_delegation
), 0, 0, NULL
);
3098 if (deleg_slab
== NULL
)
3099 goto out_free_stateid_slab
;
3102 out_free_stateid_slab
:
3103 kmem_cache_destroy(stateid_slab
);
3105 kmem_cache_destroy(file_slab
);
3106 out_free_lockowner_slab
:
3107 kmem_cache_destroy(lockowner_slab
);
3108 out_free_openowner_slab
:
3109 kmem_cache_destroy(openowner_slab
);
3111 dprintk("nfsd4: out of memory while initializing nfsv4\n");
3115 static void init_nfs4_replay(struct nfs4_replay
*rp
)
3117 rp
->rp_status
= nfserr_serverfault
;
3119 rp
->rp_buf
= rp
->rp_ibuf
;
3120 mutex_init(&rp
->rp_mutex
);
3123 static void nfsd4_cstate_assign_replay(struct nfsd4_compound_state
*cstate
,
3124 struct nfs4_stateowner
*so
)
3126 if (!nfsd4_has_session(cstate
)) {
3127 mutex_lock(&so
->so_replay
.rp_mutex
);
3128 cstate
->replay_owner
= nfs4_get_stateowner(so
);
3132 void nfsd4_cstate_clear_replay(struct nfsd4_compound_state
*cstate
)
3134 struct nfs4_stateowner
*so
= cstate
->replay_owner
;
3137 cstate
->replay_owner
= NULL
;
3138 mutex_unlock(&so
->so_replay
.rp_mutex
);
3139 nfs4_put_stateowner(so
);
3143 static inline void *alloc_stateowner(struct kmem_cache
*slab
, struct xdr_netobj
*owner
, struct nfs4_client
*clp
)
3145 struct nfs4_stateowner
*sop
;
3147 sop
= kmem_cache_alloc(slab
, GFP_KERNEL
);
3151 sop
->so_owner
.data
= kmemdup(owner
->data
, owner
->len
, GFP_KERNEL
);
3152 if (!sop
->so_owner
.data
) {
3153 kmem_cache_free(slab
, sop
);
3156 sop
->so_owner
.len
= owner
->len
;
3158 INIT_LIST_HEAD(&sop
->so_stateids
);
3159 sop
->so_client
= clp
;
3160 init_nfs4_replay(&sop
->so_replay
);
3161 atomic_set(&sop
->so_count
, 1);
3165 static void hash_openowner(struct nfs4_openowner
*oo
, struct nfs4_client
*clp
, unsigned int strhashval
)
3167 lockdep_assert_held(&clp
->cl_lock
);
3169 list_add(&oo
->oo_owner
.so_strhash
,
3170 &clp
->cl_ownerstr_hashtbl
[strhashval
]);
3171 list_add(&oo
->oo_perclient
, &clp
->cl_openowners
);
3174 static void nfs4_unhash_openowner(struct nfs4_stateowner
*so
)
3176 unhash_openowner_locked(openowner(so
));
3179 static void nfs4_free_openowner(struct nfs4_stateowner
*so
)
3181 struct nfs4_openowner
*oo
= openowner(so
);
3183 kmem_cache_free(openowner_slab
, oo
);
3186 static const struct nfs4_stateowner_operations openowner_ops
= {
3187 .so_unhash
= nfs4_unhash_openowner
,
3188 .so_free
= nfs4_free_openowner
,
3191 static struct nfs4_openowner
*
3192 alloc_init_open_stateowner(unsigned int strhashval
, struct nfsd4_open
*open
,
3193 struct nfsd4_compound_state
*cstate
)
3195 struct nfs4_client
*clp
= cstate
->clp
;
3196 struct nfs4_openowner
*oo
, *ret
;
3198 oo
= alloc_stateowner(openowner_slab
, &open
->op_owner
, clp
);
3201 oo
->oo_owner
.so_ops
= &openowner_ops
;
3202 oo
->oo_owner
.so_is_open_owner
= 1;
3203 oo
->oo_owner
.so_seqid
= open
->op_seqid
;
3205 if (nfsd4_has_session(cstate
))
3206 oo
->oo_flags
|= NFS4_OO_CONFIRMED
;
3208 oo
->oo_last_closed_stid
= NULL
;
3209 INIT_LIST_HEAD(&oo
->oo_close_lru
);
3210 spin_lock(&clp
->cl_lock
);
3211 ret
= find_openstateowner_str_locked(strhashval
, open
, clp
);
3213 hash_openowner(oo
, clp
, strhashval
);
3216 nfs4_free_openowner(&oo
->oo_owner
);
3217 spin_unlock(&clp
->cl_lock
);
3221 static void init_open_stateid(struct nfs4_ol_stateid
*stp
, struct nfs4_file
*fp
, struct nfsd4_open
*open
) {
3222 struct nfs4_openowner
*oo
= open
->op_openowner
;
3224 atomic_inc(&stp
->st_stid
.sc_count
);
3225 stp
->st_stid
.sc_type
= NFS4_OPEN_STID
;
3226 INIT_LIST_HEAD(&stp
->st_locks
);
3227 stp
->st_stateowner
= nfs4_get_stateowner(&oo
->oo_owner
);
3229 stp
->st_stid
.sc_file
= fp
;
3230 stp
->st_access_bmap
= 0;
3231 stp
->st_deny_bmap
= 0;
3232 stp
->st_openstp
= NULL
;
3233 spin_lock(&oo
->oo_owner
.so_client
->cl_lock
);
3234 list_add(&stp
->st_perstateowner
, &oo
->oo_owner
.so_stateids
);
3235 spin_lock(&fp
->fi_lock
);
3236 list_add(&stp
->st_perfile
, &fp
->fi_stateids
);
3237 spin_unlock(&fp
->fi_lock
);
3238 spin_unlock(&oo
->oo_owner
.so_client
->cl_lock
);
3242 * In the 4.0 case we need to keep the owners around a little while to handle
3243 * CLOSE replay. We still do need to release any file access that is held by
3244 * them before returning however.
3247 move_to_close_lru(struct nfs4_ol_stateid
*s
, struct net
*net
)
3249 struct nfs4_ol_stateid
*last
;
3250 struct nfs4_openowner
*oo
= openowner(s
->st_stateowner
);
3251 struct nfsd_net
*nn
= net_generic(s
->st_stid
.sc_client
->net
,
3254 dprintk("NFSD: move_to_close_lru nfs4_openowner %p\n", oo
);
3257 * We know that we hold one reference via nfsd4_close, and another
3258 * "persistent" reference for the client. If the refcount is higher
3259 * than 2, then there are still calls in progress that are using this
3260 * stateid. We can't put the sc_file reference until they are finished.
3261 * Wait for the refcount to drop to 2. Since it has been unhashed,
3262 * there should be no danger of the refcount going back up again at
3265 wait_event(close_wq
, atomic_read(&s
->st_stid
.sc_count
) == 2);
3267 release_all_access(s
);
3268 if (s
->st_stid
.sc_file
) {
3269 put_nfs4_file(s
->st_stid
.sc_file
);
3270 s
->st_stid
.sc_file
= NULL
;
3273 spin_lock(&nn
->client_lock
);
3274 last
= oo
->oo_last_closed_stid
;
3275 oo
->oo_last_closed_stid
= s
;
3276 list_move_tail(&oo
->oo_close_lru
, &nn
->close_lru
);
3277 oo
->oo_time
= get_seconds();
3278 spin_unlock(&nn
->client_lock
);
3280 nfs4_put_stid(&last
->st_stid
);
3283 /* search file_hashtbl[] for file */
3284 static struct nfs4_file
*
3285 find_file_locked(struct knfsd_fh
*fh
, unsigned int hashval
)
3287 struct nfs4_file
*fp
;
3289 hlist_for_each_entry_rcu(fp
, &file_hashtbl
[hashval
], fi_hash
) {
3290 if (fh_match(&fp
->fi_fhandle
, fh
)) {
3291 if (atomic_inc_not_zero(&fp
->fi_ref
))
3298 static struct nfs4_file
*
3299 find_file(struct knfsd_fh
*fh
)
3301 struct nfs4_file
*fp
;
3302 unsigned int hashval
= file_hashval(fh
);
3305 fp
= find_file_locked(fh
, hashval
);
3310 static struct nfs4_file
*
3311 find_or_add_file(struct nfs4_file
*new, struct knfsd_fh
*fh
)
3313 struct nfs4_file
*fp
;
3314 unsigned int hashval
= file_hashval(fh
);
3317 fp
= find_file_locked(fh
, hashval
);
3322 spin_lock(&state_lock
);
3323 fp
= find_file_locked(fh
, hashval
);
3324 if (likely(fp
== NULL
)) {
3325 nfsd4_init_file(fh
, hashval
, new);
3328 spin_unlock(&state_lock
);
3334 * Called to check deny when READ with all zero stateid or
3335 * WRITE with all zero or all one stateid
3338 nfs4_share_conflict(struct svc_fh
*current_fh
, unsigned int deny_type
)
3340 struct nfs4_file
*fp
;
3341 __be32 ret
= nfs_ok
;
3343 fp
= find_file(¤t_fh
->fh_handle
);
3346 /* Check for conflicting share reservations */
3347 spin_lock(&fp
->fi_lock
);
3348 if (fp
->fi_share_deny
& deny_type
)
3349 ret
= nfserr_locked
;
3350 spin_unlock(&fp
->fi_lock
);
3355 static void nfsd4_cb_recall_prepare(struct nfsd4_callback
*cb
)
3357 struct nfs4_delegation
*dp
= cb_to_delegation(cb
);
3358 struct nfsd_net
*nn
= net_generic(dp
->dl_stid
.sc_client
->net
,
3361 block_delegations(&dp
->dl_stid
.sc_file
->fi_fhandle
);
3364 * We can't do this in nfsd_break_deleg_cb because it is
3365 * already holding inode->i_lock.
3367 * If the dl_time != 0, then we know that it has already been
3368 * queued for a lease break. Don't queue it again.
3370 spin_lock(&state_lock
);
3371 if (dp
->dl_time
== 0) {
3372 dp
->dl_time
= get_seconds();
3373 list_add_tail(&dp
->dl_recall_lru
, &nn
->del_recall_lru
);
3375 spin_unlock(&state_lock
);
3378 static int nfsd4_cb_recall_done(struct nfsd4_callback
*cb
,
3379 struct rpc_task
*task
)
3381 struct nfs4_delegation
*dp
= cb_to_delegation(cb
);
3383 switch (task
->tk_status
) {
3387 case -NFS4ERR_BAD_STATEID
:
3389 * Race: client probably got cb_recall before open reply
3390 * granting delegation.
3392 if (dp
->dl_retries
--) {
3393 rpc_delay(task
, 2 * HZ
);
3402 static void nfsd4_cb_recall_release(struct nfsd4_callback
*cb
)
3404 struct nfs4_delegation
*dp
= cb_to_delegation(cb
);
3406 nfs4_put_stid(&dp
->dl_stid
);
3409 static struct nfsd4_callback_ops nfsd4_cb_recall_ops
= {
3410 .prepare
= nfsd4_cb_recall_prepare
,
3411 .done
= nfsd4_cb_recall_done
,
3412 .release
= nfsd4_cb_recall_release
,
3415 static void nfsd_break_one_deleg(struct nfs4_delegation
*dp
)
3418 * We're assuming the state code never drops its reference
3419 * without first removing the lease. Since we're in this lease
3420 * callback (and since the lease code is serialized by the kernel
3421 * lock) we know the server hasn't removed the lease yet, we know
3422 * it's safe to take a reference.
3424 atomic_inc(&dp
->dl_stid
.sc_count
);
3425 nfsd4_run_cb(&dp
->dl_recall
);
3428 /* Called from break_lease() with i_lock held. */
3430 nfsd_break_deleg_cb(struct file_lock
*fl
)
3433 struct nfs4_file
*fp
= (struct nfs4_file
*)fl
->fl_owner
;
3434 struct nfs4_delegation
*dp
;
3437 WARN(1, "(%p)->fl_owner NULL\n", fl
);
3440 if (fp
->fi_had_conflict
) {
3441 WARN(1, "duplicate break on %p\n", fp
);
3445 * We don't want the locks code to timeout the lease for us;
3446 * we'll remove it ourself if a delegation isn't returned
3449 fl
->fl_break_time
= 0;
3451 spin_lock(&fp
->fi_lock
);
3452 fp
->fi_had_conflict
= true;
3454 * If there are no delegations on the list, then return true
3455 * so that the lease code will go ahead and delete it.
3457 if (list_empty(&fp
->fi_delegations
))
3460 list_for_each_entry(dp
, &fp
->fi_delegations
, dl_perfile
)
3461 nfsd_break_one_deleg(dp
);
3462 spin_unlock(&fp
->fi_lock
);
3467 nfsd_change_deleg_cb(struct file_lock
*onlist
, int arg
,
3468 struct list_head
*dispose
)
3471 return lease_modify(onlist
, arg
, dispose
);
3476 static const struct lock_manager_operations nfsd_lease_mng_ops
= {
3477 .lm_break
= nfsd_break_deleg_cb
,
3478 .lm_change
= nfsd_change_deleg_cb
,
3481 static __be32
nfsd4_check_seqid(struct nfsd4_compound_state
*cstate
, struct nfs4_stateowner
*so
, u32 seqid
)
3483 if (nfsd4_has_session(cstate
))
3485 if (seqid
== so
->so_seqid
- 1)
3486 return nfserr_replay_me
;
3487 if (seqid
== so
->so_seqid
)
3489 return nfserr_bad_seqid
;
3492 static __be32
lookup_clientid(clientid_t
*clid
,
3493 struct nfsd4_compound_state
*cstate
,
3494 struct nfsd_net
*nn
)
3496 struct nfs4_client
*found
;
3499 found
= cstate
->clp
;
3500 if (!same_clid(&found
->cl_clientid
, clid
))
3501 return nfserr_stale_clientid
;
3505 if (STALE_CLIENTID(clid
, nn
))
3506 return nfserr_stale_clientid
;
3509 * For v4.1+ we get the client in the SEQUENCE op. If we don't have one
3510 * cached already then we know this is for is for v4.0 and "sessions"
3513 WARN_ON_ONCE(cstate
->session
);
3514 spin_lock(&nn
->client_lock
);
3515 found
= find_confirmed_client(clid
, false, nn
);
3517 spin_unlock(&nn
->client_lock
);
3518 return nfserr_expired
;
3520 atomic_inc(&found
->cl_refcount
);
3521 spin_unlock(&nn
->client_lock
);
3523 /* Cache the nfs4_client in cstate! */
3524 cstate
->clp
= found
;
3529 nfsd4_process_open1(struct nfsd4_compound_state
*cstate
,
3530 struct nfsd4_open
*open
, struct nfsd_net
*nn
)
3532 clientid_t
*clientid
= &open
->op_clientid
;
3533 struct nfs4_client
*clp
= NULL
;
3534 unsigned int strhashval
;
3535 struct nfs4_openowner
*oo
= NULL
;
3538 if (STALE_CLIENTID(&open
->op_clientid
, nn
))
3539 return nfserr_stale_clientid
;
3541 * In case we need it later, after we've already created the
3542 * file and don't want to risk a further failure:
3544 open
->op_file
= nfsd4_alloc_file();
3545 if (open
->op_file
== NULL
)
3546 return nfserr_jukebox
;
3548 status
= lookup_clientid(clientid
, cstate
, nn
);
3553 strhashval
= ownerstr_hashval(&open
->op_owner
);
3554 oo
= find_openstateowner_str(strhashval
, open
, clp
);
3555 open
->op_openowner
= oo
;
3559 if (!(oo
->oo_flags
& NFS4_OO_CONFIRMED
)) {
3560 /* Replace unconfirmed owners without checking for replay. */
3561 release_openowner(oo
);
3562 open
->op_openowner
= NULL
;
3565 status
= nfsd4_check_seqid(cstate
, &oo
->oo_owner
, open
->op_seqid
);
3570 oo
= alloc_init_open_stateowner(strhashval
, open
, cstate
);
3572 return nfserr_jukebox
;
3573 open
->op_openowner
= oo
;
3575 open
->op_stp
= nfs4_alloc_open_stateid(clp
);
3577 return nfserr_jukebox
;
3581 static inline __be32
3582 nfs4_check_delegmode(struct nfs4_delegation
*dp
, int flags
)
3584 if ((flags
& WR_STATE
) && (dp
->dl_type
== NFS4_OPEN_DELEGATE_READ
))
3585 return nfserr_openmode
;
3590 static int share_access_to_flags(u32 share_access
)
3592 return share_access
== NFS4_SHARE_ACCESS_READ
? RD_STATE
: WR_STATE
;
3595 static struct nfs4_delegation
*find_deleg_stateid(struct nfs4_client
*cl
, stateid_t
*s
)
3597 struct nfs4_stid
*ret
;
3599 ret
= find_stateid_by_type(cl
, s
, NFS4_DELEG_STID
);
3602 return delegstateid(ret
);
3605 static bool nfsd4_is_deleg_cur(struct nfsd4_open
*open
)
3607 return open
->op_claim_type
== NFS4_OPEN_CLAIM_DELEGATE_CUR
||
3608 open
->op_claim_type
== NFS4_OPEN_CLAIM_DELEG_CUR_FH
;
3612 nfs4_check_deleg(struct nfs4_client
*cl
, struct nfsd4_open
*open
,
3613 struct nfs4_delegation
**dp
)
3616 __be32 status
= nfserr_bad_stateid
;
3617 struct nfs4_delegation
*deleg
;
3619 deleg
= find_deleg_stateid(cl
, &open
->op_delegate_stateid
);
3622 flags
= share_access_to_flags(open
->op_share_access
);
3623 status
= nfs4_check_delegmode(deleg
, flags
);
3625 nfs4_put_stid(&deleg
->dl_stid
);
3630 if (!nfsd4_is_deleg_cur(open
))
3634 open
->op_openowner
->oo_flags
|= NFS4_OO_CONFIRMED
;
3638 static struct nfs4_ol_stateid
*
3639 nfsd4_find_existing_open(struct nfs4_file
*fp
, struct nfsd4_open
*open
)
3641 struct nfs4_ol_stateid
*local
, *ret
= NULL
;
3642 struct nfs4_openowner
*oo
= open
->op_openowner
;
3644 spin_lock(&fp
->fi_lock
);
3645 list_for_each_entry(local
, &fp
->fi_stateids
, st_perfile
) {
3646 /* ignore lock owners */
3647 if (local
->st_stateowner
->so_is_open_owner
== 0)
3649 if (local
->st_stateowner
== &oo
->oo_owner
) {
3651 atomic_inc(&ret
->st_stid
.sc_count
);
3655 spin_unlock(&fp
->fi_lock
);
3659 static inline int nfs4_access_to_access(u32 nfs4_access
)
3663 if (nfs4_access
& NFS4_SHARE_ACCESS_READ
)
3664 flags
|= NFSD_MAY_READ
;
3665 if (nfs4_access
& NFS4_SHARE_ACCESS_WRITE
)
3666 flags
|= NFSD_MAY_WRITE
;
3670 static inline __be32
3671 nfsd4_truncate(struct svc_rqst
*rqstp
, struct svc_fh
*fh
,
3672 struct nfsd4_open
*open
)
3674 struct iattr iattr
= {
3675 .ia_valid
= ATTR_SIZE
,
3678 if (!open
->op_truncate
)
3680 if (!(open
->op_share_access
& NFS4_SHARE_ACCESS_WRITE
))
3681 return nfserr_inval
;
3682 return nfsd_setattr(rqstp
, fh
, &iattr
, 0, (time_t)0);
3685 static __be32
nfs4_get_vfs_file(struct svc_rqst
*rqstp
, struct nfs4_file
*fp
,
3686 struct svc_fh
*cur_fh
, struct nfs4_ol_stateid
*stp
,
3687 struct nfsd4_open
*open
)
3689 struct file
*filp
= NULL
;
3691 int oflag
= nfs4_access_to_omode(open
->op_share_access
);
3692 int access
= nfs4_access_to_access(open
->op_share_access
);
3693 unsigned char old_access_bmap
, old_deny_bmap
;
3695 spin_lock(&fp
->fi_lock
);
3698 * Are we trying to set a deny mode that would conflict with
3701 status
= nfs4_file_check_deny(fp
, open
->op_share_deny
);
3702 if (status
!= nfs_ok
) {
3703 spin_unlock(&fp
->fi_lock
);
3707 /* set access to the file */
3708 status
= nfs4_file_get_access(fp
, open
->op_share_access
);
3709 if (status
!= nfs_ok
) {
3710 spin_unlock(&fp
->fi_lock
);
3714 /* Set access bits in stateid */
3715 old_access_bmap
= stp
->st_access_bmap
;
3716 set_access(open
->op_share_access
, stp
);
3718 /* Set new deny mask */
3719 old_deny_bmap
= stp
->st_deny_bmap
;
3720 set_deny(open
->op_share_deny
, stp
);
3721 fp
->fi_share_deny
|= (open
->op_share_deny
& NFS4_SHARE_DENY_BOTH
);
3723 if (!fp
->fi_fds
[oflag
]) {
3724 spin_unlock(&fp
->fi_lock
);
3725 status
= nfsd_open(rqstp
, cur_fh
, S_IFREG
, access
, &filp
);
3727 goto out_put_access
;
3728 spin_lock(&fp
->fi_lock
);
3729 if (!fp
->fi_fds
[oflag
]) {
3730 fp
->fi_fds
[oflag
] = filp
;
3734 spin_unlock(&fp
->fi_lock
);
3738 status
= nfsd4_truncate(rqstp
, cur_fh
, open
);
3740 goto out_put_access
;
3744 stp
->st_access_bmap
= old_access_bmap
;
3745 nfs4_file_put_access(fp
, open
->op_share_access
);
3746 reset_union_bmap_deny(bmap_to_share_mode(old_deny_bmap
), stp
);
3751 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
)
3754 unsigned char old_deny_bmap
;
3756 if (!test_access(open
->op_share_access
, stp
))
3757 return nfs4_get_vfs_file(rqstp
, fp
, cur_fh
, stp
, open
);
3759 /* test and set deny mode */
3760 spin_lock(&fp
->fi_lock
);
3761 status
= nfs4_file_check_deny(fp
, open
->op_share_deny
);
3762 if (status
== nfs_ok
) {
3763 old_deny_bmap
= stp
->st_deny_bmap
;
3764 set_deny(open
->op_share_deny
, stp
);
3765 fp
->fi_share_deny
|=
3766 (open
->op_share_deny
& NFS4_SHARE_DENY_BOTH
);
3768 spin_unlock(&fp
->fi_lock
);
3770 if (status
!= nfs_ok
)
3773 status
= nfsd4_truncate(rqstp
, cur_fh
, open
);
3774 if (status
!= nfs_ok
)
3775 reset_union_bmap_deny(old_deny_bmap
, stp
);
3780 nfs4_set_claim_prev(struct nfsd4_open
*open
, bool has_session
)
3782 open
->op_openowner
->oo_flags
|= NFS4_OO_CONFIRMED
;
3785 /* Should we give out recallable state?: */
3786 static bool nfsd4_cb_channel_good(struct nfs4_client
*clp
)
3788 if (clp
->cl_cb_state
== NFSD4_CB_UP
)
3791 * In the sessions case, since we don't have to establish a
3792 * separate connection for callbacks, we assume it's OK
3793 * until we hear otherwise:
3795 return clp
->cl_minorversion
&& clp
->cl_cb_state
== NFSD4_CB_UNKNOWN
;
3798 static struct file_lock
*nfs4_alloc_init_lease(struct nfs4_file
*fp
, int flag
)
3800 struct file_lock
*fl
;
3802 fl
= locks_alloc_lock();
3805 fl
->fl_lmops
= &nfsd_lease_mng_ops
;
3806 fl
->fl_flags
= FL_DELEG
;
3807 fl
->fl_type
= flag
== NFS4_OPEN_DELEGATE_READ
? F_RDLCK
: F_WRLCK
;
3808 fl
->fl_end
= OFFSET_MAX
;
3809 fl
->fl_owner
= (fl_owner_t
)fp
;
3810 fl
->fl_pid
= current
->tgid
;
3814 static int nfs4_setlease(struct nfs4_delegation
*dp
)
3816 struct nfs4_file
*fp
= dp
->dl_stid
.sc_file
;
3817 struct file_lock
*fl
, *ret
;
3821 fl
= nfs4_alloc_init_lease(fp
, NFS4_OPEN_DELEGATE_READ
);
3824 filp
= find_readable_file(fp
);
3826 /* We should always have a readable file here */
3832 status
= vfs_setlease(filp
, fl
->fl_type
, &fl
, NULL
);
3834 locks_free_lock(fl
);
3837 spin_lock(&state_lock
);
3838 spin_lock(&fp
->fi_lock
);
3839 /* Did the lease get broken before we took the lock? */
3841 if (fp
->fi_had_conflict
)
3844 if (fp
->fi_deleg_file
) {
3847 hash_delegation_locked(dp
, fp
);
3850 fp
->fi_deleg_file
= filp
;
3851 fp
->fi_delegees
= 1;
3852 hash_delegation_locked(dp
, fp
);
3853 spin_unlock(&fp
->fi_lock
);
3854 spin_unlock(&state_lock
);
3857 spin_unlock(&fp
->fi_lock
);
3858 spin_unlock(&state_lock
);
3864 static struct nfs4_delegation
*
3865 nfs4_set_delegation(struct nfs4_client
*clp
, struct svc_fh
*fh
,
3866 struct nfs4_file
*fp
)
3869 struct nfs4_delegation
*dp
;
3871 if (fp
->fi_had_conflict
)
3872 return ERR_PTR(-EAGAIN
);
3874 dp
= alloc_init_deleg(clp
, fh
);
3876 return ERR_PTR(-ENOMEM
);
3879 spin_lock(&state_lock
);
3880 spin_lock(&fp
->fi_lock
);
3881 dp
->dl_stid
.sc_file
= fp
;
3882 if (!fp
->fi_deleg_file
) {
3883 spin_unlock(&fp
->fi_lock
);
3884 spin_unlock(&state_lock
);
3885 status
= nfs4_setlease(dp
);
3888 if (fp
->fi_had_conflict
) {
3893 hash_delegation_locked(dp
, fp
);
3896 spin_unlock(&fp
->fi_lock
);
3897 spin_unlock(&state_lock
);
3900 nfs4_put_stid(&dp
->dl_stid
);
3901 return ERR_PTR(status
);
3906 static void nfsd4_open_deleg_none_ext(struct nfsd4_open
*open
, int status
)
3908 open
->op_delegate_type
= NFS4_OPEN_DELEGATE_NONE_EXT
;
3909 if (status
== -EAGAIN
)
3910 open
->op_why_no_deleg
= WND4_CONTENTION
;
3912 open
->op_why_no_deleg
= WND4_RESOURCE
;
3913 switch (open
->op_deleg_want
) {
3914 case NFS4_SHARE_WANT_READ_DELEG
:
3915 case NFS4_SHARE_WANT_WRITE_DELEG
:
3916 case NFS4_SHARE_WANT_ANY_DELEG
:
3918 case NFS4_SHARE_WANT_CANCEL
:
3919 open
->op_why_no_deleg
= WND4_CANCELLED
;
3921 case NFS4_SHARE_WANT_NO_DELEG
:
3928 * Attempt to hand out a delegation.
3930 * Note we don't support write delegations, and won't until the vfs has
3931 * proper support for them.
3934 nfs4_open_delegation(struct svc_fh
*fh
, struct nfsd4_open
*open
,
3935 struct nfs4_ol_stateid
*stp
)
3937 struct nfs4_delegation
*dp
;
3938 struct nfs4_openowner
*oo
= openowner(stp
->st_stateowner
);
3939 struct nfs4_client
*clp
= stp
->st_stid
.sc_client
;
3943 cb_up
= nfsd4_cb_channel_good(oo
->oo_owner
.so_client
);
3944 open
->op_recall
= 0;
3945 switch (open
->op_claim_type
) {
3946 case NFS4_OPEN_CLAIM_PREVIOUS
:
3948 open
->op_recall
= 1;
3949 if (open
->op_delegate_type
!= NFS4_OPEN_DELEGATE_READ
)
3952 case NFS4_OPEN_CLAIM_NULL
:
3953 case NFS4_OPEN_CLAIM_FH
:
3955 * Let's not give out any delegations till everyone's
3956 * had the chance to reclaim theirs....
3958 if (locks_in_grace(clp
->net
))
3960 if (!cb_up
|| !(oo
->oo_flags
& NFS4_OO_CONFIRMED
))
3963 * Also, if the file was opened for write or
3964 * create, there's a good chance the client's
3965 * about to write to it, resulting in an
3966 * immediate recall (since we don't support
3967 * write delegations):
3969 if (open
->op_share_access
& NFS4_SHARE_ACCESS_WRITE
)
3971 if (open
->op_create
== NFS4_OPEN_CREATE
)
3977 dp
= nfs4_set_delegation(clp
, fh
, stp
->st_stid
.sc_file
);
3981 memcpy(&open
->op_delegate_stateid
, &dp
->dl_stid
.sc_stateid
, sizeof(dp
->dl_stid
.sc_stateid
));
3983 dprintk("NFSD: delegation stateid=" STATEID_FMT
"\n",
3984 STATEID_VAL(&dp
->dl_stid
.sc_stateid
));
3985 open
->op_delegate_type
= NFS4_OPEN_DELEGATE_READ
;
3986 nfs4_put_stid(&dp
->dl_stid
);
3989 open
->op_delegate_type
= NFS4_OPEN_DELEGATE_NONE
;
3990 if (open
->op_claim_type
== NFS4_OPEN_CLAIM_PREVIOUS
&&
3991 open
->op_delegate_type
!= NFS4_OPEN_DELEGATE_NONE
) {
3992 dprintk("NFSD: WARNING: refusing delegation reclaim\n");
3993 open
->op_recall
= 1;
3996 /* 4.1 client asking for a delegation? */
3997 if (open
->op_deleg_want
)
3998 nfsd4_open_deleg_none_ext(open
, status
);
4002 static void nfsd4_deleg_xgrade_none_ext(struct nfsd4_open
*open
,
4003 struct nfs4_delegation
*dp
)
4005 if (open
->op_deleg_want
== NFS4_SHARE_WANT_READ_DELEG
&&
4006 dp
->dl_type
== NFS4_OPEN_DELEGATE_WRITE
) {
4007 open
->op_delegate_type
= NFS4_OPEN_DELEGATE_NONE_EXT
;
4008 open
->op_why_no_deleg
= WND4_NOT_SUPP_DOWNGRADE
;
4009 } else if (open
->op_deleg_want
== NFS4_SHARE_WANT_WRITE_DELEG
&&
4010 dp
->dl_type
== NFS4_OPEN_DELEGATE_WRITE
) {
4011 open
->op_delegate_type
= NFS4_OPEN_DELEGATE_NONE_EXT
;
4012 open
->op_why_no_deleg
= WND4_NOT_SUPP_UPGRADE
;
4014 /* Otherwise the client must be confused wanting a delegation
4015 * it already has, therefore we don't return
4016 * NFS4_OPEN_DELEGATE_NONE_EXT and reason.
4021 nfsd4_process_open2(struct svc_rqst
*rqstp
, struct svc_fh
*current_fh
, struct nfsd4_open
*open
)
4023 struct nfsd4_compoundres
*resp
= rqstp
->rq_resp
;
4024 struct nfs4_client
*cl
= open
->op_openowner
->oo_owner
.so_client
;
4025 struct nfs4_file
*fp
= NULL
;
4026 struct nfs4_ol_stateid
*stp
= NULL
;
4027 struct nfs4_delegation
*dp
= NULL
;
4031 * Lookup file; if found, lookup stateid and check open request,
4032 * and check for delegations in the process of being recalled.
4033 * If not found, create the nfs4_file struct
4035 fp
= find_or_add_file(open
->op_file
, ¤t_fh
->fh_handle
);
4036 if (fp
!= open
->op_file
) {
4037 status
= nfs4_check_deleg(cl
, open
, &dp
);
4040 stp
= nfsd4_find_existing_open(fp
, open
);
4042 open
->op_file
= NULL
;
4043 status
= nfserr_bad_stateid
;
4044 if (nfsd4_is_deleg_cur(open
))
4046 status
= nfserr_jukebox
;
4050 * OPEN the file, or upgrade an existing OPEN.
4051 * If truncate fails, the OPEN fails.
4054 /* Stateid was found, this is an OPEN upgrade */
4055 status
= nfs4_upgrade_open(rqstp
, fp
, current_fh
, stp
, open
);
4060 open
->op_stp
= NULL
;
4061 init_open_stateid(stp
, fp
, open
);
4062 status
= nfs4_get_vfs_file(rqstp
, fp
, current_fh
, stp
, open
);
4064 release_open_stateid(stp
);
4068 update_stateid(&stp
->st_stid
.sc_stateid
);
4069 memcpy(&open
->op_stateid
, &stp
->st_stid
.sc_stateid
, sizeof(stateid_t
));
4071 if (nfsd4_has_session(&resp
->cstate
)) {
4072 if (open
->op_deleg_want
& NFS4_SHARE_WANT_NO_DELEG
) {
4073 open
->op_delegate_type
= NFS4_OPEN_DELEGATE_NONE_EXT
;
4074 open
->op_why_no_deleg
= WND4_NOT_WANTED
;
4080 * Attempt to hand out a delegation. No error return, because the
4081 * OPEN succeeds even if we fail.
4083 nfs4_open_delegation(current_fh
, open
, stp
);
4087 dprintk("%s: stateid=" STATEID_FMT
"\n", __func__
,
4088 STATEID_VAL(&stp
->st_stid
.sc_stateid
));
4090 /* 4.1 client trying to upgrade/downgrade delegation? */
4091 if (open
->op_delegate_type
== NFS4_OPEN_DELEGATE_NONE
&& dp
&&
4092 open
->op_deleg_want
)
4093 nfsd4_deleg_xgrade_none_ext(open
, dp
);
4097 if (status
== 0 && open
->op_claim_type
== NFS4_OPEN_CLAIM_PREVIOUS
)
4098 nfs4_set_claim_prev(open
, nfsd4_has_session(&resp
->cstate
));
4100 * To finish the open response, we just need to set the rflags.
4102 open
->op_rflags
= NFS4_OPEN_RESULT_LOCKTYPE_POSIX
;
4103 if (!(open
->op_openowner
->oo_flags
& NFS4_OO_CONFIRMED
) &&
4104 !nfsd4_has_session(&resp
->cstate
))
4105 open
->op_rflags
|= NFS4_OPEN_RESULT_CONFIRM
;
4107 nfs4_put_stid(&dp
->dl_stid
);
4109 nfs4_put_stid(&stp
->st_stid
);
4114 void nfsd4_cleanup_open_state(struct nfsd4_compound_state
*cstate
,
4115 struct nfsd4_open
*open
, __be32 status
)
4117 if (open
->op_openowner
) {
4118 struct nfs4_stateowner
*so
= &open
->op_openowner
->oo_owner
;
4120 nfsd4_cstate_assign_replay(cstate
, so
);
4121 nfs4_put_stateowner(so
);
4124 kmem_cache_free(file_slab
, open
->op_file
);
4126 nfs4_put_stid(&open
->op_stp
->st_stid
);
4130 nfsd4_renew(struct svc_rqst
*rqstp
, struct nfsd4_compound_state
*cstate
,
4133 struct nfs4_client
*clp
;
4135 struct nfsd_net
*nn
= net_generic(SVC_NET(rqstp
), nfsd_net_id
);
4137 dprintk("process_renew(%08x/%08x): starting\n",
4138 clid
->cl_boot
, clid
->cl_id
);
4139 status
= lookup_clientid(clid
, cstate
, nn
);
4143 status
= nfserr_cb_path_down
;
4144 if (!list_empty(&clp
->cl_delegations
)
4145 && clp
->cl_cb_state
!= NFSD4_CB_UP
)
4153 nfsd4_end_grace(struct nfsd_net
*nn
)
4155 /* do nothing if grace period already ended */
4156 if (nn
->grace_ended
)
4159 dprintk("NFSD: end of grace period\n");
4160 nn
->grace_ended
= true;
4162 * If the server goes down again right now, an NFSv4
4163 * client will still be allowed to reclaim after it comes back up,
4164 * even if it hasn't yet had a chance to reclaim state this time.
4167 nfsd4_record_grace_done(nn
);
4169 * At this point, NFSv4 clients can still reclaim. But if the
4170 * server crashes, any that have not yet reclaimed will be out
4171 * of luck on the next boot.
4173 * (NFSv4.1+ clients are considered to have reclaimed once they
4174 * call RECLAIM_COMPLETE. NFSv4.0 clients are considered to
4175 * have reclaimed after their first OPEN.)
4177 locks_end_grace(&nn
->nfsd4_manager
);
4179 * At this point, and once lockd and/or any other containers
4180 * exit their grace period, further reclaims will fail and
4181 * regular locking can resume.
4186 nfs4_laundromat(struct nfsd_net
*nn
)
4188 struct nfs4_client
*clp
;
4189 struct nfs4_openowner
*oo
;
4190 struct nfs4_delegation
*dp
;
4191 struct nfs4_ol_stateid
*stp
;
4192 struct list_head
*pos
, *next
, reaplist
;
4193 time_t cutoff
= get_seconds() - nn
->nfsd4_lease
;
4194 time_t t
, new_timeo
= nn
->nfsd4_lease
;
4196 dprintk("NFSD: laundromat service - starting\n");
4197 nfsd4_end_grace(nn
);
4198 INIT_LIST_HEAD(&reaplist
);
4199 spin_lock(&nn
->client_lock
);
4200 list_for_each_safe(pos
, next
, &nn
->client_lru
) {
4201 clp
= list_entry(pos
, struct nfs4_client
, cl_lru
);
4202 if (time_after((unsigned long)clp
->cl_time
, (unsigned long)cutoff
)) {
4203 t
= clp
->cl_time
- cutoff
;
4204 new_timeo
= min(new_timeo
, t
);
4207 if (mark_client_expired_locked(clp
)) {
4208 dprintk("NFSD: client in use (clientid %08x)\n",
4209 clp
->cl_clientid
.cl_id
);
4212 list_add(&clp
->cl_lru
, &reaplist
);
4214 spin_unlock(&nn
->client_lock
);
4215 list_for_each_safe(pos
, next
, &reaplist
) {
4216 clp
= list_entry(pos
, struct nfs4_client
, cl_lru
);
4217 dprintk("NFSD: purging unused client (clientid %08x)\n",
4218 clp
->cl_clientid
.cl_id
);
4219 list_del_init(&clp
->cl_lru
);
4222 spin_lock(&state_lock
);
4223 list_for_each_safe(pos
, next
, &nn
->del_recall_lru
) {
4224 dp
= list_entry (pos
, struct nfs4_delegation
, dl_recall_lru
);
4225 if (net_generic(dp
->dl_stid
.sc_client
->net
, nfsd_net_id
) != nn
)
4227 if (time_after((unsigned long)dp
->dl_time
, (unsigned long)cutoff
)) {
4228 t
= dp
->dl_time
- cutoff
;
4229 new_timeo
= min(new_timeo
, t
);
4232 unhash_delegation_locked(dp
);
4233 list_add(&dp
->dl_recall_lru
, &reaplist
);
4235 spin_unlock(&state_lock
);
4236 while (!list_empty(&reaplist
)) {
4237 dp
= list_first_entry(&reaplist
, struct nfs4_delegation
,
4239 list_del_init(&dp
->dl_recall_lru
);
4240 revoke_delegation(dp
);
4243 spin_lock(&nn
->client_lock
);
4244 while (!list_empty(&nn
->close_lru
)) {
4245 oo
= list_first_entry(&nn
->close_lru
, struct nfs4_openowner
,
4247 if (time_after((unsigned long)oo
->oo_time
,
4248 (unsigned long)cutoff
)) {
4249 t
= oo
->oo_time
- cutoff
;
4250 new_timeo
= min(new_timeo
, t
);
4253 list_del_init(&oo
->oo_close_lru
);
4254 stp
= oo
->oo_last_closed_stid
;
4255 oo
->oo_last_closed_stid
= NULL
;
4256 spin_unlock(&nn
->client_lock
);
4257 nfs4_put_stid(&stp
->st_stid
);
4258 spin_lock(&nn
->client_lock
);
4260 spin_unlock(&nn
->client_lock
);
4262 new_timeo
= max_t(time_t, new_timeo
, NFSD_LAUNDROMAT_MINTIMEOUT
);
4266 static struct workqueue_struct
*laundry_wq
;
4267 static void laundromat_main(struct work_struct
*);
4270 laundromat_main(struct work_struct
*laundry
)
4273 struct delayed_work
*dwork
= container_of(laundry
, struct delayed_work
,
4275 struct nfsd_net
*nn
= container_of(dwork
, struct nfsd_net
,
4278 t
= nfs4_laundromat(nn
);
4279 dprintk("NFSD: laundromat_main - sleeping for %ld seconds\n", t
);
4280 queue_delayed_work(laundry_wq
, &nn
->laundromat_work
, t
*HZ
);
4283 static inline __be32
nfs4_check_fh(struct svc_fh
*fhp
, struct nfs4_ol_stateid
*stp
)
4285 if (!fh_match(&fhp
->fh_handle
, &stp
->st_stid
.sc_file
->fi_fhandle
))
4286 return nfserr_bad_stateid
;
4291 access_permit_read(struct nfs4_ol_stateid
*stp
)
4293 return test_access(NFS4_SHARE_ACCESS_READ
, stp
) ||
4294 test_access(NFS4_SHARE_ACCESS_BOTH
, stp
) ||
4295 test_access(NFS4_SHARE_ACCESS_WRITE
, stp
);
4299 access_permit_write(struct nfs4_ol_stateid
*stp
)
4301 return test_access(NFS4_SHARE_ACCESS_WRITE
, stp
) ||
4302 test_access(NFS4_SHARE_ACCESS_BOTH
, stp
);
4306 __be32
nfs4_check_openmode(struct nfs4_ol_stateid
*stp
, int flags
)
4308 __be32 status
= nfserr_openmode
;
4310 /* For lock stateid's, we test the parent open, not the lock: */
4311 if (stp
->st_openstp
)
4312 stp
= stp
->st_openstp
;
4313 if ((flags
& WR_STATE
) && !access_permit_write(stp
))
4315 if ((flags
& RD_STATE
) && !access_permit_read(stp
))
4322 static inline __be32
4323 check_special_stateids(struct net
*net
, svc_fh
*current_fh
, stateid_t
*stateid
, int flags
)
4325 if (ONE_STATEID(stateid
) && (flags
& RD_STATE
))
4327 else if (locks_in_grace(net
)) {
4328 /* Answer in remaining cases depends on existence of
4329 * conflicting state; so we must wait out the grace period. */
4330 return nfserr_grace
;
4331 } else if (flags
& WR_STATE
)
4332 return nfs4_share_conflict(current_fh
,
4333 NFS4_SHARE_DENY_WRITE
);
4334 else /* (flags & RD_STATE) && ZERO_STATEID(stateid) */
4335 return nfs4_share_conflict(current_fh
,
4336 NFS4_SHARE_DENY_READ
);
4340 * Allow READ/WRITE during grace period on recovered state only for files
4341 * that are not able to provide mandatory locking.
4344 grace_disallows_io(struct net
*net
, struct inode
*inode
)
4346 return locks_in_grace(net
) && mandatory_lock(inode
);
4349 /* Returns true iff a is later than b: */
4350 static bool stateid_generation_after(stateid_t
*a
, stateid_t
*b
)
4352 return (s32
)(a
->si_generation
- b
->si_generation
) > 0;
4355 static __be32
check_stateid_generation(stateid_t
*in
, stateid_t
*ref
, bool has_session
)
4358 * When sessions are used the stateid generation number is ignored
4361 if (has_session
&& in
->si_generation
== 0)
4364 if (in
->si_generation
== ref
->si_generation
)
4367 /* If the client sends us a stateid from the future, it's buggy: */
4368 if (stateid_generation_after(in
, ref
))
4369 return nfserr_bad_stateid
;
4371 * However, we could see a stateid from the past, even from a
4372 * non-buggy client. For example, if the client sends a lock
4373 * while some IO is outstanding, the lock may bump si_generation
4374 * while the IO is still in flight. The client could avoid that
4375 * situation by waiting for responses on all the IO requests,
4376 * but better performance may result in retrying IO that
4377 * receives an old_stateid error if requests are rarely
4378 * reordered in flight:
4380 return nfserr_old_stateid
;
4383 static __be32
nfsd4_validate_stateid(struct nfs4_client
*cl
, stateid_t
*stateid
)
4385 struct nfs4_stid
*s
;
4386 struct nfs4_ol_stateid
*ols
;
4387 __be32 status
= nfserr_bad_stateid
;
4389 if (ZERO_STATEID(stateid
) || ONE_STATEID(stateid
))
4391 /* Client debugging aid. */
4392 if (!same_clid(&stateid
->si_opaque
.so_clid
, &cl
->cl_clientid
)) {
4393 char addr_str
[INET6_ADDRSTRLEN
];
4394 rpc_ntop((struct sockaddr
*)&cl
->cl_addr
, addr_str
,
4396 pr_warn_ratelimited("NFSD: client %s testing state ID "
4397 "with incorrect client ID\n", addr_str
);
4400 spin_lock(&cl
->cl_lock
);
4401 s
= find_stateid_locked(cl
, stateid
);
4404 status
= check_stateid_generation(stateid
, &s
->sc_stateid
, 1);
4407 switch (s
->sc_type
) {
4408 case NFS4_DELEG_STID
:
4411 case NFS4_REVOKED_DELEG_STID
:
4412 status
= nfserr_deleg_revoked
;
4414 case NFS4_OPEN_STID
:
4415 case NFS4_LOCK_STID
:
4416 ols
= openlockstateid(s
);
4417 if (ols
->st_stateowner
->so_is_open_owner
4418 && !(openowner(ols
->st_stateowner
)->oo_flags
4419 & NFS4_OO_CONFIRMED
))
4420 status
= nfserr_bad_stateid
;
4425 printk("unknown stateid type %x\n", s
->sc_type
);
4427 case NFS4_CLOSED_STID
:
4428 case NFS4_CLOSED_DELEG_STID
:
4429 status
= nfserr_bad_stateid
;
4432 spin_unlock(&cl
->cl_lock
);
4437 nfsd4_lookup_stateid(struct nfsd4_compound_state
*cstate
,
4438 stateid_t
*stateid
, unsigned char typemask
,
4439 struct nfs4_stid
**s
, struct nfsd_net
*nn
)
4443 if (ZERO_STATEID(stateid
) || ONE_STATEID(stateid
))
4444 return nfserr_bad_stateid
;
4445 status
= lookup_clientid(&stateid
->si_opaque
.so_clid
, cstate
, nn
);
4446 if (status
== nfserr_stale_clientid
) {
4447 if (cstate
->session
)
4448 return nfserr_bad_stateid
;
4449 return nfserr_stale_stateid
;
4453 *s
= find_stateid_by_type(cstate
->clp
, stateid
, typemask
);
4455 return nfserr_bad_stateid
;
4460 * Checks for stateid operations
4463 nfs4_preprocess_stateid_op(struct net
*net
, struct nfsd4_compound_state
*cstate
,
4464 stateid_t
*stateid
, int flags
, struct file
**filpp
)
4466 struct nfs4_stid
*s
;
4467 struct nfs4_ol_stateid
*stp
= NULL
;
4468 struct nfs4_delegation
*dp
= NULL
;
4469 struct svc_fh
*current_fh
= &cstate
->current_fh
;
4470 struct inode
*ino
= current_fh
->fh_dentry
->d_inode
;
4471 struct nfsd_net
*nn
= net_generic(net
, nfsd_net_id
);
4472 struct file
*file
= NULL
;
4478 if (grace_disallows_io(net
, ino
))
4479 return nfserr_grace
;
4481 if (ZERO_STATEID(stateid
) || ONE_STATEID(stateid
))
4482 return check_special_stateids(net
, current_fh
, stateid
, flags
);
4484 status
= nfsd4_lookup_stateid(cstate
, stateid
,
4485 NFS4_DELEG_STID
|NFS4_OPEN_STID
|NFS4_LOCK_STID
,
4489 status
= check_stateid_generation(stateid
, &s
->sc_stateid
, nfsd4_has_session(cstate
));
4492 switch (s
->sc_type
) {
4493 case NFS4_DELEG_STID
:
4494 dp
= delegstateid(s
);
4495 status
= nfs4_check_delegmode(dp
, flags
);
4499 file
= dp
->dl_stid
.sc_file
->fi_deleg_file
;
4502 status
= nfserr_serverfault
;
4508 case NFS4_OPEN_STID
:
4509 case NFS4_LOCK_STID
:
4510 stp
= openlockstateid(s
);
4511 status
= nfs4_check_fh(current_fh
, stp
);
4514 if (stp
->st_stateowner
->so_is_open_owner
4515 && !(openowner(stp
->st_stateowner
)->oo_flags
& NFS4_OO_CONFIRMED
))
4517 status
= nfs4_check_openmode(stp
, flags
);
4521 struct nfs4_file
*fp
= stp
->st_stid
.sc_file
;
4523 if (flags
& RD_STATE
)
4524 file
= find_readable_file(fp
);
4526 file
= find_writeable_file(fp
);
4530 status
= nfserr_bad_stateid
;
4542 * Test if the stateid is valid
4545 nfsd4_test_stateid(struct svc_rqst
*rqstp
, struct nfsd4_compound_state
*cstate
,
4546 struct nfsd4_test_stateid
*test_stateid
)
4548 struct nfsd4_test_stateid_id
*stateid
;
4549 struct nfs4_client
*cl
= cstate
->session
->se_client
;
4551 list_for_each_entry(stateid
, &test_stateid
->ts_stateid_list
, ts_id_list
)
4552 stateid
->ts_id_status
=
4553 nfsd4_validate_stateid(cl
, &stateid
->ts_id_stateid
);
4559 nfsd4_free_stateid(struct svc_rqst
*rqstp
, struct nfsd4_compound_state
*cstate
,
4560 struct nfsd4_free_stateid
*free_stateid
)
4562 stateid_t
*stateid
= &free_stateid
->fr_stateid
;
4563 struct nfs4_stid
*s
;
4564 struct nfs4_delegation
*dp
;
4565 struct nfs4_ol_stateid
*stp
;
4566 struct nfs4_client
*cl
= cstate
->session
->se_client
;
4567 __be32 ret
= nfserr_bad_stateid
;
4569 spin_lock(&cl
->cl_lock
);
4570 s
= find_stateid_locked(cl
, stateid
);
4573 switch (s
->sc_type
) {
4574 case NFS4_DELEG_STID
:
4575 ret
= nfserr_locks_held
;
4577 case NFS4_OPEN_STID
:
4578 ret
= check_stateid_generation(stateid
, &s
->sc_stateid
, 1);
4581 ret
= nfserr_locks_held
;
4583 case NFS4_LOCK_STID
:
4584 ret
= check_stateid_generation(stateid
, &s
->sc_stateid
, 1);
4587 stp
= openlockstateid(s
);
4588 ret
= nfserr_locks_held
;
4589 if (check_for_locks(stp
->st_stid
.sc_file
,
4590 lockowner(stp
->st_stateowner
)))
4592 unhash_lock_stateid(stp
);
4593 spin_unlock(&cl
->cl_lock
);
4597 case NFS4_REVOKED_DELEG_STID
:
4598 dp
= delegstateid(s
);
4599 list_del_init(&dp
->dl_recall_lru
);
4600 spin_unlock(&cl
->cl_lock
);
4604 /* Default falls through and returns nfserr_bad_stateid */
4607 spin_unlock(&cl
->cl_lock
);
4615 return (type
== NFS4_READW_LT
|| type
== NFS4_READ_LT
) ?
4616 RD_STATE
: WR_STATE
;
4619 static __be32
nfs4_seqid_op_checks(struct nfsd4_compound_state
*cstate
, stateid_t
*stateid
, u32 seqid
, struct nfs4_ol_stateid
*stp
)
4621 struct svc_fh
*current_fh
= &cstate
->current_fh
;
4622 struct nfs4_stateowner
*sop
= stp
->st_stateowner
;
4625 status
= nfsd4_check_seqid(cstate
, sop
, seqid
);
4628 if (stp
->st_stid
.sc_type
== NFS4_CLOSED_STID
4629 || stp
->st_stid
.sc_type
== NFS4_REVOKED_DELEG_STID
)
4631 * "Closed" stateid's exist *only* to return
4632 * nfserr_replay_me from the previous step, and
4633 * revoked delegations are kept only for free_stateid.
4635 return nfserr_bad_stateid
;
4636 status
= check_stateid_generation(stateid
, &stp
->st_stid
.sc_stateid
, nfsd4_has_session(cstate
));
4639 return nfs4_check_fh(current_fh
, stp
);
4643 * Checks for sequence id mutating operations.
4646 nfs4_preprocess_seqid_op(struct nfsd4_compound_state
*cstate
, u32 seqid
,
4647 stateid_t
*stateid
, char typemask
,
4648 struct nfs4_ol_stateid
**stpp
,
4649 struct nfsd_net
*nn
)
4652 struct nfs4_stid
*s
;
4653 struct nfs4_ol_stateid
*stp
= NULL
;
4655 dprintk("NFSD: %s: seqid=%d stateid = " STATEID_FMT
"\n", __func__
,
4656 seqid
, STATEID_VAL(stateid
));
4659 status
= nfsd4_lookup_stateid(cstate
, stateid
, typemask
, &s
, nn
);
4662 stp
= openlockstateid(s
);
4663 nfsd4_cstate_assign_replay(cstate
, stp
->st_stateowner
);
4665 status
= nfs4_seqid_op_checks(cstate
, stateid
, seqid
, stp
);
4669 nfs4_put_stid(&stp
->st_stid
);
4673 static __be32
nfs4_preprocess_confirmed_seqid_op(struct nfsd4_compound_state
*cstate
, u32 seqid
,
4674 stateid_t
*stateid
, struct nfs4_ol_stateid
**stpp
, struct nfsd_net
*nn
)
4677 struct nfs4_openowner
*oo
;
4678 struct nfs4_ol_stateid
*stp
;
4680 status
= nfs4_preprocess_seqid_op(cstate
, seqid
, stateid
,
4681 NFS4_OPEN_STID
, &stp
, nn
);
4684 oo
= openowner(stp
->st_stateowner
);
4685 if (!(oo
->oo_flags
& NFS4_OO_CONFIRMED
)) {
4686 nfs4_put_stid(&stp
->st_stid
);
4687 return nfserr_bad_stateid
;
4694 nfsd4_open_confirm(struct svc_rqst
*rqstp
, struct nfsd4_compound_state
*cstate
,
4695 struct nfsd4_open_confirm
*oc
)
4698 struct nfs4_openowner
*oo
;
4699 struct nfs4_ol_stateid
*stp
;
4700 struct nfsd_net
*nn
= net_generic(SVC_NET(rqstp
), nfsd_net_id
);
4702 dprintk("NFSD: nfsd4_open_confirm on file %pd\n",
4703 cstate
->current_fh
.fh_dentry
);
4705 status
= fh_verify(rqstp
, &cstate
->current_fh
, S_IFREG
, 0);
4709 status
= nfs4_preprocess_seqid_op(cstate
,
4710 oc
->oc_seqid
, &oc
->oc_req_stateid
,
4711 NFS4_OPEN_STID
, &stp
, nn
);
4714 oo
= openowner(stp
->st_stateowner
);
4715 status
= nfserr_bad_stateid
;
4716 if (oo
->oo_flags
& NFS4_OO_CONFIRMED
)
4718 oo
->oo_flags
|= NFS4_OO_CONFIRMED
;
4719 update_stateid(&stp
->st_stid
.sc_stateid
);
4720 memcpy(&oc
->oc_resp_stateid
, &stp
->st_stid
.sc_stateid
, sizeof(stateid_t
));
4721 dprintk("NFSD: %s: success, seqid=%d stateid=" STATEID_FMT
"\n",
4722 __func__
, oc
->oc_seqid
, STATEID_VAL(&stp
->st_stid
.sc_stateid
));
4724 nfsd4_client_record_create(oo
->oo_owner
.so_client
);
4727 nfs4_put_stid(&stp
->st_stid
);
4729 nfsd4_bump_seqid(cstate
, status
);
4733 static inline void nfs4_stateid_downgrade_bit(struct nfs4_ol_stateid
*stp
, u32 access
)
4735 if (!test_access(access
, stp
))
4737 nfs4_file_put_access(stp
->st_stid
.sc_file
, access
);
4738 clear_access(access
, stp
);
4741 static inline void nfs4_stateid_downgrade(struct nfs4_ol_stateid
*stp
, u32 to_access
)
4743 switch (to_access
) {
4744 case NFS4_SHARE_ACCESS_READ
:
4745 nfs4_stateid_downgrade_bit(stp
, NFS4_SHARE_ACCESS_WRITE
);
4746 nfs4_stateid_downgrade_bit(stp
, NFS4_SHARE_ACCESS_BOTH
);
4748 case NFS4_SHARE_ACCESS_WRITE
:
4749 nfs4_stateid_downgrade_bit(stp
, NFS4_SHARE_ACCESS_READ
);
4750 nfs4_stateid_downgrade_bit(stp
, NFS4_SHARE_ACCESS_BOTH
);
4752 case NFS4_SHARE_ACCESS_BOTH
:
4760 nfsd4_open_downgrade(struct svc_rqst
*rqstp
,
4761 struct nfsd4_compound_state
*cstate
,
4762 struct nfsd4_open_downgrade
*od
)
4765 struct nfs4_ol_stateid
*stp
;
4766 struct nfsd_net
*nn
= net_generic(SVC_NET(rqstp
), nfsd_net_id
);
4768 dprintk("NFSD: nfsd4_open_downgrade on file %pd\n",
4769 cstate
->current_fh
.fh_dentry
);
4771 /* We don't yet support WANT bits: */
4772 if (od
->od_deleg_want
)
4773 dprintk("NFSD: %s: od_deleg_want=0x%x ignored\n", __func__
,
4776 status
= nfs4_preprocess_confirmed_seqid_op(cstate
, od
->od_seqid
,
4777 &od
->od_stateid
, &stp
, nn
);
4780 status
= nfserr_inval
;
4781 if (!test_access(od
->od_share_access
, stp
)) {
4782 dprintk("NFSD: access not a subset of current bitmap: 0x%hhx, input access=%08x\n",
4783 stp
->st_access_bmap
, od
->od_share_access
);
4786 if (!test_deny(od
->od_share_deny
, stp
)) {
4787 dprintk("NFSD: deny not a subset of current bitmap: 0x%hhx, input deny=%08x\n",
4788 stp
->st_deny_bmap
, od
->od_share_deny
);
4791 nfs4_stateid_downgrade(stp
, od
->od_share_access
);
4793 reset_union_bmap_deny(od
->od_share_deny
, stp
);
4795 update_stateid(&stp
->st_stid
.sc_stateid
);
4796 memcpy(&od
->od_stateid
, &stp
->st_stid
.sc_stateid
, sizeof(stateid_t
));
4799 nfs4_put_stid(&stp
->st_stid
);
4801 nfsd4_bump_seqid(cstate
, status
);
4805 static void nfsd4_close_open_stateid(struct nfs4_ol_stateid
*s
)
4807 struct nfs4_client
*clp
= s
->st_stid
.sc_client
;
4808 LIST_HEAD(reaplist
);
4810 s
->st_stid
.sc_type
= NFS4_CLOSED_STID
;
4811 spin_lock(&clp
->cl_lock
);
4812 unhash_open_stateid(s
, &reaplist
);
4814 if (clp
->cl_minorversion
) {
4815 put_ol_stateid_locked(s
, &reaplist
);
4816 spin_unlock(&clp
->cl_lock
);
4817 free_ol_stateid_reaplist(&reaplist
);
4819 spin_unlock(&clp
->cl_lock
);
4820 free_ol_stateid_reaplist(&reaplist
);
4821 move_to_close_lru(s
, clp
->net
);
4826 * nfs4_unlock_state() called after encode
4829 nfsd4_close(struct svc_rqst
*rqstp
, struct nfsd4_compound_state
*cstate
,
4830 struct nfsd4_close
*close
)
4833 struct nfs4_ol_stateid
*stp
;
4834 struct net
*net
= SVC_NET(rqstp
);
4835 struct nfsd_net
*nn
= net_generic(net
, nfsd_net_id
);
4837 dprintk("NFSD: nfsd4_close on file %pd\n",
4838 cstate
->current_fh
.fh_dentry
);
4840 status
= nfs4_preprocess_seqid_op(cstate
, close
->cl_seqid
,
4842 NFS4_OPEN_STID
|NFS4_CLOSED_STID
,
4844 nfsd4_bump_seqid(cstate
, status
);
4847 update_stateid(&stp
->st_stid
.sc_stateid
);
4848 memcpy(&close
->cl_stateid
, &stp
->st_stid
.sc_stateid
, sizeof(stateid_t
));
4850 nfsd4_close_open_stateid(stp
);
4852 /* put reference from nfs4_preprocess_seqid_op */
4853 nfs4_put_stid(&stp
->st_stid
);
4859 nfsd4_delegreturn(struct svc_rqst
*rqstp
, struct nfsd4_compound_state
*cstate
,
4860 struct nfsd4_delegreturn
*dr
)
4862 struct nfs4_delegation
*dp
;
4863 stateid_t
*stateid
= &dr
->dr_stateid
;
4864 struct nfs4_stid
*s
;
4866 struct nfsd_net
*nn
= net_generic(SVC_NET(rqstp
), nfsd_net_id
);
4868 if ((status
= fh_verify(rqstp
, &cstate
->current_fh
, S_IFREG
, 0)))
4871 status
= nfsd4_lookup_stateid(cstate
, stateid
, NFS4_DELEG_STID
, &s
, nn
);
4874 dp
= delegstateid(s
);
4875 status
= check_stateid_generation(stateid
, &dp
->dl_stid
.sc_stateid
, nfsd4_has_session(cstate
));
4879 destroy_delegation(dp
);
4881 nfs4_put_stid(&dp
->dl_stid
);
4887 #define LOFF_OVERFLOW(start, len) ((u64)(len) > ~(u64)(start))
4890 end_offset(u64 start
, u64 len
)
4895 return end
>= start
? end
: NFS4_MAX_UINT64
;
4898 /* last octet in a range */
4900 last_byte_offset(u64 start
, u64 len
)
4906 return end
> start
? end
- 1: NFS4_MAX_UINT64
;
4910 * TODO: Linux file offsets are _signed_ 64-bit quantities, which means that
4911 * we can't properly handle lock requests that go beyond the (2^63 - 1)-th
4912 * byte, because of sign extension problems. Since NFSv4 calls for 64-bit
4913 * locking, this prevents us from being completely protocol-compliant. The
4914 * real solution to this problem is to start using unsigned file offsets in
4915 * the VFS, but this is a very deep change!
4918 nfs4_transform_lock_offset(struct file_lock
*lock
)
4920 if (lock
->fl_start
< 0)
4921 lock
->fl_start
= OFFSET_MAX
;
4922 if (lock
->fl_end
< 0)
4923 lock
->fl_end
= OFFSET_MAX
;
4926 static void nfsd4_fl_get_owner(struct file_lock
*dst
, struct file_lock
*src
)
4928 struct nfs4_lockowner
*lo
= (struct nfs4_lockowner
*)src
->fl_owner
;
4929 dst
->fl_owner
= (fl_owner_t
)lockowner(nfs4_get_stateowner(&lo
->lo_owner
));
4932 static void nfsd4_fl_put_owner(struct file_lock
*fl
)
4934 struct nfs4_lockowner
*lo
= (struct nfs4_lockowner
*)fl
->fl_owner
;
4937 nfs4_put_stateowner(&lo
->lo_owner
);
4938 fl
->fl_owner
= NULL
;
4942 static const struct lock_manager_operations nfsd_posix_mng_ops
= {
4943 .lm_get_owner
= nfsd4_fl_get_owner
,
4944 .lm_put_owner
= nfsd4_fl_put_owner
,
4948 nfs4_set_lock_denied(struct file_lock
*fl
, struct nfsd4_lock_denied
*deny
)
4950 struct nfs4_lockowner
*lo
;
4952 if (fl
->fl_lmops
== &nfsd_posix_mng_ops
) {
4953 lo
= (struct nfs4_lockowner
*) fl
->fl_owner
;
4954 deny
->ld_owner
.data
= kmemdup(lo
->lo_owner
.so_owner
.data
,
4955 lo
->lo_owner
.so_owner
.len
, GFP_KERNEL
);
4956 if (!deny
->ld_owner
.data
)
4957 /* We just don't care that much */
4959 deny
->ld_owner
.len
= lo
->lo_owner
.so_owner
.len
;
4960 deny
->ld_clientid
= lo
->lo_owner
.so_client
->cl_clientid
;
4963 deny
->ld_owner
.len
= 0;
4964 deny
->ld_owner
.data
= NULL
;
4965 deny
->ld_clientid
.cl_boot
= 0;
4966 deny
->ld_clientid
.cl_id
= 0;
4968 deny
->ld_start
= fl
->fl_start
;
4969 deny
->ld_length
= NFS4_MAX_UINT64
;
4970 if (fl
->fl_end
!= NFS4_MAX_UINT64
)
4971 deny
->ld_length
= fl
->fl_end
- fl
->fl_start
+ 1;
4972 deny
->ld_type
= NFS4_READ_LT
;
4973 if (fl
->fl_type
!= F_RDLCK
)
4974 deny
->ld_type
= NFS4_WRITE_LT
;
4977 static struct nfs4_lockowner
*
4978 find_lockowner_str_locked(clientid_t
*clid
, struct xdr_netobj
*owner
,
4979 struct nfs4_client
*clp
)
4981 unsigned int strhashval
= ownerstr_hashval(owner
);
4982 struct nfs4_stateowner
*so
;
4984 lockdep_assert_held(&clp
->cl_lock
);
4986 list_for_each_entry(so
, &clp
->cl_ownerstr_hashtbl
[strhashval
],
4988 if (so
->so_is_open_owner
)
4990 if (same_owner_str(so
, owner
))
4991 return lockowner(nfs4_get_stateowner(so
));
4996 static struct nfs4_lockowner
*
4997 find_lockowner_str(clientid_t
*clid
, struct xdr_netobj
*owner
,
4998 struct nfs4_client
*clp
)
5000 struct nfs4_lockowner
*lo
;
5002 spin_lock(&clp
->cl_lock
);
5003 lo
= find_lockowner_str_locked(clid
, owner
, clp
);
5004 spin_unlock(&clp
->cl_lock
);
5008 static void nfs4_unhash_lockowner(struct nfs4_stateowner
*sop
)
5010 unhash_lockowner_locked(lockowner(sop
));
5013 static void nfs4_free_lockowner(struct nfs4_stateowner
*sop
)
5015 struct nfs4_lockowner
*lo
= lockowner(sop
);
5017 kmem_cache_free(lockowner_slab
, lo
);
5020 static const struct nfs4_stateowner_operations lockowner_ops
= {
5021 .so_unhash
= nfs4_unhash_lockowner
,
5022 .so_free
= nfs4_free_lockowner
,
5026 * Alloc a lock owner structure.
5027 * Called in nfsd4_lock - therefore, OPEN and OPEN_CONFIRM (if needed) has
5030 * strhashval = ownerstr_hashval
5032 static struct nfs4_lockowner
*
5033 alloc_init_lock_stateowner(unsigned int strhashval
, struct nfs4_client
*clp
,
5034 struct nfs4_ol_stateid
*open_stp
,
5035 struct nfsd4_lock
*lock
)
5037 struct nfs4_lockowner
*lo
, *ret
;
5039 lo
= alloc_stateowner(lockowner_slab
, &lock
->lk_new_owner
, clp
);
5042 INIT_LIST_HEAD(&lo
->lo_owner
.so_stateids
);
5043 lo
->lo_owner
.so_is_open_owner
= 0;
5044 lo
->lo_owner
.so_seqid
= lock
->lk_new_lock_seqid
;
5045 lo
->lo_owner
.so_ops
= &lockowner_ops
;
5046 spin_lock(&clp
->cl_lock
);
5047 ret
= find_lockowner_str_locked(&clp
->cl_clientid
,
5048 &lock
->lk_new_owner
, clp
);
5050 list_add(&lo
->lo_owner
.so_strhash
,
5051 &clp
->cl_ownerstr_hashtbl
[strhashval
]);
5054 nfs4_free_lockowner(&lo
->lo_owner
);
5055 spin_unlock(&clp
->cl_lock
);
5060 init_lock_stateid(struct nfs4_ol_stateid
*stp
, struct nfs4_lockowner
*lo
,
5061 struct nfs4_file
*fp
, struct inode
*inode
,
5062 struct nfs4_ol_stateid
*open_stp
)
5064 struct nfs4_client
*clp
= lo
->lo_owner
.so_client
;
5066 lockdep_assert_held(&clp
->cl_lock
);
5068 atomic_inc(&stp
->st_stid
.sc_count
);
5069 stp
->st_stid
.sc_type
= NFS4_LOCK_STID
;
5070 stp
->st_stateowner
= nfs4_get_stateowner(&lo
->lo_owner
);
5072 stp
->st_stid
.sc_file
= fp
;
5073 stp
->st_stid
.sc_free
= nfs4_free_lock_stateid
;
5074 stp
->st_access_bmap
= 0;
5075 stp
->st_deny_bmap
= open_stp
->st_deny_bmap
;
5076 stp
->st_openstp
= open_stp
;
5077 list_add(&stp
->st_locks
, &open_stp
->st_locks
);
5078 list_add(&stp
->st_perstateowner
, &lo
->lo_owner
.so_stateids
);
5079 spin_lock(&fp
->fi_lock
);
5080 list_add(&stp
->st_perfile
, &fp
->fi_stateids
);
5081 spin_unlock(&fp
->fi_lock
);
5084 static struct nfs4_ol_stateid
*
5085 find_lock_stateid(struct nfs4_lockowner
*lo
, struct nfs4_file
*fp
)
5087 struct nfs4_ol_stateid
*lst
;
5088 struct nfs4_client
*clp
= lo
->lo_owner
.so_client
;
5090 lockdep_assert_held(&clp
->cl_lock
);
5092 list_for_each_entry(lst
, &lo
->lo_owner
.so_stateids
, st_perstateowner
) {
5093 if (lst
->st_stid
.sc_file
== fp
) {
5094 atomic_inc(&lst
->st_stid
.sc_count
);
5101 static struct nfs4_ol_stateid
*
5102 find_or_create_lock_stateid(struct nfs4_lockowner
*lo
, struct nfs4_file
*fi
,
5103 struct inode
*inode
, struct nfs4_ol_stateid
*ost
,
5106 struct nfs4_stid
*ns
= NULL
;
5107 struct nfs4_ol_stateid
*lst
;
5108 struct nfs4_openowner
*oo
= openowner(ost
->st_stateowner
);
5109 struct nfs4_client
*clp
= oo
->oo_owner
.so_client
;
5111 spin_lock(&clp
->cl_lock
);
5112 lst
= find_lock_stateid(lo
, fi
);
5114 spin_unlock(&clp
->cl_lock
);
5115 ns
= nfs4_alloc_stid(clp
, stateid_slab
);
5119 spin_lock(&clp
->cl_lock
);
5120 lst
= find_lock_stateid(lo
, fi
);
5122 lst
= openlockstateid(ns
);
5123 init_lock_stateid(lst
, lo
, fi
, inode
, ost
);
5128 spin_unlock(&clp
->cl_lock
);
5135 check_lock_length(u64 offset
, u64 length
)
5137 return ((length
== 0) || ((length
!= NFS4_MAX_UINT64
) &&
5138 LOFF_OVERFLOW(offset
, length
)));
5141 static void get_lock_access(struct nfs4_ol_stateid
*lock_stp
, u32 access
)
5143 struct nfs4_file
*fp
= lock_stp
->st_stid
.sc_file
;
5145 lockdep_assert_held(&fp
->fi_lock
);
5147 if (test_access(access
, lock_stp
))
5149 __nfs4_file_get_access(fp
, access
);
5150 set_access(access
, lock_stp
);
5154 lookup_or_create_lock_state(struct nfsd4_compound_state
*cstate
,
5155 struct nfs4_ol_stateid
*ost
,
5156 struct nfsd4_lock
*lock
,
5157 struct nfs4_ol_stateid
**lst
, bool *new)
5160 struct nfs4_file
*fi
= ost
->st_stid
.sc_file
;
5161 struct nfs4_openowner
*oo
= openowner(ost
->st_stateowner
);
5162 struct nfs4_client
*cl
= oo
->oo_owner
.so_client
;
5163 struct inode
*inode
= cstate
->current_fh
.fh_dentry
->d_inode
;
5164 struct nfs4_lockowner
*lo
;
5165 unsigned int strhashval
;
5167 lo
= find_lockowner_str(&cl
->cl_clientid
, &lock
->v
.new.owner
, cl
);
5169 strhashval
= ownerstr_hashval(&lock
->v
.new.owner
);
5170 lo
= alloc_init_lock_stateowner(strhashval
, cl
, ost
, lock
);
5172 return nfserr_jukebox
;
5174 /* with an existing lockowner, seqids must be the same */
5175 status
= nfserr_bad_seqid
;
5176 if (!cstate
->minorversion
&&
5177 lock
->lk_new_lock_seqid
!= lo
->lo_owner
.so_seqid
)
5181 *lst
= find_or_create_lock_stateid(lo
, fi
, inode
, ost
, new);
5183 status
= nfserr_jukebox
;
5188 nfs4_put_stateowner(&lo
->lo_owner
);
5196 nfsd4_lock(struct svc_rqst
*rqstp
, struct nfsd4_compound_state
*cstate
,
5197 struct nfsd4_lock
*lock
)
5199 struct nfs4_openowner
*open_sop
= NULL
;
5200 struct nfs4_lockowner
*lock_sop
= NULL
;
5201 struct nfs4_ol_stateid
*lock_stp
= NULL
;
5202 struct nfs4_ol_stateid
*open_stp
= NULL
;
5203 struct nfs4_file
*fp
;
5204 struct file
*filp
= NULL
;
5205 struct file_lock
*file_lock
= NULL
;
5206 struct file_lock
*conflock
= NULL
;
5211 struct net
*net
= SVC_NET(rqstp
);
5212 struct nfsd_net
*nn
= net_generic(net
, nfsd_net_id
);
5214 dprintk("NFSD: nfsd4_lock: start=%Ld length=%Ld\n",
5215 (long long) lock
->lk_offset
,
5216 (long long) lock
->lk_length
);
5218 if (check_lock_length(lock
->lk_offset
, lock
->lk_length
))
5219 return nfserr_inval
;
5221 if ((status
= fh_verify(rqstp
, &cstate
->current_fh
,
5222 S_IFREG
, NFSD_MAY_LOCK
))) {
5223 dprintk("NFSD: nfsd4_lock: permission denied!\n");
5227 if (lock
->lk_is_new
) {
5228 if (nfsd4_has_session(cstate
))
5229 /* See rfc 5661 18.10.3: given clientid is ignored: */
5230 memcpy(&lock
->v
.new.clientid
,
5231 &cstate
->session
->se_client
->cl_clientid
,
5232 sizeof(clientid_t
));
5234 status
= nfserr_stale_clientid
;
5235 if (STALE_CLIENTID(&lock
->lk_new_clientid
, nn
))
5238 /* validate and update open stateid and open seqid */
5239 status
= nfs4_preprocess_confirmed_seqid_op(cstate
,
5240 lock
->lk_new_open_seqid
,
5241 &lock
->lk_new_open_stateid
,
5245 open_sop
= openowner(open_stp
->st_stateowner
);
5246 status
= nfserr_bad_stateid
;
5247 if (!same_clid(&open_sop
->oo_owner
.so_client
->cl_clientid
,
5248 &lock
->v
.new.clientid
))
5250 status
= lookup_or_create_lock_state(cstate
, open_stp
, lock
,
5253 status
= nfs4_preprocess_seqid_op(cstate
,
5254 lock
->lk_old_lock_seqid
,
5255 &lock
->lk_old_lock_stateid
,
5256 NFS4_LOCK_STID
, &lock_stp
, nn
);
5260 lock_sop
= lockowner(lock_stp
->st_stateowner
);
5262 lkflg
= setlkflg(lock
->lk_type
);
5263 status
= nfs4_check_openmode(lock_stp
, lkflg
);
5267 status
= nfserr_grace
;
5268 if (locks_in_grace(net
) && !lock
->lk_reclaim
)
5270 status
= nfserr_no_grace
;
5271 if (!locks_in_grace(net
) && lock
->lk_reclaim
)
5274 file_lock
= locks_alloc_lock();
5276 dprintk("NFSD: %s: unable to allocate lock!\n", __func__
);
5277 status
= nfserr_jukebox
;
5281 fp
= lock_stp
->st_stid
.sc_file
;
5282 switch (lock
->lk_type
) {
5285 spin_lock(&fp
->fi_lock
);
5286 filp
= find_readable_file_locked(fp
);
5288 get_lock_access(lock_stp
, NFS4_SHARE_ACCESS_READ
);
5289 spin_unlock(&fp
->fi_lock
);
5290 file_lock
->fl_type
= F_RDLCK
;
5293 case NFS4_WRITEW_LT
:
5294 spin_lock(&fp
->fi_lock
);
5295 filp
= find_writeable_file_locked(fp
);
5297 get_lock_access(lock_stp
, NFS4_SHARE_ACCESS_WRITE
);
5298 spin_unlock(&fp
->fi_lock
);
5299 file_lock
->fl_type
= F_WRLCK
;
5302 status
= nfserr_inval
;
5306 status
= nfserr_openmode
;
5310 file_lock
->fl_owner
= (fl_owner_t
)lockowner(nfs4_get_stateowner(&lock_sop
->lo_owner
));
5311 file_lock
->fl_pid
= current
->tgid
;
5312 file_lock
->fl_file
= filp
;
5313 file_lock
->fl_flags
= FL_POSIX
;
5314 file_lock
->fl_lmops
= &nfsd_posix_mng_ops
;
5315 file_lock
->fl_start
= lock
->lk_offset
;
5316 file_lock
->fl_end
= last_byte_offset(lock
->lk_offset
, lock
->lk_length
);
5317 nfs4_transform_lock_offset(file_lock
);
5319 conflock
= locks_alloc_lock();
5321 dprintk("NFSD: %s: unable to allocate lock!\n", __func__
);
5322 status
= nfserr_jukebox
;
5326 err
= vfs_lock_file(filp
, F_SETLK
, file_lock
, conflock
);
5328 case 0: /* success! */
5329 update_stateid(&lock_stp
->st_stid
.sc_stateid
);
5330 memcpy(&lock
->lk_resp_stateid
, &lock_stp
->st_stid
.sc_stateid
,
5334 case (EAGAIN
): /* conflock holds conflicting lock */
5335 status
= nfserr_denied
;
5336 dprintk("NFSD: nfsd4_lock: conflicting lock found!\n");
5337 nfs4_set_lock_denied(conflock
, &lock
->lk_denied
);
5340 status
= nfserr_deadlock
;
5343 dprintk("NFSD: nfsd4_lock: vfs_lock_file() failed! status %d\n",err
);
5344 status
= nfserrno(err
);
5351 /* Bump seqid manually if the 4.0 replay owner is openowner */
5352 if (cstate
->replay_owner
&&
5353 cstate
->replay_owner
!= &lock_sop
->lo_owner
&&
5354 seqid_mutating_err(ntohl(status
)))
5355 lock_sop
->lo_owner
.so_seqid
++;
5358 * If this is a new, never-before-used stateid, and we are
5359 * returning an error, then just go ahead and release it.
5362 release_lock_stateid(lock_stp
);
5364 nfs4_put_stid(&lock_stp
->st_stid
);
5367 nfs4_put_stid(&open_stp
->st_stid
);
5368 nfsd4_bump_seqid(cstate
, status
);
5370 locks_free_lock(file_lock
);
5372 locks_free_lock(conflock
);
5377 * The NFSv4 spec allows a client to do a LOCKT without holding an OPEN,
5378 * so we do a temporary open here just to get an open file to pass to
5379 * vfs_test_lock. (Arguably perhaps test_lock should be done with an
5382 static __be32
nfsd_test_lock(struct svc_rqst
*rqstp
, struct svc_fh
*fhp
, struct file_lock
*lock
)
5385 __be32 err
= nfsd_open(rqstp
, fhp
, S_IFREG
, NFSD_MAY_READ
, &file
);
5387 err
= nfserrno(vfs_test_lock(file
, lock
));
5397 nfsd4_lockt(struct svc_rqst
*rqstp
, struct nfsd4_compound_state
*cstate
,
5398 struct nfsd4_lockt
*lockt
)
5400 struct file_lock
*file_lock
= NULL
;
5401 struct nfs4_lockowner
*lo
= NULL
;
5403 struct nfsd_net
*nn
= net_generic(SVC_NET(rqstp
), nfsd_net_id
);
5405 if (locks_in_grace(SVC_NET(rqstp
)))
5406 return nfserr_grace
;
5408 if (check_lock_length(lockt
->lt_offset
, lockt
->lt_length
))
5409 return nfserr_inval
;
5411 if (!nfsd4_has_session(cstate
)) {
5412 status
= lookup_clientid(&lockt
->lt_clientid
, cstate
, nn
);
5417 if ((status
= fh_verify(rqstp
, &cstate
->current_fh
, S_IFREG
, 0)))
5420 file_lock
= locks_alloc_lock();
5422 dprintk("NFSD: %s: unable to allocate lock!\n", __func__
);
5423 status
= nfserr_jukebox
;
5427 switch (lockt
->lt_type
) {
5430 file_lock
->fl_type
= F_RDLCK
;
5433 case NFS4_WRITEW_LT
:
5434 file_lock
->fl_type
= F_WRLCK
;
5437 dprintk("NFSD: nfs4_lockt: bad lock type!\n");
5438 status
= nfserr_inval
;
5442 lo
= find_lockowner_str(&lockt
->lt_clientid
, &lockt
->lt_owner
,
5445 file_lock
->fl_owner
= (fl_owner_t
)lo
;
5446 file_lock
->fl_pid
= current
->tgid
;
5447 file_lock
->fl_flags
= FL_POSIX
;
5449 file_lock
->fl_start
= lockt
->lt_offset
;
5450 file_lock
->fl_end
= last_byte_offset(lockt
->lt_offset
, lockt
->lt_length
);
5452 nfs4_transform_lock_offset(file_lock
);
5454 status
= nfsd_test_lock(rqstp
, &cstate
->current_fh
, file_lock
);
5458 if (file_lock
->fl_type
!= F_UNLCK
) {
5459 status
= nfserr_denied
;
5460 nfs4_set_lock_denied(file_lock
, &lockt
->lt_denied
);
5464 nfs4_put_stateowner(&lo
->lo_owner
);
5466 locks_free_lock(file_lock
);
5471 nfsd4_locku(struct svc_rqst
*rqstp
, struct nfsd4_compound_state
*cstate
,
5472 struct nfsd4_locku
*locku
)
5474 struct nfs4_ol_stateid
*stp
;
5475 struct file
*filp
= NULL
;
5476 struct file_lock
*file_lock
= NULL
;
5479 struct nfsd_net
*nn
= net_generic(SVC_NET(rqstp
), nfsd_net_id
);
5481 dprintk("NFSD: nfsd4_locku: start=%Ld length=%Ld\n",
5482 (long long) locku
->lu_offset
,
5483 (long long) locku
->lu_length
);
5485 if (check_lock_length(locku
->lu_offset
, locku
->lu_length
))
5486 return nfserr_inval
;
5488 status
= nfs4_preprocess_seqid_op(cstate
, locku
->lu_seqid
,
5489 &locku
->lu_stateid
, NFS4_LOCK_STID
,
5493 filp
= find_any_file(stp
->st_stid
.sc_file
);
5495 status
= nfserr_lock_range
;
5498 file_lock
= locks_alloc_lock();
5500 dprintk("NFSD: %s: unable to allocate lock!\n", __func__
);
5501 status
= nfserr_jukebox
;
5505 file_lock
->fl_type
= F_UNLCK
;
5506 file_lock
->fl_owner
= (fl_owner_t
)lockowner(nfs4_get_stateowner(stp
->st_stateowner
));
5507 file_lock
->fl_pid
= current
->tgid
;
5508 file_lock
->fl_file
= filp
;
5509 file_lock
->fl_flags
= FL_POSIX
;
5510 file_lock
->fl_lmops
= &nfsd_posix_mng_ops
;
5511 file_lock
->fl_start
= locku
->lu_offset
;
5513 file_lock
->fl_end
= last_byte_offset(locku
->lu_offset
,
5515 nfs4_transform_lock_offset(file_lock
);
5517 err
= vfs_lock_file(filp
, F_SETLK
, file_lock
, NULL
);
5519 dprintk("NFSD: nfs4_locku: vfs_lock_file failed!\n");
5522 update_stateid(&stp
->st_stid
.sc_stateid
);
5523 memcpy(&locku
->lu_stateid
, &stp
->st_stid
.sc_stateid
, sizeof(stateid_t
));
5527 nfs4_put_stid(&stp
->st_stid
);
5529 nfsd4_bump_seqid(cstate
, status
);
5531 locks_free_lock(file_lock
);
5535 status
= nfserrno(err
);
5541 * true: locks held by lockowner
5542 * false: no locks held by lockowner
5545 check_for_locks(struct nfs4_file
*fp
, struct nfs4_lockowner
*lowner
)
5547 struct file_lock
*fl
;
5549 struct file
*filp
= find_any_file(fp
);
5550 struct inode
*inode
;
5551 struct file_lock_context
*flctx
;
5554 /* Any valid lock stateid should have some sort of access */
5559 inode
= file_inode(filp
);
5560 flctx
= inode
->i_flctx
;
5562 if (flctx
&& !list_empty_careful(&flctx
->flc_posix
)) {
5563 spin_lock(&flctx
->flc_lock
);
5564 list_for_each_entry(fl
, &flctx
->flc_posix
, fl_list
) {
5565 if (fl
->fl_owner
== (fl_owner_t
)lowner
) {
5570 spin_unlock(&flctx
->flc_lock
);
5577 nfsd4_release_lockowner(struct svc_rqst
*rqstp
,
5578 struct nfsd4_compound_state
*cstate
,
5579 struct nfsd4_release_lockowner
*rlockowner
)
5581 clientid_t
*clid
= &rlockowner
->rl_clientid
;
5582 struct nfs4_stateowner
*sop
;
5583 struct nfs4_lockowner
*lo
= NULL
;
5584 struct nfs4_ol_stateid
*stp
;
5585 struct xdr_netobj
*owner
= &rlockowner
->rl_owner
;
5586 unsigned int hashval
= ownerstr_hashval(owner
);
5588 struct nfsd_net
*nn
= net_generic(SVC_NET(rqstp
), nfsd_net_id
);
5589 struct nfs4_client
*clp
;
5591 dprintk("nfsd4_release_lockowner clientid: (%08x/%08x):\n",
5592 clid
->cl_boot
, clid
->cl_id
);
5594 status
= lookup_clientid(clid
, cstate
, nn
);
5599 /* Find the matching lock stateowner */
5600 spin_lock(&clp
->cl_lock
);
5601 list_for_each_entry(sop
, &clp
->cl_ownerstr_hashtbl
[hashval
],
5604 if (sop
->so_is_open_owner
|| !same_owner_str(sop
, owner
))
5607 /* see if there are still any locks associated with it */
5608 lo
= lockowner(sop
);
5609 list_for_each_entry(stp
, &sop
->so_stateids
, st_perstateowner
) {
5610 if (check_for_locks(stp
->st_stid
.sc_file
, lo
)) {
5611 status
= nfserr_locks_held
;
5612 spin_unlock(&clp
->cl_lock
);
5617 nfs4_get_stateowner(sop
);
5620 spin_unlock(&clp
->cl_lock
);
5622 release_lockowner(lo
);
5626 static inline struct nfs4_client_reclaim
*
5629 return kmalloc(sizeof(struct nfs4_client_reclaim
), GFP_KERNEL
);
5633 nfs4_has_reclaimed_state(const char *name
, struct nfsd_net
*nn
)
5635 struct nfs4_client_reclaim
*crp
;
5637 crp
= nfsd4_find_reclaim_client(name
, nn
);
5638 return (crp
&& crp
->cr_clp
);
5642 * failure => all reset bets are off, nfserr_no_grace...
5644 struct nfs4_client_reclaim
*
5645 nfs4_client_to_reclaim(const char *name
, struct nfsd_net
*nn
)
5647 unsigned int strhashval
;
5648 struct nfs4_client_reclaim
*crp
;
5650 dprintk("NFSD nfs4_client_to_reclaim NAME: %.*s\n", HEXDIR_LEN
, name
);
5651 crp
= alloc_reclaim();
5653 strhashval
= clientstr_hashval(name
);
5654 INIT_LIST_HEAD(&crp
->cr_strhash
);
5655 list_add(&crp
->cr_strhash
, &nn
->reclaim_str_hashtbl
[strhashval
]);
5656 memcpy(crp
->cr_recdir
, name
, HEXDIR_LEN
);
5658 nn
->reclaim_str_hashtbl_size
++;
5664 nfs4_remove_reclaim_record(struct nfs4_client_reclaim
*crp
, struct nfsd_net
*nn
)
5666 list_del(&crp
->cr_strhash
);
5668 nn
->reclaim_str_hashtbl_size
--;
5672 nfs4_release_reclaim(struct nfsd_net
*nn
)
5674 struct nfs4_client_reclaim
*crp
= NULL
;
5677 for (i
= 0; i
< CLIENT_HASH_SIZE
; i
++) {
5678 while (!list_empty(&nn
->reclaim_str_hashtbl
[i
])) {
5679 crp
= list_entry(nn
->reclaim_str_hashtbl
[i
].next
,
5680 struct nfs4_client_reclaim
, cr_strhash
);
5681 nfs4_remove_reclaim_record(crp
, nn
);
5684 WARN_ON_ONCE(nn
->reclaim_str_hashtbl_size
);
5688 * called from OPEN, CLAIM_PREVIOUS with a new clientid. */
5689 struct nfs4_client_reclaim
*
5690 nfsd4_find_reclaim_client(const char *recdir
, struct nfsd_net
*nn
)
5692 unsigned int strhashval
;
5693 struct nfs4_client_reclaim
*crp
= NULL
;
5695 dprintk("NFSD: nfs4_find_reclaim_client for recdir %s\n", recdir
);
5697 strhashval
= clientstr_hashval(recdir
);
5698 list_for_each_entry(crp
, &nn
->reclaim_str_hashtbl
[strhashval
], cr_strhash
) {
5699 if (same_name(crp
->cr_recdir
, recdir
)) {
5707 * Called from OPEN. Look for clientid in reclaim list.
5710 nfs4_check_open_reclaim(clientid_t
*clid
,
5711 struct nfsd4_compound_state
*cstate
,
5712 struct nfsd_net
*nn
)
5716 /* find clientid in conf_id_hashtbl */
5717 status
= lookup_clientid(clid
, cstate
, nn
);
5719 return nfserr_reclaim_bad
;
5721 if (test_bit(NFSD4_CLIENT_RECLAIM_COMPLETE
, &cstate
->clp
->cl_flags
))
5722 return nfserr_no_grace
;
5724 if (nfsd4_client_record_check(cstate
->clp
))
5725 return nfserr_reclaim_bad
;
5730 #ifdef CONFIG_NFSD_FAULT_INJECTION
5732 put_client(struct nfs4_client
*clp
)
5734 atomic_dec(&clp
->cl_refcount
);
5737 static struct nfs4_client
*
5738 nfsd_find_client(struct sockaddr_storage
*addr
, size_t addr_size
)
5740 struct nfs4_client
*clp
;
5741 struct nfsd_net
*nn
= net_generic(current
->nsproxy
->net_ns
,
5744 if (!nfsd_netns_ready(nn
))
5747 list_for_each_entry(clp
, &nn
->client_lru
, cl_lru
) {
5748 if (memcmp(&clp
->cl_addr
, addr
, addr_size
) == 0)
5755 nfsd_inject_print_clients(void)
5757 struct nfs4_client
*clp
;
5759 struct nfsd_net
*nn
= net_generic(current
->nsproxy
->net_ns
,
5761 char buf
[INET6_ADDRSTRLEN
];
5763 if (!nfsd_netns_ready(nn
))
5766 spin_lock(&nn
->client_lock
);
5767 list_for_each_entry(clp
, &nn
->client_lru
, cl_lru
) {
5768 rpc_ntop((struct sockaddr
*)&clp
->cl_addr
, buf
, sizeof(buf
));
5769 pr_info("NFS Client: %s\n", buf
);
5772 spin_unlock(&nn
->client_lock
);
5778 nfsd_inject_forget_client(struct sockaddr_storage
*addr
, size_t addr_size
)
5781 struct nfs4_client
*clp
;
5782 struct nfsd_net
*nn
= net_generic(current
->nsproxy
->net_ns
,
5785 if (!nfsd_netns_ready(nn
))
5788 spin_lock(&nn
->client_lock
);
5789 clp
= nfsd_find_client(addr
, addr_size
);
5791 if (mark_client_expired_locked(clp
) == nfs_ok
)
5796 spin_unlock(&nn
->client_lock
);
5805 nfsd_inject_forget_clients(u64 max
)
5808 struct nfs4_client
*clp
, *next
;
5809 struct nfsd_net
*nn
= net_generic(current
->nsproxy
->net_ns
,
5811 LIST_HEAD(reaplist
);
5813 if (!nfsd_netns_ready(nn
))
5816 spin_lock(&nn
->client_lock
);
5817 list_for_each_entry_safe(clp
, next
, &nn
->client_lru
, cl_lru
) {
5818 if (mark_client_expired_locked(clp
) == nfs_ok
) {
5819 list_add(&clp
->cl_lru
, &reaplist
);
5820 if (max
!= 0 && ++count
>= max
)
5824 spin_unlock(&nn
->client_lock
);
5826 list_for_each_entry_safe(clp
, next
, &reaplist
, cl_lru
)
5832 static void nfsd_print_count(struct nfs4_client
*clp
, unsigned int count
,
5835 char buf
[INET6_ADDRSTRLEN
];
5836 rpc_ntop((struct sockaddr
*)&clp
->cl_addr
, buf
, sizeof(buf
));
5837 printk(KERN_INFO
"NFS Client: %s has %u %s\n", buf
, count
, type
);
5841 nfsd_inject_add_lock_to_list(struct nfs4_ol_stateid
*lst
,
5842 struct list_head
*collect
)
5844 struct nfs4_client
*clp
= lst
->st_stid
.sc_client
;
5845 struct nfsd_net
*nn
= net_generic(current
->nsproxy
->net_ns
,
5851 lockdep_assert_held(&nn
->client_lock
);
5852 atomic_inc(&clp
->cl_refcount
);
5853 list_add(&lst
->st_locks
, collect
);
5856 static u64
nfsd_foreach_client_lock(struct nfs4_client
*clp
, u64 max
,
5857 struct list_head
*collect
,
5858 void (*func
)(struct nfs4_ol_stateid
*))
5860 struct nfs4_openowner
*oop
;
5861 struct nfs4_ol_stateid
*stp
, *st_next
;
5862 struct nfs4_ol_stateid
*lst
, *lst_next
;
5865 spin_lock(&clp
->cl_lock
);
5866 list_for_each_entry(oop
, &clp
->cl_openowners
, oo_perclient
) {
5867 list_for_each_entry_safe(stp
, st_next
,
5868 &oop
->oo_owner
.so_stateids
, st_perstateowner
) {
5869 list_for_each_entry_safe(lst
, lst_next
,
5870 &stp
->st_locks
, st_locks
) {
5873 nfsd_inject_add_lock_to_list(lst
,
5878 * Despite the fact that these functions deal
5879 * with 64-bit integers for "count", we must
5880 * ensure that it doesn't blow up the
5881 * clp->cl_refcount. Throw a warning if we
5882 * start to approach INT_MAX here.
5884 WARN_ON_ONCE(count
== (INT_MAX
/ 2));
5891 spin_unlock(&clp
->cl_lock
);
5897 nfsd_collect_client_locks(struct nfs4_client
*clp
, struct list_head
*collect
,
5900 return nfsd_foreach_client_lock(clp
, max
, collect
, unhash_lock_stateid
);
5904 nfsd_print_client_locks(struct nfs4_client
*clp
)
5906 u64 count
= nfsd_foreach_client_lock(clp
, 0, NULL
, NULL
);
5907 nfsd_print_count(clp
, count
, "locked files");
5912 nfsd_inject_print_locks(void)
5914 struct nfs4_client
*clp
;
5916 struct nfsd_net
*nn
= net_generic(current
->nsproxy
->net_ns
,
5919 if (!nfsd_netns_ready(nn
))
5922 spin_lock(&nn
->client_lock
);
5923 list_for_each_entry(clp
, &nn
->client_lru
, cl_lru
)
5924 count
+= nfsd_print_client_locks(clp
);
5925 spin_unlock(&nn
->client_lock
);
5931 nfsd_reap_locks(struct list_head
*reaplist
)
5933 struct nfs4_client
*clp
;
5934 struct nfs4_ol_stateid
*stp
, *next
;
5936 list_for_each_entry_safe(stp
, next
, reaplist
, st_locks
) {
5937 list_del_init(&stp
->st_locks
);
5938 clp
= stp
->st_stid
.sc_client
;
5939 nfs4_put_stid(&stp
->st_stid
);
5945 nfsd_inject_forget_client_locks(struct sockaddr_storage
*addr
, size_t addr_size
)
5947 unsigned int count
= 0;
5948 struct nfs4_client
*clp
;
5949 struct nfsd_net
*nn
= net_generic(current
->nsproxy
->net_ns
,
5951 LIST_HEAD(reaplist
);
5953 if (!nfsd_netns_ready(nn
))
5956 spin_lock(&nn
->client_lock
);
5957 clp
= nfsd_find_client(addr
, addr_size
);
5959 count
= nfsd_collect_client_locks(clp
, &reaplist
, 0);
5960 spin_unlock(&nn
->client_lock
);
5961 nfsd_reap_locks(&reaplist
);
5966 nfsd_inject_forget_locks(u64 max
)
5969 struct nfs4_client
*clp
;
5970 struct nfsd_net
*nn
= net_generic(current
->nsproxy
->net_ns
,
5972 LIST_HEAD(reaplist
);
5974 if (!nfsd_netns_ready(nn
))
5977 spin_lock(&nn
->client_lock
);
5978 list_for_each_entry(clp
, &nn
->client_lru
, cl_lru
) {
5979 count
+= nfsd_collect_client_locks(clp
, &reaplist
, max
- count
);
5980 if (max
!= 0 && count
>= max
)
5983 spin_unlock(&nn
->client_lock
);
5984 nfsd_reap_locks(&reaplist
);
5989 nfsd_foreach_client_openowner(struct nfs4_client
*clp
, u64 max
,
5990 struct list_head
*collect
,
5991 void (*func
)(struct nfs4_openowner
*))
5993 struct nfs4_openowner
*oop
, *next
;
5994 struct nfsd_net
*nn
= net_generic(current
->nsproxy
->net_ns
,
5998 lockdep_assert_held(&nn
->client_lock
);
6000 spin_lock(&clp
->cl_lock
);
6001 list_for_each_entry_safe(oop
, next
, &clp
->cl_openowners
, oo_perclient
) {
6005 atomic_inc(&clp
->cl_refcount
);
6006 list_add(&oop
->oo_perclient
, collect
);
6011 * Despite the fact that these functions deal with
6012 * 64-bit integers for "count", we must ensure that
6013 * it doesn't blow up the clp->cl_refcount. Throw a
6014 * warning if we start to approach INT_MAX here.
6016 WARN_ON_ONCE(count
== (INT_MAX
/ 2));
6020 spin_unlock(&clp
->cl_lock
);
6026 nfsd_print_client_openowners(struct nfs4_client
*clp
)
6028 u64 count
= nfsd_foreach_client_openowner(clp
, 0, NULL
, NULL
);
6030 nfsd_print_count(clp
, count
, "openowners");
6035 nfsd_collect_client_openowners(struct nfs4_client
*clp
,
6036 struct list_head
*collect
, u64 max
)
6038 return nfsd_foreach_client_openowner(clp
, max
, collect
,
6039 unhash_openowner_locked
);
6043 nfsd_inject_print_openowners(void)
6045 struct nfs4_client
*clp
;
6047 struct nfsd_net
*nn
= net_generic(current
->nsproxy
->net_ns
,
6050 if (!nfsd_netns_ready(nn
))
6053 spin_lock(&nn
->client_lock
);
6054 list_for_each_entry(clp
, &nn
->client_lru
, cl_lru
)
6055 count
+= nfsd_print_client_openowners(clp
);
6056 spin_unlock(&nn
->client_lock
);
6062 nfsd_reap_openowners(struct list_head
*reaplist
)
6064 struct nfs4_client
*clp
;
6065 struct nfs4_openowner
*oop
, *next
;
6067 list_for_each_entry_safe(oop
, next
, reaplist
, oo_perclient
) {
6068 list_del_init(&oop
->oo_perclient
);
6069 clp
= oop
->oo_owner
.so_client
;
6070 release_openowner(oop
);
6076 nfsd_inject_forget_client_openowners(struct sockaddr_storage
*addr
,
6079 unsigned int count
= 0;
6080 struct nfs4_client
*clp
;
6081 struct nfsd_net
*nn
= net_generic(current
->nsproxy
->net_ns
,
6083 LIST_HEAD(reaplist
);
6085 if (!nfsd_netns_ready(nn
))
6088 spin_lock(&nn
->client_lock
);
6089 clp
= nfsd_find_client(addr
, addr_size
);
6091 count
= nfsd_collect_client_openowners(clp
, &reaplist
, 0);
6092 spin_unlock(&nn
->client_lock
);
6093 nfsd_reap_openowners(&reaplist
);
6098 nfsd_inject_forget_openowners(u64 max
)
6101 struct nfs4_client
*clp
;
6102 struct nfsd_net
*nn
= net_generic(current
->nsproxy
->net_ns
,
6104 LIST_HEAD(reaplist
);
6106 if (!nfsd_netns_ready(nn
))
6109 spin_lock(&nn
->client_lock
);
6110 list_for_each_entry(clp
, &nn
->client_lru
, cl_lru
) {
6111 count
+= nfsd_collect_client_openowners(clp
, &reaplist
,
6113 if (max
!= 0 && count
>= max
)
6116 spin_unlock(&nn
->client_lock
);
6117 nfsd_reap_openowners(&reaplist
);
6121 static u64
nfsd_find_all_delegations(struct nfs4_client
*clp
, u64 max
,
6122 struct list_head
*victims
)
6124 struct nfs4_delegation
*dp
, *next
;
6125 struct nfsd_net
*nn
= net_generic(current
->nsproxy
->net_ns
,
6129 lockdep_assert_held(&nn
->client_lock
);
6131 spin_lock(&state_lock
);
6132 list_for_each_entry_safe(dp
, next
, &clp
->cl_delegations
, dl_perclnt
) {
6135 * It's not safe to mess with delegations that have a
6136 * non-zero dl_time. They might have already been broken
6137 * and could be processed by the laundromat outside of
6138 * the state_lock. Just leave them be.
6140 if (dp
->dl_time
!= 0)
6143 atomic_inc(&clp
->cl_refcount
);
6144 unhash_delegation_locked(dp
);
6145 list_add(&dp
->dl_recall_lru
, victims
);
6149 * Despite the fact that these functions deal with
6150 * 64-bit integers for "count", we must ensure that
6151 * it doesn't blow up the clp->cl_refcount. Throw a
6152 * warning if we start to approach INT_MAX here.
6154 WARN_ON_ONCE(count
== (INT_MAX
/ 2));
6158 spin_unlock(&state_lock
);
6163 nfsd_print_client_delegations(struct nfs4_client
*clp
)
6165 u64 count
= nfsd_find_all_delegations(clp
, 0, NULL
);
6167 nfsd_print_count(clp
, count
, "delegations");
6172 nfsd_inject_print_delegations(void)
6174 struct nfs4_client
*clp
;
6176 struct nfsd_net
*nn
= net_generic(current
->nsproxy
->net_ns
,
6179 if (!nfsd_netns_ready(nn
))
6182 spin_lock(&nn
->client_lock
);
6183 list_for_each_entry(clp
, &nn
->client_lru
, cl_lru
)
6184 count
+= nfsd_print_client_delegations(clp
);
6185 spin_unlock(&nn
->client_lock
);
6191 nfsd_forget_delegations(struct list_head
*reaplist
)
6193 struct nfs4_client
*clp
;
6194 struct nfs4_delegation
*dp
, *next
;
6196 list_for_each_entry_safe(dp
, next
, reaplist
, dl_recall_lru
) {
6197 list_del_init(&dp
->dl_recall_lru
);
6198 clp
= dp
->dl_stid
.sc_client
;
6199 revoke_delegation(dp
);
6205 nfsd_inject_forget_client_delegations(struct sockaddr_storage
*addr
,
6209 struct nfs4_client
*clp
;
6210 struct nfsd_net
*nn
= net_generic(current
->nsproxy
->net_ns
,
6212 LIST_HEAD(reaplist
);
6214 if (!nfsd_netns_ready(nn
))
6217 spin_lock(&nn
->client_lock
);
6218 clp
= nfsd_find_client(addr
, addr_size
);
6220 count
= nfsd_find_all_delegations(clp
, 0, &reaplist
);
6221 spin_unlock(&nn
->client_lock
);
6223 nfsd_forget_delegations(&reaplist
);
6228 nfsd_inject_forget_delegations(u64 max
)
6231 struct nfs4_client
*clp
;
6232 struct nfsd_net
*nn
= net_generic(current
->nsproxy
->net_ns
,
6234 LIST_HEAD(reaplist
);
6236 if (!nfsd_netns_ready(nn
))
6239 spin_lock(&nn
->client_lock
);
6240 list_for_each_entry(clp
, &nn
->client_lru
, cl_lru
) {
6241 count
+= nfsd_find_all_delegations(clp
, max
- count
, &reaplist
);
6242 if (max
!= 0 && count
>= max
)
6245 spin_unlock(&nn
->client_lock
);
6246 nfsd_forget_delegations(&reaplist
);
6251 nfsd_recall_delegations(struct list_head
*reaplist
)
6253 struct nfs4_client
*clp
;
6254 struct nfs4_delegation
*dp
, *next
;
6256 list_for_each_entry_safe(dp
, next
, reaplist
, dl_recall_lru
) {
6257 list_del_init(&dp
->dl_recall_lru
);
6258 clp
= dp
->dl_stid
.sc_client
;
6260 * We skipped all entries that had a zero dl_time before,
6261 * so we can now reset the dl_time back to 0. If a delegation
6262 * break comes in now, then it won't make any difference since
6263 * we're recalling it either way.
6265 spin_lock(&state_lock
);
6267 spin_unlock(&state_lock
);
6268 nfsd_break_one_deleg(dp
);
6274 nfsd_inject_recall_client_delegations(struct sockaddr_storage
*addr
,
6278 struct nfs4_client
*clp
;
6279 struct nfsd_net
*nn
= net_generic(current
->nsproxy
->net_ns
,
6281 LIST_HEAD(reaplist
);
6283 if (!nfsd_netns_ready(nn
))
6286 spin_lock(&nn
->client_lock
);
6287 clp
= nfsd_find_client(addr
, addr_size
);
6289 count
= nfsd_find_all_delegations(clp
, 0, &reaplist
);
6290 spin_unlock(&nn
->client_lock
);
6292 nfsd_recall_delegations(&reaplist
);
6297 nfsd_inject_recall_delegations(u64 max
)
6300 struct nfs4_client
*clp
, *next
;
6301 struct nfsd_net
*nn
= net_generic(current
->nsproxy
->net_ns
,
6303 LIST_HEAD(reaplist
);
6305 if (!nfsd_netns_ready(nn
))
6308 spin_lock(&nn
->client_lock
);
6309 list_for_each_entry_safe(clp
, next
, &nn
->client_lru
, cl_lru
) {
6310 count
+= nfsd_find_all_delegations(clp
, max
- count
, &reaplist
);
6311 if (max
!= 0 && ++count
>= max
)
6314 spin_unlock(&nn
->client_lock
);
6315 nfsd_recall_delegations(&reaplist
);
6318 #endif /* CONFIG_NFSD_FAULT_INJECTION */
6321 * Since the lifetime of a delegation isn't limited to that of an open, a
6322 * client may quite reasonably hang on to a delegation as long as it has
6323 * the inode cached. This becomes an obvious problem the first time a
6324 * client's inode cache approaches the size of the server's total memory.
6326 * For now we avoid this problem by imposing a hard limit on the number
6327 * of delegations, which varies according to the server's memory size.
6330 set_max_delegations(void)
6333 * Allow at most 4 delegations per megabyte of RAM. Quick
6334 * estimates suggest that in the worst case (where every delegation
6335 * is for a different inode), a delegation could take about 1.5K,
6336 * giving a worst case usage of about 6% of memory.
6338 max_delegations
= nr_free_buffer_pages() >> (20 - 2 - PAGE_SHIFT
);
6341 static int nfs4_state_create_net(struct net
*net
)
6343 struct nfsd_net
*nn
= net_generic(net
, nfsd_net_id
);
6346 nn
->conf_id_hashtbl
= kmalloc(sizeof(struct list_head
) *
6347 CLIENT_HASH_SIZE
, GFP_KERNEL
);
6348 if (!nn
->conf_id_hashtbl
)
6350 nn
->unconf_id_hashtbl
= kmalloc(sizeof(struct list_head
) *
6351 CLIENT_HASH_SIZE
, GFP_KERNEL
);
6352 if (!nn
->unconf_id_hashtbl
)
6354 nn
->sessionid_hashtbl
= kmalloc(sizeof(struct list_head
) *
6355 SESSION_HASH_SIZE
, GFP_KERNEL
);
6356 if (!nn
->sessionid_hashtbl
)
6359 for (i
= 0; i
< CLIENT_HASH_SIZE
; i
++) {
6360 INIT_LIST_HEAD(&nn
->conf_id_hashtbl
[i
]);
6361 INIT_LIST_HEAD(&nn
->unconf_id_hashtbl
[i
]);
6363 for (i
= 0; i
< SESSION_HASH_SIZE
; i
++)
6364 INIT_LIST_HEAD(&nn
->sessionid_hashtbl
[i
]);
6365 nn
->conf_name_tree
= RB_ROOT
;
6366 nn
->unconf_name_tree
= RB_ROOT
;
6367 INIT_LIST_HEAD(&nn
->client_lru
);
6368 INIT_LIST_HEAD(&nn
->close_lru
);
6369 INIT_LIST_HEAD(&nn
->del_recall_lru
);
6370 spin_lock_init(&nn
->client_lock
);
6372 INIT_DELAYED_WORK(&nn
->laundromat_work
, laundromat_main
);
6378 kfree(nn
->unconf_id_hashtbl
);
6380 kfree(nn
->conf_id_hashtbl
);
6386 nfs4_state_destroy_net(struct net
*net
)
6389 struct nfs4_client
*clp
= NULL
;
6390 struct nfsd_net
*nn
= net_generic(net
, nfsd_net_id
);
6392 for (i
= 0; i
< CLIENT_HASH_SIZE
; i
++) {
6393 while (!list_empty(&nn
->conf_id_hashtbl
[i
])) {
6394 clp
= list_entry(nn
->conf_id_hashtbl
[i
].next
, struct nfs4_client
, cl_idhash
);
6395 destroy_client(clp
);
6399 for (i
= 0; i
< CLIENT_HASH_SIZE
; i
++) {
6400 while (!list_empty(&nn
->unconf_id_hashtbl
[i
])) {
6401 clp
= list_entry(nn
->unconf_id_hashtbl
[i
].next
, struct nfs4_client
, cl_idhash
);
6402 destroy_client(clp
);
6406 kfree(nn
->sessionid_hashtbl
);
6407 kfree(nn
->unconf_id_hashtbl
);
6408 kfree(nn
->conf_id_hashtbl
);
6413 nfs4_state_start_net(struct net
*net
)
6415 struct nfsd_net
*nn
= net_generic(net
, nfsd_net_id
);
6418 ret
= nfs4_state_create_net(net
);
6421 nn
->boot_time
= get_seconds();
6422 nn
->grace_ended
= false;
6423 locks_start_grace(net
, &nn
->nfsd4_manager
);
6424 nfsd4_client_tracking_init(net
);
6425 printk(KERN_INFO
"NFSD: starting %ld-second grace period (net %p)\n",
6426 nn
->nfsd4_grace
, net
);
6427 queue_delayed_work(laundry_wq
, &nn
->laundromat_work
, nn
->nfsd4_grace
* HZ
);
6431 /* initialization to perform when the nfsd service is started: */
6434 nfs4_state_start(void)
6438 ret
= set_callback_cred();
6441 laundry_wq
= create_singlethread_workqueue("nfsd4");
6442 if (laundry_wq
== NULL
) {
6446 ret
= nfsd4_create_callback_queue();
6448 goto out_free_laundry
;
6450 set_max_delegations();
6455 destroy_workqueue(laundry_wq
);
6461 nfs4_state_shutdown_net(struct net
*net
)
6463 struct nfs4_delegation
*dp
= NULL
;
6464 struct list_head
*pos
, *next
, reaplist
;
6465 struct nfsd_net
*nn
= net_generic(net
, nfsd_net_id
);
6467 cancel_delayed_work_sync(&nn
->laundromat_work
);
6468 locks_end_grace(&nn
->nfsd4_manager
);
6470 INIT_LIST_HEAD(&reaplist
);
6471 spin_lock(&state_lock
);
6472 list_for_each_safe(pos
, next
, &nn
->del_recall_lru
) {
6473 dp
= list_entry (pos
, struct nfs4_delegation
, dl_recall_lru
);
6474 unhash_delegation_locked(dp
);
6475 list_add(&dp
->dl_recall_lru
, &reaplist
);
6477 spin_unlock(&state_lock
);
6478 list_for_each_safe(pos
, next
, &reaplist
) {
6479 dp
= list_entry (pos
, struct nfs4_delegation
, dl_recall_lru
);
6480 list_del_init(&dp
->dl_recall_lru
);
6481 nfs4_put_deleg_lease(dp
->dl_stid
.sc_file
);
6482 nfs4_put_stid(&dp
->dl_stid
);
6485 nfsd4_client_tracking_exit(net
);
6486 nfs4_state_destroy_net(net
);
6490 nfs4_state_shutdown(void)
6492 destroy_workqueue(laundry_wq
);
6493 nfsd4_destroy_callback_queue();
6497 get_stateid(struct nfsd4_compound_state
*cstate
, stateid_t
*stateid
)
6499 if (HAS_STATE_ID(cstate
, CURRENT_STATE_ID_FLAG
) && CURRENT_STATEID(stateid
))
6500 memcpy(stateid
, &cstate
->current_stateid
, sizeof(stateid_t
));
6504 put_stateid(struct nfsd4_compound_state
*cstate
, stateid_t
*stateid
)
6506 if (cstate
->minorversion
) {
6507 memcpy(&cstate
->current_stateid
, stateid
, sizeof(stateid_t
));
6508 SET_STATE_ID(cstate
, CURRENT_STATE_ID_FLAG
);
6513 clear_current_stateid(struct nfsd4_compound_state
*cstate
)
6515 CLEAR_STATE_ID(cstate
, CURRENT_STATE_ID_FLAG
);
6519 * functions to set current state id
6522 nfsd4_set_opendowngradestateid(struct nfsd4_compound_state
*cstate
, struct nfsd4_open_downgrade
*odp
)
6524 put_stateid(cstate
, &odp
->od_stateid
);
6528 nfsd4_set_openstateid(struct nfsd4_compound_state
*cstate
, struct nfsd4_open
*open
)
6530 put_stateid(cstate
, &open
->op_stateid
);
6534 nfsd4_set_closestateid(struct nfsd4_compound_state
*cstate
, struct nfsd4_close
*close
)
6536 put_stateid(cstate
, &close
->cl_stateid
);
6540 nfsd4_set_lockstateid(struct nfsd4_compound_state
*cstate
, struct nfsd4_lock
*lock
)
6542 put_stateid(cstate
, &lock
->lk_resp_stateid
);
6546 * functions to consume current state id
6550 nfsd4_get_opendowngradestateid(struct nfsd4_compound_state
*cstate
, struct nfsd4_open_downgrade
*odp
)
6552 get_stateid(cstate
, &odp
->od_stateid
);
6556 nfsd4_get_delegreturnstateid(struct nfsd4_compound_state
*cstate
, struct nfsd4_delegreturn
*drp
)
6558 get_stateid(cstate
, &drp
->dr_stateid
);
6562 nfsd4_get_freestateid(struct nfsd4_compound_state
*cstate
, struct nfsd4_free_stateid
*fsp
)
6564 get_stateid(cstate
, &fsp
->fr_stateid
);
6568 nfsd4_get_setattrstateid(struct nfsd4_compound_state
*cstate
, struct nfsd4_setattr
*setattr
)
6570 get_stateid(cstate
, &setattr
->sa_stateid
);
6574 nfsd4_get_closestateid(struct nfsd4_compound_state
*cstate
, struct nfsd4_close
*close
)
6576 get_stateid(cstate
, &close
->cl_stateid
);
6580 nfsd4_get_lockustateid(struct nfsd4_compound_state
*cstate
, struct nfsd4_locku
*locku
)
6582 get_stateid(cstate
, &locku
->lu_stateid
);
6586 nfsd4_get_readstateid(struct nfsd4_compound_state
*cstate
, struct nfsd4_read
*read
)
6588 get_stateid(cstate
, &read
->rd_stateid
);
6592 nfsd4_get_writestateid(struct nfsd4_compound_state
*cstate
, struct nfsd4_write
*write
)
6594 get_stateid(cstate
, &write
->wr_stateid
);