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