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