NFSD: SETCLIENTID_CONFIRM returns NFS4ERR_CLID_INUSE too often
[deliverable/linux.git] / fs / nfsd / nfs4state.c
CommitLineData
1da177e4 1/*
1da177e4
LT
2* Copyright (c) 2001 The Regents of the University of Michigan.
3* All rights reserved.
4*
5* Kendrick Smith <kmsmith@umich.edu>
6* Andy Adamson <kandros@umich.edu>
7*
8* Redistribution and use in source and binary forms, with or without
9* modification, are permitted provided that the following conditions
10* are met:
11*
12* 1. Redistributions of source code must retain the above copyright
13* notice, this list of conditions and the following disclaimer.
14* 2. Redistributions in binary form must reproduce the above copyright
15* notice, this list of conditions and the following disclaimer in the
16* documentation and/or other materials provided with the distribution.
17* 3. Neither the name of the University nor the names of its
18* contributors may be used to endorse or promote products derived
19* from this software without specific prior written permission.
20*
21* THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED
22* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
23* MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
24* DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
26* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
27* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
28* BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
29* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
30* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
31* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32*
33*/
34
aceaf78d 35#include <linux/file.h>
b89f4321 36#include <linux/fs.h>
5a0e3ad6 37#include <linux/slab.h>
0964a3d3 38#include <linux/namei.h>
c2f1a551 39#include <linux/swap.h>
17456804 40#include <linux/pagemap.h>
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;
57b7b43b 889 __be32 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 1479 struct nfs4_client *unconf, *conf, *new;
57b7b43b 1480 __be32 status;
0733d213
AA
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
57b7b43b 1602static __be32
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,
57b7b43b 1630 struct nfsd4_clid_slot *slot, __be32 nfserr)
49557cc7
AA
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
57b7b43b 1661static bool check_forechannel_attrs(struct nfsd4_channel_attrs fchannel)
1b74c25b
MJ
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;
57b7b43b 1677 __be32 status = 0;
ec6b5d7b 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 1821 struct nfsd4_session *ses;
57b7b43b 1822 __be32 status = nfserr_badsession;
e10e0cfc
BH
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;
57b7b43b 1918 __be32 status;
b85d4c01 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;
57b7b43b 2023 __be32 status = 0;
345c2842
MJ
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{
57b7b43b 2059 __be32 status = 0;
bcecf1cc 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{
21ab45a4 2217 struct nfs4_client *conf, *unconf;
1da177e4
LT
2218 nfs4_verifier confirm = setclientid_confirm->sc_confirm;
2219 clientid_t * clid = &setclientid_confirm->sc_clientid;
b37ad28b 2220 __be32 status;
1da177e4
LT
2221
2222 if (STALE_CLIENTID(clid))
2223 return nfserr_stale_clientid;
2224 /*
2225 * XXX The Duplicate Request Cache (DRC) has been checked (??)
2226 * We get here on a DRC miss.
2227 */
2228
2229 nfs4_lock_state();
21ab45a4
N
2230
2231 conf = find_confirmed_client(clid);
2232 unconf = find_unconfirmed_client(clid);
2233
a186e767
BF
2234 /*
2235 * section 14.2.34 of RFC 3530 has a description of
2236 * SETCLIENTID_CONFIRM request processing consisting
2237 * of 4 bullet points, labeled as CASE1 - CASE4 below.
2238 */
f07ea10d 2239 status = nfserr_clid_inuse;
366e0c1d 2240 if (conf && unconf && same_verf(&confirm, &unconf->cl_confirm)) {
a186e767
BF
2241 /*
2242 * RFC 3530 14.2.34 CASE 1:
2243 * callback update
2244 */
599e0a22 2245 if (!same_creds(&conf->cl_cred, &unconf->cl_cred))
1da177e4
LT
2246 status = nfserr_clid_inuse;
2247 else {
5a3c9d71
BF
2248 nfsd4_change_callback(conf, &unconf->cl_cb_conn);
2249 nfsd4_probe_callback(conf);
1a69c179 2250 expire_client(unconf);
1da177e4
LT
2251 status = nfs_ok;
2252 }
f3aba4e5 2253 } else if (conf && !unconf) {
a186e767
BF
2254 /*
2255 * RFC 3530 14.2.34 CASE 2:
2256 * probable retransmitted request; play it safe and
2257 * do nothing.
7c79f737 2258 */
599e0a22 2259 if (!same_creds(&conf->cl_cred, &rqstp->rq_cred))
1da177e4 2260 status = nfserr_clid_inuse;
21ab45a4 2261 else
1da177e4 2262 status = nfs_ok;
7c79f737 2263 } else if (!conf && unconf
599e0a22 2264 && same_verf(&unconf->cl_confirm, &confirm)) {
a186e767
BF
2265 /*
2266 * RFC 3530 14.2.34 CASE 3:
2267 * Normal case; new or rebooted client:
7c79f737 2268 */
599e0a22 2269 if (!same_creds(&unconf->cl_cred, &rqstp->rq_cred)) {
1da177e4
LT
2270 status = nfserr_clid_inuse;
2271 } else {
1a69c179
N
2272 unsigned int hash =
2273 clientstr_hashval(unconf->cl_recdir);
2274 conf = find_confirmed_client_by_str(unconf->cl_recdir,
e203d506 2275 hash);
1a69c179 2276 if (conf) {
2a4317c5 2277 nfsd4_client_record_remove(conf);
1a69c179
N
2278 expire_client(conf);
2279 }
1da177e4 2280 move_to_confirmed(unconf);
21ab45a4 2281 conf = unconf;
5a3c9d71 2282 nfsd4_probe_callback(conf);
1a69c179 2283 status = nfs_ok;
1da177e4 2284 }
599e0a22
BF
2285 } else if ((!conf || (conf && !same_verf(&conf->cl_confirm, &confirm)))
2286 && (!unconf || (unconf && !same_verf(&unconf->cl_confirm,
7c79f737 2287 &confirm)))) {
a186e767
BF
2288 /*
2289 * RFC 3530 14.2.34 CASE 4:
2290 * Client probably hasn't noticed that we rebooted yet.
7c79f737 2291 */
1da177e4 2292 status = nfserr_stale_clientid;
08e8987c 2293 }
f07ea10d 2294
1da177e4
LT
2295 nfs4_unlock_state();
2296 return status;
2297}
2298
32513b40
BF
2299static struct nfs4_file *nfsd4_alloc_file(void)
2300{
2301 return kmem_cache_alloc(file_slab, GFP_KERNEL);
2302}
2303
1da177e4 2304/* OPEN Share state helper functions */
32513b40 2305static void nfsd4_init_file(struct nfs4_file *fp, struct inode *ino)
1da177e4 2306{
1da177e4
LT
2307 unsigned int hashval = file_hashval(ino);
2308
32513b40
BF
2309 atomic_set(&fp->fi_ref, 1);
2310 INIT_LIST_HEAD(&fp->fi_hash);
2311 INIT_LIST_HEAD(&fp->fi_stateids);
2312 INIT_LIST_HEAD(&fp->fi_delegations);
2313 fp->fi_inode = igrab(ino);
2314 fp->fi_had_conflict = false;
2315 fp->fi_lease = NULL;
2316 memset(fp->fi_fds, 0, sizeof(fp->fi_fds));
2317 memset(fp->fi_access, 0, sizeof(fp->fi_access));
2318 spin_lock(&recall_lock);
2319 list_add(&fp->fi_hash, &file_hashtbl[hashval]);
2320 spin_unlock(&recall_lock);
1da177e4
LT
2321}
2322
e60d4398 2323static void
e18b890b 2324nfsd4_free_slab(struct kmem_cache **slab)
1da177e4 2325{
e60d4398
N
2326 if (*slab == NULL)
2327 return;
1a1d92c1 2328 kmem_cache_destroy(*slab);
e60d4398 2329 *slab = NULL;
1da177e4
LT
2330}
2331
e8ff2a84 2332void
1da177e4
LT
2333nfsd4_free_slabs(void)
2334{
fe0750e5
BF
2335 nfsd4_free_slab(&openowner_slab);
2336 nfsd4_free_slab(&lockowner_slab);
e60d4398 2337 nfsd4_free_slab(&file_slab);
5ac049ac 2338 nfsd4_free_slab(&stateid_slab);
5b2d21c1 2339 nfsd4_free_slab(&deleg_slab);
e60d4398 2340}
1da177e4 2341
72083396 2342int
e60d4398
N
2343nfsd4_init_slabs(void)
2344{
fe0750e5
BF
2345 openowner_slab = kmem_cache_create("nfsd4_openowners",
2346 sizeof(struct nfs4_openowner), 0, 0, NULL);
2347 if (openowner_slab == NULL)
2348 goto out_nomem;
2349 lockowner_slab = kmem_cache_create("nfsd4_lockowners",
2350 sizeof(struct nfs4_openowner), 0, 0, NULL);
2351 if (lockowner_slab == NULL)
e60d4398
N
2352 goto out_nomem;
2353 file_slab = kmem_cache_create("nfsd4_files",
20c2df83 2354 sizeof(struct nfs4_file), 0, 0, NULL);
e60d4398
N
2355 if (file_slab == NULL)
2356 goto out_nomem;
5ac049ac 2357 stateid_slab = kmem_cache_create("nfsd4_stateids",
dcef0413 2358 sizeof(struct nfs4_ol_stateid), 0, 0, NULL);
5ac049ac
N
2359 if (stateid_slab == NULL)
2360 goto out_nomem;
5b2d21c1 2361 deleg_slab = kmem_cache_create("nfsd4_delegations",
20c2df83 2362 sizeof(struct nfs4_delegation), 0, 0, NULL);
5b2d21c1
N
2363 if (deleg_slab == NULL)
2364 goto out_nomem;
e60d4398
N
2365 return 0;
2366out_nomem:
2367 nfsd4_free_slabs();
2368 dprintk("nfsd4: out of memory while initializing nfsv4\n");
2369 return -ENOMEM;
1da177e4
LT
2370}
2371
fe0750e5
BF
2372void nfs4_free_openowner(struct nfs4_openowner *oo)
2373{
2374 kfree(oo->oo_owner.so_owner.data);
2375 kmem_cache_free(openowner_slab, oo);
2376}
2377
2378void nfs4_free_lockowner(struct nfs4_lockowner *lo)
1da177e4 2379{
fe0750e5
BF
2380 kfree(lo->lo_owner.so_owner.data);
2381 kmem_cache_free(lockowner_slab, lo);
1da177e4
LT
2382}
2383
ff194bd9 2384static void init_nfs4_replay(struct nfs4_replay *rp)
1da177e4 2385{
ff194bd9
BF
2386 rp->rp_status = nfserr_serverfault;
2387 rp->rp_buflen = 0;
2388 rp->rp_buf = rp->rp_ibuf;
1da177e4
LT
2389}
2390
fe0750e5 2391static inline void *alloc_stateowner(struct kmem_cache *slab, struct xdr_netobj *owner, struct nfs4_client *clp)
ff194bd9 2392{
1da177e4 2393 struct nfs4_stateowner *sop;
1da177e4 2394
fe0750e5 2395 sop = kmem_cache_alloc(slab, GFP_KERNEL);
ff194bd9
BF
2396 if (!sop)
2397 return NULL;
2398
2399 sop->so_owner.data = kmemdup(owner->data, owner->len, GFP_KERNEL);
2400 if (!sop->so_owner.data) {
fe0750e5 2401 kmem_cache_free(slab, sop);
1da177e4 2402 return NULL;
ff194bd9
BF
2403 }
2404 sop->so_owner.len = owner->len;
2405
ea1da636 2406 INIT_LIST_HEAD(&sop->so_stateids);
ff194bd9
BF
2407 sop->so_client = clp;
2408 init_nfs4_replay(&sop->so_replay);
2409 return sop;
2410}
2411
fe0750e5 2412static void hash_openowner(struct nfs4_openowner *oo, struct nfs4_client *clp, unsigned int strhashval)
ff194bd9 2413{
16bfdaaf 2414 list_add(&oo->oo_owner.so_strhash, &ownerstr_hashtbl[strhashval]);
fe0750e5 2415 list_add(&oo->oo_perclient, &clp->cl_openowners);
ff194bd9
BF
2416}
2417
fe0750e5 2418static struct nfs4_openowner *
ff194bd9 2419alloc_init_open_stateowner(unsigned int strhashval, struct nfs4_client *clp, struct nfsd4_open *open) {
fe0750e5 2420 struct nfs4_openowner *oo;
ff194bd9 2421
fe0750e5
BF
2422 oo = alloc_stateowner(openowner_slab, &open->op_owner, clp);
2423 if (!oo)
ff194bd9 2424 return NULL;
fe0750e5
BF
2425 oo->oo_owner.so_is_open_owner = 1;
2426 oo->oo_owner.so_seqid = open->op_seqid;
d29b20cd 2427 oo->oo_flags = NFS4_OO_NEW;
fe0750e5 2428 oo->oo_time = 0;
38c387b5 2429 oo->oo_last_closed_stid = NULL;
fe0750e5
BF
2430 INIT_LIST_HEAD(&oo->oo_close_lru);
2431 hash_openowner(oo, clp, strhashval);
2432 return oo;
1da177e4
LT
2433}
2434
996e0938 2435static void init_open_stateid(struct nfs4_ol_stateid *stp, struct nfs4_file *fp, struct nfsd4_open *open) {
fe0750e5 2436 struct nfs4_openowner *oo = open->op_openowner;
d3b313a4 2437 struct nfs4_client *clp = oo->oo_owner.so_client;
1da177e4 2438
996e0938 2439 init_stid(&stp->st_stid, clp, NFS4_OPEN_STID);
ea1da636 2440 INIT_LIST_HEAD(&stp->st_lockowners);
fe0750e5 2441 list_add(&stp->st_perstateowner, &oo->oo_owner.so_stateids);
8beefa24 2442 list_add(&stp->st_perfile, &fp->fi_stateids);
fe0750e5 2443 stp->st_stateowner = &oo->oo_owner;
13cd2184 2444 get_nfs4_file(fp);
1da177e4 2445 stp->st_file = fp;
1da177e4
LT
2446 stp->st_access_bmap = 0;
2447 stp->st_deny_bmap = 0;
b6d2f1ca 2448 __set_bit(open->op_share_access, &stp->st_access_bmap);
1da177e4 2449 __set_bit(open->op_share_deny, &stp->st_deny_bmap);
4c4cd222 2450 stp->st_openstp = NULL;
1da177e4
LT
2451}
2452
fd39ca9a 2453static void
fe0750e5 2454move_to_close_lru(struct nfs4_openowner *oo)
1da177e4 2455{
fe0750e5 2456 dprintk("NFSD: move_to_close_lru nfs4_openowner %p\n", oo);
1da177e4 2457
fe0750e5
BF
2458 list_move_tail(&oo->oo_close_lru, &close_lru);
2459 oo->oo_time = get_seconds();
1da177e4
LT
2460}
2461
1da177e4 2462static int
599e0a22
BF
2463same_owner_str(struct nfs4_stateowner *sop, struct xdr_netobj *owner,
2464 clientid_t *clid)
2465{
2466 return (sop->so_owner.len == owner->len) &&
2467 0 == memcmp(sop->so_owner.data, owner->data, owner->len) &&
2468 (sop->so_client->cl_clientid.cl_id == clid->cl_id);
1da177e4
LT
2469}
2470
fe0750e5 2471static struct nfs4_openowner *
1da177e4
LT
2472find_openstateowner_str(unsigned int hashval, struct nfsd4_open *open)
2473{
a50d2ad1
BF
2474 struct nfs4_stateowner *so;
2475 struct nfs4_openowner *oo;
1da177e4 2476
16bfdaaf
BF
2477 list_for_each_entry(so, &ownerstr_hashtbl[hashval], so_strhash) {
2478 if (!so->so_is_open_owner)
2479 continue;
a50d2ad1
BF
2480 if (same_owner_str(so, &open->op_owner, &open->op_clientid)) {
2481 oo = openowner(so);
2482 renew_client(oo->oo_owner.so_client);
2483 return oo;
2484 }
1da177e4
LT
2485 }
2486 return NULL;
2487}
2488
2489/* search file_hashtbl[] for file */
2490static struct nfs4_file *
2491find_file(struct inode *ino)
2492{
2493 unsigned int hashval = file_hashval(ino);
2494 struct nfs4_file *fp;
2495
8b671b80 2496 spin_lock(&recall_lock);
1da177e4 2497 list_for_each_entry(fp, &file_hashtbl[hashval], fi_hash) {
13cd2184
N
2498 if (fp->fi_inode == ino) {
2499 get_nfs4_file(fp);
8b671b80 2500 spin_unlock(&recall_lock);
1da177e4 2501 return fp;
13cd2184 2502 }
1da177e4 2503 }
8b671b80 2504 spin_unlock(&recall_lock);
1da177e4
LT
2505 return NULL;
2506}
2507
1da177e4
LT
2508/*
2509 * Called to check deny when READ with all zero stateid or
2510 * WRITE with all zero or all one stateid
2511 */
b37ad28b 2512static __be32
1da177e4
LT
2513nfs4_share_conflict(struct svc_fh *current_fh, unsigned int deny_type)
2514{
2515 struct inode *ino = current_fh->fh_dentry->d_inode;
2516 struct nfs4_file *fp;
dcef0413 2517 struct nfs4_ol_stateid *stp;
b37ad28b 2518 __be32 ret;
1da177e4
LT
2519
2520 dprintk("NFSD: nfs4_share_conflict\n");
2521
2522 fp = find_file(ino);
13cd2184
N
2523 if (!fp)
2524 return nfs_ok;
b700949b 2525 ret = nfserr_locked;
1da177e4 2526 /* Search for conflicting share reservations */
13cd2184
N
2527 list_for_each_entry(stp, &fp->fi_stateids, st_perfile) {
2528 if (test_bit(deny_type, &stp->st_deny_bmap) ||
2529 test_bit(NFS4_SHARE_DENY_BOTH, &stp->st_deny_bmap))
2530 goto out;
1da177e4 2531 }
13cd2184
N
2532 ret = nfs_ok;
2533out:
2534 put_nfs4_file(fp);
2535 return ret;
1da177e4
LT
2536}
2537
6b57d9c8 2538static void nfsd_break_one_deleg(struct nfs4_delegation *dp)
1da177e4 2539{
1da177e4
LT
2540 /* We're assuming the state code never drops its reference
2541 * without first removing the lease. Since we're in this lease
2542 * callback (and since the lease code is serialized by the kernel
2543 * lock) we know the server hasn't removed the lease yet, we know
2544 * it's safe to take a reference: */
2545 atomic_inc(&dp->dl_count);
2546
1da177e4 2547 list_add_tail(&dp->dl_recall_lru, &del_recall_lru);
1da177e4 2548
460781b5 2549 /* only place dl_time is set. protected by lock_flocks*/
1da177e4
LT
2550 dp->dl_time = get_seconds();
2551
6b57d9c8
BF
2552 nfsd4_cb_recall(dp);
2553}
2554
acfdf5c3 2555/* Called from break_lease() with lock_flocks() held. */
6b57d9c8
BF
2556static void nfsd_break_deleg_cb(struct file_lock *fl)
2557{
acfdf5c3
BF
2558 struct nfs4_file *fp = (struct nfs4_file *)fl->fl_owner;
2559 struct nfs4_delegation *dp;
6b57d9c8 2560
acfdf5c3
BF
2561 BUG_ON(!fp);
2562 /* We assume break_lease is only called once per lease: */
2563 BUG_ON(fp->fi_had_conflict);
0272e1fd
BF
2564 /*
2565 * We don't want the locks code to timeout the lease for us;
acfdf5c3 2566 * we'll remove it ourself if a delegation isn't returned
6b57d9c8 2567 * in time:
0272e1fd
BF
2568 */
2569 fl->fl_break_time = 0;
1da177e4 2570
5d926e8c 2571 spin_lock(&recall_lock);
acfdf5c3
BF
2572 fp->fi_had_conflict = true;
2573 list_for_each_entry(dp, &fp->fi_delegations, dl_perfile)
2574 nfsd_break_one_deleg(dp);
5d926e8c 2575 spin_unlock(&recall_lock);
1da177e4
LT
2576}
2577
1da177e4
LT
2578static
2579int nfsd_change_deleg_cb(struct file_lock **onlist, int arg)
2580{
2581 if (arg & F_UNLCK)
2582 return lease_modify(onlist, arg);
2583 else
2584 return -EAGAIN;
2585}
2586
7b021967 2587static const struct lock_manager_operations nfsd_lease_mng_ops = {
8fb47a4f
BF
2588 .lm_break = nfsd_break_deleg_cb,
2589 .lm_change = nfsd_change_deleg_cb,
1da177e4
LT
2590};
2591
7a8711c9
BF
2592static __be32 nfsd4_check_seqid(struct nfsd4_compound_state *cstate, struct nfs4_stateowner *so, u32 seqid)
2593{
2594 if (nfsd4_has_session(cstate))
2595 return nfs_ok;
2596 if (seqid == so->so_seqid - 1)
2597 return nfserr_replay_me;
2598 if (seqid == so->so_seqid)
2599 return nfs_ok;
2600 return nfserr_bad_seqid;
2601}
1da177e4 2602
b37ad28b 2603__be32
6668958f
AA
2604nfsd4_process_open1(struct nfsd4_compound_state *cstate,
2605 struct nfsd4_open *open)
1da177e4 2606{
1da177e4
LT
2607 clientid_t *clientid = &open->op_clientid;
2608 struct nfs4_client *clp = NULL;
2609 unsigned int strhashval;
fe0750e5 2610 struct nfs4_openowner *oo = NULL;
4cdc951b 2611 __be32 status;
1da177e4 2612
1da177e4
LT
2613 if (STALE_CLIENTID(&open->op_clientid))
2614 return nfserr_stale_clientid;
32513b40
BF
2615 /*
2616 * In case we need it later, after we've already created the
2617 * file and don't want to risk a further failure:
2618 */
2619 open->op_file = nfsd4_alloc_file();
2620 if (open->op_file == NULL)
2621 return nfserr_jukebox;
1da177e4 2622
16bfdaaf 2623 strhashval = ownerstr_hashval(clientid->cl_id, &open->op_owner);
fe0750e5
BF
2624 oo = find_openstateowner_str(strhashval, open);
2625 open->op_openowner = oo;
2626 if (!oo) {
1da177e4
LT
2627 clp = find_confirmed_client(clientid);
2628 if (clp == NULL)
0f442aa2 2629 return nfserr_expired;
bcf130f9 2630 goto new_owner;
1da177e4 2631 }
dad1c067 2632 if (!(oo->oo_flags & NFS4_OO_CONFIRMED)) {
0f442aa2 2633 /* Replace unconfirmed owners without checking for replay. */
fe0750e5
BF
2634 clp = oo->oo_owner.so_client;
2635 release_openowner(oo);
2636 open->op_openowner = NULL;
bcf130f9 2637 goto new_owner;
0f442aa2 2638 }
4cdc951b
BF
2639 status = nfsd4_check_seqid(cstate, &oo->oo_owner, open->op_seqid);
2640 if (status)
2641 return status;
2642 clp = oo->oo_owner.so_client;
2643 goto alloc_stateid;
bcf130f9
BF
2644new_owner:
2645 oo = alloc_init_open_stateowner(strhashval, clp, open);
2646 if (oo == NULL)
2647 return nfserr_jukebox;
2648 open->op_openowner = oo;
4cdc951b
BF
2649alloc_stateid:
2650 open->op_stp = nfs4_alloc_stateid(clp);
2651 if (!open->op_stp)
2652 return nfserr_jukebox;
0f442aa2 2653 return nfs_ok;
1da177e4
LT
2654}
2655
b37ad28b 2656static inline __be32
4a6e43e6
N
2657nfs4_check_delegmode(struct nfs4_delegation *dp, int flags)
2658{
2659 if ((flags & WR_STATE) && (dp->dl_type == NFS4_OPEN_DELEGATE_READ))
2660 return nfserr_openmode;
2661 else
2662 return nfs_ok;
2663}
2664
f459e453 2665static int share_access_to_flags(u32 share_access)
52f4fb43 2666{
f459e453 2667 return share_access == NFS4_SHARE_ACCESS_READ ? RD_STATE : WR_STATE;
52f4fb43
N
2668}
2669
38c2f4b1 2670static struct nfs4_delegation *find_deleg_stateid(struct nfs4_client *cl, stateid_t *s)
24a0111e 2671{
f459e453 2672 struct nfs4_stid *ret;
24a0111e 2673
38c2f4b1 2674 ret = find_stateid_by_type(cl, s, NFS4_DELEG_STID);
f459e453
BF
2675 if (!ret)
2676 return NULL;
2677 return delegstateid(ret);
24a0111e
BF
2678}
2679
8b289b2c
BF
2680static bool nfsd4_is_deleg_cur(struct nfsd4_open *open)
2681{
2682 return open->op_claim_type == NFS4_OPEN_CLAIM_DELEGATE_CUR ||
2683 open->op_claim_type == NFS4_OPEN_CLAIM_DELEG_CUR_FH;
2684}
2685
b37ad28b 2686static __be32
38c2f4b1 2687nfs4_check_deleg(struct nfs4_client *cl, struct nfs4_file *fp, struct nfsd4_open *open,
567d9829
N
2688 struct nfs4_delegation **dp)
2689{
2690 int flags;
b37ad28b 2691 __be32 status = nfserr_bad_stateid;
567d9829 2692
38c2f4b1 2693 *dp = find_deleg_stateid(cl, &open->op_delegate_stateid);
567d9829 2694 if (*dp == NULL)
c44c5eeb 2695 goto out;
24a0111e 2696 flags = share_access_to_flags(open->op_share_access);
567d9829
N
2697 status = nfs4_check_delegmode(*dp, flags);
2698 if (status)
2699 *dp = NULL;
c44c5eeb 2700out:
8b289b2c 2701 if (!nfsd4_is_deleg_cur(open))
c44c5eeb
N
2702 return nfs_ok;
2703 if (status)
2704 return status;
dad1c067 2705 open->op_openowner->oo_flags |= NFS4_OO_CONFIRMED;
c44c5eeb 2706 return nfs_ok;
567d9829
N
2707}
2708
b37ad28b 2709static __be32
dcef0413 2710nfs4_check_open(struct nfs4_file *fp, struct nfsd4_open *open, struct nfs4_ol_stateid **stpp)
1da177e4 2711{
dcef0413 2712 struct nfs4_ol_stateid *local;
fe0750e5 2713 struct nfs4_openowner *oo = open->op_openowner;
1da177e4 2714
8beefa24 2715 list_for_each_entry(local, &fp->fi_stateids, st_perfile) {
1da177e4
LT
2716 /* ignore lock owners */
2717 if (local->st_stateowner->so_is_open_owner == 0)
2718 continue;
2719 /* remember if we have seen this open owner */
fe0750e5 2720 if (local->st_stateowner == &oo->oo_owner)
1da177e4
LT
2721 *stpp = local;
2722 /* check for conflicting share reservations */
2723 if (!test_share(local, open))
77eaae8d 2724 return nfserr_share_denied;
1da177e4 2725 }
77eaae8d 2726 return nfs_ok;
1da177e4
LT
2727}
2728
996e0938
BF
2729static void nfs4_free_stateid(struct nfs4_ol_stateid *s)
2730{
2731 kmem_cache_free(stateid_slab, s);
5ac049ac
N
2732}
2733
21fb4016
BF
2734static inline int nfs4_access_to_access(u32 nfs4_access)
2735{
2736 int flags = 0;
2737
2738 if (nfs4_access & NFS4_SHARE_ACCESS_READ)
2739 flags |= NFSD_MAY_READ;
2740 if (nfs4_access & NFS4_SHARE_ACCESS_WRITE)
2741 flags |= NFSD_MAY_WRITE;
2742 return flags;
2743}
2744
0c12eaff
CB
2745static __be32 nfs4_get_vfs_file(struct svc_rqst *rqstp, struct nfs4_file *fp,
2746 struct svc_fh *cur_fh, struct nfsd4_open *open)
f9d7562f
BF
2747{
2748 __be32 status;
0c12eaff
CB
2749 int oflag = nfs4_access_to_omode(open->op_share_access);
2750 int access = nfs4_access_to_access(open->op_share_access);
2751
f9d7562f
BF
2752 if (!fp->fi_fds[oflag]) {
2753 status = nfsd_open(rqstp, cur_fh, S_IFREG, access,
2754 &fp->fi_fds[oflag]);
f9d7562f
BF
2755 if (status)
2756 return status;
2757 }
2758 nfs4_file_get_access(fp, oflag);
2759
2760 return nfs_ok;
2761}
2762
b37ad28b 2763static inline __be32
1da177e4
LT
2764nfsd4_truncate(struct svc_rqst *rqstp, struct svc_fh *fh,
2765 struct nfsd4_open *open)
2766{
2767 struct iattr iattr = {
2768 .ia_valid = ATTR_SIZE,
2769 .ia_size = 0,
2770 };
2771 if (!open->op_truncate)
2772 return 0;
2773 if (!(open->op_share_access & NFS4_SHARE_ACCESS_WRITE))
9246585a 2774 return nfserr_inval;
1da177e4
LT
2775 return nfsd_setattr(rqstp, fh, &iattr, 0, (time_t)0);
2776}
2777
b37ad28b 2778static __be32
dcef0413 2779nfs4_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 2780{
b6d2f1ca 2781 u32 op_share_access = open->op_share_access;
7d947842 2782 bool new_access;
b37ad28b 2783 __be32 status;
1da177e4 2784
7d947842 2785 new_access = !test_bit(op_share_access, &stp->st_access_bmap);
f9d7562f 2786 if (new_access) {
0c12eaff 2787 status = nfs4_get_vfs_file(rqstp, fp, cur_fh, open);
f9d7562f
BF
2788 if (status)
2789 return status;
6c26d08f 2790 }
1da177e4
LT
2791 status = nfsd4_truncate(rqstp, cur_fh, open);
2792 if (status) {
f9d7562f 2793 if (new_access) {
f197c271 2794 int oflag = nfs4_access_to_omode(op_share_access);
f9d7562f
BF
2795 nfs4_file_put_access(fp, oflag);
2796 }
1da177e4
LT
2797 return status;
2798 }
2799 /* remember the open */
24a0111e 2800 __set_bit(op_share_access, &stp->st_access_bmap);
b55e0ba1 2801 __set_bit(open->op_share_deny, &stp->st_deny_bmap);
1da177e4
LT
2802
2803 return nfs_ok;
2804}
2805
2806
1da177e4 2807static void
1255a8f3 2808nfs4_set_claim_prev(struct nfsd4_open *open, bool has_session)
1da177e4 2809{
dad1c067 2810 open->op_openowner->oo_flags |= NFS4_OO_CONFIRMED;
1da177e4
LT
2811}
2812
14a24e99
BF
2813/* Should we give out recallable state?: */
2814static bool nfsd4_cb_channel_good(struct nfs4_client *clp)
2815{
2816 if (clp->cl_cb_state == NFSD4_CB_UP)
2817 return true;
2818 /*
2819 * In the sessions case, since we don't have to establish a
2820 * separate connection for callbacks, we assume it's OK
2821 * until we hear otherwise:
2822 */
2823 return clp->cl_minorversion && clp->cl_cb_state == NFSD4_CB_UNKNOWN;
2824}
2825
22d38c4c
BF
2826static struct file_lock *nfs4_alloc_init_lease(struct nfs4_delegation *dp, int flag)
2827{
2828 struct file_lock *fl;
2829
2830 fl = locks_alloc_lock();
2831 if (!fl)
2832 return NULL;
2833 locks_init_lock(fl);
2834 fl->fl_lmops = &nfsd_lease_mng_ops;
2835 fl->fl_flags = FL_LEASE;
2836 fl->fl_type = flag == NFS4_OPEN_DELEGATE_READ? F_RDLCK: F_WRLCK;
2837 fl->fl_end = OFFSET_MAX;
acfdf5c3 2838 fl->fl_owner = (fl_owner_t)(dp->dl_file);
22d38c4c 2839 fl->fl_pid = current->tgid;
22d38c4c
BF
2840 return fl;
2841}
2842
edab9782
BF
2843static int nfs4_setlease(struct nfs4_delegation *dp, int flag)
2844{
acfdf5c3 2845 struct nfs4_file *fp = dp->dl_file;
edab9782
BF
2846 struct file_lock *fl;
2847 int status;
2848
2849 fl = nfs4_alloc_init_lease(dp, flag);
2850 if (!fl)
2851 return -ENOMEM;
acfdf5c3 2852 fl->fl_file = find_readable_file(fp);
2a74aba7 2853 list_add(&dp->dl_perclnt, &dp->dl_stid.sc_client->cl_delegations);
acfdf5c3 2854 status = vfs_setlease(fl->fl_file, fl->fl_type, &fl);
edab9782 2855 if (status) {
acfdf5c3 2856 list_del_init(&dp->dl_perclnt);
edab9782
BF
2857 locks_free_lock(fl);
2858 return -ENOMEM;
2859 }
acfdf5c3
BF
2860 fp->fi_lease = fl;
2861 fp->fi_deleg_file = fl->fl_file;
2862 get_file(fp->fi_deleg_file);
2863 atomic_set(&fp->fi_delegees, 1);
2864 list_add(&dp->dl_perfile, &fp->fi_delegations);
2865 return 0;
2866}
2867
2868static int nfs4_set_delegation(struct nfs4_delegation *dp, int flag)
2869{
2870 struct nfs4_file *fp = dp->dl_file;
2871
2872 if (!fp->fi_lease)
2873 return nfs4_setlease(dp, flag);
2874 spin_lock(&recall_lock);
2875 if (fp->fi_had_conflict) {
2876 spin_unlock(&recall_lock);
2877 return -EAGAIN;
2878 }
2879 atomic_inc(&fp->fi_delegees);
2880 list_add(&dp->dl_perfile, &fp->fi_delegations);
2881 spin_unlock(&recall_lock);
2a74aba7 2882 list_add(&dp->dl_perclnt, &dp->dl_stid.sc_client->cl_delegations);
edab9782
BF
2883 return 0;
2884}
2885
4aa8913c
BH
2886static void nfsd4_open_deleg_none_ext(struct nfsd4_open *open, int status)
2887{
2888 open->op_delegate_type = NFS4_OPEN_DELEGATE_NONE_EXT;
2889 if (status == -EAGAIN)
2890 open->op_why_no_deleg = WND4_CONTENTION;
2891 else {
2892 open->op_why_no_deleg = WND4_RESOURCE;
2893 switch (open->op_deleg_want) {
2894 case NFS4_SHARE_WANT_READ_DELEG:
2895 case NFS4_SHARE_WANT_WRITE_DELEG:
2896 case NFS4_SHARE_WANT_ANY_DELEG:
2897 break;
2898 case NFS4_SHARE_WANT_CANCEL:
2899 open->op_why_no_deleg = WND4_CANCELLED;
2900 break;
2901 case NFS4_SHARE_WANT_NO_DELEG:
2902 BUG(); /* not supposed to get here */
2903 }
2904 }
2905}
2906
1da177e4
LT
2907/*
2908 * Attempt to hand out a delegation.
2909 */
2910static void
dcef0413 2911nfs4_open_delegation(struct svc_fh *fh, struct nfsd4_open *open, struct nfs4_ol_stateid *stp)
1da177e4
LT
2912{
2913 struct nfs4_delegation *dp;
fe0750e5 2914 struct nfs4_openowner *oo = container_of(stp->st_stateowner, struct nfs4_openowner, oo_owner);
14a24e99 2915 int cb_up;
d24433cd 2916 int status = 0, flag = 0;
1da177e4 2917
fe0750e5 2918 cb_up = nfsd4_cb_channel_good(oo->oo_owner.so_client);
1da177e4 2919 flag = NFS4_OPEN_DELEGATE_NONE;
7b190fec
N
2920 open->op_recall = 0;
2921 switch (open->op_claim_type) {
2922 case NFS4_OPEN_CLAIM_PREVIOUS:
2bf23875 2923 if (!cb_up)
7b190fec
N
2924 open->op_recall = 1;
2925 flag = open->op_delegate_type;
2926 if (flag == NFS4_OPEN_DELEGATE_NONE)
2927 goto out;
2928 break;
2929 case NFS4_OPEN_CLAIM_NULL:
2930 /* Let's not give out any delegations till everyone's
2931 * had the chance to reclaim theirs.... */
af558e33 2932 if (locks_in_grace())
7b190fec 2933 goto out;
dad1c067 2934 if (!cb_up || !(oo->oo_flags & NFS4_OO_CONFIRMED))
7b190fec
N
2935 goto out;
2936 if (open->op_share_access & NFS4_SHARE_ACCESS_WRITE)
2937 flag = NFS4_OPEN_DELEGATE_WRITE;
2938 else
2939 flag = NFS4_OPEN_DELEGATE_READ;
2940 break;
2941 default:
2942 goto out;
2943 }
1da177e4 2944
fe0750e5 2945 dp = alloc_init_deleg(oo->oo_owner.so_client, stp, fh, flag);
dd239cc0
BF
2946 if (dp == NULL)
2947 goto out_no_deleg;
acfdf5c3 2948 status = nfs4_set_delegation(dp, flag);
edab9782 2949 if (status)
dd239cc0 2950 goto out_free;
1da177e4 2951
d5477a8d 2952 memcpy(&open->op_delegate_stateid, &dp->dl_stid.sc_stateid, sizeof(dp->dl_stid.sc_stateid));
1da177e4 2953
8c10cbdb 2954 dprintk("NFSD: delegation stateid=" STATEID_FMT "\n",
d5477a8d 2955 STATEID_VAL(&dp->dl_stid.sc_stateid));
1da177e4
LT
2956out:
2957 open->op_delegate_type = flag;
d24433cd
BH
2958 if (flag == NFS4_OPEN_DELEGATE_NONE) {
2959 if (open->op_claim_type == NFS4_OPEN_CLAIM_PREVIOUS &&
2960 open->op_delegate_type != NFS4_OPEN_DELEGATE_NONE)
2961 dprintk("NFSD: WARNING: refusing delegation reclaim\n");
2962
4aa8913c
BH
2963 /* 4.1 client asking for a delegation? */
2964 if (open->op_deleg_want)
2965 nfsd4_open_deleg_none_ext(open, status);
d24433cd 2966 }
dd239cc0
BF
2967 return;
2968out_free:
acfdf5c3 2969 nfs4_put_delegation(dp);
dd239cc0
BF
2970out_no_deleg:
2971 flag = NFS4_OPEN_DELEGATE_NONE;
2972 goto out;
1da177e4
LT
2973}
2974
e27f49c3
BH
2975static void nfsd4_deleg_xgrade_none_ext(struct nfsd4_open *open,
2976 struct nfs4_delegation *dp)
2977{
2978 if (open->op_deleg_want == NFS4_SHARE_WANT_READ_DELEG &&
2979 dp->dl_type == NFS4_OPEN_DELEGATE_WRITE) {
2980 open->op_delegate_type = NFS4_OPEN_DELEGATE_NONE_EXT;
2981 open->op_why_no_deleg = WND4_NOT_SUPP_DOWNGRADE;
2982 } else if (open->op_deleg_want == NFS4_SHARE_WANT_WRITE_DELEG &&
2983 dp->dl_type == NFS4_OPEN_DELEGATE_WRITE) {
2984 open->op_delegate_type = NFS4_OPEN_DELEGATE_NONE_EXT;
2985 open->op_why_no_deleg = WND4_NOT_SUPP_UPGRADE;
2986 }
2987 /* Otherwise the client must be confused wanting a delegation
2988 * it already has, therefore we don't return
2989 * NFS4_OPEN_DELEGATE_NONE_EXT and reason.
2990 */
2991}
2992
1da177e4
LT
2993/*
2994 * called with nfs4_lock_state() held.
2995 */
b37ad28b 2996__be32
1da177e4
LT
2997nfsd4_process_open2(struct svc_rqst *rqstp, struct svc_fh *current_fh, struct nfsd4_open *open)
2998{
6668958f 2999 struct nfsd4_compoundres *resp = rqstp->rq_resp;
38c2f4b1 3000 struct nfs4_client *cl = open->op_openowner->oo_owner.so_client;
1da177e4
LT
3001 struct nfs4_file *fp = NULL;
3002 struct inode *ino = current_fh->fh_dentry->d_inode;
dcef0413 3003 struct nfs4_ol_stateid *stp = NULL;
567d9829 3004 struct nfs4_delegation *dp = NULL;
b37ad28b 3005 __be32 status;
1da177e4 3006
1da177e4
LT
3007 /*
3008 * Lookup file; if found, lookup stateid and check open request,
3009 * and check for delegations in the process of being recalled.
3010 * If not found, create the nfs4_file struct
3011 */
3012 fp = find_file(ino);
3013 if (fp) {
3014 if ((status = nfs4_check_open(fp, open, &stp)))
3015 goto out;
38c2f4b1 3016 status = nfs4_check_deleg(cl, fp, open, &dp);
c44c5eeb
N
3017 if (status)
3018 goto out;
1da177e4 3019 } else {
c44c5eeb 3020 status = nfserr_bad_stateid;
8b289b2c 3021 if (nfsd4_is_deleg_cur(open))
c44c5eeb 3022 goto out;
3e772463 3023 status = nfserr_jukebox;
32513b40
BF
3024 fp = open->op_file;
3025 open->op_file = NULL;
3026 nfsd4_init_file(fp, ino);
1da177e4
LT
3027 }
3028
3029 /*
3030 * OPEN the file, or upgrade an existing OPEN.
3031 * If truncate fails, the OPEN fails.
3032 */
3033 if (stp) {
3034 /* Stateid was found, this is an OPEN upgrade */
f9d7562f 3035 status = nfs4_upgrade_open(rqstp, fp, current_fh, stp, open);
1da177e4
LT
3036 if (status)
3037 goto out;
3038 } else {
4cdc951b 3039 status = nfs4_get_vfs_file(rqstp, fp, current_fh, open);
567d9829 3040 if (status)
1da177e4 3041 goto out;
4cdc951b
BF
3042 stp = open->op_stp;
3043 open->op_stp = NULL;
996e0938 3044 init_open_stateid(stp, fp, open);
1da177e4
LT
3045 status = nfsd4_truncate(rqstp, current_fh, open);
3046 if (status) {
2283963f 3047 release_open_stateid(stp);
1da177e4
LT
3048 goto out;
3049 }
3050 }
dcef0413
BF
3051 update_stateid(&stp->st_stid.sc_stateid);
3052 memcpy(&open->op_stateid, &stp->st_stid.sc_stateid, sizeof(stateid_t));
1da177e4 3053
d24433cd 3054 if (nfsd4_has_session(&resp->cstate)) {
dad1c067 3055 open->op_openowner->oo_flags |= NFS4_OO_CONFIRMED;
6668958f 3056
d24433cd
BH
3057 if (open->op_deleg_want & NFS4_SHARE_WANT_NO_DELEG) {
3058 open->op_delegate_type = NFS4_OPEN_DELEGATE_NONE_EXT;
3059 open->op_why_no_deleg = WND4_NOT_WANTED;
3060 goto nodeleg;
3061 }
3062 }
3063
1da177e4
LT
3064 /*
3065 * Attempt to hand out a delegation. No error return, because the
3066 * OPEN succeeds even if we fail.
3067 */
3068 nfs4_open_delegation(current_fh, open, stp);
d24433cd 3069nodeleg:
1da177e4
LT
3070 status = nfs_ok;
3071
8c10cbdb 3072 dprintk("%s: stateid=" STATEID_FMT "\n", __func__,
dcef0413 3073 STATEID_VAL(&stp->st_stid.sc_stateid));
1da177e4 3074out:
d24433cd
BH
3075 /* 4.1 client trying to upgrade/downgrade delegation? */
3076 if (open->op_delegate_type == NFS4_OPEN_DELEGATE_NONE && dp &&
e27f49c3
BH
3077 open->op_deleg_want)
3078 nfsd4_deleg_xgrade_none_ext(open, dp);
d24433cd 3079
13cd2184
N
3080 if (fp)
3081 put_nfs4_file(fp);
37515177 3082 if (status == 0 && open->op_claim_type == NFS4_OPEN_CLAIM_PREVIOUS)
1255a8f3 3083 nfs4_set_claim_prev(open, nfsd4_has_session(&resp->cstate));
1da177e4
LT
3084 /*
3085 * To finish the open response, we just need to set the rflags.
3086 */
3087 open->op_rflags = NFS4_OPEN_RESULT_LOCKTYPE_POSIX;
dad1c067 3088 if (!(open->op_openowner->oo_flags & NFS4_OO_CONFIRMED) &&
6668958f 3089 !nfsd4_has_session(&resp->cstate))
1da177e4
LT
3090 open->op_rflags |= NFS4_OPEN_RESULT_CONFIRM;
3091
3092 return status;
3093}
3094
d29b20cd
BF
3095void nfsd4_cleanup_open_state(struct nfsd4_open *open, __be32 status)
3096{
3097 if (open->op_openowner) {
3098 struct nfs4_openowner *oo = open->op_openowner;
3099
3100 if (!list_empty(&oo->oo_owner.so_stateids))
3101 list_del_init(&oo->oo_close_lru);
3102 if (oo->oo_flags & NFS4_OO_NEW) {
3103 if (status) {
3104 release_openowner(oo);
3105 open->op_openowner = NULL;
3106 } else
3107 oo->oo_flags &= ~NFS4_OO_NEW;
3108 }
3109 }
32513b40
BF
3110 if (open->op_file)
3111 nfsd4_free_file(open->op_file);
4cdc951b
BF
3112 if (open->op_stp)
3113 nfs4_free_stateid(open->op_stp);
d29b20cd
BF
3114}
3115
b37ad28b 3116__be32
b591480b
BF
3117nfsd4_renew(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
3118 clientid_t *clid)
1da177e4
LT
3119{
3120 struct nfs4_client *clp;
b37ad28b 3121 __be32 status;
1da177e4
LT
3122
3123 nfs4_lock_state();
3124 dprintk("process_renew(%08x/%08x): starting\n",
3125 clid->cl_boot, clid->cl_id);
3126 status = nfserr_stale_clientid;
3127 if (STALE_CLIENTID(clid))
3128 goto out;
3129 clp = find_confirmed_client(clid);
3130 status = nfserr_expired;
3131 if (clp == NULL) {
3132 /* We assume the client took too long to RENEW. */
3133 dprintk("nfsd4_renew: clientid not found!\n");
3134 goto out;
3135 }
1da177e4 3136 status = nfserr_cb_path_down;
ea1da636 3137 if (!list_empty(&clp->cl_delegations)
77a3569d 3138 && clp->cl_cb_state != NFSD4_CB_UP)
1da177e4
LT
3139 goto out;
3140 status = nfs_ok;
3141out:
3142 nfs4_unlock_state();
3143 return status;
3144}
3145
c47d832b 3146static struct lock_manager nfsd4_manager = {
af558e33
BF
3147};
3148
33dcc481
JL
3149static bool grace_ended;
3150
a76b4319 3151static void
af558e33 3152nfsd4_end_grace(void)
a76b4319 3153{
33dcc481
JL
3154 /* do nothing if grace period already ended */
3155 if (grace_ended)
3156 return;
3157
a76b4319 3158 dprintk("NFSD: end of grace period\n");
33dcc481 3159 grace_ended = true;
2a4317c5 3160 nfsd4_record_grace_done(&init_net, boot_time);
af558e33 3161 locks_end_grace(&nfsd4_manager);
e46b498c
BF
3162 /*
3163 * Now that every NFSv4 client has had the chance to recover and
3164 * to see the (possibly new, possibly shorter) lease time, we
3165 * can safely set the next grace time to the current lease time:
3166 */
3167 nfsd4_grace = nfsd4_lease;
a76b4319
N
3168}
3169
fd39ca9a 3170static time_t
1da177e4
LT
3171nfs4_laundromat(void)
3172{
3173 struct nfs4_client *clp;
fe0750e5 3174 struct nfs4_openowner *oo;
1da177e4
LT
3175 struct nfs4_delegation *dp;
3176 struct list_head *pos, *next, reaplist;
cf07d2ea
BF
3177 time_t cutoff = get_seconds() - nfsd4_lease;
3178 time_t t, clientid_val = nfsd4_lease;
3179 time_t u, test_val = nfsd4_lease;
1da177e4
LT
3180
3181 nfs4_lock_state();
3182
3183 dprintk("NFSD: laundromat service - starting\n");
33dcc481 3184 nfsd4_end_grace();
36acb66b
BH
3185 INIT_LIST_HEAD(&reaplist);
3186 spin_lock(&client_lock);
1da177e4
LT
3187 list_for_each_safe(pos, next, &client_lru) {
3188 clp = list_entry(pos, struct nfs4_client, cl_lru);
3189 if (time_after((unsigned long)clp->cl_time, (unsigned long)cutoff)) {
3190 t = clp->cl_time - cutoff;
3191 if (clientid_val > t)
3192 clientid_val = t;
3193 break;
3194 }
d7682988
BH
3195 if (atomic_read(&clp->cl_refcount)) {
3196 dprintk("NFSD: client in use (clientid %08x)\n",
3197 clp->cl_clientid.cl_id);
3198 continue;
3199 }
3200 unhash_client_locked(clp);
3201 list_add(&clp->cl_lru, &reaplist);
36acb66b
BH
3202 }
3203 spin_unlock(&client_lock);
3204 list_for_each_safe(pos, next, &reaplist) {
3205 clp = list_entry(pos, struct nfs4_client, cl_lru);
1da177e4
LT
3206 dprintk("NFSD: purging unused client (clientid %08x)\n",
3207 clp->cl_clientid.cl_id);
2a4317c5 3208 nfsd4_client_record_remove(clp);
1da177e4
LT
3209 expire_client(clp);
3210 }
1da177e4
LT
3211 spin_lock(&recall_lock);
3212 list_for_each_safe(pos, next, &del_recall_lru) {
3213 dp = list_entry (pos, struct nfs4_delegation, dl_recall_lru);
3214 if (time_after((unsigned long)dp->dl_time, (unsigned long)cutoff)) {
3215 u = dp->dl_time - cutoff;
3216 if (test_val > u)
3217 test_val = u;
3218 break;
3219 }
1da177e4
LT
3220 list_move(&dp->dl_recall_lru, &reaplist);
3221 }
3222 spin_unlock(&recall_lock);
3223 list_for_each_safe(pos, next, &reaplist) {
3224 dp = list_entry (pos, struct nfs4_delegation, dl_recall_lru);
1da177e4
LT
3225 unhash_delegation(dp);
3226 }
cf07d2ea 3227 test_val = nfsd4_lease;
1da177e4 3228 list_for_each_safe(pos, next, &close_lru) {
fe0750e5
BF
3229 oo = container_of(pos, struct nfs4_openowner, oo_close_lru);
3230 if (time_after((unsigned long)oo->oo_time, (unsigned long)cutoff)) {
3231 u = oo->oo_time - cutoff;
1da177e4
LT
3232 if (test_val > u)
3233 test_val = u;
3234 break;
3235 }
fe0750e5 3236 release_openowner(oo);
1da177e4
LT
3237 }
3238 if (clientid_val < NFSD_LAUNDROMAT_MINTIMEOUT)
3239 clientid_val = NFSD_LAUNDROMAT_MINTIMEOUT;
3240 nfs4_unlock_state();
3241 return clientid_val;
3242}
3243
a254b246
HH
3244static struct workqueue_struct *laundry_wq;
3245static void laundromat_main(struct work_struct *);
3246static DECLARE_DELAYED_WORK(laundromat_work, laundromat_main);
3247
3248static void
c4028958 3249laundromat_main(struct work_struct *not_used)
1da177e4
LT
3250{
3251 time_t t;
3252
3253 t = nfs4_laundromat();
3254 dprintk("NFSD: laundromat_main - sleeping for %ld seconds\n", t);
58da282b 3255 queue_delayed_work(laundry_wq, &laundromat_work, t*HZ);
1da177e4
LT
3256}
3257
f7a4d872 3258static inline __be32 nfs4_check_fh(struct svc_fh *fhp, struct nfs4_ol_stateid *stp)
1da177e4 3259{
f7a4d872
BF
3260 if (fhp->fh_dentry->d_inode != stp->st_file->fi_inode)
3261 return nfserr_bad_stateid;
3262 return nfs_ok;
1da177e4
LT
3263}
3264
3265static int
3266STALE_STATEID(stateid_t *stateid)
3267{
d3b313a4 3268 if (stateid->si_opaque.so_clid.cl_boot == boot_time)
e4e83ea4
BF
3269 return 0;
3270 dprintk("NFSD: stale stateid " STATEID_FMT "!\n",
8c10cbdb 3271 STATEID_VAL(stateid));
e4e83ea4 3272 return 1;
1da177e4
LT
3273}
3274
3275static inline int
3276access_permit_read(unsigned long access_bmap)
3277{
3278 return test_bit(NFS4_SHARE_ACCESS_READ, &access_bmap) ||
3279 test_bit(NFS4_SHARE_ACCESS_BOTH, &access_bmap) ||
3280 test_bit(NFS4_SHARE_ACCESS_WRITE, &access_bmap);
3281}
3282
3283static inline int
3284access_permit_write(unsigned long access_bmap)
3285{
3286 return test_bit(NFS4_SHARE_ACCESS_WRITE, &access_bmap) ||
3287 test_bit(NFS4_SHARE_ACCESS_BOTH, &access_bmap);
3288}
3289
3290static
dcef0413 3291__be32 nfs4_check_openmode(struct nfs4_ol_stateid *stp, int flags)
1da177e4 3292{
b37ad28b 3293 __be32 status = nfserr_openmode;
1da177e4 3294
02921914
BF
3295 /* For lock stateid's, we test the parent open, not the lock: */
3296 if (stp->st_openstp)
3297 stp = stp->st_openstp;
1da177e4
LT
3298 if ((flags & WR_STATE) && (!access_permit_write(stp->st_access_bmap)))
3299 goto out;
3300 if ((flags & RD_STATE) && (!access_permit_read(stp->st_access_bmap)))
3301 goto out;
3302 status = nfs_ok;
3303out:
3304 return status;
3305}
3306
b37ad28b 3307static inline __be32
1da177e4
LT
3308check_special_stateids(svc_fh *current_fh, stateid_t *stateid, int flags)
3309{
203a8c8e 3310 if (ONE_STATEID(stateid) && (flags & RD_STATE))
1da177e4 3311 return nfs_ok;
af558e33 3312 else if (locks_in_grace()) {
25985edc 3313 /* Answer in remaining cases depends on existence of
1da177e4
LT
3314 * conflicting state; so we must wait out the grace period. */
3315 return nfserr_grace;
3316 } else if (flags & WR_STATE)
3317 return nfs4_share_conflict(current_fh,
3318 NFS4_SHARE_DENY_WRITE);
3319 else /* (flags & RD_STATE) && ZERO_STATEID(stateid) */
3320 return nfs4_share_conflict(current_fh,
3321 NFS4_SHARE_DENY_READ);
3322}
3323
3324/*
3325 * Allow READ/WRITE during grace period on recovered state only for files
3326 * that are not able to provide mandatory locking.
3327 */
3328static inline int
18f82731 3329grace_disallows_io(struct inode *inode)
1da177e4 3330{
203a8c8e 3331 return locks_in_grace() && mandatory_lock(inode);
1da177e4
LT
3332}
3333
81b82965
BF
3334/* Returns true iff a is later than b: */
3335static bool stateid_generation_after(stateid_t *a, stateid_t *b)
3336{
3337 return (s32)a->si_generation - (s32)b->si_generation > 0;
3338}
3339
57b7b43b 3340static __be32 check_stateid_generation(stateid_t *in, stateid_t *ref, bool has_session)
0836f587 3341{
6668958f
AA
3342 /*
3343 * When sessions are used the stateid generation number is ignored
3344 * when it is zero.
3345 */
28dde241 3346 if (has_session && in->si_generation == 0)
81b82965
BF
3347 return nfs_ok;
3348
3349 if (in->si_generation == ref->si_generation)
3350 return nfs_ok;
6668958f 3351
0836f587 3352 /* If the client sends us a stateid from the future, it's buggy: */
81b82965 3353 if (stateid_generation_after(in, ref))
0836f587
BF
3354 return nfserr_bad_stateid;
3355 /*
81b82965
BF
3356 * However, we could see a stateid from the past, even from a
3357 * non-buggy client. For example, if the client sends a lock
3358 * while some IO is outstanding, the lock may bump si_generation
3359 * while the IO is still in flight. The client could avoid that
3360 * situation by waiting for responses on all the IO requests,
3361 * but better performance may result in retrying IO that
3362 * receives an old_stateid error if requests are rarely
3363 * reordered in flight:
0836f587 3364 */
81b82965 3365 return nfserr_old_stateid;
0836f587
BF
3366}
3367
38c2f4b1 3368__be32 nfs4_validate_stateid(struct nfs4_client *cl, stateid_t *stateid)
17456804 3369{
97b7e3b6
BF
3370 struct nfs4_stid *s;
3371 struct nfs4_ol_stateid *ols;
3372 __be32 status;
17456804
BS
3373
3374 if (STALE_STATEID(stateid))
97b7e3b6 3375 return nfserr_stale_stateid;
17456804 3376
38c2f4b1 3377 s = find_stateid(cl, stateid);
97b7e3b6
BF
3378 if (!s)
3379 return nfserr_stale_stateid;
36279ac1 3380 status = check_stateid_generation(stateid, &s->sc_stateid, 1);
17456804 3381 if (status)
97b7e3b6
BF
3382 return status;
3383 if (!(s->sc_type & (NFS4_OPEN_STID | NFS4_LOCK_STID)))
3384 return nfs_ok;
3385 ols = openlockstateid(s);
3386 if (ols->st_stateowner->so_is_open_owner
dad1c067 3387 && !(openowner(ols->st_stateowner)->oo_flags & NFS4_OO_CONFIRMED))
97b7e3b6
BF
3388 return nfserr_bad_stateid;
3389 return nfs_ok;
17456804
BS
3390}
3391
38c2f4b1
BF
3392static __be32 nfsd4_lookup_stateid(stateid_t *stateid, unsigned char typemask, struct nfs4_stid **s)
3393{
3394 struct nfs4_client *cl;
3395
3396 if (ZERO_STATEID(stateid) || ONE_STATEID(stateid))
3397 return nfserr_bad_stateid;
3398 if (STALE_STATEID(stateid))
3399 return nfserr_stale_stateid;
3400 cl = find_confirmed_client(&stateid->si_opaque.so_clid);
3401 if (!cl)
3402 return nfserr_expired;
3403 *s = find_stateid_by_type(cl, stateid, typemask);
3404 if (!*s)
3405 return nfserr_bad_stateid;
3406 return nfs_ok;
3407
3408}
3409
1da177e4
LT
3410/*
3411* Checks for stateid operations
3412*/
b37ad28b 3413__be32
dd453dfd
BH
3414nfs4_preprocess_stateid_op(struct nfsd4_compound_state *cstate,
3415 stateid_t *stateid, int flags, struct file **filpp)
1da177e4 3416{
69064a27 3417 struct nfs4_stid *s;
dcef0413 3418 struct nfs4_ol_stateid *stp = NULL;
1da177e4 3419 struct nfs4_delegation *dp = NULL;
dd453dfd 3420 struct svc_fh *current_fh = &cstate->current_fh;
1da177e4 3421 struct inode *ino = current_fh->fh_dentry->d_inode;
b37ad28b 3422 __be32 status;
1da177e4 3423
1da177e4
LT
3424 if (filpp)
3425 *filpp = NULL;
3426
18f82731 3427 if (grace_disallows_io(ino))
1da177e4
LT
3428 return nfserr_grace;
3429
3430 if (ZERO_STATEID(stateid) || ONE_STATEID(stateid))
3431 return check_special_stateids(current_fh, stateid, flags);
3432
38c2f4b1
BF
3433 status = nfsd4_lookup_stateid(stateid, NFS4_DELEG_STID|NFS4_OPEN_STID|NFS4_LOCK_STID, &s);
3434 if (status)
3435 return status;
69064a27
BF
3436 status = check_stateid_generation(stateid, &s->sc_stateid, nfsd4_has_session(cstate));
3437 if (status)
3438 goto out;
f7a4d872
BF
3439 switch (s->sc_type) {
3440 case NFS4_DELEG_STID:
69064a27 3441 dp = delegstateid(s);
dc9bf700
BF
3442 status = nfs4_check_delegmode(dp, flags);
3443 if (status)
3444 goto out;
43b0178e 3445 if (filpp) {
acfdf5c3 3446 *filpp = dp->dl_file->fi_deleg_file;
43b0178e
DC
3447 BUG_ON(!*filpp);
3448 }
f7a4d872
BF
3449 break;
3450 case NFS4_OPEN_STID:
3451 case NFS4_LOCK_STID:
69064a27 3452 stp = openlockstateid(s);
f7a4d872
BF
3453 status = nfs4_check_fh(current_fh, stp);
3454 if (status)
1da177e4 3455 goto out;
fe0750e5 3456 if (stp->st_stateowner->so_is_open_owner
dad1c067 3457 && !(openowner(stp->st_stateowner)->oo_flags & NFS4_OO_CONFIRMED))
1da177e4 3458 goto out;
a4455be0
BF
3459 status = nfs4_check_openmode(stp, flags);
3460 if (status)
1da177e4 3461 goto out;
f9d7562f
BF
3462 if (filpp) {
3463 if (flags & RD_STATE)
3464 *filpp = find_readable_file(stp->st_file);
3465 else
3466 *filpp = find_writeable_file(stp->st_file);
f9d7562f 3467 }
f7a4d872
BF
3468 break;
3469 default:
3470 return nfserr_bad_stateid;
1da177e4
LT
3471 }
3472 status = nfs_ok;
3473out:
3474 return status;
3475}
3476
e1ca12df 3477static __be32
dcef0413 3478nfsd4_free_lock_stateid(struct nfs4_ol_stateid *stp)
e1ca12df 3479{
fe0750e5 3480 if (check_for_locks(stp->st_file, lockowner(stp->st_stateowner)))
e1ca12df
BS
3481 return nfserr_locks_held;
3482 release_lock_stateid(stp);
3483 return nfs_ok;
3484}
3485
17456804
BS
3486/*
3487 * Test if the stateid is valid
3488 */
3489__be32
3490nfsd4_test_stateid(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
3491 struct nfsd4_test_stateid *test_stateid)
3492{
03cfb420
BS
3493 struct nfsd4_test_stateid_id *stateid;
3494 struct nfs4_client *cl = cstate->session->se_client;
3495
3496 nfs4_lock_state();
3497 list_for_each_entry(stateid, &test_stateid->ts_stateid_list, ts_id_list)
3498 stateid->ts_id_status = nfs4_validate_stateid(cl, &stateid->ts_id_stateid);
3499 nfs4_unlock_state();
3500
17456804
BS
3501 return nfs_ok;
3502}
3503
e1ca12df
BS
3504__be32
3505nfsd4_free_stateid(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
3506 struct nfsd4_free_stateid *free_stateid)
3507{
3508 stateid_t *stateid = &free_stateid->fr_stateid;
2da1cec7 3509 struct nfs4_stid *s;
38c2f4b1 3510 struct nfs4_client *cl = cstate->session->se_client;
2da1cec7 3511 __be32 ret = nfserr_bad_stateid;
e1ca12df
BS
3512
3513 nfs4_lock_state();
38c2f4b1 3514 s = find_stateid(cl, stateid);
2da1cec7 3515 if (!s)
81b82965 3516 goto out;
2da1cec7
BF
3517 switch (s->sc_type) {
3518 case NFS4_DELEG_STID:
e1ca12df
BS
3519 ret = nfserr_locks_held;
3520 goto out;
2da1cec7
BF
3521 case NFS4_OPEN_STID:
3522 case NFS4_LOCK_STID:
3523 ret = check_stateid_generation(stateid, &s->sc_stateid, 1);
3524 if (ret)
3525 goto out;
3526 if (s->sc_type == NFS4_LOCK_STID)
3527 ret = nfsd4_free_lock_stateid(openlockstateid(s));
3528 else
3529 ret = nfserr_locks_held;
f7a4d872
BF
3530 break;
3531 default:
3532 ret = nfserr_bad_stateid;
e1ca12df 3533 }
e1ca12df
BS
3534out:
3535 nfs4_unlock_state();
3536 return ret;
3537}
3538
4c4cd222
N
3539static inline int
3540setlkflg (int type)
3541{
3542 return (type == NFS4_READW_LT || type == NFS4_READ_LT) ?
3543 RD_STATE : WR_STATE;
3544}
1da177e4 3545
dcef0413 3546static __be32 nfs4_seqid_op_checks(struct nfsd4_compound_state *cstate, stateid_t *stateid, u32 seqid, struct nfs4_ol_stateid *stp)
c0a5d93e
BF
3547{
3548 struct svc_fh *current_fh = &cstate->current_fh;
3549 struct nfs4_stateowner *sop = stp->st_stateowner;
3550 __be32 status;
3551
c0a5d93e
BF
3552 status = nfsd4_check_seqid(cstate, sop, seqid);
3553 if (status)
3554 return status;
f7a4d872
BF
3555 if (stp->st_stid.sc_type == NFS4_CLOSED_STID)
3556 /*
3557 * "Closed" stateid's exist *only* to return
3558 * nfserr_replay_me from the previous step.
3559 */
3560 return nfserr_bad_stateid;
3561 status = check_stateid_generation(stateid, &stp->st_stid.sc_stateid, nfsd4_has_session(cstate));
3562 if (status)
3563 return status;
3564 return nfs4_check_fh(current_fh, stp);
c0a5d93e
BF
3565}
3566
1da177e4
LT
3567/*
3568 * Checks for sequence id mutating operations.
3569 */
b37ad28b 3570static __be32
dd453dfd 3571nfs4_preprocess_seqid_op(struct nfsd4_compound_state *cstate, u32 seqid,
2288d0e3 3572 stateid_t *stateid, char typemask,
dcef0413 3573 struct nfs4_ol_stateid **stpp)
1da177e4 3574{
0836f587 3575 __be32 status;
38c2f4b1 3576 struct nfs4_stid *s;
1da177e4 3577
8c10cbdb
BH
3578 dprintk("NFSD: %s: seqid=%d stateid = " STATEID_FMT "\n", __func__,
3579 seqid, STATEID_VAL(stateid));
3a4f98bb 3580
1da177e4 3581 *stpp = NULL;
38c2f4b1 3582 status = nfsd4_lookup_stateid(stateid, typemask, &s);
c0a5d93e
BF
3583 if (status)
3584 return status;
38c2f4b1 3585 *stpp = openlockstateid(s);
c0a5d93e 3586 cstate->replay_owner = (*stpp)->st_stateowner;
1da177e4 3587
c0a5d93e
BF
3588 return nfs4_seqid_op_checks(cstate, stateid, seqid, *stpp);
3589}
39325bd0 3590
dcef0413 3591static __be32 nfs4_preprocess_confirmed_seqid_op(struct nfsd4_compound_state *cstate, u32 seqid, stateid_t *stateid, struct nfs4_ol_stateid **stpp)
c0a5d93e
BF
3592{
3593 __be32 status;
3594 struct nfs4_openowner *oo;
1da177e4 3595
c0a5d93e 3596 status = nfs4_preprocess_seqid_op(cstate, seqid, stateid,
2288d0e3 3597 NFS4_OPEN_STID, stpp);
7a8711c9
BF
3598 if (status)
3599 return status;
c0a5d93e 3600 oo = openowner((*stpp)->st_stateowner);
dad1c067 3601 if (!(oo->oo_flags & NFS4_OO_CONFIRMED))
3a4f98bb 3602 return nfserr_bad_stateid;
3a4f98bb 3603 return nfs_ok;
1da177e4
LT
3604}
3605
b37ad28b 3606__be32
ca364317 3607nfsd4_open_confirm(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
a4f1706a 3608 struct nfsd4_open_confirm *oc)
1da177e4 3609{
b37ad28b 3610 __be32 status;
fe0750e5 3611 struct nfs4_openowner *oo;
dcef0413 3612 struct nfs4_ol_stateid *stp;
1da177e4
LT
3613
3614 dprintk("NFSD: nfsd4_open_confirm on file %.*s\n",
ca364317
BF
3615 (int)cstate->current_fh.fh_dentry->d_name.len,
3616 cstate->current_fh.fh_dentry->d_name.name);
1da177e4 3617
ca364317 3618 status = fh_verify(rqstp, &cstate->current_fh, S_IFREG, 0);
a8cddc5d
BF
3619 if (status)
3620 return status;
1da177e4
LT
3621
3622 nfs4_lock_state();
3623
9072d5c6 3624 status = nfs4_preprocess_seqid_op(cstate,
ca364317 3625 oc->oc_seqid, &oc->oc_req_stateid,
2288d0e3 3626 NFS4_OPEN_STID, &stp);
9072d5c6 3627 if (status)
68b66e82 3628 goto out;
fe0750e5 3629 oo = openowner(stp->st_stateowner);
68b66e82 3630 status = nfserr_bad_stateid;
dad1c067 3631 if (oo->oo_flags & NFS4_OO_CONFIRMED)
68b66e82 3632 goto out;
dad1c067 3633 oo->oo_flags |= NFS4_OO_CONFIRMED;
dcef0413
BF
3634 update_stateid(&stp->st_stid.sc_stateid);
3635 memcpy(&oc->oc_resp_stateid, &stp->st_stid.sc_stateid, sizeof(stateid_t));
8c10cbdb 3636 dprintk("NFSD: %s: success, seqid=%d stateid=" STATEID_FMT "\n",
dcef0413 3637 __func__, oc->oc_seqid, STATEID_VAL(&stp->st_stid.sc_stateid));
c7b9a459 3638
2a4317c5 3639 nfsd4_client_record_create(oo->oo_owner.so_client);
68b66e82 3640 status = nfs_ok;
1da177e4 3641out:
5ec094c1
BF
3642 if (!cstate->replay_owner)
3643 nfs4_unlock_state();
1da177e4
LT
3644 return status;
3645}
3646
6409a5a6 3647static inline void nfs4_stateid_downgrade_bit(struct nfs4_ol_stateid *stp, u32 access)
1da177e4 3648{
6409a5a6
BF
3649 if (!test_bit(access, &stp->st_access_bmap))
3650 return;
3651 nfs4_file_put_access(stp->st_file, nfs4_access_to_omode(access));
3652 __clear_bit(access, &stp->st_access_bmap);
3653}
f197c271 3654
6409a5a6
BF
3655static inline void nfs4_stateid_downgrade(struct nfs4_ol_stateid *stp, u32 to_access)
3656{
3657 switch (to_access) {
3658 case NFS4_SHARE_ACCESS_READ:
3659 nfs4_stateid_downgrade_bit(stp, NFS4_SHARE_ACCESS_WRITE);
3660 nfs4_stateid_downgrade_bit(stp, NFS4_SHARE_ACCESS_BOTH);
3661 break;
3662 case NFS4_SHARE_ACCESS_WRITE:
3663 nfs4_stateid_downgrade_bit(stp, NFS4_SHARE_ACCESS_READ);
3664 nfs4_stateid_downgrade_bit(stp, NFS4_SHARE_ACCESS_BOTH);
3665 break;
3666 case NFS4_SHARE_ACCESS_BOTH:
3667 break;
3668 default:
3669 BUG();
1da177e4
LT
3670 }
3671}
3672
3673static void
3674reset_union_bmap_deny(unsigned long deny, unsigned long *bmap)
3675{
3676 int i;
3677 for (i = 0; i < 4; i++) {
3678 if ((i & deny) != i)
3679 __clear_bit(i, bmap);
3680 }
3681}
3682
b37ad28b 3683__be32
ca364317
BF
3684nfsd4_open_downgrade(struct svc_rqst *rqstp,
3685 struct nfsd4_compound_state *cstate,
a4f1706a 3686 struct nfsd4_open_downgrade *od)
1da177e4 3687{
b37ad28b 3688 __be32 status;
dcef0413 3689 struct nfs4_ol_stateid *stp;
1da177e4
LT
3690
3691 dprintk("NFSD: nfsd4_open_downgrade on file %.*s\n",
ca364317
BF
3692 (int)cstate->current_fh.fh_dentry->d_name.len,
3693 cstate->current_fh.fh_dentry->d_name.name);
1da177e4 3694
c30e92df 3695 /* We don't yet support WANT bits: */
2c8bd7e0
BH
3696 if (od->od_deleg_want)
3697 dprintk("NFSD: %s: od_deleg_want=0x%x ignored\n", __func__,
3698 od->od_deleg_want);
1da177e4
LT
3699
3700 nfs4_lock_state();
c0a5d93e
BF
3701 status = nfs4_preprocess_confirmed_seqid_op(cstate, od->od_seqid,
3702 &od->od_stateid, &stp);
9072d5c6 3703 if (status)
1da177e4 3704 goto out;
1da177e4
LT
3705 status = nfserr_inval;
3706 if (!test_bit(od->od_share_access, &stp->st_access_bmap)) {
3707 dprintk("NFSD:access not a subset current bitmap: 0x%lx, input access=%08x\n",
3708 stp->st_access_bmap, od->od_share_access);
3709 goto out;
3710 }
3711 if (!test_bit(od->od_share_deny, &stp->st_deny_bmap)) {
3712 dprintk("NFSD:deny not a subset current bitmap: 0x%lx, input deny=%08x\n",
3713 stp->st_deny_bmap, od->od_share_deny);
3714 goto out;
3715 }
6409a5a6 3716 nfs4_stateid_downgrade(stp, od->od_share_access);
1da177e4 3717
1da177e4
LT
3718 reset_union_bmap_deny(od->od_share_deny, &stp->st_deny_bmap);
3719
dcef0413
BF
3720 update_stateid(&stp->st_stid.sc_stateid);
3721 memcpy(&od->od_stateid, &stp->st_stid.sc_stateid, sizeof(stateid_t));
1da177e4
LT
3722 status = nfs_ok;
3723out:
5ec094c1
BF
3724 if (!cstate->replay_owner)
3725 nfs4_unlock_state();
1da177e4
LT
3726 return status;
3727}
3728
38c387b5
BF
3729void nfsd4_purge_closed_stateid(struct nfs4_stateowner *so)
3730{
3731 struct nfs4_openowner *oo;
3732 struct nfs4_ol_stateid *s;
3733
3734 if (!so->so_is_open_owner)
3735 return;
3736 oo = openowner(so);
3737 s = oo->oo_last_closed_stid;
3738 if (!s)
3739 return;
3740 if (!(oo->oo_flags & NFS4_OO_PURGE_CLOSE)) {
3741 /* Release the last_closed_stid on the next seqid bump: */
3742 oo->oo_flags |= NFS4_OO_PURGE_CLOSE;
3743 return;
3744 }
3745 oo->oo_flags &= ~NFS4_OO_PURGE_CLOSE;
f7a4d872
BF
3746 release_last_closed_stateid(oo);
3747}
3748
3749static void nfsd4_close_open_stateid(struct nfs4_ol_stateid *s)
3750{
3751 unhash_open_stateid(s);
3752 s->st_stid.sc_type = NFS4_CLOSED_STID;
38c387b5
BF
3753}
3754
1da177e4
LT
3755/*
3756 * nfs4_unlock_state() called after encode
3757 */
b37ad28b 3758__be32
ca364317 3759nfsd4_close(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
a4f1706a 3760 struct nfsd4_close *close)
1da177e4 3761{
b37ad28b 3762 __be32 status;
fe0750e5 3763 struct nfs4_openowner *oo;
dcef0413 3764 struct nfs4_ol_stateid *stp;
1da177e4
LT
3765
3766 dprintk("NFSD: nfsd4_close on file %.*s\n",
ca364317
BF
3767 (int)cstate->current_fh.fh_dentry->d_name.len,
3768 cstate->current_fh.fh_dentry->d_name.name);
1da177e4
LT
3769
3770 nfs4_lock_state();
f7a4d872
BF
3771 status = nfs4_preprocess_seqid_op(cstate, close->cl_seqid,
3772 &close->cl_stateid,
3773 NFS4_OPEN_STID|NFS4_CLOSED_STID,
3774 &stp);
9072d5c6 3775 if (status)
1da177e4 3776 goto out;
fe0750e5 3777 oo = openowner(stp->st_stateowner);
1da177e4 3778 status = nfs_ok;
dcef0413
BF
3779 update_stateid(&stp->st_stid.sc_stateid);
3780 memcpy(&close->cl_stateid, &stp->st_stid.sc_stateid, sizeof(stateid_t));
1da177e4 3781
f7a4d872 3782 nfsd4_close_open_stateid(stp);
38c387b5 3783 oo->oo_last_closed_stid = stp;
04ef5954
BF
3784
3785 /* place unused nfs4_stateowners on so_close_lru list to be
3786 * released by the laundromat service after the lease period
3787 * to enable us to handle CLOSE replay
3788 */
fe0750e5
BF
3789 if (list_empty(&oo->oo_owner.so_stateids))
3790 move_to_close_lru(oo);
1da177e4 3791out:
5ec094c1
BF
3792 if (!cstate->replay_owner)
3793 nfs4_unlock_state();
1da177e4
LT
3794 return status;
3795}
3796
b37ad28b 3797__be32
ca364317
BF
3798nfsd4_delegreturn(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
3799 struct nfsd4_delegreturn *dr)
1da177e4 3800{
203a8c8e
BF
3801 struct nfs4_delegation *dp;
3802 stateid_t *stateid = &dr->dr_stateid;
38c2f4b1 3803 struct nfs4_stid *s;
203a8c8e 3804 struct inode *inode;
b37ad28b 3805 __be32 status;
1da177e4 3806
ca364317 3807 if ((status = fh_verify(rqstp, &cstate->current_fh, S_IFREG, 0)))
203a8c8e
BF
3808 return status;
3809 inode = cstate->current_fh.fh_dentry->d_inode;
1da177e4
LT
3810
3811 nfs4_lock_state();
38c2f4b1
BF
3812 status = nfsd4_lookup_stateid(stateid, NFS4_DELEG_STID, &s);
3813 if (status)
203a8c8e 3814 goto out;
38c2f4b1 3815 dp = delegstateid(s);
d5477a8d 3816 status = check_stateid_generation(stateid, &dp->dl_stid.sc_stateid, nfsd4_has_session(cstate));
203a8c8e
BF
3817 if (status)
3818 goto out;
203a8c8e
BF
3819
3820 unhash_delegation(dp);
1da177e4 3821out:
203a8c8e
BF
3822 nfs4_unlock_state();
3823
1da177e4
LT
3824 return status;
3825}
3826
3827
1da177e4 3828#define LOFF_OVERFLOW(start, len) ((u64)(len) > ~(u64)(start))
1da177e4 3829
009673b4
BF
3830#define LOCKOWNER_INO_HASH_BITS 8
3831#define LOCKOWNER_INO_HASH_SIZE (1 << LOCKOWNER_INO_HASH_BITS)
3832#define LOCKOWNER_INO_HASH_MASK (LOCKOWNER_INO_HASH_SIZE - 1)
1da177e4 3833
87df4de8
BH
3834static inline u64
3835end_offset(u64 start, u64 len)
3836{
3837 u64 end;
3838
3839 end = start + len;
3840 return end >= start ? end: NFS4_MAX_UINT64;
3841}
3842
3843/* last octet in a range */
3844static inline u64
3845last_byte_offset(u64 start, u64 len)
3846{
3847 u64 end;
3848
3849 BUG_ON(!len);
3850 end = start + len;
3851 return end > start ? end - 1: NFS4_MAX_UINT64;
3852}
3853
009673b4 3854static unsigned int lockowner_ino_hashval(struct inode *inode, u32 cl_id, struct xdr_netobj *ownername)
1da177e4
LT
3855{
3856 return (file_hashval(inode) + cl_id
3857 + opaque_hashval(ownername->data, ownername->len))
009673b4 3858 & LOCKOWNER_INO_HASH_MASK;
1da177e4
LT
3859}
3860
009673b4 3861static struct list_head lockowner_ino_hashtbl[LOCKOWNER_INO_HASH_SIZE];
1da177e4 3862
1da177e4
LT
3863/*
3864 * TODO: Linux file offsets are _signed_ 64-bit quantities, which means that
3865 * we can't properly handle lock requests that go beyond the (2^63 - 1)-th
3866 * byte, because of sign extension problems. Since NFSv4 calls for 64-bit
3867 * locking, this prevents us from being completely protocol-compliant. The
3868 * real solution to this problem is to start using unsigned file offsets in
3869 * the VFS, but this is a very deep change!
3870 */
3871static inline void
3872nfs4_transform_lock_offset(struct file_lock *lock)
3873{
3874 if (lock->fl_start < 0)
3875 lock->fl_start = OFFSET_MAX;
3876 if (lock->fl_end < 0)
3877 lock->fl_end = OFFSET_MAX;
3878}
3879
d5b9026a
N
3880/* Hack!: For now, we're defining this just so we can use a pointer to it
3881 * as a unique cookie to identify our (NFSv4's) posix locks. */
7b021967 3882static const struct lock_manager_operations nfsd_posix_mng_ops = {
d5b9026a 3883};
1da177e4
LT
3884
3885static inline void
3886nfs4_set_lock_denied(struct file_lock *fl, struct nfsd4_lock_denied *deny)
3887{
fe0750e5 3888 struct nfs4_lockowner *lo;
1da177e4 3889
d5b9026a 3890 if (fl->fl_lmops == &nfsd_posix_mng_ops) {
fe0750e5
BF
3891 lo = (struct nfs4_lockowner *) fl->fl_owner;
3892 deny->ld_owner.data = kmemdup(lo->lo_owner.so_owner.data,
3893 lo->lo_owner.so_owner.len, GFP_KERNEL);
7c13f344
BF
3894 if (!deny->ld_owner.data)
3895 /* We just don't care that much */
3896 goto nevermind;
fe0750e5
BF
3897 deny->ld_owner.len = lo->lo_owner.so_owner.len;
3898 deny->ld_clientid = lo->lo_owner.so_client->cl_clientid;
d5b9026a 3899 } else {
7c13f344
BF
3900nevermind:
3901 deny->ld_owner.len = 0;
3902 deny->ld_owner.data = NULL;
d5b9026a
N
3903 deny->ld_clientid.cl_boot = 0;
3904 deny->ld_clientid.cl_id = 0;
1da177e4
LT
3905 }
3906 deny->ld_start = fl->fl_start;
87df4de8
BH
3907 deny->ld_length = NFS4_MAX_UINT64;
3908 if (fl->fl_end != NFS4_MAX_UINT64)
1da177e4
LT
3909 deny->ld_length = fl->fl_end - fl->fl_start + 1;
3910 deny->ld_type = NFS4_READ_LT;
3911 if (fl->fl_type != F_RDLCK)
3912 deny->ld_type = NFS4_WRITE_LT;
3913}
3914
b93d87c1
BF
3915static bool same_lockowner_ino(struct nfs4_lockowner *lo, struct inode *inode, clientid_t *clid, struct xdr_netobj *owner)
3916{
3917 struct nfs4_ol_stateid *lst;
3918
3919 if (!same_owner_str(&lo->lo_owner, owner, clid))
3920 return false;
3921 lst = list_first_entry(&lo->lo_owner.so_stateids,
3922 struct nfs4_ol_stateid, st_perstateowner);
3923 return lst->st_file->fi_inode == inode;
3924}
3925
fe0750e5
BF
3926static struct nfs4_lockowner *
3927find_lockowner_str(struct inode *inode, clientid_t *clid,
1da177e4
LT
3928 struct xdr_netobj *owner)
3929{
009673b4 3930 unsigned int hashval = lockowner_ino_hashval(inode, clid->cl_id, owner);
b93d87c1 3931 struct nfs4_lockowner *lo;
1da177e4 3932
009673b4 3933 list_for_each_entry(lo, &lockowner_ino_hashtbl[hashval], lo_owner_ino_hash) {
b93d87c1
BF
3934 if (same_lockowner_ino(lo, inode, clid, owner))
3935 return lo;
1da177e4
LT
3936 }
3937 return NULL;
3938}
3939
dcef0413 3940static void hash_lockowner(struct nfs4_lockowner *lo, unsigned int strhashval, struct nfs4_client *clp, struct nfs4_ol_stateid *open_stp)
ff194bd9 3941{
009673b4
BF
3942 struct inode *inode = open_stp->st_file->fi_inode;
3943 unsigned int inohash = lockowner_ino_hashval(inode,
3944 clp->cl_clientid.cl_id, &lo->lo_owner.so_owner);
3945
16bfdaaf 3946 list_add(&lo->lo_owner.so_strhash, &ownerstr_hashtbl[strhashval]);
009673b4 3947 list_add(&lo->lo_owner_ino_hash, &lockowner_ino_hashtbl[inohash]);
fe0750e5 3948 list_add(&lo->lo_perstateid, &open_stp->st_lockowners);
ff194bd9
BF
3949}
3950
1da177e4
LT
3951/*
3952 * Alloc a lock owner structure.
3953 * Called in nfsd4_lock - therefore, OPEN and OPEN_CONFIRM (if needed) has
25985edc 3954 * occurred.
1da177e4 3955 *
16bfdaaf 3956 * strhashval = ownerstr_hashval
1da177e4
LT
3957 */
3958
fe0750e5 3959static struct nfs4_lockowner *
dcef0413 3960alloc_init_lock_stateowner(unsigned int strhashval, struct nfs4_client *clp, struct nfs4_ol_stateid *open_stp, struct nfsd4_lock *lock) {
fe0750e5 3961 struct nfs4_lockowner *lo;
1da177e4 3962
fe0750e5
BF
3963 lo = alloc_stateowner(lockowner_slab, &lock->lk_new_owner, clp);
3964 if (!lo)
1da177e4 3965 return NULL;
fe0750e5
BF
3966 INIT_LIST_HEAD(&lo->lo_owner.so_stateids);
3967 lo->lo_owner.so_is_open_owner = 0;
b59e3c0e
NB
3968 /* It is the openowner seqid that will be incremented in encode in the
3969 * case of new lockowners; so increment the lock seqid manually: */
fe0750e5
BF
3970 lo->lo_owner.so_seqid = lock->lk_new_lock_seqid + 1;
3971 hash_lockowner(lo, strhashval, clp, open_stp);
3972 return lo;
1da177e4
LT
3973}
3974
dcef0413
BF
3975static struct nfs4_ol_stateid *
3976alloc_init_lock_stateid(struct nfs4_lockowner *lo, struct nfs4_file *fp, struct nfs4_ol_stateid *open_stp)
1da177e4 3977{
dcef0413 3978 struct nfs4_ol_stateid *stp;
d3b313a4 3979 struct nfs4_client *clp = lo->lo_owner.so_client;
1da177e4 3980
996e0938 3981 stp = nfs4_alloc_stateid(clp);
5ac049ac 3982 if (stp == NULL)
6136d2b4 3983 return NULL;
996e0938 3984 init_stid(&stp->st_stid, clp, NFS4_LOCK_STID);
8beefa24 3985 list_add(&stp->st_perfile, &fp->fi_stateids);
fe0750e5
BF
3986 list_add(&stp->st_perstateowner, &lo->lo_owner.so_stateids);
3987 stp->st_stateowner = &lo->lo_owner;
13cd2184 3988 get_nfs4_file(fp);
1da177e4 3989 stp->st_file = fp;
0997b173 3990 stp->st_access_bmap = 0;
1da177e4 3991 stp->st_deny_bmap = open_stp->st_deny_bmap;
4c4cd222 3992 stp->st_openstp = open_stp;
1da177e4
LT
3993 return stp;
3994}
3995
fd39ca9a 3996static int
1da177e4
LT
3997check_lock_length(u64 offset, u64 length)
3998{
87df4de8 3999 return ((length == 0) || ((length != NFS4_MAX_UINT64) &&
1da177e4
LT
4000 LOFF_OVERFLOW(offset, length)));
4001}
4002
dcef0413 4003static void get_lock_access(struct nfs4_ol_stateid *lock_stp, u32 access)
0997b173
BF
4004{
4005 struct nfs4_file *fp = lock_stp->st_file;
4006 int oflag = nfs4_access_to_omode(access);
4007
4008 if (test_bit(access, &lock_stp->st_access_bmap))
4009 return;
4010 nfs4_file_get_access(fp, oflag);
4011 __set_bit(access, &lock_stp->st_access_bmap);
4012}
4013
2355c596 4014static __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
4015{
4016 struct nfs4_file *fi = ost->st_file;
4017 struct nfs4_openowner *oo = openowner(ost->st_stateowner);
4018 struct nfs4_client *cl = oo->oo_owner.so_client;
4019 struct nfs4_lockowner *lo;
4020 unsigned int strhashval;
4021
4022 lo = find_lockowner_str(fi->fi_inode, &cl->cl_clientid, &lock->v.new.owner);
4023 if (lo) {
4024 if (!cstate->minorversion)
4025 return nfserr_bad_seqid;
4026 /* XXX: a lockowner always has exactly one stateid: */
4027 *lst = list_first_entry(&lo->lo_owner.so_stateids,
4028 struct nfs4_ol_stateid, st_perstateowner);
4029 return nfs_ok;
4030 }
16bfdaaf 4031 strhashval = ownerstr_hashval(cl->cl_clientid.cl_id,
64a284d0
BF
4032 &lock->v.new.owner);
4033 lo = alloc_init_lock_stateowner(strhashval, cl, ost, lock);
4034 if (lo == NULL)
4035 return nfserr_jukebox;
4036 *lst = alloc_init_lock_stateid(lo, fi, ost);
4037 if (*lst == NULL) {
4038 release_lockowner(lo);
4039 return nfserr_jukebox;
4040 }
4041 *new = true;
4042 return nfs_ok;
4043}
4044
1da177e4
LT
4045/*
4046 * LOCK operation
4047 */
b37ad28b 4048__be32
ca364317 4049nfsd4_lock(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
a4f1706a 4050 struct nfsd4_lock *lock)
1da177e4 4051{
fe0750e5
BF
4052 struct nfs4_openowner *open_sop = NULL;
4053 struct nfs4_lockowner *lock_sop = NULL;
dcef0413 4054 struct nfs4_ol_stateid *lock_stp;
7d947842 4055 struct file *filp = NULL;
1da177e4 4056 struct file_lock file_lock;
8dc7c311 4057 struct file_lock conflock;
b37ad28b 4058 __be32 status = 0;
64a284d0 4059 bool new_state = false;
b34f27aa 4060 int lkflg;
b8dd7b9a 4061 int err;
1da177e4
LT
4062
4063 dprintk("NFSD: nfsd4_lock: start=%Ld length=%Ld\n",
4064 (long long) lock->lk_offset,
4065 (long long) lock->lk_length);
4066
1da177e4
LT
4067 if (check_lock_length(lock->lk_offset, lock->lk_length))
4068 return nfserr_inval;
4069
ca364317 4070 if ((status = fh_verify(rqstp, &cstate->current_fh,
8837abca 4071 S_IFREG, NFSD_MAY_LOCK))) {
a6f6ef2f
AA
4072 dprintk("NFSD: nfsd4_lock: permission denied!\n");
4073 return status;
4074 }
4075
1da177e4
LT
4076 nfs4_lock_state();
4077
4078 if (lock->lk_is_new) {
893f8770
N
4079 /*
4080 * Client indicates that this is a new lockowner.
4081 * Use open owner and open stateid to create lock owner and
4082 * lock stateid.
4083 */
dcef0413 4084 struct nfs4_ol_stateid *open_stp = NULL;
684e5638
BF
4085
4086 if (nfsd4_has_session(cstate))
4087 /* See rfc 5661 18.10.3: given clientid is ignored: */
4088 memcpy(&lock->v.new.clientid,
4089 &cstate->session->se_client->cl_clientid,
4090 sizeof(clientid_t));
4091
1da177e4 4092 status = nfserr_stale_clientid;
684e5638 4093 if (STALE_CLIENTID(&lock->lk_new_clientid))
1da177e4 4094 goto out;
1da177e4 4095
1da177e4 4096 /* validate and update open stateid and open seqid */
c0a5d93e 4097 status = nfs4_preprocess_confirmed_seqid_op(cstate,
1da177e4
LT
4098 lock->lk_new_open_seqid,
4099 &lock->lk_new_open_stateid,
c0a5d93e 4100 &open_stp);
37515177 4101 if (status)
1da177e4 4102 goto out;
fe0750e5 4103 open_sop = openowner(open_stp->st_stateowner);
b34f27aa 4104 status = nfserr_bad_stateid;
684e5638 4105 if (!same_clid(&open_sop->oo_owner.so_client->cl_clientid,
b34f27aa
BF
4106 &lock->v.new.clientid))
4107 goto out;
64a284d0
BF
4108 status = lookup_or_create_lock_state(cstate, open_stp, lock,
4109 &lock_stp, &new_state);
4110 if (status)
1da177e4 4111 goto out;
1da177e4
LT
4112 } else {
4113 /* lock (lock owner + lock stateid) already exists */
dd453dfd 4114 status = nfs4_preprocess_seqid_op(cstate,
fe0750e5
BF
4115 lock->lk_old_lock_seqid,
4116 &lock->lk_old_lock_stateid,
2288d0e3 4117 NFS4_LOCK_STID, &lock_stp);
1da177e4
LT
4118 if (status)
4119 goto out;
4120 }
64a284d0 4121 lock_sop = lockowner(lock_stp->st_stateowner);
1da177e4 4122
b34f27aa
BF
4123 lkflg = setlkflg(lock->lk_type);
4124 status = nfs4_check_openmode(lock_stp, lkflg);
4125 if (status)
4126 goto out;
4127
0dd395dc 4128 status = nfserr_grace;
af558e33 4129 if (locks_in_grace() && !lock->lk_reclaim)
0dd395dc
N
4130 goto out;
4131 status = nfserr_no_grace;
af558e33 4132 if (!locks_in_grace() && lock->lk_reclaim)
0dd395dc
N
4133 goto out;
4134
1da177e4
LT
4135 locks_init_lock(&file_lock);
4136 switch (lock->lk_type) {
4137 case NFS4_READ_LT:
4138 case NFS4_READW_LT:
0997b173
BF
4139 filp = find_readable_file(lock_stp->st_file);
4140 if (filp)
4141 get_lock_access(lock_stp, NFS4_SHARE_ACCESS_READ);
1da177e4 4142 file_lock.fl_type = F_RDLCK;
529d7b2a 4143 break;
1da177e4
LT
4144 case NFS4_WRITE_LT:
4145 case NFS4_WRITEW_LT:
0997b173
BF
4146 filp = find_writeable_file(lock_stp->st_file);
4147 if (filp)
4148 get_lock_access(lock_stp, NFS4_SHARE_ACCESS_WRITE);
1da177e4 4149 file_lock.fl_type = F_WRLCK;
529d7b2a 4150 break;
1da177e4
LT
4151 default:
4152 status = nfserr_inval;
4153 goto out;
4154 }
f9d7562f
BF
4155 if (!filp) {
4156 status = nfserr_openmode;
4157 goto out;
4158 }
b59e3c0e 4159 file_lock.fl_owner = (fl_owner_t)lock_sop;
1da177e4
LT
4160 file_lock.fl_pid = current->tgid;
4161 file_lock.fl_file = filp;
4162 file_lock.fl_flags = FL_POSIX;
d5b9026a 4163 file_lock.fl_lmops = &nfsd_posix_mng_ops;
1da177e4
LT
4164
4165 file_lock.fl_start = lock->lk_offset;
87df4de8 4166 file_lock.fl_end = last_byte_offset(lock->lk_offset, lock->lk_length);
1da177e4
LT
4167 nfs4_transform_lock_offset(&file_lock);
4168
4169 /*
4170 * Try to lock the file in the VFS.
4171 * Note: locks.c uses the BKL to protect the inode's lock list.
4172 */
4173
529d7b2a 4174 err = vfs_lock_file(filp, F_SETLK, &file_lock, &conflock);
b8dd7b9a 4175 switch (-err) {
1da177e4 4176 case 0: /* success! */
dcef0413
BF
4177 update_stateid(&lock_stp->st_stid.sc_stateid);
4178 memcpy(&lock->lk_resp_stateid, &lock_stp->st_stid.sc_stateid,
1da177e4 4179 sizeof(stateid_t));
b8dd7b9a 4180 status = 0;
eb76b3fd
AA
4181 break;
4182 case (EAGAIN): /* conflock holds conflicting lock */
4183 status = nfserr_denied;
4184 dprintk("NFSD: nfsd4_lock: conflicting lock found!\n");
4185 nfs4_set_lock_denied(&conflock, &lock->lk_denied);
4186 break;
1da177e4
LT
4187 case (EDEADLK):
4188 status = nfserr_deadlock;
eb76b3fd 4189 break;
3e772463 4190 default:
fd85b817 4191 dprintk("NFSD: nfsd4_lock: vfs_lock_file() failed! status %d\n",err);
3e772463 4192 status = nfserrno(err);
eb76b3fd 4193 break;
1da177e4 4194 }
1da177e4 4195out:
64a284d0 4196 if (status && new_state)
f044ff83 4197 release_lockowner(lock_sop);
5ec094c1
BF
4198 if (!cstate->replay_owner)
4199 nfs4_unlock_state();
1da177e4
LT
4200 return status;
4201}
4202
55ef1274
BF
4203/*
4204 * The NFSv4 spec allows a client to do a LOCKT without holding an OPEN,
4205 * so we do a temporary open here just to get an open file to pass to
4206 * vfs_test_lock. (Arguably perhaps test_lock should be done with an
4207 * inode operation.)
4208 */
4209static int nfsd_test_lock(struct svc_rqst *rqstp, struct svc_fh *fhp, struct file_lock *lock)
4210{
4211 struct file *file;
4212 int err;
4213
4214 err = nfsd_open(rqstp, fhp, S_IFREG, NFSD_MAY_READ, &file);
4215 if (err)
4216 return err;
4217 err = vfs_test_lock(file, lock);
4218 nfsd_close(file);
4219 return err;
4220}
4221
1da177e4
LT
4222/*
4223 * LOCKT operation
4224 */
b37ad28b 4225__be32
ca364317
BF
4226nfsd4_lockt(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
4227 struct nfsd4_lockt *lockt)
1da177e4
LT
4228{
4229 struct inode *inode;
1da177e4 4230 struct file_lock file_lock;
fe0750e5 4231 struct nfs4_lockowner *lo;
fd85b817 4232 int error;
b37ad28b 4233 __be32 status;
1da177e4 4234
af558e33 4235 if (locks_in_grace())
1da177e4
LT
4236 return nfserr_grace;
4237
4238 if (check_lock_length(lockt->lt_offset, lockt->lt_length))
4239 return nfserr_inval;
4240
1da177e4
LT
4241 nfs4_lock_state();
4242
4243 status = nfserr_stale_clientid;
60adfc50 4244 if (!nfsd4_has_session(cstate) && STALE_CLIENTID(&lockt->lt_clientid))
1da177e4 4245 goto out;
1da177e4 4246
75c096f7 4247 if ((status = fh_verify(rqstp, &cstate->current_fh, S_IFREG, 0)))
1da177e4 4248 goto out;
1da177e4 4249
ca364317 4250 inode = cstate->current_fh.fh_dentry->d_inode;
1da177e4
LT
4251 locks_init_lock(&file_lock);
4252 switch (lockt->lt_type) {
4253 case NFS4_READ_LT:
4254 case NFS4_READW_LT:
4255 file_lock.fl_type = F_RDLCK;
4256 break;
4257 case NFS4_WRITE_LT:
4258 case NFS4_WRITEW_LT:
4259 file_lock.fl_type = F_WRLCK;
4260 break;
4261 default:
2fdada03 4262 dprintk("NFSD: nfs4_lockt: bad lock type!\n");
1da177e4
LT
4263 status = nfserr_inval;
4264 goto out;
4265 }
4266
fe0750e5
BF
4267 lo = find_lockowner_str(inode, &lockt->lt_clientid, &lockt->lt_owner);
4268 if (lo)
4269 file_lock.fl_owner = (fl_owner_t)lo;
1da177e4
LT
4270 file_lock.fl_pid = current->tgid;
4271 file_lock.fl_flags = FL_POSIX;
4272
4273 file_lock.fl_start = lockt->lt_offset;
87df4de8 4274 file_lock.fl_end = last_byte_offset(lockt->lt_offset, lockt->lt_length);
1da177e4
LT
4275
4276 nfs4_transform_lock_offset(&file_lock);
4277
1da177e4 4278 status = nfs_ok;
55ef1274 4279 error = nfsd_test_lock(rqstp, &cstate->current_fh, &file_lock);
fd85b817
ME
4280 if (error) {
4281 status = nfserrno(error);
4282 goto out;
4283 }
9d6a8c5c 4284 if (file_lock.fl_type != F_UNLCK) {
1da177e4 4285 status = nfserr_denied;
9d6a8c5c 4286 nfs4_set_lock_denied(&file_lock, &lockt->lt_denied);
1da177e4
LT
4287 }
4288out:
4289 nfs4_unlock_state();
4290 return status;
4291}
4292
b37ad28b 4293__be32
ca364317 4294nfsd4_locku(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
a4f1706a 4295 struct nfsd4_locku *locku)
1da177e4 4296{
dcef0413 4297 struct nfs4_ol_stateid *stp;
1da177e4
LT
4298 struct file *filp = NULL;
4299 struct file_lock file_lock;
b37ad28b 4300 __be32 status;
b8dd7b9a 4301 int err;
1da177e4
LT
4302
4303 dprintk("NFSD: nfsd4_locku: start=%Ld length=%Ld\n",
4304 (long long) locku->lu_offset,
4305 (long long) locku->lu_length);
4306
4307 if (check_lock_length(locku->lu_offset, locku->lu_length))
4308 return nfserr_inval;
4309
4310 nfs4_lock_state();
4311
9072d5c6 4312 status = nfs4_preprocess_seqid_op(cstate, locku->lu_seqid,
2288d0e3 4313 &locku->lu_stateid, NFS4_LOCK_STID, &stp);
9072d5c6 4314 if (status)
1da177e4 4315 goto out;
f9d7562f
BF
4316 filp = find_any_file(stp->st_file);
4317 if (!filp) {
4318 status = nfserr_lock_range;
4319 goto out;
4320 }
1da177e4
LT
4321 BUG_ON(!filp);
4322 locks_init_lock(&file_lock);
4323 file_lock.fl_type = F_UNLCK;
fe0750e5 4324 file_lock.fl_owner = (fl_owner_t)lockowner(stp->st_stateowner);
1da177e4
LT
4325 file_lock.fl_pid = current->tgid;
4326 file_lock.fl_file = filp;
4327 file_lock.fl_flags = FL_POSIX;
d5b9026a 4328 file_lock.fl_lmops = &nfsd_posix_mng_ops;
1da177e4
LT
4329 file_lock.fl_start = locku->lu_offset;
4330
87df4de8 4331 file_lock.fl_end = last_byte_offset(locku->lu_offset, locku->lu_length);
1da177e4
LT
4332 nfs4_transform_lock_offset(&file_lock);
4333
4334 /*
4335 * Try to unlock the file in the VFS.
4336 */
fd85b817 4337 err = vfs_lock_file(filp, F_SETLK, &file_lock, NULL);
b8dd7b9a 4338 if (err) {
fd85b817 4339 dprintk("NFSD: nfs4_locku: vfs_lock_file failed!\n");
1da177e4
LT
4340 goto out_nfserr;
4341 }
4342 /*
4343 * OK, unlock succeeded; the only thing left to do is update the stateid.
4344 */
dcef0413
BF
4345 update_stateid(&stp->st_stid.sc_stateid);
4346 memcpy(&locku->lu_stateid, &stp->st_stid.sc_stateid, sizeof(stateid_t));
1da177e4
LT
4347
4348out:
71c3bcd7
BF
4349 if (!cstate->replay_owner)
4350 nfs4_unlock_state();
1da177e4
LT
4351 return status;
4352
4353out_nfserr:
b8dd7b9a 4354 status = nfserrno(err);
1da177e4
LT
4355 goto out;
4356}
4357
4358/*
4359 * returns
4360 * 1: locks held by lockowner
4361 * 0: no locks held by lockowner
4362 */
4363static int
fe0750e5 4364check_for_locks(struct nfs4_file *filp, struct nfs4_lockowner *lowner)
1da177e4
LT
4365{
4366 struct file_lock **flpp;
f9d7562f 4367 struct inode *inode = filp->fi_inode;
1da177e4
LT
4368 int status = 0;
4369
b89f4321 4370 lock_flocks();
1da177e4 4371 for (flpp = &inode->i_flock; *flpp != NULL; flpp = &(*flpp)->fl_next) {
796dadfd 4372 if ((*flpp)->fl_owner == (fl_owner_t)lowner) {
1da177e4
LT
4373 status = 1;
4374 goto out;
796dadfd 4375 }
1da177e4
LT
4376 }
4377out:
b89f4321 4378 unlock_flocks();
1da177e4
LT
4379 return status;
4380}
4381
b37ad28b 4382__be32
b591480b
BF
4383nfsd4_release_lockowner(struct svc_rqst *rqstp,
4384 struct nfsd4_compound_state *cstate,
4385 struct nfsd4_release_lockowner *rlockowner)
1da177e4
LT
4386{
4387 clientid_t *clid = &rlockowner->rl_clientid;
3e9e3dbe 4388 struct nfs4_stateowner *sop;
fe0750e5 4389 struct nfs4_lockowner *lo;
dcef0413 4390 struct nfs4_ol_stateid *stp;
1da177e4 4391 struct xdr_netobj *owner = &rlockowner->rl_owner;
3e9e3dbe 4392 struct list_head matches;
16bfdaaf 4393 unsigned int hashval = ownerstr_hashval(clid->cl_id, owner);
b37ad28b 4394 __be32 status;
1da177e4
LT
4395
4396 dprintk("nfsd4_release_lockowner clientid: (%08x/%08x):\n",
4397 clid->cl_boot, clid->cl_id);
4398
4399 /* XXX check for lease expiration */
4400
4401 status = nfserr_stale_clientid;
849823c5 4402 if (STALE_CLIENTID(clid))
1da177e4 4403 return status;
1da177e4
LT
4404
4405 nfs4_lock_state();
4406
3e9e3dbe 4407 status = nfserr_locks_held;
3e9e3dbe 4408 INIT_LIST_HEAD(&matches);
06f1f864 4409
16bfdaaf
BF
4410 list_for_each_entry(sop, &ownerstr_hashtbl[hashval], so_strhash) {
4411 if (sop->so_is_open_owner)
4412 continue;
06f1f864
BF
4413 if (!same_owner_str(sop, owner, clid))
4414 continue;
4415 list_for_each_entry(stp, &sop->so_stateids,
4416 st_perstateowner) {
4417 lo = lockowner(sop);
4418 if (check_for_locks(stp->st_file, lo))
4419 goto out;
4420 list_add(&lo->lo_list, &matches);
1da177e4 4421 }
3e9e3dbe
N
4422 }
4423 /* Clients probably won't expect us to return with some (but not all)
4424 * of the lockowner state released; so don't release any until all
4425 * have been checked. */
4426 status = nfs_ok;
0fa822e4 4427 while (!list_empty(&matches)) {
fe0750e5
BF
4428 lo = list_entry(matches.next, struct nfs4_lockowner,
4429 lo_list);
0fa822e4
N
4430 /* unhash_stateowner deletes so_perclient only
4431 * for openowners. */
fe0750e5
BF
4432 list_del(&lo->lo_list);
4433 release_lockowner(lo);
1da177e4
LT
4434 }
4435out:
4436 nfs4_unlock_state();
4437 return status;
4438}
4439
4440static inline struct nfs4_client_reclaim *
a55370a3 4441alloc_reclaim(void)
1da177e4 4442{
a55370a3 4443 return kmalloc(sizeof(struct nfs4_client_reclaim), GFP_KERNEL);
1da177e4
LT
4444}
4445
c7b9a459 4446int
a1bcecd2 4447nfs4_has_reclaimed_state(const char *name, bool use_exchange_id)
c7b9a459
N
4448{
4449 unsigned int strhashval = clientstr_hashval(name);
4450 struct nfs4_client *clp;
4451
e203d506 4452 clp = find_confirmed_client_by_str(name, strhashval);
393d8ed8
BF
4453 if (!clp)
4454 return 0;
a52d726b 4455 return test_bit(NFSD4_CLIENT_STABLE, &clp->cl_flags);
c7b9a459
N
4456}
4457
1da177e4
LT
4458/*
4459 * failure => all reset bets are off, nfserr_no_grace...
4460 */
190e4fbf
N
4461int
4462nfs4_client_to_reclaim(const char *name)
1da177e4
LT
4463{
4464 unsigned int strhashval;
4465 struct nfs4_client_reclaim *crp = NULL;
4466
a55370a3
N
4467 dprintk("NFSD nfs4_client_to_reclaim NAME: %.*s\n", HEXDIR_LEN, name);
4468 crp = alloc_reclaim();
1da177e4
LT
4469 if (!crp)
4470 return 0;
a55370a3 4471 strhashval = clientstr_hashval(name);
1da177e4
LT
4472 INIT_LIST_HEAD(&crp->cr_strhash);
4473 list_add(&crp->cr_strhash, &reclaim_str_hashtbl[strhashval]);
a55370a3 4474 memcpy(crp->cr_recdir, name, HEXDIR_LEN);
1da177e4
LT
4475 reclaim_str_hashtbl_size++;
4476 return 1;
4477}
4478
2a4317c5 4479void
1da177e4
LT
4480nfs4_release_reclaim(void)
4481{
4482 struct nfs4_client_reclaim *crp = NULL;
4483 int i;
4484
1da177e4
LT
4485 for (i = 0; i < CLIENT_HASH_SIZE; i++) {
4486 while (!list_empty(&reclaim_str_hashtbl[i])) {
4487 crp = list_entry(reclaim_str_hashtbl[i].next,
4488 struct nfs4_client_reclaim, cr_strhash);
4489 list_del(&crp->cr_strhash);
1da177e4
LT
4490 kfree(crp);
4491 reclaim_str_hashtbl_size--;
4492 }
4493 }
4494 BUG_ON(reclaim_str_hashtbl_size);
4495}
4496
4497/*
4498 * called from OPEN, CLAIM_PREVIOUS with a new clientid. */
2a4317c5 4499struct nfs4_client_reclaim *
a52d726b 4500nfsd4_find_reclaim_client(struct nfs4_client *clp)
1da177e4
LT
4501{
4502 unsigned int strhashval;
1da177e4
LT
4503 struct nfs4_client_reclaim *crp = NULL;
4504
a55370a3
N
4505 dprintk("NFSD: nfs4_find_reclaim_client for %.*s with recdir %s\n",
4506 clp->cl_name.len, clp->cl_name.data,
4507 clp->cl_recdir);
1da177e4
LT
4508
4509 /* find clp->cl_name in reclaim_str_hashtbl */
a55370a3 4510 strhashval = clientstr_hashval(clp->cl_recdir);
1da177e4 4511 list_for_each_entry(crp, &reclaim_str_hashtbl[strhashval], cr_strhash) {
a55370a3 4512 if (same_name(crp->cr_recdir, clp->cl_recdir)) {
1da177e4
LT
4513 return crp;
4514 }
4515 }
4516 return NULL;
4517}
4518
4519/*
4520* Called from OPEN. Look for clientid in reclaim list.
4521*/
b37ad28b 4522__be32
1da177e4
LT
4523nfs4_check_open_reclaim(clientid_t *clid)
4524{
a52d726b
JL
4525 struct nfs4_client *clp;
4526
4527 /* find clientid in conf_id_hashtbl */
4528 clp = find_confirmed_client(clid);
4529 if (clp == NULL)
4530 return nfserr_reclaim_bad;
4531
4532 return nfsd4_client_record_check(clp) ? nfserr_reclaim_bad : nfs_ok;
1da177e4
LT
4533}
4534
65178db4
BS
4535#ifdef CONFIG_NFSD_FAULT_INJECTION
4536
4537void nfsd_forget_clients(u64 num)
4538{
4539 struct nfs4_client *clp, *next;
4540 int count = 0;
4541
4542 nfs4_lock_state();
4543 list_for_each_entry_safe(clp, next, &client_lru, cl_lru) {
2a4317c5 4544 nfsd4_client_record_remove(clp);
65178db4
BS
4545 expire_client(clp);
4546 if (++count == num)
4547 break;
4548 }
4549 nfs4_unlock_state();
4550
4551 printk(KERN_INFO "NFSD: Forgot %d clients", count);
4552}
4553
4554static void release_lockowner_sop(struct nfs4_stateowner *sop)
4555{
4556 release_lockowner(lockowner(sop));
4557}
4558
4559static void release_openowner_sop(struct nfs4_stateowner *sop)
4560{
4561 release_openowner(openowner(sop));
4562}
4563
353de31b 4564static int nfsd_release_n_owners(u64 num, bool is_open_owner,
65178db4
BS
4565 void (*release_sop)(struct nfs4_stateowner *))
4566{
4567 int i, count = 0;
4568 struct nfs4_stateowner *sop, *next;
4569
16bfdaaf
BF
4570 for (i = 0; i < OWNER_HASH_SIZE; i++) {
4571 list_for_each_entry_safe(sop, next, &ownerstr_hashtbl[i], so_strhash) {
4572 if (sop->so_is_open_owner != is_open_owner)
4573 continue;
65178db4
BS
4574 release_sop(sop);
4575 if (++count == num)
4576 return count;
4577 }
4578 }
4579 return count;
4580}
4581
4582void nfsd_forget_locks(u64 num)
4583{
4584 int count;
4585
4586 nfs4_lock_state();
16bfdaaf 4587 count = nfsd_release_n_owners(num, false, release_lockowner_sop);
65178db4
BS
4588 nfs4_unlock_state();
4589
4590 printk(KERN_INFO "NFSD: Forgot %d locks", count);
4591}
4592
4593void nfsd_forget_openowners(u64 num)
4594{
4595 int count;
4596
4597 nfs4_lock_state();
16bfdaaf 4598 count = nfsd_release_n_owners(num, true, release_openowner_sop);
65178db4
BS
4599 nfs4_unlock_state();
4600
4601 printk(KERN_INFO "NFSD: Forgot %d open owners", count);
4602}
4603
4604int nfsd_process_n_delegations(u64 num, void (*deleg_func)(struct nfs4_delegation *))
4605{
4606 int i, count = 0;
2d3475c0
BS
4607 struct nfs4_file *fp, *fnext;
4608 struct nfs4_delegation *dp, *dnext;
65178db4
BS
4609
4610 for (i = 0; i < FILE_HASH_SIZE; i++) {
2d3475c0
BS
4611 list_for_each_entry_safe(fp, fnext, &file_hashtbl[i], fi_hash) {
4612 list_for_each_entry_safe(dp, dnext, &fp->fi_delegations, dl_perfile) {
65178db4
BS
4613 deleg_func(dp);
4614 if (++count == num)
4615 return count;
4616 }
4617 }
4618 }
2d3475c0 4619
65178db4
BS
4620 return count;
4621}
4622
4623void nfsd_forget_delegations(u64 num)
4624{
4625 unsigned int count;
4626
4627 nfs4_lock_state();
4628 count = nfsd_process_n_delegations(num, unhash_delegation);
4629 nfs4_unlock_state();
4630
4631 printk(KERN_INFO "NFSD: Forgot %d delegations", count);
4632}
4633
4634void nfsd_recall_delegations(u64 num)
4635{
4636 unsigned int count;
4637
4638 nfs4_lock_state();
4639 spin_lock(&recall_lock);
4640 count = nfsd_process_n_delegations(num, nfsd_break_one_deleg);
4641 spin_unlock(&recall_lock);
4642 nfs4_unlock_state();
4643
4644 printk(KERN_INFO "NFSD: Recalled %d delegations", count);
4645}
4646
4647#endif /* CONFIG_NFSD_FAULT_INJECTION */
4648
ac4d8ff2 4649/* initialization to perform at module load time: */
1da177e4 4650
72083396 4651void
ac4d8ff2 4652nfs4_state_init(void)
1da177e4 4653{
72083396 4654 int i;
1da177e4 4655
1da177e4
LT
4656 for (i = 0; i < CLIENT_HASH_SIZE; i++) {
4657 INIT_LIST_HEAD(&conf_id_hashtbl[i]);
4658 INIT_LIST_HEAD(&conf_str_hashtbl[i]);
4659 INIT_LIST_HEAD(&unconf_str_hashtbl[i]);
4660 INIT_LIST_HEAD(&unconf_id_hashtbl[i]);
02cb2858 4661 INIT_LIST_HEAD(&reclaim_str_hashtbl[i]);
1da177e4 4662 }
5282fd72
ME
4663 for (i = 0; i < SESSION_HASH_SIZE; i++)
4664 INIT_LIST_HEAD(&sessionid_hashtbl[i]);
1da177e4
LT
4665 for (i = 0; i < FILE_HASH_SIZE; i++) {
4666 INIT_LIST_HEAD(&file_hashtbl[i]);
4667 }
16bfdaaf
BF
4668 for (i = 0; i < OWNER_HASH_SIZE; i++) {
4669 INIT_LIST_HEAD(&ownerstr_hashtbl[i]);
1da177e4 4670 }
009673b4
BF
4671 for (i = 0; i < LOCKOWNER_INO_HASH_SIZE; i++)
4672 INIT_LIST_HEAD(&lockowner_ino_hashtbl[i]);
1da177e4
LT
4673 INIT_LIST_HEAD(&close_lru);
4674 INIT_LIST_HEAD(&client_lru);
4675 INIT_LIST_HEAD(&del_recall_lru);
ac4d8ff2 4676 reclaim_str_hashtbl_size = 0;
ac4d8ff2
N
4677}
4678
c2f1a551
MS
4679/*
4680 * Since the lifetime of a delegation isn't limited to that of an open, a
4681 * client may quite reasonably hang on to a delegation as long as it has
4682 * the inode cached. This becomes an obvious problem the first time a
4683 * client's inode cache approaches the size of the server's total memory.
4684 *
4685 * For now we avoid this problem by imposing a hard limit on the number
4686 * of delegations, which varies according to the server's memory size.
4687 */
4688static void
4689set_max_delegations(void)
4690{
4691 /*
4692 * Allow at most 4 delegations per megabyte of RAM. Quick
4693 * estimates suggest that in the worst case (where every delegation
4694 * is for a different inode), a delegation could take about 1.5K,
4695 * giving a worst case usage of about 6% of memory.
4696 */
4697 max_delegations = nr_free_buffer_pages() >> (20 - 2 - PAGE_SHIFT);
4698}
4699
ac4d8ff2
N
4700/* initialization to perform when the nfsd service is started: */
4701
a6d6b781
JL
4702int
4703nfs4_state_start(void)
ac4d8ff2 4704{
b5a1a81e
BF
4705 int ret;
4706
2a4317c5
JL
4707 /*
4708 * FIXME: For now, we hang most of the pernet global stuff off of
4709 * init_net until nfsd is fully containerized. Eventually, we'll
4710 * need to pass a net pointer into this function, take a reference
4711 * to that instead and then do most of the rest of this on a per-net
4712 * basis.
4713 */
4714 get_net(&init_net);
4715 nfsd4_client_tracking_init(&init_net);
1da177e4 4716 boot_time = get_seconds();
af558e33 4717 locks_start_grace(&nfsd4_manager);
33dcc481 4718 grace_ended = false;
9a8db97e 4719 printk(KERN_INFO "NFSD: starting %ld-second grace period\n",
e46b498c 4720 nfsd4_grace);
b5a1a81e 4721 ret = set_callback_cred();
a6d6b781
JL
4722 if (ret) {
4723 ret = -ENOMEM;
4724 goto out_recovery;
4725 }
58da282b 4726 laundry_wq = create_singlethread_workqueue("nfsd4");
a6d6b781
JL
4727 if (laundry_wq == NULL) {
4728 ret = -ENOMEM;
4729 goto out_recovery;
4730 }
b5a1a81e
BF
4731 ret = nfsd4_create_callback_queue();
4732 if (ret)
4733 goto out_free_laundry;
e46b498c 4734 queue_delayed_work(laundry_wq, &laundromat_work, nfsd4_grace * HZ);
c2f1a551 4735 set_max_delegations();
b5a1a81e
BF
4736 return 0;
4737out_free_laundry:
4738 destroy_workqueue(laundry_wq);
a6d6b781 4739out_recovery:
2a4317c5
JL
4740 nfsd4_client_tracking_exit(&init_net);
4741 put_net(&init_net);
b5a1a81e 4742 return ret;
1da177e4
LT
4743}
4744
1da177e4
LT
4745static void
4746__nfs4_state_shutdown(void)
4747{
4748 int i;
4749 struct nfs4_client *clp = NULL;
4750 struct nfs4_delegation *dp = NULL;
1da177e4
LT
4751 struct list_head *pos, *next, reaplist;
4752
1da177e4
LT
4753 for (i = 0; i < CLIENT_HASH_SIZE; i++) {
4754 while (!list_empty(&conf_id_hashtbl[i])) {
4755 clp = list_entry(conf_id_hashtbl[i].next, struct nfs4_client, cl_idhash);
4756 expire_client(clp);
4757 }
4758 while (!list_empty(&unconf_str_hashtbl[i])) {
4759 clp = list_entry(unconf_str_hashtbl[i].next, struct nfs4_client, cl_strhash);
4760 expire_client(clp);
4761 }
4762 }
4763 INIT_LIST_HEAD(&reaplist);
4764 spin_lock(&recall_lock);
4765 list_for_each_safe(pos, next, &del_recall_lru) {
4766 dp = list_entry (pos, struct nfs4_delegation, dl_recall_lru);
4767 list_move(&dp->dl_recall_lru, &reaplist);
4768 }
4769 spin_unlock(&recall_lock);
4770 list_for_each_safe(pos, next, &reaplist) {
4771 dp = list_entry (pos, struct nfs4_delegation, dl_recall_lru);
1da177e4
LT
4772 unhash_delegation(dp);
4773 }
4774
2a4317c5
JL
4775 nfsd4_client_tracking_exit(&init_net);
4776 put_net(&init_net);
1da177e4
LT
4777}
4778
4779void
4780nfs4_state_shutdown(void)
4781{
afe2c511 4782 cancel_delayed_work_sync(&laundromat_work);
5e8d5c29 4783 destroy_workqueue(laundry_wq);
2c5e7615 4784 locks_end_grace(&nfsd4_manager);
1da177e4 4785 nfs4_lock_state();
1da177e4 4786 __nfs4_state_shutdown();
1da177e4 4787 nfs4_unlock_state();
c3935e30 4788 nfsd4_destroy_callback_queue();
1da177e4 4789}
8b70484c
TM
4790
4791static void
4792get_stateid(struct nfsd4_compound_state *cstate, stateid_t *stateid)
4793{
37c593c5
TM
4794 if (HAS_STATE_ID(cstate, CURRENT_STATE_ID_FLAG) && CURRENT_STATEID(stateid))
4795 memcpy(stateid, &cstate->current_stateid, sizeof(stateid_t));
8b70484c
TM
4796}
4797
4798static void
4799put_stateid(struct nfsd4_compound_state *cstate, stateid_t *stateid)
4800{
37c593c5
TM
4801 if (cstate->minorversion) {
4802 memcpy(&cstate->current_stateid, stateid, sizeof(stateid_t));
4803 SET_STATE_ID(cstate, CURRENT_STATE_ID_FLAG);
4804 }
4805}
4806
4807void
4808clear_current_stateid(struct nfsd4_compound_state *cstate)
4809{
4810 CLEAR_STATE_ID(cstate, CURRENT_STATE_ID_FLAG);
8b70484c
TM
4811}
4812
62cd4a59
TM
4813/*
4814 * functions to set current state id
4815 */
9428fe1a
TM
4816void
4817nfsd4_set_opendowngradestateid(struct nfsd4_compound_state *cstate, struct nfsd4_open_downgrade *odp)
4818{
4819 put_stateid(cstate, &odp->od_stateid);
4820}
4821
8b70484c
TM
4822void
4823nfsd4_set_openstateid(struct nfsd4_compound_state *cstate, struct nfsd4_open *open)
4824{
4825 put_stateid(cstate, &open->op_stateid);
4826}
4827
62cd4a59
TM
4828void
4829nfsd4_set_closestateid(struct nfsd4_compound_state *cstate, struct nfsd4_close *close)
4830{
4831 put_stateid(cstate, &close->cl_stateid);
4832}
4833
4834void
4835nfsd4_set_lockstateid(struct nfsd4_compound_state *cstate, struct nfsd4_lock *lock)
4836{
4837 put_stateid(cstate, &lock->lk_resp_stateid);
4838}
4839
4840/*
4841 * functions to consume current state id
4842 */
1e97b519 4843
9428fe1a
TM
4844void
4845nfsd4_get_opendowngradestateid(struct nfsd4_compound_state *cstate, struct nfsd4_open_downgrade *odp)
4846{
4847 get_stateid(cstate, &odp->od_stateid);
4848}
4849
4850void
4851nfsd4_get_delegreturnstateid(struct nfsd4_compound_state *cstate, struct nfsd4_delegreturn *drp)
4852{
4853 get_stateid(cstate, &drp->dr_stateid);
4854}
4855
1e97b519
TM
4856void
4857nfsd4_get_freestateid(struct nfsd4_compound_state *cstate, struct nfsd4_free_stateid *fsp)
4858{
4859 get_stateid(cstate, &fsp->fr_stateid);
4860}
4861
4862void
4863nfsd4_get_setattrstateid(struct nfsd4_compound_state *cstate, struct nfsd4_setattr *setattr)
4864{
4865 get_stateid(cstate, &setattr->sa_stateid);
4866}
4867
8b70484c
TM
4868void
4869nfsd4_get_closestateid(struct nfsd4_compound_state *cstate, struct nfsd4_close *close)
4870{
4871 get_stateid(cstate, &close->cl_stateid);
4872}
4873
4874void
62cd4a59 4875nfsd4_get_lockustateid(struct nfsd4_compound_state *cstate, struct nfsd4_locku *locku)
8b70484c 4876{
62cd4a59 4877 get_stateid(cstate, &locku->lu_stateid);
8b70484c 4878}
30813e27
TM
4879
4880void
4881nfsd4_get_readstateid(struct nfsd4_compound_state *cstate, struct nfsd4_read *read)
4882{
4883 get_stateid(cstate, &read->rd_stateid);
4884}
4885
4886void
4887nfsd4_get_writestateid(struct nfsd4_compound_state *cstate, struct nfsd4_write *write)
4888{
4889 get_stateid(cstate, &write->wr_stateid);
4890}
This page took 1.059731 seconds and 5 git commands to generate.