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/sunrpc/svcauth_gss.h>
41 #include <linux/sunrpc/clnt.h>
45 #define NFSDDBG_FACILITY NFSDDBG_PROC
48 time_t nfsd4_lease = 90; /* default lease time */
49 time_t nfsd4_grace = 90;
50 static time_t boot_time;
51 static u32 current_ownerid = 1;
52 static u32 current_fileid = 1;
53 static u32 current_delegid = 1;
54 static stateid_t zerostateid; /* bits all 0 */
55 static stateid_t onestateid; /* bits all 1 */
56 static u64 current_sessionid = 1;
58 #define ZERO_STATEID(stateid) (!memcmp((stateid), &zerostateid, sizeof(stateid_t)))
59 #define ONE_STATEID(stateid) (!memcmp((stateid), &onestateid, sizeof(stateid_t)))
61 /* forward declarations */
62 static struct nfs4_stateid * find_stateid(stateid_t *stid, int flags);
63 static struct nfs4_delegation * find_delegation_stateid(struct inode *ino, stateid_t *stid);
64 static char user_recovery_dirname[PATH_MAX] = "/var/lib/nfs/v4recovery";
65 static void nfs4_set_recdir(char *recdir);
69 /* Currently used for almost all code touching nfsv4 state: */
70 static DEFINE_MUTEX(client_mutex);
73 * Currently used for the del_recall_lru and file hash table. In an
74 * effort to decrease the scope of the client_mutex, this spinlock may
75 * eventually cover more:
77 static DEFINE_SPINLOCK(recall_lock);
79 static struct kmem_cache *stateowner_slab = NULL;
80 static struct kmem_cache *file_slab = NULL;
81 static struct kmem_cache *stateid_slab = NULL;
82 static struct kmem_cache *deleg_slab = NULL;
87 mutex_lock(&client_mutex);
91 nfs4_unlock_state(void)
93 mutex_unlock(&client_mutex);
97 opaque_hashval(const void *ptr, int nbytes)
99 unsigned char *cptr = (unsigned char *) ptr;
109 static struct list_head del_recall_lru;
112 put_nfs4_file(struct nfs4_file *fi)
114 if (atomic_dec_and_lock(&fi->fi_ref, &recall_lock)) {
115 list_del(&fi->fi_hash);
116 spin_unlock(&recall_lock);
118 kmem_cache_free(file_slab, fi);
123 get_nfs4_file(struct nfs4_file *fi)
125 atomic_inc(&fi->fi_ref);
128 static int num_delegations;
129 unsigned int max_delegations;
132 * Open owner state (share locks)
135 /* hash tables for nfs4_stateowner */
136 #define OWNER_HASH_BITS 8
137 #define OWNER_HASH_SIZE (1 << OWNER_HASH_BITS)
138 #define OWNER_HASH_MASK (OWNER_HASH_SIZE - 1)
140 #define ownerid_hashval(id) \
141 ((id) & OWNER_HASH_MASK)
142 #define ownerstr_hashval(clientid, ownername) \
143 (((clientid) + opaque_hashval((ownername.data), (ownername.len))) & OWNER_HASH_MASK)
145 static struct list_head ownerid_hashtbl[OWNER_HASH_SIZE];
146 static struct list_head ownerstr_hashtbl[OWNER_HASH_SIZE];
148 /* hash table for nfs4_file */
149 #define FILE_HASH_BITS 8
150 #define FILE_HASH_SIZE (1 << FILE_HASH_BITS)
152 /* hash table for (open)nfs4_stateid */
153 #define STATEID_HASH_BITS 10
154 #define STATEID_HASH_SIZE (1 << STATEID_HASH_BITS)
155 #define STATEID_HASH_MASK (STATEID_HASH_SIZE - 1)
157 #define file_hashval(x) \
158 hash_ptr(x, FILE_HASH_BITS)
159 #define stateid_hashval(owner_id, file_id) \
160 (((owner_id) + (file_id)) & STATEID_HASH_MASK)
162 static struct list_head file_hashtbl[FILE_HASH_SIZE];
163 static struct list_head stateid_hashtbl[STATEID_HASH_SIZE];
165 static void __nfs4_file_get_access(struct nfs4_file *fp, int oflag)
167 BUG_ON(!(fp->fi_fds[oflag] || fp->fi_fds[O_RDWR]));
168 atomic_inc(&fp->fi_access[oflag]);
171 static void nfs4_file_get_access(struct nfs4_file *fp, int oflag)
173 if (oflag == O_RDWR) {
174 __nfs4_file_get_access(fp, O_RDONLY);
175 __nfs4_file_get_access(fp, O_WRONLY);
177 __nfs4_file_get_access(fp, oflag);
180 static void nfs4_file_put_fd(struct nfs4_file *fp, int oflag)
182 if (fp->fi_fds[oflag]) {
183 fput(fp->fi_fds[oflag]);
184 fp->fi_fds[oflag] = NULL;
188 static void __nfs4_file_put_access(struct nfs4_file *fp, int oflag)
190 if (atomic_dec_and_test(&fp->fi_access[oflag])) {
191 nfs4_file_put_fd(fp, oflag);
193 * It's also safe to get rid of the RDWR open *if*
194 * we no longer have need of the other kind of access
195 * or if we already have the other kind of open:
197 if (fp->fi_fds[1-oflag]
198 || atomic_read(&fp->fi_access[1 - oflag]) == 0)
199 nfs4_file_put_fd(fp, O_RDWR);
203 static void nfs4_file_put_access(struct nfs4_file *fp, int oflag)
205 if (oflag == O_RDWR) {
206 __nfs4_file_put_access(fp, O_RDONLY);
207 __nfs4_file_put_access(fp, O_WRONLY);
209 __nfs4_file_put_access(fp, oflag);
212 static struct nfs4_delegation *
213 alloc_init_deleg(struct nfs4_client *clp, struct nfs4_stateid *stp, struct svc_fh *current_fh, u32 type)
215 struct nfs4_delegation *dp;
216 struct nfs4_file *fp = stp->st_file;
218 dprintk("NFSD alloc_init_deleg\n");
220 * Major work on the lease subsystem (for example, to support
221 * calbacks on stat) will be required before we can support
222 * write delegations properly.
224 if (type != NFS4_OPEN_DELEGATE_READ)
226 if (fp->fi_had_conflict)
228 if (num_delegations > max_delegations)
230 dp = kmem_cache_alloc(deleg_slab, GFP_KERNEL);
234 INIT_LIST_HEAD(&dp->dl_perfile);
235 INIT_LIST_HEAD(&dp->dl_perclnt);
236 INIT_LIST_HEAD(&dp->dl_recall_lru);
241 dp->dl_stateid.si_boot = boot_time;
242 dp->dl_stateid.si_stateownerid = current_delegid++;
243 dp->dl_stateid.si_fileid = 0;
244 dp->dl_stateid.si_generation = 0;
245 fh_copy_shallow(&dp->dl_fh, ¤t_fh->fh_handle);
247 atomic_set(&dp->dl_count, 1);
248 INIT_WORK(&dp->dl_recall.cb_work, nfsd4_do_callback_rpc);
253 nfs4_put_delegation(struct nfs4_delegation *dp)
255 if (atomic_dec_and_test(&dp->dl_count)) {
256 dprintk("NFSD: freeing dp %p\n",dp);
257 put_nfs4_file(dp->dl_file);
258 kmem_cache_free(deleg_slab, dp);
263 static void nfs4_put_deleg_lease(struct nfs4_file *fp)
265 if (atomic_dec_and_test(&fp->fi_delegees)) {
266 vfs_setlease(fp->fi_deleg_file, F_UNLCK, &fp->fi_lease);
268 fput(fp->fi_deleg_file);
269 fp->fi_deleg_file = NULL;
273 /* Called under the state lock. */
275 unhash_delegation(struct nfs4_delegation *dp)
277 list_del_init(&dp->dl_perclnt);
278 spin_lock(&recall_lock);
279 list_del_init(&dp->dl_perfile);
280 list_del_init(&dp->dl_recall_lru);
281 spin_unlock(&recall_lock);
282 nfs4_put_deleg_lease(dp->dl_file);
283 nfs4_put_delegation(dp);
290 /* client_lock protects the client lru list and session hash table */
291 static DEFINE_SPINLOCK(client_lock);
293 /* Hash tables for nfs4_clientid state */
294 #define CLIENT_HASH_BITS 4
295 #define CLIENT_HASH_SIZE (1 << CLIENT_HASH_BITS)
296 #define CLIENT_HASH_MASK (CLIENT_HASH_SIZE - 1)
298 #define clientid_hashval(id) \
299 ((id) & CLIENT_HASH_MASK)
300 #define clientstr_hashval(name) \
301 (opaque_hashval((name), 8) & CLIENT_HASH_MASK)
303 * reclaim_str_hashtbl[] holds known client info from previous reset/reboot
304 * used in reboot/reset lease grace period processing
306 * conf_id_hashtbl[], and conf_str_hashtbl[] hold confirmed
307 * setclientid_confirmed info.
309 * unconf_str_hastbl[] and unconf_id_hashtbl[] hold unconfirmed
312 * client_lru holds client queue ordered by nfs4_client.cl_time
315 * close_lru holds (open) stateowner queue ordered by nfs4_stateowner.so_time
316 * for last close replay.
318 static struct list_head reclaim_str_hashtbl[CLIENT_HASH_SIZE];
319 static int reclaim_str_hashtbl_size = 0;
320 static struct list_head conf_id_hashtbl[CLIENT_HASH_SIZE];
321 static struct list_head conf_str_hashtbl[CLIENT_HASH_SIZE];
322 static struct list_head unconf_str_hashtbl[CLIENT_HASH_SIZE];
323 static struct list_head unconf_id_hashtbl[CLIENT_HASH_SIZE];
324 static struct list_head client_lru;
325 static struct list_head close_lru;
328 * We store the NONE, READ, WRITE, and BOTH bits separately in the
329 * st_{access,deny}_bmap field of the stateid, in order to track not
330 * only what share bits are currently in force, but also what
331 * combinations of share bits previous opens have used. This allows us
332 * to enforce the recommendation of rfc 3530 14.2.19 that the server
333 * return an error if the client attempt to downgrade to a combination
334 * of share bits not explicable by closing some of its previous opens.
336 * XXX: This enforcement is actually incomplete, since we don't keep
337 * track of access/deny bit combinations; so, e.g., we allow:
339 * OPEN allow read, deny write
340 * OPEN allow both, deny none
341 * DOWNGRADE allow read, deny none
343 * which we should reject.
346 set_access(unsigned int *access, unsigned long bmap) {
350 for (i = 1; i < 4; i++) {
351 if (test_bit(i, &bmap))
357 set_deny(unsigned int *deny, unsigned long bmap) {
361 for (i = 0; i < 4; i++) {
362 if (test_bit(i, &bmap))
368 test_share(struct nfs4_stateid *stp, struct nfsd4_open *open) {
369 unsigned int access, deny;
371 set_access(&access, stp->st_access_bmap);
372 set_deny(&deny, stp->st_deny_bmap);
373 if ((access & open->op_share_deny) || (deny & open->op_share_access))
378 static int nfs4_access_to_omode(u32 access)
380 switch (access & NFS4_SHARE_ACCESS_BOTH) {
381 case NFS4_SHARE_ACCESS_READ:
383 case NFS4_SHARE_ACCESS_WRITE:
385 case NFS4_SHARE_ACCESS_BOTH:
391 static void unhash_generic_stateid(struct nfs4_stateid *stp)
393 list_del(&stp->st_hash);
394 list_del(&stp->st_perfile);
395 list_del(&stp->st_perstateowner);
398 static void free_generic_stateid(struct nfs4_stateid *stp)
402 if (stp->st_access_bmap) {
403 for (i = 1; i < 4; i++) {
404 if (test_bit(i, &stp->st_access_bmap))
405 nfs4_file_put_access(stp->st_file,
406 nfs4_access_to_omode(i));
409 put_nfs4_file(stp->st_file);
410 kmem_cache_free(stateid_slab, stp);
413 static void release_lock_stateid(struct nfs4_stateid *stp)
417 unhash_generic_stateid(stp);
418 file = find_any_file(stp->st_file);
420 locks_remove_posix(file, (fl_owner_t)stp->st_stateowner);
421 free_generic_stateid(stp);
424 static void unhash_lockowner(struct nfs4_stateowner *sop)
426 struct nfs4_stateid *stp;
428 list_del(&sop->so_idhash);
429 list_del(&sop->so_strhash);
430 list_del(&sop->so_perstateid);
431 while (!list_empty(&sop->so_stateids)) {
432 stp = list_first_entry(&sop->so_stateids,
433 struct nfs4_stateid, st_perstateowner);
434 release_lock_stateid(stp);
438 static void release_lockowner(struct nfs4_stateowner *sop)
440 unhash_lockowner(sop);
441 nfs4_put_stateowner(sop);
445 release_stateid_lockowners(struct nfs4_stateid *open_stp)
447 struct nfs4_stateowner *lock_sop;
449 while (!list_empty(&open_stp->st_lockowners)) {
450 lock_sop = list_entry(open_stp->st_lockowners.next,
451 struct nfs4_stateowner, so_perstateid);
452 /* list_del(&open_stp->st_lockowners); */
453 BUG_ON(lock_sop->so_is_open_owner);
454 release_lockowner(lock_sop);
458 static void release_open_stateid(struct nfs4_stateid *stp)
460 unhash_generic_stateid(stp);
461 release_stateid_lockowners(stp);
462 free_generic_stateid(stp);
465 static void unhash_openowner(struct nfs4_stateowner *sop)
467 struct nfs4_stateid *stp;
469 list_del(&sop->so_idhash);
470 list_del(&sop->so_strhash);
471 list_del(&sop->so_perclient);
472 list_del(&sop->so_perstateid); /* XXX: necessary? */
473 while (!list_empty(&sop->so_stateids)) {
474 stp = list_first_entry(&sop->so_stateids,
475 struct nfs4_stateid, st_perstateowner);
476 release_open_stateid(stp);
480 static void release_openowner(struct nfs4_stateowner *sop)
482 unhash_openowner(sop);
483 list_del(&sop->so_close_lru);
484 nfs4_put_stateowner(sop);
487 #define SESSION_HASH_SIZE 512
488 static struct list_head sessionid_hashtbl[SESSION_HASH_SIZE];
491 hash_sessionid(struct nfs4_sessionid *sessionid)
493 struct nfsd4_sessionid *sid = (struct nfsd4_sessionid *)sessionid;
495 return sid->sequence % SESSION_HASH_SIZE;
499 dump_sessionid(const char *fn, struct nfs4_sessionid *sessionid)
501 u32 *ptr = (u32 *)(&sessionid->data[0]);
502 dprintk("%s: %u:%u:%u:%u\n", fn, ptr[0], ptr[1], ptr[2], ptr[3]);
506 gen_sessionid(struct nfsd4_session *ses)
508 struct nfs4_client *clp = ses->se_client;
509 struct nfsd4_sessionid *sid;
511 sid = (struct nfsd4_sessionid *)ses->se_sessionid.data;
512 sid->clientid = clp->cl_clientid;
513 sid->sequence = current_sessionid++;
518 * The protocol defines ca_maxresponssize_cached to include the size of
519 * the rpc header, but all we need to cache is the data starting after
520 * the end of the initial SEQUENCE operation--the rest we regenerate
521 * each time. Therefore we can advertise a ca_maxresponssize_cached
522 * value that is the number of bytes in our cache plus a few additional
523 * bytes. In order to stay on the safe side, and not promise more than
524 * we can cache, those additional bytes must be the minimum possible: 24
525 * bytes of rpc header (xid through accept state, with AUTH_NULL
526 * verifier), 12 for the compound header (with zero-length tag), and 44
527 * for the SEQUENCE op response:
529 #define NFSD_MIN_HDR_SEQ_SZ (24 + 12 + 44)
532 free_session_slots(struct nfsd4_session *ses)
536 for (i = 0; i < ses->se_fchannel.maxreqs; i++)
537 kfree(ses->se_slots[i]);
541 * We don't actually need to cache the rpc and session headers, so we
542 * can allocate a little less for each slot:
544 static inline int slot_bytes(struct nfsd4_channel_attrs *ca)
546 return ca->maxresp_cached - NFSD_MIN_HDR_SEQ_SZ;
549 static int nfsd4_sanitize_slot_size(u32 size)
551 size -= NFSD_MIN_HDR_SEQ_SZ; /* We don't cache the rpc header */
552 size = min_t(u32, size, NFSD_SLOT_CACHE_SIZE);
558 * XXX: If we run out of reserved DRC memory we could (up to a point)
559 * re-negotiate active sessions and reduce their slot usage to make
560 * rooom for new connections. For now we just fail the create session.
562 static int nfsd4_get_drc_mem(int slotsize, u32 num)
566 num = min_t(u32, num, NFSD_MAX_SLOTS_PER_SESSION);
568 spin_lock(&nfsd_drc_lock);
569 avail = min_t(int, NFSD_MAX_MEM_PER_SESSION,
570 nfsd_drc_max_mem - nfsd_drc_mem_used);
571 num = min_t(int, num, avail / slotsize);
572 nfsd_drc_mem_used += num * slotsize;
573 spin_unlock(&nfsd_drc_lock);
578 static void nfsd4_put_drc_mem(int slotsize, int num)
580 spin_lock(&nfsd_drc_lock);
581 nfsd_drc_mem_used -= slotsize * num;
582 spin_unlock(&nfsd_drc_lock);
585 static struct nfsd4_session *alloc_session(int slotsize, int numslots)
587 struct nfsd4_session *new;
590 BUILD_BUG_ON(NFSD_MAX_SLOTS_PER_SESSION * sizeof(struct nfsd4_slot *)
591 + sizeof(struct nfsd4_session) > PAGE_SIZE);
592 mem = numslots * sizeof(struct nfsd4_slot *);
594 new = kzalloc(sizeof(*new) + mem, GFP_KERNEL);
597 /* allocate each struct nfsd4_slot and data cache in one piece */
598 for (i = 0; i < numslots; i++) {
599 mem = sizeof(struct nfsd4_slot) + slotsize;
600 new->se_slots[i] = kzalloc(mem, GFP_KERNEL);
601 if (!new->se_slots[i])
607 kfree(new->se_slots[i]);
612 static void init_forechannel_attrs(struct nfsd4_channel_attrs *new, struct nfsd4_channel_attrs *req, int numslots, int slotsize)
614 u32 maxrpc = nfsd_serv->sv_max_mesg;
616 new->maxreqs = numslots;
617 new->maxresp_cached = min_t(u32, req->maxresp_cached,
618 slotsize + NFSD_MIN_HDR_SEQ_SZ);
619 new->maxreq_sz = min_t(u32, req->maxreq_sz, maxrpc);
620 new->maxresp_sz = min_t(u32, req->maxresp_sz, maxrpc);
621 new->maxops = min_t(u32, req->maxops, NFSD_MAX_OPS_PER_COMPOUND);
624 static void free_conn(struct nfsd4_conn *c)
626 svc_xprt_put(c->cn_xprt);
630 static void nfsd4_conn_lost(struct svc_xpt_user *u)
632 struct nfsd4_conn *c = container_of(u, struct nfsd4_conn, cn_xpt_user);
633 struct nfs4_client *clp = c->cn_session->se_client;
635 spin_lock(&clp->cl_lock);
636 if (!list_empty(&c->cn_persession)) {
637 list_del(&c->cn_persession);
640 spin_unlock(&clp->cl_lock);
641 nfsd4_probe_callback(clp);
644 static struct nfsd4_conn *alloc_conn(struct svc_rqst *rqstp, u32 flags)
646 struct nfsd4_conn *conn;
648 conn = kmalloc(sizeof(struct nfsd4_conn), GFP_KERNEL);
651 svc_xprt_get(rqstp->rq_xprt);
652 conn->cn_xprt = rqstp->rq_xprt;
653 conn->cn_flags = flags;
654 INIT_LIST_HEAD(&conn->cn_xpt_user.list);
658 static void __nfsd4_hash_conn(struct nfsd4_conn *conn, struct nfsd4_session *ses)
660 conn->cn_session = ses;
661 list_add(&conn->cn_persession, &ses->se_conns);
664 static void nfsd4_hash_conn(struct nfsd4_conn *conn, struct nfsd4_session *ses)
666 struct nfs4_client *clp = ses->se_client;
668 spin_lock(&clp->cl_lock);
669 __nfsd4_hash_conn(conn, ses);
670 spin_unlock(&clp->cl_lock);
673 static int nfsd4_register_conn(struct nfsd4_conn *conn)
675 conn->cn_xpt_user.callback = nfsd4_conn_lost;
676 return register_xpt_user(conn->cn_xprt, &conn->cn_xpt_user);
679 static __be32 nfsd4_new_conn(struct svc_rqst *rqstp, struct nfsd4_session *ses, u32 dir)
681 struct nfsd4_conn *conn;
684 conn = alloc_conn(rqstp, dir);
686 return nfserr_jukebox;
687 nfsd4_hash_conn(conn, ses);
688 ret = nfsd4_register_conn(conn);
690 /* oops; xprt is already down: */
691 nfsd4_conn_lost(&conn->cn_xpt_user);
695 static __be32 nfsd4_new_conn_from_crses(struct svc_rqst *rqstp, struct nfsd4_session *ses)
697 u32 dir = NFS4_CDFC4_FORE;
699 if (ses->se_flags & SESSION4_BACK_CHAN)
700 dir |= NFS4_CDFC4_BACK;
702 return nfsd4_new_conn(rqstp, ses, dir);
705 /* must be called under client_lock */
706 static void nfsd4_del_conns(struct nfsd4_session *s)
708 struct nfs4_client *clp = s->se_client;
709 struct nfsd4_conn *c;
711 spin_lock(&clp->cl_lock);
712 while (!list_empty(&s->se_conns)) {
713 c = list_first_entry(&s->se_conns, struct nfsd4_conn, cn_persession);
714 list_del_init(&c->cn_persession);
715 spin_unlock(&clp->cl_lock);
717 unregister_xpt_user(c->cn_xprt, &c->cn_xpt_user);
720 spin_lock(&clp->cl_lock);
722 spin_unlock(&clp->cl_lock);
725 void free_session(struct kref *kref)
727 struct nfsd4_session *ses;
730 ses = container_of(kref, struct nfsd4_session, se_ref);
731 nfsd4_del_conns(ses);
732 spin_lock(&nfsd_drc_lock);
733 mem = ses->se_fchannel.maxreqs * slot_bytes(&ses->se_fchannel);
734 nfsd_drc_mem_used -= mem;
735 spin_unlock(&nfsd_drc_lock);
736 free_session_slots(ses);
740 static struct nfsd4_session *alloc_init_session(struct svc_rqst *rqstp, struct nfs4_client *clp, struct nfsd4_create_session *cses)
742 struct nfsd4_session *new;
743 struct nfsd4_channel_attrs *fchan = &cses->fore_channel;
744 int numslots, slotsize;
749 * Note decreasing slot size below client's request may
750 * make it difficult for client to function correctly, whereas
751 * decreasing the number of slots will (just?) affect
752 * performance. When short on memory we therefore prefer to
753 * decrease number of slots instead of their size.
755 slotsize = nfsd4_sanitize_slot_size(fchan->maxresp_cached);
756 numslots = nfsd4_get_drc_mem(slotsize, fchan->maxreqs);
760 new = alloc_session(slotsize, numslots);
762 nfsd4_put_drc_mem(slotsize, fchan->maxreqs);
765 init_forechannel_attrs(&new->se_fchannel, fchan, numslots, slotsize);
767 new->se_client = clp;
770 INIT_LIST_HEAD(&new->se_conns);
772 new->se_cb_seq_nr = 1;
773 new->se_flags = cses->flags;
774 new->se_cb_prog = cses->callback_prog;
775 kref_init(&new->se_ref);
776 idx = hash_sessionid(&new->se_sessionid);
777 spin_lock(&client_lock);
778 list_add(&new->se_hash, &sessionid_hashtbl[idx]);
779 spin_lock(&clp->cl_lock);
780 list_add(&new->se_perclnt, &clp->cl_sessions);
781 spin_unlock(&clp->cl_lock);
782 spin_unlock(&client_lock);
784 status = nfsd4_new_conn_from_crses(rqstp, new);
785 /* whoops: benny points out, status is ignored! (err, or bogus) */
787 free_session(&new->se_ref);
790 if (cses->flags & SESSION4_BACK_CHAN) {
791 struct sockaddr *sa = svc_addr(rqstp);
793 * This is a little silly; with sessions there's no real
794 * use for the callback address. Use the peer address
795 * as a reasonable default for now, but consider fixing
796 * the rpc client not to require an address in the
799 rpc_copy_addr((struct sockaddr *)&clp->cl_cb_conn.cb_addr, sa);
800 clp->cl_cb_conn.cb_addrlen = svc_addr_len(sa);
802 nfsd4_probe_callback(clp);
806 /* caller must hold client_lock */
807 static struct nfsd4_session *
808 find_in_sessionid_hashtbl(struct nfs4_sessionid *sessionid)
810 struct nfsd4_session *elem;
813 dump_sessionid(__func__, sessionid);
814 idx = hash_sessionid(sessionid);
815 /* Search in the appropriate list */
816 list_for_each_entry(elem, &sessionid_hashtbl[idx], se_hash) {
817 if (!memcmp(elem->se_sessionid.data, sessionid->data,
818 NFS4_MAX_SESSIONID_LEN)) {
823 dprintk("%s: session not found\n", __func__);
827 /* caller must hold client_lock */
829 unhash_session(struct nfsd4_session *ses)
831 list_del(&ses->se_hash);
832 spin_lock(&ses->se_client->cl_lock);
833 list_del(&ses->se_perclnt);
834 spin_unlock(&ses->se_client->cl_lock);
837 /* must be called under the client_lock */
839 renew_client_locked(struct nfs4_client *clp)
841 if (is_client_expired(clp)) {
842 dprintk("%s: client (clientid %08x/%08x) already expired\n",
844 clp->cl_clientid.cl_boot,
845 clp->cl_clientid.cl_id);
850 * Move client to the end to the LRU list.
852 dprintk("renewing client (clientid %08x/%08x)\n",
853 clp->cl_clientid.cl_boot,
854 clp->cl_clientid.cl_id);
855 list_move_tail(&clp->cl_lru, &client_lru);
856 clp->cl_time = get_seconds();
860 renew_client(struct nfs4_client *clp)
862 spin_lock(&client_lock);
863 renew_client_locked(clp);
864 spin_unlock(&client_lock);
867 /* SETCLIENTID and SETCLIENTID_CONFIRM Helper functions */
869 STALE_CLIENTID(clientid_t *clid)
871 if (clid->cl_boot == boot_time)
873 dprintk("NFSD stale clientid (%08x/%08x) boot_time %08lx\n",
874 clid->cl_boot, clid->cl_id, boot_time);
879 * XXX Should we use a slab cache ?
880 * This type of memory management is somewhat inefficient, but we use it
881 * anyway since SETCLIENTID is not a common operation.
883 static struct nfs4_client *alloc_client(struct xdr_netobj name)
885 struct nfs4_client *clp;
887 clp = kzalloc(sizeof(struct nfs4_client), GFP_KERNEL);
890 clp->cl_name.data = kmalloc(name.len, GFP_KERNEL);
891 if (clp->cl_name.data == NULL) {
895 memcpy(clp->cl_name.data, name.data, name.len);
896 clp->cl_name.len = name.len;
901 free_client(struct nfs4_client *clp)
903 while (!list_empty(&clp->cl_sessions)) {
904 struct nfsd4_session *ses;
905 ses = list_entry(clp->cl_sessions.next, struct nfsd4_session,
907 list_del(&ses->se_perclnt);
908 nfsd4_put_session(ses);
910 if (clp->cl_cred.cr_group_info)
911 put_group_info(clp->cl_cred.cr_group_info);
912 kfree(clp->cl_principal);
913 kfree(clp->cl_name.data);
918 release_session_client(struct nfsd4_session *session)
920 struct nfs4_client *clp = session->se_client;
922 if (!atomic_dec_and_lock(&clp->cl_refcount, &client_lock))
924 if (is_client_expired(clp)) {
926 session->se_client = NULL;
928 renew_client_locked(clp);
929 spin_unlock(&client_lock);
932 /* must be called under the client_lock */
934 unhash_client_locked(struct nfs4_client *clp)
936 struct nfsd4_session *ses;
938 mark_client_expired(clp);
939 list_del(&clp->cl_lru);
940 spin_lock(&clp->cl_lock);
941 list_for_each_entry(ses, &clp->cl_sessions, se_perclnt)
942 list_del_init(&ses->se_hash);
943 spin_unlock(&clp->cl_lock);
947 expire_client(struct nfs4_client *clp)
949 struct nfs4_stateowner *sop;
950 struct nfs4_delegation *dp;
951 struct list_head reaplist;
953 INIT_LIST_HEAD(&reaplist);
954 spin_lock(&recall_lock);
955 while (!list_empty(&clp->cl_delegations)) {
956 dp = list_entry(clp->cl_delegations.next, struct nfs4_delegation, dl_perclnt);
957 list_del_init(&dp->dl_perclnt);
958 list_move(&dp->dl_recall_lru, &reaplist);
960 spin_unlock(&recall_lock);
961 while (!list_empty(&reaplist)) {
962 dp = list_entry(reaplist.next, struct nfs4_delegation, dl_recall_lru);
963 list_del_init(&dp->dl_recall_lru);
964 unhash_delegation(dp);
966 while (!list_empty(&clp->cl_openowners)) {
967 sop = list_entry(clp->cl_openowners.next, struct nfs4_stateowner, so_perclient);
968 release_openowner(sop);
970 nfsd4_shutdown_callback(clp);
971 if (clp->cl_cb_conn.cb_xprt)
972 svc_xprt_put(clp->cl_cb_conn.cb_xprt);
973 list_del(&clp->cl_idhash);
974 list_del(&clp->cl_strhash);
975 spin_lock(&client_lock);
976 unhash_client_locked(clp);
977 if (atomic_read(&clp->cl_refcount) == 0)
979 spin_unlock(&client_lock);
982 static void copy_verf(struct nfs4_client *target, nfs4_verifier *source)
984 memcpy(target->cl_verifier.data, source->data,
985 sizeof(target->cl_verifier.data));
988 static void copy_clid(struct nfs4_client *target, struct nfs4_client *source)
990 target->cl_clientid.cl_boot = source->cl_clientid.cl_boot;
991 target->cl_clientid.cl_id = source->cl_clientid.cl_id;
994 static void copy_cred(struct svc_cred *target, struct svc_cred *source)
996 target->cr_uid = source->cr_uid;
997 target->cr_gid = source->cr_gid;
998 target->cr_group_info = source->cr_group_info;
999 get_group_info(target->cr_group_info);
1002 static int same_name(const char *n1, const char *n2)
1004 return 0 == memcmp(n1, n2, HEXDIR_LEN);
1008 same_verf(nfs4_verifier *v1, nfs4_verifier *v2)
1010 return 0 == memcmp(v1->data, v2->data, sizeof(v1->data));
1014 same_clid(clientid_t *cl1, clientid_t *cl2)
1016 return (cl1->cl_boot == cl2->cl_boot) && (cl1->cl_id == cl2->cl_id);
1019 /* XXX what about NGROUP */
1021 same_creds(struct svc_cred *cr1, struct svc_cred *cr2)
1023 return cr1->cr_uid == cr2->cr_uid;
1026 static void gen_clid(struct nfs4_client *clp)
1028 static u32 current_clientid = 1;
1030 clp->cl_clientid.cl_boot = boot_time;
1031 clp->cl_clientid.cl_id = current_clientid++;
1034 static void gen_confirm(struct nfs4_client *clp)
1039 p = (u32 *)clp->cl_confirm.data;
1040 *p++ = get_seconds();
1044 static struct nfs4_client *create_client(struct xdr_netobj name, char *recdir,
1045 struct svc_rqst *rqstp, nfs4_verifier *verf)
1047 struct nfs4_client *clp;
1048 struct sockaddr *sa = svc_addr(rqstp);
1051 clp = alloc_client(name);
1055 INIT_LIST_HEAD(&clp->cl_sessions);
1057 princ = svc_gss_principal(rqstp);
1059 clp->cl_principal = kstrdup(princ, GFP_KERNEL);
1060 if (clp->cl_principal == NULL) {
1066 memcpy(clp->cl_recdir, recdir, HEXDIR_LEN);
1067 atomic_set(&clp->cl_refcount, 0);
1068 clp->cl_cb_state = NFSD4_CB_UNKNOWN;
1069 INIT_LIST_HEAD(&clp->cl_idhash);
1070 INIT_LIST_HEAD(&clp->cl_strhash);
1071 INIT_LIST_HEAD(&clp->cl_openowners);
1072 INIT_LIST_HEAD(&clp->cl_delegations);
1073 INIT_LIST_HEAD(&clp->cl_lru);
1074 INIT_LIST_HEAD(&clp->cl_callbacks);
1075 spin_lock_init(&clp->cl_lock);
1076 INIT_WORK(&clp->cl_cb_null.cb_work, nfsd4_do_callback_rpc);
1077 clp->cl_time = get_seconds();
1078 clear_bit(0, &clp->cl_cb_slot_busy);
1079 rpc_init_wait_queue(&clp->cl_cb_waitq, "Backchannel slot table");
1080 copy_verf(clp, verf);
1081 rpc_copy_addr((struct sockaddr *) &clp->cl_addr, sa);
1082 clp->cl_flavor = rqstp->rq_flavor;
1083 copy_cred(&clp->cl_cred, &rqstp->rq_cred);
1085 clp->cl_cb_session = NULL;
1089 static int check_name(struct xdr_netobj name)
1093 if (name.len > NFS4_OPAQUE_LIMIT) {
1094 dprintk("NFSD: check_name: name too long(%d)!\n", name.len);
1101 add_to_unconfirmed(struct nfs4_client *clp, unsigned int strhashval)
1103 unsigned int idhashval;
1105 list_add(&clp->cl_strhash, &unconf_str_hashtbl[strhashval]);
1106 idhashval = clientid_hashval(clp->cl_clientid.cl_id);
1107 list_add(&clp->cl_idhash, &unconf_id_hashtbl[idhashval]);
1112 move_to_confirmed(struct nfs4_client *clp)
1114 unsigned int idhashval = clientid_hashval(clp->cl_clientid.cl_id);
1115 unsigned int strhashval;
1117 dprintk("NFSD: move_to_confirm nfs4_client %p\n", clp);
1118 list_move(&clp->cl_idhash, &conf_id_hashtbl[idhashval]);
1119 strhashval = clientstr_hashval(clp->cl_recdir);
1120 list_move(&clp->cl_strhash, &conf_str_hashtbl[strhashval]);
1124 static struct nfs4_client *
1125 find_confirmed_client(clientid_t *clid)
1127 struct nfs4_client *clp;
1128 unsigned int idhashval = clientid_hashval(clid->cl_id);
1130 list_for_each_entry(clp, &conf_id_hashtbl[idhashval], cl_idhash) {
1131 if (same_clid(&clp->cl_clientid, clid))
1137 static struct nfs4_client *
1138 find_unconfirmed_client(clientid_t *clid)
1140 struct nfs4_client *clp;
1141 unsigned int idhashval = clientid_hashval(clid->cl_id);
1143 list_for_each_entry(clp, &unconf_id_hashtbl[idhashval], cl_idhash) {
1144 if (same_clid(&clp->cl_clientid, clid))
1150 static bool clp_used_exchangeid(struct nfs4_client *clp)
1152 return clp->cl_exchange_flags != 0;
1155 static struct nfs4_client *
1156 find_confirmed_client_by_str(const char *dname, unsigned int hashval)
1158 struct nfs4_client *clp;
1160 list_for_each_entry(clp, &conf_str_hashtbl[hashval], cl_strhash) {
1161 if (same_name(clp->cl_recdir, dname))
1167 static struct nfs4_client *
1168 find_unconfirmed_client_by_str(const char *dname, unsigned int hashval)
1170 struct nfs4_client *clp;
1172 list_for_each_entry(clp, &unconf_str_hashtbl[hashval], cl_strhash) {
1173 if (same_name(clp->cl_recdir, dname))
1179 static void rpc_svcaddr2sockaddr(struct sockaddr *sa, unsigned short family, union svc_addr_u *svcaddr)
1183 ((struct sockaddr_in *)sa)->sin_family = AF_INET;
1184 ((struct sockaddr_in *)sa)->sin_addr = svcaddr->addr;
1187 ((struct sockaddr_in6 *)sa)->sin6_family = AF_INET6;
1188 ((struct sockaddr_in6 *)sa)->sin6_addr = svcaddr->addr6;
1194 gen_callback(struct nfs4_client *clp, struct nfsd4_setclientid *se, struct svc_rqst *rqstp)
1196 struct nfs4_cb_conn *conn = &clp->cl_cb_conn;
1197 struct sockaddr *sa = svc_addr(rqstp);
1198 u32 scopeid = rpc_get_scope_id(sa);
1199 unsigned short expected_family;
1201 /* Currently, we only support tcp and tcp6 for the callback channel */
1202 if (se->se_callback_netid_len == 3 &&
1203 !memcmp(se->se_callback_netid_val, "tcp", 3))
1204 expected_family = AF_INET;
1205 else if (se->se_callback_netid_len == 4 &&
1206 !memcmp(se->se_callback_netid_val, "tcp6", 4))
1207 expected_family = AF_INET6;
1211 conn->cb_addrlen = rpc_uaddr2sockaddr(se->se_callback_addr_val,
1212 se->se_callback_addr_len,
1213 (struct sockaddr *)&conn->cb_addr,
1214 sizeof(conn->cb_addr));
1216 if (!conn->cb_addrlen || conn->cb_addr.ss_family != expected_family)
1219 if (conn->cb_addr.ss_family == AF_INET6)
1220 ((struct sockaddr_in6 *)&conn->cb_addr)->sin6_scope_id = scopeid;
1222 conn->cb_prog = se->se_callback_prog;
1223 conn->cb_ident = se->se_callback_ident;
1224 rpc_svcaddr2sockaddr((struct sockaddr *)&conn->cb_saddr, expected_family, &rqstp->rq_daddr);
1227 conn->cb_addr.ss_family = AF_UNSPEC;
1228 conn->cb_addrlen = 0;
1229 dprintk(KERN_INFO "NFSD: this client (clientid %08x/%08x) "
1230 "will not receive delegations\n",
1231 clp->cl_clientid.cl_boot, clp->cl_clientid.cl_id);
1237 * Cache a reply. nfsd4_check_drc_limit() has bounded the cache size.
1240 nfsd4_store_cache_entry(struct nfsd4_compoundres *resp)
1242 struct nfsd4_slot *slot = resp->cstate.slot;
1245 dprintk("--> %s slot %p\n", __func__, slot);
1247 slot->sl_opcnt = resp->opcnt;
1248 slot->sl_status = resp->cstate.status;
1250 if (nfsd4_not_cached(resp)) {
1251 slot->sl_datalen = 0;
1254 slot->sl_datalen = (char *)resp->p - (char *)resp->cstate.datap;
1255 base = (char *)resp->cstate.datap -
1256 (char *)resp->xbuf->head[0].iov_base;
1257 if (read_bytes_from_xdr_buf(resp->xbuf, base, slot->sl_data,
1259 WARN("%s: sessions DRC could not cache compound\n", __func__);
1264 * Encode the replay sequence operation from the slot values.
1265 * If cachethis is FALSE encode the uncached rep error on the next
1266 * operation which sets resp->p and increments resp->opcnt for
1267 * nfs4svc_encode_compoundres.
1271 nfsd4_enc_sequence_replay(struct nfsd4_compoundargs *args,
1272 struct nfsd4_compoundres *resp)
1274 struct nfsd4_op *op;
1275 struct nfsd4_slot *slot = resp->cstate.slot;
1277 dprintk("--> %s resp->opcnt %d cachethis %u \n", __func__,
1278 resp->opcnt, resp->cstate.slot->sl_cachethis);
1280 /* Encode the replayed sequence operation */
1281 op = &args->ops[resp->opcnt - 1];
1282 nfsd4_encode_operation(resp, op);
1284 /* Return nfserr_retry_uncached_rep in next operation. */
1285 if (args->opcnt > 1 && slot->sl_cachethis == 0) {
1286 op = &args->ops[resp->opcnt++];
1287 op->status = nfserr_retry_uncached_rep;
1288 nfsd4_encode_operation(resp, op);
1294 * The sequence operation is not cached because we can use the slot and
1298 nfsd4_replay_cache_entry(struct nfsd4_compoundres *resp,
1299 struct nfsd4_sequence *seq)
1301 struct nfsd4_slot *slot = resp->cstate.slot;
1304 dprintk("--> %s slot %p\n", __func__, slot);
1306 /* Either returns 0 or nfserr_retry_uncached */
1307 status = nfsd4_enc_sequence_replay(resp->rqstp->rq_argp, resp);
1308 if (status == nfserr_retry_uncached_rep)
1311 /* The sequence operation has been encoded, cstate->datap set. */
1312 memcpy(resp->cstate.datap, slot->sl_data, slot->sl_datalen);
1314 resp->opcnt = slot->sl_opcnt;
1315 resp->p = resp->cstate.datap + XDR_QUADLEN(slot->sl_datalen);
1316 status = slot->sl_status;
1322 * Set the exchange_id flags returned by the server.
1325 nfsd4_set_ex_flags(struct nfs4_client *new, struct nfsd4_exchange_id *clid)
1327 /* pNFS is not supported */
1328 new->cl_exchange_flags |= EXCHGID4_FLAG_USE_NON_PNFS;
1330 /* Referrals are supported, Migration is not. */
1331 new->cl_exchange_flags |= EXCHGID4_FLAG_SUPP_MOVED_REFER;
1333 /* set the wire flags to return to client. */
1334 clid->flags = new->cl_exchange_flags;
1338 nfsd4_exchange_id(struct svc_rqst *rqstp,
1339 struct nfsd4_compound_state *cstate,
1340 struct nfsd4_exchange_id *exid)
1342 struct nfs4_client *unconf, *conf, *new;
1344 unsigned int strhashval;
1345 char dname[HEXDIR_LEN];
1346 char addr_str[INET6_ADDRSTRLEN];
1347 nfs4_verifier verf = exid->verifier;
1348 struct sockaddr *sa = svc_addr(rqstp);
1350 rpc_ntop(sa, addr_str, sizeof(addr_str));
1351 dprintk("%s rqstp=%p exid=%p clname.len=%u clname.data=%p "
1352 "ip_addr=%s flags %x, spa_how %d\n",
1353 __func__, rqstp, exid, exid->clname.len, exid->clname.data,
1354 addr_str, exid->flags, exid->spa_how);
1356 if (!check_name(exid->clname) || (exid->flags & ~EXCHGID4_FLAG_MASK_A))
1357 return nfserr_inval;
1359 /* Currently only support SP4_NONE */
1360 switch (exid->spa_how) {
1364 return nfserr_serverfault;
1366 BUG(); /* checked by xdr code */
1368 return nfserr_serverfault; /* no excuse :-/ */
1371 status = nfs4_make_rec_clidname(dname, &exid->clname);
1376 strhashval = clientstr_hashval(dname);
1381 conf = find_confirmed_client_by_str(dname, strhashval);
1383 if (!clp_used_exchangeid(conf)) {
1384 status = nfserr_clid_inuse; /* XXX: ? */
1387 if (!same_verf(&verf, &conf->cl_verifier)) {
1388 /* 18.35.4 case 8 */
1389 if (exid->flags & EXCHGID4_FLAG_UPD_CONFIRMED_REC_A) {
1390 status = nfserr_not_same;
1393 /* Client reboot: destroy old state */
1394 expire_client(conf);
1397 if (!same_creds(&conf->cl_cred, &rqstp->rq_cred)) {
1398 /* 18.35.4 case 9 */
1399 if (exid->flags & EXCHGID4_FLAG_UPD_CONFIRMED_REC_A) {
1400 status = nfserr_perm;
1403 expire_client(conf);
1407 * Set bit when the owner id and verifier map to an already
1408 * confirmed client id (18.35.3).
1410 exid->flags |= EXCHGID4_FLAG_CONFIRMED_R;
1413 * Falling into 18.35.4 case 2, possible router replay.
1414 * Leave confirmed record intact and return same result.
1416 copy_verf(conf, &verf);
1421 /* 18.35.4 case 7 */
1422 if (exid->flags & EXCHGID4_FLAG_UPD_CONFIRMED_REC_A) {
1423 status = nfserr_noent;
1427 unconf = find_unconfirmed_client_by_str(dname, strhashval);
1430 * Possible retry or client restart. Per 18.35.4 case 4,
1431 * a new unconfirmed record should be generated regardless
1432 * of whether any properties have changed.
1434 expire_client(unconf);
1439 new = create_client(exid->clname, dname, rqstp, &verf);
1441 status = nfserr_jukebox;
1446 add_to_unconfirmed(new, strhashval);
1448 exid->clientid.cl_boot = new->cl_clientid.cl_boot;
1449 exid->clientid.cl_id = new->cl_clientid.cl_id;
1452 nfsd4_set_ex_flags(new, exid);
1454 dprintk("nfsd4_exchange_id seqid %d flags %x\n",
1455 new->cl_cs_slot.sl_seqid, new->cl_exchange_flags);
1459 nfs4_unlock_state();
1461 dprintk("nfsd4_exchange_id returns %d\n", ntohl(status));
1466 check_slot_seqid(u32 seqid, u32 slot_seqid, int slot_inuse)
1468 dprintk("%s enter. seqid %d slot_seqid %d\n", __func__, seqid,
1471 /* The slot is in use, and no response has been sent. */
1473 if (seqid == slot_seqid)
1474 return nfserr_jukebox;
1476 return nfserr_seq_misordered;
1479 if (likely(seqid == slot_seqid + 1))
1482 if (seqid == slot_seqid)
1483 return nfserr_replay_cache;
1485 if (seqid == 1 && (slot_seqid + 1) == 0)
1487 /* Misordered replay or misordered new request */
1488 return nfserr_seq_misordered;
1492 * Cache the create session result into the create session single DRC
1493 * slot cache by saving the xdr structure. sl_seqid has been set.
1494 * Do this for solo or embedded create session operations.
1497 nfsd4_cache_create_session(struct nfsd4_create_session *cr_ses,
1498 struct nfsd4_clid_slot *slot, int nfserr)
1500 slot->sl_status = nfserr;
1501 memcpy(&slot->sl_cr_ses, cr_ses, sizeof(*cr_ses));
1505 nfsd4_replay_create_session(struct nfsd4_create_session *cr_ses,
1506 struct nfsd4_clid_slot *slot)
1508 memcpy(cr_ses, &slot->sl_cr_ses, sizeof(*cr_ses));
1509 return slot->sl_status;
1513 nfsd4_create_session(struct svc_rqst *rqstp,
1514 struct nfsd4_compound_state *cstate,
1515 struct nfsd4_create_session *cr_ses)
1517 struct sockaddr *sa = svc_addr(rqstp);
1518 struct nfs4_client *conf, *unconf;
1519 struct nfsd4_session *new;
1520 struct nfsd4_clid_slot *cs_slot = NULL;
1521 bool confirm_me = false;
1524 if (cr_ses->flags & ~SESSION4_FLAG_MASK_A)
1525 return nfserr_inval;
1528 unconf = find_unconfirmed_client(&cr_ses->clientid);
1529 conf = find_confirmed_client(&cr_ses->clientid);
1532 cs_slot = &conf->cl_cs_slot;
1533 status = check_slot_seqid(cr_ses->seqid, cs_slot->sl_seqid, 0);
1534 if (status == nfserr_replay_cache) {
1535 dprintk("Got a create_session replay! seqid= %d\n",
1537 /* Return the cached reply status */
1538 status = nfsd4_replay_create_session(cr_ses, cs_slot);
1540 } else if (cr_ses->seqid != cs_slot->sl_seqid + 1) {
1541 status = nfserr_seq_misordered;
1542 dprintk("Sequence misordered!\n");
1543 dprintk("Expected seqid= %d but got seqid= %d\n",
1544 cs_slot->sl_seqid, cr_ses->seqid);
1547 } else if (unconf) {
1548 if (!same_creds(&unconf->cl_cred, &rqstp->rq_cred) ||
1549 !rpc_cmp_addr(sa, (struct sockaddr *) &unconf->cl_addr)) {
1550 status = nfserr_clid_inuse;
1554 cs_slot = &unconf->cl_cs_slot;
1555 status = check_slot_seqid(cr_ses->seqid, cs_slot->sl_seqid, 0);
1557 /* an unconfirmed replay returns misordered */
1558 status = nfserr_seq_misordered;
1565 status = nfserr_stale_clientid;
1570 * XXX: we should probably set this at creation time, and check
1571 * for consistent minorversion use throughout:
1573 conf->cl_minorversion = 1;
1575 * We do not support RDMA or persistent sessions
1577 cr_ses->flags &= ~SESSION4_PERSIST;
1578 cr_ses->flags &= ~SESSION4_RDMA;
1580 status = nfserr_jukebox;
1581 new = alloc_init_session(rqstp, conf, cr_ses);
1585 memcpy(cr_ses->sessionid.data, new->se_sessionid.data,
1586 NFS4_MAX_SESSIONID_LEN);
1587 memcpy(&cr_ses->fore_channel, &new->se_fchannel,
1588 sizeof(struct nfsd4_channel_attrs));
1589 cs_slot->sl_seqid++;
1590 cr_ses->seqid = cs_slot->sl_seqid;
1592 /* cache solo and embedded create sessions under the state lock */
1593 nfsd4_cache_create_session(cr_ses, cs_slot, status);
1595 move_to_confirmed(conf);
1597 nfs4_unlock_state();
1598 dprintk("%s returns %d\n", __func__, ntohl(status));
1602 static bool nfsd4_last_compound_op(struct svc_rqst *rqstp)
1604 struct nfsd4_compoundres *resp = rqstp->rq_resp;
1605 struct nfsd4_compoundargs *argp = rqstp->rq_argp;
1607 return argp->opcnt == resp->opcnt;
1610 static __be32 nfsd4_map_bcts_dir(u32 *dir)
1613 case NFS4_CDFC4_FORE:
1614 case NFS4_CDFC4_BACK:
1616 case NFS4_CDFC4_FORE_OR_BOTH:
1617 case NFS4_CDFC4_BACK_OR_BOTH:
1618 *dir = NFS4_CDFC4_BOTH;
1621 return nfserr_inval;
1624 __be32 nfsd4_bind_conn_to_session(struct svc_rqst *rqstp,
1625 struct nfsd4_compound_state *cstate,
1626 struct nfsd4_bind_conn_to_session *bcts)
1630 if (!nfsd4_last_compound_op(rqstp))
1631 return nfserr_not_only_op;
1632 spin_lock(&client_lock);
1633 cstate->session = find_in_sessionid_hashtbl(&bcts->sessionid);
1634 /* Sorta weird: we only need the refcnt'ing because new_conn acquires
1635 * client_lock iself: */
1636 if (cstate->session) {
1637 nfsd4_get_session(cstate->session);
1638 atomic_inc(&cstate->session->se_client->cl_refcount);
1640 spin_unlock(&client_lock);
1641 if (!cstate->session)
1642 return nfserr_badsession;
1644 status = nfsd4_map_bcts_dir(&bcts->dir);
1646 nfsd4_new_conn(rqstp, cstate->session, bcts->dir);
1650 static bool nfsd4_compound_in_session(struct nfsd4_session *session, struct nfs4_sessionid *sid)
1654 return !memcmp(sid, &session->se_sessionid, sizeof(*sid));
1658 nfsd4_destroy_session(struct svc_rqst *r,
1659 struct nfsd4_compound_state *cstate,
1660 struct nfsd4_destroy_session *sessionid)
1662 struct nfsd4_session *ses;
1663 u32 status = nfserr_badsession;
1666 * - The confirmed nfs4_client->cl_sessionid holds destroyed sessinid
1667 * - Should we return nfserr_back_chan_busy if waiting for
1668 * callbacks on to-be-destroyed session?
1669 * - Do we need to clear any callback info from previous session?
1672 if (nfsd4_compound_in_session(cstate->session, &sessionid->sessionid)) {
1673 if (!nfsd4_last_compound_op(r))
1674 return nfserr_not_only_op;
1676 dump_sessionid(__func__, &sessionid->sessionid);
1677 spin_lock(&client_lock);
1678 ses = find_in_sessionid_hashtbl(&sessionid->sessionid);
1680 spin_unlock(&client_lock);
1684 unhash_session(ses);
1685 spin_unlock(&client_lock);
1688 nfsd4_probe_callback_sync(ses->se_client);
1689 nfs4_unlock_state();
1691 nfsd4_del_conns(ses);
1693 nfsd4_put_session(ses);
1696 dprintk("%s returns %d\n", __func__, ntohl(status));
1700 static struct nfsd4_conn *__nfsd4_find_conn(struct svc_xprt *xpt, struct nfsd4_session *s)
1702 struct nfsd4_conn *c;
1704 list_for_each_entry(c, &s->se_conns, cn_persession) {
1705 if (c->cn_xprt == xpt) {
1712 static void nfsd4_sequence_check_conn(struct nfsd4_conn *new, struct nfsd4_session *ses)
1714 struct nfs4_client *clp = ses->se_client;
1715 struct nfsd4_conn *c;
1718 spin_lock(&clp->cl_lock);
1719 c = __nfsd4_find_conn(new->cn_xprt, ses);
1721 spin_unlock(&clp->cl_lock);
1725 __nfsd4_hash_conn(new, ses);
1726 spin_unlock(&clp->cl_lock);
1727 ret = nfsd4_register_conn(new);
1729 /* oops; xprt is already down: */
1730 nfsd4_conn_lost(&new->cn_xpt_user);
1734 static bool nfsd4_session_too_many_ops(struct svc_rqst *rqstp, struct nfsd4_session *session)
1736 struct nfsd4_compoundargs *args = rqstp->rq_argp;
1738 return args->opcnt > session->se_fchannel.maxops;
1742 nfsd4_sequence(struct svc_rqst *rqstp,
1743 struct nfsd4_compound_state *cstate,
1744 struct nfsd4_sequence *seq)
1746 struct nfsd4_compoundres *resp = rqstp->rq_resp;
1747 struct nfsd4_session *session;
1748 struct nfsd4_slot *slot;
1749 struct nfsd4_conn *conn;
1752 if (resp->opcnt != 1)
1753 return nfserr_sequence_pos;
1756 * Will be either used or freed by nfsd4_sequence_check_conn
1759 conn = alloc_conn(rqstp, NFS4_CDFC4_FORE);
1761 return nfserr_jukebox;
1763 spin_lock(&client_lock);
1764 status = nfserr_badsession;
1765 session = find_in_sessionid_hashtbl(&seq->sessionid);
1769 status = nfserr_too_many_ops;
1770 if (nfsd4_session_too_many_ops(rqstp, session))
1773 status = nfserr_badslot;
1774 if (seq->slotid >= session->se_fchannel.maxreqs)
1777 slot = session->se_slots[seq->slotid];
1778 dprintk("%s: slotid %d\n", __func__, seq->slotid);
1780 /* We do not negotiate the number of slots yet, so set the
1781 * maxslots to the session maxreqs which is used to encode
1782 * sr_highest_slotid and the sr_target_slot id to maxslots */
1783 seq->maxslots = session->se_fchannel.maxreqs;
1785 status = check_slot_seqid(seq->seqid, slot->sl_seqid, slot->sl_inuse);
1786 if (status == nfserr_replay_cache) {
1787 cstate->slot = slot;
1788 cstate->session = session;
1789 /* Return the cached reply status and set cstate->status
1790 * for nfsd4_proc_compound processing */
1791 status = nfsd4_replay_cache_entry(resp, seq);
1792 cstate->status = nfserr_replay_cache;
1798 nfsd4_sequence_check_conn(conn, session);
1801 /* Success! bump slot seqid */
1802 slot->sl_inuse = true;
1803 slot->sl_seqid = seq->seqid;
1804 slot->sl_cachethis = seq->cachethis;
1806 cstate->slot = slot;
1807 cstate->session = session;
1810 /* Hold a session reference until done processing the compound. */
1811 if (cstate->session) {
1812 struct nfs4_client *clp = session->se_client;
1814 nfsd4_get_session(cstate->session);
1815 atomic_inc(&clp->cl_refcount);
1816 if (clp->cl_cb_state == NFSD4_CB_DOWN)
1817 seq->status_flags |= SEQ4_STATUS_CB_PATH_DOWN;
1820 spin_unlock(&client_lock);
1821 dprintk("%s: return %d\n", __func__, ntohl(status));
1826 nfsd4_reclaim_complete(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate, struct nfsd4_reclaim_complete *rc)
1830 if (rc->rca_one_fs) {
1831 if (!cstate->current_fh.fh_dentry)
1832 return nfserr_nofilehandle;
1834 * We don't take advantage of the rca_one_fs case.
1835 * That's OK, it's optional, we can safely ignore it.
1841 status = nfserr_complete_already;
1842 if (cstate->session->se_client->cl_firststate)
1845 status = nfserr_stale_clientid;
1846 if (is_client_expired(cstate->session->se_client))
1848 * The following error isn't really legal.
1849 * But we only get here if the client just explicitly
1850 * destroyed the client. Surely it no longer cares what
1851 * error it gets back on an operation for the dead
1857 nfsd4_create_clid_dir(cstate->session->se_client);
1859 nfs4_unlock_state();
1864 nfsd4_setclientid(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
1865 struct nfsd4_setclientid *setclid)
1867 struct xdr_netobj clname = {
1868 .len = setclid->se_namelen,
1869 .data = setclid->se_name,
1871 nfs4_verifier clverifier = setclid->se_verf;
1872 unsigned int strhashval;
1873 struct nfs4_client *conf, *unconf, *new;
1875 char dname[HEXDIR_LEN];
1877 if (!check_name(clname))
1878 return nfserr_inval;
1880 status = nfs4_make_rec_clidname(dname, &clname);
1885 * XXX The Duplicate Request Cache (DRC) has been checked (??)
1886 * We get here on a DRC miss.
1889 strhashval = clientstr_hashval(dname);
1892 conf = find_confirmed_client_by_str(dname, strhashval);
1894 /* RFC 3530 14.2.33 CASE 0: */
1895 status = nfserr_clid_inuse;
1896 if (clp_used_exchangeid(conf))
1898 if (!same_creds(&conf->cl_cred, &rqstp->rq_cred)) {
1899 char addr_str[INET6_ADDRSTRLEN];
1900 rpc_ntop((struct sockaddr *) &conf->cl_addr, addr_str,
1902 dprintk("NFSD: setclientid: string in use by client "
1903 "at %s\n", addr_str);
1908 * section 14.2.33 of RFC 3530 (under the heading "IMPLEMENTATION")
1909 * has a description of SETCLIENTID request processing consisting
1910 * of 5 bullet points, labeled as CASE0 - CASE4 below.
1912 unconf = find_unconfirmed_client_by_str(dname, strhashval);
1913 status = nfserr_jukebox;
1916 * RFC 3530 14.2.33 CASE 4:
1917 * placed first, because it is the normal case
1920 expire_client(unconf);
1921 new = create_client(clname, dname, rqstp, &clverifier);
1925 } else if (same_verf(&conf->cl_verifier, &clverifier)) {
1927 * RFC 3530 14.2.33 CASE 1:
1928 * probable callback update
1931 /* Note this is removing unconfirmed {*x***},
1932 * which is stronger than RFC recommended {vxc**}.
1933 * This has the advantage that there is at most
1934 * one {*x***} in either list at any time.
1936 expire_client(unconf);
1938 new = create_client(clname, dname, rqstp, &clverifier);
1941 copy_clid(new, conf);
1942 } else if (!unconf) {
1944 * RFC 3530 14.2.33 CASE 2:
1945 * probable client reboot; state will be removed if
1948 new = create_client(clname, dname, rqstp, &clverifier);
1954 * RFC 3530 14.2.33 CASE 3:
1955 * probable client reboot; state will be removed if
1958 expire_client(unconf);
1959 new = create_client(clname, dname, rqstp, &clverifier);
1965 * XXX: we should probably set this at creation time, and check
1966 * for consistent minorversion use throughout:
1968 new->cl_minorversion = 0;
1969 gen_callback(new, setclid, rqstp);
1970 add_to_unconfirmed(new, strhashval);
1971 setclid->se_clientid.cl_boot = new->cl_clientid.cl_boot;
1972 setclid->se_clientid.cl_id = new->cl_clientid.cl_id;
1973 memcpy(setclid->se_confirm.data, new->cl_confirm.data, sizeof(setclid->se_confirm.data));
1976 nfs4_unlock_state();
1982 * Section 14.2.34 of RFC 3530 (under the heading "IMPLEMENTATION") has
1983 * a description of SETCLIENTID_CONFIRM request processing consisting of 4
1984 * bullets, labeled as CASE1 - CASE4 below.
1987 nfsd4_setclientid_confirm(struct svc_rqst *rqstp,
1988 struct nfsd4_compound_state *cstate,
1989 struct nfsd4_setclientid_confirm *setclientid_confirm)
1991 struct sockaddr *sa = svc_addr(rqstp);
1992 struct nfs4_client *conf, *unconf;
1993 nfs4_verifier confirm = setclientid_confirm->sc_confirm;
1994 clientid_t * clid = &setclientid_confirm->sc_clientid;
1997 if (STALE_CLIENTID(clid))
1998 return nfserr_stale_clientid;
2000 * XXX The Duplicate Request Cache (DRC) has been checked (??)
2001 * We get here on a DRC miss.
2006 conf = find_confirmed_client(clid);
2007 unconf = find_unconfirmed_client(clid);
2009 status = nfserr_clid_inuse;
2010 if (conf && !rpc_cmp_addr((struct sockaddr *) &conf->cl_addr, sa))
2012 if (unconf && !rpc_cmp_addr((struct sockaddr *) &unconf->cl_addr, sa))
2016 * section 14.2.34 of RFC 3530 has a description of
2017 * SETCLIENTID_CONFIRM request processing consisting
2018 * of 4 bullet points, labeled as CASE1 - CASE4 below.
2020 if (conf && unconf && same_verf(&confirm, &unconf->cl_confirm)) {
2022 * RFC 3530 14.2.34 CASE 1:
2025 if (!same_creds(&conf->cl_cred, &unconf->cl_cred))
2026 status = nfserr_clid_inuse;
2028 nfsd4_change_callback(conf, &unconf->cl_cb_conn);
2029 nfsd4_probe_callback(conf);
2030 expire_client(unconf);
2034 } else if (conf && !unconf) {
2036 * RFC 3530 14.2.34 CASE 2:
2037 * probable retransmitted request; play it safe and
2040 if (!same_creds(&conf->cl_cred, &rqstp->rq_cred))
2041 status = nfserr_clid_inuse;
2044 } else if (!conf && unconf
2045 && same_verf(&unconf->cl_confirm, &confirm)) {
2047 * RFC 3530 14.2.34 CASE 3:
2048 * Normal case; new or rebooted client:
2050 if (!same_creds(&unconf->cl_cred, &rqstp->rq_cred)) {
2051 status = nfserr_clid_inuse;
2054 clientstr_hashval(unconf->cl_recdir);
2055 conf = find_confirmed_client_by_str(unconf->cl_recdir,
2058 nfsd4_remove_clid_dir(conf);
2059 expire_client(conf);
2061 move_to_confirmed(unconf);
2063 nfsd4_probe_callback(conf);
2066 } else if ((!conf || (conf && !same_verf(&conf->cl_confirm, &confirm)))
2067 && (!unconf || (unconf && !same_verf(&unconf->cl_confirm,
2070 * RFC 3530 14.2.34 CASE 4:
2071 * Client probably hasn't noticed that we rebooted yet.
2073 status = nfserr_stale_clientid;
2075 /* check that we have hit one of the cases...*/
2076 status = nfserr_clid_inuse;
2079 nfs4_unlock_state();
2083 /* OPEN Share state helper functions */
2084 static inline struct nfs4_file *
2085 alloc_init_file(struct inode *ino)
2087 struct nfs4_file *fp;
2088 unsigned int hashval = file_hashval(ino);
2090 fp = kmem_cache_alloc(file_slab, GFP_KERNEL);
2092 atomic_set(&fp->fi_ref, 1);
2093 INIT_LIST_HEAD(&fp->fi_hash);
2094 INIT_LIST_HEAD(&fp->fi_stateids);
2095 INIT_LIST_HEAD(&fp->fi_delegations);
2096 fp->fi_inode = igrab(ino);
2097 fp->fi_id = current_fileid++;
2098 fp->fi_had_conflict = false;
2099 fp->fi_lease = NULL;
2100 memset(fp->fi_fds, 0, sizeof(fp->fi_fds));
2101 memset(fp->fi_access, 0, sizeof(fp->fi_access));
2102 spin_lock(&recall_lock);
2103 list_add(&fp->fi_hash, &file_hashtbl[hashval]);
2104 spin_unlock(&recall_lock);
2111 nfsd4_free_slab(struct kmem_cache **slab)
2115 kmem_cache_destroy(*slab);
2120 nfsd4_free_slabs(void)
2122 nfsd4_free_slab(&stateowner_slab);
2123 nfsd4_free_slab(&file_slab);
2124 nfsd4_free_slab(&stateid_slab);
2125 nfsd4_free_slab(&deleg_slab);
2129 nfsd4_init_slabs(void)
2131 stateowner_slab = kmem_cache_create("nfsd4_stateowners",
2132 sizeof(struct nfs4_stateowner), 0, 0, NULL);
2133 if (stateowner_slab == NULL)
2135 file_slab = kmem_cache_create("nfsd4_files",
2136 sizeof(struct nfs4_file), 0, 0, NULL);
2137 if (file_slab == NULL)
2139 stateid_slab = kmem_cache_create("nfsd4_stateids",
2140 sizeof(struct nfs4_stateid), 0, 0, NULL);
2141 if (stateid_slab == NULL)
2143 deleg_slab = kmem_cache_create("nfsd4_delegations",
2144 sizeof(struct nfs4_delegation), 0, 0, NULL);
2145 if (deleg_slab == NULL)
2150 dprintk("nfsd4: out of memory while initializing nfsv4\n");
2155 nfs4_free_stateowner(struct kref *kref)
2157 struct nfs4_stateowner *sop =
2158 container_of(kref, struct nfs4_stateowner, so_ref);
2159 kfree(sop->so_owner.data);
2160 kmem_cache_free(stateowner_slab, sop);
2163 static inline struct nfs4_stateowner *
2164 alloc_stateowner(struct xdr_netobj *owner)
2166 struct nfs4_stateowner *sop;
2168 if ((sop = kmem_cache_alloc(stateowner_slab, GFP_KERNEL))) {
2169 if ((sop->so_owner.data = kmalloc(owner->len, GFP_KERNEL))) {
2170 memcpy(sop->so_owner.data, owner->data, owner->len);
2171 sop->so_owner.len = owner->len;
2172 kref_init(&sop->so_ref);
2175 kmem_cache_free(stateowner_slab, sop);
2180 static struct nfs4_stateowner *
2181 alloc_init_open_stateowner(unsigned int strhashval, struct nfs4_client *clp, struct nfsd4_open *open) {
2182 struct nfs4_stateowner *sop;
2183 struct nfs4_replay *rp;
2184 unsigned int idhashval;
2186 if (!(sop = alloc_stateowner(&open->op_owner)))
2188 idhashval = ownerid_hashval(current_ownerid);
2189 INIT_LIST_HEAD(&sop->so_idhash);
2190 INIT_LIST_HEAD(&sop->so_strhash);
2191 INIT_LIST_HEAD(&sop->so_perclient);
2192 INIT_LIST_HEAD(&sop->so_stateids);
2193 INIT_LIST_HEAD(&sop->so_perstateid); /* not used */
2194 INIT_LIST_HEAD(&sop->so_close_lru);
2196 list_add(&sop->so_idhash, &ownerid_hashtbl[idhashval]);
2197 list_add(&sop->so_strhash, &ownerstr_hashtbl[strhashval]);
2198 list_add(&sop->so_perclient, &clp->cl_openowners);
2199 sop->so_is_open_owner = 1;
2200 sop->so_id = current_ownerid++;
2201 sop->so_client = clp;
2202 sop->so_seqid = open->op_seqid;
2203 sop->so_confirmed = 0;
2204 rp = &sop->so_replay;
2205 rp->rp_status = nfserr_serverfault;
2207 rp->rp_buf = rp->rp_ibuf;
2212 init_stateid(struct nfs4_stateid *stp, struct nfs4_file *fp, struct nfsd4_open *open) {
2213 struct nfs4_stateowner *sop = open->op_stateowner;
2214 unsigned int hashval = stateid_hashval(sop->so_id, fp->fi_id);
2216 INIT_LIST_HEAD(&stp->st_hash);
2217 INIT_LIST_HEAD(&stp->st_perstateowner);
2218 INIT_LIST_HEAD(&stp->st_lockowners);
2219 INIT_LIST_HEAD(&stp->st_perfile);
2220 list_add(&stp->st_hash, &stateid_hashtbl[hashval]);
2221 list_add(&stp->st_perstateowner, &sop->so_stateids);
2222 list_add(&stp->st_perfile, &fp->fi_stateids);
2223 stp->st_stateowner = sop;
2226 stp->st_stateid.si_boot = boot_time;
2227 stp->st_stateid.si_stateownerid = sop->so_id;
2228 stp->st_stateid.si_fileid = fp->fi_id;
2229 stp->st_stateid.si_generation = 0;
2230 stp->st_access_bmap = 0;
2231 stp->st_deny_bmap = 0;
2232 __set_bit(open->op_share_access & ~NFS4_SHARE_WANT_MASK,
2233 &stp->st_access_bmap);
2234 __set_bit(open->op_share_deny, &stp->st_deny_bmap);
2235 stp->st_openstp = NULL;
2239 move_to_close_lru(struct nfs4_stateowner *sop)
2241 dprintk("NFSD: move_to_close_lru nfs4_stateowner %p\n", sop);
2243 list_move_tail(&sop->so_close_lru, &close_lru);
2244 sop->so_time = get_seconds();
2248 same_owner_str(struct nfs4_stateowner *sop, struct xdr_netobj *owner,
2251 return (sop->so_owner.len == owner->len) &&
2252 0 == memcmp(sop->so_owner.data, owner->data, owner->len) &&
2253 (sop->so_client->cl_clientid.cl_id == clid->cl_id);
2256 static struct nfs4_stateowner *
2257 find_openstateowner_str(unsigned int hashval, struct nfsd4_open *open)
2259 struct nfs4_stateowner *so = NULL;
2261 list_for_each_entry(so, &ownerstr_hashtbl[hashval], so_strhash) {
2262 if (same_owner_str(so, &open->op_owner, &open->op_clientid))
2268 /* search file_hashtbl[] for file */
2269 static struct nfs4_file *
2270 find_file(struct inode *ino)
2272 unsigned int hashval = file_hashval(ino);
2273 struct nfs4_file *fp;
2275 spin_lock(&recall_lock);
2276 list_for_each_entry(fp, &file_hashtbl[hashval], fi_hash) {
2277 if (fp->fi_inode == ino) {
2279 spin_unlock(&recall_lock);
2283 spin_unlock(&recall_lock);
2287 static inline int access_valid(u32 x, u32 minorversion)
2289 if ((x & NFS4_SHARE_ACCESS_MASK) < NFS4_SHARE_ACCESS_READ)
2291 if ((x & NFS4_SHARE_ACCESS_MASK) > NFS4_SHARE_ACCESS_BOTH)
2293 x &= ~NFS4_SHARE_ACCESS_MASK;
2294 if (minorversion && x) {
2295 if ((x & NFS4_SHARE_WANT_MASK) > NFS4_SHARE_WANT_CANCEL)
2297 if ((x & NFS4_SHARE_WHEN_MASK) > NFS4_SHARE_PUSH_DELEG_WHEN_UNCONTENDED)
2299 x &= ~(NFS4_SHARE_WANT_MASK | NFS4_SHARE_WHEN_MASK);
2306 static inline int deny_valid(u32 x)
2308 /* Note: unlike access bits, deny bits may be zero. */
2309 return x <= NFS4_SHARE_DENY_BOTH;
2313 * Called to check deny when READ with all zero stateid or
2314 * WRITE with all zero or all one stateid
2317 nfs4_share_conflict(struct svc_fh *current_fh, unsigned int deny_type)
2319 struct inode *ino = current_fh->fh_dentry->d_inode;
2320 struct nfs4_file *fp;
2321 struct nfs4_stateid *stp;
2324 dprintk("NFSD: nfs4_share_conflict\n");
2326 fp = find_file(ino);
2329 ret = nfserr_locked;
2330 /* Search for conflicting share reservations */
2331 list_for_each_entry(stp, &fp->fi_stateids, st_perfile) {
2332 if (test_bit(deny_type, &stp->st_deny_bmap) ||
2333 test_bit(NFS4_SHARE_DENY_BOTH, &stp->st_deny_bmap))
2342 static void nfsd_break_one_deleg(struct nfs4_delegation *dp)
2344 /* We're assuming the state code never drops its reference
2345 * without first removing the lease. Since we're in this lease
2346 * callback (and since the lease code is serialized by the kernel
2347 * lock) we know the server hasn't removed the lease yet, we know
2348 * it's safe to take a reference: */
2349 atomic_inc(&dp->dl_count);
2351 list_add_tail(&dp->dl_recall_lru, &del_recall_lru);
2353 /* only place dl_time is set. protected by lock_flocks*/
2354 dp->dl_time = get_seconds();
2356 nfsd4_cb_recall(dp);
2359 /* Called from break_lease() with lock_flocks() held. */
2360 static void nfsd_break_deleg_cb(struct file_lock *fl)
2362 struct nfs4_file *fp = (struct nfs4_file *)fl->fl_owner;
2363 struct nfs4_delegation *dp;
2366 /* We assume break_lease is only called once per lease: */
2367 BUG_ON(fp->fi_had_conflict);
2369 * We don't want the locks code to timeout the lease for us;
2370 * we'll remove it ourself if a delegation isn't returned
2373 fl->fl_break_time = 0;
2375 spin_lock(&recall_lock);
2376 fp->fi_had_conflict = true;
2377 list_for_each_entry(dp, &fp->fi_delegations, dl_perfile)
2378 nfsd_break_one_deleg(dp);
2379 spin_unlock(&recall_lock);
2383 int nfsd_change_deleg_cb(struct file_lock **onlist, int arg)
2386 return lease_modify(onlist, arg);
2391 static const struct lock_manager_operations nfsd_lease_mng_ops = {
2392 .fl_break = nfsd_break_deleg_cb,
2393 .fl_change = nfsd_change_deleg_cb,
2398 nfsd4_process_open1(struct nfsd4_compound_state *cstate,
2399 struct nfsd4_open *open)
2401 clientid_t *clientid = &open->op_clientid;
2402 struct nfs4_client *clp = NULL;
2403 unsigned int strhashval;
2404 struct nfs4_stateowner *sop = NULL;
2406 if (!check_name(open->op_owner))
2407 return nfserr_inval;
2409 if (STALE_CLIENTID(&open->op_clientid))
2410 return nfserr_stale_clientid;
2412 strhashval = ownerstr_hashval(clientid->cl_id, open->op_owner);
2413 sop = find_openstateowner_str(strhashval, open);
2414 open->op_stateowner = sop;
2416 /* Make sure the client's lease hasn't expired. */
2417 clp = find_confirmed_client(clientid);
2419 return nfserr_expired;
2422 /* When sessions are used, skip open sequenceid processing */
2423 if (nfsd4_has_session(cstate))
2425 if (!sop->so_confirmed) {
2426 /* Replace unconfirmed owners without checking for replay. */
2427 clp = sop->so_client;
2428 release_openowner(sop);
2429 open->op_stateowner = NULL;
2432 if (open->op_seqid == sop->so_seqid - 1) {
2433 if (sop->so_replay.rp_buflen)
2434 return nfserr_replay_me;
2435 /* The original OPEN failed so spectacularly
2436 * that we don't even have replay data saved!
2437 * Therefore, we have no choice but to continue
2438 * processing this OPEN; presumably, we'll
2439 * fail again for the same reason.
2441 dprintk("nfsd4_process_open1: replay with no replay cache\n");
2444 if (open->op_seqid != sop->so_seqid)
2445 return nfserr_bad_seqid;
2447 if (open->op_stateowner == NULL) {
2448 sop = alloc_init_open_stateowner(strhashval, clp, open);
2450 return nfserr_jukebox;
2451 open->op_stateowner = sop;
2453 list_del_init(&sop->so_close_lru);
2454 renew_client(sop->so_client);
2458 static inline __be32
2459 nfs4_check_delegmode(struct nfs4_delegation *dp, int flags)
2461 if ((flags & WR_STATE) && (dp->dl_type == NFS4_OPEN_DELEGATE_READ))
2462 return nfserr_openmode;
2467 static struct nfs4_delegation *
2468 find_delegation_file(struct nfs4_file *fp, stateid_t *stid)
2470 struct nfs4_delegation *dp;
2472 spin_lock(&recall_lock);
2473 list_for_each_entry(dp, &fp->fi_delegations, dl_perfile)
2474 if (dp->dl_stateid.si_stateownerid == stid->si_stateownerid) {
2475 spin_unlock(&recall_lock);
2478 spin_unlock(&recall_lock);
2482 static int share_access_to_flags(u32 share_access)
2484 share_access &= ~NFS4_SHARE_WANT_MASK;
2486 return share_access == NFS4_SHARE_ACCESS_READ ? RD_STATE : WR_STATE;
2490 nfs4_check_deleg(struct nfs4_file *fp, struct nfsd4_open *open,
2491 struct nfs4_delegation **dp)
2494 __be32 status = nfserr_bad_stateid;
2496 *dp = find_delegation_file(fp, &open->op_delegate_stateid);
2499 flags = share_access_to_flags(open->op_share_access);
2500 status = nfs4_check_delegmode(*dp, flags);
2504 if (open->op_claim_type != NFS4_OPEN_CLAIM_DELEGATE_CUR)
2508 open->op_stateowner->so_confirmed = 1;
2513 nfs4_check_open(struct nfs4_file *fp, struct nfsd4_open *open, struct nfs4_stateid **stpp)
2515 struct nfs4_stateid *local;
2516 __be32 status = nfserr_share_denied;
2517 struct nfs4_stateowner *sop = open->op_stateowner;
2519 list_for_each_entry(local, &fp->fi_stateids, st_perfile) {
2520 /* ignore lock owners */
2521 if (local->st_stateowner->so_is_open_owner == 0)
2523 /* remember if we have seen this open owner */
2524 if (local->st_stateowner == sop)
2526 /* check for conflicting share reservations */
2527 if (!test_share(local, open))
2535 static inline struct nfs4_stateid *
2536 nfs4_alloc_stateid(void)
2538 return kmem_cache_alloc(stateid_slab, GFP_KERNEL);
2541 static inline int nfs4_access_to_access(u32 nfs4_access)
2545 if (nfs4_access & NFS4_SHARE_ACCESS_READ)
2546 flags |= NFSD_MAY_READ;
2547 if (nfs4_access & NFS4_SHARE_ACCESS_WRITE)
2548 flags |= NFSD_MAY_WRITE;
2552 static __be32 nfs4_get_vfs_file(struct svc_rqst *rqstp, struct nfs4_file *fp,
2553 struct svc_fh *cur_fh, struct nfsd4_open *open)
2556 int oflag = nfs4_access_to_omode(open->op_share_access);
2557 int access = nfs4_access_to_access(open->op_share_access);
2559 /* CLAIM_DELEGATE_CUR is used in response to a broken lease;
2560 * allowing it to break the lease and return EAGAIN leaves the
2561 * client unable to make progress in returning the delegation */
2562 if (open->op_claim_type == NFS4_OPEN_CLAIM_DELEGATE_CUR)
2563 access |= NFSD_MAY_NOT_BREAK_LEASE;
2565 if (!fp->fi_fds[oflag]) {
2566 status = nfsd_open(rqstp, cur_fh, S_IFREG, access,
2567 &fp->fi_fds[oflag]);
2571 nfs4_file_get_access(fp, oflag);
2577 nfs4_new_open(struct svc_rqst *rqstp, struct nfs4_stateid **stpp,
2578 struct nfs4_file *fp, struct svc_fh *cur_fh,
2579 struct nfsd4_open *open)
2581 struct nfs4_stateid *stp;
2584 stp = nfs4_alloc_stateid();
2586 return nfserr_jukebox;
2588 status = nfs4_get_vfs_file(rqstp, fp, cur_fh, open);
2590 kmem_cache_free(stateid_slab, stp);
2597 static inline __be32
2598 nfsd4_truncate(struct svc_rqst *rqstp, struct svc_fh *fh,
2599 struct nfsd4_open *open)
2601 struct iattr iattr = {
2602 .ia_valid = ATTR_SIZE,
2605 if (!open->op_truncate)
2607 if (!(open->op_share_access & NFS4_SHARE_ACCESS_WRITE))
2608 return nfserr_inval;
2609 return nfsd_setattr(rqstp, fh, &iattr, 0, (time_t)0);
2613 nfs4_upgrade_open(struct svc_rqst *rqstp, struct nfs4_file *fp, struct svc_fh *cur_fh, struct nfs4_stateid *stp, struct nfsd4_open *open)
2615 u32 op_share_access = open->op_share_access & ~NFS4_SHARE_WANT_MASK;
2619 new_access = !test_bit(op_share_access, &stp->st_access_bmap);
2621 status = nfs4_get_vfs_file(rqstp, fp, cur_fh, open);
2625 status = nfsd4_truncate(rqstp, cur_fh, open);
2628 int oflag = nfs4_access_to_omode(op_share_access);
2629 nfs4_file_put_access(fp, oflag);
2633 /* remember the open */
2634 __set_bit(op_share_access, &stp->st_access_bmap);
2635 __set_bit(open->op_share_deny, &stp->st_deny_bmap);
2642 nfs4_set_claim_prev(struct nfsd4_open *open)
2644 open->op_stateowner->so_confirmed = 1;
2645 open->op_stateowner->so_client->cl_firststate = 1;
2648 /* Should we give out recallable state?: */
2649 static bool nfsd4_cb_channel_good(struct nfs4_client *clp)
2651 if (clp->cl_cb_state == NFSD4_CB_UP)
2654 * In the sessions case, since we don't have to establish a
2655 * separate connection for callbacks, we assume it's OK
2656 * until we hear otherwise:
2658 return clp->cl_minorversion && clp->cl_cb_state == NFSD4_CB_UNKNOWN;
2661 static struct file_lock *nfs4_alloc_init_lease(struct nfs4_delegation *dp, int flag)
2663 struct file_lock *fl;
2665 fl = locks_alloc_lock();
2668 locks_init_lock(fl);
2669 fl->fl_lmops = &nfsd_lease_mng_ops;
2670 fl->fl_flags = FL_LEASE;
2671 fl->fl_type = flag == NFS4_OPEN_DELEGATE_READ? F_RDLCK: F_WRLCK;
2672 fl->fl_end = OFFSET_MAX;
2673 fl->fl_owner = (fl_owner_t)(dp->dl_file);
2674 fl->fl_pid = current->tgid;
2678 static int nfs4_setlease(struct nfs4_delegation *dp, int flag)
2680 struct nfs4_file *fp = dp->dl_file;
2681 struct file_lock *fl;
2684 fl = nfs4_alloc_init_lease(dp, flag);
2687 fl->fl_file = find_readable_file(fp);
2688 list_add(&dp->dl_perclnt, &dp->dl_client->cl_delegations);
2689 status = vfs_setlease(fl->fl_file, fl->fl_type, &fl);
2691 list_del_init(&dp->dl_perclnt);
2692 locks_free_lock(fl);
2696 fp->fi_deleg_file = fl->fl_file;
2697 get_file(fp->fi_deleg_file);
2698 atomic_set(&fp->fi_delegees, 1);
2699 list_add(&dp->dl_perfile, &fp->fi_delegations);
2703 static int nfs4_set_delegation(struct nfs4_delegation *dp, int flag)
2705 struct nfs4_file *fp = dp->dl_file;
2708 return nfs4_setlease(dp, flag);
2709 spin_lock(&recall_lock);
2710 if (fp->fi_had_conflict) {
2711 spin_unlock(&recall_lock);
2714 atomic_inc(&fp->fi_delegees);
2715 list_add(&dp->dl_perfile, &fp->fi_delegations);
2716 spin_unlock(&recall_lock);
2717 list_add(&dp->dl_perclnt, &dp->dl_client->cl_delegations);
2722 * Attempt to hand out a delegation.
2725 nfs4_open_delegation(struct svc_fh *fh, struct nfsd4_open *open, struct nfs4_stateid *stp)
2727 struct nfs4_delegation *dp;
2728 struct nfs4_stateowner *sop = stp->st_stateowner;
2730 int status, flag = 0;
2732 cb_up = nfsd4_cb_channel_good(sop->so_client);
2733 flag = NFS4_OPEN_DELEGATE_NONE;
2734 open->op_recall = 0;
2735 switch (open->op_claim_type) {
2736 case NFS4_OPEN_CLAIM_PREVIOUS:
2738 open->op_recall = 1;
2739 flag = open->op_delegate_type;
2740 if (flag == NFS4_OPEN_DELEGATE_NONE)
2743 case NFS4_OPEN_CLAIM_NULL:
2744 /* Let's not give out any delegations till everyone's
2745 * had the chance to reclaim theirs.... */
2746 if (locks_in_grace())
2748 if (!cb_up || !sop->so_confirmed)
2750 if (open->op_share_access & NFS4_SHARE_ACCESS_WRITE)
2751 flag = NFS4_OPEN_DELEGATE_WRITE;
2753 flag = NFS4_OPEN_DELEGATE_READ;
2759 dp = alloc_init_deleg(sop->so_client, stp, fh, flag);
2762 status = nfs4_set_delegation(dp, flag);
2766 memcpy(&open->op_delegate_stateid, &dp->dl_stateid, sizeof(dp->dl_stateid));
2768 dprintk("NFSD: delegation stateid=" STATEID_FMT "\n",
2769 STATEID_VAL(&dp->dl_stateid));
2771 if (open->op_claim_type == NFS4_OPEN_CLAIM_PREVIOUS
2772 && flag == NFS4_OPEN_DELEGATE_NONE
2773 && open->op_delegate_type != NFS4_OPEN_DELEGATE_NONE)
2774 dprintk("NFSD: WARNING: refusing delegation reclaim\n");
2775 open->op_delegate_type = flag;
2778 nfs4_put_delegation(dp);
2780 flag = NFS4_OPEN_DELEGATE_NONE;
2785 * called with nfs4_lock_state() held.
2788 nfsd4_process_open2(struct svc_rqst *rqstp, struct svc_fh *current_fh, struct nfsd4_open *open)
2790 struct nfsd4_compoundres *resp = rqstp->rq_resp;
2791 struct nfs4_file *fp = NULL;
2792 struct inode *ino = current_fh->fh_dentry->d_inode;
2793 struct nfs4_stateid *stp = NULL;
2794 struct nfs4_delegation *dp = NULL;
2797 status = nfserr_inval;
2798 if (!access_valid(open->op_share_access, resp->cstate.minorversion)
2799 || !deny_valid(open->op_share_deny))
2802 * Lookup file; if found, lookup stateid and check open request,
2803 * and check for delegations in the process of being recalled.
2804 * If not found, create the nfs4_file struct
2806 fp = find_file(ino);
2808 if ((status = nfs4_check_open(fp, open, &stp)))
2810 status = nfs4_check_deleg(fp, open, &dp);
2814 status = nfserr_bad_stateid;
2815 if (open->op_claim_type == NFS4_OPEN_CLAIM_DELEGATE_CUR)
2817 status = nfserr_jukebox;
2818 fp = alloc_init_file(ino);
2824 * OPEN the file, or upgrade an existing OPEN.
2825 * If truncate fails, the OPEN fails.
2828 /* Stateid was found, this is an OPEN upgrade */
2829 status = nfs4_upgrade_open(rqstp, fp, current_fh, stp, open);
2832 update_stateid(&stp->st_stateid);
2834 status = nfs4_new_open(rqstp, &stp, fp, current_fh, open);
2837 init_stateid(stp, fp, open);
2838 status = nfsd4_truncate(rqstp, current_fh, open);
2840 release_open_stateid(stp);
2843 if (nfsd4_has_session(&resp->cstate))
2844 update_stateid(&stp->st_stateid);
2846 memcpy(&open->op_stateid, &stp->st_stateid, sizeof(stateid_t));
2848 if (nfsd4_has_session(&resp->cstate))
2849 open->op_stateowner->so_confirmed = 1;
2852 * Attempt to hand out a delegation. No error return, because the
2853 * OPEN succeeds even if we fail.
2855 nfs4_open_delegation(current_fh, open, stp);
2859 dprintk("%s: stateid=" STATEID_FMT "\n", __func__,
2860 STATEID_VAL(&stp->st_stateid));
2864 if (status == 0 && open->op_claim_type == NFS4_OPEN_CLAIM_PREVIOUS)
2865 nfs4_set_claim_prev(open);
2867 * To finish the open response, we just need to set the rflags.
2869 open->op_rflags = NFS4_OPEN_RESULT_LOCKTYPE_POSIX;
2870 if (!open->op_stateowner->so_confirmed &&
2871 !nfsd4_has_session(&resp->cstate))
2872 open->op_rflags |= NFS4_OPEN_RESULT_CONFIRM;
2878 nfsd4_renew(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
2881 struct nfs4_client *clp;
2885 dprintk("process_renew(%08x/%08x): starting\n",
2886 clid->cl_boot, clid->cl_id);
2887 status = nfserr_stale_clientid;
2888 if (STALE_CLIENTID(clid))
2890 clp = find_confirmed_client(clid);
2891 status = nfserr_expired;
2893 /* We assume the client took too long to RENEW. */
2894 dprintk("nfsd4_renew: clientid not found!\n");
2898 status = nfserr_cb_path_down;
2899 if (!list_empty(&clp->cl_delegations)
2900 && clp->cl_cb_state != NFSD4_CB_UP)
2904 nfs4_unlock_state();
2908 static struct lock_manager nfsd4_manager = {
2912 nfsd4_end_grace(void)
2914 dprintk("NFSD: end of grace period\n");
2915 nfsd4_recdir_purge_old();
2916 locks_end_grace(&nfsd4_manager);
2918 * Now that every NFSv4 client has had the chance to recover and
2919 * to see the (possibly new, possibly shorter) lease time, we
2920 * can safely set the next grace time to the current lease time:
2922 nfsd4_grace = nfsd4_lease;
2926 nfs4_laundromat(void)
2928 struct nfs4_client *clp;
2929 struct nfs4_stateowner *sop;
2930 struct nfs4_delegation *dp;
2931 struct list_head *pos, *next, reaplist;
2932 time_t cutoff = get_seconds() - nfsd4_lease;
2933 time_t t, clientid_val = nfsd4_lease;
2934 time_t u, test_val = nfsd4_lease;
2938 dprintk("NFSD: laundromat service - starting\n");
2939 if (locks_in_grace())
2941 INIT_LIST_HEAD(&reaplist);
2942 spin_lock(&client_lock);
2943 list_for_each_safe(pos, next, &client_lru) {
2944 clp = list_entry(pos, struct nfs4_client, cl_lru);
2945 if (time_after((unsigned long)clp->cl_time, (unsigned long)cutoff)) {
2946 t = clp->cl_time - cutoff;
2947 if (clientid_val > t)
2951 if (atomic_read(&clp->cl_refcount)) {
2952 dprintk("NFSD: client in use (clientid %08x)\n",
2953 clp->cl_clientid.cl_id);
2956 unhash_client_locked(clp);
2957 list_add(&clp->cl_lru, &reaplist);
2959 spin_unlock(&client_lock);
2960 list_for_each_safe(pos, next, &reaplist) {
2961 clp = list_entry(pos, struct nfs4_client, cl_lru);
2962 dprintk("NFSD: purging unused client (clientid %08x)\n",
2963 clp->cl_clientid.cl_id);
2964 nfsd4_remove_clid_dir(clp);
2967 spin_lock(&recall_lock);
2968 list_for_each_safe(pos, next, &del_recall_lru) {
2969 dp = list_entry (pos, struct nfs4_delegation, dl_recall_lru);
2970 if (time_after((unsigned long)dp->dl_time, (unsigned long)cutoff)) {
2971 u = dp->dl_time - cutoff;
2976 list_move(&dp->dl_recall_lru, &reaplist);
2978 spin_unlock(&recall_lock);
2979 list_for_each_safe(pos, next, &reaplist) {
2980 dp = list_entry (pos, struct nfs4_delegation, dl_recall_lru);
2981 list_del_init(&dp->dl_recall_lru);
2982 unhash_delegation(dp);
2984 test_val = nfsd4_lease;
2985 list_for_each_safe(pos, next, &close_lru) {
2986 sop = list_entry(pos, struct nfs4_stateowner, so_close_lru);
2987 if (time_after((unsigned long)sop->so_time, (unsigned long)cutoff)) {
2988 u = sop->so_time - cutoff;
2993 dprintk("NFSD: purging unused open stateowner (so_id %d)\n",
2995 release_openowner(sop);
2997 if (clientid_val < NFSD_LAUNDROMAT_MINTIMEOUT)
2998 clientid_val = NFSD_LAUNDROMAT_MINTIMEOUT;
2999 nfs4_unlock_state();
3000 return clientid_val;
3003 static struct workqueue_struct *laundry_wq;
3004 static void laundromat_main(struct work_struct *);
3005 static DECLARE_DELAYED_WORK(laundromat_work, laundromat_main);
3008 laundromat_main(struct work_struct *not_used)
3012 t = nfs4_laundromat();
3013 dprintk("NFSD: laundromat_main - sleeping for %ld seconds\n", t);
3014 queue_delayed_work(laundry_wq, &laundromat_work, t*HZ);
3017 static struct nfs4_stateowner *
3018 search_close_lru(u32 st_id, int flags)
3020 struct nfs4_stateowner *local = NULL;
3022 if (flags & CLOSE_STATE) {
3023 list_for_each_entry(local, &close_lru, so_close_lru) {
3024 if (local->so_id == st_id)
3032 nfs4_check_fh(struct svc_fh *fhp, struct nfs4_stateid *stp)
3034 return fhp->fh_dentry->d_inode != stp->st_file->fi_inode;
3038 STALE_STATEID(stateid_t *stateid)
3040 if (stateid->si_boot == boot_time)
3042 dprintk("NFSD: stale stateid " STATEID_FMT "!\n",
3043 STATEID_VAL(stateid));
3048 access_permit_read(unsigned long access_bmap)
3050 return test_bit(NFS4_SHARE_ACCESS_READ, &access_bmap) ||
3051 test_bit(NFS4_SHARE_ACCESS_BOTH, &access_bmap) ||
3052 test_bit(NFS4_SHARE_ACCESS_WRITE, &access_bmap);
3056 access_permit_write(unsigned long access_bmap)
3058 return test_bit(NFS4_SHARE_ACCESS_WRITE, &access_bmap) ||
3059 test_bit(NFS4_SHARE_ACCESS_BOTH, &access_bmap);
3063 __be32 nfs4_check_openmode(struct nfs4_stateid *stp, int flags)
3065 __be32 status = nfserr_openmode;
3067 /* For lock stateid's, we test the parent open, not the lock: */
3068 if (stp->st_openstp)
3069 stp = stp->st_openstp;
3070 if ((flags & WR_STATE) && (!access_permit_write(stp->st_access_bmap)))
3072 if ((flags & RD_STATE) && (!access_permit_read(stp->st_access_bmap)))
3079 static inline __be32
3080 check_special_stateids(svc_fh *current_fh, stateid_t *stateid, int flags)
3082 if (ONE_STATEID(stateid) && (flags & RD_STATE))
3084 else if (locks_in_grace()) {
3085 /* Answer in remaining cases depends on existence of
3086 * conflicting state; so we must wait out the grace period. */
3087 return nfserr_grace;
3088 } else if (flags & WR_STATE)
3089 return nfs4_share_conflict(current_fh,
3090 NFS4_SHARE_DENY_WRITE);
3091 else /* (flags & RD_STATE) && ZERO_STATEID(stateid) */
3092 return nfs4_share_conflict(current_fh,
3093 NFS4_SHARE_DENY_READ);
3097 * Allow READ/WRITE during grace period on recovered state only for files
3098 * that are not able to provide mandatory locking.
3101 grace_disallows_io(struct inode *inode)
3103 return locks_in_grace() && mandatory_lock(inode);
3106 static int check_stateid_generation(stateid_t *in, stateid_t *ref, int flags)
3109 * When sessions are used the stateid generation number is ignored
3112 if ((flags & HAS_SESSION) && in->si_generation == 0)
3115 /* If the client sends us a stateid from the future, it's buggy: */
3116 if (in->si_generation > ref->si_generation)
3117 return nfserr_bad_stateid;
3119 * The following, however, can happen. For example, if the
3120 * client sends an open and some IO at the same time, the open
3121 * may bump si_generation while the IO is still in flight.
3122 * Thanks to hard links and renames, the client never knows what
3123 * file an open will affect. So it could avoid that situation
3124 * only by serializing all opens and IO from the same open
3125 * owner. To recover from the old_stateid error, the client
3126 * will just have to retry the IO:
3128 if (in->si_generation < ref->si_generation)
3129 return nfserr_old_stateid;
3134 static int is_delegation_stateid(stateid_t *stateid)
3136 return stateid->si_fileid == 0;
3140 * Checks for stateid operations
3143 nfs4_preprocess_stateid_op(struct nfsd4_compound_state *cstate,
3144 stateid_t *stateid, int flags, struct file **filpp)
3146 struct nfs4_stateid *stp = NULL;
3147 struct nfs4_delegation *dp = NULL;
3148 struct svc_fh *current_fh = &cstate->current_fh;
3149 struct inode *ino = current_fh->fh_dentry->d_inode;
3155 if (grace_disallows_io(ino))
3156 return nfserr_grace;
3158 if (nfsd4_has_session(cstate))
3159 flags |= HAS_SESSION;
3161 if (ZERO_STATEID(stateid) || ONE_STATEID(stateid))
3162 return check_special_stateids(current_fh, stateid, flags);
3164 status = nfserr_stale_stateid;
3165 if (STALE_STATEID(stateid))
3169 * We assume that any stateid that has the current boot time,
3170 * but that we can't find, is expired:
3172 status = nfserr_expired;
3173 if (is_delegation_stateid(stateid)) {
3174 dp = find_delegation_stateid(ino, stateid);
3177 status = check_stateid_generation(stateid, &dp->dl_stateid,
3181 status = nfs4_check_delegmode(dp, flags);
3184 renew_client(dp->dl_client);
3186 *filpp = dp->dl_file->fi_deleg_file;
3189 } else { /* open or lock stateid */
3190 stp = find_stateid(stateid, flags);
3193 status = nfserr_bad_stateid;
3194 if (nfs4_check_fh(current_fh, stp))
3196 if (!stp->st_stateowner->so_confirmed)
3198 status = check_stateid_generation(stateid, &stp->st_stateid,
3202 status = nfs4_check_openmode(stp, flags);
3205 renew_client(stp->st_stateowner->so_client);
3207 if (flags & RD_STATE)
3208 *filpp = find_readable_file(stp->st_file);
3210 *filpp = find_writeable_file(stp->st_file);
3221 return (type == NFS4_READW_LT || type == NFS4_READ_LT) ?
3222 RD_STATE : WR_STATE;
3226 * Checks for sequence id mutating operations.
3229 nfs4_preprocess_seqid_op(struct nfsd4_compound_state *cstate, u32 seqid,
3230 stateid_t *stateid, int flags,
3231 struct nfs4_stateowner **sopp,
3232 struct nfs4_stateid **stpp, struct nfsd4_lock *lock)
3234 struct nfs4_stateid *stp;
3235 struct nfs4_stateowner *sop;
3236 struct svc_fh *current_fh = &cstate->current_fh;
3239 dprintk("NFSD: %s: seqid=%d stateid = " STATEID_FMT "\n", __func__,
3240 seqid, STATEID_VAL(stateid));
3245 if (ZERO_STATEID(stateid) || ONE_STATEID(stateid)) {
3246 dprintk("NFSD: preprocess_seqid_op: magic stateid!\n");
3247 return nfserr_bad_stateid;
3250 if (STALE_STATEID(stateid))
3251 return nfserr_stale_stateid;
3253 if (nfsd4_has_session(cstate))
3254 flags |= HAS_SESSION;
3257 * We return BAD_STATEID if filehandle doesn't match stateid,
3258 * the confirmed flag is incorrecly set, or the generation
3259 * number is incorrect.
3261 stp = find_stateid(stateid, flags);
3264 * Also, we should make sure this isn't just the result of
3267 sop = search_close_lru(stateid->si_stateownerid, flags);
3268 /* It's not stale; let's assume it's expired: */
3270 return nfserr_expired;
3276 *sopp = sop = stp->st_stateowner;
3279 clientid_t *lockclid = &lock->v.new.clientid;
3280 struct nfs4_client *clp = sop->so_client;
3284 lkflg = setlkflg(lock->lk_type);
3286 if (lock->lk_is_new) {
3287 if (!sop->so_is_open_owner)
3288 return nfserr_bad_stateid;
3289 if (!(flags & HAS_SESSION) &&
3290 !same_clid(&clp->cl_clientid, lockclid))
3291 return nfserr_bad_stateid;
3292 /* stp is the open stateid */
3293 status = nfs4_check_openmode(stp, lkflg);
3297 /* stp is the lock stateid */
3298 status = nfs4_check_openmode(stp->st_openstp, lkflg);
3304 if (nfs4_check_fh(current_fh, stp)) {
3305 dprintk("NFSD: preprocess_seqid_op: fh-stateid mismatch!\n");
3306 return nfserr_bad_stateid;
3310 * We now validate the seqid and stateid generation numbers.
3311 * For the moment, we ignore the possibility of
3312 * generation number wraparound.
3314 if (!(flags & HAS_SESSION) && seqid != sop->so_seqid)
3317 if (sop->so_confirmed && flags & CONFIRM) {
3318 dprintk("NFSD: preprocess_seqid_op: expected"
3319 " unconfirmed stateowner!\n");
3320 return nfserr_bad_stateid;
3322 if (!sop->so_confirmed && !(flags & CONFIRM)) {
3323 dprintk("NFSD: preprocess_seqid_op: stateowner not"
3324 " confirmed yet!\n");
3325 return nfserr_bad_stateid;
3327 status = check_stateid_generation(stateid, &stp->st_stateid, flags);
3330 renew_client(sop->so_client);
3334 if (seqid == sop->so_seqid - 1) {
3335 dprintk("NFSD: preprocess_seqid_op: retransmission?\n");
3336 /* indicate replay to calling function */
3337 return nfserr_replay_me;
3339 dprintk("NFSD: preprocess_seqid_op: bad seqid (expected %d, got %d)\n",
3340 sop->so_seqid, seqid);
3342 return nfserr_bad_seqid;
3346 nfsd4_open_confirm(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
3347 struct nfsd4_open_confirm *oc)
3350 struct nfs4_stateowner *sop;
3351 struct nfs4_stateid *stp;
3353 dprintk("NFSD: nfsd4_open_confirm on file %.*s\n",
3354 (int)cstate->current_fh.fh_dentry->d_name.len,
3355 cstate->current_fh.fh_dentry->d_name.name);
3357 status = fh_verify(rqstp, &cstate->current_fh, S_IFREG, 0);
3363 if ((status = nfs4_preprocess_seqid_op(cstate,
3364 oc->oc_seqid, &oc->oc_req_stateid,
3365 CONFIRM | OPEN_STATE,
3366 &oc->oc_stateowner, &stp, NULL)))
3369 sop = oc->oc_stateowner;
3370 sop->so_confirmed = 1;
3371 update_stateid(&stp->st_stateid);
3372 memcpy(&oc->oc_resp_stateid, &stp->st_stateid, sizeof(stateid_t));
3373 dprintk("NFSD: %s: success, seqid=%d stateid=" STATEID_FMT "\n",
3374 __func__, oc->oc_seqid, STATEID_VAL(&stp->st_stateid));
3376 nfsd4_create_clid_dir(sop->so_client);
3378 if (oc->oc_stateowner) {
3379 nfs4_get_stateowner(oc->oc_stateowner);
3380 cstate->replay_owner = oc->oc_stateowner;
3382 nfs4_unlock_state();
3386 static inline void nfs4_file_downgrade(struct nfs4_stateid *stp, unsigned int to_access)
3390 for (i = 1; i < 4; i++) {
3391 if (test_bit(i, &stp->st_access_bmap)
3392 && ((i & to_access) != i)) {
3393 nfs4_file_put_access(stp->st_file, nfs4_access_to_omode(i));
3394 __clear_bit(i, &stp->st_access_bmap);
3400 reset_union_bmap_deny(unsigned long deny, unsigned long *bmap)
3403 for (i = 0; i < 4; i++) {
3404 if ((i & deny) != i)
3405 __clear_bit(i, bmap);
3410 nfsd4_open_downgrade(struct svc_rqst *rqstp,
3411 struct nfsd4_compound_state *cstate,
3412 struct nfsd4_open_downgrade *od)
3415 struct nfs4_stateid *stp;
3417 dprintk("NFSD: nfsd4_open_downgrade on file %.*s\n",
3418 (int)cstate->current_fh.fh_dentry->d_name.len,
3419 cstate->current_fh.fh_dentry->d_name.name);
3421 if (!access_valid(od->od_share_access, cstate->minorversion)
3422 || !deny_valid(od->od_share_deny))
3423 return nfserr_inval;
3426 if ((status = nfs4_preprocess_seqid_op(cstate,
3430 &od->od_stateowner, &stp, NULL)))
3433 status = nfserr_inval;
3434 if (!test_bit(od->od_share_access, &stp->st_access_bmap)) {
3435 dprintk("NFSD:access not a subset current bitmap: 0x%lx, input access=%08x\n",
3436 stp->st_access_bmap, od->od_share_access);
3439 if (!test_bit(od->od_share_deny, &stp->st_deny_bmap)) {
3440 dprintk("NFSD:deny not a subset current bitmap: 0x%lx, input deny=%08x\n",
3441 stp->st_deny_bmap, od->od_share_deny);
3444 nfs4_file_downgrade(stp, od->od_share_access);
3446 reset_union_bmap_deny(od->od_share_deny, &stp->st_deny_bmap);
3448 update_stateid(&stp->st_stateid);
3449 memcpy(&od->od_stateid, &stp->st_stateid, sizeof(stateid_t));
3452 if (od->od_stateowner) {
3453 nfs4_get_stateowner(od->od_stateowner);
3454 cstate->replay_owner = od->od_stateowner;
3456 nfs4_unlock_state();
3461 * nfs4_unlock_state() called after encode
3464 nfsd4_close(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
3465 struct nfsd4_close *close)
3468 struct nfs4_stateid *stp;
3470 dprintk("NFSD: nfsd4_close on file %.*s\n",
3471 (int)cstate->current_fh.fh_dentry->d_name.len,
3472 cstate->current_fh.fh_dentry->d_name.name);
3475 /* check close_lru for replay */
3476 if ((status = nfs4_preprocess_seqid_op(cstate,
3479 OPEN_STATE | CLOSE_STATE,
3480 &close->cl_stateowner, &stp, NULL)))
3483 update_stateid(&stp->st_stateid);
3484 memcpy(&close->cl_stateid, &stp->st_stateid, sizeof(stateid_t));
3486 /* release_stateid() calls nfsd_close() if needed */
3487 release_open_stateid(stp);
3489 /* place unused nfs4_stateowners on so_close_lru list to be
3490 * released by the laundromat service after the lease period
3491 * to enable us to handle CLOSE replay
3493 if (list_empty(&close->cl_stateowner->so_stateids))
3494 move_to_close_lru(close->cl_stateowner);
3496 if (close->cl_stateowner) {
3497 nfs4_get_stateowner(close->cl_stateowner);
3498 cstate->replay_owner = close->cl_stateowner;
3500 nfs4_unlock_state();
3505 nfsd4_delegreturn(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
3506 struct nfsd4_delegreturn *dr)
3508 struct nfs4_delegation *dp;
3509 stateid_t *stateid = &dr->dr_stateid;
3510 struct inode *inode;
3514 if ((status = fh_verify(rqstp, &cstate->current_fh, S_IFREG, 0)))
3516 inode = cstate->current_fh.fh_dentry->d_inode;
3518 if (nfsd4_has_session(cstate))
3519 flags |= HAS_SESSION;
3521 status = nfserr_bad_stateid;
3522 if (ZERO_STATEID(stateid) || ONE_STATEID(stateid))
3524 status = nfserr_stale_stateid;
3525 if (STALE_STATEID(stateid))
3527 status = nfserr_bad_stateid;
3528 if (!is_delegation_stateid(stateid))
3530 status = nfserr_expired;
3531 dp = find_delegation_stateid(inode, stateid);
3534 status = check_stateid_generation(stateid, &dp->dl_stateid, flags);
3537 renew_client(dp->dl_client);
3539 unhash_delegation(dp);
3541 nfs4_unlock_state();
3548 * Lock owner state (byte-range locks)
3550 #define LOFF_OVERFLOW(start, len) ((u64)(len) > ~(u64)(start))
3551 #define LOCK_HASH_BITS 8
3552 #define LOCK_HASH_SIZE (1 << LOCK_HASH_BITS)
3553 #define LOCK_HASH_MASK (LOCK_HASH_SIZE - 1)
3556 end_offset(u64 start, u64 len)
3561 return end >= start ? end: NFS4_MAX_UINT64;
3564 /* last octet in a range */
3566 last_byte_offset(u64 start, u64 len)
3572 return end > start ? end - 1: NFS4_MAX_UINT64;
3575 #define lockownerid_hashval(id) \
3576 ((id) & LOCK_HASH_MASK)
3578 static inline unsigned int
3579 lock_ownerstr_hashval(struct inode *inode, u32 cl_id,
3580 struct xdr_netobj *ownername)
3582 return (file_hashval(inode) + cl_id
3583 + opaque_hashval(ownername->data, ownername->len))
3587 static struct list_head lock_ownerid_hashtbl[LOCK_HASH_SIZE];
3588 static struct list_head lock_ownerstr_hashtbl[LOCK_HASH_SIZE];
3589 static struct list_head lockstateid_hashtbl[STATEID_HASH_SIZE];
3591 static struct nfs4_stateid *
3592 find_stateid(stateid_t *stid, int flags)
3594 struct nfs4_stateid *local;
3595 u32 st_id = stid->si_stateownerid;
3596 u32 f_id = stid->si_fileid;
3597 unsigned int hashval;
3599 dprintk("NFSD: find_stateid flags 0x%x\n",flags);
3600 if (flags & (LOCK_STATE | RD_STATE | WR_STATE)) {
3601 hashval = stateid_hashval(st_id, f_id);
3602 list_for_each_entry(local, &lockstateid_hashtbl[hashval], st_hash) {
3603 if ((local->st_stateid.si_stateownerid == st_id) &&
3604 (local->st_stateid.si_fileid == f_id))
3609 if (flags & (OPEN_STATE | RD_STATE | WR_STATE)) {
3610 hashval = stateid_hashval(st_id, f_id);
3611 list_for_each_entry(local, &stateid_hashtbl[hashval], st_hash) {
3612 if ((local->st_stateid.si_stateownerid == st_id) &&
3613 (local->st_stateid.si_fileid == f_id))
3620 static struct nfs4_delegation *
3621 find_delegation_stateid(struct inode *ino, stateid_t *stid)
3623 struct nfs4_file *fp;
3624 struct nfs4_delegation *dl;
3626 dprintk("NFSD: %s: stateid=" STATEID_FMT "\n", __func__,
3629 fp = find_file(ino);
3632 dl = find_delegation_file(fp, stid);
3638 * TODO: Linux file offsets are _signed_ 64-bit quantities, which means that
3639 * we can't properly handle lock requests that go beyond the (2^63 - 1)-th
3640 * byte, because of sign extension problems. Since NFSv4 calls for 64-bit
3641 * locking, this prevents us from being completely protocol-compliant. The
3642 * real solution to this problem is to start using unsigned file offsets in
3643 * the VFS, but this is a very deep change!
3646 nfs4_transform_lock_offset(struct file_lock *lock)
3648 if (lock->fl_start < 0)
3649 lock->fl_start = OFFSET_MAX;
3650 if (lock->fl_end < 0)
3651 lock->fl_end = OFFSET_MAX;
3654 /* Hack!: For now, we're defining this just so we can use a pointer to it
3655 * as a unique cookie to identify our (NFSv4's) posix locks. */
3656 static const struct lock_manager_operations nfsd_posix_mng_ops = {
3660 nfs4_set_lock_denied(struct file_lock *fl, struct nfsd4_lock_denied *deny)
3662 struct nfs4_stateowner *sop;
3664 if (fl->fl_lmops == &nfsd_posix_mng_ops) {
3665 sop = (struct nfs4_stateowner *) fl->fl_owner;
3666 kref_get(&sop->so_ref);
3668 deny->ld_clientid = sop->so_client->cl_clientid;
3670 deny->ld_sop = NULL;
3671 deny->ld_clientid.cl_boot = 0;
3672 deny->ld_clientid.cl_id = 0;
3674 deny->ld_start = fl->fl_start;
3675 deny->ld_length = NFS4_MAX_UINT64;
3676 if (fl->fl_end != NFS4_MAX_UINT64)
3677 deny->ld_length = fl->fl_end - fl->fl_start + 1;
3678 deny->ld_type = NFS4_READ_LT;
3679 if (fl->fl_type != F_RDLCK)
3680 deny->ld_type = NFS4_WRITE_LT;
3683 static struct nfs4_stateowner *
3684 find_lockstateowner_str(struct inode *inode, clientid_t *clid,
3685 struct xdr_netobj *owner)
3687 unsigned int hashval = lock_ownerstr_hashval(inode, clid->cl_id, owner);
3688 struct nfs4_stateowner *op;
3690 list_for_each_entry(op, &lock_ownerstr_hashtbl[hashval], so_strhash) {
3691 if (same_owner_str(op, owner, clid))
3698 * Alloc a lock owner structure.
3699 * Called in nfsd4_lock - therefore, OPEN and OPEN_CONFIRM (if needed) has
3702 * strhashval = lock_ownerstr_hashval
3705 static struct nfs4_stateowner *
3706 alloc_init_lock_stateowner(unsigned int strhashval, struct nfs4_client *clp, struct nfs4_stateid *open_stp, struct nfsd4_lock *lock) {
3707 struct nfs4_stateowner *sop;
3708 struct nfs4_replay *rp;
3709 unsigned int idhashval;
3711 if (!(sop = alloc_stateowner(&lock->lk_new_owner)))
3713 idhashval = lockownerid_hashval(current_ownerid);
3714 INIT_LIST_HEAD(&sop->so_idhash);
3715 INIT_LIST_HEAD(&sop->so_strhash);
3716 INIT_LIST_HEAD(&sop->so_perclient);
3717 INIT_LIST_HEAD(&sop->so_stateids);
3718 INIT_LIST_HEAD(&sop->so_perstateid);
3719 INIT_LIST_HEAD(&sop->so_close_lru); /* not used */
3721 list_add(&sop->so_idhash, &lock_ownerid_hashtbl[idhashval]);
3722 list_add(&sop->so_strhash, &lock_ownerstr_hashtbl[strhashval]);
3723 list_add(&sop->so_perstateid, &open_stp->st_lockowners);
3724 sop->so_is_open_owner = 0;
3725 sop->so_id = current_ownerid++;
3726 sop->so_client = clp;
3727 /* It is the openowner seqid that will be incremented in encode in the
3728 * case of new lockowners; so increment the lock seqid manually: */
3729 sop->so_seqid = lock->lk_new_lock_seqid + 1;
3730 sop->so_confirmed = 1;
3731 rp = &sop->so_replay;
3732 rp->rp_status = nfserr_serverfault;
3734 rp->rp_buf = rp->rp_ibuf;
3738 static struct nfs4_stateid *
3739 alloc_init_lock_stateid(struct nfs4_stateowner *sop, struct nfs4_file *fp, struct nfs4_stateid *open_stp)
3741 struct nfs4_stateid *stp;
3742 unsigned int hashval = stateid_hashval(sop->so_id, fp->fi_id);
3744 stp = nfs4_alloc_stateid();
3747 INIT_LIST_HEAD(&stp->st_hash);
3748 INIT_LIST_HEAD(&stp->st_perfile);
3749 INIT_LIST_HEAD(&stp->st_perstateowner);
3750 INIT_LIST_HEAD(&stp->st_lockowners); /* not used */
3751 list_add(&stp->st_hash, &lockstateid_hashtbl[hashval]);
3752 list_add(&stp->st_perfile, &fp->fi_stateids);
3753 list_add(&stp->st_perstateowner, &sop->so_stateids);
3754 stp->st_stateowner = sop;
3757 stp->st_stateid.si_boot = boot_time;
3758 stp->st_stateid.si_stateownerid = sop->so_id;
3759 stp->st_stateid.si_fileid = fp->fi_id;
3760 stp->st_stateid.si_generation = 0;
3761 stp->st_access_bmap = 0;
3762 stp->st_deny_bmap = open_stp->st_deny_bmap;
3763 stp->st_openstp = open_stp;
3770 check_lock_length(u64 offset, u64 length)
3772 return ((length == 0) || ((length != NFS4_MAX_UINT64) &&
3773 LOFF_OVERFLOW(offset, length)));
3776 static void get_lock_access(struct nfs4_stateid *lock_stp, u32 access)
3778 struct nfs4_file *fp = lock_stp->st_file;
3779 int oflag = nfs4_access_to_omode(access);
3781 if (test_bit(access, &lock_stp->st_access_bmap))
3783 nfs4_file_get_access(fp, oflag);
3784 __set_bit(access, &lock_stp->st_access_bmap);
3791 nfsd4_lock(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
3792 struct nfsd4_lock *lock)
3794 struct nfs4_stateowner *open_sop = NULL;
3795 struct nfs4_stateowner *lock_sop = NULL;
3796 struct nfs4_stateid *lock_stp;
3797 struct nfs4_file *fp;
3798 struct file *filp = NULL;
3799 struct file_lock file_lock;
3800 struct file_lock conflock;
3802 unsigned int strhashval;
3805 dprintk("NFSD: nfsd4_lock: start=%Ld length=%Ld\n",
3806 (long long) lock->lk_offset,
3807 (long long) lock->lk_length);
3809 if (check_lock_length(lock->lk_offset, lock->lk_length))
3810 return nfserr_inval;
3812 if ((status = fh_verify(rqstp, &cstate->current_fh,
3813 S_IFREG, NFSD_MAY_LOCK))) {
3814 dprintk("NFSD: nfsd4_lock: permission denied!\n");
3820 if (lock->lk_is_new) {
3822 * Client indicates that this is a new lockowner.
3823 * Use open owner and open stateid to create lock owner and
3826 struct nfs4_stateid *open_stp = NULL;
3828 status = nfserr_stale_clientid;
3829 if (!nfsd4_has_session(cstate) &&
3830 STALE_CLIENTID(&lock->lk_new_clientid))
3833 /* validate and update open stateid and open seqid */
3834 status = nfs4_preprocess_seqid_op(cstate,
3835 lock->lk_new_open_seqid,
3836 &lock->lk_new_open_stateid,
3838 &lock->lk_replay_owner, &open_stp,
3842 open_sop = lock->lk_replay_owner;
3843 /* create lockowner and lock stateid */
3844 fp = open_stp->st_file;
3845 strhashval = lock_ownerstr_hashval(fp->fi_inode,
3846 open_sop->so_client->cl_clientid.cl_id,
3847 &lock->v.new.owner);
3848 /* XXX: Do we need to check for duplicate stateowners on
3849 * the same file, or should they just be allowed (and
3850 * create new stateids)? */
3851 status = nfserr_jukebox;
3852 lock_sop = alloc_init_lock_stateowner(strhashval,
3853 open_sop->so_client, open_stp, lock);
3854 if (lock_sop == NULL)
3856 lock_stp = alloc_init_lock_stateid(lock_sop, fp, open_stp);
3857 if (lock_stp == NULL)
3860 /* lock (lock owner + lock stateid) already exists */
3861 status = nfs4_preprocess_seqid_op(cstate,
3862 lock->lk_old_lock_seqid,
3863 &lock->lk_old_lock_stateid,
3865 &lock->lk_replay_owner, &lock_stp, lock);
3868 lock_sop = lock->lk_replay_owner;
3869 fp = lock_stp->st_file;
3871 /* lock->lk_replay_owner and lock_stp have been created or found */
3873 status = nfserr_grace;
3874 if (locks_in_grace() && !lock->lk_reclaim)
3876 status = nfserr_no_grace;
3877 if (!locks_in_grace() && lock->lk_reclaim)
3880 locks_init_lock(&file_lock);
3881 switch (lock->lk_type) {
3884 filp = find_readable_file(lock_stp->st_file);
3886 get_lock_access(lock_stp, NFS4_SHARE_ACCESS_READ);
3887 file_lock.fl_type = F_RDLCK;
3890 case NFS4_WRITEW_LT:
3891 filp = find_writeable_file(lock_stp->st_file);
3893 get_lock_access(lock_stp, NFS4_SHARE_ACCESS_WRITE);
3894 file_lock.fl_type = F_WRLCK;
3897 status = nfserr_inval;
3901 status = nfserr_openmode;
3904 file_lock.fl_owner = (fl_owner_t)lock_sop;
3905 file_lock.fl_pid = current->tgid;
3906 file_lock.fl_file = filp;
3907 file_lock.fl_flags = FL_POSIX;
3908 file_lock.fl_lmops = &nfsd_posix_mng_ops;
3910 file_lock.fl_start = lock->lk_offset;
3911 file_lock.fl_end = last_byte_offset(lock->lk_offset, lock->lk_length);
3912 nfs4_transform_lock_offset(&file_lock);
3915 * Try to lock the file in the VFS.
3916 * Note: locks.c uses the BKL to protect the inode's lock list.
3919 err = vfs_lock_file(filp, F_SETLK, &file_lock, &conflock);
3921 case 0: /* success! */
3922 update_stateid(&lock_stp->st_stateid);
3923 memcpy(&lock->lk_resp_stateid, &lock_stp->st_stateid,
3927 case (EAGAIN): /* conflock holds conflicting lock */
3928 status = nfserr_denied;
3929 dprintk("NFSD: nfsd4_lock: conflicting lock found!\n");
3930 nfs4_set_lock_denied(&conflock, &lock->lk_denied);
3933 status = nfserr_deadlock;
3936 dprintk("NFSD: nfsd4_lock: vfs_lock_file() failed! status %d\n",err);
3937 status = nfserrno(err);
3941 if (status && lock->lk_is_new && lock_sop)
3942 release_lockowner(lock_sop);
3943 if (lock->lk_replay_owner) {
3944 nfs4_get_stateowner(lock->lk_replay_owner);
3945 cstate->replay_owner = lock->lk_replay_owner;
3947 nfs4_unlock_state();
3952 * The NFSv4 spec allows a client to do a LOCKT without holding an OPEN,
3953 * so we do a temporary open here just to get an open file to pass to
3954 * vfs_test_lock. (Arguably perhaps test_lock should be done with an
3957 static int nfsd_test_lock(struct svc_rqst *rqstp, struct svc_fh *fhp, struct file_lock *lock)
3962 err = nfsd_open(rqstp, fhp, S_IFREG, NFSD_MAY_READ, &file);
3965 err = vfs_test_lock(file, lock);
3974 nfsd4_lockt(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
3975 struct nfsd4_lockt *lockt)
3977 struct inode *inode;
3978 struct file_lock file_lock;
3982 if (locks_in_grace())
3983 return nfserr_grace;
3985 if (check_lock_length(lockt->lt_offset, lockt->lt_length))
3986 return nfserr_inval;
3988 lockt->lt_stateowner = NULL;
3991 status = nfserr_stale_clientid;
3992 if (!nfsd4_has_session(cstate) && STALE_CLIENTID(&lockt->lt_clientid))
3995 if ((status = fh_verify(rqstp, &cstate->current_fh, S_IFREG, 0))) {
3996 dprintk("NFSD: nfsd4_lockt: fh_verify() failed!\n");
3997 if (status == nfserr_symlink)
3998 status = nfserr_inval;
4002 inode = cstate->current_fh.fh_dentry->d_inode;
4003 locks_init_lock(&file_lock);
4004 switch (lockt->lt_type) {
4007 file_lock.fl_type = F_RDLCK;
4010 case NFS4_WRITEW_LT:
4011 file_lock.fl_type = F_WRLCK;
4014 dprintk("NFSD: nfs4_lockt: bad lock type!\n");
4015 status = nfserr_inval;
4019 lockt->lt_stateowner = find_lockstateowner_str(inode,
4020 &lockt->lt_clientid, &lockt->lt_owner);
4021 if (lockt->lt_stateowner)
4022 file_lock.fl_owner = (fl_owner_t)lockt->lt_stateowner;
4023 file_lock.fl_pid = current->tgid;
4024 file_lock.fl_flags = FL_POSIX;
4026 file_lock.fl_start = lockt->lt_offset;
4027 file_lock.fl_end = last_byte_offset(lockt->lt_offset, lockt->lt_length);
4029 nfs4_transform_lock_offset(&file_lock);
4032 error = nfsd_test_lock(rqstp, &cstate->current_fh, &file_lock);
4034 status = nfserrno(error);
4037 if (file_lock.fl_type != F_UNLCK) {
4038 status = nfserr_denied;
4039 nfs4_set_lock_denied(&file_lock, &lockt->lt_denied);
4042 nfs4_unlock_state();
4047 nfsd4_locku(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
4048 struct nfsd4_locku *locku)
4050 struct nfs4_stateid *stp;
4051 struct file *filp = NULL;
4052 struct file_lock file_lock;
4056 dprintk("NFSD: nfsd4_locku: start=%Ld length=%Ld\n",
4057 (long long) locku->lu_offset,
4058 (long long) locku->lu_length);
4060 if (check_lock_length(locku->lu_offset, locku->lu_length))
4061 return nfserr_inval;
4065 if ((status = nfs4_preprocess_seqid_op(cstate,
4069 &locku->lu_stateowner, &stp, NULL)))
4072 filp = find_any_file(stp->st_file);
4074 status = nfserr_lock_range;
4078 locks_init_lock(&file_lock);
4079 file_lock.fl_type = F_UNLCK;
4080 file_lock.fl_owner = (fl_owner_t) locku->lu_stateowner;
4081 file_lock.fl_pid = current->tgid;
4082 file_lock.fl_file = filp;
4083 file_lock.fl_flags = FL_POSIX;
4084 file_lock.fl_lmops = &nfsd_posix_mng_ops;
4085 file_lock.fl_start = locku->lu_offset;
4087 file_lock.fl_end = last_byte_offset(locku->lu_offset, locku->lu_length);
4088 nfs4_transform_lock_offset(&file_lock);
4091 * Try to unlock the file in the VFS.
4093 err = vfs_lock_file(filp, F_SETLK, &file_lock, NULL);
4095 dprintk("NFSD: nfs4_locku: vfs_lock_file failed!\n");
4099 * OK, unlock succeeded; the only thing left to do is update the stateid.
4101 update_stateid(&stp->st_stateid);
4102 memcpy(&locku->lu_stateid, &stp->st_stateid, sizeof(stateid_t));
4105 if (locku->lu_stateowner) {
4106 nfs4_get_stateowner(locku->lu_stateowner);
4107 cstate->replay_owner = locku->lu_stateowner;
4109 nfs4_unlock_state();
4113 status = nfserrno(err);
4119 * 1: locks held by lockowner
4120 * 0: no locks held by lockowner
4123 check_for_locks(struct nfs4_file *filp, struct nfs4_stateowner *lowner)
4125 struct file_lock **flpp;
4126 struct inode *inode = filp->fi_inode;
4130 for (flpp = &inode->i_flock; *flpp != NULL; flpp = &(*flpp)->fl_next) {
4131 if ((*flpp)->fl_owner == (fl_owner_t)lowner) {
4142 nfsd4_release_lockowner(struct svc_rqst *rqstp,
4143 struct nfsd4_compound_state *cstate,
4144 struct nfsd4_release_lockowner *rlockowner)
4146 clientid_t *clid = &rlockowner->rl_clientid;
4147 struct nfs4_stateowner *sop;
4148 struct nfs4_stateid *stp;
4149 struct xdr_netobj *owner = &rlockowner->rl_owner;
4150 struct list_head matches;
4154 dprintk("nfsd4_release_lockowner clientid: (%08x/%08x):\n",
4155 clid->cl_boot, clid->cl_id);
4157 /* XXX check for lease expiration */
4159 status = nfserr_stale_clientid;
4160 if (STALE_CLIENTID(clid))
4165 status = nfserr_locks_held;
4166 /* XXX: we're doing a linear search through all the lockowners.
4167 * Yipes! For now we'll just hope clients aren't really using
4168 * release_lockowner much, but eventually we have to fix these
4169 * data structures. */
4170 INIT_LIST_HEAD(&matches);
4171 for (i = 0; i < LOCK_HASH_SIZE; i++) {
4172 list_for_each_entry(sop, &lock_ownerid_hashtbl[i], so_idhash) {
4173 if (!same_owner_str(sop, owner, clid))
4175 list_for_each_entry(stp, &sop->so_stateids,
4177 if (check_for_locks(stp->st_file, sop))
4179 /* Note: so_perclient unused for lockowners,
4180 * so it's OK to fool with here. */
4181 list_add(&sop->so_perclient, &matches);
4185 /* Clients probably won't expect us to return with some (but not all)
4186 * of the lockowner state released; so don't release any until all
4187 * have been checked. */
4189 while (!list_empty(&matches)) {
4190 sop = list_entry(matches.next, struct nfs4_stateowner,
4192 /* unhash_stateowner deletes so_perclient only
4193 * for openowners. */
4194 list_del(&sop->so_perclient);
4195 release_lockowner(sop);
4198 nfs4_unlock_state();
4202 static inline struct nfs4_client_reclaim *
4205 return kmalloc(sizeof(struct nfs4_client_reclaim), GFP_KERNEL);
4209 nfs4_has_reclaimed_state(const char *name, bool use_exchange_id)
4211 unsigned int strhashval = clientstr_hashval(name);
4212 struct nfs4_client *clp;
4214 clp = find_confirmed_client_by_str(name, strhashval);
4219 * failure => all reset bets are off, nfserr_no_grace...
4222 nfs4_client_to_reclaim(const char *name)
4224 unsigned int strhashval;
4225 struct nfs4_client_reclaim *crp = NULL;
4227 dprintk("NFSD nfs4_client_to_reclaim NAME: %.*s\n", HEXDIR_LEN, name);
4228 crp = alloc_reclaim();
4231 strhashval = clientstr_hashval(name);
4232 INIT_LIST_HEAD(&crp->cr_strhash);
4233 list_add(&crp->cr_strhash, &reclaim_str_hashtbl[strhashval]);
4234 memcpy(crp->cr_recdir, name, HEXDIR_LEN);
4235 reclaim_str_hashtbl_size++;
4240 nfs4_release_reclaim(void)
4242 struct nfs4_client_reclaim *crp = NULL;
4245 for (i = 0; i < CLIENT_HASH_SIZE; i++) {
4246 while (!list_empty(&reclaim_str_hashtbl[i])) {
4247 crp = list_entry(reclaim_str_hashtbl[i].next,
4248 struct nfs4_client_reclaim, cr_strhash);
4249 list_del(&crp->cr_strhash);
4251 reclaim_str_hashtbl_size--;
4254 BUG_ON(reclaim_str_hashtbl_size);
4258 * called from OPEN, CLAIM_PREVIOUS with a new clientid. */
4259 static struct nfs4_client_reclaim *
4260 nfs4_find_reclaim_client(clientid_t *clid)
4262 unsigned int strhashval;
4263 struct nfs4_client *clp;
4264 struct nfs4_client_reclaim *crp = NULL;
4267 /* find clientid in conf_id_hashtbl */
4268 clp = find_confirmed_client(clid);
4272 dprintk("NFSD: nfs4_find_reclaim_client for %.*s with recdir %s\n",
4273 clp->cl_name.len, clp->cl_name.data,
4276 /* find clp->cl_name in reclaim_str_hashtbl */
4277 strhashval = clientstr_hashval(clp->cl_recdir);
4278 list_for_each_entry(crp, &reclaim_str_hashtbl[strhashval], cr_strhash) {
4279 if (same_name(crp->cr_recdir, clp->cl_recdir)) {
4287 * Called from OPEN. Look for clientid in reclaim list.
4290 nfs4_check_open_reclaim(clientid_t *clid)
4292 return nfs4_find_reclaim_client(clid) ? nfs_ok : nfserr_reclaim_bad;
4295 /* initialization to perform at module load time: */
4298 nfs4_state_init(void)
4302 status = nfsd4_init_slabs();
4305 for (i = 0; i < CLIENT_HASH_SIZE; i++) {
4306 INIT_LIST_HEAD(&conf_id_hashtbl[i]);
4307 INIT_LIST_HEAD(&conf_str_hashtbl[i]);
4308 INIT_LIST_HEAD(&unconf_str_hashtbl[i]);
4309 INIT_LIST_HEAD(&unconf_id_hashtbl[i]);
4310 INIT_LIST_HEAD(&reclaim_str_hashtbl[i]);
4312 for (i = 0; i < SESSION_HASH_SIZE; i++)
4313 INIT_LIST_HEAD(&sessionid_hashtbl[i]);
4314 for (i = 0; i < FILE_HASH_SIZE; i++) {
4315 INIT_LIST_HEAD(&file_hashtbl[i]);
4317 for (i = 0; i < OWNER_HASH_SIZE; i++) {
4318 INIT_LIST_HEAD(&ownerstr_hashtbl[i]);
4319 INIT_LIST_HEAD(&ownerid_hashtbl[i]);
4321 for (i = 0; i < STATEID_HASH_SIZE; i++) {
4322 INIT_LIST_HEAD(&stateid_hashtbl[i]);
4323 INIT_LIST_HEAD(&lockstateid_hashtbl[i]);
4325 for (i = 0; i < LOCK_HASH_SIZE; i++) {
4326 INIT_LIST_HEAD(&lock_ownerid_hashtbl[i]);
4327 INIT_LIST_HEAD(&lock_ownerstr_hashtbl[i]);
4329 memset(&onestateid, ~0, sizeof(stateid_t));
4330 INIT_LIST_HEAD(&close_lru);
4331 INIT_LIST_HEAD(&client_lru);
4332 INIT_LIST_HEAD(&del_recall_lru);
4333 reclaim_str_hashtbl_size = 0;
4338 nfsd4_load_reboot_recovery_data(void)
4343 nfsd4_init_recdir(user_recovery_dirname);
4344 status = nfsd4_recdir_load();
4345 nfs4_unlock_state();
4347 printk("NFSD: Failure reading reboot recovery data\n");
4351 * Since the lifetime of a delegation isn't limited to that of an open, a
4352 * client may quite reasonably hang on to a delegation as long as it has
4353 * the inode cached. This becomes an obvious problem the first time a
4354 * client's inode cache approaches the size of the server's total memory.
4356 * For now we avoid this problem by imposing a hard limit on the number
4357 * of delegations, which varies according to the server's memory size.
4360 set_max_delegations(void)
4363 * Allow at most 4 delegations per megabyte of RAM. Quick
4364 * estimates suggest that in the worst case (where every delegation
4365 * is for a different inode), a delegation could take about 1.5K,
4366 * giving a worst case usage of about 6% of memory.
4368 max_delegations = nr_free_buffer_pages() >> (20 - 2 - PAGE_SHIFT);
4371 /* initialization to perform when the nfsd service is started: */
4374 __nfs4_state_start(void)
4378 boot_time = get_seconds();
4379 locks_start_grace(&nfsd4_manager);
4380 printk(KERN_INFO "NFSD: starting %ld-second grace period\n",
4382 ret = set_callback_cred();
4385 laundry_wq = create_singlethread_workqueue("nfsd4");
4386 if (laundry_wq == NULL)
4388 ret = nfsd4_create_callback_queue();
4390 goto out_free_laundry;
4391 queue_delayed_work(laundry_wq, &laundromat_work, nfsd4_grace * HZ);
4392 set_max_delegations();
4395 destroy_workqueue(laundry_wq);
4400 nfs4_state_start(void)
4402 nfsd4_load_reboot_recovery_data();
4403 return __nfs4_state_start();
4407 __nfs4_state_shutdown(void)
4410 struct nfs4_client *clp = NULL;
4411 struct nfs4_delegation *dp = NULL;
4412 struct list_head *pos, *next, reaplist;
4414 for (i = 0; i < CLIENT_HASH_SIZE; i++) {
4415 while (!list_empty(&conf_id_hashtbl[i])) {
4416 clp = list_entry(conf_id_hashtbl[i].next, struct nfs4_client, cl_idhash);
4419 while (!list_empty(&unconf_str_hashtbl[i])) {
4420 clp = list_entry(unconf_str_hashtbl[i].next, struct nfs4_client, cl_strhash);
4424 INIT_LIST_HEAD(&reaplist);
4425 spin_lock(&recall_lock);
4426 list_for_each_safe(pos, next, &del_recall_lru) {
4427 dp = list_entry (pos, struct nfs4_delegation, dl_recall_lru);
4428 list_move(&dp->dl_recall_lru, &reaplist);
4430 spin_unlock(&recall_lock);
4431 list_for_each_safe(pos, next, &reaplist) {
4432 dp = list_entry (pos, struct nfs4_delegation, dl_recall_lru);
4433 list_del_init(&dp->dl_recall_lru);
4434 unhash_delegation(dp);
4437 nfsd4_shutdown_recdir();
4441 nfs4_state_shutdown(void)
4443 cancel_delayed_work_sync(&laundromat_work);
4444 destroy_workqueue(laundry_wq);
4445 locks_end_grace(&nfsd4_manager);
4447 nfs4_release_reclaim();
4448 __nfs4_state_shutdown();
4449 nfs4_unlock_state();
4450 nfsd4_destroy_callback_queue();
4454 * user_recovery_dirname is protected by the nfsd_mutex since it's only
4455 * accessed when nfsd is starting.
4458 nfs4_set_recdir(char *recdir)
4460 strcpy(user_recovery_dirname, recdir);
4464 * Change the NFSv4 recovery directory to recdir.
4467 nfs4_reset_recoverydir(char *recdir)
4472 status = kern_path(recdir, LOOKUP_FOLLOW, &path);
4476 if (S_ISDIR(path.dentry->d_inode->i_mode)) {
4477 nfs4_set_recdir(recdir);
4485 nfs4_recoverydir(void)
4487 return user_recovery_dirname;