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