2 * Copyright (c) 2001 The Regents of the University of Michigan.
5 * Kendrick Smith <kmsmith@umich.edu>
6 * Andy Adamson <kandros@umich.edu>
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the distribution.
17 * 3. Neither the name of the University nor the names of its
18 * contributors may be used to endorse or promote products derived
19 * from this software without specific prior written permission.
21 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED
22 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
23 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
24 * DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
26 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
27 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
28 * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
29 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
30 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
31 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
35 #include <linux/file.h>
37 #include <linux/slab.h>
38 #include <linux/namei.h>
39 #include <linux/swap.h>
40 #include <linux/pagemap.h>
41 #include <linux/sunrpc/svcauth_gss.h>
42 #include <linux/sunrpc/clnt.h>
46 #define NFSDDBG_FACILITY NFSDDBG_PROC
49 time_t nfsd4_lease = 90; /* default lease time */
50 time_t nfsd4_grace = 90;
51 static time_t boot_time;
52 static u32 current_ownerid = 1;
53 static u32 current_fileid = 1;
54 static u32 current_delegid = 1;
55 static stateid_t zerostateid; /* bits all 0 */
56 static stateid_t onestateid; /* bits all 1 */
57 static u64 current_sessionid = 1;
59 #define ZERO_STATEID(stateid) (!memcmp((stateid), &zerostateid, sizeof(stateid_t)))
60 #define ONE_STATEID(stateid) (!memcmp((stateid), &onestateid, sizeof(stateid_t)))
62 /* forward declarations */
63 static struct nfs4_delegation * search_for_delegation(stateid_t *stid);
64 static struct nfs4_delegation * find_delegation_stateid(struct inode *ino, stateid_t *stid);
65 static int check_for_locks(struct nfs4_file *filp, struct nfs4_lockowner *lowner);
69 /* Currently used for almost all code touching nfsv4 state: */
70 static DEFINE_MUTEX(client_mutex);
73 * Currently used for the del_recall_lru and file hash table. In an
74 * effort to decrease the scope of the client_mutex, this spinlock may
75 * eventually cover more:
77 static DEFINE_SPINLOCK(recall_lock);
79 static struct kmem_cache *openowner_slab = NULL;
80 static struct kmem_cache *lockowner_slab = NULL;
81 static struct kmem_cache *file_slab = NULL;
82 static struct kmem_cache *stateid_slab = NULL;
83 static struct kmem_cache *deleg_slab = NULL;
88 mutex_lock(&client_mutex);
92 nfs4_unlock_state(void)
94 mutex_unlock(&client_mutex);
98 opaque_hashval(const void *ptr, int nbytes)
100 unsigned char *cptr = (unsigned char *) ptr;
110 static struct list_head del_recall_lru;
113 put_nfs4_file(struct nfs4_file *fi)
115 if (atomic_dec_and_lock(&fi->fi_ref, &recall_lock)) {
116 list_del(&fi->fi_hash);
117 spin_unlock(&recall_lock);
119 kmem_cache_free(file_slab, fi);
124 get_nfs4_file(struct nfs4_file *fi)
126 atomic_inc(&fi->fi_ref);
129 static int num_delegations;
130 unsigned int max_delegations;
133 * Open owner state (share locks)
136 /* hash tables for open owners */
137 #define OPEN_OWNER_HASH_BITS 8
138 #define OPEN_OWNER_HASH_SIZE (1 << OPEN_OWNER_HASH_BITS)
139 #define OPEN_OWNER_HASH_MASK (OPEN_OWNER_HASH_SIZE - 1)
141 static unsigned int open_ownerid_hashval(const u32 id)
143 return id & OPEN_OWNER_HASH_MASK;
146 static unsigned int open_ownerstr_hashval(u32 clientid, struct xdr_netobj *ownername)
150 ret = opaque_hashval(ownername->data, ownername->len);
152 return ret & OPEN_OWNER_HASH_MASK;
155 static struct list_head open_ownerid_hashtbl[OPEN_OWNER_HASH_SIZE];
156 static struct list_head open_ownerstr_hashtbl[OPEN_OWNER_HASH_SIZE];
158 /* hash table for nfs4_file */
159 #define FILE_HASH_BITS 8
160 #define FILE_HASH_SIZE (1 << FILE_HASH_BITS)
162 /* hash table for (open)nfs4_stateid */
163 #define STATEID_HASH_BITS 10
164 #define STATEID_HASH_SIZE (1 << STATEID_HASH_BITS)
165 #define STATEID_HASH_MASK (STATEID_HASH_SIZE - 1)
167 static unsigned int file_hashval(struct inode *ino)
169 /* XXX: why are we hashing on inode pointer, anyway? */
170 return hash_ptr(ino, FILE_HASH_BITS);
173 static unsigned int stateid_hashval(u32 owner_id, u32 file_id)
175 return (owner_id + file_id) & STATEID_HASH_MASK;
178 static struct list_head file_hashtbl[FILE_HASH_SIZE];
179 static struct list_head stateid_hashtbl[STATEID_HASH_SIZE];
181 static void __nfs4_file_get_access(struct nfs4_file *fp, int oflag)
183 BUG_ON(!(fp->fi_fds[oflag] || fp->fi_fds[O_RDWR]));
184 atomic_inc(&fp->fi_access[oflag]);
187 static void nfs4_file_get_access(struct nfs4_file *fp, int oflag)
189 if (oflag == O_RDWR) {
190 __nfs4_file_get_access(fp, O_RDONLY);
191 __nfs4_file_get_access(fp, O_WRONLY);
193 __nfs4_file_get_access(fp, oflag);
196 static void nfs4_file_put_fd(struct nfs4_file *fp, int oflag)
198 if (fp->fi_fds[oflag]) {
199 fput(fp->fi_fds[oflag]);
200 fp->fi_fds[oflag] = NULL;
204 static void __nfs4_file_put_access(struct nfs4_file *fp, int oflag)
206 if (atomic_dec_and_test(&fp->fi_access[oflag])) {
207 nfs4_file_put_fd(fp, O_RDWR);
208 nfs4_file_put_fd(fp, oflag);
212 static void nfs4_file_put_access(struct nfs4_file *fp, int oflag)
214 if (oflag == O_RDWR) {
215 __nfs4_file_put_access(fp, O_RDONLY);
216 __nfs4_file_put_access(fp, O_WRONLY);
218 __nfs4_file_put_access(fp, oflag);
221 static struct nfs4_delegation *
222 alloc_init_deleg(struct nfs4_client *clp, struct nfs4_stateid *stp, struct svc_fh *current_fh, u32 type)
224 struct nfs4_delegation *dp;
225 struct nfs4_file *fp = stp->st_file;
227 dprintk("NFSD alloc_init_deleg\n");
229 * Major work on the lease subsystem (for example, to support
230 * calbacks on stat) will be required before we can support
231 * write delegations properly.
233 if (type != NFS4_OPEN_DELEGATE_READ)
235 if (fp->fi_had_conflict)
237 if (num_delegations > max_delegations)
239 dp = kmem_cache_alloc(deleg_slab, GFP_KERNEL);
243 INIT_LIST_HEAD(&dp->dl_perfile);
244 INIT_LIST_HEAD(&dp->dl_perclnt);
245 INIT_LIST_HEAD(&dp->dl_recall_lru);
250 dp->dl_stateid.si_boot = boot_time;
251 dp->dl_stateid.si_stateownerid = current_delegid++;
252 dp->dl_stateid.si_fileid = 0;
253 dp->dl_stateid.si_generation = 1;
254 fh_copy_shallow(&dp->dl_fh, ¤t_fh->fh_handle);
256 atomic_set(&dp->dl_count, 1);
257 INIT_WORK(&dp->dl_recall.cb_work, nfsd4_do_callback_rpc);
262 nfs4_put_delegation(struct nfs4_delegation *dp)
264 if (atomic_dec_and_test(&dp->dl_count)) {
265 dprintk("NFSD: freeing dp %p\n",dp);
266 put_nfs4_file(dp->dl_file);
267 kmem_cache_free(deleg_slab, dp);
272 static void nfs4_put_deleg_lease(struct nfs4_file *fp)
274 if (atomic_dec_and_test(&fp->fi_delegees)) {
275 vfs_setlease(fp->fi_deleg_file, F_UNLCK, &fp->fi_lease);
277 fput(fp->fi_deleg_file);
278 fp->fi_deleg_file = NULL;
282 /* Called under the state lock. */
284 unhash_delegation(struct nfs4_delegation *dp)
286 list_del_init(&dp->dl_perclnt);
287 spin_lock(&recall_lock);
288 list_del_init(&dp->dl_perfile);
289 list_del_init(&dp->dl_recall_lru);
290 spin_unlock(&recall_lock);
291 nfs4_put_deleg_lease(dp->dl_file);
292 nfs4_put_delegation(dp);
299 /* client_lock protects the client lru list and session hash table */
300 static DEFINE_SPINLOCK(client_lock);
302 /* Hash tables for nfs4_clientid state */
303 #define CLIENT_HASH_BITS 4
304 #define CLIENT_HASH_SIZE (1 << CLIENT_HASH_BITS)
305 #define CLIENT_HASH_MASK (CLIENT_HASH_SIZE - 1)
307 static unsigned int clientid_hashval(u32 id)
309 return id & CLIENT_HASH_MASK;
312 static unsigned int clientstr_hashval(const char *name)
314 return opaque_hashval(name, 8) & CLIENT_HASH_MASK;
318 * reclaim_str_hashtbl[] holds known client info from previous reset/reboot
319 * used in reboot/reset lease grace period processing
321 * conf_id_hashtbl[], and conf_str_hashtbl[] hold confirmed
322 * setclientid_confirmed info.
324 * unconf_str_hastbl[] and unconf_id_hashtbl[] hold unconfirmed
327 * client_lru holds client queue ordered by nfs4_client.cl_time
330 * close_lru holds (open) stateowner queue ordered by nfs4_stateowner.so_time
331 * for last close replay.
333 static struct list_head reclaim_str_hashtbl[CLIENT_HASH_SIZE];
334 static int reclaim_str_hashtbl_size = 0;
335 static struct list_head conf_id_hashtbl[CLIENT_HASH_SIZE];
336 static struct list_head conf_str_hashtbl[CLIENT_HASH_SIZE];
337 static struct list_head unconf_str_hashtbl[CLIENT_HASH_SIZE];
338 static struct list_head unconf_id_hashtbl[CLIENT_HASH_SIZE];
339 static struct list_head client_lru;
340 static struct list_head close_lru;
343 * We store the NONE, READ, WRITE, and BOTH bits separately in the
344 * st_{access,deny}_bmap field of the stateid, in order to track not
345 * only what share bits are currently in force, but also what
346 * combinations of share bits previous opens have used. This allows us
347 * to enforce the recommendation of rfc 3530 14.2.19 that the server
348 * return an error if the client attempt to downgrade to a combination
349 * of share bits not explicable by closing some of its previous opens.
351 * XXX: This enforcement is actually incomplete, since we don't keep
352 * track of access/deny bit combinations; so, e.g., we allow:
354 * OPEN allow read, deny write
355 * OPEN allow both, deny none
356 * DOWNGRADE allow read, deny none
358 * which we should reject.
361 set_access(unsigned int *access, unsigned long bmap) {
365 for (i = 1; i < 4; i++) {
366 if (test_bit(i, &bmap))
372 set_deny(unsigned int *deny, unsigned long bmap) {
376 for (i = 0; i < 4; i++) {
377 if (test_bit(i, &bmap))
383 test_share(struct nfs4_stateid *stp, struct nfsd4_open *open) {
384 unsigned int access, deny;
386 set_access(&access, stp->st_access_bmap);
387 set_deny(&deny, stp->st_deny_bmap);
388 if ((access & open->op_share_deny) || (deny & open->op_share_access))
393 static int nfs4_access_to_omode(u32 access)
395 switch (access & NFS4_SHARE_ACCESS_BOTH) {
396 case NFS4_SHARE_ACCESS_READ:
398 case NFS4_SHARE_ACCESS_WRITE:
400 case NFS4_SHARE_ACCESS_BOTH:
406 static void unhash_generic_stateid(struct nfs4_stateid *stp)
408 list_del(&stp->st_hash);
409 list_del(&stp->st_perfile);
410 list_del(&stp->st_perstateowner);
413 static void close_generic_stateid(struct nfs4_stateid *stp)
417 if (stp->st_access_bmap) {
418 for (i = 1; i < 4; i++) {
419 if (test_bit(i, &stp->st_access_bmap))
420 nfs4_file_put_access(stp->st_file,
421 nfs4_access_to_omode(i));
422 __clear_bit(i, &stp->st_access_bmap);
425 put_nfs4_file(stp->st_file);
429 static void free_generic_stateid(struct nfs4_stateid *stp)
431 close_generic_stateid(stp);
432 kmem_cache_free(stateid_slab, stp);
435 static void release_lock_stateid(struct nfs4_stateid *stp)
439 unhash_generic_stateid(stp);
440 file = find_any_file(stp->st_file);
442 locks_remove_posix(file, (fl_owner_t)lockowner(stp->st_stateowner));
443 free_generic_stateid(stp);
446 static void unhash_lockowner(struct nfs4_lockowner *lo)
448 struct nfs4_stateid *stp;
450 list_del(&lo->lo_owner.so_idhash);
451 list_del(&lo->lo_owner.so_strhash);
452 list_del(&lo->lo_perstateid);
453 while (!list_empty(&lo->lo_owner.so_stateids)) {
454 stp = list_first_entry(&lo->lo_owner.so_stateids,
455 struct nfs4_stateid, st_perstateowner);
456 release_lock_stateid(stp);
460 static void release_lockowner(struct nfs4_lockowner *lo)
462 unhash_lockowner(lo);
463 nfs4_free_lockowner(lo);
467 release_stateid_lockowners(struct nfs4_stateid *open_stp)
469 struct nfs4_lockowner *lo;
471 while (!list_empty(&open_stp->st_lockowners)) {
472 lo = list_entry(open_stp->st_lockowners.next,
473 struct nfs4_lockowner, lo_perstateid);
474 release_lockowner(lo);
478 static void release_open_stateid(struct nfs4_stateid *stp)
480 unhash_generic_stateid(stp);
481 release_stateid_lockowners(stp);
482 free_generic_stateid(stp);
485 static void unhash_openowner(struct nfs4_openowner *oo)
487 struct nfs4_stateid *stp;
489 list_del(&oo->oo_owner.so_idhash);
490 list_del(&oo->oo_owner.so_strhash);
491 list_del(&oo->oo_perclient);
492 while (!list_empty(&oo->oo_owner.so_stateids)) {
493 stp = list_first_entry(&oo->oo_owner.so_stateids,
494 struct nfs4_stateid, st_perstateowner);
495 release_open_stateid(stp);
499 static void release_openowner(struct nfs4_openowner *oo)
501 unhash_openowner(oo);
502 list_del(&oo->oo_close_lru);
503 nfs4_free_openowner(oo);
506 #define SESSION_HASH_SIZE 512
507 static struct list_head sessionid_hashtbl[SESSION_HASH_SIZE];
510 hash_sessionid(struct nfs4_sessionid *sessionid)
512 struct nfsd4_sessionid *sid = (struct nfsd4_sessionid *)sessionid;
514 return sid->sequence % SESSION_HASH_SIZE;
518 dump_sessionid(const char *fn, struct nfs4_sessionid *sessionid)
520 u32 *ptr = (u32 *)(&sessionid->data[0]);
521 dprintk("%s: %u:%u:%u:%u\n", fn, ptr[0], ptr[1], ptr[2], ptr[3]);
525 gen_sessionid(struct nfsd4_session *ses)
527 struct nfs4_client *clp = ses->se_client;
528 struct nfsd4_sessionid *sid;
530 sid = (struct nfsd4_sessionid *)ses->se_sessionid.data;
531 sid->clientid = clp->cl_clientid;
532 sid->sequence = current_sessionid++;
537 * The protocol defines ca_maxresponssize_cached to include the size of
538 * the rpc header, but all we need to cache is the data starting after
539 * the end of the initial SEQUENCE operation--the rest we regenerate
540 * each time. Therefore we can advertise a ca_maxresponssize_cached
541 * value that is the number of bytes in our cache plus a few additional
542 * bytes. In order to stay on the safe side, and not promise more than
543 * we can cache, those additional bytes must be the minimum possible: 24
544 * bytes of rpc header (xid through accept state, with AUTH_NULL
545 * verifier), 12 for the compound header (with zero-length tag), and 44
546 * for the SEQUENCE op response:
548 #define NFSD_MIN_HDR_SEQ_SZ (24 + 12 + 44)
551 free_session_slots(struct nfsd4_session *ses)
555 for (i = 0; i < ses->se_fchannel.maxreqs; i++)
556 kfree(ses->se_slots[i]);
560 * We don't actually need to cache the rpc and session headers, so we
561 * can allocate a little less for each slot:
563 static inline int slot_bytes(struct nfsd4_channel_attrs *ca)
565 return ca->maxresp_cached - NFSD_MIN_HDR_SEQ_SZ;
568 static int nfsd4_sanitize_slot_size(u32 size)
570 size -= NFSD_MIN_HDR_SEQ_SZ; /* We don't cache the rpc header */
571 size = min_t(u32, size, NFSD_SLOT_CACHE_SIZE);
577 * XXX: If we run out of reserved DRC memory we could (up to a point)
578 * re-negotiate active sessions and reduce their slot usage to make
579 * rooom for new connections. For now we just fail the create session.
581 static int nfsd4_get_drc_mem(int slotsize, u32 num)
585 num = min_t(u32, num, NFSD_MAX_SLOTS_PER_SESSION);
587 spin_lock(&nfsd_drc_lock);
588 avail = min_t(int, NFSD_MAX_MEM_PER_SESSION,
589 nfsd_drc_max_mem - nfsd_drc_mem_used);
590 num = min_t(int, num, avail / slotsize);
591 nfsd_drc_mem_used += num * slotsize;
592 spin_unlock(&nfsd_drc_lock);
597 static void nfsd4_put_drc_mem(int slotsize, int num)
599 spin_lock(&nfsd_drc_lock);
600 nfsd_drc_mem_used -= slotsize * num;
601 spin_unlock(&nfsd_drc_lock);
604 static struct nfsd4_session *alloc_session(int slotsize, int numslots)
606 struct nfsd4_session *new;
609 BUILD_BUG_ON(NFSD_MAX_SLOTS_PER_SESSION * sizeof(struct nfsd4_slot *)
610 + sizeof(struct nfsd4_session) > PAGE_SIZE);
611 mem = numslots * sizeof(struct nfsd4_slot *);
613 new = kzalloc(sizeof(*new) + mem, GFP_KERNEL);
616 /* allocate each struct nfsd4_slot and data cache in one piece */
617 for (i = 0; i < numslots; i++) {
618 mem = sizeof(struct nfsd4_slot) + slotsize;
619 new->se_slots[i] = kzalloc(mem, GFP_KERNEL);
620 if (!new->se_slots[i])
626 kfree(new->se_slots[i]);
631 static void init_forechannel_attrs(struct nfsd4_channel_attrs *new, struct nfsd4_channel_attrs *req, int numslots, int slotsize)
633 u32 maxrpc = nfsd_serv->sv_max_mesg;
635 new->maxreqs = numslots;
636 new->maxresp_cached = min_t(u32, req->maxresp_cached,
637 slotsize + NFSD_MIN_HDR_SEQ_SZ);
638 new->maxreq_sz = min_t(u32, req->maxreq_sz, maxrpc);
639 new->maxresp_sz = min_t(u32, req->maxresp_sz, maxrpc);
640 new->maxops = min_t(u32, req->maxops, NFSD_MAX_OPS_PER_COMPOUND);
643 static void free_conn(struct nfsd4_conn *c)
645 svc_xprt_put(c->cn_xprt);
649 static void nfsd4_conn_lost(struct svc_xpt_user *u)
651 struct nfsd4_conn *c = container_of(u, struct nfsd4_conn, cn_xpt_user);
652 struct nfs4_client *clp = c->cn_session->se_client;
654 spin_lock(&clp->cl_lock);
655 if (!list_empty(&c->cn_persession)) {
656 list_del(&c->cn_persession);
659 spin_unlock(&clp->cl_lock);
660 nfsd4_probe_callback(clp);
663 static struct nfsd4_conn *alloc_conn(struct svc_rqst *rqstp, u32 flags)
665 struct nfsd4_conn *conn;
667 conn = kmalloc(sizeof(struct nfsd4_conn), GFP_KERNEL);
670 svc_xprt_get(rqstp->rq_xprt);
671 conn->cn_xprt = rqstp->rq_xprt;
672 conn->cn_flags = flags;
673 INIT_LIST_HEAD(&conn->cn_xpt_user.list);
677 static void __nfsd4_hash_conn(struct nfsd4_conn *conn, struct nfsd4_session *ses)
679 conn->cn_session = ses;
680 list_add(&conn->cn_persession, &ses->se_conns);
683 static void nfsd4_hash_conn(struct nfsd4_conn *conn, struct nfsd4_session *ses)
685 struct nfs4_client *clp = ses->se_client;
687 spin_lock(&clp->cl_lock);
688 __nfsd4_hash_conn(conn, ses);
689 spin_unlock(&clp->cl_lock);
692 static int nfsd4_register_conn(struct nfsd4_conn *conn)
694 conn->cn_xpt_user.callback = nfsd4_conn_lost;
695 return register_xpt_user(conn->cn_xprt, &conn->cn_xpt_user);
698 static __be32 nfsd4_new_conn(struct svc_rqst *rqstp, struct nfsd4_session *ses, u32 dir)
700 struct nfsd4_conn *conn;
703 conn = alloc_conn(rqstp, dir);
705 return nfserr_jukebox;
706 nfsd4_hash_conn(conn, ses);
707 ret = nfsd4_register_conn(conn);
709 /* oops; xprt is already down: */
710 nfsd4_conn_lost(&conn->cn_xpt_user);
714 static __be32 nfsd4_new_conn_from_crses(struct svc_rqst *rqstp, struct nfsd4_session *ses)
716 u32 dir = NFS4_CDFC4_FORE;
718 if (ses->se_flags & SESSION4_BACK_CHAN)
719 dir |= NFS4_CDFC4_BACK;
721 return nfsd4_new_conn(rqstp, ses, dir);
724 /* must be called under client_lock */
725 static void nfsd4_del_conns(struct nfsd4_session *s)
727 struct nfs4_client *clp = s->se_client;
728 struct nfsd4_conn *c;
730 spin_lock(&clp->cl_lock);
731 while (!list_empty(&s->se_conns)) {
732 c = list_first_entry(&s->se_conns, struct nfsd4_conn, cn_persession);
733 list_del_init(&c->cn_persession);
734 spin_unlock(&clp->cl_lock);
736 unregister_xpt_user(c->cn_xprt, &c->cn_xpt_user);
739 spin_lock(&clp->cl_lock);
741 spin_unlock(&clp->cl_lock);
744 void free_session(struct kref *kref)
746 struct nfsd4_session *ses;
749 ses = container_of(kref, struct nfsd4_session, se_ref);
750 nfsd4_del_conns(ses);
751 spin_lock(&nfsd_drc_lock);
752 mem = ses->se_fchannel.maxreqs * slot_bytes(&ses->se_fchannel);
753 nfsd_drc_mem_used -= mem;
754 spin_unlock(&nfsd_drc_lock);
755 free_session_slots(ses);
759 static struct nfsd4_session *alloc_init_session(struct svc_rqst *rqstp, struct nfs4_client *clp, struct nfsd4_create_session *cses)
761 struct nfsd4_session *new;
762 struct nfsd4_channel_attrs *fchan = &cses->fore_channel;
763 int numslots, slotsize;
768 * Note decreasing slot size below client's request may
769 * make it difficult for client to function correctly, whereas
770 * decreasing the number of slots will (just?) affect
771 * performance. When short on memory we therefore prefer to
772 * decrease number of slots instead of their size.
774 slotsize = nfsd4_sanitize_slot_size(fchan->maxresp_cached);
775 numslots = nfsd4_get_drc_mem(slotsize, fchan->maxreqs);
779 new = alloc_session(slotsize, numslots);
781 nfsd4_put_drc_mem(slotsize, fchan->maxreqs);
784 init_forechannel_attrs(&new->se_fchannel, fchan, numslots, slotsize);
786 new->se_client = clp;
789 INIT_LIST_HEAD(&new->se_conns);
791 new->se_cb_seq_nr = 1;
792 new->se_flags = cses->flags;
793 new->se_cb_prog = cses->callback_prog;
794 kref_init(&new->se_ref);
795 idx = hash_sessionid(&new->se_sessionid);
796 spin_lock(&client_lock);
797 list_add(&new->se_hash, &sessionid_hashtbl[idx]);
798 spin_lock(&clp->cl_lock);
799 list_add(&new->se_perclnt, &clp->cl_sessions);
800 spin_unlock(&clp->cl_lock);
801 spin_unlock(&client_lock);
803 status = nfsd4_new_conn_from_crses(rqstp, new);
804 /* whoops: benny points out, status is ignored! (err, or bogus) */
806 free_session(&new->se_ref);
809 if (cses->flags & SESSION4_BACK_CHAN) {
810 struct sockaddr *sa = svc_addr(rqstp);
812 * This is a little silly; with sessions there's no real
813 * use for the callback address. Use the peer address
814 * as a reasonable default for now, but consider fixing
815 * the rpc client not to require an address in the
818 rpc_copy_addr((struct sockaddr *)&clp->cl_cb_conn.cb_addr, sa);
819 clp->cl_cb_conn.cb_addrlen = svc_addr_len(sa);
821 nfsd4_probe_callback(clp);
825 /* caller must hold client_lock */
826 static struct nfsd4_session *
827 find_in_sessionid_hashtbl(struct nfs4_sessionid *sessionid)
829 struct nfsd4_session *elem;
832 dump_sessionid(__func__, sessionid);
833 idx = hash_sessionid(sessionid);
834 /* Search in the appropriate list */
835 list_for_each_entry(elem, &sessionid_hashtbl[idx], se_hash) {
836 if (!memcmp(elem->se_sessionid.data, sessionid->data,
837 NFS4_MAX_SESSIONID_LEN)) {
842 dprintk("%s: session not found\n", __func__);
846 /* caller must hold client_lock */
848 unhash_session(struct nfsd4_session *ses)
850 list_del(&ses->se_hash);
851 spin_lock(&ses->se_client->cl_lock);
852 list_del(&ses->se_perclnt);
853 spin_unlock(&ses->se_client->cl_lock);
856 /* must be called under the client_lock */
858 renew_client_locked(struct nfs4_client *clp)
860 if (is_client_expired(clp)) {
861 dprintk("%s: client (clientid %08x/%08x) already expired\n",
863 clp->cl_clientid.cl_boot,
864 clp->cl_clientid.cl_id);
869 * Move client to the end to the LRU list.
871 dprintk("renewing client (clientid %08x/%08x)\n",
872 clp->cl_clientid.cl_boot,
873 clp->cl_clientid.cl_id);
874 list_move_tail(&clp->cl_lru, &client_lru);
875 clp->cl_time = get_seconds();
879 renew_client(struct nfs4_client *clp)
881 spin_lock(&client_lock);
882 renew_client_locked(clp);
883 spin_unlock(&client_lock);
886 /* SETCLIENTID and SETCLIENTID_CONFIRM Helper functions */
888 STALE_CLIENTID(clientid_t *clid)
890 if (clid->cl_boot == boot_time)
892 dprintk("NFSD stale clientid (%08x/%08x) boot_time %08lx\n",
893 clid->cl_boot, clid->cl_id, boot_time);
898 * XXX Should we use a slab cache ?
899 * This type of memory management is somewhat inefficient, but we use it
900 * anyway since SETCLIENTID is not a common operation.
902 static struct nfs4_client *alloc_client(struct xdr_netobj name)
904 struct nfs4_client *clp;
906 clp = kzalloc(sizeof(struct nfs4_client), GFP_KERNEL);
909 clp->cl_name.data = kmalloc(name.len, GFP_KERNEL);
910 if (clp->cl_name.data == NULL) {
914 memcpy(clp->cl_name.data, name.data, name.len);
915 clp->cl_name.len = name.len;
920 free_client(struct nfs4_client *clp)
922 while (!list_empty(&clp->cl_sessions)) {
923 struct nfsd4_session *ses;
924 ses = list_entry(clp->cl_sessions.next, struct nfsd4_session,
926 list_del(&ses->se_perclnt);
927 nfsd4_put_session(ses);
929 if (clp->cl_cred.cr_group_info)
930 put_group_info(clp->cl_cred.cr_group_info);
931 kfree(clp->cl_principal);
932 kfree(clp->cl_name.data);
937 release_session_client(struct nfsd4_session *session)
939 struct nfs4_client *clp = session->se_client;
941 if (!atomic_dec_and_lock(&clp->cl_refcount, &client_lock))
943 if (is_client_expired(clp)) {
945 session->se_client = NULL;
947 renew_client_locked(clp);
948 spin_unlock(&client_lock);
951 /* must be called under the client_lock */
953 unhash_client_locked(struct nfs4_client *clp)
955 struct nfsd4_session *ses;
957 mark_client_expired(clp);
958 list_del(&clp->cl_lru);
959 spin_lock(&clp->cl_lock);
960 list_for_each_entry(ses, &clp->cl_sessions, se_perclnt)
961 list_del_init(&ses->se_hash);
962 spin_unlock(&clp->cl_lock);
966 expire_client(struct nfs4_client *clp)
968 struct nfs4_openowner *oo;
969 struct nfs4_delegation *dp;
970 struct list_head reaplist;
972 INIT_LIST_HEAD(&reaplist);
973 spin_lock(&recall_lock);
974 while (!list_empty(&clp->cl_delegations)) {
975 dp = list_entry(clp->cl_delegations.next, struct nfs4_delegation, dl_perclnt);
976 list_del_init(&dp->dl_perclnt);
977 list_move(&dp->dl_recall_lru, &reaplist);
979 spin_unlock(&recall_lock);
980 while (!list_empty(&reaplist)) {
981 dp = list_entry(reaplist.next, struct nfs4_delegation, dl_recall_lru);
982 list_del_init(&dp->dl_recall_lru);
983 unhash_delegation(dp);
985 while (!list_empty(&clp->cl_openowners)) {
986 oo = list_entry(clp->cl_openowners.next, struct nfs4_openowner, oo_perclient);
987 release_openowner(oo);
989 nfsd4_shutdown_callback(clp);
990 if (clp->cl_cb_conn.cb_xprt)
991 svc_xprt_put(clp->cl_cb_conn.cb_xprt);
992 list_del(&clp->cl_idhash);
993 list_del(&clp->cl_strhash);
994 spin_lock(&client_lock);
995 unhash_client_locked(clp);
996 if (atomic_read(&clp->cl_refcount) == 0)
998 spin_unlock(&client_lock);
1001 static void copy_verf(struct nfs4_client *target, nfs4_verifier *source)
1003 memcpy(target->cl_verifier.data, source->data,
1004 sizeof(target->cl_verifier.data));
1007 static void copy_clid(struct nfs4_client *target, struct nfs4_client *source)
1009 target->cl_clientid.cl_boot = source->cl_clientid.cl_boot;
1010 target->cl_clientid.cl_id = source->cl_clientid.cl_id;
1013 static void copy_cred(struct svc_cred *target, struct svc_cred *source)
1015 target->cr_uid = source->cr_uid;
1016 target->cr_gid = source->cr_gid;
1017 target->cr_group_info = source->cr_group_info;
1018 get_group_info(target->cr_group_info);
1021 static int same_name(const char *n1, const char *n2)
1023 return 0 == memcmp(n1, n2, HEXDIR_LEN);
1027 same_verf(nfs4_verifier *v1, nfs4_verifier *v2)
1029 return 0 == memcmp(v1->data, v2->data, sizeof(v1->data));
1033 same_clid(clientid_t *cl1, clientid_t *cl2)
1035 return (cl1->cl_boot == cl2->cl_boot) && (cl1->cl_id == cl2->cl_id);
1038 /* XXX what about NGROUP */
1040 same_creds(struct svc_cred *cr1, struct svc_cred *cr2)
1042 return cr1->cr_uid == cr2->cr_uid;
1045 static void gen_clid(struct nfs4_client *clp)
1047 static u32 current_clientid = 1;
1049 clp->cl_clientid.cl_boot = boot_time;
1050 clp->cl_clientid.cl_id = current_clientid++;
1053 static void gen_confirm(struct nfs4_client *clp)
1058 p = (u32 *)clp->cl_confirm.data;
1059 *p++ = get_seconds();
1064 same_stateid(stateid_t *id_one, stateid_t *id_two)
1066 if (id_one->si_stateownerid != id_two->si_stateownerid)
1068 return id_one->si_fileid == id_two->si_fileid;
1071 static struct nfs4_stateid *find_stateid(stateid_t *t)
1073 struct nfs4_stateid *s;
1074 unsigned int hashval;
1076 hashval = stateid_hashval(t->si_stateownerid, t->si_fileid);
1077 list_for_each_entry(s, &stateid_hashtbl[hashval], st_hash)
1078 if (same_stateid(&s->st_stateid, t))
1083 static struct nfs4_stateid *find_stateid_by_type(stateid_t *t, char typemask)
1085 struct nfs4_stateid *s;
1087 s = find_stateid(t);
1090 if (typemask & s->st_type)
1095 static struct nfs4_client *create_client(struct xdr_netobj name, char *recdir,
1096 struct svc_rqst *rqstp, nfs4_verifier *verf)
1098 struct nfs4_client *clp;
1099 struct sockaddr *sa = svc_addr(rqstp);
1102 clp = alloc_client(name);
1106 INIT_LIST_HEAD(&clp->cl_sessions);
1108 princ = svc_gss_principal(rqstp);
1110 clp->cl_principal = kstrdup(princ, GFP_KERNEL);
1111 if (clp->cl_principal == NULL) {
1117 memcpy(clp->cl_recdir, recdir, HEXDIR_LEN);
1118 atomic_set(&clp->cl_refcount, 0);
1119 clp->cl_cb_state = NFSD4_CB_UNKNOWN;
1120 INIT_LIST_HEAD(&clp->cl_idhash);
1121 INIT_LIST_HEAD(&clp->cl_strhash);
1122 INIT_LIST_HEAD(&clp->cl_openowners);
1123 INIT_LIST_HEAD(&clp->cl_delegations);
1124 INIT_LIST_HEAD(&clp->cl_lru);
1125 INIT_LIST_HEAD(&clp->cl_callbacks);
1126 spin_lock_init(&clp->cl_lock);
1127 INIT_WORK(&clp->cl_cb_null.cb_work, nfsd4_do_callback_rpc);
1128 clp->cl_time = get_seconds();
1129 clear_bit(0, &clp->cl_cb_slot_busy);
1130 rpc_init_wait_queue(&clp->cl_cb_waitq, "Backchannel slot table");
1131 copy_verf(clp, verf);
1132 rpc_copy_addr((struct sockaddr *) &clp->cl_addr, sa);
1133 clp->cl_flavor = rqstp->rq_flavor;
1134 copy_cred(&clp->cl_cred, &rqstp->rq_cred);
1136 clp->cl_cb_session = NULL;
1140 static int check_name(struct xdr_netobj name)
1144 if (name.len > NFS4_OPAQUE_LIMIT) {
1145 dprintk("NFSD: check_name: name too long(%d)!\n", name.len);
1152 add_to_unconfirmed(struct nfs4_client *clp, unsigned int strhashval)
1154 unsigned int idhashval;
1156 list_add(&clp->cl_strhash, &unconf_str_hashtbl[strhashval]);
1157 idhashval = clientid_hashval(clp->cl_clientid.cl_id);
1158 list_add(&clp->cl_idhash, &unconf_id_hashtbl[idhashval]);
1163 move_to_confirmed(struct nfs4_client *clp)
1165 unsigned int idhashval = clientid_hashval(clp->cl_clientid.cl_id);
1166 unsigned int strhashval;
1168 dprintk("NFSD: move_to_confirm nfs4_client %p\n", clp);
1169 list_move(&clp->cl_idhash, &conf_id_hashtbl[idhashval]);
1170 strhashval = clientstr_hashval(clp->cl_recdir);
1171 list_move(&clp->cl_strhash, &conf_str_hashtbl[strhashval]);
1175 static struct nfs4_client *
1176 find_confirmed_client(clientid_t *clid)
1178 struct nfs4_client *clp;
1179 unsigned int idhashval = clientid_hashval(clid->cl_id);
1181 list_for_each_entry(clp, &conf_id_hashtbl[idhashval], cl_idhash) {
1182 if (same_clid(&clp->cl_clientid, clid))
1188 static struct nfs4_client *
1189 find_unconfirmed_client(clientid_t *clid)
1191 struct nfs4_client *clp;
1192 unsigned int idhashval = clientid_hashval(clid->cl_id);
1194 list_for_each_entry(clp, &unconf_id_hashtbl[idhashval], cl_idhash) {
1195 if (same_clid(&clp->cl_clientid, clid))
1201 static bool clp_used_exchangeid(struct nfs4_client *clp)
1203 return clp->cl_exchange_flags != 0;
1206 static struct nfs4_client *
1207 find_confirmed_client_by_str(const char *dname, unsigned int hashval)
1209 struct nfs4_client *clp;
1211 list_for_each_entry(clp, &conf_str_hashtbl[hashval], cl_strhash) {
1212 if (same_name(clp->cl_recdir, dname))
1218 static struct nfs4_client *
1219 find_unconfirmed_client_by_str(const char *dname, unsigned int hashval)
1221 struct nfs4_client *clp;
1223 list_for_each_entry(clp, &unconf_str_hashtbl[hashval], cl_strhash) {
1224 if (same_name(clp->cl_recdir, dname))
1230 static void rpc_svcaddr2sockaddr(struct sockaddr *sa, unsigned short family, union svc_addr_u *svcaddr)
1234 ((struct sockaddr_in *)sa)->sin_family = AF_INET;
1235 ((struct sockaddr_in *)sa)->sin_addr = svcaddr->addr;
1238 ((struct sockaddr_in6 *)sa)->sin6_family = AF_INET6;
1239 ((struct sockaddr_in6 *)sa)->sin6_addr = svcaddr->addr6;
1245 gen_callback(struct nfs4_client *clp, struct nfsd4_setclientid *se, struct svc_rqst *rqstp)
1247 struct nfs4_cb_conn *conn = &clp->cl_cb_conn;
1248 struct sockaddr *sa = svc_addr(rqstp);
1249 u32 scopeid = rpc_get_scope_id(sa);
1250 unsigned short expected_family;
1252 /* Currently, we only support tcp and tcp6 for the callback channel */
1253 if (se->se_callback_netid_len == 3 &&
1254 !memcmp(se->se_callback_netid_val, "tcp", 3))
1255 expected_family = AF_INET;
1256 else if (se->se_callback_netid_len == 4 &&
1257 !memcmp(se->se_callback_netid_val, "tcp6", 4))
1258 expected_family = AF_INET6;
1262 conn->cb_addrlen = rpc_uaddr2sockaddr(se->se_callback_addr_val,
1263 se->se_callback_addr_len,
1264 (struct sockaddr *)&conn->cb_addr,
1265 sizeof(conn->cb_addr));
1267 if (!conn->cb_addrlen || conn->cb_addr.ss_family != expected_family)
1270 if (conn->cb_addr.ss_family == AF_INET6)
1271 ((struct sockaddr_in6 *)&conn->cb_addr)->sin6_scope_id = scopeid;
1273 conn->cb_prog = se->se_callback_prog;
1274 conn->cb_ident = se->se_callback_ident;
1275 rpc_svcaddr2sockaddr((struct sockaddr *)&conn->cb_saddr, expected_family, &rqstp->rq_daddr);
1278 conn->cb_addr.ss_family = AF_UNSPEC;
1279 conn->cb_addrlen = 0;
1280 dprintk(KERN_INFO "NFSD: this client (clientid %08x/%08x) "
1281 "will not receive delegations\n",
1282 clp->cl_clientid.cl_boot, clp->cl_clientid.cl_id);
1288 * Cache a reply. nfsd4_check_drc_limit() has bounded the cache size.
1291 nfsd4_store_cache_entry(struct nfsd4_compoundres *resp)
1293 struct nfsd4_slot *slot = resp->cstate.slot;
1296 dprintk("--> %s slot %p\n", __func__, slot);
1298 slot->sl_opcnt = resp->opcnt;
1299 slot->sl_status = resp->cstate.status;
1301 if (nfsd4_not_cached(resp)) {
1302 slot->sl_datalen = 0;
1305 slot->sl_datalen = (char *)resp->p - (char *)resp->cstate.datap;
1306 base = (char *)resp->cstate.datap -
1307 (char *)resp->xbuf->head[0].iov_base;
1308 if (read_bytes_from_xdr_buf(resp->xbuf, base, slot->sl_data,
1310 WARN("%s: sessions DRC could not cache compound\n", __func__);
1315 * Encode the replay sequence operation from the slot values.
1316 * If cachethis is FALSE encode the uncached rep error on the next
1317 * operation which sets resp->p and increments resp->opcnt for
1318 * nfs4svc_encode_compoundres.
1322 nfsd4_enc_sequence_replay(struct nfsd4_compoundargs *args,
1323 struct nfsd4_compoundres *resp)
1325 struct nfsd4_op *op;
1326 struct nfsd4_slot *slot = resp->cstate.slot;
1328 dprintk("--> %s resp->opcnt %d cachethis %u \n", __func__,
1329 resp->opcnt, resp->cstate.slot->sl_cachethis);
1331 /* Encode the replayed sequence operation */
1332 op = &args->ops[resp->opcnt - 1];
1333 nfsd4_encode_operation(resp, op);
1335 /* Return nfserr_retry_uncached_rep in next operation. */
1336 if (args->opcnt > 1 && slot->sl_cachethis == 0) {
1337 op = &args->ops[resp->opcnt++];
1338 op->status = nfserr_retry_uncached_rep;
1339 nfsd4_encode_operation(resp, op);
1345 * The sequence operation is not cached because we can use the slot and
1349 nfsd4_replay_cache_entry(struct nfsd4_compoundres *resp,
1350 struct nfsd4_sequence *seq)
1352 struct nfsd4_slot *slot = resp->cstate.slot;
1355 dprintk("--> %s slot %p\n", __func__, slot);
1357 /* Either returns 0 or nfserr_retry_uncached */
1358 status = nfsd4_enc_sequence_replay(resp->rqstp->rq_argp, resp);
1359 if (status == nfserr_retry_uncached_rep)
1362 /* The sequence operation has been encoded, cstate->datap set. */
1363 memcpy(resp->cstate.datap, slot->sl_data, slot->sl_datalen);
1365 resp->opcnt = slot->sl_opcnt;
1366 resp->p = resp->cstate.datap + XDR_QUADLEN(slot->sl_datalen);
1367 status = slot->sl_status;
1373 * Set the exchange_id flags returned by the server.
1376 nfsd4_set_ex_flags(struct nfs4_client *new, struct nfsd4_exchange_id *clid)
1378 /* pNFS is not supported */
1379 new->cl_exchange_flags |= EXCHGID4_FLAG_USE_NON_PNFS;
1381 /* Referrals are supported, Migration is not. */
1382 new->cl_exchange_flags |= EXCHGID4_FLAG_SUPP_MOVED_REFER;
1384 /* set the wire flags to return to client. */
1385 clid->flags = new->cl_exchange_flags;
1389 nfsd4_exchange_id(struct svc_rqst *rqstp,
1390 struct nfsd4_compound_state *cstate,
1391 struct nfsd4_exchange_id *exid)
1393 struct nfs4_client *unconf, *conf, *new;
1395 unsigned int strhashval;
1396 char dname[HEXDIR_LEN];
1397 char addr_str[INET6_ADDRSTRLEN];
1398 nfs4_verifier verf = exid->verifier;
1399 struct sockaddr *sa = svc_addr(rqstp);
1401 rpc_ntop(sa, addr_str, sizeof(addr_str));
1402 dprintk("%s rqstp=%p exid=%p clname.len=%u clname.data=%p "
1403 "ip_addr=%s flags %x, spa_how %d\n",
1404 __func__, rqstp, exid, exid->clname.len, exid->clname.data,
1405 addr_str, exid->flags, exid->spa_how);
1407 if (!check_name(exid->clname) || (exid->flags & ~EXCHGID4_FLAG_MASK_A))
1408 return nfserr_inval;
1410 /* Currently only support SP4_NONE */
1411 switch (exid->spa_how) {
1415 return nfserr_serverfault;
1417 BUG(); /* checked by xdr code */
1419 return nfserr_serverfault; /* no excuse :-/ */
1422 status = nfs4_make_rec_clidname(dname, &exid->clname);
1427 strhashval = clientstr_hashval(dname);
1432 conf = find_confirmed_client_by_str(dname, strhashval);
1434 if (!clp_used_exchangeid(conf)) {
1435 status = nfserr_clid_inuse; /* XXX: ? */
1438 if (!same_verf(&verf, &conf->cl_verifier)) {
1439 /* 18.35.4 case 8 */
1440 if (exid->flags & EXCHGID4_FLAG_UPD_CONFIRMED_REC_A) {
1441 status = nfserr_not_same;
1444 /* Client reboot: destroy old state */
1445 expire_client(conf);
1448 if (!same_creds(&conf->cl_cred, &rqstp->rq_cred)) {
1449 /* 18.35.4 case 9 */
1450 if (exid->flags & EXCHGID4_FLAG_UPD_CONFIRMED_REC_A) {
1451 status = nfserr_perm;
1454 expire_client(conf);
1458 * Set bit when the owner id and verifier map to an already
1459 * confirmed client id (18.35.3).
1461 exid->flags |= EXCHGID4_FLAG_CONFIRMED_R;
1464 * Falling into 18.35.4 case 2, possible router replay.
1465 * Leave confirmed record intact and return same result.
1467 copy_verf(conf, &verf);
1472 /* 18.35.4 case 7 */
1473 if (exid->flags & EXCHGID4_FLAG_UPD_CONFIRMED_REC_A) {
1474 status = nfserr_noent;
1478 unconf = find_unconfirmed_client_by_str(dname, strhashval);
1481 * Possible retry or client restart. Per 18.35.4 case 4,
1482 * a new unconfirmed record should be generated regardless
1483 * of whether any properties have changed.
1485 expire_client(unconf);
1490 new = create_client(exid->clname, dname, rqstp, &verf);
1492 status = nfserr_jukebox;
1497 add_to_unconfirmed(new, strhashval);
1499 exid->clientid.cl_boot = new->cl_clientid.cl_boot;
1500 exid->clientid.cl_id = new->cl_clientid.cl_id;
1503 nfsd4_set_ex_flags(new, exid);
1505 dprintk("nfsd4_exchange_id seqid %d flags %x\n",
1506 new->cl_cs_slot.sl_seqid, new->cl_exchange_flags);
1510 nfs4_unlock_state();
1512 dprintk("nfsd4_exchange_id returns %d\n", ntohl(status));
1517 check_slot_seqid(u32 seqid, u32 slot_seqid, int slot_inuse)
1519 dprintk("%s enter. seqid %d slot_seqid %d\n", __func__, seqid,
1522 /* The slot is in use, and no response has been sent. */
1524 if (seqid == slot_seqid)
1525 return nfserr_jukebox;
1527 return nfserr_seq_misordered;
1530 if (likely(seqid == slot_seqid + 1))
1533 if (seqid == slot_seqid)
1534 return nfserr_replay_cache;
1536 if (seqid == 1 && (slot_seqid + 1) == 0)
1538 /* Misordered replay or misordered new request */
1539 return nfserr_seq_misordered;
1543 * Cache the create session result into the create session single DRC
1544 * slot cache by saving the xdr structure. sl_seqid has been set.
1545 * Do this for solo or embedded create session operations.
1548 nfsd4_cache_create_session(struct nfsd4_create_session *cr_ses,
1549 struct nfsd4_clid_slot *slot, int nfserr)
1551 slot->sl_status = nfserr;
1552 memcpy(&slot->sl_cr_ses, cr_ses, sizeof(*cr_ses));
1556 nfsd4_replay_create_session(struct nfsd4_create_session *cr_ses,
1557 struct nfsd4_clid_slot *slot)
1559 memcpy(cr_ses, &slot->sl_cr_ses, sizeof(*cr_ses));
1560 return slot->sl_status;
1563 #define NFSD_MIN_REQ_HDR_SEQ_SZ ((\
1564 2 * 2 + /* credential,verifier: AUTH_NULL, length 0 */ \
1565 1 + /* MIN tag is length with zero, only length */ \
1566 3 + /* version, opcount, opcode */ \
1567 XDR_QUADLEN(NFS4_MAX_SESSIONID_LEN) + \
1568 /* seqid, slotID, slotID, cache */ \
1569 4 ) * sizeof(__be32))
1571 #define NFSD_MIN_RESP_HDR_SEQ_SZ ((\
1572 2 + /* verifier: AUTH_NULL, length 0 */\
1574 1 + /* MIN tag is length with zero, only length */ \
1575 3 + /* opcount, opcode, opstatus*/ \
1576 XDR_QUADLEN(NFS4_MAX_SESSIONID_LEN) + \
1577 /* seqid, slotID, slotID, slotID, status */ \
1578 5 ) * sizeof(__be32))
1580 static __be32 check_forechannel_attrs(struct nfsd4_channel_attrs fchannel)
1582 return fchannel.maxreq_sz < NFSD_MIN_REQ_HDR_SEQ_SZ
1583 || fchannel.maxresp_sz < NFSD_MIN_RESP_HDR_SEQ_SZ;
1587 nfsd4_create_session(struct svc_rqst *rqstp,
1588 struct nfsd4_compound_state *cstate,
1589 struct nfsd4_create_session *cr_ses)
1591 struct sockaddr *sa = svc_addr(rqstp);
1592 struct nfs4_client *conf, *unconf;
1593 struct nfsd4_session *new;
1594 struct nfsd4_clid_slot *cs_slot = NULL;
1595 bool confirm_me = false;
1598 if (cr_ses->flags & ~SESSION4_FLAG_MASK_A)
1599 return nfserr_inval;
1602 unconf = find_unconfirmed_client(&cr_ses->clientid);
1603 conf = find_confirmed_client(&cr_ses->clientid);
1606 cs_slot = &conf->cl_cs_slot;
1607 status = check_slot_seqid(cr_ses->seqid, cs_slot->sl_seqid, 0);
1608 if (status == nfserr_replay_cache) {
1609 dprintk("Got a create_session replay! seqid= %d\n",
1611 /* Return the cached reply status */
1612 status = nfsd4_replay_create_session(cr_ses, cs_slot);
1614 } else if (cr_ses->seqid != cs_slot->sl_seqid + 1) {
1615 status = nfserr_seq_misordered;
1616 dprintk("Sequence misordered!\n");
1617 dprintk("Expected seqid= %d but got seqid= %d\n",
1618 cs_slot->sl_seqid, cr_ses->seqid);
1621 } else if (unconf) {
1622 if (!same_creds(&unconf->cl_cred, &rqstp->rq_cred) ||
1623 !rpc_cmp_addr(sa, (struct sockaddr *) &unconf->cl_addr)) {
1624 status = nfserr_clid_inuse;
1628 cs_slot = &unconf->cl_cs_slot;
1629 status = check_slot_seqid(cr_ses->seqid, cs_slot->sl_seqid, 0);
1631 /* an unconfirmed replay returns misordered */
1632 status = nfserr_seq_misordered;
1639 status = nfserr_stale_clientid;
1644 * XXX: we should probably set this at creation time, and check
1645 * for consistent minorversion use throughout:
1647 conf->cl_minorversion = 1;
1649 * We do not support RDMA or persistent sessions
1651 cr_ses->flags &= ~SESSION4_PERSIST;
1652 cr_ses->flags &= ~SESSION4_RDMA;
1654 status = nfserr_toosmall;
1655 if (check_forechannel_attrs(cr_ses->fore_channel))
1658 status = nfserr_jukebox;
1659 new = alloc_init_session(rqstp, conf, cr_ses);
1663 memcpy(cr_ses->sessionid.data, new->se_sessionid.data,
1664 NFS4_MAX_SESSIONID_LEN);
1665 memcpy(&cr_ses->fore_channel, &new->se_fchannel,
1666 sizeof(struct nfsd4_channel_attrs));
1667 cs_slot->sl_seqid++;
1668 cr_ses->seqid = cs_slot->sl_seqid;
1670 /* cache solo and embedded create sessions under the state lock */
1671 nfsd4_cache_create_session(cr_ses, cs_slot, status);
1673 move_to_confirmed(conf);
1675 nfs4_unlock_state();
1676 dprintk("%s returns %d\n", __func__, ntohl(status));
1680 static bool nfsd4_last_compound_op(struct svc_rqst *rqstp)
1682 struct nfsd4_compoundres *resp = rqstp->rq_resp;
1683 struct nfsd4_compoundargs *argp = rqstp->rq_argp;
1685 return argp->opcnt == resp->opcnt;
1688 static __be32 nfsd4_map_bcts_dir(u32 *dir)
1691 case NFS4_CDFC4_FORE:
1692 case NFS4_CDFC4_BACK:
1694 case NFS4_CDFC4_FORE_OR_BOTH:
1695 case NFS4_CDFC4_BACK_OR_BOTH:
1696 *dir = NFS4_CDFC4_BOTH;
1699 return nfserr_inval;
1702 __be32 nfsd4_bind_conn_to_session(struct svc_rqst *rqstp,
1703 struct nfsd4_compound_state *cstate,
1704 struct nfsd4_bind_conn_to_session *bcts)
1708 if (!nfsd4_last_compound_op(rqstp))
1709 return nfserr_not_only_op;
1710 spin_lock(&client_lock);
1711 cstate->session = find_in_sessionid_hashtbl(&bcts->sessionid);
1712 /* Sorta weird: we only need the refcnt'ing because new_conn acquires
1713 * client_lock iself: */
1714 if (cstate->session) {
1715 nfsd4_get_session(cstate->session);
1716 atomic_inc(&cstate->session->se_client->cl_refcount);
1718 spin_unlock(&client_lock);
1719 if (!cstate->session)
1720 return nfserr_badsession;
1722 status = nfsd4_map_bcts_dir(&bcts->dir);
1724 nfsd4_new_conn(rqstp, cstate->session, bcts->dir);
1728 static bool nfsd4_compound_in_session(struct nfsd4_session *session, struct nfs4_sessionid *sid)
1732 return !memcmp(sid, &session->se_sessionid, sizeof(*sid));
1736 nfsd4_destroy_session(struct svc_rqst *r,
1737 struct nfsd4_compound_state *cstate,
1738 struct nfsd4_destroy_session *sessionid)
1740 struct nfsd4_session *ses;
1741 u32 status = nfserr_badsession;
1744 * - The confirmed nfs4_client->cl_sessionid holds destroyed sessinid
1745 * - Should we return nfserr_back_chan_busy if waiting for
1746 * callbacks on to-be-destroyed session?
1747 * - Do we need to clear any callback info from previous session?
1750 if (nfsd4_compound_in_session(cstate->session, &sessionid->sessionid)) {
1751 if (!nfsd4_last_compound_op(r))
1752 return nfserr_not_only_op;
1754 dump_sessionid(__func__, &sessionid->sessionid);
1755 spin_lock(&client_lock);
1756 ses = find_in_sessionid_hashtbl(&sessionid->sessionid);
1758 spin_unlock(&client_lock);
1762 unhash_session(ses);
1763 spin_unlock(&client_lock);
1766 nfsd4_probe_callback_sync(ses->se_client);
1767 nfs4_unlock_state();
1769 nfsd4_del_conns(ses);
1771 nfsd4_put_session(ses);
1774 dprintk("%s returns %d\n", __func__, ntohl(status));
1778 static struct nfsd4_conn *__nfsd4_find_conn(struct svc_xprt *xpt, struct nfsd4_session *s)
1780 struct nfsd4_conn *c;
1782 list_for_each_entry(c, &s->se_conns, cn_persession) {
1783 if (c->cn_xprt == xpt) {
1790 static void nfsd4_sequence_check_conn(struct nfsd4_conn *new, struct nfsd4_session *ses)
1792 struct nfs4_client *clp = ses->se_client;
1793 struct nfsd4_conn *c;
1796 spin_lock(&clp->cl_lock);
1797 c = __nfsd4_find_conn(new->cn_xprt, ses);
1799 spin_unlock(&clp->cl_lock);
1803 __nfsd4_hash_conn(new, ses);
1804 spin_unlock(&clp->cl_lock);
1805 ret = nfsd4_register_conn(new);
1807 /* oops; xprt is already down: */
1808 nfsd4_conn_lost(&new->cn_xpt_user);
1812 static bool nfsd4_session_too_many_ops(struct svc_rqst *rqstp, struct nfsd4_session *session)
1814 struct nfsd4_compoundargs *args = rqstp->rq_argp;
1816 return args->opcnt > session->se_fchannel.maxops;
1819 static bool nfsd4_request_too_big(struct svc_rqst *rqstp,
1820 struct nfsd4_session *session)
1822 struct xdr_buf *xb = &rqstp->rq_arg;
1824 return xb->len > session->se_fchannel.maxreq_sz;
1828 nfsd4_sequence(struct svc_rqst *rqstp,
1829 struct nfsd4_compound_state *cstate,
1830 struct nfsd4_sequence *seq)
1832 struct nfsd4_compoundres *resp = rqstp->rq_resp;
1833 struct nfsd4_session *session;
1834 struct nfsd4_slot *slot;
1835 struct nfsd4_conn *conn;
1838 if (resp->opcnt != 1)
1839 return nfserr_sequence_pos;
1842 * Will be either used or freed by nfsd4_sequence_check_conn
1845 conn = alloc_conn(rqstp, NFS4_CDFC4_FORE);
1847 return nfserr_jukebox;
1849 spin_lock(&client_lock);
1850 status = nfserr_badsession;
1851 session = find_in_sessionid_hashtbl(&seq->sessionid);
1855 status = nfserr_too_many_ops;
1856 if (nfsd4_session_too_many_ops(rqstp, session))
1859 status = nfserr_req_too_big;
1860 if (nfsd4_request_too_big(rqstp, session))
1863 status = nfserr_badslot;
1864 if (seq->slotid >= session->se_fchannel.maxreqs)
1867 slot = session->se_slots[seq->slotid];
1868 dprintk("%s: slotid %d\n", __func__, seq->slotid);
1870 /* We do not negotiate the number of slots yet, so set the
1871 * maxslots to the session maxreqs which is used to encode
1872 * sr_highest_slotid and the sr_target_slot id to maxslots */
1873 seq->maxslots = session->se_fchannel.maxreqs;
1875 status = check_slot_seqid(seq->seqid, slot->sl_seqid, slot->sl_inuse);
1876 if (status == nfserr_replay_cache) {
1877 cstate->slot = slot;
1878 cstate->session = session;
1879 /* Return the cached reply status and set cstate->status
1880 * for nfsd4_proc_compound processing */
1881 status = nfsd4_replay_cache_entry(resp, seq);
1882 cstate->status = nfserr_replay_cache;
1888 nfsd4_sequence_check_conn(conn, session);
1891 /* Success! bump slot seqid */
1892 slot->sl_inuse = true;
1893 slot->sl_seqid = seq->seqid;
1894 slot->sl_cachethis = seq->cachethis;
1896 cstate->slot = slot;
1897 cstate->session = session;
1900 /* Hold a session reference until done processing the compound. */
1901 if (cstate->session) {
1902 struct nfs4_client *clp = session->se_client;
1904 nfsd4_get_session(cstate->session);
1905 atomic_inc(&clp->cl_refcount);
1906 if (clp->cl_cb_state == NFSD4_CB_DOWN)
1907 seq->status_flags |= SEQ4_STATUS_CB_PATH_DOWN;
1910 spin_unlock(&client_lock);
1911 dprintk("%s: return %d\n", __func__, ntohl(status));
1916 nfsd4_reclaim_complete(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate, struct nfsd4_reclaim_complete *rc)
1920 if (rc->rca_one_fs) {
1921 if (!cstate->current_fh.fh_dentry)
1922 return nfserr_nofilehandle;
1924 * We don't take advantage of the rca_one_fs case.
1925 * That's OK, it's optional, we can safely ignore it.
1931 status = nfserr_complete_already;
1932 if (cstate->session->se_client->cl_firststate)
1935 status = nfserr_stale_clientid;
1936 if (is_client_expired(cstate->session->se_client))
1938 * The following error isn't really legal.
1939 * But we only get here if the client just explicitly
1940 * destroyed the client. Surely it no longer cares what
1941 * error it gets back on an operation for the dead
1947 nfsd4_create_clid_dir(cstate->session->se_client);
1949 nfs4_unlock_state();
1954 nfsd4_setclientid(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
1955 struct nfsd4_setclientid *setclid)
1957 struct xdr_netobj clname = {
1958 .len = setclid->se_namelen,
1959 .data = setclid->se_name,
1961 nfs4_verifier clverifier = setclid->se_verf;
1962 unsigned int strhashval;
1963 struct nfs4_client *conf, *unconf, *new;
1965 char dname[HEXDIR_LEN];
1967 if (!check_name(clname))
1968 return nfserr_inval;
1970 status = nfs4_make_rec_clidname(dname, &clname);
1975 * XXX The Duplicate Request Cache (DRC) has been checked (??)
1976 * We get here on a DRC miss.
1979 strhashval = clientstr_hashval(dname);
1982 conf = find_confirmed_client_by_str(dname, strhashval);
1984 /* RFC 3530 14.2.33 CASE 0: */
1985 status = nfserr_clid_inuse;
1986 if (clp_used_exchangeid(conf))
1988 if (!same_creds(&conf->cl_cred, &rqstp->rq_cred)) {
1989 char addr_str[INET6_ADDRSTRLEN];
1990 rpc_ntop((struct sockaddr *) &conf->cl_addr, addr_str,
1992 dprintk("NFSD: setclientid: string in use by client "
1993 "at %s\n", addr_str);
1998 * section 14.2.33 of RFC 3530 (under the heading "IMPLEMENTATION")
1999 * has a description of SETCLIENTID request processing consisting
2000 * of 5 bullet points, labeled as CASE0 - CASE4 below.
2002 unconf = find_unconfirmed_client_by_str(dname, strhashval);
2003 status = nfserr_jukebox;
2006 * RFC 3530 14.2.33 CASE 4:
2007 * placed first, because it is the normal case
2010 expire_client(unconf);
2011 new = create_client(clname, dname, rqstp, &clverifier);
2015 } else if (same_verf(&conf->cl_verifier, &clverifier)) {
2017 * RFC 3530 14.2.33 CASE 1:
2018 * probable callback update
2021 /* Note this is removing unconfirmed {*x***},
2022 * which is stronger than RFC recommended {vxc**}.
2023 * This has the advantage that there is at most
2024 * one {*x***} in either list at any time.
2026 expire_client(unconf);
2028 new = create_client(clname, dname, rqstp, &clverifier);
2031 copy_clid(new, conf);
2032 } else if (!unconf) {
2034 * RFC 3530 14.2.33 CASE 2:
2035 * probable client reboot; state will be removed if
2038 new = create_client(clname, dname, rqstp, &clverifier);
2044 * RFC 3530 14.2.33 CASE 3:
2045 * probable client reboot; state will be removed if
2048 expire_client(unconf);
2049 new = create_client(clname, dname, rqstp, &clverifier);
2055 * XXX: we should probably set this at creation time, and check
2056 * for consistent minorversion use throughout:
2058 new->cl_minorversion = 0;
2059 gen_callback(new, setclid, rqstp);
2060 add_to_unconfirmed(new, strhashval);
2061 setclid->se_clientid.cl_boot = new->cl_clientid.cl_boot;
2062 setclid->se_clientid.cl_id = new->cl_clientid.cl_id;
2063 memcpy(setclid->se_confirm.data, new->cl_confirm.data, sizeof(setclid->se_confirm.data));
2066 nfs4_unlock_state();
2072 * Section 14.2.34 of RFC 3530 (under the heading "IMPLEMENTATION") has
2073 * a description of SETCLIENTID_CONFIRM request processing consisting of 4
2074 * bullets, labeled as CASE1 - CASE4 below.
2077 nfsd4_setclientid_confirm(struct svc_rqst *rqstp,
2078 struct nfsd4_compound_state *cstate,
2079 struct nfsd4_setclientid_confirm *setclientid_confirm)
2081 struct sockaddr *sa = svc_addr(rqstp);
2082 struct nfs4_client *conf, *unconf;
2083 nfs4_verifier confirm = setclientid_confirm->sc_confirm;
2084 clientid_t * clid = &setclientid_confirm->sc_clientid;
2087 if (STALE_CLIENTID(clid))
2088 return nfserr_stale_clientid;
2090 * XXX The Duplicate Request Cache (DRC) has been checked (??)
2091 * We get here on a DRC miss.
2096 conf = find_confirmed_client(clid);
2097 unconf = find_unconfirmed_client(clid);
2099 status = nfserr_clid_inuse;
2100 if (conf && !rpc_cmp_addr((struct sockaddr *) &conf->cl_addr, sa))
2102 if (unconf && !rpc_cmp_addr((struct sockaddr *) &unconf->cl_addr, sa))
2106 * section 14.2.34 of RFC 3530 has a description of
2107 * SETCLIENTID_CONFIRM request processing consisting
2108 * of 4 bullet points, labeled as CASE1 - CASE4 below.
2110 if (conf && unconf && same_verf(&confirm, &unconf->cl_confirm)) {
2112 * RFC 3530 14.2.34 CASE 1:
2115 if (!same_creds(&conf->cl_cred, &unconf->cl_cred))
2116 status = nfserr_clid_inuse;
2118 nfsd4_change_callback(conf, &unconf->cl_cb_conn);
2119 nfsd4_probe_callback(conf);
2120 expire_client(unconf);
2124 } else if (conf && !unconf) {
2126 * RFC 3530 14.2.34 CASE 2:
2127 * probable retransmitted request; play it safe and
2130 if (!same_creds(&conf->cl_cred, &rqstp->rq_cred))
2131 status = nfserr_clid_inuse;
2134 } else if (!conf && unconf
2135 && same_verf(&unconf->cl_confirm, &confirm)) {
2137 * RFC 3530 14.2.34 CASE 3:
2138 * Normal case; new or rebooted client:
2140 if (!same_creds(&unconf->cl_cred, &rqstp->rq_cred)) {
2141 status = nfserr_clid_inuse;
2144 clientstr_hashval(unconf->cl_recdir);
2145 conf = find_confirmed_client_by_str(unconf->cl_recdir,
2148 nfsd4_remove_clid_dir(conf);
2149 expire_client(conf);
2151 move_to_confirmed(unconf);
2153 nfsd4_probe_callback(conf);
2156 } else if ((!conf || (conf && !same_verf(&conf->cl_confirm, &confirm)))
2157 && (!unconf || (unconf && !same_verf(&unconf->cl_confirm,
2160 * RFC 3530 14.2.34 CASE 4:
2161 * Client probably hasn't noticed that we rebooted yet.
2163 status = nfserr_stale_clientid;
2165 /* check that we have hit one of the cases...*/
2166 status = nfserr_clid_inuse;
2169 nfs4_unlock_state();
2173 /* OPEN Share state helper functions */
2174 static inline struct nfs4_file *
2175 alloc_init_file(struct inode *ino)
2177 struct nfs4_file *fp;
2178 unsigned int hashval = file_hashval(ino);
2180 fp = kmem_cache_alloc(file_slab, GFP_KERNEL);
2182 atomic_set(&fp->fi_ref, 1);
2183 INIT_LIST_HEAD(&fp->fi_hash);
2184 INIT_LIST_HEAD(&fp->fi_stateids);
2185 INIT_LIST_HEAD(&fp->fi_delegations);
2186 fp->fi_inode = igrab(ino);
2187 fp->fi_id = current_fileid++;
2188 fp->fi_had_conflict = false;
2189 fp->fi_lease = NULL;
2190 memset(fp->fi_fds, 0, sizeof(fp->fi_fds));
2191 memset(fp->fi_access, 0, sizeof(fp->fi_access));
2192 spin_lock(&recall_lock);
2193 list_add(&fp->fi_hash, &file_hashtbl[hashval]);
2194 spin_unlock(&recall_lock);
2201 nfsd4_free_slab(struct kmem_cache **slab)
2205 kmem_cache_destroy(*slab);
2210 nfsd4_free_slabs(void)
2212 nfsd4_free_slab(&openowner_slab);
2213 nfsd4_free_slab(&lockowner_slab);
2214 nfsd4_free_slab(&file_slab);
2215 nfsd4_free_slab(&stateid_slab);
2216 nfsd4_free_slab(&deleg_slab);
2220 nfsd4_init_slabs(void)
2222 openowner_slab = kmem_cache_create("nfsd4_openowners",
2223 sizeof(struct nfs4_openowner), 0, 0, NULL);
2224 if (openowner_slab == NULL)
2226 lockowner_slab = kmem_cache_create("nfsd4_lockowners",
2227 sizeof(struct nfs4_openowner), 0, 0, NULL);
2228 if (lockowner_slab == NULL)
2230 file_slab = kmem_cache_create("nfsd4_files",
2231 sizeof(struct nfs4_file), 0, 0, NULL);
2232 if (file_slab == NULL)
2234 stateid_slab = kmem_cache_create("nfsd4_stateids",
2235 sizeof(struct nfs4_stateid), 0, 0, NULL);
2236 if (stateid_slab == NULL)
2238 deleg_slab = kmem_cache_create("nfsd4_delegations",
2239 sizeof(struct nfs4_delegation), 0, 0, NULL);
2240 if (deleg_slab == NULL)
2245 dprintk("nfsd4: out of memory while initializing nfsv4\n");
2249 void nfs4_free_openowner(struct nfs4_openowner *oo)
2251 kfree(oo->oo_owner.so_owner.data);
2252 kmem_cache_free(openowner_slab, oo);
2255 void nfs4_free_lockowner(struct nfs4_lockowner *lo)
2257 kfree(lo->lo_owner.so_owner.data);
2258 kmem_cache_free(lockowner_slab, lo);
2261 static void init_nfs4_replay(struct nfs4_replay *rp)
2263 rp->rp_status = nfserr_serverfault;
2265 rp->rp_buf = rp->rp_ibuf;
2268 static inline void *alloc_stateowner(struct kmem_cache *slab, struct xdr_netobj *owner, struct nfs4_client *clp)
2270 struct nfs4_stateowner *sop;
2272 sop = kmem_cache_alloc(slab, GFP_KERNEL);
2276 sop->so_owner.data = kmemdup(owner->data, owner->len, GFP_KERNEL);
2277 if (!sop->so_owner.data) {
2278 kmem_cache_free(slab, sop);
2281 sop->so_owner.len = owner->len;
2283 INIT_LIST_HEAD(&sop->so_stateids);
2284 sop->so_id = current_ownerid++;
2285 sop->so_client = clp;
2286 init_nfs4_replay(&sop->so_replay);
2290 static void hash_openowner(struct nfs4_openowner *oo, struct nfs4_client *clp, unsigned int strhashval)
2292 unsigned int idhashval;
2294 idhashval = open_ownerid_hashval(oo->oo_owner.so_id);
2295 list_add(&oo->oo_owner.so_idhash, &open_ownerid_hashtbl[idhashval]);
2296 list_add(&oo->oo_owner.so_strhash, &open_ownerstr_hashtbl[strhashval]);
2297 list_add(&oo->oo_perclient, &clp->cl_openowners);
2300 static struct nfs4_openowner *
2301 alloc_init_open_stateowner(unsigned int strhashval, struct nfs4_client *clp, struct nfsd4_open *open) {
2302 struct nfs4_openowner *oo;
2304 oo = alloc_stateowner(openowner_slab, &open->op_owner, clp);
2307 oo->oo_owner.so_is_open_owner = 1;
2308 oo->oo_owner.so_seqid = open->op_seqid;
2309 oo->oo_confirmed = 0;
2311 INIT_LIST_HEAD(&oo->oo_close_lru);
2312 hash_openowner(oo, clp, strhashval);
2317 init_open_stateid(struct nfs4_stateid *stp, struct nfs4_file *fp, struct nfsd4_open *open) {
2318 struct nfs4_openowner *oo = open->op_openowner;
2319 unsigned int hashval = stateid_hashval(oo->oo_owner.so_id, fp->fi_id);
2321 INIT_LIST_HEAD(&stp->st_lockowners);
2322 list_add(&stp->st_hash, &stateid_hashtbl[hashval]);
2323 list_add(&stp->st_perstateowner, &oo->oo_owner.so_stateids);
2324 list_add(&stp->st_perfile, &fp->fi_stateids);
2325 stp->st_type = NFS4_OPEN_STID;
2326 stp->st_stateowner = &oo->oo_owner;
2329 stp->st_stateid.si_boot = boot_time;
2330 stp->st_stateid.si_stateownerid = oo->oo_owner.so_id;
2331 stp->st_stateid.si_fileid = fp->fi_id;
2332 /* note will be incremented before first return to client: */
2333 stp->st_stateid.si_generation = 0;
2334 stp->st_access_bmap = 0;
2335 stp->st_deny_bmap = 0;
2336 __set_bit(open->op_share_access & ~NFS4_SHARE_WANT_MASK,
2337 &stp->st_access_bmap);
2338 __set_bit(open->op_share_deny, &stp->st_deny_bmap);
2339 stp->st_openstp = NULL;
2343 move_to_close_lru(struct nfs4_openowner *oo)
2345 dprintk("NFSD: move_to_close_lru nfs4_openowner %p\n", oo);
2347 list_move_tail(&oo->oo_close_lru, &close_lru);
2348 oo->oo_time = get_seconds();
2352 same_owner_str(struct nfs4_stateowner *sop, struct xdr_netobj *owner,
2355 return (sop->so_owner.len == owner->len) &&
2356 0 == memcmp(sop->so_owner.data, owner->data, owner->len) &&
2357 (sop->so_client->cl_clientid.cl_id == clid->cl_id);
2360 static struct nfs4_openowner *
2361 find_openstateowner_str(unsigned int hashval, struct nfsd4_open *open)
2363 struct nfs4_stateowner *so = NULL;
2365 list_for_each_entry(so, &open_ownerstr_hashtbl[hashval], so_strhash) {
2366 if (same_owner_str(so, &open->op_owner, &open->op_clientid))
2367 return container_of(so, struct nfs4_openowner, oo_owner);
2372 /* search file_hashtbl[] for file */
2373 static struct nfs4_file *
2374 find_file(struct inode *ino)
2376 unsigned int hashval = file_hashval(ino);
2377 struct nfs4_file *fp;
2379 spin_lock(&recall_lock);
2380 list_for_each_entry(fp, &file_hashtbl[hashval], fi_hash) {
2381 if (fp->fi_inode == ino) {
2383 spin_unlock(&recall_lock);
2387 spin_unlock(&recall_lock);
2391 static inline int access_valid(u32 x, u32 minorversion)
2393 if ((x & NFS4_SHARE_ACCESS_MASK) < NFS4_SHARE_ACCESS_READ)
2395 if ((x & NFS4_SHARE_ACCESS_MASK) > NFS4_SHARE_ACCESS_BOTH)
2397 x &= ~NFS4_SHARE_ACCESS_MASK;
2398 if (minorversion && x) {
2399 if ((x & NFS4_SHARE_WANT_MASK) > NFS4_SHARE_WANT_CANCEL)
2401 if ((x & NFS4_SHARE_WHEN_MASK) > NFS4_SHARE_PUSH_DELEG_WHEN_UNCONTENDED)
2403 x &= ~(NFS4_SHARE_WANT_MASK | NFS4_SHARE_WHEN_MASK);
2410 static inline int deny_valid(u32 x)
2412 /* Note: unlike access bits, deny bits may be zero. */
2413 return x <= NFS4_SHARE_DENY_BOTH;
2417 * Called to check deny when READ with all zero stateid or
2418 * WRITE with all zero or all one stateid
2421 nfs4_share_conflict(struct svc_fh *current_fh, unsigned int deny_type)
2423 struct inode *ino = current_fh->fh_dentry->d_inode;
2424 struct nfs4_file *fp;
2425 struct nfs4_stateid *stp;
2428 dprintk("NFSD: nfs4_share_conflict\n");
2430 fp = find_file(ino);
2433 ret = nfserr_locked;
2434 /* Search for conflicting share reservations */
2435 list_for_each_entry(stp, &fp->fi_stateids, st_perfile) {
2436 if (test_bit(deny_type, &stp->st_deny_bmap) ||
2437 test_bit(NFS4_SHARE_DENY_BOTH, &stp->st_deny_bmap))
2446 static void nfsd_break_one_deleg(struct nfs4_delegation *dp)
2448 /* We're assuming the state code never drops its reference
2449 * without first removing the lease. Since we're in this lease
2450 * callback (and since the lease code is serialized by the kernel
2451 * lock) we know the server hasn't removed the lease yet, we know
2452 * it's safe to take a reference: */
2453 atomic_inc(&dp->dl_count);
2455 list_add_tail(&dp->dl_recall_lru, &del_recall_lru);
2457 /* only place dl_time is set. protected by lock_flocks*/
2458 dp->dl_time = get_seconds();
2460 nfsd4_cb_recall(dp);
2463 /* Called from break_lease() with lock_flocks() held. */
2464 static void nfsd_break_deleg_cb(struct file_lock *fl)
2466 struct nfs4_file *fp = (struct nfs4_file *)fl->fl_owner;
2467 struct nfs4_delegation *dp;
2470 /* We assume break_lease is only called once per lease: */
2471 BUG_ON(fp->fi_had_conflict);
2473 * We don't want the locks code to timeout the lease for us;
2474 * we'll remove it ourself if a delegation isn't returned
2477 fl->fl_break_time = 0;
2479 spin_lock(&recall_lock);
2480 fp->fi_had_conflict = true;
2481 list_for_each_entry(dp, &fp->fi_delegations, dl_perfile)
2482 nfsd_break_one_deleg(dp);
2483 spin_unlock(&recall_lock);
2487 int nfsd_change_deleg_cb(struct file_lock **onlist, int arg)
2490 return lease_modify(onlist, arg);
2495 static const struct lock_manager_operations nfsd_lease_mng_ops = {
2496 .lm_break = nfsd_break_deleg_cb,
2497 .lm_change = nfsd_change_deleg_cb,
2500 static __be32 nfsd4_check_seqid(struct nfsd4_compound_state *cstate, struct nfs4_stateowner *so, u32 seqid)
2502 if (nfsd4_has_session(cstate))
2504 if (seqid == so->so_seqid - 1)
2505 return nfserr_replay_me;
2506 if (seqid == so->so_seqid)
2508 return nfserr_bad_seqid;
2512 nfsd4_process_open1(struct nfsd4_compound_state *cstate,
2513 struct nfsd4_open *open)
2515 clientid_t *clientid = &open->op_clientid;
2516 struct nfs4_client *clp = NULL;
2517 unsigned int strhashval;
2518 struct nfs4_openowner *oo = NULL;
2521 if (!check_name(open->op_owner))
2522 return nfserr_inval;
2524 if (STALE_CLIENTID(&open->op_clientid))
2525 return nfserr_stale_clientid;
2527 strhashval = open_ownerstr_hashval(clientid->cl_id, &open->op_owner);
2528 oo = find_openstateowner_str(strhashval, open);
2529 open->op_openowner = oo;
2531 /* Make sure the client's lease hasn't expired. */
2532 clp = find_confirmed_client(clientid);
2534 return nfserr_expired;
2537 if (!oo->oo_confirmed) {
2538 /* Replace unconfirmed owners without checking for replay. */
2539 clp = oo->oo_owner.so_client;
2540 release_openowner(oo);
2541 open->op_openowner = NULL;
2544 status = nfsd4_check_seqid(cstate, &oo->oo_owner, open->op_seqid);
2548 if (open->op_openowner == NULL) {
2549 oo = alloc_init_open_stateowner(strhashval, clp, open);
2551 return nfserr_jukebox;
2552 open->op_openowner = oo;
2554 list_del_init(&oo->oo_close_lru);
2555 renew_client(oo->oo_owner.so_client);
2559 static inline __be32
2560 nfs4_check_delegmode(struct nfs4_delegation *dp, int flags)
2562 if ((flags & WR_STATE) && (dp->dl_type == NFS4_OPEN_DELEGATE_READ))
2563 return nfserr_openmode;
2568 static struct nfs4_delegation *
2569 find_delegation_file(struct nfs4_file *fp, stateid_t *stid)
2571 struct nfs4_delegation *dp;
2573 spin_lock(&recall_lock);
2574 list_for_each_entry(dp, &fp->fi_delegations, dl_perfile)
2575 if (dp->dl_stateid.si_stateownerid == stid->si_stateownerid) {
2576 spin_unlock(&recall_lock);
2579 spin_unlock(&recall_lock);
2583 static int share_access_to_flags(u32 share_access)
2585 share_access &= ~NFS4_SHARE_WANT_MASK;
2587 return share_access == NFS4_SHARE_ACCESS_READ ? RD_STATE : WR_STATE;
2591 nfs4_check_deleg(struct nfs4_file *fp, struct nfsd4_open *open,
2592 struct nfs4_delegation **dp)
2595 __be32 status = nfserr_bad_stateid;
2597 *dp = find_delegation_file(fp, &open->op_delegate_stateid);
2600 flags = share_access_to_flags(open->op_share_access);
2601 status = nfs4_check_delegmode(*dp, flags);
2605 if (open->op_claim_type != NFS4_OPEN_CLAIM_DELEGATE_CUR)
2609 open->op_openowner->oo_confirmed = 1;
2614 nfs4_check_open(struct nfs4_file *fp, struct nfsd4_open *open, struct nfs4_stateid **stpp)
2616 struct nfs4_stateid *local;
2617 struct nfs4_openowner *oo = open->op_openowner;
2619 list_for_each_entry(local, &fp->fi_stateids, st_perfile) {
2620 /* ignore lock owners */
2621 if (local->st_stateowner->so_is_open_owner == 0)
2623 /* remember if we have seen this open owner */
2624 if (local->st_stateowner == &oo->oo_owner)
2626 /* check for conflicting share reservations */
2627 if (!test_share(local, open))
2628 return nfserr_share_denied;
2633 static inline struct nfs4_stateid *
2634 nfs4_alloc_stateid(void)
2636 return kmem_cache_alloc(stateid_slab, GFP_KERNEL);
2639 static inline int nfs4_access_to_access(u32 nfs4_access)
2643 if (nfs4_access & NFS4_SHARE_ACCESS_READ)
2644 flags |= NFSD_MAY_READ;
2645 if (nfs4_access & NFS4_SHARE_ACCESS_WRITE)
2646 flags |= NFSD_MAY_WRITE;
2650 static __be32 nfs4_get_vfs_file(struct svc_rqst *rqstp, struct nfs4_file *fp,
2651 struct svc_fh *cur_fh, struct nfsd4_open *open)
2654 int oflag = nfs4_access_to_omode(open->op_share_access);
2655 int access = nfs4_access_to_access(open->op_share_access);
2657 /* CLAIM_DELEGATE_CUR is used in response to a broken lease;
2658 * allowing it to break the lease and return EAGAIN leaves the
2659 * client unable to make progress in returning the delegation */
2660 if (open->op_claim_type == NFS4_OPEN_CLAIM_DELEGATE_CUR)
2661 access |= NFSD_MAY_NOT_BREAK_LEASE;
2663 if (!fp->fi_fds[oflag]) {
2664 status = nfsd_open(rqstp, cur_fh, S_IFREG, access,
2665 &fp->fi_fds[oflag]);
2669 nfs4_file_get_access(fp, oflag);
2675 nfs4_new_open(struct svc_rqst *rqstp, struct nfs4_stateid **stpp,
2676 struct nfs4_file *fp, struct svc_fh *cur_fh,
2677 struct nfsd4_open *open)
2679 struct nfs4_stateid *stp;
2682 stp = nfs4_alloc_stateid();
2684 return nfserr_jukebox;
2686 status = nfs4_get_vfs_file(rqstp, fp, cur_fh, open);
2688 kmem_cache_free(stateid_slab, stp);
2695 static inline __be32
2696 nfsd4_truncate(struct svc_rqst *rqstp, struct svc_fh *fh,
2697 struct nfsd4_open *open)
2699 struct iattr iattr = {
2700 .ia_valid = ATTR_SIZE,
2703 if (!open->op_truncate)
2705 if (!(open->op_share_access & NFS4_SHARE_ACCESS_WRITE))
2706 return nfserr_inval;
2707 return nfsd_setattr(rqstp, fh, &iattr, 0, (time_t)0);
2711 nfs4_upgrade_open(struct svc_rqst *rqstp, struct nfs4_file *fp, struct svc_fh *cur_fh, struct nfs4_stateid *stp, struct nfsd4_open *open)
2713 u32 op_share_access = open->op_share_access & ~NFS4_SHARE_WANT_MASK;
2717 new_access = !test_bit(op_share_access, &stp->st_access_bmap);
2719 status = nfs4_get_vfs_file(rqstp, fp, cur_fh, open);
2723 status = nfsd4_truncate(rqstp, cur_fh, open);
2726 int oflag = nfs4_access_to_omode(op_share_access);
2727 nfs4_file_put_access(fp, oflag);
2731 /* remember the open */
2732 __set_bit(op_share_access, &stp->st_access_bmap);
2733 __set_bit(open->op_share_deny, &stp->st_deny_bmap);
2740 nfs4_set_claim_prev(struct nfsd4_open *open)
2742 open->op_openowner->oo_confirmed = 1;
2743 open->op_openowner->oo_owner.so_client->cl_firststate = 1;
2746 /* Should we give out recallable state?: */
2747 static bool nfsd4_cb_channel_good(struct nfs4_client *clp)
2749 if (clp->cl_cb_state == NFSD4_CB_UP)
2752 * In the sessions case, since we don't have to establish a
2753 * separate connection for callbacks, we assume it's OK
2754 * until we hear otherwise:
2756 return clp->cl_minorversion && clp->cl_cb_state == NFSD4_CB_UNKNOWN;
2759 static struct file_lock *nfs4_alloc_init_lease(struct nfs4_delegation *dp, int flag)
2761 struct file_lock *fl;
2763 fl = locks_alloc_lock();
2766 locks_init_lock(fl);
2767 fl->fl_lmops = &nfsd_lease_mng_ops;
2768 fl->fl_flags = FL_LEASE;
2769 fl->fl_type = flag == NFS4_OPEN_DELEGATE_READ? F_RDLCK: F_WRLCK;
2770 fl->fl_end = OFFSET_MAX;
2771 fl->fl_owner = (fl_owner_t)(dp->dl_file);
2772 fl->fl_pid = current->tgid;
2776 static int nfs4_setlease(struct nfs4_delegation *dp, int flag)
2778 struct nfs4_file *fp = dp->dl_file;
2779 struct file_lock *fl;
2782 fl = nfs4_alloc_init_lease(dp, flag);
2785 fl->fl_file = find_readable_file(fp);
2786 list_add(&dp->dl_perclnt, &dp->dl_client->cl_delegations);
2787 status = vfs_setlease(fl->fl_file, fl->fl_type, &fl);
2789 list_del_init(&dp->dl_perclnt);
2790 locks_free_lock(fl);
2794 fp->fi_deleg_file = fl->fl_file;
2795 get_file(fp->fi_deleg_file);
2796 atomic_set(&fp->fi_delegees, 1);
2797 list_add(&dp->dl_perfile, &fp->fi_delegations);
2801 static int nfs4_set_delegation(struct nfs4_delegation *dp, int flag)
2803 struct nfs4_file *fp = dp->dl_file;
2806 return nfs4_setlease(dp, flag);
2807 spin_lock(&recall_lock);
2808 if (fp->fi_had_conflict) {
2809 spin_unlock(&recall_lock);
2812 atomic_inc(&fp->fi_delegees);
2813 list_add(&dp->dl_perfile, &fp->fi_delegations);
2814 spin_unlock(&recall_lock);
2815 list_add(&dp->dl_perclnt, &dp->dl_client->cl_delegations);
2820 * Attempt to hand out a delegation.
2823 nfs4_open_delegation(struct svc_fh *fh, struct nfsd4_open *open, struct nfs4_stateid *stp)
2825 struct nfs4_delegation *dp;
2826 struct nfs4_openowner *oo = container_of(stp->st_stateowner, struct nfs4_openowner, oo_owner);
2828 int status, flag = 0;
2830 cb_up = nfsd4_cb_channel_good(oo->oo_owner.so_client);
2831 flag = NFS4_OPEN_DELEGATE_NONE;
2832 open->op_recall = 0;
2833 switch (open->op_claim_type) {
2834 case NFS4_OPEN_CLAIM_PREVIOUS:
2836 open->op_recall = 1;
2837 flag = open->op_delegate_type;
2838 if (flag == NFS4_OPEN_DELEGATE_NONE)
2841 case NFS4_OPEN_CLAIM_NULL:
2842 /* Let's not give out any delegations till everyone's
2843 * had the chance to reclaim theirs.... */
2844 if (locks_in_grace())
2846 if (!cb_up || !oo->oo_confirmed)
2848 if (open->op_share_access & NFS4_SHARE_ACCESS_WRITE)
2849 flag = NFS4_OPEN_DELEGATE_WRITE;
2851 flag = NFS4_OPEN_DELEGATE_READ;
2857 dp = alloc_init_deleg(oo->oo_owner.so_client, stp, fh, flag);
2860 status = nfs4_set_delegation(dp, flag);
2864 memcpy(&open->op_delegate_stateid, &dp->dl_stateid, sizeof(dp->dl_stateid));
2866 dprintk("NFSD: delegation stateid=" STATEID_FMT "\n",
2867 STATEID_VAL(&dp->dl_stateid));
2869 if (open->op_claim_type == NFS4_OPEN_CLAIM_PREVIOUS
2870 && flag == NFS4_OPEN_DELEGATE_NONE
2871 && open->op_delegate_type != NFS4_OPEN_DELEGATE_NONE)
2872 dprintk("NFSD: WARNING: refusing delegation reclaim\n");
2873 open->op_delegate_type = flag;
2876 nfs4_put_delegation(dp);
2878 flag = NFS4_OPEN_DELEGATE_NONE;
2883 * called with nfs4_lock_state() held.
2886 nfsd4_process_open2(struct svc_rqst *rqstp, struct svc_fh *current_fh, struct nfsd4_open *open)
2888 struct nfsd4_compoundres *resp = rqstp->rq_resp;
2889 struct nfs4_file *fp = NULL;
2890 struct inode *ino = current_fh->fh_dentry->d_inode;
2891 struct nfs4_stateid *stp = NULL;
2892 struct nfs4_delegation *dp = NULL;
2895 status = nfserr_inval;
2896 if (!access_valid(open->op_share_access, resp->cstate.minorversion)
2897 || !deny_valid(open->op_share_deny))
2900 * Lookup file; if found, lookup stateid and check open request,
2901 * and check for delegations in the process of being recalled.
2902 * If not found, create the nfs4_file struct
2904 fp = find_file(ino);
2906 if ((status = nfs4_check_open(fp, open, &stp)))
2908 status = nfs4_check_deleg(fp, open, &dp);
2912 status = nfserr_bad_stateid;
2913 if (open->op_claim_type == NFS4_OPEN_CLAIM_DELEGATE_CUR)
2915 status = nfserr_jukebox;
2916 fp = alloc_init_file(ino);
2922 * OPEN the file, or upgrade an existing OPEN.
2923 * If truncate fails, the OPEN fails.
2926 /* Stateid was found, this is an OPEN upgrade */
2927 status = nfs4_upgrade_open(rqstp, fp, current_fh, stp, open);
2931 status = nfs4_new_open(rqstp, &stp, fp, current_fh, open);
2934 init_open_stateid(stp, fp, open);
2935 status = nfsd4_truncate(rqstp, current_fh, open);
2937 release_open_stateid(stp);
2941 update_stateid(&stp->st_stateid);
2942 memcpy(&open->op_stateid, &stp->st_stateid, sizeof(stateid_t));
2944 if (nfsd4_has_session(&resp->cstate))
2945 open->op_openowner->oo_confirmed = 1;
2948 * Attempt to hand out a delegation. No error return, because the
2949 * OPEN succeeds even if we fail.
2951 nfs4_open_delegation(current_fh, open, stp);
2955 dprintk("%s: stateid=" STATEID_FMT "\n", __func__,
2956 STATEID_VAL(&stp->st_stateid));
2960 if (status == 0 && open->op_claim_type == NFS4_OPEN_CLAIM_PREVIOUS)
2961 nfs4_set_claim_prev(open);
2963 * To finish the open response, we just need to set the rflags.
2965 open->op_rflags = NFS4_OPEN_RESULT_LOCKTYPE_POSIX;
2966 if (!open->op_openowner->oo_confirmed &&
2967 !nfsd4_has_session(&resp->cstate))
2968 open->op_rflags |= NFS4_OPEN_RESULT_CONFIRM;
2974 nfsd4_renew(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
2977 struct nfs4_client *clp;
2981 dprintk("process_renew(%08x/%08x): starting\n",
2982 clid->cl_boot, clid->cl_id);
2983 status = nfserr_stale_clientid;
2984 if (STALE_CLIENTID(clid))
2986 clp = find_confirmed_client(clid);
2987 status = nfserr_expired;
2989 /* We assume the client took too long to RENEW. */
2990 dprintk("nfsd4_renew: clientid not found!\n");
2994 status = nfserr_cb_path_down;
2995 if (!list_empty(&clp->cl_delegations)
2996 && clp->cl_cb_state != NFSD4_CB_UP)
3000 nfs4_unlock_state();
3004 static struct lock_manager nfsd4_manager = {
3008 nfsd4_end_grace(void)
3010 dprintk("NFSD: end of grace period\n");
3011 nfsd4_recdir_purge_old();
3012 locks_end_grace(&nfsd4_manager);
3014 * Now that every NFSv4 client has had the chance to recover and
3015 * to see the (possibly new, possibly shorter) lease time, we
3016 * can safely set the next grace time to the current lease time:
3018 nfsd4_grace = nfsd4_lease;
3022 nfs4_laundromat(void)
3024 struct nfs4_client *clp;
3025 struct nfs4_openowner *oo;
3026 struct nfs4_delegation *dp;
3027 struct list_head *pos, *next, reaplist;
3028 time_t cutoff = get_seconds() - nfsd4_lease;
3029 time_t t, clientid_val = nfsd4_lease;
3030 time_t u, test_val = nfsd4_lease;
3034 dprintk("NFSD: laundromat service - starting\n");
3035 if (locks_in_grace())
3037 INIT_LIST_HEAD(&reaplist);
3038 spin_lock(&client_lock);
3039 list_for_each_safe(pos, next, &client_lru) {
3040 clp = list_entry(pos, struct nfs4_client, cl_lru);
3041 if (time_after((unsigned long)clp->cl_time, (unsigned long)cutoff)) {
3042 t = clp->cl_time - cutoff;
3043 if (clientid_val > t)
3047 if (atomic_read(&clp->cl_refcount)) {
3048 dprintk("NFSD: client in use (clientid %08x)\n",
3049 clp->cl_clientid.cl_id);
3052 unhash_client_locked(clp);
3053 list_add(&clp->cl_lru, &reaplist);
3055 spin_unlock(&client_lock);
3056 list_for_each_safe(pos, next, &reaplist) {
3057 clp = list_entry(pos, struct nfs4_client, cl_lru);
3058 dprintk("NFSD: purging unused client (clientid %08x)\n",
3059 clp->cl_clientid.cl_id);
3060 nfsd4_remove_clid_dir(clp);
3063 spin_lock(&recall_lock);
3064 list_for_each_safe(pos, next, &del_recall_lru) {
3065 dp = list_entry (pos, struct nfs4_delegation, dl_recall_lru);
3066 if (time_after((unsigned long)dp->dl_time, (unsigned long)cutoff)) {
3067 u = dp->dl_time - cutoff;
3072 list_move(&dp->dl_recall_lru, &reaplist);
3074 spin_unlock(&recall_lock);
3075 list_for_each_safe(pos, next, &reaplist) {
3076 dp = list_entry (pos, struct nfs4_delegation, dl_recall_lru);
3077 list_del_init(&dp->dl_recall_lru);
3078 unhash_delegation(dp);
3080 test_val = nfsd4_lease;
3081 list_for_each_safe(pos, next, &close_lru) {
3082 oo = container_of(pos, struct nfs4_openowner, oo_close_lru);
3083 if (time_after((unsigned long)oo->oo_time, (unsigned long)cutoff)) {
3084 u = oo->oo_time - cutoff;
3089 dprintk("NFSD: purging unused open stateowner (so_id %d)\n",
3090 oo->oo_owner.so_id);
3091 release_openowner(oo);
3093 if (clientid_val < NFSD_LAUNDROMAT_MINTIMEOUT)
3094 clientid_val = NFSD_LAUNDROMAT_MINTIMEOUT;
3095 nfs4_unlock_state();
3096 return clientid_val;
3099 static struct workqueue_struct *laundry_wq;
3100 static void laundromat_main(struct work_struct *);
3101 static DECLARE_DELAYED_WORK(laundromat_work, laundromat_main);
3104 laundromat_main(struct work_struct *not_used)
3108 t = nfs4_laundromat();
3109 dprintk("NFSD: laundromat_main - sleeping for %ld seconds\n", t);
3110 queue_delayed_work(laundry_wq, &laundromat_work, t*HZ);
3113 static struct nfs4_openowner * search_close_lru(u32 st_id)
3115 struct nfs4_openowner *local;
3117 list_for_each_entry(local, &close_lru, oo_close_lru) {
3118 if (local->oo_owner.so_id == st_id)
3125 nfs4_check_fh(struct svc_fh *fhp, struct nfs4_stateid *stp)
3127 return fhp->fh_dentry->d_inode != stp->st_file->fi_inode;
3131 STALE_STATEID(stateid_t *stateid)
3133 if (stateid->si_boot == boot_time)
3135 dprintk("NFSD: stale stateid " STATEID_FMT "!\n",
3136 STATEID_VAL(stateid));
3141 access_permit_read(unsigned long access_bmap)
3143 return test_bit(NFS4_SHARE_ACCESS_READ, &access_bmap) ||
3144 test_bit(NFS4_SHARE_ACCESS_BOTH, &access_bmap) ||
3145 test_bit(NFS4_SHARE_ACCESS_WRITE, &access_bmap);
3149 access_permit_write(unsigned long access_bmap)
3151 return test_bit(NFS4_SHARE_ACCESS_WRITE, &access_bmap) ||
3152 test_bit(NFS4_SHARE_ACCESS_BOTH, &access_bmap);
3156 __be32 nfs4_check_openmode(struct nfs4_stateid *stp, int flags)
3158 __be32 status = nfserr_openmode;
3160 /* For lock stateid's, we test the parent open, not the lock: */
3161 if (stp->st_openstp)
3162 stp = stp->st_openstp;
3163 if ((flags & WR_STATE) && (!access_permit_write(stp->st_access_bmap)))
3165 if ((flags & RD_STATE) && (!access_permit_read(stp->st_access_bmap)))
3172 static inline __be32
3173 check_special_stateids(svc_fh *current_fh, stateid_t *stateid, int flags)
3175 if (ONE_STATEID(stateid) && (flags & RD_STATE))
3177 else if (locks_in_grace()) {
3178 /* Answer in remaining cases depends on existence of
3179 * conflicting state; so we must wait out the grace period. */
3180 return nfserr_grace;
3181 } else if (flags & WR_STATE)
3182 return nfs4_share_conflict(current_fh,
3183 NFS4_SHARE_DENY_WRITE);
3184 else /* (flags & RD_STATE) && ZERO_STATEID(stateid) */
3185 return nfs4_share_conflict(current_fh,
3186 NFS4_SHARE_DENY_READ);
3190 * Allow READ/WRITE during grace period on recovered state only for files
3191 * that are not able to provide mandatory locking.
3194 grace_disallows_io(struct inode *inode)
3196 return locks_in_grace() && mandatory_lock(inode);
3199 /* Returns true iff a is later than b: */
3200 static bool stateid_generation_after(stateid_t *a, stateid_t *b)
3202 return (s32)a->si_generation - (s32)b->si_generation > 0;
3205 static int check_stateid_generation(stateid_t *in, stateid_t *ref, bool has_session)
3208 * When sessions are used the stateid generation number is ignored
3211 if (has_session && in->si_generation == 0)
3214 if (in->si_generation == ref->si_generation)
3217 /* If the client sends us a stateid from the future, it's buggy: */
3218 if (stateid_generation_after(in, ref))
3219 return nfserr_bad_stateid;
3221 * However, we could see a stateid from the past, even from a
3222 * non-buggy client. For example, if the client sends a lock
3223 * while some IO is outstanding, the lock may bump si_generation
3224 * while the IO is still in flight. The client could avoid that
3225 * situation by waiting for responses on all the IO requests,
3226 * but better performance may result in retrying IO that
3227 * receives an old_stateid error if requests are rarely
3228 * reordered in flight:
3230 return nfserr_old_stateid;
3233 static int is_delegation_stateid(stateid_t *stateid)
3235 return stateid->si_fileid == 0;
3238 __be32 nfs4_validate_stateid(stateid_t *stateid, bool has_session)
3240 struct nfs4_stateid *stp = NULL;
3241 __be32 status = nfserr_stale_stateid;
3243 if (STALE_STATEID(stateid))
3246 status = nfserr_expired;
3247 stp = find_stateid(stateid);
3250 status = nfserr_bad_stateid;
3251 if (stp->st_stateowner->so_is_open_owner
3252 && !openowner(stp->st_stateowner)->oo_confirmed)
3255 status = check_stateid_generation(stateid, &stp->st_stateid, has_session);
3265 * Checks for stateid operations
3268 nfs4_preprocess_stateid_op(struct nfsd4_compound_state *cstate,
3269 stateid_t *stateid, int flags, struct file **filpp)
3271 struct nfs4_stateid *stp = NULL;
3272 struct nfs4_delegation *dp = NULL;
3273 struct svc_fh *current_fh = &cstate->current_fh;
3274 struct inode *ino = current_fh->fh_dentry->d_inode;
3280 if (grace_disallows_io(ino))
3281 return nfserr_grace;
3283 if (ZERO_STATEID(stateid) || ONE_STATEID(stateid))
3284 return check_special_stateids(current_fh, stateid, flags);
3286 status = nfserr_stale_stateid;
3287 if (STALE_STATEID(stateid))
3291 * We assume that any stateid that has the current boot time,
3292 * but that we can't find, is expired:
3294 status = nfserr_expired;
3295 if (is_delegation_stateid(stateid)) {
3296 dp = find_delegation_stateid(ino, stateid);
3299 status = check_stateid_generation(stateid, &dp->dl_stateid, nfsd4_has_session(cstate));
3302 status = nfs4_check_delegmode(dp, flags);
3305 renew_client(dp->dl_client);
3307 *filpp = dp->dl_file->fi_deleg_file;
3310 } else { /* open or lock stateid */
3311 stp = find_stateid(stateid);
3314 status = nfserr_bad_stateid;
3315 if (nfs4_check_fh(current_fh, stp))
3317 if (stp->st_stateowner->so_is_open_owner
3318 && !openowner(stp->st_stateowner)->oo_confirmed)
3320 status = check_stateid_generation(stateid, &stp->st_stateid,
3321 nfsd4_has_session(cstate));
3324 status = nfs4_check_openmode(stp, flags);
3327 renew_client(stp->st_stateowner->so_client);
3329 if (flags & RD_STATE)
3330 *filpp = find_readable_file(stp->st_file);
3332 *filpp = find_writeable_file(stp->st_file);
3341 nfsd4_free_delegation_stateid(stateid_t *stateid)
3343 struct nfs4_delegation *dp = search_for_delegation(stateid);
3345 return nfserr_locks_held;
3346 return nfserr_bad_stateid;
3350 nfsd4_free_lock_stateid(struct nfs4_stateid *stp)
3352 if (check_for_locks(stp->st_file, lockowner(stp->st_stateowner)))
3353 return nfserr_locks_held;
3354 release_lock_stateid(stp);
3359 * Test if the stateid is valid
3362 nfsd4_test_stateid(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
3363 struct nfsd4_test_stateid *test_stateid)
3365 test_stateid->ts_has_session = nfsd4_has_session(cstate);
3373 nfsd4_free_stateid(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
3374 struct nfsd4_free_stateid *free_stateid)
3376 stateid_t *stateid = &free_stateid->fr_stateid;
3377 struct nfs4_stateid *stp;
3381 if (is_delegation_stateid(stateid)) {
3382 ret = nfsd4_free_delegation_stateid(stateid);
3386 stp = find_stateid(stateid);
3388 ret = nfserr_bad_stateid;
3391 ret = check_stateid_generation(stateid, &stp->st_stateid, 1);
3395 if (stp->st_type == NFS4_OPEN_STID) {
3396 ret = nfserr_locks_held;
3399 ret = nfsd4_free_lock_stateid(stp);
3404 nfs4_unlock_state();
3411 return (type == NFS4_READW_LT || type == NFS4_READ_LT) ?
3412 RD_STATE : WR_STATE;
3415 static __be32 nfs4_nospecial_stateid_checks(stateid_t *stateid)
3417 if (ZERO_STATEID(stateid) || ONE_STATEID(stateid))
3418 return nfserr_bad_stateid;
3419 if (STALE_STATEID(stateid))
3420 return nfserr_stale_stateid;
3424 static __be32 nfs4_seqid_op_checks(struct nfsd4_compound_state *cstate, stateid_t *stateid, u32 seqid, struct nfs4_stateid *stp)
3426 struct svc_fh *current_fh = &cstate->current_fh;
3427 struct nfs4_stateowner *sop = stp->st_stateowner;
3430 if (nfs4_check_fh(current_fh, stp))
3431 return nfserr_bad_stateid;
3432 status = nfsd4_check_seqid(cstate, sop, seqid);
3435 return check_stateid_generation(stateid, &stp->st_stateid, nfsd4_has_session(cstate));
3439 * Checks for sequence id mutating operations.
3442 nfs4_preprocess_seqid_op(struct nfsd4_compound_state *cstate, u32 seqid,
3443 stateid_t *stateid, char typemask,
3444 struct nfs4_stateid **stpp)
3448 dprintk("NFSD: %s: seqid=%d stateid = " STATEID_FMT "\n", __func__,
3449 seqid, STATEID_VAL(stateid));
3452 status = nfs4_nospecial_stateid_checks(stateid);
3455 *stpp = find_stateid_by_type(stateid, typemask);
3457 return nfserr_expired;
3458 cstate->replay_owner = (*stpp)->st_stateowner;
3459 renew_client((*stpp)->st_stateowner->so_client);
3461 return nfs4_seqid_op_checks(cstate, stateid, seqid, *stpp);
3464 static __be32 nfs4_preprocess_confirmed_seqid_op(struct nfsd4_compound_state *cstate, u32 seqid, stateid_t *stateid, struct nfs4_stateid **stpp)
3467 struct nfs4_openowner *oo;
3469 status = nfs4_preprocess_seqid_op(cstate, seqid, stateid,
3470 NFS4_OPEN_STID, stpp);
3473 oo = openowner((*stpp)->st_stateowner);
3474 if (!oo->oo_confirmed)
3475 return nfserr_bad_stateid;
3480 nfsd4_open_confirm(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
3481 struct nfsd4_open_confirm *oc)
3484 struct nfs4_openowner *oo;
3485 struct nfs4_stateid *stp;
3487 dprintk("NFSD: nfsd4_open_confirm on file %.*s\n",
3488 (int)cstate->current_fh.fh_dentry->d_name.len,
3489 cstate->current_fh.fh_dentry->d_name.name);
3491 status = fh_verify(rqstp, &cstate->current_fh, S_IFREG, 0);
3497 status = nfs4_preprocess_seqid_op(cstate,
3498 oc->oc_seqid, &oc->oc_req_stateid,
3499 NFS4_OPEN_STID, &stp);
3502 oo = openowner(stp->st_stateowner);
3503 status = nfserr_bad_stateid;
3504 if (oo->oo_confirmed)
3506 oo->oo_confirmed = 1;
3507 update_stateid(&stp->st_stateid);
3508 memcpy(&oc->oc_resp_stateid, &stp->st_stateid, sizeof(stateid_t));
3509 dprintk("NFSD: %s: success, seqid=%d stateid=" STATEID_FMT "\n",
3510 __func__, oc->oc_seqid, STATEID_VAL(&stp->st_stateid));
3512 nfsd4_create_clid_dir(oo->oo_owner.so_client);
3515 if (!cstate->replay_owner)
3516 nfs4_unlock_state();
3520 static inline void nfs4_file_downgrade(struct nfs4_stateid *stp, unsigned int to_access)
3524 for (i = 1; i < 4; i++) {
3525 if (test_bit(i, &stp->st_access_bmap) && !(i & to_access)) {
3526 nfs4_file_put_access(stp->st_file, i);
3527 __clear_bit(i, &stp->st_access_bmap);
3533 reset_union_bmap_deny(unsigned long deny, unsigned long *bmap)
3536 for (i = 0; i < 4; i++) {
3537 if ((i & deny) != i)
3538 __clear_bit(i, bmap);
3543 nfsd4_open_downgrade(struct svc_rqst *rqstp,
3544 struct nfsd4_compound_state *cstate,
3545 struct nfsd4_open_downgrade *od)
3548 struct nfs4_stateid *stp;
3550 dprintk("NFSD: nfsd4_open_downgrade on file %.*s\n",
3551 (int)cstate->current_fh.fh_dentry->d_name.len,
3552 cstate->current_fh.fh_dentry->d_name.name);
3554 if (!access_valid(od->od_share_access, cstate->minorversion)
3555 || !deny_valid(od->od_share_deny))
3556 return nfserr_inval;
3559 status = nfs4_preprocess_confirmed_seqid_op(cstate, od->od_seqid,
3560 &od->od_stateid, &stp);
3563 status = nfserr_inval;
3564 if (!test_bit(od->od_share_access, &stp->st_access_bmap)) {
3565 dprintk("NFSD:access not a subset current bitmap: 0x%lx, input access=%08x\n",
3566 stp->st_access_bmap, od->od_share_access);
3569 if (!test_bit(od->od_share_deny, &stp->st_deny_bmap)) {
3570 dprintk("NFSD:deny not a subset current bitmap: 0x%lx, input deny=%08x\n",
3571 stp->st_deny_bmap, od->od_share_deny);
3574 nfs4_file_downgrade(stp, od->od_share_access);
3576 reset_union_bmap_deny(od->od_share_deny, &stp->st_deny_bmap);
3578 update_stateid(&stp->st_stateid);
3579 memcpy(&od->od_stateid, &stp->st_stateid, sizeof(stateid_t));
3582 if (!cstate->replay_owner)
3583 nfs4_unlock_state();
3588 * nfs4_unlock_state() called after encode
3591 nfsd4_close(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
3592 struct nfsd4_close *close)
3595 struct nfs4_openowner *oo;
3596 struct nfs4_stateid *stp;
3598 dprintk("NFSD: nfsd4_close on file %.*s\n",
3599 (int)cstate->current_fh.fh_dentry->d_name.len,
3600 cstate->current_fh.fh_dentry->d_name.name);
3603 /* check close_lru for replay */
3604 status = nfs4_preprocess_confirmed_seqid_op(cstate, close->cl_seqid,
3605 &close->cl_stateid, &stp);
3606 if (stp == NULL && status == nfserr_expired) {
3608 * Also, we should make sure this isn't just the result of
3611 oo = search_close_lru(close->cl_stateid.si_stateownerid);
3612 /* It's not stale; let's assume it's expired: */
3615 cstate->replay_owner = &oo->oo_owner;
3616 status = nfsd4_check_seqid(cstate, &oo->oo_owner, close->cl_seqid);
3619 status = nfserr_bad_seqid;
3623 oo = openowner(stp->st_stateowner);
3625 update_stateid(&stp->st_stateid);
3626 memcpy(&close->cl_stateid, &stp->st_stateid, sizeof(stateid_t));
3628 /* release_stateid() calls nfsd_close() if needed */
3629 release_open_stateid(stp);
3631 /* place unused nfs4_stateowners on so_close_lru list to be
3632 * released by the laundromat service after the lease period
3633 * to enable us to handle CLOSE replay
3635 if (list_empty(&oo->oo_owner.so_stateids))
3636 move_to_close_lru(oo);
3638 if (!cstate->replay_owner)
3639 nfs4_unlock_state();
3644 nfsd4_delegreturn(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
3645 struct nfsd4_delegreturn *dr)
3647 struct nfs4_delegation *dp;
3648 stateid_t *stateid = &dr->dr_stateid;
3649 struct inode *inode;
3652 if ((status = fh_verify(rqstp, &cstate->current_fh, S_IFREG, 0)))
3654 inode = cstate->current_fh.fh_dentry->d_inode;
3657 status = nfserr_bad_stateid;
3658 if (ZERO_STATEID(stateid) || ONE_STATEID(stateid))
3660 status = nfserr_stale_stateid;
3661 if (STALE_STATEID(stateid))
3663 status = nfserr_bad_stateid;
3664 if (!is_delegation_stateid(stateid))
3666 status = nfserr_expired;
3667 dp = find_delegation_stateid(inode, stateid);
3670 status = check_stateid_generation(stateid, &dp->dl_stateid, nfsd4_has_session(cstate));
3673 renew_client(dp->dl_client);
3675 unhash_delegation(dp);
3677 nfs4_unlock_state();
3684 * Lock owner state (byte-range locks)
3686 #define LOFF_OVERFLOW(start, len) ((u64)(len) > ~(u64)(start))
3687 #define LOCK_HASH_BITS 8
3688 #define LOCK_HASH_SIZE (1 << LOCK_HASH_BITS)
3689 #define LOCK_HASH_MASK (LOCK_HASH_SIZE - 1)
3692 end_offset(u64 start, u64 len)
3697 return end >= start ? end: NFS4_MAX_UINT64;
3700 /* last octet in a range */
3702 last_byte_offset(u64 start, u64 len)
3708 return end > start ? end - 1: NFS4_MAX_UINT64;
3711 static unsigned int lockownerid_hashval(u32 id)
3713 return id & LOCK_HASH_MASK;
3716 static inline unsigned int
3717 lock_ownerstr_hashval(struct inode *inode, u32 cl_id,
3718 struct xdr_netobj *ownername)
3720 return (file_hashval(inode) + cl_id
3721 + opaque_hashval(ownername->data, ownername->len))
3725 static struct list_head lock_ownerid_hashtbl[LOCK_HASH_SIZE];
3726 static struct list_head lock_ownerstr_hashtbl[LOCK_HASH_SIZE];
3728 static struct nfs4_delegation *
3729 search_for_delegation(stateid_t *stid)
3731 struct nfs4_file *fp;
3732 struct nfs4_delegation *dp;
3733 struct list_head *pos;
3736 for (i = 0; i < FILE_HASH_SIZE; i++) {
3737 list_for_each_entry(fp, &file_hashtbl[i], fi_hash) {
3738 list_for_each(pos, &fp->fi_delegations) {
3739 dp = list_entry(pos, struct nfs4_delegation, dl_perfile);
3740 if (same_stateid(&dp->dl_stateid, stid))
3748 static struct nfs4_delegation *
3749 find_delegation_stateid(struct inode *ino, stateid_t *stid)
3751 struct nfs4_file *fp;
3752 struct nfs4_delegation *dl;
3754 dprintk("NFSD: %s: stateid=" STATEID_FMT "\n", __func__,
3757 fp = find_file(ino);
3760 dl = find_delegation_file(fp, stid);
3766 * TODO: Linux file offsets are _signed_ 64-bit quantities, which means that
3767 * we can't properly handle lock requests that go beyond the (2^63 - 1)-th
3768 * byte, because of sign extension problems. Since NFSv4 calls for 64-bit
3769 * locking, this prevents us from being completely protocol-compliant. The
3770 * real solution to this problem is to start using unsigned file offsets in
3771 * the VFS, but this is a very deep change!
3774 nfs4_transform_lock_offset(struct file_lock *lock)
3776 if (lock->fl_start < 0)
3777 lock->fl_start = OFFSET_MAX;
3778 if (lock->fl_end < 0)
3779 lock->fl_end = OFFSET_MAX;
3782 /* Hack!: For now, we're defining this just so we can use a pointer to it
3783 * as a unique cookie to identify our (NFSv4's) posix locks. */
3784 static const struct lock_manager_operations nfsd_posix_mng_ops = {
3788 nfs4_set_lock_denied(struct file_lock *fl, struct nfsd4_lock_denied *deny)
3790 struct nfs4_lockowner *lo;
3792 if (fl->fl_lmops == &nfsd_posix_mng_ops) {
3793 lo = (struct nfs4_lockowner *) fl->fl_owner;
3794 deny->ld_owner.data = kmemdup(lo->lo_owner.so_owner.data,
3795 lo->lo_owner.so_owner.len, GFP_KERNEL);
3796 if (!deny->ld_owner.data)
3797 /* We just don't care that much */
3799 deny->ld_owner.len = lo->lo_owner.so_owner.len;
3800 deny->ld_clientid = lo->lo_owner.so_client->cl_clientid;
3803 deny->ld_owner.len = 0;
3804 deny->ld_owner.data = NULL;
3805 deny->ld_clientid.cl_boot = 0;
3806 deny->ld_clientid.cl_id = 0;
3808 deny->ld_start = fl->fl_start;
3809 deny->ld_length = NFS4_MAX_UINT64;
3810 if (fl->fl_end != NFS4_MAX_UINT64)
3811 deny->ld_length = fl->fl_end - fl->fl_start + 1;
3812 deny->ld_type = NFS4_READ_LT;
3813 if (fl->fl_type != F_RDLCK)
3814 deny->ld_type = NFS4_WRITE_LT;
3817 static struct nfs4_lockowner *
3818 find_lockowner_str(struct inode *inode, clientid_t *clid,
3819 struct xdr_netobj *owner)
3821 unsigned int hashval = lock_ownerstr_hashval(inode, clid->cl_id, owner);
3822 struct nfs4_stateowner *op;
3824 list_for_each_entry(op, &lock_ownerstr_hashtbl[hashval], so_strhash) {
3825 if (same_owner_str(op, owner, clid))
3826 return lockowner(op);
3831 static void hash_lockowner(struct nfs4_lockowner *lo, unsigned int strhashval, struct nfs4_client *clp, struct nfs4_stateid *open_stp)
3833 unsigned int idhashval;
3835 idhashval = lockownerid_hashval(lo->lo_owner.so_id);
3836 list_add(&lo->lo_owner.so_idhash, &lock_ownerid_hashtbl[idhashval]);
3837 list_add(&lo->lo_owner.so_strhash, &lock_ownerstr_hashtbl[strhashval]);
3838 list_add(&lo->lo_perstateid, &open_stp->st_lockowners);
3842 * Alloc a lock owner structure.
3843 * Called in nfsd4_lock - therefore, OPEN and OPEN_CONFIRM (if needed) has
3846 * strhashval = lock_ownerstr_hashval
3849 static struct nfs4_lockowner *
3850 alloc_init_lock_stateowner(unsigned int strhashval, struct nfs4_client *clp, struct nfs4_stateid *open_stp, struct nfsd4_lock *lock) {
3851 struct nfs4_lockowner *lo;
3853 lo = alloc_stateowner(lockowner_slab, &lock->lk_new_owner, clp);
3856 INIT_LIST_HEAD(&lo->lo_owner.so_stateids);
3857 lo->lo_owner.so_is_open_owner = 0;
3858 /* It is the openowner seqid that will be incremented in encode in the
3859 * case of new lockowners; so increment the lock seqid manually: */
3860 lo->lo_owner.so_seqid = lock->lk_new_lock_seqid + 1;
3861 hash_lockowner(lo, strhashval, clp, open_stp);
3865 static struct nfs4_stateid *
3866 alloc_init_lock_stateid(struct nfs4_lockowner *lo, struct nfs4_file *fp, struct nfs4_stateid *open_stp)
3868 struct nfs4_stateid *stp;
3869 unsigned int hashval = stateid_hashval(lo->lo_owner.so_id, fp->fi_id);
3871 stp = nfs4_alloc_stateid();
3874 list_add(&stp->st_hash, &stateid_hashtbl[hashval]);
3875 list_add(&stp->st_perfile, &fp->fi_stateids);
3876 list_add(&stp->st_perstateowner, &lo->lo_owner.so_stateids);
3877 stp->st_stateowner = &lo->lo_owner;
3878 stp->st_type = NFS4_LOCK_STID;
3881 stp->st_stateid.si_boot = boot_time;
3882 stp->st_stateid.si_stateownerid = lo->lo_owner.so_id;
3883 stp->st_stateid.si_fileid = fp->fi_id;
3884 /* note will be incremented before first return to client: */
3885 stp->st_stateid.si_generation = 0;
3886 stp->st_access_bmap = 0;
3887 stp->st_deny_bmap = open_stp->st_deny_bmap;
3888 stp->st_openstp = open_stp;
3895 check_lock_length(u64 offset, u64 length)
3897 return ((length == 0) || ((length != NFS4_MAX_UINT64) &&
3898 LOFF_OVERFLOW(offset, length)));
3901 static void get_lock_access(struct nfs4_stateid *lock_stp, u32 access)
3903 struct nfs4_file *fp = lock_stp->st_file;
3904 int oflag = nfs4_access_to_omode(access);
3906 if (test_bit(access, &lock_stp->st_access_bmap))
3908 nfs4_file_get_access(fp, oflag);
3909 __set_bit(access, &lock_stp->st_access_bmap);
3916 nfsd4_lock(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
3917 struct nfsd4_lock *lock)
3919 struct nfs4_openowner *open_sop = NULL;
3920 struct nfs4_lockowner *lock_sop = NULL;
3921 struct nfs4_stateid *lock_stp;
3922 struct nfs4_file *fp;
3923 struct file *filp = NULL;
3924 struct file_lock file_lock;
3925 struct file_lock conflock;
3927 unsigned int strhashval;
3931 dprintk("NFSD: nfsd4_lock: start=%Ld length=%Ld\n",
3932 (long long) lock->lk_offset,
3933 (long long) lock->lk_length);
3935 if (check_lock_length(lock->lk_offset, lock->lk_length))
3936 return nfserr_inval;
3938 if ((status = fh_verify(rqstp, &cstate->current_fh,
3939 S_IFREG, NFSD_MAY_LOCK))) {
3940 dprintk("NFSD: nfsd4_lock: permission denied!\n");
3946 if (lock->lk_is_new) {
3948 * Client indicates that this is a new lockowner.
3949 * Use open owner and open stateid to create lock owner and
3952 struct nfs4_stateid *open_stp = NULL;
3954 status = nfserr_stale_clientid;
3955 if (!nfsd4_has_session(cstate) &&
3956 STALE_CLIENTID(&lock->lk_new_clientid))
3959 /* validate and update open stateid and open seqid */
3960 status = nfs4_preprocess_confirmed_seqid_op(cstate,
3961 lock->lk_new_open_seqid,
3962 &lock->lk_new_open_stateid,
3966 open_sop = openowner(open_stp->st_stateowner);
3967 status = nfserr_bad_stateid;
3968 if (!nfsd4_has_session(cstate) &&
3969 !same_clid(&open_sop->oo_owner.so_client->cl_clientid,
3970 &lock->v.new.clientid))
3972 /* create lockowner and lock stateid */
3973 fp = open_stp->st_file;
3974 strhashval = lock_ownerstr_hashval(fp->fi_inode,
3975 open_sop->oo_owner.so_client->cl_clientid.cl_id,
3976 &lock->v.new.owner);
3977 /* XXX: Do we need to check for duplicate stateowners on
3978 * the same file, or should they just be allowed (and
3979 * create new stateids)? */
3980 status = nfserr_jukebox;
3981 lock_sop = alloc_init_lock_stateowner(strhashval,
3982 open_sop->oo_owner.so_client, open_stp, lock);
3983 if (lock_sop == NULL)
3985 lock_stp = alloc_init_lock_stateid(lock_sop, fp, open_stp);
3986 if (lock_stp == NULL)
3989 /* lock (lock owner + lock stateid) already exists */
3990 status = nfs4_preprocess_seqid_op(cstate,
3991 lock->lk_old_lock_seqid,
3992 &lock->lk_old_lock_stateid,
3993 NFS4_LOCK_STID, &lock_stp);
3996 lock_sop = lockowner(lock_stp->st_stateowner);
3997 fp = lock_stp->st_file;
3999 /* lock_sop and lock_stp have been created or found */
4001 lkflg = setlkflg(lock->lk_type);
4002 status = nfs4_check_openmode(lock_stp, lkflg);
4006 status = nfserr_grace;
4007 if (locks_in_grace() && !lock->lk_reclaim)
4009 status = nfserr_no_grace;
4010 if (!locks_in_grace() && lock->lk_reclaim)
4013 locks_init_lock(&file_lock);
4014 switch (lock->lk_type) {
4017 filp = find_readable_file(lock_stp->st_file);
4019 get_lock_access(lock_stp, NFS4_SHARE_ACCESS_READ);
4020 file_lock.fl_type = F_RDLCK;
4023 case NFS4_WRITEW_LT:
4024 filp = find_writeable_file(lock_stp->st_file);
4026 get_lock_access(lock_stp, NFS4_SHARE_ACCESS_WRITE);
4027 file_lock.fl_type = F_WRLCK;
4030 status = nfserr_inval;
4034 status = nfserr_openmode;
4037 file_lock.fl_owner = (fl_owner_t)lock_sop;
4038 file_lock.fl_pid = current->tgid;
4039 file_lock.fl_file = filp;
4040 file_lock.fl_flags = FL_POSIX;
4041 file_lock.fl_lmops = &nfsd_posix_mng_ops;
4043 file_lock.fl_start = lock->lk_offset;
4044 file_lock.fl_end = last_byte_offset(lock->lk_offset, lock->lk_length);
4045 nfs4_transform_lock_offset(&file_lock);
4048 * Try to lock the file in the VFS.
4049 * Note: locks.c uses the BKL to protect the inode's lock list.
4052 err = vfs_lock_file(filp, F_SETLK, &file_lock, &conflock);
4054 case 0: /* success! */
4055 update_stateid(&lock_stp->st_stateid);
4056 memcpy(&lock->lk_resp_stateid, &lock_stp->st_stateid,
4060 case (EAGAIN): /* conflock holds conflicting lock */
4061 status = nfserr_denied;
4062 dprintk("NFSD: nfsd4_lock: conflicting lock found!\n");
4063 nfs4_set_lock_denied(&conflock, &lock->lk_denied);
4066 status = nfserr_deadlock;
4069 dprintk("NFSD: nfsd4_lock: vfs_lock_file() failed! status %d\n",err);
4070 status = nfserrno(err);
4074 if (status && lock->lk_is_new && lock_sop)
4075 release_lockowner(lock_sop);
4076 if (!cstate->replay_owner)
4077 nfs4_unlock_state();
4082 * The NFSv4 spec allows a client to do a LOCKT without holding an OPEN,
4083 * so we do a temporary open here just to get an open file to pass to
4084 * vfs_test_lock. (Arguably perhaps test_lock should be done with an
4087 static int nfsd_test_lock(struct svc_rqst *rqstp, struct svc_fh *fhp, struct file_lock *lock)
4092 err = nfsd_open(rqstp, fhp, S_IFREG, NFSD_MAY_READ, &file);
4095 err = vfs_test_lock(file, lock);
4104 nfsd4_lockt(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
4105 struct nfsd4_lockt *lockt)
4107 struct inode *inode;
4108 struct file_lock file_lock;
4109 struct nfs4_lockowner *lo;
4113 if (locks_in_grace())
4114 return nfserr_grace;
4116 if (check_lock_length(lockt->lt_offset, lockt->lt_length))
4117 return nfserr_inval;
4121 status = nfserr_stale_clientid;
4122 if (!nfsd4_has_session(cstate) && STALE_CLIENTID(&lockt->lt_clientid))
4125 if ((status = fh_verify(rqstp, &cstate->current_fh, S_IFREG, 0)))
4128 inode = cstate->current_fh.fh_dentry->d_inode;
4129 locks_init_lock(&file_lock);
4130 switch (lockt->lt_type) {
4133 file_lock.fl_type = F_RDLCK;
4136 case NFS4_WRITEW_LT:
4137 file_lock.fl_type = F_WRLCK;
4140 dprintk("NFSD: nfs4_lockt: bad lock type!\n");
4141 status = nfserr_inval;
4145 lo = find_lockowner_str(inode, &lockt->lt_clientid, &lockt->lt_owner);
4147 file_lock.fl_owner = (fl_owner_t)lo;
4148 file_lock.fl_pid = current->tgid;
4149 file_lock.fl_flags = FL_POSIX;
4151 file_lock.fl_start = lockt->lt_offset;
4152 file_lock.fl_end = last_byte_offset(lockt->lt_offset, lockt->lt_length);
4154 nfs4_transform_lock_offset(&file_lock);
4157 error = nfsd_test_lock(rqstp, &cstate->current_fh, &file_lock);
4159 status = nfserrno(error);
4162 if (file_lock.fl_type != F_UNLCK) {
4163 status = nfserr_denied;
4164 nfs4_set_lock_denied(&file_lock, &lockt->lt_denied);
4167 nfs4_unlock_state();
4172 nfsd4_locku(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
4173 struct nfsd4_locku *locku)
4175 struct nfs4_stateid *stp;
4176 struct file *filp = NULL;
4177 struct file_lock file_lock;
4181 dprintk("NFSD: nfsd4_locku: start=%Ld length=%Ld\n",
4182 (long long) locku->lu_offset,
4183 (long long) locku->lu_length);
4185 if (check_lock_length(locku->lu_offset, locku->lu_length))
4186 return nfserr_inval;
4190 status = nfs4_preprocess_seqid_op(cstate, locku->lu_seqid,
4191 &locku->lu_stateid, NFS4_LOCK_STID, &stp);
4194 filp = find_any_file(stp->st_file);
4196 status = nfserr_lock_range;
4200 locks_init_lock(&file_lock);
4201 file_lock.fl_type = F_UNLCK;
4202 file_lock.fl_owner = (fl_owner_t)lockowner(stp->st_stateowner);
4203 file_lock.fl_pid = current->tgid;
4204 file_lock.fl_file = filp;
4205 file_lock.fl_flags = FL_POSIX;
4206 file_lock.fl_lmops = &nfsd_posix_mng_ops;
4207 file_lock.fl_start = locku->lu_offset;
4209 file_lock.fl_end = last_byte_offset(locku->lu_offset, locku->lu_length);
4210 nfs4_transform_lock_offset(&file_lock);
4213 * Try to unlock the file in the VFS.
4215 err = vfs_lock_file(filp, F_SETLK, &file_lock, NULL);
4217 dprintk("NFSD: nfs4_locku: vfs_lock_file failed!\n");
4221 * OK, unlock succeeded; the only thing left to do is update the stateid.
4223 update_stateid(&stp->st_stateid);
4224 memcpy(&locku->lu_stateid, &stp->st_stateid, sizeof(stateid_t));
4227 nfs4_unlock_state();
4231 status = nfserrno(err);
4237 * 1: locks held by lockowner
4238 * 0: no locks held by lockowner
4241 check_for_locks(struct nfs4_file *filp, struct nfs4_lockowner *lowner)
4243 struct file_lock **flpp;
4244 struct inode *inode = filp->fi_inode;
4248 for (flpp = &inode->i_flock; *flpp != NULL; flpp = &(*flpp)->fl_next) {
4249 if ((*flpp)->fl_owner == (fl_owner_t)lowner) {
4260 nfsd4_release_lockowner(struct svc_rqst *rqstp,
4261 struct nfsd4_compound_state *cstate,
4262 struct nfsd4_release_lockowner *rlockowner)
4264 clientid_t *clid = &rlockowner->rl_clientid;
4265 struct nfs4_stateowner *sop;
4266 struct nfs4_lockowner *lo;
4267 struct nfs4_stateid *stp;
4268 struct xdr_netobj *owner = &rlockowner->rl_owner;
4269 struct list_head matches;
4273 dprintk("nfsd4_release_lockowner clientid: (%08x/%08x):\n",
4274 clid->cl_boot, clid->cl_id);
4276 /* XXX check for lease expiration */
4278 status = nfserr_stale_clientid;
4279 if (STALE_CLIENTID(clid))
4284 status = nfserr_locks_held;
4285 /* XXX: we're doing a linear search through all the lockowners.
4286 * Yipes! For now we'll just hope clients aren't really using
4287 * release_lockowner much, but eventually we have to fix these
4288 * data structures. */
4289 INIT_LIST_HEAD(&matches);
4290 for (i = 0; i < LOCK_HASH_SIZE; i++) {
4291 list_for_each_entry(sop, &lock_ownerid_hashtbl[i], so_idhash) {
4292 if (!same_owner_str(sop, owner, clid))
4294 list_for_each_entry(stp, &sop->so_stateids,
4296 lo = lockowner(sop);
4297 if (check_for_locks(stp->st_file, lo))
4299 list_add(&lo->lo_list, &matches);
4303 /* Clients probably won't expect us to return with some (but not all)
4304 * of the lockowner state released; so don't release any until all
4305 * have been checked. */
4307 while (!list_empty(&matches)) {
4308 lo = list_entry(matches.next, struct nfs4_lockowner,
4310 /* unhash_stateowner deletes so_perclient only
4311 * for openowners. */
4312 list_del(&lo->lo_list);
4313 release_lockowner(lo);
4316 nfs4_unlock_state();
4320 static inline struct nfs4_client_reclaim *
4323 return kmalloc(sizeof(struct nfs4_client_reclaim), GFP_KERNEL);
4327 nfs4_has_reclaimed_state(const char *name, bool use_exchange_id)
4329 unsigned int strhashval = clientstr_hashval(name);
4330 struct nfs4_client *clp;
4332 clp = find_confirmed_client_by_str(name, strhashval);
4337 * failure => all reset bets are off, nfserr_no_grace...
4340 nfs4_client_to_reclaim(const char *name)
4342 unsigned int strhashval;
4343 struct nfs4_client_reclaim *crp = NULL;
4345 dprintk("NFSD nfs4_client_to_reclaim NAME: %.*s\n", HEXDIR_LEN, name);
4346 crp = alloc_reclaim();
4349 strhashval = clientstr_hashval(name);
4350 INIT_LIST_HEAD(&crp->cr_strhash);
4351 list_add(&crp->cr_strhash, &reclaim_str_hashtbl[strhashval]);
4352 memcpy(crp->cr_recdir, name, HEXDIR_LEN);
4353 reclaim_str_hashtbl_size++;
4358 nfs4_release_reclaim(void)
4360 struct nfs4_client_reclaim *crp = NULL;
4363 for (i = 0; i < CLIENT_HASH_SIZE; i++) {
4364 while (!list_empty(&reclaim_str_hashtbl[i])) {
4365 crp = list_entry(reclaim_str_hashtbl[i].next,
4366 struct nfs4_client_reclaim, cr_strhash);
4367 list_del(&crp->cr_strhash);
4369 reclaim_str_hashtbl_size--;
4372 BUG_ON(reclaim_str_hashtbl_size);
4376 * called from OPEN, CLAIM_PREVIOUS with a new clientid. */
4377 static struct nfs4_client_reclaim *
4378 nfs4_find_reclaim_client(clientid_t *clid)
4380 unsigned int strhashval;
4381 struct nfs4_client *clp;
4382 struct nfs4_client_reclaim *crp = NULL;
4385 /* find clientid in conf_id_hashtbl */
4386 clp = find_confirmed_client(clid);
4390 dprintk("NFSD: nfs4_find_reclaim_client for %.*s with recdir %s\n",
4391 clp->cl_name.len, clp->cl_name.data,
4394 /* find clp->cl_name in reclaim_str_hashtbl */
4395 strhashval = clientstr_hashval(clp->cl_recdir);
4396 list_for_each_entry(crp, &reclaim_str_hashtbl[strhashval], cr_strhash) {
4397 if (same_name(crp->cr_recdir, clp->cl_recdir)) {
4405 * Called from OPEN. Look for clientid in reclaim list.
4408 nfs4_check_open_reclaim(clientid_t *clid)
4410 return nfs4_find_reclaim_client(clid) ? nfs_ok : nfserr_reclaim_bad;
4413 /* initialization to perform at module load time: */
4416 nfs4_state_init(void)
4420 status = nfsd4_init_slabs();
4423 for (i = 0; i < CLIENT_HASH_SIZE; i++) {
4424 INIT_LIST_HEAD(&conf_id_hashtbl[i]);
4425 INIT_LIST_HEAD(&conf_str_hashtbl[i]);
4426 INIT_LIST_HEAD(&unconf_str_hashtbl[i]);
4427 INIT_LIST_HEAD(&unconf_id_hashtbl[i]);
4428 INIT_LIST_HEAD(&reclaim_str_hashtbl[i]);
4430 for (i = 0; i < SESSION_HASH_SIZE; i++)
4431 INIT_LIST_HEAD(&sessionid_hashtbl[i]);
4432 for (i = 0; i < FILE_HASH_SIZE; i++) {
4433 INIT_LIST_HEAD(&file_hashtbl[i]);
4435 for (i = 0; i < OPEN_OWNER_HASH_SIZE; i++) {
4436 INIT_LIST_HEAD(&open_ownerstr_hashtbl[i]);
4437 INIT_LIST_HEAD(&open_ownerid_hashtbl[i]);
4439 for (i = 0; i < STATEID_HASH_SIZE; i++)
4440 INIT_LIST_HEAD(&stateid_hashtbl[i]);
4441 for (i = 0; i < LOCK_HASH_SIZE; i++) {
4442 INIT_LIST_HEAD(&lock_ownerid_hashtbl[i]);
4443 INIT_LIST_HEAD(&lock_ownerstr_hashtbl[i]);
4445 memset(&onestateid, ~0, sizeof(stateid_t));
4446 INIT_LIST_HEAD(&close_lru);
4447 INIT_LIST_HEAD(&client_lru);
4448 INIT_LIST_HEAD(&del_recall_lru);
4449 reclaim_str_hashtbl_size = 0;
4454 nfsd4_load_reboot_recovery_data(void)
4459 nfsd4_init_recdir();
4460 status = nfsd4_recdir_load();
4461 nfs4_unlock_state();
4463 printk("NFSD: Failure reading reboot recovery data\n");
4467 * Since the lifetime of a delegation isn't limited to that of an open, a
4468 * client may quite reasonably hang on to a delegation as long as it has
4469 * the inode cached. This becomes an obvious problem the first time a
4470 * client's inode cache approaches the size of the server's total memory.
4472 * For now we avoid this problem by imposing a hard limit on the number
4473 * of delegations, which varies according to the server's memory size.
4476 set_max_delegations(void)
4479 * Allow at most 4 delegations per megabyte of RAM. Quick
4480 * estimates suggest that in the worst case (where every delegation
4481 * is for a different inode), a delegation could take about 1.5K,
4482 * giving a worst case usage of about 6% of memory.
4484 max_delegations = nr_free_buffer_pages() >> (20 - 2 - PAGE_SHIFT);
4487 /* initialization to perform when the nfsd service is started: */
4490 __nfs4_state_start(void)
4494 boot_time = get_seconds();
4495 locks_start_grace(&nfsd4_manager);
4496 printk(KERN_INFO "NFSD: starting %ld-second grace period\n",
4498 ret = set_callback_cred();
4501 laundry_wq = create_singlethread_workqueue("nfsd4");
4502 if (laundry_wq == NULL)
4504 ret = nfsd4_create_callback_queue();
4506 goto out_free_laundry;
4507 queue_delayed_work(laundry_wq, &laundromat_work, nfsd4_grace * HZ);
4508 set_max_delegations();
4511 destroy_workqueue(laundry_wq);
4516 nfs4_state_start(void)
4518 nfsd4_load_reboot_recovery_data();
4519 return __nfs4_state_start();
4523 __nfs4_state_shutdown(void)
4526 struct nfs4_client *clp = NULL;
4527 struct nfs4_delegation *dp = NULL;
4528 struct list_head *pos, *next, reaplist;
4530 for (i = 0; i < CLIENT_HASH_SIZE; i++) {
4531 while (!list_empty(&conf_id_hashtbl[i])) {
4532 clp = list_entry(conf_id_hashtbl[i].next, struct nfs4_client, cl_idhash);
4535 while (!list_empty(&unconf_str_hashtbl[i])) {
4536 clp = list_entry(unconf_str_hashtbl[i].next, struct nfs4_client, cl_strhash);
4540 INIT_LIST_HEAD(&reaplist);
4541 spin_lock(&recall_lock);
4542 list_for_each_safe(pos, next, &del_recall_lru) {
4543 dp = list_entry (pos, struct nfs4_delegation, dl_recall_lru);
4544 list_move(&dp->dl_recall_lru, &reaplist);
4546 spin_unlock(&recall_lock);
4547 list_for_each_safe(pos, next, &reaplist) {
4548 dp = list_entry (pos, struct nfs4_delegation, dl_recall_lru);
4549 list_del_init(&dp->dl_recall_lru);
4550 unhash_delegation(dp);
4553 nfsd4_shutdown_recdir();
4557 nfs4_state_shutdown(void)
4559 cancel_delayed_work_sync(&laundromat_work);
4560 destroy_workqueue(laundry_wq);
4561 locks_end_grace(&nfsd4_manager);
4563 nfs4_release_reclaim();
4564 __nfs4_state_shutdown();
4565 nfs4_unlock_state();
4566 nfsd4_destroy_callback_queue();