]> git.proxmox.com Git - mirror_ubuntu-zesty-kernel.git/blob - fs/nfsd/nfs4state.c
[PATCH] knfsd: nfsd4: limit number of delegations handed out.
[mirror_ubuntu-zesty-kernel.git] / fs / nfsd / nfs4state.c
1 /*
2 * linux/fs/nfsd/nfs4state.c
3 *
4 * Copyright (c) 2001 The Regents of the University of Michigan.
5 * All rights reserved.
6 *
7 * Kendrick Smith <kmsmith@umich.edu>
8 * Andy Adamson <kandros@umich.edu>
9 *
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 *
14 * 1. Redistributions of source code must retain the above copyright
15 * notice, this list of conditions and the following disclaimer.
16 * 2. Redistributions in binary form must reproduce the above copyright
17 * notice, this list of conditions and the following disclaimer in the
18 * documentation and/or other materials provided with the distribution.
19 * 3. Neither the name of the University nor the names of its
20 * contributors may be used to endorse or promote products derived
21 * from this software without specific prior written permission.
22 *
23 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED
24 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
25 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
26 * DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
27 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
28 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
29 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
30 * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
31 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
32 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
33 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
34 *
35 */
36
37 #include <linux/param.h>
38 #include <linux/major.h>
39 #include <linux/slab.h>
40
41 #include <linux/sunrpc/svc.h>
42 #include <linux/nfsd/nfsd.h>
43 #include <linux/nfsd/cache.h>
44 #include <linux/mount.h>
45 #include <linux/workqueue.h>
46 #include <linux/smp_lock.h>
47 #include <linux/kthread.h>
48 #include <linux/nfs4.h>
49 #include <linux/nfsd/state.h>
50 #include <linux/nfsd/xdr4.h>
51 #include <linux/namei.h>
52 #include <linux/mutex.h>
53
54 #define NFSDDBG_FACILITY NFSDDBG_PROC
55
56 /* Globals */
57 static time_t lease_time = 90; /* default lease time */
58 static time_t user_lease_time = 90;
59 static time_t boot_time;
60 static int in_grace = 1;
61 static u32 current_clientid = 1;
62 static u32 current_ownerid = 1;
63 static u32 current_fileid = 1;
64 static u32 current_delegid = 1;
65 static u32 nfs4_init;
66 static stateid_t zerostateid; /* bits all 0 */
67 static stateid_t onestateid; /* bits all 1 */
68
69 #define ZERO_STATEID(stateid) (!memcmp((stateid), &zerostateid, sizeof(stateid_t)))
70 #define ONE_STATEID(stateid) (!memcmp((stateid), &onestateid, sizeof(stateid_t)))
71
72 /* forward declarations */
73 static struct nfs4_stateid * find_stateid(stateid_t *stid, int flags);
74 static struct nfs4_delegation * find_delegation_stateid(struct inode *ino, stateid_t *stid);
75 static void release_stateid_lockowners(struct nfs4_stateid *open_stp);
76 static char user_recovery_dirname[PATH_MAX] = "/var/lib/nfs/v4recovery";
77 static void nfs4_set_recdir(char *recdir);
78
79 /* Locking:
80 *
81 * client_mutex:
82 * protects clientid_hashtbl[], clientstr_hashtbl[],
83 * unconfstr_hashtbl[], uncofid_hashtbl[].
84 */
85 static DEFINE_MUTEX(client_mutex);
86
87 static kmem_cache_t *stateowner_slab = NULL;
88 static kmem_cache_t *file_slab = NULL;
89 static kmem_cache_t *stateid_slab = NULL;
90 static kmem_cache_t *deleg_slab = NULL;
91
92 void
93 nfs4_lock_state(void)
94 {
95 mutex_lock(&client_mutex);
96 }
97
98 void
99 nfs4_unlock_state(void)
100 {
101 mutex_unlock(&client_mutex);
102 }
103
104 static inline u32
105 opaque_hashval(const void *ptr, int nbytes)
106 {
107 unsigned char *cptr = (unsigned char *) ptr;
108
109 u32 x = 0;
110 while (nbytes--) {
111 x *= 37;
112 x += *cptr++;
113 }
114 return x;
115 }
116
117 /* forward declarations */
118 static void release_stateowner(struct nfs4_stateowner *sop);
119 static void release_stateid(struct nfs4_stateid *stp, int flags);
120
121 /*
122 * Delegation state
123 */
124
125 /* recall_lock protects the del_recall_lru */
126 static spinlock_t recall_lock = SPIN_LOCK_UNLOCKED;
127 static struct list_head del_recall_lru;
128
129 static void
130 free_nfs4_file(struct kref *kref)
131 {
132 struct nfs4_file *fp = container_of(kref, struct nfs4_file, fi_ref);
133 list_del(&fp->fi_hash);
134 iput(fp->fi_inode);
135 kmem_cache_free(file_slab, fp);
136 }
137
138 static inline void
139 put_nfs4_file(struct nfs4_file *fi)
140 {
141 kref_put(&fi->fi_ref, free_nfs4_file);
142 }
143
144 static inline void
145 get_nfs4_file(struct nfs4_file *fi)
146 {
147 kref_get(&fi->fi_ref);
148 }
149
150 static int num_delegations;
151
152 /*
153 * Open owner state (share locks)
154 */
155
156 /* hash tables for nfs4_stateowner */
157 #define OWNER_HASH_BITS 8
158 #define OWNER_HASH_SIZE (1 << OWNER_HASH_BITS)
159 #define OWNER_HASH_MASK (OWNER_HASH_SIZE - 1)
160
161 #define ownerid_hashval(id) \
162 ((id) & OWNER_HASH_MASK)
163 #define ownerstr_hashval(clientid, ownername) \
164 (((clientid) + opaque_hashval((ownername.data), (ownername.len))) & OWNER_HASH_MASK)
165
166 static struct list_head ownerid_hashtbl[OWNER_HASH_SIZE];
167 static struct list_head ownerstr_hashtbl[OWNER_HASH_SIZE];
168
169 /* hash table for nfs4_file */
170 #define FILE_HASH_BITS 8
171 #define FILE_HASH_SIZE (1 << FILE_HASH_BITS)
172 #define FILE_HASH_MASK (FILE_HASH_SIZE - 1)
173 /* hash table for (open)nfs4_stateid */
174 #define STATEID_HASH_BITS 10
175 #define STATEID_HASH_SIZE (1 << STATEID_HASH_BITS)
176 #define STATEID_HASH_MASK (STATEID_HASH_SIZE - 1)
177
178 #define file_hashval(x) \
179 hash_ptr(x, FILE_HASH_BITS)
180 #define stateid_hashval(owner_id, file_id) \
181 (((owner_id) + (file_id)) & STATEID_HASH_MASK)
182
183 static struct list_head file_hashtbl[FILE_HASH_SIZE];
184 static struct list_head stateid_hashtbl[STATEID_HASH_SIZE];
185
186 static struct nfs4_delegation *
187 alloc_init_deleg(struct nfs4_client *clp, struct nfs4_stateid *stp, struct svc_fh *current_fh, u32 type)
188 {
189 struct nfs4_delegation *dp;
190 struct nfs4_file *fp = stp->st_file;
191 struct nfs4_callback *cb = &stp->st_stateowner->so_client->cl_callback;
192
193 dprintk("NFSD alloc_init_deleg\n");
194 if (num_delegations > STATEID_HASH_SIZE * 4)
195 return NULL;
196 dp = kmem_cache_alloc(deleg_slab, GFP_KERNEL);
197 if (dp == NULL)
198 return dp;
199 num_delegations++;
200 INIT_LIST_HEAD(&dp->dl_perfile);
201 INIT_LIST_HEAD(&dp->dl_perclnt);
202 INIT_LIST_HEAD(&dp->dl_recall_lru);
203 dp->dl_client = clp;
204 get_nfs4_file(fp);
205 dp->dl_file = fp;
206 dp->dl_flock = NULL;
207 get_file(stp->st_vfs_file);
208 dp->dl_vfs_file = stp->st_vfs_file;
209 dp->dl_type = type;
210 dp->dl_recall.cbr_dp = NULL;
211 dp->dl_recall.cbr_ident = cb->cb_ident;
212 dp->dl_recall.cbr_trunc = 0;
213 dp->dl_stateid.si_boot = boot_time;
214 dp->dl_stateid.si_stateownerid = current_delegid++;
215 dp->dl_stateid.si_fileid = 0;
216 dp->dl_stateid.si_generation = 0;
217 dp->dl_fhlen = current_fh->fh_handle.fh_size;
218 memcpy(dp->dl_fhval, &current_fh->fh_handle.fh_base,
219 current_fh->fh_handle.fh_size);
220 dp->dl_time = 0;
221 atomic_set(&dp->dl_count, 1);
222 list_add(&dp->dl_perfile, &fp->fi_delegations);
223 list_add(&dp->dl_perclnt, &clp->cl_delegations);
224 return dp;
225 }
226
227 void
228 nfs4_put_delegation(struct nfs4_delegation *dp)
229 {
230 if (atomic_dec_and_test(&dp->dl_count)) {
231 dprintk("NFSD: freeing dp %p\n",dp);
232 put_nfs4_file(dp->dl_file);
233 kmem_cache_free(deleg_slab, dp);
234 num_delegations--;
235 }
236 }
237
238 /* Remove the associated file_lock first, then remove the delegation.
239 * lease_modify() is called to remove the FS_LEASE file_lock from
240 * the i_flock list, eventually calling nfsd's lock_manager
241 * fl_release_callback.
242 */
243 static void
244 nfs4_close_delegation(struct nfs4_delegation *dp)
245 {
246 struct file *filp = dp->dl_vfs_file;
247
248 dprintk("NFSD: close_delegation dp %p\n",dp);
249 dp->dl_vfs_file = NULL;
250 /* The following nfsd_close may not actually close the file,
251 * but we want to remove the lease in any case. */
252 if (dp->dl_flock)
253 setlease(filp, F_UNLCK, &dp->dl_flock);
254 nfsd_close(filp);
255 }
256
257 /* Called under the state lock. */
258 static void
259 unhash_delegation(struct nfs4_delegation *dp)
260 {
261 list_del_init(&dp->dl_perfile);
262 list_del_init(&dp->dl_perclnt);
263 spin_lock(&recall_lock);
264 list_del_init(&dp->dl_recall_lru);
265 spin_unlock(&recall_lock);
266 nfs4_close_delegation(dp);
267 nfs4_put_delegation(dp);
268 }
269
270 /*
271 * SETCLIENTID state
272 */
273
274 /* Hash tables for nfs4_clientid state */
275 #define CLIENT_HASH_BITS 4
276 #define CLIENT_HASH_SIZE (1 << CLIENT_HASH_BITS)
277 #define CLIENT_HASH_MASK (CLIENT_HASH_SIZE - 1)
278
279 #define clientid_hashval(id) \
280 ((id) & CLIENT_HASH_MASK)
281 #define clientstr_hashval(name) \
282 (opaque_hashval((name), 8) & CLIENT_HASH_MASK)
283 /*
284 * reclaim_str_hashtbl[] holds known client info from previous reset/reboot
285 * used in reboot/reset lease grace period processing
286 *
287 * conf_id_hashtbl[], and conf_str_hashtbl[] hold confirmed
288 * setclientid_confirmed info.
289 *
290 * unconf_str_hastbl[] and unconf_id_hashtbl[] hold unconfirmed
291 * setclientid info.
292 *
293 * client_lru holds client queue ordered by nfs4_client.cl_time
294 * for lease renewal.
295 *
296 * close_lru holds (open) stateowner queue ordered by nfs4_stateowner.so_time
297 * for last close replay.
298 */
299 static struct list_head reclaim_str_hashtbl[CLIENT_HASH_SIZE];
300 static int reclaim_str_hashtbl_size = 0;
301 static struct list_head conf_id_hashtbl[CLIENT_HASH_SIZE];
302 static struct list_head conf_str_hashtbl[CLIENT_HASH_SIZE];
303 static struct list_head unconf_str_hashtbl[CLIENT_HASH_SIZE];
304 static struct list_head unconf_id_hashtbl[CLIENT_HASH_SIZE];
305 static struct list_head client_lru;
306 static struct list_head close_lru;
307
308 static inline void
309 renew_client(struct nfs4_client *clp)
310 {
311 /*
312 * Move client to the end to the LRU list.
313 */
314 dprintk("renewing client (clientid %08x/%08x)\n",
315 clp->cl_clientid.cl_boot,
316 clp->cl_clientid.cl_id);
317 list_move_tail(&clp->cl_lru, &client_lru);
318 clp->cl_time = get_seconds();
319 }
320
321 /* SETCLIENTID and SETCLIENTID_CONFIRM Helper functions */
322 static int
323 STALE_CLIENTID(clientid_t *clid)
324 {
325 if (clid->cl_boot == boot_time)
326 return 0;
327 dprintk("NFSD stale clientid (%08x/%08x)\n",
328 clid->cl_boot, clid->cl_id);
329 return 1;
330 }
331
332 /*
333 * XXX Should we use a slab cache ?
334 * This type of memory management is somewhat inefficient, but we use it
335 * anyway since SETCLIENTID is not a common operation.
336 */
337 static inline struct nfs4_client *
338 alloc_client(struct xdr_netobj name)
339 {
340 struct nfs4_client *clp;
341
342 if ((clp = kmalloc(sizeof(struct nfs4_client), GFP_KERNEL))!= NULL) {
343 memset(clp, 0, sizeof(*clp));
344 if ((clp->cl_name.data = kmalloc(name.len, GFP_KERNEL)) != NULL) {
345 memcpy(clp->cl_name.data, name.data, name.len);
346 clp->cl_name.len = name.len;
347 }
348 else {
349 kfree(clp);
350 clp = NULL;
351 }
352 }
353 return clp;
354 }
355
356 static inline void
357 free_client(struct nfs4_client *clp)
358 {
359 if (clp->cl_cred.cr_group_info)
360 put_group_info(clp->cl_cred.cr_group_info);
361 kfree(clp->cl_name.data);
362 kfree(clp);
363 }
364
365 void
366 put_nfs4_client(struct nfs4_client *clp)
367 {
368 if (atomic_dec_and_test(&clp->cl_count))
369 free_client(clp);
370 }
371
372 static void
373 shutdown_callback_client(struct nfs4_client *clp)
374 {
375 struct rpc_clnt *clnt = clp->cl_callback.cb_client;
376
377 /* shutdown rpc client, ending any outstanding recall rpcs */
378 if (clnt) {
379 clp->cl_callback.cb_client = NULL;
380 rpc_shutdown_client(clnt);
381 rpciod_down();
382 }
383 }
384
385 static void
386 expire_client(struct nfs4_client *clp)
387 {
388 struct nfs4_stateowner *sop;
389 struct nfs4_delegation *dp;
390 struct list_head reaplist;
391
392 dprintk("NFSD: expire_client cl_count %d\n",
393 atomic_read(&clp->cl_count));
394
395 shutdown_callback_client(clp);
396
397 INIT_LIST_HEAD(&reaplist);
398 spin_lock(&recall_lock);
399 while (!list_empty(&clp->cl_delegations)) {
400 dp = list_entry(clp->cl_delegations.next, struct nfs4_delegation, dl_perclnt);
401 dprintk("NFSD: expire client. dp %p, fp %p\n", dp,
402 dp->dl_flock);
403 list_del_init(&dp->dl_perclnt);
404 list_move(&dp->dl_recall_lru, &reaplist);
405 }
406 spin_unlock(&recall_lock);
407 while (!list_empty(&reaplist)) {
408 dp = list_entry(reaplist.next, struct nfs4_delegation, dl_recall_lru);
409 list_del_init(&dp->dl_recall_lru);
410 unhash_delegation(dp);
411 }
412 list_del(&clp->cl_idhash);
413 list_del(&clp->cl_strhash);
414 list_del(&clp->cl_lru);
415 while (!list_empty(&clp->cl_openowners)) {
416 sop = list_entry(clp->cl_openowners.next, struct nfs4_stateowner, so_perclient);
417 release_stateowner(sop);
418 }
419 put_nfs4_client(clp);
420 }
421
422 static struct nfs4_client *
423 create_client(struct xdr_netobj name, char *recdir) {
424 struct nfs4_client *clp;
425
426 if (!(clp = alloc_client(name)))
427 goto out;
428 memcpy(clp->cl_recdir, recdir, HEXDIR_LEN);
429 atomic_set(&clp->cl_count, 1);
430 atomic_set(&clp->cl_callback.cb_set, 0);
431 INIT_LIST_HEAD(&clp->cl_idhash);
432 INIT_LIST_HEAD(&clp->cl_strhash);
433 INIT_LIST_HEAD(&clp->cl_openowners);
434 INIT_LIST_HEAD(&clp->cl_delegations);
435 INIT_LIST_HEAD(&clp->cl_lru);
436 out:
437 return clp;
438 }
439
440 static void
441 copy_verf(struct nfs4_client *target, nfs4_verifier *source) {
442 memcpy(target->cl_verifier.data, source->data, sizeof(target->cl_verifier.data));
443 }
444
445 static void
446 copy_clid(struct nfs4_client *target, struct nfs4_client *source) {
447 target->cl_clientid.cl_boot = source->cl_clientid.cl_boot;
448 target->cl_clientid.cl_id = source->cl_clientid.cl_id;
449 }
450
451 static void
452 copy_cred(struct svc_cred *target, struct svc_cred *source) {
453
454 target->cr_uid = source->cr_uid;
455 target->cr_gid = source->cr_gid;
456 target->cr_group_info = source->cr_group_info;
457 get_group_info(target->cr_group_info);
458 }
459
460 static inline int
461 same_name(const char *n1, const char *n2) {
462 return 0 == memcmp(n1, n2, HEXDIR_LEN);
463 }
464
465 static int
466 cmp_verf(nfs4_verifier *v1, nfs4_verifier *v2) {
467 return(!memcmp(v1->data,v2->data,sizeof(v1->data)));
468 }
469
470 static int
471 cmp_clid(clientid_t * cl1, clientid_t * cl2) {
472 return((cl1->cl_boot == cl2->cl_boot) &&
473 (cl1->cl_id == cl2->cl_id));
474 }
475
476 /* XXX what about NGROUP */
477 static int
478 cmp_creds(struct svc_cred *cr1, struct svc_cred *cr2){
479 return(cr1->cr_uid == cr2->cr_uid);
480
481 }
482
483 static void
484 gen_clid(struct nfs4_client *clp) {
485 clp->cl_clientid.cl_boot = boot_time;
486 clp->cl_clientid.cl_id = current_clientid++;
487 }
488
489 static void
490 gen_confirm(struct nfs4_client *clp) {
491 struct timespec tv;
492 u32 * p;
493
494 tv = CURRENT_TIME;
495 p = (u32 *)clp->cl_confirm.data;
496 *p++ = tv.tv_sec;
497 *p++ = tv.tv_nsec;
498 }
499
500 static int
501 check_name(struct xdr_netobj name) {
502
503 if (name.len == 0)
504 return 0;
505 if (name.len > NFS4_OPAQUE_LIMIT) {
506 printk("NFSD: check_name: name too long(%d)!\n", name.len);
507 return 0;
508 }
509 return 1;
510 }
511
512 static void
513 add_to_unconfirmed(struct nfs4_client *clp, unsigned int strhashval)
514 {
515 unsigned int idhashval;
516
517 list_add(&clp->cl_strhash, &unconf_str_hashtbl[strhashval]);
518 idhashval = clientid_hashval(clp->cl_clientid.cl_id);
519 list_add(&clp->cl_idhash, &unconf_id_hashtbl[idhashval]);
520 list_add_tail(&clp->cl_lru, &client_lru);
521 clp->cl_time = get_seconds();
522 }
523
524 static void
525 move_to_confirmed(struct nfs4_client *clp)
526 {
527 unsigned int idhashval = clientid_hashval(clp->cl_clientid.cl_id);
528 unsigned int strhashval;
529
530 dprintk("NFSD: move_to_confirm nfs4_client %p\n", clp);
531 list_del_init(&clp->cl_strhash);
532 list_del_init(&clp->cl_idhash);
533 list_add(&clp->cl_idhash, &conf_id_hashtbl[idhashval]);
534 strhashval = clientstr_hashval(clp->cl_recdir);
535 list_add(&clp->cl_strhash, &conf_str_hashtbl[strhashval]);
536 renew_client(clp);
537 }
538
539 static struct nfs4_client *
540 find_confirmed_client(clientid_t *clid)
541 {
542 struct nfs4_client *clp;
543 unsigned int idhashval = clientid_hashval(clid->cl_id);
544
545 list_for_each_entry(clp, &conf_id_hashtbl[idhashval], cl_idhash) {
546 if (cmp_clid(&clp->cl_clientid, clid))
547 return clp;
548 }
549 return NULL;
550 }
551
552 static struct nfs4_client *
553 find_unconfirmed_client(clientid_t *clid)
554 {
555 struct nfs4_client *clp;
556 unsigned int idhashval = clientid_hashval(clid->cl_id);
557
558 list_for_each_entry(clp, &unconf_id_hashtbl[idhashval], cl_idhash) {
559 if (cmp_clid(&clp->cl_clientid, clid))
560 return clp;
561 }
562 return NULL;
563 }
564
565 static struct nfs4_client *
566 find_confirmed_client_by_str(const char *dname, unsigned int hashval)
567 {
568 struct nfs4_client *clp;
569
570 list_for_each_entry(clp, &conf_str_hashtbl[hashval], cl_strhash) {
571 if (same_name(clp->cl_recdir, dname))
572 return clp;
573 }
574 return NULL;
575 }
576
577 static struct nfs4_client *
578 find_unconfirmed_client_by_str(const char *dname, unsigned int hashval)
579 {
580 struct nfs4_client *clp;
581
582 list_for_each_entry(clp, &unconf_str_hashtbl[hashval], cl_strhash) {
583 if (same_name(clp->cl_recdir, dname))
584 return clp;
585 }
586 return NULL;
587 }
588
589 /* a helper function for parse_callback */
590 static int
591 parse_octet(unsigned int *lenp, char **addrp)
592 {
593 unsigned int len = *lenp;
594 char *p = *addrp;
595 int n = -1;
596 char c;
597
598 for (;;) {
599 if (!len)
600 break;
601 len--;
602 c = *p++;
603 if (c == '.')
604 break;
605 if ((c < '0') || (c > '9')) {
606 n = -1;
607 break;
608 }
609 if (n < 0)
610 n = 0;
611 n = (n * 10) + (c - '0');
612 if (n > 255) {
613 n = -1;
614 break;
615 }
616 }
617 *lenp = len;
618 *addrp = p;
619 return n;
620 }
621
622 /* parse and set the setclientid ipv4 callback address */
623 static int
624 parse_ipv4(unsigned int addr_len, char *addr_val, unsigned int *cbaddrp, unsigned short *cbportp)
625 {
626 int temp = 0;
627 u32 cbaddr = 0;
628 u16 cbport = 0;
629 u32 addrlen = addr_len;
630 char *addr = addr_val;
631 int i, shift;
632
633 /* ipaddress */
634 shift = 24;
635 for(i = 4; i > 0 ; i--) {
636 if ((temp = parse_octet(&addrlen, &addr)) < 0) {
637 return 0;
638 }
639 cbaddr |= (temp << shift);
640 if (shift > 0)
641 shift -= 8;
642 }
643 *cbaddrp = cbaddr;
644
645 /* port */
646 shift = 8;
647 for(i = 2; i > 0 ; i--) {
648 if ((temp = parse_octet(&addrlen, &addr)) < 0) {
649 return 0;
650 }
651 cbport |= (temp << shift);
652 if (shift > 0)
653 shift -= 8;
654 }
655 *cbportp = cbport;
656 return 1;
657 }
658
659 static void
660 gen_callback(struct nfs4_client *clp, struct nfsd4_setclientid *se)
661 {
662 struct nfs4_callback *cb = &clp->cl_callback;
663
664 /* Currently, we only support tcp for the callback channel */
665 if ((se->se_callback_netid_len != 3) || memcmp((char *)se->se_callback_netid_val, "tcp", 3))
666 goto out_err;
667
668 if ( !(parse_ipv4(se->se_callback_addr_len, se->se_callback_addr_val,
669 &cb->cb_addr, &cb->cb_port)))
670 goto out_err;
671 cb->cb_prog = se->se_callback_prog;
672 cb->cb_ident = se->se_callback_ident;
673 return;
674 out_err:
675 dprintk(KERN_INFO "NFSD: this client (clientid %08x/%08x) "
676 "will not receive delegations\n",
677 clp->cl_clientid.cl_boot, clp->cl_clientid.cl_id);
678
679 return;
680 }
681
682 /*
683 * RFC 3010 has a complex implmentation description of processing a
684 * SETCLIENTID request consisting of 5 bullets, labeled as
685 * CASE0 - CASE4 below.
686 *
687 * NOTES:
688 * callback information will be processed in a future patch
689 *
690 * an unconfirmed record is added when:
691 * NORMAL (part of CASE 4): there is no confirmed nor unconfirmed record.
692 * CASE 1: confirmed record found with matching name, principal,
693 * verifier, and clientid.
694 * CASE 2: confirmed record found with matching name, principal,
695 * and there is no unconfirmed record with matching
696 * name and principal
697 *
698 * an unconfirmed record is replaced when:
699 * CASE 3: confirmed record found with matching name, principal,
700 * and an unconfirmed record is found with matching
701 * name, principal, and with clientid and
702 * confirm that does not match the confirmed record.
703 * CASE 4: there is no confirmed record with matching name and
704 * principal. there is an unconfirmed record with
705 * matching name, principal.
706 *
707 * an unconfirmed record is deleted when:
708 * CASE 1: an unconfirmed record that matches input name, verifier,
709 * and confirmed clientid.
710 * CASE 4: any unconfirmed records with matching name and principal
711 * that exist after an unconfirmed record has been replaced
712 * as described above.
713 *
714 */
715 int
716 nfsd4_setclientid(struct svc_rqst *rqstp, struct nfsd4_setclientid *setclid)
717 {
718 u32 ip_addr = rqstp->rq_addr.sin_addr.s_addr;
719 struct xdr_netobj clname = {
720 .len = setclid->se_namelen,
721 .data = setclid->se_name,
722 };
723 nfs4_verifier clverifier = setclid->se_verf;
724 unsigned int strhashval;
725 struct nfs4_client *conf, *unconf, *new;
726 int status;
727 char dname[HEXDIR_LEN];
728
729 if (!check_name(clname))
730 return nfserr_inval;
731
732 status = nfs4_make_rec_clidname(dname, &clname);
733 if (status)
734 return status;
735
736 /*
737 * XXX The Duplicate Request Cache (DRC) has been checked (??)
738 * We get here on a DRC miss.
739 */
740
741 strhashval = clientstr_hashval(dname);
742
743 nfs4_lock_state();
744 conf = find_confirmed_client_by_str(dname, strhashval);
745 if (conf) {
746 /*
747 * CASE 0:
748 * clname match, confirmed, different principal
749 * or different ip_address
750 */
751 status = nfserr_clid_inuse;
752 if (!cmp_creds(&conf->cl_cred, &rqstp->rq_cred)
753 || conf->cl_addr != ip_addr) {
754 printk("NFSD: setclientid: string in use by client"
755 "(clientid %08x/%08x)\n",
756 conf->cl_clientid.cl_boot, conf->cl_clientid.cl_id);
757 goto out;
758 }
759 }
760 unconf = find_unconfirmed_client_by_str(dname, strhashval);
761 status = nfserr_resource;
762 if (!conf) {
763 /*
764 * CASE 4:
765 * placed first, because it is the normal case.
766 */
767 if (unconf)
768 expire_client(unconf);
769 new = create_client(clname, dname);
770 if (new == NULL)
771 goto out;
772 copy_verf(new, &clverifier);
773 new->cl_addr = ip_addr;
774 copy_cred(&new->cl_cred,&rqstp->rq_cred);
775 gen_clid(new);
776 gen_confirm(new);
777 gen_callback(new, setclid);
778 add_to_unconfirmed(new, strhashval);
779 } else if (cmp_verf(&conf->cl_verifier, &clverifier)) {
780 /*
781 * CASE 1:
782 * cl_name match, confirmed, principal match
783 * verifier match: probable callback update
784 *
785 * remove any unconfirmed nfs4_client with
786 * matching cl_name, cl_verifier, and cl_clientid
787 *
788 * create and insert an unconfirmed nfs4_client with same
789 * cl_name, cl_verifier, and cl_clientid as existing
790 * nfs4_client, but with the new callback info and a
791 * new cl_confirm
792 */
793 if (unconf) {
794 /* Note this is removing unconfirmed {*x***},
795 * which is stronger than RFC recommended {vxc**}.
796 * This has the advantage that there is at most
797 * one {*x***} in either list at any time.
798 */
799 expire_client(unconf);
800 }
801 new = create_client(clname, dname);
802 if (new == NULL)
803 goto out;
804 copy_verf(new,&conf->cl_verifier);
805 new->cl_addr = ip_addr;
806 copy_cred(&new->cl_cred,&rqstp->rq_cred);
807 copy_clid(new, conf);
808 gen_confirm(new);
809 gen_callback(new, setclid);
810 add_to_unconfirmed(new,strhashval);
811 } else if (!unconf) {
812 /*
813 * CASE 2:
814 * clname match, confirmed, principal match
815 * verfier does not match
816 * no unconfirmed. create a new unconfirmed nfs4_client
817 * using input clverifier, clname, and callback info
818 * and generate a new cl_clientid and cl_confirm.
819 */
820 new = create_client(clname, dname);
821 if (new == NULL)
822 goto out;
823 copy_verf(new,&clverifier);
824 new->cl_addr = ip_addr;
825 copy_cred(&new->cl_cred,&rqstp->rq_cred);
826 gen_clid(new);
827 gen_confirm(new);
828 gen_callback(new, setclid);
829 add_to_unconfirmed(new, strhashval);
830 } else if (!cmp_verf(&conf->cl_confirm, &unconf->cl_confirm)) {
831 /*
832 * CASE3:
833 * confirmed found (name, principal match)
834 * confirmed verifier does not match input clverifier
835 *
836 * unconfirmed found (name match)
837 * confirmed->cl_confirm != unconfirmed->cl_confirm
838 *
839 * remove unconfirmed.
840 *
841 * create an unconfirmed nfs4_client
842 * with same cl_name as existing confirmed nfs4_client,
843 * but with new callback info, new cl_clientid,
844 * new cl_verifier and a new cl_confirm
845 */
846 expire_client(unconf);
847 new = create_client(clname, dname);
848 if (new == NULL)
849 goto out;
850 copy_verf(new,&clverifier);
851 new->cl_addr = ip_addr;
852 copy_cred(&new->cl_cred,&rqstp->rq_cred);
853 gen_clid(new);
854 gen_confirm(new);
855 gen_callback(new, setclid);
856 add_to_unconfirmed(new, strhashval);
857 } else {
858 /* No cases hit !!! */
859 status = nfserr_inval;
860 goto out;
861
862 }
863 setclid->se_clientid.cl_boot = new->cl_clientid.cl_boot;
864 setclid->se_clientid.cl_id = new->cl_clientid.cl_id;
865 memcpy(setclid->se_confirm.data, new->cl_confirm.data, sizeof(setclid->se_confirm.data));
866 status = nfs_ok;
867 out:
868 nfs4_unlock_state();
869 return status;
870 }
871
872
873 /*
874 * RFC 3010 has a complex implmentation description of processing a
875 * SETCLIENTID_CONFIRM request consisting of 4 bullets describing
876 * processing on a DRC miss, labeled as CASE1 - CASE4 below.
877 *
878 * NOTE: callback information will be processed here in a future patch
879 */
880 int
881 nfsd4_setclientid_confirm(struct svc_rqst *rqstp, struct nfsd4_setclientid_confirm *setclientid_confirm)
882 {
883 u32 ip_addr = rqstp->rq_addr.sin_addr.s_addr;
884 struct nfs4_client *conf, *unconf;
885 nfs4_verifier confirm = setclientid_confirm->sc_confirm;
886 clientid_t * clid = &setclientid_confirm->sc_clientid;
887 int status;
888
889 if (STALE_CLIENTID(clid))
890 return nfserr_stale_clientid;
891 /*
892 * XXX The Duplicate Request Cache (DRC) has been checked (??)
893 * We get here on a DRC miss.
894 */
895
896 nfs4_lock_state();
897
898 conf = find_confirmed_client(clid);
899 unconf = find_unconfirmed_client(clid);
900
901 status = nfserr_clid_inuse;
902 if (conf && conf->cl_addr != ip_addr)
903 goto out;
904 if (unconf && unconf->cl_addr != ip_addr)
905 goto out;
906
907 if ((conf && unconf) &&
908 (cmp_verf(&unconf->cl_confirm, &confirm)) &&
909 (cmp_verf(&conf->cl_verifier, &unconf->cl_verifier)) &&
910 (same_name(conf->cl_recdir,unconf->cl_recdir)) &&
911 (!cmp_verf(&conf->cl_confirm, &unconf->cl_confirm))) {
912 /* CASE 1:
913 * unconf record that matches input clientid and input confirm.
914 * conf record that matches input clientid.
915 * conf and unconf records match names, verifiers
916 */
917 if (!cmp_creds(&conf->cl_cred, &unconf->cl_cred))
918 status = nfserr_clid_inuse;
919 else {
920 /* XXX: We just turn off callbacks until we can handle
921 * change request correctly. */
922 atomic_set(&conf->cl_callback.cb_set, 0);
923 gen_confirm(conf);
924 nfsd4_remove_clid_dir(unconf);
925 expire_client(unconf);
926 status = nfs_ok;
927
928 }
929 } else if ((conf && !unconf) ||
930 ((conf && unconf) &&
931 (!cmp_verf(&conf->cl_verifier, &unconf->cl_verifier) ||
932 !same_name(conf->cl_recdir, unconf->cl_recdir)))) {
933 /* CASE 2:
934 * conf record that matches input clientid.
935 * if unconf record matches input clientid, then
936 * unconf->cl_name or unconf->cl_verifier don't match the
937 * conf record.
938 */
939 if (!cmp_creds(&conf->cl_cred,&rqstp->rq_cred))
940 status = nfserr_clid_inuse;
941 else
942 status = nfs_ok;
943 } else if (!conf && unconf
944 && cmp_verf(&unconf->cl_confirm, &confirm)) {
945 /* CASE 3:
946 * conf record not found.
947 * unconf record found.
948 * unconf->cl_confirm matches input confirm
949 */
950 if (!cmp_creds(&unconf->cl_cred, &rqstp->rq_cred)) {
951 status = nfserr_clid_inuse;
952 } else {
953 unsigned int hash =
954 clientstr_hashval(unconf->cl_recdir);
955 conf = find_confirmed_client_by_str(unconf->cl_recdir,
956 hash);
957 if (conf) {
958 nfsd4_remove_clid_dir(conf);
959 expire_client(conf);
960 }
961 move_to_confirmed(unconf);
962 conf = unconf;
963 status = nfs_ok;
964 }
965 } else if ((!conf || (conf && !cmp_verf(&conf->cl_confirm, &confirm)))
966 && (!unconf || (unconf && !cmp_verf(&unconf->cl_confirm,
967 &confirm)))) {
968 /* CASE 4:
969 * conf record not found, or if conf, conf->cl_confirm does not
970 * match input confirm.
971 * unconf record not found, or if unconf, unconf->cl_confirm
972 * does not match input confirm.
973 */
974 status = nfserr_stale_clientid;
975 } else {
976 /* check that we have hit one of the cases...*/
977 status = nfserr_clid_inuse;
978 }
979 out:
980 if (!status)
981 nfsd4_probe_callback(conf);
982 nfs4_unlock_state();
983 return status;
984 }
985
986 /* OPEN Share state helper functions */
987 static inline struct nfs4_file *
988 alloc_init_file(struct inode *ino)
989 {
990 struct nfs4_file *fp;
991 unsigned int hashval = file_hashval(ino);
992
993 fp = kmem_cache_alloc(file_slab, GFP_KERNEL);
994 if (fp) {
995 kref_init(&fp->fi_ref);
996 INIT_LIST_HEAD(&fp->fi_hash);
997 INIT_LIST_HEAD(&fp->fi_stateids);
998 INIT_LIST_HEAD(&fp->fi_delegations);
999 list_add(&fp->fi_hash, &file_hashtbl[hashval]);
1000 fp->fi_inode = igrab(ino);
1001 fp->fi_id = current_fileid++;
1002 return fp;
1003 }
1004 return NULL;
1005 }
1006
1007 static void
1008 nfsd4_free_slab(kmem_cache_t **slab)
1009 {
1010 int status;
1011
1012 if (*slab == NULL)
1013 return;
1014 status = kmem_cache_destroy(*slab);
1015 *slab = NULL;
1016 WARN_ON(status);
1017 }
1018
1019 static void
1020 nfsd4_free_slabs(void)
1021 {
1022 nfsd4_free_slab(&stateowner_slab);
1023 nfsd4_free_slab(&file_slab);
1024 nfsd4_free_slab(&stateid_slab);
1025 nfsd4_free_slab(&deleg_slab);
1026 }
1027
1028 static int
1029 nfsd4_init_slabs(void)
1030 {
1031 stateowner_slab = kmem_cache_create("nfsd4_stateowners",
1032 sizeof(struct nfs4_stateowner), 0, 0, NULL, NULL);
1033 if (stateowner_slab == NULL)
1034 goto out_nomem;
1035 file_slab = kmem_cache_create("nfsd4_files",
1036 sizeof(struct nfs4_file), 0, 0, NULL, NULL);
1037 if (file_slab == NULL)
1038 goto out_nomem;
1039 stateid_slab = kmem_cache_create("nfsd4_stateids",
1040 sizeof(struct nfs4_stateid), 0, 0, NULL, NULL);
1041 if (stateid_slab == NULL)
1042 goto out_nomem;
1043 deleg_slab = kmem_cache_create("nfsd4_delegations",
1044 sizeof(struct nfs4_delegation), 0, 0, NULL, NULL);
1045 if (deleg_slab == NULL)
1046 goto out_nomem;
1047 return 0;
1048 out_nomem:
1049 nfsd4_free_slabs();
1050 dprintk("nfsd4: out of memory while initializing nfsv4\n");
1051 return -ENOMEM;
1052 }
1053
1054 void
1055 nfs4_free_stateowner(struct kref *kref)
1056 {
1057 struct nfs4_stateowner *sop =
1058 container_of(kref, struct nfs4_stateowner, so_ref);
1059 kfree(sop->so_owner.data);
1060 kmem_cache_free(stateowner_slab, sop);
1061 }
1062
1063 static inline struct nfs4_stateowner *
1064 alloc_stateowner(struct xdr_netobj *owner)
1065 {
1066 struct nfs4_stateowner *sop;
1067
1068 if ((sop = kmem_cache_alloc(stateowner_slab, GFP_KERNEL))) {
1069 if ((sop->so_owner.data = kmalloc(owner->len, GFP_KERNEL))) {
1070 memcpy(sop->so_owner.data, owner->data, owner->len);
1071 sop->so_owner.len = owner->len;
1072 kref_init(&sop->so_ref);
1073 return sop;
1074 }
1075 kmem_cache_free(stateowner_slab, sop);
1076 }
1077 return NULL;
1078 }
1079
1080 static struct nfs4_stateowner *
1081 alloc_init_open_stateowner(unsigned int strhashval, struct nfs4_client *clp, struct nfsd4_open *open) {
1082 struct nfs4_stateowner *sop;
1083 struct nfs4_replay *rp;
1084 unsigned int idhashval;
1085
1086 if (!(sop = alloc_stateowner(&open->op_owner)))
1087 return NULL;
1088 idhashval = ownerid_hashval(current_ownerid);
1089 INIT_LIST_HEAD(&sop->so_idhash);
1090 INIT_LIST_HEAD(&sop->so_strhash);
1091 INIT_LIST_HEAD(&sop->so_perclient);
1092 INIT_LIST_HEAD(&sop->so_stateids);
1093 INIT_LIST_HEAD(&sop->so_perstateid); /* not used */
1094 INIT_LIST_HEAD(&sop->so_close_lru);
1095 sop->so_time = 0;
1096 list_add(&sop->so_idhash, &ownerid_hashtbl[idhashval]);
1097 list_add(&sop->so_strhash, &ownerstr_hashtbl[strhashval]);
1098 list_add(&sop->so_perclient, &clp->cl_openowners);
1099 sop->so_is_open_owner = 1;
1100 sop->so_id = current_ownerid++;
1101 sop->so_client = clp;
1102 sop->so_seqid = open->op_seqid;
1103 sop->so_confirmed = 0;
1104 rp = &sop->so_replay;
1105 rp->rp_status = nfserr_serverfault;
1106 rp->rp_buflen = 0;
1107 rp->rp_buf = rp->rp_ibuf;
1108 return sop;
1109 }
1110
1111 static void
1112 release_stateid_lockowners(struct nfs4_stateid *open_stp)
1113 {
1114 struct nfs4_stateowner *lock_sop;
1115
1116 while (!list_empty(&open_stp->st_lockowners)) {
1117 lock_sop = list_entry(open_stp->st_lockowners.next,
1118 struct nfs4_stateowner, so_perstateid);
1119 /* list_del(&open_stp->st_lockowners); */
1120 BUG_ON(lock_sop->so_is_open_owner);
1121 release_stateowner(lock_sop);
1122 }
1123 }
1124
1125 static void
1126 unhash_stateowner(struct nfs4_stateowner *sop)
1127 {
1128 struct nfs4_stateid *stp;
1129
1130 list_del(&sop->so_idhash);
1131 list_del(&sop->so_strhash);
1132 if (sop->so_is_open_owner)
1133 list_del(&sop->so_perclient);
1134 list_del(&sop->so_perstateid);
1135 while (!list_empty(&sop->so_stateids)) {
1136 stp = list_entry(sop->so_stateids.next,
1137 struct nfs4_stateid, st_perstateowner);
1138 if (sop->so_is_open_owner)
1139 release_stateid(stp, OPEN_STATE);
1140 else
1141 release_stateid(stp, LOCK_STATE);
1142 }
1143 }
1144
1145 static void
1146 release_stateowner(struct nfs4_stateowner *sop)
1147 {
1148 unhash_stateowner(sop);
1149 list_del(&sop->so_close_lru);
1150 nfs4_put_stateowner(sop);
1151 }
1152
1153 static inline void
1154 init_stateid(struct nfs4_stateid *stp, struct nfs4_file *fp, struct nfsd4_open *open) {
1155 struct nfs4_stateowner *sop = open->op_stateowner;
1156 unsigned int hashval = stateid_hashval(sop->so_id, fp->fi_id);
1157
1158 INIT_LIST_HEAD(&stp->st_hash);
1159 INIT_LIST_HEAD(&stp->st_perstateowner);
1160 INIT_LIST_HEAD(&stp->st_lockowners);
1161 INIT_LIST_HEAD(&stp->st_perfile);
1162 list_add(&stp->st_hash, &stateid_hashtbl[hashval]);
1163 list_add(&stp->st_perstateowner, &sop->so_stateids);
1164 list_add(&stp->st_perfile, &fp->fi_stateids);
1165 stp->st_stateowner = sop;
1166 get_nfs4_file(fp);
1167 stp->st_file = fp;
1168 stp->st_stateid.si_boot = boot_time;
1169 stp->st_stateid.si_stateownerid = sop->so_id;
1170 stp->st_stateid.si_fileid = fp->fi_id;
1171 stp->st_stateid.si_generation = 0;
1172 stp->st_access_bmap = 0;
1173 stp->st_deny_bmap = 0;
1174 __set_bit(open->op_share_access, &stp->st_access_bmap);
1175 __set_bit(open->op_share_deny, &stp->st_deny_bmap);
1176 stp->st_openstp = NULL;
1177 }
1178
1179 static void
1180 release_stateid(struct nfs4_stateid *stp, int flags)
1181 {
1182 struct file *filp = stp->st_vfs_file;
1183
1184 list_del(&stp->st_hash);
1185 list_del(&stp->st_perfile);
1186 list_del(&stp->st_perstateowner);
1187 if (flags & OPEN_STATE) {
1188 release_stateid_lockowners(stp);
1189 stp->st_vfs_file = NULL;
1190 nfsd_close(filp);
1191 } else if (flags & LOCK_STATE)
1192 locks_remove_posix(filp, (fl_owner_t) stp->st_stateowner);
1193 put_nfs4_file(stp->st_file);
1194 kmem_cache_free(stateid_slab, stp);
1195 }
1196
1197 static void
1198 move_to_close_lru(struct nfs4_stateowner *sop)
1199 {
1200 dprintk("NFSD: move_to_close_lru nfs4_stateowner %p\n", sop);
1201
1202 unhash_stateowner(sop);
1203 list_add_tail(&sop->so_close_lru, &close_lru);
1204 sop->so_time = get_seconds();
1205 }
1206
1207 static int
1208 cmp_owner_str(struct nfs4_stateowner *sop, struct xdr_netobj *owner, clientid_t *clid) {
1209 return ((sop->so_owner.len == owner->len) &&
1210 !memcmp(sop->so_owner.data, owner->data, owner->len) &&
1211 (sop->so_client->cl_clientid.cl_id == clid->cl_id));
1212 }
1213
1214 static struct nfs4_stateowner *
1215 find_openstateowner_str(unsigned int hashval, struct nfsd4_open *open)
1216 {
1217 struct nfs4_stateowner *so = NULL;
1218
1219 list_for_each_entry(so, &ownerstr_hashtbl[hashval], so_strhash) {
1220 if (cmp_owner_str(so, &open->op_owner, &open->op_clientid))
1221 return so;
1222 }
1223 return NULL;
1224 }
1225
1226 /* search file_hashtbl[] for file */
1227 static struct nfs4_file *
1228 find_file(struct inode *ino)
1229 {
1230 unsigned int hashval = file_hashval(ino);
1231 struct nfs4_file *fp;
1232
1233 list_for_each_entry(fp, &file_hashtbl[hashval], fi_hash) {
1234 if (fp->fi_inode == ino) {
1235 get_nfs4_file(fp);
1236 return fp;
1237 }
1238 }
1239 return NULL;
1240 }
1241
1242 #define TEST_ACCESS(x) ((x > 0 || x < 4)?1:0)
1243 #define TEST_DENY(x) ((x >= 0 || x < 5)?1:0)
1244
1245 static void
1246 set_access(unsigned int *access, unsigned long bmap) {
1247 int i;
1248
1249 *access = 0;
1250 for (i = 1; i < 4; i++) {
1251 if (test_bit(i, &bmap))
1252 *access |= i;
1253 }
1254 }
1255
1256 static void
1257 set_deny(unsigned int *deny, unsigned long bmap) {
1258 int i;
1259
1260 *deny = 0;
1261 for (i = 0; i < 4; i++) {
1262 if (test_bit(i, &bmap))
1263 *deny |= i ;
1264 }
1265 }
1266
1267 static int
1268 test_share(struct nfs4_stateid *stp, struct nfsd4_open *open) {
1269 unsigned int access, deny;
1270
1271 set_access(&access, stp->st_access_bmap);
1272 set_deny(&deny, stp->st_deny_bmap);
1273 if ((access & open->op_share_deny) || (deny & open->op_share_access))
1274 return 0;
1275 return 1;
1276 }
1277
1278 /*
1279 * Called to check deny when READ with all zero stateid or
1280 * WRITE with all zero or all one stateid
1281 */
1282 static int
1283 nfs4_share_conflict(struct svc_fh *current_fh, unsigned int deny_type)
1284 {
1285 struct inode *ino = current_fh->fh_dentry->d_inode;
1286 struct nfs4_file *fp;
1287 struct nfs4_stateid *stp;
1288 int ret;
1289
1290 dprintk("NFSD: nfs4_share_conflict\n");
1291
1292 fp = find_file(ino);
1293 if (!fp)
1294 return nfs_ok;
1295 ret = nfserr_locked;
1296 /* Search for conflicting share reservations */
1297 list_for_each_entry(stp, &fp->fi_stateids, st_perfile) {
1298 if (test_bit(deny_type, &stp->st_deny_bmap) ||
1299 test_bit(NFS4_SHARE_DENY_BOTH, &stp->st_deny_bmap))
1300 goto out;
1301 }
1302 ret = nfs_ok;
1303 out:
1304 put_nfs4_file(fp);
1305 return ret;
1306 }
1307
1308 static inline void
1309 nfs4_file_downgrade(struct file *filp, unsigned int share_access)
1310 {
1311 if (share_access & NFS4_SHARE_ACCESS_WRITE) {
1312 put_write_access(filp->f_dentry->d_inode);
1313 filp->f_mode = (filp->f_mode | FMODE_READ) & ~FMODE_WRITE;
1314 }
1315 }
1316
1317 /*
1318 * Recall a delegation
1319 */
1320 static int
1321 do_recall(void *__dp)
1322 {
1323 struct nfs4_delegation *dp = __dp;
1324
1325 daemonize("nfsv4-recall");
1326
1327 nfsd4_cb_recall(dp);
1328 return 0;
1329 }
1330
1331 /*
1332 * Spawn a thread to perform a recall on the delegation represented
1333 * by the lease (file_lock)
1334 *
1335 * Called from break_lease() with lock_kernel() held.
1336 * Note: we assume break_lease will only call this *once* for any given
1337 * lease.
1338 */
1339 static
1340 void nfsd_break_deleg_cb(struct file_lock *fl)
1341 {
1342 struct nfs4_delegation *dp= (struct nfs4_delegation *)fl->fl_owner;
1343 struct task_struct *t;
1344
1345 dprintk("NFSD nfsd_break_deleg_cb: dp %p fl %p\n",dp,fl);
1346 if (!dp)
1347 return;
1348
1349 /* We're assuming the state code never drops its reference
1350 * without first removing the lease. Since we're in this lease
1351 * callback (and since the lease code is serialized by the kernel
1352 * lock) we know the server hasn't removed the lease yet, we know
1353 * it's safe to take a reference: */
1354 atomic_inc(&dp->dl_count);
1355
1356 spin_lock(&recall_lock);
1357 list_add_tail(&dp->dl_recall_lru, &del_recall_lru);
1358 spin_unlock(&recall_lock);
1359
1360 /* only place dl_time is set. protected by lock_kernel*/
1361 dp->dl_time = get_seconds();
1362
1363 /* XXX need to merge NFSD_LEASE_TIME with fs/locks.c:lease_break_time */
1364 fl->fl_break_time = jiffies + NFSD_LEASE_TIME * HZ;
1365
1366 t = kthread_run(do_recall, dp, "%s", "nfs4_cb_recall");
1367 if (IS_ERR(t)) {
1368 struct nfs4_client *clp = dp->dl_client;
1369
1370 printk(KERN_INFO "NFSD: Callback thread failed for "
1371 "for client (clientid %08x/%08x)\n",
1372 clp->cl_clientid.cl_boot, clp->cl_clientid.cl_id);
1373 nfs4_put_delegation(dp);
1374 }
1375 }
1376
1377 /*
1378 * The file_lock is being reapd.
1379 *
1380 * Called by locks_free_lock() with lock_kernel() held.
1381 */
1382 static
1383 void nfsd_release_deleg_cb(struct file_lock *fl)
1384 {
1385 struct nfs4_delegation *dp = (struct nfs4_delegation *)fl->fl_owner;
1386
1387 dprintk("NFSD nfsd_release_deleg_cb: fl %p dp %p dl_count %d\n", fl,dp, atomic_read(&dp->dl_count));
1388
1389 if (!(fl->fl_flags & FL_LEASE) || !dp)
1390 return;
1391 dp->dl_flock = NULL;
1392 }
1393
1394 /*
1395 * Set the delegation file_lock back pointer.
1396 *
1397 * Called from __setlease() with lock_kernel() held.
1398 */
1399 static
1400 void nfsd_copy_lock_deleg_cb(struct file_lock *new, struct file_lock *fl)
1401 {
1402 struct nfs4_delegation *dp = (struct nfs4_delegation *)new->fl_owner;
1403
1404 dprintk("NFSD: nfsd_copy_lock_deleg_cb: new fl %p dp %p\n", new, dp);
1405 if (!dp)
1406 return;
1407 dp->dl_flock = new;
1408 }
1409
1410 /*
1411 * Called from __setlease() with lock_kernel() held
1412 */
1413 static
1414 int nfsd_same_client_deleg_cb(struct file_lock *onlist, struct file_lock *try)
1415 {
1416 struct nfs4_delegation *onlistd =
1417 (struct nfs4_delegation *)onlist->fl_owner;
1418 struct nfs4_delegation *tryd =
1419 (struct nfs4_delegation *)try->fl_owner;
1420
1421 if (onlist->fl_lmops != try->fl_lmops)
1422 return 0;
1423
1424 return onlistd->dl_client == tryd->dl_client;
1425 }
1426
1427
1428 static
1429 int nfsd_change_deleg_cb(struct file_lock **onlist, int arg)
1430 {
1431 if (arg & F_UNLCK)
1432 return lease_modify(onlist, arg);
1433 else
1434 return -EAGAIN;
1435 }
1436
1437 static struct lock_manager_operations nfsd_lease_mng_ops = {
1438 .fl_break = nfsd_break_deleg_cb,
1439 .fl_release_private = nfsd_release_deleg_cb,
1440 .fl_copy_lock = nfsd_copy_lock_deleg_cb,
1441 .fl_mylease = nfsd_same_client_deleg_cb,
1442 .fl_change = nfsd_change_deleg_cb,
1443 };
1444
1445
1446 int
1447 nfsd4_process_open1(struct nfsd4_open *open)
1448 {
1449 clientid_t *clientid = &open->op_clientid;
1450 struct nfs4_client *clp = NULL;
1451 unsigned int strhashval;
1452 struct nfs4_stateowner *sop = NULL;
1453
1454 if (!check_name(open->op_owner))
1455 return nfserr_inval;
1456
1457 if (STALE_CLIENTID(&open->op_clientid))
1458 return nfserr_stale_clientid;
1459
1460 strhashval = ownerstr_hashval(clientid->cl_id, open->op_owner);
1461 sop = find_openstateowner_str(strhashval, open);
1462 open->op_stateowner = sop;
1463 if (!sop) {
1464 /* Make sure the client's lease hasn't expired. */
1465 clp = find_confirmed_client(clientid);
1466 if (clp == NULL)
1467 return nfserr_expired;
1468 goto renew;
1469 }
1470 if (!sop->so_confirmed) {
1471 /* Replace unconfirmed owners without checking for replay. */
1472 clp = sop->so_client;
1473 release_stateowner(sop);
1474 open->op_stateowner = NULL;
1475 goto renew;
1476 }
1477 if (open->op_seqid == sop->so_seqid - 1) {
1478 if (sop->so_replay.rp_buflen)
1479 return NFSERR_REPLAY_ME;
1480 /* The original OPEN failed so spectacularly
1481 * that we don't even have replay data saved!
1482 * Therefore, we have no choice but to continue
1483 * processing this OPEN; presumably, we'll
1484 * fail again for the same reason.
1485 */
1486 dprintk("nfsd4_process_open1: replay with no replay cache\n");
1487 goto renew;
1488 }
1489 if (open->op_seqid != sop->so_seqid)
1490 return nfserr_bad_seqid;
1491 renew:
1492 if (open->op_stateowner == NULL) {
1493 sop = alloc_init_open_stateowner(strhashval, clp, open);
1494 if (sop == NULL)
1495 return nfserr_resource;
1496 open->op_stateowner = sop;
1497 }
1498 list_del_init(&sop->so_close_lru);
1499 renew_client(sop->so_client);
1500 return nfs_ok;
1501 }
1502
1503 static inline int
1504 nfs4_check_delegmode(struct nfs4_delegation *dp, int flags)
1505 {
1506 if ((flags & WR_STATE) && (dp->dl_type == NFS4_OPEN_DELEGATE_READ))
1507 return nfserr_openmode;
1508 else
1509 return nfs_ok;
1510 }
1511
1512 static struct nfs4_delegation *
1513 find_delegation_file(struct nfs4_file *fp, stateid_t *stid)
1514 {
1515 struct nfs4_delegation *dp;
1516
1517 list_for_each_entry(dp, &fp->fi_delegations, dl_perfile) {
1518 if (dp->dl_stateid.si_stateownerid == stid->si_stateownerid)
1519 return dp;
1520 }
1521 return NULL;
1522 }
1523
1524 static int
1525 nfs4_check_deleg(struct nfs4_file *fp, struct nfsd4_open *open,
1526 struct nfs4_delegation **dp)
1527 {
1528 int flags;
1529 int status = nfserr_bad_stateid;
1530
1531 *dp = find_delegation_file(fp, &open->op_delegate_stateid);
1532 if (*dp == NULL)
1533 goto out;
1534 flags = open->op_share_access == NFS4_SHARE_ACCESS_READ ?
1535 RD_STATE : WR_STATE;
1536 status = nfs4_check_delegmode(*dp, flags);
1537 if (status)
1538 *dp = NULL;
1539 out:
1540 if (open->op_claim_type != NFS4_OPEN_CLAIM_DELEGATE_CUR)
1541 return nfs_ok;
1542 if (status)
1543 return status;
1544 open->op_stateowner->so_confirmed = 1;
1545 return nfs_ok;
1546 }
1547
1548 static int
1549 nfs4_check_open(struct nfs4_file *fp, struct nfsd4_open *open, struct nfs4_stateid **stpp)
1550 {
1551 struct nfs4_stateid *local;
1552 int status = nfserr_share_denied;
1553 struct nfs4_stateowner *sop = open->op_stateowner;
1554
1555 list_for_each_entry(local, &fp->fi_stateids, st_perfile) {
1556 /* ignore lock owners */
1557 if (local->st_stateowner->so_is_open_owner == 0)
1558 continue;
1559 /* remember if we have seen this open owner */
1560 if (local->st_stateowner == sop)
1561 *stpp = local;
1562 /* check for conflicting share reservations */
1563 if (!test_share(local, open))
1564 goto out;
1565 }
1566 status = 0;
1567 out:
1568 return status;
1569 }
1570
1571 static inline struct nfs4_stateid *
1572 nfs4_alloc_stateid(void)
1573 {
1574 return kmem_cache_alloc(stateid_slab, GFP_KERNEL);
1575 }
1576
1577 static int
1578 nfs4_new_open(struct svc_rqst *rqstp, struct nfs4_stateid **stpp,
1579 struct nfs4_delegation *dp,
1580 struct svc_fh *cur_fh, int flags)
1581 {
1582 struct nfs4_stateid *stp;
1583
1584 stp = nfs4_alloc_stateid();
1585 if (stp == NULL)
1586 return nfserr_resource;
1587
1588 if (dp) {
1589 get_file(dp->dl_vfs_file);
1590 stp->st_vfs_file = dp->dl_vfs_file;
1591 } else {
1592 int status;
1593 status = nfsd_open(rqstp, cur_fh, S_IFREG, flags,
1594 &stp->st_vfs_file);
1595 if (status) {
1596 if (status == nfserr_dropit)
1597 status = nfserr_jukebox;
1598 kmem_cache_free(stateid_slab, stp);
1599 return status;
1600 }
1601 }
1602 *stpp = stp;
1603 return 0;
1604 }
1605
1606 static inline int
1607 nfsd4_truncate(struct svc_rqst *rqstp, struct svc_fh *fh,
1608 struct nfsd4_open *open)
1609 {
1610 struct iattr iattr = {
1611 .ia_valid = ATTR_SIZE,
1612 .ia_size = 0,
1613 };
1614 if (!open->op_truncate)
1615 return 0;
1616 if (!(open->op_share_access & NFS4_SHARE_ACCESS_WRITE))
1617 return nfserr_inval;
1618 return nfsd_setattr(rqstp, fh, &iattr, 0, (time_t)0);
1619 }
1620
1621 static int
1622 nfs4_upgrade_open(struct svc_rqst *rqstp, struct svc_fh *cur_fh, struct nfs4_stateid *stp, struct nfsd4_open *open)
1623 {
1624 struct file *filp = stp->st_vfs_file;
1625 struct inode *inode = filp->f_dentry->d_inode;
1626 unsigned int share_access, new_writer;
1627 int status;
1628
1629 set_access(&share_access, stp->st_access_bmap);
1630 new_writer = (~share_access) & open->op_share_access
1631 & NFS4_SHARE_ACCESS_WRITE;
1632
1633 if (new_writer) {
1634 status = get_write_access(inode);
1635 if (status)
1636 return nfserrno(status);
1637 }
1638 status = nfsd4_truncate(rqstp, cur_fh, open);
1639 if (status) {
1640 if (new_writer)
1641 put_write_access(inode);
1642 return status;
1643 }
1644 /* remember the open */
1645 filp->f_mode |= open->op_share_access;
1646 set_bit(open->op_share_access, &stp->st_access_bmap);
1647 set_bit(open->op_share_deny, &stp->st_deny_bmap);
1648
1649 return nfs_ok;
1650 }
1651
1652
1653 static void
1654 nfs4_set_claim_prev(struct nfsd4_open *open)
1655 {
1656 open->op_stateowner->so_confirmed = 1;
1657 open->op_stateowner->so_client->cl_firststate = 1;
1658 }
1659
1660 /*
1661 * Attempt to hand out a delegation.
1662 */
1663 static void
1664 nfs4_open_delegation(struct svc_fh *fh, struct nfsd4_open *open, struct nfs4_stateid *stp)
1665 {
1666 struct nfs4_delegation *dp;
1667 struct nfs4_stateowner *sop = stp->st_stateowner;
1668 struct nfs4_callback *cb = &sop->so_client->cl_callback;
1669 struct file_lock fl, *flp = &fl;
1670 int status, flag = 0;
1671
1672 flag = NFS4_OPEN_DELEGATE_NONE;
1673 open->op_recall = 0;
1674 switch (open->op_claim_type) {
1675 case NFS4_OPEN_CLAIM_PREVIOUS:
1676 if (!atomic_read(&cb->cb_set))
1677 open->op_recall = 1;
1678 flag = open->op_delegate_type;
1679 if (flag == NFS4_OPEN_DELEGATE_NONE)
1680 goto out;
1681 break;
1682 case NFS4_OPEN_CLAIM_NULL:
1683 /* Let's not give out any delegations till everyone's
1684 * had the chance to reclaim theirs.... */
1685 if (nfs4_in_grace())
1686 goto out;
1687 if (!atomic_read(&cb->cb_set) || !sop->so_confirmed)
1688 goto out;
1689 if (open->op_share_access & NFS4_SHARE_ACCESS_WRITE)
1690 flag = NFS4_OPEN_DELEGATE_WRITE;
1691 else
1692 flag = NFS4_OPEN_DELEGATE_READ;
1693 break;
1694 default:
1695 goto out;
1696 }
1697
1698 dp = alloc_init_deleg(sop->so_client, stp, fh, flag);
1699 if (dp == NULL) {
1700 flag = NFS4_OPEN_DELEGATE_NONE;
1701 goto out;
1702 }
1703 locks_init_lock(&fl);
1704 fl.fl_lmops = &nfsd_lease_mng_ops;
1705 fl.fl_flags = FL_LEASE;
1706 fl.fl_end = OFFSET_MAX;
1707 fl.fl_owner = (fl_owner_t)dp;
1708 fl.fl_file = stp->st_vfs_file;
1709 fl.fl_pid = current->tgid;
1710
1711 /* setlease checks to see if delegation should be handed out.
1712 * the lock_manager callbacks fl_mylease and fl_change are used
1713 */
1714 if ((status = setlease(stp->st_vfs_file,
1715 flag == NFS4_OPEN_DELEGATE_READ? F_RDLCK: F_WRLCK, &flp))) {
1716 dprintk("NFSD: setlease failed [%d], no delegation\n", status);
1717 unhash_delegation(dp);
1718 flag = NFS4_OPEN_DELEGATE_NONE;
1719 goto out;
1720 }
1721
1722 memcpy(&open->op_delegate_stateid, &dp->dl_stateid, sizeof(dp->dl_stateid));
1723
1724 dprintk("NFSD: delegation stateid=(%08x/%08x/%08x/%08x)\n\n",
1725 dp->dl_stateid.si_boot,
1726 dp->dl_stateid.si_stateownerid,
1727 dp->dl_stateid.si_fileid,
1728 dp->dl_stateid.si_generation);
1729 out:
1730 if (open->op_claim_type == NFS4_OPEN_CLAIM_PREVIOUS
1731 && flag == NFS4_OPEN_DELEGATE_NONE
1732 && open->op_delegate_type != NFS4_OPEN_DELEGATE_NONE)
1733 printk("NFSD: WARNING: refusing delegation reclaim\n");
1734 open->op_delegate_type = flag;
1735 }
1736
1737 /*
1738 * called with nfs4_lock_state() held.
1739 */
1740 int
1741 nfsd4_process_open2(struct svc_rqst *rqstp, struct svc_fh *current_fh, struct nfsd4_open *open)
1742 {
1743 struct nfs4_file *fp = NULL;
1744 struct inode *ino = current_fh->fh_dentry->d_inode;
1745 struct nfs4_stateid *stp = NULL;
1746 struct nfs4_delegation *dp = NULL;
1747 int status;
1748
1749 status = nfserr_inval;
1750 if (!TEST_ACCESS(open->op_share_access) || !TEST_DENY(open->op_share_deny))
1751 goto out;
1752 /*
1753 * Lookup file; if found, lookup stateid and check open request,
1754 * and check for delegations in the process of being recalled.
1755 * If not found, create the nfs4_file struct
1756 */
1757 fp = find_file(ino);
1758 if (fp) {
1759 if ((status = nfs4_check_open(fp, open, &stp)))
1760 goto out;
1761 status = nfs4_check_deleg(fp, open, &dp);
1762 if (status)
1763 goto out;
1764 } else {
1765 status = nfserr_bad_stateid;
1766 if (open->op_claim_type == NFS4_OPEN_CLAIM_DELEGATE_CUR)
1767 goto out;
1768 status = nfserr_resource;
1769 fp = alloc_init_file(ino);
1770 if (fp == NULL)
1771 goto out;
1772 }
1773
1774 /*
1775 * OPEN the file, or upgrade an existing OPEN.
1776 * If truncate fails, the OPEN fails.
1777 */
1778 if (stp) {
1779 /* Stateid was found, this is an OPEN upgrade */
1780 status = nfs4_upgrade_open(rqstp, current_fh, stp, open);
1781 if (status)
1782 goto out;
1783 update_stateid(&stp->st_stateid);
1784 } else {
1785 /* Stateid was not found, this is a new OPEN */
1786 int flags = 0;
1787 if (open->op_share_access & NFS4_SHARE_ACCESS_WRITE)
1788 flags = MAY_WRITE;
1789 else
1790 flags = MAY_READ;
1791 status = nfs4_new_open(rqstp, &stp, dp, current_fh, flags);
1792 if (status)
1793 goto out;
1794 init_stateid(stp, fp, open);
1795 status = nfsd4_truncate(rqstp, current_fh, open);
1796 if (status) {
1797 release_stateid(stp, OPEN_STATE);
1798 goto out;
1799 }
1800 }
1801 memcpy(&open->op_stateid, &stp->st_stateid, sizeof(stateid_t));
1802
1803 /*
1804 * Attempt to hand out a delegation. No error return, because the
1805 * OPEN succeeds even if we fail.
1806 */
1807 nfs4_open_delegation(current_fh, open, stp);
1808
1809 status = nfs_ok;
1810
1811 dprintk("nfs4_process_open2: stateid=(%08x/%08x/%08x/%08x)\n",
1812 stp->st_stateid.si_boot, stp->st_stateid.si_stateownerid,
1813 stp->st_stateid.si_fileid, stp->st_stateid.si_generation);
1814 out:
1815 if (fp)
1816 put_nfs4_file(fp);
1817 if (status == 0 && open->op_claim_type == NFS4_OPEN_CLAIM_PREVIOUS)
1818 nfs4_set_claim_prev(open);
1819 /*
1820 * To finish the open response, we just need to set the rflags.
1821 */
1822 open->op_rflags = NFS4_OPEN_RESULT_LOCKTYPE_POSIX;
1823 if (!open->op_stateowner->so_confirmed)
1824 open->op_rflags |= NFS4_OPEN_RESULT_CONFIRM;
1825
1826 return status;
1827 }
1828
1829 static struct workqueue_struct *laundry_wq;
1830 static struct work_struct laundromat_work;
1831 static void laundromat_main(void *);
1832 static DECLARE_WORK(laundromat_work, laundromat_main, NULL);
1833
1834 int
1835 nfsd4_renew(clientid_t *clid)
1836 {
1837 struct nfs4_client *clp;
1838 int status;
1839
1840 nfs4_lock_state();
1841 dprintk("process_renew(%08x/%08x): starting\n",
1842 clid->cl_boot, clid->cl_id);
1843 status = nfserr_stale_clientid;
1844 if (STALE_CLIENTID(clid))
1845 goto out;
1846 clp = find_confirmed_client(clid);
1847 status = nfserr_expired;
1848 if (clp == NULL) {
1849 /* We assume the client took too long to RENEW. */
1850 dprintk("nfsd4_renew: clientid not found!\n");
1851 goto out;
1852 }
1853 renew_client(clp);
1854 status = nfserr_cb_path_down;
1855 if (!list_empty(&clp->cl_delegations)
1856 && !atomic_read(&clp->cl_callback.cb_set))
1857 goto out;
1858 status = nfs_ok;
1859 out:
1860 nfs4_unlock_state();
1861 return status;
1862 }
1863
1864 static void
1865 end_grace(void)
1866 {
1867 dprintk("NFSD: end of grace period\n");
1868 nfsd4_recdir_purge_old();
1869 in_grace = 0;
1870 }
1871
1872 static time_t
1873 nfs4_laundromat(void)
1874 {
1875 struct nfs4_client *clp;
1876 struct nfs4_stateowner *sop;
1877 struct nfs4_delegation *dp;
1878 struct list_head *pos, *next, reaplist;
1879 time_t cutoff = get_seconds() - NFSD_LEASE_TIME;
1880 time_t t, clientid_val = NFSD_LEASE_TIME;
1881 time_t u, test_val = NFSD_LEASE_TIME;
1882
1883 nfs4_lock_state();
1884
1885 dprintk("NFSD: laundromat service - starting\n");
1886 if (in_grace)
1887 end_grace();
1888 list_for_each_safe(pos, next, &client_lru) {
1889 clp = list_entry(pos, struct nfs4_client, cl_lru);
1890 if (time_after((unsigned long)clp->cl_time, (unsigned long)cutoff)) {
1891 t = clp->cl_time - cutoff;
1892 if (clientid_val > t)
1893 clientid_val = t;
1894 break;
1895 }
1896 dprintk("NFSD: purging unused client (clientid %08x)\n",
1897 clp->cl_clientid.cl_id);
1898 nfsd4_remove_clid_dir(clp);
1899 expire_client(clp);
1900 }
1901 INIT_LIST_HEAD(&reaplist);
1902 spin_lock(&recall_lock);
1903 list_for_each_safe(pos, next, &del_recall_lru) {
1904 dp = list_entry (pos, struct nfs4_delegation, dl_recall_lru);
1905 if (time_after((unsigned long)dp->dl_time, (unsigned long)cutoff)) {
1906 u = dp->dl_time - cutoff;
1907 if (test_val > u)
1908 test_val = u;
1909 break;
1910 }
1911 dprintk("NFSD: purging unused delegation dp %p, fp %p\n",
1912 dp, dp->dl_flock);
1913 list_move(&dp->dl_recall_lru, &reaplist);
1914 }
1915 spin_unlock(&recall_lock);
1916 list_for_each_safe(pos, next, &reaplist) {
1917 dp = list_entry (pos, struct nfs4_delegation, dl_recall_lru);
1918 list_del_init(&dp->dl_recall_lru);
1919 unhash_delegation(dp);
1920 }
1921 test_val = NFSD_LEASE_TIME;
1922 list_for_each_safe(pos, next, &close_lru) {
1923 sop = list_entry(pos, struct nfs4_stateowner, so_close_lru);
1924 if (time_after((unsigned long)sop->so_time, (unsigned long)cutoff)) {
1925 u = sop->so_time - cutoff;
1926 if (test_val > u)
1927 test_val = u;
1928 break;
1929 }
1930 dprintk("NFSD: purging unused open stateowner (so_id %d)\n",
1931 sop->so_id);
1932 list_del(&sop->so_close_lru);
1933 nfs4_put_stateowner(sop);
1934 }
1935 if (clientid_val < NFSD_LAUNDROMAT_MINTIMEOUT)
1936 clientid_val = NFSD_LAUNDROMAT_MINTIMEOUT;
1937 nfs4_unlock_state();
1938 return clientid_val;
1939 }
1940
1941 void
1942 laundromat_main(void *not_used)
1943 {
1944 time_t t;
1945
1946 t = nfs4_laundromat();
1947 dprintk("NFSD: laundromat_main - sleeping for %ld seconds\n", t);
1948 queue_delayed_work(laundry_wq, &laundromat_work, t*HZ);
1949 }
1950
1951 static struct nfs4_stateowner *
1952 search_close_lru(u32 st_id, int flags)
1953 {
1954 struct nfs4_stateowner *local = NULL;
1955
1956 if (flags & CLOSE_STATE) {
1957 list_for_each_entry(local, &close_lru, so_close_lru) {
1958 if (local->so_id == st_id)
1959 return local;
1960 }
1961 }
1962 return NULL;
1963 }
1964
1965 static inline int
1966 nfs4_check_fh(struct svc_fh *fhp, struct nfs4_stateid *stp)
1967 {
1968 return fhp->fh_dentry->d_inode != stp->st_vfs_file->f_dentry->d_inode;
1969 }
1970
1971 static int
1972 STALE_STATEID(stateid_t *stateid)
1973 {
1974 if (stateid->si_boot == boot_time)
1975 return 0;
1976 dprintk("NFSD: stale stateid (%08x/%08x/%08x/%08x)!\n",
1977 stateid->si_boot, stateid->si_stateownerid, stateid->si_fileid,
1978 stateid->si_generation);
1979 return 1;
1980 }
1981
1982 static inline int
1983 access_permit_read(unsigned long access_bmap)
1984 {
1985 return test_bit(NFS4_SHARE_ACCESS_READ, &access_bmap) ||
1986 test_bit(NFS4_SHARE_ACCESS_BOTH, &access_bmap) ||
1987 test_bit(NFS4_SHARE_ACCESS_WRITE, &access_bmap);
1988 }
1989
1990 static inline int
1991 access_permit_write(unsigned long access_bmap)
1992 {
1993 return test_bit(NFS4_SHARE_ACCESS_WRITE, &access_bmap) ||
1994 test_bit(NFS4_SHARE_ACCESS_BOTH, &access_bmap);
1995 }
1996
1997 static
1998 int nfs4_check_openmode(struct nfs4_stateid *stp, int flags)
1999 {
2000 int status = nfserr_openmode;
2001
2002 if ((flags & WR_STATE) && (!access_permit_write(stp->st_access_bmap)))
2003 goto out;
2004 if ((flags & RD_STATE) && (!access_permit_read(stp->st_access_bmap)))
2005 goto out;
2006 status = nfs_ok;
2007 out:
2008 return status;
2009 }
2010
2011 static inline int
2012 check_special_stateids(svc_fh *current_fh, stateid_t *stateid, int flags)
2013 {
2014 /* Trying to call delegreturn with a special stateid? Yuch: */
2015 if (!(flags & (RD_STATE | WR_STATE)))
2016 return nfserr_bad_stateid;
2017 else if (ONE_STATEID(stateid) && (flags & RD_STATE))
2018 return nfs_ok;
2019 else if (nfs4_in_grace()) {
2020 /* Answer in remaining cases depends on existance of
2021 * conflicting state; so we must wait out the grace period. */
2022 return nfserr_grace;
2023 } else if (flags & WR_STATE)
2024 return nfs4_share_conflict(current_fh,
2025 NFS4_SHARE_DENY_WRITE);
2026 else /* (flags & RD_STATE) && ZERO_STATEID(stateid) */
2027 return nfs4_share_conflict(current_fh,
2028 NFS4_SHARE_DENY_READ);
2029 }
2030
2031 /*
2032 * Allow READ/WRITE during grace period on recovered state only for files
2033 * that are not able to provide mandatory locking.
2034 */
2035 static inline int
2036 io_during_grace_disallowed(struct inode *inode, int flags)
2037 {
2038 return nfs4_in_grace() && (flags & (RD_STATE | WR_STATE))
2039 && MANDATORY_LOCK(inode);
2040 }
2041
2042 /*
2043 * Checks for stateid operations
2044 */
2045 int
2046 nfs4_preprocess_stateid_op(struct svc_fh *current_fh, stateid_t *stateid, int flags, struct file **filpp)
2047 {
2048 struct nfs4_stateid *stp = NULL;
2049 struct nfs4_delegation *dp = NULL;
2050 stateid_t *stidp;
2051 struct inode *ino = current_fh->fh_dentry->d_inode;
2052 int status;
2053
2054 dprintk("NFSD: preprocess_stateid_op: stateid = (%08x/%08x/%08x/%08x)\n",
2055 stateid->si_boot, stateid->si_stateownerid,
2056 stateid->si_fileid, stateid->si_generation);
2057 if (filpp)
2058 *filpp = NULL;
2059
2060 if (io_during_grace_disallowed(ino, flags))
2061 return nfserr_grace;
2062
2063 if (ZERO_STATEID(stateid) || ONE_STATEID(stateid))
2064 return check_special_stateids(current_fh, stateid, flags);
2065
2066 /* STALE STATEID */
2067 status = nfserr_stale_stateid;
2068 if (STALE_STATEID(stateid))
2069 goto out;
2070
2071 /* BAD STATEID */
2072 status = nfserr_bad_stateid;
2073 if (!stateid->si_fileid) { /* delegation stateid */
2074 if(!(dp = find_delegation_stateid(ino, stateid))) {
2075 dprintk("NFSD: delegation stateid not found\n");
2076 if (nfs4_in_grace())
2077 status = nfserr_grace;
2078 goto out;
2079 }
2080 stidp = &dp->dl_stateid;
2081 } else { /* open or lock stateid */
2082 if (!(stp = find_stateid(stateid, flags))) {
2083 dprintk("NFSD: open or lock stateid not found\n");
2084 if (nfs4_in_grace())
2085 status = nfserr_grace;
2086 goto out;
2087 }
2088 if ((flags & CHECK_FH) && nfs4_check_fh(current_fh, stp))
2089 goto out;
2090 if (!stp->st_stateowner->so_confirmed)
2091 goto out;
2092 stidp = &stp->st_stateid;
2093 }
2094 if (stateid->si_generation > stidp->si_generation)
2095 goto out;
2096
2097 /* OLD STATEID */
2098 status = nfserr_old_stateid;
2099 if (stateid->si_generation < stidp->si_generation)
2100 goto out;
2101 if (stp) {
2102 if ((status = nfs4_check_openmode(stp,flags)))
2103 goto out;
2104 renew_client(stp->st_stateowner->so_client);
2105 if (filpp)
2106 *filpp = stp->st_vfs_file;
2107 } else if (dp) {
2108 if ((status = nfs4_check_delegmode(dp, flags)))
2109 goto out;
2110 renew_client(dp->dl_client);
2111 if (flags & DELEG_RET)
2112 unhash_delegation(dp);
2113 if (filpp)
2114 *filpp = dp->dl_vfs_file;
2115 }
2116 status = nfs_ok;
2117 out:
2118 return status;
2119 }
2120
2121 static inline int
2122 setlkflg (int type)
2123 {
2124 return (type == NFS4_READW_LT || type == NFS4_READ_LT) ?
2125 RD_STATE : WR_STATE;
2126 }
2127
2128 /*
2129 * Checks for sequence id mutating operations.
2130 */
2131 static int
2132 nfs4_preprocess_seqid_op(struct svc_fh *current_fh, u32 seqid, stateid_t *stateid, int flags, struct nfs4_stateowner **sopp, struct nfs4_stateid **stpp, struct nfsd4_lock *lock)
2133 {
2134 struct nfs4_stateid *stp;
2135 struct nfs4_stateowner *sop;
2136
2137 dprintk("NFSD: preprocess_seqid_op: seqid=%d "
2138 "stateid = (%08x/%08x/%08x/%08x)\n", seqid,
2139 stateid->si_boot, stateid->si_stateownerid, stateid->si_fileid,
2140 stateid->si_generation);
2141
2142 *stpp = NULL;
2143 *sopp = NULL;
2144
2145 if (ZERO_STATEID(stateid) || ONE_STATEID(stateid)) {
2146 printk("NFSD: preprocess_seqid_op: magic stateid!\n");
2147 return nfserr_bad_stateid;
2148 }
2149
2150 if (STALE_STATEID(stateid))
2151 return nfserr_stale_stateid;
2152 /*
2153 * We return BAD_STATEID if filehandle doesn't match stateid,
2154 * the confirmed flag is incorrecly set, or the generation
2155 * number is incorrect.
2156 */
2157 stp = find_stateid(stateid, flags);
2158 if (stp == NULL) {
2159 /*
2160 * Also, we should make sure this isn't just the result of
2161 * a replayed close:
2162 */
2163 sop = search_close_lru(stateid->si_stateownerid, flags);
2164 if (sop == NULL)
2165 return nfserr_bad_stateid;
2166 *sopp = sop;
2167 goto check_replay;
2168 }
2169
2170 if (lock) {
2171 struct nfs4_stateowner *sop = stp->st_stateowner;
2172 clientid_t *lockclid = &lock->v.new.clientid;
2173 struct nfs4_client *clp = sop->so_client;
2174 int lkflg = 0;
2175 int status;
2176
2177 lkflg = setlkflg(lock->lk_type);
2178
2179 if (lock->lk_is_new) {
2180 if (!sop->so_is_open_owner)
2181 return nfserr_bad_stateid;
2182 if (!cmp_clid(&clp->cl_clientid, lockclid))
2183 return nfserr_bad_stateid;
2184 /* stp is the open stateid */
2185 status = nfs4_check_openmode(stp, lkflg);
2186 if (status)
2187 return status;
2188 } else {
2189 /* stp is the lock stateid */
2190 status = nfs4_check_openmode(stp->st_openstp, lkflg);
2191 if (status)
2192 return status;
2193 }
2194
2195 }
2196
2197 if ((flags & CHECK_FH) && nfs4_check_fh(current_fh, stp)) {
2198 printk("NFSD: preprocess_seqid_op: fh-stateid mismatch!\n");
2199 return nfserr_bad_stateid;
2200 }
2201
2202 *stpp = stp;
2203 *sopp = sop = stp->st_stateowner;
2204
2205 /*
2206 * We now validate the seqid and stateid generation numbers.
2207 * For the moment, we ignore the possibility of
2208 * generation number wraparound.
2209 */
2210 if (seqid != sop->so_seqid)
2211 goto check_replay;
2212
2213 if (sop->so_confirmed && flags & CONFIRM) {
2214 printk("NFSD: preprocess_seqid_op: expected"
2215 " unconfirmed stateowner!\n");
2216 return nfserr_bad_stateid;
2217 }
2218 if (!sop->so_confirmed && !(flags & CONFIRM)) {
2219 printk("NFSD: preprocess_seqid_op: stateowner not"
2220 " confirmed yet!\n");
2221 return nfserr_bad_stateid;
2222 }
2223 if (stateid->si_generation > stp->st_stateid.si_generation) {
2224 printk("NFSD: preprocess_seqid_op: future stateid?!\n");
2225 return nfserr_bad_stateid;
2226 }
2227
2228 if (stateid->si_generation < stp->st_stateid.si_generation) {
2229 printk("NFSD: preprocess_seqid_op: old stateid!\n");
2230 return nfserr_old_stateid;
2231 }
2232 renew_client(sop->so_client);
2233 return nfs_ok;
2234
2235 check_replay:
2236 if (seqid == sop->so_seqid - 1) {
2237 dprintk("NFSD: preprocess_seqid_op: retransmission?\n");
2238 /* indicate replay to calling function */
2239 return NFSERR_REPLAY_ME;
2240 }
2241 printk("NFSD: preprocess_seqid_op: bad seqid (expected %d, got %d)\n",
2242 sop->so_seqid, seqid);
2243 *sopp = NULL;
2244 return nfserr_bad_seqid;
2245 }
2246
2247 int
2248 nfsd4_open_confirm(struct svc_rqst *rqstp, struct svc_fh *current_fh, struct nfsd4_open_confirm *oc, struct nfs4_stateowner **replay_owner)
2249 {
2250 int status;
2251 struct nfs4_stateowner *sop;
2252 struct nfs4_stateid *stp;
2253
2254 dprintk("NFSD: nfsd4_open_confirm on file %.*s\n",
2255 (int)current_fh->fh_dentry->d_name.len,
2256 current_fh->fh_dentry->d_name.name);
2257
2258 if ((status = fh_verify(rqstp, current_fh, S_IFREG, 0)))
2259 goto out;
2260
2261 nfs4_lock_state();
2262
2263 if ((status = nfs4_preprocess_seqid_op(current_fh, oc->oc_seqid,
2264 &oc->oc_req_stateid,
2265 CHECK_FH | CONFIRM | OPEN_STATE,
2266 &oc->oc_stateowner, &stp, NULL)))
2267 goto out;
2268
2269 sop = oc->oc_stateowner;
2270 sop->so_confirmed = 1;
2271 update_stateid(&stp->st_stateid);
2272 memcpy(&oc->oc_resp_stateid, &stp->st_stateid, sizeof(stateid_t));
2273 dprintk("NFSD: nfsd4_open_confirm: success, seqid=%d "
2274 "stateid=(%08x/%08x/%08x/%08x)\n", oc->oc_seqid,
2275 stp->st_stateid.si_boot,
2276 stp->st_stateid.si_stateownerid,
2277 stp->st_stateid.si_fileid,
2278 stp->st_stateid.si_generation);
2279
2280 nfsd4_create_clid_dir(sop->so_client);
2281 out:
2282 if (oc->oc_stateowner) {
2283 nfs4_get_stateowner(oc->oc_stateowner);
2284 *replay_owner = oc->oc_stateowner;
2285 }
2286 nfs4_unlock_state();
2287 return status;
2288 }
2289
2290
2291 /*
2292 * unset all bits in union bitmap (bmap) that
2293 * do not exist in share (from successful OPEN_DOWNGRADE)
2294 */
2295 static void
2296 reset_union_bmap_access(unsigned long access, unsigned long *bmap)
2297 {
2298 int i;
2299 for (i = 1; i < 4; i++) {
2300 if ((i & access) != i)
2301 __clear_bit(i, bmap);
2302 }
2303 }
2304
2305 static void
2306 reset_union_bmap_deny(unsigned long deny, unsigned long *bmap)
2307 {
2308 int i;
2309 for (i = 0; i < 4; i++) {
2310 if ((i & deny) != i)
2311 __clear_bit(i, bmap);
2312 }
2313 }
2314
2315 int
2316 nfsd4_open_downgrade(struct svc_rqst *rqstp, struct svc_fh *current_fh, struct nfsd4_open_downgrade *od, struct nfs4_stateowner **replay_owner)
2317 {
2318 int status;
2319 struct nfs4_stateid *stp;
2320 unsigned int share_access;
2321
2322 dprintk("NFSD: nfsd4_open_downgrade on file %.*s\n",
2323 (int)current_fh->fh_dentry->d_name.len,
2324 current_fh->fh_dentry->d_name.name);
2325
2326 if (!TEST_ACCESS(od->od_share_access) || !TEST_DENY(od->od_share_deny))
2327 return nfserr_inval;
2328
2329 nfs4_lock_state();
2330 if ((status = nfs4_preprocess_seqid_op(current_fh, od->od_seqid,
2331 &od->od_stateid,
2332 CHECK_FH | OPEN_STATE,
2333 &od->od_stateowner, &stp, NULL)))
2334 goto out;
2335
2336 status = nfserr_inval;
2337 if (!test_bit(od->od_share_access, &stp->st_access_bmap)) {
2338 dprintk("NFSD:access not a subset current bitmap: 0x%lx, input access=%08x\n",
2339 stp->st_access_bmap, od->od_share_access);
2340 goto out;
2341 }
2342 if (!test_bit(od->od_share_deny, &stp->st_deny_bmap)) {
2343 dprintk("NFSD:deny not a subset current bitmap: 0x%lx, input deny=%08x\n",
2344 stp->st_deny_bmap, od->od_share_deny);
2345 goto out;
2346 }
2347 set_access(&share_access, stp->st_access_bmap);
2348 nfs4_file_downgrade(stp->st_vfs_file,
2349 share_access & ~od->od_share_access);
2350
2351 reset_union_bmap_access(od->od_share_access, &stp->st_access_bmap);
2352 reset_union_bmap_deny(od->od_share_deny, &stp->st_deny_bmap);
2353
2354 update_stateid(&stp->st_stateid);
2355 memcpy(&od->od_stateid, &stp->st_stateid, sizeof(stateid_t));
2356 status = nfs_ok;
2357 out:
2358 if (od->od_stateowner) {
2359 nfs4_get_stateowner(od->od_stateowner);
2360 *replay_owner = od->od_stateowner;
2361 }
2362 nfs4_unlock_state();
2363 return status;
2364 }
2365
2366 /*
2367 * nfs4_unlock_state() called after encode
2368 */
2369 int
2370 nfsd4_close(struct svc_rqst *rqstp, struct svc_fh *current_fh, struct nfsd4_close *close, struct nfs4_stateowner **replay_owner)
2371 {
2372 int status;
2373 struct nfs4_stateid *stp;
2374
2375 dprintk("NFSD: nfsd4_close on file %.*s\n",
2376 (int)current_fh->fh_dentry->d_name.len,
2377 current_fh->fh_dentry->d_name.name);
2378
2379 nfs4_lock_state();
2380 /* check close_lru for replay */
2381 if ((status = nfs4_preprocess_seqid_op(current_fh, close->cl_seqid,
2382 &close->cl_stateid,
2383 CHECK_FH | OPEN_STATE | CLOSE_STATE,
2384 &close->cl_stateowner, &stp, NULL)))
2385 goto out;
2386 status = nfs_ok;
2387 update_stateid(&stp->st_stateid);
2388 memcpy(&close->cl_stateid, &stp->st_stateid, sizeof(stateid_t));
2389
2390 /* release_stateid() calls nfsd_close() if needed */
2391 release_stateid(stp, OPEN_STATE);
2392
2393 /* place unused nfs4_stateowners on so_close_lru list to be
2394 * released by the laundromat service after the lease period
2395 * to enable us to handle CLOSE replay
2396 */
2397 if (list_empty(&close->cl_stateowner->so_stateids))
2398 move_to_close_lru(close->cl_stateowner);
2399 out:
2400 if (close->cl_stateowner) {
2401 nfs4_get_stateowner(close->cl_stateowner);
2402 *replay_owner = close->cl_stateowner;
2403 }
2404 nfs4_unlock_state();
2405 return status;
2406 }
2407
2408 int
2409 nfsd4_delegreturn(struct svc_rqst *rqstp, struct svc_fh *current_fh, struct nfsd4_delegreturn *dr)
2410 {
2411 int status;
2412
2413 if ((status = fh_verify(rqstp, current_fh, S_IFREG, 0)))
2414 goto out;
2415
2416 nfs4_lock_state();
2417 status = nfs4_preprocess_stateid_op(current_fh, &dr->dr_stateid, DELEG_RET, NULL);
2418 nfs4_unlock_state();
2419 out:
2420 return status;
2421 }
2422
2423
2424 /*
2425 * Lock owner state (byte-range locks)
2426 */
2427 #define LOFF_OVERFLOW(start, len) ((u64)(len) > ~(u64)(start))
2428 #define LOCK_HASH_BITS 8
2429 #define LOCK_HASH_SIZE (1 << LOCK_HASH_BITS)
2430 #define LOCK_HASH_MASK (LOCK_HASH_SIZE - 1)
2431
2432 #define lockownerid_hashval(id) \
2433 ((id) & LOCK_HASH_MASK)
2434
2435 static inline unsigned int
2436 lock_ownerstr_hashval(struct inode *inode, u32 cl_id,
2437 struct xdr_netobj *ownername)
2438 {
2439 return (file_hashval(inode) + cl_id
2440 + opaque_hashval(ownername->data, ownername->len))
2441 & LOCK_HASH_MASK;
2442 }
2443
2444 static struct list_head lock_ownerid_hashtbl[LOCK_HASH_SIZE];
2445 static struct list_head lock_ownerstr_hashtbl[LOCK_HASH_SIZE];
2446 static struct list_head lockstateid_hashtbl[STATEID_HASH_SIZE];
2447
2448 static struct nfs4_stateid *
2449 find_stateid(stateid_t *stid, int flags)
2450 {
2451 struct nfs4_stateid *local = NULL;
2452 u32 st_id = stid->si_stateownerid;
2453 u32 f_id = stid->si_fileid;
2454 unsigned int hashval;
2455
2456 dprintk("NFSD: find_stateid flags 0x%x\n",flags);
2457 if ((flags & LOCK_STATE) || (flags & RD_STATE) || (flags & WR_STATE)) {
2458 hashval = stateid_hashval(st_id, f_id);
2459 list_for_each_entry(local, &lockstateid_hashtbl[hashval], st_hash) {
2460 if ((local->st_stateid.si_stateownerid == st_id) &&
2461 (local->st_stateid.si_fileid == f_id))
2462 return local;
2463 }
2464 }
2465 if ((flags & OPEN_STATE) || (flags & RD_STATE) || (flags & WR_STATE)) {
2466 hashval = stateid_hashval(st_id, f_id);
2467 list_for_each_entry(local, &stateid_hashtbl[hashval], st_hash) {
2468 if ((local->st_stateid.si_stateownerid == st_id) &&
2469 (local->st_stateid.si_fileid == f_id))
2470 return local;
2471 }
2472 }
2473 return NULL;
2474 }
2475
2476 static struct nfs4_delegation *
2477 find_delegation_stateid(struct inode *ino, stateid_t *stid)
2478 {
2479 struct nfs4_file *fp;
2480 struct nfs4_delegation *dl;
2481
2482 dprintk("NFSD:find_delegation_stateid stateid=(%08x/%08x/%08x/%08x)\n",
2483 stid->si_boot, stid->si_stateownerid,
2484 stid->si_fileid, stid->si_generation);
2485
2486 fp = find_file(ino);
2487 if (!fp)
2488 return NULL;
2489 dl = find_delegation_file(fp, stid);
2490 put_nfs4_file(fp);
2491 return dl;
2492 }
2493
2494 /*
2495 * TODO: Linux file offsets are _signed_ 64-bit quantities, which means that
2496 * we can't properly handle lock requests that go beyond the (2^63 - 1)-th
2497 * byte, because of sign extension problems. Since NFSv4 calls for 64-bit
2498 * locking, this prevents us from being completely protocol-compliant. The
2499 * real solution to this problem is to start using unsigned file offsets in
2500 * the VFS, but this is a very deep change!
2501 */
2502 static inline void
2503 nfs4_transform_lock_offset(struct file_lock *lock)
2504 {
2505 if (lock->fl_start < 0)
2506 lock->fl_start = OFFSET_MAX;
2507 if (lock->fl_end < 0)
2508 lock->fl_end = OFFSET_MAX;
2509 }
2510
2511 /* Hack!: For now, we're defining this just so we can use a pointer to it
2512 * as a unique cookie to identify our (NFSv4's) posix locks. */
2513 static struct lock_manager_operations nfsd_posix_mng_ops = {
2514 };
2515
2516 static inline void
2517 nfs4_set_lock_denied(struct file_lock *fl, struct nfsd4_lock_denied *deny)
2518 {
2519 struct nfs4_stateowner *sop;
2520 unsigned int hval;
2521
2522 if (fl->fl_lmops == &nfsd_posix_mng_ops) {
2523 sop = (struct nfs4_stateowner *) fl->fl_owner;
2524 hval = lockownerid_hashval(sop->so_id);
2525 kref_get(&sop->so_ref);
2526 deny->ld_sop = sop;
2527 deny->ld_clientid = sop->so_client->cl_clientid;
2528 } else {
2529 deny->ld_sop = NULL;
2530 deny->ld_clientid.cl_boot = 0;
2531 deny->ld_clientid.cl_id = 0;
2532 }
2533 deny->ld_start = fl->fl_start;
2534 deny->ld_length = ~(u64)0;
2535 if (fl->fl_end != ~(u64)0)
2536 deny->ld_length = fl->fl_end - fl->fl_start + 1;
2537 deny->ld_type = NFS4_READ_LT;
2538 if (fl->fl_type != F_RDLCK)
2539 deny->ld_type = NFS4_WRITE_LT;
2540 }
2541
2542 static struct nfs4_stateowner *
2543 find_lockstateowner_str(struct inode *inode, clientid_t *clid,
2544 struct xdr_netobj *owner)
2545 {
2546 unsigned int hashval = lock_ownerstr_hashval(inode, clid->cl_id, owner);
2547 struct nfs4_stateowner *op;
2548
2549 list_for_each_entry(op, &lock_ownerstr_hashtbl[hashval], so_strhash) {
2550 if (cmp_owner_str(op, owner, clid))
2551 return op;
2552 }
2553 return NULL;
2554 }
2555
2556 /*
2557 * Alloc a lock owner structure.
2558 * Called in nfsd4_lock - therefore, OPEN and OPEN_CONFIRM (if needed) has
2559 * occured.
2560 *
2561 * strhashval = lock_ownerstr_hashval
2562 */
2563
2564 static struct nfs4_stateowner *
2565 alloc_init_lock_stateowner(unsigned int strhashval, struct nfs4_client *clp, struct nfs4_stateid *open_stp, struct nfsd4_lock *lock) {
2566 struct nfs4_stateowner *sop;
2567 struct nfs4_replay *rp;
2568 unsigned int idhashval;
2569
2570 if (!(sop = alloc_stateowner(&lock->lk_new_owner)))
2571 return NULL;
2572 idhashval = lockownerid_hashval(current_ownerid);
2573 INIT_LIST_HEAD(&sop->so_idhash);
2574 INIT_LIST_HEAD(&sop->so_strhash);
2575 INIT_LIST_HEAD(&sop->so_perclient);
2576 INIT_LIST_HEAD(&sop->so_stateids);
2577 INIT_LIST_HEAD(&sop->so_perstateid);
2578 INIT_LIST_HEAD(&sop->so_close_lru); /* not used */
2579 sop->so_time = 0;
2580 list_add(&sop->so_idhash, &lock_ownerid_hashtbl[idhashval]);
2581 list_add(&sop->so_strhash, &lock_ownerstr_hashtbl[strhashval]);
2582 list_add(&sop->so_perstateid, &open_stp->st_lockowners);
2583 sop->so_is_open_owner = 0;
2584 sop->so_id = current_ownerid++;
2585 sop->so_client = clp;
2586 /* It is the openowner seqid that will be incremented in encode in the
2587 * case of new lockowners; so increment the lock seqid manually: */
2588 sop->so_seqid = lock->lk_new_lock_seqid + 1;
2589 sop->so_confirmed = 1;
2590 rp = &sop->so_replay;
2591 rp->rp_status = nfserr_serverfault;
2592 rp->rp_buflen = 0;
2593 rp->rp_buf = rp->rp_ibuf;
2594 return sop;
2595 }
2596
2597 static struct nfs4_stateid *
2598 alloc_init_lock_stateid(struct nfs4_stateowner *sop, struct nfs4_file *fp, struct nfs4_stateid *open_stp)
2599 {
2600 struct nfs4_stateid *stp;
2601 unsigned int hashval = stateid_hashval(sop->so_id, fp->fi_id);
2602
2603 stp = nfs4_alloc_stateid();
2604 if (stp == NULL)
2605 goto out;
2606 INIT_LIST_HEAD(&stp->st_hash);
2607 INIT_LIST_HEAD(&stp->st_perfile);
2608 INIT_LIST_HEAD(&stp->st_perstateowner);
2609 INIT_LIST_HEAD(&stp->st_lockowners); /* not used */
2610 list_add(&stp->st_hash, &lockstateid_hashtbl[hashval]);
2611 list_add(&stp->st_perfile, &fp->fi_stateids);
2612 list_add(&stp->st_perstateowner, &sop->so_stateids);
2613 stp->st_stateowner = sop;
2614 get_nfs4_file(fp);
2615 stp->st_file = fp;
2616 stp->st_stateid.si_boot = boot_time;
2617 stp->st_stateid.si_stateownerid = sop->so_id;
2618 stp->st_stateid.si_fileid = fp->fi_id;
2619 stp->st_stateid.si_generation = 0;
2620 stp->st_vfs_file = open_stp->st_vfs_file; /* FIXME refcount?? */
2621 stp->st_access_bmap = open_stp->st_access_bmap;
2622 stp->st_deny_bmap = open_stp->st_deny_bmap;
2623 stp->st_openstp = open_stp;
2624
2625 out:
2626 return stp;
2627 }
2628
2629 static int
2630 check_lock_length(u64 offset, u64 length)
2631 {
2632 return ((length == 0) || ((length != ~(u64)0) &&
2633 LOFF_OVERFLOW(offset, length)));
2634 }
2635
2636 /*
2637 * LOCK operation
2638 */
2639 int
2640 nfsd4_lock(struct svc_rqst *rqstp, struct svc_fh *current_fh, struct nfsd4_lock *lock, struct nfs4_stateowner **replay_owner)
2641 {
2642 struct nfs4_stateowner *open_sop = NULL;
2643 struct nfs4_stateowner *lock_sop = NULL;
2644 struct nfs4_stateid *lock_stp;
2645 struct file *filp;
2646 struct file_lock file_lock;
2647 struct file_lock conflock;
2648 int status = 0;
2649 unsigned int strhashval;
2650
2651 dprintk("NFSD: nfsd4_lock: start=%Ld length=%Ld\n",
2652 (long long) lock->lk_offset,
2653 (long long) lock->lk_length);
2654
2655 if (check_lock_length(lock->lk_offset, lock->lk_length))
2656 return nfserr_inval;
2657
2658 if ((status = fh_verify(rqstp, current_fh, S_IFREG, MAY_LOCK))) {
2659 dprintk("NFSD: nfsd4_lock: permission denied!\n");
2660 return status;
2661 }
2662
2663 nfs4_lock_state();
2664
2665 if (lock->lk_is_new) {
2666 /*
2667 * Client indicates that this is a new lockowner.
2668 * Use open owner and open stateid to create lock owner and
2669 * lock stateid.
2670 */
2671 struct nfs4_stateid *open_stp = NULL;
2672 struct nfs4_file *fp;
2673
2674 status = nfserr_stale_clientid;
2675 if (STALE_CLIENTID(&lock->lk_new_clientid))
2676 goto out;
2677
2678 /* validate and update open stateid and open seqid */
2679 status = nfs4_preprocess_seqid_op(current_fh,
2680 lock->lk_new_open_seqid,
2681 &lock->lk_new_open_stateid,
2682 CHECK_FH | OPEN_STATE,
2683 &lock->lk_replay_owner, &open_stp,
2684 lock);
2685 if (status)
2686 goto out;
2687 open_sop = lock->lk_replay_owner;
2688 /* create lockowner and lock stateid */
2689 fp = open_stp->st_file;
2690 strhashval = lock_ownerstr_hashval(fp->fi_inode,
2691 open_sop->so_client->cl_clientid.cl_id,
2692 &lock->v.new.owner);
2693 /* XXX: Do we need to check for duplicate stateowners on
2694 * the same file, or should they just be allowed (and
2695 * create new stateids)? */
2696 status = nfserr_resource;
2697 lock_sop = alloc_init_lock_stateowner(strhashval,
2698 open_sop->so_client, open_stp, lock);
2699 if (lock_sop == NULL)
2700 goto out;
2701 lock_stp = alloc_init_lock_stateid(lock_sop, fp, open_stp);
2702 if (lock_stp == NULL)
2703 goto out;
2704 } else {
2705 /* lock (lock owner + lock stateid) already exists */
2706 status = nfs4_preprocess_seqid_op(current_fh,
2707 lock->lk_old_lock_seqid,
2708 &lock->lk_old_lock_stateid,
2709 CHECK_FH | LOCK_STATE,
2710 &lock->lk_replay_owner, &lock_stp, lock);
2711 if (status)
2712 goto out;
2713 lock_sop = lock->lk_replay_owner;
2714 }
2715 /* lock->lk_replay_owner and lock_stp have been created or found */
2716 filp = lock_stp->st_vfs_file;
2717
2718 status = nfserr_grace;
2719 if (nfs4_in_grace() && !lock->lk_reclaim)
2720 goto out;
2721 status = nfserr_no_grace;
2722 if (!nfs4_in_grace() && lock->lk_reclaim)
2723 goto out;
2724
2725 locks_init_lock(&file_lock);
2726 switch (lock->lk_type) {
2727 case NFS4_READ_LT:
2728 case NFS4_READW_LT:
2729 file_lock.fl_type = F_RDLCK;
2730 break;
2731 case NFS4_WRITE_LT:
2732 case NFS4_WRITEW_LT:
2733 file_lock.fl_type = F_WRLCK;
2734 break;
2735 default:
2736 status = nfserr_inval;
2737 goto out;
2738 }
2739 file_lock.fl_owner = (fl_owner_t)lock_sop;
2740 file_lock.fl_pid = current->tgid;
2741 file_lock.fl_file = filp;
2742 file_lock.fl_flags = FL_POSIX;
2743 file_lock.fl_lmops = &nfsd_posix_mng_ops;
2744
2745 file_lock.fl_start = lock->lk_offset;
2746 if ((lock->lk_length == ~(u64)0) ||
2747 LOFF_OVERFLOW(lock->lk_offset, lock->lk_length))
2748 file_lock.fl_end = ~(u64)0;
2749 else
2750 file_lock.fl_end = lock->lk_offset + lock->lk_length - 1;
2751 nfs4_transform_lock_offset(&file_lock);
2752
2753 /*
2754 * Try to lock the file in the VFS.
2755 * Note: locks.c uses the BKL to protect the inode's lock list.
2756 */
2757
2758 /* XXX?: Just to divert the locks_release_private at the start of
2759 * locks_copy_lock: */
2760 conflock.fl_ops = NULL;
2761 conflock.fl_lmops = NULL;
2762 status = posix_lock_file_conf(filp, &file_lock, &conflock);
2763 dprintk("NFSD: nfsd4_lock: posix_lock_file_conf status %d\n",status);
2764 switch (-status) {
2765 case 0: /* success! */
2766 update_stateid(&lock_stp->st_stateid);
2767 memcpy(&lock->lk_resp_stateid, &lock_stp->st_stateid,
2768 sizeof(stateid_t));
2769 break;
2770 case (EAGAIN): /* conflock holds conflicting lock */
2771 status = nfserr_denied;
2772 dprintk("NFSD: nfsd4_lock: conflicting lock found!\n");
2773 nfs4_set_lock_denied(&conflock, &lock->lk_denied);
2774 break;
2775 case (EDEADLK):
2776 status = nfserr_deadlock;
2777 break;
2778 default:
2779 dprintk("NFSD: nfsd4_lock: posix_lock_file_conf() failed! status %d\n",status);
2780 status = nfserr_resource;
2781 break;
2782 }
2783 out:
2784 if (status && lock->lk_is_new && lock_sop)
2785 release_stateowner(lock_sop);
2786 if (lock->lk_replay_owner) {
2787 nfs4_get_stateowner(lock->lk_replay_owner);
2788 *replay_owner = lock->lk_replay_owner;
2789 }
2790 nfs4_unlock_state();
2791 return status;
2792 }
2793
2794 /*
2795 * LOCKT operation
2796 */
2797 int
2798 nfsd4_lockt(struct svc_rqst *rqstp, struct svc_fh *current_fh, struct nfsd4_lockt *lockt)
2799 {
2800 struct inode *inode;
2801 struct file file;
2802 struct file_lock file_lock;
2803 struct file_lock conflock;
2804 int status;
2805
2806 if (nfs4_in_grace())
2807 return nfserr_grace;
2808
2809 if (check_lock_length(lockt->lt_offset, lockt->lt_length))
2810 return nfserr_inval;
2811
2812 lockt->lt_stateowner = NULL;
2813 nfs4_lock_state();
2814
2815 status = nfserr_stale_clientid;
2816 if (STALE_CLIENTID(&lockt->lt_clientid))
2817 goto out;
2818
2819 if ((status = fh_verify(rqstp, current_fh, S_IFREG, 0))) {
2820 dprintk("NFSD: nfsd4_lockt: fh_verify() failed!\n");
2821 if (status == nfserr_symlink)
2822 status = nfserr_inval;
2823 goto out;
2824 }
2825
2826 inode = current_fh->fh_dentry->d_inode;
2827 locks_init_lock(&file_lock);
2828 switch (lockt->lt_type) {
2829 case NFS4_READ_LT:
2830 case NFS4_READW_LT:
2831 file_lock.fl_type = F_RDLCK;
2832 break;
2833 case NFS4_WRITE_LT:
2834 case NFS4_WRITEW_LT:
2835 file_lock.fl_type = F_WRLCK;
2836 break;
2837 default:
2838 printk("NFSD: nfs4_lockt: bad lock type!\n");
2839 status = nfserr_inval;
2840 goto out;
2841 }
2842
2843 lockt->lt_stateowner = find_lockstateowner_str(inode,
2844 &lockt->lt_clientid, &lockt->lt_owner);
2845 if (lockt->lt_stateowner)
2846 file_lock.fl_owner = (fl_owner_t)lockt->lt_stateowner;
2847 file_lock.fl_pid = current->tgid;
2848 file_lock.fl_flags = FL_POSIX;
2849 file_lock.fl_lmops = &nfsd_posix_mng_ops;
2850
2851 file_lock.fl_start = lockt->lt_offset;
2852 if ((lockt->lt_length == ~(u64)0) || LOFF_OVERFLOW(lockt->lt_offset, lockt->lt_length))
2853 file_lock.fl_end = ~(u64)0;
2854 else
2855 file_lock.fl_end = lockt->lt_offset + lockt->lt_length - 1;
2856
2857 nfs4_transform_lock_offset(&file_lock);
2858
2859 /* posix_test_lock uses the struct file _only_ to resolve the inode.
2860 * since LOCKT doesn't require an OPEN, and therefore a struct
2861 * file may not exist, pass posix_test_lock a struct file with
2862 * only the dentry:inode set.
2863 */
2864 memset(&file, 0, sizeof (struct file));
2865 file.f_dentry = current_fh->fh_dentry;
2866
2867 status = nfs_ok;
2868 if (posix_test_lock(&file, &file_lock, &conflock)) {
2869 status = nfserr_denied;
2870 nfs4_set_lock_denied(&conflock, &lockt->lt_denied);
2871 }
2872 out:
2873 nfs4_unlock_state();
2874 return status;
2875 }
2876
2877 int
2878 nfsd4_locku(struct svc_rqst *rqstp, struct svc_fh *current_fh, struct nfsd4_locku *locku, struct nfs4_stateowner **replay_owner)
2879 {
2880 struct nfs4_stateid *stp;
2881 struct file *filp = NULL;
2882 struct file_lock file_lock;
2883 int status;
2884
2885 dprintk("NFSD: nfsd4_locku: start=%Ld length=%Ld\n",
2886 (long long) locku->lu_offset,
2887 (long long) locku->lu_length);
2888
2889 if (check_lock_length(locku->lu_offset, locku->lu_length))
2890 return nfserr_inval;
2891
2892 nfs4_lock_state();
2893
2894 if ((status = nfs4_preprocess_seqid_op(current_fh,
2895 locku->lu_seqid,
2896 &locku->lu_stateid,
2897 CHECK_FH | LOCK_STATE,
2898 &locku->lu_stateowner, &stp, NULL)))
2899 goto out;
2900
2901 filp = stp->st_vfs_file;
2902 BUG_ON(!filp);
2903 locks_init_lock(&file_lock);
2904 file_lock.fl_type = F_UNLCK;
2905 file_lock.fl_owner = (fl_owner_t) locku->lu_stateowner;
2906 file_lock.fl_pid = current->tgid;
2907 file_lock.fl_file = filp;
2908 file_lock.fl_flags = FL_POSIX;
2909 file_lock.fl_lmops = &nfsd_posix_mng_ops;
2910 file_lock.fl_start = locku->lu_offset;
2911
2912 if ((locku->lu_length == ~(u64)0) || LOFF_OVERFLOW(locku->lu_offset, locku->lu_length))
2913 file_lock.fl_end = ~(u64)0;
2914 else
2915 file_lock.fl_end = locku->lu_offset + locku->lu_length - 1;
2916 nfs4_transform_lock_offset(&file_lock);
2917
2918 /*
2919 * Try to unlock the file in the VFS.
2920 */
2921 status = posix_lock_file(filp, &file_lock);
2922 if (status) {
2923 dprintk("NFSD: nfs4_locku: posix_lock_file failed!\n");
2924 goto out_nfserr;
2925 }
2926 /*
2927 * OK, unlock succeeded; the only thing left to do is update the stateid.
2928 */
2929 update_stateid(&stp->st_stateid);
2930 memcpy(&locku->lu_stateid, &stp->st_stateid, sizeof(stateid_t));
2931
2932 out:
2933 if (locku->lu_stateowner) {
2934 nfs4_get_stateowner(locku->lu_stateowner);
2935 *replay_owner = locku->lu_stateowner;
2936 }
2937 nfs4_unlock_state();
2938 return status;
2939
2940 out_nfserr:
2941 status = nfserrno(status);
2942 goto out;
2943 }
2944
2945 /*
2946 * returns
2947 * 1: locks held by lockowner
2948 * 0: no locks held by lockowner
2949 */
2950 static int
2951 check_for_locks(struct file *filp, struct nfs4_stateowner *lowner)
2952 {
2953 struct file_lock **flpp;
2954 struct inode *inode = filp->f_dentry->d_inode;
2955 int status = 0;
2956
2957 lock_kernel();
2958 for (flpp = &inode->i_flock; *flpp != NULL; flpp = &(*flpp)->fl_next) {
2959 if ((*flpp)->fl_owner == (fl_owner_t)lowner) {
2960 status = 1;
2961 goto out;
2962 }
2963 }
2964 out:
2965 unlock_kernel();
2966 return status;
2967 }
2968
2969 int
2970 nfsd4_release_lockowner(struct svc_rqst *rqstp, struct nfsd4_release_lockowner *rlockowner)
2971 {
2972 clientid_t *clid = &rlockowner->rl_clientid;
2973 struct nfs4_stateowner *sop;
2974 struct nfs4_stateid *stp;
2975 struct xdr_netobj *owner = &rlockowner->rl_owner;
2976 struct list_head matches;
2977 int i;
2978 int status;
2979
2980 dprintk("nfsd4_release_lockowner clientid: (%08x/%08x):\n",
2981 clid->cl_boot, clid->cl_id);
2982
2983 /* XXX check for lease expiration */
2984
2985 status = nfserr_stale_clientid;
2986 if (STALE_CLIENTID(clid))
2987 return status;
2988
2989 nfs4_lock_state();
2990
2991 status = nfserr_locks_held;
2992 /* XXX: we're doing a linear search through all the lockowners.
2993 * Yipes! For now we'll just hope clients aren't really using
2994 * release_lockowner much, but eventually we have to fix these
2995 * data structures. */
2996 INIT_LIST_HEAD(&matches);
2997 for (i = 0; i < LOCK_HASH_SIZE; i++) {
2998 list_for_each_entry(sop, &lock_ownerid_hashtbl[i], so_idhash) {
2999 if (!cmp_owner_str(sop, owner, clid))
3000 continue;
3001 list_for_each_entry(stp, &sop->so_stateids,
3002 st_perstateowner) {
3003 if (check_for_locks(stp->st_vfs_file, sop))
3004 goto out;
3005 /* Note: so_perclient unused for lockowners,
3006 * so it's OK to fool with here. */
3007 list_add(&sop->so_perclient, &matches);
3008 }
3009 }
3010 }
3011 /* Clients probably won't expect us to return with some (but not all)
3012 * of the lockowner state released; so don't release any until all
3013 * have been checked. */
3014 status = nfs_ok;
3015 while (!list_empty(&matches)) {
3016 sop = list_entry(matches.next, struct nfs4_stateowner,
3017 so_perclient);
3018 /* unhash_stateowner deletes so_perclient only
3019 * for openowners. */
3020 list_del(&sop->so_perclient);
3021 release_stateowner(sop);
3022 }
3023 out:
3024 nfs4_unlock_state();
3025 return status;
3026 }
3027
3028 static inline struct nfs4_client_reclaim *
3029 alloc_reclaim(void)
3030 {
3031 return kmalloc(sizeof(struct nfs4_client_reclaim), GFP_KERNEL);
3032 }
3033
3034 int
3035 nfs4_has_reclaimed_state(const char *name)
3036 {
3037 unsigned int strhashval = clientstr_hashval(name);
3038 struct nfs4_client *clp;
3039
3040 clp = find_confirmed_client_by_str(name, strhashval);
3041 return clp ? 1 : 0;
3042 }
3043
3044 /*
3045 * failure => all reset bets are off, nfserr_no_grace...
3046 */
3047 int
3048 nfs4_client_to_reclaim(const char *name)
3049 {
3050 unsigned int strhashval;
3051 struct nfs4_client_reclaim *crp = NULL;
3052
3053 dprintk("NFSD nfs4_client_to_reclaim NAME: %.*s\n", HEXDIR_LEN, name);
3054 crp = alloc_reclaim();
3055 if (!crp)
3056 return 0;
3057 strhashval = clientstr_hashval(name);
3058 INIT_LIST_HEAD(&crp->cr_strhash);
3059 list_add(&crp->cr_strhash, &reclaim_str_hashtbl[strhashval]);
3060 memcpy(crp->cr_recdir, name, HEXDIR_LEN);
3061 reclaim_str_hashtbl_size++;
3062 return 1;
3063 }
3064
3065 static void
3066 nfs4_release_reclaim(void)
3067 {
3068 struct nfs4_client_reclaim *crp = NULL;
3069 int i;
3070
3071 for (i = 0; i < CLIENT_HASH_SIZE; i++) {
3072 while (!list_empty(&reclaim_str_hashtbl[i])) {
3073 crp = list_entry(reclaim_str_hashtbl[i].next,
3074 struct nfs4_client_reclaim, cr_strhash);
3075 list_del(&crp->cr_strhash);
3076 kfree(crp);
3077 reclaim_str_hashtbl_size--;
3078 }
3079 }
3080 BUG_ON(reclaim_str_hashtbl_size);
3081 }
3082
3083 /*
3084 * called from OPEN, CLAIM_PREVIOUS with a new clientid. */
3085 static struct nfs4_client_reclaim *
3086 nfs4_find_reclaim_client(clientid_t *clid)
3087 {
3088 unsigned int strhashval;
3089 struct nfs4_client *clp;
3090 struct nfs4_client_reclaim *crp = NULL;
3091
3092
3093 /* find clientid in conf_id_hashtbl */
3094 clp = find_confirmed_client(clid);
3095 if (clp == NULL)
3096 return NULL;
3097
3098 dprintk("NFSD: nfs4_find_reclaim_client for %.*s with recdir %s\n",
3099 clp->cl_name.len, clp->cl_name.data,
3100 clp->cl_recdir);
3101
3102 /* find clp->cl_name in reclaim_str_hashtbl */
3103 strhashval = clientstr_hashval(clp->cl_recdir);
3104 list_for_each_entry(crp, &reclaim_str_hashtbl[strhashval], cr_strhash) {
3105 if (same_name(crp->cr_recdir, clp->cl_recdir)) {
3106 return crp;
3107 }
3108 }
3109 return NULL;
3110 }
3111
3112 /*
3113 * Called from OPEN. Look for clientid in reclaim list.
3114 */
3115 int
3116 nfs4_check_open_reclaim(clientid_t *clid)
3117 {
3118 return nfs4_find_reclaim_client(clid) ? nfs_ok : nfserr_reclaim_bad;
3119 }
3120
3121 /* initialization to perform at module load time: */
3122
3123 void
3124 nfs4_state_init(void)
3125 {
3126 int i;
3127
3128 for (i = 0; i < CLIENT_HASH_SIZE; i++) {
3129 INIT_LIST_HEAD(&conf_id_hashtbl[i]);
3130 INIT_LIST_HEAD(&conf_str_hashtbl[i]);
3131 INIT_LIST_HEAD(&unconf_str_hashtbl[i]);
3132 INIT_LIST_HEAD(&unconf_id_hashtbl[i]);
3133 }
3134 for (i = 0; i < FILE_HASH_SIZE; i++) {
3135 INIT_LIST_HEAD(&file_hashtbl[i]);
3136 }
3137 for (i = 0; i < OWNER_HASH_SIZE; i++) {
3138 INIT_LIST_HEAD(&ownerstr_hashtbl[i]);
3139 INIT_LIST_HEAD(&ownerid_hashtbl[i]);
3140 }
3141 for (i = 0; i < STATEID_HASH_SIZE; i++) {
3142 INIT_LIST_HEAD(&stateid_hashtbl[i]);
3143 INIT_LIST_HEAD(&lockstateid_hashtbl[i]);
3144 }
3145 for (i = 0; i < LOCK_HASH_SIZE; i++) {
3146 INIT_LIST_HEAD(&lock_ownerid_hashtbl[i]);
3147 INIT_LIST_HEAD(&lock_ownerstr_hashtbl[i]);
3148 }
3149 memset(&onestateid, ~0, sizeof(stateid_t));
3150 INIT_LIST_HEAD(&close_lru);
3151 INIT_LIST_HEAD(&client_lru);
3152 INIT_LIST_HEAD(&del_recall_lru);
3153 for (i = 0; i < CLIENT_HASH_SIZE; i++)
3154 INIT_LIST_HEAD(&reclaim_str_hashtbl[i]);
3155 reclaim_str_hashtbl_size = 0;
3156 }
3157
3158 static void
3159 nfsd4_load_reboot_recovery_data(void)
3160 {
3161 int status;
3162
3163 nfs4_lock_state();
3164 nfsd4_init_recdir(user_recovery_dirname);
3165 status = nfsd4_recdir_load();
3166 nfs4_unlock_state();
3167 if (status)
3168 printk("NFSD: Failure reading reboot recovery data\n");
3169 }
3170
3171 /* initialization to perform when the nfsd service is started: */
3172
3173 static void
3174 __nfs4_state_start(void)
3175 {
3176 time_t grace_time;
3177
3178 boot_time = get_seconds();
3179 grace_time = max(user_lease_time, lease_time);
3180 lease_time = user_lease_time;
3181 in_grace = 1;
3182 printk("NFSD: starting %ld-second grace period\n", grace_time);
3183 laundry_wq = create_singlethread_workqueue("nfsd4");
3184 queue_delayed_work(laundry_wq, &laundromat_work, grace_time*HZ);
3185 }
3186
3187 int
3188 nfs4_state_start(void)
3189 {
3190 int status;
3191
3192 if (nfs4_init)
3193 return 0;
3194 status = nfsd4_init_slabs();
3195 if (status)
3196 return status;
3197 nfsd4_load_reboot_recovery_data();
3198 __nfs4_state_start();
3199 nfs4_init = 1;
3200 return 0;
3201 }
3202
3203 int
3204 nfs4_in_grace(void)
3205 {
3206 return in_grace;
3207 }
3208
3209 time_t
3210 nfs4_lease_time(void)
3211 {
3212 return lease_time;
3213 }
3214
3215 static void
3216 __nfs4_state_shutdown(void)
3217 {
3218 int i;
3219 struct nfs4_client *clp = NULL;
3220 struct nfs4_delegation *dp = NULL;
3221 struct nfs4_stateowner *sop = NULL;
3222 struct list_head *pos, *next, reaplist;
3223
3224 list_for_each_safe(pos, next, &close_lru) {
3225 sop = list_entry(pos, struct nfs4_stateowner, so_close_lru);
3226 list_del(&sop->so_close_lru);
3227 nfs4_put_stateowner(sop);
3228 }
3229
3230 for (i = 0; i < CLIENT_HASH_SIZE; i++) {
3231 while (!list_empty(&conf_id_hashtbl[i])) {
3232 clp = list_entry(conf_id_hashtbl[i].next, struct nfs4_client, cl_idhash);
3233 expire_client(clp);
3234 }
3235 while (!list_empty(&unconf_str_hashtbl[i])) {
3236 clp = list_entry(unconf_str_hashtbl[i].next, struct nfs4_client, cl_strhash);
3237 expire_client(clp);
3238 }
3239 }
3240 INIT_LIST_HEAD(&reaplist);
3241 spin_lock(&recall_lock);
3242 list_for_each_safe(pos, next, &del_recall_lru) {
3243 dp = list_entry (pos, struct nfs4_delegation, dl_recall_lru);
3244 list_move(&dp->dl_recall_lru, &reaplist);
3245 }
3246 spin_unlock(&recall_lock);
3247 list_for_each_safe(pos, next, &reaplist) {
3248 dp = list_entry (pos, struct nfs4_delegation, dl_recall_lru);
3249 list_del_init(&dp->dl_recall_lru);
3250 unhash_delegation(dp);
3251 }
3252
3253 cancel_delayed_work(&laundromat_work);
3254 nfsd4_shutdown_recdir();
3255 nfs4_init = 0;
3256 }
3257
3258 void
3259 nfs4_state_shutdown(void)
3260 {
3261 cancel_rearming_delayed_workqueue(laundry_wq, &laundromat_work);
3262 destroy_workqueue(laundry_wq);
3263 nfs4_lock_state();
3264 nfs4_release_reclaim();
3265 __nfs4_state_shutdown();
3266 nfsd4_free_slabs();
3267 nfs4_unlock_state();
3268 }
3269
3270 static void
3271 nfs4_set_recdir(char *recdir)
3272 {
3273 nfs4_lock_state();
3274 strcpy(user_recovery_dirname, recdir);
3275 nfs4_unlock_state();
3276 }
3277
3278 /*
3279 * Change the NFSv4 recovery directory to recdir.
3280 */
3281 int
3282 nfs4_reset_recoverydir(char *recdir)
3283 {
3284 int status;
3285 struct nameidata nd;
3286
3287 status = path_lookup(recdir, LOOKUP_FOLLOW, &nd);
3288 if (status)
3289 return status;
3290 status = -ENOTDIR;
3291 if (S_ISDIR(nd.dentry->d_inode->i_mode)) {
3292 nfs4_set_recdir(recdir);
3293 status = 0;
3294 }
3295 path_release(&nd);
3296 return status;
3297 }
3298
3299 /*
3300 * Called when leasetime is changed.
3301 *
3302 * The only way the protocol gives us to handle on-the-fly lease changes is to
3303 * simulate a reboot. Instead of doing that, we just wait till the next time
3304 * we start to register any changes in lease time. If the administrator
3305 * really wants to change the lease time *now*, they can go ahead and bring
3306 * nfsd down and then back up again after changing the lease time.
3307 */
3308 void
3309 nfs4_reset_lease(time_t leasetime)
3310 {
3311 lock_kernel();
3312 user_lease_time = leasetime;
3313 unlock_kernel();
3314 }