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
)
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
))
1083 static struct nfs4_stateid
*find_stateid_by_type(stateid_t
*t
, int flags
)
1085 struct nfs4_stateid
*s
;
1087 s
= find_stateid(t
);
1090 if (flags
& LOCK_STATE
&& s
->st_type
== NFS4_LOCK_STID
)
1092 if (flags
& OPEN_STATE
&& s
->st_type
== NFS4_OPEN_STID
)
1097 static struct nfs4_client
*create_client(struct xdr_netobj name
, char *recdir
,
1098 struct svc_rqst
*rqstp
, nfs4_verifier
*verf
)
1100 struct nfs4_client
*clp
;
1101 struct sockaddr
*sa
= svc_addr(rqstp
);
1104 clp
= alloc_client(name
);
1108 INIT_LIST_HEAD(&clp
->cl_sessions
);
1110 princ
= svc_gss_principal(rqstp
);
1112 clp
->cl_principal
= kstrdup(princ
, GFP_KERNEL
);
1113 if (clp
->cl_principal
== NULL
) {
1119 memcpy(clp
->cl_recdir
, recdir
, HEXDIR_LEN
);
1120 atomic_set(&clp
->cl_refcount
, 0);
1121 clp
->cl_cb_state
= NFSD4_CB_UNKNOWN
;
1122 INIT_LIST_HEAD(&clp
->cl_idhash
);
1123 INIT_LIST_HEAD(&clp
->cl_strhash
);
1124 INIT_LIST_HEAD(&clp
->cl_openowners
);
1125 INIT_LIST_HEAD(&clp
->cl_delegations
);
1126 INIT_LIST_HEAD(&clp
->cl_lru
);
1127 INIT_LIST_HEAD(&clp
->cl_callbacks
);
1128 spin_lock_init(&clp
->cl_lock
);
1129 INIT_WORK(&clp
->cl_cb_null
.cb_work
, nfsd4_do_callback_rpc
);
1130 clp
->cl_time
= get_seconds();
1131 clear_bit(0, &clp
->cl_cb_slot_busy
);
1132 rpc_init_wait_queue(&clp
->cl_cb_waitq
, "Backchannel slot table");
1133 copy_verf(clp
, verf
);
1134 rpc_copy_addr((struct sockaddr
*) &clp
->cl_addr
, sa
);
1135 clp
->cl_flavor
= rqstp
->rq_flavor
;
1136 copy_cred(&clp
->cl_cred
, &rqstp
->rq_cred
);
1138 clp
->cl_cb_session
= NULL
;
1142 static int check_name(struct xdr_netobj name
)
1146 if (name
.len
> NFS4_OPAQUE_LIMIT
) {
1147 dprintk("NFSD: check_name: name too long(%d)!\n", name
.len
);
1154 add_to_unconfirmed(struct nfs4_client
*clp
, unsigned int strhashval
)
1156 unsigned int idhashval
;
1158 list_add(&clp
->cl_strhash
, &unconf_str_hashtbl
[strhashval
]);
1159 idhashval
= clientid_hashval(clp
->cl_clientid
.cl_id
);
1160 list_add(&clp
->cl_idhash
, &unconf_id_hashtbl
[idhashval
]);
1165 move_to_confirmed(struct nfs4_client
*clp
)
1167 unsigned int idhashval
= clientid_hashval(clp
->cl_clientid
.cl_id
);
1168 unsigned int strhashval
;
1170 dprintk("NFSD: move_to_confirm nfs4_client %p\n", clp
);
1171 list_move(&clp
->cl_idhash
, &conf_id_hashtbl
[idhashval
]);
1172 strhashval
= clientstr_hashval(clp
->cl_recdir
);
1173 list_move(&clp
->cl_strhash
, &conf_str_hashtbl
[strhashval
]);
1177 static struct nfs4_client
*
1178 find_confirmed_client(clientid_t
*clid
)
1180 struct nfs4_client
*clp
;
1181 unsigned int idhashval
= clientid_hashval(clid
->cl_id
);
1183 list_for_each_entry(clp
, &conf_id_hashtbl
[idhashval
], cl_idhash
) {
1184 if (same_clid(&clp
->cl_clientid
, clid
))
1190 static struct nfs4_client
*
1191 find_unconfirmed_client(clientid_t
*clid
)
1193 struct nfs4_client
*clp
;
1194 unsigned int idhashval
= clientid_hashval(clid
->cl_id
);
1196 list_for_each_entry(clp
, &unconf_id_hashtbl
[idhashval
], cl_idhash
) {
1197 if (same_clid(&clp
->cl_clientid
, clid
))
1203 static bool clp_used_exchangeid(struct nfs4_client
*clp
)
1205 return clp
->cl_exchange_flags
!= 0;
1208 static struct nfs4_client
*
1209 find_confirmed_client_by_str(const char *dname
, unsigned int hashval
)
1211 struct nfs4_client
*clp
;
1213 list_for_each_entry(clp
, &conf_str_hashtbl
[hashval
], cl_strhash
) {
1214 if (same_name(clp
->cl_recdir
, dname
))
1220 static struct nfs4_client
*
1221 find_unconfirmed_client_by_str(const char *dname
, unsigned int hashval
)
1223 struct nfs4_client
*clp
;
1225 list_for_each_entry(clp
, &unconf_str_hashtbl
[hashval
], cl_strhash
) {
1226 if (same_name(clp
->cl_recdir
, dname
))
1232 static void rpc_svcaddr2sockaddr(struct sockaddr
*sa
, unsigned short family
, union svc_addr_u
*svcaddr
)
1236 ((struct sockaddr_in
*)sa
)->sin_family
= AF_INET
;
1237 ((struct sockaddr_in
*)sa
)->sin_addr
= svcaddr
->addr
;
1240 ((struct sockaddr_in6
*)sa
)->sin6_family
= AF_INET6
;
1241 ((struct sockaddr_in6
*)sa
)->sin6_addr
= svcaddr
->addr6
;
1247 gen_callback(struct nfs4_client
*clp
, struct nfsd4_setclientid
*se
, struct svc_rqst
*rqstp
)
1249 struct nfs4_cb_conn
*conn
= &clp
->cl_cb_conn
;
1250 struct sockaddr
*sa
= svc_addr(rqstp
);
1251 u32 scopeid
= rpc_get_scope_id(sa
);
1252 unsigned short expected_family
;
1254 /* Currently, we only support tcp and tcp6 for the callback channel */
1255 if (se
->se_callback_netid_len
== 3 &&
1256 !memcmp(se
->se_callback_netid_val
, "tcp", 3))
1257 expected_family
= AF_INET
;
1258 else if (se
->se_callback_netid_len
== 4 &&
1259 !memcmp(se
->se_callback_netid_val
, "tcp6", 4))
1260 expected_family
= AF_INET6
;
1264 conn
->cb_addrlen
= rpc_uaddr2sockaddr(se
->se_callback_addr_val
,
1265 se
->se_callback_addr_len
,
1266 (struct sockaddr
*)&conn
->cb_addr
,
1267 sizeof(conn
->cb_addr
));
1269 if (!conn
->cb_addrlen
|| conn
->cb_addr
.ss_family
!= expected_family
)
1272 if (conn
->cb_addr
.ss_family
== AF_INET6
)
1273 ((struct sockaddr_in6
*)&conn
->cb_addr
)->sin6_scope_id
= scopeid
;
1275 conn
->cb_prog
= se
->se_callback_prog
;
1276 conn
->cb_ident
= se
->se_callback_ident
;
1277 rpc_svcaddr2sockaddr((struct sockaddr
*)&conn
->cb_saddr
, expected_family
, &rqstp
->rq_daddr
);
1280 conn
->cb_addr
.ss_family
= AF_UNSPEC
;
1281 conn
->cb_addrlen
= 0;
1282 dprintk(KERN_INFO
"NFSD: this client (clientid %08x/%08x) "
1283 "will not receive delegations\n",
1284 clp
->cl_clientid
.cl_boot
, clp
->cl_clientid
.cl_id
);
1290 * Cache a reply. nfsd4_check_drc_limit() has bounded the cache size.
1293 nfsd4_store_cache_entry(struct nfsd4_compoundres
*resp
)
1295 struct nfsd4_slot
*slot
= resp
->cstate
.slot
;
1298 dprintk("--> %s slot %p\n", __func__
, slot
);
1300 slot
->sl_opcnt
= resp
->opcnt
;
1301 slot
->sl_status
= resp
->cstate
.status
;
1303 if (nfsd4_not_cached(resp
)) {
1304 slot
->sl_datalen
= 0;
1307 slot
->sl_datalen
= (char *)resp
->p
- (char *)resp
->cstate
.datap
;
1308 base
= (char *)resp
->cstate
.datap
-
1309 (char *)resp
->xbuf
->head
[0].iov_base
;
1310 if (read_bytes_from_xdr_buf(resp
->xbuf
, base
, slot
->sl_data
,
1312 WARN("%s: sessions DRC could not cache compound\n", __func__
);
1317 * Encode the replay sequence operation from the slot values.
1318 * If cachethis is FALSE encode the uncached rep error on the next
1319 * operation which sets resp->p and increments resp->opcnt for
1320 * nfs4svc_encode_compoundres.
1324 nfsd4_enc_sequence_replay(struct nfsd4_compoundargs
*args
,
1325 struct nfsd4_compoundres
*resp
)
1327 struct nfsd4_op
*op
;
1328 struct nfsd4_slot
*slot
= resp
->cstate
.slot
;
1330 dprintk("--> %s resp->opcnt %d cachethis %u \n", __func__
,
1331 resp
->opcnt
, resp
->cstate
.slot
->sl_cachethis
);
1333 /* Encode the replayed sequence operation */
1334 op
= &args
->ops
[resp
->opcnt
- 1];
1335 nfsd4_encode_operation(resp
, op
);
1337 /* Return nfserr_retry_uncached_rep in next operation. */
1338 if (args
->opcnt
> 1 && slot
->sl_cachethis
== 0) {
1339 op
= &args
->ops
[resp
->opcnt
++];
1340 op
->status
= nfserr_retry_uncached_rep
;
1341 nfsd4_encode_operation(resp
, op
);
1347 * The sequence operation is not cached because we can use the slot and
1351 nfsd4_replay_cache_entry(struct nfsd4_compoundres
*resp
,
1352 struct nfsd4_sequence
*seq
)
1354 struct nfsd4_slot
*slot
= resp
->cstate
.slot
;
1357 dprintk("--> %s slot %p\n", __func__
, slot
);
1359 /* Either returns 0 or nfserr_retry_uncached */
1360 status
= nfsd4_enc_sequence_replay(resp
->rqstp
->rq_argp
, resp
);
1361 if (status
== nfserr_retry_uncached_rep
)
1364 /* The sequence operation has been encoded, cstate->datap set. */
1365 memcpy(resp
->cstate
.datap
, slot
->sl_data
, slot
->sl_datalen
);
1367 resp
->opcnt
= slot
->sl_opcnt
;
1368 resp
->p
= resp
->cstate
.datap
+ XDR_QUADLEN(slot
->sl_datalen
);
1369 status
= slot
->sl_status
;
1375 * Set the exchange_id flags returned by the server.
1378 nfsd4_set_ex_flags(struct nfs4_client
*new, struct nfsd4_exchange_id
*clid
)
1380 /* pNFS is not supported */
1381 new->cl_exchange_flags
|= EXCHGID4_FLAG_USE_NON_PNFS
;
1383 /* Referrals are supported, Migration is not. */
1384 new->cl_exchange_flags
|= EXCHGID4_FLAG_SUPP_MOVED_REFER
;
1386 /* set the wire flags to return to client. */
1387 clid
->flags
= new->cl_exchange_flags
;
1391 nfsd4_exchange_id(struct svc_rqst
*rqstp
,
1392 struct nfsd4_compound_state
*cstate
,
1393 struct nfsd4_exchange_id
*exid
)
1395 struct nfs4_client
*unconf
, *conf
, *new;
1397 unsigned int strhashval
;
1398 char dname
[HEXDIR_LEN
];
1399 char addr_str
[INET6_ADDRSTRLEN
];
1400 nfs4_verifier verf
= exid
->verifier
;
1401 struct sockaddr
*sa
= svc_addr(rqstp
);
1403 rpc_ntop(sa
, addr_str
, sizeof(addr_str
));
1404 dprintk("%s rqstp=%p exid=%p clname.len=%u clname.data=%p "
1405 "ip_addr=%s flags %x, spa_how %d\n",
1406 __func__
, rqstp
, exid
, exid
->clname
.len
, exid
->clname
.data
,
1407 addr_str
, exid
->flags
, exid
->spa_how
);
1409 if (!check_name(exid
->clname
) || (exid
->flags
& ~EXCHGID4_FLAG_MASK_A
))
1410 return nfserr_inval
;
1412 /* Currently only support SP4_NONE */
1413 switch (exid
->spa_how
) {
1417 return nfserr_serverfault
;
1419 BUG(); /* checked by xdr code */
1421 return nfserr_serverfault
; /* no excuse :-/ */
1424 status
= nfs4_make_rec_clidname(dname
, &exid
->clname
);
1429 strhashval
= clientstr_hashval(dname
);
1434 conf
= find_confirmed_client_by_str(dname
, strhashval
);
1436 if (!clp_used_exchangeid(conf
)) {
1437 status
= nfserr_clid_inuse
; /* XXX: ? */
1440 if (!same_verf(&verf
, &conf
->cl_verifier
)) {
1441 /* 18.35.4 case 8 */
1442 if (exid
->flags
& EXCHGID4_FLAG_UPD_CONFIRMED_REC_A
) {
1443 status
= nfserr_not_same
;
1446 /* Client reboot: destroy old state */
1447 expire_client(conf
);
1450 if (!same_creds(&conf
->cl_cred
, &rqstp
->rq_cred
)) {
1451 /* 18.35.4 case 9 */
1452 if (exid
->flags
& EXCHGID4_FLAG_UPD_CONFIRMED_REC_A
) {
1453 status
= nfserr_perm
;
1456 expire_client(conf
);
1460 * Set bit when the owner id and verifier map to an already
1461 * confirmed client id (18.35.3).
1463 exid
->flags
|= EXCHGID4_FLAG_CONFIRMED_R
;
1466 * Falling into 18.35.4 case 2, possible router replay.
1467 * Leave confirmed record intact and return same result.
1469 copy_verf(conf
, &verf
);
1474 /* 18.35.4 case 7 */
1475 if (exid
->flags
& EXCHGID4_FLAG_UPD_CONFIRMED_REC_A
) {
1476 status
= nfserr_noent
;
1480 unconf
= find_unconfirmed_client_by_str(dname
, strhashval
);
1483 * Possible retry or client restart. Per 18.35.4 case 4,
1484 * a new unconfirmed record should be generated regardless
1485 * of whether any properties have changed.
1487 expire_client(unconf
);
1492 new = create_client(exid
->clname
, dname
, rqstp
, &verf
);
1494 status
= nfserr_jukebox
;
1499 add_to_unconfirmed(new, strhashval
);
1501 exid
->clientid
.cl_boot
= new->cl_clientid
.cl_boot
;
1502 exid
->clientid
.cl_id
= new->cl_clientid
.cl_id
;
1505 nfsd4_set_ex_flags(new, exid
);
1507 dprintk("nfsd4_exchange_id seqid %d flags %x\n",
1508 new->cl_cs_slot
.sl_seqid
, new->cl_exchange_flags
);
1512 nfs4_unlock_state();
1514 dprintk("nfsd4_exchange_id returns %d\n", ntohl(status
));
1519 check_slot_seqid(u32 seqid
, u32 slot_seqid
, int slot_inuse
)
1521 dprintk("%s enter. seqid %d slot_seqid %d\n", __func__
, seqid
,
1524 /* The slot is in use, and no response has been sent. */
1526 if (seqid
== slot_seqid
)
1527 return nfserr_jukebox
;
1529 return nfserr_seq_misordered
;
1532 if (likely(seqid
== slot_seqid
+ 1))
1535 if (seqid
== slot_seqid
)
1536 return nfserr_replay_cache
;
1538 if (seqid
== 1 && (slot_seqid
+ 1) == 0)
1540 /* Misordered replay or misordered new request */
1541 return nfserr_seq_misordered
;
1545 * Cache the create session result into the create session single DRC
1546 * slot cache by saving the xdr structure. sl_seqid has been set.
1547 * Do this for solo or embedded create session operations.
1550 nfsd4_cache_create_session(struct nfsd4_create_session
*cr_ses
,
1551 struct nfsd4_clid_slot
*slot
, int nfserr
)
1553 slot
->sl_status
= nfserr
;
1554 memcpy(&slot
->sl_cr_ses
, cr_ses
, sizeof(*cr_ses
));
1558 nfsd4_replay_create_session(struct nfsd4_create_session
*cr_ses
,
1559 struct nfsd4_clid_slot
*slot
)
1561 memcpy(cr_ses
, &slot
->sl_cr_ses
, sizeof(*cr_ses
));
1562 return slot
->sl_status
;
1565 #define NFSD_MIN_REQ_HDR_SEQ_SZ ((\
1566 2 * 2 + /* credential,verifier: AUTH_NULL, length 0 */ \
1567 1 + /* MIN tag is length with zero, only length */ \
1568 3 + /* version, opcount, opcode */ \
1569 XDR_QUADLEN(NFS4_MAX_SESSIONID_LEN) + \
1570 /* seqid, slotID, slotID, cache */ \
1571 4 ) * sizeof(__be32))
1573 #define NFSD_MIN_RESP_HDR_SEQ_SZ ((\
1574 2 + /* verifier: AUTH_NULL, length 0 */\
1576 1 + /* MIN tag is length with zero, only length */ \
1577 3 + /* opcount, opcode, opstatus*/ \
1578 XDR_QUADLEN(NFS4_MAX_SESSIONID_LEN) + \
1579 /* seqid, slotID, slotID, slotID, status */ \
1580 5 ) * sizeof(__be32))
1582 static __be32
check_forechannel_attrs(struct nfsd4_channel_attrs fchannel
)
1584 return fchannel
.maxreq_sz
< NFSD_MIN_REQ_HDR_SEQ_SZ
1585 || fchannel
.maxresp_sz
< NFSD_MIN_RESP_HDR_SEQ_SZ
;
1589 nfsd4_create_session(struct svc_rqst
*rqstp
,
1590 struct nfsd4_compound_state
*cstate
,
1591 struct nfsd4_create_session
*cr_ses
)
1593 struct sockaddr
*sa
= svc_addr(rqstp
);
1594 struct nfs4_client
*conf
, *unconf
;
1595 struct nfsd4_session
*new;
1596 struct nfsd4_clid_slot
*cs_slot
= NULL
;
1597 bool confirm_me
= false;
1600 if (cr_ses
->flags
& ~SESSION4_FLAG_MASK_A
)
1601 return nfserr_inval
;
1604 unconf
= find_unconfirmed_client(&cr_ses
->clientid
);
1605 conf
= find_confirmed_client(&cr_ses
->clientid
);
1608 cs_slot
= &conf
->cl_cs_slot
;
1609 status
= check_slot_seqid(cr_ses
->seqid
, cs_slot
->sl_seqid
, 0);
1610 if (status
== nfserr_replay_cache
) {
1611 dprintk("Got a create_session replay! seqid= %d\n",
1613 /* Return the cached reply status */
1614 status
= nfsd4_replay_create_session(cr_ses
, cs_slot
);
1616 } else if (cr_ses
->seqid
!= cs_slot
->sl_seqid
+ 1) {
1617 status
= nfserr_seq_misordered
;
1618 dprintk("Sequence misordered!\n");
1619 dprintk("Expected seqid= %d but got seqid= %d\n",
1620 cs_slot
->sl_seqid
, cr_ses
->seqid
);
1623 } else if (unconf
) {
1624 if (!same_creds(&unconf
->cl_cred
, &rqstp
->rq_cred
) ||
1625 !rpc_cmp_addr(sa
, (struct sockaddr
*) &unconf
->cl_addr
)) {
1626 status
= nfserr_clid_inuse
;
1630 cs_slot
= &unconf
->cl_cs_slot
;
1631 status
= check_slot_seqid(cr_ses
->seqid
, cs_slot
->sl_seqid
, 0);
1633 /* an unconfirmed replay returns misordered */
1634 status
= nfserr_seq_misordered
;
1641 status
= nfserr_stale_clientid
;
1646 * XXX: we should probably set this at creation time, and check
1647 * for consistent minorversion use throughout:
1649 conf
->cl_minorversion
= 1;
1651 * We do not support RDMA or persistent sessions
1653 cr_ses
->flags
&= ~SESSION4_PERSIST
;
1654 cr_ses
->flags
&= ~SESSION4_RDMA
;
1656 status
= nfserr_toosmall
;
1657 if (check_forechannel_attrs(cr_ses
->fore_channel
))
1660 status
= nfserr_jukebox
;
1661 new = alloc_init_session(rqstp
, conf
, cr_ses
);
1665 memcpy(cr_ses
->sessionid
.data
, new->se_sessionid
.data
,
1666 NFS4_MAX_SESSIONID_LEN
);
1667 memcpy(&cr_ses
->fore_channel
, &new->se_fchannel
,
1668 sizeof(struct nfsd4_channel_attrs
));
1669 cs_slot
->sl_seqid
++;
1670 cr_ses
->seqid
= cs_slot
->sl_seqid
;
1672 /* cache solo and embedded create sessions under the state lock */
1673 nfsd4_cache_create_session(cr_ses
, cs_slot
, status
);
1675 move_to_confirmed(conf
);
1677 nfs4_unlock_state();
1678 dprintk("%s returns %d\n", __func__
, ntohl(status
));
1682 static bool nfsd4_last_compound_op(struct svc_rqst
*rqstp
)
1684 struct nfsd4_compoundres
*resp
= rqstp
->rq_resp
;
1685 struct nfsd4_compoundargs
*argp
= rqstp
->rq_argp
;
1687 return argp
->opcnt
== resp
->opcnt
;
1690 static __be32
nfsd4_map_bcts_dir(u32
*dir
)
1693 case NFS4_CDFC4_FORE
:
1694 case NFS4_CDFC4_BACK
:
1696 case NFS4_CDFC4_FORE_OR_BOTH
:
1697 case NFS4_CDFC4_BACK_OR_BOTH
:
1698 *dir
= NFS4_CDFC4_BOTH
;
1701 return nfserr_inval
;
1704 __be32
nfsd4_bind_conn_to_session(struct svc_rqst
*rqstp
,
1705 struct nfsd4_compound_state
*cstate
,
1706 struct nfsd4_bind_conn_to_session
*bcts
)
1710 if (!nfsd4_last_compound_op(rqstp
))
1711 return nfserr_not_only_op
;
1712 spin_lock(&client_lock
);
1713 cstate
->session
= find_in_sessionid_hashtbl(&bcts
->sessionid
);
1714 /* Sorta weird: we only need the refcnt'ing because new_conn acquires
1715 * client_lock iself: */
1716 if (cstate
->session
) {
1717 nfsd4_get_session(cstate
->session
);
1718 atomic_inc(&cstate
->session
->se_client
->cl_refcount
);
1720 spin_unlock(&client_lock
);
1721 if (!cstate
->session
)
1722 return nfserr_badsession
;
1724 status
= nfsd4_map_bcts_dir(&bcts
->dir
);
1726 nfsd4_new_conn(rqstp
, cstate
->session
, bcts
->dir
);
1730 static bool nfsd4_compound_in_session(struct nfsd4_session
*session
, struct nfs4_sessionid
*sid
)
1734 return !memcmp(sid
, &session
->se_sessionid
, sizeof(*sid
));
1738 nfsd4_destroy_session(struct svc_rqst
*r
,
1739 struct nfsd4_compound_state
*cstate
,
1740 struct nfsd4_destroy_session
*sessionid
)
1742 struct nfsd4_session
*ses
;
1743 u32 status
= nfserr_badsession
;
1746 * - The confirmed nfs4_client->cl_sessionid holds destroyed sessinid
1747 * - Should we return nfserr_back_chan_busy if waiting for
1748 * callbacks on to-be-destroyed session?
1749 * - Do we need to clear any callback info from previous session?
1752 if (nfsd4_compound_in_session(cstate
->session
, &sessionid
->sessionid
)) {
1753 if (!nfsd4_last_compound_op(r
))
1754 return nfserr_not_only_op
;
1756 dump_sessionid(__func__
, &sessionid
->sessionid
);
1757 spin_lock(&client_lock
);
1758 ses
= find_in_sessionid_hashtbl(&sessionid
->sessionid
);
1760 spin_unlock(&client_lock
);
1764 unhash_session(ses
);
1765 spin_unlock(&client_lock
);
1768 nfsd4_probe_callback_sync(ses
->se_client
);
1769 nfs4_unlock_state();
1771 nfsd4_del_conns(ses
);
1773 nfsd4_put_session(ses
);
1776 dprintk("%s returns %d\n", __func__
, ntohl(status
));
1780 static struct nfsd4_conn
*__nfsd4_find_conn(struct svc_xprt
*xpt
, struct nfsd4_session
*s
)
1782 struct nfsd4_conn
*c
;
1784 list_for_each_entry(c
, &s
->se_conns
, cn_persession
) {
1785 if (c
->cn_xprt
== xpt
) {
1792 static void nfsd4_sequence_check_conn(struct nfsd4_conn
*new, struct nfsd4_session
*ses
)
1794 struct nfs4_client
*clp
= ses
->se_client
;
1795 struct nfsd4_conn
*c
;
1798 spin_lock(&clp
->cl_lock
);
1799 c
= __nfsd4_find_conn(new->cn_xprt
, ses
);
1801 spin_unlock(&clp
->cl_lock
);
1805 __nfsd4_hash_conn(new, ses
);
1806 spin_unlock(&clp
->cl_lock
);
1807 ret
= nfsd4_register_conn(new);
1809 /* oops; xprt is already down: */
1810 nfsd4_conn_lost(&new->cn_xpt_user
);
1814 static bool nfsd4_session_too_many_ops(struct svc_rqst
*rqstp
, struct nfsd4_session
*session
)
1816 struct nfsd4_compoundargs
*args
= rqstp
->rq_argp
;
1818 return args
->opcnt
> session
->se_fchannel
.maxops
;
1821 static bool nfsd4_request_too_big(struct svc_rqst
*rqstp
,
1822 struct nfsd4_session
*session
)
1824 struct xdr_buf
*xb
= &rqstp
->rq_arg
;
1826 return xb
->len
> session
->se_fchannel
.maxreq_sz
;
1830 nfsd4_sequence(struct svc_rqst
*rqstp
,
1831 struct nfsd4_compound_state
*cstate
,
1832 struct nfsd4_sequence
*seq
)
1834 struct nfsd4_compoundres
*resp
= rqstp
->rq_resp
;
1835 struct nfsd4_session
*session
;
1836 struct nfsd4_slot
*slot
;
1837 struct nfsd4_conn
*conn
;
1840 if (resp
->opcnt
!= 1)
1841 return nfserr_sequence_pos
;
1844 * Will be either used or freed by nfsd4_sequence_check_conn
1847 conn
= alloc_conn(rqstp
, NFS4_CDFC4_FORE
);
1849 return nfserr_jukebox
;
1851 spin_lock(&client_lock
);
1852 status
= nfserr_badsession
;
1853 session
= find_in_sessionid_hashtbl(&seq
->sessionid
);
1857 status
= nfserr_too_many_ops
;
1858 if (nfsd4_session_too_many_ops(rqstp
, session
))
1861 status
= nfserr_req_too_big
;
1862 if (nfsd4_request_too_big(rqstp
, session
))
1865 status
= nfserr_badslot
;
1866 if (seq
->slotid
>= session
->se_fchannel
.maxreqs
)
1869 slot
= session
->se_slots
[seq
->slotid
];
1870 dprintk("%s: slotid %d\n", __func__
, seq
->slotid
);
1872 /* We do not negotiate the number of slots yet, so set the
1873 * maxslots to the session maxreqs which is used to encode
1874 * sr_highest_slotid and the sr_target_slot id to maxslots */
1875 seq
->maxslots
= session
->se_fchannel
.maxreqs
;
1877 status
= check_slot_seqid(seq
->seqid
, slot
->sl_seqid
, slot
->sl_inuse
);
1878 if (status
== nfserr_replay_cache
) {
1879 cstate
->slot
= slot
;
1880 cstate
->session
= session
;
1881 /* Return the cached reply status and set cstate->status
1882 * for nfsd4_proc_compound processing */
1883 status
= nfsd4_replay_cache_entry(resp
, seq
);
1884 cstate
->status
= nfserr_replay_cache
;
1890 nfsd4_sequence_check_conn(conn
, session
);
1893 /* Success! bump slot seqid */
1894 slot
->sl_inuse
= true;
1895 slot
->sl_seqid
= seq
->seqid
;
1896 slot
->sl_cachethis
= seq
->cachethis
;
1898 cstate
->slot
= slot
;
1899 cstate
->session
= session
;
1902 /* Hold a session reference until done processing the compound. */
1903 if (cstate
->session
) {
1904 struct nfs4_client
*clp
= session
->se_client
;
1906 nfsd4_get_session(cstate
->session
);
1907 atomic_inc(&clp
->cl_refcount
);
1908 if (clp
->cl_cb_state
== NFSD4_CB_DOWN
)
1909 seq
->status_flags
|= SEQ4_STATUS_CB_PATH_DOWN
;
1912 spin_unlock(&client_lock
);
1913 dprintk("%s: return %d\n", __func__
, ntohl(status
));
1918 nfsd4_reclaim_complete(struct svc_rqst
*rqstp
, struct nfsd4_compound_state
*cstate
, struct nfsd4_reclaim_complete
*rc
)
1922 if (rc
->rca_one_fs
) {
1923 if (!cstate
->current_fh
.fh_dentry
)
1924 return nfserr_nofilehandle
;
1926 * We don't take advantage of the rca_one_fs case.
1927 * That's OK, it's optional, we can safely ignore it.
1933 status
= nfserr_complete_already
;
1934 if (cstate
->session
->se_client
->cl_firststate
)
1937 status
= nfserr_stale_clientid
;
1938 if (is_client_expired(cstate
->session
->se_client
))
1940 * The following error isn't really legal.
1941 * But we only get here if the client just explicitly
1942 * destroyed the client. Surely it no longer cares what
1943 * error it gets back on an operation for the dead
1949 nfsd4_create_clid_dir(cstate
->session
->se_client
);
1951 nfs4_unlock_state();
1956 nfsd4_setclientid(struct svc_rqst
*rqstp
, struct nfsd4_compound_state
*cstate
,
1957 struct nfsd4_setclientid
*setclid
)
1959 struct xdr_netobj clname
= {
1960 .len
= setclid
->se_namelen
,
1961 .data
= setclid
->se_name
,
1963 nfs4_verifier clverifier
= setclid
->se_verf
;
1964 unsigned int strhashval
;
1965 struct nfs4_client
*conf
, *unconf
, *new;
1967 char dname
[HEXDIR_LEN
];
1969 if (!check_name(clname
))
1970 return nfserr_inval
;
1972 status
= nfs4_make_rec_clidname(dname
, &clname
);
1977 * XXX The Duplicate Request Cache (DRC) has been checked (??)
1978 * We get here on a DRC miss.
1981 strhashval
= clientstr_hashval(dname
);
1984 conf
= find_confirmed_client_by_str(dname
, strhashval
);
1986 /* RFC 3530 14.2.33 CASE 0: */
1987 status
= nfserr_clid_inuse
;
1988 if (clp_used_exchangeid(conf
))
1990 if (!same_creds(&conf
->cl_cred
, &rqstp
->rq_cred
)) {
1991 char addr_str
[INET6_ADDRSTRLEN
];
1992 rpc_ntop((struct sockaddr
*) &conf
->cl_addr
, addr_str
,
1994 dprintk("NFSD: setclientid: string in use by client "
1995 "at %s\n", addr_str
);
2000 * section 14.2.33 of RFC 3530 (under the heading "IMPLEMENTATION")
2001 * has a description of SETCLIENTID request processing consisting
2002 * of 5 bullet points, labeled as CASE0 - CASE4 below.
2004 unconf
= find_unconfirmed_client_by_str(dname
, strhashval
);
2005 status
= nfserr_jukebox
;
2008 * RFC 3530 14.2.33 CASE 4:
2009 * placed first, because it is the normal case
2012 expire_client(unconf
);
2013 new = create_client(clname
, dname
, rqstp
, &clverifier
);
2017 } else if (same_verf(&conf
->cl_verifier
, &clverifier
)) {
2019 * RFC 3530 14.2.33 CASE 1:
2020 * probable callback update
2023 /* Note this is removing unconfirmed {*x***},
2024 * which is stronger than RFC recommended {vxc**}.
2025 * This has the advantage that there is at most
2026 * one {*x***} in either list at any time.
2028 expire_client(unconf
);
2030 new = create_client(clname
, dname
, rqstp
, &clverifier
);
2033 copy_clid(new, conf
);
2034 } else if (!unconf
) {
2036 * RFC 3530 14.2.33 CASE 2:
2037 * probable client reboot; state will be removed if
2040 new = create_client(clname
, dname
, rqstp
, &clverifier
);
2046 * RFC 3530 14.2.33 CASE 3:
2047 * probable client reboot; state will be removed if
2050 expire_client(unconf
);
2051 new = create_client(clname
, dname
, rqstp
, &clverifier
);
2057 * XXX: we should probably set this at creation time, and check
2058 * for consistent minorversion use throughout:
2060 new->cl_minorversion
= 0;
2061 gen_callback(new, setclid
, rqstp
);
2062 add_to_unconfirmed(new, strhashval
);
2063 setclid
->se_clientid
.cl_boot
= new->cl_clientid
.cl_boot
;
2064 setclid
->se_clientid
.cl_id
= new->cl_clientid
.cl_id
;
2065 memcpy(setclid
->se_confirm
.data
, new->cl_confirm
.data
, sizeof(setclid
->se_confirm
.data
));
2068 nfs4_unlock_state();
2074 * Section 14.2.34 of RFC 3530 (under the heading "IMPLEMENTATION") has
2075 * a description of SETCLIENTID_CONFIRM request processing consisting of 4
2076 * bullets, labeled as CASE1 - CASE4 below.
2079 nfsd4_setclientid_confirm(struct svc_rqst
*rqstp
,
2080 struct nfsd4_compound_state
*cstate
,
2081 struct nfsd4_setclientid_confirm
*setclientid_confirm
)
2083 struct sockaddr
*sa
= svc_addr(rqstp
);
2084 struct nfs4_client
*conf
, *unconf
;
2085 nfs4_verifier confirm
= setclientid_confirm
->sc_confirm
;
2086 clientid_t
* clid
= &setclientid_confirm
->sc_clientid
;
2089 if (STALE_CLIENTID(clid
))
2090 return nfserr_stale_clientid
;
2092 * XXX The Duplicate Request Cache (DRC) has been checked (??)
2093 * We get here on a DRC miss.
2098 conf
= find_confirmed_client(clid
);
2099 unconf
= find_unconfirmed_client(clid
);
2101 status
= nfserr_clid_inuse
;
2102 if (conf
&& !rpc_cmp_addr((struct sockaddr
*) &conf
->cl_addr
, sa
))
2104 if (unconf
&& !rpc_cmp_addr((struct sockaddr
*) &unconf
->cl_addr
, sa
))
2108 * section 14.2.34 of RFC 3530 has a description of
2109 * SETCLIENTID_CONFIRM request processing consisting
2110 * of 4 bullet points, labeled as CASE1 - CASE4 below.
2112 if (conf
&& unconf
&& same_verf(&confirm
, &unconf
->cl_confirm
)) {
2114 * RFC 3530 14.2.34 CASE 1:
2117 if (!same_creds(&conf
->cl_cred
, &unconf
->cl_cred
))
2118 status
= nfserr_clid_inuse
;
2120 nfsd4_change_callback(conf
, &unconf
->cl_cb_conn
);
2121 nfsd4_probe_callback(conf
);
2122 expire_client(unconf
);
2126 } else if (conf
&& !unconf
) {
2128 * RFC 3530 14.2.34 CASE 2:
2129 * probable retransmitted request; play it safe and
2132 if (!same_creds(&conf
->cl_cred
, &rqstp
->rq_cred
))
2133 status
= nfserr_clid_inuse
;
2136 } else if (!conf
&& unconf
2137 && same_verf(&unconf
->cl_confirm
, &confirm
)) {
2139 * RFC 3530 14.2.34 CASE 3:
2140 * Normal case; new or rebooted client:
2142 if (!same_creds(&unconf
->cl_cred
, &rqstp
->rq_cred
)) {
2143 status
= nfserr_clid_inuse
;
2146 clientstr_hashval(unconf
->cl_recdir
);
2147 conf
= find_confirmed_client_by_str(unconf
->cl_recdir
,
2150 nfsd4_remove_clid_dir(conf
);
2151 expire_client(conf
);
2153 move_to_confirmed(unconf
);
2155 nfsd4_probe_callback(conf
);
2158 } else if ((!conf
|| (conf
&& !same_verf(&conf
->cl_confirm
, &confirm
)))
2159 && (!unconf
|| (unconf
&& !same_verf(&unconf
->cl_confirm
,
2162 * RFC 3530 14.2.34 CASE 4:
2163 * Client probably hasn't noticed that we rebooted yet.
2165 status
= nfserr_stale_clientid
;
2167 /* check that we have hit one of the cases...*/
2168 status
= nfserr_clid_inuse
;
2171 nfs4_unlock_state();
2175 /* OPEN Share state helper functions */
2176 static inline struct nfs4_file
*
2177 alloc_init_file(struct inode
*ino
)
2179 struct nfs4_file
*fp
;
2180 unsigned int hashval
= file_hashval(ino
);
2182 fp
= kmem_cache_alloc(file_slab
, GFP_KERNEL
);
2184 atomic_set(&fp
->fi_ref
, 1);
2185 INIT_LIST_HEAD(&fp
->fi_hash
);
2186 INIT_LIST_HEAD(&fp
->fi_stateids
);
2187 INIT_LIST_HEAD(&fp
->fi_delegations
);
2188 fp
->fi_inode
= igrab(ino
);
2189 fp
->fi_id
= current_fileid
++;
2190 fp
->fi_had_conflict
= false;
2191 fp
->fi_lease
= NULL
;
2192 memset(fp
->fi_fds
, 0, sizeof(fp
->fi_fds
));
2193 memset(fp
->fi_access
, 0, sizeof(fp
->fi_access
));
2194 spin_lock(&recall_lock
);
2195 list_add(&fp
->fi_hash
, &file_hashtbl
[hashval
]);
2196 spin_unlock(&recall_lock
);
2203 nfsd4_free_slab(struct kmem_cache
**slab
)
2207 kmem_cache_destroy(*slab
);
2212 nfsd4_free_slabs(void)
2214 nfsd4_free_slab(&openowner_slab
);
2215 nfsd4_free_slab(&lockowner_slab
);
2216 nfsd4_free_slab(&file_slab
);
2217 nfsd4_free_slab(&stateid_slab
);
2218 nfsd4_free_slab(&deleg_slab
);
2222 nfsd4_init_slabs(void)
2224 openowner_slab
= kmem_cache_create("nfsd4_openowners",
2225 sizeof(struct nfs4_openowner
), 0, 0, NULL
);
2226 if (openowner_slab
== NULL
)
2228 lockowner_slab
= kmem_cache_create("nfsd4_lockowners",
2229 sizeof(struct nfs4_openowner
), 0, 0, NULL
);
2230 if (lockowner_slab
== NULL
)
2232 file_slab
= kmem_cache_create("nfsd4_files",
2233 sizeof(struct nfs4_file
), 0, 0, NULL
);
2234 if (file_slab
== NULL
)
2236 stateid_slab
= kmem_cache_create("nfsd4_stateids",
2237 sizeof(struct nfs4_stateid
), 0, 0, NULL
);
2238 if (stateid_slab
== NULL
)
2240 deleg_slab
= kmem_cache_create("nfsd4_delegations",
2241 sizeof(struct nfs4_delegation
), 0, 0, NULL
);
2242 if (deleg_slab
== NULL
)
2247 dprintk("nfsd4: out of memory while initializing nfsv4\n");
2251 void nfs4_free_openowner(struct nfs4_openowner
*oo
)
2253 kfree(oo
->oo_owner
.so_owner
.data
);
2254 kmem_cache_free(openowner_slab
, oo
);
2257 void nfs4_free_lockowner(struct nfs4_lockowner
*lo
)
2259 kfree(lo
->lo_owner
.so_owner
.data
);
2260 kmem_cache_free(lockowner_slab
, lo
);
2263 static void init_nfs4_replay(struct nfs4_replay
*rp
)
2265 rp
->rp_status
= nfserr_serverfault
;
2267 rp
->rp_buf
= rp
->rp_ibuf
;
2270 static inline void *alloc_stateowner(struct kmem_cache
*slab
, struct xdr_netobj
*owner
, struct nfs4_client
*clp
)
2272 struct nfs4_stateowner
*sop
;
2274 sop
= kmem_cache_alloc(slab
, GFP_KERNEL
);
2278 sop
->so_owner
.data
= kmemdup(owner
->data
, owner
->len
, GFP_KERNEL
);
2279 if (!sop
->so_owner
.data
) {
2280 kmem_cache_free(slab
, sop
);
2283 sop
->so_owner
.len
= owner
->len
;
2285 INIT_LIST_HEAD(&sop
->so_stateids
);
2286 sop
->so_id
= current_ownerid
++;
2287 sop
->so_client
= clp
;
2288 init_nfs4_replay(&sop
->so_replay
);
2292 static void hash_openowner(struct nfs4_openowner
*oo
, struct nfs4_client
*clp
, unsigned int strhashval
)
2294 unsigned int idhashval
;
2296 idhashval
= open_ownerid_hashval(oo
->oo_owner
.so_id
);
2297 list_add(&oo
->oo_owner
.so_idhash
, &open_ownerid_hashtbl
[idhashval
]);
2298 list_add(&oo
->oo_owner
.so_strhash
, &open_ownerstr_hashtbl
[strhashval
]);
2299 list_add(&oo
->oo_perclient
, &clp
->cl_openowners
);
2302 static struct nfs4_openowner
*
2303 alloc_init_open_stateowner(unsigned int strhashval
, struct nfs4_client
*clp
, struct nfsd4_open
*open
) {
2304 struct nfs4_openowner
*oo
;
2306 oo
= alloc_stateowner(openowner_slab
, &open
->op_owner
, clp
);
2309 oo
->oo_owner
.so_is_open_owner
= 1;
2310 oo
->oo_owner
.so_seqid
= open
->op_seqid
;
2311 oo
->oo_confirmed
= 0;
2313 INIT_LIST_HEAD(&oo
->oo_close_lru
);
2314 hash_openowner(oo
, clp
, strhashval
);
2319 init_stateid(struct nfs4_stateid
*stp
, struct nfs4_file
*fp
, struct nfsd4_open
*open
) {
2320 struct nfs4_openowner
*oo
= open
->op_openowner
;
2321 unsigned int hashval
= stateid_hashval(oo
->oo_owner
.so_id
, fp
->fi_id
);
2323 INIT_LIST_HEAD(&stp
->st_hash
);
2324 INIT_LIST_HEAD(&stp
->st_perstateowner
);
2325 INIT_LIST_HEAD(&stp
->st_lockowners
);
2326 INIT_LIST_HEAD(&stp
->st_perfile
);
2327 list_add(&stp
->st_hash
, &stateid_hashtbl
[hashval
]);
2328 list_add(&stp
->st_perstateowner
, &oo
->oo_owner
.so_stateids
);
2329 list_add(&stp
->st_perfile
, &fp
->fi_stateids
);
2330 stp
->st_type
= NFS4_OPEN_STID
;
2331 stp
->st_stateowner
= &oo
->oo_owner
;
2334 stp
->st_stateid
.si_boot
= boot_time
;
2335 stp
->st_stateid
.si_stateownerid
= oo
->oo_owner
.so_id
;
2336 stp
->st_stateid
.si_fileid
= fp
->fi_id
;
2337 /* note will be incremented before first return to client: */
2338 stp
->st_stateid
.si_generation
= 0;
2339 stp
->st_access_bmap
= 0;
2340 stp
->st_deny_bmap
= 0;
2341 __set_bit(open
->op_share_access
& ~NFS4_SHARE_WANT_MASK
,
2342 &stp
->st_access_bmap
);
2343 __set_bit(open
->op_share_deny
, &stp
->st_deny_bmap
);
2344 stp
->st_openstp
= NULL
;
2348 move_to_close_lru(struct nfs4_openowner
*oo
)
2350 dprintk("NFSD: move_to_close_lru nfs4_openowner %p\n", oo
);
2352 list_move_tail(&oo
->oo_close_lru
, &close_lru
);
2353 oo
->oo_time
= get_seconds();
2357 same_owner_str(struct nfs4_stateowner
*sop
, struct xdr_netobj
*owner
,
2360 return (sop
->so_owner
.len
== owner
->len
) &&
2361 0 == memcmp(sop
->so_owner
.data
, owner
->data
, owner
->len
) &&
2362 (sop
->so_client
->cl_clientid
.cl_id
== clid
->cl_id
);
2365 static struct nfs4_openowner
*
2366 find_openstateowner_str(unsigned int hashval
, struct nfsd4_open
*open
)
2368 struct nfs4_stateowner
*so
= NULL
;
2370 list_for_each_entry(so
, &open_ownerstr_hashtbl
[hashval
], so_strhash
) {
2371 if (same_owner_str(so
, &open
->op_owner
, &open
->op_clientid
))
2372 return container_of(so
, struct nfs4_openowner
, oo_owner
);
2377 /* search file_hashtbl[] for file */
2378 static struct nfs4_file
*
2379 find_file(struct inode
*ino
)
2381 unsigned int hashval
= file_hashval(ino
);
2382 struct nfs4_file
*fp
;
2384 spin_lock(&recall_lock
);
2385 list_for_each_entry(fp
, &file_hashtbl
[hashval
], fi_hash
) {
2386 if (fp
->fi_inode
== ino
) {
2388 spin_unlock(&recall_lock
);
2392 spin_unlock(&recall_lock
);
2396 static inline int access_valid(u32 x
, u32 minorversion
)
2398 if ((x
& NFS4_SHARE_ACCESS_MASK
) < NFS4_SHARE_ACCESS_READ
)
2400 if ((x
& NFS4_SHARE_ACCESS_MASK
) > NFS4_SHARE_ACCESS_BOTH
)
2402 x
&= ~NFS4_SHARE_ACCESS_MASK
;
2403 if (minorversion
&& x
) {
2404 if ((x
& NFS4_SHARE_WANT_MASK
) > NFS4_SHARE_WANT_CANCEL
)
2406 if ((x
& NFS4_SHARE_WHEN_MASK
) > NFS4_SHARE_PUSH_DELEG_WHEN_UNCONTENDED
)
2408 x
&= ~(NFS4_SHARE_WANT_MASK
| NFS4_SHARE_WHEN_MASK
);
2415 static inline int deny_valid(u32 x
)
2417 /* Note: unlike access bits, deny bits may be zero. */
2418 return x
<= NFS4_SHARE_DENY_BOTH
;
2422 * Called to check deny when READ with all zero stateid or
2423 * WRITE with all zero or all one stateid
2426 nfs4_share_conflict(struct svc_fh
*current_fh
, unsigned int deny_type
)
2428 struct inode
*ino
= current_fh
->fh_dentry
->d_inode
;
2429 struct nfs4_file
*fp
;
2430 struct nfs4_stateid
*stp
;
2433 dprintk("NFSD: nfs4_share_conflict\n");
2435 fp
= find_file(ino
);
2438 ret
= nfserr_locked
;
2439 /* Search for conflicting share reservations */
2440 list_for_each_entry(stp
, &fp
->fi_stateids
, st_perfile
) {
2441 if (test_bit(deny_type
, &stp
->st_deny_bmap
) ||
2442 test_bit(NFS4_SHARE_DENY_BOTH
, &stp
->st_deny_bmap
))
2451 static void nfsd_break_one_deleg(struct nfs4_delegation
*dp
)
2453 /* We're assuming the state code never drops its reference
2454 * without first removing the lease. Since we're in this lease
2455 * callback (and since the lease code is serialized by the kernel
2456 * lock) we know the server hasn't removed the lease yet, we know
2457 * it's safe to take a reference: */
2458 atomic_inc(&dp
->dl_count
);
2460 list_add_tail(&dp
->dl_recall_lru
, &del_recall_lru
);
2462 /* only place dl_time is set. protected by lock_flocks*/
2463 dp
->dl_time
= get_seconds();
2465 nfsd4_cb_recall(dp
);
2468 /* Called from break_lease() with lock_flocks() held. */
2469 static void nfsd_break_deleg_cb(struct file_lock
*fl
)
2471 struct nfs4_file
*fp
= (struct nfs4_file
*)fl
->fl_owner
;
2472 struct nfs4_delegation
*dp
;
2475 /* We assume break_lease is only called once per lease: */
2476 BUG_ON(fp
->fi_had_conflict
);
2478 * We don't want the locks code to timeout the lease for us;
2479 * we'll remove it ourself if a delegation isn't returned
2482 fl
->fl_break_time
= 0;
2484 spin_lock(&recall_lock
);
2485 fp
->fi_had_conflict
= true;
2486 list_for_each_entry(dp
, &fp
->fi_delegations
, dl_perfile
)
2487 nfsd_break_one_deleg(dp
);
2488 spin_unlock(&recall_lock
);
2492 int nfsd_change_deleg_cb(struct file_lock
**onlist
, int arg
)
2495 return lease_modify(onlist
, arg
);
2500 static const struct lock_manager_operations nfsd_lease_mng_ops
= {
2501 .lm_break
= nfsd_break_deleg_cb
,
2502 .lm_change
= nfsd_change_deleg_cb
,
2505 static __be32
nfsd4_check_seqid(struct nfsd4_compound_state
*cstate
, struct nfs4_stateowner
*so
, u32 seqid
)
2507 if (nfsd4_has_session(cstate
))
2509 if (seqid
== so
->so_seqid
- 1)
2510 return nfserr_replay_me
;
2511 if (seqid
== so
->so_seqid
)
2513 return nfserr_bad_seqid
;
2517 nfsd4_process_open1(struct nfsd4_compound_state
*cstate
,
2518 struct nfsd4_open
*open
)
2520 clientid_t
*clientid
= &open
->op_clientid
;
2521 struct nfs4_client
*clp
= NULL
;
2522 unsigned int strhashval
;
2523 struct nfs4_openowner
*oo
= NULL
;
2526 if (!check_name(open
->op_owner
))
2527 return nfserr_inval
;
2529 if (STALE_CLIENTID(&open
->op_clientid
))
2530 return nfserr_stale_clientid
;
2532 strhashval
= open_ownerstr_hashval(clientid
->cl_id
, &open
->op_owner
);
2533 oo
= find_openstateowner_str(strhashval
, open
);
2534 open
->op_openowner
= oo
;
2536 /* Make sure the client's lease hasn't expired. */
2537 clp
= find_confirmed_client(clientid
);
2539 return nfserr_expired
;
2542 if (!oo
->oo_confirmed
) {
2543 /* Replace unconfirmed owners without checking for replay. */
2544 clp
= oo
->oo_owner
.so_client
;
2545 release_openowner(oo
);
2546 open
->op_openowner
= NULL
;
2549 status
= nfsd4_check_seqid(cstate
, &oo
->oo_owner
, open
->op_seqid
);
2553 if (open
->op_openowner
== NULL
) {
2554 oo
= alloc_init_open_stateowner(strhashval
, clp
, open
);
2556 return nfserr_jukebox
;
2557 open
->op_openowner
= oo
;
2559 list_del_init(&oo
->oo_close_lru
);
2560 renew_client(oo
->oo_owner
.so_client
);
2564 static inline __be32
2565 nfs4_check_delegmode(struct nfs4_delegation
*dp
, int flags
)
2567 if ((flags
& WR_STATE
) && (dp
->dl_type
== NFS4_OPEN_DELEGATE_READ
))
2568 return nfserr_openmode
;
2573 static struct nfs4_delegation
*
2574 find_delegation_file(struct nfs4_file
*fp
, stateid_t
*stid
)
2576 struct nfs4_delegation
*dp
;
2578 spin_lock(&recall_lock
);
2579 list_for_each_entry(dp
, &fp
->fi_delegations
, dl_perfile
)
2580 if (dp
->dl_stateid
.si_stateownerid
== stid
->si_stateownerid
) {
2581 spin_unlock(&recall_lock
);
2584 spin_unlock(&recall_lock
);
2588 static int share_access_to_flags(u32 share_access
)
2590 share_access
&= ~NFS4_SHARE_WANT_MASK
;
2592 return share_access
== NFS4_SHARE_ACCESS_READ
? RD_STATE
: WR_STATE
;
2596 nfs4_check_deleg(struct nfs4_file
*fp
, struct nfsd4_open
*open
,
2597 struct nfs4_delegation
**dp
)
2600 __be32 status
= nfserr_bad_stateid
;
2602 *dp
= find_delegation_file(fp
, &open
->op_delegate_stateid
);
2605 flags
= share_access_to_flags(open
->op_share_access
);
2606 status
= nfs4_check_delegmode(*dp
, flags
);
2610 if (open
->op_claim_type
!= NFS4_OPEN_CLAIM_DELEGATE_CUR
)
2614 open
->op_openowner
->oo_confirmed
= 1;
2619 nfs4_check_open(struct nfs4_file
*fp
, struct nfsd4_open
*open
, struct nfs4_stateid
**stpp
)
2621 struct nfs4_stateid
*local
;
2622 struct nfs4_openowner
*oo
= open
->op_openowner
;
2624 list_for_each_entry(local
, &fp
->fi_stateids
, st_perfile
) {
2625 /* ignore lock owners */
2626 if (local
->st_stateowner
->so_is_open_owner
== 0)
2628 /* remember if we have seen this open owner */
2629 if (local
->st_stateowner
== &oo
->oo_owner
)
2631 /* check for conflicting share reservations */
2632 if (!test_share(local
, open
))
2633 return nfserr_share_denied
;
2638 static inline struct nfs4_stateid
*
2639 nfs4_alloc_stateid(void)
2641 return kmem_cache_alloc(stateid_slab
, GFP_KERNEL
);
2644 static inline int nfs4_access_to_access(u32 nfs4_access
)
2648 if (nfs4_access
& NFS4_SHARE_ACCESS_READ
)
2649 flags
|= NFSD_MAY_READ
;
2650 if (nfs4_access
& NFS4_SHARE_ACCESS_WRITE
)
2651 flags
|= NFSD_MAY_WRITE
;
2655 static __be32
nfs4_get_vfs_file(struct svc_rqst
*rqstp
, struct nfs4_file
*fp
,
2656 struct svc_fh
*cur_fh
, struct nfsd4_open
*open
)
2659 int oflag
= nfs4_access_to_omode(open
->op_share_access
);
2660 int access
= nfs4_access_to_access(open
->op_share_access
);
2662 /* CLAIM_DELEGATE_CUR is used in response to a broken lease;
2663 * allowing it to break the lease and return EAGAIN leaves the
2664 * client unable to make progress in returning the delegation */
2665 if (open
->op_claim_type
== NFS4_OPEN_CLAIM_DELEGATE_CUR
)
2666 access
|= NFSD_MAY_NOT_BREAK_LEASE
;
2668 if (!fp
->fi_fds
[oflag
]) {
2669 status
= nfsd_open(rqstp
, cur_fh
, S_IFREG
, access
,
2670 &fp
->fi_fds
[oflag
]);
2674 nfs4_file_get_access(fp
, oflag
);
2680 nfs4_new_open(struct svc_rqst
*rqstp
, struct nfs4_stateid
**stpp
,
2681 struct nfs4_file
*fp
, struct svc_fh
*cur_fh
,
2682 struct nfsd4_open
*open
)
2684 struct nfs4_stateid
*stp
;
2687 stp
= nfs4_alloc_stateid();
2689 return nfserr_jukebox
;
2691 status
= nfs4_get_vfs_file(rqstp
, fp
, cur_fh
, open
);
2693 kmem_cache_free(stateid_slab
, stp
);
2700 static inline __be32
2701 nfsd4_truncate(struct svc_rqst
*rqstp
, struct svc_fh
*fh
,
2702 struct nfsd4_open
*open
)
2704 struct iattr iattr
= {
2705 .ia_valid
= ATTR_SIZE
,
2708 if (!open
->op_truncate
)
2710 if (!(open
->op_share_access
& NFS4_SHARE_ACCESS_WRITE
))
2711 return nfserr_inval
;
2712 return nfsd_setattr(rqstp
, fh
, &iattr
, 0, (time_t)0);
2716 nfs4_upgrade_open(struct svc_rqst
*rqstp
, struct nfs4_file
*fp
, struct svc_fh
*cur_fh
, struct nfs4_stateid
*stp
, struct nfsd4_open
*open
)
2718 u32 op_share_access
= open
->op_share_access
& ~NFS4_SHARE_WANT_MASK
;
2722 new_access
= !test_bit(op_share_access
, &stp
->st_access_bmap
);
2724 status
= nfs4_get_vfs_file(rqstp
, fp
, cur_fh
, open
);
2728 status
= nfsd4_truncate(rqstp
, cur_fh
, open
);
2731 int oflag
= nfs4_access_to_omode(op_share_access
);
2732 nfs4_file_put_access(fp
, oflag
);
2736 /* remember the open */
2737 __set_bit(op_share_access
, &stp
->st_access_bmap
);
2738 __set_bit(open
->op_share_deny
, &stp
->st_deny_bmap
);
2745 nfs4_set_claim_prev(struct nfsd4_open
*open
)
2747 open
->op_openowner
->oo_confirmed
= 1;
2748 open
->op_openowner
->oo_owner
.so_client
->cl_firststate
= 1;
2751 /* Should we give out recallable state?: */
2752 static bool nfsd4_cb_channel_good(struct nfs4_client
*clp
)
2754 if (clp
->cl_cb_state
== NFSD4_CB_UP
)
2757 * In the sessions case, since we don't have to establish a
2758 * separate connection for callbacks, we assume it's OK
2759 * until we hear otherwise:
2761 return clp
->cl_minorversion
&& clp
->cl_cb_state
== NFSD4_CB_UNKNOWN
;
2764 static struct file_lock
*nfs4_alloc_init_lease(struct nfs4_delegation
*dp
, int flag
)
2766 struct file_lock
*fl
;
2768 fl
= locks_alloc_lock();
2771 locks_init_lock(fl
);
2772 fl
->fl_lmops
= &nfsd_lease_mng_ops
;
2773 fl
->fl_flags
= FL_LEASE
;
2774 fl
->fl_type
= flag
== NFS4_OPEN_DELEGATE_READ
? F_RDLCK
: F_WRLCK
;
2775 fl
->fl_end
= OFFSET_MAX
;
2776 fl
->fl_owner
= (fl_owner_t
)(dp
->dl_file
);
2777 fl
->fl_pid
= current
->tgid
;
2781 static int nfs4_setlease(struct nfs4_delegation
*dp
, int flag
)
2783 struct nfs4_file
*fp
= dp
->dl_file
;
2784 struct file_lock
*fl
;
2787 fl
= nfs4_alloc_init_lease(dp
, flag
);
2790 fl
->fl_file
= find_readable_file(fp
);
2791 list_add(&dp
->dl_perclnt
, &dp
->dl_client
->cl_delegations
);
2792 status
= vfs_setlease(fl
->fl_file
, fl
->fl_type
, &fl
);
2794 list_del_init(&dp
->dl_perclnt
);
2795 locks_free_lock(fl
);
2799 fp
->fi_deleg_file
= fl
->fl_file
;
2800 get_file(fp
->fi_deleg_file
);
2801 atomic_set(&fp
->fi_delegees
, 1);
2802 list_add(&dp
->dl_perfile
, &fp
->fi_delegations
);
2806 static int nfs4_set_delegation(struct nfs4_delegation
*dp
, int flag
)
2808 struct nfs4_file
*fp
= dp
->dl_file
;
2811 return nfs4_setlease(dp
, flag
);
2812 spin_lock(&recall_lock
);
2813 if (fp
->fi_had_conflict
) {
2814 spin_unlock(&recall_lock
);
2817 atomic_inc(&fp
->fi_delegees
);
2818 list_add(&dp
->dl_perfile
, &fp
->fi_delegations
);
2819 spin_unlock(&recall_lock
);
2820 list_add(&dp
->dl_perclnt
, &dp
->dl_client
->cl_delegations
);
2825 * Attempt to hand out a delegation.
2828 nfs4_open_delegation(struct svc_fh
*fh
, struct nfsd4_open
*open
, struct nfs4_stateid
*stp
)
2830 struct nfs4_delegation
*dp
;
2831 struct nfs4_openowner
*oo
= container_of(stp
->st_stateowner
, struct nfs4_openowner
, oo_owner
);
2833 int status
, flag
= 0;
2835 cb_up
= nfsd4_cb_channel_good(oo
->oo_owner
.so_client
);
2836 flag
= NFS4_OPEN_DELEGATE_NONE
;
2837 open
->op_recall
= 0;
2838 switch (open
->op_claim_type
) {
2839 case NFS4_OPEN_CLAIM_PREVIOUS
:
2841 open
->op_recall
= 1;
2842 flag
= open
->op_delegate_type
;
2843 if (flag
== NFS4_OPEN_DELEGATE_NONE
)
2846 case NFS4_OPEN_CLAIM_NULL
:
2847 /* Let's not give out any delegations till everyone's
2848 * had the chance to reclaim theirs.... */
2849 if (locks_in_grace())
2851 if (!cb_up
|| !oo
->oo_confirmed
)
2853 if (open
->op_share_access
& NFS4_SHARE_ACCESS_WRITE
)
2854 flag
= NFS4_OPEN_DELEGATE_WRITE
;
2856 flag
= NFS4_OPEN_DELEGATE_READ
;
2862 dp
= alloc_init_deleg(oo
->oo_owner
.so_client
, stp
, fh
, flag
);
2865 status
= nfs4_set_delegation(dp
, flag
);
2869 memcpy(&open
->op_delegate_stateid
, &dp
->dl_stateid
, sizeof(dp
->dl_stateid
));
2871 dprintk("NFSD: delegation stateid=" STATEID_FMT
"\n",
2872 STATEID_VAL(&dp
->dl_stateid
));
2874 if (open
->op_claim_type
== NFS4_OPEN_CLAIM_PREVIOUS
2875 && flag
== NFS4_OPEN_DELEGATE_NONE
2876 && open
->op_delegate_type
!= NFS4_OPEN_DELEGATE_NONE
)
2877 dprintk("NFSD: WARNING: refusing delegation reclaim\n");
2878 open
->op_delegate_type
= flag
;
2881 nfs4_put_delegation(dp
);
2883 flag
= NFS4_OPEN_DELEGATE_NONE
;
2888 * called with nfs4_lock_state() held.
2891 nfsd4_process_open2(struct svc_rqst
*rqstp
, struct svc_fh
*current_fh
, struct nfsd4_open
*open
)
2893 struct nfsd4_compoundres
*resp
= rqstp
->rq_resp
;
2894 struct nfs4_file
*fp
= NULL
;
2895 struct inode
*ino
= current_fh
->fh_dentry
->d_inode
;
2896 struct nfs4_stateid
*stp
= NULL
;
2897 struct nfs4_delegation
*dp
= NULL
;
2900 status
= nfserr_inval
;
2901 if (!access_valid(open
->op_share_access
, resp
->cstate
.minorversion
)
2902 || !deny_valid(open
->op_share_deny
))
2905 * Lookup file; if found, lookup stateid and check open request,
2906 * and check for delegations in the process of being recalled.
2907 * If not found, create the nfs4_file struct
2909 fp
= find_file(ino
);
2911 if ((status
= nfs4_check_open(fp
, open
, &stp
)))
2913 status
= nfs4_check_deleg(fp
, open
, &dp
);
2917 status
= nfserr_bad_stateid
;
2918 if (open
->op_claim_type
== NFS4_OPEN_CLAIM_DELEGATE_CUR
)
2920 status
= nfserr_jukebox
;
2921 fp
= alloc_init_file(ino
);
2927 * OPEN the file, or upgrade an existing OPEN.
2928 * If truncate fails, the OPEN fails.
2931 /* Stateid was found, this is an OPEN upgrade */
2932 status
= nfs4_upgrade_open(rqstp
, fp
, current_fh
, stp
, open
);
2936 status
= nfs4_new_open(rqstp
, &stp
, fp
, current_fh
, open
);
2939 init_stateid(stp
, fp
, open
);
2940 status
= nfsd4_truncate(rqstp
, current_fh
, open
);
2942 release_open_stateid(stp
);
2946 update_stateid(&stp
->st_stateid
);
2947 memcpy(&open
->op_stateid
, &stp
->st_stateid
, sizeof(stateid_t
));
2949 if (nfsd4_has_session(&resp
->cstate
))
2950 open
->op_openowner
->oo_confirmed
= 1;
2953 * Attempt to hand out a delegation. No error return, because the
2954 * OPEN succeeds even if we fail.
2956 nfs4_open_delegation(current_fh
, open
, stp
);
2960 dprintk("%s: stateid=" STATEID_FMT
"\n", __func__
,
2961 STATEID_VAL(&stp
->st_stateid
));
2965 if (status
== 0 && open
->op_claim_type
== NFS4_OPEN_CLAIM_PREVIOUS
)
2966 nfs4_set_claim_prev(open
);
2968 * To finish the open response, we just need to set the rflags.
2970 open
->op_rflags
= NFS4_OPEN_RESULT_LOCKTYPE_POSIX
;
2971 if (!open
->op_openowner
->oo_confirmed
&&
2972 !nfsd4_has_session(&resp
->cstate
))
2973 open
->op_rflags
|= NFS4_OPEN_RESULT_CONFIRM
;
2979 nfsd4_renew(struct svc_rqst
*rqstp
, struct nfsd4_compound_state
*cstate
,
2982 struct nfs4_client
*clp
;
2986 dprintk("process_renew(%08x/%08x): starting\n",
2987 clid
->cl_boot
, clid
->cl_id
);
2988 status
= nfserr_stale_clientid
;
2989 if (STALE_CLIENTID(clid
))
2991 clp
= find_confirmed_client(clid
);
2992 status
= nfserr_expired
;
2994 /* We assume the client took too long to RENEW. */
2995 dprintk("nfsd4_renew: clientid not found!\n");
2999 status
= nfserr_cb_path_down
;
3000 if (!list_empty(&clp
->cl_delegations
)
3001 && clp
->cl_cb_state
!= NFSD4_CB_UP
)
3005 nfs4_unlock_state();
3009 static struct lock_manager nfsd4_manager
= {
3013 nfsd4_end_grace(void)
3015 dprintk("NFSD: end of grace period\n");
3016 nfsd4_recdir_purge_old();
3017 locks_end_grace(&nfsd4_manager
);
3019 * Now that every NFSv4 client has had the chance to recover and
3020 * to see the (possibly new, possibly shorter) lease time, we
3021 * can safely set the next grace time to the current lease time:
3023 nfsd4_grace
= nfsd4_lease
;
3027 nfs4_laundromat(void)
3029 struct nfs4_client
*clp
;
3030 struct nfs4_openowner
*oo
;
3031 struct nfs4_delegation
*dp
;
3032 struct list_head
*pos
, *next
, reaplist
;
3033 time_t cutoff
= get_seconds() - nfsd4_lease
;
3034 time_t t
, clientid_val
= nfsd4_lease
;
3035 time_t u
, test_val
= nfsd4_lease
;
3039 dprintk("NFSD: laundromat service - starting\n");
3040 if (locks_in_grace())
3042 INIT_LIST_HEAD(&reaplist
);
3043 spin_lock(&client_lock
);
3044 list_for_each_safe(pos
, next
, &client_lru
) {
3045 clp
= list_entry(pos
, struct nfs4_client
, cl_lru
);
3046 if (time_after((unsigned long)clp
->cl_time
, (unsigned long)cutoff
)) {
3047 t
= clp
->cl_time
- cutoff
;
3048 if (clientid_val
> t
)
3052 if (atomic_read(&clp
->cl_refcount
)) {
3053 dprintk("NFSD: client in use (clientid %08x)\n",
3054 clp
->cl_clientid
.cl_id
);
3057 unhash_client_locked(clp
);
3058 list_add(&clp
->cl_lru
, &reaplist
);
3060 spin_unlock(&client_lock
);
3061 list_for_each_safe(pos
, next
, &reaplist
) {
3062 clp
= list_entry(pos
, struct nfs4_client
, cl_lru
);
3063 dprintk("NFSD: purging unused client (clientid %08x)\n",
3064 clp
->cl_clientid
.cl_id
);
3065 nfsd4_remove_clid_dir(clp
);
3068 spin_lock(&recall_lock
);
3069 list_for_each_safe(pos
, next
, &del_recall_lru
) {
3070 dp
= list_entry (pos
, struct nfs4_delegation
, dl_recall_lru
);
3071 if (time_after((unsigned long)dp
->dl_time
, (unsigned long)cutoff
)) {
3072 u
= dp
->dl_time
- cutoff
;
3077 list_move(&dp
->dl_recall_lru
, &reaplist
);
3079 spin_unlock(&recall_lock
);
3080 list_for_each_safe(pos
, next
, &reaplist
) {
3081 dp
= list_entry (pos
, struct nfs4_delegation
, dl_recall_lru
);
3082 list_del_init(&dp
->dl_recall_lru
);
3083 unhash_delegation(dp
);
3085 test_val
= nfsd4_lease
;
3086 list_for_each_safe(pos
, next
, &close_lru
) {
3087 oo
= container_of(pos
, struct nfs4_openowner
, oo_close_lru
);
3088 if (time_after((unsigned long)oo
->oo_time
, (unsigned long)cutoff
)) {
3089 u
= oo
->oo_time
- cutoff
;
3094 dprintk("NFSD: purging unused open stateowner (so_id %d)\n",
3095 oo
->oo_owner
.so_id
);
3096 release_openowner(oo
);
3098 if (clientid_val
< NFSD_LAUNDROMAT_MINTIMEOUT
)
3099 clientid_val
= NFSD_LAUNDROMAT_MINTIMEOUT
;
3100 nfs4_unlock_state();
3101 return clientid_val
;
3104 static struct workqueue_struct
*laundry_wq
;
3105 static void laundromat_main(struct work_struct
*);
3106 static DECLARE_DELAYED_WORK(laundromat_work
, laundromat_main
);
3109 laundromat_main(struct work_struct
*not_used
)
3113 t
= nfs4_laundromat();
3114 dprintk("NFSD: laundromat_main - sleeping for %ld seconds\n", t
);
3115 queue_delayed_work(laundry_wq
, &laundromat_work
, t
*HZ
);
3118 static struct nfs4_openowner
* search_close_lru(u32 st_id
)
3120 struct nfs4_openowner
*local
;
3122 list_for_each_entry(local
, &close_lru
, oo_close_lru
) {
3123 if (local
->oo_owner
.so_id
== st_id
)
3130 nfs4_check_fh(struct svc_fh
*fhp
, struct nfs4_stateid
*stp
)
3132 return fhp
->fh_dentry
->d_inode
!= stp
->st_file
->fi_inode
;
3136 STALE_STATEID(stateid_t
*stateid
)
3138 if (stateid
->si_boot
== boot_time
)
3140 dprintk("NFSD: stale stateid " STATEID_FMT
"!\n",
3141 STATEID_VAL(stateid
));
3146 access_permit_read(unsigned long access_bmap
)
3148 return test_bit(NFS4_SHARE_ACCESS_READ
, &access_bmap
) ||
3149 test_bit(NFS4_SHARE_ACCESS_BOTH
, &access_bmap
) ||
3150 test_bit(NFS4_SHARE_ACCESS_WRITE
, &access_bmap
);
3154 access_permit_write(unsigned long access_bmap
)
3156 return test_bit(NFS4_SHARE_ACCESS_WRITE
, &access_bmap
) ||
3157 test_bit(NFS4_SHARE_ACCESS_BOTH
, &access_bmap
);
3161 __be32
nfs4_check_openmode(struct nfs4_stateid
*stp
, int flags
)
3163 __be32 status
= nfserr_openmode
;
3165 /* For lock stateid's, we test the parent open, not the lock: */
3166 if (stp
->st_openstp
)
3167 stp
= stp
->st_openstp
;
3168 if ((flags
& WR_STATE
) && (!access_permit_write(stp
->st_access_bmap
)))
3170 if ((flags
& RD_STATE
) && (!access_permit_read(stp
->st_access_bmap
)))
3177 static inline __be32
3178 check_special_stateids(svc_fh
*current_fh
, stateid_t
*stateid
, int flags
)
3180 if (ONE_STATEID(stateid
) && (flags
& RD_STATE
))
3182 else if (locks_in_grace()) {
3183 /* Answer in remaining cases depends on existence of
3184 * conflicting state; so we must wait out the grace period. */
3185 return nfserr_grace
;
3186 } else if (flags
& WR_STATE
)
3187 return nfs4_share_conflict(current_fh
,
3188 NFS4_SHARE_DENY_WRITE
);
3189 else /* (flags & RD_STATE) && ZERO_STATEID(stateid) */
3190 return nfs4_share_conflict(current_fh
,
3191 NFS4_SHARE_DENY_READ
);
3195 * Allow READ/WRITE during grace period on recovered state only for files
3196 * that are not able to provide mandatory locking.
3199 grace_disallows_io(struct inode
*inode
)
3201 return locks_in_grace() && mandatory_lock(inode
);
3204 /* Returns true iff a is later than b: */
3205 static bool stateid_generation_after(stateid_t
*a
, stateid_t
*b
)
3207 return (s32
)a
->si_generation
- (s32
)b
->si_generation
> 0;
3210 static int check_stateid_generation(stateid_t
*in
, stateid_t
*ref
, bool has_session
)
3213 * When sessions are used the stateid generation number is ignored
3216 if (has_session
&& in
->si_generation
== 0)
3219 if (in
->si_generation
== ref
->si_generation
)
3222 /* If the client sends us a stateid from the future, it's buggy: */
3223 if (stateid_generation_after(in
, ref
))
3224 return nfserr_bad_stateid
;
3226 * However, we could see a stateid from the past, even from a
3227 * non-buggy client. For example, if the client sends a lock
3228 * while some IO is outstanding, the lock may bump si_generation
3229 * while the IO is still in flight. The client could avoid that
3230 * situation by waiting for responses on all the IO requests,
3231 * but better performance may result in retrying IO that
3232 * receives an old_stateid error if requests are rarely
3233 * reordered in flight:
3235 return nfserr_old_stateid
;
3238 static int is_delegation_stateid(stateid_t
*stateid
)
3240 return stateid
->si_fileid
== 0;
3243 __be32
nfs4_validate_stateid(stateid_t
*stateid
, bool has_session
)
3245 struct nfs4_stateid
*stp
= NULL
;
3246 __be32 status
= nfserr_stale_stateid
;
3248 if (STALE_STATEID(stateid
))
3251 status
= nfserr_expired
;
3252 stp
= find_stateid(stateid
);
3255 status
= nfserr_bad_stateid
;
3256 if (stp
->st_stateowner
->so_is_open_owner
3257 && !openowner(stp
->st_stateowner
)->oo_confirmed
)
3260 status
= check_stateid_generation(stateid
, &stp
->st_stateid
, has_session
);
3270 * Checks for stateid operations
3273 nfs4_preprocess_stateid_op(struct nfsd4_compound_state
*cstate
,
3274 stateid_t
*stateid
, int flags
, struct file
**filpp
)
3276 struct nfs4_stateid
*stp
= NULL
;
3277 struct nfs4_delegation
*dp
= NULL
;
3278 struct svc_fh
*current_fh
= &cstate
->current_fh
;
3279 struct inode
*ino
= current_fh
->fh_dentry
->d_inode
;
3285 if (grace_disallows_io(ino
))
3286 return nfserr_grace
;
3288 if (ZERO_STATEID(stateid
) || ONE_STATEID(stateid
))
3289 return check_special_stateids(current_fh
, stateid
, flags
);
3291 status
= nfserr_stale_stateid
;
3292 if (STALE_STATEID(stateid
))
3296 * We assume that any stateid that has the current boot time,
3297 * but that we can't find, is expired:
3299 status
= nfserr_expired
;
3300 if (is_delegation_stateid(stateid
)) {
3301 dp
= find_delegation_stateid(ino
, stateid
);
3304 status
= check_stateid_generation(stateid
, &dp
->dl_stateid
, nfsd4_has_session(cstate
));
3307 status
= nfs4_check_delegmode(dp
, flags
);
3310 renew_client(dp
->dl_client
);
3312 *filpp
= dp
->dl_file
->fi_deleg_file
;
3315 } else { /* open or lock stateid */
3316 stp
= find_stateid(stateid
);
3319 status
= nfserr_bad_stateid
;
3320 if (nfs4_check_fh(current_fh
, stp
))
3322 if (stp
->st_stateowner
->so_is_open_owner
3323 && !openowner(stp
->st_stateowner
)->oo_confirmed
)
3325 status
= check_stateid_generation(stateid
, &stp
->st_stateid
,
3326 nfsd4_has_session(cstate
));
3329 status
= nfs4_check_openmode(stp
, flags
);
3332 renew_client(stp
->st_stateowner
->so_client
);
3334 if (flags
& RD_STATE
)
3335 *filpp
= find_readable_file(stp
->st_file
);
3337 *filpp
= find_writeable_file(stp
->st_file
);
3346 nfsd4_free_delegation_stateid(stateid_t
*stateid
)
3348 struct nfs4_delegation
*dp
= search_for_delegation(stateid
);
3350 return nfserr_locks_held
;
3351 return nfserr_bad_stateid
;
3355 nfsd4_free_lock_stateid(struct nfs4_stateid
*stp
)
3357 if (check_for_locks(stp
->st_file
, lockowner(stp
->st_stateowner
)))
3358 return nfserr_locks_held
;
3359 release_lock_stateid(stp
);
3364 * Test if the stateid is valid
3367 nfsd4_test_stateid(struct svc_rqst
*rqstp
, struct nfsd4_compound_state
*cstate
,
3368 struct nfsd4_test_stateid
*test_stateid
)
3370 test_stateid
->ts_has_session
= nfsd4_has_session(cstate
);
3378 nfsd4_free_stateid(struct svc_rqst
*rqstp
, struct nfsd4_compound_state
*cstate
,
3379 struct nfsd4_free_stateid
*free_stateid
)
3381 stateid_t
*stateid
= &free_stateid
->fr_stateid
;
3382 struct nfs4_stateid
*stp
;
3386 if (is_delegation_stateid(stateid
)) {
3387 ret
= nfsd4_free_delegation_stateid(stateid
);
3391 stp
= find_stateid(stateid
);
3393 ret
= nfserr_bad_stateid
;
3396 ret
= check_stateid_generation(stateid
, &stp
->st_stateid
, 1);
3400 if (stp
->st_type
== NFS4_OPEN_STID
) {
3401 ret
= nfserr_locks_held
;
3404 ret
= nfsd4_free_lock_stateid(stp
);
3409 nfs4_unlock_state();
3416 return (type
== NFS4_READW_LT
|| type
== NFS4_READ_LT
) ?
3417 RD_STATE
: WR_STATE
;
3420 static __be32
nfs4_nospecial_stateid_checks(stateid_t
*stateid
)
3422 if (ZERO_STATEID(stateid
) || ONE_STATEID(stateid
))
3423 return nfserr_bad_stateid
;
3424 if (STALE_STATEID(stateid
))
3425 return nfserr_stale_stateid
;
3429 static __be32
nfs4_seqid_op_checks(struct nfsd4_compound_state
*cstate
, stateid_t
*stateid
, u32 seqid
, struct nfs4_stateid
*stp
)
3431 struct svc_fh
*current_fh
= &cstate
->current_fh
;
3432 struct nfs4_stateowner
*sop
= stp
->st_stateowner
;
3435 if (nfs4_check_fh(current_fh
, stp
))
3436 return nfserr_bad_stateid
;
3437 status
= nfsd4_check_seqid(cstate
, sop
, seqid
);
3440 return check_stateid_generation(stateid
, &stp
->st_stateid
, nfsd4_has_session(cstate
));
3444 * Checks for sequence id mutating operations.
3447 nfs4_preprocess_seqid_op(struct nfsd4_compound_state
*cstate
, u32 seqid
,
3448 stateid_t
*stateid
, int flags
,
3449 struct nfs4_stateid
**stpp
)
3453 dprintk("NFSD: %s: seqid=%d stateid = " STATEID_FMT
"\n", __func__
,
3454 seqid
, STATEID_VAL(stateid
));
3457 status
= nfs4_nospecial_stateid_checks(stateid
);
3460 *stpp
= find_stateid_by_type(stateid
, flags
);
3462 return nfserr_expired
;
3463 cstate
->replay_owner
= (*stpp
)->st_stateowner
;
3464 renew_client((*stpp
)->st_stateowner
->so_client
);
3466 return nfs4_seqid_op_checks(cstate
, stateid
, seqid
, *stpp
);
3469 static __be32
nfs4_preprocess_confirmed_seqid_op(struct nfsd4_compound_state
*cstate
, u32 seqid
, stateid_t
*stateid
, struct nfs4_stateid
**stpp
)
3472 struct nfs4_openowner
*oo
;
3474 status
= nfs4_preprocess_seqid_op(cstate
, seqid
, stateid
,
3478 oo
= openowner((*stpp
)->st_stateowner
);
3479 if (!oo
->oo_confirmed
)
3480 return nfserr_bad_stateid
;
3485 nfsd4_open_confirm(struct svc_rqst
*rqstp
, struct nfsd4_compound_state
*cstate
,
3486 struct nfsd4_open_confirm
*oc
)
3489 struct nfs4_openowner
*oo
;
3490 struct nfs4_stateid
*stp
;
3492 dprintk("NFSD: nfsd4_open_confirm on file %.*s\n",
3493 (int)cstate
->current_fh
.fh_dentry
->d_name
.len
,
3494 cstate
->current_fh
.fh_dentry
->d_name
.name
);
3496 status
= fh_verify(rqstp
, &cstate
->current_fh
, S_IFREG
, 0);
3502 status
= nfs4_preprocess_seqid_op(cstate
,
3503 oc
->oc_seqid
, &oc
->oc_req_stateid
,
3507 oo
= openowner(stp
->st_stateowner
);
3508 status
= nfserr_bad_stateid
;
3509 if (oo
->oo_confirmed
)
3511 oo
->oo_confirmed
= 1;
3512 update_stateid(&stp
->st_stateid
);
3513 memcpy(&oc
->oc_resp_stateid
, &stp
->st_stateid
, sizeof(stateid_t
));
3514 dprintk("NFSD: %s: success, seqid=%d stateid=" STATEID_FMT
"\n",
3515 __func__
, oc
->oc_seqid
, STATEID_VAL(&stp
->st_stateid
));
3517 nfsd4_create_clid_dir(oo
->oo_owner
.so_client
);
3520 if (!cstate
->replay_owner
)
3521 nfs4_unlock_state();
3525 static inline void nfs4_file_downgrade(struct nfs4_stateid
*stp
, unsigned int to_access
)
3529 for (i
= 1; i
< 4; i
++) {
3530 if (test_bit(i
, &stp
->st_access_bmap
) && !(i
& to_access
)) {
3531 nfs4_file_put_access(stp
->st_file
, i
);
3532 __clear_bit(i
, &stp
->st_access_bmap
);
3538 reset_union_bmap_deny(unsigned long deny
, unsigned long *bmap
)
3541 for (i
= 0; i
< 4; i
++) {
3542 if ((i
& deny
) != i
)
3543 __clear_bit(i
, bmap
);
3548 nfsd4_open_downgrade(struct svc_rqst
*rqstp
,
3549 struct nfsd4_compound_state
*cstate
,
3550 struct nfsd4_open_downgrade
*od
)
3553 struct nfs4_stateid
*stp
;
3555 dprintk("NFSD: nfsd4_open_downgrade on file %.*s\n",
3556 (int)cstate
->current_fh
.fh_dentry
->d_name
.len
,
3557 cstate
->current_fh
.fh_dentry
->d_name
.name
);
3559 if (!access_valid(od
->od_share_access
, cstate
->minorversion
)
3560 || !deny_valid(od
->od_share_deny
))
3561 return nfserr_inval
;
3564 status
= nfs4_preprocess_confirmed_seqid_op(cstate
, od
->od_seqid
,
3565 &od
->od_stateid
, &stp
);
3568 status
= nfserr_inval
;
3569 if (!test_bit(od
->od_share_access
, &stp
->st_access_bmap
)) {
3570 dprintk("NFSD:access not a subset current bitmap: 0x%lx, input access=%08x\n",
3571 stp
->st_access_bmap
, od
->od_share_access
);
3574 if (!test_bit(od
->od_share_deny
, &stp
->st_deny_bmap
)) {
3575 dprintk("NFSD:deny not a subset current bitmap: 0x%lx, input deny=%08x\n",
3576 stp
->st_deny_bmap
, od
->od_share_deny
);
3579 nfs4_file_downgrade(stp
, od
->od_share_access
);
3581 reset_union_bmap_deny(od
->od_share_deny
, &stp
->st_deny_bmap
);
3583 update_stateid(&stp
->st_stateid
);
3584 memcpy(&od
->od_stateid
, &stp
->st_stateid
, sizeof(stateid_t
));
3587 if (!cstate
->replay_owner
)
3588 nfs4_unlock_state();
3593 * nfs4_unlock_state() called after encode
3596 nfsd4_close(struct svc_rqst
*rqstp
, struct nfsd4_compound_state
*cstate
,
3597 struct nfsd4_close
*close
)
3600 struct nfs4_openowner
*oo
;
3601 struct nfs4_stateid
*stp
;
3603 dprintk("NFSD: nfsd4_close on file %.*s\n",
3604 (int)cstate
->current_fh
.fh_dentry
->d_name
.len
,
3605 cstate
->current_fh
.fh_dentry
->d_name
.name
);
3608 /* check close_lru for replay */
3609 status
= nfs4_preprocess_confirmed_seqid_op(cstate
, close
->cl_seqid
,
3610 &close
->cl_stateid
, &stp
);
3611 if (stp
== NULL
&& status
== nfserr_expired
) {
3613 * Also, we should make sure this isn't just the result of
3616 oo
= search_close_lru(close
->cl_stateid
.si_stateownerid
);
3617 /* It's not stale; let's assume it's expired: */
3620 cstate
->replay_owner
= &oo
->oo_owner
;
3621 status
= nfsd4_check_seqid(cstate
, &oo
->oo_owner
, close
->cl_seqid
);
3624 status
= nfserr_bad_seqid
;
3628 oo
= openowner(stp
->st_stateowner
);
3630 update_stateid(&stp
->st_stateid
);
3631 memcpy(&close
->cl_stateid
, &stp
->st_stateid
, sizeof(stateid_t
));
3633 /* release_stateid() calls nfsd_close() if needed */
3634 release_open_stateid(stp
);
3636 /* place unused nfs4_stateowners on so_close_lru list to be
3637 * released by the laundromat service after the lease period
3638 * to enable us to handle CLOSE replay
3640 if (list_empty(&oo
->oo_owner
.so_stateids
))
3641 move_to_close_lru(oo
);
3643 if (!cstate
->replay_owner
)
3644 nfs4_unlock_state();
3649 nfsd4_delegreturn(struct svc_rqst
*rqstp
, struct nfsd4_compound_state
*cstate
,
3650 struct nfsd4_delegreturn
*dr
)
3652 struct nfs4_delegation
*dp
;
3653 stateid_t
*stateid
= &dr
->dr_stateid
;
3654 struct inode
*inode
;
3657 if ((status
= fh_verify(rqstp
, &cstate
->current_fh
, S_IFREG
, 0)))
3659 inode
= cstate
->current_fh
.fh_dentry
->d_inode
;
3662 status
= nfserr_bad_stateid
;
3663 if (ZERO_STATEID(stateid
) || ONE_STATEID(stateid
))
3665 status
= nfserr_stale_stateid
;
3666 if (STALE_STATEID(stateid
))
3668 status
= nfserr_bad_stateid
;
3669 if (!is_delegation_stateid(stateid
))
3671 status
= nfserr_expired
;
3672 dp
= find_delegation_stateid(inode
, stateid
);
3675 status
= check_stateid_generation(stateid
, &dp
->dl_stateid
, nfsd4_has_session(cstate
));
3678 renew_client(dp
->dl_client
);
3680 unhash_delegation(dp
);
3682 nfs4_unlock_state();
3689 * Lock owner state (byte-range locks)
3691 #define LOFF_OVERFLOW(start, len) ((u64)(len) > ~(u64)(start))
3692 #define LOCK_HASH_BITS 8
3693 #define LOCK_HASH_SIZE (1 << LOCK_HASH_BITS)
3694 #define LOCK_HASH_MASK (LOCK_HASH_SIZE - 1)
3697 end_offset(u64 start
, u64 len
)
3702 return end
>= start
? end
: NFS4_MAX_UINT64
;
3705 /* last octet in a range */
3707 last_byte_offset(u64 start
, u64 len
)
3713 return end
> start
? end
- 1: NFS4_MAX_UINT64
;
3716 static unsigned int lockownerid_hashval(u32 id
)
3718 return id
& LOCK_HASH_MASK
;
3721 static inline unsigned int
3722 lock_ownerstr_hashval(struct inode
*inode
, u32 cl_id
,
3723 struct xdr_netobj
*ownername
)
3725 return (file_hashval(inode
) + cl_id
3726 + opaque_hashval(ownername
->data
, ownername
->len
))
3730 static struct list_head lock_ownerid_hashtbl
[LOCK_HASH_SIZE
];
3731 static struct list_head lock_ownerstr_hashtbl
[LOCK_HASH_SIZE
];
3733 static struct nfs4_delegation
*
3734 search_for_delegation(stateid_t
*stid
)
3736 struct nfs4_file
*fp
;
3737 struct nfs4_delegation
*dp
;
3738 struct list_head
*pos
;
3741 for (i
= 0; i
< FILE_HASH_SIZE
; i
++) {
3742 list_for_each_entry(fp
, &file_hashtbl
[i
], fi_hash
) {
3743 list_for_each(pos
, &fp
->fi_delegations
) {
3744 dp
= list_entry(pos
, struct nfs4_delegation
, dl_perfile
);
3745 if (same_stateid(&dp
->dl_stateid
, stid
))
3753 static struct nfs4_delegation
*
3754 find_delegation_stateid(struct inode
*ino
, stateid_t
*stid
)
3756 struct nfs4_file
*fp
;
3757 struct nfs4_delegation
*dl
;
3759 dprintk("NFSD: %s: stateid=" STATEID_FMT
"\n", __func__
,
3762 fp
= find_file(ino
);
3765 dl
= find_delegation_file(fp
, stid
);
3771 * TODO: Linux file offsets are _signed_ 64-bit quantities, which means that
3772 * we can't properly handle lock requests that go beyond the (2^63 - 1)-th
3773 * byte, because of sign extension problems. Since NFSv4 calls for 64-bit
3774 * locking, this prevents us from being completely protocol-compliant. The
3775 * real solution to this problem is to start using unsigned file offsets in
3776 * the VFS, but this is a very deep change!
3779 nfs4_transform_lock_offset(struct file_lock
*lock
)
3781 if (lock
->fl_start
< 0)
3782 lock
->fl_start
= OFFSET_MAX
;
3783 if (lock
->fl_end
< 0)
3784 lock
->fl_end
= OFFSET_MAX
;
3787 /* Hack!: For now, we're defining this just so we can use a pointer to it
3788 * as a unique cookie to identify our (NFSv4's) posix locks. */
3789 static const struct lock_manager_operations nfsd_posix_mng_ops
= {
3793 nfs4_set_lock_denied(struct file_lock
*fl
, struct nfsd4_lock_denied
*deny
)
3795 struct nfs4_lockowner
*lo
;
3797 if (fl
->fl_lmops
== &nfsd_posix_mng_ops
) {
3798 lo
= (struct nfs4_lockowner
*) fl
->fl_owner
;
3799 deny
->ld_owner
.data
= kmemdup(lo
->lo_owner
.so_owner
.data
,
3800 lo
->lo_owner
.so_owner
.len
, GFP_KERNEL
);
3801 if (!deny
->ld_owner
.data
)
3802 /* We just don't care that much */
3804 deny
->ld_owner
.len
= lo
->lo_owner
.so_owner
.len
;
3805 deny
->ld_clientid
= lo
->lo_owner
.so_client
->cl_clientid
;
3808 deny
->ld_owner
.len
= 0;
3809 deny
->ld_owner
.data
= NULL
;
3810 deny
->ld_clientid
.cl_boot
= 0;
3811 deny
->ld_clientid
.cl_id
= 0;
3813 deny
->ld_start
= fl
->fl_start
;
3814 deny
->ld_length
= NFS4_MAX_UINT64
;
3815 if (fl
->fl_end
!= NFS4_MAX_UINT64
)
3816 deny
->ld_length
= fl
->fl_end
- fl
->fl_start
+ 1;
3817 deny
->ld_type
= NFS4_READ_LT
;
3818 if (fl
->fl_type
!= F_RDLCK
)
3819 deny
->ld_type
= NFS4_WRITE_LT
;
3822 static struct nfs4_lockowner
*
3823 find_lockowner_str(struct inode
*inode
, clientid_t
*clid
,
3824 struct xdr_netobj
*owner
)
3826 unsigned int hashval
= lock_ownerstr_hashval(inode
, clid
->cl_id
, owner
);
3827 struct nfs4_stateowner
*op
;
3829 list_for_each_entry(op
, &lock_ownerstr_hashtbl
[hashval
], so_strhash
) {
3830 if (same_owner_str(op
, owner
, clid
))
3831 return lockowner(op
);
3836 static void hash_lockowner(struct nfs4_lockowner
*lo
, unsigned int strhashval
, struct nfs4_client
*clp
, struct nfs4_stateid
*open_stp
)
3838 unsigned int idhashval
;
3840 idhashval
= lockownerid_hashval(lo
->lo_owner
.so_id
);
3841 list_add(&lo
->lo_owner
.so_idhash
, &lock_ownerid_hashtbl
[idhashval
]);
3842 list_add(&lo
->lo_owner
.so_strhash
, &lock_ownerstr_hashtbl
[strhashval
]);
3843 list_add(&lo
->lo_perstateid
, &open_stp
->st_lockowners
);
3847 * Alloc a lock owner structure.
3848 * Called in nfsd4_lock - therefore, OPEN and OPEN_CONFIRM (if needed) has
3851 * strhashval = lock_ownerstr_hashval
3854 static struct nfs4_lockowner
*
3855 alloc_init_lock_stateowner(unsigned int strhashval
, struct nfs4_client
*clp
, struct nfs4_stateid
*open_stp
, struct nfsd4_lock
*lock
) {
3856 struct nfs4_lockowner
*lo
;
3858 lo
= alloc_stateowner(lockowner_slab
, &lock
->lk_new_owner
, clp
);
3861 INIT_LIST_HEAD(&lo
->lo_owner
.so_stateids
);
3862 lo
->lo_owner
.so_is_open_owner
= 0;
3863 /* It is the openowner seqid that will be incremented in encode in the
3864 * case of new lockowners; so increment the lock seqid manually: */
3865 lo
->lo_owner
.so_seqid
= lock
->lk_new_lock_seqid
+ 1;
3866 hash_lockowner(lo
, strhashval
, clp
, open_stp
);
3870 static struct nfs4_stateid
*
3871 alloc_init_lock_stateid(struct nfs4_lockowner
*lo
, struct nfs4_file
*fp
, struct nfs4_stateid
*open_stp
)
3873 struct nfs4_stateid
*stp
;
3874 unsigned int hashval
= stateid_hashval(lo
->lo_owner
.so_id
, fp
->fi_id
);
3876 stp
= nfs4_alloc_stateid();
3879 INIT_LIST_HEAD(&stp
->st_hash
);
3880 INIT_LIST_HEAD(&stp
->st_perfile
);
3881 INIT_LIST_HEAD(&stp
->st_perstateowner
);
3882 INIT_LIST_HEAD(&stp
->st_lockowners
); /* not used */
3883 list_add(&stp
->st_hash
, &stateid_hashtbl
[hashval
]);
3884 list_add(&stp
->st_perfile
, &fp
->fi_stateids
);
3885 list_add(&stp
->st_perstateowner
, &lo
->lo_owner
.so_stateids
);
3886 stp
->st_stateowner
= &lo
->lo_owner
;
3887 stp
->st_type
= NFS4_LOCK_STID
;
3890 stp
->st_stateid
.si_boot
= boot_time
;
3891 stp
->st_stateid
.si_stateownerid
= lo
->lo_owner
.so_id
;
3892 stp
->st_stateid
.si_fileid
= fp
->fi_id
;
3893 /* note will be incremented before first return to client: */
3894 stp
->st_stateid
.si_generation
= 0;
3895 stp
->st_access_bmap
= 0;
3896 stp
->st_deny_bmap
= open_stp
->st_deny_bmap
;
3897 stp
->st_openstp
= open_stp
;
3904 check_lock_length(u64 offset
, u64 length
)
3906 return ((length
== 0) || ((length
!= NFS4_MAX_UINT64
) &&
3907 LOFF_OVERFLOW(offset
, length
)));
3910 static void get_lock_access(struct nfs4_stateid
*lock_stp
, u32 access
)
3912 struct nfs4_file
*fp
= lock_stp
->st_file
;
3913 int oflag
= nfs4_access_to_omode(access
);
3915 if (test_bit(access
, &lock_stp
->st_access_bmap
))
3917 nfs4_file_get_access(fp
, oflag
);
3918 __set_bit(access
, &lock_stp
->st_access_bmap
);
3925 nfsd4_lock(struct svc_rqst
*rqstp
, struct nfsd4_compound_state
*cstate
,
3926 struct nfsd4_lock
*lock
)
3928 struct nfs4_openowner
*open_sop
= NULL
;
3929 struct nfs4_lockowner
*lock_sop
= NULL
;
3930 struct nfs4_stateid
*lock_stp
;
3931 struct nfs4_file
*fp
;
3932 struct file
*filp
= NULL
;
3933 struct file_lock file_lock
;
3934 struct file_lock conflock
;
3936 unsigned int strhashval
;
3940 dprintk("NFSD: nfsd4_lock: start=%Ld length=%Ld\n",
3941 (long long) lock
->lk_offset
,
3942 (long long) lock
->lk_length
);
3944 if (check_lock_length(lock
->lk_offset
, lock
->lk_length
))
3945 return nfserr_inval
;
3947 if ((status
= fh_verify(rqstp
, &cstate
->current_fh
,
3948 S_IFREG
, NFSD_MAY_LOCK
))) {
3949 dprintk("NFSD: nfsd4_lock: permission denied!\n");
3955 if (lock
->lk_is_new
) {
3957 * Client indicates that this is a new lockowner.
3958 * Use open owner and open stateid to create lock owner and
3961 struct nfs4_stateid
*open_stp
= NULL
;
3963 status
= nfserr_stale_clientid
;
3964 if (!nfsd4_has_session(cstate
) &&
3965 STALE_CLIENTID(&lock
->lk_new_clientid
))
3968 /* validate and update open stateid and open seqid */
3969 status
= nfs4_preprocess_confirmed_seqid_op(cstate
,
3970 lock
->lk_new_open_seqid
,
3971 &lock
->lk_new_open_stateid
,
3975 open_sop
= openowner(open_stp
->st_stateowner
);
3976 status
= nfserr_bad_stateid
;
3977 if (!nfsd4_has_session(cstate
) &&
3978 !same_clid(&open_sop
->oo_owner
.so_client
->cl_clientid
,
3979 &lock
->v
.new.clientid
))
3981 /* create lockowner and lock stateid */
3982 fp
= open_stp
->st_file
;
3983 strhashval
= lock_ownerstr_hashval(fp
->fi_inode
,
3984 open_sop
->oo_owner
.so_client
->cl_clientid
.cl_id
,
3985 &lock
->v
.new.owner
);
3986 /* XXX: Do we need to check for duplicate stateowners on
3987 * the same file, or should they just be allowed (and
3988 * create new stateids)? */
3989 status
= nfserr_jukebox
;
3990 lock_sop
= alloc_init_lock_stateowner(strhashval
,
3991 open_sop
->oo_owner
.so_client
, open_stp
, lock
);
3992 if (lock_sop
== NULL
)
3994 lock_stp
= alloc_init_lock_stateid(lock_sop
, fp
, open_stp
);
3995 if (lock_stp
== NULL
)
3998 /* lock (lock owner + lock stateid) already exists */
3999 status
= nfs4_preprocess_seqid_op(cstate
,
4000 lock
->lk_old_lock_seqid
,
4001 &lock
->lk_old_lock_stateid
,
4002 LOCK_STATE
, &lock_stp
);
4005 lock_sop
= lockowner(lock_stp
->st_stateowner
);
4006 fp
= lock_stp
->st_file
;
4008 /* lock_sop and lock_stp have been created or found */
4010 lkflg
= setlkflg(lock
->lk_type
);
4011 status
= nfs4_check_openmode(lock_stp
, lkflg
);
4015 status
= nfserr_grace
;
4016 if (locks_in_grace() && !lock
->lk_reclaim
)
4018 status
= nfserr_no_grace
;
4019 if (!locks_in_grace() && lock
->lk_reclaim
)
4022 locks_init_lock(&file_lock
);
4023 switch (lock
->lk_type
) {
4026 filp
= find_readable_file(lock_stp
->st_file
);
4028 get_lock_access(lock_stp
, NFS4_SHARE_ACCESS_READ
);
4029 file_lock
.fl_type
= F_RDLCK
;
4032 case NFS4_WRITEW_LT
:
4033 filp
= find_writeable_file(lock_stp
->st_file
);
4035 get_lock_access(lock_stp
, NFS4_SHARE_ACCESS_WRITE
);
4036 file_lock
.fl_type
= F_WRLCK
;
4039 status
= nfserr_inval
;
4043 status
= nfserr_openmode
;
4046 file_lock
.fl_owner
= (fl_owner_t
)lock_sop
;
4047 file_lock
.fl_pid
= current
->tgid
;
4048 file_lock
.fl_file
= filp
;
4049 file_lock
.fl_flags
= FL_POSIX
;
4050 file_lock
.fl_lmops
= &nfsd_posix_mng_ops
;
4052 file_lock
.fl_start
= lock
->lk_offset
;
4053 file_lock
.fl_end
= last_byte_offset(lock
->lk_offset
, lock
->lk_length
);
4054 nfs4_transform_lock_offset(&file_lock
);
4057 * Try to lock the file in the VFS.
4058 * Note: locks.c uses the BKL to protect the inode's lock list.
4061 err
= vfs_lock_file(filp
, F_SETLK
, &file_lock
, &conflock
);
4063 case 0: /* success! */
4064 update_stateid(&lock_stp
->st_stateid
);
4065 memcpy(&lock
->lk_resp_stateid
, &lock_stp
->st_stateid
,
4069 case (EAGAIN
): /* conflock holds conflicting lock */
4070 status
= nfserr_denied
;
4071 dprintk("NFSD: nfsd4_lock: conflicting lock found!\n");
4072 nfs4_set_lock_denied(&conflock
, &lock
->lk_denied
);
4075 status
= nfserr_deadlock
;
4078 dprintk("NFSD: nfsd4_lock: vfs_lock_file() failed! status %d\n",err
);
4079 status
= nfserrno(err
);
4083 if (status
&& lock
->lk_is_new
&& lock_sop
)
4084 release_lockowner(lock_sop
);
4085 if (!cstate
->replay_owner
)
4086 nfs4_unlock_state();
4091 * The NFSv4 spec allows a client to do a LOCKT without holding an OPEN,
4092 * so we do a temporary open here just to get an open file to pass to
4093 * vfs_test_lock. (Arguably perhaps test_lock should be done with an
4096 static int nfsd_test_lock(struct svc_rqst
*rqstp
, struct svc_fh
*fhp
, struct file_lock
*lock
)
4101 err
= nfsd_open(rqstp
, fhp
, S_IFREG
, NFSD_MAY_READ
, &file
);
4104 err
= vfs_test_lock(file
, lock
);
4113 nfsd4_lockt(struct svc_rqst
*rqstp
, struct nfsd4_compound_state
*cstate
,
4114 struct nfsd4_lockt
*lockt
)
4116 struct inode
*inode
;
4117 struct file_lock file_lock
;
4118 struct nfs4_lockowner
*lo
;
4122 if (locks_in_grace())
4123 return nfserr_grace
;
4125 if (check_lock_length(lockt
->lt_offset
, lockt
->lt_length
))
4126 return nfserr_inval
;
4130 status
= nfserr_stale_clientid
;
4131 if (!nfsd4_has_session(cstate
) && STALE_CLIENTID(&lockt
->lt_clientid
))
4134 if ((status
= fh_verify(rqstp
, &cstate
->current_fh
, S_IFREG
, 0)))
4137 inode
= cstate
->current_fh
.fh_dentry
->d_inode
;
4138 locks_init_lock(&file_lock
);
4139 switch (lockt
->lt_type
) {
4142 file_lock
.fl_type
= F_RDLCK
;
4145 case NFS4_WRITEW_LT
:
4146 file_lock
.fl_type
= F_WRLCK
;
4149 dprintk("NFSD: nfs4_lockt: bad lock type!\n");
4150 status
= nfserr_inval
;
4154 lo
= find_lockowner_str(inode
, &lockt
->lt_clientid
, &lockt
->lt_owner
);
4156 file_lock
.fl_owner
= (fl_owner_t
)lo
;
4157 file_lock
.fl_pid
= current
->tgid
;
4158 file_lock
.fl_flags
= FL_POSIX
;
4160 file_lock
.fl_start
= lockt
->lt_offset
;
4161 file_lock
.fl_end
= last_byte_offset(lockt
->lt_offset
, lockt
->lt_length
);
4163 nfs4_transform_lock_offset(&file_lock
);
4166 error
= nfsd_test_lock(rqstp
, &cstate
->current_fh
, &file_lock
);
4168 status
= nfserrno(error
);
4171 if (file_lock
.fl_type
!= F_UNLCK
) {
4172 status
= nfserr_denied
;
4173 nfs4_set_lock_denied(&file_lock
, &lockt
->lt_denied
);
4176 nfs4_unlock_state();
4181 nfsd4_locku(struct svc_rqst
*rqstp
, struct nfsd4_compound_state
*cstate
,
4182 struct nfsd4_locku
*locku
)
4184 struct nfs4_stateid
*stp
;
4185 struct file
*filp
= NULL
;
4186 struct file_lock file_lock
;
4190 dprintk("NFSD: nfsd4_locku: start=%Ld length=%Ld\n",
4191 (long long) locku
->lu_offset
,
4192 (long long) locku
->lu_length
);
4194 if (check_lock_length(locku
->lu_offset
, locku
->lu_length
))
4195 return nfserr_inval
;
4199 status
= nfs4_preprocess_seqid_op(cstate
, locku
->lu_seqid
,
4200 &locku
->lu_stateid
, LOCK_STATE
, &stp
);
4203 filp
= find_any_file(stp
->st_file
);
4205 status
= nfserr_lock_range
;
4209 locks_init_lock(&file_lock
);
4210 file_lock
.fl_type
= F_UNLCK
;
4211 file_lock
.fl_owner
= (fl_owner_t
)lockowner(stp
->st_stateowner
);
4212 file_lock
.fl_pid
= current
->tgid
;
4213 file_lock
.fl_file
= filp
;
4214 file_lock
.fl_flags
= FL_POSIX
;
4215 file_lock
.fl_lmops
= &nfsd_posix_mng_ops
;
4216 file_lock
.fl_start
= locku
->lu_offset
;
4218 file_lock
.fl_end
= last_byte_offset(locku
->lu_offset
, locku
->lu_length
);
4219 nfs4_transform_lock_offset(&file_lock
);
4222 * Try to unlock the file in the VFS.
4224 err
= vfs_lock_file(filp
, F_SETLK
, &file_lock
, NULL
);
4226 dprintk("NFSD: nfs4_locku: vfs_lock_file failed!\n");
4230 * OK, unlock succeeded; the only thing left to do is update the stateid.
4232 update_stateid(&stp
->st_stateid
);
4233 memcpy(&locku
->lu_stateid
, &stp
->st_stateid
, sizeof(stateid_t
));
4236 nfs4_unlock_state();
4240 status
= nfserrno(err
);
4246 * 1: locks held by lockowner
4247 * 0: no locks held by lockowner
4250 check_for_locks(struct nfs4_file
*filp
, struct nfs4_lockowner
*lowner
)
4252 struct file_lock
**flpp
;
4253 struct inode
*inode
= filp
->fi_inode
;
4257 for (flpp
= &inode
->i_flock
; *flpp
!= NULL
; flpp
= &(*flpp
)->fl_next
) {
4258 if ((*flpp
)->fl_owner
== (fl_owner_t
)lowner
) {
4269 nfsd4_release_lockowner(struct svc_rqst
*rqstp
,
4270 struct nfsd4_compound_state
*cstate
,
4271 struct nfsd4_release_lockowner
*rlockowner
)
4273 clientid_t
*clid
= &rlockowner
->rl_clientid
;
4274 struct nfs4_stateowner
*sop
;
4275 struct nfs4_lockowner
*lo
;
4276 struct nfs4_stateid
*stp
;
4277 struct xdr_netobj
*owner
= &rlockowner
->rl_owner
;
4278 struct list_head matches
;
4282 dprintk("nfsd4_release_lockowner clientid: (%08x/%08x):\n",
4283 clid
->cl_boot
, clid
->cl_id
);
4285 /* XXX check for lease expiration */
4287 status
= nfserr_stale_clientid
;
4288 if (STALE_CLIENTID(clid
))
4293 status
= nfserr_locks_held
;
4294 /* XXX: we're doing a linear search through all the lockowners.
4295 * Yipes! For now we'll just hope clients aren't really using
4296 * release_lockowner much, but eventually we have to fix these
4297 * data structures. */
4298 INIT_LIST_HEAD(&matches
);
4299 for (i
= 0; i
< LOCK_HASH_SIZE
; i
++) {
4300 list_for_each_entry(sop
, &lock_ownerid_hashtbl
[i
], so_idhash
) {
4301 if (!same_owner_str(sop
, owner
, clid
))
4303 list_for_each_entry(stp
, &sop
->so_stateids
,
4305 lo
= lockowner(sop
);
4306 if (check_for_locks(stp
->st_file
, lo
))
4308 list_add(&lo
->lo_list
, &matches
);
4312 /* Clients probably won't expect us to return with some (but not all)
4313 * of the lockowner state released; so don't release any until all
4314 * have been checked. */
4316 while (!list_empty(&matches
)) {
4317 lo
= list_entry(matches
.next
, struct nfs4_lockowner
,
4319 /* unhash_stateowner deletes so_perclient only
4320 * for openowners. */
4321 list_del(&lo
->lo_list
);
4322 release_lockowner(lo
);
4325 nfs4_unlock_state();
4329 static inline struct nfs4_client_reclaim
*
4332 return kmalloc(sizeof(struct nfs4_client_reclaim
), GFP_KERNEL
);
4336 nfs4_has_reclaimed_state(const char *name
, bool use_exchange_id
)
4338 unsigned int strhashval
= clientstr_hashval(name
);
4339 struct nfs4_client
*clp
;
4341 clp
= find_confirmed_client_by_str(name
, strhashval
);
4346 * failure => all reset bets are off, nfserr_no_grace...
4349 nfs4_client_to_reclaim(const char *name
)
4351 unsigned int strhashval
;
4352 struct nfs4_client_reclaim
*crp
= NULL
;
4354 dprintk("NFSD nfs4_client_to_reclaim NAME: %.*s\n", HEXDIR_LEN
, name
);
4355 crp
= alloc_reclaim();
4358 strhashval
= clientstr_hashval(name
);
4359 INIT_LIST_HEAD(&crp
->cr_strhash
);
4360 list_add(&crp
->cr_strhash
, &reclaim_str_hashtbl
[strhashval
]);
4361 memcpy(crp
->cr_recdir
, name
, HEXDIR_LEN
);
4362 reclaim_str_hashtbl_size
++;
4367 nfs4_release_reclaim(void)
4369 struct nfs4_client_reclaim
*crp
= NULL
;
4372 for (i
= 0; i
< CLIENT_HASH_SIZE
; i
++) {
4373 while (!list_empty(&reclaim_str_hashtbl
[i
])) {
4374 crp
= list_entry(reclaim_str_hashtbl
[i
].next
,
4375 struct nfs4_client_reclaim
, cr_strhash
);
4376 list_del(&crp
->cr_strhash
);
4378 reclaim_str_hashtbl_size
--;
4381 BUG_ON(reclaim_str_hashtbl_size
);
4385 * called from OPEN, CLAIM_PREVIOUS with a new clientid. */
4386 static struct nfs4_client_reclaim
*
4387 nfs4_find_reclaim_client(clientid_t
*clid
)
4389 unsigned int strhashval
;
4390 struct nfs4_client
*clp
;
4391 struct nfs4_client_reclaim
*crp
= NULL
;
4394 /* find clientid in conf_id_hashtbl */
4395 clp
= find_confirmed_client(clid
);
4399 dprintk("NFSD: nfs4_find_reclaim_client for %.*s with recdir %s\n",
4400 clp
->cl_name
.len
, clp
->cl_name
.data
,
4403 /* find clp->cl_name in reclaim_str_hashtbl */
4404 strhashval
= clientstr_hashval(clp
->cl_recdir
);
4405 list_for_each_entry(crp
, &reclaim_str_hashtbl
[strhashval
], cr_strhash
) {
4406 if (same_name(crp
->cr_recdir
, clp
->cl_recdir
)) {
4414 * Called from OPEN. Look for clientid in reclaim list.
4417 nfs4_check_open_reclaim(clientid_t
*clid
)
4419 return nfs4_find_reclaim_client(clid
) ? nfs_ok
: nfserr_reclaim_bad
;
4422 /* initialization to perform at module load time: */
4425 nfs4_state_init(void)
4429 status
= nfsd4_init_slabs();
4432 for (i
= 0; i
< CLIENT_HASH_SIZE
; i
++) {
4433 INIT_LIST_HEAD(&conf_id_hashtbl
[i
]);
4434 INIT_LIST_HEAD(&conf_str_hashtbl
[i
]);
4435 INIT_LIST_HEAD(&unconf_str_hashtbl
[i
]);
4436 INIT_LIST_HEAD(&unconf_id_hashtbl
[i
]);
4437 INIT_LIST_HEAD(&reclaim_str_hashtbl
[i
]);
4439 for (i
= 0; i
< SESSION_HASH_SIZE
; i
++)
4440 INIT_LIST_HEAD(&sessionid_hashtbl
[i
]);
4441 for (i
= 0; i
< FILE_HASH_SIZE
; i
++) {
4442 INIT_LIST_HEAD(&file_hashtbl
[i
]);
4444 for (i
= 0; i
< OPEN_OWNER_HASH_SIZE
; i
++) {
4445 INIT_LIST_HEAD(&open_ownerstr_hashtbl
[i
]);
4446 INIT_LIST_HEAD(&open_ownerid_hashtbl
[i
]);
4448 for (i
= 0; i
< STATEID_HASH_SIZE
; i
++)
4449 INIT_LIST_HEAD(&stateid_hashtbl
[i
]);
4450 for (i
= 0; i
< LOCK_HASH_SIZE
; i
++) {
4451 INIT_LIST_HEAD(&lock_ownerid_hashtbl
[i
]);
4452 INIT_LIST_HEAD(&lock_ownerstr_hashtbl
[i
]);
4454 memset(&onestateid
, ~0, sizeof(stateid_t
));
4455 INIT_LIST_HEAD(&close_lru
);
4456 INIT_LIST_HEAD(&client_lru
);
4457 INIT_LIST_HEAD(&del_recall_lru
);
4458 reclaim_str_hashtbl_size
= 0;
4463 nfsd4_load_reboot_recovery_data(void)
4468 nfsd4_init_recdir();
4469 status
= nfsd4_recdir_load();
4470 nfs4_unlock_state();
4472 printk("NFSD: Failure reading reboot recovery data\n");
4476 * Since the lifetime of a delegation isn't limited to that of an open, a
4477 * client may quite reasonably hang on to a delegation as long as it has
4478 * the inode cached. This becomes an obvious problem the first time a
4479 * client's inode cache approaches the size of the server's total memory.
4481 * For now we avoid this problem by imposing a hard limit on the number
4482 * of delegations, which varies according to the server's memory size.
4485 set_max_delegations(void)
4488 * Allow at most 4 delegations per megabyte of RAM. Quick
4489 * estimates suggest that in the worst case (where every delegation
4490 * is for a different inode), a delegation could take about 1.5K,
4491 * giving a worst case usage of about 6% of memory.
4493 max_delegations
= nr_free_buffer_pages() >> (20 - 2 - PAGE_SHIFT
);
4496 /* initialization to perform when the nfsd service is started: */
4499 __nfs4_state_start(void)
4503 boot_time
= get_seconds();
4504 locks_start_grace(&nfsd4_manager
);
4505 printk(KERN_INFO
"NFSD: starting %ld-second grace period\n",
4507 ret
= set_callback_cred();
4510 laundry_wq
= create_singlethread_workqueue("nfsd4");
4511 if (laundry_wq
== NULL
)
4513 ret
= nfsd4_create_callback_queue();
4515 goto out_free_laundry
;
4516 queue_delayed_work(laundry_wq
, &laundromat_work
, nfsd4_grace
* HZ
);
4517 set_max_delegations();
4520 destroy_workqueue(laundry_wq
);
4525 nfs4_state_start(void)
4527 nfsd4_load_reboot_recovery_data();
4528 return __nfs4_state_start();
4532 __nfs4_state_shutdown(void)
4535 struct nfs4_client
*clp
= NULL
;
4536 struct nfs4_delegation
*dp
= NULL
;
4537 struct list_head
*pos
, *next
, reaplist
;
4539 for (i
= 0; i
< CLIENT_HASH_SIZE
; i
++) {
4540 while (!list_empty(&conf_id_hashtbl
[i
])) {
4541 clp
= list_entry(conf_id_hashtbl
[i
].next
, struct nfs4_client
, cl_idhash
);
4544 while (!list_empty(&unconf_str_hashtbl
[i
])) {
4545 clp
= list_entry(unconf_str_hashtbl
[i
].next
, struct nfs4_client
, cl_strhash
);
4549 INIT_LIST_HEAD(&reaplist
);
4550 spin_lock(&recall_lock
);
4551 list_for_each_safe(pos
, next
, &del_recall_lru
) {
4552 dp
= list_entry (pos
, struct nfs4_delegation
, dl_recall_lru
);
4553 list_move(&dp
->dl_recall_lru
, &reaplist
);
4555 spin_unlock(&recall_lock
);
4556 list_for_each_safe(pos
, next
, &reaplist
) {
4557 dp
= list_entry (pos
, struct nfs4_delegation
, dl_recall_lru
);
4558 list_del_init(&dp
->dl_recall_lru
);
4559 unhash_delegation(dp
);
4562 nfsd4_shutdown_recdir();
4566 nfs4_state_shutdown(void)
4568 cancel_delayed_work_sync(&laundromat_work
);
4569 destroy_workqueue(laundry_wq
);
4570 locks_end_grace(&nfsd4_manager
);
4572 nfs4_release_reclaim();
4573 __nfs4_state_shutdown();
4574 nfs4_unlock_state();
4575 nfsd4_destroy_callback_queue();