blob: 2dc6da74ccea3421b7463f6e0fec3148820db4e5 [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) {
NeilBrown22de4d82005-06-23 22:04:09 -0700852 status = nfserr_clid_inuse;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700853 if (clp->cl_addr != ip_addr) {
854 printk("NFSD: setclientid: string in use by client"
855 "(clientid %08x/%08x)\n",
856 clp->cl_clientid.cl_boot, clp->cl_clientid.cl_id);
857 goto out;
858 }
859 conf = clp;
860 }
861 clp = find_unconfirmed_client(clid);
862 if (clp) {
NeilBrown22de4d82005-06-23 22:04:09 -0700863 status = nfserr_clid_inuse;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700864 if (clp->cl_addr != ip_addr) {
865 printk("NFSD: setclientid: string in use by client"
866 "(clientid %08x/%08x)\n",
867 clp->cl_clientid.cl_boot, clp->cl_clientid.cl_id);
868 goto out;
869 }
870 unconf = clp;
871 }
872 /* CASE 1:
873 * unconf record that matches input clientid and input confirm.
874 * conf record that matches input clientid.
875 * conf and unconf records match names, verifiers
876 */
877 if ((conf && unconf) &&
878 (cmp_verf(&unconf->cl_confirm, &confirm)) &&
879 (cmp_verf(&conf->cl_verifier, &unconf->cl_verifier)) &&
NeilBrowna55370a2005-06-23 22:03:52 -0700880 (same_name(conf->cl_recdir,unconf->cl_recdir)) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -0700881 (!cmp_verf(&conf->cl_confirm, &unconf->cl_confirm))) {
882 if (!cmp_creds(&conf->cl_cred, &unconf->cl_cred))
883 status = nfserr_clid_inuse;
884 else {
NeilBrown1a69c172005-06-23 22:04:08 -0700885 /* XXX: We just turn off callbacks until we can handle
886 * change request correctly. */
887 clp = conf;
888 clp->cl_callback.cb_parsed = 0;
889 gen_confirm(clp);
890 expire_client(unconf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700891 status = nfs_ok;
NeilBrown1a69c172005-06-23 22:04:08 -0700892
Linus Torvalds1da177e2005-04-16 15:20:36 -0700893 }
894 goto out;
895 }
896 /* CASE 2:
897 * conf record that matches input clientid.
898 * if unconf record that matches input clientid, then unconf->cl_name
899 * or unconf->cl_verifier don't match the conf record.
900 */
901 if ((conf && !unconf) ||
902 ((conf && unconf) &&
903 (!cmp_verf(&conf->cl_verifier, &unconf->cl_verifier) ||
NeilBrowna55370a2005-06-23 22:03:52 -0700904 !same_name(conf->cl_recdir, unconf->cl_recdir)))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700905 if (!cmp_creds(&conf->cl_cred,&rqstp->rq_cred)) {
906 status = nfserr_clid_inuse;
907 } else {
908 clp = conf;
909 status = nfs_ok;
910 }
911 goto out;
912 }
913 /* CASE 3:
914 * conf record not found.
915 * unconf record found.
916 * unconf->cl_confirm matches input confirm
917 */
918 if (!conf && unconf && cmp_verf(&unconf->cl_confirm, &confirm)) {
919 if (!cmp_creds(&unconf->cl_cred, &rqstp->rq_cred)) {
920 status = nfserr_clid_inuse;
921 } else {
NeilBrown1a69c172005-06-23 22:04:08 -0700922 unsigned int hash =
923 clientstr_hashval(unconf->cl_recdir);
924 conf = find_confirmed_client_by_str(unconf->cl_recdir,
925 hash);
926 if (conf) {
927 expire_client(conf);
928 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700929 clp = unconf;
930 move_to_confirmed(unconf);
NeilBrown1a69c172005-06-23 22:04:08 -0700931 status = nfs_ok;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700932 }
933 goto out;
934 }
935 /* CASE 4:
936 * conf record not found, or if conf, then conf->cl_confirm does not
937 * match input confirm.
938 * unconf record not found, or if unconf, then unconf->cl_confirm
939 * does not match input confirm.
940 */
941 if ((!conf || (conf && !cmp_verf(&conf->cl_confirm, &confirm))) &&
942 (!unconf || (unconf && !cmp_verf(&unconf->cl_confirm, &confirm)))) {
943 status = nfserr_stale_clientid;
944 goto out;
945 }
946 /* check that we have hit one of the cases...*/
NeilBrown22de4d82005-06-23 22:04:09 -0700947 status = nfserr_clid_inuse;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700948 goto out;
949out:
950 if (!status)
951 nfsd4_probe_callback(clp);
952 nfs4_unlock_state();
953 return status;
954}
955
956/*
957 * Open owner state (share locks)
958 */
959
960/* hash tables for nfs4_stateowner */
961#define OWNER_HASH_BITS 8
962#define OWNER_HASH_SIZE (1 << OWNER_HASH_BITS)
963#define OWNER_HASH_MASK (OWNER_HASH_SIZE - 1)
964
965#define ownerid_hashval(id) \
966 ((id) & OWNER_HASH_MASK)
967#define ownerstr_hashval(clientid, ownername) \
968 (((clientid) + opaque_hashval((ownername.data), (ownername.len))) & OWNER_HASH_MASK)
969
970static struct list_head ownerid_hashtbl[OWNER_HASH_SIZE];
971static struct list_head ownerstr_hashtbl[OWNER_HASH_SIZE];
972
973/* hash table for nfs4_file */
974#define FILE_HASH_BITS 8
975#define FILE_HASH_SIZE (1 << FILE_HASH_BITS)
976#define FILE_HASH_MASK (FILE_HASH_SIZE - 1)
977/* hash table for (open)nfs4_stateid */
978#define STATEID_HASH_BITS 10
979#define STATEID_HASH_SIZE (1 << STATEID_HASH_BITS)
980#define STATEID_HASH_MASK (STATEID_HASH_SIZE - 1)
981
982#define file_hashval(x) \
983 hash_ptr(x, FILE_HASH_BITS)
984#define stateid_hashval(owner_id, file_id) \
985 (((owner_id) + (file_id)) & STATEID_HASH_MASK)
986
987static struct list_head file_hashtbl[FILE_HASH_SIZE];
988static struct list_head stateid_hashtbl[STATEID_HASH_SIZE];
989
990/* OPEN Share state helper functions */
991static inline struct nfs4_file *
992alloc_init_file(struct inode *ino)
993{
994 struct nfs4_file *fp;
995 unsigned int hashval = file_hashval(ino);
996
NeilBrowne60d4392005-06-23 22:03:01 -0700997 fp = kmem_cache_alloc(file_slab, GFP_KERNEL);
998 if (fp) {
NeilBrown13cd2182005-06-23 22:03:10 -0700999 kref_init(&fp->fi_ref);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001000 INIT_LIST_HEAD(&fp->fi_hash);
NeilBrown8beefa22005-06-23 22:03:08 -07001001 INIT_LIST_HEAD(&fp->fi_stateids);
1002 INIT_LIST_HEAD(&fp->fi_delegations);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001003 list_add(&fp->fi_hash, &file_hashtbl[hashval]);
1004 fp->fi_inode = igrab(ino);
1005 fp->fi_id = current_fileid++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001006 return fp;
1007 }
1008 return NULL;
1009}
1010
1011static void
NeilBrowne60d4392005-06-23 22:03:01 -07001012nfsd4_free_slab(kmem_cache_t **slab)
1013{
1014 int status;
1015
1016 if (*slab == NULL)
1017 return;
1018 status = kmem_cache_destroy(*slab);
1019 *slab = NULL;
1020 WARN_ON(status);
1021}
1022
1023static void
1024nfsd4_free_slabs(void)
1025{
1026 nfsd4_free_slab(&stateowner_slab);
1027 nfsd4_free_slab(&file_slab);
NeilBrown5ac049a2005-06-23 22:03:03 -07001028 nfsd4_free_slab(&stateid_slab);
NeilBrown5b2d21c2005-06-23 22:03:04 -07001029 nfsd4_free_slab(&deleg_slab);
NeilBrowne60d4392005-06-23 22:03:01 -07001030}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001031
1032static int
1033nfsd4_init_slabs(void)
1034{
1035 stateowner_slab = kmem_cache_create("nfsd4_stateowners",
1036 sizeof(struct nfs4_stateowner), 0, 0, NULL, NULL);
NeilBrowne60d4392005-06-23 22:03:01 -07001037 if (stateowner_slab == NULL)
1038 goto out_nomem;
1039 file_slab = kmem_cache_create("nfsd4_files",
1040 sizeof(struct nfs4_file), 0, 0, NULL, NULL);
1041 if (file_slab == NULL)
1042 goto out_nomem;
NeilBrown5ac049a2005-06-23 22:03:03 -07001043 stateid_slab = kmem_cache_create("nfsd4_stateids",
1044 sizeof(struct nfs4_stateid), 0, 0, NULL, NULL);
1045 if (stateid_slab == NULL)
1046 goto out_nomem;
NeilBrown5b2d21c2005-06-23 22:03:04 -07001047 deleg_slab = kmem_cache_create("nfsd4_delegations",
1048 sizeof(struct nfs4_delegation), 0, 0, NULL, NULL);
1049 if (deleg_slab == NULL)
1050 goto out_nomem;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001051 return 0;
NeilBrowne60d4392005-06-23 22:03:01 -07001052out_nomem:
1053 nfsd4_free_slabs();
1054 dprintk("nfsd4: out of memory while initializing nfsv4\n");
1055 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001056}
1057
1058void
1059nfs4_free_stateowner(struct kref *kref)
1060{
1061 struct nfs4_stateowner *sop =
1062 container_of(kref, struct nfs4_stateowner, so_ref);
1063 kfree(sop->so_owner.data);
1064 kmem_cache_free(stateowner_slab, sop);
1065}
1066
1067static inline struct nfs4_stateowner *
1068alloc_stateowner(struct xdr_netobj *owner)
1069{
1070 struct nfs4_stateowner *sop;
1071
1072 if ((sop = kmem_cache_alloc(stateowner_slab, GFP_KERNEL))) {
1073 if ((sop->so_owner.data = kmalloc(owner->len, GFP_KERNEL))) {
1074 memcpy(sop->so_owner.data, owner->data, owner->len);
1075 sop->so_owner.len = owner->len;
1076 kref_init(&sop->so_ref);
1077 return sop;
1078 }
1079 kmem_cache_free(stateowner_slab, sop);
1080 }
1081 return NULL;
1082}
1083
1084static struct nfs4_stateowner *
1085alloc_init_open_stateowner(unsigned int strhashval, struct nfs4_client *clp, struct nfsd4_open *open) {
1086 struct nfs4_stateowner *sop;
1087 struct nfs4_replay *rp;
1088 unsigned int idhashval;
1089
1090 if (!(sop = alloc_stateowner(&open->op_owner)))
1091 return NULL;
1092 idhashval = ownerid_hashval(current_ownerid);
1093 INIT_LIST_HEAD(&sop->so_idhash);
1094 INIT_LIST_HEAD(&sop->so_strhash);
1095 INIT_LIST_HEAD(&sop->so_perclient);
1096 INIT_LIST_HEAD(&sop->so_perfilestate);
1097 INIT_LIST_HEAD(&sop->so_perlockowner); /* not used */
1098 INIT_LIST_HEAD(&sop->so_close_lru);
1099 sop->so_time = 0;
1100 list_add(&sop->so_idhash, &ownerid_hashtbl[idhashval]);
1101 list_add(&sop->so_strhash, &ownerstr_hashtbl[strhashval]);
1102 list_add(&sop->so_perclient, &clp->cl_perclient);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001103 sop->so_is_open_owner = 1;
1104 sop->so_id = current_ownerid++;
1105 sop->so_client = clp;
1106 sop->so_seqid = open->op_seqid;
1107 sop->so_confirmed = 0;
1108 rp = &sop->so_replay;
1109 rp->rp_status = NFSERR_SERVERFAULT;
1110 rp->rp_buflen = 0;
1111 rp->rp_buf = rp->rp_ibuf;
1112 return sop;
1113}
1114
1115static void
1116release_stateid_lockowners(struct nfs4_stateid *open_stp)
1117{
1118 struct nfs4_stateowner *lock_sop;
1119
1120 while (!list_empty(&open_stp->st_perlockowner)) {
1121 lock_sop = list_entry(open_stp->st_perlockowner.next,
1122 struct nfs4_stateowner, so_perlockowner);
1123 /* list_del(&open_stp->st_perlockowner); */
1124 BUG_ON(lock_sop->so_is_open_owner);
1125 release_stateowner(lock_sop);
1126 }
1127}
1128
1129static void
1130unhash_stateowner(struct nfs4_stateowner *sop)
1131{
1132 struct nfs4_stateid *stp;
1133
1134 list_del(&sop->so_idhash);
1135 list_del(&sop->so_strhash);
NeilBrown6fa305d2005-06-23 22:03:06 -07001136 if (sop->so_is_open_owner)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001137 list_del(&sop->so_perclient);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001138 list_del(&sop->so_perlockowner);
1139 while (!list_empty(&sop->so_perfilestate)) {
1140 stp = list_entry(sop->so_perfilestate.next,
1141 struct nfs4_stateid, st_perfilestate);
1142 if (sop->so_is_open_owner)
1143 release_stateid(stp, OPEN_STATE);
1144 else
1145 release_stateid(stp, LOCK_STATE);
1146 }
1147}
1148
1149static void
1150release_stateowner(struct nfs4_stateowner *sop)
1151{
1152 unhash_stateowner(sop);
1153 list_del(&sop->so_close_lru);
1154 nfs4_put_stateowner(sop);
1155}
1156
1157static inline void
1158init_stateid(struct nfs4_stateid *stp, struct nfs4_file *fp, struct nfsd4_open *open) {
1159 struct nfs4_stateowner *sop = open->op_stateowner;
1160 unsigned int hashval = stateid_hashval(sop->so_id, fp->fi_id);
1161
1162 INIT_LIST_HEAD(&stp->st_hash);
1163 INIT_LIST_HEAD(&stp->st_perfilestate);
1164 INIT_LIST_HEAD(&stp->st_perlockowner);
1165 INIT_LIST_HEAD(&stp->st_perfile);
1166 list_add(&stp->st_hash, &stateid_hashtbl[hashval]);
1167 list_add(&stp->st_perfilestate, &sop->so_perfilestate);
NeilBrown8beefa22005-06-23 22:03:08 -07001168 list_add(&stp->st_perfile, &fp->fi_stateids);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001169 stp->st_stateowner = sop;
NeilBrown13cd2182005-06-23 22:03:10 -07001170 get_nfs4_file(fp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001171 stp->st_file = fp;
1172 stp->st_stateid.si_boot = boot_time;
1173 stp->st_stateid.si_stateownerid = sop->so_id;
1174 stp->st_stateid.si_fileid = fp->fi_id;
1175 stp->st_stateid.si_generation = 0;
1176 stp->st_access_bmap = 0;
1177 stp->st_deny_bmap = 0;
1178 __set_bit(open->op_share_access, &stp->st_access_bmap);
1179 __set_bit(open->op_share_deny, &stp->st_deny_bmap);
1180}
1181
1182static void
1183release_stateid(struct nfs4_stateid *stp, int flags)
1184{
1185 struct file *filp = stp->st_vfs_file;
1186
1187 list_del(&stp->st_hash);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001188 list_del(&stp->st_perfile);
1189 list_del(&stp->st_perfilestate);
1190 if (flags & OPEN_STATE) {
1191 release_stateid_lockowners(stp);
1192 stp->st_vfs_file = NULL;
1193 nfsd_close(filp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001194 } else if (flags & LOCK_STATE)
1195 locks_remove_posix(filp, (fl_owner_t) stp->st_stateowner);
NeilBrown13cd2182005-06-23 22:03:10 -07001196 put_nfs4_file(stp->st_file);
NeilBrown5ac049a2005-06-23 22:03:03 -07001197 kmem_cache_free(stateid_slab, stp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001198 stp = NULL;
1199}
1200
NeilBrownfd39ca92005-06-23 22:04:03 -07001201static void
Linus Torvalds1da177e2005-04-16 15:20:36 -07001202move_to_close_lru(struct nfs4_stateowner *sop)
1203{
1204 dprintk("NFSD: move_to_close_lru nfs4_stateowner %p\n", sop);
1205
1206 unhash_stateowner(sop);
1207 list_add_tail(&sop->so_close_lru, &close_lru);
1208 sop->so_time = get_seconds();
1209}
1210
NeilBrownfd39ca92005-06-23 22:04:03 -07001211static void
Linus Torvalds1da177e2005-04-16 15:20:36 -07001212release_state_owner(struct nfs4_stateid *stp, int flag)
1213{
1214 struct nfs4_stateowner *sop = stp->st_stateowner;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001215
1216 dprintk("NFSD: release_state_owner\n");
1217 release_stateid(stp, flag);
1218
1219 /* place unused nfs4_stateowners on so_close_lru list to be
1220 * released by the laundromat service after the lease period
1221 * to enable us to handle CLOSE replay
1222 */
1223 if (sop->so_confirmed && list_empty(&sop->so_perfilestate))
1224 move_to_close_lru(sop);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001225}
1226
1227static int
1228cmp_owner_str(struct nfs4_stateowner *sop, struct xdr_netobj *owner, clientid_t *clid) {
1229 return ((sop->so_owner.len == owner->len) &&
1230 !memcmp(sop->so_owner.data, owner->data, owner->len) &&
1231 (sop->so_client->cl_clientid.cl_id == clid->cl_id));
1232}
1233
1234static struct nfs4_stateowner *
1235find_openstateowner_str(unsigned int hashval, struct nfsd4_open *open)
1236{
1237 struct nfs4_stateowner *so = NULL;
1238
1239 list_for_each_entry(so, &ownerstr_hashtbl[hashval], so_strhash) {
1240 if (cmp_owner_str(so, &open->op_owner, &open->op_clientid))
1241 return so;
1242 }
1243 return NULL;
1244}
1245
1246/* search file_hashtbl[] for file */
1247static struct nfs4_file *
1248find_file(struct inode *ino)
1249{
1250 unsigned int hashval = file_hashval(ino);
1251 struct nfs4_file *fp;
1252
1253 list_for_each_entry(fp, &file_hashtbl[hashval], fi_hash) {
NeilBrown13cd2182005-06-23 22:03:10 -07001254 if (fp->fi_inode == ino) {
1255 get_nfs4_file(fp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001256 return fp;
NeilBrown13cd2182005-06-23 22:03:10 -07001257 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001258 }
1259 return NULL;
1260}
1261
1262#define TEST_ACCESS(x) ((x > 0 || x < 4)?1:0)
1263#define TEST_DENY(x) ((x >= 0 || x < 5)?1:0)
1264
NeilBrownfd39ca92005-06-23 22:04:03 -07001265static void
Linus Torvalds1da177e2005-04-16 15:20:36 -07001266set_access(unsigned int *access, unsigned long bmap) {
1267 int i;
1268
1269 *access = 0;
1270 for (i = 1; i < 4; i++) {
1271 if (test_bit(i, &bmap))
1272 *access |= i;
1273 }
1274}
1275
NeilBrownfd39ca92005-06-23 22:04:03 -07001276static void
Linus Torvalds1da177e2005-04-16 15:20:36 -07001277set_deny(unsigned int *deny, unsigned long bmap) {
1278 int i;
1279
1280 *deny = 0;
1281 for (i = 0; i < 4; i++) {
1282 if (test_bit(i, &bmap))
1283 *deny |= i ;
1284 }
1285}
1286
1287static int
1288test_share(struct nfs4_stateid *stp, struct nfsd4_open *open) {
1289 unsigned int access, deny;
1290
1291 set_access(&access, stp->st_access_bmap);
1292 set_deny(&deny, stp->st_deny_bmap);
1293 if ((access & open->op_share_deny) || (deny & open->op_share_access))
1294 return 0;
1295 return 1;
1296}
1297
1298/*
1299 * Called to check deny when READ with all zero stateid or
1300 * WRITE with all zero or all one stateid
1301 */
NeilBrownfd39ca92005-06-23 22:04:03 -07001302static int
Linus Torvalds1da177e2005-04-16 15:20:36 -07001303nfs4_share_conflict(struct svc_fh *current_fh, unsigned int deny_type)
1304{
1305 struct inode *ino = current_fh->fh_dentry->d_inode;
1306 struct nfs4_file *fp;
1307 struct nfs4_stateid *stp;
NeilBrown13cd2182005-06-23 22:03:10 -07001308 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001309
1310 dprintk("NFSD: nfs4_share_conflict\n");
1311
1312 fp = find_file(ino);
NeilBrown13cd2182005-06-23 22:03:10 -07001313 if (!fp)
1314 return nfs_ok;
1315 ret = nfserr_share_denied;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001316 /* Search for conflicting share reservations */
NeilBrown13cd2182005-06-23 22:03:10 -07001317 list_for_each_entry(stp, &fp->fi_stateids, st_perfile) {
1318 if (test_bit(deny_type, &stp->st_deny_bmap) ||
1319 test_bit(NFS4_SHARE_DENY_BOTH, &stp->st_deny_bmap))
1320 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001321 }
NeilBrown13cd2182005-06-23 22:03:10 -07001322 ret = nfs_ok;
1323out:
1324 put_nfs4_file(fp);
1325 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001326}
1327
1328static inline void
1329nfs4_file_downgrade(struct file *filp, unsigned int share_access)
1330{
1331 if (share_access & NFS4_SHARE_ACCESS_WRITE) {
1332 put_write_access(filp->f_dentry->d_inode);
1333 filp->f_mode = (filp->f_mode | FMODE_READ) & ~FMODE_WRITE;
1334 }
1335}
1336
1337/*
1338 * Recall a delegation
1339 */
1340static int
1341do_recall(void *__dp)
1342{
1343 struct nfs4_delegation *dp = __dp;
1344
1345 daemonize("nfsv4-recall");
1346
1347 nfsd4_cb_recall(dp);
1348 return 0;
1349}
1350
1351/*
1352 * Spawn a thread to perform a recall on the delegation represented
1353 * by the lease (file_lock)
1354 *
1355 * Called from break_lease() with lock_kernel() held.
1356 * Note: we assume break_lease will only call this *once* for any given
1357 * lease.
1358 */
1359static
1360void nfsd_break_deleg_cb(struct file_lock *fl)
1361{
1362 struct nfs4_delegation *dp= (struct nfs4_delegation *)fl->fl_owner;
1363 struct task_struct *t;
1364
1365 dprintk("NFSD nfsd_break_deleg_cb: dp %p fl %p\n",dp,fl);
1366 if (!dp)
1367 return;
1368
1369 /* We're assuming the state code never drops its reference
1370 * without first removing the lease. Since we're in this lease
1371 * callback (and since the lease code is serialized by the kernel
1372 * lock) we know the server hasn't removed the lease yet, we know
1373 * it's safe to take a reference: */
1374 atomic_inc(&dp->dl_count);
1375
1376 spin_lock(&recall_lock);
1377 list_add_tail(&dp->dl_recall_lru, &del_recall_lru);
1378 spin_unlock(&recall_lock);
1379
1380 /* only place dl_time is set. protected by lock_kernel*/
1381 dp->dl_time = get_seconds();
1382
1383 /* XXX need to merge NFSD_LEASE_TIME with fs/locks.c:lease_break_time */
1384 fl->fl_break_time = jiffies + NFSD_LEASE_TIME * HZ;
1385
1386 t = kthread_run(do_recall, dp, "%s", "nfs4_cb_recall");
1387 if (IS_ERR(t)) {
1388 struct nfs4_client *clp = dp->dl_client;
1389
1390 printk(KERN_INFO "NFSD: Callback thread failed for "
1391 "for client (clientid %08x/%08x)\n",
1392 clp->cl_clientid.cl_boot, clp->cl_clientid.cl_id);
1393 nfs4_put_delegation(dp);
1394 }
1395}
1396
1397/*
1398 * The file_lock is being reapd.
1399 *
1400 * Called by locks_free_lock() with lock_kernel() held.
1401 */
1402static
1403void nfsd_release_deleg_cb(struct file_lock *fl)
1404{
1405 struct nfs4_delegation *dp = (struct nfs4_delegation *)fl->fl_owner;
1406
1407 dprintk("NFSD nfsd_release_deleg_cb: fl %p dp %p dl_count %d\n", fl,dp, atomic_read(&dp->dl_count));
1408
1409 if (!(fl->fl_flags & FL_LEASE) || !dp)
1410 return;
1411 dp->dl_flock = NULL;
1412}
1413
1414/*
1415 * Set the delegation file_lock back pointer.
1416 *
1417 * Called from __setlease() with lock_kernel() held.
1418 */
1419static
1420void nfsd_copy_lock_deleg_cb(struct file_lock *new, struct file_lock *fl)
1421{
1422 struct nfs4_delegation *dp = (struct nfs4_delegation *)new->fl_owner;
1423
1424 dprintk("NFSD: nfsd_copy_lock_deleg_cb: new fl %p dp %p\n", new, dp);
1425 if (!dp)
1426 return;
1427 dp->dl_flock = new;
1428}
1429
1430/*
1431 * Called from __setlease() with lock_kernel() held
1432 */
1433static
1434int nfsd_same_client_deleg_cb(struct file_lock *onlist, struct file_lock *try)
1435{
1436 struct nfs4_delegation *onlistd =
1437 (struct nfs4_delegation *)onlist->fl_owner;
1438 struct nfs4_delegation *tryd =
1439 (struct nfs4_delegation *)try->fl_owner;
1440
1441 if (onlist->fl_lmops != try->fl_lmops)
1442 return 0;
1443
1444 return onlistd->dl_client == tryd->dl_client;
1445}
1446
1447
1448static
1449int nfsd_change_deleg_cb(struct file_lock **onlist, int arg)
1450{
1451 if (arg & F_UNLCK)
1452 return lease_modify(onlist, arg);
1453 else
1454 return -EAGAIN;
1455}
1456
NeilBrownfd39ca92005-06-23 22:04:03 -07001457static struct lock_manager_operations nfsd_lease_mng_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001458 .fl_break = nfsd_break_deleg_cb,
1459 .fl_release_private = nfsd_release_deleg_cb,
1460 .fl_copy_lock = nfsd_copy_lock_deleg_cb,
1461 .fl_mylease = nfsd_same_client_deleg_cb,
1462 .fl_change = nfsd_change_deleg_cb,
1463};
1464
1465
1466/*
1467 * nfsd4_process_open1()
1468 * lookup stateowner.
1469 * found:
1470 * check confirmed
1471 * confirmed:
1472 * check seqid
1473 * not confirmed:
1474 * delete owner
1475 * create new owner
1476 * notfound:
1477 * verify clientid
1478 * create new owner
1479 *
1480 * called with nfs4_lock_state() held.
1481 */
1482int
1483nfsd4_process_open1(struct nfsd4_open *open)
1484{
1485 int status;
1486 clientid_t *clientid = &open->op_clientid;
1487 struct nfs4_client *clp = NULL;
1488 unsigned int strhashval;
1489 struct nfs4_stateowner *sop = NULL;
1490
1491 status = nfserr_inval;
1492 if (!check_name(open->op_owner))
1493 goto out;
1494
1495 if (STALE_CLIENTID(&open->op_clientid))
1496 return nfserr_stale_clientid;
1497
1498 strhashval = ownerstr_hashval(clientid->cl_id, open->op_owner);
1499 sop = find_openstateowner_str(strhashval, open);
1500 if (sop) {
1501 open->op_stateowner = sop;
1502 /* check for replay */
1503 if (open->op_seqid == sop->so_seqid){
1504 if (sop->so_replay.rp_buflen)
1505 return NFSERR_REPLAY_ME;
1506 else {
1507 /* The original OPEN failed so spectacularly
1508 * that we don't even have replay data saved!
1509 * Therefore, we have no choice but to continue
1510 * processing this OPEN; presumably, we'll
1511 * fail again for the same reason.
1512 */
1513 dprintk("nfsd4_process_open1:"
1514 " replay with no replay cache\n");
1515 goto renew;
1516 }
1517 } else if (sop->so_confirmed) {
1518 if (open->op_seqid == sop->so_seqid + 1)
1519 goto renew;
1520 status = nfserr_bad_seqid;
1521 goto out;
1522 } else {
1523 /* If we get here, we received an OPEN for an
1524 * unconfirmed nfs4_stateowner. Since the seqid's are
1525 * different, purge the existing nfs4_stateowner, and
1526 * instantiate a new one.
1527 */
1528 clp = sop->so_client;
1529 release_stateowner(sop);
1530 }
1531 } else {
1532 /* nfs4_stateowner not found.
1533 * Verify clientid and instantiate new nfs4_stateowner.
1534 * If verify fails this is presumably the result of the
1535 * client's lease expiring.
1536 */
1537 status = nfserr_expired;
1538 clp = find_confirmed_client(clientid);
1539 if (clp == NULL)
1540 goto out;
1541 }
1542 status = nfserr_resource;
1543 sop = alloc_init_open_stateowner(strhashval, clp, open);
1544 if (sop == NULL)
1545 goto out;
1546 open->op_stateowner = sop;
1547renew:
1548 status = nfs_ok;
1549 renew_client(sop->so_client);
1550out:
1551 if (status && open->op_claim_type == NFS4_OPEN_CLAIM_PREVIOUS)
1552 status = nfserr_reclaim_bad;
1553 return status;
1554}
1555
NeilBrown4a6e43e2005-06-23 22:02:50 -07001556static inline int
1557nfs4_check_delegmode(struct nfs4_delegation *dp, int flags)
1558{
1559 if ((flags & WR_STATE) && (dp->dl_type == NFS4_OPEN_DELEGATE_READ))
1560 return nfserr_openmode;
1561 else
1562 return nfs_ok;
1563}
1564
NeilBrown52f4fb42005-06-23 22:02:49 -07001565static struct nfs4_delegation *
1566find_delegation_file(struct nfs4_file *fp, stateid_t *stid)
1567{
1568 struct nfs4_delegation *dp;
1569
NeilBrown8beefa22005-06-23 22:03:08 -07001570 list_for_each_entry(dp, &fp->fi_delegations, dl_del_perfile) {
NeilBrown52f4fb42005-06-23 22:02:49 -07001571 if (dp->dl_stateid.si_stateownerid == stid->si_stateownerid)
1572 return dp;
1573 }
1574 return NULL;
1575}
1576
NeilBrownc44c5ee2005-06-23 22:02:54 -07001577static int
NeilBrown567d9822005-06-23 22:02:53 -07001578nfs4_check_deleg(struct nfs4_file *fp, struct nfsd4_open *open,
1579 struct nfs4_delegation **dp)
1580{
1581 int flags;
NeilBrownc44c5ee2005-06-23 22:02:54 -07001582 int status = nfserr_bad_stateid;
NeilBrown567d9822005-06-23 22:02:53 -07001583
1584 *dp = find_delegation_file(fp, &open->op_delegate_stateid);
1585 if (*dp == NULL)
NeilBrownc44c5ee2005-06-23 22:02:54 -07001586 goto out;
NeilBrown567d9822005-06-23 22:02:53 -07001587 flags = open->op_share_access == NFS4_SHARE_ACCESS_READ ?
1588 RD_STATE : WR_STATE;
1589 status = nfs4_check_delegmode(*dp, flags);
1590 if (status)
1591 *dp = NULL;
NeilBrownc44c5ee2005-06-23 22:02:54 -07001592out:
1593 if (open->op_claim_type != NFS4_OPEN_CLAIM_DELEGATE_CUR)
1594 return nfs_ok;
1595 if (status)
1596 return status;
1597 open->op_stateowner->so_confirmed = 1;
1598 return nfs_ok;
NeilBrown567d9822005-06-23 22:02:53 -07001599}
1600
Linus Torvalds1da177e2005-04-16 15:20:36 -07001601static int
1602nfs4_check_open(struct nfs4_file *fp, struct nfsd4_open *open, struct nfs4_stateid **stpp)
1603{
1604 struct nfs4_stateid *local;
1605 int status = nfserr_share_denied;
1606 struct nfs4_stateowner *sop = open->op_stateowner;
1607
NeilBrown8beefa22005-06-23 22:03:08 -07001608 list_for_each_entry(local, &fp->fi_stateids, st_perfile) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001609 /* ignore lock owners */
1610 if (local->st_stateowner->so_is_open_owner == 0)
1611 continue;
1612 /* remember if we have seen this open owner */
1613 if (local->st_stateowner == sop)
1614 *stpp = local;
1615 /* check for conflicting share reservations */
1616 if (!test_share(local, open))
1617 goto out;
1618 }
1619 status = 0;
1620out:
1621 return status;
1622}
1623
NeilBrown5ac049a2005-06-23 22:03:03 -07001624static inline struct nfs4_stateid *
1625nfs4_alloc_stateid(void)
1626{
1627 return kmem_cache_alloc(stateid_slab, GFP_KERNEL);
1628}
1629
Linus Torvalds1da177e2005-04-16 15:20:36 -07001630static int
1631nfs4_new_open(struct svc_rqst *rqstp, struct nfs4_stateid **stpp,
NeilBrown567d9822005-06-23 22:02:53 -07001632 struct nfs4_delegation *dp,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001633 struct svc_fh *cur_fh, int flags)
1634{
1635 struct nfs4_stateid *stp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001636
NeilBrown5ac049a2005-06-23 22:03:03 -07001637 stp = nfs4_alloc_stateid();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001638 if (stp == NULL)
1639 return nfserr_resource;
1640
NeilBrown567d9822005-06-23 22:02:53 -07001641 if (dp) {
1642 get_file(dp->dl_vfs_file);
1643 stp->st_vfs_file = dp->dl_vfs_file;
1644 } else {
1645 int status;
1646 status = nfsd_open(rqstp, cur_fh, S_IFREG, flags,
1647 &stp->st_vfs_file);
1648 if (status) {
1649 if (status == nfserr_dropit)
1650 status = nfserr_jukebox;
NeilBrown5ac049a2005-06-23 22:03:03 -07001651 kmem_cache_free(stateid_slab, stp);
NeilBrown567d9822005-06-23 22:02:53 -07001652 return status;
1653 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001654 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001655 *stpp = stp;
1656 return 0;
1657}
1658
1659static inline int
1660nfsd4_truncate(struct svc_rqst *rqstp, struct svc_fh *fh,
1661 struct nfsd4_open *open)
1662{
1663 struct iattr iattr = {
1664 .ia_valid = ATTR_SIZE,
1665 .ia_size = 0,
1666 };
1667 if (!open->op_truncate)
1668 return 0;
1669 if (!(open->op_share_access & NFS4_SHARE_ACCESS_WRITE))
1670 return -EINVAL;
1671 return nfsd_setattr(rqstp, fh, &iattr, 0, (time_t)0);
1672}
1673
1674static int
1675nfs4_upgrade_open(struct svc_rqst *rqstp, struct svc_fh *cur_fh, struct nfs4_stateid *stp, struct nfsd4_open *open)
1676{
1677 struct file *filp = stp->st_vfs_file;
1678 struct inode *inode = filp->f_dentry->d_inode;
1679 unsigned int share_access;
1680 int status;
1681
1682 set_access(&share_access, stp->st_access_bmap);
1683 share_access = ~share_access;
1684 share_access &= open->op_share_access;
1685
1686 if (!(share_access & NFS4_SHARE_ACCESS_WRITE))
1687 return nfsd4_truncate(rqstp, cur_fh, open);
1688
1689 status = get_write_access(inode);
1690 if (status)
1691 return nfserrno(status);
1692 status = nfsd4_truncate(rqstp, cur_fh, open);
1693 if (status) {
1694 put_write_access(inode);
1695 return status;
1696 }
1697 /* remember the open */
1698 filp->f_mode = (filp->f_mode | FMODE_WRITE) & ~FMODE_READ;
1699 set_bit(open->op_share_access, &stp->st_access_bmap);
1700 set_bit(open->op_share_deny, &stp->st_deny_bmap);
1701
1702 return nfs_ok;
1703}
1704
1705
1706/* decrement seqid on successful reclaim, it will be bumped in encode_open */
1707static void
1708nfs4_set_claim_prev(struct nfsd4_open *open, int *status)
1709{
1710 if (open->op_claim_type == NFS4_OPEN_CLAIM_PREVIOUS) {
1711 if (*status)
1712 *status = nfserr_reclaim_bad;
1713 else {
1714 open->op_stateowner->so_confirmed = 1;
1715 open->op_stateowner->so_seqid--;
1716 }
1717 }
1718}
1719
1720/*
1721 * Attempt to hand out a delegation.
1722 */
1723static void
1724nfs4_open_delegation(struct svc_fh *fh, struct nfsd4_open *open, struct nfs4_stateid *stp)
1725{
1726 struct nfs4_delegation *dp;
1727 struct nfs4_stateowner *sop = stp->st_stateowner;
1728 struct nfs4_callback *cb = &sop->so_client->cl_callback;
1729 struct file_lock fl, *flp = &fl;
1730 int status, flag = 0;
1731
1732 flag = NFS4_OPEN_DELEGATE_NONE;
NeilBrown7b190fe2005-06-23 22:03:23 -07001733 open->op_recall = 0;
1734 switch (open->op_claim_type) {
1735 case NFS4_OPEN_CLAIM_PREVIOUS:
1736 if (!atomic_read(&cb->cb_set))
1737 open->op_recall = 1;
1738 flag = open->op_delegate_type;
1739 if (flag == NFS4_OPEN_DELEGATE_NONE)
1740 goto out;
1741 break;
1742 case NFS4_OPEN_CLAIM_NULL:
1743 /* Let's not give out any delegations till everyone's
1744 * had the chance to reclaim theirs.... */
1745 if (nfs4_in_grace())
1746 goto out;
1747 if (!atomic_read(&cb->cb_set) || !sop->so_confirmed)
1748 goto out;
1749 if (open->op_share_access & NFS4_SHARE_ACCESS_WRITE)
1750 flag = NFS4_OPEN_DELEGATE_WRITE;
1751 else
1752 flag = NFS4_OPEN_DELEGATE_READ;
1753 break;
1754 default:
1755 goto out;
1756 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001757
1758 dp = alloc_init_deleg(sop->so_client, stp, fh, flag);
1759 if (dp == NULL) {
1760 flag = NFS4_OPEN_DELEGATE_NONE;
1761 goto out;
1762 }
1763 locks_init_lock(&fl);
1764 fl.fl_lmops = &nfsd_lease_mng_ops;
1765 fl.fl_flags = FL_LEASE;
1766 fl.fl_end = OFFSET_MAX;
1767 fl.fl_owner = (fl_owner_t)dp;
1768 fl.fl_file = stp->st_vfs_file;
1769 fl.fl_pid = current->tgid;
1770
1771 /* setlease checks to see if delegation should be handed out.
1772 * the lock_manager callbacks fl_mylease and fl_change are used
1773 */
1774 if ((status = setlease(stp->st_vfs_file,
1775 flag == NFS4_OPEN_DELEGATE_READ? F_RDLCK: F_WRLCK, &flp))) {
1776 dprintk("NFSD: setlease failed [%d], no delegation\n", status);
NeilBrownc9071322005-04-16 15:26:38 -07001777 unhash_delegation(dp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001778 flag = NFS4_OPEN_DELEGATE_NONE;
1779 goto out;
1780 }
1781
1782 memcpy(&open->op_delegate_stateid, &dp->dl_stateid, sizeof(dp->dl_stateid));
1783
1784 dprintk("NFSD: delegation stateid=(%08x/%08x/%08x/%08x)\n\n",
1785 dp->dl_stateid.si_boot,
1786 dp->dl_stateid.si_stateownerid,
1787 dp->dl_stateid.si_fileid,
1788 dp->dl_stateid.si_generation);
1789out:
NeilBrown7b190fe2005-06-23 22:03:23 -07001790 if (open->op_claim_type == NFS4_OPEN_CLAIM_PREVIOUS
1791 && flag == NFS4_OPEN_DELEGATE_NONE
1792 && open->op_delegate_type != NFS4_OPEN_DELEGATE_NONE)
1793 printk("NFSD: WARNING: refusing delegation reclaim\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001794 open->op_delegate_type = flag;
1795}
1796
1797/*
1798 * called with nfs4_lock_state() held.
1799 */
1800int
1801nfsd4_process_open2(struct svc_rqst *rqstp, struct svc_fh *current_fh, struct nfsd4_open *open)
1802{
1803 struct nfs4_file *fp = NULL;
1804 struct inode *ino = current_fh->fh_dentry->d_inode;
1805 struct nfs4_stateid *stp = NULL;
NeilBrown567d9822005-06-23 22:02:53 -07001806 struct nfs4_delegation *dp = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001807 int status;
1808
1809 status = nfserr_inval;
1810 if (!TEST_ACCESS(open->op_share_access) || !TEST_DENY(open->op_share_deny))
1811 goto out;
1812 /*
1813 * Lookup file; if found, lookup stateid and check open request,
1814 * and check for delegations in the process of being recalled.
1815 * If not found, create the nfs4_file struct
1816 */
1817 fp = find_file(ino);
1818 if (fp) {
1819 if ((status = nfs4_check_open(fp, open, &stp)))
1820 goto out;
NeilBrownc44c5ee2005-06-23 22:02:54 -07001821 status = nfs4_check_deleg(fp, open, &dp);
1822 if (status)
1823 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001824 } else {
NeilBrownc44c5ee2005-06-23 22:02:54 -07001825 status = nfserr_bad_stateid;
1826 if (open->op_claim_type == NFS4_OPEN_CLAIM_DELEGATE_CUR)
1827 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001828 status = nfserr_resource;
1829 fp = alloc_init_file(ino);
1830 if (fp == NULL)
1831 goto out;
1832 }
1833
1834 /*
1835 * OPEN the file, or upgrade an existing OPEN.
1836 * If truncate fails, the OPEN fails.
1837 */
1838 if (stp) {
1839 /* Stateid was found, this is an OPEN upgrade */
1840 status = nfs4_upgrade_open(rqstp, current_fh, stp, open);
1841 if (status)
1842 goto out;
1843 } else {
1844 /* Stateid was not found, this is a new OPEN */
1845 int flags = 0;
1846 if (open->op_share_access & NFS4_SHARE_ACCESS_WRITE)
1847 flags = MAY_WRITE;
1848 else
1849 flags = MAY_READ;
NeilBrown567d9822005-06-23 22:02:53 -07001850 status = nfs4_new_open(rqstp, &stp, dp, current_fh, flags);
1851 if (status)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001852 goto out;
1853 init_stateid(stp, fp, open);
1854 status = nfsd4_truncate(rqstp, current_fh, open);
1855 if (status) {
1856 release_stateid(stp, OPEN_STATE);
1857 goto out;
1858 }
1859 }
1860 memcpy(&open->op_stateid, &stp->st_stateid, sizeof(stateid_t));
1861
1862 /*
1863 * Attempt to hand out a delegation. No error return, because the
1864 * OPEN succeeds even if we fail.
1865 */
1866 nfs4_open_delegation(current_fh, open, stp);
1867
1868 status = nfs_ok;
1869
1870 dprintk("nfs4_process_open2: stateid=(%08x/%08x/%08x/%08x)\n",
1871 stp->st_stateid.si_boot, stp->st_stateid.si_stateownerid,
1872 stp->st_stateid.si_fileid, stp->st_stateid.si_generation);
1873out:
NeilBrown13cd2182005-06-23 22:03:10 -07001874 if (fp)
1875 put_nfs4_file(fp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001876 /* CLAIM_PREVIOUS has different error returns */
1877 nfs4_set_claim_prev(open, &status);
1878 /*
1879 * To finish the open response, we just need to set the rflags.
1880 */
1881 open->op_rflags = NFS4_OPEN_RESULT_LOCKTYPE_POSIX;
1882 if (!open->op_stateowner->so_confirmed)
1883 open->op_rflags |= NFS4_OPEN_RESULT_CONFIRM;
1884
1885 return status;
1886}
1887
NeilBrown58da2822005-06-23 22:03:19 -07001888static struct workqueue_struct *laundry_wq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001889static struct work_struct laundromat_work;
1890static void laundromat_main(void *);
1891static DECLARE_WORK(laundromat_work, laundromat_main, NULL);
1892
1893int
1894nfsd4_renew(clientid_t *clid)
1895{
1896 struct nfs4_client *clp;
1897 int status;
1898
1899 nfs4_lock_state();
1900 dprintk("process_renew(%08x/%08x): starting\n",
1901 clid->cl_boot, clid->cl_id);
1902 status = nfserr_stale_clientid;
1903 if (STALE_CLIENTID(clid))
1904 goto out;
1905 clp = find_confirmed_client(clid);
1906 status = nfserr_expired;
1907 if (clp == NULL) {
1908 /* We assume the client took too long to RENEW. */
1909 dprintk("nfsd4_renew: clientid not found!\n");
1910 goto out;
1911 }
1912 renew_client(clp);
1913 status = nfserr_cb_path_down;
1914 if (!list_empty(&clp->cl_del_perclnt)
1915 && !atomic_read(&clp->cl_callback.cb_set))
1916 goto out;
1917 status = nfs_ok;
1918out:
1919 nfs4_unlock_state();
1920 return status;
1921}
1922
NeilBrowna76b4312005-06-23 22:04:01 -07001923static void
1924end_grace(void)
1925{
1926 dprintk("NFSD: end of grace period\n");
1927 in_grace = 0;
1928}
1929
NeilBrownfd39ca92005-06-23 22:04:03 -07001930static time_t
Linus Torvalds1da177e2005-04-16 15:20:36 -07001931nfs4_laundromat(void)
1932{
1933 struct nfs4_client *clp;
1934 struct nfs4_stateowner *sop;
1935 struct nfs4_delegation *dp;
1936 struct list_head *pos, *next, reaplist;
1937 time_t cutoff = get_seconds() - NFSD_LEASE_TIME;
1938 time_t t, clientid_val = NFSD_LEASE_TIME;
1939 time_t u, test_val = NFSD_LEASE_TIME;
1940
1941 nfs4_lock_state();
1942
1943 dprintk("NFSD: laundromat service - starting\n");
NeilBrowna76b4312005-06-23 22:04:01 -07001944 if (in_grace)
1945 end_grace();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001946 list_for_each_safe(pos, next, &client_lru) {
1947 clp = list_entry(pos, struct nfs4_client, cl_lru);
1948 if (time_after((unsigned long)clp->cl_time, (unsigned long)cutoff)) {
1949 t = clp->cl_time - cutoff;
1950 if (clientid_val > t)
1951 clientid_val = t;
1952 break;
1953 }
1954 dprintk("NFSD: purging unused client (clientid %08x)\n",
1955 clp->cl_clientid.cl_id);
1956 expire_client(clp);
1957 }
1958 INIT_LIST_HEAD(&reaplist);
1959 spin_lock(&recall_lock);
1960 list_for_each_safe(pos, next, &del_recall_lru) {
1961 dp = list_entry (pos, struct nfs4_delegation, dl_recall_lru);
1962 if (time_after((unsigned long)dp->dl_time, (unsigned long)cutoff)) {
1963 u = dp->dl_time - cutoff;
1964 if (test_val > u)
1965 test_val = u;
1966 break;
1967 }
1968 dprintk("NFSD: purging unused delegation dp %p, fp %p\n",
1969 dp, dp->dl_flock);
1970 list_move(&dp->dl_recall_lru, &reaplist);
1971 }
1972 spin_unlock(&recall_lock);
1973 list_for_each_safe(pos, next, &reaplist) {
1974 dp = list_entry (pos, struct nfs4_delegation, dl_recall_lru);
1975 list_del_init(&dp->dl_recall_lru);
1976 unhash_delegation(dp);
1977 }
1978 test_val = NFSD_LEASE_TIME;
1979 list_for_each_safe(pos, next, &close_lru) {
1980 sop = list_entry(pos, struct nfs4_stateowner, so_close_lru);
1981 if (time_after((unsigned long)sop->so_time, (unsigned long)cutoff)) {
1982 u = sop->so_time - cutoff;
1983 if (test_val > u)
1984 test_val = u;
1985 break;
1986 }
1987 dprintk("NFSD: purging unused open stateowner (so_id %d)\n",
1988 sop->so_id);
1989 list_del(&sop->so_close_lru);
1990 nfs4_put_stateowner(sop);
1991 }
1992 if (clientid_val < NFSD_LAUNDROMAT_MINTIMEOUT)
1993 clientid_val = NFSD_LAUNDROMAT_MINTIMEOUT;
1994 nfs4_unlock_state();
1995 return clientid_val;
1996}
1997
1998void
1999laundromat_main(void *not_used)
2000{
2001 time_t t;
2002
2003 t = nfs4_laundromat();
2004 dprintk("NFSD: laundromat_main - sleeping for %ld seconds\n", t);
NeilBrown58da2822005-06-23 22:03:19 -07002005 queue_delayed_work(laundry_wq, &laundromat_work, t*HZ);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002006}
2007
2008/* search ownerid_hashtbl[] and close_lru for stateid owner
2009 * (stateid->si_stateownerid)
2010 */
NeilBrownfd39ca92005-06-23 22:04:03 -07002011static struct nfs4_stateowner *
Linus Torvalds1da177e2005-04-16 15:20:36 -07002012find_openstateowner_id(u32 st_id, int flags) {
2013 struct nfs4_stateowner *local = NULL;
2014
2015 dprintk("NFSD: find_openstateowner_id %d\n", st_id);
2016 if (flags & CLOSE_STATE) {
2017 list_for_each_entry(local, &close_lru, so_close_lru) {
2018 if (local->so_id == st_id)
2019 return local;
2020 }
2021 }
2022 return NULL;
2023}
2024
2025static inline int
2026nfs4_check_fh(struct svc_fh *fhp, struct nfs4_stateid *stp)
2027{
2028 return fhp->fh_dentry->d_inode != stp->st_vfs_file->f_dentry->d_inode;
2029}
2030
2031static int
2032STALE_STATEID(stateid_t *stateid)
2033{
2034 if (stateid->si_boot == boot_time)
2035 return 0;
2036 printk("NFSD: stale stateid (%08x/%08x/%08x/%08x)!\n",
2037 stateid->si_boot, stateid->si_stateownerid, stateid->si_fileid,
2038 stateid->si_generation);
2039 return 1;
2040}
2041
2042static inline int
2043access_permit_read(unsigned long access_bmap)
2044{
2045 return test_bit(NFS4_SHARE_ACCESS_READ, &access_bmap) ||
2046 test_bit(NFS4_SHARE_ACCESS_BOTH, &access_bmap) ||
2047 test_bit(NFS4_SHARE_ACCESS_WRITE, &access_bmap);
2048}
2049
2050static inline int
2051access_permit_write(unsigned long access_bmap)
2052{
2053 return test_bit(NFS4_SHARE_ACCESS_WRITE, &access_bmap) ||
2054 test_bit(NFS4_SHARE_ACCESS_BOTH, &access_bmap);
2055}
2056
2057static
2058int nfs4_check_openmode(struct nfs4_stateid *stp, int flags)
2059{
2060 int status = nfserr_openmode;
2061
2062 if ((flags & WR_STATE) && (!access_permit_write(stp->st_access_bmap)))
2063 goto out;
2064 if ((flags & RD_STATE) && (!access_permit_read(stp->st_access_bmap)))
2065 goto out;
2066 status = nfs_ok;
2067out:
2068 return status;
2069}
2070
2071static inline int
Linus Torvalds1da177e2005-04-16 15:20:36 -07002072check_special_stateids(svc_fh *current_fh, stateid_t *stateid, int flags)
2073{
2074 /* Trying to call delegreturn with a special stateid? Yuch: */
2075 if (!(flags & (RD_STATE | WR_STATE)))
2076 return nfserr_bad_stateid;
2077 else if (ONE_STATEID(stateid) && (flags & RD_STATE))
2078 return nfs_ok;
2079 else if (nfs4_in_grace()) {
2080 /* Answer in remaining cases depends on existance of
2081 * conflicting state; so we must wait out the grace period. */
2082 return nfserr_grace;
2083 } else if (flags & WR_STATE)
2084 return nfs4_share_conflict(current_fh,
2085 NFS4_SHARE_DENY_WRITE);
2086 else /* (flags & RD_STATE) && ZERO_STATEID(stateid) */
2087 return nfs4_share_conflict(current_fh,
2088 NFS4_SHARE_DENY_READ);
2089}
2090
2091/*
2092 * Allow READ/WRITE during grace period on recovered state only for files
2093 * that are not able to provide mandatory locking.
2094 */
2095static inline int
2096io_during_grace_disallowed(struct inode *inode, int flags)
2097{
2098 return nfs4_in_grace() && (flags & (RD_STATE | WR_STATE))
2099 && MANDATORY_LOCK(inode);
2100}
2101
2102/*
2103* Checks for stateid operations
2104*/
2105int
2106nfs4_preprocess_stateid_op(struct svc_fh *current_fh, stateid_t *stateid, int flags, struct file **filpp)
2107{
2108 struct nfs4_stateid *stp = NULL;
2109 struct nfs4_delegation *dp = NULL;
2110 stateid_t *stidp;
2111 struct inode *ino = current_fh->fh_dentry->d_inode;
2112 int status;
2113
2114 dprintk("NFSD: preprocess_stateid_op: stateid = (%08x/%08x/%08x/%08x)\n",
2115 stateid->si_boot, stateid->si_stateownerid,
2116 stateid->si_fileid, stateid->si_generation);
2117 if (filpp)
2118 *filpp = NULL;
2119
2120 if (io_during_grace_disallowed(ino, flags))
2121 return nfserr_grace;
2122
2123 if (ZERO_STATEID(stateid) || ONE_STATEID(stateid))
2124 return check_special_stateids(current_fh, stateid, flags);
2125
2126 /* STALE STATEID */
2127 status = nfserr_stale_stateid;
2128 if (STALE_STATEID(stateid))
2129 goto out;
2130
2131 /* BAD STATEID */
2132 status = nfserr_bad_stateid;
2133 if (!stateid->si_fileid) { /* delegation stateid */
2134 if(!(dp = find_delegation_stateid(ino, stateid))) {
2135 dprintk("NFSD: delegation stateid not found\n");
2136 if (nfs4_in_grace())
2137 status = nfserr_grace;
2138 goto out;
2139 }
2140 stidp = &dp->dl_stateid;
2141 } else { /* open or lock stateid */
2142 if (!(stp = find_stateid(stateid, flags))) {
2143 dprintk("NFSD: open or lock stateid not found\n");
2144 if (nfs4_in_grace())
2145 status = nfserr_grace;
2146 goto out;
2147 }
2148 if ((flags & CHECK_FH) && nfs4_check_fh(current_fh, stp))
2149 goto out;
2150 if (!stp->st_stateowner->so_confirmed)
2151 goto out;
2152 stidp = &stp->st_stateid;
2153 }
2154 if (stateid->si_generation > stidp->si_generation)
2155 goto out;
2156
2157 /* OLD STATEID */
2158 status = nfserr_old_stateid;
2159 if (stateid->si_generation < stidp->si_generation)
2160 goto out;
2161 if (stp) {
2162 if ((status = nfs4_check_openmode(stp,flags)))
2163 goto out;
2164 renew_client(stp->st_stateowner->so_client);
2165 if (filpp)
2166 *filpp = stp->st_vfs_file;
2167 } else if (dp) {
2168 if ((status = nfs4_check_delegmode(dp, flags)))
2169 goto out;
2170 renew_client(dp->dl_client);
2171 if (flags & DELEG_RET)
2172 unhash_delegation(dp);
2173 if (filpp)
2174 *filpp = dp->dl_vfs_file;
2175 }
2176 status = nfs_ok;
2177out:
2178 return status;
2179}
2180
2181
2182/*
2183 * Checks for sequence id mutating operations.
2184 */
NeilBrownfd39ca92005-06-23 22:04:03 -07002185static int
Linus Torvalds1da177e2005-04-16 15:20:36 -07002186nfs4_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)
2187{
2188 int status;
2189 struct nfs4_stateid *stp;
2190 struct nfs4_stateowner *sop;
2191
2192 dprintk("NFSD: preprocess_seqid_op: seqid=%d "
2193 "stateid = (%08x/%08x/%08x/%08x)\n", seqid,
2194 stateid->si_boot, stateid->si_stateownerid, stateid->si_fileid,
2195 stateid->si_generation);
2196
2197 *stpp = NULL;
2198 *sopp = NULL;
2199
2200 status = nfserr_bad_stateid;
2201 if (ZERO_STATEID(stateid) || ONE_STATEID(stateid)) {
2202 printk("NFSD: preprocess_seqid_op: magic stateid!\n");
2203 goto out;
2204 }
2205
2206 status = nfserr_stale_stateid;
2207 if (STALE_STATEID(stateid))
2208 goto out;
2209 /*
2210 * We return BAD_STATEID if filehandle doesn't match stateid,
2211 * the confirmed flag is incorrecly set, or the generation
2212 * number is incorrect.
2213 * If there is no entry in the openfile table for this id,
2214 * we can't always return BAD_STATEID;
2215 * this might be a retransmitted CLOSE which has arrived after
2216 * the openfile has been released.
2217 */
2218 if (!(stp = find_stateid(stateid, flags)))
2219 goto no_nfs4_stateid;
2220
2221 status = nfserr_bad_stateid;
2222
2223 /* for new lock stateowners:
2224 * check that the lock->v.new.open_stateid
2225 * refers to an open stateowner
2226 *
2227 * check that the lockclid (nfs4_lock->v.new.clientid) is the same
2228 * as the open_stateid->st_stateowner->so_client->clientid
2229 */
2230 if (lockclid) {
2231 struct nfs4_stateowner *sop = stp->st_stateowner;
2232 struct nfs4_client *clp = sop->so_client;
2233
2234 if (!sop->so_is_open_owner)
2235 goto out;
2236 if (!cmp_clid(&clp->cl_clientid, lockclid))
2237 goto out;
2238 }
2239
2240 if ((flags & CHECK_FH) && nfs4_check_fh(current_fh, stp)) {
2241 printk("NFSD: preprocess_seqid_op: fh-stateid mismatch!\n");
2242 goto out;
2243 }
2244
2245 *stpp = stp;
2246 *sopp = sop = stp->st_stateowner;
2247
2248 /*
2249 * We now validate the seqid and stateid generation numbers.
2250 * For the moment, we ignore the possibility of
2251 * generation number wraparound.
2252 */
2253 if (seqid != sop->so_seqid + 1)
2254 goto check_replay;
2255
2256 if (sop->so_confirmed) {
2257 if (flags & CONFIRM) {
2258 printk("NFSD: preprocess_seqid_op: expected unconfirmed stateowner!\n");
2259 goto out;
2260 }
2261 }
2262 else {
2263 if (!(flags & CONFIRM)) {
2264 printk("NFSD: preprocess_seqid_op: stateowner not confirmed yet!\n");
2265 goto out;
2266 }
2267 }
2268 if (stateid->si_generation > stp->st_stateid.si_generation) {
2269 printk("NFSD: preprocess_seqid_op: future stateid?!\n");
2270 goto out;
2271 }
2272
2273 status = nfserr_old_stateid;
2274 if (stateid->si_generation < stp->st_stateid.si_generation) {
2275 printk("NFSD: preprocess_seqid_op: old stateid!\n");
2276 goto out;
2277 }
2278 /* XXX renew the client lease here */
2279 status = nfs_ok;
2280
2281out:
2282 return status;
2283
2284no_nfs4_stateid:
2285
2286 /*
2287 * We determine whether this is a bad stateid or a replay,
2288 * starting by trying to look up the stateowner.
2289 * If stateowner is not found - stateid is bad.
2290 */
2291 if (!(sop = find_openstateowner_id(stateid->si_stateownerid, flags))) {
2292 printk("NFSD: preprocess_seqid_op: no stateowner or nfs4_stateid!\n");
2293 status = nfserr_bad_stateid;
2294 goto out;
2295 }
2296 *sopp = sop;
2297
2298check_replay:
2299 if (seqid == sop->so_seqid) {
2300 printk("NFSD: preprocess_seqid_op: retransmission?\n");
2301 /* indicate replay to calling function */
2302 status = NFSERR_REPLAY_ME;
2303 } else {
2304 printk("NFSD: preprocess_seqid_op: bad seqid (expected %d, got %d\n", sop->so_seqid +1, seqid);
2305
2306 *sopp = NULL;
2307 status = nfserr_bad_seqid;
2308 }
2309 goto out;
2310}
2311
2312int
2313nfsd4_open_confirm(struct svc_rqst *rqstp, struct svc_fh *current_fh, struct nfsd4_open_confirm *oc)
2314{
2315 int status;
2316 struct nfs4_stateowner *sop;
2317 struct nfs4_stateid *stp;
2318
2319 dprintk("NFSD: nfsd4_open_confirm on file %.*s\n",
2320 (int)current_fh->fh_dentry->d_name.len,
2321 current_fh->fh_dentry->d_name.name);
2322
2323 if ((status = fh_verify(rqstp, current_fh, S_IFREG, 0)))
2324 goto out;
2325
2326 nfs4_lock_state();
2327
2328 if ((status = nfs4_preprocess_seqid_op(current_fh, oc->oc_seqid,
2329 &oc->oc_req_stateid,
2330 CHECK_FH | CONFIRM | OPEN_STATE,
2331 &oc->oc_stateowner, &stp, NULL)))
2332 goto out;
2333
2334 sop = oc->oc_stateowner;
2335 sop->so_confirmed = 1;
2336 update_stateid(&stp->st_stateid);
2337 memcpy(&oc->oc_resp_stateid, &stp->st_stateid, sizeof(stateid_t));
2338 dprintk("NFSD: nfsd4_open_confirm: success, seqid=%d "
2339 "stateid=(%08x/%08x/%08x/%08x)\n", oc->oc_seqid,
2340 stp->st_stateid.si_boot,
2341 stp->st_stateid.si_stateownerid,
2342 stp->st_stateid.si_fileid,
2343 stp->st_stateid.si_generation);
2344out:
2345 if (oc->oc_stateowner)
2346 nfs4_get_stateowner(oc->oc_stateowner);
2347 nfs4_unlock_state();
2348 return status;
2349}
2350
2351
2352/*
2353 * unset all bits in union bitmap (bmap) that
2354 * do not exist in share (from successful OPEN_DOWNGRADE)
2355 */
2356static void
2357reset_union_bmap_access(unsigned long access, unsigned long *bmap)
2358{
2359 int i;
2360 for (i = 1; i < 4; i++) {
2361 if ((i & access) != i)
2362 __clear_bit(i, bmap);
2363 }
2364}
2365
2366static void
2367reset_union_bmap_deny(unsigned long deny, unsigned long *bmap)
2368{
2369 int i;
2370 for (i = 0; i < 4; i++) {
2371 if ((i & deny) != i)
2372 __clear_bit(i, bmap);
2373 }
2374}
2375
2376int
2377nfsd4_open_downgrade(struct svc_rqst *rqstp, struct svc_fh *current_fh, struct nfsd4_open_downgrade *od)
2378{
2379 int status;
2380 struct nfs4_stateid *stp;
2381 unsigned int share_access;
2382
2383 dprintk("NFSD: nfsd4_open_downgrade on file %.*s\n",
2384 (int)current_fh->fh_dentry->d_name.len,
2385 current_fh->fh_dentry->d_name.name);
2386
2387 if (!TEST_ACCESS(od->od_share_access) || !TEST_DENY(od->od_share_deny))
2388 return nfserr_inval;
2389
2390 nfs4_lock_state();
2391 if ((status = nfs4_preprocess_seqid_op(current_fh, od->od_seqid,
2392 &od->od_stateid,
2393 CHECK_FH | OPEN_STATE,
2394 &od->od_stateowner, &stp, NULL)))
2395 goto out;
2396
2397 status = nfserr_inval;
2398 if (!test_bit(od->od_share_access, &stp->st_access_bmap)) {
2399 dprintk("NFSD:access not a subset current bitmap: 0x%lx, input access=%08x\n",
2400 stp->st_access_bmap, od->od_share_access);
2401 goto out;
2402 }
2403 if (!test_bit(od->od_share_deny, &stp->st_deny_bmap)) {
2404 dprintk("NFSD:deny not a subset current bitmap: 0x%lx, input deny=%08x\n",
2405 stp->st_deny_bmap, od->od_share_deny);
2406 goto out;
2407 }
2408 set_access(&share_access, stp->st_access_bmap);
2409 nfs4_file_downgrade(stp->st_vfs_file,
2410 share_access & ~od->od_share_access);
2411
2412 reset_union_bmap_access(od->od_share_access, &stp->st_access_bmap);
2413 reset_union_bmap_deny(od->od_share_deny, &stp->st_deny_bmap);
2414
2415 update_stateid(&stp->st_stateid);
2416 memcpy(&od->od_stateid, &stp->st_stateid, sizeof(stateid_t));
2417 status = nfs_ok;
2418out:
2419 if (od->od_stateowner)
2420 nfs4_get_stateowner(od->od_stateowner);
2421 nfs4_unlock_state();
2422 return status;
2423}
2424
2425/*
2426 * nfs4_unlock_state() called after encode
2427 */
2428int
2429nfsd4_close(struct svc_rqst *rqstp, struct svc_fh *current_fh, struct nfsd4_close *close)
2430{
2431 int status;
2432 struct nfs4_stateid *stp;
2433
2434 dprintk("NFSD: nfsd4_close on file %.*s\n",
2435 (int)current_fh->fh_dentry->d_name.len,
2436 current_fh->fh_dentry->d_name.name);
2437
2438 nfs4_lock_state();
2439 /* check close_lru for replay */
2440 if ((status = nfs4_preprocess_seqid_op(current_fh, close->cl_seqid,
2441 &close->cl_stateid,
2442 CHECK_FH | OPEN_STATE | CLOSE_STATE,
2443 &close->cl_stateowner, &stp, NULL)))
2444 goto out;
2445 /*
2446 * Return success, but first update the stateid.
2447 */
2448 status = nfs_ok;
2449 update_stateid(&stp->st_stateid);
2450 memcpy(&close->cl_stateid, &stp->st_stateid, sizeof(stateid_t));
2451
2452 /* release_state_owner() calls nfsd_close() if needed */
2453 release_state_owner(stp, OPEN_STATE);
2454out:
2455 if (close->cl_stateowner)
2456 nfs4_get_stateowner(close->cl_stateowner);
2457 nfs4_unlock_state();
2458 return status;
2459}
2460
2461int
2462nfsd4_delegreturn(struct svc_rqst *rqstp, struct svc_fh *current_fh, struct nfsd4_delegreturn *dr)
2463{
2464 int status;
2465
2466 if ((status = fh_verify(rqstp, current_fh, S_IFREG, 0)))
2467 goto out;
2468
2469 nfs4_lock_state();
2470 status = nfs4_preprocess_stateid_op(current_fh, &dr->dr_stateid, DELEG_RET, NULL);
2471 nfs4_unlock_state();
2472out:
2473 return status;
2474}
2475
2476
2477/*
2478 * Lock owner state (byte-range locks)
2479 */
2480#define LOFF_OVERFLOW(start, len) ((u64)(len) > ~(u64)(start))
2481#define LOCK_HASH_BITS 8
2482#define LOCK_HASH_SIZE (1 << LOCK_HASH_BITS)
2483#define LOCK_HASH_MASK (LOCK_HASH_SIZE - 1)
2484
2485#define lockownerid_hashval(id) \
2486 ((id) & LOCK_HASH_MASK)
2487
2488static inline unsigned int
2489lock_ownerstr_hashval(struct inode *inode, u32 cl_id,
2490 struct xdr_netobj *ownername)
2491{
2492 return (file_hashval(inode) + cl_id
2493 + opaque_hashval(ownername->data, ownername->len))
2494 & LOCK_HASH_MASK;
2495}
2496
2497static struct list_head lock_ownerid_hashtbl[LOCK_HASH_SIZE];
2498static struct list_head lock_ownerstr_hashtbl[LOCK_HASH_SIZE];
2499static struct list_head lockstateid_hashtbl[STATEID_HASH_SIZE];
2500
NeilBrownfd39ca92005-06-23 22:04:03 -07002501static struct nfs4_stateid *
Linus Torvalds1da177e2005-04-16 15:20:36 -07002502find_stateid(stateid_t *stid, int flags)
2503{
2504 struct nfs4_stateid *local = NULL;
2505 u32 st_id = stid->si_stateownerid;
2506 u32 f_id = stid->si_fileid;
2507 unsigned int hashval;
2508
2509 dprintk("NFSD: find_stateid flags 0x%x\n",flags);
2510 if ((flags & LOCK_STATE) || (flags & RD_STATE) || (flags & WR_STATE)) {
2511 hashval = stateid_hashval(st_id, f_id);
2512 list_for_each_entry(local, &lockstateid_hashtbl[hashval], st_hash) {
2513 if ((local->st_stateid.si_stateownerid == st_id) &&
2514 (local->st_stateid.si_fileid == f_id))
2515 return local;
2516 }
2517 }
2518 if ((flags & OPEN_STATE) || (flags & RD_STATE) || (flags & WR_STATE)) {
2519 hashval = stateid_hashval(st_id, f_id);
2520 list_for_each_entry(local, &stateid_hashtbl[hashval], st_hash) {
2521 if ((local->st_stateid.si_stateownerid == st_id) &&
2522 (local->st_stateid.si_fileid == f_id))
2523 return local;
2524 }
2525 } else
2526 printk("NFSD: find_stateid: ERROR: no state flag\n");
2527 return NULL;
2528}
2529
2530static struct nfs4_delegation *
2531find_delegation_stateid(struct inode *ino, stateid_t *stid)
2532{
NeilBrown13cd2182005-06-23 22:03:10 -07002533 struct nfs4_file *fp;
2534 struct nfs4_delegation *dl;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002535
2536 dprintk("NFSD:find_delegation_stateid stateid=(%08x/%08x/%08x/%08x)\n",
2537 stid->si_boot, stid->si_stateownerid,
2538 stid->si_fileid, stid->si_generation);
2539
Linus Torvalds1da177e2005-04-16 15:20:36 -07002540 fp = find_file(ino);
NeilBrown13cd2182005-06-23 22:03:10 -07002541 if (!fp)
2542 return NULL;
2543 dl = find_delegation_file(fp, stid);
2544 put_nfs4_file(fp);
2545 return dl;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002546}
2547
2548/*
2549 * TODO: Linux file offsets are _signed_ 64-bit quantities, which means that
2550 * we can't properly handle lock requests that go beyond the (2^63 - 1)-th
2551 * byte, because of sign extension problems. Since NFSv4 calls for 64-bit
2552 * locking, this prevents us from being completely protocol-compliant. The
2553 * real solution to this problem is to start using unsigned file offsets in
2554 * the VFS, but this is a very deep change!
2555 */
2556static inline void
2557nfs4_transform_lock_offset(struct file_lock *lock)
2558{
2559 if (lock->fl_start < 0)
2560 lock->fl_start = OFFSET_MAX;
2561 if (lock->fl_end < 0)
2562 lock->fl_end = OFFSET_MAX;
2563}
2564
NeilBrownfd39ca92005-06-23 22:04:03 -07002565static int
Linus Torvalds1da177e2005-04-16 15:20:36 -07002566nfs4_verify_lock_stateowner(struct nfs4_stateowner *sop, unsigned int hashval)
2567{
2568 struct nfs4_stateowner *local = NULL;
2569 int status = 0;
2570
2571 if (hashval >= LOCK_HASH_SIZE)
2572 goto out;
2573 list_for_each_entry(local, &lock_ownerid_hashtbl[hashval], so_idhash) {
2574 if (local == sop) {
2575 status = 1;
2576 goto out;
2577 }
2578 }
2579out:
2580 return status;
2581}
2582
2583
2584static inline void
2585nfs4_set_lock_denied(struct file_lock *fl, struct nfsd4_lock_denied *deny)
2586{
2587 struct nfs4_stateowner *sop = (struct nfs4_stateowner *) fl->fl_owner;
2588 unsigned int hval = lockownerid_hashval(sop->so_id);
2589
2590 deny->ld_sop = NULL;
2591 if (nfs4_verify_lock_stateowner(sop, hval)) {
2592 kref_get(&sop->so_ref);
2593 deny->ld_sop = sop;
2594 deny->ld_clientid = sop->so_client->cl_clientid;
2595 }
2596 deny->ld_start = fl->fl_start;
2597 deny->ld_length = ~(u64)0;
2598 if (fl->fl_end != ~(u64)0)
2599 deny->ld_length = fl->fl_end - fl->fl_start + 1;
2600 deny->ld_type = NFS4_READ_LT;
2601 if (fl->fl_type != F_RDLCK)
2602 deny->ld_type = NFS4_WRITE_LT;
2603}
2604
2605static struct nfs4_stateowner *
2606find_lockstateowner(struct xdr_netobj *owner, clientid_t *clid)
2607{
2608 struct nfs4_stateowner *local = NULL;
2609 int i;
2610
2611 for (i = 0; i < LOCK_HASH_SIZE; i++) {
2612 list_for_each_entry(local, &lock_ownerid_hashtbl[i], so_idhash) {
2613 if (!cmp_owner_str(local, owner, clid))
2614 continue;
2615 return local;
2616 }
2617 }
2618 return NULL;
2619}
2620
2621static struct nfs4_stateowner *
2622find_lockstateowner_str(struct inode *inode, clientid_t *clid,
2623 struct xdr_netobj *owner)
2624{
2625 unsigned int hashval = lock_ownerstr_hashval(inode, clid->cl_id, owner);
2626 struct nfs4_stateowner *op;
2627
2628 list_for_each_entry(op, &lock_ownerstr_hashtbl[hashval], so_strhash) {
2629 if (cmp_owner_str(op, owner, clid))
2630 return op;
2631 }
2632 return NULL;
2633}
2634
2635/*
2636 * Alloc a lock owner structure.
2637 * Called in nfsd4_lock - therefore, OPEN and OPEN_CONFIRM (if needed) has
2638 * occured.
2639 *
2640 * strhashval = lock_ownerstr_hashval
2641 * so_seqid = lock->lk_new_lock_seqid - 1: it gets bumped in encode
2642 */
2643
2644static struct nfs4_stateowner *
2645alloc_init_lock_stateowner(unsigned int strhashval, struct nfs4_client *clp, struct nfs4_stateid *open_stp, struct nfsd4_lock *lock) {
2646 struct nfs4_stateowner *sop;
2647 struct nfs4_replay *rp;
2648 unsigned int idhashval;
2649
2650 if (!(sop = alloc_stateowner(&lock->lk_new_owner)))
2651 return NULL;
2652 idhashval = lockownerid_hashval(current_ownerid);
2653 INIT_LIST_HEAD(&sop->so_idhash);
2654 INIT_LIST_HEAD(&sop->so_strhash);
2655 INIT_LIST_HEAD(&sop->so_perclient);
2656 INIT_LIST_HEAD(&sop->so_perfilestate);
2657 INIT_LIST_HEAD(&sop->so_perlockowner);
2658 INIT_LIST_HEAD(&sop->so_close_lru); /* not used */
2659 sop->so_time = 0;
2660 list_add(&sop->so_idhash, &lock_ownerid_hashtbl[idhashval]);
2661 list_add(&sop->so_strhash, &lock_ownerstr_hashtbl[strhashval]);
2662 list_add(&sop->so_perlockowner, &open_stp->st_perlockowner);
2663 sop->so_is_open_owner = 0;
2664 sop->so_id = current_ownerid++;
2665 sop->so_client = clp;
2666 sop->so_seqid = lock->lk_new_lock_seqid - 1;
2667 sop->so_confirmed = 1;
2668 rp = &sop->so_replay;
2669 rp->rp_status = NFSERR_SERVERFAULT;
2670 rp->rp_buflen = 0;
2671 rp->rp_buf = rp->rp_ibuf;
2672 return sop;
2673}
2674
NeilBrownfd39ca92005-06-23 22:04:03 -07002675static struct nfs4_stateid *
Linus Torvalds1da177e2005-04-16 15:20:36 -07002676alloc_init_lock_stateid(struct nfs4_stateowner *sop, struct nfs4_file *fp, struct nfs4_stateid *open_stp)
2677{
2678 struct nfs4_stateid *stp;
2679 unsigned int hashval = stateid_hashval(sop->so_id, fp->fi_id);
2680
NeilBrown5ac049a2005-06-23 22:03:03 -07002681 stp = nfs4_alloc_stateid();
2682 if (stp == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002683 goto out;
2684 INIT_LIST_HEAD(&stp->st_hash);
2685 INIT_LIST_HEAD(&stp->st_perfile);
2686 INIT_LIST_HEAD(&stp->st_perfilestate);
2687 INIT_LIST_HEAD(&stp->st_perlockowner); /* not used */
2688 list_add(&stp->st_hash, &lockstateid_hashtbl[hashval]);
NeilBrown8beefa22005-06-23 22:03:08 -07002689 list_add(&stp->st_perfile, &fp->fi_stateids);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002690 list_add(&stp->st_perfilestate, &sop->so_perfilestate);
2691 stp->st_stateowner = sop;
NeilBrown13cd2182005-06-23 22:03:10 -07002692 get_nfs4_file(fp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002693 stp->st_file = fp;
2694 stp->st_stateid.si_boot = boot_time;
2695 stp->st_stateid.si_stateownerid = sop->so_id;
2696 stp->st_stateid.si_fileid = fp->fi_id;
2697 stp->st_stateid.si_generation = 0;
2698 stp->st_vfs_file = open_stp->st_vfs_file; /* FIXME refcount?? */
2699 stp->st_access_bmap = open_stp->st_access_bmap;
2700 stp->st_deny_bmap = open_stp->st_deny_bmap;
2701
2702out:
2703 return stp;
2704}
2705
NeilBrownfd39ca92005-06-23 22:04:03 -07002706static int
Linus Torvalds1da177e2005-04-16 15:20:36 -07002707check_lock_length(u64 offset, u64 length)
2708{
2709 return ((length == 0) || ((length != ~(u64)0) &&
2710 LOFF_OVERFLOW(offset, length)));
2711}
2712
2713/*
2714 * LOCK operation
2715 */
2716int
2717nfsd4_lock(struct svc_rqst *rqstp, struct svc_fh *current_fh, struct nfsd4_lock *lock)
2718{
2719 struct nfs4_stateowner *lock_sop = NULL, *open_sop = NULL;
2720 struct nfs4_stateid *lock_stp;
2721 struct file *filp;
2722 struct file_lock file_lock;
2723 struct file_lock *conflock;
2724 int status = 0;
2725 unsigned int strhashval;
2726
2727 dprintk("NFSD: nfsd4_lock: start=%Ld length=%Ld\n",
2728 (long long) lock->lk_offset,
2729 (long long) lock->lk_length);
2730
2731 if (nfs4_in_grace() && !lock->lk_reclaim)
2732 return nfserr_grace;
2733 if (!nfs4_in_grace() && lock->lk_reclaim)
2734 return nfserr_no_grace;
2735
2736 if (check_lock_length(lock->lk_offset, lock->lk_length))
2737 return nfserr_inval;
2738
2739 nfs4_lock_state();
2740
2741 if (lock->lk_is_new) {
2742 /*
2743 * Client indicates that this is a new lockowner.
2744 * Use open owner and open stateid to create lock owner and lock
2745 * stateid.
2746 */
2747 struct nfs4_stateid *open_stp = NULL;
2748 struct nfs4_file *fp;
2749
2750 status = nfserr_stale_clientid;
2751 if (STALE_CLIENTID(&lock->lk_new_clientid)) {
2752 printk("NFSD: nfsd4_lock: clientid is stale!\n");
2753 goto out;
2754 }
2755
2756 /* is the new lock seqid presented by the client zero? */
2757 status = nfserr_bad_seqid;
2758 if (lock->v.new.lock_seqid != 0)
2759 goto out;
2760
2761 /* validate and update open stateid and open seqid */
2762 status = nfs4_preprocess_seqid_op(current_fh,
2763 lock->lk_new_open_seqid,
2764 &lock->lk_new_open_stateid,
2765 CHECK_FH | OPEN_STATE,
2766 &open_sop, &open_stp,
2767 &lock->v.new.clientid);
2768 if (status) {
2769 if (lock->lk_reclaim)
2770 status = nfserr_reclaim_bad;
2771 goto out;
2772 }
2773 /* create lockowner and lock stateid */
2774 fp = open_stp->st_file;
2775 strhashval = lock_ownerstr_hashval(fp->fi_inode,
2776 open_sop->so_client->cl_clientid.cl_id,
2777 &lock->v.new.owner);
2778 /*
2779 * If we already have this lock owner, the client is in
2780 * error (or our bookeeping is wrong!)
2781 * for asking for a 'new lock'.
2782 */
2783 status = nfserr_bad_stateid;
2784 lock_sop = find_lockstateowner(&lock->v.new.owner,
2785 &lock->v.new.clientid);
2786 if (lock_sop)
2787 goto out;
2788 status = nfserr_resource;
2789 if (!(lock->lk_stateowner = alloc_init_lock_stateowner(strhashval, open_sop->so_client, open_stp, lock)))
2790 goto out;
2791 if ((lock_stp = alloc_init_lock_stateid(lock->lk_stateowner,
2792 fp, open_stp)) == NULL) {
2793 release_stateowner(lock->lk_stateowner);
2794 lock->lk_stateowner = NULL;
2795 goto out;
2796 }
2797 /* bump the open seqid used to create the lock */
2798 open_sop->so_seqid++;
2799 } else {
2800 /* lock (lock owner + lock stateid) already exists */
2801 status = nfs4_preprocess_seqid_op(current_fh,
2802 lock->lk_old_lock_seqid,
2803 &lock->lk_old_lock_stateid,
2804 CHECK_FH | LOCK_STATE,
2805 &lock->lk_stateowner, &lock_stp, NULL);
2806 if (status)
2807 goto out;
2808 }
2809 /* lock->lk_stateowner and lock_stp have been created or found */
2810 filp = lock_stp->st_vfs_file;
2811
2812 if ((status = fh_verify(rqstp, current_fh, S_IFREG, MAY_LOCK))) {
2813 printk("NFSD: nfsd4_lock: permission denied!\n");
2814 goto out;
2815 }
2816
2817 locks_init_lock(&file_lock);
2818 switch (lock->lk_type) {
2819 case NFS4_READ_LT:
2820 case NFS4_READW_LT:
2821 file_lock.fl_type = F_RDLCK;
2822 break;
2823 case NFS4_WRITE_LT:
2824 case NFS4_WRITEW_LT:
2825 file_lock.fl_type = F_WRLCK;
2826 break;
2827 default:
2828 status = nfserr_inval;
2829 goto out;
2830 }
2831 file_lock.fl_owner = (fl_owner_t) lock->lk_stateowner;
2832 file_lock.fl_pid = current->tgid;
2833 file_lock.fl_file = filp;
2834 file_lock.fl_flags = FL_POSIX;
2835
2836 file_lock.fl_start = lock->lk_offset;
2837 if ((lock->lk_length == ~(u64)0) ||
2838 LOFF_OVERFLOW(lock->lk_offset, lock->lk_length))
2839 file_lock.fl_end = ~(u64)0;
2840 else
2841 file_lock.fl_end = lock->lk_offset + lock->lk_length - 1;
2842 nfs4_transform_lock_offset(&file_lock);
2843
2844 /*
2845 * Try to lock the file in the VFS.
2846 * Note: locks.c uses the BKL to protect the inode's lock list.
2847 */
2848
2849 status = posix_lock_file(filp, &file_lock);
2850 if (file_lock.fl_ops && file_lock.fl_ops->fl_release_private)
2851 file_lock.fl_ops->fl_release_private(&file_lock);
2852 dprintk("NFSD: nfsd4_lock: posix_lock_file status %d\n",status);
2853 switch (-status) {
2854 case 0: /* success! */
2855 update_stateid(&lock_stp->st_stateid);
2856 memcpy(&lock->lk_resp_stateid, &lock_stp->st_stateid,
2857 sizeof(stateid_t));
2858 goto out;
2859 case (EAGAIN):
2860 goto conflicting_lock;
2861 case (EDEADLK):
2862 status = nfserr_deadlock;
2863 default:
2864 dprintk("NFSD: nfsd4_lock: posix_lock_file() failed! status %d\n",status);
2865 goto out_destroy_new_stateid;
2866 }
2867
2868conflicting_lock:
2869 dprintk("NFSD: nfsd4_lock: conflicting lock found!\n");
2870 status = nfserr_denied;
2871 /* XXX There is a race here. Future patch needed to provide
2872 * an atomic posix_lock_and_test_file
2873 */
2874 if (!(conflock = posix_test_lock(filp, &file_lock))) {
2875 status = nfserr_serverfault;
2876 goto out;
2877 }
2878 nfs4_set_lock_denied(conflock, &lock->lk_denied);
2879
2880out_destroy_new_stateid:
2881 if (lock->lk_is_new) {
2882 dprintk("NFSD: nfsd4_lock: destroy new stateid!\n");
2883 /*
2884 * An error encountered after instantiation of the new
2885 * stateid has forced us to destroy it.
2886 */
2887 if (!seqid_mutating_err(status))
2888 open_sop->so_seqid--;
2889
2890 release_state_owner(lock_stp, LOCK_STATE);
2891 }
2892out:
2893 if (lock->lk_stateowner)
2894 nfs4_get_stateowner(lock->lk_stateowner);
2895 nfs4_unlock_state();
2896 return status;
2897}
2898
2899/*
2900 * LOCKT operation
2901 */
2902int
2903nfsd4_lockt(struct svc_rqst *rqstp, struct svc_fh *current_fh, struct nfsd4_lockt *lockt)
2904{
2905 struct inode *inode;
2906 struct file file;
2907 struct file_lock file_lock;
2908 struct file_lock *conflicting_lock;
2909 int status;
2910
2911 if (nfs4_in_grace())
2912 return nfserr_grace;
2913
2914 if (check_lock_length(lockt->lt_offset, lockt->lt_length))
2915 return nfserr_inval;
2916
2917 lockt->lt_stateowner = NULL;
2918 nfs4_lock_state();
2919
2920 status = nfserr_stale_clientid;
2921 if (STALE_CLIENTID(&lockt->lt_clientid)) {
2922 printk("NFSD: nfsd4_lockt: clientid is stale!\n");
2923 goto out;
2924 }
2925
2926 if ((status = fh_verify(rqstp, current_fh, S_IFREG, 0))) {
2927 printk("NFSD: nfsd4_lockt: fh_verify() failed!\n");
2928 if (status == nfserr_symlink)
2929 status = nfserr_inval;
2930 goto out;
2931 }
2932
2933 inode = current_fh->fh_dentry->d_inode;
2934 locks_init_lock(&file_lock);
2935 switch (lockt->lt_type) {
2936 case NFS4_READ_LT:
2937 case NFS4_READW_LT:
2938 file_lock.fl_type = F_RDLCK;
2939 break;
2940 case NFS4_WRITE_LT:
2941 case NFS4_WRITEW_LT:
2942 file_lock.fl_type = F_WRLCK;
2943 break;
2944 default:
2945 printk("NFSD: nfs4_lockt: bad lock type!\n");
2946 status = nfserr_inval;
2947 goto out;
2948 }
2949
2950 lockt->lt_stateowner = find_lockstateowner_str(inode,
2951 &lockt->lt_clientid, &lockt->lt_owner);
2952 if (lockt->lt_stateowner)
2953 file_lock.fl_owner = (fl_owner_t)lockt->lt_stateowner;
2954 file_lock.fl_pid = current->tgid;
2955 file_lock.fl_flags = FL_POSIX;
2956
2957 file_lock.fl_start = lockt->lt_offset;
2958 if ((lockt->lt_length == ~(u64)0) || LOFF_OVERFLOW(lockt->lt_offset, lockt->lt_length))
2959 file_lock.fl_end = ~(u64)0;
2960 else
2961 file_lock.fl_end = lockt->lt_offset + lockt->lt_length - 1;
2962
2963 nfs4_transform_lock_offset(&file_lock);
2964
2965 /* posix_test_lock uses the struct file _only_ to resolve the inode.
2966 * since LOCKT doesn't require an OPEN, and therefore a struct
2967 * file may not exist, pass posix_test_lock a struct file with
2968 * only the dentry:inode set.
2969 */
2970 memset(&file, 0, sizeof (struct file));
2971 file.f_dentry = current_fh->fh_dentry;
2972
2973 status = nfs_ok;
2974 conflicting_lock = posix_test_lock(&file, &file_lock);
2975 if (conflicting_lock) {
2976 status = nfserr_denied;
2977 nfs4_set_lock_denied(conflicting_lock, &lockt->lt_denied);
2978 }
2979out:
2980 nfs4_unlock_state();
2981 return status;
2982}
2983
2984int
2985nfsd4_locku(struct svc_rqst *rqstp, struct svc_fh *current_fh, struct nfsd4_locku *locku)
2986{
2987 struct nfs4_stateid *stp;
2988 struct file *filp = NULL;
2989 struct file_lock file_lock;
2990 int status;
2991
2992 dprintk("NFSD: nfsd4_locku: start=%Ld length=%Ld\n",
2993 (long long) locku->lu_offset,
2994 (long long) locku->lu_length);
2995
2996 if (check_lock_length(locku->lu_offset, locku->lu_length))
2997 return nfserr_inval;
2998
2999 nfs4_lock_state();
3000
3001 if ((status = nfs4_preprocess_seqid_op(current_fh,
3002 locku->lu_seqid,
3003 &locku->lu_stateid,
3004 CHECK_FH | LOCK_STATE,
3005 &locku->lu_stateowner, &stp, NULL)))
3006 goto out;
3007
3008 filp = stp->st_vfs_file;
3009 BUG_ON(!filp);
3010 locks_init_lock(&file_lock);
3011 file_lock.fl_type = F_UNLCK;
3012 file_lock.fl_owner = (fl_owner_t) locku->lu_stateowner;
3013 file_lock.fl_pid = current->tgid;
3014 file_lock.fl_file = filp;
3015 file_lock.fl_flags = FL_POSIX;
3016 file_lock.fl_start = locku->lu_offset;
3017
3018 if ((locku->lu_length == ~(u64)0) || LOFF_OVERFLOW(locku->lu_offset, locku->lu_length))
3019 file_lock.fl_end = ~(u64)0;
3020 else
3021 file_lock.fl_end = locku->lu_offset + locku->lu_length - 1;
3022 nfs4_transform_lock_offset(&file_lock);
3023
3024 /*
3025 * Try to unlock the file in the VFS.
3026 */
3027 status = posix_lock_file(filp, &file_lock);
3028 if (file_lock.fl_ops && file_lock.fl_ops->fl_release_private)
3029 file_lock.fl_ops->fl_release_private(&file_lock);
3030 if (status) {
3031 printk("NFSD: nfs4_locku: posix_lock_file failed!\n");
3032 goto out_nfserr;
3033 }
3034 /*
3035 * OK, unlock succeeded; the only thing left to do is update the stateid.
3036 */
3037 update_stateid(&stp->st_stateid);
3038 memcpy(&locku->lu_stateid, &stp->st_stateid, sizeof(stateid_t));
3039
3040out:
3041 if (locku->lu_stateowner)
3042 nfs4_get_stateowner(locku->lu_stateowner);
3043 nfs4_unlock_state();
3044 return status;
3045
3046out_nfserr:
3047 status = nfserrno(status);
3048 goto out;
3049}
3050
3051/*
3052 * returns
3053 * 1: locks held by lockowner
3054 * 0: no locks held by lockowner
3055 */
3056static int
3057check_for_locks(struct file *filp, struct nfs4_stateowner *lowner)
3058{
3059 struct file_lock **flpp;
3060 struct inode *inode = filp->f_dentry->d_inode;
3061 int status = 0;
3062
3063 lock_kernel();
3064 for (flpp = &inode->i_flock; *flpp != NULL; flpp = &(*flpp)->fl_next) {
3065 if ((*flpp)->fl_owner == (fl_owner_t)lowner)
3066 status = 1;
3067 goto out;
3068 }
3069out:
3070 unlock_kernel();
3071 return status;
3072}
3073
3074int
3075nfsd4_release_lockowner(struct svc_rqst *rqstp, struct nfsd4_release_lockowner *rlockowner)
3076{
3077 clientid_t *clid = &rlockowner->rl_clientid;
3078 struct nfs4_stateowner *local = NULL;
3079 struct xdr_netobj *owner = &rlockowner->rl_owner;
3080 int status;
3081
3082 dprintk("nfsd4_release_lockowner clientid: (%08x/%08x):\n",
3083 clid->cl_boot, clid->cl_id);
3084
3085 /* XXX check for lease expiration */
3086
3087 status = nfserr_stale_clientid;
3088 if (STALE_CLIENTID(clid)) {
3089 printk("NFSD: nfsd4_release_lockowner: clientid is stale!\n");
3090 return status;
3091 }
3092
3093 nfs4_lock_state();
3094
3095 status = nfs_ok;
3096 local = find_lockstateowner(owner, clid);
3097 if (local) {
3098 struct nfs4_stateid *stp;
3099
3100 /* check for any locks held by any stateid
3101 * associated with the (lock) stateowner */
3102 status = nfserr_locks_held;
3103 list_for_each_entry(stp, &local->so_perfilestate,
3104 st_perfilestate) {
3105 if (check_for_locks(stp->st_vfs_file, local))
3106 goto out;
3107 }
3108 /* no locks held by (lock) stateowner */
3109 status = nfs_ok;
3110 release_stateowner(local);
3111 }
3112out:
3113 nfs4_unlock_state();
3114 return status;
3115}
3116
3117static inline struct nfs4_client_reclaim *
NeilBrowna55370a2005-06-23 22:03:52 -07003118alloc_reclaim(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003119{
NeilBrowna55370a2005-06-23 22:03:52 -07003120 return kmalloc(sizeof(struct nfs4_client_reclaim), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003121}
3122
3123/*
3124 * failure => all reset bets are off, nfserr_no_grace...
3125 */
3126static int
NeilBrowna55370a2005-06-23 22:03:52 -07003127nfs4_client_to_reclaim(char *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003128{
3129 unsigned int strhashval;
3130 struct nfs4_client_reclaim *crp = NULL;
3131
NeilBrowna55370a2005-06-23 22:03:52 -07003132 dprintk("NFSD nfs4_client_to_reclaim NAME: %.*s\n", HEXDIR_LEN, name);
3133 crp = alloc_reclaim();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003134 if (!crp)
3135 return 0;
NeilBrowna55370a2005-06-23 22:03:52 -07003136 strhashval = clientstr_hashval(name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003137 INIT_LIST_HEAD(&crp->cr_strhash);
3138 list_add(&crp->cr_strhash, &reclaim_str_hashtbl[strhashval]);
NeilBrowna55370a2005-06-23 22:03:52 -07003139 memcpy(crp->cr_recdir, name, HEXDIR_LEN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003140 reclaim_str_hashtbl_size++;
3141 return 1;
3142}
3143
3144static void
3145nfs4_release_reclaim(void)
3146{
3147 struct nfs4_client_reclaim *crp = NULL;
3148 int i;
3149
Linus Torvalds1da177e2005-04-16 15:20:36 -07003150 for (i = 0; i < CLIENT_HASH_SIZE; i++) {
3151 while (!list_empty(&reclaim_str_hashtbl[i])) {
3152 crp = list_entry(reclaim_str_hashtbl[i].next,
3153 struct nfs4_client_reclaim, cr_strhash);
3154 list_del(&crp->cr_strhash);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003155 kfree(crp);
3156 reclaim_str_hashtbl_size--;
3157 }
3158 }
3159 BUG_ON(reclaim_str_hashtbl_size);
3160}
3161
3162/*
3163 * called from OPEN, CLAIM_PREVIOUS with a new clientid. */
NeilBrownfd39ca92005-06-23 22:04:03 -07003164static struct nfs4_client_reclaim *
Linus Torvalds1da177e2005-04-16 15:20:36 -07003165nfs4_find_reclaim_client(clientid_t *clid)
3166{
3167 unsigned int strhashval;
3168 struct nfs4_client *clp;
3169 struct nfs4_client_reclaim *crp = NULL;
3170
3171
3172 /* find clientid in conf_id_hashtbl */
3173 clp = find_confirmed_client(clid);
3174 if (clp == NULL)
3175 return NULL;
3176
NeilBrowna55370a2005-06-23 22:03:52 -07003177 dprintk("NFSD: nfs4_find_reclaim_client for %.*s with recdir %s\n",
3178 clp->cl_name.len, clp->cl_name.data,
3179 clp->cl_recdir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003180
3181 /* find clp->cl_name in reclaim_str_hashtbl */
NeilBrowna55370a2005-06-23 22:03:52 -07003182 strhashval = clientstr_hashval(clp->cl_recdir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003183 list_for_each_entry(crp, &reclaim_str_hashtbl[strhashval], cr_strhash) {
NeilBrowna55370a2005-06-23 22:03:52 -07003184 if (same_name(crp->cr_recdir, clp->cl_recdir)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003185 return crp;
3186 }
3187 }
3188 return NULL;
3189}
3190
3191/*
3192* Called from OPEN. Look for clientid in reclaim list.
3193*/
3194int
3195nfs4_check_open_reclaim(clientid_t *clid)
3196{
NeilBrowndfc83562005-06-23 22:03:16 -07003197 return nfs4_find_reclaim_client(clid) ? nfs_ok : nfserr_reclaim_bad;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003198}
3199
NeilBrownac4d8ff2005-06-23 22:03:30 -07003200/* initialization to perform at module load time: */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003201
NeilBrownac4d8ff2005-06-23 22:03:30 -07003202void
3203nfs4_state_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003204{
3205 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003206
Linus Torvalds1da177e2005-04-16 15:20:36 -07003207 for (i = 0; i < CLIENT_HASH_SIZE; i++) {
3208 INIT_LIST_HEAD(&conf_id_hashtbl[i]);
3209 INIT_LIST_HEAD(&conf_str_hashtbl[i]);
3210 INIT_LIST_HEAD(&unconf_str_hashtbl[i]);
3211 INIT_LIST_HEAD(&unconf_id_hashtbl[i]);
3212 }
3213 for (i = 0; i < FILE_HASH_SIZE; i++) {
3214 INIT_LIST_HEAD(&file_hashtbl[i]);
3215 }
3216 for (i = 0; i < OWNER_HASH_SIZE; i++) {
3217 INIT_LIST_HEAD(&ownerstr_hashtbl[i]);
3218 INIT_LIST_HEAD(&ownerid_hashtbl[i]);
3219 }
3220 for (i = 0; i < STATEID_HASH_SIZE; i++) {
3221 INIT_LIST_HEAD(&stateid_hashtbl[i]);
3222 INIT_LIST_HEAD(&lockstateid_hashtbl[i]);
3223 }
3224 for (i = 0; i < LOCK_HASH_SIZE; i++) {
3225 INIT_LIST_HEAD(&lock_ownerid_hashtbl[i]);
3226 INIT_LIST_HEAD(&lock_ownerstr_hashtbl[i]);
3227 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003228 memset(&onestateid, ~0, sizeof(stateid_t));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003229 INIT_LIST_HEAD(&close_lru);
3230 INIT_LIST_HEAD(&client_lru);
3231 INIT_LIST_HEAD(&del_recall_lru);
NeilBrownac4d8ff2005-06-23 22:03:30 -07003232 for (i = 0; i < CLIENT_HASH_SIZE; i++)
3233 INIT_LIST_HEAD(&reclaim_str_hashtbl[i]);
3234 reclaim_str_hashtbl_size = 0;
NeilBrownac4d8ff2005-06-23 22:03:30 -07003235}
3236
3237/* initialization to perform when the nfsd service is started: */
3238
3239static void
3240__nfs4_state_start(void)
3241{
3242 time_t grace_time;
3243
Linus Torvalds1da177e2005-04-16 15:20:36 -07003244 boot_time = get_seconds();
NeilBrownd99a05a2005-06-23 22:03:21 -07003245 grace_time = max(user_lease_time, lease_time);
3246 lease_time = user_lease_time;
NeilBrowna76b4312005-06-23 22:04:01 -07003247 in_grace = 1;
NeilBrownd99a05a2005-06-23 22:03:21 -07003248 printk("NFSD: starting %ld-second grace period\n", grace_time);
NeilBrown58da2822005-06-23 22:03:19 -07003249 laundry_wq = create_singlethread_workqueue("nfsd4");
NeilBrowna76b4312005-06-23 22:04:01 -07003250 queue_delayed_work(laundry_wq, &laundromat_work, grace_time*HZ);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003251}
3252
3253int
NeilBrown76a35502005-06-23 22:03:26 -07003254nfs4_state_start(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003255{
3256 int status;
3257
3258 if (nfs4_init)
3259 return 0;
3260 status = nfsd4_init_slabs();
3261 if (status)
3262 return status;
NeilBrown76a35502005-06-23 22:03:26 -07003263 __nfs4_state_start();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003264 nfs4_init = 1;
3265 return 0;
3266}
3267
3268int
3269nfs4_in_grace(void)
3270{
NeilBrowna76b4312005-06-23 22:04:01 -07003271 return in_grace;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003272}
3273
3274time_t
3275nfs4_lease_time(void)
3276{
3277 return lease_time;
3278}
3279
3280static void
3281__nfs4_state_shutdown(void)
3282{
3283 int i;
3284 struct nfs4_client *clp = NULL;
3285 struct nfs4_delegation *dp = NULL;
3286 struct nfs4_stateowner *sop = NULL;
3287 struct list_head *pos, *next, reaplist;
3288
3289 list_for_each_safe(pos, next, &close_lru) {
3290 sop = list_entry(pos, struct nfs4_stateowner, so_close_lru);
3291 list_del(&sop->so_close_lru);
3292 nfs4_put_stateowner(sop);
3293 }
3294
3295 for (i = 0; i < CLIENT_HASH_SIZE; i++) {
3296 while (!list_empty(&conf_id_hashtbl[i])) {
3297 clp = list_entry(conf_id_hashtbl[i].next, struct nfs4_client, cl_idhash);
3298 expire_client(clp);
3299 }
3300 while (!list_empty(&unconf_str_hashtbl[i])) {
3301 clp = list_entry(unconf_str_hashtbl[i].next, struct nfs4_client, cl_strhash);
3302 expire_client(clp);
3303 }
3304 }
3305 INIT_LIST_HEAD(&reaplist);
3306 spin_lock(&recall_lock);
3307 list_for_each_safe(pos, next, &del_recall_lru) {
3308 dp = list_entry (pos, struct nfs4_delegation, dl_recall_lru);
3309 list_move(&dp->dl_recall_lru, &reaplist);
3310 }
3311 spin_unlock(&recall_lock);
3312 list_for_each_safe(pos, next, &reaplist) {
3313 dp = list_entry (pos, struct nfs4_delegation, dl_recall_lru);
3314 list_del_init(&dp->dl_recall_lru);
3315 unhash_delegation(dp);
3316 }
3317
Linus Torvalds1da177e2005-04-16 15:20:36 -07003318 cancel_delayed_work(&laundromat_work);
NeilBrown58da2822005-06-23 22:03:19 -07003319 flush_workqueue(laundry_wq);
3320 destroy_workqueue(laundry_wq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003321 nfs4_init = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003322}
3323
3324void
3325nfs4_state_shutdown(void)
3326{
3327 nfs4_lock_state();
3328 nfs4_release_reclaim();
3329 __nfs4_state_shutdown();
3330 nfsd4_free_slabs();
3331 nfs4_unlock_state();
3332}
3333
3334/*
3335 * Called when leasetime is changed.
3336 *
NeilBrownd99a05a2005-06-23 22:03:21 -07003337 * The only way the protocol gives us to handle on-the-fly lease changes is to
3338 * simulate a reboot. Instead of doing that, we just wait till the next time
3339 * we start to register any changes in lease time. If the administrator
3340 * really wants to change the lease time *now*, they can go ahead and bring
3341 * nfsd down and then back up again after changing the lease time.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003342 */
3343void
3344nfs4_reset_lease(time_t leasetime)
3345{
NeilBrownd99a05a2005-06-23 22:03:21 -07003346 lock_kernel();
3347 user_lease_time = leasetime;
3348 unlock_kernel();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003349}