RT-AC56 3.0.0.4.374.37 core
[tomato.git] / release / src-rt-6.x.4708 / linux / linux-2.6.36 / fs / nfsd / nfs4state.c
bloba0a5606f81d63dd32bb593494ed3589cd1930df3
1 /*
2 * Copyright (c) 2001 The Regents of the University of Michigan.
3 * All rights reserved.
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
10 * are met:
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>
36 #include <linux/smp_lock.h>
37 #include <linux/slab.h>
38 #include <linux/namei.h>
39 #include <linux/swap.h>
40 #include <linux/sunrpc/svcauth_gss.h>
41 #include <linux/sunrpc/clnt.h>
42 #include "xdr4.h"
43 #include "vfs.h"
45 #define NFSDDBG_FACILITY NFSDDBG_PROC
47 /* Globals */
48 time_t nfsd4_lease = 90; /* default lease time */
49 time_t nfsd4_grace = 90;
50 static time_t boot_time;
51 static u32 current_ownerid = 1;
52 static u32 current_fileid = 1;
53 static u32 current_delegid = 1;
54 static stateid_t zerostateid; /* bits all 0 */
55 static stateid_t onestateid; /* bits all 1 */
56 static u64 current_sessionid = 1;
58 #define ZERO_STATEID(stateid) (!memcmp((stateid), &zerostateid, sizeof(stateid_t)))
59 #define ONE_STATEID(stateid) (!memcmp((stateid), &onestateid, sizeof(stateid_t)))
61 /* forward declarations */
62 static struct nfs4_stateid * find_stateid(stateid_t *stid, int flags);
63 static struct nfs4_delegation * find_delegation_stateid(struct inode *ino, stateid_t *stid);
64 static char user_recovery_dirname[PATH_MAX] = "/var/lib/nfs/v4recovery";
65 static void nfs4_set_recdir(char *recdir);
67 /* Locking: */
69 /* Currently used for almost all code touching nfsv4 state: */
70 static DEFINE_MUTEX(client_mutex);
73 * Currently used for the del_recall_lru and file hash table. In an
74 * effort to decrease the scope of the client_mutex, this spinlock may
75 * eventually cover more:
77 static DEFINE_SPINLOCK(recall_lock);
79 static struct kmem_cache *stateowner_slab = NULL;
80 static struct kmem_cache *file_slab = NULL;
81 static struct kmem_cache *stateid_slab = NULL;
82 static struct kmem_cache *deleg_slab = NULL;
84 void
85 nfs4_lock_state(void)
87 mutex_lock(&client_mutex);
90 void
91 nfs4_unlock_state(void)
93 mutex_unlock(&client_mutex);
96 static inline u32
97 opaque_hashval(const void *ptr, int nbytes)
99 unsigned char *cptr = (unsigned char *) ptr;
101 u32 x = 0;
102 while (nbytes--) {
103 x *= 37;
104 x += *cptr++;
106 return x;
109 static struct list_head del_recall_lru;
111 static inline void
112 put_nfs4_file(struct nfs4_file *fi)
114 if (atomic_dec_and_lock(&fi->fi_ref, &recall_lock)) {
115 list_del(&fi->fi_hash);
116 spin_unlock(&recall_lock);
117 iput(fi->fi_inode);
118 kmem_cache_free(file_slab, fi);
122 static inline void
123 get_nfs4_file(struct nfs4_file *fi)
125 atomic_inc(&fi->fi_ref);
128 static int num_delegations;
129 unsigned int max_delegations;
132 * Open owner state (share locks)
135 /* hash tables for nfs4_stateowner */
136 #define OWNER_HASH_BITS 8
137 #define OWNER_HASH_SIZE (1 << OWNER_HASH_BITS)
138 #define OWNER_HASH_MASK (OWNER_HASH_SIZE - 1)
140 #define ownerid_hashval(id) \
141 ((id) & OWNER_HASH_MASK)
142 #define ownerstr_hashval(clientid, ownername) \
143 (((clientid) + opaque_hashval((ownername.data), (ownername.len))) & OWNER_HASH_MASK)
145 static struct list_head ownerid_hashtbl[OWNER_HASH_SIZE];
146 static struct list_head ownerstr_hashtbl[OWNER_HASH_SIZE];
148 /* hash table for nfs4_file */
149 #define FILE_HASH_BITS 8
150 #define FILE_HASH_SIZE (1 << FILE_HASH_BITS)
151 #define FILE_HASH_MASK (FILE_HASH_SIZE - 1)
152 /* hash table for (open)nfs4_stateid */
153 #define STATEID_HASH_BITS 10
154 #define STATEID_HASH_SIZE (1 << STATEID_HASH_BITS)
155 #define STATEID_HASH_MASK (STATEID_HASH_SIZE - 1)
157 #define file_hashval(x) \
158 hash_ptr(x, FILE_HASH_BITS)
159 #define stateid_hashval(owner_id, file_id) \
160 (((owner_id) + (file_id)) & STATEID_HASH_MASK)
162 static struct list_head file_hashtbl[FILE_HASH_SIZE];
163 static struct list_head stateid_hashtbl[STATEID_HASH_SIZE];
165 static void __nfs4_file_get_access(struct nfs4_file *fp, int oflag)
167 BUG_ON(!(fp->fi_fds[oflag] || fp->fi_fds[O_RDWR]));
168 atomic_inc(&fp->fi_access[oflag]);
171 static void nfs4_file_get_access(struct nfs4_file *fp, int oflag)
173 if (oflag == O_RDWR) {
174 __nfs4_file_get_access(fp, O_RDONLY);
175 __nfs4_file_get_access(fp, O_WRONLY);
176 } else
177 __nfs4_file_get_access(fp, oflag);
180 static void nfs4_file_put_fd(struct nfs4_file *fp, int oflag)
182 if (fp->fi_fds[oflag]) {
183 fput(fp->fi_fds[oflag]);
184 fp->fi_fds[oflag] = NULL;
188 static void __nfs4_file_put_access(struct nfs4_file *fp, int oflag)
190 if (atomic_dec_and_test(&fp->fi_access[oflag])) {
191 nfs4_file_put_fd(fp, O_RDWR);
192 nfs4_file_put_fd(fp, oflag);
196 static void nfs4_file_put_access(struct nfs4_file *fp, int oflag)
198 if (oflag == O_RDWR) {
199 __nfs4_file_put_access(fp, O_RDONLY);
200 __nfs4_file_put_access(fp, O_WRONLY);
201 } else
202 __nfs4_file_put_access(fp, oflag);
205 static struct nfs4_delegation *
206 alloc_init_deleg(struct nfs4_client *clp, struct nfs4_stateid *stp, struct svc_fh *current_fh, u32 type)
208 struct nfs4_delegation *dp;
209 struct nfs4_file *fp = stp->st_file;
210 struct nfs4_cb_conn *cb = &stp->st_stateowner->so_client->cl_cb_conn;
212 dprintk("NFSD alloc_init_deleg\n");
214 * Major work on the lease subsystem (for example, to support
215 * calbacks on stat) will be required before we can support
216 * write delegations properly.
218 if (type != NFS4_OPEN_DELEGATE_READ)
219 return NULL;
220 if (fp->fi_had_conflict)
221 return NULL;
222 if (num_delegations > max_delegations)
223 return NULL;
224 dp = kmem_cache_alloc(deleg_slab, GFP_KERNEL);
225 if (dp == NULL)
226 return dp;
227 num_delegations++;
228 INIT_LIST_HEAD(&dp->dl_perfile);
229 INIT_LIST_HEAD(&dp->dl_perclnt);
230 INIT_LIST_HEAD(&dp->dl_recall_lru);
231 dp->dl_client = clp;
232 get_nfs4_file(fp);
233 dp->dl_file = fp;
234 nfs4_file_get_access(fp, O_RDONLY);
235 dp->dl_flock = NULL;
236 dp->dl_type = type;
237 dp->dl_ident = cb->cb_ident;
238 dp->dl_stateid.si_boot = boot_time;
239 dp->dl_stateid.si_stateownerid = current_delegid++;
240 dp->dl_stateid.si_fileid = 0;
241 dp->dl_stateid.si_generation = 0;
242 fh_copy_shallow(&dp->dl_fh, &current_fh->fh_handle);
243 dp->dl_time = 0;
244 atomic_set(&dp->dl_count, 1);
245 list_add(&dp->dl_perfile, &fp->fi_delegations);
246 list_add(&dp->dl_perclnt, &clp->cl_delegations);
247 INIT_WORK(&dp->dl_recall.cb_work, nfsd4_do_callback_rpc);
248 return dp;
251 void
252 nfs4_put_delegation(struct nfs4_delegation *dp)
254 if (atomic_dec_and_test(&dp->dl_count)) {
255 dprintk("NFSD: freeing dp %p\n",dp);
256 put_nfs4_file(dp->dl_file);
257 kmem_cache_free(deleg_slab, dp);
258 num_delegations--;
262 /* Remove the associated file_lock first, then remove the delegation.
263 * lease_modify() is called to remove the FS_LEASE file_lock from
264 * the i_flock list, eventually calling nfsd's lock_manager
265 * fl_release_callback.
267 static void
268 nfs4_close_delegation(struct nfs4_delegation *dp)
270 struct file *filp = find_readable_file(dp->dl_file);
272 dprintk("NFSD: close_delegation dp %p\n",dp);
273 if (dp->dl_flock)
274 vfs_setlease(filp, F_UNLCK, &dp->dl_flock);
275 nfs4_file_put_access(dp->dl_file, O_RDONLY);
278 /* Called under the state lock. */
279 static void
280 unhash_delegation(struct nfs4_delegation *dp)
282 list_del_init(&dp->dl_perfile);
283 list_del_init(&dp->dl_perclnt);
284 spin_lock(&recall_lock);
285 list_del_init(&dp->dl_recall_lru);
286 spin_unlock(&recall_lock);
287 nfs4_close_delegation(dp);
288 nfs4_put_delegation(dp);
292 * SETCLIENTID state
295 /* client_lock protects the client lru list and session hash table */
296 static DEFINE_SPINLOCK(client_lock);
298 /* Hash tables for nfs4_clientid state */
299 #define CLIENT_HASH_BITS 4
300 #define CLIENT_HASH_SIZE (1 << CLIENT_HASH_BITS)
301 #define CLIENT_HASH_MASK (CLIENT_HASH_SIZE - 1)
303 #define clientid_hashval(id) \
304 ((id) & CLIENT_HASH_MASK)
305 #define clientstr_hashval(name) \
306 (opaque_hashval((name), 8) & CLIENT_HASH_MASK)
308 * reclaim_str_hashtbl[] holds known client info from previous reset/reboot
309 * used in reboot/reset lease grace period processing
311 * conf_id_hashtbl[], and conf_str_hashtbl[] hold confirmed
312 * setclientid_confirmed info.
314 * unconf_str_hastbl[] and unconf_id_hashtbl[] hold unconfirmed
315 * setclientid info.
317 * client_lru holds client queue ordered by nfs4_client.cl_time
318 * for lease renewal.
320 * close_lru holds (open) stateowner queue ordered by nfs4_stateowner.so_time
321 * for last close replay.
323 static struct list_head reclaim_str_hashtbl[CLIENT_HASH_SIZE];
324 static int reclaim_str_hashtbl_size = 0;
325 static struct list_head conf_id_hashtbl[CLIENT_HASH_SIZE];
326 static struct list_head conf_str_hashtbl[CLIENT_HASH_SIZE];
327 static struct list_head unconf_str_hashtbl[CLIENT_HASH_SIZE];
328 static struct list_head unconf_id_hashtbl[CLIENT_HASH_SIZE];
329 static struct list_head client_lru;
330 static struct list_head close_lru;
332 static void unhash_generic_stateid(struct nfs4_stateid *stp)
334 list_del(&stp->st_hash);
335 list_del(&stp->st_perfile);
336 list_del(&stp->st_perstateowner);
339 static void free_generic_stateid(struct nfs4_stateid *stp)
341 put_nfs4_file(stp->st_file);
342 kmem_cache_free(stateid_slab, stp);
345 static void release_lock_stateid(struct nfs4_stateid *stp)
347 struct file *file;
349 unhash_generic_stateid(stp);
350 file = find_any_file(stp->st_file);
351 if (file)
352 locks_remove_posix(file, (fl_owner_t)stp->st_stateowner);
353 free_generic_stateid(stp);
356 static void unhash_lockowner(struct nfs4_stateowner *sop)
358 struct nfs4_stateid *stp;
360 list_del(&sop->so_idhash);
361 list_del(&sop->so_strhash);
362 list_del(&sop->so_perstateid);
363 while (!list_empty(&sop->so_stateids)) {
364 stp = list_first_entry(&sop->so_stateids,
365 struct nfs4_stateid, st_perstateowner);
366 release_lock_stateid(stp);
370 static void release_lockowner(struct nfs4_stateowner *sop)
372 unhash_lockowner(sop);
373 nfs4_put_stateowner(sop);
376 static void
377 release_stateid_lockowners(struct nfs4_stateid *open_stp)
379 struct nfs4_stateowner *lock_sop;
381 while (!list_empty(&open_stp->st_lockowners)) {
382 lock_sop = list_entry(open_stp->st_lockowners.next,
383 struct nfs4_stateowner, so_perstateid);
384 /* list_del(&open_stp->st_lockowners); */
385 BUG_ON(lock_sop->so_is_open_owner);
386 release_lockowner(lock_sop);
390 static void
391 set_access(unsigned int *access, unsigned long bmap) {
392 int i;
394 *access = 0;
395 for (i = 1; i < 4; i++) {
396 if (test_bit(i, &bmap))
397 *access |= i;
401 static void
402 set_deny(unsigned int *deny, unsigned long bmap) {
403 int i;
405 *deny = 0;
406 for (i = 0; i < 4; i++) {
407 if (test_bit(i, &bmap))
408 *deny |= i ;
412 static int
413 test_share(struct nfs4_stateid *stp, struct nfsd4_open *open) {
414 unsigned int access, deny;
416 set_access(&access, stp->st_access_bmap);
417 set_deny(&deny, stp->st_deny_bmap);
418 if ((access & open->op_share_deny) || (deny & open->op_share_access))
419 return 0;
420 return 1;
423 static int nfs4_access_to_omode(u32 access)
425 switch (access & NFS4_SHARE_ACCESS_BOTH) {
426 case NFS4_SHARE_ACCESS_READ:
427 return O_RDONLY;
428 case NFS4_SHARE_ACCESS_WRITE:
429 return O_WRONLY;
430 case NFS4_SHARE_ACCESS_BOTH:
431 return O_RDWR;
433 BUG();
436 static int nfs4_access_bmap_to_omode(struct nfs4_stateid *stp)
438 unsigned int access;
440 set_access(&access, stp->st_access_bmap);
441 return nfs4_access_to_omode(access);
444 static void release_open_stateid(struct nfs4_stateid *stp)
446 int oflag = nfs4_access_bmap_to_omode(stp);
448 unhash_generic_stateid(stp);
449 release_stateid_lockowners(stp);
450 nfs4_file_put_access(stp->st_file, oflag);
451 free_generic_stateid(stp);
454 static void unhash_openowner(struct nfs4_stateowner *sop)
456 struct nfs4_stateid *stp;
458 list_del(&sop->so_idhash);
459 list_del(&sop->so_strhash);
460 list_del(&sop->so_perclient);
461 list_del(&sop->so_perstateid);
462 while (!list_empty(&sop->so_stateids)) {
463 stp = list_first_entry(&sop->so_stateids,
464 struct nfs4_stateid, st_perstateowner);
465 release_open_stateid(stp);
469 static void release_openowner(struct nfs4_stateowner *sop)
471 unhash_openowner(sop);
472 list_del(&sop->so_close_lru);
473 nfs4_put_stateowner(sop);
476 #define SESSION_HASH_SIZE 512
477 static struct list_head sessionid_hashtbl[SESSION_HASH_SIZE];
479 static inline int
480 hash_sessionid(struct nfs4_sessionid *sessionid)
482 struct nfsd4_sessionid *sid = (struct nfsd4_sessionid *)sessionid;
484 return sid->sequence % SESSION_HASH_SIZE;
487 static inline void
488 dump_sessionid(const char *fn, struct nfs4_sessionid *sessionid)
490 u32 *ptr = (u32 *)(&sessionid->data[0]);
491 dprintk("%s: %u:%u:%u:%u\n", fn, ptr[0], ptr[1], ptr[2], ptr[3]);
494 static void
495 gen_sessionid(struct nfsd4_session *ses)
497 struct nfs4_client *clp = ses->se_client;
498 struct nfsd4_sessionid *sid;
500 sid = (struct nfsd4_sessionid *)ses->se_sessionid.data;
501 sid->clientid = clp->cl_clientid;
502 sid->sequence = current_sessionid++;
503 sid->reserved = 0;
507 * The protocol defines ca_maxresponssize_cached to include the size of
508 * the rpc header, but all we need to cache is the data starting after
509 * the end of the initial SEQUENCE operation--the rest we regenerate
510 * each time. Therefore we can advertise a ca_maxresponssize_cached
511 * value that is the number of bytes in our cache plus a few additional
512 * bytes. In order to stay on the safe side, and not promise more than
513 * we can cache, those additional bytes must be the minimum possible: 24
514 * bytes of rpc header (xid through accept state, with AUTH_NULL
515 * verifier), 12 for the compound header (with zero-length tag), and 44
516 * for the SEQUENCE op response:
518 #define NFSD_MIN_HDR_SEQ_SZ (24 + 12 + 44)
521 * Give the client the number of ca_maxresponsesize_cached slots it
522 * requests, of size bounded by NFSD_SLOT_CACHE_SIZE,
523 * NFSD_MAX_MEM_PER_SESSION, and nfsd_drc_max_mem. Do not allow more
524 * than NFSD_MAX_SLOTS_PER_SESSION.
526 * If we run out of reserved DRC memory we should (up to a point)
527 * re-negotiate active sessions and reduce their slot usage to make
528 * rooom for new connections. For now we just fail the create session.
530 static int set_forechannel_drc_size(struct nfsd4_channel_attrs *fchan)
532 int mem, size = fchan->maxresp_cached;
534 if (fchan->maxreqs < 1)
535 return nfserr_inval;
537 if (size < NFSD_MIN_HDR_SEQ_SZ)
538 size = NFSD_MIN_HDR_SEQ_SZ;
539 size -= NFSD_MIN_HDR_SEQ_SZ;
540 if (size > NFSD_SLOT_CACHE_SIZE)
541 size = NFSD_SLOT_CACHE_SIZE;
543 /* bound the maxreqs by NFSD_MAX_MEM_PER_SESSION */
544 mem = fchan->maxreqs * size;
545 if (mem > NFSD_MAX_MEM_PER_SESSION) {
546 fchan->maxreqs = NFSD_MAX_MEM_PER_SESSION / size;
547 if (fchan->maxreqs > NFSD_MAX_SLOTS_PER_SESSION)
548 fchan->maxreqs = NFSD_MAX_SLOTS_PER_SESSION;
549 mem = fchan->maxreqs * size;
552 spin_lock(&nfsd_drc_lock);
553 /* bound the total session drc memory ussage */
554 if (mem + nfsd_drc_mem_used > nfsd_drc_max_mem) {
555 fchan->maxreqs = (nfsd_drc_max_mem - nfsd_drc_mem_used) / size;
556 mem = fchan->maxreqs * size;
558 nfsd_drc_mem_used += mem;
559 spin_unlock(&nfsd_drc_lock);
561 if (fchan->maxreqs == 0)
562 return nfserr_jukebox;
564 fchan->maxresp_cached = size + NFSD_MIN_HDR_SEQ_SZ;
565 return 0;
569 * fchan holds the client values on input, and the server values on output
570 * sv_max_mesg is the maximum payload plus one page for overhead.
572 static int init_forechannel_attrs(struct svc_rqst *rqstp,
573 struct nfsd4_channel_attrs *session_fchan,
574 struct nfsd4_channel_attrs *fchan)
576 int status = 0;
577 __u32 maxcount = nfsd_serv->sv_max_mesg;
579 /* headerpadsz set to zero in encode routine */
581 /* Use the client's max request and max response size if possible */
582 if (fchan->maxreq_sz > maxcount)
583 fchan->maxreq_sz = maxcount;
584 session_fchan->maxreq_sz = fchan->maxreq_sz;
586 if (fchan->maxresp_sz > maxcount)
587 fchan->maxresp_sz = maxcount;
588 session_fchan->maxresp_sz = fchan->maxresp_sz;
590 /* Use the client's maxops if possible */
591 if (fchan->maxops > NFSD_MAX_OPS_PER_COMPOUND)
592 fchan->maxops = NFSD_MAX_OPS_PER_COMPOUND;
593 session_fchan->maxops = fchan->maxops;
595 status = set_forechannel_drc_size(fchan);
597 session_fchan->maxresp_cached = fchan->maxresp_cached;
598 session_fchan->maxreqs = fchan->maxreqs;
600 dprintk("%s status %d\n", __func__, status);
601 return status;
604 static void
605 free_session_slots(struct nfsd4_session *ses)
607 int i;
609 for (i = 0; i < ses->se_fchannel.maxreqs; i++)
610 kfree(ses->se_slots[i]);
614 * We don't actually need to cache the rpc and session headers, so we
615 * can allocate a little less for each slot:
617 static inline int slot_bytes(struct nfsd4_channel_attrs *ca)
619 return ca->maxresp_cached - NFSD_MIN_HDR_SEQ_SZ;
622 static int
623 alloc_init_session(struct svc_rqst *rqstp, struct nfs4_client *clp,
624 struct nfsd4_create_session *cses)
626 struct nfsd4_session *new, tmp;
627 struct nfsd4_slot *sp;
628 int idx, slotsize, cachesize, i;
629 int status;
631 memset(&tmp, 0, sizeof(tmp));
633 tmp.se_bchannel = cses->back_channel;
634 status = init_forechannel_attrs(rqstp, &tmp.se_fchannel,
635 &cses->fore_channel);
636 if (status)
637 goto out;
639 BUILD_BUG_ON(NFSD_MAX_SLOTS_PER_SESSION * sizeof(struct nfsd4_slot)
640 + sizeof(struct nfsd4_session) > PAGE_SIZE);
642 status = nfserr_jukebox;
643 /* allocate struct nfsd4_session and slot table pointers in one piece */
644 slotsize = tmp.se_fchannel.maxreqs * sizeof(struct nfsd4_slot *);
645 new = kzalloc(sizeof(*new) + slotsize, GFP_KERNEL);
646 if (!new)
647 goto out;
649 memcpy(new, &tmp, sizeof(*new));
651 /* allocate each struct nfsd4_slot and data cache in one piece */
652 cachesize = slot_bytes(&new->se_fchannel);
653 for (i = 0; i < new->se_fchannel.maxreqs; i++) {
654 sp = kzalloc(sizeof(*sp) + cachesize, GFP_KERNEL);
655 if (!sp)
656 goto out_free;
657 new->se_slots[i] = sp;
660 new->se_client = clp;
661 gen_sessionid(new);
662 idx = hash_sessionid(&new->se_sessionid);
663 memcpy(clp->cl_sessionid.data, new->se_sessionid.data,
664 NFS4_MAX_SESSIONID_LEN);
666 new->se_flags = cses->flags;
667 kref_init(&new->se_ref);
668 spin_lock(&client_lock);
669 list_add(&new->se_hash, &sessionid_hashtbl[idx]);
670 list_add(&new->se_perclnt, &clp->cl_sessions);
671 spin_unlock(&client_lock);
673 status = nfs_ok;
674 out:
675 return status;
676 out_free:
677 free_session_slots(new);
678 kfree(new);
679 goto out;
682 /* caller must hold client_lock */
683 static struct nfsd4_session *
684 find_in_sessionid_hashtbl(struct nfs4_sessionid *sessionid)
686 struct nfsd4_session *elem;
687 int idx;
689 dump_sessionid(__func__, sessionid);
690 idx = hash_sessionid(sessionid);
691 /* Search in the appropriate list */
692 list_for_each_entry(elem, &sessionid_hashtbl[idx], se_hash) {
693 if (!memcmp(elem->se_sessionid.data, sessionid->data,
694 NFS4_MAX_SESSIONID_LEN)) {
695 return elem;
699 dprintk("%s: session not found\n", __func__);
700 return NULL;
703 /* caller must hold client_lock */
704 static void
705 unhash_session(struct nfsd4_session *ses)
707 list_del(&ses->se_hash);
708 list_del(&ses->se_perclnt);
711 void
712 free_session(struct kref *kref)
714 struct nfsd4_session *ses;
715 int mem;
717 ses = container_of(kref, struct nfsd4_session, se_ref);
718 spin_lock(&nfsd_drc_lock);
719 mem = ses->se_fchannel.maxreqs * slot_bytes(&ses->se_fchannel);
720 nfsd_drc_mem_used -= mem;
721 spin_unlock(&nfsd_drc_lock);
722 free_session_slots(ses);
723 kfree(ses);
726 /* must be called under the client_lock */
727 static inline void
728 renew_client_locked(struct nfs4_client *clp)
730 if (is_client_expired(clp)) {
731 dprintk("%s: client (clientid %08x/%08x) already expired\n",
732 __func__,
733 clp->cl_clientid.cl_boot,
734 clp->cl_clientid.cl_id);
735 return;
739 * Move client to the end to the LRU list.
741 dprintk("renewing client (clientid %08x/%08x)\n",
742 clp->cl_clientid.cl_boot,
743 clp->cl_clientid.cl_id);
744 list_move_tail(&clp->cl_lru, &client_lru);
745 clp->cl_time = get_seconds();
748 static inline void
749 renew_client(struct nfs4_client *clp)
751 spin_lock(&client_lock);
752 renew_client_locked(clp);
753 spin_unlock(&client_lock);
756 /* SETCLIENTID and SETCLIENTID_CONFIRM Helper functions */
757 static int
758 STALE_CLIENTID(clientid_t *clid)
760 if (clid->cl_boot == boot_time)
761 return 0;
762 dprintk("NFSD stale clientid (%08x/%08x) boot_time %08lx\n",
763 clid->cl_boot, clid->cl_id, boot_time);
764 return 1;
767 static struct nfs4_client *alloc_client(struct xdr_netobj name)
769 struct nfs4_client *clp;
771 clp = kzalloc(sizeof(struct nfs4_client), GFP_KERNEL);
772 if (clp == NULL)
773 return NULL;
774 clp->cl_name.data = kmalloc(name.len, GFP_KERNEL);
775 if (clp->cl_name.data == NULL) {
776 kfree(clp);
777 return NULL;
779 memcpy(clp->cl_name.data, name.data, name.len);
780 clp->cl_name.len = name.len;
781 return clp;
784 static inline void
785 free_client(struct nfs4_client *clp)
787 if (clp->cl_cred.cr_group_info)
788 put_group_info(clp->cl_cred.cr_group_info);
789 kfree(clp->cl_principal);
790 kfree(clp->cl_name.data);
791 kfree(clp);
794 void
795 release_session_client(struct nfsd4_session *session)
797 struct nfs4_client *clp = session->se_client;
799 if (!atomic_dec_and_lock(&clp->cl_refcount, &client_lock))
800 return;
801 if (is_client_expired(clp)) {
802 free_client(clp);
803 session->se_client = NULL;
804 } else
805 renew_client_locked(clp);
806 spin_unlock(&client_lock);
809 /* must be called under the client_lock */
810 static inline void
811 unhash_client_locked(struct nfs4_client *clp)
813 mark_client_expired(clp);
814 list_del(&clp->cl_lru);
815 while (!list_empty(&clp->cl_sessions)) {
816 struct nfsd4_session *ses;
817 ses = list_entry(clp->cl_sessions.next, struct nfsd4_session,
818 se_perclnt);
819 unhash_session(ses);
820 nfsd4_put_session(ses);
824 static void
825 expire_client(struct nfs4_client *clp)
827 struct nfs4_stateowner *sop;
828 struct nfs4_delegation *dp;
829 struct list_head reaplist;
831 INIT_LIST_HEAD(&reaplist);
832 spin_lock(&recall_lock);
833 while (!list_empty(&clp->cl_delegations)) {
834 dp = list_entry(clp->cl_delegations.next, struct nfs4_delegation, dl_perclnt);
835 dprintk("NFSD: expire client. dp %p, fp %p\n", dp,
836 dp->dl_flock);
837 list_del_init(&dp->dl_perclnt);
838 list_move(&dp->dl_recall_lru, &reaplist);
840 spin_unlock(&recall_lock);
841 while (!list_empty(&reaplist)) {
842 dp = list_entry(reaplist.next, struct nfs4_delegation, dl_recall_lru);
843 list_del_init(&dp->dl_recall_lru);
844 unhash_delegation(dp);
846 while (!list_empty(&clp->cl_openowners)) {
847 sop = list_entry(clp->cl_openowners.next, struct nfs4_stateowner, so_perclient);
848 release_openowner(sop);
850 nfsd4_set_callback_client(clp, NULL);
851 if (clp->cl_cb_conn.cb_xprt)
852 svc_xprt_put(clp->cl_cb_conn.cb_xprt);
853 list_del(&clp->cl_idhash);
854 list_del(&clp->cl_strhash);
855 spin_lock(&client_lock);
856 unhash_client_locked(clp);
857 if (atomic_read(&clp->cl_refcount) == 0)
858 free_client(clp);
859 spin_unlock(&client_lock);
862 static void copy_verf(struct nfs4_client *target, nfs4_verifier *source)
864 memcpy(target->cl_verifier.data, source->data,
865 sizeof(target->cl_verifier.data));
868 static void copy_clid(struct nfs4_client *target, struct nfs4_client *source)
870 target->cl_clientid.cl_boot = source->cl_clientid.cl_boot;
871 target->cl_clientid.cl_id = source->cl_clientid.cl_id;
874 static void copy_cred(struct svc_cred *target, struct svc_cred *source)
876 target->cr_uid = source->cr_uid;
877 target->cr_gid = source->cr_gid;
878 target->cr_group_info = source->cr_group_info;
879 get_group_info(target->cr_group_info);
882 static int same_name(const char *n1, const char *n2)
884 return 0 == memcmp(n1, n2, HEXDIR_LEN);
887 static int
888 same_verf(nfs4_verifier *v1, nfs4_verifier *v2)
890 return 0 == memcmp(v1->data, v2->data, sizeof(v1->data));
893 static int
894 same_clid(clientid_t *cl1, clientid_t *cl2)
896 return (cl1->cl_boot == cl2->cl_boot) && (cl1->cl_id == cl2->cl_id);
899 static int
900 same_creds(struct svc_cred *cr1, struct svc_cred *cr2)
902 return cr1->cr_uid == cr2->cr_uid;
905 static void gen_clid(struct nfs4_client *clp)
907 static u32 current_clientid = 1;
909 clp->cl_clientid.cl_boot = boot_time;
910 clp->cl_clientid.cl_id = current_clientid++;
913 static void gen_confirm(struct nfs4_client *clp)
915 static u32 i;
916 u32 *p;
918 p = (u32 *)clp->cl_confirm.data;
919 *p++ = get_seconds();
920 *p++ = i++;
923 static struct nfs4_client *create_client(struct xdr_netobj name, char *recdir,
924 struct svc_rqst *rqstp, nfs4_verifier *verf)
926 struct nfs4_client *clp;
927 struct sockaddr *sa = svc_addr(rqstp);
928 char *princ;
930 clp = alloc_client(name);
931 if (clp == NULL)
932 return NULL;
934 princ = svc_gss_principal(rqstp);
935 if (princ) {
936 clp->cl_principal = kstrdup(princ, GFP_KERNEL);
937 if (clp->cl_principal == NULL) {
938 free_client(clp);
939 return NULL;
943 memcpy(clp->cl_recdir, recdir, HEXDIR_LEN);
944 atomic_set(&clp->cl_refcount, 0);
945 atomic_set(&clp->cl_cb_set, 0);
946 INIT_LIST_HEAD(&clp->cl_idhash);
947 INIT_LIST_HEAD(&clp->cl_strhash);
948 INIT_LIST_HEAD(&clp->cl_openowners);
949 INIT_LIST_HEAD(&clp->cl_delegations);
950 INIT_LIST_HEAD(&clp->cl_sessions);
951 INIT_LIST_HEAD(&clp->cl_lru);
952 clp->cl_time = get_seconds();
953 clear_bit(0, &clp->cl_cb_slot_busy);
954 rpc_init_wait_queue(&clp->cl_cb_waitq, "Backchannel slot table");
955 copy_verf(clp, verf);
956 rpc_copy_addr((struct sockaddr *) &clp->cl_addr, sa);
957 clp->cl_flavor = rqstp->rq_flavor;
958 copy_cred(&clp->cl_cred, &rqstp->rq_cred);
959 gen_confirm(clp);
961 return clp;
964 static int check_name(struct xdr_netobj name)
966 if (name.len == 0)
967 return 0;
968 if (name.len > NFS4_OPAQUE_LIMIT) {
969 dprintk("NFSD: check_name: name too long(%d)!\n", name.len);
970 return 0;
972 return 1;
975 static void
976 add_to_unconfirmed(struct nfs4_client *clp, unsigned int strhashval)
978 unsigned int idhashval;
980 list_add(&clp->cl_strhash, &unconf_str_hashtbl[strhashval]);
981 idhashval = clientid_hashval(clp->cl_clientid.cl_id);
982 list_add(&clp->cl_idhash, &unconf_id_hashtbl[idhashval]);
983 renew_client(clp);
986 static void
987 move_to_confirmed(struct nfs4_client *clp)
989 unsigned int idhashval = clientid_hashval(clp->cl_clientid.cl_id);
990 unsigned int strhashval;
992 dprintk("NFSD: move_to_confirm nfs4_client %p\n", clp);
993 list_move(&clp->cl_idhash, &conf_id_hashtbl[idhashval]);
994 strhashval = clientstr_hashval(clp->cl_recdir);
995 list_move(&clp->cl_strhash, &conf_str_hashtbl[strhashval]);
996 renew_client(clp);
999 static struct nfs4_client *
1000 find_confirmed_client(clientid_t *clid)
1002 struct nfs4_client *clp;
1003 unsigned int idhashval = clientid_hashval(clid->cl_id);
1005 list_for_each_entry(clp, &conf_id_hashtbl[idhashval], cl_idhash) {
1006 if (same_clid(&clp->cl_clientid, clid))
1007 return clp;
1009 return NULL;
1012 static struct nfs4_client *
1013 find_unconfirmed_client(clientid_t *clid)
1015 struct nfs4_client *clp;
1016 unsigned int idhashval = clientid_hashval(clid->cl_id);
1018 list_for_each_entry(clp, &unconf_id_hashtbl[idhashval], cl_idhash) {
1019 if (same_clid(&clp->cl_clientid, clid))
1020 return clp;
1022 return NULL;
1025 static inline int
1026 match_clientid_establishment(struct nfs4_client *clp, bool use_exchange_id)
1028 bool has_exchange_flags = (clp->cl_exchange_flags != 0);
1029 return use_exchange_id == has_exchange_flags;
1032 static struct nfs4_client *
1033 find_confirmed_client_by_str(const char *dname, unsigned int hashval,
1034 bool use_exchange_id)
1036 struct nfs4_client *clp;
1038 list_for_each_entry(clp, &conf_str_hashtbl[hashval], cl_strhash) {
1039 if (same_name(clp->cl_recdir, dname) &&
1040 match_clientid_establishment(clp, use_exchange_id))
1041 return clp;
1043 return NULL;
1046 static struct nfs4_client *
1047 find_unconfirmed_client_by_str(const char *dname, unsigned int hashval,
1048 bool use_exchange_id)
1050 struct nfs4_client *clp;
1052 list_for_each_entry(clp, &unconf_str_hashtbl[hashval], cl_strhash) {
1053 if (same_name(clp->cl_recdir, dname) &&
1054 match_clientid_establishment(clp, use_exchange_id))
1055 return clp;
1057 return NULL;
1060 static void
1061 gen_callback(struct nfs4_client *clp, struct nfsd4_setclientid *se, u32 scopeid)
1063 struct nfs4_cb_conn *cb = &clp->cl_cb_conn;
1064 unsigned short expected_family;
1066 /* Currently, we only support tcp and tcp6 for the callback channel */
1067 if (se->se_callback_netid_len == 3 &&
1068 !memcmp(se->se_callback_netid_val, "tcp", 3))
1069 expected_family = AF_INET;
1070 else if (se->se_callback_netid_len == 4 &&
1071 !memcmp(se->se_callback_netid_val, "tcp6", 4))
1072 expected_family = AF_INET6;
1073 else
1074 goto out_err;
1076 cb->cb_addrlen = rpc_uaddr2sockaddr(se->se_callback_addr_val,
1077 se->se_callback_addr_len,
1078 (struct sockaddr *) &cb->cb_addr,
1079 sizeof(cb->cb_addr));
1081 if (!cb->cb_addrlen || cb->cb_addr.ss_family != expected_family)
1082 goto out_err;
1084 if (cb->cb_addr.ss_family == AF_INET6)
1085 ((struct sockaddr_in6 *) &cb->cb_addr)->sin6_scope_id = scopeid;
1087 cb->cb_minorversion = 0;
1088 cb->cb_prog = se->se_callback_prog;
1089 cb->cb_ident = se->se_callback_ident;
1090 return;
1091 out_err:
1092 cb->cb_addr.ss_family = AF_UNSPEC;
1093 cb->cb_addrlen = 0;
1094 dprintk(KERN_INFO "NFSD: this client (clientid %08x/%08x) "
1095 "will not receive delegations\n",
1096 clp->cl_clientid.cl_boot, clp->cl_clientid.cl_id);
1098 return;
1102 * Cache a reply. nfsd4_check_drc_limit() has bounded the cache size.
1104 void
1105 nfsd4_store_cache_entry(struct nfsd4_compoundres *resp)
1107 struct nfsd4_slot *slot = resp->cstate.slot;
1108 unsigned int base;
1110 dprintk("--> %s slot %p\n", __func__, slot);
1112 slot->sl_opcnt = resp->opcnt;
1113 slot->sl_status = resp->cstate.status;
1115 if (nfsd4_not_cached(resp)) {
1116 slot->sl_datalen = 0;
1117 return;
1119 slot->sl_datalen = (char *)resp->p - (char *)resp->cstate.datap;
1120 base = (char *)resp->cstate.datap -
1121 (char *)resp->xbuf->head[0].iov_base;
1122 if (read_bytes_from_xdr_buf(resp->xbuf, base, slot->sl_data,
1123 slot->sl_datalen))
1124 WARN("%s: sessions DRC could not cache compound\n", __func__);
1125 return;
1129 * Encode the replay sequence operation from the slot values.
1130 * If cachethis is FALSE encode the uncached rep error on the next
1131 * operation which sets resp->p and increments resp->opcnt for
1132 * nfs4svc_encode_compoundres.
1135 static __be32
1136 nfsd4_enc_sequence_replay(struct nfsd4_compoundargs *args,
1137 struct nfsd4_compoundres *resp)
1139 struct nfsd4_op *op;
1140 struct nfsd4_slot *slot = resp->cstate.slot;
1142 dprintk("--> %s resp->opcnt %d cachethis %u \n", __func__,
1143 resp->opcnt, resp->cstate.slot->sl_cachethis);
1145 /* Encode the replayed sequence operation */
1146 op = &args->ops[resp->opcnt - 1];
1147 nfsd4_encode_operation(resp, op);
1149 /* Return nfserr_retry_uncached_rep in next operation. */
1150 if (args->opcnt > 1 && slot->sl_cachethis == 0) {
1151 op = &args->ops[resp->opcnt++];
1152 op->status = nfserr_retry_uncached_rep;
1153 nfsd4_encode_operation(resp, op);
1155 return op->status;
1159 * The sequence operation is not cached because we can use the slot and
1160 * session values.
1162 __be32
1163 nfsd4_replay_cache_entry(struct nfsd4_compoundres *resp,
1164 struct nfsd4_sequence *seq)
1166 struct nfsd4_slot *slot = resp->cstate.slot;
1167 __be32 status;
1169 dprintk("--> %s slot %p\n", __func__, slot);
1171 /* Either returns 0 or nfserr_retry_uncached */
1172 status = nfsd4_enc_sequence_replay(resp->rqstp->rq_argp, resp);
1173 if (status == nfserr_retry_uncached_rep)
1174 return status;
1176 /* The sequence operation has been encoded, cstate->datap set. */
1177 memcpy(resp->cstate.datap, slot->sl_data, slot->sl_datalen);
1179 resp->opcnt = slot->sl_opcnt;
1180 resp->p = resp->cstate.datap + XDR_QUADLEN(slot->sl_datalen);
1181 status = slot->sl_status;
1183 return status;
1187 * Set the exchange_id flags returned by the server.
1189 static void
1190 nfsd4_set_ex_flags(struct nfs4_client *new, struct nfsd4_exchange_id *clid)
1192 /* pNFS is not supported */
1193 new->cl_exchange_flags |= EXCHGID4_FLAG_USE_NON_PNFS;
1195 /* Referrals are supported, Migration is not. */
1196 new->cl_exchange_flags |= EXCHGID4_FLAG_SUPP_MOVED_REFER;
1198 /* set the wire flags to return to client. */
1199 clid->flags = new->cl_exchange_flags;
1202 __be32
1203 nfsd4_exchange_id(struct svc_rqst *rqstp,
1204 struct nfsd4_compound_state *cstate,
1205 struct nfsd4_exchange_id *exid)
1207 struct nfs4_client *unconf, *conf, *new;
1208 int status;
1209 unsigned int strhashval;
1210 char dname[HEXDIR_LEN];
1211 char addr_str[INET6_ADDRSTRLEN];
1212 nfs4_verifier verf = exid->verifier;
1213 struct sockaddr *sa = svc_addr(rqstp);
1215 rpc_ntop(sa, addr_str, sizeof(addr_str));
1216 dprintk("%s rqstp=%p exid=%p clname.len=%u clname.data=%p "
1217 "ip_addr=%s flags %x, spa_how %d\n",
1218 __func__, rqstp, exid, exid->clname.len, exid->clname.data,
1219 addr_str, exid->flags, exid->spa_how);
1221 if (!check_name(exid->clname) || (exid->flags & ~EXCHGID4_FLAG_MASK_A))
1222 return nfserr_inval;
1224 /* Currently only support SP4_NONE */
1225 switch (exid->spa_how) {
1226 case SP4_NONE:
1227 break;
1228 case SP4_SSV:
1229 return nfserr_encr_alg_unsupp;
1230 default:
1231 BUG(); /* checked by xdr code */
1232 case SP4_MACH_CRED:
1233 return nfserr_serverfault; /* no excuse :-/ */
1236 status = nfs4_make_rec_clidname(dname, &exid->clname);
1238 if (status)
1239 goto error;
1241 strhashval = clientstr_hashval(dname);
1243 nfs4_lock_state();
1244 status = nfs_ok;
1246 conf = find_confirmed_client_by_str(dname, strhashval, true);
1247 if (conf) {
1248 if (!same_verf(&verf, &conf->cl_verifier)) {
1249 /* 18.35.4 case 8 */
1250 if (exid->flags & EXCHGID4_FLAG_UPD_CONFIRMED_REC_A) {
1251 status = nfserr_not_same;
1252 goto out;
1254 /* Client reboot: destroy old state */
1255 expire_client(conf);
1256 goto out_new;
1258 if (!same_creds(&conf->cl_cred, &rqstp->rq_cred)) {
1259 /* 18.35.4 case 9 */
1260 if (exid->flags & EXCHGID4_FLAG_UPD_CONFIRMED_REC_A) {
1261 status = nfserr_perm;
1262 goto out;
1264 expire_client(conf);
1265 goto out_new;
1268 * Set bit when the owner id and verifier map to an already
1269 * confirmed client id (18.35.3).
1271 exid->flags |= EXCHGID4_FLAG_CONFIRMED_R;
1274 * Falling into 18.35.4 case 2, possible router replay.
1275 * Leave confirmed record intact and return same result.
1277 copy_verf(conf, &verf);
1278 new = conf;
1279 goto out_copy;
1282 /* 18.35.4 case 7 */
1283 if (exid->flags & EXCHGID4_FLAG_UPD_CONFIRMED_REC_A) {
1284 status = nfserr_noent;
1285 goto out;
1288 unconf = find_unconfirmed_client_by_str(dname, strhashval, true);
1289 if (unconf) {
1291 * Possible retry or client restart. Per 18.35.4 case 4,
1292 * a new unconfirmed record should be generated regardless
1293 * of whether any properties have changed.
1295 expire_client(unconf);
1298 out_new:
1299 /* Normal case */
1300 new = create_client(exid->clname, dname, rqstp, &verf);
1301 if (new == NULL) {
1302 status = nfserr_jukebox;
1303 goto out;
1306 gen_clid(new);
1307 add_to_unconfirmed(new, strhashval);
1308 out_copy:
1309 exid->clientid.cl_boot = new->cl_clientid.cl_boot;
1310 exid->clientid.cl_id = new->cl_clientid.cl_id;
1312 exid->seqid = 1;
1313 nfsd4_set_ex_flags(new, exid);
1315 dprintk("nfsd4_exchange_id seqid %d flags %x\n",
1316 new->cl_cs_slot.sl_seqid, new->cl_exchange_flags);
1317 status = nfs_ok;
1319 out:
1320 nfs4_unlock_state();
1321 error:
1322 dprintk("nfsd4_exchange_id returns %d\n", ntohl(status));
1323 return status;
1326 static int
1327 check_slot_seqid(u32 seqid, u32 slot_seqid, int slot_inuse)
1329 dprintk("%s enter. seqid %d slot_seqid %d\n", __func__, seqid,
1330 slot_seqid);
1332 /* The slot is in use, and no response has been sent. */
1333 if (slot_inuse) {
1334 if (seqid == slot_seqid)
1335 return nfserr_jukebox;
1336 else
1337 return nfserr_seq_misordered;
1339 /* Normal */
1340 if (likely(seqid == slot_seqid + 1))
1341 return nfs_ok;
1342 /* Replay */
1343 if (seqid == slot_seqid)
1344 return nfserr_replay_cache;
1345 /* Wraparound */
1346 if (seqid == 1 && (slot_seqid + 1) == 0)
1347 return nfs_ok;
1348 /* Misordered replay or misordered new request */
1349 return nfserr_seq_misordered;
1353 * Cache the create session result into the create session single DRC
1354 * slot cache by saving the xdr structure. sl_seqid has been set.
1355 * Do this for solo or embedded create session operations.
1357 static void
1358 nfsd4_cache_create_session(struct nfsd4_create_session *cr_ses,
1359 struct nfsd4_clid_slot *slot, int nfserr)
1361 slot->sl_status = nfserr;
1362 memcpy(&slot->sl_cr_ses, cr_ses, sizeof(*cr_ses));
1365 static __be32
1366 nfsd4_replay_create_session(struct nfsd4_create_session *cr_ses,
1367 struct nfsd4_clid_slot *slot)
1369 memcpy(cr_ses, &slot->sl_cr_ses, sizeof(*cr_ses));
1370 return slot->sl_status;
1373 __be32
1374 nfsd4_create_session(struct svc_rqst *rqstp,
1375 struct nfsd4_compound_state *cstate,
1376 struct nfsd4_create_session *cr_ses)
1378 struct sockaddr *sa = svc_addr(rqstp);
1379 struct nfs4_client *conf, *unconf;
1380 struct nfsd4_clid_slot *cs_slot = NULL;
1381 int status = 0;
1383 nfs4_lock_state();
1384 unconf = find_unconfirmed_client(&cr_ses->clientid);
1385 conf = find_confirmed_client(&cr_ses->clientid);
1387 if (conf) {
1388 cs_slot = &conf->cl_cs_slot;
1389 status = check_slot_seqid(cr_ses->seqid, cs_slot->sl_seqid, 0);
1390 if (status == nfserr_replay_cache) {
1391 dprintk("Got a create_session replay! seqid= %d\n",
1392 cs_slot->sl_seqid);
1393 /* Return the cached reply status */
1394 status = nfsd4_replay_create_session(cr_ses, cs_slot);
1395 goto out;
1396 } else if (cr_ses->seqid != cs_slot->sl_seqid + 1) {
1397 status = nfserr_seq_misordered;
1398 dprintk("Sequence misordered!\n");
1399 dprintk("Expected seqid= %d but got seqid= %d\n",
1400 cs_slot->sl_seqid, cr_ses->seqid);
1401 goto out;
1403 cs_slot->sl_seqid++;
1404 } else if (unconf) {
1405 if (!same_creds(&unconf->cl_cred, &rqstp->rq_cred) ||
1406 !rpc_cmp_addr(sa, (struct sockaddr *) &unconf->cl_addr)) {
1407 status = nfserr_clid_inuse;
1408 goto out;
1411 cs_slot = &unconf->cl_cs_slot;
1412 status = check_slot_seqid(cr_ses->seqid, cs_slot->sl_seqid, 0);
1413 if (status) {
1414 /* an unconfirmed replay returns misordered */
1415 status = nfserr_seq_misordered;
1416 goto out_cache;
1419 cs_slot->sl_seqid++; /* from 0 to 1 */
1420 move_to_confirmed(unconf);
1422 if (cr_ses->flags & SESSION4_BACK_CHAN) {
1423 unconf->cl_cb_conn.cb_xprt = rqstp->rq_xprt;
1424 svc_xprt_get(rqstp->rq_xprt);
1425 rpc_copy_addr(
1426 (struct sockaddr *)&unconf->cl_cb_conn.cb_addr,
1427 sa);
1428 unconf->cl_cb_conn.cb_addrlen = svc_addr_len(sa);
1429 unconf->cl_cb_conn.cb_minorversion =
1430 cstate->minorversion;
1431 unconf->cl_cb_conn.cb_prog = cr_ses->callback_prog;
1432 unconf->cl_cb_seq_nr = 1;
1433 nfsd4_probe_callback(unconf, &unconf->cl_cb_conn);
1435 conf = unconf;
1436 } else {
1437 status = nfserr_stale_clientid;
1438 goto out;
1442 * We do not support RDMA or persistent sessions
1444 cr_ses->flags &= ~SESSION4_PERSIST;
1445 cr_ses->flags &= ~SESSION4_RDMA;
1447 status = alloc_init_session(rqstp, conf, cr_ses);
1448 if (status)
1449 goto out;
1451 memcpy(cr_ses->sessionid.data, conf->cl_sessionid.data,
1452 NFS4_MAX_SESSIONID_LEN);
1453 cr_ses->seqid = cs_slot->sl_seqid;
1455 out_cache:
1456 /* cache solo and embedded create sessions under the state lock */
1457 nfsd4_cache_create_session(cr_ses, cs_slot, status);
1458 out:
1459 nfs4_unlock_state();
1460 dprintk("%s returns %d\n", __func__, ntohl(status));
1461 return status;
1464 static bool nfsd4_last_compound_op(struct svc_rqst *rqstp)
1466 struct nfsd4_compoundres *resp = rqstp->rq_resp;
1467 struct nfsd4_compoundargs *argp = rqstp->rq_argp;
1469 return argp->opcnt == resp->opcnt;
1472 static bool nfsd4_compound_in_session(struct nfsd4_session *session, struct nfs4_sessionid *sid)
1474 if (!session)
1475 return 0;
1476 return !memcmp(sid, &session->se_sessionid, sizeof(*sid));
1479 __be32
1480 nfsd4_destroy_session(struct svc_rqst *r,
1481 struct nfsd4_compound_state *cstate,
1482 struct nfsd4_destroy_session *sessionid)
1484 struct nfsd4_session *ses;
1485 u32 status = nfserr_badsession;
1487 /* Notes:
1488 * - The confirmed nfs4_client->cl_sessionid holds destroyed sessinid
1489 * - Should we return nfserr_back_chan_busy if waiting for
1490 * callbacks on to-be-destroyed session?
1491 * - Do we need to clear any callback info from previous session?
1494 if (nfsd4_compound_in_session(cstate->session, &sessionid->sessionid)) {
1495 if (!nfsd4_last_compound_op(r))
1496 return nfserr_not_only_op;
1498 dump_sessionid(__func__, &sessionid->sessionid);
1499 spin_lock(&client_lock);
1500 ses = find_in_sessionid_hashtbl(&sessionid->sessionid);
1501 if (!ses) {
1502 spin_unlock(&client_lock);
1503 goto out;
1506 unhash_session(ses);
1507 spin_unlock(&client_lock);
1509 nfs4_lock_state();
1510 /* wait for callbacks */
1511 nfsd4_set_callback_client(ses->se_client, NULL);
1512 nfs4_unlock_state();
1513 nfsd4_put_session(ses);
1514 status = nfs_ok;
1515 out:
1516 dprintk("%s returns %d\n", __func__, ntohl(status));
1517 return status;
1520 __be32
1521 nfsd4_sequence(struct svc_rqst *rqstp,
1522 struct nfsd4_compound_state *cstate,
1523 struct nfsd4_sequence *seq)
1525 struct nfsd4_compoundres *resp = rqstp->rq_resp;
1526 struct nfsd4_session *session;
1527 struct nfsd4_slot *slot;
1528 int status;
1530 if (resp->opcnt != 1)
1531 return nfserr_sequence_pos;
1533 spin_lock(&client_lock);
1534 status = nfserr_badsession;
1535 session = find_in_sessionid_hashtbl(&seq->sessionid);
1536 if (!session)
1537 goto out;
1539 status = nfserr_badslot;
1540 if (seq->slotid >= session->se_fchannel.maxreqs)
1541 goto out;
1543 slot = session->se_slots[seq->slotid];
1544 dprintk("%s: slotid %d\n", __func__, seq->slotid);
1546 /* We do not negotiate the number of slots yet, so set the
1547 * maxslots to the session maxreqs which is used to encode
1548 * sr_highest_slotid and the sr_target_slot id to maxslots */
1549 seq->maxslots = session->se_fchannel.maxreqs;
1551 status = check_slot_seqid(seq->seqid, slot->sl_seqid, slot->sl_inuse);
1552 if (status == nfserr_replay_cache) {
1553 cstate->slot = slot;
1554 cstate->session = session;
1555 /* Return the cached reply status and set cstate->status
1556 * for nfsd4_proc_compound processing */
1557 status = nfsd4_replay_cache_entry(resp, seq);
1558 cstate->status = nfserr_replay_cache;
1559 goto out;
1561 if (status)
1562 goto out;
1564 /* Success! bump slot seqid */
1565 slot->sl_inuse = true;
1566 slot->sl_seqid = seq->seqid;
1567 slot->sl_cachethis = seq->cachethis;
1569 cstate->slot = slot;
1570 cstate->session = session;
1572 out:
1573 /* Hold a session reference until done processing the compound. */
1574 if (cstate->session) {
1575 nfsd4_get_session(cstate->session);
1576 atomic_inc(&session->se_client->cl_refcount);
1578 spin_unlock(&client_lock);
1579 dprintk("%s: return %d\n", __func__, ntohl(status));
1580 return status;
1583 __be32
1584 nfsd4_reclaim_complete(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate, struct nfsd4_reclaim_complete *rc)
1586 if (rc->rca_one_fs) {
1587 if (!cstate->current_fh.fh_dentry)
1588 return nfserr_nofilehandle;
1590 * We don't take advantage of the rca_one_fs case.
1591 * That's OK, it's optional, we can safely ignore it.
1593 return nfs_ok;
1595 nfs4_lock_state();
1596 if (is_client_expired(cstate->session->se_client)) {
1597 nfs4_unlock_state();
1599 * The following error isn't really legal.
1600 * But we only get here if the client just explicitly
1601 * destroyed the client. Surely it no longer cares what
1602 * error it gets back on an operation for the dead
1603 * client.
1605 return nfserr_stale_clientid;
1607 nfsd4_create_clid_dir(cstate->session->se_client);
1608 nfs4_unlock_state();
1609 return nfs_ok;
1612 __be32
1613 nfsd4_setclientid(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
1614 struct nfsd4_setclientid *setclid)
1616 struct sockaddr *sa = svc_addr(rqstp);
1617 struct xdr_netobj clname = {
1618 .len = setclid->se_namelen,
1619 .data = setclid->se_name,
1621 nfs4_verifier clverifier = setclid->se_verf;
1622 unsigned int strhashval;
1623 struct nfs4_client *conf, *unconf, *new;
1624 __be32 status;
1625 char dname[HEXDIR_LEN];
1627 if (!check_name(clname))
1628 return nfserr_inval;
1630 status = nfs4_make_rec_clidname(dname, &clname);
1631 if (status)
1632 return status;
1635 strhashval = clientstr_hashval(dname);
1637 nfs4_lock_state();
1638 conf = find_confirmed_client_by_str(dname, strhashval, false);
1639 if (conf) {
1640 /* RFC 3530 14.2.33 CASE 0: */
1641 status = nfserr_clid_inuse;
1642 if (!same_creds(&conf->cl_cred, &rqstp->rq_cred)) {
1643 char addr_str[INET6_ADDRSTRLEN];
1644 rpc_ntop((struct sockaddr *) &conf->cl_addr, addr_str,
1645 sizeof(addr_str));
1646 dprintk("NFSD: setclientid: string in use by client "
1647 "at %s\n", addr_str);
1648 goto out;
1652 * section 14.2.33 of RFC 3530 (under the heading "IMPLEMENTATION")
1653 * has a description of SETCLIENTID request processing consisting
1654 * of 5 bullet points, labeled as CASE0 - CASE4 below.
1656 unconf = find_unconfirmed_client_by_str(dname, strhashval, false);
1657 status = nfserr_resource;
1658 if (!conf) {
1660 * RFC 3530 14.2.33 CASE 4:
1661 * placed first, because it is the normal case
1663 if (unconf)
1664 expire_client(unconf);
1665 new = create_client(clname, dname, rqstp, &clverifier);
1666 if (new == NULL)
1667 goto out;
1668 gen_clid(new);
1669 } else if (same_verf(&conf->cl_verifier, &clverifier)) {
1671 * RFC 3530 14.2.33 CASE 1:
1672 * probable callback update
1674 if (unconf) {
1675 /* Note this is removing unconfirmed {*x***},
1676 * which is stronger than RFC recommended {vxc**}.
1677 * This has the advantage that there is at most
1678 * one {*x***} in either list at any time.
1680 expire_client(unconf);
1682 new = create_client(clname, dname, rqstp, &clverifier);
1683 if (new == NULL)
1684 goto out;
1685 copy_clid(new, conf);
1686 } else if (!unconf) {
1688 * RFC 3530 14.2.33 CASE 2:
1689 * probable client reboot; state will be removed if
1690 * confirmed.
1692 new = create_client(clname, dname, rqstp, &clverifier);
1693 if (new == NULL)
1694 goto out;
1695 gen_clid(new);
1696 } else {
1698 * RFC 3530 14.2.33 CASE 3:
1699 * probable client reboot; state will be removed if
1700 * confirmed.
1702 expire_client(unconf);
1703 new = create_client(clname, dname, rqstp, &clverifier);
1704 if (new == NULL)
1705 goto out;
1706 gen_clid(new);
1708 gen_callback(new, setclid, rpc_get_scope_id(sa));
1709 add_to_unconfirmed(new, strhashval);
1710 setclid->se_clientid.cl_boot = new->cl_clientid.cl_boot;
1711 setclid->se_clientid.cl_id = new->cl_clientid.cl_id;
1712 memcpy(setclid->se_confirm.data, new->cl_confirm.data, sizeof(setclid->se_confirm.data));
1713 status = nfs_ok;
1714 out:
1715 nfs4_unlock_state();
1716 return status;
1721 * Section 14.2.34 of RFC 3530 (under the heading "IMPLEMENTATION") has
1722 * a description of SETCLIENTID_CONFIRM request processing consisting of 4
1723 * bullets, labeled as CASE1 - CASE4 below.
1725 __be32
1726 nfsd4_setclientid_confirm(struct svc_rqst *rqstp,
1727 struct nfsd4_compound_state *cstate,
1728 struct nfsd4_setclientid_confirm *setclientid_confirm)
1730 struct sockaddr *sa = svc_addr(rqstp);
1731 struct nfs4_client *conf, *unconf;
1732 nfs4_verifier confirm = setclientid_confirm->sc_confirm;
1733 clientid_t * clid = &setclientid_confirm->sc_clientid;
1734 __be32 status;
1736 if (STALE_CLIENTID(clid))
1737 return nfserr_stale_clientid;
1739 nfs4_lock_state();
1741 conf = find_confirmed_client(clid);
1742 unconf = find_unconfirmed_client(clid);
1744 status = nfserr_clid_inuse;
1745 if (conf && !rpc_cmp_addr((struct sockaddr *) &conf->cl_addr, sa))
1746 goto out;
1747 if (unconf && !rpc_cmp_addr((struct sockaddr *) &unconf->cl_addr, sa))
1748 goto out;
1751 * section 14.2.34 of RFC 3530 has a description of
1752 * SETCLIENTID_CONFIRM request processing consisting
1753 * of 4 bullet points, labeled as CASE1 - CASE4 below.
1755 if (conf && unconf && same_verf(&confirm, &unconf->cl_confirm)) {
1757 * RFC 3530 14.2.34 CASE 1:
1758 * callback update
1760 if (!same_creds(&conf->cl_cred, &unconf->cl_cred))
1761 status = nfserr_clid_inuse;
1762 else {
1763 atomic_set(&conf->cl_cb_set, 0);
1764 nfsd4_probe_callback(conf, &unconf->cl_cb_conn);
1765 expire_client(unconf);
1766 status = nfs_ok;
1769 } else if (conf && !unconf) {
1771 * RFC 3530 14.2.34 CASE 2:
1772 * probable retransmitted request; play it safe and
1773 * do nothing.
1775 if (!same_creds(&conf->cl_cred, &rqstp->rq_cred))
1776 status = nfserr_clid_inuse;
1777 else
1778 status = nfs_ok;
1779 } else if (!conf && unconf
1780 && same_verf(&unconf->cl_confirm, &confirm)) {
1782 * RFC 3530 14.2.34 CASE 3:
1783 * Normal case; new or rebooted client:
1785 if (!same_creds(&unconf->cl_cred, &rqstp->rq_cred)) {
1786 status = nfserr_clid_inuse;
1787 } else {
1788 unsigned int hash =
1789 clientstr_hashval(unconf->cl_recdir);
1790 conf = find_confirmed_client_by_str(unconf->cl_recdir,
1791 hash, false);
1792 if (conf) {
1793 nfsd4_remove_clid_dir(conf);
1794 expire_client(conf);
1796 move_to_confirmed(unconf);
1797 conf = unconf;
1798 nfsd4_probe_callback(conf, &conf->cl_cb_conn);
1799 status = nfs_ok;
1801 } else if ((!conf || (conf && !same_verf(&conf->cl_confirm, &confirm)))
1802 && (!unconf || (unconf && !same_verf(&unconf->cl_confirm,
1803 &confirm)))) {
1805 * RFC 3530 14.2.34 CASE 4:
1806 * Client probably hasn't noticed that we rebooted yet.
1808 status = nfserr_stale_clientid;
1809 } else {
1810 /* check that we have hit one of the cases...*/
1811 status = nfserr_clid_inuse;
1813 out:
1814 nfs4_unlock_state();
1815 return status;
1818 /* OPEN Share state helper functions */
1819 static inline struct nfs4_file *
1820 alloc_init_file(struct inode *ino)
1822 struct nfs4_file *fp;
1823 unsigned int hashval = file_hashval(ino);
1825 fp = kmem_cache_alloc(file_slab, GFP_KERNEL);
1826 if (fp) {
1827 atomic_set(&fp->fi_ref, 1);
1828 INIT_LIST_HEAD(&fp->fi_hash);
1829 INIT_LIST_HEAD(&fp->fi_stateids);
1830 INIT_LIST_HEAD(&fp->fi_delegations);
1831 fp->fi_inode = igrab(ino);
1832 fp->fi_id = current_fileid++;
1833 fp->fi_had_conflict = false;
1834 memset(fp->fi_fds, 0, sizeof(fp->fi_fds));
1835 memset(fp->fi_access, 0, sizeof(fp->fi_access));
1836 spin_lock(&recall_lock);
1837 list_add(&fp->fi_hash, &file_hashtbl[hashval]);
1838 spin_unlock(&recall_lock);
1839 return fp;
1841 return NULL;
1844 static void
1845 nfsd4_free_slab(struct kmem_cache **slab)
1847 if (*slab == NULL)
1848 return;
1849 kmem_cache_destroy(*slab);
1850 *slab = NULL;
1853 void
1854 nfsd4_free_slabs(void)
1856 nfsd4_free_slab(&stateowner_slab);
1857 nfsd4_free_slab(&file_slab);
1858 nfsd4_free_slab(&stateid_slab);
1859 nfsd4_free_slab(&deleg_slab);
1862 static int
1863 nfsd4_init_slabs(void)
1865 stateowner_slab = kmem_cache_create("nfsd4_stateowners",
1866 sizeof(struct nfs4_stateowner), 0, 0, NULL);
1867 if (stateowner_slab == NULL)
1868 goto out_nomem;
1869 file_slab = kmem_cache_create("nfsd4_files",
1870 sizeof(struct nfs4_file), 0, 0, NULL);
1871 if (file_slab == NULL)
1872 goto out_nomem;
1873 stateid_slab = kmem_cache_create("nfsd4_stateids",
1874 sizeof(struct nfs4_stateid), 0, 0, NULL);
1875 if (stateid_slab == NULL)
1876 goto out_nomem;
1877 deleg_slab = kmem_cache_create("nfsd4_delegations",
1878 sizeof(struct nfs4_delegation), 0, 0, NULL);
1879 if (deleg_slab == NULL)
1880 goto out_nomem;
1881 return 0;
1882 out_nomem:
1883 nfsd4_free_slabs();
1884 dprintk("nfsd4: out of memory while initializing nfsv4\n");
1885 return -ENOMEM;
1888 void
1889 nfs4_free_stateowner(struct kref *kref)
1891 struct nfs4_stateowner *sop =
1892 container_of(kref, struct nfs4_stateowner, so_ref);
1893 kfree(sop->so_owner.data);
1894 kmem_cache_free(stateowner_slab, sop);
1897 static inline struct nfs4_stateowner *
1898 alloc_stateowner(struct xdr_netobj *owner)
1900 struct nfs4_stateowner *sop;
1902 if ((sop = kmem_cache_alloc(stateowner_slab, GFP_KERNEL))) {
1903 if ((sop->so_owner.data = kmalloc(owner->len, GFP_KERNEL))) {
1904 memcpy(sop->so_owner.data, owner->data, owner->len);
1905 sop->so_owner.len = owner->len;
1906 kref_init(&sop->so_ref);
1907 return sop;
1909 kmem_cache_free(stateowner_slab, sop);
1911 return NULL;
1914 static struct nfs4_stateowner *
1915 alloc_init_open_stateowner(unsigned int strhashval, struct nfs4_client *clp, struct nfsd4_open *open) {
1916 struct nfs4_stateowner *sop;
1917 struct nfs4_replay *rp;
1918 unsigned int idhashval;
1920 if (!(sop = alloc_stateowner(&open->op_owner)))
1921 return NULL;
1922 idhashval = ownerid_hashval(current_ownerid);
1923 INIT_LIST_HEAD(&sop->so_idhash);
1924 INIT_LIST_HEAD(&sop->so_strhash);
1925 INIT_LIST_HEAD(&sop->so_perclient);
1926 INIT_LIST_HEAD(&sop->so_stateids);
1927 INIT_LIST_HEAD(&sop->so_perstateid); /* not used */
1928 INIT_LIST_HEAD(&sop->so_close_lru);
1929 sop->so_time = 0;
1930 list_add(&sop->so_idhash, &ownerid_hashtbl[idhashval]);
1931 list_add(&sop->so_strhash, &ownerstr_hashtbl[strhashval]);
1932 list_add(&sop->so_perclient, &clp->cl_openowners);
1933 sop->so_is_open_owner = 1;
1934 sop->so_id = current_ownerid++;
1935 sop->so_client = clp;
1936 sop->so_seqid = open->op_seqid;
1937 sop->so_confirmed = 0;
1938 rp = &sop->so_replay;
1939 rp->rp_status = nfserr_serverfault;
1940 rp->rp_buflen = 0;
1941 rp->rp_buf = rp->rp_ibuf;
1942 return sop;
1945 static inline void
1946 init_stateid(struct nfs4_stateid *stp, struct nfs4_file *fp, struct nfsd4_open *open) {
1947 struct nfs4_stateowner *sop = open->op_stateowner;
1948 unsigned int hashval = stateid_hashval(sop->so_id, fp->fi_id);
1950 INIT_LIST_HEAD(&stp->st_hash);
1951 INIT_LIST_HEAD(&stp->st_perstateowner);
1952 INIT_LIST_HEAD(&stp->st_lockowners);
1953 INIT_LIST_HEAD(&stp->st_perfile);
1954 list_add(&stp->st_hash, &stateid_hashtbl[hashval]);
1955 list_add(&stp->st_perstateowner, &sop->so_stateids);
1956 list_add(&stp->st_perfile, &fp->fi_stateids);
1957 stp->st_stateowner = sop;
1958 get_nfs4_file(fp);
1959 stp->st_file = fp;
1960 stp->st_stateid.si_boot = boot_time;
1961 stp->st_stateid.si_stateownerid = sop->so_id;
1962 stp->st_stateid.si_fileid = fp->fi_id;
1963 stp->st_stateid.si_generation = 0;
1964 stp->st_access_bmap = 0;
1965 stp->st_deny_bmap = 0;
1966 __set_bit(open->op_share_access & ~NFS4_SHARE_WANT_MASK,
1967 &stp->st_access_bmap);
1968 __set_bit(open->op_share_deny, &stp->st_deny_bmap);
1969 stp->st_openstp = NULL;
1972 static void
1973 move_to_close_lru(struct nfs4_stateowner *sop)
1975 dprintk("NFSD: move_to_close_lru nfs4_stateowner %p\n", sop);
1977 list_move_tail(&sop->so_close_lru, &close_lru);
1978 sop->so_time = get_seconds();
1981 static int
1982 same_owner_str(struct nfs4_stateowner *sop, struct xdr_netobj *owner,
1983 clientid_t *clid)
1985 return (sop->so_owner.len == owner->len) &&
1986 0 == memcmp(sop->so_owner.data, owner->data, owner->len) &&
1987 (sop->so_client->cl_clientid.cl_id == clid->cl_id);
1990 static struct nfs4_stateowner *
1991 find_openstateowner_str(unsigned int hashval, struct nfsd4_open *open)
1993 struct nfs4_stateowner *so = NULL;
1995 list_for_each_entry(so, &ownerstr_hashtbl[hashval], so_strhash) {
1996 if (same_owner_str(so, &open->op_owner, &open->op_clientid))
1997 return so;
1999 return NULL;
2002 /* search file_hashtbl[] for file */
2003 static struct nfs4_file *
2004 find_file(struct inode *ino)
2006 unsigned int hashval = file_hashval(ino);
2007 struct nfs4_file *fp;
2009 spin_lock(&recall_lock);
2010 list_for_each_entry(fp, &file_hashtbl[hashval], fi_hash) {
2011 if (fp->fi_inode == ino) {
2012 get_nfs4_file(fp);
2013 spin_unlock(&recall_lock);
2014 return fp;
2017 spin_unlock(&recall_lock);
2018 return NULL;
2021 static inline int access_valid(u32 x, u32 minorversion)
2023 if ((x & NFS4_SHARE_ACCESS_MASK) < NFS4_SHARE_ACCESS_READ)
2024 return 0;
2025 if ((x & NFS4_SHARE_ACCESS_MASK) > NFS4_SHARE_ACCESS_BOTH)
2026 return 0;
2027 x &= ~NFS4_SHARE_ACCESS_MASK;
2028 if (minorversion && x) {
2029 if ((x & NFS4_SHARE_WANT_MASK) > NFS4_SHARE_WANT_CANCEL)
2030 return 0;
2031 if ((x & NFS4_SHARE_WHEN_MASK) > NFS4_SHARE_PUSH_DELEG_WHEN_UNCONTENDED)
2032 return 0;
2033 x &= ~(NFS4_SHARE_WANT_MASK | NFS4_SHARE_WHEN_MASK);
2035 if (x)
2036 return 0;
2037 return 1;
2040 static inline int deny_valid(u32 x)
2042 /* Note: unlike access bits, deny bits may be zero. */
2043 return x <= NFS4_SHARE_DENY_BOTH;
2047 * Called to check deny when READ with all zero stateid or
2048 * WRITE with all zero or all one stateid
2050 static __be32
2051 nfs4_share_conflict(struct svc_fh *current_fh, unsigned int deny_type)
2053 struct inode *ino = current_fh->fh_dentry->d_inode;
2054 struct nfs4_file *fp;
2055 struct nfs4_stateid *stp;
2056 __be32 ret;
2058 dprintk("NFSD: nfs4_share_conflict\n");
2060 fp = find_file(ino);
2061 if (!fp)
2062 return nfs_ok;
2063 ret = nfserr_locked;
2064 /* Search for conflicting share reservations */
2065 list_for_each_entry(stp, &fp->fi_stateids, st_perfile) {
2066 if (test_bit(deny_type, &stp->st_deny_bmap) ||
2067 test_bit(NFS4_SHARE_DENY_BOTH, &stp->st_deny_bmap))
2068 goto out;
2070 ret = nfs_ok;
2071 out:
2072 put_nfs4_file(fp);
2073 return ret;
2076 static inline void
2077 nfs4_file_downgrade(struct nfs4_file *fp, unsigned int share_access)
2079 if (share_access & NFS4_SHARE_ACCESS_WRITE)
2080 nfs4_file_put_access(fp, O_WRONLY);
2081 if (share_access & NFS4_SHARE_ACCESS_READ)
2082 nfs4_file_put_access(fp, O_RDONLY);
2086 * Spawn a thread to perform a recall on the delegation represented
2087 * by the lease (file_lock)
2089 * Called from break_lease() with lock_kernel() held.
2090 * Note: we assume break_lease will only call this *once* for any given
2091 * lease.
2093 static
2094 void nfsd_break_deleg_cb(struct file_lock *fl)
2096 struct nfs4_delegation *dp = (struct nfs4_delegation *)fl->fl_owner;
2098 dprintk("NFSD nfsd_break_deleg_cb: dp %p fl %p\n",dp,fl);
2099 if (!dp)
2100 return;
2102 /* We're assuming the state code never drops its reference
2103 * without first removing the lease. Since we're in this lease
2104 * callback (and since the lease code is serialized by the kernel
2105 * lock) we know the server hasn't removed the lease yet, we know
2106 * it's safe to take a reference: */
2107 atomic_inc(&dp->dl_count);
2109 spin_lock(&recall_lock);
2110 list_add_tail(&dp->dl_recall_lru, &del_recall_lru);
2111 spin_unlock(&recall_lock);
2113 /* only place dl_time is set. protected by lock_kernel*/
2114 dp->dl_time = get_seconds();
2117 * We don't want the locks code to timeout the lease for us;
2118 * we'll remove it ourself if the delegation isn't returned
2119 * in time.
2121 fl->fl_break_time = 0;
2123 dp->dl_file->fi_had_conflict = true;
2124 nfsd4_cb_recall(dp);
2128 * The file_lock is being reapd.
2130 * Called by locks_free_lock() with lock_kernel() held.
2132 static
2133 void nfsd_release_deleg_cb(struct file_lock *fl)
2135 struct nfs4_delegation *dp = (struct nfs4_delegation *)fl->fl_owner;
2137 dprintk("NFSD nfsd_release_deleg_cb: fl %p dp %p dl_count %d\n", fl,dp, atomic_read(&dp->dl_count));
2139 if (!(fl->fl_flags & FL_LEASE) || !dp)
2140 return;
2141 dp->dl_flock = NULL;
2145 * Set the delegation file_lock back pointer.
2147 * Called from setlease() with lock_kernel() held.
2149 static
2150 void nfsd_copy_lock_deleg_cb(struct file_lock *new, struct file_lock *fl)
2152 struct nfs4_delegation *dp = (struct nfs4_delegation *)new->fl_owner;
2154 dprintk("NFSD: nfsd_copy_lock_deleg_cb: new fl %p dp %p\n", new, dp);
2155 if (!dp)
2156 return;
2157 dp->dl_flock = new;
2161 * Called from setlease() with lock_kernel() held
2163 static
2164 int nfsd_same_client_deleg_cb(struct file_lock *onlist, struct file_lock *try)
2166 struct nfs4_delegation *onlistd =
2167 (struct nfs4_delegation *)onlist->fl_owner;
2168 struct nfs4_delegation *tryd =
2169 (struct nfs4_delegation *)try->fl_owner;
2171 if (onlist->fl_lmops != try->fl_lmops)
2172 return 0;
2174 return onlistd->dl_client == tryd->dl_client;
2178 static
2179 int nfsd_change_deleg_cb(struct file_lock **onlist, int arg)
2181 if (arg & F_UNLCK)
2182 return lease_modify(onlist, arg);
2183 else
2184 return -EAGAIN;
2187 static const struct lock_manager_operations nfsd_lease_mng_ops = {
2188 .fl_break = nfsd_break_deleg_cb,
2189 .fl_release_private = nfsd_release_deleg_cb,
2190 .fl_copy_lock = nfsd_copy_lock_deleg_cb,
2191 .fl_mylease = nfsd_same_client_deleg_cb,
2192 .fl_change = nfsd_change_deleg_cb,
2196 __be32
2197 nfsd4_process_open1(struct nfsd4_compound_state *cstate,
2198 struct nfsd4_open *open)
2200 clientid_t *clientid = &open->op_clientid;
2201 struct nfs4_client *clp = NULL;
2202 unsigned int strhashval;
2203 struct nfs4_stateowner *sop = NULL;
2205 if (!check_name(open->op_owner))
2206 return nfserr_inval;
2208 if (STALE_CLIENTID(&open->op_clientid))
2209 return nfserr_stale_clientid;
2211 strhashval = ownerstr_hashval(clientid->cl_id, open->op_owner);
2212 sop = find_openstateowner_str(strhashval, open);
2213 open->op_stateowner = sop;
2214 if (!sop) {
2215 /* Make sure the client's lease hasn't expired. */
2216 clp = find_confirmed_client(clientid);
2217 if (clp == NULL)
2218 return nfserr_expired;
2219 goto renew;
2221 /* When sessions are used, skip open sequenceid processing */
2222 if (nfsd4_has_session(cstate))
2223 goto renew;
2224 if (!sop->so_confirmed) {
2225 /* Replace unconfirmed owners without checking for replay. */
2226 clp = sop->so_client;
2227 release_openowner(sop);
2228 open->op_stateowner = NULL;
2229 goto renew;
2231 if (open->op_seqid == sop->so_seqid - 1) {
2232 if (sop->so_replay.rp_buflen)
2233 return nfserr_replay_me;
2234 /* The original OPEN failed so spectacularly
2235 * that we don't even have replay data saved!
2236 * Therefore, we have no choice but to continue
2237 * processing this OPEN; presumably, we'll
2238 * fail again for the same reason.
2240 dprintk("nfsd4_process_open1: replay with no replay cache\n");
2241 goto renew;
2243 if (open->op_seqid != sop->so_seqid)
2244 return nfserr_bad_seqid;
2245 renew:
2246 if (open->op_stateowner == NULL) {
2247 sop = alloc_init_open_stateowner(strhashval, clp, open);
2248 if (sop == NULL)
2249 return nfserr_resource;
2250 open->op_stateowner = sop;
2252 list_del_init(&sop->so_close_lru);
2253 renew_client(sop->so_client);
2254 return nfs_ok;
2257 static inline __be32
2258 nfs4_check_delegmode(struct nfs4_delegation *dp, int flags)
2260 if ((flags & WR_STATE) && (dp->dl_type == NFS4_OPEN_DELEGATE_READ))
2261 return nfserr_openmode;
2262 else
2263 return nfs_ok;
2266 static struct nfs4_delegation *
2267 find_delegation_file(struct nfs4_file *fp, stateid_t *stid)
2269 struct nfs4_delegation *dp;
2271 list_for_each_entry(dp, &fp->fi_delegations, dl_perfile) {
2272 if (dp->dl_stateid.si_stateownerid == stid->si_stateownerid)
2273 return dp;
2275 return NULL;
2278 int share_access_to_flags(u32 share_access)
2280 share_access &= ~NFS4_SHARE_WANT_MASK;
2282 return share_access == NFS4_SHARE_ACCESS_READ ? RD_STATE : WR_STATE;
2285 static __be32
2286 nfs4_check_deleg(struct nfs4_file *fp, struct nfsd4_open *open,
2287 struct nfs4_delegation **dp)
2289 int flags;
2290 __be32 status = nfserr_bad_stateid;
2292 *dp = find_delegation_file(fp, &open->op_delegate_stateid);
2293 if (*dp == NULL)
2294 goto out;
2295 flags = share_access_to_flags(open->op_share_access);
2296 status = nfs4_check_delegmode(*dp, flags);
2297 if (status)
2298 *dp = NULL;
2299 out:
2300 if (open->op_claim_type != NFS4_OPEN_CLAIM_DELEGATE_CUR)
2301 return nfs_ok;
2302 if (status)
2303 return status;
2304 open->op_stateowner->so_confirmed = 1;
2305 return nfs_ok;
2308 static __be32
2309 nfs4_check_open(struct nfs4_file *fp, struct nfsd4_open *open, struct nfs4_stateid **stpp)
2311 struct nfs4_stateid *local;
2312 __be32 status = nfserr_share_denied;
2313 struct nfs4_stateowner *sop = open->op_stateowner;
2315 list_for_each_entry(local, &fp->fi_stateids, st_perfile) {
2316 /* ignore lock owners */
2317 if (local->st_stateowner->so_is_open_owner == 0)
2318 continue;
2319 /* remember if we have seen this open owner */
2320 if (local->st_stateowner == sop)
2321 *stpp = local;
2322 /* check for conflicting share reservations */
2323 if (!test_share(local, open))
2324 goto out;
2326 status = 0;
2327 out:
2328 return status;
2331 static inline struct nfs4_stateid *
2332 nfs4_alloc_stateid(void)
2334 return kmem_cache_alloc(stateid_slab, GFP_KERNEL);
2337 static inline int nfs4_access_to_access(u32 nfs4_access)
2339 int flags = 0;
2341 if (nfs4_access & NFS4_SHARE_ACCESS_READ)
2342 flags |= NFSD_MAY_READ;
2343 if (nfs4_access & NFS4_SHARE_ACCESS_WRITE)
2344 flags |= NFSD_MAY_WRITE;
2345 return flags;
2348 static __be32 nfs4_get_vfs_file(struct svc_rqst *rqstp, struct nfs4_file
2349 *fp, struct svc_fh *cur_fh, u32 nfs4_access)
2351 __be32 status;
2352 int oflag = nfs4_access_to_omode(nfs4_access);
2353 int access = nfs4_access_to_access(nfs4_access);
2355 if (!fp->fi_fds[oflag]) {
2356 status = nfsd_open(rqstp, cur_fh, S_IFREG, access,
2357 &fp->fi_fds[oflag]);
2358 if (status == nfserr_dropit)
2359 status = nfserr_jukebox;
2360 if (status)
2361 return status;
2363 nfs4_file_get_access(fp, oflag);
2365 return nfs_ok;
2368 static __be32
2369 nfs4_new_open(struct svc_rqst *rqstp, struct nfs4_stateid **stpp,
2370 struct nfs4_file *fp, struct svc_fh *cur_fh,
2371 struct nfsd4_open *open)
2373 struct nfs4_stateid *stp;
2374 __be32 status;
2376 stp = nfs4_alloc_stateid();
2377 if (stp == NULL)
2378 return nfserr_resource;
2380 status = nfs4_get_vfs_file(rqstp, fp, cur_fh, open->op_share_access);
2381 if (status) {
2382 kmem_cache_free(stateid_slab, stp);
2383 return status;
2385 *stpp = stp;
2386 return 0;
2389 static inline __be32
2390 nfsd4_truncate(struct svc_rqst *rqstp, struct svc_fh *fh,
2391 struct nfsd4_open *open)
2393 struct iattr iattr = {
2394 .ia_valid = ATTR_SIZE,
2395 .ia_size = 0,
2397 if (!open->op_truncate)
2398 return 0;
2399 if (!(open->op_share_access & NFS4_SHARE_ACCESS_WRITE))
2400 return nfserr_inval;
2401 return nfsd_setattr(rqstp, fh, &iattr, 0, (time_t)0);
2404 static __be32
2405 nfs4_upgrade_open(struct svc_rqst *rqstp, struct nfs4_file *fp, struct svc_fh *cur_fh, struct nfs4_stateid *stp, struct nfsd4_open *open)
2407 u32 op_share_access = open->op_share_access & ~NFS4_SHARE_WANT_MASK;
2408 bool new_access;
2409 __be32 status;
2411 new_access = !test_bit(op_share_access, &stp->st_access_bmap);
2412 if (new_access) {
2413 status = nfs4_get_vfs_file(rqstp, fp, cur_fh, op_share_access);
2414 if (status)
2415 return status;
2417 status = nfsd4_truncate(rqstp, cur_fh, open);
2418 if (status) {
2419 if (new_access) {
2420 int oflag = nfs4_access_to_omode(new_access);
2421 nfs4_file_put_access(fp, oflag);
2423 return status;
2425 /* remember the open */
2426 __set_bit(op_share_access, &stp->st_access_bmap);
2427 __set_bit(open->op_share_deny, &stp->st_deny_bmap);
2429 return nfs_ok;
2433 static void
2434 nfs4_set_claim_prev(struct nfsd4_open *open)
2436 open->op_stateowner->so_confirmed = 1;
2437 open->op_stateowner->so_client->cl_firststate = 1;
2441 * Attempt to hand out a delegation.
2443 static void
2444 nfs4_open_delegation(struct svc_fh *fh, struct nfsd4_open *open, struct nfs4_stateid *stp)
2446 struct nfs4_delegation *dp;
2447 struct nfs4_stateowner *sop = stp->st_stateowner;
2448 int cb_up = atomic_read(&sop->so_client->cl_cb_set);
2449 struct file_lock fl, *flp = &fl;
2450 int status, flag = 0;
2452 flag = NFS4_OPEN_DELEGATE_NONE;
2453 open->op_recall = 0;
2454 switch (open->op_claim_type) {
2455 case NFS4_OPEN_CLAIM_PREVIOUS:
2456 if (!cb_up)
2457 open->op_recall = 1;
2458 flag = open->op_delegate_type;
2459 if (flag == NFS4_OPEN_DELEGATE_NONE)
2460 goto out;
2461 break;
2462 case NFS4_OPEN_CLAIM_NULL:
2463 /* Let's not give out any delegations till everyone's
2464 * had the chance to reclaim theirs.... */
2465 if (locks_in_grace())
2466 goto out;
2467 if (!cb_up || !sop->so_confirmed)
2468 goto out;
2469 if (open->op_share_access & NFS4_SHARE_ACCESS_WRITE)
2470 flag = NFS4_OPEN_DELEGATE_WRITE;
2471 else
2472 flag = NFS4_OPEN_DELEGATE_READ;
2473 break;
2474 default:
2475 goto out;
2478 dp = alloc_init_deleg(sop->so_client, stp, fh, flag);
2479 if (dp == NULL) {
2480 flag = NFS4_OPEN_DELEGATE_NONE;
2481 goto out;
2483 locks_init_lock(&fl);
2484 fl.fl_lmops = &nfsd_lease_mng_ops;
2485 fl.fl_flags = FL_LEASE;
2486 fl.fl_type = flag == NFS4_OPEN_DELEGATE_READ? F_RDLCK: F_WRLCK;
2487 fl.fl_end = OFFSET_MAX;
2488 fl.fl_owner = (fl_owner_t)dp;
2489 fl.fl_file = find_readable_file(stp->st_file);
2490 BUG_ON(!fl.fl_file);
2491 fl.fl_pid = current->tgid;
2493 /* vfs_setlease checks to see if delegation should be handed out.
2494 * the lock_manager callbacks fl_mylease and fl_change are used
2496 if ((status = vfs_setlease(fl.fl_file, fl.fl_type, &flp))) {
2497 dprintk("NFSD: setlease failed [%d], no delegation\n", status);
2498 unhash_delegation(dp);
2499 flag = NFS4_OPEN_DELEGATE_NONE;
2500 goto out;
2503 memcpy(&open->op_delegate_stateid, &dp->dl_stateid, sizeof(dp->dl_stateid));
2505 dprintk("NFSD: delegation stateid=" STATEID_FMT "\n",
2506 STATEID_VAL(&dp->dl_stateid));
2507 out:
2508 if (open->op_claim_type == NFS4_OPEN_CLAIM_PREVIOUS
2509 && flag == NFS4_OPEN_DELEGATE_NONE
2510 && open->op_delegate_type != NFS4_OPEN_DELEGATE_NONE)
2511 dprintk("NFSD: WARNING: refusing delegation reclaim\n");
2512 open->op_delegate_type = flag;
2516 * called with nfs4_lock_state() held.
2518 __be32
2519 nfsd4_process_open2(struct svc_rqst *rqstp, struct svc_fh *current_fh, struct nfsd4_open *open)
2521 struct nfsd4_compoundres *resp = rqstp->rq_resp;
2522 struct nfs4_file *fp = NULL;
2523 struct inode *ino = current_fh->fh_dentry->d_inode;
2524 struct nfs4_stateid *stp = NULL;
2525 struct nfs4_delegation *dp = NULL;
2526 __be32 status;
2528 status = nfserr_inval;
2529 if (!access_valid(open->op_share_access, resp->cstate.minorversion)
2530 || !deny_valid(open->op_share_deny))
2531 goto out;
2533 * Lookup file; if found, lookup stateid and check open request,
2534 * and check for delegations in the process of being recalled.
2535 * If not found, create the nfs4_file struct
2537 fp = find_file(ino);
2538 if (fp) {
2539 if ((status = nfs4_check_open(fp, open, &stp)))
2540 goto out;
2541 status = nfs4_check_deleg(fp, open, &dp);
2542 if (status)
2543 goto out;
2544 } else {
2545 status = nfserr_bad_stateid;
2546 if (open->op_claim_type == NFS4_OPEN_CLAIM_DELEGATE_CUR)
2547 goto out;
2548 status = nfserr_resource;
2549 fp = alloc_init_file(ino);
2550 if (fp == NULL)
2551 goto out;
2555 * OPEN the file, or upgrade an existing OPEN.
2556 * If truncate fails, the OPEN fails.
2558 if (stp) {
2559 /* Stateid was found, this is an OPEN upgrade */
2560 status = nfs4_upgrade_open(rqstp, fp, current_fh, stp, open);
2561 if (status)
2562 goto out;
2563 update_stateid(&stp->st_stateid);
2564 } else {
2565 status = nfs4_new_open(rqstp, &stp, fp, current_fh, open);
2566 if (status)
2567 goto out;
2568 init_stateid(stp, fp, open);
2569 status = nfsd4_truncate(rqstp, current_fh, open);
2570 if (status) {
2571 release_open_stateid(stp);
2572 goto out;
2574 if (nfsd4_has_session(&resp->cstate))
2575 update_stateid(&stp->st_stateid);
2577 memcpy(&open->op_stateid, &stp->st_stateid, sizeof(stateid_t));
2579 if (nfsd4_has_session(&resp->cstate))
2580 open->op_stateowner->so_confirmed = 1;
2583 * Attempt to hand out a delegation. No error return, because the
2584 * OPEN succeeds even if we fail.
2586 nfs4_open_delegation(current_fh, open, stp);
2588 status = nfs_ok;
2590 dprintk("%s: stateid=" STATEID_FMT "\n", __func__,
2591 STATEID_VAL(&stp->st_stateid));
2592 out:
2593 if (fp)
2594 put_nfs4_file(fp);
2595 if (status == 0 && open->op_claim_type == NFS4_OPEN_CLAIM_PREVIOUS)
2596 nfs4_set_claim_prev(open);
2598 * To finish the open response, we just need to set the rflags.
2600 open->op_rflags = NFS4_OPEN_RESULT_LOCKTYPE_POSIX;
2601 if (!open->op_stateowner->so_confirmed &&
2602 !nfsd4_has_session(&resp->cstate))
2603 open->op_rflags |= NFS4_OPEN_RESULT_CONFIRM;
2605 return status;
2608 __be32
2609 nfsd4_renew(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
2610 clientid_t *clid)
2612 struct nfs4_client *clp;
2613 __be32 status;
2615 nfs4_lock_state();
2616 dprintk("process_renew(%08x/%08x): starting\n",
2617 clid->cl_boot, clid->cl_id);
2618 status = nfserr_stale_clientid;
2619 if (STALE_CLIENTID(clid))
2620 goto out;
2621 clp = find_confirmed_client(clid);
2622 status = nfserr_expired;
2623 if (clp == NULL) {
2624 /* We assume the client took too long to RENEW. */
2625 dprintk("nfsd4_renew: clientid not found!\n");
2626 goto out;
2628 renew_client(clp);
2629 status = nfserr_cb_path_down;
2630 if (!list_empty(&clp->cl_delegations)
2631 && !atomic_read(&clp->cl_cb_set))
2632 goto out;
2633 status = nfs_ok;
2634 out:
2635 nfs4_unlock_state();
2636 return status;
2639 struct lock_manager nfsd4_manager = {
2642 static void
2643 nfsd4_end_grace(void)
2645 dprintk("NFSD: end of grace period\n");
2646 nfsd4_recdir_purge_old();
2647 locks_end_grace(&nfsd4_manager);
2649 * Now that every NFSv4 client has had the chance to recover and
2650 * to see the (possibly new, possibly shorter) lease time, we
2651 * can safely set the next grace time to the current lease time:
2653 nfsd4_grace = nfsd4_lease;
2656 static time_t
2657 nfs4_laundromat(void)
2659 struct nfs4_client *clp;
2660 struct nfs4_stateowner *sop;
2661 struct nfs4_delegation *dp;
2662 struct list_head *pos, *next, reaplist;
2663 time_t cutoff = get_seconds() - nfsd4_lease;
2664 time_t t, clientid_val = nfsd4_lease;
2665 time_t u, test_val = nfsd4_lease;
2667 nfs4_lock_state();
2669 dprintk("NFSD: laundromat service - starting\n");
2670 if (locks_in_grace())
2671 nfsd4_end_grace();
2672 INIT_LIST_HEAD(&reaplist);
2673 spin_lock(&client_lock);
2674 list_for_each_safe(pos, next, &client_lru) {
2675 clp = list_entry(pos, struct nfs4_client, cl_lru);
2676 if (time_after((unsigned long)clp->cl_time, (unsigned long)cutoff)) {
2677 t = clp->cl_time - cutoff;
2678 if (clientid_val > t)
2679 clientid_val = t;
2680 break;
2682 if (atomic_read(&clp->cl_refcount)) {
2683 dprintk("NFSD: client in use (clientid %08x)\n",
2684 clp->cl_clientid.cl_id);
2685 continue;
2687 unhash_client_locked(clp);
2688 list_add(&clp->cl_lru, &reaplist);
2690 spin_unlock(&client_lock);
2691 list_for_each_safe(pos, next, &reaplist) {
2692 clp = list_entry(pos, struct nfs4_client, cl_lru);
2693 dprintk("NFSD: purging unused client (clientid %08x)\n",
2694 clp->cl_clientid.cl_id);
2695 nfsd4_remove_clid_dir(clp);
2696 expire_client(clp);
2698 spin_lock(&recall_lock);
2699 list_for_each_safe(pos, next, &del_recall_lru) {
2700 dp = list_entry (pos, struct nfs4_delegation, dl_recall_lru);
2701 if (time_after((unsigned long)dp->dl_time, (unsigned long)cutoff)) {
2702 u = dp->dl_time - cutoff;
2703 if (test_val > u)
2704 test_val = u;
2705 break;
2707 dprintk("NFSD: purging unused delegation dp %p, fp %p\n",
2708 dp, dp->dl_flock);
2709 list_move(&dp->dl_recall_lru, &reaplist);
2711 spin_unlock(&recall_lock);
2712 list_for_each_safe(pos, next, &reaplist) {
2713 dp = list_entry (pos, struct nfs4_delegation, dl_recall_lru);
2714 list_del_init(&dp->dl_recall_lru);
2715 unhash_delegation(dp);
2717 test_val = nfsd4_lease;
2718 list_for_each_safe(pos, next, &close_lru) {
2719 sop = list_entry(pos, struct nfs4_stateowner, so_close_lru);
2720 if (time_after((unsigned long)sop->so_time, (unsigned long)cutoff)) {
2721 u = sop->so_time - cutoff;
2722 if (test_val > u)
2723 test_val = u;
2724 break;
2726 dprintk("NFSD: purging unused open stateowner (so_id %d)\n",
2727 sop->so_id);
2728 release_openowner(sop);
2730 if (clientid_val < NFSD_LAUNDROMAT_MINTIMEOUT)
2731 clientid_val = NFSD_LAUNDROMAT_MINTIMEOUT;
2732 nfs4_unlock_state();
2733 return clientid_val;
2736 static struct workqueue_struct *laundry_wq;
2737 static void laundromat_main(struct work_struct *);
2738 static DECLARE_DELAYED_WORK(laundromat_work, laundromat_main);
2740 static void
2741 laundromat_main(struct work_struct *not_used)
2743 time_t t;
2745 t = nfs4_laundromat();
2746 dprintk("NFSD: laundromat_main - sleeping for %ld seconds\n", t);
2747 queue_delayed_work(laundry_wq, &laundromat_work, t*HZ);
2750 static struct nfs4_stateowner *
2751 search_close_lru(u32 st_id, int flags)
2753 struct nfs4_stateowner *local = NULL;
2755 if (flags & CLOSE_STATE) {
2756 list_for_each_entry(local, &close_lru, so_close_lru) {
2757 if (local->so_id == st_id)
2758 return local;
2761 return NULL;
2764 static inline int
2765 nfs4_check_fh(struct svc_fh *fhp, struct nfs4_stateid *stp)
2767 return fhp->fh_dentry->d_inode != stp->st_file->fi_inode;
2770 static int
2771 STALE_STATEID(stateid_t *stateid)
2773 if (stateid->si_boot == boot_time)
2774 return 0;
2775 dprintk("NFSD: stale stateid " STATEID_FMT "!\n",
2776 STATEID_VAL(stateid));
2777 return 1;
2780 static inline int
2781 access_permit_read(unsigned long access_bmap)
2783 return test_bit(NFS4_SHARE_ACCESS_READ, &access_bmap) ||
2784 test_bit(NFS4_SHARE_ACCESS_BOTH, &access_bmap) ||
2785 test_bit(NFS4_SHARE_ACCESS_WRITE, &access_bmap);
2788 static inline int
2789 access_permit_write(unsigned long access_bmap)
2791 return test_bit(NFS4_SHARE_ACCESS_WRITE, &access_bmap) ||
2792 test_bit(NFS4_SHARE_ACCESS_BOTH, &access_bmap);
2795 static
2796 __be32 nfs4_check_openmode(struct nfs4_stateid *stp, int flags)
2798 __be32 status = nfserr_openmode;
2800 /* For lock stateid's, we test the parent open, not the lock: */
2801 if (stp->st_openstp)
2802 stp = stp->st_openstp;
2803 if ((flags & WR_STATE) && (!access_permit_write(stp->st_access_bmap)))
2804 goto out;
2805 if ((flags & RD_STATE) && (!access_permit_read(stp->st_access_bmap)))
2806 goto out;
2807 status = nfs_ok;
2808 out:
2809 return status;
2812 static inline __be32
2813 check_special_stateids(svc_fh *current_fh, stateid_t *stateid, int flags)
2815 if (ONE_STATEID(stateid) && (flags & RD_STATE))
2816 return nfs_ok;
2817 else if (locks_in_grace()) {
2818 /* Answer in remaining cases depends on existance of
2819 * conflicting state; so we must wait out the grace period. */
2820 return nfserr_grace;
2821 } else if (flags & WR_STATE)
2822 return nfs4_share_conflict(current_fh,
2823 NFS4_SHARE_DENY_WRITE);
2824 else /* (flags & RD_STATE) && ZERO_STATEID(stateid) */
2825 return nfs4_share_conflict(current_fh,
2826 NFS4_SHARE_DENY_READ);
2830 * Allow READ/WRITE during grace period on recovered state only for files
2831 * that are not able to provide mandatory locking.
2833 static inline int
2834 grace_disallows_io(struct inode *inode)
2836 return locks_in_grace() && mandatory_lock(inode);
2839 static int check_stateid_generation(stateid_t *in, stateid_t *ref, int flags)
2842 * When sessions are used the stateid generation number is ignored
2843 * when it is zero.
2845 if ((flags & HAS_SESSION) && in->si_generation == 0)
2846 goto out;
2848 /* If the client sends us a stateid from the future, it's buggy: */
2849 if (in->si_generation > ref->si_generation)
2850 return nfserr_bad_stateid;
2852 * The following, however, can happen. For example, if the
2853 * client sends an open and some IO at the same time, the open
2854 * may bump si_generation while the IO is still in flight.
2855 * Thanks to hard links and renames, the client never knows what
2856 * file an open will affect. So it could avoid that situation
2857 * only by serializing all opens and IO from the same open
2858 * owner. To recover from the old_stateid error, the client
2859 * will just have to retry the IO:
2861 if (in->si_generation < ref->si_generation)
2862 return nfserr_old_stateid;
2863 out:
2864 return nfs_ok;
2867 static int is_delegation_stateid(stateid_t *stateid)
2869 return stateid->si_fileid == 0;
2873 * Checks for stateid operations
2875 __be32
2876 nfs4_preprocess_stateid_op(struct nfsd4_compound_state *cstate,
2877 stateid_t *stateid, int flags, struct file **filpp)
2879 struct nfs4_stateid *stp = NULL;
2880 struct nfs4_delegation *dp = NULL;
2881 struct svc_fh *current_fh = &cstate->current_fh;
2882 struct inode *ino = current_fh->fh_dentry->d_inode;
2883 __be32 status;
2885 if (filpp)
2886 *filpp = NULL;
2888 if (grace_disallows_io(ino))
2889 return nfserr_grace;
2891 if (nfsd4_has_session(cstate))
2892 flags |= HAS_SESSION;
2894 if (ZERO_STATEID(stateid) || ONE_STATEID(stateid))
2895 return check_special_stateids(current_fh, stateid, flags);
2897 status = nfserr_stale_stateid;
2898 if (STALE_STATEID(stateid))
2899 goto out;
2901 status = nfserr_bad_stateid;
2902 if (is_delegation_stateid(stateid)) {
2903 dp = find_delegation_stateid(ino, stateid);
2904 if (!dp)
2905 goto out;
2906 status = check_stateid_generation(stateid, &dp->dl_stateid,
2907 flags);
2908 if (status)
2909 goto out;
2910 status = nfs4_check_delegmode(dp, flags);
2911 if (status)
2912 goto out;
2913 renew_client(dp->dl_client);
2914 if (filpp)
2915 *filpp = find_readable_file(dp->dl_file);
2916 BUG_ON(!*filpp);
2917 } else { /* open or lock stateid */
2918 stp = find_stateid(stateid, flags);
2919 if (!stp)
2920 goto out;
2921 if (nfs4_check_fh(current_fh, stp))
2922 goto out;
2923 if (!stp->st_stateowner->so_confirmed)
2924 goto out;
2925 status = check_stateid_generation(stateid, &stp->st_stateid,
2926 flags);
2927 if (status)
2928 goto out;
2929 status = nfs4_check_openmode(stp, flags);
2930 if (status)
2931 goto out;
2932 renew_client(stp->st_stateowner->so_client);
2933 if (filpp) {
2934 if (flags & RD_STATE)
2935 *filpp = find_readable_file(stp->st_file);
2936 else
2937 *filpp = find_writeable_file(stp->st_file);
2940 status = nfs_ok;
2941 out:
2942 return status;
2945 static inline int
2946 setlkflg (int type)
2948 return (type == NFS4_READW_LT || type == NFS4_READ_LT) ?
2949 RD_STATE : WR_STATE;
2953 * Checks for sequence id mutating operations.
2955 static __be32
2956 nfs4_preprocess_seqid_op(struct nfsd4_compound_state *cstate, u32 seqid,
2957 stateid_t *stateid, int flags,
2958 struct nfs4_stateowner **sopp,
2959 struct nfs4_stateid **stpp, struct nfsd4_lock *lock)
2961 struct nfs4_stateid *stp;
2962 struct nfs4_stateowner *sop;
2963 struct svc_fh *current_fh = &cstate->current_fh;
2964 __be32 status;
2966 dprintk("NFSD: %s: seqid=%d stateid = " STATEID_FMT "\n", __func__,
2967 seqid, STATEID_VAL(stateid));
2969 *stpp = NULL;
2970 *sopp = NULL;
2972 if (ZERO_STATEID(stateid) || ONE_STATEID(stateid)) {
2973 dprintk("NFSD: preprocess_seqid_op: magic stateid!\n");
2974 return nfserr_bad_stateid;
2977 if (STALE_STATEID(stateid))
2978 return nfserr_stale_stateid;
2980 if (nfsd4_has_session(cstate))
2981 flags |= HAS_SESSION;
2984 * We return BAD_STATEID if filehandle doesn't match stateid,
2985 * the confirmed flag is incorrecly set, or the generation
2986 * number is incorrect.
2988 stp = find_stateid(stateid, flags);
2989 if (stp == NULL) {
2991 * Also, we should make sure this isn't just the result of
2992 * a replayed close:
2994 sop = search_close_lru(stateid->si_stateownerid, flags);
2995 if (sop == NULL)
2996 return nfserr_bad_stateid;
2997 *sopp = sop;
2998 goto check_replay;
3001 *stpp = stp;
3002 *sopp = sop = stp->st_stateowner;
3004 if (lock) {
3005 clientid_t *lockclid = &lock->v.new.clientid;
3006 struct nfs4_client *clp = sop->so_client;
3007 int lkflg = 0;
3008 __be32 status;
3010 lkflg = setlkflg(lock->lk_type);
3012 if (lock->lk_is_new) {
3013 if (!sop->so_is_open_owner)
3014 return nfserr_bad_stateid;
3015 if (!(flags & HAS_SESSION) &&
3016 !same_clid(&clp->cl_clientid, lockclid))
3017 return nfserr_bad_stateid;
3018 /* stp is the open stateid */
3019 status = nfs4_check_openmode(stp, lkflg);
3020 if (status)
3021 return status;
3022 } else {
3023 /* stp is the lock stateid */
3024 status = nfs4_check_openmode(stp->st_openstp, lkflg);
3025 if (status)
3026 return status;
3030 if (nfs4_check_fh(current_fh, stp)) {
3031 dprintk("NFSD: preprocess_seqid_op: fh-stateid mismatch!\n");
3032 return nfserr_bad_stateid;
3036 * We now validate the seqid and stateid generation numbers.
3037 * For the moment, we ignore the possibility of
3038 * generation number wraparound.
3040 if (!(flags & HAS_SESSION) && seqid != sop->so_seqid)
3041 goto check_replay;
3043 if (sop->so_confirmed && flags & CONFIRM) {
3044 dprintk("NFSD: preprocess_seqid_op: expected"
3045 " unconfirmed stateowner!\n");
3046 return nfserr_bad_stateid;
3048 if (!sop->so_confirmed && !(flags & CONFIRM)) {
3049 dprintk("NFSD: preprocess_seqid_op: stateowner not"
3050 " confirmed yet!\n");
3051 return nfserr_bad_stateid;
3053 status = check_stateid_generation(stateid, &stp->st_stateid, flags);
3054 if (status)
3055 return status;
3056 renew_client(sop->so_client);
3057 return nfs_ok;
3059 check_replay:
3060 if (seqid == sop->so_seqid - 1) {
3061 dprintk("NFSD: preprocess_seqid_op: retransmission?\n");
3062 /* indicate replay to calling function */
3063 return nfserr_replay_me;
3065 dprintk("NFSD: preprocess_seqid_op: bad seqid (expected %d, got %d)\n",
3066 sop->so_seqid, seqid);
3067 *sopp = NULL;
3068 return nfserr_bad_seqid;
3071 __be32
3072 nfsd4_open_confirm(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
3073 struct nfsd4_open_confirm *oc)
3075 __be32 status;
3076 struct nfs4_stateowner *sop;
3077 struct nfs4_stateid *stp;
3079 dprintk("NFSD: nfsd4_open_confirm on file %.*s\n",
3080 (int)cstate->current_fh.fh_dentry->d_name.len,
3081 cstate->current_fh.fh_dentry->d_name.name);
3083 status = fh_verify(rqstp, &cstate->current_fh, S_IFREG, 0);
3084 if (status)
3085 return status;
3087 nfs4_lock_state();
3089 if ((status = nfs4_preprocess_seqid_op(cstate,
3090 oc->oc_seqid, &oc->oc_req_stateid,
3091 CONFIRM | OPEN_STATE,
3092 &oc->oc_stateowner, &stp, NULL)))
3093 goto out;
3095 sop = oc->oc_stateowner;
3096 sop->so_confirmed = 1;
3097 update_stateid(&stp->st_stateid);
3098 memcpy(&oc->oc_resp_stateid, &stp->st_stateid, sizeof(stateid_t));
3099 dprintk("NFSD: %s: success, seqid=%d stateid=" STATEID_FMT "\n",
3100 __func__, oc->oc_seqid, STATEID_VAL(&stp->st_stateid));
3102 nfsd4_create_clid_dir(sop->so_client);
3103 out:
3104 if (oc->oc_stateowner) {
3105 nfs4_get_stateowner(oc->oc_stateowner);
3106 cstate->replay_owner = oc->oc_stateowner;
3108 nfs4_unlock_state();
3109 return status;
3114 * unset all bits in union bitmap (bmap) that
3115 * do not exist in share (from successful OPEN_DOWNGRADE)
3117 static void
3118 reset_union_bmap_access(unsigned long access, unsigned long *bmap)
3120 int i;
3121 for (i = 1; i < 4; i++) {
3122 if ((i & access) != i)
3123 __clear_bit(i, bmap);
3127 static void
3128 reset_union_bmap_deny(unsigned long deny, unsigned long *bmap)
3130 int i;
3131 for (i = 0; i < 4; i++) {
3132 if ((i & deny) != i)
3133 __clear_bit(i, bmap);
3137 __be32
3138 nfsd4_open_downgrade(struct svc_rqst *rqstp,
3139 struct nfsd4_compound_state *cstate,
3140 struct nfsd4_open_downgrade *od)
3142 __be32 status;
3143 struct nfs4_stateid *stp;
3144 unsigned int share_access;
3146 dprintk("NFSD: nfsd4_open_downgrade on file %.*s\n",
3147 (int)cstate->current_fh.fh_dentry->d_name.len,
3148 cstate->current_fh.fh_dentry->d_name.name);
3150 if (!access_valid(od->od_share_access, cstate->minorversion)
3151 || !deny_valid(od->od_share_deny))
3152 return nfserr_inval;
3154 nfs4_lock_state();
3155 if ((status = nfs4_preprocess_seqid_op(cstate,
3156 od->od_seqid,
3157 &od->od_stateid,
3158 OPEN_STATE,
3159 &od->od_stateowner, &stp, NULL)))
3160 goto out;
3162 status = nfserr_inval;
3163 if (!test_bit(od->od_share_access, &stp->st_access_bmap)) {
3164 dprintk("NFSD:access not a subset current bitmap: 0x%lx, input access=%08x\n",
3165 stp->st_access_bmap, od->od_share_access);
3166 goto out;
3168 if (!test_bit(od->od_share_deny, &stp->st_deny_bmap)) {
3169 dprintk("NFSD:deny not a subset current bitmap: 0x%lx, input deny=%08x\n",
3170 stp->st_deny_bmap, od->od_share_deny);
3171 goto out;
3173 set_access(&share_access, stp->st_access_bmap);
3174 nfs4_file_downgrade(stp->st_file, share_access & ~od->od_share_access);
3176 reset_union_bmap_access(od->od_share_access, &stp->st_access_bmap);
3177 reset_union_bmap_deny(od->od_share_deny, &stp->st_deny_bmap);
3179 update_stateid(&stp->st_stateid);
3180 memcpy(&od->od_stateid, &stp->st_stateid, sizeof(stateid_t));
3181 status = nfs_ok;
3182 out:
3183 if (od->od_stateowner) {
3184 nfs4_get_stateowner(od->od_stateowner);
3185 cstate->replay_owner = od->od_stateowner;
3187 nfs4_unlock_state();
3188 return status;
3192 * nfs4_unlock_state() called after encode
3194 __be32
3195 nfsd4_close(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
3196 struct nfsd4_close *close)
3198 __be32 status;
3199 struct nfs4_stateid *stp;
3201 dprintk("NFSD: nfsd4_close on file %.*s\n",
3202 (int)cstate->current_fh.fh_dentry->d_name.len,
3203 cstate->current_fh.fh_dentry->d_name.name);
3205 nfs4_lock_state();
3206 /* check close_lru for replay */
3207 if ((status = nfs4_preprocess_seqid_op(cstate,
3208 close->cl_seqid,
3209 &close->cl_stateid,
3210 OPEN_STATE | CLOSE_STATE,
3211 &close->cl_stateowner, &stp, NULL)))
3212 goto out;
3213 status = nfs_ok;
3214 update_stateid(&stp->st_stateid);
3215 memcpy(&close->cl_stateid, &stp->st_stateid, sizeof(stateid_t));
3217 /* release_stateid() calls nfsd_close() if needed */
3218 release_open_stateid(stp);
3220 /* place unused nfs4_stateowners on so_close_lru list to be
3221 * released by the laundromat service after the lease period
3222 * to enable us to handle CLOSE replay
3224 if (list_empty(&close->cl_stateowner->so_stateids))
3225 move_to_close_lru(close->cl_stateowner);
3226 out:
3227 if (close->cl_stateowner) {
3228 nfs4_get_stateowner(close->cl_stateowner);
3229 cstate->replay_owner = close->cl_stateowner;
3231 nfs4_unlock_state();
3232 return status;
3235 __be32
3236 nfsd4_delegreturn(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
3237 struct nfsd4_delegreturn *dr)
3239 struct nfs4_delegation *dp;
3240 stateid_t *stateid = &dr->dr_stateid;
3241 struct inode *inode;
3242 __be32 status;
3243 int flags = 0;
3245 if ((status = fh_verify(rqstp, &cstate->current_fh, S_IFREG, 0)))
3246 return status;
3247 inode = cstate->current_fh.fh_dentry->d_inode;
3249 if (nfsd4_has_session(cstate))
3250 flags |= HAS_SESSION;
3251 nfs4_lock_state();
3252 status = nfserr_bad_stateid;
3253 if (ZERO_STATEID(stateid) || ONE_STATEID(stateid))
3254 goto out;
3255 status = nfserr_stale_stateid;
3256 if (STALE_STATEID(stateid))
3257 goto out;
3258 status = nfserr_bad_stateid;
3259 if (!is_delegation_stateid(stateid))
3260 goto out;
3261 dp = find_delegation_stateid(inode, stateid);
3262 if (!dp)
3263 goto out;
3264 status = check_stateid_generation(stateid, &dp->dl_stateid, flags);
3265 if (status)
3266 goto out;
3267 renew_client(dp->dl_client);
3269 unhash_delegation(dp);
3270 out:
3271 nfs4_unlock_state();
3273 return status;
3278 * Lock owner state (byte-range locks)
3280 #define LOFF_OVERFLOW(start, len) ((u64)(len) > ~(u64)(start))
3281 #define LOCK_HASH_BITS 8
3282 #define LOCK_HASH_SIZE (1 << LOCK_HASH_BITS)
3283 #define LOCK_HASH_MASK (LOCK_HASH_SIZE - 1)
3285 static inline u64
3286 end_offset(u64 start, u64 len)
3288 u64 end;
3290 end = start + len;
3291 return end >= start ? end: NFS4_MAX_UINT64;
3294 /* last octet in a range */
3295 static inline u64
3296 last_byte_offset(u64 start, u64 len)
3298 u64 end;
3300 BUG_ON(!len);
3301 end = start + len;
3302 return end > start ? end - 1: NFS4_MAX_UINT64;
3305 #define lockownerid_hashval(id) \
3306 ((id) & LOCK_HASH_MASK)
3308 static inline unsigned int
3309 lock_ownerstr_hashval(struct inode *inode, u32 cl_id,
3310 struct xdr_netobj *ownername)
3312 return (file_hashval(inode) + cl_id
3313 + opaque_hashval(ownername->data, ownername->len))
3314 & LOCK_HASH_MASK;
3317 static struct list_head lock_ownerid_hashtbl[LOCK_HASH_SIZE];
3318 static struct list_head lock_ownerstr_hashtbl[LOCK_HASH_SIZE];
3319 static struct list_head lockstateid_hashtbl[STATEID_HASH_SIZE];
3321 static struct nfs4_stateid *
3322 find_stateid(stateid_t *stid, int flags)
3324 struct nfs4_stateid *local;
3325 u32 st_id = stid->si_stateownerid;
3326 u32 f_id = stid->si_fileid;
3327 unsigned int hashval;
3329 dprintk("NFSD: find_stateid flags 0x%x\n",flags);
3330 if (flags & (LOCK_STATE | RD_STATE | WR_STATE)) {
3331 hashval = stateid_hashval(st_id, f_id);
3332 list_for_each_entry(local, &lockstateid_hashtbl[hashval], st_hash) {
3333 if ((local->st_stateid.si_stateownerid == st_id) &&
3334 (local->st_stateid.si_fileid == f_id))
3335 return local;
3339 if (flags & (OPEN_STATE | RD_STATE | WR_STATE)) {
3340 hashval = stateid_hashval(st_id, f_id);
3341 list_for_each_entry(local, &stateid_hashtbl[hashval], st_hash) {
3342 if ((local->st_stateid.si_stateownerid == st_id) &&
3343 (local->st_stateid.si_fileid == f_id))
3344 return local;
3347 return NULL;
3350 static struct nfs4_delegation *
3351 find_delegation_stateid(struct inode *ino, stateid_t *stid)
3353 struct nfs4_file *fp;
3354 struct nfs4_delegation *dl;
3356 dprintk("NFSD: %s: stateid=" STATEID_FMT "\n", __func__,
3357 STATEID_VAL(stid));
3359 fp = find_file(ino);
3360 if (!fp)
3361 return NULL;
3362 dl = find_delegation_file(fp, stid);
3363 put_nfs4_file(fp);
3364 return dl;
3368 * TODO: Linux file offsets are _signed_ 64-bit quantities, which means that
3369 * we can't properly handle lock requests that go beyond the (2^63 - 1)-th
3370 * byte, because of sign extension problems. Since NFSv4 calls for 64-bit
3371 * locking, this prevents us from being completely protocol-compliant. The
3372 * real solution to this problem is to start using unsigned file offsets in
3373 * the VFS, but this is a very deep change!
3375 static inline void
3376 nfs4_transform_lock_offset(struct file_lock *lock)
3378 if (lock->fl_start < 0)
3379 lock->fl_start = OFFSET_MAX;
3380 if (lock->fl_end < 0)
3381 lock->fl_end = OFFSET_MAX;
3384 /* Hack!: For now, we're defining this just so we can use a pointer to it
3385 * as a unique cookie to identify our (NFSv4's) posix locks. */
3386 static const struct lock_manager_operations nfsd_posix_mng_ops = {
3389 static inline void
3390 nfs4_set_lock_denied(struct file_lock *fl, struct nfsd4_lock_denied *deny)
3392 struct nfs4_stateowner *sop;
3394 if (fl->fl_lmops == &nfsd_posix_mng_ops) {
3395 sop = (struct nfs4_stateowner *) fl->fl_owner;
3396 kref_get(&sop->so_ref);
3397 deny->ld_sop = sop;
3398 deny->ld_clientid = sop->so_client->cl_clientid;
3399 } else {
3400 deny->ld_sop = NULL;
3401 deny->ld_clientid.cl_boot = 0;
3402 deny->ld_clientid.cl_id = 0;
3404 deny->ld_start = fl->fl_start;
3405 deny->ld_length = NFS4_MAX_UINT64;
3406 if (fl->fl_end != NFS4_MAX_UINT64)
3407 deny->ld_length = fl->fl_end - fl->fl_start + 1;
3408 deny->ld_type = NFS4_READ_LT;
3409 if (fl->fl_type != F_RDLCK)
3410 deny->ld_type = NFS4_WRITE_LT;
3413 static struct nfs4_stateowner *
3414 find_lockstateowner_str(struct inode *inode, clientid_t *clid,
3415 struct xdr_netobj *owner)
3417 unsigned int hashval = lock_ownerstr_hashval(inode, clid->cl_id, owner);
3418 struct nfs4_stateowner *op;
3420 list_for_each_entry(op, &lock_ownerstr_hashtbl[hashval], so_strhash) {
3421 if (same_owner_str(op, owner, clid))
3422 return op;
3424 return NULL;
3428 * Alloc a lock owner structure.
3429 * Called in nfsd4_lock - therefore, OPEN and OPEN_CONFIRM (if needed) has
3430 * occured.
3432 * strhashval = lock_ownerstr_hashval
3435 static struct nfs4_stateowner *
3436 alloc_init_lock_stateowner(unsigned int strhashval, struct nfs4_client *clp, struct nfs4_stateid *open_stp, struct nfsd4_lock *lock) {
3437 struct nfs4_stateowner *sop;
3438 struct nfs4_replay *rp;
3439 unsigned int idhashval;
3441 if (!(sop = alloc_stateowner(&lock->lk_new_owner)))
3442 return NULL;
3443 idhashval = lockownerid_hashval(current_ownerid);
3444 INIT_LIST_HEAD(&sop->so_idhash);
3445 INIT_LIST_HEAD(&sop->so_strhash);
3446 INIT_LIST_HEAD(&sop->so_perclient);
3447 INIT_LIST_HEAD(&sop->so_stateids);
3448 INIT_LIST_HEAD(&sop->so_perstateid);
3449 INIT_LIST_HEAD(&sop->so_close_lru); /* not used */
3450 sop->so_time = 0;
3451 list_add(&sop->so_idhash, &lock_ownerid_hashtbl[idhashval]);
3452 list_add(&sop->so_strhash, &lock_ownerstr_hashtbl[strhashval]);
3453 list_add(&sop->so_perstateid, &open_stp->st_lockowners);
3454 sop->so_is_open_owner = 0;
3455 sop->so_id = current_ownerid++;
3456 sop->so_client = clp;
3457 /* It is the openowner seqid that will be incremented in encode in the
3458 * case of new lockowners; so increment the lock seqid manually: */
3459 sop->so_seqid = lock->lk_new_lock_seqid + 1;
3460 sop->so_confirmed = 1;
3461 rp = &sop->so_replay;
3462 rp->rp_status = nfserr_serverfault;
3463 rp->rp_buflen = 0;
3464 rp->rp_buf = rp->rp_ibuf;
3465 return sop;
3468 static struct nfs4_stateid *
3469 alloc_init_lock_stateid(struct nfs4_stateowner *sop, struct nfs4_file *fp, struct nfs4_stateid *open_stp)
3471 struct nfs4_stateid *stp;
3472 unsigned int hashval = stateid_hashval(sop->so_id, fp->fi_id);
3474 stp = nfs4_alloc_stateid();
3475 if (stp == NULL)
3476 goto out;
3477 INIT_LIST_HEAD(&stp->st_hash);
3478 INIT_LIST_HEAD(&stp->st_perfile);
3479 INIT_LIST_HEAD(&stp->st_perstateowner);
3480 INIT_LIST_HEAD(&stp->st_lockowners); /* not used */
3481 list_add(&stp->st_hash, &lockstateid_hashtbl[hashval]);
3482 list_add(&stp->st_perfile, &fp->fi_stateids);
3483 list_add(&stp->st_perstateowner, &sop->so_stateids);
3484 stp->st_stateowner = sop;
3485 get_nfs4_file(fp);
3486 stp->st_file = fp;
3487 stp->st_stateid.si_boot = boot_time;
3488 stp->st_stateid.si_stateownerid = sop->so_id;
3489 stp->st_stateid.si_fileid = fp->fi_id;
3490 stp->st_stateid.si_generation = 0;
3491 stp->st_deny_bmap = open_stp->st_deny_bmap;
3492 stp->st_openstp = open_stp;
3494 out:
3495 return stp;
3498 static int
3499 check_lock_length(u64 offset, u64 length)
3501 return ((length == 0) || ((length != NFS4_MAX_UINT64) &&
3502 LOFF_OVERFLOW(offset, length)));
3506 * LOCK operation
3508 __be32
3509 nfsd4_lock(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
3510 struct nfsd4_lock *lock)
3512 struct nfs4_stateowner *open_sop = NULL;
3513 struct nfs4_stateowner *lock_sop = NULL;
3514 struct nfs4_stateid *lock_stp;
3515 struct nfs4_file *fp;
3516 struct file *filp = NULL;
3517 struct file_lock file_lock;
3518 struct file_lock conflock;
3519 __be32 status = 0;
3520 unsigned int strhashval;
3521 unsigned int cmd;
3522 int err;
3524 dprintk("NFSD: nfsd4_lock: start=%Ld length=%Ld\n",
3525 (long long) lock->lk_offset,
3526 (long long) lock->lk_length);
3528 if (check_lock_length(lock->lk_offset, lock->lk_length))
3529 return nfserr_inval;
3531 if ((status = fh_verify(rqstp, &cstate->current_fh,
3532 S_IFREG, NFSD_MAY_LOCK))) {
3533 dprintk("NFSD: nfsd4_lock: permission denied!\n");
3534 return status;
3537 nfs4_lock_state();
3539 if (lock->lk_is_new) {
3541 * Client indicates that this is a new lockowner.
3542 * Use open owner and open stateid to create lock owner and
3543 * lock stateid.
3545 struct nfs4_stateid *open_stp = NULL;
3547 status = nfserr_stale_clientid;
3548 if (!nfsd4_has_session(cstate) &&
3549 STALE_CLIENTID(&lock->lk_new_clientid))
3550 goto out;
3552 /* validate and update open stateid and open seqid */
3553 status = nfs4_preprocess_seqid_op(cstate,
3554 lock->lk_new_open_seqid,
3555 &lock->lk_new_open_stateid,
3556 OPEN_STATE,
3557 &lock->lk_replay_owner, &open_stp,
3558 lock);
3559 if (status)
3560 goto out;
3561 open_sop = lock->lk_replay_owner;
3562 /* create lockowner and lock stateid */
3563 fp = open_stp->st_file;
3564 strhashval = lock_ownerstr_hashval(fp->fi_inode,
3565 open_sop->so_client->cl_clientid.cl_id,
3566 &lock->v.new.owner);
3567 status = nfserr_resource;
3568 lock_sop = alloc_init_lock_stateowner(strhashval,
3569 open_sop->so_client, open_stp, lock);
3570 if (lock_sop == NULL)
3571 goto out;
3572 lock_stp = alloc_init_lock_stateid(lock_sop, fp, open_stp);
3573 if (lock_stp == NULL)
3574 goto out;
3575 } else {
3576 /* lock (lock owner + lock stateid) already exists */
3577 status = nfs4_preprocess_seqid_op(cstate,
3578 lock->lk_old_lock_seqid,
3579 &lock->lk_old_lock_stateid,
3580 LOCK_STATE,
3581 &lock->lk_replay_owner, &lock_stp, lock);
3582 if (status)
3583 goto out;
3584 lock_sop = lock->lk_replay_owner;
3585 fp = lock_stp->st_file;
3587 /* lock->lk_replay_owner and lock_stp have been created or found */
3589 status = nfserr_grace;
3590 if (locks_in_grace() && !lock->lk_reclaim)
3591 goto out;
3592 status = nfserr_no_grace;
3593 if (!locks_in_grace() && lock->lk_reclaim)
3594 goto out;
3596 locks_init_lock(&file_lock);
3597 switch (lock->lk_type) {
3598 case NFS4_READ_LT:
3599 case NFS4_READW_LT:
3600 if (find_readable_file(lock_stp->st_file)) {
3601 nfs4_get_vfs_file(rqstp, fp, &cstate->current_fh, NFS4_SHARE_ACCESS_READ);
3602 filp = find_readable_file(lock_stp->st_file);
3604 file_lock.fl_type = F_RDLCK;
3605 cmd = F_SETLK;
3606 break;
3607 case NFS4_WRITE_LT:
3608 case NFS4_WRITEW_LT:
3609 if (find_writeable_file(lock_stp->st_file)) {
3610 nfs4_get_vfs_file(rqstp, fp, &cstate->current_fh, NFS4_SHARE_ACCESS_WRITE);
3611 filp = find_writeable_file(lock_stp->st_file);
3613 file_lock.fl_type = F_WRLCK;
3614 cmd = F_SETLK;
3615 break;
3616 default:
3617 status = nfserr_inval;
3618 goto out;
3620 if (!filp) {
3621 status = nfserr_openmode;
3622 goto out;
3624 file_lock.fl_owner = (fl_owner_t)lock_sop;
3625 file_lock.fl_pid = current->tgid;
3626 file_lock.fl_file = filp;
3627 file_lock.fl_flags = FL_POSIX;
3628 file_lock.fl_lmops = &nfsd_posix_mng_ops;
3630 file_lock.fl_start = lock->lk_offset;
3631 file_lock.fl_end = last_byte_offset(lock->lk_offset, lock->lk_length);
3632 nfs4_transform_lock_offset(&file_lock);
3635 * Try to lock the file in the VFS.
3636 * Note: locks.c uses the BKL to protect the inode's lock list.
3639 err = vfs_lock_file(filp, cmd, &file_lock, &conflock);
3640 switch (-err) {
3641 case 0: /* success! */
3642 update_stateid(&lock_stp->st_stateid);
3643 memcpy(&lock->lk_resp_stateid, &lock_stp->st_stateid,
3644 sizeof(stateid_t));
3645 status = 0;
3646 break;
3647 case (EAGAIN): /* conflock holds conflicting lock */
3648 status = nfserr_denied;
3649 dprintk("NFSD: nfsd4_lock: conflicting lock found!\n");
3650 nfs4_set_lock_denied(&conflock, &lock->lk_denied);
3651 break;
3652 case (EDEADLK):
3653 status = nfserr_deadlock;
3654 break;
3655 default:
3656 dprintk("NFSD: nfsd4_lock: vfs_lock_file() failed! status %d\n",err);
3657 status = nfserr_resource;
3658 break;
3660 out:
3661 if (status && lock->lk_is_new && lock_sop)
3662 release_lockowner(lock_sop);
3663 if (lock->lk_replay_owner) {
3664 nfs4_get_stateowner(lock->lk_replay_owner);
3665 cstate->replay_owner = lock->lk_replay_owner;
3667 nfs4_unlock_state();
3668 return status;
3672 * The NFSv4 spec allows a client to do a LOCKT without holding an OPEN,
3673 * so we do a temporary open here just to get an open file to pass to
3674 * vfs_test_lock. (Arguably perhaps test_lock should be done with an
3675 * inode operation.)
3677 static int nfsd_test_lock(struct svc_rqst *rqstp, struct svc_fh *fhp, struct file_lock *lock)
3679 struct file *file;
3680 int err;
3682 err = nfsd_open(rqstp, fhp, S_IFREG, NFSD_MAY_READ, &file);
3683 if (err)
3684 return err;
3685 err = vfs_test_lock(file, lock);
3686 nfsd_close(file);
3687 return err;
3691 * LOCKT operation
3693 __be32
3694 nfsd4_lockt(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
3695 struct nfsd4_lockt *lockt)
3697 struct inode *inode;
3698 struct file_lock file_lock;
3699 int error;
3700 __be32 status;
3702 if (locks_in_grace())
3703 return nfserr_grace;
3705 if (check_lock_length(lockt->lt_offset, lockt->lt_length))
3706 return nfserr_inval;
3708 lockt->lt_stateowner = NULL;
3709 nfs4_lock_state();
3711 status = nfserr_stale_clientid;
3712 if (!nfsd4_has_session(cstate) && STALE_CLIENTID(&lockt->lt_clientid))
3713 goto out;
3715 if ((status = fh_verify(rqstp, &cstate->current_fh, S_IFREG, 0))) {
3716 dprintk("NFSD: nfsd4_lockt: fh_verify() failed!\n");
3717 if (status == nfserr_symlink)
3718 status = nfserr_inval;
3719 goto out;
3722 inode = cstate->current_fh.fh_dentry->d_inode;
3723 locks_init_lock(&file_lock);
3724 switch (lockt->lt_type) {
3725 case NFS4_READ_LT:
3726 case NFS4_READW_LT:
3727 file_lock.fl_type = F_RDLCK;
3728 break;
3729 case NFS4_WRITE_LT:
3730 case NFS4_WRITEW_LT:
3731 file_lock.fl_type = F_WRLCK;
3732 break;
3733 default:
3734 dprintk("NFSD: nfs4_lockt: bad lock type!\n");
3735 status = nfserr_inval;
3736 goto out;
3739 lockt->lt_stateowner = find_lockstateowner_str(inode,
3740 &lockt->lt_clientid, &lockt->lt_owner);
3741 if (lockt->lt_stateowner)
3742 file_lock.fl_owner = (fl_owner_t)lockt->lt_stateowner;
3743 file_lock.fl_pid = current->tgid;
3744 file_lock.fl_flags = FL_POSIX;
3746 file_lock.fl_start = lockt->lt_offset;
3747 file_lock.fl_end = last_byte_offset(lockt->lt_offset, lockt->lt_length);
3749 nfs4_transform_lock_offset(&file_lock);
3751 status = nfs_ok;
3752 error = nfsd_test_lock(rqstp, &cstate->current_fh, &file_lock);
3753 if (error) {
3754 status = nfserrno(error);
3755 goto out;
3757 if (file_lock.fl_type != F_UNLCK) {
3758 status = nfserr_denied;
3759 nfs4_set_lock_denied(&file_lock, &lockt->lt_denied);
3761 out:
3762 nfs4_unlock_state();
3763 return status;
3766 __be32
3767 nfsd4_locku(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
3768 struct nfsd4_locku *locku)
3770 struct nfs4_stateid *stp;
3771 struct file *filp = NULL;
3772 struct file_lock file_lock;
3773 __be32 status;
3774 int err;
3776 dprintk("NFSD: nfsd4_locku: start=%Ld length=%Ld\n",
3777 (long long) locku->lu_offset,
3778 (long long) locku->lu_length);
3780 if (check_lock_length(locku->lu_offset, locku->lu_length))
3781 return nfserr_inval;
3783 nfs4_lock_state();
3785 if ((status = nfs4_preprocess_seqid_op(cstate,
3786 locku->lu_seqid,
3787 &locku->lu_stateid,
3788 LOCK_STATE,
3789 &locku->lu_stateowner, &stp, NULL)))
3790 goto out;
3792 filp = find_any_file(stp->st_file);
3793 if (!filp) {
3794 status = nfserr_lock_range;
3795 goto out;
3797 BUG_ON(!filp);
3798 locks_init_lock(&file_lock);
3799 file_lock.fl_type = F_UNLCK;
3800 file_lock.fl_owner = (fl_owner_t) locku->lu_stateowner;
3801 file_lock.fl_pid = current->tgid;
3802 file_lock.fl_file = filp;
3803 file_lock.fl_flags = FL_POSIX;
3804 file_lock.fl_lmops = &nfsd_posix_mng_ops;
3805 file_lock.fl_start = locku->lu_offset;
3807 file_lock.fl_end = last_byte_offset(locku->lu_offset, locku->lu_length);
3808 nfs4_transform_lock_offset(&file_lock);
3811 * Try to unlock the file in the VFS.
3813 err = vfs_lock_file(filp, F_SETLK, &file_lock, NULL);
3814 if (err) {
3815 dprintk("NFSD: nfs4_locku: vfs_lock_file failed!\n");
3816 goto out_nfserr;
3819 * OK, unlock succeeded; the only thing left to do is update the stateid.
3821 update_stateid(&stp->st_stateid);
3822 memcpy(&locku->lu_stateid, &stp->st_stateid, sizeof(stateid_t));
3824 out:
3825 if (locku->lu_stateowner) {
3826 nfs4_get_stateowner(locku->lu_stateowner);
3827 cstate->replay_owner = locku->lu_stateowner;
3829 nfs4_unlock_state();
3830 return status;
3832 out_nfserr:
3833 status = nfserrno(err);
3834 goto out;
3838 * returns
3839 * 1: locks held by lockowner
3840 * 0: no locks held by lockowner
3842 static int
3843 check_for_locks(struct nfs4_file *filp, struct nfs4_stateowner *lowner)
3845 struct file_lock **flpp;
3846 struct inode *inode = filp->fi_inode;
3847 int status = 0;
3849 lock_kernel();
3850 for (flpp = &inode->i_flock; *flpp != NULL; flpp = &(*flpp)->fl_next) {
3851 if ((*flpp)->fl_owner == (fl_owner_t)lowner) {
3852 status = 1;
3853 goto out;
3856 out:
3857 unlock_kernel();
3858 return status;
3861 __be32
3862 nfsd4_release_lockowner(struct svc_rqst *rqstp,
3863 struct nfsd4_compound_state *cstate,
3864 struct nfsd4_release_lockowner *rlockowner)
3866 clientid_t *clid = &rlockowner->rl_clientid;
3867 struct nfs4_stateowner *sop;
3868 struct nfs4_stateid *stp;
3869 struct xdr_netobj *owner = &rlockowner->rl_owner;
3870 struct list_head matches;
3871 int i;
3872 __be32 status;
3874 dprintk("nfsd4_release_lockowner clientid: (%08x/%08x):\n",
3875 clid->cl_boot, clid->cl_id);
3878 status = nfserr_stale_clientid;
3879 if (STALE_CLIENTID(clid))
3880 return status;
3882 nfs4_lock_state();
3884 status = nfserr_locks_held;
3885 INIT_LIST_HEAD(&matches);
3886 for (i = 0; i < LOCK_HASH_SIZE; i++) {
3887 list_for_each_entry(sop, &lock_ownerid_hashtbl[i], so_idhash) {
3888 if (!same_owner_str(sop, owner, clid))
3889 continue;
3890 list_for_each_entry(stp, &sop->so_stateids,
3891 st_perstateowner) {
3892 if (check_for_locks(stp->st_file, sop))
3893 goto out;
3894 /* Note: so_perclient unused for lockowners,
3895 * so it's OK to fool with here. */
3896 list_add(&sop->so_perclient, &matches);
3900 /* Clients probably won't expect us to return with some (but not all)
3901 * of the lockowner state released; so don't release any until all
3902 * have been checked. */
3903 status = nfs_ok;
3904 while (!list_empty(&matches)) {
3905 sop = list_entry(matches.next, struct nfs4_stateowner,
3906 so_perclient);
3907 /* unhash_stateowner deletes so_perclient only
3908 * for openowners. */
3909 list_del(&sop->so_perclient);
3910 release_lockowner(sop);
3912 out:
3913 nfs4_unlock_state();
3914 return status;
3917 static inline struct nfs4_client_reclaim *
3918 alloc_reclaim(void)
3920 return kmalloc(sizeof(struct nfs4_client_reclaim), GFP_KERNEL);
3924 nfs4_has_reclaimed_state(const char *name, bool use_exchange_id)
3926 unsigned int strhashval = clientstr_hashval(name);
3927 struct nfs4_client *clp;
3929 clp = find_confirmed_client_by_str(name, strhashval, use_exchange_id);
3930 return clp ? 1 : 0;
3934 * failure => all reset bets are off, nfserr_no_grace...
3937 nfs4_client_to_reclaim(const char *name)
3939 unsigned int strhashval;
3940 struct nfs4_client_reclaim *crp = NULL;
3942 dprintk("NFSD nfs4_client_to_reclaim NAME: %.*s\n", HEXDIR_LEN, name);
3943 crp = alloc_reclaim();
3944 if (!crp)
3945 return 0;
3946 strhashval = clientstr_hashval(name);
3947 INIT_LIST_HEAD(&crp->cr_strhash);
3948 list_add(&crp->cr_strhash, &reclaim_str_hashtbl[strhashval]);
3949 memcpy(crp->cr_recdir, name, HEXDIR_LEN);
3950 reclaim_str_hashtbl_size++;
3951 return 1;
3954 static void
3955 nfs4_release_reclaim(void)
3957 struct nfs4_client_reclaim *crp = NULL;
3958 int i;
3960 for (i = 0; i < CLIENT_HASH_SIZE; i++) {
3961 while (!list_empty(&reclaim_str_hashtbl[i])) {
3962 crp = list_entry(reclaim_str_hashtbl[i].next,
3963 struct nfs4_client_reclaim, cr_strhash);
3964 list_del(&crp->cr_strhash);
3965 kfree(crp);
3966 reclaim_str_hashtbl_size--;
3969 BUG_ON(reclaim_str_hashtbl_size);
3973 * called from OPEN, CLAIM_PREVIOUS with a new clientid. */
3974 static struct nfs4_client_reclaim *
3975 nfs4_find_reclaim_client(clientid_t *clid)
3977 unsigned int strhashval;
3978 struct nfs4_client *clp;
3979 struct nfs4_client_reclaim *crp = NULL;
3982 /* find clientid in conf_id_hashtbl */
3983 clp = find_confirmed_client(clid);
3984 if (clp == NULL)
3985 return NULL;
3987 dprintk("NFSD: nfs4_find_reclaim_client for %.*s with recdir %s\n",
3988 clp->cl_name.len, clp->cl_name.data,
3989 clp->cl_recdir);
3991 /* find clp->cl_name in reclaim_str_hashtbl */
3992 strhashval = clientstr_hashval(clp->cl_recdir);
3993 list_for_each_entry(crp, &reclaim_str_hashtbl[strhashval], cr_strhash) {
3994 if (same_name(crp->cr_recdir, clp->cl_recdir)) {
3995 return crp;
3998 return NULL;
4002 * Called from OPEN. Look for clientid in reclaim list.
4004 __be32
4005 nfs4_check_open_reclaim(clientid_t *clid)
4007 return nfs4_find_reclaim_client(clid) ? nfs_ok : nfserr_reclaim_bad;
4010 /* initialization to perform at module load time: */
4013 nfs4_state_init(void)
4015 int i, status;
4017 status = nfsd4_init_slabs();
4018 if (status)
4019 return status;
4020 for (i = 0; i < CLIENT_HASH_SIZE; i++) {
4021 INIT_LIST_HEAD(&conf_id_hashtbl[i]);
4022 INIT_LIST_HEAD(&conf_str_hashtbl[i]);
4023 INIT_LIST_HEAD(&unconf_str_hashtbl[i]);
4024 INIT_LIST_HEAD(&unconf_id_hashtbl[i]);
4025 INIT_LIST_HEAD(&reclaim_str_hashtbl[i]);
4027 for (i = 0; i < SESSION_HASH_SIZE; i++)
4028 INIT_LIST_HEAD(&sessionid_hashtbl[i]);
4029 for (i = 0; i < FILE_HASH_SIZE; i++) {
4030 INIT_LIST_HEAD(&file_hashtbl[i]);
4032 for (i = 0; i < OWNER_HASH_SIZE; i++) {
4033 INIT_LIST_HEAD(&ownerstr_hashtbl[i]);
4034 INIT_LIST_HEAD(&ownerid_hashtbl[i]);
4036 for (i = 0; i < STATEID_HASH_SIZE; i++) {
4037 INIT_LIST_HEAD(&stateid_hashtbl[i]);
4038 INIT_LIST_HEAD(&lockstateid_hashtbl[i]);
4040 for (i = 0; i < LOCK_HASH_SIZE; i++) {
4041 INIT_LIST_HEAD(&lock_ownerid_hashtbl[i]);
4042 INIT_LIST_HEAD(&lock_ownerstr_hashtbl[i]);
4044 memset(&onestateid, ~0, sizeof(stateid_t));
4045 INIT_LIST_HEAD(&close_lru);
4046 INIT_LIST_HEAD(&client_lru);
4047 INIT_LIST_HEAD(&del_recall_lru);
4048 reclaim_str_hashtbl_size = 0;
4049 return 0;
4052 static void
4053 nfsd4_load_reboot_recovery_data(void)
4055 int status;
4057 nfs4_lock_state();
4058 nfsd4_init_recdir(user_recovery_dirname);
4059 status = nfsd4_recdir_load();
4060 nfs4_unlock_state();
4061 if (status)
4062 printk("NFSD: Failure reading reboot recovery data\n");
4066 * Since the lifetime of a delegation isn't limited to that of an open, a
4067 * client may quite reasonably hang on to a delegation as long as it has
4068 * the inode cached. This becomes an obvious problem the first time a
4069 * client's inode cache approaches the size of the server's total memory.
4071 * For now we avoid this problem by imposing a hard limit on the number
4072 * of delegations, which varies according to the server's memory size.
4074 static void
4075 set_max_delegations(void)
4078 * Allow at most 4 delegations per megabyte of RAM. Quick
4079 * estimates suggest that in the worst case (where every delegation
4080 * is for a different inode), a delegation could take about 1.5K,
4081 * giving a worst case usage of about 6% of memory.
4083 max_delegations = nr_free_buffer_pages() >> (20 - 2 - PAGE_SHIFT);
4086 /* initialization to perform when the nfsd service is started: */
4088 static int
4089 __nfs4_state_start(void)
4091 int ret;
4093 boot_time = get_seconds();
4094 locks_start_grace(&nfsd4_manager);
4095 printk(KERN_INFO "NFSD: starting %ld-second grace period\n",
4096 nfsd4_grace);
4097 ret = set_callback_cred();
4098 if (ret)
4099 return -ENOMEM;
4100 laundry_wq = create_singlethread_workqueue("nfsd4");
4101 if (laundry_wq == NULL)
4102 return -ENOMEM;
4103 ret = nfsd4_create_callback_queue();
4104 if (ret)
4105 goto out_free_laundry;
4106 queue_delayed_work(laundry_wq, &laundromat_work, nfsd4_grace * HZ);
4107 set_max_delegations();
4108 return 0;
4109 out_free_laundry:
4110 destroy_workqueue(laundry_wq);
4111 return ret;
4115 nfs4_state_start(void)
4117 nfsd4_load_reboot_recovery_data();
4118 return __nfs4_state_start();
4121 static void
4122 __nfs4_state_shutdown(void)
4124 int i;
4125 struct nfs4_client *clp = NULL;
4126 struct nfs4_delegation *dp = NULL;
4127 struct list_head *pos, *next, reaplist;
4129 for (i = 0; i < CLIENT_HASH_SIZE; i++) {
4130 while (!list_empty(&conf_id_hashtbl[i])) {
4131 clp = list_entry(conf_id_hashtbl[i].next, struct nfs4_client, cl_idhash);
4132 expire_client(clp);
4134 while (!list_empty(&unconf_str_hashtbl[i])) {
4135 clp = list_entry(unconf_str_hashtbl[i].next, struct nfs4_client, cl_strhash);
4136 expire_client(clp);
4139 INIT_LIST_HEAD(&reaplist);
4140 spin_lock(&recall_lock);
4141 list_for_each_safe(pos, next, &del_recall_lru) {
4142 dp = list_entry (pos, struct nfs4_delegation, dl_recall_lru);
4143 list_move(&dp->dl_recall_lru, &reaplist);
4145 spin_unlock(&recall_lock);
4146 list_for_each_safe(pos, next, &reaplist) {
4147 dp = list_entry (pos, struct nfs4_delegation, dl_recall_lru);
4148 list_del_init(&dp->dl_recall_lru);
4149 unhash_delegation(dp);
4152 nfsd4_shutdown_recdir();
4155 void
4156 nfs4_state_shutdown(void)
4158 cancel_rearming_delayed_workqueue(laundry_wq, &laundromat_work);
4159 destroy_workqueue(laundry_wq);
4160 locks_end_grace(&nfsd4_manager);
4161 nfs4_lock_state();
4162 nfs4_release_reclaim();
4163 __nfs4_state_shutdown();
4164 nfs4_unlock_state();
4165 nfsd4_destroy_callback_queue();
4169 * user_recovery_dirname is protected by the nfsd_mutex since it's only
4170 * accessed when nfsd is starting.
4172 static void
4173 nfs4_set_recdir(char *recdir)
4175 strcpy(user_recovery_dirname, recdir);
4179 * Change the NFSv4 recovery directory to recdir.
4182 nfs4_reset_recoverydir(char *recdir)
4184 int status;
4185 struct path path;
4187 status = kern_path(recdir, LOOKUP_FOLLOW, &path);
4188 if (status)
4189 return status;
4190 status = -ENOTDIR;
4191 if (S_ISDIR(path.dentry->d_inode->i_mode)) {
4192 nfs4_set_recdir(recdir);
4193 status = 0;
4195 path_put(&path);
4196 return status;
4199 char *
4200 nfs4_recoverydir(void)
4202 return user_recovery_dirname;