Skip to content

Commit 1b340d0

Browse files
Stanislav KinsburskyTrond Myklebust
authored andcommitted
NFS: DNS resolver cache per network namespace context introduced
This patch implements DNS resolver cache creation and registration for each alive network namespace context. This was done by registering NFS per-net operations, responsible for DNS cache allocation/register and unregister/destructioning instead of initialization and destruction of static "nfs_dns_resolve" cache detail (this one was removed). Pointer to network dns resolver cache is stored in new per-net "nfs_net" structure. This patch also changes nfs_dns_resolve_name() function prototype (and it's calls) by adding network pointer parameter, which is used to get proper DNS resolver cache pointer for do_cache_lookup_wait() call. Note: empty nfs_dns_resolver_init() and nfs_dns_resolver_destroy() functions will be used in next patch in the series. Signed-off-by: Stanislav Kinsbursky <skinsbursky@parallels.com> Signed-off-by: Trond Myklebust <Trond.Myklebust@netapp.com>
1 parent 5c1cacb commit 1b340d0

5 files changed

Lines changed: 123 additions & 41 deletions

File tree

‎fs/nfs/dns_resolve.c‎

Lines changed: 63 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
#include <linux/sunrpc/clnt.h>
1212
#include <linux/dns_resolver.h>
1313

14-
ssize_t nfs_dns_resolve_name(char *name, size_t namelen,
14+
ssize_t nfs_dns_resolve_name(struct net *net, char *name, size_t namelen,
1515
struct sockaddr *sa, size_t salen)
1616
{
1717
ssize_t ret;
@@ -43,12 +43,11 @@ ssize_t nfs_dns_resolve_name(char *name, size_t namelen,
4343

4444
#include "dns_resolve.h"
4545
#include "cache_lib.h"
46+
#include "netns.h"
4647

4748
#define NFS_DNS_HASHBITS 4
4849
#define NFS_DNS_HASHTBL_SIZE (1 << NFS_DNS_HASHBITS)
4950

50-
static struct cache_head *nfs_dns_table[NFS_DNS_HASHTBL_SIZE];
51-
5251
struct nfs_dns_ent {
5352
struct cache_head h;
5453

@@ -259,21 +258,6 @@ static int nfs_dns_parse(struct cache_detail *cd, char *buf, int buflen)
259258
return ret;
260259
}
261260

262-
static struct cache_detail nfs_dns_resolve = {
263-
.owner = THIS_MODULE,
264-
.hash_size = NFS_DNS_HASHTBL_SIZE,
265-
.hash_table = nfs_dns_table,
266-
.name = "dns_resolve",
267-
.cache_put = nfs_dns_ent_put,
268-
.cache_upcall = nfs_dns_upcall,
269-
.cache_parse = nfs_dns_parse,
270-
.cache_show = nfs_dns_show,
271-
.match = nfs_dns_match,
272-
.init = nfs_dns_ent_init,
273-
.update = nfs_dns_ent_update,
274-
.alloc = nfs_dns_ent_alloc,
275-
};
276-
277261
static int do_cache_lookup(struct cache_detail *cd,
278262
struct nfs_dns_ent *key,
279263
struct nfs_dns_ent **item,
@@ -336,46 +320,92 @@ static int do_cache_lookup_wait(struct cache_detail *cd,
336320
return ret;
337321
}
338322

339-
ssize_t nfs_dns_resolve_name(char *name, size_t namelen,
340-
struct sockaddr *sa, size_t salen)
323+
ssize_t nfs_dns_resolve_name(struct net *net, char *name,
324+
size_t namelen, struct sockaddr *sa, size_t salen)
341325
{
342326
struct nfs_dns_ent key = {
343327
.hostname = name,
344328
.namelen = namelen,
345329
};
346330
struct nfs_dns_ent *item = NULL;
347331
ssize_t ret;
332+
struct nfs_net *nn = net_generic(net, nfs_net_id);
348333

349-
ret = do_cache_lookup_wait(&nfs_dns_resolve, &key, &item);
334+
ret = do_cache_lookup_wait(nn->nfs_dns_resolve, &key, &item);
350335
if (ret == 0) {
351336
if (salen >= item->addrlen) {
352337
memcpy(sa, &item->addr, item->addrlen);
353338
ret = item->addrlen;
354339
} else
355340
ret = -EOVERFLOW;
356-
cache_put(&item->h, &nfs_dns_resolve);
341+
cache_put(&item->h, nn->nfs_dns_resolve);
357342
} else if (ret == -ENOENT)
358343
ret = -ESRCH;
359344
return ret;
360345
}
361346

362-
int nfs_dns_resolver_init(void)
347+
int nfs_dns_resolver_cache_init(struct net *net)
363348
{
364-
int err;
349+
int err = -ENOMEM;
350+
struct nfs_net *nn = net_generic(net, nfs_net_id);
351+
struct cache_detail *cd;
352+
struct cache_head **tbl;
353+
354+
cd = kzalloc(sizeof(struct cache_detail), GFP_KERNEL);
355+
if (cd == NULL)
356+
goto err_cd;
357+
358+
tbl = kzalloc(NFS_DNS_HASHTBL_SIZE * sizeof(struct cache_head *),
359+
GFP_KERNEL);
360+
if (tbl == NULL)
361+
goto err_tbl;
362+
363+
cd->owner = THIS_MODULE,
364+
cd->hash_size = NFS_DNS_HASHTBL_SIZE,
365+
cd->hash_table = tbl,
366+
cd->name = "dns_resolve",
367+
cd->cache_put = nfs_dns_ent_put,
368+
cd->cache_upcall = nfs_dns_upcall,
369+
cd->cache_parse = nfs_dns_parse,
370+
cd->cache_show = nfs_dns_show,
371+
cd->match = nfs_dns_match,
372+
cd->init = nfs_dns_ent_init,
373+
cd->update = nfs_dns_ent_update,
374+
cd->alloc = nfs_dns_ent_alloc,
375+
376+
nfs_cache_init(cd);
377+
err = nfs_cache_register_net(net, cd);
378+
if (err)
379+
goto err_reg;
380+
nn->nfs_dns_resolve = cd;
381+
return 0;
365382

366-
nfs_cache_init(&nfs_dns_resolve);
367-
err = nfs_cache_register_net(&init_net, &nfs_dns_resolve);
368-
if (err) {
369-
nfs_cache_destroy(&nfs_dns_resolve);
370-
return err;
371-
}
383+
err_reg:
384+
nfs_cache_destroy(cd);
385+
kfree(cd->hash_table);
386+
err_tbl:
387+
kfree(cd);
388+
err_cd:
389+
return err;
390+
}
391+
392+
void nfs_dns_resolver_cache_destroy(struct net *net)
393+
{
394+
struct nfs_net *nn = net_generic(net, nfs_net_id);
395+
struct cache_detail *cd = nn->nfs_dns_resolve;
396+
397+
nfs_cache_unregister_net(net, cd);
398+
nfs_cache_destroy(cd);
399+
kfree(cd->hash_table);
400+
kfree(cd);
401+
}
402+
403+
int nfs_dns_resolver_init(void)
404+
{
372405
return 0;
373406
}
374407

375408
void nfs_dns_resolver_destroy(void)
376409
{
377-
nfs_cache_unregister_net(&init_net, &nfs_dns_resolve);
378-
nfs_cache_destroy(&nfs_dns_resolve);
379410
}
380-
381411
#endif

‎fs/nfs/dns_resolve.h‎

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,22 @@ static inline int nfs_dns_resolver_init(void)
1515

1616
static inline void nfs_dns_resolver_destroy(void)
1717
{}
18+
19+
static inline int nfs_dns_resolver_cache_init(struct net *net)
20+
{
21+
return 0;
22+
}
23+
24+
static inline void nfs_dns_resolver_cache_destroy(struct net *net)
25+
{}
1826
#else
1927
extern int nfs_dns_resolver_init(void);
2028
extern void nfs_dns_resolver_destroy(void);
29+
extern int nfs_dns_resolver_cache_init(struct net *net);
30+
extern void nfs_dns_resolver_cache_destroy(struct net *net);
2131
#endif
2232

23-
extern ssize_t nfs_dns_resolve_name(char *name, size_t namelen,
24-
struct sockaddr *sa, size_t salen);
33+
extern ssize_t nfs_dns_resolve_name(struct net *net, char *name,
34+
size_t namelen, struct sockaddr *sa, size_t salen);
2535

2636
#endif

‎fs/nfs/inode.c‎

Lines changed: 30 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@
5151
#include "fscache.h"
5252
#include "dns_resolve.h"
5353
#include "pnfs.h"
54+
#include "netns.h"
5455

5556
#define NFSDBG_FACILITY NFSDBG_VFS
5657

@@ -1552,6 +1553,25 @@ static void nfsiod_stop(void)
15521553
destroy_workqueue(wq);
15531554
}
15541555

1556+
int nfs_net_id;
1557+
1558+
static int nfs_net_init(struct net *net)
1559+
{
1560+
return nfs_dns_resolver_cache_init(net);
1561+
}
1562+
1563+
static void nfs_net_exit(struct net *net)
1564+
{
1565+
nfs_dns_resolver_cache_destroy(net);
1566+
}
1567+
1568+
static struct pernet_operations nfs_net_ops = {
1569+
.init = nfs_net_init,
1570+
.exit = nfs_net_exit,
1571+
.id = &nfs_net_id,
1572+
.size = sizeof(struct nfs_net),
1573+
};
1574+
15551575
/*
15561576
* Initialize NFS
15571577
*/
@@ -1561,9 +1581,13 @@ static int __init init_nfs_fs(void)
15611581

15621582
err = nfs_idmap_init();
15631583
if (err < 0)
1564-
goto out9;
1584+
goto out10;
15651585

15661586
err = nfs_dns_resolver_init();
1587+
if (err < 0)
1588+
goto out9;
1589+
1590+
err = register_pernet_subsys(&nfs_net_ops);
15671591
if (err < 0)
15681592
goto out8;
15691593

@@ -1625,10 +1649,12 @@ static int __init init_nfs_fs(void)
16251649
out6:
16261650
nfs_fscache_unregister();
16271651
out7:
1628-
nfs_dns_resolver_destroy();
1652+
unregister_pernet_subsys(&nfs_net_ops);
16291653
out8:
1630-
nfs_idmap_quit();
1654+
nfs_dns_resolver_destroy();
16311655
out9:
1656+
nfs_idmap_quit();
1657+
out10:
16321658
return err;
16331659
}
16341660

@@ -1640,6 +1666,7 @@ static void __exit exit_nfs_fs(void)
16401666
nfs_destroy_inodecache();
16411667
nfs_destroy_nfspagecache();
16421668
nfs_fscache_unregister();
1669+
unregister_pernet_subsys(&nfs_net_ops);
16431670
nfs_dns_resolver_destroy();
16441671
nfs_idmap_quit();
16451672
#ifdef CONFIG_PROC_FS

‎fs/nfs/netns.h‎

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
#ifndef __NFS_NETNS_H__
2+
#define __NFS_NETNS_H__
3+
4+
#include <net/net_namespace.h>
5+
#include <net/netns/generic.h>
6+
7+
struct nfs_net {
8+
struct cache_detail *nfs_dns_resolve;
9+
};
10+
11+
extern int nfs_net_id;
12+
13+
#endif

‎fs/nfs/nfs4namespace.c‎

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -94,13 +94,14 @@ static int nfs4_validate_fspath(struct dentry *dentry,
9494
}
9595

9696
static size_t nfs_parse_server_name(char *string, size_t len,
97-
struct sockaddr *sa, size_t salen)
97+
struct sockaddr *sa, size_t salen, struct nfs_server *server)
9898
{
9999
ssize_t ret;
100100

101101
ret = rpc_pton(string, len, sa, salen);
102102
if (ret == 0) {
103-
ret = nfs_dns_resolve_name(string, len, sa, salen);
103+
ret = nfs_dns_resolve_name(server->client->cl_xprt->xprt_net,
104+
string, len, sa, salen);
104105
if (ret < 0)
105106
ret = 0;
106107
}
@@ -137,7 +138,8 @@ static struct vfsmount *try_location(struct nfs_clone_mount *mountdata,
137138
continue;
138139

139140
mountdata->addrlen = nfs_parse_server_name(buf->data, buf->len,
140-
mountdata->addr, addr_bufsize);
141+
mountdata->addr, addr_bufsize,
142+
NFS_SB(mountdata->sb));
141143
if (mountdata->addrlen == 0)
142144
continue;
143145

0 commit comments

Comments
 (0)