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