Skip to content

Commit 1c72511

Browse files
bcodding-rhTrond Myklebust
authored andcommitted
NFS: add superblock sysfs entries
Create a sysfs directory for each mount that corresponds to the mount's nfs_server struct. As the mount is being constructed, use the name "server-n", but rename it to the "MAJOR:MINOR" of the mount after assigning a device_id. The rename approach allows us to populate the mount's directory with links to the various rpc_client objects during the mount's construction. The naming convention (MAJOR:MINOR) can be used to reference a particular NFS mount's sysfs tree. Signed-off-by: Benjamin Coddington <bcodding@redhat.com> Signed-off-by: Trond Myklebust <trond.myklebust@hammerspace.com>
1 parent e96f926 commit 1c72511

File tree

6 files changed

+90
-1
lines changed

6 files changed

+90
-1
lines changed

fs/nfs/client.c

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -698,6 +698,7 @@ static int nfs_init_server(struct nfs_server *server,
698698
return PTR_ERR(clp);
699699

700700
server->nfs_client = clp;
701+
nfs_sysfs_add_server(server);
701702

702703
/* Initialise the client representation from the mount data */
703704
server->flags = ctx->flags;
@@ -952,6 +953,8 @@ void nfs_server_remove_lists(struct nfs_server *server)
952953
}
953954
EXPORT_SYMBOL_GPL(nfs_server_remove_lists);
954955

956+
static DEFINE_IDA(s_sysfs_ids);
957+
955958
/*
956959
* Allocate and initialise a server record
957960
*/
@@ -963,6 +966,12 @@ struct nfs_server *nfs_alloc_server(void)
963966
if (!server)
964967
return NULL;
965968

969+
server->s_sysfs_id = ida_alloc(&s_sysfs_ids, GFP_KERNEL);
970+
if (server->s_sysfs_id < 0) {
971+
kfree(server);
972+
return NULL;
973+
}
974+
966975
server->client = server->client_acl = ERR_PTR(-EINVAL);
967976

968977
/* Zero out the NFS state stuff */
@@ -1009,6 +1018,10 @@ void nfs_free_server(struct nfs_server *server)
10091018

10101019
nfs_put_client(server->nfs_client);
10111020

1021+
nfs_sysfs_remove_server(server);
1022+
kobject_put(&server->kobj);
1023+
ida_free(&s_sysfs_ids, server->s_sysfs_id);
1024+
10121025
ida_destroy(&server->lockowner_id);
10131026
ida_destroy(&server->openowner_id);
10141027
nfs_free_iostats(server->io_stats);
@@ -1110,6 +1123,8 @@ struct nfs_server *nfs_clone_server(struct nfs_server *source,
11101123

11111124
server->fsid = fattr->fsid;
11121125

1126+
nfs_sysfs_add_server(server);
1127+
11131128
error = nfs_init_server_rpcclient(server,
11141129
source->client->cl_timeout,
11151130
flavor);
@@ -1393,6 +1408,7 @@ int __init nfs_fs_proc_init(void)
13931408
void nfs_fs_proc_exit(void)
13941409
{
13951410
remove_proc_subtree("fs/nfsfs", NULL);
1411+
ida_destroy(&s_sysfs_ids);
13961412
}
13971413

13981414
#endif /* CONFIG_PROC_FS */

fs/nfs/nfs4client.c

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
#include "nfs4idmap.h"
1919
#include "pnfs.h"
2020
#include "netns.h"
21+
#include "sysfs.h"
2122

2223
#define NFSDBG_FACILITY NFSDBG_CLIENT
2324

@@ -952,6 +953,8 @@ static int nfs4_set_client(struct nfs_server *server,
952953
set_bit(NFS_CS_CHECK_LEASE_TIME, &clp->cl_res_state);
953954

954955
server->nfs_client = clp;
956+
nfs_sysfs_add_server(server);
957+
955958
return 0;
956959
}
957960

fs/nfs/super.c

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,8 @@
7070
#include "nfs4session.h"
7171
#include "pnfs.h"
7272
#include "nfs.h"
73+
#include "netns.h"
74+
#include "sysfs.h"
7375

7476
#define NFSDBG_FACILITY NFSDBG_VFS
7577

@@ -1089,6 +1091,7 @@ static void nfs_fill_super(struct super_block *sb, struct nfs_fs_context *ctx)
10891091
&sb->s_blocksize_bits);
10901092

10911093
nfs_super_set_maxbytes(sb, server->maxfilesize);
1094+
nfs_sysfs_move_server_to_sb(sb);
10921095
server->has_sec_mnt_opts = ctx->has_sec_mnt_opts;
10931096
}
10941097

