nfsd: reduce some spinlocking in put_client_renew
[linux-2.6-block.git] / fs / nfsd / nfs4state.c
CommitLineData
1da177e4 1/*
1da177e4
LT
2* Copyright (c) 2001 The Regents of the University of Michigan.
3* All rights reserved.
4*
5* Kendrick Smith <kmsmith@umich.edu>
6* Andy Adamson <kandros@umich.edu>
7*
8* Redistribution and use in source and binary forms, with or without
9* modification, are permitted provided that the following conditions
10* are met:
11*
12* 1. Redistributions of source code must retain the above copyright
13* notice, this list of conditions and the following disclaimer.
14* 2. Redistributions in binary form must reproduce the above copyright
15* notice, this list of conditions and the following disclaimer in the
16* documentation and/or other materials provided with the distribution.
17* 3. Neither the name of the University nor the names of its
18* contributors may be used to endorse or promote products derived
19* from this software without specific prior written permission.
20*
21* THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED
22* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
23* MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
24* DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
26* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
27* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
28* BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
29* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
30* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
31* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32*
33*/
34
aceaf78d 35#include <linux/file.h>
b89f4321 36#include <linux/fs.h>
5a0e3ad6 37#include <linux/slab.h>
0964a3d3 38#include <linux/namei.h>
c2f1a551 39#include <linux/swap.h>
17456804 40#include <linux/pagemap.h>
7df302f7 41#include <linux/ratelimit.h>
68e76ad0 42#include <linux/sunrpc/svcauth_gss.h>
5976687a 43#include <linux/sunrpc/addr.h>
6282cd56 44#include <linux/hash.h>
9a74af21 45#include "xdr4.h"
06b332a5 46#include "xdr4cb.h"
0a3adade 47#include "vfs.h"
bfa4b365 48#include "current_stateid.h"
1da177e4 49
5e1533c7
SK
50#include "netns.h"
51
1da177e4
LT
52#define NFSDDBG_FACILITY NFSDDBG_PROC
53
f32f3c2d
BF
54#define all_ones {{~0,~0},~0}
55static const stateid_t one_stateid = {
56 .si_generation = ~0,
57 .si_opaque = all_ones,
58};
59static const stateid_t zero_stateid = {
60 /* all fields zero */
61};
19ff0f28
TM
62static const stateid_t currentstateid = {
63 .si_generation = 1,
64};
f32f3c2d 65
ec6b5d7b 66static u64 current_sessionid = 1;
fd39ca9a 67
f32f3c2d
BF
68#define ZERO_STATEID(stateid) (!memcmp((stateid), &zero_stateid, sizeof(stateid_t)))
69#define ONE_STATEID(stateid) (!memcmp((stateid), &one_stateid, sizeof(stateid_t)))
19ff0f28 70#define CURRENT_STATEID(stateid) (!memcmp((stateid), &currentstateid, sizeof(stateid_t)))
1da177e4 71
1da177e4 72/* forward declarations */
fe0750e5 73static int check_for_locks(struct nfs4_file *filp, struct nfs4_lockowner *lowner);
1da177e4 74
8b671b80
BF
75/* Locking: */
76
77/* Currently used for almost all code touching nfsv4 state: */
353ab6e9 78static DEFINE_MUTEX(client_mutex);
1da177e4 79
8b671b80
BF
80/*
81 * Currently used for the del_recall_lru and file hash table. In an
82 * effort to decrease the scope of the client_mutex, this spinlock may
83 * eventually cover more:
84 */
cdc97505 85static DEFINE_SPINLOCK(state_lock);
8b671b80 86
abf1135b
CH
87static struct kmem_cache *openowner_slab;
88static struct kmem_cache *lockowner_slab;
89static struct kmem_cache *file_slab;
90static struct kmem_cache *stateid_slab;
91static struct kmem_cache *deleg_slab;
e60d4398 92
1da177e4
LT
93void
94nfs4_lock_state(void)
95{
353ab6e9 96 mutex_lock(&client_mutex);
1da177e4
LT
97}
98
66b2b9b2 99static void free_session(struct nfsd4_session *);
508dc6e1 100
f0f51f5c 101static bool is_session_dead(struct nfsd4_session *ses)
66b2b9b2 102{
f0f51f5c 103 return ses->se_flags & NFS4_SESSION_DEAD;
66b2b9b2
BF
104}
105
f0f51f5c 106static __be32 mark_session_dead_locked(struct nfsd4_session *ses, int ref_held_by_me)
508dc6e1 107{
f0f51f5c 108 if (atomic_read(&ses->se_ref) > ref_held_by_me)
66b2b9b2
BF
109 return nfserr_jukebox;
110 ses->se_flags |= NFS4_SESSION_DEAD;
111 return nfs_ok;
508dc6e1
BH
112}
113
1da177e4
LT
114void
115nfs4_unlock_state(void)
116{
353ab6e9 117 mutex_unlock(&client_mutex);
1da177e4
LT
118}
119
221a6876
BF
120static bool is_client_expired(struct nfs4_client *clp)
121{
122 return clp->cl_time == 0;
123}
124
125static __be32 mark_client_expired_locked(struct nfs4_client *clp)
126{
127 if (atomic_read(&clp->cl_refcount))
128 return nfserr_jukebox;
129 clp->cl_time = 0;
130 return nfs_ok;
131}
132
133static __be32 mark_client_expired(struct nfs4_client *clp)
134{
135 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
136 __be32 ret;
137
138 spin_lock(&nn->client_lock);
139 ret = mark_client_expired_locked(clp);
140 spin_unlock(&nn->client_lock);
141 return ret;
142}
143
144static __be32 get_client_locked(struct nfs4_client *clp)
145{
146 if (is_client_expired(clp))
147 return nfserr_expired;
148 atomic_inc(&clp->cl_refcount);
149 return nfs_ok;
150}
151
152/* must be called under the client_lock */
153static inline void
154renew_client_locked(struct nfs4_client *clp)
155{
156 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
157
158 if (is_client_expired(clp)) {
159 WARN_ON(1);
160 printk("%s: client (clientid %08x/%08x) already expired\n",
161 __func__,
162 clp->cl_clientid.cl_boot,
163 clp->cl_clientid.cl_id);
164 return;
165 }
166
167 dprintk("renewing client (clientid %08x/%08x)\n",
168 clp->cl_clientid.cl_boot,
169 clp->cl_clientid.cl_id);
170 list_move_tail(&clp->cl_lru, &nn->client_lru);
171 clp->cl_time = get_seconds();
172}
173
174static inline void
175renew_client(struct nfs4_client *clp)
176{
177 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
178
179 spin_lock(&nn->client_lock);
180 renew_client_locked(clp);
181 spin_unlock(&nn->client_lock);
182}
183
ba138435 184static void put_client_renew_locked(struct nfs4_client *clp)
221a6876
BF
185{
186 if (!atomic_dec_and_test(&clp->cl_refcount))
187 return;
188 if (!is_client_expired(clp))
189 renew_client_locked(clp);
190}
191
4b24ca7d
JL
192static void put_client_renew(struct nfs4_client *clp)
193{
194 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
195
d6c249b4
JL
196 if (!atomic_dec_and_lock(&clp->cl_refcount, &nn->client_lock))
197 return;
198 if (!is_client_expired(clp))
199 renew_client_locked(clp);
4b24ca7d
JL
200 spin_unlock(&nn->client_lock);
201}
202
d4e19e70
TM
203static __be32 nfsd4_get_session_locked(struct nfsd4_session *ses)
204{
205 __be32 status;
206
207 if (is_session_dead(ses))
208 return nfserr_badsession;
209 status = get_client_locked(ses->se_client);
210 if (status)
211 return status;
212 atomic_inc(&ses->se_ref);
213 return nfs_ok;
214}
215
216static void nfsd4_put_session_locked(struct nfsd4_session *ses)
217{
218 struct nfs4_client *clp = ses->se_client;
219
220 if (atomic_dec_and_test(&ses->se_ref) && is_session_dead(ses))
221 free_session(ses);
222 put_client_renew_locked(clp);
223}
224
225static void nfsd4_put_session(struct nfsd4_session *ses)
226{
227 struct nfs4_client *clp = ses->se_client;
228 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
229
230 spin_lock(&nn->client_lock);
231 nfsd4_put_session_locked(ses);
232 spin_unlock(&nn->client_lock);
233}
234
235
1da177e4
LT
236static inline u32
237opaque_hashval(const void *ptr, int nbytes)
238{
239 unsigned char *cptr = (unsigned char *) ptr;
240
241 u32 x = 0;
242 while (nbytes--) {
243 x *= 37;
244 x += *cptr++;
245 }
246 return x;
247}
248
32513b40
BF
249static void nfsd4_free_file(struct nfs4_file *f)
250{
251 kmem_cache_free(file_slab, f);
252}
253
13cd2184
N
254static inline void
255put_nfs4_file(struct nfs4_file *fi)
256{
cdc97505 257 if (atomic_dec_and_lock(&fi->fi_ref, &state_lock)) {
89876f8c 258 hlist_del(&fi->fi_hash);
cdc97505 259 spin_unlock(&state_lock);
8b671b80 260 iput(fi->fi_inode);
32513b40 261 nfsd4_free_file(fi);
8b671b80 262 }
13cd2184
N
263}
264
265static inline void
266get_nfs4_file(struct nfs4_file *fi)
267{
8b671b80 268 atomic_inc(&fi->fi_ref);
13cd2184
N
269}
270
ef0f3390 271static int num_delegations;
697ce9be 272unsigned long max_delegations;
ef0f3390
N
273
274/*
275 * Open owner state (share locks)
276 */
277
16bfdaaf
BF
278/* hash tables for lock and open owners */
279#define OWNER_HASH_BITS 8
280#define OWNER_HASH_SIZE (1 << OWNER_HASH_BITS)
281#define OWNER_HASH_MASK (OWNER_HASH_SIZE - 1)
ef0f3390 282
16bfdaaf 283static unsigned int ownerstr_hashval(u32 clientid, struct xdr_netobj *ownername)
ddc04c41
BF
284{
285 unsigned int ret;
286
287 ret = opaque_hashval(ownername->data, ownername->len);
288 ret += clientid;
16bfdaaf 289 return ret & OWNER_HASH_MASK;
ddc04c41 290}
ef0f3390 291
ef0f3390
N
292/* hash table for nfs4_file */
293#define FILE_HASH_BITS 8
294#define FILE_HASH_SIZE (1 << FILE_HASH_BITS)
35079582 295
ddc04c41
BF
296static unsigned int file_hashval(struct inode *ino)
297{
298 /* XXX: why are we hashing on inode pointer, anyway? */
299 return hash_ptr(ino, FILE_HASH_BITS);
300}
301
89876f8c 302static struct hlist_head file_hashtbl[FILE_HASH_SIZE];
ef0f3390 303
3477565e
BF
304static void __nfs4_file_get_access(struct nfs4_file *fp, int oflag)
305{
306 WARN_ON_ONCE(!(fp->fi_fds[oflag] || fp->fi_fds[O_RDWR]));
307 atomic_inc(&fp->fi_access[oflag]);
308}
309
998db52c
BF
310static void nfs4_file_get_access(struct nfs4_file *fp, int oflag)
311{
312 if (oflag == O_RDWR) {
3477565e
BF
313 __nfs4_file_get_access(fp, O_RDONLY);
314 __nfs4_file_get_access(fp, O_WRONLY);
998db52c 315 } else
3477565e 316 __nfs4_file_get_access(fp, oflag);
998db52c
BF
317}
318
319static void nfs4_file_put_fd(struct nfs4_file *fp, int oflag)
f9d7562f
BF
320{
321 if (fp->fi_fds[oflag]) {
322 fput(fp->fi_fds[oflag]);
323 fp->fi_fds[oflag] = NULL;
324 }
325}
326
998db52c 327static void __nfs4_file_put_access(struct nfs4_file *fp, int oflag)
f9d7562f
BF
328{
329 if (atomic_dec_and_test(&fp->fi_access[oflag])) {
f9d7562f 330 nfs4_file_put_fd(fp, oflag);
0c7c3e67 331 if (atomic_read(&fp->fi_access[1 - oflag]) == 0)
3d02fa29 332 nfs4_file_put_fd(fp, O_RDWR);
f9d7562f
BF
333 }
334}
335
998db52c
BF
336static void nfs4_file_put_access(struct nfs4_file *fp, int oflag)
337{
338 if (oflag == O_RDWR) {
339 __nfs4_file_put_access(fp, O_RDONLY);
340 __nfs4_file_put_access(fp, O_WRONLY);
341 } else
342 __nfs4_file_put_access(fp, oflag);
343}
344
3abdb607
BF
345static struct nfs4_stid *nfs4_alloc_stid(struct nfs4_client *cl, struct
346kmem_cache *slab)
2a74aba7 347{
3abdb607 348 struct idr *stateids = &cl->cl_stateids;
3abdb607 349 struct nfs4_stid *stid;
6136d2b4 350 int new_id;
2a74aba7 351
3abdb607
BF
352 stid = kmem_cache_alloc(slab, GFP_KERNEL);
353 if (!stid)
354 return NULL;
355
398c33aa 356 new_id = idr_alloc_cyclic(stateids, stid, 0, 0, GFP_KERNEL);
ebd6c707 357 if (new_id < 0)
3abdb607 358 goto out_free;
2a74aba7 359 stid->sc_client = cl;
3abdb607
BF
360 stid->sc_type = 0;
361 stid->sc_stateid.si_opaque.so_id = new_id;
362 stid->sc_stateid.si_opaque.so_clid = cl->cl_clientid;
2a74aba7 363 /* Will be incremented before return to client: */
3abdb607 364 stid->sc_stateid.si_generation = 0;
996e0938 365
996e0938 366 /*
3abdb607
BF
367 * It shouldn't be a problem to reuse an opaque stateid value.
368 * I don't think it is for 4.1. But with 4.0 I worry that, for
369 * example, a stray write retransmission could be accepted by
370 * the server when it should have been rejected. Therefore,
371 * adopt a trick from the sctp code to attempt to maximize the
372 * amount of time until an id is reused, by ensuring they always
373 * "increase" (mod INT_MAX):
996e0938 374 */
3abdb607
BF
375 return stid;
376out_free:
2c44a234 377 kmem_cache_free(slab, stid);
3abdb607 378 return NULL;
2a74aba7
BF
379}
380
4cdc951b
BF
381static struct nfs4_ol_stateid * nfs4_alloc_stateid(struct nfs4_client *clp)
382{
383 return openlockstateid(nfs4_alloc_stid(clp, stateid_slab));
384}
385
6282cd56
N
386/*
387 * When we recall a delegation, we should be careful not to hand it
388 * out again straight away.
389 * To ensure this we keep a pair of bloom filters ('new' and 'old')
390 * in which the filehandles of recalled delegations are "stored".
391 * If a filehandle appear in either filter, a delegation is blocked.
392 * When a delegation is recalled, the filehandle is stored in the "new"
393 * filter.
394 * Every 30 seconds we swap the filters and clear the "new" one,
395 * unless both are empty of course.
396 *
397 * Each filter is 256 bits. We hash the filehandle to 32bit and use the
398 * low 3 bytes as hash-table indices.
399 *
400 * 'state_lock', which is always held when block_delegations() is called,
401 * is used to manage concurrent access. Testing does not need the lock
402 * except when swapping the two filters.
403 */
404static struct bloom_pair {
405 int entries, old_entries;
406 time_t swap_time;
407 int new; /* index into 'set' */
408 DECLARE_BITMAP(set[2], 256);
409} blocked_delegations;
410
411static int delegation_blocked(struct knfsd_fh *fh)
412{
413 u32 hash;
414 struct bloom_pair *bd = &blocked_delegations;
415
416 if (bd->entries == 0)
417 return 0;
418 if (seconds_since_boot() - bd->swap_time > 30) {
419 spin_lock(&state_lock);
420 if (seconds_since_boot() - bd->swap_time > 30) {
421 bd->entries -= bd->old_entries;
422 bd->old_entries = bd->entries;
423 memset(bd->set[bd->new], 0,
424 sizeof(bd->set[0]));
425 bd->new = 1-bd->new;
426 bd->swap_time = seconds_since_boot();
427 }
428 spin_unlock(&state_lock);
429 }
430 hash = arch_fast_hash(&fh->fh_base, fh->fh_size, 0);
431 if (test_bit(hash&255, bd->set[0]) &&
432 test_bit((hash>>8)&255, bd->set[0]) &&
433 test_bit((hash>>16)&255, bd->set[0]))
434 return 1;
435
436 if (test_bit(hash&255, bd->set[1]) &&
437 test_bit((hash>>8)&255, bd->set[1]) &&
438 test_bit((hash>>16)&255, bd->set[1]))
439 return 1;
440
441 return 0;
442}
443
444static void block_delegations(struct knfsd_fh *fh)
445{
446 u32 hash;
447 struct bloom_pair *bd = &blocked_delegations;
448
449 hash = arch_fast_hash(&fh->fh_base, fh->fh_size, 0);
450
451 __set_bit(hash&255, bd->set[bd->new]);
452 __set_bit((hash>>8)&255, bd->set[bd->new]);
453 __set_bit((hash>>16)&255, bd->set[bd->new]);
454 if (bd->entries == 0)
455 bd->swap_time = seconds_since_boot();
456 bd->entries += 1;
457}
458
1da177e4 459static struct nfs4_delegation *
99c41515 460alloc_init_deleg(struct nfs4_client *clp, struct nfs4_ol_stateid *stp, struct svc_fh *current_fh)
1da177e4
LT
461{
462 struct nfs4_delegation *dp;
1da177e4
LT
463
464 dprintk("NFSD alloc_init_deleg\n");
c2f1a551 465 if (num_delegations > max_delegations)
ef0f3390 466 return NULL;
6282cd56
N
467 if (delegation_blocked(&current_fh->fh_handle))
468 return NULL;
996e0938 469 dp = delegstateid(nfs4_alloc_stid(clp, deleg_slab));
5b2d21c1 470 if (dp == NULL)
1da177e4 471 return dp;
2a74aba7
BF
472 /*
473 * delegation seqid's are never incremented. The 4.1 special
6136d2b4
BF
474 * meaning of seqid 0 isn't meaningful, really, but let's avoid
475 * 0 anyway just for consistency and use 1:
2a74aba7
BF
476 */
477 dp->dl_stid.sc_stateid.si_generation = 1;
ef0f3390 478 num_delegations++;
ea1da636
N
479 INIT_LIST_HEAD(&dp->dl_perfile);
480 INIT_LIST_HEAD(&dp->dl_perclnt);
1da177e4 481 INIT_LIST_HEAD(&dp->dl_recall_lru);
bf7bd3e9 482 dp->dl_file = NULL;
99c41515 483 dp->dl_type = NFS4_OPEN_DELEGATE_READ;
6c02eaa1 484 fh_copy_shallow(&dp->dl_fh, &current_fh->fh_handle);
1da177e4
LT
485 dp->dl_time = 0;
486 atomic_set(&dp->dl_count, 1);
57725155 487 nfsd4_init_callback(&dp->dl_recall);
1da177e4
LT
488 return dp;
489}
490
68a33961 491static void remove_stid(struct nfs4_stid *s)
3abdb607
BF
492{
493 struct idr *stateids = &s->sc_client->cl_stateids;
494
495 idr_remove(stateids, s->sc_stateid.si_opaque.so_id);
3abdb607
BF
496}
497
9857df81
BH
498static void nfs4_free_stid(struct kmem_cache *slab, struct nfs4_stid *s)
499{
500 kmem_cache_free(slab, s);
501}
502
1da177e4
LT
503void
504nfs4_put_delegation(struct nfs4_delegation *dp)
505{
506 if (atomic_dec_and_test(&dp->dl_count)) {
9857df81 507 nfs4_free_stid(deleg_slab, &dp->dl_stid);
ef0f3390 508 num_delegations--;
1da177e4
LT
509 }
510}
511
acfdf5c3 512static void nfs4_put_deleg_lease(struct nfs4_file *fp)
1da177e4 513{
cbf7a75b
BF
514 if (!fp->fi_lease)
515 return;
acfdf5c3
BF
516 if (atomic_dec_and_test(&fp->fi_delegees)) {
517 vfs_setlease(fp->fi_deleg_file, F_UNLCK, &fp->fi_lease);
518 fp->fi_lease = NULL;
4ee63624 519 fput(fp->fi_deleg_file);
acfdf5c3
BF
520 fp->fi_deleg_file = NULL;
521 }
1da177e4
LT
522}
523
6136d2b4
BF
524static void unhash_stid(struct nfs4_stid *s)
525{
3abdb607 526 s->sc_type = 0;
6136d2b4
BF
527}
528
931ee56c
BH
529static void
530hash_delegation_locked(struct nfs4_delegation *dp, struct nfs4_file *fp)
531{
cdc97505 532 lockdep_assert_held(&state_lock);
931ee56c 533
3fb87d13 534 dp->dl_stid.sc_type = NFS4_DELEG_STID;
931ee56c
BH
535 list_add(&dp->dl_perfile, &fp->fi_delegations);
536 list_add(&dp->dl_perclnt, &dp->dl_stid.sc_client->cl_delegations);
537}
538
1da177e4
LT
539/* Called under the state lock. */
540static void
541unhash_delegation(struct nfs4_delegation *dp)
542{
cdc97505 543 spin_lock(&state_lock);
931ee56c 544 list_del_init(&dp->dl_perclnt);
5d926e8c 545 list_del_init(&dp->dl_perfile);
1da177e4 546 list_del_init(&dp->dl_recall_lru);
cdc97505 547 spin_unlock(&state_lock);
cbf7a75b
BF
548 if (dp->dl_file) {
549 nfs4_put_deleg_lease(dp->dl_file);
550 put_nfs4_file(dp->dl_file);
551 dp->dl_file = NULL;
552 }
3bd64a5b
BF
553}
554
555
556
557static void destroy_revoked_delegation(struct nfs4_delegation *dp)
558{
559 list_del_init(&dp->dl_recall_lru);
68a33961 560 remove_stid(&dp->dl_stid);
1da177e4
LT
561 nfs4_put_delegation(dp);
562}
563
3bd64a5b
BF
564static void destroy_delegation(struct nfs4_delegation *dp)
565{
566 unhash_delegation(dp);
567 remove_stid(&dp->dl_stid);
568 nfs4_put_delegation(dp);
569}
570
571static void revoke_delegation(struct nfs4_delegation *dp)
572{
573 struct nfs4_client *clp = dp->dl_stid.sc_client;
574
575 if (clp->cl_minorversion == 0)
576 destroy_delegation(dp);
577 else {
578 unhash_delegation(dp);
579 dp->dl_stid.sc_type = NFS4_REVOKED_DELEG_STID;
580 list_add(&dp->dl_recall_lru, &clp->cl_revoked);
581 }
582}
583
1da177e4
LT
584/*
585 * SETCLIENTID state
586 */
587
ddc04c41
BF
588static unsigned int clientid_hashval(u32 id)
589{
590 return id & CLIENT_HASH_MASK;
591}
592
593static unsigned int clientstr_hashval(const char *name)
594{
595 return opaque_hashval(name, 8) & CLIENT_HASH_MASK;
596}
597
f9d7562f
BF
598/*
599 * We store the NONE, READ, WRITE, and BOTH bits separately in the
600 * st_{access,deny}_bmap field of the stateid, in order to track not
601 * only what share bits are currently in force, but also what
602 * combinations of share bits previous opens have used. This allows us
603 * to enforce the recommendation of rfc 3530 14.2.19 that the server
604 * return an error if the client attempt to downgrade to a combination
605 * of share bits not explicable by closing some of its previous opens.
606 *
607 * XXX: This enforcement is actually incomplete, since we don't keep
608 * track of access/deny bit combinations; so, e.g., we allow:
609 *
610 * OPEN allow read, deny write
611 * OPEN allow both, deny none
612 * DOWNGRADE allow read, deny none
613 *
614 * which we should reject.
615 */
5ae037e5
JL
616static unsigned int
617bmap_to_share_mode(unsigned long bmap) {
f9d7562f 618 int i;
5ae037e5 619 unsigned int access = 0;
f9d7562f 620
f9d7562f
BF
621 for (i = 1; i < 4; i++) {
622 if (test_bit(i, &bmap))
5ae037e5 623 access |= i;
f9d7562f 624 }
5ae037e5 625 return access;
f9d7562f
BF
626}
627
3a328614 628static bool
dcef0413 629test_share(struct nfs4_ol_stateid *stp, struct nfsd4_open *open) {
f9d7562f
BF
630 unsigned int access, deny;
631
5ae037e5
JL
632 access = bmap_to_share_mode(stp->st_access_bmap);
633 deny = bmap_to_share_mode(stp->st_deny_bmap);
f9d7562f 634 if ((access & open->op_share_deny) || (deny & open->op_share_access))
3a328614
JL
635 return false;
636 return true;
f9d7562f
BF
637}
638
82c5ff1b
JL
639/* set share access for a given stateid */
640static inline void
641set_access(u32 access, struct nfs4_ol_stateid *stp)
642{
643 __set_bit(access, &stp->st_access_bmap);
644}
645
646/* clear share access for a given stateid */
647static inline void
648clear_access(u32 access, struct nfs4_ol_stateid *stp)
649{
650 __clear_bit(access, &stp->st_access_bmap);
651}
652
653/* test whether a given stateid has access */
654static inline bool
655test_access(u32 access, struct nfs4_ol_stateid *stp)
656{
657 return test_bit(access, &stp->st_access_bmap);
658}
659
ce0fc43c
JL
660/* set share deny for a given stateid */
661static inline void
662set_deny(u32 access, struct nfs4_ol_stateid *stp)
663{
664 __set_bit(access, &stp->st_deny_bmap);
665}
666
667/* clear share deny for a given stateid */
668static inline void
669clear_deny(u32 access, struct nfs4_ol_stateid *stp)
670{
671 __clear_bit(access, &stp->st_deny_bmap);
672}
673
674/* test whether a given stateid is denying specific access */
675static inline bool
676test_deny(u32 access, struct nfs4_ol_stateid *stp)
677{
678 return test_bit(access, &stp->st_deny_bmap);
f9d7562f
BF
679}
680
681static int nfs4_access_to_omode(u32 access)
682{
8f34a430 683 switch (access & NFS4_SHARE_ACCESS_BOTH) {
f9d7562f
BF
684 case NFS4_SHARE_ACCESS_READ:
685 return O_RDONLY;
686 case NFS4_SHARE_ACCESS_WRITE:
687 return O_WRONLY;
688 case NFS4_SHARE_ACCESS_BOTH:
689 return O_RDWR;
690 }
063b0fb9
BF
691 WARN_ON_ONCE(1);
692 return O_RDONLY;
f9d7562f
BF
693}
694
82c5ff1b
JL
695/* release all access and file references for a given stateid */
696static void
697release_all_access(struct nfs4_ol_stateid *stp)
698{
699 int i;
700
701 for (i = 1; i < 4; i++) {
702 if (test_access(i, stp))
703 nfs4_file_put_access(stp->st_file,
704 nfs4_access_to_omode(i));
705 clear_access(i, stp);
706 }
707}
708
dcef0413 709static void unhash_generic_stateid(struct nfs4_ol_stateid *stp)
529d7b2a 710{
529d7b2a
BF
711 list_del(&stp->st_perfile);
712 list_del(&stp->st_perstateowner);
713}
714
dcef0413 715static void close_generic_stateid(struct nfs4_ol_stateid *stp)
529d7b2a 716{
82c5ff1b 717 release_all_access(stp);
a96e5b90 718 put_nfs4_file(stp->st_file);
4665e2ba
BF
719 stp->st_file = NULL;
720}
721
dcef0413 722static void free_generic_stateid(struct nfs4_ol_stateid *stp)
4665e2ba 723{
68a33961 724 remove_stid(&stp->st_stid);
9857df81 725 nfs4_free_stid(stateid_slab, &stp->st_stid);
529d7b2a
BF
726}
727
3c87b9b7 728static void __release_lock_stateid(struct nfs4_ol_stateid *stp)
529d7b2a
BF
729{
730 struct file *file;
731
3c87b9b7 732 list_del(&stp->st_locks);
529d7b2a 733 unhash_generic_stateid(stp);
6136d2b4 734 unhash_stid(&stp->st_stid);
529d7b2a
BF
735 file = find_any_file(stp->st_file);
736 if (file)
fe0750e5 737 locks_remove_posix(file, (fl_owner_t)lockowner(stp->st_stateowner));
38c387b5 738 close_generic_stateid(stp);
529d7b2a
BF
739 free_generic_stateid(stp);
740}
741
fe0750e5 742static void unhash_lockowner(struct nfs4_lockowner *lo)
529d7b2a 743{
dcef0413 744 struct nfs4_ol_stateid *stp;
529d7b2a 745
fe0750e5 746 list_del(&lo->lo_owner.so_strhash);
fe0750e5
BF
747 while (!list_empty(&lo->lo_owner.so_stateids)) {
748 stp = list_first_entry(&lo->lo_owner.so_stateids,
dcef0413 749 struct nfs4_ol_stateid, st_perstateowner);
3c87b9b7 750 __release_lock_stateid(stp);
529d7b2a
BF
751 }
752}
753
50cc6231
TM
754static void nfs4_free_lockowner(struct nfs4_lockowner *lo)
755{
756 kfree(lo->lo_owner.so_owner.data);
757 kmem_cache_free(lockowner_slab, lo);
758}
759
fe0750e5 760static void release_lockowner(struct nfs4_lockowner *lo)
529d7b2a 761{
fe0750e5
BF
762 unhash_lockowner(lo);
763 nfs4_free_lockowner(lo);
529d7b2a
BF
764}
765
3c87b9b7
TM
766static void release_lockowner_if_empty(struct nfs4_lockowner *lo)
767{
768 if (list_empty(&lo->lo_owner.so_stateids))
769 release_lockowner(lo);
770}
771
772static void release_lock_stateid(struct nfs4_ol_stateid *stp)
529d7b2a 773{
fe0750e5 774 struct nfs4_lockowner *lo;
529d7b2a 775
3c87b9b7
TM
776 lo = lockowner(stp->st_stateowner);
777 __release_lock_stateid(stp);
778 release_lockowner_if_empty(lo);
779}
780
781static void release_open_stateid_locks(struct nfs4_ol_stateid *open_stp)
782{
783 struct nfs4_ol_stateid *stp;
784
785 while (!list_empty(&open_stp->st_locks)) {
786 stp = list_entry(open_stp->st_locks.next,
787 struct nfs4_ol_stateid, st_locks);
788 release_lock_stateid(stp);
529d7b2a
BF
789 }
790}
791
38c387b5 792static void unhash_open_stateid(struct nfs4_ol_stateid *stp)
2283963f
BF
793{
794 unhash_generic_stateid(stp);
3c87b9b7 795 release_open_stateid_locks(stp);
38c387b5
BF
796 close_generic_stateid(stp);
797}
798
799static void release_open_stateid(struct nfs4_ol_stateid *stp)
800{
801 unhash_open_stateid(stp);
2283963f
BF
802 free_generic_stateid(stp);
803}
804
fe0750e5 805static void unhash_openowner(struct nfs4_openowner *oo)
f1d110ca 806{
dcef0413 807 struct nfs4_ol_stateid *stp;
f1d110ca 808
fe0750e5
BF
809 list_del(&oo->oo_owner.so_strhash);
810 list_del(&oo->oo_perclient);
811 while (!list_empty(&oo->oo_owner.so_stateids)) {
812 stp = list_first_entry(&oo->oo_owner.so_stateids,
dcef0413 813 struct nfs4_ol_stateid, st_perstateowner);
f044ff83 814 release_open_stateid(stp);
f1d110ca
BF
815 }
816}
817
f7a4d872
BF
818static void release_last_closed_stateid(struct nfs4_openowner *oo)
819{
820 struct nfs4_ol_stateid *s = oo->oo_last_closed_stid;
821
822 if (s) {
f7a4d872
BF
823 free_generic_stateid(s);
824 oo->oo_last_closed_stid = NULL;
825 }
826}
827
50cc6231
TM
828static void nfs4_free_openowner(struct nfs4_openowner *oo)
829{
830 kfree(oo->oo_owner.so_owner.data);
831 kmem_cache_free(openowner_slab, oo);
832}
833
fe0750e5 834static void release_openowner(struct nfs4_openowner *oo)
f1d110ca 835{
fe0750e5
BF
836 unhash_openowner(oo);
837 list_del(&oo->oo_close_lru);
f7a4d872 838 release_last_closed_stateid(oo);
fe0750e5 839 nfs4_free_openowner(oo);
f1d110ca
BF
840}
841
5282fd72
ME
842static inline int
843hash_sessionid(struct nfs4_sessionid *sessionid)
844{
845 struct nfsd4_sessionid *sid = (struct nfsd4_sessionid *)sessionid;
846
847 return sid->sequence % SESSION_HASH_SIZE;
848}
849
8f199b82 850#ifdef NFSD_DEBUG
5282fd72
ME
851static inline void
852dump_sessionid(const char *fn, struct nfs4_sessionid *sessionid)
853{
854 u32 *ptr = (u32 *)(&sessionid->data[0]);
855 dprintk("%s: %u:%u:%u:%u\n", fn, ptr[0], ptr[1], ptr[2], ptr[3]);
856}
8f199b82
TM
857#else
858static inline void
859dump_sessionid(const char *fn, struct nfs4_sessionid *sessionid)
860{
861}
862#endif
863
9411b1d4
BF
864/*
865 * Bump the seqid on cstate->replay_owner, and clear replay_owner if it
866 * won't be used for replay.
867 */
868void nfsd4_bump_seqid(struct nfsd4_compound_state *cstate, __be32 nfserr)
869{
870 struct nfs4_stateowner *so = cstate->replay_owner;
871
872 if (nfserr == nfserr_replay_me)
873 return;
874
875 if (!seqid_mutating_err(ntohl(nfserr))) {
876 cstate->replay_owner = NULL;
877 return;
878 }
879 if (!so)
880 return;
881 if (so->so_is_open_owner)
882 release_last_closed_stateid(openowner(so));
883 so->so_seqid++;
884 return;
885}
5282fd72 886
ec6b5d7b
AA
887static void
888gen_sessionid(struct nfsd4_session *ses)
889{
890 struct nfs4_client *clp = ses->se_client;
891 struct nfsd4_sessionid *sid;
892
893 sid = (struct nfsd4_sessionid *)ses->se_sessionid.data;
894 sid->clientid = clp->cl_clientid;
895 sid->sequence = current_sessionid++;
896 sid->reserved = 0;
897}
898
899/*
a649637c
AA
900 * The protocol defines ca_maxresponssize_cached to include the size of
901 * the rpc header, but all we need to cache is the data starting after
902 * the end of the initial SEQUENCE operation--the rest we regenerate
903 * each time. Therefore we can advertise a ca_maxresponssize_cached
904 * value that is the number of bytes in our cache plus a few additional
905 * bytes. In order to stay on the safe side, and not promise more than
906 * we can cache, those additional bytes must be the minimum possible: 24
907 * bytes of rpc header (xid through accept state, with AUTH_NULL
908 * verifier), 12 for the compound header (with zero-length tag), and 44
909 * for the SEQUENCE op response:
910 */
911#define NFSD_MIN_HDR_SEQ_SZ (24 + 12 + 44)
912
557ce264
AA
913static void
914free_session_slots(struct nfsd4_session *ses)
915{
916 int i;
917
918 for (i = 0; i < ses->se_fchannel.maxreqs; i++)
919 kfree(ses->se_slots[i]);
920}
921
a649637c 922/*
efe0cb6d
BF
923 * We don't actually need to cache the rpc and session headers, so we
924 * can allocate a little less for each slot:
925 */
55c760cf 926static inline u32 slot_bytes(struct nfsd4_channel_attrs *ca)
efe0cb6d 927{
55c760cf 928 u32 size;
efe0cb6d 929
55c760cf
BF
930 if (ca->maxresp_cached < NFSD_MIN_HDR_SEQ_SZ)
931 size = 0;
932 else
933 size = ca->maxresp_cached - NFSD_MIN_HDR_SEQ_SZ;
934 return size + sizeof(struct nfsd4_slot);
5b6feee9 935}
ec6b5d7b 936
5b6feee9
BF
937/*
938 * XXX: If we run out of reserved DRC memory we could (up to a point)
a649637c 939 * re-negotiate active sessions and reduce their slot usage to make
42b2aa86 940 * room for new connections. For now we just fail the create session.
ec6b5d7b 941 */
55c760cf 942static u32 nfsd4_get_drc_mem(struct nfsd4_channel_attrs *ca)
ec6b5d7b 943{
55c760cf
BF
944 u32 slotsize = slot_bytes(ca);
945 u32 num = ca->maxreqs;
5b6feee9 946 int avail;
ec6b5d7b 947
5b6feee9 948 spin_lock(&nfsd_drc_lock);
697ce9be
ZY
949 avail = min((unsigned long)NFSD_MAX_MEM_PER_SESSION,
950 nfsd_drc_max_mem - nfsd_drc_mem_used);
5b6feee9
BF
951 num = min_t(int, num, avail / slotsize);
952 nfsd_drc_mem_used += num * slotsize;
953 spin_unlock(&nfsd_drc_lock);
ec6b5d7b 954
5b6feee9
BF
955 return num;
956}
ec6b5d7b 957
55c760cf 958static void nfsd4_put_drc_mem(struct nfsd4_channel_attrs *ca)
5b6feee9 959{
55c760cf
BF
960 int slotsize = slot_bytes(ca);
961
4bd9b0f4 962 spin_lock(&nfsd_drc_lock);
55c760cf 963 nfsd_drc_mem_used -= slotsize * ca->maxreqs;
4bd9b0f4 964 spin_unlock(&nfsd_drc_lock);
5b6feee9 965}
ec6b5d7b 966
60810e54
KM
967static struct nfsd4_session *alloc_session(struct nfsd4_channel_attrs *fattrs,
968 struct nfsd4_channel_attrs *battrs)
5b6feee9 969{
60810e54
KM
970 int numslots = fattrs->maxreqs;
971 int slotsize = slot_bytes(fattrs);
5b6feee9
BF
972 struct nfsd4_session *new;
973 int mem, i;
a649637c 974
5b6feee9
BF
975 BUILD_BUG_ON(NFSD_MAX_SLOTS_PER_SESSION * sizeof(struct nfsd4_slot *)
976 + sizeof(struct nfsd4_session) > PAGE_SIZE);
977 mem = numslots * sizeof(struct nfsd4_slot *);
ec6b5d7b 978
5b6feee9
BF
979 new = kzalloc(sizeof(*new) + mem, GFP_KERNEL);
980 if (!new)
981 return NULL;
557ce264 982 /* allocate each struct nfsd4_slot and data cache in one piece */
5b6feee9 983 for (i = 0; i < numslots; i++) {
55c760cf 984 new->se_slots[i] = kzalloc(slotsize, GFP_KERNEL);
5b6feee9 985 if (!new->se_slots[i])
557ce264 986 goto out_free;
557ce264 987 }
60810e54
KM
988
989 memcpy(&new->se_fchannel, fattrs, sizeof(struct nfsd4_channel_attrs));
990 memcpy(&new->se_bchannel, battrs, sizeof(struct nfsd4_channel_attrs));
991
5b6feee9
BF
992 return new;
993out_free:
994 while (i--)
995 kfree(new->se_slots[i]);
996 kfree(new);
997 return NULL;
ec6b5d7b
AA
998}
999
19cf5c02
BF
1000static void free_conn(struct nfsd4_conn *c)
1001{
1002 svc_xprt_put(c->cn_xprt);
1003 kfree(c);
1004}
ec6b5d7b 1005
19cf5c02
BF
1006static void nfsd4_conn_lost(struct svc_xpt_user *u)
1007{
1008 struct nfsd4_conn *c = container_of(u, struct nfsd4_conn, cn_xpt_user);
1009 struct nfs4_client *clp = c->cn_session->se_client;
ec6b5d7b 1010
19cf5c02
BF
1011 spin_lock(&clp->cl_lock);
1012 if (!list_empty(&c->cn_persession)) {
1013 list_del(&c->cn_persession);
1014 free_conn(c);
1015 }
eea49806 1016 nfsd4_probe_callback(clp);
2e4b7239 1017 spin_unlock(&clp->cl_lock);
19cf5c02 1018}
ec6b5d7b 1019
d29c374c 1020static struct nfsd4_conn *alloc_conn(struct svc_rqst *rqstp, u32 flags)
c7662518 1021{
c7662518 1022 struct nfsd4_conn *conn;
ec6b5d7b 1023
c7662518
BF
1024 conn = kmalloc(sizeof(struct nfsd4_conn), GFP_KERNEL);
1025 if (!conn)
db90681d 1026 return NULL;
c7662518
BF
1027 svc_xprt_get(rqstp->rq_xprt);
1028 conn->cn_xprt = rqstp->rq_xprt;
d29c374c 1029 conn->cn_flags = flags;
db90681d
BF
1030 INIT_LIST_HEAD(&conn->cn_xpt_user.list);
1031 return conn;
1032}
a649637c 1033
328ead28
BF
1034static void __nfsd4_hash_conn(struct nfsd4_conn *conn, struct nfsd4_session *ses)
1035{
1036 conn->cn_session = ses;
1037 list_add(&conn->cn_persession, &ses->se_conns);
ec6b5d7b
AA
1038}
1039
db90681d 1040static void nfsd4_hash_conn(struct nfsd4_conn *conn, struct nfsd4_session *ses)
557ce264 1041{
db90681d 1042 struct nfs4_client *clp = ses->se_client;
557ce264 1043
c7662518 1044 spin_lock(&clp->cl_lock);
328ead28 1045 __nfsd4_hash_conn(conn, ses);
c7662518 1046 spin_unlock(&clp->cl_lock);
557ce264
AA
1047}
1048
21b75b01 1049static int nfsd4_register_conn(struct nfsd4_conn *conn)
efe0cb6d 1050{
19cf5c02 1051 conn->cn_xpt_user.callback = nfsd4_conn_lost;
21b75b01 1052 return register_xpt_user(conn->cn_xprt, &conn->cn_xpt_user);
efe0cb6d
BF
1053}
1054
e1ff371f 1055static void nfsd4_init_conn(struct svc_rqst *rqstp, struct nfsd4_conn *conn, struct nfsd4_session *ses)
ec6b5d7b 1056{
21b75b01 1057 int ret;
ec6b5d7b 1058
db90681d 1059 nfsd4_hash_conn(conn, ses);
21b75b01
BF
1060 ret = nfsd4_register_conn(conn);
1061 if (ret)
1062 /* oops; xprt is already down: */
1063 nfsd4_conn_lost(&conn->cn_xpt_user);
6a3b1563 1064 if (conn->cn_flags & NFS4_CDFC4_BACK) {
24119673
WAA
1065 /* callback channel may be back up */
1066 nfsd4_probe_callback(ses->se_client);
1067 }
c7662518 1068}
ec6b5d7b 1069
e1ff371f 1070static struct nfsd4_conn *alloc_conn_from_crses(struct svc_rqst *rqstp, struct nfsd4_create_session *cses)
1d1bc8f2
BF
1071{
1072 u32 dir = NFS4_CDFC4_FORE;
1073
e1ff371f 1074 if (cses->flags & SESSION4_BACK_CHAN)
1d1bc8f2 1075 dir |= NFS4_CDFC4_BACK;
e1ff371f 1076 return alloc_conn(rqstp, dir);
1d1bc8f2
BF
1077}
1078
1079/* must be called under client_lock */
19cf5c02 1080static void nfsd4_del_conns(struct nfsd4_session *s)
c7662518 1081{
19cf5c02
BF
1082 struct nfs4_client *clp = s->se_client;
1083 struct nfsd4_conn *c;
ec6b5d7b 1084
19cf5c02
BF
1085 spin_lock(&clp->cl_lock);
1086 while (!list_empty(&s->se_conns)) {
1087 c = list_first_entry(&s->se_conns, struct nfsd4_conn, cn_persession);
1088 list_del_init(&c->cn_persession);
1089 spin_unlock(&clp->cl_lock);
557ce264 1090
19cf5c02
BF
1091 unregister_xpt_user(c->cn_xprt, &c->cn_xpt_user);
1092 free_conn(c);
ec6b5d7b 1093
19cf5c02
BF
1094 spin_lock(&clp->cl_lock);
1095 }
1096 spin_unlock(&clp->cl_lock);
c7662518 1097}
ec6b5d7b 1098
1377b69e
BF
1099static void __free_session(struct nfsd4_session *ses)
1100{
1377b69e
BF
1101 free_session_slots(ses);
1102 kfree(ses);
1103}
1104
66b2b9b2 1105static void free_session(struct nfsd4_session *ses)
c7662518 1106{
66b2b9b2 1107 struct nfsd_net *nn = net_generic(ses->se_client->net, nfsd_net_id);
c9a49628
SK
1108
1109 lockdep_assert_held(&nn->client_lock);
19cf5c02 1110 nfsd4_del_conns(ses);
55c760cf 1111 nfsd4_put_drc_mem(&ses->se_fchannel);
1377b69e 1112 __free_session(ses);
c7662518
BF
1113}
1114
135ae827 1115static void init_session(struct svc_rqst *rqstp, struct nfsd4_session *new, struct nfs4_client *clp, struct nfsd4_create_session *cses)
a827bcb2 1116{
a827bcb2 1117 int idx;
1872de0e 1118 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
a827bcb2 1119
ec6b5d7b
AA
1120 new->se_client = clp;
1121 gen_sessionid(new);
ec6b5d7b 1122
c7662518
BF
1123 INIT_LIST_HEAD(&new->se_conns);
1124
ac7c46f2 1125 new->se_cb_seq_nr = 1;
ec6b5d7b 1126 new->se_flags = cses->flags;
8b5ce5cd 1127 new->se_cb_prog = cses->callback_prog;
c6bb3ca2 1128 new->se_cb_sec = cses->cb_sec;
66b2b9b2 1129 atomic_set(&new->se_ref, 0);
5b6feee9 1130 idx = hash_sessionid(&new->se_sessionid);
c9a49628 1131 spin_lock(&nn->client_lock);
1872de0e 1132 list_add(&new->se_hash, &nn->sessionid_hashtbl[idx]);
4c649378 1133 spin_lock(&clp->cl_lock);
ec6b5d7b 1134 list_add(&new->se_perclnt, &clp->cl_sessions);
4c649378 1135 spin_unlock(&clp->cl_lock);
c9a49628 1136 spin_unlock(&nn->client_lock);
60810e54 1137
dcbeaa68 1138 if (cses->flags & SESSION4_BACK_CHAN) {
edd76786 1139 struct sockaddr *sa = svc_addr(rqstp);
dcbeaa68
BF
1140 /*
1141 * This is a little silly; with sessions there's no real
1142 * use for the callback address. Use the peer address
1143 * as a reasonable default for now, but consider fixing
1144 * the rpc client not to require an address in the
1145 * future:
1146 */
edd76786
BF
1147 rpc_copy_addr((struct sockaddr *)&clp->cl_cb_conn.cb_addr, sa);
1148 clp->cl_cb_conn.cb_addrlen = svc_addr_len(sa);
edd76786 1149 }
ec6b5d7b
AA
1150}
1151
9089f1b4 1152/* caller must hold client_lock */
5282fd72 1153static struct nfsd4_session *
d4e19e70 1154__find_in_sessionid_hashtbl(struct nfs4_sessionid *sessionid, struct net *net)
5282fd72
ME
1155{
1156 struct nfsd4_session *elem;
1157 int idx;
1872de0e 1158 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
5282fd72
ME
1159
1160 dump_sessionid(__func__, sessionid);
1161 idx = hash_sessionid(sessionid);
5282fd72 1162 /* Search in the appropriate list */
1872de0e 1163 list_for_each_entry(elem, &nn->sessionid_hashtbl[idx], se_hash) {
5282fd72
ME
1164 if (!memcmp(elem->se_sessionid.data, sessionid->data,
1165 NFS4_MAX_SESSIONID_LEN)) {
1166 return elem;
1167 }
1168 }
1169
1170 dprintk("%s: session not found\n", __func__);
1171 return NULL;
1172}
1173
d4e19e70
TM
1174static struct nfsd4_session *
1175find_in_sessionid_hashtbl(struct nfs4_sessionid *sessionid, struct net *net,
1176 __be32 *ret)
1177{
1178 struct nfsd4_session *session;
1179 __be32 status = nfserr_badsession;
1180
1181 session = __find_in_sessionid_hashtbl(sessionid, net);
1182 if (!session)
1183 goto out;
1184 status = nfsd4_get_session_locked(session);
1185 if (status)
1186 session = NULL;
1187out:
1188 *ret = status;
1189 return session;
1190}
1191
9089f1b4 1192/* caller must hold client_lock */
7116ed6b 1193static void
5282fd72 1194unhash_session(struct nfsd4_session *ses)
7116ed6b
AA
1195{
1196 list_del(&ses->se_hash);
4c649378 1197 spin_lock(&ses->se_client->cl_lock);
7116ed6b 1198 list_del(&ses->se_perclnt);
4c649378 1199 spin_unlock(&ses->se_client->cl_lock);
5282fd72
ME
1200}
1201
1da177e4
LT
1202/* SETCLIENTID and SETCLIENTID_CONFIRM Helper functions */
1203static int
2c142baa 1204STALE_CLIENTID(clientid_t *clid, struct nfsd_net *nn)
1da177e4 1205{
2c142baa 1206 if (clid->cl_boot == nn->boot_time)
1da177e4 1207 return 0;
60adfc50 1208 dprintk("NFSD stale clientid (%08x/%08x) boot_time %08lx\n",
2c142baa 1209 clid->cl_boot, clid->cl_id, nn->boot_time);
1da177e4
LT
1210 return 1;
1211}
1212
1213/*
1214 * XXX Should we use a slab cache ?
1215 * This type of memory management is somewhat inefficient, but we use it
1216 * anyway since SETCLIENTID is not a common operation.
1217 */
35bba9a3 1218static struct nfs4_client *alloc_client(struct xdr_netobj name)
1da177e4
LT
1219{
1220 struct nfs4_client *clp;
1221
35bba9a3
BF
1222 clp = kzalloc(sizeof(struct nfs4_client), GFP_KERNEL);
1223 if (clp == NULL)
1224 return NULL;
67114fe6 1225 clp->cl_name.data = kmemdup(name.data, name.len, GFP_KERNEL);
35bba9a3
BF
1226 if (clp->cl_name.data == NULL) {
1227 kfree(clp);
1228 return NULL;
1da177e4 1229 }
35bba9a3 1230 clp->cl_name.len = name.len;
5694c93e
TM
1231 INIT_LIST_HEAD(&clp->cl_sessions);
1232 idr_init(&clp->cl_stateids);
1233 atomic_set(&clp->cl_refcount, 0);
1234 clp->cl_cb_state = NFSD4_CB_UNKNOWN;
1235 INIT_LIST_HEAD(&clp->cl_idhash);
1236 INIT_LIST_HEAD(&clp->cl_openowners);
1237 INIT_LIST_HEAD(&clp->cl_delegations);
1238 INIT_LIST_HEAD(&clp->cl_lru);
1239 INIT_LIST_HEAD(&clp->cl_callbacks);
1240 INIT_LIST_HEAD(&clp->cl_revoked);
1241 spin_lock_init(&clp->cl_lock);
1242 rpc_init_wait_queue(&clp->cl_cb_waitq, "Backchannel slot table");
1da177e4
LT
1243 return clp;
1244}
1245
4dd86e15 1246static void
1da177e4
LT
1247free_client(struct nfs4_client *clp)
1248{
bca0ec65 1249 struct nfsd_net __maybe_unused *nn = net_generic(clp->net, nfsd_net_id);
c9a49628
SK
1250
1251 lockdep_assert_held(&nn->client_lock);
792c95dd
BF
1252 while (!list_empty(&clp->cl_sessions)) {
1253 struct nfsd4_session *ses;
1254 ses = list_entry(clp->cl_sessions.next, struct nfsd4_session,
1255 se_perclnt);
1256 list_del(&ses->se_perclnt);
66b2b9b2
BF
1257 WARN_ON_ONCE(atomic_read(&ses->se_ref));
1258 free_session(ses);
792c95dd 1259 }
4cb57e30 1260 rpc_destroy_wait_queue(&clp->cl_cb_waitq);
03a4e1f6 1261 free_svc_cred(&clp->cl_cred);
1da177e4 1262 kfree(clp->cl_name.data);
2d32b29a 1263 idr_destroy(&clp->cl_stateids);
1da177e4
LT
1264 kfree(clp);
1265}
1266
84d38ac9
BH
1267/* must be called under the client_lock */
1268static inline void
1269unhash_client_locked(struct nfs4_client *clp)
1270{
792c95dd
BF
1271 struct nfsd4_session *ses;
1272
84d38ac9 1273 list_del(&clp->cl_lru);
4c649378 1274 spin_lock(&clp->cl_lock);
792c95dd
BF
1275 list_for_each_entry(ses, &clp->cl_sessions, se_perclnt)
1276 list_del_init(&ses->se_hash);
4c649378 1277 spin_unlock(&clp->cl_lock);
84d38ac9
BH
1278}
1279
1da177e4 1280static void
0d22f68f 1281destroy_client(struct nfs4_client *clp)
1da177e4 1282{
fe0750e5 1283 struct nfs4_openowner *oo;
1da177e4 1284 struct nfs4_delegation *dp;
1da177e4 1285 struct list_head reaplist;
382a62e7 1286 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
1da177e4 1287
1da177e4 1288 INIT_LIST_HEAD(&reaplist);
cdc97505 1289 spin_lock(&state_lock);
ea1da636
N
1290 while (!list_empty(&clp->cl_delegations)) {
1291 dp = list_entry(clp->cl_delegations.next, struct nfs4_delegation, dl_perclnt);
ea1da636 1292 list_del_init(&dp->dl_perclnt);
dff1399f
JL
1293 /* Ensure that deleg break won't try to requeue it */
1294 ++dp->dl_time;
1da177e4
LT
1295 list_move(&dp->dl_recall_lru, &reaplist);
1296 }
cdc97505 1297 spin_unlock(&state_lock);
1da177e4
LT
1298 while (!list_empty(&reaplist)) {
1299 dp = list_entry(reaplist.next, struct nfs4_delegation, dl_recall_lru);
3bd64a5b 1300 destroy_delegation(dp);
1da177e4 1301 }
956c4fee
BH
1302 list_splice_init(&clp->cl_revoked, &reaplist);
1303 while (!list_empty(&reaplist)) {
1304 dp = list_entry(reaplist.next, struct nfs4_delegation, dl_recall_lru);
1305 destroy_revoked_delegation(dp);
1306 }
ea1da636 1307 while (!list_empty(&clp->cl_openowners)) {
fe0750e5
BF
1308 oo = list_entry(clp->cl_openowners.next, struct nfs4_openowner, oo_perclient);
1309 release_openowner(oo);
1da177e4 1310 }
6ff8da08 1311 nfsd4_shutdown_callback(clp);
84d38ac9
BH
1312 if (clp->cl_cb_conn.cb_xprt)
1313 svc_xprt_put(clp->cl_cb_conn.cb_xprt);
36acb66b 1314 list_del(&clp->cl_idhash);
ac55fdc4 1315 if (test_bit(NFSD4_CLIENT_CONFIRMED, &clp->cl_flags))
382a62e7 1316 rb_erase(&clp->cl_namenode, &nn->conf_name_tree);
ac55fdc4 1317 else
a99454aa 1318 rb_erase(&clp->cl_namenode, &nn->unconf_name_tree);
c9a49628 1319 spin_lock(&nn->client_lock);
84d38ac9 1320 unhash_client_locked(clp);
221a6876
BF
1321 WARN_ON_ONCE(atomic_read(&clp->cl_refcount));
1322 free_client(clp);
c9a49628 1323 spin_unlock(&nn->client_lock);
1da177e4
LT
1324}
1325
0d22f68f
BF
1326static void expire_client(struct nfs4_client *clp)
1327{
1328 nfsd4_client_record_remove(clp);
1329 destroy_client(clp);
1330}
1331
35bba9a3
BF
1332static void copy_verf(struct nfs4_client *target, nfs4_verifier *source)
1333{
1334 memcpy(target->cl_verifier.data, source->data,
1335 sizeof(target->cl_verifier.data));
1da177e4
LT
1336}
1337
35bba9a3
BF
1338static void copy_clid(struct nfs4_client *target, struct nfs4_client *source)
1339{
1da177e4
LT
1340 target->cl_clientid.cl_boot = source->cl_clientid.cl_boot;
1341 target->cl_clientid.cl_id = source->cl_clientid.cl_id;
1342}
1343
03a4e1f6 1344static int copy_cred(struct svc_cred *target, struct svc_cred *source)
35bba9a3 1345{
03a4e1f6
BF
1346 if (source->cr_principal) {
1347 target->cr_principal =
1348 kstrdup(source->cr_principal, GFP_KERNEL);
1349 if (target->cr_principal == NULL)
1350 return -ENOMEM;
1351 } else
1352 target->cr_principal = NULL;
d5497fc6 1353 target->cr_flavor = source->cr_flavor;
1da177e4
LT
1354 target->cr_uid = source->cr_uid;
1355 target->cr_gid = source->cr_gid;
1356 target->cr_group_info = source->cr_group_info;
1357 get_group_info(target->cr_group_info);
0dc1531a
BF
1358 target->cr_gss_mech = source->cr_gss_mech;
1359 if (source->cr_gss_mech)
1360 gss_mech_get(source->cr_gss_mech);
03a4e1f6 1361 return 0;
1da177e4
LT
1362}
1363
ac55fdc4
JL
1364static long long
1365compare_blob(const struct xdr_netobj *o1, const struct xdr_netobj *o2)
1366{
1367 long long res;
1368
1369 res = o1->len - o2->len;
1370 if (res)
1371 return res;
1372 return (long long)memcmp(o1->data, o2->data, o1->len);
1373}
1374
35bba9a3 1375static int same_name(const char *n1, const char *n2)
599e0a22 1376{
a55370a3 1377 return 0 == memcmp(n1, n2, HEXDIR_LEN);
1da177e4
LT
1378}
1379
1380static int
599e0a22
BF
1381same_verf(nfs4_verifier *v1, nfs4_verifier *v2)
1382{
1383 return 0 == memcmp(v1->data, v2->data, sizeof(v1->data));
1da177e4
LT
1384}
1385
1386static int
599e0a22
BF
1387same_clid(clientid_t *cl1, clientid_t *cl2)
1388{
1389 return (cl1->cl_boot == cl2->cl_boot) && (cl1->cl_id == cl2->cl_id);
1da177e4
LT
1390}
1391
8fbba96e
BF
1392static bool groups_equal(struct group_info *g1, struct group_info *g2)
1393{
1394 int i;
1395
1396 if (g1->ngroups != g2->ngroups)
1397 return false;
1398 for (i=0; i<g1->ngroups; i++)
6fab8779 1399 if (!gid_eq(GROUP_AT(g1, i), GROUP_AT(g2, i)))
8fbba96e
BF
1400 return false;
1401 return true;
1402}
1403
68eb3508
BF
1404/*
1405 * RFC 3530 language requires clid_inuse be returned when the
1406 * "principal" associated with a requests differs from that previously
1407 * used. We use uid, gid's, and gss principal string as our best
1408 * approximation. We also don't want to allow non-gss use of a client
1409 * established using gss: in theory cr_principal should catch that
1410 * change, but in practice cr_principal can be null even in the gss case
1411 * since gssd doesn't always pass down a principal string.
1412 */
1413static bool is_gss_cred(struct svc_cred *cr)
1414{
1415 /* Is cr_flavor one of the gss "pseudoflavors"?: */
1416 return (cr->cr_flavor > RPC_AUTH_MAXFLAVOR);
1417}
1418
1419
5559b50a 1420static bool
599e0a22
BF
1421same_creds(struct svc_cred *cr1, struct svc_cred *cr2)
1422{
68eb3508 1423 if ((is_gss_cred(cr1) != is_gss_cred(cr2))
6fab8779
EB
1424 || (!uid_eq(cr1->cr_uid, cr2->cr_uid))
1425 || (!gid_eq(cr1->cr_gid, cr2->cr_gid))
8fbba96e
BF
1426 || !groups_equal(cr1->cr_group_info, cr2->cr_group_info))
1427 return false;
1428 if (cr1->cr_principal == cr2->cr_principal)
1429 return true;
1430 if (!cr1->cr_principal || !cr2->cr_principal)
1431 return false;
5559b50a 1432 return 0 == strcmp(cr1->cr_principal, cr2->cr_principal);
1da177e4
LT
1433}
1434
57266a6e
BF
1435static bool svc_rqst_integrity_protected(struct svc_rqst *rqstp)
1436{
1437 struct svc_cred *cr = &rqstp->rq_cred;
1438 u32 service;
1439
c4720591
BF
1440 if (!cr->cr_gss_mech)
1441 return false;
57266a6e
BF
1442 service = gss_pseudoflavor_to_service(cr->cr_gss_mech, cr->cr_flavor);
1443 return service == RPC_GSS_SVC_INTEGRITY ||
1444 service == RPC_GSS_SVC_PRIVACY;
1445}
1446
1447static bool mach_creds_match(struct nfs4_client *cl, struct svc_rqst *rqstp)
1448{
1449 struct svc_cred *cr = &rqstp->rq_cred;
1450
1451 if (!cl->cl_mach_cred)
1452 return true;
1453 if (cl->cl_cred.cr_gss_mech != cr->cr_gss_mech)
1454 return false;
1455 if (!svc_rqst_integrity_protected(rqstp))
1456 return false;
1457 if (!cr->cr_principal)
1458 return false;
1459 return 0 == strcmp(cl->cl_cred.cr_principal, cr->cr_principal);
1460}
1461
c212cecf 1462static void gen_clid(struct nfs4_client *clp, struct nfsd_net *nn)
5ec7b46c
BF
1463{
1464 static u32 current_clientid = 1;
1465
2c142baa 1466 clp->cl_clientid.cl_boot = nn->boot_time;
1da177e4
LT
1467 clp->cl_clientid.cl_id = current_clientid++;
1468}
1469
deda2faa
BF
1470static void gen_confirm(struct nfs4_client *clp)
1471{
ab4684d1 1472 __be32 verf[2];
deda2faa 1473 static u32 i;
1da177e4 1474
f419992c
JL
1475 /*
1476 * This is opaque to client, so no need to byte-swap. Use
1477 * __force to keep sparse happy
1478 */
1479 verf[0] = (__force __be32)get_seconds();
1480 verf[1] = (__force __be32)i++;
ab4684d1 1481 memcpy(clp->cl_confirm.data, verf, sizeof(clp->cl_confirm.data));
1da177e4
LT
1482}
1483
38c2f4b1 1484static struct nfs4_stid *find_stateid(struct nfs4_client *cl, stateid_t *t)
4581d140 1485{
3abdb607
BF
1486 struct nfs4_stid *ret;
1487
1488 ret = idr_find(&cl->cl_stateids, t->si_opaque.so_id);
1489 if (!ret || !ret->sc_type)
1490 return NULL;
1491 return ret;
4d71ab87
BF
1492}
1493
38c2f4b1 1494static struct nfs4_stid *find_stateid_by_type(struct nfs4_client *cl, stateid_t *t, char typemask)
f459e453
BF
1495{
1496 struct nfs4_stid *s;
4d71ab87 1497
38c2f4b1 1498 s = find_stateid(cl, t);
4d71ab87
BF
1499 if (!s)
1500 return NULL;
f459e453 1501 if (typemask & s->sc_type)
4581d140 1502 return s;
4581d140
BF
1503 return NULL;
1504}
1505
2216d449 1506static struct nfs4_client *create_client(struct xdr_netobj name,
b09333c4
RL
1507 struct svc_rqst *rqstp, nfs4_verifier *verf)
1508{
1509 struct nfs4_client *clp;
1510 struct sockaddr *sa = svc_addr(rqstp);
03a4e1f6 1511 int ret;
c212cecf 1512 struct net *net = SVC_NET(rqstp);
c9a49628 1513 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
b09333c4
RL
1514
1515 clp = alloc_client(name);
1516 if (clp == NULL)
1517 return NULL;
1518
03a4e1f6
BF
1519 ret = copy_cred(&clp->cl_cred, &rqstp->rq_cred);
1520 if (ret) {
c9a49628 1521 spin_lock(&nn->client_lock);
03a4e1f6 1522 free_client(clp);
c9a49628 1523 spin_unlock(&nn->client_lock);
03a4e1f6 1524 return NULL;
b09333c4 1525 }
57725155 1526 nfsd4_init_callback(&clp->cl_cb_null);
07cd4909 1527 clp->cl_time = get_seconds();
b09333c4 1528 clear_bit(0, &clp->cl_cb_slot_busy);
b09333c4
RL
1529 copy_verf(clp, verf);
1530 rpc_copy_addr((struct sockaddr *) &clp->cl_addr, sa);
b09333c4 1531 gen_confirm(clp);
edd76786 1532 clp->cl_cb_session = NULL;
c212cecf 1533 clp->net = net;
b09333c4
RL
1534 return clp;
1535}
1536
fd39ca9a 1537static void
ac55fdc4
JL
1538add_clp_to_name_tree(struct nfs4_client *new_clp, struct rb_root *root)
1539{
1540 struct rb_node **new = &(root->rb_node), *parent = NULL;
1541 struct nfs4_client *clp;
1542
1543 while (*new) {
1544 clp = rb_entry(*new, struct nfs4_client, cl_namenode);
1545 parent = *new;
1546
1547 if (compare_blob(&clp->cl_name, &new_clp->cl_name) > 0)
1548 new = &((*new)->rb_left);
1549 else
1550 new = &((*new)->rb_right);
1551 }
1552
1553 rb_link_node(&new_clp->cl_namenode, parent, new);
1554 rb_insert_color(&new_clp->cl_namenode, root);
1555}
1556
1557static struct nfs4_client *
1558find_clp_in_name_tree(struct xdr_netobj *name, struct rb_root *root)
1559{
1560 long long cmp;
1561 struct rb_node *node = root->rb_node;
1562 struct nfs4_client *clp;
1563
1564 while (node) {
1565 clp = rb_entry(node, struct nfs4_client, cl_namenode);
1566 cmp = compare_blob(&clp->cl_name, name);
1567 if (cmp > 0)
1568 node = node->rb_left;
1569 else if (cmp < 0)
1570 node = node->rb_right;
1571 else
1572 return clp;
1573 }
1574 return NULL;
1575}
1576
1577static void
1578add_to_unconfirmed(struct nfs4_client *clp)
1da177e4
LT
1579{
1580 unsigned int idhashval;
0a7ec377 1581 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
1da177e4 1582
ac55fdc4 1583 clear_bit(NFSD4_CLIENT_CONFIRMED, &clp->cl_flags);
a99454aa 1584 add_clp_to_name_tree(clp, &nn->unconf_name_tree);
1da177e4 1585 idhashval = clientid_hashval(clp->cl_clientid.cl_id);
0a7ec377 1586 list_add(&clp->cl_idhash, &nn->unconf_id_hashtbl[idhashval]);
36acb66b 1587 renew_client(clp);
1da177e4
LT
1588}
1589
fd39ca9a 1590static void
1da177e4
LT
1591move_to_confirmed(struct nfs4_client *clp)
1592{
1593 unsigned int idhashval = clientid_hashval(clp->cl_clientid.cl_id);
8daae4dc 1594 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
1da177e4
LT
1595
1596 dprintk("NFSD: move_to_confirm nfs4_client %p\n", clp);
8daae4dc 1597 list_move(&clp->cl_idhash, &nn->conf_id_hashtbl[idhashval]);
a99454aa 1598 rb_erase(&clp->cl_namenode, &nn->unconf_name_tree);
382a62e7 1599 add_clp_to_name_tree(clp, &nn->conf_name_tree);
ac55fdc4 1600 set_bit(NFSD4_CLIENT_CONFIRMED, &clp->cl_flags);
1da177e4
LT
1601 renew_client(clp);
1602}
1603
1604static struct nfs4_client *
bfa85e83 1605find_client_in_id_table(struct list_head *tbl, clientid_t *clid, bool sessions)
1da177e4
LT
1606{
1607 struct nfs4_client *clp;
1608 unsigned int idhashval = clientid_hashval(clid->cl_id);
1609
bfa85e83 1610 list_for_each_entry(clp, &tbl[idhashval], cl_idhash) {
a50d2ad1 1611 if (same_clid(&clp->cl_clientid, clid)) {
d15c077e
BF
1612 if ((bool)clp->cl_minorversion != sessions)
1613 return NULL;
a50d2ad1 1614 renew_client(clp);
1da177e4 1615 return clp;
a50d2ad1 1616 }
1da177e4
LT
1617 }
1618 return NULL;
1619}
1620
bfa85e83
BF
1621static struct nfs4_client *
1622find_confirmed_client(clientid_t *clid, bool sessions, struct nfsd_net *nn)
1623{
1624 struct list_head *tbl = nn->conf_id_hashtbl;
1625
1626 return find_client_in_id_table(tbl, clid, sessions);
1627}
1628
1da177e4 1629static struct nfs4_client *
0a7ec377 1630find_unconfirmed_client(clientid_t *clid, bool sessions, struct nfsd_net *nn)
1da177e4 1631{
bfa85e83 1632 struct list_head *tbl = nn->unconf_id_hashtbl;
1da177e4 1633
bfa85e83 1634 return find_client_in_id_table(tbl, clid, sessions);
1da177e4
LT
1635}
1636
6e5f15c9 1637static bool clp_used_exchangeid(struct nfs4_client *clp)
a1bcecd2 1638{
6e5f15c9 1639 return clp->cl_exchange_flags != 0;
e203d506 1640}
a1bcecd2 1641
28ce6054 1642static struct nfs4_client *
382a62e7 1643find_confirmed_client_by_name(struct xdr_netobj *name, struct nfsd_net *nn)
28ce6054 1644{
382a62e7 1645 return find_clp_in_name_tree(name, &nn->conf_name_tree);
28ce6054
N
1646}
1647
1648static struct nfs4_client *
a99454aa 1649find_unconfirmed_client_by_name(struct xdr_netobj *name, struct nfsd_net *nn)
28ce6054 1650{
a99454aa 1651 return find_clp_in_name_tree(name, &nn->unconf_name_tree);
28ce6054
N
1652}
1653
fd39ca9a 1654static void
6f3d772f 1655gen_callback(struct nfs4_client *clp, struct nfsd4_setclientid *se, struct svc_rqst *rqstp)
1da177e4 1656{
07263f1e 1657 struct nfs4_cb_conn *conn = &clp->cl_cb_conn;
6f3d772f
TU
1658 struct sockaddr *sa = svc_addr(rqstp);
1659 u32 scopeid = rpc_get_scope_id(sa);
7077ecba
JL
1660 unsigned short expected_family;
1661
1662 /* Currently, we only support tcp and tcp6 for the callback channel */
1663 if (se->se_callback_netid_len == 3 &&
1664 !memcmp(se->se_callback_netid_val, "tcp", 3))
1665 expected_family = AF_INET;
1666 else if (se->se_callback_netid_len == 4 &&
1667 !memcmp(se->se_callback_netid_val, "tcp6", 4))
1668 expected_family = AF_INET6;
1669 else
1da177e4
LT
1670 goto out_err;
1671
c212cecf 1672 conn->cb_addrlen = rpc_uaddr2sockaddr(clp->net, se->se_callback_addr_val,
aa9a4ec7 1673 se->se_callback_addr_len,
07263f1e
BF
1674 (struct sockaddr *)&conn->cb_addr,
1675 sizeof(conn->cb_addr));
aa9a4ec7 1676
07263f1e 1677 if (!conn->cb_addrlen || conn->cb_addr.ss_family != expected_family)
1da177e4 1678 goto out_err;
aa9a4ec7 1679
07263f1e
BF
1680 if (conn->cb_addr.ss_family == AF_INET6)
1681 ((struct sockaddr_in6 *)&conn->cb_addr)->sin6_scope_id = scopeid;
fbf4665f 1682
07263f1e
BF
1683 conn->cb_prog = se->se_callback_prog;
1684 conn->cb_ident = se->se_callback_ident;
849a1cf1 1685 memcpy(&conn->cb_saddr, &rqstp->rq_daddr, rqstp->rq_daddrlen);
1da177e4
LT
1686 return;
1687out_err:
07263f1e
BF
1688 conn->cb_addr.ss_family = AF_UNSPEC;
1689 conn->cb_addrlen = 0;
849823c5 1690 dprintk(KERN_INFO "NFSD: this client (clientid %08x/%08x) "
1da177e4
LT
1691 "will not receive delegations\n",
1692 clp->cl_clientid.cl_boot, clp->cl_clientid.cl_id);
1693
1da177e4
LT
1694 return;
1695}
1696
074fe897 1697/*
067e1ace 1698 * Cache a reply. nfsd4_check_resp_size() has bounded the cache size.
074fe897 1699 */
b607664e 1700static void
074fe897 1701nfsd4_store_cache_entry(struct nfsd4_compoundres *resp)
074fe897 1702{
f5236013 1703 struct xdr_buf *buf = resp->xdr.buf;
557ce264
AA
1704 struct nfsd4_slot *slot = resp->cstate.slot;
1705 unsigned int base;
074fe897 1706
557ce264 1707 dprintk("--> %s slot %p\n", __func__, slot);
074fe897 1708
557ce264
AA
1709 slot->sl_opcnt = resp->opcnt;
1710 slot->sl_status = resp->cstate.status;
074fe897 1711
bf5c43c8 1712 slot->sl_flags |= NFSD4_SLOT_INITIALIZED;
bf864a31 1713 if (nfsd4_not_cached(resp)) {
557ce264 1714 slot->sl_datalen = 0;
bf864a31 1715 return;
074fe897 1716 }
f5236013
BF
1717 base = resp->cstate.data_offset;
1718 slot->sl_datalen = buf->len - base;
1719 if (read_bytes_from_xdr_buf(buf, base, slot->sl_data, slot->sl_datalen))
557ce264
AA
1720 WARN("%s: sessions DRC could not cache compound\n", __func__);
1721 return;
074fe897
AA
1722}
1723
1724/*
abfabf8c
AA
1725 * Encode the replay sequence operation from the slot values.
1726 * If cachethis is FALSE encode the uncached rep error on the next
1727 * operation which sets resp->p and increments resp->opcnt for
1728 * nfs4svc_encode_compoundres.
074fe897 1729 *
074fe897 1730 */
abfabf8c
AA
1731static __be32
1732nfsd4_enc_sequence_replay(struct nfsd4_compoundargs *args,
1733 struct nfsd4_compoundres *resp)
074fe897 1734{
abfabf8c
AA
1735 struct nfsd4_op *op;
1736 struct nfsd4_slot *slot = resp->cstate.slot;
bf864a31 1737
abfabf8c
AA
1738 /* Encode the replayed sequence operation */
1739 op = &args->ops[resp->opcnt - 1];
1740 nfsd4_encode_operation(resp, op);
bf864a31 1741
abfabf8c 1742 /* Return nfserr_retry_uncached_rep in next operation. */
73e79482 1743 if (args->opcnt > 1 && !(slot->sl_flags & NFSD4_SLOT_CACHETHIS)) {
abfabf8c
AA
1744 op = &args->ops[resp->opcnt++];
1745 op->status = nfserr_retry_uncached_rep;
1746 nfsd4_encode_operation(resp, op);
074fe897 1747 }
abfabf8c 1748 return op->status;
074fe897
AA
1749}
1750
1751/*
557ce264
AA
1752 * The sequence operation is not cached because we can use the slot and
1753 * session values.
074fe897 1754 */
3ca2eb98 1755static __be32
bf864a31
AA
1756nfsd4_replay_cache_entry(struct nfsd4_compoundres *resp,
1757 struct nfsd4_sequence *seq)
074fe897 1758{
557ce264 1759 struct nfsd4_slot *slot = resp->cstate.slot;
f5236013
BF
1760 struct xdr_stream *xdr = &resp->xdr;
1761 __be32 *p;
074fe897
AA
1762 __be32 status;
1763
557ce264 1764 dprintk("--> %s slot %p\n", __func__, slot);
074fe897 1765
abfabf8c 1766 status = nfsd4_enc_sequence_replay(resp->rqstp->rq_argp, resp);
0da7b19c 1767 if (status)
abfabf8c 1768 return status;
074fe897 1769
f5236013
BF
1770 p = xdr_reserve_space(xdr, slot->sl_datalen);
1771 if (!p) {
1772 WARN_ON_ONCE(1);
1773 return nfserr_serverfault;
1774 }
1775 xdr_encode_opaque_fixed(p, slot->sl_data, slot->sl_datalen);
1776 xdr_commit_encode(xdr);
074fe897 1777
557ce264 1778 resp->opcnt = slot->sl_opcnt;
f5236013 1779 return slot->sl_status;
074fe897
AA
1780}
1781
0733d213
AA
1782/*
1783 * Set the exchange_id flags returned by the server.
1784 */
1785static void
1786nfsd4_set_ex_flags(struct nfs4_client *new, struct nfsd4_exchange_id *clid)
1787{
1788 /* pNFS is not supported */
1789 new->cl_exchange_flags |= EXCHGID4_FLAG_USE_NON_PNFS;
1790
1791 /* Referrals are supported, Migration is not. */
1792 new->cl_exchange_flags |= EXCHGID4_FLAG_SUPP_MOVED_REFER;
1793
1794 /* set the wire flags to return to client. */
1795 clid->flags = new->cl_exchange_flags;
1796}
1797
631fc9ea
BF
1798static bool client_has_state(struct nfs4_client *clp)
1799{
1800 /*
1801 * Note clp->cl_openowners check isn't quite right: there's no
1802 * need to count owners without stateid's.
1803 *
1804 * Also note we should probably be using this in 4.0 case too.
1805 */
6eccece9
BF
1806 return !list_empty(&clp->cl_openowners)
1807 || !list_empty(&clp->cl_delegations)
1808 || !list_empty(&clp->cl_sessions);
631fc9ea
BF
1809}
1810
069b6ad4
AA
1811__be32
1812nfsd4_exchange_id(struct svc_rqst *rqstp,
1813 struct nfsd4_compound_state *cstate,
1814 struct nfsd4_exchange_id *exid)
1815{
0733d213 1816 struct nfs4_client *unconf, *conf, *new;
57b7b43b 1817 __be32 status;
363168b4 1818 char addr_str[INET6_ADDRSTRLEN];
0733d213 1819 nfs4_verifier verf = exid->verifier;
363168b4 1820 struct sockaddr *sa = svc_addr(rqstp);
83e08fd4 1821 bool update = exid->flags & EXCHGID4_FLAG_UPD_CONFIRMED_REC_A;
c212cecf 1822 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
0733d213 1823
363168b4 1824 rpc_ntop(sa, addr_str, sizeof(addr_str));
0733d213 1825 dprintk("%s rqstp=%p exid=%p clname.len=%u clname.data=%p "
363168b4 1826 "ip_addr=%s flags %x, spa_how %d\n",
0733d213 1827 __func__, rqstp, exid, exid->clname.len, exid->clname.data,
363168b4 1828 addr_str, exid->flags, exid->spa_how);
0733d213 1829
a084daf5 1830 if (exid->flags & ~EXCHGID4_FLAG_MASK_A)
0733d213
AA
1831 return nfserr_inval;
1832
0733d213 1833 switch (exid->spa_how) {
57266a6e
BF
1834 case SP4_MACH_CRED:
1835 if (!svc_rqst_integrity_protected(rqstp))
1836 return nfserr_inval;
0733d213
AA
1837 case SP4_NONE:
1838 break;
063b0fb9
BF
1839 default: /* checked by xdr code */
1840 WARN_ON_ONCE(1);
0733d213 1841 case SP4_SSV:
dd30333c 1842 return nfserr_encr_alg_unsupp;
0733d213
AA
1843 }
1844
2dbb269d 1845 /* Cases below refer to rfc 5661 section 18.35.4: */
0733d213 1846 nfs4_lock_state();
382a62e7 1847 conf = find_confirmed_client_by_name(&exid->clname, nn);
0733d213 1848 if (conf) {
83e08fd4
BF
1849 bool creds_match = same_creds(&conf->cl_cred, &rqstp->rq_cred);
1850 bool verfs_match = same_verf(&verf, &conf->cl_verifier);
1851
136e658d
BF
1852 if (update) {
1853 if (!clp_used_exchangeid(conf)) { /* buggy client */
2dbb269d 1854 status = nfserr_inval;
1a308118
BF
1855 goto out;
1856 }
57266a6e
BF
1857 if (!mach_creds_match(conf, rqstp)) {
1858 status = nfserr_wrong_cred;
1859 goto out;
1860 }
136e658d 1861 if (!creds_match) { /* case 9 */
ea236d07 1862 status = nfserr_perm;
136e658d
BF
1863 goto out;
1864 }
1865 if (!verfs_match) { /* case 8 */
0733d213
AA
1866 status = nfserr_not_same;
1867 goto out;
1868 }
136e658d
BF
1869 /* case 6 */
1870 exid->flags |= EXCHGID4_FLAG_CONFIRMED_R;
1871 new = conf;
1872 goto out_copy;
0733d213 1873 }
136e658d 1874 if (!creds_match) { /* case 3 */
631fc9ea
BF
1875 if (client_has_state(conf)) {
1876 status = nfserr_clid_inuse;
0733d213
AA
1877 goto out;
1878 }
1879 expire_client(conf);
1880 goto out_new;
1881 }
136e658d 1882 if (verfs_match) { /* case 2 */
0f1ba0ef 1883 conf->cl_exchange_flags |= EXCHGID4_FLAG_CONFIRMED_R;
136e658d
BF
1884 new = conf;
1885 goto out_copy;
1886 }
1887 /* case 5, client reboot */
136e658d 1888 goto out_new;
6ddbbbfe
MS
1889 }
1890
2dbb269d 1891 if (update) { /* case 7 */
6ddbbbfe
MS
1892 status = nfserr_noent;
1893 goto out;
0733d213
AA
1894 }
1895
a99454aa 1896 unconf = find_unconfirmed_client_by_name(&exid->clname, nn);
2dbb269d 1897 if (unconf) /* case 4, possible retry or client restart */
0733d213 1898 expire_client(unconf);
0733d213 1899
2dbb269d 1900 /* case 1 (normal case) */
0733d213 1901out_new:
2216d449 1902 new = create_client(exid->clname, rqstp, &verf);
0733d213 1903 if (new == NULL) {
4731030d 1904 status = nfserr_jukebox;
0733d213
AA
1905 goto out;
1906 }
4f540e29 1907 new->cl_minorversion = cstate->minorversion;
57266a6e 1908 new->cl_mach_cred = (exid->spa_how == SP4_MACH_CRED);
0733d213 1909
c212cecf 1910 gen_clid(new, nn);
ac55fdc4 1911 add_to_unconfirmed(new);
0733d213
AA
1912out_copy:
1913 exid->clientid.cl_boot = new->cl_clientid.cl_boot;
1914 exid->clientid.cl_id = new->cl_clientid.cl_id;
1915
778df3f0 1916 exid->seqid = new->cl_cs_slot.sl_seqid + 1;
0733d213
AA
1917 nfsd4_set_ex_flags(new, exid);
1918
1919 dprintk("nfsd4_exchange_id seqid %d flags %x\n",
49557cc7 1920 new->cl_cs_slot.sl_seqid, new->cl_exchange_flags);
0733d213
AA
1921 status = nfs_ok;
1922
1923out:
1924 nfs4_unlock_state();
0733d213 1925 return status;
069b6ad4
AA
1926}
1927
57b7b43b 1928static __be32
88e588d5 1929check_slot_seqid(u32 seqid, u32 slot_seqid, int slot_inuse)
b85d4c01 1930{
88e588d5
AA
1931 dprintk("%s enter. seqid %d slot_seqid %d\n", __func__, seqid,
1932 slot_seqid);
b85d4c01
BH
1933
1934 /* The slot is in use, and no response has been sent. */
88e588d5
AA
1935 if (slot_inuse) {
1936 if (seqid == slot_seqid)
b85d4c01
BH
1937 return nfserr_jukebox;
1938 else
1939 return nfserr_seq_misordered;
1940 }
f6d82485 1941 /* Note unsigned 32-bit arithmetic handles wraparound: */
88e588d5 1942 if (likely(seqid == slot_seqid + 1))
b85d4c01 1943 return nfs_ok;
88e588d5 1944 if (seqid == slot_seqid)
b85d4c01 1945 return nfserr_replay_cache;
b85d4c01
BH
1946 return nfserr_seq_misordered;
1947}
1948
49557cc7
AA
1949/*
1950 * Cache the create session result into the create session single DRC
1951 * slot cache by saving the xdr structure. sl_seqid has been set.
1952 * Do this for solo or embedded create session operations.
1953 */
1954static void
1955nfsd4_cache_create_session(struct nfsd4_create_session *cr_ses,
57b7b43b 1956 struct nfsd4_clid_slot *slot, __be32 nfserr)
49557cc7
AA
1957{
1958 slot->sl_status = nfserr;
1959 memcpy(&slot->sl_cr_ses, cr_ses, sizeof(*cr_ses));
1960}
1961
1962static __be32
1963nfsd4_replay_create_session(struct nfsd4_create_session *cr_ses,
1964 struct nfsd4_clid_slot *slot)
1965{
1966 memcpy(cr_ses, &slot->sl_cr_ses, sizeof(*cr_ses));
1967 return slot->sl_status;
1968}
1969
1b74c25b
MJ
1970#define NFSD_MIN_REQ_HDR_SEQ_SZ ((\
1971 2 * 2 + /* credential,verifier: AUTH_NULL, length 0 */ \
1972 1 + /* MIN tag is length with zero, only length */ \
1973 3 + /* version, opcount, opcode */ \
1974 XDR_QUADLEN(NFS4_MAX_SESSIONID_LEN) + \
1975 /* seqid, slotID, slotID, cache */ \
1976 4 ) * sizeof(__be32))
1977
1978#define NFSD_MIN_RESP_HDR_SEQ_SZ ((\
1979 2 + /* verifier: AUTH_NULL, length 0 */\
1980 1 + /* status */ \
1981 1 + /* MIN tag is length with zero, only length */ \
1982 3 + /* opcount, opcode, opstatus*/ \
1983 XDR_QUADLEN(NFS4_MAX_SESSIONID_LEN) + \
1984 /* seqid, slotID, slotID, slotID, status */ \
1985 5 ) * sizeof(__be32))
1986
55c760cf 1987static __be32 check_forechannel_attrs(struct nfsd4_channel_attrs *ca, struct nfsd_net *nn)
1b74c25b 1988{
55c760cf
BF
1989 u32 maxrpc = nn->nfsd_serv->sv_max_mesg;
1990
373cd409
BF
1991 if (ca->maxreq_sz < NFSD_MIN_REQ_HDR_SEQ_SZ)
1992 return nfserr_toosmall;
1993 if (ca->maxresp_sz < NFSD_MIN_RESP_HDR_SEQ_SZ)
1994 return nfserr_toosmall;
55c760cf
BF
1995 ca->headerpadsz = 0;
1996 ca->maxreq_sz = min_t(u32, ca->maxreq_sz, maxrpc);
1997 ca->maxresp_sz = min_t(u32, ca->maxresp_sz, maxrpc);
1998 ca->maxops = min_t(u32, ca->maxops, NFSD_MAX_OPS_PER_COMPOUND);
1999 ca->maxresp_cached = min_t(u32, ca->maxresp_cached,
2000 NFSD_SLOT_CACHE_SIZE + NFSD_MIN_HDR_SEQ_SZ);
2001 ca->maxreqs = min_t(u32, ca->maxreqs, NFSD_MAX_SLOTS_PER_SESSION);
2002 /*
2003 * Note decreasing slot size below client's request may make it
2004 * difficult for client to function correctly, whereas
2005 * decreasing the number of slots will (just?) affect
2006 * performance. When short on memory we therefore prefer to
2007 * decrease number of slots instead of their size. Clients that
2008 * request larger slots than they need will get poor results:
2009 */
2010 ca->maxreqs = nfsd4_get_drc_mem(ca);
2011 if (!ca->maxreqs)
2012 return nfserr_jukebox;
2013
373cd409 2014 return nfs_ok;
1b74c25b
MJ
2015}
2016
8a891633
KM
2017#define NFSD_CB_MAX_REQ_SZ ((NFS4_enc_cb_recall_sz + \
2018 RPC_MAX_HEADER_WITH_AUTH) * sizeof(__be32))
2019#define NFSD_CB_MAX_RESP_SZ ((NFS4_dec_cb_recall_sz + \
2020 RPC_MAX_REPHEADER_WITH_AUTH) * sizeof(__be32))
2021
06b332a5 2022static __be32 check_backchannel_attrs(struct nfsd4_channel_attrs *ca)
1b74c25b 2023{
06b332a5
BF
2024 ca->headerpadsz = 0;
2025
2026 /*
2027 * These RPC_MAX_HEADER macros are overkill, especially since we
2028 * don't even do gss on the backchannel yet. But this is still
2029 * less than 1k. Tighten up this estimate in the unlikely event
2030 * it turns out to be a problem for some client:
2031 */
8a891633 2032 if (ca->maxreq_sz < NFSD_CB_MAX_REQ_SZ)
06b332a5 2033 return nfserr_toosmall;
8a891633 2034 if (ca->maxresp_sz < NFSD_CB_MAX_RESP_SZ)
06b332a5
BF
2035 return nfserr_toosmall;
2036 ca->maxresp_cached = 0;
2037 if (ca->maxops < 2)
2038 return nfserr_toosmall;
2039
2040 return nfs_ok;
1b74c25b
MJ
2041}
2042
b78724b7
BF
2043static __be32 nfsd4_check_cb_sec(struct nfsd4_cb_sec *cbs)
2044{
2045 switch (cbs->flavor) {
2046 case RPC_AUTH_NULL:
2047 case RPC_AUTH_UNIX:
2048 return nfs_ok;
2049 default:
2050 /*
2051 * GSS case: the spec doesn't allow us to return this
2052 * error. But it also doesn't allow us not to support
2053 * GSS.
2054 * I'd rather this fail hard than return some error the
2055 * client might think it can already handle:
2056 */
2057 return nfserr_encr_alg_unsupp;
2058 }
2059}
2060
069b6ad4
AA
2061__be32
2062nfsd4_create_session(struct svc_rqst *rqstp,
2063 struct nfsd4_compound_state *cstate,
2064 struct nfsd4_create_session *cr_ses)
2065{
363168b4 2066 struct sockaddr *sa = svc_addr(rqstp);
ec6b5d7b 2067 struct nfs4_client *conf, *unconf;
ac7c46f2 2068 struct nfsd4_session *new;
81f0b2a4 2069 struct nfsd4_conn *conn;
49557cc7 2070 struct nfsd4_clid_slot *cs_slot = NULL;
57b7b43b 2071 __be32 status = 0;
8daae4dc 2072 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
ec6b5d7b 2073
a62573dc
MJ
2074 if (cr_ses->flags & ~SESSION4_FLAG_MASK_A)
2075 return nfserr_inval;
b78724b7
BF
2076 status = nfsd4_check_cb_sec(&cr_ses->cb_sec);
2077 if (status)
2078 return status;
55c760cf 2079 status = check_forechannel_attrs(&cr_ses->fore_channel, nn);
06b332a5
BF
2080 if (status)
2081 return status;
2082 status = check_backchannel_attrs(&cr_ses->back_channel);
373cd409 2083 if (status)
f403e450 2084 goto out_release_drc_mem;
81f0b2a4 2085 status = nfserr_jukebox;
60810e54 2086 new = alloc_session(&cr_ses->fore_channel, &cr_ses->back_channel);
55c760cf
BF
2087 if (!new)
2088 goto out_release_drc_mem;
81f0b2a4
BF
2089 conn = alloc_conn_from_crses(rqstp, cr_ses);
2090 if (!conn)
2091 goto out_free_session;
a62573dc 2092
ec6b5d7b 2093 nfs4_lock_state();
0a7ec377 2094 unconf = find_unconfirmed_client(&cr_ses->clientid, true, nn);
8daae4dc 2095 conf = find_confirmed_client(&cr_ses->clientid, true, nn);
78389046 2096 WARN_ON_ONCE(conf && unconf);
ec6b5d7b
AA
2097
2098 if (conf) {
57266a6e
BF
2099 status = nfserr_wrong_cred;
2100 if (!mach_creds_match(conf, rqstp))
2101 goto out_free_conn;
49557cc7
AA
2102 cs_slot = &conf->cl_cs_slot;
2103 status = check_slot_seqid(cr_ses->seqid, cs_slot->sl_seqid, 0);
38eb76a5 2104 if (status == nfserr_replay_cache) {
49557cc7 2105 status = nfsd4_replay_create_session(cr_ses, cs_slot);
81f0b2a4 2106 goto out_free_conn;
49557cc7 2107 } else if (cr_ses->seqid != cs_slot->sl_seqid + 1) {
ec6b5d7b 2108 status = nfserr_seq_misordered;
81f0b2a4 2109 goto out_free_conn;
ec6b5d7b 2110 }
ec6b5d7b 2111 } else if (unconf) {
8f9d3d3b 2112 struct nfs4_client *old;
ec6b5d7b 2113 if (!same_creds(&unconf->cl_cred, &rqstp->rq_cred) ||
363168b4 2114 !rpc_cmp_addr(sa, (struct sockaddr *) &unconf->cl_addr)) {
ec6b5d7b 2115 status = nfserr_clid_inuse;
81f0b2a4 2116 goto out_free_conn;
ec6b5d7b 2117 }
57266a6e
BF
2118 status = nfserr_wrong_cred;
2119 if (!mach_creds_match(unconf, rqstp))
2120 goto out_free_conn;
49557cc7
AA
2121 cs_slot = &unconf->cl_cs_slot;
2122 status = check_slot_seqid(cr_ses->seqid, cs_slot->sl_seqid, 0);
38eb76a5
AA
2123 if (status) {
2124 /* an unconfirmed replay returns misordered */
ec6b5d7b 2125 status = nfserr_seq_misordered;
81f0b2a4 2126 goto out_free_conn;
ec6b5d7b 2127 }
382a62e7 2128 old = find_confirmed_client_by_name(&unconf->cl_name, nn);
221a6876
BF
2129 if (old) {
2130 status = mark_client_expired(old);
2131 if (status)
2132 goto out_free_conn;
8f9d3d3b 2133 expire_client(old);
221a6876 2134 }
8f9d3d3b 2135 move_to_confirmed(unconf);
ec6b5d7b
AA
2136 conf = unconf;
2137 } else {
2138 status = nfserr_stale_clientid;
81f0b2a4 2139 goto out_free_conn;
ec6b5d7b 2140 }
81f0b2a4 2141 status = nfs_ok;
408b79bc
BF
2142 /*
2143 * We do not support RDMA or persistent sessions
2144 */
2145 cr_ses->flags &= ~SESSION4_PERSIST;
2146 cr_ses->flags &= ~SESSION4_RDMA;
2147
81f0b2a4
BF
2148 init_session(rqstp, new, conf, cr_ses);
2149 nfsd4_init_conn(rqstp, conn, new);
2150
ac7c46f2 2151 memcpy(cr_ses->sessionid.data, new->se_sessionid.data,
ec6b5d7b 2152 NFS4_MAX_SESSIONID_LEN);
86c3e16c 2153 cs_slot->sl_seqid++;
49557cc7 2154 cr_ses->seqid = cs_slot->sl_seqid;
ec6b5d7b 2155
49557cc7
AA
2156 /* cache solo and embedded create sessions under the state lock */
2157 nfsd4_cache_create_session(cr_ses, cs_slot, status);
ec6b5d7b 2158 nfs4_unlock_state();
ec6b5d7b 2159 return status;
81f0b2a4 2160out_free_conn:
266533c6 2161 nfs4_unlock_state();
81f0b2a4
BF
2162 free_conn(conn);
2163out_free_session:
2164 __free_session(new);
55c760cf
BF
2165out_release_drc_mem:
2166 nfsd4_put_drc_mem(&cr_ses->fore_channel);
1ca50792 2167 return status;
069b6ad4
AA
2168}
2169
1d1bc8f2
BF
2170static __be32 nfsd4_map_bcts_dir(u32 *dir)
2171{
2172 switch (*dir) {
2173 case NFS4_CDFC4_FORE:
2174 case NFS4_CDFC4_BACK:
2175 return nfs_ok;
2176 case NFS4_CDFC4_FORE_OR_BOTH:
2177 case NFS4_CDFC4_BACK_OR_BOTH:
2178 *dir = NFS4_CDFC4_BOTH;
2179 return nfs_ok;
2180 };
2181 return nfserr_inval;
2182}
2183
cb73a9f4
BF
2184__be32 nfsd4_backchannel_ctl(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate, struct nfsd4_backchannel_ctl *bc)
2185{
2186 struct nfsd4_session *session = cstate->session;
c9a49628 2187 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
b78724b7 2188 __be32 status;
cb73a9f4 2189
b78724b7
BF
2190 status = nfsd4_check_cb_sec(&bc->bc_cb_sec);
2191 if (status)
2192 return status;
c9a49628 2193 spin_lock(&nn->client_lock);
cb73a9f4
BF
2194 session->se_cb_prog = bc->bc_cb_program;
2195 session->se_cb_sec = bc->bc_cb_sec;
c9a49628 2196 spin_unlock(&nn->client_lock);
cb73a9f4
BF
2197
2198 nfsd4_probe_callback(session->se_client);
2199
2200 return nfs_ok;
2201}
2202
1d1bc8f2
BF
2203__be32 nfsd4_bind_conn_to_session(struct svc_rqst *rqstp,
2204 struct nfsd4_compound_state *cstate,
2205 struct nfsd4_bind_conn_to_session *bcts)
2206{
2207 __be32 status;
3ba63671 2208 struct nfsd4_conn *conn;
4f6e6c17 2209 struct nfsd4_session *session;
d4e19e70
TM
2210 struct net *net = SVC_NET(rqstp);
2211 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
1d1bc8f2
BF
2212
2213 if (!nfsd4_last_compound_op(rqstp))
2214 return nfserr_not_only_op;
4f6e6c17 2215 nfs4_lock_state();
c9a49628 2216 spin_lock(&nn->client_lock);
d4e19e70 2217 session = find_in_sessionid_hashtbl(&bcts->sessionid, net, &status);
c9a49628 2218 spin_unlock(&nn->client_lock);
4f6e6c17 2219 if (!session)
d4e19e70 2220 goto out_no_session;
57266a6e
BF
2221 status = nfserr_wrong_cred;
2222 if (!mach_creds_match(session->se_client, rqstp))
2223 goto out;
1d1bc8f2 2224 status = nfsd4_map_bcts_dir(&bcts->dir);
3ba63671 2225 if (status)
4f6e6c17 2226 goto out;
3ba63671 2227 conn = alloc_conn(rqstp, bcts->dir);
4f6e6c17 2228 status = nfserr_jukebox;
3ba63671 2229 if (!conn)
4f6e6c17
BF
2230 goto out;
2231 nfsd4_init_conn(rqstp, conn, session);
2232 status = nfs_ok;
2233out:
d4e19e70
TM
2234 nfsd4_put_session(session);
2235out_no_session:
4f6e6c17
BF
2236 nfs4_unlock_state();
2237 return status;
1d1bc8f2
BF
2238}
2239
5d4cec2f
BF
2240static bool nfsd4_compound_in_session(struct nfsd4_session *session, struct nfs4_sessionid *sid)
2241{
2242 if (!session)
2243 return 0;
2244 return !memcmp(sid, &session->se_sessionid, sizeof(*sid));
2245}
2246
069b6ad4
AA
2247__be32
2248nfsd4_destroy_session(struct svc_rqst *r,
2249 struct nfsd4_compound_state *cstate,
2250 struct nfsd4_destroy_session *sessionid)
2251{
e10e0cfc 2252 struct nfsd4_session *ses;
abcdff09 2253 __be32 status;
f0f51f5c 2254 int ref_held_by_me = 0;
d4e19e70
TM
2255 struct net *net = SVC_NET(r);
2256 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
e10e0cfc 2257
abcdff09
BF
2258 nfs4_lock_state();
2259 status = nfserr_not_only_op;
5d4cec2f 2260 if (nfsd4_compound_in_session(cstate->session, &sessionid->sessionid)) {
57716355 2261 if (!nfsd4_last_compound_op(r))
abcdff09 2262 goto out;
f0f51f5c 2263 ref_held_by_me++;
57716355 2264 }
e10e0cfc 2265 dump_sessionid(__func__, &sessionid->sessionid);
c9a49628 2266 spin_lock(&nn->client_lock);
d4e19e70 2267 ses = find_in_sessionid_hashtbl(&sessionid->sessionid, net, &status);
abcdff09
BF
2268 if (!ses)
2269 goto out_client_lock;
57266a6e
BF
2270 status = nfserr_wrong_cred;
2271 if (!mach_creds_match(ses->se_client, r))
d4e19e70 2272 goto out_put_session;
f0f51f5c 2273 status = mark_session_dead_locked(ses, 1 + ref_held_by_me);
66b2b9b2 2274 if (status)
f0f51f5c 2275 goto out_put_session;
e10e0cfc 2276 unhash_session(ses);
c9a49628 2277 spin_unlock(&nn->client_lock);
e10e0cfc 2278
84f5f7cc 2279 nfsd4_probe_callback_sync(ses->se_client);
19cf5c02 2280
c9a49628 2281 spin_lock(&nn->client_lock);
e10e0cfc 2282 status = nfs_ok;
f0f51f5c 2283out_put_session:
d4e19e70 2284 nfsd4_put_session_locked(ses);
abcdff09
BF
2285out_client_lock:
2286 spin_unlock(&nn->client_lock);
e10e0cfc 2287out:
abcdff09 2288 nfs4_unlock_state();
e10e0cfc 2289 return status;
069b6ad4
AA
2290}
2291
a663bdd8 2292static struct nfsd4_conn *__nfsd4_find_conn(struct svc_xprt *xpt, struct nfsd4_session *s)
328ead28
BF
2293{
2294 struct nfsd4_conn *c;
2295
2296 list_for_each_entry(c, &s->se_conns, cn_persession) {
a663bdd8 2297 if (c->cn_xprt == xpt) {
328ead28
BF
2298 return c;
2299 }
2300 }
2301 return NULL;
2302}
2303
57266a6e 2304static __be32 nfsd4_sequence_check_conn(struct nfsd4_conn *new, struct nfsd4_session *ses)
328ead28
BF
2305{
2306 struct nfs4_client *clp = ses->se_client;
a663bdd8 2307 struct nfsd4_conn *c;
57266a6e 2308 __be32 status = nfs_ok;
21b75b01 2309 int ret;
328ead28
BF
2310
2311 spin_lock(&clp->cl_lock);
a663bdd8 2312 c = __nfsd4_find_conn(new->cn_xprt, ses);
57266a6e
BF
2313 if (c)
2314 goto out_free;
2315 status = nfserr_conn_not_bound_to_session;
2316 if (clp->cl_mach_cred)
2317 goto out_free;
328ead28
BF
2318 __nfsd4_hash_conn(new, ses);
2319 spin_unlock(&clp->cl_lock);
21b75b01
BF
2320 ret = nfsd4_register_conn(new);
2321 if (ret)
2322 /* oops; xprt is already down: */
2323 nfsd4_conn_lost(&new->cn_xpt_user);
57266a6e
BF
2324 return nfs_ok;
2325out_free:
2326 spin_unlock(&clp->cl_lock);
2327 free_conn(new);
2328 return status;
328ead28
BF
2329}
2330
868b89c3
MJ
2331static bool nfsd4_session_too_many_ops(struct svc_rqst *rqstp, struct nfsd4_session *session)
2332{
2333 struct nfsd4_compoundargs *args = rqstp->rq_argp;
2334
2335 return args->opcnt > session->se_fchannel.maxops;
2336}
2337
ae82a8d0
MJ
2338static bool nfsd4_request_too_big(struct svc_rqst *rqstp,
2339 struct nfsd4_session *session)
2340{
2341 struct xdr_buf *xb = &rqstp->rq_arg;
2342
2343 return xb->len > session->se_fchannel.maxreq_sz;
2344}
2345
069b6ad4 2346__be32
b85d4c01 2347nfsd4_sequence(struct svc_rqst *rqstp,
069b6ad4
AA
2348 struct nfsd4_compound_state *cstate,
2349 struct nfsd4_sequence *seq)
2350{
f9bb94c4 2351 struct nfsd4_compoundres *resp = rqstp->rq_resp;
47ee5298 2352 struct xdr_stream *xdr = &resp->xdr;
b85d4c01 2353 struct nfsd4_session *session;
221a6876 2354 struct nfs4_client *clp;
b85d4c01 2355 struct nfsd4_slot *slot;
a663bdd8 2356 struct nfsd4_conn *conn;
57b7b43b 2357 __be32 status;
47ee5298 2358 int buflen;
d4e19e70
TM
2359 struct net *net = SVC_NET(rqstp);
2360 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
b85d4c01 2361
f9bb94c4
AA
2362 if (resp->opcnt != 1)
2363 return nfserr_sequence_pos;
2364
a663bdd8
BF
2365 /*
2366 * Will be either used or freed by nfsd4_sequence_check_conn
2367 * below.
2368 */
2369 conn = alloc_conn(rqstp, NFS4_CDFC4_FORE);
2370 if (!conn)
2371 return nfserr_jukebox;
2372
c9a49628 2373 spin_lock(&nn->client_lock);
d4e19e70 2374 session = find_in_sessionid_hashtbl(&seq->sessionid, net, &status);
b85d4c01 2375 if (!session)
221a6876
BF
2376 goto out_no_session;
2377 clp = session->se_client;
b85d4c01 2378
868b89c3
MJ
2379 status = nfserr_too_many_ops;
2380 if (nfsd4_session_too_many_ops(rqstp, session))
66b2b9b2 2381 goto out_put_session;
868b89c3 2382
ae82a8d0
MJ
2383 status = nfserr_req_too_big;
2384 if (nfsd4_request_too_big(rqstp, session))
66b2b9b2 2385 goto out_put_session;
ae82a8d0 2386
b85d4c01 2387 status = nfserr_badslot;
6c18ba9f 2388 if (seq->slotid >= session->se_fchannel.maxreqs)
66b2b9b2 2389 goto out_put_session;
b85d4c01 2390
557ce264 2391 slot = session->se_slots[seq->slotid];
b85d4c01
BH
2392 dprintk("%s: slotid %d\n", __func__, seq->slotid);
2393
a8dfdaeb
AA
2394 /* We do not negotiate the number of slots yet, so set the
2395 * maxslots to the session maxreqs which is used to encode
2396 * sr_highest_slotid and the sr_target_slot id to maxslots */
2397 seq->maxslots = session->se_fchannel.maxreqs;
2398
73e79482
BF
2399 status = check_slot_seqid(seq->seqid, slot->sl_seqid,
2400 slot->sl_flags & NFSD4_SLOT_INUSE);
b85d4c01 2401 if (status == nfserr_replay_cache) {
bf5c43c8
BF
2402 status = nfserr_seq_misordered;
2403 if (!(slot->sl_flags & NFSD4_SLOT_INITIALIZED))
66b2b9b2 2404 goto out_put_session;
b85d4c01
BH
2405 cstate->slot = slot;
2406 cstate->session = session;
4b24ca7d 2407 cstate->clp = clp;
da3846a2 2408 /* Return the cached reply status and set cstate->status
557ce264 2409 * for nfsd4_proc_compound processing */
bf864a31 2410 status = nfsd4_replay_cache_entry(resp, seq);
da3846a2 2411 cstate->status = nfserr_replay_cache;
aaf84eb9 2412 goto out;
b85d4c01
BH
2413 }
2414 if (status)
66b2b9b2 2415 goto out_put_session;
b85d4c01 2416
57266a6e 2417 status = nfsd4_sequence_check_conn(conn, session);
a663bdd8 2418 conn = NULL;
57266a6e
BF
2419 if (status)
2420 goto out_put_session;
328ead28 2421
47ee5298
BF
2422 buflen = (seq->cachethis) ?
2423 session->se_fchannel.maxresp_cached :
2424 session->se_fchannel.maxresp_sz;
2425 status = (seq->cachethis) ? nfserr_rep_too_big_to_cache :
2426 nfserr_rep_too_big;
a5cddc88 2427 if (xdr_restrict_buflen(xdr, buflen - rqstp->rq_auth_slack))
47ee5298 2428 goto out_put_session;
32aaa62e 2429 svc_reserve(rqstp, buflen);
47ee5298
BF
2430
2431 status = nfs_ok;
b85d4c01 2432 /* Success! bump slot seqid */
b85d4c01 2433 slot->sl_seqid = seq->seqid;
bf5c43c8 2434 slot->sl_flags |= NFSD4_SLOT_INUSE;
73e79482
BF
2435 if (seq->cachethis)
2436 slot->sl_flags |= NFSD4_SLOT_CACHETHIS;
bf5c43c8
BF
2437 else
2438 slot->sl_flags &= ~NFSD4_SLOT_CACHETHIS;
b85d4c01
BH
2439
2440 cstate->slot = slot;
2441 cstate->session = session;
4b24ca7d 2442 cstate->clp = clp;
b85d4c01 2443
b85d4c01 2444out:
221a6876
BF
2445 switch (clp->cl_cb_state) {
2446 case NFSD4_CB_DOWN:
2447 seq->status_flags = SEQ4_STATUS_CB_PATH_DOWN;
2448 break;
2449 case NFSD4_CB_FAULT:
2450 seq->status_flags = SEQ4_STATUS_BACKCHANNEL_FAULT;
2451 break;
2452 default:
2453 seq->status_flags = 0;
aaf84eb9 2454 }
3bd64a5b
BF
2455 if (!list_empty(&clp->cl_revoked))
2456 seq->status_flags |= SEQ4_STATUS_RECALLABLE_STATE_REVOKED;
221a6876 2457out_no_session:
3f42d2c4
KM
2458 if (conn)
2459 free_conn(conn);
c9a49628 2460 spin_unlock(&nn->client_lock);
b85d4c01 2461 return status;
66b2b9b2 2462out_put_session:
d4e19e70 2463 nfsd4_put_session_locked(session);
221a6876 2464 goto out_no_session;
069b6ad4
AA
2465}
2466
b607664e
TM
2467void
2468nfsd4_sequence_done(struct nfsd4_compoundres *resp)
2469{
2470 struct nfsd4_compound_state *cs = &resp->cstate;
2471
2472 if (nfsd4_has_session(cs)) {
b607664e
TM
2473 if (cs->status != nfserr_replay_cache) {
2474 nfsd4_store_cache_entry(resp);
2475 cs->slot->sl_flags &= ~NFSD4_SLOT_INUSE;
2476 }
d4e19e70 2477 /* Drop session reference that was taken in nfsd4_sequence() */
b607664e 2478 nfsd4_put_session(cs->session);
4b24ca7d
JL
2479 } else if (cs->clp)
2480 put_client_renew(cs->clp);
b607664e
TM
2481}
2482
345c2842
MJ
2483__be32
2484nfsd4_destroy_clientid(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate, struct nfsd4_destroy_clientid *dc)
2485{
2486 struct nfs4_client *conf, *unconf, *clp;
57b7b43b 2487 __be32 status = 0;
8daae4dc 2488 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
345c2842
MJ
2489
2490 nfs4_lock_state();
0a7ec377 2491 unconf = find_unconfirmed_client(&dc->clientid, true, nn);
8daae4dc 2492 conf = find_confirmed_client(&dc->clientid, true, nn);
78389046 2493 WARN_ON_ONCE(conf && unconf);
345c2842
MJ
2494
2495 if (conf) {
2496 clp = conf;
2497
c0293b01 2498 if (client_has_state(conf)) {
345c2842
MJ
2499 status = nfserr_clientid_busy;
2500 goto out;
2501 }
2502 } else if (unconf)
2503 clp = unconf;
2504 else {
2505 status = nfserr_stale_clientid;
2506 goto out;
2507 }
57266a6e
BF
2508 if (!mach_creds_match(clp, rqstp)) {
2509 status = nfserr_wrong_cred;
2510 goto out;
2511 }
345c2842
MJ
2512 expire_client(clp);
2513out:
2514 nfs4_unlock_state();
345c2842
MJ
2515 return status;
2516}
2517
4dc6ec00
BF
2518__be32
2519nfsd4_reclaim_complete(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate, struct nfsd4_reclaim_complete *rc)
2520{
57b7b43b 2521 __be32 status = 0;
bcecf1cc 2522
4dc6ec00
BF
2523 if (rc->rca_one_fs) {
2524 if (!cstate->current_fh.fh_dentry)
2525 return nfserr_nofilehandle;
2526 /*
2527 * We don't take advantage of the rca_one_fs case.
2528 * That's OK, it's optional, we can safely ignore it.
2529 */
2530 return nfs_ok;
2531 }
bcecf1cc 2532
4dc6ec00 2533 nfs4_lock_state();
bcecf1cc 2534 status = nfserr_complete_already;
a52d726b
JL
2535 if (test_and_set_bit(NFSD4_CLIENT_RECLAIM_COMPLETE,
2536 &cstate->session->se_client->cl_flags))
bcecf1cc
MJ
2537 goto out;
2538
2539 status = nfserr_stale_clientid;
2540 if (is_client_expired(cstate->session->se_client))
4dc6ec00
BF
2541 /*
2542 * The following error isn't really legal.
2543 * But we only get here if the client just explicitly
2544 * destroyed the client. Surely it no longer cares what
2545 * error it gets back on an operation for the dead
2546 * client.
2547 */
bcecf1cc
MJ
2548 goto out;
2549
2550 status = nfs_ok;
2a4317c5 2551 nfsd4_client_record_create(cstate->session->se_client);
bcecf1cc 2552out:
4dc6ec00 2553 nfs4_unlock_state();
bcecf1cc 2554 return status;
4dc6ec00
BF
2555}
2556
b37ad28b 2557__be32
b591480b
BF
2558nfsd4_setclientid(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
2559 struct nfsd4_setclientid *setclid)
1da177e4 2560{
a084daf5 2561 struct xdr_netobj clname = setclid->se_name;
1da177e4 2562 nfs4_verifier clverifier = setclid->se_verf;
28ce6054 2563 struct nfs4_client *conf, *unconf, *new;
b37ad28b 2564 __be32 status;
c212cecf
SK
2565 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
2566
63db4632 2567 /* Cases below refer to rfc 3530 section 14.2.33: */
1da177e4 2568 nfs4_lock_state();
382a62e7 2569 conf = find_confirmed_client_by_name(&clname, nn);
28ce6054 2570 if (conf) {
63db4632 2571 /* case 0: */
1da177e4 2572 status = nfserr_clid_inuse;
e203d506
BF
2573 if (clp_used_exchangeid(conf))
2574 goto out;
026722c2 2575 if (!same_creds(&conf->cl_cred, &rqstp->rq_cred)) {
363168b4
JL
2576 char addr_str[INET6_ADDRSTRLEN];
2577 rpc_ntop((struct sockaddr *) &conf->cl_addr, addr_str,
2578 sizeof(addr_str));
2579 dprintk("NFSD: setclientid: string in use by client "
2580 "at %s\n", addr_str);
1da177e4
LT
2581 goto out;
2582 }
1da177e4 2583 }
a99454aa 2584 unconf = find_unconfirmed_client_by_name(&clname, nn);
8f930711
BF
2585 if (unconf)
2586 expire_client(unconf);
3e772463 2587 status = nfserr_jukebox;
2216d449 2588 new = create_client(clname, rqstp, &clverifier);
8f930711
BF
2589 if (new == NULL)
2590 goto out;
34b232bb 2591 if (conf && same_verf(&conf->cl_verifier, &clverifier))
63db4632 2592 /* case 1: probable callback update */
1da177e4 2593 copy_clid(new, conf);
34b232bb 2594 else /* case 4 (new client) or cases 2, 3 (client reboot): */
c212cecf 2595 gen_clid(new, nn);
8323c3b2 2596 new->cl_minorversion = 0;
6f3d772f 2597 gen_callback(new, setclid, rqstp);
ac55fdc4 2598 add_to_unconfirmed(new);
1da177e4
LT
2599 setclid->se_clientid.cl_boot = new->cl_clientid.cl_boot;
2600 setclid->se_clientid.cl_id = new->cl_clientid.cl_id;
2601 memcpy(setclid->se_confirm.data, new->cl_confirm.data, sizeof(setclid->se_confirm.data));
2602 status = nfs_ok;
2603out:
2604 nfs4_unlock_state();
2605 return status;
2606}
2607
2608
b37ad28b 2609__be32
b591480b
BF
2610nfsd4_setclientid_confirm(struct svc_rqst *rqstp,
2611 struct nfsd4_compound_state *cstate,
2612 struct nfsd4_setclientid_confirm *setclientid_confirm)
1da177e4 2613{
21ab45a4 2614 struct nfs4_client *conf, *unconf;
1da177e4
LT
2615 nfs4_verifier confirm = setclientid_confirm->sc_confirm;
2616 clientid_t * clid = &setclientid_confirm->sc_clientid;
b37ad28b 2617 __be32 status;
7f2210fa 2618 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
1da177e4 2619
2c142baa 2620 if (STALE_CLIENTID(clid, nn))
1da177e4 2621 return nfserr_stale_clientid;
1da177e4 2622 nfs4_lock_state();
21ab45a4 2623
8daae4dc 2624 conf = find_confirmed_client(clid, false, nn);
0a7ec377 2625 unconf = find_unconfirmed_client(clid, false, nn);
a186e767 2626 /*
8695b90a
BF
2627 * We try hard to give out unique clientid's, so if we get an
2628 * attempt to confirm the same clientid with a different cred,
2629 * there's a bug somewhere. Let's charitably assume it's our
2630 * bug.
a186e767 2631 */
8695b90a
BF
2632 status = nfserr_serverfault;
2633 if (unconf && !same_creds(&unconf->cl_cred, &rqstp->rq_cred))
2634 goto out;
2635 if (conf && !same_creds(&conf->cl_cred, &rqstp->rq_cred))
2636 goto out;
63db4632 2637 /* cases below refer to rfc 3530 section 14.2.34: */
90d700b7
BF
2638 if (!unconf || !same_verf(&confirm, &unconf->cl_confirm)) {
2639 if (conf && !unconf) /* case 2: probable retransmit */
1da177e4 2640 status = nfs_ok;
90d700b7
BF
2641 else /* case 4: client hasn't noticed we rebooted yet? */
2642 status = nfserr_stale_clientid;
2643 goto out;
2644 }
2645 status = nfs_ok;
2646 if (conf) { /* case 1: callback update */
8695b90a
BF
2647 nfsd4_change_callback(conf, &unconf->cl_cb_conn);
2648 nfsd4_probe_callback(conf);
2649 expire_client(unconf);
90d700b7 2650 } else { /* case 3: normal case; new or rebooted client */
382a62e7 2651 conf = find_confirmed_client_by_name(&unconf->cl_name, nn);
221a6876
BF
2652 if (conf) {
2653 status = mark_client_expired(conf);
2654 if (status)
2655 goto out;
8695b90a 2656 expire_client(conf);
221a6876 2657 }
8695b90a 2658 move_to_confirmed(unconf);
f3d03b92 2659 nfsd4_probe_callback(unconf);
08e8987c 2660 }
1da177e4 2661out:
1da177e4
LT
2662 nfs4_unlock_state();
2663 return status;
2664}
2665
32513b40
BF
2666static struct nfs4_file *nfsd4_alloc_file(void)
2667{
2668 return kmem_cache_alloc(file_slab, GFP_KERNEL);
2669}
2670
1da177e4 2671/* OPEN Share state helper functions */
32513b40 2672static void nfsd4_init_file(struct nfs4_file *fp, struct inode *ino)
1da177e4 2673{
1da177e4
LT
2674 unsigned int hashval = file_hashval(ino);
2675
950e0118
TM
2676 lockdep_assert_held(&state_lock);
2677
32513b40 2678 atomic_set(&fp->fi_ref, 1);
32513b40
BF
2679 INIT_LIST_HEAD(&fp->fi_stateids);
2680 INIT_LIST_HEAD(&fp->fi_delegations);
950e0118
TM
2681 ihold(ino);
2682 fp->fi_inode = ino;
32513b40
BF
2683 fp->fi_had_conflict = false;
2684 fp->fi_lease = NULL;
2685 memset(fp->fi_fds, 0, sizeof(fp->fi_fds));
2686 memset(fp->fi_access, 0, sizeof(fp->fi_access));
89876f8c 2687 hlist_add_head(&fp->fi_hash, &file_hashtbl[hashval]);
1da177e4
LT
2688}
2689
e8ff2a84 2690void
1da177e4
LT
2691nfsd4_free_slabs(void)
2692{
abf1135b
CH
2693 kmem_cache_destroy(openowner_slab);
2694 kmem_cache_destroy(lockowner_slab);
2695 kmem_cache_destroy(file_slab);
2696 kmem_cache_destroy(stateid_slab);
2697 kmem_cache_destroy(deleg_slab);
e60d4398 2698}
1da177e4 2699
72083396 2700int
e60d4398
N
2701nfsd4_init_slabs(void)
2702{
fe0750e5
BF
2703 openowner_slab = kmem_cache_create("nfsd4_openowners",
2704 sizeof(struct nfs4_openowner), 0, 0, NULL);
2705 if (openowner_slab == NULL)
abf1135b 2706 goto out;
fe0750e5 2707 lockowner_slab = kmem_cache_create("nfsd4_lockowners",
3c40794b 2708 sizeof(struct nfs4_lockowner), 0, 0, NULL);
fe0750e5 2709 if (lockowner_slab == NULL)
abf1135b 2710 goto out_free_openowner_slab;
e60d4398 2711 file_slab = kmem_cache_create("nfsd4_files",
20c2df83 2712 sizeof(struct nfs4_file), 0, 0, NULL);
e60d4398 2713 if (file_slab == NULL)
abf1135b 2714 goto out_free_lockowner_slab;
5ac049ac 2715 stateid_slab = kmem_cache_create("nfsd4_stateids",
dcef0413 2716 sizeof(struct nfs4_ol_stateid), 0, 0, NULL);
5ac049ac 2717 if (stateid_slab == NULL)
abf1135b 2718 goto out_free_file_slab;
5b2d21c1 2719 deleg_slab = kmem_cache_create("nfsd4_delegations",
20c2df83 2720 sizeof(struct nfs4_delegation), 0, 0, NULL);
5b2d21c1 2721 if (deleg_slab == NULL)
abf1135b 2722 goto out_free_stateid_slab;
e60d4398 2723 return 0;
abf1135b
CH
2724
2725out_free_stateid_slab:
2726 kmem_cache_destroy(stateid_slab);
2727out_free_file_slab:
2728 kmem_cache_destroy(file_slab);
2729out_free_lockowner_slab:
2730 kmem_cache_destroy(lockowner_slab);
2731out_free_openowner_slab:
2732 kmem_cache_destroy(openowner_slab);
2733out:
e60d4398
N
2734 dprintk("nfsd4: out of memory while initializing nfsv4\n");
2735 return -ENOMEM;
1da177e4
LT
2736}
2737
ff194bd9 2738static void init_nfs4_replay(struct nfs4_replay *rp)
1da177e4 2739{
ff194bd9
BF
2740 rp->rp_status = nfserr_serverfault;
2741 rp->rp_buflen = 0;
2742 rp->rp_buf = rp->rp_ibuf;
1da177e4
LT
2743}
2744
fe0750e5 2745static inline void *alloc_stateowner(struct kmem_cache *slab, struct xdr_netobj *owner, struct nfs4_client *clp)
ff194bd9 2746{
1da177e4 2747 struct nfs4_stateowner *sop;
1da177e4 2748
fe0750e5 2749 sop = kmem_cache_alloc(slab, GFP_KERNEL);
ff194bd9
BF
2750 if (!sop)
2751 return NULL;
2752
2753 sop->so_owner.data = kmemdup(owner->data, owner->len, GFP_KERNEL);
2754 if (!sop->so_owner.data) {
fe0750e5 2755 kmem_cache_free(slab, sop);
1da177e4 2756 return NULL;
ff194bd9
BF
2757 }
2758 sop->so_owner.len = owner->len;
2759
ea1da636 2760 INIT_LIST_HEAD(&sop->so_stateids);
ff194bd9
BF
2761 sop->so_client = clp;
2762 init_nfs4_replay(&sop->so_replay);
2763 return sop;
2764}
2765
fe0750e5 2766static void hash_openowner(struct nfs4_openowner *oo, struct nfs4_client *clp, unsigned int strhashval)
ff194bd9 2767{
9b531137
SK
2768 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
2769
2770 list_add(&oo->oo_owner.so_strhash, &nn->ownerstr_hashtbl[strhashval]);
fe0750e5 2771 list_add(&oo->oo_perclient, &clp->cl_openowners);
ff194bd9
BF
2772}
2773
fe0750e5 2774static struct nfs4_openowner *
13d6f66b 2775alloc_init_open_stateowner(unsigned int strhashval, struct nfsd4_open *open,
db24b3b4
JL
2776 struct nfsd4_compound_state *cstate)
2777{
13d6f66b 2778 struct nfs4_client *clp = cstate->clp;
fe0750e5 2779 struct nfs4_openowner *oo;
ff194bd9 2780
fe0750e5
BF
2781 oo = alloc_stateowner(openowner_slab, &open->op_owner, clp);
2782 if (!oo)
ff194bd9 2783 return NULL;
fe0750e5
BF
2784 oo->oo_owner.so_is_open_owner = 1;
2785 oo->oo_owner.so_seqid = open->op_seqid;
d29b20cd 2786 oo->oo_flags = NFS4_OO_NEW;
db24b3b4
JL
2787 if (nfsd4_has_session(cstate))
2788 oo->oo_flags |= NFS4_OO_CONFIRMED;
fe0750e5 2789 oo->oo_time = 0;
38c387b5 2790 oo->oo_last_closed_stid = NULL;
fe0750e5
BF
2791 INIT_LIST_HEAD(&oo->oo_close_lru);
2792 hash_openowner(oo, clp, strhashval);
2793 return oo;
1da177e4
LT
2794}
2795
996e0938 2796static void init_open_stateid(struct nfs4_ol_stateid *stp, struct nfs4_file *fp, struct nfsd4_open *open) {
fe0750e5 2797 struct nfs4_openowner *oo = open->op_openowner;
1da177e4 2798
3abdb607 2799 stp->st_stid.sc_type = NFS4_OPEN_STID;
3c87b9b7 2800 INIT_LIST_HEAD(&stp->st_locks);
fe0750e5 2801 list_add(&stp->st_perstateowner, &oo->oo_owner.so_stateids);
8beefa24 2802 list_add(&stp->st_perfile, &fp->fi_stateids);
fe0750e5 2803 stp->st_stateowner = &oo->oo_owner;
13cd2184 2804 get_nfs4_file(fp);
1da177e4 2805 stp->st_file = fp;
1da177e4
LT
2806 stp->st_access_bmap = 0;
2807 stp->st_deny_bmap = 0;
82c5ff1b 2808 set_access(open->op_share_access, stp);
ce0fc43c 2809 set_deny(open->op_share_deny, stp);
4c4cd222 2810 stp->st_openstp = NULL;
1da177e4
LT
2811}
2812
fd39ca9a 2813static void
73758fed 2814move_to_close_lru(struct nfs4_openowner *oo, struct net *net)
1da177e4 2815{
73758fed
SK
2816 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
2817
fe0750e5 2818 dprintk("NFSD: move_to_close_lru nfs4_openowner %p\n", oo);
1da177e4 2819
73758fed 2820 list_move_tail(&oo->oo_close_lru, &nn->close_lru);
fe0750e5 2821 oo->oo_time = get_seconds();
1da177e4
LT
2822}
2823
1da177e4 2824static int
599e0a22
BF
2825same_owner_str(struct nfs4_stateowner *sop, struct xdr_netobj *owner,
2826 clientid_t *clid)
2827{
2828 return (sop->so_owner.len == owner->len) &&
2829 0 == memcmp(sop->so_owner.data, owner->data, owner->len) &&
2830 (sop->so_client->cl_clientid.cl_id == clid->cl_id);
1da177e4
LT
2831}
2832
fe0750e5 2833static struct nfs4_openowner *
9b531137
SK
2834find_openstateowner_str(unsigned int hashval, struct nfsd4_open *open,
2835 bool sessions, struct nfsd_net *nn)
1da177e4 2836{
a50d2ad1
BF
2837 struct nfs4_stateowner *so;
2838 struct nfs4_openowner *oo;
d15c077e 2839 struct nfs4_client *clp;
1da177e4 2840
9b531137 2841 list_for_each_entry(so, &nn->ownerstr_hashtbl[hashval], so_strhash) {
16bfdaaf
BF
2842 if (!so->so_is_open_owner)
2843 continue;
a50d2ad1
BF
2844 if (same_owner_str(so, &open->op_owner, &open->op_clientid)) {
2845 oo = openowner(so);
d15c077e
BF
2846 clp = oo->oo_owner.so_client;
2847 if ((bool)clp->cl_minorversion != sessions)
2848 return NULL;
a50d2ad1
BF
2849 renew_client(oo->oo_owner.so_client);
2850 return oo;
2851 }
1da177e4
LT
2852 }
2853 return NULL;
2854}
2855
2856/* search file_hashtbl[] for file */
2857static struct nfs4_file *
950e0118 2858find_file_locked(struct inode *ino)
1da177e4
LT
2859{
2860 unsigned int hashval = file_hashval(ino);
2861 struct nfs4_file *fp;
2862
950e0118
TM
2863 lockdep_assert_held(&state_lock);
2864
89876f8c 2865 hlist_for_each_entry(fp, &file_hashtbl[hashval], fi_hash) {
13cd2184
N
2866 if (fp->fi_inode == ino) {
2867 get_nfs4_file(fp);
1da177e4 2868 return fp;
13cd2184 2869 }
1da177e4
LT
2870 }
2871 return NULL;
2872}
2873
950e0118
TM
2874static struct nfs4_file *
2875find_file(struct inode *ino)
2876{
2877 struct nfs4_file *fp;
2878
2879 spin_lock(&state_lock);
2880 fp = find_file_locked(ino);
2881 spin_unlock(&state_lock);
2882 return fp;
2883}
2884
2885static struct nfs4_file *
2886find_or_add_file(struct inode *ino, struct nfs4_file *new)
2887{
2888 struct nfs4_file *fp;
2889
2890 spin_lock(&state_lock);
2891 fp = find_file_locked(ino);
2892 if (fp == NULL) {
2893 nfsd4_init_file(new, ino);
2894 fp = new;
2895 }
2896 spin_unlock(&state_lock);
2897
2898 return fp;
2899}
2900
1da177e4
LT
2901/*
2902 * Called to check deny when READ with all zero stateid or
2903 * WRITE with all zero or all one stateid
2904 */
b37ad28b 2905static __be32
1da177e4
LT
2906nfs4_share_conflict(struct svc_fh *current_fh, unsigned int deny_type)
2907{
2908 struct inode *ino = current_fh->fh_dentry->d_inode;
2909 struct nfs4_file *fp;
dcef0413 2910 struct nfs4_ol_stateid *stp;
b37ad28b 2911 __be32 ret;
1da177e4 2912
1da177e4 2913 fp = find_file(ino);
13cd2184
N
2914 if (!fp)
2915 return nfs_ok;
b700949b 2916 ret = nfserr_locked;
1da177e4 2917 /* Search for conflicting share reservations */
13cd2184 2918 list_for_each_entry(stp, &fp->fi_stateids, st_perfile) {
ce0fc43c
JL
2919 if (test_deny(deny_type, stp) ||
2920 test_deny(NFS4_SHARE_DENY_BOTH, stp))
13cd2184 2921 goto out;
1da177e4 2922 }
13cd2184
N
2923 ret = nfs_ok;
2924out:
2925 put_nfs4_file(fp);
2926 return ret;
1da177e4
LT
2927}
2928
6b57d9c8 2929static void nfsd_break_one_deleg(struct nfs4_delegation *dp)
1da177e4 2930{
e8c69d17
BF
2931 struct nfs4_client *clp = dp->dl_stid.sc_client;
2932 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
2933
cdc97505 2934 lockdep_assert_held(&state_lock);
1da177e4
LT
2935 /* We're assuming the state code never drops its reference
2936 * without first removing the lease. Since we're in this lease
2937 * callback (and since the lease code is serialized by the kernel
2938 * lock) we know the server hasn't removed the lease yet, we know
2939 * it's safe to take a reference: */
2940 atomic_inc(&dp->dl_count);
2941
dff1399f
JL
2942 /*
2943 * If the dl_time != 0, then we know that it has already been
2944 * queued for a lease break. Don't queue it again.
2945 */
2946 if (dp->dl_time == 0) {
2947 list_add_tail(&dp->dl_recall_lru, &nn->del_recall_lru);
2948 dp->dl_time = get_seconds();
2949 }
1da177e4 2950
6282cd56
N
2951 block_delegations(&dp->dl_fh);
2952
6b57d9c8
BF
2953 nfsd4_cb_recall(dp);
2954}
2955
1c8c601a 2956/* Called from break_lease() with i_lock held. */
6b57d9c8
BF
2957static void nfsd_break_deleg_cb(struct file_lock *fl)
2958{
acfdf5c3
BF
2959 struct nfs4_file *fp = (struct nfs4_file *)fl->fl_owner;
2960 struct nfs4_delegation *dp;
6b57d9c8 2961
7fa10cd1
BF
2962 if (!fp) {
2963 WARN(1, "(%p)->fl_owner NULL\n", fl);
2964 return;
2965 }
2966 if (fp->fi_had_conflict) {
2967 WARN(1, "duplicate break on %p\n", fp);
2968 return;
2969 }
0272e1fd
BF
2970 /*
2971 * We don't want the locks code to timeout the lease for us;
acfdf5c3 2972 * we'll remove it ourself if a delegation isn't returned
6b57d9c8 2973 * in time:
0272e1fd
BF
2974 */
2975 fl->fl_break_time = 0;
1da177e4 2976
cdc97505 2977 spin_lock(&state_lock);
acfdf5c3
BF
2978 fp->fi_had_conflict = true;
2979 list_for_each_entry(dp, &fp->fi_delegations, dl_perfile)
2980 nfsd_break_one_deleg(dp);
cdc97505 2981 spin_unlock(&state_lock);
1da177e4
LT
2982}
2983
1da177e4
LT
2984static
2985int nfsd_change_deleg_cb(struct file_lock **onlist, int arg)
2986{
2987 if (arg & F_UNLCK)
2988 return lease_modify(onlist, arg);
2989 else
2990 return -EAGAIN;
2991}
2992
7b021967 2993static const struct lock_manager_operations nfsd_lease_mng_ops = {
8fb47a4f
BF
2994 .lm_break = nfsd_break_deleg_cb,
2995 .lm_change = nfsd_change_deleg_cb,
1da177e4
LT
2996};
2997
7a8711c9
BF
2998static __be32 nfsd4_check_seqid(struct nfsd4_compound_state *cstate, struct nfs4_stateowner *so, u32 seqid)
2999{
3000 if (nfsd4_has_session(cstate))
3001 return nfs_ok;
3002 if (seqid == so->so_seqid - 1)
3003 return nfserr_replay_me;
3004 if (seqid == so->so_seqid)
3005 return nfs_ok;
3006 return nfserr_bad_seqid;
3007}
1da177e4 3008
4b24ca7d
JL
3009static __be32 lookup_clientid(clientid_t *clid,
3010 struct nfsd4_compound_state *cstate,
3011 struct nfsd_net *nn)
3012{
3013 struct nfs4_client *found;
3014
3015 if (cstate->clp) {
3016 found = cstate->clp;
3017 if (!same_clid(&found->cl_clientid, clid))
3018 return nfserr_stale_clientid;
3019 return nfs_ok;
3020 }
3021
3022 if (STALE_CLIENTID(clid, nn))
3023 return nfserr_stale_clientid;
3024
3025 /*
3026 * For v4.1+ we get the client in the SEQUENCE op. If we don't have one
3027 * cached already then we know this is for is for v4.0 and "sessions"
3028 * will be false.
3029 */
3030 WARN_ON_ONCE(cstate->session);
3031 found = find_confirmed_client(clid, false, nn);
3032 if (!found)
3033 return nfserr_expired;
3034
3035 /* Cache the nfs4_client in cstate! */
3036 cstate->clp = found;
3037 atomic_inc(&found->cl_refcount);
3038 return nfs_ok;
3039}
3040
b37ad28b 3041__be32
6668958f 3042nfsd4_process_open1(struct nfsd4_compound_state *cstate,
3320fef1 3043 struct nfsd4_open *open, struct nfsd_net *nn)
1da177e4 3044{
1da177e4
LT
3045 clientid_t *clientid = &open->op_clientid;
3046 struct nfs4_client *clp = NULL;
3047 unsigned int strhashval;
fe0750e5 3048 struct nfs4_openowner *oo = NULL;
4cdc951b 3049 __be32 status;
1da177e4 3050
2c142baa 3051 if (STALE_CLIENTID(&open->op_clientid, nn))
1da177e4 3052 return nfserr_stale_clientid;
32513b40
BF
3053 /*
3054 * In case we need it later, after we've already created the
3055 * file and don't want to risk a further failure:
3056 */
3057 open->op_file = nfsd4_alloc_file();
3058 if (open->op_file == NULL)
3059 return nfserr_jukebox;
1da177e4 3060
2d91e895
TM
3061 status = lookup_clientid(clientid, cstate, nn);
3062 if (status)
3063 return status;
3064 clp = cstate->clp;
3065
16bfdaaf 3066 strhashval = ownerstr_hashval(clientid->cl_id, &open->op_owner);
9b531137 3067 oo = find_openstateowner_str(strhashval, open, cstate->minorversion, nn);
fe0750e5
BF
3068 open->op_openowner = oo;
3069 if (!oo) {
bcf130f9 3070 goto new_owner;
1da177e4 3071 }
dad1c067 3072 if (!(oo->oo_flags & NFS4_OO_CONFIRMED)) {
0f442aa2 3073 /* Replace unconfirmed owners without checking for replay. */
fe0750e5
BF
3074 release_openowner(oo);
3075 open->op_openowner = NULL;
bcf130f9 3076 goto new_owner;
0f442aa2 3077 }
4cdc951b
BF
3078 status = nfsd4_check_seqid(cstate, &oo->oo_owner, open->op_seqid);
3079 if (status)
3080 return status;
4cdc951b 3081 goto alloc_stateid;
bcf130f9 3082new_owner:
13d6f66b 3083 oo = alloc_init_open_stateowner(strhashval, open, cstate);
bcf130f9
BF
3084 if (oo == NULL)
3085 return nfserr_jukebox;
3086 open->op_openowner = oo;
4cdc951b
BF
3087alloc_stateid:
3088 open->op_stp = nfs4_alloc_stateid(clp);
3089 if (!open->op_stp)
3090 return nfserr_jukebox;
0f442aa2 3091 return nfs_ok;
1da177e4
LT
3092}
3093
b37ad28b 3094static inline __be32
4a6e43e6
N
3095nfs4_check_delegmode(struct nfs4_delegation *dp, int flags)
3096{
3097 if ((flags & WR_STATE) && (dp->dl_type == NFS4_OPEN_DELEGATE_READ))
3098 return nfserr_openmode;
3099 else
3100 return nfs_ok;
3101}
3102
f459e453 3103static int share_access_to_flags(u32 share_access)
52f4fb43 3104{
f459e453 3105 return share_access == NFS4_SHARE_ACCESS_READ ? RD_STATE : WR_STATE;
52f4fb43
N
3106}
3107
38c2f4b1 3108static struct nfs4_delegation *find_deleg_stateid(struct nfs4_client *cl, stateid_t *s)
24a0111e 3109{
f459e453 3110 struct nfs4_stid *ret;
24a0111e 3111
38c2f4b1 3112 ret = find_stateid_by_type(cl, s, NFS4_DELEG_STID);
f459e453
BF
3113 if (!ret)
3114 return NULL;
3115 return delegstateid(ret);
24a0111e
BF
3116}
3117
8b289b2c
BF
3118static bool nfsd4_is_deleg_cur(struct nfsd4_open *open)
3119{
3120 return open->op_claim_type == NFS4_OPEN_CLAIM_DELEGATE_CUR ||
3121 open->op_claim_type == NFS4_OPEN_CLAIM_DELEG_CUR_FH;
3122}
3123
b37ad28b 3124static __be32
41d22663 3125nfs4_check_deleg(struct nfs4_client *cl, struct nfsd4_open *open,
567d9829
N
3126 struct nfs4_delegation **dp)
3127{
3128 int flags;
b37ad28b 3129 __be32 status = nfserr_bad_stateid;
567d9829 3130
38c2f4b1 3131 *dp = find_deleg_stateid(cl, &open->op_delegate_stateid);
567d9829 3132 if (*dp == NULL)
c44c5eeb 3133 goto out;
24a0111e 3134 flags = share_access_to_flags(open->op_share_access);
567d9829
N
3135 status = nfs4_check_delegmode(*dp, flags);
3136 if (status)
3137 *dp = NULL;
c44c5eeb 3138out:
8b289b2c 3139 if (!nfsd4_is_deleg_cur(open))
c44c5eeb
N
3140 return nfs_ok;
3141 if (status)
3142 return status;
dad1c067 3143 open->op_openowner->oo_flags |= NFS4_OO_CONFIRMED;
c44c5eeb 3144 return nfs_ok;
567d9829
N
3145}
3146
b37ad28b 3147static __be32
dcef0413 3148nfs4_check_open(struct nfs4_file *fp, struct nfsd4_open *open, struct nfs4_ol_stateid **stpp)
1da177e4 3149{
dcef0413 3150 struct nfs4_ol_stateid *local;
fe0750e5 3151 struct nfs4_openowner *oo = open->op_openowner;
1da177e4 3152
8beefa24 3153 list_for_each_entry(local, &fp->fi_stateids, st_perfile) {
1da177e4
LT
3154 /* ignore lock owners */
3155 if (local->st_stateowner->so_is_open_owner == 0)
3156 continue;
3157 /* remember if we have seen this open owner */
fe0750e5 3158 if (local->st_stateowner == &oo->oo_owner)
1da177e4
LT
3159 *stpp = local;
3160 /* check for conflicting share reservations */
3161 if (!test_share(local, open))
77eaae8d 3162 return nfserr_share_denied;
1da177e4 3163 }
77eaae8d 3164 return nfs_ok;
1da177e4
LT
3165}
3166
21fb4016
BF
3167static inline int nfs4_access_to_access(u32 nfs4_access)
3168{
3169 int flags = 0;
3170
3171 if (nfs4_access & NFS4_SHARE_ACCESS_READ)
3172 flags |= NFSD_MAY_READ;
3173 if (nfs4_access & NFS4_SHARE_ACCESS_WRITE)
3174 flags |= NFSD_MAY_WRITE;
3175 return flags;
3176}
3177
7e6a72e5
CH
3178static inline __be32
3179nfsd4_truncate(struct svc_rqst *rqstp, struct svc_fh *fh,
3180 struct nfsd4_open *open)
3181{
3182 struct iattr iattr = {
3183 .ia_valid = ATTR_SIZE,
3184 .ia_size = 0,
3185 };
3186 if (!open->op_truncate)
3187 return 0;
3188 if (!(open->op_share_access & NFS4_SHARE_ACCESS_WRITE))
3189 return nfserr_inval;
3190 return nfsd_setattr(rqstp, fh, &iattr, 0, (time_t)0);
3191}
3192
0c12eaff
CB
3193static __be32 nfs4_get_vfs_file(struct svc_rqst *rqstp, struct nfs4_file *fp,
3194 struct svc_fh *cur_fh, struct nfsd4_open *open)
f9d7562f
BF
3195{
3196 __be32 status;
0c12eaff
CB
3197 int oflag = nfs4_access_to_omode(open->op_share_access);
3198 int access = nfs4_access_to_access(open->op_share_access);
3199
f9d7562f
BF
3200 if (!fp->fi_fds[oflag]) {
3201 status = nfsd_open(rqstp, cur_fh, S_IFREG, access,
3202 &fp->fi_fds[oflag]);
f9d7562f 3203 if (status)
7e6a72e5 3204 goto out;
f9d7562f
BF
3205 }
3206 nfs4_file_get_access(fp, oflag);
3207
7e6a72e5
CH
3208 status = nfsd4_truncate(rqstp, cur_fh, open);
3209 if (status)
3210 goto out_put_access;
3211
f9d7562f 3212 return nfs_ok;
f9d7562f 3213
7e6a72e5
CH
3214out_put_access:
3215 nfs4_file_put_access(fp, oflag);
3216out:
3217 return status;
1da177e4
LT
3218}
3219
b37ad28b 3220static __be32
dcef0413 3221nfs4_upgrade_open(struct svc_rqst *rqstp, struct nfs4_file *fp, struct svc_fh *cur_fh, struct nfs4_ol_stateid *stp, struct nfsd4_open *open)
1da177e4 3222{
b6d2f1ca 3223 u32 op_share_access = open->op_share_access;
b37ad28b 3224 __be32 status;
1da177e4 3225
7e6a72e5 3226 if (!test_access(op_share_access, stp))
0c12eaff 3227 status = nfs4_get_vfs_file(rqstp, fp, cur_fh, open);
7e6a72e5
CH
3228 else
3229 status = nfsd4_truncate(rqstp, cur_fh, open);
3230
3231 if (status)
1da177e4 3232 return status;
7e6a72e5 3233
1da177e4 3234 /* remember the open */
82c5ff1b 3235 set_access(op_share_access, stp);
ce0fc43c 3236 set_deny(open->op_share_deny, stp);
1da177e4
LT
3237 return nfs_ok;
3238}
3239
3240
1da177e4 3241static void
1255a8f3 3242nfs4_set_claim_prev(struct nfsd4_open *open, bool has_session)
1da177e4 3243{
dad1c067 3244 open->op_openowner->oo_flags |= NFS4_OO_CONFIRMED;
1da177e4
LT
3245}
3246
14a24e99
BF
3247/* Should we give out recallable state?: */
3248static bool nfsd4_cb_channel_good(struct nfs4_client *clp)
3249{
3250 if (clp->cl_cb_state == NFSD4_CB_UP)
3251 return true;
3252 /*
3253 * In the sessions case, since we don't have to establish a
3254 * separate connection for callbacks, we assume it's OK
3255 * until we hear otherwise:
3256 */
3257 return clp->cl_minorversion && clp->cl_cb_state == NFSD4_CB_UNKNOWN;
3258}
3259
22d38c4c
BF
3260static struct file_lock *nfs4_alloc_init_lease(struct nfs4_delegation *dp, int flag)
3261{
3262 struct file_lock *fl;
3263
3264 fl = locks_alloc_lock();
3265 if (!fl)
3266 return NULL;
3267 locks_init_lock(fl);
3268 fl->fl_lmops = &nfsd_lease_mng_ops;
617588d5 3269 fl->fl_flags = FL_DELEG;
22d38c4c
BF
3270 fl->fl_type = flag == NFS4_OPEN_DELEGATE_READ? F_RDLCK: F_WRLCK;
3271 fl->fl_end = OFFSET_MAX;
acfdf5c3 3272 fl->fl_owner = (fl_owner_t)(dp->dl_file);
22d38c4c 3273 fl->fl_pid = current->tgid;
22d38c4c
BF
3274 return fl;
3275}
3276
99c41515 3277static int nfs4_setlease(struct nfs4_delegation *dp)
edab9782 3278{
acfdf5c3 3279 struct nfs4_file *fp = dp->dl_file;
edab9782
BF
3280 struct file_lock *fl;
3281 int status;
3282
99c41515 3283 fl = nfs4_alloc_init_lease(dp, NFS4_OPEN_DELEGATE_READ);
edab9782
BF
3284 if (!fl)
3285 return -ENOMEM;
acfdf5c3 3286 fl->fl_file = find_readable_file(fp);
acfdf5c3 3287 status = vfs_setlease(fl->fl_file, fl->fl_type, &fl);
e873088f
BF
3288 if (status)
3289 goto out_free;
acfdf5c3 3290 fp->fi_lease = fl;
cb0942b8 3291 fp->fi_deleg_file = get_file(fl->fl_file);
acfdf5c3 3292 atomic_set(&fp->fi_delegees, 1);
cdc97505 3293 spin_lock(&state_lock);
931ee56c 3294 hash_delegation_locked(dp, fp);
cdc97505 3295 spin_unlock(&state_lock);
acfdf5c3 3296 return 0;
e873088f
BF
3297out_free:
3298 locks_free_lock(fl);
3299 return status;
acfdf5c3
BF
3300}
3301
bf7bd3e9 3302static int nfs4_set_delegation(struct nfs4_delegation *dp, struct nfs4_file *fp)
acfdf5c3 3303{
bf7bd3e9
BF
3304 if (fp->fi_had_conflict)
3305 return -EAGAIN;
3306 get_nfs4_file(fp);
3307 dp->dl_file = fp;
cbf7a75b
BF
3308 if (!fp->fi_lease)
3309 return nfs4_setlease(dp);
cdc97505 3310 spin_lock(&state_lock);
cbf7a75b 3311 atomic_inc(&fp->fi_delegees);
acfdf5c3 3312 if (fp->fi_had_conflict) {
cdc97505 3313 spin_unlock(&state_lock);
cbf7a75b 3314 return -EAGAIN;
acfdf5c3 3315 }
931ee56c 3316 hash_delegation_locked(dp, fp);
cdc97505 3317 spin_unlock(&state_lock);
edab9782
BF
3318 return 0;
3319}
3320
4aa8913c
BH
3321static void nfsd4_open_deleg_none_ext(struct nfsd4_open *open, int status)
3322{
3323 open->op_delegate_type = NFS4_OPEN_DELEGATE_NONE_EXT;
3324 if (status == -EAGAIN)
3325 open->op_why_no_deleg = WND4_CONTENTION;
3326 else {
3327 open->op_why_no_deleg = WND4_RESOURCE;
3328 switch (open->op_deleg_want) {
3329 case NFS4_SHARE_WANT_READ_DELEG:
3330 case NFS4_SHARE_WANT_WRITE_DELEG:
3331 case NFS4_SHARE_WANT_ANY_DELEG:
3332 break;
3333 case NFS4_SHARE_WANT_CANCEL:
3334 open->op_why_no_deleg = WND4_CANCELLED;
3335 break;
3336 case NFS4_SHARE_WANT_NO_DELEG:
063b0fb9 3337 WARN_ON_ONCE(1);
4aa8913c
BH
3338 }
3339 }
3340}
3341
1da177e4
LT
3342/*
3343 * Attempt to hand out a delegation.
99c41515
BF
3344 *
3345 * Note we don't support write delegations, and won't until the vfs has
3346 * proper support for them.
1da177e4
LT
3347 */
3348static void
5ccb0066
SK
3349nfs4_open_delegation(struct net *net, struct svc_fh *fh,
3350 struct nfsd4_open *open, struct nfs4_ol_stateid *stp)
1da177e4
LT
3351{
3352 struct nfs4_delegation *dp;
fe0750e5 3353 struct nfs4_openowner *oo = container_of(stp->st_stateowner, struct nfs4_openowner, oo_owner);
14a24e99 3354 int cb_up;
99c41515 3355 int status = 0;
1da177e4 3356
fe0750e5 3357 cb_up = nfsd4_cb_channel_good(oo->oo_owner.so_client);
7b190fec
N
3358 open->op_recall = 0;
3359 switch (open->op_claim_type) {
3360 case NFS4_OPEN_CLAIM_PREVIOUS:
2bf23875 3361 if (!cb_up)
7b190fec 3362 open->op_recall = 1;
99c41515
BF
3363 if (open->op_delegate_type != NFS4_OPEN_DELEGATE_READ)
3364 goto out_no_deleg;
7b190fec
N
3365 break;
3366 case NFS4_OPEN_CLAIM_NULL:
ed47b062 3367 case NFS4_OPEN_CLAIM_FH:
99c41515
BF
3368 /*
3369 * Let's not give out any delegations till everyone's
3370 * had the chance to reclaim theirs....
3371 */
5ccb0066 3372 if (locks_in_grace(net))
99c41515 3373 goto out_no_deleg;
dad1c067 3374 if (!cb_up || !(oo->oo_flags & NFS4_OO_CONFIRMED))
99c41515 3375 goto out_no_deleg;
9a0590ae
SD
3376 /*
3377 * Also, if the file was opened for write or
3378 * create, there's a good chance the client's
3379 * about to write to it, resulting in an
3380 * immediate recall (since we don't support
3381 * write delegations):
3382 */
7b190fec 3383 if (open->op_share_access & NFS4_SHARE_ACCESS_WRITE)
99c41515
BF
3384 goto out_no_deleg;
3385 if (open->op_create == NFS4_OPEN_CREATE)
3386 goto out_no_deleg;
7b190fec
N
3387 break;
3388 default:
99c41515 3389 goto out_no_deleg;
7b190fec 3390 }
99c41515 3391 dp = alloc_init_deleg(oo->oo_owner.so_client, stp, fh);
dd239cc0
BF
3392 if (dp == NULL)
3393 goto out_no_deleg;
bf7bd3e9 3394 status = nfs4_set_delegation(dp, stp->st_file);
edab9782 3395 if (status)
dd239cc0 3396 goto out_free;
1da177e4 3397
d5477a8d 3398 memcpy(&open->op_delegate_stateid, &dp->dl_stid.sc_stateid, sizeof(dp->dl_stid.sc_stateid));
1da177e4 3399
8c10cbdb 3400 dprintk("NFSD: delegation stateid=" STATEID_FMT "\n",
d5477a8d 3401 STATEID_VAL(&dp->dl_stid.sc_stateid));
99c41515 3402 open->op_delegate_type = NFS4_OPEN_DELEGATE_READ;
dd239cc0
BF
3403 return;
3404out_free:
cbf7a75b 3405 destroy_delegation(dp);
dd239cc0 3406out_no_deleg:
99c41515
BF
3407 open->op_delegate_type = NFS4_OPEN_DELEGATE_NONE;
3408 if (open->op_claim_type == NFS4_OPEN_CLAIM_PREVIOUS &&
d08d32e6 3409 open->op_delegate_type != NFS4_OPEN_DELEGATE_NONE) {
99c41515 3410 dprintk("NFSD: WARNING: refusing delegation reclaim\n");
d08d32e6
BF
3411 open->op_recall = 1;
3412 }
99c41515
BF
3413
3414 /* 4.1 client asking for a delegation? */
3415 if (open->op_deleg_want)
3416 nfsd4_open_deleg_none_ext(open, status);
3417 return;
1da177e4
LT
3418}
3419
e27f49c3
BH
3420static void nfsd4_deleg_xgrade_none_ext(struct nfsd4_open *open,
3421 struct nfs4_delegation *dp)
3422{
3423 if (open->op_deleg_want == NFS4_SHARE_WANT_READ_DELEG &&
3424 dp->dl_type == NFS4_OPEN_DELEGATE_WRITE) {
3425 open->op_delegate_type = NFS4_OPEN_DELEGATE_NONE_EXT;
3426 open->op_why_no_deleg = WND4_NOT_SUPP_DOWNGRADE;
3427 } else if (open->op_deleg_want == NFS4_SHARE_WANT_WRITE_DELEG &&
3428 dp->dl_type == NFS4_OPEN_DELEGATE_WRITE) {
3429 open->op_delegate_type = NFS4_OPEN_DELEGATE_NONE_EXT;
3430 open->op_why_no_deleg = WND4_NOT_SUPP_UPGRADE;
3431 }
3432 /* Otherwise the client must be confused wanting a delegation
3433 * it already has, therefore we don't return
3434 * NFS4_OPEN_DELEGATE_NONE_EXT and reason.
3435 */
3436}
3437
1da177e4
LT
3438/*
3439 * called with nfs4_lock_state() held.
3440 */
b37ad28b 3441__be32
1da177e4
LT
3442nfsd4_process_open2(struct svc_rqst *rqstp, struct svc_fh *current_fh, struct nfsd4_open *open)
3443{
6668958f 3444 struct nfsd4_compoundres *resp = rqstp->rq_resp;
38c2f4b1 3445 struct nfs4_client *cl = open->op_openowner->oo_owner.so_client;
1da177e4
LT
3446 struct nfs4_file *fp = NULL;
3447 struct inode *ino = current_fh->fh_dentry->d_inode;
dcef0413 3448 struct nfs4_ol_stateid *stp = NULL;
567d9829 3449 struct nfs4_delegation *dp = NULL;
b37ad28b 3450 __be32 status;
1da177e4 3451
1da177e4
LT
3452 /*
3453 * Lookup file; if found, lookup stateid and check open request,
3454 * and check for delegations in the process of being recalled.
3455 * If not found, create the nfs4_file struct
3456 */
950e0118
TM
3457 fp = find_or_add_file(ino, open->op_file);
3458 if (fp != open->op_file) {
1da177e4
LT
3459 if ((status = nfs4_check_open(fp, open, &stp)))
3460 goto out;
41d22663 3461 status = nfs4_check_deleg(cl, open, &dp);
c44c5eeb
N
3462 if (status)
3463 goto out;
1da177e4 3464 } else {
950e0118 3465 open->op_file = NULL;
c44c5eeb 3466 status = nfserr_bad_stateid;
8b289b2c 3467 if (nfsd4_is_deleg_cur(open))
c44c5eeb 3468 goto out;
3e772463 3469 status = nfserr_jukebox;
1da177e4
LT
3470 }
3471
3472 /*
3473 * OPEN the file, or upgrade an existing OPEN.
3474 * If truncate fails, the OPEN fails.
3475 */
3476 if (stp) {
3477 /* Stateid was found, this is an OPEN upgrade */
f9d7562f 3478 status = nfs4_upgrade_open(rqstp, fp, current_fh, stp, open);
1da177e4
LT
3479 if (status)
3480 goto out;
3481 } else {
4cdc951b 3482 status = nfs4_get_vfs_file(rqstp, fp, current_fh, open);
567d9829 3483 if (status)
1da177e4 3484 goto out;
4cdc951b
BF
3485 stp = open->op_stp;
3486 open->op_stp = NULL;
996e0938 3487 init_open_stateid(stp, fp, open);
1da177e4 3488 }
dcef0413
BF
3489 update_stateid(&stp->st_stid.sc_stateid);
3490 memcpy(&open->op_stateid, &stp->st_stid.sc_stateid, sizeof(stateid_t));
1da177e4 3491
d24433cd 3492 if (nfsd4_has_session(&resp->cstate)) {
d24433cd
BH
3493 if (open->op_deleg_want & NFS4_SHARE_WANT_NO_DELEG) {
3494 open->op_delegate_type = NFS4_OPEN_DELEGATE_NONE_EXT;
3495 open->op_why_no_deleg = WND4_NOT_WANTED;
3496 goto nodeleg;
3497 }
3498 }
3499
1da177e4
LT
3500 /*
3501 * Attempt to hand out a delegation. No error return, because the
3502 * OPEN succeeds even if we fail.
3503 */
5ccb0066 3504 nfs4_open_delegation(SVC_NET(rqstp), current_fh, open, stp);
d24433cd 3505nodeleg:
1da177e4
LT
3506 status = nfs_ok;
3507
8c10cbdb 3508 dprintk("%s: stateid=" STATEID_FMT "\n", __func__,
dcef0413 3509 STATEID_VAL(&stp->st_stid.sc_stateid));
1da177e4 3510out:
d24433cd
BH
3511 /* 4.1 client trying to upgrade/downgrade delegation? */
3512 if (open->op_delegate_type == NFS4_OPEN_DELEGATE_NONE && dp &&
e27f49c3
BH
3513 open->op_deleg_want)
3514 nfsd4_deleg_xgrade_none_ext(open, dp);
d24433cd 3515
13cd2184
N
3516 if (fp)
3517 put_nfs4_file(fp);
37515177 3518 if (status == 0 && open->op_claim_type == NFS4_OPEN_CLAIM_PREVIOUS)
1255a8f3 3519 nfs4_set_claim_prev(open, nfsd4_has_session(&resp->cstate));
1da177e4
LT
3520 /*
3521 * To finish the open response, we just need to set the rflags.
3522 */
3523 open->op_rflags = NFS4_OPEN_RESULT_LOCKTYPE_POSIX;
dad1c067 3524 if (!(open->op_openowner->oo_flags & NFS4_OO_CONFIRMED) &&
6668958f 3525 !nfsd4_has_session(&resp->cstate))
1da177e4
LT
3526 open->op_rflags |= NFS4_OPEN_RESULT_CONFIRM;
3527
3528 return status;
3529}
3530
d29b20cd
BF
3531void nfsd4_cleanup_open_state(struct nfsd4_open *open, __be32 status)
3532{
3533 if (open->op_openowner) {
3534 struct nfs4_openowner *oo = open->op_openowner;
3535
3536 if (!list_empty(&oo->oo_owner.so_stateids))
3537 list_del_init(&oo->oo_close_lru);
3538 if (oo->oo_flags & NFS4_OO_NEW) {
3539 if (status) {
3540 release_openowner(oo);
3541 open->op_openowner = NULL;
3542 } else
3543 oo->oo_flags &= ~NFS4_OO_NEW;
3544 }
3545 }
32513b40
BF
3546 if (open->op_file)
3547 nfsd4_free_file(open->op_file);
4cdc951b 3548 if (open->op_stp)
ef79859e 3549 free_generic_stateid(open->op_stp);
d29b20cd
BF
3550}
3551
b37ad28b 3552__be32
b591480b
BF
3553nfsd4_renew(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
3554 clientid_t *clid)
1da177e4
LT
3555{
3556 struct nfs4_client *clp;
b37ad28b 3557 __be32 status;
7f2210fa 3558 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
1da177e4
LT
3559
3560 nfs4_lock_state();
3561 dprintk("process_renew(%08x/%08x): starting\n",
3562 clid->cl_boot, clid->cl_id);
4b24ca7d 3563 status = lookup_clientid(clid, cstate, nn);
9b2ef62b 3564 if (status)
1da177e4 3565 goto out;
4b24ca7d 3566 clp = cstate->clp;
1da177e4 3567 status = nfserr_cb_path_down;
ea1da636 3568 if (!list_empty(&clp->cl_delegations)
77a3569d 3569 && clp->cl_cb_state != NFSD4_CB_UP)
1da177e4
LT
3570 goto out;
3571 status = nfs_ok;
3572out:
3573 nfs4_unlock_state();
3574 return status;
3575}
3576
a76b4319 3577static void
12760c66 3578nfsd4_end_grace(struct nfsd_net *nn)
a76b4319 3579{
33dcc481 3580 /* do nothing if grace period already ended */
a51c84ed 3581 if (nn->grace_ended)
33dcc481
JL
3582 return;
3583
a76b4319 3584 dprintk("NFSD: end of grace period\n");
a51c84ed 3585 nn->grace_ended = true;
12760c66 3586 nfsd4_record_grace_done(nn, nn->boot_time);
5e1533c7 3587 locks_end_grace(&nn->nfsd4_manager);
e46b498c
BF
3588 /*
3589 * Now that every NFSv4 client has had the chance to recover and
3590 * to see the (possibly new, possibly shorter) lease time, we
3591 * can safely set the next grace time to the current lease time:
3592 */
5284b44e 3593 nn->nfsd4_grace = nn->nfsd4_lease;
a76b4319
N
3594}
3595
fd39ca9a 3596static time_t
09121281 3597nfs4_laundromat(struct nfsd_net *nn)
1da177e4
LT
3598{
3599 struct nfs4_client *clp;
fe0750e5 3600 struct nfs4_openowner *oo;
1da177e4
LT
3601 struct nfs4_delegation *dp;
3602 struct list_head *pos, *next, reaplist;
3d733711 3603 time_t cutoff = get_seconds() - nn->nfsd4_lease;
a832e7ae 3604 time_t t, new_timeo = nn->nfsd4_lease;
1da177e4
LT
3605
3606 nfs4_lock_state();
3607
3608 dprintk("NFSD: laundromat service - starting\n");
12760c66 3609 nfsd4_end_grace(nn);
36acb66b 3610 INIT_LIST_HEAD(&reaplist);
c9a49628 3611 spin_lock(&nn->client_lock);
5ed58bb2 3612 list_for_each_safe(pos, next, &nn->client_lru) {
1da177e4
LT
3613 clp = list_entry(pos, struct nfs4_client, cl_lru);
3614 if (time_after((unsigned long)clp->cl_time, (unsigned long)cutoff)) {
3615 t = clp->cl_time - cutoff;
a832e7ae 3616 new_timeo = min(new_timeo, t);
1da177e4
LT
3617 break;
3618 }
221a6876 3619 if (mark_client_expired_locked(clp)) {
d7682988
BH
3620 dprintk("NFSD: client in use (clientid %08x)\n",
3621 clp->cl_clientid.cl_id);
3622 continue;
3623 }
221a6876 3624 list_move(&clp->cl_lru, &reaplist);
36acb66b 3625 }
c9a49628 3626 spin_unlock(&nn->client_lock);
36acb66b
BH
3627 list_for_each_safe(pos, next, &reaplist) {
3628 clp = list_entry(pos, struct nfs4_client, cl_lru);
1da177e4
LT
3629 dprintk("NFSD: purging unused client (clientid %08x)\n",
3630 clp->cl_clientid.cl_id);
3631 expire_client(clp);
3632 }
cdc97505 3633 spin_lock(&state_lock);
e8c69d17 3634 list_for_each_safe(pos, next, &nn->del_recall_lru) {
1da177e4 3635 dp = list_entry (pos, struct nfs4_delegation, dl_recall_lru);
4e37a7c2
SK
3636 if (net_generic(dp->dl_stid.sc_client->net, nfsd_net_id) != nn)
3637 continue;
1da177e4 3638 if (time_after((unsigned long)dp->dl_time, (unsigned long)cutoff)) {
a832e7ae
JL
3639 t = dp->dl_time - cutoff;
3640 new_timeo = min(new_timeo, t);
1da177e4
LT
3641 break;
3642 }
1da177e4
LT
3643 list_move(&dp->dl_recall_lru, &reaplist);
3644 }
cdc97505 3645 spin_unlock(&state_lock);
1da177e4
LT
3646 list_for_each_safe(pos, next, &reaplist) {
3647 dp = list_entry (pos, struct nfs4_delegation, dl_recall_lru);
3bd64a5b 3648 revoke_delegation(dp);
1da177e4 3649 }
73758fed 3650 list_for_each_safe(pos, next, &nn->close_lru) {
fe0750e5
BF
3651 oo = container_of(pos, struct nfs4_openowner, oo_close_lru);
3652 if (time_after((unsigned long)oo->oo_time, (unsigned long)cutoff)) {
a832e7ae
JL
3653 t = oo->oo_time - cutoff;
3654 new_timeo = min(new_timeo, t);
1da177e4
LT
3655 break;
3656 }
fe0750e5 3657 release_openowner(oo);
1da177e4 3658 }
a832e7ae 3659 new_timeo = max_t(time_t, new_timeo, NFSD_LAUNDROMAT_MINTIMEOUT);
1da177e4 3660 nfs4_unlock_state();
a832e7ae 3661 return new_timeo;
1da177e4
LT
3662}
3663
a254b246
HH
3664static struct workqueue_struct *laundry_wq;
3665static void laundromat_main(struct work_struct *);
a254b246
HH
3666
3667static void
09121281 3668laundromat_main(struct work_struct *laundry)
1da177e4
LT
3669{
3670 time_t t;
09121281
SK
3671 struct delayed_work *dwork = container_of(laundry, struct delayed_work,
3672 work);
3673 struct nfsd_net *nn = container_of(dwork, struct nfsd_net,
3674 laundromat_work);
1da177e4 3675
09121281 3676 t = nfs4_laundromat(nn);
1da177e4 3677 dprintk("NFSD: laundromat_main - sleeping for %ld seconds\n", t);
09121281 3678 queue_delayed_work(laundry_wq, &nn->laundromat_work, t*HZ);
1da177e4
LT
3679}
3680
f7a4d872 3681static inline __be32 nfs4_check_fh(struct svc_fh *fhp, struct nfs4_ol_stateid *stp)
1da177e4 3682{
f7a4d872
BF
3683 if (fhp->fh_dentry->d_inode != stp->st_file->fi_inode)
3684 return nfserr_bad_stateid;
3685 return nfs_ok;
1da177e4
LT
3686}
3687
1da177e4 3688static inline int
82c5ff1b 3689access_permit_read(struct nfs4_ol_stateid *stp)
1da177e4 3690{
82c5ff1b
JL
3691 return test_access(NFS4_SHARE_ACCESS_READ, stp) ||
3692 test_access(NFS4_SHARE_ACCESS_BOTH, stp) ||
3693 test_access(NFS4_SHARE_ACCESS_WRITE, stp);
1da177e4
LT
3694}
3695
3696static inline int
82c5ff1b 3697access_permit_write(struct nfs4_ol_stateid *stp)
1da177e4 3698{
82c5ff1b
JL
3699 return test_access(NFS4_SHARE_ACCESS_WRITE, stp) ||
3700 test_access(NFS4_SHARE_ACCESS_BOTH, stp);
1da177e4
LT
3701}
3702
3703static
dcef0413 3704__be32 nfs4_check_openmode(struct nfs4_ol_stateid *stp, int flags)
1da177e4 3705{
b37ad28b 3706 __be32 status = nfserr_openmode;
1da177e4 3707
02921914
BF
3708 /* For lock stateid's, we test the parent open, not the lock: */
3709 if (stp->st_openstp)
3710 stp = stp->st_openstp;
82c5ff1b 3711 if ((flags & WR_STATE) && !access_permit_write(stp))
1da177e4 3712 goto out;
82c5ff1b 3713 if ((flags & RD_STATE) && !access_permit_read(stp))
1da177e4
LT
3714 goto out;
3715 status = nfs_ok;
3716out:
3717 return status;
3718}
3719
b37ad28b 3720static inline __be32
5ccb0066 3721check_special_stateids(struct net *net, svc_fh *current_fh, stateid_t *stateid, int flags)
1da177e4 3722{
203a8c8e 3723 if (ONE_STATEID(stateid) && (flags & RD_STATE))
1da177e4 3724 return nfs_ok;
5ccb0066 3725 else if (locks_in_grace(net)) {
25985edc 3726 /* Answer in remaining cases depends on existence of
1da177e4
LT
3727 * conflicting state; so we must wait out the grace period. */
3728 return nfserr_grace;
3729 } else if (flags & WR_STATE)
3730 return nfs4_share_conflict(current_fh,
3731 NFS4_SHARE_DENY_WRITE);
3732 else /* (flags & RD_STATE) && ZERO_STATEID(stateid) */
3733 return nfs4_share_conflict(current_fh,
3734 NFS4_SHARE_DENY_READ);
3735}
3736
3737/*
3738 * Allow READ/WRITE during grace period on recovered state only for files
3739 * that are not able to provide mandatory locking.
3740 */
3741static inline int
5ccb0066 3742grace_disallows_io(struct net *net, struct inode *inode)
1da177e4 3743{
5ccb0066 3744 return locks_in_grace(net) && mandatory_lock(inode);
1da177e4
LT
3745}
3746
81b82965
BF
3747/* Returns true iff a is later than b: */
3748static bool stateid_generation_after(stateid_t *a, stateid_t *b)
3749{
1a9357f4 3750 return (s32)(a->si_generation - b->si_generation) > 0;
81b82965
BF
3751}
3752
57b7b43b 3753static __be32 check_stateid_generation(stateid_t *in, stateid_t *ref, bool has_session)
0836f587 3754{
6668958f
AA
3755 /*
3756 * When sessions are used the stateid generation number is ignored
3757 * when it is zero.
3758 */
28dde241 3759 if (has_session && in->si_generation == 0)
81b82965
BF
3760 return nfs_ok;
3761
3762 if (in->si_generation == ref->si_generation)
3763 return nfs_ok;
6668958f 3764
0836f587 3765 /* If the client sends us a stateid from the future, it's buggy: */
81b82965 3766 if (stateid_generation_after(in, ref))
0836f587
BF
3767 return nfserr_bad_stateid;
3768 /*
81b82965
BF
3769 * However, we could see a stateid from the past, even from a
3770 * non-buggy client. For example, if the client sends a lock
3771 * while some IO is outstanding, the lock may bump si_generation
3772 * while the IO is still in flight. The client could avoid that
3773 * situation by waiting for responses on all the IO requests,
3774 * but better performance may result in retrying IO that
3775 * receives an old_stateid error if requests are rarely
3776 * reordered in flight:
0836f587 3777 */
81b82965 3778 return nfserr_old_stateid;
0836f587
BF
3779}
3780
7df302f7 3781static __be32 nfsd4_validate_stateid(struct nfs4_client *cl, stateid_t *stateid)
17456804 3782{
97b7e3b6
BF
3783 struct nfs4_stid *s;
3784 struct nfs4_ol_stateid *ols;
3785 __be32 status;
17456804 3786
7df302f7
CL
3787 if (ZERO_STATEID(stateid) || ONE_STATEID(stateid))
3788 return nfserr_bad_stateid;
3789 /* Client debugging aid. */
3790 if (!same_clid(&stateid->si_opaque.so_clid, &cl->cl_clientid)) {
3791 char addr_str[INET6_ADDRSTRLEN];
3792 rpc_ntop((struct sockaddr *)&cl->cl_addr, addr_str,
3793 sizeof(addr_str));
3794 pr_warn_ratelimited("NFSD: client %s testing state ID "
3795 "with incorrect client ID\n", addr_str);
3796 return nfserr_bad_stateid;
3797 }
38c2f4b1 3798 s = find_stateid(cl, stateid);
97b7e3b6 3799 if (!s)
7df302f7 3800 return nfserr_bad_stateid;
36279ac1 3801 status = check_stateid_generation(stateid, &s->sc_stateid, 1);
17456804 3802 if (status)
97b7e3b6 3803 return status;
23340032
BF
3804 switch (s->sc_type) {
3805 case NFS4_DELEG_STID:
97b7e3b6 3806 return nfs_ok;
3bd64a5b
BF
3807 case NFS4_REVOKED_DELEG_STID:
3808 return nfserr_deleg_revoked;
23340032
BF
3809 case NFS4_OPEN_STID:
3810 case NFS4_LOCK_STID:
3811 ols = openlockstateid(s);
3812 if (ols->st_stateowner->so_is_open_owner
3813 && !(openowner(ols->st_stateowner)->oo_flags
3814 & NFS4_OO_CONFIRMED))
3815 return nfserr_bad_stateid;
97b7e3b6 3816 return nfs_ok;
23340032
BF
3817 default:
3818 printk("unknown stateid type %x\n", s->sc_type);
3819 case NFS4_CLOSED_STID:
97b7e3b6 3820 return nfserr_bad_stateid;
23340032 3821 }
17456804
BS
3822}
3823
2dd6e458
TM
3824static __be32
3825nfsd4_lookup_stateid(struct nfsd4_compound_state *cstate,
3826 stateid_t *stateid, unsigned char typemask,
3827 struct nfs4_stid **s, struct nfsd_net *nn)
38c2f4b1 3828{
0eb6f20a 3829 __be32 status;
38c2f4b1
BF
3830
3831 if (ZERO_STATEID(stateid) || ONE_STATEID(stateid))
3832 return nfserr_bad_stateid;
4b24ca7d 3833 status = lookup_clientid(&stateid->si_opaque.so_clid, cstate, nn);
a8a7c677 3834 if (status == nfserr_stale_clientid) {
4b24ca7d 3835 if (cstate->session)
a8a7c677 3836 return nfserr_bad_stateid;
38c2f4b1 3837 return nfserr_stale_stateid;
a8a7c677 3838 }
0eb6f20a
BF
3839 if (status)
3840 return status;
4b24ca7d 3841 *s = find_stateid_by_type(cstate->clp, stateid, typemask);
38c2f4b1
BF
3842 if (!*s)
3843 return nfserr_bad_stateid;
3844 return nfs_ok;
38c2f4b1
BF
3845}
3846
1da177e4
LT
3847/*
3848* Checks for stateid operations
3849*/
b37ad28b 3850__be32
5ccb0066 3851nfs4_preprocess_stateid_op(struct net *net, struct nfsd4_compound_state *cstate,
dd453dfd 3852 stateid_t *stateid, int flags, struct file **filpp)
1da177e4 3853{
69064a27 3854 struct nfs4_stid *s;
dcef0413 3855 struct nfs4_ol_stateid *stp = NULL;
1da177e4 3856 struct nfs4_delegation *dp = NULL;
dd453dfd 3857 struct svc_fh *current_fh = &cstate->current_fh;
1da177e4 3858 struct inode *ino = current_fh->fh_dentry->d_inode;
3320fef1 3859 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
14bcab1a 3860 struct file *file = NULL;
b37ad28b 3861 __be32 status;
1da177e4 3862
1da177e4
LT
3863 if (filpp)
3864 *filpp = NULL;
3865
5ccb0066 3866 if (grace_disallows_io(net, ino))
1da177e4
LT
3867 return nfserr_grace;
3868
3869 if (ZERO_STATEID(stateid) || ONE_STATEID(stateid))
5ccb0066 3870 return check_special_stateids(net, current_fh, stateid, flags);
1da177e4 3871
14bcab1a
TM
3872 nfs4_lock_state();
3873
2dd6e458 3874 status = nfsd4_lookup_stateid(cstate, stateid,
db24b3b4 3875 NFS4_DELEG_STID|NFS4_OPEN_STID|NFS4_LOCK_STID,
2dd6e458 3876 &s, nn);
38c2f4b1 3877 if (status)
14bcab1a 3878 goto out;
69064a27
BF
3879 status = check_stateid_generation(stateid, &s->sc_stateid, nfsd4_has_session(cstate));
3880 if (status)
3881 goto out;
f7a4d872
BF
3882 switch (s->sc_type) {
3883 case NFS4_DELEG_STID:
69064a27 3884 dp = delegstateid(s);
dc9bf700
BF
3885 status = nfs4_check_delegmode(dp, flags);
3886 if (status)
3887 goto out;
43b0178e 3888 if (filpp) {
14bcab1a
TM
3889 file = dp->dl_file->fi_deleg_file;
3890 if (!file) {
063b0fb9
BF
3891 WARN_ON_ONCE(1);
3892 status = nfserr_serverfault;
3893 goto out;
3894 }
43b0178e 3895 }
f7a4d872
BF
3896 break;
3897 case NFS4_OPEN_STID:
3898 case NFS4_LOCK_STID:
69064a27 3899 stp = openlockstateid(s);
f7a4d872
BF
3900 status = nfs4_check_fh(current_fh, stp);
3901 if (status)
1da177e4 3902 goto out;
fe0750e5 3903 if (stp->st_stateowner->so_is_open_owner
dad1c067 3904 && !(openowner(stp->st_stateowner)->oo_flags & NFS4_OO_CONFIRMED))
1da177e4 3905 goto out;
a4455be0
BF
3906 status = nfs4_check_openmode(stp, flags);
3907 if (status)
1da177e4 3908 goto out;
f9d7562f
BF
3909 if (filpp) {
3910 if (flags & RD_STATE)
14bcab1a 3911 file = find_readable_file(stp->st_file);
f9d7562f 3912 else
14bcab1a 3913 file = find_writeable_file(stp->st_file);
f9d7562f 3914 }
f7a4d872
BF
3915 break;
3916 default:
14bcab1a
TM
3917 status = nfserr_bad_stateid;
3918 goto out;
1da177e4
LT
3919 }
3920 status = nfs_ok;
14bcab1a
TM
3921 if (file)
3922 *filpp = get_file(file);
1da177e4 3923out:
14bcab1a 3924 nfs4_unlock_state();
1da177e4
LT
3925 return status;
3926}
3927
e1ca12df 3928static __be32
dcef0413 3929nfsd4_free_lock_stateid(struct nfs4_ol_stateid *stp)
e1ca12df 3930{
a1b8ff4c
BF
3931 struct nfs4_lockowner *lo = lockowner(stp->st_stateowner);
3932
3933 if (check_for_locks(stp->st_file, lo))
e1ca12df 3934 return nfserr_locks_held;
c53530da 3935 release_lockowner_if_empty(lo);
e1ca12df
BS
3936 return nfs_ok;
3937}
3938
17456804
BS
3939/*
3940 * Test if the stateid is valid
3941 */
3942__be32
3943nfsd4_test_stateid(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
3944 struct nfsd4_test_stateid *test_stateid)
3945{
03cfb420
BS
3946 struct nfsd4_test_stateid_id *stateid;
3947 struct nfs4_client *cl = cstate->session->se_client;
3948
3949 nfs4_lock_state();
3950 list_for_each_entry(stateid, &test_stateid->ts_stateid_list, ts_id_list)
7df302f7
CL
3951 stateid->ts_id_status =
3952 nfsd4_validate_stateid(cl, &stateid->ts_id_stateid);
03cfb420
BS
3953 nfs4_unlock_state();
3954
17456804
BS
3955 return nfs_ok;
3956}
3957
e1ca12df
BS
3958__be32
3959nfsd4_free_stateid(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
3960 struct nfsd4_free_stateid *free_stateid)
3961{
3962 stateid_t *stateid = &free_stateid->fr_stateid;
2da1cec7 3963 struct nfs4_stid *s;
3bd64a5b 3964 struct nfs4_delegation *dp;
38c2f4b1 3965 struct nfs4_client *cl = cstate->session->se_client;
2da1cec7 3966 __be32 ret = nfserr_bad_stateid;
e1ca12df
BS
3967
3968 nfs4_lock_state();
38c2f4b1 3969 s = find_stateid(cl, stateid);
2da1cec7 3970 if (!s)
81b82965 3971 goto out;
2da1cec7
BF
3972 switch (s->sc_type) {
3973 case NFS4_DELEG_STID:
e1ca12df
BS
3974 ret = nfserr_locks_held;
3975 goto out;
2da1cec7
BF
3976 case NFS4_OPEN_STID:
3977 case NFS4_LOCK_STID:
3978 ret = check_stateid_generation(stateid, &s->sc_stateid, 1);
3979 if (ret)
3980 goto out;
3981 if (s->sc_type == NFS4_LOCK_STID)
3982 ret = nfsd4_free_lock_stateid(openlockstateid(s));
3983 else
3984 ret = nfserr_locks_held;
f7a4d872 3985 break;
3bd64a5b
BF
3986 case NFS4_REVOKED_DELEG_STID:
3987 dp = delegstateid(s);
3988 destroy_revoked_delegation(dp);
3989 ret = nfs_ok;
3990 break;
f7a4d872
BF
3991 default:
3992 ret = nfserr_bad_stateid;
e1ca12df 3993 }
e1ca12df
BS
3994out:
3995 nfs4_unlock_state();
3996 return ret;
3997}
3998
4c4cd222
N
3999static inline int
4000setlkflg (int type)
4001{
4002 return (type == NFS4_READW_LT || type == NFS4_READ_LT) ?
4003 RD_STATE : WR_STATE;
4004}
1da177e4 4005
dcef0413 4006static __be32 nfs4_seqid_op_checks(struct nfsd4_compound_state *cstate, stateid_t *stateid, u32 seqid, struct nfs4_ol_stateid *stp)
c0a5d93e
BF
4007{
4008 struct svc_fh *current_fh = &cstate->current_fh;
4009 struct nfs4_stateowner *sop = stp->st_stateowner;
4010 __be32 status;
4011
c0a5d93e
BF
4012 status = nfsd4_check_seqid(cstate, sop, seqid);
4013 if (status)
4014 return status;
3bd64a5b
BF
4015 if (stp->st_stid.sc_type == NFS4_CLOSED_STID
4016 || stp->st_stid.sc_type == NFS4_REVOKED_DELEG_STID)
f7a4d872
BF
4017 /*
4018 * "Closed" stateid's exist *only* to return
3bd64a5b
BF
4019 * nfserr_replay_me from the previous step, and
4020 * revoked delegations are kept only for free_stateid.
f7a4d872
BF
4021 */
4022 return nfserr_bad_stateid;
4023 status = check_stateid_generation(stateid, &stp->st_stid.sc_stateid, nfsd4_has_session(cstate));
4024 if (status)
4025 return status;
4026 return nfs4_check_fh(current_fh, stp);
c0a5d93e
BF
4027}
4028
1da177e4
LT
4029/*
4030 * Checks for sequence id mutating operations.
4031 */
b37ad28b 4032static __be32
dd453dfd 4033nfs4_preprocess_seqid_op(struct nfsd4_compound_state *cstate, u32 seqid,
2288d0e3 4034 stateid_t *stateid, char typemask,
3320fef1
SK
4035 struct nfs4_ol_stateid **stpp,
4036 struct nfsd_net *nn)
1da177e4 4037{
0836f587 4038 __be32 status;
38c2f4b1 4039 struct nfs4_stid *s;
e17f99b7 4040 struct nfs4_ol_stateid *stp = NULL;
1da177e4 4041
8c10cbdb
BH
4042 dprintk("NFSD: %s: seqid=%d stateid = " STATEID_FMT "\n", __func__,
4043 seqid, STATEID_VAL(stateid));
3a4f98bb 4044
1da177e4 4045 *stpp = NULL;
2dd6e458 4046 status = nfsd4_lookup_stateid(cstate, stateid, typemask, &s, nn);
c0a5d93e
BF
4047 if (status)
4048 return status;
e17f99b7 4049 stp = openlockstateid(s);
3d74e6a5 4050 if (!nfsd4_has_session(cstate))
e17f99b7 4051 cstate->replay_owner = stp->st_stateowner;
1da177e4 4052
e17f99b7
TM
4053 status = nfs4_seqid_op_checks(cstate, stateid, seqid, stp);
4054 if (!status)
4055 *stpp = stp;
4056 return status;
c0a5d93e 4057}
39325bd0 4058
3320fef1
SK
4059static __be32 nfs4_preprocess_confirmed_seqid_op(struct nfsd4_compound_state *cstate, u32 seqid,
4060 stateid_t *stateid, struct nfs4_ol_stateid **stpp, struct nfsd_net *nn)
c0a5d93e
BF
4061{
4062 __be32 status;
4063 struct nfs4_openowner *oo;
1da177e4 4064
c0a5d93e 4065 status = nfs4_preprocess_seqid_op(cstate, seqid, stateid,
3320fef1 4066 NFS4_OPEN_STID, stpp, nn);
7a8711c9
BF
4067 if (status)
4068 return status;
c0a5d93e 4069 oo = openowner((*stpp)->st_stateowner);
dad1c067 4070 if (!(oo->oo_flags & NFS4_OO_CONFIRMED))
3a4f98bb 4071 return nfserr_bad_stateid;
3a4f98bb 4072 return nfs_ok;
1da177e4
LT
4073}
4074
b37ad28b 4075__be32
ca364317 4076nfsd4_open_confirm(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
a4f1706a 4077 struct nfsd4_open_confirm *oc)
1da177e4 4078{
b37ad28b 4079 __be32 status;
fe0750e5 4080 struct nfs4_openowner *oo;
dcef0413 4081 struct nfs4_ol_stateid *stp;
3320fef1 4082 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
1da177e4 4083
a6a9f18f
AV
4084 dprintk("NFSD: nfsd4_open_confirm on file %pd\n",
4085 cstate->current_fh.fh_dentry);
1da177e4 4086
ca364317 4087 status = fh_verify(rqstp, &cstate->current_fh, S_IFREG, 0);
a8cddc5d
BF
4088 if (status)
4089 return status;
1da177e4
LT
4090
4091 nfs4_lock_state();
4092
9072d5c6 4093 status = nfs4_preprocess_seqid_op(cstate,
ca364317 4094 oc->oc_seqid, &oc->oc_req_stateid,
3320fef1 4095 NFS4_OPEN_STID, &stp, nn);
9072d5c6 4096 if (status)
68b66e82 4097 goto out;
fe0750e5 4098 oo = openowner(stp->st_stateowner);
68b66e82 4099 status = nfserr_bad_stateid;
dad1c067 4100 if (oo->oo_flags & NFS4_OO_CONFIRMED)
68b66e82 4101 goto out;
dad1c067 4102 oo->oo_flags |= NFS4_OO_CONFIRMED;
dcef0413
BF
4103 update_stateid(&stp->st_stid.sc_stateid);
4104 memcpy(&oc->oc_resp_stateid, &stp->st_stid.sc_stateid, sizeof(stateid_t));
8c10cbdb 4105 dprintk("NFSD: %s: success, seqid=%d stateid=" STATEID_FMT "\n",
dcef0413 4106 __func__, oc->oc_seqid, STATEID_VAL(&stp->st_stid.sc_stateid));
c7b9a459 4107
2a4317c5 4108 nfsd4_client_record_create(oo->oo_owner.so_client);
68b66e82 4109 status = nfs_ok;
1da177e4 4110out:
9411b1d4 4111 nfsd4_bump_seqid(cstate, status);
5ec094c1
BF
4112 if (!cstate->replay_owner)
4113 nfs4_unlock_state();
1da177e4
LT
4114 return status;
4115}
4116
6409a5a6 4117static inline void nfs4_stateid_downgrade_bit(struct nfs4_ol_stateid *stp, u32 access)
1da177e4 4118{
82c5ff1b 4119 if (!test_access(access, stp))
6409a5a6
BF
4120 return;
4121 nfs4_file_put_access(stp->st_file, nfs4_access_to_omode(access));
82c5ff1b 4122 clear_access(access, stp);
6409a5a6 4123}
f197c271 4124
6409a5a6
BF
4125static inline void nfs4_stateid_downgrade(struct nfs4_ol_stateid *stp, u32 to_access)
4126{
4127 switch (to_access) {
4128 case NFS4_SHARE_ACCESS_READ:
4129 nfs4_stateid_downgrade_bit(stp, NFS4_SHARE_ACCESS_WRITE);
4130 nfs4_stateid_downgrade_bit(stp, NFS4_SHARE_ACCESS_BOTH);
4131 break;
4132 case NFS4_SHARE_ACCESS_WRITE:
4133 nfs4_stateid_downgrade_bit(stp, NFS4_SHARE_ACCESS_READ);
4134 nfs4_stateid_downgrade_bit(stp, NFS4_SHARE_ACCESS_BOTH);
4135 break;
4136 case NFS4_SHARE_ACCESS_BOTH:
4137 break;
4138 default:
063b0fb9 4139 WARN_ON_ONCE(1);
1da177e4
LT
4140 }
4141}
4142
4143static void
ce0fc43c 4144reset_union_bmap_deny(unsigned long deny, struct nfs4_ol_stateid *stp)
1da177e4
LT
4145{
4146 int i;
4147 for (i = 0; i < 4; i++) {
4148 if ((i & deny) != i)
ce0fc43c 4149 clear_deny(i, stp);
1da177e4
LT
4150 }
4151}
4152
b37ad28b 4153__be32
ca364317
BF
4154nfsd4_open_downgrade(struct svc_rqst *rqstp,
4155 struct nfsd4_compound_state *cstate,
a4f1706a 4156 struct nfsd4_open_downgrade *od)
1da177e4 4157{
b37ad28b 4158 __be32 status;
dcef0413 4159 struct nfs4_ol_stateid *stp;
3320fef1 4160 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
1da177e4 4161
a6a9f18f
AV
4162 dprintk("NFSD: nfsd4_open_downgrade on file %pd\n",
4163 cstate->current_fh.fh_dentry);
1da177e4 4164
c30e92df 4165 /* We don't yet support WANT bits: */
2c8bd7e0
BH
4166 if (od->od_deleg_want)
4167 dprintk("NFSD: %s: od_deleg_want=0x%x ignored\n", __func__,
4168 od->od_deleg_want);
1da177e4
LT
4169
4170 nfs4_lock_state();
c0a5d93e 4171 status = nfs4_preprocess_confirmed_seqid_op(cstate, od->od_seqid,
3320fef1 4172 &od->od_stateid, &stp, nn);
9072d5c6 4173 if (status)
1da177e4 4174 goto out;
1da177e4 4175 status = nfserr_inval;
82c5ff1b
JL
4176 if (!test_access(od->od_share_access, stp)) {
4177 dprintk("NFSD: access not a subset current bitmap: 0x%lx, input access=%08x\n",
1da177e4
LT
4178 stp->st_access_bmap, od->od_share_access);
4179 goto out;
4180 }
ce0fc43c 4181 if (!test_deny(od->od_share_deny, stp)) {
1da177e4
LT
4182 dprintk("NFSD:deny not a subset current bitmap: 0x%lx, input deny=%08x\n",
4183 stp->st_deny_bmap, od->od_share_deny);
4184 goto out;
4185 }
6409a5a6 4186 nfs4_stateid_downgrade(stp, od->od_share_access);
1da177e4 4187
ce0fc43c 4188 reset_union_bmap_deny(od->od_share_deny, stp);
1da177e4 4189
dcef0413
BF
4190 update_stateid(&stp->st_stid.sc_stateid);
4191 memcpy(&od->od_stateid, &stp->st_stid.sc_stateid, sizeof(stateid_t));
1da177e4
LT
4192 status = nfs_ok;
4193out:
9411b1d4 4194 nfsd4_bump_seqid(cstate, status);
5ec094c1
BF
4195 if (!cstate->replay_owner)
4196 nfs4_unlock_state();
1da177e4
LT
4197 return status;
4198}
4199
f7a4d872
BF
4200static void nfsd4_close_open_stateid(struct nfs4_ol_stateid *s)
4201{
acf9295b
TM
4202 struct nfs4_client *clp = s->st_stid.sc_client;
4203 struct nfs4_openowner *oo = openowner(s->st_stateowner);
4204
f7a4d872 4205 s->st_stid.sc_type = NFS4_CLOSED_STID;
acf9295b
TM
4206 unhash_open_stateid(s);
4207
4208 if (clp->cl_minorversion) {
4209 free_generic_stateid(s);
4210 if (list_empty(&oo->oo_owner.so_stateids))
4211 release_openowner(oo);
4212 } else {
4213 oo->oo_last_closed_stid = s;
4214 /*
4215 * In the 4.0 case we need to keep the owners around a
4216 * little while to handle CLOSE replay.
4217 */
4218 if (list_empty(&oo->oo_owner.so_stateids))
4219 move_to_close_lru(oo, clp->net);
4220 }
38c387b5
BF
4221}
4222
1da177e4
LT
4223/*
4224 * nfs4_unlock_state() called after encode
4225 */
b37ad28b 4226__be32
ca364317 4227nfsd4_close(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
a4f1706a 4228 struct nfsd4_close *close)
1da177e4 4229{
b37ad28b 4230 __be32 status;
dcef0413 4231 struct nfs4_ol_stateid *stp;
3320fef1
SK
4232 struct net *net = SVC_NET(rqstp);
4233 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
1da177e4 4234
a6a9f18f
AV
4235 dprintk("NFSD: nfsd4_close on file %pd\n",
4236 cstate->current_fh.fh_dentry);
1da177e4
LT
4237
4238 nfs4_lock_state();
f7a4d872
BF
4239 status = nfs4_preprocess_seqid_op(cstate, close->cl_seqid,
4240 &close->cl_stateid,
4241 NFS4_OPEN_STID|NFS4_CLOSED_STID,
3320fef1 4242 &stp, nn);
9411b1d4 4243 nfsd4_bump_seqid(cstate, status);
9072d5c6 4244 if (status)
1da177e4 4245 goto out;
dcef0413
BF
4246 update_stateid(&stp->st_stid.sc_stateid);
4247 memcpy(&close->cl_stateid, &stp->st_stid.sc_stateid, sizeof(stateid_t));
1da177e4 4248
f7a4d872 4249 nfsd4_close_open_stateid(stp);
1da177e4 4250out:
5ec094c1
BF
4251 if (!cstate->replay_owner)
4252 nfs4_unlock_state();
1da177e4
LT
4253 return status;
4254}
4255
b37ad28b 4256__be32
ca364317
BF
4257nfsd4_delegreturn(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
4258 struct nfsd4_delegreturn *dr)
1da177e4 4259{
203a8c8e
BF
4260 struct nfs4_delegation *dp;
4261 stateid_t *stateid = &dr->dr_stateid;
38c2f4b1 4262 struct nfs4_stid *s;
b37ad28b 4263 __be32 status;
3320fef1 4264 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
1da177e4 4265
ca364317 4266 if ((status = fh_verify(rqstp, &cstate->current_fh, S_IFREG, 0)))
203a8c8e 4267 return status;
1da177e4
LT
4268
4269 nfs4_lock_state();
2dd6e458 4270 status = nfsd4_lookup_stateid(cstate, stateid, NFS4_DELEG_STID, &s, nn);
38c2f4b1 4271 if (status)
203a8c8e 4272 goto out;
38c2f4b1 4273 dp = delegstateid(s);
d5477a8d 4274 status = check_stateid_generation(stateid, &dp->dl_stid.sc_stateid, nfsd4_has_session(cstate));
203a8c8e
BF
4275 if (status)
4276 goto out;
203a8c8e 4277
3bd64a5b 4278 destroy_delegation(dp);
1da177e4 4279out:
203a8c8e
BF
4280 nfs4_unlock_state();
4281
1da177e4
LT
4282 return status;
4283}
4284
4285
1da177e4 4286#define LOFF_OVERFLOW(start, len) ((u64)(len) > ~(u64)(start))
1da177e4 4287
87df4de8
BH
4288static inline u64
4289end_offset(u64 start, u64 len)
4290{
4291 u64 end;
4292
4293 end = start + len;
4294 return end >= start ? end: NFS4_MAX_UINT64;
4295}
4296
4297/* last octet in a range */
4298static inline u64
4299last_byte_offset(u64 start, u64 len)
4300{
4301 u64 end;
4302
063b0fb9 4303 WARN_ON_ONCE(!len);
87df4de8
BH
4304 end = start + len;
4305 return end > start ? end - 1: NFS4_MAX_UINT64;
4306}
4307
1da177e4
LT
4308/*
4309 * TODO: Linux file offsets are _signed_ 64-bit quantities, which means that
4310 * we can't properly handle lock requests that go beyond the (2^63 - 1)-th
4311 * byte, because of sign extension problems. Since NFSv4 calls for 64-bit
4312 * locking, this prevents us from being completely protocol-compliant. The
4313 * real solution to this problem is to start using unsigned file offsets in
4314 * the VFS, but this is a very deep change!
4315 */
4316static inline void
4317nfs4_transform_lock_offset(struct file_lock *lock)
4318{
4319 if (lock->fl_start < 0)
4320 lock->fl_start = OFFSET_MAX;
4321 if (lock->fl_end < 0)
4322 lock->fl_end = OFFSET_MAX;
4323}
4324
d5b9026a
N
4325/* Hack!: For now, we're defining this just so we can use a pointer to it
4326 * as a unique cookie to identify our (NFSv4's) posix locks. */
7b021967 4327static const struct lock_manager_operations nfsd_posix_mng_ops = {
d5b9026a 4328};
1da177e4
LT
4329
4330static inline void
4331nfs4_set_lock_denied(struct file_lock *fl, struct nfsd4_lock_denied *deny)
4332{
fe0750e5 4333 struct nfs4_lockowner *lo;
1da177e4 4334
d5b9026a 4335 if (fl->fl_lmops == &nfsd_posix_mng_ops) {
fe0750e5
BF
4336 lo = (struct nfs4_lockowner *) fl->fl_owner;
4337 deny->ld_owner.data = kmemdup(lo->lo_owner.so_owner.data,
4338 lo->lo_owner.so_owner.len, GFP_KERNEL);
7c13f344
BF
4339 if (!deny->ld_owner.data)
4340 /* We just don't care that much */
4341 goto nevermind;
fe0750e5
BF
4342 deny->ld_owner.len = lo->lo_owner.so_owner.len;
4343 deny->ld_clientid = lo->lo_owner.so_client->cl_clientid;
d5b9026a 4344 } else {
7c13f344
BF
4345nevermind:
4346 deny->ld_owner.len = 0;
4347 deny->ld_owner.data = NULL;
d5b9026a
N
4348 deny->ld_clientid.cl_boot = 0;
4349 deny->ld_clientid.cl_id = 0;
1da177e4
LT
4350 }
4351 deny->ld_start = fl->fl_start;
87df4de8
BH
4352 deny->ld_length = NFS4_MAX_UINT64;
4353 if (fl->fl_end != NFS4_MAX_UINT64)
1da177e4
LT
4354 deny->ld_length = fl->fl_end - fl->fl_start + 1;
4355 deny->ld_type = NFS4_READ_LT;
4356 if (fl->fl_type != F_RDLCK)
4357 deny->ld_type = NFS4_WRITE_LT;
4358}
4359
fe0750e5 4360static struct nfs4_lockowner *
b3c32bcd
TM
4361find_lockowner_str(clientid_t *clid, struct xdr_netobj *owner,
4362 struct nfsd_net *nn)
1da177e4 4363{
b3c32bcd
TM
4364 unsigned int strhashval = ownerstr_hashval(clid->cl_id, owner);
4365 struct nfs4_stateowner *so;
1da177e4 4366
b3c32bcd
TM
4367 list_for_each_entry(so, &nn->ownerstr_hashtbl[strhashval], so_strhash) {
4368 if (so->so_is_open_owner)
4369 continue;
4370 if (!same_owner_str(so, owner, clid))
4371 continue;
4372 return lockowner(so);
1da177e4
LT
4373 }
4374 return NULL;
4375}
4376
4377/*
4378 * Alloc a lock owner structure.
4379 * Called in nfsd4_lock - therefore, OPEN and OPEN_CONFIRM (if needed) has
25985edc 4380 * occurred.
1da177e4 4381 *
16bfdaaf 4382 * strhashval = ownerstr_hashval
1da177e4 4383 */
fe0750e5 4384static struct nfs4_lockowner *
dcef0413 4385alloc_init_lock_stateowner(unsigned int strhashval, struct nfs4_client *clp, struct nfs4_ol_stateid *open_stp, struct nfsd4_lock *lock) {
fe0750e5 4386 struct nfs4_lockowner *lo;
b3c32bcd 4387 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
1da177e4 4388
fe0750e5
BF
4389 lo = alloc_stateowner(lockowner_slab, &lock->lk_new_owner, clp);
4390 if (!lo)
1da177e4 4391 return NULL;
fe0750e5
BF
4392 INIT_LIST_HEAD(&lo->lo_owner.so_stateids);
4393 lo->lo_owner.so_is_open_owner = 0;
b59e3c0e
NB
4394 /* It is the openowner seqid that will be incremented in encode in the
4395 * case of new lockowners; so increment the lock seqid manually: */
fe0750e5 4396 lo->lo_owner.so_seqid = lock->lk_new_lock_seqid + 1;
b3c32bcd 4397 list_add(&lo->lo_owner.so_strhash, &nn->ownerstr_hashtbl[strhashval]);
fe0750e5 4398 return lo;
1da177e4
LT
4399}
4400
dcef0413
BF
4401static struct nfs4_ol_stateid *
4402alloc_init_lock_stateid(struct nfs4_lockowner *lo, struct nfs4_file *fp, struct nfs4_ol_stateid *open_stp)
1da177e4 4403{
dcef0413 4404 struct nfs4_ol_stateid *stp;
d3b313a4 4405 struct nfs4_client *clp = lo->lo_owner.so_client;
1da177e4 4406
996e0938 4407 stp = nfs4_alloc_stateid(clp);
5ac049ac 4408 if (stp == NULL)
6136d2b4 4409 return NULL;
3abdb607 4410 stp->st_stid.sc_type = NFS4_LOCK_STID;
8beefa24 4411 list_add(&stp->st_perfile, &fp->fi_stateids);
fe0750e5
BF
4412 list_add(&stp->st_perstateowner, &lo->lo_owner.so_stateids);
4413 stp->st_stateowner = &lo->lo_owner;
13cd2184 4414 get_nfs4_file(fp);
1da177e4 4415 stp->st_file = fp;
0997b173 4416 stp->st_access_bmap = 0;
1da177e4 4417 stp->st_deny_bmap = open_stp->st_deny_bmap;
4c4cd222 4418 stp->st_openstp = open_stp;
3c87b9b7 4419 list_add(&stp->st_locks, &open_stp->st_locks);
1da177e4
LT
4420 return stp;
4421}
4422
c53530da
JL
4423static struct nfs4_ol_stateid *
4424find_lock_stateid(struct nfs4_lockowner *lo, struct nfs4_file *fp)
4425{
4426 struct nfs4_ol_stateid *lst;
4427
4428 list_for_each_entry(lst, &lo->lo_owner.so_stateids, st_perstateowner) {
4429 if (lst->st_file == fp)
4430 return lst;
4431 }
4432 return NULL;
4433}
4434
4435
fd39ca9a 4436static int
1da177e4
LT
4437check_lock_length(u64 offset, u64 length)
4438{
87df4de8 4439 return ((length == 0) || ((length != NFS4_MAX_UINT64) &&
1da177e4
LT
4440 LOFF_OVERFLOW(offset, length)));
4441}
4442
dcef0413 4443static void get_lock_access(struct nfs4_ol_stateid *lock_stp, u32 access)
0997b173
BF
4444{
4445 struct nfs4_file *fp = lock_stp->st_file;
4446 int oflag = nfs4_access_to_omode(access);
4447
82c5ff1b 4448 if (test_access(access, lock_stp))
0997b173
BF
4449 return;
4450 nfs4_file_get_access(fp, oflag);
82c5ff1b 4451 set_access(access, lock_stp);
0997b173
BF
4452}
4453
2355c596 4454static __be32 lookup_or_create_lock_state(struct nfsd4_compound_state *cstate, struct nfs4_ol_stateid *ost, struct nfsd4_lock *lock, struct nfs4_ol_stateid **lst, bool *new)
64a284d0
BF
4455{
4456 struct nfs4_file *fi = ost->st_file;
4457 struct nfs4_openowner *oo = openowner(ost->st_stateowner);
4458 struct nfs4_client *cl = oo->oo_owner.so_client;
4459 struct nfs4_lockowner *lo;
4460 unsigned int strhashval;
20e9e2bc 4461 struct nfsd_net *nn = net_generic(cl->net, nfsd_net_id);
64a284d0 4462
b3c32bcd 4463 lo = find_lockowner_str(&cl->cl_clientid, &lock->v.new.owner, nn);
c53530da
JL
4464 if (!lo) {
4465 strhashval = ownerstr_hashval(cl->cl_clientid.cl_id,
4466 &lock->v.new.owner);
4467 lo = alloc_init_lock_stateowner(strhashval, cl, ost, lock);
4468 if (lo == NULL)
4469 return nfserr_jukebox;
4470 } else {
4471 /* with an existing lockowner, seqids must be the same */
4472 if (!cstate->minorversion &&
4473 lock->lk_new_lock_seqid != lo->lo_owner.so_seqid)
64a284d0 4474 return nfserr_bad_seqid;
64a284d0 4475 }
c53530da
JL
4476
4477 *lst = find_lock_stateid(lo, fi);
64a284d0 4478 if (*lst == NULL) {
c53530da
JL
4479 *lst = alloc_init_lock_stateid(lo, fi, ost);
4480 if (*lst == NULL) {
4481 release_lockowner_if_empty(lo);
4482 return nfserr_jukebox;
4483 }
4484 *new = true;
64a284d0 4485 }
64a284d0
BF
4486 return nfs_ok;
4487}
4488
1da177e4
LT
4489/*
4490 * LOCK operation
4491 */
b37ad28b 4492__be32
ca364317 4493nfsd4_lock(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
a4f1706a 4494 struct nfsd4_lock *lock)
1da177e4 4495{
fe0750e5
BF
4496 struct nfs4_openowner *open_sop = NULL;
4497 struct nfs4_lockowner *lock_sop = NULL;
dcef0413 4498 struct nfs4_ol_stateid *lock_stp;
7d947842 4499 struct file *filp = NULL;
21179d81
JL
4500 struct file_lock *file_lock = NULL;
4501 struct file_lock *conflock = NULL;
b37ad28b 4502 __be32 status = 0;
64a284d0 4503 bool new_state = false;
b34f27aa 4504 int lkflg;
b8dd7b9a 4505 int err;
3320fef1
SK
4506 struct net *net = SVC_NET(rqstp);
4507 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
1da177e4
LT
4508
4509 dprintk("NFSD: nfsd4_lock: start=%Ld length=%Ld\n",
4510 (long long) lock->lk_offset,
4511 (long long) lock->lk_length);
4512
1da177e4
LT
4513 if (check_lock_length(lock->lk_offset, lock->lk_length))
4514 return nfserr_inval;
4515
ca364317 4516 if ((status = fh_verify(rqstp, &cstate->current_fh,
8837abca 4517 S_IFREG, NFSD_MAY_LOCK))) {
a6f6ef2f
AA
4518 dprintk("NFSD: nfsd4_lock: permission denied!\n");
4519 return status;
4520 }
4521
1da177e4
LT
4522 nfs4_lock_state();
4523
4524 if (lock->lk_is_new) {
dcef0413 4525 struct nfs4_ol_stateid *open_stp = NULL;
684e5638
BF
4526
4527 if (nfsd4_has_session(cstate))
4528 /* See rfc 5661 18.10.3: given clientid is ignored: */
4529 memcpy(&lock->v.new.clientid,
4530 &cstate->session->se_client->cl_clientid,
4531 sizeof(clientid_t));
4532
1da177e4 4533 status = nfserr_stale_clientid;
2c142baa 4534 if (STALE_CLIENTID(&lock->lk_new_clientid, nn))
1da177e4 4535 goto out;
1da177e4 4536
1da177e4 4537 /* validate and update open stateid and open seqid */
c0a5d93e 4538 status = nfs4_preprocess_confirmed_seqid_op(cstate,
1da177e4
LT
4539 lock->lk_new_open_seqid,
4540 &lock->lk_new_open_stateid,
3320fef1 4541 &open_stp, nn);
37515177 4542 if (status)
1da177e4 4543 goto out;
fe0750e5 4544 open_sop = openowner(open_stp->st_stateowner);
b34f27aa 4545 status = nfserr_bad_stateid;
684e5638 4546 if (!same_clid(&open_sop->oo_owner.so_client->cl_clientid,
b34f27aa
BF
4547 &lock->v.new.clientid))
4548 goto out;
64a284d0
BF
4549 status = lookup_or_create_lock_state(cstate, open_stp, lock,
4550 &lock_stp, &new_state);
e1aaa891 4551 } else
dd453dfd 4552 status = nfs4_preprocess_seqid_op(cstate,
fe0750e5
BF
4553 lock->lk_old_lock_seqid,
4554 &lock->lk_old_lock_stateid,
3320fef1 4555 NFS4_LOCK_STID, &lock_stp, nn);
e1aaa891
BF
4556 if (status)
4557 goto out;
64a284d0 4558 lock_sop = lockowner(lock_stp->st_stateowner);
1da177e4 4559
b34f27aa
BF
4560 lkflg = setlkflg(lock->lk_type);
4561 status = nfs4_check_openmode(lock_stp, lkflg);
4562 if (status)
4563 goto out;
4564
0dd395dc 4565 status = nfserr_grace;
3320fef1 4566 if (locks_in_grace(net) && !lock->lk_reclaim)
0dd395dc
N
4567 goto out;
4568 status = nfserr_no_grace;
3320fef1 4569 if (!locks_in_grace(net) && lock->lk_reclaim)
0dd395dc
N
4570 goto out;
4571
21179d81
JL
4572 file_lock = locks_alloc_lock();
4573 if (!file_lock) {
4574 dprintk("NFSD: %s: unable to allocate lock!\n", __func__);
4575 status = nfserr_jukebox;
4576 goto out;
4577 }
4578
4579 locks_init_lock(file_lock);
1da177e4
LT
4580 switch (lock->lk_type) {
4581 case NFS4_READ_LT:
4582 case NFS4_READW_LT:
0997b173
BF
4583 filp = find_readable_file(lock_stp->st_file);
4584 if (filp)
4585 get_lock_access(lock_stp, NFS4_SHARE_ACCESS_READ);
21179d81 4586 file_lock->fl_type = F_RDLCK;
529d7b2a 4587 break;
1da177e4
LT
4588 case NFS4_WRITE_LT:
4589 case NFS4_WRITEW_LT:
0997b173
BF
4590 filp = find_writeable_file(lock_stp->st_file);
4591 if (filp)
4592 get_lock_access(lock_stp, NFS4_SHARE_ACCESS_WRITE);
21179d81 4593 file_lock->fl_type = F_WRLCK;
529d7b2a 4594 break;
1da177e4
LT
4595 default:
4596 status = nfserr_inval;
4597 goto out;
4598 }
f9d7562f
BF
4599 if (!filp) {
4600 status = nfserr_openmode;
4601 goto out;
4602 }
21179d81
JL
4603 file_lock->fl_owner = (fl_owner_t)lock_sop;
4604 file_lock->fl_pid = current->tgid;
4605 file_lock->fl_file = filp;
4606 file_lock->fl_flags = FL_POSIX;
4607 file_lock->fl_lmops = &nfsd_posix_mng_ops;
4608 file_lock->fl_start = lock->lk_offset;
4609 file_lock->fl_end = last_byte_offset(lock->lk_offset, lock->lk_length);
4610 nfs4_transform_lock_offset(file_lock);
4611
4612 conflock = locks_alloc_lock();
4613 if (!conflock) {
4614 dprintk("NFSD: %s: unable to allocate lock!\n", __func__);
4615 status = nfserr_jukebox;
4616 goto out;
4617 }
1da177e4 4618
21179d81 4619 err = vfs_lock_file(filp, F_SETLK, file_lock, conflock);
b8dd7b9a 4620 switch (-err) {
1da177e4 4621 case 0: /* success! */
dcef0413
BF
4622 update_stateid(&lock_stp->st_stid.sc_stateid);
4623 memcpy(&lock->lk_resp_stateid, &lock_stp->st_stid.sc_stateid,
1da177e4 4624 sizeof(stateid_t));
b8dd7b9a 4625 status = 0;
eb76b3fd
AA
4626 break;
4627 case (EAGAIN): /* conflock holds conflicting lock */
4628 status = nfserr_denied;
4629 dprintk("NFSD: nfsd4_lock: conflicting lock found!\n");
21179d81 4630 nfs4_set_lock_denied(conflock, &lock->lk_denied);
eb76b3fd 4631 break;
1da177e4
LT
4632 case (EDEADLK):
4633 status = nfserr_deadlock;
eb76b3fd 4634 break;
3e772463 4635 default:
fd85b817 4636 dprintk("NFSD: nfsd4_lock: vfs_lock_file() failed! status %d\n",err);
3e772463 4637 status = nfserrno(err);
eb76b3fd 4638 break;
1da177e4 4639 }
1da177e4 4640out:
64a284d0 4641 if (status && new_state)
c53530da 4642 release_lock_stateid(lock_stp);
9411b1d4 4643 nfsd4_bump_seqid(cstate, status);
5ec094c1
BF
4644 if (!cstate->replay_owner)
4645 nfs4_unlock_state();
21179d81
JL
4646 if (file_lock)
4647 locks_free_lock(file_lock);
4648 if (conflock)
4649 locks_free_lock(conflock);
1da177e4
LT
4650 return status;
4651}
4652
55ef1274
BF
4653/*
4654 * The NFSv4 spec allows a client to do a LOCKT without holding an OPEN,
4655 * so we do a temporary open here just to get an open file to pass to
4656 * vfs_test_lock. (Arguably perhaps test_lock should be done with an
4657 * inode operation.)
4658 */
04da6e9d 4659static __be32 nfsd_test_lock(struct svc_rqst *rqstp, struct svc_fh *fhp, struct file_lock *lock)
55ef1274
BF
4660{
4661 struct file *file;
04da6e9d
AV
4662 __be32 err = nfsd_open(rqstp, fhp, S_IFREG, NFSD_MAY_READ, &file);
4663 if (!err) {
4664 err = nfserrno(vfs_test_lock(file, lock));
4665 nfsd_close(file);
4666 }
55ef1274
BF
4667 return err;
4668}
4669
1da177e4
LT
4670/*
4671 * LOCKT operation
4672 */
b37ad28b 4673__be32
ca364317
BF
4674nfsd4_lockt(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
4675 struct nfsd4_lockt *lockt)
1da177e4 4676{
21179d81 4677 struct file_lock *file_lock = NULL;
fe0750e5 4678 struct nfs4_lockowner *lo;
b37ad28b 4679 __be32 status;
7f2210fa 4680 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
1da177e4 4681
5ccb0066 4682 if (locks_in_grace(SVC_NET(rqstp)))
1da177e4
LT
4683 return nfserr_grace;
4684
4685 if (check_lock_length(lockt->lt_offset, lockt->lt_length))
4686 return nfserr_inval;
4687
1da177e4
LT
4688 nfs4_lock_state();
4689
9b2ef62b 4690 if (!nfsd4_has_session(cstate)) {
4b24ca7d 4691 status = lookup_clientid(&lockt->lt_clientid, cstate, nn);
9b2ef62b
BF
4692 if (status)
4693 goto out;
4694 }
1da177e4 4695
75c096f7 4696 if ((status = fh_verify(rqstp, &cstate->current_fh, S_IFREG, 0)))
1da177e4 4697 goto out;
1da177e4 4698
21179d81
JL
4699 file_lock = locks_alloc_lock();
4700 if (!file_lock) {
4701 dprintk("NFSD: %s: unable to allocate lock!\n", __func__);
4702 status = nfserr_jukebox;
4703 goto out;
4704 }
4705 locks_init_lock(file_lock);
1da177e4
LT
4706 switch (lockt->lt_type) {
4707 case NFS4_READ_LT:
4708 case NFS4_READW_LT:
21179d81 4709 file_lock->fl_type = F_RDLCK;
1da177e4
LT
4710 break;
4711 case NFS4_WRITE_LT:
4712 case NFS4_WRITEW_LT:
21179d81 4713 file_lock->fl_type = F_WRLCK;
1da177e4
LT
4714 break;
4715 default:
2fdada03 4716 dprintk("NFSD: nfs4_lockt: bad lock type!\n");
1da177e4
LT
4717 status = nfserr_inval;
4718 goto out;
4719 }
4720
b3c32bcd 4721 lo = find_lockowner_str(&lockt->lt_clientid, &lockt->lt_owner, nn);
fe0750e5 4722 if (lo)
21179d81
JL
4723 file_lock->fl_owner = (fl_owner_t)lo;
4724 file_lock->fl_pid = current->tgid;
4725 file_lock->fl_flags = FL_POSIX;
1da177e4 4726
21179d81
JL
4727 file_lock->fl_start = lockt->lt_offset;
4728 file_lock->fl_end = last_byte_offset(lockt->lt_offset, lockt->lt_length);
1da177e4 4729
21179d81 4730 nfs4_transform_lock_offset(file_lock);
1da177e4 4731
21179d81 4732 status = nfsd_test_lock(rqstp, &cstate->current_fh, file_lock);
04da6e9d 4733 if (status)
fd85b817 4734 goto out;
04da6e9d 4735
21179d81 4736 if (file_lock->fl_type != F_UNLCK) {
1da177e4 4737 status = nfserr_denied;
21179d81 4738 nfs4_set_lock_denied(file_lock, &lockt->lt_denied);
1da177e4
LT
4739 }
4740out:
4741 nfs4_unlock_state();
21179d81
JL
4742 if (file_lock)
4743 locks_free_lock(file_lock);
1da177e4
LT
4744 return status;
4745}
4746
b37ad28b 4747__be32
ca364317 4748nfsd4_locku(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
a4f1706a 4749 struct nfsd4_locku *locku)
1da177e4 4750{
dcef0413 4751 struct nfs4_ol_stateid *stp;
1da177e4 4752 struct file *filp = NULL;
21179d81 4753 struct file_lock *file_lock = NULL;
b37ad28b 4754 __be32 status;
b8dd7b9a 4755 int err;
3320fef1
SK
4756 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
4757
1da177e4
LT
4758 dprintk("NFSD: nfsd4_locku: start=%Ld length=%Ld\n",
4759 (long long) locku->lu_offset,
4760 (long long) locku->lu_length);
4761
4762 if (check_lock_length(locku->lu_offset, locku->lu_length))
4763 return nfserr_inval;
4764
4765 nfs4_lock_state();
4766
9072d5c6 4767 status = nfs4_preprocess_seqid_op(cstate, locku->lu_seqid,
3320fef1
SK
4768 &locku->lu_stateid, NFS4_LOCK_STID,
4769 &stp, nn);
9072d5c6 4770 if (status)
1da177e4 4771 goto out;
f9d7562f
BF
4772 filp = find_any_file(stp->st_file);
4773 if (!filp) {
4774 status = nfserr_lock_range;
4775 goto out;
4776 }
21179d81
JL
4777 file_lock = locks_alloc_lock();
4778 if (!file_lock) {
4779 dprintk("NFSD: %s: unable to allocate lock!\n", __func__);
4780 status = nfserr_jukebox;
4781 goto out;
4782 }
4783 locks_init_lock(file_lock);
4784 file_lock->fl_type = F_UNLCK;
0a262ffb 4785 file_lock->fl_owner = (fl_owner_t)lockowner(stp->st_stateowner);
21179d81
JL
4786 file_lock->fl_pid = current->tgid;
4787 file_lock->fl_file = filp;
4788 file_lock->fl_flags = FL_POSIX;
4789 file_lock->fl_lmops = &nfsd_posix_mng_ops;
4790 file_lock->fl_start = locku->lu_offset;
4791
4792 file_lock->fl_end = last_byte_offset(locku->lu_offset,
4793 locku->lu_length);
4794 nfs4_transform_lock_offset(file_lock);
1da177e4 4795
21179d81 4796 err = vfs_lock_file(filp, F_SETLK, file_lock, NULL);
b8dd7b9a 4797 if (err) {
fd85b817 4798 dprintk("NFSD: nfs4_locku: vfs_lock_file failed!\n");
1da177e4
LT
4799 goto out_nfserr;
4800 }
dcef0413
BF
4801 update_stateid(&stp->st_stid.sc_stateid);
4802 memcpy(&locku->lu_stateid, &stp->st_stid.sc_stateid, sizeof(stateid_t));
1da177e4
LT
4803
4804out:
9411b1d4 4805 nfsd4_bump_seqid(cstate, status);
71c3bcd7
BF
4806 if (!cstate->replay_owner)
4807 nfs4_unlock_state();
21179d81
JL
4808 if (file_lock)
4809 locks_free_lock(file_lock);
1da177e4
LT
4810 return status;
4811
4812out_nfserr:
b8dd7b9a 4813 status = nfserrno(err);
1da177e4
LT
4814 goto out;
4815}
4816
4817/*
4818 * returns
4819 * 1: locks held by lockowner
4820 * 0: no locks held by lockowner
4821 */
4822static int
fe0750e5 4823check_for_locks(struct nfs4_file *filp, struct nfs4_lockowner *lowner)
1da177e4
LT
4824{
4825 struct file_lock **flpp;
f9d7562f 4826 struct inode *inode = filp->fi_inode;
1da177e4
LT
4827 int status = 0;
4828
1c8c601a 4829 spin_lock(&inode->i_lock);
1da177e4 4830 for (flpp = &inode->i_flock; *flpp != NULL; flpp = &(*flpp)->fl_next) {
796dadfd 4831 if ((*flpp)->fl_owner == (fl_owner_t)lowner) {
1da177e4
LT
4832 status = 1;
4833 goto out;
796dadfd 4834 }
1da177e4
LT
4835 }
4836out:
1c8c601a 4837 spin_unlock(&inode->i_lock);
1da177e4
LT
4838 return status;
4839}
4840
b37ad28b 4841__be32
b591480b
BF
4842nfsd4_release_lockowner(struct svc_rqst *rqstp,
4843 struct nfsd4_compound_state *cstate,
4844 struct nfsd4_release_lockowner *rlockowner)
1da177e4
LT
4845{
4846 clientid_t *clid = &rlockowner->rl_clientid;
fd44907c 4847 struct nfs4_stateowner *sop = NULL, *tmp;
fe0750e5 4848 struct nfs4_lockowner *lo;
dcef0413 4849 struct nfs4_ol_stateid *stp;
1da177e4 4850 struct xdr_netobj *owner = &rlockowner->rl_owner;
16bfdaaf 4851 unsigned int hashval = ownerstr_hashval(clid->cl_id, owner);
b37ad28b 4852 __be32 status;
7f2210fa 4853 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
1da177e4
LT
4854
4855 dprintk("nfsd4_release_lockowner clientid: (%08x/%08x):\n",
4856 clid->cl_boot, clid->cl_id);
4857
1da177e4
LT
4858 nfs4_lock_state();
4859
4b24ca7d 4860 status = lookup_clientid(clid, cstate, nn);
9b2ef62b
BF
4861 if (status)
4862 goto out;
4863
3e9e3dbe 4864 status = nfserr_locks_held;
06f1f864 4865
fd44907c
JL
4866 /* Find the matching lock stateowner */
4867 list_for_each_entry(tmp, &nn->ownerstr_hashtbl[hashval], so_strhash) {
4868 if (tmp->so_is_open_owner)
06f1f864 4869 continue;
fd44907c
JL
4870 if (same_owner_str(tmp, owner, clid)) {
4871 sop = tmp;
4872 break;
1da177e4 4873 }
3e9e3dbe 4874 }
fd44907c
JL
4875
4876 /* No matching owner found, maybe a replay? Just declare victory... */
4877 if (!sop) {
4878 status = nfs_ok;
4879 goto out;
4880 }
4881
4882 lo = lockowner(sop);
4883 /* see if there are still any locks associated with it */
4884 list_for_each_entry(stp, &sop->so_stateids, st_perstateowner) {
4885 if (check_for_locks(stp->st_file, lo))
4886 goto out;
1da177e4 4887 }
fd44907c
JL
4888
4889 status = nfs_ok;
4890 release_lockowner(lo);
1da177e4
LT
4891out:
4892 nfs4_unlock_state();
4893 return status;
4894}
4895
4896static inline struct nfs4_client_reclaim *
a55370a3 4897alloc_reclaim(void)
1da177e4 4898{
a55370a3 4899 return kmalloc(sizeof(struct nfs4_client_reclaim), GFP_KERNEL);
1da177e4
LT
4900}
4901
0ce0c2b5 4902bool
52e19c09 4903nfs4_has_reclaimed_state(const char *name, struct nfsd_net *nn)
c7b9a459 4904{
0ce0c2b5 4905 struct nfs4_client_reclaim *crp;
c7b9a459 4906
52e19c09 4907 crp = nfsd4_find_reclaim_client(name, nn);
0ce0c2b5 4908 return (crp && crp->cr_clp);
c7b9a459
N
4909}
4910
1da177e4
LT
4911/*
4912 * failure => all reset bets are off, nfserr_no_grace...
4913 */
772a9bbb 4914struct nfs4_client_reclaim *
52e19c09 4915nfs4_client_to_reclaim(const char *name, struct nfsd_net *nn)
1da177e4
LT
4916{
4917 unsigned int strhashval;
772a9bbb 4918 struct nfs4_client_reclaim *crp;
1da177e4 4919
a55370a3
N
4920 dprintk("NFSD nfs4_client_to_reclaim NAME: %.*s\n", HEXDIR_LEN, name);
4921 crp = alloc_reclaim();
772a9bbb
JL
4922 if (crp) {
4923 strhashval = clientstr_hashval(name);
4924 INIT_LIST_HEAD(&crp->cr_strhash);
52e19c09 4925 list_add(&crp->cr_strhash, &nn->reclaim_str_hashtbl[strhashval]);
772a9bbb 4926 memcpy(crp->cr_recdir, name, HEXDIR_LEN);
0ce0c2b5 4927 crp->cr_clp = NULL;
52e19c09 4928 nn->reclaim_str_hashtbl_size++;
772a9bbb
JL
4929 }
4930 return crp;
1da177e4
LT
4931}
4932
ce30e539 4933void
52e19c09 4934nfs4_remove_reclaim_record(struct nfs4_client_reclaim *crp, struct nfsd_net *nn)
ce30e539
JL
4935{
4936 list_del(&crp->cr_strhash);
4937 kfree(crp);
52e19c09 4938 nn->reclaim_str_hashtbl_size--;
ce30e539
JL
4939}
4940
2a4317c5 4941void
52e19c09 4942nfs4_release_reclaim(struct nfsd_net *nn)
1da177e4
LT
4943{
4944 struct nfs4_client_reclaim *crp = NULL;
4945 int i;
4946
1da177e4 4947 for (i = 0; i < CLIENT_HASH_SIZE; i++) {
52e19c09
SK
4948 while (!list_empty(&nn->reclaim_str_hashtbl[i])) {
4949 crp = list_entry(nn->reclaim_str_hashtbl[i].next,
1da177e4 4950 struct nfs4_client_reclaim, cr_strhash);
52e19c09 4951 nfs4_remove_reclaim_record(crp, nn);
1da177e4
LT
4952 }
4953 }
063b0fb9 4954 WARN_ON_ONCE(nn->reclaim_str_hashtbl_size);
1da177e4
LT
4955}
4956
4957/*
4958 * called from OPEN, CLAIM_PREVIOUS with a new clientid. */
2a4317c5 4959struct nfs4_client_reclaim *
52e19c09 4960nfsd4_find_reclaim_client(const char *recdir, struct nfsd_net *nn)
1da177e4
LT
4961{
4962 unsigned int strhashval;
1da177e4
LT
4963 struct nfs4_client_reclaim *crp = NULL;
4964
278c931c 4965 dprintk("NFSD: nfs4_find_reclaim_client for recdir %s\n", recdir);
1da177e4 4966
278c931c 4967 strhashval = clientstr_hashval(recdir);
52e19c09 4968 list_for_each_entry(crp, &nn->reclaim_str_hashtbl[strhashval], cr_strhash) {
278c931c 4969 if (same_name(crp->cr_recdir, recdir)) {
1da177e4
LT
4970 return crp;
4971 }
4972 }
4973 return NULL;
4974}
4975
4976/*
4977* Called from OPEN. Look for clientid in reclaim list.
4978*/
b37ad28b 4979__be32
0fe492db
TM
4980nfs4_check_open_reclaim(clientid_t *clid,
4981 struct nfsd4_compound_state *cstate,
4982 struct nfsd_net *nn)
1da177e4 4983{
0fe492db 4984 __be32 status;
a52d726b
JL
4985
4986 /* find clientid in conf_id_hashtbl */
0fe492db
TM
4987 status = lookup_clientid(clid, cstate, nn);
4988 if (status)
a52d726b
JL
4989 return nfserr_reclaim_bad;
4990
0fe492db
TM
4991 if (nfsd4_client_record_check(cstate->clp))
4992 return nfserr_reclaim_bad;
4993
4994 return nfs_ok;
1da177e4
LT
4995}
4996
65178db4
BS
4997#ifdef CONFIG_NFSD_FAULT_INJECTION
4998
44e34da6
BS
4999u64 nfsd_forget_client(struct nfs4_client *clp, u64 max)
5000{
221a6876
BF
5001 if (mark_client_expired(clp))
5002 return 0;
44e34da6
BS
5003 expire_client(clp);
5004 return 1;
5005}
5006
184c1847
BS
5007u64 nfsd_print_client(struct nfs4_client *clp, u64 num)
5008{
5009 char buf[INET6_ADDRSTRLEN];
0a5c33e2 5010 rpc_ntop((struct sockaddr *)&clp->cl_addr, buf, sizeof(buf));
184c1847
BS
5011 printk(KERN_INFO "NFS Client: %s\n", buf);
5012 return 1;
5013}
5014
5015static void nfsd_print_count(struct nfs4_client *clp, unsigned int count,
5016 const char *type)
5017{
5018 char buf[INET6_ADDRSTRLEN];
0a5c33e2 5019 rpc_ntop((struct sockaddr *)&clp->cl_addr, buf, sizeof(buf));
184c1847
BS
5020 printk(KERN_INFO "NFS Client: %s has %u %s\n", buf, count, type);
5021}
5022
3c87b9b7
TM
5023static u64 nfsd_foreach_client_lock(struct nfs4_client *clp, u64 max,
5024 void (*func)(struct nfs4_ol_stateid *))
fc29171f
BS
5025{
5026 struct nfs4_openowner *oop;
fc29171f 5027 struct nfs4_ol_stateid *stp, *st_next;
3c87b9b7 5028 struct nfs4_ol_stateid *lst, *lst_next;
fc29171f
BS
5029 u64 count = 0;
5030
5031 list_for_each_entry(oop, &clp->cl_openowners, oo_perclient) {
3c87b9b7
TM
5032 list_for_each_entry_safe(stp, st_next,
5033 &oop->oo_owner.so_stateids, st_perstateowner) {
5034 list_for_each_entry_safe(lst, lst_next,
5035 &stp->st_locks, st_locks) {
fc29171f 5036 if (func)
3c87b9b7 5037 func(lst);
fc29171f
BS
5038 if (++count == max)
5039 return count;
5040 }
5041 }
5042 }
5043
5044 return count;
5045}
5046
5047u64 nfsd_forget_client_locks(struct nfs4_client *clp, u64 max)
5048{
3c87b9b7 5049 return nfsd_foreach_client_lock(clp, max, release_lock_stateid);
fc29171f
BS
5050}
5051
184c1847
BS
5052u64 nfsd_print_client_locks(struct nfs4_client *clp, u64 max)
5053{
5054 u64 count = nfsd_foreach_client_lock(clp, max, NULL);
5055 nfsd_print_count(clp, count, "locked files");
5056 return count;
5057}
5058
4dbdbda8
BS
5059static u64 nfsd_foreach_client_open(struct nfs4_client *clp, u64 max, void (*func)(struct nfs4_openowner *))
5060{
5061 struct nfs4_openowner *oop, *next;
5062 u64 count = 0;
5063
5064 list_for_each_entry_safe(oop, next, &clp->cl_openowners, oo_perclient) {
5065 if (func)
5066 func(oop);
5067 if (++count == max)
5068 break;
5069 }
5070
5071 return count;
5072}
5073
5074u64 nfsd_forget_client_openowners(struct nfs4_client *clp, u64 max)
5075{
5076 return nfsd_foreach_client_open(clp, max, release_openowner);
5077}
5078
184c1847
BS
5079u64 nfsd_print_client_openowners(struct nfs4_client *clp, u64 max)
5080{
5081 u64 count = nfsd_foreach_client_open(clp, max, NULL);
5082 nfsd_print_count(clp, count, "open files");
5083 return count;
5084}
5085
269de30f
BS
5086static u64 nfsd_find_all_delegations(struct nfs4_client *clp, u64 max,
5087 struct list_head *victims)
5088{
5089 struct nfs4_delegation *dp, *next;
5090 u64 count = 0;
5091
cdc97505 5092 lockdep_assert_held(&state_lock);
269de30f 5093 list_for_each_entry_safe(dp, next, &clp->cl_delegations, dl_perclnt) {
dff1399f
JL
5094 if (victims) {
5095 /*
5096 * It's not safe to mess with delegations that have a
5097 * non-zero dl_time. They might have already been broken
5098 * and could be processed by the laundromat outside of
5099 * the state_lock. Just leave them be.
5100 */
5101 if (dp->dl_time != 0)
5102 continue;
5103
5104 /*
5105 * Increment dl_time to ensure that delegation breaks
5106 * don't monkey with it now that we are.
5107 */
5108 ++dp->dl_time;
269de30f 5109 list_move(&dp->dl_recall_lru, victims);
dff1399f 5110 }
269de30f
BS
5111 if (++count == max)
5112 break;
5113 }
5114 return count;
5115}
5116
5117u64 nfsd_forget_client_delegations(struct nfs4_client *clp, u64 max)
5118{
5119 struct nfs4_delegation *dp, *next;
5120 LIST_HEAD(victims);
5121 u64 count;
5122
cdc97505 5123 spin_lock(&state_lock);
269de30f 5124 count = nfsd_find_all_delegations(clp, max, &victims);
cdc97505 5125 spin_unlock(&state_lock);
269de30f
BS
5126
5127 list_for_each_entry_safe(dp, next, &victims, dl_recall_lru)
3bd64a5b 5128 revoke_delegation(dp);
269de30f
BS
5129
5130 return count;
5131}
5132
5133u64 nfsd_recall_client_delegations(struct nfs4_client *clp, u64 max)
5134{
dff1399f 5135 struct nfs4_delegation *dp;
269de30f
BS
5136 LIST_HEAD(victims);
5137 u64 count;
5138
cdc97505 5139 spin_lock(&state_lock);
269de30f 5140 count = nfsd_find_all_delegations(clp, max, &victims);
dff1399f
JL
5141 while (!list_empty(&victims)) {
5142 dp = list_first_entry(&victims, struct nfs4_delegation,
5143 dl_recall_lru);
5144 list_del_init(&dp->dl_recall_lru);
5145 dp->dl_time = 0;
269de30f 5146 nfsd_break_one_deleg(dp);
dff1399f 5147 }
cdc97505 5148 spin_unlock(&state_lock);
269de30f
BS
5149
5150 return count;
5151}
5152
184c1847
BS
5153u64 nfsd_print_client_delegations(struct nfs4_client *clp, u64 max)
5154{
5155 u64 count = 0;
5156
cdc97505 5157 spin_lock(&state_lock);
184c1847 5158 count = nfsd_find_all_delegations(clp, max, NULL);
cdc97505 5159 spin_unlock(&state_lock);
184c1847
BS
5160
5161 nfsd_print_count(clp, count, "delegations");
5162 return count;
5163}
5164
44e34da6 5165u64 nfsd_for_n_state(u64 max, u64 (*func)(struct nfs4_client *, u64))
65178db4
BS
5166{
5167 struct nfs4_client *clp, *next;
44e34da6 5168 u64 count = 0;
3320fef1 5169 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns, nfsd_net_id);
65178db4 5170
44e34da6
BS
5171 if (!nfsd_netns_ready(nn))
5172 return 0;
5173
5ed58bb2 5174 list_for_each_entry_safe(clp, next, &nn->client_lru, cl_lru) {
44e34da6
BS
5175 count += func(clp, max - count);
5176 if ((max != 0) && (count >= max))
65178db4
BS
5177 break;
5178 }
65178db4 5179
44e34da6
BS
5180 return count;
5181}
5182
6c1e82a4
BS
5183struct nfs4_client *nfsd_find_client(struct sockaddr_storage *addr, size_t addr_size)
5184{
5185 struct nfs4_client *clp;
5186 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns, nfsd_net_id);
5187
5188 if (!nfsd_netns_ready(nn))
5189 return NULL;
5190
5191 list_for_each_entry(clp, &nn->client_lru, cl_lru) {
5192 if (memcmp(&clp->cl_addr, addr, addr_size) == 0)
5193 return clp;
5194 }
5195 return NULL;
5196}
5197
65178db4
BS
5198#endif /* CONFIG_NFSD_FAULT_INJECTION */
5199
c2f1a551
MS
5200/*
5201 * Since the lifetime of a delegation isn't limited to that of an open, a
5202 * client may quite reasonably hang on to a delegation as long as it has
5203 * the inode cached. This becomes an obvious problem the first time a
5204 * client's inode cache approaches the size of the server's total memory.
5205 *
5206 * For now we avoid this problem by imposing a hard limit on the number
5207 * of delegations, which varies according to the server's memory size.
5208 */
5209static void
5210set_max_delegations(void)
5211{
5212 /*
5213 * Allow at most 4 delegations per megabyte of RAM. Quick
5214 * estimates suggest that in the worst case (where every delegation
5215 * is for a different inode), a delegation could take about 1.5K,
5216 * giving a worst case usage of about 6% of memory.
5217 */
5218 max_delegations = nr_free_buffer_pages() >> (20 - 2 - PAGE_SHIFT);
5219}
5220
d85ed443 5221static int nfs4_state_create_net(struct net *net)
8daae4dc
SK
5222{
5223 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
5224 int i;
5225
5226 nn->conf_id_hashtbl = kmalloc(sizeof(struct list_head) *
5227 CLIENT_HASH_SIZE, GFP_KERNEL);
5228 if (!nn->conf_id_hashtbl)
382a62e7 5229 goto err;
0a7ec377
SK
5230 nn->unconf_id_hashtbl = kmalloc(sizeof(struct list_head) *
5231 CLIENT_HASH_SIZE, GFP_KERNEL);
5232 if (!nn->unconf_id_hashtbl)
5233 goto err_unconf_id;
9b531137
SK
5234 nn->ownerstr_hashtbl = kmalloc(sizeof(struct list_head) *
5235 OWNER_HASH_SIZE, GFP_KERNEL);
5236 if (!nn->ownerstr_hashtbl)
5237 goto err_ownerstr;
1872de0e
SK
5238 nn->sessionid_hashtbl = kmalloc(sizeof(struct list_head) *
5239 SESSION_HASH_SIZE, GFP_KERNEL);
5240 if (!nn->sessionid_hashtbl)
5241 goto err_sessionid;
8daae4dc 5242
382a62e7 5243 for (i = 0; i < CLIENT_HASH_SIZE; i++) {
8daae4dc 5244 INIT_LIST_HEAD(&nn->conf_id_hashtbl[i]);
0a7ec377 5245 INIT_LIST_HEAD(&nn->unconf_id_hashtbl[i]);
382a62e7 5246 }
9b531137
SK
5247 for (i = 0; i < OWNER_HASH_SIZE; i++)
5248 INIT_LIST_HEAD(&nn->ownerstr_hashtbl[i]);
1872de0e
SK
5249 for (i = 0; i < SESSION_HASH_SIZE; i++)
5250 INIT_LIST_HEAD(&nn->sessionid_hashtbl[i]);
382a62e7 5251 nn->conf_name_tree = RB_ROOT;
a99454aa 5252 nn->unconf_name_tree = RB_ROOT;
5ed58bb2 5253 INIT_LIST_HEAD(&nn->client_lru);
73758fed 5254 INIT_LIST_HEAD(&nn->close_lru);
e8c69d17 5255 INIT_LIST_HEAD(&nn->del_recall_lru);
c9a49628 5256 spin_lock_init(&nn->client_lock);
8daae4dc 5257
09121281 5258 INIT_DELAYED_WORK(&nn->laundromat_work, laundromat_main);
d85ed443 5259 get_net(net);
09121281 5260
8daae4dc 5261 return 0;
382a62e7 5262
1872de0e 5263err_sessionid:
20e9e2bc 5264 kfree(nn->ownerstr_hashtbl);
9b531137
SK
5265err_ownerstr:
5266 kfree(nn->unconf_id_hashtbl);
0a7ec377
SK
5267err_unconf_id:
5268 kfree(nn->conf_id_hashtbl);
382a62e7
SK
5269err:
5270 return -ENOMEM;
8daae4dc
SK
5271}
5272
5273static void
4dce0ac9 5274nfs4_state_destroy_net(struct net *net)
8daae4dc
SK
5275{
5276 int i;
5277 struct nfs4_client *clp = NULL;
5278 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
5279
5280 for (i = 0; i < CLIENT_HASH_SIZE; i++) {
5281 while (!list_empty(&nn->conf_id_hashtbl[i])) {
5282 clp = list_entry(nn->conf_id_hashtbl[i].next, struct nfs4_client, cl_idhash);
5283 destroy_client(clp);
5284 }
5285 }
a99454aa 5286
2b905635
KM
5287 for (i = 0; i < CLIENT_HASH_SIZE; i++) {
5288 while (!list_empty(&nn->unconf_id_hashtbl[i])) {
5289 clp = list_entry(nn->unconf_id_hashtbl[i].next, struct nfs4_client, cl_idhash);
5290 destroy_client(clp);
5291 }
a99454aa
SK
5292 }
5293
1872de0e 5294 kfree(nn->sessionid_hashtbl);
9b531137 5295 kfree(nn->ownerstr_hashtbl);
0a7ec377 5296 kfree(nn->unconf_id_hashtbl);
8daae4dc 5297 kfree(nn->conf_id_hashtbl);
4dce0ac9 5298 put_net(net);
8daae4dc
SK
5299}
5300
f252bc68 5301int
d85ed443 5302nfs4_state_start_net(struct net *net)
ac4d8ff2 5303{
5e1533c7 5304 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
b5a1a81e
BF
5305 int ret;
5306
d85ed443 5307 ret = nfs4_state_create_net(net);
8daae4dc
SK
5308 if (ret)
5309 return ret;
5e1533c7 5310 nfsd4_client_tracking_init(net);
2c142baa 5311 nn->boot_time = get_seconds();
5ccb0066 5312 locks_start_grace(net, &nn->nfsd4_manager);
a51c84ed 5313 nn->grace_ended = false;
d85ed443 5314 printk(KERN_INFO "NFSD: starting %ld-second grace period (net %p)\n",
5284b44e
SK
5315 nn->nfsd4_grace, net);
5316 queue_delayed_work(laundry_wq, &nn->laundromat_work, nn->nfsd4_grace * HZ);
d85ed443
SK
5317 return 0;
5318}
5319
5320/* initialization to perform when the nfsd service is started: */
5321
5322int
5323nfs4_state_start(void)
5324{
5325 int ret;
5326
b5a1a81e 5327 ret = set_callback_cred();
d85ed443
SK
5328 if (ret)
5329 return -ENOMEM;
58da282b 5330 laundry_wq = create_singlethread_workqueue("nfsd4");
a6d6b781
JL
5331 if (laundry_wq == NULL) {
5332 ret = -ENOMEM;
5333 goto out_recovery;
5334 }
b5a1a81e
BF
5335 ret = nfsd4_create_callback_queue();
5336 if (ret)
5337 goto out_free_laundry;
09121281 5338
c2f1a551 5339 set_max_delegations();
d85ed443 5340
b5a1a81e 5341 return 0;
d85ed443 5342
b5a1a81e
BF
5343out_free_laundry:
5344 destroy_workqueue(laundry_wq);
a6d6b781 5345out_recovery:
b5a1a81e 5346 return ret;
1da177e4
LT
5347}
5348
f252bc68 5349void
4dce0ac9 5350nfs4_state_shutdown_net(struct net *net)
1da177e4 5351{
1da177e4 5352 struct nfs4_delegation *dp = NULL;
1da177e4 5353 struct list_head *pos, *next, reaplist;
4dce0ac9 5354 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
1da177e4 5355
4dce0ac9
SK
5356 cancel_delayed_work_sync(&nn->laundromat_work);
5357 locks_end_grace(&nn->nfsd4_manager);
ac55fdc4 5358
e50a26dc 5359 nfs4_lock_state();
1da177e4 5360 INIT_LIST_HEAD(&reaplist);
cdc97505 5361 spin_lock(&state_lock);
e8c69d17 5362 list_for_each_safe(pos, next, &nn->del_recall_lru) {
1da177e4
LT
5363 dp = list_entry (pos, struct nfs4_delegation, dl_recall_lru);
5364 list_move(&dp->dl_recall_lru, &reaplist);
5365 }
cdc97505 5366 spin_unlock(&state_lock);
1da177e4
LT
5367 list_for_each_safe(pos, next, &reaplist) {
5368 dp = list_entry (pos, struct nfs4_delegation, dl_recall_lru);
3bd64a5b 5369 destroy_delegation(dp);
1da177e4
LT
5370 }
5371
3320fef1 5372 nfsd4_client_tracking_exit(net);
4dce0ac9 5373 nfs4_state_destroy_net(net);
e50a26dc 5374 nfs4_unlock_state();
1da177e4
LT
5375}
5376
5377void
5378nfs4_state_shutdown(void)
5379{
5e8d5c29 5380 destroy_workqueue(laundry_wq);
c3935e30 5381 nfsd4_destroy_callback_queue();
1da177e4 5382}
8b70484c
TM
5383
5384static void
5385get_stateid(struct nfsd4_compound_state *cstate, stateid_t *stateid)
5386{
37c593c5
TM
5387 if (HAS_STATE_ID(cstate, CURRENT_STATE_ID_FLAG) && CURRENT_STATEID(stateid))
5388 memcpy(stateid, &cstate->current_stateid, sizeof(stateid_t));
8b70484c
TM
5389}
5390
5391static void
5392put_stateid(struct nfsd4_compound_state *cstate, stateid_t *stateid)
5393{
37c593c5
TM
5394 if (cstate->minorversion) {
5395 memcpy(&cstate->current_stateid, stateid, sizeof(stateid_t));
5396 SET_STATE_ID(cstate, CURRENT_STATE_ID_FLAG);
5397 }
5398}
5399
5400void
5401clear_current_stateid(struct nfsd4_compound_state *cstate)
5402{
5403 CLEAR_STATE_ID(cstate, CURRENT_STATE_ID_FLAG);
8b70484c
TM
5404}
5405
62cd4a59
TM
5406/*
5407 * functions to set current state id
5408 */
9428fe1a
TM
5409void
5410nfsd4_set_opendowngradestateid(struct nfsd4_compound_state *cstate, struct nfsd4_open_downgrade *odp)
5411{
5412 put_stateid(cstate, &odp->od_stateid);
5413}
5414
8b70484c
TM
5415void
5416nfsd4_set_openstateid(struct nfsd4_compound_state *cstate, struct nfsd4_open *open)
5417{
5418 put_stateid(cstate, &open->op_stateid);
5419}
5420
62cd4a59
TM
5421void
5422nfsd4_set_closestateid(struct nfsd4_compound_state *cstate, struct nfsd4_close *close)
5423{
5424 put_stateid(cstate, &close->cl_stateid);
5425}
5426
5427void
5428nfsd4_set_lockstateid(struct nfsd4_compound_state *cstate, struct nfsd4_lock *lock)
5429{
5430 put_stateid(cstate, &lock->lk_resp_stateid);
5431}
5432
5433/*
5434 * functions to consume current state id
5435 */
1e97b519 5436
9428fe1a
TM
5437void
5438nfsd4_get_opendowngradestateid(struct nfsd4_compound_state *cstate, struct nfsd4_open_downgrade *odp)
5439{
5440 get_stateid(cstate, &odp->od_stateid);
5441}
5442
5443void
5444nfsd4_get_delegreturnstateid(struct nfsd4_compound_state *cstate, struct nfsd4_delegreturn *drp)
5445{
5446 get_stateid(cstate, &drp->dr_stateid);
5447}
5448
1e97b519
TM
5449void
5450nfsd4_get_freestateid(struct nfsd4_compound_state *cstate, struct nfsd4_free_stateid *fsp)
5451{
5452 get_stateid(cstate, &fsp->fr_stateid);
5453}
5454
5455void
5456nfsd4_get_setattrstateid(struct nfsd4_compound_state *cstate, struct nfsd4_setattr *setattr)
5457{
5458 get_stateid(cstate, &setattr->sa_stateid);
5459}
5460
8b70484c
TM
5461void
5462nfsd4_get_closestateid(struct nfsd4_compound_state *cstate, struct nfsd4_close *close)
5463{
5464 get_stateid(cstate, &close->cl_stateid);
5465}
5466
5467void
62cd4a59 5468nfsd4_get_lockustateid(struct nfsd4_compound_state *cstate, struct nfsd4_locku *locku)
8b70484c 5469{
62cd4a59 5470 get_stateid(cstate, &locku->lu_stateid);
8b70484c 5471}
30813e27
TM
5472
5473void
5474nfsd4_get_readstateid(struct nfsd4_compound_state *cstate, struct nfsd4_read *read)
5475{
5476 get_stateid(cstate, &read->rd_stateid);
5477}
5478
5479void
5480nfsd4_get_writestateid(struct nfsd4_compound_state *cstate, struct nfsd4_write *write)
5481{
5482 get_stateid(cstate, &write->wr_stateid);
5483}