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 stateid_t zerostateid; /* bits all 0 */
53 static stateid_t onestateid; /* bits all 1 */
54 static u64 current_sessionid = 1;
56 #define ZERO_STATEID(stateid) (!memcmp((stateid), &zerostateid, sizeof(stateid_t)))
57 #define ONE_STATEID(stateid) (!memcmp((stateid), &onestateid, sizeof(stateid_t)))
59 /* forward declarations */
60 static int check_for_locks(struct nfs4_file *filp, struct nfs4_lockowner *lowner);
64 /* Currently used for almost all code touching nfsv4 state: */
65 static DEFINE_MUTEX(client_mutex);
68 * Currently used for the del_recall_lru and file hash table. In an
69 * effort to decrease the scope of the client_mutex, this spinlock may
70 * eventually cover more:
72 static DEFINE_SPINLOCK(recall_lock);
74 static struct kmem_cache *openowner_slab = NULL;
75 static struct kmem_cache *lockowner_slab = NULL;
76 static struct kmem_cache *file_slab = NULL;
77 static struct kmem_cache *stateid_slab = NULL;
78 static struct kmem_cache *deleg_slab = NULL;
83 mutex_lock(&client_mutex);
87 nfs4_unlock_state(void)
89 mutex_unlock(&client_mutex);
93 opaque_hashval(const void *ptr, int nbytes)
95 unsigned char *cptr = (unsigned char *) ptr;
105 static struct list_head del_recall_lru;
107 static void nfsd4_free_file(struct nfs4_file *f)
109 kmem_cache_free(file_slab, f);
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);
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_ownerstr_hashval(u32 clientid, struct xdr_netobj *ownername)
145 ret = opaque_hashval(ownername->data, ownername->len);
147 return ret & OPEN_OWNER_HASH_MASK;
150 static struct list_head open_ownerstr_hashtbl[OPEN_OWNER_HASH_SIZE];
152 /* hash table for nfs4_file */
153 #define FILE_HASH_BITS 8
154 #define FILE_HASH_SIZE (1 << FILE_HASH_BITS)
156 static unsigned int file_hashval(struct inode *ino)
158 /* XXX: why are we hashing on inode pointer, anyway? */
159 return hash_ptr(ino, FILE_HASH_BITS);
162 static struct list_head file_hashtbl[FILE_HASH_SIZE];
164 static void __nfs4_file_get_access(struct nfs4_file *fp, int oflag)
166 BUG_ON(!(fp->fi_fds[oflag] || fp->fi_fds[O_RDWR]));
167 atomic_inc(&fp->fi_access[oflag]);
170 static void nfs4_file_get_access(struct nfs4_file *fp, int oflag)
172 if (oflag == O_RDWR) {
173 __nfs4_file_get_access(fp, O_RDONLY);
174 __nfs4_file_get_access(fp, O_WRONLY);
176 __nfs4_file_get_access(fp, oflag);
179 static void nfs4_file_put_fd(struct nfs4_file *fp, int oflag)
181 if (fp->fi_fds[oflag]) {
182 fput(fp->fi_fds[oflag]);
183 fp->fi_fds[oflag] = NULL;
187 static void __nfs4_file_put_access(struct nfs4_file *fp, int oflag)
189 if (atomic_dec_and_test(&fp->fi_access[oflag])) {
190 nfs4_file_put_fd(fp, oflag);
191 if (atomic_read(&fp->fi_access[1 - oflag]) == 0)
192 nfs4_file_put_fd(fp, O_RDWR);
196 static void nfs4_file_put_access(struct nfs4_file *fp, int oflag)
198 if (oflag == O_RDWR) {
199 __nfs4_file_put_access(fp, O_RDONLY);
200 __nfs4_file_put_access(fp, O_WRONLY);
202 __nfs4_file_put_access(fp, oflag);
205 static inline int get_new_stid(struct nfs4_stid *stid)
207 static int min_stateid = 0;
208 struct idr *stateids = &stid->sc_client->cl_stateids;
212 error = idr_get_new_above(stateids, stid, min_stateid, &new_stid);
214 * Note: the necessary preallocation was done in
215 * nfs4_alloc_stateid(). The idr code caps the number of
216 * preallocations that can exist at a time, but the state lock
217 * prevents anyone from using ours before we get here:
221 * It shouldn't be a problem to reuse an opaque stateid value.
222 * I don't think it is for 4.1. But with 4.0 I worry that, for
223 * example, a stray write retransmission could be accepted by
224 * the server when it should have been rejected. Therefore,
225 * adopt a trick from the sctp code to attempt to maximize the
226 * amount of time until an id is reused, by ensuring they always
227 * "increase" (mod INT_MAX):
230 min_stateid = new_stid+1;
231 if (min_stateid == INT_MAX)
236 static void init_stid(struct nfs4_stid *stid, struct nfs4_client *cl, unsigned char type)
238 stateid_t *s = &stid->sc_stateid;
241 stid->sc_type = type;
242 stid->sc_client = cl;
243 s->si_opaque.so_clid = cl->cl_clientid;
244 new_id = get_new_stid(stid);
245 s->si_opaque.so_id = (u32)new_id;
246 /* Will be incremented before return to client: */
247 s->si_generation = 0;
250 static struct nfs4_stid *nfs4_alloc_stid(struct nfs4_client *cl, struct kmem_cache *slab)
252 struct idr *stateids = &cl->cl_stateids;
254 if (!idr_pre_get(stateids, GFP_KERNEL))
257 * Note: if we fail here (or any time between now and the time
258 * we actually get the new idr), we won't need to undo the idr
259 * preallocation, since the idr code caps the number of
260 * preallocated entries.
262 return kmem_cache_alloc(slab, GFP_KERNEL);
265 static struct nfs4_ol_stateid * nfs4_alloc_stateid(struct nfs4_client *clp)
267 return openlockstateid(nfs4_alloc_stid(clp, stateid_slab));
270 static struct nfs4_delegation *
271 alloc_init_deleg(struct nfs4_client *clp, struct nfs4_ol_stateid *stp, struct svc_fh *current_fh, u32 type)
273 struct nfs4_delegation *dp;
274 struct nfs4_file *fp = stp->st_file;
276 dprintk("NFSD alloc_init_deleg\n");
278 * Major work on the lease subsystem (for example, to support
279 * calbacks on stat) will be required before we can support
280 * write delegations properly.
282 if (type != NFS4_OPEN_DELEGATE_READ)
284 if (fp->fi_had_conflict)
286 if (num_delegations > max_delegations)
288 dp = delegstateid(nfs4_alloc_stid(clp, deleg_slab));
291 init_stid(&dp->dl_stid, clp, NFS4_DELEG_STID);
293 * delegation seqid's are never incremented. The 4.1 special
294 * meaning of seqid 0 isn't meaningful, really, but let's avoid
295 * 0 anyway just for consistency and use 1:
297 dp->dl_stid.sc_stateid.si_generation = 1;
299 INIT_LIST_HEAD(&dp->dl_perfile);
300 INIT_LIST_HEAD(&dp->dl_perclnt);
301 INIT_LIST_HEAD(&dp->dl_recall_lru);
305 fh_copy_shallow(&dp->dl_fh, ¤t_fh->fh_handle);
307 atomic_set(&dp->dl_count, 1);
308 INIT_WORK(&dp->dl_recall.cb_work, nfsd4_do_callback_rpc);
313 nfs4_put_delegation(struct nfs4_delegation *dp)
315 if (atomic_dec_and_test(&dp->dl_count)) {
316 dprintk("NFSD: freeing dp %p\n",dp);
317 put_nfs4_file(dp->dl_file);
318 kmem_cache_free(deleg_slab, dp);
323 static void nfs4_put_deleg_lease(struct nfs4_file *fp)
325 if (atomic_dec_and_test(&fp->fi_delegees)) {
326 vfs_setlease(fp->fi_deleg_file, F_UNLCK, &fp->fi_lease);
328 fput(fp->fi_deleg_file);
329 fp->fi_deleg_file = NULL;
333 static void unhash_stid(struct nfs4_stid *s)
335 struct idr *stateids = &s->sc_client->cl_stateids;
337 idr_remove(stateids, s->sc_stateid.si_opaque.so_id);
341 hash_delegation_locked(struct nfs4_delegation *dp, struct nfs4_file *fp)
343 lockdep_assert_held(&recall_lock);
345 list_add(&dp->dl_perfile, &fp->fi_delegations);
346 list_add(&dp->dl_perclnt, &dp->dl_stid.sc_client->cl_delegations);
349 /* Called under the state lock. */
351 unhash_delegation(struct nfs4_delegation *dp)
353 unhash_stid(&dp->dl_stid);
354 spin_lock(&recall_lock);
355 list_del_init(&dp->dl_perclnt);
356 list_del_init(&dp->dl_perfile);
357 list_del_init(&dp->dl_recall_lru);
358 spin_unlock(&recall_lock);
359 nfs4_put_deleg_lease(dp->dl_file);
360 nfs4_put_delegation(dp);
367 /* client_lock protects the client lru list and session hash table */
368 static DEFINE_SPINLOCK(client_lock);
370 /* Hash tables for nfs4_clientid state */
371 #define CLIENT_HASH_BITS 4
372 #define CLIENT_HASH_SIZE (1 << CLIENT_HASH_BITS)
373 #define CLIENT_HASH_MASK (CLIENT_HASH_SIZE - 1)
375 static unsigned int clientid_hashval(u32 id)
377 return id & CLIENT_HASH_MASK;
380 static unsigned int clientstr_hashval(const char *name)
382 return opaque_hashval(name, 8) & CLIENT_HASH_MASK;
386 * reclaim_str_hashtbl[] holds known client info from previous reset/reboot
387 * used in reboot/reset lease grace period processing
389 * conf_id_hashtbl[], and conf_str_hashtbl[] hold confirmed
390 * setclientid_confirmed info.
392 * unconf_str_hastbl[] and unconf_id_hashtbl[] hold unconfirmed
395 * client_lru holds client queue ordered by nfs4_client.cl_time
398 * close_lru holds (open) stateowner queue ordered by nfs4_stateowner.so_time
399 * for last close replay.
401 static struct list_head reclaim_str_hashtbl[CLIENT_HASH_SIZE];
402 static int reclaim_str_hashtbl_size = 0;
403 static struct list_head conf_id_hashtbl[CLIENT_HASH_SIZE];
404 static struct list_head conf_str_hashtbl[CLIENT_HASH_SIZE];
405 static struct list_head unconf_str_hashtbl[CLIENT_HASH_SIZE];
406 static struct list_head unconf_id_hashtbl[CLIENT_HASH_SIZE];
407 static struct list_head client_lru;
408 static struct list_head close_lru;
411 * We store the NONE, READ, WRITE, and BOTH bits separately in the
412 * st_{access,deny}_bmap field of the stateid, in order to track not
413 * only what share bits are currently in force, but also what
414 * combinations of share bits previous opens have used. This allows us
415 * to enforce the recommendation of rfc 3530 14.2.19 that the server
416 * return an error if the client attempt to downgrade to a combination
417 * of share bits not explicable by closing some of its previous opens.
419 * XXX: This enforcement is actually incomplete, since we don't keep
420 * track of access/deny bit combinations; so, e.g., we allow:
422 * OPEN allow read, deny write
423 * OPEN allow both, deny none
424 * DOWNGRADE allow read, deny none
426 * which we should reject.
429 set_access(unsigned int *access, unsigned long bmap) {
433 for (i = 1; i < 4; i++) {
434 if (test_bit(i, &bmap))
440 set_deny(unsigned int *deny, unsigned long bmap) {
444 for (i = 0; i < 4; i++) {
445 if (test_bit(i, &bmap))
451 test_share(struct nfs4_ol_stateid *stp, struct nfsd4_open *open) {
452 unsigned int access, deny;
454 set_access(&access, stp->st_access_bmap);
455 set_deny(&deny, stp->st_deny_bmap);
456 if ((access & open->op_share_deny) || (deny & open->op_share_access))
461 static int nfs4_access_to_omode(u32 access)
463 switch (access & NFS4_SHARE_ACCESS_BOTH) {
464 case NFS4_SHARE_ACCESS_READ:
466 case NFS4_SHARE_ACCESS_WRITE:
468 case NFS4_SHARE_ACCESS_BOTH:
474 static void unhash_generic_stateid(struct nfs4_ol_stateid *stp)
476 list_del(&stp->st_perfile);
477 list_del(&stp->st_perstateowner);
480 static void close_generic_stateid(struct nfs4_ol_stateid *stp)
484 if (stp->st_access_bmap) {
485 for (i = 1; i < 4; i++) {
486 if (test_bit(i, &stp->st_access_bmap))
487 nfs4_file_put_access(stp->st_file,
488 nfs4_access_to_omode(i));
489 __clear_bit(i, &stp->st_access_bmap);
492 put_nfs4_file(stp->st_file);
496 static void free_generic_stateid(struct nfs4_ol_stateid *stp)
498 kmem_cache_free(stateid_slab, stp);
501 static void release_lock_stateid(struct nfs4_ol_stateid *stp)
505 unhash_generic_stateid(stp);
506 unhash_stid(&stp->st_stid);
507 file = find_any_file(stp->st_file);
509 locks_remove_posix(file, (fl_owner_t)lockowner(stp->st_stateowner));
510 close_generic_stateid(stp);
511 free_generic_stateid(stp);
514 static void unhash_lockowner(struct nfs4_lockowner *lo)
516 struct nfs4_ol_stateid *stp;
518 list_del(&lo->lo_owner.so_strhash);
519 list_del(&lo->lo_perstateid);
520 while (!list_empty(&lo->lo_owner.so_stateids)) {
521 stp = list_first_entry(&lo->lo_owner.so_stateids,
522 struct nfs4_ol_stateid, st_perstateowner);
523 release_lock_stateid(stp);
527 static void release_lockowner(struct nfs4_lockowner *lo)
529 unhash_lockowner(lo);
530 nfs4_free_lockowner(lo);
534 release_stateid_lockowners(struct nfs4_ol_stateid *open_stp)
536 struct nfs4_lockowner *lo;
538 while (!list_empty(&open_stp->st_lockowners)) {
539 lo = list_entry(open_stp->st_lockowners.next,
540 struct nfs4_lockowner, lo_perstateid);
541 release_lockowner(lo);
545 static void unhash_open_stateid(struct nfs4_ol_stateid *stp)
547 unhash_generic_stateid(stp);
548 release_stateid_lockowners(stp);
549 close_generic_stateid(stp);
552 static void release_open_stateid(struct nfs4_ol_stateid *stp)
554 unhash_open_stateid(stp);
555 unhash_stid(&stp->st_stid);
556 free_generic_stateid(stp);
559 static void unhash_openowner(struct nfs4_openowner *oo)
561 struct nfs4_ol_stateid *stp;
563 list_del(&oo->oo_owner.so_strhash);
564 list_del(&oo->oo_perclient);
565 while (!list_empty(&oo->oo_owner.so_stateids)) {
566 stp = list_first_entry(&oo->oo_owner.so_stateids,
567 struct nfs4_ol_stateid, st_perstateowner);
568 release_open_stateid(stp);
572 static void release_last_closed_stateid(struct nfs4_openowner *oo)
574 struct nfs4_ol_stateid *s = oo->oo_last_closed_stid;
577 unhash_stid(&s->st_stid);
578 free_generic_stateid(s);
579 oo->oo_last_closed_stid = NULL;
583 static void release_openowner(struct nfs4_openowner *oo)
585 unhash_openowner(oo);
586 list_del(&oo->oo_close_lru);
587 release_last_closed_stateid(oo);
588 nfs4_free_openowner(oo);
591 #define SESSION_HASH_SIZE 512
592 static struct list_head sessionid_hashtbl[SESSION_HASH_SIZE];
595 hash_sessionid(struct nfs4_sessionid *sessionid)
597 struct nfsd4_sessionid *sid = (struct nfsd4_sessionid *)sessionid;
599 return sid->sequence % SESSION_HASH_SIZE;
603 dump_sessionid(const char *fn, struct nfs4_sessionid *sessionid)
605 u32 *ptr = (u32 *)(&sessionid->data[0]);
606 dprintk("%s: %u:%u:%u:%u\n", fn, ptr[0], ptr[1], ptr[2], ptr[3]);
610 gen_sessionid(struct nfsd4_session *ses)
612 struct nfs4_client *clp = ses->se_client;
613 struct nfsd4_sessionid *sid;
615 sid = (struct nfsd4_sessionid *)ses->se_sessionid.data;
616 sid->clientid = clp->cl_clientid;
617 sid->sequence = current_sessionid++;
622 * The protocol defines ca_maxresponssize_cached to include the size of
623 * the rpc header, but all we need to cache is the data starting after
624 * the end of the initial SEQUENCE operation--the rest we regenerate
625 * each time. Therefore we can advertise a ca_maxresponssize_cached
626 * value that is the number of bytes in our cache plus a few additional
627 * bytes. In order to stay on the safe side, and not promise more than
628 * we can cache, those additional bytes must be the minimum possible: 24
629 * bytes of rpc header (xid through accept state, with AUTH_NULL
630 * verifier), 12 for the compound header (with zero-length tag), and 44
631 * for the SEQUENCE op response:
633 #define NFSD_MIN_HDR_SEQ_SZ (24 + 12 + 44)
636 free_session_slots(struct nfsd4_session *ses)
640 for (i = 0; i < ses->se_fchannel.maxreqs; i++)
641 kfree(ses->se_slots[i]);
645 * We don't actually need to cache the rpc and session headers, so we
646 * can allocate a little less for each slot:
648 static inline int slot_bytes(struct nfsd4_channel_attrs *ca)
650 return ca->maxresp_cached - NFSD_MIN_HDR_SEQ_SZ;
653 static int nfsd4_sanitize_slot_size(u32 size)
655 size -= NFSD_MIN_HDR_SEQ_SZ; /* We don't cache the rpc header */
656 size = min_t(u32, size, NFSD_SLOT_CACHE_SIZE);
662 * XXX: If we run out of reserved DRC memory we could (up to a point)
663 * re-negotiate active sessions and reduce their slot usage to make
664 * rooom for new connections. For now we just fail the create session.
666 static int nfsd4_get_drc_mem(int slotsize, u32 num)
670 num = min_t(u32, num, NFSD_MAX_SLOTS_PER_SESSION);
672 spin_lock(&nfsd_drc_lock);
673 avail = min_t(int, NFSD_MAX_MEM_PER_SESSION,
674 nfsd_drc_max_mem - nfsd_drc_mem_used);
675 num = min_t(int, num, avail / slotsize);
676 nfsd_drc_mem_used += num * slotsize;
677 spin_unlock(&nfsd_drc_lock);
682 static void nfsd4_put_drc_mem(int slotsize, int num)
684 spin_lock(&nfsd_drc_lock);
685 nfsd_drc_mem_used -= slotsize * num;
686 spin_unlock(&nfsd_drc_lock);
689 static struct nfsd4_session *alloc_session(int slotsize, int numslots)
691 struct nfsd4_session *new;
694 BUILD_BUG_ON(NFSD_MAX_SLOTS_PER_SESSION * sizeof(struct nfsd4_slot *)
695 + sizeof(struct nfsd4_session) > PAGE_SIZE);
696 mem = numslots * sizeof(struct nfsd4_slot *);
698 new = kzalloc(sizeof(*new) + mem, GFP_KERNEL);
701 /* allocate each struct nfsd4_slot and data cache in one piece */
702 for (i = 0; i < numslots; i++) {
703 mem = sizeof(struct nfsd4_slot) + slotsize;
704 new->se_slots[i] = kzalloc(mem, GFP_KERNEL);
705 if (!new->se_slots[i])
711 kfree(new->se_slots[i]);
716 static void init_forechannel_attrs(struct nfsd4_channel_attrs *new, struct nfsd4_channel_attrs *req, int numslots, int slotsize)
718 u32 maxrpc = nfsd_serv->sv_max_mesg;
720 new->maxreqs = numslots;
721 new->maxresp_cached = min_t(u32, req->maxresp_cached,
722 slotsize + NFSD_MIN_HDR_SEQ_SZ);
723 new->maxreq_sz = min_t(u32, req->maxreq_sz, maxrpc);
724 new->maxresp_sz = min_t(u32, req->maxresp_sz, maxrpc);
725 new->maxops = min_t(u32, req->maxops, NFSD_MAX_OPS_PER_COMPOUND);
728 static void free_conn(struct nfsd4_conn *c)
730 svc_xprt_put(c->cn_xprt);
734 static void nfsd4_conn_lost(struct svc_xpt_user *u)
736 struct nfsd4_conn *c = container_of(u, struct nfsd4_conn, cn_xpt_user);
737 struct nfs4_client *clp = c->cn_session->se_client;
739 spin_lock(&clp->cl_lock);
740 if (!list_empty(&c->cn_persession)) {
741 list_del(&c->cn_persession);
744 spin_unlock(&clp->cl_lock);
745 nfsd4_probe_callback(clp);
748 static struct nfsd4_conn *alloc_conn(struct svc_rqst *rqstp, u32 flags)
750 struct nfsd4_conn *conn;
752 conn = kmalloc(sizeof(struct nfsd4_conn), GFP_KERNEL);
755 svc_xprt_get(rqstp->rq_xprt);
756 conn->cn_xprt = rqstp->rq_xprt;
757 conn->cn_flags = flags;
758 INIT_LIST_HEAD(&conn->cn_xpt_user.list);
762 static void __nfsd4_hash_conn(struct nfsd4_conn *conn, struct nfsd4_session *ses)
764 conn->cn_session = ses;
765 list_add(&conn->cn_persession, &ses->se_conns);
768 static void nfsd4_hash_conn(struct nfsd4_conn *conn, struct nfsd4_session *ses)
770 struct nfs4_client *clp = ses->se_client;
772 spin_lock(&clp->cl_lock);
773 __nfsd4_hash_conn(conn, ses);
774 spin_unlock(&clp->cl_lock);
777 static int nfsd4_register_conn(struct nfsd4_conn *conn)
779 conn->cn_xpt_user.callback = nfsd4_conn_lost;
780 return register_xpt_user(conn->cn_xprt, &conn->cn_xpt_user);
783 static __be32 nfsd4_new_conn(struct svc_rqst *rqstp, struct nfsd4_session *ses, u32 dir)
785 struct nfsd4_conn *conn;
788 conn = alloc_conn(rqstp, dir);
790 return nfserr_jukebox;
791 nfsd4_hash_conn(conn, ses);
792 ret = nfsd4_register_conn(conn);
794 /* oops; xprt is already down: */
795 nfsd4_conn_lost(&conn->cn_xpt_user);
799 static __be32 nfsd4_new_conn_from_crses(struct svc_rqst *rqstp, struct nfsd4_session *ses)
801 u32 dir = NFS4_CDFC4_FORE;
803 if (ses->se_flags & SESSION4_BACK_CHAN)
804 dir |= NFS4_CDFC4_BACK;
806 return nfsd4_new_conn(rqstp, ses, dir);
809 /* must be called under client_lock */
810 static void nfsd4_del_conns(struct nfsd4_session *s)
812 struct nfs4_client *clp = s->se_client;
813 struct nfsd4_conn *c;
815 spin_lock(&clp->cl_lock);
816 while (!list_empty(&s->se_conns)) {
817 c = list_first_entry(&s->se_conns, struct nfsd4_conn, cn_persession);
818 list_del_init(&c->cn_persession);
819 spin_unlock(&clp->cl_lock);
821 unregister_xpt_user(c->cn_xprt, &c->cn_xpt_user);
824 spin_lock(&clp->cl_lock);
826 spin_unlock(&clp->cl_lock);
829 void free_session(struct kref *kref)
831 struct nfsd4_session *ses;
834 ses = container_of(kref, struct nfsd4_session, se_ref);
835 nfsd4_del_conns(ses);
836 spin_lock(&nfsd_drc_lock);
837 mem = ses->se_fchannel.maxreqs * slot_bytes(&ses->se_fchannel);
838 nfsd_drc_mem_used -= mem;
839 spin_unlock(&nfsd_drc_lock);
840 free_session_slots(ses);
844 static struct nfsd4_session *alloc_init_session(struct svc_rqst *rqstp, struct nfs4_client *clp, struct nfsd4_create_session *cses)
846 struct nfsd4_session *new;
847 struct nfsd4_channel_attrs *fchan = &cses->fore_channel;
848 int numslots, slotsize;
853 * Note decreasing slot size below client's request may
854 * make it difficult for client to function correctly, whereas
855 * decreasing the number of slots will (just?) affect
856 * performance. When short on memory we therefore prefer to
857 * decrease number of slots instead of their size.
859 slotsize = nfsd4_sanitize_slot_size(fchan->maxresp_cached);
860 numslots = nfsd4_get_drc_mem(slotsize, fchan->maxreqs);
864 new = alloc_session(slotsize, numslots);
866 nfsd4_put_drc_mem(slotsize, fchan->maxreqs);
869 init_forechannel_attrs(&new->se_fchannel, fchan, numslots, slotsize);
871 new->se_client = clp;
874 INIT_LIST_HEAD(&new->se_conns);
876 new->se_cb_seq_nr = 1;
877 new->se_flags = cses->flags;
878 new->se_cb_prog = cses->callback_prog;
879 kref_init(&new->se_ref);
880 idx = hash_sessionid(&new->se_sessionid);
881 spin_lock(&client_lock);
882 list_add(&new->se_hash, &sessionid_hashtbl[idx]);
883 spin_lock(&clp->cl_lock);
884 list_add(&new->se_perclnt, &clp->cl_sessions);
885 spin_unlock(&clp->cl_lock);
886 spin_unlock(&client_lock);
888 status = nfsd4_new_conn_from_crses(rqstp, new);
889 /* whoops: benny points out, status is ignored! (err, or bogus) */
891 free_session(&new->se_ref);
894 if (cses->flags & SESSION4_BACK_CHAN) {
895 struct sockaddr *sa = svc_addr(rqstp);
897 * This is a little silly; with sessions there's no real
898 * use for the callback address. Use the peer address
899 * as a reasonable default for now, but consider fixing
900 * the rpc client not to require an address in the
903 rpc_copy_addr((struct sockaddr *)&clp->cl_cb_conn.cb_addr, sa);
904 clp->cl_cb_conn.cb_addrlen = svc_addr_len(sa);
906 nfsd4_probe_callback(clp);
910 /* caller must hold client_lock */
911 static struct nfsd4_session *
912 find_in_sessionid_hashtbl(struct nfs4_sessionid *sessionid)
914 struct nfsd4_session *elem;
917 dump_sessionid(__func__, sessionid);
918 idx = hash_sessionid(sessionid);
919 /* Search in the appropriate list */
920 list_for_each_entry(elem, &sessionid_hashtbl[idx], se_hash) {
921 if (!memcmp(elem->se_sessionid.data, sessionid->data,
922 NFS4_MAX_SESSIONID_LEN)) {
927 dprintk("%s: session not found\n", __func__);
931 /* caller must hold client_lock */
933 unhash_session(struct nfsd4_session *ses)
935 list_del(&ses->se_hash);
936 spin_lock(&ses->se_client->cl_lock);
937 list_del(&ses->se_perclnt);
938 spin_unlock(&ses->se_client->cl_lock);
941 /* must be called under the client_lock */
943 renew_client_locked(struct nfs4_client *clp)
945 if (is_client_expired(clp)) {
946 dprintk("%s: client (clientid %08x/%08x) already expired\n",
948 clp->cl_clientid.cl_boot,
949 clp->cl_clientid.cl_id);
953 dprintk("renewing client (clientid %08x/%08x)\n",
954 clp->cl_clientid.cl_boot,
955 clp->cl_clientid.cl_id);
956 list_move_tail(&clp->cl_lru, &client_lru);
957 clp->cl_time = get_seconds();
961 renew_client(struct nfs4_client *clp)
963 spin_lock(&client_lock);
964 renew_client_locked(clp);
965 spin_unlock(&client_lock);
968 /* SETCLIENTID and SETCLIENTID_CONFIRM Helper functions */
970 STALE_CLIENTID(clientid_t *clid)
972 if (clid->cl_boot == boot_time)
974 dprintk("NFSD stale clientid (%08x/%08x) boot_time %08lx\n",
975 clid->cl_boot, clid->cl_id, boot_time);
980 * XXX Should we use a slab cache ?
981 * This type of memory management is somewhat inefficient, but we use it
982 * anyway since SETCLIENTID is not a common operation.
984 static struct nfs4_client *alloc_client(struct xdr_netobj name)
986 struct nfs4_client *clp;
988 clp = kzalloc(sizeof(struct nfs4_client), GFP_KERNEL);
991 clp->cl_name.data = kmalloc(name.len, GFP_KERNEL);
992 if (clp->cl_name.data == NULL) {
996 memcpy(clp->cl_name.data, name.data, name.len);
997 clp->cl_name.len = name.len;
998 INIT_LIST_HEAD(&clp->cl_sessions);
999 idr_init(&clp->cl_stateids);
1000 atomic_set(&clp->cl_refcount, 0);
1001 clp->cl_cb_state = NFSD4_CB_UNKNOWN;
1002 INIT_LIST_HEAD(&clp->cl_idhash);
1003 INIT_LIST_HEAD(&clp->cl_strhash);
1004 INIT_LIST_HEAD(&clp->cl_openowners);
1005 INIT_LIST_HEAD(&clp->cl_delegations);
1006 INIT_LIST_HEAD(&clp->cl_lru);
1007 INIT_LIST_HEAD(&clp->cl_callbacks);
1008 spin_lock_init(&clp->cl_lock);
1009 rpc_init_wait_queue(&clp->cl_cb_waitq, "Backchannel slot table");
1014 free_client(struct nfs4_client *clp)
1016 while (!list_empty(&clp->cl_sessions)) {
1017 struct nfsd4_session *ses;
1018 ses = list_entry(clp->cl_sessions.next, struct nfsd4_session,
1020 list_del(&ses->se_perclnt);
1021 nfsd4_put_session(ses);
1023 rpc_destroy_wait_queue(&clp->cl_cb_waitq);
1024 if (clp->cl_cred.cr_group_info)
1025 put_group_info(clp->cl_cred.cr_group_info);
1026 kfree(clp->cl_principal);
1027 kfree(clp->cl_name.data);
1028 idr_remove_all(&clp->cl_stateids);
1029 idr_destroy(&clp->cl_stateids);
1034 release_session_client(struct nfsd4_session *session)
1036 struct nfs4_client *clp = session->se_client;
1038 if (!atomic_dec_and_lock(&clp->cl_refcount, &client_lock))
1040 if (is_client_expired(clp)) {
1042 session->se_client = NULL;
1044 renew_client_locked(clp);
1045 spin_unlock(&client_lock);
1048 /* must be called under the client_lock */
1050 unhash_client_locked(struct nfs4_client *clp)
1052 struct nfsd4_session *ses;
1054 mark_client_expired(clp);
1055 list_del(&clp->cl_lru);
1056 spin_lock(&clp->cl_lock);
1057 list_for_each_entry(ses, &clp->cl_sessions, se_perclnt)
1058 list_del_init(&ses->se_hash);
1059 spin_unlock(&clp->cl_lock);
1063 expire_client(struct nfs4_client *clp)
1065 struct nfs4_openowner *oo;
1066 struct nfs4_delegation *dp;
1067 struct list_head reaplist;
1069 INIT_LIST_HEAD(&reaplist);
1070 spin_lock(&recall_lock);
1071 while (!list_empty(&clp->cl_delegations)) {
1072 dp = list_entry(clp->cl_delegations.next, struct nfs4_delegation, dl_perclnt);
1073 list_del_init(&dp->dl_perclnt);
1074 list_move(&dp->dl_recall_lru, &reaplist);
1076 spin_unlock(&recall_lock);
1077 while (!list_empty(&reaplist)) {
1078 dp = list_entry(reaplist.next, struct nfs4_delegation, dl_recall_lru);
1079 list_del_init(&dp->dl_recall_lru);
1080 unhash_delegation(dp);
1082 while (!list_empty(&clp->cl_openowners)) {
1083 oo = list_entry(clp->cl_openowners.next, struct nfs4_openowner, oo_perclient);
1084 release_openowner(oo);
1086 nfsd4_shutdown_callback(clp);
1087 if (clp->cl_cb_conn.cb_xprt)
1088 svc_xprt_put(clp->cl_cb_conn.cb_xprt);
1089 list_del(&clp->cl_idhash);
1090 list_del(&clp->cl_strhash);
1091 spin_lock(&client_lock);
1092 unhash_client_locked(clp);
1093 if (atomic_read(&clp->cl_refcount) == 0)
1095 spin_unlock(&client_lock);
1098 static void copy_verf(struct nfs4_client *target, nfs4_verifier *source)
1100 memcpy(target->cl_verifier.data, source->data,
1101 sizeof(target->cl_verifier.data));
1104 static void copy_clid(struct nfs4_client *target, struct nfs4_client *source)
1106 target->cl_clientid.cl_boot = source->cl_clientid.cl_boot;
1107 target->cl_clientid.cl_id = source->cl_clientid.cl_id;
1110 static void copy_cred(struct svc_cred *target, struct svc_cred *source)
1112 target->cr_uid = source->cr_uid;
1113 target->cr_gid = source->cr_gid;
1114 target->cr_group_info = source->cr_group_info;
1115 get_group_info(target->cr_group_info);
1118 static int same_name(const char *n1, const char *n2)
1120 return 0 == memcmp(n1, n2, HEXDIR_LEN);
1124 same_verf(nfs4_verifier *v1, nfs4_verifier *v2)
1126 return 0 == memcmp(v1->data, v2->data, sizeof(v1->data));
1130 same_clid(clientid_t *cl1, clientid_t *cl2)
1132 return (cl1->cl_boot == cl2->cl_boot) && (cl1->cl_id == cl2->cl_id);
1135 /* XXX what about NGROUP */
1137 same_creds(struct svc_cred *cr1, struct svc_cred *cr2)
1139 return cr1->cr_uid == cr2->cr_uid;
1142 static void gen_clid(struct nfs4_client *clp)
1144 static u32 current_clientid = 1;
1146 clp->cl_clientid.cl_boot = boot_time;
1147 clp->cl_clientid.cl_id = current_clientid++;
1150 static void gen_confirm(struct nfs4_client *clp)
1155 p = (u32 *)clp->cl_confirm.data;
1156 *p++ = get_seconds();
1160 static struct nfs4_stid *find_stateid(struct nfs4_client *cl, stateid_t *t)
1162 return idr_find(&cl->cl_stateids, t->si_opaque.so_id);
1165 static struct nfs4_stid *find_stateid_by_type(struct nfs4_client *cl, stateid_t *t, char typemask)
1167 struct nfs4_stid *s;
1169 s = find_stateid(cl, t);
1172 if (typemask & s->sc_type)
1177 static struct nfs4_client *create_client(struct xdr_netobj name, char *recdir,
1178 struct svc_rqst *rqstp, nfs4_verifier *verf)
1180 struct nfs4_client *clp;
1181 struct sockaddr *sa = svc_addr(rqstp);
1184 clp = alloc_client(name);
1189 princ = svc_gss_principal(rqstp);
1191 clp->cl_principal = kstrdup(princ, GFP_KERNEL);
1192 if (clp->cl_principal == NULL) {
1198 memcpy(clp->cl_recdir, recdir, HEXDIR_LEN);
1199 INIT_WORK(&clp->cl_cb_null.cb_work, nfsd4_do_callback_rpc);
1200 clp->cl_time = get_seconds();
1201 clear_bit(0, &clp->cl_cb_slot_busy);
1202 copy_verf(clp, verf);
1203 rpc_copy_addr((struct sockaddr *) &clp->cl_addr, sa);
1204 clp->cl_flavor = rqstp->rq_flavor;
1205 copy_cred(&clp->cl_cred, &rqstp->rq_cred);
1207 clp->cl_cb_session = NULL;
1212 add_to_unconfirmed(struct nfs4_client *clp, unsigned int strhashval)
1214 unsigned int idhashval;
1216 list_add(&clp->cl_strhash, &unconf_str_hashtbl[strhashval]);
1217 idhashval = clientid_hashval(clp->cl_clientid.cl_id);
1218 list_add(&clp->cl_idhash, &unconf_id_hashtbl[idhashval]);
1223 move_to_confirmed(struct nfs4_client *clp)
1225 unsigned int idhashval = clientid_hashval(clp->cl_clientid.cl_id);
1226 unsigned int strhashval;
1228 dprintk("NFSD: move_to_confirm nfs4_client %p\n", clp);
1229 list_move(&clp->cl_idhash, &conf_id_hashtbl[idhashval]);
1230 strhashval = clientstr_hashval(clp->cl_recdir);
1231 list_move(&clp->cl_strhash, &conf_str_hashtbl[strhashval]);
1235 static struct nfs4_client *
1236 find_confirmed_client(clientid_t *clid)
1238 struct nfs4_client *clp;
1239 unsigned int idhashval = clientid_hashval(clid->cl_id);
1241 list_for_each_entry(clp, &conf_id_hashtbl[idhashval], cl_idhash) {
1242 if (same_clid(&clp->cl_clientid, clid)) {
1250 static struct nfs4_client *
1251 find_unconfirmed_client(clientid_t *clid)
1253 struct nfs4_client *clp;
1254 unsigned int idhashval = clientid_hashval(clid->cl_id);
1256 list_for_each_entry(clp, &unconf_id_hashtbl[idhashval], cl_idhash) {
1257 if (same_clid(&clp->cl_clientid, clid))
1263 static bool clp_used_exchangeid(struct nfs4_client *clp)
1265 return clp->cl_exchange_flags != 0;
1268 static struct nfs4_client *
1269 find_confirmed_client_by_str(const char *dname, unsigned int hashval)
1271 struct nfs4_client *clp;
1273 list_for_each_entry(clp, &conf_str_hashtbl[hashval], cl_strhash) {
1274 if (same_name(clp->cl_recdir, dname))
1280 static struct nfs4_client *
1281 find_unconfirmed_client_by_str(const char *dname, unsigned int hashval)
1283 struct nfs4_client *clp;
1285 list_for_each_entry(clp, &unconf_str_hashtbl[hashval], cl_strhash) {
1286 if (same_name(clp->cl_recdir, dname))
1293 gen_callback(struct nfs4_client *clp, struct nfsd4_setclientid *se, struct svc_rqst *rqstp)
1295 struct nfs4_cb_conn *conn = &clp->cl_cb_conn;
1296 struct sockaddr *sa = svc_addr(rqstp);
1297 u32 scopeid = rpc_get_scope_id(sa);
1298 unsigned short expected_family;
1300 /* Currently, we only support tcp and tcp6 for the callback channel */
1301 if (se->se_callback_netid_len == 3 &&
1302 !memcmp(se->se_callback_netid_val, "tcp", 3))
1303 expected_family = AF_INET;
1304 else if (se->se_callback_netid_len == 4 &&
1305 !memcmp(se->se_callback_netid_val, "tcp6", 4))
1306 expected_family = AF_INET6;
1310 conn->cb_addrlen = rpc_uaddr2sockaddr(se->se_callback_addr_val,
1311 se->se_callback_addr_len,
1312 (struct sockaddr *)&conn->cb_addr,
1313 sizeof(conn->cb_addr));
1315 if (!conn->cb_addrlen || conn->cb_addr.ss_family != expected_family)
1318 if (conn->cb_addr.ss_family == AF_INET6)
1319 ((struct sockaddr_in6 *)&conn->cb_addr)->sin6_scope_id = scopeid;
1321 conn->cb_prog = se->se_callback_prog;
1322 conn->cb_ident = se->se_callback_ident;
1323 memcpy(&conn->cb_saddr, &rqstp->rq_daddr, rqstp->rq_daddrlen);
1326 conn->cb_addr.ss_family = AF_UNSPEC;
1327 conn->cb_addrlen = 0;
1328 dprintk(KERN_INFO "NFSD: this client (clientid %08x/%08x) "
1329 "will not receive delegations\n",
1330 clp->cl_clientid.cl_boot, clp->cl_clientid.cl_id);
1336 * Cache a reply. nfsd4_check_drc_limit() has bounded the cache size.
1339 nfsd4_store_cache_entry(struct nfsd4_compoundres *resp)
1341 struct nfsd4_slot *slot = resp->cstate.slot;
1344 dprintk("--> %s slot %p\n", __func__, slot);
1346 slot->sl_opcnt = resp->opcnt;
1347 slot->sl_status = resp->cstate.status;
1349 if (nfsd4_not_cached(resp)) {
1350 slot->sl_datalen = 0;
1353 slot->sl_datalen = (char *)resp->p - (char *)resp->cstate.datap;
1354 base = (char *)resp->cstate.datap -
1355 (char *)resp->xbuf->head[0].iov_base;
1356 if (read_bytes_from_xdr_buf(resp->xbuf, base, slot->sl_data,
1358 WARN("%s: sessions DRC could not cache compound\n", __func__);
1363 * Encode the replay sequence operation from the slot values.
1364 * If cachethis is FALSE encode the uncached rep error on the next
1365 * operation which sets resp->p and increments resp->opcnt for
1366 * nfs4svc_encode_compoundres.
1370 nfsd4_enc_sequence_replay(struct nfsd4_compoundargs *args,
1371 struct nfsd4_compoundres *resp)
1373 struct nfsd4_op *op;
1374 struct nfsd4_slot *slot = resp->cstate.slot;
1376 dprintk("--> %s resp->opcnt %d cachethis %u \n", __func__,
1377 resp->opcnt, resp->cstate.slot->sl_cachethis);
1379 /* Encode the replayed sequence operation */
1380 op = &args->ops[resp->opcnt - 1];
1381 nfsd4_encode_operation(resp, op);
1383 /* Return nfserr_retry_uncached_rep in next operation. */
1384 if (args->opcnt > 1 && slot->sl_cachethis == 0) {
1385 op = &args->ops[resp->opcnt++];
1386 op->status = nfserr_retry_uncached_rep;
1387 nfsd4_encode_operation(resp, op);
1393 * The sequence operation is not cached because we can use the slot and
1397 nfsd4_replay_cache_entry(struct nfsd4_compoundres *resp,
1398 struct nfsd4_sequence *seq)
1400 struct nfsd4_slot *slot = resp->cstate.slot;
1403 dprintk("--> %s slot %p\n", __func__, slot);
1405 /* Either returns 0 or nfserr_retry_uncached */
1406 status = nfsd4_enc_sequence_replay(resp->rqstp->rq_argp, resp);
1407 if (status == nfserr_retry_uncached_rep)
1410 /* The sequence operation has been encoded, cstate->datap set. */
1411 memcpy(resp->cstate.datap, slot->sl_data, slot->sl_datalen);
1413 resp->opcnt = slot->sl_opcnt;
1414 resp->p = resp->cstate.datap + XDR_QUADLEN(slot->sl_datalen);
1415 status = slot->sl_status;
1421 * Set the exchange_id flags returned by the server.
1424 nfsd4_set_ex_flags(struct nfs4_client *new, struct nfsd4_exchange_id *clid)
1426 /* pNFS is not supported */
1427 new->cl_exchange_flags |= EXCHGID4_FLAG_USE_NON_PNFS;
1429 /* Referrals are supported, Migration is not. */
1430 new->cl_exchange_flags |= EXCHGID4_FLAG_SUPP_MOVED_REFER;
1432 /* set the wire flags to return to client. */
1433 clid->flags = new->cl_exchange_flags;
1437 nfsd4_exchange_id(struct svc_rqst *rqstp,
1438 struct nfsd4_compound_state *cstate,
1439 struct nfsd4_exchange_id *exid)
1441 struct nfs4_client *unconf, *conf, *new;
1443 unsigned int strhashval;
1444 char dname[HEXDIR_LEN];
1445 char addr_str[INET6_ADDRSTRLEN];
1446 nfs4_verifier verf = exid->verifier;
1447 struct sockaddr *sa = svc_addr(rqstp);
1449 rpc_ntop(sa, addr_str, sizeof(addr_str));
1450 dprintk("%s rqstp=%p exid=%p clname.len=%u clname.data=%p "
1451 "ip_addr=%s flags %x, spa_how %d\n",
1452 __func__, rqstp, exid, exid->clname.len, exid->clname.data,
1453 addr_str, exid->flags, exid->spa_how);
1455 if (exid->flags & ~EXCHGID4_FLAG_MASK_A)
1456 return nfserr_inval;
1458 /* Currently only support SP4_NONE */
1459 switch (exid->spa_how) {
1463 return nfserr_serverfault;
1465 BUG(); /* checked by xdr code */
1467 return nfserr_serverfault; /* no excuse :-/ */
1470 status = nfs4_make_rec_clidname(dname, &exid->clname);
1475 strhashval = clientstr_hashval(dname);
1480 conf = find_confirmed_client_by_str(dname, strhashval);
1482 if (!clp_used_exchangeid(conf)) {
1483 status = nfserr_clid_inuse; /* XXX: ? */
1486 if (!same_verf(&verf, &conf->cl_verifier)) {
1487 /* 18.35.4 case 8 */
1488 if (exid->flags & EXCHGID4_FLAG_UPD_CONFIRMED_REC_A) {
1489 status = nfserr_not_same;
1492 /* Client reboot: destroy old state */
1493 expire_client(conf);
1496 if (!same_creds(&conf->cl_cred, &rqstp->rq_cred)) {
1497 /* 18.35.4 case 9 */
1498 if (exid->flags & EXCHGID4_FLAG_UPD_CONFIRMED_REC_A) {
1499 status = nfserr_perm;
1502 expire_client(conf);
1506 * Set bit when the owner id and verifier map to an already
1507 * confirmed client id (18.35.3).
1509 exid->flags |= EXCHGID4_FLAG_CONFIRMED_R;
1512 * Falling into 18.35.4 case 2, possible router replay.
1513 * Leave confirmed record intact and return same result.
1515 copy_verf(conf, &verf);
1520 /* 18.35.4 case 7 */
1521 if (exid->flags & EXCHGID4_FLAG_UPD_CONFIRMED_REC_A) {
1522 status = nfserr_noent;
1526 unconf = find_unconfirmed_client_by_str(dname, strhashval);
1529 * Possible retry or client restart. Per 18.35.4 case 4,
1530 * a new unconfirmed record should be generated regardless
1531 * of whether any properties have changed.
1533 expire_client(unconf);
1538 new = create_client(exid->clname, dname, rqstp, &verf);
1540 status = nfserr_jukebox;
1545 add_to_unconfirmed(new, strhashval);
1547 exid->clientid.cl_boot = new->cl_clientid.cl_boot;
1548 exid->clientid.cl_id = new->cl_clientid.cl_id;
1551 nfsd4_set_ex_flags(new, exid);
1553 dprintk("nfsd4_exchange_id seqid %d flags %x\n",
1554 new->cl_cs_slot.sl_seqid, new->cl_exchange_flags);
1558 nfs4_unlock_state();
1560 dprintk("nfsd4_exchange_id returns %d\n", ntohl(status));
1565 check_slot_seqid(u32 seqid, u32 slot_seqid, int slot_inuse)
1567 dprintk("%s enter. seqid %d slot_seqid %d\n", __func__, seqid,
1570 /* The slot is in use, and no response has been sent. */
1572 if (seqid == slot_seqid)
1573 return nfserr_jukebox;
1575 return nfserr_seq_misordered;
1578 if (likely(seqid == slot_seqid + 1))
1581 if (seqid == slot_seqid)
1582 return nfserr_replay_cache;
1584 if (seqid == 1 && (slot_seqid + 1) == 0)
1586 /* Misordered replay or misordered new request */
1587 return nfserr_seq_misordered;
1591 * Cache the create session result into the create session single DRC
1592 * slot cache by saving the xdr structure. sl_seqid has been set.
1593 * Do this for solo or embedded create session operations.
1596 nfsd4_cache_create_session(struct nfsd4_create_session *cr_ses,
1597 struct nfsd4_clid_slot *slot, int nfserr)
1599 slot->sl_status = nfserr;
1600 memcpy(&slot->sl_cr_ses, cr_ses, sizeof(*cr_ses));
1604 nfsd4_replay_create_session(struct nfsd4_create_session *cr_ses,
1605 struct nfsd4_clid_slot *slot)
1607 memcpy(cr_ses, &slot->sl_cr_ses, sizeof(*cr_ses));
1608 return slot->sl_status;
1611 #define NFSD_MIN_REQ_HDR_SEQ_SZ ((\
1612 2 * 2 + /* credential,verifier: AUTH_NULL, length 0 */ \
1613 1 + /* MIN tag is length with zero, only length */ \
1614 3 + /* version, opcount, opcode */ \
1615 XDR_QUADLEN(NFS4_MAX_SESSIONID_LEN) + \
1616 /* seqid, slotID, slotID, cache */ \
1617 4 ) * sizeof(__be32))
1619 #define NFSD_MIN_RESP_HDR_SEQ_SZ ((\
1620 2 + /* verifier: AUTH_NULL, length 0 */\
1622 1 + /* MIN tag is length with zero, only length */ \
1623 3 + /* opcount, opcode, opstatus*/ \
1624 XDR_QUADLEN(NFS4_MAX_SESSIONID_LEN) + \
1625 /* seqid, slotID, slotID, slotID, status */ \
1626 5 ) * sizeof(__be32))
1628 static __be32 check_forechannel_attrs(struct nfsd4_channel_attrs fchannel)
1630 return fchannel.maxreq_sz < NFSD_MIN_REQ_HDR_SEQ_SZ
1631 || fchannel.maxresp_sz < NFSD_MIN_RESP_HDR_SEQ_SZ;
1635 nfsd4_create_session(struct svc_rqst *rqstp,
1636 struct nfsd4_compound_state *cstate,
1637 struct nfsd4_create_session *cr_ses)
1639 struct sockaddr *sa = svc_addr(rqstp);
1640 struct nfs4_client *conf, *unconf;
1641 struct nfsd4_session *new;
1642 struct nfsd4_clid_slot *cs_slot = NULL;
1643 bool confirm_me = false;
1646 if (cr_ses->flags & ~SESSION4_FLAG_MASK_A)
1647 return nfserr_inval;
1650 unconf = find_unconfirmed_client(&cr_ses->clientid);
1651 conf = find_confirmed_client(&cr_ses->clientid);
1654 cs_slot = &conf->cl_cs_slot;
1655 status = check_slot_seqid(cr_ses->seqid, cs_slot->sl_seqid, 0);
1656 if (status == nfserr_replay_cache) {
1657 dprintk("Got a create_session replay! seqid= %d\n",
1659 /* Return the cached reply status */
1660 status = nfsd4_replay_create_session(cr_ses, cs_slot);
1662 } else if (cr_ses->seqid != cs_slot->sl_seqid + 1) {
1663 status = nfserr_seq_misordered;
1664 dprintk("Sequence misordered!\n");
1665 dprintk("Expected seqid= %d but got seqid= %d\n",
1666 cs_slot->sl_seqid, cr_ses->seqid);
1669 } else if (unconf) {
1670 if (!same_creds(&unconf->cl_cred, &rqstp->rq_cred) ||
1671 !rpc_cmp_addr(sa, (struct sockaddr *) &unconf->cl_addr)) {
1672 status = nfserr_clid_inuse;
1676 cs_slot = &unconf->cl_cs_slot;
1677 status = check_slot_seqid(cr_ses->seqid, cs_slot->sl_seqid, 0);
1679 /* an unconfirmed replay returns misordered */
1680 status = nfserr_seq_misordered;
1687 status = nfserr_stale_clientid;
1692 * XXX: we should probably set this at creation time, and check
1693 * for consistent minorversion use throughout:
1695 conf->cl_minorversion = 1;
1697 * We do not support RDMA or persistent sessions
1699 cr_ses->flags &= ~SESSION4_PERSIST;
1700 cr_ses->flags &= ~SESSION4_RDMA;
1702 status = nfserr_toosmall;
1703 if (check_forechannel_attrs(cr_ses->fore_channel))
1706 status = nfserr_jukebox;
1707 new = alloc_init_session(rqstp, conf, cr_ses);
1711 memcpy(cr_ses->sessionid.data, new->se_sessionid.data,
1712 NFS4_MAX_SESSIONID_LEN);
1713 memcpy(&cr_ses->fore_channel, &new->se_fchannel,
1714 sizeof(struct nfsd4_channel_attrs));
1715 cs_slot->sl_seqid++;
1716 cr_ses->seqid = cs_slot->sl_seqid;
1718 /* cache solo and embedded create sessions under the state lock */
1719 nfsd4_cache_create_session(cr_ses, cs_slot, status);
1721 move_to_confirmed(conf);
1723 nfs4_unlock_state();
1724 dprintk("%s returns %d\n", __func__, ntohl(status));
1728 static bool nfsd4_last_compound_op(struct svc_rqst *rqstp)
1730 struct nfsd4_compoundres *resp = rqstp->rq_resp;
1731 struct nfsd4_compoundargs *argp = rqstp->rq_argp;
1733 return argp->opcnt == resp->opcnt;
1736 static __be32 nfsd4_map_bcts_dir(u32 *dir)
1739 case NFS4_CDFC4_FORE:
1740 case NFS4_CDFC4_BACK:
1742 case NFS4_CDFC4_FORE_OR_BOTH:
1743 case NFS4_CDFC4_BACK_OR_BOTH:
1744 *dir = NFS4_CDFC4_BOTH;
1747 return nfserr_inval;
1750 __be32 nfsd4_bind_conn_to_session(struct svc_rqst *rqstp,
1751 struct nfsd4_compound_state *cstate,
1752 struct nfsd4_bind_conn_to_session *bcts)
1756 if (!nfsd4_last_compound_op(rqstp))
1757 return nfserr_not_only_op;
1758 spin_lock(&client_lock);
1759 cstate->session = find_in_sessionid_hashtbl(&bcts->sessionid);
1760 /* Sorta weird: we only need the refcnt'ing because new_conn acquires
1761 * client_lock iself: */
1762 if (cstate->session) {
1763 nfsd4_get_session(cstate->session);
1764 atomic_inc(&cstate->session->se_client->cl_refcount);
1766 spin_unlock(&client_lock);
1767 if (!cstate->session)
1768 return nfserr_badsession;
1770 status = nfsd4_map_bcts_dir(&bcts->dir);
1772 nfsd4_new_conn(rqstp, cstate->session, bcts->dir);
1776 static bool nfsd4_compound_in_session(struct nfsd4_session *session, struct nfs4_sessionid *sid)
1780 return !memcmp(sid, &session->se_sessionid, sizeof(*sid));
1784 nfsd4_destroy_session(struct svc_rqst *r,
1785 struct nfsd4_compound_state *cstate,
1786 struct nfsd4_destroy_session *sessionid)
1788 struct nfsd4_session *ses;
1789 u32 status = nfserr_badsession;
1792 * - The confirmed nfs4_client->cl_sessionid holds destroyed sessinid
1793 * - Should we return nfserr_back_chan_busy if waiting for
1794 * callbacks on to-be-destroyed session?
1795 * - Do we need to clear any callback info from previous session?
1798 if (nfsd4_compound_in_session(cstate->session, &sessionid->sessionid)) {
1799 if (!nfsd4_last_compound_op(r))
1800 return nfserr_not_only_op;
1802 dump_sessionid(__func__, &sessionid->sessionid);
1803 spin_lock(&client_lock);
1804 ses = find_in_sessionid_hashtbl(&sessionid->sessionid);
1806 spin_unlock(&client_lock);
1810 unhash_session(ses);
1811 spin_unlock(&client_lock);
1814 nfsd4_probe_callback_sync(ses->se_client);
1815 nfs4_unlock_state();
1817 nfsd4_del_conns(ses);
1819 nfsd4_put_session(ses);
1822 dprintk("%s returns %d\n", __func__, ntohl(status));
1826 static struct nfsd4_conn *__nfsd4_find_conn(struct svc_xprt *xpt, struct nfsd4_session *s)
1828 struct nfsd4_conn *c;
1830 list_for_each_entry(c, &s->se_conns, cn_persession) {
1831 if (c->cn_xprt == xpt) {
1838 static void nfsd4_sequence_check_conn(struct nfsd4_conn *new, struct nfsd4_session *ses)
1840 struct nfs4_client *clp = ses->se_client;
1841 struct nfsd4_conn *c;
1844 spin_lock(&clp->cl_lock);
1845 c = __nfsd4_find_conn(new->cn_xprt, ses);
1847 spin_unlock(&clp->cl_lock);
1851 __nfsd4_hash_conn(new, ses);
1852 spin_unlock(&clp->cl_lock);
1853 ret = nfsd4_register_conn(new);
1855 /* oops; xprt is already down: */
1856 nfsd4_conn_lost(&new->cn_xpt_user);
1860 static bool nfsd4_session_too_many_ops(struct svc_rqst *rqstp, struct nfsd4_session *session)
1862 struct nfsd4_compoundargs *args = rqstp->rq_argp;
1864 return args->opcnt > session->se_fchannel.maxops;
1867 static bool nfsd4_request_too_big(struct svc_rqst *rqstp,
1868 struct nfsd4_session *session)
1870 struct xdr_buf *xb = &rqstp->rq_arg;
1872 return xb->len > session->se_fchannel.maxreq_sz;
1876 nfsd4_sequence(struct svc_rqst *rqstp,
1877 struct nfsd4_compound_state *cstate,
1878 struct nfsd4_sequence *seq)
1880 struct nfsd4_compoundres *resp = rqstp->rq_resp;
1881 struct nfsd4_session *session;
1882 struct nfsd4_slot *slot;
1883 struct nfsd4_conn *conn;
1886 if (resp->opcnt != 1)
1887 return nfserr_sequence_pos;
1890 * Will be either used or freed by nfsd4_sequence_check_conn
1893 conn = alloc_conn(rqstp, NFS4_CDFC4_FORE);
1895 return nfserr_jukebox;
1897 spin_lock(&client_lock);
1898 status = nfserr_badsession;
1899 session = find_in_sessionid_hashtbl(&seq->sessionid);
1903 status = nfserr_too_many_ops;
1904 if (nfsd4_session_too_many_ops(rqstp, session))
1907 status = nfserr_req_too_big;
1908 if (nfsd4_request_too_big(rqstp, session))
1911 status = nfserr_badslot;
1912 if (seq->slotid >= session->se_fchannel.maxreqs)
1915 slot = session->se_slots[seq->slotid];
1916 dprintk("%s: slotid %d\n", __func__, seq->slotid);
1918 /* We do not negotiate the number of slots yet, so set the
1919 * maxslots to the session maxreqs which is used to encode
1920 * sr_highest_slotid and the sr_target_slot id to maxslots */
1921 seq->maxslots = session->se_fchannel.maxreqs;
1923 status = check_slot_seqid(seq->seqid, slot->sl_seqid, slot->sl_inuse);
1924 if (status == nfserr_replay_cache) {
1925 cstate->slot = slot;
1926 cstate->session = session;
1927 /* Return the cached reply status and set cstate->status
1928 * for nfsd4_proc_compound processing */
1929 status = nfsd4_replay_cache_entry(resp, seq);
1930 cstate->status = nfserr_replay_cache;
1936 nfsd4_sequence_check_conn(conn, session);
1939 /* Success! bump slot seqid */
1940 slot->sl_inuse = true;
1941 slot->sl_seqid = seq->seqid;
1942 slot->sl_cachethis = seq->cachethis;
1944 cstate->slot = slot;
1945 cstate->session = session;
1948 /* Hold a session reference until done processing the compound. */
1949 if (cstate->session) {
1950 struct nfs4_client *clp = session->se_client;
1952 nfsd4_get_session(cstate->session);
1953 atomic_inc(&clp->cl_refcount);
1954 switch (clp->cl_cb_state) {
1956 seq->status_flags = SEQ4_STATUS_CB_PATH_DOWN;
1958 case NFSD4_CB_FAULT:
1959 seq->status_flags = SEQ4_STATUS_BACKCHANNEL_FAULT;
1962 seq->status_flags = 0;
1966 spin_unlock(&client_lock);
1967 dprintk("%s: return %d\n", __func__, ntohl(status));
1971 static inline bool has_resources(struct nfs4_client *clp)
1973 return !list_empty(&clp->cl_openowners)
1974 || !list_empty(&clp->cl_delegations)
1975 || !list_empty(&clp->cl_sessions);
1979 nfsd4_destroy_clientid(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate, struct nfsd4_destroy_clientid *dc)
1981 struct nfs4_client *conf, *unconf, *clp;
1985 unconf = find_unconfirmed_client(&dc->clientid);
1986 conf = find_confirmed_client(&dc->clientid);
1991 if (!is_client_expired(conf) && has_resources(conf)) {
1992 status = nfserr_clientid_busy;
1996 /* rfc5661 18.50.3 */
1997 if (cstate->session && conf == cstate->session->se_client) {
1998 status = nfserr_clientid_busy;
2004 status = nfserr_stale_clientid;
2010 nfs4_unlock_state();
2011 dprintk("%s return %d\n", __func__, ntohl(status));
2016 nfsd4_reclaim_complete(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate, struct nfsd4_reclaim_complete *rc)
2020 if (rc->rca_one_fs) {
2021 if (!cstate->current_fh.fh_dentry)
2022 return nfserr_nofilehandle;
2024 * We don't take advantage of the rca_one_fs case.
2025 * That's OK, it's optional, we can safely ignore it.
2031 status = nfserr_complete_already;
2032 if (cstate->session->se_client->cl_firststate)
2035 status = nfserr_stale_clientid;
2036 if (is_client_expired(cstate->session->se_client))
2038 * The following error isn't really legal.
2039 * But we only get here if the client just explicitly
2040 * destroyed the client. Surely it no longer cares what
2041 * error it gets back on an operation for the dead
2047 nfsd4_create_clid_dir(cstate->session->se_client);
2049 nfs4_unlock_state();
2054 nfsd4_setclientid(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
2055 struct nfsd4_setclientid *setclid)
2057 struct xdr_netobj clname = setclid->se_name;
2058 nfs4_verifier clverifier = setclid->se_verf;
2059 unsigned int strhashval;
2060 struct nfs4_client *conf, *unconf, *new;
2062 char dname[HEXDIR_LEN];
2064 status = nfs4_make_rec_clidname(dname, &clname);
2069 * XXX The Duplicate Request Cache (DRC) has been checked (??)
2070 * We get here on a DRC miss.
2073 strhashval = clientstr_hashval(dname);
2076 conf = find_confirmed_client_by_str(dname, strhashval);
2078 /* RFC 3530 14.2.33 CASE 0: */
2079 status = nfserr_clid_inuse;
2080 if (clp_used_exchangeid(conf))
2082 if (!same_creds(&conf->cl_cred, &rqstp->rq_cred)) {
2083 char addr_str[INET6_ADDRSTRLEN];
2084 rpc_ntop((struct sockaddr *) &conf->cl_addr, addr_str,
2086 dprintk("NFSD: setclientid: string in use by client "
2087 "at %s\n", addr_str);
2092 * section 14.2.33 of RFC 3530 (under the heading "IMPLEMENTATION")
2093 * has a description of SETCLIENTID request processing consisting
2094 * of 5 bullet points, labeled as CASE0 - CASE4 below.
2096 unconf = find_unconfirmed_client_by_str(dname, strhashval);
2097 status = nfserr_jukebox;
2100 * RFC 3530 14.2.33 CASE 4:
2101 * placed first, because it is the normal case
2104 expire_client(unconf);
2105 new = create_client(clname, dname, rqstp, &clverifier);
2109 } else if (same_verf(&conf->cl_verifier, &clverifier)) {
2111 * RFC 3530 14.2.33 CASE 1:
2112 * probable callback update
2115 /* Note this is removing unconfirmed {*x***},
2116 * which is stronger than RFC recommended {vxc**}.
2117 * This has the advantage that there is at most
2118 * one {*x***} in either list at any time.
2120 expire_client(unconf);
2122 new = create_client(clname, dname, rqstp, &clverifier);
2125 copy_clid(new, conf);
2126 } else if (!unconf) {
2128 * RFC 3530 14.2.33 CASE 2:
2129 * probable client reboot; state will be removed if
2132 new = create_client(clname, dname, rqstp, &clverifier);
2138 * RFC 3530 14.2.33 CASE 3:
2139 * probable client reboot; state will be removed if
2142 expire_client(unconf);
2143 new = create_client(clname, dname, rqstp, &clverifier);
2149 * XXX: we should probably set this at creation time, and check
2150 * for consistent minorversion use throughout:
2152 new->cl_minorversion = 0;
2153 gen_callback(new, setclid, rqstp);
2154 add_to_unconfirmed(new, strhashval);
2155 setclid->se_clientid.cl_boot = new->cl_clientid.cl_boot;
2156 setclid->se_clientid.cl_id = new->cl_clientid.cl_id;
2157 memcpy(setclid->se_confirm.data, new->cl_confirm.data, sizeof(setclid->se_confirm.data));
2160 nfs4_unlock_state();
2166 * Section 14.2.34 of RFC 3530 (under the heading "IMPLEMENTATION") has
2167 * a description of SETCLIENTID_CONFIRM request processing consisting of 4
2168 * bullets, labeled as CASE1 - CASE4 below.
2171 nfsd4_setclientid_confirm(struct svc_rqst *rqstp,
2172 struct nfsd4_compound_state *cstate,
2173 struct nfsd4_setclientid_confirm *setclientid_confirm)
2175 struct sockaddr *sa = svc_addr(rqstp);
2176 struct nfs4_client *conf, *unconf;
2177 nfs4_verifier confirm = setclientid_confirm->sc_confirm;
2178 clientid_t * clid = &setclientid_confirm->sc_clientid;
2181 if (STALE_CLIENTID(clid))
2182 return nfserr_stale_clientid;
2184 * XXX The Duplicate Request Cache (DRC) has been checked (??)
2185 * We get here on a DRC miss.
2190 conf = find_confirmed_client(clid);
2191 unconf = find_unconfirmed_client(clid);
2193 status = nfserr_clid_inuse;
2194 if (conf && !rpc_cmp_addr((struct sockaddr *) &conf->cl_addr, sa))
2196 if (unconf && !rpc_cmp_addr((struct sockaddr *) &unconf->cl_addr, sa))
2200 * section 14.2.34 of RFC 3530 has a description of
2201 * SETCLIENTID_CONFIRM request processing consisting
2202 * of 4 bullet points, labeled as CASE1 - CASE4 below.
2204 if (conf && unconf && same_verf(&confirm, &unconf->cl_confirm)) {
2206 * RFC 3530 14.2.34 CASE 1:
2209 if (!same_creds(&conf->cl_cred, &unconf->cl_cred))
2210 status = nfserr_clid_inuse;
2212 nfsd4_change_callback(conf, &unconf->cl_cb_conn);
2213 nfsd4_probe_callback(conf);
2214 expire_client(unconf);
2218 } else if (conf && !unconf) {
2220 * RFC 3530 14.2.34 CASE 2:
2221 * probable retransmitted request; play it safe and
2224 if (!same_creds(&conf->cl_cred, &rqstp->rq_cred))
2225 status = nfserr_clid_inuse;
2228 } else if (!conf && unconf
2229 && same_verf(&unconf->cl_confirm, &confirm)) {
2231 * RFC 3530 14.2.34 CASE 3:
2232 * Normal case; new or rebooted client:
2234 if (!same_creds(&unconf->cl_cred, &rqstp->rq_cred)) {
2235 status = nfserr_clid_inuse;
2238 clientstr_hashval(unconf->cl_recdir);
2239 conf = find_confirmed_client_by_str(unconf->cl_recdir,
2242 nfsd4_remove_clid_dir(conf);
2243 expire_client(conf);
2245 move_to_confirmed(unconf);
2247 nfsd4_probe_callback(conf);
2250 } else if ((!conf || (conf && !same_verf(&conf->cl_confirm, &confirm)))
2251 && (!unconf || (unconf && !same_verf(&unconf->cl_confirm,
2254 * RFC 3530 14.2.34 CASE 4:
2255 * Client probably hasn't noticed that we rebooted yet.
2257 status = nfserr_stale_clientid;
2259 /* check that we have hit one of the cases...*/
2260 status = nfserr_clid_inuse;
2263 nfs4_unlock_state();
2267 static struct nfs4_file *nfsd4_alloc_file(void)
2269 return kmem_cache_alloc(file_slab, GFP_KERNEL);
2272 /* OPEN Share state helper functions */
2273 static void nfsd4_init_file(struct nfs4_file *fp, struct inode *ino)
2275 unsigned int hashval = file_hashval(ino);
2277 atomic_set(&fp->fi_ref, 1);
2278 INIT_LIST_HEAD(&fp->fi_hash);
2279 INIT_LIST_HEAD(&fp->fi_stateids);
2280 INIT_LIST_HEAD(&fp->fi_delegations);
2281 fp->fi_inode = igrab(ino);
2282 fp->fi_had_conflict = false;
2283 fp->fi_lease = NULL;
2284 memset(fp->fi_fds, 0, sizeof(fp->fi_fds));
2285 memset(fp->fi_access, 0, sizeof(fp->fi_access));
2286 spin_lock(&recall_lock);
2287 list_add(&fp->fi_hash, &file_hashtbl[hashval]);
2288 spin_unlock(&recall_lock);
2292 nfsd4_free_slab(struct kmem_cache **slab)
2296 kmem_cache_destroy(*slab);
2301 nfsd4_free_slabs(void)
2303 nfsd4_free_slab(&openowner_slab);
2304 nfsd4_free_slab(&lockowner_slab);
2305 nfsd4_free_slab(&file_slab);
2306 nfsd4_free_slab(&stateid_slab);
2307 nfsd4_free_slab(&deleg_slab);
2311 nfsd4_init_slabs(void)
2313 openowner_slab = kmem_cache_create("nfsd4_openowners",
2314 sizeof(struct nfs4_openowner), 0, 0, NULL);
2315 if (openowner_slab == NULL)
2317 lockowner_slab = kmem_cache_create("nfsd4_lockowners",
2318 sizeof(struct nfs4_lockowner), 0, 0, NULL);
2319 if (lockowner_slab == NULL)
2321 file_slab = kmem_cache_create("nfsd4_files",
2322 sizeof(struct nfs4_file), 0, 0, NULL);
2323 if (file_slab == NULL)
2325 stateid_slab = kmem_cache_create("nfsd4_stateids",
2326 sizeof(struct nfs4_ol_stateid), 0, 0, NULL);
2327 if (stateid_slab == NULL)
2329 deleg_slab = kmem_cache_create("nfsd4_delegations",
2330 sizeof(struct nfs4_delegation), 0, 0, NULL);
2331 if (deleg_slab == NULL)
2336 dprintk("nfsd4: out of memory while initializing nfsv4\n");
2340 void nfs4_free_openowner(struct nfs4_openowner *oo)
2342 kfree(oo->oo_owner.so_owner.data);
2343 kmem_cache_free(openowner_slab, oo);
2346 void nfs4_free_lockowner(struct nfs4_lockowner *lo)
2348 kfree(lo->lo_owner.so_owner.data);
2349 kmem_cache_free(lockowner_slab, lo);
2352 static void init_nfs4_replay(struct nfs4_replay *rp)
2354 rp->rp_status = nfserr_serverfault;
2356 rp->rp_buf = rp->rp_ibuf;
2359 static inline void *alloc_stateowner(struct kmem_cache *slab, struct xdr_netobj *owner, struct nfs4_client *clp)
2361 struct nfs4_stateowner *sop;
2363 sop = kmem_cache_alloc(slab, GFP_KERNEL);
2367 sop->so_owner.data = kmemdup(owner->data, owner->len, GFP_KERNEL);
2368 if (!sop->so_owner.data) {
2369 kmem_cache_free(slab, sop);
2372 sop->so_owner.len = owner->len;
2374 INIT_LIST_HEAD(&sop->so_stateids);
2375 sop->so_client = clp;
2376 init_nfs4_replay(&sop->so_replay);
2380 static void hash_openowner(struct nfs4_openowner *oo, struct nfs4_client *clp, unsigned int strhashval)
2382 list_add(&oo->oo_owner.so_strhash, &open_ownerstr_hashtbl[strhashval]);
2383 list_add(&oo->oo_perclient, &clp->cl_openowners);
2386 static struct nfs4_openowner *
2387 alloc_init_open_stateowner(unsigned int strhashval, struct nfs4_client *clp, struct nfsd4_open *open) {
2388 struct nfs4_openowner *oo;
2390 oo = alloc_stateowner(openowner_slab, &open->op_owner, clp);
2393 oo->oo_owner.so_is_open_owner = 1;
2394 oo->oo_owner.so_seqid = open->op_seqid;
2395 oo->oo_flags = NFS4_OO_NEW;
2397 oo->oo_last_closed_stid = NULL;
2398 INIT_LIST_HEAD(&oo->oo_close_lru);
2399 hash_openowner(oo, clp, strhashval);
2403 static void init_open_stateid(struct nfs4_ol_stateid *stp, struct nfs4_file *fp, struct nfsd4_open *open) {
2404 struct nfs4_openowner *oo = open->op_openowner;
2405 struct nfs4_client *clp = oo->oo_owner.so_client;
2407 init_stid(&stp->st_stid, clp, NFS4_OPEN_STID);
2408 INIT_LIST_HEAD(&stp->st_lockowners);
2409 list_add(&stp->st_perstateowner, &oo->oo_owner.so_stateids);
2410 list_add(&stp->st_perfile, &fp->fi_stateids);
2411 stp->st_stateowner = &oo->oo_owner;
2414 stp->st_access_bmap = 0;
2415 stp->st_deny_bmap = 0;
2416 __set_bit(open->op_share_access, &stp->st_access_bmap);
2417 __set_bit(open->op_share_deny, &stp->st_deny_bmap);
2418 stp->st_openstp = NULL;
2422 move_to_close_lru(struct nfs4_openowner *oo)
2424 dprintk("NFSD: move_to_close_lru nfs4_openowner %p\n", oo);
2426 list_move_tail(&oo->oo_close_lru, &close_lru);
2427 oo->oo_time = get_seconds();
2431 same_owner_str(struct nfs4_stateowner *sop, struct xdr_netobj *owner,
2434 return (sop->so_owner.len == owner->len) &&
2435 0 == memcmp(sop->so_owner.data, owner->data, owner->len) &&
2436 (sop->so_client->cl_clientid.cl_id == clid->cl_id);
2439 static struct nfs4_openowner *
2440 find_openstateowner_str(unsigned int hashval, struct nfsd4_open *open)
2442 struct nfs4_stateowner *so;
2443 struct nfs4_openowner *oo;
2445 list_for_each_entry(so, &open_ownerstr_hashtbl[hashval], so_strhash) {
2446 if (same_owner_str(so, &open->op_owner, &open->op_clientid)) {
2448 renew_client(oo->oo_owner.so_client);
2455 /* search file_hashtbl[] for file */
2456 static struct nfs4_file *
2457 find_file(struct inode *ino)
2459 unsigned int hashval = file_hashval(ino);
2460 struct nfs4_file *fp;
2462 spin_lock(&recall_lock);
2463 list_for_each_entry(fp, &file_hashtbl[hashval], fi_hash) {
2464 if (fp->fi_inode == ino) {
2466 spin_unlock(&recall_lock);
2470 spin_unlock(&recall_lock);
2475 * Called to check deny when READ with all zero stateid or
2476 * WRITE with all zero or all one stateid
2479 nfs4_share_conflict(struct svc_fh *current_fh, unsigned int deny_type)
2481 struct inode *ino = current_fh->fh_dentry->d_inode;
2482 struct nfs4_file *fp;
2483 struct nfs4_ol_stateid *stp;
2486 dprintk("NFSD: nfs4_share_conflict\n");
2488 fp = find_file(ino);
2491 ret = nfserr_locked;
2492 /* Search for conflicting share reservations */
2493 list_for_each_entry(stp, &fp->fi_stateids, st_perfile) {
2494 if (test_bit(deny_type, &stp->st_deny_bmap) ||
2495 test_bit(NFS4_SHARE_DENY_BOTH, &stp->st_deny_bmap))
2504 static void nfsd_break_one_deleg(struct nfs4_delegation *dp)
2506 /* We're assuming the state code never drops its reference
2507 * without first removing the lease. Since we're in this lease
2508 * callback (and since the lease code is serialized by the kernel
2509 * lock) we know the server hasn't removed the lease yet, we know
2510 * it's safe to take a reference: */
2511 atomic_inc(&dp->dl_count);
2513 list_add_tail(&dp->dl_recall_lru, &del_recall_lru);
2515 /* only place dl_time is set. protected by lock_flocks*/
2516 dp->dl_time = get_seconds();
2518 nfsd4_cb_recall(dp);
2521 /* Called from break_lease() with lock_flocks() held. */
2522 static void nfsd_break_deleg_cb(struct file_lock *fl)
2524 struct nfs4_file *fp = (struct nfs4_file *)fl->fl_owner;
2525 struct nfs4_delegation *dp;
2528 /* We assume break_lease is only called once per lease: */
2529 BUG_ON(fp->fi_had_conflict);
2531 * We don't want the locks code to timeout the lease for us;
2532 * we'll remove it ourself if a delegation isn't returned
2535 fl->fl_break_time = 0;
2537 spin_lock(&recall_lock);
2538 fp->fi_had_conflict = true;
2539 list_for_each_entry(dp, &fp->fi_delegations, dl_perfile)
2540 nfsd_break_one_deleg(dp);
2541 spin_unlock(&recall_lock);
2545 int nfsd_change_deleg_cb(struct file_lock **onlist, int arg)
2548 return lease_modify(onlist, arg);
2553 static const struct lock_manager_operations nfsd_lease_mng_ops = {
2554 .lm_break = nfsd_break_deleg_cb,
2555 .lm_change = nfsd_change_deleg_cb,
2558 static __be32 nfsd4_check_seqid(struct nfsd4_compound_state *cstate, struct nfs4_stateowner *so, u32 seqid)
2560 if (nfsd4_has_session(cstate))
2562 if (seqid == so->so_seqid - 1)
2563 return nfserr_replay_me;
2564 if (seqid == so->so_seqid)
2566 return nfserr_bad_seqid;
2570 nfsd4_process_open1(struct nfsd4_compound_state *cstate,
2571 struct nfsd4_open *open)
2573 clientid_t *clientid = &open->op_clientid;
2574 struct nfs4_client *clp = NULL;
2575 unsigned int strhashval;
2576 struct nfs4_openowner *oo = NULL;
2579 if (STALE_CLIENTID(&open->op_clientid))
2580 return nfserr_stale_clientid;
2582 * In case we need it later, after we've already created the
2583 * file and don't want to risk a further failure:
2585 open->op_file = nfsd4_alloc_file();
2586 if (open->op_file == NULL)
2587 return nfserr_jukebox;
2589 strhashval = open_ownerstr_hashval(clientid->cl_id, &open->op_owner);
2590 oo = find_openstateowner_str(strhashval, open);
2591 open->op_openowner = oo;
2593 clp = find_confirmed_client(clientid);
2595 return nfserr_expired;
2598 if (!(oo->oo_flags & NFS4_OO_CONFIRMED)) {
2599 /* Replace unconfirmed owners without checking for replay. */
2600 clp = oo->oo_owner.so_client;
2601 release_openowner(oo);
2602 open->op_openowner = NULL;
2605 status = nfsd4_check_seqid(cstate, &oo->oo_owner, open->op_seqid);
2608 clp = oo->oo_owner.so_client;
2611 oo = alloc_init_open_stateowner(strhashval, clp, open);
2613 return nfserr_jukebox;
2614 open->op_openowner = oo;
2616 open->op_stp = nfs4_alloc_stateid(clp);
2618 return nfserr_jukebox;
2622 static inline __be32
2623 nfs4_check_delegmode(struct nfs4_delegation *dp, int flags)
2625 if ((flags & WR_STATE) && (dp->dl_type == NFS4_OPEN_DELEGATE_READ))
2626 return nfserr_openmode;
2631 static int share_access_to_flags(u32 share_access)
2633 share_access &= ~NFS4_SHARE_WANT_MASK;
2635 return share_access == NFS4_SHARE_ACCESS_READ ? RD_STATE : WR_STATE;
2638 static struct nfs4_delegation *find_deleg_stateid(struct nfs4_client *cl, stateid_t *s)
2640 struct nfs4_stid *ret;
2642 ret = find_stateid_by_type(cl, s, NFS4_DELEG_STID);
2645 return delegstateid(ret);
2648 static bool nfsd4_is_deleg_cur(struct nfsd4_open *open)
2650 return open->op_claim_type == NFS4_OPEN_CLAIM_DELEGATE_CUR ||
2651 open->op_claim_type == NFS4_OPEN_CLAIM_DELEG_CUR_FH;
2655 nfs4_check_deleg(struct nfs4_client *cl, struct nfs4_file *fp, struct nfsd4_open *open,
2656 struct nfs4_delegation **dp)
2659 __be32 status = nfserr_bad_stateid;
2661 *dp = find_deleg_stateid(cl, &open->op_delegate_stateid);
2664 flags = share_access_to_flags(open->op_share_access);
2665 status = nfs4_check_delegmode(*dp, flags);
2669 if (!nfsd4_is_deleg_cur(open))
2673 open->op_openowner->oo_flags |= NFS4_OO_CONFIRMED;
2678 nfs4_check_open(struct nfs4_file *fp, struct nfsd4_open *open, struct nfs4_ol_stateid **stpp)
2680 struct nfs4_ol_stateid *local;
2681 struct nfs4_openowner *oo = open->op_openowner;
2683 list_for_each_entry(local, &fp->fi_stateids, st_perfile) {
2684 /* ignore lock owners */
2685 if (local->st_stateowner->so_is_open_owner == 0)
2687 /* remember if we have seen this open owner */
2688 if (local->st_stateowner == &oo->oo_owner)
2690 /* check for conflicting share reservations */
2691 if (!test_share(local, open))
2692 return nfserr_share_denied;
2697 static void nfs4_free_stateid(struct nfs4_ol_stateid *s)
2699 kmem_cache_free(stateid_slab, s);
2702 static inline int nfs4_access_to_access(u32 nfs4_access)
2706 if (nfs4_access & NFS4_SHARE_ACCESS_READ)
2707 flags |= NFSD_MAY_READ;
2708 if (nfs4_access & NFS4_SHARE_ACCESS_WRITE)
2709 flags |= NFSD_MAY_WRITE;
2713 static __be32 nfs4_get_vfs_file(struct svc_rqst *rqstp, struct nfs4_file *fp,
2714 struct svc_fh *cur_fh, struct nfsd4_open *open)
2717 int oflag = nfs4_access_to_omode(open->op_share_access);
2718 int access = nfs4_access_to_access(open->op_share_access);
2720 if (!fp->fi_fds[oflag]) {
2721 status = nfsd_open(rqstp, cur_fh, S_IFREG, access,
2722 &fp->fi_fds[oflag]);
2726 nfs4_file_get_access(fp, oflag);
2731 static inline __be32
2732 nfsd4_truncate(struct svc_rqst *rqstp, struct svc_fh *fh,
2733 struct nfsd4_open *open)
2735 struct iattr iattr = {
2736 .ia_valid = ATTR_SIZE,
2739 if (!open->op_truncate)
2741 if (!(open->op_share_access & NFS4_SHARE_ACCESS_WRITE))
2742 return nfserr_inval;
2743 return nfsd_setattr(rqstp, fh, &iattr, 0, (time_t)0);
2747 nfs4_upgrade_open(struct svc_rqst *rqstp, struct nfs4_file *fp, struct svc_fh *cur_fh, struct nfs4_ol_stateid *stp, struct nfsd4_open *open)
2749 u32 op_share_access = open->op_share_access;
2753 new_access = !test_bit(op_share_access, &stp->st_access_bmap);
2755 status = nfs4_get_vfs_file(rqstp, fp, cur_fh, open);
2759 status = nfsd4_truncate(rqstp, cur_fh, open);
2762 int oflag = nfs4_access_to_omode(op_share_access);
2763 nfs4_file_put_access(fp, oflag);
2767 /* remember the open */
2768 __set_bit(op_share_access, &stp->st_access_bmap);
2769 __set_bit(open->op_share_deny, &stp->st_deny_bmap);
2776 nfs4_set_claim_prev(struct nfsd4_open *open)
2778 open->op_openowner->oo_flags |= NFS4_OO_CONFIRMED;
2779 open->op_openowner->oo_owner.so_client->cl_firststate = 1;
2782 /* Should we give out recallable state?: */
2783 static bool nfsd4_cb_channel_good(struct nfs4_client *clp)
2785 if (clp->cl_cb_state == NFSD4_CB_UP)
2788 * In the sessions case, since we don't have to establish a
2789 * separate connection for callbacks, we assume it's OK
2790 * until we hear otherwise:
2792 return clp->cl_minorversion && clp->cl_cb_state == NFSD4_CB_UNKNOWN;
2795 static struct file_lock *nfs4_alloc_init_lease(struct nfs4_delegation *dp, int flag)
2797 struct file_lock *fl;
2799 fl = locks_alloc_lock();
2802 locks_init_lock(fl);
2803 fl->fl_lmops = &nfsd_lease_mng_ops;
2804 fl->fl_flags = FL_LEASE;
2805 fl->fl_type = flag == NFS4_OPEN_DELEGATE_READ? F_RDLCK: F_WRLCK;
2806 fl->fl_end = OFFSET_MAX;
2807 fl->fl_owner = (fl_owner_t)(dp->dl_file);
2808 fl->fl_pid = current->tgid;
2812 static int nfs4_setlease(struct nfs4_delegation *dp, int flag)
2814 struct nfs4_file *fp = dp->dl_file;
2815 struct file_lock *fl;
2818 fl = nfs4_alloc_init_lease(dp, flag);
2821 fl->fl_file = find_readable_file(fp);
2822 status = vfs_setlease(fl->fl_file, fl->fl_type, &fl);
2824 locks_free_lock(fl);
2828 fp->fi_deleg_file = fl->fl_file;
2829 get_file(fp->fi_deleg_file);
2830 atomic_set(&fp->fi_delegees, 1);
2831 spin_lock(&recall_lock);
2832 hash_delegation_locked(dp, fp);
2833 spin_unlock(&recall_lock);
2837 static int nfs4_set_delegation(struct nfs4_delegation *dp, int flag)
2839 struct nfs4_file *fp = dp->dl_file;
2842 return nfs4_setlease(dp, flag);
2843 spin_lock(&recall_lock);
2844 if (fp->fi_had_conflict) {
2845 spin_unlock(&recall_lock);
2848 atomic_inc(&fp->fi_delegees);
2849 hash_delegation_locked(dp, fp);
2850 spin_unlock(&recall_lock);
2855 * Attempt to hand out a delegation.
2858 nfs4_open_delegation(struct svc_fh *fh, struct nfsd4_open *open, struct nfs4_ol_stateid *stp)
2860 struct nfs4_delegation *dp;
2861 struct nfs4_openowner *oo = container_of(stp->st_stateowner, struct nfs4_openowner, oo_owner);
2863 int status, flag = 0;
2865 cb_up = nfsd4_cb_channel_good(oo->oo_owner.so_client);
2866 flag = NFS4_OPEN_DELEGATE_NONE;
2867 open->op_recall = 0;
2868 switch (open->op_claim_type) {
2869 case NFS4_OPEN_CLAIM_PREVIOUS:
2871 open->op_recall = 1;
2872 flag = open->op_delegate_type;
2873 if (flag == NFS4_OPEN_DELEGATE_NONE)
2876 case NFS4_OPEN_CLAIM_NULL:
2877 /* Let's not give out any delegations till everyone's
2878 * had the chance to reclaim theirs.... */
2879 if (locks_in_grace())
2881 if (!cb_up || !(oo->oo_flags & NFS4_OO_CONFIRMED))
2883 if (open->op_share_access & NFS4_SHARE_ACCESS_WRITE)
2884 flag = NFS4_OPEN_DELEGATE_WRITE;
2886 flag = NFS4_OPEN_DELEGATE_READ;
2892 dp = alloc_init_deleg(oo->oo_owner.so_client, stp, fh, flag);
2895 status = nfs4_set_delegation(dp, flag);
2899 memcpy(&open->op_delegate_stateid, &dp->dl_stid.sc_stateid, sizeof(dp->dl_stid.sc_stateid));
2901 dprintk("NFSD: delegation stateid=" STATEID_FMT "\n",
2902 STATEID_VAL(&dp->dl_stid.sc_stateid));
2904 if (open->op_claim_type == NFS4_OPEN_CLAIM_PREVIOUS
2905 && flag == NFS4_OPEN_DELEGATE_NONE
2906 && open->op_delegate_type != NFS4_OPEN_DELEGATE_NONE)
2907 dprintk("NFSD: WARNING: refusing delegation reclaim\n");
2908 open->op_delegate_type = flag;
2911 nfs4_put_delegation(dp);
2913 flag = NFS4_OPEN_DELEGATE_NONE;
2918 * called with nfs4_lock_state() held.
2921 nfsd4_process_open2(struct svc_rqst *rqstp, struct svc_fh *current_fh, struct nfsd4_open *open)
2923 struct nfsd4_compoundres *resp = rqstp->rq_resp;
2924 struct nfs4_client *cl = open->op_openowner->oo_owner.so_client;
2925 struct nfs4_file *fp = NULL;
2926 struct inode *ino = current_fh->fh_dentry->d_inode;
2927 struct nfs4_ol_stateid *stp = NULL;
2928 struct nfs4_delegation *dp = NULL;
2932 * Lookup file; if found, lookup stateid and check open request,
2933 * and check for delegations in the process of being recalled.
2934 * If not found, create the nfs4_file struct
2936 fp = find_file(ino);
2938 if ((status = nfs4_check_open(fp, open, &stp)))
2940 status = nfs4_check_deleg(cl, fp, open, &dp);
2944 status = nfserr_bad_stateid;
2945 if (nfsd4_is_deleg_cur(open))
2947 status = nfserr_jukebox;
2949 open->op_file = NULL;
2950 nfsd4_init_file(fp, ino);
2954 * OPEN the file, or upgrade an existing OPEN.
2955 * If truncate fails, the OPEN fails.
2958 /* Stateid was found, this is an OPEN upgrade */
2959 status = nfs4_upgrade_open(rqstp, fp, current_fh, stp, open);
2963 status = nfs4_get_vfs_file(rqstp, fp, current_fh, open);
2967 open->op_stp = NULL;
2968 init_open_stateid(stp, fp, open);
2969 status = nfsd4_truncate(rqstp, current_fh, open);
2971 release_open_stateid(stp);
2975 update_stateid(&stp->st_stid.sc_stateid);
2976 memcpy(&open->op_stateid, &stp->st_stid.sc_stateid, sizeof(stateid_t));
2978 if (nfsd4_has_session(&resp->cstate))
2979 open->op_openowner->oo_flags |= NFS4_OO_CONFIRMED;
2982 * Attempt to hand out a delegation. No error return, because the
2983 * OPEN succeeds even if we fail.
2985 nfs4_open_delegation(current_fh, open, stp);
2989 dprintk("%s: stateid=" STATEID_FMT "\n", __func__,
2990 STATEID_VAL(&stp->st_stid.sc_stateid));
2994 if (status == 0 && open->op_claim_type == NFS4_OPEN_CLAIM_PREVIOUS)
2995 nfs4_set_claim_prev(open);
2997 * To finish the open response, we just need to set the rflags.
2999 open->op_rflags = NFS4_OPEN_RESULT_LOCKTYPE_POSIX;
3000 if (!(open->op_openowner->oo_flags & NFS4_OO_CONFIRMED) &&
3001 !nfsd4_has_session(&resp->cstate))
3002 open->op_rflags |= NFS4_OPEN_RESULT_CONFIRM;
3007 void nfsd4_cleanup_open_state(struct nfsd4_open *open, __be32 status)
3009 if (open->op_openowner) {
3010 struct nfs4_openowner *oo = open->op_openowner;
3012 if (!list_empty(&oo->oo_owner.so_stateids))
3013 list_del_init(&oo->oo_close_lru);
3014 if (oo->oo_flags & NFS4_OO_NEW) {
3016 release_openowner(oo);
3017 open->op_openowner = NULL;
3019 oo->oo_flags &= ~NFS4_OO_NEW;
3023 nfsd4_free_file(open->op_file);
3025 nfs4_free_stateid(open->op_stp);
3029 nfsd4_renew(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
3032 struct nfs4_client *clp;
3036 dprintk("process_renew(%08x/%08x): starting\n",
3037 clid->cl_boot, clid->cl_id);
3038 status = nfserr_stale_clientid;
3039 if (STALE_CLIENTID(clid))
3041 clp = find_confirmed_client(clid);
3042 status = nfserr_expired;
3044 /* We assume the client took too long to RENEW. */
3045 dprintk("nfsd4_renew: clientid not found!\n");
3048 status = nfserr_cb_path_down;
3049 if (!list_empty(&clp->cl_delegations)
3050 && clp->cl_cb_state != NFSD4_CB_UP)
3054 nfs4_unlock_state();
3058 static struct lock_manager nfsd4_manager = {
3062 nfsd4_end_grace(void)
3064 dprintk("NFSD: end of grace period\n");
3065 nfsd4_recdir_purge_old();
3066 locks_end_grace(&nfsd4_manager);
3068 * Now that every NFSv4 client has had the chance to recover and
3069 * to see the (possibly new, possibly shorter) lease time, we
3070 * can safely set the next grace time to the current lease time:
3072 nfsd4_grace = nfsd4_lease;
3076 nfs4_laundromat(void)
3078 struct nfs4_client *clp;
3079 struct nfs4_openowner *oo;
3080 struct nfs4_delegation *dp;
3081 struct list_head *pos, *next, reaplist;
3082 time_t cutoff = get_seconds() - nfsd4_lease;
3083 time_t t, clientid_val = nfsd4_lease;
3084 time_t u, test_val = nfsd4_lease;
3088 dprintk("NFSD: laundromat service - starting\n");
3089 if (locks_in_grace())
3091 INIT_LIST_HEAD(&reaplist);
3092 spin_lock(&client_lock);
3093 list_for_each_safe(pos, next, &client_lru) {
3094 clp = list_entry(pos, struct nfs4_client, cl_lru);
3095 if (time_after((unsigned long)clp->cl_time, (unsigned long)cutoff)) {
3096 t = clp->cl_time - cutoff;
3097 if (clientid_val > t)
3101 if (atomic_read(&clp->cl_refcount)) {
3102 dprintk("NFSD: client in use (clientid %08x)\n",
3103 clp->cl_clientid.cl_id);
3106 unhash_client_locked(clp);
3107 list_add(&clp->cl_lru, &reaplist);
3109 spin_unlock(&client_lock);
3110 list_for_each_safe(pos, next, &reaplist) {
3111 clp = list_entry(pos, struct nfs4_client, cl_lru);
3112 dprintk("NFSD: purging unused client (clientid %08x)\n",
3113 clp->cl_clientid.cl_id);
3114 nfsd4_remove_clid_dir(clp);
3117 spin_lock(&recall_lock);
3118 list_for_each_safe(pos, next, &del_recall_lru) {
3119 dp = list_entry (pos, struct nfs4_delegation, dl_recall_lru);
3120 if (time_after((unsigned long)dp->dl_time, (unsigned long)cutoff)) {
3121 u = dp->dl_time - cutoff;
3126 list_move(&dp->dl_recall_lru, &reaplist);
3128 spin_unlock(&recall_lock);
3129 list_for_each_safe(pos, next, &reaplist) {
3130 dp = list_entry (pos, struct nfs4_delegation, dl_recall_lru);
3131 list_del_init(&dp->dl_recall_lru);
3132 unhash_delegation(dp);
3134 test_val = nfsd4_lease;
3135 list_for_each_safe(pos, next, &close_lru) {
3136 oo = container_of(pos, struct nfs4_openowner, oo_close_lru);
3137 if (time_after((unsigned long)oo->oo_time, (unsigned long)cutoff)) {
3138 u = oo->oo_time - cutoff;
3143 release_openowner(oo);
3145 if (clientid_val < NFSD_LAUNDROMAT_MINTIMEOUT)
3146 clientid_val = NFSD_LAUNDROMAT_MINTIMEOUT;
3147 nfs4_unlock_state();
3148 return clientid_val;
3151 static struct workqueue_struct *laundry_wq;
3152 static void laundromat_main(struct work_struct *);
3153 static DECLARE_DELAYED_WORK(laundromat_work, laundromat_main);
3156 laundromat_main(struct work_struct *not_used)
3160 t = nfs4_laundromat();
3161 dprintk("NFSD: laundromat_main - sleeping for %ld seconds\n", t);
3162 queue_delayed_work(laundry_wq, &laundromat_work, t*HZ);
3165 static inline __be32 nfs4_check_fh(struct svc_fh *fhp, struct nfs4_ol_stateid *stp)
3167 if (fhp->fh_dentry->d_inode != stp->st_file->fi_inode)
3168 return nfserr_bad_stateid;
3173 STALE_STATEID(stateid_t *stateid)
3175 if (stateid->si_opaque.so_clid.cl_boot == boot_time)
3177 dprintk("NFSD: stale stateid " STATEID_FMT "!\n",
3178 STATEID_VAL(stateid));
3183 access_permit_read(unsigned long access_bmap)
3185 return test_bit(NFS4_SHARE_ACCESS_READ, &access_bmap) ||
3186 test_bit(NFS4_SHARE_ACCESS_BOTH, &access_bmap) ||
3187 test_bit(NFS4_SHARE_ACCESS_WRITE, &access_bmap);
3191 access_permit_write(unsigned long access_bmap)
3193 return test_bit(NFS4_SHARE_ACCESS_WRITE, &access_bmap) ||
3194 test_bit(NFS4_SHARE_ACCESS_BOTH, &access_bmap);
3198 __be32 nfs4_check_openmode(struct nfs4_ol_stateid *stp, int flags)
3200 __be32 status = nfserr_openmode;
3202 /* For lock stateid's, we test the parent open, not the lock: */
3203 if (stp->st_openstp)
3204 stp = stp->st_openstp;
3205 if ((flags & WR_STATE) && (!access_permit_write(stp->st_access_bmap)))
3207 if ((flags & RD_STATE) && (!access_permit_read(stp->st_access_bmap)))
3214 static inline __be32
3215 check_special_stateids(svc_fh *current_fh, stateid_t *stateid, int flags)
3217 if (ONE_STATEID(stateid) && (flags & RD_STATE))
3219 else if (locks_in_grace()) {
3220 /* Answer in remaining cases depends on existence of
3221 * conflicting state; so we must wait out the grace period. */
3222 return nfserr_grace;
3223 } else if (flags & WR_STATE)
3224 return nfs4_share_conflict(current_fh,
3225 NFS4_SHARE_DENY_WRITE);
3226 else /* (flags & RD_STATE) && ZERO_STATEID(stateid) */
3227 return nfs4_share_conflict(current_fh,
3228 NFS4_SHARE_DENY_READ);
3232 * Allow READ/WRITE during grace period on recovered state only for files
3233 * that are not able to provide mandatory locking.
3236 grace_disallows_io(struct inode *inode)
3238 return locks_in_grace() && mandatory_lock(inode);
3241 /* Returns true iff a is later than b: */
3242 static bool stateid_generation_after(stateid_t *a, stateid_t *b)
3244 return (s32)a->si_generation - (s32)b->si_generation > 0;
3247 static int check_stateid_generation(stateid_t *in, stateid_t *ref, bool has_session)
3250 * When sessions are used the stateid generation number is ignored
3253 if (has_session && in->si_generation == 0)
3256 if (in->si_generation == ref->si_generation)
3259 /* If the client sends us a stateid from the future, it's buggy: */
3260 if (stateid_generation_after(in, ref))
3261 return nfserr_bad_stateid;
3263 * However, we could see a stateid from the past, even from a
3264 * non-buggy client. For example, if the client sends a lock
3265 * while some IO is outstanding, the lock may bump si_generation
3266 * while the IO is still in flight. The client could avoid that
3267 * situation by waiting for responses on all the IO requests,
3268 * but better performance may result in retrying IO that
3269 * receives an old_stateid error if requests are rarely
3270 * reordered in flight:
3272 return nfserr_old_stateid;
3275 __be32 nfs4_validate_stateid(struct nfs4_client *cl, stateid_t *stateid)
3277 struct nfs4_stid *s;
3278 struct nfs4_ol_stateid *ols;
3281 if (STALE_STATEID(stateid))
3282 return nfserr_stale_stateid;
3284 s = find_stateid(cl, stateid);
3286 return nfserr_stale_stateid;
3287 status = check_stateid_generation(stateid, &s->sc_stateid, 1);
3290 if (!(s->sc_type & (NFS4_OPEN_STID | NFS4_LOCK_STID)))
3292 ols = openlockstateid(s);
3293 if (ols->st_stateowner->so_is_open_owner
3294 && !(openowner(ols->st_stateowner)->oo_flags & NFS4_OO_CONFIRMED))
3295 return nfserr_bad_stateid;
3299 static __be32 nfsd4_lookup_stateid(stateid_t *stateid, unsigned char typemask, struct nfs4_stid **s)
3301 struct nfs4_client *cl;
3303 if (ZERO_STATEID(stateid) || ONE_STATEID(stateid))
3304 return nfserr_bad_stateid;
3305 if (STALE_STATEID(stateid))
3306 return nfserr_stale_stateid;
3307 cl = find_confirmed_client(&stateid->si_opaque.so_clid);
3309 return nfserr_expired;
3310 *s = find_stateid_by_type(cl, stateid, typemask);
3312 return nfserr_bad_stateid;
3318 * Checks for stateid operations
3321 nfs4_preprocess_stateid_op(struct nfsd4_compound_state *cstate,
3322 stateid_t *stateid, int flags, struct file **filpp)
3324 struct nfs4_stid *s;
3325 struct nfs4_ol_stateid *stp = NULL;
3326 struct nfs4_delegation *dp = NULL;
3327 struct svc_fh *current_fh = &cstate->current_fh;
3328 struct inode *ino = current_fh->fh_dentry->d_inode;
3334 if (grace_disallows_io(ino))
3335 return nfserr_grace;
3337 if (ZERO_STATEID(stateid) || ONE_STATEID(stateid))
3338 return check_special_stateids(current_fh, stateid, flags);
3340 status = nfsd4_lookup_stateid(stateid, NFS4_DELEG_STID|NFS4_OPEN_STID|NFS4_LOCK_STID, &s);
3343 status = check_stateid_generation(stateid, &s->sc_stateid, nfsd4_has_session(cstate));
3346 switch (s->sc_type) {
3347 case NFS4_DELEG_STID:
3348 dp = delegstateid(s);
3349 status = nfs4_check_delegmode(dp, flags);
3353 *filpp = dp->dl_file->fi_deleg_file;
3357 case NFS4_OPEN_STID:
3358 case NFS4_LOCK_STID:
3359 stp = openlockstateid(s);
3360 status = nfs4_check_fh(current_fh, stp);
3363 if (stp->st_stateowner->so_is_open_owner
3364 && !(openowner(stp->st_stateowner)->oo_flags & NFS4_OO_CONFIRMED))
3366 status = nfs4_check_openmode(stp, flags);
3370 if (flags & RD_STATE)
3371 *filpp = find_readable_file(stp->st_file);
3373 *filpp = find_writeable_file(stp->st_file);
3377 return nfserr_bad_stateid;
3385 nfsd4_free_lock_stateid(struct nfs4_ol_stateid *stp)
3387 struct nfs4_lockowner *lo = lockowner(stp->st_stateowner);
3389 if (check_for_locks(stp->st_file, lo))
3390 return nfserr_locks_held;
3392 * Currently there's a 1-1 lock stateid<->lockowner
3393 * correspondance, and we have to delete the lockowner when we
3394 * delete the lock stateid:
3396 unhash_lockowner(lo);
3401 * Test if the stateid is valid
3404 nfsd4_test_stateid(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
3405 struct nfsd4_test_stateid *test_stateid)
3407 /* real work is done during encoding */
3412 nfsd4_free_stateid(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
3413 struct nfsd4_free_stateid *free_stateid)
3415 stateid_t *stateid = &free_stateid->fr_stateid;
3416 struct nfs4_stid *s;
3417 struct nfs4_client *cl = cstate->session->se_client;
3418 __be32 ret = nfserr_bad_stateid;
3421 s = find_stateid(cl, stateid);
3424 switch (s->sc_type) {
3425 case NFS4_DELEG_STID:
3426 ret = nfserr_locks_held;
3428 case NFS4_OPEN_STID:
3429 case NFS4_LOCK_STID:
3430 ret = check_stateid_generation(stateid, &s->sc_stateid, 1);
3433 if (s->sc_type == NFS4_LOCK_STID)
3434 ret = nfsd4_free_lock_stateid(openlockstateid(s));
3436 ret = nfserr_locks_held;
3439 ret = nfserr_bad_stateid;
3442 nfs4_unlock_state();
3449 return (type == NFS4_READW_LT || type == NFS4_READ_LT) ?
3450 RD_STATE : WR_STATE;
3453 static __be32 nfs4_seqid_op_checks(struct nfsd4_compound_state *cstate, stateid_t *stateid, u32 seqid, struct nfs4_ol_stateid *stp)
3455 struct svc_fh *current_fh = &cstate->current_fh;
3456 struct nfs4_stateowner *sop = stp->st_stateowner;
3459 status = nfsd4_check_seqid(cstate, sop, seqid);
3462 if (stp->st_stid.sc_type == NFS4_CLOSED_STID)
3464 * "Closed" stateid's exist *only* to return
3465 * nfserr_replay_me from the previous step.
3467 return nfserr_bad_stateid;
3468 status = check_stateid_generation(stateid, &stp->st_stid.sc_stateid, nfsd4_has_session(cstate));
3471 return nfs4_check_fh(current_fh, stp);
3475 * Checks for sequence id mutating operations.
3478 nfs4_preprocess_seqid_op(struct nfsd4_compound_state *cstate, u32 seqid,
3479 stateid_t *stateid, char typemask,
3480 struct nfs4_ol_stateid **stpp)
3483 struct nfs4_stid *s;
3485 dprintk("NFSD: %s: seqid=%d stateid = " STATEID_FMT "\n", __func__,
3486 seqid, STATEID_VAL(stateid));
3489 status = nfsd4_lookup_stateid(stateid, typemask, &s);
3492 *stpp = openlockstateid(s);
3493 cstate->replay_owner = (*stpp)->st_stateowner;
3495 return nfs4_seqid_op_checks(cstate, stateid, seqid, *stpp);
3498 static __be32 nfs4_preprocess_confirmed_seqid_op(struct nfsd4_compound_state *cstate, u32 seqid, stateid_t *stateid, struct nfs4_ol_stateid **stpp)
3501 struct nfs4_openowner *oo;
3503 status = nfs4_preprocess_seqid_op(cstate, seqid, stateid,
3504 NFS4_OPEN_STID, stpp);
3507 oo = openowner((*stpp)->st_stateowner);
3508 if (!(oo->oo_flags & NFS4_OO_CONFIRMED))
3509 return nfserr_bad_stateid;
3514 nfsd4_open_confirm(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
3515 struct nfsd4_open_confirm *oc)
3518 struct nfs4_openowner *oo;
3519 struct nfs4_ol_stateid *stp;
3521 dprintk("NFSD: nfsd4_open_confirm on file %.*s\n",
3522 (int)cstate->current_fh.fh_dentry->d_name.len,
3523 cstate->current_fh.fh_dentry->d_name.name);
3525 status = fh_verify(rqstp, &cstate->current_fh, S_IFREG, 0);
3531 status = nfs4_preprocess_seqid_op(cstate,
3532 oc->oc_seqid, &oc->oc_req_stateid,
3533 NFS4_OPEN_STID, &stp);
3536 oo = openowner(stp->st_stateowner);
3537 status = nfserr_bad_stateid;
3538 if (oo->oo_flags & NFS4_OO_CONFIRMED)
3540 oo->oo_flags |= NFS4_OO_CONFIRMED;
3541 update_stateid(&stp->st_stid.sc_stateid);
3542 memcpy(&oc->oc_resp_stateid, &stp->st_stid.sc_stateid, sizeof(stateid_t));
3543 dprintk("NFSD: %s: success, seqid=%d stateid=" STATEID_FMT "\n",
3544 __func__, oc->oc_seqid, STATEID_VAL(&stp->st_stid.sc_stateid));
3546 nfsd4_create_clid_dir(oo->oo_owner.so_client);
3549 if (!cstate->replay_owner)
3550 nfs4_unlock_state();
3554 static inline void nfs4_stateid_downgrade_bit(struct nfs4_ol_stateid *stp, u32 access)
3556 if (!test_bit(access, &stp->st_access_bmap))
3558 nfs4_file_put_access(stp->st_file, nfs4_access_to_omode(access));
3559 __clear_bit(access, &stp->st_access_bmap);
3562 static inline void nfs4_stateid_downgrade(struct nfs4_ol_stateid *stp, u32 to_access)
3564 switch (to_access) {
3565 case NFS4_SHARE_ACCESS_READ:
3566 nfs4_stateid_downgrade_bit(stp, NFS4_SHARE_ACCESS_WRITE);
3567 nfs4_stateid_downgrade_bit(stp, NFS4_SHARE_ACCESS_BOTH);
3569 case NFS4_SHARE_ACCESS_WRITE:
3570 nfs4_stateid_downgrade_bit(stp, NFS4_SHARE_ACCESS_READ);
3571 nfs4_stateid_downgrade_bit(stp, NFS4_SHARE_ACCESS_BOTH);
3573 case NFS4_SHARE_ACCESS_BOTH:
3581 reset_union_bmap_deny(unsigned long deny, unsigned long *bmap)
3584 for (i = 0; i < 4; i++) {
3585 if ((i & deny) != i)
3586 __clear_bit(i, bmap);
3591 nfsd4_open_downgrade(struct svc_rqst *rqstp,
3592 struct nfsd4_compound_state *cstate,
3593 struct nfsd4_open_downgrade *od)
3596 struct nfs4_ol_stateid *stp;
3598 dprintk("NFSD: nfsd4_open_downgrade on file %.*s\n",
3599 (int)cstate->current_fh.fh_dentry->d_name.len,
3600 cstate->current_fh.fh_dentry->d_name.name);
3602 /* We don't yet support WANT bits: */
3603 od->od_share_access &= NFS4_SHARE_ACCESS_MASK;
3606 status = nfs4_preprocess_confirmed_seqid_op(cstate, od->od_seqid,
3607 &od->od_stateid, &stp);
3610 status = nfserr_inval;
3611 if (!test_bit(od->od_share_access, &stp->st_access_bmap)) {
3612 dprintk("NFSD:access not a subset current bitmap: 0x%lx, input access=%08x\n",
3613 stp->st_access_bmap, od->od_share_access);
3616 if (!test_bit(od->od_share_deny, &stp->st_deny_bmap)) {
3617 dprintk("NFSD:deny not a subset current bitmap: 0x%lx, input deny=%08x\n",
3618 stp->st_deny_bmap, od->od_share_deny);
3621 nfs4_stateid_downgrade(stp, od->od_share_access);
3623 reset_union_bmap_deny(od->od_share_deny, &stp->st_deny_bmap);
3625 update_stateid(&stp->st_stid.sc_stateid);
3626 memcpy(&od->od_stateid, &stp->st_stid.sc_stateid, sizeof(stateid_t));
3629 if (!cstate->replay_owner)
3630 nfs4_unlock_state();
3634 void nfsd4_purge_closed_stateid(struct nfs4_stateowner *so)
3636 struct nfs4_openowner *oo;
3637 struct nfs4_ol_stateid *s;
3639 if (!so->so_is_open_owner)
3642 s = oo->oo_last_closed_stid;
3645 if (!(oo->oo_flags & NFS4_OO_PURGE_CLOSE)) {
3646 /* Release the last_closed_stid on the next seqid bump: */
3647 oo->oo_flags |= NFS4_OO_PURGE_CLOSE;
3650 oo->oo_flags &= ~NFS4_OO_PURGE_CLOSE;
3651 release_last_closed_stateid(oo);
3654 static void nfsd4_close_open_stateid(struct nfs4_ol_stateid *s)
3656 unhash_open_stateid(s);
3657 s->st_stid.sc_type = NFS4_CLOSED_STID;
3661 * nfs4_unlock_state() called after encode
3664 nfsd4_close(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
3665 struct nfsd4_close *close)
3668 struct nfs4_openowner *oo;
3669 struct nfs4_ol_stateid *stp;
3671 dprintk("NFSD: nfsd4_close on file %.*s\n",
3672 (int)cstate->current_fh.fh_dentry->d_name.len,
3673 cstate->current_fh.fh_dentry->d_name.name);
3676 status = nfs4_preprocess_seqid_op(cstate, close->cl_seqid,
3678 NFS4_OPEN_STID|NFS4_CLOSED_STID,
3682 oo = openowner(stp->st_stateowner);
3684 update_stateid(&stp->st_stid.sc_stateid);
3685 memcpy(&close->cl_stateid, &stp->st_stid.sc_stateid, sizeof(stateid_t));
3687 nfsd4_close_open_stateid(stp);
3688 release_last_closed_stateid(oo);
3689 oo->oo_last_closed_stid = stp;
3691 /* place unused nfs4_stateowners on so_close_lru list to be
3692 * released by the laundromat service after the lease period
3693 * to enable us to handle CLOSE replay
3695 if (list_empty(&oo->oo_owner.so_stateids))
3696 move_to_close_lru(oo);
3698 if (!cstate->replay_owner)
3699 nfs4_unlock_state();
3704 nfsd4_delegreturn(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
3705 struct nfsd4_delegreturn *dr)
3707 struct nfs4_delegation *dp;
3708 stateid_t *stateid = &dr->dr_stateid;
3709 struct nfs4_stid *s;
3710 struct inode *inode;
3713 if ((status = fh_verify(rqstp, &cstate->current_fh, S_IFREG, 0)))
3715 inode = cstate->current_fh.fh_dentry->d_inode;
3718 status = nfsd4_lookup_stateid(stateid, NFS4_DELEG_STID, &s);
3721 dp = delegstateid(s);
3722 status = check_stateid_generation(stateid, &dp->dl_stid.sc_stateid, nfsd4_has_session(cstate));
3726 unhash_delegation(dp);
3728 nfs4_unlock_state();
3735 * Lock owner state (byte-range locks)
3737 #define LOFF_OVERFLOW(start, len) ((u64)(len) > ~(u64)(start))
3738 #define LOCK_HASH_BITS 8
3739 #define LOCK_HASH_SIZE (1 << LOCK_HASH_BITS)
3740 #define LOCK_HASH_MASK (LOCK_HASH_SIZE - 1)
3743 end_offset(u64 start, u64 len)
3748 return end >= start ? end: NFS4_MAX_UINT64;
3751 /* last octet in a range */
3753 last_byte_offset(u64 start, u64 len)
3759 return end > start ? end - 1: NFS4_MAX_UINT64;
3762 static inline unsigned int
3763 lock_ownerstr_hashval(struct inode *inode, u32 cl_id,
3764 struct xdr_netobj *ownername)
3766 return (file_hashval(inode) + cl_id
3767 + opaque_hashval(ownername->data, ownername->len))
3771 static struct list_head lock_ownerstr_hashtbl[LOCK_HASH_SIZE];
3774 * TODO: Linux file offsets are _signed_ 64-bit quantities, which means that
3775 * we can't properly handle lock requests that go beyond the (2^63 - 1)-th
3776 * byte, because of sign extension problems. Since NFSv4 calls for 64-bit
3777 * locking, this prevents us from being completely protocol-compliant. The
3778 * real solution to this problem is to start using unsigned file offsets in
3779 * the VFS, but this is a very deep change!
3782 nfs4_transform_lock_offset(struct file_lock *lock)
3784 if (lock->fl_start < 0)
3785 lock->fl_start = OFFSET_MAX;
3786 if (lock->fl_end < 0)
3787 lock->fl_end = OFFSET_MAX;
3790 /* Hack!: For now, we're defining this just so we can use a pointer to it
3791 * as a unique cookie to identify our (NFSv4's) posix locks. */
3792 static const struct lock_manager_operations nfsd_posix_mng_ops = {
3796 nfs4_set_lock_denied(struct file_lock *fl, struct nfsd4_lock_denied *deny)
3798 struct nfs4_lockowner *lo;
3800 if (fl->fl_lmops == &nfsd_posix_mng_ops) {
3801 lo = (struct nfs4_lockowner *) fl->fl_owner;
3802 deny->ld_owner.data = kmemdup(lo->lo_owner.so_owner.data,
3803 lo->lo_owner.so_owner.len, GFP_KERNEL);
3804 if (!deny->ld_owner.data)
3805 /* We just don't care that much */
3807 deny->ld_owner.len = lo->lo_owner.so_owner.len;
3808 deny->ld_clientid = lo->lo_owner.so_client->cl_clientid;
3811 deny->ld_owner.len = 0;
3812 deny->ld_owner.data = NULL;
3813 deny->ld_clientid.cl_boot = 0;
3814 deny->ld_clientid.cl_id = 0;
3816 deny->ld_start = fl->fl_start;
3817 deny->ld_length = NFS4_MAX_UINT64;
3818 if (fl->fl_end != NFS4_MAX_UINT64)
3819 deny->ld_length = fl->fl_end - fl->fl_start + 1;
3820 deny->ld_type = NFS4_READ_LT;
3821 if (fl->fl_type != F_RDLCK)
3822 deny->ld_type = NFS4_WRITE_LT;
3825 static bool same_lockowner_ino(struct nfs4_lockowner *lo, struct inode *inode, clientid_t *clid, struct xdr_netobj *owner)
3827 struct nfs4_ol_stateid *lst;
3829 if (!same_owner_str(&lo->lo_owner, owner, clid))
3831 if (list_empty(&lo->lo_owner.so_stateids)) {
3835 lst = list_first_entry(&lo->lo_owner.so_stateids,
3836 struct nfs4_ol_stateid, st_perstateowner);
3837 return lst->st_file->fi_inode == inode;
3840 static struct nfs4_lockowner *
3841 find_lockowner_str(struct inode *inode, clientid_t *clid,
3842 struct xdr_netobj *owner)
3844 unsigned int hashval = lock_ownerstr_hashval(inode, clid->cl_id, owner);
3845 struct nfs4_lockowner *lo;
3846 struct nfs4_stateowner *op;
3848 list_for_each_entry(op, &lock_ownerstr_hashtbl[hashval], so_strhash) {
3850 if (same_lockowner_ino(lo, inode, clid, owner))
3856 static void hash_lockowner(struct nfs4_lockowner *lo, unsigned int strhashval, struct nfs4_client *clp, struct nfs4_ol_stateid *open_stp)
3858 list_add(&lo->lo_owner.so_strhash, &lock_ownerstr_hashtbl[strhashval]);
3859 list_add(&lo->lo_perstateid, &open_stp->st_lockowners);
3863 * Alloc a lock owner structure.
3864 * Called in nfsd4_lock - therefore, OPEN and OPEN_CONFIRM (if needed) has
3867 * strhashval = lock_ownerstr_hashval
3870 static struct nfs4_lockowner *
3871 alloc_init_lock_stateowner(unsigned int strhashval, struct nfs4_client *clp, struct nfs4_ol_stateid *open_stp, struct nfsd4_lock *lock) {
3872 struct nfs4_lockowner *lo;
3874 lo = alloc_stateowner(lockowner_slab, &lock->lk_new_owner, clp);
3877 INIT_LIST_HEAD(&lo->lo_owner.so_stateids);
3878 lo->lo_owner.so_is_open_owner = 0;
3879 /* It is the openowner seqid that will be incremented in encode in the
3880 * case of new lockowners; so increment the lock seqid manually: */
3881 lo->lo_owner.so_seqid = lock->lk_new_lock_seqid + 1;
3882 hash_lockowner(lo, strhashval, clp, open_stp);
3886 static struct nfs4_ol_stateid *
3887 alloc_init_lock_stateid(struct nfs4_lockowner *lo, struct nfs4_file *fp, struct nfs4_ol_stateid *open_stp)
3889 struct nfs4_ol_stateid *stp;
3890 struct nfs4_client *clp = lo->lo_owner.so_client;
3892 stp = nfs4_alloc_stateid(clp);
3895 init_stid(&stp->st_stid, clp, NFS4_LOCK_STID);
3896 list_add(&stp->st_perfile, &fp->fi_stateids);
3897 list_add(&stp->st_perstateowner, &lo->lo_owner.so_stateids);
3898 stp->st_stateowner = &lo->lo_owner;
3901 stp->st_access_bmap = 0;
3902 stp->st_deny_bmap = open_stp->st_deny_bmap;
3903 stp->st_openstp = open_stp;
3908 check_lock_length(u64 offset, u64 length)
3910 return ((length == 0) || ((length != NFS4_MAX_UINT64) &&
3911 LOFF_OVERFLOW(offset, length)));
3914 static void get_lock_access(struct nfs4_ol_stateid *lock_stp, u32 access)
3916 struct nfs4_file *fp = lock_stp->st_file;
3917 int oflag = nfs4_access_to_omode(access);
3919 if (test_bit(access, &lock_stp->st_access_bmap))
3921 nfs4_file_get_access(fp, oflag);
3922 __set_bit(access, &lock_stp->st_access_bmap);
3929 nfsd4_lock(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
3930 struct nfsd4_lock *lock)
3932 struct nfs4_openowner *open_sop = NULL;
3933 struct nfs4_lockowner *lock_sop = NULL;
3934 struct nfs4_ol_stateid *lock_stp;
3935 struct nfs4_file *fp;
3936 struct file *filp = NULL;
3937 struct file_lock file_lock;
3938 struct file_lock conflock;
3940 unsigned int strhashval;
3944 dprintk("NFSD: nfsd4_lock: start=%Ld length=%Ld\n",
3945 (long long) lock->lk_offset,
3946 (long long) lock->lk_length);
3948 if (check_lock_length(lock->lk_offset, lock->lk_length))
3949 return nfserr_inval;
3951 if ((status = fh_verify(rqstp, &cstate->current_fh,
3952 S_IFREG, NFSD_MAY_LOCK))) {
3953 dprintk("NFSD: nfsd4_lock: permission denied!\n");
3959 if (lock->lk_is_new) {
3961 * Client indicates that this is a new lockowner.
3962 * Use open owner and open stateid to create lock owner and
3965 struct nfs4_ol_stateid *open_stp = NULL;
3967 status = nfserr_stale_clientid;
3968 if (!nfsd4_has_session(cstate) &&
3969 STALE_CLIENTID(&lock->lk_new_clientid))
3972 /* validate and update open stateid and open seqid */
3973 status = nfs4_preprocess_confirmed_seqid_op(cstate,
3974 lock->lk_new_open_seqid,
3975 &lock->lk_new_open_stateid,
3979 open_sop = openowner(open_stp->st_stateowner);
3980 status = nfserr_bad_stateid;
3981 if (!nfsd4_has_session(cstate) &&
3982 !same_clid(&open_sop->oo_owner.so_client->cl_clientid,
3983 &lock->v.new.clientid))
3985 /* create lockowner and lock stateid */
3986 fp = open_stp->st_file;
3987 strhashval = lock_ownerstr_hashval(fp->fi_inode,
3988 open_sop->oo_owner.so_client->cl_clientid.cl_id,
3989 &lock->v.new.owner);
3990 /* XXX: Do we need to check for duplicate stateowners on
3991 * the same file, or should they just be allowed (and
3992 * create new stateids)? */
3993 status = nfserr_jukebox;
3994 lock_sop = alloc_init_lock_stateowner(strhashval,
3995 open_sop->oo_owner.so_client, open_stp, lock);
3996 if (lock_sop == NULL)
3998 lock_stp = alloc_init_lock_stateid(lock_sop, fp, open_stp);
3999 if (lock_stp == NULL)
4002 /* lock (lock owner + lock stateid) already exists */
4003 status = nfs4_preprocess_seqid_op(cstate,
4004 lock->lk_old_lock_seqid,
4005 &lock->lk_old_lock_stateid,
4006 NFS4_LOCK_STID, &lock_stp);
4009 lock_sop = lockowner(lock_stp->st_stateowner);
4010 fp = lock_stp->st_file;
4012 /* lock_sop and lock_stp have been created or found */
4014 lkflg = setlkflg(lock->lk_type);
4015 status = nfs4_check_openmode(lock_stp, lkflg);
4019 status = nfserr_grace;
4020 if (locks_in_grace() && !lock->lk_reclaim)
4022 status = nfserr_no_grace;
4023 if (!locks_in_grace() && lock->lk_reclaim)
4026 locks_init_lock(&file_lock);
4027 switch (lock->lk_type) {
4030 filp = find_readable_file(lock_stp->st_file);
4032 get_lock_access(lock_stp, NFS4_SHARE_ACCESS_READ);
4033 file_lock.fl_type = F_RDLCK;
4036 case NFS4_WRITEW_LT:
4037 filp = find_writeable_file(lock_stp->st_file);
4039 get_lock_access(lock_stp, NFS4_SHARE_ACCESS_WRITE);
4040 file_lock.fl_type = F_WRLCK;
4043 status = nfserr_inval;
4047 status = nfserr_openmode;
4050 file_lock.fl_owner = (fl_owner_t)lock_sop;
4051 file_lock.fl_pid = current->tgid;
4052 file_lock.fl_file = filp;
4053 file_lock.fl_flags = FL_POSIX;
4054 file_lock.fl_lmops = &nfsd_posix_mng_ops;
4056 file_lock.fl_start = lock->lk_offset;
4057 file_lock.fl_end = last_byte_offset(lock->lk_offset, lock->lk_length);
4058 nfs4_transform_lock_offset(&file_lock);
4061 * Try to lock the file in the VFS.
4062 * Note: locks.c uses the BKL to protect the inode's lock list.
4065 err = vfs_lock_file(filp, F_SETLK, &file_lock, &conflock);
4067 case 0: /* success! */
4068 update_stateid(&lock_stp->st_stid.sc_stateid);
4069 memcpy(&lock->lk_resp_stateid, &lock_stp->st_stid.sc_stateid,
4073 case (EAGAIN): /* conflock holds conflicting lock */
4074 status = nfserr_denied;
4075 dprintk("NFSD: nfsd4_lock: conflicting lock found!\n");
4076 nfs4_set_lock_denied(&conflock, &lock->lk_denied);
4079 status = nfserr_deadlock;
4082 dprintk("NFSD: nfsd4_lock: vfs_lock_file() failed! status %d\n",err);
4083 status = nfserrno(err);
4087 if (status && lock->lk_is_new && lock_sop)
4088 release_lockowner(lock_sop);
4089 if (!cstate->replay_owner)
4090 nfs4_unlock_state();
4095 * The NFSv4 spec allows a client to do a LOCKT without holding an OPEN,
4096 * so we do a temporary open here just to get an open file to pass to
4097 * vfs_test_lock. (Arguably perhaps test_lock should be done with an
4100 static __be32 nfsd_test_lock(struct svc_rqst *rqstp, struct svc_fh *fhp, struct file_lock *lock)
4103 __be32 err = nfsd_open(rqstp, fhp, S_IFREG, NFSD_MAY_READ, &file);
4105 err = nfserrno(vfs_test_lock(file, lock));
4115 nfsd4_lockt(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
4116 struct nfsd4_lockt *lockt)
4118 struct inode *inode;
4119 struct file_lock file_lock;
4120 struct nfs4_lockowner *lo;
4123 if (locks_in_grace())
4124 return nfserr_grace;
4126 if (check_lock_length(lockt->lt_offset, lockt->lt_length))
4127 return nfserr_inval;
4131 status = nfserr_stale_clientid;
4132 if (!nfsd4_has_session(cstate) && STALE_CLIENTID(&lockt->lt_clientid))
4135 if ((status = fh_verify(rqstp, &cstate->current_fh, S_IFREG, 0)))
4138 inode = cstate->current_fh.fh_dentry->d_inode;
4139 locks_init_lock(&file_lock);
4140 switch (lockt->lt_type) {
4143 file_lock.fl_type = F_RDLCK;
4146 case NFS4_WRITEW_LT:
4147 file_lock.fl_type = F_WRLCK;
4150 dprintk("NFSD: nfs4_lockt: bad lock type!\n");
4151 status = nfserr_inval;
4155 lo = find_lockowner_str(inode, &lockt->lt_clientid, &lockt->lt_owner);
4157 file_lock.fl_owner = (fl_owner_t)lo;
4158 file_lock.fl_pid = current->tgid;
4159 file_lock.fl_flags = FL_POSIX;
4161 file_lock.fl_start = lockt->lt_offset;
4162 file_lock.fl_end = last_byte_offset(lockt->lt_offset, lockt->lt_length);
4164 nfs4_transform_lock_offset(&file_lock);
4166 status = nfsd_test_lock(rqstp, &cstate->current_fh, &file_lock);
4170 if (file_lock.fl_type != F_UNLCK) {
4171 status = nfserr_denied;
4172 nfs4_set_lock_denied(&file_lock, &lockt->lt_denied);
4175 nfs4_unlock_state();
4180 nfsd4_locku(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
4181 struct nfsd4_locku *locku)
4183 struct nfs4_ol_stateid *stp;
4184 struct file *filp = NULL;
4185 struct file_lock file_lock;
4189 dprintk("NFSD: nfsd4_locku: start=%Ld length=%Ld\n",
4190 (long long) locku->lu_offset,
4191 (long long) locku->lu_length);
4193 if (check_lock_length(locku->lu_offset, locku->lu_length))
4194 return nfserr_inval;
4198 status = nfs4_preprocess_seqid_op(cstate, locku->lu_seqid,
4199 &locku->lu_stateid, NFS4_LOCK_STID, &stp);
4202 filp = find_any_file(stp->st_file);
4204 status = nfserr_lock_range;
4208 locks_init_lock(&file_lock);
4209 file_lock.fl_type = F_UNLCK;
4210 file_lock.fl_owner = (fl_owner_t)lockowner(stp->st_stateowner);
4211 file_lock.fl_pid = current->tgid;
4212 file_lock.fl_file = filp;
4213 file_lock.fl_flags = FL_POSIX;
4214 file_lock.fl_lmops = &nfsd_posix_mng_ops;
4215 file_lock.fl_start = locku->lu_offset;
4217 file_lock.fl_end = last_byte_offset(locku->lu_offset, locku->lu_length);
4218 nfs4_transform_lock_offset(&file_lock);
4221 * Try to unlock the file in the VFS.
4223 err = vfs_lock_file(filp, F_SETLK, &file_lock, NULL);
4225 dprintk("NFSD: nfs4_locku: vfs_lock_file failed!\n");
4229 * OK, unlock succeeded; the only thing left to do is update the stateid.
4231 update_stateid(&stp->st_stid.sc_stateid);
4232 memcpy(&locku->lu_stateid, &stp->st_stid.sc_stateid, sizeof(stateid_t));
4235 if (!cstate->replay_owner)
4236 nfs4_unlock_state();
4240 status = nfserrno(err);
4246 * 1: locks held by lockowner
4247 * 0: no locks held by lockowner
4250 check_for_locks(struct nfs4_file *filp, struct nfs4_lockowner *lowner)
4252 struct file_lock **flpp;
4253 struct inode *inode = filp->fi_inode;
4257 for (flpp = &inode->i_flock; *flpp != NULL; flpp = &(*flpp)->fl_next) {
4258 if ((*flpp)->fl_owner == (fl_owner_t)lowner) {
4269 nfsd4_release_lockowner(struct svc_rqst *rqstp,
4270 struct nfsd4_compound_state *cstate,
4271 struct nfsd4_release_lockowner *rlockowner)
4273 clientid_t *clid = &rlockowner->rl_clientid;
4274 struct nfs4_stateowner *sop;
4275 struct nfs4_lockowner *lo;
4276 struct nfs4_ol_stateid *stp;
4277 struct xdr_netobj *owner = &rlockowner->rl_owner;
4278 struct list_head matches;
4282 dprintk("nfsd4_release_lockowner clientid: (%08x/%08x):\n",
4283 clid->cl_boot, clid->cl_id);
4285 /* XXX check for lease expiration */
4287 status = nfserr_stale_clientid;
4288 if (STALE_CLIENTID(clid))
4293 status = nfserr_locks_held;
4294 /* XXX: we're doing a linear search through all the lockowners.
4295 * Yipes! For now we'll just hope clients aren't really using
4296 * release_lockowner much, but eventually we have to fix these
4297 * data structures. */
4298 INIT_LIST_HEAD(&matches);
4299 for (i = 0; i < LOCK_HASH_SIZE; i++) {
4300 list_for_each_entry(sop, &lock_ownerstr_hashtbl[i], so_strhash) {
4301 if (!same_owner_str(sop, owner, clid))
4303 list_for_each_entry(stp, &sop->so_stateids,
4305 lo = lockowner(sop);
4306 if (check_for_locks(stp->st_file, lo))
4308 list_add(&lo->lo_list, &matches);
4312 /* Clients probably won't expect us to return with some (but not all)
4313 * of the lockowner state released; so don't release any until all
4314 * have been checked. */
4316 while (!list_empty(&matches)) {
4317 lo = list_entry(matches.next, struct nfs4_lockowner,
4319 /* unhash_stateowner deletes so_perclient only
4320 * for openowners. */
4321 list_del(&lo->lo_list);
4322 release_lockowner(lo);
4325 nfs4_unlock_state();
4329 static inline struct nfs4_client_reclaim *
4332 return kmalloc(sizeof(struct nfs4_client_reclaim), GFP_KERNEL);
4336 nfs4_has_reclaimed_state(const char *name, bool use_exchange_id)
4338 unsigned int strhashval = clientstr_hashval(name);
4339 struct nfs4_client *clp;
4341 clp = find_confirmed_client_by_str(name, strhashval);
4346 * failure => all reset bets are off, nfserr_no_grace...
4349 nfs4_client_to_reclaim(const char *name)
4351 unsigned int strhashval;
4352 struct nfs4_client_reclaim *crp = NULL;
4354 dprintk("NFSD nfs4_client_to_reclaim NAME: %.*s\n", HEXDIR_LEN, name);
4355 crp = alloc_reclaim();
4358 strhashval = clientstr_hashval(name);
4359 INIT_LIST_HEAD(&crp->cr_strhash);
4360 list_add(&crp->cr_strhash, &reclaim_str_hashtbl[strhashval]);
4361 memcpy(crp->cr_recdir, name, HEXDIR_LEN);
4362 reclaim_str_hashtbl_size++;
4367 nfs4_release_reclaim(void)
4369 struct nfs4_client_reclaim *crp = NULL;
4372 for (i = 0; i < CLIENT_HASH_SIZE; i++) {
4373 while (!list_empty(&reclaim_str_hashtbl[i])) {
4374 crp = list_entry(reclaim_str_hashtbl[i].next,
4375 struct nfs4_client_reclaim, cr_strhash);
4376 list_del(&crp->cr_strhash);
4378 reclaim_str_hashtbl_size--;
4381 BUG_ON(reclaim_str_hashtbl_size);
4385 * called from OPEN, CLAIM_PREVIOUS with a new clientid. */
4386 static struct nfs4_client_reclaim *
4387 nfs4_find_reclaim_client(clientid_t *clid)
4389 unsigned int strhashval;
4390 struct nfs4_client *clp;
4391 struct nfs4_client_reclaim *crp = NULL;
4394 /* find clientid in conf_id_hashtbl */
4395 clp = find_confirmed_client(clid);
4399 dprintk("NFSD: nfs4_find_reclaim_client for %.*s with recdir %s\n",
4400 clp->cl_name.len, clp->cl_name.data,
4403 /* find clp->cl_name in reclaim_str_hashtbl */
4404 strhashval = clientstr_hashval(clp->cl_recdir);
4405 list_for_each_entry(crp, &reclaim_str_hashtbl[strhashval], cr_strhash) {
4406 if (same_name(crp->cr_recdir, clp->cl_recdir)) {
4414 * Called from OPEN. Look for clientid in reclaim list.
4417 nfs4_check_open_reclaim(clientid_t *clid)
4419 return nfs4_find_reclaim_client(clid) ? nfs_ok : nfserr_reclaim_bad;
4422 /* initialization to perform at module load time: */
4425 nfs4_state_init(void)
4429 status = nfsd4_init_slabs();
4432 for (i = 0; i < CLIENT_HASH_SIZE; i++) {
4433 INIT_LIST_HEAD(&conf_id_hashtbl[i]);
4434 INIT_LIST_HEAD(&conf_str_hashtbl[i]);
4435 INIT_LIST_HEAD(&unconf_str_hashtbl[i]);
4436 INIT_LIST_HEAD(&unconf_id_hashtbl[i]);
4437 INIT_LIST_HEAD(&reclaim_str_hashtbl[i]);
4439 for (i = 0; i < SESSION_HASH_SIZE; i++)
4440 INIT_LIST_HEAD(&sessionid_hashtbl[i]);
4441 for (i = 0; i < FILE_HASH_SIZE; i++) {
4442 INIT_LIST_HEAD(&file_hashtbl[i]);
4444 for (i = 0; i < OPEN_OWNER_HASH_SIZE; i++) {
4445 INIT_LIST_HEAD(&open_ownerstr_hashtbl[i]);
4447 for (i = 0; i < LOCK_HASH_SIZE; i++) {
4448 INIT_LIST_HEAD(&lock_ownerstr_hashtbl[i]);
4450 memset(&onestateid, ~0, sizeof(stateid_t));
4451 INIT_LIST_HEAD(&close_lru);
4452 INIT_LIST_HEAD(&client_lru);
4453 INIT_LIST_HEAD(&del_recall_lru);
4454 reclaim_str_hashtbl_size = 0;
4459 nfsd4_load_reboot_recovery_data(void)
4464 nfsd4_init_recdir();
4465 status = nfsd4_recdir_load();
4466 nfs4_unlock_state();
4468 printk("NFSD: Failure reading reboot recovery data\n");
4472 * Since the lifetime of a delegation isn't limited to that of an open, a
4473 * client may quite reasonably hang on to a delegation as long as it has
4474 * the inode cached. This becomes an obvious problem the first time a
4475 * client's inode cache approaches the size of the server's total memory.
4477 * For now we avoid this problem by imposing a hard limit on the number
4478 * of delegations, which varies according to the server's memory size.
4481 set_max_delegations(void)
4484 * Allow at most 4 delegations per megabyte of RAM. Quick
4485 * estimates suggest that in the worst case (where every delegation
4486 * is for a different inode), a delegation could take about 1.5K,
4487 * giving a worst case usage of about 6% of memory.
4489 max_delegations = nr_free_buffer_pages() >> (20 - 2 - PAGE_SHIFT);
4492 /* initialization to perform when the nfsd service is started: */
4495 __nfs4_state_start(void)
4499 boot_time = get_seconds();
4500 locks_start_grace(&nfsd4_manager);
4501 printk(KERN_INFO "NFSD: starting %ld-second grace period\n",
4503 ret = set_callback_cred();
4506 laundry_wq = create_singlethread_workqueue("nfsd4");
4507 if (laundry_wq == NULL)
4509 ret = nfsd4_create_callback_queue();
4511 goto out_free_laundry;
4512 queue_delayed_work(laundry_wq, &laundromat_work, nfsd4_grace * HZ);
4513 set_max_delegations();
4516 destroy_workqueue(laundry_wq);
4521 nfs4_state_start(void)
4523 nfsd4_load_reboot_recovery_data();
4524 return __nfs4_state_start();
4528 __nfs4_state_shutdown(void)
4531 struct nfs4_client *clp = NULL;
4532 struct nfs4_delegation *dp = NULL;
4533 struct list_head *pos, *next, reaplist;
4535 for (i = 0; i < CLIENT_HASH_SIZE; i++) {
4536 while (!list_empty(&conf_id_hashtbl[i])) {
4537 clp = list_entry(conf_id_hashtbl[i].next, struct nfs4_client, cl_idhash);
4540 while (!list_empty(&unconf_str_hashtbl[i])) {
4541 clp = list_entry(unconf_str_hashtbl[i].next, struct nfs4_client, cl_strhash);
4545 INIT_LIST_HEAD(&reaplist);
4546 spin_lock(&recall_lock);
4547 list_for_each_safe(pos, next, &del_recall_lru) {
4548 dp = list_entry (pos, struct nfs4_delegation, dl_recall_lru);
4549 list_move(&dp->dl_recall_lru, &reaplist);
4551 spin_unlock(&recall_lock);
4552 list_for_each_safe(pos, next, &reaplist) {
4553 dp = list_entry (pos, struct nfs4_delegation, dl_recall_lru);
4554 list_del_init(&dp->dl_recall_lru);
4555 unhash_delegation(dp);
4558 nfsd4_shutdown_recdir();
4562 nfs4_state_shutdown(void)
4564 cancel_delayed_work_sync(&laundromat_work);
4565 destroy_workqueue(laundry_wq);
4566 locks_end_grace(&nfsd4_manager);
4568 nfs4_release_reclaim();
4569 __nfs4_state_shutdown();
4570 nfs4_unlock_state();
4571 nfsd4_destroy_callback_queue();