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