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