Skip to content

Commit af558e3

Browse files
author
J. Bruce Fields
committed
nfsd: common grace period control
Rewrite grace period code to unify management of grace period across lockd and nfsd. The current code has lockd and nfsd cooperate to compute a grace period which is satisfactory to them both, and then individually enforce it. This creates a slight race condition, since the enforcement is not coordinated. It's also more complicated than necessary. Here instead we have lockd and nfsd each inform common code when they enter the grace period, and when they're ready to leave the grace period, and allow normal locking only after both of them are ready to leave. We also expect the locks_start_grace()/locks_end_grace() interface here to be simpler to build on for future cluster/high-availability work, which may require (for example) putting individual filesystems into grace, or enforcing grace periods across multiple cluster nodes. Signed-off-by: J. Bruce Fields <bfields@citi.umich.edu>
1 parent d5b337b commit af558e3

10 files changed

Lines changed: 104 additions & 61 deletions

File tree

‎fs/lockd/Makefile‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,6 @@
55
obj-$(CONFIG_LOCKD) += lockd.o
66

77
lockd-objs-y := clntlock.o clntproc.o host.o svc.o svclock.o svcshare.o \
8-
svcproc.o svcsubs.o mon.o xdr.o
8+
svcproc.o svcsubs.o mon.o xdr.o grace.o
99
lockd-objs-$(CONFIG_LOCKD_V4) += xdr4.o svc4proc.o
1010
lockd-objs := $(lockd-objs-y)

‎fs/lockd/grace.c‎

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
/*
2+
* Common code for control of lockd and nfsv4 grace periods.
3+
*/
4+
5+
#include <linux/module.h>
6+
#include <linux/lockd/bind.h>
7+
8+
static LIST_HEAD(grace_list);
9+
static DEFINE_SPINLOCK(grace_lock);
10+
11+
/**
12+
* locks_start_grace
13+
* @lm: who this grace period is for
14+
*
15+
* A grace period is a period during which locks should not be given
16+
* out. Currently grace periods are only enforced by the two lock
17+
* managers (lockd and nfsd), using the locks_in_grace() function to
18+
* check when they are in a grace period.
19+
*
20+
* This function is called to start a grace period.
21+
*/
22+
void locks_start_grace(struct lock_manager *lm)
23+
{
24+
spin_lock(&grace_lock);
25+
list_add(&lm->list, &grace_list);
26+
spin_unlock(&grace_lock);
27+
}
28+
EXPORT_SYMBOL_GPL(locks_start_grace);
29+
30+
/**
31+
* locks_end_grace
32+
* @lm: who this grace period is for
33+
*
34+
* Call this function to state that the given lock manager is ready to
35+
* resume regular locking. The grace period will not end until all lock
36+
* managers that called locks_start_grace() also call locks_end_grace().
37+
* Note that callers count on it being safe to call this more than once,
38+
* and the second call should be a no-op.
39+
*/
40+
void locks_end_grace(struct lock_manager *lm)
41+
{
42+
spin_lock(&grace_lock);
43+
list_del_init(&lm->list);
44+
spin_unlock(&grace_lock);
45+
}
46+
EXPORT_SYMBOL_GPL(locks_end_grace);
47+
48+
/**
49+
* locks_in_grace
50+
*
51+
* Lock managers call this function to determine when it is OK for them
52+
* to answer ordinary lock requests, and when they should accept only
53+
* lock reclaims.
54+
*/
55+
int locks_in_grace(void)
56+
{
57+
return !list_empty(&grace_list);
58+
}
59+
EXPORT_SYMBOL_GPL(locks_in_grace);

‎fs/lockd/svc.c‎

Lines changed: 5 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,6 @@ static DEFINE_MUTEX(nlmsvc_mutex);
5151
static unsigned int nlmsvc_users;
5252
static struct task_struct *nlmsvc_task;
5353
static struct svc_rqst *nlmsvc_rqst;
54-
int nlmsvc_grace_period;
5554
unsigned long nlmsvc_timeout;
5655

