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