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