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;
53 #define all_ones {{~0,~0},~0}
54 static const stateid_t one_stateid = {
56 .si_opaque = all_ones,
58 static const stateid_t zero_stateid = {
61 static const stateid_t currentstateid = {
65 static u64 current_sessionid = 1;
67 #define ZERO_STATEID(stateid) (!memcmp((stateid), &zero_stateid, sizeof(stateid_t)))
68 #define ONE_STATEID(stateid) (!memcmp((stateid), &one_stateid, sizeof(stateid_t)))
69 #define CURRENT_STATEID(stateid) (!memcmp((stateid), ¤tstateid, sizeof(stateid_t)))
71 /* forward declarations */
72 static int check_for_locks(struct nfs4_file *filp, struct nfs4_lockowner *lowner);
76 /* Currently used for almost all code touching nfsv4 state: */
77 static DEFINE_MUTEX(client_mutex);
80 * Currently used for the del_recall_lru and file hash table. In an
81 * effort to decrease the scope of the client_mutex, this spinlock may
82 * eventually cover more:
84 static DEFINE_SPINLOCK(recall_lock);
86 static struct kmem_cache *openowner_slab = NULL;
87 static struct kmem_cache *lockowner_slab = NULL;
88 static struct kmem_cache *file_slab = NULL;
89 static struct kmem_cache *stateid_slab = NULL;
90 static struct kmem_cache *deleg_slab = NULL;
95 mutex_lock(&client_mutex);
98 static void free_session(struct kref *);
100 /* Must be called under the client_lock */
101 static void nfsd4_put_session_locked(struct nfsd4_session *ses)
103 kref_put(&ses->se_ref, free_session);
106 static void nfsd4_get_session(struct nfsd4_session *ses)
108 kref_get(&ses->se_ref);
112 nfs4_unlock_state(void)
114 mutex_unlock(&client_mutex);
118 opaque_hashval(const void *ptr, int nbytes)
120 unsigned char *cptr = (unsigned char *) ptr;
130 static struct list_head del_recall_lru;
132 static void nfsd4_free_file(struct nfs4_file *f)
134 kmem_cache_free(file_slab, f);
138 put_nfs4_file(struct nfs4_file *fi)
140 if (atomic_dec_and_lock(&fi->fi_ref, &recall_lock)) {
141 list_del(&fi->fi_hash);
142 spin_unlock(&recall_lock);
149 get_nfs4_file(struct nfs4_file *fi)
151 atomic_inc(&fi->fi_ref);
154 static int num_delegations;
155 unsigned int max_delegations;
158 * Open owner state (share locks)
161 /* hash tables for lock and open owners */
162 #define OWNER_HASH_BITS 8
163 #define OWNER_HASH_SIZE (1 << OWNER_HASH_BITS)
164 #define OWNER_HASH_MASK (OWNER_HASH_SIZE - 1)
166 static unsigned int ownerstr_hashval(u32 clientid, struct xdr_netobj *ownername)
170 ret = opaque_hashval(ownername->data, ownername->len);
172 return ret & OWNER_HASH_MASK;
175 static struct list_head ownerstr_hashtbl[OWNER_HASH_SIZE];
177 /* hash table for nfs4_file */
178 #define FILE_HASH_BITS 8
179 #define FILE_HASH_SIZE (1 << FILE_HASH_BITS)
181 static unsigned int file_hashval(struct inode *ino)
183 /* XXX: why are we hashing on inode pointer, anyway? */
184 return hash_ptr(ino, FILE_HASH_BITS);
187 static struct list_head file_hashtbl[FILE_HASH_SIZE];
189 static void __nfs4_file_get_access(struct nfs4_file *fp, int oflag)
191 BUG_ON(!(fp->fi_fds[oflag] || fp->fi_fds[O_RDWR]));
192 atomic_inc(&fp->fi_access[oflag]);
195 static void nfs4_file_get_access(struct nfs4_file *fp, int oflag)
197 if (oflag == O_RDWR) {
198 __nfs4_file_get_access(fp, O_RDONLY);
199 __nfs4_file_get_access(fp, O_WRONLY);
201 __nfs4_file_get_access(fp, oflag);
204 static void nfs4_file_put_fd(struct nfs4_file *fp, int oflag)
206 if (fp->fi_fds[oflag]) {
207 fput(fp->fi_fds[oflag]);
208 fp->fi_fds[oflag] = NULL;
212 static void __nfs4_file_put_access(struct nfs4_file *fp, int oflag)
214 if (atomic_dec_and_test(&fp->fi_access[oflag])) {
215 nfs4_file_put_fd(fp, oflag);
217 * It's also safe to get rid of the RDWR open *if*
218 * we no longer have need of the other kind of access
219 * or if we already have the other kind of open:
221 if (fp->fi_fds[1-oflag]
222 || atomic_read(&fp->fi_access[1 - oflag]) == 0)
223 nfs4_file_put_fd(fp, O_RDWR);
227 static void nfs4_file_put_access(struct nfs4_file *fp, int oflag)
229 if (oflag == O_RDWR) {
230 __nfs4_file_put_access(fp, O_RDONLY);
231 __nfs4_file_put_access(fp, O_WRONLY);
233 __nfs4_file_put_access(fp, oflag);
236 static inline int get_new_stid(struct nfs4_stid *stid)
238 static int min_stateid = 0;
239 struct idr *stateids = &stid->sc_client->cl_stateids;
243 error = idr_get_new_above(stateids, stid, min_stateid, &new_stid);
245 * Note: the necessary preallocation was done in
246 * nfs4_alloc_stateid(). The idr code caps the number of
247 * preallocations that can exist at a time, but the state lock
248 * prevents anyone from using ours before we get here:
252 * It shouldn't be a problem to reuse an opaque stateid value.
253 * I don't think it is for 4.1. But with 4.0 I worry that, for
254 * example, a stray write retransmission could be accepted by
255 * the server when it should have been rejected. Therefore,
256 * adopt a trick from the sctp code to attempt to maximize the
257 * amount of time until an id is reused, by ensuring they always
258 * "increase" (mod INT_MAX):
261 min_stateid = new_stid+1;
262 if (min_stateid == INT_MAX)
267 static void init_stid(struct nfs4_stid *stid, struct nfs4_client *cl, unsigned char type)
269 stateid_t *s = &stid->sc_stateid;
272 stid->sc_type = type;
273 stid->sc_client = cl;
274 s->si_opaque.so_clid = cl->cl_clientid;
275 new_id = get_new_stid(stid);
276 s->si_opaque.so_id = (u32)new_id;
277 /* Will be incremented before return to client: */
278 s->si_generation = 0;
281 static struct nfs4_stid *nfs4_alloc_stid(struct nfs4_client *cl, struct kmem_cache *slab)
283 struct idr *stateids = &cl->cl_stateids;
285 if (!idr_pre_get(stateids, GFP_KERNEL))
288 * Note: if we fail here (or any time between now and the time
289 * we actually get the new idr), we won't need to undo the idr
290 * preallocation, since the idr code caps the number of
291 * preallocated entries.
293 return kmem_cache_alloc(slab, GFP_KERNEL);
296 static struct nfs4_ol_stateid * nfs4_alloc_stateid(struct nfs4_client *clp)
298 return openlockstateid(nfs4_alloc_stid(clp, stateid_slab));
301 static struct nfs4_delegation *
302 alloc_init_deleg(struct nfs4_client *clp, struct nfs4_ol_stateid *stp, struct svc_fh *current_fh, u32 type)
304 struct nfs4_delegation *dp;
305 struct nfs4_file *fp = stp->st_file;
307 dprintk("NFSD alloc_init_deleg\n");
309 * Major work on the lease subsystem (for example, to support
310 * calbacks on stat) will be required before we can support
311 * write delegations properly.
313 if (type != NFS4_OPEN_DELEGATE_READ)
315 if (fp->fi_had_conflict)
317 if (num_delegations > max_delegations)
319 dp = delegstateid(nfs4_alloc_stid(clp, deleg_slab));
322 init_stid(&dp->dl_stid, clp, NFS4_DELEG_STID);
324 * delegation seqid's are never incremented. The 4.1 special
325 * meaning of seqid 0 isn't meaningful, really, but let's avoid
326 * 0 anyway just for consistency and use 1:
328 dp->dl_stid.sc_stateid.si_generation = 1;
330 INIT_LIST_HEAD(&dp->dl_perfile);
331 INIT_LIST_HEAD(&dp->dl_perclnt);
332 INIT_LIST_HEAD(&dp->dl_recall_lru);
336 fh_copy_shallow(&dp->dl_fh, ¤t_fh->fh_handle);
338 atomic_set(&dp->dl_count, 1);
339 INIT_WORK(&dp->dl_recall.cb_work, nfsd4_do_callback_rpc);
344 nfs4_put_delegation(struct nfs4_delegation *dp)
346 if (atomic_dec_and_test(&dp->dl_count)) {
347 dprintk("NFSD: freeing dp %p\n",dp);
348 put_nfs4_file(dp->dl_file);
349 kmem_cache_free(deleg_slab, dp);
354 static void nfs4_put_deleg_lease(struct nfs4_file *fp)
356 if (atomic_dec_and_test(&fp->fi_delegees)) {
357 vfs_setlease(fp->fi_deleg_file, F_UNLCK, &fp->fi_lease);
359 fput(fp->fi_deleg_file);
360 fp->fi_deleg_file = NULL;
364 static void unhash_stid(struct nfs4_stid *s)
366 struct idr *stateids = &s->sc_client->cl_stateids;
368 idr_remove(stateids, s->sc_stateid.si_opaque.so_id);
371 /* Called under the state lock. */
373 unhash_delegation(struct nfs4_delegation *dp)
375 unhash_stid(&dp->dl_stid);
376 list_del_init(&dp->dl_perclnt);
377 spin_lock(&recall_lock);
378 list_del_init(&dp->dl_perfile);
379 list_del_init(&dp->dl_recall_lru);
380 spin_unlock(&recall_lock);
381 nfs4_put_deleg_lease(dp->dl_file);
382 nfs4_put_delegation(dp);
389 /* client_lock protects the client lru list and session hash table */
390 static DEFINE_SPINLOCK(client_lock);
392 /* Hash tables for nfs4_clientid state */
393 #define CLIENT_HASH_BITS 4
394 #define CLIENT_HASH_SIZE (1 << CLIENT_HASH_BITS)
395 #define CLIENT_HASH_MASK (CLIENT_HASH_SIZE - 1)
397 static unsigned int clientid_hashval(u32 id)
399 return id & CLIENT_HASH_MASK;
402 static unsigned int clientstr_hashval(const char *name)
404 return opaque_hashval(name, 8) & CLIENT_HASH_MASK;
408 * reclaim_str_hashtbl[] holds known client info from previous reset/reboot
409 * used in reboot/reset lease grace period processing
411 * conf_id_hashtbl[], and conf_str_hashtbl[] hold confirmed
412 * setclientid_confirmed info.
414 * unconf_str_hastbl[] and unconf_id_hashtbl[] hold unconfirmed
417 * client_lru holds client queue ordered by nfs4_client.cl_time
420 * close_lru holds (open) stateowner queue ordered by nfs4_stateowner.so_time
421 * for last close replay.
423 static struct list_head reclaim_str_hashtbl[CLIENT_HASH_SIZE];
424 static int reclaim_str_hashtbl_size = 0;
425 static struct list_head conf_id_hashtbl[CLIENT_HASH_SIZE];
426 static struct list_head conf_str_hashtbl[CLIENT_HASH_SIZE];
427 static struct list_head unconf_str_hashtbl[CLIENT_HASH_SIZE];
428 static struct list_head unconf_id_hashtbl[CLIENT_HASH_SIZE];
429 static struct list_head client_lru;
430 static struct list_head close_lru;
433 * We store the NONE, READ, WRITE, and BOTH bits separately in the
434 * st_{access,deny}_bmap field of the stateid, in order to track not
435 * only what share bits are currently in force, but also what
436 * combinations of share bits previous opens have used. This allows us
437 * to enforce the recommendation of rfc 3530 14.2.19 that the server
438 * return an error if the client attempt to downgrade to a combination
439 * of share bits not explicable by closing some of its previous opens.
441 * XXX: This enforcement is actually incomplete, since we don't keep
442 * track of access/deny bit combinations; so, e.g., we allow:
444 * OPEN allow read, deny write
445 * OPEN allow both, deny none
446 * DOWNGRADE allow read, deny none
448 * which we should reject.
451 set_access(unsigned int *access, unsigned long bmap) {
455 for (i = 1; i < 4; i++) {
456 if (test_bit(i, &bmap))
462 set_deny(unsigned int *deny, unsigned long bmap) {
466 for (i = 0; i < 4; i++) {
467 if (test_bit(i, &bmap))
473 test_share(struct nfs4_ol_stateid *stp, struct nfsd4_open *open) {
474 unsigned int access, deny;
476 set_access(&access, stp->st_access_bmap);
477 set_deny(&deny, stp->st_deny_bmap);
478 if ((access & open->op_share_deny) || (deny & open->op_share_access))
483 static int nfs4_access_to_omode(u32 access)
485 switch (access & NFS4_SHARE_ACCESS_BOTH) {
486 case NFS4_SHARE_ACCESS_READ:
488 case NFS4_SHARE_ACCESS_WRITE:
490 case NFS4_SHARE_ACCESS_BOTH:
496 static void unhash_generic_stateid(struct nfs4_ol_stateid *stp)
498 list_del(&stp->st_perfile);
499 list_del(&stp->st_perstateowner);
502 static void close_generic_stateid(struct nfs4_ol_stateid *stp)
506 if (stp->st_access_bmap) {
507 for (i = 1; i < 4; i++) {
508 if (test_bit(i, &stp->st_access_bmap))
509 nfs4_file_put_access(stp->st_file,
510 nfs4_access_to_omode(i));
511 __clear_bit(i, &stp->st_access_bmap);
514 put_nfs4_file(stp->st_file);
518 static void free_generic_stateid(struct nfs4_ol_stateid *stp)
520 kmem_cache_free(stateid_slab, stp);
523 static void release_lock_stateid(struct nfs4_ol_stateid *stp)
527 unhash_generic_stateid(stp);
528 unhash_stid(&stp->st_stid);
529 file = find_any_file(stp->st_file);
531 locks_remove_posix(file, (fl_owner_t)lockowner(stp->st_stateowner));
532 close_generic_stateid(stp);
533 free_generic_stateid(stp);
536 static void unhash_lockowner(struct nfs4_lockowner *lo)
538 struct nfs4_ol_stateid *stp;
540 list_del(&lo->lo_owner.so_strhash);
541 list_del(&lo->lo_perstateid);
542 list_del(&lo->lo_owner_ino_hash);
543 while (!list_empty(&lo->lo_owner.so_stateids)) {
544 stp = list_first_entry(&lo->lo_owner.so_stateids,
545 struct nfs4_ol_stateid, st_perstateowner);
546 release_lock_stateid(stp);
550 static void release_lockowner(struct nfs4_lockowner *lo)
552 unhash_lockowner(lo);
553 nfs4_free_lockowner(lo);
557 release_stateid_lockowners(struct nfs4_ol_stateid *open_stp)
559 struct nfs4_lockowner *lo;
561 while (!list_empty(&open_stp->st_lockowners)) {
562 lo = list_entry(open_stp->st_lockowners.next,
563 struct nfs4_lockowner, lo_perstateid);
564 release_lockowner(lo);
568 static void unhash_open_stateid(struct nfs4_ol_stateid *stp)
570 unhash_generic_stateid(stp);
571 release_stateid_lockowners(stp);
572 close_generic_stateid(stp);
575 static void release_open_stateid(struct nfs4_ol_stateid *stp)
577 unhash_open_stateid(stp);
578 unhash_stid(&stp->st_stid);
579 free_generic_stateid(stp);
582 static void unhash_openowner(struct nfs4_openowner *oo)
584 struct nfs4_ol_stateid *stp;
586 list_del(&oo->oo_owner.so_strhash);
587 list_del(&oo->oo_perclient);
588 while (!list_empty(&oo->oo_owner.so_stateids)) {
589 stp = list_first_entry(&oo->oo_owner.so_stateids,
590 struct nfs4_ol_stateid, st_perstateowner);
591 release_open_stateid(stp);
595 static void release_last_closed_stateid(struct nfs4_openowner *oo)
597 struct nfs4_ol_stateid *s = oo->oo_last_closed_stid;
600 unhash_stid(&s->st_stid);
601 free_generic_stateid(s);
602 oo->oo_last_closed_stid = NULL;
606 static void release_openowner(struct nfs4_openowner *oo)
608 unhash_openowner(oo);
609 list_del(&oo->oo_close_lru);
610 release_last_closed_stateid(oo);
611 nfs4_free_openowner(oo);
614 #define SESSION_HASH_SIZE 512
615 static struct list_head sessionid_hashtbl[SESSION_HASH_SIZE];
618 hash_sessionid(struct nfs4_sessionid *sessionid)
620 struct nfsd4_sessionid *sid = (struct nfsd4_sessionid *)sessionid;
622 return sid->sequence % SESSION_HASH_SIZE;
627 dump_sessionid(const char *fn, struct nfs4_sessionid *sessionid)
629 u32 *ptr = (u32 *)(&sessionid->data[0]);
630 dprintk("%s: %u:%u:%u:%u\n", fn, ptr[0], ptr[1], ptr[2], ptr[3]);
634 dump_sessionid(const char *fn, struct nfs4_sessionid *sessionid)
641 gen_sessionid(struct nfsd4_session *ses)
643 struct nfs4_client *clp = ses->se_client;
644 struct nfsd4_sessionid *sid;
646 sid = (struct nfsd4_sessionid *)ses->se_sessionid.data;
647 sid->clientid = clp->cl_clientid;
648 sid->sequence = current_sessionid++;
653 * The protocol defines ca_maxresponssize_cached to include the size of
654 * the rpc header, but all we need to cache is the data starting after
655 * the end of the initial SEQUENCE operation--the rest we regenerate
656 * each time. Therefore we can advertise a ca_maxresponssize_cached
657 * value that is the number of bytes in our cache plus a few additional
658 * bytes. In order to stay on the safe side, and not promise more than
659 * we can cache, those additional bytes must be the minimum possible: 24
660 * bytes of rpc header (xid through accept state, with AUTH_NULL
661 * verifier), 12 for the compound header (with zero-length tag), and 44
662 * for the SEQUENCE op response:
664 #define NFSD_MIN_HDR_SEQ_SZ (24 + 12 + 44)
667 free_session_slots(struct nfsd4_session *ses)
671 for (i = 0; i < ses->se_fchannel.maxreqs; i++)
672 kfree(ses->se_slots[i]);
676 * We don't actually need to cache the rpc and session headers, so we
677 * can allocate a little less for each slot:
679 static inline int slot_bytes(struct nfsd4_channel_attrs *ca)
681 return ca->maxresp_cached - NFSD_MIN_HDR_SEQ_SZ;
684 static int nfsd4_sanitize_slot_size(u32 size)
686 size -= NFSD_MIN_HDR_SEQ_SZ; /* We don't cache the rpc header */
687 size = min_t(u32, size, NFSD_SLOT_CACHE_SIZE);
693 * XXX: If we run out of reserved DRC memory we could (up to a point)
694 * re-negotiate active sessions and reduce their slot usage to make
695 * room for new connections. For now we just fail the create session.
697 static int nfsd4_get_drc_mem(int slotsize, u32 num)
701 num = min_t(u32, num, NFSD_MAX_SLOTS_PER_SESSION);
703 spin_lock(&nfsd_drc_lock);
704 avail = min_t(int, NFSD_MAX_MEM_PER_SESSION,
705 nfsd_drc_max_mem - nfsd_drc_mem_used);
706 num = min_t(int, num, avail / slotsize);
707 nfsd_drc_mem_used += num * slotsize;
708 spin_unlock(&nfsd_drc_lock);
713 static void nfsd4_put_drc_mem(int slotsize, int num)
715 spin_lock(&nfsd_drc_lock);
716 nfsd_drc_mem_used -= slotsize * num;
717 spin_unlock(&nfsd_drc_lock);
720 static struct nfsd4_session *alloc_session(int slotsize, int numslots)
722 struct nfsd4_session *new;
725 BUILD_BUG_ON(NFSD_MAX_SLOTS_PER_SESSION * sizeof(struct nfsd4_slot *)
726 + sizeof(struct nfsd4_session) > PAGE_SIZE);
727 mem = numslots * sizeof(struct nfsd4_slot *);
729 new = kzalloc(sizeof(*new) + mem, GFP_KERNEL);
732 /* allocate each struct nfsd4_slot and data cache in one piece */
733 for (i = 0; i < numslots; i++) {
734 mem = sizeof(struct nfsd4_slot) + slotsize;
735 new->se_slots[i] = kzalloc(mem, GFP_KERNEL);
736 if (!new->se_slots[i])
742 kfree(new->se_slots[i]);
747 static void init_forechannel_attrs(struct nfsd4_channel_attrs *new, struct nfsd4_channel_attrs *req, int numslots, int slotsize)
749 u32 maxrpc = nfsd_serv->sv_max_mesg;
751 new->maxreqs = numslots;
752 new->maxresp_cached = min_t(u32, req->maxresp_cached,
753 slotsize + NFSD_MIN_HDR_SEQ_SZ);
754 new->maxreq_sz = min_t(u32, req->maxreq_sz, maxrpc);
755 new->maxresp_sz = min_t(u32, req->maxresp_sz, maxrpc);
756 new->maxops = min_t(u32, req->maxops, NFSD_MAX_OPS_PER_COMPOUND);
759 static void free_conn(struct nfsd4_conn *c)
761 svc_xprt_put(c->cn_xprt);
765 static void nfsd4_conn_lost(struct svc_xpt_user *u)
767 struct nfsd4_conn *c = container_of(u, struct nfsd4_conn, cn_xpt_user);
768 struct nfs4_client *clp = c->cn_session->se_client;
770 spin_lock(&clp->cl_lock);
771 if (!list_empty(&c->cn_persession)) {
772 list_del(&c->cn_persession);
775 spin_unlock(&clp->cl_lock);
776 nfsd4_probe_callback(clp);
779 static struct nfsd4_conn *alloc_conn(struct svc_rqst *rqstp, u32 flags)
781 struct nfsd4_conn *conn;
783 conn = kmalloc(sizeof(struct nfsd4_conn), GFP_KERNEL);
786 svc_xprt_get(rqstp->rq_xprt);
787 conn->cn_xprt = rqstp->rq_xprt;
788 conn->cn_flags = flags;
789 INIT_LIST_HEAD(&conn->cn_xpt_user.list);
793 static void __nfsd4_hash_conn(struct nfsd4_conn *conn, struct nfsd4_session *ses)
795 conn->cn_session = ses;
796 list_add(&conn->cn_persession, &ses->se_conns);
799 static void nfsd4_hash_conn(struct nfsd4_conn *conn, struct nfsd4_session *ses)
801 struct nfs4_client *clp = ses->se_client;
803 spin_lock(&clp->cl_lock);
804 __nfsd4_hash_conn(conn, ses);
805 spin_unlock(&clp->cl_lock);
808 static int nfsd4_register_conn(struct nfsd4_conn *conn)
810 conn->cn_xpt_user.callback = nfsd4_conn_lost;
811 return register_xpt_user(conn->cn_xprt, &conn->cn_xpt_user);
814 static __be32 nfsd4_new_conn(struct svc_rqst *rqstp, struct nfsd4_session *ses, u32 dir)
816 struct nfsd4_conn *conn;
819 conn = alloc_conn(rqstp, dir);
821 return nfserr_jukebox;
822 nfsd4_hash_conn(conn, ses);
823 ret = nfsd4_register_conn(conn);
825 /* oops; xprt is already down: */
826 nfsd4_conn_lost(&conn->cn_xpt_user);
830 static __be32 nfsd4_new_conn_from_crses(struct svc_rqst *rqstp, struct nfsd4_session *ses)
832 u32 dir = NFS4_CDFC4_FORE;
834 if (ses->se_flags & SESSION4_BACK_CHAN)
835 dir |= NFS4_CDFC4_BACK;
837 return nfsd4_new_conn(rqstp, ses, dir);
840 /* must be called under client_lock */
841 static void nfsd4_del_conns(struct nfsd4_session *s)
843 struct nfs4_client *clp = s->se_client;
844 struct nfsd4_conn *c;
846 spin_lock(&clp->cl_lock);
847 while (!list_empty(&s->se_conns)) {
848 c = list_first_entry(&s->se_conns, struct nfsd4_conn, cn_persession);
849 list_del_init(&c->cn_persession);
850 spin_unlock(&clp->cl_lock);
852 unregister_xpt_user(c->cn_xprt, &c->cn_xpt_user);
855 spin_lock(&clp->cl_lock);
857 spin_unlock(&clp->cl_lock);
860 static void free_session(struct kref *kref)
862 struct nfsd4_session *ses;
865 BUG_ON(!spin_is_locked(&client_lock));
866 ses = container_of(kref, struct nfsd4_session, se_ref);
867 nfsd4_del_conns(ses);
868 spin_lock(&nfsd_drc_lock);
869 mem = ses->se_fchannel.maxreqs * slot_bytes(&ses->se_fchannel);
870 nfsd_drc_mem_used -= mem;
871 spin_unlock(&nfsd_drc_lock);
872 free_session_slots(ses);
876 void nfsd4_put_session(struct nfsd4_session *ses)
878 spin_lock(&client_lock);
879 nfsd4_put_session_locked(ses);
880 spin_unlock(&client_lock);
883 static struct nfsd4_session *alloc_init_session(struct svc_rqst *rqstp, struct nfs4_client *clp, struct nfsd4_create_session *cses)
885 struct nfsd4_session *new;
886 struct nfsd4_channel_attrs *fchan = &cses->fore_channel;
887 int numslots, slotsize;
892 * Note decreasing slot size below client's request may
893 * make it difficult for client to function correctly, whereas
894 * decreasing the number of slots will (just?) affect
895 * performance. When short on memory we therefore prefer to
896 * decrease number of slots instead of their size.
898 slotsize = nfsd4_sanitize_slot_size(fchan->maxresp_cached);
899 numslots = nfsd4_get_drc_mem(slotsize, fchan->maxreqs);
903 new = alloc_session(slotsize, numslots);
905 nfsd4_put_drc_mem(slotsize, fchan->maxreqs);
908 init_forechannel_attrs(&new->se_fchannel, fchan, numslots, slotsize);
910 new->se_client = clp;
913 INIT_LIST_HEAD(&new->se_conns);
915 new->se_cb_seq_nr = 1;
916 new->se_flags = cses->flags;
917 new->se_cb_prog = cses->callback_prog;
918 kref_init(&new->se_ref);
919 idx = hash_sessionid(&new->se_sessionid);
920 spin_lock(&client_lock);
921 list_add(&new->se_hash, &sessionid_hashtbl[idx]);
922 spin_lock(&clp->cl_lock);
923 list_add(&new->se_perclnt, &clp->cl_sessions);
924 spin_unlock(&clp->cl_lock);
925 spin_unlock(&client_lock);
927 status = nfsd4_new_conn_from_crses(rqstp, new);
928 /* whoops: benny points out, status is ignored! (err, or bogus) */
930 spin_lock(&client_lock);
931 free_session(&new->se_ref);
932 spin_unlock(&client_lock);
935 if (cses->flags & SESSION4_BACK_CHAN) {
936 struct sockaddr *sa = svc_addr(rqstp);
938 * This is a little silly; with sessions there's no real
939 * use for the callback address. Use the peer address
940 * as a reasonable default for now, but consider fixing
941 * the rpc client not to require an address in the
944 rpc_copy_addr((struct sockaddr *)&clp->cl_cb_conn.cb_addr, sa);
945 clp->cl_cb_conn.cb_addrlen = svc_addr_len(sa);
947 nfsd4_probe_callback(clp);
951 /* caller must hold client_lock */
952 static struct nfsd4_session *
953 find_in_sessionid_hashtbl(struct nfs4_sessionid *sessionid)
955 struct nfsd4_session *elem;
958 dump_sessionid(__func__, sessionid);
959 idx = hash_sessionid(sessionid);
960 /* Search in the appropriate list */
961 list_for_each_entry(elem, &sessionid_hashtbl[idx], se_hash) {
962 if (!memcmp(elem->se_sessionid.data, sessionid->data,
963 NFS4_MAX_SESSIONID_LEN)) {
968 dprintk("%s: session not found\n", __func__);
972 /* caller must hold client_lock */
974 unhash_session(struct nfsd4_session *ses)
976 list_del(&ses->se_hash);
977 spin_lock(&ses->se_client->cl_lock);
978 list_del(&ses->se_perclnt);
979 spin_unlock(&ses->se_client->cl_lock);
982 /* must be called under the client_lock */
984 renew_client_locked(struct nfs4_client *clp)
986 if (is_client_expired(clp)) {
987 dprintk("%s: client (clientid %08x/%08x) already expired\n",
989 clp->cl_clientid.cl_boot,
990 clp->cl_clientid.cl_id);
994 dprintk("renewing client (clientid %08x/%08x)\n",
995 clp->cl_clientid.cl_boot,
996 clp->cl_clientid.cl_id);
997 list_move_tail(&clp->cl_lru, &client_lru);
998 clp->cl_time = get_seconds();
1002 renew_client(struct nfs4_client *clp)
1004 spin_lock(&client_lock);
1005 renew_client_locked(clp);
1006 spin_unlock(&client_lock);
1009 /* SETCLIENTID and SETCLIENTID_CONFIRM Helper functions */
1011 STALE_CLIENTID(clientid_t *clid)
1013 if (clid->cl_boot == boot_time)
1015 dprintk("NFSD stale clientid (%08x/%08x) boot_time %08lx\n",
1016 clid->cl_boot, clid->cl_id, boot_time);
1021 * XXX Should we use a slab cache ?
1022 * This type of memory management is somewhat inefficient, but we use it
1023 * anyway since SETCLIENTID is not a common operation.
1025 static struct nfs4_client *alloc_client(struct xdr_netobj name)
1027 struct nfs4_client *clp;
1029 clp = kzalloc(sizeof(struct nfs4_client), GFP_KERNEL);
1032 clp->cl_name.data = kmemdup(name.data, name.len, GFP_KERNEL);
1033 if (clp->cl_name.data == NULL) {
1037 clp->cl_name.len = name.len;
1042 free_client(struct nfs4_client *clp)
1044 BUG_ON(!spin_is_locked(&client_lock));
1045 while (!list_empty(&clp->cl_sessions)) {
1046 struct nfsd4_session *ses;
1047 ses = list_entry(clp->cl_sessions.next, struct nfsd4_session,
1049 list_del(&ses->se_perclnt);
1050 nfsd4_put_session_locked(ses);
1052 if (clp->cl_cred.cr_group_info)
1053 put_group_info(clp->cl_cred.cr_group_info);
1054 kfree(clp->cl_principal);
1055 kfree(clp->cl_name.data);
1060 release_session_client(struct nfsd4_session *session)
1062 struct nfs4_client *clp = session->se_client;
1064 if (!atomic_dec_and_lock(&clp->cl_refcount, &client_lock))
1066 if (is_client_expired(clp)) {
1068 session->se_client = NULL;
1070 renew_client_locked(clp);
1071 spin_unlock(&client_lock);
1074 /* must be called under the client_lock */
1076 unhash_client_locked(struct nfs4_client *clp)
1078 struct nfsd4_session *ses;
1080 mark_client_expired(clp);
1081 list_del(&clp->cl_lru);
1082 spin_lock(&clp->cl_lock);
1083 list_for_each_entry(ses, &clp->cl_sessions, se_perclnt)
1084 list_del_init(&ses->se_hash);
1085 spin_unlock(&clp->cl_lock);
1089 expire_client(struct nfs4_client *clp)
1091 struct nfs4_openowner *oo;
1092 struct nfs4_delegation *dp;
1093 struct list_head reaplist;
1095 INIT_LIST_HEAD(&reaplist);
1096 spin_lock(&recall_lock);
1097 while (!list_empty(&clp->cl_delegations)) {
1098 dp = list_entry(clp->cl_delegations.next, struct nfs4_delegation, dl_perclnt);
1099 list_del_init(&dp->dl_perclnt);
1100 list_move(&dp->dl_recall_lru, &reaplist);
1102 spin_unlock(&recall_lock);
1103 while (!list_empty(&reaplist)) {
1104 dp = list_entry(reaplist.next, struct nfs4_delegation, dl_recall_lru);
1105 unhash_delegation(dp);
1107 while (!list_empty(&clp->cl_openowners)) {
1108 oo = list_entry(clp->cl_openowners.next, struct nfs4_openowner, oo_perclient);
1109 release_openowner(oo);
1111 nfsd4_shutdown_callback(clp);
1112 if (clp->cl_cb_conn.cb_xprt)
1113 svc_xprt_put(clp->cl_cb_conn.cb_xprt);
1114 list_del(&clp->cl_idhash);
1115 list_del(&clp->cl_strhash);
1116 spin_lock(&client_lock);
1117 unhash_client_locked(clp);
1118 if (atomic_read(&clp->cl_refcount) == 0)
1120 spin_unlock(&client_lock);
1123 static void copy_verf(struct nfs4_client *target, nfs4_verifier *source)
1125 memcpy(target->cl_verifier.data, source->data,
1126 sizeof(target->cl_verifier.data));
1129 static void copy_clid(struct nfs4_client *target, struct nfs4_client *source)
1131 target->cl_clientid.cl_boot = source->cl_clientid.cl_boot;
1132 target->cl_clientid.cl_id = source->cl_clientid.cl_id;
1135 static void copy_cred(struct svc_cred *target, struct svc_cred *source)
1137 target->cr_uid = source->cr_uid;
1138 target->cr_gid = source->cr_gid;
1139 target->cr_group_info = source->cr_group_info;
1140 get_group_info(target->cr_group_info);
1143 static int same_name(const char *n1, const char *n2)
1145 return 0 == memcmp(n1, n2, HEXDIR_LEN);
1149 same_verf(nfs4_verifier *v1, nfs4_verifier *v2)
1151 return 0 == memcmp(v1->data, v2->data, sizeof(v1->data));
1155 same_clid(clientid_t *cl1, clientid_t *cl2)
1157 return (cl1->cl_boot == cl2->cl_boot) && (cl1->cl_id == cl2->cl_id);
1160 /* XXX what about NGROUP */
1162 same_creds(struct svc_cred *cr1, struct svc_cred *cr2)
1164 return cr1->cr_uid == cr2->cr_uid;
1167 static void gen_clid(struct nfs4_client *clp)
1169 static u32 current_clientid = 1;
1171 clp->cl_clientid.cl_boot = boot_time;
1172 clp->cl_clientid.cl_id = current_clientid++;
1175 static void gen_confirm(struct nfs4_client *clp)
1180 verf[0] = (__be32)get_seconds();
1181 verf[1] = (__be32)i++;
1182 memcpy(clp->cl_confirm.data, verf, sizeof(clp->cl_confirm.data));
1185 static struct nfs4_stid *find_stateid(struct nfs4_client *cl, stateid_t *t)
1187 return idr_find(&cl->cl_stateids, t->si_opaque.so_id);
1190 static struct nfs4_stid *find_stateid_by_type(struct nfs4_client *cl, stateid_t *t, char typemask)
1192 struct nfs4_stid *s;
1194 s = find_stateid(cl, t);
1197 if (typemask & s->sc_type)
1202 static struct nfs4_client *create_client(struct xdr_netobj name, char *recdir,
1203 struct svc_rqst *rqstp, nfs4_verifier *verf)
1205 struct nfs4_client *clp;
1206 struct sockaddr *sa = svc_addr(rqstp);
1209 clp = alloc_client(name);
1213 INIT_LIST_HEAD(&clp->cl_sessions);
1215 princ = svc_gss_principal(rqstp);
1217 clp->cl_principal = kstrdup(princ, GFP_KERNEL);
1218 if (clp->cl_principal == NULL) {
1219 spin_lock(&client_lock);
1221 spin_unlock(&client_lock);
1226 idr_init(&clp->cl_stateids);
1227 memcpy(clp->cl_recdir, recdir, HEXDIR_LEN);
1228 atomic_set(&clp->cl_refcount, 0);
1229 clp->cl_cb_state = NFSD4_CB_UNKNOWN;
1230 INIT_LIST_HEAD(&clp->cl_idhash);
1231 INIT_LIST_HEAD(&clp->cl_strhash);
1232 INIT_LIST_HEAD(&clp->cl_openowners);
1233 INIT_LIST_HEAD(&clp->cl_delegations);
1234 INIT_LIST_HEAD(&clp->cl_lru);
1235 INIT_LIST_HEAD(&clp->cl_callbacks);
1236 spin_lock_init(&clp->cl_lock);
1237 INIT_WORK(&clp->cl_cb_null.cb_work, nfsd4_do_callback_rpc);
1238 clp->cl_time = get_seconds();
1239 clear_bit(0, &clp->cl_cb_slot_busy);
1240 rpc_init_wait_queue(&clp->cl_cb_waitq, "Backchannel slot table");
1241 copy_verf(clp, verf);
1242 rpc_copy_addr((struct sockaddr *) &clp->cl_addr, sa);
1243 clp->cl_flavor = rqstp->rq_flavor;
1244 copy_cred(&clp->cl_cred, &rqstp->rq_cred);
1246 clp->cl_cb_session = NULL;
1251 add_to_unconfirmed(struct nfs4_client *clp, unsigned int strhashval)
1253 unsigned int idhashval;
1255 list_add(&clp->cl_strhash, &unconf_str_hashtbl[strhashval]);
1256 idhashval = clientid_hashval(clp->cl_clientid.cl_id);
1257 list_add(&clp->cl_idhash, &unconf_id_hashtbl[idhashval]);
1262 move_to_confirmed(struct nfs4_client *clp)
1264 unsigned int idhashval = clientid_hashval(clp->cl_clientid.cl_id);
1265 unsigned int strhashval;
1267 dprintk("NFSD: move_to_confirm nfs4_client %p\n", clp);
1268 list_move(&clp->cl_idhash, &conf_id_hashtbl[idhashval]);
1269 strhashval = clientstr_hashval(clp->cl_recdir);
1270 list_move(&clp->cl_strhash, &conf_str_hashtbl[strhashval]);
1274 static struct nfs4_client *
1275 find_confirmed_client(clientid_t *clid)
1277 struct nfs4_client *clp;
1278 unsigned int idhashval = clientid_hashval(clid->cl_id);
1280 list_for_each_entry(clp, &conf_id_hashtbl[idhashval], cl_idhash) {
1281 if (same_clid(&clp->cl_clientid, clid)) {
1289 static struct nfs4_client *
1290 find_unconfirmed_client(clientid_t *clid)
1292 struct nfs4_client *clp;
1293 unsigned int idhashval = clientid_hashval(clid->cl_id);
1295 list_for_each_entry(clp, &unconf_id_hashtbl[idhashval], cl_idhash) {
1296 if (same_clid(&clp->cl_clientid, clid))
1302 static bool clp_used_exchangeid(struct nfs4_client *clp)
1304 return clp->cl_exchange_flags != 0;
1307 static struct nfs4_client *
1308 find_confirmed_client_by_str(const char *dname, unsigned int hashval)
1310 struct nfs4_client *clp;
1312 list_for_each_entry(clp, &conf_str_hashtbl[hashval], cl_strhash) {
1313 if (same_name(clp->cl_recdir, dname))
1319 static struct nfs4_client *
1320 find_unconfirmed_client_by_str(const char *dname, unsigned int hashval)
1322 struct nfs4_client *clp;
1324 list_for_each_entry(clp, &unconf_str_hashtbl[hashval], cl_strhash) {
1325 if (same_name(clp->cl_recdir, dname))
1332 gen_callback(struct nfs4_client *clp, struct nfsd4_setclientid *se, struct svc_rqst *rqstp)
1334 struct nfs4_cb_conn *conn = &clp->cl_cb_conn;
1335 struct sockaddr *sa = svc_addr(rqstp);
1336 u32 scopeid = rpc_get_scope_id(sa);
1337 unsigned short expected_family;
1339 /* Currently, we only support tcp and tcp6 for the callback channel */
1340 if (se->se_callback_netid_len == 3 &&
1341 !memcmp(se->se_callback_netid_val, "tcp", 3))
1342 expected_family = AF_INET;
1343 else if (se->se_callback_netid_len == 4 &&
1344 !memcmp(se->se_callback_netid_val, "tcp6", 4))
1345 expected_family = AF_INET6;
1349 conn->cb_addrlen = rpc_uaddr2sockaddr(&init_net, se->se_callback_addr_val,
1350 se->se_callback_addr_len,
1351 (struct sockaddr *)&conn->cb_addr,
1352 sizeof(conn->cb_addr));
1354 if (!conn->cb_addrlen || conn->cb_addr.ss_family != expected_family)
1357 if (conn->cb_addr.ss_family == AF_INET6)
1358 ((struct sockaddr_in6 *)&conn->cb_addr)->sin6_scope_id = scopeid;
1360 conn->cb_prog = se->se_callback_prog;
1361 conn->cb_ident = se->se_callback_ident;
1362 memcpy(&conn->cb_saddr, &rqstp->rq_daddr, rqstp->rq_daddrlen);
1365 conn->cb_addr.ss_family = AF_UNSPEC;
1366 conn->cb_addrlen = 0;
1367 dprintk(KERN_INFO "NFSD: this client (clientid %08x/%08x) "
1368 "will not receive delegations\n",
1369 clp->cl_clientid.cl_boot, clp->cl_clientid.cl_id);
1375 * Cache a reply. nfsd4_check_drc_limit() has bounded the cache size.
1378 nfsd4_store_cache_entry(struct nfsd4_compoundres *resp)
1380 struct nfsd4_slot *slot = resp->cstate.slot;
1383 dprintk("--> %s slot %p\n", __func__, slot);
1385 slot->sl_opcnt = resp->opcnt;
1386 slot->sl_status = resp->cstate.status;
1388 slot->sl_flags |= NFSD4_SLOT_INITIALIZED;
1389 if (nfsd4_not_cached(resp)) {
1390 slot->sl_datalen = 0;
1393 slot->sl_datalen = (char *)resp->p - (char *)resp->cstate.datap;
1394 base = (char *)resp->cstate.datap -
1395 (char *)resp->xbuf->head[0].iov_base;
1396 if (read_bytes_from_xdr_buf(resp->xbuf, base, slot->sl_data,
1398 WARN("%s: sessions DRC could not cache compound\n", __func__);
1403 * Encode the replay sequence operation from the slot values.
1404 * If cachethis is FALSE encode the uncached rep error on the next
1405 * operation which sets resp->p and increments resp->opcnt for
1406 * nfs4svc_encode_compoundres.
1410 nfsd4_enc_sequence_replay(struct nfsd4_compoundargs *args,
1411 struct nfsd4_compoundres *resp)
1413 struct nfsd4_op *op;
1414 struct nfsd4_slot *slot = resp->cstate.slot;
1416 /* Encode the replayed sequence operation */
1417 op = &args->ops[resp->opcnt - 1];
1418 nfsd4_encode_operation(resp, op);
1420 /* Return nfserr_retry_uncached_rep in next operation. */
1421 if (args->opcnt > 1 && !(slot->sl_flags & NFSD4_SLOT_CACHETHIS)) {
1422 op = &args->ops[resp->opcnt++];
1423 op->status = nfserr_retry_uncached_rep;
1424 nfsd4_encode_operation(resp, op);
1430 * The sequence operation is not cached because we can use the slot and
1434 nfsd4_replay_cache_entry(struct nfsd4_compoundres *resp,
1435 struct nfsd4_sequence *seq)
1437 struct nfsd4_slot *slot = resp->cstate.slot;
1440 dprintk("--> %s slot %p\n", __func__, slot);
1442 /* Either returns 0 or nfserr_retry_uncached */
1443 status = nfsd4_enc_sequence_replay(resp->rqstp->rq_argp, resp);
1444 if (status == nfserr_retry_uncached_rep)
1447 /* The sequence operation has been encoded, cstate->datap set. */
1448 memcpy(resp->cstate.datap, slot->sl_data, slot->sl_datalen);
1450 resp->opcnt = slot->sl_opcnt;
1451 resp->p = resp->cstate.datap + XDR_QUADLEN(slot->sl_datalen);
1452 status = slot->sl_status;
1458 * Set the exchange_id flags returned by the server.
1461 nfsd4_set_ex_flags(struct nfs4_client *new, struct nfsd4_exchange_id *clid)
1463 /* pNFS is not supported */
1464 new->cl_exchange_flags |= EXCHGID4_FLAG_USE_NON_PNFS;
1466 /* Referrals are supported, Migration is not. */
1467 new->cl_exchange_flags |= EXCHGID4_FLAG_SUPP_MOVED_REFER;
1469 /* set the wire flags to return to client. */
1470 clid->flags = new->cl_exchange_flags;
1474 nfsd4_exchange_id(struct svc_rqst *rqstp,
1475 struct nfsd4_compound_state *cstate,
1476 struct nfsd4_exchange_id *exid)
1478 struct nfs4_client *unconf, *conf, *new;
1480 unsigned int strhashval;
1481 char dname[HEXDIR_LEN];
1482 char addr_str[INET6_ADDRSTRLEN];
1483 nfs4_verifier verf = exid->verifier;
1484 struct sockaddr *sa = svc_addr(rqstp);
1486 rpc_ntop(sa, addr_str, sizeof(addr_str));
1487 dprintk("%s rqstp=%p exid=%p clname.len=%u clname.data=%p "
1488 "ip_addr=%s flags %x, spa_how %d\n",
1489 __func__, rqstp, exid, exid->clname.len, exid->clname.data,
1490 addr_str, exid->flags, exid->spa_how);
1492 if (exid->flags & ~EXCHGID4_FLAG_MASK_A)
1493 return nfserr_inval;
1495 /* Currently only support SP4_NONE */
1496 switch (exid->spa_how) {
1500 return nfserr_serverfault;
1502 BUG(); /* checked by xdr code */
1504 return nfserr_serverfault; /* no excuse :-/ */
1507 status = nfs4_make_rec_clidname(dname, &exid->clname);
1512 strhashval = clientstr_hashval(dname);
1517 conf = find_confirmed_client_by_str(dname, strhashval);
1519 if (!clp_used_exchangeid(conf)) {
1520 status = nfserr_clid_inuse; /* XXX: ? */
1523 if (!same_verf(&verf, &conf->cl_verifier)) {
1524 /* 18.35.4 case 8 */
1525 if (exid->flags & EXCHGID4_FLAG_UPD_CONFIRMED_REC_A) {
1526 status = nfserr_not_same;
1529 /* Client reboot: destroy old state */
1530 expire_client(conf);
1533 if (!same_creds(&conf->cl_cred, &rqstp->rq_cred)) {
1534 /* 18.35.4 case 9 */
1535 if (exid->flags & EXCHGID4_FLAG_UPD_CONFIRMED_REC_A) {
1536 status = nfserr_perm;
1539 expire_client(conf);
1543 * Set bit when the owner id and verifier map to an already
1544 * confirmed client id (18.35.3).
1546 exid->flags |= EXCHGID4_FLAG_CONFIRMED_R;
1549 * Falling into 18.35.4 case 2, possible router replay.
1550 * Leave confirmed record intact and return same result.
1552 copy_verf(conf, &verf);
1557 /* 18.35.4 case 7 */
1558 if (exid->flags & EXCHGID4_FLAG_UPD_CONFIRMED_REC_A) {
1559 status = nfserr_noent;
1563 unconf = find_unconfirmed_client_by_str(dname, strhashval);
1566 * Possible retry or client restart. Per 18.35.4 case 4,
1567 * a new unconfirmed record should be generated regardless
1568 * of whether any properties have changed.
1570 expire_client(unconf);
1575 new = create_client(exid->clname, dname, rqstp, &verf);
1577 status = nfserr_jukebox;
1582 add_to_unconfirmed(new, strhashval);
1584 exid->clientid.cl_boot = new->cl_clientid.cl_boot;
1585 exid->clientid.cl_id = new->cl_clientid.cl_id;
1588 nfsd4_set_ex_flags(new, exid);
1590 dprintk("nfsd4_exchange_id seqid %d flags %x\n",
1591 new->cl_cs_slot.sl_seqid, new->cl_exchange_flags);
1595 nfs4_unlock_state();
1597 dprintk("nfsd4_exchange_id returns %d\n", ntohl(status));
1602 check_slot_seqid(u32 seqid, u32 slot_seqid, int slot_inuse)
1604 dprintk("%s enter. seqid %d slot_seqid %d\n", __func__, seqid,
1607 /* The slot is in use, and no response has been sent. */
1609 if (seqid == slot_seqid)
1610 return nfserr_jukebox;
1612 return nfserr_seq_misordered;
1614 /* Note unsigned 32-bit arithmetic handles wraparound: */
1615 if (likely(seqid == slot_seqid + 1))
1617 if (seqid == slot_seqid)
1618 return nfserr_replay_cache;
1619 return nfserr_seq_misordered;
1623 * Cache the create session result into the create session single DRC
1624 * slot cache by saving the xdr structure. sl_seqid has been set.
1625 * Do this for solo or embedded create session operations.
1628 nfsd4_cache_create_session(struct nfsd4_create_session *cr_ses,
1629 struct nfsd4_clid_slot *slot, int nfserr)
1631 slot->sl_status = nfserr;
1632 memcpy(&slot->sl_cr_ses, cr_ses, sizeof(*cr_ses));
1636 nfsd4_replay_create_session(struct nfsd4_create_session *cr_ses,
1637 struct nfsd4_clid_slot *slot)
1639 memcpy(cr_ses, &slot->sl_cr_ses, sizeof(*cr_ses));
1640 return slot->sl_status;
1643 #define NFSD_MIN_REQ_HDR_SEQ_SZ ((\
1644 2 * 2 + /* credential,verifier: AUTH_NULL, length 0 */ \
1645 1 + /* MIN tag is length with zero, only length */ \
1646 3 + /* version, opcount, opcode */ \
1647 XDR_QUADLEN(NFS4_MAX_SESSIONID_LEN) + \
1648 /* seqid, slotID, slotID, cache */ \
1649 4 ) * sizeof(__be32))
1651 #define NFSD_MIN_RESP_HDR_SEQ_SZ ((\
1652 2 + /* verifier: AUTH_NULL, length 0 */\
1654 1 + /* MIN tag is length with zero, only length */ \
1655 3 + /* opcount, opcode, opstatus*/ \
1656 XDR_QUADLEN(NFS4_MAX_SESSIONID_LEN) + \
1657 /* seqid, slotID, slotID, slotID, status */ \
1658 5 ) * sizeof(__be32))
1660 static __be32 check_forechannel_attrs(struct nfsd4_channel_attrs fchannel)
1662 return fchannel.maxreq_sz < NFSD_MIN_REQ_HDR_SEQ_SZ
1663 || fchannel.maxresp_sz < NFSD_MIN_RESP_HDR_SEQ_SZ;
1667 nfsd4_create_session(struct svc_rqst *rqstp,
1668 struct nfsd4_compound_state *cstate,
1669 struct nfsd4_create_session *cr_ses)
1671 struct sockaddr *sa = svc_addr(rqstp);
1672 struct nfs4_client *conf, *unconf;
1673 struct nfsd4_session *new;
1674 struct nfsd4_clid_slot *cs_slot = NULL;
1675 bool confirm_me = false;
1678 if (cr_ses->flags & ~SESSION4_FLAG_MASK_A)
1679 return nfserr_inval;
1682 unconf = find_unconfirmed_client(&cr_ses->clientid);
1683 conf = find_confirmed_client(&cr_ses->clientid);
1686 cs_slot = &conf->cl_cs_slot;
1687 status = check_slot_seqid(cr_ses->seqid, cs_slot->sl_seqid, 0);
1688 if (status == nfserr_replay_cache) {
1689 dprintk("Got a create_session replay! seqid= %d\n",
1691 /* Return the cached reply status */
1692 status = nfsd4_replay_create_session(cr_ses, cs_slot);
1694 } else if (cr_ses->seqid != cs_slot->sl_seqid + 1) {
1695 status = nfserr_seq_misordered;
1696 dprintk("Sequence misordered!\n");
1697 dprintk("Expected seqid= %d but got seqid= %d\n",
1698 cs_slot->sl_seqid, cr_ses->seqid);
1701 } else if (unconf) {
1702 if (!same_creds(&unconf->cl_cred, &rqstp->rq_cred) ||
1703 !rpc_cmp_addr(sa, (struct sockaddr *) &unconf->cl_addr)) {
1704 status = nfserr_clid_inuse;
1708 cs_slot = &unconf->cl_cs_slot;
1709 status = check_slot_seqid(cr_ses->seqid, cs_slot->sl_seqid, 0);
1711 /* an unconfirmed replay returns misordered */
1712 status = nfserr_seq_misordered;
1719 status = nfserr_stale_clientid;
1724 * XXX: we should probably set this at creation time, and check
1725 * for consistent minorversion use throughout:
1727 conf->cl_minorversion = 1;
1729 * We do not support RDMA or persistent sessions
1731 cr_ses->flags &= ~SESSION4_PERSIST;
1732 cr_ses->flags &= ~SESSION4_RDMA;
1734 status = nfserr_toosmall;
1735 if (check_forechannel_attrs(cr_ses->fore_channel))
1738 status = nfserr_jukebox;
1739 new = alloc_init_session(rqstp, conf, cr_ses);
1743 memcpy(cr_ses->sessionid.data, new->se_sessionid.data,
1744 NFS4_MAX_SESSIONID_LEN);
1745 memcpy(&cr_ses->fore_channel, &new->se_fchannel,
1746 sizeof(struct nfsd4_channel_attrs));
1747 cs_slot->sl_seqid++;
1748 cr_ses->seqid = cs_slot->sl_seqid;
1750 /* cache solo and embedded create sessions under the state lock */
1751 nfsd4_cache_create_session(cr_ses, cs_slot, status);
1753 move_to_confirmed(conf);
1755 nfs4_unlock_state();
1756 dprintk("%s returns %d\n", __func__, ntohl(status));
1760 static bool nfsd4_last_compound_op(struct svc_rqst *rqstp)
1762 struct nfsd4_compoundres *resp = rqstp->rq_resp;
1763 struct nfsd4_compoundargs *argp = rqstp->rq_argp;
1765 return argp->opcnt == resp->opcnt;
1768 static __be32 nfsd4_map_bcts_dir(u32 *dir)
1771 case NFS4_CDFC4_FORE:
1772 case NFS4_CDFC4_BACK:
1774 case NFS4_CDFC4_FORE_OR_BOTH:
1775 case NFS4_CDFC4_BACK_OR_BOTH:
1776 *dir = NFS4_CDFC4_BOTH;
1779 return nfserr_inval;
1782 __be32 nfsd4_bind_conn_to_session(struct svc_rqst *rqstp,
1783 struct nfsd4_compound_state *cstate,
1784 struct nfsd4_bind_conn_to_session *bcts)
1788 if (!nfsd4_last_compound_op(rqstp))
1789 return nfserr_not_only_op;
1790 spin_lock(&client_lock);
1791 cstate->session = find_in_sessionid_hashtbl(&bcts->sessionid);
1792 /* Sorta weird: we only need the refcnt'ing because new_conn acquires
1793 * client_lock iself: */
1794 if (cstate->session) {
1795 nfsd4_get_session(cstate->session);
1796 atomic_inc(&cstate->session->se_client->cl_refcount);
1798 spin_unlock(&client_lock);
1799 if (!cstate->session)
1800 return nfserr_badsession;
1802 status = nfsd4_map_bcts_dir(&bcts->dir);
1804 nfsd4_new_conn(rqstp, cstate->session, bcts->dir);
1808 static bool nfsd4_compound_in_session(struct nfsd4_session *session, struct nfs4_sessionid *sid)
1812 return !memcmp(sid, &session->se_sessionid, sizeof(*sid));
1816 nfsd4_destroy_session(struct svc_rqst *r,
1817 struct nfsd4_compound_state *cstate,
1818 struct nfsd4_destroy_session *sessionid)
1820 struct nfsd4_session *ses;
1821 u32 status = nfserr_badsession;
1824 * - The confirmed nfs4_client->cl_sessionid holds destroyed sessinid
1825 * - Should we return nfserr_back_chan_busy if waiting for
1826 * callbacks on to-be-destroyed session?
1827 * - Do we need to clear any callback info from previous session?
1830 if (nfsd4_compound_in_session(cstate->session, &sessionid->sessionid)) {
1831 if (!nfsd4_last_compound_op(r))
1832 return nfserr_not_only_op;
1834 dump_sessionid(__func__, &sessionid->sessionid);
1835 spin_lock(&client_lock);
1836 ses = find_in_sessionid_hashtbl(&sessionid->sessionid);
1838 spin_unlock(&client_lock);
1842 unhash_session(ses);
1843 spin_unlock(&client_lock);
1846 nfsd4_probe_callback_sync(ses->se_client);
1847 nfs4_unlock_state();
1849 spin_lock(&client_lock);
1850 nfsd4_del_conns(ses);
1851 nfsd4_put_session_locked(ses);
1852 spin_unlock(&client_lock);
1855 dprintk("%s returns %d\n", __func__, ntohl(status));
1859 static struct nfsd4_conn *__nfsd4_find_conn(struct svc_xprt *xpt, struct nfsd4_session *s)
1861 struct nfsd4_conn *c;
1863 list_for_each_entry(c, &s->se_conns, cn_persession) {
1864 if (c->cn_xprt == xpt) {
1871 static void nfsd4_sequence_check_conn(struct nfsd4_conn *new, struct nfsd4_session *ses)
1873 struct nfs4_client *clp = ses->se_client;
1874 struct nfsd4_conn *c;
1877 spin_lock(&clp->cl_lock);
1878 c = __nfsd4_find_conn(new->cn_xprt, ses);
1880 spin_unlock(&clp->cl_lock);
1884 __nfsd4_hash_conn(new, ses);
1885 spin_unlock(&clp->cl_lock);
1886 ret = nfsd4_register_conn(new);
1888 /* oops; xprt is already down: */
1889 nfsd4_conn_lost(&new->cn_xpt_user);
1893 static bool nfsd4_session_too_many_ops(struct svc_rqst *rqstp, struct nfsd4_session *session)
1895 struct nfsd4_compoundargs *args = rqstp->rq_argp;
1897 return args->opcnt > session->se_fchannel.maxops;
1900 static bool nfsd4_request_too_big(struct svc_rqst *rqstp,
1901 struct nfsd4_session *session)
1903 struct xdr_buf *xb = &rqstp->rq_arg;
1905 return xb->len > session->se_fchannel.maxreq_sz;
1909 nfsd4_sequence(struct svc_rqst *rqstp,
1910 struct nfsd4_compound_state *cstate,
1911 struct nfsd4_sequence *seq)
1913 struct nfsd4_compoundres *resp = rqstp->rq_resp;
1914 struct nfsd4_session *session;
1915 struct nfsd4_slot *slot;
1916 struct nfsd4_conn *conn;
1919 if (resp->opcnt != 1)
1920 return nfserr_sequence_pos;
1923 * Will be either used or freed by nfsd4_sequence_check_conn
1926 conn = alloc_conn(rqstp, NFS4_CDFC4_FORE);
1928 return nfserr_jukebox;
1930 spin_lock(&client_lock);
1931 status = nfserr_badsession;
1932 session = find_in_sessionid_hashtbl(&seq->sessionid);
1936 status = nfserr_too_many_ops;
1937 if (nfsd4_session_too_many_ops(rqstp, session))
1940 status = nfserr_req_too_big;
1941 if (nfsd4_request_too_big(rqstp, session))
1944 status = nfserr_badslot;
1945 if (seq->slotid >= session->se_fchannel.maxreqs)
1948 slot = session->se_slots[seq->slotid];
1949 dprintk("%s: slotid %d\n", __func__, seq->slotid);
1951 /* We do not negotiate the number of slots yet, so set the
1952 * maxslots to the session maxreqs which is used to encode
1953 * sr_highest_slotid and the sr_target_slot id to maxslots */
1954 seq->maxslots = session->se_fchannel.maxreqs;
1956 status = check_slot_seqid(seq->seqid, slot->sl_seqid,
1957 slot->sl_flags & NFSD4_SLOT_INUSE);
1958 if (status == nfserr_replay_cache) {
1959 status = nfserr_seq_misordered;
1960 if (!(slot->sl_flags & NFSD4_SLOT_INITIALIZED))
1962 cstate->slot = slot;
1963 cstate->session = session;
1964 /* Return the cached reply status and set cstate->status
1965 * for nfsd4_proc_compound processing */
1966 status = nfsd4_replay_cache_entry(resp, seq);
1967 cstate->status = nfserr_replay_cache;
1973 nfsd4_sequence_check_conn(conn, session);
1976 /* Success! bump slot seqid */
1977 slot->sl_seqid = seq->seqid;
1978 slot->sl_flags |= NFSD4_SLOT_INUSE;
1980 slot->sl_flags |= NFSD4_SLOT_CACHETHIS;
1982 slot->sl_flags &= ~NFSD4_SLOT_CACHETHIS;
1984 cstate->slot = slot;
1985 cstate->session = session;
1988 /* Hold a session reference until done processing the compound. */
1989 if (cstate->session) {
1990 struct nfs4_client *clp = session->se_client;
1992 nfsd4_get_session(cstate->session);
1993 atomic_inc(&clp->cl_refcount);
1994 switch (clp->cl_cb_state) {
1996 seq->status_flags = SEQ4_STATUS_CB_PATH_DOWN;
1998 case NFSD4_CB_FAULT:
1999 seq->status_flags = SEQ4_STATUS_BACKCHANNEL_FAULT;
2002 seq->status_flags = 0;
2006 spin_unlock(&client_lock);
2007 dprintk("%s: return %d\n", __func__, ntohl(status));
2011 static inline bool has_resources(struct nfs4_client *clp)
2013 return !list_empty(&clp->cl_openowners)
2014 || !list_empty(&clp->cl_delegations)
2015 || !list_empty(&clp->cl_sessions);
2019 nfsd4_destroy_clientid(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate, struct nfsd4_destroy_clientid *dc)
2021 struct nfs4_client *conf, *unconf, *clp;
2025 unconf = find_unconfirmed_client(&dc->clientid);
2026 conf = find_confirmed_client(&dc->clientid);
2031 if (!is_client_expired(conf) && has_resources(conf)) {
2032 status = nfserr_clientid_busy;
2036 /* rfc5661 18.50.3 */
2037 if (cstate->session && conf == cstate->session->se_client) {
2038 status = nfserr_clientid_busy;
2044 status = nfserr_stale_clientid;
2050 nfs4_unlock_state();
2051 dprintk("%s return %d\n", __func__, ntohl(status));
2056 nfsd4_reclaim_complete(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate, struct nfsd4_reclaim_complete *rc)
2060 if (rc->rca_one_fs) {
2061 if (!cstate->current_fh.fh_dentry)
2062 return nfserr_nofilehandle;
2064 * We don't take advantage of the rca_one_fs case.
2065 * That's OK, it's optional, we can safely ignore it.
2071 status = nfserr_complete_already;
2072 if (test_and_set_bit(NFSD4_CLIENT_RECLAIM_COMPLETE,
2073 &cstate->session->se_client->cl_flags))
2076 status = nfserr_stale_clientid;
2077 if (is_client_expired(cstate->session->se_client))
2079 * The following error isn't really legal.
2080 * But we only get here if the client just explicitly
2081 * destroyed the client. Surely it no longer cares what
2082 * error it gets back on an operation for the dead
2088 nfsd4_client_record_create(cstate->session->se_client);
2090 nfs4_unlock_state();
2095 nfsd4_setclientid(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
2096 struct nfsd4_setclientid *setclid)
2098 struct xdr_netobj clname = setclid->se_name;
2099 nfs4_verifier clverifier = setclid->se_verf;
2100 unsigned int strhashval;
2101 struct nfs4_client *conf, *unconf, *new;
2103 char dname[HEXDIR_LEN];
2105 status = nfs4_make_rec_clidname(dname, &clname);
2110 * XXX The Duplicate Request Cache (DRC) has been checked (??)
2111 * We get here on a DRC miss.
2114 strhashval = clientstr_hashval(dname);
2117 conf = find_confirmed_client_by_str(dname, strhashval);
2119 /* RFC 3530 14.2.33 CASE 0: */
2120 status = nfserr_clid_inuse;
2121 if (clp_used_exchangeid(conf))
2123 if (!same_creds(&conf->cl_cred, &rqstp->rq_cred)) {
2124 char addr_str[INET6_ADDRSTRLEN];
2125 rpc_ntop((struct sockaddr *) &conf->cl_addr, addr_str,
2127 dprintk("NFSD: setclientid: string in use by client "
2128 "at %s\n", addr_str);
2133 * section 14.2.33 of RFC 3530 (under the heading "IMPLEMENTATION")
2134 * has a description of SETCLIENTID request processing consisting
2135 * of 5 bullet points, labeled as CASE0 - CASE4 below.
2137 unconf = find_unconfirmed_client_by_str(dname, strhashval);
2138 status = nfserr_jukebox;
2141 * RFC 3530 14.2.33 CASE 4:
2142 * placed first, because it is the normal case
2145 expire_client(unconf);
2146 new = create_client(clname, dname, rqstp, &clverifier);
2150 } else if (same_verf(&conf->cl_verifier, &clverifier)) {
2152 * RFC 3530 14.2.33 CASE 1:
2153 * probable callback update
2156 /* Note this is removing unconfirmed {*x***},
2157 * which is stronger than RFC recommended {vxc**}.
2158 * This has the advantage that there is at most
2159 * one {*x***} in either list at any time.
2161 expire_client(unconf);
2163 new = create_client(clname, dname, rqstp, &clverifier);
2166 copy_clid(new, conf);
2167 } else if (!unconf) {
2169 * RFC 3530 14.2.33 CASE 2:
2170 * probable client reboot; state will be removed if
2173 new = create_client(clname, dname, rqstp, &clverifier);
2179 * RFC 3530 14.2.33 CASE 3:
2180 * probable client reboot; state will be removed if
2183 expire_client(unconf);
2184 new = create_client(clname, dname, rqstp, &clverifier);
2190 * XXX: we should probably set this at creation time, and check
2191 * for consistent minorversion use throughout:
2193 new->cl_minorversion = 0;
2194 gen_callback(new, setclid, rqstp);
2195 add_to_unconfirmed(new, strhashval);
2196 setclid->se_clientid.cl_boot = new->cl_clientid.cl_boot;
2197 setclid->se_clientid.cl_id = new->cl_clientid.cl_id;
2198 memcpy(setclid->se_confirm.data, new->cl_confirm.data, sizeof(setclid->se_confirm.data));
2201 nfs4_unlock_state();
2207 * Section 14.2.34 of RFC 3530 (under the heading "IMPLEMENTATION") has
2208 * a description of SETCLIENTID_CONFIRM request processing consisting of 4
2209 * bullets, labeled as CASE1 - CASE4 below.
2212 nfsd4_setclientid_confirm(struct svc_rqst *rqstp,
2213 struct nfsd4_compound_state *cstate,
2214 struct nfsd4_setclientid_confirm *setclientid_confirm)
2216 struct sockaddr *sa = svc_addr(rqstp);
2217 struct nfs4_client *conf, *unconf;
2218 nfs4_verifier confirm = setclientid_confirm->sc_confirm;
2219 clientid_t * clid = &setclientid_confirm->sc_clientid;
2222 if (STALE_CLIENTID(clid))
2223 return nfserr_stale_clientid;
2225 * XXX The Duplicate Request Cache (DRC) has been checked (??)
2226 * We get here on a DRC miss.
2231 conf = find_confirmed_client(clid);
2232 unconf = find_unconfirmed_client(clid);
2234 status = nfserr_clid_inuse;
2235 if (conf && !rpc_cmp_addr((struct sockaddr *) &conf->cl_addr, sa))
2237 if (unconf && !rpc_cmp_addr((struct sockaddr *) &unconf->cl_addr, sa))
2241 * section 14.2.34 of RFC 3530 has a description of
2242 * SETCLIENTID_CONFIRM request processing consisting
2243 * of 4 bullet points, labeled as CASE1 - CASE4 below.
2245 if (conf && unconf && same_verf(&confirm, &unconf->cl_confirm)) {
2247 * RFC 3530 14.2.34 CASE 1:
2250 if (!same_creds(&conf->cl_cred, &unconf->cl_cred))
2251 status = nfserr_clid_inuse;
2253 nfsd4_change_callback(conf, &unconf->cl_cb_conn);
2254 nfsd4_probe_callback(conf);
2255 expire_client(unconf);
2259 } else if (conf && !unconf) {
2261 * RFC 3530 14.2.34 CASE 2:
2262 * probable retransmitted request; play it safe and
2265 if (!same_creds(&conf->cl_cred, &rqstp->rq_cred))
2266 status = nfserr_clid_inuse;
2269 } else if (!conf && unconf
2270 && same_verf(&unconf->cl_confirm, &confirm)) {
2272 * RFC 3530 14.2.34 CASE 3:
2273 * Normal case; new or rebooted client:
2275 if (!same_creds(&unconf->cl_cred, &rqstp->rq_cred)) {
2276 status = nfserr_clid_inuse;
2279 clientstr_hashval(unconf->cl_recdir);
2280 conf = find_confirmed_client_by_str(unconf->cl_recdir,
2283 nfsd4_client_record_remove(conf);
2284 expire_client(conf);
2286 move_to_confirmed(unconf);
2288 nfsd4_probe_callback(conf);
2291 } else if ((!conf || (conf && !same_verf(&conf->cl_confirm, &confirm)))
2292 && (!unconf || (unconf && !same_verf(&unconf->cl_confirm,
2295 * RFC 3530 14.2.34 CASE 4:
2296 * Client probably hasn't noticed that we rebooted yet.
2298 status = nfserr_stale_clientid;
2300 /* check that we have hit one of the cases...*/
2301 status = nfserr_clid_inuse;
2304 nfs4_unlock_state();
2308 static struct nfs4_file *nfsd4_alloc_file(void)
2310 return kmem_cache_alloc(file_slab, GFP_KERNEL);
2313 /* OPEN Share state helper functions */
2314 static void nfsd4_init_file(struct nfs4_file *fp, struct inode *ino)
2316 unsigned int hashval = file_hashval(ino);
2318 atomic_set(&fp->fi_ref, 1);
2319 INIT_LIST_HEAD(&fp->fi_hash);
2320 INIT_LIST_HEAD(&fp->fi_stateids);
2321 INIT_LIST_HEAD(&fp->fi_delegations);
2322 fp->fi_inode = igrab(ino);
2323 fp->fi_had_conflict = false;
2324 fp->fi_lease = NULL;
2325 memset(fp->fi_fds, 0, sizeof(fp->fi_fds));
2326 memset(fp->fi_access, 0, sizeof(fp->fi_access));
2327 spin_lock(&recall_lock);
2328 list_add(&fp->fi_hash, &file_hashtbl[hashval]);
2329 spin_unlock(&recall_lock);
2333 nfsd4_free_slab(struct kmem_cache **slab)
2337 kmem_cache_destroy(*slab);
2342 nfsd4_free_slabs(void)
2344 nfsd4_free_slab(&openowner_slab);
2345 nfsd4_free_slab(&lockowner_slab);
2346 nfsd4_free_slab(&file_slab);
2347 nfsd4_free_slab(&stateid_slab);
2348 nfsd4_free_slab(&deleg_slab);
2352 nfsd4_init_slabs(void)
2354 openowner_slab = kmem_cache_create("nfsd4_openowners",
2355 sizeof(struct nfs4_openowner), 0, 0, NULL);
2356 if (openowner_slab == NULL)
2358 lockowner_slab = kmem_cache_create("nfsd4_lockowners",
2359 sizeof(struct nfs4_openowner), 0, 0, NULL);
2360 if (lockowner_slab == NULL)
2362 file_slab = kmem_cache_create("nfsd4_files",
2363 sizeof(struct nfs4_file), 0, 0, NULL);
2364 if (file_slab == NULL)
2366 stateid_slab = kmem_cache_create("nfsd4_stateids",
2367 sizeof(struct nfs4_ol_stateid), 0, 0, NULL);
2368 if (stateid_slab == NULL)
2370 deleg_slab = kmem_cache_create("nfsd4_delegations",
2371 sizeof(struct nfs4_delegation), 0, 0, NULL);
2372 if (deleg_slab == NULL)
2377 dprintk("nfsd4: out of memory while initializing nfsv4\n");
2381 void nfs4_free_openowner(struct nfs4_openowner *oo)
2383 kfree(oo->oo_owner.so_owner.data);
2384 kmem_cache_free(openowner_slab, oo);
2387 void nfs4_free_lockowner(struct nfs4_lockowner *lo)
2389 kfree(lo->lo_owner.so_owner.data);
2390 kmem_cache_free(lockowner_slab, lo);
2393 static void init_nfs4_replay(struct nfs4_replay *rp)
2395 rp->rp_status = nfserr_serverfault;
2397 rp->rp_buf = rp->rp_ibuf;
2400 static inline void *alloc_stateowner(struct kmem_cache *slab, struct xdr_netobj *owner, struct nfs4_client *clp)
2402 struct nfs4_stateowner *sop;
2404 sop = kmem_cache_alloc(slab, GFP_KERNEL);
2408 sop->so_owner.data = kmemdup(owner->data, owner->len, GFP_KERNEL);
2409 if (!sop->so_owner.data) {
2410 kmem_cache_free(slab, sop);
2413 sop->so_owner.len = owner->len;
2415 INIT_LIST_HEAD(&sop->so_stateids);
2416 sop->so_client = clp;
2417 init_nfs4_replay(&sop->so_replay);
2421 static void hash_openowner(struct nfs4_openowner *oo, struct nfs4_client *clp, unsigned int strhashval)
2423 list_add(&oo->oo_owner.so_strhash, &ownerstr_hashtbl[strhashval]);
2424 list_add(&oo->oo_perclient, &clp->cl_openowners);
2427 static struct nfs4_openowner *
2428 alloc_init_open_stateowner(unsigned int strhashval, struct nfs4_client *clp, struct nfsd4_open *open) {
2429 struct nfs4_openowner *oo;
2431 oo = alloc_stateowner(openowner_slab, &open->op_owner, clp);
2434 oo->oo_owner.so_is_open_owner = 1;
2435 oo->oo_owner.so_seqid = open->op_seqid;
2436 oo->oo_flags = NFS4_OO_NEW;
2438 oo->oo_last_closed_stid = NULL;
2439 INIT_LIST_HEAD(&oo->oo_close_lru);
2440 hash_openowner(oo, clp, strhashval);
2444 static void init_open_stateid(struct nfs4_ol_stateid *stp, struct nfs4_file *fp, struct nfsd4_open *open) {
2445 struct nfs4_openowner *oo = open->op_openowner;
2446 struct nfs4_client *clp = oo->oo_owner.so_client;
2448 init_stid(&stp->st_stid, clp, NFS4_OPEN_STID);
2449 INIT_LIST_HEAD(&stp->st_lockowners);
2450 list_add(&stp->st_perstateowner, &oo->oo_owner.so_stateids);
2451 list_add(&stp->st_perfile, &fp->fi_stateids);
2452 stp->st_stateowner = &oo->oo_owner;
2455 stp->st_access_bmap = 0;
2456 stp->st_deny_bmap = 0;
2457 __set_bit(open->op_share_access, &stp->st_access_bmap);
2458 __set_bit(open->op_share_deny, &stp->st_deny_bmap);
2459 stp->st_openstp = NULL;
2463 move_to_close_lru(struct nfs4_openowner *oo)
2465 dprintk("NFSD: move_to_close_lru nfs4_openowner %p\n", oo);
2467 list_move_tail(&oo->oo_close_lru, &close_lru);
2468 oo->oo_time = get_seconds();
2472 same_owner_str(struct nfs4_stateowner *sop, struct xdr_netobj *owner,
2475 return (sop->so_owner.len == owner->len) &&
2476 0 == memcmp(sop->so_owner.data, owner->data, owner->len) &&
2477 (sop->so_client->cl_clientid.cl_id == clid->cl_id);
2480 static struct nfs4_openowner *
2481 find_openstateowner_str(unsigned int hashval, struct nfsd4_open *open)
2483 struct nfs4_stateowner *so;
2484 struct nfs4_openowner *oo;
2486 list_for_each_entry(so, &ownerstr_hashtbl[hashval], so_strhash) {
2487 if (!so->so_is_open_owner)
2489 if (same_owner_str(so, &open->op_owner, &open->op_clientid)) {
2491 renew_client(oo->oo_owner.so_client);
2498 /* search file_hashtbl[] for file */
2499 static struct nfs4_file *
2500 find_file(struct inode *ino)
2502 unsigned int hashval = file_hashval(ino);
2503 struct nfs4_file *fp;
2505 spin_lock(&recall_lock);
2506 list_for_each_entry(fp, &file_hashtbl[hashval], fi_hash) {
2507 if (fp->fi_inode == ino) {
2509 spin_unlock(&recall_lock);
2513 spin_unlock(&recall_lock);
2518 * Called to check deny when READ with all zero stateid or
2519 * WRITE with all zero or all one stateid
2522 nfs4_share_conflict(struct svc_fh *current_fh, unsigned int deny_type)
2524 struct inode *ino = current_fh->fh_dentry->d_inode;
2525 struct nfs4_file *fp;
2526 struct nfs4_ol_stateid *stp;
2529 dprintk("NFSD: nfs4_share_conflict\n");
2531 fp = find_file(ino);
2534 ret = nfserr_locked;
2535 /* Search for conflicting share reservations */
2536 list_for_each_entry(stp, &fp->fi_stateids, st_perfile) {
2537 if (test_bit(deny_type, &stp->st_deny_bmap) ||
2538 test_bit(NFS4_SHARE_DENY_BOTH, &stp->st_deny_bmap))
2547 static void nfsd_break_one_deleg(struct nfs4_delegation *dp)
2549 /* We're assuming the state code never drops its reference
2550 * without first removing the lease. Since we're in this lease
2551 * callback (and since the lease code is serialized by the kernel
2552 * lock) we know the server hasn't removed the lease yet, we know
2553 * it's safe to take a reference: */
2554 atomic_inc(&dp->dl_count);
2556 list_add_tail(&dp->dl_recall_lru, &del_recall_lru);
2558 /* only place dl_time is set. protected by lock_flocks*/
2559 dp->dl_time = get_seconds();
2561 nfsd4_cb_recall(dp);
2564 /* Called from break_lease() with lock_flocks() held. */
2565 static void nfsd_break_deleg_cb(struct file_lock *fl)
2567 struct nfs4_file *fp = (struct nfs4_file *)fl->fl_owner;
2568 struct nfs4_delegation *dp;
2571 /* We assume break_lease is only called once per lease: */
2572 BUG_ON(fp->fi_had_conflict);
2574 * We don't want the locks code to timeout the lease for us;
2575 * we'll remove it ourself if a delegation isn't returned
2578 fl->fl_break_time = 0;
2580 spin_lock(&recall_lock);
2581 fp->fi_had_conflict = true;
2582 list_for_each_entry(dp, &fp->fi_delegations, dl_perfile)
2583 nfsd_break_one_deleg(dp);
2584 spin_unlock(&recall_lock);
2588 int nfsd_change_deleg_cb(struct file_lock **onlist, int arg)
2591 return lease_modify(onlist, arg);
2596 static const struct lock_manager_operations nfsd_lease_mng_ops = {
2597 .lm_break = nfsd_break_deleg_cb,
2598 .lm_change = nfsd_change_deleg_cb,
2601 static __be32 nfsd4_check_seqid(struct nfsd4_compound_state *cstate, struct nfs4_stateowner *so, u32 seqid)
2603 if (nfsd4_has_session(cstate))
2605 if (seqid == so->so_seqid - 1)
2606 return nfserr_replay_me;
2607 if (seqid == so->so_seqid)
2609 return nfserr_bad_seqid;
2613 nfsd4_process_open1(struct nfsd4_compound_state *cstate,
2614 struct nfsd4_open *open)
2616 clientid_t *clientid = &open->op_clientid;
2617 struct nfs4_client *clp = NULL;
2618 unsigned int strhashval;
2619 struct nfs4_openowner *oo = NULL;
2622 if (STALE_CLIENTID(&open->op_clientid))
2623 return nfserr_stale_clientid;
2625 * In case we need it later, after we've already created the
2626 * file and don't want to risk a further failure:
2628 open->op_file = nfsd4_alloc_file();
2629 if (open->op_file == NULL)
2630 return nfserr_jukebox;
2632 strhashval = ownerstr_hashval(clientid->cl_id, &open->op_owner);
2633 oo = find_openstateowner_str(strhashval, open);
2634 open->op_openowner = oo;
2636 clp = find_confirmed_client(clientid);
2638 return nfserr_expired;
2641 if (!(oo->oo_flags & NFS4_OO_CONFIRMED)) {
2642 /* Replace unconfirmed owners without checking for replay. */
2643 clp = oo->oo_owner.so_client;
2644 release_openowner(oo);
2645 open->op_openowner = NULL;
2648 status = nfsd4_check_seqid(cstate, &oo->oo_owner, open->op_seqid);
2651 clp = oo->oo_owner.so_client;
2654 oo = alloc_init_open_stateowner(strhashval, clp, open);
2656 return nfserr_jukebox;
2657 open->op_openowner = oo;
2659 open->op_stp = nfs4_alloc_stateid(clp);
2661 return nfserr_jukebox;
2665 static inline __be32
2666 nfs4_check_delegmode(struct nfs4_delegation *dp, int flags)
2668 if ((flags & WR_STATE) && (dp->dl_type == NFS4_OPEN_DELEGATE_READ))
2669 return nfserr_openmode;
2674 static int share_access_to_flags(u32 share_access)
2676 return share_access == NFS4_SHARE_ACCESS_READ ? RD_STATE : WR_STATE;
2679 static struct nfs4_delegation *find_deleg_stateid(struct nfs4_client *cl, stateid_t *s)
2681 struct nfs4_stid *ret;
2683 ret = find_stateid_by_type(cl, s, NFS4_DELEG_STID);
2686 return delegstateid(ret);
2689 static bool nfsd4_is_deleg_cur(struct nfsd4_open *open)
2691 return open->op_claim_type == NFS4_OPEN_CLAIM_DELEGATE_CUR ||
2692 open->op_claim_type == NFS4_OPEN_CLAIM_DELEG_CUR_FH;
2696 nfs4_check_deleg(struct nfs4_client *cl, struct nfs4_file *fp, struct nfsd4_open *open,
2697 struct nfs4_delegation **dp)
2700 __be32 status = nfserr_bad_stateid;
2702 *dp = find_deleg_stateid(cl, &open->op_delegate_stateid);
2705 flags = share_access_to_flags(open->op_share_access);
2706 status = nfs4_check_delegmode(*dp, flags);
2710 if (!nfsd4_is_deleg_cur(open))
2714 open->op_openowner->oo_flags |= NFS4_OO_CONFIRMED;
2719 nfs4_check_open(struct nfs4_file *fp, struct nfsd4_open *open, struct nfs4_ol_stateid **stpp)
2721 struct nfs4_ol_stateid *local;
2722 struct nfs4_openowner *oo = open->op_openowner;
2724 list_for_each_entry(local, &fp->fi_stateids, st_perfile) {
2725 /* ignore lock owners */
2726 if (local->st_stateowner->so_is_open_owner == 0)
2728 /* remember if we have seen this open owner */
2729 if (local->st_stateowner == &oo->oo_owner)
2731 /* check for conflicting share reservations */
2732 if (!test_share(local, open))
2733 return nfserr_share_denied;
2738 static void nfs4_free_stateid(struct nfs4_ol_stateid *s)
2740 kmem_cache_free(stateid_slab, s);
2743 static inline int nfs4_access_to_access(u32 nfs4_access)
2747 if (nfs4_access & NFS4_SHARE_ACCESS_READ)
2748 flags |= NFSD_MAY_READ;
2749 if (nfs4_access & NFS4_SHARE_ACCESS_WRITE)
2750 flags |= NFSD_MAY_WRITE;
2754 static __be32 nfs4_get_vfs_file(struct svc_rqst *rqstp, struct nfs4_file *fp,
2755 struct svc_fh *cur_fh, struct nfsd4_open *open)
2758 int oflag = nfs4_access_to_omode(open->op_share_access);
2759 int access = nfs4_access_to_access(open->op_share_access);
2761 if (!fp->fi_fds[oflag]) {
2762 status = nfsd_open(rqstp, cur_fh, S_IFREG, access,
2763 &fp->fi_fds[oflag]);
2767 nfs4_file_get_access(fp, oflag);
2772 static inline __be32
2773 nfsd4_truncate(struct svc_rqst *rqstp, struct svc_fh *fh,
2774 struct nfsd4_open *open)
2776 struct iattr iattr = {
2777 .ia_valid = ATTR_SIZE,
2780 if (!open->op_truncate)
2782 if (!(open->op_share_access & NFS4_SHARE_ACCESS_WRITE))
2783 return nfserr_inval;
2784 return nfsd_setattr(rqstp, fh, &iattr, 0, (time_t)0);
2788 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)
2790 u32 op_share_access = open->op_share_access;
2794 new_access = !test_bit(op_share_access, &stp->st_access_bmap);
2796 status = nfs4_get_vfs_file(rqstp, fp, cur_fh, open);
2800 status = nfsd4_truncate(rqstp, cur_fh, open);
2803 int oflag = nfs4_access_to_omode(op_share_access);
2804 nfs4_file_put_access(fp, oflag);
2808 /* remember the open */
2809 __set_bit(op_share_access, &stp->st_access_bmap);
2810 __set_bit(open->op_share_deny, &stp->st_deny_bmap);
2817 nfs4_set_claim_prev(struct nfsd4_open *open, bool has_session)
2819 open->op_openowner->oo_flags |= NFS4_OO_CONFIRMED;
2822 /* Should we give out recallable state?: */
2823 static bool nfsd4_cb_channel_good(struct nfs4_client *clp)
2825 if (clp->cl_cb_state == NFSD4_CB_UP)
2828 * In the sessions case, since we don't have to establish a
2829 * separate connection for callbacks, we assume it's OK
2830 * until we hear otherwise:
2832 return clp->cl_minorversion && clp->cl_cb_state == NFSD4_CB_UNKNOWN;
2835 static struct file_lock *nfs4_alloc_init_lease(struct nfs4_delegation *dp, int flag)
2837 struct file_lock *fl;
2839 fl = locks_alloc_lock();
2842 locks_init_lock(fl);
2843 fl->fl_lmops = &nfsd_lease_mng_ops;
2844 fl->fl_flags = FL_LEASE;
2845 fl->fl_type = flag == NFS4_OPEN_DELEGATE_READ? F_RDLCK: F_WRLCK;
2846 fl->fl_end = OFFSET_MAX;
2847 fl->fl_owner = (fl_owner_t)(dp->dl_file);
2848 fl->fl_pid = current->tgid;
2852 static int nfs4_setlease(struct nfs4_delegation *dp, int flag)
2854 struct nfs4_file *fp = dp->dl_file;
2855 struct file_lock *fl;
2858 fl = nfs4_alloc_init_lease(dp, flag);
2861 fl->fl_file = find_readable_file(fp);
2862 list_add(&dp->dl_perclnt, &dp->dl_stid.sc_client->cl_delegations);
2863 status = vfs_setlease(fl->fl_file, fl->fl_type, &fl);
2865 list_del_init(&dp->dl_perclnt);
2866 locks_free_lock(fl);
2870 fp->fi_deleg_file = fl->fl_file;
2871 get_file(fp->fi_deleg_file);
2872 atomic_set(&fp->fi_delegees, 1);
2873 list_add(&dp->dl_perfile, &fp->fi_delegations);
2877 static int nfs4_set_delegation(struct nfs4_delegation *dp, int flag)
2879 struct nfs4_file *fp = dp->dl_file;
2882 return nfs4_setlease(dp, flag);
2883 spin_lock(&recall_lock);
2884 if (fp->fi_had_conflict) {
2885 spin_unlock(&recall_lock);
2888 atomic_inc(&fp->fi_delegees);
2889 list_add(&dp->dl_perfile, &fp->fi_delegations);
2890 spin_unlock(&recall_lock);
2891 list_add(&dp->dl_perclnt, &dp->dl_stid.sc_client->cl_delegations);
2895 static void nfsd4_open_deleg_none_ext(struct nfsd4_open *open, int status)
2897 open->op_delegate_type = NFS4_OPEN_DELEGATE_NONE_EXT;
2898 if (status == -EAGAIN)
2899 open->op_why_no_deleg = WND4_CONTENTION;
2901 open->op_why_no_deleg = WND4_RESOURCE;
2902 switch (open->op_deleg_want) {
2903 case NFS4_SHARE_WANT_READ_DELEG:
2904 case NFS4_SHARE_WANT_WRITE_DELEG:
2905 case NFS4_SHARE_WANT_ANY_DELEG:
2907 case NFS4_SHARE_WANT_CANCEL:
2908 open->op_why_no_deleg = WND4_CANCELLED;
2910 case NFS4_SHARE_WANT_NO_DELEG:
2911 BUG(); /* not supposed to get here */
2917 * Attempt to hand out a delegation.
2920 nfs4_open_delegation(struct svc_fh *fh, struct nfsd4_open *open, struct nfs4_ol_stateid *stp)
2922 struct nfs4_delegation *dp;
2923 struct nfs4_openowner *oo = container_of(stp->st_stateowner, struct nfs4_openowner, oo_owner);
2925 int status = 0, flag = 0;
2927 cb_up = nfsd4_cb_channel_good(oo->oo_owner.so_client);
2928 flag = NFS4_OPEN_DELEGATE_NONE;
2929 open->op_recall = 0;
2930 switch (open->op_claim_type) {
2931 case NFS4_OPEN_CLAIM_PREVIOUS:
2933 open->op_recall = 1;
2934 flag = open->op_delegate_type;
2935 if (flag == NFS4_OPEN_DELEGATE_NONE)
2938 case NFS4_OPEN_CLAIM_NULL:
2939 /* Let's not give out any delegations till everyone's
2940 * had the chance to reclaim theirs.... */
2941 if (locks_in_grace())
2943 if (!cb_up || !(oo->oo_flags & NFS4_OO_CONFIRMED))
2945 if (open->op_share_access & NFS4_SHARE_ACCESS_WRITE)
2946 flag = NFS4_OPEN_DELEGATE_WRITE;
2948 flag = NFS4_OPEN_DELEGATE_READ;
2954 dp = alloc_init_deleg(oo->oo_owner.so_client, stp, fh, flag);
2957 status = nfs4_set_delegation(dp, flag);
2961 memcpy(&open->op_delegate_stateid, &dp->dl_stid.sc_stateid, sizeof(dp->dl_stid.sc_stateid));
2963 dprintk("NFSD: delegation stateid=" STATEID_FMT "\n",
2964 STATEID_VAL(&dp->dl_stid.sc_stateid));
2966 open->op_delegate_type = flag;
2967 if (flag == NFS4_OPEN_DELEGATE_NONE) {
2968 if (open->op_claim_type == NFS4_OPEN_CLAIM_PREVIOUS &&
2969 open->op_delegate_type != NFS4_OPEN_DELEGATE_NONE)
2970 dprintk("NFSD: WARNING: refusing delegation reclaim\n");
2972 /* 4.1 client asking for a delegation? */
2973 if (open->op_deleg_want)
2974 nfsd4_open_deleg_none_ext(open, status);
2978 nfs4_put_delegation(dp);
2980 flag = NFS4_OPEN_DELEGATE_NONE;
2984 static void nfsd4_deleg_xgrade_none_ext(struct nfsd4_open *open,
2985 struct nfs4_delegation *dp)
2987 if (open->op_deleg_want == NFS4_SHARE_WANT_READ_DELEG &&
2988 dp->dl_type == NFS4_OPEN_DELEGATE_WRITE) {
2989 open->op_delegate_type = NFS4_OPEN_DELEGATE_NONE_EXT;
2990 open->op_why_no_deleg = WND4_NOT_SUPP_DOWNGRADE;
2991 } else if (open->op_deleg_want == NFS4_SHARE_WANT_WRITE_DELEG &&
2992 dp->dl_type == NFS4_OPEN_DELEGATE_WRITE) {
2993 open->op_delegate_type = NFS4_OPEN_DELEGATE_NONE_EXT;
2994 open->op_why_no_deleg = WND4_NOT_SUPP_UPGRADE;
2996 /* Otherwise the client must be confused wanting a delegation
2997 * it already has, therefore we don't return
2998 * NFS4_OPEN_DELEGATE_NONE_EXT and reason.
3003 * called with nfs4_lock_state() held.
3006 nfsd4_process_open2(struct svc_rqst *rqstp, struct svc_fh *current_fh, struct nfsd4_open *open)
3008 struct nfsd4_compoundres *resp = rqstp->rq_resp;
3009 struct nfs4_client *cl = open->op_openowner->oo_owner.so_client;
3010 struct nfs4_file *fp = NULL;
3011 struct inode *ino = current_fh->fh_dentry->d_inode;
3012 struct nfs4_ol_stateid *stp = NULL;
3013 struct nfs4_delegation *dp = NULL;
3017 * Lookup file; if found, lookup stateid and check open request,
3018 * and check for delegations in the process of being recalled.
3019 * If not found, create the nfs4_file struct
3021 fp = find_file(ino);
3023 if ((status = nfs4_check_open(fp, open, &stp)))
3025 status = nfs4_check_deleg(cl, fp, open, &dp);
3029 status = nfserr_bad_stateid;
3030 if (nfsd4_is_deleg_cur(open))
3032 status = nfserr_jukebox;
3034 open->op_file = NULL;
3035 nfsd4_init_file(fp, ino);
3039 * OPEN the file, or upgrade an existing OPEN.
3040 * If truncate fails, the OPEN fails.
3043 /* Stateid was found, this is an OPEN upgrade */
3044 status = nfs4_upgrade_open(rqstp, fp, current_fh, stp, open);
3048 status = nfs4_get_vfs_file(rqstp, fp, current_fh, open);
3052 open->op_stp = NULL;
3053 init_open_stateid(stp, fp, open);
3054 status = nfsd4_truncate(rqstp, current_fh, open);
3056 release_open_stateid(stp);
3060 update_stateid(&stp->st_stid.sc_stateid);
3061 memcpy(&open->op_stateid, &stp->st_stid.sc_stateid, sizeof(stateid_t));
3063 if (nfsd4_has_session(&resp->cstate)) {
3064 open->op_openowner->oo_flags |= NFS4_OO_CONFIRMED;
3066 if (open->op_deleg_want & NFS4_SHARE_WANT_NO_DELEG) {
3067 open->op_delegate_type = NFS4_OPEN_DELEGATE_NONE_EXT;
3068 open->op_why_no_deleg = WND4_NOT_WANTED;
3074 * Attempt to hand out a delegation. No error return, because the
3075 * OPEN succeeds even if we fail.
3077 nfs4_open_delegation(current_fh, open, stp);
3081 dprintk("%s: stateid=" STATEID_FMT "\n", __func__,
3082 STATEID_VAL(&stp->st_stid.sc_stateid));
3084 /* 4.1 client trying to upgrade/downgrade delegation? */
3085 if (open->op_delegate_type == NFS4_OPEN_DELEGATE_NONE && dp &&
3086 open->op_deleg_want)
3087 nfsd4_deleg_xgrade_none_ext(open, dp);
3091 if (status == 0 && open->op_claim_type == NFS4_OPEN_CLAIM_PREVIOUS)
3092 nfs4_set_claim_prev(open, nfsd4_has_session(&resp->cstate));
3094 * To finish the open response, we just need to set the rflags.
3096 open->op_rflags = NFS4_OPEN_RESULT_LOCKTYPE_POSIX;
3097 if (!(open->op_openowner->oo_flags & NFS4_OO_CONFIRMED) &&
3098 !nfsd4_has_session(&resp->cstate))
3099 open->op_rflags |= NFS4_OPEN_RESULT_CONFIRM;
3104 void nfsd4_cleanup_open_state(struct nfsd4_open *open, __be32 status)
3106 if (open->op_openowner) {
3107 struct nfs4_openowner *oo = open->op_openowner;
3109 if (!list_empty(&oo->oo_owner.so_stateids))
3110 list_del_init(&oo->oo_close_lru);
3111 if (oo->oo_flags & NFS4_OO_NEW) {
3113 release_openowner(oo);
3114 open->op_openowner = NULL;
3116 oo->oo_flags &= ~NFS4_OO_NEW;
3120 nfsd4_free_file(open->op_file);
3122 nfs4_free_stateid(open->op_stp);
3126 nfsd4_renew(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
3129 struct nfs4_client *clp;
3133 dprintk("process_renew(%08x/%08x): starting\n",
3134 clid->cl_boot, clid->cl_id);
3135 status = nfserr_stale_clientid;
3136 if (STALE_CLIENTID(clid))
3138 clp = find_confirmed_client(clid);
3139 status = nfserr_expired;
3141 /* We assume the client took too long to RENEW. */
3142 dprintk("nfsd4_renew: clientid not found!\n");
3145 status = nfserr_cb_path_down;
3146 if (!list_empty(&clp->cl_delegations)
3147 && clp->cl_cb_state != NFSD4_CB_UP)
3151 nfs4_unlock_state();
3155 static struct lock_manager nfsd4_manager = {
3159 nfsd4_end_grace(void)
3161 dprintk("NFSD: end of grace period\n");
3162 nfsd4_record_grace_done(&init_net, boot_time);
3163 locks_end_grace(&nfsd4_manager);
3165 * Now that every NFSv4 client has had the chance to recover and
3166 * to see the (possibly new, possibly shorter) lease time, we
3167 * can safely set the next grace time to the current lease time:
3169 nfsd4_grace = nfsd4_lease;
3173 nfs4_laundromat(void)
3175 struct nfs4_client *clp;
3176 struct nfs4_openowner *oo;
3177 struct nfs4_delegation *dp;
3178 struct list_head *pos, *next, reaplist;
3179 time_t cutoff = get_seconds() - nfsd4_lease;
3180 time_t t, clientid_val = nfsd4_lease;
3181 time_t u, test_val = nfsd4_lease;
3185 dprintk("NFSD: laundromat service - starting\n");
3186 if (locks_in_grace())
3188 INIT_LIST_HEAD(&reaplist);
3189 spin_lock(&client_lock);
3190 list_for_each_safe(pos, next, &client_lru) {
3191 clp = list_entry(pos, struct nfs4_client, cl_lru);
3192 if (time_after((unsigned long)clp->cl_time, (unsigned long)cutoff)) {
3193 t = clp->cl_time - cutoff;
3194 if (clientid_val > t)
3198 if (atomic_read(&clp->cl_refcount)) {
3199 dprintk("NFSD: client in use (clientid %08x)\n",
3200 clp->cl_clientid.cl_id);
3203 unhash_client_locked(clp);
3204 list_add(&clp->cl_lru, &reaplist);
3206 spin_unlock(&client_lock);
3207 list_for_each_safe(pos, next, &reaplist) {
3208 clp = list_entry(pos, struct nfs4_client, cl_lru);
3209 dprintk("NFSD: purging unused client (clientid %08x)\n",
3210 clp->cl_clientid.cl_id);
3211 nfsd4_client_record_remove(clp);
3214 spin_lock(&recall_lock);
3215 list_for_each_safe(pos, next, &del_recall_lru) {
3216 dp = list_entry (pos, struct nfs4_delegation, dl_recall_lru);
3217 if (time_after((unsigned long)dp->dl_time, (unsigned long)cutoff)) {
3218 u = dp->dl_time - cutoff;
3223 list_move(&dp->dl_recall_lru, &reaplist);
3225 spin_unlock(&recall_lock);
3226 list_for_each_safe(pos, next, &reaplist) {
3227 dp = list_entry (pos, struct nfs4_delegation, dl_recall_lru);
3228 unhash_delegation(dp);
3230 test_val = nfsd4_lease;
3231 list_for_each_safe(pos, next, &close_lru) {
3232 oo = container_of(pos, struct nfs4_openowner, oo_close_lru);
3233 if (time_after((unsigned long)oo->oo_time, (unsigned long)cutoff)) {
3234 u = oo->oo_time - cutoff;
3239 release_openowner(oo);
3241 if (clientid_val < NFSD_LAUNDROMAT_MINTIMEOUT)
3242 clientid_val = NFSD_LAUNDROMAT_MINTIMEOUT;
3243 nfs4_unlock_state();
3244 return clientid_val;
3247 static struct workqueue_struct *laundry_wq;
3248 static void laundromat_main(struct work_struct *);
3249 static DECLARE_DELAYED_WORK(laundromat_work, laundromat_main);
3252 laundromat_main(struct work_struct *not_used)
3256 t = nfs4_laundromat();
3257 dprintk("NFSD: laundromat_main - sleeping for %ld seconds\n", t);
3258 queue_delayed_work(laundry_wq, &laundromat_work, t*HZ);
3261 static inline __be32 nfs4_check_fh(struct svc_fh *fhp, struct nfs4_ol_stateid *stp)
3263 if (fhp->fh_dentry->d_inode != stp->st_file->fi_inode)
3264 return nfserr_bad_stateid;
3269 STALE_STATEID(stateid_t *stateid)
3271 if (stateid->si_opaque.so_clid.cl_boot == boot_time)
3273 dprintk("NFSD: stale stateid " STATEID_FMT "!\n",
3274 STATEID_VAL(stateid));
3279 access_permit_read(unsigned long access_bmap)
3281 return test_bit(NFS4_SHARE_ACCESS_READ, &access_bmap) ||
3282 test_bit(NFS4_SHARE_ACCESS_BOTH, &access_bmap) ||
3283 test_bit(NFS4_SHARE_ACCESS_WRITE, &access_bmap);
3287 access_permit_write(unsigned long access_bmap)
3289 return test_bit(NFS4_SHARE_ACCESS_WRITE, &access_bmap) ||
3290 test_bit(NFS4_SHARE_ACCESS_BOTH, &access_bmap);
3294 __be32 nfs4_check_openmode(struct nfs4_ol_stateid *stp, int flags)
3296 __be32 status = nfserr_openmode;
3298 /* For lock stateid's, we test the parent open, not the lock: */
3299 if (stp->st_openstp)
3300 stp = stp->st_openstp;
3301 if ((flags & WR_STATE) && (!access_permit_write(stp->st_access_bmap)))
3303 if ((flags & RD_STATE) && (!access_permit_read(stp->st_access_bmap)))
3310 static inline __be32
3311 check_special_stateids(svc_fh *current_fh, stateid_t *stateid, int flags)
3313 if (ONE_STATEID(stateid) && (flags & RD_STATE))
3315 else if (locks_in_grace()) {
3316 /* Answer in remaining cases depends on existence of
3317 * conflicting state; so we must wait out the grace period. */
3318 return nfserr_grace;
3319 } else if (flags & WR_STATE)
3320 return nfs4_share_conflict(current_fh,
3321 NFS4_SHARE_DENY_WRITE);
3322 else /* (flags & RD_STATE) && ZERO_STATEID(stateid) */
3323 return nfs4_share_conflict(current_fh,
3324 NFS4_SHARE_DENY_READ);
3328 * Allow READ/WRITE during grace period on recovered state only for files
3329 * that are not able to provide mandatory locking.
3332 grace_disallows_io(struct inode *inode)
3334 return locks_in_grace() && mandatory_lock(inode);
3337 /* Returns true iff a is later than b: */
3338 static bool stateid_generation_after(stateid_t *a, stateid_t *b)
3340 return (s32)a->si_generation - (s32)b->si_generation > 0;
3343 static int check_stateid_generation(stateid_t *in, stateid_t *ref, bool has_session)
3346 * When sessions are used the stateid generation number is ignored
3349 if (has_session && in->si_generation == 0)
3352 if (in->si_generation == ref->si_generation)
3355 /* If the client sends us a stateid from the future, it's buggy: */
3356 if (stateid_generation_after(in, ref))
3357 return nfserr_bad_stateid;
3359 * However, we could see a stateid from the past, even from a
3360 * non-buggy client. For example, if the client sends a lock
3361 * while some IO is outstanding, the lock may bump si_generation
3362 * while the IO is still in flight. The client could avoid that
3363 * situation by waiting for responses on all the IO requests,
3364 * but better performance may result in retrying IO that
3365 * receives an old_stateid error if requests are rarely
3366 * reordered in flight:
3368 return nfserr_old_stateid;
3371 __be32 nfs4_validate_stateid(struct nfs4_client *cl, stateid_t *stateid)
3373 struct nfs4_stid *s;
3374 struct nfs4_ol_stateid *ols;
3377 if (STALE_STATEID(stateid))
3378 return nfserr_stale_stateid;
3380 s = find_stateid(cl, stateid);
3382 return nfserr_stale_stateid;
3383 status = check_stateid_generation(stateid, &s->sc_stateid, 1);
3386 if (!(s->sc_type & (NFS4_OPEN_STID | NFS4_LOCK_STID)))
3388 ols = openlockstateid(s);
3389 if (ols->st_stateowner->so_is_open_owner
3390 && !(openowner(ols->st_stateowner)->oo_flags & NFS4_OO_CONFIRMED))
3391 return nfserr_bad_stateid;
3395 static __be32 nfsd4_lookup_stateid(stateid_t *stateid, unsigned char typemask, struct nfs4_stid **s)
3397 struct nfs4_client *cl;
3399 if (ZERO_STATEID(stateid) || ONE_STATEID(stateid))
3400 return nfserr_bad_stateid;
3401 if (STALE_STATEID(stateid))
3402 return nfserr_stale_stateid;
3403 cl = find_confirmed_client(&stateid->si_opaque.so_clid);
3405 return nfserr_expired;
3406 *s = find_stateid_by_type(cl, stateid, typemask);
3408 return nfserr_bad_stateid;
3414 * Checks for stateid operations
3417 nfs4_preprocess_stateid_op(struct nfsd4_compound_state *cstate,
3418 stateid_t *stateid, int flags, struct file **filpp)
3420 struct nfs4_stid *s;
3421 struct nfs4_ol_stateid *stp = NULL;
3422 struct nfs4_delegation *dp = NULL;
3423 struct svc_fh *current_fh = &cstate->current_fh;
3424 struct inode *ino = current_fh->fh_dentry->d_inode;
3430 if (grace_disallows_io(ino))
3431 return nfserr_grace;
3433 if (ZERO_STATEID(stateid) || ONE_STATEID(stateid))
3434 return check_special_stateids(current_fh, stateid, flags);
3436 status = nfsd4_lookup_stateid(stateid, NFS4_DELEG_STID|NFS4_OPEN_STID|NFS4_LOCK_STID, &s);
3439 status = check_stateid_generation(stateid, &s->sc_stateid, nfsd4_has_session(cstate));
3442 switch (s->sc_type) {
3443 case NFS4_DELEG_STID:
3444 dp = delegstateid(s);
3445 status = nfs4_check_delegmode(dp, flags);
3449 *filpp = dp->dl_file->fi_deleg_file;
3453 case NFS4_OPEN_STID:
3454 case NFS4_LOCK_STID:
3455 stp = openlockstateid(s);
3456 status = nfs4_check_fh(current_fh, stp);
3459 if (stp->st_stateowner->so_is_open_owner
3460 && !(openowner(stp->st_stateowner)->oo_flags & NFS4_OO_CONFIRMED))
3462 status = nfs4_check_openmode(stp, flags);
3466 if (flags & RD_STATE)
3467 *filpp = find_readable_file(stp->st_file);
3469 *filpp = find_writeable_file(stp->st_file);
3473 return nfserr_bad_stateid;
3481 nfsd4_free_lock_stateid(struct nfs4_ol_stateid *stp)
3483 if (check_for_locks(stp->st_file, lockowner(stp->st_stateowner)))
3484 return nfserr_locks_held;
3485 release_lock_stateid(stp);
3490 * Test if the stateid is valid
3493 nfsd4_test_stateid(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
3494 struct nfsd4_test_stateid *test_stateid)
3496 struct nfsd4_test_stateid_id *stateid;
3497 struct nfs4_client *cl = cstate->session->se_client;
3500 list_for_each_entry(stateid, &test_stateid->ts_stateid_list, ts_id_list)
3501 stateid->ts_id_status = nfs4_validate_stateid(cl, &stateid->ts_id_stateid);
3502 nfs4_unlock_state();
3508 nfsd4_free_stateid(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
3509 struct nfsd4_free_stateid *free_stateid)
3511 stateid_t *stateid = &free_stateid->fr_stateid;
3512 struct nfs4_stid *s;
3513 struct nfs4_client *cl = cstate->session->se_client;
3514 __be32 ret = nfserr_bad_stateid;
3517 s = find_stateid(cl, stateid);
3520 switch (s->sc_type) {
3521 case NFS4_DELEG_STID:
3522 ret = nfserr_locks_held;
3524 case NFS4_OPEN_STID:
3525 case NFS4_LOCK_STID:
3526 ret = check_stateid_generation(stateid, &s->sc_stateid, 1);
3529 if (s->sc_type == NFS4_LOCK_STID)
3530 ret = nfsd4_free_lock_stateid(openlockstateid(s));
3532 ret = nfserr_locks_held;
3535 ret = nfserr_bad_stateid;
3538 nfs4_unlock_state();
3545 return (type == NFS4_READW_LT || type == NFS4_READ_LT) ?
3546 RD_STATE : WR_STATE;
3549 static __be32 nfs4_seqid_op_checks(struct nfsd4_compound_state *cstate, stateid_t *stateid, u32 seqid, struct nfs4_ol_stateid *stp)
3551 struct svc_fh *current_fh = &cstate->current_fh;
3552 struct nfs4_stateowner *sop = stp->st_stateowner;
3555 status = nfsd4_check_seqid(cstate, sop, seqid);
3558 if (stp->st_stid.sc_type == NFS4_CLOSED_STID)
3560 * "Closed" stateid's exist *only* to return
3561 * nfserr_replay_me from the previous step.
3563 return nfserr_bad_stateid;
3564 status = check_stateid_generation(stateid, &stp->st_stid.sc_stateid, nfsd4_has_session(cstate));
3567 return nfs4_check_fh(current_fh, stp);
3571 * Checks for sequence id mutating operations.
3574 nfs4_preprocess_seqid_op(struct nfsd4_compound_state *cstate, u32 seqid,
3575 stateid_t *stateid, char typemask,
3576 struct nfs4_ol_stateid **stpp)
3579 struct nfs4_stid *s;
3581 dprintk("NFSD: %s: seqid=%d stateid = " STATEID_FMT "\n", __func__,
3582 seqid, STATEID_VAL(stateid));
3585 status = nfsd4_lookup_stateid(stateid, typemask, &s);
3588 *stpp = openlockstateid(s);
3589 cstate->replay_owner = (*stpp)->st_stateowner;
3591 return nfs4_seqid_op_checks(cstate, stateid, seqid, *stpp);
3594 static __be32 nfs4_preprocess_confirmed_seqid_op(struct nfsd4_compound_state *cstate, u32 seqid, stateid_t *stateid, struct nfs4_ol_stateid **stpp)
3597 struct nfs4_openowner *oo;
3599 status = nfs4_preprocess_seqid_op(cstate, seqid, stateid,
3600 NFS4_OPEN_STID, stpp);
3603 oo = openowner((*stpp)->st_stateowner);
3604 if (!(oo->oo_flags & NFS4_OO_CONFIRMED))
3605 return nfserr_bad_stateid;
3610 nfsd4_open_confirm(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
3611 struct nfsd4_open_confirm *oc)
3614 struct nfs4_openowner *oo;
3615 struct nfs4_ol_stateid *stp;
3617 dprintk("NFSD: nfsd4_open_confirm on file %.*s\n",
3618 (int)cstate->current_fh.fh_dentry->d_name.len,
3619 cstate->current_fh.fh_dentry->d_name.name);
3621 status = fh_verify(rqstp, &cstate->current_fh, S_IFREG, 0);
3627 status = nfs4_preprocess_seqid_op(cstate,
3628 oc->oc_seqid, &oc->oc_req_stateid,
3629 NFS4_OPEN_STID, &stp);
3632 oo = openowner(stp->st_stateowner);
3633 status = nfserr_bad_stateid;
3634 if (oo->oo_flags & NFS4_OO_CONFIRMED)
3636 oo->oo_flags |= NFS4_OO_CONFIRMED;
3637 update_stateid(&stp->st_stid.sc_stateid);
3638 memcpy(&oc->oc_resp_stateid, &stp->st_stid.sc_stateid, sizeof(stateid_t));
3639 dprintk("NFSD: %s: success, seqid=%d stateid=" STATEID_FMT "\n",
3640 __func__, oc->oc_seqid, STATEID_VAL(&stp->st_stid.sc_stateid));
3642 nfsd4_client_record_create(oo->oo_owner.so_client);
3645 if (!cstate->replay_owner)
3646 nfs4_unlock_state();
3650 static inline void nfs4_stateid_downgrade_bit(struct nfs4_ol_stateid *stp, u32 access)
3652 if (!test_bit(access, &stp->st_access_bmap))
3654 nfs4_file_put_access(stp->st_file, nfs4_access_to_omode(access));
3655 __clear_bit(access, &stp->st_access_bmap);
3658 static inline void nfs4_stateid_downgrade(struct nfs4_ol_stateid *stp, u32 to_access)
3660 switch (to_access) {
3661 case NFS4_SHARE_ACCESS_READ:
3662 nfs4_stateid_downgrade_bit(stp, NFS4_SHARE_ACCESS_WRITE);
3663 nfs4_stateid_downgrade_bit(stp, NFS4_SHARE_ACCESS_BOTH);
3665 case NFS4_SHARE_ACCESS_WRITE:
3666 nfs4_stateid_downgrade_bit(stp, NFS4_SHARE_ACCESS_READ);
3667 nfs4_stateid_downgrade_bit(stp, NFS4_SHARE_ACCESS_BOTH);
3669 case NFS4_SHARE_ACCESS_BOTH:
3677 reset_union_bmap_deny(unsigned long deny, unsigned long *bmap)
3680 for (i = 0; i < 4; i++) {
3681 if ((i & deny) != i)
3682 __clear_bit(i, bmap);
3687 nfsd4_open_downgrade(struct svc_rqst *rqstp,
3688 struct nfsd4_compound_state *cstate,
3689 struct nfsd4_open_downgrade *od)
3692 struct nfs4_ol_stateid *stp;
3694 dprintk("NFSD: nfsd4_open_downgrade on file %.*s\n",
3695 (int)cstate->current_fh.fh_dentry->d_name.len,
3696 cstate->current_fh.fh_dentry->d_name.name);
3698 /* We don't yet support WANT bits: */
3699 if (od->od_deleg_want)
3700 dprintk("NFSD: %s: od_deleg_want=0x%x ignored\n", __func__,
3704 status = nfs4_preprocess_confirmed_seqid_op(cstate, od->od_seqid,
3705 &od->od_stateid, &stp);
3708 status = nfserr_inval;
3709 if (!test_bit(od->od_share_access, &stp->st_access_bmap)) {
3710 dprintk("NFSD:access not a subset current bitmap: 0x%lx, input access=%08x\n",
3711 stp->st_access_bmap, od->od_share_access);
3714 if (!test_bit(od->od_share_deny, &stp->st_deny_bmap)) {
3715 dprintk("NFSD:deny not a subset current bitmap: 0x%lx, input deny=%08x\n",
3716 stp->st_deny_bmap, od->od_share_deny);
3719 nfs4_stateid_downgrade(stp, od->od_share_access);
3721 reset_union_bmap_deny(od->od_share_deny, &stp->st_deny_bmap);
3723 update_stateid(&stp->st_stid.sc_stateid);
3724 memcpy(&od->od_stateid, &stp->st_stid.sc_stateid, sizeof(stateid_t));
3727 if (!cstate->replay_owner)
3728 nfs4_unlock_state();
3732 void nfsd4_purge_closed_stateid(struct nfs4_stateowner *so)
3734 struct nfs4_openowner *oo;
3735 struct nfs4_ol_stateid *s;
3737 if (!so->so_is_open_owner)
3740 s = oo->oo_last_closed_stid;
3743 if (!(oo->oo_flags & NFS4_OO_PURGE_CLOSE)) {
3744 /* Release the last_closed_stid on the next seqid bump: */
3745 oo->oo_flags |= NFS4_OO_PURGE_CLOSE;
3748 oo->oo_flags &= ~NFS4_OO_PURGE_CLOSE;
3749 release_last_closed_stateid(oo);
3752 static void nfsd4_close_open_stateid(struct nfs4_ol_stateid *s)
3754 unhash_open_stateid(s);
3755 s->st_stid.sc_type = NFS4_CLOSED_STID;
3759 * nfs4_unlock_state() called after encode
3762 nfsd4_close(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
3763 struct nfsd4_close *close)
3766 struct nfs4_openowner *oo;
3767 struct nfs4_ol_stateid *stp;
3769 dprintk("NFSD: nfsd4_close on file %.*s\n",
3770 (int)cstate->current_fh.fh_dentry->d_name.len,
3771 cstate->current_fh.fh_dentry->d_name.name);
3774 status = nfs4_preprocess_seqid_op(cstate, close->cl_seqid,
3776 NFS4_OPEN_STID|NFS4_CLOSED_STID,
3780 oo = openowner(stp->st_stateowner);
3782 update_stateid(&stp->st_stid.sc_stateid);
3783 memcpy(&close->cl_stateid, &stp->st_stid.sc_stateid, sizeof(stateid_t));
3785 nfsd4_close_open_stateid(stp);
3786 oo->oo_last_closed_stid = stp;
3788 /* place unused nfs4_stateowners on so_close_lru list to be
3789 * released by the laundromat service after the lease period
3790 * to enable us to handle CLOSE replay
3792 if (list_empty(&oo->oo_owner.so_stateids))
3793 move_to_close_lru(oo);
3795 if (!cstate->replay_owner)
3796 nfs4_unlock_state();
3801 nfsd4_delegreturn(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
3802 struct nfsd4_delegreturn *dr)
3804 struct nfs4_delegation *dp;
3805 stateid_t *stateid = &dr->dr_stateid;
3806 struct nfs4_stid *s;
3807 struct inode *inode;
3810 if ((status = fh_verify(rqstp, &cstate->current_fh, S_IFREG, 0)))
3812 inode = cstate->current_fh.fh_dentry->d_inode;
3815 status = nfsd4_lookup_stateid(stateid, NFS4_DELEG_STID, &s);
3818 dp = delegstateid(s);
3819 status = check_stateid_generation(stateid, &dp->dl_stid.sc_stateid, nfsd4_has_session(cstate));
3823 unhash_delegation(dp);
3825 nfs4_unlock_state();
3831 #define LOFF_OVERFLOW(start, len) ((u64)(len) > ~(u64)(start))
3833 #define LOCKOWNER_INO_HASH_BITS 8
3834 #define LOCKOWNER_INO_HASH_SIZE (1 << LOCKOWNER_INO_HASH_BITS)
3835 #define LOCKOWNER_INO_HASH_MASK (LOCKOWNER_INO_HASH_SIZE - 1)
3838 end_offset(u64 start, u64 len)
3843 return end >= start ? end: NFS4_MAX_UINT64;
3846 /* last octet in a range */
3848 last_byte_offset(u64 start, u64 len)
3854 return end > start ? end - 1: NFS4_MAX_UINT64;
3857 static unsigned int lockowner_ino_hashval(struct inode *inode, u32 cl_id, struct xdr_netobj *ownername)
3859 return (file_hashval(inode) + cl_id
3860 + opaque_hashval(ownername->data, ownername->len))
3861 & LOCKOWNER_INO_HASH_MASK;
3864 static struct list_head lockowner_ino_hashtbl[LOCKOWNER_INO_HASH_SIZE];
3867 * TODO: Linux file offsets are _signed_ 64-bit quantities, which means that
3868 * we can't properly handle lock requests that go beyond the (2^63 - 1)-th
3869 * byte, because of sign extension problems. Since NFSv4 calls for 64-bit
3870 * locking, this prevents us from being completely protocol-compliant. The
3871 * real solution to this problem is to start using unsigned file offsets in
3872 * the VFS, but this is a very deep change!