nfsd4: rearrange to avoid a forward reference
[deliverable/linux.git] / fs / nfsd / nfs4state.c
1 /*
2 * Copyright (c) 2001 The Regents of the University of Michigan.
3 * All rights reserved.
4 *
5 * Kendrick Smith <kmsmith@umich.edu>
6 * Andy Adamson <kandros@umich.edu>
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
11 *
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.
20 *
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.
32 *
33 */
34
35 #include <linux/file.h>
36 #include <linux/fs.h>
37 #include <linux/slab.h>
38 #include <linux/namei.h>
39 #include <linux/swap.h>
40 #include <linux/pagemap.h>
41 #include <linux/sunrpc/svcauth_gss.h>
42 #include <linux/sunrpc/clnt.h>
43 #include "xdr4.h"
44 #include "vfs.h"
45
46 #define NFSDDBG_FACILITY NFSDDBG_PROC
47
48 /* Globals */
49 time_t nfsd4_lease = 90; /* default lease time */
50 time_t nfsd4_grace = 90;
51 static time_t boot_time;
52 static u32 current_ownerid = 1;
53 static u32 current_fileid = 1;
54 static u32 current_delegid = 1;
55 static stateid_t zerostateid; /* bits all 0 */
56 static stateid_t onestateid; /* bits all 1 */
57 static u64 current_sessionid = 1;
58
59 #define ZERO_STATEID(stateid) (!memcmp((stateid), &zerostateid, sizeof(stateid_t)))
60 #define ONE_STATEID(stateid) (!memcmp((stateid), &onestateid, sizeof(stateid_t)))
61
62 /* forward declarations */
63 static struct nfs4_delegation * search_for_delegation(stateid_t *stid);
64 static struct nfs4_delegation * find_delegation_stateid(struct inode *ino, stateid_t *stid);
65 static int check_for_locks(struct nfs4_file *filp, struct nfs4_lockowner *lowner);
66
67 /* Locking: */
68
69 /* Currently used for almost all code touching nfsv4 state: */
70 static DEFINE_MUTEX(client_mutex);
71
72 /*
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:
76 */
77 static DEFINE_SPINLOCK(recall_lock);
78
79 static struct kmem_cache *openowner_slab = NULL;
80 static struct kmem_cache *lockowner_slab = NULL;
81 static struct kmem_cache *file_slab = NULL;
82 static struct kmem_cache *stateid_slab = NULL;
83 static struct kmem_cache *deleg_slab = NULL;
84
85 void
86 nfs4_lock_state(void)
87 {
88 mutex_lock(&client_mutex);
89 }
90
91 void
92 nfs4_unlock_state(void)
93 {
94 mutex_unlock(&client_mutex);
95 }
96
97 static inline u32
98 opaque_hashval(const void *ptr, int nbytes)
99 {
100 unsigned char *cptr = (unsigned char *) ptr;
101
102 u32 x = 0;
103 while (nbytes--) {
104 x *= 37;
105 x += *cptr++;
106 }
107 return x;
108 }
109
110 static struct list_head del_recall_lru;
111
112 static inline void
113 put_nfs4_file(struct nfs4_file *fi)
114 {
115 if (atomic_dec_and_lock(&fi->fi_ref, &recall_lock)) {
116 list_del(&fi->fi_hash);
117 spin_unlock(&recall_lock);
118 iput(fi->fi_inode);
119 kmem_cache_free(file_slab, fi);
120 }
121 }
122
123 static inline void
124 get_nfs4_file(struct nfs4_file *fi)
125 {
126 atomic_inc(&fi->fi_ref);
127 }
128
129 static int num_delegations;
130 unsigned int max_delegations;
131
132 /*
133 * Open owner state (share locks)
134 */
135
136 /* hash tables for open owners */
137 #define OPEN_OWNER_HASH_BITS 8
138 #define OPEN_OWNER_HASH_SIZE (1 << OPEN_OWNER_HASH_BITS)
139 #define OPEN_OWNER_HASH_MASK (OPEN_OWNER_HASH_SIZE - 1)
140
141 static unsigned int open_ownerid_hashval(const u32 id)
142 {
143 return id & OPEN_OWNER_HASH_MASK;
144 }
145
146 static unsigned int open_ownerstr_hashval(u32 clientid, struct xdr_netobj *ownername)
147 {
148 unsigned int ret;
149
150 ret = opaque_hashval(ownername->data, ownername->len);
151 ret += clientid;
152 return ret & OPEN_OWNER_HASH_MASK;
153 }
154
155 static struct list_head open_ownerid_hashtbl[OPEN_OWNER_HASH_SIZE];
156 static struct list_head open_ownerstr_hashtbl[OPEN_OWNER_HASH_SIZE];
157
158 /* hash table for nfs4_file */
159 #define FILE_HASH_BITS 8
160 #define FILE_HASH_SIZE (1 << FILE_HASH_BITS)
161
162 /* hash table for (open)nfs4_stateid */
163 #define STATEID_HASH_BITS 10
164 #define STATEID_HASH_SIZE (1 << STATEID_HASH_BITS)
165 #define STATEID_HASH_MASK (STATEID_HASH_SIZE - 1)
166
167 static unsigned int file_hashval(struct inode *ino)
168 {
169 /* XXX: why are we hashing on inode pointer, anyway? */
170 return hash_ptr(ino, FILE_HASH_BITS);
171 }
172
173 static unsigned int stateid_hashval(u32 owner_id, u32 file_id)
174 {
175 return (owner_id + file_id) & STATEID_HASH_MASK;
176 }
177
178 static struct list_head file_hashtbl[FILE_HASH_SIZE];
179 static struct list_head stateid_hashtbl[STATEID_HASH_SIZE];
180
181 static void __nfs4_file_get_access(struct nfs4_file *fp, int oflag)
182 {
183 BUG_ON(!(fp->fi_fds[oflag] || fp->fi_fds[O_RDWR]));
184 atomic_inc(&fp->fi_access[oflag]);
185 }
186
187 static void nfs4_file_get_access(struct nfs4_file *fp, int oflag)
188 {
189 if (oflag == O_RDWR) {
190 __nfs4_file_get_access(fp, O_RDONLY);
191 __nfs4_file_get_access(fp, O_WRONLY);
192 } else
193 __nfs4_file_get_access(fp, oflag);
194 }
195
196 static void nfs4_file_put_fd(struct nfs4_file *fp, int oflag)
197 {
198 if (fp->fi_fds[oflag]) {
199 fput(fp->fi_fds[oflag]);
200 fp->fi_fds[oflag] = NULL;
201 }
202 }
203
204 static void __nfs4_file_put_access(struct nfs4_file *fp, int oflag)
205 {
206 if (atomic_dec_and_test(&fp->fi_access[oflag])) {
207 nfs4_file_put_fd(fp, O_RDWR);
208 nfs4_file_put_fd(fp, oflag);
209 }
210 }
211
212 static void nfs4_file_put_access(struct nfs4_file *fp, int oflag)
213 {
214 if (oflag == O_RDWR) {
215 __nfs4_file_put_access(fp, O_RDONLY);
216 __nfs4_file_put_access(fp, O_WRONLY);
217 } else
218 __nfs4_file_put_access(fp, oflag);
219 }
220
221 static struct nfs4_delegation *
222 alloc_init_deleg(struct nfs4_client *clp, struct nfs4_stateid *stp, struct svc_fh *current_fh, u32 type)
223 {
224 struct nfs4_delegation *dp;
225 struct nfs4_file *fp = stp->st_file;
226
227 dprintk("NFSD alloc_init_deleg\n");
228 /*
229 * Major work on the lease subsystem (for example, to support
230 * calbacks on stat) will be required before we can support
231 * write delegations properly.
232 */
233 if (type != NFS4_OPEN_DELEGATE_READ)
234 return NULL;
235 if (fp->fi_had_conflict)
236 return NULL;
237 if (num_delegations > max_delegations)
238 return NULL;
239 dp = kmem_cache_alloc(deleg_slab, GFP_KERNEL);
240 if (dp == NULL)
241 return dp;
242 num_delegations++;
243 INIT_LIST_HEAD(&dp->dl_perfile);
244 INIT_LIST_HEAD(&dp->dl_perclnt);
245 INIT_LIST_HEAD(&dp->dl_recall_lru);
246 dp->dl_client = clp;
247 get_nfs4_file(fp);
248 dp->dl_file = fp;
249 dp->dl_type = type;
250 dp->dl_stateid.si_boot = boot_time;
251 dp->dl_stateid.si_stateownerid = current_delegid++;
252 dp->dl_stateid.si_fileid = 0;
253 dp->dl_stateid.si_generation = 1;
254 fh_copy_shallow(&dp->dl_fh, &current_fh->fh_handle);
255 dp->dl_time = 0;
256 atomic_set(&dp->dl_count, 1);
257 INIT_WORK(&dp->dl_recall.cb_work, nfsd4_do_callback_rpc);
258 return dp;
259 }
260
261 void
262 nfs4_put_delegation(struct nfs4_delegation *dp)
263 {
264 if (atomic_dec_and_test(&dp->dl_count)) {
265 dprintk("NFSD: freeing dp %p\n",dp);
266 put_nfs4_file(dp->dl_file);
267 kmem_cache_free(deleg_slab, dp);
268 num_delegations--;
269 }
270 }
271
272 static void nfs4_put_deleg_lease(struct nfs4_file *fp)
273 {
274 if (atomic_dec_and_test(&fp->fi_delegees)) {
275 vfs_setlease(fp->fi_deleg_file, F_UNLCK, &fp->fi_lease);
276 fp->fi_lease = NULL;
277 fput(fp->fi_deleg_file);
278 fp->fi_deleg_file = NULL;
279 }
280 }
281
282 /* Called under the state lock. */
283 static void
284 unhash_delegation(struct nfs4_delegation *dp)
285 {
286 list_del_init(&dp->dl_perclnt);
287 spin_lock(&recall_lock);
288 list_del_init(&dp->dl_perfile);
289 list_del_init(&dp->dl_recall_lru);
290 spin_unlock(&recall_lock);
291 nfs4_put_deleg_lease(dp->dl_file);
292 nfs4_put_delegation(dp);
293 }
294
295 /*
296 * SETCLIENTID state
297 */
298
299 /* client_lock protects the client lru list and session hash table */
300 static DEFINE_SPINLOCK(client_lock);
301
302 /* Hash tables for nfs4_clientid state */
303 #define CLIENT_HASH_BITS 4
304 #define CLIENT_HASH_SIZE (1 << CLIENT_HASH_BITS)
305 #define CLIENT_HASH_MASK (CLIENT_HASH_SIZE - 1)
306
307 static unsigned int clientid_hashval(u32 id)
308 {
309 return id & CLIENT_HASH_MASK;
310 }
311
312 static unsigned int clientstr_hashval(const char *name)
313 {
314 return opaque_hashval(name, 8) & CLIENT_HASH_MASK;
315 }
316
317 /*
318 * reclaim_str_hashtbl[] holds known client info from previous reset/reboot
319 * used in reboot/reset lease grace period processing
320 *
321 * conf_id_hashtbl[], and conf_str_hashtbl[] hold confirmed
322 * setclientid_confirmed info.
323 *
324 * unconf_str_hastbl[] and unconf_id_hashtbl[] hold unconfirmed
325 * setclientid info.
326 *
327 * client_lru holds client queue ordered by nfs4_client.cl_time
328 * for lease renewal.
329 *
330 * close_lru holds (open) stateowner queue ordered by nfs4_stateowner.so_time
331 * for last close replay.
332 */
333 static struct list_head reclaim_str_hashtbl[CLIENT_HASH_SIZE];
334 static int reclaim_str_hashtbl_size = 0;
335 static struct list_head conf_id_hashtbl[CLIENT_HASH_SIZE];
336 static struct list_head conf_str_hashtbl[CLIENT_HASH_SIZE];
337 static struct list_head unconf_str_hashtbl[CLIENT_HASH_SIZE];
338 static struct list_head unconf_id_hashtbl[CLIENT_HASH_SIZE];
339 static struct list_head client_lru;
340 static struct list_head close_lru;
341
342 /*
343 * We store the NONE, READ, WRITE, and BOTH bits separately in the
344 * st_{access,deny}_bmap field of the stateid, in order to track not
345 * only what share bits are currently in force, but also what
346 * combinations of share bits previous opens have used. This allows us
347 * to enforce the recommendation of rfc 3530 14.2.19 that the server
348 * return an error if the client attempt to downgrade to a combination
349 * of share bits not explicable by closing some of its previous opens.
350 *
351 * XXX: This enforcement is actually incomplete, since we don't keep
352 * track of access/deny bit combinations; so, e.g., we allow:
353 *
354 * OPEN allow read, deny write
355 * OPEN allow both, deny none
356 * DOWNGRADE allow read, deny none
357 *
358 * which we should reject.
359 */
360 static void
361 set_access(unsigned int *access, unsigned long bmap) {
362 int i;
363
364 *access = 0;
365 for (i = 1; i < 4; i++) {
366 if (test_bit(i, &bmap))
367 *access |= i;
368 }
369 }
370
371 static void
372 set_deny(unsigned int *deny, unsigned long bmap) {
373 int i;
374
375 *deny = 0;
376 for (i = 0; i < 4; i++) {
377 if (test_bit(i, &bmap))
378 *deny |= i ;
379 }
380 }
381
382 static int
383 test_share(struct nfs4_stateid *stp, struct nfsd4_open *open) {
384 unsigned int access, deny;
385
386 set_access(&access, stp->st_access_bmap);
387 set_deny(&deny, stp->st_deny_bmap);
388 if ((access & open->op_share_deny) || (deny & open->op_share_access))
389 return 0;
390 return 1;
391 }
392
393 static int nfs4_access_to_omode(u32 access)
394 {
395 switch (access & NFS4_SHARE_ACCESS_BOTH) {
396 case NFS4_SHARE_ACCESS_READ:
397 return O_RDONLY;
398 case NFS4_SHARE_ACCESS_WRITE:
399 return O_WRONLY;
400 case NFS4_SHARE_ACCESS_BOTH:
401 return O_RDWR;
402 }
403 BUG();
404 }
405
406 static void unhash_generic_stateid(struct nfs4_stateid *stp)
407 {
408 list_del(&stp->st_hash);
409 list_del(&stp->st_perfile);
410 list_del(&stp->st_perstateowner);
411 }
412
413 static void close_generic_stateid(struct nfs4_stateid *stp)
414 {
415 int i;
416
417 if (stp->st_access_bmap) {
418 for (i = 1; i < 4; i++) {
419 if (test_bit(i, &stp->st_access_bmap))
420 nfs4_file_put_access(stp->st_file,
421 nfs4_access_to_omode(i));
422 __clear_bit(i, &stp->st_access_bmap);
423 }
424 }
425 put_nfs4_file(stp->st_file);
426 stp->st_file = NULL;
427 }
428
429 static void free_generic_stateid(struct nfs4_stateid *stp)
430 {
431 close_generic_stateid(stp);
432 kmem_cache_free(stateid_slab, stp);
433 }
434
435 static void release_lock_stateid(struct nfs4_stateid *stp)
436 {
437 struct file *file;
438
439 unhash_generic_stateid(stp);
440 file = find_any_file(stp->st_file);
441 if (file)
442 locks_remove_posix(file, (fl_owner_t)lockowner(stp->st_stateowner));
443 free_generic_stateid(stp);
444 }
445
446 static void unhash_lockowner(struct nfs4_lockowner *lo)
447 {
448 struct nfs4_stateid *stp;
449
450 list_del(&lo->lo_owner.so_idhash);
451 list_del(&lo->lo_owner.so_strhash);
452 list_del(&lo->lo_perstateid);
453 while (!list_empty(&lo->lo_owner.so_stateids)) {
454 stp = list_first_entry(&lo->lo_owner.so_stateids,
455 struct nfs4_stateid, st_perstateowner);
456 release_lock_stateid(stp);
457 }
458 }
459
460 static void release_lockowner(struct nfs4_lockowner *lo)
461 {
462 unhash_lockowner(lo);
463 nfs4_free_lockowner(lo);
464 }
465
466 static void
467 release_stateid_lockowners(struct nfs4_stateid *open_stp)
468 {
469 struct nfs4_lockowner *lo;
470
471 while (!list_empty(&open_stp->st_lockowners)) {
472 lo = list_entry(open_stp->st_lockowners.next,
473 struct nfs4_lockowner, lo_perstateid);
474 release_lockowner(lo);
475 }
476 }
477
478 static void release_open_stateid(struct nfs4_stateid *stp)
479 {
480 unhash_generic_stateid(stp);
481 release_stateid_lockowners(stp);
482 free_generic_stateid(stp);
483 }
484
485 static void unhash_openowner(struct nfs4_openowner *oo)
486 {
487 struct nfs4_stateid *stp;
488
489 list_del(&oo->oo_owner.so_idhash);
490 list_del(&oo->oo_owner.so_strhash);
491 list_del(&oo->oo_perclient);
492 while (!list_empty(&oo->oo_owner.so_stateids)) {
493 stp = list_first_entry(&oo->oo_owner.so_stateids,
494 struct nfs4_stateid, st_perstateowner);
495 release_open_stateid(stp);
496 }
497 }
498
499 static void release_openowner(struct nfs4_openowner *oo)
500 {
501 unhash_openowner(oo);
502 list_del(&oo->oo_close_lru);
503 nfs4_free_openowner(oo);
504 }
505
506 #define SESSION_HASH_SIZE 512
507 static struct list_head sessionid_hashtbl[SESSION_HASH_SIZE];
508
509 static inline int
510 hash_sessionid(struct nfs4_sessionid *sessionid)
511 {
512 struct nfsd4_sessionid *sid = (struct nfsd4_sessionid *)sessionid;
513
514 return sid->sequence % SESSION_HASH_SIZE;
515 }
516
517 static inline void
518 dump_sessionid(const char *fn, struct nfs4_sessionid *sessionid)
519 {
520 u32 *ptr = (u32 *)(&sessionid->data[0]);
521 dprintk("%s: %u:%u:%u:%u\n", fn, ptr[0], ptr[1], ptr[2], ptr[3]);
522 }
523
524 static void
525 gen_sessionid(struct nfsd4_session *ses)
526 {
527 struct nfs4_client *clp = ses->se_client;
528 struct nfsd4_sessionid *sid;
529
530 sid = (struct nfsd4_sessionid *)ses->se_sessionid.data;
531 sid->clientid = clp->cl_clientid;
532 sid->sequence = current_sessionid++;
533 sid->reserved = 0;
534 }
535
536 /*
537 * The protocol defines ca_maxresponssize_cached to include the size of
538 * the rpc header, but all we need to cache is the data starting after
539 * the end of the initial SEQUENCE operation--the rest we regenerate
540 * each time. Therefore we can advertise a ca_maxresponssize_cached
541 * value that is the number of bytes in our cache plus a few additional
542 * bytes. In order to stay on the safe side, and not promise more than
543 * we can cache, those additional bytes must be the minimum possible: 24
544 * bytes of rpc header (xid through accept state, with AUTH_NULL
545 * verifier), 12 for the compound header (with zero-length tag), and 44
546 * for the SEQUENCE op response:
547 */
548 #define NFSD_MIN_HDR_SEQ_SZ (24 + 12 + 44)
549
550 static void
551 free_session_slots(struct nfsd4_session *ses)
552 {
553 int i;
554
555 for (i = 0; i < ses->se_fchannel.maxreqs; i++)
556 kfree(ses->se_slots[i]);
557 }
558
559 /*
560 * We don't actually need to cache the rpc and session headers, so we
561 * can allocate a little less for each slot:
562 */
563 static inline int slot_bytes(struct nfsd4_channel_attrs *ca)
564 {
565 return ca->maxresp_cached - NFSD_MIN_HDR_SEQ_SZ;
566 }
567
568 static int nfsd4_sanitize_slot_size(u32 size)
569 {
570 size -= NFSD_MIN_HDR_SEQ_SZ; /* We don't cache the rpc header */
571 size = min_t(u32, size, NFSD_SLOT_CACHE_SIZE);
572
573 return size;
574 }
575
576 /*
577 * XXX: If we run out of reserved DRC memory we could (up to a point)
578 * re-negotiate active sessions and reduce their slot usage to make
579 * rooom for new connections. For now we just fail the create session.
580 */
581 static int nfsd4_get_drc_mem(int slotsize, u32 num)
582 {
583 int avail;
584
585 num = min_t(u32, num, NFSD_MAX_SLOTS_PER_SESSION);
586
587 spin_lock(&nfsd_drc_lock);
588 avail = min_t(int, NFSD_MAX_MEM_PER_SESSION,
589 nfsd_drc_max_mem - nfsd_drc_mem_used);
590 num = min_t(int, num, avail / slotsize);
591 nfsd_drc_mem_used += num * slotsize;
592 spin_unlock(&nfsd_drc_lock);
593
594 return num;
595 }
596
597 static void nfsd4_put_drc_mem(int slotsize, int num)
598 {
599 spin_lock(&nfsd_drc_lock);
600 nfsd_drc_mem_used -= slotsize * num;
601 spin_unlock(&nfsd_drc_lock);
602 }
603
604 static struct nfsd4_session *alloc_session(int slotsize, int numslots)
605 {
606 struct nfsd4_session *new;
607 int mem, i;
608
609 BUILD_BUG_ON(NFSD_MAX_SLOTS_PER_SESSION * sizeof(struct nfsd4_slot *)
610 + sizeof(struct nfsd4_session) > PAGE_SIZE);
611 mem = numslots * sizeof(struct nfsd4_slot *);
612
613 new = kzalloc(sizeof(*new) + mem, GFP_KERNEL);
614 if (!new)
615 return NULL;
616 /* allocate each struct nfsd4_slot and data cache in one piece */
617 for (i = 0; i < numslots; i++) {
618 mem = sizeof(struct nfsd4_slot) + slotsize;
619 new->se_slots[i] = kzalloc(mem, GFP_KERNEL);
620 if (!new->se_slots[i])
621 goto out_free;
622 }
623 return new;
624 out_free:
625 while (i--)
626 kfree(new->se_slots[i]);
627 kfree(new);
628 return NULL;
629 }
630
631 static void init_forechannel_attrs(struct nfsd4_channel_attrs *new, struct nfsd4_channel_attrs *req, int numslots, int slotsize)
632 {
633 u32 maxrpc = nfsd_serv->sv_max_mesg;
634
635 new->maxreqs = numslots;
636 new->maxresp_cached = min_t(u32, req->maxresp_cached,
637 slotsize + NFSD_MIN_HDR_SEQ_SZ);
638 new->maxreq_sz = min_t(u32, req->maxreq_sz, maxrpc);
639 new->maxresp_sz = min_t(u32, req->maxresp_sz, maxrpc);
640 new->maxops = min_t(u32, req->maxops, NFSD_MAX_OPS_PER_COMPOUND);
641 }
642
643 static void free_conn(struct nfsd4_conn *c)
644 {
645 svc_xprt_put(c->cn_xprt);
646 kfree(c);
647 }
648
649 static void nfsd4_conn_lost(struct svc_xpt_user *u)
650 {
651 struct nfsd4_conn *c = container_of(u, struct nfsd4_conn, cn_xpt_user);
652 struct nfs4_client *clp = c->cn_session->se_client;
653
654 spin_lock(&clp->cl_lock);
655 if (!list_empty(&c->cn_persession)) {
656 list_del(&c->cn_persession);
657 free_conn(c);
658 }
659 spin_unlock(&clp->cl_lock);
660 nfsd4_probe_callback(clp);
661 }
662
663 static struct nfsd4_conn *alloc_conn(struct svc_rqst *rqstp, u32 flags)
664 {
665 struct nfsd4_conn *conn;
666
667 conn = kmalloc(sizeof(struct nfsd4_conn), GFP_KERNEL);
668 if (!conn)
669 return NULL;
670 svc_xprt_get(rqstp->rq_xprt);
671 conn->cn_xprt = rqstp->rq_xprt;
672 conn->cn_flags = flags;
673 INIT_LIST_HEAD(&conn->cn_xpt_user.list);
674 return conn;
675 }
676
677 static void __nfsd4_hash_conn(struct nfsd4_conn *conn, struct nfsd4_session *ses)
678 {
679 conn->cn_session = ses;
680 list_add(&conn->cn_persession, &ses->se_conns);
681 }
682
683 static void nfsd4_hash_conn(struct nfsd4_conn *conn, struct nfsd4_session *ses)
684 {
685 struct nfs4_client *clp = ses->se_client;
686
687 spin_lock(&clp->cl_lock);
688 __nfsd4_hash_conn(conn, ses);
689 spin_unlock(&clp->cl_lock);
690 }
691
692 static int nfsd4_register_conn(struct nfsd4_conn *conn)
693 {
694 conn->cn_xpt_user.callback = nfsd4_conn_lost;
695 return register_xpt_user(conn->cn_xprt, &conn->cn_xpt_user);
696 }
697
698 static __be32 nfsd4_new_conn(struct svc_rqst *rqstp, struct nfsd4_session *ses, u32 dir)
699 {
700 struct nfsd4_conn *conn;
701 int ret;
702
703 conn = alloc_conn(rqstp, dir);
704 if (!conn)
705 return nfserr_jukebox;
706 nfsd4_hash_conn(conn, ses);
707 ret = nfsd4_register_conn(conn);
708 if (ret)
709 /* oops; xprt is already down: */
710 nfsd4_conn_lost(&conn->cn_xpt_user);
711 return nfs_ok;
712 }
713
714 static __be32 nfsd4_new_conn_from_crses(struct svc_rqst *rqstp, struct nfsd4_session *ses)
715 {
716 u32 dir = NFS4_CDFC4_FORE;
717
718 if (ses->se_flags & SESSION4_BACK_CHAN)
719 dir |= NFS4_CDFC4_BACK;
720
721 return nfsd4_new_conn(rqstp, ses, dir);
722 }
723
724 /* must be called under client_lock */
725 static void nfsd4_del_conns(struct nfsd4_session *s)
726 {
727 struct nfs4_client *clp = s->se_client;
728 struct nfsd4_conn *c;
729
730 spin_lock(&clp->cl_lock);
731 while (!list_empty(&s->se_conns)) {
732 c = list_first_entry(&s->se_conns, struct nfsd4_conn, cn_persession);
733 list_del_init(&c->cn_persession);
734 spin_unlock(&clp->cl_lock);
735
736 unregister_xpt_user(c->cn_xprt, &c->cn_xpt_user);
737 free_conn(c);
738
739 spin_lock(&clp->cl_lock);
740 }
741 spin_unlock(&clp->cl_lock);
742 }
743
744 void free_session(struct kref *kref)
745 {
746 struct nfsd4_session *ses;
747 int mem;
748
749 ses = container_of(kref, struct nfsd4_session, se_ref);
750 nfsd4_del_conns(ses);
751 spin_lock(&nfsd_drc_lock);
752 mem = ses->se_fchannel.maxreqs * slot_bytes(&ses->se_fchannel);
753 nfsd_drc_mem_used -= mem;
754 spin_unlock(&nfsd_drc_lock);
755 free_session_slots(ses);
756 kfree(ses);
757 }
758
759 static struct nfsd4_session *alloc_init_session(struct svc_rqst *rqstp, struct nfs4_client *clp, struct nfsd4_create_session *cses)
760 {
761 struct nfsd4_session *new;
762 struct nfsd4_channel_attrs *fchan = &cses->fore_channel;
763 int numslots, slotsize;
764 int status;
765 int idx;
766
767 /*
768 * Note decreasing slot size below client's request may
769 * make it difficult for client to function correctly, whereas
770 * decreasing the number of slots will (just?) affect
771 * performance. When short on memory we therefore prefer to
772 * decrease number of slots instead of their size.
773 */
774 slotsize = nfsd4_sanitize_slot_size(fchan->maxresp_cached);
775 numslots = nfsd4_get_drc_mem(slotsize, fchan->maxreqs);
776 if (numslots < 1)
777 return NULL;
778
779 new = alloc_session(slotsize, numslots);
780 if (!new) {
781 nfsd4_put_drc_mem(slotsize, fchan->maxreqs);
782 return NULL;
783 }
784 init_forechannel_attrs(&new->se_fchannel, fchan, numslots, slotsize);
785
786 new->se_client = clp;
787 gen_sessionid(new);
788
789 INIT_LIST_HEAD(&new->se_conns);
790
791 new->se_cb_seq_nr = 1;
792 new->se_flags = cses->flags;
793 new->se_cb_prog = cses->callback_prog;
794 kref_init(&new->se_ref);
795 idx = hash_sessionid(&new->se_sessionid);
796 spin_lock(&client_lock);
797 list_add(&new->se_hash, &sessionid_hashtbl[idx]);
798 spin_lock(&clp->cl_lock);
799 list_add(&new->se_perclnt, &clp->cl_sessions);
800 spin_unlock(&clp->cl_lock);
801 spin_unlock(&client_lock);
802
803 status = nfsd4_new_conn_from_crses(rqstp, new);
804 /* whoops: benny points out, status is ignored! (err, or bogus) */
805 if (status) {
806 free_session(&new->se_ref);
807 return NULL;
808 }
809 if (cses->flags & SESSION4_BACK_CHAN) {
810 struct sockaddr *sa = svc_addr(rqstp);
811 /*
812 * This is a little silly; with sessions there's no real
813 * use for the callback address. Use the peer address
814 * as a reasonable default for now, but consider fixing
815 * the rpc client not to require an address in the
816 * future:
817 */
818 rpc_copy_addr((struct sockaddr *)&clp->cl_cb_conn.cb_addr, sa);
819 clp->cl_cb_conn.cb_addrlen = svc_addr_len(sa);
820 }
821 nfsd4_probe_callback(clp);
822 return new;
823 }
824
825 /* caller must hold client_lock */
826 static struct nfsd4_session *
827 find_in_sessionid_hashtbl(struct nfs4_sessionid *sessionid)
828 {
829 struct nfsd4_session *elem;
830 int idx;
831
832 dump_sessionid(__func__, sessionid);
833 idx = hash_sessionid(sessionid);
834 /* Search in the appropriate list */
835 list_for_each_entry(elem, &sessionid_hashtbl[idx], se_hash) {
836 if (!memcmp(elem->se_sessionid.data, sessionid->data,
837 NFS4_MAX_SESSIONID_LEN)) {
838 return elem;
839 }
840 }
841
842 dprintk("%s: session not found\n", __func__);
843 return NULL;
844 }
845
846 /* caller must hold client_lock */
847 static void
848 unhash_session(struct nfsd4_session *ses)
849 {
850 list_del(&ses->se_hash);
851 spin_lock(&ses->se_client->cl_lock);
852 list_del(&ses->se_perclnt);
853 spin_unlock(&ses->se_client->cl_lock);
854 }
855
856 /* must be called under the client_lock */
857 static inline void
858 renew_client_locked(struct nfs4_client *clp)
859 {
860 if (is_client_expired(clp)) {
861 dprintk("%s: client (clientid %08x/%08x) already expired\n",
862 __func__,
863 clp->cl_clientid.cl_boot,
864 clp->cl_clientid.cl_id);
865 return;
866 }
867
868 /*
869 * Move client to the end to the LRU list.
870 */
871 dprintk("renewing client (clientid %08x/%08x)\n",
872 clp->cl_clientid.cl_boot,
873 clp->cl_clientid.cl_id);
874 list_move_tail(&clp->cl_lru, &client_lru);
875 clp->cl_time = get_seconds();
876 }
877
878 static inline void
879 renew_client(struct nfs4_client *clp)
880 {
881 spin_lock(&client_lock);
882 renew_client_locked(clp);
883 spin_unlock(&client_lock);
884 }
885
886 /* SETCLIENTID and SETCLIENTID_CONFIRM Helper functions */
887 static int
888 STALE_CLIENTID(clientid_t *clid)
889 {
890 if (clid->cl_boot == boot_time)
891 return 0;
892 dprintk("NFSD stale clientid (%08x/%08x) boot_time %08lx\n",
893 clid->cl_boot, clid->cl_id, boot_time);
894 return 1;
895 }
896
897 /*
898 * XXX Should we use a slab cache ?
899 * This type of memory management is somewhat inefficient, but we use it
900 * anyway since SETCLIENTID is not a common operation.
901 */
902 static struct nfs4_client *alloc_client(struct xdr_netobj name)
903 {
904 struct nfs4_client *clp;
905
906 clp = kzalloc(sizeof(struct nfs4_client), GFP_KERNEL);
907 if (clp == NULL)
908 return NULL;
909 clp->cl_name.data = kmalloc(name.len, GFP_KERNEL);
910 if (clp->cl_name.data == NULL) {
911 kfree(clp);
912 return NULL;
913 }
914 memcpy(clp->cl_name.data, name.data, name.len);
915 clp->cl_name.len = name.len;
916 return clp;
917 }
918
919 static inline void
920 free_client(struct nfs4_client *clp)
921 {
922 while (!list_empty(&clp->cl_sessions)) {
923 struct nfsd4_session *ses;
924 ses = list_entry(clp->cl_sessions.next, struct nfsd4_session,
925 se_perclnt);
926 list_del(&ses->se_perclnt);
927 nfsd4_put_session(ses);
928 }
929 if (clp->cl_cred.cr_group_info)
930 put_group_info(clp->cl_cred.cr_group_info);
931 kfree(clp->cl_principal);
932 kfree(clp->cl_name.data);
933 kfree(clp);
934 }
935
936 void
937 release_session_client(struct nfsd4_session *session)
938 {
939 struct nfs4_client *clp = session->se_client;
940
941 if (!atomic_dec_and_lock(&clp->cl_refcount, &client_lock))
942 return;
943 if (is_client_expired(clp)) {
944 free_client(clp);
945 session->se_client = NULL;
946 } else
947 renew_client_locked(clp);
948 spin_unlock(&client_lock);
949 }
950
951 /* must be called under the client_lock */
952 static inline void
953 unhash_client_locked(struct nfs4_client *clp)
954 {
955 struct nfsd4_session *ses;
956
957 mark_client_expired(clp);
958 list_del(&clp->cl_lru);
959 spin_lock(&clp->cl_lock);
960 list_for_each_entry(ses, &clp->cl_sessions, se_perclnt)
961 list_del_init(&ses->se_hash);
962 spin_unlock(&clp->cl_lock);
963 }
964
965 static void
966 expire_client(struct nfs4_client *clp)
967 {
968 struct nfs4_openowner *oo;
969 struct nfs4_delegation *dp;
970 struct list_head reaplist;
971
972 INIT_LIST_HEAD(&reaplist);
973 spin_lock(&recall_lock);
974 while (!list_empty(&clp->cl_delegations)) {
975 dp = list_entry(clp->cl_delegations.next, struct nfs4_delegation, dl_perclnt);
976 list_del_init(&dp->dl_perclnt);
977 list_move(&dp->dl_recall_lru, &reaplist);
978 }
979 spin_unlock(&recall_lock);
980 while (!list_empty(&reaplist)) {
981 dp = list_entry(reaplist.next, struct nfs4_delegation, dl_recall_lru);
982 list_del_init(&dp->dl_recall_lru);
983 unhash_delegation(dp);
984 }
985 while (!list_empty(&clp->cl_openowners)) {
986 oo = list_entry(clp->cl_openowners.next, struct nfs4_openowner, oo_perclient);
987 release_openowner(oo);
988 }
989 nfsd4_shutdown_callback(clp);
990 if (clp->cl_cb_conn.cb_xprt)
991 svc_xprt_put(clp->cl_cb_conn.cb_xprt);
992 list_del(&clp->cl_idhash);
993 list_del(&clp->cl_strhash);
994 spin_lock(&client_lock);
995 unhash_client_locked(clp);
996 if (atomic_read(&clp->cl_refcount) == 0)
997 free_client(clp);
998 spin_unlock(&client_lock);
999 }
1000
1001 static void copy_verf(struct nfs4_client *target, nfs4_verifier *source)
1002 {
1003 memcpy(target->cl_verifier.data, source->data,
1004 sizeof(target->cl_verifier.data));
1005 }
1006
1007 static void copy_clid(struct nfs4_client *target, struct nfs4_client *source)
1008 {
1009 target->cl_clientid.cl_boot = source->cl_clientid.cl_boot;
1010 target->cl_clientid.cl_id = source->cl_clientid.cl_id;
1011 }
1012
1013 static void copy_cred(struct svc_cred *target, struct svc_cred *source)
1014 {
1015 target->cr_uid = source->cr_uid;
1016 target->cr_gid = source->cr_gid;
1017 target->cr_group_info = source->cr_group_info;
1018 get_group_info(target->cr_group_info);
1019 }
1020
1021 static int same_name(const char *n1, const char *n2)
1022 {
1023 return 0 == memcmp(n1, n2, HEXDIR_LEN);
1024 }
1025
1026 static int
1027 same_verf(nfs4_verifier *v1, nfs4_verifier *v2)
1028 {
1029 return 0 == memcmp(v1->data, v2->data, sizeof(v1->data));
1030 }
1031
1032 static int
1033 same_clid(clientid_t *cl1, clientid_t *cl2)
1034 {
1035 return (cl1->cl_boot == cl2->cl_boot) && (cl1->cl_id == cl2->cl_id);
1036 }
1037
1038 /* XXX what about NGROUP */
1039 static int
1040 same_creds(struct svc_cred *cr1, struct svc_cred *cr2)
1041 {
1042 return cr1->cr_uid == cr2->cr_uid;
1043 }
1044
1045 static void gen_clid(struct nfs4_client *clp)
1046 {
1047 static u32 current_clientid = 1;
1048
1049 clp->cl_clientid.cl_boot = boot_time;
1050 clp->cl_clientid.cl_id = current_clientid++;
1051 }
1052
1053 static void gen_confirm(struct nfs4_client *clp)
1054 {
1055 static u32 i;
1056 u32 *p;
1057
1058 p = (u32 *)clp->cl_confirm.data;
1059 *p++ = get_seconds();
1060 *p++ = i++;
1061 }
1062
1063 static int
1064 same_stateid(stateid_t *id_one, stateid_t *id_two)
1065 {
1066 if (id_one->si_stateownerid != id_two->si_stateownerid)
1067 return 0;
1068 return id_one->si_fileid == id_two->si_fileid;
1069 }
1070
1071 static struct nfs4_stateid *find_stateid(stateid_t *t, int flags)
1072 {
1073 struct nfs4_stateid *s;
1074 unsigned int hashval;
1075
1076 hashval = stateid_hashval(t->si_stateownerid, t->si_fileid);
1077 list_for_each_entry(s, &stateid_hashtbl[hashval], st_hash) {
1078 if (!same_stateid(&s->st_stateid, t))
1079 continue;
1080 if (flags & LOCK_STATE && s->st_type != NFS4_LOCK_STID)
1081 return NULL;
1082 if (flags & OPEN_STATE && s->st_type != NFS4_OPEN_STID)
1083 return NULL;
1084 return s;
1085 }
1086 return NULL;
1087 }
1088
1089 static struct nfs4_client *create_client(struct xdr_netobj name, char *recdir,
1090 struct svc_rqst *rqstp, nfs4_verifier *verf)
1091 {
1092 struct nfs4_client *clp;
1093 struct sockaddr *sa = svc_addr(rqstp);
1094 char *princ;
1095
1096 clp = alloc_client(name);
1097 if (clp == NULL)
1098 return NULL;
1099
1100 INIT_LIST_HEAD(&clp->cl_sessions);
1101
1102 princ = svc_gss_principal(rqstp);
1103 if (princ) {
1104 clp->cl_principal = kstrdup(princ, GFP_KERNEL);
1105 if (clp->cl_principal == NULL) {
1106 free_client(clp);
1107 return NULL;
1108 }
1109 }
1110
1111 memcpy(clp->cl_recdir, recdir, HEXDIR_LEN);
1112 atomic_set(&clp->cl_refcount, 0);
1113 clp->cl_cb_state = NFSD4_CB_UNKNOWN;
1114 INIT_LIST_HEAD(&clp->cl_idhash);
1115 INIT_LIST_HEAD(&clp->cl_strhash);
1116 INIT_LIST_HEAD(&clp->cl_openowners);
1117 INIT_LIST_HEAD(&clp->cl_delegations);
1118 INIT_LIST_HEAD(&clp->cl_lru);
1119 INIT_LIST_HEAD(&clp->cl_callbacks);
1120 spin_lock_init(&clp->cl_lock);
1121 INIT_WORK(&clp->cl_cb_null.cb_work, nfsd4_do_callback_rpc);
1122 clp->cl_time = get_seconds();
1123 clear_bit(0, &clp->cl_cb_slot_busy);
1124 rpc_init_wait_queue(&clp->cl_cb_waitq, "Backchannel slot table");
1125 copy_verf(clp, verf);
1126 rpc_copy_addr((struct sockaddr *) &clp->cl_addr, sa);
1127 clp->cl_flavor = rqstp->rq_flavor;
1128 copy_cred(&clp->cl_cred, &rqstp->rq_cred);
1129 gen_confirm(clp);
1130 clp->cl_cb_session = NULL;
1131 return clp;
1132 }
1133
1134 static int check_name(struct xdr_netobj name)
1135 {
1136 if (name.len == 0)
1137 return 0;
1138 if (name.len > NFS4_OPAQUE_LIMIT) {
1139 dprintk("NFSD: check_name: name too long(%d)!\n", name.len);
1140 return 0;
1141 }
1142 return 1;
1143 }
1144
1145 static void
1146 add_to_unconfirmed(struct nfs4_client *clp, unsigned int strhashval)
1147 {
1148 unsigned int idhashval;
1149
1150 list_add(&clp->cl_strhash, &unconf_str_hashtbl[strhashval]);
1151 idhashval = clientid_hashval(clp->cl_clientid.cl_id);
1152 list_add(&clp->cl_idhash, &unconf_id_hashtbl[idhashval]);
1153 renew_client(clp);
1154 }
1155
1156 static void
1157 move_to_confirmed(struct nfs4_client *clp)
1158 {
1159 unsigned int idhashval = clientid_hashval(clp->cl_clientid.cl_id);
1160 unsigned int strhashval;
1161
1162 dprintk("NFSD: move_to_confirm nfs4_client %p\n", clp);
1163 list_move(&clp->cl_idhash, &conf_id_hashtbl[idhashval]);
1164 strhashval = clientstr_hashval(clp->cl_recdir);
1165 list_move(&clp->cl_strhash, &conf_str_hashtbl[strhashval]);
1166 renew_client(clp);
1167 }
1168
1169 static struct nfs4_client *
1170 find_confirmed_client(clientid_t *clid)
1171 {
1172 struct nfs4_client *clp;
1173 unsigned int idhashval = clientid_hashval(clid->cl_id);
1174
1175 list_for_each_entry(clp, &conf_id_hashtbl[idhashval], cl_idhash) {
1176 if (same_clid(&clp->cl_clientid, clid))
1177 return clp;
1178 }
1179 return NULL;
1180 }
1181
1182 static struct nfs4_client *
1183 find_unconfirmed_client(clientid_t *clid)
1184 {
1185 struct nfs4_client *clp;
1186 unsigned int idhashval = clientid_hashval(clid->cl_id);
1187
1188 list_for_each_entry(clp, &unconf_id_hashtbl[idhashval], cl_idhash) {
1189 if (same_clid(&clp->cl_clientid, clid))
1190 return clp;
1191 }
1192 return NULL;
1193 }
1194
1195 static bool clp_used_exchangeid(struct nfs4_client *clp)
1196 {
1197 return clp->cl_exchange_flags != 0;
1198 }
1199
1200 static struct nfs4_client *
1201 find_confirmed_client_by_str(const char *dname, unsigned int hashval)
1202 {
1203 struct nfs4_client *clp;
1204
1205 list_for_each_entry(clp, &conf_str_hashtbl[hashval], cl_strhash) {
1206 if (same_name(clp->cl_recdir, dname))
1207 return clp;
1208 }
1209 return NULL;
1210 }
1211
1212 static struct nfs4_client *
1213 find_unconfirmed_client_by_str(const char *dname, unsigned int hashval)
1214 {
1215 struct nfs4_client *clp;
1216
1217 list_for_each_entry(clp, &unconf_str_hashtbl[hashval], cl_strhash) {
1218 if (same_name(clp->cl_recdir, dname))
1219 return clp;
1220 }
1221 return NULL;
1222 }
1223
1224 static void rpc_svcaddr2sockaddr(struct sockaddr *sa, unsigned short family, union svc_addr_u *svcaddr)
1225 {
1226 switch (family) {
1227 case AF_INET:
1228 ((struct sockaddr_in *)sa)->sin_family = AF_INET;
1229 ((struct sockaddr_in *)sa)->sin_addr = svcaddr->addr;
1230 return;
1231 case AF_INET6:
1232 ((struct sockaddr_in6 *)sa)->sin6_family = AF_INET6;
1233 ((struct sockaddr_in6 *)sa)->sin6_addr = svcaddr->addr6;
1234 return;
1235 }
1236 }
1237
1238 static void
1239 gen_callback(struct nfs4_client *clp, struct nfsd4_setclientid *se, struct svc_rqst *rqstp)
1240 {
1241 struct nfs4_cb_conn *conn = &clp->cl_cb_conn;
1242 struct sockaddr *sa = svc_addr(rqstp);
1243 u32 scopeid = rpc_get_scope_id(sa);
1244 unsigned short expected_family;
1245
1246 /* Currently, we only support tcp and tcp6 for the callback channel */
1247 if (se->se_callback_netid_len == 3 &&
1248 !memcmp(se->se_callback_netid_val, "tcp", 3))
1249 expected_family = AF_INET;
1250 else if (se->se_callback_netid_len == 4 &&
1251 !memcmp(se->se_callback_netid_val, "tcp6", 4))
1252 expected_family = AF_INET6;
1253 else
1254 goto out_err;
1255
1256 conn->cb_addrlen = rpc_uaddr2sockaddr(se->se_callback_addr_val,
1257 se->se_callback_addr_len,
1258 (struct sockaddr *)&conn->cb_addr,
1259 sizeof(conn->cb_addr));
1260
1261 if (!conn->cb_addrlen || conn->cb_addr.ss_family != expected_family)
1262 goto out_err;
1263
1264 if (conn->cb_addr.ss_family == AF_INET6)
1265 ((struct sockaddr_in6 *)&conn->cb_addr)->sin6_scope_id = scopeid;
1266
1267 conn->cb_prog = se->se_callback_prog;
1268 conn->cb_ident = se->se_callback_ident;
1269 rpc_svcaddr2sockaddr((struct sockaddr *)&conn->cb_saddr, expected_family, &rqstp->rq_daddr);
1270 return;
1271 out_err:
1272 conn->cb_addr.ss_family = AF_UNSPEC;
1273 conn->cb_addrlen = 0;
1274 dprintk(KERN_INFO "NFSD: this client (clientid %08x/%08x) "
1275 "will not receive delegations\n",
1276 clp->cl_clientid.cl_boot, clp->cl_clientid.cl_id);
1277
1278 return;
1279 }
1280
1281 /*
1282 * Cache a reply. nfsd4_check_drc_limit() has bounded the cache size.
1283 */
1284 void
1285 nfsd4_store_cache_entry(struct nfsd4_compoundres *resp)
1286 {
1287 struct nfsd4_slot *slot = resp->cstate.slot;
1288 unsigned int base;
1289
1290 dprintk("--> %s slot %p\n", __func__, slot);
1291
1292 slot->sl_opcnt = resp->opcnt;
1293 slot->sl_status = resp->cstate.status;
1294
1295 if (nfsd4_not_cached(resp)) {
1296 slot->sl_datalen = 0;
1297 return;
1298 }
1299 slot->sl_datalen = (char *)resp->p - (char *)resp->cstate.datap;
1300 base = (char *)resp->cstate.datap -
1301 (char *)resp->xbuf->head[0].iov_base;
1302 if (read_bytes_from_xdr_buf(resp->xbuf, base, slot->sl_data,
1303 slot->sl_datalen))
1304 WARN("%s: sessions DRC could not cache compound\n", __func__);
1305 return;
1306 }
1307
1308 /*
1309 * Encode the replay sequence operation from the slot values.
1310 * If cachethis is FALSE encode the uncached rep error on the next
1311 * operation which sets resp->p and increments resp->opcnt for
1312 * nfs4svc_encode_compoundres.
1313 *
1314 */
1315 static __be32
1316 nfsd4_enc_sequence_replay(struct nfsd4_compoundargs *args,
1317 struct nfsd4_compoundres *resp)
1318 {
1319 struct nfsd4_op *op;
1320 struct nfsd4_slot *slot = resp->cstate.slot;
1321
1322 dprintk("--> %s resp->opcnt %d cachethis %u \n", __func__,
1323 resp->opcnt, resp->cstate.slot->sl_cachethis);
1324
1325 /* Encode the replayed sequence operation */
1326 op = &args->ops[resp->opcnt - 1];
1327 nfsd4_encode_operation(resp, op);
1328
1329 /* Return nfserr_retry_uncached_rep in next operation. */
1330 if (args->opcnt > 1 && slot->sl_cachethis == 0) {
1331 op = &args->ops[resp->opcnt++];
1332 op->status = nfserr_retry_uncached_rep;
1333 nfsd4_encode_operation(resp, op);
1334 }
1335 return op->status;
1336 }
1337
1338 /*
1339 * The sequence operation is not cached because we can use the slot and
1340 * session values.
1341 */
1342 __be32
1343 nfsd4_replay_cache_entry(struct nfsd4_compoundres *resp,
1344 struct nfsd4_sequence *seq)
1345 {
1346 struct nfsd4_slot *slot = resp->cstate.slot;
1347 __be32 status;
1348
1349 dprintk("--> %s slot %p\n", __func__, slot);
1350
1351 /* Either returns 0 or nfserr_retry_uncached */
1352 status = nfsd4_enc_sequence_replay(resp->rqstp->rq_argp, resp);
1353 if (status == nfserr_retry_uncached_rep)
1354 return status;
1355
1356 /* The sequence operation has been encoded, cstate->datap set. */
1357 memcpy(resp->cstate.datap, slot->sl_data, slot->sl_datalen);
1358
1359 resp->opcnt = slot->sl_opcnt;
1360 resp->p = resp->cstate.datap + XDR_QUADLEN(slot->sl_datalen);
1361 status = slot->sl_status;
1362
1363 return status;
1364 }
1365
1366 /*
1367 * Set the exchange_id flags returned by the server.
1368 */
1369 static void
1370 nfsd4_set_ex_flags(struct nfs4_client *new, struct nfsd4_exchange_id *clid)
1371 {
1372 /* pNFS is not supported */
1373 new->cl_exchange_flags |= EXCHGID4_FLAG_USE_NON_PNFS;
1374
1375 /* Referrals are supported, Migration is not. */
1376 new->cl_exchange_flags |= EXCHGID4_FLAG_SUPP_MOVED_REFER;
1377
1378 /* set the wire flags to return to client. */
1379 clid->flags = new->cl_exchange_flags;
1380 }
1381
1382 __be32
1383 nfsd4_exchange_id(struct svc_rqst *rqstp,
1384 struct nfsd4_compound_state *cstate,
1385 struct nfsd4_exchange_id *exid)
1386 {
1387 struct nfs4_client *unconf, *conf, *new;
1388 int status;
1389 unsigned int strhashval;
1390 char dname[HEXDIR_LEN];
1391 char addr_str[INET6_ADDRSTRLEN];
1392 nfs4_verifier verf = exid->verifier;
1393 struct sockaddr *sa = svc_addr(rqstp);
1394
1395 rpc_ntop(sa, addr_str, sizeof(addr_str));
1396 dprintk("%s rqstp=%p exid=%p clname.len=%u clname.data=%p "
1397 "ip_addr=%s flags %x, spa_how %d\n",
1398 __func__, rqstp, exid, exid->clname.len, exid->clname.data,
1399 addr_str, exid->flags, exid->spa_how);
1400
1401 if (!check_name(exid->clname) || (exid->flags & ~EXCHGID4_FLAG_MASK_A))
1402 return nfserr_inval;
1403
1404 /* Currently only support SP4_NONE */
1405 switch (exid->spa_how) {
1406 case SP4_NONE:
1407 break;
1408 case SP4_SSV:
1409 return nfserr_serverfault;
1410 default:
1411 BUG(); /* checked by xdr code */
1412 case SP4_MACH_CRED:
1413 return nfserr_serverfault; /* no excuse :-/ */
1414 }
1415
1416 status = nfs4_make_rec_clidname(dname, &exid->clname);
1417
1418 if (status)
1419 goto error;
1420
1421 strhashval = clientstr_hashval(dname);
1422
1423 nfs4_lock_state();
1424 status = nfs_ok;
1425
1426 conf = find_confirmed_client_by_str(dname, strhashval);
1427 if (conf) {
1428 if (!clp_used_exchangeid(conf)) {
1429 status = nfserr_clid_inuse; /* XXX: ? */
1430 goto out;
1431 }
1432 if (!same_verf(&verf, &conf->cl_verifier)) {
1433 /* 18.35.4 case 8 */
1434 if (exid->flags & EXCHGID4_FLAG_UPD_CONFIRMED_REC_A) {
1435 status = nfserr_not_same;
1436 goto out;
1437 }
1438 /* Client reboot: destroy old state */
1439 expire_client(conf);
1440 goto out_new;
1441 }
1442 if (!same_creds(&conf->cl_cred, &rqstp->rq_cred)) {
1443 /* 18.35.4 case 9 */
1444 if (exid->flags & EXCHGID4_FLAG_UPD_CONFIRMED_REC_A) {
1445 status = nfserr_perm;
1446 goto out;
1447 }
1448 expire_client(conf);
1449 goto out_new;
1450 }
1451 /*
1452 * Set bit when the owner id and verifier map to an already
1453 * confirmed client id (18.35.3).
1454 */
1455 exid->flags |= EXCHGID4_FLAG_CONFIRMED_R;
1456
1457 /*
1458 * Falling into 18.35.4 case 2, possible router replay.
1459 * Leave confirmed record intact and return same result.
1460 */
1461 copy_verf(conf, &verf);
1462 new = conf;
1463 goto out_copy;
1464 }
1465
1466 /* 18.35.4 case 7 */
1467 if (exid->flags & EXCHGID4_FLAG_UPD_CONFIRMED_REC_A) {
1468 status = nfserr_noent;
1469 goto out;
1470 }
1471
1472 unconf = find_unconfirmed_client_by_str(dname, strhashval);
1473 if (unconf) {
1474 /*
1475 * Possible retry or client restart. Per 18.35.4 case 4,
1476 * a new unconfirmed record should be generated regardless
1477 * of whether any properties have changed.
1478 */
1479 expire_client(unconf);
1480 }
1481
1482 out_new:
1483 /* Normal case */
1484 new = create_client(exid->clname, dname, rqstp, &verf);
1485 if (new == NULL) {
1486 status = nfserr_jukebox;
1487 goto out;
1488 }
1489
1490 gen_clid(new);
1491 add_to_unconfirmed(new, strhashval);
1492 out_copy:
1493 exid->clientid.cl_boot = new->cl_clientid.cl_boot;
1494 exid->clientid.cl_id = new->cl_clientid.cl_id;
1495
1496 exid->seqid = 1;
1497 nfsd4_set_ex_flags(new, exid);
1498
1499 dprintk("nfsd4_exchange_id seqid %d flags %x\n",
1500 new->cl_cs_slot.sl_seqid, new->cl_exchange_flags);
1501 status = nfs_ok;
1502
1503 out:
1504 nfs4_unlock_state();
1505 error:
1506 dprintk("nfsd4_exchange_id returns %d\n", ntohl(status));
1507 return status;
1508 }
1509
1510 static int
1511 check_slot_seqid(u32 seqid, u32 slot_seqid, int slot_inuse)
1512 {
1513 dprintk("%s enter. seqid %d slot_seqid %d\n", __func__, seqid,
1514 slot_seqid);
1515
1516 /* The slot is in use, and no response has been sent. */
1517 if (slot_inuse) {
1518 if (seqid == slot_seqid)
1519 return nfserr_jukebox;
1520 else
1521 return nfserr_seq_misordered;
1522 }
1523 /* Normal */
1524 if (likely(seqid == slot_seqid + 1))
1525 return nfs_ok;
1526 /* Replay */
1527 if (seqid == slot_seqid)
1528 return nfserr_replay_cache;
1529 /* Wraparound */
1530 if (seqid == 1 && (slot_seqid + 1) == 0)
1531 return nfs_ok;
1532 /* Misordered replay or misordered new request */
1533 return nfserr_seq_misordered;
1534 }
1535
1536 /*
1537 * Cache the create session result into the create session single DRC
1538 * slot cache by saving the xdr structure. sl_seqid has been set.
1539 * Do this for solo or embedded create session operations.
1540 */
1541 static void
1542 nfsd4_cache_create_session(struct nfsd4_create_session *cr_ses,
1543 struct nfsd4_clid_slot *slot, int nfserr)
1544 {
1545 slot->sl_status = nfserr;
1546 memcpy(&slot->sl_cr_ses, cr_ses, sizeof(*cr_ses));
1547 }
1548
1549 static __be32
1550 nfsd4_replay_create_session(struct nfsd4_create_session *cr_ses,
1551 struct nfsd4_clid_slot *slot)
1552 {
1553 memcpy(cr_ses, &slot->sl_cr_ses, sizeof(*cr_ses));
1554 return slot->sl_status;
1555 }
1556
1557 #define NFSD_MIN_REQ_HDR_SEQ_SZ ((\
1558 2 * 2 + /* credential,verifier: AUTH_NULL, length 0 */ \
1559 1 + /* MIN tag is length with zero, only length */ \
1560 3 + /* version, opcount, opcode */ \
1561 XDR_QUADLEN(NFS4_MAX_SESSIONID_LEN) + \
1562 /* seqid, slotID, slotID, cache */ \
1563 4 ) * sizeof(__be32))
1564
1565 #define NFSD_MIN_RESP_HDR_SEQ_SZ ((\
1566 2 + /* verifier: AUTH_NULL, length 0 */\
1567 1 + /* status */ \
1568 1 + /* MIN tag is length with zero, only length */ \
1569 3 + /* opcount, opcode, opstatus*/ \
1570 XDR_QUADLEN(NFS4_MAX_SESSIONID_LEN) + \
1571 /* seqid, slotID, slotID, slotID, status */ \
1572 5 ) * sizeof(__be32))
1573
1574 static __be32 check_forechannel_attrs(struct nfsd4_channel_attrs fchannel)
1575 {
1576 return fchannel.maxreq_sz < NFSD_MIN_REQ_HDR_SEQ_SZ
1577 || fchannel.maxresp_sz < NFSD_MIN_RESP_HDR_SEQ_SZ;
1578 }
1579
1580 __be32
1581 nfsd4_create_session(struct svc_rqst *rqstp,
1582 struct nfsd4_compound_state *cstate,
1583 struct nfsd4_create_session *cr_ses)
1584 {
1585 struct sockaddr *sa = svc_addr(rqstp);
1586 struct nfs4_client *conf, *unconf;
1587 struct nfsd4_session *new;
1588 struct nfsd4_clid_slot *cs_slot = NULL;
1589 bool confirm_me = false;
1590 int status = 0;
1591
1592 if (cr_ses->flags & ~SESSION4_FLAG_MASK_A)
1593 return nfserr_inval;
1594
1595 nfs4_lock_state();
1596 unconf = find_unconfirmed_client(&cr_ses->clientid);
1597 conf = find_confirmed_client(&cr_ses->clientid);
1598
1599 if (conf) {
1600 cs_slot = &conf->cl_cs_slot;
1601 status = check_slot_seqid(cr_ses->seqid, cs_slot->sl_seqid, 0);
1602 if (status == nfserr_replay_cache) {
1603 dprintk("Got a create_session replay! seqid= %d\n",
1604 cs_slot->sl_seqid);
1605 /* Return the cached reply status */
1606 status = nfsd4_replay_create_session(cr_ses, cs_slot);
1607 goto out;
1608 } else if (cr_ses->seqid != cs_slot->sl_seqid + 1) {
1609 status = nfserr_seq_misordered;
1610 dprintk("Sequence misordered!\n");
1611 dprintk("Expected seqid= %d but got seqid= %d\n",
1612 cs_slot->sl_seqid, cr_ses->seqid);
1613 goto out;
1614 }
1615 } else if (unconf) {
1616 if (!same_creds(&unconf->cl_cred, &rqstp->rq_cred) ||
1617 !rpc_cmp_addr(sa, (struct sockaddr *) &unconf->cl_addr)) {
1618 status = nfserr_clid_inuse;
1619 goto out;
1620 }
1621
1622 cs_slot = &unconf->cl_cs_slot;
1623 status = check_slot_seqid(cr_ses->seqid, cs_slot->sl_seqid, 0);
1624 if (status) {
1625 /* an unconfirmed replay returns misordered */
1626 status = nfserr_seq_misordered;
1627 goto out;
1628 }
1629
1630 confirm_me = true;
1631 conf = unconf;
1632 } else {
1633 status = nfserr_stale_clientid;
1634 goto out;
1635 }
1636
1637 /*
1638 * XXX: we should probably set this at creation time, and check
1639 * for consistent minorversion use throughout:
1640 */
1641 conf->cl_minorversion = 1;
1642 /*
1643 * We do not support RDMA or persistent sessions
1644 */
1645 cr_ses->flags &= ~SESSION4_PERSIST;
1646 cr_ses->flags &= ~SESSION4_RDMA;
1647
1648 status = nfserr_toosmall;
1649 if (check_forechannel_attrs(cr_ses->fore_channel))
1650 goto out;
1651
1652 status = nfserr_jukebox;
1653 new = alloc_init_session(rqstp, conf, cr_ses);
1654 if (!new)
1655 goto out;
1656 status = nfs_ok;
1657 memcpy(cr_ses->sessionid.data, new->se_sessionid.data,
1658 NFS4_MAX_SESSIONID_LEN);
1659 memcpy(&cr_ses->fore_channel, &new->se_fchannel,
1660 sizeof(struct nfsd4_channel_attrs));
1661 cs_slot->sl_seqid++;
1662 cr_ses->seqid = cs_slot->sl_seqid;
1663
1664 /* cache solo and embedded create sessions under the state lock */
1665 nfsd4_cache_create_session(cr_ses, cs_slot, status);
1666 if (confirm_me)
1667 move_to_confirmed(conf);
1668 out:
1669 nfs4_unlock_state();
1670 dprintk("%s returns %d\n", __func__, ntohl(status));
1671 return status;
1672 }
1673
1674 static bool nfsd4_last_compound_op(struct svc_rqst *rqstp)
1675 {
1676 struct nfsd4_compoundres *resp = rqstp->rq_resp;
1677 struct nfsd4_compoundargs *argp = rqstp->rq_argp;
1678
1679 return argp->opcnt == resp->opcnt;
1680 }
1681
1682 static __be32 nfsd4_map_bcts_dir(u32 *dir)
1683 {
1684 switch (*dir) {
1685 case NFS4_CDFC4_FORE:
1686 case NFS4_CDFC4_BACK:
1687 return nfs_ok;
1688 case NFS4_CDFC4_FORE_OR_BOTH:
1689 case NFS4_CDFC4_BACK_OR_BOTH:
1690 *dir = NFS4_CDFC4_BOTH;
1691 return nfs_ok;
1692 };
1693 return nfserr_inval;
1694 }
1695
1696 __be32 nfsd4_bind_conn_to_session(struct svc_rqst *rqstp,
1697 struct nfsd4_compound_state *cstate,
1698 struct nfsd4_bind_conn_to_session *bcts)
1699 {
1700 __be32 status;
1701
1702 if (!nfsd4_last_compound_op(rqstp))
1703 return nfserr_not_only_op;
1704 spin_lock(&client_lock);
1705 cstate->session = find_in_sessionid_hashtbl(&bcts->sessionid);
1706 /* Sorta weird: we only need the refcnt'ing because new_conn acquires
1707 * client_lock iself: */
1708 if (cstate->session) {
1709 nfsd4_get_session(cstate->session);
1710 atomic_inc(&cstate->session->se_client->cl_refcount);
1711 }
1712 spin_unlock(&client_lock);
1713 if (!cstate->session)
1714 return nfserr_badsession;
1715
1716 status = nfsd4_map_bcts_dir(&bcts->dir);
1717 if (!status)
1718 nfsd4_new_conn(rqstp, cstate->session, bcts->dir);
1719 return status;
1720 }
1721
1722 static bool nfsd4_compound_in_session(struct nfsd4_session *session, struct nfs4_sessionid *sid)
1723 {
1724 if (!session)
1725 return 0;
1726 return !memcmp(sid, &session->se_sessionid, sizeof(*sid));
1727 }
1728
1729 __be32
1730 nfsd4_destroy_session(struct svc_rqst *r,
1731 struct nfsd4_compound_state *cstate,
1732 struct nfsd4_destroy_session *sessionid)
1733 {
1734 struct nfsd4_session *ses;
1735 u32 status = nfserr_badsession;
1736
1737 /* Notes:
1738 * - The confirmed nfs4_client->cl_sessionid holds destroyed sessinid
1739 * - Should we return nfserr_back_chan_busy if waiting for
1740 * callbacks on to-be-destroyed session?
1741 * - Do we need to clear any callback info from previous session?
1742 */
1743
1744 if (nfsd4_compound_in_session(cstate->session, &sessionid->sessionid)) {
1745 if (!nfsd4_last_compound_op(r))
1746 return nfserr_not_only_op;
1747 }
1748 dump_sessionid(__func__, &sessionid->sessionid);
1749 spin_lock(&client_lock);
1750 ses = find_in_sessionid_hashtbl(&sessionid->sessionid);
1751 if (!ses) {
1752 spin_unlock(&client_lock);
1753 goto out;
1754 }
1755
1756 unhash_session(ses);
1757 spin_unlock(&client_lock);
1758
1759 nfs4_lock_state();
1760 nfsd4_probe_callback_sync(ses->se_client);
1761 nfs4_unlock_state();
1762
1763 nfsd4_del_conns(ses);
1764
1765 nfsd4_put_session(ses);
1766 status = nfs_ok;
1767 out:
1768 dprintk("%s returns %d\n", __func__, ntohl(status));
1769 return status;
1770 }
1771
1772 static struct nfsd4_conn *__nfsd4_find_conn(struct svc_xprt *xpt, struct nfsd4_session *s)
1773 {
1774 struct nfsd4_conn *c;
1775
1776 list_for_each_entry(c, &s->se_conns, cn_persession) {
1777 if (c->cn_xprt == xpt) {
1778 return c;
1779 }
1780 }
1781 return NULL;
1782 }
1783
1784 static void nfsd4_sequence_check_conn(struct nfsd4_conn *new, struct nfsd4_session *ses)
1785 {
1786 struct nfs4_client *clp = ses->se_client;
1787 struct nfsd4_conn *c;
1788 int ret;
1789
1790 spin_lock(&clp->cl_lock);
1791 c = __nfsd4_find_conn(new->cn_xprt, ses);
1792 if (c) {
1793 spin_unlock(&clp->cl_lock);
1794 free_conn(new);
1795 return;
1796 }
1797 __nfsd4_hash_conn(new, ses);
1798 spin_unlock(&clp->cl_lock);
1799 ret = nfsd4_register_conn(new);
1800 if (ret)
1801 /* oops; xprt is already down: */
1802 nfsd4_conn_lost(&new->cn_xpt_user);
1803 return;
1804 }
1805
1806 static bool nfsd4_session_too_many_ops(struct svc_rqst *rqstp, struct nfsd4_session *session)
1807 {
1808 struct nfsd4_compoundargs *args = rqstp->rq_argp;
1809
1810 return args->opcnt > session->se_fchannel.maxops;
1811 }
1812
1813 static bool nfsd4_request_too_big(struct svc_rqst *rqstp,
1814 struct nfsd4_session *session)
1815 {
1816 struct xdr_buf *xb = &rqstp->rq_arg;
1817
1818 return xb->len > session->se_fchannel.maxreq_sz;
1819 }
1820
1821 __be32
1822 nfsd4_sequence(struct svc_rqst *rqstp,
1823 struct nfsd4_compound_state *cstate,
1824 struct nfsd4_sequence *seq)
1825 {
1826 struct nfsd4_compoundres *resp = rqstp->rq_resp;
1827 struct nfsd4_session *session;
1828 struct nfsd4_slot *slot;
1829 struct nfsd4_conn *conn;
1830 int status;
1831
1832 if (resp->opcnt != 1)
1833 return nfserr_sequence_pos;
1834
1835 /*
1836 * Will be either used or freed by nfsd4_sequence_check_conn
1837 * below.
1838 */
1839 conn = alloc_conn(rqstp, NFS4_CDFC4_FORE);
1840 if (!conn)
1841 return nfserr_jukebox;
1842
1843 spin_lock(&client_lock);
1844 status = nfserr_badsession;
1845 session = find_in_sessionid_hashtbl(&seq->sessionid);
1846 if (!session)
1847 goto out;
1848
1849 status = nfserr_too_many_ops;
1850 if (nfsd4_session_too_many_ops(rqstp, session))
1851 goto out;
1852
1853 status = nfserr_req_too_big;
1854 if (nfsd4_request_too_big(rqstp, session))
1855 goto out;
1856
1857 status = nfserr_badslot;
1858 if (seq->slotid >= session->se_fchannel.maxreqs)
1859 goto out;
1860
1861 slot = session->se_slots[seq->slotid];
1862 dprintk("%s: slotid %d\n", __func__, seq->slotid);
1863
1864 /* We do not negotiate the number of slots yet, so set the
1865 * maxslots to the session maxreqs which is used to encode
1866 * sr_highest_slotid and the sr_target_slot id to maxslots */
1867 seq->maxslots = session->se_fchannel.maxreqs;
1868
1869 status = check_slot_seqid(seq->seqid, slot->sl_seqid, slot->sl_inuse);
1870 if (status == nfserr_replay_cache) {
1871 cstate->slot = slot;
1872 cstate->session = session;
1873 /* Return the cached reply status and set cstate->status
1874 * for nfsd4_proc_compound processing */
1875 status = nfsd4_replay_cache_entry(resp, seq);
1876 cstate->status = nfserr_replay_cache;
1877 goto out;
1878 }
1879 if (status)
1880 goto out;
1881
1882 nfsd4_sequence_check_conn(conn, session);
1883 conn = NULL;
1884
1885 /* Success! bump slot seqid */
1886 slot->sl_inuse = true;
1887 slot->sl_seqid = seq->seqid;
1888 slot->sl_cachethis = seq->cachethis;
1889
1890 cstate->slot = slot;
1891 cstate->session = session;
1892
1893 out:
1894 /* Hold a session reference until done processing the compound. */
1895 if (cstate->session) {
1896 struct nfs4_client *clp = session->se_client;
1897
1898 nfsd4_get_session(cstate->session);
1899 atomic_inc(&clp->cl_refcount);
1900 if (clp->cl_cb_state == NFSD4_CB_DOWN)
1901 seq->status_flags |= SEQ4_STATUS_CB_PATH_DOWN;
1902 }
1903 kfree(conn);
1904 spin_unlock(&client_lock);
1905 dprintk("%s: return %d\n", __func__, ntohl(status));
1906 return status;
1907 }
1908
1909 __be32
1910 nfsd4_reclaim_complete(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate, struct nfsd4_reclaim_complete *rc)
1911 {
1912 int status = 0;
1913
1914 if (rc->rca_one_fs) {
1915 if (!cstate->current_fh.fh_dentry)
1916 return nfserr_nofilehandle;
1917 /*
1918 * We don't take advantage of the rca_one_fs case.
1919 * That's OK, it's optional, we can safely ignore it.
1920 */
1921 return nfs_ok;
1922 }
1923
1924 nfs4_lock_state();
1925 status = nfserr_complete_already;
1926 if (cstate->session->se_client->cl_firststate)
1927 goto out;
1928
1929 status = nfserr_stale_clientid;
1930 if (is_client_expired(cstate->session->se_client))
1931 /*
1932 * The following error isn't really legal.
1933 * But we only get here if the client just explicitly
1934 * destroyed the client. Surely it no longer cares what
1935 * error it gets back on an operation for the dead
1936 * client.
1937 */
1938 goto out;
1939
1940 status = nfs_ok;
1941 nfsd4_create_clid_dir(cstate->session->se_client);
1942 out:
1943 nfs4_unlock_state();
1944 return status;
1945 }
1946
1947 __be32
1948 nfsd4_setclientid(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
1949 struct nfsd4_setclientid *setclid)
1950 {
1951 struct xdr_netobj clname = {
1952 .len = setclid->se_namelen,
1953 .data = setclid->se_name,
1954 };
1955 nfs4_verifier clverifier = setclid->se_verf;
1956 unsigned int strhashval;
1957 struct nfs4_client *conf, *unconf, *new;
1958 __be32 status;
1959 char dname[HEXDIR_LEN];
1960
1961 if (!check_name(clname))
1962 return nfserr_inval;
1963
1964 status = nfs4_make_rec_clidname(dname, &clname);
1965 if (status)
1966 return status;
1967
1968 /*
1969 * XXX The Duplicate Request Cache (DRC) has been checked (??)
1970 * We get here on a DRC miss.
1971 */
1972
1973 strhashval = clientstr_hashval(dname);
1974
1975 nfs4_lock_state();
1976 conf = find_confirmed_client_by_str(dname, strhashval);
1977 if (conf) {
1978 /* RFC 3530 14.2.33 CASE 0: */
1979 status = nfserr_clid_inuse;
1980 if (clp_used_exchangeid(conf))
1981 goto out;
1982 if (!same_creds(&conf->cl_cred, &rqstp->rq_cred)) {
1983 char addr_str[INET6_ADDRSTRLEN];
1984 rpc_ntop((struct sockaddr *) &conf->cl_addr, addr_str,
1985 sizeof(addr_str));
1986 dprintk("NFSD: setclientid: string in use by client "
1987 "at %s\n", addr_str);
1988 goto out;
1989 }
1990 }
1991 /*
1992 * section 14.2.33 of RFC 3530 (under the heading "IMPLEMENTATION")
1993 * has a description of SETCLIENTID request processing consisting
1994 * of 5 bullet points, labeled as CASE0 - CASE4 below.
1995 */
1996 unconf = find_unconfirmed_client_by_str(dname, strhashval);
1997 status = nfserr_jukebox;
1998 if (!conf) {
1999 /*
2000 * RFC 3530 14.2.33 CASE 4:
2001 * placed first, because it is the normal case
2002 */
2003 if (unconf)
2004 expire_client(unconf);
2005 new = create_client(clname, dname, rqstp, &clverifier);
2006 if (new == NULL)
2007 goto out;
2008 gen_clid(new);
2009 } else if (same_verf(&conf->cl_verifier, &clverifier)) {
2010 /*
2011 * RFC 3530 14.2.33 CASE 1:
2012 * probable callback update
2013 */
2014 if (unconf) {
2015 /* Note this is removing unconfirmed {*x***},
2016 * which is stronger than RFC recommended {vxc**}.
2017 * This has the advantage that there is at most
2018 * one {*x***} in either list at any time.
2019 */
2020 expire_client(unconf);
2021 }
2022 new = create_client(clname, dname, rqstp, &clverifier);
2023 if (new == NULL)
2024 goto out;
2025 copy_clid(new, conf);
2026 } else if (!unconf) {
2027 /*
2028 * RFC 3530 14.2.33 CASE 2:
2029 * probable client reboot; state will be removed if
2030 * confirmed.
2031 */
2032 new = create_client(clname, dname, rqstp, &clverifier);
2033 if (new == NULL)
2034 goto out;
2035 gen_clid(new);
2036 } else {
2037 /*
2038 * RFC 3530 14.2.33 CASE 3:
2039 * probable client reboot; state will be removed if
2040 * confirmed.
2041 */
2042 expire_client(unconf);
2043 new = create_client(clname, dname, rqstp, &clverifier);
2044 if (new == NULL)
2045 goto out;
2046 gen_clid(new);
2047 }
2048 /*
2049 * XXX: we should probably set this at creation time, and check
2050 * for consistent minorversion use throughout:
2051 */
2052 new->cl_minorversion = 0;
2053 gen_callback(new, setclid, rqstp);
2054 add_to_unconfirmed(new, strhashval);
2055 setclid->se_clientid.cl_boot = new->cl_clientid.cl_boot;
2056 setclid->se_clientid.cl_id = new->cl_clientid.cl_id;
2057 memcpy(setclid->se_confirm.data, new->cl_confirm.data, sizeof(setclid->se_confirm.data));
2058 status = nfs_ok;
2059 out:
2060 nfs4_unlock_state();
2061 return status;
2062 }
2063
2064
2065 /*
2066 * Section 14.2.34 of RFC 3530 (under the heading "IMPLEMENTATION") has
2067 * a description of SETCLIENTID_CONFIRM request processing consisting of 4
2068 * bullets, labeled as CASE1 - CASE4 below.
2069 */
2070 __be32
2071 nfsd4_setclientid_confirm(struct svc_rqst *rqstp,
2072 struct nfsd4_compound_state *cstate,
2073 struct nfsd4_setclientid_confirm *setclientid_confirm)
2074 {
2075 struct sockaddr *sa = svc_addr(rqstp);
2076 struct nfs4_client *conf, *unconf;
2077 nfs4_verifier confirm = setclientid_confirm->sc_confirm;
2078 clientid_t * clid = &setclientid_confirm->sc_clientid;
2079 __be32 status;
2080
2081 if (STALE_CLIENTID(clid))
2082 return nfserr_stale_clientid;
2083 /*
2084 * XXX The Duplicate Request Cache (DRC) has been checked (??)
2085 * We get here on a DRC miss.
2086 */
2087
2088 nfs4_lock_state();
2089
2090 conf = find_confirmed_client(clid);
2091 unconf = find_unconfirmed_client(clid);
2092
2093 status = nfserr_clid_inuse;
2094 if (conf && !rpc_cmp_addr((struct sockaddr *) &conf->cl_addr, sa))
2095 goto out;
2096 if (unconf && !rpc_cmp_addr((struct sockaddr *) &unconf->cl_addr, sa))
2097 goto out;
2098
2099 /*
2100 * section 14.2.34 of RFC 3530 has a description of
2101 * SETCLIENTID_CONFIRM request processing consisting
2102 * of 4 bullet points, labeled as CASE1 - CASE4 below.
2103 */
2104 if (conf && unconf && same_verf(&confirm, &unconf->cl_confirm)) {
2105 /*
2106 * RFC 3530 14.2.34 CASE 1:
2107 * callback update
2108 */
2109 if (!same_creds(&conf->cl_cred, &unconf->cl_cred))
2110 status = nfserr_clid_inuse;
2111 else {
2112 nfsd4_change_callback(conf, &unconf->cl_cb_conn);
2113 nfsd4_probe_callback(conf);
2114 expire_client(unconf);
2115 status = nfs_ok;
2116
2117 }
2118 } else if (conf && !unconf) {
2119 /*
2120 * RFC 3530 14.2.34 CASE 2:
2121 * probable retransmitted request; play it safe and
2122 * do nothing.
2123 */
2124 if (!same_creds(&conf->cl_cred, &rqstp->rq_cred))
2125 status = nfserr_clid_inuse;
2126 else
2127 status = nfs_ok;
2128 } else if (!conf && unconf
2129 && same_verf(&unconf->cl_confirm, &confirm)) {
2130 /*
2131 * RFC 3530 14.2.34 CASE 3:
2132 * Normal case; new or rebooted client:
2133 */
2134 if (!same_creds(&unconf->cl_cred, &rqstp->rq_cred)) {
2135 status = nfserr_clid_inuse;
2136 } else {
2137 unsigned int hash =
2138 clientstr_hashval(unconf->cl_recdir);
2139 conf = find_confirmed_client_by_str(unconf->cl_recdir,
2140 hash);
2141 if (conf) {
2142 nfsd4_remove_clid_dir(conf);
2143 expire_client(conf);
2144 }
2145 move_to_confirmed(unconf);
2146 conf = unconf;
2147 nfsd4_probe_callback(conf);
2148 status = nfs_ok;
2149 }
2150 } else if ((!conf || (conf && !same_verf(&conf->cl_confirm, &confirm)))
2151 && (!unconf || (unconf && !same_verf(&unconf->cl_confirm,
2152 &confirm)))) {
2153 /*
2154 * RFC 3530 14.2.34 CASE 4:
2155 * Client probably hasn't noticed that we rebooted yet.
2156 */
2157 status = nfserr_stale_clientid;
2158 } else {
2159 /* check that we have hit one of the cases...*/
2160 status = nfserr_clid_inuse;
2161 }
2162 out:
2163 nfs4_unlock_state();
2164 return status;
2165 }
2166
2167 /* OPEN Share state helper functions */
2168 static inline struct nfs4_file *
2169 alloc_init_file(struct inode *ino)
2170 {
2171 struct nfs4_file *fp;
2172 unsigned int hashval = file_hashval(ino);
2173
2174 fp = kmem_cache_alloc(file_slab, GFP_KERNEL);
2175 if (fp) {
2176 atomic_set(&fp->fi_ref, 1);
2177 INIT_LIST_HEAD(&fp->fi_hash);
2178 INIT_LIST_HEAD(&fp->fi_stateids);
2179 INIT_LIST_HEAD(&fp->fi_delegations);
2180 fp->fi_inode = igrab(ino);
2181 fp->fi_id = current_fileid++;
2182 fp->fi_had_conflict = false;
2183 fp->fi_lease = NULL;
2184 memset(fp->fi_fds, 0, sizeof(fp->fi_fds));
2185 memset(fp->fi_access, 0, sizeof(fp->fi_access));
2186 spin_lock(&recall_lock);
2187 list_add(&fp->fi_hash, &file_hashtbl[hashval]);
2188 spin_unlock(&recall_lock);
2189 return fp;
2190 }
2191 return NULL;
2192 }
2193
2194 static void
2195 nfsd4_free_slab(struct kmem_cache **slab)
2196 {
2197 if (*slab == NULL)
2198 return;
2199 kmem_cache_destroy(*slab);
2200 *slab = NULL;
2201 }
2202
2203 void
2204 nfsd4_free_slabs(void)
2205 {
2206 nfsd4_free_slab(&openowner_slab);
2207 nfsd4_free_slab(&lockowner_slab);
2208 nfsd4_free_slab(&file_slab);
2209 nfsd4_free_slab(&stateid_slab);
2210 nfsd4_free_slab(&deleg_slab);
2211 }
2212
2213 static int
2214 nfsd4_init_slabs(void)
2215 {
2216 openowner_slab = kmem_cache_create("nfsd4_openowners",
2217 sizeof(struct nfs4_openowner), 0, 0, NULL);
2218 if (openowner_slab == NULL)
2219 goto out_nomem;
2220 lockowner_slab = kmem_cache_create("nfsd4_lockowners",
2221 sizeof(struct nfs4_openowner), 0, 0, NULL);
2222 if (lockowner_slab == NULL)
2223 goto out_nomem;
2224 file_slab = kmem_cache_create("nfsd4_files",
2225 sizeof(struct nfs4_file), 0, 0, NULL);
2226 if (file_slab == NULL)
2227 goto out_nomem;
2228 stateid_slab = kmem_cache_create("nfsd4_stateids",
2229 sizeof(struct nfs4_stateid), 0, 0, NULL);
2230 if (stateid_slab == NULL)
2231 goto out_nomem;
2232 deleg_slab = kmem_cache_create("nfsd4_delegations",
2233 sizeof(struct nfs4_delegation), 0, 0, NULL);
2234 if (deleg_slab == NULL)
2235 goto out_nomem;
2236 return 0;
2237 out_nomem:
2238 nfsd4_free_slabs();
2239 dprintk("nfsd4: out of memory while initializing nfsv4\n");
2240 return -ENOMEM;
2241 }
2242
2243 void nfs4_free_openowner(struct nfs4_openowner *oo)
2244 {
2245 kfree(oo->oo_owner.so_owner.data);
2246 kmem_cache_free(openowner_slab, oo);
2247 }
2248
2249 void nfs4_free_lockowner(struct nfs4_lockowner *lo)
2250 {
2251 kfree(lo->lo_owner.so_owner.data);
2252 kmem_cache_free(lockowner_slab, lo);
2253 }
2254
2255 static void init_nfs4_replay(struct nfs4_replay *rp)
2256 {
2257 rp->rp_status = nfserr_serverfault;
2258 rp->rp_buflen = 0;
2259 rp->rp_buf = rp->rp_ibuf;
2260 }
2261
2262 static inline void *alloc_stateowner(struct kmem_cache *slab, struct xdr_netobj *owner, struct nfs4_client *clp)
2263 {
2264 struct nfs4_stateowner *sop;
2265
2266 sop = kmem_cache_alloc(slab, GFP_KERNEL);
2267 if (!sop)
2268 return NULL;
2269
2270 sop->so_owner.data = kmemdup(owner->data, owner->len, GFP_KERNEL);
2271 if (!sop->so_owner.data) {
2272 kmem_cache_free(slab, sop);
2273 return NULL;
2274 }
2275 sop->so_owner.len = owner->len;
2276
2277 INIT_LIST_HEAD(&sop->so_stateids);
2278 sop->so_id = current_ownerid++;
2279 sop->so_client = clp;
2280 init_nfs4_replay(&sop->so_replay);
2281 return sop;
2282 }
2283
2284 static void hash_openowner(struct nfs4_openowner *oo, struct nfs4_client *clp, unsigned int strhashval)
2285 {
2286 unsigned int idhashval;
2287
2288 idhashval = open_ownerid_hashval(oo->oo_owner.so_id);
2289 list_add(&oo->oo_owner.so_idhash, &open_ownerid_hashtbl[idhashval]);
2290 list_add(&oo->oo_owner.so_strhash, &open_ownerstr_hashtbl[strhashval]);
2291 list_add(&oo->oo_perclient, &clp->cl_openowners);
2292 }
2293
2294 static struct nfs4_openowner *
2295 alloc_init_open_stateowner(unsigned int strhashval, struct nfs4_client *clp, struct nfsd4_open *open) {
2296 struct nfs4_openowner *oo;
2297
2298 oo = alloc_stateowner(openowner_slab, &open->op_owner, clp);
2299 if (!oo)
2300 return NULL;
2301 oo->oo_owner.so_is_open_owner = 1;
2302 oo->oo_owner.so_seqid = open->op_seqid;
2303 oo->oo_confirmed = 0;
2304 oo->oo_time = 0;
2305 INIT_LIST_HEAD(&oo->oo_close_lru);
2306 hash_openowner(oo, clp, strhashval);
2307 return oo;
2308 }
2309
2310 static inline void
2311 init_stateid(struct nfs4_stateid *stp, struct nfs4_file *fp, struct nfsd4_open *open) {
2312 struct nfs4_openowner *oo = open->op_openowner;
2313 unsigned int hashval = stateid_hashval(oo->oo_owner.so_id, fp->fi_id);
2314
2315 INIT_LIST_HEAD(&stp->st_hash);
2316 INIT_LIST_HEAD(&stp->st_perstateowner);
2317 INIT_LIST_HEAD(&stp->st_lockowners);
2318 INIT_LIST_HEAD(&stp->st_perfile);
2319 list_add(&stp->st_hash, &stateid_hashtbl[hashval]);
2320 list_add(&stp->st_perstateowner, &oo->oo_owner.so_stateids);
2321 list_add(&stp->st_perfile, &fp->fi_stateids);
2322 stp->st_type = NFS4_OPEN_STID;
2323 stp->st_stateowner = &oo->oo_owner;
2324 get_nfs4_file(fp);
2325 stp->st_file = fp;
2326 stp->st_stateid.si_boot = boot_time;
2327 stp->st_stateid.si_stateownerid = oo->oo_owner.so_id;
2328 stp->st_stateid.si_fileid = fp->fi_id;
2329 /* note will be incremented before first return to client: */
2330 stp->st_stateid.si_generation = 0;
2331 stp->st_access_bmap = 0;
2332 stp->st_deny_bmap = 0;
2333 __set_bit(open->op_share_access & ~NFS4_SHARE_WANT_MASK,
2334 &stp->st_access_bmap);
2335 __set_bit(open->op_share_deny, &stp->st_deny_bmap);
2336 stp->st_openstp = NULL;
2337 }
2338
2339 static void
2340 move_to_close_lru(struct nfs4_openowner *oo)
2341 {
2342 dprintk("NFSD: move_to_close_lru nfs4_openowner %p\n", oo);
2343
2344 list_move_tail(&oo->oo_close_lru, &close_lru);
2345 oo->oo_time = get_seconds();
2346 }
2347
2348 static int
2349 same_owner_str(struct nfs4_stateowner *sop, struct xdr_netobj *owner,
2350 clientid_t *clid)
2351 {
2352 return (sop->so_owner.len == owner->len) &&
2353 0 == memcmp(sop->so_owner.data, owner->data, owner->len) &&
2354 (sop->so_client->cl_clientid.cl_id == clid->cl_id);
2355 }
2356
2357 static struct nfs4_openowner *
2358 find_openstateowner_str(unsigned int hashval, struct nfsd4_open *open)
2359 {
2360 struct nfs4_stateowner *so = NULL;
2361
2362 list_for_each_entry(so, &open_ownerstr_hashtbl[hashval], so_strhash) {
2363 if (same_owner_str(so, &open->op_owner, &open->op_clientid))
2364 return container_of(so, struct nfs4_openowner, oo_owner);
2365 }
2366 return NULL;
2367 }
2368
2369 /* search file_hashtbl[] for file */
2370 static struct nfs4_file *
2371 find_file(struct inode *ino)
2372 {
2373 unsigned int hashval = file_hashval(ino);
2374 struct nfs4_file *fp;
2375
2376 spin_lock(&recall_lock);
2377 list_for_each_entry(fp, &file_hashtbl[hashval], fi_hash) {
2378 if (fp->fi_inode == ino) {
2379 get_nfs4_file(fp);
2380 spin_unlock(&recall_lock);
2381 return fp;
2382 }
2383 }
2384 spin_unlock(&recall_lock);
2385 return NULL;
2386 }
2387
2388 static inline int access_valid(u32 x, u32 minorversion)
2389 {
2390 if ((x & NFS4_SHARE_ACCESS_MASK) < NFS4_SHARE_ACCESS_READ)
2391 return 0;
2392 if ((x & NFS4_SHARE_ACCESS_MASK) > NFS4_SHARE_ACCESS_BOTH)
2393 return 0;
2394 x &= ~NFS4_SHARE_ACCESS_MASK;
2395 if (minorversion && x) {
2396 if ((x & NFS4_SHARE_WANT_MASK) > NFS4_SHARE_WANT_CANCEL)
2397 return 0;
2398 if ((x & NFS4_SHARE_WHEN_MASK) > NFS4_SHARE_PUSH_DELEG_WHEN_UNCONTENDED)
2399 return 0;
2400 x &= ~(NFS4_SHARE_WANT_MASK | NFS4_SHARE_WHEN_MASK);
2401 }
2402 if (x)
2403 return 0;
2404 return 1;
2405 }
2406
2407 static inline int deny_valid(u32 x)
2408 {
2409 /* Note: unlike access bits, deny bits may be zero. */
2410 return x <= NFS4_SHARE_DENY_BOTH;
2411 }
2412
2413 /*
2414 * Called to check deny when READ with all zero stateid or
2415 * WRITE with all zero or all one stateid
2416 */
2417 static __be32
2418 nfs4_share_conflict(struct svc_fh *current_fh, unsigned int deny_type)
2419 {
2420 struct inode *ino = current_fh->fh_dentry->d_inode;
2421 struct nfs4_file *fp;
2422 struct nfs4_stateid *stp;
2423 __be32 ret;
2424
2425 dprintk("NFSD: nfs4_share_conflict\n");
2426
2427 fp = find_file(ino);
2428 if (!fp)
2429 return nfs_ok;
2430 ret = nfserr_locked;
2431 /* Search for conflicting share reservations */
2432 list_for_each_entry(stp, &fp->fi_stateids, st_perfile) {
2433 if (test_bit(deny_type, &stp->st_deny_bmap) ||
2434 test_bit(NFS4_SHARE_DENY_BOTH, &stp->st_deny_bmap))
2435 goto out;
2436 }
2437 ret = nfs_ok;
2438 out:
2439 put_nfs4_file(fp);
2440 return ret;
2441 }
2442
2443 static void nfsd_break_one_deleg(struct nfs4_delegation *dp)
2444 {
2445 /* We're assuming the state code never drops its reference
2446 * without first removing the lease. Since we're in this lease
2447 * callback (and since the lease code is serialized by the kernel
2448 * lock) we know the server hasn't removed the lease yet, we know
2449 * it's safe to take a reference: */
2450 atomic_inc(&dp->dl_count);
2451
2452 list_add_tail(&dp->dl_recall_lru, &del_recall_lru);
2453
2454 /* only place dl_time is set. protected by lock_flocks*/
2455 dp->dl_time = get_seconds();
2456
2457 nfsd4_cb_recall(dp);
2458 }
2459
2460 /* Called from break_lease() with lock_flocks() held. */
2461 static void nfsd_break_deleg_cb(struct file_lock *fl)
2462 {
2463 struct nfs4_file *fp = (struct nfs4_file *)fl->fl_owner;
2464 struct nfs4_delegation *dp;
2465
2466 BUG_ON(!fp);
2467 /* We assume break_lease is only called once per lease: */
2468 BUG_ON(fp->fi_had_conflict);
2469 /*
2470 * We don't want the locks code to timeout the lease for us;
2471 * we'll remove it ourself if a delegation isn't returned
2472 * in time:
2473 */
2474 fl->fl_break_time = 0;
2475
2476 spin_lock(&recall_lock);
2477 fp->fi_had_conflict = true;
2478 list_for_each_entry(dp, &fp->fi_delegations, dl_perfile)
2479 nfsd_break_one_deleg(dp);
2480 spin_unlock(&recall_lock);
2481 }
2482
2483 static
2484 int nfsd_change_deleg_cb(struct file_lock **onlist, int arg)
2485 {
2486 if (arg & F_UNLCK)
2487 return lease_modify(onlist, arg);
2488 else
2489 return -EAGAIN;
2490 }
2491
2492 static const struct lock_manager_operations nfsd_lease_mng_ops = {
2493 .lm_break = nfsd_break_deleg_cb,
2494 .lm_change = nfsd_change_deleg_cb,
2495 };
2496
2497 static __be32 nfsd4_check_seqid(struct nfsd4_compound_state *cstate, struct nfs4_stateowner *so, u32 seqid)
2498 {
2499 if (nfsd4_has_session(cstate))
2500 return nfs_ok;
2501 if (seqid == so->so_seqid - 1)
2502 return nfserr_replay_me;
2503 if (seqid == so->so_seqid)
2504 return nfs_ok;
2505 return nfserr_bad_seqid;
2506 }
2507
2508 __be32
2509 nfsd4_process_open1(struct nfsd4_compound_state *cstate,
2510 struct nfsd4_open *open)
2511 {
2512 clientid_t *clientid = &open->op_clientid;
2513 struct nfs4_client *clp = NULL;
2514 unsigned int strhashval;
2515 struct nfs4_openowner *oo = NULL;
2516 __be32 status;
2517
2518 if (!check_name(open->op_owner))
2519 return nfserr_inval;
2520
2521 if (STALE_CLIENTID(&open->op_clientid))
2522 return nfserr_stale_clientid;
2523
2524 strhashval = open_ownerstr_hashval(clientid->cl_id, &open->op_owner);
2525 oo = find_openstateowner_str(strhashval, open);
2526 open->op_openowner = oo;
2527 if (!oo) {
2528 /* Make sure the client's lease hasn't expired. */
2529 clp = find_confirmed_client(clientid);
2530 if (clp == NULL)
2531 return nfserr_expired;
2532 goto renew;
2533 }
2534 if (!oo->oo_confirmed) {
2535 /* Replace unconfirmed owners without checking for replay. */
2536 clp = oo->oo_owner.so_client;
2537 release_openowner(oo);
2538 open->op_openowner = NULL;
2539 goto renew;
2540 }
2541 status = nfsd4_check_seqid(cstate, &oo->oo_owner, open->op_seqid);
2542 if (status)
2543 return status;
2544 renew:
2545 if (open->op_openowner == NULL) {
2546 oo = alloc_init_open_stateowner(strhashval, clp, open);
2547 if (oo == NULL)
2548 return nfserr_jukebox;
2549 open->op_openowner = oo;
2550 }
2551 list_del_init(&oo->oo_close_lru);
2552 renew_client(oo->oo_owner.so_client);
2553 return nfs_ok;
2554 }
2555
2556 static inline __be32
2557 nfs4_check_delegmode(struct nfs4_delegation *dp, int flags)
2558 {
2559 if ((flags & WR_STATE) && (dp->dl_type == NFS4_OPEN_DELEGATE_READ))
2560 return nfserr_openmode;
2561 else
2562 return nfs_ok;
2563 }
2564
2565 static struct nfs4_delegation *
2566 find_delegation_file(struct nfs4_file *fp, stateid_t *stid)
2567 {
2568 struct nfs4_delegation *dp;
2569
2570 spin_lock(&recall_lock);
2571 list_for_each_entry(dp, &fp->fi_delegations, dl_perfile)
2572 if (dp->dl_stateid.si_stateownerid == stid->si_stateownerid) {
2573 spin_unlock(&recall_lock);
2574 return dp;
2575 }
2576 spin_unlock(&recall_lock);
2577 return NULL;
2578 }
2579
2580 static int share_access_to_flags(u32 share_access)
2581 {
2582 share_access &= ~NFS4_SHARE_WANT_MASK;
2583
2584 return share_access == NFS4_SHARE_ACCESS_READ ? RD_STATE : WR_STATE;
2585 }
2586
2587 static __be32
2588 nfs4_check_deleg(struct nfs4_file *fp, struct nfsd4_open *open,
2589 struct nfs4_delegation **dp)
2590 {
2591 int flags;
2592 __be32 status = nfserr_bad_stateid;
2593
2594 *dp = find_delegation_file(fp, &open->op_delegate_stateid);
2595 if (*dp == NULL)
2596 goto out;
2597 flags = share_access_to_flags(open->op_share_access);
2598 status = nfs4_check_delegmode(*dp, flags);
2599 if (status)
2600 *dp = NULL;
2601 out:
2602 if (open->op_claim_type != NFS4_OPEN_CLAIM_DELEGATE_CUR)
2603 return nfs_ok;
2604 if (status)
2605 return status;
2606 open->op_openowner->oo_confirmed = 1;
2607 return nfs_ok;
2608 }
2609
2610 static __be32
2611 nfs4_check_open(struct nfs4_file *fp, struct nfsd4_open *open, struct nfs4_stateid **stpp)
2612 {
2613 struct nfs4_stateid *local;
2614 struct nfs4_openowner *oo = open->op_openowner;
2615
2616 list_for_each_entry(local, &fp->fi_stateids, st_perfile) {
2617 /* ignore lock owners */
2618 if (local->st_stateowner->so_is_open_owner == 0)
2619 continue;
2620 /* remember if we have seen this open owner */
2621 if (local->st_stateowner == &oo->oo_owner)
2622 *stpp = local;
2623 /* check for conflicting share reservations */
2624 if (!test_share(local, open))
2625 return nfserr_share_denied;
2626 }
2627 return nfs_ok;
2628 }
2629
2630 static inline struct nfs4_stateid *
2631 nfs4_alloc_stateid(void)
2632 {
2633 return kmem_cache_alloc(stateid_slab, GFP_KERNEL);
2634 }
2635
2636 static inline int nfs4_access_to_access(u32 nfs4_access)
2637 {
2638 int flags = 0;
2639
2640 if (nfs4_access & NFS4_SHARE_ACCESS_READ)
2641 flags |= NFSD_MAY_READ;
2642 if (nfs4_access & NFS4_SHARE_ACCESS_WRITE)
2643 flags |= NFSD_MAY_WRITE;
2644 return flags;
2645 }
2646
2647 static __be32 nfs4_get_vfs_file(struct svc_rqst *rqstp, struct nfs4_file *fp,
2648 struct svc_fh *cur_fh, struct nfsd4_open *open)
2649 {
2650 __be32 status;
2651 int oflag = nfs4_access_to_omode(open->op_share_access);
2652 int access = nfs4_access_to_access(open->op_share_access);
2653
2654 /* CLAIM_DELEGATE_CUR is used in response to a broken lease;
2655 * allowing it to break the lease and return EAGAIN leaves the
2656 * client unable to make progress in returning the delegation */
2657 if (open->op_claim_type == NFS4_OPEN_CLAIM_DELEGATE_CUR)
2658 access |= NFSD_MAY_NOT_BREAK_LEASE;
2659
2660 if (!fp->fi_fds[oflag]) {
2661 status = nfsd_open(rqstp, cur_fh, S_IFREG, access,
2662 &fp->fi_fds[oflag]);
2663 if (status)
2664 return status;
2665 }
2666 nfs4_file_get_access(fp, oflag);
2667
2668 return nfs_ok;
2669 }
2670
2671 static __be32
2672 nfs4_new_open(struct svc_rqst *rqstp, struct nfs4_stateid **stpp,
2673 struct nfs4_file *fp, struct svc_fh *cur_fh,
2674 struct nfsd4_open *open)
2675 {
2676 struct nfs4_stateid *stp;
2677 __be32 status;
2678
2679 stp = nfs4_alloc_stateid();
2680 if (stp == NULL)
2681 return nfserr_jukebox;
2682
2683 status = nfs4_get_vfs_file(rqstp, fp, cur_fh, open);
2684 if (status) {
2685 kmem_cache_free(stateid_slab, stp);
2686 return status;
2687 }
2688 *stpp = stp;
2689 return 0;
2690 }
2691
2692 static inline __be32
2693 nfsd4_truncate(struct svc_rqst *rqstp, struct svc_fh *fh,
2694 struct nfsd4_open *open)
2695 {
2696 struct iattr iattr = {
2697 .ia_valid = ATTR_SIZE,
2698 .ia_size = 0,
2699 };
2700 if (!open->op_truncate)
2701 return 0;
2702 if (!(open->op_share_access & NFS4_SHARE_ACCESS_WRITE))
2703 return nfserr_inval;
2704 return nfsd_setattr(rqstp, fh, &iattr, 0, (time_t)0);
2705 }
2706
2707 static __be32
2708 nfs4_upgrade_open(struct svc_rqst *rqstp, struct nfs4_file *fp, struct svc_fh *cur_fh, struct nfs4_stateid *stp, struct nfsd4_open *open)
2709 {
2710 u32 op_share_access = open->op_share_access & ~NFS4_SHARE_WANT_MASK;
2711 bool new_access;
2712 __be32 status;
2713
2714 new_access = !test_bit(op_share_access, &stp->st_access_bmap);
2715 if (new_access) {
2716 status = nfs4_get_vfs_file(rqstp, fp, cur_fh, open);
2717 if (status)
2718 return status;
2719 }
2720 status = nfsd4_truncate(rqstp, cur_fh, open);
2721 if (status) {
2722 if (new_access) {
2723 int oflag = nfs4_access_to_omode(op_share_access);
2724 nfs4_file_put_access(fp, oflag);
2725 }
2726 return status;
2727 }
2728 /* remember the open */
2729 __set_bit(op_share_access, &stp->st_access_bmap);
2730 __set_bit(open->op_share_deny, &stp->st_deny_bmap);
2731
2732 return nfs_ok;
2733 }
2734
2735
2736 static void
2737 nfs4_set_claim_prev(struct nfsd4_open *open)
2738 {
2739 open->op_openowner->oo_confirmed = 1;
2740 open->op_openowner->oo_owner.so_client->cl_firststate = 1;
2741 }
2742
2743 /* Should we give out recallable state?: */
2744 static bool nfsd4_cb_channel_good(struct nfs4_client *clp)
2745 {
2746 if (clp->cl_cb_state == NFSD4_CB_UP)
2747 return true;
2748 /*
2749 * In the sessions case, since we don't have to establish a
2750 * separate connection for callbacks, we assume it's OK
2751 * until we hear otherwise:
2752 */
2753 return clp->cl_minorversion && clp->cl_cb_state == NFSD4_CB_UNKNOWN;
2754 }
2755
2756 static struct file_lock *nfs4_alloc_init_lease(struct nfs4_delegation *dp, int flag)
2757 {
2758 struct file_lock *fl;
2759
2760 fl = locks_alloc_lock();
2761 if (!fl)
2762 return NULL;
2763 locks_init_lock(fl);
2764 fl->fl_lmops = &nfsd_lease_mng_ops;
2765 fl->fl_flags = FL_LEASE;
2766 fl->fl_type = flag == NFS4_OPEN_DELEGATE_READ? F_RDLCK: F_WRLCK;
2767 fl->fl_end = OFFSET_MAX;
2768 fl->fl_owner = (fl_owner_t)(dp->dl_file);
2769 fl->fl_pid = current->tgid;
2770 return fl;
2771 }
2772
2773 static int nfs4_setlease(struct nfs4_delegation *dp, int flag)
2774 {
2775 struct nfs4_file *fp = dp->dl_file;
2776 struct file_lock *fl;
2777 int status;
2778
2779 fl = nfs4_alloc_init_lease(dp, flag);
2780 if (!fl)
2781 return -ENOMEM;
2782 fl->fl_file = find_readable_file(fp);
2783 list_add(&dp->dl_perclnt, &dp->dl_client->cl_delegations);
2784 status = vfs_setlease(fl->fl_file, fl->fl_type, &fl);
2785 if (status) {
2786 list_del_init(&dp->dl_perclnt);
2787 locks_free_lock(fl);
2788 return -ENOMEM;
2789 }
2790 fp->fi_lease = fl;
2791 fp->fi_deleg_file = fl->fl_file;
2792 get_file(fp->fi_deleg_file);
2793 atomic_set(&fp->fi_delegees, 1);
2794 list_add(&dp->dl_perfile, &fp->fi_delegations);
2795 return 0;
2796 }
2797
2798 static int nfs4_set_delegation(struct nfs4_delegation *dp, int flag)
2799 {
2800 struct nfs4_file *fp = dp->dl_file;
2801
2802 if (!fp->fi_lease)
2803 return nfs4_setlease(dp, flag);
2804 spin_lock(&recall_lock);
2805 if (fp->fi_had_conflict) {
2806 spin_unlock(&recall_lock);
2807 return -EAGAIN;
2808 }
2809 atomic_inc(&fp->fi_delegees);
2810 list_add(&dp->dl_perfile, &fp->fi_delegations);
2811 spin_unlock(&recall_lock);
2812 list_add(&dp->dl_perclnt, &dp->dl_client->cl_delegations);
2813 return 0;
2814 }
2815
2816 /*
2817 * Attempt to hand out a delegation.
2818 */
2819 static void
2820 nfs4_open_delegation(struct svc_fh *fh, struct nfsd4_open *open, struct nfs4_stateid *stp)
2821 {
2822 struct nfs4_delegation *dp;
2823 struct nfs4_openowner *oo = container_of(stp->st_stateowner, struct nfs4_openowner, oo_owner);
2824 int cb_up;
2825 int status, flag = 0;
2826
2827 cb_up = nfsd4_cb_channel_good(oo->oo_owner.so_client);
2828 flag = NFS4_OPEN_DELEGATE_NONE;
2829 open->op_recall = 0;
2830 switch (open->op_claim_type) {
2831 case NFS4_OPEN_CLAIM_PREVIOUS:
2832 if (!cb_up)
2833 open->op_recall = 1;
2834 flag = open->op_delegate_type;
2835 if (flag == NFS4_OPEN_DELEGATE_NONE)
2836 goto out;
2837 break;
2838 case NFS4_OPEN_CLAIM_NULL:
2839 /* Let's not give out any delegations till everyone's
2840 * had the chance to reclaim theirs.... */
2841 if (locks_in_grace())
2842 goto out;
2843 if (!cb_up || !oo->oo_confirmed)
2844 goto out;
2845 if (open->op_share_access & NFS4_SHARE_ACCESS_WRITE)
2846 flag = NFS4_OPEN_DELEGATE_WRITE;
2847 else
2848 flag = NFS4_OPEN_DELEGATE_READ;
2849 break;
2850 default:
2851 goto out;
2852 }
2853
2854 dp = alloc_init_deleg(oo->oo_owner.so_client, stp, fh, flag);
2855 if (dp == NULL)
2856 goto out_no_deleg;
2857 status = nfs4_set_delegation(dp, flag);
2858 if (status)
2859 goto out_free;
2860
2861 memcpy(&open->op_delegate_stateid, &dp->dl_stateid, sizeof(dp->dl_stateid));
2862
2863 dprintk("NFSD: delegation stateid=" STATEID_FMT "\n",
2864 STATEID_VAL(&dp->dl_stateid));
2865 out:
2866 if (open->op_claim_type == NFS4_OPEN_CLAIM_PREVIOUS
2867 && flag == NFS4_OPEN_DELEGATE_NONE
2868 && open->op_delegate_type != NFS4_OPEN_DELEGATE_NONE)
2869 dprintk("NFSD: WARNING: refusing delegation reclaim\n");
2870 open->op_delegate_type = flag;
2871 return;
2872 out_free:
2873 nfs4_put_delegation(dp);
2874 out_no_deleg:
2875 flag = NFS4_OPEN_DELEGATE_NONE;
2876 goto out;
2877 }
2878
2879 /*
2880 * called with nfs4_lock_state() held.
2881 */
2882 __be32
2883 nfsd4_process_open2(struct svc_rqst *rqstp, struct svc_fh *current_fh, struct nfsd4_open *open)
2884 {
2885 struct nfsd4_compoundres *resp = rqstp->rq_resp;
2886 struct nfs4_file *fp = NULL;
2887 struct inode *ino = current_fh->fh_dentry->d_inode;
2888 struct nfs4_stateid *stp = NULL;
2889 struct nfs4_delegation *dp = NULL;
2890 __be32 status;
2891
2892 status = nfserr_inval;
2893 if (!access_valid(open->op_share_access, resp->cstate.minorversion)
2894 || !deny_valid(open->op_share_deny))
2895 goto out;
2896 /*
2897 * Lookup file; if found, lookup stateid and check open request,
2898 * and check for delegations in the process of being recalled.
2899 * If not found, create the nfs4_file struct
2900 */
2901 fp = find_file(ino);
2902 if (fp) {
2903 if ((status = nfs4_check_open(fp, open, &stp)))
2904 goto out;
2905 status = nfs4_check_deleg(fp, open, &dp);
2906 if (status)
2907 goto out;
2908 } else {
2909 status = nfserr_bad_stateid;
2910 if (open->op_claim_type == NFS4_OPEN_CLAIM_DELEGATE_CUR)
2911 goto out;
2912 status = nfserr_jukebox;
2913 fp = alloc_init_file(ino);
2914 if (fp == NULL)
2915 goto out;
2916 }
2917
2918 /*
2919 * OPEN the file, or upgrade an existing OPEN.
2920 * If truncate fails, the OPEN fails.
2921 */
2922 if (stp) {
2923 /* Stateid was found, this is an OPEN upgrade */
2924 status = nfs4_upgrade_open(rqstp, fp, current_fh, stp, open);
2925 if (status)
2926 goto out;
2927 } else {
2928 status = nfs4_new_open(rqstp, &stp, fp, current_fh, open);
2929 if (status)
2930 goto out;
2931 init_stateid(stp, fp, open);
2932 status = nfsd4_truncate(rqstp, current_fh, open);
2933 if (status) {
2934 release_open_stateid(stp);
2935 goto out;
2936 }
2937 }
2938 update_stateid(&stp->st_stateid);
2939 memcpy(&open->op_stateid, &stp->st_stateid, sizeof(stateid_t));
2940
2941 if (nfsd4_has_session(&resp->cstate))
2942 open->op_openowner->oo_confirmed = 1;
2943
2944 /*
2945 * Attempt to hand out a delegation. No error return, because the
2946 * OPEN succeeds even if we fail.
2947 */
2948 nfs4_open_delegation(current_fh, open, stp);
2949
2950 status = nfs_ok;
2951
2952 dprintk("%s: stateid=" STATEID_FMT "\n", __func__,
2953 STATEID_VAL(&stp->st_stateid));
2954 out:
2955 if (fp)
2956 put_nfs4_file(fp);
2957 if (status == 0 && open->op_claim_type == NFS4_OPEN_CLAIM_PREVIOUS)
2958 nfs4_set_claim_prev(open);
2959 /*
2960 * To finish the open response, we just need to set the rflags.
2961 */
2962 open->op_rflags = NFS4_OPEN_RESULT_LOCKTYPE_POSIX;
2963 if (!open->op_openowner->oo_confirmed &&
2964 !nfsd4_has_session(&resp->cstate))
2965 open->op_rflags |= NFS4_OPEN_RESULT_CONFIRM;
2966
2967 return status;
2968 }
2969
2970 __be32
2971 nfsd4_renew(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
2972 clientid_t *clid)
2973 {
2974 struct nfs4_client *clp;
2975 __be32 status;
2976
2977 nfs4_lock_state();
2978 dprintk("process_renew(%08x/%08x): starting\n",
2979 clid->cl_boot, clid->cl_id);
2980 status = nfserr_stale_clientid;
2981 if (STALE_CLIENTID(clid))
2982 goto out;
2983 clp = find_confirmed_client(clid);
2984 status = nfserr_expired;
2985 if (clp == NULL) {
2986 /* We assume the client took too long to RENEW. */
2987 dprintk("nfsd4_renew: clientid not found!\n");
2988 goto out;
2989 }
2990 renew_client(clp);
2991 status = nfserr_cb_path_down;
2992 if (!list_empty(&clp->cl_delegations)
2993 && clp->cl_cb_state != NFSD4_CB_UP)
2994 goto out;
2995 status = nfs_ok;
2996 out:
2997 nfs4_unlock_state();
2998 return status;
2999 }
3000
3001 static struct lock_manager nfsd4_manager = {
3002 };
3003
3004 static void
3005 nfsd4_end_grace(void)
3006 {
3007 dprintk("NFSD: end of grace period\n");
3008 nfsd4_recdir_purge_old();
3009 locks_end_grace(&nfsd4_manager);
3010 /*
3011 * Now that every NFSv4 client has had the chance to recover and
3012 * to see the (possibly new, possibly shorter) lease time, we
3013 * can safely set the next grace time to the current lease time:
3014 */
3015 nfsd4_grace = nfsd4_lease;
3016 }
3017
3018 static time_t
3019 nfs4_laundromat(void)
3020 {
3021 struct nfs4_client *clp;
3022 struct nfs4_openowner *oo;
3023 struct nfs4_delegation *dp;
3024 struct list_head *pos, *next, reaplist;
3025 time_t cutoff = get_seconds() - nfsd4_lease;
3026 time_t t, clientid_val = nfsd4_lease;
3027 time_t u, test_val = nfsd4_lease;
3028
3029 nfs4_lock_state();
3030
3031 dprintk("NFSD: laundromat service - starting\n");
3032 if (locks_in_grace())
3033 nfsd4_end_grace();
3034 INIT_LIST_HEAD(&reaplist);
3035 spin_lock(&client_lock);
3036 list_for_each_safe(pos, next, &client_lru) {
3037 clp = list_entry(pos, struct nfs4_client, cl_lru);
3038 if (time_after((unsigned long)clp->cl_time, (unsigned long)cutoff)) {
3039 t = clp->cl_time - cutoff;
3040 if (clientid_val > t)
3041 clientid_val = t;
3042 break;
3043 }
3044 if (atomic_read(&clp->cl_refcount)) {
3045 dprintk("NFSD: client in use (clientid %08x)\n",
3046 clp->cl_clientid.cl_id);
3047 continue;
3048 }
3049 unhash_client_locked(clp);
3050 list_add(&clp->cl_lru, &reaplist);
3051 }
3052 spin_unlock(&client_lock);
3053 list_for_each_safe(pos, next, &reaplist) {
3054 clp = list_entry(pos, struct nfs4_client, cl_lru);
3055 dprintk("NFSD: purging unused client (clientid %08x)\n",
3056 clp->cl_clientid.cl_id);
3057 nfsd4_remove_clid_dir(clp);
3058 expire_client(clp);
3059 }
3060 spin_lock(&recall_lock);
3061 list_for_each_safe(pos, next, &del_recall_lru) {
3062 dp = list_entry (pos, struct nfs4_delegation, dl_recall_lru);
3063 if (time_after((unsigned long)dp->dl_time, (unsigned long)cutoff)) {
3064 u = dp->dl_time - cutoff;
3065 if (test_val > u)
3066 test_val = u;
3067 break;
3068 }
3069 list_move(&dp->dl_recall_lru, &reaplist);
3070 }
3071 spin_unlock(&recall_lock);
3072 list_for_each_safe(pos, next, &reaplist) {
3073 dp = list_entry (pos, struct nfs4_delegation, dl_recall_lru);
3074 list_del_init(&dp->dl_recall_lru);
3075 unhash_delegation(dp);
3076 }
3077 test_val = nfsd4_lease;
3078 list_for_each_safe(pos, next, &close_lru) {
3079 oo = container_of(pos, struct nfs4_openowner, oo_close_lru);
3080 if (time_after((unsigned long)oo->oo_time, (unsigned long)cutoff)) {
3081 u = oo->oo_time - cutoff;
3082 if (test_val > u)
3083 test_val = u;
3084 break;
3085 }
3086 dprintk("NFSD: purging unused open stateowner (so_id %d)\n",
3087 oo->oo_owner.so_id);
3088 release_openowner(oo);
3089 }
3090 if (clientid_val < NFSD_LAUNDROMAT_MINTIMEOUT)
3091 clientid_val = NFSD_LAUNDROMAT_MINTIMEOUT;
3092 nfs4_unlock_state();
3093 return clientid_val;
3094 }
3095
3096 static struct workqueue_struct *laundry_wq;
3097 static void laundromat_main(struct work_struct *);
3098 static DECLARE_DELAYED_WORK(laundromat_work, laundromat_main);
3099
3100 static void
3101 laundromat_main(struct work_struct *not_used)
3102 {
3103 time_t t;
3104
3105 t = nfs4_laundromat();
3106 dprintk("NFSD: laundromat_main - sleeping for %ld seconds\n", t);
3107 queue_delayed_work(laundry_wq, &laundromat_work, t*HZ);
3108 }
3109
3110 static struct nfs4_openowner * search_close_lru(u32 st_id)
3111 {
3112 struct nfs4_openowner *local;
3113
3114 list_for_each_entry(local, &close_lru, oo_close_lru) {
3115 if (local->oo_owner.so_id == st_id)
3116 return local;
3117 }
3118 return NULL;
3119 }
3120
3121 static inline int
3122 nfs4_check_fh(struct svc_fh *fhp, struct nfs4_stateid *stp)
3123 {
3124 return fhp->fh_dentry->d_inode != stp->st_file->fi_inode;
3125 }
3126
3127 static int
3128 STALE_STATEID(stateid_t *stateid)
3129 {
3130 if (stateid->si_boot == boot_time)
3131 return 0;
3132 dprintk("NFSD: stale stateid " STATEID_FMT "!\n",
3133 STATEID_VAL(stateid));
3134 return 1;
3135 }
3136
3137 static inline int
3138 access_permit_read(unsigned long access_bmap)
3139 {
3140 return test_bit(NFS4_SHARE_ACCESS_READ, &access_bmap) ||
3141 test_bit(NFS4_SHARE_ACCESS_BOTH, &access_bmap) ||
3142 test_bit(NFS4_SHARE_ACCESS_WRITE, &access_bmap);
3143 }
3144
3145 static inline int
3146 access_permit_write(unsigned long access_bmap)
3147 {
3148 return test_bit(NFS4_SHARE_ACCESS_WRITE, &access_bmap) ||
3149 test_bit(NFS4_SHARE_ACCESS_BOTH, &access_bmap);
3150 }
3151
3152 static
3153 __be32 nfs4_check_openmode(struct nfs4_stateid *stp, int flags)
3154 {
3155 __be32 status = nfserr_openmode;
3156
3157 /* For lock stateid's, we test the parent open, not the lock: */
3158 if (stp->st_openstp)
3159 stp = stp->st_openstp;
3160 if ((flags & WR_STATE) && (!access_permit_write(stp->st_access_bmap)))
3161 goto out;
3162 if ((flags & RD_STATE) && (!access_permit_read(stp->st_access_bmap)))
3163 goto out;
3164 status = nfs_ok;
3165 out:
3166 return status;
3167 }
3168
3169 static inline __be32
3170 check_special_stateids(svc_fh *current_fh, stateid_t *stateid, int flags)
3171 {
3172 if (ONE_STATEID(stateid) && (flags & RD_STATE))
3173 return nfs_ok;
3174 else if (locks_in_grace()) {
3175 /* Answer in remaining cases depends on existence of
3176 * conflicting state; so we must wait out the grace period. */
3177 return nfserr_grace;
3178 } else if (flags & WR_STATE)
3179 return nfs4_share_conflict(current_fh,
3180 NFS4_SHARE_DENY_WRITE);
3181 else /* (flags & RD_STATE) && ZERO_STATEID(stateid) */
3182 return nfs4_share_conflict(current_fh,
3183 NFS4_SHARE_DENY_READ);
3184 }
3185
3186 /*
3187 * Allow READ/WRITE during grace period on recovered state only for files
3188 * that are not able to provide mandatory locking.
3189 */
3190 static inline int
3191 grace_disallows_io(struct inode *inode)
3192 {
3193 return locks_in_grace() && mandatory_lock(inode);
3194 }
3195
3196 /* Returns true iff a is later than b: */
3197 static bool stateid_generation_after(stateid_t *a, stateid_t *b)
3198 {
3199 return (s32)a->si_generation - (s32)b->si_generation > 0;
3200 }
3201
3202 static int check_stateid_generation(stateid_t *in, stateid_t *ref, bool has_session)
3203 {
3204 /*
3205 * When sessions are used the stateid generation number is ignored
3206 * when it is zero.
3207 */
3208 if (has_session && in->si_generation == 0)
3209 return nfs_ok;
3210
3211 if (in->si_generation == ref->si_generation)
3212 return nfs_ok;
3213
3214 /* If the client sends us a stateid from the future, it's buggy: */
3215 if (stateid_generation_after(in, ref))
3216 return nfserr_bad_stateid;
3217 /*
3218 * However, we could see a stateid from the past, even from a
3219 * non-buggy client. For example, if the client sends a lock
3220 * while some IO is outstanding, the lock may bump si_generation
3221 * while the IO is still in flight. The client could avoid that
3222 * situation by waiting for responses on all the IO requests,
3223 * but better performance may result in retrying IO that
3224 * receives an old_stateid error if requests are rarely
3225 * reordered in flight:
3226 */
3227 return nfserr_old_stateid;
3228 }
3229
3230 static int is_delegation_stateid(stateid_t *stateid)
3231 {
3232 return stateid->si_fileid == 0;
3233 }
3234
3235 __be32 nfs4_validate_stateid(stateid_t *stateid, bool has_session)
3236 {
3237 struct nfs4_stateid *stp = NULL;
3238 __be32 status = nfserr_stale_stateid;
3239
3240 if (STALE_STATEID(stateid))
3241 goto out;
3242
3243 status = nfserr_expired;
3244 stp = find_stateid(stateid, 0);
3245 if (!stp)
3246 goto out;
3247 status = nfserr_bad_stateid;
3248
3249 if (stp->st_stateowner->so_is_open_owner
3250 && !openowner(stp->st_stateowner)->oo_confirmed)
3251 goto out;
3252
3253 status = check_stateid_generation(stateid, &stp->st_stateid, has_session);
3254 if (status)
3255 goto out;
3256
3257 status = nfs_ok;
3258 out:
3259 return status;
3260 }
3261
3262 /*
3263 * Checks for stateid operations
3264 */
3265 __be32
3266 nfs4_preprocess_stateid_op(struct nfsd4_compound_state *cstate,
3267 stateid_t *stateid, int flags, struct file **filpp)
3268 {
3269 struct nfs4_stateid *stp = NULL;
3270 struct nfs4_delegation *dp = NULL;
3271 struct svc_fh *current_fh = &cstate->current_fh;
3272 struct inode *ino = current_fh->fh_dentry->d_inode;
3273 __be32 status;
3274
3275 if (filpp)
3276 *filpp = NULL;
3277
3278 if (grace_disallows_io(ino))
3279 return nfserr_grace;
3280
3281 if (ZERO_STATEID(stateid) || ONE_STATEID(stateid))
3282 return check_special_stateids(current_fh, stateid, flags);
3283
3284 status = nfserr_stale_stateid;
3285 if (STALE_STATEID(stateid))
3286 goto out;
3287
3288 /*
3289 * We assume that any stateid that has the current boot time,
3290 * but that we can't find, is expired:
3291 */
3292 status = nfserr_expired;
3293 if (is_delegation_stateid(stateid)) {
3294 dp = find_delegation_stateid(ino, stateid);
3295 if (!dp)
3296 goto out;
3297 status = check_stateid_generation(stateid, &dp->dl_stateid, nfsd4_has_session(cstate));
3298 if (status)
3299 goto out;
3300 status = nfs4_check_delegmode(dp, flags);
3301 if (status)
3302 goto out;
3303 renew_client(dp->dl_client);
3304 if (filpp) {
3305 *filpp = dp->dl_file->fi_deleg_file;
3306 BUG_ON(!*filpp);
3307 }
3308 } else { /* open or lock stateid */
3309 stp = find_stateid(stateid, flags);
3310 if (!stp)
3311 goto out;
3312 status = nfserr_bad_stateid;
3313 if (nfs4_check_fh(current_fh, stp))
3314 goto out;
3315 if (stp->st_stateowner->so_is_open_owner
3316 && !openowner(stp->st_stateowner)->oo_confirmed)
3317 goto out;
3318 status = check_stateid_generation(stateid, &stp->st_stateid,
3319 nfsd4_has_session(cstate));
3320 if (status)
3321 goto out;
3322 status = nfs4_check_openmode(stp, flags);
3323 if (status)
3324 goto out;
3325 renew_client(stp->st_stateowner->so_client);
3326 if (filpp) {
3327 if (flags & RD_STATE)
3328 *filpp = find_readable_file(stp->st_file);
3329 else
3330 *filpp = find_writeable_file(stp->st_file);
3331 }
3332 }
3333 status = nfs_ok;
3334 out:
3335 return status;
3336 }
3337
3338 static __be32
3339 nfsd4_free_delegation_stateid(stateid_t *stateid)
3340 {
3341 struct nfs4_delegation *dp = search_for_delegation(stateid);
3342 if (dp)
3343 return nfserr_locks_held;
3344 return nfserr_bad_stateid;
3345 }
3346
3347 static __be32
3348 nfsd4_free_lock_stateid(struct nfs4_stateid *stp)
3349 {
3350 if (check_for_locks(stp->st_file, lockowner(stp->st_stateowner)))
3351 return nfserr_locks_held;
3352 release_lock_stateid(stp);
3353 return nfs_ok;
3354 }
3355
3356 /*
3357 * Test if the stateid is valid
3358 */
3359 __be32
3360 nfsd4_test_stateid(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
3361 struct nfsd4_test_stateid *test_stateid)
3362 {
3363 test_stateid->ts_has_session = nfsd4_has_session(cstate);
3364 return nfs_ok;
3365 }
3366
3367 /*
3368 * Free a state id
3369 */
3370 __be32
3371 nfsd4_free_stateid(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
3372 struct nfsd4_free_stateid *free_stateid)
3373 {
3374 stateid_t *stateid = &free_stateid->fr_stateid;
3375 struct nfs4_stateid *stp;
3376 __be32 ret;
3377
3378 nfs4_lock_state();
3379 if (is_delegation_stateid(stateid)) {
3380 ret = nfsd4_free_delegation_stateid(stateid);
3381 goto out;
3382 }
3383
3384 stp = find_stateid(stateid, 0);
3385 if (!stp) {
3386 ret = nfserr_bad_stateid;
3387 goto out;
3388 }
3389 ret = check_stateid_generation(stateid, &stp->st_stateid, 1);
3390 if (ret)
3391 goto out;
3392
3393 if (stp->st_type == NFS4_OPEN_STID) {
3394 ret = nfserr_locks_held;
3395 goto out;
3396 } else {
3397 ret = nfsd4_free_lock_stateid(stp);
3398 goto out;
3399 }
3400
3401 out:
3402 nfs4_unlock_state();
3403 return ret;
3404 }
3405
3406 static inline int
3407 setlkflg (int type)
3408 {
3409 return (type == NFS4_READW_LT || type == NFS4_READ_LT) ?
3410 RD_STATE : WR_STATE;
3411 }
3412
3413 /*
3414 * Checks for sequence id mutating operations.
3415 */
3416 static __be32
3417 nfs4_preprocess_seqid_op(struct nfsd4_compound_state *cstate, u32 seqid,
3418 stateid_t *stateid, int flags,
3419 struct nfs4_stateid **stpp)
3420 {
3421 struct nfs4_stateowner *sop;
3422 struct svc_fh *current_fh = &cstate->current_fh;
3423 __be32 status;
3424
3425 dprintk("NFSD: %s: seqid=%d stateid = " STATEID_FMT "\n", __func__,
3426 seqid, STATEID_VAL(stateid));
3427
3428 *stpp = NULL;
3429
3430 if (ZERO_STATEID(stateid) || ONE_STATEID(stateid)) {
3431 dprintk("NFSD: preprocess_seqid_op: magic stateid!\n");
3432 return nfserr_bad_stateid;
3433 }
3434
3435 if (STALE_STATEID(stateid))
3436 return nfserr_stale_stateid;
3437
3438 /*
3439 * We return BAD_STATEID if filehandle doesn't match stateid,
3440 * the confirmed flag is incorrecly set, or the generation
3441 * number is incorrect.
3442 */
3443 *stpp = find_stateid(stateid, flags);
3444 if (*stpp == NULL)
3445 return nfserr_expired;
3446
3447 sop = (*stpp)->st_stateowner;
3448 cstate->replay_owner = sop;
3449
3450 if (nfs4_check_fh(current_fh, *stpp)) {
3451 dprintk("NFSD: preprocess_seqid_op: fh-stateid mismatch!\n");
3452 return nfserr_bad_stateid;
3453 }
3454
3455 status = nfsd4_check_seqid(cstate, sop, seqid);
3456 if (status)
3457 return status;
3458
3459 if (sop->so_is_open_owner && !openowner(sop)->oo_confirmed
3460 && !(flags & CONFIRM)) {
3461 dprintk("NFSD: preprocess_seqid_op: stateowner not"
3462 " confirmed yet!\n");
3463 return nfserr_bad_stateid;
3464 }
3465 status = check_stateid_generation(stateid, &(*stpp)->st_stateid, nfsd4_has_session(cstate));
3466 if (status)
3467 return status;
3468 renew_client(sop->so_client);
3469 return nfs_ok;
3470 }
3471
3472 __be32
3473 nfsd4_open_confirm(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
3474 struct nfsd4_open_confirm *oc)
3475 {
3476 __be32 status;
3477 struct nfs4_openowner *oo;
3478 struct nfs4_stateid *stp;
3479
3480 dprintk("NFSD: nfsd4_open_confirm on file %.*s\n",
3481 (int)cstate->current_fh.fh_dentry->d_name.len,
3482 cstate->current_fh.fh_dentry->d_name.name);
3483
3484 status = fh_verify(rqstp, &cstate->current_fh, S_IFREG, 0);
3485 if (status)
3486 return status;
3487
3488 nfs4_lock_state();
3489
3490 status = nfs4_preprocess_seqid_op(cstate,
3491 oc->oc_seqid, &oc->oc_req_stateid,
3492 CONFIRM | OPEN_STATE, &stp);
3493 if (status)
3494 goto out;
3495 oo = openowner(stp->st_stateowner);
3496 status = nfserr_bad_stateid;
3497 if (oo->oo_confirmed)
3498 goto out;
3499 oo->oo_confirmed = 1;
3500 update_stateid(&stp->st_stateid);
3501 memcpy(&oc->oc_resp_stateid, &stp->st_stateid, sizeof(stateid_t));
3502 dprintk("NFSD: %s: success, seqid=%d stateid=" STATEID_FMT "\n",
3503 __func__, oc->oc_seqid, STATEID_VAL(&stp->st_stateid));
3504
3505 nfsd4_create_clid_dir(oo->oo_owner.so_client);
3506 status = nfs_ok;
3507 out:
3508 if (!cstate->replay_owner)
3509 nfs4_unlock_state();
3510 return status;
3511 }
3512
3513 static inline void nfs4_file_downgrade(struct nfs4_stateid *stp, unsigned int to_access)
3514 {
3515 int i;
3516
3517 for (i = 1; i < 4; i++) {
3518 if (test_bit(i, &stp->st_access_bmap) && !(i & to_access)) {
3519 nfs4_file_put_access(stp->st_file, i);
3520 __clear_bit(i, &stp->st_access_bmap);
3521 }
3522 }
3523 }
3524
3525 static void
3526 reset_union_bmap_deny(unsigned long deny, unsigned long *bmap)
3527 {
3528 int i;
3529 for (i = 0; i < 4; i++) {
3530 if ((i & deny) != i)
3531 __clear_bit(i, bmap);
3532 }
3533 }
3534
3535 __be32
3536 nfsd4_open_downgrade(struct svc_rqst *rqstp,
3537 struct nfsd4_compound_state *cstate,
3538 struct nfsd4_open_downgrade *od)
3539 {
3540 __be32 status;
3541 struct nfs4_stateid *stp;
3542
3543 dprintk("NFSD: nfsd4_open_downgrade on file %.*s\n",
3544 (int)cstate->current_fh.fh_dentry->d_name.len,
3545 cstate->current_fh.fh_dentry->d_name.name);
3546
3547 if (!access_valid(od->od_share_access, cstate->minorversion)
3548 || !deny_valid(od->od_share_deny))
3549 return nfserr_inval;
3550
3551 nfs4_lock_state();
3552 status = nfs4_preprocess_seqid_op(cstate, od->od_seqid,
3553 &od->od_stateid, OPEN_STATE, &stp);
3554 if (status)
3555 goto out;
3556 status = nfserr_inval;
3557 if (!test_bit(od->od_share_access, &stp->st_access_bmap)) {
3558 dprintk("NFSD:access not a subset current bitmap: 0x%lx, input access=%08x\n",
3559 stp->st_access_bmap, od->od_share_access);
3560 goto out;
3561 }
3562 if (!test_bit(od->od_share_deny, &stp->st_deny_bmap)) {
3563 dprintk("NFSD:deny not a subset current bitmap: 0x%lx, input deny=%08x\n",
3564 stp->st_deny_bmap, od->od_share_deny);
3565 goto out;
3566 }
3567 nfs4_file_downgrade(stp, od->od_share_access);
3568
3569 reset_union_bmap_deny(od->od_share_deny, &stp->st_deny_bmap);
3570
3571 update_stateid(&stp->st_stateid);
3572 memcpy(&od->od_stateid, &stp->st_stateid, sizeof(stateid_t));
3573 status = nfs_ok;
3574 out:
3575 if (!cstate->replay_owner)
3576 nfs4_unlock_state();
3577 return status;
3578 }
3579
3580 /*
3581 * nfs4_unlock_state() called after encode
3582 */
3583 __be32
3584 nfsd4_close(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
3585 struct nfsd4_close *close)
3586 {
3587 __be32 status;
3588 struct nfs4_openowner *oo;
3589 struct nfs4_stateid *stp;
3590
3591 dprintk("NFSD: nfsd4_close on file %.*s\n",
3592 (int)cstate->current_fh.fh_dentry->d_name.len,
3593 cstate->current_fh.fh_dentry->d_name.name);
3594
3595 nfs4_lock_state();
3596 /* check close_lru for replay */
3597 status = nfs4_preprocess_seqid_op(cstate, close->cl_seqid,
3598 &close->cl_stateid,
3599 OPEN_STATE, &stp);
3600 if (stp == NULL && status == nfserr_expired) {
3601 /*
3602 * Also, we should make sure this isn't just the result of
3603 * a replayed close:
3604 */
3605 oo = search_close_lru(close->cl_stateid.si_stateownerid);
3606 /* It's not stale; let's assume it's expired: */
3607 if (oo == NULL)
3608 goto out;
3609 cstate->replay_owner = &oo->oo_owner;
3610 status = nfsd4_check_seqid(cstate, &oo->oo_owner, close->cl_seqid);
3611 if (status)
3612 goto out;
3613 status = nfserr_bad_seqid;
3614 }
3615 if (status)
3616 goto out;
3617 oo = openowner(stp->st_stateowner);
3618 status = nfs_ok;
3619 update_stateid(&stp->st_stateid);
3620 memcpy(&close->cl_stateid, &stp->st_stateid, sizeof(stateid_t));
3621
3622 /* release_stateid() calls nfsd_close() if needed */
3623 release_open_stateid(stp);
3624
3625 /* place unused nfs4_stateowners on so_close_lru list to be
3626 * released by the laundromat service after the lease period
3627 * to enable us to handle CLOSE replay
3628 */
3629 if (list_empty(&oo->oo_owner.so_stateids))
3630 move_to_close_lru(oo);
3631 out:
3632 if (!cstate->replay_owner)
3633 nfs4_unlock_state();
3634 return status;
3635 }
3636
3637 __be32
3638 nfsd4_delegreturn(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
3639 struct nfsd4_delegreturn *dr)
3640 {
3641 struct nfs4_delegation *dp;
3642 stateid_t *stateid = &dr->dr_stateid;
3643 struct inode *inode;
3644 __be32 status;
3645
3646 if ((status = fh_verify(rqstp, &cstate->current_fh, S_IFREG, 0)))
3647 return status;
3648 inode = cstate->current_fh.fh_dentry->d_inode;
3649
3650 nfs4_lock_state();
3651 status = nfserr_bad_stateid;
3652 if (ZERO_STATEID(stateid) || ONE_STATEID(stateid))
3653 goto out;
3654 status = nfserr_stale_stateid;
3655 if (STALE_STATEID(stateid))
3656 goto out;
3657 status = nfserr_bad_stateid;
3658 if (!is_delegation_stateid(stateid))
3659 goto out;
3660 status = nfserr_expired;
3661 dp = find_delegation_stateid(inode, stateid);
3662 if (!dp)
3663 goto out;
3664 status = check_stateid_generation(stateid, &dp->dl_stateid, nfsd4_has_session(cstate));
3665 if (status)
3666 goto out;
3667 renew_client(dp->dl_client);
3668
3669 unhash_delegation(dp);
3670 out:
3671 nfs4_unlock_state();
3672
3673 return status;
3674 }
3675
3676
3677 /*
3678 * Lock owner state (byte-range locks)
3679 */
3680 #define LOFF_OVERFLOW(start, len) ((u64)(len) > ~(u64)(start))
3681 #define LOCK_HASH_BITS 8
3682 #define LOCK_HASH_SIZE (1 << LOCK_HASH_BITS)
3683 #define LOCK_HASH_MASK (LOCK_HASH_SIZE - 1)
3684
3685 static inline u64
3686 end_offset(u64 start, u64 len)
3687 {
3688 u64 end;
3689
3690 end = start + len;
3691 return end >= start ? end: NFS4_MAX_UINT64;
3692 }
3693
3694 /* last octet in a range */
3695 static inline u64
3696 last_byte_offset(u64 start, u64 len)
3697 {
3698 u64 end;
3699
3700 BUG_ON(!len);
3701 end = start + len;
3702 return end > start ? end - 1: NFS4_MAX_UINT64;
3703 }
3704
3705 static unsigned int lockownerid_hashval(u32 id)
3706 {
3707 return id & LOCK_HASH_MASK;
3708 }
3709
3710 static inline unsigned int
3711 lock_ownerstr_hashval(struct inode *inode, u32 cl_id,
3712 struct xdr_netobj *ownername)
3713 {
3714 return (file_hashval(inode) + cl_id
3715 + opaque_hashval(ownername->data, ownername->len))
3716 & LOCK_HASH_MASK;
3717 }
3718
3719 static struct list_head lock_ownerid_hashtbl[LOCK_HASH_SIZE];
3720 static struct list_head lock_ownerstr_hashtbl[LOCK_HASH_SIZE];
3721
3722 static struct nfs4_delegation *
3723 search_for_delegation(stateid_t *stid)
3724 {
3725 struct nfs4_file *fp;
3726 struct nfs4_delegation *dp;
3727 struct list_head *pos;
3728 int i;
3729
3730 for (i = 0; i < FILE_HASH_SIZE; i++) {
3731 list_for_each_entry(fp, &file_hashtbl[i], fi_hash) {
3732 list_for_each(pos, &fp->fi_delegations) {
3733 dp = list_entry(pos, struct nfs4_delegation, dl_perfile);
3734 if (same_stateid(&dp->dl_stateid, stid))
3735 return dp;
3736 }
3737 }
3738 }
3739 return NULL;
3740 }
3741
3742 static struct nfs4_delegation *
3743 find_delegation_stateid(struct inode *ino, stateid_t *stid)
3744 {
3745 struct nfs4_file *fp;
3746 struct nfs4_delegation *dl;
3747
3748 dprintk("NFSD: %s: stateid=" STATEID_FMT "\n", __func__,
3749 STATEID_VAL(stid));
3750
3751 fp = find_file(ino);
3752 if (!fp)
3753 return NULL;
3754 dl = find_delegation_file(fp, stid);
3755 put_nfs4_file(fp);
3756 return dl;
3757 }
3758
3759 /*
3760 * TODO: Linux file offsets are _signed_ 64-bit quantities, which means that
3761 * we can't properly handle lock requests that go beyond the (2^63 - 1)-th
3762 * byte, because of sign extension problems. Since NFSv4 calls for 64-bit
3763 * locking, this prevents us from being completely protocol-compliant. The
3764 * real solution to this problem is to start using unsigned file offsets in
3765 * the VFS, but this is a very deep change!
3766 */
3767 static inline void
3768 nfs4_transform_lock_offset(struct file_lock *lock)
3769 {
3770 if (lock->fl_start < 0)
3771 lock->fl_start = OFFSET_MAX;
3772 if (lock->fl_end < 0)
3773 lock->fl_end = OFFSET_MAX;
3774 }
3775
3776 /* Hack!: For now, we're defining this just so we can use a pointer to it
3777 * as a unique cookie to identify our (NFSv4's) posix locks. */
3778 static const struct lock_manager_operations nfsd_posix_mng_ops = {
3779 };
3780
3781 static inline void
3782 nfs4_set_lock_denied(struct file_lock *fl, struct nfsd4_lock_denied *deny)
3783 {
3784 struct nfs4_lockowner *lo;
3785
3786 if (fl->fl_lmops == &nfsd_posix_mng_ops) {
3787 lo = (struct nfs4_lockowner *) fl->fl_owner;
3788 deny->ld_owner.data = kmemdup(lo->lo_owner.so_owner.data,
3789 lo->lo_owner.so_owner.len, GFP_KERNEL);
3790 if (!deny->ld_owner.data)
3791 /* We just don't care that much */
3792 goto nevermind;
3793 deny->ld_owner.len = lo->lo_owner.so_owner.len;
3794 deny->ld_clientid = lo->lo_owner.so_client->cl_clientid;
3795 } else {
3796 nevermind:
3797 deny->ld_owner.len = 0;
3798 deny->ld_owner.data = NULL;
3799 deny->ld_clientid.cl_boot = 0;
3800 deny->ld_clientid.cl_id = 0;
3801 }
3802 deny->ld_start = fl->fl_start;
3803 deny->ld_length = NFS4_MAX_UINT64;
3804 if (fl->fl_end != NFS4_MAX_UINT64)
3805 deny->ld_length = fl->fl_end - fl->fl_start + 1;
3806 deny->ld_type = NFS4_READ_LT;
3807 if (fl->fl_type != F_RDLCK)
3808 deny->ld_type = NFS4_WRITE_LT;
3809 }
3810
3811 static struct nfs4_lockowner *
3812 find_lockowner_str(struct inode *inode, clientid_t *clid,
3813 struct xdr_netobj *owner)
3814 {
3815 unsigned int hashval = lock_ownerstr_hashval(inode, clid->cl_id, owner);
3816 struct nfs4_stateowner *op;
3817
3818 list_for_each_entry(op, &lock_ownerstr_hashtbl[hashval], so_strhash) {
3819 if (same_owner_str(op, owner, clid))
3820 return lockowner(op);
3821 }
3822 return NULL;
3823 }
3824
3825 static void hash_lockowner(struct nfs4_lockowner *lo, unsigned int strhashval, struct nfs4_client *clp, struct nfs4_stateid *open_stp)
3826 {
3827 unsigned int idhashval;
3828
3829 idhashval = lockownerid_hashval(lo->lo_owner.so_id);
3830 list_add(&lo->lo_owner.so_idhash, &lock_ownerid_hashtbl[idhashval]);
3831 list_add(&lo->lo_owner.so_strhash, &lock_ownerstr_hashtbl[strhashval]);
3832 list_add(&lo->lo_perstateid, &open_stp->st_lockowners);
3833 }
3834
3835 /*
3836 * Alloc a lock owner structure.
3837 * Called in nfsd4_lock - therefore, OPEN and OPEN_CONFIRM (if needed) has
3838 * occurred.
3839 *
3840 * strhashval = lock_ownerstr_hashval
3841 */
3842
3843 static struct nfs4_lockowner *
3844 alloc_init_lock_stateowner(unsigned int strhashval, struct nfs4_client *clp, struct nfs4_stateid *open_stp, struct nfsd4_lock *lock) {
3845 struct nfs4_lockowner *lo;
3846
3847 lo = alloc_stateowner(lockowner_slab, &lock->lk_new_owner, clp);
3848 if (!lo)
3849 return NULL;
3850 INIT_LIST_HEAD(&lo->lo_owner.so_stateids);
3851 lo->lo_owner.so_is_open_owner = 0;
3852 /* It is the openowner seqid that will be incremented in encode in the
3853 * case of new lockowners; so increment the lock seqid manually: */
3854 lo->lo_owner.so_seqid = lock->lk_new_lock_seqid + 1;
3855 hash_lockowner(lo, strhashval, clp, open_stp);
3856 return lo;
3857 }
3858
3859 static struct nfs4_stateid *
3860 alloc_init_lock_stateid(struct nfs4_lockowner *lo, struct nfs4_file *fp, struct nfs4_stateid *open_stp)
3861 {
3862 struct nfs4_stateid *stp;
3863 unsigned int hashval = stateid_hashval(lo->lo_owner.so_id, fp->fi_id);
3864
3865 stp = nfs4_alloc_stateid();
3866 if (stp == NULL)
3867 goto out;
3868 INIT_LIST_HEAD(&stp->st_hash);
3869 INIT_LIST_HEAD(&stp->st_perfile);
3870 INIT_LIST_HEAD(&stp->st_perstateowner);
3871 INIT_LIST_HEAD(&stp->st_lockowners); /* not used */
3872 list_add(&stp->st_hash, &stateid_hashtbl[hashval]);
3873 list_add(&stp->st_perfile, &fp->fi_stateids);
3874 list_add(&stp->st_perstateowner, &lo->lo_owner.so_stateids);
3875 stp->st_stateowner = &lo->lo_owner;
3876 stp->st_type = NFS4_LOCK_STID;
3877 get_nfs4_file(fp);
3878 stp->st_file = fp;
3879 stp->st_stateid.si_boot = boot_time;
3880 stp->st_stateid.si_stateownerid = lo->lo_owner.so_id;
3881 stp->st_stateid.si_fileid = fp->fi_id;
3882 /* note will be incremented before first return to client: */
3883 stp->st_stateid.si_generation = 0;
3884 stp->st_access_bmap = 0;
3885 stp->st_deny_bmap = open_stp->st_deny_bmap;
3886 stp->st_openstp = open_stp;
3887
3888 out:
3889 return stp;
3890 }
3891
3892 static int
3893 check_lock_length(u64 offset, u64 length)
3894 {
3895 return ((length == 0) || ((length != NFS4_MAX_UINT64) &&
3896 LOFF_OVERFLOW(offset, length)));
3897 }
3898
3899 static void get_lock_access(struct nfs4_stateid *lock_stp, u32 access)
3900 {
3901 struct nfs4_file *fp = lock_stp->st_file;
3902 int oflag = nfs4_access_to_omode(access);
3903
3904 if (test_bit(access, &lock_stp->st_access_bmap))
3905 return;
3906 nfs4_file_get_access(fp, oflag);
3907 __set_bit(access, &lock_stp->st_access_bmap);
3908 }
3909
3910 /*
3911 * LOCK operation
3912 */
3913 __be32
3914 nfsd4_lock(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
3915 struct nfsd4_lock *lock)
3916 {
3917 struct nfs4_openowner *open_sop = NULL;
3918 struct nfs4_lockowner *lock_sop = NULL;
3919 struct nfs4_stateid *lock_stp;
3920 struct nfs4_file *fp;
3921 struct file *filp = NULL;
3922 struct file_lock file_lock;
3923 struct file_lock conflock;
3924 __be32 status = 0;
3925 unsigned int strhashval;
3926 int lkflg;
3927 int err;
3928
3929 dprintk("NFSD: nfsd4_lock: start=%Ld length=%Ld\n",
3930 (long long) lock->lk_offset,
3931 (long long) lock->lk_length);
3932
3933 if (check_lock_length(lock->lk_offset, lock->lk_length))
3934 return nfserr_inval;
3935
3936 if ((status = fh_verify(rqstp, &cstate->current_fh,
3937 S_IFREG, NFSD_MAY_LOCK))) {
3938 dprintk("NFSD: nfsd4_lock: permission denied!\n");
3939 return status;
3940 }
3941
3942 nfs4_lock_state();
3943
3944 if (lock->lk_is_new) {
3945 /*
3946 * Client indicates that this is a new lockowner.
3947 * Use open owner and open stateid to create lock owner and
3948 * lock stateid.
3949 */
3950 struct nfs4_stateid *open_stp = NULL;
3951
3952 status = nfserr_stale_clientid;
3953 if (!nfsd4_has_session(cstate) &&
3954 STALE_CLIENTID(&lock->lk_new_clientid))
3955 goto out;
3956
3957 /* validate and update open stateid and open seqid */
3958 status = nfs4_preprocess_seqid_op(cstate,
3959 lock->lk_new_open_seqid,
3960 &lock->lk_new_open_stateid,
3961 OPEN_STATE, &open_stp);
3962 if (status)
3963 goto out;
3964 open_sop = openowner(open_stp->st_stateowner);
3965 status = nfserr_bad_stateid;
3966 if (!nfsd4_has_session(cstate) &&
3967 !same_clid(&open_sop->oo_owner.so_client->cl_clientid,
3968 &lock->v.new.clientid))
3969 goto out;
3970 /* create lockowner and lock stateid */
3971 fp = open_stp->st_file;
3972 strhashval = lock_ownerstr_hashval(fp->fi_inode,
3973 open_sop->oo_owner.so_client->cl_clientid.cl_id,
3974 &lock->v.new.owner);
3975 /* XXX: Do we need to check for duplicate stateowners on
3976 * the same file, or should they just be allowed (and
3977 * create new stateids)? */
3978 status = nfserr_jukebox;
3979 lock_sop = alloc_init_lock_stateowner(strhashval,
3980 open_sop->oo_owner.so_client, open_stp, lock);
3981 if (lock_sop == NULL)
3982 goto out;
3983 lock_stp = alloc_init_lock_stateid(lock_sop, fp, open_stp);
3984 if (lock_stp == NULL)
3985 goto out;
3986 } else {
3987 /* lock (lock owner + lock stateid) already exists */
3988 status = nfs4_preprocess_seqid_op(cstate,
3989 lock->lk_old_lock_seqid,
3990 &lock->lk_old_lock_stateid,
3991 LOCK_STATE, &lock_stp);
3992 if (status)
3993 goto out;
3994 lock_sop = lockowner(lock_stp->st_stateowner);
3995 fp = lock_stp->st_file;
3996 }
3997 /* lock_sop and lock_stp have been created or found */
3998
3999 lkflg = setlkflg(lock->lk_type);
4000 status = nfs4_check_openmode(lock_stp, lkflg);
4001 if (status)
4002 goto out;
4003
4004 status = nfserr_grace;
4005 if (locks_in_grace() && !lock->lk_reclaim)
4006 goto out;
4007 status = nfserr_no_grace;
4008 if (!locks_in_grace() && lock->lk_reclaim)
4009 goto out;
4010
4011 locks_init_lock(&file_lock);
4012 switch (lock->lk_type) {
4013 case NFS4_READ_LT:
4014 case NFS4_READW_LT:
4015 filp = find_readable_file(lock_stp->st_file);
4016 if (filp)
4017 get_lock_access(lock_stp, NFS4_SHARE_ACCESS_READ);
4018 file_lock.fl_type = F_RDLCK;
4019 break;
4020 case NFS4_WRITE_LT:
4021 case NFS4_WRITEW_LT:
4022 filp = find_writeable_file(lock_stp->st_file);
4023 if (filp)
4024 get_lock_access(lock_stp, NFS4_SHARE_ACCESS_WRITE);
4025 file_lock.fl_type = F_WRLCK;
4026 break;
4027 default:
4028 status = nfserr_inval;
4029 goto out;
4030 }
4031 if (!filp) {
4032 status = nfserr_openmode;
4033 goto out;
4034 }
4035 file_lock.fl_owner = (fl_owner_t)lock_sop;
4036 file_lock.fl_pid = current->tgid;
4037 file_lock.fl_file = filp;
4038 file_lock.fl_flags = FL_POSIX;
4039 file_lock.fl_lmops = &nfsd_posix_mng_ops;
4040
4041 file_lock.fl_start = lock->lk_offset;
4042 file_lock.fl_end = last_byte_offset(lock->lk_offset, lock->lk_length);
4043 nfs4_transform_lock_offset(&file_lock);
4044
4045 /*
4046 * Try to lock the file in the VFS.
4047 * Note: locks.c uses the BKL to protect the inode's lock list.
4048 */
4049
4050 err = vfs_lock_file(filp, F_SETLK, &file_lock, &conflock);
4051 switch (-err) {
4052 case 0: /* success! */
4053 update_stateid(&lock_stp->st_stateid);
4054 memcpy(&lock->lk_resp_stateid, &lock_stp->st_stateid,
4055 sizeof(stateid_t));
4056 status = 0;
4057 break;
4058 case (EAGAIN): /* conflock holds conflicting lock */
4059 status = nfserr_denied;
4060 dprintk("NFSD: nfsd4_lock: conflicting lock found!\n");
4061 nfs4_set_lock_denied(&conflock, &lock->lk_denied);
4062 break;
4063 case (EDEADLK):
4064 status = nfserr_deadlock;
4065 break;
4066 default:
4067 dprintk("NFSD: nfsd4_lock: vfs_lock_file() failed! status %d\n",err);
4068 status = nfserrno(err);
4069 break;
4070 }
4071 out:
4072 if (status && lock->lk_is_new && lock_sop)
4073 release_lockowner(lock_sop);
4074 if (!cstate->replay_owner)
4075 nfs4_unlock_state();
4076 return status;
4077 }
4078
4079 /*
4080 * The NFSv4 spec allows a client to do a LOCKT without holding an OPEN,
4081 * so we do a temporary open here just to get an open file to pass to
4082 * vfs_test_lock. (Arguably perhaps test_lock should be done with an
4083 * inode operation.)
4084 */
4085 static int nfsd_test_lock(struct svc_rqst *rqstp, struct svc_fh *fhp, struct file_lock *lock)
4086 {
4087 struct file *file;
4088 int err;
4089
4090 err = nfsd_open(rqstp, fhp, S_IFREG, NFSD_MAY_READ, &file);
4091 if (err)
4092 return err;
4093 err = vfs_test_lock(file, lock);
4094 nfsd_close(file);
4095 return err;
4096 }
4097
4098 /*
4099 * LOCKT operation
4100 */
4101 __be32
4102 nfsd4_lockt(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
4103 struct nfsd4_lockt *lockt)
4104 {
4105 struct inode *inode;
4106 struct file_lock file_lock;
4107 struct nfs4_lockowner *lo;
4108 int error;
4109 __be32 status;
4110
4111 if (locks_in_grace())
4112 return nfserr_grace;
4113
4114 if (check_lock_length(lockt->lt_offset, lockt->lt_length))
4115 return nfserr_inval;
4116
4117 nfs4_lock_state();
4118
4119 status = nfserr_stale_clientid;
4120 if (!nfsd4_has_session(cstate) && STALE_CLIENTID(&lockt->lt_clientid))
4121 goto out;
4122
4123 if ((status = fh_verify(rqstp, &cstate->current_fh, S_IFREG, 0)))
4124 goto out;
4125
4126 inode = cstate->current_fh.fh_dentry->d_inode;
4127 locks_init_lock(&file_lock);
4128 switch (lockt->lt_type) {
4129 case NFS4_READ_LT:
4130 case NFS4_READW_LT:
4131 file_lock.fl_type = F_RDLCK;
4132 break;
4133 case NFS4_WRITE_LT:
4134 case NFS4_WRITEW_LT:
4135 file_lock.fl_type = F_WRLCK;
4136 break;
4137 default:
4138 dprintk("NFSD: nfs4_lockt: bad lock type!\n");
4139 status = nfserr_inval;
4140 goto out;
4141 }
4142
4143 lo = find_lockowner_str(inode, &lockt->lt_clientid, &lockt->lt_owner);
4144 if (lo)
4145 file_lock.fl_owner = (fl_owner_t)lo;
4146 file_lock.fl_pid = current->tgid;
4147 file_lock.fl_flags = FL_POSIX;
4148
4149 file_lock.fl_start = lockt->lt_offset;
4150 file_lock.fl_end = last_byte_offset(lockt->lt_offset, lockt->lt_length);
4151
4152 nfs4_transform_lock_offset(&file_lock);
4153
4154 status = nfs_ok;
4155 error = nfsd_test_lock(rqstp, &cstate->current_fh, &file_lock);
4156 if (error) {
4157 status = nfserrno(error);
4158 goto out;
4159 }
4160 if (file_lock.fl_type != F_UNLCK) {
4161 status = nfserr_denied;
4162 nfs4_set_lock_denied(&file_lock, &lockt->lt_denied);
4163 }
4164 out:
4165 nfs4_unlock_state();
4166 return status;
4167 }
4168
4169 __be32
4170 nfsd4_locku(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
4171 struct nfsd4_locku *locku)
4172 {
4173 struct nfs4_stateid *stp;
4174 struct file *filp = NULL;
4175 struct file_lock file_lock;
4176 __be32 status;
4177 int err;
4178
4179 dprintk("NFSD: nfsd4_locku: start=%Ld length=%Ld\n",
4180 (long long) locku->lu_offset,
4181 (long long) locku->lu_length);
4182
4183 if (check_lock_length(locku->lu_offset, locku->lu_length))
4184 return nfserr_inval;
4185
4186 nfs4_lock_state();
4187
4188 status = nfs4_preprocess_seqid_op(cstate, locku->lu_seqid,
4189 &locku->lu_stateid, LOCK_STATE, &stp);
4190 if (status)
4191 goto out;
4192 filp = find_any_file(stp->st_file);
4193 if (!filp) {
4194 status = nfserr_lock_range;
4195 goto out;
4196 }
4197 BUG_ON(!filp);
4198 locks_init_lock(&file_lock);
4199 file_lock.fl_type = F_UNLCK;
4200 file_lock.fl_owner = (fl_owner_t)lockowner(stp->st_stateowner);
4201 file_lock.fl_pid = current->tgid;
4202 file_lock.fl_file = filp;
4203 file_lock.fl_flags = FL_POSIX;
4204 file_lock.fl_lmops = &nfsd_posix_mng_ops;
4205 file_lock.fl_start = locku->lu_offset;
4206
4207 file_lock.fl_end = last_byte_offset(locku->lu_offset, locku->lu_length);
4208 nfs4_transform_lock_offset(&file_lock);
4209
4210 /*
4211 * Try to unlock the file in the VFS.
4212 */
4213 err = vfs_lock_file(filp, F_SETLK, &file_lock, NULL);
4214 if (err) {
4215 dprintk("NFSD: nfs4_locku: vfs_lock_file failed!\n");
4216 goto out_nfserr;
4217 }
4218 /*
4219 * OK, unlock succeeded; the only thing left to do is update the stateid.
4220 */
4221 update_stateid(&stp->st_stateid);
4222 memcpy(&locku->lu_stateid, &stp->st_stateid, sizeof(stateid_t));
4223
4224 out:
4225 nfs4_unlock_state();
4226 return status;
4227
4228 out_nfserr:
4229 status = nfserrno(err);
4230 goto out;
4231 }
4232
4233 /*
4234 * returns
4235 * 1: locks held by lockowner
4236 * 0: no locks held by lockowner
4237 */
4238 static int
4239 check_for_locks(struct nfs4_file *filp, struct nfs4_lockowner *lowner)
4240 {
4241 struct file_lock **flpp;
4242 struct inode *inode = filp->fi_inode;
4243 int status = 0;
4244
4245 lock_flocks();
4246 for (flpp = &inode->i_flock; *flpp != NULL; flpp = &(*flpp)->fl_next) {
4247 if ((*flpp)->fl_owner == (fl_owner_t)lowner) {
4248 status = 1;
4249 goto out;
4250 }
4251 }
4252 out:
4253 unlock_flocks();
4254 return status;
4255 }
4256
4257 __be32
4258 nfsd4_release_lockowner(struct svc_rqst *rqstp,
4259 struct nfsd4_compound_state *cstate,
4260 struct nfsd4_release_lockowner *rlockowner)
4261 {
4262 clientid_t *clid = &rlockowner->rl_clientid;
4263 struct nfs4_stateowner *sop;
4264 struct nfs4_lockowner *lo;
4265 struct nfs4_stateid *stp;
4266 struct xdr_netobj *owner = &rlockowner->rl_owner;
4267 struct list_head matches;
4268 int i;
4269 __be32 status;
4270
4271 dprintk("nfsd4_release_lockowner clientid: (%08x/%08x):\n",
4272 clid->cl_boot, clid->cl_id);
4273
4274 /* XXX check for lease expiration */
4275
4276 status = nfserr_stale_clientid;
4277 if (STALE_CLIENTID(clid))
4278 return status;
4279
4280 nfs4_lock_state();
4281
4282 status = nfserr_locks_held;
4283 /* XXX: we're doing a linear search through all the lockowners.
4284 * Yipes! For now we'll just hope clients aren't really using
4285 * release_lockowner much, but eventually we have to fix these
4286 * data structures. */
4287 INIT_LIST_HEAD(&matches);
4288 for (i = 0; i < LOCK_HASH_SIZE; i++) {
4289 list_for_each_entry(sop, &lock_ownerid_hashtbl[i], so_idhash) {
4290 if (!same_owner_str(sop, owner, clid))
4291 continue;
4292 list_for_each_entry(stp, &sop->so_stateids,
4293 st_perstateowner) {
4294 lo = lockowner(sop);
4295 if (check_for_locks(stp->st_file, lo))
4296 goto out;
4297 list_add(&lo->lo_list, &matches);
4298 }
4299 }
4300 }
4301 /* Clients probably won't expect us to return with some (but not all)
4302 * of the lockowner state released; so don't release any until all
4303 * have been checked. */
4304 status = nfs_ok;
4305 while (!list_empty(&matches)) {
4306 lo = list_entry(matches.next, struct nfs4_lockowner,
4307 lo_list);
4308 /* unhash_stateowner deletes so_perclient only
4309 * for openowners. */
4310 list_del(&lo->lo_list);
4311 release_lockowner(lo);
4312 }
4313 out:
4314 nfs4_unlock_state();
4315 return status;
4316 }
4317
4318 static inline struct nfs4_client_reclaim *
4319 alloc_reclaim(void)
4320 {
4321 return kmalloc(sizeof(struct nfs4_client_reclaim), GFP_KERNEL);
4322 }
4323
4324 int
4325 nfs4_has_reclaimed_state(const char *name, bool use_exchange_id)
4326 {
4327 unsigned int strhashval = clientstr_hashval(name);
4328 struct nfs4_client *clp;
4329
4330 clp = find_confirmed_client_by_str(name, strhashval);
4331 return clp ? 1 : 0;
4332 }
4333
4334 /*
4335 * failure => all reset bets are off, nfserr_no_grace...
4336 */
4337 int
4338 nfs4_client_to_reclaim(const char *name)
4339 {
4340 unsigned int strhashval;
4341 struct nfs4_client_reclaim *crp = NULL;
4342
4343 dprintk("NFSD nfs4_client_to_reclaim NAME: %.*s\n", HEXDIR_LEN, name);
4344 crp = alloc_reclaim();
4345 if (!crp)
4346 return 0;
4347 strhashval = clientstr_hashval(name);
4348 INIT_LIST_HEAD(&crp->cr_strhash);
4349 list_add(&crp->cr_strhash, &reclaim_str_hashtbl[strhashval]);
4350 memcpy(crp->cr_recdir, name, HEXDIR_LEN);
4351 reclaim_str_hashtbl_size++;
4352 return 1;
4353 }
4354
4355 static void
4356 nfs4_release_reclaim(void)
4357 {
4358 struct nfs4_client_reclaim *crp = NULL;
4359 int i;
4360
4361 for (i = 0; i < CLIENT_HASH_SIZE; i++) {
4362 while (!list_empty(&reclaim_str_hashtbl[i])) {
4363 crp = list_entry(reclaim_str_hashtbl[i].next,
4364 struct nfs4_client_reclaim, cr_strhash);
4365 list_del(&crp->cr_strhash);
4366 kfree(crp);
4367 reclaim_str_hashtbl_size--;
4368 }
4369 }
4370 BUG_ON(reclaim_str_hashtbl_size);
4371 }
4372
4373 /*
4374 * called from OPEN, CLAIM_PREVIOUS with a new clientid. */
4375 static struct nfs4_client_reclaim *
4376 nfs4_find_reclaim_client(clientid_t *clid)
4377 {
4378 unsigned int strhashval;
4379 struct nfs4_client *clp;
4380 struct nfs4_client_reclaim *crp = NULL;
4381
4382
4383 /* find clientid in conf_id_hashtbl */
4384 clp = find_confirmed_client(clid);
4385 if (clp == NULL)
4386 return NULL;
4387
4388 dprintk("NFSD: nfs4_find_reclaim_client for %.*s with recdir %s\n",
4389 clp->cl_name.len, clp->cl_name.data,
4390 clp->cl_recdir);
4391
4392 /* find clp->cl_name in reclaim_str_hashtbl */
4393 strhashval = clientstr_hashval(clp->cl_recdir);
4394 list_for_each_entry(crp, &reclaim_str_hashtbl[strhashval], cr_strhash) {
4395 if (same_name(crp->cr_recdir, clp->cl_recdir)) {
4396 return crp;
4397 }
4398 }
4399 return NULL;
4400 }
4401
4402 /*
4403 * Called from OPEN. Look for clientid in reclaim list.
4404 */
4405 __be32
4406 nfs4_check_open_reclaim(clientid_t *clid)
4407 {
4408 return nfs4_find_reclaim_client(clid) ? nfs_ok : nfserr_reclaim_bad;
4409 }
4410
4411 /* initialization to perform at module load time: */
4412
4413 int
4414 nfs4_state_init(void)
4415 {
4416 int i, status;
4417
4418 status = nfsd4_init_slabs();
4419 if (status)
4420 return status;
4421 for (i = 0; i < CLIENT_HASH_SIZE; i++) {
4422 INIT_LIST_HEAD(&conf_id_hashtbl[i]);
4423 INIT_LIST_HEAD(&conf_str_hashtbl[i]);
4424 INIT_LIST_HEAD(&unconf_str_hashtbl[i]);
4425 INIT_LIST_HEAD(&unconf_id_hashtbl[i]);
4426 INIT_LIST_HEAD(&reclaim_str_hashtbl[i]);
4427 }
4428 for (i = 0; i < SESSION_HASH_SIZE; i++)
4429 INIT_LIST_HEAD(&sessionid_hashtbl[i]);
4430 for (i = 0; i < FILE_HASH_SIZE; i++) {
4431 INIT_LIST_HEAD(&file_hashtbl[i]);
4432 }
4433 for (i = 0; i < OPEN_OWNER_HASH_SIZE; i++) {
4434 INIT_LIST_HEAD(&open_ownerstr_hashtbl[i]);
4435 INIT_LIST_HEAD(&open_ownerid_hashtbl[i]);
4436 }
4437 for (i = 0; i < STATEID_HASH_SIZE; i++)
4438 INIT_LIST_HEAD(&stateid_hashtbl[i]);
4439 for (i = 0; i < LOCK_HASH_SIZE; i++) {
4440 INIT_LIST_HEAD(&lock_ownerid_hashtbl[i]);
4441 INIT_LIST_HEAD(&lock_ownerstr_hashtbl[i]);
4442 }
4443 memset(&onestateid, ~0, sizeof(stateid_t));
4444 INIT_LIST_HEAD(&close_lru);
4445 INIT_LIST_HEAD(&client_lru);
4446 INIT_LIST_HEAD(&del_recall_lru);
4447 reclaim_str_hashtbl_size = 0;
4448 return 0;
4449 }
4450
4451 static void
4452 nfsd4_load_reboot_recovery_data(void)
4453 {
4454 int status;
4455
4456 nfs4_lock_state();
4457 nfsd4_init_recdir();
4458 status = nfsd4_recdir_load();
4459 nfs4_unlock_state();
4460 if (status)
4461 printk("NFSD: Failure reading reboot recovery data\n");
4462 }
4463
4464 /*
4465 * Since the lifetime of a delegation isn't limited to that of an open, a
4466 * client may quite reasonably hang on to a delegation as long as it has
4467 * the inode cached. This becomes an obvious problem the first time a
4468 * client's inode cache approaches the size of the server's total memory.
4469 *
4470 * For now we avoid this problem by imposing a hard limit on the number
4471 * of delegations, which varies according to the server's memory size.
4472 */
4473 static void
4474 set_max_delegations(void)
4475 {
4476 /*
4477 * Allow at most 4 delegations per megabyte of RAM. Quick
4478 * estimates suggest that in the worst case (where every delegation
4479 * is for a different inode), a delegation could take about 1.5K,
4480 * giving a worst case usage of about 6% of memory.
4481 */
4482 max_delegations = nr_free_buffer_pages() >> (20 - 2 - PAGE_SHIFT);
4483 }
4484
4485 /* initialization to perform when the nfsd service is started: */
4486
4487 static int
4488 __nfs4_state_start(void)
4489 {
4490 int ret;
4491
4492 boot_time = get_seconds();
4493 locks_start_grace(&nfsd4_manager);
4494 printk(KERN_INFO "NFSD: starting %ld-second grace period\n",
4495 nfsd4_grace);
4496 ret = set_callback_cred();
4497 if (ret)
4498 return -ENOMEM;
4499 laundry_wq = create_singlethread_workqueue("nfsd4");
4500 if (laundry_wq == NULL)
4501 return -ENOMEM;
4502 ret = nfsd4_create_callback_queue();
4503 if (ret)
4504 goto out_free_laundry;
4505 queue_delayed_work(laundry_wq, &laundromat_work, nfsd4_grace * HZ);
4506 set_max_delegations();
4507 return 0;
4508 out_free_laundry:
4509 destroy_workqueue(laundry_wq);
4510 return ret;
4511 }
4512
4513 int
4514 nfs4_state_start(void)
4515 {
4516 nfsd4_load_reboot_recovery_data();
4517 return __nfs4_state_start();
4518 }
4519
4520 static void
4521 __nfs4_state_shutdown(void)
4522 {
4523 int i;
4524 struct nfs4_client *clp = NULL;
4525 struct nfs4_delegation *dp = NULL;
4526 struct list_head *pos, *next, reaplist;
4527
4528 for (i = 0; i < CLIENT_HASH_SIZE; i++) {
4529 while (!list_empty(&conf_id_hashtbl[i])) {
4530 clp = list_entry(conf_id_hashtbl[i].next, struct nfs4_client, cl_idhash);
4531 expire_client(clp);
4532 }
4533 while (!list_empty(&unconf_str_hashtbl[i])) {
4534 clp = list_entry(unconf_str_hashtbl[i].next, struct nfs4_client, cl_strhash);
4535 expire_client(clp);
4536 }
4537 }
4538 INIT_LIST_HEAD(&reaplist);
4539 spin_lock(&recall_lock);
4540 list_for_each_safe(pos, next, &del_recall_lru) {
4541 dp = list_entry (pos, struct nfs4_delegation, dl_recall_lru);
4542 list_move(&dp->dl_recall_lru, &reaplist);
4543 }
4544 spin_unlock(&recall_lock);
4545 list_for_each_safe(pos, next, &reaplist) {
4546 dp = list_entry (pos, struct nfs4_delegation, dl_recall_lru);
4547 list_del_init(&dp->dl_recall_lru);
4548 unhash_delegation(dp);
4549 }
4550
4551 nfsd4_shutdown_recdir();
4552 }
4553
4554 void
4555 nfs4_state_shutdown(void)
4556 {
4557 cancel_delayed_work_sync(&laundromat_work);
4558 destroy_workqueue(laundry_wq);
4559 locks_end_grace(&nfsd4_manager);
4560 nfs4_lock_state();
4561 nfs4_release_reclaim();
4562 __nfs4_state_shutdown();
4563 nfs4_unlock_state();
4564 nfsd4_destroy_callback_queue();
4565 }
This page took 0.122298 seconds and 6 git commands to generate.