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