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/hash.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
);
77 /* Currently used for almost all code touching nfsv4 state: */
78 static DEFINE_MUTEX(client_mutex
);
81 * Currently used for the del_recall_lru and file hash table. In an
82 * effort to decrease the scope of the client_mutex, this spinlock may
83 * eventually cover more:
85 static DEFINE_SPINLOCK(state_lock
);
87 static struct kmem_cache
*openowner_slab
;
88 static struct kmem_cache
*lockowner_slab
;
89 static struct kmem_cache
*file_slab
;
90 static struct kmem_cache
*stateid_slab
;
91 static struct kmem_cache
*deleg_slab
;
96 mutex_lock(&client_mutex
);
99 static void free_session(struct nfsd4_session
*);
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
;
115 nfs4_unlock_state(void)
117 mutex_unlock(&client_mutex
);
120 static bool is_client_expired(struct nfs4_client
*clp
)
122 return clp
->cl_time
== 0;
125 static __be32
mark_client_expired_locked(struct nfs4_client
*clp
)
127 if (atomic_read(&clp
->cl_refcount
))
128 return nfserr_jukebox
;
133 static __be32
mark_client_expired(struct nfs4_client
*clp
)
135 struct nfsd_net
*nn
= net_generic(clp
->net
, nfsd_net_id
);
138 spin_lock(&nn
->client_lock
);
139 ret
= mark_client_expired_locked(clp
);
140 spin_unlock(&nn
->client_lock
);
144 static __be32
get_client_locked(struct nfs4_client
*clp
)
146 if (is_client_expired(clp
))
147 return nfserr_expired
;
148 atomic_inc(&clp
->cl_refcount
);
152 /* must be called under the client_lock */
154 renew_client_locked(struct nfs4_client
*clp
)
156 struct nfsd_net
*nn
= net_generic(clp
->net
, nfsd_net_id
);
158 if (is_client_expired(clp
)) {
160 printk("%s: client (clientid %08x/%08x) already expired\n",
162 clp
->cl_clientid
.cl_boot
,
163 clp
->cl_clientid
.cl_id
);
167 dprintk("renewing client (clientid %08x/%08x)\n",
168 clp
->cl_clientid
.cl_boot
,
169 clp
->cl_clientid
.cl_id
);
170 list_move_tail(&clp
->cl_lru
, &nn
->client_lru
);
171 clp
->cl_time
= get_seconds();
175 renew_client(struct nfs4_client
*clp
)
177 struct nfsd_net
*nn
= net_generic(clp
->net
, nfsd_net_id
);
179 spin_lock(&nn
->client_lock
);
180 renew_client_locked(clp
);
181 spin_unlock(&nn
->client_lock
);
184 static void put_client_renew_locked(struct nfs4_client
*clp
)
186 if (!atomic_dec_and_test(&clp
->cl_refcount
))
188 if (!is_client_expired(clp
))
189 renew_client_locked(clp
);
192 static void put_client_renew(struct nfs4_client
*clp
)
194 struct nfsd_net
*nn
= net_generic(clp
->net
, nfsd_net_id
);
196 if (!atomic_dec_and_lock(&clp
->cl_refcount
, &nn
->client_lock
))
198 if (!is_client_expired(clp
))
199 renew_client_locked(clp
);
200 spin_unlock(&nn
->client_lock
);
203 static __be32
nfsd4_get_session_locked(struct nfsd4_session
*ses
)
207 if (is_session_dead(ses
))
208 return nfserr_badsession
;
209 status
= get_client_locked(ses
->se_client
);
212 atomic_inc(&ses
->se_ref
);
216 static void nfsd4_put_session_locked(struct nfsd4_session
*ses
)
218 struct nfs4_client
*clp
= ses
->se_client
;
220 if (atomic_dec_and_test(&ses
->se_ref
) && is_session_dead(ses
))
222 put_client_renew_locked(clp
);
225 static void nfsd4_put_session(struct nfsd4_session
*ses
)
227 struct nfs4_client
*clp
= ses
->se_client
;
228 struct nfsd_net
*nn
= net_generic(clp
->net
, nfsd_net_id
);
230 spin_lock(&nn
->client_lock
);
231 nfsd4_put_session_locked(ses
);
232 spin_unlock(&nn
->client_lock
);
237 opaque_hashval(const void *ptr
, int nbytes
)
239 unsigned char *cptr
= (unsigned char *) ptr
;
249 static void nfsd4_free_file(struct nfs4_file
*f
)
251 kmem_cache_free(file_slab
, f
);
255 put_nfs4_file(struct nfs4_file
*fi
)
257 might_lock(&state_lock
);
259 if (atomic_dec_and_lock(&fi
->fi_ref
, &state_lock
)) {
260 hlist_del(&fi
->fi_hash
);
261 spin_unlock(&state_lock
);
267 get_nfs4_file(struct nfs4_file
*fi
)
269 atomic_inc(&fi
->fi_ref
);
273 __nfs4_get_fd(struct nfs4_file
*f
, int oflag
)
275 if (f
->fi_fds
[oflag
])
276 return get_file(f
->fi_fds
[oflag
]);
281 find_writeable_file_locked(struct nfs4_file
*f
)
285 lockdep_assert_held(&f
->fi_lock
);
287 ret
= __nfs4_get_fd(f
, O_WRONLY
);
289 ret
= __nfs4_get_fd(f
, O_RDWR
);
294 find_writeable_file(struct nfs4_file
*f
)
298 spin_lock(&f
->fi_lock
);
299 ret
= find_writeable_file_locked(f
);
300 spin_unlock(&f
->fi_lock
);
305 static struct file
*find_readable_file_locked(struct nfs4_file
*f
)
309 lockdep_assert_held(&f
->fi_lock
);
311 ret
= __nfs4_get_fd(f
, O_RDONLY
);
313 ret
= __nfs4_get_fd(f
, O_RDWR
);
318 find_readable_file(struct nfs4_file
*f
)
322 spin_lock(&f
->fi_lock
);
323 ret
= find_readable_file_locked(f
);
324 spin_unlock(&f
->fi_lock
);
330 find_any_file(struct nfs4_file
*f
)
334 spin_lock(&f
->fi_lock
);
335 ret
= __nfs4_get_fd(f
, O_RDWR
);
337 ret
= __nfs4_get_fd(f
, O_WRONLY
);
339 ret
= __nfs4_get_fd(f
, O_RDONLY
);
341 spin_unlock(&f
->fi_lock
);
345 static atomic_long_t num_delegations
;
346 unsigned long max_delegations
;
349 * Open owner state (share locks)
352 /* hash tables for lock and open owners */
353 #define OWNER_HASH_BITS 8
354 #define OWNER_HASH_SIZE (1 << OWNER_HASH_BITS)
355 #define OWNER_HASH_MASK (OWNER_HASH_SIZE - 1)
357 static unsigned int ownerstr_hashval(u32 clientid
, struct xdr_netobj
*ownername
)
361 ret
= opaque_hashval(ownername
->data
, ownername
->len
);
363 return ret
& OWNER_HASH_MASK
;
366 /* hash table for nfs4_file */
367 #define FILE_HASH_BITS 8
368 #define FILE_HASH_SIZE (1 << FILE_HASH_BITS)
370 static unsigned int nfsd_fh_hashval(struct knfsd_fh
*fh
)
372 return jhash2(fh
->fh_base
.fh_pad
, XDR_QUADLEN(fh
->fh_size
), 0);
375 static unsigned int file_hashval(struct knfsd_fh
*fh
)
377 return nfsd_fh_hashval(fh
) & (FILE_HASH_SIZE
- 1);
380 static bool nfsd_fh_match(struct knfsd_fh
*fh1
, struct knfsd_fh
*fh2
)
382 return fh1
->fh_size
== fh2
->fh_size
&&
383 !memcmp(fh1
->fh_base
.fh_pad
,
388 static struct hlist_head file_hashtbl
[FILE_HASH_SIZE
];
391 __nfs4_file_get_access(struct nfs4_file
*fp
, u32 access
)
393 lockdep_assert_held(&fp
->fi_lock
);
395 if (access
& NFS4_SHARE_ACCESS_WRITE
)
396 atomic_inc(&fp
->fi_access
[O_WRONLY
]);
397 if (access
& NFS4_SHARE_ACCESS_READ
)
398 atomic_inc(&fp
->fi_access
[O_RDONLY
]);
402 nfs4_file_get_access(struct nfs4_file
*fp
, u32 access
)
404 lockdep_assert_held(&fp
->fi_lock
);
406 /* Does this access mode make sense? */
407 if (access
& ~NFS4_SHARE_ACCESS_BOTH
)
410 /* Does it conflict with a deny mode already set? */
411 if ((access
& fp
->fi_share_deny
) != 0)
412 return nfserr_share_denied
;
414 __nfs4_file_get_access(fp
, access
);
418 static __be32
nfs4_file_check_deny(struct nfs4_file
*fp
, u32 deny
)
420 /* Common case is that there is no deny mode. */
422 /* Does this deny mode make sense? */
423 if (deny
& ~NFS4_SHARE_DENY_BOTH
)
426 if ((deny
& NFS4_SHARE_DENY_READ
) &&
427 atomic_read(&fp
->fi_access
[O_RDONLY
]))
428 return nfserr_share_denied
;
430 if ((deny
& NFS4_SHARE_DENY_WRITE
) &&
431 atomic_read(&fp
->fi_access
[O_WRONLY
]))
432 return nfserr_share_denied
;
437 static void __nfs4_file_put_access(struct nfs4_file
*fp
, int oflag
)
439 might_lock(&fp
->fi_lock
);
441 if (atomic_dec_and_lock(&fp
->fi_access
[oflag
], &fp
->fi_lock
)) {
442 struct file
*f1
= NULL
;
443 struct file
*f2
= NULL
;
445 swap(f1
, fp
->fi_fds
[oflag
]);
446 if (atomic_read(&fp
->fi_access
[1 - oflag
]) == 0)
447 swap(f2
, fp
->fi_fds
[O_RDWR
]);
448 spin_unlock(&fp
->fi_lock
);
456 static void nfs4_file_put_access(struct nfs4_file
*fp
, u32 access
)
458 WARN_ON_ONCE(access
& ~NFS4_SHARE_ACCESS_BOTH
);
460 if (access
& NFS4_SHARE_ACCESS_WRITE
)
461 __nfs4_file_put_access(fp
, O_WRONLY
);
462 if (access
& NFS4_SHARE_ACCESS_READ
)
463 __nfs4_file_put_access(fp
, O_RDONLY
);
466 static struct nfs4_stid
*nfs4_alloc_stid(struct nfs4_client
*cl
, struct
469 struct idr
*stateids
= &cl
->cl_stateids
;
470 struct nfs4_stid
*stid
;
473 stid
= kmem_cache_zalloc(slab
, GFP_KERNEL
);
477 new_id
= idr_alloc_cyclic(stateids
, stid
, 0, 0, GFP_KERNEL
);
480 stid
->sc_client
= cl
;
481 stid
->sc_stateid
.si_opaque
.so_id
= new_id
;
482 stid
->sc_stateid
.si_opaque
.so_clid
= cl
->cl_clientid
;
483 /* Will be incremented before return to client: */
484 atomic_set(&stid
->sc_count
, 1);
487 * It shouldn't be a problem to reuse an opaque stateid value.
488 * I don't think it is for 4.1. But with 4.0 I worry that, for
489 * example, a stray write retransmission could be accepted by
490 * the server when it should have been rejected. Therefore,
491 * adopt a trick from the sctp code to attempt to maximize the
492 * amount of time until an id is reused, by ensuring they always
493 * "increase" (mod INT_MAX):
497 kmem_cache_free(slab
, stid
);
501 static struct nfs4_ol_stateid
* nfs4_alloc_stateid(struct nfs4_client
*clp
)
503 return openlockstateid(nfs4_alloc_stid(clp
, stateid_slab
));
507 * When we recall a delegation, we should be careful not to hand it
508 * out again straight away.
509 * To ensure this we keep a pair of bloom filters ('new' and 'old')
510 * in which the filehandles of recalled delegations are "stored".
511 * If a filehandle appear in either filter, a delegation is blocked.
512 * When a delegation is recalled, the filehandle is stored in the "new"
514 * Every 30 seconds we swap the filters and clear the "new" one,
515 * unless both are empty of course.
517 * Each filter is 256 bits. We hash the filehandle to 32bit and use the
518 * low 3 bytes as hash-table indices.
520 * 'state_lock', which is always held when block_delegations() is called,
521 * is used to manage concurrent access. Testing does not need the lock
522 * except when swapping the two filters.
524 static struct bloom_pair
{
525 int entries
, old_entries
;
527 int new; /* index into 'set' */
528 DECLARE_BITMAP(set
[2], 256);
529 } blocked_delegations
;
531 static int delegation_blocked(struct knfsd_fh
*fh
)
534 struct bloom_pair
*bd
= &blocked_delegations
;
536 if (bd
->entries
== 0)
538 if (seconds_since_boot() - bd
->swap_time
> 30) {
539 spin_lock(&state_lock
);
540 if (seconds_since_boot() - bd
->swap_time
> 30) {
541 bd
->entries
-= bd
->old_entries
;
542 bd
->old_entries
= bd
->entries
;
543 memset(bd
->set
[bd
->new], 0,
546 bd
->swap_time
= seconds_since_boot();
548 spin_unlock(&state_lock
);
550 hash
= arch_fast_hash(&fh
->fh_base
, fh
->fh_size
, 0);
551 if (test_bit(hash
&255, bd
->set
[0]) &&
552 test_bit((hash
>>8)&255, bd
->set
[0]) &&
553 test_bit((hash
>>16)&255, bd
->set
[0]))
556 if (test_bit(hash
&255, bd
->set
[1]) &&
557 test_bit((hash
>>8)&255, bd
->set
[1]) &&
558 test_bit((hash
>>16)&255, bd
->set
[1]))
564 static void block_delegations(struct knfsd_fh
*fh
)
567 struct bloom_pair
*bd
= &blocked_delegations
;
569 lockdep_assert_held(&state_lock
);
571 hash
= arch_fast_hash(&fh
->fh_base
, fh
->fh_size
, 0);
573 __set_bit(hash
&255, bd
->set
[bd
->new]);
574 __set_bit((hash
>>8)&255, bd
->set
[bd
->new]);
575 __set_bit((hash
>>16)&255, bd
->set
[bd
->new]);
576 if (bd
->entries
== 0)
577 bd
->swap_time
= seconds_since_boot();
581 static struct nfs4_delegation
*
582 alloc_init_deleg(struct nfs4_client
*clp
, struct nfs4_ol_stateid
*stp
, struct svc_fh
*current_fh
)
584 struct nfs4_delegation
*dp
;
587 dprintk("NFSD alloc_init_deleg\n");
588 n
= atomic_long_inc_return(&num_delegations
);
589 if (n
< 0 || n
> max_delegations
)
591 if (delegation_blocked(¤t_fh
->fh_handle
))
593 dp
= delegstateid(nfs4_alloc_stid(clp
, deleg_slab
));
597 * delegation seqid's are never incremented. The 4.1 special
598 * meaning of seqid 0 isn't meaningful, really, but let's avoid
599 * 0 anyway just for consistency and use 1:
601 dp
->dl_stid
.sc_stateid
.si_generation
= 1;
602 INIT_LIST_HEAD(&dp
->dl_perfile
);
603 INIT_LIST_HEAD(&dp
->dl_perclnt
);
604 INIT_LIST_HEAD(&dp
->dl_recall_lru
);
605 dp
->dl_type
= NFS4_OPEN_DELEGATE_READ
;
606 fh_copy_shallow(&dp
->dl_fh
, ¤t_fh
->fh_handle
);
607 INIT_WORK(&dp
->dl_recall
.cb_work
, nfsd4_run_cb_recall
);
610 atomic_long_dec(&num_delegations
);
614 static void remove_stid(struct nfs4_stid
*s
)
616 struct idr
*stateids
= &s
->sc_client
->cl_stateids
;
618 idr_remove(stateids
, s
->sc_stateid
.si_opaque
.so_id
);
621 static void nfs4_free_stid(struct kmem_cache
*slab
, struct nfs4_stid
*s
)
623 kmem_cache_free(slab
, s
);
627 nfs4_put_delegation(struct nfs4_delegation
*dp
)
629 if (atomic_dec_and_test(&dp
->dl_stid
.sc_count
)) {
631 put_nfs4_file(dp
->dl_file
);
632 remove_stid(&dp
->dl_stid
);
633 nfs4_free_stid(deleg_slab
, &dp
->dl_stid
);
634 atomic_long_dec(&num_delegations
);
638 static void nfs4_put_deleg_lease(struct nfs4_file
*fp
)
640 lockdep_assert_held(&state_lock
);
644 if (atomic_dec_and_test(&fp
->fi_delegees
)) {
645 vfs_setlease(fp
->fi_deleg_file
, F_UNLCK
, &fp
->fi_lease
);
647 fput(fp
->fi_deleg_file
);
648 fp
->fi_deleg_file
= NULL
;
652 static void unhash_stid(struct nfs4_stid
*s
)
658 hash_delegation_locked(struct nfs4_delegation
*dp
, struct nfs4_file
*fp
)
660 lockdep_assert_held(&state_lock
);
661 lockdep_assert_held(&fp
->fi_lock
);
663 dp
->dl_stid
.sc_type
= NFS4_DELEG_STID
;
664 list_add(&dp
->dl_perfile
, &fp
->fi_delegations
);
665 list_add(&dp
->dl_perclnt
, &dp
->dl_stid
.sc_client
->cl_delegations
);
669 unhash_delegation_locked(struct nfs4_delegation
*dp
)
671 struct nfs4_file
*fp
= dp
->dl_file
;
673 lockdep_assert_held(&state_lock
);
675 dp
->dl_stid
.sc_type
= NFS4_CLOSED_DELEG_STID
;
676 /* Ensure that deleg break won't try to requeue it */
678 spin_lock(&fp
->fi_lock
);
679 list_del_init(&dp
->dl_perclnt
);
680 list_del_init(&dp
->dl_recall_lru
);
681 list_del_init(&dp
->dl_perfile
);
682 spin_unlock(&fp
->fi_lock
);
684 nfs4_put_deleg_lease(fp
);
687 static void destroy_delegation(struct nfs4_delegation
*dp
)
689 spin_lock(&state_lock
);
690 unhash_delegation_locked(dp
);
691 spin_unlock(&state_lock
);
692 nfs4_put_delegation(dp
);
695 static void revoke_delegation(struct nfs4_delegation
*dp
)
697 struct nfs4_client
*clp
= dp
->dl_stid
.sc_client
;
699 WARN_ON(!list_empty(&dp
->dl_recall_lru
));
701 if (clp
->cl_minorversion
== 0)
702 nfs4_put_delegation(dp
);
704 dp
->dl_stid
.sc_type
= NFS4_REVOKED_DELEG_STID
;
705 spin_lock(&clp
->cl_lock
);
706 list_add(&dp
->dl_recall_lru
, &clp
->cl_revoked
);
707 spin_unlock(&clp
->cl_lock
);
715 static unsigned int clientid_hashval(u32 id
)
717 return id
& CLIENT_HASH_MASK
;
720 static unsigned int clientstr_hashval(const char *name
)
722 return opaque_hashval(name
, 8) & CLIENT_HASH_MASK
;
726 * We store the NONE, READ, WRITE, and BOTH bits separately in the
727 * st_{access,deny}_bmap field of the stateid, in order to track not
728 * only what share bits are currently in force, but also what
729 * combinations of share bits previous opens have used. This allows us
730 * to enforce the recommendation of rfc 3530 14.2.19 that the server
731 * return an error if the client attempt to downgrade to a combination
732 * of share bits not explicable by closing some of its previous opens.
734 * XXX: This enforcement is actually incomplete, since we don't keep
735 * track of access/deny bit combinations; so, e.g., we allow:
737 * OPEN allow read, deny write
738 * OPEN allow both, deny none
739 * DOWNGRADE allow read, deny none
741 * which we should reject.
744 bmap_to_share_mode(unsigned long bmap
) {
746 unsigned int access
= 0;
748 for (i
= 1; i
< 4; i
++) {
749 if (test_bit(i
, &bmap
))
755 /* set share access for a given stateid */
757 set_access(u32 access
, struct nfs4_ol_stateid
*stp
)
759 unsigned char mask
= 1 << access
;
761 WARN_ON_ONCE(access
> NFS4_SHARE_ACCESS_BOTH
);
762 stp
->st_access_bmap
|= mask
;
765 /* clear share access for a given stateid */
767 clear_access(u32 access
, struct nfs4_ol_stateid
*stp
)
769 unsigned char mask
= 1 << access
;
771 WARN_ON_ONCE(access
> NFS4_SHARE_ACCESS_BOTH
);
772 stp
->st_access_bmap
&= ~mask
;
775 /* test whether a given stateid has access */
777 test_access(u32 access
, struct nfs4_ol_stateid
*stp
)
779 unsigned char mask
= 1 << access
;
781 return (bool)(stp
->st_access_bmap
& mask
);
784 /* set share deny for a given stateid */
786 set_deny(u32 deny
, struct nfs4_ol_stateid
*stp
)
788 unsigned char mask
= 1 << deny
;
790 WARN_ON_ONCE(deny
> NFS4_SHARE_DENY_BOTH
);
791 stp
->st_deny_bmap
|= mask
;
794 /* clear share deny for a given stateid */
796 clear_deny(u32 deny
, struct nfs4_ol_stateid
*stp
)
798 unsigned char mask
= 1 << deny
;
800 WARN_ON_ONCE(deny
> NFS4_SHARE_DENY_BOTH
);
801 stp
->st_deny_bmap
&= ~mask
;
804 /* test whether a given stateid is denying specific access */
806 test_deny(u32 deny
, struct nfs4_ol_stateid
*stp
)
808 unsigned char mask
= 1 << deny
;
810 return (bool)(stp
->st_deny_bmap
& mask
);
813 static int nfs4_access_to_omode(u32 access
)
815 switch (access
& NFS4_SHARE_ACCESS_BOTH
) {
816 case NFS4_SHARE_ACCESS_READ
:
818 case NFS4_SHARE_ACCESS_WRITE
:
820 case NFS4_SHARE_ACCESS_BOTH
:
828 * A stateid that had a deny mode associated with it is being released
829 * or downgraded. Recalculate the deny mode on the file.
832 recalculate_deny_mode(struct nfs4_file
*fp
)
834 struct nfs4_ol_stateid
*stp
;
836 spin_lock(&fp
->fi_lock
);
837 fp
->fi_share_deny
= 0;
838 list_for_each_entry(stp
, &fp
->fi_stateids
, st_perfile
)
839 fp
->fi_share_deny
|= bmap_to_share_mode(stp
->st_deny_bmap
);
840 spin_unlock(&fp
->fi_lock
);
844 reset_union_bmap_deny(u32 deny
, struct nfs4_ol_stateid
*stp
)
849 for (i
= 1; i
< 4; i
++) {
850 if ((i
& deny
) != i
) {
856 /* Recalculate per-file deny mode if there was a change */
858 recalculate_deny_mode(stp
->st_file
);
861 /* release all access and file references for a given stateid */
863 release_all_access(struct nfs4_ol_stateid
*stp
)
866 struct nfs4_file
*fp
= stp
->st_file
;
868 if (fp
&& stp
->st_deny_bmap
!= 0)
869 recalculate_deny_mode(fp
);
871 for (i
= 1; i
< 4; i
++) {
872 if (test_access(i
, stp
))
873 nfs4_file_put_access(stp
->st_file
, i
);
874 clear_access(i
, stp
);
878 static void unhash_generic_stateid(struct nfs4_ol_stateid
*stp
)
880 struct nfs4_file
*fp
= stp
->st_file
;
882 spin_lock(&fp
->fi_lock
);
883 list_del(&stp
->st_perfile
);
884 spin_unlock(&fp
->fi_lock
);
885 list_del(&stp
->st_perstateowner
);
888 static void close_generic_stateid(struct nfs4_ol_stateid
*stp
)
890 release_all_access(stp
);
893 static void free_generic_stateid(struct nfs4_ol_stateid
*stp
)
896 put_nfs4_file(stp
->st_file
);
897 remove_stid(&stp
->st_stid
);
898 nfs4_free_stid(stateid_slab
, &stp
->st_stid
);
901 static void __release_lock_stateid(struct nfs4_ol_stateid
*stp
)
905 list_del(&stp
->st_locks
);
906 unhash_generic_stateid(stp
);
907 unhash_stid(&stp
->st_stid
);
908 file
= find_any_file(stp
->st_file
);
910 filp_close(file
, (fl_owner_t
)lockowner(stp
->st_stateowner
));
911 close_generic_stateid(stp
);
912 free_generic_stateid(stp
);
915 static void unhash_lockowner(struct nfs4_lockowner
*lo
)
917 struct nfs4_ol_stateid
*stp
;
919 list_del(&lo
->lo_owner
.so_strhash
);
920 while (!list_empty(&lo
->lo_owner
.so_stateids
)) {
921 stp
= list_first_entry(&lo
->lo_owner
.so_stateids
,
922 struct nfs4_ol_stateid
, st_perstateowner
);
923 __release_lock_stateid(stp
);
927 static void nfs4_free_lockowner(struct nfs4_lockowner
*lo
)
929 kfree(lo
->lo_owner
.so_owner
.data
);
930 kmem_cache_free(lockowner_slab
, lo
);
933 static void release_lockowner(struct nfs4_lockowner
*lo
)
935 unhash_lockowner(lo
);
936 nfs4_free_lockowner(lo
);
939 static void release_lockowner_if_empty(struct nfs4_lockowner
*lo
)
941 if (list_empty(&lo
->lo_owner
.so_stateids
))
942 release_lockowner(lo
);
945 static void release_lock_stateid(struct nfs4_ol_stateid
*stp
)
947 struct nfs4_lockowner
*lo
;
949 lo
= lockowner(stp
->st_stateowner
);
950 __release_lock_stateid(stp
);
951 release_lockowner_if_empty(lo
);
954 static void release_open_stateid_locks(struct nfs4_ol_stateid
*open_stp
)
956 struct nfs4_ol_stateid
*stp
;
958 while (!list_empty(&open_stp
->st_locks
)) {
959 stp
= list_entry(open_stp
->st_locks
.next
,
960 struct nfs4_ol_stateid
, st_locks
);
961 release_lock_stateid(stp
);
965 static void unhash_open_stateid(struct nfs4_ol_stateid
*stp
)
967 unhash_generic_stateid(stp
);
968 release_open_stateid_locks(stp
);
969 close_generic_stateid(stp
);
972 static void release_open_stateid(struct nfs4_ol_stateid
*stp
)
974 unhash_open_stateid(stp
);
975 free_generic_stateid(stp
);
978 static void unhash_openowner(struct nfs4_openowner
*oo
)
980 struct nfs4_ol_stateid
*stp
;
982 list_del(&oo
->oo_owner
.so_strhash
);
983 list_del(&oo
->oo_perclient
);
984 while (!list_empty(&oo
->oo_owner
.so_stateids
)) {
985 stp
= list_first_entry(&oo
->oo_owner
.so_stateids
,
986 struct nfs4_ol_stateid
, st_perstateowner
);
987 release_open_stateid(stp
);
991 static void release_last_closed_stateid(struct nfs4_openowner
*oo
)
993 struct nfs4_ol_stateid
*s
= oo
->oo_last_closed_stid
;
996 free_generic_stateid(s
);
997 oo
->oo_last_closed_stid
= NULL
;
1001 static void nfs4_free_openowner(struct nfs4_openowner
*oo
)
1003 kfree(oo
->oo_owner
.so_owner
.data
);
1004 kmem_cache_free(openowner_slab
, oo
);
1007 static void release_openowner(struct nfs4_openowner
*oo
)
1009 unhash_openowner(oo
);
1010 list_del(&oo
->oo_close_lru
);
1011 release_last_closed_stateid(oo
);
1012 nfs4_free_openowner(oo
);
1016 hash_sessionid(struct nfs4_sessionid
*sessionid
)
1018 struct nfsd4_sessionid
*sid
= (struct nfsd4_sessionid
*)sessionid
;
1020 return sid
->sequence
% SESSION_HASH_SIZE
;
1025 dump_sessionid(const char *fn
, struct nfs4_sessionid
*sessionid
)
1027 u32
*ptr
= (u32
*)(&sessionid
->data
[0]);
1028 dprintk("%s: %u:%u:%u:%u\n", fn
, ptr
[0], ptr
[1], ptr
[2], ptr
[3]);
1032 dump_sessionid(const char *fn
, struct nfs4_sessionid
*sessionid
)
1038 * Bump the seqid on cstate->replay_owner, and clear replay_owner if it
1039 * won't be used for replay.
1041 void nfsd4_bump_seqid(struct nfsd4_compound_state
*cstate
, __be32 nfserr
)
1043 struct nfs4_stateowner
*so
= cstate
->replay_owner
;
1045 if (nfserr
== nfserr_replay_me
)
1048 if (!seqid_mutating_err(ntohl(nfserr
))) {
1049 cstate
->replay_owner
= NULL
;
1054 if (so
->so_is_open_owner
)
1055 release_last_closed_stateid(openowner(so
));
1061 gen_sessionid(struct nfsd4_session
*ses
)
1063 struct nfs4_client
*clp
= ses
->se_client
;
1064 struct nfsd4_sessionid
*sid
;
1066 sid
= (struct nfsd4_sessionid
*)ses
->se_sessionid
.data
;
1067 sid
->clientid
= clp
->cl_clientid
;
1068 sid
->sequence
= current_sessionid
++;
1073 * The protocol defines ca_maxresponssize_cached to include the size of
1074 * the rpc header, but all we need to cache is the data starting after
1075 * the end of the initial SEQUENCE operation--the rest we regenerate
1076 * each time. Therefore we can advertise a ca_maxresponssize_cached
1077 * value that is the number of bytes in our cache plus a few additional
1078 * bytes. In order to stay on the safe side, and not promise more than
1079 * we can cache, those additional bytes must be the minimum possible: 24
1080 * bytes of rpc header (xid through accept state, with AUTH_NULL
1081 * verifier), 12 for the compound header (with zero-length tag), and 44
1082 * for the SEQUENCE op response:
1084 #define NFSD_MIN_HDR_SEQ_SZ (24 + 12 + 44)
1087 free_session_slots(struct nfsd4_session
*ses
)
1091 for (i
= 0; i
< ses
->se_fchannel
.maxreqs
; i
++)
1092 kfree(ses
->se_slots
[i
]);
1096 * We don't actually need to cache the rpc and session headers, so we
1097 * can allocate a little less for each slot:
1099 static inline u32
slot_bytes(struct nfsd4_channel_attrs
*ca
)
1103 if (ca
->maxresp_cached
< NFSD_MIN_HDR_SEQ_SZ
)
1106 size
= ca
->maxresp_cached
- NFSD_MIN_HDR_SEQ_SZ
;
1107 return size
+ sizeof(struct nfsd4_slot
);
1111 * XXX: If we run out of reserved DRC memory we could (up to a point)
1112 * re-negotiate active sessions and reduce their slot usage to make
1113 * room for new connections. For now we just fail the create session.
1115 static u32
nfsd4_get_drc_mem(struct nfsd4_channel_attrs
*ca
)
1117 u32 slotsize
= slot_bytes(ca
);
1118 u32 num
= ca
->maxreqs
;
1121 spin_lock(&nfsd_drc_lock
);
1122 avail
= min((unsigned long)NFSD_MAX_MEM_PER_SESSION
,
1123 nfsd_drc_max_mem
- nfsd_drc_mem_used
);
1124 num
= min_t(int, num
, avail
/ slotsize
);
1125 nfsd_drc_mem_used
+= num
* slotsize
;
1126 spin_unlock(&nfsd_drc_lock
);
1131 static void nfsd4_put_drc_mem(struct nfsd4_channel_attrs
*ca
)
1133 int slotsize
= slot_bytes(ca
);
1135 spin_lock(&nfsd_drc_lock
);
1136 nfsd_drc_mem_used
-= slotsize
* ca
->maxreqs
;
1137 spin_unlock(&nfsd_drc_lock
);
1140 static struct nfsd4_session
*alloc_session(struct nfsd4_channel_attrs
*fattrs
,
1141 struct nfsd4_channel_attrs
*battrs
)
1143 int numslots
= fattrs
->maxreqs
;
1144 int slotsize
= slot_bytes(fattrs
);
1145 struct nfsd4_session
*new;
1148 BUILD_BUG_ON(NFSD_MAX_SLOTS_PER_SESSION
* sizeof(struct nfsd4_slot
*)
1149 + sizeof(struct nfsd4_session
) > PAGE_SIZE
);
1150 mem
= numslots
* sizeof(struct nfsd4_slot
*);
1152 new = kzalloc(sizeof(*new) + mem
, GFP_KERNEL
);
1155 /* allocate each struct nfsd4_slot and data cache in one piece */
1156 for (i
= 0; i
< numslots
; i
++) {
1157 new->se_slots
[i
] = kzalloc(slotsize
, GFP_KERNEL
);
1158 if (!new->se_slots
[i
])
1162 memcpy(&new->se_fchannel
, fattrs
, sizeof(struct nfsd4_channel_attrs
));
1163 memcpy(&new->se_bchannel
, battrs
, sizeof(struct nfsd4_channel_attrs
));
1168 kfree(new->se_slots
[i
]);
1173 static void free_conn(struct nfsd4_conn
*c
)
1175 svc_xprt_put(c
->cn_xprt
);
1179 static void nfsd4_conn_lost(struct svc_xpt_user
*u
)
1181 struct nfsd4_conn
*c
= container_of(u
, struct nfsd4_conn
, cn_xpt_user
);
1182 struct nfs4_client
*clp
= c
->cn_session
->se_client
;
1184 spin_lock(&clp
->cl_lock
);
1185 if (!list_empty(&c
->cn_persession
)) {
1186 list_del(&c
->cn_persession
);
1189 nfsd4_probe_callback(clp
);
1190 spin_unlock(&clp
->cl_lock
);
1193 static struct nfsd4_conn
*alloc_conn(struct svc_rqst
*rqstp
, u32 flags
)
1195 struct nfsd4_conn
*conn
;
1197 conn
= kmalloc(sizeof(struct nfsd4_conn
), GFP_KERNEL
);
1200 svc_xprt_get(rqstp
->rq_xprt
);
1201 conn
->cn_xprt
= rqstp
->rq_xprt
;
1202 conn
->cn_flags
= flags
;
1203 INIT_LIST_HEAD(&conn
->cn_xpt_user
.list
);
1207 static void __nfsd4_hash_conn(struct nfsd4_conn
*conn
, struct nfsd4_session
*ses
)
1209 conn
->cn_session
= ses
;
1210 list_add(&conn
->cn_persession
, &ses
->se_conns
);
1213 static void nfsd4_hash_conn(struct nfsd4_conn
*conn
, struct nfsd4_session
*ses
)
1215 struct nfs4_client
*clp
= ses
->se_client
;
1217 spin_lock(&clp
->cl_lock
);
1218 __nfsd4_hash_conn(conn
, ses
);
1219 spin_unlock(&clp
->cl_lock
);
1222 static int nfsd4_register_conn(struct nfsd4_conn
*conn
)
1224 conn
->cn_xpt_user
.callback
= nfsd4_conn_lost
;
1225 return register_xpt_user(conn
->cn_xprt
, &conn
->cn_xpt_user
);
1228 static void nfsd4_init_conn(struct svc_rqst
*rqstp
, struct nfsd4_conn
*conn
, struct nfsd4_session
*ses
)
1232 nfsd4_hash_conn(conn
, ses
);
1233 ret
= nfsd4_register_conn(conn
);
1235 /* oops; xprt is already down: */
1236 nfsd4_conn_lost(&conn
->cn_xpt_user
);
1237 /* We may have gained or lost a callback channel: */
1238 nfsd4_probe_callback_sync(ses
->se_client
);
1241 static struct nfsd4_conn
*alloc_conn_from_crses(struct svc_rqst
*rqstp
, struct nfsd4_create_session
*cses
)
1243 u32 dir
= NFS4_CDFC4_FORE
;
1245 if (cses
->flags
& SESSION4_BACK_CHAN
)
1246 dir
|= NFS4_CDFC4_BACK
;
1247 return alloc_conn(rqstp
, dir
);
1250 /* must be called under client_lock */
1251 static void nfsd4_del_conns(struct nfsd4_session
*s
)
1253 struct nfs4_client
*clp
= s
->se_client
;
1254 struct nfsd4_conn
*c
;
1256 spin_lock(&clp
->cl_lock
);
1257 while (!list_empty(&s
->se_conns
)) {
1258 c
= list_first_entry(&s
->se_conns
, struct nfsd4_conn
, cn_persession
);
1259 list_del_init(&c
->cn_persession
);
1260 spin_unlock(&clp
->cl_lock
);
1262 unregister_xpt_user(c
->cn_xprt
, &c
->cn_xpt_user
);
1265 spin_lock(&clp
->cl_lock
);
1267 spin_unlock(&clp
->cl_lock
);
1270 static void __free_session(struct nfsd4_session
*ses
)
1272 free_session_slots(ses
);
1276 static void free_session(struct nfsd4_session
*ses
)
1278 struct nfsd_net
*nn
= net_generic(ses
->se_client
->net
, nfsd_net_id
);
1280 lockdep_assert_held(&nn
->client_lock
);
1281 nfsd4_del_conns(ses
);
1282 nfsd4_put_drc_mem(&ses
->se_fchannel
);
1283 __free_session(ses
);
1286 static void init_session(struct svc_rqst
*rqstp
, struct nfsd4_session
*new, struct nfs4_client
*clp
, struct nfsd4_create_session
*cses
)
1289 struct nfsd_net
*nn
= net_generic(SVC_NET(rqstp
), nfsd_net_id
);
1291 new->se_client
= clp
;
1294 INIT_LIST_HEAD(&new->se_conns
);
1296 new->se_cb_seq_nr
= 1;
1297 new->se_flags
= cses
->flags
;
1298 new->se_cb_prog
= cses
->callback_prog
;
1299 new->se_cb_sec
= cses
->cb_sec
;
1300 atomic_set(&new->se_ref
, 0);
1301 idx
= hash_sessionid(&new->se_sessionid
);
1302 spin_lock(&nn
->client_lock
);
1303 list_add(&new->se_hash
, &nn
->sessionid_hashtbl
[idx
]);
1304 spin_lock(&clp
->cl_lock
);
1305 list_add(&new->se_perclnt
, &clp
->cl_sessions
);
1306 spin_unlock(&clp
->cl_lock
);
1307 spin_unlock(&nn
->client_lock
);
1309 if (cses
->flags
& SESSION4_BACK_CHAN
) {
1310 struct sockaddr
*sa
= svc_addr(rqstp
);
1312 * This is a little silly; with sessions there's no real
1313 * use for the callback address. Use the peer address
1314 * as a reasonable default for now, but consider fixing
1315 * the rpc client not to require an address in the
1318 rpc_copy_addr((struct sockaddr
*)&clp
->cl_cb_conn
.cb_addr
, sa
);
1319 clp
->cl_cb_conn
.cb_addrlen
= svc_addr_len(sa
);
1323 /* caller must hold client_lock */
1324 static struct nfsd4_session
*
1325 __find_in_sessionid_hashtbl(struct nfs4_sessionid
*sessionid
, struct net
*net
)
1327 struct nfsd4_session
*elem
;
1329 struct nfsd_net
*nn
= net_generic(net
, nfsd_net_id
);
1331 dump_sessionid(__func__
, sessionid
);
1332 idx
= hash_sessionid(sessionid
);
1333 /* Search in the appropriate list */
1334 list_for_each_entry(elem
, &nn
->sessionid_hashtbl
[idx
], se_hash
) {
1335 if (!memcmp(elem
->se_sessionid
.data
, sessionid
->data
,
1336 NFS4_MAX_SESSIONID_LEN
)) {
1341 dprintk("%s: session not found\n", __func__
);
1345 static struct nfsd4_session
*
1346 find_in_sessionid_hashtbl(struct nfs4_sessionid
*sessionid
, struct net
*net
,
1349 struct nfsd4_session
*session
;
1350 __be32 status
= nfserr_badsession
;
1352 session
= __find_in_sessionid_hashtbl(sessionid
, net
);
1355 status
= nfsd4_get_session_locked(session
);
1363 /* caller must hold client_lock */
1365 unhash_session(struct nfsd4_session
*ses
)
1367 list_del(&ses
->se_hash
);
1368 spin_lock(&ses
->se_client
->cl_lock
);
1369 list_del(&ses
->se_perclnt
);
1370 spin_unlock(&ses
->se_client
->cl_lock
);
1373 /* SETCLIENTID and SETCLIENTID_CONFIRM Helper functions */
1375 STALE_CLIENTID(clientid_t
*clid
, struct nfsd_net
*nn
)
1377 if (clid
->cl_boot
== nn
->boot_time
)
1379 dprintk("NFSD stale clientid (%08x/%08x) boot_time %08lx\n",
1380 clid
->cl_boot
, clid
->cl_id
, nn
->boot_time
);
1385 * XXX Should we use a slab cache ?
1386 * This type of memory management is somewhat inefficient, but we use it
1387 * anyway since SETCLIENTID is not a common operation.
1389 static struct nfs4_client
*alloc_client(struct xdr_netobj name
)
1391 struct nfs4_client
*clp
;
1393 clp
= kzalloc(sizeof(struct nfs4_client
), GFP_KERNEL
);
1396 clp
->cl_name
.data
= kmemdup(name
.data
, name
.len
, GFP_KERNEL
);
1397 if (clp
->cl_name
.data
== NULL
) {
1401 clp
->cl_name
.len
= name
.len
;
1402 INIT_LIST_HEAD(&clp
->cl_sessions
);
1403 idr_init(&clp
->cl_stateids
);
1404 atomic_set(&clp
->cl_refcount
, 0);
1405 clp
->cl_cb_state
= NFSD4_CB_UNKNOWN
;
1406 INIT_LIST_HEAD(&clp
->cl_idhash
);
1407 INIT_LIST_HEAD(&clp
->cl_openowners
);
1408 INIT_LIST_HEAD(&clp
->cl_delegations
);
1409 INIT_LIST_HEAD(&clp
->cl_lru
);
1410 INIT_LIST_HEAD(&clp
->cl_callbacks
);
1411 INIT_LIST_HEAD(&clp
->cl_revoked
);
1412 spin_lock_init(&clp
->cl_lock
);
1413 rpc_init_wait_queue(&clp
->cl_cb_waitq
, "Backchannel slot table");
1418 free_client(struct nfs4_client
*clp
)
1420 struct nfsd_net __maybe_unused
*nn
= net_generic(clp
->net
, nfsd_net_id
);
1422 lockdep_assert_held(&nn
->client_lock
);
1423 while (!list_empty(&clp
->cl_sessions
)) {
1424 struct nfsd4_session
*ses
;
1425 ses
= list_entry(clp
->cl_sessions
.next
, struct nfsd4_session
,
1427 list_del(&ses
->se_perclnt
);
1428 WARN_ON_ONCE(atomic_read(&ses
->se_ref
));
1431 rpc_destroy_wait_queue(&clp
->cl_cb_waitq
);
1432 free_svc_cred(&clp
->cl_cred
);
1433 kfree(clp
->cl_name
.data
);
1434 idr_destroy(&clp
->cl_stateids
);
1438 /* must be called under the client_lock */
1440 unhash_client_locked(struct nfs4_client
*clp
)
1442 struct nfsd4_session
*ses
;
1444 list_del(&clp
->cl_lru
);
1445 spin_lock(&clp
->cl_lock
);
1446 list_for_each_entry(ses
, &clp
->cl_sessions
, se_perclnt
)
1447 list_del_init(&ses
->se_hash
);
1448 spin_unlock(&clp
->cl_lock
);
1452 destroy_client(struct nfs4_client
*clp
)
1454 struct nfs4_openowner
*oo
;
1455 struct nfs4_delegation
*dp
;
1456 struct list_head reaplist
;
1457 struct nfsd_net
*nn
= net_generic(clp
->net
, nfsd_net_id
);
1459 INIT_LIST_HEAD(&reaplist
);
1460 spin_lock(&state_lock
);
1461 while (!list_empty(&clp
->cl_delegations
)) {
1462 dp
= list_entry(clp
->cl_delegations
.next
, struct nfs4_delegation
, dl_perclnt
);
1463 unhash_delegation_locked(dp
);
1464 list_add(&dp
->dl_recall_lru
, &reaplist
);
1466 spin_unlock(&state_lock
);
1467 while (!list_empty(&reaplist
)) {
1468 dp
= list_entry(reaplist
.next
, struct nfs4_delegation
, dl_recall_lru
);
1469 list_del_init(&dp
->dl_recall_lru
);
1470 nfs4_put_delegation(dp
);
1472 while (!list_empty(&clp
->cl_revoked
)) {
1473 dp
= list_entry(reaplist
.next
, struct nfs4_delegation
, dl_recall_lru
);
1474 list_del_init(&dp
->dl_recall_lru
);
1475 nfs4_put_delegation(dp
);
1477 while (!list_empty(&clp
->cl_openowners
)) {
1478 oo
= list_entry(clp
->cl_openowners
.next
, struct nfs4_openowner
, oo_perclient
);
1479 release_openowner(oo
);
1481 nfsd4_shutdown_callback(clp
);
1482 if (clp
->cl_cb_conn
.cb_xprt
)
1483 svc_xprt_put(clp
->cl_cb_conn
.cb_xprt
);
1484 list_del(&clp
->cl_idhash
);
1485 if (test_bit(NFSD4_CLIENT_CONFIRMED
, &clp
->cl_flags
))
1486 rb_erase(&clp
->cl_namenode
, &nn
->conf_name_tree
);
1488 rb_erase(&clp
->cl_namenode
, &nn
->unconf_name_tree
);
1489 spin_lock(&nn
->client_lock
);
1490 unhash_client_locked(clp
);
1491 WARN_ON_ONCE(atomic_read(&clp
->cl_refcount
));
1493 spin_unlock(&nn
->client_lock
);
1496 static void expire_client(struct nfs4_client
*clp
)
1498 nfsd4_client_record_remove(clp
);
1499 destroy_client(clp
);
1502 static void copy_verf(struct nfs4_client
*target
, nfs4_verifier
*source
)
1504 memcpy(target
->cl_verifier
.data
, source
->data
,
1505 sizeof(target
->cl_verifier
.data
));
1508 static void copy_clid(struct nfs4_client
*target
, struct nfs4_client
*source
)
1510 target
->cl_clientid
.cl_boot
= source
->cl_clientid
.cl_boot
;
1511 target
->cl_clientid
.cl_id
= source
->cl_clientid
.cl_id
;
1514 static int copy_cred(struct svc_cred
*target
, struct svc_cred
*source
)
1516 if (source
->cr_principal
) {
1517 target
->cr_principal
=
1518 kstrdup(source
->cr_principal
, GFP_KERNEL
);
1519 if (target
->cr_principal
== NULL
)
1522 target
->cr_principal
= NULL
;
1523 target
->cr_flavor
= source
->cr_flavor
;
1524 target
->cr_uid
= source
->cr_uid
;
1525 target
->cr_gid
= source
->cr_gid
;
1526 target
->cr_group_info
= source
->cr_group_info
;
1527 get_group_info(target
->cr_group_info
);
1528 target
->cr_gss_mech
= source
->cr_gss_mech
;
1529 if (source
->cr_gss_mech
)
1530 gss_mech_get(source
->cr_gss_mech
);
1535 compare_blob(const struct xdr_netobj
*o1
, const struct xdr_netobj
*o2
)
1539 res
= o1
->len
- o2
->len
;
1542 return (long long)memcmp(o1
->data
, o2
->data
, o1
->len
);
1545 static int same_name(const char *n1
, const char *n2
)
1547 return 0 == memcmp(n1
, n2
, HEXDIR_LEN
);
1551 same_verf(nfs4_verifier
*v1
, nfs4_verifier
*v2
)
1553 return 0 == memcmp(v1
->data
, v2
->data
, sizeof(v1
->data
));
1557 same_clid(clientid_t
*cl1
, clientid_t
*cl2
)
1559 return (cl1
->cl_boot
== cl2
->cl_boot
) && (cl1
->cl_id
== cl2
->cl_id
);
1562 static bool groups_equal(struct group_info
*g1
, struct group_info
*g2
)
1566 if (g1
->ngroups
!= g2
->ngroups
)
1568 for (i
=0; i
<g1
->ngroups
; i
++)
1569 if (!gid_eq(GROUP_AT(g1
, i
), GROUP_AT(g2
, i
)))
1575 * RFC 3530 language requires clid_inuse be returned when the
1576 * "principal" associated with a requests differs from that previously
1577 * used. We use uid, gid's, and gss principal string as our best
1578 * approximation. We also don't want to allow non-gss use of a client
1579 * established using gss: in theory cr_principal should catch that
1580 * change, but in practice cr_principal can be null even in the gss case
1581 * since gssd doesn't always pass down a principal string.
1583 static bool is_gss_cred(struct svc_cred
*cr
)
1585 /* Is cr_flavor one of the gss "pseudoflavors"?: */
1586 return (cr
->cr_flavor
> RPC_AUTH_MAXFLAVOR
);
1591 same_creds(struct svc_cred
*cr1
, struct svc_cred
*cr2
)
1593 if ((is_gss_cred(cr1
) != is_gss_cred(cr2
))
1594 || (!uid_eq(cr1
->cr_uid
, cr2
->cr_uid
))
1595 || (!gid_eq(cr1
->cr_gid
, cr2
->cr_gid
))
1596 || !groups_equal(cr1
->cr_group_info
, cr2
->cr_group_info
))
1598 if (cr1
->cr_principal
== cr2
->cr_principal
)
1600 if (!cr1
->cr_principal
|| !cr2
->cr_principal
)
1602 return 0 == strcmp(cr1
->cr_principal
, cr2
->cr_principal
);
1605 static bool svc_rqst_integrity_protected(struct svc_rqst
*rqstp
)
1607 struct svc_cred
*cr
= &rqstp
->rq_cred
;
1610 if (!cr
->cr_gss_mech
)
1612 service
= gss_pseudoflavor_to_service(cr
->cr_gss_mech
, cr
->cr_flavor
);
1613 return service
== RPC_GSS_SVC_INTEGRITY
||
1614 service
== RPC_GSS_SVC_PRIVACY
;
1617 static bool mach_creds_match(struct nfs4_client
*cl
, struct svc_rqst
*rqstp
)
1619 struct svc_cred
*cr
= &rqstp
->rq_cred
;
1621 if (!cl
->cl_mach_cred
)
1623 if (cl
->cl_cred
.cr_gss_mech
!= cr
->cr_gss_mech
)
1625 if (!svc_rqst_integrity_protected(rqstp
))
1627 if (!cr
->cr_principal
)
1629 return 0 == strcmp(cl
->cl_cred
.cr_principal
, cr
->cr_principal
);
1632 static void gen_clid(struct nfs4_client
*clp
, struct nfsd_net
*nn
)
1634 static u32 current_clientid
= 1;
1636 clp
->cl_clientid
.cl_boot
= nn
->boot_time
;
1637 clp
->cl_clientid
.cl_id
= current_clientid
++;
1640 static void gen_confirm(struct nfs4_client
*clp
)
1646 * This is opaque to client, so no need to byte-swap. Use
1647 * __force to keep sparse happy
1649 verf
[0] = (__force __be32
)get_seconds();
1650 verf
[1] = (__force __be32
)i
++;
1651 memcpy(clp
->cl_confirm
.data
, verf
, sizeof(clp
->cl_confirm
.data
));
1654 static struct nfs4_stid
*find_stateid(struct nfs4_client
*cl
, stateid_t
*t
)
1656 struct nfs4_stid
*ret
;
1658 ret
= idr_find(&cl
->cl_stateids
, t
->si_opaque
.so_id
);
1659 if (!ret
|| !ret
->sc_type
)
1664 static struct nfs4_stid
*find_stateid_by_type(struct nfs4_client
*cl
, stateid_t
*t
, char typemask
)
1666 struct nfs4_stid
*s
;
1668 s
= find_stateid(cl
, t
);
1671 if (typemask
& s
->sc_type
)
1676 static struct nfs4_client
*create_client(struct xdr_netobj name
,
1677 struct svc_rqst
*rqstp
, nfs4_verifier
*verf
)
1679 struct nfs4_client
*clp
;
1680 struct sockaddr
*sa
= svc_addr(rqstp
);
1682 struct net
*net
= SVC_NET(rqstp
);
1683 struct nfsd_net
*nn
= net_generic(net
, nfsd_net_id
);
1685 clp
= alloc_client(name
);
1689 ret
= copy_cred(&clp
->cl_cred
, &rqstp
->rq_cred
);
1691 spin_lock(&nn
->client_lock
);
1693 spin_unlock(&nn
->client_lock
);
1696 INIT_WORK(&clp
->cl_cb_null
.cb_work
, nfsd4_run_cb_null
);
1697 clp
->cl_time
= get_seconds();
1698 clear_bit(0, &clp
->cl_cb_slot_busy
);
1699 copy_verf(clp
, verf
);
1700 rpc_copy_addr((struct sockaddr
*) &clp
->cl_addr
, sa
);
1702 clp
->cl_cb_session
= NULL
;
1708 add_clp_to_name_tree(struct nfs4_client
*new_clp
, struct rb_root
*root
)
1710 struct rb_node
**new = &(root
->rb_node
), *parent
= NULL
;
1711 struct nfs4_client
*clp
;
1714 clp
= rb_entry(*new, struct nfs4_client
, cl_namenode
);
1717 if (compare_blob(&clp
->cl_name
, &new_clp
->cl_name
) > 0)
1718 new = &((*new)->rb_left
);
1720 new = &((*new)->rb_right
);
1723 rb_link_node(&new_clp
->cl_namenode
, parent
, new);
1724 rb_insert_color(&new_clp
->cl_namenode
, root
);
1727 static struct nfs4_client
*
1728 find_clp_in_name_tree(struct xdr_netobj
*name
, struct rb_root
*root
)
1731 struct rb_node
*node
= root
->rb_node
;
1732 struct nfs4_client
*clp
;
1735 clp
= rb_entry(node
, struct nfs4_client
, cl_namenode
);
1736 cmp
= compare_blob(&clp
->cl_name
, name
);
1738 node
= node
->rb_left
;
1740 node
= node
->rb_right
;
1748 add_to_unconfirmed(struct nfs4_client
*clp
)
1750 unsigned int idhashval
;
1751 struct nfsd_net
*nn
= net_generic(clp
->net
, nfsd_net_id
);
1753 clear_bit(NFSD4_CLIENT_CONFIRMED
, &clp
->cl_flags
);
1754 add_clp_to_name_tree(clp
, &nn
->unconf_name_tree
);
1755 idhashval
= clientid_hashval(clp
->cl_clientid
.cl_id
);
1756 list_add(&clp
->cl_idhash
, &nn
->unconf_id_hashtbl
[idhashval
]);
1761 move_to_confirmed(struct nfs4_client
*clp
)
1763 unsigned int idhashval
= clientid_hashval(clp
->cl_clientid
.cl_id
);
1764 struct nfsd_net
*nn
= net_generic(clp
->net
, nfsd_net_id
);
1766 dprintk("NFSD: move_to_confirm nfs4_client %p\n", clp
);
1767 list_move(&clp
->cl_idhash
, &nn
->conf_id_hashtbl
[idhashval
]);
1768 rb_erase(&clp
->cl_namenode
, &nn
->unconf_name_tree
);
1769 add_clp_to_name_tree(clp
, &nn
->conf_name_tree
);
1770 set_bit(NFSD4_CLIENT_CONFIRMED
, &clp
->cl_flags
);
1774 static struct nfs4_client
*
1775 find_client_in_id_table(struct list_head
*tbl
, clientid_t
*clid
, bool sessions
)
1777 struct nfs4_client
*clp
;
1778 unsigned int idhashval
= clientid_hashval(clid
->cl_id
);
1780 list_for_each_entry(clp
, &tbl
[idhashval
], cl_idhash
) {
1781 if (same_clid(&clp
->cl_clientid
, clid
)) {
1782 if ((bool)clp
->cl_minorversion
!= sessions
)
1791 static struct nfs4_client
*
1792 find_confirmed_client(clientid_t
*clid
, bool sessions
, struct nfsd_net
*nn
)
1794 struct list_head
*tbl
= nn
->conf_id_hashtbl
;
1796 return find_client_in_id_table(tbl
, clid
, sessions
);
1799 static struct nfs4_client
*
1800 find_unconfirmed_client(clientid_t
*clid
, bool sessions
, struct nfsd_net
*nn
)
1802 struct list_head
*tbl
= nn
->unconf_id_hashtbl
;
1804 return find_client_in_id_table(tbl
, clid
, sessions
);
1807 static bool clp_used_exchangeid(struct nfs4_client
*clp
)
1809 return clp
->cl_exchange_flags
!= 0;
1812 static struct nfs4_client
*
1813 find_confirmed_client_by_name(struct xdr_netobj
*name
, struct nfsd_net
*nn
)
1815 return find_clp_in_name_tree(name
, &nn
->conf_name_tree
);
1818 static struct nfs4_client
*
1819 find_unconfirmed_client_by_name(struct xdr_netobj
*name
, struct nfsd_net
*nn
)
1821 return find_clp_in_name_tree(name
, &nn
->unconf_name_tree
);
1825 gen_callback(struct nfs4_client
*clp
, struct nfsd4_setclientid
*se
, struct svc_rqst
*rqstp
)
1827 struct nfs4_cb_conn
*conn
= &clp
->cl_cb_conn
;
1828 struct sockaddr
*sa
= svc_addr(rqstp
);
1829 u32 scopeid
= rpc_get_scope_id(sa
);
1830 unsigned short expected_family
;
1832 /* Currently, we only support tcp and tcp6 for the callback channel */
1833 if (se
->se_callback_netid_len
== 3 &&
1834 !memcmp(se
->se_callback_netid_val
, "tcp", 3))
1835 expected_family
= AF_INET
;
1836 else if (se
->se_callback_netid_len
== 4 &&
1837 !memcmp(se
->se_callback_netid_val
, "tcp6", 4))
1838 expected_family
= AF_INET6
;
1842 conn
->cb_addrlen
= rpc_uaddr2sockaddr(clp
->net
, se
->se_callback_addr_val
,
1843 se
->se_callback_addr_len
,
1844 (struct sockaddr
*)&conn
->cb_addr
,
1845 sizeof(conn
->cb_addr
));
1847 if (!conn
->cb_addrlen
|| conn
->cb_addr
.ss_family
!= expected_family
)
1850 if (conn
->cb_addr
.ss_family
== AF_INET6
)
1851 ((struct sockaddr_in6
*)&conn
->cb_addr
)->sin6_scope_id
= scopeid
;
1853 conn
->cb_prog
= se
->se_callback_prog
;
1854 conn
->cb_ident
= se
->se_callback_ident
;
1855 memcpy(&conn
->cb_saddr
, &rqstp
->rq_daddr
, rqstp
->rq_daddrlen
);
1858 conn
->cb_addr
.ss_family
= AF_UNSPEC
;
1859 conn
->cb_addrlen
= 0;
1860 dprintk(KERN_INFO
"NFSD: this client (clientid %08x/%08x) "
1861 "will not receive delegations\n",
1862 clp
->cl_clientid
.cl_boot
, clp
->cl_clientid
.cl_id
);
1868 * Cache a reply. nfsd4_check_resp_size() has bounded the cache size.
1871 nfsd4_store_cache_entry(struct nfsd4_compoundres
*resp
)
1873 struct xdr_buf
*buf
= resp
->xdr
.buf
;
1874 struct nfsd4_slot
*slot
= resp
->cstate
.slot
;
1877 dprintk("--> %s slot %p\n", __func__
, slot
);
1879 slot
->sl_opcnt
= resp
->opcnt
;
1880 slot
->sl_status
= resp
->cstate
.status
;
1882 slot
->sl_flags
|= NFSD4_SLOT_INITIALIZED
;
1883 if (nfsd4_not_cached(resp
)) {
1884 slot
->sl_datalen
= 0;
1887 base
= resp
->cstate
.data_offset
;
1888 slot
->sl_datalen
= buf
->len
- base
;
1889 if (read_bytes_from_xdr_buf(buf
, base
, slot
->sl_data
, slot
->sl_datalen
))
1890 WARN("%s: sessions DRC could not cache compound\n", __func__
);
1895 * Encode the replay sequence operation from the slot values.
1896 * If cachethis is FALSE encode the uncached rep error on the next
1897 * operation which sets resp->p and increments resp->opcnt for
1898 * nfs4svc_encode_compoundres.
1902 nfsd4_enc_sequence_replay(struct nfsd4_compoundargs
*args
,
1903 struct nfsd4_compoundres
*resp
)
1905 struct nfsd4_op
*op
;
1906 struct nfsd4_slot
*slot
= resp
->cstate
.slot
;
1908 /* Encode the replayed sequence operation */
1909 op
= &args
->ops
[resp
->opcnt
- 1];
1910 nfsd4_encode_operation(resp
, op
);
1912 /* Return nfserr_retry_uncached_rep in next operation. */
1913 if (args
->opcnt
> 1 && !(slot
->sl_flags
& NFSD4_SLOT_CACHETHIS
)) {
1914 op
= &args
->ops
[resp
->opcnt
++];
1915 op
->status
= nfserr_retry_uncached_rep
;
1916 nfsd4_encode_operation(resp
, op
);
1922 * The sequence operation is not cached because we can use the slot and
1926 nfsd4_replay_cache_entry(struct nfsd4_compoundres
*resp
,
1927 struct nfsd4_sequence
*seq
)
1929 struct nfsd4_slot
*slot
= resp
->cstate
.slot
;
1930 struct xdr_stream
*xdr
= &resp
->xdr
;
1934 dprintk("--> %s slot %p\n", __func__
, slot
);
1936 status
= nfsd4_enc_sequence_replay(resp
->rqstp
->rq_argp
, resp
);
1940 p
= xdr_reserve_space(xdr
, slot
->sl_datalen
);
1943 return nfserr_serverfault
;
1945 xdr_encode_opaque_fixed(p
, slot
->sl_data
, slot
->sl_datalen
);
1946 xdr_commit_encode(xdr
);
1948 resp
->opcnt
= slot
->sl_opcnt
;
1949 return slot
->sl_status
;
1953 * Set the exchange_id flags returned by the server.
1956 nfsd4_set_ex_flags(struct nfs4_client
*new, struct nfsd4_exchange_id
*clid
)
1958 /* pNFS is not supported */
1959 new->cl_exchange_flags
|= EXCHGID4_FLAG_USE_NON_PNFS
;
1961 /* Referrals are supported, Migration is not. */
1962 new->cl_exchange_flags
|= EXCHGID4_FLAG_SUPP_MOVED_REFER
;
1964 /* set the wire flags to return to client. */
1965 clid
->flags
= new->cl_exchange_flags
;
1968 static bool client_has_state(struct nfs4_client
*clp
)
1971 * Note clp->cl_openowners check isn't quite right: there's no
1972 * need to count owners without stateid's.
1974 * Also note we should probably be using this in 4.0 case too.
1976 return !list_empty(&clp
->cl_openowners
)
1977 || !list_empty(&clp
->cl_delegations
)
1978 || !list_empty(&clp
->cl_sessions
);
1982 nfsd4_exchange_id(struct svc_rqst
*rqstp
,
1983 struct nfsd4_compound_state
*cstate
,
1984 struct nfsd4_exchange_id
*exid
)
1986 struct nfs4_client
*unconf
, *conf
, *new;
1988 char addr_str
[INET6_ADDRSTRLEN
];
1989 nfs4_verifier verf
= exid
->verifier
;
1990 struct sockaddr
*sa
= svc_addr(rqstp
);
1991 bool update
= exid
->flags
& EXCHGID4_FLAG_UPD_CONFIRMED_REC_A
;
1992 struct nfsd_net
*nn
= net_generic(SVC_NET(rqstp
), nfsd_net_id
);
1994 rpc_ntop(sa
, addr_str
, sizeof(addr_str
));
1995 dprintk("%s rqstp=%p exid=%p clname.len=%u clname.data=%p "
1996 "ip_addr=%s flags %x, spa_how %d\n",
1997 __func__
, rqstp
, exid
, exid
->clname
.len
, exid
->clname
.data
,
1998 addr_str
, exid
->flags
, exid
->spa_how
);
2000 if (exid
->flags
& ~EXCHGID4_FLAG_MASK_A
)
2001 return nfserr_inval
;
2003 switch (exid
->spa_how
) {
2005 if (!svc_rqst_integrity_protected(rqstp
))
2006 return nfserr_inval
;
2009 default: /* checked by xdr code */
2012 return nfserr_encr_alg_unsupp
;
2015 /* Cases below refer to rfc 5661 section 18.35.4: */
2017 conf
= find_confirmed_client_by_name(&exid
->clname
, nn
);
2019 bool creds_match
= same_creds(&conf
->cl_cred
, &rqstp
->rq_cred
);
2020 bool verfs_match
= same_verf(&verf
, &conf
->cl_verifier
);
2023 if (!clp_used_exchangeid(conf
)) { /* buggy client */
2024 status
= nfserr_inval
;
2027 if (!mach_creds_match(conf
, rqstp
)) {
2028 status
= nfserr_wrong_cred
;
2031 if (!creds_match
) { /* case 9 */
2032 status
= nfserr_perm
;
2035 if (!verfs_match
) { /* case 8 */
2036 status
= nfserr_not_same
;
2040 exid
->flags
|= EXCHGID4_FLAG_CONFIRMED_R
;
2044 if (!creds_match
) { /* case 3 */
2045 if (client_has_state(conf
)) {
2046 status
= nfserr_clid_inuse
;
2049 expire_client(conf
);
2052 if (verfs_match
) { /* case 2 */
2053 conf
->cl_exchange_flags
|= EXCHGID4_FLAG_CONFIRMED_R
;
2057 /* case 5, client reboot */
2061 if (update
) { /* case 7 */
2062 status
= nfserr_noent
;
2066 unconf
= find_unconfirmed_client_by_name(&exid
->clname
, nn
);
2067 if (unconf
) /* case 4, possible retry or client restart */
2068 expire_client(unconf
);
2070 /* case 1 (normal case) */
2072 new = create_client(exid
->clname
, rqstp
, &verf
);
2074 status
= nfserr_jukebox
;
2077 new->cl_minorversion
= cstate
->minorversion
;
2078 new->cl_mach_cred
= (exid
->spa_how
== SP4_MACH_CRED
);
2081 add_to_unconfirmed(new);
2083 exid
->clientid
.cl_boot
= new->cl_clientid
.cl_boot
;
2084 exid
->clientid
.cl_id
= new->cl_clientid
.cl_id
;
2086 exid
->seqid
= new->cl_cs_slot
.sl_seqid
+ 1;
2087 nfsd4_set_ex_flags(new, exid
);
2089 dprintk("nfsd4_exchange_id seqid %d flags %x\n",
2090 new->cl_cs_slot
.sl_seqid
, new->cl_exchange_flags
);
2094 nfs4_unlock_state();
2099 check_slot_seqid(u32 seqid
, u32 slot_seqid
, int slot_inuse
)
2101 dprintk("%s enter. seqid %d slot_seqid %d\n", __func__
, seqid
,
2104 /* The slot is in use, and no response has been sent. */
2106 if (seqid
== slot_seqid
)
2107 return nfserr_jukebox
;
2109 return nfserr_seq_misordered
;
2111 /* Note unsigned 32-bit arithmetic handles wraparound: */
2112 if (likely(seqid
== slot_seqid
+ 1))
2114 if (seqid
== slot_seqid
)
2115 return nfserr_replay_cache
;
2116 return nfserr_seq_misordered
;
2120 * Cache the create session result into the create session single DRC
2121 * slot cache by saving the xdr structure. sl_seqid has been set.
2122 * Do this for solo or embedded create session operations.
2125 nfsd4_cache_create_session(struct nfsd4_create_session
*cr_ses
,
2126 struct nfsd4_clid_slot
*slot
, __be32 nfserr
)
2128 slot
->sl_status
= nfserr
;
2129 memcpy(&slot
->sl_cr_ses
, cr_ses
, sizeof(*cr_ses
));
2133 nfsd4_replay_create_session(struct nfsd4_create_session
*cr_ses
,
2134 struct nfsd4_clid_slot
*slot
)
2136 memcpy(cr_ses
, &slot
->sl_cr_ses
, sizeof(*cr_ses
));
2137 return slot
->sl_status
;
2140 #define NFSD_MIN_REQ_HDR_SEQ_SZ ((\
2141 2 * 2 + /* credential,verifier: AUTH_NULL, length 0 */ \
2142 1 + /* MIN tag is length with zero, only length */ \
2143 3 + /* version, opcount, opcode */ \
2144 XDR_QUADLEN(NFS4_MAX_SESSIONID_LEN) + \
2145 /* seqid, slotID, slotID, cache */ \
2146 4 ) * sizeof(__be32))
2148 #define NFSD_MIN_RESP_HDR_SEQ_SZ ((\
2149 2 + /* verifier: AUTH_NULL, length 0 */\
2151 1 + /* MIN tag is length with zero, only length */ \
2152 3 + /* opcount, opcode, opstatus*/ \
2153 XDR_QUADLEN(NFS4_MAX_SESSIONID_LEN) + \
2154 /* seqid, slotID, slotID, slotID, status */ \
2155 5 ) * sizeof(__be32))
2157 static __be32
check_forechannel_attrs(struct nfsd4_channel_attrs
*ca
, struct nfsd_net
*nn
)
2159 u32 maxrpc
= nn
->nfsd_serv
->sv_max_mesg
;
2161 if (ca
->maxreq_sz
< NFSD_MIN_REQ_HDR_SEQ_SZ
)
2162 return nfserr_toosmall
;
2163 if (ca
->maxresp_sz
< NFSD_MIN_RESP_HDR_SEQ_SZ
)
2164 return nfserr_toosmall
;
2165 ca
->headerpadsz
= 0;
2166 ca
->maxreq_sz
= min_t(u32
, ca
->maxreq_sz
, maxrpc
);
2167 ca
->maxresp_sz
= min_t(u32
, ca
->maxresp_sz
, maxrpc
);
2168 ca
->maxops
= min_t(u32
, ca
->maxops
, NFSD_MAX_OPS_PER_COMPOUND
);
2169 ca
->maxresp_cached
= min_t(u32
, ca
->maxresp_cached
,
2170 NFSD_SLOT_CACHE_SIZE
+ NFSD_MIN_HDR_SEQ_SZ
);
2171 ca
->maxreqs
= min_t(u32
, ca
->maxreqs
, NFSD_MAX_SLOTS_PER_SESSION
);
2173 * Note decreasing slot size below client's request may make it
2174 * difficult for client to function correctly, whereas
2175 * decreasing the number of slots will (just?) affect
2176 * performance. When short on memory we therefore prefer to
2177 * decrease number of slots instead of their size. Clients that
2178 * request larger slots than they need will get poor results:
2180 ca
->maxreqs
= nfsd4_get_drc_mem(ca
);
2182 return nfserr_jukebox
;
2187 #define NFSD_CB_MAX_REQ_SZ ((NFS4_enc_cb_recall_sz + \
2188 RPC_MAX_HEADER_WITH_AUTH) * sizeof(__be32))
2189 #define NFSD_CB_MAX_RESP_SZ ((NFS4_dec_cb_recall_sz + \
2190 RPC_MAX_REPHEADER_WITH_AUTH) * sizeof(__be32))
2192 static __be32
check_backchannel_attrs(struct nfsd4_channel_attrs
*ca
)
2194 ca
->headerpadsz
= 0;
2197 * These RPC_MAX_HEADER macros are overkill, especially since we
2198 * don't even do gss on the backchannel yet. But this is still
2199 * less than 1k. Tighten up this estimate in the unlikely event
2200 * it turns out to be a problem for some client:
2202 if (ca
->maxreq_sz
< NFSD_CB_MAX_REQ_SZ
)
2203 return nfserr_toosmall
;
2204 if (ca
->maxresp_sz
< NFSD_CB_MAX_RESP_SZ
)
2205 return nfserr_toosmall
;
2206 ca
->maxresp_cached
= 0;
2208 return nfserr_toosmall
;
2213 static __be32
nfsd4_check_cb_sec(struct nfsd4_cb_sec
*cbs
)
2215 switch (cbs
->flavor
) {
2221 * GSS case: the spec doesn't allow us to return this
2222 * error. But it also doesn't allow us not to support
2224 * I'd rather this fail hard than return some error the
2225 * client might think it can already handle:
2227 return nfserr_encr_alg_unsupp
;
2232 nfsd4_create_session(struct svc_rqst
*rqstp
,
2233 struct nfsd4_compound_state
*cstate
,
2234 struct nfsd4_create_session
*cr_ses
)
2236 struct sockaddr
*sa
= svc_addr(rqstp
);
2237 struct nfs4_client
*conf
, *unconf
;
2238 struct nfsd4_session
*new;
2239 struct nfsd4_conn
*conn
;
2240 struct nfsd4_clid_slot
*cs_slot
= NULL
;
2242 struct nfsd_net
*nn
= net_generic(SVC_NET(rqstp
), nfsd_net_id
);
2244 if (cr_ses
->flags
& ~SESSION4_FLAG_MASK_A
)
2245 return nfserr_inval
;
2246 status
= nfsd4_check_cb_sec(&cr_ses
->cb_sec
);
2249 status
= check_forechannel_attrs(&cr_ses
->fore_channel
, nn
);
2252 status
= check_backchannel_attrs(&cr_ses
->back_channel
);
2254 goto out_release_drc_mem
;
2255 status
= nfserr_jukebox
;
2256 new = alloc_session(&cr_ses
->fore_channel
, &cr_ses
->back_channel
);
2258 goto out_release_drc_mem
;
2259 conn
= alloc_conn_from_crses(rqstp
, cr_ses
);
2261 goto out_free_session
;
2264 unconf
= find_unconfirmed_client(&cr_ses
->clientid
, true, nn
);
2265 conf
= find_confirmed_client(&cr_ses
->clientid
, true, nn
);
2266 WARN_ON_ONCE(conf
&& unconf
);
2269 status
= nfserr_wrong_cred
;
2270 if (!mach_creds_match(conf
, rqstp
))
2272 cs_slot
= &conf
->cl_cs_slot
;
2273 status
= check_slot_seqid(cr_ses
->seqid
, cs_slot
->sl_seqid
, 0);
2274 if (status
== nfserr_replay_cache
) {
2275 status
= nfsd4_replay_create_session(cr_ses
, cs_slot
);
2277 } else if (cr_ses
->seqid
!= cs_slot
->sl_seqid
+ 1) {
2278 status
= nfserr_seq_misordered
;
2281 } else if (unconf
) {
2282 struct nfs4_client
*old
;
2283 if (!same_creds(&unconf
->cl_cred
, &rqstp
->rq_cred
) ||
2284 !rpc_cmp_addr(sa
, (struct sockaddr
*) &unconf
->cl_addr
)) {
2285 status
= nfserr_clid_inuse
;
2288 status
= nfserr_wrong_cred
;
2289 if (!mach_creds_match(unconf
, rqstp
))
2291 cs_slot
= &unconf
->cl_cs_slot
;
2292 status
= check_slot_seqid(cr_ses
->seqid
, cs_slot
->sl_seqid
, 0);
2294 /* an unconfirmed replay returns misordered */
2295 status
= nfserr_seq_misordered
;
2298 old
= find_confirmed_client_by_name(&unconf
->cl_name
, nn
);
2300 status
= mark_client_expired(old
);
2305 move_to_confirmed(unconf
);
2308 status
= nfserr_stale_clientid
;
2313 * We do not support RDMA or persistent sessions
2315 cr_ses
->flags
&= ~SESSION4_PERSIST
;
2316 cr_ses
->flags
&= ~SESSION4_RDMA
;
2318 init_session(rqstp
, new, conf
, cr_ses
);
2319 nfsd4_init_conn(rqstp
, conn
, new);
2321 memcpy(cr_ses
->sessionid
.data
, new->se_sessionid
.data
,
2322 NFS4_MAX_SESSIONID_LEN
);
2323 cs_slot
->sl_seqid
++;
2324 cr_ses
->seqid
= cs_slot
->sl_seqid
;
2326 /* cache solo and embedded create sessions under the state lock */
2327 nfsd4_cache_create_session(cr_ses
, cs_slot
, status
);
2328 nfs4_unlock_state();
2331 nfs4_unlock_state();
2334 __free_session(new);
2335 out_release_drc_mem
:
2336 nfsd4_put_drc_mem(&cr_ses
->fore_channel
);
2340 static __be32
nfsd4_map_bcts_dir(u32
*dir
)
2343 case NFS4_CDFC4_FORE
:
2344 case NFS4_CDFC4_BACK
:
2346 case NFS4_CDFC4_FORE_OR_BOTH
:
2347 case NFS4_CDFC4_BACK_OR_BOTH
:
2348 *dir
= NFS4_CDFC4_BOTH
;
2351 return nfserr_inval
;
2354 __be32
nfsd4_backchannel_ctl(struct svc_rqst
*rqstp
, struct nfsd4_compound_state
*cstate
, struct nfsd4_backchannel_ctl
*bc
)
2356 struct nfsd4_session
*session
= cstate
->session
;
2357 struct nfsd_net
*nn
= net_generic(SVC_NET(rqstp
), nfsd_net_id
);
2360 status
= nfsd4_check_cb_sec(&bc
->bc_cb_sec
);
2363 spin_lock(&nn
->client_lock
);
2364 session
->se_cb_prog
= bc
->bc_cb_program
;
2365 session
->se_cb_sec
= bc
->bc_cb_sec
;
2366 spin_unlock(&nn
->client_lock
);
2368 nfsd4_probe_callback(session
->se_client
);
2373 __be32
nfsd4_bind_conn_to_session(struct svc_rqst
*rqstp
,
2374 struct nfsd4_compound_state
*cstate
,
2375 struct nfsd4_bind_conn_to_session
*bcts
)
2378 struct nfsd4_conn
*conn
;
2379 struct nfsd4_session
*session
;
2380 struct net
*net
= SVC_NET(rqstp
);
2381 struct nfsd_net
*nn
= net_generic(net
, nfsd_net_id
);
2383 if (!nfsd4_last_compound_op(rqstp
))
2384 return nfserr_not_only_op
;
2386 spin_lock(&nn
->client_lock
);
2387 session
= find_in_sessionid_hashtbl(&bcts
->sessionid
, net
, &status
);
2388 spin_unlock(&nn
->client_lock
);
2390 goto out_no_session
;
2391 status
= nfserr_wrong_cred
;
2392 if (!mach_creds_match(session
->se_client
, rqstp
))
2394 status
= nfsd4_map_bcts_dir(&bcts
->dir
);
2397 conn
= alloc_conn(rqstp
, bcts
->dir
);
2398 status
= nfserr_jukebox
;
2401 nfsd4_init_conn(rqstp
, conn
, session
);
2404 nfsd4_put_session(session
);
2406 nfs4_unlock_state();
2410 static bool nfsd4_compound_in_session(struct nfsd4_session
*session
, struct nfs4_sessionid
*sid
)
2414 return !memcmp(sid
, &session
->se_sessionid
, sizeof(*sid
));
2418 nfsd4_destroy_session(struct svc_rqst
*r
,
2419 struct nfsd4_compound_state
*cstate
,
2420 struct nfsd4_destroy_session
*sessionid
)
2422 struct nfsd4_session
*ses
;
2424 int ref_held_by_me
= 0;
2425 struct net
*net
= SVC_NET(r
);
2426 struct nfsd_net
*nn
= net_generic(net
, nfsd_net_id
);
2429 status
= nfserr_not_only_op
;
2430 if (nfsd4_compound_in_session(cstate
->session
, &sessionid
->sessionid
)) {
2431 if (!nfsd4_last_compound_op(r
))
2435 dump_sessionid(__func__
, &sessionid
->sessionid
);
2436 spin_lock(&nn
->client_lock
);
2437 ses
= find_in_sessionid_hashtbl(&sessionid
->sessionid
, net
, &status
);
2439 goto out_client_lock
;
2440 status
= nfserr_wrong_cred
;
2441 if (!mach_creds_match(ses
->se_client
, r
))
2442 goto out_put_session
;
2443 status
= mark_session_dead_locked(ses
, 1 + ref_held_by_me
);
2445 goto out_put_session
;
2446 unhash_session(ses
);
2447 spin_unlock(&nn
->client_lock
);
2449 nfsd4_probe_callback_sync(ses
->se_client
);
2451 spin_lock(&nn
->client_lock
);
2454 nfsd4_put_session_locked(ses
);
2456 spin_unlock(&nn
->client_lock
);
2458 nfs4_unlock_state();
2462 static struct nfsd4_conn
*__nfsd4_find_conn(struct svc_xprt
*xpt
, struct nfsd4_session
*s
)
2464 struct nfsd4_conn
*c
;
2466 list_for_each_entry(c
, &s
->se_conns
, cn_persession
) {
2467 if (c
->cn_xprt
== xpt
) {
2474 static __be32
nfsd4_sequence_check_conn(struct nfsd4_conn
*new, struct nfsd4_session
*ses
)
2476 struct nfs4_client
*clp
= ses
->se_client
;
2477 struct nfsd4_conn
*c
;
2478 __be32 status
= nfs_ok
;
2481 spin_lock(&clp
->cl_lock
);
2482 c
= __nfsd4_find_conn(new->cn_xprt
, ses
);
2485 status
= nfserr_conn_not_bound_to_session
;
2486 if (clp
->cl_mach_cred
)
2488 __nfsd4_hash_conn(new, ses
);
2489 spin_unlock(&clp
->cl_lock
);
2490 ret
= nfsd4_register_conn(new);
2492 /* oops; xprt is already down: */
2493 nfsd4_conn_lost(&new->cn_xpt_user
);
2496 spin_unlock(&clp
->cl_lock
);
2501 static bool nfsd4_session_too_many_ops(struct svc_rqst
*rqstp
, struct nfsd4_session
*session
)
2503 struct nfsd4_compoundargs
*args
= rqstp
->rq_argp
;
2505 return args
->opcnt
> session
->se_fchannel
.maxops
;
2508 static bool nfsd4_request_too_big(struct svc_rqst
*rqstp
,
2509 struct nfsd4_session
*session
)
2511 struct xdr_buf
*xb
= &rqstp
->rq_arg
;
2513 return xb
->len
> session
->se_fchannel
.maxreq_sz
;
2517 nfsd4_sequence(struct svc_rqst
*rqstp
,
2518 struct nfsd4_compound_state
*cstate
,
2519 struct nfsd4_sequence
*seq
)
2521 struct nfsd4_compoundres
*resp
= rqstp
->rq_resp
;
2522 struct xdr_stream
*xdr
= &resp
->xdr
;
2523 struct nfsd4_session
*session
;
2524 struct nfs4_client
*clp
;
2525 struct nfsd4_slot
*slot
;
2526 struct nfsd4_conn
*conn
;
2529 struct net
*net
= SVC_NET(rqstp
);
2530 struct nfsd_net
*nn
= net_generic(net
, nfsd_net_id
);
2532 if (resp
->opcnt
!= 1)
2533 return nfserr_sequence_pos
;
2536 * Will be either used or freed by nfsd4_sequence_check_conn
2539 conn
= alloc_conn(rqstp
, NFS4_CDFC4_FORE
);
2541 return nfserr_jukebox
;
2543 spin_lock(&nn
->client_lock
);
2544 session
= find_in_sessionid_hashtbl(&seq
->sessionid
, net
, &status
);
2546 goto out_no_session
;
2547 clp
= session
->se_client
;
2549 status
= nfserr_too_many_ops
;
2550 if (nfsd4_session_too_many_ops(rqstp
, session
))
2551 goto out_put_session
;
2553 status
= nfserr_req_too_big
;
2554 if (nfsd4_request_too_big(rqstp
, session
))
2555 goto out_put_session
;
2557 status
= nfserr_badslot
;
2558 if (seq
->slotid
>= session
->se_fchannel
.maxreqs
)
2559 goto out_put_session
;
2561 slot
= session
->se_slots
[seq
->slotid
];
2562 dprintk("%s: slotid %d\n", __func__
, seq
->slotid
);
2564 /* We do not negotiate the number of slots yet, so set the
2565 * maxslots to the session maxreqs which is used to encode
2566 * sr_highest_slotid and the sr_target_slot id to maxslots */
2567 seq
->maxslots
= session
->se_fchannel
.maxreqs
;
2569 status
= check_slot_seqid(seq
->seqid
, slot
->sl_seqid
,
2570 slot
->sl_flags
& NFSD4_SLOT_INUSE
);
2571 if (status
== nfserr_replay_cache
) {
2572 status
= nfserr_seq_misordered
;
2573 if (!(slot
->sl_flags
& NFSD4_SLOT_INITIALIZED
))
2574 goto out_put_session
;
2575 cstate
->slot
= slot
;
2576 cstate
->session
= session
;
2578 /* Return the cached reply status and set cstate->status
2579 * for nfsd4_proc_compound processing */
2580 status
= nfsd4_replay_cache_entry(resp
, seq
);
2581 cstate
->status
= nfserr_replay_cache
;
2585 goto out_put_session
;
2587 status
= nfsd4_sequence_check_conn(conn
, session
);
2590 goto out_put_session
;
2592 buflen
= (seq
->cachethis
) ?
2593 session
->se_fchannel
.maxresp_cached
:
2594 session
->se_fchannel
.maxresp_sz
;
2595 status
= (seq
->cachethis
) ? nfserr_rep_too_big_to_cache
:
2597 if (xdr_restrict_buflen(xdr
, buflen
- rqstp
->rq_auth_slack
))
2598 goto out_put_session
;
2599 svc_reserve(rqstp
, buflen
);
2602 /* Success! bump slot seqid */
2603 slot
->sl_seqid
= seq
->seqid
;
2604 slot
->sl_flags
|= NFSD4_SLOT_INUSE
;
2606 slot
->sl_flags
|= NFSD4_SLOT_CACHETHIS
;
2608 slot
->sl_flags
&= ~NFSD4_SLOT_CACHETHIS
;
2610 cstate
->slot
= slot
;
2611 cstate
->session
= session
;
2615 switch (clp
->cl_cb_state
) {
2617 seq
->status_flags
= SEQ4_STATUS_CB_PATH_DOWN
;
2619 case NFSD4_CB_FAULT
:
2620 seq
->status_flags
= SEQ4_STATUS_BACKCHANNEL_FAULT
;
2623 seq
->status_flags
= 0;
2625 if (!list_empty(&clp
->cl_revoked
))
2626 seq
->status_flags
|= SEQ4_STATUS_RECALLABLE_STATE_REVOKED
;
2630 spin_unlock(&nn
->client_lock
);
2633 nfsd4_put_session_locked(session
);
2634 goto out_no_session
;
2638 nfsd4_sequence_done(struct nfsd4_compoundres
*resp
)
2640 struct nfsd4_compound_state
*cs
= &resp
->cstate
;
2642 if (nfsd4_has_session(cs
)) {
2643 if (cs
->status
!= nfserr_replay_cache
) {
2644 nfsd4_store_cache_entry(resp
);
2645 cs
->slot
->sl_flags
&= ~NFSD4_SLOT_INUSE
;
2647 /* Drop session reference that was taken in nfsd4_sequence() */
2648 nfsd4_put_session(cs
->session
);
2650 put_client_renew(cs
->clp
);
2654 nfsd4_destroy_clientid(struct svc_rqst
*rqstp
, struct nfsd4_compound_state
*cstate
, struct nfsd4_destroy_clientid
*dc
)
2656 struct nfs4_client
*conf
, *unconf
, *clp
;
2658 struct nfsd_net
*nn
= net_generic(SVC_NET(rqstp
), nfsd_net_id
);
2661 unconf
= find_unconfirmed_client(&dc
->clientid
, true, nn
);
2662 conf
= find_confirmed_client(&dc
->clientid
, true, nn
);
2663 WARN_ON_ONCE(conf
&& unconf
);
2668 if (client_has_state(conf
)) {
2669 status
= nfserr_clientid_busy
;
2675 status
= nfserr_stale_clientid
;
2678 if (!mach_creds_match(clp
, rqstp
)) {
2679 status
= nfserr_wrong_cred
;
2684 nfs4_unlock_state();
2689 nfsd4_reclaim_complete(struct svc_rqst
*rqstp
, struct nfsd4_compound_state
*cstate
, struct nfsd4_reclaim_complete
*rc
)
2693 if (rc
->rca_one_fs
) {
2694 if (!cstate
->current_fh
.fh_dentry
)
2695 return nfserr_nofilehandle
;
2697 * We don't take advantage of the rca_one_fs case.
2698 * That's OK, it's optional, we can safely ignore it.
2704 status
= nfserr_complete_already
;
2705 if (test_and_set_bit(NFSD4_CLIENT_RECLAIM_COMPLETE
,
2706 &cstate
->session
->se_client
->cl_flags
))
2709 status
= nfserr_stale_clientid
;
2710 if (is_client_expired(cstate
->session
->se_client
))
2712 * The following error isn't really legal.
2713 * But we only get here if the client just explicitly
2714 * destroyed the client. Surely it no longer cares what
2715 * error it gets back on an operation for the dead
2721 nfsd4_client_record_create(cstate
->session
->se_client
);
2723 nfs4_unlock_state();
2728 nfsd4_setclientid(struct svc_rqst
*rqstp
, struct nfsd4_compound_state
*cstate
,
2729 struct nfsd4_setclientid
*setclid
)
2731 struct xdr_netobj clname
= setclid
->se_name
;
2732 nfs4_verifier clverifier
= setclid
->se_verf
;
2733 struct nfs4_client
*conf
, *unconf
, *new;
2735 struct nfsd_net
*nn
= net_generic(SVC_NET(rqstp
), nfsd_net_id
);
2737 /* Cases below refer to rfc 3530 section 14.2.33: */
2739 conf
= find_confirmed_client_by_name(&clname
, nn
);
2742 status
= nfserr_clid_inuse
;
2743 if (clp_used_exchangeid(conf
))
2745 if (!same_creds(&conf
->cl_cred
, &rqstp
->rq_cred
)) {
2746 char addr_str
[INET6_ADDRSTRLEN
];
2747 rpc_ntop((struct sockaddr
*) &conf
->cl_addr
, addr_str
,
2749 dprintk("NFSD: setclientid: string in use by client "
2750 "at %s\n", addr_str
);
2754 unconf
= find_unconfirmed_client_by_name(&clname
, nn
);
2756 expire_client(unconf
);
2757 status
= nfserr_jukebox
;
2758 new = create_client(clname
, rqstp
, &clverifier
);
2761 if (conf
&& same_verf(&conf
->cl_verifier
, &clverifier
))
2762 /* case 1: probable callback update */
2763 copy_clid(new, conf
);
2764 else /* case 4 (new client) or cases 2, 3 (client reboot): */
2766 new->cl_minorversion
= 0;
2767 gen_callback(new, setclid
, rqstp
);
2768 add_to_unconfirmed(new);
2769 setclid
->se_clientid
.cl_boot
= new->cl_clientid
.cl_boot
;
2770 setclid
->se_clientid
.cl_id
= new->cl_clientid
.cl_id
;
2771 memcpy(setclid
->se_confirm
.data
, new->cl_confirm
.data
, sizeof(setclid
->se_confirm
.data
));
2774 nfs4_unlock_state();
2780 nfsd4_setclientid_confirm(struct svc_rqst
*rqstp
,
2781 struct nfsd4_compound_state
*cstate
,
2782 struct nfsd4_setclientid_confirm
*setclientid_confirm
)
2784 struct nfs4_client
*conf
, *unconf
;
2785 nfs4_verifier confirm
= setclientid_confirm
->sc_confirm
;
2786 clientid_t
* clid
= &setclientid_confirm
->sc_clientid
;
2788 struct nfsd_net
*nn
= net_generic(SVC_NET(rqstp
), nfsd_net_id
);
2790 if (STALE_CLIENTID(clid
, nn
))
2791 return nfserr_stale_clientid
;
2794 conf
= find_confirmed_client(clid
, false, nn
);
2795 unconf
= find_unconfirmed_client(clid
, false, nn
);
2797 * We try hard to give out unique clientid's, so if we get an
2798 * attempt to confirm the same clientid with a different cred,
2799 * there's a bug somewhere. Let's charitably assume it's our
2802 status
= nfserr_serverfault
;
2803 if (unconf
&& !same_creds(&unconf
->cl_cred
, &rqstp
->rq_cred
))
2805 if (conf
&& !same_creds(&conf
->cl_cred
, &rqstp
->rq_cred
))
2807 /* cases below refer to rfc 3530 section 14.2.34: */
2808 if (!unconf
|| !same_verf(&confirm
, &unconf
->cl_confirm
)) {
2809 if (conf
&& !unconf
) /* case 2: probable retransmit */
2811 else /* case 4: client hasn't noticed we rebooted yet? */
2812 status
= nfserr_stale_clientid
;
2816 if (conf
) { /* case 1: callback update */
2817 nfsd4_change_callback(conf
, &unconf
->cl_cb_conn
);
2818 nfsd4_probe_callback(conf
);
2819 expire_client(unconf
);
2820 } else { /* case 3: normal case; new or rebooted client */
2821 conf
= find_confirmed_client_by_name(&unconf
->cl_name
, nn
);
2823 status
= mark_client_expired(conf
);
2826 expire_client(conf
);
2828 move_to_confirmed(unconf
);
2829 nfsd4_probe_callback(unconf
);
2832 nfs4_unlock_state();
2836 static struct nfs4_file
*nfsd4_alloc_file(void)
2838 return kmem_cache_alloc(file_slab
, GFP_KERNEL
);
2841 /* OPEN Share state helper functions */
2842 static void nfsd4_init_file(struct nfs4_file
*fp
, struct knfsd_fh
*fh
)
2844 unsigned int hashval
= file_hashval(fh
);
2846 lockdep_assert_held(&state_lock
);
2848 atomic_set(&fp
->fi_ref
, 1);
2849 spin_lock_init(&fp
->fi_lock
);
2850 INIT_LIST_HEAD(&fp
->fi_stateids
);
2851 INIT_LIST_HEAD(&fp
->fi_delegations
);
2852 fh_copy_shallow(&fp
->fi_fhandle
, fh
);
2853 fp
->fi_had_conflict
= false;
2854 fp
->fi_lease
= NULL
;
2855 fp
->fi_share_deny
= 0;
2856 memset(fp
->fi_fds
, 0, sizeof(fp
->fi_fds
));
2857 memset(fp
->fi_access
, 0, sizeof(fp
->fi_access
));
2858 hlist_add_head(&fp
->fi_hash
, &file_hashtbl
[hashval
]);
2862 nfsd4_free_slabs(void)
2864 kmem_cache_destroy(openowner_slab
);
2865 kmem_cache_destroy(lockowner_slab
);
2866 kmem_cache_destroy(file_slab
);
2867 kmem_cache_destroy(stateid_slab
);
2868 kmem_cache_destroy(deleg_slab
);
2872 nfsd4_init_slabs(void)
2874 openowner_slab
= kmem_cache_create("nfsd4_openowners",
2875 sizeof(struct nfs4_openowner
), 0, 0, NULL
);
2876 if (openowner_slab
== NULL
)
2878 lockowner_slab
= kmem_cache_create("nfsd4_lockowners",
2879 sizeof(struct nfs4_lockowner
), 0, 0, NULL
);
2880 if (lockowner_slab
== NULL
)
2881 goto out_free_openowner_slab
;
2882 file_slab
= kmem_cache_create("nfsd4_files",
2883 sizeof(struct nfs4_file
), 0, 0, NULL
);
2884 if (file_slab
== NULL
)
2885 goto out_free_lockowner_slab
;
2886 stateid_slab
= kmem_cache_create("nfsd4_stateids",
2887 sizeof(struct nfs4_ol_stateid
), 0, 0, NULL
);
2888 if (stateid_slab
== NULL
)
2889 goto out_free_file_slab
;
2890 deleg_slab
= kmem_cache_create("nfsd4_delegations",
2891 sizeof(struct nfs4_delegation
), 0, 0, NULL
);
2892 if (deleg_slab
== NULL
)
2893 goto out_free_stateid_slab
;
2896 out_free_stateid_slab
:
2897 kmem_cache_destroy(stateid_slab
);
2899 kmem_cache_destroy(file_slab
);
2900 out_free_lockowner_slab
:
2901 kmem_cache_destroy(lockowner_slab
);
2902 out_free_openowner_slab
:
2903 kmem_cache_destroy(openowner_slab
);
2905 dprintk("nfsd4: out of memory while initializing nfsv4\n");
2909 static void init_nfs4_replay(struct nfs4_replay
*rp
)
2911 rp
->rp_status
= nfserr_serverfault
;
2913 rp
->rp_buf
= rp
->rp_ibuf
;
2916 static inline void *alloc_stateowner(struct kmem_cache
*slab
, struct xdr_netobj
*owner
, struct nfs4_client
*clp
)
2918 struct nfs4_stateowner
*sop
;
2920 sop
= kmem_cache_alloc(slab
, GFP_KERNEL
);
2924 sop
->so_owner
.data
= kmemdup(owner
->data
, owner
->len
, GFP_KERNEL
);
2925 if (!sop
->so_owner
.data
) {
2926 kmem_cache_free(slab
, sop
);
2929 sop
->so_owner
.len
= owner
->len
;
2931 INIT_LIST_HEAD(&sop
->so_stateids
);
2932 sop
->so_client
= clp
;
2933 init_nfs4_replay(&sop
->so_replay
);
2937 static void hash_openowner(struct nfs4_openowner
*oo
, struct nfs4_client
*clp
, unsigned int strhashval
)
2939 struct nfsd_net
*nn
= net_generic(clp
->net
, nfsd_net_id
);
2941 list_add(&oo
->oo_owner
.so_strhash
, &nn
->ownerstr_hashtbl
[strhashval
]);
2942 list_add(&oo
->oo_perclient
, &clp
->cl_openowners
);
2945 static struct nfs4_openowner
*
2946 alloc_init_open_stateowner(unsigned int strhashval
, struct nfsd4_open
*open
,
2947 struct nfsd4_compound_state
*cstate
)
2949 struct nfs4_client
*clp
= cstate
->clp
;
2950 struct nfs4_openowner
*oo
;
2952 oo
= alloc_stateowner(openowner_slab
, &open
->op_owner
, clp
);
2955 oo
->oo_owner
.so_is_open_owner
= 1;
2956 oo
->oo_owner
.so_seqid
= open
->op_seqid
;
2957 oo
->oo_flags
= NFS4_OO_NEW
;
2958 if (nfsd4_has_session(cstate
))
2959 oo
->oo_flags
|= NFS4_OO_CONFIRMED
;
2961 oo
->oo_last_closed_stid
= NULL
;
2962 INIT_LIST_HEAD(&oo
->oo_close_lru
);
2963 hash_openowner(oo
, clp
, strhashval
);
2967 static void init_open_stateid(struct nfs4_ol_stateid
*stp
, struct nfs4_file
*fp
, struct nfsd4_open
*open
) {
2968 struct nfs4_openowner
*oo
= open
->op_openowner
;
2970 stp
->st_stid
.sc_type
= NFS4_OPEN_STID
;
2971 INIT_LIST_HEAD(&stp
->st_locks
);
2972 list_add(&stp
->st_perstateowner
, &oo
->oo_owner
.so_stateids
);
2973 stp
->st_stateowner
= &oo
->oo_owner
;
2976 stp
->st_access_bmap
= 0;
2977 stp
->st_deny_bmap
= 0;
2978 stp
->st_openstp
= NULL
;
2979 spin_lock(&fp
->fi_lock
);
2980 list_add(&stp
->st_perfile
, &fp
->fi_stateids
);
2981 spin_unlock(&fp
->fi_lock
);
2985 move_to_close_lru(struct nfs4_openowner
*oo
, struct net
*net
)
2987 struct nfsd_net
*nn
= net_generic(net
, nfsd_net_id
);
2989 dprintk("NFSD: move_to_close_lru nfs4_openowner %p\n", oo
);
2991 list_move_tail(&oo
->oo_close_lru
, &nn
->close_lru
);
2992 oo
->oo_time
= get_seconds();
2996 same_owner_str(struct nfs4_stateowner
*sop
, struct xdr_netobj
*owner
,
2999 return (sop
->so_owner
.len
== owner
->len
) &&
3000 0 == memcmp(sop
->so_owner
.data
, owner
->data
, owner
->len
) &&
3001 (sop
->so_client
->cl_clientid
.cl_id
== clid
->cl_id
);
3004 static struct nfs4_openowner
*
3005 find_openstateowner_str(unsigned int hashval
, struct nfsd4_open
*open
,
3006 bool sessions
, struct nfsd_net
*nn
)
3008 struct nfs4_stateowner
*so
;
3009 struct nfs4_openowner
*oo
;
3010 struct nfs4_client
*clp
;
3012 list_for_each_entry(so
, &nn
->ownerstr_hashtbl
[hashval
], so_strhash
) {
3013 if (!so
->so_is_open_owner
)
3015 if (same_owner_str(so
, &open
->op_owner
, &open
->op_clientid
)) {
3017 clp
= oo
->oo_owner
.so_client
;
3018 if ((bool)clp
->cl_minorversion
!= sessions
)
3020 renew_client(oo
->oo_owner
.so_client
);
3027 /* search file_hashtbl[] for file */
3028 static struct nfs4_file
*
3029 find_file_locked(struct knfsd_fh
*fh
)
3031 unsigned int hashval
= file_hashval(fh
);
3032 struct nfs4_file
*fp
;
3034 lockdep_assert_held(&state_lock
);
3036 hlist_for_each_entry(fp
, &file_hashtbl
[hashval
], fi_hash
) {
3037 if (nfsd_fh_match(&fp
->fi_fhandle
, fh
)) {
3045 static struct nfs4_file
*
3046 find_file(struct knfsd_fh
*fh
)
3048 struct nfs4_file
*fp
;
3050 spin_lock(&state_lock
);
3051 fp
= find_file_locked(fh
);
3052 spin_unlock(&state_lock
);
3056 static struct nfs4_file
*
3057 find_or_add_file(struct nfs4_file
*new, struct knfsd_fh
*fh
)
3059 struct nfs4_file
*fp
;
3061 spin_lock(&state_lock
);
3062 fp
= find_file_locked(fh
);
3064 nfsd4_init_file(new, fh
);
3067 spin_unlock(&state_lock
);
3073 * Called to check deny when READ with all zero stateid or
3074 * WRITE with all zero or all one stateid
3077 nfs4_share_conflict(struct svc_fh
*current_fh
, unsigned int deny_type
)
3079 struct nfs4_file
*fp
;
3080 __be32 ret
= nfs_ok
;
3082 fp
= find_file(¤t_fh
->fh_handle
);
3085 /* Check for conflicting share reservations */
3086 spin_lock(&fp
->fi_lock
);
3087 if (fp
->fi_share_deny
& deny_type
)
3088 ret
= nfserr_locked
;
3089 spin_unlock(&fp
->fi_lock
);
3094 void nfsd4_prepare_cb_recall(struct nfs4_delegation
*dp
)
3096 struct nfs4_client
*clp
= dp
->dl_stid
.sc_client
;
3097 struct nfsd_net
*nn
= net_generic(clp
->net
, nfsd_net_id
);
3100 * We can't do this in nfsd_break_deleg_cb because it is
3101 * already holding inode->i_lock
3103 spin_lock(&state_lock
);
3104 block_delegations(&dp
->dl_fh
);
3106 * If the dl_time != 0, then we know that it has already been
3107 * queued for a lease break. Don't queue it again.
3109 if (dp
->dl_time
== 0) {
3110 dp
->dl_time
= get_seconds();
3111 list_add_tail(&dp
->dl_recall_lru
, &nn
->del_recall_lru
);
3113 spin_unlock(&state_lock
);
3116 static void nfsd_break_one_deleg(struct nfs4_delegation
*dp
)
3119 * We're assuming the state code never drops its reference
3120 * without first removing the lease. Since we're in this lease
3121 * callback (and since the lease code is serialized by the kernel
3122 * lock) we know the server hasn't removed the lease yet, we know
3123 * it's safe to take a reference.
3125 atomic_inc(&dp
->dl_stid
.sc_count
);
3126 nfsd4_cb_recall(dp
);
3129 /* Called from break_lease() with i_lock held. */
3130 static void nfsd_break_deleg_cb(struct file_lock
*fl
)
3132 struct nfs4_file
*fp
= (struct nfs4_file
*)fl
->fl_owner
;
3133 struct nfs4_delegation
*dp
;
3136 WARN(1, "(%p)->fl_owner NULL\n", fl
);
3139 if (fp
->fi_had_conflict
) {
3140 WARN(1, "duplicate break on %p\n", fp
);
3144 * We don't want the locks code to timeout the lease for us;
3145 * we'll remove it ourself if a delegation isn't returned
3148 fl
->fl_break_time
= 0;
3150 spin_lock(&fp
->fi_lock
);
3151 fp
->fi_had_conflict
= true;
3153 * If there are no delegations on the list, then we can't count on this
3154 * lease ever being cleaned up. Set the fl_break_time to jiffies so that
3155 * time_out_leases will do it ASAP. The fact that fi_had_conflict is now
3156 * true should keep any new delegations from being hashed.
3158 if (list_empty(&fp
->fi_delegations
))
3159 fl
->fl_break_time
= jiffies
;
3161 list_for_each_entry(dp
, &fp
->fi_delegations
, dl_perfile
)
3162 nfsd_break_one_deleg(dp
);
3163 spin_unlock(&fp
->fi_lock
);
3167 int nfsd_change_deleg_cb(struct file_lock
**onlist
, int arg
)
3170 return lease_modify(onlist
, arg
);
3175 static const struct lock_manager_operations nfsd_lease_mng_ops
= {
3176 .lm_break
= nfsd_break_deleg_cb
,
3177 .lm_change
= nfsd_change_deleg_cb
,
3180 static __be32
nfsd4_check_seqid(struct nfsd4_compound_state
*cstate
, struct nfs4_stateowner
*so
, u32 seqid
)
3182 if (nfsd4_has_session(cstate
))
3184 if (seqid
== so
->so_seqid
- 1)
3185 return nfserr_replay_me
;
3186 if (seqid
== so
->so_seqid
)
3188 return nfserr_bad_seqid
;
3191 static __be32
lookup_clientid(clientid_t
*clid
,
3192 struct nfsd4_compound_state
*cstate
,
3193 struct nfsd_net
*nn
)
3195 struct nfs4_client
*found
;
3198 found
= cstate
->clp
;
3199 if (!same_clid(&found
->cl_clientid
, clid
))
3200 return nfserr_stale_clientid
;
3204 if (STALE_CLIENTID(clid
, nn
))
3205 return nfserr_stale_clientid
;
3208 * For v4.1+ we get the client in the SEQUENCE op. If we don't have one
3209 * cached already then we know this is for is for v4.0 and "sessions"
3212 WARN_ON_ONCE(cstate
->session
);
3213 found
= find_confirmed_client(clid
, false, nn
);
3215 return nfserr_expired
;
3217 /* Cache the nfs4_client in cstate! */
3218 cstate
->clp
= found
;
3219 atomic_inc(&found
->cl_refcount
);
3224 nfsd4_process_open1(struct nfsd4_compound_state
*cstate
,
3225 struct nfsd4_open
*open
, struct nfsd_net
*nn
)
3227 clientid_t
*clientid
= &open
->op_clientid
;
3228 struct nfs4_client
*clp
= NULL
;
3229 unsigned int strhashval
;
3230 struct nfs4_openowner
*oo
= NULL
;
3233 if (STALE_CLIENTID(&open
->op_clientid
, nn
))
3234 return nfserr_stale_clientid
;
3236 * In case we need it later, after we've already created the
3237 * file and don't want to risk a further failure:
3239 open
->op_file
= nfsd4_alloc_file();
3240 if (open
->op_file
== NULL
)
3241 return nfserr_jukebox
;
3243 status
= lookup_clientid(clientid
, cstate
, nn
);
3248 strhashval
= ownerstr_hashval(clientid
->cl_id
, &open
->op_owner
);
3249 oo
= find_openstateowner_str(strhashval
, open
, cstate
->minorversion
, nn
);
3250 open
->op_openowner
= oo
;
3254 if (!(oo
->oo_flags
& NFS4_OO_CONFIRMED
)) {
3255 /* Replace unconfirmed owners without checking for replay. */
3256 release_openowner(oo
);
3257 open
->op_openowner
= NULL
;
3260 status
= nfsd4_check_seqid(cstate
, &oo
->oo_owner
, open
->op_seqid
);
3265 oo
= alloc_init_open_stateowner(strhashval
, open
, cstate
);
3267 return nfserr_jukebox
;
3268 open
->op_openowner
= oo
;
3270 open
->op_stp
= nfs4_alloc_stateid(clp
);
3272 return nfserr_jukebox
;
3276 static inline __be32
3277 nfs4_check_delegmode(struct nfs4_delegation
*dp
, int flags
)
3279 if ((flags
& WR_STATE
) && (dp
->dl_type
== NFS4_OPEN_DELEGATE_READ
))
3280 return nfserr_openmode
;
3285 static int share_access_to_flags(u32 share_access
)
3287 return share_access
== NFS4_SHARE_ACCESS_READ
? RD_STATE
: WR_STATE
;
3290 static struct nfs4_delegation
*find_deleg_stateid(struct nfs4_client
*cl
, stateid_t
*s
)
3292 struct nfs4_stid
*ret
;
3294 ret
= find_stateid_by_type(cl
, s
, NFS4_DELEG_STID
);
3297 return delegstateid(ret
);
3300 static bool nfsd4_is_deleg_cur(struct nfsd4_open
*open
)
3302 return open
->op_claim_type
== NFS4_OPEN_CLAIM_DELEGATE_CUR
||
3303 open
->op_claim_type
== NFS4_OPEN_CLAIM_DELEG_CUR_FH
;
3307 nfs4_check_deleg(struct nfs4_client
*cl
, struct nfsd4_open
*open
,
3308 struct nfs4_delegation
**dp
)
3311 __be32 status
= nfserr_bad_stateid
;
3313 *dp
= find_deleg_stateid(cl
, &open
->op_delegate_stateid
);
3316 flags
= share_access_to_flags(open
->op_share_access
);
3317 status
= nfs4_check_delegmode(*dp
, flags
);
3321 if (!nfsd4_is_deleg_cur(open
))
3325 open
->op_openowner
->oo_flags
|= NFS4_OO_CONFIRMED
;
3329 static struct nfs4_ol_stateid
*
3330 nfsd4_find_existing_open(struct nfs4_file
*fp
, struct nfsd4_open
*open
)
3332 struct nfs4_ol_stateid
*local
, *ret
= NULL
;
3333 struct nfs4_openowner
*oo
= open
->op_openowner
;
3335 spin_lock(&fp
->fi_lock
);
3336 list_for_each_entry(local
, &fp
->fi_stateids
, st_perfile
) {
3337 /* ignore lock owners */
3338 if (local
->st_stateowner
->so_is_open_owner
== 0)
3340 if (local
->st_stateowner
== &oo
->oo_owner
) {
3345 spin_unlock(&fp
->fi_lock
);
3349 static inline int nfs4_access_to_access(u32 nfs4_access
)
3353 if (nfs4_access
& NFS4_SHARE_ACCESS_READ
)
3354 flags
|= NFSD_MAY_READ
;
3355 if (nfs4_access
& NFS4_SHARE_ACCESS_WRITE
)
3356 flags
|= NFSD_MAY_WRITE
;
3360 static inline __be32
3361 nfsd4_truncate(struct svc_rqst
*rqstp
, struct svc_fh
*fh
,
3362 struct nfsd4_open
*open
)
3364 struct iattr iattr
= {
3365 .ia_valid
= ATTR_SIZE
,
3368 if (!open
->op_truncate
)
3370 if (!(open
->op_share_access
& NFS4_SHARE_ACCESS_WRITE
))
3371 return nfserr_inval
;
3372 return nfsd_setattr(rqstp
, fh
, &iattr
, 0, (time_t)0);
3375 static __be32
nfs4_get_vfs_file(struct svc_rqst
*rqstp
, struct nfs4_file
*fp
,
3376 struct svc_fh
*cur_fh
, struct nfs4_ol_stateid
*stp
,
3377 struct nfsd4_open
*open
)
3379 struct file
*filp
= NULL
;
3381 int oflag
= nfs4_access_to_omode(open
->op_share_access
);
3382 int access
= nfs4_access_to_access(open
->op_share_access
);
3383 unsigned char old_access_bmap
, old_deny_bmap
;
3385 spin_lock(&fp
->fi_lock
);
3388 * Are we trying to set a deny mode that would conflict with
3391 status
= nfs4_file_check_deny(fp
, open
->op_share_deny
);
3392 if (status
!= nfs_ok
) {
3393 spin_unlock(&fp
->fi_lock
);
3397 /* set access to the file */
3398 status
= nfs4_file_get_access(fp
, open
->op_share_access
);
3399 if (status
!= nfs_ok
) {
3400 spin_unlock(&fp
->fi_lock
);
3404 /* Set access bits in stateid */
3405 old_access_bmap
= stp
->st_access_bmap
;
3406 set_access(open
->op_share_access
, stp
);
3408 /* Set new deny mask */
3409 old_deny_bmap
= stp
->st_deny_bmap
;
3410 set_deny(open
->op_share_deny
, stp
);
3411 fp
->fi_share_deny
|= (open
->op_share_deny
& NFS4_SHARE_DENY_BOTH
);
3413 if (!fp
->fi_fds
[oflag
]) {
3414 spin_unlock(&fp
->fi_lock
);
3415 status
= nfsd_open(rqstp
, cur_fh
, S_IFREG
, access
, &filp
);
3417 goto out_put_access
;
3418 spin_lock(&fp
->fi_lock
);
3419 if (!fp
->fi_fds
[oflag
]) {
3420 fp
->fi_fds
[oflag
] = filp
;
3424 spin_unlock(&fp
->fi_lock
);
3428 status
= nfsd4_truncate(rqstp
, cur_fh
, open
);
3430 goto out_put_access
;
3434 stp
->st_access_bmap
= old_access_bmap
;
3435 nfs4_file_put_access(fp
, open
->op_share_access
);
3436 reset_union_bmap_deny(bmap_to_share_mode(old_deny_bmap
), stp
);
3441 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
)
3444 unsigned char old_deny_bmap
;
3446 if (!test_access(open
->op_share_access
, stp
))
3447 return nfs4_get_vfs_file(rqstp
, fp
, cur_fh
, stp
, open
);
3449 /* test and set deny mode */
3450 spin_lock(&fp
->fi_lock
);
3451 status
= nfs4_file_check_deny(fp
, open
->op_share_deny
);
3452 if (status
== nfs_ok
) {
3453 old_deny_bmap
= stp
->st_deny_bmap
;
3454 set_deny(open
->op_share_deny
, stp
);
3455 fp
->fi_share_deny
|=
3456 (open
->op_share_deny
& NFS4_SHARE_DENY_BOTH
);
3458 spin_unlock(&fp
->fi_lock
);
3460 if (status
!= nfs_ok
)
3463 status
= nfsd4_truncate(rqstp
, cur_fh
, open
);
3464 if (status
!= nfs_ok
)
3465 reset_union_bmap_deny(old_deny_bmap
, stp
);
3470 nfs4_set_claim_prev(struct nfsd4_open
*open
, bool has_session
)
3472 open
->op_openowner
->oo_flags
|= NFS4_OO_CONFIRMED
;
3475 /* Should we give out recallable state?: */
3476 static bool nfsd4_cb_channel_good(struct nfs4_client
*clp
)
3478 if (clp
->cl_cb_state
== NFSD4_CB_UP
)
3481 * In the sessions case, since we don't have to establish a
3482 * separate connection for callbacks, we assume it's OK
3483 * until we hear otherwise:
3485 return clp
->cl_minorversion
&& clp
->cl_cb_state
== NFSD4_CB_UNKNOWN
;
3488 static struct file_lock
*nfs4_alloc_init_lease(struct nfs4_file
*fp
, int flag
)
3490 struct file_lock
*fl
;
3492 fl
= locks_alloc_lock();
3495 locks_init_lock(fl
);
3496 fl
->fl_lmops
= &nfsd_lease_mng_ops
;
3497 fl
->fl_flags
= FL_DELEG
;
3498 fl
->fl_type
= flag
== NFS4_OPEN_DELEGATE_READ
? F_RDLCK
: F_WRLCK
;
3499 fl
->fl_end
= OFFSET_MAX
;
3500 fl
->fl_owner
= (fl_owner_t
)fp
;
3501 fl
->fl_pid
= current
->tgid
;
3505 static int nfs4_setlease(struct nfs4_delegation
*dp
)
3507 struct nfs4_file
*fp
= dp
->dl_file
;
3508 struct file_lock
*fl
;
3512 fl
= nfs4_alloc_init_lease(fp
, NFS4_OPEN_DELEGATE_READ
);
3515 filp
= find_readable_file(fp
);
3517 /* We should always have a readable file here */
3522 status
= vfs_setlease(filp
, fl
->fl_type
, &fl
);
3524 locks_free_lock(fl
);
3527 spin_lock(&state_lock
);
3528 spin_lock(&fp
->fi_lock
);
3529 /* Did the lease get broken before we took the lock? */
3531 if (fp
->fi_had_conflict
)
3536 atomic_inc(&fp
->fi_delegees
);
3537 hash_delegation_locked(dp
, fp
);
3541 fp
->fi_deleg_file
= filp
;
3542 atomic_set(&fp
->fi_delegees
, 1);
3543 hash_delegation_locked(dp
, fp
);
3544 spin_unlock(&fp
->fi_lock
);
3545 spin_unlock(&state_lock
);
3548 spin_unlock(&fp
->fi_lock
);
3549 spin_unlock(&state_lock
);
3555 static int nfs4_set_delegation(struct nfs4_delegation
*dp
, struct nfs4_file
*fp
)
3559 if (fp
->fi_had_conflict
)
3562 spin_lock(&state_lock
);
3563 spin_lock(&fp
->fi_lock
);
3565 if (!fp
->fi_lease
) {
3566 spin_unlock(&fp
->fi_lock
);
3567 spin_unlock(&state_lock
);
3568 return nfs4_setlease(dp
);
3570 atomic_inc(&fp
->fi_delegees
);
3571 if (fp
->fi_had_conflict
) {
3575 hash_delegation_locked(dp
, fp
);
3577 spin_unlock(&fp
->fi_lock
);
3578 spin_unlock(&state_lock
);
3582 static void nfsd4_open_deleg_none_ext(struct nfsd4_open
*open
, int status
)
3584 open
->op_delegate_type
= NFS4_OPEN_DELEGATE_NONE_EXT
;
3585 if (status
== -EAGAIN
)
3586 open
->op_why_no_deleg
= WND4_CONTENTION
;
3588 open
->op_why_no_deleg
= WND4_RESOURCE
;
3589 switch (open
->op_deleg_want
) {
3590 case NFS4_SHARE_WANT_READ_DELEG
:
3591 case NFS4_SHARE_WANT_WRITE_DELEG
:
3592 case NFS4_SHARE_WANT_ANY_DELEG
:
3594 case NFS4_SHARE_WANT_CANCEL
:
3595 open
->op_why_no_deleg
= WND4_CANCELLED
;
3597 case NFS4_SHARE_WANT_NO_DELEG
:
3604 * Attempt to hand out a delegation.
3606 * Note we don't support write delegations, and won't until the vfs has
3607 * proper support for them.
3610 nfs4_open_delegation(struct net
*net
, struct svc_fh
*fh
,
3611 struct nfsd4_open
*open
, struct nfs4_ol_stateid
*stp
)
3613 struct nfs4_delegation
*dp
;
3614 struct nfs4_openowner
*oo
= container_of(stp
->st_stateowner
, struct nfs4_openowner
, oo_owner
);
3618 cb_up
= nfsd4_cb_channel_good(oo
->oo_owner
.so_client
);
3619 open
->op_recall
= 0;
3620 switch (open
->op_claim_type
) {
3621 case NFS4_OPEN_CLAIM_PREVIOUS
:
3623 open
->op_recall
= 1;
3624 if (open
->op_delegate_type
!= NFS4_OPEN_DELEGATE_READ
)
3627 case NFS4_OPEN_CLAIM_NULL
:
3628 case NFS4_OPEN_CLAIM_FH
:
3630 * Let's not give out any delegations till everyone's
3631 * had the chance to reclaim theirs....
3633 if (locks_in_grace(net
))
3635 if (!cb_up
|| !(oo
->oo_flags
& NFS4_OO_CONFIRMED
))
3638 * Also, if the file was opened for write or
3639 * create, there's a good chance the client's
3640 * about to write to it, resulting in an
3641 * immediate recall (since we don't support
3642 * write delegations):
3644 if (open
->op_share_access
& NFS4_SHARE_ACCESS_WRITE
)
3646 if (open
->op_create
== NFS4_OPEN_CREATE
)
3652 dp
= alloc_init_deleg(oo
->oo_owner
.so_client
, stp
, fh
);
3655 status
= nfs4_set_delegation(dp
, stp
->st_file
);
3659 memcpy(&open
->op_delegate_stateid
, &dp
->dl_stid
.sc_stateid
, sizeof(dp
->dl_stid
.sc_stateid
));
3661 dprintk("NFSD: delegation stateid=" STATEID_FMT
"\n",
3662 STATEID_VAL(&dp
->dl_stid
.sc_stateid
));
3663 open
->op_delegate_type
= NFS4_OPEN_DELEGATE_READ
;
3666 nfs4_put_delegation(dp
);
3668 open
->op_delegate_type
= NFS4_OPEN_DELEGATE_NONE
;
3669 if (open
->op_claim_type
== NFS4_OPEN_CLAIM_PREVIOUS
&&
3670 open
->op_delegate_type
!= NFS4_OPEN_DELEGATE_NONE
) {
3671 dprintk("NFSD: WARNING: refusing delegation reclaim\n");
3672 open
->op_recall
= 1;
3675 /* 4.1 client asking for a delegation? */
3676 if (open
->op_deleg_want
)
3677 nfsd4_open_deleg_none_ext(open
, status
);
3681 static void nfsd4_deleg_xgrade_none_ext(struct nfsd4_open
*open
,
3682 struct nfs4_delegation
*dp
)
3684 if (open
->op_deleg_want
== NFS4_SHARE_WANT_READ_DELEG
&&
3685 dp
->dl_type
== NFS4_OPEN_DELEGATE_WRITE
) {
3686 open
->op_delegate_type
= NFS4_OPEN_DELEGATE_NONE_EXT
;
3687 open
->op_why_no_deleg
= WND4_NOT_SUPP_DOWNGRADE
;
3688 } else if (open
->op_deleg_want
== NFS4_SHARE_WANT_WRITE_DELEG
&&
3689 dp
->dl_type
== NFS4_OPEN_DELEGATE_WRITE
) {
3690 open
->op_delegate_type
= NFS4_OPEN_DELEGATE_NONE_EXT
;
3691 open
->op_why_no_deleg
= WND4_NOT_SUPP_UPGRADE
;
3693 /* Otherwise the client must be confused wanting a delegation
3694 * it already has, therefore we don't return
3695 * NFS4_OPEN_DELEGATE_NONE_EXT and reason.
3700 * called with nfs4_lock_state() held.
3703 nfsd4_process_open2(struct svc_rqst
*rqstp
, struct svc_fh
*current_fh
, struct nfsd4_open
*open
)
3705 struct nfsd4_compoundres
*resp
= rqstp
->rq_resp
;
3706 struct nfs4_client
*cl
= open
->op_openowner
->oo_owner
.so_client
;
3707 struct nfs4_file
*fp
= NULL
;
3708 struct nfs4_ol_stateid
*stp
= NULL
;
3709 struct nfs4_delegation
*dp
= NULL
;
3713 * Lookup file; if found, lookup stateid and check open request,
3714 * and check for delegations in the process of being recalled.
3715 * If not found, create the nfs4_file struct
3717 fp
= find_or_add_file(open
->op_file
, ¤t_fh
->fh_handle
);
3718 if (fp
!= open
->op_file
) {
3719 status
= nfs4_check_deleg(cl
, open
, &dp
);
3722 stp
= nfsd4_find_existing_open(fp
, open
);
3724 open
->op_file
= NULL
;
3725 status
= nfserr_bad_stateid
;
3726 if (nfsd4_is_deleg_cur(open
))
3728 status
= nfserr_jukebox
;
3732 * OPEN the file, or upgrade an existing OPEN.
3733 * If truncate fails, the OPEN fails.
3736 /* Stateid was found, this is an OPEN upgrade */
3737 status
= nfs4_upgrade_open(rqstp
, fp
, current_fh
, stp
, open
);
3742 open
->op_stp
= NULL
;
3743 init_open_stateid(stp
, fp
, open
);
3744 status
= nfs4_get_vfs_file(rqstp
, fp
, current_fh
, stp
, open
);
3746 release_open_stateid(stp
);
3750 update_stateid(&stp
->st_stid
.sc_stateid
);
3751 memcpy(&open
->op_stateid
, &stp
->st_stid
.sc_stateid
, sizeof(stateid_t
));
3753 if (nfsd4_has_session(&resp
->cstate
)) {
3754 if (open
->op_deleg_want
& NFS4_SHARE_WANT_NO_DELEG
) {
3755 open
->op_delegate_type
= NFS4_OPEN_DELEGATE_NONE_EXT
;
3756 open
->op_why_no_deleg
= WND4_NOT_WANTED
;
3762 * Attempt to hand out a delegation. No error return, because the
3763 * OPEN succeeds even if we fail.
3765 nfs4_open_delegation(SVC_NET(rqstp
), current_fh
, open
, stp
);
3769 dprintk("%s: stateid=" STATEID_FMT
"\n", __func__
,
3770 STATEID_VAL(&stp
->st_stid
.sc_stateid
));
3772 /* 4.1 client trying to upgrade/downgrade delegation? */
3773 if (open
->op_delegate_type
== NFS4_OPEN_DELEGATE_NONE
&& dp
&&
3774 open
->op_deleg_want
)
3775 nfsd4_deleg_xgrade_none_ext(open
, dp
);
3779 if (status
== 0 && open
->op_claim_type
== NFS4_OPEN_CLAIM_PREVIOUS
)
3780 nfs4_set_claim_prev(open
, nfsd4_has_session(&resp
->cstate
));
3782 * To finish the open response, we just need to set the rflags.
3784 open
->op_rflags
= NFS4_OPEN_RESULT_LOCKTYPE_POSIX
;
3785 if (!(open
->op_openowner
->oo_flags
& NFS4_OO_CONFIRMED
) &&
3786 !nfsd4_has_session(&resp
->cstate
))
3787 open
->op_rflags
|= NFS4_OPEN_RESULT_CONFIRM
;
3792 void nfsd4_cleanup_open_state(struct nfsd4_open
*open
, __be32 status
)
3794 if (open
->op_openowner
) {
3795 struct nfs4_openowner
*oo
= open
->op_openowner
;
3797 if (!list_empty(&oo
->oo_owner
.so_stateids
))
3798 list_del_init(&oo
->oo_close_lru
);
3799 if (oo
->oo_flags
& NFS4_OO_NEW
) {
3801 release_openowner(oo
);
3802 open
->op_openowner
= NULL
;
3804 oo
->oo_flags
&= ~NFS4_OO_NEW
;
3808 nfsd4_free_file(open
->op_file
);
3810 free_generic_stateid(open
->op_stp
);
3814 nfsd4_renew(struct svc_rqst
*rqstp
, struct nfsd4_compound_state
*cstate
,
3817 struct nfs4_client
*clp
;
3819 struct nfsd_net
*nn
= net_generic(SVC_NET(rqstp
), nfsd_net_id
);
3822 dprintk("process_renew(%08x/%08x): starting\n",
3823 clid
->cl_boot
, clid
->cl_id
);
3824 status
= lookup_clientid(clid
, cstate
, nn
);
3828 status
= nfserr_cb_path_down
;
3829 if (!list_empty(&clp
->cl_delegations
)
3830 && clp
->cl_cb_state
!= NFSD4_CB_UP
)
3834 nfs4_unlock_state();
3839 nfsd4_end_grace(struct nfsd_net
*nn
)
3841 /* do nothing if grace period already ended */
3842 if (nn
->grace_ended
)
3845 dprintk("NFSD: end of grace period\n");
3846 nn
->grace_ended
= true;
3847 nfsd4_record_grace_done(nn
, nn
->boot_time
);
3848 locks_end_grace(&nn
->nfsd4_manager
);
3850 * Now that every NFSv4 client has had the chance to recover and
3851 * to see the (possibly new, possibly shorter) lease time, we
3852 * can safely set the next grace time to the current lease time:
3854 nn
->nfsd4_grace
= nn
->nfsd4_lease
;
3858 nfs4_laundromat(struct nfsd_net
*nn
)
3860 struct nfs4_client
*clp
;
3861 struct nfs4_openowner
*oo
;
3862 struct nfs4_delegation
*dp
;
3863 struct list_head
*pos
, *next
, reaplist
;
3864 time_t cutoff
= get_seconds() - nn
->nfsd4_lease
;
3865 time_t t
, new_timeo
= nn
->nfsd4_lease
;
3869 dprintk("NFSD: laundromat service - starting\n");
3870 nfsd4_end_grace(nn
);
3871 INIT_LIST_HEAD(&reaplist
);
3872 spin_lock(&nn
->client_lock
);
3873 list_for_each_safe(pos
, next
, &nn
->client_lru
) {
3874 clp
= list_entry(pos
, struct nfs4_client
, cl_lru
);
3875 if (time_after((unsigned long)clp
->cl_time
, (unsigned long)cutoff
)) {
3876 t
= clp
->cl_time
- cutoff
;
3877 new_timeo
= min(new_timeo
, t
);
3880 if (mark_client_expired_locked(clp
)) {
3881 dprintk("NFSD: client in use (clientid %08x)\n",
3882 clp
->cl_clientid
.cl_id
);
3885 list_move(&clp
->cl_lru
, &reaplist
);
3887 spin_unlock(&nn
->client_lock
);
3888 list_for_each_safe(pos
, next
, &reaplist
) {
3889 clp
= list_entry(pos
, struct nfs4_client
, cl_lru
);
3890 dprintk("NFSD: purging unused client (clientid %08x)\n",
3891 clp
->cl_clientid
.cl_id
);
3894 spin_lock(&state_lock
);
3895 list_for_each_safe(pos
, next
, &nn
->del_recall_lru
) {
3896 dp
= list_entry (pos
, struct nfs4_delegation
, dl_recall_lru
);
3897 if (net_generic(dp
->dl_stid
.sc_client
->net
, nfsd_net_id
) != nn
)
3899 if (time_after((unsigned long)dp
->dl_time
, (unsigned long)cutoff
)) {
3900 t
= dp
->dl_time
- cutoff
;
3901 new_timeo
= min(new_timeo
, t
);
3904 unhash_delegation_locked(dp
);
3905 list_add(&dp
->dl_recall_lru
, &reaplist
);
3907 spin_unlock(&state_lock
);
3908 while (!list_empty(&reaplist
)) {
3909 dp
= list_first_entry(&reaplist
, struct nfs4_delegation
,
3911 list_del_init(&dp
->dl_recall_lru
);
3912 revoke_delegation(dp
);
3914 list_for_each_safe(pos
, next
, &nn
->close_lru
) {
3915 oo
= container_of(pos
, struct nfs4_openowner
, oo_close_lru
);
3916 if (time_after((unsigned long)oo
->oo_time
, (unsigned long)cutoff
)) {
3917 t
= oo
->oo_time
- cutoff
;
3918 new_timeo
= min(new_timeo
, t
);
3921 release_openowner(oo
);
3923 new_timeo
= max_t(time_t, new_timeo
, NFSD_LAUNDROMAT_MINTIMEOUT
);
3924 nfs4_unlock_state();
3928 static struct workqueue_struct
*laundry_wq
;
3929 static void laundromat_main(struct work_struct
*);
3932 laundromat_main(struct work_struct
*laundry
)
3935 struct delayed_work
*dwork
= container_of(laundry
, struct delayed_work
,
3937 struct nfsd_net
*nn
= container_of(dwork
, struct nfsd_net
,
3940 t
= nfs4_laundromat(nn
);
3941 dprintk("NFSD: laundromat_main - sleeping for %ld seconds\n", t
);
3942 queue_delayed_work(laundry_wq
, &nn
->laundromat_work
, t
*HZ
);
3945 static inline __be32
nfs4_check_fh(struct svc_fh
*fhp
, struct nfs4_ol_stateid
*stp
)
3947 if (!nfsd_fh_match(&fhp
->fh_handle
, &stp
->st_file
->fi_fhandle
))
3948 return nfserr_bad_stateid
;
3953 access_permit_read(struct nfs4_ol_stateid
*stp
)
3955 return test_access(NFS4_SHARE_ACCESS_READ
, stp
) ||
3956 test_access(NFS4_SHARE_ACCESS_BOTH
, stp
) ||
3957 test_access(NFS4_SHARE_ACCESS_WRITE
, stp
);
3961 access_permit_write(struct nfs4_ol_stateid
*stp
)
3963 return test_access(NFS4_SHARE_ACCESS_WRITE
, stp
) ||
3964 test_access(NFS4_SHARE_ACCESS_BOTH
, stp
);
3968 __be32
nfs4_check_openmode(struct nfs4_ol_stateid
*stp
, int flags
)
3970 __be32 status
= nfserr_openmode
;
3972 /* For lock stateid's, we test the parent open, not the lock: */
3973 if (stp
->st_openstp
)
3974 stp
= stp
->st_openstp
;
3975 if ((flags
& WR_STATE
) && !access_permit_write(stp
))
3977 if ((flags
& RD_STATE
) && !access_permit_read(stp
))
3984 static inline __be32
3985 check_special_stateids(struct net
*net
, svc_fh
*current_fh
, stateid_t
*stateid
, int flags
)
3987 if (ONE_STATEID(stateid
) && (flags
& RD_STATE
))
3989 else if (locks_in_grace(net
)) {
3990 /* Answer in remaining cases depends on existence of
3991 * conflicting state; so we must wait out the grace period. */
3992 return nfserr_grace
;
3993 } else if (flags
& WR_STATE
)
3994 return nfs4_share_conflict(current_fh
,
3995 NFS4_SHARE_DENY_WRITE
);
3996 else /* (flags & RD_STATE) && ZERO_STATEID(stateid) */
3997 return nfs4_share_conflict(current_fh
,
3998 NFS4_SHARE_DENY_READ
);
4002 * Allow READ/WRITE during grace period on recovered state only for files
4003 * that are not able to provide mandatory locking.
4006 grace_disallows_io(struct net
*net
, struct inode
*inode
)
4008 return locks_in_grace(net
) && mandatory_lock(inode
);
4011 /* Returns true iff a is later than b: */
4012 static bool stateid_generation_after(stateid_t
*a
, stateid_t
*b
)
4014 return (s32
)(a
->si_generation
- b
->si_generation
) > 0;
4017 static __be32
check_stateid_generation(stateid_t
*in
, stateid_t
*ref
, bool has_session
)
4020 * When sessions are used the stateid generation number is ignored
4023 if (has_session
&& in
->si_generation
== 0)
4026 if (in
->si_generation
== ref
->si_generation
)
4029 /* If the client sends us a stateid from the future, it's buggy: */
4030 if (stateid_generation_after(in
, ref
))
4031 return nfserr_bad_stateid
;
4033 * However, we could see a stateid from the past, even from a
4034 * non-buggy client. For example, if the client sends a lock
4035 * while some IO is outstanding, the lock may bump si_generation
4036 * while the IO is still in flight. The client could avoid that
4037 * situation by waiting for responses on all the IO requests,
4038 * but better performance may result in retrying IO that
4039 * receives an old_stateid error if requests are rarely
4040 * reordered in flight:
4042 return nfserr_old_stateid
;
4045 static __be32
nfsd4_validate_stateid(struct nfs4_client
*cl
, stateid_t
*stateid
)
4047 struct nfs4_stid
*s
;
4048 struct nfs4_ol_stateid
*ols
;
4051 if (ZERO_STATEID(stateid
) || ONE_STATEID(stateid
))
4052 return nfserr_bad_stateid
;
4053 /* Client debugging aid. */
4054 if (!same_clid(&stateid
->si_opaque
.so_clid
, &cl
->cl_clientid
)) {
4055 char addr_str
[INET6_ADDRSTRLEN
];
4056 rpc_ntop((struct sockaddr
*)&cl
->cl_addr
, addr_str
,
4058 pr_warn_ratelimited("NFSD: client %s testing state ID "
4059 "with incorrect client ID\n", addr_str
);
4060 return nfserr_bad_stateid
;
4062 s
= find_stateid(cl
, stateid
);
4064 return nfserr_bad_stateid
;
4065 status
= check_stateid_generation(stateid
, &s
->sc_stateid
, 1);
4068 switch (s
->sc_type
) {
4069 case NFS4_DELEG_STID
:
4071 case NFS4_REVOKED_DELEG_STID
:
4072 return nfserr_deleg_revoked
;
4073 case NFS4_OPEN_STID
:
4074 case NFS4_LOCK_STID
:
4075 ols
= openlockstateid(s
);
4076 if (ols
->st_stateowner
->so_is_open_owner
4077 && !(openowner(ols
->st_stateowner
)->oo_flags
4078 & NFS4_OO_CONFIRMED
))
4079 return nfserr_bad_stateid
;
4082 printk("unknown stateid type %x\n", s
->sc_type
);
4084 case NFS4_CLOSED_STID
:
4085 case NFS4_CLOSED_DELEG_STID
:
4086 return nfserr_bad_stateid
;
4091 nfsd4_lookup_stateid(struct nfsd4_compound_state
*cstate
,
4092 stateid_t
*stateid
, unsigned char typemask
,
4093 struct nfs4_stid
**s
, struct nfsd_net
*nn
)
4097 if (ZERO_STATEID(stateid
) || ONE_STATEID(stateid
))
4098 return nfserr_bad_stateid
;
4099 status
= lookup_clientid(&stateid
->si_opaque
.so_clid
, cstate
, nn
);
4100 if (status
== nfserr_stale_clientid
) {
4101 if (cstate
->session
)
4102 return nfserr_bad_stateid
;
4103 return nfserr_stale_stateid
;
4107 *s
= find_stateid_by_type(cstate
->clp
, stateid
, typemask
);
4109 return nfserr_bad_stateid
;
4114 * Checks for stateid operations
4117 nfs4_preprocess_stateid_op(struct net
*net
, struct nfsd4_compound_state
*cstate
,
4118 stateid_t
*stateid
, int flags
, struct file
**filpp
)
4120 struct nfs4_stid
*s
;
4121 struct nfs4_ol_stateid
*stp
= NULL
;
4122 struct nfs4_delegation
*dp
= NULL
;
4123 struct svc_fh
*current_fh
= &cstate
->current_fh
;
4124 struct inode
*ino
= current_fh
->fh_dentry
->d_inode
;
4125 struct nfsd_net
*nn
= net_generic(net
, nfsd_net_id
);
4126 struct file
*file
= NULL
;
4132 if (grace_disallows_io(net
, ino
))
4133 return nfserr_grace
;
4135 if (ZERO_STATEID(stateid
) || ONE_STATEID(stateid
))
4136 return check_special_stateids(net
, current_fh
, stateid
, flags
);
4140 status
= nfsd4_lookup_stateid(cstate
, stateid
,
4141 NFS4_DELEG_STID
|NFS4_OPEN_STID
|NFS4_LOCK_STID
,
4145 status
= check_stateid_generation(stateid
, &s
->sc_stateid
, nfsd4_has_session(cstate
));
4148 switch (s
->sc_type
) {
4149 case NFS4_DELEG_STID
:
4150 dp
= delegstateid(s
);
4151 status
= nfs4_check_delegmode(dp
, flags
);
4155 file
= dp
->dl_file
->fi_deleg_file
;
4158 status
= nfserr_serverfault
;
4164 case NFS4_OPEN_STID
:
4165 case NFS4_LOCK_STID
:
4166 stp
= openlockstateid(s
);
4167 status
= nfs4_check_fh(current_fh
, stp
);
4170 if (stp
->st_stateowner
->so_is_open_owner
4171 && !(openowner(stp
->st_stateowner
)->oo_flags
& NFS4_OO_CONFIRMED
))
4173 status
= nfs4_check_openmode(stp
, flags
);
4177 if (flags
& RD_STATE
)
4178 file
= find_readable_file(stp
->st_file
);
4180 file
= find_writeable_file(stp
->st_file
);
4184 status
= nfserr_bad_stateid
;
4191 nfs4_unlock_state();
4196 nfsd4_free_lock_stateid(struct nfs4_ol_stateid
*stp
)
4198 struct nfs4_lockowner
*lo
= lockowner(stp
->st_stateowner
);
4200 if (check_for_locks(stp
->st_file
, lo
))
4201 return nfserr_locks_held
;
4202 release_lockowner_if_empty(lo
);
4207 * Test if the stateid is valid
4210 nfsd4_test_stateid(struct svc_rqst
*rqstp
, struct nfsd4_compound_state
*cstate
,
4211 struct nfsd4_test_stateid
*test_stateid
)
4213 struct nfsd4_test_stateid_id
*stateid
;
4214 struct nfs4_client
*cl
= cstate
->session
->se_client
;
4217 list_for_each_entry(stateid
, &test_stateid
->ts_stateid_list
, ts_id_list
)
4218 stateid
->ts_id_status
=
4219 nfsd4_validate_stateid(cl
, &stateid
->ts_id_stateid
);
4220 nfs4_unlock_state();
4226 nfsd4_free_stateid(struct svc_rqst
*rqstp
, struct nfsd4_compound_state
*cstate
,
4227 struct nfsd4_free_stateid
*free_stateid
)
4229 stateid_t
*stateid
= &free_stateid
->fr_stateid
;
4230 struct nfs4_stid
*s
;
4231 struct nfs4_delegation
*dp
;
4232 struct nfs4_client
*cl
= cstate
->session
->se_client
;
4233 __be32 ret
= nfserr_bad_stateid
;
4236 s
= find_stateid(cl
, stateid
);
4239 switch (s
->sc_type
) {
4240 case NFS4_DELEG_STID
:
4241 ret
= nfserr_locks_held
;
4243 case NFS4_OPEN_STID
:
4244 case NFS4_LOCK_STID
:
4245 ret
= check_stateid_generation(stateid
, &s
->sc_stateid
, 1);
4248 if (s
->sc_type
== NFS4_LOCK_STID
)
4249 ret
= nfsd4_free_lock_stateid(openlockstateid(s
));
4251 ret
= nfserr_locks_held
;
4253 case NFS4_REVOKED_DELEG_STID
:
4254 dp
= delegstateid(s
);
4255 spin_lock(&cl
->cl_lock
);
4256 list_del_init(&dp
->dl_recall_lru
);
4257 spin_unlock(&cl
->cl_lock
);
4258 nfs4_put_delegation(dp
);
4262 ret
= nfserr_bad_stateid
;
4265 nfs4_unlock_state();
4272 return (type
== NFS4_READW_LT
|| type
== NFS4_READ_LT
) ?
4273 RD_STATE
: WR_STATE
;
4276 static __be32
nfs4_seqid_op_checks(struct nfsd4_compound_state
*cstate
, stateid_t
*stateid
, u32 seqid
, struct nfs4_ol_stateid
*stp
)
4278 struct svc_fh
*current_fh
= &cstate
->current_fh
;
4279 struct nfs4_stateowner
*sop
= stp
->st_stateowner
;
4282 status
= nfsd4_check_seqid(cstate
, sop
, seqid
);
4285 if (stp
->st_stid
.sc_type
== NFS4_CLOSED_STID
4286 || stp
->st_stid
.sc_type
== NFS4_REVOKED_DELEG_STID
)
4288 * "Closed" stateid's exist *only* to return
4289 * nfserr_replay_me from the previous step, and
4290 * revoked delegations are kept only for free_stateid.
4292 return nfserr_bad_stateid
;
4293 status
= check_stateid_generation(stateid
, &stp
->st_stid
.sc_stateid
, nfsd4_has_session(cstate
));
4296 return nfs4_check_fh(current_fh
, stp
);
4300 * Checks for sequence id mutating operations.
4303 nfs4_preprocess_seqid_op(struct nfsd4_compound_state
*cstate
, u32 seqid
,
4304 stateid_t
*stateid
, char typemask
,
4305 struct nfs4_ol_stateid
**stpp
,
4306 struct nfsd_net
*nn
)
4309 struct nfs4_stid
*s
;
4310 struct nfs4_ol_stateid
*stp
= NULL
;
4312 dprintk("NFSD: %s: seqid=%d stateid = " STATEID_FMT
"\n", __func__
,
4313 seqid
, STATEID_VAL(stateid
));
4316 status
= nfsd4_lookup_stateid(cstate
, stateid
, typemask
, &s
, nn
);
4319 stp
= openlockstateid(s
);
4320 if (!nfsd4_has_session(cstate
))
4321 cstate
->replay_owner
= stp
->st_stateowner
;
4323 status
= nfs4_seqid_op_checks(cstate
, stateid
, seqid
, stp
);
4329 static __be32
nfs4_preprocess_confirmed_seqid_op(struct nfsd4_compound_state
*cstate
, u32 seqid
,
4330 stateid_t
*stateid
, struct nfs4_ol_stateid
**stpp
, struct nfsd_net
*nn
)
4333 struct nfs4_openowner
*oo
;
4335 status
= nfs4_preprocess_seqid_op(cstate
, seqid
, stateid
,
4336 NFS4_OPEN_STID
, stpp
, nn
);
4339 oo
= openowner((*stpp
)->st_stateowner
);
4340 if (!(oo
->oo_flags
& NFS4_OO_CONFIRMED
))
4341 return nfserr_bad_stateid
;
4346 nfsd4_open_confirm(struct svc_rqst
*rqstp
, struct nfsd4_compound_state
*cstate
,
4347 struct nfsd4_open_confirm
*oc
)
4350 struct nfs4_openowner
*oo
;
4351 struct nfs4_ol_stateid
*stp
;
4352 struct nfsd_net
*nn
= net_generic(SVC_NET(rqstp
), nfsd_net_id
);
4354 dprintk("NFSD: nfsd4_open_confirm on file %pd\n",
4355 cstate
->current_fh
.fh_dentry
);
4357 status
= fh_verify(rqstp
, &cstate
->current_fh
, S_IFREG
, 0);
4363 status
= nfs4_preprocess_seqid_op(cstate
,
4364 oc
->oc_seqid
, &oc
->oc_req_stateid
,
4365 NFS4_OPEN_STID
, &stp
, nn
);
4368 oo
= openowner(stp
->st_stateowner
);
4369 status
= nfserr_bad_stateid
;
4370 if (oo
->oo_flags
& NFS4_OO_CONFIRMED
)
4372 oo
->oo_flags
|= NFS4_OO_CONFIRMED
;
4373 update_stateid(&stp
->st_stid
.sc_stateid
);
4374 memcpy(&oc
->oc_resp_stateid
, &stp
->st_stid
.sc_stateid
, sizeof(stateid_t
));
4375 dprintk("NFSD: %s: success, seqid=%d stateid=" STATEID_FMT
"\n",
4376 __func__
, oc
->oc_seqid
, STATEID_VAL(&stp
->st_stid
.sc_stateid
));
4378 nfsd4_client_record_create(oo
->oo_owner
.so_client
);
4381 nfsd4_bump_seqid(cstate
, status
);
4382 if (!cstate
->replay_owner
)
4383 nfs4_unlock_state();
4387 static inline void nfs4_stateid_downgrade_bit(struct nfs4_ol_stateid
*stp
, u32 access
)
4389 if (!test_access(access
, stp
))
4391 nfs4_file_put_access(stp
->st_file
, access
);
4392 clear_access(access
, stp
);
4395 static inline void nfs4_stateid_downgrade(struct nfs4_ol_stateid
*stp
, u32 to_access
)
4397 switch (to_access
) {
4398 case NFS4_SHARE_ACCESS_READ
:
4399 nfs4_stateid_downgrade_bit(stp
, NFS4_SHARE_ACCESS_WRITE
);
4400 nfs4_stateid_downgrade_bit(stp
, NFS4_SHARE_ACCESS_BOTH
);
4402 case NFS4_SHARE_ACCESS_WRITE
:
4403 nfs4_stateid_downgrade_bit(stp
, NFS4_SHARE_ACCESS_READ
);
4404 nfs4_stateid_downgrade_bit(stp
, NFS4_SHARE_ACCESS_BOTH
);
4406 case NFS4_SHARE_ACCESS_BOTH
:
4414 nfsd4_open_downgrade(struct svc_rqst
*rqstp
,
4415 struct nfsd4_compound_state
*cstate
,
4416 struct nfsd4_open_downgrade
*od
)
4419 struct nfs4_ol_stateid
*stp
;
4420 struct nfsd_net
*nn
= net_generic(SVC_NET(rqstp
), nfsd_net_id
);
4422 dprintk("NFSD: nfsd4_open_downgrade on file %pd\n",
4423 cstate
->current_fh
.fh_dentry
);
4425 /* We don't yet support WANT bits: */
4426 if (od
->od_deleg_want
)
4427 dprintk("NFSD: %s: od_deleg_want=0x%x ignored\n", __func__
,
4431 status
= nfs4_preprocess_confirmed_seqid_op(cstate
, od
->od_seqid
,
4432 &od
->od_stateid
, &stp
, nn
);
4435 status
= nfserr_inval
;
4436 if (!test_access(od
->od_share_access
, stp
)) {
4437 dprintk("NFSD: access not a subset of current bitmap: 0x%hhx, input access=%08x\n",
4438 stp
->st_access_bmap
, od
->od_share_access
);
4441 if (!test_deny(od
->od_share_deny
, stp
)) {
4442 dprintk("NFSD: deny not a subset of current bitmap: 0x%hhx, input deny=%08x\n",
4443 stp
->st_deny_bmap
, od
->od_share_deny
);
4446 nfs4_stateid_downgrade(stp
, od
->od_share_access
);
4448 reset_union_bmap_deny(od
->od_share_deny
, stp
);
4450 update_stateid(&stp
->st_stid
.sc_stateid
);
4451 memcpy(&od
->od_stateid
, &stp
->st_stid
.sc_stateid
, sizeof(stateid_t
));
4454 nfsd4_bump_seqid(cstate
, status
);
4455 if (!cstate
->replay_owner
)
4456 nfs4_unlock_state();
4460 static void nfsd4_close_open_stateid(struct nfs4_ol_stateid
*s
)
4462 struct nfs4_client
*clp
= s
->st_stid
.sc_client
;
4463 struct nfs4_openowner
*oo
= openowner(s
->st_stateowner
);
4465 s
->st_stid
.sc_type
= NFS4_CLOSED_STID
;
4466 unhash_open_stateid(s
);
4468 if (clp
->cl_minorversion
) {
4469 free_generic_stateid(s
);
4470 if (list_empty(&oo
->oo_owner
.so_stateids
))
4471 release_openowner(oo
);
4474 put_nfs4_file(s
->st_file
);
4477 oo
->oo_last_closed_stid
= s
;
4479 * In the 4.0 case we need to keep the owners around a
4480 * little while to handle CLOSE replay.
4482 if (list_empty(&oo
->oo_owner
.so_stateids
))
4483 move_to_close_lru(oo
, clp
->net
);
4488 * nfs4_unlock_state() called after encode
4491 nfsd4_close(struct svc_rqst
*rqstp
, struct nfsd4_compound_state
*cstate
,
4492 struct nfsd4_close
*close
)
4495 struct nfs4_ol_stateid
*stp
;
4496 struct net
*net
= SVC_NET(rqstp
);
4497 struct nfsd_net
*nn
= net_generic(net
, nfsd_net_id
);
4499 dprintk("NFSD: nfsd4_close on file %pd\n",
4500 cstate
->current_fh
.fh_dentry
);
4503 status
= nfs4_preprocess_seqid_op(cstate
, close
->cl_seqid
,
4505 NFS4_OPEN_STID
|NFS4_CLOSED_STID
,
4507 nfsd4_bump_seqid(cstate
, status
);
4510 update_stateid(&stp
->st_stid
.sc_stateid
);
4511 memcpy(&close
->cl_stateid
, &stp
->st_stid
.sc_stateid
, sizeof(stateid_t
));
4513 nfsd4_close_open_stateid(stp
);
4515 if (!cstate
->replay_owner
)
4516 nfs4_unlock_state();
4521 nfsd4_delegreturn(struct svc_rqst
*rqstp
, struct nfsd4_compound_state
*cstate
,
4522 struct nfsd4_delegreturn
*dr
)
4524 struct nfs4_delegation
*dp
;
4525 stateid_t
*stateid
= &dr
->dr_stateid
;
4526 struct nfs4_stid
*s
;
4528 struct nfsd_net
*nn
= net_generic(SVC_NET(rqstp
), nfsd_net_id
);
4530 if ((status
= fh_verify(rqstp
, &cstate
->current_fh
, S_IFREG
, 0)))
4534 status
= nfsd4_lookup_stateid(cstate
, stateid
, NFS4_DELEG_STID
, &s
, nn
);
4537 dp
= delegstateid(s
);
4538 status
= check_stateid_generation(stateid
, &dp
->dl_stid
.sc_stateid
, nfsd4_has_session(cstate
));
4542 destroy_delegation(dp
);
4544 nfs4_unlock_state();
4550 #define LOFF_OVERFLOW(start, len) ((u64)(len) > ~(u64)(start))
4553 end_offset(u64 start
, u64 len
)
4558 return end
>= start
? end
: NFS4_MAX_UINT64
;
4561 /* last octet in a range */
4563 last_byte_offset(u64 start
, u64 len
)
4569 return end
> start
? end
- 1: NFS4_MAX_UINT64
;
4573 * TODO: Linux file offsets are _signed_ 64-bit quantities, which means that
4574 * we can't properly handle lock requests that go beyond the (2^63 - 1)-th
4575 * byte, because of sign extension problems. Since NFSv4 calls for 64-bit
4576 * locking, this prevents us from being completely protocol-compliant. The
4577 * real solution to this problem is to start using unsigned file offsets in
4578 * the VFS, but this is a very deep change!
4581 nfs4_transform_lock_offset(struct file_lock
*lock
)
4583 if (lock
->fl_start
< 0)
4584 lock
->fl_start
= OFFSET_MAX
;
4585 if (lock
->fl_end
< 0)
4586 lock
->fl_end
= OFFSET_MAX
;
4589 /* Hack!: For now, we're defining this just so we can use a pointer to it
4590 * as a unique cookie to identify our (NFSv4's) posix locks. */
4591 static const struct lock_manager_operations nfsd_posix_mng_ops
= {
4595 nfs4_set_lock_denied(struct file_lock
*fl
, struct nfsd4_lock_denied
*deny
)
4597 struct nfs4_lockowner
*lo
;
4599 if (fl
->fl_lmops
== &nfsd_posix_mng_ops
) {
4600 lo
= (struct nfs4_lockowner
*) fl
->fl_owner
;
4601 deny
->ld_owner
.data
= kmemdup(lo
->lo_owner
.so_owner
.data
,
4602 lo
->lo_owner
.so_owner
.len
, GFP_KERNEL
);
4603 if (!deny
->ld_owner
.data
)
4604 /* We just don't care that much */
4606 deny
->ld_owner
.len
= lo
->lo_owner
.so_owner
.len
;
4607 deny
->ld_clientid
= lo
->lo_owner
.so_client
->cl_clientid
;
4610 deny
->ld_owner
.len
= 0;
4611 deny
->ld_owner
.data
= NULL
;
4612 deny
->ld_clientid
.cl_boot
= 0;
4613 deny
->ld_clientid
.cl_id
= 0;
4615 deny
->ld_start
= fl
->fl_start
;
4616 deny
->ld_length
= NFS4_MAX_UINT64
;
4617 if (fl
->fl_end
!= NFS4_MAX_UINT64
)
4618 deny
->ld_length
= fl
->fl_end
- fl
->fl_start
+ 1;
4619 deny
->ld_type
= NFS4_READ_LT
;
4620 if (fl
->fl_type
!= F_RDLCK
)
4621 deny
->ld_type
= NFS4_WRITE_LT
;
4624 static struct nfs4_lockowner
*
4625 find_lockowner_str(clientid_t
*clid
, struct xdr_netobj
*owner
,
4626 struct nfsd_net
*nn
)
4628 unsigned int strhashval
= ownerstr_hashval(clid
->cl_id
, owner
);
4629 struct nfs4_stateowner
*so
;
4631 list_for_each_entry(so
, &nn
->ownerstr_hashtbl
[strhashval
], so_strhash
) {
4632 if (so
->so_is_open_owner
)
4634 if (!same_owner_str(so
, owner
, clid
))
4636 return lockowner(so
);
4642 * Alloc a lock owner structure.
4643 * Called in nfsd4_lock - therefore, OPEN and OPEN_CONFIRM (if needed) has
4646 * strhashval = ownerstr_hashval
4648 static struct nfs4_lockowner
*
4649 alloc_init_lock_stateowner(unsigned int strhashval
, struct nfs4_client
*clp
, struct nfs4_ol_stateid
*open_stp
, struct nfsd4_lock
*lock
) {
4650 struct nfs4_lockowner
*lo
;
4651 struct nfsd_net
*nn
= net_generic(clp
->net
, nfsd_net_id
);
4653 lo
= alloc_stateowner(lockowner_slab
, &lock
->lk_new_owner
, clp
);
4656 INIT_LIST_HEAD(&lo
->lo_owner
.so_stateids
);
4657 lo
->lo_owner
.so_is_open_owner
= 0;
4658 /* It is the openowner seqid that will be incremented in encode in the
4659 * case of new lockowners; so increment the lock seqid manually: */
4660 lo
->lo_owner
.so_seqid
= lock
->lk_new_lock_seqid
+ 1;
4661 list_add(&lo
->lo_owner
.so_strhash
, &nn
->ownerstr_hashtbl
[strhashval
]);
4665 static struct nfs4_ol_stateid
*
4666 alloc_init_lock_stateid(struct nfs4_lockowner
*lo
, struct nfs4_file
*fp
,
4667 struct inode
*inode
,
4668 struct nfs4_ol_stateid
*open_stp
)
4670 struct nfs4_ol_stateid
*stp
;
4671 struct nfs4_client
*clp
= lo
->lo_owner
.so_client
;
4673 stp
= nfs4_alloc_stateid(clp
);
4676 stp
->st_stid
.sc_type
= NFS4_LOCK_STID
;
4677 list_add(&stp
->st_perstateowner
, &lo
->lo_owner
.so_stateids
);
4678 stp
->st_stateowner
= &lo
->lo_owner
;
4681 stp
->st_access_bmap
= 0;
4682 stp
->st_deny_bmap
= open_stp
->st_deny_bmap
;
4683 stp
->st_openstp
= open_stp
;
4684 list_add(&stp
->st_locks
, &open_stp
->st_locks
);
4685 spin_lock(&fp
->fi_lock
);
4686 list_add(&stp
->st_perfile
, &fp
->fi_stateids
);
4687 spin_unlock(&fp
->fi_lock
);
4691 static struct nfs4_ol_stateid
*
4692 find_lock_stateid(struct nfs4_lockowner
*lo
, struct nfs4_file
*fp
)
4694 struct nfs4_ol_stateid
*lst
;
4696 list_for_each_entry(lst
, &lo
->lo_owner
.so_stateids
, st_perstateowner
) {
4697 if (lst
->st_file
== fp
)
4705 check_lock_length(u64 offset
, u64 length
)
4707 return ((length
== 0) || ((length
!= NFS4_MAX_UINT64
) &&
4708 LOFF_OVERFLOW(offset
, length
)));
4711 static void get_lock_access(struct nfs4_ol_stateid
*lock_stp
, u32 access
)
4713 struct nfs4_file
*fp
= lock_stp
->st_file
;
4715 lockdep_assert_held(&fp
->fi_lock
);
4717 if (test_access(access
, lock_stp
))
4719 __nfs4_file_get_access(fp
, access
);
4720 set_access(access
, lock_stp
);
4723 static __be32
lookup_or_create_lock_state(struct nfsd4_compound_state
*cstate
, struct nfs4_ol_stateid
*ost
, struct nfsd4_lock
*lock
, struct nfs4_ol_stateid
**lst
, bool *new)
4725 struct nfs4_file
*fi
= ost
->st_file
;
4726 struct nfs4_openowner
*oo
= openowner(ost
->st_stateowner
);
4727 struct nfs4_client
*cl
= oo
->oo_owner
.so_client
;
4728 struct inode
*inode
= cstate
->current_fh
.fh_dentry
->d_inode
;
4729 struct nfs4_lockowner
*lo
;
4730 unsigned int strhashval
;
4731 struct nfsd_net
*nn
= net_generic(cl
->net
, nfsd_net_id
);
4733 lo
= find_lockowner_str(&cl
->cl_clientid
, &lock
->v
.new.owner
, nn
);
4735 strhashval
= ownerstr_hashval(cl
->cl_clientid
.cl_id
,
4736 &lock
->v
.new.owner
);
4737 lo
= alloc_init_lock_stateowner(strhashval
, cl
, ost
, lock
);
4739 return nfserr_jukebox
;
4741 /* with an existing lockowner, seqids must be the same */
4742 if (!cstate
->minorversion
&&
4743 lock
->lk_new_lock_seqid
!= lo
->lo_owner
.so_seqid
)
4744 return nfserr_bad_seqid
;
4747 *lst
= find_lock_stateid(lo
, fi
);
4749 *lst
= alloc_init_lock_stateid(lo
, fi
, inode
, ost
);
4751 release_lockowner_if_empty(lo
);
4752 return nfserr_jukebox
;
4763 nfsd4_lock(struct svc_rqst
*rqstp
, struct nfsd4_compound_state
*cstate
,
4764 struct nfsd4_lock
*lock
)
4766 struct nfs4_openowner
*open_sop
= NULL
;
4767 struct nfs4_lockowner
*lock_sop
= NULL
;
4768 struct nfs4_ol_stateid
*lock_stp
;
4769 struct nfs4_file
*fp
;
4770 struct file
*filp
= NULL
;
4771 struct file_lock
*file_lock
= NULL
;
4772 struct file_lock
*conflock
= NULL
;
4774 bool new_state
= false;
4777 struct net
*net
= SVC_NET(rqstp
);
4778 struct nfsd_net
*nn
= net_generic(net
, nfsd_net_id
);
4780 dprintk("NFSD: nfsd4_lock: start=%Ld length=%Ld\n",
4781 (long long) lock
->lk_offset
,
4782 (long long) lock
->lk_length
);
4784 if (check_lock_length(lock
->lk_offset
, lock
->lk_length
))
4785 return nfserr_inval
;
4787 if ((status
= fh_verify(rqstp
, &cstate
->current_fh
,
4788 S_IFREG
, NFSD_MAY_LOCK
))) {
4789 dprintk("NFSD: nfsd4_lock: permission denied!\n");
4795 if (lock
->lk_is_new
) {
4796 struct nfs4_ol_stateid
*open_stp
= NULL
;
4798 if (nfsd4_has_session(cstate
))
4799 /* See rfc 5661 18.10.3: given clientid is ignored: */
4800 memcpy(&lock
->v
.new.clientid
,
4801 &cstate
->session
->se_client
->cl_clientid
,
4802 sizeof(clientid_t
));
4804 status
= nfserr_stale_clientid
;
4805 if (STALE_CLIENTID(&lock
->lk_new_clientid
, nn
))
4808 /* validate and update open stateid and open seqid */
4809 status
= nfs4_preprocess_confirmed_seqid_op(cstate
,
4810 lock
->lk_new_open_seqid
,
4811 &lock
->lk_new_open_stateid
,
4815 open_sop
= openowner(open_stp
->st_stateowner
);
4816 status
= nfserr_bad_stateid
;
4817 if (!same_clid(&open_sop
->oo_owner
.so_client
->cl_clientid
,
4818 &lock
->v
.new.clientid
))
4820 status
= lookup_or_create_lock_state(cstate
, open_stp
, lock
,
4821 &lock_stp
, &new_state
);
4823 status
= nfs4_preprocess_seqid_op(cstate
,
4824 lock
->lk_old_lock_seqid
,
4825 &lock
->lk_old_lock_stateid
,
4826 NFS4_LOCK_STID
, &lock_stp
, nn
);
4829 lock_sop
= lockowner(lock_stp
->st_stateowner
);
4831 lkflg
= setlkflg(lock
->lk_type
);
4832 status
= nfs4_check_openmode(lock_stp
, lkflg
);
4836 status
= nfserr_grace
;
4837 if (locks_in_grace(net
) && !lock
->lk_reclaim
)
4839 status
= nfserr_no_grace
;
4840 if (!locks_in_grace(net
) && lock
->lk_reclaim
)
4843 file_lock
= locks_alloc_lock();
4845 dprintk("NFSD: %s: unable to allocate lock!\n", __func__
);
4846 status
= nfserr_jukebox
;
4850 fp
= lock_stp
->st_file
;
4851 locks_init_lock(file_lock
);
4852 switch (lock
->lk_type
) {
4855 spin_lock(&fp
->fi_lock
);
4856 filp
= find_readable_file_locked(fp
);
4858 get_lock_access(lock_stp
, NFS4_SHARE_ACCESS_READ
);
4859 spin_unlock(&fp
->fi_lock
);
4860 file_lock
->fl_type
= F_RDLCK
;
4863 case NFS4_WRITEW_LT
:
4864 spin_lock(&fp
->fi_lock
);
4865 filp
= find_writeable_file_locked(fp
);
4867 get_lock_access(lock_stp
, NFS4_SHARE_ACCESS_WRITE
);
4868 spin_unlock(&fp
->fi_lock
);
4869 file_lock
->fl_type
= F_WRLCK
;
4872 status
= nfserr_inval
;
4876 status
= nfserr_openmode
;
4879 file_lock
->fl_owner
= (fl_owner_t
)lock_sop
;
4880 file_lock
->fl_pid
= current
->tgid
;
4881 file_lock
->fl_file
= filp
;
4882 file_lock
->fl_flags
= FL_POSIX
;
4883 file_lock
->fl_lmops
= &nfsd_posix_mng_ops
;
4884 file_lock
->fl_start
= lock
->lk_offset
;
4885 file_lock
->fl_end
= last_byte_offset(lock
->lk_offset
, lock
->lk_length
);
4886 nfs4_transform_lock_offset(file_lock
);
4888 conflock
= locks_alloc_lock();
4890 dprintk("NFSD: %s: unable to allocate lock!\n", __func__
);
4891 status
= nfserr_jukebox
;
4895 err
= vfs_lock_file(filp
, F_SETLK
, file_lock
, conflock
);
4897 case 0: /* success! */
4898 update_stateid(&lock_stp
->st_stid
.sc_stateid
);
4899 memcpy(&lock
->lk_resp_stateid
, &lock_stp
->st_stid
.sc_stateid
,
4903 case (EAGAIN
): /* conflock holds conflicting lock */
4904 status
= nfserr_denied
;
4905 dprintk("NFSD: nfsd4_lock: conflicting lock found!\n");
4906 nfs4_set_lock_denied(conflock
, &lock
->lk_denied
);
4909 status
= nfserr_deadlock
;
4912 dprintk("NFSD: nfsd4_lock: vfs_lock_file() failed! status %d\n",err
);
4913 status
= nfserrno(err
);
4919 if (status
&& new_state
)
4920 release_lock_stateid(lock_stp
);
4921 nfsd4_bump_seqid(cstate
, status
);
4922 if (!cstate
->replay_owner
)
4923 nfs4_unlock_state();
4925 locks_free_lock(file_lock
);
4927 locks_free_lock(conflock
);
4932 * The NFSv4 spec allows a client to do a LOCKT without holding an OPEN,
4933 * so we do a temporary open here just to get an open file to pass to
4934 * vfs_test_lock. (Arguably perhaps test_lock should be done with an
4937 static __be32
nfsd_test_lock(struct svc_rqst
*rqstp
, struct svc_fh
*fhp
, struct file_lock
*lock
)
4940 __be32 err
= nfsd_open(rqstp
, fhp
, S_IFREG
, NFSD_MAY_READ
, &file
);
4942 err
= nfserrno(vfs_test_lock(file
, lock
));
4952 nfsd4_lockt(struct svc_rqst
*rqstp
, struct nfsd4_compound_state
*cstate
,
4953 struct nfsd4_lockt
*lockt
)
4955 struct file_lock
*file_lock
= NULL
;
4956 struct nfs4_lockowner
*lo
;
4958 struct nfsd_net
*nn
= net_generic(SVC_NET(rqstp
), nfsd_net_id
);
4960 if (locks_in_grace(SVC_NET(rqstp
)))
4961 return nfserr_grace
;
4963 if (check_lock_length(lockt
->lt_offset
, lockt
->lt_length
))
4964 return nfserr_inval
;
4968 if (!nfsd4_has_session(cstate
)) {
4969 status
= lookup_clientid(&lockt
->lt_clientid
, cstate
, nn
);
4974 if ((status
= fh_verify(rqstp
, &cstate
->current_fh
, S_IFREG
, 0)))
4977 file_lock
= locks_alloc_lock();
4979 dprintk("NFSD: %s: unable to allocate lock!\n", __func__
);
4980 status
= nfserr_jukebox
;
4983 locks_init_lock(file_lock
);
4984 switch (lockt
->lt_type
) {
4987 file_lock
->fl_type
= F_RDLCK
;
4990 case NFS4_WRITEW_LT
:
4991 file_lock
->fl_type
= F_WRLCK
;
4994 dprintk("NFSD: nfs4_lockt: bad lock type!\n");
4995 status
= nfserr_inval
;
4999 lo
= find_lockowner_str(&lockt
->lt_clientid
, &lockt
->lt_owner
, nn
);
5001 file_lock
->fl_owner
= (fl_owner_t
)lo
;
5002 file_lock
->fl_pid
= current
->tgid
;
5003 file_lock
->fl_flags
= FL_POSIX
;
5005 file_lock
->fl_start
= lockt
->lt_offset
;
5006 file_lock
->fl_end
= last_byte_offset(lockt
->lt_offset
, lockt
->lt_length
);
5008 nfs4_transform_lock_offset(file_lock
);
5010 status
= nfsd_test_lock(rqstp
, &cstate
->current_fh
, file_lock
);
5014 if (file_lock
->fl_type
!= F_UNLCK
) {
5015 status
= nfserr_denied
;
5016 nfs4_set_lock_denied(file_lock
, &lockt
->lt_denied
);
5019 nfs4_unlock_state();
5021 locks_free_lock(file_lock
);
5026 nfsd4_locku(struct svc_rqst
*rqstp
, struct nfsd4_compound_state
*cstate
,
5027 struct nfsd4_locku
*locku
)
5029 struct nfs4_ol_stateid
*stp
;
5030 struct file
*filp
= NULL
;
5031 struct file_lock
*file_lock
= NULL
;
5034 struct nfsd_net
*nn
= net_generic(SVC_NET(rqstp
), nfsd_net_id
);
5036 dprintk("NFSD: nfsd4_locku: start=%Ld length=%Ld\n",
5037 (long long) locku
->lu_offset
,
5038 (long long) locku
->lu_length
);
5040 if (check_lock_length(locku
->lu_offset
, locku
->lu_length
))
5041 return nfserr_inval
;
5045 status
= nfs4_preprocess_seqid_op(cstate
, locku
->lu_seqid
,
5046 &locku
->lu_stateid
, NFS4_LOCK_STID
,
5050 filp
= find_any_file(stp
->st_file
);
5052 status
= nfserr_lock_range
;
5055 file_lock
= locks_alloc_lock();
5057 dprintk("NFSD: %s: unable to allocate lock!\n", __func__
);
5058 status
= nfserr_jukebox
;
5061 locks_init_lock(file_lock
);
5062 file_lock
->fl_type
= F_UNLCK
;
5063 file_lock
->fl_owner
= (fl_owner_t
)lockowner(stp
->st_stateowner
);
5064 file_lock
->fl_pid
= current
->tgid
;
5065 file_lock
->fl_file
= filp
;
5066 file_lock
->fl_flags
= FL_POSIX
;
5067 file_lock
->fl_lmops
= &nfsd_posix_mng_ops
;
5068 file_lock
->fl_start
= locku
->lu_offset
;
5070 file_lock
->fl_end
= last_byte_offset(locku
->lu_offset
,
5072 nfs4_transform_lock_offset(file_lock
);
5074 err
= vfs_lock_file(filp
, F_SETLK
, file_lock
, NULL
);
5076 dprintk("NFSD: nfs4_locku: vfs_lock_file failed!\n");
5079 update_stateid(&stp
->st_stid
.sc_stateid
);
5080 memcpy(&locku
->lu_stateid
, &stp
->st_stid
.sc_stateid
, sizeof(stateid_t
));
5084 nfsd4_bump_seqid(cstate
, status
);
5085 if (!cstate
->replay_owner
)
5086 nfs4_unlock_state();
5088 locks_free_lock(file_lock
);
5092 status
= nfserrno(err
);
5098 * true: locks held by lockowner
5099 * false: no locks held by lockowner
5102 check_for_locks(struct nfs4_file
*fp
, struct nfs4_lockowner
*lowner
)
5104 struct file_lock
**flpp
;
5106 struct file
*filp
= find_any_file(fp
);
5107 struct inode
*inode
;
5110 /* Any valid lock stateid should have some sort of access */
5115 inode
= file_inode(filp
);
5117 spin_lock(&inode
->i_lock
);
5118 for (flpp
= &inode
->i_flock
; *flpp
!= NULL
; flpp
= &(*flpp
)->fl_next
) {
5119 if ((*flpp
)->fl_owner
== (fl_owner_t
)lowner
) {
5124 spin_unlock(&inode
->i_lock
);
5130 nfsd4_release_lockowner(struct svc_rqst
*rqstp
,
5131 struct nfsd4_compound_state
*cstate
,
5132 struct nfsd4_release_lockowner
*rlockowner
)
5134 clientid_t
*clid
= &rlockowner
->rl_clientid
;
5135 struct nfs4_stateowner
*sop
= NULL
, *tmp
;
5136 struct nfs4_lockowner
*lo
;
5137 struct nfs4_ol_stateid
*stp
;
5138 struct xdr_netobj
*owner
= &rlockowner
->rl_owner
;
5139 unsigned int hashval
= ownerstr_hashval(clid
->cl_id
, owner
);
5141 struct nfsd_net
*nn
= net_generic(SVC_NET(rqstp
), nfsd_net_id
);
5143 dprintk("nfsd4_release_lockowner clientid: (%08x/%08x):\n",
5144 clid
->cl_boot
, clid
->cl_id
);
5148 status
= lookup_clientid(clid
, cstate
, nn
);
5152 status
= nfserr_locks_held
;
5154 /* Find the matching lock stateowner */
5155 list_for_each_entry(tmp
, &nn
->ownerstr_hashtbl
[hashval
], so_strhash
) {
5156 if (tmp
->so_is_open_owner
)
5158 if (same_owner_str(tmp
, owner
, clid
)) {
5164 /* No matching owner found, maybe a replay? Just declare victory... */
5170 lo
= lockowner(sop
);
5171 /* see if there are still any locks associated with it */
5172 list_for_each_entry(stp
, &sop
->so_stateids
, st_perstateowner
) {
5173 if (check_for_locks(stp
->st_file
, lo
))
5178 release_lockowner(lo
);
5180 nfs4_unlock_state();
5184 static inline struct nfs4_client_reclaim
*
5187 return kmalloc(sizeof(struct nfs4_client_reclaim
), GFP_KERNEL
);
5191 nfs4_has_reclaimed_state(const char *name
, struct nfsd_net
*nn
)
5193 struct nfs4_client_reclaim
*crp
;
5195 crp
= nfsd4_find_reclaim_client(name
, nn
);
5196 return (crp
&& crp
->cr_clp
);
5200 * failure => all reset bets are off, nfserr_no_grace...
5202 struct nfs4_client_reclaim
*
5203 nfs4_client_to_reclaim(const char *name
, struct nfsd_net
*nn
)
5205 unsigned int strhashval
;
5206 struct nfs4_client_reclaim
*crp
;
5208 dprintk("NFSD nfs4_client_to_reclaim NAME: %.*s\n", HEXDIR_LEN
, name
);
5209 crp
= alloc_reclaim();
5211 strhashval
= clientstr_hashval(name
);
5212 INIT_LIST_HEAD(&crp
->cr_strhash
);
5213 list_add(&crp
->cr_strhash
, &nn
->reclaim_str_hashtbl
[strhashval
]);
5214 memcpy(crp
->cr_recdir
, name
, HEXDIR_LEN
);
5216 nn
->reclaim_str_hashtbl_size
++;
5222 nfs4_remove_reclaim_record(struct nfs4_client_reclaim
*crp
, struct nfsd_net
*nn
)
5224 list_del(&crp
->cr_strhash
);
5226 nn
->reclaim_str_hashtbl_size
--;
5230 nfs4_release_reclaim(struct nfsd_net
*nn
)
5232 struct nfs4_client_reclaim
*crp
= NULL
;
5235 for (i
= 0; i
< CLIENT_HASH_SIZE
; i
++) {
5236 while (!list_empty(&nn
->reclaim_str_hashtbl
[i
])) {
5237 crp
= list_entry(nn
->reclaim_str_hashtbl
[i
].next
,
5238 struct nfs4_client_reclaim
, cr_strhash
);
5239 nfs4_remove_reclaim_record(crp
, nn
);
5242 WARN_ON_ONCE(nn
->reclaim_str_hashtbl_size
);
5246 * called from OPEN, CLAIM_PREVIOUS with a new clientid. */
5247 struct nfs4_client_reclaim
*
5248 nfsd4_find_reclaim_client(const char *recdir
, struct nfsd_net
*nn
)
5250 unsigned int strhashval
;
5251 struct nfs4_client_reclaim
*crp
= NULL
;
5253 dprintk("NFSD: nfs4_find_reclaim_client for recdir %s\n", recdir
);
5255 strhashval
= clientstr_hashval(recdir
);
5256 list_for_each_entry(crp
, &nn
->reclaim_str_hashtbl
[strhashval
], cr_strhash
) {
5257 if (same_name(crp
->cr_recdir
, recdir
)) {
5265 * Called from OPEN. Look for clientid in reclaim list.
5268 nfs4_check_open_reclaim(clientid_t
*clid
,
5269 struct nfsd4_compound_state
*cstate
,
5270 struct nfsd_net
*nn
)
5274 /* find clientid in conf_id_hashtbl */
5275 status
= lookup_clientid(clid
, cstate
, nn
);
5277 return nfserr_reclaim_bad
;
5279 if (nfsd4_client_record_check(cstate
->clp
))
5280 return nfserr_reclaim_bad
;
5285 #ifdef CONFIG_NFSD_FAULT_INJECTION
5287 u64
nfsd_forget_client(struct nfs4_client
*clp
, u64 max
)
5289 if (mark_client_expired(clp
))
5295 u64
nfsd_print_client(struct nfs4_client
*clp
, u64 num
)
5297 char buf
[INET6_ADDRSTRLEN
];
5298 rpc_ntop((struct sockaddr
*)&clp
->cl_addr
, buf
, sizeof(buf
));
5299 printk(KERN_INFO
"NFS Client: %s\n", buf
);
5303 static void nfsd_print_count(struct nfs4_client
*clp
, unsigned int count
,
5306 char buf
[INET6_ADDRSTRLEN
];
5307 rpc_ntop((struct sockaddr
*)&clp
->cl_addr
, buf
, sizeof(buf
));
5308 printk(KERN_INFO
"NFS Client: %s has %u %s\n", buf
, count
, type
);
5311 static u64
nfsd_foreach_client_lock(struct nfs4_client
*clp
, u64 max
,
5312 void (*func
)(struct nfs4_ol_stateid
*))
5314 struct nfs4_openowner
*oop
;
5315 struct nfs4_ol_stateid
*stp
, *st_next
;
5316 struct nfs4_ol_stateid
*lst
, *lst_next
;
5319 list_for_each_entry(oop
, &clp
->cl_openowners
, oo_perclient
) {
5320 list_for_each_entry_safe(stp
, st_next
,
5321 &oop
->oo_owner
.so_stateids
, st_perstateowner
) {
5322 list_for_each_entry_safe(lst
, lst_next
,
5323 &stp
->st_locks
, st_locks
) {
5335 u64
nfsd_forget_client_locks(struct nfs4_client
*clp
, u64 max
)
5337 return nfsd_foreach_client_lock(clp
, max
, release_lock_stateid
);
5340 u64
nfsd_print_client_locks(struct nfs4_client
*clp
, u64 max
)
5342 u64 count
= nfsd_foreach_client_lock(clp
, max
, NULL
);
5343 nfsd_print_count(clp
, count
, "locked files");
5347 static u64
nfsd_foreach_client_open(struct nfs4_client
*clp
, u64 max
, void (*func
)(struct nfs4_openowner
*))
5349 struct nfs4_openowner
*oop
, *next
;
5352 list_for_each_entry_safe(oop
, next
, &clp
->cl_openowners
, oo_perclient
) {
5362 u64
nfsd_forget_client_openowners(struct nfs4_client
*clp
, u64 max
)
5364 return nfsd_foreach_client_open(clp
, max
, release_openowner
);
5367 u64
nfsd_print_client_openowners(struct nfs4_client
*clp
, u64 max
)
5369 u64 count
= nfsd_foreach_client_open(clp
, max
, NULL
);
5370 nfsd_print_count(clp
, count
, "open files");
5374 static u64
nfsd_find_all_delegations(struct nfs4_client
*clp
, u64 max
,
5375 struct list_head
*victims
)
5377 struct nfs4_delegation
*dp
, *next
;
5380 lockdep_assert_held(&state_lock
);
5381 list_for_each_entry_safe(dp
, next
, &clp
->cl_delegations
, dl_perclnt
) {
5384 * It's not safe to mess with delegations that have a
5385 * non-zero dl_time. They might have already been broken
5386 * and could be processed by the laundromat outside of
5387 * the state_lock. Just leave them be.
5389 if (dp
->dl_time
!= 0)
5392 unhash_delegation_locked(dp
);
5393 list_add(&dp
->dl_recall_lru
, victims
);
5401 u64
nfsd_forget_client_delegations(struct nfs4_client
*clp
, u64 max
)
5403 struct nfs4_delegation
*dp
, *next
;
5407 spin_lock(&state_lock
);
5408 count
= nfsd_find_all_delegations(clp
, max
, &victims
);
5409 spin_unlock(&state_lock
);
5411 list_for_each_entry_safe(dp
, next
, &victims
, dl_recall_lru
) {
5412 list_del_init(&dp
->dl_recall_lru
);
5413 revoke_delegation(dp
);
5419 u64
nfsd_recall_client_delegations(struct nfs4_client
*clp
, u64 max
)
5421 struct nfs4_delegation
*dp
;
5425 spin_lock(&state_lock
);
5426 count
= nfsd_find_all_delegations(clp
, max
, &victims
);
5427 while (!list_empty(&victims
)) {
5428 dp
= list_first_entry(&victims
, struct nfs4_delegation
,
5430 list_del_init(&dp
->dl_recall_lru
);
5432 nfsd_break_one_deleg(dp
);
5434 spin_unlock(&state_lock
);
5439 u64
nfsd_print_client_delegations(struct nfs4_client
*clp
, u64 max
)
5443 spin_lock(&state_lock
);
5444 count
= nfsd_find_all_delegations(clp
, max
, NULL
);
5445 spin_unlock(&state_lock
);
5447 nfsd_print_count(clp
, count
, "delegations");
5451 u64
nfsd_for_n_state(u64 max
, u64 (*func
)(struct nfs4_client
*, u64
))
5453 struct nfs4_client
*clp
, *next
;
5455 struct nfsd_net
*nn
= net_generic(current
->nsproxy
->net_ns
, nfsd_net_id
);
5457 if (!nfsd_netns_ready(nn
))
5460 list_for_each_entry_safe(clp
, next
, &nn
->client_lru
, cl_lru
) {
5461 count
+= func(clp
, max
- count
);
5462 if ((max
!= 0) && (count
>= max
))
5469 struct nfs4_client
*nfsd_find_client(struct sockaddr_storage
*addr
, size_t addr_size
)
5471 struct nfs4_client
*clp
;
5472 struct nfsd_net
*nn
= net_generic(current
->nsproxy
->net_ns
, nfsd_net_id
);
5474 if (!nfsd_netns_ready(nn
))
5477 list_for_each_entry(clp
, &nn
->client_lru
, cl_lru
) {
5478 if (memcmp(&clp
->cl_addr
, addr
, addr_size
) == 0)
5484 #endif /* CONFIG_NFSD_FAULT_INJECTION */
5487 * Since the lifetime of a delegation isn't limited to that of an open, a
5488 * client may quite reasonably hang on to a delegation as long as it has
5489 * the inode cached. This becomes an obvious problem the first time a
5490 * client's inode cache approaches the size of the server's total memory.
5492 * For now we avoid this problem by imposing a hard limit on the number
5493 * of delegations, which varies according to the server's memory size.
5496 set_max_delegations(void)
5499 * Allow at most 4 delegations per megabyte of RAM. Quick
5500 * estimates suggest that in the worst case (where every delegation
5501 * is for a different inode), a delegation could take about 1.5K,
5502 * giving a worst case usage of about 6% of memory.
5504 max_delegations
= nr_free_buffer_pages() >> (20 - 2 - PAGE_SHIFT
);
5507 static int nfs4_state_create_net(struct net
*net
)
5509 struct nfsd_net
*nn
= net_generic(net
, nfsd_net_id
);
5512 nn
->conf_id_hashtbl
= kmalloc(sizeof(struct list_head
) *
5513 CLIENT_HASH_SIZE
, GFP_KERNEL
);
5514 if (!nn
->conf_id_hashtbl
)
5516 nn
->unconf_id_hashtbl
= kmalloc(sizeof(struct list_head
) *
5517 CLIENT_HASH_SIZE
, GFP_KERNEL
);
5518 if (!nn
->unconf_id_hashtbl
)
5520 nn
->ownerstr_hashtbl
= kmalloc(sizeof(struct list_head
) *
5521 OWNER_HASH_SIZE
, GFP_KERNEL
);
5522 if (!nn
->ownerstr_hashtbl
)
5524 nn
->sessionid_hashtbl
= kmalloc(sizeof(struct list_head
) *
5525 SESSION_HASH_SIZE
, GFP_KERNEL
);
5526 if (!nn
->sessionid_hashtbl
)
5529 for (i
= 0; i
< CLIENT_HASH_SIZE
; i
++) {
5530 INIT_LIST_HEAD(&nn
->conf_id_hashtbl
[i
]);
5531 INIT_LIST_HEAD(&nn
->unconf_id_hashtbl
[i
]);
5533 for (i
= 0; i
< OWNER_HASH_SIZE
; i
++)
5534 INIT_LIST_HEAD(&nn
->ownerstr_hashtbl
[i
]);
5535 for (i
= 0; i
< SESSION_HASH_SIZE
; i
++)
5536 INIT_LIST_HEAD(&nn
->sessionid_hashtbl
[i
]);
5537 nn
->conf_name_tree
= RB_ROOT
;
5538 nn
->unconf_name_tree
= RB_ROOT
;
5539 INIT_LIST_HEAD(&nn
->client_lru
);
5540 INIT_LIST_HEAD(&nn
->close_lru
);
5541 INIT_LIST_HEAD(&nn
->del_recall_lru
);
5542 spin_lock_init(&nn
->client_lock
);
5544 INIT_DELAYED_WORK(&nn
->laundromat_work
, laundromat_main
);
5550 kfree(nn
->ownerstr_hashtbl
);
5552 kfree(nn
->unconf_id_hashtbl
);
5554 kfree(nn
->conf_id_hashtbl
);
5560 nfs4_state_destroy_net(struct net
*net
)
5563 struct nfs4_client
*clp
= NULL
;
5564 struct nfsd_net
*nn
= net_generic(net
, nfsd_net_id
);
5566 for (i
= 0; i
< CLIENT_HASH_SIZE
; i
++) {
5567 while (!list_empty(&nn
->conf_id_hashtbl
[i
])) {
5568 clp
= list_entry(nn
->conf_id_hashtbl
[i
].next
, struct nfs4_client
, cl_idhash
);
5569 destroy_client(clp
);
5573 for (i
= 0; i
< CLIENT_HASH_SIZE
; i
++) {
5574 while (!list_empty(&nn
->unconf_id_hashtbl
[i
])) {
5575 clp
= list_entry(nn
->unconf_id_hashtbl
[i
].next
, struct nfs4_client
, cl_idhash
);
5576 destroy_client(clp
);
5580 kfree(nn
->sessionid_hashtbl
);
5581 kfree(nn
->ownerstr_hashtbl
);
5582 kfree(nn
->unconf_id_hashtbl
);
5583 kfree(nn
->conf_id_hashtbl
);
5588 nfs4_state_start_net(struct net
*net
)
5590 struct nfsd_net
*nn
= net_generic(net
, nfsd_net_id
);
5593 ret
= nfs4_state_create_net(net
);
5596 nfsd4_client_tracking_init(net
);
5597 nn
->boot_time
= get_seconds();
5598 locks_start_grace(net
, &nn
->nfsd4_manager
);
5599 nn
->grace_ended
= false;
5600 printk(KERN_INFO
"NFSD: starting %ld-second grace period (net %p)\n",
5601 nn
->nfsd4_grace
, net
);
5602 queue_delayed_work(laundry_wq
, &nn
->laundromat_work
, nn
->nfsd4_grace
* HZ
);
5606 /* initialization to perform when the nfsd service is started: */
5609 nfs4_state_start(void)
5613 ret
= set_callback_cred();
5616 laundry_wq
= create_singlethread_workqueue("nfsd4");
5617 if (laundry_wq
== NULL
) {
5621 ret
= nfsd4_create_callback_queue();
5623 goto out_free_laundry
;
5625 set_max_delegations();
5630 destroy_workqueue(laundry_wq
);
5636 nfs4_state_shutdown_net(struct net
*net
)
5638 struct nfs4_delegation
*dp
= NULL
;
5639 struct list_head
*pos
, *next
, reaplist
;
5640 struct nfsd_net
*nn
= net_generic(net
, nfsd_net_id
);
5642 cancel_delayed_work_sync(&nn
->laundromat_work
);
5643 locks_end_grace(&nn
->nfsd4_manager
);
5646 INIT_LIST_HEAD(&reaplist
);
5647 spin_lock(&state_lock
);
5648 list_for_each_safe(pos
, next
, &nn
->del_recall_lru
) {
5649 dp
= list_entry (pos
, struct nfs4_delegation
, dl_recall_lru
);
5650 unhash_delegation_locked(dp
);
5651 list_add(&dp
->dl_recall_lru
, &reaplist
);
5653 spin_unlock(&state_lock
);
5654 list_for_each_safe(pos
, next
, &reaplist
) {
5655 dp
= list_entry (pos
, struct nfs4_delegation
, dl_recall_lru
);
5656 list_del_init(&dp
->dl_recall_lru
);
5657 nfs4_put_delegation(dp
);
5660 nfsd4_client_tracking_exit(net
);
5661 nfs4_state_destroy_net(net
);
5662 nfs4_unlock_state();
5666 nfs4_state_shutdown(void)
5668 destroy_workqueue(laundry_wq
);
5669 nfsd4_destroy_callback_queue();
5673 get_stateid(struct nfsd4_compound_state
*cstate
, stateid_t
*stateid
)
5675 if (HAS_STATE_ID(cstate
, CURRENT_STATE_ID_FLAG
) && CURRENT_STATEID(stateid
))
5676 memcpy(stateid
, &cstate
->current_stateid
, sizeof(stateid_t
));
5680 put_stateid(struct nfsd4_compound_state
*cstate
, stateid_t
*stateid
)
5682 if (cstate
->minorversion
) {
5683 memcpy(&cstate
->current_stateid
, stateid
, sizeof(stateid_t
));
5684 SET_STATE_ID(cstate
, CURRENT_STATE_ID_FLAG
);
5689 clear_current_stateid(struct nfsd4_compound_state
*cstate
)
5691 CLEAR_STATE_ID(cstate
, CURRENT_STATE_ID_FLAG
);
5695 * functions to set current state id
5698 nfsd4_set_opendowngradestateid(struct nfsd4_compound_state
*cstate
, struct nfsd4_open_downgrade
*odp
)
5700 put_stateid(cstate
, &odp
->od_stateid
);
5704 nfsd4_set_openstateid(struct nfsd4_compound_state
*cstate
, struct nfsd4_open
*open
)
5706 put_stateid(cstate
, &open
->op_stateid
);
5710 nfsd4_set_closestateid(struct nfsd4_compound_state
*cstate
, struct nfsd4_close
*close
)
5712 put_stateid(cstate
, &close
->cl_stateid
);
5716 nfsd4_set_lockstateid(struct nfsd4_compound_state
*cstate
, struct nfsd4_lock
*lock
)
5718 put_stateid(cstate
, &lock
->lk_resp_stateid
);
5722 * functions to consume current state id
5726 nfsd4_get_opendowngradestateid(struct nfsd4_compound_state
*cstate
, struct nfsd4_open_downgrade
*odp
)
5728 get_stateid(cstate
, &odp
->od_stateid
);
5732 nfsd4_get_delegreturnstateid(struct nfsd4_compound_state
*cstate
, struct nfsd4_delegreturn
*drp
)
5734 get_stateid(cstate
, &drp
->dr_stateid
);
5738 nfsd4_get_freestateid(struct nfsd4_compound_state
*cstate
, struct nfsd4_free_stateid
*fsp
)
5740 get_stateid(cstate
, &fsp
->fr_stateid
);
5744 nfsd4_get_setattrstateid(struct nfsd4_compound_state
*cstate
, struct nfsd4_setattr
*setattr
)
5746 get_stateid(cstate
, &setattr
->sa_stateid
);
5750 nfsd4_get_closestateid(struct nfsd4_compound_state
*cstate
, struct nfsd4_close
*close
)
5752 get_stateid(cstate
, &close
->cl_stateid
);
5756 nfsd4_get_lockustateid(struct nfsd4_compound_state
*cstate
, struct nfsd4_locku
*locku
)
5758 get_stateid(cstate
, &locku
->lu_stateid
);
5762 nfsd4_get_readstateid(struct nfsd4_compound_state
*cstate
, struct nfsd4_read
*read
)
5764 get_stateid(cstate
, &read
->rd_stateid
);
5768 nfsd4_get_writestateid(struct nfsd4_compound_state
*cstate
, struct nfsd4_write
*write
)
5770 get_stateid(cstate
, &write
->wr_stateid
);