@@ -1331,13 +1334,14 @@ int nfs_get_tree_common(struct fs_context *fc)
13311334
}
13321335

13331336
/*
1334-
* Destroy an NFS2/3 superblock
1337+
* Destroy an NFS superblock
13351338
*/
13361339
void nfs_kill_super(struct super_block *s)
13371340
{
13381341
struct nfs_server *server = NFS_SB(s);
13391342
dev_t dev = s->s_dev;
13401343

1344+
nfs_sysfs_move_sb_to_server(server);
13411345
generic_shutdown_super(s);
13421346

13431347
nfs_fscache_release_super_cookie(s);

fs/nfs/sysfs.c

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -215,3 +215,62 @@ void nfs_netns_sysfs_destroy(struct nfs_net *netns)
215215
netns->nfs_client = NULL;
216216
}
217217
}
218+
219+
static void nfs_sysfs_sb_release(struct kobject *kobj)
220+
{
221+
/* no-op: why? see lib/kobject.c kobject_cleanup() */
222+
}
223+
224+
static const void *nfs_netns_server_namespace(const struct kobject *kobj)
225+
{
226+
return container_of(kobj, struct nfs_server, kobj)->nfs_client->cl_net;
227+
}
228+
229+
static struct kobj_type nfs_sb_ktype = {
230+
.release = nfs_sysfs_sb_release,
231+
.sysfs_ops = &kobj_sysfs_ops,
232+
.namespace = nfs_netns_server_namespace,
233+
.child_ns_type = nfs_netns_object_child_ns_type,
234+
};
235+
236+
void nfs_sysfs_add_server(struct nfs_server *server)
237+
{
238+
int ret;
239+
240+
ret = kobject_init_and_add(&server->kobj, &nfs_sb_ktype,
241+
&nfs_kset->kobj, "server-%d", server->s_sysfs_id);
242+
if (ret < 0)
243+
pr_warn("NFS: nfs sysfs add server-%d failed (%d)\n",
244+
server->s_sysfs_id, ret);
245+
}
246+
EXPORT_SYMBOL_GPL(nfs_sysfs_add_server);
247+
248+
void nfs_sysfs_move_server_to_sb(struct super_block *s)
249+
{
250+
struct nfs_server *server = s->s_fs_info;
251+
int ret;
252+
253+
ret = kobject_rename(&server->kobj, s->s_id);
254+
if (ret < 0)
255+
pr_warn("NFS: rename sysfs %s failed (%d)\n",
256+
server->kobj.name, ret);
257+
}
258+
259+
void nfs_sysfs_move_sb_to_server(struct nfs_server *server)
260+
{
261+
const char *s;
262+
int ret = -ENOMEM;
263+
264+
s = kasprintf(GFP_KERNEL, "server-%d", server->s_sysfs_id);
265+
if (s)
266+
ret = kobject_rename(&server->kobj, s);
267+
if (ret < 0)
268+
pr_warn("NFS: rename sysfs %s failed (%d)\n",
269+
server->kobj.name, ret);
270+
}
271+
272+
/* unlink, not dec-ref */
273+
void nfs_sysfs_remove_server(struct nfs_server *server)
274+
{
275+
kobject_del(&server->kobj);
276+
}

fs/nfs/sysfs.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,4 +23,9 @@ extern void nfs_sysfs_exit(void);
2323
void nfs_netns_sysfs_setup(struct nfs_net *netns, struct net *net);
2424
void nfs_netns_sysfs_destroy(struct nfs_net *netns);
2525

26+
void nfs_sysfs_add_server(struct nfs_server *s);
27+
void nfs_sysfs_move_server_to_sb(struct super_block *s);
28+
void nfs_sysfs_move_sb_to_server(struct nfs_server *s);
29+
void nfs_sysfs_remove_server(struct nfs_server *s);
30+
2631
#endif

include/linux/nfs_fs_sb.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -184,6 +184,7 @@ struct nfs_server {
184184
change_attr_type;/* Description of change attribute */
185185

186186
struct nfs_fsid fsid;
187+
int s_sysfs_id; /* sysfs dentry index */
187188
__u64 maxfilesize; /* maximum file size */
188189
struct timespec64 time_delta; /* smallest time granularity */
189190
unsigned long mount_time; /* when this fs was mounted */
@@ -260,6 +261,7 @@ struct nfs_server {
260261
/* User namespace info */
261262
const struct cred *cred;
262263
bool has_sec_mnt_opts;
264+
struct kobject kobj;
263265
};
264266

265267
/* Server capabilities */

0 commit comments

Comments
 (0)