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