2 * linux/fs/nfs/dns_resolve.c
4 * Copyright (c) 2009 Trond Myklebust <Trond.Myklebust@netapp.com>
6 * Resolves DNS hostnames into valid ip addresses
9 #ifdef CONFIG_NFS_USE_KERNEL_DNS
11 #include <linux/module.h>
12 #include <linux/sunrpc/clnt.h>
13 #include <linux/sunrpc/addr.h>
14 #include <linux/dns_resolver.h>
15 #include "dns_resolve.h"
17 ssize_t nfs_dns_resolve_name(struct net *net, char *name, size_t namelen,
18 struct sockaddr *sa, size_t salen)
24 ip_len = dns_query(NULL, name, namelen, NULL, &ip_addr, NULL);
26 ret = rpc_pton(net, ip_addr, ip_len, sa, salen);
35 #include <linux/module.h>
36 #include <linux/hash.h>
37 #include <linux/string.h>
38 #include <linux/kmod.h>
39 #include <linux/slab.h>
40 #include <linux/module.h>
41 #include <linux/socket.h>
42 #include <linux/seq_file.h>
43 #include <linux/inet.h>
44 #include <linux/sunrpc/clnt.h>
45 #include <linux/sunrpc/addr.h>
46 #include <linux/sunrpc/cache.h>
47 #include <linux/sunrpc/svcauth.h>
48 #include <linux/sunrpc/rpc_pipe_fs.h>
50 #include "dns_resolve.h"
51 #include "cache_lib.h"
54 #define NFS_DNS_HASHBITS 4
55 #define NFS_DNS_HASHTBL_SIZE (1 << NFS_DNS_HASHBITS)
63 struct sockaddr_storage addr;
68 static void nfs_dns_ent_update(struct cache_head *cnew,
69 struct cache_head *ckey)
71 struct nfs_dns_ent *new;
72 struct nfs_dns_ent *key;
74 new = container_of(cnew, struct nfs_dns_ent, h);
75 key = container_of(ckey, struct nfs_dns_ent, h);
77 memcpy(&new->addr, &key->addr, key->addrlen);
78 new->addrlen = key->addrlen;
81 static void nfs_dns_ent_init(struct cache_head *cnew,
82 struct cache_head *ckey)
84 struct nfs_dns_ent *new;
85 struct nfs_dns_ent *key;
87 new = container_of(cnew, struct nfs_dns_ent, h);
88 key = container_of(ckey, struct nfs_dns_ent, h);
91 new->hostname = kstrndup(key->hostname, key->namelen, GFP_KERNEL);
93 new->namelen = key->namelen;
94 nfs_dns_ent_update(cnew, ckey);
101 static void nfs_dns_ent_put(struct kref *ref)
103 struct nfs_dns_ent *item;
105 item = container_of(ref, struct nfs_dns_ent, h.ref);
106 kfree(item->hostname);
110 static struct cache_head *nfs_dns_ent_alloc(void)
112 struct nfs_dns_ent *item = kmalloc(sizeof(*item), GFP_KERNEL);
115 item->hostname = NULL;
123 static unsigned int nfs_dns_hash(const struct nfs_dns_ent *key)
125 return hash_str(key->hostname, NFS_DNS_HASHBITS);
128 static void nfs_dns_request(struct cache_detail *cd,
129 struct cache_head *ch,
130 char **bpp, int *blen)
132 struct nfs_dns_ent *key = container_of(ch, struct nfs_dns_ent, h);
134 qword_add(bpp, blen, key->hostname);
138 static int nfs_dns_upcall(struct cache_detail *cd,
139 struct cache_head *ch)
141 struct nfs_dns_ent *key = container_of(ch, struct nfs_dns_ent, h);
144 ret = nfs_cache_upcall(cd, key->hostname);
146 ret = sunrpc_cache_pipe_upcall(cd, ch);
150 static int nfs_dns_match(struct cache_head *ca,
151 struct cache_head *cb)
153 struct nfs_dns_ent *a;
154 struct nfs_dns_ent *b;
156 a = container_of(ca, struct nfs_dns_ent, h);
157 b = container_of(cb, struct nfs_dns_ent, h);
159 if (a->namelen == 0 || a->namelen != b->namelen)
161 return memcmp(a->hostname, b->hostname, a->namelen) == 0;
164 static int nfs_dns_show(struct seq_file *m, struct cache_detail *cd,
165 struct cache_head *h)
167 struct nfs_dns_ent *item;
171 seq_puts(m, "# ip address hostname ttl\n");
174 item = container_of(h, struct nfs_dns_ent, h);
175 ttl = item->h.expiry_time - seconds_since_boot();
179 if (!test_bit(CACHE_NEGATIVE, &h->flags)) {
180 char buf[INET6_ADDRSTRLEN+IPV6_SCOPE_ID_LEN+1];
182 rpc_ntop((struct sockaddr *)&item->addr, buf, sizeof(buf));
183 seq_printf(m, "%15s ", buf);
185 seq_puts(m, "<none> ");
186 seq_printf(m, "%15s %ld\n", item->hostname, ttl);
190 static struct nfs_dns_ent *nfs_dns_lookup(struct cache_detail *cd,
191 struct nfs_dns_ent *key)
193 struct cache_head *ch;
195 ch = sunrpc_cache_lookup(cd,
200 return container_of(ch, struct nfs_dns_ent, h);
203 static struct nfs_dns_ent *nfs_dns_update(struct cache_detail *cd,
204 struct nfs_dns_ent *new,
205 struct nfs_dns_ent *key)
207 struct cache_head *ch;
209 ch = sunrpc_cache_update(cd,
214 return container_of(ch, struct nfs_dns_ent, h);
217 static int nfs_dns_parse(struct cache_detail *cd, char *buf, int buflen)
219 char buf1[NFS_DNS_HOSTNAME_MAXLEN+1];
220 struct nfs_dns_ent key, *item;
225 if (buf[buflen-1] != '\n')
227 buf[buflen-1] = '\0';
229 len = qword_get(&buf, buf1, sizeof(buf1));
232 key.addrlen = rpc_pton(cd->net, buf1, len,
233 (struct sockaddr *)&key.addr,
236 len = qword_get(&buf, buf1, sizeof(buf1));
242 memset(&key.h, 0, sizeof(key.h));
244 if (get_uint(&buf, &ttl) < 0)
248 key.h.expiry_time = ttl + seconds_since_boot();
251 item = nfs_dns_lookup(cd, &key);
255 if (key.addrlen == 0)
256 set_bit(CACHE_NEGATIVE, &key.h.flags);
258 item = nfs_dns_update(cd, &key, item);
263 cache_put(&item->h, cd);
268 static int do_cache_lookup(struct cache_detail *cd,
269 struct nfs_dns_ent *key,
270 struct nfs_dns_ent **item,
271 struct nfs_cache_defer_req *dreq)
275 *item = nfs_dns_lookup(cd, key);
277 ret = cache_check(cd, &(*item)->h, &dreq->req);
284 static int do_cache_lookup_nowait(struct cache_detail *cd,
285 struct nfs_dns_ent *key,
286 struct nfs_dns_ent **item)
290 *item = nfs_dns_lookup(cd, key);
294 if (!test_bit(CACHE_VALID, &(*item)->h.flags)
295 || (*item)->h.expiry_time < seconds_since_boot()
296 || cd->flush_time > (*item)->h.last_refresh)
299 if (test_bit(CACHE_NEGATIVE, &(*item)->h.flags))
303 cache_put(&(*item)->h, cd);
309 static int do_cache_lookup_wait(struct cache_detail *cd,
310 struct nfs_dns_ent *key,
311 struct nfs_dns_ent **item)
313 struct nfs_cache_defer_req *dreq;
316 dreq = nfs_cache_defer_req_alloc();
319 ret = do_cache_lookup(cd, key, item, dreq);
320 if (ret == -EAGAIN) {
321 ret = nfs_cache_wait_for_upcall(dreq);
323 ret = do_cache_lookup_nowait(cd, key, item);
325 nfs_cache_defer_req_put(dreq);
330 ssize_t nfs_dns_resolve_name(struct net *net, char *name,
331 size_t namelen, struct sockaddr *sa, size_t salen)
333 struct nfs_dns_ent key = {
337 struct nfs_dns_ent *item = NULL;
339 struct nfs_net *nn = net_generic(net, nfs_net_id);
341 ret = do_cache_lookup_wait(nn->nfs_dns_resolve, &key, &item);
343 if (salen >= item->addrlen) {
344 memcpy(sa, &item->addr, item->addrlen);
348 cache_put(&item->h, nn->nfs_dns_resolve);
349 } else if (ret == -ENOENT)
354 static struct cache_detail nfs_dns_resolve_template = {
355 .owner = THIS_MODULE,
356 .hash_size = NFS_DNS_HASHTBL_SIZE,
357 .name = "dns_resolve",
358 .cache_put = nfs_dns_ent_put,
359 .cache_upcall = nfs_dns_upcall,
360 .cache_request = nfs_dns_request,
361 .cache_parse = nfs_dns_parse,
362 .cache_show = nfs_dns_show,
363 .match = nfs_dns_match,
364 .init = nfs_dns_ent_init,
365 .update = nfs_dns_ent_update,
366 .alloc = nfs_dns_ent_alloc,
370 int nfs_dns_resolver_cache_init(struct net *net)
373 struct nfs_net *nn = net_generic(net, nfs_net_id);
375 nn->nfs_dns_resolve = cache_create_net(&nfs_dns_resolve_template, net);
376 if (IS_ERR(nn->nfs_dns_resolve))
377 return PTR_ERR(nn->nfs_dns_resolve);
379 err = nfs_cache_register_net(net, nn->nfs_dns_resolve);
385 cache_destroy_net(nn->nfs_dns_resolve, net);
389 void nfs_dns_resolver_cache_destroy(struct net *net)
391 struct nfs_net *nn = net_generic(net, nfs_net_id);
393 nfs_cache_unregister_net(net, nn->nfs_dns_resolve);
394 cache_destroy_net(nn->nfs_dns_resolve, net);
397 static int nfs4_dns_net_init(struct net *net)
399 return nfs_dns_resolver_cache_init(net);
402 static void nfs4_dns_net_exit(struct net *net)
404 nfs_dns_resolver_cache_destroy(net);
407 static struct pernet_operations nfs4_dns_resolver_ops = {
408 .init = nfs4_dns_net_init,
409 .exit = nfs4_dns_net_exit,
412 static int rpc_pipefs_event(struct notifier_block *nb, unsigned long event,
415 struct super_block *sb = ptr;
416 struct net *net = sb->s_fs_info;
417 struct nfs_net *nn = net_generic(net, nfs_net_id);
418 struct cache_detail *cd = nn->nfs_dns_resolve;
424 if (!try_module_get(THIS_MODULE))
428 case RPC_PIPEFS_MOUNT:
429 ret = nfs_cache_register_sb(sb, cd);
431 case RPC_PIPEFS_UMOUNT:
432 nfs_cache_unregister_sb(sb, cd);
438 module_put(THIS_MODULE);
442 static struct notifier_block nfs_dns_resolver_block = {
443 .notifier_call = rpc_pipefs_event,
446 int nfs_dns_resolver_init(void)
450 err = register_pernet_subsys(&nfs4_dns_resolver_ops);
453 err = rpc_pipefs_notifier_register(&nfs_dns_resolver_block);
458 unregister_pernet_subsys(&nfs4_dns_resolver_ops);
463 void nfs_dns_resolver_destroy(void)
465 rpc_pipefs_notifier_unregister(&nfs_dns_resolver_block);
466 unregister_pernet_subsys(&nfs4_dns_resolver_ops);