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/dns_resolver.h>
14 #include "dns_resolve.h"
16 ssize_t nfs_dns_resolve_name(struct net *net, char *name, size_t namelen,
17 struct sockaddr *sa, size_t salen)
23 ip_len = dns_query(NULL, name, namelen, NULL, &ip_addr, NULL);
25 ret = rpc_pton(net, ip_addr, ip_len, sa, salen);
31 EXPORT_SYMBOL_GPL(nfs_dns_resolve_name);
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/cache.h>
46 #include <linux/sunrpc/svcauth.h>
47 #include <linux/sunrpc/rpc_pipe_fs.h>
49 #include "dns_resolve.h"
50 #include "cache_lib.h"
53 #define NFS_DNS_HASHBITS 4
54 #define NFS_DNS_HASHTBL_SIZE (1 << NFS_DNS_HASHBITS)
62 struct sockaddr_storage addr;
67 static void nfs_dns_ent_update(struct cache_head *cnew,
68 struct cache_head *ckey)
70 struct nfs_dns_ent *new;
71 struct nfs_dns_ent *key;
73 new = container_of(cnew, struct nfs_dns_ent, h);
74 key = container_of(ckey, struct nfs_dns_ent, h);
76 memcpy(&new->addr, &key->addr, key->addrlen);
77 new->addrlen = key->addrlen;
80 static void nfs_dns_ent_init(struct cache_head *cnew,
81 struct cache_head *ckey)
83 struct nfs_dns_ent *new;
84 struct nfs_dns_ent *key;
86 new = container_of(cnew, struct nfs_dns_ent, h);
87 key = container_of(ckey, struct nfs_dns_ent, h);
90 new->hostname = kstrndup(key->hostname, key->namelen, GFP_KERNEL);
92 new->namelen = key->namelen;
93 nfs_dns_ent_update(cnew, ckey);
100 static void nfs_dns_ent_put(struct kref *ref)
102 struct nfs_dns_ent *item;
104 item = container_of(ref, struct nfs_dns_ent, h.ref);
105 kfree(item->hostname);
109 static struct cache_head *nfs_dns_ent_alloc(void)
111 struct nfs_dns_ent *item = kmalloc(sizeof(*item), GFP_KERNEL);
114 item->hostname = NULL;
122 static unsigned int nfs_dns_hash(const struct nfs_dns_ent *key)
124 return hash_str(key->hostname, NFS_DNS_HASHBITS);
127 static void nfs_dns_request(struct cache_detail *cd,
128 struct cache_head *ch,
129 char **bpp, int *blen)
131 struct nfs_dns_ent *key = container_of(ch, struct nfs_dns_ent, h);
133 qword_add(bpp, blen, key->hostname);
137 static int nfs_dns_upcall(struct cache_detail *cd,
138 struct cache_head *ch)
140 struct nfs_dns_ent *key = container_of(ch, struct nfs_dns_ent, h);
143 ret = nfs_cache_upcall(cd, key->hostname);
145 ret = sunrpc_cache_pipe_upcall(cd, ch, nfs_dns_request);
149 static int nfs_dns_match(struct cache_head *ca,
150 struct cache_head *cb)
152 struct nfs_dns_ent *a;
153 struct nfs_dns_ent *b;
155 a = container_of(ca, struct nfs_dns_ent, h);
156 b = container_of(cb, struct nfs_dns_ent, h);
158 if (a->namelen == 0 || a->namelen != b->namelen)
160 return memcmp(a->hostname, b->hostname, a->namelen) == 0;
163 static int nfs_dns_show(struct seq_file *m, struct cache_detail *cd,
164 struct cache_head *h)
166 struct nfs_dns_ent *item;
170 seq_puts(m, "# ip address hostname ttl\n");
173 item = container_of(h, struct nfs_dns_ent, h);
174 ttl = item->h.expiry_time - seconds_since_boot();
178 if (!test_bit(CACHE_NEGATIVE, &h->flags)) {
179 char buf[INET6_ADDRSTRLEN+IPV6_SCOPE_ID_LEN+1];
181 rpc_ntop((struct sockaddr *)&item->addr, buf, sizeof(buf));
182 seq_printf(m, "%15s ", buf);
184 seq_puts(m, "<none> ");
185 seq_printf(m, "%15s %ld\n", item->hostname, ttl);
189 static struct nfs_dns_ent *nfs_dns_lookup(struct cache_detail *cd,
190 struct nfs_dns_ent *key)
192 struct cache_head *ch;
194 ch = sunrpc_cache_lookup(cd,
199 return container_of(ch, struct nfs_dns_ent, h);
202 static struct nfs_dns_ent *nfs_dns_update(struct cache_detail *cd,
203 struct nfs_dns_ent *new,
204 struct nfs_dns_ent *key)
206 struct cache_head *ch;
208 ch = sunrpc_cache_update(cd,
213 return container_of(ch, struct nfs_dns_ent, h);
216 static int nfs_dns_parse(struct cache_detail *cd, char *buf, int buflen)
218 char buf1[NFS_DNS_HOSTNAME_MAXLEN+1];
219 struct nfs_dns_ent key, *item;
224 if (buf[buflen-1] != '\n')
226 buf[buflen-1] = '\0';
228 len = qword_get(&buf, buf1, sizeof(buf1));
231 key.addrlen = rpc_pton(cd->net, buf1, len,
232 (struct sockaddr *)&key.addr,
235 len = qword_get(&buf, buf1, sizeof(buf1));
241 memset(&key.h, 0, sizeof(key.h));
243 ttl = get_expiry(&buf);
246 key.h.expiry_time = ttl + seconds_since_boot();
249 item = nfs_dns_lookup(cd, &key);
253 if (key.addrlen == 0)
254 set_bit(CACHE_NEGATIVE, &key.h.flags);
256 item = nfs_dns_update(cd, &key, item);
261 cache_put(&item->h, cd);
266 static int do_cache_lookup(struct cache_detail *cd,
267 struct nfs_dns_ent *key,
268 struct nfs_dns_ent **item,
269 struct nfs_cache_defer_req *dreq)
273 *item = nfs_dns_lookup(cd, key);
275 ret = cache_check(cd, &(*item)->h, &dreq->req);
282 static int do_cache_lookup_nowait(struct cache_detail *cd,
283 struct nfs_dns_ent *key,
284 struct nfs_dns_ent **item)
288 *item = nfs_dns_lookup(cd, key);
292 if (!test_bit(CACHE_VALID, &(*item)->h.flags)
293 || (*item)->h.expiry_time < seconds_since_boot()
294 || cd->flush_time > (*item)->h.last_refresh)
297 if (test_bit(CACHE_NEGATIVE, &(*item)->h.flags))
301 cache_put(&(*item)->h, cd);
307 static int do_cache_lookup_wait(struct cache_detail *cd,
308 struct nfs_dns_ent *key,
309 struct nfs_dns_ent **item)
311 struct nfs_cache_defer_req *dreq;
314 dreq = nfs_cache_defer_req_alloc();
317 ret = do_cache_lookup(cd, key, item, dreq);
318 if (ret == -EAGAIN) {
319 ret = nfs_cache_wait_for_upcall(dreq);
321 ret = do_cache_lookup_nowait(cd, key, item);
323 nfs_cache_defer_req_put(dreq);
328 ssize_t nfs_dns_resolve_name(struct net *net, char *name,
329 size_t namelen, struct sockaddr *sa, size_t salen)
331 struct nfs_dns_ent key = {
335 struct nfs_dns_ent *item = NULL;
337 struct nfs_net *nn = net_generic(net, nfs_net_id);
339 ret = do_cache_lookup_wait(nn->nfs_dns_resolve, &key, &item);
341 if (salen >= item->addrlen) {
342 memcpy(sa, &item->addr, item->addrlen);
346 cache_put(&item->h, nn->nfs_dns_resolve);
347 } else if (ret == -ENOENT)
351 EXPORT_SYMBOL_GPL(nfs_dns_resolve_name);
353 int nfs_dns_resolver_cache_init(struct net *net)
356 struct nfs_net *nn = net_generic(net, nfs_net_id);
357 struct cache_detail *cd;
358 struct cache_head **tbl;
360 cd = kzalloc(sizeof(struct cache_detail), GFP_KERNEL);
364 tbl = kzalloc(NFS_DNS_HASHTBL_SIZE * sizeof(struct cache_head *),
369 cd->owner = THIS_MODULE,
370 cd->hash_size = NFS_DNS_HASHTBL_SIZE,
371 cd->hash_table = tbl,
372 cd->name = "dns_resolve",
373 cd->cache_put = nfs_dns_ent_put,
374 cd->cache_upcall = nfs_dns_upcall,
375 cd->cache_parse = nfs_dns_parse,
376 cd->cache_show = nfs_dns_show,
377 cd->match = nfs_dns_match,
378 cd->init = nfs_dns_ent_init,
379 cd->update = nfs_dns_ent_update,
380 cd->alloc = nfs_dns_ent_alloc,
383 err = nfs_cache_register_net(net, cd);
386 nn->nfs_dns_resolve = cd;
390 nfs_cache_destroy(cd);
391 kfree(cd->hash_table);
398 void nfs_dns_resolver_cache_destroy(struct net *net)
400 struct nfs_net *nn = net_generic(net, nfs_net_id);
401 struct cache_detail *cd = nn->nfs_dns_resolve;
403 nfs_cache_unregister_net(net, cd);
404 nfs_cache_destroy(cd);
405 kfree(cd->hash_table);
409 static int rpc_pipefs_event(struct notifier_block *nb, unsigned long event,
412 struct super_block *sb = ptr;
413 struct net *net = sb->s_fs_info;
414 struct nfs_net *nn = net_generic(net, nfs_net_id);
415 struct cache_detail *cd = nn->nfs_dns_resolve;
421 if (!try_module_get(THIS_MODULE))
425 case RPC_PIPEFS_MOUNT:
426 ret = nfs_cache_register_sb(sb, cd);
428 case RPC_PIPEFS_UMOUNT:
429 nfs_cache_unregister_sb(sb, cd);
435 module_put(THIS_MODULE);
439 static struct notifier_block nfs_dns_resolver_block = {
440 .notifier_call = rpc_pipefs_event,
443 int nfs_dns_resolver_init(void)
445 return rpc_pipefs_notifier_register(&nfs_dns_resolver_block);
448 void nfs_dns_resolver_destroy(void)
450 rpc_pipefs_notifier_unregister(&nfs_dns_resolver_block);