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 int 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_alloc(slab
, GFP_KERNEL
);
477 new_id
= idr_alloc_cyclic(stateids
, stid
, 0, 0, GFP_KERNEL
);
480 stid
->sc_client
= cl
;
482 stid
->sc_stateid
.si_opaque
.so_id
= new_id
;
483 stid
->sc_stateid
.si_opaque
.so_clid
= cl
->cl_clientid
;
484 /* Will be incremented before return to client: */
485 stid
->sc_stateid
.si_generation
= 0;
486 atomic_set(&stid
->sc_count
, 1);
489 * It shouldn't be a problem to reuse an opaque stateid value.
490 * I don't think it is for 4.1. But with 4.0 I worry that, for
491 * example, a stray write retransmission could be accepted by
492 * the server when it should have been rejected. Therefore,
493 * adopt a trick from the sctp code to attempt to maximize the
494 * amount of time until an id is reused, by ensuring they always
495 * "increase" (mod INT_MAX):
499 kmem_cache_free(slab
, stid
);
503 static struct nfs4_ol_stateid
* nfs4_alloc_stateid(struct nfs4_client
*clp
)
505 return openlockstateid(nfs4_alloc_stid(clp
, stateid_slab
));
509 * When we recall a delegation, we should be careful not to hand it
510 * out again straight away.
511 * To ensure this we keep a pair of bloom filters ('new' and 'old')
512 * in which the filehandles of recalled delegations are "stored".
513 * If a filehandle appear in either filter, a delegation is blocked.
514 * When a delegation is recalled, the filehandle is stored in the "new"
516 * Every 30 seconds we swap the filters and clear the "new" one,
517 * unless both are empty of course.
519 * Each filter is 256 bits. We hash the filehandle to 32bit and use the
520 * low 3 bytes as hash-table indices.
522 * 'state_lock', which is always held when block_delegations() is called,
523 * is used to manage concurrent access. Testing does not need the lock
524 * except when swapping the two filters.
526 static struct bloom_pair
{
527 int entries
, old_entries
;
529 int new; /* index into 'set' */
530 DECLARE_BITMAP(set
[2], 256);
531 } blocked_delegations
;
533 static int delegation_blocked(struct knfsd_fh
*fh
)
536 struct bloom_pair
*bd
= &blocked_delegations
;
538 if (bd
->entries
== 0)
540 if (seconds_since_boot() - bd
->swap_time
> 30) {
541 spin_lock(&state_lock
);
542 if (seconds_since_boot() - bd
->swap_time
> 30) {
543 bd
->entries
-= bd
->old_entries
;
544 bd
->old_entries
= bd
->entries
;
545 memset(bd
->set
[bd
->new], 0,
548 bd
->swap_time
= seconds_since_boot();
550 spin_unlock(&state_lock
);
552 hash
= arch_fast_hash(&fh
->fh_base
, fh
->fh_size
, 0);
553 if (test_bit(hash
&255, bd
->set
[0]) &&
554 test_bit((hash
>>8)&255, bd
->set
[0]) &&
555 test_bit((hash
>>16)&255, bd
->set
[0]))
558 if (test_bit(hash
&255, bd
->set
[1]) &&
559 test_bit((hash
>>8)&255, bd
->set
[1]) &&
560 test_bit((hash
>>16)&255, bd
->set
[1]))
566 static void block_delegations(struct knfsd_fh
*fh
)
569 struct bloom_pair
*bd
= &blocked_delegations
;
571 lockdep_assert_held(&state_lock
);
573 hash
= arch_fast_hash(&fh
->fh_base
, fh
->fh_size
, 0);
575 __set_bit(hash
&255, bd
->set
[bd
->new]);
576 __set_bit((hash
>>8)&255, bd
->set
[bd
->new]);
577 __set_bit((hash
>>16)&255, bd
->set
[bd
->new]);
578 if (bd
->entries
== 0)
579 bd
->swap_time
= seconds_since_boot();
583 static struct nfs4_delegation
*
584 alloc_init_deleg(struct nfs4_client
*clp
, struct nfs4_ol_stateid
*stp
, struct svc_fh
*current_fh
)
586 struct nfs4_delegation
*dp
;
588 dprintk("NFSD alloc_init_deleg\n");
589 if (num_delegations
> 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;
603 INIT_LIST_HEAD(&dp
->dl_perfile
);
604 INIT_LIST_HEAD(&dp
->dl_perclnt
);
605 INIT_LIST_HEAD(&dp
->dl_recall_lru
);
607 dp
->dl_type
= NFS4_OPEN_DELEGATE_READ
;
608 fh_copy_shallow(&dp
->dl_fh
, ¤t_fh
->fh_handle
);
610 INIT_WORK(&dp
->dl_recall
.cb_work
, nfsd4_run_cb_recall
);
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
)) {
630 remove_stid(&dp
->dl_stid
);
631 nfs4_free_stid(deleg_slab
, &dp
->dl_stid
);
636 static void nfs4_put_deleg_lease(struct nfs4_file
*fp
)
638 lockdep_assert_held(&state_lock
);
642 if (atomic_dec_and_test(&fp
->fi_delegees
)) {
643 vfs_setlease(fp
->fi_deleg_file
, F_UNLCK
, &fp
->fi_lease
);
645 fput(fp
->fi_deleg_file
);
646 fp
->fi_deleg_file
= NULL
;
650 static void unhash_stid(struct nfs4_stid
*s
)
656 hash_delegation_locked(struct nfs4_delegation
*dp
, struct nfs4_file
*fp
)
658 lockdep_assert_held(&state_lock
);
659 lockdep_assert_held(&fp
->fi_lock
);
661 dp
->dl_stid
.sc_type
= NFS4_DELEG_STID
;
662 list_add(&dp
->dl_perfile
, &fp
->fi_delegations
);
663 list_add(&dp
->dl_perclnt
, &dp
->dl_stid
.sc_client
->cl_delegations
);
666 /* Called under the state lock. */
668 unhash_delegation(struct nfs4_delegation
*dp
)
670 struct nfs4_file
*fp
= dp
->dl_file
;
672 spin_lock(&state_lock
);
673 dp
->dl_stid
.sc_type
= NFS4_CLOSED_DELEG_STID
;
674 /* Ensure that deleg break won't try to requeue it */
676 spin_lock(&fp
->fi_lock
);
677 list_del_init(&dp
->dl_perclnt
);
678 list_del_init(&dp
->dl_recall_lru
);
679 list_del_init(&dp
->dl_perfile
);
680 spin_unlock(&fp
->fi_lock
);
682 nfs4_put_deleg_lease(fp
);
685 spin_unlock(&state_lock
);
690 static void destroy_revoked_delegation(struct nfs4_delegation
*dp
)
692 list_del_init(&dp
->dl_recall_lru
);
693 nfs4_put_delegation(dp
);
696 static void destroy_delegation(struct nfs4_delegation
*dp
)
698 unhash_delegation(dp
);
699 nfs4_put_delegation(dp
);
702 static void revoke_delegation(struct nfs4_delegation
*dp
)
704 struct nfs4_client
*clp
= dp
->dl_stid
.sc_client
;
706 if (clp
->cl_minorversion
== 0)
707 destroy_delegation(dp
);
709 unhash_delegation(dp
);
710 dp
->dl_stid
.sc_type
= NFS4_REVOKED_DELEG_STID
;
711 list_add(&dp
->dl_recall_lru
, &clp
->cl_revoked
);
719 static unsigned int clientid_hashval(u32 id
)
721 return id
& CLIENT_HASH_MASK
;
724 static unsigned int clientstr_hashval(const char *name
)
726 return opaque_hashval(name
, 8) & CLIENT_HASH_MASK
;
730 * We store the NONE, READ, WRITE, and BOTH bits separately in the
731 * st_{access,deny}_bmap field of the stateid, in order to track not
732 * only what share bits are currently in force, but also what
733 * combinations of share bits previous opens have used. This allows us
734 * to enforce the recommendation of rfc 3530 14.2.19 that the server
735 * return an error if the client attempt to downgrade to a combination
736 * of share bits not explicable by closing some of its previous opens.
738 * XXX: This enforcement is actually incomplete, since we don't keep
739 * track of access/deny bit combinations; so, e.g., we allow:
741 * OPEN allow read, deny write
742 * OPEN allow both, deny none
743 * DOWNGRADE allow read, deny none
745 * which we should reject.
748 bmap_to_share_mode(unsigned long bmap
) {
750 unsigned int access
= 0;
752 for (i
= 1; i
< 4; i
++) {
753 if (test_bit(i
, &bmap
))
759 /* set share access for a given stateid */
761 set_access(u32 access
, struct nfs4_ol_stateid
*stp
)
763 unsigned char mask
= 1 << access
;
765 WARN_ON_ONCE(access
> NFS4_SHARE_ACCESS_BOTH
);
766 stp
->st_access_bmap
|= mask
;
769 /* clear share access for a given stateid */
771 clear_access(u32 access
, struct nfs4_ol_stateid
*stp
)
773 unsigned char mask
= 1 << access
;
775 WARN_ON_ONCE(access
> NFS4_SHARE_ACCESS_BOTH
);
776 stp
->st_access_bmap
&= ~mask
;
779 /* test whether a given stateid has access */
781 test_access(u32 access
, struct nfs4_ol_stateid
*stp
)
783 unsigned char mask
= 1 << access
;
785 return (bool)(stp
->st_access_bmap
& mask
);
788 /* set share deny for a given stateid */
790 set_deny(u32 deny
, struct nfs4_ol_stateid
*stp
)
792 unsigned char mask
= 1 << deny
;
794 WARN_ON_ONCE(deny
> NFS4_SHARE_DENY_BOTH
);
795 stp
->st_deny_bmap
|= mask
;
798 /* clear share deny for a given stateid */
800 clear_deny(u32 deny
, struct nfs4_ol_stateid
*stp
)
802 unsigned char mask
= 1 << deny
;
804 WARN_ON_ONCE(deny
> NFS4_SHARE_DENY_BOTH
);
805 stp
->st_deny_bmap
&= ~mask
;
808 /* test whether a given stateid is denying specific access */
810 test_deny(u32 deny
, struct nfs4_ol_stateid
*stp
)
812 unsigned char mask
= 1 << deny
;
814 return (bool)(stp
->st_deny_bmap
& mask
);
817 static int nfs4_access_to_omode(u32 access
)
819 switch (access
& NFS4_SHARE_ACCESS_BOTH
) {
820 case NFS4_SHARE_ACCESS_READ
:
822 case NFS4_SHARE_ACCESS_WRITE
:
824 case NFS4_SHARE_ACCESS_BOTH
:
832 * A stateid that had a deny mode associated with it is being released
833 * or downgraded. Recalculate the deny mode on the file.
836 recalculate_deny_mode(struct nfs4_file
*fp
)
838 struct nfs4_ol_stateid
*stp
;
840 spin_lock(&fp
->fi_lock
);
841 fp
->fi_share_deny
= 0;
842 list_for_each_entry(stp
, &fp
->fi_stateids
, st_perfile
)
843 fp
->fi_share_deny
|= bmap_to_share_mode(stp
->st_deny_bmap
);
844 spin_unlock(&fp
->fi_lock
);
848 reset_union_bmap_deny(u32 deny
, struct nfs4_ol_stateid
*stp
)
853 for (i
= 1; i
< 4; i
++) {
854 if ((i
& deny
) != i
) {
860 /* Recalculate per-file deny mode if there was a change */
862 recalculate_deny_mode(stp
->st_file
);
865 /* release all access and file references for a given stateid */
867 release_all_access(struct nfs4_ol_stateid
*stp
)
870 struct nfs4_file
*fp
= stp
->st_file
;
872 if (fp
&& stp
->st_deny_bmap
!= 0)
873 recalculate_deny_mode(fp
);
875 for (i
= 1; i
< 4; i
++) {
876 if (test_access(i
, stp
))
877 nfs4_file_put_access(stp
->st_file
, i
);
878 clear_access(i
, stp
);
882 static void unhash_generic_stateid(struct nfs4_ol_stateid
*stp
)
884 struct nfs4_file
*fp
= stp
->st_file
;
886 spin_lock(&fp
->fi_lock
);
887 list_del(&stp
->st_perfile
);
888 spin_unlock(&fp
->fi_lock
);
889 list_del(&stp
->st_perstateowner
);
892 static void close_generic_stateid(struct nfs4_ol_stateid
*stp
)
894 release_all_access(stp
);
895 put_nfs4_file(stp
->st_file
);
899 static void free_generic_stateid(struct nfs4_ol_stateid
*stp
)
901 remove_stid(&stp
->st_stid
);
902 nfs4_free_stid(stateid_slab
, &stp
->st_stid
);
905 static void __release_lock_stateid(struct nfs4_ol_stateid
*stp
)
909 list_del(&stp
->st_locks
);
910 unhash_generic_stateid(stp
);
911 unhash_stid(&stp
->st_stid
);
912 file
= find_any_file(stp
->st_file
);
914 filp_close(file
, (fl_owner_t
)lockowner(stp
->st_stateowner
));
915 close_generic_stateid(stp
);
916 free_generic_stateid(stp
);
919 static void unhash_lockowner(struct nfs4_lockowner
*lo
)
921 struct nfs4_ol_stateid
*stp
;
923 list_del(&lo
->lo_owner
.so_strhash
);
924 while (!list_empty(&lo
->lo_owner
.so_stateids
)) {
925 stp
= list_first_entry(&lo
->lo_owner
.so_stateids
,
926 struct nfs4_ol_stateid
, st_perstateowner
);
927 __release_lock_stateid(stp
);
931 static void nfs4_free_lockowner(struct nfs4_lockowner
*lo
)
933 kfree(lo
->lo_owner
.so_owner
.data
);
934 kmem_cache_free(lockowner_slab
, lo
);
937 static void release_lockowner(struct nfs4_lockowner
*lo
)
939 unhash_lockowner(lo
);
940 nfs4_free_lockowner(lo
);
943 static void release_lockowner_if_empty(struct nfs4_lockowner
*lo
)
945 if (list_empty(&lo
->lo_owner
.so_stateids
))
946 release_lockowner(lo
);
949 static void release_lock_stateid(struct nfs4_ol_stateid
*stp
)
951 struct nfs4_lockowner
*lo
;
953 lo
= lockowner(stp
->st_stateowner
);
954 __release_lock_stateid(stp
);
955 release_lockowner_if_empty(lo
);
958 static void release_open_stateid_locks(struct nfs4_ol_stateid
*open_stp
)
960 struct nfs4_ol_stateid
*stp
;
962 while (!list_empty(&open_stp
->st_locks
)) {
963 stp
= list_entry(open_stp
->st_locks
.next
,
964 struct nfs4_ol_stateid
, st_locks
);
965 release_lock_stateid(stp
);
969 static void unhash_open_stateid(struct nfs4_ol_stateid
*stp
)
971 unhash_generic_stateid(stp
);
972 release_open_stateid_locks(stp
);
973 close_generic_stateid(stp
);
976 static void release_open_stateid(struct nfs4_ol_stateid
*stp
)
978 unhash_open_stateid(stp
);
979 free_generic_stateid(stp
);
982 static void unhash_openowner(struct nfs4_openowner
*oo
)
984 struct nfs4_ol_stateid
*stp
;
986 list_del(&oo
->oo_owner
.so_strhash
);
987 list_del(&oo
->oo_perclient
);
988 while (!list_empty(&oo
->oo_owner
.so_stateids
)) {
989 stp
= list_first_entry(&oo
->oo_owner
.so_stateids
,
990 struct nfs4_ol_stateid
, st_perstateowner
);
991 release_open_stateid(stp
);
995 static void release_last_closed_stateid(struct nfs4_openowner
*oo
)
997 struct nfs4_ol_stateid
*s
= oo
->oo_last_closed_stid
;
1000 free_generic_stateid(s
);
1001 oo
->oo_last_closed_stid
= NULL
;
1005 static void nfs4_free_openowner(struct nfs4_openowner
*oo
)
1007 kfree(oo
->oo_owner
.so_owner
.data
);
1008 kmem_cache_free(openowner_slab
, oo
);
1011 static void release_openowner(struct nfs4_openowner
*oo
)
1013 unhash_openowner(oo
);
1014 list_del(&oo
->oo_close_lru
);
1015 release_last_closed_stateid(oo
);
1016 nfs4_free_openowner(oo
);
1020 hash_sessionid(struct nfs4_sessionid
*sessionid
)
1022 struct nfsd4_sessionid
*sid
= (struct nfsd4_sessionid
*)sessionid
;
1024 return sid
->sequence
% SESSION_HASH_SIZE
;
1029 dump_sessionid(const char *fn
, struct nfs4_sessionid
*sessionid
)
1031 u32
*ptr
= (u32
*)(&sessionid
->data
[0]);
1032 dprintk("%s: %u:%u:%u:%u\n", fn
, ptr
[0], ptr
[1], ptr
[2], ptr
[3]);
1036 dump_sessionid(const char *fn
, struct nfs4_sessionid
*sessionid
)
1042 * Bump the seqid on cstate->replay_owner, and clear replay_owner if it
1043 * won't be used for replay.
1045 void nfsd4_bump_seqid(struct nfsd4_compound_state
*cstate
, __be32 nfserr
)
1047 struct nfs4_stateowner
*so
= cstate
->replay_owner
;
1049 if (nfserr
== nfserr_replay_me
)
1052 if (!seqid_mutating_err(ntohl(nfserr
))) {
1053 cstate
->replay_owner
= NULL
;
1058 if (so
->so_is_open_owner
)
1059 release_last_closed_stateid(openowner(so
));
1065 gen_sessionid(struct nfsd4_session
*ses
)
1067 struct nfs4_client
*clp
= ses
->se_client
;
1068 struct nfsd4_sessionid
*sid
;
1070 sid
= (struct nfsd4_sessionid
*)ses
->se_sessionid
.data
;
1071 sid
->clientid
= clp
->cl_clientid
;
1072 sid
->sequence
= current_sessionid
++;
1077 * The protocol defines ca_maxresponssize_cached to include the size of
1078 * the rpc header, but all we need to cache is the data starting after
1079 * the end of the initial SEQUENCE operation--the rest we regenerate
1080 * each time. Therefore we can advertise a ca_maxresponssize_cached
1081 * value that is the number of bytes in our cache plus a few additional
1082 * bytes. In order to stay on the safe side, and not promise more than
1083 * we can cache, those additional bytes must be the minimum possible: 24
1084 * bytes of rpc header (xid through accept state, with AUTH_NULL
1085 * verifier), 12 for the compound header (with zero-length tag), and 44
1086 * for the SEQUENCE op response:
1088 #define NFSD_MIN_HDR_SEQ_SZ (24 + 12 + 44)
1091 free_session_slots(struct nfsd4_session
*ses
)
1095 for (i
= 0; i
< ses
->se_fchannel
.maxreqs
; i
++)
1096 kfree(ses
->se_slots
[i
]);
1100 * We don't actually need to cache the rpc and session headers, so we
1101 * can allocate a little less for each slot:
1103 static inline u32
slot_bytes(struct nfsd4_channel_attrs
*ca
)
1107 if (ca
->maxresp_cached
< NFSD_MIN_HDR_SEQ_SZ
)
1110 size
= ca
->maxresp_cached
- NFSD_MIN_HDR_SEQ_SZ
;
1111 return size
+ sizeof(struct nfsd4_slot
);
1115 * XXX: If we run out of reserved DRC memory we could (up to a point)
1116 * re-negotiate active sessions and reduce their slot usage to make
1117 * room for new connections. For now we just fail the create session.
1119 static u32
nfsd4_get_drc_mem(struct nfsd4_channel_attrs
*ca
)
1121 u32 slotsize
= slot_bytes(ca
);
1122 u32 num
= ca
->maxreqs
;
1125 spin_lock(&nfsd_drc_lock
);
1126 avail
= min((unsigned long)NFSD_MAX_MEM_PER_SESSION
,
1127 nfsd_drc_max_mem
- nfsd_drc_mem_used
);
1128 num
= min_t(int, num
, avail
/ slotsize
);
1129 nfsd_drc_mem_used
+= num
* slotsize
;
1130 spin_unlock(&nfsd_drc_lock
);
1135 static void nfsd4_put_drc_mem(struct nfsd4_channel_attrs
*ca
)
1137 int slotsize
= slot_bytes(ca
);
1139 spin_lock(&nfsd_drc_lock
);
1140 nfsd_drc_mem_used
-= slotsize
* ca
->maxreqs
;
1141 spin_unlock(&nfsd_drc_lock
);
1144 static struct nfsd4_session
*alloc_session(struct nfsd4_channel_attrs
*fattrs
,
1145 struct nfsd4_channel_attrs
*battrs
)
1147 int numslots
= fattrs
->maxreqs
;
1148 int slotsize
= slot_bytes(fattrs
);
1149 struct nfsd4_session
*new;
1152 BUILD_BUG_ON(NFSD_MAX_SLOTS_PER_SESSION
* sizeof(struct nfsd4_slot
*)
1153 + sizeof(struct nfsd4_session
) > PAGE_SIZE
);
1154 mem
= numslots
* sizeof(struct nfsd4_slot
*);
1156 new = kzalloc(sizeof(*new) + mem
, GFP_KERNEL
);
1159 /* allocate each struct nfsd4_slot and data cache in one piece */
1160 for (i
= 0; i
< numslots
; i
++) {
1161 new->se_slots
[i
] = kzalloc(slotsize
, GFP_KERNEL
);
1162 if (!new->se_slots
[i
])
1166 memcpy(&new->se_fchannel
, fattrs
, sizeof(struct nfsd4_channel_attrs
));
1167 memcpy(&new->se_bchannel
, battrs
, sizeof(struct nfsd4_channel_attrs
));
1172 kfree(new->se_slots
[i
]);
1177 static void free_conn(struct nfsd4_conn
*c
)
1179 svc_xprt_put(c
->cn_xprt
);
1183 static void nfsd4_conn_lost(struct svc_xpt_user
*u
)
1185 struct nfsd4_conn
*c
= container_of(u
, struct nfsd4_conn
, cn_xpt_user
);
1186 struct nfs4_client
*clp
= c
->cn_session
->se_client
;
1188 spin_lock(&clp
->cl_lock
);
1189 if (!list_empty(&c
->cn_persession
)) {
1190 list_del(&c
->cn_persession
);
1193 nfsd4_probe_callback(clp
);
1194 spin_unlock(&clp
->cl_lock
);
1197 static struct nfsd4_conn
*alloc_conn(struct svc_rqst
*rqstp
, u32 flags
)
1199 struct nfsd4_conn
*conn
;
1201 conn
= kmalloc(sizeof(struct nfsd4_conn
), GFP_KERNEL
);
1204 svc_xprt_get(rqstp
->rq_xprt
);
1205 conn
->cn_xprt
= rqstp
->rq_xprt
;
1206 conn
->cn_flags
= flags
;
1207 INIT_LIST_HEAD(&conn
->cn_xpt_user
.list
);
1211 static void __nfsd4_hash_conn(struct nfsd4_conn
*conn
, struct nfsd4_session
*ses
)
1213 conn
->cn_session
= ses
;
1214 list_add(&conn
->cn_persession
, &ses
->se_conns
);
1217 static void nfsd4_hash_conn(struct nfsd4_conn
*conn
, struct nfsd4_session
*ses
)
1219 struct nfs4_client
*clp
= ses
->se_client
;
1221 spin_lock(&clp
->cl_lock
);
1222 __nfsd4_hash_conn(conn
, ses
);
1223 spin_unlock(&clp
->cl_lock
);
1226 static int nfsd4_register_conn(struct nfsd4_conn
*conn
)
1228 conn
->cn_xpt_user
.callback
= nfsd4_conn_lost
;
1229 return register_xpt_user(conn
->cn_xprt
, &conn
->cn_xpt_user
);
1232 static void nfsd4_init_conn(struct svc_rqst
*rqstp
, struct nfsd4_conn
*conn
, struct nfsd4_session
*ses
)
1236 nfsd4_hash_conn(conn
, ses
);
1237 ret
= nfsd4_register_conn(conn
);
1239 /* oops; xprt is already down: */
1240 nfsd4_conn_lost(&conn
->cn_xpt_user
);
1241 /* We may have gained or lost a callback channel: */
1242 nfsd4_probe_callback_sync(ses
->se_client
);
1245 static struct nfsd4_conn
*alloc_conn_from_crses(struct svc_rqst
*rqstp
, struct nfsd4_create_session
*cses
)
1247 u32 dir
= NFS4_CDFC4_FORE
;
1249 if (cses
->flags
& SESSION4_BACK_CHAN
)
1250 dir
|= NFS4_CDFC4_BACK
;
1251 return alloc_conn(rqstp
, dir
);
1254 /* must be called under client_lock */
1255 static void nfsd4_del_conns(struct nfsd4_session
*s
)
1257 struct nfs4_client
*clp
= s
->se_client
;
1258 struct nfsd4_conn
*c
;
1260 spin_lock(&clp
->cl_lock
);
1261 while (!list_empty(&s
->se_conns
)) {
1262 c
= list_first_entry(&s
->se_conns
, struct nfsd4_conn
, cn_persession
);
1263 list_del_init(&c
->cn_persession
);
1264 spin_unlock(&clp
->cl_lock
);
1266 unregister_xpt_user(c
->cn_xprt
, &c
->cn_xpt_user
);
1269 spin_lock(&clp
->cl_lock
);
1271 spin_unlock(&clp
->cl_lock
);
1274 static void __free_session(struct nfsd4_session
*ses
)
1276 free_session_slots(ses
);
1280 static void free_session(struct nfsd4_session
*ses
)
1282 struct nfsd_net
*nn
= net_generic(ses
->se_client
->net
, nfsd_net_id
);
1284 lockdep_assert_held(&nn
->client_lock
);
1285 nfsd4_del_conns(ses
);
1286 nfsd4_put_drc_mem(&ses
->se_fchannel
);
1287 __free_session(ses
);
1290 static void init_session(struct svc_rqst
*rqstp
, struct nfsd4_session
*new, struct nfs4_client
*clp
, struct nfsd4_create_session
*cses
)
1293 struct nfsd_net
*nn
= net_generic(SVC_NET(rqstp
), nfsd_net_id
);
1295 new->se_client
= clp
;
1298 INIT_LIST_HEAD(&new->se_conns
);
1300 new->se_cb_seq_nr
= 1;
1301 new->se_flags
= cses
->flags
;
1302 new->se_cb_prog
= cses
->callback_prog
;
1303 new->se_cb_sec
= cses
->cb_sec
;
1304 atomic_set(&new->se_ref
, 0);
1305 idx
= hash_sessionid(&new->se_sessionid
);
1306 spin_lock(&nn
->client_lock
);
1307 list_add(&new->se_hash
, &nn
->sessionid_hashtbl
[idx
]);
1308 spin_lock(&clp
->cl_lock
);
1309 list_add(&new->se_perclnt
, &clp
->cl_sessions
);
1310 spin_unlock(&clp
->cl_lock
);
1311 spin_unlock(&nn
->client_lock
);
1313 if (cses
->flags
& SESSION4_BACK_CHAN
) {
1314 struct sockaddr
*sa
= svc_addr(rqstp
);
1316 * This is a little silly; with sessions there's no real
1317 * use for the callback address. Use the peer address
1318 * as a reasonable default for now, but consider fixing
1319 * the rpc client not to require an address in the
1322 rpc_copy_addr((struct sockaddr
*)&clp
->cl_cb_conn
.cb_addr
, sa
);
1323 clp
->cl_cb_conn
.cb_addrlen
= svc_addr_len(sa
);
1327 /* caller must hold client_lock */
1328 static struct nfsd4_session
*
1329 __find_in_sessionid_hashtbl(struct nfs4_sessionid
*sessionid
, struct net
*net
)
1331 struct nfsd4_session
*elem
;
1333 struct nfsd_net
*nn
= net_generic(net
, nfsd_net_id
);
1335 dump_sessionid(__func__
, sessionid
);
1336 idx
= hash_sessionid(sessionid
);
1337 /* Search in the appropriate list */
1338 list_for_each_entry(elem
, &nn
->sessionid_hashtbl
[idx
], se_hash
) {
1339 if (!memcmp(elem
->se_sessionid
.data
, sessionid
->data
,
1340 NFS4_MAX_SESSIONID_LEN
)) {
1345 dprintk("%s: session not found\n", __func__
);
1349 static struct nfsd4_session
*
1350 find_in_sessionid_hashtbl(struct nfs4_sessionid
*sessionid
, struct net
*net
,
1353 struct nfsd4_session
*session
;
1354 __be32 status
= nfserr_badsession
;
1356 session
= __find_in_sessionid_hashtbl(sessionid
, net
);
1359 status
= nfsd4_get_session_locked(session
);
1367 /* caller must hold client_lock */
1369 unhash_session(struct nfsd4_session
*ses
)
1371 list_del(&ses
->se_hash
);
1372 spin_lock(&ses
->se_client
->cl_lock
);
1373 list_del(&ses
->se_perclnt
);
1374 spin_unlock(&ses
->se_client
->cl_lock
);
1377 /* SETCLIENTID and SETCLIENTID_CONFIRM Helper functions */
1379 STALE_CLIENTID(clientid_t
*clid
, struct nfsd_net
*nn
)
1381 if (clid
->cl_boot
== nn
->boot_time
)
1383 dprintk("NFSD stale clientid (%08x/%08x) boot_time %08lx\n",
1384 clid
->cl_boot
, clid
->cl_id
, nn
->boot_time
);
1389 * XXX Should we use a slab cache ?
1390 * This type of memory management is somewhat inefficient, but we use it
1391 * anyway since SETCLIENTID is not a common operation.
1393 static struct nfs4_client
*alloc_client(struct xdr_netobj name
)
1395 struct nfs4_client
*clp
;
1397 clp
= kzalloc(sizeof(struct nfs4_client
), GFP_KERNEL
);
1400 clp
->cl_name
.data
= kmemdup(name
.data
, name
.len
, GFP_KERNEL
);
1401 if (clp
->cl_name
.data
== NULL
) {
1405 clp
->cl_name
.len
= name
.len
;
1406 INIT_LIST_HEAD(&clp
->cl_sessions
);
1407 idr_init(&clp
->cl_stateids
);
1408 atomic_set(&clp
->cl_refcount
, 0);
1409 clp
->cl_cb_state
= NFSD4_CB_UNKNOWN
;
1410 INIT_LIST_HEAD(&clp
->cl_idhash
);
1411 INIT_LIST_HEAD(&clp
->cl_openowners
);
1412 INIT_LIST_HEAD(&clp
->cl_delegations
);
1413 INIT_LIST_HEAD(&clp
->cl_lru
);
1414 INIT_LIST_HEAD(&clp
->cl_callbacks
);
1415 INIT_LIST_HEAD(&clp
->cl_revoked
);
1416 spin_lock_init(&clp
->cl_lock
);
1417 rpc_init_wait_queue(&clp
->cl_cb_waitq
, "Backchannel slot table");
1422 free_client(struct nfs4_client
*clp
)
1424 struct nfsd_net __maybe_unused
*nn
= net_generic(clp
->net
, nfsd_net_id
);
1426 lockdep_assert_held(&nn
->client_lock
);
1427 while (!list_empty(&clp
->cl_sessions
)) {
1428 struct nfsd4_session
*ses
;
1429 ses
= list_entry(clp
->cl_sessions
.next
, struct nfsd4_session
,
1431 list_del(&ses
->se_perclnt
);
1432 WARN_ON_ONCE(atomic_read(&ses
->se_ref
));
1435 rpc_destroy_wait_queue(&clp
->cl_cb_waitq
);
1436 free_svc_cred(&clp
->cl_cred
);
1437 kfree(clp
->cl_name
.data
);
1438 idr_destroy(&clp
->cl_stateids
);
1442 /* must be called under the client_lock */
1444 unhash_client_locked(struct nfs4_client
*clp
)
1446 struct nfsd4_session
*ses
;
1448 list_del(&clp
->cl_lru
);
1449 spin_lock(&clp
->cl_lock
);
1450 list_for_each_entry(ses
, &clp
->cl_sessions
, se_perclnt
)
1451 list_del_init(&ses
->se_hash
);
1452 spin_unlock(&clp
->cl_lock
);
1456 destroy_client(struct nfs4_client
*clp
)
1458 struct nfs4_openowner
*oo
;
1459 struct nfs4_delegation
*dp
;
1460 struct list_head reaplist
;
1461 struct nfsd_net
*nn
= net_generic(clp
->net
, nfsd_net_id
);
1463 INIT_LIST_HEAD(&reaplist
);
1464 spin_lock(&state_lock
);
1465 while (!list_empty(&clp
->cl_delegations
)) {
1466 dp
= list_entry(clp
->cl_delegations
.next
, struct nfs4_delegation
, dl_perclnt
);
1467 list_del_init(&dp
->dl_perclnt
);
1468 /* Ensure that deleg break won't try to requeue it */
1470 list_move(&dp
->dl_recall_lru
, &reaplist
);
1472 spin_unlock(&state_lock
);
1473 while (!list_empty(&reaplist
)) {
1474 dp
= list_entry(reaplist
.next
, struct nfs4_delegation
, dl_recall_lru
);
1475 destroy_delegation(dp
);
1477 list_splice_init(&clp
->cl_revoked
, &reaplist
);
1478 while (!list_empty(&reaplist
)) {
1479 dp
= list_entry(reaplist
.next
, struct nfs4_delegation
, dl_recall_lru
);
1480 destroy_revoked_delegation(dp
);
1482 while (!list_empty(&clp
->cl_openowners
)) {
1483 oo
= list_entry(clp
->cl_openowners
.next
, struct nfs4_openowner
, oo_perclient
);
1484 release_openowner(oo
);
1486 nfsd4_shutdown_callback(clp
);
1487 if (clp
->cl_cb_conn
.cb_xprt
)
1488 svc_xprt_put(clp
->cl_cb_conn
.cb_xprt
);
1489 list_del(&clp
->cl_idhash
);
1490 if (test_bit(NFSD4_CLIENT_CONFIRMED
, &clp
->cl_flags
))
1491 rb_erase(&clp
->cl_namenode
, &nn
->conf_name_tree
);
1493 rb_erase(&clp
->cl_namenode
, &nn
->unconf_name_tree
);
1494 spin_lock(&nn
->client_lock
);
1495 unhash_client_locked(clp
);
1496 WARN_ON_ONCE(atomic_read(&clp
->cl_refcount
));
1498 spin_unlock(&nn
->client_lock
);
1501 static void expire_client(struct nfs4_client
*clp
)
1503 nfsd4_client_record_remove(clp
);
1504 destroy_client(clp
);
1507 static void copy_verf(struct nfs4_client
*target
, nfs4_verifier
*source
)
1509 memcpy(target
->cl_verifier
.data
, source
->data
,
1510 sizeof(target
->cl_verifier
.data
));
1513 static void copy_clid(struct nfs4_client
*target
, struct nfs4_client
*source
)
1515 target
->cl_clientid
.cl_boot
= source
->cl_clientid
.cl_boot
;
1516 target
->cl_clientid
.cl_id
= source
->cl_clientid
.cl_id
;
1519 static int copy_cred(struct svc_cred
*target
, struct svc_cred
*source
)
1521 if (source
->cr_principal
) {
1522 target
->cr_principal
=
1523 kstrdup(source
->cr_principal
, GFP_KERNEL
);
1524 if (target
->cr_principal
== NULL
)
1527 target
->cr_principal
= NULL
;
1528 target
->cr_flavor
= source
->cr_flavor
;
1529 target
->cr_uid
= source
->cr_uid
;
1530 target
->cr_gid
= source
->cr_gid
;
1531 target
->cr_group_info
= source
->cr_group_info
;
1532 get_group_info(target
->cr_group_info
);
1533 target
->cr_gss_mech
= source
->cr_gss_mech
;
1534 if (source
->cr_gss_mech
)
1535 gss_mech_get(source
->cr_gss_mech
);
1540 compare_blob(const struct xdr_netobj
*o1
, const struct xdr_netobj
*o2
)
1544 res
= o1
->len
- o2
->len
;
1547 return (long long)memcmp(o1
->data
, o2
->data
, o1
->len
);
1550 static int same_name(const char *n1
, const char *n2
)
1552 return 0 == memcmp(n1
, n2
, HEXDIR_LEN
);
1556 same_verf(nfs4_verifier
*v1
, nfs4_verifier
*v2
)
1558 return 0 == memcmp(v1
->data
, v2
->data
, sizeof(v1
->data
));
1562 same_clid(clientid_t
*cl1
, clientid_t
*cl2
)
1564 return (cl1
->cl_boot
== cl2
->cl_boot
) && (cl1
->cl_id
== cl2
->cl_id
);
1567 static bool groups_equal(struct group_info
*g1
, struct group_info
*g2
)
1571 if (g1
->ngroups
!= g2
->ngroups
)
1573 for (i
=0; i
<g1
->ngroups
; i
++)
1574 if (!gid_eq(GROUP_AT(g1
, i
), GROUP_AT(g2
, i
)))
1580 * RFC 3530 language requires clid_inuse be returned when the
1581 * "principal" associated with a requests differs from that previously
1582 * used. We use uid, gid's, and gss principal string as our best
1583 * approximation. We also don't want to allow non-gss use of a client
1584 * established using gss: in theory cr_principal should catch that
1585 * change, but in practice cr_principal can be null even in the gss case
1586 * since gssd doesn't always pass down a principal string.
1588 static bool is_gss_cred(struct svc_cred
*cr
)
1590 /* Is cr_flavor one of the gss "pseudoflavors"?: */
1591 return (cr
->cr_flavor
> RPC_AUTH_MAXFLAVOR
);
1596 same_creds(struct svc_cred
*cr1
, struct svc_cred
*cr2
)
1598 if ((is_gss_cred(cr1
) != is_gss_cred(cr2
))
1599 || (!uid_eq(cr1
->cr_uid
, cr2
->cr_uid
))
1600 || (!gid_eq(cr1
->cr_gid
, cr2
->cr_gid
))
1601 || !groups_equal(cr1
->cr_group_info
, cr2
->cr_group_info
))
1603 if (cr1
->cr_principal
== cr2
->cr_principal
)
1605 if (!cr1
->cr_principal
|| !cr2
->cr_principal
)
1607 return 0 == strcmp(cr1
->cr_principal
, cr2
->cr_principal
);
1610 static bool svc_rqst_integrity_protected(struct svc_rqst
*rqstp
)
1612 struct svc_cred
*cr
= &rqstp
->rq_cred
;
1615 if (!cr
->cr_gss_mech
)
1617 service
= gss_pseudoflavor_to_service(cr
->cr_gss_mech
, cr
->cr_flavor
);
1618 return service
== RPC_GSS_SVC_INTEGRITY
||
1619 service
== RPC_GSS_SVC_PRIVACY
;
1622 static bool mach_creds_match(struct nfs4_client
*cl
, struct svc_rqst
*rqstp
)
1624 struct svc_cred
*cr
= &rqstp
->rq_cred
;
1626 if (!cl
->cl_mach_cred
)
1628 if (cl
->cl_cred
.cr_gss_mech
!= cr
->cr_gss_mech
)
1630 if (!svc_rqst_integrity_protected(rqstp
))
1632 if (!cr
->cr_principal
)
1634 return 0 == strcmp(cl
->cl_cred
.cr_principal
, cr
->cr_principal
);
1637 static void gen_clid(struct nfs4_client
*clp
, struct nfsd_net
*nn
)
1639 static u32 current_clientid
= 1;
1641 clp
->cl_clientid
.cl_boot
= nn
->boot_time
;
1642 clp
->cl_clientid
.cl_id
= current_clientid
++;
1645 static void gen_confirm(struct nfs4_client
*clp
)
1651 * This is opaque to client, so no need to byte-swap. Use
1652 * __force to keep sparse happy
1654 verf
[0] = (__force __be32
)get_seconds();
1655 verf
[1] = (__force __be32
)i
++;
1656 memcpy(clp
->cl_confirm
.data
, verf
, sizeof(clp
->cl_confirm
.data
));
1659 static struct nfs4_stid
*find_stateid(struct nfs4_client
*cl
, stateid_t
*t
)
1661 struct nfs4_stid
*ret
;
1663 ret
= idr_find(&cl
->cl_stateids
, t
->si_opaque
.so_id
);
1664 if (!ret
|| !ret
->sc_type
)
1669 static struct nfs4_stid
*find_stateid_by_type(struct nfs4_client
*cl
, stateid_t
*t
, char typemask
)
1671 struct nfs4_stid
*s
;
1673 s
= find_stateid(cl
, t
);
1676 if (typemask
& s
->sc_type
)
1681 static struct nfs4_client
*create_client(struct xdr_netobj name
,
1682 struct svc_rqst
*rqstp
, nfs4_verifier
*verf
)
1684 struct nfs4_client
*clp
;
1685 struct sockaddr
*sa
= svc_addr(rqstp
);
1687 struct net
*net
= SVC_NET(rqstp
);
1688 struct nfsd_net
*nn
= net_generic(net
, nfsd_net_id
);
1690 clp
= alloc_client(name
);
1694 ret
= copy_cred(&clp
->cl_cred
, &rqstp
->rq_cred
);
1696 spin_lock(&nn
->client_lock
);
1698 spin_unlock(&nn
->client_lock
);
1701 INIT_WORK(&clp
->cl_cb_null
.cb_work
, nfsd4_run_cb_null
);
1702 clp
->cl_time
= get_seconds();
1703 clear_bit(0, &clp
->cl_cb_slot_busy
);
1704 copy_verf(clp
, verf
);
1705 rpc_copy_addr((struct sockaddr
*) &clp
->cl_addr
, sa
);
1707 clp
->cl_cb_session
= NULL
;
1713 add_clp_to_name_tree(struct nfs4_client
*new_clp
, struct rb_root
*root
)
1715 struct rb_node
**new = &(root
->rb_node
), *parent
= NULL
;
1716 struct nfs4_client
*clp
;
1719 clp
= rb_entry(*new, struct nfs4_client
, cl_namenode
);
1722 if (compare_blob(&clp
->cl_name
, &new_clp
->cl_name
) > 0)
1723 new = &((*new)->rb_left
);
1725 new = &((*new)->rb_right
);
1728 rb_link_node(&new_clp
->cl_namenode
, parent
, new);
1729 rb_insert_color(&new_clp
->cl_namenode
, root
);
1732 static struct nfs4_client
*
1733 find_clp_in_name_tree(struct xdr_netobj
*name
, struct rb_root
*root
)
1736 struct rb_node
*node
= root
->rb_node
;
1737 struct nfs4_client
*clp
;
1740 clp
= rb_entry(node
, struct nfs4_client
, cl_namenode
);
1741 cmp
= compare_blob(&clp
->cl_name
, name
);
1743 node
= node
->rb_left
;
1745 node
= node
->rb_right
;
1753 add_to_unconfirmed(struct nfs4_client
*clp
)
1755 unsigned int idhashval
;
1756 struct nfsd_net
*nn
= net_generic(clp
->net
, nfsd_net_id
);
1758 clear_bit(NFSD4_CLIENT_CONFIRMED
, &clp
->cl_flags
);
1759 add_clp_to_name_tree(clp
, &nn
->unconf_name_tree
);
1760 idhashval
= clientid_hashval(clp
->cl_clientid
.cl_id
);
1761 list_add(&clp
->cl_idhash
, &nn
->unconf_id_hashtbl
[idhashval
]);
1766 move_to_confirmed(struct nfs4_client
*clp
)
1768 unsigned int idhashval
= clientid_hashval(clp
->cl_clientid
.cl_id
);
1769 struct nfsd_net
*nn
= net_generic(clp
->net
, nfsd_net_id
);
1771 dprintk("NFSD: move_to_confirm nfs4_client %p\n", clp
);
1772 list_move(&clp
->cl_idhash
, &nn
->conf_id_hashtbl
[idhashval
]);
1773 rb_erase(&clp
->cl_namenode
, &nn
->unconf_name_tree
);
1774 add_clp_to_name_tree(clp
, &nn
->conf_name_tree
);
1775 set_bit(NFSD4_CLIENT_CONFIRMED
, &clp
->cl_flags
);
1779 static struct nfs4_client
*
1780 find_client_in_id_table(struct list_head
*tbl
, clientid_t
*clid
, bool sessions
)
1782 struct nfs4_client
*clp
;
1783 unsigned int idhashval
= clientid_hashval(clid
->cl_id
);
1785 list_for_each_entry(clp
, &tbl
[idhashval
], cl_idhash
) {
1786 if (same_clid(&clp
->cl_clientid
, clid
)) {
1787 if ((bool)clp
->cl_minorversion
!= sessions
)
1796 static struct nfs4_client
*
1797 find_confirmed_client(clientid_t
*clid
, bool sessions
, struct nfsd_net
*nn
)
1799 struct list_head
*tbl
= nn
->conf_id_hashtbl
;
1801 return find_client_in_id_table(tbl
, clid
, sessions
);
1804 static struct nfs4_client
*
1805 find_unconfirmed_client(clientid_t
*clid
, bool sessions
, struct nfsd_net
*nn
)
1807 struct list_head
*tbl
= nn
->unconf_id_hashtbl
;
1809 return find_client_in_id_table(tbl
, clid
, sessions
);
1812 static bool clp_used_exchangeid(struct nfs4_client
*clp
)
1814 return clp
->cl_exchange_flags
!= 0;
1817 static struct nfs4_client
*
1818 find_confirmed_client_by_name(struct xdr_netobj
*name
, struct nfsd_net
*nn
)
1820 return find_clp_in_name_tree(name
, &nn
->conf_name_tree
);
1823 static struct nfs4_client
*
1824 find_unconfirmed_client_by_name(struct xdr_netobj
*name
, struct nfsd_net
*nn
)
1826 return find_clp_in_name_tree(name
, &nn
->unconf_name_tree
);
1830 gen_callback(struct nfs4_client
*clp
, struct nfsd4_setclientid
*se
, struct svc_rqst
*rqstp
)
1832 struct nfs4_cb_conn
*conn
= &clp
->cl_cb_conn
;
1833 struct sockaddr
*sa
= svc_addr(rqstp
);
1834 u32 scopeid
= rpc_get_scope_id(sa
);
1835 unsigned short expected_family
;
1837 /* Currently, we only support tcp and tcp6 for the callback channel */
1838 if (se
->se_callback_netid_len
== 3 &&
1839 !memcmp(se
->se_callback_netid_val
, "tcp", 3))
1840 expected_family
= AF_INET
;
1841 else if (se
->se_callback_netid_len
== 4 &&
1842 !memcmp(se
->se_callback_netid_val
, "tcp6", 4))
1843 expected_family
= AF_INET6
;
1847 conn
->cb_addrlen
= rpc_uaddr2sockaddr(clp
->net
, se
->se_callback_addr_val
,
1848 se
->se_callback_addr_len
,
1849 (struct sockaddr
*)&conn
->cb_addr
,
1850 sizeof(conn
->cb_addr
));
1852 if (!conn
->cb_addrlen
|| conn
->cb_addr
.ss_family
!= expected_family
)
1855 if (conn
->cb_addr
.ss_family
== AF_INET6
)
1856 ((struct sockaddr_in6
*)&conn
->cb_addr
)->sin6_scope_id
= scopeid
;
1858 conn
->cb_prog
= se
->se_callback_prog
;
1859 conn
->cb_ident
= se
->se_callback_ident
;
1860 memcpy(&conn
->cb_saddr
, &rqstp
->rq_daddr
, rqstp
->rq_daddrlen
);
1863 conn
->cb_addr
.ss_family
= AF_UNSPEC
;
1864 conn
->cb_addrlen
= 0;
1865 dprintk(KERN_INFO
"NFSD: this client (clientid %08x/%08x) "
1866 "will not receive delegations\n",
1867 clp
->cl_clientid
.cl_boot
, clp
->cl_clientid
.cl_id
);
1873 * Cache a reply. nfsd4_check_resp_size() has bounded the cache size.
1876 nfsd4_store_cache_entry(struct nfsd4_compoundres
*resp
)
1878 struct xdr_buf
*buf
= resp
->xdr
.buf
;
1879 struct nfsd4_slot
*slot
= resp
->cstate
.slot
;
1882 dprintk("--> %s slot %p\n", __func__
, slot
);
1884 slot
->sl_opcnt
= resp
->opcnt
;
1885 slot
->sl_status
= resp
->cstate
.status
;
1887 slot
->sl_flags
|= NFSD4_SLOT_INITIALIZED
;
1888 if (nfsd4_not_cached(resp
)) {
1889 slot
->sl_datalen
= 0;
1892 base
= resp
->cstate
.data_offset
;
1893 slot
->sl_datalen
= buf
->len
- base
;
1894 if (read_bytes_from_xdr_buf(buf
, base
, slot
->sl_data
, slot
->sl_datalen
))
1895 WARN("%s: sessions DRC could not cache compound\n", __func__
);
1900 * Encode the replay sequence operation from the slot values.
1901 * If cachethis is FALSE encode the uncached rep error on the next
1902 * operation which sets resp->p and increments resp->opcnt for
1903 * nfs4svc_encode_compoundres.
1907 nfsd4_enc_sequence_replay(struct nfsd4_compoundargs
*args
,
1908 struct nfsd4_compoundres
*resp
)
1910 struct nfsd4_op
*op
;
1911 struct nfsd4_slot
*slot
= resp
->cstate
.slot
;
1913 /* Encode the replayed sequence operation */
1914 op
= &args
->ops
[resp
->opcnt
- 1];
1915 nfsd4_encode_operation(resp
, op
);
1917 /* Return nfserr_retry_uncached_rep in next operation. */
1918 if (args
->opcnt
> 1 && !(slot
->sl_flags
& NFSD4_SLOT_CACHETHIS
)) {
1919 op
= &args
->ops
[resp
->opcnt
++];
1920 op
->status
= nfserr_retry_uncached_rep
;
1921 nfsd4_encode_operation(resp
, op
);
1927 * The sequence operation is not cached because we can use the slot and
1931 nfsd4_replay_cache_entry(struct nfsd4_compoundres
*resp
,
1932 struct nfsd4_sequence
*seq
)
1934 struct nfsd4_slot
*slot
= resp
->cstate
.slot
;
1935 struct xdr_stream
*xdr
= &resp
->xdr
;
1939 dprintk("--> %s slot %p\n", __func__
, slot
);
1941 status
= nfsd4_enc_sequence_replay(resp
->rqstp
->rq_argp
, resp
);
1945 p
= xdr_reserve_space(xdr
, slot
->sl_datalen
);
1948 return nfserr_serverfault
;
1950 xdr_encode_opaque_fixed(p
, slot
->sl_data
, slot
->sl_datalen
);
1951 xdr_commit_encode(xdr
);
1953 resp
->opcnt
= slot
->sl_opcnt
;
1954 return slot
->sl_status
;
1958 * Set the exchange_id flags returned by the server.
1961 nfsd4_set_ex_flags(struct nfs4_client
*new, struct nfsd4_exchange_id
*clid
)
1963 /* pNFS is not supported */
1964 new->cl_exchange_flags
|= EXCHGID4_FLAG_USE_NON_PNFS
;
1966 /* Referrals are supported, Migration is not. */
1967 new->cl_exchange_flags
|= EXCHGID4_FLAG_SUPP_MOVED_REFER
;
1969 /* set the wire flags to return to client. */
1970 clid
->flags
= new->cl_exchange_flags
;
1973 static bool client_has_state(struct nfs4_client
*clp
)
1976 * Note clp->cl_openowners check isn't quite right: there's no
1977 * need to count owners without stateid's.
1979 * Also note we should probably be using this in 4.0 case too.
1981 return !list_empty(&clp
->cl_openowners
)
1982 || !list_empty(&clp
->cl_delegations
)
1983 || !list_empty(&clp
->cl_sessions
);
1987 nfsd4_exchange_id(struct svc_rqst
*rqstp
,
1988 struct nfsd4_compound_state
*cstate
,
1989 struct nfsd4_exchange_id
*exid
)
1991 struct nfs4_client
*unconf
, *conf
, *new;
1993 char addr_str
[INET6_ADDRSTRLEN
];
1994 nfs4_verifier verf
= exid
->verifier
;
1995 struct sockaddr
*sa
= svc_addr(rqstp
);
1996 bool update
= exid
->flags
& EXCHGID4_FLAG_UPD_CONFIRMED_REC_A
;
1997 struct nfsd_net
*nn
= net_generic(SVC_NET(rqstp
), nfsd_net_id
);
1999 rpc_ntop(sa
, addr_str
, sizeof(addr_str
));
2000 dprintk("%s rqstp=%p exid=%p clname.len=%u clname.data=%p "
2001 "ip_addr=%s flags %x, spa_how %d\n",
2002 __func__
, rqstp
, exid
, exid
->clname
.len
, exid
->clname
.data
,
2003 addr_str
, exid
->flags
, exid
->spa_how
);
2005 if (exid
->flags
& ~EXCHGID4_FLAG_MASK_A
)
2006 return nfserr_inval
;
2008 switch (exid
->spa_how
) {
2010 if (!svc_rqst_integrity_protected(rqstp
))
2011 return nfserr_inval
;
2014 default: /* checked by xdr code */
2017 return nfserr_encr_alg_unsupp
;
2020 /* Cases below refer to rfc 5661 section 18.35.4: */
2022 conf
= find_confirmed_client_by_name(&exid
->clname
, nn
);
2024 bool creds_match
= same_creds(&conf
->cl_cred
, &rqstp
->rq_cred
);
2025 bool verfs_match
= same_verf(&verf
, &conf
->cl_verifier
);
2028 if (!clp_used_exchangeid(conf
)) { /* buggy client */
2029 status
= nfserr_inval
;
2032 if (!mach_creds_match(conf
, rqstp
)) {
2033 status
= nfserr_wrong_cred
;
2036 if (!creds_match
) { /* case 9 */
2037 status
= nfserr_perm
;
2040 if (!verfs_match
) { /* case 8 */
2041 status
= nfserr_not_same
;
2045 exid
->flags
|= EXCHGID4_FLAG_CONFIRMED_R
;
2049 if (!creds_match
) { /* case 3 */
2050 if (client_has_state(conf
)) {
2051 status
= nfserr_clid_inuse
;
2054 expire_client(conf
);
2057 if (verfs_match
) { /* case 2 */
2058 conf
->cl_exchange_flags
|= EXCHGID4_FLAG_CONFIRMED_R
;
2062 /* case 5, client reboot */
2066 if (update
) { /* case 7 */
2067 status
= nfserr_noent
;
2071 unconf
= find_unconfirmed_client_by_name(&exid
->clname
, nn
);
2072 if (unconf
) /* case 4, possible retry or client restart */
2073 expire_client(unconf
);
2075 /* case 1 (normal case) */
2077 new = create_client(exid
->clname
, rqstp
, &verf
);
2079 status
= nfserr_jukebox
;
2082 new->cl_minorversion
= cstate
->minorversion
;
2083 new->cl_mach_cred
= (exid
->spa_how
== SP4_MACH_CRED
);
2086 add_to_unconfirmed(new);
2088 exid
->clientid
.cl_boot
= new->cl_clientid
.cl_boot
;
2089 exid
->clientid
.cl_id
= new->cl_clientid
.cl_id
;
2091 exid
->seqid
= new->cl_cs_slot
.sl_seqid
+ 1;
2092 nfsd4_set_ex_flags(new, exid
);
2094 dprintk("nfsd4_exchange_id seqid %d flags %x\n",
2095 new->cl_cs_slot
.sl_seqid
, new->cl_exchange_flags
);
2099 nfs4_unlock_state();
2104 check_slot_seqid(u32 seqid
, u32 slot_seqid
, int slot_inuse
)
2106 dprintk("%s enter. seqid %d slot_seqid %d\n", __func__
, seqid
,
2109 /* The slot is in use, and no response has been sent. */
2111 if (seqid
== slot_seqid
)
2112 return nfserr_jukebox
;
2114 return nfserr_seq_misordered
;
2116 /* Note unsigned 32-bit arithmetic handles wraparound: */
2117 if (likely(seqid
== slot_seqid
+ 1))
2119 if (seqid
== slot_seqid
)
2120 return nfserr_replay_cache
;
2121 return nfserr_seq_misordered
;
2125 * Cache the create session result into the create session single DRC
2126 * slot cache by saving the xdr structure. sl_seqid has been set.
2127 * Do this for solo or embedded create session operations.
2130 nfsd4_cache_create_session(struct nfsd4_create_session
*cr_ses
,
2131 struct nfsd4_clid_slot
*slot
, __be32 nfserr
)
2133 slot
->sl_status
= nfserr
;
2134 memcpy(&slot
->sl_cr_ses
, cr_ses
, sizeof(*cr_ses
));
2138 nfsd4_replay_create_session(struct nfsd4_create_session
*cr_ses
,
2139 struct nfsd4_clid_slot
*slot
)
2141 memcpy(cr_ses
, &slot
->sl_cr_ses
, sizeof(*cr_ses
));
2142 return slot
->sl_status
;
2145 #define NFSD_MIN_REQ_HDR_SEQ_SZ ((\
2146 2 * 2 + /* credential,verifier: AUTH_NULL, length 0 */ \
2147 1 + /* MIN tag is length with zero, only length */ \
2148 3 + /* version, opcount, opcode */ \
2149 XDR_QUADLEN(NFS4_MAX_SESSIONID_LEN) + \
2150 /* seqid, slotID, slotID, cache */ \
2151 4 ) * sizeof(__be32))
2153 #define NFSD_MIN_RESP_HDR_SEQ_SZ ((\
2154 2 + /* verifier: AUTH_NULL, length 0 */\
2156 1 + /* MIN tag is length with zero, only length */ \
2157 3 + /* opcount, opcode, opstatus*/ \
2158 XDR_QUADLEN(NFS4_MAX_SESSIONID_LEN) + \
2159 /* seqid, slotID, slotID, slotID, status */ \
2160 5 ) * sizeof(__be32))
2162 static __be32
check_forechannel_attrs(struct nfsd4_channel_attrs
*ca
, struct nfsd_net
*nn
)
2164 u32 maxrpc
= nn
->nfsd_serv
->sv_max_mesg
;
2166 if (ca
->maxreq_sz
< NFSD_MIN_REQ_HDR_SEQ_SZ
)
2167 return nfserr_toosmall
;
2168 if (ca
->maxresp_sz
< NFSD_MIN_RESP_HDR_SEQ_SZ
)
2169 return nfserr_toosmall
;
2170 ca
->headerpadsz
= 0;
2171 ca
->maxreq_sz
= min_t(u32
, ca
->maxreq_sz
, maxrpc
);
2172 ca
->maxresp_sz
= min_t(u32
, ca
->maxresp_sz
, maxrpc
);
2173 ca
->maxops
= min_t(u32
, ca
->maxops
, NFSD_MAX_OPS_PER_COMPOUND
);
2174 ca
->maxresp_cached
= min_t(u32
, ca
->maxresp_cached
,
2175 NFSD_SLOT_CACHE_SIZE
+ NFSD_MIN_HDR_SEQ_SZ
);
2176 ca
->maxreqs
= min_t(u32
, ca
->maxreqs
, NFSD_MAX_SLOTS_PER_SESSION
);
2178 * Note decreasing slot size below client's request may make it
2179 * difficult for client to function correctly, whereas
2180 * decreasing the number of slots will (just?) affect
2181 * performance. When short on memory we therefore prefer to
2182 * decrease number of slots instead of their size. Clients that
2183 * request larger slots than they need will get poor results:
2185 ca
->maxreqs
= nfsd4_get_drc_mem(ca
);
2187 return nfserr_jukebox
;
2192 #define NFSD_CB_MAX_REQ_SZ ((NFS4_enc_cb_recall_sz + \
2193 RPC_MAX_HEADER_WITH_AUTH) * sizeof(__be32))
2194 #define NFSD_CB_MAX_RESP_SZ ((NFS4_dec_cb_recall_sz + \
2195 RPC_MAX_REPHEADER_WITH_AUTH) * sizeof(__be32))
2197 static __be32
check_backchannel_attrs(struct nfsd4_channel_attrs
*ca
)
2199 ca
->headerpadsz
= 0;
2202 * These RPC_MAX_HEADER macros are overkill, especially since we
2203 * don't even do gss on the backchannel yet. But this is still
2204 * less than 1k. Tighten up this estimate in the unlikely event
2205 * it turns out to be a problem for some client:
2207 if (ca
->maxreq_sz
< NFSD_CB_MAX_REQ_SZ
)
2208 return nfserr_toosmall
;
2209 if (ca
->maxresp_sz
< NFSD_CB_MAX_RESP_SZ
)
2210 return nfserr_toosmall
;
2211 ca
->maxresp_cached
= 0;
2213 return nfserr_toosmall
;
2218 static __be32
nfsd4_check_cb_sec(struct nfsd4_cb_sec
*cbs
)
2220 switch (cbs
->flavor
) {
2226 * GSS case: the spec doesn't allow us to return this
2227 * error. But it also doesn't allow us not to support
2229 * I'd rather this fail hard than return some error the
2230 * client might think it can already handle:
2232 return nfserr_encr_alg_unsupp
;
2237 nfsd4_create_session(struct svc_rqst
*rqstp
,
2238 struct nfsd4_compound_state
*cstate
,
2239 struct nfsd4_create_session
*cr_ses
)
2241 struct sockaddr
*sa
= svc_addr(rqstp
);
2242 struct nfs4_client
*conf
, *unconf
;
2243 struct nfsd4_session
*new;
2244 struct nfsd4_conn
*conn
;
2245 struct nfsd4_clid_slot
*cs_slot
= NULL
;
2247 struct nfsd_net
*nn
= net_generic(SVC_NET(rqstp
), nfsd_net_id
);
2249 if (cr_ses
->flags
& ~SESSION4_FLAG_MASK_A
)
2250 return nfserr_inval
;
2251 status
= nfsd4_check_cb_sec(&cr_ses
->cb_sec
);
2254 status
= check_forechannel_attrs(&cr_ses
->fore_channel
, nn
);
2257 status
= check_backchannel_attrs(&cr_ses
->back_channel
);
2259 goto out_release_drc_mem
;
2260 status
= nfserr_jukebox
;
2261 new = alloc_session(&cr_ses
->fore_channel
, &cr_ses
->back_channel
);
2263 goto out_release_drc_mem
;
2264 conn
= alloc_conn_from_crses(rqstp
, cr_ses
);
2266 goto out_free_session
;
2269 unconf
= find_unconfirmed_client(&cr_ses
->clientid
, true, nn
);
2270 conf
= find_confirmed_client(&cr_ses
->clientid
, true, nn
);
2271 WARN_ON_ONCE(conf
&& unconf
);
2274 status
= nfserr_wrong_cred
;
2275 if (!mach_creds_match(conf
, rqstp
))
2277 cs_slot
= &conf
->cl_cs_slot
;
2278 status
= check_slot_seqid(cr_ses
->seqid
, cs_slot
->sl_seqid
, 0);
2279 if (status
== nfserr_replay_cache
) {
2280 status
= nfsd4_replay_create_session(cr_ses
, cs_slot
);
2282 } else if (cr_ses
->seqid
!= cs_slot
->sl_seqid
+ 1) {
2283 status
= nfserr_seq_misordered
;
2286 } else if (unconf
) {
2287 struct nfs4_client
*old
;
2288 if (!same_creds(&unconf
->cl_cred
, &rqstp
->rq_cred
) ||
2289 !rpc_cmp_addr(sa
, (struct sockaddr
*) &unconf
->cl_addr
)) {
2290 status
= nfserr_clid_inuse
;
2293 status
= nfserr_wrong_cred
;
2294 if (!mach_creds_match(unconf
, rqstp
))
2296 cs_slot
= &unconf
->cl_cs_slot
;
2297 status
= check_slot_seqid(cr_ses
->seqid
, cs_slot
->sl_seqid
, 0);
2299 /* an unconfirmed replay returns misordered */
2300 status
= nfserr_seq_misordered
;
2303 old
= find_confirmed_client_by_name(&unconf
->cl_name
, nn
);
2305 status
= mark_client_expired(old
);
2310 move_to_confirmed(unconf
);
2313 status
= nfserr_stale_clientid
;
2318 * We do not support RDMA or persistent sessions
2320 cr_ses
->flags
&= ~SESSION4_PERSIST
;
2321 cr_ses
->flags
&= ~SESSION4_RDMA
;
2323 init_session(rqstp
, new, conf
, cr_ses
);
2324 nfsd4_init_conn(rqstp
, conn
, new);
2326 memcpy(cr_ses
->sessionid
.data
, new->se_sessionid
.data
,
2327 NFS4_MAX_SESSIONID_LEN
);
2328 cs_slot
->sl_seqid
++;
2329 cr_ses
->seqid
= cs_slot
->sl_seqid
;
2331 /* cache solo and embedded create sessions under the state lock */
2332 nfsd4_cache_create_session(cr_ses
, cs_slot
, status
);
2333 nfs4_unlock_state();
2336 nfs4_unlock_state();
2339 __free_session(new);
2340 out_release_drc_mem
:
2341 nfsd4_put_drc_mem(&cr_ses
->fore_channel
);
2345 static __be32
nfsd4_map_bcts_dir(u32
*dir
)
2348 case NFS4_CDFC4_FORE
:
2349 case NFS4_CDFC4_BACK
:
2351 case NFS4_CDFC4_FORE_OR_BOTH
:
2352 case NFS4_CDFC4_BACK_OR_BOTH
:
2353 *dir
= NFS4_CDFC4_BOTH
;
2356 return nfserr_inval
;
2359 __be32
nfsd4_backchannel_ctl(struct svc_rqst
*rqstp
, struct nfsd4_compound_state
*cstate
, struct nfsd4_backchannel_ctl
*bc
)
2361 struct nfsd4_session
*session
= cstate
->session
;
2362 struct nfsd_net
*nn
= net_generic(SVC_NET(rqstp
), nfsd_net_id
);
2365 status
= nfsd4_check_cb_sec(&bc
->bc_cb_sec
);
2368 spin_lock(&nn
->client_lock
);
2369 session
->se_cb_prog
= bc
->bc_cb_program
;
2370 session
->se_cb_sec
= bc
->bc_cb_sec
;
2371 spin_unlock(&nn
->client_lock
);
2373 nfsd4_probe_callback(session
->se_client
);
2378 __be32
nfsd4_bind_conn_to_session(struct svc_rqst
*rqstp
,
2379 struct nfsd4_compound_state
*cstate
,
2380 struct nfsd4_bind_conn_to_session
*bcts
)
2383 struct nfsd4_conn
*conn
;
2384 struct nfsd4_session
*session
;
2385 struct net
*net
= SVC_NET(rqstp
);
2386 struct nfsd_net
*nn
= net_generic(net
, nfsd_net_id
);
2388 if (!nfsd4_last_compound_op(rqstp
))
2389 return nfserr_not_only_op
;
2391 spin_lock(&nn
->client_lock
);
2392 session
= find_in_sessionid_hashtbl(&bcts
->sessionid
, net
, &status
);
2393 spin_unlock(&nn
->client_lock
);
2395 goto out_no_session
;
2396 status
= nfserr_wrong_cred
;
2397 if (!mach_creds_match(session
->se_client
, rqstp
))
2399 status
= nfsd4_map_bcts_dir(&bcts
->dir
);
2402 conn
= alloc_conn(rqstp
, bcts
->dir
);
2403 status
= nfserr_jukebox
;
2406 nfsd4_init_conn(rqstp
, conn
, session
);
2409 nfsd4_put_session(session
);
2411 nfs4_unlock_state();
2415 static bool nfsd4_compound_in_session(struct nfsd4_session
*session
, struct nfs4_sessionid
*sid
)
2419 return !memcmp(sid
, &session
->se_sessionid
, sizeof(*sid
));
2423 nfsd4_destroy_session(struct svc_rqst
*r
,
2424 struct nfsd4_compound_state
*cstate
,
2425 struct nfsd4_destroy_session
*sessionid
)
2427 struct nfsd4_session
*ses
;
2429 int ref_held_by_me
= 0;
2430 struct net
*net
= SVC_NET(r
);
2431 struct nfsd_net
*nn
= net_generic(net
, nfsd_net_id
);
2434 status
= nfserr_not_only_op
;
2435 if (nfsd4_compound_in_session(cstate
->session
, &sessionid
->sessionid
)) {
2436 if (!nfsd4_last_compound_op(r
))
2440 dump_sessionid(__func__
, &sessionid
->sessionid
);
2441 spin_lock(&nn
->client_lock
);
2442 ses
= find_in_sessionid_hashtbl(&sessionid
->sessionid
, net
, &status
);
2444 goto out_client_lock
;
2445 status
= nfserr_wrong_cred
;
2446 if (!mach_creds_match(ses
->se_client
, r
))
2447 goto out_put_session
;
2448 status
= mark_session_dead_locked(ses
, 1 + ref_held_by_me
);
2450 goto out_put_session
;
2451 unhash_session(ses
);
2452 spin_unlock(&nn
->client_lock
);
2454 nfsd4_probe_callback_sync(ses
->se_client
);
2456 spin_lock(&nn
->client_lock
);
2459 nfsd4_put_session_locked(ses
);
2461 spin_unlock(&nn
->client_lock
);
2463 nfs4_unlock_state();
2467 static struct nfsd4_conn
*__nfsd4_find_conn(struct svc_xprt
*xpt
, struct nfsd4_session
*s
)
2469 struct nfsd4_conn
*c
;
2471 list_for_each_entry(c
, &s
->se_conns
, cn_persession
) {
2472 if (c
->cn_xprt
== xpt
) {
2479 static __be32
nfsd4_sequence_check_conn(struct nfsd4_conn
*new, struct nfsd4_session
*ses
)
2481 struct nfs4_client
*clp
= ses
->se_client
;
2482 struct nfsd4_conn
*c
;
2483 __be32 status
= nfs_ok
;
2486 spin_lock(&clp
->cl_lock
);
2487 c
= __nfsd4_find_conn(new->cn_xprt
, ses
);
2490 status
= nfserr_conn_not_bound_to_session
;
2491 if (clp
->cl_mach_cred
)
2493 __nfsd4_hash_conn(new, ses
);
2494 spin_unlock(&clp
->cl_lock
);
2495 ret
= nfsd4_register_conn(new);
2497 /* oops; xprt is already down: */
2498 nfsd4_conn_lost(&new->cn_xpt_user
);
2501 spin_unlock(&clp
->cl_lock
);
2506 static bool nfsd4_session_too_many_ops(struct svc_rqst
*rqstp
, struct nfsd4_session
*session
)
2508 struct nfsd4_compoundargs
*args
= rqstp
->rq_argp
;
2510 return args
->opcnt
> session
->se_fchannel
.maxops
;
2513 static bool nfsd4_request_too_big(struct svc_rqst
*rqstp
,
2514 struct nfsd4_session
*session
)
2516 struct xdr_buf
*xb
= &rqstp
->rq_arg
;
2518 return xb
->len
> session
->se_fchannel
.maxreq_sz
;
2522 nfsd4_sequence(struct svc_rqst
*rqstp
,
2523 struct nfsd4_compound_state
*cstate
,
2524 struct nfsd4_sequence
*seq
)
2526 struct nfsd4_compoundres
*resp
= rqstp
->rq_resp
;
2527 struct xdr_stream
*xdr
= &resp
->xdr
;
2528 struct nfsd4_session
*session
;
2529 struct nfs4_client
*clp
;
2530 struct nfsd4_slot
*slot
;
2531 struct nfsd4_conn
*conn
;
2534 struct net
*net
= SVC_NET(rqstp
);
2535 struct nfsd_net
*nn
= net_generic(net
, nfsd_net_id
);
2537 if (resp
->opcnt
!= 1)
2538 return nfserr_sequence_pos
;
2541 * Will be either used or freed by nfsd4_sequence_check_conn
2544 conn
= alloc_conn(rqstp
, NFS4_CDFC4_FORE
);
2546 return nfserr_jukebox
;
2548 spin_lock(&nn
->client_lock
);
2549 session
= find_in_sessionid_hashtbl(&seq
->sessionid
, net
, &status
);
2551 goto out_no_session
;
2552 clp
= session
->se_client
;
2554 status
= nfserr_too_many_ops
;
2555 if (nfsd4_session_too_many_ops(rqstp
, session
))
2556 goto out_put_session
;
2558 status
= nfserr_req_too_big
;
2559 if (nfsd4_request_too_big(rqstp
, session
))
2560 goto out_put_session
;
2562 status
= nfserr_badslot
;
2563 if (seq
->slotid
>= session
->se_fchannel
.maxreqs
)
2564 goto out_put_session
;
2566 slot
= session
->se_slots
[seq
->slotid
];
2567 dprintk("%s: slotid %d\n", __func__
, seq
->slotid
);
2569 /* We do not negotiate the number of slots yet, so set the
2570 * maxslots to the session maxreqs which is used to encode
2571 * sr_highest_slotid and the sr_target_slot id to maxslots */
2572 seq
->maxslots
= session
->se_fchannel
.maxreqs
;
2574 status
= check_slot_seqid(seq
->seqid
, slot
->sl_seqid
,
2575 slot
->sl_flags
& NFSD4_SLOT_INUSE
);
2576 if (status
== nfserr_replay_cache
) {
2577 status
= nfserr_seq_misordered
;
2578 if (!(slot
->sl_flags
& NFSD4_SLOT_INITIALIZED
))
2579 goto out_put_session
;
2580 cstate
->slot
= slot
;
2581 cstate
->session
= session
;
2583 /* Return the cached reply status and set cstate->status
2584 * for nfsd4_proc_compound processing */
2585 status
= nfsd4_replay_cache_entry(resp
, seq
);
2586 cstate
->status
= nfserr_replay_cache
;
2590 goto out_put_session
;
2592 status
= nfsd4_sequence_check_conn(conn
, session
);
2595 goto out_put_session
;
2597 buflen
= (seq
->cachethis
) ?
2598 session
->se_fchannel
.maxresp_cached
:
2599 session
->se_fchannel
.maxresp_sz
;
2600 status
= (seq
->cachethis
) ? nfserr_rep_too_big_to_cache
:
2602 if (xdr_restrict_buflen(xdr
, buflen
- rqstp
->rq_auth_slack
))
2603 goto out_put_session
;
2604 svc_reserve(rqstp
, buflen
);
2607 /* Success! bump slot seqid */
2608 slot
->sl_seqid
= seq
->seqid
;
2609 slot
->sl_flags
|= NFSD4_SLOT_INUSE
;
2611 slot
->sl_flags
|= NFSD4_SLOT_CACHETHIS
;
2613 slot
->sl_flags
&= ~NFSD4_SLOT_CACHETHIS
;
2615 cstate
->slot
= slot
;
2616 cstate
->session
= session
;
2620 switch (clp
->cl_cb_state
) {
2622 seq
->status_flags
= SEQ4_STATUS_CB_PATH_DOWN
;
2624 case NFSD4_CB_FAULT
:
2625 seq
->status_flags
= SEQ4_STATUS_BACKCHANNEL_FAULT
;
2628 seq
->status_flags
= 0;
2630 if (!list_empty(&clp
->cl_revoked
))
2631 seq
->status_flags
|= SEQ4_STATUS_RECALLABLE_STATE_REVOKED
;
2635 spin_unlock(&nn
->client_lock
);
2638 nfsd4_put_session_locked(session
);
2639 goto out_no_session
;
2643 nfsd4_sequence_done(struct nfsd4_compoundres
*resp
)
2645 struct nfsd4_compound_state
*cs
= &resp
->cstate
;
2647 if (nfsd4_has_session(cs
)) {
2648 if (cs
->status
!= nfserr_replay_cache
) {
2649 nfsd4_store_cache_entry(resp
);
2650 cs
->slot
->sl_flags
&= ~NFSD4_SLOT_INUSE
;
2652 /* Drop session reference that was taken in nfsd4_sequence() */
2653 nfsd4_put_session(cs
->session
);
2655 put_client_renew(cs
->clp
);
2659 nfsd4_destroy_clientid(struct svc_rqst
*rqstp
, struct nfsd4_compound_state
*cstate
, struct nfsd4_destroy_clientid
*dc
)
2661 struct nfs4_client
*conf
, *unconf
, *clp
;
2663 struct nfsd_net
*nn
= net_generic(SVC_NET(rqstp
), nfsd_net_id
);
2666 unconf
= find_unconfirmed_client(&dc
->clientid
, true, nn
);
2667 conf
= find_confirmed_client(&dc
->clientid
, true, nn
);
2668 WARN_ON_ONCE(conf
&& unconf
);
2673 if (client_has_state(conf
)) {
2674 status
= nfserr_clientid_busy
;
2680 status
= nfserr_stale_clientid
;
2683 if (!mach_creds_match(clp
, rqstp
)) {
2684 status
= nfserr_wrong_cred
;
2689 nfs4_unlock_state();
2694 nfsd4_reclaim_complete(struct svc_rqst
*rqstp
, struct nfsd4_compound_state
*cstate
, struct nfsd4_reclaim_complete
*rc
)
2698 if (rc
->rca_one_fs
) {
2699 if (!cstate
->current_fh
.fh_dentry
)
2700 return nfserr_nofilehandle
;
2702 * We don't take advantage of the rca_one_fs case.
2703 * That's OK, it's optional, we can safely ignore it.
2709 status
= nfserr_complete_already
;
2710 if (test_and_set_bit(NFSD4_CLIENT_RECLAIM_COMPLETE
,
2711 &cstate
->session
->se_client
->cl_flags
))
2714 status
= nfserr_stale_clientid
;
2715 if (is_client_expired(cstate
->session
->se_client
))
2717 * The following error isn't really legal.
2718 * But we only get here if the client just explicitly
2719 * destroyed the client. Surely it no longer cares what
2720 * error it gets back on an operation for the dead
2726 nfsd4_client_record_create(cstate
->session
->se_client
);
2728 nfs4_unlock_state();
2733 nfsd4_setclientid(struct svc_rqst
*rqstp
, struct nfsd4_compound_state
*cstate
,
2734 struct nfsd4_setclientid
*setclid
)
2736 struct xdr_netobj clname
= setclid
->se_name
;
2737 nfs4_verifier clverifier
= setclid
->se_verf
;
2738 struct nfs4_client
*conf
, *unconf
, *new;
2740 struct nfsd_net
*nn
= net_generic(SVC_NET(rqstp
), nfsd_net_id
);
2742 /* Cases below refer to rfc 3530 section 14.2.33: */
2744 conf
= find_confirmed_client_by_name(&clname
, nn
);
2747 status
= nfserr_clid_inuse
;
2748 if (clp_used_exchangeid(conf
))
2750 if (!same_creds(&conf
->cl_cred
, &rqstp
->rq_cred
)) {
2751 char addr_str
[INET6_ADDRSTRLEN
];
2752 rpc_ntop((struct sockaddr
*) &conf
->cl_addr
, addr_str
,
2754 dprintk("NFSD: setclientid: string in use by client "
2755 "at %s\n", addr_str
);
2759 unconf
= find_unconfirmed_client_by_name(&clname
, nn
);
2761 expire_client(unconf
);
2762 status
= nfserr_jukebox
;
2763 new = create_client(clname
, rqstp
, &clverifier
);
2766 if (conf
&& same_verf(&conf
->cl_verifier
, &clverifier
))
2767 /* case 1: probable callback update */
2768 copy_clid(new, conf
);
2769 else /* case 4 (new client) or cases 2, 3 (client reboot): */
2771 new->cl_minorversion
= 0;
2772 gen_callback(new, setclid
, rqstp
);
2773 add_to_unconfirmed(new);
2774 setclid
->se_clientid
.cl_boot
= new->cl_clientid
.cl_boot
;
2775 setclid
->se_clientid
.cl_id
= new->cl_clientid
.cl_id
;
2776 memcpy(setclid
->se_confirm
.data
, new->cl_confirm
.data
, sizeof(setclid
->se_confirm
.data
));
2779 nfs4_unlock_state();
2785 nfsd4_setclientid_confirm(struct svc_rqst
*rqstp
,
2786 struct nfsd4_compound_state
*cstate
,
2787 struct nfsd4_setclientid_confirm
*setclientid_confirm
)
2789 struct nfs4_client
*conf
, *unconf
;
2790 nfs4_verifier confirm
= setclientid_confirm
->sc_confirm
;
2791 clientid_t
* clid
= &setclientid_confirm
->sc_clientid
;
2793 struct nfsd_net
*nn
= net_generic(SVC_NET(rqstp
), nfsd_net_id
);
2795 if (STALE_CLIENTID(clid
, nn
))
2796 return nfserr_stale_clientid
;
2799 conf
= find_confirmed_client(clid
, false, nn
);
2800 unconf
= find_unconfirmed_client(clid
, false, nn
);
2802 * We try hard to give out unique clientid's, so if we get an
2803 * attempt to confirm the same clientid with a different cred,
2804 * there's a bug somewhere. Let's charitably assume it's our
2807 status
= nfserr_serverfault
;
2808 if (unconf
&& !same_creds(&unconf
->cl_cred
, &rqstp
->rq_cred
))
2810 if (conf
&& !same_creds(&conf
->cl_cred
, &rqstp
->rq_cred
))
2812 /* cases below refer to rfc 3530 section 14.2.34: */
2813 if (!unconf
|| !same_verf(&confirm
, &unconf
->cl_confirm
)) {
2814 if (conf
&& !unconf
) /* case 2: probable retransmit */
2816 else /* case 4: client hasn't noticed we rebooted yet? */
2817 status
= nfserr_stale_clientid
;
2821 if (conf
) { /* case 1: callback update */
2822 nfsd4_change_callback(conf
, &unconf
->cl_cb_conn
);
2823 nfsd4_probe_callback(conf
);
2824 expire_client(unconf
);
2825 } else { /* case 3: normal case; new or rebooted client */
2826 conf
= find_confirmed_client_by_name(&unconf
->cl_name
, nn
);
2828 status
= mark_client_expired(conf
);
2831 expire_client(conf
);
2833 move_to_confirmed(unconf
);
2834 nfsd4_probe_callback(unconf
);
2837 nfs4_unlock_state();
2841 static struct nfs4_file
*nfsd4_alloc_file(void)
2843 return kmem_cache_alloc(file_slab
, GFP_KERNEL
);
2846 /* OPEN Share state helper functions */
2847 static void nfsd4_init_file(struct nfs4_file
*fp
, struct knfsd_fh
*fh
)
2849 unsigned int hashval
= file_hashval(fh
);
2851 lockdep_assert_held(&state_lock
);
2853 atomic_set(&fp
->fi_ref
, 1);
2854 spin_lock_init(&fp
->fi_lock
);
2855 INIT_LIST_HEAD(&fp
->fi_stateids
);
2856 INIT_LIST_HEAD(&fp
->fi_delegations
);
2857 fh_copy_shallow(&fp
->fi_fhandle
, fh
);
2858 fp
->fi_had_conflict
= false;
2859 fp
->fi_lease
= NULL
;
2860 fp
->fi_share_deny
= 0;
2861 memset(fp
->fi_fds
, 0, sizeof(fp
->fi_fds
));
2862 memset(fp
->fi_access
, 0, sizeof(fp
->fi_access
));
2863 hlist_add_head(&fp
->fi_hash
, &file_hashtbl
[hashval
]);
2867 nfsd4_free_slabs(void)
2869 kmem_cache_destroy(openowner_slab
);
2870 kmem_cache_destroy(lockowner_slab
);
2871 kmem_cache_destroy(file_slab
);
2872 kmem_cache_destroy(stateid_slab
);
2873 kmem_cache_destroy(deleg_slab
);
2877 nfsd4_init_slabs(void)
2879 openowner_slab
= kmem_cache_create("nfsd4_openowners",
2880 sizeof(struct nfs4_openowner
), 0, 0, NULL
);
2881 if (openowner_slab
== NULL
)
2883 lockowner_slab
= kmem_cache_create("nfsd4_lockowners",
2884 sizeof(struct nfs4_lockowner
), 0, 0, NULL
);
2885 if (lockowner_slab
== NULL
)
2886 goto out_free_openowner_slab
;
2887 file_slab
= kmem_cache_create("nfsd4_files",
2888 sizeof(struct nfs4_file
), 0, 0, NULL
);
2889 if (file_slab
== NULL
)
2890 goto out_free_lockowner_slab
;
2891 stateid_slab
= kmem_cache_create("nfsd4_stateids",
2892 sizeof(struct nfs4_ol_stateid
), 0, 0, NULL
);
2893 if (stateid_slab
== NULL
)
2894 goto out_free_file_slab
;
2895 deleg_slab
= kmem_cache_create("nfsd4_delegations",
2896 sizeof(struct nfs4_delegation
), 0, 0, NULL
);
2897 if (deleg_slab
== NULL
)
2898 goto out_free_stateid_slab
;
2901 out_free_stateid_slab
:
2902 kmem_cache_destroy(stateid_slab
);
2904 kmem_cache_destroy(file_slab
);
2905 out_free_lockowner_slab
:
2906 kmem_cache_destroy(lockowner_slab
);
2907 out_free_openowner_slab
:
2908 kmem_cache_destroy(openowner_slab
);
2910 dprintk("nfsd4: out of memory while initializing nfsv4\n");
2914 static void init_nfs4_replay(struct nfs4_replay
*rp
)
2916 rp
->rp_status
= nfserr_serverfault
;
2918 rp
->rp_buf
= rp
->rp_ibuf
;
2921 static inline void *alloc_stateowner(struct kmem_cache
*slab
, struct xdr_netobj
*owner
, struct nfs4_client
*clp
)
2923 struct nfs4_stateowner
*sop
;
2925 sop
= kmem_cache_alloc(slab
, GFP_KERNEL
);
2929 sop
->so_owner
.data
= kmemdup(owner
->data
, owner
->len
, GFP_KERNEL
);
2930 if (!sop
->so_owner
.data
) {
2931 kmem_cache_free(slab
, sop
);
2934 sop
->so_owner
.len
= owner
->len
;
2936 INIT_LIST_HEAD(&sop
->so_stateids
);
2937 sop
->so_client
= clp
;
2938 init_nfs4_replay(&sop
->so_replay
);
2942 static void hash_openowner(struct nfs4_openowner
*oo
, struct nfs4_client
*clp
, unsigned int strhashval
)
2944 struct nfsd_net
*nn
= net_generic(clp
->net
, nfsd_net_id
);
2946 list_add(&oo
->oo_owner
.so_strhash
, &nn
->ownerstr_hashtbl
[strhashval
]);
2947 list_add(&oo
->oo_perclient
, &clp
->cl_openowners
);
2950 static struct nfs4_openowner
*
2951 alloc_init_open_stateowner(unsigned int strhashval
, struct nfsd4_open
*open
,
2952 struct nfsd4_compound_state
*cstate
)
2954 struct nfs4_client
*clp
= cstate
->clp
;
2955 struct nfs4_openowner
*oo
;
2957 oo
= alloc_stateowner(openowner_slab
, &open
->op_owner
, clp
);
2960 oo
->oo_owner
.so_is_open_owner
= 1;
2961 oo
->oo_owner
.so_seqid
= open
->op_seqid
;
2962 oo
->oo_flags
= NFS4_OO_NEW
;
2963 if (nfsd4_has_session(cstate
))
2964 oo
->oo_flags
|= NFS4_OO_CONFIRMED
;
2966 oo
->oo_last_closed_stid
= NULL
;
2967 INIT_LIST_HEAD(&oo
->oo_close_lru
);
2968 hash_openowner(oo
, clp
, strhashval
);
2972 static void init_open_stateid(struct nfs4_ol_stateid
*stp
, struct nfs4_file
*fp
, struct nfsd4_open
*open
) {
2973 struct nfs4_openowner
*oo
= open
->op_openowner
;
2975 stp
->st_stid
.sc_type
= NFS4_OPEN_STID
;
2976 INIT_LIST_HEAD(&stp
->st_locks
);
2977 list_add(&stp
->st_perstateowner
, &oo
->oo_owner
.so_stateids
);
2978 stp
->st_stateowner
= &oo
->oo_owner
;
2981 stp
->st_access_bmap
= 0;
2982 stp
->st_deny_bmap
= 0;
2983 stp
->st_openstp
= NULL
;
2984 spin_lock(&fp
->fi_lock
);
2985 list_add(&stp
->st_perfile
, &fp
->fi_stateids
);
2986 spin_unlock(&fp
->fi_lock
);
2990 move_to_close_lru(struct nfs4_openowner
*oo
, struct net
*net
)
2992 struct nfsd_net
*nn
= net_generic(net
, nfsd_net_id
);
2994 dprintk("NFSD: move_to_close_lru nfs4_openowner %p\n", oo
);
2996 list_move_tail(&oo
->oo_close_lru
, &nn
->close_lru
);
2997 oo
->oo_time
= get_seconds();
3001 same_owner_str(struct nfs4_stateowner
*sop
, struct xdr_netobj
*owner
,
3004 return (sop
->so_owner
.len
== owner
->len
) &&
3005 0 == memcmp(sop
->so_owner
.data
, owner
->data
, owner
->len
) &&
3006 (sop
->so_client
->cl_clientid
.cl_id
== clid
->cl_id
);
3009 static struct nfs4_openowner
*
3010 find_openstateowner_str(unsigned int hashval
, struct nfsd4_open
*open
,
3011 bool sessions
, struct nfsd_net
*nn
)
3013 struct nfs4_stateowner
*so
;
3014 struct nfs4_openowner
*oo
;
3015 struct nfs4_client
*clp
;
3017 list_for_each_entry(so
, &nn
->ownerstr_hashtbl
[hashval
], so_strhash
) {
3018 if (!so
->so_is_open_owner
)
3020 if (same_owner_str(so
, &open
->op_owner
, &open
->op_clientid
)) {
3022 clp
= oo
->oo_owner
.so_client
;
3023 if ((bool)clp
->cl_minorversion
!= sessions
)
3025 renew_client(oo
->oo_owner
.so_client
);
3032 /* search file_hashtbl[] for file */
3033 static struct nfs4_file
*
3034 find_file_locked(struct knfsd_fh
*fh
)
3036 unsigned int hashval
= file_hashval(fh
);
3037 struct nfs4_file
*fp
;
3039 lockdep_assert_held(&state_lock
);
3041 hlist_for_each_entry(fp
, &file_hashtbl
[hashval
], fi_hash
) {
3042 if (nfsd_fh_match(&fp
->fi_fhandle
, fh
)) {
3050 static struct nfs4_file
*
3051 find_file(struct knfsd_fh
*fh
)
3053 struct nfs4_file
*fp
;
3055 spin_lock(&state_lock
);
3056 fp
= find_file_locked(fh
);
3057 spin_unlock(&state_lock
);
3061 static struct nfs4_file
*
3062 find_or_add_file(struct nfs4_file
*new, struct knfsd_fh
*fh
)
3064 struct nfs4_file
*fp
;
3066 spin_lock(&state_lock
);
3067 fp
= find_file_locked(fh
);
3069 nfsd4_init_file(new, fh
);
3072 spin_unlock(&state_lock
);
3078 * Called to check deny when READ with all zero stateid or
3079 * WRITE with all zero or all one stateid
3082 nfs4_share_conflict(struct svc_fh
*current_fh
, unsigned int deny_type
)
3084 struct nfs4_file
*fp
;
3085 __be32 ret
= nfs_ok
;
3087 fp
= find_file(¤t_fh
->fh_handle
);
3090 /* Check for conflicting share reservations */
3091 spin_lock(&fp
->fi_lock
);
3092 if (fp
->fi_share_deny
& deny_type
)
3093 ret
= nfserr_locked
;
3094 spin_unlock(&fp
->fi_lock
);
3099 void nfsd4_prepare_cb_recall(struct nfs4_delegation
*dp
)
3101 struct nfs4_client
*clp
= dp
->dl_stid
.sc_client
;
3102 struct nfsd_net
*nn
= net_generic(clp
->net
, nfsd_net_id
);
3105 * We can't do this in nfsd_break_deleg_cb because it is
3106 * already holding inode->i_lock
3108 spin_lock(&state_lock
);
3109 block_delegations(&dp
->dl_fh
);
3111 * If the dl_time != 0, then we know that it has already been
3112 * queued for a lease break. Don't queue it again.
3114 if (dp
->dl_time
== 0) {
3115 dp
->dl_time
= get_seconds();
3116 list_add_tail(&dp
->dl_recall_lru
, &nn
->del_recall_lru
);
3118 spin_unlock(&state_lock
);
3121 static void nfsd_break_one_deleg(struct nfs4_delegation
*dp
)
3124 * We're assuming the state code never drops its reference
3125 * without first removing the lease. Since we're in this lease
3126 * callback (and since the lease code is serialized by the kernel
3127 * lock) we know the server hasn't removed the lease yet, we know
3128 * it's safe to take a reference.
3130 atomic_inc(&dp
->dl_stid
.sc_count
);
3131 nfsd4_cb_recall(dp
);
3134 /* Called from break_lease() with i_lock held. */
3135 static void nfsd_break_deleg_cb(struct file_lock
*fl
)
3137 struct nfs4_file
*fp
= (struct nfs4_file
*)fl
->fl_owner
;
3138 struct nfs4_delegation
*dp
;
3141 WARN(1, "(%p)->fl_owner NULL\n", fl
);
3144 if (fp
->fi_had_conflict
) {
3145 WARN(1, "duplicate break on %p\n", fp
);
3149 * We don't want the locks code to timeout the lease for us;
3150 * we'll remove it ourself if a delegation isn't returned
3153 fl
->fl_break_time
= 0;
3155 spin_lock(&fp
->fi_lock
);
3156 fp
->fi_had_conflict
= true;
3158 * If there are no delegations on the list, then we can't count on this
3159 * lease ever being cleaned up. Set the fl_break_time to jiffies so that
3160 * time_out_leases will do it ASAP. The fact that fi_had_conflict is now
3161 * true should keep any new delegations from being hashed.
3163 if (list_empty(&fp
->fi_delegations
))
3164 fl
->fl_break_time
= jiffies
;
3166 list_for_each_entry(dp
, &fp
->fi_delegations
, dl_perfile
)
3167 nfsd_break_one_deleg(dp
);
3168 spin_unlock(&fp
->fi_lock
);
3172 int nfsd_change_deleg_cb(struct file_lock
**onlist
, int arg
)
3175 return lease_modify(onlist
, arg
);
3180 static const struct lock_manager_operations nfsd_lease_mng_ops
= {
3181 .lm_break
= nfsd_break_deleg_cb
,
3182 .lm_change
= nfsd_change_deleg_cb
,
3185 static __be32
nfsd4_check_seqid(struct nfsd4_compound_state
*cstate
, struct nfs4_stateowner
*so
, u32 seqid
)
3187 if (nfsd4_has_session(cstate
))
3189 if (seqid
== so
->so_seqid
- 1)
3190 return nfserr_replay_me
;
3191 if (seqid
== so
->so_seqid
)
3193 return nfserr_bad_seqid
;
3196 static __be32
lookup_clientid(clientid_t
*clid
,
3197 struct nfsd4_compound_state
*cstate
,
3198 struct nfsd_net
*nn
)
3200 struct nfs4_client
*found
;
3203 found
= cstate
->clp
;
3204 if (!same_clid(&found
->cl_clientid
, clid
))
3205 return nfserr_stale_clientid
;
3209 if (STALE_CLIENTID(clid
, nn
))
3210 return nfserr_stale_clientid
;
3213 * For v4.1+ we get the client in the SEQUENCE op. If we don't have one
3214 * cached already then we know this is for is for v4.0 and "sessions"
3217 WARN_ON_ONCE(cstate
->session
);
3218 found
= find_confirmed_client(clid
, false, nn
);
3220 return nfserr_expired
;
3222 /* Cache the nfs4_client in cstate! */
3223 cstate
->clp
= found
;
3224 atomic_inc(&found
->cl_refcount
);
3229 nfsd4_process_open1(struct nfsd4_compound_state
*cstate
,
3230 struct nfsd4_open
*open
, struct nfsd_net
*nn
)
3232 clientid_t
*clientid
= &open
->op_clientid
;
3233 struct nfs4_client
*clp
= NULL
;
3234 unsigned int strhashval
;
3235 struct nfs4_openowner
*oo
= NULL
;
3238 if (STALE_CLIENTID(&open
->op_clientid
, nn
))
3239 return nfserr_stale_clientid
;
3241 * In case we need it later, after we've already created the
3242 * file and don't want to risk a further failure:
3244 open
->op_file
= nfsd4_alloc_file();
3245 if (open
->op_file
== NULL
)
3246 return nfserr_jukebox
;
3248 status
= lookup_clientid(clientid
, cstate
, nn
);
3253 strhashval
= ownerstr_hashval(clientid
->cl_id
, &open
->op_owner
);
3254 oo
= find_openstateowner_str(strhashval
, open
, cstate
->minorversion
, nn
);
3255 open
->op_openowner
= oo
;
3259 if (!(oo
->oo_flags
& NFS4_OO_CONFIRMED
)) {
3260 /* Replace unconfirmed owners without checking for replay. */
3261 release_openowner(oo
);
3262 open
->op_openowner
= NULL
;
3265 status
= nfsd4_check_seqid(cstate
, &oo
->oo_owner
, open
->op_seqid
);
3270 oo
= alloc_init_open_stateowner(strhashval
, open
, cstate
);
3272 return nfserr_jukebox
;
3273 open
->op_openowner
= oo
;
3275 open
->op_stp
= nfs4_alloc_stateid(clp
);
3277 return nfserr_jukebox
;
3281 static inline __be32
3282 nfs4_check_delegmode(struct nfs4_delegation
*dp
, int flags
)
3284 if ((flags
& WR_STATE
) && (dp
->dl_type
== NFS4_OPEN_DELEGATE_READ
))
3285 return nfserr_openmode
;
3290 static int share_access_to_flags(u32 share_access
)
3292 return share_access
== NFS4_SHARE_ACCESS_READ
? RD_STATE
: WR_STATE
;
3295 static struct nfs4_delegation
*find_deleg_stateid(struct nfs4_client
*cl
, stateid_t
*s
)
3297 struct nfs4_stid
*ret
;
3299 ret
= find_stateid_by_type(cl
, s
, NFS4_DELEG_STID
);
3302 return delegstateid(ret
);
3305 static bool nfsd4_is_deleg_cur(struct nfsd4_open
*open
)
3307 return open
->op_claim_type
== NFS4_OPEN_CLAIM_DELEGATE_CUR
||
3308 open
->op_claim_type
== NFS4_OPEN_CLAIM_DELEG_CUR_FH
;
3312 nfs4_check_deleg(struct nfs4_client
*cl
, struct nfsd4_open
*open
,
3313 struct nfs4_delegation
**dp
)
3316 __be32 status
= nfserr_bad_stateid
;
3318 *dp
= find_deleg_stateid(cl
, &open
->op_delegate_stateid
);
3321 flags
= share_access_to_flags(open
->op_share_access
);
3322 status
= nfs4_check_delegmode(*dp
, flags
);
3326 if (!nfsd4_is_deleg_cur(open
))
3330 open
->op_openowner
->oo_flags
|= NFS4_OO_CONFIRMED
;
3334 static struct nfs4_ol_stateid
*
3335 nfsd4_find_existing_open(struct nfs4_file
*fp
, struct nfsd4_open
*open
)
3337 struct nfs4_ol_stateid
*local
, *ret
= NULL
;
3338 struct nfs4_openowner
*oo
= open
->op_openowner
;
3340 spin_lock(&fp
->fi_lock
);
3341 list_for_each_entry(local
, &fp
->fi_stateids
, st_perfile
) {
3342 /* ignore lock owners */
3343 if (local
->st_stateowner
->so_is_open_owner
== 0)
3345 if (local
->st_stateowner
== &oo
->oo_owner
) {
3350 spin_unlock(&fp
->fi_lock
);
3354 static inline int nfs4_access_to_access(u32 nfs4_access
)
3358 if (nfs4_access
& NFS4_SHARE_ACCESS_READ
)
3359 flags
|= NFSD_MAY_READ
;
3360 if (nfs4_access
& NFS4_SHARE_ACCESS_WRITE
)
3361 flags
|= NFSD_MAY_WRITE
;
3365 static inline __be32
3366 nfsd4_truncate(struct svc_rqst
*rqstp
, struct svc_fh
*fh
,
3367 struct nfsd4_open
*open
)
3369 struct iattr iattr
= {
3370 .ia_valid
= ATTR_SIZE
,
3373 if (!open
->op_truncate
)
3375 if (!(open
->op_share_access
& NFS4_SHARE_ACCESS_WRITE
))
3376 return nfserr_inval
;
3377 return nfsd_setattr(rqstp
, fh
, &iattr
, 0, (time_t)0);
3380 static __be32
nfs4_get_vfs_file(struct svc_rqst
*rqstp
, struct nfs4_file
*fp
,
3381 struct svc_fh
*cur_fh
, struct nfs4_ol_stateid
*stp
,
3382 struct nfsd4_open
*open
)
3384 struct file
*filp
= NULL
;
3386 int oflag
= nfs4_access_to_omode(open
->op_share_access
);
3387 int access
= nfs4_access_to_access(open
->op_share_access
);
3388 unsigned char old_access_bmap
, old_deny_bmap
;
3390 spin_lock(&fp
->fi_lock
);
3393 * Are we trying to set a deny mode that would conflict with
3396 status
= nfs4_file_check_deny(fp
, open
->op_share_deny
);
3397 if (status
!= nfs_ok
) {
3398 spin_unlock(&fp
->fi_lock
);
3402 /* set access to the file */
3403 status
= nfs4_file_get_access(fp
, open
->op_share_access
);
3404 if (status
!= nfs_ok
) {
3405 spin_unlock(&fp
->fi_lock
);
3409 /* Set access bits in stateid */
3410 old_access_bmap
= stp
->st_access_bmap
;
3411 set_access(open
->op_share_access
, stp
);
3413 /* Set new deny mask */
3414 old_deny_bmap
= stp
->st_deny_bmap
;
3415 set_deny(open
->op_share_deny
, stp
);
3416 fp
->fi_share_deny
|= (open
->op_share_deny
& NFS4_SHARE_DENY_BOTH
);
3418 if (!fp
->fi_fds
[oflag
]) {
3419 spin_unlock(&fp
->fi_lock
);
3420 status
= nfsd_open(rqstp
, cur_fh
, S_IFREG
, access
, &filp
);
3422 goto out_put_access
;
3423 spin_lock(&fp
->fi_lock
);
3424 if (!fp
->fi_fds
[oflag
]) {
3425 fp
->fi_fds
[oflag
] = filp
;
3429 spin_unlock(&fp
->fi_lock
);
3433 status
= nfsd4_truncate(rqstp
, cur_fh
, open
);
3435 goto out_put_access
;
3439 stp
->st_access_bmap
= old_access_bmap
;
3440 nfs4_file_put_access(fp
, open
->op_share_access
);
3441 reset_union_bmap_deny(bmap_to_share_mode(old_deny_bmap
), stp
);
3446 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
)
3449 unsigned char old_deny_bmap
;
3451 if (!test_access(open
->op_share_access
, stp
))
3452 return nfs4_get_vfs_file(rqstp
, fp
, cur_fh
, stp
, open
);
3454 /* test and set deny mode */
3455 spin_lock(&fp
->fi_lock
);
3456 status
= nfs4_file_check_deny(fp
, open
->op_share_deny
);
3457 if (status
== nfs_ok
) {
3458 old_deny_bmap
= stp
->st_deny_bmap
;
3459 set_deny(open
->op_share_deny
, stp
);
3460 fp
->fi_share_deny
|=
3461 (open
->op_share_deny
& NFS4_SHARE_DENY_BOTH
);
3463 spin_unlock(&fp
->fi_lock
);
3465 if (status
!= nfs_ok
)
3468 status
= nfsd4_truncate(rqstp
, cur_fh
, open
);
3469 if (status
!= nfs_ok
)
3470 reset_union_bmap_deny(old_deny_bmap
, stp
);
3475 nfs4_set_claim_prev(struct nfsd4_open
*open
, bool has_session
)
3477 open
->op_openowner
->oo_flags
|= NFS4_OO_CONFIRMED
;
3480 /* Should we give out recallable state?: */
3481 static bool nfsd4_cb_channel_good(struct nfs4_client
*clp
)
3483 if (clp
->cl_cb_state
== NFSD4_CB_UP
)
3486 * In the sessions case, since we don't have to establish a
3487 * separate connection for callbacks, we assume it's OK
3488 * until we hear otherwise:
3490 return clp
->cl_minorversion
&& clp
->cl_cb_state
== NFSD4_CB_UNKNOWN
;
3493 static struct file_lock
*nfs4_alloc_init_lease(struct nfs4_file
*fp
, int flag
)
3495 struct file_lock
*fl
;
3497 fl
= locks_alloc_lock();
3500 locks_init_lock(fl
);
3501 fl
->fl_lmops
= &nfsd_lease_mng_ops
;
3502 fl
->fl_flags
= FL_DELEG
;
3503 fl
->fl_type
= flag
== NFS4_OPEN_DELEGATE_READ
? F_RDLCK
: F_WRLCK
;
3504 fl
->fl_end
= OFFSET_MAX
;
3505 fl
->fl_owner
= (fl_owner_t
)fp
;
3506 fl
->fl_pid
= current
->tgid
;
3510 static int nfs4_setlease(struct nfs4_delegation
*dp
)
3512 struct nfs4_file
*fp
= dp
->dl_file
;
3513 struct file_lock
*fl
;
3517 fl
= nfs4_alloc_init_lease(fp
, NFS4_OPEN_DELEGATE_READ
);
3520 filp
= find_readable_file(fp
);
3522 /* We should always have a readable file here */
3527 status
= vfs_setlease(filp
, fl
->fl_type
, &fl
);
3529 locks_free_lock(fl
);
3532 spin_lock(&state_lock
);
3533 spin_lock(&fp
->fi_lock
);
3534 /* Did the lease get broken before we took the lock? */
3536 if (fp
->fi_had_conflict
)
3541 atomic_inc(&fp
->fi_delegees
);
3542 hash_delegation_locked(dp
, fp
);
3546 fp
->fi_deleg_file
= filp
;
3547 atomic_set(&fp
->fi_delegees
, 1);
3548 hash_delegation_locked(dp
, fp
);
3549 spin_unlock(&fp
->fi_lock
);
3550 spin_unlock(&state_lock
);
3553 spin_unlock(&fp
->fi_lock
);
3554 spin_unlock(&state_lock
);
3560 static int nfs4_set_delegation(struct nfs4_delegation
*dp
, struct nfs4_file
*fp
)
3564 if (fp
->fi_had_conflict
)
3567 spin_lock(&state_lock
);
3568 spin_lock(&fp
->fi_lock
);
3570 if (!fp
->fi_lease
) {
3571 spin_unlock(&fp
->fi_lock
);
3572 spin_unlock(&state_lock
);
3573 return nfs4_setlease(dp
);
3575 atomic_inc(&fp
->fi_delegees
);
3576 if (fp
->fi_had_conflict
) {
3580 hash_delegation_locked(dp
, fp
);
3582 spin_unlock(&fp
->fi_lock
);
3583 spin_unlock(&state_lock
);
3587 static void nfsd4_open_deleg_none_ext(struct nfsd4_open
*open
, int status
)
3589 open
->op_delegate_type
= NFS4_OPEN_DELEGATE_NONE_EXT
;
3590 if (status
== -EAGAIN
)
3591 open
->op_why_no_deleg
= WND4_CONTENTION
;
3593 open
->op_why_no_deleg
= WND4_RESOURCE
;
3594 switch (open
->op_deleg_want
) {
3595 case NFS4_SHARE_WANT_READ_DELEG
:
3596 case NFS4_SHARE_WANT_WRITE_DELEG
:
3597 case NFS4_SHARE_WANT_ANY_DELEG
:
3599 case NFS4_SHARE_WANT_CANCEL
:
3600 open
->op_why_no_deleg
= WND4_CANCELLED
;
3602 case NFS4_SHARE_WANT_NO_DELEG
:
3609 * Attempt to hand out a delegation.
3611 * Note we don't support write delegations, and won't until the vfs has
3612 * proper support for them.
3615 nfs4_open_delegation(struct net
*net
, struct svc_fh
*fh
,
3616 struct nfsd4_open
*open
, struct nfs4_ol_stateid
*stp
)
3618 struct nfs4_delegation
*dp
;
3619 struct nfs4_openowner
*oo
= container_of(stp
->st_stateowner
, struct nfs4_openowner
, oo_owner
);
3623 cb_up
= nfsd4_cb_channel_good(oo
->oo_owner
.so_client
);
3624 open
->op_recall
= 0;
3625 switch (open
->op_claim_type
) {
3626 case NFS4_OPEN_CLAIM_PREVIOUS
:
3628 open
->op_recall
= 1;
3629 if (open
->op_delegate_type
!= NFS4_OPEN_DELEGATE_READ
)
3632 case NFS4_OPEN_CLAIM_NULL
:
3633 case NFS4_OPEN_CLAIM_FH
:
3635 * Let's not give out any delegations till everyone's
3636 * had the chance to reclaim theirs....
3638 if (locks_in_grace(net
))
3640 if (!cb_up
|| !(oo
->oo_flags
& NFS4_OO_CONFIRMED
))
3643 * Also, if the file was opened for write or
3644 * create, there's a good chance the client's
3645 * about to write to it, resulting in an
3646 * immediate recall (since we don't support
3647 * write delegations):
3649 if (open
->op_share_access
& NFS4_SHARE_ACCESS_WRITE
)
3651 if (open
->op_create
== NFS4_OPEN_CREATE
)
3657 dp
= alloc_init_deleg(oo
->oo_owner
.so_client
, stp
, fh
);
3660 status
= nfs4_set_delegation(dp
, stp
->st_file
);
3664 memcpy(&open
->op_delegate_stateid
, &dp
->dl_stid
.sc_stateid
, sizeof(dp
->dl_stid
.sc_stateid
));
3666 dprintk("NFSD: delegation stateid=" STATEID_FMT
"\n",
3667 STATEID_VAL(&dp
->dl_stid
.sc_stateid
));
3668 open
->op_delegate_type
= NFS4_OPEN_DELEGATE_READ
;
3671 destroy_delegation(dp
);
3673 open
->op_delegate_type
= NFS4_OPEN_DELEGATE_NONE
;
3674 if (open
->op_claim_type
== NFS4_OPEN_CLAIM_PREVIOUS
&&
3675 open
->op_delegate_type
!= NFS4_OPEN_DELEGATE_NONE
) {
3676 dprintk("NFSD: WARNING: refusing delegation reclaim\n");
3677 open
->op_recall
= 1;
3680 /* 4.1 client asking for a delegation? */
3681 if (open
->op_deleg_want
)
3682 nfsd4_open_deleg_none_ext(open
, status
);
3686 static void nfsd4_deleg_xgrade_none_ext(struct nfsd4_open
*open
,
3687 struct nfs4_delegation
*dp
)
3689 if (open
->op_deleg_want
== NFS4_SHARE_WANT_READ_DELEG
&&
3690 dp
->dl_type
== NFS4_OPEN_DELEGATE_WRITE
) {
3691 open
->op_delegate_type
= NFS4_OPEN_DELEGATE_NONE_EXT
;
3692 open
->op_why_no_deleg
= WND4_NOT_SUPP_DOWNGRADE
;
3693 } else if (open
->op_deleg_want
== NFS4_SHARE_WANT_WRITE_DELEG
&&
3694 dp
->dl_type
== NFS4_OPEN_DELEGATE_WRITE
) {
3695 open
->op_delegate_type
= NFS4_OPEN_DELEGATE_NONE_EXT
;
3696 open
->op_why_no_deleg
= WND4_NOT_SUPP_UPGRADE
;
3698 /* Otherwise the client must be confused wanting a delegation
3699 * it already has, therefore we don't return
3700 * NFS4_OPEN_DELEGATE_NONE_EXT and reason.
3705 * called with nfs4_lock_state() held.
3708 nfsd4_process_open2(struct svc_rqst
*rqstp
, struct svc_fh
*current_fh
, struct nfsd4_open
*open
)
3710 struct nfsd4_compoundres
*resp
= rqstp
->rq_resp
;
3711 struct nfs4_client
*cl
= open
->op_openowner
->oo_owner
.so_client
;
3712 struct nfs4_file
*fp
= NULL
;
3713 struct nfs4_ol_stateid
*stp
= NULL
;
3714 struct nfs4_delegation
*dp
= NULL
;
3718 * Lookup file; if found, lookup stateid and check open request,
3719 * and check for delegations in the process of being recalled.
3720 * If not found, create the nfs4_file struct
3722 fp
= find_or_add_file(open
->op_file
, ¤t_fh
->fh_handle
);
3723 if (fp
!= open
->op_file
) {
3724 status
= nfs4_check_deleg(cl
, open
, &dp
);
3727 stp
= nfsd4_find_existing_open(fp
, open
);
3729 open
->op_file
= NULL
;
3730 status
= nfserr_bad_stateid
;
3731 if (nfsd4_is_deleg_cur(open
))
3733 status
= nfserr_jukebox
;
3737 * OPEN the file, or upgrade an existing OPEN.
3738 * If truncate fails, the OPEN fails.
3741 /* Stateid was found, this is an OPEN upgrade */
3742 status
= nfs4_upgrade_open(rqstp
, fp
, current_fh
, stp
, open
);
3747 open
->op_stp
= NULL
;
3748 init_open_stateid(stp
, fp
, open
);
3749 status
= nfs4_get_vfs_file(rqstp
, fp
, current_fh
, stp
, open
);
3751 release_open_stateid(stp
);
3755 update_stateid(&stp
->st_stid
.sc_stateid
);
3756 memcpy(&open
->op_stateid
, &stp
->st_stid
.sc_stateid
, sizeof(stateid_t
));
3758 if (nfsd4_has_session(&resp
->cstate
)) {
3759 if (open
->op_deleg_want
& NFS4_SHARE_WANT_NO_DELEG
) {
3760 open
->op_delegate_type
= NFS4_OPEN_DELEGATE_NONE_EXT
;
3761 open
->op_why_no_deleg
= WND4_NOT_WANTED
;
3767 * Attempt to hand out a delegation. No error return, because the
3768 * OPEN succeeds even if we fail.
3770 nfs4_open_delegation(SVC_NET(rqstp
), current_fh
, open
, stp
);
3774 dprintk("%s: stateid=" STATEID_FMT
"\n", __func__
,
3775 STATEID_VAL(&stp
->st_stid
.sc_stateid
));
3777 /* 4.1 client trying to upgrade/downgrade delegation? */
3778 if (open
->op_delegate_type
== NFS4_OPEN_DELEGATE_NONE
&& dp
&&
3779 open
->op_deleg_want
)
3780 nfsd4_deleg_xgrade_none_ext(open
, dp
);
3784 if (status
== 0 && open
->op_claim_type
== NFS4_OPEN_CLAIM_PREVIOUS
)
3785 nfs4_set_claim_prev(open
, nfsd4_has_session(&resp
->cstate
));
3787 * To finish the open response, we just need to set the rflags.
3789 open
->op_rflags
= NFS4_OPEN_RESULT_LOCKTYPE_POSIX
;
3790 if (!(open
->op_openowner
->oo_flags
& NFS4_OO_CONFIRMED
) &&
3791 !nfsd4_has_session(&resp
->cstate
))
3792 open
->op_rflags
|= NFS4_OPEN_RESULT_CONFIRM
;
3797 void nfsd4_cleanup_open_state(struct nfsd4_open
*open
, __be32 status
)
3799 if (open
->op_openowner
) {
3800 struct nfs4_openowner
*oo
= open
->op_openowner
;
3802 if (!list_empty(&oo
->oo_owner
.so_stateids
))
3803 list_del_init(&oo
->oo_close_lru
);
3804 if (oo
->oo_flags
& NFS4_OO_NEW
) {
3806 release_openowner(oo
);
3807 open
->op_openowner
= NULL
;
3809 oo
->oo_flags
&= ~NFS4_OO_NEW
;
3813 nfsd4_free_file(open
->op_file
);
3815 free_generic_stateid(open
->op_stp
);
3819 nfsd4_renew(struct svc_rqst
*rqstp
, struct nfsd4_compound_state
*cstate
,
3822 struct nfs4_client
*clp
;
3824 struct nfsd_net
*nn
= net_generic(SVC_NET(rqstp
), nfsd_net_id
);
3827 dprintk("process_renew(%08x/%08x): starting\n",
3828 clid
->cl_boot
, clid
->cl_id
);
3829 status
= lookup_clientid(clid
, cstate
, nn
);
3833 status
= nfserr_cb_path_down
;
3834 if (!list_empty(&clp
->cl_delegations
)
3835 && clp
->cl_cb_state
!= NFSD4_CB_UP
)
3839 nfs4_unlock_state();
3844 nfsd4_end_grace(struct nfsd_net
*nn
)
3846 /* do nothing if grace period already ended */
3847 if (nn
->grace_ended
)
3850 dprintk("NFSD: end of grace period\n");
3851 nn
->grace_ended
= true;
3852 nfsd4_record_grace_done(nn
, nn
->boot_time
);
3853 locks_end_grace(&nn
->nfsd4_manager
);
3855 * Now that every NFSv4 client has had the chance to recover and
3856 * to see the (possibly new, possibly shorter) lease time, we
3857 * can safely set the next grace time to the current lease time:
3859 nn
->nfsd4_grace
= nn
->nfsd4_lease
;
3863 nfs4_laundromat(struct nfsd_net
*nn
)
3865 struct nfs4_client
*clp
;
3866 struct nfs4_openowner
*oo
;
3867 struct nfs4_delegation
*dp
;
3868 struct list_head
*pos
, *next
, reaplist
;
3869 time_t cutoff
= get_seconds() - nn
->nfsd4_lease
;
3870 time_t t
, new_timeo
= nn
->nfsd4_lease
;
3874 dprintk("NFSD: laundromat service - starting\n");
3875 nfsd4_end_grace(nn
);
3876 INIT_LIST_HEAD(&reaplist
);
3877 spin_lock(&nn
->client_lock
);
3878 list_for_each_safe(pos
, next
, &nn
->client_lru
) {
3879 clp
= list_entry(pos
, struct nfs4_client
, cl_lru
);
3880 if (time_after((unsigned long)clp
->cl_time
, (unsigned long)cutoff
)) {
3881 t
= clp
->cl_time
- cutoff
;
3882 new_timeo
= min(new_timeo
, t
);
3885 if (mark_client_expired_locked(clp
)) {
3886 dprintk("NFSD: client in use (clientid %08x)\n",
3887 clp
->cl_clientid
.cl_id
);
3890 list_move(&clp
->cl_lru
, &reaplist
);
3892 spin_unlock(&nn
->client_lock
);
3893 list_for_each_safe(pos
, next
, &reaplist
) {
3894 clp
= list_entry(pos
, struct nfs4_client
, cl_lru
);
3895 dprintk("NFSD: purging unused client (clientid %08x)\n",
3896 clp
->cl_clientid
.cl_id
);
3899 spin_lock(&state_lock
);
3900 list_for_each_safe(pos
, next
, &nn
->del_recall_lru
) {
3901 dp
= list_entry (pos
, struct nfs4_delegation
, dl_recall_lru
);
3902 if (net_generic(dp
->dl_stid
.sc_client
->net
, nfsd_net_id
) != nn
)
3904 if (time_after((unsigned long)dp
->dl_time
, (unsigned long)cutoff
)) {
3905 t
= dp
->dl_time
- cutoff
;
3906 new_timeo
= min(new_timeo
, t
);
3909 list_move(&dp
->dl_recall_lru
, &reaplist
);
3911 spin_unlock(&state_lock
);
3912 list_for_each_safe(pos
, next
, &reaplist
) {
3913 dp
= list_entry (pos
, struct nfs4_delegation
, dl_recall_lru
);
3914 revoke_delegation(dp
);
3916 list_for_each_safe(pos
, next
, &nn
->close_lru
) {
3917 oo
= container_of(pos
, struct nfs4_openowner
, oo_close_lru
);
3918 if (time_after((unsigned long)oo
->oo_time
, (unsigned long)cutoff
)) {
3919 t
= oo
->oo_time
- cutoff
;
3920 new_timeo
= min(new_timeo
, t
);
3923 release_openowner(oo
);
3925 new_timeo
= max_t(time_t, new_timeo
, NFSD_LAUNDROMAT_MINTIMEOUT
);
3926 nfs4_unlock_state();
3930 static struct workqueue_struct
*laundry_wq
;
3931 static void laundromat_main(struct work_struct
*);
3934 laundromat_main(struct work_struct
*laundry
)
3937 struct delayed_work
*dwork
= container_of(laundry
, struct delayed_work
,
3939 struct nfsd_net
*nn
= container_of(dwork
, struct nfsd_net
,
3942 t
= nfs4_laundromat(nn
);
3943 dprintk("NFSD: laundromat_main - sleeping for %ld seconds\n", t
);
3944 queue_delayed_work(laundry_wq
, &nn
->laundromat_work
, t
*HZ
);
3947 static inline __be32
nfs4_check_fh(struct svc_fh
*fhp
, struct nfs4_ol_stateid
*stp
)
3949 if (!nfsd_fh_match(&fhp
->fh_handle
, &stp
->st_file
->fi_fhandle
))
3950 return nfserr_bad_stateid
;
3955 access_permit_read(struct nfs4_ol_stateid
*stp
)
3957 return test_access(NFS4_SHARE_ACCESS_READ
, stp
) ||
3958 test_access(NFS4_SHARE_ACCESS_BOTH
, stp
) ||
3959 test_access(NFS4_SHARE_ACCESS_WRITE
, stp
);
3963 access_permit_write(struct nfs4_ol_stateid
*stp
)
3965 return test_access(NFS4_SHARE_ACCESS_WRITE
, stp
) ||
3966 test_access(NFS4_SHARE_ACCESS_BOTH
, stp
);
3970 __be32
nfs4_check_openmode(struct nfs4_ol_stateid
*stp
, int flags
)
3972 __be32 status
= nfserr_openmode
;
3974 /* For lock stateid's, we test the parent open, not the lock: */
3975 if (stp
->st_openstp
)
3976 stp
= stp
->st_openstp
;
3977 if ((flags
& WR_STATE
) && !access_permit_write(stp
))
3979 if ((flags
& RD_STATE
) && !access_permit_read(stp
))
3986 static inline __be32
3987 check_special_stateids(struct net
*net
, svc_fh
*current_fh
, stateid_t
*stateid
, int flags
)
3989 if (ONE_STATEID(stateid
) && (flags
& RD_STATE
))
3991 else if (locks_in_grace(net
)) {
3992 /* Answer in remaining cases depends on existence of
3993 * conflicting state; so we must wait out the grace period. */
3994 return nfserr_grace
;
3995 } else if (flags
& WR_STATE
)
3996 return nfs4_share_conflict(current_fh
,
3997 NFS4_SHARE_DENY_WRITE
);
3998 else /* (flags & RD_STATE) && ZERO_STATEID(stateid) */
3999 return nfs4_share_conflict(current_fh
,
4000 NFS4_SHARE_DENY_READ
);
4004 * Allow READ/WRITE during grace period on recovered state only for files
4005 * that are not able to provide mandatory locking.
4008 grace_disallows_io(struct net
*net
, struct inode
*inode
)
4010 return locks_in_grace(net
) && mandatory_lock(inode
);
4013 /* Returns true iff a is later than b: */
4014 static bool stateid_generation_after(stateid_t
*a
, stateid_t
*b
)
4016 return (s32
)(a
->si_generation
- b
->si_generation
) > 0;
4019 static __be32
check_stateid_generation(stateid_t
*in
, stateid_t
*ref
, bool has_session
)
4022 * When sessions are used the stateid generation number is ignored
4025 if (has_session
&& in
->si_generation
== 0)
4028 if (in
->si_generation
== ref
->si_generation
)
4031 /* If the client sends us a stateid from the future, it's buggy: */
4032 if (stateid_generation_after(in
, ref
))
4033 return nfserr_bad_stateid
;
4035 * However, we could see a stateid from the past, even from a
4036 * non-buggy client. For example, if the client sends a lock
4037 * while some IO is outstanding, the lock may bump si_generation
4038 * while the IO is still in flight. The client could avoid that
4039 * situation by waiting for responses on all the IO requests,
4040 * but better performance may result in retrying IO that
4041 * receives an old_stateid error if requests are rarely
4042 * reordered in flight:
4044 return nfserr_old_stateid
;
4047 static __be32
nfsd4_validate_stateid(struct nfs4_client
*cl
, stateid_t
*stateid
)
4049 struct nfs4_stid
*s
;
4050 struct nfs4_ol_stateid
*ols
;
4053 if (ZERO_STATEID(stateid
) || ONE_STATEID(stateid
))
4054 return nfserr_bad_stateid
;
4055 /* Client debugging aid. */
4056 if (!same_clid(&stateid
->si_opaque
.so_clid
, &cl
->cl_clientid
)) {
4057 char addr_str
[INET6_ADDRSTRLEN
];
4058 rpc_ntop((struct sockaddr
*)&cl
->cl_addr
, addr_str
,
4060 pr_warn_ratelimited("NFSD: client %s testing state ID "
4061 "with incorrect client ID\n", addr_str
);
4062 return nfserr_bad_stateid
;
4064 s
= find_stateid(cl
, stateid
);
4066 return nfserr_bad_stateid
;
4067 status
= check_stateid_generation(stateid
, &s
->sc_stateid
, 1);
4070 switch (s
->sc_type
) {
4071 case NFS4_DELEG_STID
:
4073 case NFS4_REVOKED_DELEG_STID
:
4074 return nfserr_deleg_revoked
;
4075 case NFS4_OPEN_STID
:
4076 case NFS4_LOCK_STID
:
4077 ols
= openlockstateid(s
);
4078 if (ols
->st_stateowner
->so_is_open_owner
4079 && !(openowner(ols
->st_stateowner
)->oo_flags
4080 & NFS4_OO_CONFIRMED
))
4081 return nfserr_bad_stateid
;
4084 printk("unknown stateid type %x\n", s
->sc_type
);
4086 case NFS4_CLOSED_STID
:
4087 case NFS4_CLOSED_DELEG_STID
:
4088 return nfserr_bad_stateid
;
4093 nfsd4_lookup_stateid(struct nfsd4_compound_state
*cstate
,
4094 stateid_t
*stateid
, unsigned char typemask
,
4095 struct nfs4_stid
**s
, struct nfsd_net
*nn
)
4099 if (ZERO_STATEID(stateid
) || ONE_STATEID(stateid
))
4100 return nfserr_bad_stateid
;
4101 status
= lookup_clientid(&stateid
->si_opaque
.so_clid
, cstate
, nn
);
4102 if (status
== nfserr_stale_clientid
) {
4103 if (cstate
->session
)
4104 return nfserr_bad_stateid
;
4105 return nfserr_stale_stateid
;
4109 *s
= find_stateid_by_type(cstate
->clp
, stateid
, typemask
);
4111 return nfserr_bad_stateid
;
4116 * Checks for stateid operations
4119 nfs4_preprocess_stateid_op(struct net
*net
, struct nfsd4_compound_state
*cstate
,
4120 stateid_t
*stateid
, int flags
, struct file
**filpp
)
4122 struct nfs4_stid
*s
;
4123 struct nfs4_ol_stateid
*stp
= NULL
;
4124 struct nfs4_delegation
*dp
= NULL
;
4125 struct svc_fh
*current_fh
= &cstate
->current_fh
;
4126 struct inode
*ino
= current_fh
->fh_dentry
->d_inode
;
4127 struct nfsd_net
*nn
= net_generic(net
, nfsd_net_id
);
4128 struct file
*file
= NULL
;
4134 if (grace_disallows_io(net
, ino
))
4135 return nfserr_grace
;
4137 if (ZERO_STATEID(stateid
) || ONE_STATEID(stateid
))
4138 return check_special_stateids(net
, current_fh
, stateid
, flags
);
4142 status
= nfsd4_lookup_stateid(cstate
, stateid
,
4143 NFS4_DELEG_STID
|NFS4_OPEN_STID
|NFS4_LOCK_STID
,
4147 status
= check_stateid_generation(stateid
, &s
->sc_stateid
, nfsd4_has_session(cstate
));
4150 switch (s
->sc_type
) {
4151 case NFS4_DELEG_STID
:
4152 dp
= delegstateid(s
);
4153 status
= nfs4_check_delegmode(dp
, flags
);
4157 file
= dp
->dl_file
->fi_deleg_file
;
4160 status
= nfserr_serverfault
;
4166 case NFS4_OPEN_STID
:
4167 case NFS4_LOCK_STID
:
4168 stp
= openlockstateid(s
);
4169 status
= nfs4_check_fh(current_fh
, stp
);
4172 if (stp
->st_stateowner
->so_is_open_owner
4173 && !(openowner(stp
->st_stateowner
)->oo_flags
& NFS4_OO_CONFIRMED
))
4175 status
= nfs4_check_openmode(stp
, flags
);
4179 if (flags
& RD_STATE
)
4180 file
= find_readable_file(stp
->st_file
);
4182 file
= find_writeable_file(stp
->st_file
);
4186 status
= nfserr_bad_stateid
;
4193 nfs4_unlock_state();
4198 nfsd4_free_lock_stateid(struct nfs4_ol_stateid
*stp
)
4200 struct nfs4_lockowner
*lo
= lockowner(stp
->st_stateowner
);
4202 if (check_for_locks(stp
->st_file
, lo
))
4203 return nfserr_locks_held
;
4204 release_lockowner_if_empty(lo
);
4209 * Test if the stateid is valid
4212 nfsd4_test_stateid(struct svc_rqst
*rqstp
, struct nfsd4_compound_state
*cstate
,
4213 struct nfsd4_test_stateid
*test_stateid
)
4215 struct nfsd4_test_stateid_id
*stateid
;
4216 struct nfs4_client
*cl
= cstate
->session
->se_client
;
4219 list_for_each_entry(stateid
, &test_stateid
->ts_stateid_list
, ts_id_list
)
4220 stateid
->ts_id_status
=
4221 nfsd4_validate_stateid(cl
, &stateid
->ts_id_stateid
);
4222 nfs4_unlock_state();
4228 nfsd4_free_stateid(struct svc_rqst
*rqstp
, struct nfsd4_compound_state
*cstate
,
4229 struct nfsd4_free_stateid
*free_stateid
)
4231 stateid_t
*stateid
= &free_stateid
->fr_stateid
;
4232 struct nfs4_stid
*s
;
4233 struct nfs4_delegation
*dp
;
4234 struct nfs4_client
*cl
= cstate
->session
->se_client
;
4235 __be32 ret
= nfserr_bad_stateid
;
4238 s
= find_stateid(cl
, stateid
);
4241 switch (s
->sc_type
) {
4242 case NFS4_DELEG_STID
:
4243 ret
= nfserr_locks_held
;
4245 case NFS4_OPEN_STID
:
4246 case NFS4_LOCK_STID
:
4247 ret
= check_stateid_generation(stateid
, &s
->sc_stateid
, 1);
4250 if (s
->sc_type
== NFS4_LOCK_STID
)
4251 ret
= nfsd4_free_lock_stateid(openlockstateid(s
));
4253 ret
= nfserr_locks_held
;
4255 case NFS4_REVOKED_DELEG_STID
:
4256 dp
= delegstateid(s
);
4257 destroy_revoked_delegation(dp
);
4261 ret
= nfserr_bad_stateid
;
4264 nfs4_unlock_state();
4271 return (type
== NFS4_READW_LT
|| type
== NFS4_READ_LT
) ?
4272 RD_STATE
: WR_STATE
;
4275 static __be32
nfs4_seqid_op_checks(struct nfsd4_compound_state
*cstate
, stateid_t
*stateid
, u32 seqid
, struct nfs4_ol_stateid
*stp
)
4277 struct svc_fh
*current_fh
= &cstate
->current_fh
;
4278 struct nfs4_stateowner
*sop
= stp
->st_stateowner
;
4281 status
= nfsd4_check_seqid(cstate
, sop
, seqid
);
4284 if (stp
->st_stid
.sc_type
== NFS4_CLOSED_STID
4285 || stp
->st_stid
.sc_type
== NFS4_REVOKED_DELEG_STID
)
4287 * "Closed" stateid's exist *only* to return
4288 * nfserr_replay_me from the previous step, and
4289 * revoked delegations are kept only for free_stateid.
4291 return nfserr_bad_stateid
;
4292 status
= check_stateid_generation(stateid
, &stp
->st_stid
.sc_stateid
, nfsd4_has_session(cstate
));
4295 return nfs4_check_fh(current_fh
, stp
);
4299 * Checks for sequence id mutating operations.
4302 nfs4_preprocess_seqid_op(struct nfsd4_compound_state
*cstate
, u32 seqid
,
4303 stateid_t
*stateid
, char typemask
,
4304 struct nfs4_ol_stateid
**stpp
,
4305 struct nfsd_net
*nn
)
4308 struct nfs4_stid
*s
;
4309 struct nfs4_ol_stateid
*stp
= NULL
;
4311 dprintk("NFSD: %s: seqid=%d stateid = " STATEID_FMT
"\n", __func__
,
4312 seqid
, STATEID_VAL(stateid
));
4315 status
= nfsd4_lookup_stateid(cstate
, stateid
, typemask
, &s
, nn
);
4318 stp
= openlockstateid(s
);
4319 if (!nfsd4_has_session(cstate
))
4320 cstate
->replay_owner
= stp
->st_stateowner
;
4322 status
= nfs4_seqid_op_checks(cstate
, stateid
, seqid
, stp
);
4328 static __be32
nfs4_preprocess_confirmed_seqid_op(struct nfsd4_compound_state
*cstate
, u32 seqid
,
4329 stateid_t
*stateid
, struct nfs4_ol_stateid
**stpp
, struct nfsd_net
*nn
)
4332 struct nfs4_openowner
*oo
;
4334 status
= nfs4_preprocess_seqid_op(cstate
, seqid
, stateid
,
4335 NFS4_OPEN_STID
, stpp
, nn
);
4338 oo
= openowner((*stpp
)->st_stateowner
);
4339 if (!(oo
->oo_flags
& NFS4_OO_CONFIRMED
))
4340 return nfserr_bad_stateid
;
4345 nfsd4_open_confirm(struct svc_rqst
*rqstp
, struct nfsd4_compound_state
*cstate
,
4346 struct nfsd4_open_confirm
*oc
)
4349 struct nfs4_openowner
*oo
;
4350 struct nfs4_ol_stateid
*stp
;
4351 struct nfsd_net
*nn
= net_generic(SVC_NET(rqstp
), nfsd_net_id
);
4353 dprintk("NFSD: nfsd4_open_confirm on file %pd\n",
4354 cstate
->current_fh
.fh_dentry
);
4356 status
= fh_verify(rqstp
, &cstate
->current_fh
, S_IFREG
, 0);
4362 status
= nfs4_preprocess_seqid_op(cstate
,
4363 oc
->oc_seqid
, &oc
->oc_req_stateid
,
4364 NFS4_OPEN_STID
, &stp
, nn
);
4367 oo
= openowner(stp
->st_stateowner
);
4368 status
= nfserr_bad_stateid
;
4369 if (oo
->oo_flags
& NFS4_OO_CONFIRMED
)
4371 oo
->oo_flags
|= NFS4_OO_CONFIRMED
;
4372 update_stateid(&stp
->st_stid
.sc_stateid
);
4373 memcpy(&oc
->oc_resp_stateid
, &stp
->st_stid
.sc_stateid
, sizeof(stateid_t
));
4374 dprintk("NFSD: %s: success, seqid=%d stateid=" STATEID_FMT
"\n",
4375 __func__
, oc
->oc_seqid
, STATEID_VAL(&stp
->st_stid
.sc_stateid
));
4377 nfsd4_client_record_create(oo
->oo_owner
.so_client
);
4380 nfsd4_bump_seqid(cstate
, status
);
4381 if (!cstate
->replay_owner
)
4382 nfs4_unlock_state();
4386 static inline void nfs4_stateid_downgrade_bit(struct nfs4_ol_stateid
*stp
, u32 access
)
4388 if (!test_access(access
, stp
))
4390 nfs4_file_put_access(stp
->st_file
, access
);
4391 clear_access(access
, stp
);
4394 static inline void nfs4_stateid_downgrade(struct nfs4_ol_stateid
*stp
, u32 to_access
)
4396 switch (to_access
) {
4397 case NFS4_SHARE_ACCESS_READ
:
4398 nfs4_stateid_downgrade_bit(stp
, NFS4_SHARE_ACCESS_WRITE
);
4399 nfs4_stateid_downgrade_bit(stp
, NFS4_SHARE_ACCESS_BOTH
);
4401 case NFS4_SHARE_ACCESS_WRITE
:
4402 nfs4_stateid_downgrade_bit(stp
, NFS4_SHARE_ACCESS_READ
);
4403 nfs4_stateid_downgrade_bit(stp
, NFS4_SHARE_ACCESS_BOTH
);
4405 case NFS4_SHARE_ACCESS_BOTH
:
4413 nfsd4_open_downgrade(struct svc_rqst
*rqstp
,
4414 struct nfsd4_compound_state
*cstate
,
4415 struct nfsd4_open_downgrade
*od
)
4418 struct nfs4_ol_stateid
*stp
;
4419 struct nfsd_net
*nn
= net_generic(SVC_NET(rqstp
), nfsd_net_id
);
4421 dprintk("NFSD: nfsd4_open_downgrade on file %pd\n",
4422 cstate
->current_fh
.fh_dentry
);
4424 /* We don't yet support WANT bits: */
4425 if (od
->od_deleg_want
)
4426 dprintk("NFSD: %s: od_deleg_want=0x%x ignored\n", __func__
,
4430 status
= nfs4_preprocess_confirmed_seqid_op(cstate
, od
->od_seqid
,
4431 &od
->od_stateid
, &stp
, nn
);
4434 status
= nfserr_inval
;
4435 if (!test_access(od
->od_share_access
, stp
)) {
4436 dprintk("NFSD: access not a subset of current bitmap: 0x%hhx, input access=%08x\n",
4437 stp
->st_access_bmap
, od
->od_share_access
);
4440 if (!test_deny(od
->od_share_deny
, stp
)) {
4441 dprintk("NFSD: deny not a subset of current bitmap: 0x%hhx, input deny=%08x\n",
4442 stp
->st_deny_bmap
, od
->od_share_deny
);
4445 nfs4_stateid_downgrade(stp
, od
->od_share_access
);
4447 reset_union_bmap_deny(od
->od_share_deny
, stp
);
4449 update_stateid(&stp
->st_stid
.sc_stateid
);
4450 memcpy(&od
->od_stateid
, &stp
->st_stid
.sc_stateid
, sizeof(stateid_t
));
4453 nfsd4_bump_seqid(cstate
, status
);
4454 if (!cstate
->replay_owner
)
4455 nfs4_unlock_state();
4459 static void nfsd4_close_open_stateid(struct nfs4_ol_stateid
*s
)
4461 struct nfs4_client
*clp
= s
->st_stid
.sc_client
;
4462 struct nfs4_openowner
*oo
= openowner(s
->st_stateowner
);
4464 s
->st_stid
.sc_type
= NFS4_CLOSED_STID
;
4465 unhash_open_stateid(s
);
4467 if (clp
->cl_minorversion
) {
4468 free_generic_stateid(s
);
4469 if (list_empty(&oo
->oo_owner
.so_stateids
))
4470 release_openowner(oo
);
4472 oo
->oo_last_closed_stid
= s
;
4474 * In the 4.0 case we need to keep the owners around a
4475 * little while to handle CLOSE replay.
4477 if (list_empty(&oo
->oo_owner
.so_stateids
))
4478 move_to_close_lru(oo
, clp
->net
);
4483 * nfs4_unlock_state() called after encode
4486 nfsd4_close(struct svc_rqst
*rqstp
, struct nfsd4_compound_state
*cstate
,
4487 struct nfsd4_close
*close
)
4490 struct nfs4_ol_stateid
*stp
;
4491 struct net
*net
= SVC_NET(rqstp
);
4492 struct nfsd_net
*nn
= net_generic(net
, nfsd_net_id
);
4494 dprintk("NFSD: nfsd4_close on file %pd\n",
4495 cstate
->current_fh
.fh_dentry
);
4498 status
= nfs4_preprocess_seqid_op(cstate
, close
->cl_seqid
,
4500 NFS4_OPEN_STID
|NFS4_CLOSED_STID
,
4502 nfsd4_bump_seqid(cstate
, status
);
4505 update_stateid(&stp
->st_stid
.sc_stateid
);
4506 memcpy(&close
->cl_stateid
, &stp
->st_stid
.sc_stateid
, sizeof(stateid_t
));
4508 nfsd4_close_open_stateid(stp
);
4510 if (!cstate
->replay_owner
)
4511 nfs4_unlock_state();
4516 nfsd4_delegreturn(struct svc_rqst
*rqstp
, struct nfsd4_compound_state
*cstate
,
4517 struct nfsd4_delegreturn
*dr
)
4519 struct nfs4_delegation
*dp
;
4520 stateid_t
*stateid
= &dr
->dr_stateid
;
4521 struct nfs4_stid
*s
;
4523 struct nfsd_net
*nn
= net_generic(SVC_NET(rqstp
), nfsd_net_id
);
4525 if ((status
= fh_verify(rqstp
, &cstate
->current_fh
, S_IFREG
, 0)))
4529 status
= nfsd4_lookup_stateid(cstate
, stateid
, NFS4_DELEG_STID
, &s
, nn
);
4532 dp
= delegstateid(s
);
4533 status
= check_stateid_generation(stateid
, &dp
->dl_stid
.sc_stateid
, nfsd4_has_session(cstate
));
4537 destroy_delegation(dp
);
4539 nfs4_unlock_state();
4545 #define LOFF_OVERFLOW(start, len) ((u64)(len) > ~(u64)(start))
4548 end_offset(u64 start
, u64 len
)
4553 return end
>= start
? end
: NFS4_MAX_UINT64
;
4556 /* last octet in a range */
4558 last_byte_offset(u64 start
, u64 len
)
4564 return end
> start
? end
- 1: NFS4_MAX_UINT64
;
4568 * TODO: Linux file offsets are _signed_ 64-bit quantities, which means that
4569 * we can't properly handle lock requests that go beyond the (2^63 - 1)-th
4570 * byte, because of sign extension problems. Since NFSv4 calls for 64-bit
4571 * locking, this prevents us from being completely protocol-compliant. The
4572 * real solution to this problem is to start using unsigned file offsets in
4573 * the VFS, but this is a very deep change!
4576 nfs4_transform_lock_offset(struct file_lock
*lock
)
4578 if (lock
->fl_start
< 0)
4579 lock
->fl_start
= OFFSET_MAX
;
4580 if (lock
->fl_end
< 0)
4581 lock
->fl_end
= OFFSET_MAX
;
4584 /* Hack!: For now, we're defining this just so we can use a pointer to it
4585 * as a unique cookie to identify our (NFSv4's) posix locks. */
4586 static const struct lock_manager_operations nfsd_posix_mng_ops
= {
4590 nfs4_set_lock_denied(struct file_lock
*fl
, struct nfsd4_lock_denied
*deny
)
4592 struct nfs4_lockowner
*lo
;
4594 if (fl
->fl_lmops
== &nfsd_posix_mng_ops
) {
4595 lo
= (struct nfs4_lockowner
*) fl
->fl_owner
;
4596 deny
->ld_owner
.data
= kmemdup(lo
->lo_owner
.so_owner
.data
,
4597 lo
->lo_owner
.so_owner
.len
, GFP_KERNEL
);
4598 if (!deny
->ld_owner
.data
)
4599 /* We just don't care that much */
4601 deny
->ld_owner
.len
= lo
->lo_owner
.so_owner
.len
;
4602 deny
->ld_clientid
= lo
->lo_owner
.so_client
->cl_clientid
;
4605 deny
->ld_owner
.len
= 0;
4606 deny
->ld_owner
.data
= NULL
;
4607 deny
->ld_clientid
.cl_boot
= 0;
4608 deny
->ld_clientid
.cl_id
= 0;
4610 deny
->ld_start
= fl
->fl_start
;
4611 deny
->ld_length
= NFS4_MAX_UINT64
;
4612 if (fl
->fl_end
!= NFS4_MAX_UINT64
)
4613 deny
->ld_length
= fl
->fl_end
- fl
->fl_start
+ 1;
4614 deny
->ld_type
= NFS4_READ_LT
;
4615 if (fl
->fl_type
!= F_RDLCK
)
4616 deny
->ld_type
= NFS4_WRITE_LT
;
4619 static struct nfs4_lockowner
*
4620 find_lockowner_str(clientid_t
*clid
, struct xdr_netobj
*owner
,
4621 struct nfsd_net
*nn
)
4623 unsigned int strhashval
= ownerstr_hashval(clid
->cl_id
, owner
);
4624 struct nfs4_stateowner
*so
;
4626 list_for_each_entry(so
, &nn
->ownerstr_hashtbl
[strhashval
], so_strhash
) {
4627 if (so
->so_is_open_owner
)
4629 if (!same_owner_str(so
, owner
, clid
))
4631 return lockowner(so
);
4637 * Alloc a lock owner structure.
4638 * Called in nfsd4_lock - therefore, OPEN and OPEN_CONFIRM (if needed) has
4641 * strhashval = ownerstr_hashval
4643 static struct nfs4_lockowner
*
4644 alloc_init_lock_stateowner(unsigned int strhashval
, struct nfs4_client
*clp
, struct nfs4_ol_stateid
*open_stp
, struct nfsd4_lock
*lock
) {
4645 struct nfs4_lockowner
*lo
;
4646 struct nfsd_net
*nn
= net_generic(clp
->net
, nfsd_net_id
);
4648 lo
= alloc_stateowner(lockowner_slab
, &lock
->lk_new_owner
, clp
);
4651 INIT_LIST_HEAD(&lo
->lo_owner
.so_stateids
);
4652 lo
->lo_owner
.so_is_open_owner
= 0;
4653 /* It is the openowner seqid that will be incremented in encode in the
4654 * case of new lockowners; so increment the lock seqid manually: */
4655 lo
->lo_owner
.so_seqid
= lock
->lk_new_lock_seqid
+ 1;
4656 list_add(&lo
->lo_owner
.so_strhash
, &nn
->ownerstr_hashtbl
[strhashval
]);
4660 static struct nfs4_ol_stateid
*
4661 alloc_init_lock_stateid(struct nfs4_lockowner
*lo
, struct nfs4_file
*fp
,
4662 struct inode
*inode
,
4663 struct nfs4_ol_stateid
*open_stp
)
4665 struct nfs4_ol_stateid
*stp
;
4666 struct nfs4_client
*clp
= lo
->lo_owner
.so_client
;
4668 stp
= nfs4_alloc_stateid(clp
);
4671 stp
->st_stid
.sc_type
= NFS4_LOCK_STID
;
4672 list_add(&stp
->st_perstateowner
, &lo
->lo_owner
.so_stateids
);
4673 stp
->st_stateowner
= &lo
->lo_owner
;
4676 stp
->st_access_bmap
= 0;
4677 stp
->st_deny_bmap
= open_stp
->st_deny_bmap
;
4678 stp
->st_openstp
= open_stp
;
4679 list_add(&stp
->st_locks
, &open_stp
->st_locks
);
4680 spin_lock(&fp
->fi_lock
);
4681 list_add(&stp
->st_perfile
, &fp
->fi_stateids
);
4682 spin_unlock(&fp
->fi_lock
);
4686 static struct nfs4_ol_stateid
*
4687 find_lock_stateid(struct nfs4_lockowner
*lo
, struct nfs4_file
*fp
)
4689 struct nfs4_ol_stateid
*lst
;
4691 list_for_each_entry(lst
, &lo
->lo_owner
.so_stateids
, st_perstateowner
) {
4692 if (lst
->st_file
== fp
)
4700 check_lock_length(u64 offset
, u64 length
)
4702 return ((length
== 0) || ((length
!= NFS4_MAX_UINT64
) &&
4703 LOFF_OVERFLOW(offset
, length
)));
4706 static void get_lock_access(struct nfs4_ol_stateid
*lock_stp
, u32 access
)
4708 struct nfs4_file
*fp
= lock_stp
->st_file
;
4710 lockdep_assert_held(&fp
->fi_lock
);
4712 if (test_access(access
, lock_stp
))
4714 __nfs4_file_get_access(fp
, access
);
4715 set_access(access
, lock_stp
);
4718 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)
4720 struct nfs4_file
*fi
= ost
->st_file
;
4721 struct nfs4_openowner
*oo
= openowner(ost
->st_stateowner
);
4722 struct nfs4_client
*cl
= oo
->oo_owner
.so_client
;
4723 struct inode
*inode
= cstate
->current_fh
.fh_dentry
->d_inode
;
4724 struct nfs4_lockowner
*lo
;
4725 unsigned int strhashval
;
4726 struct nfsd_net
*nn
= net_generic(cl
->net
, nfsd_net_id
);
4728 lo
= find_lockowner_str(&cl
->cl_clientid
, &lock
->v
.new.owner
, nn
);
4730 strhashval
= ownerstr_hashval(cl
->cl_clientid
.cl_id
,
4731 &lock
->v
.new.owner
);
4732 lo
= alloc_init_lock_stateowner(strhashval
, cl
, ost
, lock
);
4734 return nfserr_jukebox
;
4736 /* with an existing lockowner, seqids must be the same */
4737 if (!cstate
->minorversion
&&
4738 lock
->lk_new_lock_seqid
!= lo
->lo_owner
.so_seqid
)
4739 return nfserr_bad_seqid
;
4742 *lst
= find_lock_stateid(lo
, fi
);
4744 *lst
= alloc_init_lock_stateid(lo
, fi
, inode
, ost
);
4746 release_lockowner_if_empty(lo
);
4747 return nfserr_jukebox
;
4758 nfsd4_lock(struct svc_rqst
*rqstp
, struct nfsd4_compound_state
*cstate
,
4759 struct nfsd4_lock
*lock
)
4761 struct nfs4_openowner
*open_sop
= NULL
;
4762 struct nfs4_lockowner
*lock_sop
= NULL
;
4763 struct nfs4_ol_stateid
*lock_stp
;
4764 struct nfs4_file
*fp
;
4765 struct file
*filp
= NULL
;
4766 struct file_lock
*file_lock
= NULL
;
4767 struct file_lock
*conflock
= NULL
;
4769 bool new_state
= false;
4772 struct net
*net
= SVC_NET(rqstp
);
4773 struct nfsd_net
*nn
= net_generic(net
, nfsd_net_id
);
4775 dprintk("NFSD: nfsd4_lock: start=%Ld length=%Ld\n",
4776 (long long) lock
->lk_offset
,
4777 (long long) lock
->lk_length
);
4779 if (check_lock_length(lock
->lk_offset
, lock
->lk_length
))
4780 return nfserr_inval
;
4782 if ((status
= fh_verify(rqstp
, &cstate
->current_fh
,
4783 S_IFREG
, NFSD_MAY_LOCK
))) {
4784 dprintk("NFSD: nfsd4_lock: permission denied!\n");
4790 if (lock
->lk_is_new
) {
4791 struct nfs4_ol_stateid
*open_stp
= NULL
;
4793 if (nfsd4_has_session(cstate
))
4794 /* See rfc 5661 18.10.3: given clientid is ignored: */
4795 memcpy(&lock
->v
.new.clientid
,
4796 &cstate
->session
->se_client
->cl_clientid
,
4797 sizeof(clientid_t
));
4799 status
= nfserr_stale_clientid
;
4800 if (STALE_CLIENTID(&lock
->lk_new_clientid
, nn
))
4803 /* validate and update open stateid and open seqid */
4804 status
= nfs4_preprocess_confirmed_seqid_op(cstate
,
4805 lock
->lk_new_open_seqid
,
4806 &lock
->lk_new_open_stateid
,
4810 open_sop
= openowner(open_stp
->st_stateowner
);
4811 status
= nfserr_bad_stateid
;
4812 if (!same_clid(&open_sop
->oo_owner
.so_client
->cl_clientid
,
4813 &lock
->v
.new.clientid
))
4815 status
= lookup_or_create_lock_state(cstate
, open_stp
, lock
,
4816 &lock_stp
, &new_state
);
4818 status
= nfs4_preprocess_seqid_op(cstate
,
4819 lock
->lk_old_lock_seqid
,
4820 &lock
->lk_old_lock_stateid
,
4821 NFS4_LOCK_STID
, &lock_stp
, nn
);
4824 lock_sop
= lockowner(lock_stp
->st_stateowner
);
4826 lkflg
= setlkflg(lock
->lk_type
);
4827 status
= nfs4_check_openmode(lock_stp
, lkflg
);
4831 status
= nfserr_grace
;
4832 if (locks_in_grace(net
) && !lock
->lk_reclaim
)
4834 status
= nfserr_no_grace
;
4835 if (!locks_in_grace(net
) && lock
->lk_reclaim
)
4838 file_lock
= locks_alloc_lock();
4840 dprintk("NFSD: %s: unable to allocate lock!\n", __func__
);
4841 status
= nfserr_jukebox
;
4845 fp
= lock_stp
->st_file
;
4846 locks_init_lock(file_lock
);
4847 switch (lock
->lk_type
) {
4850 spin_lock(&fp
->fi_lock
);
4851 filp
= find_readable_file_locked(fp
);
4853 get_lock_access(lock_stp
, NFS4_SHARE_ACCESS_READ
);
4854 spin_unlock(&fp
->fi_lock
);
4855 file_lock
->fl_type
= F_RDLCK
;
4858 case NFS4_WRITEW_LT
:
4859 spin_lock(&fp
->fi_lock
);
4860 filp
= find_writeable_file_locked(fp
);
4862 get_lock_access(lock_stp
, NFS4_SHARE_ACCESS_WRITE
);
4863 spin_unlock(&fp
->fi_lock
);
4864 file_lock
->fl_type
= F_WRLCK
;
4867 status
= nfserr_inval
;
4871 status
= nfserr_openmode
;
4874 file_lock
->fl_owner
= (fl_owner_t
)lock_sop
;
4875 file_lock
->fl_pid
= current
->tgid
;
4876 file_lock
->fl_file
= filp
;
4877 file_lock
->fl_flags
= FL_POSIX
;
4878 file_lock
->fl_lmops
= &nfsd_posix_mng_ops
;
4879 file_lock
->fl_start
= lock
->lk_offset
;
4880 file_lock
->fl_end
= last_byte_offset(lock
->lk_offset
, lock
->lk_length
);
4881 nfs4_transform_lock_offset(file_lock
);
4883 conflock
= locks_alloc_lock();
4885 dprintk("NFSD: %s: unable to allocate lock!\n", __func__
);
4886 status
= nfserr_jukebox
;
4890 err
= vfs_lock_file(filp
, F_SETLK
, file_lock
, conflock
);
4892 case 0: /* success! */
4893 update_stateid(&lock_stp
->st_stid
.sc_stateid
);
4894 memcpy(&lock
->lk_resp_stateid
, &lock_stp
->st_stid
.sc_stateid
,
4898 case (EAGAIN
): /* conflock holds conflicting lock */
4899 status
= nfserr_denied
;
4900 dprintk("NFSD: nfsd4_lock: conflicting lock found!\n");
4901 nfs4_set_lock_denied(conflock
, &lock
->lk_denied
);
4904 status
= nfserr_deadlock
;
4907 dprintk("NFSD: nfsd4_lock: vfs_lock_file() failed! status %d\n",err
);
4908 status
= nfserrno(err
);
4914 if (status
&& new_state
)
4915 release_lock_stateid(lock_stp
);
4916 nfsd4_bump_seqid(cstate
, status
);
4917 if (!cstate
->replay_owner
)
4918 nfs4_unlock_state();
4920 locks_free_lock(file_lock
);
4922 locks_free_lock(conflock
);
4927 * The NFSv4 spec allows a client to do a LOCKT without holding an OPEN,
4928 * so we do a temporary open here just to get an open file to pass to
4929 * vfs_test_lock. (Arguably perhaps test_lock should be done with an
4932 static __be32
nfsd_test_lock(struct svc_rqst
*rqstp
, struct svc_fh
*fhp
, struct file_lock
*lock
)
4935 __be32 err
= nfsd_open(rqstp
, fhp
, S_IFREG
, NFSD_MAY_READ
, &file
);
4937 err
= nfserrno(vfs_test_lock(file
, lock
));
4947 nfsd4_lockt(struct svc_rqst
*rqstp
, struct nfsd4_compound_state
*cstate
,
4948 struct nfsd4_lockt
*lockt
)
4950 struct file_lock
*file_lock
= NULL
;
4951 struct nfs4_lockowner
*lo
;
4953 struct nfsd_net
*nn
= net_generic(SVC_NET(rqstp
), nfsd_net_id
);
4955 if (locks_in_grace(SVC_NET(rqstp
)))
4956 return nfserr_grace
;
4958 if (check_lock_length(lockt
->lt_offset
, lockt
->lt_length
))
4959 return nfserr_inval
;
4963 if (!nfsd4_has_session(cstate
)) {
4964 status
= lookup_clientid(&lockt
->lt_clientid
, cstate
, nn
);
4969 if ((status
= fh_verify(rqstp
, &cstate
->current_fh
, S_IFREG
, 0)))
4972 file_lock
= locks_alloc_lock();
4974 dprintk("NFSD: %s: unable to allocate lock!\n", __func__
);
4975 status
= nfserr_jukebox
;
4978 locks_init_lock(file_lock
);
4979 switch (lockt
->lt_type
) {
4982 file_lock
->fl_type
= F_RDLCK
;
4985 case NFS4_WRITEW_LT
:
4986 file_lock
->fl_type
= F_WRLCK
;
4989 dprintk("NFSD: nfs4_lockt: bad lock type!\n");
4990 status
= nfserr_inval
;
4994 lo
= find_lockowner_str(&lockt
->lt_clientid
, &lockt
->lt_owner
, nn
);
4996 file_lock
->fl_owner
= (fl_owner_t
)lo
;
4997 file_lock
->fl_pid
= current
->tgid
;
4998 file_lock
->fl_flags
= FL_POSIX
;
5000 file_lock
->fl_start
= lockt
->lt_offset
;
5001 file_lock
->fl_end
= last_byte_offset(lockt
->lt_offset
, lockt
->lt_length
);
5003 nfs4_transform_lock_offset(file_lock
);
5005 status
= nfsd_test_lock(rqstp
, &cstate
->current_fh
, file_lock
);
5009 if (file_lock
->fl_type
!= F_UNLCK
) {
5010 status
= nfserr_denied
;
5011 nfs4_set_lock_denied(file_lock
, &lockt
->lt_denied
);
5014 nfs4_unlock_state();
5016 locks_free_lock(file_lock
);
5021 nfsd4_locku(struct svc_rqst
*rqstp
, struct nfsd4_compound_state
*cstate
,
5022 struct nfsd4_locku
*locku
)
5024 struct nfs4_ol_stateid
*stp
;
5025 struct file
*filp
= NULL
;
5026 struct file_lock
*file_lock
= NULL
;
5029 struct nfsd_net
*nn
= net_generic(SVC_NET(rqstp
), nfsd_net_id
);
5031 dprintk("NFSD: nfsd4_locku: start=%Ld length=%Ld\n",
5032 (long long) locku
->lu_offset
,
5033 (long long) locku
->lu_length
);
5035 if (check_lock_length(locku
->lu_offset
, locku
->lu_length
))
5036 return nfserr_inval
;
5040 status
= nfs4_preprocess_seqid_op(cstate
, locku
->lu_seqid
,
5041 &locku
->lu_stateid
, NFS4_LOCK_STID
,
5045 filp
= find_any_file(stp
->st_file
);
5047 status
= nfserr_lock_range
;
5050 file_lock
= locks_alloc_lock();
5052 dprintk("NFSD: %s: unable to allocate lock!\n", __func__
);
5053 status
= nfserr_jukebox
;
5056 locks_init_lock(file_lock
);
5057 file_lock
->fl_type
= F_UNLCK
;
5058 file_lock
->fl_owner
= (fl_owner_t
)lockowner(stp
->st_stateowner
);
5059 file_lock
->fl_pid
= current
->tgid
;
5060 file_lock
->fl_file
= filp
;
5061 file_lock
->fl_flags
= FL_POSIX
;
5062 file_lock
->fl_lmops
= &nfsd_posix_mng_ops
;
5063 file_lock
->fl_start
= locku
->lu_offset
;
5065 file_lock
->fl_end
= last_byte_offset(locku
->lu_offset
,
5067 nfs4_transform_lock_offset(file_lock
);
5069 err
= vfs_lock_file(filp
, F_SETLK
, file_lock
, NULL
);
5071 dprintk("NFSD: nfs4_locku: vfs_lock_file failed!\n");
5074 update_stateid(&stp
->st_stid
.sc_stateid
);
5075 memcpy(&locku
->lu_stateid
, &stp
->st_stid
.sc_stateid
, sizeof(stateid_t
));
5079 nfsd4_bump_seqid(cstate
, status
);
5080 if (!cstate
->replay_owner
)
5081 nfs4_unlock_state();
5083 locks_free_lock(file_lock
);
5087 status
= nfserrno(err
);
5093 * true: locks held by lockowner
5094 * false: no locks held by lockowner
5097 check_for_locks(struct nfs4_file
*fp
, struct nfs4_lockowner
*lowner
)
5099 struct file_lock
**flpp
;
5101 struct file
*filp
= find_any_file(fp
);
5102 struct inode
*inode
;
5105 /* Any valid lock stateid should have some sort of access */
5110 inode
= file_inode(filp
);
5112 spin_lock(&inode
->i_lock
);
5113 for (flpp
= &inode
->i_flock
; *flpp
!= NULL
; flpp
= &(*flpp
)->fl_next
) {
5114 if ((*flpp
)->fl_owner
== (fl_owner_t
)lowner
) {
5119 spin_unlock(&inode
->i_lock
);
5125 nfsd4_release_lockowner(struct svc_rqst
*rqstp
,
5126 struct nfsd4_compound_state
*cstate
,
5127 struct nfsd4_release_lockowner
*rlockowner
)
5129 clientid_t
*clid
= &rlockowner
->rl_clientid
;
5130 struct nfs4_stateowner
*sop
= NULL
, *tmp
;
5131 struct nfs4_lockowner
*lo
;
5132 struct nfs4_ol_stateid
*stp
;
5133 struct xdr_netobj
*owner
= &rlockowner
->rl_owner
;
5134 unsigned int hashval
= ownerstr_hashval(clid
->cl_id
, owner
);
5136 struct nfsd_net
*nn
= net_generic(SVC_NET(rqstp
), nfsd_net_id
);
5138 dprintk("nfsd4_release_lockowner clientid: (%08x/%08x):\n",
5139 clid
->cl_boot
, clid
->cl_id
);
5143 status
= lookup_clientid(clid
, cstate
, nn
);
5147 status
= nfserr_locks_held
;
5149 /* Find the matching lock stateowner */
5150 list_for_each_entry(tmp
, &nn
->ownerstr_hashtbl
[hashval
], so_strhash
) {
5151 if (tmp
->so_is_open_owner
)
5153 if (same_owner_str(tmp
, owner
, clid
)) {
5159 /* No matching owner found, maybe a replay? Just declare victory... */
5165 lo
= lockowner(sop
);
5166 /* see if there are still any locks associated with it */
5167 list_for_each_entry(stp
, &sop
->so_stateids
, st_perstateowner
) {
5168 if (check_for_locks(stp
->st_file
, lo
))
5173 release_lockowner(lo
);
5175 nfs4_unlock_state();
5179 static inline struct nfs4_client_reclaim
*
5182 return kmalloc(sizeof(struct nfs4_client_reclaim
), GFP_KERNEL
);
5186 nfs4_has_reclaimed_state(const char *name
, struct nfsd_net
*nn
)
5188 struct nfs4_client_reclaim
*crp
;
5190 crp
= nfsd4_find_reclaim_client(name
, nn
);
5191 return (crp
&& crp
->cr_clp
);
5195 * failure => all reset bets are off, nfserr_no_grace...
5197 struct nfs4_client_reclaim
*
5198 nfs4_client_to_reclaim(const char *name
, struct nfsd_net
*nn
)
5200 unsigned int strhashval
;
5201 struct nfs4_client_reclaim
*crp
;
5203 dprintk("NFSD nfs4_client_to_reclaim NAME: %.*s\n", HEXDIR_LEN
, name
);
5204 crp
= alloc_reclaim();
5206 strhashval
= clientstr_hashval(name
);
5207 INIT_LIST_HEAD(&crp
->cr_strhash
);
5208 list_add(&crp
->cr_strhash
, &nn
->reclaim_str_hashtbl
[strhashval
]);
5209 memcpy(crp
->cr_recdir
, name
, HEXDIR_LEN
);
5211 nn
->reclaim_str_hashtbl_size
++;
5217 nfs4_remove_reclaim_record(struct nfs4_client_reclaim
*crp
, struct nfsd_net
*nn
)
5219 list_del(&crp
->cr_strhash
);
5221 nn
->reclaim_str_hashtbl_size
--;
5225 nfs4_release_reclaim(struct nfsd_net
*nn
)
5227 struct nfs4_client_reclaim
*crp
= NULL
;
5230 for (i
= 0; i
< CLIENT_HASH_SIZE
; i
++) {
5231 while (!list_empty(&nn
->reclaim_str_hashtbl
[i
])) {
5232 crp
= list_entry(nn
->reclaim_str_hashtbl
[i
].next
,
5233 struct nfs4_client_reclaim
, cr_strhash
);
5234 nfs4_remove_reclaim_record(crp
, nn
);
5237 WARN_ON_ONCE(nn
->reclaim_str_hashtbl_size
);
5241 * called from OPEN, CLAIM_PREVIOUS with a new clientid. */
5242 struct nfs4_client_reclaim
*
5243 nfsd4_find_reclaim_client(const char *recdir
, struct nfsd_net
*nn
)
5245 unsigned int strhashval
;
5246 struct nfs4_client_reclaim
*crp
= NULL
;
5248 dprintk("NFSD: nfs4_find_reclaim_client for recdir %s\n", recdir
);
5250 strhashval
= clientstr_hashval(recdir
);
5251 list_for_each_entry(crp
, &nn
->reclaim_str_hashtbl
[strhashval
], cr_strhash
) {
5252 if (same_name(crp
->cr_recdir
, recdir
)) {
5260 * Called from OPEN. Look for clientid in reclaim list.
5263 nfs4_check_open_reclaim(clientid_t
*clid
,
5264 struct nfsd4_compound_state
*cstate
,
5265 struct nfsd_net
*nn
)
5269 /* find clientid in conf_id_hashtbl */
5270 status
= lookup_clientid(clid
, cstate
, nn
);
5272 return nfserr_reclaim_bad
;
5274 if (nfsd4_client_record_check(cstate
->clp
))
5275 return nfserr_reclaim_bad
;
5280 #ifdef CONFIG_NFSD_FAULT_INJECTION
5282 u64
nfsd_forget_client(struct nfs4_client
*clp
, u64 max
)
5284 if (mark_client_expired(clp
))
5290 u64
nfsd_print_client(struct nfs4_client
*clp
, u64 num
)
5292 char buf
[INET6_ADDRSTRLEN
];
5293 rpc_ntop((struct sockaddr
*)&clp
->cl_addr
, buf
, sizeof(buf
));
5294 printk(KERN_INFO
"NFS Client: %s\n", buf
);
5298 static void nfsd_print_count(struct nfs4_client
*clp
, unsigned int count
,
5301 char buf
[INET6_ADDRSTRLEN
];
5302 rpc_ntop((struct sockaddr
*)&clp
->cl_addr
, buf
, sizeof(buf
));
5303 printk(KERN_INFO
"NFS Client: %s has %u %s\n", buf
, count
, type
);
5306 static u64
nfsd_foreach_client_lock(struct nfs4_client
*clp
, u64 max
,
5307 void (*func
)(struct nfs4_ol_stateid
*))
5309 struct nfs4_openowner
*oop
;
5310 struct nfs4_ol_stateid
*stp
, *st_next
;
5311 struct nfs4_ol_stateid
*lst
, *lst_next
;
5314 list_for_each_entry(oop
, &clp
->cl_openowners
, oo_perclient
) {
5315 list_for_each_entry_safe(stp
, st_next
,
5316 &oop
->oo_owner
.so_stateids
, st_perstateowner
) {
5317 list_for_each_entry_safe(lst
, lst_next
,
5318 &stp
->st_locks
, st_locks
) {
5330 u64
nfsd_forget_client_locks(struct nfs4_client
*clp
, u64 max
)
5332 return nfsd_foreach_client_lock(clp
, max
, release_lock_stateid
);
5335 u64
nfsd_print_client_locks(struct nfs4_client
*clp
, u64 max
)
5337 u64 count
= nfsd_foreach_client_lock(clp
, max
, NULL
);
5338 nfsd_print_count(clp
, count
, "locked files");
5342 static u64
nfsd_foreach_client_open(struct nfs4_client
*clp
, u64 max
, void (*func
)(struct nfs4_openowner
*))
5344 struct nfs4_openowner
*oop
, *next
;
5347 list_for_each_entry_safe(oop
, next
, &clp
->cl_openowners
, oo_perclient
) {
5357 u64
nfsd_forget_client_openowners(struct nfs4_client
*clp
, u64 max
)
5359 return nfsd_foreach_client_open(clp
, max
, release_openowner
);
5362 u64
nfsd_print_client_openowners(struct nfs4_client
*clp
, u64 max
)
5364 u64 count
= nfsd_foreach_client_open(clp
, max
, NULL
);
5365 nfsd_print_count(clp
, count
, "open files");
5369 static u64
nfsd_find_all_delegations(struct nfs4_client
*clp
, u64 max
,
5370 struct list_head
*victims
)
5372 struct nfs4_delegation
*dp
, *next
;
5375 lockdep_assert_held(&state_lock
);
5376 list_for_each_entry_safe(dp
, next
, &clp
->cl_delegations
, dl_perclnt
) {
5379 * It's not safe to mess with delegations that have a
5380 * non-zero dl_time. They might have already been broken
5381 * and could be processed by the laundromat outside of
5382 * the state_lock. Just leave them be.
5384 if (dp
->dl_time
!= 0)
5388 * Increment dl_time to ensure that delegation breaks
5389 * don't monkey with it now that we are.
5392 list_move(&dp
->dl_recall_lru
, victims
);
5400 u64
nfsd_forget_client_delegations(struct nfs4_client
*clp
, u64 max
)
5402 struct nfs4_delegation
*dp
, *next
;
5406 spin_lock(&state_lock
);
5407 count
= nfsd_find_all_delegations(clp
, max
, &victims
);
5408 spin_unlock(&state_lock
);
5410 list_for_each_entry_safe(dp
, next
, &victims
, dl_recall_lru
)
5411 revoke_delegation(dp
);
5416 u64
nfsd_recall_client_delegations(struct nfs4_client
*clp
, u64 max
)
5418 struct nfs4_delegation
*dp
;
5422 spin_lock(&state_lock
);
5423 count
= nfsd_find_all_delegations(clp
, max
, &victims
);
5424 while (!list_empty(&victims
)) {
5425 dp
= list_first_entry(&victims
, struct nfs4_delegation
,
5427 list_del_init(&dp
->dl_recall_lru
);
5429 nfsd_break_one_deleg(dp
);
5431 spin_unlock(&state_lock
);
5436 u64
nfsd_print_client_delegations(struct nfs4_client
*clp
, u64 max
)
5440 spin_lock(&state_lock
);
5441 count
= nfsd_find_all_delegations(clp
, max
, NULL
);
5442 spin_unlock(&state_lock
);
5444 nfsd_print_count(clp
, count
, "delegations");
5448 u64
nfsd_for_n_state(u64 max
, u64 (*func
)(struct nfs4_client
*, u64
))
5450 struct nfs4_client
*clp
, *next
;
5452 struct nfsd_net
*nn
= net_generic(current
->nsproxy
->net_ns
, nfsd_net_id
);
5454 if (!nfsd_netns_ready(nn
))
5457 list_for_each_entry_safe(clp
, next
, &nn
->client_lru
, cl_lru
) {
5458 count
+= func(clp
, max
- count
);
5459 if ((max
!= 0) && (count
>= max
))
5466 struct nfs4_client
*nfsd_find_client(struct sockaddr_storage
*addr
, size_t addr_size
)
5468 struct nfs4_client
*clp
;
5469 struct nfsd_net
*nn
= net_generic(current
->nsproxy
->net_ns
, nfsd_net_id
);
5471 if (!nfsd_netns_ready(nn
))
5474 list_for_each_entry(clp
, &nn
->client_lru
, cl_lru
) {
5475 if (memcmp(&clp
->cl_addr
, addr
, addr_size
) == 0)
5481 #endif /* CONFIG_NFSD_FAULT_INJECTION */
5484 * Since the lifetime of a delegation isn't limited to that of an open, a
5485 * client may quite reasonably hang on to a delegation as long as it has
5486 * the inode cached. This becomes an obvious problem the first time a
5487 * client's inode cache approaches the size of the server's total memory.
5489 * For now we avoid this problem by imposing a hard limit on the number
5490 * of delegations, which varies according to the server's memory size.
5493 set_max_delegations(void)
5496 * Allow at most 4 delegations per megabyte of RAM. Quick
5497 * estimates suggest that in the worst case (where every delegation
5498 * is for a different inode), a delegation could take about 1.5K,
5499 * giving a worst case usage of about 6% of memory.
5501 max_delegations
= nr_free_buffer_pages() >> (20 - 2 - PAGE_SHIFT
);
5504 static int nfs4_state_create_net(struct net
*net
)
5506 struct nfsd_net
*nn
= net_generic(net
, nfsd_net_id
);
5509 nn
->conf_id_hashtbl
= kmalloc(sizeof(struct list_head
) *
5510 CLIENT_HASH_SIZE
, GFP_KERNEL
);
5511 if (!nn
->conf_id_hashtbl
)
5513 nn
->unconf_id_hashtbl
= kmalloc(sizeof(struct list_head
) *
5514 CLIENT_HASH_SIZE
, GFP_KERNEL
);
5515 if (!nn
->unconf_id_hashtbl
)
5517 nn
->ownerstr_hashtbl
= kmalloc(sizeof(struct list_head
) *
5518 OWNER_HASH_SIZE
, GFP_KERNEL
);
5519 if (!nn
->ownerstr_hashtbl
)
5521 nn
->sessionid_hashtbl
= kmalloc(sizeof(struct list_head
) *
5522 SESSION_HASH_SIZE
, GFP_KERNEL
);
5523 if (!nn
->sessionid_hashtbl
)
5526 for (i
= 0; i
< CLIENT_HASH_SIZE
; i
++) {
5527 INIT_LIST_HEAD(&nn
->conf_id_hashtbl
[i
]);
5528 INIT_LIST_HEAD(&nn
->unconf_id_hashtbl
[i
]);
5530 for (i
= 0; i
< OWNER_HASH_SIZE
; i
++)
5531 INIT_LIST_HEAD(&nn
->ownerstr_hashtbl
[i
]);
5532 for (i
= 0; i
< SESSION_HASH_SIZE
; i
++)
5533 INIT_LIST_HEAD(&nn
->sessionid_hashtbl
[i
]);
5534 nn
->conf_name_tree
= RB_ROOT
;
5535 nn
->unconf_name_tree
= RB_ROOT
;
5536 INIT_LIST_HEAD(&nn
->client_lru
);
5537 INIT_LIST_HEAD(&nn
->close_lru
);
5538 INIT_LIST_HEAD(&nn
->del_recall_lru
);
5539 spin_lock_init(&nn
->client_lock
);
5541 INIT_DELAYED_WORK(&nn
->laundromat_work
, laundromat_main
);
5547 kfree(nn
->ownerstr_hashtbl
);
5549 kfree(nn
->unconf_id_hashtbl
);
5551 kfree(nn
->conf_id_hashtbl
);
5557 nfs4_state_destroy_net(struct net
*net
)
5560 struct nfs4_client
*clp
= NULL
;
5561 struct nfsd_net
*nn
= net_generic(net
, nfsd_net_id
);
5563 for (i
= 0; i
< CLIENT_HASH_SIZE
; i
++) {
5564 while (!list_empty(&nn
->conf_id_hashtbl
[i
])) {
5565 clp
= list_entry(nn
->conf_id_hashtbl
[i
].next
, struct nfs4_client
, cl_idhash
);
5566 destroy_client(clp
);
5570 for (i
= 0; i
< CLIENT_HASH_SIZE
; i
++) {
5571 while (!list_empty(&nn
->unconf_id_hashtbl
[i
])) {
5572 clp
= list_entry(nn
->unconf_id_hashtbl
[i
].next
, struct nfs4_client
, cl_idhash
);
5573 destroy_client(clp
);
5577 kfree(nn
->sessionid_hashtbl
);
5578 kfree(nn
->ownerstr_hashtbl
);
5579 kfree(nn
->unconf_id_hashtbl
);
5580 kfree(nn
->conf_id_hashtbl
);
5585 nfs4_state_start_net(struct net
*net
)
5587 struct nfsd_net
*nn
= net_generic(net
, nfsd_net_id
);
5590 ret
= nfs4_state_create_net(net
);
5593 nfsd4_client_tracking_init(net
);
5594 nn
->boot_time
= get_seconds();
5595 locks_start_grace(net
, &nn
->nfsd4_manager
);
5596 nn
->grace_ended
= false;
5597 printk(KERN_INFO
"NFSD: starting %ld-second grace period (net %p)\n",
5598 nn
->nfsd4_grace
, net
);
5599 queue_delayed_work(laundry_wq
, &nn
->laundromat_work
, nn
->nfsd4_grace
* HZ
);
5603 /* initialization to perform when the nfsd service is started: */
5606 nfs4_state_start(void)
5610 ret
= set_callback_cred();
5613 laundry_wq
= create_singlethread_workqueue("nfsd4");
5614 if (laundry_wq
== NULL
) {
5618 ret
= nfsd4_create_callback_queue();
5620 goto out_free_laundry
;
5622 set_max_delegations();
5627 destroy_workqueue(laundry_wq
);
5633 nfs4_state_shutdown_net(struct net
*net
)
5635 struct nfs4_delegation
*dp
= NULL
;
5636 struct list_head
*pos
, *next
, reaplist
;
5637 struct nfsd_net
*nn
= net_generic(net
, nfsd_net_id
);
5639 cancel_delayed_work_sync(&nn
->laundromat_work
);
5640 locks_end_grace(&nn
->nfsd4_manager
);
5643 INIT_LIST_HEAD(&reaplist
);
5644 spin_lock(&state_lock
);
5645 list_for_each_safe(pos
, next
, &nn
->del_recall_lru
) {
5646 dp
= list_entry (pos
, struct nfs4_delegation
, dl_recall_lru
);
5647 list_move(&dp
->dl_recall_lru
, &reaplist
);
5649 spin_unlock(&state_lock
);
5650 list_for_each_safe(pos
, next
, &reaplist
) {
5651 dp
= list_entry (pos
, struct nfs4_delegation
, dl_recall_lru
);
5652 destroy_delegation(dp
);
5655 nfsd4_client_tracking_exit(net
);
5656 nfs4_state_destroy_net(net
);
5657 nfs4_unlock_state();
5661 nfs4_state_shutdown(void)
5663 destroy_workqueue(laundry_wq
);
5664 nfsd4_destroy_callback_queue();
5668 get_stateid(struct nfsd4_compound_state
*cstate
, stateid_t
*stateid
)
5670 if (HAS_STATE_ID(cstate
, CURRENT_STATE_ID_FLAG
) && CURRENT_STATEID(stateid
))
5671 memcpy(stateid
, &cstate
->current_stateid
, sizeof(stateid_t
));
5675 put_stateid(struct nfsd4_compound_state
*cstate
, stateid_t
*stateid
)
5677 if (cstate
->minorversion
) {
5678 memcpy(&cstate
->current_stateid
, stateid
, sizeof(stateid_t
));
5679 SET_STATE_ID(cstate
, CURRENT_STATE_ID_FLAG
);
5684 clear_current_stateid(struct nfsd4_compound_state
*cstate
)
5686 CLEAR_STATE_ID(cstate
, CURRENT_STATE_ID_FLAG
);
5690 * functions to set current state id
5693 nfsd4_set_opendowngradestateid(struct nfsd4_compound_state
*cstate
, struct nfsd4_open_downgrade
*odp
)
5695 put_stateid(cstate
, &odp
->od_stateid
);
5699 nfsd4_set_openstateid(struct nfsd4_compound_state
*cstate
, struct nfsd4_open
*open
)
5701 put_stateid(cstate
, &open
->op_stateid
);
5705 nfsd4_set_closestateid(struct nfsd4_compound_state
*cstate
, struct nfsd4_close
*close
)
5707 put_stateid(cstate
, &close
->cl_stateid
);
5711 nfsd4_set_lockstateid(struct nfsd4_compound_state
*cstate
, struct nfsd4_lock
*lock
)
5713 put_stateid(cstate
, &lock
->lk_resp_stateid
);
5717 * functions to consume current state id
5721 nfsd4_get_opendowngradestateid(struct nfsd4_compound_state
*cstate
, struct nfsd4_open_downgrade
*odp
)
5723 get_stateid(cstate
, &odp
->od_stateid
);
5727 nfsd4_get_delegreturnstateid(struct nfsd4_compound_state
*cstate
, struct nfsd4_delegreturn
*drp
)
5729 get_stateid(cstate
, &drp
->dr_stateid
);
5733 nfsd4_get_freestateid(struct nfsd4_compound_state
*cstate
, struct nfsd4_free_stateid
*fsp
)
5735 get_stateid(cstate
, &fsp
->fr_stateid
);
5739 nfsd4_get_setattrstateid(struct nfsd4_compound_state
*cstate
, struct nfsd4_setattr
*setattr
)
5741 get_stateid(cstate
, &setattr
->sa_stateid
);
5745 nfsd4_get_closestateid(struct nfsd4_compound_state
*cstate
, struct nfsd4_close
*close
)
5747 get_stateid(cstate
, &close
->cl_stateid
);
5751 nfsd4_get_lockustateid(struct nfsd4_compound_state
*cstate
, struct nfsd4_locku
*locku
)
5753 get_stateid(cstate
, &locku
->lu_stateid
);
5757 nfsd4_get_readstateid(struct nfsd4_compound_state
*cstate
, struct nfsd4_read
*read
)
5759 get_stateid(cstate
, &read
->rd_stateid
);
5763 nfsd4_get_writestateid(struct nfsd4_compound_state
*cstate
, struct nfsd4_write
*write
)
5765 get_stateid(cstate
, &write
->wr_stateid
);