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