5756
/*
@@ -85,30 +84,21 @@ static unsigned long get_lockd_grace_period(void)
8584
return nlm_timeout * 5 * HZ;
8685
}
8786

88-
unsigned long get_nfs_grace_period(void)
89-
{
90-
unsigned long lockdgrace = get_lockd_grace_period();
91-
unsigned long nfsdgrace = 0;
92-
93-
if (nlmsvc_ops)
94-
nfsdgrace = nlmsvc_ops->get_grace_period();
95-
96-
return max(lockdgrace, nfsdgrace);
97-
}
98-
EXPORT_SYMBOL(get_nfs_grace_period);
87+
static struct lock_manager lockd_manager = {
88+
};
9989

10090
static void grace_ender(struct work_struct *not_used)
10191
{
102-
nlmsvc_grace_period = 0;
92+
locks_end_grace(&lockd_manager);
10393
}
10494

10595
static DECLARE_DELAYED_WORK(grace_period_end, grace_ender);
10696

10797
static void set_grace_period(void)
10898
{
109-
unsigned long grace_period = get_nfs_grace_period() + jiffies;
99+
unsigned long grace_period = get_lockd_grace_period();
110100

111-
nlmsvc_grace_period = 1;
101+
locks_start_grace(&lockd_manager);
112102
cancel_delayed_work_sync(&grace_period_end);
113103
schedule_delayed_work(&grace_period_end, grace_period);
114104
}

‎fs/lockd/svc4proc.c‎

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ nlm4svc_proc_test(struct svc_rqst *rqstp, struct nlm_args *argp,
8989
resp->cookie = argp->cookie;
9090

9191
/* Don't accept test requests during grace period */
92-
if (nlmsvc_grace_period) {
92+
if (locks_in_grace()) {
9393
resp->status = nlm_lck_denied_grace_period;
9494
return rc;
9595
}
@@ -123,7 +123,7 @@ nlm4svc_proc_lock(struct svc_rqst *rqstp, struct nlm_args *argp,
123123
resp->cookie = argp->cookie;
124124

125125
/* Don't accept new lock requests during grace period */
126-
if (nlmsvc_grace_period && !argp->reclaim) {
126+
if (locks_in_grace() && !argp->reclaim) {
127127
resp->status = nlm_lck_denied_grace_period;
128128
return rc;
129129
}
@@ -169,7 +169,7 @@ nlm4svc_proc_cancel(struct svc_rqst *rqstp, struct nlm_args *argp,
169169
resp->cookie = argp->cookie;
170170

171171
/* Don't accept requests during grace period */
172-
if (nlmsvc_grace_period) {
172+
if (locks_in_grace()) {
173173
resp->status = nlm_lck_denied_grace_period;
174174
return rpc_success;
175175
}
@@ -202,7 +202,7 @@ nlm4svc_proc_unlock(struct svc_rqst *rqstp, struct nlm_args *argp,
202202
resp->cookie = argp->cookie;
203203

204204
/* Don't accept new lock requests during grace period */
205-
if (nlmsvc_grace_period) {
205+
if (locks_in_grace()) {
206206
resp->status = nlm_lck_denied_grace_period;
207207
return rpc_success;
208208
}
@@ -341,7 +341,7 @@ nlm4svc_proc_share(struct svc_rqst *rqstp, struct nlm_args *argp,
341341
resp->cookie = argp->cookie;
342342

343343
/* Don't accept new lock requests during grace period */
344-
if (nlmsvc_grace_period && !argp->reclaim) {
344+
if (locks_in_grace() && !argp->reclaim) {
345345
resp->status = nlm_lck_denied_grace_period;
346346
return rpc_success;
347347
}
@@ -374,7 +374,7 @@ nlm4svc_proc_unshare(struct svc_rqst *rqstp, struct nlm_args *argp,
374374
resp->cookie = argp->cookie;
375375

376376
/* Don't accept requests during grace period */
377-
if (nlmsvc_grace_period) {
377+
if (locks_in_grace()) {
378378
resp->status = nlm_lck_denied_grace_period;
379379
return rpc_success;
380380
}

‎fs/lockd/svcproc.c‎

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,7 @@ nlmsvc_proc_test(struct svc_rqst *rqstp, struct nlm_args *argp,
118118
resp->cookie = argp->cookie;
119119

120120
/* Don't accept test requests during grace period */
121-
if (nlmsvc_grace_period) {
121+
if (locks_in_grace()) {
122122
resp->status = nlm_lck_denied_grace_period;
123123
return rc;
124124
}
@@ -153,7 +153,7 @@ nlmsvc_proc_lock(struct svc_rqst *rqstp, struct nlm_args *argp,
153153
resp->cookie = argp->cookie;
154154

155155
/* Don't accept new lock requests during grace period */
156-
if (nlmsvc_grace_period && !argp->reclaim) {
156+
if (locks_in_grace() && !argp->reclaim) {
157157
resp->status = nlm_lck_denied_grace_period;
158158
return rc;
159159
}
@@ -199,7 +199,7 @@ nlmsvc_proc_cancel(struct svc_rqst *rqstp, struct nlm_args *argp,
199199
resp->cookie = argp->cookie;
200200

201201
/* Don't accept requests during grace period */
202-
if (nlmsvc_grace_period) {
202+
if (locks_in_grace()) {
203203
resp->status = nlm_lck_denied_grace_period;
204204
return rpc_success;
205205
}
@@ -232,7 +232,7 @@ nlmsvc_proc_unlock(struct svc_rqst *rqstp, struct nlm_args *argp,
232232
resp->cookie = argp->cookie;
233233

234234
/* Don't accept new lock requests during grace period */
235-
if (nlmsvc_grace_period) {
235+
if (locks_in_grace()) {
236236
resp->status = nlm_lck_denied_grace_period;
237237
return rpc_success;
238238
}
@@ -373,7 +373,7 @@ nlmsvc_proc_share(struct svc_rqst *rqstp, struct nlm_args *argp,
373373
resp->cookie = argp->cookie;
374374

375375
/* Don't accept new lock requests during grace period */
376-
if (nlmsvc_grace_period && !argp->reclaim) {
376+
if (locks_in_grace() && !argp->reclaim) {
377377
resp->status = nlm_lck_denied_grace_period;
378378
return rpc_success;
379379
}
@@ -406,7 +406,7 @@ nlmsvc_proc_unshare(struct svc_rqst *rqstp, struct nlm_args *argp,
406406
resp->cookie = argp->cookie;
407407

408408
/* Don't accept requests during grace period */
409-
if (nlmsvc_grace_period) {
409+
if (locks_in_grace()) {
410410
resp->status = nlm_lck_denied_grace_period;
411411
return rpc_success;
412412
}

‎fs/nfsd/lockd.c‎

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,6 @@ nlm_fclose(struct file *filp)
7070
static struct nlmsvc_binding nfsd_nlm_ops = {
7171
.fopen = nlm_fopen, /* open file for locking */
7272
.fclose = nlm_fclose, /* close file */
73-
.get_grace_period = get_nfs4_grace_period,
7473
};
7574

7675
void

‎fs/nfsd/nfs4proc.c‎

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -201,10 +201,10 @@ nfsd4_open(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
201201
/* Openowner is now set, so sequence id will get bumped. Now we need
202202
* these checks before we do any creates: */
203203
status = nfserr_grace;
204-
if (nfs4_in_grace() && open->op_claim_type != NFS4_OPEN_CLAIM_PREVIOUS)
204+
if (locks_in_grace() && open->op_claim_type != NFS4_OPEN_CLAIM_PREVIOUS)
205205
goto out;
206206
status = nfserr_no_grace;
207-
if (!nfs4_in_grace() && open->op_claim_type == NFS4_OPEN_CLAIM_PREVIOUS)
207+
if (!locks_in_grace() && open->op_claim_type == NFS4_OPEN_CLAIM_PREVIOUS)
208208
goto out;
209209

210210
switch (open->op_claim_type) {
@@ -575,7 +575,7 @@ nfsd4_remove(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
575575
{
576576
__be32 status;
577577

578-
if (nfs4_in_grace())
578+
if (locks_in_grace())
579579
return nfserr_grace;
580580
status = nfsd_unlink(rqstp, &cstate->current_fh, 0,
581581
remove->rm_name, remove->rm_namelen);
@@ -596,7 +596,7 @@ nfsd4_rename(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
596596

597597
if (!cstate->save_fh.fh_dentry)
598598
return status;
599-
if (nfs4_in_grace() && !(cstate->save_fh.fh_export->ex_flags
599+
if (locks_in_grace() && !(cstate->save_fh.fh_export->ex_flags
600600
& NFSEXP_NOSUBTREECHECK))
601601
return nfserr_grace;
602602
status = nfsd_rename(rqstp, &cstate->save_fh, rename->rn_sname,

‎fs/nfsd/nfs4state.c‎

Lines changed: 15 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,6 @@
6161
static time_t lease_time = 90; /* default lease time */
6262
static time_t user_lease_time = 90;
6363
static time_t boot_time;
64-
static int in_grace = 1;
6564
static u32 current_ownerid = 1;
6665
static u32 current_fileid = 1;
6766
static u32 current_delegid = 1;
@@ -1640,7 +1639,7 @@ nfs4_open_delegation(struct svc_fh *fh, struct nfsd4_open *open, struct nfs4_sta
16401639
case NFS4_OPEN_CLAIM_NULL:
16411640
/* Let's not give out any delegations till everyone's
16421641
* had the chance to reclaim theirs.... */
1643-
if (nfs4_in_grace())
1642+
if (locks_in_grace())
16441643
goto out;
16451644
if (!atomic_read(&cb->cb_set) || !sop->so_confirmed)
16461645
goto out;
@@ -1816,12 +1815,15 @@ nfsd4_renew(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
18161815
return status;
18171816
}
18181817

1818+
struct lock_manager nfsd4_manager = {
1819+
};
1820+
18191821
static void
1820-
end_grace(void)
1822+
nfsd4_end_grace(void)
18211823
{
18221824
dprintk("NFSD: end of grace period\n");
18231825
nfsd4_recdir_purge_old();
1824-
in_grace = 0;
1826+
locks_end_grace(&nfsd4_manager);
18251827
}
18261828

18271829
static time_t
@@ -1838,8 +1840,8 @@ nfs4_laundromat(void)
18381840
nfs4_lock_state();
18391841

18401842
dprintk("NFSD: laundromat service - starting\n");
1841-
if (in_grace)
1842-
end_grace();
1843+
if (locks_in_grace())
1844+
nfsd4_end_grace();
18431845
list_for_each_safe(pos, next, &client_lru) {
18441846
clp = list_entry(pos, struct nfs4_client, cl_lru);
18451847
if (time_after((unsigned long)clp->cl_time, (unsigned long)cutoff)) {
@@ -1974,7 +1976,7 @@ check_special_stateids(svc_fh *current_fh, stateid_t *stateid, int flags)
19741976
return nfserr_bad_stateid;
19751977
else if (ONE_STATEID(stateid) && (flags & RD_STATE))
19761978
return nfs_ok;
1977-
else if (nfs4_in_grace()) {
1979+
else if (locks_in_grace()) {
19781980
/* Answer in remaining cases depends on existance of
19791981
* conflicting state; so we must wait out the grace period. */
19801982
return nfserr_grace;
@@ -1993,7 +1995,7 @@ check_special_stateids(svc_fh *current_fh, stateid_t *stateid, int flags)
19931995
static inline int
19941996
io_during_grace_disallowed(struct inode *inode, int flags)
19951997
{
1996-
return nfs4_in_grace() && (flags & (RD_STATE | WR_STATE))
1998+
return locks_in_grace() && (flags & (RD_STATE | WR_STATE))
19971999
&& mandatory_lock(inode);
19982000
}
19992001

@@ -2693,10 +2695,10 @@ nfsd4_lock(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
26932695
filp = lock_stp->st_vfs_file;
26942696

26952697
status = nfserr_grace;
2696-
if (nfs4_in_grace() && !lock->lk_reclaim)
2698+
if (locks_in_grace() && !lock->lk_reclaim)
26972699
goto out;
26982700
status = nfserr_no_grace;
2699-
if (!nfs4_in_grace() && lock->lk_reclaim)
2701+
if (!locks_in_grace() && lock->lk_reclaim)
27002702
goto out;
27012703

27022704
locks_init_lock(&file_lock);
@@ -2779,7 +2781,7 @@ nfsd4_lockt(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
27792781
int error;
27802782
__be32 status;
27812783

2782-
if (nfs4_in_grace())
2784+
if (locks_in_grace())
27832785
return nfserr_grace;
27842786

27852787
if (check_lock_length(lockt->lt_offset, lockt->lt_length))
@@ -3192,9 +3194,9 @@ __nfs4_state_start(void)
31923194
unsigned long grace_time;
31933195

31943196
boot_time = get_seconds();
3195-
grace_time = get_nfs_grace_period();
3197+
grace_time = get_nfs4_grace_period();
31963198
lease_time = user_lease_time;
3197-
in_grace = 1;
3199+
locks_start_grace(&nfsd4_manager);
31983200
printk(KERN_INFO "NFSD: starting %ld-second grace period\n",
31993201
grace_time/HZ);
32003202
laundry_wq = create_singlethread_workqueue("nfsd4");
@@ -3213,12 +3215,6 @@ nfs4_state_start(void)
32133215
return;
32143216
}
32153217

3216-
int
3217-
nfs4_in_grace(void)
3218-
{
3219-
return in_grace;
3220-
}
3221-
32223218
time_t
32233219
nfs4_lease_time(void)
32243220
{

‎include/linux/fs.h‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -942,6 +942,14 @@ struct lock_manager_operations {
942942
int (*fl_change)(struct file_lock **, int);
943943
};
944944

945+
struct lock_manager {
946+
struct list_head list;
947+
};
948+
949+
void locks_start_grace(struct lock_manager *);
950+
void locks_end_grace(struct lock_manager *);
951+
int locks_in_grace(void);
952+
945953
/* that will die - we need it for nfs_lock_info */
946954
#include <linux/nfs_fs_i.h>
947955

0 commit comments

Comments
 (0)