]> git.proxmox.com Git - mirror_ubuntu-bionic-kernel.git/blame - fs/nfsd/nfs4state.c
nfsd41: xdr infrastructure
[mirror_ubuntu-bionic-kernel.git] / fs / nfsd / nfs4state.c
CommitLineData
1da177e4
LT
1/*
2* linux/fs/nfsd/nfs4state.c
3*
4* Copyright (c) 2001 The Regents of the University of Michigan.
5* All rights reserved.
6*
7* Kendrick Smith <kmsmith@umich.edu>
8* Andy Adamson <kandros@umich.edu>
9*
10* Redistribution and use in source and binary forms, with or without
11* modification, are permitted provided that the following conditions
12* are met:
13*
14* 1. Redistributions of source code must retain the above copyright
15* notice, this list of conditions and the following disclaimer.
16* 2. Redistributions in binary form must reproduce the above copyright
17* notice, this list of conditions and the following disclaimer in the
18* documentation and/or other materials provided with the distribution.
19* 3. Neither the name of the University nor the names of its
20* contributors may be used to endorse or promote products derived
21* from this software without specific prior written permission.
22*
23* THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED
24* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
25* MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
26* DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
27* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
28* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
29* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
30* BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
31* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
32* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
33* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
34*
35*/
36
37#include <linux/param.h>
38#include <linux/major.h>
39#include <linux/slab.h>
40
41#include <linux/sunrpc/svc.h>
42#include <linux/nfsd/nfsd.h>
43#include <linux/nfsd/cache.h>
aceaf78d 44#include <linux/file.h>
1da177e4
LT
45#include <linux/mount.h>
46#include <linux/workqueue.h>
47#include <linux/smp_lock.h>
48#include <linux/kthread.h>
49#include <linux/nfs4.h>
50#include <linux/nfsd/state.h>
51#include <linux/nfsd/xdr4.h>
0964a3d3 52#include <linux/namei.h>
c2f1a551 53#include <linux/swap.h>
353ab6e9 54#include <linux/mutex.h>
fd85b817 55#include <linux/lockd/bind.h>
9a8db97e 56#include <linux/module.h>
68e76ad0 57#include <linux/sunrpc/svcauth_gss.h>
1da177e4
LT
58
59#define NFSDDBG_FACILITY NFSDDBG_PROC
60
61/* Globals */
62static time_t lease_time = 90; /* default lease time */
d99a05ad 63static time_t user_lease_time = 90;
fd39ca9a 64static time_t boot_time;
1da177e4
LT
65static u32 current_ownerid = 1;
66static u32 current_fileid = 1;
67static u32 current_delegid = 1;
68static u32 nfs4_init;
fd39ca9a
N
69static stateid_t zerostateid; /* bits all 0 */
70static stateid_t onestateid; /* bits all 1 */
71
72#define ZERO_STATEID(stateid) (!memcmp((stateid), &zerostateid, sizeof(stateid_t)))
73#define ONE_STATEID(stateid) (!memcmp((stateid), &onestateid, sizeof(stateid_t)))
1da177e4 74
1da177e4 75/* forward declarations */
fd39ca9a 76static struct nfs4_stateid * find_stateid(stateid_t *stid, int flags);
1da177e4 77static struct nfs4_delegation * find_delegation_stateid(struct inode *ino, stateid_t *stid);
0964a3d3
N
78static char user_recovery_dirname[PATH_MAX] = "/var/lib/nfs/v4recovery";
79static void nfs4_set_recdir(char *recdir);
1da177e4 80
8b671b80
BF
81/* Locking: */
82
83/* Currently used for almost all code touching nfsv4 state: */
353ab6e9 84static DEFINE_MUTEX(client_mutex);
1da177e4 85
8b671b80
BF
86/*
87 * Currently used for the del_recall_lru and file hash table. In an
88 * effort to decrease the scope of the client_mutex, this spinlock may
89 * eventually cover more:
90 */
91static DEFINE_SPINLOCK(recall_lock);
92
e18b890b
CL
93static struct kmem_cache *stateowner_slab = NULL;
94static struct kmem_cache *file_slab = NULL;
95static struct kmem_cache *stateid_slab = NULL;
96static struct kmem_cache *deleg_slab = NULL;
e60d4398 97
1da177e4
LT
98void
99nfs4_lock_state(void)
100{
353ab6e9 101 mutex_lock(&client_mutex);
1da177e4
LT
102}
103
104void
105nfs4_unlock_state(void)
106{
353ab6e9 107 mutex_unlock(&client_mutex);
1da177e4
LT
108}
109
110static inline u32
111opaque_hashval(const void *ptr, int nbytes)
112{
113 unsigned char *cptr = (unsigned char *) ptr;
114
115 u32 x = 0;
116 while (nbytes--) {
117 x *= 37;
118 x += *cptr++;
119 }
120 return x;
121}
122
1da177e4
LT
123static struct list_head del_recall_lru;
124
13cd2184
N
125static inline void
126put_nfs4_file(struct nfs4_file *fi)
127{
8b671b80
BF
128 if (atomic_dec_and_lock(&fi->fi_ref, &recall_lock)) {
129 list_del(&fi->fi_hash);
130 spin_unlock(&recall_lock);
131 iput(fi->fi_inode);
132 kmem_cache_free(file_slab, fi);
133 }
13cd2184
N
134}
135
136static inline void
137get_nfs4_file(struct nfs4_file *fi)
138{
8b671b80 139 atomic_inc(&fi->fi_ref);
13cd2184
N
140}
141
ef0f3390 142static int num_delegations;
c2f1a551 143unsigned int max_delegations;
ef0f3390
N
144
145/*
146 * Open owner state (share locks)
147 */
148
149/* hash tables for nfs4_stateowner */
150#define OWNER_HASH_BITS 8
151#define OWNER_HASH_SIZE (1 << OWNER_HASH_BITS)
152#define OWNER_HASH_MASK (OWNER_HASH_SIZE - 1)
153
154#define ownerid_hashval(id) \
155 ((id) & OWNER_HASH_MASK)
156#define ownerstr_hashval(clientid, ownername) \
157 (((clientid) + opaque_hashval((ownername.data), (ownername.len))) & OWNER_HASH_MASK)
158
159static struct list_head ownerid_hashtbl[OWNER_HASH_SIZE];
160static struct list_head ownerstr_hashtbl[OWNER_HASH_SIZE];
161
162/* hash table for nfs4_file */
163#define FILE_HASH_BITS 8
164#define FILE_HASH_SIZE (1 << FILE_HASH_BITS)
165#define FILE_HASH_MASK (FILE_HASH_SIZE - 1)
166/* hash table for (open)nfs4_stateid */
167#define STATEID_HASH_BITS 10
168#define STATEID_HASH_SIZE (1 << STATEID_HASH_BITS)
169#define STATEID_HASH_MASK (STATEID_HASH_SIZE - 1)
170
171#define file_hashval(x) \
172 hash_ptr(x, FILE_HASH_BITS)
173#define stateid_hashval(owner_id, file_id) \
174 (((owner_id) + (file_id)) & STATEID_HASH_MASK)
175
176static struct list_head file_hashtbl[FILE_HASH_SIZE];
177static struct list_head stateid_hashtbl[STATEID_HASH_SIZE];
178
1da177e4
LT
179static struct nfs4_delegation *
180alloc_init_deleg(struct nfs4_client *clp, struct nfs4_stateid *stp, struct svc_fh *current_fh, u32 type)
181{
182 struct nfs4_delegation *dp;
183 struct nfs4_file *fp = stp->st_file;
184 struct nfs4_callback *cb = &stp->st_stateowner->so_client->cl_callback;
185
186 dprintk("NFSD alloc_init_deleg\n");
47f9940c
MS
187 if (fp->fi_had_conflict)
188 return NULL;
c2f1a551 189 if (num_delegations > max_delegations)
ef0f3390 190 return NULL;
5b2d21c1
N
191 dp = kmem_cache_alloc(deleg_slab, GFP_KERNEL);
192 if (dp == NULL)
1da177e4 193 return dp;
ef0f3390 194 num_delegations++;
ea1da636
N
195 INIT_LIST_HEAD(&dp->dl_perfile);
196 INIT_LIST_HEAD(&dp->dl_perclnt);
1da177e4
LT
197 INIT_LIST_HEAD(&dp->dl_recall_lru);
198 dp->dl_client = clp;
13cd2184 199 get_nfs4_file(fp);
1da177e4
LT
200 dp->dl_file = fp;
201 dp->dl_flock = NULL;
202 get_file(stp->st_vfs_file);
203 dp->dl_vfs_file = stp->st_vfs_file;
204 dp->dl_type = type;
205 dp->dl_recall.cbr_dp = NULL;
206 dp->dl_recall.cbr_ident = cb->cb_ident;
207 dp->dl_recall.cbr_trunc = 0;
208 dp->dl_stateid.si_boot = boot_time;
209 dp->dl_stateid.si_stateownerid = current_delegid++;
210 dp->dl_stateid.si_fileid = 0;
211 dp->dl_stateid.si_generation = 0;
6c02eaa1 212 fh_copy_shallow(&dp->dl_fh, &current_fh->fh_handle);
1da177e4
LT
213 dp->dl_time = 0;
214 atomic_set(&dp->dl_count, 1);
ea1da636
N
215 list_add(&dp->dl_perfile, &fp->fi_delegations);
216 list_add(&dp->dl_perclnt, &clp->cl_delegations);
1da177e4
LT
217 return dp;
218}
219
220void
221nfs4_put_delegation(struct nfs4_delegation *dp)
222{
223 if (atomic_dec_and_test(&dp->dl_count)) {
224 dprintk("NFSD: freeing dp %p\n",dp);
13cd2184 225 put_nfs4_file(dp->dl_file);
5b2d21c1 226 kmem_cache_free(deleg_slab, dp);
ef0f3390 227 num_delegations--;
1da177e4
LT
228 }
229}
230
231/* Remove the associated file_lock first, then remove the delegation.
232 * lease_modify() is called to remove the FS_LEASE file_lock from
233 * the i_flock list, eventually calling nfsd's lock_manager
234 * fl_release_callback.
235 */
236static void
237nfs4_close_delegation(struct nfs4_delegation *dp)
238{
239 struct file *filp = dp->dl_vfs_file;
240
241 dprintk("NFSD: close_delegation dp %p\n",dp);
242 dp->dl_vfs_file = NULL;
243 /* The following nfsd_close may not actually close the file,
244 * but we want to remove the lease in any case. */
c907132d 245 if (dp->dl_flock)
a9933cea 246 vfs_setlease(filp, F_UNLCK, &dp->dl_flock);
1da177e4 247 nfsd_close(filp);
1da177e4
LT
248}
249
250/* Called under the state lock. */
251static void
252unhash_delegation(struct nfs4_delegation *dp)
253{
ea1da636
N
254 list_del_init(&dp->dl_perfile);
255 list_del_init(&dp->dl_perclnt);
1da177e4
LT
256 spin_lock(&recall_lock);
257 list_del_init(&dp->dl_recall_lru);
258 spin_unlock(&recall_lock);
259 nfs4_close_delegation(dp);
260 nfs4_put_delegation(dp);
261}
262
263/*
264 * SETCLIENTID state
265 */
266
267/* Hash tables for nfs4_clientid state */
268#define CLIENT_HASH_BITS 4
269#define CLIENT_HASH_SIZE (1 << CLIENT_HASH_BITS)
270#define CLIENT_HASH_MASK (CLIENT_HASH_SIZE - 1)
271
272#define clientid_hashval(id) \
273 ((id) & CLIENT_HASH_MASK)
a55370a3
N
274#define clientstr_hashval(name) \
275 (opaque_hashval((name), 8) & CLIENT_HASH_MASK)
1da177e4
LT
276/*
277 * reclaim_str_hashtbl[] holds known client info from previous reset/reboot
278 * used in reboot/reset lease grace period processing
279 *
280 * conf_id_hashtbl[], and conf_str_hashtbl[] hold confirmed
281 * setclientid_confirmed info.
282 *
283 * unconf_str_hastbl[] and unconf_id_hashtbl[] hold unconfirmed
284 * setclientid info.
285 *
286 * client_lru holds client queue ordered by nfs4_client.cl_time
287 * for lease renewal.
288 *
289 * close_lru holds (open) stateowner queue ordered by nfs4_stateowner.so_time
290 * for last close replay.
291 */
292static struct list_head reclaim_str_hashtbl[CLIENT_HASH_SIZE];
293static int reclaim_str_hashtbl_size = 0;
294static struct list_head conf_id_hashtbl[CLIENT_HASH_SIZE];
295static struct list_head conf_str_hashtbl[CLIENT_HASH_SIZE];
296static struct list_head unconf_str_hashtbl[CLIENT_HASH_SIZE];
297static struct list_head unconf_id_hashtbl[CLIENT_HASH_SIZE];
298static struct list_head client_lru;
299static struct list_head close_lru;
300
2283963f
BF
301static void unhash_generic_stateid(struct nfs4_stateid *stp)
302{
303 list_del(&stp->st_hash);
304 list_del(&stp->st_perfile);
305 list_del(&stp->st_perstateowner);
306}
307
308static void free_generic_stateid(struct nfs4_stateid *stp)
309{
310 put_nfs4_file(stp->st_file);
311 kmem_cache_free(stateid_slab, stp);
312}
313
314static void release_lock_stateid(struct nfs4_stateid *stp)
315{
316 unhash_generic_stateid(stp);
317 locks_remove_posix(stp->st_vfs_file, (fl_owner_t)stp->st_stateowner);
318 free_generic_stateid(stp);
319}
320
f044ff83
BF
321static void unhash_lockowner(struct nfs4_stateowner *sop)
322{
323 struct nfs4_stateid *stp;
324
325 list_del(&sop->so_idhash);
326 list_del(&sop->so_strhash);
327 list_del(&sop->so_perstateid);
328 while (!list_empty(&sop->so_stateids)) {
329 stp = list_first_entry(&sop->so_stateids,
330 struct nfs4_stateid, st_perstateowner);
331 release_lock_stateid(stp);
332 }
333}
334
335static void release_lockowner(struct nfs4_stateowner *sop)
336{
337 unhash_lockowner(sop);
338 nfs4_put_stateowner(sop);
339}
340
f1d110ca
BF
341static void
342release_stateid_lockowners(struct nfs4_stateid *open_stp)
343{
344 struct nfs4_stateowner *lock_sop;
345
346 while (!list_empty(&open_stp->st_lockowners)) {
347 lock_sop = list_entry(open_stp->st_lockowners.next,
348 struct nfs4_stateowner, so_perstateid);
349 /* list_del(&open_stp->st_lockowners); */
350 BUG_ON(lock_sop->so_is_open_owner);
f044ff83 351 release_lockowner(lock_sop);
f1d110ca
BF
352 }
353}
354
2283963f
BF
355static void release_open_stateid(struct nfs4_stateid *stp)
356{
357 unhash_generic_stateid(stp);
358 release_stateid_lockowners(stp);
359 nfsd_close(stp->st_vfs_file);
360 free_generic_stateid(stp);
361}
362
f044ff83 363static void unhash_openowner(struct nfs4_stateowner *sop)
f1d110ca
BF
364{
365 struct nfs4_stateid *stp;
366
367 list_del(&sop->so_idhash);
368 list_del(&sop->so_strhash);
f044ff83
BF
369 list_del(&sop->so_perclient);
370 list_del(&sop->so_perstateid); /* XXX: necessary? */
f1d110ca 371 while (!list_empty(&sop->so_stateids)) {
f044ff83
BF
372 stp = list_first_entry(&sop->so_stateids,
373 struct nfs4_stateid, st_perstateowner);
374 release_open_stateid(stp);
f1d110ca
BF
375 }
376}
377
f044ff83 378static void release_openowner(struct nfs4_stateowner *sop)
f1d110ca 379{
f044ff83 380 unhash_openowner(sop);
f1d110ca
BF
381 list_del(&sop->so_close_lru);
382 nfs4_put_stateowner(sop);
383}
384
5282fd72
ME
385static DEFINE_SPINLOCK(sessionid_lock);
386#define SESSION_HASH_SIZE 512
387static struct list_head sessionid_hashtbl[SESSION_HASH_SIZE];
388
389static inline int
390hash_sessionid(struct nfs4_sessionid *sessionid)
391{
392 struct nfsd4_sessionid *sid = (struct nfsd4_sessionid *)sessionid;
393
394 return sid->sequence % SESSION_HASH_SIZE;
395}
396
397static inline void
398dump_sessionid(const char *fn, struct nfs4_sessionid *sessionid)
399{
400 u32 *ptr = (u32 *)(&sessionid->data[0]);
401 dprintk("%s: %u:%u:%u:%u\n", fn, ptr[0], ptr[1], ptr[2], ptr[3]);
402}
403
404/* caller must hold sessionid_lock */
405static struct nfsd4_session *
406find_in_sessionid_hashtbl(struct nfs4_sessionid *sessionid)
407{
408 struct nfsd4_session *elem;
409 int idx;
410
411 dump_sessionid(__func__, sessionid);
412 idx = hash_sessionid(sessionid);
413 dprintk("%s: idx is %d\n", __func__, idx);
414 /* Search in the appropriate list */
415 list_for_each_entry(elem, &sessionid_hashtbl[idx], se_hash) {
416 dump_sessionid("list traversal", &elem->se_sessionid);
417 if (!memcmp(elem->se_sessionid.data, sessionid->data,
418 NFS4_MAX_SESSIONID_LEN)) {
419 return elem;
420 }
421 }
422
423 dprintk("%s: session not found\n", __func__);
424 return NULL;
425}
426
427/* caller must hold sessionid_lock */
7116ed6b 428static void
5282fd72 429unhash_session(struct nfsd4_session *ses)
7116ed6b
AA
430{
431 list_del(&ses->se_hash);
432 list_del(&ses->se_perclnt);
5282fd72
ME
433}
434
435static void
436release_session(struct nfsd4_session *ses)
437{
438 spin_lock(&sessionid_lock);
439 unhash_session(ses);
440 spin_unlock(&sessionid_lock);
7116ed6b
AA
441 nfsd4_put_session(ses);
442}
443
444void
445free_session(struct kref *kref)
446{
447 struct nfsd4_session *ses;
448
449 ses = container_of(kref, struct nfsd4_session, se_ref);
450 kfree(ses->se_slots);
451 kfree(ses);
452}
453
1da177e4
LT
454static inline void
455renew_client(struct nfs4_client *clp)
456{
457 /*
458 * Move client to the end to the LRU list.
459 */
460 dprintk("renewing client (clientid %08x/%08x)\n",
461 clp->cl_clientid.cl_boot,
462 clp->cl_clientid.cl_id);
463 list_move_tail(&clp->cl_lru, &client_lru);
464 clp->cl_time = get_seconds();
465}
466
467/* SETCLIENTID and SETCLIENTID_CONFIRM Helper functions */
468static int
469STALE_CLIENTID(clientid_t *clid)
470{
471 if (clid->cl_boot == boot_time)
472 return 0;
473 dprintk("NFSD stale clientid (%08x/%08x)\n",
474 clid->cl_boot, clid->cl_id);
475 return 1;
476}
477
478/*
479 * XXX Should we use a slab cache ?
480 * This type of memory management is somewhat inefficient, but we use it
481 * anyway since SETCLIENTID is not a common operation.
482 */
35bba9a3 483static struct nfs4_client *alloc_client(struct xdr_netobj name)
1da177e4
LT
484{
485 struct nfs4_client *clp;
486
35bba9a3
BF
487 clp = kzalloc(sizeof(struct nfs4_client), GFP_KERNEL);
488 if (clp == NULL)
489 return NULL;
490 clp->cl_name.data = kmalloc(name.len, GFP_KERNEL);
491 if (clp->cl_name.data == NULL) {
492 kfree(clp);
493 return NULL;
1da177e4 494 }
35bba9a3
BF
495 memcpy(clp->cl_name.data, name.data, name.len);
496 clp->cl_name.len = name.len;
1da177e4
LT
497 return clp;
498}
499
1b1a9b31
BF
500static void
501shutdown_callback_client(struct nfs4_client *clp)
502{
503 struct rpc_clnt *clnt = clp->cl_callback.cb_client;
504
1b1a9b31 505 if (clnt) {
404ec117
BF
506 /*
507 * Callback threads take a reference on the client, so there
508 * should be no outstanding callbacks at this point.
509 */
1b1a9b31
BF
510 clp->cl_callback.cb_client = NULL;
511 rpc_shutdown_client(clnt);
512 }
513}
514
1da177e4
LT
515static inline void
516free_client(struct nfs4_client *clp)
517{
1b1a9b31 518 shutdown_callback_client(clp);
1da177e4
LT
519 if (clp->cl_cred.cr_group_info)
520 put_group_info(clp->cl_cred.cr_group_info);
68e76ad0 521 kfree(clp->cl_principal);
1da177e4
LT
522 kfree(clp->cl_name.data);
523 kfree(clp);
524}
525
526void
527put_nfs4_client(struct nfs4_client *clp)
528{
529 if (atomic_dec_and_test(&clp->cl_count))
530 free_client(clp);
531}
532
533static void
534expire_client(struct nfs4_client *clp)
535{
536 struct nfs4_stateowner *sop;
537 struct nfs4_delegation *dp;
1da177e4
LT
538 struct list_head reaplist;
539
540 dprintk("NFSD: expire_client cl_count %d\n",
541 atomic_read(&clp->cl_count));
542
1da177e4
LT
543 INIT_LIST_HEAD(&reaplist);
544 spin_lock(&recall_lock);
ea1da636
N
545 while (!list_empty(&clp->cl_delegations)) {
546 dp = list_entry(clp->cl_delegations.next, struct nfs4_delegation, dl_perclnt);
1da177e4
LT
547 dprintk("NFSD: expire client. dp %p, fp %p\n", dp,
548 dp->dl_flock);
ea1da636 549 list_del_init(&dp->dl_perclnt);
1da177e4
LT
550 list_move(&dp->dl_recall_lru, &reaplist);
551 }
552 spin_unlock(&recall_lock);
553 while (!list_empty(&reaplist)) {
554 dp = list_entry(reaplist.next, struct nfs4_delegation, dl_recall_lru);
555 list_del_init(&dp->dl_recall_lru);
556 unhash_delegation(dp);
557 }
558 list_del(&clp->cl_idhash);
559 list_del(&clp->cl_strhash);
560 list_del(&clp->cl_lru);
ea1da636
N
561 while (!list_empty(&clp->cl_openowners)) {
562 sop = list_entry(clp->cl_openowners.next, struct nfs4_stateowner, so_perclient);
f044ff83 563 release_openowner(sop);
1da177e4 564 }
c4bf7868
ME
565 while (!list_empty(&clp->cl_sessions)) {
566 struct nfsd4_session *ses;
567 ses = list_entry(clp->cl_sessions.next, struct nfsd4_session,
568 se_perclnt);
569 release_session(ses);
570 }
1da177e4
LT
571 put_nfs4_client(clp);
572}
573
35bba9a3
BF
574static struct nfs4_client *create_client(struct xdr_netobj name, char *recdir)
575{
1da177e4
LT
576 struct nfs4_client *clp;
577
35bba9a3
BF
578 clp = alloc_client(name);
579 if (clp == NULL)
580 return NULL;
a55370a3 581 memcpy(clp->cl_recdir, recdir, HEXDIR_LEN);
1da177e4
LT
582 atomic_set(&clp->cl_count, 1);
583 atomic_set(&clp->cl_callback.cb_set, 0);
1da177e4
LT
584 INIT_LIST_HEAD(&clp->cl_idhash);
585 INIT_LIST_HEAD(&clp->cl_strhash);
ea1da636
N
586 INIT_LIST_HEAD(&clp->cl_openowners);
587 INIT_LIST_HEAD(&clp->cl_delegations);
9fb87070 588 INIT_LIST_HEAD(&clp->cl_sessions);
1da177e4 589 INIT_LIST_HEAD(&clp->cl_lru);
1da177e4
LT
590 return clp;
591}
592
35bba9a3
BF
593static void copy_verf(struct nfs4_client *target, nfs4_verifier *source)
594{
595 memcpy(target->cl_verifier.data, source->data,
596 sizeof(target->cl_verifier.data));
1da177e4
LT
597}
598
35bba9a3
BF
599static void copy_clid(struct nfs4_client *target, struct nfs4_client *source)
600{
1da177e4
LT
601 target->cl_clientid.cl_boot = source->cl_clientid.cl_boot;
602 target->cl_clientid.cl_id = source->cl_clientid.cl_id;
603}
604
35bba9a3
BF
605static void copy_cred(struct svc_cred *target, struct svc_cred *source)
606{
1da177e4
LT
607 target->cr_uid = source->cr_uid;
608 target->cr_gid = source->cr_gid;
609 target->cr_group_info = source->cr_group_info;
610 get_group_info(target->cr_group_info);
611}
612
35bba9a3 613static int same_name(const char *n1, const char *n2)
599e0a22 614{
a55370a3 615 return 0 == memcmp(n1, n2, HEXDIR_LEN);
1da177e4
LT
616}
617
618static int
599e0a22
BF
619same_verf(nfs4_verifier *v1, nfs4_verifier *v2)
620{
621 return 0 == memcmp(v1->data, v2->data, sizeof(v1->data));
1da177e4
LT
622}
623
624static int
599e0a22
BF
625same_clid(clientid_t *cl1, clientid_t *cl2)
626{
627 return (cl1->cl_boot == cl2->cl_boot) && (cl1->cl_id == cl2->cl_id);
1da177e4
LT
628}
629
630/* XXX what about NGROUP */
631static int
599e0a22
BF
632same_creds(struct svc_cred *cr1, struct svc_cred *cr2)
633{
634 return cr1->cr_uid == cr2->cr_uid;
1da177e4
LT
635}
636
5ec7b46c
BF
637static void gen_clid(struct nfs4_client *clp)
638{
639 static u32 current_clientid = 1;
640
1da177e4
LT
641 clp->cl_clientid.cl_boot = boot_time;
642 clp->cl_clientid.cl_id = current_clientid++;
643}
644
deda2faa
BF
645static void gen_confirm(struct nfs4_client *clp)
646{
647 static u32 i;
648 u32 *p;
1da177e4 649
1da177e4 650 p = (u32 *)clp->cl_confirm.data;
deda2faa
BF
651 *p++ = get_seconds();
652 *p++ = i++;
1da177e4
LT
653}
654
35bba9a3
BF
655static int check_name(struct xdr_netobj name)
656{
1da177e4
LT
657 if (name.len == 0)
658 return 0;
659 if (name.len > NFS4_OPAQUE_LIMIT) {
2fdada03 660 dprintk("NFSD: check_name: name too long(%d)!\n", name.len);
1da177e4
LT
661 return 0;
662 }
663 return 1;
664}
665
fd39ca9a 666static void
1da177e4
LT
667add_to_unconfirmed(struct nfs4_client *clp, unsigned int strhashval)
668{
669 unsigned int idhashval;
670
671 list_add(&clp->cl_strhash, &unconf_str_hashtbl[strhashval]);
672 idhashval = clientid_hashval(clp->cl_clientid.cl_id);
673 list_add(&clp->cl_idhash, &unconf_id_hashtbl[idhashval]);
674 list_add_tail(&clp->cl_lru, &client_lru);
675 clp->cl_time = get_seconds();
676}
677
fd39ca9a 678static void
1da177e4
LT
679move_to_confirmed(struct nfs4_client *clp)
680{
681 unsigned int idhashval = clientid_hashval(clp->cl_clientid.cl_id);
682 unsigned int strhashval;
683
684 dprintk("NFSD: move_to_confirm nfs4_client %p\n", clp);
685 list_del_init(&clp->cl_strhash);
f116629d 686 list_move(&clp->cl_idhash, &conf_id_hashtbl[idhashval]);
a55370a3 687 strhashval = clientstr_hashval(clp->cl_recdir);
1da177e4
LT
688 list_add(&clp->cl_strhash, &conf_str_hashtbl[strhashval]);
689 renew_client(clp);
690}
691
692static struct nfs4_client *
693find_confirmed_client(clientid_t *clid)
694{
695 struct nfs4_client *clp;
696 unsigned int idhashval = clientid_hashval(clid->cl_id);
697
698 list_for_each_entry(clp, &conf_id_hashtbl[idhashval], cl_idhash) {
599e0a22 699 if (same_clid(&clp->cl_clientid, clid))
1da177e4
LT
700 return clp;
701 }
702 return NULL;
703}
704
705static struct nfs4_client *
706find_unconfirmed_client(clientid_t *clid)
707{
708 struct nfs4_client *clp;
709 unsigned int idhashval = clientid_hashval(clid->cl_id);
710
711 list_for_each_entry(clp, &unconf_id_hashtbl[idhashval], cl_idhash) {
599e0a22 712 if (same_clid(&clp->cl_clientid, clid))
1da177e4
LT
713 return clp;
714 }
715 return NULL;
716}
717
28ce6054
N
718static struct nfs4_client *
719find_confirmed_client_by_str(const char *dname, unsigned int hashval)
720{
721 struct nfs4_client *clp;
722
723 list_for_each_entry(clp, &conf_str_hashtbl[hashval], cl_strhash) {
724 if (same_name(clp->cl_recdir, dname))
725 return clp;
726 }
727 return NULL;
728}
729
730static struct nfs4_client *
731find_unconfirmed_client_by_str(const char *dname, unsigned int hashval)
732{
733 struct nfs4_client *clp;
734
735 list_for_each_entry(clp, &unconf_str_hashtbl[hashval], cl_strhash) {
736 if (same_name(clp->cl_recdir, dname))
737 return clp;
738 }
739 return NULL;
740}
741
1da177e4
LT
742/* a helper function for parse_callback */
743static int
744parse_octet(unsigned int *lenp, char **addrp)
745{
746 unsigned int len = *lenp;
747 char *p = *addrp;
748 int n = -1;
749 char c;
750
751 for (;;) {
752 if (!len)
753 break;
754 len--;
755 c = *p++;
756 if (c == '.')
757 break;
758 if ((c < '0') || (c > '9')) {
759 n = -1;
760 break;
761 }
762 if (n < 0)
763 n = 0;
764 n = (n * 10) + (c - '0');
765 if (n > 255) {
766 n = -1;
767 break;
768 }
769 }
770 *lenp = len;
771 *addrp = p;
772 return n;
773}
774
775/* parse and set the setclientid ipv4 callback address */
fd39ca9a 776static int
1da177e4
LT
777parse_ipv4(unsigned int addr_len, char *addr_val, unsigned int *cbaddrp, unsigned short *cbportp)
778{
779 int temp = 0;
780 u32 cbaddr = 0;
781 u16 cbport = 0;
782 u32 addrlen = addr_len;
783 char *addr = addr_val;
784 int i, shift;
785
786 /* ipaddress */
787 shift = 24;
788 for(i = 4; i > 0 ; i--) {
789 if ((temp = parse_octet(&addrlen, &addr)) < 0) {
790 return 0;
791 }
792 cbaddr |= (temp << shift);
793 if (shift > 0)
794 shift -= 8;
795 }
796 *cbaddrp = cbaddr;
797
798 /* port */
799 shift = 8;
800 for(i = 2; i > 0 ; i--) {
801 if ((temp = parse_octet(&addrlen, &addr)) < 0) {
802 return 0;
803 }
804 cbport |= (temp << shift);
805 if (shift > 0)
806 shift -= 8;
807 }
808 *cbportp = cbport;
809 return 1;
810}
811
fd39ca9a 812static void
1da177e4
LT
813gen_callback(struct nfs4_client *clp, struct nfsd4_setclientid *se)
814{
815 struct nfs4_callback *cb = &clp->cl_callback;
816
817 /* Currently, we only support tcp for the callback channel */
818 if ((se->se_callback_netid_len != 3) || memcmp((char *)se->se_callback_netid_val, "tcp", 3))
819 goto out_err;
820
821 if ( !(parse_ipv4(se->se_callback_addr_len, se->se_callback_addr_val,
822 &cb->cb_addr, &cb->cb_port)))
823 goto out_err;
824 cb->cb_prog = se->se_callback_prog;
825 cb->cb_ident = se->se_callback_ident;
1da177e4
LT
826 return;
827out_err:
849823c5 828 dprintk(KERN_INFO "NFSD: this client (clientid %08x/%08x) "
1da177e4
LT
829 "will not receive delegations\n",
830 clp->cl_clientid.cl_boot, clp->cl_clientid.cl_id);
831
1da177e4
LT
832 return;
833}
834
b37ad28b 835__be32
b591480b
BF
836nfsd4_setclientid(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
837 struct nfsd4_setclientid *setclid)
1da177e4 838{
27459f09 839 struct sockaddr_in *sin = svc_addr_in(rqstp);
1da177e4
LT
840 struct xdr_netobj clname = {
841 .len = setclid->se_namelen,
842 .data = setclid->se_name,
843 };
844 nfs4_verifier clverifier = setclid->se_verf;
845 unsigned int strhashval;
28ce6054 846 struct nfs4_client *conf, *unconf, *new;
b37ad28b 847 __be32 status;
68e76ad0 848 char *princ;
a55370a3 849 char dname[HEXDIR_LEN];
1da177e4 850
1da177e4 851 if (!check_name(clname))
73aea4ec 852 return nfserr_inval;
1da177e4 853
a55370a3
N
854 status = nfs4_make_rec_clidname(dname, &clname);
855 if (status)
73aea4ec 856 return status;
a55370a3 857
1da177e4
LT
858 /*
859 * XXX The Duplicate Request Cache (DRC) has been checked (??)
860 * We get here on a DRC miss.
861 */
862
a55370a3 863 strhashval = clientstr_hashval(dname);
1da177e4 864
1da177e4 865 nfs4_lock_state();
28ce6054
N
866 conf = find_confirmed_client_by_str(dname, strhashval);
867 if (conf) {
a186e767 868 /* RFC 3530 14.2.33 CASE 0: */
1da177e4 869 status = nfserr_clid_inuse;
026722c2
BF
870 if (!same_creds(&conf->cl_cred, &rqstp->rq_cred)) {
871 dprintk("NFSD: setclientid: string in use by client"
872 " at %pI4\n", &conf->cl_addr);
1da177e4
LT
873 goto out;
874 }
1da177e4 875 }
a186e767
BF
876 /*
877 * section 14.2.33 of RFC 3530 (under the heading "IMPLEMENTATION")
878 * has a description of SETCLIENTID request processing consisting
879 * of 5 bullet points, labeled as CASE0 - CASE4 below.
880 */
28ce6054 881 unconf = find_unconfirmed_client_by_str(dname, strhashval);
1da177e4
LT
882 status = nfserr_resource;
883 if (!conf) {
a186e767
BF
884 /*
885 * RFC 3530 14.2.33 CASE 4:
886 * placed first, because it is the normal case
1da177e4
LT
887 */
888 if (unconf)
889 expire_client(unconf);
a55370a3
N
890 new = create_client(clname, dname);
891 if (new == NULL)
1da177e4 892 goto out;
1da177e4 893 gen_clid(new);
599e0a22 894 } else if (same_verf(&conf->cl_verifier, &clverifier)) {
1da177e4 895 /*
a186e767
BF
896 * RFC 3530 14.2.33 CASE 1:
897 * probable callback update
1da177e4 898 */
31f4a6c1
N
899 if (unconf) {
900 /* Note this is removing unconfirmed {*x***},
901 * which is stronger than RFC recommended {vxc**}.
902 * This has the advantage that there is at most
903 * one {*x***} in either list at any time.
904 */
905 expire_client(unconf);
1da177e4 906 }
a55370a3
N
907 new = create_client(clname, dname);
908 if (new == NULL)
1da177e4 909 goto out;
1da177e4 910 copy_clid(new, conf);
1da177e4
LT
911 } else if (!unconf) {
912 /*
a186e767
BF
913 * RFC 3530 14.2.33 CASE 2:
914 * probable client reboot; state will be removed if
915 * confirmed.
1da177e4 916 */
a55370a3
N
917 new = create_client(clname, dname);
918 if (new == NULL)
1da177e4 919 goto out;
1da177e4 920 gen_clid(new);
49ba8781 921 } else {
a186e767
BF
922 /*
923 * RFC 3530 14.2.33 CASE 3:
924 * probable client reboot; state will be removed if
925 * confirmed.
1da177e4
LT
926 */
927 expire_client(unconf);
a55370a3
N
928 new = create_client(clname, dname);
929 if (new == NULL)
1da177e4 930 goto out;
1da177e4 931 gen_clid(new);
1da177e4 932 }
c175b83c
BF
933 copy_verf(new, &clverifier);
934 new->cl_addr = sin->sin_addr.s_addr;
61054b14 935 new->cl_flavor = rqstp->rq_flavor;
68e76ad0
OK
936 princ = svc_gss_principal(rqstp);
937 if (princ) {
938 new->cl_principal = kstrdup(princ, GFP_KERNEL);
939 if (new->cl_principal == NULL) {
940 free_client(new);
941 goto out;
942 }
943 }
c175b83c
BF
944 copy_cred(&new->cl_cred, &rqstp->rq_cred);
945 gen_confirm(new);
946 gen_callback(new, setclid);
947 add_to_unconfirmed(new, strhashval);
1da177e4
LT
948 setclid->se_clientid.cl_boot = new->cl_clientid.cl_boot;
949 setclid->se_clientid.cl_id = new->cl_clientid.cl_id;
950 memcpy(setclid->se_confirm.data, new->cl_confirm.data, sizeof(setclid->se_confirm.data));
951 status = nfs_ok;
952out:
953 nfs4_unlock_state();
954 return status;
955}
956
957
958/*
a186e767
BF
959 * Section 14.2.34 of RFC 3530 (under the heading "IMPLEMENTATION") has
960 * a description of SETCLIENTID_CONFIRM request processing consisting of 4
961 * bullets, labeled as CASE1 - CASE4 below.
1da177e4 962 */
b37ad28b 963__be32
b591480b
BF
964nfsd4_setclientid_confirm(struct svc_rqst *rqstp,
965 struct nfsd4_compound_state *cstate,
966 struct nfsd4_setclientid_confirm *setclientid_confirm)
1da177e4 967{
27459f09 968 struct sockaddr_in *sin = svc_addr_in(rqstp);
21ab45a4 969 struct nfs4_client *conf, *unconf;
1da177e4
LT
970 nfs4_verifier confirm = setclientid_confirm->sc_confirm;
971 clientid_t * clid = &setclientid_confirm->sc_clientid;
b37ad28b 972 __be32 status;
1da177e4
LT
973
974 if (STALE_CLIENTID(clid))
975 return nfserr_stale_clientid;
976 /*
977 * XXX The Duplicate Request Cache (DRC) has been checked (??)
978 * We get here on a DRC miss.
979 */
980
981 nfs4_lock_state();
21ab45a4
N
982
983 conf = find_confirmed_client(clid);
984 unconf = find_unconfirmed_client(clid);
985
986 status = nfserr_clid_inuse;
27459f09 987 if (conf && conf->cl_addr != sin->sin_addr.s_addr)
21ab45a4 988 goto out;
27459f09 989 if (unconf && unconf->cl_addr != sin->sin_addr.s_addr)
21ab45a4
N
990 goto out;
991
a186e767
BF
992 /*
993 * section 14.2.34 of RFC 3530 has a description of
994 * SETCLIENTID_CONFIRM request processing consisting
995 * of 4 bullet points, labeled as CASE1 - CASE4 below.
996 */
366e0c1d 997 if (conf && unconf && same_verf(&confirm, &unconf->cl_confirm)) {
a186e767
BF
998 /*
999 * RFC 3530 14.2.34 CASE 1:
1000 * callback update
1001 */
599e0a22 1002 if (!same_creds(&conf->cl_cred, &unconf->cl_cred))
1da177e4
LT
1003 status = nfserr_clid_inuse;
1004 else {
1a69c179
N
1005 /* XXX: We just turn off callbacks until we can handle
1006 * change request correctly. */
cb36d634 1007 atomic_set(&conf->cl_callback.cb_set, 0);
21ab45a4 1008 gen_confirm(conf);
46309029 1009 nfsd4_remove_clid_dir(unconf);
1a69c179 1010 expire_client(unconf);
1da177e4 1011 status = nfs_ok;
1a69c179 1012
1da177e4 1013 }
f3aba4e5 1014 } else if (conf && !unconf) {
a186e767
BF
1015 /*
1016 * RFC 3530 14.2.34 CASE 2:
1017 * probable retransmitted request; play it safe and
1018 * do nothing.
7c79f737 1019 */
599e0a22 1020 if (!same_creds(&conf->cl_cred, &rqstp->rq_cred))
1da177e4 1021 status = nfserr_clid_inuse;
21ab45a4 1022 else
1da177e4 1023 status = nfs_ok;
7c79f737 1024 } else if (!conf && unconf
599e0a22 1025 && same_verf(&unconf->cl_confirm, &confirm)) {
a186e767
BF
1026 /*
1027 * RFC 3530 14.2.34 CASE 3:
1028 * Normal case; new or rebooted client:
7c79f737 1029 */
599e0a22 1030 if (!same_creds(&unconf->cl_cred, &rqstp->rq_cred)) {
1da177e4
LT
1031 status = nfserr_clid_inuse;
1032 } else {
1a69c179
N
1033 unsigned int hash =
1034 clientstr_hashval(unconf->cl_recdir);
1035 conf = find_confirmed_client_by_str(unconf->cl_recdir,
1036 hash);
1037 if (conf) {
c7b9a459 1038 nfsd4_remove_clid_dir(conf);
1a69c179
N
1039 expire_client(conf);
1040 }
1da177e4 1041 move_to_confirmed(unconf);
21ab45a4 1042 conf = unconf;
46f8a64b 1043 nfsd4_probe_callback(conf);
1a69c179 1044 status = nfs_ok;
1da177e4 1045 }
599e0a22
BF
1046 } else if ((!conf || (conf && !same_verf(&conf->cl_confirm, &confirm)))
1047 && (!unconf || (unconf && !same_verf(&unconf->cl_confirm,
7c79f737 1048 &confirm)))) {
a186e767
BF
1049 /*
1050 * RFC 3530 14.2.34 CASE 4:
1051 * Client probably hasn't noticed that we rebooted yet.
7c79f737 1052 */
1da177e4 1053 status = nfserr_stale_clientid;
7c79f737 1054 } else {
08e8987c
N
1055 /* check that we have hit one of the cases...*/
1056 status = nfserr_clid_inuse;
1057 }
1da177e4 1058out:
1da177e4
LT
1059 nfs4_unlock_state();
1060 return status;
1061}
1062
1da177e4
LT
1063/* OPEN Share state helper functions */
1064static inline struct nfs4_file *
1065alloc_init_file(struct inode *ino)
1066{
1067 struct nfs4_file *fp;
1068 unsigned int hashval = file_hashval(ino);
1069
e60d4398
N
1070 fp = kmem_cache_alloc(file_slab, GFP_KERNEL);
1071 if (fp) {
8b671b80 1072 atomic_set(&fp->fi_ref, 1);
1da177e4 1073 INIT_LIST_HEAD(&fp->fi_hash);
8beefa24
N
1074 INIT_LIST_HEAD(&fp->fi_stateids);
1075 INIT_LIST_HEAD(&fp->fi_delegations);
8b671b80 1076 spin_lock(&recall_lock);
1da177e4 1077 list_add(&fp->fi_hash, &file_hashtbl[hashval]);
8b671b80 1078 spin_unlock(&recall_lock);
1da177e4
LT
1079 fp->fi_inode = igrab(ino);
1080 fp->fi_id = current_fileid++;
47f9940c 1081 fp->fi_had_conflict = false;
1da177e4
LT
1082 return fp;
1083 }
1084 return NULL;
1085}
1086
e60d4398 1087static void
e18b890b 1088nfsd4_free_slab(struct kmem_cache **slab)
1da177e4 1089{
e60d4398
N
1090 if (*slab == NULL)
1091 return;
1a1d92c1 1092 kmem_cache_destroy(*slab);
e60d4398 1093 *slab = NULL;
1da177e4
LT
1094}
1095
e8ff2a84 1096void
1da177e4
LT
1097nfsd4_free_slabs(void)
1098{
e60d4398
N
1099 nfsd4_free_slab(&stateowner_slab);
1100 nfsd4_free_slab(&file_slab);
5ac049ac 1101 nfsd4_free_slab(&stateid_slab);
5b2d21c1 1102 nfsd4_free_slab(&deleg_slab);
e60d4398 1103}
1da177e4 1104
e60d4398
N
1105static int
1106nfsd4_init_slabs(void)
1107{
1108 stateowner_slab = kmem_cache_create("nfsd4_stateowners",
20c2df83 1109 sizeof(struct nfs4_stateowner), 0, 0, NULL);
e60d4398
N
1110 if (stateowner_slab == NULL)
1111 goto out_nomem;
1112 file_slab = kmem_cache_create("nfsd4_files",
20c2df83 1113 sizeof(struct nfs4_file), 0, 0, NULL);
e60d4398
N
1114 if (file_slab == NULL)
1115 goto out_nomem;
5ac049ac 1116 stateid_slab = kmem_cache_create("nfsd4_stateids",
20c2df83 1117 sizeof(struct nfs4_stateid), 0, 0, NULL);
5ac049ac
N
1118 if (stateid_slab == NULL)
1119 goto out_nomem;
5b2d21c1 1120 deleg_slab = kmem_cache_create("nfsd4_delegations",
20c2df83 1121 sizeof(struct nfs4_delegation), 0, 0, NULL);
5b2d21c1
N
1122 if (deleg_slab == NULL)
1123 goto out_nomem;
e60d4398
N
1124 return 0;
1125out_nomem:
1126 nfsd4_free_slabs();
1127 dprintk("nfsd4: out of memory while initializing nfsv4\n");
1128 return -ENOMEM;
1da177e4
LT
1129}
1130
1131void
1132nfs4_free_stateowner(struct kref *kref)
1133{
1134 struct nfs4_stateowner *sop =
1135 container_of(kref, struct nfs4_stateowner, so_ref);
1136 kfree(sop->so_owner.data);
1137 kmem_cache_free(stateowner_slab, sop);
1138}
1139
1140static inline struct nfs4_stateowner *
1141alloc_stateowner(struct xdr_netobj *owner)
1142{
1143 struct nfs4_stateowner *sop;
1144
1145 if ((sop = kmem_cache_alloc(stateowner_slab, GFP_KERNEL))) {
1146 if ((sop->so_owner.data = kmalloc(owner->len, GFP_KERNEL))) {
1147 memcpy(sop->so_owner.data, owner->data, owner->len);
1148 sop->so_owner.len = owner->len;
1149 kref_init(&sop->so_ref);
1150 return sop;
1151 }
1152 kmem_cache_free(stateowner_slab, sop);
1153 }
1154 return NULL;
1155}
1156
1157static struct nfs4_stateowner *
1158alloc_init_open_stateowner(unsigned int strhashval, struct nfs4_client *clp, struct nfsd4_open *open) {
1159 struct nfs4_stateowner *sop;
1160 struct nfs4_replay *rp;
1161 unsigned int idhashval;
1162
1163 if (!(sop = alloc_stateowner(&open->op_owner)))
1164 return NULL;
1165 idhashval = ownerid_hashval(current_ownerid);
1166 INIT_LIST_HEAD(&sop->so_idhash);
1167 INIT_LIST_HEAD(&sop->so_strhash);
1168 INIT_LIST_HEAD(&sop->so_perclient);
ea1da636
N
1169 INIT_LIST_HEAD(&sop->so_stateids);
1170 INIT_LIST_HEAD(&sop->so_perstateid); /* not used */
1da177e4
LT
1171 INIT_LIST_HEAD(&sop->so_close_lru);
1172 sop->so_time = 0;
1173 list_add(&sop->so_idhash, &ownerid_hashtbl[idhashval]);
1174 list_add(&sop->so_strhash, &ownerstr_hashtbl[strhashval]);
ea1da636 1175 list_add(&sop->so_perclient, &clp->cl_openowners);
1da177e4
LT
1176 sop->so_is_open_owner = 1;
1177 sop->so_id = current_ownerid++;
1178 sop->so_client = clp;
1179 sop->so_seqid = open->op_seqid;
1180 sop->so_confirmed = 0;
1181 rp = &sop->so_replay;
de1ae286 1182 rp->rp_status = nfserr_serverfault;
1da177e4
LT
1183 rp->rp_buflen = 0;
1184 rp->rp_buf = rp->rp_ibuf;
1185 return sop;
1186}
1187
1da177e4
LT
1188static inline void
1189init_stateid(struct nfs4_stateid *stp, struct nfs4_file *fp, struct nfsd4_open *open) {
1190 struct nfs4_stateowner *sop = open->op_stateowner;
1191 unsigned int hashval = stateid_hashval(sop->so_id, fp->fi_id);
1192
1193 INIT_LIST_HEAD(&stp->st_hash);
ea1da636
N
1194 INIT_LIST_HEAD(&stp->st_perstateowner);
1195 INIT_LIST_HEAD(&stp->st_lockowners);
1da177e4
LT
1196 INIT_LIST_HEAD(&stp->st_perfile);
1197 list_add(&stp->st_hash, &stateid_hashtbl[hashval]);
ea1da636 1198 list_add(&stp->st_perstateowner, &sop->so_stateids);
8beefa24 1199 list_add(&stp->st_perfile, &fp->fi_stateids);
1da177e4 1200 stp->st_stateowner = sop;
13cd2184 1201 get_nfs4_file(fp);
1da177e4
LT
1202 stp->st_file = fp;
1203 stp->st_stateid.si_boot = boot_time;
1204 stp->st_stateid.si_stateownerid = sop->so_id;
1205 stp->st_stateid.si_fileid = fp->fi_id;
1206 stp->st_stateid.si_generation = 0;
1207 stp->st_access_bmap = 0;
1208 stp->st_deny_bmap = 0;
1209 __set_bit(open->op_share_access, &stp->st_access_bmap);
1210 __set_bit(open->op_share_deny, &stp->st_deny_bmap);
4c4cd222 1211 stp->st_openstp = NULL;
1da177e4
LT
1212}
1213
fd39ca9a 1214static void
1da177e4
LT
1215move_to_close_lru(struct nfs4_stateowner *sop)
1216{
1217 dprintk("NFSD: move_to_close_lru nfs4_stateowner %p\n", sop);
1218
358dd55a 1219 list_move_tail(&sop->so_close_lru, &close_lru);
1da177e4
LT
1220 sop->so_time = get_seconds();
1221}
1222
1da177e4 1223static int
599e0a22
BF
1224same_owner_str(struct nfs4_stateowner *sop, struct xdr_netobj *owner,
1225 clientid_t *clid)
1226{
1227 return (sop->so_owner.len == owner->len) &&
1228 0 == memcmp(sop->so_owner.data, owner->data, owner->len) &&
1229 (sop->so_client->cl_clientid.cl_id == clid->cl_id);
1da177e4
LT
1230}
1231
1232static struct nfs4_stateowner *
1233find_openstateowner_str(unsigned int hashval, struct nfsd4_open *open)
1234{
1235 struct nfs4_stateowner *so = NULL;
1236
1237 list_for_each_entry(so, &ownerstr_hashtbl[hashval], so_strhash) {
599e0a22 1238 if (same_owner_str(so, &open->op_owner, &open->op_clientid))
1da177e4
LT
1239 return so;
1240 }
1241 return NULL;
1242}
1243
1244/* search file_hashtbl[] for file */
1245static struct nfs4_file *
1246find_file(struct inode *ino)
1247{
1248 unsigned int hashval = file_hashval(ino);
1249 struct nfs4_file *fp;
1250
8b671b80 1251 spin_lock(&recall_lock);
1da177e4 1252 list_for_each_entry(fp, &file_hashtbl[hashval], fi_hash) {
13cd2184
N
1253 if (fp->fi_inode == ino) {
1254 get_nfs4_file(fp);
8b671b80 1255 spin_unlock(&recall_lock);
1da177e4 1256 return fp;
13cd2184 1257 }
1da177e4 1258 }
8b671b80 1259 spin_unlock(&recall_lock);
1da177e4
LT
1260 return NULL;
1261}
1262
8838dc43 1263static inline int access_valid(u32 x)
ba5a6a19 1264{
8838dc43
BF
1265 if (x < NFS4_SHARE_ACCESS_READ)
1266 return 0;
1267 if (x > NFS4_SHARE_ACCESS_BOTH)
1268 return 0;
1269 return 1;
ba5a6a19
BF
1270}
1271
8838dc43 1272static inline int deny_valid(u32 x)
ba5a6a19 1273{
8838dc43
BF
1274 /* Note: unlike access bits, deny bits may be zero. */
1275 return x <= NFS4_SHARE_DENY_BOTH;
ba5a6a19 1276}
1da177e4 1277
4f83aa30
BF
1278/*
1279 * We store the NONE, READ, WRITE, and BOTH bits separately in the
1280 * st_{access,deny}_bmap field of the stateid, in order to track not
1281 * only what share bits are currently in force, but also what
1282 * combinations of share bits previous opens have used. This allows us
1283 * to enforce the recommendation of rfc 3530 14.2.19 that the server
1284 * return an error if the client attempt to downgrade to a combination
1285 * of share bits not explicable by closing some of its previous opens.
1286 *
1287 * XXX: This enforcement is actually incomplete, since we don't keep
1288 * track of access/deny bit combinations; so, e.g., we allow:
1289 *
1290 * OPEN allow read, deny write
1291 * OPEN allow both, deny none
1292 * DOWNGRADE allow read, deny none
1293 *
1294 * which we should reject.
1295 */
fd39ca9a 1296static void
1da177e4
LT
1297set_access(unsigned int *access, unsigned long bmap) {
1298 int i;
1299
1300 *access = 0;
1301 for (i = 1; i < 4; i++) {
1302 if (test_bit(i, &bmap))
1303 *access |= i;
1304 }
1305}
1306
fd39ca9a 1307static void
1da177e4
LT
1308set_deny(unsigned int *deny, unsigned long bmap) {
1309 int i;
1310
1311 *deny = 0;
1312 for (i = 0; i < 4; i++) {
1313 if (test_bit(i, &bmap))
1314 *deny |= i ;
1315 }
1316}
1317
1318static int
1319test_share(struct nfs4_stateid *stp, struct nfsd4_open *open) {
1320 unsigned int access, deny;
1321
1322 set_access(&access, stp->st_access_bmap);
1323 set_deny(&deny, stp->st_deny_bmap);
1324 if ((access & open->op_share_deny) || (deny & open->op_share_access))
1325 return 0;
1326 return 1;
1327}
1328
1329/*
1330 * Called to check deny when READ with all zero stateid or
1331 * WRITE with all zero or all one stateid
1332 */
b37ad28b 1333static __be32
1da177e4
LT
1334nfs4_share_conflict(struct svc_fh *current_fh, unsigned int deny_type)
1335{
1336 struct inode *ino = current_fh->fh_dentry->d_inode;
1337 struct nfs4_file *fp;
1338 struct nfs4_stateid *stp;
b37ad28b 1339 __be32 ret;
1da177e4
LT
1340
1341 dprintk("NFSD: nfs4_share_conflict\n");
1342
1343 fp = find_file(ino);
13cd2184
N
1344 if (!fp)
1345 return nfs_ok;
b700949b 1346 ret = nfserr_locked;
1da177e4 1347 /* Search for conflicting share reservations */
13cd2184
N
1348 list_for_each_entry(stp, &fp->fi_stateids, st_perfile) {
1349 if (test_bit(deny_type, &stp->st_deny_bmap) ||
1350 test_bit(NFS4_SHARE_DENY_BOTH, &stp->st_deny_bmap))
1351 goto out;
1da177e4 1352 }
13cd2184
N
1353 ret = nfs_ok;
1354out:
1355 put_nfs4_file(fp);
1356 return ret;
1da177e4
LT
1357}
1358
1359static inline void
1360nfs4_file_downgrade(struct file *filp, unsigned int share_access)
1361{
1362 if (share_access & NFS4_SHARE_ACCESS_WRITE) {
aceaf78d 1363 drop_file_write_access(filp);
1da177e4
LT
1364 filp->f_mode = (filp->f_mode | FMODE_READ) & ~FMODE_WRITE;
1365 }
1366}
1367
1368/*
1369 * Recall a delegation
1370 */
1371static int
1372do_recall(void *__dp)
1373{
1374 struct nfs4_delegation *dp = __dp;
1375
47f9940c 1376 dp->dl_file->fi_had_conflict = true;
1da177e4
LT
1377 nfsd4_cb_recall(dp);
1378 return 0;
1379}
1380
1381/*
1382 * Spawn a thread to perform a recall on the delegation represented
1383 * by the lease (file_lock)
1384 *
1385 * Called from break_lease() with lock_kernel() held.
1386 * Note: we assume break_lease will only call this *once* for any given
1387 * lease.
1388 */
1389static
1390void nfsd_break_deleg_cb(struct file_lock *fl)
1391{
1392 struct nfs4_delegation *dp= (struct nfs4_delegation *)fl->fl_owner;
1393 struct task_struct *t;
1394
1395 dprintk("NFSD nfsd_break_deleg_cb: dp %p fl %p\n",dp,fl);
1396 if (!dp)
1397 return;
1398
1399 /* We're assuming the state code never drops its reference
1400 * without first removing the lease. Since we're in this lease
1401 * callback (and since the lease code is serialized by the kernel
1402 * lock) we know the server hasn't removed the lease yet, we know
1403 * it's safe to take a reference: */
1404 atomic_inc(&dp->dl_count);
cfdcad4d 1405 atomic_inc(&dp->dl_client->cl_count);
1da177e4
LT
1406
1407 spin_lock(&recall_lock);
1408 list_add_tail(&dp->dl_recall_lru, &del_recall_lru);
1409 spin_unlock(&recall_lock);
1410
1411 /* only place dl_time is set. protected by lock_kernel*/
1412 dp->dl_time = get_seconds();
1413
0272e1fd
BF
1414 /*
1415 * We don't want the locks code to timeout the lease for us;
1416 * we'll remove it ourself if the delegation isn't returned
1417 * in time.
1418 */
1419 fl->fl_break_time = 0;
1da177e4
LT
1420
1421 t = kthread_run(do_recall, dp, "%s", "nfs4_cb_recall");
1422 if (IS_ERR(t)) {
1423 struct nfs4_client *clp = dp->dl_client;
1424
1425 printk(KERN_INFO "NFSD: Callback thread failed for "
1426 "for client (clientid %08x/%08x)\n",
1427 clp->cl_clientid.cl_boot, clp->cl_clientid.cl_id);
cfdcad4d 1428 put_nfs4_client(dp->dl_client);
1da177e4
LT
1429 nfs4_put_delegation(dp);
1430 }
1431}
1432
1433/*
1434 * The file_lock is being reapd.
1435 *
1436 * Called by locks_free_lock() with lock_kernel() held.
1437 */
1438static
1439void nfsd_release_deleg_cb(struct file_lock *fl)
1440{
1441 struct nfs4_delegation *dp = (struct nfs4_delegation *)fl->fl_owner;
1442
1443 dprintk("NFSD nfsd_release_deleg_cb: fl %p dp %p dl_count %d\n", fl,dp, atomic_read(&dp->dl_count));
1444
1445 if (!(fl->fl_flags & FL_LEASE) || !dp)
1446 return;
1447 dp->dl_flock = NULL;
1448}
1449
1450/*
1451 * Set the delegation file_lock back pointer.
1452 *
a9933cea 1453 * Called from setlease() with lock_kernel() held.
1da177e4
LT
1454 */
1455static
1456void nfsd_copy_lock_deleg_cb(struct file_lock *new, struct file_lock *fl)
1457{
1458 struct nfs4_delegation *dp = (struct nfs4_delegation *)new->fl_owner;
1459
1460 dprintk("NFSD: nfsd_copy_lock_deleg_cb: new fl %p dp %p\n", new, dp);
1461 if (!dp)
1462 return;
1463 dp->dl_flock = new;
1464}
1465
1466/*
a9933cea 1467 * Called from setlease() with lock_kernel() held
1da177e4
LT
1468 */
1469static
1470int nfsd_same_client_deleg_cb(struct file_lock *onlist, struct file_lock *try)
1471{
1472 struct nfs4_delegation *onlistd =
1473 (struct nfs4_delegation *)onlist->fl_owner;
1474 struct nfs4_delegation *tryd =
1475 (struct nfs4_delegation *)try->fl_owner;
1476
1477 if (onlist->fl_lmops != try->fl_lmops)
1478 return 0;
1479
1480 return onlistd->dl_client == tryd->dl_client;
1481}
1482
1483
1484static
1485int nfsd_change_deleg_cb(struct file_lock **onlist, int arg)
1486{
1487 if (arg & F_UNLCK)
1488 return lease_modify(onlist, arg);
1489 else
1490 return -EAGAIN;
1491}
1492
fd39ca9a 1493static struct lock_manager_operations nfsd_lease_mng_ops = {
1da177e4
LT
1494 .fl_break = nfsd_break_deleg_cb,
1495 .fl_release_private = nfsd_release_deleg_cb,
1496 .fl_copy_lock = nfsd_copy_lock_deleg_cb,
1497 .fl_mylease = nfsd_same_client_deleg_cb,
1498 .fl_change = nfsd_change_deleg_cb,
1499};
1500
1501
b37ad28b 1502__be32
1da177e4
LT
1503nfsd4_process_open1(struct nfsd4_open *open)
1504{
1da177e4
LT
1505 clientid_t *clientid = &open->op_clientid;
1506 struct nfs4_client *clp = NULL;
1507 unsigned int strhashval;
1508 struct nfs4_stateowner *sop = NULL;
1509
1da177e4 1510 if (!check_name(open->op_owner))
0f442aa2 1511 return nfserr_inval;
1da177e4
LT
1512
1513 if (STALE_CLIENTID(&open->op_clientid))
1514 return nfserr_stale_clientid;
1515
1516 strhashval = ownerstr_hashval(clientid->cl_id, open->op_owner);
1517 sop = find_openstateowner_str(strhashval, open);
0f442aa2
BF
1518 open->op_stateowner = sop;
1519 if (!sop) {
1520 /* Make sure the client's lease hasn't expired. */
1da177e4
LT
1521 clp = find_confirmed_client(clientid);
1522 if (clp == NULL)
0f442aa2
BF
1523 return nfserr_expired;
1524 goto renew;
1da177e4 1525 }
0f442aa2
BF
1526 if (!sop->so_confirmed) {
1527 /* Replace unconfirmed owners without checking for replay. */
1528 clp = sop->so_client;
f044ff83 1529 release_openowner(sop);
0f442aa2
BF
1530 open->op_stateowner = NULL;
1531 goto renew;
1532 }
1533 if (open->op_seqid == sop->so_seqid - 1) {
1534 if (sop->so_replay.rp_buflen)
a90b061c 1535 return nfserr_replay_me;
0f442aa2
BF
1536 /* The original OPEN failed so spectacularly
1537 * that we don't even have replay data saved!
1538 * Therefore, we have no choice but to continue
1539 * processing this OPEN; presumably, we'll
1540 * fail again for the same reason.
1541 */
1542 dprintk("nfsd4_process_open1: replay with no replay cache\n");
1543 goto renew;
1544 }
1545 if (open->op_seqid != sop->so_seqid)
1546 return nfserr_bad_seqid;
1da177e4 1547renew:
0f442aa2
BF
1548 if (open->op_stateowner == NULL) {
1549 sop = alloc_init_open_stateowner(strhashval, clp, open);
1550 if (sop == NULL)
1551 return nfserr_resource;
1552 open->op_stateowner = sop;
1553 }
1554 list_del_init(&sop->so_close_lru);
1da177e4 1555 renew_client(sop->so_client);
0f442aa2 1556 return nfs_ok;
1da177e4
LT
1557}
1558
b37ad28b 1559static inline __be32
4a6e43e6
N
1560nfs4_check_delegmode(struct nfs4_delegation *dp, int flags)
1561{
1562 if ((flags & WR_STATE) && (dp->dl_type == NFS4_OPEN_DELEGATE_READ))
1563 return nfserr_openmode;
1564 else
1565 return nfs_ok;
1566}
1567
52f4fb43
N
1568static struct nfs4_delegation *
1569find_delegation_file(struct nfs4_file *fp, stateid_t *stid)
1570{
1571 struct nfs4_delegation *dp;
1572
ea1da636 1573 list_for_each_entry(dp, &fp->fi_delegations, dl_perfile) {
52f4fb43
N
1574 if (dp->dl_stateid.si_stateownerid == stid->si_stateownerid)
1575 return dp;
1576 }
1577 return NULL;
1578}
1579
b37ad28b 1580static __be32
567d9829
N
1581nfs4_check_deleg(struct nfs4_file *fp, struct nfsd4_open *open,
1582 struct nfs4_delegation **dp)
1583{
1584 int flags;
b37ad28b 1585 __be32 status = nfserr_bad_stateid;
567d9829
N
1586
1587 *dp = find_delegation_file(fp, &open->op_delegate_stateid);
1588 if (*dp == NULL)
c44c5eeb 1589 goto out;
567d9829
N
1590 flags = open->op_share_access == NFS4_SHARE_ACCESS_READ ?
1591 RD_STATE : WR_STATE;
1592 status = nfs4_check_delegmode(*dp, flags);
1593 if (status)
1594 *dp = NULL;
c44c5eeb
N
1595out:
1596 if (open->op_claim_type != NFS4_OPEN_CLAIM_DELEGATE_CUR)
1597 return nfs_ok;
1598 if (status)
1599 return status;
1600 open->op_stateowner->so_confirmed = 1;
1601 return nfs_ok;
567d9829
N
1602}
1603
b37ad28b 1604static __be32
1da177e4
LT
1605nfs4_check_open(struct nfs4_file *fp, struct nfsd4_open *open, struct nfs4_stateid **stpp)
1606{
1607 struct nfs4_stateid *local;
b37ad28b 1608 __be32 status = nfserr_share_denied;
1da177e4
LT
1609 struct nfs4_stateowner *sop = open->op_stateowner;
1610
8beefa24 1611 list_for_each_entry(local, &fp->fi_stateids, st_perfile) {
1da177e4
LT
1612 /* ignore lock owners */
1613 if (local->st_stateowner->so_is_open_owner == 0)
1614 continue;
1615 /* remember if we have seen this open owner */
1616 if (local->st_stateowner == sop)
1617 *stpp = local;
1618 /* check for conflicting share reservations */
1619 if (!test_share(local, open))
1620 goto out;
1621 }
1622 status = 0;
1623out:
1624 return status;
1625}
1626
5ac049ac
N
1627static inline struct nfs4_stateid *
1628nfs4_alloc_stateid(void)
1629{
1630 return kmem_cache_alloc(stateid_slab, GFP_KERNEL);
1631}
1632
b37ad28b 1633static __be32
1da177e4 1634nfs4_new_open(struct svc_rqst *rqstp, struct nfs4_stateid **stpp,
567d9829 1635 struct nfs4_delegation *dp,
1da177e4
LT
1636 struct svc_fh *cur_fh, int flags)
1637{
1638 struct nfs4_stateid *stp;
1da177e4 1639
5ac049ac 1640 stp = nfs4_alloc_stateid();
1da177e4
LT
1641 if (stp == NULL)
1642 return nfserr_resource;
1643
567d9829
N
1644 if (dp) {
1645 get_file(dp->dl_vfs_file);
1646 stp->st_vfs_file = dp->dl_vfs_file;
1647 } else {
b37ad28b 1648 __be32 status;
567d9829
N
1649 status = nfsd_open(rqstp, cur_fh, S_IFREG, flags,
1650 &stp->st_vfs_file);
1651 if (status) {
1652 if (status == nfserr_dropit)
1653 status = nfserr_jukebox;
5ac049ac 1654 kmem_cache_free(stateid_slab, stp);
567d9829
N
1655 return status;
1656 }
1da177e4 1657 }
1da177e4
LT
1658 *stpp = stp;
1659 return 0;
1660}
1661
b37ad28b 1662static inline __be32
1da177e4
LT
1663nfsd4_truncate(struct svc_rqst *rqstp, struct svc_fh *fh,
1664 struct nfsd4_open *open)
1665{
1666 struct iattr iattr = {
1667 .ia_valid = ATTR_SIZE,
1668 .ia_size = 0,
1669 };
1670 if (!open->op_truncate)
1671 return 0;
1672 if (!(open->op_share_access & NFS4_SHARE_ACCESS_WRITE))
9246585a 1673 return nfserr_inval;
1da177e4
LT
1674 return nfsd_setattr(rqstp, fh, &iattr, 0, (time_t)0);
1675}
1676
b37ad28b 1677static __be32
1da177e4
LT
1678nfs4_upgrade_open(struct svc_rqst *rqstp, struct svc_fh *cur_fh, struct nfs4_stateid *stp, struct nfsd4_open *open)
1679{
1680 struct file *filp = stp->st_vfs_file;
7eaa36e2 1681 struct inode *inode = filp->f_path.dentry->d_inode;
6c26d08f 1682 unsigned int share_access, new_writer;
b37ad28b 1683 __be32 status;
1da177e4
LT
1684
1685 set_access(&share_access, stp->st_access_bmap);
6c26d08f
BF
1686 new_writer = (~share_access) & open->op_share_access
1687 & NFS4_SHARE_ACCESS_WRITE;
1da177e4 1688
6c26d08f 1689 if (new_writer) {
b37ad28b
AV
1690 int err = get_write_access(inode);
1691 if (err)
1692 return nfserrno(err);
e518f056
BH
1693 err = mnt_want_write(cur_fh->fh_export->ex_path.mnt);
1694 if (err)
1695 return nfserrno(err);
1696 file_take_write(filp);
6c26d08f 1697 }
1da177e4
LT
1698 status = nfsd4_truncate(rqstp, cur_fh, open);
1699 if (status) {
6c26d08f
BF
1700 if (new_writer)
1701 put_write_access(inode);
1da177e4
LT
1702 return status;
1703 }
1704 /* remember the open */
6c26d08f 1705 filp->f_mode |= open->op_share_access;
b55e0ba1
BF
1706 __set_bit(open->op_share_access, &stp->st_access_bmap);
1707 __set_bit(open->op_share_deny, &stp->st_deny_bmap);
1da177e4
LT
1708
1709 return nfs_ok;
1710}
1711
1712
1da177e4 1713static void
37515177 1714nfs4_set_claim_prev(struct nfsd4_open *open)
1da177e4 1715{
37515177
N
1716 open->op_stateowner->so_confirmed = 1;
1717 open->op_stateowner->so_client->cl_firststate = 1;
1da177e4
LT
1718}
1719
1720/*
1721 * Attempt to hand out a delegation.
1722 */
1723static void
1724nfs4_open_delegation(struct svc_fh *fh, struct nfsd4_open *open, struct nfs4_stateid *stp)
1725{
1726 struct nfs4_delegation *dp;
1727 struct nfs4_stateowner *sop = stp->st_stateowner;
1728 struct nfs4_callback *cb = &sop->so_client->cl_callback;
1729 struct file_lock fl, *flp = &fl;
1730 int status, flag = 0;
1731
1732 flag = NFS4_OPEN_DELEGATE_NONE;
7b190fec
N
1733 open->op_recall = 0;
1734 switch (open->op_claim_type) {
1735 case NFS4_OPEN_CLAIM_PREVIOUS:
1736 if (!atomic_read(&cb->cb_set))
1737 open->op_recall = 1;
1738 flag = open->op_delegate_type;
1739 if (flag == NFS4_OPEN_DELEGATE_NONE)
1740 goto out;
1741 break;
1742 case NFS4_OPEN_CLAIM_NULL:
1743 /* Let's not give out any delegations till everyone's
1744 * had the chance to reclaim theirs.... */
af558e33 1745 if (locks_in_grace())
7b190fec
N
1746 goto out;
1747 if (!atomic_read(&cb->cb_set) || !sop->so_confirmed)
1748 goto out;
1749 if (open->op_share_access & NFS4_SHARE_ACCESS_WRITE)
1750 flag = NFS4_OPEN_DELEGATE_WRITE;
1751 else
1752 flag = NFS4_OPEN_DELEGATE_READ;
1753 break;
1754 default:
1755 goto out;
1756 }
1da177e4
LT
1757
1758 dp = alloc_init_deleg(sop->so_client, stp, fh, flag);
1759 if (dp == NULL) {
1760 flag = NFS4_OPEN_DELEGATE_NONE;
1761 goto out;
1762 }
1763 locks_init_lock(&fl);
1764 fl.fl_lmops = &nfsd_lease_mng_ops;
1765 fl.fl_flags = FL_LEASE;
9167f501 1766 fl.fl_type = flag == NFS4_OPEN_DELEGATE_READ? F_RDLCK: F_WRLCK;
1da177e4
LT
1767 fl.fl_end = OFFSET_MAX;
1768 fl.fl_owner = (fl_owner_t)dp;
1769 fl.fl_file = stp->st_vfs_file;
1770 fl.fl_pid = current->tgid;
1771
a9933cea 1772 /* vfs_setlease checks to see if delegation should be handed out.
1da177e4
LT
1773 * the lock_manager callbacks fl_mylease and fl_change are used
1774 */
9167f501 1775 if ((status = vfs_setlease(stp->st_vfs_file, fl.fl_type, &flp))) {
1da177e4 1776 dprintk("NFSD: setlease failed [%d], no delegation\n", status);
c907132d 1777 unhash_delegation(dp);
1da177e4
LT
1778 flag = NFS4_OPEN_DELEGATE_NONE;
1779 goto out;
1780 }
1781
1782 memcpy(&open->op_delegate_stateid, &dp->dl_stateid, sizeof(dp->dl_stateid));
1783
1784 dprintk("NFSD: delegation stateid=(%08x/%08x/%08x/%08x)\n\n",
1785 dp->dl_stateid.si_boot,
1786 dp->dl_stateid.si_stateownerid,
1787 dp->dl_stateid.si_fileid,
1788 dp->dl_stateid.si_generation);
1789out:
7b190fec
N
1790 if (open->op_claim_type == NFS4_OPEN_CLAIM_PREVIOUS
1791 && flag == NFS4_OPEN_DELEGATE_NONE
1792 && open->op_delegate_type != NFS4_OPEN_DELEGATE_NONE)
2fdada03 1793 dprintk("NFSD: WARNING: refusing delegation reclaim\n");
1da177e4
LT
1794 open->op_delegate_type = flag;
1795}
1796
1797/*
1798 * called with nfs4_lock_state() held.
1799 */
b37ad28b 1800__be32
1da177e4
LT
1801nfsd4_process_open2(struct svc_rqst *rqstp, struct svc_fh *current_fh, struct nfsd4_open *open)
1802{
1803 struct nfs4_file *fp = NULL;
1804 struct inode *ino = current_fh->fh_dentry->d_inode;
1805 struct nfs4_stateid *stp = NULL;
567d9829 1806 struct nfs4_delegation *dp = NULL;
b37ad28b 1807 __be32 status;
1da177e4
LT
1808
1809 status = nfserr_inval;
ba5a6a19
BF
1810 if (!access_valid(open->op_share_access)
1811 || !deny_valid(open->op_share_deny))
1da177e4
LT
1812 goto out;
1813 /*
1814 * Lookup file; if found, lookup stateid and check open request,
1815 * and check for delegations in the process of being recalled.
1816 * If not found, create the nfs4_file struct
1817 */
1818 fp = find_file(ino);
1819 if (fp) {
1820 if ((status = nfs4_check_open(fp, open, &stp)))
1821 goto out;
c44c5eeb
N
1822 status = nfs4_check_deleg(fp, open, &dp);
1823 if (status)
1824 goto out;
1da177e4 1825 } else {
c44c5eeb
N
1826 status = nfserr_bad_stateid;
1827 if (open->op_claim_type == NFS4_OPEN_CLAIM_DELEGATE_CUR)
1828 goto out;
1da177e4
LT
1829 status = nfserr_resource;
1830 fp = alloc_init_file(ino);
1831 if (fp == NULL)
1832 goto out;
1833 }
1834
1835 /*
1836 * OPEN the file, or upgrade an existing OPEN.
1837 * If truncate fails, the OPEN fails.
1838 */
1839 if (stp) {
1840 /* Stateid was found, this is an OPEN upgrade */
1841 status = nfs4_upgrade_open(rqstp, current_fh, stp, open);
1842 if (status)
1843 goto out;
444c2c07 1844 update_stateid(&stp->st_stateid);
1da177e4
LT
1845 } else {
1846 /* Stateid was not found, this is a new OPEN */
1847 int flags = 0;
9ecb6a08 1848 if (open->op_share_access & NFS4_SHARE_ACCESS_READ)
8837abca 1849 flags |= NFSD_MAY_READ;
1da177e4 1850 if (open->op_share_access & NFS4_SHARE_ACCESS_WRITE)
8837abca 1851 flags |= NFSD_MAY_WRITE;
567d9829
N
1852 status = nfs4_new_open(rqstp, &stp, dp, current_fh, flags);
1853 if (status)
1da177e4
LT
1854 goto out;
1855 init_stateid(stp, fp, open);
1856 status = nfsd4_truncate(rqstp, current_fh, open);
1857 if (status) {
2283963f 1858 release_open_stateid(stp);
1da177e4
LT
1859 goto out;
1860 }
1861 }
1862 memcpy(&open->op_stateid, &stp->st_stateid, sizeof(stateid_t));
1863
1864 /*
1865 * Attempt to hand out a delegation. No error return, because the
1866 * OPEN succeeds even if we fail.
1867 */
1868 nfs4_open_delegation(current_fh, open, stp);
1869
1870 status = nfs_ok;
1871
1872 dprintk("nfs4_process_open2: stateid=(%08x/%08x/%08x/%08x)\n",
1873 stp->st_stateid.si_boot, stp->st_stateid.si_stateownerid,
1874 stp->st_stateid.si_fileid, stp->st_stateid.si_generation);
1875out:
13cd2184
N
1876 if (fp)
1877 put_nfs4_file(fp);
37515177
N
1878 if (status == 0 && open->op_claim_type == NFS4_OPEN_CLAIM_PREVIOUS)
1879 nfs4_set_claim_prev(open);
1da177e4
LT
1880 /*
1881 * To finish the open response, we just need to set the rflags.
1882 */
1883 open->op_rflags = NFS4_OPEN_RESULT_LOCKTYPE_POSIX;
1884 if (!open->op_stateowner->so_confirmed)
1885 open->op_rflags |= NFS4_OPEN_RESULT_CONFIRM;
1886
1887 return status;
1888}
1889
b37ad28b 1890__be32
b591480b
BF
1891nfsd4_renew(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
1892 clientid_t *clid)
1da177e4
LT
1893{
1894 struct nfs4_client *clp;
b37ad28b 1895 __be32 status;
1da177e4
LT
1896
1897 nfs4_lock_state();
1898 dprintk("process_renew(%08x/%08x): starting\n",
1899 clid->cl_boot, clid->cl_id);
1900 status = nfserr_stale_clientid;
1901 if (STALE_CLIENTID(clid))
1902 goto out;
1903 clp = find_confirmed_client(clid);
1904 status = nfserr_expired;
1905 if (clp == NULL) {
1906 /* We assume the client took too long to RENEW. */
1907 dprintk("nfsd4_renew: clientid not found!\n");
1908 goto out;
1909 }
1910 renew_client(clp);
1911 status = nfserr_cb_path_down;
ea1da636 1912 if (!list_empty(&clp->cl_delegations)
1da177e4
LT
1913 && !atomic_read(&clp->cl_callback.cb_set))
1914 goto out;
1915 status = nfs_ok;
1916out:
1917 nfs4_unlock_state();
1918 return status;
1919}
1920
af558e33
BF
1921struct lock_manager nfsd4_manager = {
1922};
1923
a76b4319 1924static void
af558e33 1925nfsd4_end_grace(void)
a76b4319
N
1926{
1927 dprintk("NFSD: end of grace period\n");
c7b9a459 1928 nfsd4_recdir_purge_old();
af558e33 1929 locks_end_grace(&nfsd4_manager);
a76b4319
N
1930}
1931
fd39ca9a 1932static time_t
1da177e4
LT
1933nfs4_laundromat(void)
1934{
1935 struct nfs4_client *clp;
1936 struct nfs4_stateowner *sop;
1937 struct nfs4_delegation *dp;
1938 struct list_head *pos, *next, reaplist;
1939 time_t cutoff = get_seconds() - NFSD_LEASE_TIME;
1940 time_t t, clientid_val = NFSD_LEASE_TIME;
1941 time_t u, test_val = NFSD_LEASE_TIME;
1942
1943 nfs4_lock_state();
1944
1945 dprintk("NFSD: laundromat service - starting\n");
af558e33
BF
1946 if (locks_in_grace())
1947 nfsd4_end_grace();
1da177e4
LT
1948 list_for_each_safe(pos, next, &client_lru) {
1949 clp = list_entry(pos, struct nfs4_client, cl_lru);
1950 if (time_after((unsigned long)clp->cl_time, (unsigned long)cutoff)) {
1951 t = clp->cl_time - cutoff;
1952 if (clientid_val > t)
1953 clientid_val = t;
1954 break;
1955 }
1956 dprintk("NFSD: purging unused client (clientid %08x)\n",
1957 clp->cl_clientid.cl_id);
c7b9a459 1958 nfsd4_remove_clid_dir(clp);
1da177e4
LT
1959 expire_client(clp);
1960 }
1961 INIT_LIST_HEAD(&reaplist);
1962 spin_lock(&recall_lock);
1963 list_for_each_safe(pos, next, &del_recall_lru) {
1964 dp = list_entry (pos, struct nfs4_delegation, dl_recall_lru);
1965 if (time_after((unsigned long)dp->dl_time, (unsigned long)cutoff)) {
1966 u = dp->dl_time - cutoff;
1967 if (test_val > u)
1968 test_val = u;
1969 break;
1970 }
1971 dprintk("NFSD: purging unused delegation dp %p, fp %p\n",
1972 dp, dp->dl_flock);
1973 list_move(&dp->dl_recall_lru, &reaplist);
1974 }
1975 spin_unlock(&recall_lock);
1976 list_for_each_safe(pos, next, &reaplist) {
1977 dp = list_entry (pos, struct nfs4_delegation, dl_recall_lru);
1978 list_del_init(&dp->dl_recall_lru);
1979 unhash_delegation(dp);
1980 }
1981 test_val = NFSD_LEASE_TIME;
1982 list_for_each_safe(pos, next, &close_lru) {
1983 sop = list_entry(pos, struct nfs4_stateowner, so_close_lru);
1984 if (time_after((unsigned long)sop->so_time, (unsigned long)cutoff)) {
1985 u = sop->so_time - cutoff;
1986 if (test_val > u)
1987 test_val = u;
1988 break;
1989 }
1990 dprintk("NFSD: purging unused open stateowner (so_id %d)\n",
1991 sop->so_id);
f044ff83 1992 release_openowner(sop);
1da177e4
LT
1993 }
1994 if (clientid_val < NFSD_LAUNDROMAT_MINTIMEOUT)
1995 clientid_val = NFSD_LAUNDROMAT_MINTIMEOUT;
1996 nfs4_unlock_state();
1997 return clientid_val;
1998}
1999
a254b246
HH
2000static struct workqueue_struct *laundry_wq;
2001static void laundromat_main(struct work_struct *);
2002static DECLARE_DELAYED_WORK(laundromat_work, laundromat_main);
2003
2004static void
c4028958 2005laundromat_main(struct work_struct *not_used)
1da177e4
LT
2006{
2007 time_t t;
2008
2009 t = nfs4_laundromat();
2010 dprintk("NFSD: laundromat_main - sleeping for %ld seconds\n", t);
58da282b 2011 queue_delayed_work(laundry_wq, &laundromat_work, t*HZ);
1da177e4
LT
2012}
2013
fd39ca9a 2014static struct nfs4_stateowner *
f8816512
N
2015search_close_lru(u32 st_id, int flags)
2016{
1da177e4
LT
2017 struct nfs4_stateowner *local = NULL;
2018
1da177e4
LT
2019 if (flags & CLOSE_STATE) {
2020 list_for_each_entry(local, &close_lru, so_close_lru) {
2021 if (local->so_id == st_id)
2022 return local;
2023 }
2024 }
2025 return NULL;
2026}
2027
2028static inline int
2029nfs4_check_fh(struct svc_fh *fhp, struct nfs4_stateid *stp)
2030{
7eaa36e2 2031 return fhp->fh_dentry->d_inode != stp->st_vfs_file->f_path.dentry->d_inode;
1da177e4
LT
2032}
2033
2034static int
2035STALE_STATEID(stateid_t *stateid)
2036{
2037 if (stateid->si_boot == boot_time)
2038 return 0;
849823c5 2039 dprintk("NFSD: stale stateid (%08x/%08x/%08x/%08x)!\n",
1da177e4
LT
2040 stateid->si_boot, stateid->si_stateownerid, stateid->si_fileid,
2041 stateid->si_generation);
2042 return 1;
2043}
2044
2045static inline int
2046access_permit_read(unsigned long access_bmap)
2047{
2048 return test_bit(NFS4_SHARE_ACCESS_READ, &access_bmap) ||
2049 test_bit(NFS4_SHARE_ACCESS_BOTH, &access_bmap) ||
2050 test_bit(NFS4_SHARE_ACCESS_WRITE, &access_bmap);
2051}
2052
2053static inline int
2054access_permit_write(unsigned long access_bmap)
2055{
2056 return test_bit(NFS4_SHARE_ACCESS_WRITE, &access_bmap) ||
2057 test_bit(NFS4_SHARE_ACCESS_BOTH, &access_bmap);
2058}
2059
2060static
b37ad28b 2061__be32 nfs4_check_openmode(struct nfs4_stateid *stp, int flags)
1da177e4 2062{
b37ad28b 2063 __be32 status = nfserr_openmode;
1da177e4
LT
2064
2065 if ((flags & WR_STATE) && (!access_permit_write(stp->st_access_bmap)))
2066 goto out;
2067 if ((flags & RD_STATE) && (!access_permit_read(stp->st_access_bmap)))
2068 goto out;
2069 status = nfs_ok;
2070out:
2071 return status;
2072}
2073
b37ad28b 2074static inline __be32
1da177e4
LT
2075check_special_stateids(svc_fh *current_fh, stateid_t *stateid, int flags)
2076{
203a8c8e 2077 if (ONE_STATEID(stateid) && (flags & RD_STATE))
1da177e4 2078 return nfs_ok;
af558e33 2079 else if (locks_in_grace()) {
1da177e4
LT
2080 /* Answer in remaining cases depends on existance of
2081 * conflicting state; so we must wait out the grace period. */
2082 return nfserr_grace;
2083 } else if (flags & WR_STATE)
2084 return nfs4_share_conflict(current_fh,
2085 NFS4_SHARE_DENY_WRITE);
2086 else /* (flags & RD_STATE) && ZERO_STATEID(stateid) */
2087 return nfs4_share_conflict(current_fh,
2088 NFS4_SHARE_DENY_READ);
2089}
2090
2091/*
2092 * Allow READ/WRITE during grace period on recovered state only for files
2093 * that are not able to provide mandatory locking.
2094 */
2095static inline int
18f82731 2096grace_disallows_io(struct inode *inode)
1da177e4 2097{
203a8c8e 2098 return locks_in_grace() && mandatory_lock(inode);
1da177e4
LT
2099}
2100
0836f587
BF
2101static int check_stateid_generation(stateid_t *in, stateid_t *ref)
2102{
2103 /* If the client sends us a stateid from the future, it's buggy: */
2104 if (in->si_generation > ref->si_generation)
2105 return nfserr_bad_stateid;
2106 /*
2107 * The following, however, can happen. For example, if the
2108 * client sends an open and some IO at the same time, the open
2109 * may bump si_generation while the IO is still in flight.
2110 * Thanks to hard links and renames, the client never knows what
2111 * file an open will affect. So it could avoid that situation
2112 * only by serializing all opens and IO from the same open
2113 * owner. To recover from the old_stateid error, the client
2114 * will just have to retry the IO:
2115 */
2116 if (in->si_generation < ref->si_generation)
2117 return nfserr_old_stateid;
2118 return nfs_ok;
2119}
2120
3e633079
BF
2121static int is_delegation_stateid(stateid_t *stateid)
2122{
2123 return stateid->si_fileid == 0;
2124}
2125
1da177e4
LT
2126/*
2127* Checks for stateid operations
2128*/
b37ad28b 2129__be32
1da177e4
LT
2130nfs4_preprocess_stateid_op(struct svc_fh *current_fh, stateid_t *stateid, int flags, struct file **filpp)
2131{
2132 struct nfs4_stateid *stp = NULL;
2133 struct nfs4_delegation *dp = NULL;
1da177e4 2134 struct inode *ino = current_fh->fh_dentry->d_inode;
b37ad28b 2135 __be32 status;
1da177e4 2136
1da177e4
LT
2137 if (filpp)
2138 *filpp = NULL;
2139
18f82731 2140 if (grace_disallows_io(ino))
1da177e4
LT
2141 return nfserr_grace;
2142
2143 if (ZERO_STATEID(stateid) || ONE_STATEID(stateid))
2144 return check_special_stateids(current_fh, stateid, flags);
2145
1da177e4
LT
2146 status = nfserr_stale_stateid;
2147 if (STALE_STATEID(stateid))
2148 goto out;
2149
1da177e4 2150 status = nfserr_bad_stateid;
3e633079 2151 if (is_delegation_stateid(stateid)) {
a4455be0 2152 dp = find_delegation_stateid(ino, stateid);
819a8f53 2153 if (!dp)
1da177e4 2154 goto out;
fd03b099 2155 status = check_stateid_generation(stateid, &dp->dl_stateid);
0c2a498f
BF
2156 if (status)
2157 goto out;
dc9bf700
BF
2158 status = nfs4_check_delegmode(dp, flags);
2159 if (status)
2160 goto out;
2161 renew_client(dp->dl_client);
dc9bf700
BF
2162 if (filpp)
2163 *filpp = dp->dl_vfs_file;
1da177e4 2164 } else { /* open or lock stateid */
a4455be0 2165 stp = find_stateid(stateid, flags);
819a8f53 2166 if (!stp)
1da177e4 2167 goto out;
6150ef0d 2168 if (nfs4_check_fh(current_fh, stp))
1da177e4
LT
2169 goto out;
2170 if (!stp->st_stateowner->so_confirmed)
2171 goto out;
fd03b099 2172 status = check_stateid_generation(stateid, &stp->st_stateid);
0c2a498f
BF
2173 if (status)
2174 goto out;
a4455be0
BF
2175 status = nfs4_check_openmode(stp, flags);
2176 if (status)
1da177e4
LT
2177 goto out;
2178 renew_client(stp->st_stateowner->so_client);
2179 if (filpp)
2180 *filpp = stp->st_vfs_file;
1da177e4
LT
2181 }
2182 status = nfs_ok;
2183out:
2184 return status;
2185}
2186
4c4cd222
N
2187static inline int
2188setlkflg (int type)
2189{
2190 return (type == NFS4_READW_LT || type == NFS4_READ_LT) ?
2191 RD_STATE : WR_STATE;
2192}
1da177e4
LT
2193
2194/*
2195 * Checks for sequence id mutating operations.
2196 */
b37ad28b 2197static __be32
4c4cd222 2198nfs4_preprocess_seqid_op(struct svc_fh *current_fh, u32 seqid, stateid_t *stateid, int flags, struct nfs4_stateowner **sopp, struct nfs4_stateid **stpp, struct nfsd4_lock *lock)
1da177e4 2199{
1da177e4
LT
2200 struct nfs4_stateid *stp;
2201 struct nfs4_stateowner *sop;
0836f587 2202 __be32 status;
1da177e4
LT
2203
2204 dprintk("NFSD: preprocess_seqid_op: seqid=%d "
2205 "stateid = (%08x/%08x/%08x/%08x)\n", seqid,
2206 stateid->si_boot, stateid->si_stateownerid, stateid->si_fileid,
2207 stateid->si_generation);
3a4f98bb 2208
1da177e4
LT
2209 *stpp = NULL;
2210 *sopp = NULL;
2211
1da177e4 2212 if (ZERO_STATEID(stateid) || ONE_STATEID(stateid)) {
2fdada03 2213 dprintk("NFSD: preprocess_seqid_op: magic stateid!\n");
3a4f98bb 2214 return nfserr_bad_stateid;
1da177e4
LT
2215 }
2216
1da177e4 2217 if (STALE_STATEID(stateid))
3a4f98bb 2218 return nfserr_stale_stateid;
1da177e4
LT
2219 /*
2220 * We return BAD_STATEID if filehandle doesn't match stateid,
2221 * the confirmed flag is incorrecly set, or the generation
2222 * number is incorrect.
1da177e4 2223 */
f8816512
N
2224 stp = find_stateid(stateid, flags);
2225 if (stp == NULL) {
2226 /*
2227 * Also, we should make sure this isn't just the result of
2228 * a replayed close:
2229 */
2230 sop = search_close_lru(stateid->si_stateownerid, flags);
2231 if (sop == NULL)
2232 return nfserr_bad_stateid;
2233 *sopp = sop;
2234 goto check_replay;
2235 }
1da177e4 2236
39325bd0
BF
2237 *stpp = stp;
2238 *sopp = sop = stp->st_stateowner;
2239
4c4cd222 2240 if (lock) {
4c4cd222 2241 clientid_t *lockclid = &lock->v.new.clientid;
1da177e4 2242 struct nfs4_client *clp = sop->so_client;
4c4cd222 2243 int lkflg = 0;
b37ad28b 2244 __be32 status;
4c4cd222
N
2245
2246 lkflg = setlkflg(lock->lk_type);
2247
2248 if (lock->lk_is_new) {
599e0a22
BF
2249 if (!sop->so_is_open_owner)
2250 return nfserr_bad_stateid;
2251 if (!same_clid(&clp->cl_clientid, lockclid))
4c4cd222 2252 return nfserr_bad_stateid;
599e0a22
BF
2253 /* stp is the open stateid */
2254 status = nfs4_check_openmode(stp, lkflg);
2255 if (status)
2256 return status;
2257 } else {
2258 /* stp is the lock stateid */
2259 status = nfs4_check_openmode(stp->st_openstp, lkflg);
2260 if (status)
2261 return status;
4c4cd222 2262 }
1da177e4
LT
2263 }
2264
f3362737 2265 if (nfs4_check_fh(current_fh, stp)) {
2fdada03 2266 dprintk("NFSD: preprocess_seqid_op: fh-stateid mismatch!\n");
3a4f98bb 2267 return nfserr_bad_stateid;
1da177e4
LT
2268 }
2269
1da177e4
LT
2270 /*
2271 * We now validate the seqid and stateid generation numbers.
2272 * For the moment, we ignore the possibility of
2273 * generation number wraparound.
2274 */
bd9aac52 2275 if (seqid != sop->so_seqid)
1da177e4
LT
2276 goto check_replay;
2277
3a4f98bb 2278 if (sop->so_confirmed && flags & CONFIRM) {
2fdada03 2279 dprintk("NFSD: preprocess_seqid_op: expected"
3a4f98bb
N
2280 " unconfirmed stateowner!\n");
2281 return nfserr_bad_stateid;
1da177e4 2282 }
3a4f98bb 2283 if (!sop->so_confirmed && !(flags & CONFIRM)) {
2fdada03 2284 dprintk("NFSD: preprocess_seqid_op: stateowner not"
3a4f98bb
N
2285 " confirmed yet!\n");
2286 return nfserr_bad_stateid;
1da177e4 2287 }
0836f587
BF
2288 status = check_stateid_generation(stateid, &stp->st_stateid);
2289 if (status)
2290 return status;
52fd004e 2291 renew_client(sop->so_client);
3a4f98bb 2292 return nfs_ok;
1da177e4 2293
1da177e4 2294check_replay:
bd9aac52 2295 if (seqid == sop->so_seqid - 1) {
849823c5 2296 dprintk("NFSD: preprocess_seqid_op: retransmission?\n");
1da177e4 2297 /* indicate replay to calling function */
a90b061c 2298 return nfserr_replay_me;
1da177e4 2299 }
2fdada03 2300 dprintk("NFSD: preprocess_seqid_op: bad seqid (expected %d, got %d)\n",
3a4f98bb
N
2301 sop->so_seqid, seqid);
2302 *sopp = NULL;
2303 return nfserr_bad_seqid;
1da177e4
LT
2304}
2305
b37ad28b 2306__be32
ca364317 2307nfsd4_open_confirm(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
a4f1706a 2308 struct nfsd4_open_confirm *oc)
1da177e4 2309{
b37ad28b 2310 __be32 status;
1da177e4
LT
2311 struct nfs4_stateowner *sop;
2312 struct nfs4_stateid *stp;
2313
2314 dprintk("NFSD: nfsd4_open_confirm on file %.*s\n",
ca364317
BF
2315 (int)cstate->current_fh.fh_dentry->d_name.len,
2316 cstate->current_fh.fh_dentry->d_name.name);
1da177e4 2317
ca364317 2318 status = fh_verify(rqstp, &cstate->current_fh, S_IFREG, 0);
a8cddc5d
BF
2319 if (status)
2320 return status;
1da177e4
LT
2321
2322 nfs4_lock_state();
2323
ca364317
BF
2324 if ((status = nfs4_preprocess_seqid_op(&cstate->current_fh,
2325 oc->oc_seqid, &oc->oc_req_stateid,
f3362737 2326 CONFIRM | OPEN_STATE,
1da177e4
LT
2327 &oc->oc_stateowner, &stp, NULL)))
2328 goto out;
2329
2330 sop = oc->oc_stateowner;
2331 sop->so_confirmed = 1;
2332 update_stateid(&stp->st_stateid);
2333 memcpy(&oc->oc_resp_stateid, &stp->st_stateid, sizeof(stateid_t));
2334 dprintk("NFSD: nfsd4_open_confirm: success, seqid=%d "
2335 "stateid=(%08x/%08x/%08x/%08x)\n", oc->oc_seqid,
2336 stp->st_stateid.si_boot,
2337 stp->st_stateid.si_stateownerid,
2338 stp->st_stateid.si_fileid,
2339 stp->st_stateid.si_generation);
c7b9a459
N
2340
2341 nfsd4_create_clid_dir(sop->so_client);
1da177e4 2342out:
f2327d9a 2343 if (oc->oc_stateowner) {
1da177e4 2344 nfs4_get_stateowner(oc->oc_stateowner);
a4f1706a 2345 cstate->replay_owner = oc->oc_stateowner;
f2327d9a 2346 }
1da177e4
LT
2347 nfs4_unlock_state();
2348 return status;
2349}
2350
2351
2352/*
2353 * unset all bits in union bitmap (bmap) that
2354 * do not exist in share (from successful OPEN_DOWNGRADE)
2355 */
2356static void
2357reset_union_bmap_access(unsigned long access, unsigned long *bmap)
2358{
2359 int i;
2360 for (i = 1; i < 4; i++) {
2361 if ((i & access) != i)
2362 __clear_bit(i, bmap);
2363 }
2364}
2365
2366static void
2367reset_union_bmap_deny(unsigned long deny, unsigned long *bmap)
2368{
2369 int i;
2370 for (i = 0; i < 4; i++) {
2371 if ((i & deny) != i)
2372 __clear_bit(i, bmap);
2373 }
2374}
2375
b37ad28b 2376__be32
ca364317
BF
2377nfsd4_open_downgrade(struct svc_rqst *rqstp,
2378 struct nfsd4_compound_state *cstate,
a4f1706a 2379 struct nfsd4_open_downgrade *od)
1da177e4 2380{
b37ad28b 2381 __be32 status;
1da177e4
LT
2382 struct nfs4_stateid *stp;
2383 unsigned int share_access;
2384
2385 dprintk("NFSD: nfsd4_open_downgrade on file %.*s\n",
ca364317
BF
2386 (int)cstate->current_fh.fh_dentry->d_name.len,
2387 cstate->current_fh.fh_dentry->d_name.name);
1da177e4 2388
ba5a6a19
BF
2389 if (!access_valid(od->od_share_access)
2390 || !deny_valid(od->od_share_deny))
1da177e4
LT
2391 return nfserr_inval;
2392
2393 nfs4_lock_state();
ca364317
BF
2394 if ((status = nfs4_preprocess_seqid_op(&cstate->current_fh,
2395 od->od_seqid,
1da177e4 2396 &od->od_stateid,
f3362737 2397 OPEN_STATE,
1da177e4
LT
2398 &od->od_stateowner, &stp, NULL)))
2399 goto out;
2400
2401 status = nfserr_inval;
2402 if (!test_bit(od->od_share_access, &stp->st_access_bmap)) {
2403 dprintk("NFSD:access not a subset current bitmap: 0x%lx, input access=%08x\n",
2404 stp->st_access_bmap, od->od_share_access);
2405 goto out;
2406 }
2407 if (!test_bit(od->od_share_deny, &stp->st_deny_bmap)) {
2408 dprintk("NFSD:deny not a subset current bitmap: 0x%lx, input deny=%08x\n",
2409 stp->st_deny_bmap, od->od_share_deny);
2410 goto out;
2411 }
2412 set_access(&share_access, stp->st_access_bmap);
2413 nfs4_file_downgrade(stp->st_vfs_file,
2414 share_access & ~od->od_share_access);
2415
2416 reset_union_bmap_access(od->od_share_access, &stp->st_access_bmap);
2417 reset_union_bmap_deny(od->od_share_deny, &stp->st_deny_bmap);
2418
2419 update_stateid(&stp->st_stateid);
2420 memcpy(&od->od_stateid, &stp->st_stateid, sizeof(stateid_t));
2421 status = nfs_ok;
2422out:
f2327d9a 2423 if (od->od_stateowner) {
1da177e4 2424 nfs4_get_stateowner(od->od_stateowner);
a4f1706a 2425 cstate->replay_owner = od->od_stateowner;
f2327d9a 2426 }
1da177e4
LT
2427 nfs4_unlock_state();
2428 return status;
2429}
2430
2431/*
2432 * nfs4_unlock_state() called after encode
2433 */
b37ad28b 2434__be32
ca364317 2435nfsd4_close(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
a4f1706a 2436 struct nfsd4_close *close)
1da177e4 2437{
b37ad28b 2438 __be32 status;
1da177e4
LT
2439 struct nfs4_stateid *stp;
2440
2441 dprintk("NFSD: nfsd4_close on file %.*s\n",
ca364317
BF
2442 (int)cstate->current_fh.fh_dentry->d_name.len,
2443 cstate->current_fh.fh_dentry->d_name.name);
1da177e4
LT
2444
2445 nfs4_lock_state();
2446 /* check close_lru for replay */
ca364317
BF
2447 if ((status = nfs4_preprocess_seqid_op(&cstate->current_fh,
2448 close->cl_seqid,
1da177e4 2449 &close->cl_stateid,
f3362737 2450 OPEN_STATE | CLOSE_STATE,
1da177e4
LT
2451 &close->cl_stateowner, &stp, NULL)))
2452 goto out;
1da177e4
LT
2453 status = nfs_ok;
2454 update_stateid(&stp->st_stateid);
2455 memcpy(&close->cl_stateid, &stp->st_stateid, sizeof(stateid_t));
2456
04ef5954 2457 /* release_stateid() calls nfsd_close() if needed */
2283963f 2458 release_open_stateid(stp);
04ef5954
BF
2459
2460 /* place unused nfs4_stateowners on so_close_lru list to be
2461 * released by the laundromat service after the lease period
2462 * to enable us to handle CLOSE replay
2463 */
2464 if (list_empty(&close->cl_stateowner->so_stateids))
2465 move_to_close_lru(close->cl_stateowner);
1da177e4 2466out:
f2327d9a 2467 if (close->cl_stateowner) {
1da177e4 2468 nfs4_get_stateowner(close->cl_stateowner);
a4f1706a 2469 cstate->replay_owner = close->cl_stateowner;
f2327d9a 2470 }
1da177e4
LT
2471 nfs4_unlock_state();
2472 return status;
2473}
2474
b37ad28b 2475__be32
ca364317
BF
2476nfsd4_delegreturn(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
2477 struct nfsd4_delegreturn *dr)
1da177e4 2478{
203a8c8e
BF
2479 struct nfs4_delegation *dp;
2480 stateid_t *stateid = &dr->dr_stateid;
2481 struct inode *inode;
b37ad28b 2482 __be32 status;
1da177e4 2483
ca364317 2484 if ((status = fh_verify(rqstp, &cstate->current_fh, S_IFREG, 0)))
203a8c8e
BF
2485 return status;
2486 inode = cstate->current_fh.fh_dentry->d_inode;
1da177e4
LT
2487
2488 nfs4_lock_state();
203a8c8e
BF
2489 status = nfserr_bad_stateid;
2490 if (ZERO_STATEID(stateid) || ONE_STATEID(stateid))
2491 goto out;
2492 status = nfserr_stale_stateid;
2493 if (STALE_STATEID(stateid))
2494 goto out;
7e0f7cf5 2495 status = nfserr_bad_stateid;
203a8c8e
BF
2496 if (!is_delegation_stateid(stateid))
2497 goto out;
203a8c8e
BF
2498 dp = find_delegation_stateid(inode, stateid);
2499 if (!dp)
2500 goto out;
2501 status = check_stateid_generation(stateid, &dp->dl_stateid);
2502 if (status)
2503 goto out;
2504 renew_client(dp->dl_client);
2505
2506 unhash_delegation(dp);
1da177e4 2507out:
203a8c8e
BF
2508 nfs4_unlock_state();
2509
1da177e4
LT
2510 return status;
2511}
2512
2513
2514/*
2515 * Lock owner state (byte-range locks)
2516 */
2517#define LOFF_OVERFLOW(start, len) ((u64)(len) > ~(u64)(start))
2518#define LOCK_HASH_BITS 8
2519#define LOCK_HASH_SIZE (1 << LOCK_HASH_BITS)
2520#define LOCK_HASH_MASK (LOCK_HASH_SIZE - 1)
2521
87df4de8
BH
2522static inline u64
2523end_offset(u64 start, u64 len)
2524{
2525 u64 end;
2526
2527 end = start + len;
2528 return end >= start ? end: NFS4_MAX_UINT64;
2529}
2530
2531/* last octet in a range */
2532static inline u64
2533last_byte_offset(u64 start, u64 len)
2534{
2535 u64 end;
2536
2537 BUG_ON(!len);
2538 end = start + len;
2539 return end > start ? end - 1: NFS4_MAX_UINT64;
2540}
2541
1da177e4
LT
2542#define lockownerid_hashval(id) \
2543 ((id) & LOCK_HASH_MASK)
2544
2545static inline unsigned int
2546lock_ownerstr_hashval(struct inode *inode, u32 cl_id,
2547 struct xdr_netobj *ownername)
2548{
2549 return (file_hashval(inode) + cl_id
2550 + opaque_hashval(ownername->data, ownername->len))
2551 & LOCK_HASH_MASK;
2552}
2553
2554static struct list_head lock_ownerid_hashtbl[LOCK_HASH_SIZE];
2555static struct list_head lock_ownerstr_hashtbl[LOCK_HASH_SIZE];
2556static struct list_head lockstateid_hashtbl[STATEID_HASH_SIZE];
2557
fd39ca9a 2558static struct nfs4_stateid *
1da177e4
LT
2559find_stateid(stateid_t *stid, int flags)
2560{
9346eff0 2561 struct nfs4_stateid *local;
1da177e4
LT
2562 u32 st_id = stid->si_stateownerid;
2563 u32 f_id = stid->si_fileid;
2564 unsigned int hashval;
2565
2566 dprintk("NFSD: find_stateid flags 0x%x\n",flags);
9346eff0 2567 if (flags & (LOCK_STATE | RD_STATE | WR_STATE)) {
1da177e4
LT
2568 hashval = stateid_hashval(st_id, f_id);
2569 list_for_each_entry(local, &lockstateid_hashtbl[hashval], st_hash) {
2570 if ((local->st_stateid.si_stateownerid == st_id) &&
2571 (local->st_stateid.si_fileid == f_id))
2572 return local;
2573 }
2574 }
9346eff0
KK
2575
2576 if (flags & (OPEN_STATE | RD_STATE | WR_STATE)) {
1da177e4
LT
2577 hashval = stateid_hashval(st_id, f_id);
2578 list_for_each_entry(local, &stateid_hashtbl[hashval], st_hash) {
2579 if ((local->st_stateid.si_stateownerid == st_id) &&
2580 (local->st_stateid.si_fileid == f_id))
2581 return local;
2582 }
849823c5 2583 }
1da177e4
LT
2584 return NULL;
2585}
2586
2587static struct nfs4_delegation *
2588find_delegation_stateid(struct inode *ino, stateid_t *stid)
2589{
13cd2184
N
2590 struct nfs4_file *fp;
2591 struct nfs4_delegation *dl;
1da177e4
LT
2592
2593 dprintk("NFSD:find_delegation_stateid stateid=(%08x/%08x/%08x/%08x)\n",
2594 stid->si_boot, stid->si_stateownerid,
2595 stid->si_fileid, stid->si_generation);
2596
1da177e4 2597 fp = find_file(ino);
13cd2184
N
2598 if (!fp)
2599 return NULL;
2600 dl = find_delegation_file(fp, stid);
2601 put_nfs4_file(fp);
2602 return dl;
1da177e4
LT
2603}
2604
2605/*
2606 * TODO: Linux file offsets are _signed_ 64-bit quantities, which means that
2607 * we can't properly handle lock requests that go beyond the (2^63 - 1)-th
2608 * byte, because of sign extension problems. Since NFSv4 calls for 64-bit
2609 * locking, this prevents us from being completely protocol-compliant. The
2610 * real solution to this problem is to start using unsigned file offsets in
2611 * the VFS, but this is a very deep change!
2612 */
2613static inline void
2614nfs4_transform_lock_offset(struct file_lock *lock)
2615{
2616 if (lock->fl_start < 0)
2617 lock->fl_start = OFFSET_MAX;
2618 if (lock->fl_end < 0)
2619 lock->fl_end = OFFSET_MAX;
2620}
2621
d5b9026a
N
2622/* Hack!: For now, we're defining this just so we can use a pointer to it
2623 * as a unique cookie to identify our (NFSv4's) posix locks. */
e465a77f 2624static struct lock_manager_operations nfsd_posix_mng_ops = {
d5b9026a 2625};
1da177e4
LT
2626
2627static inline void
2628nfs4_set_lock_denied(struct file_lock *fl, struct nfsd4_lock_denied *deny)
2629{
d5b9026a
N
2630 struct nfs4_stateowner *sop;
2631 unsigned int hval;
1da177e4 2632
d5b9026a
N
2633 if (fl->fl_lmops == &nfsd_posix_mng_ops) {
2634 sop = (struct nfs4_stateowner *) fl->fl_owner;
2635 hval = lockownerid_hashval(sop->so_id);
1da177e4
LT
2636 kref_get(&sop->so_ref);
2637 deny->ld_sop = sop;
2638 deny->ld_clientid = sop->so_client->cl_clientid;
d5b9026a
N
2639 } else {
2640 deny->ld_sop = NULL;
2641 deny->ld_clientid.cl_boot = 0;
2642 deny->ld_clientid.cl_id = 0;
1da177e4
LT
2643 }
2644 deny->ld_start = fl->fl_start;
87df4de8
BH
2645 deny->ld_length = NFS4_MAX_UINT64;
2646 if (fl->fl_end != NFS4_MAX_UINT64)
1da177e4
LT
2647 deny->ld_length = fl->fl_end - fl->fl_start + 1;
2648 deny->ld_type = NFS4_READ_LT;
2649 if (fl->fl_type != F_RDLCK)
2650 deny->ld_type = NFS4_WRITE_LT;
2651}
2652
1da177e4
LT
2653static struct nfs4_stateowner *
2654find_lockstateowner_str(struct inode *inode, clientid_t *clid,
2655 struct xdr_netobj *owner)
2656{
2657 unsigned int hashval = lock_ownerstr_hashval(inode, clid->cl_id, owner);
2658 struct nfs4_stateowner *op;
2659
2660 list_for_each_entry(op, &lock_ownerstr_hashtbl[hashval], so_strhash) {
599e0a22 2661 if (same_owner_str(op, owner, clid))
1da177e4
LT
2662 return op;
2663 }
2664 return NULL;
2665}
2666
2667/*
2668 * Alloc a lock owner structure.
2669 * Called in nfsd4_lock - therefore, OPEN and OPEN_CONFIRM (if needed) has
2670 * occured.
2671 *
2672 * strhashval = lock_ownerstr_hashval
1da177e4
LT
2673 */
2674
2675static struct nfs4_stateowner *
2676alloc_init_lock_stateowner(unsigned int strhashval, struct nfs4_client *clp, struct nfs4_stateid *open_stp, struct nfsd4_lock *lock) {
2677 struct nfs4_stateowner *sop;
2678 struct nfs4_replay *rp;
2679 unsigned int idhashval;
2680
2681 if (!(sop = alloc_stateowner(&lock->lk_new_owner)))
2682 return NULL;
2683 idhashval = lockownerid_hashval(current_ownerid);
2684 INIT_LIST_HEAD(&sop->so_idhash);
2685 INIT_LIST_HEAD(&sop->so_strhash);
2686 INIT_LIST_HEAD(&sop->so_perclient);
ea1da636
N
2687 INIT_LIST_HEAD(&sop->so_stateids);
2688 INIT_LIST_HEAD(&sop->so_perstateid);
1da177e4
LT
2689 INIT_LIST_HEAD(&sop->so_close_lru); /* not used */
2690 sop->so_time = 0;
2691 list_add(&sop->so_idhash, &lock_ownerid_hashtbl[idhashval]);
2692 list_add(&sop->so_strhash, &lock_ownerstr_hashtbl[strhashval]);
ea1da636 2693 list_add(&sop->so_perstateid, &open_stp->st_lockowners);
1da177e4
LT
2694 sop->so_is_open_owner = 0;
2695 sop->so_id = current_ownerid++;
2696 sop->so_client = clp;
b59e3c0e
NB
2697 /* It is the openowner seqid that will be incremented in encode in the
2698 * case of new lockowners; so increment the lock seqid manually: */
2699 sop->so_seqid = lock->lk_new_lock_seqid + 1;
1da177e4
LT
2700 sop->so_confirmed = 1;
2701 rp = &sop->so_replay;
de1ae286 2702 rp->rp_status = nfserr_serverfault;
1da177e4
LT
2703 rp->rp_buflen = 0;
2704 rp->rp_buf = rp->rp_ibuf;
2705 return sop;
2706}
2707
fd39ca9a 2708static struct nfs4_stateid *
1da177e4
LT
2709alloc_init_lock_stateid(struct nfs4_stateowner *sop, struct nfs4_file *fp, struct nfs4_stateid *open_stp)
2710{
2711 struct nfs4_stateid *stp;
2712 unsigned int hashval = stateid_hashval(sop->so_id, fp->fi_id);
2713
5ac049ac
N
2714 stp = nfs4_alloc_stateid();
2715 if (stp == NULL)
1da177e4
LT
2716 goto out;
2717 INIT_LIST_HEAD(&stp->st_hash);
2718 INIT_LIST_HEAD(&stp->st_perfile);
ea1da636
N
2719 INIT_LIST_HEAD(&stp->st_perstateowner);
2720 INIT_LIST_HEAD(&stp->st_lockowners); /* not used */
1da177e4 2721 list_add(&stp->st_hash, &lockstateid_hashtbl[hashval]);
8beefa24 2722 list_add(&stp->st_perfile, &fp->fi_stateids);
ea1da636 2723 list_add(&stp->st_perstateowner, &sop->so_stateids);
1da177e4 2724 stp->st_stateowner = sop;
13cd2184 2725 get_nfs4_file(fp);
1da177e4
LT
2726 stp->st_file = fp;
2727 stp->st_stateid.si_boot = boot_time;
2728 stp->st_stateid.si_stateownerid = sop->so_id;
2729 stp->st_stateid.si_fileid = fp->fi_id;
2730 stp->st_stateid.si_generation = 0;
2731 stp->st_vfs_file = open_stp->st_vfs_file; /* FIXME refcount?? */
2732 stp->st_access_bmap = open_stp->st_access_bmap;
2733 stp->st_deny_bmap = open_stp->st_deny_bmap;
4c4cd222 2734 stp->st_openstp = open_stp;
1da177e4
LT
2735
2736out:
2737 return stp;
2738}
2739
fd39ca9a 2740static int
1da177e4
LT
2741check_lock_length(u64 offset, u64 length)
2742{
87df4de8 2743 return ((length == 0) || ((length != NFS4_MAX_UINT64) &&
1da177e4
LT
2744 LOFF_OVERFLOW(offset, length)));
2745}
2746
2747/*
2748 * LOCK operation
2749 */
b37ad28b 2750__be32
ca364317 2751nfsd4_lock(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
a4f1706a 2752 struct nfsd4_lock *lock)
1da177e4 2753{
3e9e3dbe 2754 struct nfs4_stateowner *open_sop = NULL;
b59e3c0e 2755 struct nfs4_stateowner *lock_sop = NULL;
1da177e4
LT
2756 struct nfs4_stateid *lock_stp;
2757 struct file *filp;
2758 struct file_lock file_lock;
8dc7c311 2759 struct file_lock conflock;
b37ad28b 2760 __be32 status = 0;
1da177e4 2761 unsigned int strhashval;
150b3934 2762 unsigned int cmd;
b8dd7b9a 2763 int err;
1da177e4
LT
2764
2765 dprintk("NFSD: nfsd4_lock: start=%Ld length=%Ld\n",
2766 (long long) lock->lk_offset,
2767 (long long) lock->lk_length);
2768
1da177e4
LT
2769 if (check_lock_length(lock->lk_offset, lock->lk_length))
2770 return nfserr_inval;
2771
ca364317 2772 if ((status = fh_verify(rqstp, &cstate->current_fh,
8837abca 2773 S_IFREG, NFSD_MAY_LOCK))) {
a6f6ef2f
AA
2774 dprintk("NFSD: nfsd4_lock: permission denied!\n");
2775 return status;
2776 }
2777
1da177e4
LT
2778 nfs4_lock_state();
2779
2780 if (lock->lk_is_new) {
893f8770
N
2781 /*
2782 * Client indicates that this is a new lockowner.
2783 * Use open owner and open stateid to create lock owner and
2784 * lock stateid.
2785 */
1da177e4
LT
2786 struct nfs4_stateid *open_stp = NULL;
2787 struct nfs4_file *fp;
2788
2789 status = nfserr_stale_clientid;
849823c5 2790 if (STALE_CLIENTID(&lock->lk_new_clientid))
1da177e4 2791 goto out;
1da177e4 2792
1da177e4 2793 /* validate and update open stateid and open seqid */
ca364317 2794 status = nfs4_preprocess_seqid_op(&cstate->current_fh,
1da177e4
LT
2795 lock->lk_new_open_seqid,
2796 &lock->lk_new_open_stateid,
f3362737 2797 OPEN_STATE,
3a65588a 2798 &lock->lk_replay_owner, &open_stp,
b59e3c0e 2799 lock);
37515177 2800 if (status)
1da177e4 2801 goto out;
3a65588a 2802 open_sop = lock->lk_replay_owner;
1da177e4
LT
2803 /* create lockowner and lock stateid */
2804 fp = open_stp->st_file;
2805 strhashval = lock_ownerstr_hashval(fp->fi_inode,
2806 open_sop->so_client->cl_clientid.cl_id,
2807 &lock->v.new.owner);
3e9e3dbe
N
2808 /* XXX: Do we need to check for duplicate stateowners on
2809 * the same file, or should they just be allowed (and
2810 * create new stateids)? */
1da177e4 2811 status = nfserr_resource;
b59e3c0e
NB
2812 lock_sop = alloc_init_lock_stateowner(strhashval,
2813 open_sop->so_client, open_stp, lock);
2814 if (lock_sop == NULL)
1da177e4 2815 goto out;
b59e3c0e 2816 lock_stp = alloc_init_lock_stateid(lock_sop, fp, open_stp);
8a280510 2817 if (lock_stp == NULL)
1da177e4 2818 goto out;
1da177e4
LT
2819 } else {
2820 /* lock (lock owner + lock stateid) already exists */
ca364317 2821 status = nfs4_preprocess_seqid_op(&cstate->current_fh,
1da177e4
LT
2822 lock->lk_old_lock_seqid,
2823 &lock->lk_old_lock_stateid,
f3362737 2824 LOCK_STATE,
3a65588a 2825 &lock->lk_replay_owner, &lock_stp, lock);
1da177e4
LT
2826 if (status)
2827 goto out;
3a65588a 2828 lock_sop = lock->lk_replay_owner;
1da177e4 2829 }
3a65588a 2830 /* lock->lk_replay_owner and lock_stp have been created or found */
1da177e4
LT
2831 filp = lock_stp->st_vfs_file;
2832
0dd395dc 2833 status = nfserr_grace;
af558e33 2834 if (locks_in_grace() && !lock->lk_reclaim)
0dd395dc
N
2835 goto out;
2836 status = nfserr_no_grace;
af558e33 2837 if (!locks_in_grace() && lock->lk_reclaim)
0dd395dc
N
2838 goto out;
2839
1da177e4
LT
2840 locks_init_lock(&file_lock);
2841 switch (lock->lk_type) {
2842 case NFS4_READ_LT:
2843 case NFS4_READW_LT:
2844 file_lock.fl_type = F_RDLCK;
150b3934 2845 cmd = F_SETLK;
1da177e4
LT
2846 break;
2847 case NFS4_WRITE_LT:
2848 case NFS4_WRITEW_LT:
2849 file_lock.fl_type = F_WRLCK;
150b3934 2850 cmd = F_SETLK;
1da177e4
LT
2851 break;
2852 default:
2853 status = nfserr_inval;
2854 goto out;
2855 }
b59e3c0e 2856 file_lock.fl_owner = (fl_owner_t)lock_sop;
1da177e4
LT
2857 file_lock.fl_pid = current->tgid;
2858 file_lock.fl_file = filp;
2859 file_lock.fl_flags = FL_POSIX;
d5b9026a 2860 file_lock.fl_lmops = &nfsd_posix_mng_ops;
1da177e4
LT
2861
2862 file_lock.fl_start = lock->lk_offset;
87df4de8 2863 file_lock.fl_end = last_byte_offset(lock->lk_offset, lock->lk_length);
1da177e4
LT
2864 nfs4_transform_lock_offset(&file_lock);
2865
2866 /*
2867 * Try to lock the file in the VFS.
2868 * Note: locks.c uses the BKL to protect the inode's lock list.
2869 */
2870
fd85b817 2871 err = vfs_lock_file(filp, cmd, &file_lock, &conflock);
b8dd7b9a 2872 switch (-err) {
1da177e4
LT
2873 case 0: /* success! */
2874 update_stateid(&lock_stp->st_stateid);
2875 memcpy(&lock->lk_resp_stateid, &lock_stp->st_stateid,
2876 sizeof(stateid_t));
b8dd7b9a 2877 status = 0;
eb76b3fd
AA
2878 break;
2879 case (EAGAIN): /* conflock holds conflicting lock */
2880 status = nfserr_denied;
2881 dprintk("NFSD: nfsd4_lock: conflicting lock found!\n");
2882 nfs4_set_lock_denied(&conflock, &lock->lk_denied);
2883 break;
1da177e4
LT
2884 case (EDEADLK):
2885 status = nfserr_deadlock;
eb76b3fd 2886 break;
1da177e4 2887 default:
fd85b817 2888 dprintk("NFSD: nfsd4_lock: vfs_lock_file() failed! status %d\n",err);
eb76b3fd
AA
2889 status = nfserr_resource;
2890 break;
1da177e4 2891 }
1da177e4 2892out:
8a280510 2893 if (status && lock->lk_is_new && lock_sop)
f044ff83 2894 release_lockowner(lock_sop);
3a65588a
BF
2895 if (lock->lk_replay_owner) {
2896 nfs4_get_stateowner(lock->lk_replay_owner);
a4f1706a 2897 cstate->replay_owner = lock->lk_replay_owner;
f2327d9a 2898 }
1da177e4
LT
2899 nfs4_unlock_state();
2900 return status;
2901}
2902
55ef1274
BF
2903/*
2904 * The NFSv4 spec allows a client to do a LOCKT without holding an OPEN,
2905 * so we do a temporary open here just to get an open file to pass to
2906 * vfs_test_lock. (Arguably perhaps test_lock should be done with an
2907 * inode operation.)
2908 */
2909static int nfsd_test_lock(struct svc_rqst *rqstp, struct svc_fh *fhp, struct file_lock *lock)
2910{
2911 struct file *file;
2912 int err;
2913
2914 err = nfsd_open(rqstp, fhp, S_IFREG, NFSD_MAY_READ, &file);
2915 if (err)
2916 return err;
2917 err = vfs_test_lock(file, lock);
2918 nfsd_close(file);
2919 return err;
2920}
2921
1da177e4
LT
2922/*
2923 * LOCKT operation
2924 */
b37ad28b 2925__be32
ca364317
BF
2926nfsd4_lockt(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
2927 struct nfsd4_lockt *lockt)
1da177e4
LT
2928{
2929 struct inode *inode;
1da177e4 2930 struct file_lock file_lock;
fd85b817 2931 int error;
b37ad28b 2932 __be32 status;
1da177e4 2933
af558e33 2934 if (locks_in_grace())
1da177e4
LT
2935 return nfserr_grace;
2936
2937 if (check_lock_length(lockt->lt_offset, lockt->lt_length))
2938 return nfserr_inval;
2939
2940 lockt->lt_stateowner = NULL;
2941 nfs4_lock_state();
2942
2943 status = nfserr_stale_clientid;
849823c5 2944 if (STALE_CLIENTID(&lockt->lt_clientid))
1da177e4 2945 goto out;
1da177e4 2946
ca364317 2947 if ((status = fh_verify(rqstp, &cstate->current_fh, S_IFREG, 0))) {
849823c5 2948 dprintk("NFSD: nfsd4_lockt: fh_verify() failed!\n");
1da177e4
LT
2949 if (status == nfserr_symlink)
2950 status = nfserr_inval;
2951 goto out;
2952 }
2953
ca364317 2954 inode = cstate->current_fh.fh_dentry->d_inode;
1da177e4
LT
2955 locks_init_lock(&file_lock);
2956 switch (lockt->lt_type) {
2957 case NFS4_READ_LT:
2958 case NFS4_READW_LT:
2959 file_lock.fl_type = F_RDLCK;
2960 break;
2961 case NFS4_WRITE_LT:
2962 case NFS4_WRITEW_LT:
2963 file_lock.fl_type = F_WRLCK;
2964 break;
2965 default:
2fdada03 2966 dprintk("NFSD: nfs4_lockt: bad lock type!\n");
1da177e4
LT
2967 status = nfserr_inval;
2968 goto out;
2969 }
2970
2971 lockt->lt_stateowner = find_lockstateowner_str(inode,
2972 &lockt->lt_clientid, &lockt->lt_owner);
2973 if (lockt->lt_stateowner)
2974 file_lock.fl_owner = (fl_owner_t)lockt->lt_stateowner;
2975 file_lock.fl_pid = current->tgid;
2976 file_lock.fl_flags = FL_POSIX;
2977
2978 file_lock.fl_start = lockt->lt_offset;
87df4de8 2979 file_lock.fl_end = last_byte_offset(lockt->lt_offset, lockt->lt_length);
1da177e4
LT
2980
2981 nfs4_transform_lock_offset(&file_lock);
2982
1da177e4 2983 status = nfs_ok;
55ef1274 2984 error = nfsd_test_lock(rqstp, &cstate->current_fh, &file_lock);
fd85b817
ME
2985 if (error) {
2986 status = nfserrno(error);
2987 goto out;
2988 }
9d6a8c5c 2989 if (file_lock.fl_type != F_UNLCK) {
1da177e4 2990 status = nfserr_denied;
9d6a8c5c 2991 nfs4_set_lock_denied(&file_lock, &lockt->lt_denied);
1da177e4
LT
2992 }
2993out:
2994 nfs4_unlock_state();
2995 return status;
2996}
2997
b37ad28b 2998__be32
ca364317 2999nfsd4_locku(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
a4f1706a 3000 struct nfsd4_locku *locku)
1da177e4
LT
3001{
3002 struct nfs4_stateid *stp;
3003 struct file *filp = NULL;
3004 struct file_lock file_lock;
b37ad28b 3005 __be32 status;
b8dd7b9a 3006 int err;
1da177e4
LT
3007
3008 dprintk("NFSD: nfsd4_locku: start=%Ld length=%Ld\n",
3009 (long long) locku->lu_offset,
3010 (long long) locku->lu_length);
3011
3012 if (check_lock_length(locku->lu_offset, locku->lu_length))
3013 return nfserr_inval;
3014
3015 nfs4_lock_state();
3016
ca364317 3017 if ((status = nfs4_preprocess_seqid_op(&cstate->current_fh,
1da177e4
LT
3018 locku->lu_seqid,
3019 &locku->lu_stateid,
f3362737 3020 LOCK_STATE,
1da177e4
LT
3021 &locku->lu_stateowner, &stp, NULL)))
3022 goto out;
3023
3024 filp = stp->st_vfs_file;
3025 BUG_ON(!filp);
3026 locks_init_lock(&file_lock);
3027 file_lock.fl_type = F_UNLCK;
3028 file_lock.fl_owner = (fl_owner_t) locku->lu_stateowner;
3029 file_lock.fl_pid = current->tgid;
3030 file_lock.fl_file = filp;
3031 file_lock.fl_flags = FL_POSIX;
d5b9026a 3032 file_lock.fl_lmops = &nfsd_posix_mng_ops;
1da177e4
LT
3033 file_lock.fl_start = locku->lu_offset;
3034
87df4de8 3035 file_lock.fl_end = last_byte_offset(locku->lu_offset, locku->lu_length);
1da177e4
LT
3036 nfs4_transform_lock_offset(&file_lock);
3037
3038 /*
3039 * Try to unlock the file in the VFS.
3040 */
fd85b817 3041 err = vfs_lock_file(filp, F_SETLK, &file_lock, NULL);
b8dd7b9a 3042 if (err) {
fd85b817 3043 dprintk("NFSD: nfs4_locku: vfs_lock_file failed!\n");
1da177e4
LT
3044 goto out_nfserr;
3045 }
3046 /*
3047 * OK, unlock succeeded; the only thing left to do is update the stateid.
3048 */
3049 update_stateid(&stp->st_stateid);
3050 memcpy(&locku->lu_stateid, &stp->st_stateid, sizeof(stateid_t));
3051
3052out:
f2327d9a 3053 if (locku->lu_stateowner) {
1da177e4 3054 nfs4_get_stateowner(locku->lu_stateowner);
a4f1706a 3055 cstate->replay_owner = locku->lu_stateowner;
f2327d9a 3056 }
1da177e4
LT
3057 nfs4_unlock_state();
3058 return status;
3059
3060out_nfserr:
b8dd7b9a 3061 status = nfserrno(err);
1da177e4
LT
3062 goto out;
3063}
3064
3065/*
3066 * returns
3067 * 1: locks held by lockowner
3068 * 0: no locks held by lockowner
3069 */
3070static int
3071check_for_locks(struct file *filp, struct nfs4_stateowner *lowner)
3072{
3073 struct file_lock **flpp;
7eaa36e2 3074 struct inode *inode = filp->f_path.dentry->d_inode;
1da177e4
LT
3075 int status = 0;
3076
3077 lock_kernel();
3078 for (flpp = &inode->i_flock; *flpp != NULL; flpp = &(*flpp)->fl_next) {
796dadfd 3079 if ((*flpp)->fl_owner == (fl_owner_t)lowner) {
1da177e4
LT
3080 status = 1;
3081 goto out;
796dadfd 3082 }
1da177e4
LT
3083 }
3084out:
3085 unlock_kernel();
3086 return status;
3087}
3088
b37ad28b 3089__be32
b591480b
BF
3090nfsd4_release_lockowner(struct svc_rqst *rqstp,
3091 struct nfsd4_compound_state *cstate,
3092 struct nfsd4_release_lockowner *rlockowner)
1da177e4
LT
3093{
3094 clientid_t *clid = &rlockowner->rl_clientid;
3e9e3dbe
N
3095 struct nfs4_stateowner *sop;
3096 struct nfs4_stateid *stp;
1da177e4 3097 struct xdr_netobj *owner = &rlockowner->rl_owner;
3e9e3dbe
N
3098 struct list_head matches;
3099 int i;
b37ad28b 3100 __be32 status;
1da177e4
LT
3101
3102 dprintk("nfsd4_release_lockowner clientid: (%08x/%08x):\n",
3103 clid->cl_boot, clid->cl_id);
3104
3105 /* XXX check for lease expiration */
3106
3107 status = nfserr_stale_clientid;
849823c5 3108 if (STALE_CLIENTID(clid))
1da177e4 3109 return status;
1da177e4
LT
3110
3111 nfs4_lock_state();
3112
3e9e3dbe
N
3113 status = nfserr_locks_held;
3114 /* XXX: we're doing a linear search through all the lockowners.
3115 * Yipes! For now we'll just hope clients aren't really using
3116 * release_lockowner much, but eventually we have to fix these
3117 * data structures. */
3118 INIT_LIST_HEAD(&matches);
3119 for (i = 0; i < LOCK_HASH_SIZE; i++) {
3120 list_for_each_entry(sop, &lock_ownerid_hashtbl[i], so_idhash) {
599e0a22 3121 if (!same_owner_str(sop, owner, clid))
3e9e3dbe
N
3122 continue;
3123 list_for_each_entry(stp, &sop->so_stateids,
3124 st_perstateowner) {
3125 if (check_for_locks(stp->st_vfs_file, sop))
3126 goto out;
3127 /* Note: so_perclient unused for lockowners,
3128 * so it's OK to fool with here. */
3129 list_add(&sop->so_perclient, &matches);
3130 }
1da177e4 3131 }
3e9e3dbe
N
3132 }
3133 /* Clients probably won't expect us to return with some (but not all)
3134 * of the lockowner state released; so don't release any until all
3135 * have been checked. */
3136 status = nfs_ok;
0fa822e4
N
3137 while (!list_empty(&matches)) {
3138 sop = list_entry(matches.next, struct nfs4_stateowner,
3139 so_perclient);
3140 /* unhash_stateowner deletes so_perclient only
3141 * for openowners. */
3142 list_del(&sop->so_perclient);
f044ff83 3143 release_lockowner(sop);
1da177e4
LT
3144 }
3145out:
3146 nfs4_unlock_state();
3147 return status;
3148}
3149
3150static inline struct nfs4_client_reclaim *
a55370a3 3151alloc_reclaim(void)
1da177e4 3152{
a55370a3 3153 return kmalloc(sizeof(struct nfs4_client_reclaim), GFP_KERNEL);
1da177e4
LT
3154}
3155
c7b9a459
N
3156int
3157nfs4_has_reclaimed_state(const char *name)
3158{
3159 unsigned int strhashval = clientstr_hashval(name);
3160 struct nfs4_client *clp;
3161
3162 clp = find_confirmed_client_by_str(name, strhashval);
3163 return clp ? 1 : 0;
3164}
3165
1da177e4
LT
3166/*
3167 * failure => all reset bets are off, nfserr_no_grace...
3168 */
190e4fbf
N
3169int
3170nfs4_client_to_reclaim(const char *name)
1da177e4
LT
3171{
3172 unsigned int strhashval;
3173 struct nfs4_client_reclaim *crp = NULL;
3174
a55370a3
N
3175 dprintk("NFSD nfs4_client_to_reclaim NAME: %.*s\n", HEXDIR_LEN, name);
3176 crp = alloc_reclaim();
1da177e4
LT
3177 if (!crp)
3178 return 0;
a55370a3 3179 strhashval = clientstr_hashval(name);
1da177e4
LT
3180 INIT_LIST_HEAD(&crp->cr_strhash);
3181 list_add(&crp->cr_strhash, &reclaim_str_hashtbl[strhashval]);
a55370a3 3182 memcpy(crp->cr_recdir, name, HEXDIR_LEN);
1da177e4
LT
3183 reclaim_str_hashtbl_size++;
3184 return 1;
3185}
3186
3187static void
3188nfs4_release_reclaim(void)
3189{
3190 struct nfs4_client_reclaim *crp = NULL;
3191 int i;
3192
1da177e4
LT
3193 for (i = 0; i < CLIENT_HASH_SIZE; i++) {
3194 while (!list_empty(&reclaim_str_hashtbl[i])) {
3195 crp = list_entry(reclaim_str_hashtbl[i].next,
3196 struct nfs4_client_reclaim, cr_strhash);
3197 list_del(&crp->cr_strhash);
1da177e4
LT
3198 kfree(crp);
3199 reclaim_str_hashtbl_size--;
3200 }
3201 }
3202 BUG_ON(reclaim_str_hashtbl_size);
3203}
3204
3205/*
3206 * called from OPEN, CLAIM_PREVIOUS with a new clientid. */
fd39ca9a 3207static struct nfs4_client_reclaim *
1da177e4
LT
3208nfs4_find_reclaim_client(clientid_t *clid)
3209{
3210 unsigned int strhashval;
3211 struct nfs4_client *clp;
3212 struct nfs4_client_reclaim *crp = NULL;
3213
3214
3215 /* find clientid in conf_id_hashtbl */
3216 clp = find_confirmed_client(clid);
3217 if (clp == NULL)
3218 return NULL;
3219
a55370a3
N
3220 dprintk("NFSD: nfs4_find_reclaim_client for %.*s with recdir %s\n",
3221 clp->cl_name.len, clp->cl_name.data,
3222 clp->cl_recdir);
1da177e4
LT
3223
3224 /* find clp->cl_name in reclaim_str_hashtbl */
a55370a3 3225 strhashval = clientstr_hashval(clp->cl_recdir);
1da177e4 3226 list_for_each_entry(crp, &reclaim_str_hashtbl[strhashval], cr_strhash) {
a55370a3 3227 if (same_name(crp->cr_recdir, clp->cl_recdir)) {
1da177e4
LT
3228 return crp;
3229 }
3230 }
3231 return NULL;
3232}
3233
3234/*
3235* Called from OPEN. Look for clientid in reclaim list.
3236*/
b37ad28b 3237__be32
1da177e4
LT
3238nfs4_check_open_reclaim(clientid_t *clid)
3239{
dfc83565 3240 return nfs4_find_reclaim_client(clid) ? nfs_ok : nfserr_reclaim_bad;
1da177e4
LT
3241}
3242
ac4d8ff2 3243/* initialization to perform at module load time: */
1da177e4 3244
e8ff2a84 3245int
ac4d8ff2 3246nfs4_state_init(void)
1da177e4 3247{
e8ff2a84 3248 int i, status;
1da177e4 3249
e8ff2a84
BF
3250 status = nfsd4_init_slabs();
3251 if (status)
3252 return status;
1da177e4
LT
3253 for (i = 0; i < CLIENT_HASH_SIZE; i++) {
3254 INIT_LIST_HEAD(&conf_id_hashtbl[i]);
3255 INIT_LIST_HEAD(&conf_str_hashtbl[i]);
3256 INIT_LIST_HEAD(&unconf_str_hashtbl[i]);
3257 INIT_LIST_HEAD(&unconf_id_hashtbl[i]);
3258 }
5282fd72
ME
3259 for (i = 0; i < SESSION_HASH_SIZE; i++)
3260 INIT_LIST_HEAD(&sessionid_hashtbl[i]);
1da177e4
LT
3261 for (i = 0; i < FILE_HASH_SIZE; i++) {
3262 INIT_LIST_HEAD(&file_hashtbl[i]);
3263 }
3264 for (i = 0; i < OWNER_HASH_SIZE; i++) {
3265 INIT_LIST_HEAD(&ownerstr_hashtbl[i]);
3266 INIT_LIST_HEAD(&ownerid_hashtbl[i]);
3267 }
3268 for (i = 0; i < STATEID_HASH_SIZE; i++) {
3269 INIT_LIST_HEAD(&stateid_hashtbl[i]);
3270 INIT_LIST_HEAD(&lockstateid_hashtbl[i]);
3271 }
3272 for (i = 0; i < LOCK_HASH_SIZE; i++) {
3273 INIT_LIST_HEAD(&lock_ownerid_hashtbl[i]);
3274 INIT_LIST_HEAD(&lock_ownerstr_hashtbl[i]);
3275 }
1da177e4 3276 memset(&onestateid, ~0, sizeof(stateid_t));
1da177e4
LT
3277 INIT_LIST_HEAD(&close_lru);
3278 INIT_LIST_HEAD(&client_lru);
3279 INIT_LIST_HEAD(&del_recall_lru);
ac4d8ff2
N
3280 for (i = 0; i < CLIENT_HASH_SIZE; i++)
3281 INIT_LIST_HEAD(&reclaim_str_hashtbl[i]);
3282 reclaim_str_hashtbl_size = 0;
e8ff2a84 3283 return 0;
ac4d8ff2
N
3284}
3285
190e4fbf
N
3286static void
3287nfsd4_load_reboot_recovery_data(void)
3288{
3289 int status;
3290
0964a3d3
N
3291 nfs4_lock_state();
3292 nfsd4_init_recdir(user_recovery_dirname);
190e4fbf 3293 status = nfsd4_recdir_load();
0964a3d3 3294 nfs4_unlock_state();
190e4fbf
N
3295 if (status)
3296 printk("NFSD: Failure reading reboot recovery data\n");
3297}
3298
9a8db97e
ME
3299unsigned long
3300get_nfs4_grace_period(void)
3301{
3302 return max(user_lease_time, lease_time) * HZ;
3303}
3304
c2f1a551
MS
3305/*
3306 * Since the lifetime of a delegation isn't limited to that of an open, a
3307 * client may quite reasonably hang on to a delegation as long as it has
3308 * the inode cached. This becomes an obvious problem the first time a
3309 * client's inode cache approaches the size of the server's total memory.
3310 *
3311 * For now we avoid this problem by imposing a hard limit on the number
3312 * of delegations, which varies according to the server's memory size.
3313 */
3314static void
3315set_max_delegations(void)
3316{
3317 /*
3318 * Allow at most 4 delegations per megabyte of RAM. Quick
3319 * estimates suggest that in the worst case (where every delegation
3320 * is for a different inode), a delegation could take about 1.5K,
3321 * giving a worst case usage of about 6% of memory.
3322 */
3323 max_delegations = nr_free_buffer_pages() >> (20 - 2 - PAGE_SHIFT);
3324}
3325
ac4d8ff2
N
3326/* initialization to perform when the nfsd service is started: */
3327
3328static void
3329__nfs4_state_start(void)
3330{
9a8db97e 3331 unsigned long grace_time;
ac4d8ff2 3332
1da177e4 3333 boot_time = get_seconds();
af558e33 3334 grace_time = get_nfs4_grace_period();
d99a05ad 3335 lease_time = user_lease_time;
af558e33 3336 locks_start_grace(&nfsd4_manager);
9a8db97e
ME
3337 printk(KERN_INFO "NFSD: starting %ld-second grace period\n",
3338 grace_time/HZ);
58da282b 3339 laundry_wq = create_singlethread_workqueue("nfsd4");
9a8db97e 3340 queue_delayed_work(laundry_wq, &laundromat_work, grace_time);
c2f1a551 3341 set_max_delegations();
1da177e4
LT
3342}
3343
e8ff2a84 3344void
76a3550e 3345nfs4_state_start(void)
1da177e4 3346{
1da177e4 3347 if (nfs4_init)
e8ff2a84 3348 return;
190e4fbf 3349 nfsd4_load_reboot_recovery_data();
76a3550e 3350 __nfs4_state_start();
1da177e4 3351 nfs4_init = 1;
e8ff2a84 3352 return;
1da177e4
LT
3353}
3354
1da177e4
LT
3355time_t
3356nfs4_lease_time(void)
3357{
3358 return lease_time;
3359}
3360
3361static void
3362__nfs4_state_shutdown(void)
3363{
3364 int i;
3365 struct nfs4_client *clp = NULL;
3366 struct nfs4_delegation *dp = NULL;
1da177e4
LT
3367 struct list_head *pos, *next, reaplist;
3368
1da177e4
LT
3369 for (i = 0; i < CLIENT_HASH_SIZE; i++) {
3370 while (!list_empty(&conf_id_hashtbl[i])) {
3371 clp = list_entry(conf_id_hashtbl[i].next, struct nfs4_client, cl_idhash);
3372 expire_client(clp);
3373 }
3374 while (!list_empty(&unconf_str_hashtbl[i])) {
3375 clp = list_entry(unconf_str_hashtbl[i].next, struct nfs4_client, cl_strhash);
3376 expire_client(clp);
3377 }
3378 }
3379 INIT_LIST_HEAD(&reaplist);
3380 spin_lock(&recall_lock);
3381 list_for_each_safe(pos, next, &del_recall_lru) {
3382 dp = list_entry (pos, struct nfs4_delegation, dl_recall_lru);
3383 list_move(&dp->dl_recall_lru, &reaplist);
3384 }
3385 spin_unlock(&recall_lock);
3386 list_for_each_safe(pos, next, &reaplist) {
3387 dp = list_entry (pos, struct nfs4_delegation, dl_recall_lru);
3388 list_del_init(&dp->dl_recall_lru);
3389 unhash_delegation(dp);
3390 }
3391
190e4fbf 3392 nfsd4_shutdown_recdir();
1da177e4 3393 nfs4_init = 0;
1da177e4
LT
3394}
3395
3396void
3397nfs4_state_shutdown(void)
3398{
5e8d5c29
N
3399 cancel_rearming_delayed_workqueue(laundry_wq, &laundromat_work);
3400 destroy_workqueue(laundry_wq);
2c5e7615 3401 locks_end_grace(&nfsd4_manager);
1da177e4
LT
3402 nfs4_lock_state();
3403 nfs4_release_reclaim();
3404 __nfs4_state_shutdown();
1da177e4
LT
3405 nfs4_unlock_state();
3406}
3407
3dd98a3b
JL
3408/*
3409 * user_recovery_dirname is protected by the nfsd_mutex since it's only
3410 * accessed when nfsd is starting.
3411 */
0964a3d3
N
3412static void
3413nfs4_set_recdir(char *recdir)
3414{
0964a3d3 3415 strcpy(user_recovery_dirname, recdir);
0964a3d3
N
3416}
3417
3418/*
3419 * Change the NFSv4 recovery directory to recdir.
3420 */
3421int
3422nfs4_reset_recoverydir(char *recdir)
3423{
3424 int status;
a63bb996 3425 struct path path;
0964a3d3 3426
a63bb996 3427 status = kern_path(recdir, LOOKUP_FOLLOW, &path);
0964a3d3
N
3428 if (status)
3429 return status;
3430 status = -ENOTDIR;
a63bb996 3431 if (S_ISDIR(path.dentry->d_inode->i_mode)) {
0964a3d3
N
3432 nfs4_set_recdir(recdir);
3433 status = 0;
3434 }
a63bb996 3435 path_put(&path);
0964a3d3
N
3436 return status;
3437}
3438
3dd98a3b
JL
3439char *
3440nfs4_recoverydir(void)
3441{
3442 return user_recovery_dirname;
3443}
3444
1da177e4
LT
3445/*
3446 * Called when leasetime is changed.
3447 *
d99a05ad
N
3448 * The only way the protocol gives us to handle on-the-fly lease changes is to
3449 * simulate a reboot. Instead of doing that, we just wait till the next time
3450 * we start to register any changes in lease time. If the administrator
3451 * really wants to change the lease time *now*, they can go ahead and bring
3452 * nfsd down and then back up again after changing the lease time.
3dd98a3b
JL
3453 *
3454 * user_lease_time is protected by nfsd_mutex since it's only really accessed
3455 * when nfsd is starting
1da177e4
LT
3456 */
3457void
3458nfs4_reset_lease(time_t leasetime)
3459{
d99a05ad 3460 user_lease_time = leasetime;
1da177e4 3461}