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