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