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/sunrpc/svcauth_gss.h>
42 #include <linux/sunrpc/clnt.h>
46 #define NFSDDBG_FACILITY NFSDDBG_PROC
49 time_t nfsd4_lease
= 90; /* default lease time */
50 time_t nfsd4_grace
= 90;
51 static time_t boot_time
;
52 static u32 current_ownerid
= 1;
53 static u32 current_fileid
= 1;
54 static u32 current_delegid
= 1;
55 static stateid_t zerostateid
; /* bits all 0 */
56 static stateid_t onestateid
; /* bits all 1 */
57 static u64 current_sessionid
= 1;
59 #define ZERO_STATEID(stateid) (!memcmp((stateid), &zerostateid, sizeof(stateid_t)))
60 #define ONE_STATEID(stateid) (!memcmp((stateid), &onestateid, sizeof(stateid_t)))
62 /* forward declarations */
63 static struct nfs4_delegation
* search_for_delegation(stateid_t
*stid
);
64 static struct nfs4_delegation
* find_delegation_stateid(struct inode
*ino
, stateid_t
*stid
);
65 static int check_for_locks(struct nfs4_file
*filp
, struct nfs4_lockowner
*lowner
);
69 /* Currently used for almost all code touching nfsv4 state: */
70 static DEFINE_MUTEX(client_mutex
);
73 * Currently used for the del_recall_lru and file hash table. In an
74 * effort to decrease the scope of the client_mutex, this spinlock may
75 * eventually cover more:
77 static DEFINE_SPINLOCK(recall_lock
);
79 static struct kmem_cache
*openowner_slab
= NULL
;
80 static struct kmem_cache
*lockowner_slab
= NULL
;
81 static struct kmem_cache
*file_slab
= NULL
;
82 static struct kmem_cache
*stateid_slab
= NULL
;
83 static struct kmem_cache
*deleg_slab
= NULL
;
88 mutex_lock(&client_mutex
);
92 nfs4_unlock_state(void)
94 mutex_unlock(&client_mutex
);
98 opaque_hashval(const void *ptr
, int nbytes
)
100 unsigned char *cptr
= (unsigned char *) ptr
;
110 static struct list_head del_recall_lru
;
113 put_nfs4_file(struct nfs4_file
*fi
)
115 if (atomic_dec_and_lock(&fi
->fi_ref
, &recall_lock
)) {
116 list_del(&fi
->fi_hash
);
117 spin_unlock(&recall_lock
);
119 kmem_cache_free(file_slab
, fi
);
124 get_nfs4_file(struct nfs4_file
*fi
)
126 atomic_inc(&fi
->fi_ref
);
129 static int num_delegations
;
130 unsigned int max_delegations
;
133 * Open owner state (share locks)
136 /* hash tables for open owners */
137 #define OPEN_OWNER_HASH_BITS 8
138 #define OPEN_OWNER_HASH_SIZE (1 << OPEN_OWNER_HASH_BITS)
139 #define OPEN_OWNER_HASH_MASK (OPEN_OWNER_HASH_SIZE - 1)
141 static unsigned int open_ownerid_hashval(const u32 id
)
143 return id
& OPEN_OWNER_HASH_MASK
;
146 static unsigned int open_ownerstr_hashval(u32 clientid
, struct xdr_netobj
*ownername
)
150 ret
= opaque_hashval(ownername
->data
, ownername
->len
);
152 return ret
& OPEN_OWNER_HASH_MASK
;
155 static struct list_head open_ownerid_hashtbl
[OPEN_OWNER_HASH_SIZE
];
156 static struct list_head open_ownerstr_hashtbl
[OPEN_OWNER_HASH_SIZE
];
158 /* hash table for nfs4_file */
159 #define FILE_HASH_BITS 8
160 #define FILE_HASH_SIZE (1 << FILE_HASH_BITS)
162 /* hash table for (open)nfs4_stateid */
163 #define STATEID_HASH_BITS 10
164 #define STATEID_HASH_SIZE (1 << STATEID_HASH_BITS)
165 #define STATEID_HASH_MASK (STATEID_HASH_SIZE - 1)
167 static unsigned int file_hashval(struct inode
*ino
)
169 /* XXX: why are we hashing on inode pointer, anyway? */
170 return hash_ptr(ino
, FILE_HASH_BITS
);
173 static unsigned int stateid_hashval(u32 owner_id
, u32 file_id
)
175 return (owner_id
+ file_id
) & STATEID_HASH_MASK
;
178 static struct list_head file_hashtbl
[FILE_HASH_SIZE
];
179 static struct list_head stateid_hashtbl
[STATEID_HASH_SIZE
];
181 static void __nfs4_file_get_access(struct nfs4_file
*fp
, int oflag
)
183 BUG_ON(!(fp
->fi_fds
[oflag
] || fp
->fi_fds
[O_RDWR
]));
184 atomic_inc(&fp
->fi_access
[oflag
]);
187 static void nfs4_file_get_access(struct nfs4_file
*fp
, int oflag
)
189 if (oflag
== O_RDWR
) {
190 __nfs4_file_get_access(fp
, O_RDONLY
);
191 __nfs4_file_get_access(fp
, O_WRONLY
);
193 __nfs4_file_get_access(fp
, oflag
);
196 static void nfs4_file_put_fd(struct nfs4_file
*fp
, int oflag
)
198 if (fp
->fi_fds
[oflag
]) {
199 fput(fp
->fi_fds
[oflag
]);
200 fp
->fi_fds
[oflag
] = NULL
;
204 static void __nfs4_file_put_access(struct nfs4_file
*fp
, int oflag
)
206 if (atomic_dec_and_test(&fp
->fi_access
[oflag
])) {
207 nfs4_file_put_fd(fp
, O_RDWR
);
208 nfs4_file_put_fd(fp
, oflag
);
212 static void nfs4_file_put_access(struct nfs4_file
*fp
, int oflag
)
214 if (oflag
== O_RDWR
) {
215 __nfs4_file_put_access(fp
, O_RDONLY
);
216 __nfs4_file_put_access(fp
, O_WRONLY
);
218 __nfs4_file_put_access(fp
, oflag
);
221 static struct nfs4_delegation
*
222 alloc_init_deleg(struct nfs4_client
*clp
, struct nfs4_stateid
*stp
, struct svc_fh
*current_fh
, u32 type
)
224 struct nfs4_delegation
*dp
;
225 struct nfs4_file
*fp
= stp
->st_file
;
227 dprintk("NFSD alloc_init_deleg\n");
229 * Major work on the lease subsystem (for example, to support
230 * calbacks on stat) will be required before we can support
231 * write delegations properly.
233 if (type
!= NFS4_OPEN_DELEGATE_READ
)
235 if (fp
->fi_had_conflict
)
237 if (num_delegations
> max_delegations
)
239 dp
= kmem_cache_alloc(deleg_slab
, GFP_KERNEL
);
243 INIT_LIST_HEAD(&dp
->dl_perfile
);
244 INIT_LIST_HEAD(&dp
->dl_perclnt
);
245 INIT_LIST_HEAD(&dp
->dl_recall_lru
);
250 dp
->dl_stateid
.si_boot
= boot_time
;
251 dp
->dl_stateid
.si_stateownerid
= current_delegid
++;
252 dp
->dl_stateid
.si_fileid
= 0;
253 dp
->dl_stateid
.si_generation
= 1;
254 fh_copy_shallow(&dp
->dl_fh
, ¤t_fh
->fh_handle
);
256 atomic_set(&dp
->dl_count
, 1);
257 INIT_WORK(&dp
->dl_recall
.cb_work
, nfsd4_do_callback_rpc
);
262 nfs4_put_delegation(struct nfs4_delegation
*dp
)
264 if (atomic_dec_and_test(&dp
->dl_count
)) {
265 dprintk("NFSD: freeing dp %p\n",dp
);
266 put_nfs4_file(dp
->dl_file
);
267 kmem_cache_free(deleg_slab
, dp
);
272 static void nfs4_put_deleg_lease(struct nfs4_file
*fp
)
274 if (atomic_dec_and_test(&fp
->fi_delegees
)) {
275 vfs_setlease(fp
->fi_deleg_file
, F_UNLCK
, &fp
->fi_lease
);
277 fput(fp
->fi_deleg_file
);
278 fp
->fi_deleg_file
= NULL
;
282 /* Called under the state lock. */
284 unhash_delegation(struct nfs4_delegation
*dp
)
286 list_del_init(&dp
->dl_perclnt
);
287 spin_lock(&recall_lock
);
288 list_del_init(&dp
->dl_perfile
);
289 list_del_init(&dp
->dl_recall_lru
);
290 spin_unlock(&recall_lock
);
291 nfs4_put_deleg_lease(dp
->dl_file
);
292 nfs4_put_delegation(dp
);
299 /* client_lock protects the client lru list and session hash table */
300 static DEFINE_SPINLOCK(client_lock
);
302 /* Hash tables for nfs4_clientid state */
303 #define CLIENT_HASH_BITS 4
304 #define CLIENT_HASH_SIZE (1 << CLIENT_HASH_BITS)
305 #define CLIENT_HASH_MASK (CLIENT_HASH_SIZE - 1)
307 static unsigned int clientid_hashval(u32 id
)
309 return id
& CLIENT_HASH_MASK
;
312 static unsigned int clientstr_hashval(const char *name
)
314 return opaque_hashval(name
, 8) & CLIENT_HASH_MASK
;
318 * reclaim_str_hashtbl[] holds known client info from previous reset/reboot
319 * used in reboot/reset lease grace period processing
321 * conf_id_hashtbl[], and conf_str_hashtbl[] hold confirmed
322 * setclientid_confirmed info.
324 * unconf_str_hastbl[] and unconf_id_hashtbl[] hold unconfirmed
327 * client_lru holds client queue ordered by nfs4_client.cl_time
330 * close_lru holds (open) stateowner queue ordered by nfs4_stateowner.so_time
331 * for last close replay.
333 static struct list_head reclaim_str_hashtbl
[CLIENT_HASH_SIZE
];
334 static int reclaim_str_hashtbl_size
= 0;
335 static struct list_head conf_id_hashtbl
[CLIENT_HASH_SIZE
];
336 static struct list_head conf_str_hashtbl
[CLIENT_HASH_SIZE
];
337 static struct list_head unconf_str_hashtbl
[CLIENT_HASH_SIZE
];
338 static struct list_head unconf_id_hashtbl
[CLIENT_HASH_SIZE
];
339 static struct list_head client_lru
;
340 static struct list_head close_lru
;
343 * We store the NONE, READ, WRITE, and BOTH bits separately in the
344 * st_{access,deny}_bmap field of the stateid, in order to track not
345 * only what share bits are currently in force, but also what
346 * combinations of share bits previous opens have used. This allows us
347 * to enforce the recommendation of rfc 3530 14.2.19 that the server
348 * return an error if the client attempt to downgrade to a combination
349 * of share bits not explicable by closing some of its previous opens.
351 * XXX: This enforcement is actually incomplete, since we don't keep
352 * track of access/deny bit combinations; so, e.g., we allow:
354 * OPEN allow read, deny write
355 * OPEN allow both, deny none
356 * DOWNGRADE allow read, deny none
358 * which we should reject.
361 set_access(unsigned int *access
, unsigned long bmap
) {
365 for (i
= 1; i
< 4; i
++) {
366 if (test_bit(i
, &bmap
))
372 set_deny(unsigned int *deny
, unsigned long bmap
) {
376 for (i
= 0; i
< 4; i
++) {
377 if (test_bit(i
, &bmap
))
383 test_share(struct nfs4_stateid
*stp
, struct nfsd4_open
*open
) {
384 unsigned int access
, deny
;
386 set_access(&access
, stp
->st_access_bmap
);
387 set_deny(&deny
, stp
->st_deny_bmap
);
388 if ((access
& open
->op_share_deny
) || (deny
& open
->op_share_access
))
393 static int nfs4_access_to_omode(u32 access
)
395 switch (access
& NFS4_SHARE_ACCESS_BOTH
) {
396 case NFS4_SHARE_ACCESS_READ
:
398 case NFS4_SHARE_ACCESS_WRITE
:
400 case NFS4_SHARE_ACCESS_BOTH
:
406 static void unhash_generic_stateid(struct nfs4_stateid
*stp
)
408 list_del(&stp
->st_hash
);
409 list_del(&stp
->st_perfile
);
410 list_del(&stp
->st_perstateowner
);
413 static void close_generic_stateid(struct nfs4_stateid
*stp
)
417 if (stp
->st_access_bmap
) {
418 for (i
= 1; i
< 4; i
++) {
419 if (test_bit(i
, &stp
->st_access_bmap
))
420 nfs4_file_put_access(stp
->st_file
,
421 nfs4_access_to_omode(i
));
422 __clear_bit(i
, &stp
->st_access_bmap
);
425 put_nfs4_file(stp
->st_file
);
429 static void free_generic_stateid(struct nfs4_stateid
*stp
)
431 close_generic_stateid(stp
);
432 kmem_cache_free(stateid_slab
, stp
);
435 static void release_lock_stateid(struct nfs4_stateid
*stp
)
439 unhash_generic_stateid(stp
);
440 file
= find_any_file(stp
->st_file
);
442 locks_remove_posix(file
, (fl_owner_t
)lockowner(stp
->st_stateowner
));
443 free_generic_stateid(stp
);
446 static void unhash_lockowner(struct nfs4_lockowner
*lo
)
448 struct nfs4_stateid
*stp
;
450 list_del(&lo
->lo_owner
.so_idhash
);
451 list_del(&lo
->lo_owner
.so_strhash
);
452 list_del(&lo
->lo_perstateid
);
453 while (!list_empty(&lo
->lo_owner
.so_stateids
)) {
454 stp
= list_first_entry(&lo
->lo_owner
.so_stateids
,
455 struct nfs4_stateid
, st_perstateowner
);
456 release_lock_stateid(stp
);
460 static void release_lockowner(struct nfs4_lockowner
*lo
)
462 unhash_lockowner(lo
);
463 nfs4_free_lockowner(lo
);
467 release_stateid_lockowners(struct nfs4_stateid
*open_stp
)
469 struct nfs4_lockowner
*lo
;
471 while (!list_empty(&open_stp
->st_lockowners
)) {
472 lo
= list_entry(open_stp
->st_lockowners
.next
,
473 struct nfs4_lockowner
, lo_perstateid
);
474 release_lockowner(lo
);
478 static void release_open_stateid(struct nfs4_stateid
*stp
)
480 unhash_generic_stateid(stp
);
481 release_stateid_lockowners(stp
);
482 free_generic_stateid(stp
);
485 static void unhash_openowner(struct nfs4_openowner
*oo
)
487 struct nfs4_stateid
*stp
;
489 list_del(&oo
->oo_owner
.so_idhash
);
490 list_del(&oo
->oo_owner
.so_strhash
);
491 list_del(&oo
->oo_perclient
);
492 while (!list_empty(&oo
->oo_owner
.so_stateids
)) {
493 stp
= list_first_entry(&oo
->oo_owner
.so_stateids
,
494 struct nfs4_stateid
, st_perstateowner
);
495 release_open_stateid(stp
);
499 static void release_openowner(struct nfs4_openowner
*oo
)
501 unhash_openowner(oo
);
502 list_del(&oo
->oo_close_lru
);
503 nfs4_free_openowner(oo
);
506 #define SESSION_HASH_SIZE 512
507 static struct list_head sessionid_hashtbl
[SESSION_HASH_SIZE
];
510 hash_sessionid(struct nfs4_sessionid
*sessionid
)
512 struct nfsd4_sessionid
*sid
= (struct nfsd4_sessionid
*)sessionid
;
514 return sid
->sequence
% SESSION_HASH_SIZE
;
518 dump_sessionid(const char *fn
, struct nfs4_sessionid
*sessionid
)
520 u32
*ptr
= (u32
*)(&sessionid
->data
[0]);
521 dprintk("%s: %u:%u:%u:%u\n", fn
, ptr
[0], ptr
[1], ptr
[2], ptr
[3]);
525 gen_sessionid(struct nfsd4_session
*ses
)
527 struct nfs4_client
*clp
= ses
->se_client
;
528 struct nfsd4_sessionid
*sid
;
530 sid
= (struct nfsd4_sessionid
*)ses
->se_sessionid
.data
;
531 sid
->clientid
= clp
->cl_clientid
;
532 sid
->sequence
= current_sessionid
++;
537 * The protocol defines ca_maxresponssize_cached to include the size of
538 * the rpc header, but all we need to cache is the data starting after
539 * the end of the initial SEQUENCE operation--the rest we regenerate
540 * each time. Therefore we can advertise a ca_maxresponssize_cached
541 * value that is the number of bytes in our cache plus a few additional
542 * bytes. In order to stay on the safe side, and not promise more than
543 * we can cache, those additional bytes must be the minimum possible: 24
544 * bytes of rpc header (xid through accept state, with AUTH_NULL
545 * verifier), 12 for the compound header (with zero-length tag), and 44
546 * for the SEQUENCE op response:
548 #define NFSD_MIN_HDR_SEQ_SZ (24 + 12 + 44)
551 free_session_slots(struct nfsd4_session
*ses
)
555 for (i
= 0; i
< ses
->se_fchannel
.maxreqs
; i
++)
556 kfree(ses
->se_slots
[i
]);
560 * We don't actually need to cache the rpc and session headers, so we
561 * can allocate a little less for each slot:
563 static inline int slot_bytes(struct nfsd4_channel_attrs
*ca
)
565 return ca
->maxresp_cached
- NFSD_MIN_HDR_SEQ_SZ
;
568 static int nfsd4_sanitize_slot_size(u32 size
)
570 size
-= NFSD_MIN_HDR_SEQ_SZ
; /* We don't cache the rpc header */
571 size
= min_t(u32
, size
, NFSD_SLOT_CACHE_SIZE
);
577 * XXX: If we run out of reserved DRC memory we could (up to a point)
578 * re-negotiate active sessions and reduce their slot usage to make
579 * rooom for new connections. For now we just fail the create session.
581 static int nfsd4_get_drc_mem(int slotsize
, u32 num
)
585 num
= min_t(u32
, num
, NFSD_MAX_SLOTS_PER_SESSION
);
587 spin_lock(&nfsd_drc_lock
);
588 avail
= min_t(int, NFSD_MAX_MEM_PER_SESSION
,
589 nfsd_drc_max_mem
- nfsd_drc_mem_used
);
590 num
= min_t(int, num
, avail
/ slotsize
);
591 nfsd_drc_mem_used
+= num
* slotsize
;
592 spin_unlock(&nfsd_drc_lock
);
597 static void nfsd4_put_drc_mem(int slotsize
, int num
)
599 spin_lock(&nfsd_drc_lock
);
600 nfsd_drc_mem_used
-= slotsize
* num
;
601 spin_unlock(&nfsd_drc_lock
);
604 static struct nfsd4_session
*alloc_session(int slotsize
, int numslots
)
606 struct nfsd4_session
*new;
609 BUILD_BUG_ON(NFSD_MAX_SLOTS_PER_SESSION
* sizeof(struct nfsd4_slot
*)
610 + sizeof(struct nfsd4_session
) > PAGE_SIZE
);
611 mem
= numslots
* sizeof(struct nfsd4_slot
*);
613 new = kzalloc(sizeof(*new) + mem
, GFP_KERNEL
);
616 /* allocate each struct nfsd4_slot and data cache in one piece */
617 for (i
= 0; i
< numslots
; i
++) {
618 mem
= sizeof(struct nfsd4_slot
) + slotsize
;
619 new->se_slots
[i
] = kzalloc(mem
, GFP_KERNEL
);
620 if (!new->se_slots
[i
])
626 kfree(new->se_slots
[i
]);
631 static void init_forechannel_attrs(struct nfsd4_channel_attrs
*new, struct nfsd4_channel_attrs
*req
, int numslots
, int slotsize
)
633 u32 maxrpc
= nfsd_serv
->sv_max_mesg
;
635 new->maxreqs
= numslots
;
636 new->maxresp_cached
= min_t(u32
, req
->maxresp_cached
,
637 slotsize
+ NFSD_MIN_HDR_SEQ_SZ
);
638 new->maxreq_sz
= min_t(u32
, req
->maxreq_sz
, maxrpc
);
639 new->maxresp_sz
= min_t(u32
, req
->maxresp_sz
, maxrpc
);
640 new->maxops
= min_t(u32
, req
->maxops
, NFSD_MAX_OPS_PER_COMPOUND
);
643 static void free_conn(struct nfsd4_conn
*c
)
645 svc_xprt_put(c
->cn_xprt
);
649 static void nfsd4_conn_lost(struct svc_xpt_user
*u
)
651 struct nfsd4_conn
*c
= container_of(u
, struct nfsd4_conn
, cn_xpt_user
);
652 struct nfs4_client
*clp
= c
->cn_session
->se_client
;
654 spin_lock(&clp
->cl_lock
);
655 if (!list_empty(&c
->cn_persession
)) {
656 list_del(&c
->cn_persession
);
659 spin_unlock(&clp
->cl_lock
);
660 nfsd4_probe_callback(clp
);
663 static struct nfsd4_conn
*alloc_conn(struct svc_rqst
*rqstp
, u32 flags
)
665 struct nfsd4_conn
*conn
;
667 conn
= kmalloc(sizeof(struct nfsd4_conn
), GFP_KERNEL
);
670 svc_xprt_get(rqstp
->rq_xprt
);
671 conn
->cn_xprt
= rqstp
->rq_xprt
;
672 conn
->cn_flags
= flags
;
673 INIT_LIST_HEAD(&conn
->cn_xpt_user
.list
);
677 static void __nfsd4_hash_conn(struct nfsd4_conn
*conn
, struct nfsd4_session
*ses
)
679 conn
->cn_session
= ses
;
680 list_add(&conn
->cn_persession
, &ses
->se_conns
);
683 static void nfsd4_hash_conn(struct nfsd4_conn
*conn
, struct nfsd4_session
*ses
)
685 struct nfs4_client
*clp
= ses
->se_client
;
687 spin_lock(&clp
->cl_lock
);
688 __nfsd4_hash_conn(conn
, ses
);
689 spin_unlock(&clp
->cl_lock
);
692 static int nfsd4_register_conn(struct nfsd4_conn
*conn
)
694 conn
->cn_xpt_user
.callback
= nfsd4_conn_lost
;
695 return register_xpt_user(conn
->cn_xprt
, &conn
->cn_xpt_user
);
698 static __be32
nfsd4_new_conn(struct svc_rqst
*rqstp
, struct nfsd4_session
*ses
, u32 dir
)
700 struct nfsd4_conn
*conn
;
703 conn
= alloc_conn(rqstp
, dir
);
705 return nfserr_jukebox
;
706 nfsd4_hash_conn(conn
, ses
);
707 ret
= nfsd4_register_conn(conn
);
709 /* oops; xprt is already down: */
710 nfsd4_conn_lost(&conn
->cn_xpt_user
);
714 static __be32
nfsd4_new_conn_from_crses(struct svc_rqst
*rqstp
, struct nfsd4_session
*ses
)
716 u32 dir
= NFS4_CDFC4_FORE
;
718 if (ses
->se_flags
& SESSION4_BACK_CHAN
)
719 dir
|= NFS4_CDFC4_BACK
;
721 return nfsd4_new_conn(rqstp
, ses
, dir
);
724 /* must be called under client_lock */
725 static void nfsd4_del_conns(struct nfsd4_session
*s
)
727 struct nfs4_client
*clp
= s
->se_client
;
728 struct nfsd4_conn
*c
;
730 spin_lock(&clp
->cl_lock
);
731 while (!list_empty(&s
->se_conns
)) {
732 c
= list_first_entry(&s
->se_conns
, struct nfsd4_conn
, cn_persession
);
733 list_del_init(&c
->cn_persession
);
734 spin_unlock(&clp
->cl_lock
);
736 unregister_xpt_user(c
->cn_xprt
, &c
->cn_xpt_user
);
739 spin_lock(&clp
->cl_lock
);
741 spin_unlock(&clp
->cl_lock
);
744 void free_session(struct kref
*kref
)
746 struct nfsd4_session
*ses
;
749 ses
= container_of(kref
, struct nfsd4_session
, se_ref
);
750 nfsd4_del_conns(ses
);
751 spin_lock(&nfsd_drc_lock
);
752 mem
= ses
->se_fchannel
.maxreqs
* slot_bytes(&ses
->se_fchannel
);
753 nfsd_drc_mem_used
-= mem
;
754 spin_unlock(&nfsd_drc_lock
);
755 free_session_slots(ses
);
759 static struct nfsd4_session
*alloc_init_session(struct svc_rqst
*rqstp
, struct nfs4_client
*clp
, struct nfsd4_create_session
*cses
)
761 struct nfsd4_session
*new;
762 struct nfsd4_channel_attrs
*fchan
= &cses
->fore_channel
;
763 int numslots
, slotsize
;
768 * Note decreasing slot size below client's request may
769 * make it difficult for client to function correctly, whereas
770 * decreasing the number of slots will (just?) affect
771 * performance. When short on memory we therefore prefer to
772 * decrease number of slots instead of their size.
774 slotsize
= nfsd4_sanitize_slot_size(fchan
->maxresp_cached
);
775 numslots
= nfsd4_get_drc_mem(slotsize
, fchan
->maxreqs
);
779 new = alloc_session(slotsize
, numslots
);
781 nfsd4_put_drc_mem(slotsize
, fchan
->maxreqs
);
784 init_forechannel_attrs(&new->se_fchannel
, fchan
, numslots
, slotsize
);
786 new->se_client
= clp
;
789 INIT_LIST_HEAD(&new->se_conns
);
791 new->se_cb_seq_nr
= 1;
792 new->se_flags
= cses
->flags
;
793 new->se_cb_prog
= cses
->callback_prog
;
794 kref_init(&new->se_ref
);
795 idx
= hash_sessionid(&new->se_sessionid
);
796 spin_lock(&client_lock
);
797 list_add(&new->se_hash
, &sessionid_hashtbl
[idx
]);
798 spin_lock(&clp
->cl_lock
);
799 list_add(&new->se_perclnt
, &clp
->cl_sessions
);
800 spin_unlock(&clp
->cl_lock
);
801 spin_unlock(&client_lock
);
803 status
= nfsd4_new_conn_from_crses(rqstp
, new);
804 /* whoops: benny points out, status is ignored! (err, or bogus) */
806 free_session(&new->se_ref
);
809 if (cses
->flags
& SESSION4_BACK_CHAN
) {
810 struct sockaddr
*sa
= svc_addr(rqstp
);
812 * This is a little silly; with sessions there's no real
813 * use for the callback address. Use the peer address
814 * as a reasonable default for now, but consider fixing
815 * the rpc client not to require an address in the
818 rpc_copy_addr((struct sockaddr
*)&clp
->cl_cb_conn
.cb_addr
, sa
);
819 clp
->cl_cb_conn
.cb_addrlen
= svc_addr_len(sa
);
821 nfsd4_probe_callback(clp
);
825 /* caller must hold client_lock */
826 static struct nfsd4_session
*
827 find_in_sessionid_hashtbl(struct nfs4_sessionid
*sessionid
)
829 struct nfsd4_session
*elem
;
832 dump_sessionid(__func__
, sessionid
);
833 idx
= hash_sessionid(sessionid
);
834 /* Search in the appropriate list */
835 list_for_each_entry(elem
, &sessionid_hashtbl
[idx
], se_hash
) {
836 if (!memcmp(elem
->se_sessionid
.data
, sessionid
->data
,
837 NFS4_MAX_SESSIONID_LEN
)) {
842 dprintk("%s: session not found\n", __func__
);
846 /* caller must hold client_lock */
848 unhash_session(struct nfsd4_session
*ses
)
850 list_del(&ses
->se_hash
);
851 spin_lock(&ses
->se_client
->cl_lock
);
852 list_del(&ses
->se_perclnt
);
853 spin_unlock(&ses
->se_client
->cl_lock
);
856 /* must be called under the client_lock */
858 renew_client_locked(struct nfs4_client
*clp
)
860 if (is_client_expired(clp
)) {
861 dprintk("%s: client (clientid %08x/%08x) already expired\n",
863 clp
->cl_clientid
.cl_boot
,
864 clp
->cl_clientid
.cl_id
);
869 * Move client to the end to the LRU list.
871 dprintk("renewing client (clientid %08x/%08x)\n",
872 clp
->cl_clientid
.cl_boot
,
873 clp
->cl_clientid
.cl_id
);
874 list_move_tail(&clp
->cl_lru
, &client_lru
);
875 clp
->cl_time
= get_seconds();
879 renew_client(struct nfs4_client
*clp
)
881 spin_lock(&client_lock
);
882 renew_client_locked(clp
);
883 spin_unlock(&client_lock
);
886 /* SETCLIENTID and SETCLIENTID_CONFIRM Helper functions */
888 STALE_CLIENTID(clientid_t
*clid
)
890 if (clid
->cl_boot
== boot_time
)
892 dprintk("NFSD stale clientid (%08x/%08x) boot_time %08lx\n",
893 clid
->cl_boot
, clid
->cl_id
, boot_time
);
898 * XXX Should we use a slab cache ?
899 * This type of memory management is somewhat inefficient, but we use it
900 * anyway since SETCLIENTID is not a common operation.
902 static struct nfs4_client
*alloc_client(struct xdr_netobj name
)
904 struct nfs4_client
*clp
;
906 clp
= kzalloc(sizeof(struct nfs4_client
), GFP_KERNEL
);
909 clp
->cl_name
.data
= kmalloc(name
.len
, GFP_KERNEL
);
910 if (clp
->cl_name
.data
== NULL
) {
914 memcpy(clp
->cl_name
.data
, name
.data
, name
.len
);
915 clp
->cl_name
.len
= name
.len
;
920 free_client(struct nfs4_client
*clp
)
922 while (!list_empty(&clp
->cl_sessions
)) {
923 struct nfsd4_session
*ses
;
924 ses
= list_entry(clp
->cl_sessions
.next
, struct nfsd4_session
,
926 list_del(&ses
->se_perclnt
);
927 nfsd4_put_session(ses
);
929 if (clp
->cl_cred
.cr_group_info
)
930 put_group_info(clp
->cl_cred
.cr_group_info
);
931 kfree(clp
->cl_principal
);
932 kfree(clp
->cl_name
.data
);
937 release_session_client(struct nfsd4_session
*session
)
939 struct nfs4_client
*clp
= session
->se_client
;
941 if (!atomic_dec_and_lock(&clp
->cl_refcount
, &client_lock
))
943 if (is_client_expired(clp
)) {
945 session
->se_client
= NULL
;
947 renew_client_locked(clp
);
948 spin_unlock(&client_lock
);
951 /* must be called under the client_lock */
953 unhash_client_locked(struct nfs4_client
*clp
)
955 struct nfsd4_session
*ses
;
957 mark_client_expired(clp
);
958 list_del(&clp
->cl_lru
);
959 spin_lock(&clp
->cl_lock
);
960 list_for_each_entry(ses
, &clp
->cl_sessions
, se_perclnt
)
961 list_del_init(&ses
->se_hash
);
962 spin_unlock(&clp
->cl_lock
);
966 expire_client(struct nfs4_client
*clp
)
968 struct nfs4_openowner
*oo
;
969 struct nfs4_delegation
*dp
;
970 struct list_head reaplist
;
972 INIT_LIST_HEAD(&reaplist
);
973 spin_lock(&recall_lock
);
974 while (!list_empty(&clp
->cl_delegations
)) {
975 dp
= list_entry(clp
->cl_delegations
.next
, struct nfs4_delegation
, dl_perclnt
);
976 list_del_init(&dp
->dl_perclnt
);
977 list_move(&dp
->dl_recall_lru
, &reaplist
);
979 spin_unlock(&recall_lock
);
980 while (!list_empty(&reaplist
)) {
981 dp
= list_entry(reaplist
.next
, struct nfs4_delegation
, dl_recall_lru
);
982 list_del_init(&dp
->dl_recall_lru
);
983 unhash_delegation(dp
);
985 while (!list_empty(&clp
->cl_openowners
)) {
986 oo
= list_entry(clp
->cl_openowners
.next
, struct nfs4_openowner
, oo_perclient
);
987 release_openowner(oo
);
989 nfsd4_shutdown_callback(clp
);
990 if (clp
->cl_cb_conn
.cb_xprt
)
991 svc_xprt_put(clp
->cl_cb_conn
.cb_xprt
);
992 list_del(&clp
->cl_idhash
);
993 list_del(&clp
->cl_strhash
);
994 spin_lock(&client_lock
);
995 unhash_client_locked(clp
);
996 if (atomic_read(&clp
->cl_refcount
) == 0)
998 spin_unlock(&client_lock
);
1001 static void copy_verf(struct nfs4_client
*target
, nfs4_verifier
*source
)
1003 memcpy(target
->cl_verifier
.data
, source
->data
,
1004 sizeof(target
->cl_verifier
.data
));
1007 static void copy_clid(struct nfs4_client
*target
, struct nfs4_client
*source
)
1009 target
->cl_clientid
.cl_boot
= source
->cl_clientid
.cl_boot
;
1010 target
->cl_clientid
.cl_id
= source
->cl_clientid
.cl_id
;
1013 static void copy_cred(struct svc_cred
*target
, struct svc_cred
*source
)
1015 target
->cr_uid
= source
->cr_uid
;
1016 target
->cr_gid
= source
->cr_gid
;
1017 target
->cr_group_info
= source
->cr_group_info
;
1018 get_group_info(target
->cr_group_info
);
1021 static int same_name(const char *n1
, const char *n2
)
1023 return 0 == memcmp(n1
, n2
, HEXDIR_LEN
);
1027 same_verf(nfs4_verifier
*v1
, nfs4_verifier
*v2
)
1029 return 0 == memcmp(v1
->data
, v2
->data
, sizeof(v1
->data
));
1033 same_clid(clientid_t
*cl1
, clientid_t
*cl2
)
1035 return (cl1
->cl_boot
== cl2
->cl_boot
) && (cl1
->cl_id
== cl2
->cl_id
);
1038 /* XXX what about NGROUP */
1040 same_creds(struct svc_cred
*cr1
, struct svc_cred
*cr2
)
1042 return cr1
->cr_uid
== cr2
->cr_uid
;
1045 static void gen_clid(struct nfs4_client
*clp
)
1047 static u32 current_clientid
= 1;
1049 clp
->cl_clientid
.cl_boot
= boot_time
;
1050 clp
->cl_clientid
.cl_id
= current_clientid
++;
1053 static void gen_confirm(struct nfs4_client
*clp
)
1058 p
= (u32
*)clp
->cl_confirm
.data
;
1059 *p
++ = get_seconds();
1064 same_stateid(stateid_t
*id_one
, stateid_t
*id_two
)
1066 if (id_one
->si_stateownerid
!= id_two
->si_stateownerid
)
1068 return id_one
->si_fileid
== id_two
->si_fileid
;
1071 static struct nfs4_stateid
*find_stateid(stateid_t
*t
, int flags
)
1073 struct nfs4_stateid
*s
;
1074 unsigned int hashval
;
1076 hashval
= stateid_hashval(t
->si_stateownerid
, t
->si_fileid
);
1077 list_for_each_entry(s
, &stateid_hashtbl
[hashval
], st_hash
) {
1078 if (!same_stateid(&s
->st_stateid
, t
))
1080 if (flags
& LOCK_STATE
&& s
->st_type
!= NFS4_LOCK_STID
)
1082 if (flags
& OPEN_STATE
&& s
->st_type
!= NFS4_OPEN_STID
)
1089 static struct nfs4_client
*create_client(struct xdr_netobj name
, char *recdir
,
1090 struct svc_rqst
*rqstp
, nfs4_verifier
*verf
)
1092 struct nfs4_client
*clp
;
1093 struct sockaddr
*sa
= svc_addr(rqstp
);
1096 clp
= alloc_client(name
);
1100 INIT_LIST_HEAD(&clp
->cl_sessions
);
1102 princ
= svc_gss_principal(rqstp
);
1104 clp
->cl_principal
= kstrdup(princ
, GFP_KERNEL
);
1105 if (clp
->cl_principal
== NULL
) {
1111 memcpy(clp
->cl_recdir
, recdir
, HEXDIR_LEN
);
1112 atomic_set(&clp
->cl_refcount
, 0);
1113 clp
->cl_cb_state
= NFSD4_CB_UNKNOWN
;
1114 INIT_LIST_HEAD(&clp
->cl_idhash
);
1115 INIT_LIST_HEAD(&clp
->cl_strhash
);
1116 INIT_LIST_HEAD(&clp
->cl_openowners
);
1117 INIT_LIST_HEAD(&clp
->cl_delegations
);
1118 INIT_LIST_HEAD(&clp
->cl_lru
);
1119 INIT_LIST_HEAD(&clp
->cl_callbacks
);
1120 spin_lock_init(&clp
->cl_lock
);
1121 INIT_WORK(&clp
->cl_cb_null
.cb_work
, nfsd4_do_callback_rpc
);
1122 clp
->cl_time
= get_seconds();
1123 clear_bit(0, &clp
->cl_cb_slot_busy
);
1124 rpc_init_wait_queue(&clp
->cl_cb_waitq
, "Backchannel slot table");
1125 copy_verf(clp
, verf
);
1126 rpc_copy_addr((struct sockaddr
*) &clp
->cl_addr
, sa
);
1127 clp
->cl_flavor
= rqstp
->rq_flavor
;
1128 copy_cred(&clp
->cl_cred
, &rqstp
->rq_cred
);
1130 clp
->cl_cb_session
= NULL
;
1134 static int check_name(struct xdr_netobj name
)
1138 if (name
.len
> NFS4_OPAQUE_LIMIT
) {
1139 dprintk("NFSD: check_name: name too long(%d)!\n", name
.len
);
1146 add_to_unconfirmed(struct nfs4_client
*clp
, unsigned int strhashval
)
1148 unsigned int idhashval
;
1150 list_add(&clp
->cl_strhash
, &unconf_str_hashtbl
[strhashval
]);
1151 idhashval
= clientid_hashval(clp
->cl_clientid
.cl_id
);
1152 list_add(&clp
->cl_idhash
, &unconf_id_hashtbl
[idhashval
]);
1157 move_to_confirmed(struct nfs4_client
*clp
)
1159 unsigned int idhashval
= clientid_hashval(clp
->cl_clientid
.cl_id
);
1160 unsigned int strhashval
;
1162 dprintk("NFSD: move_to_confirm nfs4_client %p\n", clp
);
1163 list_move(&clp
->cl_idhash
, &conf_id_hashtbl
[idhashval
]);
1164 strhashval
= clientstr_hashval(clp
->cl_recdir
);
1165 list_move(&clp
->cl_strhash
, &conf_str_hashtbl
[strhashval
]);
1169 static struct nfs4_client
*
1170 find_confirmed_client(clientid_t
*clid
)
1172 struct nfs4_client
*clp
;
1173 unsigned int idhashval
= clientid_hashval(clid
->cl_id
);
1175 list_for_each_entry(clp
, &conf_id_hashtbl
[idhashval
], cl_idhash
) {
1176 if (same_clid(&clp
->cl_clientid
, clid
))
1182 static struct nfs4_client
*
1183 find_unconfirmed_client(clientid_t
*clid
)
1185 struct nfs4_client
*clp
;
1186 unsigned int idhashval
= clientid_hashval(clid
->cl_id
);
1188 list_for_each_entry(clp
, &unconf_id_hashtbl
[idhashval
], cl_idhash
) {
1189 if (same_clid(&clp
->cl_clientid
, clid
))
1195 static bool clp_used_exchangeid(struct nfs4_client
*clp
)
1197 return clp
->cl_exchange_flags
!= 0;
1200 static struct nfs4_client
*
1201 find_confirmed_client_by_str(const char *dname
, unsigned int hashval
)
1203 struct nfs4_client
*clp
;
1205 list_for_each_entry(clp
, &conf_str_hashtbl
[hashval
], cl_strhash
) {
1206 if (same_name(clp
->cl_recdir
, dname
))
1212 static struct nfs4_client
*
1213 find_unconfirmed_client_by_str(const char *dname
, unsigned int hashval
)
1215 struct nfs4_client
*clp
;
1217 list_for_each_entry(clp
, &unconf_str_hashtbl
[hashval
], cl_strhash
) {
1218 if (same_name(clp
->cl_recdir
, dname
))
1224 static void rpc_svcaddr2sockaddr(struct sockaddr
*sa
, unsigned short family
, union svc_addr_u
*svcaddr
)
1228 ((struct sockaddr_in
*)sa
)->sin_family
= AF_INET
;
1229 ((struct sockaddr_in
*)sa
)->sin_addr
= svcaddr
->addr
;
1232 ((struct sockaddr_in6
*)sa
)->sin6_family
= AF_INET6
;
1233 ((struct sockaddr_in6
*)sa
)->sin6_addr
= svcaddr
->addr6
;
1239 gen_callback(struct nfs4_client
*clp
, struct nfsd4_setclientid
*se
, struct svc_rqst
*rqstp
)
1241 struct nfs4_cb_conn
*conn
= &clp
->cl_cb_conn
;
1242 struct sockaddr
*sa
= svc_addr(rqstp
);
1243 u32 scopeid
= rpc_get_scope_id(sa
);
1244 unsigned short expected_family
;
1246 /* Currently, we only support tcp and tcp6 for the callback channel */
1247 if (se
->se_callback_netid_len
== 3 &&
1248 !memcmp(se
->se_callback_netid_val
, "tcp", 3))
1249 expected_family
= AF_INET
;
1250 else if (se
->se_callback_netid_len
== 4 &&
1251 !memcmp(se
->se_callback_netid_val
, "tcp6", 4))
1252 expected_family
= AF_INET6
;
1256 conn
->cb_addrlen
= rpc_uaddr2sockaddr(se
->se_callback_addr_val
,
1257 se
->se_callback_addr_len
,
1258 (struct sockaddr
*)&conn
->cb_addr
,
1259 sizeof(conn
->cb_addr
));
1261 if (!conn
->cb_addrlen
|| conn
->cb_addr
.ss_family
!= expected_family
)
1264 if (conn
->cb_addr
.ss_family
== AF_INET6
)
1265 ((struct sockaddr_in6
*)&conn
->cb_addr
)->sin6_scope_id
= scopeid
;
1267 conn
->cb_prog
= se
->se_callback_prog
;
1268 conn
->cb_ident
= se
->se_callback_ident
;
1269 rpc_svcaddr2sockaddr((struct sockaddr
*)&conn
->cb_saddr
, expected_family
, &rqstp
->rq_daddr
);
1272 conn
->cb_addr
.ss_family
= AF_UNSPEC
;
1273 conn
->cb_addrlen
= 0;
1274 dprintk(KERN_INFO
"NFSD: this client (clientid %08x/%08x) "
1275 "will not receive delegations\n",
1276 clp
->cl_clientid
.cl_boot
, clp
->cl_clientid
.cl_id
);
1282 * Cache a reply. nfsd4_check_drc_limit() has bounded the cache size.
1285 nfsd4_store_cache_entry(struct nfsd4_compoundres
*resp
)
1287 struct nfsd4_slot
*slot
= resp
->cstate
.slot
;
1290 dprintk("--> %s slot %p\n", __func__
, slot
);
1292 slot
->sl_opcnt
= resp
->opcnt
;
1293 slot
->sl_status
= resp
->cstate
.status
;
1295 if (nfsd4_not_cached(resp
)) {
1296 slot
->sl_datalen
= 0;
1299 slot
->sl_datalen
= (char *)resp
->p
- (char *)resp
->cstate
.datap
;
1300 base
= (char *)resp
->cstate
.datap
-
1301 (char *)resp
->xbuf
->head
[0].iov_base
;
1302 if (read_bytes_from_xdr_buf(resp
->xbuf
, base
, slot
->sl_data
,
1304 WARN("%s: sessions DRC could not cache compound\n", __func__
);
1309 * Encode the replay sequence operation from the slot values.
1310 * If cachethis is FALSE encode the uncached rep error on the next
1311 * operation which sets resp->p and increments resp->opcnt for
1312 * nfs4svc_encode_compoundres.
1316 nfsd4_enc_sequence_replay(struct nfsd4_compoundargs
*args
,
1317 struct nfsd4_compoundres
*resp
)
1319 struct nfsd4_op
*op
;
1320 struct nfsd4_slot
*slot
= resp
->cstate
.slot
;
1322 dprintk("--> %s resp->opcnt %d cachethis %u \n", __func__
,
1323 resp
->opcnt
, resp
->cstate
.slot
->sl_cachethis
);
1325 /* Encode the replayed sequence operation */
1326 op
= &args
->ops
[resp
->opcnt
- 1];
1327 nfsd4_encode_operation(resp
, op
);
1329 /* Return nfserr_retry_uncached_rep in next operation. */
1330 if (args
->opcnt
> 1 && slot
->sl_cachethis
== 0) {
1331 op
= &args
->ops
[resp
->opcnt
++];
1332 op
->status
= nfserr_retry_uncached_rep
;
1333 nfsd4_encode_operation(resp
, op
);
1339 * The sequence operation is not cached because we can use the slot and
1343 nfsd4_replay_cache_entry(struct nfsd4_compoundres
*resp
,
1344 struct nfsd4_sequence
*seq
)
1346 struct nfsd4_slot
*slot
= resp
->cstate
.slot
;
1349 dprintk("--> %s slot %p\n", __func__
, slot
);
1351 /* Either returns 0 or nfserr_retry_uncached */
1352 status
= nfsd4_enc_sequence_replay(resp
->rqstp
->rq_argp
, resp
);
1353 if (status
== nfserr_retry_uncached_rep
)
1356 /* The sequence operation has been encoded, cstate->datap set. */
1357 memcpy(resp
->cstate
.datap
, slot
->sl_data
, slot
->sl_datalen
);
1359 resp
->opcnt
= slot
->sl_opcnt
;
1360 resp
->p
= resp
->cstate
.datap
+ XDR_QUADLEN(slot
->sl_datalen
);
1361 status
= slot
->sl_status
;
1367 * Set the exchange_id flags returned by the server.
1370 nfsd4_set_ex_flags(struct nfs4_client
*new, struct nfsd4_exchange_id
*clid
)
1372 /* pNFS is not supported */
1373 new->cl_exchange_flags
|= EXCHGID4_FLAG_USE_NON_PNFS
;
1375 /* Referrals are supported, Migration is not. */
1376 new->cl_exchange_flags
|= EXCHGID4_FLAG_SUPP_MOVED_REFER
;
1378 /* set the wire flags to return to client. */
1379 clid
->flags
= new->cl_exchange_flags
;
1383 nfsd4_exchange_id(struct svc_rqst
*rqstp
,
1384 struct nfsd4_compound_state
*cstate
,
1385 struct nfsd4_exchange_id
*exid
)
1387 struct nfs4_client
*unconf
, *conf
, *new;
1389 unsigned int strhashval
;
1390 char dname
[HEXDIR_LEN
];
1391 char addr_str
[INET6_ADDRSTRLEN
];
1392 nfs4_verifier verf
= exid
->verifier
;
1393 struct sockaddr
*sa
= svc_addr(rqstp
);
1395 rpc_ntop(sa
, addr_str
, sizeof(addr_str
));
1396 dprintk("%s rqstp=%p exid=%p clname.len=%u clname.data=%p "
1397 "ip_addr=%s flags %x, spa_how %d\n",
1398 __func__
, rqstp
, exid
, exid
->clname
.len
, exid
->clname
.data
,
1399 addr_str
, exid
->flags
, exid
->spa_how
);
1401 if (!check_name(exid
->clname
) || (exid
->flags
& ~EXCHGID4_FLAG_MASK_A
))
1402 return nfserr_inval
;
1404 /* Currently only support SP4_NONE */
1405 switch (exid
->spa_how
) {
1409 return nfserr_serverfault
;
1411 BUG(); /* checked by xdr code */
1413 return nfserr_serverfault
; /* no excuse :-/ */
1416 status
= nfs4_make_rec_clidname(dname
, &exid
->clname
);
1421 strhashval
= clientstr_hashval(dname
);
1426 conf
= find_confirmed_client_by_str(dname
, strhashval
);
1428 if (!clp_used_exchangeid(conf
)) {
1429 status
= nfserr_clid_inuse
; /* XXX: ? */
1432 if (!same_verf(&verf
, &conf
->cl_verifier
)) {
1433 /* 18.35.4 case 8 */
1434 if (exid
->flags
& EXCHGID4_FLAG_UPD_CONFIRMED_REC_A
) {
1435 status
= nfserr_not_same
;
1438 /* Client reboot: destroy old state */
1439 expire_client(conf
);
1442 if (!same_creds(&conf
->cl_cred
, &rqstp
->rq_cred
)) {
1443 /* 18.35.4 case 9 */
1444 if (exid
->flags
& EXCHGID4_FLAG_UPD_CONFIRMED_REC_A
) {
1445 status
= nfserr_perm
;
1448 expire_client(conf
);
1452 * Set bit when the owner id and verifier map to an already
1453 * confirmed client id (18.35.3).
1455 exid
->flags
|= EXCHGID4_FLAG_CONFIRMED_R
;
1458 * Falling into 18.35.4 case 2, possible router replay.
1459 * Leave confirmed record intact and return same result.
1461 copy_verf(conf
, &verf
);
1466 /* 18.35.4 case 7 */
1467 if (exid
->flags
& EXCHGID4_FLAG_UPD_CONFIRMED_REC_A
) {
1468 status
= nfserr_noent
;
1472 unconf
= find_unconfirmed_client_by_str(dname
, strhashval
);
1475 * Possible retry or client restart. Per 18.35.4 case 4,
1476 * a new unconfirmed record should be generated regardless
1477 * of whether any properties have changed.
1479 expire_client(unconf
);
1484 new = create_client(exid
->clname
, dname
, rqstp
, &verf
);
1486 status
= nfserr_jukebox
;
1491 add_to_unconfirmed(new, strhashval
);
1493 exid
->clientid
.cl_boot
= new->cl_clientid
.cl_boot
;
1494 exid
->clientid
.cl_id
= new->cl_clientid
.cl_id
;
1497 nfsd4_set_ex_flags(new, exid
);
1499 dprintk("nfsd4_exchange_id seqid %d flags %x\n",
1500 new->cl_cs_slot
.sl_seqid
, new->cl_exchange_flags
);
1504 nfs4_unlock_state();
1506 dprintk("nfsd4_exchange_id returns %d\n", ntohl(status
));
1511 check_slot_seqid(u32 seqid
, u32 slot_seqid
, int slot_inuse
)
1513 dprintk("%s enter. seqid %d slot_seqid %d\n", __func__
, seqid
,
1516 /* The slot is in use, and no response has been sent. */
1518 if (seqid
== slot_seqid
)
1519 return nfserr_jukebox
;
1521 return nfserr_seq_misordered
;
1524 if (likely(seqid
== slot_seqid
+ 1))
1527 if (seqid
== slot_seqid
)
1528 return nfserr_replay_cache
;
1530 if (seqid
== 1 && (slot_seqid
+ 1) == 0)
1532 /* Misordered replay or misordered new request */
1533 return nfserr_seq_misordered
;
1537 * Cache the create session result into the create session single DRC
1538 * slot cache by saving the xdr structure. sl_seqid has been set.
1539 * Do this for solo or embedded create session operations.
1542 nfsd4_cache_create_session(struct nfsd4_create_session
*cr_ses
,
1543 struct nfsd4_clid_slot
*slot
, int nfserr
)
1545 slot
->sl_status
= nfserr
;
1546 memcpy(&slot
->sl_cr_ses
, cr_ses
, sizeof(*cr_ses
));
1550 nfsd4_replay_create_session(struct nfsd4_create_session
*cr_ses
,
1551 struct nfsd4_clid_slot
*slot
)
1553 memcpy(cr_ses
, &slot
->sl_cr_ses
, sizeof(*cr_ses
));
1554 return slot
->sl_status
;
1557 #define NFSD_MIN_REQ_HDR_SEQ_SZ ((\
1558 2 * 2 + /* credential,verifier: AUTH_NULL, length 0 */ \
1559 1 + /* MIN tag is length with zero, only length */ \
1560 3 + /* version, opcount, opcode */ \
1561 XDR_QUADLEN(NFS4_MAX_SESSIONID_LEN) + \
1562 /* seqid, slotID, slotID, cache */ \
1563 4 ) * sizeof(__be32))
1565 #define NFSD_MIN_RESP_HDR_SEQ_SZ ((\
1566 2 + /* verifier: AUTH_NULL, length 0 */\
1568 1 + /* MIN tag is length with zero, only length */ \
1569 3 + /* opcount, opcode, opstatus*/ \
1570 XDR_QUADLEN(NFS4_MAX_SESSIONID_LEN) + \
1571 /* seqid, slotID, slotID, slotID, status */ \
1572 5 ) * sizeof(__be32))
1574 static __be32
check_forechannel_attrs(struct nfsd4_channel_attrs fchannel
)
1576 return fchannel
.maxreq_sz
< NFSD_MIN_REQ_HDR_SEQ_SZ
1577 || fchannel
.maxresp_sz
< NFSD_MIN_RESP_HDR_SEQ_SZ
;
1581 nfsd4_create_session(struct svc_rqst
*rqstp
,
1582 struct nfsd4_compound_state
*cstate
,
1583 struct nfsd4_create_session
*cr_ses
)
1585 struct sockaddr
*sa
= svc_addr(rqstp
);
1586 struct nfs4_client
*conf
, *unconf
;
1587 struct nfsd4_session
*new;
1588 struct nfsd4_clid_slot
*cs_slot
= NULL
;
1589 bool confirm_me
= false;
1592 if (cr_ses
->flags
& ~SESSION4_FLAG_MASK_A
)
1593 return nfserr_inval
;
1596 unconf
= find_unconfirmed_client(&cr_ses
->clientid
);
1597 conf
= find_confirmed_client(&cr_ses
->clientid
);
1600 cs_slot
= &conf
->cl_cs_slot
;
1601 status
= check_slot_seqid(cr_ses
->seqid
, cs_slot
->sl_seqid
, 0);
1602 if (status
== nfserr_replay_cache
) {
1603 dprintk("Got a create_session replay! seqid= %d\n",
1605 /* Return the cached reply status */
1606 status
= nfsd4_replay_create_session(cr_ses
, cs_slot
);
1608 } else if (cr_ses
->seqid
!= cs_slot
->sl_seqid
+ 1) {
1609 status
= nfserr_seq_misordered
;
1610 dprintk("Sequence misordered!\n");
1611 dprintk("Expected seqid= %d but got seqid= %d\n",
1612 cs_slot
->sl_seqid
, cr_ses
->seqid
);
1615 } else if (unconf
) {
1616 if (!same_creds(&unconf
->cl_cred
, &rqstp
->rq_cred
) ||
1617 !rpc_cmp_addr(sa
, (struct sockaddr
*) &unconf
->cl_addr
)) {
1618 status
= nfserr_clid_inuse
;
1622 cs_slot
= &unconf
->cl_cs_slot
;
1623 status
= check_slot_seqid(cr_ses
->seqid
, cs_slot
->sl_seqid
, 0);
1625 /* an unconfirmed replay returns misordered */
1626 status
= nfserr_seq_misordered
;
1633 status
= nfserr_stale_clientid
;
1638 * XXX: we should probably set this at creation time, and check
1639 * for consistent minorversion use throughout:
1641 conf
->cl_minorversion
= 1;
1643 * We do not support RDMA or persistent sessions
1645 cr_ses
->flags
&= ~SESSION4_PERSIST
;
1646 cr_ses
->flags
&= ~SESSION4_RDMA
;
1648 status
= nfserr_toosmall
;
1649 if (check_forechannel_attrs(cr_ses
->fore_channel
))
1652 status
= nfserr_jukebox
;
1653 new = alloc_init_session(rqstp
, conf
, cr_ses
);
1657 memcpy(cr_ses
->sessionid
.data
, new->se_sessionid
.data
,
1658 NFS4_MAX_SESSIONID_LEN
);
1659 memcpy(&cr_ses
->fore_channel
, &new->se_fchannel
,
1660 sizeof(struct nfsd4_channel_attrs
));
1661 cs_slot
->sl_seqid
++;
1662 cr_ses
->seqid
= cs_slot
->sl_seqid
;
1664 /* cache solo and embedded create sessions under the state lock */
1665 nfsd4_cache_create_session(cr_ses
, cs_slot
, status
);
1667 move_to_confirmed(conf
);
1669 nfs4_unlock_state();
1670 dprintk("%s returns %d\n", __func__
, ntohl(status
));
1674 static bool nfsd4_last_compound_op(struct svc_rqst
*rqstp
)
1676 struct nfsd4_compoundres
*resp
= rqstp
->rq_resp
;
1677 struct nfsd4_compoundargs
*argp
= rqstp
->rq_argp
;
1679 return argp
->opcnt
== resp
->opcnt
;
1682 static __be32
nfsd4_map_bcts_dir(u32
*dir
)
1685 case NFS4_CDFC4_FORE
:
1686 case NFS4_CDFC4_BACK
:
1688 case NFS4_CDFC4_FORE_OR_BOTH
:
1689 case NFS4_CDFC4_BACK_OR_BOTH
:
1690 *dir
= NFS4_CDFC4_BOTH
;
1693 return nfserr_inval
;
1696 __be32
nfsd4_bind_conn_to_session(struct svc_rqst
*rqstp
,
1697 struct nfsd4_compound_state
*cstate
,
1698 struct nfsd4_bind_conn_to_session
*bcts
)
1702 if (!nfsd4_last_compound_op(rqstp
))
1703 return nfserr_not_only_op
;
1704 spin_lock(&client_lock
);
1705 cstate
->session
= find_in_sessionid_hashtbl(&bcts
->sessionid
);
1706 /* Sorta weird: we only need the refcnt'ing because new_conn acquires
1707 * client_lock iself: */
1708 if (cstate
->session
) {
1709 nfsd4_get_session(cstate
->session
);
1710 atomic_inc(&cstate
->session
->se_client
->cl_refcount
);
1712 spin_unlock(&client_lock
);
1713 if (!cstate
->session
)
1714 return nfserr_badsession
;
1716 status
= nfsd4_map_bcts_dir(&bcts
->dir
);
1718 nfsd4_new_conn(rqstp
, cstate
->session
, bcts
->dir
);
1722 static bool nfsd4_compound_in_session(struct nfsd4_session
*session
, struct nfs4_sessionid
*sid
)
1726 return !memcmp(sid
, &session
->se_sessionid
, sizeof(*sid
));
1730 nfsd4_destroy_session(struct svc_rqst
*r
,
1731 struct nfsd4_compound_state
*cstate
,
1732 struct nfsd4_destroy_session
*sessionid
)
1734 struct nfsd4_session
*ses
;
1735 u32 status
= nfserr_badsession
;
1738 * - The confirmed nfs4_client->cl_sessionid holds destroyed sessinid
1739 * - Should we return nfserr_back_chan_busy if waiting for
1740 * callbacks on to-be-destroyed session?
1741 * - Do we need to clear any callback info from previous session?
1744 if (nfsd4_compound_in_session(cstate
->session
, &sessionid
->sessionid
)) {
1745 if (!nfsd4_last_compound_op(r
))
1746 return nfserr_not_only_op
;
1748 dump_sessionid(__func__
, &sessionid
->sessionid
);
1749 spin_lock(&client_lock
);
1750 ses
= find_in_sessionid_hashtbl(&sessionid
->sessionid
);
1752 spin_unlock(&client_lock
);
1756 unhash_session(ses
);
1757 spin_unlock(&client_lock
);
1760 nfsd4_probe_callback_sync(ses
->se_client
);
1761 nfs4_unlock_state();
1763 nfsd4_del_conns(ses
);
1765 nfsd4_put_session(ses
);
1768 dprintk("%s returns %d\n", __func__
, ntohl(status
));
1772 static struct nfsd4_conn
*__nfsd4_find_conn(struct svc_xprt
*xpt
, struct nfsd4_session
*s
)
1774 struct nfsd4_conn
*c
;
1776 list_for_each_entry(c
, &s
->se_conns
, cn_persession
) {
1777 if (c
->cn_xprt
== xpt
) {
1784 static void nfsd4_sequence_check_conn(struct nfsd4_conn
*new, struct nfsd4_session
*ses
)
1786 struct nfs4_client
*clp
= ses
->se_client
;
1787 struct nfsd4_conn
*c
;
1790 spin_lock(&clp
->cl_lock
);
1791 c
= __nfsd4_find_conn(new->cn_xprt
, ses
);
1793 spin_unlock(&clp
->cl_lock
);
1797 __nfsd4_hash_conn(new, ses
);
1798 spin_unlock(&clp
->cl_lock
);
1799 ret
= nfsd4_register_conn(new);
1801 /* oops; xprt is already down: */
1802 nfsd4_conn_lost(&new->cn_xpt_user
);
1806 static bool nfsd4_session_too_many_ops(struct svc_rqst
*rqstp
, struct nfsd4_session
*session
)
1808 struct nfsd4_compoundargs
*args
= rqstp
->rq_argp
;
1810 return args
->opcnt
> session
->se_fchannel
.maxops
;
1813 static bool nfsd4_request_too_big(struct svc_rqst
*rqstp
,
1814 struct nfsd4_session
*session
)
1816 struct xdr_buf
*xb
= &rqstp
->rq_arg
;
1818 return xb
->len
> session
->se_fchannel
.maxreq_sz
;
1822 nfsd4_sequence(struct svc_rqst
*rqstp
,
1823 struct nfsd4_compound_state
*cstate
,
1824 struct nfsd4_sequence
*seq
)
1826 struct nfsd4_compoundres
*resp
= rqstp
->rq_resp
;
1827 struct nfsd4_session
*session
;
1828 struct nfsd4_slot
*slot
;
1829 struct nfsd4_conn
*conn
;
1832 if (resp
->opcnt
!= 1)
1833 return nfserr_sequence_pos
;
1836 * Will be either used or freed by nfsd4_sequence_check_conn
1839 conn
= alloc_conn(rqstp
, NFS4_CDFC4_FORE
);
1841 return nfserr_jukebox
;
1843 spin_lock(&client_lock
);
1844 status
= nfserr_badsession
;
1845 session
= find_in_sessionid_hashtbl(&seq
->sessionid
);
1849 status
= nfserr_too_many_ops
;
1850 if (nfsd4_session_too_many_ops(rqstp
, session
))
1853 status
= nfserr_req_too_big
;
1854 if (nfsd4_request_too_big(rqstp
, session
))
1857 status
= nfserr_badslot
;
1858 if (seq
->slotid
>= session
->se_fchannel
.maxreqs
)
1861 slot
= session
->se_slots
[seq
->slotid
];
1862 dprintk("%s: slotid %d\n", __func__
, seq
->slotid
);
1864 /* We do not negotiate the number of slots yet, so set the
1865 * maxslots to the session maxreqs which is used to encode
1866 * sr_highest_slotid and the sr_target_slot id to maxslots */
1867 seq
->maxslots
= session
->se_fchannel
.maxreqs
;
1869 status
= check_slot_seqid(seq
->seqid
, slot
->sl_seqid
, slot
->sl_inuse
);
1870 if (status
== nfserr_replay_cache
) {
1871 cstate
->slot
= slot
;
1872 cstate
->session
= session
;
1873 /* Return the cached reply status and set cstate->status
1874 * for nfsd4_proc_compound processing */
1875 status
= nfsd4_replay_cache_entry(resp
, seq
);
1876 cstate
->status
= nfserr_replay_cache
;
1882 nfsd4_sequence_check_conn(conn
, session
);
1885 /* Success! bump slot seqid */
1886 slot
->sl_inuse
= true;
1887 slot
->sl_seqid
= seq
->seqid
;
1888 slot
->sl_cachethis
= seq
->cachethis
;
1890 cstate
->slot
= slot
;
1891 cstate
->session
= session
;
1894 /* Hold a session reference until done processing the compound. */
1895 if (cstate
->session
) {
1896 struct nfs4_client
*clp
= session
->se_client
;
1898 nfsd4_get_session(cstate
->session
);
1899 atomic_inc(&clp
->cl_refcount
);
1900 if (clp
->cl_cb_state
== NFSD4_CB_DOWN
)
1901 seq
->status_flags
|= SEQ4_STATUS_CB_PATH_DOWN
;
1904 spin_unlock(&client_lock
);
1905 dprintk("%s: return %d\n", __func__
, ntohl(status
));
1910 nfsd4_reclaim_complete(struct svc_rqst
*rqstp
, struct nfsd4_compound_state
*cstate
, struct nfsd4_reclaim_complete
*rc
)
1914 if (rc
->rca_one_fs
) {
1915 if (!cstate
->current_fh
.fh_dentry
)
1916 return nfserr_nofilehandle
;
1918 * We don't take advantage of the rca_one_fs case.
1919 * That's OK, it's optional, we can safely ignore it.
1925 status
= nfserr_complete_already
;
1926 if (cstate
->session
->se_client
->cl_firststate
)
1929 status
= nfserr_stale_clientid
;
1930 if (is_client_expired(cstate
->session
->se_client
))
1932 * The following error isn't really legal.
1933 * But we only get here if the client just explicitly
1934 * destroyed the client. Surely it no longer cares what
1935 * error it gets back on an operation for the dead
1941 nfsd4_create_clid_dir(cstate
->session
->se_client
);
1943 nfs4_unlock_state();
1948 nfsd4_setclientid(struct svc_rqst
*rqstp
, struct nfsd4_compound_state
*cstate
,
1949 struct nfsd4_setclientid
*setclid
)
1951 struct xdr_netobj clname
= {
1952 .len
= setclid
->se_namelen
,
1953 .data
= setclid
->se_name
,
1955 nfs4_verifier clverifier
= setclid
->se_verf
;
1956 unsigned int strhashval
;
1957 struct nfs4_client
*conf
, *unconf
, *new;
1959 char dname
[HEXDIR_LEN
];
1961 if (!check_name(clname
))
1962 return nfserr_inval
;
1964 status
= nfs4_make_rec_clidname(dname
, &clname
);
1969 * XXX The Duplicate Request Cache (DRC) has been checked (??)
1970 * We get here on a DRC miss.
1973 strhashval
= clientstr_hashval(dname
);
1976 conf
= find_confirmed_client_by_str(dname
, strhashval
);
1978 /* RFC 3530 14.2.33 CASE 0: */
1979 status
= nfserr_clid_inuse
;
1980 if (clp_used_exchangeid(conf
))
1982 if (!same_creds(&conf
->cl_cred
, &rqstp
->rq_cred
)) {
1983 char addr_str
[INET6_ADDRSTRLEN
];
1984 rpc_ntop((struct sockaddr
*) &conf
->cl_addr
, addr_str
,
1986 dprintk("NFSD: setclientid: string in use by client "
1987 "at %s\n", addr_str
);
1992 * section 14.2.33 of RFC 3530 (under the heading "IMPLEMENTATION")
1993 * has a description of SETCLIENTID request processing consisting
1994 * of 5 bullet points, labeled as CASE0 - CASE4 below.
1996 unconf
= find_unconfirmed_client_by_str(dname
, strhashval
);
1997 status
= nfserr_jukebox
;
2000 * RFC 3530 14.2.33 CASE 4:
2001 * placed first, because it is the normal case
2004 expire_client(unconf
);
2005 new = create_client(clname
, dname
, rqstp
, &clverifier
);
2009 } else if (same_verf(&conf
->cl_verifier
, &clverifier
)) {
2011 * RFC 3530 14.2.33 CASE 1:
2012 * probable callback update
2015 /* Note this is removing unconfirmed {*x***},
2016 * which is stronger than RFC recommended {vxc**}.
2017 * This has the advantage that there is at most
2018 * one {*x***} in either list at any time.
2020 expire_client(unconf
);
2022 new = create_client(clname
, dname
, rqstp
, &clverifier
);
2025 copy_clid(new, conf
);
2026 } else if (!unconf
) {
2028 * RFC 3530 14.2.33 CASE 2:
2029 * probable client reboot; state will be removed if
2032 new = create_client(clname
, dname
, rqstp
, &clverifier
);
2038 * RFC 3530 14.2.33 CASE 3:
2039 * probable client reboot; state will be removed if
2042 expire_client(unconf
);
2043 new = create_client(clname
, dname
, rqstp
, &clverifier
);
2049 * XXX: we should probably set this at creation time, and check
2050 * for consistent minorversion use throughout:
2052 new->cl_minorversion
= 0;
2053 gen_callback(new, setclid
, rqstp
);
2054 add_to_unconfirmed(new, strhashval
);
2055 setclid
->se_clientid
.cl_boot
= new->cl_clientid
.cl_boot
;
2056 setclid
->se_clientid
.cl_id
= new->cl_clientid
.cl_id
;
2057 memcpy(setclid
->se_confirm
.data
, new->cl_confirm
.data
, sizeof(setclid
->se_confirm
.data
));
2060 nfs4_unlock_state();
2066 * Section 14.2.34 of RFC 3530 (under the heading "IMPLEMENTATION") has
2067 * a description of SETCLIENTID_CONFIRM request processing consisting of 4
2068 * bullets, labeled as CASE1 - CASE4 below.
2071 nfsd4_setclientid_confirm(struct svc_rqst
*rqstp
,
2072 struct nfsd4_compound_state
*cstate
,
2073 struct nfsd4_setclientid_confirm
*setclientid_confirm
)
2075 struct sockaddr
*sa
= svc_addr(rqstp
);
2076 struct nfs4_client
*conf
, *unconf
;
2077 nfs4_verifier confirm
= setclientid_confirm
->sc_confirm
;
2078 clientid_t
* clid
= &setclientid_confirm
->sc_clientid
;
2081 if (STALE_CLIENTID(clid
))
2082 return nfserr_stale_clientid
;
2084 * XXX The Duplicate Request Cache (DRC) has been checked (??)
2085 * We get here on a DRC miss.
2090 conf
= find_confirmed_client(clid
);
2091 unconf
= find_unconfirmed_client(clid
);
2093 status
= nfserr_clid_inuse
;
2094 if (conf
&& !rpc_cmp_addr((struct sockaddr
*) &conf
->cl_addr
, sa
))
2096 if (unconf
&& !rpc_cmp_addr((struct sockaddr
*) &unconf
->cl_addr
, sa
))
2100 * section 14.2.34 of RFC 3530 has a description of
2101 * SETCLIENTID_CONFIRM request processing consisting
2102 * of 4 bullet points, labeled as CASE1 - CASE4 below.
2104 if (conf
&& unconf
&& same_verf(&confirm
, &unconf
->cl_confirm
)) {
2106 * RFC 3530 14.2.34 CASE 1:
2109 if (!same_creds(&conf
->cl_cred
, &unconf
->cl_cred
))
2110 status
= nfserr_clid_inuse
;
2112 nfsd4_change_callback(conf
, &unconf
->cl_cb_conn
);
2113 nfsd4_probe_callback(conf
);
2114 expire_client(unconf
);
2118 } else if (conf
&& !unconf
) {
2120 * RFC 3530 14.2.34 CASE 2:
2121 * probable retransmitted request; play it safe and
2124 if (!same_creds(&conf
->cl_cred
, &rqstp
->rq_cred
))
2125 status
= nfserr_clid_inuse
;
2128 } else if (!conf
&& unconf
2129 && same_verf(&unconf
->cl_confirm
, &confirm
)) {
2131 * RFC 3530 14.2.34 CASE 3:
2132 * Normal case; new or rebooted client:
2134 if (!same_creds(&unconf
->cl_cred
, &rqstp
->rq_cred
)) {
2135 status
= nfserr_clid_inuse
;
2138 clientstr_hashval(unconf
->cl_recdir
);
2139 conf
= find_confirmed_client_by_str(unconf
->cl_recdir
,
2142 nfsd4_remove_clid_dir(conf
);
2143 expire_client(conf
);
2145 move_to_confirmed(unconf
);
2147 nfsd4_probe_callback(conf
);
2150 } else if ((!conf
|| (conf
&& !same_verf(&conf
->cl_confirm
, &confirm
)))
2151 && (!unconf
|| (unconf
&& !same_verf(&unconf
->cl_confirm
,
2154 * RFC 3530 14.2.34 CASE 4:
2155 * Client probably hasn't noticed that we rebooted yet.
2157 status
= nfserr_stale_clientid
;
2159 /* check that we have hit one of the cases...*/
2160 status
= nfserr_clid_inuse
;
2163 nfs4_unlock_state();
2167 /* OPEN Share state helper functions */
2168 static inline struct nfs4_file
*
2169 alloc_init_file(struct inode
*ino
)
2171 struct nfs4_file
*fp
;
2172 unsigned int hashval
= file_hashval(ino
);
2174 fp
= kmem_cache_alloc(file_slab
, GFP_KERNEL
);
2176 atomic_set(&fp
->fi_ref
, 1);
2177 INIT_LIST_HEAD(&fp
->fi_hash
);
2178 INIT_LIST_HEAD(&fp
->fi_stateids
);
2179 INIT_LIST_HEAD(&fp
->fi_delegations
);
2180 fp
->fi_inode
= igrab(ino
);
2181 fp
->fi_id
= current_fileid
++;
2182 fp
->fi_had_conflict
= false;
2183 fp
->fi_lease
= NULL
;
2184 memset(fp
->fi_fds
, 0, sizeof(fp
->fi_fds
));
2185 memset(fp
->fi_access
, 0, sizeof(fp
->fi_access
));
2186 spin_lock(&recall_lock
);
2187 list_add(&fp
->fi_hash
, &file_hashtbl
[hashval
]);
2188 spin_unlock(&recall_lock
);
2195 nfsd4_free_slab(struct kmem_cache
**slab
)
2199 kmem_cache_destroy(*slab
);
2204 nfsd4_free_slabs(void)
2206 nfsd4_free_slab(&openowner_slab
);
2207 nfsd4_free_slab(&lockowner_slab
);
2208 nfsd4_free_slab(&file_slab
);
2209 nfsd4_free_slab(&stateid_slab
);
2210 nfsd4_free_slab(&deleg_slab
);
2214 nfsd4_init_slabs(void)
2216 openowner_slab
= kmem_cache_create("nfsd4_openowners",
2217 sizeof(struct nfs4_openowner
), 0, 0, NULL
);
2218 if (openowner_slab
== NULL
)
2220 lockowner_slab
= kmem_cache_create("nfsd4_lockowners",
2221 sizeof(struct nfs4_openowner
), 0, 0, NULL
);
2222 if (lockowner_slab
== NULL
)
2224 file_slab
= kmem_cache_create("nfsd4_files",
2225 sizeof(struct nfs4_file
), 0, 0, NULL
);
2226 if (file_slab
== NULL
)
2228 stateid_slab
= kmem_cache_create("nfsd4_stateids",
2229 sizeof(struct nfs4_stateid
), 0, 0, NULL
);
2230 if (stateid_slab
== NULL
)
2232 deleg_slab
= kmem_cache_create("nfsd4_delegations",
2233 sizeof(struct nfs4_delegation
), 0, 0, NULL
);
2234 if (deleg_slab
== NULL
)
2239 dprintk("nfsd4: out of memory while initializing nfsv4\n");
2243 void nfs4_free_openowner(struct nfs4_openowner
*oo
)
2245 kfree(oo
->oo_owner
.so_owner
.data
);
2246 kmem_cache_free(openowner_slab
, oo
);
2249 void nfs4_free_lockowner(struct nfs4_lockowner
*lo
)
2251 kfree(lo
->lo_owner
.so_owner
.data
);
2252 kmem_cache_free(lockowner_slab
, lo
);
2255 static void init_nfs4_replay(struct nfs4_replay
*rp
)
2257 rp
->rp_status
= nfserr_serverfault
;
2259 rp
->rp_buf
= rp
->rp_ibuf
;
2262 static inline void *alloc_stateowner(struct kmem_cache
*slab
, struct xdr_netobj
*owner
, struct nfs4_client
*clp
)
2264 struct nfs4_stateowner
*sop
;
2266 sop
= kmem_cache_alloc(slab
, GFP_KERNEL
);
2270 sop
->so_owner
.data
= kmemdup(owner
->data
, owner
->len
, GFP_KERNEL
);
2271 if (!sop
->so_owner
.data
) {
2272 kmem_cache_free(slab
, sop
);
2275 sop
->so_owner
.len
= owner
->len
;
2277 INIT_LIST_HEAD(&sop
->so_stateids
);
2278 sop
->so_id
= current_ownerid
++;
2279 sop
->so_client
= clp
;
2280 init_nfs4_replay(&sop
->so_replay
);
2284 static void hash_openowner(struct nfs4_openowner
*oo
, struct nfs4_client
*clp
, unsigned int strhashval
)
2286 unsigned int idhashval
;
2288 idhashval
= open_ownerid_hashval(oo
->oo_owner
.so_id
);
2289 list_add(&oo
->oo_owner
.so_idhash
, &open_ownerid_hashtbl
[idhashval
]);
2290 list_add(&oo
->oo_owner
.so_strhash
, &open_ownerstr_hashtbl
[strhashval
]);
2291 list_add(&oo
->oo_perclient
, &clp
->cl_openowners
);
2294 static struct nfs4_openowner
*
2295 alloc_init_open_stateowner(unsigned int strhashval
, struct nfs4_client
*clp
, struct nfsd4_open
*open
) {
2296 struct nfs4_openowner
*oo
;
2298 oo
= alloc_stateowner(openowner_slab
, &open
->op_owner
, clp
);
2301 oo
->oo_owner
.so_is_open_owner
= 1;
2302 oo
->oo_owner
.so_seqid
= open
->op_seqid
;
2303 oo
->oo_confirmed
= 0;
2305 INIT_LIST_HEAD(&oo
->oo_close_lru
);
2306 hash_openowner(oo
, clp
, strhashval
);
2311 init_stateid(struct nfs4_stateid
*stp
, struct nfs4_file
*fp
, struct nfsd4_open
*open
) {
2312 struct nfs4_openowner
*oo
= open
->op_openowner
;
2313 unsigned int hashval
= stateid_hashval(oo
->oo_owner
.so_id
, fp
->fi_id
);
2315 INIT_LIST_HEAD(&stp
->st_hash
);
2316 INIT_LIST_HEAD(&stp
->st_perstateowner
);
2317 INIT_LIST_HEAD(&stp
->st_lockowners
);
2318 INIT_LIST_HEAD(&stp
->st_perfile
);
2319 list_add(&stp
->st_hash
, &stateid_hashtbl
[hashval
]);
2320 list_add(&stp
->st_perstateowner
, &oo
->oo_owner
.so_stateids
);
2321 list_add(&stp
->st_perfile
, &fp
->fi_stateids
);
2322 stp
->st_type
= NFS4_OPEN_STID
;
2323 stp
->st_stateowner
= &oo
->oo_owner
;
2326 stp
->st_stateid
.si_boot
= boot_time
;
2327 stp
->st_stateid
.si_stateownerid
= oo
->oo_owner
.so_id
;
2328 stp
->st_stateid
.si_fileid
= fp
->fi_id
;
2329 /* note will be incremented before first return to client: */
2330 stp
->st_stateid
.si_generation
= 0;
2331 stp
->st_access_bmap
= 0;
2332 stp
->st_deny_bmap
= 0;
2333 __set_bit(open
->op_share_access
& ~NFS4_SHARE_WANT_MASK
,
2334 &stp
->st_access_bmap
);
2335 __set_bit(open
->op_share_deny
, &stp
->st_deny_bmap
);
2336 stp
->st_openstp
= NULL
;
2340 move_to_close_lru(struct nfs4_openowner
*oo
)
2342 dprintk("NFSD: move_to_close_lru nfs4_openowner %p\n", oo
);
2344 list_move_tail(&oo
->oo_close_lru
, &close_lru
);
2345 oo
->oo_time
= get_seconds();
2349 same_owner_str(struct nfs4_stateowner
*sop
, struct xdr_netobj
*owner
,
2352 return (sop
->so_owner
.len
== owner
->len
) &&
2353 0 == memcmp(sop
->so_owner
.data
, owner
->data
, owner
->len
) &&
2354 (sop
->so_client
->cl_clientid
.cl_id
== clid
->cl_id
);
2357 static struct nfs4_openowner
*
2358 find_openstateowner_str(unsigned int hashval
, struct nfsd4_open
*open
)
2360 struct nfs4_stateowner
*so
= NULL
;
2362 list_for_each_entry(so
, &open_ownerstr_hashtbl
[hashval
], so_strhash
) {
2363 if (same_owner_str(so
, &open
->op_owner
, &open
->op_clientid
))
2364 return container_of(so
, struct nfs4_openowner
, oo_owner
);
2369 /* search file_hashtbl[] for file */
2370 static struct nfs4_file
*
2371 find_file(struct inode
*ino
)
2373 unsigned int hashval
= file_hashval(ino
);
2374 struct nfs4_file
*fp
;
2376 spin_lock(&recall_lock
);
2377 list_for_each_entry(fp
, &file_hashtbl
[hashval
], fi_hash
) {
2378 if (fp
->fi_inode
== ino
) {
2380 spin_unlock(&recall_lock
);
2384 spin_unlock(&recall_lock
);
2388 static inline int access_valid(u32 x
, u32 minorversion
)
2390 if ((x
& NFS4_SHARE_ACCESS_MASK
) < NFS4_SHARE_ACCESS_READ
)
2392 if ((x
& NFS4_SHARE_ACCESS_MASK
) > NFS4_SHARE_ACCESS_BOTH
)
2394 x
&= ~NFS4_SHARE_ACCESS_MASK
;
2395 if (minorversion
&& x
) {
2396 if ((x
& NFS4_SHARE_WANT_MASK
) > NFS4_SHARE_WANT_CANCEL
)
2398 if ((x
& NFS4_SHARE_WHEN_MASK
) > NFS4_SHARE_PUSH_DELEG_WHEN_UNCONTENDED
)
2400 x
&= ~(NFS4_SHARE_WANT_MASK
| NFS4_SHARE_WHEN_MASK
);
2407 static inline int deny_valid(u32 x
)
2409 /* Note: unlike access bits, deny bits may be zero. */
2410 return x
<= NFS4_SHARE_DENY_BOTH
;
2414 * Called to check deny when READ with all zero stateid or
2415 * WRITE with all zero or all one stateid
2418 nfs4_share_conflict(struct svc_fh
*current_fh
, unsigned int deny_type
)
2420 struct inode
*ino
= current_fh
->fh_dentry
->d_inode
;
2421 struct nfs4_file
*fp
;
2422 struct nfs4_stateid
*stp
;
2425 dprintk("NFSD: nfs4_share_conflict\n");
2427 fp
= find_file(ino
);
2430 ret
= nfserr_locked
;
2431 /* Search for conflicting share reservations */
2432 list_for_each_entry(stp
, &fp
->fi_stateids
, st_perfile
) {
2433 if (test_bit(deny_type
, &stp
->st_deny_bmap
) ||
2434 test_bit(NFS4_SHARE_DENY_BOTH
, &stp
->st_deny_bmap
))
2443 static void nfsd_break_one_deleg(struct nfs4_delegation
*dp
)
2445 /* We're assuming the state code never drops its reference
2446 * without first removing the lease. Since we're in this lease
2447 * callback (and since the lease code is serialized by the kernel
2448 * lock) we know the server hasn't removed the lease yet, we know
2449 * it's safe to take a reference: */
2450 atomic_inc(&dp
->dl_count
);
2452 list_add_tail(&dp
->dl_recall_lru
, &del_recall_lru
);
2454 /* only place dl_time is set. protected by lock_flocks*/
2455 dp
->dl_time
= get_seconds();
2457 nfsd4_cb_recall(dp
);
2460 /* Called from break_lease() with lock_flocks() held. */
2461 static void nfsd_break_deleg_cb(struct file_lock
*fl
)
2463 struct nfs4_file
*fp
= (struct nfs4_file
*)fl
->fl_owner
;
2464 struct nfs4_delegation
*dp
;
2467 /* We assume break_lease is only called once per lease: */
2468 BUG_ON(fp
->fi_had_conflict
);
2470 * We don't want the locks code to timeout the lease for us;
2471 * we'll remove it ourself if a delegation isn't returned
2474 fl
->fl_break_time
= 0;
2476 spin_lock(&recall_lock
);
2477 fp
->fi_had_conflict
= true;
2478 list_for_each_entry(dp
, &fp
->fi_delegations
, dl_perfile
)
2479 nfsd_break_one_deleg(dp
);
2480 spin_unlock(&recall_lock
);
2484 int nfsd_change_deleg_cb(struct file_lock
**onlist
, int arg
)
2487 return lease_modify(onlist
, arg
);
2492 static const struct lock_manager_operations nfsd_lease_mng_ops
= {
2493 .lm_break
= nfsd_break_deleg_cb
,
2494 .lm_change
= nfsd_change_deleg_cb
,
2497 static __be32
nfsd4_check_seqid(struct nfsd4_compound_state
*cstate
, struct nfs4_stateowner
*so
, u32 seqid
)
2499 if (nfsd4_has_session(cstate
))
2501 if (seqid
== so
->so_seqid
- 1)
2502 return nfserr_replay_me
;
2503 if (seqid
== so
->so_seqid
)
2505 return nfserr_bad_seqid
;
2509 nfsd4_process_open1(struct nfsd4_compound_state
*cstate
,
2510 struct nfsd4_open
*open
)
2512 clientid_t
*clientid
= &open
->op_clientid
;
2513 struct nfs4_client
*clp
= NULL
;
2514 unsigned int strhashval
;
2515 struct nfs4_openowner
*oo
= NULL
;
2518 if (!check_name(open
->op_owner
))
2519 return nfserr_inval
;
2521 if (STALE_CLIENTID(&open
->op_clientid
))
2522 return nfserr_stale_clientid
;
2524 strhashval
= open_ownerstr_hashval(clientid
->cl_id
, &open
->op_owner
);
2525 oo
= find_openstateowner_str(strhashval
, open
);
2526 open
->op_openowner
= oo
;
2528 /* Make sure the client's lease hasn't expired. */
2529 clp
= find_confirmed_client(clientid
);
2531 return nfserr_expired
;
2534 if (!oo
->oo_confirmed
) {
2535 /* Replace unconfirmed owners without checking for replay. */
2536 clp
= oo
->oo_owner
.so_client
;
2537 release_openowner(oo
);
2538 open
->op_openowner
= NULL
;
2541 status
= nfsd4_check_seqid(cstate
, &oo
->oo_owner
, open
->op_seqid
);
2545 if (open
->op_openowner
== NULL
) {
2546 oo
= alloc_init_open_stateowner(strhashval
, clp
, open
);
2548 return nfserr_jukebox
;
2549 open
->op_openowner
= oo
;
2551 list_del_init(&oo
->oo_close_lru
);
2552 renew_client(oo
->oo_owner
.so_client
);
2556 static inline __be32
2557 nfs4_check_delegmode(struct nfs4_delegation
*dp
, int flags
)
2559 if ((flags
& WR_STATE
) && (dp
->dl_type
== NFS4_OPEN_DELEGATE_READ
))
2560 return nfserr_openmode
;
2565 static struct nfs4_delegation
*
2566 find_delegation_file(struct nfs4_file
*fp
, stateid_t
*stid
)
2568 struct nfs4_delegation
*dp
;
2570 spin_lock(&recall_lock
);
2571 list_for_each_entry(dp
, &fp
->fi_delegations
, dl_perfile
)
2572 if (dp
->dl_stateid
.si_stateownerid
== stid
->si_stateownerid
) {
2573 spin_unlock(&recall_lock
);
2576 spin_unlock(&recall_lock
);
2580 static int share_access_to_flags(u32 share_access
)
2582 share_access
&= ~NFS4_SHARE_WANT_MASK
;
2584 return share_access
== NFS4_SHARE_ACCESS_READ
? RD_STATE
: WR_STATE
;
2588 nfs4_check_deleg(struct nfs4_file
*fp
, struct nfsd4_open
*open
,
2589 struct nfs4_delegation
**dp
)
2592 __be32 status
= nfserr_bad_stateid
;
2594 *dp
= find_delegation_file(fp
, &open
->op_delegate_stateid
);
2597 flags
= share_access_to_flags(open
->op_share_access
);
2598 status
= nfs4_check_delegmode(*dp
, flags
);
2602 if (open
->op_claim_type
!= NFS4_OPEN_CLAIM_DELEGATE_CUR
)
2606 open
->op_openowner
->oo_confirmed
= 1;
2611 nfs4_check_open(struct nfs4_file
*fp
, struct nfsd4_open
*open
, struct nfs4_stateid
**stpp
)
2613 struct nfs4_stateid
*local
;
2614 struct nfs4_openowner
*oo
= open
->op_openowner
;
2616 list_for_each_entry(local
, &fp
->fi_stateids
, st_perfile
) {
2617 /* ignore lock owners */
2618 if (local
->st_stateowner
->so_is_open_owner
== 0)
2620 /* remember if we have seen this open owner */
2621 if (local
->st_stateowner
== &oo
->oo_owner
)
2623 /* check for conflicting share reservations */
2624 if (!test_share(local
, open
))
2625 return nfserr_share_denied
;
2630 static inline struct nfs4_stateid
*
2631 nfs4_alloc_stateid(void)
2633 return kmem_cache_alloc(stateid_slab
, GFP_KERNEL
);
2636 static inline int nfs4_access_to_access(u32 nfs4_access
)
2640 if (nfs4_access
& NFS4_SHARE_ACCESS_READ
)
2641 flags
|= NFSD_MAY_READ
;
2642 if (nfs4_access
& NFS4_SHARE_ACCESS_WRITE
)
2643 flags
|= NFSD_MAY_WRITE
;
2647 static __be32
nfs4_get_vfs_file(struct svc_rqst
*rqstp
, struct nfs4_file
*fp
,
2648 struct svc_fh
*cur_fh
, struct nfsd4_open
*open
)
2651 int oflag
= nfs4_access_to_omode(open
->op_share_access
);
2652 int access
= nfs4_access_to_access(open
->op_share_access
);
2654 /* CLAIM_DELEGATE_CUR is used in response to a broken lease;
2655 * allowing it to break the lease and return EAGAIN leaves the
2656 * client unable to make progress in returning the delegation */
2657 if (open
->op_claim_type
== NFS4_OPEN_CLAIM_DELEGATE_CUR
)
2658 access
|= NFSD_MAY_NOT_BREAK_LEASE
;
2660 if (!fp
->fi_fds
[oflag
]) {
2661 status
= nfsd_open(rqstp
, cur_fh
, S_IFREG
, access
,
2662 &fp
->fi_fds
[oflag
]);
2666 nfs4_file_get_access(fp
, oflag
);
2672 nfs4_new_open(struct svc_rqst
*rqstp
, struct nfs4_stateid
**stpp
,
2673 struct nfs4_file
*fp
, struct svc_fh
*cur_fh
,
2674 struct nfsd4_open
*open
)
2676 struct nfs4_stateid
*stp
;
2679 stp
= nfs4_alloc_stateid();
2681 return nfserr_jukebox
;
2683 status
= nfs4_get_vfs_file(rqstp
, fp
, cur_fh
, open
);
2685 kmem_cache_free(stateid_slab
, stp
);
2692 static inline __be32
2693 nfsd4_truncate(struct svc_rqst
*rqstp
, struct svc_fh
*fh
,
2694 struct nfsd4_open
*open
)
2696 struct iattr iattr
= {
2697 .ia_valid
= ATTR_SIZE
,
2700 if (!open
->op_truncate
)
2702 if (!(open
->op_share_access
& NFS4_SHARE_ACCESS_WRITE
))
2703 return nfserr_inval
;
2704 return nfsd_setattr(rqstp
, fh
, &iattr
, 0, (time_t)0);
2708 nfs4_upgrade_open(struct svc_rqst
*rqstp
, struct nfs4_file
*fp
, struct svc_fh
*cur_fh
, struct nfs4_stateid
*stp
, struct nfsd4_open
*open
)
2710 u32 op_share_access
= open
->op_share_access
& ~NFS4_SHARE_WANT_MASK
;
2714 new_access
= !test_bit(op_share_access
, &stp
->st_access_bmap
);
2716 status
= nfs4_get_vfs_file(rqstp
, fp
, cur_fh
, open
);
2720 status
= nfsd4_truncate(rqstp
, cur_fh
, open
);
2723 int oflag
= nfs4_access_to_omode(op_share_access
);
2724 nfs4_file_put_access(fp
, oflag
);
2728 /* remember the open */
2729 __set_bit(op_share_access
, &stp
->st_access_bmap
);
2730 __set_bit(open
->op_share_deny
, &stp
->st_deny_bmap
);
2737 nfs4_set_claim_prev(struct nfsd4_open
*open
)
2739 open
->op_openowner
->oo_confirmed
= 1;
2740 open
->op_openowner
->oo_owner
.so_client
->cl_firststate
= 1;
2743 /* Should we give out recallable state?: */
2744 static bool nfsd4_cb_channel_good(struct nfs4_client
*clp
)
2746 if (clp
->cl_cb_state
== NFSD4_CB_UP
)
2749 * In the sessions case, since we don't have to establish a
2750 * separate connection for callbacks, we assume it's OK
2751 * until we hear otherwise:
2753 return clp
->cl_minorversion
&& clp
->cl_cb_state
== NFSD4_CB_UNKNOWN
;
2756 static struct file_lock
*nfs4_alloc_init_lease(struct nfs4_delegation
*dp
, int flag
)
2758 struct file_lock
*fl
;
2760 fl
= locks_alloc_lock();
2763 locks_init_lock(fl
);
2764 fl
->fl_lmops
= &nfsd_lease_mng_ops
;
2765 fl
->fl_flags
= FL_LEASE
;
2766 fl
->fl_type
= flag
== NFS4_OPEN_DELEGATE_READ
? F_RDLCK
: F_WRLCK
;
2767 fl
->fl_end
= OFFSET_MAX
;
2768 fl
->fl_owner
= (fl_owner_t
)(dp
->dl_file
);
2769 fl
->fl_pid
= current
->tgid
;
2773 static int nfs4_setlease(struct nfs4_delegation
*dp
, int flag
)
2775 struct nfs4_file
*fp
= dp
->dl_file
;
2776 struct file_lock
*fl
;
2779 fl
= nfs4_alloc_init_lease(dp
, flag
);
2782 fl
->fl_file
= find_readable_file(fp
);
2783 list_add(&dp
->dl_perclnt
, &dp
->dl_client
->cl_delegations
);
2784 status
= vfs_setlease(fl
->fl_file
, fl
->fl_type
, &fl
);
2786 list_del_init(&dp
->dl_perclnt
);
2787 locks_free_lock(fl
);
2791 fp
->fi_deleg_file
= fl
->fl_file
;
2792 get_file(fp
->fi_deleg_file
);
2793 atomic_set(&fp
->fi_delegees
, 1);
2794 list_add(&dp
->dl_perfile
, &fp
->fi_delegations
);
2798 static int nfs4_set_delegation(struct nfs4_delegation
*dp
, int flag
)
2800 struct nfs4_file
*fp
= dp
->dl_file
;
2803 return nfs4_setlease(dp
, flag
);
2804 spin_lock(&recall_lock
);
2805 if (fp
->fi_had_conflict
) {
2806 spin_unlock(&recall_lock
);
2809 atomic_inc(&fp
->fi_delegees
);
2810 list_add(&dp
->dl_perfile
, &fp
->fi_delegations
);
2811 spin_unlock(&recall_lock
);
2812 list_add(&dp
->dl_perclnt
, &dp
->dl_client
->cl_delegations
);
2817 * Attempt to hand out a delegation.
2820 nfs4_open_delegation(struct svc_fh
*fh
, struct nfsd4_open
*open
, struct nfs4_stateid
*stp
)
2822 struct nfs4_delegation
*dp
;
2823 struct nfs4_openowner
*oo
= container_of(stp
->st_stateowner
, struct nfs4_openowner
, oo_owner
);
2825 int status
, flag
= 0;
2827 cb_up
= nfsd4_cb_channel_good(oo
->oo_owner
.so_client
);
2828 flag
= NFS4_OPEN_DELEGATE_NONE
;
2829 open
->op_recall
= 0;
2830 switch (open
->op_claim_type
) {
2831 case NFS4_OPEN_CLAIM_PREVIOUS
:
2833 open
->op_recall
= 1;
2834 flag
= open
->op_delegate_type
;
2835 if (flag
== NFS4_OPEN_DELEGATE_NONE
)
2838 case NFS4_OPEN_CLAIM_NULL
:
2839 /* Let's not give out any delegations till everyone's
2840 * had the chance to reclaim theirs.... */
2841 if (locks_in_grace())
2843 if (!cb_up
|| !oo
->oo_confirmed
)
2845 if (open
->op_share_access
& NFS4_SHARE_ACCESS_WRITE
)
2846 flag
= NFS4_OPEN_DELEGATE_WRITE
;
2848 flag
= NFS4_OPEN_DELEGATE_READ
;
2854 dp
= alloc_init_deleg(oo
->oo_owner
.so_client
, stp
, fh
, flag
);
2857 status
= nfs4_set_delegation(dp
, flag
);
2861 memcpy(&open
->op_delegate_stateid
, &dp
->dl_stateid
, sizeof(dp
->dl_stateid
));
2863 dprintk("NFSD: delegation stateid=" STATEID_FMT
"\n",
2864 STATEID_VAL(&dp
->dl_stateid
));
2866 if (open
->op_claim_type
== NFS4_OPEN_CLAIM_PREVIOUS
2867 && flag
== NFS4_OPEN_DELEGATE_NONE
2868 && open
->op_delegate_type
!= NFS4_OPEN_DELEGATE_NONE
)
2869 dprintk("NFSD: WARNING: refusing delegation reclaim\n");
2870 open
->op_delegate_type
= flag
;
2873 nfs4_put_delegation(dp
);
2875 flag
= NFS4_OPEN_DELEGATE_NONE
;
2880 * called with nfs4_lock_state() held.
2883 nfsd4_process_open2(struct svc_rqst
*rqstp
, struct svc_fh
*current_fh
, struct nfsd4_open
*open
)
2885 struct nfsd4_compoundres
*resp
= rqstp
->rq_resp
;
2886 struct nfs4_file
*fp
= NULL
;
2887 struct inode
*ino
= current_fh
->fh_dentry
->d_inode
;
2888 struct nfs4_stateid
*stp
= NULL
;
2889 struct nfs4_delegation
*dp
= NULL
;
2892 status
= nfserr_inval
;
2893 if (!access_valid(open
->op_share_access
, resp
->cstate
.minorversion
)
2894 || !deny_valid(open
->op_share_deny
))
2897 * Lookup file; if found, lookup stateid and check open request,
2898 * and check for delegations in the process of being recalled.
2899 * If not found, create the nfs4_file struct
2901 fp
= find_file(ino
);
2903 if ((status
= nfs4_check_open(fp
, open
, &stp
)))
2905 status
= nfs4_check_deleg(fp
, open
, &dp
);
2909 status
= nfserr_bad_stateid
;
2910 if (open
->op_claim_type
== NFS4_OPEN_CLAIM_DELEGATE_CUR
)
2912 status
= nfserr_jukebox
;
2913 fp
= alloc_init_file(ino
);
2919 * OPEN the file, or upgrade an existing OPEN.
2920 * If truncate fails, the OPEN fails.
2923 /* Stateid was found, this is an OPEN upgrade */
2924 status
= nfs4_upgrade_open(rqstp
, fp
, current_fh
, stp
, open
);
2928 status
= nfs4_new_open(rqstp
, &stp
, fp
, current_fh
, open
);
2931 init_stateid(stp
, fp
, open
);
2932 status
= nfsd4_truncate(rqstp
, current_fh
, open
);
2934 release_open_stateid(stp
);
2938 update_stateid(&stp
->st_stateid
);
2939 memcpy(&open
->op_stateid
, &stp
->st_stateid
, sizeof(stateid_t
));
2941 if (nfsd4_has_session(&resp
->cstate
))
2942 open
->op_openowner
->oo_confirmed
= 1;
2945 * Attempt to hand out a delegation. No error return, because the
2946 * OPEN succeeds even if we fail.
2948 nfs4_open_delegation(current_fh
, open
, stp
);
2952 dprintk("%s: stateid=" STATEID_FMT
"\n", __func__
,
2953 STATEID_VAL(&stp
->st_stateid
));
2957 if (status
== 0 && open
->op_claim_type
== NFS4_OPEN_CLAIM_PREVIOUS
)
2958 nfs4_set_claim_prev(open
);
2960 * To finish the open response, we just need to set the rflags.
2962 open
->op_rflags
= NFS4_OPEN_RESULT_LOCKTYPE_POSIX
;
2963 if (!open
->op_openowner
->oo_confirmed
&&
2964 !nfsd4_has_session(&resp
->cstate
))
2965 open
->op_rflags
|= NFS4_OPEN_RESULT_CONFIRM
;
2971 nfsd4_renew(struct svc_rqst
*rqstp
, struct nfsd4_compound_state
*cstate
,
2974 struct nfs4_client
*clp
;
2978 dprintk("process_renew(%08x/%08x): starting\n",
2979 clid
->cl_boot
, clid
->cl_id
);
2980 status
= nfserr_stale_clientid
;
2981 if (STALE_CLIENTID(clid
))
2983 clp
= find_confirmed_client(clid
);
2984 status
= nfserr_expired
;
2986 /* We assume the client took too long to RENEW. */
2987 dprintk("nfsd4_renew: clientid not found!\n");
2991 status
= nfserr_cb_path_down
;
2992 if (!list_empty(&clp
->cl_delegations
)
2993 && clp
->cl_cb_state
!= NFSD4_CB_UP
)
2997 nfs4_unlock_state();
3001 static struct lock_manager nfsd4_manager
= {
3005 nfsd4_end_grace(void)
3007 dprintk("NFSD: end of grace period\n");
3008 nfsd4_recdir_purge_old();
3009 locks_end_grace(&nfsd4_manager
);
3011 * Now that every NFSv4 client has had the chance to recover and
3012 * to see the (possibly new, possibly shorter) lease time, we
3013 * can safely set the next grace time to the current lease time:
3015 nfsd4_grace
= nfsd4_lease
;
3019 nfs4_laundromat(void)
3021 struct nfs4_client
*clp
;
3022 struct nfs4_openowner
*oo
;
3023 struct nfs4_delegation
*dp
;
3024 struct list_head
*pos
, *next
, reaplist
;
3025 time_t cutoff
= get_seconds() - nfsd4_lease
;
3026 time_t t
, clientid_val
= nfsd4_lease
;
3027 time_t u
, test_val
= nfsd4_lease
;
3031 dprintk("NFSD: laundromat service - starting\n");
3032 if (locks_in_grace())
3034 INIT_LIST_HEAD(&reaplist
);
3035 spin_lock(&client_lock
);
3036 list_for_each_safe(pos
, next
, &client_lru
) {
3037 clp
= list_entry(pos
, struct nfs4_client
, cl_lru
);
3038 if (time_after((unsigned long)clp
->cl_time
, (unsigned long)cutoff
)) {
3039 t
= clp
->cl_time
- cutoff
;
3040 if (clientid_val
> t
)
3044 if (atomic_read(&clp
->cl_refcount
)) {
3045 dprintk("NFSD: client in use (clientid %08x)\n",
3046 clp
->cl_clientid
.cl_id
);
3049 unhash_client_locked(clp
);
3050 list_add(&clp
->cl_lru
, &reaplist
);
3052 spin_unlock(&client_lock
);
3053 list_for_each_safe(pos
, next
, &reaplist
) {
3054 clp
= list_entry(pos
, struct nfs4_client
, cl_lru
);
3055 dprintk("NFSD: purging unused client (clientid %08x)\n",
3056 clp
->cl_clientid
.cl_id
);
3057 nfsd4_remove_clid_dir(clp
);
3060 spin_lock(&recall_lock
);
3061 list_for_each_safe(pos
, next
, &del_recall_lru
) {
3062 dp
= list_entry (pos
, struct nfs4_delegation
, dl_recall_lru
);
3063 if (time_after((unsigned long)dp
->dl_time
, (unsigned long)cutoff
)) {
3064 u
= dp
->dl_time
- cutoff
;
3069 list_move(&dp
->dl_recall_lru
, &reaplist
);
3071 spin_unlock(&recall_lock
);
3072 list_for_each_safe(pos
, next
, &reaplist
) {
3073 dp
= list_entry (pos
, struct nfs4_delegation
, dl_recall_lru
);
3074 list_del_init(&dp
->dl_recall_lru
);
3075 unhash_delegation(dp
);
3077 test_val
= nfsd4_lease
;
3078 list_for_each_safe(pos
, next
, &close_lru
) {
3079 oo
= container_of(pos
, struct nfs4_openowner
, oo_close_lru
);
3080 if (time_after((unsigned long)oo
->oo_time
, (unsigned long)cutoff
)) {
3081 u
= oo
->oo_time
- cutoff
;
3086 dprintk("NFSD: purging unused open stateowner (so_id %d)\n",
3087 oo
->oo_owner
.so_id
);
3088 release_openowner(oo
);
3090 if (clientid_val
< NFSD_LAUNDROMAT_MINTIMEOUT
)
3091 clientid_val
= NFSD_LAUNDROMAT_MINTIMEOUT
;
3092 nfs4_unlock_state();
3093 return clientid_val
;
3096 static struct workqueue_struct
*laundry_wq
;
3097 static void laundromat_main(struct work_struct
*);
3098 static DECLARE_DELAYED_WORK(laundromat_work
, laundromat_main
);
3101 laundromat_main(struct work_struct
*not_used
)
3105 t
= nfs4_laundromat();
3106 dprintk("NFSD: laundromat_main - sleeping for %ld seconds\n", t
);
3107 queue_delayed_work(laundry_wq
, &laundromat_work
, t
*HZ
);
3110 static struct nfs4_openowner
* search_close_lru(u32 st_id
)
3112 struct nfs4_openowner
*local
;
3114 list_for_each_entry(local
, &close_lru
, oo_close_lru
) {
3115 if (local
->oo_owner
.so_id
== st_id
)
3122 nfs4_check_fh(struct svc_fh
*fhp
, struct nfs4_stateid
*stp
)
3124 return fhp
->fh_dentry
->d_inode
!= stp
->st_file
->fi_inode
;
3128 STALE_STATEID(stateid_t
*stateid
)
3130 if (stateid
->si_boot
== boot_time
)
3132 dprintk("NFSD: stale stateid " STATEID_FMT
"!\n",
3133 STATEID_VAL(stateid
));
3138 access_permit_read(unsigned long access_bmap
)
3140 return test_bit(NFS4_SHARE_ACCESS_READ
, &access_bmap
) ||
3141 test_bit(NFS4_SHARE_ACCESS_BOTH
, &access_bmap
) ||
3142 test_bit(NFS4_SHARE_ACCESS_WRITE
, &access_bmap
);
3146 access_permit_write(unsigned long access_bmap
)
3148 return test_bit(NFS4_SHARE_ACCESS_WRITE
, &access_bmap
) ||
3149 test_bit(NFS4_SHARE_ACCESS_BOTH
, &access_bmap
);
3153 __be32
nfs4_check_openmode(struct nfs4_stateid
*stp
, int flags
)
3155 __be32 status
= nfserr_openmode
;
3157 /* For lock stateid's, we test the parent open, not the lock: */
3158 if (stp
->st_openstp
)
3159 stp
= stp
->st_openstp
;
3160 if ((flags
& WR_STATE
) && (!access_permit_write(stp
->st_access_bmap
)))
3162 if ((flags
& RD_STATE
) && (!access_permit_read(stp
->st_access_bmap
)))
3169 static inline __be32
3170 check_special_stateids(svc_fh
*current_fh
, stateid_t
*stateid
, int flags
)
3172 if (ONE_STATEID(stateid
) && (flags
& RD_STATE
))
3174 else if (locks_in_grace()) {
3175 /* Answer in remaining cases depends on existence of
3176 * conflicting state; so we must wait out the grace period. */
3177 return nfserr_grace
;
3178 } else if (flags
& WR_STATE
)
3179 return nfs4_share_conflict(current_fh
,
3180 NFS4_SHARE_DENY_WRITE
);
3181 else /* (flags & RD_STATE) && ZERO_STATEID(stateid) */
3182 return nfs4_share_conflict(current_fh
,
3183 NFS4_SHARE_DENY_READ
);
3187 * Allow READ/WRITE during grace period on recovered state only for files
3188 * that are not able to provide mandatory locking.
3191 grace_disallows_io(struct inode
*inode
)
3193 return locks_in_grace() && mandatory_lock(inode
);
3196 /* Returns true iff a is later than b: */
3197 static bool stateid_generation_after(stateid_t
*a
, stateid_t
*b
)
3199 return (s32
)a
->si_generation
- (s32
)b
->si_generation
> 0;
3202 static int check_stateid_generation(stateid_t
*in
, stateid_t
*ref
, bool has_session
)
3205 * When sessions are used the stateid generation number is ignored
3208 if (has_session
&& in
->si_generation
== 0)
3211 if (in
->si_generation
== ref
->si_generation
)
3214 /* If the client sends us a stateid from the future, it's buggy: */
3215 if (stateid_generation_after(in
, ref
))
3216 return nfserr_bad_stateid
;
3218 * However, we could see a stateid from the past, even from a
3219 * non-buggy client. For example, if the client sends a lock
3220 * while some IO is outstanding, the lock may bump si_generation
3221 * while the IO is still in flight. The client could avoid that
3222 * situation by waiting for responses on all the IO requests,
3223 * but better performance may result in retrying IO that
3224 * receives an old_stateid error if requests are rarely
3225 * reordered in flight:
3227 return nfserr_old_stateid
;
3230 static int is_delegation_stateid(stateid_t
*stateid
)
3232 return stateid
->si_fileid
== 0;
3235 __be32
nfs4_validate_stateid(stateid_t
*stateid
, bool has_session
)
3237 struct nfs4_stateid
*stp
= NULL
;
3238 __be32 status
= nfserr_stale_stateid
;
3240 if (STALE_STATEID(stateid
))
3243 status
= nfserr_expired
;
3244 stp
= find_stateid(stateid
, 0);
3247 status
= nfserr_bad_stateid
;
3249 if (stp
->st_stateowner
->so_is_open_owner
3250 && !openowner(stp
->st_stateowner
)->oo_confirmed
)
3253 status
= check_stateid_generation(stateid
, &stp
->st_stateid
, has_session
);
3263 * Checks for stateid operations
3266 nfs4_preprocess_stateid_op(struct nfsd4_compound_state
*cstate
,
3267 stateid_t
*stateid
, int flags
, struct file
**filpp
)
3269 struct nfs4_stateid
*stp
= NULL
;
3270 struct nfs4_delegation
*dp
= NULL
;
3271 struct svc_fh
*current_fh
= &cstate
->current_fh
;
3272 struct inode
*ino
= current_fh
->fh_dentry
->d_inode
;
3278 if (grace_disallows_io(ino
))
3279 return nfserr_grace
;
3281 if (ZERO_STATEID(stateid
) || ONE_STATEID(stateid
))
3282 return check_special_stateids(current_fh
, stateid
, flags
);
3284 status
= nfserr_stale_stateid
;
3285 if (STALE_STATEID(stateid
))
3289 * We assume that any stateid that has the current boot time,
3290 * but that we can't find, is expired:
3292 status
= nfserr_expired
;
3293 if (is_delegation_stateid(stateid
)) {
3294 dp
= find_delegation_stateid(ino
, stateid
);
3297 status
= check_stateid_generation(stateid
, &dp
->dl_stateid
, nfsd4_has_session(cstate
));
3300 status
= nfs4_check_delegmode(dp
, flags
);
3303 renew_client(dp
->dl_client
);
3305 *filpp
= dp
->dl_file
->fi_deleg_file
;
3308 } else { /* open or lock stateid */
3309 stp
= find_stateid(stateid
, flags
);
3312 status
= nfserr_bad_stateid
;
3313 if (nfs4_check_fh(current_fh
, stp
))
3315 if (stp
->st_stateowner
->so_is_open_owner
3316 && !openowner(stp
->st_stateowner
)->oo_confirmed
)
3318 status
= check_stateid_generation(stateid
, &stp
->st_stateid
,
3319 nfsd4_has_session(cstate
));
3322 status
= nfs4_check_openmode(stp
, flags
);
3325 renew_client(stp
->st_stateowner
->so_client
);
3327 if (flags
& RD_STATE
)
3328 *filpp
= find_readable_file(stp
->st_file
);
3330 *filpp
= find_writeable_file(stp
->st_file
);
3339 nfsd4_free_delegation_stateid(stateid_t
*stateid
)
3341 struct nfs4_delegation
*dp
= search_for_delegation(stateid
);
3343 return nfserr_locks_held
;
3344 return nfserr_bad_stateid
;
3348 nfsd4_free_lock_stateid(struct nfs4_stateid
*stp
)
3350 if (check_for_locks(stp
->st_file
, lockowner(stp
->st_stateowner
)))
3351 return nfserr_locks_held
;
3352 release_lock_stateid(stp
);
3357 * Test if the stateid is valid
3360 nfsd4_test_stateid(struct svc_rqst
*rqstp
, struct nfsd4_compound_state
*cstate
,
3361 struct nfsd4_test_stateid
*test_stateid
)
3363 test_stateid
->ts_has_session
= nfsd4_has_session(cstate
);
3371 nfsd4_free_stateid(struct svc_rqst
*rqstp
, struct nfsd4_compound_state
*cstate
,
3372 struct nfsd4_free_stateid
*free_stateid
)
3374 stateid_t
*stateid
= &free_stateid
->fr_stateid
;
3375 struct nfs4_stateid
*stp
;
3379 if (is_delegation_stateid(stateid
)) {
3380 ret
= nfsd4_free_delegation_stateid(stateid
);
3384 stp
= find_stateid(stateid
, 0);
3386 ret
= nfserr_bad_stateid
;
3389 ret
= check_stateid_generation(stateid
, &stp
->st_stateid
, 1);
3393 if (stp
->st_type
== NFS4_OPEN_STID
) {
3394 ret
= nfserr_locks_held
;
3397 ret
= nfsd4_free_lock_stateid(stp
);
3402 nfs4_unlock_state();
3409 return (type
== NFS4_READW_LT
|| type
== NFS4_READ_LT
) ?
3410 RD_STATE
: WR_STATE
;
3414 * Checks for sequence id mutating operations.
3417 nfs4_preprocess_seqid_op(struct nfsd4_compound_state
*cstate
, u32 seqid
,
3418 stateid_t
*stateid
, int flags
,
3419 struct nfs4_stateid
**stpp
)
3421 struct nfs4_stateowner
*sop
;
3422 struct svc_fh
*current_fh
= &cstate
->current_fh
;
3425 dprintk("NFSD: %s: seqid=%d stateid = " STATEID_FMT
"\n", __func__
,
3426 seqid
, STATEID_VAL(stateid
));
3430 if (ZERO_STATEID(stateid
) || ONE_STATEID(stateid
)) {
3431 dprintk("NFSD: preprocess_seqid_op: magic stateid!\n");
3432 return nfserr_bad_stateid
;
3435 if (STALE_STATEID(stateid
))
3436 return nfserr_stale_stateid
;
3439 * We return BAD_STATEID if filehandle doesn't match stateid,
3440 * the confirmed flag is incorrecly set, or the generation
3441 * number is incorrect.
3443 *stpp
= find_stateid(stateid
, flags
);
3445 return nfserr_expired
;
3447 sop
= (*stpp
)->st_stateowner
;
3448 cstate
->replay_owner
= sop
;
3450 if (nfs4_check_fh(current_fh
, *stpp
)) {
3451 dprintk("NFSD: preprocess_seqid_op: fh-stateid mismatch!\n");
3452 return nfserr_bad_stateid
;
3455 status
= nfsd4_check_seqid(cstate
, sop
, seqid
);
3459 if (sop
->so_is_open_owner
&& !openowner(sop
)->oo_confirmed
3460 && !(flags
& CONFIRM
)) {
3461 dprintk("NFSD: preprocess_seqid_op: stateowner not"
3462 " confirmed yet!\n");
3463 return nfserr_bad_stateid
;
3465 status
= check_stateid_generation(stateid
, &(*stpp
)->st_stateid
, nfsd4_has_session(cstate
));
3468 renew_client(sop
->so_client
);
3473 nfsd4_open_confirm(struct svc_rqst
*rqstp
, struct nfsd4_compound_state
*cstate
,
3474 struct nfsd4_open_confirm
*oc
)
3477 struct nfs4_openowner
*oo
;
3478 struct nfs4_stateid
*stp
;
3480 dprintk("NFSD: nfsd4_open_confirm on file %.*s\n",
3481 (int)cstate
->current_fh
.fh_dentry
->d_name
.len
,
3482 cstate
->current_fh
.fh_dentry
->d_name
.name
);
3484 status
= fh_verify(rqstp
, &cstate
->current_fh
, S_IFREG
, 0);
3490 status
= nfs4_preprocess_seqid_op(cstate
,
3491 oc
->oc_seqid
, &oc
->oc_req_stateid
,
3492 CONFIRM
| OPEN_STATE
, &stp
);
3495 oo
= openowner(stp
->st_stateowner
);
3496 status
= nfserr_bad_stateid
;
3497 if (oo
->oo_confirmed
)
3499 oo
->oo_confirmed
= 1;
3500 update_stateid(&stp
->st_stateid
);
3501 memcpy(&oc
->oc_resp_stateid
, &stp
->st_stateid
, sizeof(stateid_t
));
3502 dprintk("NFSD: %s: success, seqid=%d stateid=" STATEID_FMT
"\n",
3503 __func__
, oc
->oc_seqid
, STATEID_VAL(&stp
->st_stateid
));
3505 nfsd4_create_clid_dir(oo
->oo_owner
.so_client
);
3508 if (!cstate
->replay_owner
)
3509 nfs4_unlock_state();
3513 static inline void nfs4_file_downgrade(struct nfs4_stateid
*stp
, unsigned int to_access
)
3517 for (i
= 1; i
< 4; i
++) {
3518 if (test_bit(i
, &stp
->st_access_bmap
) && !(i
& to_access
)) {
3519 nfs4_file_put_access(stp
->st_file
, i
);
3520 __clear_bit(i
, &stp
->st_access_bmap
);
3526 reset_union_bmap_deny(unsigned long deny
, unsigned long *bmap
)
3529 for (i
= 0; i
< 4; i
++) {
3530 if ((i
& deny
) != i
)
3531 __clear_bit(i
, bmap
);
3536 nfsd4_open_downgrade(struct svc_rqst
*rqstp
,
3537 struct nfsd4_compound_state
*cstate
,
3538 struct nfsd4_open_downgrade
*od
)
3541 struct nfs4_stateid
*stp
;
3543 dprintk("NFSD: nfsd4_open_downgrade on file %.*s\n",
3544 (int)cstate
->current_fh
.fh_dentry
->d_name
.len
,
3545 cstate
->current_fh
.fh_dentry
->d_name
.name
);
3547 if (!access_valid(od
->od_share_access
, cstate
->minorversion
)
3548 || !deny_valid(od
->od_share_deny
))
3549 return nfserr_inval
;
3552 status
= nfs4_preprocess_seqid_op(cstate
, od
->od_seqid
,
3553 &od
->od_stateid
, OPEN_STATE
, &stp
);
3556 status
= nfserr_inval
;
3557 if (!test_bit(od
->od_share_access
, &stp
->st_access_bmap
)) {
3558 dprintk("NFSD:access not a subset current bitmap: 0x%lx, input access=%08x\n",
3559 stp
->st_access_bmap
, od
->od_share_access
);
3562 if (!test_bit(od
->od_share_deny
, &stp
->st_deny_bmap
)) {
3563 dprintk("NFSD:deny not a subset current bitmap: 0x%lx, input deny=%08x\n",
3564 stp
->st_deny_bmap
, od
->od_share_deny
);
3567 nfs4_file_downgrade(stp
, od
->od_share_access
);
3569 reset_union_bmap_deny(od
->od_share_deny
, &stp
->st_deny_bmap
);
3571 update_stateid(&stp
->st_stateid
);
3572 memcpy(&od
->od_stateid
, &stp
->st_stateid
, sizeof(stateid_t
));
3575 if (!cstate
->replay_owner
)
3576 nfs4_unlock_state();
3581 * nfs4_unlock_state() called after encode
3584 nfsd4_close(struct svc_rqst
*rqstp
, struct nfsd4_compound_state
*cstate
,
3585 struct nfsd4_close
*close
)
3588 struct nfs4_openowner
*oo
;
3589 struct nfs4_stateid
*stp
;
3591 dprintk("NFSD: nfsd4_close on file %.*s\n",
3592 (int)cstate
->current_fh
.fh_dentry
->d_name
.len
,
3593 cstate
->current_fh
.fh_dentry
->d_name
.name
);
3596 /* check close_lru for replay */
3597 status
= nfs4_preprocess_seqid_op(cstate
, close
->cl_seqid
,
3600 if (stp
== NULL
&& status
== nfserr_expired
) {
3602 * Also, we should make sure this isn't just the result of
3605 oo
= search_close_lru(close
->cl_stateid
.si_stateownerid
);
3606 /* It's not stale; let's assume it's expired: */
3609 cstate
->replay_owner
= &oo
->oo_owner
;
3610 status
= nfsd4_check_seqid(cstate
, &oo
->oo_owner
, close
->cl_seqid
);
3613 status
= nfserr_bad_seqid
;
3617 oo
= openowner(stp
->st_stateowner
);
3619 update_stateid(&stp
->st_stateid
);
3620 memcpy(&close
->cl_stateid
, &stp
->st_stateid
, sizeof(stateid_t
));
3622 /* release_stateid() calls nfsd_close() if needed */
3623 release_open_stateid(stp
);
3625 /* place unused nfs4_stateowners on so_close_lru list to be
3626 * released by the laundromat service after the lease period
3627 * to enable us to handle CLOSE replay
3629 if (list_empty(&oo
->oo_owner
.so_stateids
))
3630 move_to_close_lru(oo
);
3632 if (!cstate
->replay_owner
)
3633 nfs4_unlock_state();
3638 nfsd4_delegreturn(struct svc_rqst
*rqstp
, struct nfsd4_compound_state
*cstate
,
3639 struct nfsd4_delegreturn
*dr
)
3641 struct nfs4_delegation
*dp
;
3642 stateid_t
*stateid
= &dr
->dr_stateid
;
3643 struct inode
*inode
;
3646 if ((status
= fh_verify(rqstp
, &cstate
->current_fh
, S_IFREG
, 0)))
3648 inode
= cstate
->current_fh
.fh_dentry
->d_inode
;
3651 status
= nfserr_bad_stateid
;
3652 if (ZERO_STATEID(stateid
) || ONE_STATEID(stateid
))
3654 status
= nfserr_stale_stateid
;
3655 if (STALE_STATEID(stateid
))
3657 status
= nfserr_bad_stateid
;
3658 if (!is_delegation_stateid(stateid
))
3660 status
= nfserr_expired
;
3661 dp
= find_delegation_stateid(inode
, stateid
);
3664 status
= check_stateid_generation(stateid
, &dp
->dl_stateid
, nfsd4_has_session(cstate
));
3667 renew_client(dp
->dl_client
);
3669 unhash_delegation(dp
);
3671 nfs4_unlock_state();
3678 * Lock owner state (byte-range locks)
3680 #define LOFF_OVERFLOW(start, len) ((u64)(len) > ~(u64)(start))
3681 #define LOCK_HASH_BITS 8
3682 #define LOCK_HASH_SIZE (1 << LOCK_HASH_BITS)
3683 #define LOCK_HASH_MASK (LOCK_HASH_SIZE - 1)
3686 end_offset(u64 start
, u64 len
)
3691 return end
>= start
? end
: NFS4_MAX_UINT64
;
3694 /* last octet in a range */
3696 last_byte_offset(u64 start
, u64 len
)
3702 return end
> start
? end
- 1: NFS4_MAX_UINT64
;
3705 static unsigned int lockownerid_hashval(u32 id
)
3707 return id
& LOCK_HASH_MASK
;
3710 static inline unsigned int
3711 lock_ownerstr_hashval(struct inode
*inode
, u32 cl_id
,
3712 struct xdr_netobj
*ownername
)
3714 return (file_hashval(inode
) + cl_id
3715 + opaque_hashval(ownername
->data
, ownername
->len
))
3719 static struct list_head lock_ownerid_hashtbl
[LOCK_HASH_SIZE
];
3720 static struct list_head lock_ownerstr_hashtbl
[LOCK_HASH_SIZE
];
3722 static struct nfs4_delegation
*
3723 search_for_delegation(stateid_t
*stid
)
3725 struct nfs4_file
*fp
;
3726 struct nfs4_delegation
*dp
;
3727 struct list_head
*pos
;
3730 for (i
= 0; i
< FILE_HASH_SIZE
; i
++) {
3731 list_for_each_entry(fp
, &file_hashtbl
[i
], fi_hash
) {
3732 list_for_each(pos
, &fp
->fi_delegations
) {
3733 dp
= list_entry(pos
, struct nfs4_delegation
, dl_perfile
);
3734 if (same_stateid(&dp
->dl_stateid
, stid
))
3742 static struct nfs4_delegation
*
3743 find_delegation_stateid(struct inode
*ino
, stateid_t
*stid
)
3745 struct nfs4_file
*fp
;
3746 struct nfs4_delegation
*dl
;
3748 dprintk("NFSD: %s: stateid=" STATEID_FMT
"\n", __func__
,
3751 fp
= find_file(ino
);
3754 dl
= find_delegation_file(fp
, stid
);
3760 * TODO: Linux file offsets are _signed_ 64-bit quantities, which means that
3761 * we can't properly handle lock requests that go beyond the (2^63 - 1)-th
3762 * byte, because of sign extension problems. Since NFSv4 calls for 64-bit
3763 * locking, this prevents us from being completely protocol-compliant. The
3764 * real solution to this problem is to start using unsigned file offsets in
3765 * the VFS, but this is a very deep change!
3768 nfs4_transform_lock_offset(struct file_lock
*lock
)
3770 if (lock
->fl_start
< 0)
3771 lock
->fl_start
= OFFSET_MAX
;
3772 if (lock
->fl_end
< 0)
3773 lock
->fl_end
= OFFSET_MAX
;
3776 /* Hack!: For now, we're defining this just so we can use a pointer to it
3777 * as a unique cookie to identify our (NFSv4's) posix locks. */
3778 static const struct lock_manager_operations nfsd_posix_mng_ops
= {
3782 nfs4_set_lock_denied(struct file_lock
*fl
, struct nfsd4_lock_denied
*deny
)
3784 struct nfs4_lockowner
*lo
;
3786 if (fl
->fl_lmops
== &nfsd_posix_mng_ops
) {
3787 lo
= (struct nfs4_lockowner
*) fl
->fl_owner
;
3788 deny
->ld_owner
.data
= kmemdup(lo
->lo_owner
.so_owner
.data
,
3789 lo
->lo_owner
.so_owner
.len
, GFP_KERNEL
);
3790 if (!deny
->ld_owner
.data
)
3791 /* We just don't care that much */
3793 deny
->ld_owner
.len
= lo
->lo_owner
.so_owner
.len
;
3794 deny
->ld_clientid
= lo
->lo_owner
.so_client
->cl_clientid
;
3797 deny
->ld_owner
.len
= 0;
3798 deny
->ld_owner
.data
= NULL
;
3799 deny
->ld_clientid
.cl_boot
= 0;
3800 deny
->ld_clientid
.cl_id
= 0;
3802 deny
->ld_start
= fl
->fl_start
;
3803 deny
->ld_length
= NFS4_MAX_UINT64
;
3804 if (fl
->fl_end
!= NFS4_MAX_UINT64
)
3805 deny
->ld_length
= fl
->fl_end
- fl
->fl_start
+ 1;
3806 deny
->ld_type
= NFS4_READ_LT
;
3807 if (fl
->fl_type
!= F_RDLCK
)
3808 deny
->ld_type
= NFS4_WRITE_LT
;
3811 static struct nfs4_lockowner
*
3812 find_lockowner_str(struct inode
*inode
, clientid_t
*clid
,
3813 struct xdr_netobj
*owner
)
3815 unsigned int hashval
= lock_ownerstr_hashval(inode
, clid
->cl_id
, owner
);
3816 struct nfs4_stateowner
*op
;
3818 list_for_each_entry(op
, &lock_ownerstr_hashtbl
[hashval
], so_strhash
) {
3819 if (same_owner_str(op
, owner
, clid
))
3820 return lockowner(op
);
3825 static void hash_lockowner(struct nfs4_lockowner
*lo
, unsigned int strhashval
, struct nfs4_client
*clp
, struct nfs4_stateid
*open_stp
)
3827 unsigned int idhashval
;
3829 idhashval
= lockownerid_hashval(lo
->lo_owner
.so_id
);
3830 list_add(&lo
->lo_owner
.so_idhash
, &lock_ownerid_hashtbl
[idhashval
]);
3831 list_add(&lo
->lo_owner
.so_strhash
, &lock_ownerstr_hashtbl
[strhashval
]);
3832 list_add(&lo
->lo_perstateid
, &open_stp
->st_lockowners
);
3836 * Alloc a lock owner structure.
3837 * Called in nfsd4_lock - therefore, OPEN and OPEN_CONFIRM (if needed) has
3840 * strhashval = lock_ownerstr_hashval
3843 static struct nfs4_lockowner
*
3844 alloc_init_lock_stateowner(unsigned int strhashval
, struct nfs4_client
*clp
, struct nfs4_stateid
*open_stp
, struct nfsd4_lock
*lock
) {
3845 struct nfs4_lockowner
*lo
;
3847 lo
= alloc_stateowner(lockowner_slab
, &lock
->lk_new_owner
, clp
);
3850 INIT_LIST_HEAD(&lo
->lo_owner
.so_stateids
);
3851 lo
->lo_owner
.so_is_open_owner
= 0;
3852 /* It is the openowner seqid that will be incremented in encode in the
3853 * case of new lockowners; so increment the lock seqid manually: */
3854 lo
->lo_owner
.so_seqid
= lock
->lk_new_lock_seqid
+ 1;
3855 hash_lockowner(lo
, strhashval
, clp
, open_stp
);
3859 static struct nfs4_stateid
*
3860 alloc_init_lock_stateid(struct nfs4_lockowner
*lo
, struct nfs4_file
*fp
, struct nfs4_stateid
*open_stp
)
3862 struct nfs4_stateid
*stp
;
3863 unsigned int hashval
= stateid_hashval(lo
->lo_owner
.so_id
, fp
->fi_id
);
3865 stp
= nfs4_alloc_stateid();
3868 INIT_LIST_HEAD(&stp
->st_hash
);
3869 INIT_LIST_HEAD(&stp
->st_perfile
);
3870 INIT_LIST_HEAD(&stp
->st_perstateowner
);
3871 INIT_LIST_HEAD(&stp
->st_lockowners
); /* not used */
3872 list_add(&stp
->st_hash
, &stateid_hashtbl
[hashval
]);
3873 list_add(&stp
->st_perfile
, &fp
->fi_stateids
);
3874 list_add(&stp
->st_perstateowner
, &lo
->lo_owner
.so_stateids
);
3875 stp
->st_stateowner
= &lo
->lo_owner
;
3876 stp
->st_type
= NFS4_LOCK_STID
;
3879 stp
->st_stateid
.si_boot
= boot_time
;
3880 stp
->st_stateid
.si_stateownerid
= lo
->lo_owner
.so_id
;
3881 stp
->st_stateid
.si_fileid
= fp
->fi_id
;
3882 /* note will be incremented before first return to client: */
3883 stp
->st_stateid
.si_generation
= 0;
3884 stp
->st_access_bmap
= 0;
3885 stp
->st_deny_bmap
= open_stp
->st_deny_bmap
;
3886 stp
->st_openstp
= open_stp
;
3893 check_lock_length(u64 offset
, u64 length
)
3895 return ((length
== 0) || ((length
!= NFS4_MAX_UINT64
) &&
3896 LOFF_OVERFLOW(offset
, length
)));
3899 static void get_lock_access(struct nfs4_stateid
*lock_stp
, u32 access
)
3901 struct nfs4_file
*fp
= lock_stp
->st_file
;
3902 int oflag
= nfs4_access_to_omode(access
);
3904 if (test_bit(access
, &lock_stp
->st_access_bmap
))
3906 nfs4_file_get_access(fp
, oflag
);
3907 __set_bit(access
, &lock_stp
->st_access_bmap
);
3914 nfsd4_lock(struct svc_rqst
*rqstp
, struct nfsd4_compound_state
*cstate
,
3915 struct nfsd4_lock
*lock
)
3917 struct nfs4_openowner
*open_sop
= NULL
;
3918 struct nfs4_lockowner
*lock_sop
= NULL
;
3919 struct nfs4_stateid
*lock_stp
;
3920 struct nfs4_file
*fp
;
3921 struct file
*filp
= NULL
;
3922 struct file_lock file_lock
;
3923 struct file_lock conflock
;
3925 unsigned int strhashval
;
3929 dprintk("NFSD: nfsd4_lock: start=%Ld length=%Ld\n",
3930 (long long) lock
->lk_offset
,
3931 (long long) lock
->lk_length
);
3933 if (check_lock_length(lock
->lk_offset
, lock
->lk_length
))
3934 return nfserr_inval
;
3936 if ((status
= fh_verify(rqstp
, &cstate
->current_fh
,
3937 S_IFREG
, NFSD_MAY_LOCK
))) {
3938 dprintk("NFSD: nfsd4_lock: permission denied!\n");
3944 if (lock
->lk_is_new
) {
3946 * Client indicates that this is a new lockowner.
3947 * Use open owner and open stateid to create lock owner and
3950 struct nfs4_stateid
*open_stp
= NULL
;
3952 status
= nfserr_stale_clientid
;
3953 if (!nfsd4_has_session(cstate
) &&
3954 STALE_CLIENTID(&lock
->lk_new_clientid
))
3957 /* validate and update open stateid and open seqid */
3958 status
= nfs4_preprocess_seqid_op(cstate
,
3959 lock
->lk_new_open_seqid
,
3960 &lock
->lk_new_open_stateid
,
3961 OPEN_STATE
, &open_stp
);
3964 open_sop
= openowner(open_stp
->st_stateowner
);
3965 status
= nfserr_bad_stateid
;
3966 if (!nfsd4_has_session(cstate
) &&
3967 !same_clid(&open_sop
->oo_owner
.so_client
->cl_clientid
,
3968 &lock
->v
.new.clientid
))
3970 /* create lockowner and lock stateid */
3971 fp
= open_stp
->st_file
;
3972 strhashval
= lock_ownerstr_hashval(fp
->fi_inode
,
3973 open_sop
->oo_owner
.so_client
->cl_clientid
.cl_id
,
3974 &lock
->v
.new.owner
);
3975 /* XXX: Do we need to check for duplicate stateowners on
3976 * the same file, or should they just be allowed (and
3977 * create new stateids)? */
3978 status
= nfserr_jukebox
;
3979 lock_sop
= alloc_init_lock_stateowner(strhashval
,
3980 open_sop
->oo_owner
.so_client
, open_stp
, lock
);
3981 if (lock_sop
== NULL
)
3983 lock_stp
= alloc_init_lock_stateid(lock_sop
, fp
, open_stp
);
3984 if (lock_stp
== NULL
)
3987 /* lock (lock owner + lock stateid) already exists */
3988 status
= nfs4_preprocess_seqid_op(cstate
,
3989 lock
->lk_old_lock_seqid
,
3990 &lock
->lk_old_lock_stateid
,
3991 LOCK_STATE
, &lock_stp
);
3994 lock_sop
= lockowner(lock_stp
->st_stateowner
);
3995 fp
= lock_stp
->st_file
;
3997 /* lock_sop and lock_stp have been created or found */
3999 lkflg
= setlkflg(lock
->lk_type
);
4000 status
= nfs4_check_openmode(lock_stp
, lkflg
);
4004 status
= nfserr_grace
;
4005 if (locks_in_grace() && !lock
->lk_reclaim
)
4007 status
= nfserr_no_grace
;
4008 if (!locks_in_grace() && lock
->lk_reclaim
)
4011 locks_init_lock(&file_lock
);
4012 switch (lock
->lk_type
) {
4015 filp
= find_readable_file(lock_stp
->st_file
);
4017 get_lock_access(lock_stp
, NFS4_SHARE_ACCESS_READ
);
4018 file_lock
.fl_type
= F_RDLCK
;
4021 case NFS4_WRITEW_LT
:
4022 filp
= find_writeable_file(lock_stp
->st_file
);
4024 get_lock_access(lock_stp
, NFS4_SHARE_ACCESS_WRITE
);
4025 file_lock
.fl_type
= F_WRLCK
;
4028 status
= nfserr_inval
;
4032 status
= nfserr_openmode
;
4035 file_lock
.fl_owner
= (fl_owner_t
)lock_sop
;
4036 file_lock
.fl_pid
= current
->tgid
;
4037 file_lock
.fl_file
= filp
;
4038 file_lock
.fl_flags
= FL_POSIX
;
4039 file_lock
.fl_lmops
= &nfsd_posix_mng_ops
;
4041 file_lock
.fl_start
= lock
->lk_offset
;
4042 file_lock
.fl_end
= last_byte_offset(lock
->lk_offset
, lock
->lk_length
);
4043 nfs4_transform_lock_offset(&file_lock
);
4046 * Try to lock the file in the VFS.
4047 * Note: locks.c uses the BKL to protect the inode's lock list.
4050 err
= vfs_lock_file(filp
, F_SETLK
, &file_lock
, &conflock
);
4052 case 0: /* success! */
4053 update_stateid(&lock_stp
->st_stateid
);
4054 memcpy(&lock
->lk_resp_stateid
, &lock_stp
->st_stateid
,
4058 case (EAGAIN
): /* conflock holds conflicting lock */
4059 status
= nfserr_denied
;
4060 dprintk("NFSD: nfsd4_lock: conflicting lock found!\n");
4061 nfs4_set_lock_denied(&conflock
, &lock
->lk_denied
);
4064 status
= nfserr_deadlock
;
4067 dprintk("NFSD: nfsd4_lock: vfs_lock_file() failed! status %d\n",err
);
4068 status
= nfserrno(err
);
4072 if (status
&& lock
->lk_is_new
&& lock_sop
)
4073 release_lockowner(lock_sop
);
4074 if (!cstate
->replay_owner
)
4075 nfs4_unlock_state();
4080 * The NFSv4 spec allows a client to do a LOCKT without holding an OPEN,
4081 * so we do a temporary open here just to get an open file to pass to
4082 * vfs_test_lock. (Arguably perhaps test_lock should be done with an
4085 static int nfsd_test_lock(struct svc_rqst
*rqstp
, struct svc_fh
*fhp
, struct file_lock
*lock
)
4090 err
= nfsd_open(rqstp
, fhp
, S_IFREG
, NFSD_MAY_READ
, &file
);
4093 err
= vfs_test_lock(file
, lock
);
4102 nfsd4_lockt(struct svc_rqst
*rqstp
, struct nfsd4_compound_state
*cstate
,
4103 struct nfsd4_lockt
*lockt
)
4105 struct inode
*inode
;
4106 struct file_lock file_lock
;
4107 struct nfs4_lockowner
*lo
;
4111 if (locks_in_grace())
4112 return nfserr_grace
;
4114 if (check_lock_length(lockt
->lt_offset
, lockt
->lt_length
))
4115 return nfserr_inval
;
4119 status
= nfserr_stale_clientid
;
4120 if (!nfsd4_has_session(cstate
) && STALE_CLIENTID(&lockt
->lt_clientid
))
4123 if ((status
= fh_verify(rqstp
, &cstate
->current_fh
, S_IFREG
, 0)))
4126 inode
= cstate
->current_fh
.fh_dentry
->d_inode
;
4127 locks_init_lock(&file_lock
);
4128 switch (lockt
->lt_type
) {
4131 file_lock
.fl_type
= F_RDLCK
;
4134 case NFS4_WRITEW_LT
:
4135 file_lock
.fl_type
= F_WRLCK
;
4138 dprintk("NFSD: nfs4_lockt: bad lock type!\n");
4139 status
= nfserr_inval
;
4143 lo
= find_lockowner_str(inode
, &lockt
->lt_clientid
, &lockt
->lt_owner
);
4145 file_lock
.fl_owner
= (fl_owner_t
)lo
;
4146 file_lock
.fl_pid
= current
->tgid
;
4147 file_lock
.fl_flags
= FL_POSIX
;
4149 file_lock
.fl_start
= lockt
->lt_offset
;
4150 file_lock
.fl_end
= last_byte_offset(lockt
->lt_offset
, lockt
->lt_length
);
4152 nfs4_transform_lock_offset(&file_lock
);
4155 error
= nfsd_test_lock(rqstp
, &cstate
->current_fh
, &file_lock
);
4157 status
= nfserrno(error
);
4160 if (file_lock
.fl_type
!= F_UNLCK
) {
4161 status
= nfserr_denied
;
4162 nfs4_set_lock_denied(&file_lock
, &lockt
->lt_denied
);
4165 nfs4_unlock_state();
4170 nfsd4_locku(struct svc_rqst
*rqstp
, struct nfsd4_compound_state
*cstate
,
4171 struct nfsd4_locku
*locku
)
4173 struct nfs4_stateid
*stp
;
4174 struct file
*filp
= NULL
;
4175 struct file_lock file_lock
;
4179 dprintk("NFSD: nfsd4_locku: start=%Ld length=%Ld\n",
4180 (long long) locku
->lu_offset
,
4181 (long long) locku
->lu_length
);
4183 if (check_lock_length(locku
->lu_offset
, locku
->lu_length
))
4184 return nfserr_inval
;
4188 status
= nfs4_preprocess_seqid_op(cstate
, locku
->lu_seqid
,
4189 &locku
->lu_stateid
, LOCK_STATE
, &stp
);
4192 filp
= find_any_file(stp
->st_file
);
4194 status
= nfserr_lock_range
;
4198 locks_init_lock(&file_lock
);
4199 file_lock
.fl_type
= F_UNLCK
;
4200 file_lock
.fl_owner
= (fl_owner_t
)lockowner(stp
->st_stateowner
);
4201 file_lock
.fl_pid
= current
->tgid
;
4202 file_lock
.fl_file
= filp
;
4203 file_lock
.fl_flags
= FL_POSIX
;
4204 file_lock
.fl_lmops
= &nfsd_posix_mng_ops
;
4205 file_lock
.fl_start
= locku
->lu_offset
;
4207 file_lock
.fl_end
= last_byte_offset(locku
->lu_offset
, locku
->lu_length
);
4208 nfs4_transform_lock_offset(&file_lock
);
4211 * Try to unlock the file in the VFS.
4213 err
= vfs_lock_file(filp
, F_SETLK
, &file_lock
, NULL
);
4215 dprintk("NFSD: nfs4_locku: vfs_lock_file failed!\n");
4219 * OK, unlock succeeded; the only thing left to do is update the stateid.
4221 update_stateid(&stp
->st_stateid
);
4222 memcpy(&locku
->lu_stateid
, &stp
->st_stateid
, sizeof(stateid_t
));
4225 nfs4_unlock_state();
4229 status
= nfserrno(err
);
4235 * 1: locks held by lockowner
4236 * 0: no locks held by lockowner
4239 check_for_locks(struct nfs4_file
*filp
, struct nfs4_lockowner
*lowner
)
4241 struct file_lock
**flpp
;
4242 struct inode
*inode
= filp
->fi_inode
;
4246 for (flpp
= &inode
->i_flock
; *flpp
!= NULL
; flpp
= &(*flpp
)->fl_next
) {
4247 if ((*flpp
)->fl_owner
== (fl_owner_t
)lowner
) {
4258 nfsd4_release_lockowner(struct svc_rqst
*rqstp
,
4259 struct nfsd4_compound_state
*cstate
,
4260 struct nfsd4_release_lockowner
*rlockowner
)
4262 clientid_t
*clid
= &rlockowner
->rl_clientid
;
4263 struct nfs4_stateowner
*sop
;
4264 struct nfs4_lockowner
*lo
;
4265 struct nfs4_stateid
*stp
;
4266 struct xdr_netobj
*owner
= &rlockowner
->rl_owner
;
4267 struct list_head matches
;
4271 dprintk("nfsd4_release_lockowner clientid: (%08x/%08x):\n",
4272 clid
->cl_boot
, clid
->cl_id
);
4274 /* XXX check for lease expiration */
4276 status
= nfserr_stale_clientid
;
4277 if (STALE_CLIENTID(clid
))
4282 status
= nfserr_locks_held
;
4283 /* XXX: we're doing a linear search through all the lockowners.
4284 * Yipes! For now we'll just hope clients aren't really using
4285 * release_lockowner much, but eventually we have to fix these
4286 * data structures. */
4287 INIT_LIST_HEAD(&matches
);
4288 for (i
= 0; i
< LOCK_HASH_SIZE
; i
++) {
4289 list_for_each_entry(sop
, &lock_ownerid_hashtbl
[i
], so_idhash
) {
4290 if (!same_owner_str(sop
, owner
, clid
))
4292 list_for_each_entry(stp
, &sop
->so_stateids
,
4294 lo
= lockowner(sop
);
4295 if (check_for_locks(stp
->st_file
, lo
))
4297 list_add(&lo
->lo_list
, &matches
);
4301 /* Clients probably won't expect us to return with some (but not all)
4302 * of the lockowner state released; so don't release any until all
4303 * have been checked. */
4305 while (!list_empty(&matches
)) {
4306 lo
= list_entry(matches
.next
, struct nfs4_lockowner
,
4308 /* unhash_stateowner deletes so_perclient only
4309 * for openowners. */
4310 list_del(&lo
->lo_list
);
4311 release_lockowner(lo
);
4314 nfs4_unlock_state();
4318 static inline struct nfs4_client_reclaim
*
4321 return kmalloc(sizeof(struct nfs4_client_reclaim
), GFP_KERNEL
);
4325 nfs4_has_reclaimed_state(const char *name
, bool use_exchange_id
)
4327 unsigned int strhashval
= clientstr_hashval(name
);
4328 struct nfs4_client
*clp
;
4330 clp
= find_confirmed_client_by_str(name
, strhashval
);
4335 * failure => all reset bets are off, nfserr_no_grace...
4338 nfs4_client_to_reclaim(const char *name
)
4340 unsigned int strhashval
;
4341 struct nfs4_client_reclaim
*crp
= NULL
;
4343 dprintk("NFSD nfs4_client_to_reclaim NAME: %.*s\n", HEXDIR_LEN
, name
);
4344 crp
= alloc_reclaim();
4347 strhashval
= clientstr_hashval(name
);
4348 INIT_LIST_HEAD(&crp
->cr_strhash
);
4349 list_add(&crp
->cr_strhash
, &reclaim_str_hashtbl
[strhashval
]);
4350 memcpy(crp
->cr_recdir
, name
, HEXDIR_LEN
);
4351 reclaim_str_hashtbl_size
++;
4356 nfs4_release_reclaim(void)
4358 struct nfs4_client_reclaim
*crp
= NULL
;
4361 for (i
= 0; i
< CLIENT_HASH_SIZE
; i
++) {
4362 while (!list_empty(&reclaim_str_hashtbl
[i
])) {
4363 crp
= list_entry(reclaim_str_hashtbl
[i
].next
,
4364 struct nfs4_client_reclaim
, cr_strhash
);
4365 list_del(&crp
->cr_strhash
);
4367 reclaim_str_hashtbl_size
--;
4370 BUG_ON(reclaim_str_hashtbl_size
);
4374 * called from OPEN, CLAIM_PREVIOUS with a new clientid. */
4375 static struct nfs4_client_reclaim
*
4376 nfs4_find_reclaim_client(clientid_t
*clid
)
4378 unsigned int strhashval
;
4379 struct nfs4_client
*clp
;
4380 struct nfs4_client_reclaim
*crp
= NULL
;
4383 /* find clientid in conf_id_hashtbl */
4384 clp
= find_confirmed_client(clid
);
4388 dprintk("NFSD: nfs4_find_reclaim_client for %.*s with recdir %s\n",
4389 clp
->cl_name
.len
, clp
->cl_name
.data
,
4392 /* find clp->cl_name in reclaim_str_hashtbl */
4393 strhashval
= clientstr_hashval(clp
->cl_recdir
);
4394 list_for_each_entry(crp
, &reclaim_str_hashtbl
[strhashval
], cr_strhash
) {
4395 if (same_name(crp
->cr_recdir
, clp
->cl_recdir
)) {
4403 * Called from OPEN. Look for clientid in reclaim list.
4406 nfs4_check_open_reclaim(clientid_t
*clid
)
4408 return nfs4_find_reclaim_client(clid
) ? nfs_ok
: nfserr_reclaim_bad
;
4411 /* initialization to perform at module load time: */
4414 nfs4_state_init(void)
4418 status
= nfsd4_init_slabs();
4421 for (i
= 0; i
< CLIENT_HASH_SIZE
; i
++) {
4422 INIT_LIST_HEAD(&conf_id_hashtbl
[i
]);
4423 INIT_LIST_HEAD(&conf_str_hashtbl
[i
]);
4424 INIT_LIST_HEAD(&unconf_str_hashtbl
[i
]);
4425 INIT_LIST_HEAD(&unconf_id_hashtbl
[i
]);
4426 INIT_LIST_HEAD(&reclaim_str_hashtbl
[i
]);
4428 for (i
= 0; i
< SESSION_HASH_SIZE
; i
++)
4429 INIT_LIST_HEAD(&sessionid_hashtbl
[i
]);
4430 for (i
= 0; i
< FILE_HASH_SIZE
; i
++) {
4431 INIT_LIST_HEAD(&file_hashtbl
[i
]);
4433 for (i
= 0; i
< OPEN_OWNER_HASH_SIZE
; i
++) {
4434 INIT_LIST_HEAD(&open_ownerstr_hashtbl
[i
]);
4435 INIT_LIST_HEAD(&open_ownerid_hashtbl
[i
]);
4437 for (i
= 0; i
< STATEID_HASH_SIZE
; i
++)
4438 INIT_LIST_HEAD(&stateid_hashtbl
[i
]);
4439 for (i
= 0; i
< LOCK_HASH_SIZE
; i
++) {
4440 INIT_LIST_HEAD(&lock_ownerid_hashtbl
[i
]);
4441 INIT_LIST_HEAD(&lock_ownerstr_hashtbl
[i
]);
4443 memset(&onestateid
, ~0, sizeof(stateid_t
));
4444 INIT_LIST_HEAD(&close_lru
);
4445 INIT_LIST_HEAD(&client_lru
);
4446 INIT_LIST_HEAD(&del_recall_lru
);
4447 reclaim_str_hashtbl_size
= 0;
4452 nfsd4_load_reboot_recovery_data(void)
4457 nfsd4_init_recdir();
4458 status
= nfsd4_recdir_load();
4459 nfs4_unlock_state();
4461 printk("NFSD: Failure reading reboot recovery data\n");
4465 * Since the lifetime of a delegation isn't limited to that of an open, a
4466 * client may quite reasonably hang on to a delegation as long as it has
4467 * the inode cached. This becomes an obvious problem the first time a
4468 * client's inode cache approaches the size of the server's total memory.
4470 * For now we avoid this problem by imposing a hard limit on the number
4471 * of delegations, which varies according to the server's memory size.
4474 set_max_delegations(void)
4477 * Allow at most 4 delegations per megabyte of RAM. Quick
4478 * estimates suggest that in the worst case (where every delegation
4479 * is for a different inode), a delegation could take about 1.5K,
4480 * giving a worst case usage of about 6% of memory.
4482 max_delegations
= nr_free_buffer_pages() >> (20 - 2 - PAGE_SHIFT
);
4485 /* initialization to perform when the nfsd service is started: */
4488 __nfs4_state_start(void)
4492 boot_time
= get_seconds();
4493 locks_start_grace(&nfsd4_manager
);
4494 printk(KERN_INFO
"NFSD: starting %ld-second grace period\n",
4496 ret
= set_callback_cred();
4499 laundry_wq
= create_singlethread_workqueue("nfsd4");
4500 if (laundry_wq
== NULL
)
4502 ret
= nfsd4_create_callback_queue();
4504 goto out_free_laundry
;
4505 queue_delayed_work(laundry_wq
, &laundromat_work
, nfsd4_grace
* HZ
);
4506 set_max_delegations();
4509 destroy_workqueue(laundry_wq
);
4514 nfs4_state_start(void)
4516 nfsd4_load_reboot_recovery_data();
4517 return __nfs4_state_start();
4521 __nfs4_state_shutdown(void)
4524 struct nfs4_client
*clp
= NULL
;
4525 struct nfs4_delegation
*dp
= NULL
;
4526 struct list_head
*pos
, *next
, reaplist
;
4528 for (i
= 0; i
< CLIENT_HASH_SIZE
; i
++) {
4529 while (!list_empty(&conf_id_hashtbl
[i
])) {
4530 clp
= list_entry(conf_id_hashtbl
[i
].next
, struct nfs4_client
, cl_idhash
);
4533 while (!list_empty(&unconf_str_hashtbl
[i
])) {
4534 clp
= list_entry(unconf_str_hashtbl
[i
].next
, struct nfs4_client
, cl_strhash
);
4538 INIT_LIST_HEAD(&reaplist
);
4539 spin_lock(&recall_lock
);
4540 list_for_each_safe(pos
, next
, &del_recall_lru
) {
4541 dp
= list_entry (pos
, struct nfs4_delegation
, dl_recall_lru
);
4542 list_move(&dp
->dl_recall_lru
, &reaplist
);
4544 spin_unlock(&recall_lock
);
4545 list_for_each_safe(pos
, next
, &reaplist
) {
4546 dp
= list_entry (pos
, struct nfs4_delegation
, dl_recall_lru
);
4547 list_del_init(&dp
->dl_recall_lru
);
4548 unhash_delegation(dp
);
4551 nfsd4_shutdown_recdir();
4555 nfs4_state_shutdown(void)
4557 cancel_delayed_work_sync(&laundromat_work
);
4558 destroy_workqueue(laundry_wq
);
4559 locks_end_grace(&nfsd4_manager
);
4561 nfs4_release_reclaim();
4562 __nfs4_state_shutdown();
4563 nfs4_unlock_state();
4564 nfsd4_destroy_callback_queue();