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