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 int check_for_locks(struct nfs4_file
*filp
, 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 void nfsd4_put_session(struct nfsd4_session
*ses
)
108 if (atomic_dec_and_test(&ses
->se_ref
) && is_session_dead(ses
))
112 static __be32
mark_session_dead_locked(struct nfsd4_session
*ses
, int ref_held_by_me
)
114 if (atomic_read(&ses
->se_ref
) > ref_held_by_me
)
115 return nfserr_jukebox
;
116 ses
->se_flags
|= NFS4_SESSION_DEAD
;
120 static __be32
nfsd4_get_session_locked(struct nfsd4_session
*ses
)
122 if (is_session_dead(ses
))
123 return nfserr_badsession
;
124 atomic_inc(&ses
->se_ref
);
129 nfs4_unlock_state(void)
131 mutex_unlock(&client_mutex
);
134 static bool is_client_expired(struct nfs4_client
*clp
)
136 return clp
->cl_time
== 0;
139 static __be32
mark_client_expired_locked(struct nfs4_client
*clp
)
141 if (atomic_read(&clp
->cl_refcount
))
142 return nfserr_jukebox
;
147 static __be32
mark_client_expired(struct nfs4_client
*clp
)
149 struct nfsd_net
*nn
= net_generic(clp
->net
, nfsd_net_id
);
152 spin_lock(&nn
->client_lock
);
153 ret
= mark_client_expired_locked(clp
);
154 spin_unlock(&nn
->client_lock
);
158 static __be32
get_client_locked(struct nfs4_client
*clp
)
160 if (is_client_expired(clp
))
161 return nfserr_expired
;
162 atomic_inc(&clp
->cl_refcount
);
166 /* must be called under the client_lock */
168 renew_client_locked(struct nfs4_client
*clp
)
170 struct nfsd_net
*nn
= net_generic(clp
->net
, nfsd_net_id
);
172 if (is_client_expired(clp
)) {
174 printk("%s: client (clientid %08x/%08x) already expired\n",
176 clp
->cl_clientid
.cl_boot
,
177 clp
->cl_clientid
.cl_id
);
181 dprintk("renewing client (clientid %08x/%08x)\n",
182 clp
->cl_clientid
.cl_boot
,
183 clp
->cl_clientid
.cl_id
);
184 list_move_tail(&clp
->cl_lru
, &nn
->client_lru
);
185 clp
->cl_time
= get_seconds();
189 renew_client(struct nfs4_client
*clp
)
191 struct nfsd_net
*nn
= net_generic(clp
->net
, nfsd_net_id
);
193 spin_lock(&nn
->client_lock
);
194 renew_client_locked(clp
);
195 spin_unlock(&nn
->client_lock
);
198 static void put_client_renew_locked(struct nfs4_client
*clp
)
200 if (!atomic_dec_and_test(&clp
->cl_refcount
))
202 if (!is_client_expired(clp
))
203 renew_client_locked(clp
);
207 opaque_hashval(const void *ptr
, int nbytes
)
209 unsigned char *cptr
= (unsigned char *) ptr
;
219 static void nfsd4_free_file(struct nfs4_file
*f
)
221 kmem_cache_free(file_slab
, f
);
225 put_nfs4_file(struct nfs4_file
*fi
)
227 if (atomic_dec_and_lock(&fi
->fi_ref
, &state_lock
)) {
228 hlist_del(&fi
->fi_hash
);
229 spin_unlock(&state_lock
);
236 get_nfs4_file(struct nfs4_file
*fi
)
238 atomic_inc(&fi
->fi_ref
);
241 static int num_delegations
;
242 unsigned long max_delegations
;
245 * Open owner state (share locks)
248 /* hash tables for lock and open owners */
249 #define OWNER_HASH_BITS 8
250 #define OWNER_HASH_SIZE (1 << OWNER_HASH_BITS)
251 #define OWNER_HASH_MASK (OWNER_HASH_SIZE - 1)
253 static unsigned int ownerstr_hashval(u32 clientid
, struct xdr_netobj
*ownername
)
257 ret
= opaque_hashval(ownername
->data
, ownername
->len
);
259 return ret
& OWNER_HASH_MASK
;
262 /* hash table for nfs4_file */
263 #define FILE_HASH_BITS 8
264 #define FILE_HASH_SIZE (1 << FILE_HASH_BITS)
266 static unsigned int file_hashval(struct inode
*ino
)
268 /* XXX: why are we hashing on inode pointer, anyway? */
269 return hash_ptr(ino
, FILE_HASH_BITS
);
272 static struct hlist_head file_hashtbl
[FILE_HASH_SIZE
];
274 static void __nfs4_file_get_access(struct nfs4_file
*fp
, int oflag
)
276 WARN_ON_ONCE(!(fp
->fi_fds
[oflag
] || fp
->fi_fds
[O_RDWR
]));
277 atomic_inc(&fp
->fi_access
[oflag
]);
280 static void nfs4_file_get_access(struct nfs4_file
*fp
, int oflag
)
282 if (oflag
== O_RDWR
) {
283 __nfs4_file_get_access(fp
, O_RDONLY
);
284 __nfs4_file_get_access(fp
, O_WRONLY
);
286 __nfs4_file_get_access(fp
, oflag
);
289 static void nfs4_file_put_fd(struct nfs4_file
*fp
, int oflag
)
291 if (fp
->fi_fds
[oflag
]) {
292 fput(fp
->fi_fds
[oflag
]);
293 fp
->fi_fds
[oflag
] = NULL
;
297 static void __nfs4_file_put_access(struct nfs4_file
*fp
, int oflag
)
299 if (atomic_dec_and_test(&fp
->fi_access
[oflag
])) {
300 nfs4_file_put_fd(fp
, oflag
);
301 if (atomic_read(&fp
->fi_access
[1 - oflag
]) == 0)
302 nfs4_file_put_fd(fp
, O_RDWR
);
306 static void nfs4_file_put_access(struct nfs4_file
*fp
, int oflag
)
308 if (oflag
== O_RDWR
) {
309 __nfs4_file_put_access(fp
, O_RDONLY
);
310 __nfs4_file_put_access(fp
, O_WRONLY
);
312 __nfs4_file_put_access(fp
, oflag
);
315 static struct nfs4_stid
*nfs4_alloc_stid(struct nfs4_client
*cl
, struct
318 struct idr
*stateids
= &cl
->cl_stateids
;
319 struct nfs4_stid
*stid
;
322 stid
= kmem_cache_alloc(slab
, GFP_KERNEL
);
326 new_id
= idr_alloc_cyclic(stateids
, stid
, 0, 0, GFP_KERNEL
);
329 stid
->sc_client
= cl
;
331 stid
->sc_stateid
.si_opaque
.so_id
= new_id
;
332 stid
->sc_stateid
.si_opaque
.so_clid
= cl
->cl_clientid
;
333 /* Will be incremented before return to client: */
334 stid
->sc_stateid
.si_generation
= 0;
337 * It shouldn't be a problem to reuse an opaque stateid value.
338 * I don't think it is for 4.1. But with 4.0 I worry that, for
339 * example, a stray write retransmission could be accepted by
340 * the server when it should have been rejected. Therefore,
341 * adopt a trick from the sctp code to attempt to maximize the
342 * amount of time until an id is reused, by ensuring they always
343 * "increase" (mod INT_MAX):
347 kmem_cache_free(slab
, stid
);
351 static struct nfs4_ol_stateid
* nfs4_alloc_stateid(struct nfs4_client
*clp
)
353 return openlockstateid(nfs4_alloc_stid(clp
, stateid_slab
));
357 * When we recall a delegation, we should be careful not to hand it
358 * out again straight away.
359 * To ensure this we keep a pair of bloom filters ('new' and 'old')
360 * in which the filehandles of recalled delegations are "stored".
361 * If a filehandle appear in either filter, a delegation is blocked.
362 * When a delegation is recalled, the filehandle is stored in the "new"
364 * Every 30 seconds we swap the filters and clear the "new" one,
365 * unless both are empty of course.
367 * Each filter is 256 bits. We hash the filehandle to 32bit and use the
368 * low 3 bytes as hash-table indices.
370 * 'state_lock', which is always held when block_delegations() is called,
371 * is used to manage concurrent access. Testing does not need the lock
372 * except when swapping the two filters.
374 static struct bloom_pair
{
375 int entries
, old_entries
;
377 int new; /* index into 'set' */
378 DECLARE_BITMAP(set
[2], 256);
379 } blocked_delegations
;
381 static int delegation_blocked(struct knfsd_fh
*fh
)
384 struct bloom_pair
*bd
= &blocked_delegations
;
386 if (bd
->entries
== 0)
388 if (seconds_since_boot() - bd
->swap_time
> 30) {
389 spin_lock(&state_lock
);
390 if (seconds_since_boot() - bd
->swap_time
> 30) {
391 bd
->entries
-= bd
->old_entries
;
392 bd
->old_entries
= bd
->entries
;
393 memset(bd
->set
[bd
->new], 0,
396 bd
->swap_time
= seconds_since_boot();
398 spin_unlock(&state_lock
);
400 hash
= arch_fast_hash(&fh
->fh_base
, fh
->fh_size
, 0);
401 if (test_bit(hash
&255, bd
->set
[0]) &&
402 test_bit((hash
>>8)&255, bd
->set
[0]) &&
403 test_bit((hash
>>16)&255, bd
->set
[0]))
406 if (test_bit(hash
&255, bd
->set
[1]) &&
407 test_bit((hash
>>8)&255, bd
->set
[1]) &&
408 test_bit((hash
>>16)&255, bd
->set
[1]))
414 static void block_delegations(struct knfsd_fh
*fh
)
417 struct bloom_pair
*bd
= &blocked_delegations
;
419 hash
= arch_fast_hash(&fh
->fh_base
, fh
->fh_size
, 0);
421 __set_bit(hash
&255, bd
->set
[bd
->new]);
422 __set_bit((hash
>>8)&255, bd
->set
[bd
->new]);
423 __set_bit((hash
>>16)&255, bd
->set
[bd
->new]);
424 if (bd
->entries
== 0)
425 bd
->swap_time
= seconds_since_boot();
429 static struct nfs4_delegation
*
430 alloc_init_deleg(struct nfs4_client
*clp
, struct nfs4_ol_stateid
*stp
, struct svc_fh
*current_fh
)
432 struct nfs4_delegation
*dp
;
434 dprintk("NFSD alloc_init_deleg\n");
435 if (num_delegations
> max_delegations
)
437 if (delegation_blocked(¤t_fh
->fh_handle
))
439 dp
= delegstateid(nfs4_alloc_stid(clp
, deleg_slab
));
443 * delegation seqid's are never incremented. The 4.1 special
444 * meaning of seqid 0 isn't meaningful, really, but let's avoid
445 * 0 anyway just for consistency and use 1:
447 dp
->dl_stid
.sc_stateid
.si_generation
= 1;
449 INIT_LIST_HEAD(&dp
->dl_perfile
);
450 INIT_LIST_HEAD(&dp
->dl_perclnt
);
451 INIT_LIST_HEAD(&dp
->dl_recall_lru
);
453 dp
->dl_type
= NFS4_OPEN_DELEGATE_READ
;
454 fh_copy_shallow(&dp
->dl_fh
, ¤t_fh
->fh_handle
);
456 atomic_set(&dp
->dl_count
, 1);
457 nfsd4_init_callback(&dp
->dl_recall
);
461 static void remove_stid(struct nfs4_stid
*s
)
463 struct idr
*stateids
= &s
->sc_client
->cl_stateids
;
465 idr_remove(stateids
, s
->sc_stateid
.si_opaque
.so_id
);
468 static void nfs4_free_stid(struct kmem_cache
*slab
, struct nfs4_stid
*s
)
470 kmem_cache_free(slab
, s
);
474 nfs4_put_delegation(struct nfs4_delegation
*dp
)
476 if (atomic_dec_and_test(&dp
->dl_count
)) {
477 nfs4_free_stid(deleg_slab
, &dp
->dl_stid
);
482 static void nfs4_put_deleg_lease(struct nfs4_file
*fp
)
486 if (atomic_dec_and_test(&fp
->fi_delegees
)) {
487 vfs_setlease(fp
->fi_deleg_file
, F_UNLCK
, &fp
->fi_lease
);
489 fput(fp
->fi_deleg_file
);
490 fp
->fi_deleg_file
= NULL
;
494 static void unhash_stid(struct nfs4_stid
*s
)
500 hash_delegation_locked(struct nfs4_delegation
*dp
, struct nfs4_file
*fp
)
502 lockdep_assert_held(&state_lock
);
504 dp
->dl_stid
.sc_type
= NFS4_DELEG_STID
;
505 list_add(&dp
->dl_perfile
, &fp
->fi_delegations
);
506 list_add(&dp
->dl_perclnt
, &dp
->dl_stid
.sc_client
->cl_delegations
);
509 /* Called under the state lock. */
511 unhash_delegation(struct nfs4_delegation
*dp
)
513 spin_lock(&state_lock
);
514 list_del_init(&dp
->dl_perclnt
);
515 list_del_init(&dp
->dl_perfile
);
516 list_del_init(&dp
->dl_recall_lru
);
517 spin_unlock(&state_lock
);
519 nfs4_put_deleg_lease(dp
->dl_file
);
520 put_nfs4_file(dp
->dl_file
);
527 static void destroy_revoked_delegation(struct nfs4_delegation
*dp
)
529 list_del_init(&dp
->dl_recall_lru
);
530 remove_stid(&dp
->dl_stid
);
531 nfs4_put_delegation(dp
);
534 static void destroy_delegation(struct nfs4_delegation
*dp
)
536 unhash_delegation(dp
);
537 remove_stid(&dp
->dl_stid
);
538 nfs4_put_delegation(dp
);
541 static void revoke_delegation(struct nfs4_delegation
*dp
)
543 struct nfs4_client
*clp
= dp
->dl_stid
.sc_client
;
545 if (clp
->cl_minorversion
== 0)
546 destroy_delegation(dp
);
548 unhash_delegation(dp
);
549 dp
->dl_stid
.sc_type
= NFS4_REVOKED_DELEG_STID
;
550 list_add(&dp
->dl_recall_lru
, &clp
->cl_revoked
);
558 static unsigned int clientid_hashval(u32 id
)
560 return id
& CLIENT_HASH_MASK
;
563 static unsigned int clientstr_hashval(const char *name
)
565 return opaque_hashval(name
, 8) & CLIENT_HASH_MASK
;
569 * We store the NONE, READ, WRITE, and BOTH bits separately in the
570 * st_{access,deny}_bmap field of the stateid, in order to track not
571 * only what share bits are currently in force, but also what
572 * combinations of share bits previous opens have used. This allows us
573 * to enforce the recommendation of rfc 3530 14.2.19 that the server
574 * return an error if the client attempt to downgrade to a combination
575 * of share bits not explicable by closing some of its previous opens.
577 * XXX: This enforcement is actually incomplete, since we don't keep
578 * track of access/deny bit combinations; so, e.g., we allow:
580 * OPEN allow read, deny write
581 * OPEN allow both, deny none
582 * DOWNGRADE allow read, deny none
584 * which we should reject.
587 bmap_to_share_mode(unsigned long bmap
) {
589 unsigned int access
= 0;
591 for (i
= 1; i
< 4; i
++) {
592 if (test_bit(i
, &bmap
))
599 test_share(struct nfs4_ol_stateid
*stp
, struct nfsd4_open
*open
) {
600 unsigned int access
, deny
;
602 access
= bmap_to_share_mode(stp
->st_access_bmap
);
603 deny
= bmap_to_share_mode(stp
->st_deny_bmap
);
604 if ((access
& open
->op_share_deny
) || (deny
& open
->op_share_access
))
609 /* set share access for a given stateid */
611 set_access(u32 access
, struct nfs4_ol_stateid
*stp
)
613 __set_bit(access
, &stp
->st_access_bmap
);
616 /* clear share access for a given stateid */
618 clear_access(u32 access
, struct nfs4_ol_stateid
*stp
)
620 __clear_bit(access
, &stp
->st_access_bmap
);
623 /* test whether a given stateid has access */
625 test_access(u32 access
, struct nfs4_ol_stateid
*stp
)
627 return test_bit(access
, &stp
->st_access_bmap
);
630 /* set share deny for a given stateid */
632 set_deny(u32 access
, struct nfs4_ol_stateid
*stp
)
634 __set_bit(access
, &stp
->st_deny_bmap
);
637 /* clear share deny for a given stateid */
639 clear_deny(u32 access
, struct nfs4_ol_stateid
*stp
)
641 __clear_bit(access
, &stp
->st_deny_bmap
);
644 /* test whether a given stateid is denying specific access */
646 test_deny(u32 access
, struct nfs4_ol_stateid
*stp
)
648 return test_bit(access
, &stp
->st_deny_bmap
);
651 static int nfs4_access_to_omode(u32 access
)
653 switch (access
& NFS4_SHARE_ACCESS_BOTH
) {
654 case NFS4_SHARE_ACCESS_READ
:
656 case NFS4_SHARE_ACCESS_WRITE
:
658 case NFS4_SHARE_ACCESS_BOTH
:
665 /* release all access and file references for a given stateid */
667 release_all_access(struct nfs4_ol_stateid
*stp
)
671 for (i
= 1; i
< 4; i
++) {
672 if (test_access(i
, stp
))
673 nfs4_file_put_access(stp
->st_file
,
674 nfs4_access_to_omode(i
));
675 clear_access(i
, stp
);
679 static void unhash_generic_stateid(struct nfs4_ol_stateid
*stp
)
681 list_del(&stp
->st_perfile
);
682 list_del(&stp
->st_perstateowner
);
685 static void close_generic_stateid(struct nfs4_ol_stateid
*stp
)
687 release_all_access(stp
);
688 put_nfs4_file(stp
->st_file
);
692 static void free_generic_stateid(struct nfs4_ol_stateid
*stp
)
694 remove_stid(&stp
->st_stid
);
695 nfs4_free_stid(stateid_slab
, &stp
->st_stid
);
698 static void release_lock_stateid(struct nfs4_ol_stateid
*stp
)
702 unhash_generic_stateid(stp
);
703 unhash_stid(&stp
->st_stid
);
704 file
= find_any_file(stp
->st_file
);
706 locks_remove_posix(file
, (fl_owner_t
)lockowner(stp
->st_stateowner
));
707 close_generic_stateid(stp
);
708 free_generic_stateid(stp
);
711 static void unhash_lockowner(struct nfs4_lockowner
*lo
)
713 struct nfs4_ol_stateid
*stp
;
715 list_del(&lo
->lo_owner
.so_strhash
);
716 list_del(&lo
->lo_perstateid
);
717 list_del(&lo
->lo_owner_ino_hash
);
718 while (!list_empty(&lo
->lo_owner
.so_stateids
)) {
719 stp
= list_first_entry(&lo
->lo_owner
.so_stateids
,
720 struct nfs4_ol_stateid
, st_perstateowner
);
721 release_lock_stateid(stp
);
725 static void nfs4_free_lockowner(struct nfs4_lockowner
*lo
)
727 kfree(lo
->lo_owner
.so_owner
.data
);
728 kmem_cache_free(lockowner_slab
, lo
);
731 static void release_lockowner(struct nfs4_lockowner
*lo
)
733 unhash_lockowner(lo
);
734 nfs4_free_lockowner(lo
);
738 release_stateid_lockowners(struct nfs4_ol_stateid
*open_stp
)
740 struct nfs4_lockowner
*lo
;
742 while (!list_empty(&open_stp
->st_lockowners
)) {
743 lo
= list_entry(open_stp
->st_lockowners
.next
,
744 struct nfs4_lockowner
, lo_perstateid
);
745 release_lockowner(lo
);
749 static void unhash_open_stateid(struct nfs4_ol_stateid
*stp
)
751 unhash_generic_stateid(stp
);
752 release_stateid_lockowners(stp
);
753 close_generic_stateid(stp
);
756 static void release_open_stateid(struct nfs4_ol_stateid
*stp
)
758 unhash_open_stateid(stp
);
759 free_generic_stateid(stp
);
762 static void unhash_openowner(struct nfs4_openowner
*oo
)
764 struct nfs4_ol_stateid
*stp
;
766 list_del(&oo
->oo_owner
.so_strhash
);
767 list_del(&oo
->oo_perclient
);
768 while (!list_empty(&oo
->oo_owner
.so_stateids
)) {
769 stp
= list_first_entry(&oo
->oo_owner
.so_stateids
,
770 struct nfs4_ol_stateid
, st_perstateowner
);
771 release_open_stateid(stp
);
775 static void release_last_closed_stateid(struct nfs4_openowner
*oo
)
777 struct nfs4_ol_stateid
*s
= oo
->oo_last_closed_stid
;
780 free_generic_stateid(s
);
781 oo
->oo_last_closed_stid
= NULL
;
785 static void nfs4_free_openowner(struct nfs4_openowner
*oo
)
787 kfree(oo
->oo_owner
.so_owner
.data
);
788 kmem_cache_free(openowner_slab
, oo
);
791 static void release_openowner(struct nfs4_openowner
*oo
)
793 unhash_openowner(oo
);
794 list_del(&oo
->oo_close_lru
);
795 release_last_closed_stateid(oo
);
796 nfs4_free_openowner(oo
);
800 hash_sessionid(struct nfs4_sessionid
*sessionid
)
802 struct nfsd4_sessionid
*sid
= (struct nfsd4_sessionid
*)sessionid
;
804 return sid
->sequence
% SESSION_HASH_SIZE
;
809 dump_sessionid(const char *fn
, struct nfs4_sessionid
*sessionid
)
811 u32
*ptr
= (u32
*)(&sessionid
->data
[0]);
812 dprintk("%s: %u:%u:%u:%u\n", fn
, ptr
[0], ptr
[1], ptr
[2], ptr
[3]);
816 dump_sessionid(const char *fn
, struct nfs4_sessionid
*sessionid
)
822 * Bump the seqid on cstate->replay_owner, and clear replay_owner if it
823 * won't be used for replay.
825 void nfsd4_bump_seqid(struct nfsd4_compound_state
*cstate
, __be32 nfserr
)
827 struct nfs4_stateowner
*so
= cstate
->replay_owner
;
829 if (nfserr
== nfserr_replay_me
)
832 if (!seqid_mutating_err(ntohl(nfserr
))) {
833 cstate
->replay_owner
= NULL
;
838 if (so
->so_is_open_owner
)
839 release_last_closed_stateid(openowner(so
));
845 gen_sessionid(struct nfsd4_session
*ses
)
847 struct nfs4_client
*clp
= ses
->se_client
;
848 struct nfsd4_sessionid
*sid
;
850 sid
= (struct nfsd4_sessionid
*)ses
->se_sessionid
.data
;
851 sid
->clientid
= clp
->cl_clientid
;
852 sid
->sequence
= current_sessionid
++;
857 * The protocol defines ca_maxresponssize_cached to include the size of
858 * the rpc header, but all we need to cache is the data starting after
859 * the end of the initial SEQUENCE operation--the rest we regenerate
860 * each time. Therefore we can advertise a ca_maxresponssize_cached
861 * value that is the number of bytes in our cache plus a few additional
862 * bytes. In order to stay on the safe side, and not promise more than
863 * we can cache, those additional bytes must be the minimum possible: 24
864 * bytes of rpc header (xid through accept state, with AUTH_NULL
865 * verifier), 12 for the compound header (with zero-length tag), and 44
866 * for the SEQUENCE op response:
868 #define NFSD_MIN_HDR_SEQ_SZ (24 + 12 + 44)
871 free_session_slots(struct nfsd4_session
*ses
)
875 for (i
= 0; i
< ses
->se_fchannel
.maxreqs
; i
++)
876 kfree(ses
->se_slots
[i
]);
880 * We don't actually need to cache the rpc and session headers, so we
881 * can allocate a little less for each slot:
883 static inline u32
slot_bytes(struct nfsd4_channel_attrs
*ca
)
887 if (ca
->maxresp_cached
< NFSD_MIN_HDR_SEQ_SZ
)
890 size
= ca
->maxresp_cached
- NFSD_MIN_HDR_SEQ_SZ
;
891 return size
+ sizeof(struct nfsd4_slot
);
895 * XXX: If we run out of reserved DRC memory we could (up to a point)
896 * re-negotiate active sessions and reduce their slot usage to make
897 * room for new connections. For now we just fail the create session.
899 static u32
nfsd4_get_drc_mem(struct nfsd4_channel_attrs
*ca
)
901 u32 slotsize
= slot_bytes(ca
);
902 u32 num
= ca
->maxreqs
;
905 spin_lock(&nfsd_drc_lock
);
906 avail
= min((unsigned long)NFSD_MAX_MEM_PER_SESSION
,
907 nfsd_drc_max_mem
- nfsd_drc_mem_used
);
908 num
= min_t(int, num
, avail
/ slotsize
);
909 nfsd_drc_mem_used
+= num
* slotsize
;
910 spin_unlock(&nfsd_drc_lock
);
915 static void nfsd4_put_drc_mem(struct nfsd4_channel_attrs
*ca
)
917 int slotsize
= slot_bytes(ca
);
919 spin_lock(&nfsd_drc_lock
);
920 nfsd_drc_mem_used
-= slotsize
* ca
->maxreqs
;
921 spin_unlock(&nfsd_drc_lock
);
924 static struct nfsd4_session
*alloc_session(struct nfsd4_channel_attrs
*fattrs
,
925 struct nfsd4_channel_attrs
*battrs
)
927 int numslots
= fattrs
->maxreqs
;
928 int slotsize
= slot_bytes(fattrs
);
929 struct nfsd4_session
*new;
932 BUILD_BUG_ON(NFSD_MAX_SLOTS_PER_SESSION
* sizeof(struct nfsd4_slot
*)
933 + sizeof(struct nfsd4_session
) > PAGE_SIZE
);
934 mem
= numslots
* sizeof(struct nfsd4_slot
*);
936 new = kzalloc(sizeof(*new) + mem
, GFP_KERNEL
);
939 /* allocate each struct nfsd4_slot and data cache in one piece */
940 for (i
= 0; i
< numslots
; i
++) {
941 new->se_slots
[i
] = kzalloc(slotsize
, GFP_KERNEL
);
942 if (!new->se_slots
[i
])
946 memcpy(&new->se_fchannel
, fattrs
, sizeof(struct nfsd4_channel_attrs
));
947 memcpy(&new->se_bchannel
, battrs
, sizeof(struct nfsd4_channel_attrs
));
952 kfree(new->se_slots
[i
]);
957 static void free_conn(struct nfsd4_conn
*c
)
959 svc_xprt_put(c
->cn_xprt
);
963 static void nfsd4_conn_lost(struct svc_xpt_user
*u
)
965 struct nfsd4_conn
*c
= container_of(u
, struct nfsd4_conn
, cn_xpt_user
);
966 struct nfs4_client
*clp
= c
->cn_session
->se_client
;
968 spin_lock(&clp
->cl_lock
);
969 if (!list_empty(&c
->cn_persession
)) {
970 list_del(&c
->cn_persession
);
973 nfsd4_probe_callback(clp
);
974 spin_unlock(&clp
->cl_lock
);
977 static struct nfsd4_conn
*alloc_conn(struct svc_rqst
*rqstp
, u32 flags
)
979 struct nfsd4_conn
*conn
;
981 conn
= kmalloc(sizeof(struct nfsd4_conn
), GFP_KERNEL
);
984 svc_xprt_get(rqstp
->rq_xprt
);
985 conn
->cn_xprt
= rqstp
->rq_xprt
;
986 conn
->cn_flags
= flags
;
987 INIT_LIST_HEAD(&conn
->cn_xpt_user
.list
);
991 static void __nfsd4_hash_conn(struct nfsd4_conn
*conn
, struct nfsd4_session
*ses
)
993 conn
->cn_session
= ses
;
994 list_add(&conn
->cn_persession
, &ses
->se_conns
);
997 static void nfsd4_hash_conn(struct nfsd4_conn
*conn
, struct nfsd4_session
*ses
)
999 struct nfs4_client
*clp
= ses
->se_client
;
1001 spin_lock(&clp
->cl_lock
);
1002 __nfsd4_hash_conn(conn
, ses
);
1003 spin_unlock(&clp
->cl_lock
);
1006 static int nfsd4_register_conn(struct nfsd4_conn
*conn
)
1008 conn
->cn_xpt_user
.callback
= nfsd4_conn_lost
;
1009 return register_xpt_user(conn
->cn_xprt
, &conn
->cn_xpt_user
);
1012 static void nfsd4_init_conn(struct svc_rqst
*rqstp
, struct nfsd4_conn
*conn
, struct nfsd4_session
*ses
)
1016 nfsd4_hash_conn(conn
, ses
);
1017 ret
= nfsd4_register_conn(conn
);
1019 /* oops; xprt is already down: */
1020 nfsd4_conn_lost(&conn
->cn_xpt_user
);
1021 if (conn
->cn_flags
& NFS4_CDFC4_BACK
) {
1022 /* callback channel may be back up */
1023 nfsd4_probe_callback(ses
->se_client
);
1027 static struct nfsd4_conn
*alloc_conn_from_crses(struct svc_rqst
*rqstp
, struct nfsd4_create_session
*cses
)
1029 u32 dir
= NFS4_CDFC4_FORE
;
1031 if (cses
->flags
& SESSION4_BACK_CHAN
)
1032 dir
|= NFS4_CDFC4_BACK
;
1033 return alloc_conn(rqstp
, dir
);
1036 /* must be called under client_lock */
1037 static void nfsd4_del_conns(struct nfsd4_session
*s
)
1039 struct nfs4_client
*clp
= s
->se_client
;
1040 struct nfsd4_conn
*c
;
1042 spin_lock(&clp
->cl_lock
);
1043 while (!list_empty(&s
->se_conns
)) {
1044 c
= list_first_entry(&s
->se_conns
, struct nfsd4_conn
, cn_persession
);
1045 list_del_init(&c
->cn_persession
);
1046 spin_unlock(&clp
->cl_lock
);
1048 unregister_xpt_user(c
->cn_xprt
, &c
->cn_xpt_user
);
1051 spin_lock(&clp
->cl_lock
);
1053 spin_unlock(&clp
->cl_lock
);
1056 static void __free_session(struct nfsd4_session
*ses
)
1058 free_session_slots(ses
);
1062 static void free_session(struct nfsd4_session
*ses
)
1064 struct nfsd_net
*nn
= net_generic(ses
->se_client
->net
, nfsd_net_id
);
1066 lockdep_assert_held(&nn
->client_lock
);
1067 nfsd4_del_conns(ses
);
1068 nfsd4_put_drc_mem(&ses
->se_fchannel
);
1069 __free_session(ses
);
1072 static void init_session(struct svc_rqst
*rqstp
, struct nfsd4_session
*new, struct nfs4_client
*clp
, struct nfsd4_create_session
*cses
)
1075 struct nfsd_net
*nn
= net_generic(SVC_NET(rqstp
), nfsd_net_id
);
1077 new->se_client
= clp
;
1080 INIT_LIST_HEAD(&new->se_conns
);
1082 new->se_cb_seq_nr
= 1;
1083 new->se_flags
= cses
->flags
;
1084 new->se_cb_prog
= cses
->callback_prog
;
1085 new->se_cb_sec
= cses
->cb_sec
;
1086 atomic_set(&new->se_ref
, 0);
1087 idx
= hash_sessionid(&new->se_sessionid
);
1088 spin_lock(&nn
->client_lock
);
1089 list_add(&new->se_hash
, &nn
->sessionid_hashtbl
[idx
]);
1090 spin_lock(&clp
->cl_lock
);
1091 list_add(&new->se_perclnt
, &clp
->cl_sessions
);
1092 spin_unlock(&clp
->cl_lock
);
1093 spin_unlock(&nn
->client_lock
);
1095 if (cses
->flags
& SESSION4_BACK_CHAN
) {
1096 struct sockaddr
*sa
= svc_addr(rqstp
);
1098 * This is a little silly; with sessions there's no real
1099 * use for the callback address. Use the peer address
1100 * as a reasonable default for now, but consider fixing
1101 * the rpc client not to require an address in the
1104 rpc_copy_addr((struct sockaddr
*)&clp
->cl_cb_conn
.cb_addr
, sa
);
1105 clp
->cl_cb_conn
.cb_addrlen
= svc_addr_len(sa
);
1109 /* caller must hold client_lock */
1110 static struct nfsd4_session
*
1111 find_in_sessionid_hashtbl(struct nfs4_sessionid
*sessionid
, struct net
*net
)
1113 struct nfsd4_session
*elem
;
1115 struct nfsd_net
*nn
= net_generic(net
, nfsd_net_id
);
1117 dump_sessionid(__func__
, sessionid
);
1118 idx
= hash_sessionid(sessionid
);
1119 /* Search in the appropriate list */
1120 list_for_each_entry(elem
, &nn
->sessionid_hashtbl
[idx
], se_hash
) {
1121 if (!memcmp(elem
->se_sessionid
.data
, sessionid
->data
,
1122 NFS4_MAX_SESSIONID_LEN
)) {
1127 dprintk("%s: session not found\n", __func__
);
1131 /* caller must hold client_lock */
1133 unhash_session(struct nfsd4_session
*ses
)
1135 list_del(&ses
->se_hash
);
1136 spin_lock(&ses
->se_client
->cl_lock
);
1137 list_del(&ses
->se_perclnt
);
1138 spin_unlock(&ses
->se_client
->cl_lock
);
1141 /* SETCLIENTID and SETCLIENTID_CONFIRM Helper functions */
1143 STALE_CLIENTID(clientid_t
*clid
, struct nfsd_net
*nn
)
1145 if (clid
->cl_boot
== nn
->boot_time
)
1147 dprintk("NFSD stale clientid (%08x/%08x) boot_time %08lx\n",
1148 clid
->cl_boot
, clid
->cl_id
, nn
->boot_time
);
1153 * XXX Should we use a slab cache ?
1154 * This type of memory management is somewhat inefficient, but we use it
1155 * anyway since SETCLIENTID is not a common operation.
1157 static struct nfs4_client
*alloc_client(struct xdr_netobj name
)
1159 struct nfs4_client
*clp
;
1161 clp
= kzalloc(sizeof(struct nfs4_client
), GFP_KERNEL
);
1164 clp
->cl_name
.data
= kmemdup(name
.data
, name
.len
, GFP_KERNEL
);
1165 if (clp
->cl_name
.data
== NULL
) {
1169 clp
->cl_name
.len
= name
.len
;
1170 INIT_LIST_HEAD(&clp
->cl_sessions
);
1171 idr_init(&clp
->cl_stateids
);
1172 atomic_set(&clp
->cl_refcount
, 0);
1173 clp
->cl_cb_state
= NFSD4_CB_UNKNOWN
;
1174 INIT_LIST_HEAD(&clp
->cl_idhash
);
1175 INIT_LIST_HEAD(&clp
->cl_openowners
);
1176 INIT_LIST_HEAD(&clp
->cl_delegations
);
1177 INIT_LIST_HEAD(&clp
->cl_lru
);
1178 INIT_LIST_HEAD(&clp
->cl_callbacks
);
1179 INIT_LIST_HEAD(&clp
->cl_revoked
);
1180 spin_lock_init(&clp
->cl_lock
);
1181 rpc_init_wait_queue(&clp
->cl_cb_waitq
, "Backchannel slot table");
1186 free_client(struct nfs4_client
*clp
)
1188 struct nfsd_net __maybe_unused
*nn
= net_generic(clp
->net
, nfsd_net_id
);
1190 lockdep_assert_held(&nn
->client_lock
);
1191 while (!list_empty(&clp
->cl_sessions
)) {
1192 struct nfsd4_session
*ses
;
1193 ses
= list_entry(clp
->cl_sessions
.next
, struct nfsd4_session
,
1195 list_del(&ses
->se_perclnt
);
1196 WARN_ON_ONCE(atomic_read(&ses
->se_ref
));
1199 rpc_destroy_wait_queue(&clp
->cl_cb_waitq
);
1200 free_svc_cred(&clp
->cl_cred
);
1201 kfree(clp
->cl_name
.data
);
1202 idr_destroy(&clp
->cl_stateids
);
1206 /* must be called under the client_lock */
1208 unhash_client_locked(struct nfs4_client
*clp
)
1210 struct nfsd4_session
*ses
;
1212 list_del(&clp
->cl_lru
);
1213 spin_lock(&clp
->cl_lock
);
1214 list_for_each_entry(ses
, &clp
->cl_sessions
, se_perclnt
)
1215 list_del_init(&ses
->se_hash
);
1216 spin_unlock(&clp
->cl_lock
);
1220 destroy_client(struct nfs4_client
*clp
)
1222 struct nfs4_openowner
*oo
;
1223 struct nfs4_delegation
*dp
;
1224 struct list_head reaplist
;
1225 struct nfsd_net
*nn
= net_generic(clp
->net
, nfsd_net_id
);
1227 INIT_LIST_HEAD(&reaplist
);
1228 spin_lock(&state_lock
);
1229 while (!list_empty(&clp
->cl_delegations
)) {
1230 dp
= list_entry(clp
->cl_delegations
.next
, struct nfs4_delegation
, dl_perclnt
);
1231 list_del_init(&dp
->dl_perclnt
);
1232 list_move(&dp
->dl_recall_lru
, &reaplist
);
1234 spin_unlock(&state_lock
);
1235 while (!list_empty(&reaplist
)) {
1236 dp
= list_entry(reaplist
.next
, struct nfs4_delegation
, dl_recall_lru
);
1237 destroy_delegation(dp
);
1239 list_splice_init(&clp
->cl_revoked
, &reaplist
);
1240 while (!list_empty(&reaplist
)) {
1241 dp
= list_entry(reaplist
.next
, struct nfs4_delegation
, dl_recall_lru
);
1242 destroy_revoked_delegation(dp
);
1244 while (!list_empty(&clp
->cl_openowners
)) {
1245 oo
= list_entry(clp
->cl_openowners
.next
, struct nfs4_openowner
, oo_perclient
);
1246 release_openowner(oo
);
1248 nfsd4_shutdown_callback(clp
);
1249 if (clp
->cl_cb_conn
.cb_xprt
)
1250 svc_xprt_put(clp
->cl_cb_conn
.cb_xprt
);
1251 list_del(&clp
->cl_idhash
);
1252 if (test_bit(NFSD4_CLIENT_CONFIRMED
, &clp
->cl_flags
))
1253 rb_erase(&clp
->cl_namenode
, &nn
->conf_name_tree
);
1255 rb_erase(&clp
->cl_namenode
, &nn
->unconf_name_tree
);
1256 spin_lock(&nn
->client_lock
);
1257 unhash_client_locked(clp
);
1258 WARN_ON_ONCE(atomic_read(&clp
->cl_refcount
));
1260 spin_unlock(&nn
->client_lock
);
1263 static void expire_client(struct nfs4_client
*clp
)
1265 nfsd4_client_record_remove(clp
);
1266 destroy_client(clp
);
1269 static void copy_verf(struct nfs4_client
*target
, nfs4_verifier
*source
)
1271 memcpy(target
->cl_verifier
.data
, source
->data
,
1272 sizeof(target
->cl_verifier
.data
));
1275 static void copy_clid(struct nfs4_client
*target
, struct nfs4_client
*source
)
1277 target
->cl_clientid
.cl_boot
= source
->cl_clientid
.cl_boot
;
1278 target
->cl_clientid
.cl_id
= source
->cl_clientid
.cl_id
;
1281 static int copy_cred(struct svc_cred
*target
, struct svc_cred
*source
)
1283 if (source
->cr_principal
) {
1284 target
->cr_principal
=
1285 kstrdup(source
->cr_principal
, GFP_KERNEL
);
1286 if (target
->cr_principal
== NULL
)
1289 target
->cr_principal
= NULL
;
1290 target
->cr_flavor
= source
->cr_flavor
;
1291 target
->cr_uid
= source
->cr_uid
;
1292 target
->cr_gid
= source
->cr_gid
;
1293 target
->cr_group_info
= source
->cr_group_info
;
1294 get_group_info(target
->cr_group_info
);
1295 target
->cr_gss_mech
= source
->cr_gss_mech
;
1296 if (source
->cr_gss_mech
)
1297 gss_mech_get(source
->cr_gss_mech
);
1302 compare_blob(const struct xdr_netobj
*o1
, const struct xdr_netobj
*o2
)
1306 res
= o1
->len
- o2
->len
;
1309 return (long long)memcmp(o1
->data
, o2
->data
, o1
->len
);
1312 static int same_name(const char *n1
, const char *n2
)
1314 return 0 == memcmp(n1
, n2
, HEXDIR_LEN
);
1318 same_verf(nfs4_verifier
*v1
, nfs4_verifier
*v2
)
1320 return 0 == memcmp(v1
->data
, v2
->data
, sizeof(v1
->data
));
1324 same_clid(clientid_t
*cl1
, clientid_t
*cl2
)
1326 return (cl1
->cl_boot
== cl2
->cl_boot
) && (cl1
->cl_id
== cl2
->cl_id
);
1329 static bool groups_equal(struct group_info
*g1
, struct group_info
*g2
)
1333 if (g1
->ngroups
!= g2
->ngroups
)
1335 for (i
=0; i
<g1
->ngroups
; i
++)
1336 if (!gid_eq(GROUP_AT(g1
, i
), GROUP_AT(g2
, i
)))
1342 * RFC 3530 language requires clid_inuse be returned when the
1343 * "principal" associated with a requests differs from that previously
1344 * used. We use uid, gid's, and gss principal string as our best
1345 * approximation. We also don't want to allow non-gss use of a client
1346 * established using gss: in theory cr_principal should catch that
1347 * change, but in practice cr_principal can be null even in the gss case
1348 * since gssd doesn't always pass down a principal string.
1350 static bool is_gss_cred(struct svc_cred
*cr
)
1352 /* Is cr_flavor one of the gss "pseudoflavors"?: */
1353 return (cr
->cr_flavor
> RPC_AUTH_MAXFLAVOR
);
1358 same_creds(struct svc_cred
*cr1
, struct svc_cred
*cr2
)
1360 if ((is_gss_cred(cr1
) != is_gss_cred(cr2
))
1361 || (!uid_eq(cr1
->cr_uid
, cr2
->cr_uid
))
1362 || (!gid_eq(cr1
->cr_gid
, cr2
->cr_gid
))
1363 || !groups_equal(cr1
->cr_group_info
, cr2
->cr_group_info
))
1365 if (cr1
->cr_principal
== cr2
->cr_principal
)
1367 if (!cr1
->cr_principal
|| !cr2
->cr_principal
)
1369 return 0 == strcmp(cr1
->cr_principal
, cr2
->cr_principal
);
1372 static bool svc_rqst_integrity_protected(struct svc_rqst
*rqstp
)
1374 struct svc_cred
*cr
= &rqstp
->rq_cred
;
1377 if (!cr
->cr_gss_mech
)
1379 service
= gss_pseudoflavor_to_service(cr
->cr_gss_mech
, cr
->cr_flavor
);
1380 return service
== RPC_GSS_SVC_INTEGRITY
||
1381 service
== RPC_GSS_SVC_PRIVACY
;
1384 static bool mach_creds_match(struct nfs4_client
*cl
, struct svc_rqst
*rqstp
)
1386 struct svc_cred
*cr
= &rqstp
->rq_cred
;
1388 if (!cl
->cl_mach_cred
)
1390 if (cl
->cl_cred
.cr_gss_mech
!= cr
->cr_gss_mech
)
1392 if (!svc_rqst_integrity_protected(rqstp
))
1394 if (!cr
->cr_principal
)
1396 return 0 == strcmp(cl
->cl_cred
.cr_principal
, cr
->cr_principal
);
1399 static void gen_clid(struct nfs4_client
*clp
, struct nfsd_net
*nn
)
1401 static u32 current_clientid
= 1;
1403 clp
->cl_clientid
.cl_boot
= nn
->boot_time
;
1404 clp
->cl_clientid
.cl_id
= current_clientid
++;
1407 static void gen_confirm(struct nfs4_client
*clp
)
1413 * This is opaque to client, so no need to byte-swap. Use
1414 * __force to keep sparse happy
1416 verf
[0] = (__force __be32
)get_seconds();
1417 verf
[1] = (__force __be32
)i
++;
1418 memcpy(clp
->cl_confirm
.data
, verf
, sizeof(clp
->cl_confirm
.data
));
1421 static struct nfs4_stid
*find_stateid(struct nfs4_client
*cl
, stateid_t
*t
)
1423 struct nfs4_stid
*ret
;
1425 ret
= idr_find(&cl
->cl_stateids
, t
->si_opaque
.so_id
);
1426 if (!ret
|| !ret
->sc_type
)
1431 static struct nfs4_stid
*find_stateid_by_type(struct nfs4_client
*cl
, stateid_t
*t
, char typemask
)
1433 struct nfs4_stid
*s
;
1435 s
= find_stateid(cl
, t
);
1438 if (typemask
& s
->sc_type
)
1443 static struct nfs4_client
*create_client(struct xdr_netobj name
,
1444 struct svc_rqst
*rqstp
, nfs4_verifier
*verf
)
1446 struct nfs4_client
*clp
;
1447 struct sockaddr
*sa
= svc_addr(rqstp
);
1449 struct net
*net
= SVC_NET(rqstp
);
1450 struct nfsd_net
*nn
= net_generic(net
, nfsd_net_id
);
1452 clp
= alloc_client(name
);
1456 ret
= copy_cred(&clp
->cl_cred
, &rqstp
->rq_cred
);
1458 spin_lock(&nn
->client_lock
);
1460 spin_unlock(&nn
->client_lock
);
1463 nfsd4_init_callback(&clp
->cl_cb_null
);
1464 clp
->cl_time
= get_seconds();
1465 clear_bit(0, &clp
->cl_cb_slot_busy
);
1466 copy_verf(clp
, verf
);
1467 rpc_copy_addr((struct sockaddr
*) &clp
->cl_addr
, sa
);
1469 clp
->cl_cb_session
= NULL
;
1475 add_clp_to_name_tree(struct nfs4_client
*new_clp
, struct rb_root
*root
)
1477 struct rb_node
**new = &(root
->rb_node
), *parent
= NULL
;
1478 struct nfs4_client
*clp
;
1481 clp
= rb_entry(*new, struct nfs4_client
, cl_namenode
);
1484 if (compare_blob(&clp
->cl_name
, &new_clp
->cl_name
) > 0)
1485 new = &((*new)->rb_left
);
1487 new = &((*new)->rb_right
);
1490 rb_link_node(&new_clp
->cl_namenode
, parent
, new);
1491 rb_insert_color(&new_clp
->cl_namenode
, root
);
1494 static struct nfs4_client
*
1495 find_clp_in_name_tree(struct xdr_netobj
*name
, struct rb_root
*root
)
1498 struct rb_node
*node
= root
->rb_node
;
1499 struct nfs4_client
*clp
;
1502 clp
= rb_entry(node
, struct nfs4_client
, cl_namenode
);
1503 cmp
= compare_blob(&clp
->cl_name
, name
);
1505 node
= node
->rb_left
;
1507 node
= node
->rb_right
;
1515 add_to_unconfirmed(struct nfs4_client
*clp
)
1517 unsigned int idhashval
;
1518 struct nfsd_net
*nn
= net_generic(clp
->net
, nfsd_net_id
);
1520 clear_bit(NFSD4_CLIENT_CONFIRMED
, &clp
->cl_flags
);
1521 add_clp_to_name_tree(clp
, &nn
->unconf_name_tree
);
1522 idhashval
= clientid_hashval(clp
->cl_clientid
.cl_id
);
1523 list_add(&clp
->cl_idhash
, &nn
->unconf_id_hashtbl
[idhashval
]);
1528 move_to_confirmed(struct nfs4_client
*clp
)
1530 unsigned int idhashval
= clientid_hashval(clp
->cl_clientid
.cl_id
);
1531 struct nfsd_net
*nn
= net_generic(clp
->net
, nfsd_net_id
);
1533 dprintk("NFSD: move_to_confirm nfs4_client %p\n", clp
);
1534 list_move(&clp
->cl_idhash
, &nn
->conf_id_hashtbl
[idhashval
]);
1535 rb_erase(&clp
->cl_namenode
, &nn
->unconf_name_tree
);
1536 add_clp_to_name_tree(clp
, &nn
->conf_name_tree
);
1537 set_bit(NFSD4_CLIENT_CONFIRMED
, &clp
->cl_flags
);
1541 static struct nfs4_client
*
1542 find_client_in_id_table(struct list_head
*tbl
, clientid_t
*clid
, bool sessions
)
1544 struct nfs4_client
*clp
;
1545 unsigned int idhashval
= clientid_hashval(clid
->cl_id
);
1547 list_for_each_entry(clp
, &tbl
[idhashval
], cl_idhash
) {
1548 if (same_clid(&clp
->cl_clientid
, clid
)) {
1549 if ((bool)clp
->cl_minorversion
!= sessions
)
1558 static struct nfs4_client
*
1559 find_confirmed_client(clientid_t
*clid
, bool sessions
, struct nfsd_net
*nn
)
1561 struct list_head
*tbl
= nn
->conf_id_hashtbl
;
1563 return find_client_in_id_table(tbl
, clid
, sessions
);
1566 static struct nfs4_client
*
1567 find_unconfirmed_client(clientid_t
*clid
, bool sessions
, struct nfsd_net
*nn
)
1569 struct list_head
*tbl
= nn
->unconf_id_hashtbl
;
1571 return find_client_in_id_table(tbl
, clid
, sessions
);
1574 static bool clp_used_exchangeid(struct nfs4_client
*clp
)
1576 return clp
->cl_exchange_flags
!= 0;
1579 static struct nfs4_client
*
1580 find_confirmed_client_by_name(struct xdr_netobj
*name
, struct nfsd_net
*nn
)
1582 return find_clp_in_name_tree(name
, &nn
->conf_name_tree
);
1585 static struct nfs4_client
*
1586 find_unconfirmed_client_by_name(struct xdr_netobj
*name
, struct nfsd_net
*nn
)
1588 return find_clp_in_name_tree(name
, &nn
->unconf_name_tree
);
1592 gen_callback(struct nfs4_client
*clp
, struct nfsd4_setclientid
*se
, struct svc_rqst
*rqstp
)
1594 struct nfs4_cb_conn
*conn
= &clp
->cl_cb_conn
;
1595 struct sockaddr
*sa
= svc_addr(rqstp
);
1596 u32 scopeid
= rpc_get_scope_id(sa
);
1597 unsigned short expected_family
;
1599 /* Currently, we only support tcp and tcp6 for the callback channel */
1600 if (se
->se_callback_netid_len
== 3 &&
1601 !memcmp(se
->se_callback_netid_val
, "tcp", 3))
1602 expected_family
= AF_INET
;
1603 else if (se
->se_callback_netid_len
== 4 &&
1604 !memcmp(se
->se_callback_netid_val
, "tcp6", 4))
1605 expected_family
= AF_INET6
;
1609 conn
->cb_addrlen
= rpc_uaddr2sockaddr(clp
->net
, se
->se_callback_addr_val
,
1610 se
->se_callback_addr_len
,
1611 (struct sockaddr
*)&conn
->cb_addr
,
1612 sizeof(conn
->cb_addr
));
1614 if (!conn
->cb_addrlen
|| conn
->cb_addr
.ss_family
!= expected_family
)
1617 if (conn
->cb_addr
.ss_family
== AF_INET6
)
1618 ((struct sockaddr_in6
*)&conn
->cb_addr
)->sin6_scope_id
= scopeid
;
1620 conn
->cb_prog
= se
->se_callback_prog
;
1621 conn
->cb_ident
= se
->se_callback_ident
;
1622 memcpy(&conn
->cb_saddr
, &rqstp
->rq_daddr
, rqstp
->rq_daddrlen
);
1625 conn
->cb_addr
.ss_family
= AF_UNSPEC
;
1626 conn
->cb_addrlen
= 0;
1627 dprintk(KERN_INFO
"NFSD: this client (clientid %08x/%08x) "
1628 "will not receive delegations\n",
1629 clp
->cl_clientid
.cl_boot
, clp
->cl_clientid
.cl_id
);
1635 * Cache a reply. nfsd4_check_resp_size() has bounded the cache size.
1638 nfsd4_store_cache_entry(struct nfsd4_compoundres
*resp
)
1640 struct xdr_buf
*buf
= resp
->xdr
.buf
;
1641 struct nfsd4_slot
*slot
= resp
->cstate
.slot
;
1644 dprintk("--> %s slot %p\n", __func__
, slot
);
1646 slot
->sl_opcnt
= resp
->opcnt
;
1647 slot
->sl_status
= resp
->cstate
.status
;
1649 slot
->sl_flags
|= NFSD4_SLOT_INITIALIZED
;
1650 if (nfsd4_not_cached(resp
)) {
1651 slot
->sl_datalen
= 0;
1654 base
= resp
->cstate
.data_offset
;
1655 slot
->sl_datalen
= buf
->len
- base
;
1656 if (read_bytes_from_xdr_buf(buf
, base
, slot
->sl_data
, slot
->sl_datalen
))
1657 WARN("%s: sessions DRC could not cache compound\n", __func__
);
1662 * Encode the replay sequence operation from the slot values.
1663 * If cachethis is FALSE encode the uncached rep error on the next
1664 * operation which sets resp->p and increments resp->opcnt for
1665 * nfs4svc_encode_compoundres.
1669 nfsd4_enc_sequence_replay(struct nfsd4_compoundargs
*args
,
1670 struct nfsd4_compoundres
*resp
)
1672 struct nfsd4_op
*op
;
1673 struct nfsd4_slot
*slot
= resp
->cstate
.slot
;
1675 /* Encode the replayed sequence operation */
1676 op
= &args
->ops
[resp
->opcnt
- 1];
1677 nfsd4_encode_operation(resp
, op
);
1679 /* Return nfserr_retry_uncached_rep in next operation. */
1680 if (args
->opcnt
> 1 && !(slot
->sl_flags
& NFSD4_SLOT_CACHETHIS
)) {
1681 op
= &args
->ops
[resp
->opcnt
++];
1682 op
->status
= nfserr_retry_uncached_rep
;
1683 nfsd4_encode_operation(resp
, op
);
1689 * The sequence operation is not cached because we can use the slot and
1693 nfsd4_replay_cache_entry(struct nfsd4_compoundres
*resp
,
1694 struct nfsd4_sequence
*seq
)
1696 struct nfsd4_slot
*slot
= resp
->cstate
.slot
;
1697 struct xdr_stream
*xdr
= &resp
->xdr
;
1701 dprintk("--> %s slot %p\n", __func__
, slot
);
1703 status
= nfsd4_enc_sequence_replay(resp
->rqstp
->rq_argp
, resp
);
1707 p
= xdr_reserve_space(xdr
, slot
->sl_datalen
);
1710 return nfserr_serverfault
;
1712 xdr_encode_opaque_fixed(p
, slot
->sl_data
, slot
->sl_datalen
);
1713 xdr_commit_encode(xdr
);
1715 resp
->opcnt
= slot
->sl_opcnt
;
1716 return slot
->sl_status
;
1720 * Set the exchange_id flags returned by the server.
1723 nfsd4_set_ex_flags(struct nfs4_client
*new, struct nfsd4_exchange_id
*clid
)
1725 /* pNFS is not supported */
1726 new->cl_exchange_flags
|= EXCHGID4_FLAG_USE_NON_PNFS
;
1728 /* Referrals are supported, Migration is not. */
1729 new->cl_exchange_flags
|= EXCHGID4_FLAG_SUPP_MOVED_REFER
;
1731 /* set the wire flags to return to client. */
1732 clid
->flags
= new->cl_exchange_flags
;
1735 static bool client_has_state(struct nfs4_client
*clp
)
1738 * Note clp->cl_openowners check isn't quite right: there's no
1739 * need to count owners without stateid's.
1741 * Also note we should probably be using this in 4.0 case too.
1743 return !list_empty(&clp
->cl_openowners
)
1744 || !list_empty(&clp
->cl_delegations
)
1745 || !list_empty(&clp
->cl_sessions
);
1749 nfsd4_exchange_id(struct svc_rqst
*rqstp
,
1750 struct nfsd4_compound_state
*cstate
,
1751 struct nfsd4_exchange_id
*exid
)
1753 struct nfs4_client
*unconf
, *conf
, *new;
1755 char addr_str
[INET6_ADDRSTRLEN
];
1756 nfs4_verifier verf
= exid
->verifier
;
1757 struct sockaddr
*sa
= svc_addr(rqstp
);
1758 bool update
= exid
->flags
& EXCHGID4_FLAG_UPD_CONFIRMED_REC_A
;
1759 struct nfsd_net
*nn
= net_generic(SVC_NET(rqstp
), nfsd_net_id
);
1761 rpc_ntop(sa
, addr_str
, sizeof(addr_str
));
1762 dprintk("%s rqstp=%p exid=%p clname.len=%u clname.data=%p "
1763 "ip_addr=%s flags %x, spa_how %d\n",
1764 __func__
, rqstp
, exid
, exid
->clname
.len
, exid
->clname
.data
,
1765 addr_str
, exid
->flags
, exid
->spa_how
);
1767 if (exid
->flags
& ~EXCHGID4_FLAG_MASK_A
)
1768 return nfserr_inval
;
1770 switch (exid
->spa_how
) {
1772 if (!svc_rqst_integrity_protected(rqstp
))
1773 return nfserr_inval
;
1776 default: /* checked by xdr code */
1779 return nfserr_encr_alg_unsupp
;
1782 /* Cases below refer to rfc 5661 section 18.35.4: */
1784 conf
= find_confirmed_client_by_name(&exid
->clname
, nn
);
1786 bool creds_match
= same_creds(&conf
->cl_cred
, &rqstp
->rq_cred
);
1787 bool verfs_match
= same_verf(&verf
, &conf
->cl_verifier
);
1790 if (!clp_used_exchangeid(conf
)) { /* buggy client */
1791 status
= nfserr_inval
;
1794 if (!mach_creds_match(conf
, rqstp
)) {
1795 status
= nfserr_wrong_cred
;
1798 if (!creds_match
) { /* case 9 */
1799 status
= nfserr_perm
;
1802 if (!verfs_match
) { /* case 8 */
1803 status
= nfserr_not_same
;
1807 exid
->flags
|= EXCHGID4_FLAG_CONFIRMED_R
;
1811 if (!creds_match
) { /* case 3 */
1812 if (client_has_state(conf
)) {
1813 status
= nfserr_clid_inuse
;
1816 expire_client(conf
);
1819 if (verfs_match
) { /* case 2 */
1820 conf
->cl_exchange_flags
|= EXCHGID4_FLAG_CONFIRMED_R
;
1824 /* case 5, client reboot */
1828 if (update
) { /* case 7 */
1829 status
= nfserr_noent
;
1833 unconf
= find_unconfirmed_client_by_name(&exid
->clname
, nn
);
1834 if (unconf
) /* case 4, possible retry or client restart */
1835 expire_client(unconf
);
1837 /* case 1 (normal case) */
1839 new = create_client(exid
->clname
, rqstp
, &verf
);
1841 status
= nfserr_jukebox
;
1844 new->cl_minorversion
= cstate
->minorversion
;
1845 new->cl_mach_cred
= (exid
->spa_how
== SP4_MACH_CRED
);
1848 add_to_unconfirmed(new);
1850 exid
->clientid
.cl_boot
= new->cl_clientid
.cl_boot
;
1851 exid
->clientid
.cl_id
= new->cl_clientid
.cl_id
;
1853 exid
->seqid
= new->cl_cs_slot
.sl_seqid
+ 1;
1854 nfsd4_set_ex_flags(new, exid
);
1856 dprintk("nfsd4_exchange_id seqid %d flags %x\n",
1857 new->cl_cs_slot
.sl_seqid
, new->cl_exchange_flags
);
1861 nfs4_unlock_state();
1866 check_slot_seqid(u32 seqid
, u32 slot_seqid
, int slot_inuse
)
1868 dprintk("%s enter. seqid %d slot_seqid %d\n", __func__
, seqid
,
1871 /* The slot is in use, and no response has been sent. */
1873 if (seqid
== slot_seqid
)
1874 return nfserr_jukebox
;
1876 return nfserr_seq_misordered
;
1878 /* Note unsigned 32-bit arithmetic handles wraparound: */
1879 if (likely(seqid
== slot_seqid
+ 1))
1881 if (seqid
== slot_seqid
)
1882 return nfserr_replay_cache
;
1883 return nfserr_seq_misordered
;
1887 * Cache the create session result into the create session single DRC
1888 * slot cache by saving the xdr structure. sl_seqid has been set.
1889 * Do this for solo or embedded create session operations.
1892 nfsd4_cache_create_session(struct nfsd4_create_session
*cr_ses
,
1893 struct nfsd4_clid_slot
*slot
, __be32 nfserr
)
1895 slot
->sl_status
= nfserr
;
1896 memcpy(&slot
->sl_cr_ses
, cr_ses
, sizeof(*cr_ses
));
1900 nfsd4_replay_create_session(struct nfsd4_create_session
*cr_ses
,
1901 struct nfsd4_clid_slot
*slot
)
1903 memcpy(cr_ses
, &slot
->sl_cr_ses
, sizeof(*cr_ses
));
1904 return slot
->sl_status
;
1907 #define NFSD_MIN_REQ_HDR_SEQ_SZ ((\
1908 2 * 2 + /* credential,verifier: AUTH_NULL, length 0 */ \
1909 1 + /* MIN tag is length with zero, only length */ \
1910 3 + /* version, opcount, opcode */ \
1911 XDR_QUADLEN(NFS4_MAX_SESSIONID_LEN) + \
1912 /* seqid, slotID, slotID, cache */ \
1913 4 ) * sizeof(__be32))
1915 #define NFSD_MIN_RESP_HDR_SEQ_SZ ((\
1916 2 + /* verifier: AUTH_NULL, length 0 */\
1918 1 + /* MIN tag is length with zero, only length */ \
1919 3 + /* opcount, opcode, opstatus*/ \
1920 XDR_QUADLEN(NFS4_MAX_SESSIONID_LEN) + \
1921 /* seqid, slotID, slotID, slotID, status */ \
1922 5 ) * sizeof(__be32))
1924 static __be32
check_forechannel_attrs(struct nfsd4_channel_attrs
*ca
, struct nfsd_net
*nn
)
1926 u32 maxrpc
= nn
->nfsd_serv
->sv_max_mesg
;
1928 if (ca
->maxreq_sz
< NFSD_MIN_REQ_HDR_SEQ_SZ
)
1929 return nfserr_toosmall
;
1930 if (ca
->maxresp_sz
< NFSD_MIN_RESP_HDR_SEQ_SZ
)
1931 return nfserr_toosmall
;
1932 ca
->headerpadsz
= 0;
1933 ca
->maxreq_sz
= min_t(u32
, ca
->maxreq_sz
, maxrpc
);
1934 ca
->maxresp_sz
= min_t(u32
, ca
->maxresp_sz
, maxrpc
);
1935 ca
->maxops
= min_t(u32
, ca
->maxops
, NFSD_MAX_OPS_PER_COMPOUND
);
1936 ca
->maxresp_cached
= min_t(u32
, ca
->maxresp_cached
,
1937 NFSD_SLOT_CACHE_SIZE
+ NFSD_MIN_HDR_SEQ_SZ
);
1938 ca
->maxreqs
= min_t(u32
, ca
->maxreqs
, NFSD_MAX_SLOTS_PER_SESSION
);
1940 * Note decreasing slot size below client's request may make it
1941 * difficult for client to function correctly, whereas
1942 * decreasing the number of slots will (just?) affect
1943 * performance. When short on memory we therefore prefer to
1944 * decrease number of slots instead of their size. Clients that
1945 * request larger slots than they need will get poor results:
1947 ca
->maxreqs
= nfsd4_get_drc_mem(ca
);
1949 return nfserr_jukebox
;
1954 #define NFSD_CB_MAX_REQ_SZ ((NFS4_enc_cb_recall_sz + \
1955 RPC_MAX_HEADER_WITH_AUTH) * sizeof(__be32))
1956 #define NFSD_CB_MAX_RESP_SZ ((NFS4_dec_cb_recall_sz + \
1957 RPC_MAX_REPHEADER_WITH_AUTH) * sizeof(__be32))
1959 static __be32
check_backchannel_attrs(struct nfsd4_channel_attrs
*ca
)
1961 ca
->headerpadsz
= 0;
1964 * These RPC_MAX_HEADER macros are overkill, especially since we
1965 * don't even do gss on the backchannel yet. But this is still
1966 * less than 1k. Tighten up this estimate in the unlikely event
1967 * it turns out to be a problem for some client:
1969 if (ca
->maxreq_sz
< NFSD_CB_MAX_REQ_SZ
)
1970 return nfserr_toosmall
;
1971 if (ca
->maxresp_sz
< NFSD_CB_MAX_RESP_SZ
)
1972 return nfserr_toosmall
;
1973 ca
->maxresp_cached
= 0;
1975 return nfserr_toosmall
;
1980 static __be32
nfsd4_check_cb_sec(struct nfsd4_cb_sec
*cbs
)
1982 switch (cbs
->flavor
) {
1988 * GSS case: the spec doesn't allow us to return this
1989 * error. But it also doesn't allow us not to support
1991 * I'd rather this fail hard than return some error the
1992 * client might think it can already handle:
1994 return nfserr_encr_alg_unsupp
;
1999 nfsd4_create_session(struct svc_rqst
*rqstp
,
2000 struct nfsd4_compound_state
*cstate
,
2001 struct nfsd4_create_session
*cr_ses
)
2003 struct sockaddr
*sa
= svc_addr(rqstp
);
2004 struct nfs4_client
*conf
, *unconf
;
2005 struct nfsd4_session
*new;
2006 struct nfsd4_conn
*conn
;
2007 struct nfsd4_clid_slot
*cs_slot
= NULL
;
2009 struct nfsd_net
*nn
= net_generic(SVC_NET(rqstp
), nfsd_net_id
);
2011 if (cr_ses
->flags
& ~SESSION4_FLAG_MASK_A
)
2012 return nfserr_inval
;
2013 status
= nfsd4_check_cb_sec(&cr_ses
->cb_sec
);
2016 status
= check_forechannel_attrs(&cr_ses
->fore_channel
, nn
);
2019 status
= check_backchannel_attrs(&cr_ses
->back_channel
);
2021 goto out_release_drc_mem
;
2022 status
= nfserr_jukebox
;
2023 new = alloc_session(&cr_ses
->fore_channel
, &cr_ses
->back_channel
);
2025 goto out_release_drc_mem
;
2026 conn
= alloc_conn_from_crses(rqstp
, cr_ses
);
2028 goto out_free_session
;
2031 unconf
= find_unconfirmed_client(&cr_ses
->clientid
, true, nn
);
2032 conf
= find_confirmed_client(&cr_ses
->clientid
, true, nn
);
2033 WARN_ON_ONCE(conf
&& unconf
);
2036 status
= nfserr_wrong_cred
;
2037 if (!mach_creds_match(conf
, rqstp
))
2039 cs_slot
= &conf
->cl_cs_slot
;
2040 status
= check_slot_seqid(cr_ses
->seqid
, cs_slot
->sl_seqid
, 0);
2041 if (status
== nfserr_replay_cache
) {
2042 status
= nfsd4_replay_create_session(cr_ses
, cs_slot
);
2044 } else if (cr_ses
->seqid
!= cs_slot
->sl_seqid
+ 1) {
2045 status
= nfserr_seq_misordered
;
2048 } else if (unconf
) {
2049 struct nfs4_client
*old
;
2050 if (!same_creds(&unconf
->cl_cred
, &rqstp
->rq_cred
) ||
2051 !rpc_cmp_addr(sa
, (struct sockaddr
*) &unconf
->cl_addr
)) {
2052 status
= nfserr_clid_inuse
;
2055 status
= nfserr_wrong_cred
;
2056 if (!mach_creds_match(unconf
, rqstp
))
2058 cs_slot
= &unconf
->cl_cs_slot
;
2059 status
= check_slot_seqid(cr_ses
->seqid
, cs_slot
->sl_seqid
, 0);
2061 /* an unconfirmed replay returns misordered */
2062 status
= nfserr_seq_misordered
;
2065 old
= find_confirmed_client_by_name(&unconf
->cl_name
, nn
);
2067 status
= mark_client_expired(old
);
2072 move_to_confirmed(unconf
);
2075 status
= nfserr_stale_clientid
;
2080 * We do not support RDMA or persistent sessions
2082 cr_ses
->flags
&= ~SESSION4_PERSIST
;
2083 cr_ses
->flags
&= ~SESSION4_RDMA
;
2085 init_session(rqstp
, new, conf
, cr_ses
);
2086 nfsd4_init_conn(rqstp
, conn
, new);
2088 memcpy(cr_ses
->sessionid
.data
, new->se_sessionid
.data
,
2089 NFS4_MAX_SESSIONID_LEN
);
2090 cs_slot
->sl_seqid
++;
2091 cr_ses
->seqid
= cs_slot
->sl_seqid
;
2093 /* cache solo and embedded create sessions under the state lock */
2094 nfsd4_cache_create_session(cr_ses
, cs_slot
, status
);
2095 nfs4_unlock_state();
2098 nfs4_unlock_state();
2101 __free_session(new);
2102 out_release_drc_mem
:
2103 nfsd4_put_drc_mem(&cr_ses
->fore_channel
);
2107 static __be32
nfsd4_map_bcts_dir(u32
*dir
)
2110 case NFS4_CDFC4_FORE
:
2111 case NFS4_CDFC4_BACK
:
2113 case NFS4_CDFC4_FORE_OR_BOTH
:
2114 case NFS4_CDFC4_BACK_OR_BOTH
:
2115 *dir
= NFS4_CDFC4_BOTH
;
2118 return nfserr_inval
;
2121 __be32
nfsd4_backchannel_ctl(struct svc_rqst
*rqstp
, struct nfsd4_compound_state
*cstate
, struct nfsd4_backchannel_ctl
*bc
)
2123 struct nfsd4_session
*session
= cstate
->session
;
2124 struct nfsd_net
*nn
= net_generic(SVC_NET(rqstp
), nfsd_net_id
);
2127 status
= nfsd4_check_cb_sec(&bc
->bc_cb_sec
);
2130 spin_lock(&nn
->client_lock
);
2131 session
->se_cb_prog
= bc
->bc_cb_program
;
2132 session
->se_cb_sec
= bc
->bc_cb_sec
;
2133 spin_unlock(&nn
->client_lock
);
2135 nfsd4_probe_callback(session
->se_client
);
2140 __be32
nfsd4_bind_conn_to_session(struct svc_rqst
*rqstp
,
2141 struct nfsd4_compound_state
*cstate
,
2142 struct nfsd4_bind_conn_to_session
*bcts
)
2145 struct nfsd4_conn
*conn
;
2146 struct nfsd4_session
*session
;
2147 struct nfsd_net
*nn
= net_generic(SVC_NET(rqstp
), nfsd_net_id
);
2149 if (!nfsd4_last_compound_op(rqstp
))
2150 return nfserr_not_only_op
;
2152 spin_lock(&nn
->client_lock
);
2153 session
= find_in_sessionid_hashtbl(&bcts
->sessionid
, SVC_NET(rqstp
));
2154 spin_unlock(&nn
->client_lock
);
2155 status
= nfserr_badsession
;
2158 status
= nfserr_wrong_cred
;
2159 if (!mach_creds_match(session
->se_client
, rqstp
))
2161 status
= nfsd4_map_bcts_dir(&bcts
->dir
);
2164 conn
= alloc_conn(rqstp
, bcts
->dir
);
2165 status
= nfserr_jukebox
;
2168 nfsd4_init_conn(rqstp
, conn
, session
);
2171 nfs4_unlock_state();
2175 static bool nfsd4_compound_in_session(struct nfsd4_session
*session
, struct nfs4_sessionid
*sid
)
2179 return !memcmp(sid
, &session
->se_sessionid
, sizeof(*sid
));
2183 nfsd4_destroy_session(struct svc_rqst
*r
,
2184 struct nfsd4_compound_state
*cstate
,
2185 struct nfsd4_destroy_session
*sessionid
)
2187 struct nfsd4_session
*ses
;
2189 int ref_held_by_me
= 0;
2190 struct nfsd_net
*nn
= net_generic(SVC_NET(r
), nfsd_net_id
);
2193 status
= nfserr_not_only_op
;
2194 if (nfsd4_compound_in_session(cstate
->session
, &sessionid
->sessionid
)) {
2195 if (!nfsd4_last_compound_op(r
))
2199 dump_sessionid(__func__
, &sessionid
->sessionid
);
2200 spin_lock(&nn
->client_lock
);
2201 ses
= find_in_sessionid_hashtbl(&sessionid
->sessionid
, SVC_NET(r
));
2202 status
= nfserr_badsession
;
2204 goto out_client_lock
;
2205 status
= nfserr_wrong_cred
;
2206 if (!mach_creds_match(ses
->se_client
, r
))
2207 goto out_client_lock
;
2208 nfsd4_get_session_locked(ses
);
2209 status
= mark_session_dead_locked(ses
, 1 + ref_held_by_me
);
2211 goto out_put_session
;
2212 unhash_session(ses
);
2213 spin_unlock(&nn
->client_lock
);
2215 nfsd4_probe_callback_sync(ses
->se_client
);
2217 spin_lock(&nn
->client_lock
);
2220 nfsd4_put_session(ses
);
2222 spin_unlock(&nn
->client_lock
);
2224 nfs4_unlock_state();
2228 static struct nfsd4_conn
*__nfsd4_find_conn(struct svc_xprt
*xpt
, struct nfsd4_session
*s
)
2230 struct nfsd4_conn
*c
;
2232 list_for_each_entry(c
, &s
->se_conns
, cn_persession
) {
2233 if (c
->cn_xprt
== xpt
) {
2240 static __be32
nfsd4_sequence_check_conn(struct nfsd4_conn
*new, struct nfsd4_session
*ses
)
2242 struct nfs4_client
*clp
= ses
->se_client
;
2243 struct nfsd4_conn
*c
;
2244 __be32 status
= nfs_ok
;
2247 spin_lock(&clp
->cl_lock
);
2248 c
= __nfsd4_find_conn(new->cn_xprt
, ses
);
2251 status
= nfserr_conn_not_bound_to_session
;
2252 if (clp
->cl_mach_cred
)
2254 __nfsd4_hash_conn(new, ses
);
2255 spin_unlock(&clp
->cl_lock
);
2256 ret
= nfsd4_register_conn(new);
2258 /* oops; xprt is already down: */
2259 nfsd4_conn_lost(&new->cn_xpt_user
);
2262 spin_unlock(&clp
->cl_lock
);
2267 static bool nfsd4_session_too_many_ops(struct svc_rqst
*rqstp
, struct nfsd4_session
*session
)
2269 struct nfsd4_compoundargs
*args
= rqstp
->rq_argp
;
2271 return args
->opcnt
> session
->se_fchannel
.maxops
;
2274 static bool nfsd4_request_too_big(struct svc_rqst
*rqstp
,
2275 struct nfsd4_session
*session
)
2277 struct xdr_buf
*xb
= &rqstp
->rq_arg
;
2279 return xb
->len
> session
->se_fchannel
.maxreq_sz
;
2283 nfsd4_sequence(struct svc_rqst
*rqstp
,
2284 struct nfsd4_compound_state
*cstate
,
2285 struct nfsd4_sequence
*seq
)
2287 struct nfsd4_compoundres
*resp
= rqstp
->rq_resp
;
2288 struct xdr_stream
*xdr
= &resp
->xdr
;
2289 struct nfsd4_session
*session
;
2290 struct nfs4_client
*clp
;
2291 struct nfsd4_slot
*slot
;
2292 struct nfsd4_conn
*conn
;
2295 struct nfsd_net
*nn
= net_generic(SVC_NET(rqstp
), nfsd_net_id
);
2297 if (resp
->opcnt
!= 1)
2298 return nfserr_sequence_pos
;
2301 * Will be either used or freed by nfsd4_sequence_check_conn
2304 conn
= alloc_conn(rqstp
, NFS4_CDFC4_FORE
);
2306 return nfserr_jukebox
;
2308 spin_lock(&nn
->client_lock
);
2309 status
= nfserr_badsession
;
2310 session
= find_in_sessionid_hashtbl(&seq
->sessionid
, SVC_NET(rqstp
));
2312 goto out_no_session
;
2313 clp
= session
->se_client
;
2314 status
= get_client_locked(clp
);
2316 goto out_no_session
;
2317 status
= nfsd4_get_session_locked(session
);
2319 goto out_put_client
;
2321 status
= nfserr_too_many_ops
;
2322 if (nfsd4_session_too_many_ops(rqstp
, session
))
2323 goto out_put_session
;
2325 status
= nfserr_req_too_big
;
2326 if (nfsd4_request_too_big(rqstp
, session
))
2327 goto out_put_session
;
2329 status
= nfserr_badslot
;
2330 if (seq
->slotid
>= session
->se_fchannel
.maxreqs
)
2331 goto out_put_session
;
2333 slot
= session
->se_slots
[seq
->slotid
];
2334 dprintk("%s: slotid %d\n", __func__
, seq
->slotid
);
2336 /* We do not negotiate the number of slots yet, so set the
2337 * maxslots to the session maxreqs which is used to encode
2338 * sr_highest_slotid and the sr_target_slot id to maxslots */
2339 seq
->maxslots
= session
->se_fchannel
.maxreqs
;
2341 status
= check_slot_seqid(seq
->seqid
, slot
->sl_seqid
,
2342 slot
->sl_flags
& NFSD4_SLOT_INUSE
);
2343 if (status
== nfserr_replay_cache
) {
2344 status
= nfserr_seq_misordered
;
2345 if (!(slot
->sl_flags
& NFSD4_SLOT_INITIALIZED
))
2346 goto out_put_session
;
2347 cstate
->slot
= slot
;
2348 cstate
->session
= session
;
2349 /* Return the cached reply status and set cstate->status
2350 * for nfsd4_proc_compound processing */
2351 status
= nfsd4_replay_cache_entry(resp
, seq
);
2352 cstate
->status
= nfserr_replay_cache
;
2356 goto out_put_session
;
2358 status
= nfsd4_sequence_check_conn(conn
, session
);
2361 goto out_put_session
;
2363 buflen
= (seq
->cachethis
) ?
2364 session
->se_fchannel
.maxresp_cached
:
2365 session
->se_fchannel
.maxresp_sz
;
2366 status
= (seq
->cachethis
) ? nfserr_rep_too_big_to_cache
:
2368 if (xdr_restrict_buflen(xdr
, buflen
- rqstp
->rq_auth_slack
))
2369 goto out_put_session
;
2370 svc_reserve(rqstp
, buflen
);
2373 /* Success! bump slot seqid */
2374 slot
->sl_seqid
= seq
->seqid
;
2375 slot
->sl_flags
|= NFSD4_SLOT_INUSE
;
2377 slot
->sl_flags
|= NFSD4_SLOT_CACHETHIS
;
2379 slot
->sl_flags
&= ~NFSD4_SLOT_CACHETHIS
;
2381 cstate
->slot
= slot
;
2382 cstate
->session
= session
;
2385 switch (clp
->cl_cb_state
) {
2387 seq
->status_flags
= SEQ4_STATUS_CB_PATH_DOWN
;
2389 case NFSD4_CB_FAULT
:
2390 seq
->status_flags
= SEQ4_STATUS_BACKCHANNEL_FAULT
;
2393 seq
->status_flags
= 0;
2395 if (!list_empty(&clp
->cl_revoked
))
2396 seq
->status_flags
|= SEQ4_STATUS_RECALLABLE_STATE_REVOKED
;
2400 spin_unlock(&nn
->client_lock
);
2403 nfsd4_put_session(session
);
2405 put_client_renew_locked(clp
);
2406 goto out_no_session
;
2410 nfsd4_sequence_done(struct nfsd4_compoundres
*resp
)
2412 struct nfsd4_compound_state
*cs
= &resp
->cstate
;
2414 if (nfsd4_has_session(cs
)) {
2415 struct nfs4_client
*clp
= cs
->session
->se_client
;
2416 struct nfsd_net
*nn
= net_generic(clp
->net
, nfsd_net_id
);
2418 if (cs
->status
!= nfserr_replay_cache
) {
2419 nfsd4_store_cache_entry(resp
);
2420 cs
->slot
->sl_flags
&= ~NFSD4_SLOT_INUSE
;
2422 /* Renew the clientid on success and on replay */
2423 spin_lock(&nn
->client_lock
);
2424 nfsd4_put_session(cs
->session
);
2425 put_client_renew_locked(clp
);
2426 spin_unlock(&nn
->client_lock
);
2431 nfsd4_destroy_clientid(struct svc_rqst
*rqstp
, struct nfsd4_compound_state
*cstate
, struct nfsd4_destroy_clientid
*dc
)
2433 struct nfs4_client
*conf
, *unconf
, *clp
;
2435 struct nfsd_net
*nn
= net_generic(SVC_NET(rqstp
), nfsd_net_id
);
2438 unconf
= find_unconfirmed_client(&dc
->clientid
, true, nn
);
2439 conf
= find_confirmed_client(&dc
->clientid
, true, nn
);
2440 WARN_ON_ONCE(conf
&& unconf
);
2445 if (client_has_state(conf
)) {
2446 status
= nfserr_clientid_busy
;
2452 status
= nfserr_stale_clientid
;
2455 if (!mach_creds_match(clp
, rqstp
)) {
2456 status
= nfserr_wrong_cred
;
2461 nfs4_unlock_state();
2466 nfsd4_reclaim_complete(struct svc_rqst
*rqstp
, struct nfsd4_compound_state
*cstate
, struct nfsd4_reclaim_complete
*rc
)
2470 if (rc
->rca_one_fs
) {
2471 if (!cstate
->current_fh
.fh_dentry
)
2472 return nfserr_nofilehandle
;
2474 * We don't take advantage of the rca_one_fs case.
2475 * That's OK, it's optional, we can safely ignore it.
2481 status
= nfserr_complete_already
;
2482 if (test_and_set_bit(NFSD4_CLIENT_RECLAIM_COMPLETE
,
2483 &cstate
->session
->se_client
->cl_flags
))
2486 status
= nfserr_stale_clientid
;
2487 if (is_client_expired(cstate
->session
->se_client
))
2489 * The following error isn't really legal.
2490 * But we only get here if the client just explicitly
2491 * destroyed the client. Surely it no longer cares what
2492 * error it gets back on an operation for the dead
2498 nfsd4_client_record_create(cstate
->session
->se_client
);
2500 nfs4_unlock_state();
2505 nfsd4_setclientid(struct svc_rqst
*rqstp
, struct nfsd4_compound_state
*cstate
,
2506 struct nfsd4_setclientid
*setclid
)
2508 struct xdr_netobj clname
= setclid
->se_name
;
2509 nfs4_verifier clverifier
= setclid
->se_verf
;
2510 struct nfs4_client
*conf
, *unconf
, *new;
2512 struct nfsd_net
*nn
= net_generic(SVC_NET(rqstp
), nfsd_net_id
);
2514 /* Cases below refer to rfc 3530 section 14.2.33: */
2516 conf
= find_confirmed_client_by_name(&clname
, nn
);
2519 status
= nfserr_clid_inuse
;
2520 if (clp_used_exchangeid(conf
))
2522 if (!same_creds(&conf
->cl_cred
, &rqstp
->rq_cred
)) {
2523 char addr_str
[INET6_ADDRSTRLEN
];
2524 rpc_ntop((struct sockaddr
*) &conf
->cl_addr
, addr_str
,
2526 dprintk("NFSD: setclientid: string in use by client "
2527 "at %s\n", addr_str
);
2531 unconf
= find_unconfirmed_client_by_name(&clname
, nn
);
2533 expire_client(unconf
);
2534 status
= nfserr_jukebox
;
2535 new = create_client(clname
, rqstp
, &clverifier
);
2538 if (conf
&& same_verf(&conf
->cl_verifier
, &clverifier
))
2539 /* case 1: probable callback update */
2540 copy_clid(new, conf
);
2541 else /* case 4 (new client) or cases 2, 3 (client reboot): */
2543 new->cl_minorversion
= 0;
2544 gen_callback(new, setclid
, rqstp
);
2545 add_to_unconfirmed(new);
2546 setclid
->se_clientid
.cl_boot
= new->cl_clientid
.cl_boot
;
2547 setclid
->se_clientid
.cl_id
= new->cl_clientid
.cl_id
;
2548 memcpy(setclid
->se_confirm
.data
, new->cl_confirm
.data
, sizeof(setclid
->se_confirm
.data
));
2551 nfs4_unlock_state();
2557 nfsd4_setclientid_confirm(struct svc_rqst
*rqstp
,
2558 struct nfsd4_compound_state
*cstate
,
2559 struct nfsd4_setclientid_confirm
*setclientid_confirm
)
2561 struct nfs4_client
*conf
, *unconf
;
2562 nfs4_verifier confirm
= setclientid_confirm
->sc_confirm
;
2563 clientid_t
* clid
= &setclientid_confirm
->sc_clientid
;
2565 struct nfsd_net
*nn
= net_generic(SVC_NET(rqstp
), nfsd_net_id
);
2567 if (STALE_CLIENTID(clid
, nn
))
2568 return nfserr_stale_clientid
;
2571 conf
= find_confirmed_client(clid
, false, nn
);
2572 unconf
= find_unconfirmed_client(clid
, false, nn
);
2574 * We try hard to give out unique clientid's, so if we get an
2575 * attempt to confirm the same clientid with a different cred,
2576 * there's a bug somewhere. Let's charitably assume it's our
2579 status
= nfserr_serverfault
;
2580 if (unconf
&& !same_creds(&unconf
->cl_cred
, &rqstp
->rq_cred
))
2582 if (conf
&& !same_creds(&conf
->cl_cred
, &rqstp
->rq_cred
))
2584 /* cases below refer to rfc 3530 section 14.2.34: */
2585 if (!unconf
|| !same_verf(&confirm
, &unconf
->cl_confirm
)) {
2586 if (conf
&& !unconf
) /* case 2: probable retransmit */
2588 else /* case 4: client hasn't noticed we rebooted yet? */
2589 status
= nfserr_stale_clientid
;
2593 if (conf
) { /* case 1: callback update */
2594 nfsd4_change_callback(conf
, &unconf
->cl_cb_conn
);
2595 nfsd4_probe_callback(conf
);
2596 expire_client(unconf
);
2597 } else { /* case 3: normal case; new or rebooted client */
2598 conf
= find_confirmed_client_by_name(&unconf
->cl_name
, nn
);
2600 status
= mark_client_expired(conf
);
2603 expire_client(conf
);
2605 move_to_confirmed(unconf
);
2606 nfsd4_probe_callback(unconf
);
2609 nfs4_unlock_state();
2613 static struct nfs4_file
*nfsd4_alloc_file(void)
2615 return kmem_cache_alloc(file_slab
, GFP_KERNEL
);
2618 /* OPEN Share state helper functions */
2619 static void nfsd4_init_file(struct nfs4_file
*fp
, struct inode
*ino
)
2621 unsigned int hashval
= file_hashval(ino
);
2623 lockdep_assert_held(&state_lock
);
2625 atomic_set(&fp
->fi_ref
, 1);
2626 INIT_LIST_HEAD(&fp
->fi_stateids
);
2627 INIT_LIST_HEAD(&fp
->fi_delegations
);
2630 fp
->fi_had_conflict
= false;
2631 fp
->fi_lease
= NULL
;
2632 memset(fp
->fi_fds
, 0, sizeof(fp
->fi_fds
));
2633 memset(fp
->fi_access
, 0, sizeof(fp
->fi_access
));
2634 hlist_add_head(&fp
->fi_hash
, &file_hashtbl
[hashval
]);
2638 nfsd4_free_slabs(void)
2640 kmem_cache_destroy(openowner_slab
);
2641 kmem_cache_destroy(lockowner_slab
);
2642 kmem_cache_destroy(file_slab
);
2643 kmem_cache_destroy(stateid_slab
);
2644 kmem_cache_destroy(deleg_slab
);
2648 nfsd4_init_slabs(void)
2650 openowner_slab
= kmem_cache_create("nfsd4_openowners",
2651 sizeof(struct nfs4_openowner
), 0, 0, NULL
);
2652 if (openowner_slab
== NULL
)
2654 lockowner_slab
= kmem_cache_create("nfsd4_lockowners",
2655 sizeof(struct nfs4_lockowner
), 0, 0, NULL
);
2656 if (lockowner_slab
== NULL
)
2657 goto out_free_openowner_slab
;
2658 file_slab
= kmem_cache_create("nfsd4_files",
2659 sizeof(struct nfs4_file
), 0, 0, NULL
);
2660 if (file_slab
== NULL
)
2661 goto out_free_lockowner_slab
;
2662 stateid_slab
= kmem_cache_create("nfsd4_stateids",
2663 sizeof(struct nfs4_ol_stateid
), 0, 0, NULL
);
2664 if (stateid_slab
== NULL
)
2665 goto out_free_file_slab
;
2666 deleg_slab
= kmem_cache_create("nfsd4_delegations",
2667 sizeof(struct nfs4_delegation
), 0, 0, NULL
);
2668 if (deleg_slab
== NULL
)
2669 goto out_free_stateid_slab
;
2672 out_free_stateid_slab
:
2673 kmem_cache_destroy(stateid_slab
);
2675 kmem_cache_destroy(file_slab
);
2676 out_free_lockowner_slab
:
2677 kmem_cache_destroy(lockowner_slab
);
2678 out_free_openowner_slab
:
2679 kmem_cache_destroy(openowner_slab
);
2681 dprintk("nfsd4: out of memory while initializing nfsv4\n");
2685 static void init_nfs4_replay(struct nfs4_replay
*rp
)
2687 rp
->rp_status
= nfserr_serverfault
;
2689 rp
->rp_buf
= rp
->rp_ibuf
;
2692 static inline void *alloc_stateowner(struct kmem_cache
*slab
, struct xdr_netobj
*owner
, struct nfs4_client
*clp
)
2694 struct nfs4_stateowner
*sop
;
2696 sop
= kmem_cache_alloc(slab
, GFP_KERNEL
);
2700 sop
->so_owner
.data
= kmemdup(owner
->data
, owner
->len
, GFP_KERNEL
);
2701 if (!sop
->so_owner
.data
) {
2702 kmem_cache_free(slab
, sop
);
2705 sop
->so_owner
.len
= owner
->len
;
2707 INIT_LIST_HEAD(&sop
->so_stateids
);
2708 sop
->so_client
= clp
;
2709 init_nfs4_replay(&sop
->so_replay
);
2713 static void hash_openowner(struct nfs4_openowner
*oo
, struct nfs4_client
*clp
, unsigned int strhashval
)
2715 struct nfsd_net
*nn
= net_generic(clp
->net
, nfsd_net_id
);
2717 list_add(&oo
->oo_owner
.so_strhash
, &nn
->ownerstr_hashtbl
[strhashval
]);
2718 list_add(&oo
->oo_perclient
, &clp
->cl_openowners
);
2721 static struct nfs4_openowner
*
2722 alloc_init_open_stateowner(unsigned int strhashval
, struct nfs4_client
*clp
, struct nfsd4_open
*open
) {
2723 struct nfs4_openowner
*oo
;
2725 oo
= alloc_stateowner(openowner_slab
, &open
->op_owner
, clp
);
2728 oo
->oo_owner
.so_is_open_owner
= 1;
2729 oo
->oo_owner
.so_seqid
= open
->op_seqid
;
2730 oo
->oo_flags
= NFS4_OO_NEW
;
2732 oo
->oo_last_closed_stid
= NULL
;
2733 INIT_LIST_HEAD(&oo
->oo_close_lru
);
2734 hash_openowner(oo
, clp
, strhashval
);
2738 static void init_open_stateid(struct nfs4_ol_stateid
*stp
, struct nfs4_file
*fp
, struct nfsd4_open
*open
) {
2739 struct nfs4_openowner
*oo
= open
->op_openowner
;
2741 stp
->st_stid
.sc_type
= NFS4_OPEN_STID
;
2742 INIT_LIST_HEAD(&stp
->st_lockowners
);
2743 list_add(&stp
->st_perstateowner
, &oo
->oo_owner
.so_stateids
);
2744 list_add(&stp
->st_perfile
, &fp
->fi_stateids
);
2745 stp
->st_stateowner
= &oo
->oo_owner
;
2748 stp
->st_access_bmap
= 0;
2749 stp
->st_deny_bmap
= 0;
2750 set_access(open
->op_share_access
, stp
);
2751 set_deny(open
->op_share_deny
, stp
);
2752 stp
->st_openstp
= NULL
;
2756 move_to_close_lru(struct nfs4_openowner
*oo
, struct net
*net
)
2758 struct nfsd_net
*nn
= net_generic(net
, nfsd_net_id
);
2760 dprintk("NFSD: move_to_close_lru nfs4_openowner %p\n", oo
);
2762 list_move_tail(&oo
->oo_close_lru
, &nn
->close_lru
);
2763 oo
->oo_time
= get_seconds();
2767 same_owner_str(struct nfs4_stateowner
*sop
, struct xdr_netobj
*owner
,
2770 return (sop
->so_owner
.len
== owner
->len
) &&
2771 0 == memcmp(sop
->so_owner
.data
, owner
->data
, owner
->len
) &&
2772 (sop
->so_client
->cl_clientid
.cl_id
== clid
->cl_id
);
2775 static struct nfs4_openowner
*
2776 find_openstateowner_str(unsigned int hashval
, struct nfsd4_open
*open
,
2777 bool sessions
, struct nfsd_net
*nn
)
2779 struct nfs4_stateowner
*so
;
2780 struct nfs4_openowner
*oo
;
2781 struct nfs4_client
*clp
;
2783 list_for_each_entry(so
, &nn
->ownerstr_hashtbl
[hashval
], so_strhash
) {
2784 if (!so
->so_is_open_owner
)
2786 if (same_owner_str(so
, &open
->op_owner
, &open
->op_clientid
)) {
2788 clp
= oo
->oo_owner
.so_client
;
2789 if ((bool)clp
->cl_minorversion
!= sessions
)
2791 renew_client(oo
->oo_owner
.so_client
);
2798 /* search file_hashtbl[] for file */
2799 static struct nfs4_file
*
2800 find_file_locked(struct inode
*ino
)
2802 unsigned int hashval
= file_hashval(ino
);
2803 struct nfs4_file
*fp
;
2805 lockdep_assert_held(&state_lock
);
2807 hlist_for_each_entry(fp
, &file_hashtbl
[hashval
], fi_hash
) {
2808 if (fp
->fi_inode
== ino
) {
2816 static struct nfs4_file
*
2817 find_file(struct inode
*ino
)
2819 struct nfs4_file
*fp
;
2821 spin_lock(&state_lock
);
2822 fp
= find_file_locked(ino
);
2823 spin_unlock(&state_lock
);
2827 static struct nfs4_file
*
2828 find_or_add_file(struct inode
*ino
, struct nfs4_file
*new)
2830 struct nfs4_file
*fp
;
2832 spin_lock(&state_lock
);
2833 fp
= find_file_locked(ino
);
2835 nfsd4_init_file(new, ino
);
2838 spin_unlock(&state_lock
);
2844 * Called to check deny when READ with all zero stateid or
2845 * WRITE with all zero or all one stateid
2848 nfs4_share_conflict(struct svc_fh
*current_fh
, unsigned int deny_type
)
2850 struct inode
*ino
= current_fh
->fh_dentry
->d_inode
;
2851 struct nfs4_file
*fp
;
2852 struct nfs4_ol_stateid
*stp
;
2855 fp
= find_file(ino
);
2858 ret
= nfserr_locked
;
2859 /* Search for conflicting share reservations */
2860 list_for_each_entry(stp
, &fp
->fi_stateids
, st_perfile
) {
2861 if (test_deny(deny_type
, stp
) ||
2862 test_deny(NFS4_SHARE_DENY_BOTH
, stp
))
2871 static void nfsd_break_one_deleg(struct nfs4_delegation
*dp
)
2873 struct nfs4_client
*clp
= dp
->dl_stid
.sc_client
;
2874 struct nfsd_net
*nn
= net_generic(clp
->net
, nfsd_net_id
);
2876 lockdep_assert_held(&state_lock
);
2877 /* We're assuming the state code never drops its reference
2878 * without first removing the lease. Since we're in this lease
2879 * callback (and since the lease code is serialized by the kernel
2880 * lock) we know the server hasn't removed the lease yet, we know
2881 * it's safe to take a reference: */
2882 atomic_inc(&dp
->dl_count
);
2884 list_add_tail(&dp
->dl_recall_lru
, &nn
->del_recall_lru
);
2886 /* Only place dl_time is set; protected by i_lock: */
2887 dp
->dl_time
= get_seconds();
2889 block_delegations(&dp
->dl_fh
);
2891 nfsd4_cb_recall(dp
);
2894 /* Called from break_lease() with i_lock held. */
2895 static void nfsd_break_deleg_cb(struct file_lock
*fl
)
2897 struct nfs4_file
*fp
= (struct nfs4_file
*)fl
->fl_owner
;
2898 struct nfs4_delegation
*dp
;
2901 WARN(1, "(%p)->fl_owner NULL\n", fl
);
2904 if (fp
->fi_had_conflict
) {
2905 WARN(1, "duplicate break on %p\n", fp
);
2909 * We don't want the locks code to timeout the lease for us;
2910 * we'll remove it ourself if a delegation isn't returned
2913 fl
->fl_break_time
= 0;
2915 spin_lock(&state_lock
);
2916 fp
->fi_had_conflict
= true;
2917 list_for_each_entry(dp
, &fp
->fi_delegations
, dl_perfile
)
2918 nfsd_break_one_deleg(dp
);
2919 spin_unlock(&state_lock
);
2923 int nfsd_change_deleg_cb(struct file_lock
**onlist
, int arg
)
2926 return lease_modify(onlist
, arg
);
2931 static const struct lock_manager_operations nfsd_lease_mng_ops
= {
2932 .lm_break
= nfsd_break_deleg_cb
,
2933 .lm_change
= nfsd_change_deleg_cb
,
2936 static __be32
nfsd4_check_seqid(struct nfsd4_compound_state
*cstate
, struct nfs4_stateowner
*so
, u32 seqid
)
2938 if (nfsd4_has_session(cstate
))
2940 if (seqid
== so
->so_seqid
- 1)
2941 return nfserr_replay_me
;
2942 if (seqid
== so
->so_seqid
)
2944 return nfserr_bad_seqid
;
2948 nfsd4_process_open1(struct nfsd4_compound_state
*cstate
,
2949 struct nfsd4_open
*open
, struct nfsd_net
*nn
)
2951 clientid_t
*clientid
= &open
->op_clientid
;
2952 struct nfs4_client
*clp
= NULL
;
2953 unsigned int strhashval
;
2954 struct nfs4_openowner
*oo
= NULL
;
2957 if (STALE_CLIENTID(&open
->op_clientid
, nn
))
2958 return nfserr_stale_clientid
;
2960 * In case we need it later, after we've already created the
2961 * file and don't want to risk a further failure:
2963 open
->op_file
= nfsd4_alloc_file();
2964 if (open
->op_file
== NULL
)
2965 return nfserr_jukebox
;
2967 strhashval
= ownerstr_hashval(clientid
->cl_id
, &open
->op_owner
);
2968 oo
= find_openstateowner_str(strhashval
, open
, cstate
->minorversion
, nn
);
2969 open
->op_openowner
= oo
;
2971 clp
= find_confirmed_client(clientid
, cstate
->minorversion
,
2974 return nfserr_expired
;
2977 if (!(oo
->oo_flags
& NFS4_OO_CONFIRMED
)) {
2978 /* Replace unconfirmed owners without checking for replay. */
2979 clp
= oo
->oo_owner
.so_client
;
2980 release_openowner(oo
);
2981 open
->op_openowner
= NULL
;
2984 status
= nfsd4_check_seqid(cstate
, &oo
->oo_owner
, open
->op_seqid
);
2987 clp
= oo
->oo_owner
.so_client
;
2990 oo
= alloc_init_open_stateowner(strhashval
, clp
, open
);
2992 return nfserr_jukebox
;
2993 open
->op_openowner
= oo
;
2995 open
->op_stp
= nfs4_alloc_stateid(clp
);
2997 return nfserr_jukebox
;
3001 static inline __be32
3002 nfs4_check_delegmode(struct nfs4_delegation
*dp
, int flags
)
3004 if ((flags
& WR_STATE
) && (dp
->dl_type
== NFS4_OPEN_DELEGATE_READ
))
3005 return nfserr_openmode
;
3010 static int share_access_to_flags(u32 share_access
)
3012 return share_access
== NFS4_SHARE_ACCESS_READ
? RD_STATE
: WR_STATE
;
3015 static struct nfs4_delegation
*find_deleg_stateid(struct nfs4_client
*cl
, stateid_t
*s
)
3017 struct nfs4_stid
*ret
;
3019 ret
= find_stateid_by_type(cl
, s
, NFS4_DELEG_STID
);
3022 return delegstateid(ret
);
3025 static bool nfsd4_is_deleg_cur(struct nfsd4_open
*open
)
3027 return open
->op_claim_type
== NFS4_OPEN_CLAIM_DELEGATE_CUR
||
3028 open
->op_claim_type
== NFS4_OPEN_CLAIM_DELEG_CUR_FH
;
3032 nfs4_check_deleg(struct nfs4_client
*cl
, struct nfsd4_open
*open
,
3033 struct nfs4_delegation
**dp
)
3036 __be32 status
= nfserr_bad_stateid
;
3038 *dp
= find_deleg_stateid(cl
, &open
->op_delegate_stateid
);
3041 flags
= share_access_to_flags(open
->op_share_access
);
3042 status
= nfs4_check_delegmode(*dp
, flags
);
3046 if (!nfsd4_is_deleg_cur(open
))
3050 open
->op_openowner
->oo_flags
|= NFS4_OO_CONFIRMED
;
3055 nfs4_check_open(struct nfs4_file
*fp
, struct nfsd4_open
*open
, struct nfs4_ol_stateid
**stpp
)
3057 struct nfs4_ol_stateid
*local
;
3058 struct nfs4_openowner
*oo
= open
->op_openowner
;
3060 list_for_each_entry(local
, &fp
->fi_stateids
, st_perfile
) {
3061 /* ignore lock owners */
3062 if (local
->st_stateowner
->so_is_open_owner
== 0)
3064 /* remember if we have seen this open owner */
3065 if (local
->st_stateowner
== &oo
->oo_owner
)
3067 /* check for conflicting share reservations */
3068 if (!test_share(local
, open
))
3069 return nfserr_share_denied
;
3074 static inline int nfs4_access_to_access(u32 nfs4_access
)
3078 if (nfs4_access
& NFS4_SHARE_ACCESS_READ
)
3079 flags
|= NFSD_MAY_READ
;
3080 if (nfs4_access
& NFS4_SHARE_ACCESS_WRITE
)
3081 flags
|= NFSD_MAY_WRITE
;
3085 static inline __be32
3086 nfsd4_truncate(struct svc_rqst
*rqstp
, struct svc_fh
*fh
,
3087 struct nfsd4_open
*open
)
3089 struct iattr iattr
= {
3090 .ia_valid
= ATTR_SIZE
,
3093 if (!open
->op_truncate
)
3095 if (!(open
->op_share_access
& NFS4_SHARE_ACCESS_WRITE
))
3096 return nfserr_inval
;
3097 return nfsd_setattr(rqstp
, fh
, &iattr
, 0, (time_t)0);
3100 static __be32
nfs4_get_vfs_file(struct svc_rqst
*rqstp
, struct nfs4_file
*fp
,
3101 struct svc_fh
*cur_fh
, struct nfsd4_open
*open
)
3104 int oflag
= nfs4_access_to_omode(open
->op_share_access
);
3105 int access
= nfs4_access_to_access(open
->op_share_access
);
3107 if (!fp
->fi_fds
[oflag
]) {
3108 status
= nfsd_open(rqstp
, cur_fh
, S_IFREG
, access
,
3109 &fp
->fi_fds
[oflag
]);
3113 nfs4_file_get_access(fp
, oflag
);
3115 status
= nfsd4_truncate(rqstp
, cur_fh
, open
);
3117 goto out_put_access
;
3122 nfs4_file_put_access(fp
, oflag
);
3128 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
)
3130 u32 op_share_access
= open
->op_share_access
;
3133 if (!test_access(op_share_access
, stp
))
3134 status
= nfs4_get_vfs_file(rqstp
, fp
, cur_fh
, open
);
3136 status
= nfsd4_truncate(rqstp
, cur_fh
, open
);
3141 /* remember the open */
3142 set_access(op_share_access
, stp
);
3143 set_deny(open
->op_share_deny
, stp
);
3149 nfs4_set_claim_prev(struct nfsd4_open
*open
, bool has_session
)
3151 open
->op_openowner
->oo_flags
|= NFS4_OO_CONFIRMED
;
3154 /* Should we give out recallable state?: */
3155 static bool nfsd4_cb_channel_good(struct nfs4_client
*clp
)
3157 if (clp
->cl_cb_state
== NFSD4_CB_UP
)
3160 * In the sessions case, since we don't have to establish a
3161 * separate connection for callbacks, we assume it's OK
3162 * until we hear otherwise:
3164 return clp
->cl_minorversion
&& clp
->cl_cb_state
== NFSD4_CB_UNKNOWN
;
3167 static struct file_lock
*nfs4_alloc_init_lease(struct nfs4_delegation
*dp
, int flag
)
3169 struct file_lock
*fl
;
3171 fl
= locks_alloc_lock();
3174 locks_init_lock(fl
);
3175 fl
->fl_lmops
= &nfsd_lease_mng_ops
;
3176 fl
->fl_flags
= FL_DELEG
;
3177 fl
->fl_type
= flag
== NFS4_OPEN_DELEGATE_READ
? F_RDLCK
: F_WRLCK
;
3178 fl
->fl_end
= OFFSET_MAX
;
3179 fl
->fl_owner
= (fl_owner_t
)(dp
->dl_file
);
3180 fl
->fl_pid
= current
->tgid
;
3184 static int nfs4_setlease(struct nfs4_delegation
*dp
)
3186 struct nfs4_file
*fp
= dp
->dl_file
;
3187 struct file_lock
*fl
;
3190 fl
= nfs4_alloc_init_lease(dp
, NFS4_OPEN_DELEGATE_READ
);
3193 fl
->fl_file
= find_readable_file(fp
);
3194 status
= vfs_setlease(fl
->fl_file
, fl
->fl_type
, &fl
);
3198 fp
->fi_deleg_file
= get_file(fl
->fl_file
);
3199 atomic_set(&fp
->fi_delegees
, 1);
3200 spin_lock(&state_lock
);
3201 hash_delegation_locked(dp
, fp
);
3202 spin_unlock(&state_lock
);
3205 locks_free_lock(fl
);
3209 static int nfs4_set_delegation(struct nfs4_delegation
*dp
, struct nfs4_file
*fp
)
3211 if (fp
->fi_had_conflict
)
3216 return nfs4_setlease(dp
);
3217 spin_lock(&state_lock
);
3218 atomic_inc(&fp
->fi_delegees
);
3219 if (fp
->fi_had_conflict
) {
3220 spin_unlock(&state_lock
);
3223 hash_delegation_locked(dp
, fp
);
3224 spin_unlock(&state_lock
);
3228 static void nfsd4_open_deleg_none_ext(struct nfsd4_open
*open
, int status
)
3230 open
->op_delegate_type
= NFS4_OPEN_DELEGATE_NONE_EXT
;
3231 if (status
== -EAGAIN
)
3232 open
->op_why_no_deleg
= WND4_CONTENTION
;
3234 open
->op_why_no_deleg
= WND4_RESOURCE
;
3235 switch (open
->op_deleg_want
) {
3236 case NFS4_SHARE_WANT_READ_DELEG
:
3237 case NFS4_SHARE_WANT_WRITE_DELEG
:
3238 case NFS4_SHARE_WANT_ANY_DELEG
:
3240 case NFS4_SHARE_WANT_CANCEL
:
3241 open
->op_why_no_deleg
= WND4_CANCELLED
;
3243 case NFS4_SHARE_WANT_NO_DELEG
:
3250 * Attempt to hand out a delegation.
3252 * Note we don't support write delegations, and won't until the vfs has
3253 * proper support for them.
3256 nfs4_open_delegation(struct net
*net
, struct svc_fh
*fh
,
3257 struct nfsd4_open
*open
, struct nfs4_ol_stateid
*stp
)
3259 struct nfs4_delegation
*dp
;
3260 struct nfs4_openowner
*oo
= container_of(stp
->st_stateowner
, struct nfs4_openowner
, oo_owner
);
3264 cb_up
= nfsd4_cb_channel_good(oo
->oo_owner
.so_client
);
3265 open
->op_recall
= 0;
3266 switch (open
->op_claim_type
) {
3267 case NFS4_OPEN_CLAIM_PREVIOUS
:
3269 open
->op_recall
= 1;
3270 if (open
->op_delegate_type
!= NFS4_OPEN_DELEGATE_READ
)
3273 case NFS4_OPEN_CLAIM_NULL
:
3274 case NFS4_OPEN_CLAIM_FH
:
3276 * Let's not give out any delegations till everyone's
3277 * had the chance to reclaim theirs....
3279 if (locks_in_grace(net
))
3281 if (!cb_up
|| !(oo
->oo_flags
& NFS4_OO_CONFIRMED
))
3284 * Also, if the file was opened for write or
3285 * create, there's a good chance the client's
3286 * about to write to it, resulting in an
3287 * immediate recall (since we don't support
3288 * write delegations):
3290 if (open
->op_share_access
& NFS4_SHARE_ACCESS_WRITE
)
3292 if (open
->op_create
== NFS4_OPEN_CREATE
)
3298 dp
= alloc_init_deleg(oo
->oo_owner
.so_client
, stp
, fh
);
3301 status
= nfs4_set_delegation(dp
, stp
->st_file
);
3305 memcpy(&open
->op_delegate_stateid
, &dp
->dl_stid
.sc_stateid
, sizeof(dp
->dl_stid
.sc_stateid
));
3307 dprintk("NFSD: delegation stateid=" STATEID_FMT
"\n",
3308 STATEID_VAL(&dp
->dl_stid
.sc_stateid
));
3309 open
->op_delegate_type
= NFS4_OPEN_DELEGATE_READ
;
3312 destroy_delegation(dp
);
3314 open
->op_delegate_type
= NFS4_OPEN_DELEGATE_NONE
;
3315 if (open
->op_claim_type
== NFS4_OPEN_CLAIM_PREVIOUS
&&
3316 open
->op_delegate_type
!= NFS4_OPEN_DELEGATE_NONE
) {
3317 dprintk("NFSD: WARNING: refusing delegation reclaim\n");
3318 open
->op_recall
= 1;
3321 /* 4.1 client asking for a delegation? */
3322 if (open
->op_deleg_want
)
3323 nfsd4_open_deleg_none_ext(open
, status
);
3327 static void nfsd4_deleg_xgrade_none_ext(struct nfsd4_open
*open
,
3328 struct nfs4_delegation
*dp
)
3330 if (open
->op_deleg_want
== NFS4_SHARE_WANT_READ_DELEG
&&
3331 dp
->dl_type
== NFS4_OPEN_DELEGATE_WRITE
) {
3332 open
->op_delegate_type
= NFS4_OPEN_DELEGATE_NONE_EXT
;
3333 open
->op_why_no_deleg
= WND4_NOT_SUPP_DOWNGRADE
;
3334 } else if (open
->op_deleg_want
== NFS4_SHARE_WANT_WRITE_DELEG
&&
3335 dp
->dl_type
== NFS4_OPEN_DELEGATE_WRITE
) {
3336 open
->op_delegate_type
= NFS4_OPEN_DELEGATE_NONE_EXT
;
3337 open
->op_why_no_deleg
= WND4_NOT_SUPP_UPGRADE
;
3339 /* Otherwise the client must be confused wanting a delegation
3340 * it already has, therefore we don't return
3341 * NFS4_OPEN_DELEGATE_NONE_EXT and reason.
3346 * called with nfs4_lock_state() held.
3349 nfsd4_process_open2(struct svc_rqst
*rqstp
, struct svc_fh
*current_fh
, struct nfsd4_open
*open
)
3351 struct nfsd4_compoundres
*resp
= rqstp
->rq_resp
;
3352 struct nfs4_client
*cl
= open
->op_openowner
->oo_owner
.so_client
;
3353 struct nfs4_file
*fp
= NULL
;
3354 struct inode
*ino
= current_fh
->fh_dentry
->d_inode
;
3355 struct nfs4_ol_stateid
*stp
= NULL
;
3356 struct nfs4_delegation
*dp
= NULL
;
3360 * Lookup file; if found, lookup stateid and check open request,
3361 * and check for delegations in the process of being recalled.
3362 * If not found, create the nfs4_file struct
3364 fp
= find_or_add_file(ino
, open
->op_file
);
3365 if (fp
!= open
->op_file
) {
3366 if ((status
= nfs4_check_open(fp
, open
, &stp
)))
3368 status
= nfs4_check_deleg(cl
, open
, &dp
);
3372 open
->op_file
= NULL
;
3373 status
= nfserr_bad_stateid
;
3374 if (nfsd4_is_deleg_cur(open
))
3376 status
= nfserr_jukebox
;
3380 * OPEN the file, or upgrade an existing OPEN.
3381 * If truncate fails, the OPEN fails.
3384 /* Stateid was found, this is an OPEN upgrade */
3385 status
= nfs4_upgrade_open(rqstp
, fp
, current_fh
, stp
, open
);
3389 status
= nfs4_get_vfs_file(rqstp
, fp
, current_fh
, open
);
3393 open
->op_stp
= NULL
;
3394 init_open_stateid(stp
, fp
, open
);
3396 update_stateid(&stp
->st_stid
.sc_stateid
);
3397 memcpy(&open
->op_stateid
, &stp
->st_stid
.sc_stateid
, sizeof(stateid_t
));
3399 if (nfsd4_has_session(&resp
->cstate
)) {
3400 open
->op_openowner
->oo_flags
|= NFS4_OO_CONFIRMED
;
3402 if (open
->op_deleg_want
& NFS4_SHARE_WANT_NO_DELEG
) {
3403 open
->op_delegate_type
= NFS4_OPEN_DELEGATE_NONE_EXT
;
3404 open
->op_why_no_deleg
= WND4_NOT_WANTED
;
3410 * Attempt to hand out a delegation. No error return, because the
3411 * OPEN succeeds even if we fail.
3413 nfs4_open_delegation(SVC_NET(rqstp
), current_fh
, open
, stp
);
3417 dprintk("%s: stateid=" STATEID_FMT
"\n", __func__
,
3418 STATEID_VAL(&stp
->st_stid
.sc_stateid
));
3420 /* 4.1 client trying to upgrade/downgrade delegation? */
3421 if (open
->op_delegate_type
== NFS4_OPEN_DELEGATE_NONE
&& dp
&&
3422 open
->op_deleg_want
)
3423 nfsd4_deleg_xgrade_none_ext(open
, dp
);
3427 if (status
== 0 && open
->op_claim_type
== NFS4_OPEN_CLAIM_PREVIOUS
)
3428 nfs4_set_claim_prev(open
, nfsd4_has_session(&resp
->cstate
));
3430 * To finish the open response, we just need to set the rflags.
3432 open
->op_rflags
= NFS4_OPEN_RESULT_LOCKTYPE_POSIX
;
3433 if (!(open
->op_openowner
->oo_flags
& NFS4_OO_CONFIRMED
) &&
3434 !nfsd4_has_session(&resp
->cstate
))
3435 open
->op_rflags
|= NFS4_OPEN_RESULT_CONFIRM
;
3440 void nfsd4_cleanup_open_state(struct nfsd4_open
*open
, __be32 status
)
3442 if (open
->op_openowner
) {
3443 struct nfs4_openowner
*oo
= open
->op_openowner
;
3445 if (!list_empty(&oo
->oo_owner
.so_stateids
))
3446 list_del_init(&oo
->oo_close_lru
);
3447 if (oo
->oo_flags
& NFS4_OO_NEW
) {
3449 release_openowner(oo
);
3450 open
->op_openowner
= NULL
;
3452 oo
->oo_flags
&= ~NFS4_OO_NEW
;
3456 nfsd4_free_file(open
->op_file
);
3458 free_generic_stateid(open
->op_stp
);
3461 static __be32
lookup_clientid(clientid_t
*clid
, bool session
, struct nfsd_net
*nn
, struct nfs4_client
**clp
)
3463 struct nfs4_client
*found
;
3465 if (STALE_CLIENTID(clid
, nn
))
3466 return nfserr_stale_clientid
;
3467 found
= find_confirmed_client(clid
, session
, nn
);
3470 return found
? nfs_ok
: nfserr_expired
;
3474 nfsd4_renew(struct svc_rqst
*rqstp
, struct nfsd4_compound_state
*cstate
,
3477 struct nfs4_client
*clp
;
3479 struct nfsd_net
*nn
= net_generic(SVC_NET(rqstp
), nfsd_net_id
);
3482 dprintk("process_renew(%08x/%08x): starting\n",
3483 clid
->cl_boot
, clid
->cl_id
);
3484 status
= lookup_clientid(clid
, cstate
->minorversion
, nn
, &clp
);
3487 status
= nfserr_cb_path_down
;
3488 if (!list_empty(&clp
->cl_delegations
)
3489 && clp
->cl_cb_state
!= NFSD4_CB_UP
)
3493 nfs4_unlock_state();
3498 nfsd4_end_grace(struct nfsd_net
*nn
)
3500 /* do nothing if grace period already ended */
3501 if (nn
->grace_ended
)
3504 dprintk("NFSD: end of grace period\n");
3505 nn
->grace_ended
= true;
3506 nfsd4_record_grace_done(nn
, nn
->boot_time
);
3507 locks_end_grace(&nn
->nfsd4_manager
);
3509 * Now that every NFSv4 client has had the chance to recover and
3510 * to see the (possibly new, possibly shorter) lease time, we
3511 * can safely set the next grace time to the current lease time:
3513 nn
->nfsd4_grace
= nn
->nfsd4_lease
;
3517 nfs4_laundromat(struct nfsd_net
*nn
)
3519 struct nfs4_client
*clp
;
3520 struct nfs4_openowner
*oo
;
3521 struct nfs4_delegation
*dp
;
3522 struct list_head
*pos
, *next
, reaplist
;
3523 time_t cutoff
= get_seconds() - nn
->nfsd4_lease
;
3524 time_t t
, new_timeo
= nn
->nfsd4_lease
;
3528 dprintk("NFSD: laundromat service - starting\n");
3529 nfsd4_end_grace(nn
);
3530 INIT_LIST_HEAD(&reaplist
);
3531 spin_lock(&nn
->client_lock
);
3532 list_for_each_safe(pos
, next
, &nn
->client_lru
) {
3533 clp
= list_entry(pos
, struct nfs4_client
, cl_lru
);
3534 if (time_after((unsigned long)clp
->cl_time
, (unsigned long)cutoff
)) {
3535 t
= clp
->cl_time
- cutoff
;
3536 new_timeo
= min(new_timeo
, t
);
3539 if (mark_client_expired_locked(clp
)) {
3540 dprintk("NFSD: client in use (clientid %08x)\n",
3541 clp
->cl_clientid
.cl_id
);
3544 list_move(&clp
->cl_lru
, &reaplist
);
3546 spin_unlock(&nn
->client_lock
);
3547 list_for_each_safe(pos
, next
, &reaplist
) {
3548 clp
= list_entry(pos
, struct nfs4_client
, cl_lru
);
3549 dprintk("NFSD: purging unused client (clientid %08x)\n",
3550 clp
->cl_clientid
.cl_id
);
3553 spin_lock(&state_lock
);
3554 list_for_each_safe(pos
, next
, &nn
->del_recall_lru
) {
3555 dp
= list_entry (pos
, struct nfs4_delegation
, dl_recall_lru
);
3556 if (net_generic(dp
->dl_stid
.sc_client
->net
, nfsd_net_id
) != nn
)
3558 if (time_after((unsigned long)dp
->dl_time
, (unsigned long)cutoff
)) {
3559 t
= dp
->dl_time
- cutoff
;
3560 new_timeo
= min(new_timeo
, t
);
3563 list_move(&dp
->dl_recall_lru
, &reaplist
);
3565 spin_unlock(&state_lock
);
3566 list_for_each_safe(pos
, next
, &reaplist
) {
3567 dp
= list_entry (pos
, struct nfs4_delegation
, dl_recall_lru
);
3568 revoke_delegation(dp
);
3570 list_for_each_safe(pos
, next
, &nn
->close_lru
) {
3571 oo
= container_of(pos
, struct nfs4_openowner
, oo_close_lru
);
3572 if (time_after((unsigned long)oo
->oo_time
, (unsigned long)cutoff
)) {
3573 t
= oo
->oo_time
- cutoff
;
3574 new_timeo
= min(new_timeo
, t
);
3577 release_openowner(oo
);
3579 new_timeo
= max_t(time_t, new_timeo
, NFSD_LAUNDROMAT_MINTIMEOUT
);
3580 nfs4_unlock_state();
3584 static struct workqueue_struct
*laundry_wq
;
3585 static void laundromat_main(struct work_struct
*);
3588 laundromat_main(struct work_struct
*laundry
)
3591 struct delayed_work
*dwork
= container_of(laundry
, struct delayed_work
,
3593 struct nfsd_net
*nn
= container_of(dwork
, struct nfsd_net
,
3596 t
= nfs4_laundromat(nn
);
3597 dprintk("NFSD: laundromat_main - sleeping for %ld seconds\n", t
);
3598 queue_delayed_work(laundry_wq
, &nn
->laundromat_work
, t
*HZ
);
3601 static inline __be32
nfs4_check_fh(struct svc_fh
*fhp
, struct nfs4_ol_stateid
*stp
)
3603 if (fhp
->fh_dentry
->d_inode
!= stp
->st_file
->fi_inode
)
3604 return nfserr_bad_stateid
;
3609 access_permit_read(struct nfs4_ol_stateid
*stp
)
3611 return test_access(NFS4_SHARE_ACCESS_READ
, stp
) ||
3612 test_access(NFS4_SHARE_ACCESS_BOTH
, stp
) ||
3613 test_access(NFS4_SHARE_ACCESS_WRITE
, stp
);
3617 access_permit_write(struct nfs4_ol_stateid
*stp
)
3619 return test_access(NFS4_SHARE_ACCESS_WRITE
, stp
) ||
3620 test_access(NFS4_SHARE_ACCESS_BOTH
, stp
);
3624 __be32
nfs4_check_openmode(struct nfs4_ol_stateid
*stp
, int flags
)
3626 __be32 status
= nfserr_openmode
;
3628 /* For lock stateid's, we test the parent open, not the lock: */
3629 if (stp
->st_openstp
)
3630 stp
= stp
->st_openstp
;
3631 if ((flags
& WR_STATE
) && !access_permit_write(stp
))
3633 if ((flags
& RD_STATE
) && !access_permit_read(stp
))
3640 static inline __be32
3641 check_special_stateids(struct net
*net
, svc_fh
*current_fh
, stateid_t
*stateid
, int flags
)
3643 if (ONE_STATEID(stateid
) && (flags
& RD_STATE
))
3645 else if (locks_in_grace(net
)) {
3646 /* Answer in remaining cases depends on existence of
3647 * conflicting state; so we must wait out the grace period. */
3648 return nfserr_grace
;
3649 } else if (flags
& WR_STATE
)
3650 return nfs4_share_conflict(current_fh
,
3651 NFS4_SHARE_DENY_WRITE
);
3652 else /* (flags & RD_STATE) && ZERO_STATEID(stateid) */
3653 return nfs4_share_conflict(current_fh
,
3654 NFS4_SHARE_DENY_READ
);
3658 * Allow READ/WRITE during grace period on recovered state only for files
3659 * that are not able to provide mandatory locking.
3662 grace_disallows_io(struct net
*net
, struct inode
*inode
)
3664 return locks_in_grace(net
) && mandatory_lock(inode
);
3667 /* Returns true iff a is later than b: */
3668 static bool stateid_generation_after(stateid_t
*a
, stateid_t
*b
)
3670 return (s32
)(a
->si_generation
- b
->si_generation
) > 0;
3673 static __be32
check_stateid_generation(stateid_t
*in
, stateid_t
*ref
, bool has_session
)
3676 * When sessions are used the stateid generation number is ignored
3679 if (has_session
&& in
->si_generation
== 0)
3682 if (in
->si_generation
== ref
->si_generation
)
3685 /* If the client sends us a stateid from the future, it's buggy: */
3686 if (stateid_generation_after(in
, ref
))
3687 return nfserr_bad_stateid
;
3689 * However, we could see a stateid from the past, even from a
3690 * non-buggy client. For example, if the client sends a lock
3691 * while some IO is outstanding, the lock may bump si_generation
3692 * while the IO is still in flight. The client could avoid that
3693 * situation by waiting for responses on all the IO requests,
3694 * but better performance may result in retrying IO that
3695 * receives an old_stateid error if requests are rarely
3696 * reordered in flight:
3698 return nfserr_old_stateid
;
3701 static __be32
nfsd4_validate_stateid(struct nfs4_client
*cl
, stateid_t
*stateid
)
3703 struct nfs4_stid
*s
;
3704 struct nfs4_ol_stateid
*ols
;
3707 if (ZERO_STATEID(stateid
) || ONE_STATEID(stateid
))
3708 return nfserr_bad_stateid
;
3709 /* Client debugging aid. */
3710 if (!same_clid(&stateid
->si_opaque
.so_clid
, &cl
->cl_clientid
)) {
3711 char addr_str
[INET6_ADDRSTRLEN
];
3712 rpc_ntop((struct sockaddr
*)&cl
->cl_addr
, addr_str
,
3714 pr_warn_ratelimited("NFSD: client %s testing state ID "
3715 "with incorrect client ID\n", addr_str
);
3716 return nfserr_bad_stateid
;
3718 s
= find_stateid(cl
, stateid
);
3720 return nfserr_bad_stateid
;
3721 status
= check_stateid_generation(stateid
, &s
->sc_stateid
, 1);
3724 switch (s
->sc_type
) {
3725 case NFS4_DELEG_STID
:
3727 case NFS4_REVOKED_DELEG_STID
:
3728 return nfserr_deleg_revoked
;
3729 case NFS4_OPEN_STID
:
3730 case NFS4_LOCK_STID
:
3731 ols
= openlockstateid(s
);
3732 if (ols
->st_stateowner
->so_is_open_owner
3733 && !(openowner(ols
->st_stateowner
)->oo_flags
3734 & NFS4_OO_CONFIRMED
))
3735 return nfserr_bad_stateid
;
3738 printk("unknown stateid type %x\n", s
->sc_type
);
3739 case NFS4_CLOSED_STID
:
3740 return nfserr_bad_stateid
;
3744 static __be32
nfsd4_lookup_stateid(stateid_t
*stateid
, unsigned char typemask
,
3745 struct nfs4_stid
**s
, bool sessions
,
3746 struct nfsd_net
*nn
)
3748 struct nfs4_client
*cl
;
3751 if (ZERO_STATEID(stateid
) || ONE_STATEID(stateid
))
3752 return nfserr_bad_stateid
;
3753 status
= lookup_clientid(&stateid
->si_opaque
.so_clid
, sessions
,
3755 if (status
== nfserr_stale_clientid
) {
3757 return nfserr_bad_stateid
;
3758 return nfserr_stale_stateid
;
3762 *s
= find_stateid_by_type(cl
, stateid
, typemask
);
3764 return nfserr_bad_stateid
;
3769 * Checks for stateid operations
3772 nfs4_preprocess_stateid_op(struct net
*net
, struct nfsd4_compound_state
*cstate
,
3773 stateid_t
*stateid
, int flags
, struct file
**filpp
)
3775 struct nfs4_stid
*s
;
3776 struct nfs4_ol_stateid
*stp
= NULL
;
3777 struct nfs4_delegation
*dp
= NULL
;
3778 struct svc_fh
*current_fh
= &cstate
->current_fh
;
3779 struct inode
*ino
= current_fh
->fh_dentry
->d_inode
;
3780 struct nfsd_net
*nn
= net_generic(net
, nfsd_net_id
);
3781 struct file
*file
= NULL
;
3787 if (grace_disallows_io(net
, ino
))
3788 return nfserr_grace
;
3790 if (ZERO_STATEID(stateid
) || ONE_STATEID(stateid
))
3791 return check_special_stateids(net
, current_fh
, stateid
, flags
);
3795 status
= nfsd4_lookup_stateid(stateid
, NFS4_DELEG_STID
|NFS4_OPEN_STID
|NFS4_LOCK_STID
,
3796 &s
, cstate
->minorversion
, nn
);
3799 status
= check_stateid_generation(stateid
, &s
->sc_stateid
, nfsd4_has_session(cstate
));
3802 switch (s
->sc_type
) {
3803 case NFS4_DELEG_STID
:
3804 dp
= delegstateid(s
);
3805 status
= nfs4_check_delegmode(dp
, flags
);
3809 file
= dp
->dl_file
->fi_deleg_file
;
3812 status
= nfserr_serverfault
;
3817 case NFS4_OPEN_STID
:
3818 case NFS4_LOCK_STID
:
3819 stp
= openlockstateid(s
);
3820 status
= nfs4_check_fh(current_fh
, stp
);
3823 if (stp
->st_stateowner
->so_is_open_owner
3824 && !(openowner(stp
->st_stateowner
)->oo_flags
& NFS4_OO_CONFIRMED
))
3826 status
= nfs4_check_openmode(stp
, flags
);
3830 if (flags
& RD_STATE
)
3831 file
= find_readable_file(stp
->st_file
);
3833 file
= find_writeable_file(stp
->st_file
);
3837 status
= nfserr_bad_stateid
;
3842 *filpp
= get_file(file
);
3844 nfs4_unlock_state();
3849 nfsd4_free_lock_stateid(struct nfs4_ol_stateid
*stp
)
3851 struct nfs4_lockowner
*lo
= lockowner(stp
->st_stateowner
);
3853 if (check_for_locks(stp
->st_file
, lo
))
3854 return nfserr_locks_held
;
3856 * Currently there's a 1-1 lock stateid<->lockowner
3857 * correspondance, and we have to delete the lockowner when we
3858 * delete the lock stateid:
3860 release_lockowner(lo
);
3865 * Test if the stateid is valid
3868 nfsd4_test_stateid(struct svc_rqst
*rqstp
, struct nfsd4_compound_state
*cstate
,
3869 struct nfsd4_test_stateid
*test_stateid
)
3871 struct nfsd4_test_stateid_id
*stateid
;
3872 struct nfs4_client
*cl
= cstate
->session
->se_client
;
3875 list_for_each_entry(stateid
, &test_stateid
->ts_stateid_list
, ts_id_list
)
3876 stateid
->ts_id_status
=
3877 nfsd4_validate_stateid(cl
, &stateid
->ts_id_stateid
);
3878 nfs4_unlock_state();
3884 nfsd4_free_stateid(struct svc_rqst
*rqstp
, struct nfsd4_compound_state
*cstate
,
3885 struct nfsd4_free_stateid
*free_stateid
)
3887 stateid_t
*stateid
= &free_stateid
->fr_stateid
;
3888 struct nfs4_stid
*s
;
3889 struct nfs4_delegation
*dp
;
3890 struct nfs4_client
*cl
= cstate
->session
->se_client
;
3891 __be32 ret
= nfserr_bad_stateid
;
3894 s
= find_stateid(cl
, stateid
);
3897 switch (s
->sc_type
) {
3898 case NFS4_DELEG_STID
:
3899 ret
= nfserr_locks_held
;
3901 case NFS4_OPEN_STID
:
3902 case NFS4_LOCK_STID
:
3903 ret
= check_stateid_generation(stateid
, &s
->sc_stateid
, 1);
3906 if (s
->sc_type
== NFS4_LOCK_STID
)
3907 ret
= nfsd4_free_lock_stateid(openlockstateid(s
));
3909 ret
= nfserr_locks_held
;
3911 case NFS4_REVOKED_DELEG_STID
:
3912 dp
= delegstateid(s
);
3913 destroy_revoked_delegation(dp
);
3917 ret
= nfserr_bad_stateid
;
3920 nfs4_unlock_state();
3927 return (type
== NFS4_READW_LT
|| type
== NFS4_READ_LT
) ?
3928 RD_STATE
: WR_STATE
;
3931 static __be32
nfs4_seqid_op_checks(struct nfsd4_compound_state
*cstate
, stateid_t
*stateid
, u32 seqid
, struct nfs4_ol_stateid
*stp
)
3933 struct svc_fh
*current_fh
= &cstate
->current_fh
;
3934 struct nfs4_stateowner
*sop
= stp
->st_stateowner
;
3937 status
= nfsd4_check_seqid(cstate
, sop
, seqid
);
3940 if (stp
->st_stid
.sc_type
== NFS4_CLOSED_STID
3941 || stp
->st_stid
.sc_type
== NFS4_REVOKED_DELEG_STID
)
3943 * "Closed" stateid's exist *only* to return
3944 * nfserr_replay_me from the previous step, and
3945 * revoked delegations are kept only for free_stateid.
3947 return nfserr_bad_stateid
;
3948 status
= check_stateid_generation(stateid
, &stp
->st_stid
.sc_stateid
, nfsd4_has_session(cstate
));
3951 return nfs4_check_fh(current_fh
, stp
);
3955 * Checks for sequence id mutating operations.
3958 nfs4_preprocess_seqid_op(struct nfsd4_compound_state
*cstate
, u32 seqid
,
3959 stateid_t
*stateid
, char typemask
,
3960 struct nfs4_ol_stateid
**stpp
,
3961 struct nfsd_net
*nn
)
3964 struct nfs4_stid
*s
;
3965 struct nfs4_ol_stateid
*stp
= NULL
;
3967 dprintk("NFSD: %s: seqid=%d stateid = " STATEID_FMT
"\n", __func__
,
3968 seqid
, STATEID_VAL(stateid
));
3971 status
= nfsd4_lookup_stateid(stateid
, typemask
, &s
,
3972 cstate
->minorversion
, nn
);
3975 stp
= openlockstateid(s
);
3976 if (!nfsd4_has_session(cstate
))
3977 cstate
->replay_owner
= stp
->st_stateowner
;
3979 status
= nfs4_seqid_op_checks(cstate
, stateid
, seqid
, stp
);
3985 static __be32
nfs4_preprocess_confirmed_seqid_op(struct nfsd4_compound_state
*cstate
, u32 seqid
,
3986 stateid_t
*stateid
, struct nfs4_ol_stateid
**stpp
, struct nfsd_net
*nn
)
3989 struct nfs4_openowner
*oo
;
3991 status
= nfs4_preprocess_seqid_op(cstate
, seqid
, stateid
,
3992 NFS4_OPEN_STID
, stpp
, nn
);
3995 oo
= openowner((*stpp
)->st_stateowner
);
3996 if (!(oo
->oo_flags
& NFS4_OO_CONFIRMED
))
3997 return nfserr_bad_stateid
;
4002 nfsd4_open_confirm(struct svc_rqst
*rqstp
, struct nfsd4_compound_state
*cstate
,
4003 struct nfsd4_open_confirm
*oc
)
4006 struct nfs4_openowner
*oo
;
4007 struct nfs4_ol_stateid
*stp
;
4008 struct nfsd_net
*nn
= net_generic(SVC_NET(rqstp
), nfsd_net_id
);
4010 dprintk("NFSD: nfsd4_open_confirm on file %pd\n",
4011 cstate
->current_fh
.fh_dentry
);
4013 status
= fh_verify(rqstp
, &cstate
->current_fh
, S_IFREG
, 0);
4019 status
= nfs4_preprocess_seqid_op(cstate
,
4020 oc
->oc_seqid
, &oc
->oc_req_stateid
,
4021 NFS4_OPEN_STID
, &stp
, nn
);
4024 oo
= openowner(stp
->st_stateowner
);
4025 status
= nfserr_bad_stateid
;
4026 if (oo
->oo_flags
& NFS4_OO_CONFIRMED
)
4028 oo
->oo_flags
|= NFS4_OO_CONFIRMED
;
4029 update_stateid(&stp
->st_stid
.sc_stateid
);
4030 memcpy(&oc
->oc_resp_stateid
, &stp
->st_stid
.sc_stateid
, sizeof(stateid_t
));
4031 dprintk("NFSD: %s: success, seqid=%d stateid=" STATEID_FMT
"\n",
4032 __func__
, oc
->oc_seqid
, STATEID_VAL(&stp
->st_stid
.sc_stateid
));
4034 nfsd4_client_record_create(oo
->oo_owner
.so_client
);
4037 nfsd4_bump_seqid(cstate
, status
);
4038 if (!cstate
->replay_owner
)
4039 nfs4_unlock_state();
4043 static inline void nfs4_stateid_downgrade_bit(struct nfs4_ol_stateid
*stp
, u32 access
)
4045 if (!test_access(access
, stp
))
4047 nfs4_file_put_access(stp
->st_file
, nfs4_access_to_omode(access
));
4048 clear_access(access
, stp
);
4051 static inline void nfs4_stateid_downgrade(struct nfs4_ol_stateid
*stp
, u32 to_access
)
4053 switch (to_access
) {
4054 case NFS4_SHARE_ACCESS_READ
:
4055 nfs4_stateid_downgrade_bit(stp
, NFS4_SHARE_ACCESS_WRITE
);
4056 nfs4_stateid_downgrade_bit(stp
, NFS4_SHARE_ACCESS_BOTH
);
4058 case NFS4_SHARE_ACCESS_WRITE
:
4059 nfs4_stateid_downgrade_bit(stp
, NFS4_SHARE_ACCESS_READ
);
4060 nfs4_stateid_downgrade_bit(stp
, NFS4_SHARE_ACCESS_BOTH
);
4062 case NFS4_SHARE_ACCESS_BOTH
:
4070 reset_union_bmap_deny(unsigned long deny
, struct nfs4_ol_stateid
*stp
)
4073 for (i
= 0; i
< 4; i
++) {
4074 if ((i
& deny
) != i
)
4080 nfsd4_open_downgrade(struct svc_rqst
*rqstp
,
4081 struct nfsd4_compound_state
*cstate
,
4082 struct nfsd4_open_downgrade
*od
)
4085 struct nfs4_ol_stateid
*stp
;
4086 struct nfsd_net
*nn
= net_generic(SVC_NET(rqstp
), nfsd_net_id
);
4088 dprintk("NFSD: nfsd4_open_downgrade on file %pd\n",
4089 cstate
->current_fh
.fh_dentry
);
4091 /* We don't yet support WANT bits: */
4092 if (od
->od_deleg_want
)
4093 dprintk("NFSD: %s: od_deleg_want=0x%x ignored\n", __func__
,
4097 status
= nfs4_preprocess_confirmed_seqid_op(cstate
, od
->od_seqid
,
4098 &od
->od_stateid
, &stp
, nn
);
4101 status
= nfserr_inval
;
4102 if (!test_access(od
->od_share_access
, stp
)) {
4103 dprintk("NFSD: access not a subset current bitmap: 0x%lx, input access=%08x\n",
4104 stp
->st_access_bmap
, od
->od_share_access
);
4107 if (!test_deny(od
->od_share_deny
, stp
)) {
4108 dprintk("NFSD:deny not a subset current bitmap: 0x%lx, input deny=%08x\n",
4109 stp
->st_deny_bmap
, od
->od_share_deny
);
4112 nfs4_stateid_downgrade(stp
, od
->od_share_access
);
4114 reset_union_bmap_deny(od
->od_share_deny
, stp
);
4116 update_stateid(&stp
->st_stid
.sc_stateid
);
4117 memcpy(&od
->od_stateid
, &stp
->st_stid
.sc_stateid
, sizeof(stateid_t
));
4120 nfsd4_bump_seqid(cstate
, status
);
4121 if (!cstate
->replay_owner
)
4122 nfs4_unlock_state();
4126 static void nfsd4_close_open_stateid(struct nfs4_ol_stateid
*s
)
4128 unhash_open_stateid(s
);
4129 s
->st_stid
.sc_type
= NFS4_CLOSED_STID
;
4133 * nfs4_unlock_state() called after encode
4136 nfsd4_close(struct svc_rqst
*rqstp
, struct nfsd4_compound_state
*cstate
,
4137 struct nfsd4_close
*close
)
4140 struct nfs4_openowner
*oo
;
4141 struct nfs4_ol_stateid
*stp
;
4142 struct net
*net
= SVC_NET(rqstp
);
4143 struct nfsd_net
*nn
= net_generic(net
, nfsd_net_id
);
4145 dprintk("NFSD: nfsd4_close on file %pd\n",
4146 cstate
->current_fh
.fh_dentry
);
4149 status
= nfs4_preprocess_seqid_op(cstate
, close
->cl_seqid
,
4151 NFS4_OPEN_STID
|NFS4_CLOSED_STID
,
4153 nfsd4_bump_seqid(cstate
, status
);
4156 oo
= openowner(stp
->st_stateowner
);
4157 update_stateid(&stp
->st_stid
.sc_stateid
);
4158 memcpy(&close
->cl_stateid
, &stp
->st_stid
.sc_stateid
, sizeof(stateid_t
));
4160 nfsd4_close_open_stateid(stp
);
4162 if (cstate
->minorversion
)
4163 free_generic_stateid(stp
);
4165 oo
->oo_last_closed_stid
= stp
;
4167 if (list_empty(&oo
->oo_owner
.so_stateids
)) {
4168 if (cstate
->minorversion
)
4169 release_openowner(oo
);
4172 * In the 4.0 case we need to keep the owners around a
4173 * little while to handle CLOSE replay.
4175 move_to_close_lru(oo
, SVC_NET(rqstp
));
4179 if (!cstate
->replay_owner
)
4180 nfs4_unlock_state();
4185 nfsd4_delegreturn(struct svc_rqst
*rqstp
, struct nfsd4_compound_state
*cstate
,
4186 struct nfsd4_delegreturn
*dr
)
4188 struct nfs4_delegation
*dp
;
4189 stateid_t
*stateid
= &dr
->dr_stateid
;
4190 struct nfs4_stid
*s
;
4192 struct nfsd_net
*nn
= net_generic(SVC_NET(rqstp
), nfsd_net_id
);
4194 if ((status
= fh_verify(rqstp
, &cstate
->current_fh
, S_IFREG
, 0)))
4198 status
= nfsd4_lookup_stateid(stateid
, NFS4_DELEG_STID
, &s
,
4199 cstate
->minorversion
, nn
);
4202 dp
= delegstateid(s
);
4203 status
= check_stateid_generation(stateid
, &dp
->dl_stid
.sc_stateid
, nfsd4_has_session(cstate
));
4207 destroy_delegation(dp
);
4209 nfs4_unlock_state();
4215 #define LOFF_OVERFLOW(start, len) ((u64)(len) > ~(u64)(start))
4217 #define LOCKOWNER_INO_HASH_MASK (LOCKOWNER_INO_HASH_SIZE - 1)
4220 end_offset(u64 start
, u64 len
)
4225 return end
>= start
? end
: NFS4_MAX_UINT64
;
4228 /* last octet in a range */
4230 last_byte_offset(u64 start
, u64 len
)
4236 return end
> start
? end
- 1: NFS4_MAX_UINT64
;
4239 static unsigned int lockowner_ino_hashval(struct inode
*inode
, u32 cl_id
, struct xdr_netobj
*ownername
)
4241 return (file_hashval(inode
) + cl_id
4242 + opaque_hashval(ownername
->data
, ownername
->len
))
4243 & LOCKOWNER_INO_HASH_MASK
;
4247 * TODO: Linux file offsets are _signed_ 64-bit quantities, which means that
4248 * we can't properly handle lock requests that go beyond the (2^63 - 1)-th
4249 * byte, because of sign extension problems. Since NFSv4 calls for 64-bit
4250 * locking, this prevents us from being completely protocol-compliant. The
4251 * real solution to this problem is to start using unsigned file offsets in
4252 * the VFS, but this is a very deep change!
4255 nfs4_transform_lock_offset(struct file_lock
*lock
)
4257 if (lock
->fl_start
< 0)
4258 lock
->fl_start
= OFFSET_MAX
;
4259 if (lock
->fl_end
< 0)
4260 lock
->fl_end
= OFFSET_MAX
;
4263 /* Hack!: For now, we're defining this just so we can use a pointer to it
4264 * as a unique cookie to identify our (NFSv4's) posix locks. */
4265 static const struct lock_manager_operations nfsd_posix_mng_ops
= {
4269 nfs4_set_lock_denied(struct file_lock
*fl
, struct nfsd4_lock_denied
*deny
)
4271 struct nfs4_lockowner
*lo
;
4273 if (fl
->fl_lmops
== &nfsd_posix_mng_ops
) {
4274 lo
= (struct nfs4_lockowner
*) fl
->fl_owner
;
4275 deny
->ld_owner
.data
= kmemdup(lo
->lo_owner
.so_owner
.data
,
4276 lo
->lo_owner
.so_owner
.len
, GFP_KERNEL
);
4277 if (!deny
->ld_owner
.data
)
4278 /* We just don't care that much */
4280 deny
->ld_owner
.len
= lo
->lo_owner
.so_owner
.len
;
4281 deny
->ld_clientid
= lo
->lo_owner
.so_client
->cl_clientid
;
4284 deny
->ld_owner
.len
= 0;
4285 deny
->ld_owner
.data
= NULL
;
4286 deny
->ld_clientid
.cl_boot
= 0;
4287 deny
->ld_clientid
.cl_id
= 0;
4289 deny
->ld_start
= fl
->fl_start
;
4290 deny
->ld_length
= NFS4_MAX_UINT64
;
4291 if (fl
->fl_end
!= NFS4_MAX_UINT64
)
4292 deny
->ld_length
= fl
->fl_end
- fl
->fl_start
+ 1;
4293 deny
->ld_type
= NFS4_READ_LT
;
4294 if (fl
->fl_type
!= F_RDLCK
)
4295 deny
->ld_type
= NFS4_WRITE_LT
;
4298 static bool same_lockowner_ino(struct nfs4_lockowner
*lo
, struct inode
*inode
, clientid_t
*clid
, struct xdr_netobj
*owner
)
4300 struct nfs4_ol_stateid
*lst
;
4302 if (!same_owner_str(&lo
->lo_owner
, owner
, clid
))
4304 if (list_empty(&lo
->lo_owner
.so_stateids
)) {
4308 lst
= list_first_entry(&lo
->lo_owner
.so_stateids
,
4309 struct nfs4_ol_stateid
, st_perstateowner
);
4310 return lst
->st_file
->fi_inode
== inode
;
4313 static struct nfs4_lockowner
*
4314 find_lockowner_str(struct inode
*inode
, clientid_t
*clid
,
4315 struct xdr_netobj
*owner
, struct nfsd_net
*nn
)
4317 unsigned int hashval
= lockowner_ino_hashval(inode
, clid
->cl_id
, owner
);
4318 struct nfs4_lockowner
*lo
;
4320 list_for_each_entry(lo
, &nn
->lockowner_ino_hashtbl
[hashval
], lo_owner_ino_hash
) {
4321 if (same_lockowner_ino(lo
, inode
, clid
, owner
))
4327 static void hash_lockowner(struct nfs4_lockowner
*lo
, unsigned int strhashval
, struct nfs4_client
*clp
, struct nfs4_ol_stateid
*open_stp
)
4329 struct inode
*inode
= open_stp
->st_file
->fi_inode
;
4330 unsigned int inohash
= lockowner_ino_hashval(inode
,
4331 clp
->cl_clientid
.cl_id
, &lo
->lo_owner
.so_owner
);
4332 struct nfsd_net
*nn
= net_generic(clp
->net
, nfsd_net_id
);
4334 list_add(&lo
->lo_owner
.so_strhash
, &nn
->ownerstr_hashtbl
[strhashval
]);
4335 list_add(&lo
->lo_owner_ino_hash
, &nn
->lockowner_ino_hashtbl
[inohash
]);
4336 list_add(&lo
->lo_perstateid
, &open_stp
->st_lockowners
);
4340 * Alloc a lock owner structure.
4341 * Called in nfsd4_lock - therefore, OPEN and OPEN_CONFIRM (if needed) has
4344 * strhashval = ownerstr_hashval
4347 static struct nfs4_lockowner
*
4348 alloc_init_lock_stateowner(unsigned int strhashval
, struct nfs4_client
*clp
, struct nfs4_ol_stateid
*open_stp
, struct nfsd4_lock
*lock
) {
4349 struct nfs4_lockowner
*lo
;
4351 lo
= alloc_stateowner(lockowner_slab
, &lock
->lk_new_owner
, clp
);
4354 INIT_LIST_HEAD(&lo
->lo_owner
.so_stateids
);
4355 lo
->lo_owner
.so_is_open_owner
= 0;
4356 /* It is the openowner seqid that will be incremented in encode in the
4357 * case of new lockowners; so increment the lock seqid manually: */
4358 lo
->lo_owner
.so_seqid
= lock
->lk_new_lock_seqid
+ 1;
4359 hash_lockowner(lo
, strhashval
, clp
, open_stp
);
4363 static struct nfs4_ol_stateid
*
4364 alloc_init_lock_stateid(struct nfs4_lockowner
*lo
, struct nfs4_file
*fp
, struct nfs4_ol_stateid
*open_stp
)
4366 struct nfs4_ol_stateid
*stp
;
4367 struct nfs4_client
*clp
= lo
->lo_owner
.so_client
;
4369 stp
= nfs4_alloc_stateid(clp
);
4372 stp
->st_stid
.sc_type
= NFS4_LOCK_STID
;
4373 list_add(&stp
->st_perfile
, &fp
->fi_stateids
);
4374 list_add(&stp
->st_perstateowner
, &lo
->lo_owner
.so_stateids
);
4375 stp
->st_stateowner
= &lo
->lo_owner
;
4378 stp
->st_access_bmap
= 0;
4379 stp
->st_deny_bmap
= open_stp
->st_deny_bmap
;
4380 stp
->st_openstp
= open_stp
;
4385 check_lock_length(u64 offset
, u64 length
)
4387 return ((length
== 0) || ((length
!= NFS4_MAX_UINT64
) &&
4388 LOFF_OVERFLOW(offset
, length
)));
4391 static void get_lock_access(struct nfs4_ol_stateid
*lock_stp
, u32 access
)
4393 struct nfs4_file
*fp
= lock_stp
->st_file
;
4394 int oflag
= nfs4_access_to_omode(access
);
4396 if (test_access(access
, lock_stp
))
4398 nfs4_file_get_access(fp
, oflag
);
4399 set_access(access
, lock_stp
);
4402 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)
4404 struct nfs4_file
*fi
= ost
->st_file
;
4405 struct nfs4_openowner
*oo
= openowner(ost
->st_stateowner
);
4406 struct nfs4_client
*cl
= oo
->oo_owner
.so_client
;
4407 struct nfs4_lockowner
*lo
;
4408 unsigned int strhashval
;
4409 struct nfsd_net
*nn
= net_generic(cl
->net
, nfsd_net_id
);
4411 lo
= find_lockowner_str(fi
->fi_inode
, &cl
->cl_clientid
,
4412 &lock
->v
.new.owner
, nn
);
4414 if (!cstate
->minorversion
)
4415 return nfserr_bad_seqid
;
4416 /* XXX: a lockowner always has exactly one stateid: */
4417 *lst
= list_first_entry(&lo
->lo_owner
.so_stateids
,
4418 struct nfs4_ol_stateid
, st_perstateowner
);
4421 strhashval
= ownerstr_hashval(cl
->cl_clientid
.cl_id
,
4422 &lock
->v
.new.owner
);
4423 lo
= alloc_init_lock_stateowner(strhashval
, cl
, ost
, lock
);
4425 return nfserr_jukebox
;
4426 *lst
= alloc_init_lock_stateid(lo
, fi
, ost
);
4428 release_lockowner(lo
);
4429 return nfserr_jukebox
;
4439 nfsd4_lock(struct svc_rqst
*rqstp
, struct nfsd4_compound_state
*cstate
,
4440 struct nfsd4_lock
*lock
)
4442 struct nfs4_openowner
*open_sop
= NULL
;
4443 struct nfs4_lockowner
*lock_sop
= NULL
;
4444 struct nfs4_ol_stateid
*lock_stp
;
4445 struct file
*filp
= NULL
;
4446 struct file_lock
*file_lock
= NULL
;
4447 struct file_lock
*conflock
= NULL
;
4449 bool new_state
= false;
4452 struct net
*net
= SVC_NET(rqstp
);
4453 struct nfsd_net
*nn
= net_generic(net
, nfsd_net_id
);
4455 dprintk("NFSD: nfsd4_lock: start=%Ld length=%Ld\n",
4456 (long long) lock
->lk_offset
,
4457 (long long) lock
->lk_length
);
4459 if (check_lock_length(lock
->lk_offset
, lock
->lk_length
))
4460 return nfserr_inval
;
4462 if ((status
= fh_verify(rqstp
, &cstate
->current_fh
,
4463 S_IFREG
, NFSD_MAY_LOCK
))) {
4464 dprintk("NFSD: nfsd4_lock: permission denied!\n");
4470 if (lock
->lk_is_new
) {
4471 struct nfs4_ol_stateid
*open_stp
= NULL
;
4473 if (nfsd4_has_session(cstate
))
4474 /* See rfc 5661 18.10.3: given clientid is ignored: */
4475 memcpy(&lock
->v
.new.clientid
,
4476 &cstate
->session
->se_client
->cl_clientid
,
4477 sizeof(clientid_t
));
4479 status
= nfserr_stale_clientid
;
4480 if (STALE_CLIENTID(&lock
->lk_new_clientid
, nn
))
4483 /* validate and update open stateid and open seqid */
4484 status
= nfs4_preprocess_confirmed_seqid_op(cstate
,
4485 lock
->lk_new_open_seqid
,
4486 &lock
->lk_new_open_stateid
,
4490 open_sop
= openowner(open_stp
->st_stateowner
);
4491 status
= nfserr_bad_stateid
;
4492 if (!same_clid(&open_sop
->oo_owner
.so_client
->cl_clientid
,
4493 &lock
->v
.new.clientid
))
4495 status
= lookup_or_create_lock_state(cstate
, open_stp
, lock
,
4496 &lock_stp
, &new_state
);
4498 status
= nfs4_preprocess_seqid_op(cstate
,
4499 lock
->lk_old_lock_seqid
,
4500 &lock
->lk_old_lock_stateid
,
4501 NFS4_LOCK_STID
, &lock_stp
, nn
);
4504 lock_sop
= lockowner(lock_stp
->st_stateowner
);
4506 lkflg
= setlkflg(lock
->lk_type
);
4507 status
= nfs4_check_openmode(lock_stp
, lkflg
);
4511 status
= nfserr_grace
;
4512 if (locks_in_grace(net
) && !lock
->lk_reclaim
)
4514 status
= nfserr_no_grace
;
4515 if (!locks_in_grace(net
) && lock
->lk_reclaim
)
4518 file_lock
= locks_alloc_lock();
4520 dprintk("NFSD: %s: unable to allocate lock!\n", __func__
);
4521 status
= nfserr_jukebox
;
4525 locks_init_lock(file_lock
);
4526 switch (lock
->lk_type
) {
4529 filp
= find_readable_file(lock_stp
->st_file
);
4531 get_lock_access(lock_stp
, NFS4_SHARE_ACCESS_READ
);
4532 file_lock
->fl_type
= F_RDLCK
;
4535 case NFS4_WRITEW_LT
:
4536 filp
= find_writeable_file(lock_stp
->st_file
);
4538 get_lock_access(lock_stp
, NFS4_SHARE_ACCESS_WRITE
);
4539 file_lock
->fl_type
= F_WRLCK
;
4542 status
= nfserr_inval
;
4546 status
= nfserr_openmode
;
4549 file_lock
->fl_owner
= (fl_owner_t
)lock_sop
;
4550 file_lock
->fl_pid
= current
->tgid
;
4551 file_lock
->fl_file
= filp
;
4552 file_lock
->fl_flags
= FL_POSIX
;
4553 file_lock
->fl_lmops
= &nfsd_posix_mng_ops
;
4554 file_lock
->fl_start
= lock
->lk_offset
;
4555 file_lock
->fl_end
= last_byte_offset(lock
->lk_offset
, lock
->lk_length
);
4556 nfs4_transform_lock_offset(file_lock
);
4558 conflock
= locks_alloc_lock();
4560 dprintk("NFSD: %s: unable to allocate lock!\n", __func__
);
4561 status
= nfserr_jukebox
;
4565 err
= vfs_lock_file(filp
, F_SETLK
, file_lock
, conflock
);
4567 case 0: /* success! */
4568 update_stateid(&lock_stp
->st_stid
.sc_stateid
);
4569 memcpy(&lock
->lk_resp_stateid
, &lock_stp
->st_stid
.sc_stateid
,
4573 case (EAGAIN
): /* conflock holds conflicting lock */
4574 status
= nfserr_denied
;
4575 dprintk("NFSD: nfsd4_lock: conflicting lock found!\n");
4576 nfs4_set_lock_denied(conflock
, &lock
->lk_denied
);
4579 status
= nfserr_deadlock
;
4582 dprintk("NFSD: nfsd4_lock: vfs_lock_file() failed! status %d\n",err
);
4583 status
= nfserrno(err
);
4587 if (status
&& new_state
)
4588 release_lockowner(lock_sop
);
4589 nfsd4_bump_seqid(cstate
, status
);
4590 if (!cstate
->replay_owner
)
4591 nfs4_unlock_state();
4593 locks_free_lock(file_lock
);
4595 locks_free_lock(conflock
);
4600 * The NFSv4 spec allows a client to do a LOCKT without holding an OPEN,
4601 * so we do a temporary open here just to get an open file to pass to
4602 * vfs_test_lock. (Arguably perhaps test_lock should be done with an
4605 static __be32
nfsd_test_lock(struct svc_rqst
*rqstp
, struct svc_fh
*fhp
, struct file_lock
*lock
)
4608 __be32 err
= nfsd_open(rqstp
, fhp
, S_IFREG
, NFSD_MAY_READ
, &file
);
4610 err
= nfserrno(vfs_test_lock(file
, lock
));
4620 nfsd4_lockt(struct svc_rqst
*rqstp
, struct nfsd4_compound_state
*cstate
,
4621 struct nfsd4_lockt
*lockt
)
4623 struct inode
*inode
;
4624 struct file_lock
*file_lock
= NULL
;
4625 struct nfs4_lockowner
*lo
;
4627 struct nfsd_net
*nn
= net_generic(SVC_NET(rqstp
), nfsd_net_id
);
4629 if (locks_in_grace(SVC_NET(rqstp
)))
4630 return nfserr_grace
;
4632 if (check_lock_length(lockt
->lt_offset
, lockt
->lt_length
))
4633 return nfserr_inval
;
4637 if (!nfsd4_has_session(cstate
)) {
4638 status
= lookup_clientid(&lockt
->lt_clientid
, false, nn
, NULL
);
4643 if ((status
= fh_verify(rqstp
, &cstate
->current_fh
, S_IFREG
, 0)))
4646 inode
= cstate
->current_fh
.fh_dentry
->d_inode
;
4647 file_lock
= locks_alloc_lock();
4649 dprintk("NFSD: %s: unable to allocate lock!\n", __func__
);
4650 status
= nfserr_jukebox
;
4653 locks_init_lock(file_lock
);
4654 switch (lockt
->lt_type
) {
4657 file_lock
->fl_type
= F_RDLCK
;
4660 case NFS4_WRITEW_LT
:
4661 file_lock
->fl_type
= F_WRLCK
;
4664 dprintk("NFSD: nfs4_lockt: bad lock type!\n");
4665 status
= nfserr_inval
;
4669 lo
= find_lockowner_str(inode
, &lockt
->lt_clientid
, &lockt
->lt_owner
, nn
);
4671 file_lock
->fl_owner
= (fl_owner_t
)lo
;
4672 file_lock
->fl_pid
= current
->tgid
;
4673 file_lock
->fl_flags
= FL_POSIX
;
4675 file_lock
->fl_start
= lockt
->lt_offset
;
4676 file_lock
->fl_end
= last_byte_offset(lockt
->lt_offset
, lockt
->lt_length
);
4678 nfs4_transform_lock_offset(file_lock
);
4680 status
= nfsd_test_lock(rqstp
, &cstate
->current_fh
, file_lock
);
4684 if (file_lock
->fl_type
!= F_UNLCK
) {
4685 status
= nfserr_denied
;
4686 nfs4_set_lock_denied(file_lock
, &lockt
->lt_denied
);
4689 nfs4_unlock_state();
4691 locks_free_lock(file_lock
);
4696 nfsd4_locku(struct svc_rqst
*rqstp
, struct nfsd4_compound_state
*cstate
,
4697 struct nfsd4_locku
*locku
)
4699 struct nfs4_ol_stateid
*stp
;
4700 struct file
*filp
= NULL
;
4701 struct file_lock
*file_lock
= NULL
;
4704 struct nfsd_net
*nn
= net_generic(SVC_NET(rqstp
), nfsd_net_id
);
4706 dprintk("NFSD: nfsd4_locku: start=%Ld length=%Ld\n",
4707 (long long) locku
->lu_offset
,
4708 (long long) locku
->lu_length
);
4710 if (check_lock_length(locku
->lu_offset
, locku
->lu_length
))
4711 return nfserr_inval
;
4715 status
= nfs4_preprocess_seqid_op(cstate
, locku
->lu_seqid
,
4716 &locku
->lu_stateid
, NFS4_LOCK_STID
,
4720 filp
= find_any_file(stp
->st_file
);
4722 status
= nfserr_lock_range
;
4725 file_lock
= locks_alloc_lock();
4727 dprintk("NFSD: %s: unable to allocate lock!\n", __func__
);
4728 status
= nfserr_jukebox
;
4731 locks_init_lock(file_lock
);
4732 file_lock
->fl_type
= F_UNLCK
;
4733 file_lock
->fl_owner
= (fl_owner_t
)lockowner(stp
->st_stateowner
);
4734 file_lock
->fl_pid
= current
->tgid
;
4735 file_lock
->fl_file
= filp
;
4736 file_lock
->fl_flags
= FL_POSIX
;
4737 file_lock
->fl_lmops
= &nfsd_posix_mng_ops
;
4738 file_lock
->fl_start
= locku
->lu_offset
;
4740 file_lock
->fl_end
= last_byte_offset(locku
->lu_offset
,
4742 nfs4_transform_lock_offset(file_lock
);
4744 err
= vfs_lock_file(filp
, F_SETLK
, file_lock
, NULL
);
4746 dprintk("NFSD: nfs4_locku: vfs_lock_file failed!\n");
4749 update_stateid(&stp
->st_stid
.sc_stateid
);
4750 memcpy(&locku
->lu_stateid
, &stp
->st_stid
.sc_stateid
, sizeof(stateid_t
));
4753 nfsd4_bump_seqid(cstate
, status
);
4754 if (!cstate
->replay_owner
)
4755 nfs4_unlock_state();
4757 locks_free_lock(file_lock
);
4761 status
= nfserrno(err
);
4767 * 1: locks held by lockowner
4768 * 0: no locks held by lockowner
4771 check_for_locks(struct nfs4_file
*filp
, struct nfs4_lockowner
*lowner
)
4773 struct file_lock
**flpp
;
4774 struct inode
*inode
= filp
->fi_inode
;
4777 spin_lock(&inode
->i_lock
);
4778 for (flpp
= &inode
->i_flock
; *flpp
!= NULL
; flpp
= &(*flpp
)->fl_next
) {
4779 if ((*flpp
)->fl_owner
== (fl_owner_t
)lowner
) {
4785 spin_unlock(&inode
->i_lock
);
4790 nfsd4_release_lockowner(struct svc_rqst
*rqstp
,
4791 struct nfsd4_compound_state
*cstate
,
4792 struct nfsd4_release_lockowner
*rlockowner
)
4794 clientid_t
*clid
= &rlockowner
->rl_clientid
;
4795 struct nfs4_stateowner
*sop
;
4796 struct nfs4_lockowner
*lo
;
4797 struct nfs4_ol_stateid
*stp
;
4798 struct xdr_netobj
*owner
= &rlockowner
->rl_owner
;
4799 struct list_head matches
;
4800 unsigned int hashval
= ownerstr_hashval(clid
->cl_id
, owner
);
4802 struct nfsd_net
*nn
= net_generic(SVC_NET(rqstp
), nfsd_net_id
);
4804 dprintk("nfsd4_release_lockowner clientid: (%08x/%08x):\n",
4805 clid
->cl_boot
, clid
->cl_id
);
4809 status
= lookup_clientid(clid
, cstate
->minorversion
, nn
, NULL
);
4813 status
= nfserr_locks_held
;
4814 INIT_LIST_HEAD(&matches
);
4816 list_for_each_entry(sop
, &nn
->ownerstr_hashtbl
[hashval
], so_strhash
) {
4817 if (sop
->so_is_open_owner
)
4819 if (!same_owner_str(sop
, owner
, clid
))
4821 list_for_each_entry(stp
, &sop
->so_stateids
,
4823 lo
= lockowner(sop
);
4824 if (check_for_locks(stp
->st_file
, lo
))
4826 list_add(&lo
->lo_list
, &matches
);
4829 /* Clients probably won't expect us to return with some (but not all)
4830 * of the lockowner state released; so don't release any until all
4831 * have been checked. */
4833 while (!list_empty(&matches
)) {
4834 lo
= list_entry(matches
.next
, struct nfs4_lockowner
,
4836 /* unhash_stateowner deletes so_perclient only
4837 * for openowners. */
4838 list_del(&lo
->lo_list
);
4839 release_lockowner(lo
);
4842 nfs4_unlock_state();
4846 static inline struct nfs4_client_reclaim
*
4849 return kmalloc(sizeof(struct nfs4_client_reclaim
), GFP_KERNEL
);
4853 nfs4_has_reclaimed_state(const char *name
, struct nfsd_net
*nn
)
4855 struct nfs4_client_reclaim
*crp
;
4857 crp
= nfsd4_find_reclaim_client(name
, nn
);
4858 return (crp
&& crp
->cr_clp
);
4862 * failure => all reset bets are off, nfserr_no_grace...
4864 struct nfs4_client_reclaim
*
4865 nfs4_client_to_reclaim(const char *name
, struct nfsd_net
*nn
)
4867 unsigned int strhashval
;
4868 struct nfs4_client_reclaim
*crp
;
4870 dprintk("NFSD nfs4_client_to_reclaim NAME: %.*s\n", HEXDIR_LEN
, name
);
4871 crp
= alloc_reclaim();
4873 strhashval
= clientstr_hashval(name
);
4874 INIT_LIST_HEAD(&crp
->cr_strhash
);
4875 list_add(&crp
->cr_strhash
, &nn
->reclaim_str_hashtbl
[strhashval
]);
4876 memcpy(crp
->cr_recdir
, name
, HEXDIR_LEN
);
4878 nn
->reclaim_str_hashtbl_size
++;
4884 nfs4_remove_reclaim_record(struct nfs4_client_reclaim
*crp
, struct nfsd_net
*nn
)
4886 list_del(&crp
->cr_strhash
);
4888 nn
->reclaim_str_hashtbl_size
--;
4892 nfs4_release_reclaim(struct nfsd_net
*nn
)
4894 struct nfs4_client_reclaim
*crp
= NULL
;
4897 for (i
= 0; i
< CLIENT_HASH_SIZE
; i
++) {
4898 while (!list_empty(&nn
->reclaim_str_hashtbl
[i
])) {
4899 crp
= list_entry(nn
->reclaim_str_hashtbl
[i
].next
,
4900 struct nfs4_client_reclaim
, cr_strhash
);
4901 nfs4_remove_reclaim_record(crp
, nn
);
4904 WARN_ON_ONCE(nn
->reclaim_str_hashtbl_size
);
4908 * called from OPEN, CLAIM_PREVIOUS with a new clientid. */
4909 struct nfs4_client_reclaim
*
4910 nfsd4_find_reclaim_client(const char *recdir
, struct nfsd_net
*nn
)
4912 unsigned int strhashval
;
4913 struct nfs4_client_reclaim
*crp
= NULL
;
4915 dprintk("NFSD: nfs4_find_reclaim_client for recdir %s\n", recdir
);
4917 strhashval
= clientstr_hashval(recdir
);
4918 list_for_each_entry(crp
, &nn
->reclaim_str_hashtbl
[strhashval
], cr_strhash
) {
4919 if (same_name(crp
->cr_recdir
, recdir
)) {
4927 * Called from OPEN. Look for clientid in reclaim list.
4930 nfs4_check_open_reclaim(clientid_t
*clid
, bool sessions
, struct nfsd_net
*nn
)
4932 struct nfs4_client
*clp
;
4934 /* find clientid in conf_id_hashtbl */
4935 clp
= find_confirmed_client(clid
, sessions
, nn
);
4937 return nfserr_reclaim_bad
;
4939 return nfsd4_client_record_check(clp
) ? nfserr_reclaim_bad
: nfs_ok
;
4942 #ifdef CONFIG_NFSD_FAULT_INJECTION
4944 u64
nfsd_forget_client(struct nfs4_client
*clp
, u64 max
)
4946 if (mark_client_expired(clp
))
4952 u64
nfsd_print_client(struct nfs4_client
*clp
, u64 num
)
4954 char buf
[INET6_ADDRSTRLEN
];
4955 rpc_ntop((struct sockaddr
*)&clp
->cl_addr
, buf
, sizeof(buf
));
4956 printk(KERN_INFO
"NFS Client: %s\n", buf
);
4960 static void nfsd_print_count(struct nfs4_client
*clp
, unsigned int count
,
4963 char buf
[INET6_ADDRSTRLEN
];
4964 rpc_ntop((struct sockaddr
*)&clp
->cl_addr
, buf
, sizeof(buf
));
4965 printk(KERN_INFO
"NFS Client: %s has %u %s\n", buf
, count
, type
);
4968 static u64
nfsd_foreach_client_lock(struct nfs4_client
*clp
, u64 max
, void (*func
)(struct nfs4_lockowner
*))
4970 struct nfs4_openowner
*oop
;
4971 struct nfs4_lockowner
*lop
, *lo_next
;
4972 struct nfs4_ol_stateid
*stp
, *st_next
;
4975 list_for_each_entry(oop
, &clp
->cl_openowners
, oo_perclient
) {
4976 list_for_each_entry_safe(stp
, st_next
, &oop
->oo_owner
.so_stateids
, st_perstateowner
) {
4977 list_for_each_entry_safe(lop
, lo_next
, &stp
->st_lockowners
, lo_perstateid
) {
4989 u64
nfsd_forget_client_locks(struct nfs4_client
*clp
, u64 max
)
4991 return nfsd_foreach_client_lock(clp
, max
, release_lockowner
);
4994 u64
nfsd_print_client_locks(struct nfs4_client
*clp
, u64 max
)
4996 u64 count
= nfsd_foreach_client_lock(clp
, max
, NULL
);
4997 nfsd_print_count(clp
, count
, "locked files");
5001 static u64
nfsd_foreach_client_open(struct nfs4_client
*clp
, u64 max
, void (*func
)(struct nfs4_openowner
*))
5003 struct nfs4_openowner
*oop
, *next
;
5006 list_for_each_entry_safe(oop
, next
, &clp
->cl_openowners
, oo_perclient
) {
5016 u64
nfsd_forget_client_openowners(struct nfs4_client
*clp
, u64 max
)
5018 return nfsd_foreach_client_open(clp
, max
, release_openowner
);
5021 u64
nfsd_print_client_openowners(struct nfs4_client
*clp
, u64 max
)
5023 u64 count
= nfsd_foreach_client_open(clp
, max
, NULL
);
5024 nfsd_print_count(clp
, count
, "open files");
5028 static u64
nfsd_find_all_delegations(struct nfs4_client
*clp
, u64 max
,
5029 struct list_head
*victims
)
5031 struct nfs4_delegation
*dp
, *next
;
5034 lockdep_assert_held(&state_lock
);
5035 list_for_each_entry_safe(dp
, next
, &clp
->cl_delegations
, dl_perclnt
) {
5037 list_move(&dp
->dl_recall_lru
, victims
);
5044 u64
nfsd_forget_client_delegations(struct nfs4_client
*clp
, u64 max
)
5046 struct nfs4_delegation
*dp
, *next
;
5050 spin_lock(&state_lock
);
5051 count
= nfsd_find_all_delegations(clp
, max
, &victims
);
5052 spin_unlock(&state_lock
);
5054 list_for_each_entry_safe(dp
, next
, &victims
, dl_recall_lru
)
5055 revoke_delegation(dp
);
5060 u64
nfsd_recall_client_delegations(struct nfs4_client
*clp
, u64 max
)
5062 struct nfs4_delegation
*dp
, *next
;
5066 spin_lock(&state_lock
);
5067 count
= nfsd_find_all_delegations(clp
, max
, &victims
);
5068 list_for_each_entry_safe(dp
, next
, &victims
, dl_recall_lru
)
5069 nfsd_break_one_deleg(dp
);
5070 spin_unlock(&state_lock
);
5075 u64
nfsd_print_client_delegations(struct nfs4_client
*clp
, u64 max
)
5079 spin_lock(&state_lock
);
5080 count
= nfsd_find_all_delegations(clp
, max
, NULL
);
5081 spin_unlock(&state_lock
);
5083 nfsd_print_count(clp
, count
, "delegations");
5087 u64
nfsd_for_n_state(u64 max
, u64 (*func
)(struct nfs4_client
*, u64
))
5089 struct nfs4_client
*clp
, *next
;
5091 struct nfsd_net
*nn
= net_generic(current
->nsproxy
->net_ns
, nfsd_net_id
);
5093 if (!nfsd_netns_ready(nn
))
5096 list_for_each_entry_safe(clp
, next
, &nn
->client_lru
, cl_lru
) {
5097 count
+= func(clp
, max
- count
);
5098 if ((max
!= 0) && (count
>= max
))
5105 struct nfs4_client
*nfsd_find_client(struct sockaddr_storage
*addr
, size_t addr_size
)
5107 struct nfs4_client
*clp
;
5108 struct nfsd_net
*nn
= net_generic(current
->nsproxy
->net_ns
, nfsd_net_id
);
5110 if (!nfsd_netns_ready(nn
))
5113 list_for_each_entry(clp
, &nn
->client_lru
, cl_lru
) {
5114 if (memcmp(&clp
->cl_addr
, addr
, addr_size
) == 0)
5120 #endif /* CONFIG_NFSD_FAULT_INJECTION */
5123 * Since the lifetime of a delegation isn't limited to that of an open, a
5124 * client may quite reasonably hang on to a delegation as long as it has
5125 * the inode cached. This becomes an obvious problem the first time a
5126 * client's inode cache approaches the size of the server's total memory.
5128 * For now we avoid this problem by imposing a hard limit on the number
5129 * of delegations, which varies according to the server's memory size.
5132 set_max_delegations(void)
5135 * Allow at most 4 delegations per megabyte of RAM. Quick
5136 * estimates suggest that in the worst case (where every delegation
5137 * is for a different inode), a delegation could take about 1.5K,
5138 * giving a worst case usage of about 6% of memory.
5140 max_delegations
= nr_free_buffer_pages() >> (20 - 2 - PAGE_SHIFT
);
5143 static int nfs4_state_create_net(struct net
*net
)
5145 struct nfsd_net
*nn
= net_generic(net
, nfsd_net_id
);
5148 nn
->conf_id_hashtbl
= kmalloc(sizeof(struct list_head
) *
5149 CLIENT_HASH_SIZE
, GFP_KERNEL
);
5150 if (!nn
->conf_id_hashtbl
)
5152 nn
->unconf_id_hashtbl
= kmalloc(sizeof(struct list_head
) *
5153 CLIENT_HASH_SIZE
, GFP_KERNEL
);
5154 if (!nn
->unconf_id_hashtbl
)
5156 nn
->ownerstr_hashtbl
= kmalloc(sizeof(struct list_head
) *
5157 OWNER_HASH_SIZE
, GFP_KERNEL
);
5158 if (!nn
->ownerstr_hashtbl
)
5160 nn
->lockowner_ino_hashtbl
= kmalloc(sizeof(struct list_head
) *
5161 LOCKOWNER_INO_HASH_SIZE
, GFP_KERNEL
);
5162 if (!nn
->lockowner_ino_hashtbl
)
5163 goto err_lockowner_ino
;
5164 nn
->sessionid_hashtbl
= kmalloc(sizeof(struct list_head
) *
5165 SESSION_HASH_SIZE
, GFP_KERNEL
);
5166 if (!nn
->sessionid_hashtbl
)
5169 for (i
= 0; i
< CLIENT_HASH_SIZE
; i
++) {
5170 INIT_LIST_HEAD(&nn
->conf_id_hashtbl
[i
]);
5171 INIT_LIST_HEAD(&nn
->unconf_id_hashtbl
[i
]);
5173 for (i
= 0; i
< OWNER_HASH_SIZE
; i
++)
5174 INIT_LIST_HEAD(&nn
->ownerstr_hashtbl
[i
]);
5175 for (i
= 0; i
< LOCKOWNER_INO_HASH_SIZE
; i
++)
5176 INIT_LIST_HEAD(&nn
->lockowner_ino_hashtbl
[i
]);
5177 for (i
= 0; i
< SESSION_HASH_SIZE
; i
++)
5178 INIT_LIST_HEAD(&nn
->sessionid_hashtbl
[i
]);
5179 nn
->conf_name_tree
= RB_ROOT
;
5180 nn
->unconf_name_tree
= RB_ROOT
;
5181 INIT_LIST_HEAD(&nn
->client_lru
);
5182 INIT_LIST_HEAD(&nn
->close_lru
);
5183 INIT_LIST_HEAD(&nn
->del_recall_lru
);
5184 spin_lock_init(&nn
->client_lock
);
5186 INIT_DELAYED_WORK(&nn
->laundromat_work
, laundromat_main
);
5192 kfree(nn
->lockowner_ino_hashtbl
);
5194 kfree(nn
->ownerstr_hashtbl
);
5196 kfree(nn
->unconf_id_hashtbl
);
5198 kfree(nn
->conf_id_hashtbl
);
5204 nfs4_state_destroy_net(struct net
*net
)
5207 struct nfs4_client
*clp
= NULL
;
5208 struct nfsd_net
*nn
= net_generic(net
, nfsd_net_id
);
5210 for (i
= 0; i
< CLIENT_HASH_SIZE
; i
++) {
5211 while (!list_empty(&nn
->conf_id_hashtbl
[i
])) {
5212 clp
= list_entry(nn
->conf_id_hashtbl
[i
].next
, struct nfs4_client
, cl_idhash
);
5213 destroy_client(clp
);
5217 for (i
= 0; i
< CLIENT_HASH_SIZE
; i
++) {
5218 while (!list_empty(&nn
->unconf_id_hashtbl
[i
])) {
5219 clp
= list_entry(nn
->unconf_id_hashtbl
[i
].next
, struct nfs4_client
, cl_idhash
);
5220 destroy_client(clp
);
5224 kfree(nn
->sessionid_hashtbl
);
5225 kfree(nn
->lockowner_ino_hashtbl
);
5226 kfree(nn
->ownerstr_hashtbl
);
5227 kfree(nn
->unconf_id_hashtbl
);
5228 kfree(nn
->conf_id_hashtbl
);
5233 nfs4_state_start_net(struct net
*net
)
5235 struct nfsd_net
*nn
= net_generic(net
, nfsd_net_id
);
5238 ret
= nfs4_state_create_net(net
);
5241 nfsd4_client_tracking_init(net
);
5242 nn
->boot_time
= get_seconds();
5243 locks_start_grace(net
, &nn
->nfsd4_manager
);
5244 nn
->grace_ended
= false;
5245 printk(KERN_INFO
"NFSD: starting %ld-second grace period (net %p)\n",
5246 nn
->nfsd4_grace
, net
);
5247 queue_delayed_work(laundry_wq
, &nn
->laundromat_work
, nn
->nfsd4_grace
* HZ
);
5251 /* initialization to perform when the nfsd service is started: */
5254 nfs4_state_start(void)
5258 ret
= set_callback_cred();
5261 laundry_wq
= create_singlethread_workqueue("nfsd4");
5262 if (laundry_wq
== NULL
) {
5266 ret
= nfsd4_create_callback_queue();
5268 goto out_free_laundry
;
5270 set_max_delegations();
5275 destroy_workqueue(laundry_wq
);
5281 nfs4_state_shutdown_net(struct net
*net
)
5283 struct nfs4_delegation
*dp
= NULL
;
5284 struct list_head
*pos
, *next
, reaplist
;
5285 struct nfsd_net
*nn
= net_generic(net
, nfsd_net_id
);
5287 cancel_delayed_work_sync(&nn
->laundromat_work
);
5288 locks_end_grace(&nn
->nfsd4_manager
);
5291 INIT_LIST_HEAD(&reaplist
);
5292 spin_lock(&state_lock
);
5293 list_for_each_safe(pos
, next
, &nn
->del_recall_lru
) {
5294 dp
= list_entry (pos
, struct nfs4_delegation
, dl_recall_lru
);
5295 list_move(&dp
->dl_recall_lru
, &reaplist
);
5297 spin_unlock(&state_lock
);
5298 list_for_each_safe(pos
, next
, &reaplist
) {
5299 dp
= list_entry (pos
, struct nfs4_delegation
, dl_recall_lru
);
5300 destroy_delegation(dp
);
5303 nfsd4_client_tracking_exit(net
);
5304 nfs4_state_destroy_net(net
);
5305 nfs4_unlock_state();
5309 nfs4_state_shutdown(void)
5311 destroy_workqueue(laundry_wq
);
5312 nfsd4_destroy_callback_queue();
5316 get_stateid(struct nfsd4_compound_state
*cstate
, stateid_t
*stateid
)
5318 if (HAS_STATE_ID(cstate
, CURRENT_STATE_ID_FLAG
) && CURRENT_STATEID(stateid
))
5319 memcpy(stateid
, &cstate
->current_stateid
, sizeof(stateid_t
));
5323 put_stateid(struct nfsd4_compound_state
*cstate
, stateid_t
*stateid
)
5325 if (cstate
->minorversion
) {
5326 memcpy(&cstate
->current_stateid
, stateid
, sizeof(stateid_t
));
5327 SET_STATE_ID(cstate
, CURRENT_STATE_ID_FLAG
);
5332 clear_current_stateid(struct nfsd4_compound_state
*cstate
)
5334 CLEAR_STATE_ID(cstate
, CURRENT_STATE_ID_FLAG
);
5338 * functions to set current state id
5341 nfsd4_set_opendowngradestateid(struct nfsd4_compound_state
*cstate
, struct nfsd4_open_downgrade
*odp
)
5343 put_stateid(cstate
, &odp
->od_stateid
);
5347 nfsd4_set_openstateid(struct nfsd4_compound_state
*cstate
, struct nfsd4_open
*open
)
5349 put_stateid(cstate
, &open
->op_stateid
);
5353 nfsd4_set_closestateid(struct nfsd4_compound_state
*cstate
, struct nfsd4_close
*close
)
5355 put_stateid(cstate
, &close
->cl_stateid
);
5359 nfsd4_set_lockstateid(struct nfsd4_compound_state
*cstate
, struct nfsd4_lock
*lock
)
5361 put_stateid(cstate
, &lock
->lk_resp_stateid
);
5365 * functions to consume current state id
5369 nfsd4_get_opendowngradestateid(struct nfsd4_compound_state
*cstate
, struct nfsd4_open_downgrade
*odp
)
5371 get_stateid(cstate
, &odp
->od_stateid
);
5375 nfsd4_get_delegreturnstateid(struct nfsd4_compound_state
*cstate
, struct nfsd4_delegreturn
*drp
)
5377 get_stateid(cstate
, &drp
->dr_stateid
);
5381 nfsd4_get_freestateid(struct nfsd4_compound_state
*cstate
, struct nfsd4_free_stateid
*fsp
)
5383 get_stateid(cstate
, &fsp
->fr_stateid
);
5387 nfsd4_get_setattrstateid(struct nfsd4_compound_state
*cstate
, struct nfsd4_setattr
*setattr
)
5389 get_stateid(cstate
, &setattr
->sa_stateid
);
5393 nfsd4_get_closestateid(struct nfsd4_compound_state
*cstate
, struct nfsd4_close
*close
)
5395 get_stateid(cstate
, &close
->cl_stateid
);
5399 nfsd4_get_lockustateid(struct nfsd4_compound_state
*cstate
, struct nfsd4_locku
*locku
)
5401 get_stateid(cstate
, &locku
->lu_stateid
);
5405 nfsd4_get_readstateid(struct nfsd4_compound_state
*cstate
, struct nfsd4_read
*read
)
5407 get_stateid(cstate
, &read
->rd_stateid
);
5411 nfsd4_get_writestateid(struct nfsd4_compound_state
*cstate
, struct nfsd4_write
*write
)
5413 get_stateid(cstate
, &write
->wr_stateid
);