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