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