nfsd4: move some of nfs4_stateid into a separate structure
[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_ol_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_ol_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_ol_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_ol_stateid *stp)
407 {
408 list_del(&stp->st_stid.sc_hash);
409 list_del(&stp->st_perfile);
410 list_del(&stp->st_perstateowner);
411 }
412
413 static void close_generic_stateid(struct nfs4_ol_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_ol_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_ol_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_ol_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_ol_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_ol_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_ol_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_ol_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_ol_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_ol_stateid *find_stateid(stateid_t *t)
1072 {
1073 struct nfs4_stid *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], sc_hash)
1078 if (same_stateid(&s->sc_stateid, t))
1079 return openlockstateid(s);
1080 return NULL;
1081 }
1082
1083 static struct nfs4_ol_stateid *find_stateid_by_type(stateid_t *t, char typemask)
1084 {
1085 struct nfs4_ol_stateid *s;
1086
1087 s = find_stateid(t);
1088 if (!s)
1089 return NULL;
1090 if (typemask & s->st_stid.sc_type)
1091 return s;
1092 return NULL;
1093 }
1094
1095 static struct nfs4_client *create_client(struct xdr_netobj name, char *recdir,
1096 struct svc_rqst *rqstp, nfs4_verifier *verf)
1097 {
1098 struct nfs4_client *clp;
1099 struct sockaddr *sa = svc_addr(rqstp);
1100 char *princ;
1101
1102 clp = alloc_client(name);
1103 if (clp == NULL)
1104 return NULL;
1105
1106 INIT_LIST_HEAD(&clp->cl_sessions);
1107
1108 princ = svc_gss_principal(rqstp);
1109 if (princ) {
1110 clp->cl_principal = kstrdup(princ, GFP_KERNEL);
1111 if (clp->cl_principal == NULL) {
1112 free_client(clp);
1113 return NULL;
1114 }
1115 }
1116
1117 memcpy(clp->cl_recdir, recdir, HEXDIR_LEN);
1118 atomic_set(&clp->cl_refcount, 0);
1119 clp->cl_cb_state = NFSD4_CB_UNKNOWN;
1120 INIT_LIST_HEAD(&clp->cl_idhash);
1121 INIT_LIST_HEAD(&clp->cl_strhash);
1122 INIT_LIST_HEAD(&clp->cl_openowners);
1123 INIT_LIST_HEAD(&clp->cl_delegations);
1124 INIT_LIST_HEAD(&clp->cl_lru);
1125 INIT_LIST_HEAD(&clp->cl_callbacks);
1126 spin_lock_init(&clp->cl_lock);
1127 INIT_WORK(&clp->cl_cb_null.cb_work, nfsd4_do_callback_rpc);
1128 clp->cl_time = get_seconds();
1129 clear_bit(0, &clp->cl_cb_slot_busy);
1130 rpc_init_wait_queue(&clp->cl_cb_waitq, "Backchannel slot table");
1131 copy_verf(clp, verf);
1132 rpc_copy_addr((struct sockaddr *) &clp->cl_addr, sa);
1133 clp->cl_flavor = rqstp->rq_flavor;
1134 copy_cred(&clp->cl_cred, &rqstp->rq_cred);
1135 gen_confirm(clp);
1136 clp->cl_cb_session = NULL;
1137 return clp;
1138 }
1139
1140 static int check_name(struct xdr_netobj name)
1141 {
1142 if (name.len == 0)
1143 return 0;
1144 if (name.len > NFS4_OPAQUE_LIMIT) {
1145 dprintk("NFSD: check_name: name too long(%d)!\n", name.len);
1146 return 0;
1147 }
1148 return 1;
1149 }
1150
1151 static void
1152 add_to_unconfirmed(struct nfs4_client *clp, unsigned int strhashval)
1153 {
1154 unsigned int idhashval;
1155
1156 list_add(&clp->cl_strhash, &unconf_str_hashtbl[strhashval]);
1157 idhashval = clientid_hashval(clp->cl_clientid.cl_id);
1158 list_add(&clp->cl_idhash, &unconf_id_hashtbl[idhashval]);
1159 renew_client(clp);
1160 }
1161
1162 static void
1163 move_to_confirmed(struct nfs4_client *clp)
1164 {
1165 unsigned int idhashval = clientid_hashval(clp->cl_clientid.cl_id);
1166 unsigned int strhashval;
1167
1168 dprintk("NFSD: move_to_confirm nfs4_client %p\n", clp);
1169 list_move(&clp->cl_idhash, &conf_id_hashtbl[idhashval]);
1170 strhashval = clientstr_hashval(clp->cl_recdir);
1171 list_move(&clp->cl_strhash, &conf_str_hashtbl[strhashval]);
1172 renew_client(clp);
1173 }
1174
1175 static struct nfs4_client *
1176 find_confirmed_client(clientid_t *clid)
1177 {
1178 struct nfs4_client *clp;
1179 unsigned int idhashval = clientid_hashval(clid->cl_id);
1180
1181 list_for_each_entry(clp, &conf_id_hashtbl[idhashval], cl_idhash) {
1182 if (same_clid(&clp->cl_clientid, clid))
1183 return clp;
1184 }
1185 return NULL;
1186 }
1187
1188 static struct nfs4_client *
1189 find_unconfirmed_client(clientid_t *clid)
1190 {
1191 struct nfs4_client *clp;
1192 unsigned int idhashval = clientid_hashval(clid->cl_id);
1193
1194 list_for_each_entry(clp, &unconf_id_hashtbl[idhashval], cl_idhash) {
1195 if (same_clid(&clp->cl_clientid, clid))
1196 return clp;
1197 }
1198 return NULL;
1199 }
1200
1201 static bool clp_used_exchangeid(struct nfs4_client *clp)
1202 {
1203 return clp->cl_exchange_flags != 0;
1204 }
1205
1206 static struct nfs4_client *
1207 find_confirmed_client_by_str(const char *dname, unsigned int hashval)
1208 {
1209 struct nfs4_client *clp;
1210
1211 list_for_each_entry(clp, &conf_str_hashtbl[hashval], cl_strhash) {
1212 if (same_name(clp->cl_recdir, dname))
1213 return clp;
1214 }
1215 return NULL;
1216 }
1217
1218 static struct nfs4_client *
1219 find_unconfirmed_client_by_str(const char *dname, unsigned int hashval)
1220 {
1221 struct nfs4_client *clp;
1222
1223 list_for_each_entry(clp, &unconf_str_hashtbl[hashval], cl_strhash) {
1224 if (same_name(clp->cl_recdir, dname))
1225 return clp;
1226 }
1227 return NULL;
1228 }
1229
1230 static void rpc_svcaddr2sockaddr(struct sockaddr *sa, unsigned short family, union svc_addr_u *svcaddr)
1231 {
1232 switch (family) {
1233 case AF_INET:
1234 ((struct sockaddr_in *)sa)->sin_family = AF_INET;
1235 ((struct sockaddr_in *)sa)->sin_addr = svcaddr->addr;
1236 return;
1237 case AF_INET6:
1238 ((struct sockaddr_in6 *)sa)->sin6_family = AF_INET6;
1239 ((struct sockaddr_in6 *)sa)->sin6_addr = svcaddr->addr6;
1240 return;
1241 }
1242 }
1243
1244 static void
1245 gen_callback(struct nfs4_client *clp, struct nfsd4_setclientid *se, struct svc_rqst *rqstp)
1246 {
1247 struct nfs4_cb_conn *conn = &clp->cl_cb_conn;
1248 struct sockaddr *sa = svc_addr(rqstp);
1249 u32 scopeid = rpc_get_scope_id(sa);
1250 unsigned short expected_family;
1251
1252 /* Currently, we only support tcp and tcp6 for the callback channel */
1253 if (se->se_callback_netid_len == 3 &&
1254 !memcmp(se->se_callback_netid_val, "tcp", 3))
1255 expected_family = AF_INET;
1256 else if (se->se_callback_netid_len == 4 &&
1257 !memcmp(se->se_callback_netid_val, "tcp6", 4))
1258 expected_family = AF_INET6;
1259 else
1260 goto out_err;
1261
1262 conn->cb_addrlen = rpc_uaddr2sockaddr(se->se_callback_addr_val,
1263 se->se_callback_addr_len,
1264 (struct sockaddr *)&conn->cb_addr,
1265 sizeof(conn->cb_addr));
1266
1267 if (!conn->cb_addrlen || conn->cb_addr.ss_family != expected_family)
1268 goto out_err;
1269
1270 if (conn->cb_addr.ss_family == AF_INET6)
1271 ((struct sockaddr_in6 *)&conn->cb_addr)->sin6_scope_id = scopeid;
1272
1273 conn->cb_prog = se->se_callback_prog;
1274 conn->cb_ident = se->se_callback_ident;
1275 rpc_svcaddr2sockaddr((struct sockaddr *)&conn->cb_saddr, expected_family, &rqstp->rq_daddr);
1276 return;
1277 out_err:
1278 conn->cb_addr.ss_family = AF_UNSPEC;
1279 conn->cb_addrlen = 0;
1280 dprintk(KERN_INFO "NFSD: this client (clientid %08x/%08x) "
1281 "will not receive delegations\n",
1282 clp->cl_clientid.cl_boot, clp->cl_clientid.cl_id);
1283
1284 return;
1285 }
1286
1287 /*
1288 * Cache a reply. nfsd4_check_drc_limit() has bounded the cache size.
1289 */
1290 void
1291 nfsd4_store_cache_entry(struct nfsd4_compoundres *resp)
1292 {
1293 struct nfsd4_slot *slot = resp->cstate.slot;
1294 unsigned int base;
1295
1296 dprintk("--> %s slot %p\n", __func__, slot);
1297
1298 slot->sl_opcnt = resp->opcnt;
1299 slot->sl_status = resp->cstate.status;
1300
1301 if (nfsd4_not_cached(resp)) {
1302 slot->sl_datalen = 0;
1303 return;
1304 }
1305 slot->sl_datalen = (char *)resp->p - (char *)resp->cstate.datap;
1306 base = (char *)resp->cstate.datap -
1307 (char *)resp->xbuf->head[0].iov_base;
1308 if (read_bytes_from_xdr_buf(resp->xbuf, base, slot->sl_data,
1309 slot->sl_datalen))
1310 WARN("%s: sessions DRC could not cache compound\n", __func__);
1311 return;
1312 }
1313
1314 /*
1315 * Encode the replay sequence operation from the slot values.
1316 * If cachethis is FALSE encode the uncached rep error on the next
1317 * operation which sets resp->p and increments resp->opcnt for
1318 * nfs4svc_encode_compoundres.
1319 *
1320 */
1321 static __be32
1322 nfsd4_enc_sequence_replay(struct nfsd4_compoundargs *args,
1323 struct nfsd4_compoundres *resp)
1324 {
1325 struct nfsd4_op *op;
1326 struct nfsd4_slot *slot = resp->cstate.slot;
1327
1328 dprintk("--> %s resp->opcnt %d cachethis %u \n", __func__,
1329 resp->opcnt, resp->cstate.slot->sl_cachethis);
1330
1331 /* Encode the replayed sequence operation */
1332 op = &args->ops[resp->opcnt - 1];
1333 nfsd4_encode_operation(resp, op);
1334
1335 /* Return nfserr_retry_uncached_rep in next operation. */
1336 if (args->opcnt > 1 && slot->sl_cachethis == 0) {
1337 op = &args->ops[resp->opcnt++];
1338 op->status = nfserr_retry_uncached_rep;
1339 nfsd4_encode_operation(resp, op);
1340 }
1341 return op->status;
1342 }
1343
1344 /*
1345 * The sequence operation is not cached because we can use the slot and
1346 * session values.
1347 */
1348 __be32
1349 nfsd4_replay_cache_entry(struct nfsd4_compoundres *resp,
1350 struct nfsd4_sequence *seq)
1351 {
1352 struct nfsd4_slot *slot = resp->cstate.slot;
1353 __be32 status;
1354
1355 dprintk("--> %s slot %p\n", __func__, slot);
1356
1357 /* Either returns 0 or nfserr_retry_uncached */
1358 status = nfsd4_enc_sequence_replay(resp->rqstp->rq_argp, resp);
1359 if (status == nfserr_retry_uncached_rep)
1360 return status;
1361
1362 /* The sequence operation has been encoded, cstate->datap set. */
1363 memcpy(resp->cstate.datap, slot->sl_data, slot->sl_datalen);
1364
1365 resp->opcnt = slot->sl_opcnt;
1366 resp->p = resp->cstate.datap + XDR_QUADLEN(slot->sl_datalen);
1367 status = slot->sl_status;
1368
1369 return status;
1370 }
1371
1372 /*
1373 * Set the exchange_id flags returned by the server.
1374 */
1375 static void
1376 nfsd4_set_ex_flags(struct nfs4_client *new, struct nfsd4_exchange_id *clid)
1377 {
1378 /* pNFS is not supported */
1379 new->cl_exchange_flags |= EXCHGID4_FLAG_USE_NON_PNFS;
1380
1381 /* Referrals are supported, Migration is not. */
1382 new->cl_exchange_flags |= EXCHGID4_FLAG_SUPP_MOVED_REFER;
1383
1384 /* set the wire flags to return to client. */
1385 clid->flags = new->cl_exchange_flags;
1386 }
1387
1388 __be32
1389 nfsd4_exchange_id(struct svc_rqst *rqstp,
1390 struct nfsd4_compound_state *cstate,
1391 struct nfsd4_exchange_id *exid)
1392 {
1393 struct nfs4_client *unconf, *conf, *new;
1394 int status;
1395 unsigned int strhashval;
1396 char dname[HEXDIR_LEN];
1397 char addr_str[INET6_ADDRSTRLEN];
1398 nfs4_verifier verf = exid->verifier;
1399 struct sockaddr *sa = svc_addr(rqstp);
1400
1401 rpc_ntop(sa, addr_str, sizeof(addr_str));
1402 dprintk("%s rqstp=%p exid=%p clname.len=%u clname.data=%p "
1403 "ip_addr=%s flags %x, spa_how %d\n",
1404 __func__, rqstp, exid, exid->clname.len, exid->clname.data,
1405 addr_str, exid->flags, exid->spa_how);
1406
1407 if (!check_name(exid->clname) || (exid->flags & ~EXCHGID4_FLAG_MASK_A))
1408 return nfserr_inval;
1409
1410 /* Currently only support SP4_NONE */
1411 switch (exid->spa_how) {
1412 case SP4_NONE:
1413 break;
1414 case SP4_SSV:
1415 return nfserr_serverfault;
1416 default:
1417 BUG(); /* checked by xdr code */
1418 case SP4_MACH_CRED:
1419 return nfserr_serverfault; /* no excuse :-/ */
1420 }
1421
1422 status = nfs4_make_rec_clidname(dname, &exid->clname);
1423
1424 if (status)
1425 goto error;
1426
1427 strhashval = clientstr_hashval(dname);
1428
1429 nfs4_lock_state();
1430 status = nfs_ok;
1431
1432 conf = find_confirmed_client_by_str(dname, strhashval);
1433 if (conf) {
1434 if (!clp_used_exchangeid(conf)) {
1435 status = nfserr_clid_inuse; /* XXX: ? */
1436 goto out;
1437 }
1438 if (!same_verf(&verf, &conf->cl_verifier)) {
1439 /* 18.35.4 case 8 */
1440 if (exid->flags & EXCHGID4_FLAG_UPD_CONFIRMED_REC_A) {
1441 status = nfserr_not_same;
1442 goto out;
1443 }
1444 /* Client reboot: destroy old state */
1445 expire_client(conf);
1446 goto out_new;
1447 }
1448 if (!same_creds(&conf->cl_cred, &rqstp->rq_cred)) {
1449 /* 18.35.4 case 9 */
1450 if (exid->flags & EXCHGID4_FLAG_UPD_CONFIRMED_REC_A) {
1451 status = nfserr_perm;
1452 goto out;
1453 }
1454 expire_client(conf);
1455 goto out_new;
1456 }
1457 /*
1458 * Set bit when the owner id and verifier map to an already
1459 * confirmed client id (18.35.3).
1460 */
1461 exid->flags |= EXCHGID4_FLAG_CONFIRMED_R;
1462
1463 /*
1464 * Falling into 18.35.4 case 2, possible router replay.
1465 * Leave confirmed record intact and return same result.
1466 */
1467 copy_verf(conf, &verf);
1468 new = conf;
1469 goto out_copy;
1470 }
1471
1472 /* 18.35.4 case 7 */
1473 if (exid->flags & EXCHGID4_FLAG_UPD_CONFIRMED_REC_A) {
1474 status = nfserr_noent;
1475 goto out;
1476 }
1477
1478 unconf = find_unconfirmed_client_by_str(dname, strhashval);
1479 if (unconf) {
1480 /*
1481 * Possible retry or client restart. Per 18.35.4 case 4,
1482 * a new unconfirmed record should be generated regardless
1483 * of whether any properties have changed.
1484 */
1485 expire_client(unconf);
1486 }
1487
1488 out_new:
1489 /* Normal case */
1490 new = create_client(exid->clname, dname, rqstp, &verf);
1491 if (new == NULL) {
1492 status = nfserr_jukebox;
1493 goto out;
1494 }
1495
1496 gen_clid(new);
1497 add_to_unconfirmed(new, strhashval);
1498 out_copy:
1499 exid->clientid.cl_boot = new->cl_clientid.cl_boot;
1500 exid->clientid.cl_id = new->cl_clientid.cl_id;
1501
1502 exid->seqid = 1;
1503 nfsd4_set_ex_flags(new, exid);
1504
1505 dprintk("nfsd4_exchange_id seqid %d flags %x\n",
1506 new->cl_cs_slot.sl_seqid, new->cl_exchange_flags);
1507 status = nfs_ok;
1508
1509 out:
1510 nfs4_unlock_state();
1511 error:
1512 dprintk("nfsd4_exchange_id returns %d\n", ntohl(status));
1513 return status;
1514 }
1515
1516 static int
1517 check_slot_seqid(u32 seqid, u32 slot_seqid, int slot_inuse)
1518 {
1519 dprintk("%s enter. seqid %d slot_seqid %d\n", __func__, seqid,
1520 slot_seqid);
1521
1522 /* The slot is in use, and no response has been sent. */
1523 if (slot_inuse) {
1524 if (seqid == slot_seqid)
1525 return nfserr_jukebox;
1526 else
1527 return nfserr_seq_misordered;
1528 }
1529 /* Normal */
1530 if (likely(seqid == slot_seqid + 1))
1531 return nfs_ok;
1532 /* Replay */
1533 if (seqid == slot_seqid)
1534 return nfserr_replay_cache;
1535 /* Wraparound */
1536 if (seqid == 1 && (slot_seqid + 1) == 0)
1537 return nfs_ok;
1538 /* Misordered replay or misordered new request */
1539 return nfserr_seq_misordered;
1540 }
1541
1542 /*
1543 * Cache the create session result into the create session single DRC
1544 * slot cache by saving the xdr structure. sl_seqid has been set.
1545 * Do this for solo or embedded create session operations.
1546 */
1547 static void
1548 nfsd4_cache_create_session(struct nfsd4_create_session *cr_ses,
1549 struct nfsd4_clid_slot *slot, int nfserr)
1550 {
1551 slot->sl_status = nfserr;
1552 memcpy(&slot->sl_cr_ses, cr_ses, sizeof(*cr_ses));
1553 }
1554
1555 static __be32
1556 nfsd4_replay_create_session(struct nfsd4_create_session *cr_ses,
1557 struct nfsd4_clid_slot *slot)
1558 {
1559 memcpy(cr_ses, &slot->sl_cr_ses, sizeof(*cr_ses));
1560 return slot->sl_status;
1561 }
1562
1563 #define NFSD_MIN_REQ_HDR_SEQ_SZ ((\
1564 2 * 2 + /* credential,verifier: AUTH_NULL, length 0 */ \
1565 1 + /* MIN tag is length with zero, only length */ \
1566 3 + /* version, opcount, opcode */ \
1567 XDR_QUADLEN(NFS4_MAX_SESSIONID_LEN) + \
1568 /* seqid, slotID, slotID, cache */ \
1569 4 ) * sizeof(__be32))
1570
1571 #define NFSD_MIN_RESP_HDR_SEQ_SZ ((\
1572 2 + /* verifier: AUTH_NULL, length 0 */\
1573 1 + /* status */ \
1574 1 + /* MIN tag is length with zero, only length */ \
1575 3 + /* opcount, opcode, opstatus*/ \
1576 XDR_QUADLEN(NFS4_MAX_SESSIONID_LEN) + \
1577 /* seqid, slotID, slotID, slotID, status */ \
1578 5 ) * sizeof(__be32))
1579
1580 static __be32 check_forechannel_attrs(struct nfsd4_channel_attrs fchannel)
1581 {
1582 return fchannel.maxreq_sz < NFSD_MIN_REQ_HDR_SEQ_SZ
1583 || fchannel.maxresp_sz < NFSD_MIN_RESP_HDR_SEQ_SZ;
1584 }
1585
1586 __be32
1587 nfsd4_create_session(struct svc_rqst *rqstp,
1588 struct nfsd4_compound_state *cstate,
1589 struct nfsd4_create_session *cr_ses)
1590 {
1591 struct sockaddr *sa = svc_addr(rqstp);
1592 struct nfs4_client *conf, *unconf;
1593 struct nfsd4_session *new;
1594 struct nfsd4_clid_slot *cs_slot = NULL;
1595 bool confirm_me = false;
1596 int status = 0;
1597
1598 if (cr_ses->flags & ~SESSION4_FLAG_MASK_A)
1599 return nfserr_inval;
1600
1601 nfs4_lock_state();
1602 unconf = find_unconfirmed_client(&cr_ses->clientid);
1603 conf = find_confirmed_client(&cr_ses->clientid);
1604
1605 if (conf) {
1606 cs_slot = &conf->cl_cs_slot;
1607 status = check_slot_seqid(cr_ses->seqid, cs_slot->sl_seqid, 0);
1608 if (status == nfserr_replay_cache) {
1609 dprintk("Got a create_session replay! seqid= %d\n",
1610 cs_slot->sl_seqid);
1611 /* Return the cached reply status */
1612 status = nfsd4_replay_create_session(cr_ses, cs_slot);
1613 goto out;
1614 } else if (cr_ses->seqid != cs_slot->sl_seqid + 1) {
1615 status = nfserr_seq_misordered;
1616 dprintk("Sequence misordered!\n");
1617 dprintk("Expected seqid= %d but got seqid= %d\n",
1618 cs_slot->sl_seqid, cr_ses->seqid);
1619 goto out;
1620 }
1621 } else if (unconf) {
1622 if (!same_creds(&unconf->cl_cred, &rqstp->rq_cred) ||
1623 !rpc_cmp_addr(sa, (struct sockaddr *) &unconf->cl_addr)) {
1624 status = nfserr_clid_inuse;
1625 goto out;
1626 }
1627
1628 cs_slot = &unconf->cl_cs_slot;
1629 status = check_slot_seqid(cr_ses->seqid, cs_slot->sl_seqid, 0);
1630 if (status) {
1631 /* an unconfirmed replay returns misordered */
1632 status = nfserr_seq_misordered;
1633 goto out;
1634 }
1635
1636 confirm_me = true;
1637 conf = unconf;
1638 } else {
1639 status = nfserr_stale_clientid;
1640 goto out;
1641 }
1642
1643 /*
1644 * XXX: we should probably set this at creation time, and check
1645 * for consistent minorversion use throughout:
1646 */
1647 conf->cl_minorversion = 1;
1648 /*
1649 * We do not support RDMA or persistent sessions
1650 */
1651 cr_ses->flags &= ~SESSION4_PERSIST;
1652 cr_ses->flags &= ~SESSION4_RDMA;
1653
1654 status = nfserr_toosmall;
1655 if (check_forechannel_attrs(cr_ses->fore_channel))
1656 goto out;
1657
1658 status = nfserr_jukebox;
1659 new = alloc_init_session(rqstp, conf, cr_ses);
1660 if (!new)
1661 goto out;
1662 status = nfs_ok;
1663 memcpy(cr_ses->sessionid.data, new->se_sessionid.data,
1664 NFS4_MAX_SESSIONID_LEN);
1665 memcpy(&cr_ses->fore_channel, &new->se_fchannel,
1666 sizeof(struct nfsd4_channel_attrs));
1667 cs_slot->sl_seqid++;
1668 cr_ses->seqid = cs_slot->sl_seqid;
1669
1670 /* cache solo and embedded create sessions under the state lock */
1671 nfsd4_cache_create_session(cr_ses, cs_slot, status);
1672 if (confirm_me)
1673 move_to_confirmed(conf);
1674 out:
1675 nfs4_unlock_state();
1676 dprintk("%s returns %d\n", __func__, ntohl(status));
1677 return status;
1678 }
1679
1680 static bool nfsd4_last_compound_op(struct svc_rqst *rqstp)
1681 {
1682 struct nfsd4_compoundres *resp = rqstp->rq_resp;
1683 struct nfsd4_compoundargs *argp = rqstp->rq_argp;
1684
1685 return argp->opcnt == resp->opcnt;
1686 }
1687
1688 static __be32 nfsd4_map_bcts_dir(u32 *dir)
1689 {
1690 switch (*dir) {
1691 case NFS4_CDFC4_FORE:
1692 case NFS4_CDFC4_BACK:
1693 return nfs_ok;
1694 case NFS4_CDFC4_FORE_OR_BOTH:
1695 case NFS4_CDFC4_BACK_OR_BOTH:
1696 *dir = NFS4_CDFC4_BOTH;
1697 return nfs_ok;
1698 };
1699 return nfserr_inval;
1700 }
1701
1702 __be32 nfsd4_bind_conn_to_session(struct svc_rqst *rqstp,
1703 struct nfsd4_compound_state *cstate,
1704 struct nfsd4_bind_conn_to_session *bcts)
1705 {
1706 __be32 status;
1707
1708 if (!nfsd4_last_compound_op(rqstp))
1709 return nfserr_not_only_op;
1710 spin_lock(&client_lock);
1711 cstate->session = find_in_sessionid_hashtbl(&bcts->sessionid);
1712 /* Sorta weird: we only need the refcnt'ing because new_conn acquires
1713 * client_lock iself: */
1714 if (cstate->session) {
1715 nfsd4_get_session(cstate->session);
1716 atomic_inc(&cstate->session->se_client->cl_refcount);
1717 }
1718 spin_unlock(&client_lock);
1719 if (!cstate->session)
1720 return nfserr_badsession;
1721
1722 status = nfsd4_map_bcts_dir(&bcts->dir);
1723 if (!status)
1724 nfsd4_new_conn(rqstp, cstate->session, bcts->dir);
1725 return status;
1726 }
1727
1728 static bool nfsd4_compound_in_session(struct nfsd4_session *session, struct nfs4_sessionid *sid)
1729 {
1730 if (!session)
1731 return 0;
1732 return !memcmp(sid, &session->se_sessionid, sizeof(*sid));
1733 }
1734
1735 __be32
1736 nfsd4_destroy_session(struct svc_rqst *r,
1737 struct nfsd4_compound_state *cstate,
1738 struct nfsd4_destroy_session *sessionid)
1739 {
1740 struct nfsd4_session *ses;
1741 u32 status = nfserr_badsession;
1742
1743 /* Notes:
1744 * - The confirmed nfs4_client->cl_sessionid holds destroyed sessinid
1745 * - Should we return nfserr_back_chan_busy if waiting for
1746 * callbacks on to-be-destroyed session?
1747 * - Do we need to clear any callback info from previous session?
1748 */
1749
1750 if (nfsd4_compound_in_session(cstate->session, &sessionid->sessionid)) {
1751 if (!nfsd4_last_compound_op(r))
1752 return nfserr_not_only_op;
1753 }
1754 dump_sessionid(__func__, &sessionid->sessionid);
1755 spin_lock(&client_lock);
1756 ses = find_in_sessionid_hashtbl(&sessionid->sessionid);
1757 if (!ses) {
1758 spin_unlock(&client_lock);
1759 goto out;
1760 }
1761
1762 unhash_session(ses);
1763 spin_unlock(&client_lock);
1764
1765 nfs4_lock_state();
1766 nfsd4_probe_callback_sync(ses->se_client);
1767 nfs4_unlock_state();
1768
1769 nfsd4_del_conns(ses);
1770
1771 nfsd4_put_session(ses);
1772 status = nfs_ok;
1773 out:
1774 dprintk("%s returns %d\n", __func__, ntohl(status));
1775 return status;
1776 }
1777
1778 static struct nfsd4_conn *__nfsd4_find_conn(struct svc_xprt *xpt, struct nfsd4_session *s)
1779 {
1780 struct nfsd4_conn *c;
1781
1782 list_for_each_entry(c, &s->se_conns, cn_persession) {
1783 if (c->cn_xprt == xpt) {
1784 return c;
1785 }
1786 }
1787 return NULL;
1788 }
1789
1790 static void nfsd4_sequence_check_conn(struct nfsd4_conn *new, struct nfsd4_session *ses)
1791 {
1792 struct nfs4_client *clp = ses->se_client;
1793 struct nfsd4_conn *c;
1794 int ret;
1795
1796 spin_lock(&clp->cl_lock);
1797 c = __nfsd4_find_conn(new->cn_xprt, ses);
1798 if (c) {
1799 spin_unlock(&clp->cl_lock);
1800 free_conn(new);
1801 return;
1802 }
1803 __nfsd4_hash_conn(new, ses);
1804 spin_unlock(&clp->cl_lock);
1805 ret = nfsd4_register_conn(new);
1806 if (ret)
1807 /* oops; xprt is already down: */
1808 nfsd4_conn_lost(&new->cn_xpt_user);
1809 return;
1810 }
1811
1812 static bool nfsd4_session_too_many_ops(struct svc_rqst *rqstp, struct nfsd4_session *session)
1813 {
1814 struct nfsd4_compoundargs *args = rqstp->rq_argp;
1815
1816 return args->opcnt > session->se_fchannel.maxops;
1817 }
1818
1819 static bool nfsd4_request_too_big(struct svc_rqst *rqstp,
1820 struct nfsd4_session *session)
1821 {
1822 struct xdr_buf *xb = &rqstp->rq_arg;
1823
1824 return xb->len > session->se_fchannel.maxreq_sz;
1825 }
1826
1827 __be32
1828 nfsd4_sequence(struct svc_rqst *rqstp,
1829 struct nfsd4_compound_state *cstate,
1830 struct nfsd4_sequence *seq)
1831 {
1832 struct nfsd4_compoundres *resp = rqstp->rq_resp;
1833 struct nfsd4_session *session;
1834 struct nfsd4_slot *slot;
1835 struct nfsd4_conn *conn;
1836 int status;
1837
1838 if (resp->opcnt != 1)
1839 return nfserr_sequence_pos;
1840
1841 /*
1842 * Will be either used or freed by nfsd4_sequence_check_conn
1843 * below.
1844 */
1845 conn = alloc_conn(rqstp, NFS4_CDFC4_FORE);
1846 if (!conn)
1847 return nfserr_jukebox;
1848
1849 spin_lock(&client_lock);
1850 status = nfserr_badsession;
1851 session = find_in_sessionid_hashtbl(&seq->sessionid);
1852 if (!session)
1853 goto out;
1854
1855 status = nfserr_too_many_ops;
1856 if (nfsd4_session_too_many_ops(rqstp, session))
1857 goto out;
1858
1859 status = nfserr_req_too_big;
1860 if (nfsd4_request_too_big(rqstp, session))
1861 goto out;
1862
1863 status = nfserr_badslot;
1864 if (seq->slotid >= session->se_fchannel.maxreqs)
1865 goto out;
1866
1867 slot = session->se_slots[seq->slotid];
1868 dprintk("%s: slotid %d\n", __func__, seq->slotid);
1869
1870 /* We do not negotiate the number of slots yet, so set the
1871 * maxslots to the session maxreqs which is used to encode
1872 * sr_highest_slotid and the sr_target_slot id to maxslots */
1873 seq->maxslots = session->se_fchannel.maxreqs;
1874
1875 status = check_slot_seqid(seq->seqid, slot->sl_seqid, slot->sl_inuse);
1876 if (status == nfserr_replay_cache) {
1877 cstate->slot = slot;
1878 cstate->session = session;
1879 /* Return the cached reply status and set cstate->status
1880 * for nfsd4_proc_compound processing */
1881 status = nfsd4_replay_cache_entry(resp, seq);
1882 cstate->status = nfserr_replay_cache;
1883 goto out;
1884 }
1885 if (status)
1886 goto out;
1887
1888 nfsd4_sequence_check_conn(conn, session);
1889 conn = NULL;
1890
1891 /* Success! bump slot seqid */
1892 slot->sl_inuse = true;
1893 slot->sl_seqid = seq->seqid;
1894 slot->sl_cachethis = seq->cachethis;
1895
1896 cstate->slot = slot;
1897 cstate->session = session;
1898
1899 out:
1900 /* Hold a session reference until done processing the compound. */
1901 if (cstate->session) {
1902 struct nfs4_client *clp = session->se_client;
1903
1904 nfsd4_get_session(cstate->session);
1905 atomic_inc(&clp->cl_refcount);
1906 if (clp->cl_cb_state == NFSD4_CB_DOWN)
1907 seq->status_flags |= SEQ4_STATUS_CB_PATH_DOWN;
1908 }
1909 kfree(conn);
1910 spin_unlock(&client_lock);
1911 dprintk("%s: return %d\n", __func__, ntohl(status));
1912 return status;
1913 }
1914
1915 __be32
1916 nfsd4_reclaim_complete(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate, struct nfsd4_reclaim_complete *rc)
1917 {
1918 int status = 0;
1919
1920 if (rc->rca_one_fs) {
1921 if (!cstate->current_fh.fh_dentry)
1922 return nfserr_nofilehandle;
1923 /*
1924 * We don't take advantage of the rca_one_fs case.
1925 * That's OK, it's optional, we can safely ignore it.
1926 */
1927 return nfs_ok;
1928 }
1929
1930 nfs4_lock_state();
1931 status = nfserr_complete_already;
1932 if (cstate->session->se_client->cl_firststate)
1933 goto out;
1934
1935 status = nfserr_stale_clientid;
1936 if (is_client_expired(cstate->session->se_client))
1937 /*
1938 * The following error isn't really legal.
1939 * But we only get here if the client just explicitly
1940 * destroyed the client. Surely it no longer cares what
1941 * error it gets back on an operation for the dead
1942 * client.
1943 */
1944 goto out;
1945
1946 status = nfs_ok;
1947 nfsd4_create_clid_dir(cstate->session->se_client);
1948 out:
1949 nfs4_unlock_state();
1950 return status;
1951 }
1952
1953 __be32
1954 nfsd4_setclientid(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
1955 struct nfsd4_setclientid *setclid)
1956 {
1957 struct xdr_netobj clname = {
1958 .len = setclid->se_namelen,
1959 .data = setclid->se_name,
1960 };
1961 nfs4_verifier clverifier = setclid->se_verf;
1962 unsigned int strhashval;
1963 struct nfs4_client *conf, *unconf, *new;
1964 __be32 status;
1965 char dname[HEXDIR_LEN];
1966
1967 if (!check_name(clname))
1968 return nfserr_inval;
1969
1970 status = nfs4_make_rec_clidname(dname, &clname);
1971 if (status)
1972 return status;
1973
1974 /*
1975 * XXX The Duplicate Request Cache (DRC) has been checked (??)
1976 * We get here on a DRC miss.
1977 */
1978
1979 strhashval = clientstr_hashval(dname);
1980
1981 nfs4_lock_state();
1982 conf = find_confirmed_client_by_str(dname, strhashval);
1983 if (conf) {
1984 /* RFC 3530 14.2.33 CASE 0: */
1985 status = nfserr_clid_inuse;
1986 if (clp_used_exchangeid(conf))
1987 goto out;
1988 if (!same_creds(&conf->cl_cred, &rqstp->rq_cred)) {
1989 char addr_str[INET6_ADDRSTRLEN];
1990 rpc_ntop((struct sockaddr *) &conf->cl_addr, addr_str,
1991 sizeof(addr_str));
1992 dprintk("NFSD: setclientid: string in use by client "
1993 "at %s\n", addr_str);
1994 goto out;
1995 }
1996 }
1997 /*
1998 * section 14.2.33 of RFC 3530 (under the heading "IMPLEMENTATION")
1999 * has a description of SETCLIENTID request processing consisting
2000 * of 5 bullet points, labeled as CASE0 - CASE4 below.
2001 */
2002 unconf = find_unconfirmed_client_by_str(dname, strhashval);
2003 status = nfserr_jukebox;
2004 if (!conf) {
2005 /*
2006 * RFC 3530 14.2.33 CASE 4:
2007 * placed first, because it is the normal case
2008 */
2009 if (unconf)
2010 expire_client(unconf);
2011 new = create_client(clname, dname, rqstp, &clverifier);
2012 if (new == NULL)
2013 goto out;
2014 gen_clid(new);
2015 } else if (same_verf(&conf->cl_verifier, &clverifier)) {
2016 /*
2017 * RFC 3530 14.2.33 CASE 1:
2018 * probable callback update
2019 */
2020 if (unconf) {
2021 /* Note this is removing unconfirmed {*x***},
2022 * which is stronger than RFC recommended {vxc**}.
2023 * This has the advantage that there is at most
2024 * one {*x***} in either list at any time.
2025 */
2026 expire_client(unconf);
2027 }
2028 new = create_client(clname, dname, rqstp, &clverifier);
2029 if (new == NULL)
2030 goto out;
2031 copy_clid(new, conf);
2032 } else if (!unconf) {
2033 /*
2034 * RFC 3530 14.2.33 CASE 2:
2035 * probable client reboot; state will be removed if
2036 * confirmed.
2037 */
2038 new = create_client(clname, dname, rqstp, &clverifier);
2039 if (new == NULL)
2040 goto out;
2041 gen_clid(new);
2042 } else {
2043 /*
2044 * RFC 3530 14.2.33 CASE 3:
2045 * probable client reboot; state will be removed if
2046 * confirmed.
2047 */
2048 expire_client(unconf);
2049 new = create_client(clname, dname, rqstp, &clverifier);
2050 if (new == NULL)
2051 goto out;
2052 gen_clid(new);
2053 }
2054 /*
2055 * XXX: we should probably set this at creation time, and check
2056 * for consistent minorversion use throughout:
2057 */
2058 new->cl_minorversion = 0;
2059 gen_callback(new, setclid, rqstp);
2060 add_to_unconfirmed(new, strhashval);
2061 setclid->se_clientid.cl_boot = new->cl_clientid.cl_boot;
2062 setclid->se_clientid.cl_id = new->cl_clientid.cl_id;
2063 memcpy(setclid->se_confirm.data, new->cl_confirm.data, sizeof(setclid->se_confirm.data));
2064 status = nfs_ok;
2065 out:
2066 nfs4_unlock_state();
2067 return status;
2068 }
2069
2070
2071 /*
2072 * Section 14.2.34 of RFC 3530 (under the heading "IMPLEMENTATION") has
2073 * a description of SETCLIENTID_CONFIRM request processing consisting of 4
2074 * bullets, labeled as CASE1 - CASE4 below.
2075 */
2076 __be32
2077 nfsd4_setclientid_confirm(struct svc_rqst *rqstp,
2078 struct nfsd4_compound_state *cstate,
2079 struct nfsd4_setclientid_confirm *setclientid_confirm)
2080 {
2081 struct sockaddr *sa = svc_addr(rqstp);
2082 struct nfs4_client *conf, *unconf;
2083 nfs4_verifier confirm = setclientid_confirm->sc_confirm;
2084 clientid_t * clid = &setclientid_confirm->sc_clientid;
2085 __be32 status;
2086
2087 if (STALE_CLIENTID(clid))
2088 return nfserr_stale_clientid;
2089 /*
2090 * XXX The Duplicate Request Cache (DRC) has been checked (??)
2091 * We get here on a DRC miss.
2092 */
2093
2094 nfs4_lock_state();
2095
2096 conf = find_confirmed_client(clid);
2097 unconf = find_unconfirmed_client(clid);
2098
2099 status = nfserr_clid_inuse;
2100 if (conf && !rpc_cmp_addr((struct sockaddr *) &conf->cl_addr, sa))
2101 goto out;
2102 if (unconf && !rpc_cmp_addr((struct sockaddr *) &unconf->cl_addr, sa))
2103 goto out;
2104
2105 /*
2106 * section 14.2.34 of RFC 3530 has a description of
2107 * SETCLIENTID_CONFIRM request processing consisting
2108 * of 4 bullet points, labeled as CASE1 - CASE4 below.
2109 */
2110 if (conf && unconf && same_verf(&confirm, &unconf->cl_confirm)) {
2111 /*
2112 * RFC 3530 14.2.34 CASE 1:
2113 * callback update
2114 */
2115 if (!same_creds(&conf->cl_cred, &unconf->cl_cred))
2116 status = nfserr_clid_inuse;
2117 else {
2118 nfsd4_change_callback(conf, &unconf->cl_cb_conn);
2119 nfsd4_probe_callback(conf);
2120 expire_client(unconf);
2121 status = nfs_ok;
2122
2123 }
2124 } else if (conf && !unconf) {
2125 /*
2126 * RFC 3530 14.2.34 CASE 2:
2127 * probable retransmitted request; play it safe and
2128 * do nothing.
2129 */
2130 if (!same_creds(&conf->cl_cred, &rqstp->rq_cred))
2131 status = nfserr_clid_inuse;
2132 else
2133 status = nfs_ok;
2134 } else if (!conf && unconf
2135 && same_verf(&unconf->cl_confirm, &confirm)) {
2136 /*
2137 * RFC 3530 14.2.34 CASE 3:
2138 * Normal case; new or rebooted client:
2139 */
2140 if (!same_creds(&unconf->cl_cred, &rqstp->rq_cred)) {
2141 status = nfserr_clid_inuse;
2142 } else {
2143 unsigned int hash =
2144 clientstr_hashval(unconf->cl_recdir);
2145 conf = find_confirmed_client_by_str(unconf->cl_recdir,
2146 hash);
2147 if (conf) {
2148 nfsd4_remove_clid_dir(conf);
2149 expire_client(conf);
2150 }
2151 move_to_confirmed(unconf);
2152 conf = unconf;
2153 nfsd4_probe_callback(conf);
2154 status = nfs_ok;
2155 }
2156 } else if ((!conf || (conf && !same_verf(&conf->cl_confirm, &confirm)))
2157 && (!unconf || (unconf && !same_verf(&unconf->cl_confirm,
2158 &confirm)))) {
2159 /*
2160 * RFC 3530 14.2.34 CASE 4:
2161 * Client probably hasn't noticed that we rebooted yet.
2162 */
2163 status = nfserr_stale_clientid;
2164 } else {
2165 /* check that we have hit one of the cases...*/
2166 status = nfserr_clid_inuse;
2167 }
2168 out:
2169 nfs4_unlock_state();
2170 return status;
2171 }
2172
2173 /* OPEN Share state helper functions */
2174 static inline struct nfs4_file *
2175 alloc_init_file(struct inode *ino)
2176 {
2177 struct nfs4_file *fp;
2178 unsigned int hashval = file_hashval(ino);
2179
2180 fp = kmem_cache_alloc(file_slab, GFP_KERNEL);
2181 if (fp) {
2182 atomic_set(&fp->fi_ref, 1);
2183 INIT_LIST_HEAD(&fp->fi_hash);
2184 INIT_LIST_HEAD(&fp->fi_stateids);
2185 INIT_LIST_HEAD(&fp->fi_delegations);
2186 fp->fi_inode = igrab(ino);
2187 fp->fi_id = current_fileid++;
2188 fp->fi_had_conflict = false;
2189 fp->fi_lease = NULL;
2190 memset(fp->fi_fds, 0, sizeof(fp->fi_fds));
2191 memset(fp->fi_access, 0, sizeof(fp->fi_access));
2192 spin_lock(&recall_lock);
2193 list_add(&fp->fi_hash, &file_hashtbl[hashval]);
2194 spin_unlock(&recall_lock);
2195 return fp;
2196 }
2197 return NULL;
2198 }
2199
2200 static void
2201 nfsd4_free_slab(struct kmem_cache **slab)
2202 {
2203 if (*slab == NULL)
2204 return;
2205 kmem_cache_destroy(*slab);
2206 *slab = NULL;
2207 }
2208
2209 void
2210 nfsd4_free_slabs(void)
2211 {
2212 nfsd4_free_slab(&openowner_slab);
2213 nfsd4_free_slab(&lockowner_slab);
2214 nfsd4_free_slab(&file_slab);
2215 nfsd4_free_slab(&stateid_slab);
2216 nfsd4_free_slab(&deleg_slab);
2217 }
2218
2219 static int
2220 nfsd4_init_slabs(void)
2221 {
2222 openowner_slab = kmem_cache_create("nfsd4_openowners",
2223 sizeof(struct nfs4_openowner), 0, 0, NULL);
2224 if (openowner_slab == NULL)
2225 goto out_nomem;
2226 lockowner_slab = kmem_cache_create("nfsd4_lockowners",
2227 sizeof(struct nfs4_openowner), 0, 0, NULL);
2228 if (lockowner_slab == NULL)
2229 goto out_nomem;
2230 file_slab = kmem_cache_create("nfsd4_files",
2231 sizeof(struct nfs4_file), 0, 0, NULL);
2232 if (file_slab == NULL)
2233 goto out_nomem;
2234 stateid_slab = kmem_cache_create("nfsd4_stateids",
2235 sizeof(struct nfs4_ol_stateid), 0, 0, NULL);
2236 if (stateid_slab == NULL)
2237 goto out_nomem;
2238 deleg_slab = kmem_cache_create("nfsd4_delegations",
2239 sizeof(struct nfs4_delegation), 0, 0, NULL);
2240 if (deleg_slab == NULL)
2241 goto out_nomem;
2242 return 0;
2243 out_nomem:
2244 nfsd4_free_slabs();
2245 dprintk("nfsd4: out of memory while initializing nfsv4\n");
2246 return -ENOMEM;
2247 }
2248
2249 void nfs4_free_openowner(struct nfs4_openowner *oo)
2250 {
2251 kfree(oo->oo_owner.so_owner.data);
2252 kmem_cache_free(openowner_slab, oo);
2253 }
2254
2255 void nfs4_free_lockowner(struct nfs4_lockowner *lo)
2256 {
2257 kfree(lo->lo_owner.so_owner.data);
2258 kmem_cache_free(lockowner_slab, lo);
2259 }
2260
2261 static void init_nfs4_replay(struct nfs4_replay *rp)
2262 {
2263 rp->rp_status = nfserr_serverfault;
2264 rp->rp_buflen = 0;
2265 rp->rp_buf = rp->rp_ibuf;
2266 }
2267
2268 static inline void *alloc_stateowner(struct kmem_cache *slab, struct xdr_netobj *owner, struct nfs4_client *clp)
2269 {
2270 struct nfs4_stateowner *sop;
2271
2272 sop = kmem_cache_alloc(slab, GFP_KERNEL);
2273 if (!sop)
2274 return NULL;
2275
2276 sop->so_owner.data = kmemdup(owner->data, owner->len, GFP_KERNEL);
2277 if (!sop->so_owner.data) {
2278 kmem_cache_free(slab, sop);
2279 return NULL;
2280 }
2281 sop->so_owner.len = owner->len;
2282
2283 INIT_LIST_HEAD(&sop->so_stateids);
2284 sop->so_id = current_ownerid++;
2285 sop->so_client = clp;
2286 init_nfs4_replay(&sop->so_replay);
2287 return sop;
2288 }
2289
2290 static void hash_openowner(struct nfs4_openowner *oo, struct nfs4_client *clp, unsigned int strhashval)
2291 {
2292 unsigned int idhashval;
2293
2294 idhashval = open_ownerid_hashval(oo->oo_owner.so_id);
2295 list_add(&oo->oo_owner.so_idhash, &open_ownerid_hashtbl[idhashval]);
2296 list_add(&oo->oo_owner.so_strhash, &open_ownerstr_hashtbl[strhashval]);
2297 list_add(&oo->oo_perclient, &clp->cl_openowners);
2298 }
2299
2300 static struct nfs4_openowner *
2301 alloc_init_open_stateowner(unsigned int strhashval, struct nfs4_client *clp, struct nfsd4_open *open) {
2302 struct nfs4_openowner *oo;
2303
2304 oo = alloc_stateowner(openowner_slab, &open->op_owner, clp);
2305 if (!oo)
2306 return NULL;
2307 oo->oo_owner.so_is_open_owner = 1;
2308 oo->oo_owner.so_seqid = open->op_seqid;
2309 oo->oo_confirmed = 0;
2310 oo->oo_time = 0;
2311 INIT_LIST_HEAD(&oo->oo_close_lru);
2312 hash_openowner(oo, clp, strhashval);
2313 return oo;
2314 }
2315
2316 static inline void
2317 init_open_stateid(struct nfs4_ol_stateid *stp, struct nfs4_file *fp, struct nfsd4_open *open) {
2318 struct nfs4_openowner *oo = open->op_openowner;
2319 unsigned int hashval = stateid_hashval(oo->oo_owner.so_id, fp->fi_id);
2320
2321 INIT_LIST_HEAD(&stp->st_lockowners);
2322 list_add(&stp->st_stid.sc_hash, &stateid_hashtbl[hashval]);
2323 list_add(&stp->st_perstateowner, &oo->oo_owner.so_stateids);
2324 list_add(&stp->st_perfile, &fp->fi_stateids);
2325 stp->st_stid.sc_type = NFS4_OPEN_STID;
2326 stp->st_stateowner = &oo->oo_owner;
2327 get_nfs4_file(fp);
2328 stp->st_file = fp;
2329 stp->st_stid.sc_stateid.si_boot = boot_time;
2330 stp->st_stid.sc_stateid.si_stateownerid = oo->oo_owner.so_id;
2331 stp->st_stid.sc_stateid.si_fileid = fp->fi_id;
2332 /* note will be incremented before first return to client: */
2333 stp->st_stid.sc_stateid.si_generation = 0;
2334 stp->st_access_bmap = 0;
2335 stp->st_deny_bmap = 0;
2336 __set_bit(open->op_share_access & ~NFS4_SHARE_WANT_MASK,
2337 &stp->st_access_bmap);
2338 __set_bit(open->op_share_deny, &stp->st_deny_bmap);
2339 stp->st_openstp = NULL;
2340 }
2341
2342 static void
2343 move_to_close_lru(struct nfs4_openowner *oo)
2344 {
2345 dprintk("NFSD: move_to_close_lru nfs4_openowner %p\n", oo);
2346
2347 list_move_tail(&oo->oo_close_lru, &close_lru);
2348 oo->oo_time = get_seconds();
2349 }
2350
2351 static int
2352 same_owner_str(struct nfs4_stateowner *sop, struct xdr_netobj *owner,
2353 clientid_t *clid)
2354 {
2355 return (sop->so_owner.len == owner->len) &&
2356 0 == memcmp(sop->so_owner.data, owner->data, owner->len) &&
2357 (sop->so_client->cl_clientid.cl_id == clid->cl_id);
2358 }
2359
2360 static struct nfs4_openowner *
2361 find_openstateowner_str(unsigned int hashval, struct nfsd4_open *open)
2362 {
2363 struct nfs4_stateowner *so = NULL;
2364
2365 list_for_each_entry(so, &open_ownerstr_hashtbl[hashval], so_strhash) {
2366 if (same_owner_str(so, &open->op_owner, &open->op_clientid))
2367 return container_of(so, struct nfs4_openowner, oo_owner);
2368 }
2369 return NULL;
2370 }
2371
2372 /* search file_hashtbl[] for file */
2373 static struct nfs4_file *
2374 find_file(struct inode *ino)
2375 {
2376 unsigned int hashval = file_hashval(ino);
2377 struct nfs4_file *fp;
2378
2379 spin_lock(&recall_lock);
2380 list_for_each_entry(fp, &file_hashtbl[hashval], fi_hash) {
2381 if (fp->fi_inode == ino) {
2382 get_nfs4_file(fp);
2383 spin_unlock(&recall_lock);
2384 return fp;
2385 }
2386 }
2387 spin_unlock(&recall_lock);
2388 return NULL;
2389 }
2390
2391 static inline int access_valid(u32 x, u32 minorversion)
2392 {
2393 if ((x & NFS4_SHARE_ACCESS_MASK) < NFS4_SHARE_ACCESS_READ)
2394 return 0;
2395 if ((x & NFS4_SHARE_ACCESS_MASK) > NFS4_SHARE_ACCESS_BOTH)
2396 return 0;
2397 x &= ~NFS4_SHARE_ACCESS_MASK;
2398 if (minorversion && x) {
2399 if ((x & NFS4_SHARE_WANT_MASK) > NFS4_SHARE_WANT_CANCEL)
2400 return 0;
2401 if ((x & NFS4_SHARE_WHEN_MASK) > NFS4_SHARE_PUSH_DELEG_WHEN_UNCONTENDED)
2402 return 0;
2403 x &= ~(NFS4_SHARE_WANT_MASK | NFS4_SHARE_WHEN_MASK);
2404 }
2405 if (x)
2406 return 0;
2407 return 1;
2408 }
2409
2410 static inline int deny_valid(u32 x)
2411 {
2412 /* Note: unlike access bits, deny bits may be zero. */
2413 return x <= NFS4_SHARE_DENY_BOTH;
2414 }
2415
2416 /*
2417 * Called to check deny when READ with all zero stateid or
2418 * WRITE with all zero or all one stateid
2419 */
2420 static __be32
2421 nfs4_share_conflict(struct svc_fh *current_fh, unsigned int deny_type)
2422 {
2423 struct inode *ino = current_fh->fh_dentry->d_inode;
2424 struct nfs4_file *fp;
2425 struct nfs4_ol_stateid *stp;
2426 __be32 ret;
2427
2428 dprintk("NFSD: nfs4_share_conflict\n");
2429
2430 fp = find_file(ino);
2431 if (!fp)
2432 return nfs_ok;
2433 ret = nfserr_locked;
2434 /* Search for conflicting share reservations */
2435 list_for_each_entry(stp, &fp->fi_stateids, st_perfile) {
2436 if (test_bit(deny_type, &stp->st_deny_bmap) ||
2437 test_bit(NFS4_SHARE_DENY_BOTH, &stp->st_deny_bmap))
2438 goto out;
2439 }
2440 ret = nfs_ok;
2441 out:
2442 put_nfs4_file(fp);
2443 return ret;
2444 }
2445
2446 static void nfsd_break_one_deleg(struct nfs4_delegation *dp)
2447 {
2448 /* We're assuming the state code never drops its reference
2449 * without first removing the lease. Since we're in this lease
2450 * callback (and since the lease code is serialized by the kernel
2451 * lock) we know the server hasn't removed the lease yet, we know
2452 * it's safe to take a reference: */
2453 atomic_inc(&dp->dl_count);
2454
2455 list_add_tail(&dp->dl_recall_lru, &del_recall_lru);
2456
2457 /* only place dl_time is set. protected by lock_flocks*/
2458 dp->dl_time = get_seconds();
2459
2460 nfsd4_cb_recall(dp);
2461 }
2462
2463 /* Called from break_lease() with lock_flocks() held. */
2464 static void nfsd_break_deleg_cb(struct file_lock *fl)
2465 {
2466 struct nfs4_file *fp = (struct nfs4_file *)fl->fl_owner;
2467 struct nfs4_delegation *dp;
2468
2469 BUG_ON(!fp);
2470 /* We assume break_lease is only called once per lease: */
2471 BUG_ON(fp->fi_had_conflict);
2472 /*
2473 * We don't want the locks code to timeout the lease for us;
2474 * we'll remove it ourself if a delegation isn't returned
2475 * in time:
2476 */
2477 fl->fl_break_time = 0;
2478
2479 spin_lock(&recall_lock);
2480 fp->fi_had_conflict = true;
2481 list_for_each_entry(dp, &fp->fi_delegations, dl_perfile)
2482 nfsd_break_one_deleg(dp);
2483 spin_unlock(&recall_lock);
2484 }
2485
2486 static
2487 int nfsd_change_deleg_cb(struct file_lock **onlist, int arg)
2488 {
2489 if (arg & F_UNLCK)
2490 return lease_modify(onlist, arg);
2491 else
2492 return -EAGAIN;
2493 }
2494
2495 static const struct lock_manager_operations nfsd_lease_mng_ops = {
2496 .lm_break = nfsd_break_deleg_cb,
2497 .lm_change = nfsd_change_deleg_cb,
2498 };
2499
2500 static __be32 nfsd4_check_seqid(struct nfsd4_compound_state *cstate, struct nfs4_stateowner *so, u32 seqid)
2501 {
2502 if (nfsd4_has_session(cstate))
2503 return nfs_ok;
2504 if (seqid == so->so_seqid - 1)
2505 return nfserr_replay_me;
2506 if (seqid == so->so_seqid)
2507 return nfs_ok;
2508 return nfserr_bad_seqid;
2509 }
2510
2511 __be32
2512 nfsd4_process_open1(struct nfsd4_compound_state *cstate,
2513 struct nfsd4_open *open)
2514 {
2515 clientid_t *clientid = &open->op_clientid;
2516 struct nfs4_client *clp = NULL;
2517 unsigned int strhashval;
2518 struct nfs4_openowner *oo = NULL;
2519 __be32 status;
2520
2521 if (!check_name(open->op_owner))
2522 return nfserr_inval;
2523
2524 if (STALE_CLIENTID(&open->op_clientid))
2525 return nfserr_stale_clientid;
2526
2527 strhashval = open_ownerstr_hashval(clientid->cl_id, &open->op_owner);
2528 oo = find_openstateowner_str(strhashval, open);
2529 open->op_openowner = oo;
2530 if (!oo) {
2531 /* Make sure the client's lease hasn't expired. */
2532 clp = find_confirmed_client(clientid);
2533 if (clp == NULL)
2534 return nfserr_expired;
2535 goto renew;
2536 }
2537 if (!oo->oo_confirmed) {
2538 /* Replace unconfirmed owners without checking for replay. */
2539 clp = oo->oo_owner.so_client;
2540 release_openowner(oo);
2541 open->op_openowner = NULL;
2542 goto renew;
2543 }
2544 status = nfsd4_check_seqid(cstate, &oo->oo_owner, open->op_seqid);
2545 if (status)
2546 return status;
2547 renew:
2548 if (open->op_openowner == NULL) {
2549 oo = alloc_init_open_stateowner(strhashval, clp, open);
2550 if (oo == NULL)
2551 return nfserr_jukebox;
2552 open->op_openowner = oo;
2553 }
2554 list_del_init(&oo->oo_close_lru);
2555 renew_client(oo->oo_owner.so_client);
2556 return nfs_ok;
2557 }
2558
2559 static inline __be32
2560 nfs4_check_delegmode(struct nfs4_delegation *dp, int flags)
2561 {
2562 if ((flags & WR_STATE) && (dp->dl_type == NFS4_OPEN_DELEGATE_READ))
2563 return nfserr_openmode;
2564 else
2565 return nfs_ok;
2566 }
2567
2568 static struct nfs4_delegation *
2569 find_delegation_file(struct nfs4_file *fp, stateid_t *stid)
2570 {
2571 struct nfs4_delegation *dp;
2572
2573 spin_lock(&recall_lock);
2574 list_for_each_entry(dp, &fp->fi_delegations, dl_perfile)
2575 if (dp->dl_stateid.si_stateownerid == stid->si_stateownerid) {
2576 spin_unlock(&recall_lock);
2577 return dp;
2578 }
2579 spin_unlock(&recall_lock);
2580 return NULL;
2581 }
2582
2583 static int share_access_to_flags(u32 share_access)
2584 {
2585 share_access &= ~NFS4_SHARE_WANT_MASK;
2586
2587 return share_access == NFS4_SHARE_ACCESS_READ ? RD_STATE : WR_STATE;
2588 }
2589
2590 static __be32
2591 nfs4_check_deleg(struct nfs4_file *fp, struct nfsd4_open *open,
2592 struct nfs4_delegation **dp)
2593 {
2594 int flags;
2595 __be32 status = nfserr_bad_stateid;
2596
2597 *dp = find_delegation_file(fp, &open->op_delegate_stateid);
2598 if (*dp == NULL)
2599 goto out;
2600 flags = share_access_to_flags(open->op_share_access);
2601 status = nfs4_check_delegmode(*dp, flags);
2602 if (status)
2603 *dp = NULL;
2604 out:
2605 if (open->op_claim_type != NFS4_OPEN_CLAIM_DELEGATE_CUR)
2606 return nfs_ok;
2607 if (status)
2608 return status;
2609 open->op_openowner->oo_confirmed = 1;
2610 return nfs_ok;
2611 }
2612
2613 static __be32
2614 nfs4_check_open(struct nfs4_file *fp, struct nfsd4_open *open, struct nfs4_ol_stateid **stpp)
2615 {
2616 struct nfs4_ol_stateid *local;
2617 struct nfs4_openowner *oo = open->op_openowner;
2618
2619 list_for_each_entry(local, &fp->fi_stateids, st_perfile) {
2620 /* ignore lock owners */
2621 if (local->st_stateowner->so_is_open_owner == 0)
2622 continue;
2623 /* remember if we have seen this open owner */
2624 if (local->st_stateowner == &oo->oo_owner)
2625 *stpp = local;
2626 /* check for conflicting share reservations */
2627 if (!test_share(local, open))
2628 return nfserr_share_denied;
2629 }
2630 return nfs_ok;
2631 }
2632
2633 static inline struct nfs4_ol_stateid *
2634 nfs4_alloc_stateid(void)
2635 {
2636 return kmem_cache_alloc(stateid_slab, GFP_KERNEL);
2637 }
2638
2639 static inline int nfs4_access_to_access(u32 nfs4_access)
2640 {
2641 int flags = 0;
2642
2643 if (nfs4_access & NFS4_SHARE_ACCESS_READ)
2644 flags |= NFSD_MAY_READ;
2645 if (nfs4_access & NFS4_SHARE_ACCESS_WRITE)
2646 flags |= NFSD_MAY_WRITE;
2647 return flags;
2648 }
2649
2650 static __be32 nfs4_get_vfs_file(struct svc_rqst *rqstp, struct nfs4_file *fp,
2651 struct svc_fh *cur_fh, struct nfsd4_open *open)
2652 {
2653 __be32 status;
2654 int oflag = nfs4_access_to_omode(open->op_share_access);
2655 int access = nfs4_access_to_access(open->op_share_access);
2656
2657 /* CLAIM_DELEGATE_CUR is used in response to a broken lease;
2658 * allowing it to break the lease and return EAGAIN leaves the
2659 * client unable to make progress in returning the delegation */
2660 if (open->op_claim_type == NFS4_OPEN_CLAIM_DELEGATE_CUR)
2661 access |= NFSD_MAY_NOT_BREAK_LEASE;
2662
2663 if (!fp->fi_fds[oflag]) {
2664 status = nfsd_open(rqstp, cur_fh, S_IFREG, access,
2665 &fp->fi_fds[oflag]);
2666 if (status)
2667 return status;
2668 }
2669 nfs4_file_get_access(fp, oflag);
2670
2671 return nfs_ok;
2672 }
2673
2674 static __be32
2675 nfs4_new_open(struct svc_rqst *rqstp, struct nfs4_ol_stateid **stpp,
2676 struct nfs4_file *fp, struct svc_fh *cur_fh,
2677 struct nfsd4_open *open)
2678 {
2679 struct nfs4_ol_stateid *stp;
2680 __be32 status;
2681
2682 stp = nfs4_alloc_stateid();
2683 if (stp == NULL)
2684 return nfserr_jukebox;
2685
2686 status = nfs4_get_vfs_file(rqstp, fp, cur_fh, open);
2687 if (status) {
2688 kmem_cache_free(stateid_slab, stp);
2689 return status;
2690 }
2691 *stpp = stp;
2692 return 0;
2693 }
2694
2695 static inline __be32
2696 nfsd4_truncate(struct svc_rqst *rqstp, struct svc_fh *fh,
2697 struct nfsd4_open *open)
2698 {
2699 struct iattr iattr = {
2700 .ia_valid = ATTR_SIZE,
2701 .ia_size = 0,
2702 };
2703 if (!open->op_truncate)
2704 return 0;
2705 if (!(open->op_share_access & NFS4_SHARE_ACCESS_WRITE))
2706 return nfserr_inval;
2707 return nfsd_setattr(rqstp, fh, &iattr, 0, (time_t)0);
2708 }
2709
2710 static __be32
2711 nfs4_upgrade_open(struct svc_rqst *rqstp, struct nfs4_file *fp, struct svc_fh *cur_fh, struct nfs4_ol_stateid *stp, struct nfsd4_open *open)
2712 {
2713 u32 op_share_access = open->op_share_access & ~NFS4_SHARE_WANT_MASK;
2714 bool new_access;
2715 __be32 status;
2716
2717 new_access = !test_bit(op_share_access, &stp->st_access_bmap);
2718 if (new_access) {
2719 status = nfs4_get_vfs_file(rqstp, fp, cur_fh, open);
2720 if (status)
2721 return status;
2722 }
2723 status = nfsd4_truncate(rqstp, cur_fh, open);
2724 if (status) {
2725 if (new_access) {
2726 int oflag = nfs4_access_to_omode(op_share_access);
2727 nfs4_file_put_access(fp, oflag);
2728 }
2729 return status;
2730 }
2731 /* remember the open */
2732 __set_bit(op_share_access, &stp->st_access_bmap);
2733 __set_bit(open->op_share_deny, &stp->st_deny_bmap);
2734
2735 return nfs_ok;
2736 }
2737
2738
2739 static void
2740 nfs4_set_claim_prev(struct nfsd4_open *open)
2741 {
2742 open->op_openowner->oo_confirmed = 1;
2743 open->op_openowner->oo_owner.so_client->cl_firststate = 1;
2744 }
2745
2746 /* Should we give out recallable state?: */
2747 static bool nfsd4_cb_channel_good(struct nfs4_client *clp)
2748 {
2749 if (clp->cl_cb_state == NFSD4_CB_UP)
2750 return true;
2751 /*
2752 * In the sessions case, since we don't have to establish a
2753 * separate connection for callbacks, we assume it's OK
2754 * until we hear otherwise:
2755 */
2756 return clp->cl_minorversion && clp->cl_cb_state == NFSD4_CB_UNKNOWN;
2757 }
2758
2759 static struct file_lock *nfs4_alloc_init_lease(struct nfs4_delegation *dp, int flag)
2760 {
2761 struct file_lock *fl;
2762
2763 fl = locks_alloc_lock();
2764 if (!fl)
2765 return NULL;
2766 locks_init_lock(fl);
2767 fl->fl_lmops = &nfsd_lease_mng_ops;
2768 fl->fl_flags = FL_LEASE;
2769 fl->fl_type = flag == NFS4_OPEN_DELEGATE_READ? F_RDLCK: F_WRLCK;
2770 fl->fl_end = OFFSET_MAX;
2771 fl->fl_owner = (fl_owner_t)(dp->dl_file);
2772 fl->fl_pid = current->tgid;
2773 return fl;
2774 }
2775
2776 static int nfs4_setlease(struct nfs4_delegation *dp, int flag)
2777 {
2778 struct nfs4_file *fp = dp->dl_file;
2779 struct file_lock *fl;
2780 int status;
2781
2782 fl = nfs4_alloc_init_lease(dp, flag);
2783 if (!fl)
2784 return -ENOMEM;
2785 fl->fl_file = find_readable_file(fp);
2786 list_add(&dp->dl_perclnt, &dp->dl_client->cl_delegations);
2787 status = vfs_setlease(fl->fl_file, fl->fl_type, &fl);
2788 if (status) {
2789 list_del_init(&dp->dl_perclnt);
2790 locks_free_lock(fl);
2791 return -ENOMEM;
2792 }
2793 fp->fi_lease = fl;
2794 fp->fi_deleg_file = fl->fl_file;
2795 get_file(fp->fi_deleg_file);
2796 atomic_set(&fp->fi_delegees, 1);
2797 list_add(&dp->dl_perfile, &fp->fi_delegations);
2798 return 0;
2799 }
2800
2801 static int nfs4_set_delegation(struct nfs4_delegation *dp, int flag)
2802 {
2803 struct nfs4_file *fp = dp->dl_file;
2804
2805 if (!fp->fi_lease)
2806 return nfs4_setlease(dp, flag);
2807 spin_lock(&recall_lock);
2808 if (fp->fi_had_conflict) {
2809 spin_unlock(&recall_lock);
2810 return -EAGAIN;
2811 }
2812 atomic_inc(&fp->fi_delegees);
2813 list_add(&dp->dl_perfile, &fp->fi_delegations);
2814 spin_unlock(&recall_lock);
2815 list_add(&dp->dl_perclnt, &dp->dl_client->cl_delegations);
2816 return 0;
2817 }
2818
2819 /*
2820 * Attempt to hand out a delegation.
2821 */
2822 static void
2823 nfs4_open_delegation(struct svc_fh *fh, struct nfsd4_open *open, struct nfs4_ol_stateid *stp)
2824 {
2825 struct nfs4_delegation *dp;
2826 struct nfs4_openowner *oo = container_of(stp->st_stateowner, struct nfs4_openowner, oo_owner);
2827 int cb_up;
2828 int status, flag = 0;
2829
2830 cb_up = nfsd4_cb_channel_good(oo->oo_owner.so_client);
2831 flag = NFS4_OPEN_DELEGATE_NONE;
2832 open->op_recall = 0;
2833 switch (open->op_claim_type) {
2834 case NFS4_OPEN_CLAIM_PREVIOUS:
2835 if (!cb_up)
2836 open->op_recall = 1;
2837 flag = open->op_delegate_type;
2838 if (flag == NFS4_OPEN_DELEGATE_NONE)
2839 goto out;
2840 break;
2841 case NFS4_OPEN_CLAIM_NULL:
2842 /* Let's not give out any delegations till everyone's
2843 * had the chance to reclaim theirs.... */
2844 if (locks_in_grace())
2845 goto out;
2846 if (!cb_up || !oo->oo_confirmed)
2847 goto out;
2848 if (open->op_share_access & NFS4_SHARE_ACCESS_WRITE)
2849 flag = NFS4_OPEN_DELEGATE_WRITE;
2850 else
2851 flag = NFS4_OPEN_DELEGATE_READ;
2852 break;
2853 default:
2854 goto out;
2855 }
2856
2857 dp = alloc_init_deleg(oo->oo_owner.so_client, stp, fh, flag);
2858 if (dp == NULL)
2859 goto out_no_deleg;
2860 status = nfs4_set_delegation(dp, flag);
2861 if (status)
2862 goto out_free;
2863
2864 memcpy(&open->op_delegate_stateid, &dp->dl_stateid, sizeof(dp->dl_stateid));
2865
2866 dprintk("NFSD: delegation stateid=" STATEID_FMT "\n",
2867 STATEID_VAL(&dp->dl_stateid));
2868 out:
2869 if (open->op_claim_type == NFS4_OPEN_CLAIM_PREVIOUS
2870 && flag == NFS4_OPEN_DELEGATE_NONE
2871 && open->op_delegate_type != NFS4_OPEN_DELEGATE_NONE)
2872 dprintk("NFSD: WARNING: refusing delegation reclaim\n");
2873 open->op_delegate_type = flag;
2874 return;
2875 out_free:
2876 nfs4_put_delegation(dp);
2877 out_no_deleg:
2878 flag = NFS4_OPEN_DELEGATE_NONE;
2879 goto out;
2880 }
2881
2882 /*
2883 * called with nfs4_lock_state() held.
2884 */
2885 __be32
2886 nfsd4_process_open2(struct svc_rqst *rqstp, struct svc_fh *current_fh, struct nfsd4_open *open)
2887 {
2888 struct nfsd4_compoundres *resp = rqstp->rq_resp;
2889 struct nfs4_file *fp = NULL;
2890 struct inode *ino = current_fh->fh_dentry->d_inode;
2891 struct nfs4_ol_stateid *stp = NULL;
2892 struct nfs4_delegation *dp = NULL;
2893 __be32 status;
2894
2895 status = nfserr_inval;
2896 if (!access_valid(open->op_share_access, resp->cstate.minorversion)
2897 || !deny_valid(open->op_share_deny))
2898 goto out;
2899 /*
2900 * Lookup file; if found, lookup stateid and check open request,
2901 * and check for delegations in the process of being recalled.
2902 * If not found, create the nfs4_file struct
2903 */
2904 fp = find_file(ino);
2905 if (fp) {
2906 if ((status = nfs4_check_open(fp, open, &stp)))
2907 goto out;
2908 status = nfs4_check_deleg(fp, open, &dp);
2909 if (status)
2910 goto out;
2911 } else {
2912 status = nfserr_bad_stateid;
2913 if (open->op_claim_type == NFS4_OPEN_CLAIM_DELEGATE_CUR)
2914 goto out;
2915 status = nfserr_jukebox;
2916 fp = alloc_init_file(ino);
2917 if (fp == NULL)
2918 goto out;
2919 }
2920
2921 /*
2922 * OPEN the file, or upgrade an existing OPEN.
2923 * If truncate fails, the OPEN fails.
2924 */
2925 if (stp) {
2926 /* Stateid was found, this is an OPEN upgrade */
2927 status = nfs4_upgrade_open(rqstp, fp, current_fh, stp, open);
2928 if (status)
2929 goto out;
2930 } else {
2931 status = nfs4_new_open(rqstp, &stp, fp, current_fh, open);
2932 if (status)
2933 goto out;
2934 init_open_stateid(stp, fp, open);
2935 status = nfsd4_truncate(rqstp, current_fh, open);
2936 if (status) {
2937 release_open_stateid(stp);
2938 goto out;
2939 }
2940 }
2941 update_stateid(&stp->st_stid.sc_stateid);
2942 memcpy(&open->op_stateid, &stp->st_stid.sc_stateid, sizeof(stateid_t));
2943
2944 if (nfsd4_has_session(&resp->cstate))
2945 open->op_openowner->oo_confirmed = 1;
2946
2947 /*
2948 * Attempt to hand out a delegation. No error return, because the
2949 * OPEN succeeds even if we fail.
2950 */
2951 nfs4_open_delegation(current_fh, open, stp);
2952
2953 status = nfs_ok;
2954
2955 dprintk("%s: stateid=" STATEID_FMT "\n", __func__,
2956 STATEID_VAL(&stp->st_stid.sc_stateid));
2957 out:
2958 if (fp)
2959 put_nfs4_file(fp);
2960 if (status == 0 && open->op_claim_type == NFS4_OPEN_CLAIM_PREVIOUS)
2961 nfs4_set_claim_prev(open);
2962 /*
2963 * To finish the open response, we just need to set the rflags.
2964 */
2965 open->op_rflags = NFS4_OPEN_RESULT_LOCKTYPE_POSIX;
2966 if (!open->op_openowner->oo_confirmed &&
2967 !nfsd4_has_session(&resp->cstate))
2968 open->op_rflags |= NFS4_OPEN_RESULT_CONFIRM;
2969
2970 return status;
2971 }
2972
2973 __be32
2974 nfsd4_renew(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
2975 clientid_t *clid)
2976 {
2977 struct nfs4_client *clp;
2978 __be32 status;
2979
2980 nfs4_lock_state();
2981 dprintk("process_renew(%08x/%08x): starting\n",
2982 clid->cl_boot, clid->cl_id);
2983 status = nfserr_stale_clientid;
2984 if (STALE_CLIENTID(clid))
2985 goto out;
2986 clp = find_confirmed_client(clid);
2987 status = nfserr_expired;
2988 if (clp == NULL) {
2989 /* We assume the client took too long to RENEW. */
2990 dprintk("nfsd4_renew: clientid not found!\n");
2991 goto out;
2992 }
2993 renew_client(clp);
2994 status = nfserr_cb_path_down;
2995 if (!list_empty(&clp->cl_delegations)
2996 && clp->cl_cb_state != NFSD4_CB_UP)
2997 goto out;
2998 status = nfs_ok;
2999 out:
3000 nfs4_unlock_state();
3001 return status;
3002 }
3003
3004 static struct lock_manager nfsd4_manager = {
3005 };
3006
3007 static void
3008 nfsd4_end_grace(void)
3009 {
3010 dprintk("NFSD: end of grace period\n");
3011 nfsd4_recdir_purge_old();
3012 locks_end_grace(&nfsd4_manager);
3013 /*
3014 * Now that every NFSv4 client has had the chance to recover and
3015 * to see the (possibly new, possibly shorter) lease time, we
3016 * can safely set the next grace time to the current lease time:
3017 */
3018 nfsd4_grace = nfsd4_lease;
3019 }
3020
3021 static time_t
3022 nfs4_laundromat(void)
3023 {
3024 struct nfs4_client *clp;
3025 struct nfs4_openowner *oo;
3026 struct nfs4_delegation *dp;
3027 struct list_head *pos, *next, reaplist;
3028 time_t cutoff = get_seconds() - nfsd4_lease;
3029 time_t t, clientid_val = nfsd4_lease;
3030 time_t u, test_val = nfsd4_lease;
3031
3032 nfs4_lock_state();
3033
3034 dprintk("NFSD: laundromat service - starting\n");
3035 if (locks_in_grace())
3036 nfsd4_end_grace();
3037 INIT_LIST_HEAD(&reaplist);
3038 spin_lock(&client_lock);
3039 list_for_each_safe(pos, next, &client_lru) {
3040 clp = list_entry(pos, struct nfs4_client, cl_lru);
3041 if (time_after((unsigned long)clp->cl_time, (unsigned long)cutoff)) {
3042 t = clp->cl_time - cutoff;
3043 if (clientid_val > t)
3044 clientid_val = t;
3045 break;
3046 }
3047 if (atomic_read(&clp->cl_refcount)) {
3048 dprintk("NFSD: client in use (clientid %08x)\n",
3049 clp->cl_clientid.cl_id);
3050 continue;
3051 }
3052 unhash_client_locked(clp);
3053 list_add(&clp->cl_lru, &reaplist);
3054 }
3055 spin_unlock(&client_lock);
3056 list_for_each_safe(pos, next, &reaplist) {
3057 clp = list_entry(pos, struct nfs4_client, cl_lru);
3058 dprintk("NFSD: purging unused client (clientid %08x)\n",
3059 clp->cl_clientid.cl_id);
3060 nfsd4_remove_clid_dir(clp);
3061 expire_client(clp);
3062 }
3063 spin_lock(&recall_lock);
3064 list_for_each_safe(pos, next, &del_recall_lru) {
3065 dp = list_entry (pos, struct nfs4_delegation, dl_recall_lru);
3066 if (time_after((unsigned long)dp->dl_time, (unsigned long)cutoff)) {
3067 u = dp->dl_time - cutoff;
3068 if (test_val > u)
3069 test_val = u;
3070 break;
3071 }
3072 list_move(&dp->dl_recall_lru, &reaplist);
3073 }
3074 spin_unlock(&recall_lock);
3075 list_for_each_safe(pos, next, &reaplist) {
3076 dp = list_entry (pos, struct nfs4_delegation, dl_recall_lru);
3077 list_del_init(&dp->dl_recall_lru);
3078 unhash_delegation(dp);
3079 }
3080 test_val = nfsd4_lease;
3081 list_for_each_safe(pos, next, &close_lru) {
3082 oo = container_of(pos, struct nfs4_openowner, oo_close_lru);
3083 if (time_after((unsigned long)oo->oo_time, (unsigned long)cutoff)) {
3084 u = oo->oo_time - cutoff;
3085 if (test_val > u)
3086 test_val = u;
3087 break;
3088 }
3089 dprintk("NFSD: purging unused open stateowner (so_id %d)\n",
3090 oo->oo_owner.so_id);
3091 release_openowner(oo);
3092 }
3093 if (clientid_val < NFSD_LAUNDROMAT_MINTIMEOUT)
3094 clientid_val = NFSD_LAUNDROMAT_MINTIMEOUT;
3095 nfs4_unlock_state();
3096 return clientid_val;
3097 }
3098
3099 static struct workqueue_struct *laundry_wq;
3100 static void laundromat_main(struct work_struct *);
3101 static DECLARE_DELAYED_WORK(laundromat_work, laundromat_main);
3102
3103 static void
3104 laundromat_main(struct work_struct *not_used)
3105 {
3106 time_t t;
3107
3108 t = nfs4_laundromat();
3109 dprintk("NFSD: laundromat_main - sleeping for %ld seconds\n", t);
3110 queue_delayed_work(laundry_wq, &laundromat_work, t*HZ);
3111 }
3112
3113 static struct nfs4_openowner * search_close_lru(u32 st_id)
3114 {
3115 struct nfs4_openowner *local;
3116
3117 list_for_each_entry(local, &close_lru, oo_close_lru) {
3118 if (local->oo_owner.so_id == st_id)
3119 return local;
3120 }
3121 return NULL;
3122 }
3123
3124 static inline int
3125 nfs4_check_fh(struct svc_fh *fhp, struct nfs4_ol_stateid *stp)
3126 {
3127 return fhp->fh_dentry->d_inode != stp->st_file->fi_inode;
3128 }
3129
3130 static int
3131 STALE_STATEID(stateid_t *stateid)
3132 {
3133 if (stateid->si_boot == boot_time)
3134 return 0;
3135 dprintk("NFSD: stale stateid " STATEID_FMT "!\n",
3136 STATEID_VAL(stateid));
3137 return 1;
3138 }
3139
3140 static inline int
3141 access_permit_read(unsigned long access_bmap)
3142 {
3143 return test_bit(NFS4_SHARE_ACCESS_READ, &access_bmap) ||
3144 test_bit(NFS4_SHARE_ACCESS_BOTH, &access_bmap) ||
3145 test_bit(NFS4_SHARE_ACCESS_WRITE, &access_bmap);
3146 }
3147
3148 static inline int
3149 access_permit_write(unsigned long access_bmap)
3150 {
3151 return test_bit(NFS4_SHARE_ACCESS_WRITE, &access_bmap) ||
3152 test_bit(NFS4_SHARE_ACCESS_BOTH, &access_bmap);
3153 }
3154
3155 static
3156 __be32 nfs4_check_openmode(struct nfs4_ol_stateid *stp, int flags)
3157 {
3158 __be32 status = nfserr_openmode;
3159
3160 /* For lock stateid's, we test the parent open, not the lock: */
3161 if (stp->st_openstp)
3162 stp = stp->st_openstp;
3163 if ((flags & WR_STATE) && (!access_permit_write(stp->st_access_bmap)))
3164 goto out;
3165 if ((flags & RD_STATE) && (!access_permit_read(stp->st_access_bmap)))
3166 goto out;
3167 status = nfs_ok;
3168 out:
3169 return status;
3170 }
3171
3172 static inline __be32
3173 check_special_stateids(svc_fh *current_fh, stateid_t *stateid, int flags)
3174 {
3175 if (ONE_STATEID(stateid) && (flags & RD_STATE))
3176 return nfs_ok;
3177 else if (locks_in_grace()) {
3178 /* Answer in remaining cases depends on existence of
3179 * conflicting state; so we must wait out the grace period. */
3180 return nfserr_grace;
3181 } else if (flags & WR_STATE)
3182 return nfs4_share_conflict(current_fh,
3183 NFS4_SHARE_DENY_WRITE);
3184 else /* (flags & RD_STATE) && ZERO_STATEID(stateid) */
3185 return nfs4_share_conflict(current_fh,
3186 NFS4_SHARE_DENY_READ);
3187 }
3188
3189 /*
3190 * Allow READ/WRITE during grace period on recovered state only for files
3191 * that are not able to provide mandatory locking.
3192 */
3193 static inline int
3194 grace_disallows_io(struct inode *inode)
3195 {
3196 return locks_in_grace() && mandatory_lock(inode);
3197 }
3198
3199 /* Returns true iff a is later than b: */
3200 static bool stateid_generation_after(stateid_t *a, stateid_t *b)
3201 {
3202 return (s32)a->si_generation - (s32)b->si_generation > 0;
3203 }
3204
3205 static int check_stateid_generation(stateid_t *in, stateid_t *ref, bool has_session)
3206 {
3207 /*
3208 * When sessions are used the stateid generation number is ignored
3209 * when it is zero.
3210 */
3211 if (has_session && in->si_generation == 0)
3212 return nfs_ok;
3213
3214 if (in->si_generation == ref->si_generation)
3215 return nfs_ok;
3216
3217 /* If the client sends us a stateid from the future, it's buggy: */
3218 if (stateid_generation_after(in, ref))
3219 return nfserr_bad_stateid;
3220 /*
3221 * However, we could see a stateid from the past, even from a
3222 * non-buggy client. For example, if the client sends a lock
3223 * while some IO is outstanding, the lock may bump si_generation
3224 * while the IO is still in flight. The client could avoid that
3225 * situation by waiting for responses on all the IO requests,
3226 * but better performance may result in retrying IO that
3227 * receives an old_stateid error if requests are rarely
3228 * reordered in flight:
3229 */
3230 return nfserr_old_stateid;
3231 }
3232
3233 static int is_delegation_stateid(stateid_t *stateid)
3234 {
3235 return stateid->si_fileid == 0;
3236 }
3237
3238 __be32 nfs4_validate_stateid(stateid_t *stateid, bool has_session)
3239 {
3240 struct nfs4_ol_stateid *stp = NULL;
3241 __be32 status = nfserr_stale_stateid;
3242
3243 if (STALE_STATEID(stateid))
3244 goto out;
3245
3246 status = nfserr_expired;
3247 stp = find_stateid(stateid);
3248 if (!stp)
3249 goto out;
3250 status = nfserr_bad_stateid;
3251 if (stp->st_stateowner->so_is_open_owner
3252 && !openowner(stp->st_stateowner)->oo_confirmed)
3253 goto out;
3254
3255 status = check_stateid_generation(stateid, &stp->st_stid.sc_stateid, has_session);
3256 if (status)
3257 goto out;
3258
3259 status = nfs_ok;
3260 out:
3261 return status;
3262 }
3263
3264 /*
3265 * Checks for stateid operations
3266 */
3267 __be32
3268 nfs4_preprocess_stateid_op(struct nfsd4_compound_state *cstate,
3269 stateid_t *stateid, int flags, struct file **filpp)
3270 {
3271 struct nfs4_ol_stateid *stp = NULL;
3272 struct nfs4_delegation *dp = NULL;
3273 struct svc_fh *current_fh = &cstate->current_fh;
3274 struct inode *ino = current_fh->fh_dentry->d_inode;
3275 __be32 status;
3276
3277 if (filpp)
3278 *filpp = NULL;
3279
3280 if (grace_disallows_io(ino))
3281 return nfserr_grace;
3282
3283 if (ZERO_STATEID(stateid) || ONE_STATEID(stateid))
3284 return check_special_stateids(current_fh, stateid, flags);
3285
3286 status = nfserr_stale_stateid;
3287 if (STALE_STATEID(stateid))
3288 goto out;
3289
3290 /*
3291 * We assume that any stateid that has the current boot time,
3292 * but that we can't find, is expired:
3293 */
3294 status = nfserr_expired;
3295 if (is_delegation_stateid(stateid)) {
3296 dp = find_delegation_stateid(ino, stateid);
3297 if (!dp)
3298 goto out;
3299 status = check_stateid_generation(stateid, &dp->dl_stateid, nfsd4_has_session(cstate));
3300 if (status)
3301 goto out;
3302 status = nfs4_check_delegmode(dp, flags);
3303 if (status)
3304 goto out;
3305 renew_client(dp->dl_client);
3306 if (filpp) {
3307 *filpp = dp->dl_file->fi_deleg_file;
3308 BUG_ON(!*filpp);
3309 }
3310 } else { /* open or lock stateid */
3311 stp = find_stateid(stateid);
3312 if (!stp)
3313 goto out;
3314 status = nfserr_bad_stateid;
3315 if (nfs4_check_fh(current_fh, stp))
3316 goto out;
3317 if (stp->st_stateowner->so_is_open_owner
3318 && !openowner(stp->st_stateowner)->oo_confirmed)
3319 goto out;
3320 status = check_stateid_generation(stateid, &stp->st_stid.sc_stateid,
3321 nfsd4_has_session(cstate));
3322 if (status)
3323 goto out;
3324 status = nfs4_check_openmode(stp, flags);
3325 if (status)
3326 goto out;
3327 renew_client(stp->st_stateowner->so_client);
3328 if (filpp) {
3329 if (flags & RD_STATE)
3330 *filpp = find_readable_file(stp->st_file);
3331 else
3332 *filpp = find_writeable_file(stp->st_file);
3333 }
3334 }
3335 status = nfs_ok;
3336 out:
3337 return status;
3338 }
3339
3340 static __be32
3341 nfsd4_free_delegation_stateid(stateid_t *stateid)
3342 {
3343 struct nfs4_delegation *dp = search_for_delegation(stateid);
3344 if (dp)
3345 return nfserr_locks_held;
3346 return nfserr_bad_stateid;
3347 }
3348
3349 static __be32
3350 nfsd4_free_lock_stateid(struct nfs4_ol_stateid *stp)
3351 {
3352 if (check_for_locks(stp->st_file, lockowner(stp->st_stateowner)))
3353 return nfserr_locks_held;
3354 release_lock_stateid(stp);
3355 return nfs_ok;
3356 }
3357
3358 /*
3359 * Test if the stateid is valid
3360 */
3361 __be32
3362 nfsd4_test_stateid(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
3363 struct nfsd4_test_stateid *test_stateid)
3364 {
3365 test_stateid->ts_has_session = nfsd4_has_session(cstate);
3366 return nfs_ok;
3367 }
3368
3369 /*
3370 * Free a state id
3371 */
3372 __be32
3373 nfsd4_free_stateid(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
3374 struct nfsd4_free_stateid *free_stateid)
3375 {
3376 stateid_t *stateid = &free_stateid->fr_stateid;
3377 struct nfs4_ol_stateid *stp;
3378 __be32 ret;
3379
3380 nfs4_lock_state();
3381 if (is_delegation_stateid(stateid)) {
3382 ret = nfsd4_free_delegation_stateid(stateid);
3383 goto out;
3384 }
3385
3386 stp = find_stateid(stateid);
3387 if (!stp) {
3388 ret = nfserr_bad_stateid;
3389 goto out;
3390 }
3391 ret = check_stateid_generation(stateid, &stp->st_stid.sc_stateid, 1);
3392 if (ret)
3393 goto out;
3394
3395 if (stp->st_stid.sc_type == NFS4_OPEN_STID) {
3396 ret = nfserr_locks_held;
3397 goto out;
3398 } else {
3399 ret = nfsd4_free_lock_stateid(stp);
3400 goto out;
3401 }
3402
3403 out:
3404 nfs4_unlock_state();
3405 return ret;
3406 }
3407
3408 static inline int
3409 setlkflg (int type)
3410 {
3411 return (type == NFS4_READW_LT || type == NFS4_READ_LT) ?
3412 RD_STATE : WR_STATE;
3413 }
3414
3415 static __be32 nfs4_nospecial_stateid_checks(stateid_t *stateid)
3416 {
3417 if (ZERO_STATEID(stateid) || ONE_STATEID(stateid))
3418 return nfserr_bad_stateid;
3419 if (STALE_STATEID(stateid))
3420 return nfserr_stale_stateid;
3421 return nfs_ok;
3422 }
3423
3424 static __be32 nfs4_seqid_op_checks(struct nfsd4_compound_state *cstate, stateid_t *stateid, u32 seqid, struct nfs4_ol_stateid *stp)
3425 {
3426 struct svc_fh *current_fh = &cstate->current_fh;
3427 struct nfs4_stateowner *sop = stp->st_stateowner;
3428 __be32 status;
3429
3430 if (nfs4_check_fh(current_fh, stp))
3431 return nfserr_bad_stateid;
3432 status = nfsd4_check_seqid(cstate, sop, seqid);
3433 if (status)
3434 return status;
3435 return check_stateid_generation(stateid, &stp->st_stid.sc_stateid, nfsd4_has_session(cstate));
3436 }
3437
3438 /*
3439 * Checks for sequence id mutating operations.
3440 */
3441 static __be32
3442 nfs4_preprocess_seqid_op(struct nfsd4_compound_state *cstate, u32 seqid,
3443 stateid_t *stateid, char typemask,
3444 struct nfs4_ol_stateid **stpp)
3445 {
3446 __be32 status;
3447
3448 dprintk("NFSD: %s: seqid=%d stateid = " STATEID_FMT "\n", __func__,
3449 seqid, STATEID_VAL(stateid));
3450
3451 *stpp = NULL;
3452 status = nfs4_nospecial_stateid_checks(stateid);
3453 if (status)
3454 return status;
3455 *stpp = find_stateid_by_type(stateid, typemask);
3456 if (*stpp == NULL)
3457 return nfserr_expired;
3458 cstate->replay_owner = (*stpp)->st_stateowner;
3459 renew_client((*stpp)->st_stateowner->so_client);
3460
3461 return nfs4_seqid_op_checks(cstate, stateid, seqid, *stpp);
3462 }
3463
3464 static __be32 nfs4_preprocess_confirmed_seqid_op(struct nfsd4_compound_state *cstate, u32 seqid, stateid_t *stateid, struct nfs4_ol_stateid **stpp)
3465 {
3466 __be32 status;
3467 struct nfs4_openowner *oo;
3468
3469 status = nfs4_preprocess_seqid_op(cstate, seqid, stateid,
3470 NFS4_OPEN_STID, stpp);
3471 if (status)
3472 return status;
3473 oo = openowner((*stpp)->st_stateowner);
3474 if (!oo->oo_confirmed)
3475 return nfserr_bad_stateid;
3476 return nfs_ok;
3477 }
3478
3479 __be32
3480 nfsd4_open_confirm(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
3481 struct nfsd4_open_confirm *oc)
3482 {
3483 __be32 status;
3484 struct nfs4_openowner *oo;
3485 struct nfs4_ol_stateid *stp;
3486
3487 dprintk("NFSD: nfsd4_open_confirm on file %.*s\n",
3488 (int)cstate->current_fh.fh_dentry->d_name.len,
3489 cstate->current_fh.fh_dentry->d_name.name);
3490
3491 status = fh_verify(rqstp, &cstate->current_fh, S_IFREG, 0);
3492 if (status)
3493 return status;
3494
3495 nfs4_lock_state();
3496
3497 status = nfs4_preprocess_seqid_op(cstate,
3498 oc->oc_seqid, &oc->oc_req_stateid,
3499 NFS4_OPEN_STID, &stp);
3500 if (status)
3501 goto out;
3502 oo = openowner(stp->st_stateowner);
3503 status = nfserr_bad_stateid;
3504 if (oo->oo_confirmed)
3505 goto out;
3506 oo->oo_confirmed = 1;
3507 update_stateid(&stp->st_stid.sc_stateid);
3508 memcpy(&oc->oc_resp_stateid, &stp->st_stid.sc_stateid, sizeof(stateid_t));
3509 dprintk("NFSD: %s: success, seqid=%d stateid=" STATEID_FMT "\n",
3510 __func__, oc->oc_seqid, STATEID_VAL(&stp->st_stid.sc_stateid));
3511
3512 nfsd4_create_clid_dir(oo->oo_owner.so_client);
3513 status = nfs_ok;
3514 out:
3515 if (!cstate->replay_owner)
3516 nfs4_unlock_state();
3517 return status;
3518 }
3519
3520 static inline void nfs4_file_downgrade(struct nfs4_ol_stateid *stp, unsigned int to_access)
3521 {
3522 int i;
3523
3524 for (i = 1; i < 4; i++) {
3525 if (test_bit(i, &stp->st_access_bmap) && !(i & to_access)) {
3526 nfs4_file_put_access(stp->st_file, i);
3527 __clear_bit(i, &stp->st_access_bmap);
3528 }
3529 }
3530 }
3531
3532 static void
3533 reset_union_bmap_deny(unsigned long deny, unsigned long *bmap)
3534 {
3535 int i;
3536 for (i = 0; i < 4; i++) {
3537 if ((i & deny) != i)
3538 __clear_bit(i, bmap);
3539 }
3540 }
3541
3542 __be32
3543 nfsd4_open_downgrade(struct svc_rqst *rqstp,
3544 struct nfsd4_compound_state *cstate,
3545 struct nfsd4_open_downgrade *od)
3546 {
3547 __be32 status;
3548 struct nfs4_ol_stateid *stp;
3549
3550 dprintk("NFSD: nfsd4_open_downgrade on file %.*s\n",
3551 (int)cstate->current_fh.fh_dentry->d_name.len,
3552 cstate->current_fh.fh_dentry->d_name.name);
3553
3554 if (!access_valid(od->od_share_access, cstate->minorversion)
3555 || !deny_valid(od->od_share_deny))
3556 return nfserr_inval;
3557
3558 nfs4_lock_state();
3559 status = nfs4_preprocess_confirmed_seqid_op(cstate, od->od_seqid,
3560 &od->od_stateid, &stp);
3561 if (status)
3562 goto out;
3563 status = nfserr_inval;
3564 if (!test_bit(od->od_share_access, &stp->st_access_bmap)) {
3565 dprintk("NFSD:access not a subset current bitmap: 0x%lx, input access=%08x\n",
3566 stp->st_access_bmap, od->od_share_access);
3567 goto out;
3568 }
3569 if (!test_bit(od->od_share_deny, &stp->st_deny_bmap)) {
3570 dprintk("NFSD:deny not a subset current bitmap: 0x%lx, input deny=%08x\n",
3571 stp->st_deny_bmap, od->od_share_deny);
3572 goto out;
3573 }
3574 nfs4_file_downgrade(stp, od->od_share_access);
3575
3576 reset_union_bmap_deny(od->od_share_deny, &stp->st_deny_bmap);
3577
3578 update_stateid(&stp->st_stid.sc_stateid);
3579 memcpy(&od->od_stateid, &stp->st_stid.sc_stateid, sizeof(stateid_t));
3580 status = nfs_ok;
3581 out:
3582 if (!cstate->replay_owner)
3583 nfs4_unlock_state();
3584 return status;
3585 }
3586
3587 /*
3588 * nfs4_unlock_state() called after encode
3589 */
3590 __be32
3591 nfsd4_close(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
3592 struct nfsd4_close *close)
3593 {
3594 __be32 status;
3595 struct nfs4_openowner *oo;
3596 struct nfs4_ol_stateid *stp;
3597
3598 dprintk("NFSD: nfsd4_close on file %.*s\n",
3599 (int)cstate->current_fh.fh_dentry->d_name.len,
3600 cstate->current_fh.fh_dentry->d_name.name);
3601
3602 nfs4_lock_state();
3603 /* check close_lru for replay */
3604 status = nfs4_preprocess_confirmed_seqid_op(cstate, close->cl_seqid,
3605 &close->cl_stateid, &stp);
3606 if (stp == NULL && status == nfserr_expired) {
3607 /*
3608 * Also, we should make sure this isn't just the result of
3609 * a replayed close:
3610 */
3611 oo = search_close_lru(close->cl_stateid.si_stateownerid);
3612 /* It's not stale; let's assume it's expired: */
3613 if (oo == NULL)
3614 goto out;
3615 cstate->replay_owner = &oo->oo_owner;
3616 status = nfsd4_check_seqid(cstate, &oo->oo_owner, close->cl_seqid);
3617 if (status)
3618 goto out;
3619 status = nfserr_bad_seqid;
3620 }
3621 if (status)
3622 goto out;
3623 oo = openowner(stp->st_stateowner);
3624 status = nfs_ok;
3625 update_stateid(&stp->st_stid.sc_stateid);
3626 memcpy(&close->cl_stateid, &stp->st_stid.sc_stateid, sizeof(stateid_t));
3627
3628 /* release_stateid() calls nfsd_close() if needed */
3629 release_open_stateid(stp);
3630
3631 /* place unused nfs4_stateowners on so_close_lru list to be
3632 * released by the laundromat service after the lease period
3633 * to enable us to handle CLOSE replay
3634 */
3635 if (list_empty(&oo->oo_owner.so_stateids))
3636 move_to_close_lru(oo);
3637 out:
3638 if (!cstate->replay_owner)
3639 nfs4_unlock_state();
3640 return status;
3641 }
3642
3643 __be32
3644 nfsd4_delegreturn(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
3645 struct nfsd4_delegreturn *dr)
3646 {
3647 struct nfs4_delegation *dp;
3648 stateid_t *stateid = &dr->dr_stateid;
3649 struct inode *inode;
3650 __be32 status;
3651
3652 if ((status = fh_verify(rqstp, &cstate->current_fh, S_IFREG, 0)))
3653 return status;
3654 inode = cstate->current_fh.fh_dentry->d_inode;
3655
3656 nfs4_lock_state();
3657 status = nfserr_bad_stateid;
3658 if (ZERO_STATEID(stateid) || ONE_STATEID(stateid))
3659 goto out;
3660 status = nfserr_stale_stateid;
3661 if (STALE_STATEID(stateid))
3662 goto out;
3663 status = nfserr_bad_stateid;
3664 if (!is_delegation_stateid(stateid))
3665 goto out;
3666 status = nfserr_expired;
3667 dp = find_delegation_stateid(inode, stateid);
3668 if (!dp)
3669 goto out;
3670 status = check_stateid_generation(stateid, &dp->dl_stateid, nfsd4_has_session(cstate));
3671 if (status)
3672 goto out;
3673 renew_client(dp->dl_client);
3674
3675 unhash_delegation(dp);
3676 out:
3677 nfs4_unlock_state();
3678
3679 return status;
3680 }
3681
3682
3683 /*
3684 * Lock owner state (byte-range locks)
3685 */
3686 #define LOFF_OVERFLOW(start, len) ((u64)(len) > ~(u64)(start))
3687 #define LOCK_HASH_BITS 8
3688 #define LOCK_HASH_SIZE (1 << LOCK_HASH_BITS)
3689 #define LOCK_HASH_MASK (LOCK_HASH_SIZE - 1)
3690
3691 static inline u64
3692 end_offset(u64 start, u64 len)
3693 {
3694 u64 end;
3695
3696 end = start + len;
3697 return end >= start ? end: NFS4_MAX_UINT64;
3698 }
3699
3700 /* last octet in a range */
3701 static inline u64
3702 last_byte_offset(u64 start, u64 len)
3703 {
3704 u64 end;
3705
3706 BUG_ON(!len);
3707 end = start + len;
3708 return end > start ? end - 1: NFS4_MAX_UINT64;
3709 }
3710
3711 static unsigned int lockownerid_hashval(u32 id)
3712 {
3713 return id & LOCK_HASH_MASK;
3714 }
3715
3716 static inline unsigned int
3717 lock_ownerstr_hashval(struct inode *inode, u32 cl_id,
3718 struct xdr_netobj *ownername)
3719 {
3720 return (file_hashval(inode) + cl_id
3721 + opaque_hashval(ownername->data, ownername->len))
3722 & LOCK_HASH_MASK;
3723 }
3724
3725 static struct list_head lock_ownerid_hashtbl[LOCK_HASH_SIZE];
3726 static struct list_head lock_ownerstr_hashtbl[LOCK_HASH_SIZE];
3727
3728 static struct nfs4_delegation *
3729 search_for_delegation(stateid_t *stid)
3730 {
3731 struct nfs4_file *fp;
3732 struct nfs4_delegation *dp;
3733 struct list_head *pos;
3734 int i;
3735
3736 for (i = 0; i < FILE_HASH_SIZE; i++) {
3737 list_for_each_entry(fp, &file_hashtbl[i], fi_hash) {
3738 list_for_each(pos, &fp->fi_delegations) {
3739 dp = list_entry(pos, struct nfs4_delegation, dl_perfile);
3740 if (same_stateid(&dp->dl_stateid, stid))
3741 return dp;
3742 }
3743 }
3744 }
3745 return NULL;
3746 }
3747
3748 static struct nfs4_delegation *
3749 find_delegation_stateid(struct inode *ino, stateid_t *stid)
3750 {
3751 struct nfs4_file *fp;
3752 struct nfs4_delegation *dl;
3753
3754 dprintk("NFSD: %s: stateid=" STATEID_FMT "\n", __func__,
3755 STATEID_VAL(stid));
3756
3757 fp = find_file(ino);
3758 if (!fp)
3759 return NULL;
3760 dl = find_delegation_file(fp, stid);
3761 put_nfs4_file(fp);
3762 return dl;
3763 }
3764
3765 /*
3766 * TODO: Linux file offsets are _signed_ 64-bit quantities, which means that
3767 * we can't properly handle lock requests that go beyond the (2^63 - 1)-th
3768 * byte, because of sign extension problems. Since NFSv4 calls for 64-bit
3769 * locking, this prevents us from being completely protocol-compliant. The
3770 * real solution to this problem is to start using unsigned file offsets in
3771 * the VFS, but this is a very deep change!
3772 */
3773 static inline void
3774 nfs4_transform_lock_offset(struct file_lock *lock)
3775 {
3776 if (lock->fl_start < 0)
3777 lock->fl_start = OFFSET_MAX;
3778 if (lock->fl_end < 0)
3779 lock->fl_end = OFFSET_MAX;
3780 }
3781
3782 /* Hack!: For now, we're defining this just so we can use a pointer to it
3783 * as a unique cookie to identify our (NFSv4's) posix locks. */
3784 static const struct lock_manager_operations nfsd_posix_mng_ops = {
3785 };
3786
3787 static inline void
3788 nfs4_set_lock_denied(struct file_lock *fl, struct nfsd4_lock_denied *deny)
3789 {
3790 struct nfs4_lockowner *lo;
3791
3792 if (fl->fl_lmops == &nfsd_posix_mng_ops) {
3793 lo = (struct nfs4_lockowner *) fl->fl_owner;
3794 deny->ld_owner.data = kmemdup(lo->lo_owner.so_owner.data,
3795 lo->lo_owner.so_owner.len, GFP_KERNEL);
3796 if (!deny->ld_owner.data)
3797 /* We just don't care that much */
3798 goto nevermind;
3799 deny->ld_owner.len = lo->lo_owner.so_owner.len;
3800 deny->ld_clientid = lo->lo_owner.so_client->cl_clientid;
3801 } else {
3802 nevermind:
3803 deny->ld_owner.len = 0;
3804 deny->ld_owner.data = NULL;
3805 deny->ld_clientid.cl_boot = 0;
3806 deny->ld_clientid.cl_id = 0;
3807 }
3808 deny->ld_start = fl->fl_start;
3809 deny->ld_length = NFS4_MAX_UINT64;
3810 if (fl->fl_end != NFS4_MAX_UINT64)
3811 deny->ld_length = fl->fl_end - fl->fl_start + 1;
3812 deny->ld_type = NFS4_READ_LT;
3813 if (fl->fl_type != F_RDLCK)
3814 deny->ld_type = NFS4_WRITE_LT;
3815 }
3816
3817 static struct nfs4_lockowner *
3818 find_lockowner_str(struct inode *inode, clientid_t *clid,
3819 struct xdr_netobj *owner)
3820 {
3821 unsigned int hashval = lock_ownerstr_hashval(inode, clid->cl_id, owner);
3822 struct nfs4_stateowner *op;
3823
3824 list_for_each_entry(op, &lock_ownerstr_hashtbl[hashval], so_strhash) {
3825 if (same_owner_str(op, owner, clid))
3826 return lockowner(op);
3827 }
3828 return NULL;
3829 }
3830
3831 static void hash_lockowner(struct nfs4_lockowner *lo, unsigned int strhashval, struct nfs4_client *clp, struct nfs4_ol_stateid *open_stp)
3832 {
3833 unsigned int idhashval;
3834
3835 idhashval = lockownerid_hashval(lo->lo_owner.so_id);
3836 list_add(&lo->lo_owner.so_idhash, &lock_ownerid_hashtbl[idhashval]);
3837 list_add(&lo->lo_owner.so_strhash, &lock_ownerstr_hashtbl[strhashval]);
3838 list_add(&lo->lo_perstateid, &open_stp->st_lockowners);
3839 }
3840
3841 /*
3842 * Alloc a lock owner structure.
3843 * Called in nfsd4_lock - therefore, OPEN and OPEN_CONFIRM (if needed) has
3844 * occurred.
3845 *
3846 * strhashval = lock_ownerstr_hashval
3847 */
3848
3849 static struct nfs4_lockowner *
3850 alloc_init_lock_stateowner(unsigned int strhashval, struct nfs4_client *clp, struct nfs4_ol_stateid *open_stp, struct nfsd4_lock *lock) {
3851 struct nfs4_lockowner *lo;
3852
3853 lo = alloc_stateowner(lockowner_slab, &lock->lk_new_owner, clp);
3854 if (!lo)
3855 return NULL;
3856 INIT_LIST_HEAD(&lo->lo_owner.so_stateids);
3857 lo->lo_owner.so_is_open_owner = 0;
3858 /* It is the openowner seqid that will be incremented in encode in the
3859 * case of new lockowners; so increment the lock seqid manually: */
3860 lo->lo_owner.so_seqid = lock->lk_new_lock_seqid + 1;
3861 hash_lockowner(lo, strhashval, clp, open_stp);
3862 return lo;
3863 }
3864
3865 static struct nfs4_ol_stateid *
3866 alloc_init_lock_stateid(struct nfs4_lockowner *lo, struct nfs4_file *fp, struct nfs4_ol_stateid *open_stp)
3867 {
3868 struct nfs4_ol_stateid *stp;
3869 unsigned int hashval = stateid_hashval(lo->lo_owner.so_id, fp->fi_id);
3870
3871 stp = nfs4_alloc_stateid();
3872 if (stp == NULL)
3873 goto out;
3874 list_add(&stp->st_stid.sc_hash, &stateid_hashtbl[hashval]);
3875 list_add(&stp->st_perfile, &fp->fi_stateids);
3876 list_add(&stp->st_perstateowner, &lo->lo_owner.so_stateids);
3877 stp->st_stateowner = &lo->lo_owner;
3878 stp->st_stid.sc_type = NFS4_LOCK_STID;
3879 get_nfs4_file(fp);
3880 stp->st_file = fp;
3881 stp->st_stid.sc_stateid.si_boot = boot_time;
3882 stp->st_stid.sc_stateid.si_stateownerid = lo->lo_owner.so_id;
3883 stp->st_stid.sc_stateid.si_fileid = fp->fi_id;
3884 /* note will be incremented before first return to client: */
3885 stp->st_stid.sc_stateid.si_generation = 0;
3886 stp->st_access_bmap = 0;
3887 stp->st_deny_bmap = open_stp->st_deny_bmap;
3888 stp->st_openstp = open_stp;
3889
3890 out:
3891 return stp;
3892 }
3893
3894 static int
3895 check_lock_length(u64 offset, u64 length)
3896 {
3897 return ((length == 0) || ((length != NFS4_MAX_UINT64) &&
3898 LOFF_OVERFLOW(offset, length)));
3899 }
3900
3901 static void get_lock_access(struct nfs4_ol_stateid *lock_stp, u32 access)
3902 {
3903 struct nfs4_file *fp = lock_stp->st_file;
3904 int oflag = nfs4_access_to_omode(access);
3905
3906 if (test_bit(access, &lock_stp->st_access_bmap))
3907 return;
3908 nfs4_file_get_access(fp, oflag);
3909 __set_bit(access, &lock_stp->st_access_bmap);
3910 }
3911
3912 /*
3913 * LOCK operation
3914 */
3915 __be32
3916 nfsd4_lock(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
3917 struct nfsd4_lock *lock)
3918 {
3919 struct nfs4_openowner *open_sop = NULL;
3920 struct nfs4_lockowner *lock_sop = NULL;
3921 struct nfs4_ol_stateid *lock_stp;
3922 struct nfs4_file *fp;
3923 struct file *filp = NULL;
3924 struct file_lock file_lock;
3925 struct file_lock conflock;
3926 __be32 status = 0;
3927 unsigned int strhashval;
3928 int lkflg;
3929 int err;
3930
3931 dprintk("NFSD: nfsd4_lock: start=%Ld length=%Ld\n",
3932 (long long) lock->lk_offset,
3933 (long long) lock->lk_length);
3934
3935 if (check_lock_length(lock->lk_offset, lock->lk_length))
3936 return nfserr_inval;
3937
3938 if ((status = fh_verify(rqstp, &cstate->current_fh,
3939 S_IFREG, NFSD_MAY_LOCK))) {
3940 dprintk("NFSD: nfsd4_lock: permission denied!\n");
3941 return status;
3942 }
3943
3944 nfs4_lock_state();
3945
3946 if (lock->lk_is_new) {
3947 /*
3948 * Client indicates that this is a new lockowner.
3949 * Use open owner and open stateid to create lock owner and
3950 * lock stateid.
3951 */
3952 struct nfs4_ol_stateid *open_stp = NULL;
3953
3954 status = nfserr_stale_clientid;
3955 if (!nfsd4_has_session(cstate) &&
3956 STALE_CLIENTID(&lock->lk_new_clientid))
3957 goto out;
3958
3959 /* validate and update open stateid and open seqid */
3960 status = nfs4_preprocess_confirmed_seqid_op(cstate,
3961 lock->lk_new_open_seqid,
3962 &lock->lk_new_open_stateid,
3963 &open_stp);
3964 if (status)
3965 goto out;
3966 open_sop = openowner(open_stp->st_stateowner);
3967 status = nfserr_bad_stateid;
3968 if (!nfsd4_has_session(cstate) &&
3969 !same_clid(&open_sop->oo_owner.so_client->cl_clientid,
3970 &lock->v.new.clientid))
3971 goto out;
3972 /* create lockowner and lock stateid */
3973 fp = open_stp->st_file;
3974 strhashval = lock_ownerstr_hashval(fp->fi_inode,
3975 open_sop->oo_owner.so_client->cl_clientid.cl_id,
3976 &lock->v.new.owner);
3977 /* XXX: Do we need to check for duplicate stateowners on
3978 * the same file, or should they just be allowed (and
3979 * create new stateids)? */
3980 status = nfserr_jukebox;
3981 lock_sop = alloc_init_lock_stateowner(strhashval,
3982 open_sop->oo_owner.so_client, open_stp, lock);
3983 if (lock_sop == NULL)
3984 goto out;
3985 lock_stp = alloc_init_lock_stateid(lock_sop, fp, open_stp);
3986 if (lock_stp == NULL)
3987 goto out;
3988 } else {
3989 /* lock (lock owner + lock stateid) already exists */
3990 status = nfs4_preprocess_seqid_op(cstate,
3991 lock->lk_old_lock_seqid,
3992 &lock->lk_old_lock_stateid,
3993 NFS4_LOCK_STID, &lock_stp);
3994 if (status)
3995 goto out;
3996 lock_sop = lockowner(lock_stp->st_stateowner);
3997 fp = lock_stp->st_file;
3998 }
3999 /* lock_sop and lock_stp have been created or found */
4000
4001 lkflg = setlkflg(lock->lk_type);
4002 status = nfs4_check_openmode(lock_stp, lkflg);
4003 if (status)
4004 goto out;
4005
4006 status = nfserr_grace;
4007 if (locks_in_grace() && !lock->lk_reclaim)
4008 goto out;
4009 status = nfserr_no_grace;
4010 if (!locks_in_grace() && lock->lk_reclaim)
4011 goto out;
4012
4013 locks_init_lock(&file_lock);
4014 switch (lock->lk_type) {
4015 case NFS4_READ_LT:
4016 case NFS4_READW_LT:
4017 filp = find_readable_file(lock_stp->st_file);
4018 if (filp)
4019 get_lock_access(lock_stp, NFS4_SHARE_ACCESS_READ);
4020 file_lock.fl_type = F_RDLCK;
4021 break;
4022 case NFS4_WRITE_LT:
4023 case NFS4_WRITEW_LT:
4024 filp = find_writeable_file(lock_stp->st_file);
4025 if (filp)
4026 get_lock_access(lock_stp, NFS4_SHARE_ACCESS_WRITE);
4027 file_lock.fl_type = F_WRLCK;
4028 break;
4029 default:
4030 status = nfserr_inval;
4031 goto out;
4032 }
4033 if (!filp) {
4034 status = nfserr_openmode;
4035 goto out;
4036 }
4037 file_lock.fl_owner = (fl_owner_t)lock_sop;
4038 file_lock.fl_pid = current->tgid;
4039 file_lock.fl_file = filp;
4040 file_lock.fl_flags = FL_POSIX;
4041 file_lock.fl_lmops = &nfsd_posix_mng_ops;
4042
4043 file_lock.fl_start = lock->lk_offset;
4044 file_lock.fl_end = last_byte_offset(lock->lk_offset, lock->lk_length);
4045 nfs4_transform_lock_offset(&file_lock);
4046
4047 /*
4048 * Try to lock the file in the VFS.
4049 * Note: locks.c uses the BKL to protect the inode's lock list.
4050 */
4051
4052 err = vfs_lock_file(filp, F_SETLK, &file_lock, &conflock);
4053 switch (-err) {
4054 case 0: /* success! */
4055 update_stateid(&lock_stp->st_stid.sc_stateid);
4056 memcpy(&lock->lk_resp_stateid, &lock_stp->st_stid.sc_stateid,
4057 sizeof(stateid_t));
4058 status = 0;
4059 break;
4060 case (EAGAIN): /* conflock holds conflicting lock */
4061 status = nfserr_denied;
4062 dprintk("NFSD: nfsd4_lock: conflicting lock found!\n");
4063 nfs4_set_lock_denied(&conflock, &lock->lk_denied);
4064 break;
4065 case (EDEADLK):
4066 status = nfserr_deadlock;
4067 break;
4068 default:
4069 dprintk("NFSD: nfsd4_lock: vfs_lock_file() failed! status %d\n",err);
4070 status = nfserrno(err);
4071 break;
4072 }
4073 out:
4074 if (status && lock->lk_is_new && lock_sop)
4075 release_lockowner(lock_sop);
4076 if (!cstate->replay_owner)
4077 nfs4_unlock_state();
4078 return status;
4079 }
4080
4081 /*
4082 * The NFSv4 spec allows a client to do a LOCKT without holding an OPEN,
4083 * so we do a temporary open here just to get an open file to pass to
4084 * vfs_test_lock. (Arguably perhaps test_lock should be done with an
4085 * inode operation.)
4086 */
4087 static int nfsd_test_lock(struct svc_rqst *rqstp, struct svc_fh *fhp, struct file_lock *lock)
4088 {
4089 struct file *file;
4090 int err;
4091
4092 err = nfsd_open(rqstp, fhp, S_IFREG, NFSD_MAY_READ, &file);
4093 if (err)
4094 return err;
4095 err = vfs_test_lock(file, lock);
4096 nfsd_close(file);
4097 return err;
4098 }
4099
4100 /*
4101 * LOCKT operation
4102 */
4103 __be32
4104 nfsd4_lockt(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
4105 struct nfsd4_lockt *lockt)
4106 {
4107 struct inode *inode;
4108 struct file_lock file_lock;
4109 struct nfs4_lockowner *lo;
4110 int error;
4111 __be32 status;
4112
4113 if (locks_in_grace())
4114 return nfserr_grace;
4115
4116 if (check_lock_length(lockt->lt_offset, lockt->lt_length))
4117 return nfserr_inval;
4118
4119 nfs4_lock_state();
4120
4121 status = nfserr_stale_clientid;
4122 if (!nfsd4_has_session(cstate) && STALE_CLIENTID(&lockt->lt_clientid))
4123 goto out;
4124
4125 if ((status = fh_verify(rqstp, &cstate->current_fh, S_IFREG, 0)))
4126 goto out;
4127
4128 inode = cstate->current_fh.fh_dentry->d_inode;
4129 locks_init_lock(&file_lock);
4130 switch (lockt->lt_type) {
4131 case NFS4_READ_LT:
4132 case NFS4_READW_LT:
4133 file_lock.fl_type = F_RDLCK;
4134 break;
4135 case NFS4_WRITE_LT:
4136 case NFS4_WRITEW_LT:
4137 file_lock.fl_type = F_WRLCK;
4138 break;
4139 default:
4140 dprintk("NFSD: nfs4_lockt: bad lock type!\n");
4141 status = nfserr_inval;
4142 goto out;
4143 }
4144
4145 lo = find_lockowner_str(inode, &lockt->lt_clientid, &lockt->lt_owner);
4146 if (lo)
4147 file_lock.fl_owner = (fl_owner_t)lo;
4148 file_lock.fl_pid = current->tgid;
4149 file_lock.fl_flags = FL_POSIX;
4150
4151 file_lock.fl_start = lockt->lt_offset;
4152 file_lock.fl_end = last_byte_offset(lockt->lt_offset, lockt->lt_length);
4153
4154 nfs4_transform_lock_offset(&file_lock);
4155
4156 status = nfs_ok;
4157 error = nfsd_test_lock(rqstp, &cstate->current_fh, &file_lock);
4158 if (error) {
4159 status = nfserrno(error);
4160 goto out;
4161 }
4162 if (file_lock.fl_type != F_UNLCK) {
4163 status = nfserr_denied;
4164 nfs4_set_lock_denied(&file_lock, &lockt->lt_denied);
4165 }
4166 out:
4167 nfs4_unlock_state();
4168 return status;
4169 }
4170
4171 __be32
4172 nfsd4_locku(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
4173 struct nfsd4_locku *locku)
4174 {
4175 struct nfs4_ol_stateid *stp;
4176 struct file *filp = NULL;
4177 struct file_lock file_lock;
4178 __be32 status;
4179 int err;
4180
4181 dprintk("NFSD: nfsd4_locku: start=%Ld length=%Ld\n",
4182 (long long) locku->lu_offset,
4183 (long long) locku->lu_length);
4184
4185 if (check_lock_length(locku->lu_offset, locku->lu_length))
4186 return nfserr_inval;
4187
4188 nfs4_lock_state();
4189
4190 status = nfs4_preprocess_seqid_op(cstate, locku->lu_seqid,
4191 &locku->lu_stateid, NFS4_LOCK_STID, &stp);
4192 if (status)
4193 goto out;
4194 filp = find_any_file(stp->st_file);
4195 if (!filp) {
4196 status = nfserr_lock_range;
4197 goto out;
4198 }
4199 BUG_ON(!filp);
4200 locks_init_lock(&file_lock);
4201 file_lock.fl_type = F_UNLCK;
4202 file_lock.fl_owner = (fl_owner_t)lockowner(stp->st_stateowner);
4203 file_lock.fl_pid = current->tgid;
4204 file_lock.fl_file = filp;
4205 file_lock.fl_flags = FL_POSIX;
4206 file_lock.fl_lmops = &nfsd_posix_mng_ops;
4207 file_lock.fl_start = locku->lu_offset;
4208
4209 file_lock.fl_end = last_byte_offset(locku->lu_offset, locku->lu_length);
4210 nfs4_transform_lock_offset(&file_lock);
4211
4212 /*
4213 * Try to unlock the file in the VFS.
4214 */
4215 err = vfs_lock_file(filp, F_SETLK, &file_lock, NULL);
4216 if (err) {
4217 dprintk("NFSD: nfs4_locku: vfs_lock_file failed!\n");
4218 goto out_nfserr;
4219 }
4220 /*
4221 * OK, unlock succeeded; the only thing left to do is update the stateid.
4222 */
4223 update_stateid(&stp->st_stid.sc_stateid);
4224 memcpy(&locku->lu_stateid, &stp->st_stid.sc_stateid, sizeof(stateid_t));
4225
4226 out:
4227 nfs4_unlock_state();
4228 return status;
4229
4230 out_nfserr:
4231 status = nfserrno(err);
4232 goto out;
4233 }
4234
4235 /*
4236 * returns
4237 * 1: locks held by lockowner
4238 * 0: no locks held by lockowner
4239 */
4240 static int
4241 check_for_locks(struct nfs4_file *filp, struct nfs4_lockowner *lowner)
4242 {
4243 struct file_lock **flpp;
4244 struct inode *inode = filp->fi_inode;
4245 int status = 0;
4246
4247 lock_flocks();
4248 for (flpp = &inode->i_flock; *flpp != NULL; flpp = &(*flpp)->fl_next) {
4249 if ((*flpp)->fl_owner == (fl_owner_t)lowner) {
4250 status = 1;
4251 goto out;
4252 }
4253 }
4254 out:
4255 unlock_flocks();
4256 return status;
4257 }
4258
4259 __be32
4260 nfsd4_release_lockowner(struct svc_rqst *rqstp,
4261 struct nfsd4_compound_state *cstate,
4262 struct nfsd4_release_lockowner *rlockowner)
4263 {
4264 clientid_t *clid = &rlockowner->rl_clientid;
4265 struct nfs4_stateowner *sop;
4266 struct nfs4_lockowner *lo;
4267 struct nfs4_ol_stateid *stp;
4268 struct xdr_netobj *owner = &rlockowner->rl_owner;
4269 struct list_head matches;
4270 int i;
4271 __be32 status;
4272
4273 dprintk("nfsd4_release_lockowner clientid: (%08x/%08x):\n",
4274 clid->cl_boot, clid->cl_id);
4275
4276 /* XXX check for lease expiration */
4277
4278 status = nfserr_stale_clientid;
4279 if (STALE_CLIENTID(clid))
4280 return status;
4281
4282 nfs4_lock_state();
4283
4284 status = nfserr_locks_held;
4285 /* XXX: we're doing a linear search through all the lockowners.
4286 * Yipes! For now we'll just hope clients aren't really using
4287 * release_lockowner much, but eventually we have to fix these
4288 * data structures. */
4289 INIT_LIST_HEAD(&matches);
4290 for (i = 0; i < LOCK_HASH_SIZE; i++) {
4291 list_for_each_entry(sop, &lock_ownerid_hashtbl[i], so_idhash) {
4292 if (!same_owner_str(sop, owner, clid))
4293 continue;
4294 list_for_each_entry(stp, &sop->so_stateids,
4295 st_perstateowner) {
4296 lo = lockowner(sop);
4297 if (check_for_locks(stp->st_file, lo))
4298 goto out;
4299 list_add(&lo->lo_list, &matches);
4300 }
4301 }
4302 }
4303 /* Clients probably won't expect us to return with some (but not all)
4304 * of the lockowner state released; so don't release any until all
4305 * have been checked. */
4306 status = nfs_ok;
4307 while (!list_empty(&matches)) {
4308 lo = list_entry(matches.next, struct nfs4_lockowner,
4309 lo_list);
4310 /* unhash_stateowner deletes so_perclient only
4311 * for openowners. */
4312 list_del(&lo->lo_list);
4313 release_lockowner(lo);
4314 }
4315 out:
4316 nfs4_unlock_state();
4317 return status;
4318 }
4319
4320 static inline struct nfs4_client_reclaim *
4321 alloc_reclaim(void)
4322 {
4323 return kmalloc(sizeof(struct nfs4_client_reclaim), GFP_KERNEL);
4324 }
4325
4326 int
4327 nfs4_has_reclaimed_state(const char *name, bool use_exchange_id)
4328 {
4329 unsigned int strhashval = clientstr_hashval(name);
4330 struct nfs4_client *clp;
4331
4332 clp = find_confirmed_client_by_str(name, strhashval);
4333 return clp ? 1 : 0;
4334 }
4335
4336 /*
4337 * failure => all reset bets are off, nfserr_no_grace...
4338 */
4339 int
4340 nfs4_client_to_reclaim(const char *name)
4341 {
4342 unsigned int strhashval;
4343 struct nfs4_client_reclaim *crp = NULL;
4344
4345 dprintk("NFSD nfs4_client_to_reclaim NAME: %.*s\n", HEXDIR_LEN, name);
4346 crp = alloc_reclaim();
4347 if (!crp)
4348 return 0;
4349 strhashval = clientstr_hashval(name);
4350 INIT_LIST_HEAD(&crp->cr_strhash);
4351 list_add(&crp->cr_strhash, &reclaim_str_hashtbl[strhashval]);
4352 memcpy(crp->cr_recdir, name, HEXDIR_LEN);
4353 reclaim_str_hashtbl_size++;
4354 return 1;
4355 }
4356
4357 static void
4358 nfs4_release_reclaim(void)
4359 {
4360 struct nfs4_client_reclaim *crp = NULL;
4361 int i;
4362
4363 for (i = 0; i < CLIENT_HASH_SIZE; i++) {
4364 while (!list_empty(&reclaim_str_hashtbl[i])) {
4365 crp = list_entry(reclaim_str_hashtbl[i].next,
4366 struct nfs4_client_reclaim, cr_strhash);
4367 list_del(&crp->cr_strhash);
4368 kfree(crp);
4369 reclaim_str_hashtbl_size--;
4370 }
4371 }
4372 BUG_ON(reclaim_str_hashtbl_size);
4373 }
4374
4375 /*
4376 * called from OPEN, CLAIM_PREVIOUS with a new clientid. */
4377 static struct nfs4_client_reclaim *
4378 nfs4_find_reclaim_client(clientid_t *clid)
4379 {
4380 unsigned int strhashval;
4381 struct nfs4_client *clp;
4382 struct nfs4_client_reclaim *crp = NULL;
4383
4384
4385 /* find clientid in conf_id_hashtbl */
4386 clp = find_confirmed_client(clid);
4387 if (clp == NULL)
4388 return NULL;
4389
4390 dprintk("NFSD: nfs4_find_reclaim_client for %.*s with recdir %s\n",
4391 clp->cl_name.len, clp->cl_name.data,
4392 clp->cl_recdir);
4393
4394 /* find clp->cl_name in reclaim_str_hashtbl */
4395 strhashval = clientstr_hashval(clp->cl_recdir);
4396 list_for_each_entry(crp, &reclaim_str_hashtbl[strhashval], cr_strhash) {
4397 if (same_name(crp->cr_recdir, clp->cl_recdir)) {
4398 return crp;
4399 }
4400 }
4401 return NULL;
4402 }
4403
4404 /*
4405 * Called from OPEN. Look for clientid in reclaim list.
4406 */
4407 __be32
4408 nfs4_check_open_reclaim(clientid_t *clid)
4409 {
4410 return nfs4_find_reclaim_client(clid) ? nfs_ok : nfserr_reclaim_bad;
4411 }
4412
4413 /* initialization to perform at module load time: */
4414
4415 int
4416 nfs4_state_init(void)
4417 {
4418 int i, status;
4419
4420 status = nfsd4_init_slabs();
4421 if (status)
4422 return status;
4423 for (i = 0; i < CLIENT_HASH_SIZE; i++) {
4424 INIT_LIST_HEAD(&conf_id_hashtbl[i]);
4425 INIT_LIST_HEAD(&conf_str_hashtbl[i]);
4426 INIT_LIST_HEAD(&unconf_str_hashtbl[i]);
4427 INIT_LIST_HEAD(&unconf_id_hashtbl[i]);
4428 INIT_LIST_HEAD(&reclaim_str_hashtbl[i]);
4429 }
4430 for (i = 0; i < SESSION_HASH_SIZE; i++)
4431 INIT_LIST_HEAD(&sessionid_hashtbl[i]);
4432 for (i = 0; i < FILE_HASH_SIZE; i++) {
4433 INIT_LIST_HEAD(&file_hashtbl[i]);
4434 }
4435 for (i = 0; i < OPEN_OWNER_HASH_SIZE; i++) {
4436 INIT_LIST_HEAD(&open_ownerstr_hashtbl[i]);
4437 INIT_LIST_HEAD(&open_ownerid_hashtbl[i]);
4438 }
4439 for (i = 0; i < STATEID_HASH_SIZE; i++)
4440 INIT_LIST_HEAD(&stateid_hashtbl[i]);
4441 for (i = 0; i < LOCK_HASH_SIZE; i++) {
4442 INIT_LIST_HEAD(&lock_ownerid_hashtbl[i]);
4443 INIT_LIST_HEAD(&lock_ownerstr_hashtbl[i]);
4444 }
4445 memset(&onestateid, ~0, sizeof(stateid_t));
4446 INIT_LIST_HEAD(&close_lru);
4447 INIT_LIST_HEAD(&client_lru);
4448 INIT_LIST_HEAD(&del_recall_lru);
4449 reclaim_str_hashtbl_size = 0;
4450 return 0;
4451 }
4452
4453 static void
4454 nfsd4_load_reboot_recovery_data(void)
4455 {
4456 int status;
4457
4458 nfs4_lock_state();
4459 nfsd4_init_recdir();
4460 status = nfsd4_recdir_load();
4461 nfs4_unlock_state();
4462 if (status)
4463 printk("NFSD: Failure reading reboot recovery data\n");
4464 }
4465
4466 /*
4467 * Since the lifetime of a delegation isn't limited to that of an open, a
4468 * client may quite reasonably hang on to a delegation as long as it has
4469 * the inode cached. This becomes an obvious problem the first time a
4470 * client's inode cache approaches the size of the server's total memory.
4471 *
4472 * For now we avoid this problem by imposing a hard limit on the number
4473 * of delegations, which varies according to the server's memory size.
4474 */
4475 static void
4476 set_max_delegations(void)
4477 {
4478 /*
4479 * Allow at most 4 delegations per megabyte of RAM. Quick
4480 * estimates suggest that in the worst case (where every delegation
4481 * is for a different inode), a delegation could take about 1.5K,
4482 * giving a worst case usage of about 6% of memory.
4483 */
4484 max_delegations = nr_free_buffer_pages() >> (20 - 2 - PAGE_SHIFT);
4485 }
4486
4487 /* initialization to perform when the nfsd service is started: */
4488
4489 static int
4490 __nfs4_state_start(void)
4491 {
4492 int ret;
4493
4494 boot_time = get_seconds();
4495 locks_start_grace(&nfsd4_manager);
4496 printk(KERN_INFO "NFSD: starting %ld-second grace period\n",
4497 nfsd4_grace);
4498 ret = set_callback_cred();
4499 if (ret)
4500 return -ENOMEM;
4501 laundry_wq = create_singlethread_workqueue("nfsd4");
4502 if (laundry_wq == NULL)
4503 return -ENOMEM;
4504 ret = nfsd4_create_callback_queue();
4505 if (ret)
4506 goto out_free_laundry;
4507 queue_delayed_work(laundry_wq, &laundromat_work, nfsd4_grace * HZ);
4508 set_max_delegations();
4509 return 0;
4510 out_free_laundry:
4511 destroy_workqueue(laundry_wq);
4512 return ret;
4513 }
4514
4515 int
4516 nfs4_state_start(void)
4517 {
4518 nfsd4_load_reboot_recovery_data();
4519 return __nfs4_state_start();
4520 }
4521
4522 static void
4523 __nfs4_state_shutdown(void)
4524 {
4525 int i;
4526 struct nfs4_client *clp = NULL;
4527 struct nfs4_delegation *dp = NULL;
4528 struct list_head *pos, *next, reaplist;
4529
4530 for (i = 0; i < CLIENT_HASH_SIZE; i++) {
4531 while (!list_empty(&conf_id_hashtbl[i])) {
4532 clp = list_entry(conf_id_hashtbl[i].next, struct nfs4_client, cl_idhash);
4533 expire_client(clp);
4534 }
4535 while (!list_empty(&unconf_str_hashtbl[i])) {
4536 clp = list_entry(unconf_str_hashtbl[i].next, struct nfs4_client, cl_strhash);
4537 expire_client(clp);
4538 }
4539 }
4540 INIT_LIST_HEAD(&reaplist);
4541 spin_lock(&recall_lock);
4542 list_for_each_safe(pos, next, &del_recall_lru) {
4543 dp = list_entry (pos, struct nfs4_delegation, dl_recall_lru);
4544 list_move(&dp->dl_recall_lru, &reaplist);
4545 }
4546 spin_unlock(&recall_lock);
4547 list_for_each_safe(pos, next, &reaplist) {
4548 dp = list_entry (pos, struct nfs4_delegation, dl_recall_lru);
4549 list_del_init(&dp->dl_recall_lru);
4550 unhash_delegation(dp);
4551 }
4552
4553 nfsd4_shutdown_recdir();
4554 }
4555
4556 void
4557 nfs4_state_shutdown(void)
4558 {
4559 cancel_delayed_work_sync(&laundromat_work);
4560 destroy_workqueue(laundry_wq);
4561 locks_end_grace(&nfsd4_manager);
4562 nfs4_lock_state();
4563 nfs4_release_reclaim();
4564 __nfs4_state_shutdown();
4565 nfs4_unlock_state();
4566 nfsd4_destroy_callback_queue();
4567 }
This page took 0.129705 seconds and 6 git commands to generate.