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