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