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