nfsd4: pass around typemask instead of flags
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / fs / nfsd / nfs4state.c
blob48134635fc2fc3988885d4eb46b41aa680b15aae
1 /*
2 * Copyright (c) 2001 The Regents of the University of Michigan.
3 * All rights reserved.
5 * Kendrick Smith <kmsmith@umich.edu>
6 * Andy Adamson <kandros@umich.edu>
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the distribution.
17 * 3. Neither the name of the University nor the names of its
18 * contributors may be used to endorse or promote products derived
19 * from this software without specific prior written permission.
21 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED
22 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
23 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
24 * DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
26 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
27 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
28 * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
29 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
30 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
31 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
35 #include <linux/file.h>
36 #include <linux/fs.h>
37 #include <linux/slab.h>
38 #include <linux/namei.h>
39 #include <linux/swap.h>
40 #include <linux/pagemap.h>
41 #include <linux/sunrpc/svcauth_gss.h>
42 #include <linux/sunrpc/clnt.h>
43 #include "xdr4.h"
44 #include "vfs.h"
46 #define NFSDDBG_FACILITY NFSDDBG_PROC
48 /* Globals */
49 time_t nfsd4_lease = 90; /* default lease time */
50 time_t nfsd4_grace = 90;
51 static time_t boot_time;
52 static u32 current_ownerid = 1;
53 static u32 current_fileid = 1;
54 static u32 current_delegid = 1;
55 static stateid_t zerostateid; /* bits all 0 */
56 static stateid_t onestateid; /* bits all 1 */
57 static u64 current_sessionid = 1;
59 #define ZERO_STATEID(stateid) (!memcmp((stateid), &zerostateid, sizeof(stateid_t)))
60 #define ONE_STATEID(stateid) (!memcmp((stateid), &onestateid, sizeof(stateid_t)))
62 /* forward declarations */
63 static struct nfs4_delegation * search_for_delegation(stateid_t *stid);
64 static struct nfs4_delegation * find_delegation_stateid(struct inode *ino, stateid_t *stid);
65 static int check_for_locks(struct nfs4_file *filp, struct nfs4_lockowner *lowner);
67 /* Locking: */
69 /* Currently used for almost all code touching nfsv4 state: */
70 static DEFINE_MUTEX(client_mutex);
73 * Currently used for the del_recall_lru and file hash table. In an
74 * effort to decrease the scope of the client_mutex, this spinlock may
75 * eventually cover more:
77 static DEFINE_SPINLOCK(recall_lock);
79 static struct kmem_cache *openowner_slab = NULL;
80 static struct kmem_cache *lockowner_slab = NULL;
81 static struct kmem_cache *file_slab = NULL;
82 static struct kmem_cache *stateid_slab = NULL;
83 static struct kmem_cache *deleg_slab = NULL;
85 void
86 nfs4_lock_state(void)
88 mutex_lock(&client_mutex);
91 void
92 nfs4_unlock_state(void)
94 mutex_unlock(&client_mutex);
97 static inline u32
98 opaque_hashval(const void *ptr, int nbytes)
100 unsigned char *cptr = (unsigned char *) ptr;
102 u32 x = 0;
103 while (nbytes--) {
104 x *= 37;
105 x += *cptr++;
107 return x;
110 static struct list_head del_recall_lru;
112 static inline void
113 put_nfs4_file(struct nfs4_file *fi)
115 if (atomic_dec_and_lock(&fi->fi_ref, &recall_lock)) {
116 list_del(&fi->fi_hash);
117 spin_unlock(&recall_lock);
118 iput(fi->fi_inode);
119 kmem_cache_free(file_slab, fi);
123 static inline void
124 get_nfs4_file(struct nfs4_file *fi)
126 atomic_inc(&fi->fi_ref);
129 static int num_delegations;
130 unsigned int max_delegations;
133 * Open owner state (share locks)
136 /* hash tables for open owners */
137 #define OPEN_OWNER_HASH_BITS 8
138 #define OPEN_OWNER_HASH_SIZE (1 << OPEN_OWNER_HASH_BITS)
139 #define OPEN_OWNER_HASH_MASK (OPEN_OWNER_HASH_SIZE - 1)
141 static unsigned int open_ownerid_hashval(const u32 id)
143 return id & OPEN_OWNER_HASH_MASK;
146 static unsigned int open_ownerstr_hashval(u32 clientid, struct xdr_netobj *ownername)
148 unsigned int ret;
150 ret = opaque_hashval(ownername->data, ownername->len);
151 ret += clientid;
152 return ret & OPEN_OWNER_HASH_MASK;
155 static struct list_head open_ownerid_hashtbl[OPEN_OWNER_HASH_SIZE];
156 static struct list_head open_ownerstr_hashtbl[OPEN_OWNER_HASH_SIZE];
158 /* hash table for nfs4_file */
159 #define FILE_HASH_BITS 8
160 #define FILE_HASH_SIZE (1 << FILE_HASH_BITS)
162 /* hash table for (open)nfs4_stateid */
163 #define STATEID_HASH_BITS 10
164 #define STATEID_HASH_SIZE (1 << STATEID_HASH_BITS)
165 #define STATEID_HASH_MASK (STATEID_HASH_SIZE - 1)
167 static unsigned int file_hashval(struct inode *ino)
169 /* XXX: why are we hashing on inode pointer, anyway? */
170 return hash_ptr(ino, FILE_HASH_BITS);
173 static unsigned int stateid_hashval(u32 owner_id, u32 file_id)
175 return (owner_id + file_id) & STATEID_HASH_MASK;
178 static struct list_head file_hashtbl[FILE_HASH_SIZE];
179 static struct list_head stateid_hashtbl[STATEID_HASH_SIZE];
181 static void __nfs4_file_get_access(struct nfs4_file *fp, int oflag)
183 BUG_ON(!(fp->fi_fds[oflag] || fp->fi_fds[O_RDWR]));
184 atomic_inc(&fp->fi_access[oflag]);
187 static void nfs4_file_get_access(struct nfs4_file *fp, int oflag)
189 if (oflag == O_RDWR) {
190 __nfs4_file_get_access(fp, O_RDONLY);
191 __nfs4_file_get_access(fp, O_WRONLY);
192 } else
193 __nfs4_file_get_access(fp, oflag);
196 static void nfs4_file_put_fd(struct nfs4_file *fp, int oflag)
198 if (fp->fi_fds[oflag]) {
199 fput(fp->fi_fds[oflag]);
200 fp->fi_fds[oflag] = NULL;
204 static void __nfs4_file_put_access(struct nfs4_file *fp, int oflag)
206 if (atomic_dec_and_test(&fp->fi_access[oflag])) {
207 nfs4_file_put_fd(fp, O_RDWR);
208 nfs4_file_put_fd(fp, oflag);
212 static void nfs4_file_put_access(struct nfs4_file *fp, int oflag)
214 if (oflag == O_RDWR) {
215 __nfs4_file_put_access(fp, O_RDONLY);
216 __nfs4_file_put_access(fp, O_WRONLY);
217 } else
218 __nfs4_file_put_access(fp, oflag);
221 static struct nfs4_delegation *
222 alloc_init_deleg(struct nfs4_client *clp, struct nfs4_stateid *stp, struct svc_fh *current_fh, u32 type)
224 struct nfs4_delegation *dp;
225 struct nfs4_file *fp = stp->st_file;
227 dprintk("NFSD alloc_init_deleg\n");
229 * Major work on the lease subsystem (for example, to support
230 * calbacks on stat) will be required before we can support
231 * write delegations properly.
233 if (type != NFS4_OPEN_DELEGATE_READ)
234 return NULL;
235 if (fp->fi_had_conflict)
236 return NULL;
237 if (num_delegations > max_delegations)
238 return NULL;
239 dp = kmem_cache_alloc(deleg_slab, GFP_KERNEL);
240 if (dp == NULL)
241 return dp;
242 num_delegations++;
243 INIT_LIST_HEAD(&dp->dl_perfile);
244 INIT_LIST_HEAD(&dp->dl_perclnt);
245 INIT_LIST_HEAD(&dp->dl_recall_lru);
246 dp->dl_client = clp;
247 get_nfs4_file(fp);
248 dp->dl_file = fp;
249 dp->dl_type = type;
250 dp->dl_stateid.si_boot = boot_time;
251 dp->dl_stateid.si_stateownerid = current_delegid++;
252 dp->dl_stateid.si_fileid = 0;
253 dp->dl_stateid.si_generation = 1;
254 fh_copy_shallow(&dp->dl_fh, &current_fh->fh_handle);
255 dp->dl_time = 0;
256 atomic_set(&dp->dl_count, 1);
257 INIT_WORK(&dp->dl_recall.cb_work, nfsd4_do_callback_rpc);
258 return dp;
261 void
262 nfs4_put_delegation(struct nfs4_delegation *dp)
264 if (atomic_dec_and_test(&dp->dl_count)) {
265 dprintk("NFSD: freeing dp %p\n",dp);
266 put_nfs4_file(dp->dl_file);
267 kmem_cache_free(deleg_slab, dp);
268 num_delegations--;
272 static void nfs4_put_deleg_lease(struct nfs4_file *fp)
274 if (atomic_dec_and_test(&fp->fi_delegees)) {
275 vfs_setlease(fp->fi_deleg_file, F_UNLCK, &fp->fi_lease);
276 fp->fi_lease = NULL;
277 fput(fp->fi_deleg_file);
278 fp->fi_deleg_file = NULL;
282 /* Called under the state lock. */
283 static void
284 unhash_delegation(struct nfs4_delegation *dp)
286 list_del_init(&dp->dl_perclnt);
287 spin_lock(&recall_lock);
288 list_del_init(&dp->dl_perfile);
289 list_del_init(&dp->dl_recall_lru);
290 spin_unlock(&recall_lock);
291 nfs4_put_deleg_lease(dp->dl_file);
292 nfs4_put_delegation(dp);
296 * SETCLIENTID state
299 /* client_lock protects the client lru list and session hash table */
300 static DEFINE_SPINLOCK(client_lock);
302 /* Hash tables for nfs4_clientid state */
303 #define CLIENT_HASH_BITS 4
304 #define CLIENT_HASH_SIZE (1 << CLIENT_HASH_BITS)
305 #define CLIENT_HASH_MASK (CLIENT_HASH_SIZE - 1)
307 static unsigned int clientid_hashval(u32 id)
309 return id & CLIENT_HASH_MASK;
312 static unsigned int clientstr_hashval(const char *name)
314 return opaque_hashval(name, 8) & CLIENT_HASH_MASK;
318 * reclaim_str_hashtbl[] holds known client info from previous reset/reboot
319 * used in reboot/reset lease grace period processing
321 * conf_id_hashtbl[], and conf_str_hashtbl[] hold confirmed
322 * setclientid_confirmed info.
324 * unconf_str_hastbl[] and unconf_id_hashtbl[] hold unconfirmed
325 * setclientid info.
327 * client_lru holds client queue ordered by nfs4_client.cl_time
328 * for lease renewal.
330 * close_lru holds (open) stateowner queue ordered by nfs4_stateowner.so_time
331 * for last close replay.
333 static struct list_head reclaim_str_hashtbl[CLIENT_HASH_SIZE];
334 static int reclaim_str_hashtbl_size = 0;
335 static struct list_head conf_id_hashtbl[CLIENT_HASH_SIZE];
336 static struct list_head conf_str_hashtbl[CLIENT_HASH_SIZE];
337 static struct list_head unconf_str_hashtbl[CLIENT_HASH_SIZE];
338 static struct list_head unconf_id_hashtbl[CLIENT_HASH_SIZE];
339 static struct list_head client_lru;
340 static struct list_head close_lru;
343 * We store the NONE, READ, WRITE, and BOTH bits separately in the
344 * st_{access,deny}_bmap field of the stateid, in order to track not
345 * only what share bits are currently in force, but also what
346 * combinations of share bits previous opens have used. This allows us
347 * to enforce the recommendation of rfc 3530 14.2.19 that the server
348 * return an error if the client attempt to downgrade to a combination
349 * of share bits not explicable by closing some of its previous opens.
351 * XXX: This enforcement is actually incomplete, since we don't keep
352 * track of access/deny bit combinations; so, e.g., we allow:
354 * OPEN allow read, deny write
355 * OPEN allow both, deny none
356 * DOWNGRADE allow read, deny none
358 * which we should reject.
360 static void
361 set_access(unsigned int *access, unsigned long bmap) {
362 int i;
364 *access = 0;
365 for (i = 1; i < 4; i++) {
366 if (test_bit(i, &bmap))
367 *access |= i;
371 static void
372 set_deny(unsigned int *deny, unsigned long bmap) {
373 int i;
375 *deny = 0;
376 for (i = 0; i < 4; i++) {
377 if (test_bit(i, &bmap))
378 *deny |= i ;
382 static int
383 test_share(struct nfs4_stateid *stp, struct nfsd4_open *open) {
384 unsigned int access, deny;
386 set_access(&access, stp->st_access_bmap);
387 set_deny(&deny, stp->st_deny_bmap);
388 if ((access & open->op_share_deny) || (deny & open->op_share_access))
389 return 0;
390 return 1;
393 static int nfs4_access_to_omode(u32 access)
395 switch (access & NFS4_SHARE_ACCESS_BOTH) {
396 case NFS4_SHARE_ACCESS_READ:
397 return O_RDONLY;
398 case NFS4_SHARE_ACCESS_WRITE:
399 return O_WRONLY;
400 case NFS4_SHARE_ACCESS_BOTH:
401 return O_RDWR;
403 BUG();
406 static void unhash_generic_stateid(struct nfs4_stateid *stp)
408 list_del(&stp->st_hash);
409 list_del(&stp->st_perfile);
410 list_del(&stp->st_perstateowner);
413 static void close_generic_stateid(struct nfs4_stateid *stp)
415 int i;
417 if (stp->st_access_bmap) {
418 for (i = 1; i < 4; i++) {
419 if (test_bit(i, &stp->st_access_bmap))
420 nfs4_file_put_access(stp->st_file,
421 nfs4_access_to_omode(i));
422 __clear_bit(i, &stp->st_access_bmap);
425 put_nfs4_file(stp->st_file);
426 stp->st_file = NULL;
429 static void free_generic_stateid(struct nfs4_stateid *stp)
431 close_generic_stateid(stp);
432 kmem_cache_free(stateid_slab, stp);
435 static void release_lock_stateid(struct nfs4_stateid *stp)
437 struct file *file;
439 unhash_generic_stateid(stp);
440 file = find_any_file(stp->st_file);
441 if (file)
442 locks_remove_posix(file, (fl_owner_t)lockowner(stp->st_stateowner));
443 free_generic_stateid(stp);
446 static void unhash_lockowner(struct nfs4_lockowner *lo)
448 struct nfs4_stateid *stp;
450 list_del(&lo->lo_owner.so_idhash);
451 list_del(&lo->lo_owner.so_strhash);
452 list_del(&lo->lo_perstateid);
453 while (!list_empty(&lo->lo_owner.so_stateids)) {
454 stp = list_first_entry(&lo->lo_owner.so_stateids,
455 struct nfs4_stateid, st_perstateowner);
456 release_lock_stateid(stp);
460 static void release_lockowner(struct nfs4_lockowner *lo)
462 unhash_lockowner(lo);
463 nfs4_free_lockowner(lo);
466 static void
467 release_stateid_lockowners(struct nfs4_stateid *open_stp)
469 struct nfs4_lockowner *lo;
471 while (!list_empty(&open_stp->st_lockowners)) {
472 lo = list_entry(open_stp->st_lockowners.next,
473 struct nfs4_lockowner, lo_perstateid);
474 release_lockowner(lo);
478 static void release_open_stateid(struct nfs4_stateid *stp)
480 unhash_generic_stateid(stp);
481 release_stateid_lockowners(stp);
482 free_generic_stateid(stp);
485 static void unhash_openowner(struct nfs4_openowner *oo)
487 struct nfs4_stateid *stp;
489 list_del(&oo->oo_owner.so_idhash);
490 list_del(&oo->oo_owner.so_strhash);
491 list_del(&oo->oo_perclient);
492 while (!list_empty(&oo->oo_owner.so_stateids)) {
493 stp = list_first_entry(&oo->oo_owner.so_stateids,
494 struct nfs4_stateid, st_perstateowner);
495 release_open_stateid(stp);
499 static void release_openowner(struct nfs4_openowner *oo)
501 unhash_openowner(oo);
502 list_del(&oo->oo_close_lru);
503 nfs4_free_openowner(oo);
506 #define SESSION_HASH_SIZE 512
507 static struct list_head sessionid_hashtbl[SESSION_HASH_SIZE];
509 static inline int
510 hash_sessionid(struct nfs4_sessionid *sessionid)
512 struct nfsd4_sessionid *sid = (struct nfsd4_sessionid *)sessionid;
514 return sid->sequence % SESSION_HASH_SIZE;
517 static inline void
518 dump_sessionid(const char *fn, struct nfs4_sessionid *sessionid)
520 u32 *ptr = (u32 *)(&sessionid->data[0]);
521 dprintk("%s: %u:%u:%u:%u\n", fn, ptr[0], ptr[1], ptr[2], ptr[3]);
524 static void
525 gen_sessionid(struct nfsd4_session *ses)
527 struct nfs4_client *clp = ses->se_client;
528 struct nfsd4_sessionid *sid;
530 sid = (struct nfsd4_sessionid *)ses->se_sessionid.data;
531 sid->clientid = clp->cl_clientid;
532 sid->sequence = current_sessionid++;
533 sid->reserved = 0;
537 * The protocol defines ca_maxresponssize_cached to include the size of
538 * the rpc header, but all we need to cache is the data starting after
539 * the end of the initial SEQUENCE operation--the rest we regenerate
540 * each time. Therefore we can advertise a ca_maxresponssize_cached
541 * value that is the number of bytes in our cache plus a few additional
542 * bytes. In order to stay on the safe side, and not promise more than
543 * we can cache, those additional bytes must be the minimum possible: 24
544 * bytes of rpc header (xid through accept state, with AUTH_NULL
545 * verifier), 12 for the compound header (with zero-length tag), and 44
546 * for the SEQUENCE op response:
548 #define NFSD_MIN_HDR_SEQ_SZ (24 + 12 + 44)
550 static void
551 free_session_slots(struct nfsd4_session *ses)
553 int i;
555 for (i = 0; i < ses->se_fchannel.maxreqs; i++)
556 kfree(ses->se_slots[i]);
560 * We don't actually need to cache the rpc and session headers, so we
561 * can allocate a little less for each slot:
563 static inline int slot_bytes(struct nfsd4_channel_attrs *ca)
565 return ca->maxresp_cached - NFSD_MIN_HDR_SEQ_SZ;
568 static int nfsd4_sanitize_slot_size(u32 size)
570 size -= NFSD_MIN_HDR_SEQ_SZ; /* We don't cache the rpc header */
571 size = min_t(u32, size, NFSD_SLOT_CACHE_SIZE);
573 return size;
577 * XXX: If we run out of reserved DRC memory we could (up to a point)
578 * re-negotiate active sessions and reduce their slot usage to make
579 * rooom for new connections. For now we just fail the create session.
581 static int nfsd4_get_drc_mem(int slotsize, u32 num)
583 int avail;
585 num = min_t(u32, num, NFSD_MAX_SLOTS_PER_SESSION);
587 spin_lock(&nfsd_drc_lock);
588 avail = min_t(int, NFSD_MAX_MEM_PER_SESSION,
589 nfsd_drc_max_mem - nfsd_drc_mem_used);
590 num = min_t(int, num, avail / slotsize);
591 nfsd_drc_mem_used += num * slotsize;
592 spin_unlock(&nfsd_drc_lock);
594 return num;
597 static void nfsd4_put_drc_mem(int slotsize, int num)
599 spin_lock(&nfsd_drc_lock);
600 nfsd_drc_mem_used -= slotsize * num;
601 spin_unlock(&nfsd_drc_lock);
604 static struct nfsd4_session *alloc_session(int slotsize, int numslots)
606 struct nfsd4_session *new;
607 int mem, i;
609 BUILD_BUG_ON(NFSD_MAX_SLOTS_PER_SESSION * sizeof(struct nfsd4_slot *)
610 + sizeof(struct nfsd4_session) > PAGE_SIZE);
611 mem = numslots * sizeof(struct nfsd4_slot *);
613 new = kzalloc(sizeof(*new) + mem, GFP_KERNEL);
614 if (!new)
615 return NULL;
616 /* allocate each struct nfsd4_slot and data cache in one piece */
617 for (i = 0; i < numslots; i++) {
618 mem = sizeof(struct nfsd4_slot) + slotsize;
619 new->se_slots[i] = kzalloc(mem, GFP_KERNEL);
620 if (!new->se_slots[i])
621 goto out_free;
623 return new;
624 out_free:
625 while (i--)
626 kfree(new->se_slots[i]);
627 kfree(new);
628 return NULL;
631 static void init_forechannel_attrs(struct nfsd4_channel_attrs *new, struct nfsd4_channel_attrs *req, int numslots, int slotsize)
633 u32 maxrpc = nfsd_serv->sv_max_mesg;
635 new->maxreqs = numslots;
636 new->maxresp_cached = min_t(u32, req->maxresp_cached,
637 slotsize + NFSD_MIN_HDR_SEQ_SZ);
638 new->maxreq_sz = min_t(u32, req->maxreq_sz, maxrpc);
639 new->maxresp_sz = min_t(u32, req->maxresp_sz, maxrpc);
640 new->maxops = min_t(u32, req->maxops, NFSD_MAX_OPS_PER_COMPOUND);
643 static void free_conn(struct nfsd4_conn *c)
645 svc_xprt_put(c->cn_xprt);
646 kfree(c);
649 static void nfsd4_conn_lost(struct svc_xpt_user *u)
651 struct nfsd4_conn *c = container_of(u, struct nfsd4_conn, cn_xpt_user);
652 struct nfs4_client *clp = c->cn_session->se_client;
654 spin_lock(&clp->cl_lock);
655 if (!list_empty(&c->cn_persession)) {
656 list_del(&c->cn_persession);
657 free_conn(c);
659 spin_unlock(&clp->cl_lock);
660 nfsd4_probe_callback(clp);
663 static struct nfsd4_conn *alloc_conn(struct svc_rqst *rqstp, u32 flags)
665 struct nfsd4_conn *conn;
667 conn = kmalloc(sizeof(struct nfsd4_conn), GFP_KERNEL);
668 if (!conn)
669 return NULL;
670 svc_xprt_get(rqstp->rq_xprt);
671 conn->cn_xprt = rqstp->rq_xprt;
672 conn->cn_flags = flags;
673 INIT_LIST_HEAD(&conn->cn_xpt_user.list);
674 return conn;
677 static void __nfsd4_hash_conn(struct nfsd4_conn *conn, struct nfsd4_session *ses)
679 conn->cn_session = ses;
680 list_add(&conn->cn_persession, &ses->se_conns);
683 static void nfsd4_hash_conn(struct nfsd4_conn *conn, struct nfsd4_session *ses)
685 struct nfs4_client *clp = ses->se_client;
687 spin_lock(&clp->cl_lock);
688 __nfsd4_hash_conn(conn, ses);
689 spin_unlock(&clp->cl_lock);
692 static int nfsd4_register_conn(struct nfsd4_conn *conn)
694 conn->cn_xpt_user.callback = nfsd4_conn_lost;
695 return register_xpt_user(conn->cn_xprt, &conn->cn_xpt_user);
698 static __be32 nfsd4_new_conn(struct svc_rqst *rqstp, struct nfsd4_session *ses, u32 dir)
700 struct nfsd4_conn *conn;
701 int ret;
703 conn = alloc_conn(rqstp, dir);
704 if (!conn)
705 return nfserr_jukebox;
706 nfsd4_hash_conn(conn, ses);
707 ret = nfsd4_register_conn(conn);
708 if (ret)
709 /* oops; xprt is already down: */
710 nfsd4_conn_lost(&conn->cn_xpt_user);
711 return nfs_ok;
714 static __be32 nfsd4_new_conn_from_crses(struct svc_rqst *rqstp, struct nfsd4_session *ses)
716 u32 dir = NFS4_CDFC4_FORE;
718 if (ses->se_flags & SESSION4_BACK_CHAN)
719 dir |= NFS4_CDFC4_BACK;
721 return nfsd4_new_conn(rqstp, ses, dir);
724 /* must be called under client_lock */
725 static void nfsd4_del_conns(struct nfsd4_session *s)
727 struct nfs4_client *clp = s->se_client;
728 struct nfsd4_conn *c;
730 spin_lock(&clp->cl_lock);
731 while (!list_empty(&s->se_conns)) {
732 c = list_first_entry(&s->se_conns, struct nfsd4_conn, cn_persession);
733 list_del_init(&c->cn_persession);
734 spin_unlock(&clp->cl_lock);
736 unregister_xpt_user(c->cn_xprt, &c->cn_xpt_user);
737 free_conn(c);
739 spin_lock(&clp->cl_lock);
741 spin_unlock(&clp->cl_lock);
744 void free_session(struct kref *kref)
746 struct nfsd4_session *ses;
747 int mem;
749 ses = container_of(kref, struct nfsd4_session, se_ref);
750 nfsd4_del_conns(ses);
751 spin_lock(&nfsd_drc_lock);
752 mem = ses->se_fchannel.maxreqs * slot_bytes(&ses->se_fchannel);
753 nfsd_drc_mem_used -= mem;
754 spin_unlock(&nfsd_drc_lock);
755 free_session_slots(ses);
756 kfree(ses);
759 static struct nfsd4_session *alloc_init_session(struct svc_rqst *rqstp, struct nfs4_client *clp, struct nfsd4_create_session *cses)
761 struct nfsd4_session *new;
762 struct nfsd4_channel_attrs *fchan = &cses->fore_channel;
763 int numslots, slotsize;
764 int status;
765 int idx;
768 * Note decreasing slot size below client's request may
769 * make it difficult for client to function correctly, whereas
770 * decreasing the number of slots will (just?) affect
771 * performance. When short on memory we therefore prefer to
772 * decrease number of slots instead of their size.
774 slotsize = nfsd4_sanitize_slot_size(fchan->maxresp_cached);
775 numslots = nfsd4_get_drc_mem(slotsize, fchan->maxreqs);
776 if (numslots < 1)
777 return NULL;
779 new = alloc_session(slotsize, numslots);
780 if (!new) {
781 nfsd4_put_drc_mem(slotsize, fchan->maxreqs);
782 return NULL;
784 init_forechannel_attrs(&new->se_fchannel, fchan, numslots, slotsize);
786 new->se_client = clp;
787 gen_sessionid(new);
789 INIT_LIST_HEAD(&new->se_conns);
791 new->se_cb_seq_nr = 1;
792 new->se_flags = cses->flags;
793 new->se_cb_prog = cses->callback_prog;
794 kref_init(&new->se_ref);
795 idx = hash_sessionid(&new->se_sessionid);
796 spin_lock(&client_lock);
797 list_add(&new->se_hash, &sessionid_hashtbl[idx]);
798 spin_lock(&clp->cl_lock);
799 list_add(&new->se_perclnt, &clp->cl_sessions);
800 spin_unlock(&clp->cl_lock);
801 spin_unlock(&client_lock);
803 status = nfsd4_new_conn_from_crses(rqstp, new);
804 /* whoops: benny points out, status is ignored! (err, or bogus) */
805 if (status) {
806 free_session(&new->se_ref);
807 return NULL;
809 if (cses->flags & SESSION4_BACK_CHAN) {
810 struct sockaddr *sa = svc_addr(rqstp);
812 * This is a little silly; with sessions there's no real
813 * use for the callback address. Use the peer address
814 * as a reasonable default for now, but consider fixing
815 * the rpc client not to require an address in the
816 * future:
818 rpc_copy_addr((struct sockaddr *)&clp->cl_cb_conn.cb_addr, sa);
819 clp->cl_cb_conn.cb_addrlen = svc_addr_len(sa);
821 nfsd4_probe_callback(clp);
822 return new;
825 /* caller must hold client_lock */
826 static struct nfsd4_session *
827 find_in_sessionid_hashtbl(struct nfs4_sessionid *sessionid)
829 struct nfsd4_session *elem;
830 int idx;
832 dump_sessionid(__func__, sessionid);
833 idx = hash_sessionid(sessionid);
834 /* Search in the appropriate list */
835 list_for_each_entry(elem, &sessionid_hashtbl[idx], se_hash) {
836 if (!memcmp(elem->se_sessionid.data, sessionid->data,
837 NFS4_MAX_SESSIONID_LEN)) {
838 return elem;
842 dprintk("%s: session not found\n", __func__);
843 return NULL;
846 /* caller must hold client_lock */
847 static void
848 unhash_session(struct nfsd4_session *ses)
850 list_del(&ses->se_hash);
851 spin_lock(&ses->se_client->cl_lock);
852 list_del(&ses->se_perclnt);
853 spin_unlock(&ses->se_client->cl_lock);
856 /* must be called under the client_lock */
857 static inline void
858 renew_client_locked(struct nfs4_client *clp)
860 if (is_client_expired(clp)) {
861 dprintk("%s: client (clientid %08x/%08x) already expired\n",
862 __func__,
863 clp->cl_clientid.cl_boot,
864 clp->cl_clientid.cl_id);
865 return;
869 * Move client to the end to the LRU list.
871 dprintk("renewing client (clientid %08x/%08x)\n",
872 clp->cl_clientid.cl_boot,
873 clp->cl_clientid.cl_id);
874 list_move_tail(&clp->cl_lru, &client_lru);
875 clp->cl_time = get_seconds();
878 static inline void
879 renew_client(struct nfs4_client *clp)
881 spin_lock(&client_lock);
882 renew_client_locked(clp);
883 spin_unlock(&client_lock);
886 /* SETCLIENTID and SETCLIENTID_CONFIRM Helper functions */
887 static int
888 STALE_CLIENTID(clientid_t *clid)
890 if (clid->cl_boot == boot_time)
891 return 0;
892 dprintk("NFSD stale clientid (%08x/%08x) boot_time %08lx\n",
893 clid->cl_boot, clid->cl_id, boot_time);
894 return 1;
898 * XXX Should we use a slab cache ?
899 * This type of memory management is somewhat inefficient, but we use it
900 * anyway since SETCLIENTID is not a common operation.
902 static struct nfs4_client *alloc_client(struct xdr_netobj name)
904 struct nfs4_client *clp;
906 clp = kzalloc(sizeof(struct nfs4_client), GFP_KERNEL);
907 if (clp == NULL)
908 return NULL;
909 clp->cl_name.data = kmalloc(name.len, GFP_KERNEL);
910 if (clp->cl_name.data == NULL) {
911 kfree(clp);
912 return NULL;
914 memcpy(clp->cl_name.data, name.data, name.len);
915 clp->cl_name.len = name.len;
916 return clp;
919 static inline void
920 free_client(struct nfs4_client *clp)
922 while (!list_empty(&clp->cl_sessions)) {
923 struct nfsd4_session *ses;
924 ses = list_entry(clp->cl_sessions.next, struct nfsd4_session,
925 se_perclnt);
926 list_del(&ses->se_perclnt);
927 nfsd4_put_session(ses);
929 if (clp->cl_cred.cr_group_info)
930 put_group_info(clp->cl_cred.cr_group_info);
931 kfree(clp->cl_principal);
932 kfree(clp->cl_name.data);
933 kfree(clp);
936 void
937 release_session_client(struct nfsd4_session *session)
939 struct nfs4_client *clp = session->se_client;
941 if (!atomic_dec_and_lock(&clp->cl_refcount, &client_lock))
942 return;
943 if (is_client_expired(clp)) {
944 free_client(clp);
945 session->se_client = NULL;
946 } else
947 renew_client_locked(clp);
948 spin_unlock(&client_lock);
951 /* must be called under the client_lock */
952 static inline void
953 unhash_client_locked(struct nfs4_client *clp)
955 struct nfsd4_session *ses;
957 mark_client_expired(clp);
958 list_del(&clp->cl_lru);
959 spin_lock(&clp->cl_lock);
960 list_for_each_entry(ses, &clp->cl_sessions, se_perclnt)
961 list_del_init(&ses->se_hash);
962 spin_unlock(&clp->cl_lock);
965 static void
966 expire_client(struct nfs4_client *clp)
968 struct nfs4_openowner *oo;
969 struct nfs4_delegation *dp;
970 struct list_head reaplist;
972 INIT_LIST_HEAD(&reaplist);
973 spin_lock(&recall_lock);
974 while (!list_empty(&clp->cl_delegations)) {
975 dp = list_entry(clp->cl_delegations.next, struct nfs4_delegation, dl_perclnt);
976 list_del_init(&dp->dl_perclnt);
977 list_move(&dp->dl_recall_lru, &reaplist);
979 spin_unlock(&recall_lock);
980 while (!list_empty(&reaplist)) {
981 dp = list_entry(reaplist.next, struct nfs4_delegation, dl_recall_lru);
982 list_del_init(&dp->dl_recall_lru);
983 unhash_delegation(dp);
985 while (!list_empty(&clp->cl_openowners)) {
986 oo = list_entry(clp->cl_openowners.next, struct nfs4_openowner, oo_perclient);
987 release_openowner(oo);
989 nfsd4_shutdown_callback(clp);
990 if (clp->cl_cb_conn.cb_xprt)
991 svc_xprt_put(clp->cl_cb_conn.cb_xprt);
992 list_del(&clp->cl_idhash);
993 list_del(&clp->cl_strhash);
994 spin_lock(&client_lock);
995 unhash_client_locked(clp);
996 if (atomic_read(&clp->cl_refcount) == 0)
997 free_client(clp);
998 spin_unlock(&client_lock);
1001 static void copy_verf(struct nfs4_client *target, nfs4_verifier *source)
1003 memcpy(target->cl_verifier.data, source->data,
1004 sizeof(target->cl_verifier.data));
1007 static void copy_clid(struct nfs4_client *target, struct nfs4_client *source)
1009 target->cl_clientid.cl_boot = source->cl_clientid.cl_boot;
1010 target->cl_clientid.cl_id = source->cl_clientid.cl_id;
1013 static void copy_cred(struct svc_cred *target, struct svc_cred *source)
1015 target->cr_uid = source->cr_uid;
1016 target->cr_gid = source->cr_gid;
1017 target->cr_group_info = source->cr_group_info;
1018 get_group_info(target->cr_group_info);
1021 static int same_name(const char *n1, const char *n2)
1023 return 0 == memcmp(n1, n2, HEXDIR_LEN);
1026 static int
1027 same_verf(nfs4_verifier *v1, nfs4_verifier *v2)
1029 return 0 == memcmp(v1->data, v2->data, sizeof(v1->data));
1032 static int
1033 same_clid(clientid_t *cl1, clientid_t *cl2)
1035 return (cl1->cl_boot == cl2->cl_boot) && (cl1->cl_id == cl2->cl_id);
1038 /* XXX what about NGROUP */
1039 static int
1040 same_creds(struct svc_cred *cr1, struct svc_cred *cr2)
1042 return cr1->cr_uid == cr2->cr_uid;
1045 static void gen_clid(struct nfs4_client *clp)
1047 static u32 current_clientid = 1;
1049 clp->cl_clientid.cl_boot = boot_time;
1050 clp->cl_clientid.cl_id = current_clientid++;
1053 static void gen_confirm(struct nfs4_client *clp)
1055 static u32 i;
1056 u32 *p;
1058 p = (u32 *)clp->cl_confirm.data;
1059 *p++ = get_seconds();
1060 *p++ = i++;
1063 static int
1064 same_stateid(stateid_t *id_one, stateid_t *id_two)
1066 if (id_one->si_stateownerid != id_two->si_stateownerid)
1067 return 0;
1068 return id_one->si_fileid == id_two->si_fileid;
1071 static struct nfs4_stateid *find_stateid(stateid_t *t)
1073 struct nfs4_stateid *s;
1074 unsigned int hashval;
1076 hashval = stateid_hashval(t->si_stateownerid, t->si_fileid);
1077 list_for_each_entry(s, &stateid_hashtbl[hashval], st_hash)
1078 if (same_stateid(&s->st_stateid, t))
1079 return s;
1080 return NULL;
1083 static struct nfs4_stateid *find_stateid_by_type(stateid_t *t, char typemask)
1085 struct nfs4_stateid *s;
1087 s = find_stateid(t);
1088 if (!s)
1089 return NULL;
1090 if (typemask & s->st_type)
1091 return s;
1092 return NULL;
1095 static struct nfs4_client *create_client(struct xdr_netobj name, char *recdir,
1096 struct svc_rqst *rqstp, nfs4_verifier *verf)
1098 struct nfs4_client *clp;
1099 struct sockaddr *sa = svc_addr(rqstp);
1100 char *princ;
1102 clp = alloc_client(name);
1103 if (clp == NULL)
1104 return NULL;
1106 INIT_LIST_HEAD(&clp->cl_sessions);
1108 princ = svc_gss_principal(rqstp);
1109 if (princ) {
1110 clp->cl_principal = kstrdup(princ, GFP_KERNEL);
1111 if (clp->cl_principal == NULL) {
1112 free_client(clp);
1113 return NULL;
1117 memcpy(clp->cl_recdir, recdir, HEXDIR_LEN);
1118 atomic_set(&clp->cl_refcount, 0);
1119 clp->cl_cb_state = NFSD4_CB_UNKNOWN;
1120 INIT_LIST_HEAD(&clp->cl_idhash);
1121 INIT_LIST_HEAD(&clp->cl_strhash);
1122 INIT_LIST_HEAD(&clp->cl_openowners);
1123 INIT_LIST_HEAD(&clp->cl_delegations);
1124 INIT_LIST_HEAD(&clp->cl_lru);
1125 INIT_LIST_HEAD(&clp->cl_callbacks);
1126 spin_lock_init(&clp->cl_lock);
1127 INIT_WORK(&clp->cl_cb_null.cb_work, nfsd4_do_callback_rpc);
1128 clp->cl_time = get_seconds();
1129 clear_bit(0, &clp->cl_cb_slot_busy);
1130 rpc_init_wait_queue(&clp->cl_cb_waitq, "Backchannel slot table");
1131 copy_verf(clp, verf);
1132 rpc_copy_addr((struct sockaddr *) &clp->cl_addr, sa);
1133 clp->cl_flavor = rqstp->rq_flavor;
1134 copy_cred(&clp->cl_cred, &rqstp->rq_cred);
1135 gen_confirm(clp);
1136 clp->cl_cb_session = NULL;
1137 return clp;
1140 static int check_name(struct xdr_netobj name)
1142 if (name.len == 0)
1143 return 0;
1144 if (name.len > NFS4_OPAQUE_LIMIT) {
1145 dprintk("NFSD: check_name: name too long(%d)!\n", name.len);
1146 return 0;
1148 return 1;
1151 static void
1152 add_to_unconfirmed(struct nfs4_client *clp, unsigned int strhashval)
1154 unsigned int idhashval;
1156 list_add(&clp->cl_strhash, &unconf_str_hashtbl[strhashval]);
1157 idhashval = clientid_hashval(clp->cl_clientid.cl_id);
1158 list_add(&clp->cl_idhash, &unconf_id_hashtbl[idhashval]);
1159 renew_client(clp);
1162 static void
1163 move_to_confirmed(struct nfs4_client *clp)
1165 unsigned int idhashval = clientid_hashval(clp->cl_clientid.cl_id);
1166 unsigned int strhashval;
1168 dprintk("NFSD: move_to_confirm nfs4_client %p\n", clp);
1169 list_move(&clp->cl_idhash, &conf_id_hashtbl[idhashval]);
1170 strhashval = clientstr_hashval(clp->cl_recdir);
1171 list_move(&clp->cl_strhash, &conf_str_hashtbl[strhashval]);
1172 renew_client(clp);
1175 static struct nfs4_client *
1176 find_confirmed_client(clientid_t *clid)
1178 struct nfs4_client *clp;
1179 unsigned int idhashval = clientid_hashval(clid->cl_id);
1181 list_for_each_entry(clp, &conf_id_hashtbl[idhashval], cl_idhash) {
1182 if (same_clid(&clp->cl_clientid, clid))
1183 return clp;
1185 return NULL;
1188 static struct nfs4_client *
1189 find_unconfirmed_client(clientid_t *clid)
1191 struct nfs4_client *clp;
1192 unsigned int idhashval = clientid_hashval(clid->cl_id);
1194 list_for_each_entry(clp, &unconf_id_hashtbl[idhashval], cl_idhash) {
1195 if (same_clid(&clp->cl_clientid, clid))
1196 return clp;
1198 return NULL;
1201 static bool clp_used_exchangeid(struct nfs4_client *clp)
1203 return clp->cl_exchange_flags != 0;
1206 static struct nfs4_client *
1207 find_confirmed_client_by_str(const char *dname, unsigned int hashval)
1209 struct nfs4_client *clp;
1211 list_for_each_entry(clp, &conf_str_hashtbl[hashval], cl_strhash) {
1212 if (same_name(clp->cl_recdir, dname))
1213 return clp;
1215 return NULL;
1218 static struct nfs4_client *
1219 find_unconfirmed_client_by_str(const char *dname, unsigned int hashval)
1221 struct nfs4_client *clp;
1223 list_for_each_entry(clp, &unconf_str_hashtbl[hashval], cl_strhash) {
1224 if (same_name(clp->cl_recdir, dname))
1225 return clp;
1227 return NULL;
1230 static void rpc_svcaddr2sockaddr(struct sockaddr *sa, unsigned short family, union svc_addr_u *svcaddr)
1232 switch (family) {
1233 case AF_INET:
1234 ((struct sockaddr_in *)sa)->sin_family = AF_INET;
1235 ((struct sockaddr_in *)sa)->sin_addr = svcaddr->addr;
1236 return;
1237 case AF_INET6:
1238 ((struct sockaddr_in6 *)sa)->sin6_family = AF_INET6;
1239 ((struct sockaddr_in6 *)sa)->sin6_addr = svcaddr->addr6;
1240 return;
1244 static void
1245 gen_callback(struct nfs4_client *clp, struct nfsd4_setclientid *se, struct svc_rqst *rqstp)
1247 struct nfs4_cb_conn *conn = &clp->cl_cb_conn;
1248 struct sockaddr *sa = svc_addr(rqstp);
1249 u32 scopeid = rpc_get_scope_id(sa);
1250 unsigned short expected_family;
1252 /* Currently, we only support tcp and tcp6 for the callback channel */
1253 if (se->se_callback_netid_len == 3 &&
1254 !memcmp(se->se_callback_netid_val, "tcp", 3))
1255 expected_family = AF_INET;
1256 else if (se->se_callback_netid_len == 4 &&
1257 !memcmp(se->se_callback_netid_val, "tcp6", 4))
1258 expected_family = AF_INET6;
1259 else
1260 goto out_err;
1262 conn->cb_addrlen = rpc_uaddr2sockaddr(se->se_callback_addr_val,
1263 se->se_callback_addr_len,
1264 (struct sockaddr *)&conn->cb_addr,
1265 sizeof(conn->cb_addr));
1267 if (!conn->cb_addrlen || conn->cb_addr.ss_family != expected_family)
1268 goto out_err;
1270 if (conn->cb_addr.ss_family == AF_INET6)
1271 ((struct sockaddr_in6 *)&conn->cb_addr)->sin6_scope_id = scopeid;
1273 conn->cb_prog = se->se_callback_prog;
1274 conn->cb_ident = se->se_callback_ident;
1275 rpc_svcaddr2sockaddr((struct sockaddr *)&conn->cb_saddr, expected_family, &rqstp->rq_daddr);
1276 return;
1277 out_err:
1278 conn->cb_addr.ss_family = AF_UNSPEC;
1279 conn->cb_addrlen = 0;
1280 dprintk(KERN_INFO "NFSD: this client (clientid %08x/%08x) "
1281 "will not receive delegations\n",
1282 clp->cl_clientid.cl_boot, clp->cl_clientid.cl_id);
1284 return;
1288 * Cache a reply. nfsd4_check_drc_limit() has bounded the cache size.
1290 void
1291 nfsd4_store_cache_entry(struct nfsd4_compoundres *resp)
1293 struct nfsd4_slot *slot = resp->cstate.slot;
1294 unsigned int base;
1296 dprintk("--> %s slot %p\n", __func__, slot);
1298 slot->sl_opcnt = resp->opcnt;
1299 slot->sl_status = resp->cstate.status;
1301 if (nfsd4_not_cached(resp)) {
1302 slot->sl_datalen = 0;
1303 return;
1305 slot->sl_datalen = (char *)resp->p - (char *)resp->cstate.datap;
1306 base = (char *)resp->cstate.datap -
1307 (char *)resp->xbuf->head[0].iov_base;
1308 if (read_bytes_from_xdr_buf(resp->xbuf, base, slot->sl_data,
1309 slot->sl_datalen))
1310 WARN("%s: sessions DRC could not cache compound\n", __func__);
1311 return;
1315 * Encode the replay sequence operation from the slot values.
1316 * If cachethis is FALSE encode the uncached rep error on the next
1317 * operation which sets resp->p and increments resp->opcnt for
1318 * nfs4svc_encode_compoundres.
1321 static __be32
1322 nfsd4_enc_sequence_replay(struct nfsd4_compoundargs *args,
1323 struct nfsd4_compoundres *resp)
1325 struct nfsd4_op *op;
1326 struct nfsd4_slot *slot = resp->cstate.slot;
1328 dprintk("--> %s resp->opcnt %d cachethis %u \n", __func__,
1329 resp->opcnt, resp->cstate.slot->sl_cachethis);
1331 /* Encode the replayed sequence operation */
1332 op = &args->ops[resp->opcnt - 1];
1333 nfsd4_encode_operation(resp, op);
1335 /* Return nfserr_retry_uncached_rep in next operation. */
1336 if (args->opcnt > 1 && slot->sl_cachethis == 0) {
1337 op = &args->ops[resp->opcnt++];
1338 op->status = nfserr_retry_uncached_rep;
1339 nfsd4_encode_operation(resp, op);
1341 return op->status;
1345 * The sequence operation is not cached because we can use the slot and
1346 * session values.
1348 __be32
1349 nfsd4_replay_cache_entry(struct nfsd4_compoundres *resp,
1350 struct nfsd4_sequence *seq)
1352 struct nfsd4_slot *slot = resp->cstate.slot;
1353 __be32 status;
1355 dprintk("--> %s slot %p\n", __func__, slot);
1357 /* Either returns 0 or nfserr_retry_uncached */
1358 status = nfsd4_enc_sequence_replay(resp->rqstp->rq_argp, resp);
1359 if (status == nfserr_retry_uncached_rep)
1360 return status;
1362 /* The sequence operation has been encoded, cstate->datap set. */
1363 memcpy(resp->cstate.datap, slot->sl_data, slot->sl_datalen);
1365 resp->opcnt = slot->sl_opcnt;
1366 resp->p = resp->cstate.datap + XDR_QUADLEN(slot->sl_datalen);
1367 status = slot->sl_status;
1369 return status;
1373 * Set the exchange_id flags returned by the server.
1375 static void
1376 nfsd4_set_ex_flags(struct nfs4_client *new, struct nfsd4_exchange_id *clid)
1378 /* pNFS is not supported */
1379 new->cl_exchange_flags |= EXCHGID4_FLAG_USE_NON_PNFS;
1381 /* Referrals are supported, Migration is not. */
1382 new->cl_exchange_flags |= EXCHGID4_FLAG_SUPP_MOVED_REFER;
1384 /* set the wire flags to return to client. */
1385 clid->flags = new->cl_exchange_flags;
1388 __be32
1389 nfsd4_exchange_id(struct svc_rqst *rqstp,
1390 struct nfsd4_compound_state *cstate,
1391 struct nfsd4_exchange_id *exid)
1393 struct nfs4_client *unconf, *conf, *new;
1394 int status;
1395 unsigned int strhashval;
1396 char dname[HEXDIR_LEN];
1397 char addr_str[INET6_ADDRSTRLEN];
1398 nfs4_verifier verf = exid->verifier;
1399 struct sockaddr *sa = svc_addr(rqstp);
1401 rpc_ntop(sa, addr_str, sizeof(addr_str));
1402 dprintk("%s rqstp=%p exid=%p clname.len=%u clname.data=%p "
1403 "ip_addr=%s flags %x, spa_how %d\n",
1404 __func__, rqstp, exid, exid->clname.len, exid->clname.data,
1405 addr_str, exid->flags, exid->spa_how);
1407 if (!check_name(exid->clname) || (exid->flags & ~EXCHGID4_FLAG_MASK_A))
1408 return nfserr_inval;
1410 /* Currently only support SP4_NONE */
1411 switch (exid->spa_how) {
1412 case SP4_NONE:
1413 break;
1414 case SP4_SSV:
1415 return nfserr_serverfault;
1416 default:
1417 BUG(); /* checked by xdr code */
1418 case SP4_MACH_CRED:
1419 return nfserr_serverfault; /* no excuse :-/ */
1422 status = nfs4_make_rec_clidname(dname, &exid->clname);
1424 if (status)
1425 goto error;
1427 strhashval = clientstr_hashval(dname);
1429 nfs4_lock_state();
1430 status = nfs_ok;
1432 conf = find_confirmed_client_by_str(dname, strhashval);
1433 if (conf) {
1434 if (!clp_used_exchangeid(conf)) {
1435 status = nfserr_clid_inuse; /* XXX: ? */
1436 goto out;
1438 if (!same_verf(&verf, &conf->cl_verifier)) {
1439 /* 18.35.4 case 8 */
1440 if (exid->flags & EXCHGID4_FLAG_UPD_CONFIRMED_REC_A) {
1441 status = nfserr_not_same;
1442 goto out;
1444 /* Client reboot: destroy old state */
1445 expire_client(conf);
1446 goto out_new;
1448 if (!same_creds(&conf->cl_cred, &rqstp->rq_cred)) {
1449 /* 18.35.4 case 9 */
1450 if (exid->flags & EXCHGID4_FLAG_UPD_CONFIRMED_REC_A) {
1451 status = nfserr_perm;
1452 goto out;
1454 expire_client(conf);
1455 goto out_new;
1458 * Set bit when the owner id and verifier map to an already
1459 * confirmed client id (18.35.3).
1461 exid->flags |= EXCHGID4_FLAG_CONFIRMED_R;
1464 * Falling into 18.35.4 case 2, possible router replay.
1465 * Leave confirmed record intact and return same result.
1467 copy_verf(conf, &verf);
1468 new = conf;
1469 goto out_copy;
1472 /* 18.35.4 case 7 */
1473 if (exid->flags & EXCHGID4_FLAG_UPD_CONFIRMED_REC_A) {
1474 status = nfserr_noent;
1475 goto out;
1478 unconf = find_unconfirmed_client_by_str(dname, strhashval);
1479 if (unconf) {
1481 * Possible retry or client restart. Per 18.35.4 case 4,
1482 * a new unconfirmed record should be generated regardless
1483 * of whether any properties have changed.
1485 expire_client(unconf);
1488 out_new:
1489 /* Normal case */
1490 new = create_client(exid->clname, dname, rqstp, &verf);
1491 if (new == NULL) {
1492 status = nfserr_jukebox;
1493 goto out;
1496 gen_clid(new);
1497 add_to_unconfirmed(new, strhashval);
1498 out_copy:
1499 exid->clientid.cl_boot = new->cl_clientid.cl_boot;
1500 exid->clientid.cl_id = new->cl_clientid.cl_id;
1502 exid->seqid = 1;
1503 nfsd4_set_ex_flags(new, exid);
1505 dprintk("nfsd4_exchange_id seqid %d flags %x\n",
1506 new->cl_cs_slot.sl_seqid, new->cl_exchange_flags);
1507 status = nfs_ok;
1509 out:
1510 nfs4_unlock_state();
1511 error:
1512 dprintk("nfsd4_exchange_id returns %d\n", ntohl(status));
1513 return status;
1516 static int
1517 check_slot_seqid(u32 seqid, u32 slot_seqid, int slot_inuse)
1519 dprintk("%s enter. seqid %d slot_seqid %d\n", __func__, seqid,
1520 slot_seqid);
1522 /* The slot is in use, and no response has been sent. */
1523 if (slot_inuse) {
1524 if (seqid == slot_seqid)
1525 return nfserr_jukebox;
1526 else
1527 return nfserr_seq_misordered;
1529 /* Normal */
1530 if (likely(seqid == slot_seqid + 1))
1531 return nfs_ok;
1532 /* Replay */
1533 if (seqid == slot_seqid)
1534 return nfserr_replay_cache;
1535 /* Wraparound */
1536 if (seqid == 1 && (slot_seqid + 1) == 0)
1537 return nfs_ok;
1538 /* Misordered replay or misordered new request */
1539 return nfserr_seq_misordered;
1543 * Cache the create session result into the create session single DRC
1544 * slot cache by saving the xdr structure. sl_seqid has been set.
1545 * Do this for solo or embedded create session operations.
1547 static void
1548 nfsd4_cache_create_session(struct nfsd4_create_session *cr_ses,
1549 struct nfsd4_clid_slot *slot, int nfserr)
1551 slot->sl_status = nfserr;
1552 memcpy(&slot->sl_cr_ses, cr_ses, sizeof(*cr_ses));
1555 static __be32
1556 nfsd4_replay_create_session(struct nfsd4_create_session *cr_ses,
1557 struct nfsd4_clid_slot *slot)
1559 memcpy(cr_ses, &slot->sl_cr_ses, sizeof(*cr_ses));
1560 return slot->sl_status;
1563 #define NFSD_MIN_REQ_HDR_SEQ_SZ ((\
1564 2 * 2 + /* credential,verifier: AUTH_NULL, length 0 */ \
1565 1 + /* MIN tag is length with zero, only length */ \
1566 3 + /* version, opcount, opcode */ \
1567 XDR_QUADLEN(NFS4_MAX_SESSIONID_LEN) + \
1568 /* seqid, slotID, slotID, cache */ \
1569 4 ) * sizeof(__be32))
1571 #define NFSD_MIN_RESP_HDR_SEQ_SZ ((\
1572 2 + /* verifier: AUTH_NULL, length 0 */\
1573 1 + /* status */ \
1574 1 + /* MIN tag is length with zero, only length */ \
1575 3 + /* opcount, opcode, opstatus*/ \
1576 XDR_QUADLEN(NFS4_MAX_SESSIONID_LEN) + \
1577 /* seqid, slotID, slotID, slotID, status */ \
1578 5 ) * sizeof(__be32))
1580 static __be32 check_forechannel_attrs(struct nfsd4_channel_attrs fchannel)
1582 return fchannel.maxreq_sz < NFSD_MIN_REQ_HDR_SEQ_SZ
1583 || fchannel.maxresp_sz < NFSD_MIN_RESP_HDR_SEQ_SZ;
1586 __be32
1587 nfsd4_create_session(struct svc_rqst *rqstp,
1588 struct nfsd4_compound_state *cstate,
1589 struct nfsd4_create_session *cr_ses)
1591 struct sockaddr *sa = svc_addr(rqstp);
1592 struct nfs4_client *conf, *unconf;
1593 struct nfsd4_session *new;
1594 struct nfsd4_clid_slot *cs_slot = NULL;
1595 bool confirm_me = false;
1596 int status = 0;
1598 if (cr_ses->flags & ~SESSION4_FLAG_MASK_A)
1599 return nfserr_inval;
1601 nfs4_lock_state();
1602 unconf = find_unconfirmed_client(&cr_ses->clientid);
1603 conf = find_confirmed_client(&cr_ses->clientid);
1605 if (conf) {
1606 cs_slot = &conf->cl_cs_slot;
1607 status = check_slot_seqid(cr_ses->seqid, cs_slot->sl_seqid, 0);
1608 if (status == nfserr_replay_cache) {
1609 dprintk("Got a create_session replay! seqid= %d\n",
1610 cs_slot->sl_seqid);
1611 /* Return the cached reply status */
1612 status = nfsd4_replay_create_session(cr_ses, cs_slot);
1613 goto out;
1614 } else if (cr_ses->seqid != cs_slot->sl_seqid + 1) {
1615 status = nfserr_seq_misordered;
1616 dprintk("Sequence misordered!\n");
1617 dprintk("Expected seqid= %d but got seqid= %d\n",
1618 cs_slot->sl_seqid, cr_ses->seqid);
1619 goto out;
1621 } else if (unconf) {
1622 if (!same_creds(&unconf->cl_cred, &rqstp->rq_cred) ||
1623 !rpc_cmp_addr(sa, (struct sockaddr *) &unconf->cl_addr)) {
1624 status = nfserr_clid_inuse;
1625 goto out;
1628 cs_slot = &unconf->cl_cs_slot;
1629 status = check_slot_seqid(cr_ses->seqid, cs_slot->sl_seqid, 0);
1630 if (status) {
1631 /* an unconfirmed replay returns misordered */
1632 status = nfserr_seq_misordered;
1633 goto out;
1636 confirm_me = true;
1637 conf = unconf;
1638 } else {
1639 status = nfserr_stale_clientid;
1640 goto out;
1644 * XXX: we should probably set this at creation time, and check
1645 * for consistent minorversion use throughout:
1647 conf->cl_minorversion = 1;
1649 * We do not support RDMA or persistent sessions
1651 cr_ses->flags &= ~SESSION4_PERSIST;
1652 cr_ses->flags &= ~SESSION4_RDMA;
1654 status = nfserr_toosmall;
1655 if (check_forechannel_attrs(cr_ses->fore_channel))
1656 goto out;
1658 status = nfserr_jukebox;
1659 new = alloc_init_session(rqstp, conf, cr_ses);
1660 if (!new)
1661 goto out;
1662 status = nfs_ok;
1663 memcpy(cr_ses->sessionid.data, new->se_sessionid.data,
1664 NFS4_MAX_SESSIONID_LEN);
1665 memcpy(&cr_ses->fore_channel, &new->se_fchannel,
1666 sizeof(struct nfsd4_channel_attrs));
1667 cs_slot->sl_seqid++;
1668 cr_ses->seqid = cs_slot->sl_seqid;
1670 /* cache solo and embedded create sessions under the state lock */
1671 nfsd4_cache_create_session(cr_ses, cs_slot, status);
1672 if (confirm_me)
1673 move_to_confirmed(conf);
1674 out:
1675 nfs4_unlock_state();
1676 dprintk("%s returns %d\n", __func__, ntohl(status));
1677 return status;
1680 static bool nfsd4_last_compound_op(struct svc_rqst *rqstp)
1682 struct nfsd4_compoundres *resp = rqstp->rq_resp;
1683 struct nfsd4_compoundargs *argp = rqstp->rq_argp;
1685 return argp->opcnt == resp->opcnt;
1688 static __be32 nfsd4_map_bcts_dir(u32 *dir)
1690 switch (*dir) {
1691 case NFS4_CDFC4_FORE:
1692 case NFS4_CDFC4_BACK:
1693 return nfs_ok;
1694 case NFS4_CDFC4_FORE_OR_BOTH:
1695 case NFS4_CDFC4_BACK_OR_BOTH:
1696 *dir = NFS4_CDFC4_BOTH;
1697 return nfs_ok;
1699 return nfserr_inval;
1702 __be32 nfsd4_bind_conn_to_session(struct svc_rqst *rqstp,
1703 struct nfsd4_compound_state *cstate,
1704 struct nfsd4_bind_conn_to_session *bcts)
1706 __be32 status;
1708 if (!nfsd4_last_compound_op(rqstp))
1709 return nfserr_not_only_op;
1710 spin_lock(&client_lock);
1711 cstate->session = find_in_sessionid_hashtbl(&bcts->sessionid);
1712 /* Sorta weird: we only need the refcnt'ing because new_conn acquires
1713 * client_lock iself: */
1714 if (cstate->session) {
1715 nfsd4_get_session(cstate->session);
1716 atomic_inc(&cstate->session->se_client->cl_refcount);
1718 spin_unlock(&client_lock);
1719 if (!cstate->session)
1720 return nfserr_badsession;
1722 status = nfsd4_map_bcts_dir(&bcts->dir);
1723 if (!status)
1724 nfsd4_new_conn(rqstp, cstate->session, bcts->dir);
1725 return status;
1728 static bool nfsd4_compound_in_session(struct nfsd4_session *session, struct nfs4_sessionid *sid)
1730 if (!session)
1731 return 0;
1732 return !memcmp(sid, &session->se_sessionid, sizeof(*sid));
1735 __be32
1736 nfsd4_destroy_session(struct svc_rqst *r,
1737 struct nfsd4_compound_state *cstate,
1738 struct nfsd4_destroy_session *sessionid)
1740 struct nfsd4_session *ses;
1741 u32 status = nfserr_badsession;
1743 /* Notes:
1744 * - The confirmed nfs4_client->cl_sessionid holds destroyed sessinid
1745 * - Should we return nfserr_back_chan_busy if waiting for
1746 * callbacks on to-be-destroyed session?
1747 * - Do we need to clear any callback info from previous session?
1750 if (nfsd4_compound_in_session(cstate->session, &sessionid->sessionid)) {
1751 if (!nfsd4_last_compound_op(r))
1752 return nfserr_not_only_op;
1754 dump_sessionid(__func__, &sessionid->sessionid);
1755 spin_lock(&client_lock);
1756 ses = find_in_sessionid_hashtbl(&sessionid->sessionid);
1757 if (!ses) {
1758 spin_unlock(&client_lock);
1759 goto out;
1762 unhash_session(ses);
1763 spin_unlock(&client_lock);
1765 nfs4_lock_state();
1766 nfsd4_probe_callback_sync(ses->se_client);
1767 nfs4_unlock_state();
1769 nfsd4_del_conns(ses);
1771 nfsd4_put_session(ses);
1772 status = nfs_ok;
1773 out:
1774 dprintk("%s returns %d\n", __func__, ntohl(status));
1775 return status;
1778 static struct nfsd4_conn *__nfsd4_find_conn(struct svc_xprt *xpt, struct nfsd4_session *s)
1780 struct nfsd4_conn *c;
1782 list_for_each_entry(c, &s->se_conns, cn_persession) {
1783 if (c->cn_xprt == xpt) {
1784 return c;
1787 return NULL;
1790 static void nfsd4_sequence_check_conn(struct nfsd4_conn *new, struct nfsd4_session *ses)
1792 struct nfs4_client *clp = ses->se_client;
1793 struct nfsd4_conn *c;
1794 int ret;
1796 spin_lock(&clp->cl_lock);
1797 c = __nfsd4_find_conn(new->cn_xprt, ses);
1798 if (c) {
1799 spin_unlock(&clp->cl_lock);
1800 free_conn(new);
1801 return;
1803 __nfsd4_hash_conn(new, ses);
1804 spin_unlock(&clp->cl_lock);
1805 ret = nfsd4_register_conn(new);
1806 if (ret)
1807 /* oops; xprt is already down: */
1808 nfsd4_conn_lost(&new->cn_xpt_user);
1809 return;
1812 static bool nfsd4_session_too_many_ops(struct svc_rqst *rqstp, struct nfsd4_session *session)
1814 struct nfsd4_compoundargs *args = rqstp->rq_argp;
1816 return args->opcnt > session->se_fchannel.maxops;
1819 static bool nfsd4_request_too_big(struct svc_rqst *rqstp,
1820 struct nfsd4_session *session)
1822 struct xdr_buf *xb = &rqstp->rq_arg;
1824 return xb->len > session->se_fchannel.maxreq_sz;
1827 __be32
1828 nfsd4_sequence(struct svc_rqst *rqstp,
1829 struct nfsd4_compound_state *cstate,
1830 struct nfsd4_sequence *seq)
1832 struct nfsd4_compoundres *resp = rqstp->rq_resp;
1833 struct nfsd4_session *session;
1834 struct nfsd4_slot *slot;
1835 struct nfsd4_conn *conn;
1836 int status;
1838 if (resp->opcnt != 1)
1839 return nfserr_sequence_pos;
1842 * Will be either used or freed by nfsd4_sequence_check_conn
1843 * below.
1845 conn = alloc_conn(rqstp, NFS4_CDFC4_FORE);
1846 if (!conn)
1847 return nfserr_jukebox;
1849 spin_lock(&client_lock);
1850 status = nfserr_badsession;
1851 session = find_in_sessionid_hashtbl(&seq->sessionid);
1852 if (!session)
1853 goto out;
1855 status = nfserr_too_many_ops;
1856 if (nfsd4_session_too_many_ops(rqstp, session))
1857 goto out;
1859 status = nfserr_req_too_big;
1860 if (nfsd4_request_too_big(rqstp, session))
1861 goto out;
1863 status = nfserr_badslot;
1864 if (seq->slotid >= session->se_fchannel.maxreqs)
1865 goto out;
1867 slot = session->se_slots[seq->slotid];
1868 dprintk("%s: slotid %d\n", __func__, seq->slotid);
1870 /* We do not negotiate the number of slots yet, so set the
1871 * maxslots to the session maxreqs which is used to encode
1872 * sr_highest_slotid and the sr_target_slot id to maxslots */
1873 seq->maxslots = session->se_fchannel.maxreqs;
1875 status = check_slot_seqid(seq->seqid, slot->sl_seqid, slot->sl_inuse);
1876 if (status == nfserr_replay_cache) {
1877 cstate->slot = slot;
1878 cstate->session = session;
1879 /* Return the cached reply status and set cstate->status
1880 * for nfsd4_proc_compound processing */
1881 status = nfsd4_replay_cache_entry(resp, seq);
1882 cstate->status = nfserr_replay_cache;
1883 goto out;
1885 if (status)
1886 goto out;
1888 nfsd4_sequence_check_conn(conn, session);
1889 conn = NULL;
1891 /* Success! bump slot seqid */
1892 slot->sl_inuse = true;
1893 slot->sl_seqid = seq->seqid;
1894 slot->sl_cachethis = seq->cachethis;
1896 cstate->slot = slot;
1897 cstate->session = session;
1899 out:
1900 /* Hold a session reference until done processing the compound. */
1901 if (cstate->session) {
1902 struct nfs4_client *clp = session->se_client;
1904 nfsd4_get_session(cstate->session);
1905 atomic_inc(&clp->cl_refcount);
1906 if (clp->cl_cb_state == NFSD4_CB_DOWN)
1907 seq->status_flags |= SEQ4_STATUS_CB_PATH_DOWN;
1909 kfree(conn);
1910 spin_unlock(&client_lock);
1911 dprintk("%s: return %d\n", __func__, ntohl(status));
1912 return status;
1915 __be32
1916 nfsd4_reclaim_complete(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate, struct nfsd4_reclaim_complete *rc)
1918 int status = 0;
1920 if (rc->rca_one_fs) {
1921 if (!cstate->current_fh.fh_dentry)
1922 return nfserr_nofilehandle;
1924 * We don't take advantage of the rca_one_fs case.
1925 * That's OK, it's optional, we can safely ignore it.
1927 return nfs_ok;
1930 nfs4_lock_state();
1931 status = nfserr_complete_already;
1932 if (cstate->session->se_client->cl_firststate)
1933 goto out;
1935 status = nfserr_stale_clientid;
1936 if (is_client_expired(cstate->session->se_client))
1938 * The following error isn't really legal.
1939 * But we only get here if the client just explicitly
1940 * destroyed the client. Surely it no longer cares what
1941 * error it gets back on an operation for the dead
1942 * client.
1944 goto out;
1946 status = nfs_ok;
1947 nfsd4_create_clid_dir(cstate->session->se_client);
1948 out:
1949 nfs4_unlock_state();
1950 return status;
1953 __be32
1954 nfsd4_setclientid(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
1955 struct nfsd4_setclientid *setclid)
1957 struct xdr_netobj clname = {
1958 .len = setclid->se_namelen,
1959 .data = setclid->se_name,
1961 nfs4_verifier clverifier = setclid->se_verf;
1962 unsigned int strhashval;
1963 struct nfs4_client *conf, *unconf, *new;
1964 __be32 status;
1965 char dname[HEXDIR_LEN];
1967 if (!check_name(clname))
1968 return nfserr_inval;
1970 status = nfs4_make_rec_clidname(dname, &clname);
1971 if (status)
1972 return status;
1975 * XXX The Duplicate Request Cache (DRC) has been checked (??)
1976 * We get here on a DRC miss.
1979 strhashval = clientstr_hashval(dname);
1981 nfs4_lock_state();
1982 conf = find_confirmed_client_by_str(dname, strhashval);
1983 if (conf) {
1984 /* RFC 3530 14.2.33 CASE 0: */
1985 status = nfserr_clid_inuse;
1986 if (clp_used_exchangeid(conf))
1987 goto out;
1988 if (!same_creds(&conf->cl_cred, &rqstp->rq_cred)) {
1989 char addr_str[INET6_ADDRSTRLEN];
1990 rpc_ntop((struct sockaddr *) &conf->cl_addr, addr_str,
1991 sizeof(addr_str));
1992 dprintk("NFSD: setclientid: string in use by client "
1993 "at %s\n", addr_str);
1994 goto out;
1998 * section 14.2.33 of RFC 3530 (under the heading "IMPLEMENTATION")
1999 * has a description of SETCLIENTID request processing consisting
2000 * of 5 bullet points, labeled as CASE0 - CASE4 below.
2002 unconf = find_unconfirmed_client_by_str(dname, strhashval);
2003 status = nfserr_jukebox;
2004 if (!conf) {
2006 * RFC 3530 14.2.33 CASE 4:
2007 * placed first, because it is the normal case
2009 if (unconf)
2010 expire_client(unconf);
2011 new = create_client(clname, dname, rqstp, &clverifier);
2012 if (new == NULL)
2013 goto out;
2014 gen_clid(new);
2015 } else if (same_verf(&conf->cl_verifier, &clverifier)) {
2017 * RFC 3530 14.2.33 CASE 1:
2018 * probable callback update
2020 if (unconf) {
2021 /* Note this is removing unconfirmed {*x***},
2022 * which is stronger than RFC recommended {vxc**}.
2023 * This has the advantage that there is at most
2024 * one {*x***} in either list at any time.
2026 expire_client(unconf);
2028 new = create_client(clname, dname, rqstp, &clverifier);
2029 if (new == NULL)
2030 goto out;
2031 copy_clid(new, conf);
2032 } else if (!unconf) {
2034 * RFC 3530 14.2.33 CASE 2:
2035 * probable client reboot; state will be removed if
2036 * confirmed.
2038 new = create_client(clname, dname, rqstp, &clverifier);
2039 if (new == NULL)
2040 goto out;
2041 gen_clid(new);
2042 } else {
2044 * RFC 3530 14.2.33 CASE 3:
2045 * probable client reboot; state will be removed if
2046 * confirmed.
2048 expire_client(unconf);
2049 new = create_client(clname, dname, rqstp, &clverifier);
2050 if (new == NULL)
2051 goto out;
2052 gen_clid(new);
2055 * XXX: we should probably set this at creation time, and check
2056 * for consistent minorversion use throughout:
2058 new->cl_minorversion = 0;
2059 gen_callback(new, setclid, rqstp);
2060 add_to_unconfirmed(new, strhashval);
2061 setclid->se_clientid.cl_boot = new->cl_clientid.cl_boot;
2062 setclid->se_clientid.cl_id = new->cl_clientid.cl_id;
2063 memcpy(setclid->se_confirm.data, new->cl_confirm.data, sizeof(setclid->se_confirm.data));
2064 status = nfs_ok;
2065 out:
2066 nfs4_unlock_state();
2067 return status;
2072 * Section 14.2.34 of RFC 3530 (under the heading "IMPLEMENTATION") has
2073 * a description of SETCLIENTID_CONFIRM request processing consisting of 4
2074 * bullets, labeled as CASE1 - CASE4 below.
2076 __be32
2077 nfsd4_setclientid_confirm(struct svc_rqst *rqstp,
2078 struct nfsd4_compound_state *cstate,
2079 struct nfsd4_setclientid_confirm *setclientid_confirm)
2081 struct sockaddr *sa = svc_addr(rqstp);
2082 struct nfs4_client *conf, *unconf;
2083 nfs4_verifier confirm = setclientid_confirm->sc_confirm;
2084 clientid_t * clid = &setclientid_confirm->sc_clientid;
2085 __be32 status;
2087 if (STALE_CLIENTID(clid))
2088 return nfserr_stale_clientid;
2090 * XXX The Duplicate Request Cache (DRC) has been checked (??)
2091 * We get here on a DRC miss.
2094 nfs4_lock_state();
2096 conf = find_confirmed_client(clid);
2097 unconf = find_unconfirmed_client(clid);
2099 status = nfserr_clid_inuse;
2100 if (conf && !rpc_cmp_addr((struct sockaddr *) &conf->cl_addr, sa))
2101 goto out;
2102 if (unconf && !rpc_cmp_addr((struct sockaddr *) &unconf->cl_addr, sa))
2103 goto out;
2106 * section 14.2.34 of RFC 3530 has a description of
2107 * SETCLIENTID_CONFIRM request processing consisting
2108 * of 4 bullet points, labeled as CASE1 - CASE4 below.
2110 if (conf && unconf && same_verf(&confirm, &unconf->cl_confirm)) {
2112 * RFC 3530 14.2.34 CASE 1:
2113 * callback update
2115 if (!same_creds(&conf->cl_cred, &unconf->cl_cred))
2116 status = nfserr_clid_inuse;
2117 else {
2118 nfsd4_change_callback(conf, &unconf->cl_cb_conn);
2119 nfsd4_probe_callback(conf);
2120 expire_client(unconf);
2121 status = nfs_ok;
2124 } else if (conf && !unconf) {
2126 * RFC 3530 14.2.34 CASE 2:
2127 * probable retransmitted request; play it safe and
2128 * do nothing.
2130 if (!same_creds(&conf->cl_cred, &rqstp->rq_cred))
2131 status = nfserr_clid_inuse;
2132 else
2133 status = nfs_ok;
2134 } else if (!conf && unconf
2135 && same_verf(&unconf->cl_confirm, &confirm)) {
2137 * RFC 3530 14.2.34 CASE 3:
2138 * Normal case; new or rebooted client:
2140 if (!same_creds(&unconf->cl_cred, &rqstp->rq_cred)) {
2141 status = nfserr_clid_inuse;
2142 } else {
2143 unsigned int hash =
2144 clientstr_hashval(unconf->cl_recdir);
2145 conf = find_confirmed_client_by_str(unconf->cl_recdir,
2146 hash);
2147 if (conf) {
2148 nfsd4_remove_clid_dir(conf);
2149 expire_client(conf);
2151 move_to_confirmed(unconf);
2152 conf = unconf;
2153 nfsd4_probe_callback(conf);
2154 status = nfs_ok;
2156 } else if ((!conf || (conf && !same_verf(&conf->cl_confirm, &confirm)))
2157 && (!unconf || (unconf && !same_verf(&unconf->cl_confirm,
2158 &confirm)))) {
2160 * RFC 3530 14.2.34 CASE 4:
2161 * Client probably hasn't noticed that we rebooted yet.
2163 status = nfserr_stale_clientid;
2164 } else {
2165 /* check that we have hit one of the cases...*/
2166 status = nfserr_clid_inuse;
2168 out:
2169 nfs4_unlock_state();
2170 return status;
2173 /* OPEN Share state helper functions */
2174 static inline struct nfs4_file *
2175 alloc_init_file(struct inode *ino)
2177 struct nfs4_file *fp;
2178 unsigned int hashval = file_hashval(ino);
2180 fp = kmem_cache_alloc(file_slab, GFP_KERNEL);
2181 if (fp) {
2182 atomic_set(&fp->fi_ref, 1);
2183 INIT_LIST_HEAD(&fp->fi_hash);
2184 INIT_LIST_HEAD(&fp->fi_stateids);
2185 INIT_LIST_HEAD(&fp->fi_delegations);
2186 fp->fi_inode = igrab(ino);
2187 fp->fi_id = current_fileid++;
2188 fp->fi_had_conflict = false;
2189 fp->fi_lease = NULL;
2190 memset(fp->fi_fds, 0, sizeof(fp->fi_fds));
2191 memset(fp->fi_access, 0, sizeof(fp->fi_access));
2192 spin_lock(&recall_lock);
2193 list_add(&fp->fi_hash, &file_hashtbl[hashval]);
2194 spin_unlock(&recall_lock);
2195 return fp;
2197 return NULL;
2200 static void
2201 nfsd4_free_slab(struct kmem_cache **slab)
2203 if (*slab == NULL)
2204 return;
2205 kmem_cache_destroy(*slab);
2206 *slab = NULL;
2209 void
2210 nfsd4_free_slabs(void)
2212 nfsd4_free_slab(&openowner_slab);
2213 nfsd4_free_slab(&lockowner_slab);
2214 nfsd4_free_slab(&file_slab);
2215 nfsd4_free_slab(&stateid_slab);
2216 nfsd4_free_slab(&deleg_slab);
2219 static int
2220 nfsd4_init_slabs(void)
2222 openowner_slab = kmem_cache_create("nfsd4_openowners",
2223 sizeof(struct nfs4_openowner), 0, 0, NULL);
2224 if (openowner_slab == NULL)
2225 goto out_nomem;
2226 lockowner_slab = kmem_cache_create("nfsd4_lockowners",
2227 sizeof(struct nfs4_openowner), 0, 0, NULL);
2228 if (lockowner_slab == NULL)
2229 goto out_nomem;
2230 file_slab = kmem_cache_create("nfsd4_files",
2231 sizeof(struct nfs4_file), 0, 0, NULL);
2232 if (file_slab == NULL)
2233 goto out_nomem;
2234 stateid_slab = kmem_cache_create("nfsd4_stateids",
2235 sizeof(struct nfs4_stateid), 0, 0, NULL);
2236 if (stateid_slab == NULL)
2237 goto out_nomem;
2238 deleg_slab = kmem_cache_create("nfsd4_delegations",
2239 sizeof(struct nfs4_delegation), 0, 0, NULL);
2240 if (deleg_slab == NULL)
2241 goto out_nomem;
2242 return 0;
2243 out_nomem:
2244 nfsd4_free_slabs();
2245 dprintk("nfsd4: out of memory while initializing nfsv4\n");
2246 return -ENOMEM;
2249 void nfs4_free_openowner(struct nfs4_openowner *oo)
2251 kfree(oo->oo_owner.so_owner.data);
2252 kmem_cache_free(openowner_slab, oo);
2255 void nfs4_free_lockowner(struct nfs4_lockowner *lo)
2257 kfree(lo->lo_owner.so_owner.data);
2258 kmem_cache_free(lockowner_slab, lo);
2261 static void init_nfs4_replay(struct nfs4_replay *rp)
2263 rp->rp_status = nfserr_serverfault;
2264 rp->rp_buflen = 0;
2265 rp->rp_buf = rp->rp_ibuf;
2268 static inline void *alloc_stateowner(struct kmem_cache *slab, struct xdr_netobj *owner, struct nfs4_client *clp)
2270 struct nfs4_stateowner *sop;
2272 sop = kmem_cache_alloc(slab, GFP_KERNEL);
2273 if (!sop)
2274 return NULL;
2276 sop->so_owner.data = kmemdup(owner->data, owner->len, GFP_KERNEL);
2277 if (!sop->so_owner.data) {
2278 kmem_cache_free(slab, sop);
2279 return NULL;
2281 sop->so_owner.len = owner->len;
2283 INIT_LIST_HEAD(&sop->so_stateids);
2284 sop->so_id = current_ownerid++;
2285 sop->so_client = clp;
2286 init_nfs4_replay(&sop->so_replay);
2287 return sop;
2290 static void hash_openowner(struct nfs4_openowner *oo, struct nfs4_client *clp, unsigned int strhashval)
2292 unsigned int idhashval;
2294 idhashval = open_ownerid_hashval(oo->oo_owner.so_id);
2295 list_add(&oo->oo_owner.so_idhash, &open_ownerid_hashtbl[idhashval]);
2296 list_add(&oo->oo_owner.so_strhash, &open_ownerstr_hashtbl[strhashval]);
2297 list_add(&oo->oo_perclient, &clp->cl_openowners);
2300 static struct nfs4_openowner *
2301 alloc_init_open_stateowner(unsigned int strhashval, struct nfs4_client *clp, struct nfsd4_open *open) {
2302 struct nfs4_openowner *oo;
2304 oo = alloc_stateowner(openowner_slab, &open->op_owner, clp);
2305 if (!oo)
2306 return NULL;
2307 oo->oo_owner.so_is_open_owner = 1;
2308 oo->oo_owner.so_seqid = open->op_seqid;
2309 oo->oo_confirmed = 0;
2310 oo->oo_time = 0;
2311 INIT_LIST_HEAD(&oo->oo_close_lru);
2312 hash_openowner(oo, clp, strhashval);
2313 return oo;
2316 static inline void
2317 init_stateid(struct nfs4_stateid *stp, struct nfs4_file *fp, struct nfsd4_open *open) {
2318 struct nfs4_openowner *oo = open->op_openowner;
2319 unsigned int hashval = stateid_hashval(oo->oo_owner.so_id, fp->fi_id);
2321 INIT_LIST_HEAD(&stp->st_hash);
2322 INIT_LIST_HEAD(&stp->st_perstateowner);
2323 INIT_LIST_HEAD(&stp->st_lockowners);
2324 INIT_LIST_HEAD(&stp->st_perfile);
2325 list_add(&stp->st_hash, &stateid_hashtbl[hashval]);
2326 list_add(&stp->st_perstateowner, &oo->oo_owner.so_stateids);
2327 list_add(&stp->st_perfile, &fp->fi_stateids);
2328 stp->st_type = NFS4_OPEN_STID;
2329 stp->st_stateowner = &oo->oo_owner;
2330 get_nfs4_file(fp);
2331 stp->st_file = fp;
2332 stp->st_stateid.si_boot = boot_time;
2333 stp->st_stateid.si_stateownerid = oo->oo_owner.so_id;
2334 stp->st_stateid.si_fileid = fp->fi_id;
2335 /* note will be incremented before first return to client: */
2336 stp->st_stateid.si_generation = 0;
2337 stp->st_access_bmap = 0;
2338 stp->st_deny_bmap = 0;
2339 __set_bit(open->op_share_access & ~NFS4_SHARE_WANT_MASK,
2340 &stp->st_access_bmap);
2341 __set_bit(open->op_share_deny, &stp->st_deny_bmap);
2342 stp->st_openstp = NULL;
2345 static void
2346 move_to_close_lru(struct nfs4_openowner *oo)
2348 dprintk("NFSD: move_to_close_lru nfs4_openowner %p\n", oo);
2350 list_move_tail(&oo->oo_close_lru, &close_lru);
2351 oo->oo_time = get_seconds();
2354 static int
2355 same_owner_str(struct nfs4_stateowner *sop, struct xdr_netobj *owner,
2356 clientid_t *clid)
2358 return (sop->so_owner.len == owner->len) &&
2359 0 == memcmp(sop->so_owner.data, owner->data, owner->len) &&
2360 (sop->so_client->cl_clientid.cl_id == clid->cl_id);
2363 static struct nfs4_openowner *
2364 find_openstateowner_str(unsigned int hashval, struct nfsd4_open *open)
2366 struct nfs4_stateowner *so = NULL;
2368 list_for_each_entry(so, &open_ownerstr_hashtbl[hashval], so_strhash) {
2369 if (same_owner_str(so, &open->op_owner, &open->op_clientid))
2370 return container_of(so, struct nfs4_openowner, oo_owner);
2372 return NULL;
2375 /* search file_hashtbl[] for file */
2376 static struct nfs4_file *
2377 find_file(struct inode *ino)
2379 unsigned int hashval = file_hashval(ino);
2380 struct nfs4_file *fp;
2382 spin_lock(&recall_lock);
2383 list_for_each_entry(fp, &file_hashtbl[hashval], fi_hash) {
2384 if (fp->fi_inode == ino) {
2385 get_nfs4_file(fp);
2386 spin_unlock(&recall_lock);
2387 return fp;
2390 spin_unlock(&recall_lock);
2391 return NULL;
2394 static inline int access_valid(u32 x, u32 minorversion)
2396 if ((x & NFS4_SHARE_ACCESS_MASK) < NFS4_SHARE_ACCESS_READ)
2397 return 0;
2398 if ((x & NFS4_SHARE_ACCESS_MASK) > NFS4_SHARE_ACCESS_BOTH)
2399 return 0;
2400 x &= ~NFS4_SHARE_ACCESS_MASK;
2401 if (minorversion && x) {
2402 if ((x & NFS4_SHARE_WANT_MASK) > NFS4_SHARE_WANT_CANCEL)
2403 return 0;
2404 if ((x & NFS4_SHARE_WHEN_MASK) > NFS4_SHARE_PUSH_DELEG_WHEN_UNCONTENDED)
2405 return 0;
2406 x &= ~(NFS4_SHARE_WANT_MASK | NFS4_SHARE_WHEN_MASK);
2408 if (x)
2409 return 0;
2410 return 1;
2413 static inline int deny_valid(u32 x)
2415 /* Note: unlike access bits, deny bits may be zero. */
2416 return x <= NFS4_SHARE_DENY_BOTH;
2420 * Called to check deny when READ with all zero stateid or
2421 * WRITE with all zero or all one stateid
2423 static __be32
2424 nfs4_share_conflict(struct svc_fh *current_fh, unsigned int deny_type)
2426 struct inode *ino = current_fh->fh_dentry->d_inode;
2427 struct nfs4_file *fp;
2428 struct nfs4_stateid *stp;
2429 __be32 ret;
2431 dprintk("NFSD: nfs4_share_conflict\n");
2433 fp = find_file(ino);
2434 if (!fp)
2435 return nfs_ok;
2436 ret = nfserr_locked;
2437 /* Search for conflicting share reservations */
2438 list_for_each_entry(stp, &fp->fi_stateids, st_perfile) {
2439 if (test_bit(deny_type, &stp->st_deny_bmap) ||
2440 test_bit(NFS4_SHARE_DENY_BOTH, &stp->st_deny_bmap))
2441 goto out;
2443 ret = nfs_ok;
2444 out:
2445 put_nfs4_file(fp);
2446 return ret;
2449 static void nfsd_break_one_deleg(struct nfs4_delegation *dp)
2451 /* We're assuming the state code never drops its reference
2452 * without first removing the lease. Since we're in this lease
2453 * callback (and since the lease code is serialized by the kernel
2454 * lock) we know the server hasn't removed the lease yet, we know
2455 * it's safe to take a reference: */
2456 atomic_inc(&dp->dl_count);
2458 list_add_tail(&dp->dl_recall_lru, &del_recall_lru);
2460 /* only place dl_time is set. protected by lock_flocks*/
2461 dp->dl_time = get_seconds();
2463 nfsd4_cb_recall(dp);
2466 /* Called from break_lease() with lock_flocks() held. */
2467 static void nfsd_break_deleg_cb(struct file_lock *fl)
2469 struct nfs4_file *fp = (struct nfs4_file *)fl->fl_owner;
2470 struct nfs4_delegation *dp;
2472 BUG_ON(!fp);
2473 /* We assume break_lease is only called once per lease: */
2474 BUG_ON(fp->fi_had_conflict);
2476 * We don't want the locks code to timeout the lease for us;
2477 * we'll remove it ourself if a delegation isn't returned
2478 * in time:
2480 fl->fl_break_time = 0;
2482 spin_lock(&recall_lock);
2483 fp->fi_had_conflict = true;
2484 list_for_each_entry(dp, &fp->fi_delegations, dl_perfile)
2485 nfsd_break_one_deleg(dp);
2486 spin_unlock(&recall_lock);
2489 static
2490 int nfsd_change_deleg_cb(struct file_lock **onlist, int arg)
2492 if (arg & F_UNLCK)
2493 return lease_modify(onlist, arg);
2494 else
2495 return -EAGAIN;
2498 static const struct lock_manager_operations nfsd_lease_mng_ops = {
2499 .lm_break = nfsd_break_deleg_cb,
2500 .lm_change = nfsd_change_deleg_cb,
2503 static __be32 nfsd4_check_seqid(struct nfsd4_compound_state *cstate, struct nfs4_stateowner *so, u32 seqid)
2505 if (nfsd4_has_session(cstate))
2506 return nfs_ok;
2507 if (seqid == so->so_seqid - 1)
2508 return nfserr_replay_me;
2509 if (seqid == so->so_seqid)
2510 return nfs_ok;
2511 return nfserr_bad_seqid;
2514 __be32
2515 nfsd4_process_open1(struct nfsd4_compound_state *cstate,
2516 struct nfsd4_open *open)
2518 clientid_t *clientid = &open->op_clientid;
2519 struct nfs4_client *clp = NULL;
2520 unsigned int strhashval;
2521 struct nfs4_openowner *oo = NULL;
2522 __be32 status;
2524 if (!check_name(open->op_owner))
2525 return nfserr_inval;
2527 if (STALE_CLIENTID(&open->op_clientid))
2528 return nfserr_stale_clientid;
2530 strhashval = open_ownerstr_hashval(clientid->cl_id, &open->op_owner);
2531 oo = find_openstateowner_str(strhashval, open);
2532 open->op_openowner = oo;
2533 if (!oo) {
2534 /* Make sure the client's lease hasn't expired. */
2535 clp = find_confirmed_client(clientid);
2536 if (clp == NULL)
2537 return nfserr_expired;
2538 goto renew;
2540 if (!oo->oo_confirmed) {
2541 /* Replace unconfirmed owners without checking for replay. */
2542 clp = oo->oo_owner.so_client;
2543 release_openowner(oo);
2544 open->op_openowner = NULL;
2545 goto renew;
2547 status = nfsd4_check_seqid(cstate, &oo->oo_owner, open->op_seqid);
2548 if (status)
2549 return status;
2550 renew:
2551 if (open->op_openowner == NULL) {
2552 oo = alloc_init_open_stateowner(strhashval, clp, open);
2553 if (oo == NULL)
2554 return nfserr_jukebox;
2555 open->op_openowner = oo;
2557 list_del_init(&oo->oo_close_lru);
2558 renew_client(oo->oo_owner.so_client);
2559 return nfs_ok;
2562 static inline __be32
2563 nfs4_check_delegmode(struct nfs4_delegation *dp, int flags)
2565 if ((flags & WR_STATE) && (dp->dl_type == NFS4_OPEN_DELEGATE_READ))
2566 return nfserr_openmode;
2567 else
2568 return nfs_ok;
2571 static struct nfs4_delegation *
2572 find_delegation_file(struct nfs4_file *fp, stateid_t *stid)
2574 struct nfs4_delegation *dp;
2576 spin_lock(&recall_lock);
2577 list_for_each_entry(dp, &fp->fi_delegations, dl_perfile)
2578 if (dp->dl_stateid.si_stateownerid == stid->si_stateownerid) {
2579 spin_unlock(&recall_lock);
2580 return dp;
2582 spin_unlock(&recall_lock);
2583 return NULL;
2586 static int share_access_to_flags(u32 share_access)
2588 share_access &= ~NFS4_SHARE_WANT_MASK;
2590 return share_access == NFS4_SHARE_ACCESS_READ ? RD_STATE : WR_STATE;
2593 static __be32
2594 nfs4_check_deleg(struct nfs4_file *fp, struct nfsd4_open *open,
2595 struct nfs4_delegation **dp)
2597 int flags;
2598 __be32 status = nfserr_bad_stateid;
2600 *dp = find_delegation_file(fp, &open->op_delegate_stateid);
2601 if (*dp == NULL)
2602 goto out;
2603 flags = share_access_to_flags(open->op_share_access);
2604 status = nfs4_check_delegmode(*dp, flags);
2605 if (status)
2606 *dp = NULL;
2607 out:
2608 if (open->op_claim_type != NFS4_OPEN_CLAIM_DELEGATE_CUR)
2609 return nfs_ok;
2610 if (status)
2611 return status;
2612 open->op_openowner->oo_confirmed = 1;
2613 return nfs_ok;
2616 static __be32
2617 nfs4_check_open(struct nfs4_file *fp, struct nfsd4_open *open, struct nfs4_stateid **stpp)
2619 struct nfs4_stateid *local;
2620 struct nfs4_openowner *oo = open->op_openowner;
2622 list_for_each_entry(local, &fp->fi_stateids, st_perfile) {
2623 /* ignore lock owners */
2624 if (local->st_stateowner->so_is_open_owner == 0)
2625 continue;
2626 /* remember if we have seen this open owner */
2627 if (local->st_stateowner == &oo->oo_owner)
2628 *stpp = local;
2629 /* check for conflicting share reservations */
2630 if (!test_share(local, open))
2631 return nfserr_share_denied;
2633 return nfs_ok;
2636 static inline struct nfs4_stateid *
2637 nfs4_alloc_stateid(void)
2639 return kmem_cache_alloc(stateid_slab, GFP_KERNEL);
2642 static inline int nfs4_access_to_access(u32 nfs4_access)
2644 int flags = 0;
2646 if (nfs4_access & NFS4_SHARE_ACCESS_READ)
2647 flags |= NFSD_MAY_READ;
2648 if (nfs4_access & NFS4_SHARE_ACCESS_WRITE)
2649 flags |= NFSD_MAY_WRITE;
2650 return flags;
2653 static __be32 nfs4_get_vfs_file(struct svc_rqst *rqstp, struct nfs4_file *fp,
2654 struct svc_fh *cur_fh, struct nfsd4_open *open)
2656 __be32 status;
2657 int oflag = nfs4_access_to_omode(open->op_share_access);
2658 int access = nfs4_access_to_access(open->op_share_access);
2660 /* CLAIM_DELEGATE_CUR is used in response to a broken lease;
2661 * allowing it to break the lease and return EAGAIN leaves the
2662 * client unable to make progress in returning the delegation */
2663 if (open->op_claim_type == NFS4_OPEN_CLAIM_DELEGATE_CUR)
2664 access |= NFSD_MAY_NOT_BREAK_LEASE;
2666 if (!fp->fi_fds[oflag]) {
2667 status = nfsd_open(rqstp, cur_fh, S_IFREG, access,
2668 &fp->fi_fds[oflag]);
2669 if (status)
2670 return status;
2672 nfs4_file_get_access(fp, oflag);
2674 return nfs_ok;
2677 static __be32
2678 nfs4_new_open(struct svc_rqst *rqstp, struct nfs4_stateid **stpp,
2679 struct nfs4_file *fp, struct svc_fh *cur_fh,
2680 struct nfsd4_open *open)
2682 struct nfs4_stateid *stp;
2683 __be32 status;
2685 stp = nfs4_alloc_stateid();
2686 if (stp == NULL)
2687 return nfserr_jukebox;
2689 status = nfs4_get_vfs_file(rqstp, fp, cur_fh, open);
2690 if (status) {
2691 kmem_cache_free(stateid_slab, stp);
2692 return status;
2694 *stpp = stp;
2695 return 0;
2698 static inline __be32
2699 nfsd4_truncate(struct svc_rqst *rqstp, struct svc_fh *fh,
2700 struct nfsd4_open *open)
2702 struct iattr iattr = {
2703 .ia_valid = ATTR_SIZE,
2704 .ia_size = 0,
2706 if (!open->op_truncate)
2707 return 0;
2708 if (!(open->op_share_access & NFS4_SHARE_ACCESS_WRITE))
2709 return nfserr_inval;
2710 return nfsd_setattr(rqstp, fh, &iattr, 0, (time_t)0);
2713 static __be32
2714 nfs4_upgrade_open(struct svc_rqst *rqstp, struct nfs4_file *fp, struct svc_fh *cur_fh, struct nfs4_stateid *stp, struct nfsd4_open *open)
2716 u32 op_share_access = open->op_share_access & ~NFS4_SHARE_WANT_MASK;
2717 bool new_access;
2718 __be32 status;
2720 new_access = !test_bit(op_share_access, &stp->st_access_bmap);
2721 if (new_access) {
2722 status = nfs4_get_vfs_file(rqstp, fp, cur_fh, open);
2723 if (status)
2724 return status;
2726 status = nfsd4_truncate(rqstp, cur_fh, open);
2727 if (status) {
2728 if (new_access) {
2729 int oflag = nfs4_access_to_omode(op_share_access);
2730 nfs4_file_put_access(fp, oflag);
2732 return status;
2734 /* remember the open */
2735 __set_bit(op_share_access, &stp->st_access_bmap);
2736 __set_bit(open->op_share_deny, &stp->st_deny_bmap);
2738 return nfs_ok;
2742 static void
2743 nfs4_set_claim_prev(struct nfsd4_open *open)
2745 open->op_openowner->oo_confirmed = 1;
2746 open->op_openowner->oo_owner.so_client->cl_firststate = 1;
2749 /* Should we give out recallable state?: */
2750 static bool nfsd4_cb_channel_good(struct nfs4_client *clp)
2752 if (clp->cl_cb_state == NFSD4_CB_UP)
2753 return true;
2755 * In the sessions case, since we don't have to establish a
2756 * separate connection for callbacks, we assume it's OK
2757 * until we hear otherwise:
2759 return clp->cl_minorversion && clp->cl_cb_state == NFSD4_CB_UNKNOWN;
2762 static struct file_lock *nfs4_alloc_init_lease(struct nfs4_delegation *dp, int flag)
2764 struct file_lock *fl;
2766 fl = locks_alloc_lock();
2767 if (!fl)
2768 return NULL;
2769 locks_init_lock(fl);
2770 fl->fl_lmops = &nfsd_lease_mng_ops;
2771 fl->fl_flags = FL_LEASE;
2772 fl->fl_type = flag == NFS4_OPEN_DELEGATE_READ? F_RDLCK: F_WRLCK;
2773 fl->fl_end = OFFSET_MAX;
2774 fl->fl_owner = (fl_owner_t)(dp->dl_file);
2775 fl->fl_pid = current->tgid;
2776 return fl;
2779 static int nfs4_setlease(struct nfs4_delegation *dp, int flag)
2781 struct nfs4_file *fp = dp->dl_file;
2782 struct file_lock *fl;
2783 int status;
2785 fl = nfs4_alloc_init_lease(dp, flag);
2786 if (!fl)
2787 return -ENOMEM;
2788 fl->fl_file = find_readable_file(fp);
2789 list_add(&dp->dl_perclnt, &dp->dl_client->cl_delegations);
2790 status = vfs_setlease(fl->fl_file, fl->fl_type, &fl);
2791 if (status) {
2792 list_del_init(&dp->dl_perclnt);
2793 locks_free_lock(fl);
2794 return -ENOMEM;
2796 fp->fi_lease = fl;
2797 fp->fi_deleg_file = fl->fl_file;
2798 get_file(fp->fi_deleg_file);
2799 atomic_set(&fp->fi_delegees, 1);
2800 list_add(&dp->dl_perfile, &fp->fi_delegations);
2801 return 0;
2804 static int nfs4_set_delegation(struct nfs4_delegation *dp, int flag)
2806 struct nfs4_file *fp = dp->dl_file;
2808 if (!fp->fi_lease)
2809 return nfs4_setlease(dp, flag);
2810 spin_lock(&recall_lock);
2811 if (fp->fi_had_conflict) {
2812 spin_unlock(&recall_lock);
2813 return -EAGAIN;
2815 atomic_inc(&fp->fi_delegees);
2816 list_add(&dp->dl_perfile, &fp->fi_delegations);
2817 spin_unlock(&recall_lock);
2818 list_add(&dp->dl_perclnt, &dp->dl_client->cl_delegations);
2819 return 0;
2823 * Attempt to hand out a delegation.
2825 static void
2826 nfs4_open_delegation(struct svc_fh *fh, struct nfsd4_open *open, struct nfs4_stateid *stp)
2828 struct nfs4_delegation *dp;
2829 struct nfs4_openowner *oo = container_of(stp->st_stateowner, struct nfs4_openowner, oo_owner);
2830 int cb_up;
2831 int status, flag = 0;
2833 cb_up = nfsd4_cb_channel_good(oo->oo_owner.so_client);
2834 flag = NFS4_OPEN_DELEGATE_NONE;
2835 open->op_recall = 0;
2836 switch (open->op_claim_type) {
2837 case NFS4_OPEN_CLAIM_PREVIOUS:
2838 if (!cb_up)
2839 open->op_recall = 1;
2840 flag = open->op_delegate_type;
2841 if (flag == NFS4_OPEN_DELEGATE_NONE)
2842 goto out;
2843 break;
2844 case NFS4_OPEN_CLAIM_NULL:
2845 /* Let's not give out any delegations till everyone's
2846 * had the chance to reclaim theirs.... */
2847 if (locks_in_grace())
2848 goto out;
2849 if (!cb_up || !oo->oo_confirmed)
2850 goto out;
2851 if (open->op_share_access & NFS4_SHARE_ACCESS_WRITE)
2852 flag = NFS4_OPEN_DELEGATE_WRITE;
2853 else
2854 flag = NFS4_OPEN_DELEGATE_READ;
2855 break;
2856 default:
2857 goto out;
2860 dp = alloc_init_deleg(oo->oo_owner.so_client, stp, fh, flag);
2861 if (dp == NULL)
2862 goto out_no_deleg;
2863 status = nfs4_set_delegation(dp, flag);
2864 if (status)
2865 goto out_free;
2867 memcpy(&open->op_delegate_stateid, &dp->dl_stateid, sizeof(dp->dl_stateid));
2869 dprintk("NFSD: delegation stateid=" STATEID_FMT "\n",
2870 STATEID_VAL(&dp->dl_stateid));
2871 out:
2872 if (open->op_claim_type == NFS4_OPEN_CLAIM_PREVIOUS
2873 && flag == NFS4_OPEN_DELEGATE_NONE
2874 && open->op_delegate_type != NFS4_OPEN_DELEGATE_NONE)
2875 dprintk("NFSD: WARNING: refusing delegation reclaim\n");
2876 open->op_delegate_type = flag;
2877 return;
2878 out_free:
2879 nfs4_put_delegation(dp);
2880 out_no_deleg:
2881 flag = NFS4_OPEN_DELEGATE_NONE;
2882 goto out;
2886 * called with nfs4_lock_state() held.
2888 __be32
2889 nfsd4_process_open2(struct svc_rqst *rqstp, struct svc_fh *current_fh, struct nfsd4_open *open)
2891 struct nfsd4_compoundres *resp = rqstp->rq_resp;
2892 struct nfs4_file *fp = NULL;
2893 struct inode *ino = current_fh->fh_dentry->d_inode;
2894 struct nfs4_stateid *stp = NULL;
2895 struct nfs4_delegation *dp = NULL;
2896 __be32 status;
2898 status = nfserr_inval;
2899 if (!access_valid(open->op_share_access, resp->cstate.minorversion)
2900 || !deny_valid(open->op_share_deny))
2901 goto out;
2903 * Lookup file; if found, lookup stateid and check open request,
2904 * and check for delegations in the process of being recalled.
2905 * If not found, create the nfs4_file struct
2907 fp = find_file(ino);
2908 if (fp) {
2909 if ((status = nfs4_check_open(fp, open, &stp)))
2910 goto out;
2911 status = nfs4_check_deleg(fp, open, &dp);
2912 if (status)
2913 goto out;
2914 } else {
2915 status = nfserr_bad_stateid;
2916 if (open->op_claim_type == NFS4_OPEN_CLAIM_DELEGATE_CUR)
2917 goto out;
2918 status = nfserr_jukebox;
2919 fp = alloc_init_file(ino);
2920 if (fp == NULL)
2921 goto out;
2925 * OPEN the file, or upgrade an existing OPEN.
2926 * If truncate fails, the OPEN fails.
2928 if (stp) {
2929 /* Stateid was found, this is an OPEN upgrade */
2930 status = nfs4_upgrade_open(rqstp, fp, current_fh, stp, open);
2931 if (status)
2932 goto out;
2933 } else {
2934 status = nfs4_new_open(rqstp, &stp, fp, current_fh, open);
2935 if (status)
2936 goto out;
2937 init_stateid(stp, fp, open);
2938 status = nfsd4_truncate(rqstp, current_fh, open);
2939 if (status) {
2940 release_open_stateid(stp);
2941 goto out;
2944 update_stateid(&stp->st_stateid);
2945 memcpy(&open->op_stateid, &stp->st_stateid, sizeof(stateid_t));
2947 if (nfsd4_has_session(&resp->cstate))
2948 open->op_openowner->oo_confirmed = 1;
2951 * Attempt to hand out a delegation. No error return, because the
2952 * OPEN succeeds even if we fail.
2954 nfs4_open_delegation(current_fh, open, stp);
2956 status = nfs_ok;
2958 dprintk("%s: stateid=" STATEID_FMT "\n", __func__,
2959 STATEID_VAL(&stp->st_stateid));
2960 out:
2961 if (fp)
2962 put_nfs4_file(fp);
2963 if (status == 0 && open->op_claim_type == NFS4_OPEN_CLAIM_PREVIOUS)
2964 nfs4_set_claim_prev(open);
2966 * To finish the open response, we just need to set the rflags.
2968 open->op_rflags = NFS4_OPEN_RESULT_LOCKTYPE_POSIX;
2969 if (!open->op_openowner->oo_confirmed &&
2970 !nfsd4_has_session(&resp->cstate))
2971 open->op_rflags |= NFS4_OPEN_RESULT_CONFIRM;
2973 return status;
2976 __be32
2977 nfsd4_renew(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
2978 clientid_t *clid)
2980 struct nfs4_client *clp;
2981 __be32 status;
2983 nfs4_lock_state();
2984 dprintk("process_renew(%08x/%08x): starting\n",
2985 clid->cl_boot, clid->cl_id);
2986 status = nfserr_stale_clientid;
2987 if (STALE_CLIENTID(clid))
2988 goto out;
2989 clp = find_confirmed_client(clid);
2990 status = nfserr_expired;
2991 if (clp == NULL) {
2992 /* We assume the client took too long to RENEW. */
2993 dprintk("nfsd4_renew: clientid not found!\n");
2994 goto out;
2996 renew_client(clp);
2997 status = nfserr_cb_path_down;
2998 if (!list_empty(&clp->cl_delegations)
2999 && clp->cl_cb_state != NFSD4_CB_UP)
3000 goto out;
3001 status = nfs_ok;
3002 out:
3003 nfs4_unlock_state();
3004 return status;
3007 static struct lock_manager nfsd4_manager = {
3010 static void
3011 nfsd4_end_grace(void)
3013 dprintk("NFSD: end of grace period\n");
3014 nfsd4_recdir_purge_old();
3015 locks_end_grace(&nfsd4_manager);
3017 * Now that every NFSv4 client has had the chance to recover and
3018 * to see the (possibly new, possibly shorter) lease time, we
3019 * can safely set the next grace time to the current lease time:
3021 nfsd4_grace = nfsd4_lease;
3024 static time_t
3025 nfs4_laundromat(void)
3027 struct nfs4_client *clp;
3028 struct nfs4_openowner *oo;
3029 struct nfs4_delegation *dp;
3030 struct list_head *pos, *next, reaplist;
3031 time_t cutoff = get_seconds() - nfsd4_lease;
3032 time_t t, clientid_val = nfsd4_lease;
3033 time_t u, test_val = nfsd4_lease;
3035 nfs4_lock_state();
3037 dprintk("NFSD: laundromat service - starting\n");
3038 if (locks_in_grace())
3039 nfsd4_end_grace();
3040 INIT_LIST_HEAD(&reaplist);
3041 spin_lock(&client_lock);
3042 list_for_each_safe(pos, next, &client_lru) {
3043 clp = list_entry(pos, struct nfs4_client, cl_lru);
3044 if (time_after((unsigned long)clp->cl_time, (unsigned long)cutoff)) {
3045 t = clp->cl_time - cutoff;
3046 if (clientid_val > t)
3047 clientid_val = t;
3048 break;
3050 if (atomic_read(&clp->cl_refcount)) {
3051 dprintk("NFSD: client in use (clientid %08x)\n",
3052 clp->cl_clientid.cl_id);
3053 continue;
3055 unhash_client_locked(clp);
3056 list_add(&clp->cl_lru, &reaplist);
3058 spin_unlock(&client_lock);
3059 list_for_each_safe(pos, next, &reaplist) {
3060 clp = list_entry(pos, struct nfs4_client, cl_lru);
3061 dprintk("NFSD: purging unused client (clientid %08x)\n",
3062 clp->cl_clientid.cl_id);
3063 nfsd4_remove_clid_dir(clp);
3064 expire_client(clp);
3066 spin_lock(&recall_lock);
3067 list_for_each_safe(pos, next, &del_recall_lru) {
3068 dp = list_entry (pos, struct nfs4_delegation, dl_recall_lru);
3069 if (time_after((unsigned long)dp->dl_time, (unsigned long)cutoff)) {
3070 u = dp->dl_time - cutoff;
3071 if (test_val > u)
3072 test_val = u;
3073 break;
3075 list_move(&dp->dl_recall_lru, &reaplist);
3077 spin_unlock(&recall_lock);
3078 list_for_each_safe(pos, next, &reaplist) {
3079 dp = list_entry (pos, struct nfs4_delegation, dl_recall_lru);
3080 list_del_init(&dp->dl_recall_lru);
3081 unhash_delegation(dp);
3083 test_val = nfsd4_lease;
3084 list_for_each_safe(pos, next, &close_lru) {
3085 oo = container_of(pos, struct nfs4_openowner, oo_close_lru);
3086 if (time_after((unsigned long)oo->oo_time, (unsigned long)cutoff)) {
3087 u = oo->oo_time - cutoff;
3088 if (test_val > u)
3089 test_val = u;
3090 break;
3092 dprintk("NFSD: purging unused open stateowner (so_id %d)\n",
3093 oo->oo_owner.so_id);
3094 release_openowner(oo);
3096 if (clientid_val < NFSD_LAUNDROMAT_MINTIMEOUT)
3097 clientid_val = NFSD_LAUNDROMAT_MINTIMEOUT;
3098 nfs4_unlock_state();
3099 return clientid_val;
3102 static struct workqueue_struct *laundry_wq;
3103 static void laundromat_main(struct work_struct *);
3104 static DECLARE_DELAYED_WORK(laundromat_work, laundromat_main);
3106 static void
3107 laundromat_main(struct work_struct *not_used)
3109 time_t t;
3111 t = nfs4_laundromat();
3112 dprintk("NFSD: laundromat_main - sleeping for %ld seconds\n", t);
3113 queue_delayed_work(laundry_wq, &laundromat_work, t*HZ);
3116 static struct nfs4_openowner * search_close_lru(u32 st_id)
3118 struct nfs4_openowner *local;
3120 list_for_each_entry(local, &close_lru, oo_close_lru) {
3121 if (local->oo_owner.so_id == st_id)
3122 return local;
3124 return NULL;
3127 static inline int
3128 nfs4_check_fh(struct svc_fh *fhp, struct nfs4_stateid *stp)
3130 return fhp->fh_dentry->d_inode != stp->st_file->fi_inode;
3133 static int
3134 STALE_STATEID(stateid_t *stateid)
3136 if (stateid->si_boot == boot_time)
3137 return 0;
3138 dprintk("NFSD: stale stateid " STATEID_FMT "!\n",
3139 STATEID_VAL(stateid));
3140 return 1;
3143 static inline int
3144 access_permit_read(unsigned long access_bmap)
3146 return test_bit(NFS4_SHARE_ACCESS_READ, &access_bmap) ||
3147 test_bit(NFS4_SHARE_ACCESS_BOTH, &access_bmap) ||
3148 test_bit(NFS4_SHARE_ACCESS_WRITE, &access_bmap);
3151 static inline int
3152 access_permit_write(unsigned long access_bmap)
3154 return test_bit(NFS4_SHARE_ACCESS_WRITE, &access_bmap) ||
3155 test_bit(NFS4_SHARE_ACCESS_BOTH, &access_bmap);
3158 static
3159 __be32 nfs4_check_openmode(struct nfs4_stateid *stp, int flags)
3161 __be32 status = nfserr_openmode;
3163 /* For lock stateid's, we test the parent open, not the lock: */
3164 if (stp->st_openstp)
3165 stp = stp->st_openstp;
3166 if ((flags & WR_STATE) && (!access_permit_write(stp->st_access_bmap)))
3167 goto out;
3168 if ((flags & RD_STATE) && (!access_permit_read(stp->st_access_bmap)))
3169 goto out;
3170 status = nfs_ok;
3171 out:
3172 return status;
3175 static inline __be32
3176 check_special_stateids(svc_fh *current_fh, stateid_t *stateid, int flags)
3178 if (ONE_STATEID(stateid) && (flags & RD_STATE))
3179 return nfs_ok;
3180 else if (locks_in_grace()) {
3181 /* Answer in remaining cases depends on existence of
3182 * conflicting state; so we must wait out the grace period. */
3183 return nfserr_grace;
3184 } else if (flags & WR_STATE)
3185 return nfs4_share_conflict(current_fh,
3186 NFS4_SHARE_DENY_WRITE);
3187 else /* (flags & RD_STATE) && ZERO_STATEID(stateid) */
3188 return nfs4_share_conflict(current_fh,
3189 NFS4_SHARE_DENY_READ);
3193 * Allow READ/WRITE during grace period on recovered state only for files
3194 * that are not able to provide mandatory locking.
3196 static inline int
3197 grace_disallows_io(struct inode *inode)
3199 return locks_in_grace() && mandatory_lock(inode);
3202 /* Returns true iff a is later than b: */
3203 static bool stateid_generation_after(stateid_t *a, stateid_t *b)
3205 return (s32)a->si_generation - (s32)b->si_generation > 0;
3208 static int check_stateid_generation(stateid_t *in, stateid_t *ref, bool has_session)
3211 * When sessions are used the stateid generation number is ignored
3212 * when it is zero.
3214 if (has_session && in->si_generation == 0)
3215 return nfs_ok;
3217 if (in->si_generation == ref->si_generation)
3218 return nfs_ok;
3220 /* If the client sends us a stateid from the future, it's buggy: */
3221 if (stateid_generation_after(in, ref))
3222 return nfserr_bad_stateid;
3224 * However, we could see a stateid from the past, even from a
3225 * non-buggy client. For example, if the client sends a lock
3226 * while some IO is outstanding, the lock may bump si_generation
3227 * while the IO is still in flight. The client could avoid that
3228 * situation by waiting for responses on all the IO requests,
3229 * but better performance may result in retrying IO that
3230 * receives an old_stateid error if requests are rarely
3231 * reordered in flight:
3233 return nfserr_old_stateid;
3236 static int is_delegation_stateid(stateid_t *stateid)
3238 return stateid->si_fileid == 0;
3241 __be32 nfs4_validate_stateid(stateid_t *stateid, bool has_session)
3243 struct nfs4_stateid *stp = NULL;
3244 __be32 status = nfserr_stale_stateid;
3246 if (STALE_STATEID(stateid))
3247 goto out;
3249 status = nfserr_expired;
3250 stp = find_stateid(stateid);
3251 if (!stp)
3252 goto out;
3253 status = nfserr_bad_stateid;
3254 if (stp->st_stateowner->so_is_open_owner
3255 && !openowner(stp->st_stateowner)->oo_confirmed)
3256 goto out;
3258 status = check_stateid_generation(stateid, &stp->st_stateid, has_session);
3259 if (status)
3260 goto out;
3262 status = nfs_ok;
3263 out:
3264 return status;
3268 * Checks for stateid operations
3270 __be32
3271 nfs4_preprocess_stateid_op(struct nfsd4_compound_state *cstate,
3272 stateid_t *stateid, int flags, struct file **filpp)
3274 struct nfs4_stateid *stp = NULL;
3275 struct nfs4_delegation *dp = NULL;
3276 struct svc_fh *current_fh = &cstate->current_fh;
3277 struct inode *ino = current_fh->fh_dentry->d_inode;
3278 __be32 status;
3280 if (filpp)
3281 *filpp = NULL;
3283 if (grace_disallows_io(ino))
3284 return nfserr_grace;
3286 if (ZERO_STATEID(stateid) || ONE_STATEID(stateid))
3287 return check_special_stateids(current_fh, stateid, flags);
3289 status = nfserr_stale_stateid;
3290 if (STALE_STATEID(stateid))
3291 goto out;
3294 * We assume that any stateid that has the current boot time,
3295 * but that we can't find, is expired:
3297 status = nfserr_expired;
3298 if (is_delegation_stateid(stateid)) {
3299 dp = find_delegation_stateid(ino, stateid);
3300 if (!dp)
3301 goto out;
3302 status = check_stateid_generation(stateid, &dp->dl_stateid, nfsd4_has_session(cstate));
3303 if (status)
3304 goto out;
3305 status = nfs4_check_delegmode(dp, flags);
3306 if (status)
3307 goto out;
3308 renew_client(dp->dl_client);
3309 if (filpp) {
3310 *filpp = dp->dl_file->fi_deleg_file;
3311 BUG_ON(!*filpp);
3313 } else { /* open or lock stateid */
3314 stp = find_stateid(stateid);
3315 if (!stp)
3316 goto out;
3317 status = nfserr_bad_stateid;
3318 if (nfs4_check_fh(current_fh, stp))
3319 goto out;
3320 if (stp->st_stateowner->so_is_open_owner
3321 && !openowner(stp->st_stateowner)->oo_confirmed)
3322 goto out;
3323 status = check_stateid_generation(stateid, &stp->st_stateid,
3324 nfsd4_has_session(cstate));
3325 if (status)
3326 goto out;
3327 status = nfs4_check_openmode(stp, flags);
3328 if (status)
3329 goto out;
3330 renew_client(stp->st_stateowner->so_client);
3331 if (filpp) {
3332 if (flags & RD_STATE)
3333 *filpp = find_readable_file(stp->st_file);
3334 else
3335 *filpp = find_writeable_file(stp->st_file);
3338 status = nfs_ok;
3339 out:
3340 return status;
3343 static __be32
3344 nfsd4_free_delegation_stateid(stateid_t *stateid)
3346 struct nfs4_delegation *dp = search_for_delegation(stateid);
3347 if (dp)
3348 return nfserr_locks_held;
3349 return nfserr_bad_stateid;
3352 static __be32
3353 nfsd4_free_lock_stateid(struct nfs4_stateid *stp)
3355 if (check_for_locks(stp->st_file, lockowner(stp->st_stateowner)))
3356 return nfserr_locks_held;
3357 release_lock_stateid(stp);
3358 return nfs_ok;
3362 * Test if the stateid is valid
3364 __be32
3365 nfsd4_test_stateid(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
3366 struct nfsd4_test_stateid *test_stateid)
3368 test_stateid->ts_has_session = nfsd4_has_session(cstate);
3369 return nfs_ok;
3373 * Free a state id
3375 __be32
3376 nfsd4_free_stateid(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
3377 struct nfsd4_free_stateid *free_stateid)
3379 stateid_t *stateid = &free_stateid->fr_stateid;
3380 struct nfs4_stateid *stp;
3381 __be32 ret;
3383 nfs4_lock_state();
3384 if (is_delegation_stateid(stateid)) {
3385 ret = nfsd4_free_delegation_stateid(stateid);
3386 goto out;
3389 stp = find_stateid(stateid);
3390 if (!stp) {
3391 ret = nfserr_bad_stateid;
3392 goto out;
3394 ret = check_stateid_generation(stateid, &stp->st_stateid, 1);
3395 if (ret)
3396 goto out;
3398 if (stp->st_type == NFS4_OPEN_STID) {
3399 ret = nfserr_locks_held;
3400 goto out;
3401 } else {
3402 ret = nfsd4_free_lock_stateid(stp);
3403 goto out;
3406 out:
3407 nfs4_unlock_state();
3408 return ret;
3411 static inline int
3412 setlkflg (int type)
3414 return (type == NFS4_READW_LT || type == NFS4_READ_LT) ?
3415 RD_STATE : WR_STATE;
3418 static __be32 nfs4_nospecial_stateid_checks(stateid_t *stateid)
3420 if (ZERO_STATEID(stateid) || ONE_STATEID(stateid))
3421 return nfserr_bad_stateid;
3422 if (STALE_STATEID(stateid))
3423 return nfserr_stale_stateid;
3424 return nfs_ok;
3427 static __be32 nfs4_seqid_op_checks(struct nfsd4_compound_state *cstate, stateid_t *stateid, u32 seqid, struct nfs4_stateid *stp)
3429 struct svc_fh *current_fh = &cstate->current_fh;
3430 struct nfs4_stateowner *sop = stp->st_stateowner;
3431 __be32 status;
3433 if (nfs4_check_fh(current_fh, stp))
3434 return nfserr_bad_stateid;
3435 status = nfsd4_check_seqid(cstate, sop, seqid);
3436 if (status)
3437 return status;
3438 return check_stateid_generation(stateid, &stp->st_stateid, nfsd4_has_session(cstate));
3442 * Checks for sequence id mutating operations.
3444 static __be32
3445 nfs4_preprocess_seqid_op(struct nfsd4_compound_state *cstate, u32 seqid,
3446 stateid_t *stateid, char typemask,
3447 struct nfs4_stateid **stpp)
3449 __be32 status;
3451 dprintk("NFSD: %s: seqid=%d stateid = " STATEID_FMT "\n", __func__,
3452 seqid, STATEID_VAL(stateid));
3454 *stpp = NULL;
3455 status = nfs4_nospecial_stateid_checks(stateid);
3456 if (status)
3457 return status;
3458 *stpp = find_stateid_by_type(stateid, typemask);
3459 if (*stpp == NULL)
3460 return nfserr_expired;
3461 cstate->replay_owner = (*stpp)->st_stateowner;
3462 renew_client((*stpp)->st_stateowner->so_client);
3464 return nfs4_seqid_op_checks(cstate, stateid, seqid, *stpp);
3467 static __be32 nfs4_preprocess_confirmed_seqid_op(struct nfsd4_compound_state *cstate, u32 seqid, stateid_t *stateid, struct nfs4_stateid **stpp)
3469 __be32 status;
3470 struct nfs4_openowner *oo;
3472 status = nfs4_preprocess_seqid_op(cstate, seqid, stateid,
3473 NFS4_OPEN_STID, stpp);
3474 if (status)
3475 return status;
3476 oo = openowner((*stpp)->st_stateowner);
3477 if (!oo->oo_confirmed)
3478 return nfserr_bad_stateid;
3479 return nfs_ok;
3482 __be32
3483 nfsd4_open_confirm(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
3484 struct nfsd4_open_confirm *oc)
3486 __be32 status;
3487 struct nfs4_openowner *oo;
3488 struct nfs4_stateid *stp;
3490 dprintk("NFSD: nfsd4_open_confirm on file %.*s\n",
3491 (int)cstate->current_fh.fh_dentry->d_name.len,
3492 cstate->current_fh.fh_dentry->d_name.name);
3494 status = fh_verify(rqstp, &cstate->current_fh, S_IFREG, 0);
3495 if (status)
3496 return status;
3498 nfs4_lock_state();
3500 status = nfs4_preprocess_seqid_op(cstate,
3501 oc->oc_seqid, &oc->oc_req_stateid,
3502 NFS4_OPEN_STID, &stp);
3503 if (status)
3504 goto out;
3505 oo = openowner(stp->st_stateowner);
3506 status = nfserr_bad_stateid;
3507 if (oo->oo_confirmed)
3508 goto out;
3509 oo->oo_confirmed = 1;
3510 update_stateid(&stp->st_stateid);
3511 memcpy(&oc->oc_resp_stateid, &stp->st_stateid, sizeof(stateid_t));
3512 dprintk("NFSD: %s: success, seqid=%d stateid=" STATEID_FMT "\n",
3513 __func__, oc->oc_seqid, STATEID_VAL(&stp->st_stateid));
3515 nfsd4_create_clid_dir(oo->oo_owner.so_client);
3516 status = nfs_ok;
3517 out:
3518 if (!cstate->replay_owner)
3519 nfs4_unlock_state();
3520 return status;
3523 static inline void nfs4_file_downgrade(struct nfs4_stateid *stp, unsigned int to_access)
3525 int i;
3527 for (i = 1; i < 4; i++) {
3528 if (test_bit(i, &stp->st_access_bmap) && !(i & to_access)) {
3529 nfs4_file_put_access(stp->st_file, i);
3530 __clear_bit(i, &stp->st_access_bmap);
3535 static void
3536 reset_union_bmap_deny(unsigned long deny, unsigned long *bmap)
3538 int i;
3539 for (i = 0; i < 4; i++) {
3540 if ((i & deny) != i)
3541 __clear_bit(i, bmap);
3545 __be32
3546 nfsd4_open_downgrade(struct svc_rqst *rqstp,
3547 struct nfsd4_compound_state *cstate,
3548 struct nfsd4_open_downgrade *od)
3550 __be32 status;
3551 struct nfs4_stateid *stp;
3553 dprintk("NFSD: nfsd4_open_downgrade on file %.*s\n",
3554 (int)cstate->current_fh.fh_dentry->d_name.len,
3555 cstate->current_fh.fh_dentry->d_name.name);
3557 if (!access_valid(od->od_share_access, cstate->minorversion)
3558 || !deny_valid(od->od_share_deny))
3559 return nfserr_inval;
3561 nfs4_lock_state();
3562 status = nfs4_preprocess_confirmed_seqid_op(cstate, od->od_seqid,
3563 &od->od_stateid, &stp);
3564 if (status)
3565 goto out;
3566 status = nfserr_inval;
3567 if (!test_bit(od->od_share_access, &stp->st_access_bmap)) {
3568 dprintk("NFSD:access not a subset current bitmap: 0x%lx, input access=%08x\n",
3569 stp->st_access_bmap, od->od_share_access);
3570 goto out;
3572 if (!test_bit(od->od_share_deny, &stp->st_deny_bmap)) {
3573 dprintk("NFSD:deny not a subset current bitmap: 0x%lx, input deny=%08x\n",
3574 stp->st_deny_bmap, od->od_share_deny);
3575 goto out;
3577 nfs4_file_downgrade(stp, od->od_share_access);
3579 reset_union_bmap_deny(od->od_share_deny, &stp->st_deny_bmap);
3581 update_stateid(&stp->st_stateid);
3582 memcpy(&od->od_stateid, &stp->st_stateid, sizeof(stateid_t));
3583 status = nfs_ok;
3584 out:
3585 if (!cstate->replay_owner)
3586 nfs4_unlock_state();
3587 return status;
3591 * nfs4_unlock_state() called after encode
3593 __be32
3594 nfsd4_close(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
3595 struct nfsd4_close *close)
3597 __be32 status;
3598 struct nfs4_openowner *oo;
3599 struct nfs4_stateid *stp;
3601 dprintk("NFSD: nfsd4_close on file %.*s\n",
3602 (int)cstate->current_fh.fh_dentry->d_name.len,
3603 cstate->current_fh.fh_dentry->d_name.name);
3605 nfs4_lock_state();
3606 /* check close_lru for replay */
3607 status = nfs4_preprocess_confirmed_seqid_op(cstate, close->cl_seqid,
3608 &close->cl_stateid, &stp);
3609 if (stp == NULL && status == nfserr_expired) {
3611 * Also, we should make sure this isn't just the result of
3612 * a replayed close:
3614 oo = search_close_lru(close->cl_stateid.si_stateownerid);
3615 /* It's not stale; let's assume it's expired: */
3616 if (oo == NULL)
3617 goto out;
3618 cstate->replay_owner = &oo->oo_owner;
3619 status = nfsd4_check_seqid(cstate, &oo->oo_owner, close->cl_seqid);
3620 if (status)
3621 goto out;
3622 status = nfserr_bad_seqid;
3624 if (status)
3625 goto out;
3626 oo = openowner(stp->st_stateowner);
3627 status = nfs_ok;
3628 update_stateid(&stp->st_stateid);
3629 memcpy(&close->cl_stateid, &stp->st_stateid, sizeof(stateid_t));
3631 /* release_stateid() calls nfsd_close() if needed */
3632 release_open_stateid(stp);
3634 /* place unused nfs4_stateowners on so_close_lru list to be
3635 * released by the laundromat service after the lease period
3636 * to enable us to handle CLOSE replay
3638 if (list_empty(&oo->oo_owner.so_stateids))
3639 move_to_close_lru(oo);
3640 out:
3641 if (!cstate->replay_owner)
3642 nfs4_unlock_state();
3643 return status;
3646 __be32
3647 nfsd4_delegreturn(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
3648 struct nfsd4_delegreturn *dr)
3650 struct nfs4_delegation *dp;
3651 stateid_t *stateid = &dr->dr_stateid;
3652 struct inode *inode;
3653 __be32 status;
3655 if ((status = fh_verify(rqstp, &cstate->current_fh, S_IFREG, 0)))
3656 return status;
3657 inode = cstate->current_fh.fh_dentry->d_inode;
3659 nfs4_lock_state();
3660 status = nfserr_bad_stateid;
3661 if (ZERO_STATEID(stateid) || ONE_STATEID(stateid))
3662 goto out;
3663 status = nfserr_stale_stateid;
3664 if (STALE_STATEID(stateid))
3665 goto out;
3666 status = nfserr_bad_stateid;
3667 if (!is_delegation_stateid(stateid))
3668 goto out;
3669 status = nfserr_expired;
3670 dp = find_delegation_stateid(inode, stateid);
3671 if (!dp)
3672 goto out;
3673 status = check_stateid_generation(stateid, &dp->dl_stateid, nfsd4_has_session(cstate));
3674 if (status)
3675 goto out;
3676 renew_client(dp->dl_client);
3678 unhash_delegation(dp);
3679 out:
3680 nfs4_unlock_state();
3682 return status;
3687 * Lock owner state (byte-range locks)
3689 #define LOFF_OVERFLOW(start, len) ((u64)(len) > ~(u64)(start))
3690 #define LOCK_HASH_BITS 8
3691 #define LOCK_HASH_SIZE (1 << LOCK_HASH_BITS)
3692 #define LOCK_HASH_MASK (LOCK_HASH_SIZE - 1)
3694 static inline u64
3695 end_offset(u64 start, u64 len)
3697 u64 end;
3699 end = start + len;
3700 return end >= start ? end: NFS4_MAX_UINT64;
3703 /* last octet in a range */
3704 static inline u64
3705 last_byte_offset(u64 start, u64 len)
3707 u64 end;
3709 BUG_ON(!len);
3710 end = start + len;
3711 return end > start ? end - 1: NFS4_MAX_UINT64;
3714 static unsigned int lockownerid_hashval(u32 id)
3716 return id & LOCK_HASH_MASK;
3719 static inline unsigned int
3720 lock_ownerstr_hashval(struct inode *inode, u32 cl_id,
3721 struct xdr_netobj *ownername)
3723 return (file_hashval(inode) + cl_id
3724 + opaque_hashval(ownername->data, ownername->len))
3725 & LOCK_HASH_MASK;
3728 static struct list_head lock_ownerid_hashtbl[LOCK_HASH_SIZE];
3729 static struct list_head lock_ownerstr_hashtbl[LOCK_HASH_SIZE];
3731 static struct nfs4_delegation *
3732 search_for_delegation(stateid_t *stid)
3734 struct nfs4_file *fp;
3735 struct nfs4_delegation *dp;
3736 struct list_head *pos;
3737 int i;
3739 for (i = 0; i < FILE_HASH_SIZE; i++) {
3740 list_for_each_entry(fp, &file_hashtbl[i], fi_hash) {
3741 list_for_each(pos, &fp->fi_delegations) {
3742 dp = list_entry(pos, struct nfs4_delegation, dl_perfile);
3743 if (same_stateid(&dp->dl_stateid, stid))
3744 return dp;
3748 return NULL;
3751 static struct nfs4_delegation *
3752 find_delegation_stateid(struct inode *ino, stateid_t *stid)
3754 struct nfs4_file *fp;
3755 struct nfs4_delegation *dl;
3757 dprintk("NFSD: %s: stateid=" STATEID_FMT "\n", __func__,
3758 STATEID_VAL(stid));
3760 fp = find_file(ino);
3761 if (!fp)
3762 return NULL;
3763 dl = find_delegation_file(fp, stid);
3764 put_nfs4_file(fp);
3765 return dl;
3769 * TODO: Linux file offsets are _signed_ 64-bit quantities, which means that
3770 * we can't properly handle lock requests that go beyond the (2^63 - 1)-th
3771 * byte, because of sign extension problems. Since NFSv4 calls for 64-bit
3772 * locking, this prevents us from being completely protocol-compliant. The
3773 * real solution to this problem is to start using unsigned file offsets in
3774 * the VFS, but this is a very deep change!
3776 static inline void
3777 nfs4_transform_lock_offset(struct file_lock *lock)
3779 if (lock->fl_start < 0)
3780 lock->fl_start = OFFSET_MAX;
3781 if (lock->fl_end < 0)
3782 lock->fl_end = OFFSET_MAX;
3785 /* Hack!: For now, we're defining this just so we can use a pointer to it
3786 * as a unique cookie to identify our (NFSv4's) posix locks. */
3787 static const struct lock_manager_operations nfsd_posix_mng_ops = {
3790 static inline void
3791 nfs4_set_lock_denied(struct file_lock *fl, struct nfsd4_lock_denied *deny)
3793 struct nfs4_lockowner *lo;
3795 if (fl->fl_lmops == &nfsd_posix_mng_ops) {
3796 lo = (struct nfs4_lockowner *) fl->fl_owner;
3797 deny->ld_owner.data = kmemdup(lo->lo_owner.so_owner.data,
3798 lo->lo_owner.so_owner.len, GFP_KERNEL);
3799 if (!deny->ld_owner.data)
3800 /* We just don't care that much */
3801 goto nevermind;
3802 deny->ld_owner.len = lo->lo_owner.so_owner.len;
3803 deny->ld_clientid = lo->lo_owner.so_client->cl_clientid;
3804 } else {
3805 nevermind:
3806 deny->ld_owner.len = 0;
3807 deny->ld_owner.data = NULL;
3808 deny->ld_clientid.cl_boot = 0;
3809 deny->ld_clientid.cl_id = 0;
3811 deny->ld_start = fl->fl_start;
3812 deny->ld_length = NFS4_MAX_UINT64;
3813 if (fl->fl_end != NFS4_MAX_UINT64)
3814 deny->ld_length = fl->fl_end - fl->fl_start + 1;
3815 deny->ld_type = NFS4_READ_LT;
3816 if (fl->fl_type != F_RDLCK)
3817 deny->ld_type = NFS4_WRITE_LT;
3820 static struct nfs4_lockowner *
3821 find_lockowner_str(struct inode *inode, clientid_t *clid,
3822 struct xdr_netobj *owner)
3824 unsigned int hashval = lock_ownerstr_hashval(inode, clid->cl_id, owner);
3825 struct nfs4_stateowner *op;
3827 list_for_each_entry(op, &lock_ownerstr_hashtbl[hashval], so_strhash) {
3828 if (same_owner_str(op, owner, clid))
3829 return lockowner(op);
3831 return NULL;
3834 static void hash_lockowner(struct nfs4_lockowner *lo, unsigned int strhashval, struct nfs4_client *clp, struct nfs4_stateid *open_stp)
3836 unsigned int idhashval;
3838 idhashval = lockownerid_hashval(lo->lo_owner.so_id);
3839 list_add(&lo->lo_owner.so_idhash, &lock_ownerid_hashtbl[idhashval]);
3840 list_add(&lo->lo_owner.so_strhash, &lock_ownerstr_hashtbl[strhashval]);
3841 list_add(&lo->lo_perstateid, &open_stp->st_lockowners);
3845 * Alloc a lock owner structure.
3846 * Called in nfsd4_lock - therefore, OPEN and OPEN_CONFIRM (if needed) has
3847 * occurred.
3849 * strhashval = lock_ownerstr_hashval
3852 static struct nfs4_lockowner *
3853 alloc_init_lock_stateowner(unsigned int strhashval, struct nfs4_client *clp, struct nfs4_stateid *open_stp, struct nfsd4_lock *lock) {
3854 struct nfs4_lockowner *lo;
3856 lo = alloc_stateowner(lockowner_slab, &lock->lk_new_owner, clp);
3857 if (!lo)
3858 return NULL;
3859 INIT_LIST_HEAD(&lo->lo_owner.so_stateids);
3860 lo->lo_owner.so_is_open_owner = 0;
3861 /* It is the openowner seqid that will be incremented in encode in the
3862 * case of new lockowners; so increment the lock seqid manually: */
3863 lo->lo_owner.so_seqid = lock->lk_new_lock_seqid + 1;
3864 hash_lockowner(lo, strhashval, clp, open_stp);
3865 return lo;
3868 static struct nfs4_stateid *
3869 alloc_init_lock_stateid(struct nfs4_lockowner *lo, struct nfs4_file *fp, struct nfs4_stateid *open_stp)
3871 struct nfs4_stateid *stp;
3872 unsigned int hashval = stateid_hashval(lo->lo_owner.so_id, fp->fi_id);
3874 stp = nfs4_alloc_stateid();
3875 if (stp == NULL)
3876 goto out;
3877 INIT_LIST_HEAD(&stp->st_hash);
3878 INIT_LIST_HEAD(&stp->st_perfile);
3879 INIT_LIST_HEAD(&stp->st_perstateowner);
3880 INIT_LIST_HEAD(&stp->st_lockowners); /* not used */
3881 list_add(&stp->st_hash, &stateid_hashtbl[hashval]);
3882 list_add(&stp->st_perfile, &fp->fi_stateids);
3883 list_add(&stp->st_perstateowner, &lo->lo_owner.so_stateids);
3884 stp->st_stateowner = &lo->lo_owner;
3885 stp->st_type = NFS4_LOCK_STID;
3886 get_nfs4_file(fp);
3887 stp->st_file = fp;
3888 stp->st_stateid.si_boot = boot_time;
3889 stp->st_stateid.si_stateownerid = lo->lo_owner.so_id;
3890 stp->st_stateid.si_fileid = fp->fi_id;
3891 /* note will be incremented before first return to client: */
3892 stp->st_stateid.si_generation = 0;
3893 stp->st_access_bmap = 0;
3894 stp->st_deny_bmap = open_stp->st_deny_bmap;
3895 stp->st_openstp = open_stp;
3897 out:
3898 return stp;
3901 static int
3902 check_lock_length(u64 offset, u64 length)
3904 return ((length == 0) || ((length != NFS4_MAX_UINT64) &&
3905 LOFF_OVERFLOW(offset, length)));
3908 static void get_lock_access(struct nfs4_stateid *lock_stp, u32 access)
3910 struct nfs4_file *fp = lock_stp->st_file;
3911 int oflag = nfs4_access_to_omode(access);
3913 if (test_bit(access, &lock_stp->st_access_bmap))
3914 return;
3915 nfs4_file_get_access(fp, oflag);
3916 __set_bit(access, &lock_stp->st_access_bmap);
3920 * LOCK operation
3922 __be32
3923 nfsd4_lock(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
3924 struct nfsd4_lock *lock)
3926 struct nfs4_openowner *open_sop = NULL;
3927 struct nfs4_lockowner *lock_sop = NULL;
3928 struct nfs4_stateid *lock_stp;
3929 struct nfs4_file *fp;
3930 struct file *filp = NULL;
3931 struct file_lock file_lock;
3932 struct file_lock conflock;
3933 __be32 status = 0;
3934 unsigned int strhashval;
3935 int lkflg;
3936 int err;
3938 dprintk("NFSD: nfsd4_lock: start=%Ld length=%Ld\n",
3939 (long long) lock->lk_offset,
3940 (long long) lock->lk_length);
3942 if (check_lock_length(lock->lk_offset, lock->lk_length))
3943 return nfserr_inval;
3945 if ((status = fh_verify(rqstp, &cstate->current_fh,
3946 S_IFREG, NFSD_MAY_LOCK))) {
3947 dprintk("NFSD: nfsd4_lock: permission denied!\n");
3948 return status;
3951 nfs4_lock_state();
3953 if (lock->lk_is_new) {
3955 * Client indicates that this is a new lockowner.
3956 * Use open owner and open stateid to create lock owner and
3957 * lock stateid.
3959 struct nfs4_stateid *open_stp = NULL;
3961 status = nfserr_stale_clientid;
3962 if (!nfsd4_has_session(cstate) &&
3963 STALE_CLIENTID(&lock->lk_new_clientid))
3964 goto out;
3966 /* validate and update open stateid and open seqid */
3967 status = nfs4_preprocess_confirmed_seqid_op(cstate,
3968 lock->lk_new_open_seqid,
3969 &lock->lk_new_open_stateid,
3970 &open_stp);
3971 if (status)
3972 goto out;
3973 open_sop = openowner(open_stp->st_stateowner);
3974 status = nfserr_bad_stateid;
3975 if (!nfsd4_has_session(cstate) &&
3976 !same_clid(&open_sop->oo_owner.so_client->cl_clientid,
3977 &lock->v.new.clientid))
3978 goto out;
3979 /* create lockowner and lock stateid */
3980 fp = open_stp->st_file;
3981 strhashval = lock_ownerstr_hashval(fp->fi_inode,
3982 open_sop->oo_owner.so_client->cl_clientid.cl_id,
3983 &lock->v.new.owner);
3984 /* XXX: Do we need to check for duplicate stateowners on
3985 * the same file, or should they just be allowed (and
3986 * create new stateids)? */
3987 status = nfserr_jukebox;
3988 lock_sop = alloc_init_lock_stateowner(strhashval,
3989 open_sop->oo_owner.so_client, open_stp, lock);
3990 if (lock_sop == NULL)
3991 goto out;
3992 lock_stp = alloc_init_lock_stateid(lock_sop, fp, open_stp);
3993 if (lock_stp == NULL)
3994 goto out;
3995 } else {
3996 /* lock (lock owner + lock stateid) already exists */
3997 status = nfs4_preprocess_seqid_op(cstate,
3998 lock->lk_old_lock_seqid,
3999 &lock->lk_old_lock_stateid,
4000 NFS4_LOCK_STID, &lock_stp);
4001 if (status)
4002 goto out;
4003 lock_sop = lockowner(lock_stp->st_stateowner);
4004 fp = lock_stp->st_file;
4006 /* lock_sop and lock_stp have been created or found */
4008 lkflg = setlkflg(lock->lk_type);
4009 status = nfs4_check_openmode(lock_stp, lkflg);
4010 if (status)
4011 goto out;
4013 status = nfserr_grace;
4014 if (locks_in_grace() && !lock->lk_reclaim)
4015 goto out;
4016 status = nfserr_no_grace;
4017 if (!locks_in_grace() && lock->lk_reclaim)
4018 goto out;
4020 locks_init_lock(&file_lock);
4021 switch (lock->lk_type) {
4022 case NFS4_READ_LT:
4023 case NFS4_READW_LT:
4024 filp = find_readable_file(lock_stp->st_file);
4025 if (filp)
4026 get_lock_access(lock_stp, NFS4_SHARE_ACCESS_READ);
4027 file_lock.fl_type = F_RDLCK;
4028 break;
4029 case NFS4_WRITE_LT:
4030 case NFS4_WRITEW_LT:
4031 filp = find_writeable_file(lock_stp->st_file);
4032 if (filp)
4033 get_lock_access(lock_stp, NFS4_SHARE_ACCESS_WRITE);
4034 file_lock.fl_type = F_WRLCK;
4035 break;
4036 default:
4037 status = nfserr_inval;
4038 goto out;
4040 if (!filp) {
4041 status = nfserr_openmode;
4042 goto out;
4044 file_lock.fl_owner = (fl_owner_t)lock_sop;
4045 file_lock.fl_pid = current->tgid;
4046 file_lock.fl_file = filp;
4047 file_lock.fl_flags = FL_POSIX;
4048 file_lock.fl_lmops = &nfsd_posix_mng_ops;
4050 file_lock.fl_start = lock->lk_offset;
4051 file_lock.fl_end = last_byte_offset(lock->lk_offset, lock->lk_length);
4052 nfs4_transform_lock_offset(&file_lock);
4055 * Try to lock the file in the VFS.
4056 * Note: locks.c uses the BKL to protect the inode's lock list.
4059 err = vfs_lock_file(filp, F_SETLK, &file_lock, &conflock);
4060 switch (-err) {
4061 case 0: /* success! */
4062 update_stateid(&lock_stp->st_stateid);
4063 memcpy(&lock->lk_resp_stateid, &lock_stp->st_stateid,
4064 sizeof(stateid_t));
4065 status = 0;
4066 break;
4067 case (EAGAIN): /* conflock holds conflicting lock */
4068 status = nfserr_denied;
4069 dprintk("NFSD: nfsd4_lock: conflicting lock found!\n");
4070 nfs4_set_lock_denied(&conflock, &lock->lk_denied);
4071 break;
4072 case (EDEADLK):
4073 status = nfserr_deadlock;
4074 break;
4075 default:
4076 dprintk("NFSD: nfsd4_lock: vfs_lock_file() failed! status %d\n",err);
4077 status = nfserrno(err);
4078 break;
4080 out:
4081 if (status && lock->lk_is_new && lock_sop)
4082 release_lockowner(lock_sop);
4083 if (!cstate->replay_owner)
4084 nfs4_unlock_state();
4085 return status;
4089 * The NFSv4 spec allows a client to do a LOCKT without holding an OPEN,
4090 * so we do a temporary open here just to get an open file to pass to
4091 * vfs_test_lock. (Arguably perhaps test_lock should be done with an
4092 * inode operation.)
4094 static int nfsd_test_lock(struct svc_rqst *rqstp, struct svc_fh *fhp, struct file_lock *lock)
4096 struct file *file;
4097 int err;
4099 err = nfsd_open(rqstp, fhp, S_IFREG, NFSD_MAY_READ, &file);
4100 if (err)
4101 return err;
4102 err = vfs_test_lock(file, lock);
4103 nfsd_close(file);
4104 return err;
4108 * LOCKT operation
4110 __be32
4111 nfsd4_lockt(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
4112 struct nfsd4_lockt *lockt)
4114 struct inode *inode;
4115 struct file_lock file_lock;
4116 struct nfs4_lockowner *lo;
4117 int error;
4118 __be32 status;
4120 if (locks_in_grace())
4121 return nfserr_grace;
4123 if (check_lock_length(lockt->lt_offset, lockt->lt_length))
4124 return nfserr_inval;
4126 nfs4_lock_state();
4128 status = nfserr_stale_clientid;
4129 if (!nfsd4_has_session(cstate) && STALE_CLIENTID(&lockt->lt_clientid))
4130 goto out;
4132 if ((status = fh_verify(rqstp, &cstate->current_fh, S_IFREG, 0)))
4133 goto out;
4135 inode = cstate->current_fh.fh_dentry->d_inode;
4136 locks_init_lock(&file_lock);
4137 switch (lockt->lt_type) {
4138 case NFS4_READ_LT:
4139 case NFS4_READW_LT:
4140 file_lock.fl_type = F_RDLCK;
4141 break;
4142 case NFS4_WRITE_LT:
4143 case NFS4_WRITEW_LT:
4144 file_lock.fl_type = F_WRLCK;
4145 break;
4146 default:
4147 dprintk("NFSD: nfs4_lockt: bad lock type!\n");
4148 status = nfserr_inval;
4149 goto out;
4152 lo = find_lockowner_str(inode, &lockt->lt_clientid, &lockt->lt_owner);
4153 if (lo)
4154 file_lock.fl_owner = (fl_owner_t)lo;
4155 file_lock.fl_pid = current->tgid;
4156 file_lock.fl_flags = FL_POSIX;
4158 file_lock.fl_start = lockt->lt_offset;
4159 file_lock.fl_end = last_byte_offset(lockt->lt_offset, lockt->lt_length);
4161 nfs4_transform_lock_offset(&file_lock);
4163 status = nfs_ok;
4164 error = nfsd_test_lock(rqstp, &cstate->current_fh, &file_lock);
4165 if (error) {
4166 status = nfserrno(error);
4167 goto out;
4169 if (file_lock.fl_type != F_UNLCK) {
4170 status = nfserr_denied;
4171 nfs4_set_lock_denied(&file_lock, &lockt->lt_denied);
4173 out:
4174 nfs4_unlock_state();
4175 return status;
4178 __be32
4179 nfsd4_locku(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
4180 struct nfsd4_locku *locku)
4182 struct nfs4_stateid *stp;
4183 struct file *filp = NULL;
4184 struct file_lock file_lock;
4185 __be32 status;
4186 int err;
4188 dprintk("NFSD: nfsd4_locku: start=%Ld length=%Ld\n",
4189 (long long) locku->lu_offset,
4190 (long long) locku->lu_length);
4192 if (check_lock_length(locku->lu_offset, locku->lu_length))
4193 return nfserr_inval;
4195 nfs4_lock_state();
4197 status = nfs4_preprocess_seqid_op(cstate, locku->lu_seqid,
4198 &locku->lu_stateid, NFS4_LOCK_STID, &stp);
4199 if (status)
4200 goto out;
4201 filp = find_any_file(stp->st_file);
4202 if (!filp) {
4203 status = nfserr_lock_range;
4204 goto out;
4206 BUG_ON(!filp);
4207 locks_init_lock(&file_lock);
4208 file_lock.fl_type = F_UNLCK;
4209 file_lock.fl_owner = (fl_owner_t)lockowner(stp->st_stateowner);
4210 file_lock.fl_pid = current->tgid;
4211 file_lock.fl_file = filp;
4212 file_lock.fl_flags = FL_POSIX;
4213 file_lock.fl_lmops = &nfsd_posix_mng_ops;
4214 file_lock.fl_start = locku->lu_offset;
4216 file_lock.fl_end = last_byte_offset(locku->lu_offset, locku->lu_length);
4217 nfs4_transform_lock_offset(&file_lock);
4220 * Try to unlock the file in the VFS.
4222 err = vfs_lock_file(filp, F_SETLK, &file_lock, NULL);
4223 if (err) {
4224 dprintk("NFSD: nfs4_locku: vfs_lock_file failed!\n");
4225 goto out_nfserr;
4228 * OK, unlock succeeded; the only thing left to do is update the stateid.
4230 update_stateid(&stp->st_stateid);
4231 memcpy(&locku->lu_stateid, &stp->st_stateid, sizeof(stateid_t));
4233 out:
4234 nfs4_unlock_state();
4235 return status;
4237 out_nfserr:
4238 status = nfserrno(err);
4239 goto out;
4243 * returns
4244 * 1: locks held by lockowner
4245 * 0: no locks held by lockowner
4247 static int
4248 check_for_locks(struct nfs4_file *filp, struct nfs4_lockowner *lowner)
4250 struct file_lock **flpp;
4251 struct inode *inode = filp->fi_inode;
4252 int status = 0;
4254 lock_flocks();
4255 for (flpp = &inode->i_flock; *flpp != NULL; flpp = &(*flpp)->fl_next) {
4256 if ((*flpp)->fl_owner == (fl_owner_t)lowner) {
4257 status = 1;
4258 goto out;
4261 out:
4262 unlock_flocks();
4263 return status;
4266 __be32
4267 nfsd4_release_lockowner(struct svc_rqst *rqstp,
4268 struct nfsd4_compound_state *cstate,
4269 struct nfsd4_release_lockowner *rlockowner)
4271 clientid_t *clid = &rlockowner->rl_clientid;
4272 struct nfs4_stateowner *sop;
4273 struct nfs4_lockowner *lo;
4274 struct nfs4_stateid *stp;
4275 struct xdr_netobj *owner = &rlockowner->rl_owner;
4276 struct list_head matches;
4277 int i;
4278 __be32 status;
4280 dprintk("nfsd4_release_lockowner clientid: (%08x/%08x):\n",
4281 clid->cl_boot, clid->cl_id);
4283 /* XXX check for lease expiration */
4285 status = nfserr_stale_clientid;
4286 if (STALE_CLIENTID(clid))
4287 return status;
4289 nfs4_lock_state();
4291 status = nfserr_locks_held;
4292 /* XXX: we're doing a linear search through all the lockowners.
4293 * Yipes! For now we'll just hope clients aren't really using
4294 * release_lockowner much, but eventually we have to fix these
4295 * data structures. */
4296 INIT_LIST_HEAD(&matches);
4297 for (i = 0; i < LOCK_HASH_SIZE; i++) {
4298 list_for_each_entry(sop, &lock_ownerid_hashtbl[i], so_idhash) {
4299 if (!same_owner_str(sop, owner, clid))
4300 continue;
4301 list_for_each_entry(stp, &sop->so_stateids,
4302 st_perstateowner) {
4303 lo = lockowner(sop);
4304 if (check_for_locks(stp->st_file, lo))
4305 goto out;
4306 list_add(&lo->lo_list, &matches);
4310 /* Clients probably won't expect us to return with some (but not all)
4311 * of the lockowner state released; so don't release any until all
4312 * have been checked. */
4313 status = nfs_ok;
4314 while (!list_empty(&matches)) {
4315 lo = list_entry(matches.next, struct nfs4_lockowner,
4316 lo_list);
4317 /* unhash_stateowner deletes so_perclient only
4318 * for openowners. */
4319 list_del(&lo->lo_list);
4320 release_lockowner(lo);
4322 out:
4323 nfs4_unlock_state();
4324 return status;
4327 static inline struct nfs4_client_reclaim *
4328 alloc_reclaim(void)
4330 return kmalloc(sizeof(struct nfs4_client_reclaim), GFP_KERNEL);
4334 nfs4_has_reclaimed_state(const char *name, bool use_exchange_id)
4336 unsigned int strhashval = clientstr_hashval(name);
4337 struct nfs4_client *clp;
4339 clp = find_confirmed_client_by_str(name, strhashval);
4340 return clp ? 1 : 0;
4344 * failure => all reset bets are off, nfserr_no_grace...
4347 nfs4_client_to_reclaim(const char *name)
4349 unsigned int strhashval;
4350 struct nfs4_client_reclaim *crp = NULL;
4352 dprintk("NFSD nfs4_client_to_reclaim NAME: %.*s\n", HEXDIR_LEN, name);
4353 crp = alloc_reclaim();
4354 if (!crp)
4355 return 0;
4356 strhashval = clientstr_hashval(name);
4357 INIT_LIST_HEAD(&crp->cr_strhash);
4358 list_add(&crp->cr_strhash, &reclaim_str_hashtbl[strhashval]);
4359 memcpy(crp->cr_recdir, name, HEXDIR_LEN);
4360 reclaim_str_hashtbl_size++;
4361 return 1;
4364 static void
4365 nfs4_release_reclaim(void)
4367 struct nfs4_client_reclaim *crp = NULL;
4368 int i;
4370 for (i = 0; i < CLIENT_HASH_SIZE; i++) {
4371 while (!list_empty(&reclaim_str_hashtbl[i])) {
4372 crp = list_entry(reclaim_str_hashtbl[i].next,
4373 struct nfs4_client_reclaim, cr_strhash);
4374 list_del(&crp->cr_strhash);
4375 kfree(crp);
4376 reclaim_str_hashtbl_size--;
4379 BUG_ON(reclaim_str_hashtbl_size);
4383 * called from OPEN, CLAIM_PREVIOUS with a new clientid. */
4384 static struct nfs4_client_reclaim *
4385 nfs4_find_reclaim_client(clientid_t *clid)
4387 unsigned int strhashval;
4388 struct nfs4_client *clp;
4389 struct nfs4_client_reclaim *crp = NULL;
4392 /* find clientid in conf_id_hashtbl */
4393 clp = find_confirmed_client(clid);
4394 if (clp == NULL)
4395 return NULL;
4397 dprintk("NFSD: nfs4_find_reclaim_client for %.*s with recdir %s\n",
4398 clp->cl_name.len, clp->cl_name.data,
4399 clp->cl_recdir);
4401 /* find clp->cl_name in reclaim_str_hashtbl */
4402 strhashval = clientstr_hashval(clp->cl_recdir);
4403 list_for_each_entry(crp, &reclaim_str_hashtbl[strhashval], cr_strhash) {
4404 if (same_name(crp->cr_recdir, clp->cl_recdir)) {
4405 return crp;
4408 return NULL;
4412 * Called from OPEN. Look for clientid in reclaim list.
4414 __be32
4415 nfs4_check_open_reclaim(clientid_t *clid)
4417 return nfs4_find_reclaim_client(clid) ? nfs_ok : nfserr_reclaim_bad;
4420 /* initialization to perform at module load time: */
4423 nfs4_state_init(void)
4425 int i, status;
4427 status = nfsd4_init_slabs();
4428 if (status)
4429 return status;
4430 for (i = 0; i < CLIENT_HASH_SIZE; i++) {
4431 INIT_LIST_HEAD(&conf_id_hashtbl[i]);
4432 INIT_LIST_HEAD(&conf_str_hashtbl[i]);
4433 INIT_LIST_HEAD(&unconf_str_hashtbl[i]);
4434 INIT_LIST_HEAD(&unconf_id_hashtbl[i]);
4435 INIT_LIST_HEAD(&reclaim_str_hashtbl[i]);
4437 for (i = 0; i < SESSION_HASH_SIZE; i++)
4438 INIT_LIST_HEAD(&sessionid_hashtbl[i]);
4439 for (i = 0; i < FILE_HASH_SIZE; i++) {
4440 INIT_LIST_HEAD(&file_hashtbl[i]);
4442 for (i = 0; i < OPEN_OWNER_HASH_SIZE; i++) {
4443 INIT_LIST_HEAD(&open_ownerstr_hashtbl[i]);
4444 INIT_LIST_HEAD(&open_ownerid_hashtbl[i]);
4446 for (i = 0; i < STATEID_HASH_SIZE; i++)
4447 INIT_LIST_HEAD(&stateid_hashtbl[i]);
4448 for (i = 0; i < LOCK_HASH_SIZE; i++) {
4449 INIT_LIST_HEAD(&lock_ownerid_hashtbl[i]);
4450 INIT_LIST_HEAD(&lock_ownerstr_hashtbl[i]);
4452 memset(&onestateid, ~0, sizeof(stateid_t));
4453 INIT_LIST_HEAD(&close_lru);
4454 INIT_LIST_HEAD(&client_lru);
4455 INIT_LIST_HEAD(&del_recall_lru);
4456 reclaim_str_hashtbl_size = 0;
4457 return 0;
4460 static void
4461 nfsd4_load_reboot_recovery_data(void)
4463 int status;
4465 nfs4_lock_state();
4466 nfsd4_init_recdir();
4467 status = nfsd4_recdir_load();
4468 nfs4_unlock_state();
4469 if (status)
4470 printk("NFSD: Failure reading reboot recovery data\n");
4474 * Since the lifetime of a delegation isn't limited to that of an open, a
4475 * client may quite reasonably hang on to a delegation as long as it has
4476 * the inode cached. This becomes an obvious problem the first time a
4477 * client's inode cache approaches the size of the server's total memory.
4479 * For now we avoid this problem by imposing a hard limit on the number
4480 * of delegations, which varies according to the server's memory size.
4482 static void
4483 set_max_delegations(void)
4486 * Allow at most 4 delegations per megabyte of RAM. Quick
4487 * estimates suggest that in the worst case (where every delegation
4488 * is for a different inode), a delegation could take about 1.5K,
4489 * giving a worst case usage of about 6% of memory.
4491 max_delegations = nr_free_buffer_pages() >> (20 - 2 - PAGE_SHIFT);
4494 /* initialization to perform when the nfsd service is started: */
4496 static int
4497 __nfs4_state_start(void)
4499 int ret;
4501 boot_time = get_seconds();
4502 locks_start_grace(&nfsd4_manager);
4503 printk(KERN_INFO "NFSD: starting %ld-second grace period\n",
4504 nfsd4_grace);
4505 ret = set_callback_cred();
4506 if (ret)
4507 return -ENOMEM;
4508 laundry_wq = create_singlethread_workqueue("nfsd4");
4509 if (laundry_wq == NULL)
4510 return -ENOMEM;
4511 ret = nfsd4_create_callback_queue();
4512 if (ret)
4513 goto out_free_laundry;
4514 queue_delayed_work(laundry_wq, &laundromat_work, nfsd4_grace * HZ);
4515 set_max_delegations();
4516 return 0;
4517 out_free_laundry:
4518 destroy_workqueue(laundry_wq);
4519 return ret;
4523 nfs4_state_start(void)
4525 nfsd4_load_reboot_recovery_data();
4526 return __nfs4_state_start();
4529 static void
4530 __nfs4_state_shutdown(void)
4532 int i;
4533 struct nfs4_client *clp = NULL;
4534 struct nfs4_delegation *dp = NULL;
4535 struct list_head *pos, *next, reaplist;
4537 for (i = 0; i < CLIENT_HASH_SIZE; i++) {
4538 while (!list_empty(&conf_id_hashtbl[i])) {
4539 clp = list_entry(conf_id_hashtbl[i].next, struct nfs4_client, cl_idhash);
4540 expire_client(clp);
4542 while (!list_empty(&unconf_str_hashtbl[i])) {
4543 clp = list_entry(unconf_str_hashtbl[i].next, struct nfs4_client, cl_strhash);
4544 expire_client(clp);
4547 INIT_LIST_HEAD(&reaplist);
4548 spin_lock(&recall_lock);
4549 list_for_each_safe(pos, next, &del_recall_lru) {
4550 dp = list_entry (pos, struct nfs4_delegation, dl_recall_lru);
4551 list_move(&dp->dl_recall_lru, &reaplist);
4553 spin_unlock(&recall_lock);
4554 list_for_each_safe(pos, next, &reaplist) {
4555 dp = list_entry (pos, struct nfs4_delegation, dl_recall_lru);
4556 list_del_init(&dp->dl_recall_lru);
4557 unhash_delegation(dp);
4560 nfsd4_shutdown_recdir();
4563 void
4564 nfs4_state_shutdown(void)
4566 cancel_delayed_work_sync(&laundromat_work);
4567 destroy_workqueue(laundry_wq);
4568 locks_end_grace(&nfsd4_manager);
4569 nfs4_lock_state();
4570 nfs4_release_reclaim();
4571 __nfs4_state_shutdown();
4572 nfs4_unlock_state();
4573 nfsd4_destroy_callback_queue();