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