Skip to content

Commit 47e9881

Browse files
Mike SnitzerAnna Schumaker
authored andcommitted
nfsd: add nfsd_serv_try_get and nfsd_serv_put
Introduce nfsd_serv_try_get and nfsd_serv_put and update the nfsd code to prevent nfsd_destroy_serv from destroying nn->nfsd_serv until any caller of nfsd_serv_try_get releases their reference using nfsd_serv_put. A percpu_ref is used to implement the interlock between nfsd_destroy_serv and any caller of nfsd_serv_try_get. This interlock is needed to properly wait for the completion of client initiated localio calls to nfsd (that are _not_ in the context of nfsd). Signed-off-by: Mike Snitzer <[email protected]> Reviewed-by: Chuck Lever <[email protected]> Reviewed-by: Jeff Layton <[email protected]> Reviewed-by: NeilBrown <[email protected]> Signed-off-by: Anna Schumaker <[email protected]>
1 parent c63f0e4 commit 47e9881

File tree

2 files changed

+50
-1
lines changed

2 files changed

+50
-1
lines changed

fs/nfsd/netns.h

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
#include <linux/filelock.h>
1414
#include <linux/nfs4.h>
1515
#include <linux/percpu_counter.h>
16+
#include <linux/percpu-refcount.h>
1617
#include <linux/siphash.h>
1718
#include <linux/sunrpc/stats.h>
1819

@@ -139,7 +140,9 @@ struct nfsd_net {
139140

140141
struct svc_info nfsd_info;
141142
#define nfsd_serv nfsd_info.serv
142-
143+
struct percpu_ref nfsd_serv_ref;
144+
struct completion nfsd_serv_confirm_done;
145+
struct completion nfsd_serv_free_done;
143146

144147
/*
145148
* clientid and stateid data for construction of net unique COPY
@@ -222,6 +225,9 @@ struct nfsd_net {
222225
extern bool nfsd_support_version(int vers);
223226
extern unsigned int nfsd_net_id;
224227

228+
bool nfsd_serv_try_get(struct net *net);
229+
void nfsd_serv_put(struct net *net);
230+
225231
void nfsd_copy_write_verifier(__be32 verf[2], struct nfsd_net *nn);
226232
void nfsd_reset_write_verifier(struct nfsd_net *nn);
227233
#endif /* __NFSD_NETNS_H__ */

fs/nfsd/nfssvc.c

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -193,6 +193,34 @@ int nfsd_minorversion(struct nfsd_net *nn, u32 minorversion, enum vers_op change
193193
return 0;
194194
}
195195

196+
bool nfsd_serv_try_get(struct net *net)
197+
{
198+
struct nfsd_net *nn = net_generic(net, nfsd_net_id);
199+
200+
return (nn && percpu_ref_tryget_live(&nn->nfsd_serv_ref));
201+
}
202+
203+
void nfsd_serv_put(struct net *net)
204+
{
205+
struct nfsd_net *nn = net_generic(net, nfsd_net_id);
206+
207+
percpu_ref_put(&nn->nfsd_serv_ref);
208+
}
209+
210+
static void nfsd_serv_done(struct percpu_ref *ref)
211+
{
212+
struct nfsd_net *nn = container_of(ref, struct nfsd_net, nfsd_serv_ref);
213+
214+
complete(&nn->nfsd_serv_confirm_done);
215+
}
216+
217+
static void nfsd_serv_free(struct percpu_ref *ref)
218+
{
219+
struct nfsd_net *nn = container_of(ref, struct nfsd_net, nfsd_serv_ref);
220+
221+
complete(&nn->nfsd_serv_free_done);
222+
}
223+
196224
/*
197225
* Maximum number of nfsd processes
198226
*/
@@ -392,6 +420,7 @@ static void nfsd_shutdown_net(struct net *net)
392420
lockd_down(net);
393421
nn->lockd_up = false;
394422
}
423+
percpu_ref_exit(&nn->nfsd_serv_ref);
395424
nn->nfsd_net_up = false;
396425
nfsd_shutdown_generic();
397426
}
@@ -471,6 +500,13 @@ void nfsd_destroy_serv(struct net *net)
471500
struct nfsd_net *nn = net_generic(net, nfsd_net_id);
472501
struct svc_serv *serv = nn->nfsd_serv;
473502

503+
lockdep_assert_held(&nfsd_mutex);
504+
505+
percpu_ref_kill_and_confirm(&nn->nfsd_serv_ref, nfsd_serv_done);
506+
wait_for_completion(&nn->nfsd_serv_confirm_done);
507+
wait_for_completion(&nn->nfsd_serv_free_done);
508+
/* percpu_ref_exit is called in nfsd_shutdown_net */
509+
474510
spin_lock(&nfsd_notifier_lock);
475511
nn->nfsd_serv = NULL;
476512
spin_unlock(&nfsd_notifier_lock);
@@ -595,6 +631,13 @@ int nfsd_create_serv(struct net *net)
595631
if (nn->nfsd_serv)
596632
return 0;
597633

634+
error = percpu_ref_init(&nn->nfsd_serv_ref, nfsd_serv_free,
635+
0, GFP_KERNEL);
636+
if (error)
637+
return error;
638+
init_completion(&nn->nfsd_serv_free_done);
639+
init_completion(&nn->nfsd_serv_confirm_done);
640+
598641
if (nfsd_max_blksize == 0)
599642
nfsd_max_blksize = nfsd_get_default_max_blksize();
600643
nfsd_reset_versions(nn);

0 commit comments

Comments
 (0)