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