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