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