ACPI, x86: Use SRAT table rev to use 8bit or 32bit PXM fields (x86/x86-64)
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / fs / nfsd / nfs4state.c
blobecd8152965eefc70973a24e4f5035a9cef3f7072
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/sunrpc/svcauth_gss.h>
41 #include <linux/sunrpc/clnt.h>
42 #include "xdr4.h"
43 #include "vfs.h"
45 #define NFSDDBG_FACILITY NFSDDBG_PROC
47 /* Globals */
48 time_t nfsd4_lease = 90; /* default lease time */
49 time_t nfsd4_grace = 90;
50 static time_t boot_time;
51 static u32 current_ownerid = 1;
52 static u32 current_fileid = 1;
53 static u32 current_delegid = 1;
54 static stateid_t zerostateid; /* bits all 0 */
55 static stateid_t onestateid; /* bits all 1 */
56 static u64 current_sessionid = 1;
58 #define ZERO_STATEID(stateid) (!memcmp((stateid), &zerostateid, sizeof(stateid_t)))
59 #define ONE_STATEID(stateid) (!memcmp((stateid), &onestateid, sizeof(stateid_t)))
61 /* forward declarations */
62 static struct nfs4_stateid * find_stateid(stateid_t *stid, int flags);
63 static struct nfs4_delegation * find_delegation_stateid(struct inode *ino, stateid_t *stid);
64 static char user_recovery_dirname[PATH_MAX] = "/var/lib/nfs/v4recovery";
65 static void nfs4_set_recdir(char *recdir);
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 *stateowner_slab = NULL;
80 static struct kmem_cache *file_slab = NULL;
81 static struct kmem_cache *stateid_slab = NULL;
82 static struct kmem_cache *deleg_slab = NULL;
84 void
85 nfs4_lock_state(void)
87 mutex_lock(&client_mutex);
90 void
91 nfs4_unlock_state(void)
93 mutex_unlock(&client_mutex);
96 static inline u32
97 opaque_hashval(const void *ptr, int nbytes)
99 unsigned char *cptr = (unsigned char *) ptr;
101 u32 x = 0;
102 while (nbytes--) {
103 x *= 37;
104 x += *cptr++;
106 return x;
109 static struct list_head del_recall_lru;
111 static inline void
112 put_nfs4_file(struct nfs4_file *fi)
114 if (atomic_dec_and_lock(&fi->fi_ref, &recall_lock)) {
115 list_del(&fi->fi_hash);
116 spin_unlock(&recall_lock);
117 iput(fi->fi_inode);
118 kmem_cache_free(file_slab, fi);
122 static inline void
123 get_nfs4_file(struct nfs4_file *fi)
125 atomic_inc(&fi->fi_ref);
128 static int num_delegations;
129 unsigned int max_delegations;
132 * Open owner state (share locks)
135 /* hash tables for nfs4_stateowner */
136 #define OWNER_HASH_BITS 8
137 #define OWNER_HASH_SIZE (1 << OWNER_HASH_BITS)
138 #define OWNER_HASH_MASK (OWNER_HASH_SIZE - 1)
140 #define ownerid_hashval(id) \
141 ((id) & OWNER_HASH_MASK)
142 #define ownerstr_hashval(clientid, ownername) \
143 (((clientid) + opaque_hashval((ownername.data), (ownername.len))) & OWNER_HASH_MASK)
145 static struct list_head ownerid_hashtbl[OWNER_HASH_SIZE];
146 static struct list_head ownerstr_hashtbl[OWNER_HASH_SIZE];
148 /* hash table for nfs4_file */
149 #define FILE_HASH_BITS 8
150 #define FILE_HASH_SIZE (1 << FILE_HASH_BITS)
152 /* hash table for (open)nfs4_stateid */
153 #define STATEID_HASH_BITS 10
154 #define STATEID_HASH_SIZE (1 << STATEID_HASH_BITS)
155 #define STATEID_HASH_MASK (STATEID_HASH_SIZE - 1)
157 #define file_hashval(x) \
158 hash_ptr(x, FILE_HASH_BITS)
159 #define stateid_hashval(owner_id, file_id) \
160 (((owner_id) + (file_id)) & STATEID_HASH_MASK)
162 static struct list_head file_hashtbl[FILE_HASH_SIZE];
163 static struct list_head stateid_hashtbl[STATEID_HASH_SIZE];
165 static void __nfs4_file_get_access(struct nfs4_file *fp, int oflag)
167 BUG_ON(!(fp->fi_fds[oflag] || fp->fi_fds[O_RDWR]));
168 atomic_inc(&fp->fi_access[oflag]);
171 static void nfs4_file_get_access(struct nfs4_file *fp, int oflag)
173 if (oflag == O_RDWR) {
174 __nfs4_file_get_access(fp, O_RDONLY);
175 __nfs4_file_get_access(fp, O_WRONLY);
176 } else
177 __nfs4_file_get_access(fp, oflag);
180 static void nfs4_file_put_fd(struct nfs4_file *fp, int oflag)
182 if (fp->fi_fds[oflag]) {
183 fput(fp->fi_fds[oflag]);
184 fp->fi_fds[oflag] = NULL;
188 static void __nfs4_file_put_access(struct nfs4_file *fp, int oflag)
190 if (atomic_dec_and_test(&fp->fi_access[oflag])) {
191 nfs4_file_put_fd(fp, oflag);
193 * It's also safe to get rid of the RDWR open *if*
194 * we no longer have need of the other kind of access
195 * or if we already have the other kind of open:
197 if (fp->fi_fds[1-oflag]
198 || atomic_read(&fp->fi_access[1 - oflag]) == 0)
199 nfs4_file_put_fd(fp, O_RDWR);
203 static void nfs4_file_put_access(struct nfs4_file *fp, int oflag)
205 if (oflag == O_RDWR) {
206 __nfs4_file_put_access(fp, O_RDONLY);
207 __nfs4_file_put_access(fp, O_WRONLY);
208 } else
209 __nfs4_file_put_access(fp, oflag);
212 static struct nfs4_delegation *
213 alloc_init_deleg(struct nfs4_client *clp, struct nfs4_stateid *stp, struct svc_fh *current_fh, u32 type)
215 struct nfs4_delegation *dp;
216 struct nfs4_file *fp = stp->st_file;
218 dprintk("NFSD alloc_init_deleg\n");
220 * Major work on the lease subsystem (for example, to support
221 * calbacks on stat) will be required before we can support
222 * write delegations properly.
224 if (type != NFS4_OPEN_DELEGATE_READ)
225 return NULL;
226 if (fp->fi_had_conflict)
227 return NULL;
228 if (num_delegations > max_delegations)
229 return NULL;
230 dp = kmem_cache_alloc(deleg_slab, GFP_KERNEL);
231 if (dp == NULL)
232 return dp;
233 num_delegations++;
234 INIT_LIST_HEAD(&dp->dl_perfile);
235 INIT_LIST_HEAD(&dp->dl_perclnt);
236 INIT_LIST_HEAD(&dp->dl_recall_lru);
237 dp->dl_client = clp;
238 get_nfs4_file(fp);
239 dp->dl_file = fp;
240 dp->dl_type = type;
241 dp->dl_stateid.si_boot = boot_time;
242 dp->dl_stateid.si_stateownerid = current_delegid++;
243 dp->dl_stateid.si_fileid = 0;
244 dp->dl_stateid.si_generation = 0;
245 fh_copy_shallow(&dp->dl_fh, &current_fh->fh_handle);
246 dp->dl_time = 0;
247 atomic_set(&dp->dl_count, 1);
248 INIT_WORK(&dp->dl_recall.cb_work, nfsd4_do_callback_rpc);
249 return dp;
252 void
253 nfs4_put_delegation(struct nfs4_delegation *dp)
255 if (atomic_dec_and_test(&dp->dl_count)) {
256 dprintk("NFSD: freeing dp %p\n",dp);
257 put_nfs4_file(dp->dl_file);
258 kmem_cache_free(deleg_slab, dp);
259 num_delegations--;
263 static void nfs4_put_deleg_lease(struct nfs4_file *fp)
265 if (atomic_dec_and_test(&fp->fi_delegees)) {
266 vfs_setlease(fp->fi_deleg_file, F_UNLCK, &fp->fi_lease);
267 fp->fi_lease = NULL;
268 fput(fp->fi_deleg_file);
269 fp->fi_deleg_file = NULL;
273 /* Called under the state lock. */
274 static void
275 unhash_delegation(struct nfs4_delegation *dp)
277 list_del_init(&dp->dl_perclnt);
278 spin_lock(&recall_lock);
279 list_del_init(&dp->dl_perfile);
280 list_del_init(&dp->dl_recall_lru);
281 spin_unlock(&recall_lock);
282 nfs4_put_deleg_lease(dp->dl_file);
283 nfs4_put_delegation(dp);
287 * SETCLIENTID state
290 /* client_lock protects the client lru list and session hash table */
291 static DEFINE_SPINLOCK(client_lock);
293 /* Hash tables for nfs4_clientid state */
294 #define CLIENT_HASH_BITS 4
295 #define CLIENT_HASH_SIZE (1 << CLIENT_HASH_BITS)
296 #define CLIENT_HASH_MASK (CLIENT_HASH_SIZE - 1)
298 #define clientid_hashval(id) \
299 ((id) & CLIENT_HASH_MASK)
300 #define clientstr_hashval(name) \
301 (opaque_hashval((name), 8) & CLIENT_HASH_MASK)
303 * reclaim_str_hashtbl[] holds known client info from previous reset/reboot
304 * used in reboot/reset lease grace period processing
306 * conf_id_hashtbl[], and conf_str_hashtbl[] hold confirmed
307 * setclientid_confirmed info.
309 * unconf_str_hastbl[] and unconf_id_hashtbl[] hold unconfirmed
310 * setclientid info.
312 * client_lru holds client queue ordered by nfs4_client.cl_time
313 * for lease renewal.
315 * close_lru holds (open) stateowner queue ordered by nfs4_stateowner.so_time
316 * for last close replay.
318 static struct list_head reclaim_str_hashtbl[CLIENT_HASH_SIZE];
319 static int reclaim_str_hashtbl_size = 0;
320 static struct list_head conf_id_hashtbl[CLIENT_HASH_SIZE];
321 static struct list_head conf_str_hashtbl[CLIENT_HASH_SIZE];
322 static struct list_head unconf_str_hashtbl[CLIENT_HASH_SIZE];
323 static struct list_head unconf_id_hashtbl[CLIENT_HASH_SIZE];
324 static struct list_head client_lru;
325 static struct list_head close_lru;
328 * We store the NONE, READ, WRITE, and BOTH bits separately in the
329 * st_{access,deny}_bmap field of the stateid, in order to track not
330 * only what share bits are currently in force, but also what
331 * combinations of share bits previous opens have used. This allows us
332 * to enforce the recommendation of rfc 3530 14.2.19 that the server
333 * return an error if the client attempt to downgrade to a combination
334 * of share bits not explicable by closing some of its previous opens.
336 * XXX: This enforcement is actually incomplete, since we don't keep
337 * track of access/deny bit combinations; so, e.g., we allow:
339 * OPEN allow read, deny write
340 * OPEN allow both, deny none
341 * DOWNGRADE allow read, deny none
343 * which we should reject.
345 static void
346 set_access(unsigned int *access, unsigned long bmap) {
347 int i;
349 *access = 0;
350 for (i = 1; i < 4; i++) {
351 if (test_bit(i, &bmap))
352 *access |= i;
356 static void
357 set_deny(unsigned int *deny, unsigned long bmap) {
358 int i;
360 *deny = 0;
361 for (i = 0; i < 4; i++) {
362 if (test_bit(i, &bmap))
363 *deny |= i ;
367 static int
368 test_share(struct nfs4_stateid *stp, struct nfsd4_open *open) {
369 unsigned int access, deny;
371 set_access(&access, stp->st_access_bmap);
372 set_deny(&deny, stp->st_deny_bmap);
373 if ((access & open->op_share_deny) || (deny & open->op_share_access))
374 return 0;
375 return 1;
378 static int nfs4_access_to_omode(u32 access)
380 switch (access & NFS4_SHARE_ACCESS_BOTH) {
381 case NFS4_SHARE_ACCESS_READ:
382 return O_RDONLY;
383 case NFS4_SHARE_ACCESS_WRITE:
384 return O_WRONLY;
385 case NFS4_SHARE_ACCESS_BOTH:
386 return O_RDWR;
388 BUG();
391 static void unhash_generic_stateid(struct nfs4_stateid *stp)
393 list_del(&stp->st_hash);
394 list_del(&stp->st_perfile);
395 list_del(&stp->st_perstateowner);
398 static void free_generic_stateid(struct nfs4_stateid *stp)
400 int i;
402 if (stp->st_access_bmap) {
403 for (i = 1; i < 4; i++) {
404 if (test_bit(i, &stp->st_access_bmap))
405 nfs4_file_put_access(stp->st_file,
406 nfs4_access_to_omode(i));
409 put_nfs4_file(stp->st_file);
410 kmem_cache_free(stateid_slab, stp);
413 static void release_lock_stateid(struct nfs4_stateid *stp)
415 struct file *file;
417 unhash_generic_stateid(stp);
418 file = find_any_file(stp->st_file);
419 if (file)
420 locks_remove_posix(file, (fl_owner_t)stp->st_stateowner);
421 free_generic_stateid(stp);
424 static void unhash_lockowner(struct nfs4_stateowner *sop)
426 struct nfs4_stateid *stp;
428 list_del(&sop->so_idhash);
429 list_del(&sop->so_strhash);
430 list_del(&sop->so_perstateid);
431 while (!list_empty(&sop->so_stateids)) {
432 stp = list_first_entry(&sop->so_stateids,
433 struct nfs4_stateid, st_perstateowner);
434 release_lock_stateid(stp);
438 static void release_lockowner(struct nfs4_stateowner *sop)
440 unhash_lockowner(sop);
441 nfs4_put_stateowner(sop);
444 static void
445 release_stateid_lockowners(struct nfs4_stateid *open_stp)
447 struct nfs4_stateowner *lock_sop;
449 while (!list_empty(&open_stp->st_lockowners)) {
450 lock_sop = list_entry(open_stp->st_lockowners.next,
451 struct nfs4_stateowner, so_perstateid);
452 /* list_del(&open_stp->st_lockowners); */
453 BUG_ON(lock_sop->so_is_open_owner);
454 release_lockowner(lock_sop);
458 static void release_open_stateid(struct nfs4_stateid *stp)
460 unhash_generic_stateid(stp);
461 release_stateid_lockowners(stp);
462 free_generic_stateid(stp);
465 static void unhash_openowner(struct nfs4_stateowner *sop)
467 struct nfs4_stateid *stp;
469 list_del(&sop->so_idhash);
470 list_del(&sop->so_strhash);
471 list_del(&sop->so_perclient);
472 list_del(&sop->so_perstateid); /* XXX: necessary? */
473 while (!list_empty(&sop->so_stateids)) {
474 stp = list_first_entry(&sop->so_stateids,
475 struct nfs4_stateid, st_perstateowner);
476 release_open_stateid(stp);
480 static void release_openowner(struct nfs4_stateowner *sop)
482 unhash_openowner(sop);
483 list_del(&sop->so_close_lru);
484 nfs4_put_stateowner(sop);
487 #define SESSION_HASH_SIZE 512
488 static struct list_head sessionid_hashtbl[SESSION_HASH_SIZE];
490 static inline int
491 hash_sessionid(struct nfs4_sessionid *sessionid)
493 struct nfsd4_sessionid *sid = (struct nfsd4_sessionid *)sessionid;
495 return sid->sequence % SESSION_HASH_SIZE;
498 static inline void
499 dump_sessionid(const char *fn, struct nfs4_sessionid *sessionid)
501 u32 *ptr = (u32 *)(&sessionid->data[0]);
502 dprintk("%s: %u:%u:%u:%u\n", fn, ptr[0], ptr[1], ptr[2], ptr[3]);
505 static void
506 gen_sessionid(struct nfsd4_session *ses)
508 struct nfs4_client *clp = ses->se_client;
509 struct nfsd4_sessionid *sid;
511 sid = (struct nfsd4_sessionid *)ses->se_sessionid.data;
512 sid->clientid = clp->cl_clientid;
513 sid->sequence = current_sessionid++;
514 sid->reserved = 0;
518 * The protocol defines ca_maxresponssize_cached to include the size of
519 * the rpc header, but all we need to cache is the data starting after
520 * the end of the initial SEQUENCE operation--the rest we regenerate
521 * each time. Therefore we can advertise a ca_maxresponssize_cached
522 * value that is the number of bytes in our cache plus a few additional
523 * bytes. In order to stay on the safe side, and not promise more than
524 * we can cache, those additional bytes must be the minimum possible: 24
525 * bytes of rpc header (xid through accept state, with AUTH_NULL
526 * verifier), 12 for the compound header (with zero-length tag), and 44
527 * for the SEQUENCE op response:
529 #define NFSD_MIN_HDR_SEQ_SZ (24 + 12 + 44)
531 static void
532 free_session_slots(struct nfsd4_session *ses)
534 int i;
536 for (i = 0; i < ses->se_fchannel.maxreqs; i++)
537 kfree(ses->se_slots[i]);
541 * We don't actually need to cache the rpc and session headers, so we
542 * can allocate a little less for each slot:
544 static inline int slot_bytes(struct nfsd4_channel_attrs *ca)
546 return ca->maxresp_cached - NFSD_MIN_HDR_SEQ_SZ;
549 static int nfsd4_sanitize_slot_size(u32 size)
551 size -= NFSD_MIN_HDR_SEQ_SZ; /* We don't cache the rpc header */
552 size = min_t(u32, size, NFSD_SLOT_CACHE_SIZE);
554 return size;
558 * XXX: If we run out of reserved DRC memory we could (up to a point)
559 * re-negotiate active sessions and reduce their slot usage to make
560 * rooom for new connections. For now we just fail the create session.
562 static int nfsd4_get_drc_mem(int slotsize, u32 num)
564 int avail;
566 num = min_t(u32, num, NFSD_MAX_SLOTS_PER_SESSION);
568 spin_lock(&nfsd_drc_lock);
569 avail = min_t(int, NFSD_MAX_MEM_PER_SESSION,
570 nfsd_drc_max_mem - nfsd_drc_mem_used);
571 num = min_t(int, num, avail / slotsize);
572 nfsd_drc_mem_used += num * slotsize;
573 spin_unlock(&nfsd_drc_lock);
575 return num;
578 static void nfsd4_put_drc_mem(int slotsize, int num)
580 spin_lock(&nfsd_drc_lock);
581 nfsd_drc_mem_used -= slotsize * num;
582 spin_unlock(&nfsd_drc_lock);
585 static struct nfsd4_session *alloc_session(int slotsize, int numslots)
587 struct nfsd4_session *new;
588 int mem, i;
590 BUILD_BUG_ON(NFSD_MAX_SLOTS_PER_SESSION * sizeof(struct nfsd4_slot *)
591 + sizeof(struct nfsd4_session) > PAGE_SIZE);
592 mem = numslots * sizeof(struct nfsd4_slot *);
594 new = kzalloc(sizeof(*new) + mem, GFP_KERNEL);
595 if (!new)
596 return NULL;
597 /* allocate each struct nfsd4_slot and data cache in one piece */
598 for (i = 0; i < numslots; i++) {
599 mem = sizeof(struct nfsd4_slot) + slotsize;
600 new->se_slots[i] = kzalloc(mem, GFP_KERNEL);
601 if (!new->se_slots[i])
602 goto out_free;
604 return new;
605 out_free:
606 while (i--)
607 kfree(new->se_slots[i]);
608 kfree(new);
609 return NULL;
612 static void init_forechannel_attrs(struct nfsd4_channel_attrs *new, struct nfsd4_channel_attrs *req, int numslots, int slotsize)
614 u32 maxrpc = nfsd_serv->sv_max_mesg;
616 new->maxreqs = numslots;
617 new->maxresp_cached = min_t(u32, req->maxresp_cached,
618 slotsize + NFSD_MIN_HDR_SEQ_SZ);
619 new->maxreq_sz = min_t(u32, req->maxreq_sz, maxrpc);
620 new->maxresp_sz = min_t(u32, req->maxresp_sz, maxrpc);
621 new->maxops = min_t(u32, req->maxops, NFSD_MAX_OPS_PER_COMPOUND);
624 static void free_conn(struct nfsd4_conn *c)
626 svc_xprt_put(c->cn_xprt);
627 kfree(c);
630 static void nfsd4_conn_lost(struct svc_xpt_user *u)
632 struct nfsd4_conn *c = container_of(u, struct nfsd4_conn, cn_xpt_user);
633 struct nfs4_client *clp = c->cn_session->se_client;
635 spin_lock(&clp->cl_lock);
636 if (!list_empty(&c->cn_persession)) {
637 list_del(&c->cn_persession);
638 free_conn(c);
640 spin_unlock(&clp->cl_lock);
641 nfsd4_probe_callback(clp);
644 static struct nfsd4_conn *alloc_conn(struct svc_rqst *rqstp, u32 flags)
646 struct nfsd4_conn *conn;
648 conn = kmalloc(sizeof(struct nfsd4_conn), GFP_KERNEL);
649 if (!conn)
650 return NULL;
651 svc_xprt_get(rqstp->rq_xprt);
652 conn->cn_xprt = rqstp->rq_xprt;
653 conn->cn_flags = flags;
654 INIT_LIST_HEAD(&conn->cn_xpt_user.list);
655 return conn;
658 static void __nfsd4_hash_conn(struct nfsd4_conn *conn, struct nfsd4_session *ses)
660 conn->cn_session = ses;
661 list_add(&conn->cn_persession, &ses->se_conns);
664 static void nfsd4_hash_conn(struct nfsd4_conn *conn, struct nfsd4_session *ses)
666 struct nfs4_client *clp = ses->se_client;
668 spin_lock(&clp->cl_lock);
669 __nfsd4_hash_conn(conn, ses);
670 spin_unlock(&clp->cl_lock);
673 static int nfsd4_register_conn(struct nfsd4_conn *conn)
675 conn->cn_xpt_user.callback = nfsd4_conn_lost;
676 return register_xpt_user(conn->cn_xprt, &conn->cn_xpt_user);
679 static __be32 nfsd4_new_conn(struct svc_rqst *rqstp, struct nfsd4_session *ses, u32 dir)
681 struct nfsd4_conn *conn;
682 int ret;
684 conn = alloc_conn(rqstp, dir);
685 if (!conn)
686 return nfserr_jukebox;
687 nfsd4_hash_conn(conn, ses);
688 ret = nfsd4_register_conn(conn);
689 if (ret)
690 /* oops; xprt is already down: */
691 nfsd4_conn_lost(&conn->cn_xpt_user);
692 return nfs_ok;
695 static __be32 nfsd4_new_conn_from_crses(struct svc_rqst *rqstp, struct nfsd4_session *ses)
697 u32 dir = NFS4_CDFC4_FORE;
699 if (ses->se_flags & SESSION4_BACK_CHAN)
700 dir |= NFS4_CDFC4_BACK;
702 return nfsd4_new_conn(rqstp, ses, dir);
705 /* must be called under client_lock */
706 static void nfsd4_del_conns(struct nfsd4_session *s)
708 struct nfs4_client *clp = s->se_client;
709 struct nfsd4_conn *c;
711 spin_lock(&clp->cl_lock);
712 while (!list_empty(&s->se_conns)) {
713 c = list_first_entry(&s->se_conns, struct nfsd4_conn, cn_persession);
714 list_del_init(&c->cn_persession);
715 spin_unlock(&clp->cl_lock);
717 unregister_xpt_user(c->cn_xprt, &c->cn_xpt_user);
718 free_conn(c);
720 spin_lock(&clp->cl_lock);
722 spin_unlock(&clp->cl_lock);
725 void free_session(struct kref *kref)
727 struct nfsd4_session *ses;
728 int mem;
730 ses = container_of(kref, struct nfsd4_session, se_ref);
731 nfsd4_del_conns(ses);
732 spin_lock(&nfsd_drc_lock);
733 mem = ses->se_fchannel.maxreqs * slot_bytes(&ses->se_fchannel);
734 nfsd_drc_mem_used -= mem;
735 spin_unlock(&nfsd_drc_lock);
736 free_session_slots(ses);
737 kfree(ses);
740 static struct nfsd4_session *alloc_init_session(struct svc_rqst *rqstp, struct nfs4_client *clp, struct nfsd4_create_session *cses)
742 struct nfsd4_session *new;
743 struct nfsd4_channel_attrs *fchan = &cses->fore_channel;
744 int numslots, slotsize;
745 int status;
746 int idx;
749 * Note decreasing slot size below client's request may
750 * make it difficult for client to function correctly, whereas
751 * decreasing the number of slots will (just?) affect
752 * performance. When short on memory we therefore prefer to
753 * decrease number of slots instead of their size.
755 slotsize = nfsd4_sanitize_slot_size(fchan->maxresp_cached);
756 numslots = nfsd4_get_drc_mem(slotsize, fchan->maxreqs);
757 if (numslots < 1)
758 return NULL;
760 new = alloc_session(slotsize, numslots);
761 if (!new) {
762 nfsd4_put_drc_mem(slotsize, fchan->maxreqs);
763 return NULL;
765 init_forechannel_attrs(&new->se_fchannel, fchan, numslots, slotsize);
767 new->se_client = clp;
768 gen_sessionid(new);
770 INIT_LIST_HEAD(&new->se_conns);
772 new->se_cb_seq_nr = 1;
773 new->se_flags = cses->flags;
774 new->se_cb_prog = cses->callback_prog;
775 kref_init(&new->se_ref);
776 idx = hash_sessionid(&new->se_sessionid);
777 spin_lock(&client_lock);
778 list_add(&new->se_hash, &sessionid_hashtbl[idx]);
779 spin_lock(&clp->cl_lock);
780 list_add(&new->se_perclnt, &clp->cl_sessions);
781 spin_unlock(&clp->cl_lock);
782 spin_unlock(&client_lock);
784 status = nfsd4_new_conn_from_crses(rqstp, new);
785 /* whoops: benny points out, status is ignored! (err, or bogus) */
786 if (status) {
787 free_session(&new->se_ref);
788 return NULL;
790 if (cses->flags & SESSION4_BACK_CHAN) {
791 struct sockaddr *sa = svc_addr(rqstp);
793 * This is a little silly; with sessions there's no real
794 * use for the callback address. Use the peer address
795 * as a reasonable default for now, but consider fixing
796 * the rpc client not to require an address in the
797 * future:
799 rpc_copy_addr((struct sockaddr *)&clp->cl_cb_conn.cb_addr, sa);
800 clp->cl_cb_conn.cb_addrlen = svc_addr_len(sa);
802 nfsd4_probe_callback(clp);
803 return new;
806 /* caller must hold client_lock */
807 static struct nfsd4_session *
808 find_in_sessionid_hashtbl(struct nfs4_sessionid *sessionid)
810 struct nfsd4_session *elem;
811 int idx;
813 dump_sessionid(__func__, sessionid);
814 idx = hash_sessionid(sessionid);
815 /* Search in the appropriate list */
816 list_for_each_entry(elem, &sessionid_hashtbl[idx], se_hash) {
817 if (!memcmp(elem->se_sessionid.data, sessionid->data,
818 NFS4_MAX_SESSIONID_LEN)) {
819 return elem;
823 dprintk("%s: session not found\n", __func__);
824 return NULL;
827 /* caller must hold client_lock */
828 static void
829 unhash_session(struct nfsd4_session *ses)
831 list_del(&ses->se_hash);
832 spin_lock(&ses->se_client->cl_lock);
833 list_del(&ses->se_perclnt);
834 spin_unlock(&ses->se_client->cl_lock);
837 /* must be called under the client_lock */
838 static inline void
839 renew_client_locked(struct nfs4_client *clp)
841 if (is_client_expired(clp)) {
842 dprintk("%s: client (clientid %08x/%08x) already expired\n",
843 __func__,
844 clp->cl_clientid.cl_boot,
845 clp->cl_clientid.cl_id);
846 return;
850 * Move client to the end to the LRU list.
852 dprintk("renewing client (clientid %08x/%08x)\n",
853 clp->cl_clientid.cl_boot,
854 clp->cl_clientid.cl_id);
855 list_move_tail(&clp->cl_lru, &client_lru);
856 clp->cl_time = get_seconds();
859 static inline void
860 renew_client(struct nfs4_client *clp)
862 spin_lock(&client_lock);
863 renew_client_locked(clp);
864 spin_unlock(&client_lock);
867 /* SETCLIENTID and SETCLIENTID_CONFIRM Helper functions */
868 static int
869 STALE_CLIENTID(clientid_t *clid)
871 if (clid->cl_boot == boot_time)
872 return 0;
873 dprintk("NFSD stale clientid (%08x/%08x) boot_time %08lx\n",
874 clid->cl_boot, clid->cl_id, boot_time);
875 return 1;
879 * XXX Should we use a slab cache ?
880 * This type of memory management is somewhat inefficient, but we use it
881 * anyway since SETCLIENTID is not a common operation.
883 static struct nfs4_client *alloc_client(struct xdr_netobj name)
885 struct nfs4_client *clp;
887 clp = kzalloc(sizeof(struct nfs4_client), GFP_KERNEL);
888 if (clp == NULL)
889 return NULL;
890 clp->cl_name.data = kmalloc(name.len, GFP_KERNEL);
891 if (clp->cl_name.data == NULL) {
892 kfree(clp);
893 return NULL;
895 memcpy(clp->cl_name.data, name.data, name.len);
896 clp->cl_name.len = name.len;
897 return clp;
900 static inline void
901 free_client(struct nfs4_client *clp)
903 while (!list_empty(&clp->cl_sessions)) {
904 struct nfsd4_session *ses;
905 ses = list_entry(clp->cl_sessions.next, struct nfsd4_session,
906 se_perclnt);
907 list_del(&ses->se_perclnt);
908 nfsd4_put_session(ses);
910 if (clp->cl_cred.cr_group_info)
911 put_group_info(clp->cl_cred.cr_group_info);
912 kfree(clp->cl_principal);
913 kfree(clp->cl_name.data);
914 kfree(clp);
917 void
918 release_session_client(struct nfsd4_session *session)
920 struct nfs4_client *clp = session->se_client;
922 if (!atomic_dec_and_lock(&clp->cl_refcount, &client_lock))
923 return;
924 if (is_client_expired(clp)) {
925 free_client(clp);
926 session->se_client = NULL;
927 } else
928 renew_client_locked(clp);
929 spin_unlock(&client_lock);
932 /* must be called under the client_lock */
933 static inline void
934 unhash_client_locked(struct nfs4_client *clp)
936 struct nfsd4_session *ses;
938 mark_client_expired(clp);
939 list_del(&clp->cl_lru);
940 spin_lock(&clp->cl_lock);
941 list_for_each_entry(ses, &clp->cl_sessions, se_perclnt)
942 list_del_init(&ses->se_hash);
943 spin_unlock(&clp->cl_lock);
946 static void
947 expire_client(struct nfs4_client *clp)
949 struct nfs4_stateowner *sop;
950 struct nfs4_delegation *dp;
951 struct list_head reaplist;
953 INIT_LIST_HEAD(&reaplist);
954 spin_lock(&recall_lock);
955 while (!list_empty(&clp->cl_delegations)) {
956 dp = list_entry(clp->cl_delegations.next, struct nfs4_delegation, dl_perclnt);
957 list_del_init(&dp->dl_perclnt);
958 list_move(&dp->dl_recall_lru, &reaplist);
960 spin_unlock(&recall_lock);
961 while (!list_empty(&reaplist)) {
962 dp = list_entry(reaplist.next, struct nfs4_delegation, dl_recall_lru);
963 list_del_init(&dp->dl_recall_lru);
964 unhash_delegation(dp);
966 while (!list_empty(&clp->cl_openowners)) {
967 sop = list_entry(clp->cl_openowners.next, struct nfs4_stateowner, so_perclient);
968 release_openowner(sop);
970 nfsd4_shutdown_callback(clp);
971 if (clp->cl_cb_conn.cb_xprt)
972 svc_xprt_put(clp->cl_cb_conn.cb_xprt);
973 list_del(&clp->cl_idhash);
974 list_del(&clp->cl_strhash);
975 spin_lock(&client_lock);
976 unhash_client_locked(clp);
977 if (atomic_read(&clp->cl_refcount) == 0)
978 free_client(clp);
979 spin_unlock(&client_lock);
982 static void copy_verf(struct nfs4_client *target, nfs4_verifier *source)
984 memcpy(target->cl_verifier.data, source->data,
985 sizeof(target->cl_verifier.data));
988 static void copy_clid(struct nfs4_client *target, struct nfs4_client *source)
990 target->cl_clientid.cl_boot = source->cl_clientid.cl_boot;
991 target->cl_clientid.cl_id = source->cl_clientid.cl_id;
994 static void copy_cred(struct svc_cred *target, struct svc_cred *source)
996 target->cr_uid = source->cr_uid;
997 target->cr_gid = source->cr_gid;
998 target->cr_group_info = source->cr_group_info;
999 get_group_info(target->cr_group_info);
1002 static int same_name(const char *n1, const char *n2)
1004 return 0 == memcmp(n1, n2, HEXDIR_LEN);
1007 static int
1008 same_verf(nfs4_verifier *v1, nfs4_verifier *v2)
1010 return 0 == memcmp(v1->data, v2->data, sizeof(v1->data));
1013 static int
1014 same_clid(clientid_t *cl1, clientid_t *cl2)
1016 return (cl1->cl_boot == cl2->cl_boot) && (cl1->cl_id == cl2->cl_id);
1019 /* XXX what about NGROUP */
1020 static int
1021 same_creds(struct svc_cred *cr1, struct svc_cred *cr2)
1023 return cr1->cr_uid == cr2->cr_uid;
1026 static void gen_clid(struct nfs4_client *clp)
1028 static u32 current_clientid = 1;
1030 clp->cl_clientid.cl_boot = boot_time;
1031 clp->cl_clientid.cl_id = current_clientid++;
1034 static void gen_confirm(struct nfs4_client *clp)
1036 static u32 i;
1037 u32 *p;
1039 p = (u32 *)clp->cl_confirm.data;
1040 *p++ = get_seconds();
1041 *p++ = i++;
1044 static struct nfs4_client *create_client(struct xdr_netobj name, char *recdir,
1045 struct svc_rqst *rqstp, nfs4_verifier *verf)
1047 struct nfs4_client *clp;
1048 struct sockaddr *sa = svc_addr(rqstp);
1049 char *princ;
1051 clp = alloc_client(name);
1052 if (clp == NULL)
1053 return NULL;
1055 INIT_LIST_HEAD(&clp->cl_sessions);
1057 princ = svc_gss_principal(rqstp);
1058 if (princ) {
1059 clp->cl_principal = kstrdup(princ, GFP_KERNEL);
1060 if (clp->cl_principal == NULL) {
1061 free_client(clp);
1062 return NULL;
1066 memcpy(clp->cl_recdir, recdir, HEXDIR_LEN);
1067 atomic_set(&clp->cl_refcount, 0);
1068 clp->cl_cb_state = NFSD4_CB_UNKNOWN;
1069 INIT_LIST_HEAD(&clp->cl_idhash);
1070 INIT_LIST_HEAD(&clp->cl_strhash);
1071 INIT_LIST_HEAD(&clp->cl_openowners);
1072 INIT_LIST_HEAD(&clp->cl_delegations);
1073 INIT_LIST_HEAD(&clp->cl_lru);
1074 INIT_LIST_HEAD(&clp->cl_callbacks);
1075 spin_lock_init(&clp->cl_lock);
1076 INIT_WORK(&clp->cl_cb_null.cb_work, nfsd4_do_callback_rpc);
1077 clp->cl_time = get_seconds();
1078 clear_bit(0, &clp->cl_cb_slot_busy);
1079 rpc_init_wait_queue(&clp->cl_cb_waitq, "Backchannel slot table");
1080 copy_verf(clp, verf);
1081 rpc_copy_addr((struct sockaddr *) &clp->cl_addr, sa);
1082 clp->cl_flavor = rqstp->rq_flavor;
1083 copy_cred(&clp->cl_cred, &rqstp->rq_cred);
1084 gen_confirm(clp);
1085 clp->cl_cb_session = NULL;
1086 return clp;
1089 static int check_name(struct xdr_netobj name)
1091 if (name.len == 0)
1092 return 0;
1093 if (name.len > NFS4_OPAQUE_LIMIT) {
1094 dprintk("NFSD: check_name: name too long(%d)!\n", name.len);
1095 return 0;
1097 return 1;
1100 static void
1101 add_to_unconfirmed(struct nfs4_client *clp, unsigned int strhashval)
1103 unsigned int idhashval;
1105 list_add(&clp->cl_strhash, &unconf_str_hashtbl[strhashval]);
1106 idhashval = clientid_hashval(clp->cl_clientid.cl_id);
1107 list_add(&clp->cl_idhash, &unconf_id_hashtbl[idhashval]);
1108 renew_client(clp);
1111 static void
1112 move_to_confirmed(struct nfs4_client *clp)
1114 unsigned int idhashval = clientid_hashval(clp->cl_clientid.cl_id);
1115 unsigned int strhashval;
1117 dprintk("NFSD: move_to_confirm nfs4_client %p\n", clp);
1118 list_move(&clp->cl_idhash, &conf_id_hashtbl[idhashval]);
1119 strhashval = clientstr_hashval(clp->cl_recdir);
1120 list_move(&clp->cl_strhash, &conf_str_hashtbl[strhashval]);
1121 renew_client(clp);
1124 static struct nfs4_client *
1125 find_confirmed_client(clientid_t *clid)
1127 struct nfs4_client *clp;
1128 unsigned int idhashval = clientid_hashval(clid->cl_id);
1130 list_for_each_entry(clp, &conf_id_hashtbl[idhashval], cl_idhash) {
1131 if (same_clid(&clp->cl_clientid, clid))
1132 return clp;
1134 return NULL;
1137 static struct nfs4_client *
1138 find_unconfirmed_client(clientid_t *clid)
1140 struct nfs4_client *clp;
1141 unsigned int idhashval = clientid_hashval(clid->cl_id);
1143 list_for_each_entry(clp, &unconf_id_hashtbl[idhashval], cl_idhash) {
1144 if (same_clid(&clp->cl_clientid, clid))
1145 return clp;
1147 return NULL;
1150 static bool clp_used_exchangeid(struct nfs4_client *clp)
1152 return clp->cl_exchange_flags != 0;
1155 static struct nfs4_client *
1156 find_confirmed_client_by_str(const char *dname, unsigned int hashval)
1158 struct nfs4_client *clp;
1160 list_for_each_entry(clp, &conf_str_hashtbl[hashval], cl_strhash) {
1161 if (same_name(clp->cl_recdir, dname))
1162 return clp;
1164 return NULL;
1167 static struct nfs4_client *
1168 find_unconfirmed_client_by_str(const char *dname, unsigned int hashval)
1170 struct nfs4_client *clp;
1172 list_for_each_entry(clp, &unconf_str_hashtbl[hashval], cl_strhash) {
1173 if (same_name(clp->cl_recdir, dname))
1174 return clp;
1176 return NULL;
1179 static void rpc_svcaddr2sockaddr(struct sockaddr *sa, unsigned short family, union svc_addr_u *svcaddr)
1181 switch (family) {
1182 case AF_INET:
1183 ((struct sockaddr_in *)sa)->sin_family = AF_INET;
1184 ((struct sockaddr_in *)sa)->sin_addr = svcaddr->addr;
1185 return;
1186 case AF_INET6:
1187 ((struct sockaddr_in6 *)sa)->sin6_family = AF_INET6;
1188 ((struct sockaddr_in6 *)sa)->sin6_addr = svcaddr->addr6;
1189 return;
1193 static void
1194 gen_callback(struct nfs4_client *clp, struct nfsd4_setclientid *se, struct svc_rqst *rqstp)
1196 struct nfs4_cb_conn *conn = &clp->cl_cb_conn;
1197 struct sockaddr *sa = svc_addr(rqstp);
1198 u32 scopeid = rpc_get_scope_id(sa);
1199 unsigned short expected_family;
1201 /* Currently, we only support tcp and tcp6 for the callback channel */
1202 if (se->se_callback_netid_len == 3 &&
1203 !memcmp(se->se_callback_netid_val, "tcp", 3))
1204 expected_family = AF_INET;
1205 else if (se->se_callback_netid_len == 4 &&
1206 !memcmp(se->se_callback_netid_val, "tcp6", 4))
1207 expected_family = AF_INET6;
1208 else
1209 goto out_err;
1211 conn->cb_addrlen = rpc_uaddr2sockaddr(se->se_callback_addr_val,
1212 se->se_callback_addr_len,
1213 (struct sockaddr *)&conn->cb_addr,
1214 sizeof(conn->cb_addr));
1216 if (!conn->cb_addrlen || conn->cb_addr.ss_family != expected_family)
1217 goto out_err;
1219 if (conn->cb_addr.ss_family == AF_INET6)
1220 ((struct sockaddr_in6 *)&conn->cb_addr)->sin6_scope_id = scopeid;
1222 conn->cb_prog = se->se_callback_prog;
1223 conn->cb_ident = se->se_callback_ident;
1224 rpc_svcaddr2sockaddr((struct sockaddr *)&conn->cb_saddr, expected_family, &rqstp->rq_daddr);
1225 return;
1226 out_err:
1227 conn->cb_addr.ss_family = AF_UNSPEC;
1228 conn->cb_addrlen = 0;
1229 dprintk(KERN_INFO "NFSD: this client (clientid %08x/%08x) "
1230 "will not receive delegations\n",
1231 clp->cl_clientid.cl_boot, clp->cl_clientid.cl_id);
1233 return;
1237 * Cache a reply. nfsd4_check_drc_limit() has bounded the cache size.
1239 void
1240 nfsd4_store_cache_entry(struct nfsd4_compoundres *resp)
1242 struct nfsd4_slot *slot = resp->cstate.slot;
1243 unsigned int base;
1245 dprintk("--> %s slot %p\n", __func__, slot);
1247 slot->sl_opcnt = resp->opcnt;
1248 slot->sl_status = resp->cstate.status;
1250 if (nfsd4_not_cached(resp)) {
1251 slot->sl_datalen = 0;
1252 return;
1254 slot->sl_datalen = (char *)resp->p - (char *)resp->cstate.datap;
1255 base = (char *)resp->cstate.datap -
1256 (char *)resp->xbuf->head[0].iov_base;
1257 if (read_bytes_from_xdr_buf(resp->xbuf, base, slot->sl_data,
1258 slot->sl_datalen))
1259 WARN("%s: sessions DRC could not cache compound\n", __func__);
1260 return;
1264 * Encode the replay sequence operation from the slot values.
1265 * If cachethis is FALSE encode the uncached rep error on the next
1266 * operation which sets resp->p and increments resp->opcnt for
1267 * nfs4svc_encode_compoundres.
1270 static __be32
1271 nfsd4_enc_sequence_replay(struct nfsd4_compoundargs *args,
1272 struct nfsd4_compoundres *resp)
1274 struct nfsd4_op *op;
1275 struct nfsd4_slot *slot = resp->cstate.slot;
1277 dprintk("--> %s resp->opcnt %d cachethis %u \n", __func__,
1278 resp->opcnt, resp->cstate.slot->sl_cachethis);
1280 /* Encode the replayed sequence operation */
1281 op = &args->ops[resp->opcnt - 1];
1282 nfsd4_encode_operation(resp, op);
1284 /* Return nfserr_retry_uncached_rep in next operation. */
1285 if (args->opcnt > 1 && slot->sl_cachethis == 0) {
1286 op = &args->ops[resp->opcnt++];
1287 op->status = nfserr_retry_uncached_rep;
1288 nfsd4_encode_operation(resp, op);
1290 return op->status;
1294 * The sequence operation is not cached because we can use the slot and
1295 * session values.
1297 __be32
1298 nfsd4_replay_cache_entry(struct nfsd4_compoundres *resp,
1299 struct nfsd4_sequence *seq)
1301 struct nfsd4_slot *slot = resp->cstate.slot;
1302 __be32 status;
1304 dprintk("--> %s slot %p\n", __func__, slot);
1306 /* Either returns 0 or nfserr_retry_uncached */
1307 status = nfsd4_enc_sequence_replay(resp->rqstp->rq_argp, resp);
1308 if (status == nfserr_retry_uncached_rep)
1309 return status;
1311 /* The sequence operation has been encoded, cstate->datap set. */
1312 memcpy(resp->cstate.datap, slot->sl_data, slot->sl_datalen);
1314 resp->opcnt = slot->sl_opcnt;
1315 resp->p = resp->cstate.datap + XDR_QUADLEN(slot->sl_datalen);
1316 status = slot->sl_status;
1318 return status;
1322 * Set the exchange_id flags returned by the server.
1324 static void
1325 nfsd4_set_ex_flags(struct nfs4_client *new, struct nfsd4_exchange_id *clid)
1327 /* pNFS is not supported */
1328 new->cl_exchange_flags |= EXCHGID4_FLAG_USE_NON_PNFS;
1330 /* Referrals are supported, Migration is not. */
1331 new->cl_exchange_flags |= EXCHGID4_FLAG_SUPP_MOVED_REFER;
1333 /* set the wire flags to return to client. */
1334 clid->flags = new->cl_exchange_flags;
1337 __be32
1338 nfsd4_exchange_id(struct svc_rqst *rqstp,
1339 struct nfsd4_compound_state *cstate,
1340 struct nfsd4_exchange_id *exid)
1342 struct nfs4_client *unconf, *conf, *new;
1343 int status;
1344 unsigned int strhashval;
1345 char dname[HEXDIR_LEN];
1346 char addr_str[INET6_ADDRSTRLEN];
1347 nfs4_verifier verf = exid->verifier;
1348 struct sockaddr *sa = svc_addr(rqstp);
1350 rpc_ntop(sa, addr_str, sizeof(addr_str));
1351 dprintk("%s rqstp=%p exid=%p clname.len=%u clname.data=%p "
1352 "ip_addr=%s flags %x, spa_how %d\n",
1353 __func__, rqstp, exid, exid->clname.len, exid->clname.data,
1354 addr_str, exid->flags, exid->spa_how);
1356 if (!check_name(exid->clname) || (exid->flags & ~EXCHGID4_FLAG_MASK_A))
1357 return nfserr_inval;
1359 /* Currently only support SP4_NONE */
1360 switch (exid->spa_how) {
1361 case SP4_NONE:
1362 break;
1363 case SP4_SSV:
1364 return nfserr_serverfault;
1365 default:
1366 BUG(); /* checked by xdr code */
1367 case SP4_MACH_CRED:
1368 return nfserr_serverfault; /* no excuse :-/ */
1371 status = nfs4_make_rec_clidname(dname, &exid->clname);
1373 if (status)
1374 goto error;
1376 strhashval = clientstr_hashval(dname);
1378 nfs4_lock_state();
1379 status = nfs_ok;
1381 conf = find_confirmed_client_by_str(dname, strhashval);
1382 if (conf) {
1383 if (!clp_used_exchangeid(conf)) {
1384 status = nfserr_clid_inuse; /* XXX: ? */
1385 goto out;
1387 if (!same_verf(&verf, &conf->cl_verifier)) {
1388 /* 18.35.4 case 8 */
1389 if (exid->flags & EXCHGID4_FLAG_UPD_CONFIRMED_REC_A) {
1390 status = nfserr_not_same;
1391 goto out;
1393 /* Client reboot: destroy old state */
1394 expire_client(conf);
1395 goto out_new;
1397 if (!same_creds(&conf->cl_cred, &rqstp->rq_cred)) {
1398 /* 18.35.4 case 9 */
1399 if (exid->flags & EXCHGID4_FLAG_UPD_CONFIRMED_REC_A) {
1400 status = nfserr_perm;
1401 goto out;
1403 expire_client(conf);
1404 goto out_new;
1407 * Set bit when the owner id and verifier map to an already
1408 * confirmed client id (18.35.3).
1410 exid->flags |= EXCHGID4_FLAG_CONFIRMED_R;
1413 * Falling into 18.35.4 case 2, possible router replay.
1414 * Leave confirmed record intact and return same result.
1416 copy_verf(conf, &verf);
1417 new = conf;
1418 goto out_copy;
1421 /* 18.35.4 case 7 */
1422 if (exid->flags & EXCHGID4_FLAG_UPD_CONFIRMED_REC_A) {
1423 status = nfserr_noent;
1424 goto out;
1427 unconf = find_unconfirmed_client_by_str(dname, strhashval);
1428 if (unconf) {
1430 * Possible retry or client restart. Per 18.35.4 case 4,
1431 * a new unconfirmed record should be generated regardless
1432 * of whether any properties have changed.
1434 expire_client(unconf);
1437 out_new:
1438 /* Normal case */
1439 new = create_client(exid->clname, dname, rqstp, &verf);
1440 if (new == NULL) {
1441 status = nfserr_jukebox;
1442 goto out;
1445 gen_clid(new);
1446 add_to_unconfirmed(new, strhashval);
1447 out_copy:
1448 exid->clientid.cl_boot = new->cl_clientid.cl_boot;
1449 exid->clientid.cl_id = new->cl_clientid.cl_id;
1451 exid->seqid = 1;
1452 nfsd4_set_ex_flags(new, exid);
1454 dprintk("nfsd4_exchange_id seqid %d flags %x\n",
1455 new->cl_cs_slot.sl_seqid, new->cl_exchange_flags);
1456 status = nfs_ok;
1458 out:
1459 nfs4_unlock_state();
1460 error:
1461 dprintk("nfsd4_exchange_id returns %d\n", ntohl(status));
1462 return status;
1465 static int
1466 check_slot_seqid(u32 seqid, u32 slot_seqid, int slot_inuse)
1468 dprintk("%s enter. seqid %d slot_seqid %d\n", __func__, seqid,
1469 slot_seqid);
1471 /* The slot is in use, and no response has been sent. */
1472 if (slot_inuse) {
1473 if (seqid == slot_seqid)
1474 return nfserr_jukebox;
1475 else
1476 return nfserr_seq_misordered;
1478 /* Normal */
1479 if (likely(seqid == slot_seqid + 1))
1480 return nfs_ok;
1481 /* Replay */
1482 if (seqid == slot_seqid)
1483 return nfserr_replay_cache;
1484 /* Wraparound */
1485 if (seqid == 1 && (slot_seqid + 1) == 0)
1486 return nfs_ok;
1487 /* Misordered replay or misordered new request */
1488 return nfserr_seq_misordered;
1492 * Cache the create session result into the create session single DRC
1493 * slot cache by saving the xdr structure. sl_seqid has been set.
1494 * Do this for solo or embedded create session operations.
1496 static void
1497 nfsd4_cache_create_session(struct nfsd4_create_session *cr_ses,
1498 struct nfsd4_clid_slot *slot, int nfserr)
1500 slot->sl_status = nfserr;
1501 memcpy(&slot->sl_cr_ses, cr_ses, sizeof(*cr_ses));
1504 static __be32
1505 nfsd4_replay_create_session(struct nfsd4_create_session *cr_ses,
1506 struct nfsd4_clid_slot *slot)
1508 memcpy(cr_ses, &slot->sl_cr_ses, sizeof(*cr_ses));
1509 return slot->sl_status;
1512 __be32
1513 nfsd4_create_session(struct svc_rqst *rqstp,
1514 struct nfsd4_compound_state *cstate,
1515 struct nfsd4_create_session *cr_ses)
1517 struct sockaddr *sa = svc_addr(rqstp);
1518 struct nfs4_client *conf, *unconf;
1519 struct nfsd4_session *new;
1520 struct nfsd4_clid_slot *cs_slot = NULL;
1521 bool confirm_me = false;
1522 int status = 0;
1524 if (cr_ses->flags & ~SESSION4_FLAG_MASK_A)
1525 return nfserr_inval;
1527 nfs4_lock_state();
1528 unconf = find_unconfirmed_client(&cr_ses->clientid);
1529 conf = find_confirmed_client(&cr_ses->clientid);
1531 if (conf) {
1532 cs_slot = &conf->cl_cs_slot;
1533 status = check_slot_seqid(cr_ses->seqid, cs_slot->sl_seqid, 0);
1534 if (status == nfserr_replay_cache) {
1535 dprintk("Got a create_session replay! seqid= %d\n",
1536 cs_slot->sl_seqid);
1537 /* Return the cached reply status */
1538 status = nfsd4_replay_create_session(cr_ses, cs_slot);
1539 goto out;
1540 } else if (cr_ses->seqid != cs_slot->sl_seqid + 1) {
1541 status = nfserr_seq_misordered;
1542 dprintk("Sequence misordered!\n");
1543 dprintk("Expected seqid= %d but got seqid= %d\n",
1544 cs_slot->sl_seqid, cr_ses->seqid);
1545 goto out;
1547 } else if (unconf) {
1548 if (!same_creds(&unconf->cl_cred, &rqstp->rq_cred) ||
1549 !rpc_cmp_addr(sa, (struct sockaddr *) &unconf->cl_addr)) {
1550 status = nfserr_clid_inuse;
1551 goto out;
1554 cs_slot = &unconf->cl_cs_slot;
1555 status = check_slot_seqid(cr_ses->seqid, cs_slot->sl_seqid, 0);
1556 if (status) {
1557 /* an unconfirmed replay returns misordered */
1558 status = nfserr_seq_misordered;
1559 goto out;
1562 confirm_me = true;
1563 conf = unconf;
1564 } else {
1565 status = nfserr_stale_clientid;
1566 goto out;
1570 * XXX: we should probably set this at creation time, and check
1571 * for consistent minorversion use throughout:
1573 conf->cl_minorversion = 1;
1575 * We do not support RDMA or persistent sessions
1577 cr_ses->flags &= ~SESSION4_PERSIST;
1578 cr_ses->flags &= ~SESSION4_RDMA;
1580 status = nfserr_jukebox;
1581 new = alloc_init_session(rqstp, conf, cr_ses);
1582 if (!new)
1583 goto out;
1584 status = nfs_ok;
1585 memcpy(cr_ses->sessionid.data, new->se_sessionid.data,
1586 NFS4_MAX_SESSIONID_LEN);
1587 memcpy(&cr_ses->fore_channel, &new->se_fchannel,
1588 sizeof(struct nfsd4_channel_attrs));
1589 cs_slot->sl_seqid++;
1590 cr_ses->seqid = cs_slot->sl_seqid;
1592 /* cache solo and embedded create sessions under the state lock */
1593 nfsd4_cache_create_session(cr_ses, cs_slot, status);
1594 if (confirm_me)
1595 move_to_confirmed(conf);
1596 out:
1597 nfs4_unlock_state();
1598 dprintk("%s returns %d\n", __func__, ntohl(status));
1599 return status;
1602 static bool nfsd4_last_compound_op(struct svc_rqst *rqstp)
1604 struct nfsd4_compoundres *resp = rqstp->rq_resp;
1605 struct nfsd4_compoundargs *argp = rqstp->rq_argp;
1607 return argp->opcnt == resp->opcnt;
1610 static __be32 nfsd4_map_bcts_dir(u32 *dir)
1612 switch (*dir) {
1613 case NFS4_CDFC4_FORE:
1614 case NFS4_CDFC4_BACK:
1615 return nfs_ok;
1616 case NFS4_CDFC4_FORE_OR_BOTH:
1617 case NFS4_CDFC4_BACK_OR_BOTH:
1618 *dir = NFS4_CDFC4_BOTH;
1619 return nfs_ok;
1621 return nfserr_inval;
1624 __be32 nfsd4_bind_conn_to_session(struct svc_rqst *rqstp,
1625 struct nfsd4_compound_state *cstate,
1626 struct nfsd4_bind_conn_to_session *bcts)
1628 __be32 status;
1630 if (!nfsd4_last_compound_op(rqstp))
1631 return nfserr_not_only_op;
1632 spin_lock(&client_lock);
1633 cstate->session = find_in_sessionid_hashtbl(&bcts->sessionid);
1634 /* Sorta weird: we only need the refcnt'ing because new_conn acquires
1635 * client_lock iself: */
1636 if (cstate->session) {
1637 nfsd4_get_session(cstate->session);
1638 atomic_inc(&cstate->session->se_client->cl_refcount);
1640 spin_unlock(&client_lock);
1641 if (!cstate->session)
1642 return nfserr_badsession;
1644 status = nfsd4_map_bcts_dir(&bcts->dir);
1645 if (!status)
1646 nfsd4_new_conn(rqstp, cstate->session, bcts->dir);
1647 return status;
1650 static bool nfsd4_compound_in_session(struct nfsd4_session *session, struct nfs4_sessionid *sid)
1652 if (!session)
1653 return 0;
1654 return !memcmp(sid, &session->se_sessionid, sizeof(*sid));
1657 __be32
1658 nfsd4_destroy_session(struct svc_rqst *r,
1659 struct nfsd4_compound_state *cstate,
1660 struct nfsd4_destroy_session *sessionid)
1662 struct nfsd4_session *ses;
1663 u32 status = nfserr_badsession;
1665 /* Notes:
1666 * - The confirmed nfs4_client->cl_sessionid holds destroyed sessinid
1667 * - Should we return nfserr_back_chan_busy if waiting for
1668 * callbacks on to-be-destroyed session?
1669 * - Do we need to clear any callback info from previous session?
1672 if (nfsd4_compound_in_session(cstate->session, &sessionid->sessionid)) {
1673 if (!nfsd4_last_compound_op(r))
1674 return nfserr_not_only_op;
1676 dump_sessionid(__func__, &sessionid->sessionid);
1677 spin_lock(&client_lock);
1678 ses = find_in_sessionid_hashtbl(&sessionid->sessionid);
1679 if (!ses) {
1680 spin_unlock(&client_lock);
1681 goto out;
1684 unhash_session(ses);
1685 spin_unlock(&client_lock);
1687 nfs4_lock_state();
1688 nfsd4_probe_callback_sync(ses->se_client);
1689 nfs4_unlock_state();
1691 nfsd4_del_conns(ses);
1693 nfsd4_put_session(ses);
1694 status = nfs_ok;
1695 out:
1696 dprintk("%s returns %d\n", __func__, ntohl(status));
1697 return status;
1700 static struct nfsd4_conn *__nfsd4_find_conn(struct svc_xprt *xpt, struct nfsd4_session *s)
1702 struct nfsd4_conn *c;
1704 list_for_each_entry(c, &s->se_conns, cn_persession) {
1705 if (c->cn_xprt == xpt) {
1706 return c;
1709 return NULL;
1712 static void nfsd4_sequence_check_conn(struct nfsd4_conn *new, struct nfsd4_session *ses)
1714 struct nfs4_client *clp = ses->se_client;
1715 struct nfsd4_conn *c;
1716 int ret;
1718 spin_lock(&clp->cl_lock);
1719 c = __nfsd4_find_conn(new->cn_xprt, ses);
1720 if (c) {
1721 spin_unlock(&clp->cl_lock);
1722 free_conn(new);
1723 return;
1725 __nfsd4_hash_conn(new, ses);
1726 spin_unlock(&clp->cl_lock);
1727 ret = nfsd4_register_conn(new);
1728 if (ret)
1729 /* oops; xprt is already down: */
1730 nfsd4_conn_lost(&new->cn_xpt_user);
1731 return;
1734 static bool nfsd4_session_too_many_ops(struct svc_rqst *rqstp, struct nfsd4_session *session)
1736 struct nfsd4_compoundargs *args = rqstp->rq_argp;
1738 return args->opcnt > session->se_fchannel.maxops;
1741 __be32
1742 nfsd4_sequence(struct svc_rqst *rqstp,
1743 struct nfsd4_compound_state *cstate,
1744 struct nfsd4_sequence *seq)
1746 struct nfsd4_compoundres *resp = rqstp->rq_resp;
1747 struct nfsd4_session *session;
1748 struct nfsd4_slot *slot;
1749 struct nfsd4_conn *conn;
1750 int status;
1752 if (resp->opcnt != 1)
1753 return nfserr_sequence_pos;
1756 * Will be either used or freed by nfsd4_sequence_check_conn
1757 * below.
1759 conn = alloc_conn(rqstp, NFS4_CDFC4_FORE);
1760 if (!conn)
1761 return nfserr_jukebox;
1763 spin_lock(&client_lock);
1764 status = nfserr_badsession;
1765 session = find_in_sessionid_hashtbl(&seq->sessionid);
1766 if (!session)
1767 goto out;
1769 status = nfserr_too_many_ops;
1770 if (nfsd4_session_too_many_ops(rqstp, session))
1771 goto out;
1773 status = nfserr_badslot;
1774 if (seq->slotid >= session->se_fchannel.maxreqs)
1775 goto out;
1777 slot = session->se_slots[seq->slotid];
1778 dprintk("%s: slotid %d\n", __func__, seq->slotid);
1780 /* We do not negotiate the number of slots yet, so set the
1781 * maxslots to the session maxreqs which is used to encode
1782 * sr_highest_slotid and the sr_target_slot id to maxslots */
1783 seq->maxslots = session->se_fchannel.maxreqs;
1785 status = check_slot_seqid(seq->seqid, slot->sl_seqid, slot->sl_inuse);
1786 if (status == nfserr_replay_cache) {
1787 cstate->slot = slot;
1788 cstate->session = session;
1789 /* Return the cached reply status and set cstate->status
1790 * for nfsd4_proc_compound processing */
1791 status = nfsd4_replay_cache_entry(resp, seq);
1792 cstate->status = nfserr_replay_cache;
1793 goto out;
1795 if (status)
1796 goto out;
1798 nfsd4_sequence_check_conn(conn, session);
1799 conn = NULL;
1801 /* Success! bump slot seqid */
1802 slot->sl_inuse = true;
1803 slot->sl_seqid = seq->seqid;
1804 slot->sl_cachethis = seq->cachethis;
1806 cstate->slot = slot;
1807 cstate->session = session;
1809 out:
1810 /* Hold a session reference until done processing the compound. */
1811 if (cstate->session) {
1812 struct nfs4_client *clp = session->se_client;
1814 nfsd4_get_session(cstate->session);
1815 atomic_inc(&clp->cl_refcount);
1816 if (clp->cl_cb_state == NFSD4_CB_DOWN)
1817 seq->status_flags |= SEQ4_STATUS_CB_PATH_DOWN;
1819 kfree(conn);
1820 spin_unlock(&client_lock);
1821 dprintk("%s: return %d\n", __func__, ntohl(status));
1822 return status;
1825 __be32
1826 nfsd4_reclaim_complete(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate, struct nfsd4_reclaim_complete *rc)
1828 int status = 0;
1830 if (rc->rca_one_fs) {
1831 if (!cstate->current_fh.fh_dentry)
1832 return nfserr_nofilehandle;
1834 * We don't take advantage of the rca_one_fs case.
1835 * That's OK, it's optional, we can safely ignore it.
1837 return nfs_ok;
1840 nfs4_lock_state();
1841 status = nfserr_complete_already;
1842 if (cstate->session->se_client->cl_firststate)
1843 goto out;
1845 status = nfserr_stale_clientid;
1846 if (is_client_expired(cstate->session->se_client))
1848 * The following error isn't really legal.
1849 * But we only get here if the client just explicitly
1850 * destroyed the client. Surely it no longer cares what
1851 * error it gets back on an operation for the dead
1852 * client.
1854 goto out;
1856 status = nfs_ok;
1857 nfsd4_create_clid_dir(cstate->session->se_client);
1858 out:
1859 nfs4_unlock_state();
1860 return status;
1863 __be32
1864 nfsd4_setclientid(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
1865 struct nfsd4_setclientid *setclid)
1867 struct xdr_netobj clname = {
1868 .len = setclid->se_namelen,
1869 .data = setclid->se_name,
1871 nfs4_verifier clverifier = setclid->se_verf;
1872 unsigned int strhashval;
1873 struct nfs4_client *conf, *unconf, *new;
1874 __be32 status;
1875 char dname[HEXDIR_LEN];
1877 if (!check_name(clname))
1878 return nfserr_inval;
1880 status = nfs4_make_rec_clidname(dname, &clname);
1881 if (status)
1882 return status;
1885 * XXX The Duplicate Request Cache (DRC) has been checked (??)
1886 * We get here on a DRC miss.
1889 strhashval = clientstr_hashval(dname);
1891 nfs4_lock_state();
1892 conf = find_confirmed_client_by_str(dname, strhashval);
1893 if (conf) {
1894 /* RFC 3530 14.2.33 CASE 0: */
1895 status = nfserr_clid_inuse;
1896 if (clp_used_exchangeid(conf))
1897 goto out;
1898 if (!same_creds(&conf->cl_cred, &rqstp->rq_cred)) {
1899 char addr_str[INET6_ADDRSTRLEN];
1900 rpc_ntop((struct sockaddr *) &conf->cl_addr, addr_str,
1901 sizeof(addr_str));
1902 dprintk("NFSD: setclientid: string in use by client "
1903 "at %s\n", addr_str);
1904 goto out;
1908 * section 14.2.33 of RFC 3530 (under the heading "IMPLEMENTATION")
1909 * has a description of SETCLIENTID request processing consisting
1910 * of 5 bullet points, labeled as CASE0 - CASE4 below.
1912 unconf = find_unconfirmed_client_by_str(dname, strhashval);
1913 status = nfserr_jukebox;
1914 if (!conf) {
1916 * RFC 3530 14.2.33 CASE 4:
1917 * placed first, because it is the normal case
1919 if (unconf)
1920 expire_client(unconf);
1921 new = create_client(clname, dname, rqstp, &clverifier);
1922 if (new == NULL)
1923 goto out;
1924 gen_clid(new);
1925 } else if (same_verf(&conf->cl_verifier, &clverifier)) {
1927 * RFC 3530 14.2.33 CASE 1:
1928 * probable callback update
1930 if (unconf) {
1931 /* Note this is removing unconfirmed {*x***},
1932 * which is stronger than RFC recommended {vxc**}.
1933 * This has the advantage that there is at most
1934 * one {*x***} in either list at any time.
1936 expire_client(unconf);
1938 new = create_client(clname, dname, rqstp, &clverifier);
1939 if (new == NULL)
1940 goto out;
1941 copy_clid(new, conf);
1942 } else if (!unconf) {
1944 * RFC 3530 14.2.33 CASE 2:
1945 * probable client reboot; state will be removed if
1946 * confirmed.
1948 new = create_client(clname, dname, rqstp, &clverifier);
1949 if (new == NULL)
1950 goto out;
1951 gen_clid(new);
1952 } else {
1954 * RFC 3530 14.2.33 CASE 3:
1955 * probable client reboot; state will be removed if
1956 * confirmed.
1958 expire_client(unconf);
1959 new = create_client(clname, dname, rqstp, &clverifier);
1960 if (new == NULL)
1961 goto out;
1962 gen_clid(new);
1965 * XXX: we should probably set this at creation time, and check
1966 * for consistent minorversion use throughout:
1968 new->cl_minorversion = 0;
1969 gen_callback(new, setclid, rqstp);
1970 add_to_unconfirmed(new, strhashval);
1971 setclid->se_clientid.cl_boot = new->cl_clientid.cl_boot;
1972 setclid->se_clientid.cl_id = new->cl_clientid.cl_id;
1973 memcpy(setclid->se_confirm.data, new->cl_confirm.data, sizeof(setclid->se_confirm.data));
1974 status = nfs_ok;
1975 out:
1976 nfs4_unlock_state();
1977 return status;
1982 * Section 14.2.34 of RFC 3530 (under the heading "IMPLEMENTATION") has
1983 * a description of SETCLIENTID_CONFIRM request processing consisting of 4
1984 * bullets, labeled as CASE1 - CASE4 below.
1986 __be32
1987 nfsd4_setclientid_confirm(struct svc_rqst *rqstp,
1988 struct nfsd4_compound_state *cstate,
1989 struct nfsd4_setclientid_confirm *setclientid_confirm)
1991 struct sockaddr *sa = svc_addr(rqstp);
1992 struct nfs4_client *conf, *unconf;
1993 nfs4_verifier confirm = setclientid_confirm->sc_confirm;
1994 clientid_t * clid = &setclientid_confirm->sc_clientid;
1995 __be32 status;
1997 if (STALE_CLIENTID(clid))
1998 return nfserr_stale_clientid;
2000 * XXX The Duplicate Request Cache (DRC) has been checked (??)
2001 * We get here on a DRC miss.
2004 nfs4_lock_state();
2006 conf = find_confirmed_client(clid);
2007 unconf = find_unconfirmed_client(clid);
2009 status = nfserr_clid_inuse;
2010 if (conf && !rpc_cmp_addr((struct sockaddr *) &conf->cl_addr, sa))
2011 goto out;
2012 if (unconf && !rpc_cmp_addr((struct sockaddr *) &unconf->cl_addr, sa))
2013 goto out;
2016 * section 14.2.34 of RFC 3530 has a description of
2017 * SETCLIENTID_CONFIRM request processing consisting
2018 * of 4 bullet points, labeled as CASE1 - CASE4 below.
2020 if (conf && unconf && same_verf(&confirm, &unconf->cl_confirm)) {
2022 * RFC 3530 14.2.34 CASE 1:
2023 * callback update
2025 if (!same_creds(&conf->cl_cred, &unconf->cl_cred))
2026 status = nfserr_clid_inuse;
2027 else {
2028 nfsd4_change_callback(conf, &unconf->cl_cb_conn);
2029 nfsd4_probe_callback(conf);
2030 expire_client(unconf);
2031 status = nfs_ok;
2034 } else if (conf && !unconf) {
2036 * RFC 3530 14.2.34 CASE 2:
2037 * probable retransmitted request; play it safe and
2038 * do nothing.
2040 if (!same_creds(&conf->cl_cred, &rqstp->rq_cred))
2041 status = nfserr_clid_inuse;
2042 else
2043 status = nfs_ok;
2044 } else if (!conf && unconf
2045 && same_verf(&unconf->cl_confirm, &confirm)) {
2047 * RFC 3530 14.2.34 CASE 3:
2048 * Normal case; new or rebooted client:
2050 if (!same_creds(&unconf->cl_cred, &rqstp->rq_cred)) {
2051 status = nfserr_clid_inuse;
2052 } else {
2053 unsigned int hash =
2054 clientstr_hashval(unconf->cl_recdir);
2055 conf = find_confirmed_client_by_str(unconf->cl_recdir,
2056 hash);
2057 if (conf) {
2058 nfsd4_remove_clid_dir(conf);
2059 expire_client(conf);
2061 move_to_confirmed(unconf);
2062 conf = unconf;
2063 nfsd4_probe_callback(conf);
2064 status = nfs_ok;
2066 } else if ((!conf || (conf && !same_verf(&conf->cl_confirm, &confirm)))
2067 && (!unconf || (unconf && !same_verf(&unconf->cl_confirm,
2068 &confirm)))) {
2070 * RFC 3530 14.2.34 CASE 4:
2071 * Client probably hasn't noticed that we rebooted yet.
2073 status = nfserr_stale_clientid;
2074 } else {
2075 /* check that we have hit one of the cases...*/
2076 status = nfserr_clid_inuse;
2078 out:
2079 nfs4_unlock_state();
2080 return status;
2083 /* OPEN Share state helper functions */
2084 static inline struct nfs4_file *
2085 alloc_init_file(struct inode *ino)
2087 struct nfs4_file *fp;
2088 unsigned int hashval = file_hashval(ino);
2090 fp = kmem_cache_alloc(file_slab, GFP_KERNEL);
2091 if (fp) {
2092 atomic_set(&fp->fi_ref, 1);
2093 INIT_LIST_HEAD(&fp->fi_hash);
2094 INIT_LIST_HEAD(&fp->fi_stateids);
2095 INIT_LIST_HEAD(&fp->fi_delegations);
2096 fp->fi_inode = igrab(ino);
2097 fp->fi_id = current_fileid++;
2098 fp->fi_had_conflict = false;
2099 fp->fi_lease = NULL;
2100 memset(fp->fi_fds, 0, sizeof(fp->fi_fds));
2101 memset(fp->fi_access, 0, sizeof(fp->fi_access));
2102 spin_lock(&recall_lock);
2103 list_add(&fp->fi_hash, &file_hashtbl[hashval]);
2104 spin_unlock(&recall_lock);
2105 return fp;
2107 return NULL;
2110 static void
2111 nfsd4_free_slab(struct kmem_cache **slab)
2113 if (*slab == NULL)
2114 return;
2115 kmem_cache_destroy(*slab);
2116 *slab = NULL;
2119 void
2120 nfsd4_free_slabs(void)
2122 nfsd4_free_slab(&stateowner_slab);
2123 nfsd4_free_slab(&file_slab);
2124 nfsd4_free_slab(&stateid_slab);
2125 nfsd4_free_slab(&deleg_slab);
2128 static int
2129 nfsd4_init_slabs(void)
2131 stateowner_slab = kmem_cache_create("nfsd4_stateowners",
2132 sizeof(struct nfs4_stateowner), 0, 0, NULL);
2133 if (stateowner_slab == NULL)
2134 goto out_nomem;
2135 file_slab = kmem_cache_create("nfsd4_files",
2136 sizeof(struct nfs4_file), 0, 0, NULL);
2137 if (file_slab == NULL)
2138 goto out_nomem;
2139 stateid_slab = kmem_cache_create("nfsd4_stateids",
2140 sizeof(struct nfs4_stateid), 0, 0, NULL);
2141 if (stateid_slab == NULL)
2142 goto out_nomem;
2143 deleg_slab = kmem_cache_create("nfsd4_delegations",
2144 sizeof(struct nfs4_delegation), 0, 0, NULL);
2145 if (deleg_slab == NULL)
2146 goto out_nomem;
2147 return 0;
2148 out_nomem:
2149 nfsd4_free_slabs();
2150 dprintk("nfsd4: out of memory while initializing nfsv4\n");
2151 return -ENOMEM;
2154 void
2155 nfs4_free_stateowner(struct kref *kref)
2157 struct nfs4_stateowner *sop =
2158 container_of(kref, struct nfs4_stateowner, so_ref);
2159 kfree(sop->so_owner.data);
2160 kmem_cache_free(stateowner_slab, sop);
2163 static inline struct nfs4_stateowner *
2164 alloc_stateowner(struct xdr_netobj *owner)
2166 struct nfs4_stateowner *sop;
2168 if ((sop = kmem_cache_alloc(stateowner_slab, GFP_KERNEL))) {
2169 if ((sop->so_owner.data = kmalloc(owner->len, GFP_KERNEL))) {
2170 memcpy(sop->so_owner.data, owner->data, owner->len);
2171 sop->so_owner.len = owner->len;
2172 kref_init(&sop->so_ref);
2173 return sop;
2175 kmem_cache_free(stateowner_slab, sop);
2177 return NULL;
2180 static struct nfs4_stateowner *
2181 alloc_init_open_stateowner(unsigned int strhashval, struct nfs4_client *clp, struct nfsd4_open *open) {
2182 struct nfs4_stateowner *sop;
2183 struct nfs4_replay *rp;
2184 unsigned int idhashval;
2186 if (!(sop = alloc_stateowner(&open->op_owner)))
2187 return NULL;
2188 idhashval = ownerid_hashval(current_ownerid);
2189 INIT_LIST_HEAD(&sop->so_idhash);
2190 INIT_LIST_HEAD(&sop->so_strhash);
2191 INIT_LIST_HEAD(&sop->so_perclient);
2192 INIT_LIST_HEAD(&sop->so_stateids);
2193 INIT_LIST_HEAD(&sop->so_perstateid); /* not used */
2194 INIT_LIST_HEAD(&sop->so_close_lru);
2195 sop->so_time = 0;
2196 list_add(&sop->so_idhash, &ownerid_hashtbl[idhashval]);
2197 list_add(&sop->so_strhash, &ownerstr_hashtbl[strhashval]);
2198 list_add(&sop->so_perclient, &clp->cl_openowners);
2199 sop->so_is_open_owner = 1;
2200 sop->so_id = current_ownerid++;
2201 sop->so_client = clp;
2202 sop->so_seqid = open->op_seqid;
2203 sop->so_confirmed = 0;
2204 rp = &sop->so_replay;
2205 rp->rp_status = nfserr_serverfault;
2206 rp->rp_buflen = 0;
2207 rp->rp_buf = rp->rp_ibuf;
2208 return sop;
2211 static inline void
2212 init_stateid(struct nfs4_stateid *stp, struct nfs4_file *fp, struct nfsd4_open *open) {
2213 struct nfs4_stateowner *sop = open->op_stateowner;
2214 unsigned int hashval = stateid_hashval(sop->so_id, fp->fi_id);
2216 INIT_LIST_HEAD(&stp->st_hash);
2217 INIT_LIST_HEAD(&stp->st_perstateowner);
2218 INIT_LIST_HEAD(&stp->st_lockowners);
2219 INIT_LIST_HEAD(&stp->st_perfile);
2220 list_add(&stp->st_hash, &stateid_hashtbl[hashval]);
2221 list_add(&stp->st_perstateowner, &sop->so_stateids);
2222 list_add(&stp->st_perfile, &fp->fi_stateids);
2223 stp->st_stateowner = sop;
2224 get_nfs4_file(fp);
2225 stp->st_file = fp;
2226 stp->st_stateid.si_boot = boot_time;
2227 stp->st_stateid.si_stateownerid = sop->so_id;
2228 stp->st_stateid.si_fileid = fp->fi_id;
2229 stp->st_stateid.si_generation = 0;
2230 stp->st_access_bmap = 0;
2231 stp->st_deny_bmap = 0;
2232 __set_bit(open->op_share_access & ~NFS4_SHARE_WANT_MASK,
2233 &stp->st_access_bmap);
2234 __set_bit(open->op_share_deny, &stp->st_deny_bmap);
2235 stp->st_openstp = NULL;
2238 static void
2239 move_to_close_lru(struct nfs4_stateowner *sop)
2241 dprintk("NFSD: move_to_close_lru nfs4_stateowner %p\n", sop);
2243 list_move_tail(&sop->so_close_lru, &close_lru);
2244 sop->so_time = get_seconds();
2247 static int
2248 same_owner_str(struct nfs4_stateowner *sop, struct xdr_netobj *owner,
2249 clientid_t *clid)
2251 return (sop->so_owner.len == owner->len) &&
2252 0 == memcmp(sop->so_owner.data, owner->data, owner->len) &&
2253 (sop->so_client->cl_clientid.cl_id == clid->cl_id);
2256 static struct nfs4_stateowner *
2257 find_openstateowner_str(unsigned int hashval, struct nfsd4_open *open)
2259 struct nfs4_stateowner *so = NULL;
2261 list_for_each_entry(so, &ownerstr_hashtbl[hashval], so_strhash) {
2262 if (same_owner_str(so, &open->op_owner, &open->op_clientid))
2263 return so;
2265 return NULL;
2268 /* search file_hashtbl[] for file */
2269 static struct nfs4_file *
2270 find_file(struct inode *ino)
2272 unsigned int hashval = file_hashval(ino);
2273 struct nfs4_file *fp;
2275 spin_lock(&recall_lock);
2276 list_for_each_entry(fp, &file_hashtbl[hashval], fi_hash) {
2277 if (fp->fi_inode == ino) {
2278 get_nfs4_file(fp);
2279 spin_unlock(&recall_lock);
2280 return fp;
2283 spin_unlock(&recall_lock);
2284 return NULL;
2287 static inline int access_valid(u32 x, u32 minorversion)
2289 if ((x & NFS4_SHARE_ACCESS_MASK) < NFS4_SHARE_ACCESS_READ)
2290 return 0;
2291 if ((x & NFS4_SHARE_ACCESS_MASK) > NFS4_SHARE_ACCESS_BOTH)
2292 return 0;
2293 x &= ~NFS4_SHARE_ACCESS_MASK;
2294 if (minorversion && x) {
2295 if ((x & NFS4_SHARE_WANT_MASK) > NFS4_SHARE_WANT_CANCEL)
2296 return 0;
2297 if ((x & NFS4_SHARE_WHEN_MASK) > NFS4_SHARE_PUSH_DELEG_WHEN_UNCONTENDED)
2298 return 0;
2299 x &= ~(NFS4_SHARE_WANT_MASK | NFS4_SHARE_WHEN_MASK);
2301 if (x)
2302 return 0;
2303 return 1;
2306 static inline int deny_valid(u32 x)
2308 /* Note: unlike access bits, deny bits may be zero. */
2309 return x <= NFS4_SHARE_DENY_BOTH;
2313 * Called to check deny when READ with all zero stateid or
2314 * WRITE with all zero or all one stateid
2316 static __be32
2317 nfs4_share_conflict(struct svc_fh *current_fh, unsigned int deny_type)
2319 struct inode *ino = current_fh->fh_dentry->d_inode;
2320 struct nfs4_file *fp;
2321 struct nfs4_stateid *stp;
2322 __be32 ret;
2324 dprintk("NFSD: nfs4_share_conflict\n");
2326 fp = find_file(ino);
2327 if (!fp)
2328 return nfs_ok;
2329 ret = nfserr_locked;
2330 /* Search for conflicting share reservations */
2331 list_for_each_entry(stp, &fp->fi_stateids, st_perfile) {
2332 if (test_bit(deny_type, &stp->st_deny_bmap) ||
2333 test_bit(NFS4_SHARE_DENY_BOTH, &stp->st_deny_bmap))
2334 goto out;
2336 ret = nfs_ok;
2337 out:
2338 put_nfs4_file(fp);
2339 return ret;
2342 static void nfsd_break_one_deleg(struct nfs4_delegation *dp)
2344 /* We're assuming the state code never drops its reference
2345 * without first removing the lease. Since we're in this lease
2346 * callback (and since the lease code is serialized by the kernel
2347 * lock) we know the server hasn't removed the lease yet, we know
2348 * it's safe to take a reference: */
2349 atomic_inc(&dp->dl_count);
2351 list_add_tail(&dp->dl_recall_lru, &del_recall_lru);
2353 /* only place dl_time is set. protected by lock_flocks*/
2354 dp->dl_time = get_seconds();
2356 nfsd4_cb_recall(dp);
2359 /* Called from break_lease() with lock_flocks() held. */
2360 static void nfsd_break_deleg_cb(struct file_lock *fl)
2362 struct nfs4_file *fp = (struct nfs4_file *)fl->fl_owner;
2363 struct nfs4_delegation *dp;
2365 BUG_ON(!fp);
2366 /* We assume break_lease is only called once per lease: */
2367 BUG_ON(fp->fi_had_conflict);
2369 * We don't want the locks code to timeout the lease for us;
2370 * we'll remove it ourself if a delegation isn't returned
2371 * in time:
2373 fl->fl_break_time = 0;
2375 spin_lock(&recall_lock);
2376 fp->fi_had_conflict = true;
2377 list_for_each_entry(dp, &fp->fi_delegations, dl_perfile)
2378 nfsd_break_one_deleg(dp);
2379 spin_unlock(&recall_lock);
2382 static
2383 int nfsd_change_deleg_cb(struct file_lock **onlist, int arg)
2385 if (arg & F_UNLCK)
2386 return lease_modify(onlist, arg);
2387 else
2388 return -EAGAIN;
2391 static const struct lock_manager_operations nfsd_lease_mng_ops = {
2392 .fl_break = nfsd_break_deleg_cb,
2393 .fl_change = nfsd_change_deleg_cb,
2397 __be32
2398 nfsd4_process_open1(struct nfsd4_compound_state *cstate,
2399 struct nfsd4_open *open)
2401 clientid_t *clientid = &open->op_clientid;
2402 struct nfs4_client *clp = NULL;
2403 unsigned int strhashval;
2404 struct nfs4_stateowner *sop = NULL;
2406 if (!check_name(open->op_owner))
2407 return nfserr_inval;
2409 if (STALE_CLIENTID(&open->op_clientid))
2410 return nfserr_stale_clientid;
2412 strhashval = ownerstr_hashval(clientid->cl_id, open->op_owner);
2413 sop = find_openstateowner_str(strhashval, open);
2414 open->op_stateowner = sop;
2415 if (!sop) {
2416 /* Make sure the client's lease hasn't expired. */
2417 clp = find_confirmed_client(clientid);
2418 if (clp == NULL)
2419 return nfserr_expired;
2420 goto renew;
2422 /* When sessions are used, skip open sequenceid processing */
2423 if (nfsd4_has_session(cstate))
2424 goto renew;
2425 if (!sop->so_confirmed) {
2426 /* Replace unconfirmed owners without checking for replay. */
2427 clp = sop->so_client;
2428 release_openowner(sop);
2429 open->op_stateowner = NULL;
2430 goto renew;
2432 if (open->op_seqid == sop->so_seqid - 1) {
2433 if (sop->so_replay.rp_buflen)
2434 return nfserr_replay_me;
2435 /* The original OPEN failed so spectacularly
2436 * that we don't even have replay data saved!
2437 * Therefore, we have no choice but to continue
2438 * processing this OPEN; presumably, we'll
2439 * fail again for the same reason.
2441 dprintk("nfsd4_process_open1: replay with no replay cache\n");
2442 goto renew;
2444 if (open->op_seqid != sop->so_seqid)
2445 return nfserr_bad_seqid;
2446 renew:
2447 if (open->op_stateowner == NULL) {
2448 sop = alloc_init_open_stateowner(strhashval, clp, open);
2449 if (sop == NULL)
2450 return nfserr_jukebox;
2451 open->op_stateowner = sop;
2453 list_del_init(&sop->so_close_lru);
2454 renew_client(sop->so_client);
2455 return nfs_ok;
2458 static inline __be32
2459 nfs4_check_delegmode(struct nfs4_delegation *dp, int flags)
2461 if ((flags & WR_STATE) && (dp->dl_type == NFS4_OPEN_DELEGATE_READ))
2462 return nfserr_openmode;
2463 else
2464 return nfs_ok;
2467 static struct nfs4_delegation *
2468 find_delegation_file(struct nfs4_file *fp, stateid_t *stid)
2470 struct nfs4_delegation *dp;
2472 spin_lock(&recall_lock);
2473 list_for_each_entry(dp, &fp->fi_delegations, dl_perfile)
2474 if (dp->dl_stateid.si_stateownerid == stid->si_stateownerid) {
2475 spin_unlock(&recall_lock);
2476 return dp;
2478 spin_unlock(&recall_lock);
2479 return NULL;
2482 static int share_access_to_flags(u32 share_access)
2484 share_access &= ~NFS4_SHARE_WANT_MASK;
2486 return share_access == NFS4_SHARE_ACCESS_READ ? RD_STATE : WR_STATE;
2489 static __be32
2490 nfs4_check_deleg(struct nfs4_file *fp, struct nfsd4_open *open,
2491 struct nfs4_delegation **dp)
2493 int flags;
2494 __be32 status = nfserr_bad_stateid;
2496 *dp = find_delegation_file(fp, &open->op_delegate_stateid);
2497 if (*dp == NULL)
2498 goto out;
2499 flags = share_access_to_flags(open->op_share_access);
2500 status = nfs4_check_delegmode(*dp, flags);
2501 if (status)
2502 *dp = NULL;
2503 out:
2504 if (open->op_claim_type != NFS4_OPEN_CLAIM_DELEGATE_CUR)
2505 return nfs_ok;
2506 if (status)
2507 return status;
2508 open->op_stateowner->so_confirmed = 1;
2509 return nfs_ok;
2512 static __be32
2513 nfs4_check_open(struct nfs4_file *fp, struct nfsd4_open *open, struct nfs4_stateid **stpp)
2515 struct nfs4_stateid *local;
2516 __be32 status = nfserr_share_denied;
2517 struct nfs4_stateowner *sop = open->op_stateowner;
2519 list_for_each_entry(local, &fp->fi_stateids, st_perfile) {
2520 /* ignore lock owners */
2521 if (local->st_stateowner->so_is_open_owner == 0)
2522 continue;
2523 /* remember if we have seen this open owner */
2524 if (local->st_stateowner == sop)
2525 *stpp = local;
2526 /* check for conflicting share reservations */
2527 if (!test_share(local, open))
2528 goto out;
2530 status = 0;
2531 out:
2532 return status;
2535 static inline struct nfs4_stateid *
2536 nfs4_alloc_stateid(void)
2538 return kmem_cache_alloc(stateid_slab, GFP_KERNEL);
2541 static inline int nfs4_access_to_access(u32 nfs4_access)
2543 int flags = 0;
2545 if (nfs4_access & NFS4_SHARE_ACCESS_READ)
2546 flags |= NFSD_MAY_READ;
2547 if (nfs4_access & NFS4_SHARE_ACCESS_WRITE)
2548 flags |= NFSD_MAY_WRITE;
2549 return flags;
2552 static __be32 nfs4_get_vfs_file(struct svc_rqst *rqstp, struct nfs4_file *fp,
2553 struct svc_fh *cur_fh, struct nfsd4_open *open)
2555 __be32 status;
2556 int oflag = nfs4_access_to_omode(open->op_share_access);
2557 int access = nfs4_access_to_access(open->op_share_access);
2559 /* CLAIM_DELEGATE_CUR is used in response to a broken lease;
2560 * allowing it to break the lease and return EAGAIN leaves the
2561 * client unable to make progress in returning the delegation */
2562 if (open->op_claim_type == NFS4_OPEN_CLAIM_DELEGATE_CUR)
2563 access |= NFSD_MAY_NOT_BREAK_LEASE;
2565 if (!fp->fi_fds[oflag]) {
2566 status = nfsd_open(rqstp, cur_fh, S_IFREG, access,
2567 &fp->fi_fds[oflag]);
2568 if (status)
2569 return status;
2571 nfs4_file_get_access(fp, oflag);
2573 return nfs_ok;
2576 static __be32
2577 nfs4_new_open(struct svc_rqst *rqstp, struct nfs4_stateid **stpp,
2578 struct nfs4_file *fp, struct svc_fh *cur_fh,
2579 struct nfsd4_open *open)
2581 struct nfs4_stateid *stp;
2582 __be32 status;
2584 stp = nfs4_alloc_stateid();
2585 if (stp == NULL)
2586 return nfserr_jukebox;
2588 status = nfs4_get_vfs_file(rqstp, fp, cur_fh, open);
2589 if (status) {
2590 kmem_cache_free(stateid_slab, stp);
2591 return status;
2593 *stpp = stp;
2594 return 0;
2597 static inline __be32
2598 nfsd4_truncate(struct svc_rqst *rqstp, struct svc_fh *fh,
2599 struct nfsd4_open *open)
2601 struct iattr iattr = {
2602 .ia_valid = ATTR_SIZE,
2603 .ia_size = 0,
2605 if (!open->op_truncate)
2606 return 0;
2607 if (!(open->op_share_access & NFS4_SHARE_ACCESS_WRITE))
2608 return nfserr_inval;
2609 return nfsd_setattr(rqstp, fh, &iattr, 0, (time_t)0);
2612 static __be32
2613 nfs4_upgrade_open(struct svc_rqst *rqstp, struct nfs4_file *fp, struct svc_fh *cur_fh, struct nfs4_stateid *stp, struct nfsd4_open *open)
2615 u32 op_share_access = open->op_share_access & ~NFS4_SHARE_WANT_MASK;
2616 bool new_access;
2617 __be32 status;
2619 new_access = !test_bit(op_share_access, &stp->st_access_bmap);
2620 if (new_access) {
2621 status = nfs4_get_vfs_file(rqstp, fp, cur_fh, open);
2622 if (status)
2623 return status;
2625 status = nfsd4_truncate(rqstp, cur_fh, open);
2626 if (status) {
2627 if (new_access) {
2628 int oflag = nfs4_access_to_omode(op_share_access);
2629 nfs4_file_put_access(fp, oflag);
2631 return status;
2633 /* remember the open */
2634 __set_bit(op_share_access, &stp->st_access_bmap);
2635 __set_bit(open->op_share_deny, &stp->st_deny_bmap);
2637 return nfs_ok;
2641 static void
2642 nfs4_set_claim_prev(struct nfsd4_open *open)
2644 open->op_stateowner->so_confirmed = 1;
2645 open->op_stateowner->so_client->cl_firststate = 1;
2648 /* Should we give out recallable state?: */
2649 static bool nfsd4_cb_channel_good(struct nfs4_client *clp)
2651 if (clp->cl_cb_state == NFSD4_CB_UP)
2652 return true;
2654 * In the sessions case, since we don't have to establish a
2655 * separate connection for callbacks, we assume it's OK
2656 * until we hear otherwise:
2658 return clp->cl_minorversion && clp->cl_cb_state == NFSD4_CB_UNKNOWN;
2661 static struct file_lock *nfs4_alloc_init_lease(struct nfs4_delegation *dp, int flag)
2663 struct file_lock *fl;
2665 fl = locks_alloc_lock();
2666 if (!fl)
2667 return NULL;
2668 locks_init_lock(fl);
2669 fl->fl_lmops = &nfsd_lease_mng_ops;
2670 fl->fl_flags = FL_LEASE;
2671 fl->fl_type = flag == NFS4_OPEN_DELEGATE_READ? F_RDLCK: F_WRLCK;
2672 fl->fl_end = OFFSET_MAX;
2673 fl->fl_owner = (fl_owner_t)(dp->dl_file);
2674 fl->fl_pid = current->tgid;
2675 return fl;
2678 static int nfs4_setlease(struct nfs4_delegation *dp, int flag)
2680 struct nfs4_file *fp = dp->dl_file;
2681 struct file_lock *fl;
2682 int status;
2684 fl = nfs4_alloc_init_lease(dp, flag);
2685 if (!fl)
2686 return -ENOMEM;
2687 fl->fl_file = find_readable_file(fp);
2688 list_add(&dp->dl_perclnt, &dp->dl_client->cl_delegations);
2689 status = vfs_setlease(fl->fl_file, fl->fl_type, &fl);
2690 if (status) {
2691 list_del_init(&dp->dl_perclnt);
2692 locks_free_lock(fl);
2693 return -ENOMEM;
2695 fp->fi_lease = fl;
2696 fp->fi_deleg_file = fl->fl_file;
2697 get_file(fp->fi_deleg_file);
2698 atomic_set(&fp->fi_delegees, 1);
2699 list_add(&dp->dl_perfile, &fp->fi_delegations);
2700 return 0;
2703 static int nfs4_set_delegation(struct nfs4_delegation *dp, int flag)
2705 struct nfs4_file *fp = dp->dl_file;
2707 if (!fp->fi_lease)
2708 return nfs4_setlease(dp, flag);
2709 spin_lock(&recall_lock);
2710 if (fp->fi_had_conflict) {
2711 spin_unlock(&recall_lock);
2712 return -EAGAIN;
2714 atomic_inc(&fp->fi_delegees);
2715 list_add(&dp->dl_perfile, &fp->fi_delegations);
2716 spin_unlock(&recall_lock);
2717 list_add(&dp->dl_perclnt, &dp->dl_client->cl_delegations);
2718 return 0;
2722 * Attempt to hand out a delegation.
2724 static void
2725 nfs4_open_delegation(struct svc_fh *fh, struct nfsd4_open *open, struct nfs4_stateid *stp)
2727 struct nfs4_delegation *dp;
2728 struct nfs4_stateowner *sop = stp->st_stateowner;
2729 int cb_up;
2730 int status, flag = 0;
2732 cb_up = nfsd4_cb_channel_good(sop->so_client);
2733 flag = NFS4_OPEN_DELEGATE_NONE;
2734 open->op_recall = 0;
2735 switch (open->op_claim_type) {
2736 case NFS4_OPEN_CLAIM_PREVIOUS:
2737 if (!cb_up)
2738 open->op_recall = 1;
2739 flag = open->op_delegate_type;
2740 if (flag == NFS4_OPEN_DELEGATE_NONE)
2741 goto out;
2742 break;
2743 case NFS4_OPEN_CLAIM_NULL:
2744 /* Let's not give out any delegations till everyone's
2745 * had the chance to reclaim theirs.... */
2746 if (locks_in_grace())
2747 goto out;
2748 if (!cb_up || !sop->so_confirmed)
2749 goto out;
2750 if (open->op_share_access & NFS4_SHARE_ACCESS_WRITE)
2751 flag = NFS4_OPEN_DELEGATE_WRITE;
2752 else
2753 flag = NFS4_OPEN_DELEGATE_READ;
2754 break;
2755 default:
2756 goto out;
2759 dp = alloc_init_deleg(sop->so_client, stp, fh, flag);
2760 if (dp == NULL)
2761 goto out_no_deleg;
2762 status = nfs4_set_delegation(dp, flag);
2763 if (status)
2764 goto out_free;
2766 memcpy(&open->op_delegate_stateid, &dp->dl_stateid, sizeof(dp->dl_stateid));
2768 dprintk("NFSD: delegation stateid=" STATEID_FMT "\n",
2769 STATEID_VAL(&dp->dl_stateid));
2770 out:
2771 if (open->op_claim_type == NFS4_OPEN_CLAIM_PREVIOUS
2772 && flag == NFS4_OPEN_DELEGATE_NONE
2773 && open->op_delegate_type != NFS4_OPEN_DELEGATE_NONE)
2774 dprintk("NFSD: WARNING: refusing delegation reclaim\n");
2775 open->op_delegate_type = flag;
2776 return;
2777 out_free:
2778 nfs4_put_delegation(dp);
2779 out_no_deleg:
2780 flag = NFS4_OPEN_DELEGATE_NONE;
2781 goto out;
2785 * called with nfs4_lock_state() held.
2787 __be32
2788 nfsd4_process_open2(struct svc_rqst *rqstp, struct svc_fh *current_fh, struct nfsd4_open *open)
2790 struct nfsd4_compoundres *resp = rqstp->rq_resp;
2791 struct nfs4_file *fp = NULL;
2792 struct inode *ino = current_fh->fh_dentry->d_inode;
2793 struct nfs4_stateid *stp = NULL;
2794 struct nfs4_delegation *dp = NULL;
2795 __be32 status;
2797 status = nfserr_inval;
2798 if (!access_valid(open->op_share_access, resp->cstate.minorversion)
2799 || !deny_valid(open->op_share_deny))
2800 goto out;
2802 * Lookup file; if found, lookup stateid and check open request,
2803 * and check for delegations in the process of being recalled.
2804 * If not found, create the nfs4_file struct
2806 fp = find_file(ino);
2807 if (fp) {
2808 if ((status = nfs4_check_open(fp, open, &stp)))
2809 goto out;
2810 status = nfs4_check_deleg(fp, open, &dp);
2811 if (status)
2812 goto out;
2813 } else {
2814 status = nfserr_bad_stateid;
2815 if (open->op_claim_type == NFS4_OPEN_CLAIM_DELEGATE_CUR)
2816 goto out;
2817 status = nfserr_jukebox;
2818 fp = alloc_init_file(ino);
2819 if (fp == NULL)
2820 goto out;
2824 * OPEN the file, or upgrade an existing OPEN.
2825 * If truncate fails, the OPEN fails.
2827 if (stp) {
2828 /* Stateid was found, this is an OPEN upgrade */
2829 status = nfs4_upgrade_open(rqstp, fp, current_fh, stp, open);
2830 if (status)
2831 goto out;
2832 update_stateid(&stp->st_stateid);
2833 } else {
2834 status = nfs4_new_open(rqstp, &stp, fp, current_fh, open);
2835 if (status)
2836 goto out;
2837 init_stateid(stp, fp, open);
2838 status = nfsd4_truncate(rqstp, current_fh, open);
2839 if (status) {
2840 release_open_stateid(stp);
2841 goto out;
2843 if (nfsd4_has_session(&resp->cstate))
2844 update_stateid(&stp->st_stateid);
2846 memcpy(&open->op_stateid, &stp->st_stateid, sizeof(stateid_t));
2848 if (nfsd4_has_session(&resp->cstate))
2849 open->op_stateowner->so_confirmed = 1;
2852 * Attempt to hand out a delegation. No error return, because the
2853 * OPEN succeeds even if we fail.
2855 nfs4_open_delegation(current_fh, open, stp);
2857 status = nfs_ok;
2859 dprintk("%s: stateid=" STATEID_FMT "\n", __func__,
2860 STATEID_VAL(&stp->st_stateid));
2861 out:
2862 if (fp)
2863 put_nfs4_file(fp);
2864 if (status == 0 && open->op_claim_type == NFS4_OPEN_CLAIM_PREVIOUS)
2865 nfs4_set_claim_prev(open);
2867 * To finish the open response, we just need to set the rflags.
2869 open->op_rflags = NFS4_OPEN_RESULT_LOCKTYPE_POSIX;
2870 if (!open->op_stateowner->so_confirmed &&
2871 !nfsd4_has_session(&resp->cstate))
2872 open->op_rflags |= NFS4_OPEN_RESULT_CONFIRM;
2874 return status;
2877 __be32
2878 nfsd4_renew(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
2879 clientid_t *clid)
2881 struct nfs4_client *clp;
2882 __be32 status;
2884 nfs4_lock_state();
2885 dprintk("process_renew(%08x/%08x): starting\n",
2886 clid->cl_boot, clid->cl_id);
2887 status = nfserr_stale_clientid;
2888 if (STALE_CLIENTID(clid))
2889 goto out;
2890 clp = find_confirmed_client(clid);
2891 status = nfserr_expired;
2892 if (clp == NULL) {
2893 /* We assume the client took too long to RENEW. */
2894 dprintk("nfsd4_renew: clientid not found!\n");
2895 goto out;
2897 renew_client(clp);
2898 status = nfserr_cb_path_down;
2899 if (!list_empty(&clp->cl_delegations)
2900 && clp->cl_cb_state != NFSD4_CB_UP)
2901 goto out;
2902 status = nfs_ok;
2903 out:
2904 nfs4_unlock_state();
2905 return status;
2908 static struct lock_manager nfsd4_manager = {
2911 static void
2912 nfsd4_end_grace(void)
2914 dprintk("NFSD: end of grace period\n");
2915 nfsd4_recdir_purge_old();
2916 locks_end_grace(&nfsd4_manager);
2918 * Now that every NFSv4 client has had the chance to recover and
2919 * to see the (possibly new, possibly shorter) lease time, we
2920 * can safely set the next grace time to the current lease time:
2922 nfsd4_grace = nfsd4_lease;
2925 static time_t
2926 nfs4_laundromat(void)
2928 struct nfs4_client *clp;
2929 struct nfs4_stateowner *sop;
2930 struct nfs4_delegation *dp;
2931 struct list_head *pos, *next, reaplist;
2932 time_t cutoff = get_seconds() - nfsd4_lease;
2933 time_t t, clientid_val = nfsd4_lease;
2934 time_t u, test_val = nfsd4_lease;
2936 nfs4_lock_state();
2938 dprintk("NFSD: laundromat service - starting\n");
2939 if (locks_in_grace())
2940 nfsd4_end_grace();
2941 INIT_LIST_HEAD(&reaplist);
2942 spin_lock(&client_lock);
2943 list_for_each_safe(pos, next, &client_lru) {
2944 clp = list_entry(pos, struct nfs4_client, cl_lru);
2945 if (time_after((unsigned long)clp->cl_time, (unsigned long)cutoff)) {
2946 t = clp->cl_time - cutoff;
2947 if (clientid_val > t)
2948 clientid_val = t;
2949 break;
2951 if (atomic_read(&clp->cl_refcount)) {
2952 dprintk("NFSD: client in use (clientid %08x)\n",
2953 clp->cl_clientid.cl_id);
2954 continue;
2956 unhash_client_locked(clp);
2957 list_add(&clp->cl_lru, &reaplist);
2959 spin_unlock(&client_lock);
2960 list_for_each_safe(pos, next, &reaplist) {
2961 clp = list_entry(pos, struct nfs4_client, cl_lru);
2962 dprintk("NFSD: purging unused client (clientid %08x)\n",
2963 clp->cl_clientid.cl_id);
2964 nfsd4_remove_clid_dir(clp);
2965 expire_client(clp);
2967 spin_lock(&recall_lock);
2968 list_for_each_safe(pos, next, &del_recall_lru) {
2969 dp = list_entry (pos, struct nfs4_delegation, dl_recall_lru);
2970 if (time_after((unsigned long)dp->dl_time, (unsigned long)cutoff)) {
2971 u = dp->dl_time - cutoff;
2972 if (test_val > u)
2973 test_val = u;
2974 break;
2976 list_move(&dp->dl_recall_lru, &reaplist);
2978 spin_unlock(&recall_lock);
2979 list_for_each_safe(pos, next, &reaplist) {
2980 dp = list_entry (pos, struct nfs4_delegation, dl_recall_lru);
2981 list_del_init(&dp->dl_recall_lru);
2982 unhash_delegation(dp);
2984 test_val = nfsd4_lease;
2985 list_for_each_safe(pos, next, &close_lru) {
2986 sop = list_entry(pos, struct nfs4_stateowner, so_close_lru);
2987 if (time_after((unsigned long)sop->so_time, (unsigned long)cutoff)) {
2988 u = sop->so_time - cutoff;
2989 if (test_val > u)
2990 test_val = u;
2991 break;
2993 dprintk("NFSD: purging unused open stateowner (so_id %d)\n",
2994 sop->so_id);
2995 release_openowner(sop);
2997 if (clientid_val < NFSD_LAUNDROMAT_MINTIMEOUT)
2998 clientid_val = NFSD_LAUNDROMAT_MINTIMEOUT;
2999 nfs4_unlock_state();
3000 return clientid_val;
3003 static struct workqueue_struct *laundry_wq;
3004 static void laundromat_main(struct work_struct *);
3005 static DECLARE_DELAYED_WORK(laundromat_work, laundromat_main);
3007 static void
3008 laundromat_main(struct work_struct *not_used)
3010 time_t t;
3012 t = nfs4_laundromat();
3013 dprintk("NFSD: laundromat_main - sleeping for %ld seconds\n", t);
3014 queue_delayed_work(laundry_wq, &laundromat_work, t*HZ);
3017 static struct nfs4_stateowner *
3018 search_close_lru(u32 st_id, int flags)
3020 struct nfs4_stateowner *local = NULL;
3022 if (flags & CLOSE_STATE) {
3023 list_for_each_entry(local, &close_lru, so_close_lru) {
3024 if (local->so_id == st_id)
3025 return local;
3028 return NULL;
3031 static inline int
3032 nfs4_check_fh(struct svc_fh *fhp, struct nfs4_stateid *stp)
3034 return fhp->fh_dentry->d_inode != stp->st_file->fi_inode;
3037 static int
3038 STALE_STATEID(stateid_t *stateid)
3040 if (stateid->si_boot == boot_time)
3041 return 0;
3042 dprintk("NFSD: stale stateid " STATEID_FMT "!\n",
3043 STATEID_VAL(stateid));
3044 return 1;
3047 static inline int
3048 access_permit_read(unsigned long access_bmap)
3050 return test_bit(NFS4_SHARE_ACCESS_READ, &access_bmap) ||
3051 test_bit(NFS4_SHARE_ACCESS_BOTH, &access_bmap) ||
3052 test_bit(NFS4_SHARE_ACCESS_WRITE, &access_bmap);
3055 static inline int
3056 access_permit_write(unsigned long access_bmap)
3058 return test_bit(NFS4_SHARE_ACCESS_WRITE, &access_bmap) ||
3059 test_bit(NFS4_SHARE_ACCESS_BOTH, &access_bmap);
3062 static
3063 __be32 nfs4_check_openmode(struct nfs4_stateid *stp, int flags)
3065 __be32 status = nfserr_openmode;
3067 /* For lock stateid's, we test the parent open, not the lock: */
3068 if (stp->st_openstp)
3069 stp = stp->st_openstp;
3070 if ((flags & WR_STATE) && (!access_permit_write(stp->st_access_bmap)))
3071 goto out;
3072 if ((flags & RD_STATE) && (!access_permit_read(stp->st_access_bmap)))
3073 goto out;
3074 status = nfs_ok;
3075 out:
3076 return status;
3079 static inline __be32
3080 check_special_stateids(svc_fh *current_fh, stateid_t *stateid, int flags)
3082 if (ONE_STATEID(stateid) && (flags & RD_STATE))
3083 return nfs_ok;
3084 else if (locks_in_grace()) {
3085 /* Answer in remaining cases depends on existence of
3086 * conflicting state; so we must wait out the grace period. */
3087 return nfserr_grace;
3088 } else if (flags & WR_STATE)
3089 return nfs4_share_conflict(current_fh,
3090 NFS4_SHARE_DENY_WRITE);
3091 else /* (flags & RD_STATE) && ZERO_STATEID(stateid) */
3092 return nfs4_share_conflict(current_fh,
3093 NFS4_SHARE_DENY_READ);
3097 * Allow READ/WRITE during grace period on recovered state only for files
3098 * that are not able to provide mandatory locking.
3100 static inline int
3101 grace_disallows_io(struct inode *inode)
3103 return locks_in_grace() && mandatory_lock(inode);
3106 static int check_stateid_generation(stateid_t *in, stateid_t *ref, int flags)
3109 * When sessions are used the stateid generation number is ignored
3110 * when it is zero.
3112 if ((flags & HAS_SESSION) && in->si_generation == 0)
3113 goto out;
3115 /* If the client sends us a stateid from the future, it's buggy: */
3116 if (in->si_generation > ref->si_generation)
3117 return nfserr_bad_stateid;
3119 * The following, however, can happen. For example, if the
3120 * client sends an open and some IO at the same time, the open
3121 * may bump si_generation while the IO is still in flight.
3122 * Thanks to hard links and renames, the client never knows what
3123 * file an open will affect. So it could avoid that situation
3124 * only by serializing all opens and IO from the same open
3125 * owner. To recover from the old_stateid error, the client
3126 * will just have to retry the IO:
3128 if (in->si_generation < ref->si_generation)
3129 return nfserr_old_stateid;
3130 out:
3131 return nfs_ok;
3134 static int is_delegation_stateid(stateid_t *stateid)
3136 return stateid->si_fileid == 0;
3140 * Checks for stateid operations
3142 __be32
3143 nfs4_preprocess_stateid_op(struct nfsd4_compound_state *cstate,
3144 stateid_t *stateid, int flags, struct file **filpp)
3146 struct nfs4_stateid *stp = NULL;
3147 struct nfs4_delegation *dp = NULL;
3148 struct svc_fh *current_fh = &cstate->current_fh;
3149 struct inode *ino = current_fh->fh_dentry->d_inode;
3150 __be32 status;
3152 if (filpp)
3153 *filpp = NULL;
3155 if (grace_disallows_io(ino))
3156 return nfserr_grace;
3158 if (nfsd4_has_session(cstate))
3159 flags |= HAS_SESSION;
3161 if (ZERO_STATEID(stateid) || ONE_STATEID(stateid))
3162 return check_special_stateids(current_fh, stateid, flags);
3164 status = nfserr_stale_stateid;
3165 if (STALE_STATEID(stateid))
3166 goto out;
3169 * We assume that any stateid that has the current boot time,
3170 * but that we can't find, is expired:
3172 status = nfserr_expired;
3173 if (is_delegation_stateid(stateid)) {
3174 dp = find_delegation_stateid(ino, stateid);
3175 if (!dp)
3176 goto out;
3177 status = check_stateid_generation(stateid, &dp->dl_stateid,
3178 flags);
3179 if (status)
3180 goto out;
3181 status = nfs4_check_delegmode(dp, flags);
3182 if (status)
3183 goto out;
3184 renew_client(dp->dl_client);
3185 if (filpp) {
3186 *filpp = dp->dl_file->fi_deleg_file;
3187 BUG_ON(!*filpp);
3189 } else { /* open or lock stateid */
3190 stp = find_stateid(stateid, flags);
3191 if (!stp)
3192 goto out;
3193 status = nfserr_bad_stateid;
3194 if (nfs4_check_fh(current_fh, stp))
3195 goto out;
3196 if (!stp->st_stateowner->so_confirmed)
3197 goto out;
3198 status = check_stateid_generation(stateid, &stp->st_stateid,
3199 flags);
3200 if (status)
3201 goto out;
3202 status = nfs4_check_openmode(stp, flags);
3203 if (status)
3204 goto out;
3205 renew_client(stp->st_stateowner->so_client);
3206 if (filpp) {
3207 if (flags & RD_STATE)
3208 *filpp = find_readable_file(stp->st_file);
3209 else
3210 *filpp = find_writeable_file(stp->st_file);
3213 status = nfs_ok;
3214 out:
3215 return status;
3218 static inline int
3219 setlkflg (int type)
3221 return (type == NFS4_READW_LT || type == NFS4_READ_LT) ?
3222 RD_STATE : WR_STATE;
3226 * Checks for sequence id mutating operations.
3228 static __be32
3229 nfs4_preprocess_seqid_op(struct nfsd4_compound_state *cstate, u32 seqid,
3230 stateid_t *stateid, int flags,
3231 struct nfs4_stateowner **sopp,
3232 struct nfs4_stateid **stpp, struct nfsd4_lock *lock)
3234 struct nfs4_stateid *stp;
3235 struct nfs4_stateowner *sop;
3236 struct svc_fh *current_fh = &cstate->current_fh;
3237 __be32 status;
3239 dprintk("NFSD: %s: seqid=%d stateid = " STATEID_FMT "\n", __func__,
3240 seqid, STATEID_VAL(stateid));
3242 *stpp = NULL;
3243 *sopp = NULL;
3245 if (ZERO_STATEID(stateid) || ONE_STATEID(stateid)) {
3246 dprintk("NFSD: preprocess_seqid_op: magic stateid!\n");
3247 return nfserr_bad_stateid;
3250 if (STALE_STATEID(stateid))
3251 return nfserr_stale_stateid;
3253 if (nfsd4_has_session(cstate))
3254 flags |= HAS_SESSION;
3257 * We return BAD_STATEID if filehandle doesn't match stateid,
3258 * the confirmed flag is incorrecly set, or the generation
3259 * number is incorrect.
3261 stp = find_stateid(stateid, flags);
3262 if (stp == NULL) {
3264 * Also, we should make sure this isn't just the result of
3265 * a replayed close:
3267 sop = search_close_lru(stateid->si_stateownerid, flags);
3268 /* It's not stale; let's assume it's expired: */
3269 if (sop == NULL)
3270 return nfserr_expired;
3271 *sopp = sop;
3272 goto check_replay;
3275 *stpp = stp;
3276 *sopp = sop = stp->st_stateowner;
3278 if (lock) {
3279 clientid_t *lockclid = &lock->v.new.clientid;
3280 struct nfs4_client *clp = sop->so_client;
3281 int lkflg = 0;
3282 __be32 status;
3284 lkflg = setlkflg(lock->lk_type);
3286 if (lock->lk_is_new) {
3287 if (!sop->so_is_open_owner)
3288 return nfserr_bad_stateid;
3289 if (!(flags & HAS_SESSION) &&
3290 !same_clid(&clp->cl_clientid, lockclid))
3291 return nfserr_bad_stateid;
3292 /* stp is the open stateid */
3293 status = nfs4_check_openmode(stp, lkflg);
3294 if (status)
3295 return status;
3296 } else {
3297 /* stp is the lock stateid */
3298 status = nfs4_check_openmode(stp->st_openstp, lkflg);
3299 if (status)
3300 return status;
3304 if (nfs4_check_fh(current_fh, stp)) {
3305 dprintk("NFSD: preprocess_seqid_op: fh-stateid mismatch!\n");
3306 return nfserr_bad_stateid;
3310 * We now validate the seqid and stateid generation numbers.
3311 * For the moment, we ignore the possibility of
3312 * generation number wraparound.
3314 if (!(flags & HAS_SESSION) && seqid != sop->so_seqid)
3315 goto check_replay;
3317 if (sop->so_confirmed && flags & CONFIRM) {
3318 dprintk("NFSD: preprocess_seqid_op: expected"
3319 " unconfirmed stateowner!\n");
3320 return nfserr_bad_stateid;
3322 if (!sop->so_confirmed && !(flags & CONFIRM)) {
3323 dprintk("NFSD: preprocess_seqid_op: stateowner not"
3324 " confirmed yet!\n");
3325 return nfserr_bad_stateid;
3327 status = check_stateid_generation(stateid, &stp->st_stateid, flags);
3328 if (status)
3329 return status;
3330 renew_client(sop->so_client);
3331 return nfs_ok;
3333 check_replay:
3334 if (seqid == sop->so_seqid - 1) {
3335 dprintk("NFSD: preprocess_seqid_op: retransmission?\n");
3336 /* indicate replay to calling function */
3337 return nfserr_replay_me;
3339 dprintk("NFSD: preprocess_seqid_op: bad seqid (expected %d, got %d)\n",
3340 sop->so_seqid, seqid);
3341 *sopp = NULL;
3342 return nfserr_bad_seqid;
3345 __be32
3346 nfsd4_open_confirm(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
3347 struct nfsd4_open_confirm *oc)
3349 __be32 status;
3350 struct nfs4_stateowner *sop;
3351 struct nfs4_stateid *stp;
3353 dprintk("NFSD: nfsd4_open_confirm on file %.*s\n",
3354 (int)cstate->current_fh.fh_dentry->d_name.len,
3355 cstate->current_fh.fh_dentry->d_name.name);
3357 status = fh_verify(rqstp, &cstate->current_fh, S_IFREG, 0);
3358 if (status)
3359 return status;
3361 nfs4_lock_state();
3363 if ((status = nfs4_preprocess_seqid_op(cstate,
3364 oc->oc_seqid, &oc->oc_req_stateid,
3365 CONFIRM | OPEN_STATE,
3366 &oc->oc_stateowner, &stp, NULL)))
3367 goto out;
3369 sop = oc->oc_stateowner;
3370 sop->so_confirmed = 1;
3371 update_stateid(&stp->st_stateid);
3372 memcpy(&oc->oc_resp_stateid, &stp->st_stateid, sizeof(stateid_t));
3373 dprintk("NFSD: %s: success, seqid=%d stateid=" STATEID_FMT "\n",
3374 __func__, oc->oc_seqid, STATEID_VAL(&stp->st_stateid));
3376 nfsd4_create_clid_dir(sop->so_client);
3377 out:
3378 if (oc->oc_stateowner) {
3379 nfs4_get_stateowner(oc->oc_stateowner);
3380 cstate->replay_owner = oc->oc_stateowner;
3382 nfs4_unlock_state();
3383 return status;
3386 static inline void nfs4_file_downgrade(struct nfs4_stateid *stp, unsigned int to_access)
3388 int i;
3390 for (i = 1; i < 4; i++) {
3391 if (test_bit(i, &stp->st_access_bmap)
3392 && ((i & to_access) != i)) {
3393 nfs4_file_put_access(stp->st_file, nfs4_access_to_omode(i));
3394 __clear_bit(i, &stp->st_access_bmap);
3399 static void
3400 reset_union_bmap_deny(unsigned long deny, unsigned long *bmap)
3402 int i;
3403 for (i = 0; i < 4; i++) {
3404 if ((i & deny) != i)
3405 __clear_bit(i, bmap);
3409 __be32
3410 nfsd4_open_downgrade(struct svc_rqst *rqstp,
3411 struct nfsd4_compound_state *cstate,
3412 struct nfsd4_open_downgrade *od)
3414 __be32 status;
3415 struct nfs4_stateid *stp;
3417 dprintk("NFSD: nfsd4_open_downgrade on file %.*s\n",
3418 (int)cstate->current_fh.fh_dentry->d_name.len,
3419 cstate->current_fh.fh_dentry->d_name.name);
3421 if (!access_valid(od->od_share_access, cstate->minorversion)
3422 || !deny_valid(od->od_share_deny))
3423 return nfserr_inval;
3424 /* We don't yet support WANT bits: */
3425 od->od_share_access &= NFS4_SHARE_ACCESS_MASK;
3427 nfs4_lock_state();
3428 if ((status = nfs4_preprocess_seqid_op(cstate,
3429 od->od_seqid,
3430 &od->od_stateid,
3431 OPEN_STATE,
3432 &od->od_stateowner, &stp, NULL)))
3433 goto out;
3435 status = nfserr_inval;
3436 if (!test_bit(od->od_share_access, &stp->st_access_bmap)) {
3437 dprintk("NFSD:access not a subset current bitmap: 0x%lx, input access=%08x\n",
3438 stp->st_access_bmap, od->od_share_access);
3439 goto out;
3441 if (!test_bit(od->od_share_deny, &stp->st_deny_bmap)) {
3442 dprintk("NFSD:deny not a subset current bitmap: 0x%lx, input deny=%08x\n",
3443 stp->st_deny_bmap, od->od_share_deny);
3444 goto out;
3446 nfs4_file_downgrade(stp, od->od_share_access);
3448 reset_union_bmap_deny(od->od_share_deny, &stp->st_deny_bmap);
3450 update_stateid(&stp->st_stateid);
3451 memcpy(&od->od_stateid, &stp->st_stateid, sizeof(stateid_t));
3452 status = nfs_ok;
3453 out:
3454 if (od->od_stateowner) {
3455 nfs4_get_stateowner(od->od_stateowner);
3456 cstate->replay_owner = od->od_stateowner;
3458 nfs4_unlock_state();
3459 return status;
3463 * nfs4_unlock_state() called after encode
3465 __be32
3466 nfsd4_close(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
3467 struct nfsd4_close *close)
3469 __be32 status;
3470 struct nfs4_stateid *stp;
3472 dprintk("NFSD: nfsd4_close on file %.*s\n",
3473 (int)cstate->current_fh.fh_dentry->d_name.len,
3474 cstate->current_fh.fh_dentry->d_name.name);
3476 nfs4_lock_state();
3477 /* check close_lru for replay */
3478 if ((status = nfs4_preprocess_seqid_op(cstate,
3479 close->cl_seqid,
3480 &close->cl_stateid,
3481 OPEN_STATE | CLOSE_STATE,
3482 &close->cl_stateowner, &stp, NULL)))
3483 goto out;
3484 status = nfs_ok;
3485 update_stateid(&stp->st_stateid);
3486 memcpy(&close->cl_stateid, &stp->st_stateid, sizeof(stateid_t));
3488 /* release_stateid() calls nfsd_close() if needed */
3489 release_open_stateid(stp);
3491 /* place unused nfs4_stateowners on so_close_lru list to be
3492 * released by the laundromat service after the lease period
3493 * to enable us to handle CLOSE replay
3495 if (list_empty(&close->cl_stateowner->so_stateids))
3496 move_to_close_lru(close->cl_stateowner);
3497 out:
3498 if (close->cl_stateowner) {
3499 nfs4_get_stateowner(close->cl_stateowner);
3500 cstate->replay_owner = close->cl_stateowner;
3502 nfs4_unlock_state();
3503 return status;
3506 __be32
3507 nfsd4_delegreturn(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
3508 struct nfsd4_delegreturn *dr)
3510 struct nfs4_delegation *dp;
3511 stateid_t *stateid = &dr->dr_stateid;
3512 struct inode *inode;
3513 __be32 status;
3514 int flags = 0;
3516 if ((status = fh_verify(rqstp, &cstate->current_fh, S_IFREG, 0)))
3517 return status;
3518 inode = cstate->current_fh.fh_dentry->d_inode;
3520 if (nfsd4_has_session(cstate))
3521 flags |= HAS_SESSION;
3522 nfs4_lock_state();
3523 status = nfserr_bad_stateid;
3524 if (ZERO_STATEID(stateid) || ONE_STATEID(stateid))
3525 goto out;
3526 status = nfserr_stale_stateid;
3527 if (STALE_STATEID(stateid))
3528 goto out;
3529 status = nfserr_bad_stateid;
3530 if (!is_delegation_stateid(stateid))
3531 goto out;
3532 status = nfserr_expired;
3533 dp = find_delegation_stateid(inode, stateid);
3534 if (!dp)
3535 goto out;
3536 status = check_stateid_generation(stateid, &dp->dl_stateid, flags);
3537 if (status)
3538 goto out;
3539 renew_client(dp->dl_client);
3541 unhash_delegation(dp);
3542 out:
3543 nfs4_unlock_state();
3545 return status;
3550 * Lock owner state (byte-range locks)
3552 #define LOFF_OVERFLOW(start, len) ((u64)(len) > ~(u64)(start))
3553 #define LOCK_HASH_BITS 8
3554 #define LOCK_HASH_SIZE (1 << LOCK_HASH_BITS)
3555 #define LOCK_HASH_MASK (LOCK_HASH_SIZE - 1)
3557 static inline u64
3558 end_offset(u64 start, u64 len)
3560 u64 end;
3562 end = start + len;
3563 return end >= start ? end: NFS4_MAX_UINT64;
3566 /* last octet in a range */
3567 static inline u64
3568 last_byte_offset(u64 start, u64 len)
3570 u64 end;
3572 BUG_ON(!len);
3573 end = start + len;
3574 return end > start ? end - 1: NFS4_MAX_UINT64;
3577 #define lockownerid_hashval(id) \
3578 ((id) & LOCK_HASH_MASK)
3580 static inline unsigned int
3581 lock_ownerstr_hashval(struct inode *inode, u32 cl_id,
3582 struct xdr_netobj *ownername)
3584 return (file_hashval(inode) + cl_id
3585 + opaque_hashval(ownername->data, ownername->len))
3586 & LOCK_HASH_MASK;
3589 static struct list_head lock_ownerid_hashtbl[LOCK_HASH_SIZE];
3590 static struct list_head lock_ownerstr_hashtbl[LOCK_HASH_SIZE];
3591 static struct list_head lockstateid_hashtbl[STATEID_HASH_SIZE];
3593 static struct nfs4_stateid *
3594 find_stateid(stateid_t *stid, int flags)
3596 struct nfs4_stateid *local;
3597 u32 st_id = stid->si_stateownerid;
3598 u32 f_id = stid->si_fileid;
3599 unsigned int hashval;
3601 dprintk("NFSD: find_stateid flags 0x%x\n",flags);
3602 if (flags & (LOCK_STATE | RD_STATE | WR_STATE)) {
3603 hashval = stateid_hashval(st_id, f_id);
3604 list_for_each_entry(local, &lockstateid_hashtbl[hashval], st_hash) {
3605 if ((local->st_stateid.si_stateownerid == st_id) &&
3606 (local->st_stateid.si_fileid == f_id))
3607 return local;
3611 if (flags & (OPEN_STATE | RD_STATE | WR_STATE)) {
3612 hashval = stateid_hashval(st_id, f_id);
3613 list_for_each_entry(local, &stateid_hashtbl[hashval], st_hash) {
3614 if ((local->st_stateid.si_stateownerid == st_id) &&
3615 (local->st_stateid.si_fileid == f_id))
3616 return local;
3619 return NULL;
3622 static struct nfs4_delegation *
3623 find_delegation_stateid(struct inode *ino, stateid_t *stid)
3625 struct nfs4_file *fp;
3626 struct nfs4_delegation *dl;
3628 dprintk("NFSD: %s: stateid=" STATEID_FMT "\n", __func__,
3629 STATEID_VAL(stid));
3631 fp = find_file(ino);
3632 if (!fp)
3633 return NULL;
3634 dl = find_delegation_file(fp, stid);
3635 put_nfs4_file(fp);
3636 return dl;
3640 * TODO: Linux file offsets are _signed_ 64-bit quantities, which means that
3641 * we can't properly handle lock requests that go beyond the (2^63 - 1)-th
3642 * byte, because of sign extension problems. Since NFSv4 calls for 64-bit
3643 * locking, this prevents us from being completely protocol-compliant. The
3644 * real solution to this problem is to start using unsigned file offsets in
3645 * the VFS, but this is a very deep change!
3647 static inline void
3648 nfs4_transform_lock_offset(struct file_lock *lock)
3650 if (lock->fl_start < 0)
3651 lock->fl_start = OFFSET_MAX;
3652 if (lock->fl_end < 0)
3653 lock->fl_end = OFFSET_MAX;
3656 /* Hack!: For now, we're defining this just so we can use a pointer to it
3657 * as a unique cookie to identify our (NFSv4's) posix locks. */
3658 static const struct lock_manager_operations nfsd_posix_mng_ops = {
3661 static inline void
3662 nfs4_set_lock_denied(struct file_lock *fl, struct nfsd4_lock_denied *deny)
3664 struct nfs4_stateowner *sop;
3666 if (fl->fl_lmops == &nfsd_posix_mng_ops) {
3667 sop = (struct nfs4_stateowner *) fl->fl_owner;
3668 kref_get(&sop->so_ref);
3669 deny->ld_sop = sop;
3670 deny->ld_clientid = sop->so_client->cl_clientid;
3671 } else {
3672 deny->ld_sop = NULL;
3673 deny->ld_clientid.cl_boot = 0;
3674 deny->ld_clientid.cl_id = 0;
3676 deny->ld_start = fl->fl_start;
3677 deny->ld_length = NFS4_MAX_UINT64;
3678 if (fl->fl_end != NFS4_MAX_UINT64)
3679 deny->ld_length = fl->fl_end - fl->fl_start + 1;
3680 deny->ld_type = NFS4_READ_LT;
3681 if (fl->fl_type != F_RDLCK)
3682 deny->ld_type = NFS4_WRITE_LT;
3685 static struct nfs4_stateowner *
3686 find_lockstateowner_str(struct inode *inode, clientid_t *clid,
3687 struct xdr_netobj *owner)
3689 unsigned int hashval = lock_ownerstr_hashval(inode, clid->cl_id, owner);
3690 struct nfs4_stateowner *op;
3692 list_for_each_entry(op, &lock_ownerstr_hashtbl[hashval], so_strhash) {
3693 if (same_owner_str(op, owner, clid))
3694 return op;
3696 return NULL;
3700 * Alloc a lock owner structure.
3701 * Called in nfsd4_lock - therefore, OPEN and OPEN_CONFIRM (if needed) has
3702 * occurred.
3704 * strhashval = lock_ownerstr_hashval
3707 static struct nfs4_stateowner *
3708 alloc_init_lock_stateowner(unsigned int strhashval, struct nfs4_client *clp, struct nfs4_stateid *open_stp, struct nfsd4_lock *lock) {
3709 struct nfs4_stateowner *sop;
3710 struct nfs4_replay *rp;
3711 unsigned int idhashval;
3713 if (!(sop = alloc_stateowner(&lock->lk_new_owner)))
3714 return NULL;
3715 idhashval = lockownerid_hashval(current_ownerid);
3716 INIT_LIST_HEAD(&sop->so_idhash);
3717 INIT_LIST_HEAD(&sop->so_strhash);
3718 INIT_LIST_HEAD(&sop->so_perclient);
3719 INIT_LIST_HEAD(&sop->so_stateids);
3720 INIT_LIST_HEAD(&sop->so_perstateid);
3721 INIT_LIST_HEAD(&sop->so_close_lru); /* not used */
3722 sop->so_time = 0;
3723 list_add(&sop->so_idhash, &lock_ownerid_hashtbl[idhashval]);
3724 list_add(&sop->so_strhash, &lock_ownerstr_hashtbl[strhashval]);
3725 list_add(&sop->so_perstateid, &open_stp->st_lockowners);
3726 sop->so_is_open_owner = 0;
3727 sop->so_id = current_ownerid++;
3728 sop->so_client = clp;
3729 /* It is the openowner seqid that will be incremented in encode in the
3730 * case of new lockowners; so increment the lock seqid manually: */
3731 sop->so_seqid = lock->lk_new_lock_seqid + 1;
3732 sop->so_confirmed = 1;
3733 rp = &sop->so_replay;
3734 rp->rp_status = nfserr_serverfault;
3735 rp->rp_buflen = 0;
3736 rp->rp_buf = rp->rp_ibuf;
3737 return sop;
3740 static struct nfs4_stateid *
3741 alloc_init_lock_stateid(struct nfs4_stateowner *sop, struct nfs4_file *fp, struct nfs4_stateid *open_stp)
3743 struct nfs4_stateid *stp;
3744 unsigned int hashval = stateid_hashval(sop->so_id, fp->fi_id);
3746 stp = nfs4_alloc_stateid();
3747 if (stp == NULL)
3748 goto out;
3749 INIT_LIST_HEAD(&stp->st_hash);
3750 INIT_LIST_HEAD(&stp->st_perfile);
3751 INIT_LIST_HEAD(&stp->st_perstateowner);
3752 INIT_LIST_HEAD(&stp->st_lockowners); /* not used */
3753 list_add(&stp->st_hash, &lockstateid_hashtbl[hashval]);
3754 list_add(&stp->st_perfile, &fp->fi_stateids);
3755 list_add(&stp->st_perstateowner, &sop->so_stateids);
3756 stp->st_stateowner = sop;
3757 get_nfs4_file(fp);
3758 stp->st_file = fp;
3759 stp->st_stateid.si_boot = boot_time;
3760 stp->st_stateid.si_stateownerid = sop->so_id;
3761 stp->st_stateid.si_fileid = fp->fi_id;
3762 stp->st_stateid.si_generation = 0;
3763 stp->st_access_bmap = 0;
3764 stp->st_deny_bmap = open_stp->st_deny_bmap;
3765 stp->st_openstp = open_stp;
3767 out:
3768 return stp;
3771 static int
3772 check_lock_length(u64 offset, u64 length)
3774 return ((length == 0) || ((length != NFS4_MAX_UINT64) &&
3775 LOFF_OVERFLOW(offset, length)));
3778 static void get_lock_access(struct nfs4_stateid *lock_stp, u32 access)
3780 struct nfs4_file *fp = lock_stp->st_file;
3781 int oflag = nfs4_access_to_omode(access);
3783 if (test_bit(access, &lock_stp->st_access_bmap))
3784 return;
3785 nfs4_file_get_access(fp, oflag);
3786 __set_bit(access, &lock_stp->st_access_bmap);
3790 * LOCK operation
3792 __be32
3793 nfsd4_lock(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
3794 struct nfsd4_lock *lock)
3796 struct nfs4_stateowner *open_sop = NULL;
3797 struct nfs4_stateowner *lock_sop = NULL;
3798 struct nfs4_stateid *lock_stp;
3799 struct nfs4_file *fp;
3800 struct file *filp = NULL;
3801 struct file_lock file_lock;
3802 struct file_lock conflock;
3803 __be32 status = 0;
3804 unsigned int strhashval;
3805 int err;
3807 dprintk("NFSD: nfsd4_lock: start=%Ld length=%Ld\n",
3808 (long long) lock->lk_offset,
3809 (long long) lock->lk_length);
3811 if (check_lock_length(lock->lk_offset, lock->lk_length))
3812 return nfserr_inval;
3814 if ((status = fh_verify(rqstp, &cstate->current_fh,
3815 S_IFREG, NFSD_MAY_LOCK))) {
3816 dprintk("NFSD: nfsd4_lock: permission denied!\n");
3817 return status;
3820 nfs4_lock_state();
3822 if (lock->lk_is_new) {
3824 * Client indicates that this is a new lockowner.
3825 * Use open owner and open stateid to create lock owner and
3826 * lock stateid.
3828 struct nfs4_stateid *open_stp = NULL;
3830 status = nfserr_stale_clientid;
3831 if (!nfsd4_has_session(cstate) &&
3832 STALE_CLIENTID(&lock->lk_new_clientid))
3833 goto out;
3835 /* validate and update open stateid and open seqid */
3836 status = nfs4_preprocess_seqid_op(cstate,
3837 lock->lk_new_open_seqid,
3838 &lock->lk_new_open_stateid,
3839 OPEN_STATE,
3840 &lock->lk_replay_owner, &open_stp,
3841 lock);
3842 if (status)
3843 goto out;
3844 open_sop = lock->lk_replay_owner;
3845 /* create lockowner and lock stateid */
3846 fp = open_stp->st_file;
3847 strhashval = lock_ownerstr_hashval(fp->fi_inode,
3848 open_sop->so_client->cl_clientid.cl_id,
3849 &lock->v.new.owner);
3850 /* XXX: Do we need to check for duplicate stateowners on
3851 * the same file, or should they just be allowed (and
3852 * create new stateids)? */
3853 status = nfserr_jukebox;
3854 lock_sop = alloc_init_lock_stateowner(strhashval,
3855 open_sop->so_client, open_stp, lock);
3856 if (lock_sop == NULL)
3857 goto out;
3858 lock_stp = alloc_init_lock_stateid(lock_sop, fp, open_stp);
3859 if (lock_stp == NULL)
3860 goto out;
3861 } else {
3862 /* lock (lock owner + lock stateid) already exists */
3863 status = nfs4_preprocess_seqid_op(cstate,
3864 lock->lk_old_lock_seqid,
3865 &lock->lk_old_lock_stateid,
3866 LOCK_STATE,
3867 &lock->lk_replay_owner, &lock_stp, lock);
3868 if (status)
3869 goto out;
3870 lock_sop = lock->lk_replay_owner;
3871 fp = lock_stp->st_file;
3873 /* lock->lk_replay_owner and lock_stp have been created or found */
3875 status = nfserr_grace;
3876 if (locks_in_grace() && !lock->lk_reclaim)
3877 goto out;
3878 status = nfserr_no_grace;
3879 if (!locks_in_grace() && lock->lk_reclaim)
3880 goto out;
3882 locks_init_lock(&file_lock);
3883 switch (lock->lk_type) {
3884 case NFS4_READ_LT:
3885 case NFS4_READW_LT:
3886 filp = find_readable_file(lock_stp->st_file);
3887 if (filp)
3888 get_lock_access(lock_stp, NFS4_SHARE_ACCESS_READ);
3889 file_lock.fl_type = F_RDLCK;
3890 break;
3891 case NFS4_WRITE_LT:
3892 case NFS4_WRITEW_LT:
3893 filp = find_writeable_file(lock_stp->st_file);
3894 if (filp)
3895 get_lock_access(lock_stp, NFS4_SHARE_ACCESS_WRITE);
3896 file_lock.fl_type = F_WRLCK;
3897 break;
3898 default:
3899 status = nfserr_inval;
3900 goto out;
3902 if (!filp) {
3903 status = nfserr_openmode;
3904 goto out;
3906 file_lock.fl_owner = (fl_owner_t)lock_sop;
3907 file_lock.fl_pid = current->tgid;
3908 file_lock.fl_file = filp;
3909 file_lock.fl_flags = FL_POSIX;
3910 file_lock.fl_lmops = &nfsd_posix_mng_ops;
3912 file_lock.fl_start = lock->lk_offset;
3913 file_lock.fl_end = last_byte_offset(lock->lk_offset, lock->lk_length);
3914 nfs4_transform_lock_offset(&file_lock);
3917 * Try to lock the file in the VFS.
3918 * Note: locks.c uses the BKL to protect the inode's lock list.
3921 err = vfs_lock_file(filp, F_SETLK, &file_lock, &conflock);
3922 switch (-err) {
3923 case 0: /* success! */
3924 update_stateid(&lock_stp->st_stateid);
3925 memcpy(&lock->lk_resp_stateid, &lock_stp->st_stateid,
3926 sizeof(stateid_t));
3927 status = 0;
3928 break;
3929 case (EAGAIN): /* conflock holds conflicting lock */
3930 status = nfserr_denied;
3931 dprintk("NFSD: nfsd4_lock: conflicting lock found!\n");
3932 nfs4_set_lock_denied(&conflock, &lock->lk_denied);
3933 break;
3934 case (EDEADLK):
3935 status = nfserr_deadlock;
3936 break;
3937 default:
3938 dprintk("NFSD: nfsd4_lock: vfs_lock_file() failed! status %d\n",err);
3939 status = nfserrno(err);
3940 break;
3942 out:
3943 if (status && lock->lk_is_new && lock_sop)
3944 release_lockowner(lock_sop);
3945 if (lock->lk_replay_owner) {
3946 nfs4_get_stateowner(lock->lk_replay_owner);
3947 cstate->replay_owner = lock->lk_replay_owner;
3949 nfs4_unlock_state();
3950 return status;
3954 * The NFSv4 spec allows a client to do a LOCKT without holding an OPEN,
3955 * so we do a temporary open here just to get an open file to pass to
3956 * vfs_test_lock. (Arguably perhaps test_lock should be done with an
3957 * inode operation.)
3959 static int nfsd_test_lock(struct svc_rqst *rqstp, struct svc_fh *fhp, struct file_lock *lock)
3961 struct file *file;
3962 int err;
3964 err = nfsd_open(rqstp, fhp, S_IFREG, NFSD_MAY_READ, &file);
3965 if (err)
3966 return err;
3967 err = vfs_test_lock(file, lock);
3968 nfsd_close(file);
3969 return err;
3973 * LOCKT operation
3975 __be32
3976 nfsd4_lockt(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
3977 struct nfsd4_lockt *lockt)
3979 struct inode *inode;
3980 struct file_lock file_lock;
3981 int error;
3982 __be32 status;
3984 if (locks_in_grace())
3985 return nfserr_grace;
3987 if (check_lock_length(lockt->lt_offset, lockt->lt_length))
3988 return nfserr_inval;
3990 lockt->lt_stateowner = NULL;
3991 nfs4_lock_state();
3993 status = nfserr_stale_clientid;
3994 if (!nfsd4_has_session(cstate) && STALE_CLIENTID(&lockt->lt_clientid))
3995 goto out;
3997 if ((status = fh_verify(rqstp, &cstate->current_fh, S_IFREG, 0))) {
3998 dprintk("NFSD: nfsd4_lockt: fh_verify() failed!\n");
3999 if (status == nfserr_symlink)
4000 status = nfserr_inval;
4001 goto out;
4004 inode = cstate->current_fh.fh_dentry->d_inode;
4005 locks_init_lock(&file_lock);
4006 switch (lockt->lt_type) {
4007 case NFS4_READ_LT:
4008 case NFS4_READW_LT:
4009 file_lock.fl_type = F_RDLCK;
4010 break;
4011 case NFS4_WRITE_LT:
4012 case NFS4_WRITEW_LT:
4013 file_lock.fl_type = F_WRLCK;
4014 break;
4015 default:
4016 dprintk("NFSD: nfs4_lockt: bad lock type!\n");
4017 status = nfserr_inval;
4018 goto out;
4021 lockt->lt_stateowner = find_lockstateowner_str(inode,
4022 &lockt->lt_clientid, &lockt->lt_owner);
4023 if (lockt->lt_stateowner)
4024 file_lock.fl_owner = (fl_owner_t)lockt->lt_stateowner;
4025 file_lock.fl_pid = current->tgid;
4026 file_lock.fl_flags = FL_POSIX;
4028 file_lock.fl_start = lockt->lt_offset;
4029 file_lock.fl_end = last_byte_offset(lockt->lt_offset, lockt->lt_length);
4031 nfs4_transform_lock_offset(&file_lock);
4033 status = nfs_ok;
4034 error = nfsd_test_lock(rqstp, &cstate->current_fh, &file_lock);
4035 if (error) {
4036 status = nfserrno(error);
4037 goto out;
4039 if (file_lock.fl_type != F_UNLCK) {
4040 status = nfserr_denied;
4041 nfs4_set_lock_denied(&file_lock, &lockt->lt_denied);
4043 out:
4044 nfs4_unlock_state();
4045 return status;
4048 __be32
4049 nfsd4_locku(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
4050 struct nfsd4_locku *locku)
4052 struct nfs4_stateid *stp;
4053 struct file *filp = NULL;
4054 struct file_lock file_lock;
4055 __be32 status;
4056 int err;
4058 dprintk("NFSD: nfsd4_locku: start=%Ld length=%Ld\n",
4059 (long long) locku->lu_offset,
4060 (long long) locku->lu_length);
4062 if (check_lock_length(locku->lu_offset, locku->lu_length))
4063 return nfserr_inval;
4065 nfs4_lock_state();
4067 if ((status = nfs4_preprocess_seqid_op(cstate,
4068 locku->lu_seqid,
4069 &locku->lu_stateid,
4070 LOCK_STATE,
4071 &locku->lu_stateowner, &stp, NULL)))
4072 goto out;
4074 filp = find_any_file(stp->st_file);
4075 if (!filp) {
4076 status = nfserr_lock_range;
4077 goto out;
4079 BUG_ON(!filp);
4080 locks_init_lock(&file_lock);
4081 file_lock.fl_type = F_UNLCK;
4082 file_lock.fl_owner = (fl_owner_t) locku->lu_stateowner;
4083 file_lock.fl_pid = current->tgid;
4084 file_lock.fl_file = filp;
4085 file_lock.fl_flags = FL_POSIX;
4086 file_lock.fl_lmops = &nfsd_posix_mng_ops;
4087 file_lock.fl_start = locku->lu_offset;
4089 file_lock.fl_end = last_byte_offset(locku->lu_offset, locku->lu_length);
4090 nfs4_transform_lock_offset(&file_lock);
4093 * Try to unlock the file in the VFS.
4095 err = vfs_lock_file(filp, F_SETLK, &file_lock, NULL);
4096 if (err) {
4097 dprintk("NFSD: nfs4_locku: vfs_lock_file failed!\n");
4098 goto out_nfserr;
4101 * OK, unlock succeeded; the only thing left to do is update the stateid.
4103 update_stateid(&stp->st_stateid);
4104 memcpy(&locku->lu_stateid, &stp->st_stateid, sizeof(stateid_t));
4106 out:
4107 if (locku->lu_stateowner) {
4108 nfs4_get_stateowner(locku->lu_stateowner);
4109 cstate->replay_owner = locku->lu_stateowner;
4111 nfs4_unlock_state();
4112 return status;
4114 out_nfserr:
4115 status = nfserrno(err);
4116 goto out;
4120 * returns
4121 * 1: locks held by lockowner
4122 * 0: no locks held by lockowner
4124 static int
4125 check_for_locks(struct nfs4_file *filp, struct nfs4_stateowner *lowner)
4127 struct file_lock **flpp;
4128 struct inode *inode = filp->fi_inode;
4129 int status = 0;
4131 lock_flocks();
4132 for (flpp = &inode->i_flock; *flpp != NULL; flpp = &(*flpp)->fl_next) {
4133 if ((*flpp)->fl_owner == (fl_owner_t)lowner) {
4134 status = 1;
4135 goto out;
4138 out:
4139 unlock_flocks();
4140 return status;
4143 __be32
4144 nfsd4_release_lockowner(struct svc_rqst *rqstp,
4145 struct nfsd4_compound_state *cstate,
4146 struct nfsd4_release_lockowner *rlockowner)
4148 clientid_t *clid = &rlockowner->rl_clientid;
4149 struct nfs4_stateowner *sop;
4150 struct nfs4_stateid *stp;
4151 struct xdr_netobj *owner = &rlockowner->rl_owner;
4152 struct list_head matches;
4153 int i;
4154 __be32 status;
4156 dprintk("nfsd4_release_lockowner clientid: (%08x/%08x):\n",
4157 clid->cl_boot, clid->cl_id);
4159 /* XXX check for lease expiration */
4161 status = nfserr_stale_clientid;
4162 if (STALE_CLIENTID(clid))
4163 return status;
4165 nfs4_lock_state();
4167 status = nfserr_locks_held;
4168 /* XXX: we're doing a linear search through all the lockowners.
4169 * Yipes! For now we'll just hope clients aren't really using
4170 * release_lockowner much, but eventually we have to fix these
4171 * data structures. */
4172 INIT_LIST_HEAD(&matches);
4173 for (i = 0; i < LOCK_HASH_SIZE; i++) {
4174 list_for_each_entry(sop, &lock_ownerid_hashtbl[i], so_idhash) {
4175 if (!same_owner_str(sop, owner, clid))
4176 continue;
4177 list_for_each_entry(stp, &sop->so_stateids,
4178 st_perstateowner) {
4179 if (check_for_locks(stp->st_file, sop))
4180 goto out;
4181 /* Note: so_perclient unused for lockowners,
4182 * so it's OK to fool with here. */
4183 list_add(&sop->so_perclient, &matches);
4187 /* Clients probably won't expect us to return with some (but not all)
4188 * of the lockowner state released; so don't release any until all
4189 * have been checked. */
4190 status = nfs_ok;
4191 while (!list_empty(&matches)) {
4192 sop = list_entry(matches.next, struct nfs4_stateowner,
4193 so_perclient);
4194 /* unhash_stateowner deletes so_perclient only
4195 * for openowners. */
4196 list_del(&sop->so_perclient);
4197 release_lockowner(sop);
4199 out:
4200 nfs4_unlock_state();
4201 return status;
4204 static inline struct nfs4_client_reclaim *
4205 alloc_reclaim(void)
4207 return kmalloc(sizeof(struct nfs4_client_reclaim), GFP_KERNEL);
4211 nfs4_has_reclaimed_state(const char *name, bool use_exchange_id)
4213 unsigned int strhashval = clientstr_hashval(name);
4214 struct nfs4_client *clp;
4216 clp = find_confirmed_client_by_str(name, strhashval);
4217 return clp ? 1 : 0;
4221 * failure => all reset bets are off, nfserr_no_grace...
4224 nfs4_client_to_reclaim(const char *name)
4226 unsigned int strhashval;
4227 struct nfs4_client_reclaim *crp = NULL;
4229 dprintk("NFSD nfs4_client_to_reclaim NAME: %.*s\n", HEXDIR_LEN, name);
4230 crp = alloc_reclaim();
4231 if (!crp)
4232 return 0;
4233 strhashval = clientstr_hashval(name);
4234 INIT_LIST_HEAD(&crp->cr_strhash);
4235 list_add(&crp->cr_strhash, &reclaim_str_hashtbl[strhashval]);
4236 memcpy(crp->cr_recdir, name, HEXDIR_LEN);
4237 reclaim_str_hashtbl_size++;
4238 return 1;
4241 static void
4242 nfs4_release_reclaim(void)
4244 struct nfs4_client_reclaim *crp = NULL;
4245 int i;
4247 for (i = 0; i < CLIENT_HASH_SIZE; i++) {
4248 while (!list_empty(&reclaim_str_hashtbl[i])) {
4249 crp = list_entry(reclaim_str_hashtbl[i].next,
4250 struct nfs4_client_reclaim, cr_strhash);
4251 list_del(&crp->cr_strhash);
4252 kfree(crp);
4253 reclaim_str_hashtbl_size--;
4256 BUG_ON(reclaim_str_hashtbl_size);
4260 * called from OPEN, CLAIM_PREVIOUS with a new clientid. */
4261 static struct nfs4_client_reclaim *
4262 nfs4_find_reclaim_client(clientid_t *clid)
4264 unsigned int strhashval;
4265 struct nfs4_client *clp;
4266 struct nfs4_client_reclaim *crp = NULL;
4269 /* find clientid in conf_id_hashtbl */
4270 clp = find_confirmed_client(clid);
4271 if (clp == NULL)
4272 return NULL;
4274 dprintk("NFSD: nfs4_find_reclaim_client for %.*s with recdir %s\n",
4275 clp->cl_name.len, clp->cl_name.data,
4276 clp->cl_recdir);
4278 /* find clp->cl_name in reclaim_str_hashtbl */
4279 strhashval = clientstr_hashval(clp->cl_recdir);
4280 list_for_each_entry(crp, &reclaim_str_hashtbl[strhashval], cr_strhash) {
4281 if (same_name(crp->cr_recdir, clp->cl_recdir)) {
4282 return crp;
4285 return NULL;
4289 * Called from OPEN. Look for clientid in reclaim list.
4291 __be32
4292 nfs4_check_open_reclaim(clientid_t *clid)
4294 return nfs4_find_reclaim_client(clid) ? nfs_ok : nfserr_reclaim_bad;
4297 /* initialization to perform at module load time: */
4300 nfs4_state_init(void)
4302 int i, status;
4304 status = nfsd4_init_slabs();
4305 if (status)
4306 return status;
4307 for (i = 0; i < CLIENT_HASH_SIZE; i++) {
4308 INIT_LIST_HEAD(&conf_id_hashtbl[i]);
4309 INIT_LIST_HEAD(&conf_str_hashtbl[i]);
4310 INIT_LIST_HEAD(&unconf_str_hashtbl[i]);
4311 INIT_LIST_HEAD(&unconf_id_hashtbl[i]);
4312 INIT_LIST_HEAD(&reclaim_str_hashtbl[i]);
4314 for (i = 0; i < SESSION_HASH_SIZE; i++)
4315 INIT_LIST_HEAD(&sessionid_hashtbl[i]);
4316 for (i = 0; i < FILE_HASH_SIZE; i++) {
4317 INIT_LIST_HEAD(&file_hashtbl[i]);
4319 for (i = 0; i < OWNER_HASH_SIZE; i++) {
4320 INIT_LIST_HEAD(&ownerstr_hashtbl[i]);
4321 INIT_LIST_HEAD(&ownerid_hashtbl[i]);
4323 for (i = 0; i < STATEID_HASH_SIZE; i++) {
4324 INIT_LIST_HEAD(&stateid_hashtbl[i]);
4325 INIT_LIST_HEAD(&lockstateid_hashtbl[i]);
4327 for (i = 0; i < LOCK_HASH_SIZE; i++) {
4328 INIT_LIST_HEAD(&lock_ownerid_hashtbl[i]);
4329 INIT_LIST_HEAD(&lock_ownerstr_hashtbl[i]);
4331 memset(&onestateid, ~0, sizeof(stateid_t));
4332 INIT_LIST_HEAD(&close_lru);
4333 INIT_LIST_HEAD(&client_lru);
4334 INIT_LIST_HEAD(&del_recall_lru);
4335 reclaim_str_hashtbl_size = 0;
4336 return 0;
4339 static void
4340 nfsd4_load_reboot_recovery_data(void)
4342 int status;
4344 nfs4_lock_state();
4345 nfsd4_init_recdir(user_recovery_dirname);
4346 status = nfsd4_recdir_load();
4347 nfs4_unlock_state();
4348 if (status)
4349 printk("NFSD: Failure reading reboot recovery data\n");
4353 * Since the lifetime of a delegation isn't limited to that of an open, a
4354 * client may quite reasonably hang on to a delegation as long as it has
4355 * the inode cached. This becomes an obvious problem the first time a
4356 * client's inode cache approaches the size of the server's total memory.
4358 * For now we avoid this problem by imposing a hard limit on the number
4359 * of delegations, which varies according to the server's memory size.
4361 static void
4362 set_max_delegations(void)
4365 * Allow at most 4 delegations per megabyte of RAM. Quick
4366 * estimates suggest that in the worst case (where every delegation
4367 * is for a different inode), a delegation could take about 1.5K,
4368 * giving a worst case usage of about 6% of memory.
4370 max_delegations = nr_free_buffer_pages() >> (20 - 2 - PAGE_SHIFT);
4373 /* initialization to perform when the nfsd service is started: */
4375 static int
4376 __nfs4_state_start(void)
4378 int ret;
4380 boot_time = get_seconds();
4381 locks_start_grace(&nfsd4_manager);
4382 printk(KERN_INFO "NFSD: starting %ld-second grace period\n",
4383 nfsd4_grace);
4384 ret = set_callback_cred();
4385 if (ret)
4386 return -ENOMEM;
4387 laundry_wq = create_singlethread_workqueue("nfsd4");
4388 if (laundry_wq == NULL)
4389 return -ENOMEM;
4390 ret = nfsd4_create_callback_queue();
4391 if (ret)
4392 goto out_free_laundry;
4393 queue_delayed_work(laundry_wq, &laundromat_work, nfsd4_grace * HZ);
4394 set_max_delegations();
4395 return 0;
4396 out_free_laundry:
4397 destroy_workqueue(laundry_wq);
4398 return ret;
4402 nfs4_state_start(void)
4404 nfsd4_load_reboot_recovery_data();
4405 return __nfs4_state_start();
4408 static void
4409 __nfs4_state_shutdown(void)
4411 int i;
4412 struct nfs4_client *clp = NULL;
4413 struct nfs4_delegation *dp = NULL;
4414 struct list_head *pos, *next, reaplist;
4416 for (i = 0; i < CLIENT_HASH_SIZE; i++) {
4417 while (!list_empty(&conf_id_hashtbl[i])) {
4418 clp = list_entry(conf_id_hashtbl[i].next, struct nfs4_client, cl_idhash);
4419 expire_client(clp);
4421 while (!list_empty(&unconf_str_hashtbl[i])) {
4422 clp = list_entry(unconf_str_hashtbl[i].next, struct nfs4_client, cl_strhash);
4423 expire_client(clp);
4426 INIT_LIST_HEAD(&reaplist);
4427 spin_lock(&recall_lock);
4428 list_for_each_safe(pos, next, &del_recall_lru) {
4429 dp = list_entry (pos, struct nfs4_delegation, dl_recall_lru);
4430 list_move(&dp->dl_recall_lru, &reaplist);
4432 spin_unlock(&recall_lock);
4433 list_for_each_safe(pos, next, &reaplist) {
4434 dp = list_entry (pos, struct nfs4_delegation, dl_recall_lru);
4435 list_del_init(&dp->dl_recall_lru);
4436 unhash_delegation(dp);
4439 nfsd4_shutdown_recdir();
4442 void
4443 nfs4_state_shutdown(void)
4445 cancel_delayed_work_sync(&laundromat_work);
4446 destroy_workqueue(laundry_wq);
4447 locks_end_grace(&nfsd4_manager);
4448 nfs4_lock_state();
4449 nfs4_release_reclaim();
4450 __nfs4_state_shutdown();
4451 nfs4_unlock_state();
4452 nfsd4_destroy_callback_queue();
4456 * user_recovery_dirname is protected by the nfsd_mutex since it's only
4457 * accessed when nfsd is starting.
4459 static void
4460 nfs4_set_recdir(char *recdir)
4462 strcpy(user_recovery_dirname, recdir);
4466 * Change the NFSv4 recovery directory to recdir.
4469 nfs4_reset_recoverydir(char *recdir)
4471 int status;
4472 struct path path;
4474 status = kern_path(recdir, LOOKUP_FOLLOW, &path);
4475 if (status)
4476 return status;
4477 status = -ENOTDIR;
4478 if (S_ISDIR(path.dentry->d_inode->i_mode)) {
4479 nfs4_set_recdir(recdir);
4480 status = 0;
4482 path_put(&path);
4483 return status;
4486 char *
4487 nfs4_recoverydir(void)
4489 return user_recovery_dirname;