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