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_stateid
* find_stateid(stateid_t
*stid
, int flags
);
64 static struct nfs4_stateid
* search_for_stateid(stateid_t
*stid
);
65 static struct nfs4_delegation
* search_for_delegation(stateid_t
*stid
);
66 static struct nfs4_delegation
* find_delegation_stateid(struct inode
*ino
, stateid_t
*stid
);
67 static int check_for_locks(struct nfs4_file
*filp
, struct nfs4_stateowner
*lowner
);
71 /* Currently used for almost all code touching nfsv4 state: */
72 static DEFINE_MUTEX(client_mutex
);
75 * Currently used for the del_recall_lru and file hash table. In an
76 * effort to decrease the scope of the client_mutex, this spinlock may
77 * eventually cover more:
79 static DEFINE_SPINLOCK(recall_lock
);
81 static struct kmem_cache
*stateowner_slab
= NULL
;
82 static struct kmem_cache
*file_slab
= NULL
;
83 static struct kmem_cache
*stateid_slab
= NULL
;
84 static struct kmem_cache
*deleg_slab
= NULL
;
89 mutex_lock(&client_mutex
);
93 nfs4_unlock_state(void)
95 mutex_unlock(&client_mutex
);
99 opaque_hashval(const void *ptr
, int nbytes
)
101 unsigned char *cptr
= (unsigned char *) ptr
;
111 static struct list_head del_recall_lru
;
114 put_nfs4_file(struct nfs4_file
*fi
)
116 if (atomic_dec_and_lock(&fi
->fi_ref
, &recall_lock
)) {
117 list_del(&fi
->fi_hash
);
118 spin_unlock(&recall_lock
);
120 kmem_cache_free(file_slab
, fi
);
125 get_nfs4_file(struct nfs4_file
*fi
)
127 atomic_inc(&fi
->fi_ref
);
130 static int num_delegations
;
131 unsigned int max_delegations
;
134 * Open owner state (share locks)
137 /* hash tables for nfs4_stateowner */
138 #define OWNER_HASH_BITS 8
139 #define OWNER_HASH_SIZE (1 << OWNER_HASH_BITS)
140 #define OWNER_HASH_MASK (OWNER_HASH_SIZE - 1)
142 #define ownerid_hashval(id) \
143 ((id) & OWNER_HASH_MASK)
144 #define ownerstr_hashval(clientid, ownername) \
145 (((clientid) + opaque_hashval((ownername.data), (ownername.len))) & OWNER_HASH_MASK)
147 static struct list_head ownerid_hashtbl
[OWNER_HASH_SIZE
];
148 static struct list_head ownerstr_hashtbl
[OWNER_HASH_SIZE
];
150 /* hash table for nfs4_file */
151 #define FILE_HASH_BITS 8
152 #define FILE_HASH_SIZE (1 << FILE_HASH_BITS)
154 /* hash table for (open)nfs4_stateid */
155 #define STATEID_HASH_BITS 10
156 #define STATEID_HASH_SIZE (1 << STATEID_HASH_BITS)
157 #define STATEID_HASH_MASK (STATEID_HASH_SIZE - 1)
159 #define file_hashval(x) \
160 hash_ptr(x, FILE_HASH_BITS)
161 #define stateid_hashval(owner_id, file_id) \
162 (((owner_id) + (file_id)) & STATEID_HASH_MASK)
164 static struct list_head file_hashtbl
[FILE_HASH_SIZE
];
165 static struct list_head stateid_hashtbl
[STATEID_HASH_SIZE
];
167 static void __nfs4_file_get_access(struct nfs4_file
*fp
, int oflag
)
169 BUG_ON(!(fp
->fi_fds
[oflag
] || fp
->fi_fds
[O_RDWR
]));
170 atomic_inc(&fp
->fi_access
[oflag
]);
173 static void nfs4_file_get_access(struct nfs4_file
*fp
, int oflag
)
175 if (oflag
== O_RDWR
) {
176 __nfs4_file_get_access(fp
, O_RDONLY
);
177 __nfs4_file_get_access(fp
, O_WRONLY
);
179 __nfs4_file_get_access(fp
, oflag
);
182 static void nfs4_file_put_fd(struct nfs4_file
*fp
, int oflag
)
184 if (fp
->fi_fds
[oflag
]) {
185 fput(fp
->fi_fds
[oflag
]);
186 fp
->fi_fds
[oflag
] = NULL
;
190 static void __nfs4_file_put_access(struct nfs4_file
*fp
, int oflag
)
192 if (atomic_dec_and_test(&fp
->fi_access
[oflag
])) {
193 nfs4_file_put_fd(fp
, O_RDWR
);
194 nfs4_file_put_fd(fp
, oflag
);
198 static void nfs4_file_put_access(struct nfs4_file
*fp
, int oflag
)
200 if (oflag
== O_RDWR
) {
201 __nfs4_file_put_access(fp
, O_RDONLY
);
202 __nfs4_file_put_access(fp
, O_WRONLY
);
204 __nfs4_file_put_access(fp
, oflag
);
207 static struct nfs4_delegation
*
208 alloc_init_deleg(struct nfs4_client
*clp
, struct nfs4_stateid
*stp
, struct svc_fh
*current_fh
, u32 type
)
210 struct nfs4_delegation
*dp
;
211 struct nfs4_file
*fp
= stp
->st_file
;
213 dprintk("NFSD alloc_init_deleg\n");
215 * Major work on the lease subsystem (for example, to support
216 * calbacks on stat) will be required before we can support
217 * write delegations properly.
219 if (type
!= NFS4_OPEN_DELEGATE_READ
)
221 if (fp
->fi_had_conflict
)
223 if (num_delegations
> max_delegations
)
225 dp
= kmem_cache_alloc(deleg_slab
, GFP_KERNEL
);
229 INIT_LIST_HEAD(&dp
->dl_perfile
);
230 INIT_LIST_HEAD(&dp
->dl_perclnt
);
231 INIT_LIST_HEAD(&dp
->dl_recall_lru
);
236 dp
->dl_stateid
.si_boot
= boot_time
;
237 dp
->dl_stateid
.si_stateownerid
= current_delegid
++;
238 dp
->dl_stateid
.si_fileid
= 0;
239 dp
->dl_stateid
.si_generation
= 0;
240 fh_copy_shallow(&dp
->dl_fh
, ¤t_fh
->fh_handle
);
242 atomic_set(&dp
->dl_count
, 1);
243 INIT_WORK(&dp
->dl_recall
.cb_work
, nfsd4_do_callback_rpc
);
248 nfs4_put_delegation(struct nfs4_delegation
*dp
)
250 if (atomic_dec_and_test(&dp
->dl_count
)) {
251 dprintk("NFSD: freeing dp %p\n",dp
);
252 put_nfs4_file(dp
->dl_file
);
253 kmem_cache_free(deleg_slab
, dp
);
258 static void nfs4_put_deleg_lease(struct nfs4_file
*fp
)
260 if (atomic_dec_and_test(&fp
->fi_delegees
)) {
261 vfs_setlease(fp
->fi_deleg_file
, F_UNLCK
, &fp
->fi_lease
);
263 fput(fp
->fi_deleg_file
);
264 fp
->fi_deleg_file
= NULL
;
268 /* Called under the state lock. */
270 unhash_delegation(struct nfs4_delegation
*dp
)
272 list_del_init(&dp
->dl_perclnt
);
273 spin_lock(&recall_lock
);
274 list_del_init(&dp
->dl_perfile
);
275 list_del_init(&dp
->dl_recall_lru
);
276 spin_unlock(&recall_lock
);
277 nfs4_put_deleg_lease(dp
->dl_file
);
278 nfs4_put_delegation(dp
);
285 /* client_lock protects the client lru list and session hash table */
286 static DEFINE_SPINLOCK(client_lock
);
288 /* Hash tables for nfs4_clientid state */
289 #define CLIENT_HASH_BITS 4
290 #define CLIENT_HASH_SIZE (1 << CLIENT_HASH_BITS)
291 #define CLIENT_HASH_MASK (CLIENT_HASH_SIZE - 1)
293 #define clientid_hashval(id) \
294 ((id) & CLIENT_HASH_MASK)
295 #define clientstr_hashval(name) \
296 (opaque_hashval((name), 8) & CLIENT_HASH_MASK)
298 * reclaim_str_hashtbl[] holds known client info from previous reset/reboot
299 * used in reboot/reset lease grace period processing
301 * conf_id_hashtbl[], and conf_str_hashtbl[] hold confirmed
302 * setclientid_confirmed info.
304 * unconf_str_hastbl[] and unconf_id_hashtbl[] hold unconfirmed
307 * client_lru holds client queue ordered by nfs4_client.cl_time
310 * close_lru holds (open) stateowner queue ordered by nfs4_stateowner.so_time
311 * for last close replay.
313 static struct list_head reclaim_str_hashtbl
[CLIENT_HASH_SIZE
];
314 static int reclaim_str_hashtbl_size
= 0;
315 static struct list_head conf_id_hashtbl
[CLIENT_HASH_SIZE
];
316 static struct list_head conf_str_hashtbl
[CLIENT_HASH_SIZE
];
317 static struct list_head unconf_str_hashtbl
[CLIENT_HASH_SIZE
];
318 static struct list_head unconf_id_hashtbl
[CLIENT_HASH_SIZE
];
319 static struct list_head client_lru
;
320 static struct list_head close_lru
;
323 * We store the NONE, READ, WRITE, and BOTH bits separately in the
324 * st_{access,deny}_bmap field of the stateid, in order to track not
325 * only what share bits are currently in force, but also what
326 * combinations of share bits previous opens have used. This allows us
327 * to enforce the recommendation of rfc 3530 14.2.19 that the server
328 * return an error if the client attempt to downgrade to a combination
329 * of share bits not explicable by closing some of its previous opens.
331 * XXX: This enforcement is actually incomplete, since we don't keep
332 * track of access/deny bit combinations; so, e.g., we allow:
334 * OPEN allow read, deny write
335 * OPEN allow both, deny none
336 * DOWNGRADE allow read, deny none
338 * which we should reject.
341 set_access(unsigned int *access
, unsigned long bmap
) {
345 for (i
= 1; i
< 4; i
++) {
346 if (test_bit(i
, &bmap
))
352 set_deny(unsigned int *deny
, unsigned long bmap
) {
356 for (i
= 0; i
< 4; i
++) {
357 if (test_bit(i
, &bmap
))
363 test_share(struct nfs4_stateid
*stp
, struct nfsd4_open
*open
) {
364 unsigned int access
, deny
;
366 set_access(&access
, stp
->st_access_bmap
);
367 set_deny(&deny
, stp
->st_deny_bmap
);
368 if ((access
& open
->op_share_deny
) || (deny
& open
->op_share_access
))
373 static int nfs4_access_to_omode(u32 access
)
375 switch (access
& NFS4_SHARE_ACCESS_BOTH
) {
376 case NFS4_SHARE_ACCESS_READ
:
378 case NFS4_SHARE_ACCESS_WRITE
:
380 case NFS4_SHARE_ACCESS_BOTH
:
386 static void unhash_generic_stateid(struct nfs4_stateid
*stp
)
388 list_del(&stp
->st_hash
);
389 list_del(&stp
->st_perfile
);
390 list_del(&stp
->st_perstateowner
);
393 static void free_generic_stateid(struct nfs4_stateid
*stp
)
397 if (stp
->st_access_bmap
) {
398 for (i
= 1; i
< 4; i
++) {
399 if (test_bit(i
, &stp
->st_access_bmap
))
400 nfs4_file_put_access(stp
->st_file
,
401 nfs4_access_to_omode(i
));
404 put_nfs4_file(stp
->st_file
);
405 kmem_cache_free(stateid_slab
, stp
);
408 static void release_lock_stateid(struct nfs4_stateid
*stp
)
412 unhash_generic_stateid(stp
);
413 file
= find_any_file(stp
->st_file
);
415 locks_remove_posix(file
, (fl_owner_t
)stp
->st_stateowner
);
416 free_generic_stateid(stp
);
419 static void unhash_lockowner(struct nfs4_stateowner
*sop
)
421 struct nfs4_stateid
*stp
;
423 list_del(&sop
->so_idhash
);
424 list_del(&sop
->so_strhash
);
425 list_del(&sop
->so_perstateid
);
426 while (!list_empty(&sop
->so_stateids
)) {
427 stp
= list_first_entry(&sop
->so_stateids
,
428 struct nfs4_stateid
, st_perstateowner
);
429 release_lock_stateid(stp
);
433 static void release_lockowner(struct nfs4_stateowner
*sop
)
435 unhash_lockowner(sop
);
436 nfs4_put_stateowner(sop
);
440 release_stateid_lockowners(struct nfs4_stateid
*open_stp
)
442 struct nfs4_stateowner
*lock_sop
;
444 while (!list_empty(&open_stp
->st_lockowners
)) {
445 lock_sop
= list_entry(open_stp
->st_lockowners
.next
,
446 struct nfs4_stateowner
, so_perstateid
);
447 /* list_del(&open_stp->st_lockowners); */
448 BUG_ON(lock_sop
->so_is_open_owner
);
449 release_lockowner(lock_sop
);
453 static void release_open_stateid(struct nfs4_stateid
*stp
)
455 unhash_generic_stateid(stp
);
456 release_stateid_lockowners(stp
);
457 free_generic_stateid(stp
);
460 static void unhash_openowner(struct nfs4_stateowner
*sop
)
462 struct nfs4_stateid
*stp
;
464 list_del(&sop
->so_idhash
);
465 list_del(&sop
->so_strhash
);
466 list_del(&sop
->so_perclient
);
467 list_del(&sop
->so_perstateid
); /* XXX: necessary? */
468 while (!list_empty(&sop
->so_stateids
)) {
469 stp
= list_first_entry(&sop
->so_stateids
,
470 struct nfs4_stateid
, st_perstateowner
);
471 release_open_stateid(stp
);
475 static void release_openowner(struct nfs4_stateowner
*sop
)
477 unhash_openowner(sop
);
478 list_del(&sop
->so_close_lru
);
479 nfs4_put_stateowner(sop
);
482 #define SESSION_HASH_SIZE 512
483 static struct list_head sessionid_hashtbl
[SESSION_HASH_SIZE
];
486 hash_sessionid(struct nfs4_sessionid
*sessionid
)
488 struct nfsd4_sessionid
*sid
= (struct nfsd4_sessionid
*)sessionid
;
490 return sid
->sequence
% SESSION_HASH_SIZE
;
494 dump_sessionid(const char *fn
, struct nfs4_sessionid
*sessionid
)
496 u32
*ptr
= (u32
*)(&sessionid
->data
[0]);
497 dprintk("%s: %u:%u:%u:%u\n", fn
, ptr
[0], ptr
[1], ptr
[2], ptr
[3]);
501 gen_sessionid(struct nfsd4_session
*ses
)
503 struct nfs4_client
*clp
= ses
->se_client
;
504 struct nfsd4_sessionid
*sid
;
506 sid
= (struct nfsd4_sessionid
*)ses
->se_sessionid
.data
;
507 sid
->clientid
= clp
->cl_clientid
;
508 sid
->sequence
= current_sessionid
++;
513 * The protocol defines ca_maxresponssize_cached to include the size of
514 * the rpc header, but all we need to cache is the data starting after
515 * the end of the initial SEQUENCE operation--the rest we regenerate
516 * each time. Therefore we can advertise a ca_maxresponssize_cached
517 * value that is the number of bytes in our cache plus a few additional
518 * bytes. In order to stay on the safe side, and not promise more than
519 * we can cache, those additional bytes must be the minimum possible: 24
520 * bytes of rpc header (xid through accept state, with AUTH_NULL
521 * verifier), 12 for the compound header (with zero-length tag), and 44
522 * for the SEQUENCE op response:
524 #define NFSD_MIN_HDR_SEQ_SZ (24 + 12 + 44)
527 free_session_slots(struct nfsd4_session
*ses
)
531 for (i
= 0; i
< ses
->se_fchannel
.maxreqs
; i
++)
532 kfree(ses
->se_slots
[i
]);
536 * We don't actually need to cache the rpc and session headers, so we
537 * can allocate a little less for each slot:
539 static inline int slot_bytes(struct nfsd4_channel_attrs
*ca
)
541 return ca
->maxresp_cached
- NFSD_MIN_HDR_SEQ_SZ
;
544 static int nfsd4_sanitize_slot_size(u32 size
)
546 size
-= NFSD_MIN_HDR_SEQ_SZ
; /* We don't cache the rpc header */
547 size
= min_t(u32
, size
, NFSD_SLOT_CACHE_SIZE
);
553 * XXX: If we run out of reserved DRC memory we could (up to a point)
554 * re-negotiate active sessions and reduce their slot usage to make
555 * rooom for new connections. For now we just fail the create session.
557 static int nfsd4_get_drc_mem(int slotsize
, u32 num
)
561 num
= min_t(u32
, num
, NFSD_MAX_SLOTS_PER_SESSION
);
563 spin_lock(&nfsd_drc_lock
);
564 avail
= min_t(int, NFSD_MAX_MEM_PER_SESSION
,
565 nfsd_drc_max_mem
- nfsd_drc_mem_used
);
566 num
= min_t(int, num
, avail
/ slotsize
);
567 nfsd_drc_mem_used
+= num
* slotsize
;
568 spin_unlock(&nfsd_drc_lock
);
573 static void nfsd4_put_drc_mem(int slotsize
, int num
)
575 spin_lock(&nfsd_drc_lock
);
576 nfsd_drc_mem_used
-= slotsize
* num
;
577 spin_unlock(&nfsd_drc_lock
);
580 static struct nfsd4_session
*alloc_session(int slotsize
, int numslots
)
582 struct nfsd4_session
*new;
585 BUILD_BUG_ON(NFSD_MAX_SLOTS_PER_SESSION
* sizeof(struct nfsd4_slot
*)
586 + sizeof(struct nfsd4_session
) > PAGE_SIZE
);
587 mem
= numslots
* sizeof(struct nfsd4_slot
*);
589 new = kzalloc(sizeof(*new) + mem
, GFP_KERNEL
);
592 /* allocate each struct nfsd4_slot and data cache in one piece */
593 for (i
= 0; i
< numslots
; i
++) {
594 mem
= sizeof(struct nfsd4_slot
) + slotsize
;
595 new->se_slots
[i
] = kzalloc(mem
, GFP_KERNEL
);
596 if (!new->se_slots
[i
])
602 kfree(new->se_slots
[i
]);
607 static void init_forechannel_attrs(struct nfsd4_channel_attrs
*new, struct nfsd4_channel_attrs
*req
, int numslots
, int slotsize
)
609 u32 maxrpc
= nfsd_serv
->sv_max_mesg
;
611 new->maxreqs
= numslots
;
612 new->maxresp_cached
= min_t(u32
, req
->maxresp_cached
,
613 slotsize
+ NFSD_MIN_HDR_SEQ_SZ
);
614 new->maxreq_sz
= min_t(u32
, req
->maxreq_sz
, maxrpc
);
615 new->maxresp_sz
= min_t(u32
, req
->maxresp_sz
, maxrpc
);
616 new->maxops
= min_t(u32
, req
->maxops
, NFSD_MAX_OPS_PER_COMPOUND
);
619 static void free_conn(struct nfsd4_conn
*c
)
621 svc_xprt_put(c
->cn_xprt
);
625 static void nfsd4_conn_lost(struct svc_xpt_user
*u
)
627 struct nfsd4_conn
*c
= container_of(u
, struct nfsd4_conn
, cn_xpt_user
);
628 struct nfs4_client
*clp
= c
->cn_session
->se_client
;
630 spin_lock(&clp
->cl_lock
);
631 if (!list_empty(&c
->cn_persession
)) {
632 list_del(&c
->cn_persession
);
635 spin_unlock(&clp
->cl_lock
);
636 nfsd4_probe_callback(clp
);
639 static struct nfsd4_conn
*alloc_conn(struct svc_rqst
*rqstp
, u32 flags
)
641 struct nfsd4_conn
*conn
;
643 conn
= kmalloc(sizeof(struct nfsd4_conn
), GFP_KERNEL
);
646 svc_xprt_get(rqstp
->rq_xprt
);
647 conn
->cn_xprt
= rqstp
->rq_xprt
;
648 conn
->cn_flags
= flags
;
649 INIT_LIST_HEAD(&conn
->cn_xpt_user
.list
);
653 static void __nfsd4_hash_conn(struct nfsd4_conn
*conn
, struct nfsd4_session
*ses
)
655 conn
->cn_session
= ses
;
656 list_add(&conn
->cn_persession
, &ses
->se_conns
);
659 static void nfsd4_hash_conn(struct nfsd4_conn
*conn
, struct nfsd4_session
*ses
)
661 struct nfs4_client
*clp
= ses
->se_client
;
663 spin_lock(&clp
->cl_lock
);
664 __nfsd4_hash_conn(conn
, ses
);
665 spin_unlock(&clp
->cl_lock
);
668 static int nfsd4_register_conn(struct nfsd4_conn
*conn
)
670 conn
->cn_xpt_user
.callback
= nfsd4_conn_lost
;
671 return register_xpt_user(conn
->cn_xprt
, &conn
->cn_xpt_user
);
674 static __be32
nfsd4_new_conn(struct svc_rqst
*rqstp
, struct nfsd4_session
*ses
, u32 dir
)
676 struct nfsd4_conn
*conn
;
679 conn
= alloc_conn(rqstp
, dir
);
681 return nfserr_jukebox
;
682 nfsd4_hash_conn(conn
, ses
);
683 ret
= nfsd4_register_conn(conn
);
685 /* oops; xprt is already down: */
686 nfsd4_conn_lost(&conn
->cn_xpt_user
);
690 static __be32
nfsd4_new_conn_from_crses(struct svc_rqst
*rqstp
, struct nfsd4_session
*ses
)
692 u32 dir
= NFS4_CDFC4_FORE
;
694 if (ses
->se_flags
& SESSION4_BACK_CHAN
)
695 dir
|= NFS4_CDFC4_BACK
;
697 return nfsd4_new_conn(rqstp
, ses
, dir
);
700 /* must be called under client_lock */
701 static void nfsd4_del_conns(struct nfsd4_session
*s
)
703 struct nfs4_client
*clp
= s
->se_client
;
704 struct nfsd4_conn
*c
;
706 spin_lock(&clp
->cl_lock
);
707 while (!list_empty(&s
->se_conns
)) {
708 c
= list_first_entry(&s
->se_conns
, struct nfsd4_conn
, cn_persession
);
709 list_del_init(&c
->cn_persession
);
710 spin_unlock(&clp
->cl_lock
);
712 unregister_xpt_user(c
->cn_xprt
, &c
->cn_xpt_user
);
715 spin_lock(&clp
->cl_lock
);
717 spin_unlock(&clp
->cl_lock
);
720 void free_session(struct kref
*kref
)
722 struct nfsd4_session
*ses
;
725 ses
= container_of(kref
, struct nfsd4_session
, se_ref
);
726 nfsd4_del_conns(ses
);
727 spin_lock(&nfsd_drc_lock
);
728 mem
= ses
->se_fchannel
.maxreqs
* slot_bytes(&ses
->se_fchannel
);
729 nfsd_drc_mem_used
-= mem
;
730 spin_unlock(&nfsd_drc_lock
);
731 free_session_slots(ses
);
735 static struct nfsd4_session
*alloc_init_session(struct svc_rqst
*rqstp
, struct nfs4_client
*clp
, struct nfsd4_create_session
*cses
)
737 struct nfsd4_session
*new;
738 struct nfsd4_channel_attrs
*fchan
= &cses
->fore_channel
;
739 int numslots
, slotsize
;
744 * Note decreasing slot size below client's request may
745 * make it difficult for client to function correctly, whereas
746 * decreasing the number of slots will (just?) affect
747 * performance. When short on memory we therefore prefer to
748 * decrease number of slots instead of their size.
750 slotsize
= nfsd4_sanitize_slot_size(fchan
->maxresp_cached
);
751 numslots
= nfsd4_get_drc_mem(slotsize
, fchan
->maxreqs
);
755 new = alloc_session(slotsize
, numslots
);
757 nfsd4_put_drc_mem(slotsize
, fchan
->maxreqs
);
760 init_forechannel_attrs(&new->se_fchannel
, fchan
, numslots
, slotsize
);
762 new->se_client
= clp
;
765 INIT_LIST_HEAD(&new->se_conns
);
767 new->se_cb_seq_nr
= 1;
768 new->se_flags
= cses
->flags
;
769 new->se_cb_prog
= cses
->callback_prog
;
770 kref_init(&new->se_ref
);
771 idx
= hash_sessionid(&new->se_sessionid
);
772 spin_lock(&client_lock
);
773 list_add(&new->se_hash
, &sessionid_hashtbl
[idx
]);
774 spin_lock(&clp
->cl_lock
);
775 list_add(&new->se_perclnt
, &clp
->cl_sessions
);
776 spin_unlock(&clp
->cl_lock
);
777 spin_unlock(&client_lock
);
779 status
= nfsd4_new_conn_from_crses(rqstp
, new);
780 /* whoops: benny points out, status is ignored! (err, or bogus) */
782 free_session(&new->se_ref
);
785 if (cses
->flags
& SESSION4_BACK_CHAN
) {
786 struct sockaddr
*sa
= svc_addr(rqstp
);
788 * This is a little silly; with sessions there's no real
789 * use for the callback address. Use the peer address
790 * as a reasonable default for now, but consider fixing
791 * the rpc client not to require an address in the
794 rpc_copy_addr((struct sockaddr
*)&clp
->cl_cb_conn
.cb_addr
, sa
);
795 clp
->cl_cb_conn
.cb_addrlen
= svc_addr_len(sa
);
797 nfsd4_probe_callback(clp
);
801 /* caller must hold client_lock */
802 static struct nfsd4_session
*
803 find_in_sessionid_hashtbl(struct nfs4_sessionid
*sessionid
)
805 struct nfsd4_session
*elem
;
808 dump_sessionid(__func__
, sessionid
);
809 idx
= hash_sessionid(sessionid
);
810 /* Search in the appropriate list */
811 list_for_each_entry(elem
, &sessionid_hashtbl
[idx
], se_hash
) {
812 if (!memcmp(elem
->se_sessionid
.data
, sessionid
->data
,
813 NFS4_MAX_SESSIONID_LEN
)) {
818 dprintk("%s: session not found\n", __func__
);
822 /* caller must hold client_lock */
824 unhash_session(struct nfsd4_session
*ses
)
826 list_del(&ses
->se_hash
);
827 spin_lock(&ses
->se_client
->cl_lock
);
828 list_del(&ses
->se_perclnt
);
829 spin_unlock(&ses
->se_client
->cl_lock
);
832 /* must be called under the client_lock */
834 renew_client_locked(struct nfs4_client
*clp
)
836 if (is_client_expired(clp
)) {
837 dprintk("%s: client (clientid %08x/%08x) already expired\n",
839 clp
->cl_clientid
.cl_boot
,
840 clp
->cl_clientid
.cl_id
);
845 * Move client to the end to the LRU list.
847 dprintk("renewing client (clientid %08x/%08x)\n",
848 clp
->cl_clientid
.cl_boot
,
849 clp
->cl_clientid
.cl_id
);
850 list_move_tail(&clp
->cl_lru
, &client_lru
);
851 clp
->cl_time
= get_seconds();
855 renew_client(struct nfs4_client
*clp
)
857 spin_lock(&client_lock
);
858 renew_client_locked(clp
);
859 spin_unlock(&client_lock
);
862 /* SETCLIENTID and SETCLIENTID_CONFIRM Helper functions */
864 STALE_CLIENTID(clientid_t
*clid
)
866 if (clid
->cl_boot
== boot_time
)
868 dprintk("NFSD stale clientid (%08x/%08x) boot_time %08lx\n",
869 clid
->cl_boot
, clid
->cl_id
, boot_time
);
874 * XXX Should we use a slab cache ?
875 * This type of memory management is somewhat inefficient, but we use it
876 * anyway since SETCLIENTID is not a common operation.
878 static struct nfs4_client
*alloc_client(struct xdr_netobj name
)
880 struct nfs4_client
*clp
;
882 clp
= kzalloc(sizeof(struct nfs4_client
), GFP_KERNEL
);
885 clp
->cl_name
.data
= kmalloc(name
.len
, GFP_KERNEL
);
886 if (clp
->cl_name
.data
== NULL
) {
890 memcpy(clp
->cl_name
.data
, name
.data
, name
.len
);
891 clp
->cl_name
.len
= name
.len
;
896 free_client(struct nfs4_client
*clp
)
898 while (!list_empty(&clp
->cl_sessions
)) {
899 struct nfsd4_session
*ses
;
900 ses
= list_entry(clp
->cl_sessions
.next
, struct nfsd4_session
,
902 list_del(&ses
->se_perclnt
);
903 nfsd4_put_session(ses
);
905 if (clp
->cl_cred
.cr_group_info
)
906 put_group_info(clp
->cl_cred
.cr_group_info
);
907 kfree(clp
->cl_principal
);
908 kfree(clp
->cl_name
.data
);
913 release_session_client(struct nfsd4_session
*session
)
915 struct nfs4_client
*clp
= session
->se_client
;
917 if (!atomic_dec_and_lock(&clp
->cl_refcount
, &client_lock
))
919 if (is_client_expired(clp
)) {
921 session
->se_client
= NULL
;
923 renew_client_locked(clp
);
924 spin_unlock(&client_lock
);
927 /* must be called under the client_lock */
929 unhash_client_locked(struct nfs4_client
*clp
)
931 struct nfsd4_session
*ses
;
933 mark_client_expired(clp
);
934 list_del(&clp
->cl_lru
);
935 spin_lock(&clp
->cl_lock
);
936 list_for_each_entry(ses
, &clp
->cl_sessions
, se_perclnt
)
937 list_del_init(&ses
->se_hash
);
938 spin_unlock(&clp
->cl_lock
);
942 expire_client(struct nfs4_client
*clp
)
944 struct nfs4_stateowner
*sop
;
945 struct nfs4_delegation
*dp
;
946 struct list_head reaplist
;
948 INIT_LIST_HEAD(&reaplist
);
949 spin_lock(&recall_lock
);
950 while (!list_empty(&clp
->cl_delegations
)) {
951 dp
= list_entry(clp
->cl_delegations
.next
, struct nfs4_delegation
, dl_perclnt
);
952 list_del_init(&dp
->dl_perclnt
);
953 list_move(&dp
->dl_recall_lru
, &reaplist
);
955 spin_unlock(&recall_lock
);
956 while (!list_empty(&reaplist
)) {
957 dp
= list_entry(reaplist
.next
, struct nfs4_delegation
, dl_recall_lru
);
958 list_del_init(&dp
->dl_recall_lru
);
959 unhash_delegation(dp
);
961 while (!list_empty(&clp
->cl_openowners
)) {
962 sop
= list_entry(clp
->cl_openowners
.next
, struct nfs4_stateowner
, so_perclient
);
963 release_openowner(sop
);
965 nfsd4_shutdown_callback(clp
);
966 if (clp
->cl_cb_conn
.cb_xprt
)
967 svc_xprt_put(clp
->cl_cb_conn
.cb_xprt
);
968 list_del(&clp
->cl_idhash
);
969 list_del(&clp
->cl_strhash
);
970 spin_lock(&client_lock
);
971 unhash_client_locked(clp
);
972 if (atomic_read(&clp
->cl_refcount
) == 0)
974 spin_unlock(&client_lock
);
977 static void copy_verf(struct nfs4_client
*target
, nfs4_verifier
*source
)
979 memcpy(target
->cl_verifier
.data
, source
->data
,
980 sizeof(target
->cl_verifier
.data
));
983 static void copy_clid(struct nfs4_client
*target
, struct nfs4_client
*source
)
985 target
->cl_clientid
.cl_boot
= source
->cl_clientid
.cl_boot
;
986 target
->cl_clientid
.cl_id
= source
->cl_clientid
.cl_id
;
989 static void copy_cred(struct svc_cred
*target
, struct svc_cred
*source
)
991 target
->cr_uid
= source
->cr_uid
;
992 target
->cr_gid
= source
->cr_gid
;
993 target
->cr_group_info
= source
->cr_group_info
;
994 get_group_info(target
->cr_group_info
);
997 static int same_name(const char *n1
, const char *n2
)
999 return 0 == memcmp(n1
, n2
, HEXDIR_LEN
);
1003 same_verf(nfs4_verifier
*v1
, nfs4_verifier
*v2
)
1005 return 0 == memcmp(v1
->data
, v2
->data
, sizeof(v1
->data
));
1009 same_clid(clientid_t
*cl1
, clientid_t
*cl2
)
1011 return (cl1
->cl_boot
== cl2
->cl_boot
) && (cl1
->cl_id
== cl2
->cl_id
);
1014 /* XXX what about NGROUP */
1016 same_creds(struct svc_cred
*cr1
, struct svc_cred
*cr2
)
1018 return cr1
->cr_uid
== cr2
->cr_uid
;
1021 static void gen_clid(struct nfs4_client
*clp
)
1023 static u32 current_clientid
= 1;
1025 clp
->cl_clientid
.cl_boot
= boot_time
;
1026 clp
->cl_clientid
.cl_id
= current_clientid
++;
1029 static void gen_confirm(struct nfs4_client
*clp
)
1034 p
= (u32
*)clp
->cl_confirm
.data
;
1035 *p
++ = get_seconds();
1039 static struct nfs4_client
*create_client(struct xdr_netobj name
, char *recdir
,
1040 struct svc_rqst
*rqstp
, nfs4_verifier
*verf
)
1042 struct nfs4_client
*clp
;
1043 struct sockaddr
*sa
= svc_addr(rqstp
);
1046 clp
= alloc_client(name
);
1050 INIT_LIST_HEAD(&clp
->cl_sessions
);
1052 princ
= svc_gss_principal(rqstp
);
1054 clp
->cl_principal
= kstrdup(princ
, GFP_KERNEL
);
1055 if (clp
->cl_principal
== NULL
) {
1061 memcpy(clp
->cl_recdir
, recdir
, HEXDIR_LEN
);
1062 atomic_set(&clp
->cl_refcount
, 0);
1063 clp
->cl_cb_state
= NFSD4_CB_UNKNOWN
;
1064 INIT_LIST_HEAD(&clp
->cl_idhash
);
1065 INIT_LIST_HEAD(&clp
->cl_strhash
);
1066 INIT_LIST_HEAD(&clp
->cl_openowners
);
1067 INIT_LIST_HEAD(&clp
->cl_delegations
);
1068 INIT_LIST_HEAD(&clp
->cl_lru
);
1069 INIT_LIST_HEAD(&clp
->cl_callbacks
);
1070 spin_lock_init(&clp
->cl_lock
);
1071 INIT_WORK(&clp
->cl_cb_null
.cb_work
, nfsd4_do_callback_rpc
);
1072 clp
->cl_time
= get_seconds();
1073 clear_bit(0, &clp
->cl_cb_slot_busy
);
1074 rpc_init_wait_queue(&clp
->cl_cb_waitq
, "Backchannel slot table");
1075 copy_verf(clp
, verf
);
1076 rpc_copy_addr((struct sockaddr
*) &clp
->cl_addr
, sa
);
1077 clp
->cl_flavor
= rqstp
->rq_flavor
;
1078 copy_cred(&clp
->cl_cred
, &rqstp
->rq_cred
);
1080 clp
->cl_cb_session
= NULL
;
1084 static int check_name(struct xdr_netobj name
)
1088 if (name
.len
> NFS4_OPAQUE_LIMIT
) {
1089 dprintk("NFSD: check_name: name too long(%d)!\n", name
.len
);
1096 add_to_unconfirmed(struct nfs4_client
*clp
, unsigned int strhashval
)
1098 unsigned int idhashval
;
1100 list_add(&clp
->cl_strhash
, &unconf_str_hashtbl
[strhashval
]);
1101 idhashval
= clientid_hashval(clp
->cl_clientid
.cl_id
);
1102 list_add(&clp
->cl_idhash
, &unconf_id_hashtbl
[idhashval
]);
1107 move_to_confirmed(struct nfs4_client
*clp
)
1109 unsigned int idhashval
= clientid_hashval(clp
->cl_clientid
.cl_id
);
1110 unsigned int strhashval
;
1112 dprintk("NFSD: move_to_confirm nfs4_client %p\n", clp
);
1113 list_move(&clp
->cl_idhash
, &conf_id_hashtbl
[idhashval
]);
1114 strhashval
= clientstr_hashval(clp
->cl_recdir
);
1115 list_move(&clp
->cl_strhash
, &conf_str_hashtbl
[strhashval
]);
1119 static struct nfs4_client
*
1120 find_confirmed_client(clientid_t
*clid
)
1122 struct nfs4_client
*clp
;
1123 unsigned int idhashval
= clientid_hashval(clid
->cl_id
);
1125 list_for_each_entry(clp
, &conf_id_hashtbl
[idhashval
], cl_idhash
) {
1126 if (same_clid(&clp
->cl_clientid
, clid
))
1132 static struct nfs4_client
*
1133 find_unconfirmed_client(clientid_t
*clid
)
1135 struct nfs4_client
*clp
;
1136 unsigned int idhashval
= clientid_hashval(clid
->cl_id
);
1138 list_for_each_entry(clp
, &unconf_id_hashtbl
[idhashval
], cl_idhash
) {
1139 if (same_clid(&clp
->cl_clientid
, clid
))
1145 static bool clp_used_exchangeid(struct nfs4_client
*clp
)
1147 return clp
->cl_exchange_flags
!= 0;
1150 static struct nfs4_client
*
1151 find_confirmed_client_by_str(const char *dname
, unsigned int hashval
)
1153 struct nfs4_client
*clp
;
1155 list_for_each_entry(clp
, &conf_str_hashtbl
[hashval
], cl_strhash
) {
1156 if (same_name(clp
->cl_recdir
, dname
))
1162 static struct nfs4_client
*
1163 find_unconfirmed_client_by_str(const char *dname
, unsigned int hashval
)
1165 struct nfs4_client
*clp
;
1167 list_for_each_entry(clp
, &unconf_str_hashtbl
[hashval
], cl_strhash
) {
1168 if (same_name(clp
->cl_recdir
, dname
))
1174 static void rpc_svcaddr2sockaddr(struct sockaddr
*sa
, unsigned short family
, union svc_addr_u
*svcaddr
)
1178 ((struct sockaddr_in
*)sa
)->sin_family
= AF_INET
;
1179 ((struct sockaddr_in
*)sa
)->sin_addr
= svcaddr
->addr
;
1182 ((struct sockaddr_in6
*)sa
)->sin6_family
= AF_INET6
;
1183 ((struct sockaddr_in6
*)sa
)->sin6_addr
= svcaddr
->addr6
;
1189 gen_callback(struct nfs4_client
*clp
, struct nfsd4_setclientid
*se
, struct svc_rqst
*rqstp
)
1191 struct nfs4_cb_conn
*conn
= &clp
->cl_cb_conn
;
1192 struct sockaddr
*sa
= svc_addr(rqstp
);
1193 u32 scopeid
= rpc_get_scope_id(sa
);
1194 unsigned short expected_family
;
1196 /* Currently, we only support tcp and tcp6 for the callback channel */
1197 if (se
->se_callback_netid_len
== 3 &&
1198 !memcmp(se
->se_callback_netid_val
, "tcp", 3))
1199 expected_family
= AF_INET
;
1200 else if (se
->se_callback_netid_len
== 4 &&
1201 !memcmp(se
->se_callback_netid_val
, "tcp6", 4))
1202 expected_family
= AF_INET6
;
1206 conn
->cb_addrlen
= rpc_uaddr2sockaddr(se
->se_callback_addr_val
,
1207 se
->se_callback_addr_len
,
1208 (struct sockaddr
*)&conn
->cb_addr
,
1209 sizeof(conn
->cb_addr
));
1211 if (!conn
->cb_addrlen
|| conn
->cb_addr
.ss_family
!= expected_family
)
1214 if (conn
->cb_addr
.ss_family
== AF_INET6
)
1215 ((struct sockaddr_in6
*)&conn
->cb_addr
)->sin6_scope_id
= scopeid
;
1217 conn
->cb_prog
= se
->se_callback_prog
;
1218 conn
->cb_ident
= se
->se_callback_ident
;
1219 rpc_svcaddr2sockaddr((struct sockaddr
*)&conn
->cb_saddr
, expected_family
, &rqstp
->rq_daddr
);
1222 conn
->cb_addr
.ss_family
= AF_UNSPEC
;
1223 conn
->cb_addrlen
= 0;
1224 dprintk(KERN_INFO
"NFSD: this client (clientid %08x/%08x) "
1225 "will not receive delegations\n",
1226 clp
->cl_clientid
.cl_boot
, clp
->cl_clientid
.cl_id
);
1232 * Cache a reply. nfsd4_check_drc_limit() has bounded the cache size.
1235 nfsd4_store_cache_entry(struct nfsd4_compoundres
*resp
)
1237 struct nfsd4_slot
*slot
= resp
->cstate
.slot
;
1240 dprintk("--> %s slot %p\n", __func__
, slot
);
1242 slot
->sl_opcnt
= resp
->opcnt
;
1243 slot
->sl_status
= resp
->cstate
.status
;
1245 if (nfsd4_not_cached(resp
)) {
1246 slot
->sl_datalen
= 0;
1249 slot
->sl_datalen
= (char *)resp
->p
- (char *)resp
->cstate
.datap
;
1250 base
= (char *)resp
->cstate
.datap
-
1251 (char *)resp
->xbuf
->head
[0].iov_base
;
1252 if (read_bytes_from_xdr_buf(resp
->xbuf
, base
, slot
->sl_data
,
1254 WARN("%s: sessions DRC could not cache compound\n", __func__
);
1259 * Encode the replay sequence operation from the slot values.
1260 * If cachethis is FALSE encode the uncached rep error on the next
1261 * operation which sets resp->p and increments resp->opcnt for
1262 * nfs4svc_encode_compoundres.
1266 nfsd4_enc_sequence_replay(struct nfsd4_compoundargs
*args
,
1267 struct nfsd4_compoundres
*resp
)
1269 struct nfsd4_op
*op
;
1270 struct nfsd4_slot
*slot
= resp
->cstate
.slot
;
1272 dprintk("--> %s resp->opcnt %d cachethis %u \n", __func__
,
1273 resp
->opcnt
, resp
->cstate
.slot
->sl_cachethis
);
1275 /* Encode the replayed sequence operation */
1276 op
= &args
->ops
[resp
->opcnt
- 1];
1277 nfsd4_encode_operation(resp
, op
);
1279 /* Return nfserr_retry_uncached_rep in next operation. */
1280 if (args
->opcnt
> 1 && slot
->sl_cachethis
== 0) {
1281 op
= &args
->ops
[resp
->opcnt
++];
1282 op
->status
= nfserr_retry_uncached_rep
;
1283 nfsd4_encode_operation(resp
, op
);
1289 * The sequence operation is not cached because we can use the slot and
1293 nfsd4_replay_cache_entry(struct nfsd4_compoundres
*resp
,
1294 struct nfsd4_sequence
*seq
)
1296 struct nfsd4_slot
*slot
= resp
->cstate
.slot
;
1299 dprintk("--> %s slot %p\n", __func__
, slot
);
1301 /* Either returns 0 or nfserr_retry_uncached */
1302 status
= nfsd4_enc_sequence_replay(resp
->rqstp
->rq_argp
, resp
);
1303 if (status
== nfserr_retry_uncached_rep
)
1306 /* The sequence operation has been encoded, cstate->datap set. */
1307 memcpy(resp
->cstate
.datap
, slot
->sl_data
, slot
->sl_datalen
);
1309 resp
->opcnt
= slot
->sl_opcnt
;
1310 resp
->p
= resp
->cstate
.datap
+ XDR_QUADLEN(slot
->sl_datalen
);
1311 status
= slot
->sl_status
;
1317 * Set the exchange_id flags returned by the server.
1320 nfsd4_set_ex_flags(struct nfs4_client
*new, struct nfsd4_exchange_id
*clid
)
1322 /* pNFS is not supported */
1323 new->cl_exchange_flags
|= EXCHGID4_FLAG_USE_NON_PNFS
;
1325 /* Referrals are supported, Migration is not. */
1326 new->cl_exchange_flags
|= EXCHGID4_FLAG_SUPP_MOVED_REFER
;
1328 /* set the wire flags to return to client. */
1329 clid
->flags
= new->cl_exchange_flags
;
1333 nfsd4_exchange_id(struct svc_rqst
*rqstp
,
1334 struct nfsd4_compound_state
*cstate
,
1335 struct nfsd4_exchange_id
*exid
)
1337 struct nfs4_client
*unconf
, *conf
, *new;
1339 unsigned int strhashval
;
1340 char dname
[HEXDIR_LEN
];
1341 char addr_str
[INET6_ADDRSTRLEN
];
1342 nfs4_verifier verf
= exid
->verifier
;
1343 struct sockaddr
*sa
= svc_addr(rqstp
);
1345 rpc_ntop(sa
, addr_str
, sizeof(addr_str
));
1346 dprintk("%s rqstp=%p exid=%p clname.len=%u clname.data=%p "
1347 "ip_addr=%s flags %x, spa_how %d\n",
1348 __func__
, rqstp
, exid
, exid
->clname
.len
, exid
->clname
.data
,
1349 addr_str
, exid
->flags
, exid
->spa_how
);
1351 if (!check_name(exid
->clname
) || (exid
->flags
& ~EXCHGID4_FLAG_MASK_A
))
1352 return nfserr_inval
;
1354 /* Currently only support SP4_NONE */
1355 switch (exid
->spa_how
) {
1359 return nfserr_serverfault
;
1361 BUG(); /* checked by xdr code */
1363 return nfserr_serverfault
; /* no excuse :-/ */
1366 status
= nfs4_make_rec_clidname(dname
, &exid
->clname
);
1371 strhashval
= clientstr_hashval(dname
);
1376 conf
= find_confirmed_client_by_str(dname
, strhashval
);
1378 if (!clp_used_exchangeid(conf
)) {
1379 status
= nfserr_clid_inuse
; /* XXX: ? */
1382 if (!same_verf(&verf
, &conf
->cl_verifier
)) {
1383 /* 18.35.4 case 8 */
1384 if (exid
->flags
& EXCHGID4_FLAG_UPD_CONFIRMED_REC_A
) {
1385 status
= nfserr_not_same
;
1388 /* Client reboot: destroy old state */
1389 expire_client(conf
);
1392 if (!same_creds(&conf
->cl_cred
, &rqstp
->rq_cred
)) {
1393 /* 18.35.4 case 9 */
1394 if (exid
->flags
& EXCHGID4_FLAG_UPD_CONFIRMED_REC_A
) {
1395 status
= nfserr_perm
;
1398 expire_client(conf
);
1402 * Set bit when the owner id and verifier map to an already
1403 * confirmed client id (18.35.3).
1405 exid
->flags
|= EXCHGID4_FLAG_CONFIRMED_R
;
1408 * Falling into 18.35.4 case 2, possible router replay.
1409 * Leave confirmed record intact and return same result.
1411 copy_verf(conf
, &verf
);
1416 /* 18.35.4 case 7 */
1417 if (exid
->flags
& EXCHGID4_FLAG_UPD_CONFIRMED_REC_A
) {
1418 status
= nfserr_noent
;
1422 unconf
= find_unconfirmed_client_by_str(dname
, strhashval
);
1425 * Possible retry or client restart. Per 18.35.4 case 4,
1426 * a new unconfirmed record should be generated regardless
1427 * of whether any properties have changed.
1429 expire_client(unconf
);
1434 new = create_client(exid
->clname
, dname
, rqstp
, &verf
);
1436 status
= nfserr_jukebox
;
1441 add_to_unconfirmed(new, strhashval
);
1443 exid
->clientid
.cl_boot
= new->cl_clientid
.cl_boot
;
1444 exid
->clientid
.cl_id
= new->cl_clientid
.cl_id
;
1447 nfsd4_set_ex_flags(new, exid
);
1449 dprintk("nfsd4_exchange_id seqid %d flags %x\n",
1450 new->cl_cs_slot
.sl_seqid
, new->cl_exchange_flags
);
1454 nfs4_unlock_state();
1456 dprintk("nfsd4_exchange_id returns %d\n", ntohl(status
));
1461 check_slot_seqid(u32 seqid
, u32 slot_seqid
, int slot_inuse
)
1463 dprintk("%s enter. seqid %d slot_seqid %d\n", __func__
, seqid
,
1466 /* The slot is in use, and no response has been sent. */
1468 if (seqid
== slot_seqid
)
1469 return nfserr_jukebox
;
1471 return nfserr_seq_misordered
;
1474 if (likely(seqid
== slot_seqid
+ 1))
1477 if (seqid
== slot_seqid
)
1478 return nfserr_replay_cache
;
1480 if (seqid
== 1 && (slot_seqid
+ 1) == 0)
1482 /* Misordered replay or misordered new request */
1483 return nfserr_seq_misordered
;
1487 * Cache the create session result into the create session single DRC
1488 * slot cache by saving the xdr structure. sl_seqid has been set.
1489 * Do this for solo or embedded create session operations.
1492 nfsd4_cache_create_session(struct nfsd4_create_session
*cr_ses
,
1493 struct nfsd4_clid_slot
*slot
, int nfserr
)
1495 slot
->sl_status
= nfserr
;
1496 memcpy(&slot
->sl_cr_ses
, cr_ses
, sizeof(*cr_ses
));
1500 nfsd4_replay_create_session(struct nfsd4_create_session
*cr_ses
,
1501 struct nfsd4_clid_slot
*slot
)
1503 memcpy(cr_ses
, &slot
->sl_cr_ses
, sizeof(*cr_ses
));
1504 return slot
->sl_status
;
1507 #define NFSD_MIN_REQ_HDR_SEQ_SZ ((\
1508 2 * 2 + /* credential,verifier: AUTH_NULL, length 0 */ \
1509 1 + /* MIN tag is length with zero, only length */ \
1510 3 + /* version, opcount, opcode */ \
1511 XDR_QUADLEN(NFS4_MAX_SESSIONID_LEN) + \
1512 /* seqid, slotID, slotID, cache */ \
1513 4 ) * sizeof(__be32))
1515 #define NFSD_MIN_RESP_HDR_SEQ_SZ ((\
1516 2 + /* verifier: AUTH_NULL, length 0 */\
1518 1 + /* MIN tag is length with zero, only length */ \
1519 3 + /* opcount, opcode, opstatus*/ \
1520 XDR_QUADLEN(NFS4_MAX_SESSIONID_LEN) + \
1521 /* seqid, slotID, slotID, slotID, status */ \
1522 5 ) * sizeof(__be32))
1524 static __be32
check_forechannel_attrs(struct nfsd4_channel_attrs fchannel
)
1526 return fchannel
.maxreq_sz
< NFSD_MIN_REQ_HDR_SEQ_SZ
1527 || fchannel
.maxresp_sz
< NFSD_MIN_RESP_HDR_SEQ_SZ
;
1531 nfsd4_create_session(struct svc_rqst
*rqstp
,
1532 struct nfsd4_compound_state
*cstate
,
1533 struct nfsd4_create_session
*cr_ses
)
1535 struct sockaddr
*sa
= svc_addr(rqstp
);
1536 struct nfs4_client
*conf
, *unconf
;
1537 struct nfsd4_session
*new;
1538 struct nfsd4_clid_slot
*cs_slot
= NULL
;
1539 bool confirm_me
= false;
1542 if (cr_ses
->flags
& ~SESSION4_FLAG_MASK_A
)
1543 return nfserr_inval
;
1546 unconf
= find_unconfirmed_client(&cr_ses
->clientid
);
1547 conf
= find_confirmed_client(&cr_ses
->clientid
);
1550 cs_slot
= &conf
->cl_cs_slot
;
1551 status
= check_slot_seqid(cr_ses
->seqid
, cs_slot
->sl_seqid
, 0);
1552 if (status
== nfserr_replay_cache
) {
1553 dprintk("Got a create_session replay! seqid= %d\n",
1555 /* Return the cached reply status */
1556 status
= nfsd4_replay_create_session(cr_ses
, cs_slot
);
1558 } else if (cr_ses
->seqid
!= cs_slot
->sl_seqid
+ 1) {
1559 status
= nfserr_seq_misordered
;
1560 dprintk("Sequence misordered!\n");
1561 dprintk("Expected seqid= %d but got seqid= %d\n",
1562 cs_slot
->sl_seqid
, cr_ses
->seqid
);
1565 } else if (unconf
) {
1566 if (!same_creds(&unconf
->cl_cred
, &rqstp
->rq_cred
) ||
1567 !rpc_cmp_addr(sa
, (struct sockaddr
*) &unconf
->cl_addr
)) {
1568 status
= nfserr_clid_inuse
;
1572 cs_slot
= &unconf
->cl_cs_slot
;
1573 status
= check_slot_seqid(cr_ses
->seqid
, cs_slot
->sl_seqid
, 0);
1575 /* an unconfirmed replay returns misordered */
1576 status
= nfserr_seq_misordered
;
1583 status
= nfserr_stale_clientid
;
1588 * XXX: we should probably set this at creation time, and check
1589 * for consistent minorversion use throughout:
1591 conf
->cl_minorversion
= 1;
1593 * We do not support RDMA or persistent sessions
1595 cr_ses
->flags
&= ~SESSION4_PERSIST
;
1596 cr_ses
->flags
&= ~SESSION4_RDMA
;
1598 status
= nfserr_toosmall
;
1599 if (check_forechannel_attrs(cr_ses
->fore_channel
))
1602 status
= nfserr_jukebox
;
1603 new = alloc_init_session(rqstp
, conf
, cr_ses
);
1607 memcpy(cr_ses
->sessionid
.data
, new->se_sessionid
.data
,
1608 NFS4_MAX_SESSIONID_LEN
);
1609 memcpy(&cr_ses
->fore_channel
, &new->se_fchannel
,
1610 sizeof(struct nfsd4_channel_attrs
));
1611 cs_slot
->sl_seqid
++;
1612 cr_ses
->seqid
= cs_slot
->sl_seqid
;
1614 /* cache solo and embedded create sessions under the state lock */
1615 nfsd4_cache_create_session(cr_ses
, cs_slot
, status
);
1617 move_to_confirmed(conf
);
1619 nfs4_unlock_state();
1620 dprintk("%s returns %d\n", __func__
, ntohl(status
));
1624 static bool nfsd4_last_compound_op(struct svc_rqst
*rqstp
)
1626 struct nfsd4_compoundres
*resp
= rqstp
->rq_resp
;
1627 struct nfsd4_compoundargs
*argp
= rqstp
->rq_argp
;
1629 return argp
->opcnt
== resp
->opcnt
;
1632 static __be32
nfsd4_map_bcts_dir(u32
*dir
)
1635 case NFS4_CDFC4_FORE
:
1636 case NFS4_CDFC4_BACK
:
1638 case NFS4_CDFC4_FORE_OR_BOTH
:
1639 case NFS4_CDFC4_BACK_OR_BOTH
:
1640 *dir
= NFS4_CDFC4_BOTH
;
1643 return nfserr_inval
;
1646 __be32
nfsd4_bind_conn_to_session(struct svc_rqst
*rqstp
,
1647 struct nfsd4_compound_state
*cstate
,
1648 struct nfsd4_bind_conn_to_session
*bcts
)
1652 if (!nfsd4_last_compound_op(rqstp
))
1653 return nfserr_not_only_op
;
1654 spin_lock(&client_lock
);
1655 cstate
->session
= find_in_sessionid_hashtbl(&bcts
->sessionid
);
1656 /* Sorta weird: we only need the refcnt'ing because new_conn acquires
1657 * client_lock iself: */
1658 if (cstate
->session
) {
1659 nfsd4_get_session(cstate
->session
);
1660 atomic_inc(&cstate
->session
->se_client
->cl_refcount
);
1662 spin_unlock(&client_lock
);
1663 if (!cstate
->session
)
1664 return nfserr_badsession
;
1666 status
= nfsd4_map_bcts_dir(&bcts
->dir
);
1668 nfsd4_new_conn(rqstp
, cstate
->session
, bcts
->dir
);
1672 static bool nfsd4_compound_in_session(struct nfsd4_session
*session
, struct nfs4_sessionid
*sid
)
1676 return !memcmp(sid
, &session
->se_sessionid
, sizeof(*sid
));
1680 nfsd4_destroy_session(struct svc_rqst
*r
,
1681 struct nfsd4_compound_state
*cstate
,
1682 struct nfsd4_destroy_session
*sessionid
)
1684 struct nfsd4_session
*ses
;
1685 u32 status
= nfserr_badsession
;
1688 * - The confirmed nfs4_client->cl_sessionid holds destroyed sessinid
1689 * - Should we return nfserr_back_chan_busy if waiting for
1690 * callbacks on to-be-destroyed session?
1691 * - Do we need to clear any callback info from previous session?
1694 if (nfsd4_compound_in_session(cstate
->session
, &sessionid
->sessionid
)) {
1695 if (!nfsd4_last_compound_op(r
))
1696 return nfserr_not_only_op
;
1698 dump_sessionid(__func__
, &sessionid
->sessionid
);
1699 spin_lock(&client_lock
);
1700 ses
= find_in_sessionid_hashtbl(&sessionid
->sessionid
);
1702 spin_unlock(&client_lock
);
1706 unhash_session(ses
);
1707 spin_unlock(&client_lock
);
1710 nfsd4_probe_callback_sync(ses
->se_client
);
1711 nfs4_unlock_state();
1713 nfsd4_del_conns(ses
);
1715 nfsd4_put_session(ses
);
1718 dprintk("%s returns %d\n", __func__
, ntohl(status
));
1722 static struct nfsd4_conn
*__nfsd4_find_conn(struct svc_xprt
*xpt
, struct nfsd4_session
*s
)
1724 struct nfsd4_conn
*c
;
1726 list_for_each_entry(c
, &s
->se_conns
, cn_persession
) {
1727 if (c
->cn_xprt
== xpt
) {
1734 static void nfsd4_sequence_check_conn(struct nfsd4_conn
*new, struct nfsd4_session
*ses
)
1736 struct nfs4_client
*clp
= ses
->se_client
;
1737 struct nfsd4_conn
*c
;
1740 spin_lock(&clp
->cl_lock
);
1741 c
= __nfsd4_find_conn(new->cn_xprt
, ses
);
1743 spin_unlock(&clp
->cl_lock
);
1747 __nfsd4_hash_conn(new, ses
);
1748 spin_unlock(&clp
->cl_lock
);
1749 ret
= nfsd4_register_conn(new);
1751 /* oops; xprt is already down: */
1752 nfsd4_conn_lost(&new->cn_xpt_user
);
1756 static bool nfsd4_session_too_many_ops(struct svc_rqst
*rqstp
, struct nfsd4_session
*session
)
1758 struct nfsd4_compoundargs
*args
= rqstp
->rq_argp
;
1760 return args
->opcnt
> session
->se_fchannel
.maxops
;
1763 static bool nfsd4_request_too_big(struct svc_rqst
*rqstp
,
1764 struct nfsd4_session
*session
)
1766 struct xdr_buf
*xb
= &rqstp
->rq_arg
;
1768 return xb
->len
> session
->se_fchannel
.maxreq_sz
;
1772 nfsd4_sequence(struct svc_rqst
*rqstp
,
1773 struct nfsd4_compound_state
*cstate
,
1774 struct nfsd4_sequence
*seq
)
1776 struct nfsd4_compoundres
*resp
= rqstp
->rq_resp
;
1777 struct nfsd4_session
*session
;
1778 struct nfsd4_slot
*slot
;
1779 struct nfsd4_conn
*conn
;
1782 if (resp
->opcnt
!= 1)
1783 return nfserr_sequence_pos
;
1786 * Will be either used or freed by nfsd4_sequence_check_conn
1789 conn
= alloc_conn(rqstp
, NFS4_CDFC4_FORE
);
1791 return nfserr_jukebox
;
1793 spin_lock(&client_lock
);
1794 status
= nfserr_badsession
;
1795 session
= find_in_sessionid_hashtbl(&seq
->sessionid
);
1799 status
= nfserr_too_many_ops
;
1800 if (nfsd4_session_too_many_ops(rqstp
, session
))
1803 status
= nfserr_req_too_big
;
1804 if (nfsd4_request_too_big(rqstp
, session
))
1807 status
= nfserr_badslot
;
1808 if (seq
->slotid
>= session
->se_fchannel
.maxreqs
)
1811 slot
= session
->se_slots
[seq
->slotid
];
1812 dprintk("%s: slotid %d\n", __func__
, seq
->slotid
);
1814 /* We do not negotiate the number of slots yet, so set the
1815 * maxslots to the session maxreqs which is used to encode
1816 * sr_highest_slotid and the sr_target_slot id to maxslots */
1817 seq
->maxslots
= session
->se_fchannel
.maxreqs
;
1819 status
= check_slot_seqid(seq
->seqid
, slot
->sl_seqid
, slot
->sl_inuse
);
1820 if (status
== nfserr_replay_cache
) {
1821 cstate
->slot
= slot
;
1822 cstate
->session
= session
;
1823 /* Return the cached reply status and set cstate->status
1824 * for nfsd4_proc_compound processing */
1825 status
= nfsd4_replay_cache_entry(resp
, seq
);
1826 cstate
->status
= nfserr_replay_cache
;
1832 nfsd4_sequence_check_conn(conn
, session
);
1835 /* Success! bump slot seqid */
1836 slot
->sl_inuse
= true;
1837 slot
->sl_seqid
= seq
->seqid
;
1838 slot
->sl_cachethis
= seq
->cachethis
;
1840 cstate
->slot
= slot
;
1841 cstate
->session
= session
;
1844 /* Hold a session reference until done processing the compound. */
1845 if (cstate
->session
) {
1846 struct nfs4_client
*clp
= session
->se_client
;
1848 nfsd4_get_session(cstate
->session
);
1849 atomic_inc(&clp
->cl_refcount
);
1850 if (clp
->cl_cb_state
== NFSD4_CB_DOWN
)
1851 seq
->status_flags
|= SEQ4_STATUS_CB_PATH_DOWN
;
1854 spin_unlock(&client_lock
);
1855 dprintk("%s: return %d\n", __func__
, ntohl(status
));
1860 nfsd4_reclaim_complete(struct svc_rqst
*rqstp
, struct nfsd4_compound_state
*cstate
, struct nfsd4_reclaim_complete
*rc
)
1864 if (rc
->rca_one_fs
) {
1865 if (!cstate
->current_fh
.fh_dentry
)
1866 return nfserr_nofilehandle
;
1868 * We don't take advantage of the rca_one_fs case.
1869 * That's OK, it's optional, we can safely ignore it.
1875 status
= nfserr_complete_already
;
1876 if (cstate
->session
->se_client
->cl_firststate
)
1879 status
= nfserr_stale_clientid
;
1880 if (is_client_expired(cstate
->session
->se_client
))
1882 * The following error isn't really legal.
1883 * But we only get here if the client just explicitly
1884 * destroyed the client. Surely it no longer cares what
1885 * error it gets back on an operation for the dead
1891 nfsd4_create_clid_dir(cstate
->session
->se_client
);
1893 nfs4_unlock_state();
1898 nfsd4_setclientid(struct svc_rqst
*rqstp
, struct nfsd4_compound_state
*cstate
,
1899 struct nfsd4_setclientid
*setclid
)
1901 struct xdr_netobj clname
= {
1902 .len
= setclid
->se_namelen
,
1903 .data
= setclid
->se_name
,
1905 nfs4_verifier clverifier
= setclid
->se_verf
;
1906 unsigned int strhashval
;
1907 struct nfs4_client
*conf
, *unconf
, *new;
1909 char dname
[HEXDIR_LEN
];
1911 if (!check_name(clname
))
1912 return nfserr_inval
;
1914 status
= nfs4_make_rec_clidname(dname
, &clname
);
1919 * XXX The Duplicate Request Cache (DRC) has been checked (??)
1920 * We get here on a DRC miss.
1923 strhashval
= clientstr_hashval(dname
);
1926 conf
= find_confirmed_client_by_str(dname
, strhashval
);
1928 /* RFC 3530 14.2.33 CASE 0: */
1929 status
= nfserr_clid_inuse
;
1930 if (clp_used_exchangeid(conf
))
1932 if (!same_creds(&conf
->cl_cred
, &rqstp
->rq_cred
)) {
1933 char addr_str
[INET6_ADDRSTRLEN
];
1934 rpc_ntop((struct sockaddr
*) &conf
->cl_addr
, addr_str
,
1936 dprintk("NFSD: setclientid: string in use by client "
1937 "at %s\n", addr_str
);
1942 * section 14.2.33 of RFC 3530 (under the heading "IMPLEMENTATION")
1943 * has a description of SETCLIENTID request processing consisting
1944 * of 5 bullet points, labeled as CASE0 - CASE4 below.
1946 unconf
= find_unconfirmed_client_by_str(dname
, strhashval
);
1947 status
= nfserr_resource
;
1950 * RFC 3530 14.2.33 CASE 4:
1951 * placed first, because it is the normal case
1954 expire_client(unconf
);
1955 new = create_client(clname
, dname
, rqstp
, &clverifier
);
1959 } else if (same_verf(&conf
->cl_verifier
, &clverifier
)) {
1961 * RFC 3530 14.2.33 CASE 1:
1962 * probable callback update
1965 /* Note this is removing unconfirmed {*x***},
1966 * which is stronger than RFC recommended {vxc**}.
1967 * This has the advantage that there is at most
1968 * one {*x***} in either list at any time.
1970 expire_client(unconf
);
1972 new = create_client(clname
, dname
, rqstp
, &clverifier
);
1975 copy_clid(new, conf
);
1976 } else if (!unconf
) {
1978 * RFC 3530 14.2.33 CASE 2:
1979 * probable client reboot; state will be removed if
1982 new = create_client(clname
, dname
, rqstp
, &clverifier
);
1988 * RFC 3530 14.2.33 CASE 3:
1989 * probable client reboot; state will be removed if
1992 expire_client(unconf
);
1993 new = create_client(clname
, dname
, rqstp
, &clverifier
);
1999 * XXX: we should probably set this at creation time, and check
2000 * for consistent minorversion use throughout:
2002 new->cl_minorversion
= 0;
2003 gen_callback(new, setclid
, rqstp
);
2004 add_to_unconfirmed(new, strhashval
);
2005 setclid
->se_clientid
.cl_boot
= new->cl_clientid
.cl_boot
;
2006 setclid
->se_clientid
.cl_id
= new->cl_clientid
.cl_id
;
2007 memcpy(setclid
->se_confirm
.data
, new->cl_confirm
.data
, sizeof(setclid
->se_confirm
.data
));
2010 nfs4_unlock_state();
2016 * Section 14.2.34 of RFC 3530 (under the heading "IMPLEMENTATION") has
2017 * a description of SETCLIENTID_CONFIRM request processing consisting of 4
2018 * bullets, labeled as CASE1 - CASE4 below.
2021 nfsd4_setclientid_confirm(struct svc_rqst
*rqstp
,
2022 struct nfsd4_compound_state
*cstate
,
2023 struct nfsd4_setclientid_confirm
*setclientid_confirm
)
2025 struct sockaddr
*sa
= svc_addr(rqstp
);
2026 struct nfs4_client
*conf
, *unconf
;
2027 nfs4_verifier confirm
= setclientid_confirm
->sc_confirm
;
2028 clientid_t
* clid
= &setclientid_confirm
->sc_clientid
;
2031 if (STALE_CLIENTID(clid
))
2032 return nfserr_stale_clientid
;
2034 * XXX The Duplicate Request Cache (DRC) has been checked (??)
2035 * We get here on a DRC miss.
2040 conf
= find_confirmed_client(clid
);
2041 unconf
= find_unconfirmed_client(clid
);
2043 status
= nfserr_clid_inuse
;
2044 if (conf
&& !rpc_cmp_addr((struct sockaddr
*) &conf
->cl_addr
, sa
))
2046 if (unconf
&& !rpc_cmp_addr((struct sockaddr
*) &unconf
->cl_addr
, sa
))
2050 * section 14.2.34 of RFC 3530 has a description of
2051 * SETCLIENTID_CONFIRM request processing consisting
2052 * of 4 bullet points, labeled as CASE1 - CASE4 below.
2054 if (conf
&& unconf
&& same_verf(&confirm
, &unconf
->cl_confirm
)) {
2056 * RFC 3530 14.2.34 CASE 1:
2059 if (!same_creds(&conf
->cl_cred
, &unconf
->cl_cred
))
2060 status
= nfserr_clid_inuse
;
2062 nfsd4_change_callback(conf
, &unconf
->cl_cb_conn
);
2063 nfsd4_probe_callback(conf
);
2064 expire_client(unconf
);
2068 } else if (conf
&& !unconf
) {
2070 * RFC 3530 14.2.34 CASE 2:
2071 * probable retransmitted request; play it safe and
2074 if (!same_creds(&conf
->cl_cred
, &rqstp
->rq_cred
))
2075 status
= nfserr_clid_inuse
;
2078 } else if (!conf
&& unconf
2079 && same_verf(&unconf
->cl_confirm
, &confirm
)) {
2081 * RFC 3530 14.2.34 CASE 3:
2082 * Normal case; new or rebooted client:
2084 if (!same_creds(&unconf
->cl_cred
, &rqstp
->rq_cred
)) {
2085 status
= nfserr_clid_inuse
;
2088 clientstr_hashval(unconf
->cl_recdir
);
2089 conf
= find_confirmed_client_by_str(unconf
->cl_recdir
,
2092 nfsd4_remove_clid_dir(conf
);
2093 expire_client(conf
);
2095 move_to_confirmed(unconf
);
2097 nfsd4_probe_callback(conf
);
2100 } else if ((!conf
|| (conf
&& !same_verf(&conf
->cl_confirm
, &confirm
)))
2101 && (!unconf
|| (unconf
&& !same_verf(&unconf
->cl_confirm
,
2104 * RFC 3530 14.2.34 CASE 4:
2105 * Client probably hasn't noticed that we rebooted yet.
2107 status
= nfserr_stale_clientid
;
2109 /* check that we have hit one of the cases...*/
2110 status
= nfserr_clid_inuse
;
2113 nfs4_unlock_state();
2117 /* OPEN Share state helper functions */
2118 static inline struct nfs4_file
*
2119 alloc_init_file(struct inode
*ino
)
2121 struct nfs4_file
*fp
;
2122 unsigned int hashval
= file_hashval(ino
);
2124 fp
= kmem_cache_alloc(file_slab
, GFP_KERNEL
);
2126 atomic_set(&fp
->fi_ref
, 1);
2127 INIT_LIST_HEAD(&fp
->fi_hash
);
2128 INIT_LIST_HEAD(&fp
->fi_stateids
);
2129 INIT_LIST_HEAD(&fp
->fi_delegations
);
2130 fp
->fi_inode
= igrab(ino
);
2131 fp
->fi_id
= current_fileid
++;
2132 fp
->fi_had_conflict
= false;
2133 fp
->fi_lease
= NULL
;
2134 memset(fp
->fi_fds
, 0, sizeof(fp
->fi_fds
));
2135 memset(fp
->fi_access
, 0, sizeof(fp
->fi_access
));
2136 spin_lock(&recall_lock
);
2137 list_add(&fp
->fi_hash
, &file_hashtbl
[hashval
]);
2138 spin_unlock(&recall_lock
);
2145 nfsd4_free_slab(struct kmem_cache
**slab
)
2149 kmem_cache_destroy(*slab
);
2154 nfsd4_free_slabs(void)
2156 nfsd4_free_slab(&stateowner_slab
);
2157 nfsd4_free_slab(&file_slab
);
2158 nfsd4_free_slab(&stateid_slab
);
2159 nfsd4_free_slab(&deleg_slab
);
2163 nfsd4_init_slabs(void)
2165 stateowner_slab
= kmem_cache_create("nfsd4_stateowners",
2166 sizeof(struct nfs4_stateowner
), 0, 0, NULL
);
2167 if (stateowner_slab
== NULL
)
2169 file_slab
= kmem_cache_create("nfsd4_files",
2170 sizeof(struct nfs4_file
), 0, 0, NULL
);
2171 if (file_slab
== NULL
)
2173 stateid_slab
= kmem_cache_create("nfsd4_stateids",
2174 sizeof(struct nfs4_stateid
), 0, 0, NULL
);
2175 if (stateid_slab
== NULL
)
2177 deleg_slab
= kmem_cache_create("nfsd4_delegations",
2178 sizeof(struct nfs4_delegation
), 0, 0, NULL
);
2179 if (deleg_slab
== NULL
)
2184 dprintk("nfsd4: out of memory while initializing nfsv4\n");
2189 nfs4_free_stateowner(struct kref
*kref
)
2191 struct nfs4_stateowner
*sop
=
2192 container_of(kref
, struct nfs4_stateowner
, so_ref
);
2193 kfree(sop
->so_owner
.data
);
2194 kmem_cache_free(stateowner_slab
, sop
);
2197 static inline struct nfs4_stateowner
*
2198 alloc_stateowner(struct xdr_netobj
*owner
)
2200 struct nfs4_stateowner
*sop
;
2202 if ((sop
= kmem_cache_alloc(stateowner_slab
, GFP_KERNEL
))) {
2203 if ((sop
->so_owner
.data
= kmalloc(owner
->len
, GFP_KERNEL
))) {
2204 memcpy(sop
->so_owner
.data
, owner
->data
, owner
->len
);
2205 sop
->so_owner
.len
= owner
->len
;
2206 kref_init(&sop
->so_ref
);
2209 kmem_cache_free(stateowner_slab
, sop
);
2214 static struct nfs4_stateowner
*
2215 alloc_init_open_stateowner(unsigned int strhashval
, struct nfs4_client
*clp
, struct nfsd4_open
*open
) {
2216 struct nfs4_stateowner
*sop
;
2217 struct nfs4_replay
*rp
;
2218 unsigned int idhashval
;
2220 if (!(sop
= alloc_stateowner(&open
->op_owner
)))
2222 idhashval
= ownerid_hashval(current_ownerid
);
2223 INIT_LIST_HEAD(&sop
->so_idhash
);
2224 INIT_LIST_HEAD(&sop
->so_strhash
);
2225 INIT_LIST_HEAD(&sop
->so_perclient
);
2226 INIT_LIST_HEAD(&sop
->so_stateids
);
2227 INIT_LIST_HEAD(&sop
->so_perstateid
); /* not used */
2228 INIT_LIST_HEAD(&sop
->so_close_lru
);
2230 list_add(&sop
->so_idhash
, &ownerid_hashtbl
[idhashval
]);
2231 list_add(&sop
->so_strhash
, &ownerstr_hashtbl
[strhashval
]);
2232 list_add(&sop
->so_perclient
, &clp
->cl_openowners
);
2233 sop
->so_is_open_owner
= 1;
2234 sop
->so_id
= current_ownerid
++;
2235 sop
->so_client
= clp
;
2236 sop
->so_seqid
= open
->op_seqid
;
2237 sop
->so_confirmed
= 0;
2238 rp
= &sop
->so_replay
;
2239 rp
->rp_status
= nfserr_serverfault
;
2241 rp
->rp_buf
= rp
->rp_ibuf
;
2246 init_stateid(struct nfs4_stateid
*stp
, struct nfs4_file
*fp
, struct nfsd4_open
*open
) {
2247 struct nfs4_stateowner
*sop
= open
->op_stateowner
;
2248 unsigned int hashval
= stateid_hashval(sop
->so_id
, fp
->fi_id
);
2250 INIT_LIST_HEAD(&stp
->st_hash
);
2251 INIT_LIST_HEAD(&stp
->st_perstateowner
);
2252 INIT_LIST_HEAD(&stp
->st_lockowners
);
2253 INIT_LIST_HEAD(&stp
->st_perfile
);
2254 list_add(&stp
->st_hash
, &stateid_hashtbl
[hashval
]);
2255 list_add(&stp
->st_perstateowner
, &sop
->so_stateids
);
2256 list_add(&stp
->st_perfile
, &fp
->fi_stateids
);
2257 stp
->st_stateowner
= sop
;
2260 stp
->st_stateid
.si_boot
= boot_time
;
2261 stp
->st_stateid
.si_stateownerid
= sop
->so_id
;
2262 stp
->st_stateid
.si_fileid
= fp
->fi_id
;
2263 stp
->st_stateid
.si_generation
= 0;
2264 stp
->st_access_bmap
= 0;
2265 stp
->st_deny_bmap
= 0;
2266 __set_bit(open
->op_share_access
& ~NFS4_SHARE_WANT_MASK
,
2267 &stp
->st_access_bmap
);
2268 __set_bit(open
->op_share_deny
, &stp
->st_deny_bmap
);
2269 stp
->st_openstp
= NULL
;
2273 move_to_close_lru(struct nfs4_stateowner
*sop
)
2275 dprintk("NFSD: move_to_close_lru nfs4_stateowner %p\n", sop
);
2277 list_move_tail(&sop
->so_close_lru
, &close_lru
);
2278 sop
->so_time
= get_seconds();
2282 same_owner_str(struct nfs4_stateowner
*sop
, struct xdr_netobj
*owner
,
2285 return (sop
->so_owner
.len
== owner
->len
) &&
2286 0 == memcmp(sop
->so_owner
.data
, owner
->data
, owner
->len
) &&
2287 (sop
->so_client
->cl_clientid
.cl_id
== clid
->cl_id
);
2290 static struct nfs4_stateowner
*
2291 find_openstateowner_str(unsigned int hashval
, struct nfsd4_open
*open
)
2293 struct nfs4_stateowner
*so
= NULL
;
2295 list_for_each_entry(so
, &ownerstr_hashtbl
[hashval
], so_strhash
) {
2296 if (same_owner_str(so
, &open
->op_owner
, &open
->op_clientid
))
2302 /* search file_hashtbl[] for file */
2303 static struct nfs4_file
*
2304 find_file(struct inode
*ino
)
2306 unsigned int hashval
= file_hashval(ino
);
2307 struct nfs4_file
*fp
;
2309 spin_lock(&recall_lock
);
2310 list_for_each_entry(fp
, &file_hashtbl
[hashval
], fi_hash
) {
2311 if (fp
->fi_inode
== ino
) {
2313 spin_unlock(&recall_lock
);
2317 spin_unlock(&recall_lock
);
2321 static inline int access_valid(u32 x
, u32 minorversion
)
2323 if ((x
& NFS4_SHARE_ACCESS_MASK
) < NFS4_SHARE_ACCESS_READ
)
2325 if ((x
& NFS4_SHARE_ACCESS_MASK
) > NFS4_SHARE_ACCESS_BOTH
)
2327 x
&= ~NFS4_SHARE_ACCESS_MASK
;
2328 if (minorversion
&& x
) {
2329 if ((x
& NFS4_SHARE_WANT_MASK
) > NFS4_SHARE_WANT_CANCEL
)
2331 if ((x
& NFS4_SHARE_WHEN_MASK
) > NFS4_SHARE_PUSH_DELEG_WHEN_UNCONTENDED
)
2333 x
&= ~(NFS4_SHARE_WANT_MASK
| NFS4_SHARE_WHEN_MASK
);
2340 static inline int deny_valid(u32 x
)
2342 /* Note: unlike access bits, deny bits may be zero. */
2343 return x
<= NFS4_SHARE_DENY_BOTH
;
2347 * Called to check deny when READ with all zero stateid or
2348 * WRITE with all zero or all one stateid
2351 nfs4_share_conflict(struct svc_fh
*current_fh
, unsigned int deny_type
)
2353 struct inode
*ino
= current_fh
->fh_dentry
->d_inode
;
2354 struct nfs4_file
*fp
;
2355 struct nfs4_stateid
*stp
;
2358 dprintk("NFSD: nfs4_share_conflict\n");
2360 fp
= find_file(ino
);
2363 ret
= nfserr_locked
;
2364 /* Search for conflicting share reservations */
2365 list_for_each_entry(stp
, &fp
->fi_stateids
, st_perfile
) {
2366 if (test_bit(deny_type
, &stp
->st_deny_bmap
) ||
2367 test_bit(NFS4_SHARE_DENY_BOTH
, &stp
->st_deny_bmap
))
2376 static void nfsd_break_one_deleg(struct nfs4_delegation
*dp
)
2378 /* We're assuming the state code never drops its reference
2379 * without first removing the lease. Since we're in this lease
2380 * callback (and since the lease code is serialized by the kernel
2381 * lock) we know the server hasn't removed the lease yet, we know
2382 * it's safe to take a reference: */
2383 atomic_inc(&dp
->dl_count
);
2385 list_add_tail(&dp
->dl_recall_lru
, &del_recall_lru
);
2387 /* only place dl_time is set. protected by lock_flocks*/
2388 dp
->dl_time
= get_seconds();
2390 nfsd4_cb_recall(dp
);
2393 /* Called from break_lease() with lock_flocks() held. */
2394 static void nfsd_break_deleg_cb(struct file_lock
*fl
)
2396 struct nfs4_file
*fp
= (struct nfs4_file
*)fl
->fl_owner
;
2397 struct nfs4_delegation
*dp
;
2400 /* We assume break_lease is only called once per lease: */
2401 BUG_ON(fp
->fi_had_conflict
);
2403 * We don't want the locks code to timeout the lease for us;
2404 * we'll remove it ourself if a delegation isn't returned
2407 fl
->fl_break_time
= 0;
2409 spin_lock(&recall_lock
);
2410 fp
->fi_had_conflict
= true;
2411 list_for_each_entry(dp
, &fp
->fi_delegations
, dl_perfile
)
2412 nfsd_break_one_deleg(dp
);
2413 spin_unlock(&recall_lock
);
2417 int nfsd_change_deleg_cb(struct file_lock
**onlist
, int arg
)
2420 return lease_modify(onlist
, arg
);
2425 static const struct lock_manager_operations nfsd_lease_mng_ops
= {
2426 .lm_break
= nfsd_break_deleg_cb
,
2427 .lm_change
= nfsd_change_deleg_cb
,
2432 nfsd4_process_open1(struct nfsd4_compound_state
*cstate
,
2433 struct nfsd4_open
*open
)
2435 clientid_t
*clientid
= &open
->op_clientid
;
2436 struct nfs4_client
*clp
= NULL
;
2437 unsigned int strhashval
;
2438 struct nfs4_stateowner
*sop
= NULL
;
2440 if (!check_name(open
->op_owner
))
2441 return nfserr_inval
;
2443 if (STALE_CLIENTID(&open
->op_clientid
))
2444 return nfserr_stale_clientid
;
2446 strhashval
= ownerstr_hashval(clientid
->cl_id
, open
->op_owner
);
2447 sop
= find_openstateowner_str(strhashval
, open
);
2448 open
->op_stateowner
= sop
;
2450 /* Make sure the client's lease hasn't expired. */
2451 clp
= find_confirmed_client(clientid
);
2453 return nfserr_expired
;
2456 /* When sessions are used, skip open sequenceid processing */
2457 if (nfsd4_has_session(cstate
))
2459 if (!sop
->so_confirmed
) {
2460 /* Replace unconfirmed owners without checking for replay. */
2461 clp
= sop
->so_client
;
2462 release_openowner(sop
);
2463 open
->op_stateowner
= NULL
;
2466 if (open
->op_seqid
== sop
->so_seqid
- 1) {
2467 if (sop
->so_replay
.rp_buflen
)
2468 return nfserr_replay_me
;
2469 /* The original OPEN failed so spectacularly
2470 * that we don't even have replay data saved!
2471 * Therefore, we have no choice but to continue
2472 * processing this OPEN; presumably, we'll
2473 * fail again for the same reason.
2475 dprintk("nfsd4_process_open1: replay with no replay cache\n");
2478 if (open
->op_seqid
!= sop
->so_seqid
)
2479 return nfserr_bad_seqid
;
2481 if (open
->op_stateowner
== NULL
) {
2482 sop
= alloc_init_open_stateowner(strhashval
, clp
, open
);
2484 return nfserr_resource
;
2485 open
->op_stateowner
= sop
;
2487 list_del_init(&sop
->so_close_lru
);
2488 renew_client(sop
->so_client
);
2492 static inline __be32
2493 nfs4_check_delegmode(struct nfs4_delegation
*dp
, int flags
)
2495 if ((flags
& WR_STATE
) && (dp
->dl_type
== NFS4_OPEN_DELEGATE_READ
))
2496 return nfserr_openmode
;
2501 static struct nfs4_delegation
*
2502 find_delegation_file(struct nfs4_file
*fp
, stateid_t
*stid
)
2504 struct nfs4_delegation
*dp
;
2506 spin_lock(&recall_lock
);
2507 list_for_each_entry(dp
, &fp
->fi_delegations
, dl_perfile
)
2508 if (dp
->dl_stateid
.si_stateownerid
== stid
->si_stateownerid
) {
2509 spin_unlock(&recall_lock
);
2512 spin_unlock(&recall_lock
);
2516 static int share_access_to_flags(u32 share_access
)
2518 share_access
&= ~NFS4_SHARE_WANT_MASK
;
2520 return share_access
== NFS4_SHARE_ACCESS_READ
? RD_STATE
: WR_STATE
;
2524 nfs4_check_deleg(struct nfs4_file
*fp
, struct nfsd4_open
*open
,
2525 struct nfs4_delegation
**dp
)
2528 __be32 status
= nfserr_bad_stateid
;
2530 *dp
= find_delegation_file(fp
, &open
->op_delegate_stateid
);
2533 flags
= share_access_to_flags(open
->op_share_access
);
2534 status
= nfs4_check_delegmode(*dp
, flags
);
2538 if (open
->op_claim_type
!= NFS4_OPEN_CLAIM_DELEGATE_CUR
)
2542 open
->op_stateowner
->so_confirmed
= 1;
2547 nfs4_check_open(struct nfs4_file
*fp
, struct nfsd4_open
*open
, struct nfs4_stateid
**stpp
)
2549 struct nfs4_stateid
*local
;
2550 __be32 status
= nfserr_share_denied
;
2551 struct nfs4_stateowner
*sop
= open
->op_stateowner
;
2553 list_for_each_entry(local
, &fp
->fi_stateids
, st_perfile
) {
2554 /* ignore lock owners */
2555 if (local
->st_stateowner
->so_is_open_owner
== 0)
2557 /* remember if we have seen this open owner */
2558 if (local
->st_stateowner
== sop
)
2560 /* check for conflicting share reservations */
2561 if (!test_share(local
, open
))
2569 static inline struct nfs4_stateid
*
2570 nfs4_alloc_stateid(void)
2572 return kmem_cache_alloc(stateid_slab
, GFP_KERNEL
);
2575 static inline int nfs4_access_to_access(u32 nfs4_access
)
2579 if (nfs4_access
& NFS4_SHARE_ACCESS_READ
)
2580 flags
|= NFSD_MAY_READ
;
2581 if (nfs4_access
& NFS4_SHARE_ACCESS_WRITE
)
2582 flags
|= NFSD_MAY_WRITE
;
2586 static __be32
nfs4_get_vfs_file(struct svc_rqst
*rqstp
, struct nfs4_file
*fp
,
2587 struct svc_fh
*cur_fh
, struct nfsd4_open
*open
)
2590 int oflag
= nfs4_access_to_omode(open
->op_share_access
);
2591 int access
= nfs4_access_to_access(open
->op_share_access
);
2593 /* CLAIM_DELEGATE_CUR is used in response to a broken lease;
2594 * allowing it to break the lease and return EAGAIN leaves the
2595 * client unable to make progress in returning the delegation */
2596 if (open
->op_claim_type
== NFS4_OPEN_CLAIM_DELEGATE_CUR
)
2597 access
|= NFSD_MAY_NOT_BREAK_LEASE
;
2599 if (!fp
->fi_fds
[oflag
]) {
2600 status
= nfsd_open(rqstp
, cur_fh
, S_IFREG
, access
,
2601 &fp
->fi_fds
[oflag
]);
2605 nfs4_file_get_access(fp
, oflag
);
2611 nfs4_new_open(struct svc_rqst
*rqstp
, struct nfs4_stateid
**stpp
,
2612 struct nfs4_file
*fp
, struct svc_fh
*cur_fh
,
2613 struct nfsd4_open
*open
)
2615 struct nfs4_stateid
*stp
;
2618 stp
= nfs4_alloc_stateid();
2620 return nfserr_resource
;
2622 status
= nfs4_get_vfs_file(rqstp
, fp
, cur_fh
, open
);
2624 kmem_cache_free(stateid_slab
, stp
);
2631 static inline __be32
2632 nfsd4_truncate(struct svc_rqst
*rqstp
, struct svc_fh
*fh
,
2633 struct nfsd4_open
*open
)
2635 struct iattr iattr
= {
2636 .ia_valid
= ATTR_SIZE
,
2639 if (!open
->op_truncate
)
2641 if (!(open
->op_share_access
& NFS4_SHARE_ACCESS_WRITE
))
2642 return nfserr_inval
;
2643 return nfsd_setattr(rqstp
, fh
, &iattr
, 0, (time_t)0);
2647 nfs4_upgrade_open(struct svc_rqst
*rqstp
, struct nfs4_file
*fp
, struct svc_fh
*cur_fh
, struct nfs4_stateid
*stp
, struct nfsd4_open
*open
)
2649 u32 op_share_access
= open
->op_share_access
& ~NFS4_SHARE_WANT_MASK
;
2653 new_access
= !test_bit(op_share_access
, &stp
->st_access_bmap
);
2655 status
= nfs4_get_vfs_file(rqstp
, fp
, cur_fh
, open
);
2659 status
= nfsd4_truncate(rqstp
, cur_fh
, open
);
2662 int oflag
= nfs4_access_to_omode(op_share_access
);
2663 nfs4_file_put_access(fp
, oflag
);
2667 /* remember the open */
2668 __set_bit(op_share_access
, &stp
->st_access_bmap
);
2669 __set_bit(open
->op_share_deny
, &stp
->st_deny_bmap
);
2676 nfs4_set_claim_prev(struct nfsd4_open
*open
)
2678 open
->op_stateowner
->so_confirmed
= 1;
2679 open
->op_stateowner
->so_client
->cl_firststate
= 1;
2682 /* Should we give out recallable state?: */
2683 static bool nfsd4_cb_channel_good(struct nfs4_client
*clp
)
2685 if (clp
->cl_cb_state
== NFSD4_CB_UP
)
2688 * In the sessions case, since we don't have to establish a
2689 * separate connection for callbacks, we assume it's OK
2690 * until we hear otherwise:
2692 return clp
->cl_minorversion
&& clp
->cl_cb_state
== NFSD4_CB_UNKNOWN
;
2695 static struct file_lock
*nfs4_alloc_init_lease(struct nfs4_delegation
*dp
, int flag
)
2697 struct file_lock
*fl
;
2699 fl
= locks_alloc_lock();
2702 locks_init_lock(fl
);
2703 fl
->fl_lmops
= &nfsd_lease_mng_ops
;
2704 fl
->fl_flags
= FL_LEASE
;
2705 fl
->fl_type
= flag
== NFS4_OPEN_DELEGATE_READ
? F_RDLCK
: F_WRLCK
;
2706 fl
->fl_end
= OFFSET_MAX
;
2707 fl
->fl_owner
= (fl_owner_t
)(dp
->dl_file
);
2708 fl
->fl_pid
= current
->tgid
;
2712 static int nfs4_setlease(struct nfs4_delegation
*dp
, int flag
)
2714 struct nfs4_file
*fp
= dp
->dl_file
;
2715 struct file_lock
*fl
;
2718 fl
= nfs4_alloc_init_lease(dp
, flag
);
2721 fl
->fl_file
= find_readable_file(fp
);
2722 list_add(&dp
->dl_perclnt
, &dp
->dl_client
->cl_delegations
);
2723 status
= vfs_setlease(fl
->fl_file
, fl
->fl_type
, &fl
);
2725 list_del_init(&dp
->dl_perclnt
);
2726 locks_free_lock(fl
);
2730 fp
->fi_deleg_file
= fl
->fl_file
;
2731 get_file(fp
->fi_deleg_file
);
2732 atomic_set(&fp
->fi_delegees
, 1);
2733 list_add(&dp
->dl_perfile
, &fp
->fi_delegations
);
2737 static int nfs4_set_delegation(struct nfs4_delegation
*dp
, int flag
)
2739 struct nfs4_file
*fp
= dp
->dl_file
;
2742 return nfs4_setlease(dp
, flag
);
2743 spin_lock(&recall_lock
);
2744 if (fp
->fi_had_conflict
) {
2745 spin_unlock(&recall_lock
);
2748 atomic_inc(&fp
->fi_delegees
);
2749 list_add(&dp
->dl_perfile
, &fp
->fi_delegations
);
2750 spin_unlock(&recall_lock
);
2751 list_add(&dp
->dl_perclnt
, &dp
->dl_client
->cl_delegations
);
2756 * Attempt to hand out a delegation.
2759 nfs4_open_delegation(struct svc_fh
*fh
, struct nfsd4_open
*open
, struct nfs4_stateid
*stp
)
2761 struct nfs4_delegation
*dp
;
2762 struct nfs4_stateowner
*sop
= stp
->st_stateowner
;
2764 int status
, flag
= 0;
2766 cb_up
= nfsd4_cb_channel_good(sop
->so_client
);
2767 flag
= NFS4_OPEN_DELEGATE_NONE
;
2768 open
->op_recall
= 0;
2769 switch (open
->op_claim_type
) {
2770 case NFS4_OPEN_CLAIM_PREVIOUS
:
2772 open
->op_recall
= 1;
2773 flag
= open
->op_delegate_type
;
2774 if (flag
== NFS4_OPEN_DELEGATE_NONE
)
2777 case NFS4_OPEN_CLAIM_NULL
:
2778 /* Let's not give out any delegations till everyone's
2779 * had the chance to reclaim theirs.... */
2780 if (locks_in_grace())
2782 if (!cb_up
|| !sop
->so_confirmed
)
2784 if (open
->op_share_access
& NFS4_SHARE_ACCESS_WRITE
)
2785 flag
= NFS4_OPEN_DELEGATE_WRITE
;
2787 flag
= NFS4_OPEN_DELEGATE_READ
;
2793 dp
= alloc_init_deleg(sop
->so_client
, stp
, fh
, flag
);
2796 status
= nfs4_set_delegation(dp
, flag
);
2800 memcpy(&open
->op_delegate_stateid
, &dp
->dl_stateid
, sizeof(dp
->dl_stateid
));
2802 dprintk("NFSD: delegation stateid=" STATEID_FMT
"\n",
2803 STATEID_VAL(&dp
->dl_stateid
));
2805 if (open
->op_claim_type
== NFS4_OPEN_CLAIM_PREVIOUS
2806 && flag
== NFS4_OPEN_DELEGATE_NONE
2807 && open
->op_delegate_type
!= NFS4_OPEN_DELEGATE_NONE
)
2808 dprintk("NFSD: WARNING: refusing delegation reclaim\n");
2809 open
->op_delegate_type
= flag
;
2812 nfs4_put_delegation(dp
);
2814 flag
= NFS4_OPEN_DELEGATE_NONE
;
2819 * called with nfs4_lock_state() held.
2822 nfsd4_process_open2(struct svc_rqst
*rqstp
, struct svc_fh
*current_fh
, struct nfsd4_open
*open
)
2824 struct nfsd4_compoundres
*resp
= rqstp
->rq_resp
;
2825 struct nfs4_file
*fp
= NULL
;
2826 struct inode
*ino
= current_fh
->fh_dentry
->d_inode
;
2827 struct nfs4_stateid
*stp
= NULL
;
2828 struct nfs4_delegation
*dp
= NULL
;
2831 status
= nfserr_inval
;
2832 if (!access_valid(open
->op_share_access
, resp
->cstate
.minorversion
)
2833 || !deny_valid(open
->op_share_deny
))
2836 * Lookup file; if found, lookup stateid and check open request,
2837 * and check for delegations in the process of being recalled.
2838 * If not found, create the nfs4_file struct
2840 fp
= find_file(ino
);
2842 if ((status
= nfs4_check_open(fp
, open
, &stp
)))
2844 status
= nfs4_check_deleg(fp
, open
, &dp
);
2848 status
= nfserr_bad_stateid
;
2849 if (open
->op_claim_type
== NFS4_OPEN_CLAIM_DELEGATE_CUR
)
2851 status
= nfserr_resource
;
2852 fp
= alloc_init_file(ino
);
2858 * OPEN the file, or upgrade an existing OPEN.
2859 * If truncate fails, the OPEN fails.
2862 /* Stateid was found, this is an OPEN upgrade */
2863 status
= nfs4_upgrade_open(rqstp
, fp
, current_fh
, stp
, open
);
2866 update_stateid(&stp
->st_stateid
);
2868 status
= nfs4_new_open(rqstp
, &stp
, fp
, current_fh
, open
);
2871 init_stateid(stp
, fp
, open
);
2872 status
= nfsd4_truncate(rqstp
, current_fh
, open
);
2874 release_open_stateid(stp
);
2877 if (nfsd4_has_session(&resp
->cstate
))
2878 update_stateid(&stp
->st_stateid
);
2880 memcpy(&open
->op_stateid
, &stp
->st_stateid
, sizeof(stateid_t
));
2882 if (nfsd4_has_session(&resp
->cstate
))
2883 open
->op_stateowner
->so_confirmed
= 1;
2886 * Attempt to hand out a delegation. No error return, because the
2887 * OPEN succeeds even if we fail.
2889 nfs4_open_delegation(current_fh
, open
, stp
);
2893 dprintk("%s: stateid=" STATEID_FMT
"\n", __func__
,
2894 STATEID_VAL(&stp
->st_stateid
));
2898 if (status
== 0 && open
->op_claim_type
== NFS4_OPEN_CLAIM_PREVIOUS
)
2899 nfs4_set_claim_prev(open
);
2901 * To finish the open response, we just need to set the rflags.
2903 open
->op_rflags
= NFS4_OPEN_RESULT_LOCKTYPE_POSIX
;
2904 if (!open
->op_stateowner
->so_confirmed
&&
2905 !nfsd4_has_session(&resp
->cstate
))
2906 open
->op_rflags
|= NFS4_OPEN_RESULT_CONFIRM
;
2912 nfsd4_renew(struct svc_rqst
*rqstp
, struct nfsd4_compound_state
*cstate
,
2915 struct nfs4_client
*clp
;
2919 dprintk("process_renew(%08x/%08x): starting\n",
2920 clid
->cl_boot
, clid
->cl_id
);
2921 status
= nfserr_stale_clientid
;
2922 if (STALE_CLIENTID(clid
))
2924 clp
= find_confirmed_client(clid
);
2925 status
= nfserr_expired
;
2927 /* We assume the client took too long to RENEW. */
2928 dprintk("nfsd4_renew: clientid not found!\n");
2932 status
= nfserr_cb_path_down
;
2933 if (!list_empty(&clp
->cl_delegations
)
2934 && clp
->cl_cb_state
!= NFSD4_CB_UP
)
2938 nfs4_unlock_state();
2942 static struct lock_manager nfsd4_manager
= {
2946 nfsd4_end_grace(void)
2948 dprintk("NFSD: end of grace period\n");
2949 nfsd4_recdir_purge_old();
2950 locks_end_grace(&nfsd4_manager
);
2952 * Now that every NFSv4 client has had the chance to recover and
2953 * to see the (possibly new, possibly shorter) lease time, we
2954 * can safely set the next grace time to the current lease time:
2956 nfsd4_grace
= nfsd4_lease
;
2960 nfs4_laundromat(void)
2962 struct nfs4_client
*clp
;
2963 struct nfs4_stateowner
*sop
;
2964 struct nfs4_delegation
*dp
;
2965 struct list_head
*pos
, *next
, reaplist
;
2966 time_t cutoff
= get_seconds() - nfsd4_lease
;
2967 time_t t
, clientid_val
= nfsd4_lease
;
2968 time_t u
, test_val
= nfsd4_lease
;
2972 dprintk("NFSD: laundromat service - starting\n");
2973 if (locks_in_grace())
2975 INIT_LIST_HEAD(&reaplist
);
2976 spin_lock(&client_lock
);
2977 list_for_each_safe(pos
, next
, &client_lru
) {
2978 clp
= list_entry(pos
, struct nfs4_client
, cl_lru
);
2979 if (time_after((unsigned long)clp
->cl_time
, (unsigned long)cutoff
)) {
2980 t
= clp
->cl_time
- cutoff
;
2981 if (clientid_val
> t
)
2985 if (atomic_read(&clp
->cl_refcount
)) {
2986 dprintk("NFSD: client in use (clientid %08x)\n",
2987 clp
->cl_clientid
.cl_id
);
2990 unhash_client_locked(clp
);
2991 list_add(&clp
->cl_lru
, &reaplist
);
2993 spin_unlock(&client_lock
);
2994 list_for_each_safe(pos
, next
, &reaplist
) {
2995 clp
= list_entry(pos
, struct nfs4_client
, cl_lru
);
2996 dprintk("NFSD: purging unused client (clientid %08x)\n",
2997 clp
->cl_clientid
.cl_id
);
2998 nfsd4_remove_clid_dir(clp
);
3001 spin_lock(&recall_lock
);
3002 list_for_each_safe(pos
, next
, &del_recall_lru
) {
3003 dp
= list_entry (pos
, struct nfs4_delegation
, dl_recall_lru
);
3004 if (time_after((unsigned long)dp
->dl_time
, (unsigned long)cutoff
)) {
3005 u
= dp
->dl_time
- cutoff
;
3010 list_move(&dp
->dl_recall_lru
, &reaplist
);
3012 spin_unlock(&recall_lock
);
3013 list_for_each_safe(pos
, next
, &reaplist
) {
3014 dp
= list_entry (pos
, struct nfs4_delegation
, dl_recall_lru
);
3015 list_del_init(&dp
->dl_recall_lru
);
3016 unhash_delegation(dp
);
3018 test_val
= nfsd4_lease
;
3019 list_for_each_safe(pos
, next
, &close_lru
) {
3020 sop
= list_entry(pos
, struct nfs4_stateowner
, so_close_lru
);
3021 if (time_after((unsigned long)sop
->so_time
, (unsigned long)cutoff
)) {
3022 u
= sop
->so_time
- cutoff
;
3027 dprintk("NFSD: purging unused open stateowner (so_id %d)\n",
3029 release_openowner(sop
);
3031 if (clientid_val
< NFSD_LAUNDROMAT_MINTIMEOUT
)
3032 clientid_val
= NFSD_LAUNDROMAT_MINTIMEOUT
;
3033 nfs4_unlock_state();
3034 return clientid_val
;
3037 static struct workqueue_struct
*laundry_wq
;
3038 static void laundromat_main(struct work_struct
*);
3039 static DECLARE_DELAYED_WORK(laundromat_work
, laundromat_main
);
3042 laundromat_main(struct work_struct
*not_used
)
3046 t
= nfs4_laundromat();
3047 dprintk("NFSD: laundromat_main - sleeping for %ld seconds\n", t
);
3048 queue_delayed_work(laundry_wq
, &laundromat_work
, t
*HZ
);
3051 static struct nfs4_stateowner
*
3052 search_close_lru(u32 st_id
, int flags
)
3054 struct nfs4_stateowner
*local
= NULL
;
3056 if (flags
& CLOSE_STATE
) {
3057 list_for_each_entry(local
, &close_lru
, so_close_lru
) {
3058 if (local
->so_id
== st_id
)
3066 nfs4_check_fh(struct svc_fh
*fhp
, struct nfs4_stateid
*stp
)
3068 return fhp
->fh_dentry
->d_inode
!= stp
->st_file
->fi_inode
;
3072 STALE_STATEID(stateid_t
*stateid
)
3074 if (stateid
->si_boot
== boot_time
)
3076 dprintk("NFSD: stale stateid " STATEID_FMT
"!\n",
3077 STATEID_VAL(stateid
));
3082 access_permit_read(unsigned long access_bmap
)
3084 return test_bit(NFS4_SHARE_ACCESS_READ
, &access_bmap
) ||
3085 test_bit(NFS4_SHARE_ACCESS_BOTH
, &access_bmap
) ||
3086 test_bit(NFS4_SHARE_ACCESS_WRITE
, &access_bmap
);
3090 access_permit_write(unsigned long access_bmap
)
3092 return test_bit(NFS4_SHARE_ACCESS_WRITE
, &access_bmap
) ||
3093 test_bit(NFS4_SHARE_ACCESS_BOTH
, &access_bmap
);
3097 __be32
nfs4_check_openmode(struct nfs4_stateid
*stp
, int flags
)
3099 __be32 status
= nfserr_openmode
;
3101 /* For lock stateid's, we test the parent open, not the lock: */
3102 if (stp
->st_openstp
)
3103 stp
= stp
->st_openstp
;
3104 if ((flags
& WR_STATE
) && (!access_permit_write(stp
->st_access_bmap
)))
3106 if ((flags
& RD_STATE
) && (!access_permit_read(stp
->st_access_bmap
)))
3113 static inline __be32
3114 check_special_stateids(svc_fh
*current_fh
, stateid_t
*stateid
, int flags
)
3116 if (ONE_STATEID(stateid
) && (flags
& RD_STATE
))
3118 else if (locks_in_grace()) {
3119 /* Answer in remaining cases depends on existence of
3120 * conflicting state; so we must wait out the grace period. */
3121 return nfserr_grace
;
3122 } else if (flags
& WR_STATE
)
3123 return nfs4_share_conflict(current_fh
,
3124 NFS4_SHARE_DENY_WRITE
);
3125 else /* (flags & RD_STATE) && ZERO_STATEID(stateid) */
3126 return nfs4_share_conflict(current_fh
,
3127 NFS4_SHARE_DENY_READ
);
3131 * Allow READ/WRITE during grace period on recovered state only for files
3132 * that are not able to provide mandatory locking.
3135 grace_disallows_io(struct inode
*inode
)
3137 return locks_in_grace() && mandatory_lock(inode
);
3140 static int check_stateid_generation(stateid_t
*in
, stateid_t
*ref
, int flags
)
3143 * When sessions are used the stateid generation number is ignored
3146 if ((flags
& HAS_SESSION
) && in
->si_generation
== 0)
3149 /* If the client sends us a stateid from the future, it's buggy: */
3150 if (in
->si_generation
> ref
->si_generation
)
3151 return nfserr_bad_stateid
;
3153 * The following, however, can happen. For example, if the
3154 * client sends an open and some IO at the same time, the open
3155 * may bump si_generation while the IO is still in flight.
3156 * Thanks to hard links and renames, the client never knows what
3157 * file an open will affect. So it could avoid that situation
3158 * only by serializing all opens and IO from the same open
3159 * owner. To recover from the old_stateid error, the client
3160 * will just have to retry the IO:
3162 if (in
->si_generation
< ref
->si_generation
)
3163 return nfserr_old_stateid
;
3168 static int is_delegation_stateid(stateid_t
*stateid
)
3170 return stateid
->si_fileid
== 0;
3173 static int is_open_stateid(struct nfs4_stateid
*stateid
)
3175 return stateid
->st_openstp
== NULL
;
3178 __be32
nfs4_validate_stateid(stateid_t
*stateid
, int flags
)
3180 struct nfs4_stateid
*stp
= NULL
;
3181 __be32 status
= nfserr_stale_stateid
;
3183 if (STALE_STATEID(stateid
))
3186 status
= nfserr_expired
;
3187 stp
= search_for_stateid(stateid
);
3190 status
= nfserr_bad_stateid
;
3192 if (!stp
->st_stateowner
->so_confirmed
)
3195 status
= check_stateid_generation(stateid
, &stp
->st_stateid
, flags
);
3205 * Checks for stateid operations
3208 nfs4_preprocess_stateid_op(struct nfsd4_compound_state
*cstate
,
3209 stateid_t
*stateid
, int flags
, struct file
**filpp
)
3211 struct nfs4_stateid
*stp
= NULL
;
3212 struct nfs4_delegation
*dp
= NULL
;
3213 struct svc_fh
*current_fh
= &cstate
->current_fh
;
3214 struct inode
*ino
= current_fh
->fh_dentry
->d_inode
;
3220 if (grace_disallows_io(ino
))
3221 return nfserr_grace
;
3223 if (nfsd4_has_session(cstate
))
3224 flags
|= HAS_SESSION
;
3226 if (ZERO_STATEID(stateid
) || ONE_STATEID(stateid
))
3227 return check_special_stateids(current_fh
, stateid
, flags
);
3229 status
= nfserr_stale_stateid
;
3230 if (STALE_STATEID(stateid
))
3234 * We assume that any stateid that has the current boot time,
3235 * but that we can't find, is expired:
3237 status
= nfserr_expired
;
3238 if (is_delegation_stateid(stateid
)) {
3239 dp
= find_delegation_stateid(ino
, stateid
);
3242 status
= check_stateid_generation(stateid
, &dp
->dl_stateid
,
3246 status
= nfs4_check_delegmode(dp
, flags
);
3249 renew_client(dp
->dl_client
);
3251 *filpp
= dp
->dl_file
->fi_deleg_file
;
3254 } else { /* open or lock stateid */
3255 stp
= find_stateid(stateid
, flags
);
3258 status
= nfserr_bad_stateid
;
3259 if (nfs4_check_fh(current_fh
, stp
))
3261 if (!stp
->st_stateowner
->so_confirmed
)
3263 status
= check_stateid_generation(stateid
, &stp
->st_stateid
,
3267 status
= nfs4_check_openmode(stp
, flags
);
3270 renew_client(stp
->st_stateowner
->so_client
);
3272 if (flags
& RD_STATE
)
3273 *filpp
= find_readable_file(stp
->st_file
);
3275 *filpp
= find_writeable_file(stp
->st_file
);
3284 nfsd4_free_delegation_stateid(stateid_t
*stateid
)
3286 struct nfs4_delegation
*dp
= search_for_delegation(stateid
);
3288 return nfserr_locks_held
;
3289 return nfserr_bad_stateid
;
3293 nfsd4_free_lock_stateid(struct nfs4_stateid
*stp
)
3295 if (check_for_locks(stp
->st_file
, stp
->st_stateowner
))
3296 return nfserr_locks_held
;
3297 release_lock_stateid(stp
);
3302 * Test if the stateid is valid
3305 nfsd4_test_stateid(struct svc_rqst
*rqstp
, struct nfsd4_compound_state
*cstate
,
3306 struct nfsd4_test_stateid
*test_stateid
)
3308 test_stateid
->ts_has_session
= nfsd4_has_session(cstate
);
3316 nfsd4_free_stateid(struct svc_rqst
*rqstp
, struct nfsd4_compound_state
*cstate
,
3317 struct nfsd4_free_stateid
*free_stateid
)
3319 stateid_t
*stateid
= &free_stateid
->fr_stateid
;
3320 struct nfs4_stateid
*stp
;
3324 if (is_delegation_stateid(stateid
)) {
3325 ret
= nfsd4_free_delegation_stateid(stateid
);
3329 stp
= search_for_stateid(stateid
);
3331 ret
= nfserr_bad_stateid
;
3334 if (stateid
->si_generation
!= 0) {
3335 if (stateid
->si_generation
< stp
->st_stateid
.si_generation
) {
3336 ret
= nfserr_old_stateid
;
3339 if (stateid
->si_generation
> stp
->st_stateid
.si_generation
) {
3340 ret
= nfserr_bad_stateid
;
3345 if (is_open_stateid(stp
)) {
3346 ret
= nfserr_locks_held
;
3349 ret
= nfsd4_free_lock_stateid(stp
);
3354 nfs4_unlock_state();
3361 return (type
== NFS4_READW_LT
|| type
== NFS4_READ_LT
) ?
3362 RD_STATE
: WR_STATE
;
3366 * Checks for sequence id mutating operations.
3369 nfs4_preprocess_seqid_op(struct nfsd4_compound_state
*cstate
, u32 seqid
,
3370 stateid_t
*stateid
, int flags
,
3371 struct nfs4_stateowner
**sopp
,
3372 struct nfs4_stateid
**stpp
, struct nfsd4_lock
*lock
)
3374 struct nfs4_stateid
*stp
;
3375 struct nfs4_stateowner
*sop
;
3376 struct svc_fh
*current_fh
= &cstate
->current_fh
;
3379 dprintk("NFSD: %s: seqid=%d stateid = " STATEID_FMT
"\n", __func__
,
3380 seqid
, STATEID_VAL(stateid
));
3385 if (ZERO_STATEID(stateid
) || ONE_STATEID(stateid
)) {
3386 dprintk("NFSD: preprocess_seqid_op: magic stateid!\n");
3387 return nfserr_bad_stateid
;
3390 if (STALE_STATEID(stateid
))
3391 return nfserr_stale_stateid
;
3393 if (nfsd4_has_session(cstate
))
3394 flags
|= HAS_SESSION
;
3397 * We return BAD_STATEID if filehandle doesn't match stateid,
3398 * the confirmed flag is incorrecly set, or the generation
3399 * number is incorrect.
3401 stp
= find_stateid(stateid
, flags
);
3404 * Also, we should make sure this isn't just the result of
3407 sop
= search_close_lru(stateid
->si_stateownerid
, flags
);
3408 /* It's not stale; let's assume it's expired: */
3410 return nfserr_expired
;
3416 *sopp
= sop
= stp
->st_stateowner
;
3419 clientid_t
*lockclid
= &lock
->v
.new.clientid
;
3420 struct nfs4_client
*clp
= sop
->so_client
;
3424 lkflg
= setlkflg(lock
->lk_type
);
3426 if (lock
->lk_is_new
) {
3427 if (!sop
->so_is_open_owner
)
3428 return nfserr_bad_stateid
;
3429 if (!(flags
& HAS_SESSION
) &&
3430 !same_clid(&clp
->cl_clientid
, lockclid
))
3431 return nfserr_bad_stateid
;
3432 /* stp is the open stateid */
3433 status
= nfs4_check_openmode(stp
, lkflg
);
3437 /* stp is the lock stateid */
3438 status
= nfs4_check_openmode(stp
->st_openstp
, lkflg
);
3444 if (nfs4_check_fh(current_fh
, stp
)) {
3445 dprintk("NFSD: preprocess_seqid_op: fh-stateid mismatch!\n");
3446 return nfserr_bad_stateid
;
3450 * We now validate the seqid and stateid generation numbers.
3451 * For the moment, we ignore the possibility of
3452 * generation number wraparound.
3454 if (!(flags
& HAS_SESSION
) && seqid
!= sop
->so_seqid
)
3457 if (sop
->so_confirmed
&& flags
& CONFIRM
) {
3458 dprintk("NFSD: preprocess_seqid_op: expected"
3459 " unconfirmed stateowner!\n");
3460 return nfserr_bad_stateid
;
3462 if (!sop
->so_confirmed
&& !(flags
& CONFIRM
)) {
3463 dprintk("NFSD: preprocess_seqid_op: stateowner not"
3464 " confirmed yet!\n");
3465 return nfserr_bad_stateid
;
3467 status
= check_stateid_generation(stateid
, &stp
->st_stateid
, flags
);
3470 renew_client(sop
->so_client
);
3474 if (seqid
== sop
->so_seqid
- 1) {
3475 dprintk("NFSD: preprocess_seqid_op: retransmission?\n");
3476 /* indicate replay to calling function */
3477 return nfserr_replay_me
;
3479 dprintk("NFSD: preprocess_seqid_op: bad seqid (expected %d, got %d)\n",
3480 sop
->so_seqid
, seqid
);
3482 return nfserr_bad_seqid
;
3486 nfsd4_open_confirm(struct svc_rqst
*rqstp
, struct nfsd4_compound_state
*cstate
,
3487 struct nfsd4_open_confirm
*oc
)
3490 struct nfs4_stateowner
*sop
;
3491 struct nfs4_stateid
*stp
;
3493 dprintk("NFSD: nfsd4_open_confirm on file %.*s\n",
3494 (int)cstate
->current_fh
.fh_dentry
->d_name
.len
,
3495 cstate
->current_fh
.fh_dentry
->d_name
.name
);
3497 status
= fh_verify(rqstp
, &cstate
->current_fh
, S_IFREG
, 0);
3503 if ((status
= nfs4_preprocess_seqid_op(cstate
,
3504 oc
->oc_seqid
, &oc
->oc_req_stateid
,
3505 CONFIRM
| OPEN_STATE
,
3506 &oc
->oc_stateowner
, &stp
, NULL
)))
3509 sop
= oc
->oc_stateowner
;
3510 sop
->so_confirmed
= 1;
3511 update_stateid(&stp
->st_stateid
);
3512 memcpy(&oc
->oc_resp_stateid
, &stp
->st_stateid
, sizeof(stateid_t
));
3513 dprintk("NFSD: %s: success, seqid=%d stateid=" STATEID_FMT
"\n",
3514 __func__
, oc
->oc_seqid
, STATEID_VAL(&stp
->st_stateid
));
3516 nfsd4_create_clid_dir(sop
->so_client
);
3518 if (oc
->oc_stateowner
) {
3519 nfs4_get_stateowner(oc
->oc_stateowner
);
3520 cstate
->replay_owner
= oc
->oc_stateowner
;
3522 nfs4_unlock_state();
3526 static inline void nfs4_file_downgrade(struct nfs4_stateid
*stp
, unsigned int to_access
)
3530 for (i
= 1; i
< 4; i
++) {
3531 if (test_bit(i
, &stp
->st_access_bmap
) && !(i
& to_access
)) {
3532 nfs4_file_put_access(stp
->st_file
, i
);
3533 __clear_bit(i
, &stp
->st_access_bmap
);
3539 reset_union_bmap_deny(unsigned long deny
, unsigned long *bmap
)
3542 for (i
= 0; i
< 4; i
++) {
3543 if ((i
& deny
) != i
)
3544 __clear_bit(i
, bmap
);
3549 nfsd4_open_downgrade(struct svc_rqst
*rqstp
,
3550 struct nfsd4_compound_state
*cstate
,
3551 struct nfsd4_open_downgrade
*od
)
3554 struct nfs4_stateid
*stp
;
3556 dprintk("NFSD: nfsd4_open_downgrade on file %.*s\n",
3557 (int)cstate
->current_fh
.fh_dentry
->d_name
.len
,
3558 cstate
->current_fh
.fh_dentry
->d_name
.name
);
3560 if (!access_valid(od
->od_share_access
, cstate
->minorversion
)
3561 || !deny_valid(od
->od_share_deny
))
3562 return nfserr_inval
;
3565 if ((status
= nfs4_preprocess_seqid_op(cstate
,
3569 &od
->od_stateowner
, &stp
, NULL
)))
3572 status
= nfserr_inval
;
3573 if (!test_bit(od
->od_share_access
, &stp
->st_access_bmap
)) {
3574 dprintk("NFSD:access not a subset current bitmap: 0x%lx, input access=%08x\n",
3575 stp
->st_access_bmap
, od
->od_share_access
);
3578 if (!test_bit(od
->od_share_deny
, &stp
->st_deny_bmap
)) {
3579 dprintk("NFSD:deny not a subset current bitmap: 0x%lx, input deny=%08x\n",
3580 stp
->st_deny_bmap
, od
->od_share_deny
);
3583 nfs4_file_downgrade(stp
, od
->od_share_access
);
3585 reset_union_bmap_deny(od
->od_share_deny
, &stp
->st_deny_bmap
);
3587 update_stateid(&stp
->st_stateid
);
3588 memcpy(&od
->od_stateid
, &stp
->st_stateid
, sizeof(stateid_t
));
3591 if (od
->od_stateowner
) {
3592 nfs4_get_stateowner(od
->od_stateowner
);
3593 cstate
->replay_owner
= od
->od_stateowner
;
3595 nfs4_unlock_state();
3600 * nfs4_unlock_state() called after encode
3603 nfsd4_close(struct svc_rqst
*rqstp
, struct nfsd4_compound_state
*cstate
,
3604 struct nfsd4_close
*close
)
3607 struct nfs4_stateid
*stp
;
3609 dprintk("NFSD: nfsd4_close on file %.*s\n",
3610 (int)cstate
->current_fh
.fh_dentry
->d_name
.len
,
3611 cstate
->current_fh
.fh_dentry
->d_name
.name
);
3614 /* check close_lru for replay */
3615 if ((status
= nfs4_preprocess_seqid_op(cstate
,
3618 OPEN_STATE
| CLOSE_STATE
,
3619 &close
->cl_stateowner
, &stp
, NULL
)))
3622 update_stateid(&stp
->st_stateid
);
3623 memcpy(&close
->cl_stateid
, &stp
->st_stateid
, sizeof(stateid_t
));
3625 /* release_stateid() calls nfsd_close() if needed */
3626 release_open_stateid(stp
);
3628 /* place unused nfs4_stateowners on so_close_lru list to be
3629 * released by the laundromat service after the lease period
3630 * to enable us to handle CLOSE replay
3632 if (list_empty(&close
->cl_stateowner
->so_stateids
))
3633 move_to_close_lru(close
->cl_stateowner
);
3635 if (close
->cl_stateowner
) {
3636 nfs4_get_stateowner(close
->cl_stateowner
);
3637 cstate
->replay_owner
= close
->cl_stateowner
;
3639 nfs4_unlock_state();
3644 nfsd4_delegreturn(struct svc_rqst
*rqstp
, struct nfsd4_compound_state
*cstate
,
3645 struct nfsd4_delegreturn
*dr
)
3647 struct nfs4_delegation
*dp
;
3648 stateid_t
*stateid
= &dr
->dr_stateid
;
3649 struct inode
*inode
;
3653 if ((status
= fh_verify(rqstp
, &cstate
->current_fh
, S_IFREG
, 0)))
3655 inode
= cstate
->current_fh
.fh_dentry
->d_inode
;
3657 if (nfsd4_has_session(cstate
))
3658 flags
|= HAS_SESSION
;
3660 status
= nfserr_bad_stateid
;
3661 if (ZERO_STATEID(stateid
) || ONE_STATEID(stateid
))
3663 status
= nfserr_stale_stateid
;
3664 if (STALE_STATEID(stateid
))
3666 status
= nfserr_bad_stateid
;
3667 if (!is_delegation_stateid(stateid
))
3669 status
= nfserr_expired
;
3670 dp
= find_delegation_stateid(inode
, stateid
);
3673 status
= check_stateid_generation(stateid
, &dp
->dl_stateid
, flags
);
3676 renew_client(dp
->dl_client
);
3678 unhash_delegation(dp
);
3680 nfs4_unlock_state();
3687 * Lock owner state (byte-range locks)
3689 #define LOFF_OVERFLOW(start, len) ((u64)(len) > ~(u64)(start))
3690 #define LOCK_HASH_BITS 8
3691 #define LOCK_HASH_SIZE (1 << LOCK_HASH_BITS)
3692 #define LOCK_HASH_MASK (LOCK_HASH_SIZE - 1)
3695 end_offset(u64 start
, u64 len
)
3700 return end
>= start
? end
: NFS4_MAX_UINT64
;
3703 /* last octet in a range */
3705 last_byte_offset(u64 start
, u64 len
)
3711 return end
> start
? end
- 1: NFS4_MAX_UINT64
;
3714 #define lockownerid_hashval(id) \
3715 ((id) & LOCK_HASH_MASK)
3717 static inline unsigned int
3718 lock_ownerstr_hashval(struct inode
*inode
, u32 cl_id
,
3719 struct xdr_netobj
*ownername
)
3721 return (file_hashval(inode
) + cl_id
3722 + opaque_hashval(ownername
->data
, ownername
->len
))
3726 static struct list_head lock_ownerid_hashtbl
[LOCK_HASH_SIZE
];
3727 static struct list_head lock_ownerstr_hashtbl
[LOCK_HASH_SIZE
];
3728 static struct list_head lockstateid_hashtbl
[STATEID_HASH_SIZE
];
3731 same_stateid(stateid_t
*id_one
, stateid_t
*id_two
)
3733 if (id_one
->si_stateownerid
!= id_two
->si_stateownerid
)
3735 return id_one
->si_fileid
== id_two
->si_fileid
;
3738 static struct nfs4_stateid
*
3739 find_stateid(stateid_t
*stid
, int flags
)
3741 struct nfs4_stateid
*local
;
3742 u32 st_id
= stid
->si_stateownerid
;
3743 u32 f_id
= stid
->si_fileid
;
3744 unsigned int hashval
;
3746 dprintk("NFSD: find_stateid flags 0x%x\n",flags
);
3747 if (flags
& (LOCK_STATE
| RD_STATE
| WR_STATE
)) {
3748 hashval
= stateid_hashval(st_id
, f_id
);
3749 list_for_each_entry(local
, &lockstateid_hashtbl
[hashval
], st_hash
) {
3750 if ((local
->st_stateid
.si_stateownerid
== st_id
) &&
3751 (local
->st_stateid
.si_fileid
== f_id
))
3756 if (flags
& (OPEN_STATE
| RD_STATE
| WR_STATE
)) {
3757 hashval
= stateid_hashval(st_id
, f_id
);
3758 list_for_each_entry(local
, &stateid_hashtbl
[hashval
], st_hash
) {
3759 if ((local
->st_stateid
.si_stateownerid
== st_id
) &&
3760 (local
->st_stateid
.si_fileid
== f_id
))
3767 static struct nfs4_stateid
*
3768 search_for_stateid(stateid_t
*stid
)
3770 struct nfs4_stateid
*local
;
3771 unsigned int hashval
= stateid_hashval(stid
->si_stateownerid
, stid
->si_fileid
);
3773 list_for_each_entry(local
, &lockstateid_hashtbl
[hashval
], st_hash
) {
3774 if (same_stateid(&local
->st_stateid
, stid
))
3778 list_for_each_entry(local
, &stateid_hashtbl
[hashval
], st_hash
) {
3779 if (same_stateid(&local
->st_stateid
, stid
))
3785 static struct nfs4_delegation
*
3786 search_for_delegation(stateid_t
*stid
)
3788 struct nfs4_file
*fp
;
3789 struct nfs4_delegation
*dp
;
3790 struct list_head
*pos
;
3793 for (i
= 0; i
< FILE_HASH_SIZE
; i
++) {
3794 list_for_each_entry(fp
, &file_hashtbl
[i
], fi_hash
) {
3795 list_for_each(pos
, &fp
->fi_delegations
) {
3796 dp
= list_entry(pos
, struct nfs4_delegation
, dl_perfile
);
3797 if (same_stateid(&dp
->dl_stateid
, stid
))
3805 static struct nfs4_delegation
*
3806 find_delegation_stateid(struct inode
*ino
, stateid_t
*stid
)
3808 struct nfs4_file
*fp
;
3809 struct nfs4_delegation
*dl
;
3811 dprintk("NFSD: %s: stateid=" STATEID_FMT
"\n", __func__
,
3814 fp
= find_file(ino
);
3817 dl
= find_delegation_file(fp
, stid
);
3823 * TODO: Linux file offsets are _signed_ 64-bit quantities, which means that
3824 * we can't properly handle lock requests that go beyond the (2^63 - 1)-th
3825 * byte, because of sign extension problems. Since NFSv4 calls for 64-bit
3826 * locking, this prevents us from being completely protocol-compliant. The
3827 * real solution to this problem is to start using unsigned file offsets in
3828 * the VFS, but this is a very deep change!
3831 nfs4_transform_lock_offset(struct file_lock
*lock
)
3833 if (lock
->fl_start
< 0)
3834 lock
->fl_start
= OFFSET_MAX
;
3835 if (lock
->fl_end
< 0)
3836 lock
->fl_end
= OFFSET_MAX
;
3839 /* Hack!: For now, we're defining this just so we can use a pointer to it
3840 * as a unique cookie to identify our (NFSv4's) posix locks. */
3841 static const struct lock_manager_operations nfsd_posix_mng_ops
= {
3845 nfs4_set_lock_denied(struct file_lock
*fl
, struct nfsd4_lock_denied
*deny
)
3847 struct nfs4_stateowner
*sop
;
3849 if (fl
->fl_lmops
== &nfsd_posix_mng_ops
) {
3850 sop
= (struct nfs4_stateowner
*) fl
->fl_owner
;
3851 kref_get(&sop
->so_ref
);
3853 deny
->ld_clientid
= sop
->so_client
->cl_clientid
;
3855 deny
->ld_sop
= NULL
;
3856 deny
->ld_clientid
.cl_boot
= 0;
3857 deny
->ld_clientid
.cl_id
= 0;
3859 deny
->ld_start
= fl
->fl_start
;
3860 deny
->ld_length
= NFS4_MAX_UINT64
;
3861 if (fl
->fl_end
!= NFS4_MAX_UINT64
)
3862 deny
->ld_length
= fl
->fl_end
- fl
->fl_start
+ 1;
3863 deny
->ld_type
= NFS4_READ_LT
;
3864 if (fl
->fl_type
!= F_RDLCK
)
3865 deny
->ld_type
= NFS4_WRITE_LT
;
3868 static struct nfs4_stateowner
*
3869 find_lockstateowner_str(struct inode
*inode
, clientid_t
*clid
,
3870 struct xdr_netobj
*owner
)
3872 unsigned int hashval
= lock_ownerstr_hashval(inode
, clid
->cl_id
, owner
);
3873 struct nfs4_stateowner
*op
;
3875 list_for_each_entry(op
, &lock_ownerstr_hashtbl
[hashval
], so_strhash
) {
3876 if (same_owner_str(op
, owner
, clid
))
3883 * Alloc a lock owner structure.
3884 * Called in nfsd4_lock - therefore, OPEN and OPEN_CONFIRM (if needed) has
3887 * strhashval = lock_ownerstr_hashval
3890 static struct nfs4_stateowner
*
3891 alloc_init_lock_stateowner(unsigned int strhashval
, struct nfs4_client
*clp
, struct nfs4_stateid
*open_stp
, struct nfsd4_lock
*lock
) {
3892 struct nfs4_stateowner
*sop
;
3893 struct nfs4_replay
*rp
;
3894 unsigned int idhashval
;
3896 if (!(sop
= alloc_stateowner(&lock
->lk_new_owner
)))
3898 idhashval
= lockownerid_hashval(current_ownerid
);
3899 INIT_LIST_HEAD(&sop
->so_idhash
);
3900 INIT_LIST_HEAD(&sop
->so_strhash
);
3901 INIT_LIST_HEAD(&sop
->so_perclient
);
3902 INIT_LIST_HEAD(&sop
->so_stateids
);
3903 INIT_LIST_HEAD(&sop
->so_perstateid
);
3904 INIT_LIST_HEAD(&sop
->so_close_lru
); /* not used */
3906 list_add(&sop
->so_idhash
, &lock_ownerid_hashtbl
[idhashval
]);
3907 list_add(&sop
->so_strhash
, &lock_ownerstr_hashtbl
[strhashval
]);
3908 list_add(&sop
->so_perstateid
, &open_stp
->st_lockowners
);
3909 sop
->so_is_open_owner
= 0;
3910 sop
->so_id
= current_ownerid
++;
3911 sop
->so_client
= clp
;
3912 /* It is the openowner seqid that will be incremented in encode in the
3913 * case of new lockowners; so increment the lock seqid manually: */
3914 sop
->so_seqid
= lock
->lk_new_lock_seqid
+ 1;
3915 sop
->so_confirmed
= 1;
3916 rp
= &sop
->so_replay
;
3917 rp
->rp_status
= nfserr_serverfault
;
3919 rp
->rp_buf
= rp
->rp_ibuf
;
3923 static struct nfs4_stateid
*
3924 alloc_init_lock_stateid(struct nfs4_stateowner
*sop
, struct nfs4_file
*fp
, struct nfs4_stateid
*open_stp
)
3926 struct nfs4_stateid
*stp
;
3927 unsigned int hashval
= stateid_hashval(sop
->so_id
, fp
->fi_id
);
3929 stp
= nfs4_alloc_stateid();
3932 INIT_LIST_HEAD(&stp
->st_hash
);
3933 INIT_LIST_HEAD(&stp
->st_perfile
);
3934 INIT_LIST_HEAD(&stp
->st_perstateowner
);
3935 INIT_LIST_HEAD(&stp
->st_lockowners
); /* not used */
3936 list_add(&stp
->st_hash
, &lockstateid_hashtbl
[hashval
]);
3937 list_add(&stp
->st_perfile
, &fp
->fi_stateids
);
3938 list_add(&stp
->st_perstateowner
, &sop
->so_stateids
);
3939 stp
->st_stateowner
= sop
;
3942 stp
->st_stateid
.si_boot
= boot_time
;
3943 stp
->st_stateid
.si_stateownerid
= sop
->so_id
;
3944 stp
->st_stateid
.si_fileid
= fp
->fi_id
;
3945 stp
->st_stateid
.si_generation
= 0;
3946 stp
->st_access_bmap
= 0;
3947 stp
->st_deny_bmap
= open_stp
->st_deny_bmap
;
3948 stp
->st_openstp
= open_stp
;
3955 check_lock_length(u64 offset
, u64 length
)
3957 return ((length
== 0) || ((length
!= NFS4_MAX_UINT64
) &&
3958 LOFF_OVERFLOW(offset
, length
)));
3961 static void get_lock_access(struct nfs4_stateid
*lock_stp
, u32 access
)
3963 struct nfs4_file
*fp
= lock_stp
->st_file
;
3964 int oflag
= nfs4_access_to_omode(access
);
3966 if (test_bit(access
, &lock_stp
->st_access_bmap
))
3968 nfs4_file_get_access(fp
, oflag
);
3969 __set_bit(access
, &lock_stp
->st_access_bmap
);
3976 nfsd4_lock(struct svc_rqst
*rqstp
, struct nfsd4_compound_state
*cstate
,
3977 struct nfsd4_lock
*lock
)
3979 struct nfs4_stateowner
*open_sop
= NULL
;
3980 struct nfs4_stateowner
*lock_sop
= NULL
;
3981 struct nfs4_stateid
*lock_stp
;
3982 struct nfs4_file
*fp
;
3983 struct file
*filp
= NULL
;
3984 struct file_lock file_lock
;
3985 struct file_lock conflock
;
3987 unsigned int strhashval
;
3990 dprintk("NFSD: nfsd4_lock: start=%Ld length=%Ld\n",
3991 (long long) lock
->lk_offset
,
3992 (long long) lock
->lk_length
);
3994 if (check_lock_length(lock
->lk_offset
, lock
->lk_length
))
3995 return nfserr_inval
;
3997 if ((status
= fh_verify(rqstp
, &cstate
->current_fh
,
3998 S_IFREG
, NFSD_MAY_LOCK
))) {
3999 dprintk("NFSD: nfsd4_lock: permission denied!\n");
4005 if (lock
->lk_is_new
) {
4007 * Client indicates that this is a new lockowner.
4008 * Use open owner and open stateid to create lock owner and
4011 struct nfs4_stateid
*open_stp
= NULL
;
4013 status
= nfserr_stale_clientid
;
4014 if (!nfsd4_has_session(cstate
) &&
4015 STALE_CLIENTID(&lock
->lk_new_clientid
))
4018 /* validate and update open stateid and open seqid */
4019 status
= nfs4_preprocess_seqid_op(cstate
,
4020 lock
->lk_new_open_seqid
,
4021 &lock
->lk_new_open_stateid
,
4023 &lock
->lk_replay_owner
, &open_stp
,
4027 open_sop
= lock
->lk_replay_owner
;
4028 /* create lockowner and lock stateid */
4029 fp
= open_stp
->st_file
;
4030 strhashval
= lock_ownerstr_hashval(fp
->fi_inode
,
4031 open_sop
->so_client
->cl_clientid
.cl_id
,
4032 &lock
->v
.new.owner
);
4033 /* XXX: Do we need to check for duplicate stateowners on
4034 * the same file, or should they just be allowed (and
4035 * create new stateids)? */
4036 status
= nfserr_resource
;
4037 lock_sop
= alloc_init_lock_stateowner(strhashval
,
4038 open_sop
->so_client
, open_stp
, lock
);
4039 if (lock_sop
== NULL
)
4041 lock_stp
= alloc_init_lock_stateid(lock_sop
, fp
, open_stp
);
4042 if (lock_stp
== NULL
)
4045 /* lock (lock owner + lock stateid) already exists */
4046 status
= nfs4_preprocess_seqid_op(cstate
,
4047 lock
->lk_old_lock_seqid
,
4048 &lock
->lk_old_lock_stateid
,
4050 &lock
->lk_replay_owner
, &lock_stp
, lock
);
4053 lock_sop
= lock
->lk_replay_owner
;
4054 fp
= lock_stp
->st_file
;
4056 /* lock->lk_replay_owner and lock_stp have been created or found */
4058 status
= nfserr_grace
;
4059 if (locks_in_grace() && !lock
->lk_reclaim
)
4061 status
= nfserr_no_grace
;
4062 if (!locks_in_grace() && lock
->lk_reclaim
)
4065 locks_init_lock(&file_lock
);
4066 switch (lock
->lk_type
) {
4069 filp
= find_readable_file(lock_stp
->st_file
);
4071 get_lock_access(lock_stp
, NFS4_SHARE_ACCESS_READ
);
4072 file_lock
.fl_type
= F_RDLCK
;
4075 case NFS4_WRITEW_LT
:
4076 filp
= find_writeable_file(lock_stp
->st_file
);
4078 get_lock_access(lock_stp
, NFS4_SHARE_ACCESS_WRITE
);
4079 file_lock
.fl_type
= F_WRLCK
;
4082 status
= nfserr_inval
;
4086 status
= nfserr_openmode
;
4089 file_lock
.fl_owner
= (fl_owner_t
)lock_sop
;
4090 file_lock
.fl_pid
= current
->tgid
;
4091 file_lock
.fl_file
= filp
;
4092 file_lock
.fl_flags
= FL_POSIX
;
4093 file_lock
.fl_lmops
= &nfsd_posix_mng_ops
;
4095 file_lock
.fl_start
= lock
->lk_offset
;
4096 file_lock
.fl_end
= last_byte_offset(lock
->lk_offset
, lock
->lk_length
);
4097 nfs4_transform_lock_offset(&file_lock
);
4100 * Try to lock the file in the VFS.
4101 * Note: locks.c uses the BKL to protect the inode's lock list.
4104 err
= vfs_lock_file(filp
, F_SETLK
, &file_lock
, &conflock
);
4106 case 0: /* success! */
4107 update_stateid(&lock_stp
->st_stateid
);
4108 memcpy(&lock
->lk_resp_stateid
, &lock_stp
->st_stateid
,
4112 case (EAGAIN
): /* conflock holds conflicting lock */
4113 status
= nfserr_denied
;
4114 dprintk("NFSD: nfsd4_lock: conflicting lock found!\n");
4115 nfs4_set_lock_denied(&conflock
, &lock
->lk_denied
);
4118 status
= nfserr_deadlock
;
4121 dprintk("NFSD: nfsd4_lock: vfs_lock_file() failed! status %d\n",err
);
4122 status
= nfserr_resource
;
4126 if (status
&& lock
->lk_is_new
&& lock_sop
)
4127 release_lockowner(lock_sop
);
4128 if (lock
->lk_replay_owner
) {
4129 nfs4_get_stateowner(lock
->lk_replay_owner
);
4130 cstate
->replay_owner
= lock
->lk_replay_owner
;
4132 nfs4_unlock_state();
4137 * The NFSv4 spec allows a client to do a LOCKT without holding an OPEN,
4138 * so we do a temporary open here just to get an open file to pass to
4139 * vfs_test_lock. (Arguably perhaps test_lock should be done with an
4142 static int nfsd_test_lock(struct svc_rqst
*rqstp
, struct svc_fh
*fhp
, struct file_lock
*lock
)
4147 err
= nfsd_open(rqstp
, fhp
, S_IFREG
, NFSD_MAY_READ
, &file
);
4150 err
= vfs_test_lock(file
, lock
);
4159 nfsd4_lockt(struct svc_rqst
*rqstp
, struct nfsd4_compound_state
*cstate
,
4160 struct nfsd4_lockt
*lockt
)
4162 struct inode
*inode
;
4163 struct file_lock file_lock
;
4167 if (locks_in_grace())
4168 return nfserr_grace
;
4170 if (check_lock_length(lockt
->lt_offset
, lockt
->lt_length
))
4171 return nfserr_inval
;
4173 lockt
->lt_stateowner
= NULL
;
4176 status
= nfserr_stale_clientid
;
4177 if (!nfsd4_has_session(cstate
) && STALE_CLIENTID(&lockt
->lt_clientid
))
4180 if ((status
= fh_verify(rqstp
, &cstate
->current_fh
, S_IFREG
, 0)))
4183 inode
= cstate
->current_fh
.fh_dentry
->d_inode
;
4184 locks_init_lock(&file_lock
);
4185 switch (lockt
->lt_type
) {
4188 file_lock
.fl_type
= F_RDLCK
;
4191 case NFS4_WRITEW_LT
:
4192 file_lock
.fl_type
= F_WRLCK
;
4195 dprintk("NFSD: nfs4_lockt: bad lock type!\n");
4196 status
= nfserr_inval
;
4200 lockt
->lt_stateowner
= find_lockstateowner_str(inode
,
4201 &lockt
->lt_clientid
, &lockt
->lt_owner
);
4202 if (lockt
->lt_stateowner
)
4203 file_lock
.fl_owner
= (fl_owner_t
)lockt
->lt_stateowner
;
4204 file_lock
.fl_pid
= current
->tgid
;
4205 file_lock
.fl_flags
= FL_POSIX
;
4207 file_lock
.fl_start
= lockt
->lt_offset
;
4208 file_lock
.fl_end
= last_byte_offset(lockt
->lt_offset
, lockt
->lt_length
);
4210 nfs4_transform_lock_offset(&file_lock
);
4213 error
= nfsd_test_lock(rqstp
, &cstate
->current_fh
, &file_lock
);
4215 status
= nfserrno(error
);
4218 if (file_lock
.fl_type
!= F_UNLCK
) {
4219 status
= nfserr_denied
;
4220 nfs4_set_lock_denied(&file_lock
, &lockt
->lt_denied
);
4223 nfs4_unlock_state();
4228 nfsd4_locku(struct svc_rqst
*rqstp
, struct nfsd4_compound_state
*cstate
,
4229 struct nfsd4_locku
*locku
)
4231 struct nfs4_stateid
*stp
;
4232 struct file
*filp
= NULL
;
4233 struct file_lock file_lock
;
4237 dprintk("NFSD: nfsd4_locku: start=%Ld length=%Ld\n",
4238 (long long) locku
->lu_offset
,
4239 (long long) locku
->lu_length
);
4241 if (check_lock_length(locku
->lu_offset
, locku
->lu_length
))
4242 return nfserr_inval
;
4246 if ((status
= nfs4_preprocess_seqid_op(cstate
,
4250 &locku
->lu_stateowner
, &stp
, NULL
)))
4253 filp
= find_any_file(stp
->st_file
);
4255 status
= nfserr_lock_range
;
4259 locks_init_lock(&file_lock
);
4260 file_lock
.fl_type
= F_UNLCK
;
4261 file_lock
.fl_owner
= (fl_owner_t
) locku
->lu_stateowner
;
4262 file_lock
.fl_pid
= current
->tgid
;
4263 file_lock
.fl_file
= filp
;
4264 file_lock
.fl_flags
= FL_POSIX
;
4265 file_lock
.fl_lmops
= &nfsd_posix_mng_ops
;
4266 file_lock
.fl_start
= locku
->lu_offset
;
4268 file_lock
.fl_end
= last_byte_offset(locku
->lu_offset
, locku
->lu_length
);
4269 nfs4_transform_lock_offset(&file_lock
);
4272 * Try to unlock the file in the VFS.
4274 err
= vfs_lock_file(filp
, F_SETLK
, &file_lock
, NULL
);
4276 dprintk("NFSD: nfs4_locku: vfs_lock_file failed!\n");
4280 * OK, unlock succeeded; the only thing left to do is update the stateid.
4282 update_stateid(&stp
->st_stateid
);
4283 memcpy(&locku
->lu_stateid
, &stp
->st_stateid
, sizeof(stateid_t
));
4286 if (locku
->lu_stateowner
) {
4287 nfs4_get_stateowner(locku
->lu_stateowner
);
4288 cstate
->replay_owner
= locku
->lu_stateowner
;
4290 nfs4_unlock_state();
4294 status
= nfserrno(err
);
4300 * 1: locks held by lockowner
4301 * 0: no locks held by lockowner
4304 check_for_locks(struct nfs4_file
*filp
, struct nfs4_stateowner
*lowner
)
4306 struct file_lock
**flpp
;
4307 struct inode
*inode
= filp
->fi_inode
;
4311 for (flpp
= &inode
->i_flock
; *flpp
!= NULL
; flpp
= &(*flpp
)->fl_next
) {
4312 if ((*flpp
)->fl_owner
== (fl_owner_t
)lowner
) {
4323 nfsd4_release_lockowner(struct svc_rqst
*rqstp
,
4324 struct nfsd4_compound_state
*cstate
,
4325 struct nfsd4_release_lockowner
*rlockowner
)
4327 clientid_t
*clid
= &rlockowner
->rl_clientid
;
4328 struct nfs4_stateowner
*sop
;
4329 struct nfs4_stateid
*stp
;
4330 struct xdr_netobj
*owner
= &rlockowner
->rl_owner
;
4331 struct list_head matches
;
4335 dprintk("nfsd4_release_lockowner clientid: (%08x/%08x):\n",
4336 clid
->cl_boot
, clid
->cl_id
);
4338 /* XXX check for lease expiration */
4340 status
= nfserr_stale_clientid
;
4341 if (STALE_CLIENTID(clid
))
4346 status
= nfserr_locks_held
;
4347 /* XXX: we're doing a linear search through all the lockowners.
4348 * Yipes! For now we'll just hope clients aren't really using
4349 * release_lockowner much, but eventually we have to fix these
4350 * data structures. */
4351 INIT_LIST_HEAD(&matches
);
4352 for (i
= 0; i
< LOCK_HASH_SIZE
; i
++) {
4353 list_for_each_entry(sop
, &lock_ownerid_hashtbl
[i
], so_idhash
) {
4354 if (!same_owner_str(sop
, owner
, clid
))
4356 list_for_each_entry(stp
, &sop
->so_stateids
,
4358 if (check_for_locks(stp
->st_file
, sop
))
4360 /* Note: so_perclient unused for lockowners,
4361 * so it's OK to fool with here. */
4362 list_add(&sop
->so_perclient
, &matches
);
4366 /* Clients probably won't expect us to return with some (but not all)
4367 * of the lockowner state released; so don't release any until all
4368 * have been checked. */
4370 while (!list_empty(&matches
)) {
4371 sop
= list_entry(matches
.next
, struct nfs4_stateowner
,
4373 /* unhash_stateowner deletes so_perclient only
4374 * for openowners. */
4375 list_del(&sop
->so_perclient
);
4376 release_lockowner(sop
);
4379 nfs4_unlock_state();
4383 static inline struct nfs4_client_reclaim
*
4386 return kmalloc(sizeof(struct nfs4_client_reclaim
), GFP_KERNEL
);
4390 nfs4_has_reclaimed_state(const char *name
, bool use_exchange_id
)
4392 unsigned int strhashval
= clientstr_hashval(name
);
4393 struct nfs4_client
*clp
;
4395 clp
= find_confirmed_client_by_str(name
, strhashval
);
4400 * failure => all reset bets are off, nfserr_no_grace...
4403 nfs4_client_to_reclaim(const char *name
)
4405 unsigned int strhashval
;
4406 struct nfs4_client_reclaim
*crp
= NULL
;
4408 dprintk("NFSD nfs4_client_to_reclaim NAME: %.*s\n", HEXDIR_LEN
, name
);
4409 crp
= alloc_reclaim();
4412 strhashval
= clientstr_hashval(name
);
4413 INIT_LIST_HEAD(&crp
->cr_strhash
);
4414 list_add(&crp
->cr_strhash
, &reclaim_str_hashtbl
[strhashval
]);
4415 memcpy(crp
->cr_recdir
, name
, HEXDIR_LEN
);
4416 reclaim_str_hashtbl_size
++;
4421 nfs4_release_reclaim(void)
4423 struct nfs4_client_reclaim
*crp
= NULL
;
4426 for (i
= 0; i
< CLIENT_HASH_SIZE
; i
++) {
4427 while (!list_empty(&reclaim_str_hashtbl
[i
])) {
4428 crp
= list_entry(reclaim_str_hashtbl
[i
].next
,
4429 struct nfs4_client_reclaim
, cr_strhash
);
4430 list_del(&crp
->cr_strhash
);
4432 reclaim_str_hashtbl_size
--;
4435 BUG_ON(reclaim_str_hashtbl_size
);
4439 * called from OPEN, CLAIM_PREVIOUS with a new clientid. */
4440 static struct nfs4_client_reclaim
*
4441 nfs4_find_reclaim_client(clientid_t
*clid
)
4443 unsigned int strhashval
;
4444 struct nfs4_client
*clp
;
4445 struct nfs4_client_reclaim
*crp
= NULL
;
4448 /* find clientid in conf_id_hashtbl */
4449 clp
= find_confirmed_client(clid
);
4453 dprintk("NFSD: nfs4_find_reclaim_client for %.*s with recdir %s\n",
4454 clp
->cl_name
.len
, clp
->cl_name
.data
,
4457 /* find clp->cl_name in reclaim_str_hashtbl */
4458 strhashval
= clientstr_hashval(clp
->cl_recdir
);
4459 list_for_each_entry(crp
, &reclaim_str_hashtbl
[strhashval
], cr_strhash
) {
4460 if (same_name(crp
->cr_recdir
, clp
->cl_recdir
)) {
4468 * Called from OPEN. Look for clientid in reclaim list.
4471 nfs4_check_open_reclaim(clientid_t
*clid
)
4473 return nfs4_find_reclaim_client(clid
) ? nfs_ok
: nfserr_reclaim_bad
;
4476 /* initialization to perform at module load time: */
4479 nfs4_state_init(void)
4483 status
= nfsd4_init_slabs();
4486 for (i
= 0; i
< CLIENT_HASH_SIZE
; i
++) {
4487 INIT_LIST_HEAD(&conf_id_hashtbl
[i
]);
4488 INIT_LIST_HEAD(&conf_str_hashtbl
[i
]);
4489 INIT_LIST_HEAD(&unconf_str_hashtbl
[i
]);
4490 INIT_LIST_HEAD(&unconf_id_hashtbl
[i
]);
4491 INIT_LIST_HEAD(&reclaim_str_hashtbl
[i
]);
4493 for (i
= 0; i
< SESSION_HASH_SIZE
; i
++)
4494 INIT_LIST_HEAD(&sessionid_hashtbl
[i
]);
4495 for (i
= 0; i
< FILE_HASH_SIZE
; i
++) {
4496 INIT_LIST_HEAD(&file_hashtbl
[i
]);
4498 for (i
= 0; i
< OWNER_HASH_SIZE
; i
++) {
4499 INIT_LIST_HEAD(&ownerstr_hashtbl
[i
]);
4500 INIT_LIST_HEAD(&ownerid_hashtbl
[i
]);
4502 for (i
= 0; i
< STATEID_HASH_SIZE
; i
++) {
4503 INIT_LIST_HEAD(&stateid_hashtbl
[i
]);
4504 INIT_LIST_HEAD(&lockstateid_hashtbl
[i
]);
4506 for (i
= 0; i
< LOCK_HASH_SIZE
; i
++) {
4507 INIT_LIST_HEAD(&lock_ownerid_hashtbl
[i
]);
4508 INIT_LIST_HEAD(&lock_ownerstr_hashtbl
[i
]);
4510 memset(&onestateid
, ~0, sizeof(stateid_t
));
4511 INIT_LIST_HEAD(&close_lru
);
4512 INIT_LIST_HEAD(&client_lru
);
4513 INIT_LIST_HEAD(&del_recall_lru
);
4514 reclaim_str_hashtbl_size
= 0;
4519 nfsd4_load_reboot_recovery_data(void)
4524 nfsd4_init_recdir();
4525 status
= nfsd4_recdir_load();
4526 nfs4_unlock_state();
4528 printk("NFSD: Failure reading reboot recovery data\n");
4532 * Since the lifetime of a delegation isn't limited to that of an open, a
4533 * client may quite reasonably hang on to a delegation as long as it has
4534 * the inode cached. This becomes an obvious problem the first time a
4535 * client's inode cache approaches the size of the server's total memory.
4537 * For now we avoid this problem by imposing a hard limit on the number
4538 * of delegations, which varies according to the server's memory size.
4541 set_max_delegations(void)
4544 * Allow at most 4 delegations per megabyte of RAM. Quick
4545 * estimates suggest that in the worst case (where every delegation
4546 * is for a different inode), a delegation could take about 1.5K,
4547 * giving a worst case usage of about 6% of memory.
4549 max_delegations
= nr_free_buffer_pages() >> (20 - 2 - PAGE_SHIFT
);
4552 /* initialization to perform when the nfsd service is started: */
4555 __nfs4_state_start(void)
4559 boot_time
= get_seconds();
4560 locks_start_grace(&nfsd4_manager
);
4561 printk(KERN_INFO
"NFSD: starting %ld-second grace period\n",
4563 ret
= set_callback_cred();
4566 laundry_wq
= create_singlethread_workqueue("nfsd4");
4567 if (laundry_wq
== NULL
)
4569 ret
= nfsd4_create_callback_queue();
4571 goto out_free_laundry
;
4572 queue_delayed_work(laundry_wq
, &laundromat_work
, nfsd4_grace
* HZ
);
4573 set_max_delegations();
4576 destroy_workqueue(laundry_wq
);
4581 nfs4_state_start(void)
4583 nfsd4_load_reboot_recovery_data();
4584 return __nfs4_state_start();
4588 __nfs4_state_shutdown(void)
4591 struct nfs4_client
*clp
= NULL
;
4592 struct nfs4_delegation
*dp
= NULL
;
4593 struct list_head
*pos
, *next
, reaplist
;
4595 for (i
= 0; i
< CLIENT_HASH_SIZE
; i
++) {
4596 while (!list_empty(&conf_id_hashtbl
[i
])) {
4597 clp
= list_entry(conf_id_hashtbl
[i
].next
, struct nfs4_client
, cl_idhash
);
4600 while (!list_empty(&unconf_str_hashtbl
[i
])) {
4601 clp
= list_entry(unconf_str_hashtbl
[i
].next
, struct nfs4_client
, cl_strhash
);
4605 INIT_LIST_HEAD(&reaplist
);
4606 spin_lock(&recall_lock
);
4607 list_for_each_safe(pos
, next
, &del_recall_lru
) {
4608 dp
= list_entry (pos
, struct nfs4_delegation
, dl_recall_lru
);
4609 list_move(&dp
->dl_recall_lru
, &reaplist
);
4611 spin_unlock(&recall_lock
);
4612 list_for_each_safe(pos
, next
, &reaplist
) {
4613 dp
= list_entry (pos
, struct nfs4_delegation
, dl_recall_lru
);
4614 list_del_init(&dp
->dl_recall_lru
);
4615 unhash_delegation(dp
);
4618 nfsd4_shutdown_recdir();
4622 nfs4_state_shutdown(void)
4624 cancel_delayed_work_sync(&laundromat_work
);
4625 destroy_workqueue(laundry_wq
);
4626 locks_end_grace(&nfsd4_manager
);
4628 nfs4_release_reclaim();
4629 __nfs4_state_shutdown();
4630 nfs4_unlock_state();
4631 nfsd4_destroy_callback_queue();