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