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 #include <linux/hash.h>
10 #include <linux/string.h>
11 #include <linux/kmod.h>
12 #include <linux/slab.h>
13 #include <linux/module.h>
14 #include <linux/socket.h>
15 #include <linux/seq_file.h>
16 #include <linux/inet.h>
17 #include <linux/sunrpc/clnt.h>
18 #include <linux/sunrpc/cache.h>
19 #include <linux/sunrpc/svcauth.h>
21 #include "dns_resolve.h"
22 #include "cache_lib.h"
24 #define NFS_DNS_HASHBITS 4
25 #define NFS_DNS_HASHTBL_SIZE (1 << NFS_DNS_HASHBITS)
27 static struct cache_head *nfs_dns_table[NFS_DNS_HASHTBL_SIZE];
35 struct sockaddr_storage addr;
40 static void nfs_dns_ent_update(struct cache_head *cnew,
41 struct cache_head *ckey)
43 struct nfs_dns_ent *new;
44 struct nfs_dns_ent *key;
46 new = container_of(cnew, struct nfs_dns_ent, h);
47 key = container_of(ckey, struct nfs_dns_ent, h);
49 memcpy(&new->addr, &key->addr, key->addrlen);
50 new->addrlen = key->addrlen;
53 static void nfs_dns_ent_init(struct cache_head *cnew,
54 struct cache_head *ckey)
56 struct nfs_dns_ent *new;
57 struct nfs_dns_ent *key;
59 new = container_of(cnew, struct nfs_dns_ent, h);
60 key = container_of(ckey, struct nfs_dns_ent, h);
63 new->hostname = kstrndup(key->hostname, key->namelen, GFP_KERNEL);
65 new->namelen = key->namelen;
66 nfs_dns_ent_update(cnew, ckey);
73 static void nfs_dns_ent_put(struct kref *ref)
75 struct nfs_dns_ent *item;
77 item = container_of(ref, struct nfs_dns_ent, h.ref);
78 kfree(item->hostname);
82 static struct cache_head *nfs_dns_ent_alloc(void)
84 struct nfs_dns_ent *item = kmalloc(sizeof(*item), GFP_KERNEL);
87 item->hostname = NULL;
95 static unsigned int nfs_dns_hash(const struct nfs_dns_ent *key)
97 return hash_str(key->hostname, NFS_DNS_HASHBITS);
100 static void nfs_dns_request(struct cache_detail *cd,
101 struct cache_head *ch,
102 char **bpp, int *blen)
104 struct nfs_dns_ent *key = container_of(ch, struct nfs_dns_ent, h);
106 qword_add(bpp, blen, key->hostname);
110 static int nfs_dns_upcall(struct cache_detail *cd,
111 struct cache_head *ch)
113 struct nfs_dns_ent *key = container_of(ch, struct nfs_dns_ent, h);
116 ret = nfs_cache_upcall(cd, key->hostname);
118 ret = sunrpc_cache_pipe_upcall(cd, ch, nfs_dns_request);
122 static int nfs_dns_match(struct cache_head *ca,
123 struct cache_head *cb)
125 struct nfs_dns_ent *a;
126 struct nfs_dns_ent *b;
128 a = container_of(ca, struct nfs_dns_ent, h);
129 b = container_of(cb, struct nfs_dns_ent, h);
131 if (a->namelen == 0 || a->namelen != b->namelen)
133 return memcmp(a->hostname, b->hostname, a->namelen) == 0;
136 static int nfs_dns_show(struct seq_file *m, struct cache_detail *cd,
137 struct cache_head *h)
139 struct nfs_dns_ent *item;
143 seq_puts(m, "# ip address hostname ttl\n");
146 item = container_of(h, struct nfs_dns_ent, h);
147 ttl = (long)item->h.expiry_time - (long)get_seconds();
151 if (!test_bit(CACHE_NEGATIVE, &h->flags)) {
152 char buf[INET6_ADDRSTRLEN+IPV6_SCOPE_ID_LEN+1];
154 rpc_ntop((struct sockaddr *)&item->addr, buf, sizeof(buf));
155 seq_printf(m, "%15s ", buf);
157 seq_puts(m, "<none> ");
158 seq_printf(m, "%15s %ld\n", item->hostname, ttl);
162 static struct nfs_dns_ent *nfs_dns_lookup(struct cache_detail *cd,
163 struct nfs_dns_ent *key)
165 struct cache_head *ch;
167 ch = sunrpc_cache_lookup(cd,
172 return container_of(ch, struct nfs_dns_ent, h);
175 static struct nfs_dns_ent *nfs_dns_update(struct cache_detail *cd,
176 struct nfs_dns_ent *new,
177 struct nfs_dns_ent *key)
179 struct cache_head *ch;
181 ch = sunrpc_cache_update(cd,
186 return container_of(ch, struct nfs_dns_ent, h);
189 static int nfs_dns_parse(struct cache_detail *cd, char *buf, int buflen)
191 char buf1[NFS_DNS_HOSTNAME_MAXLEN+1];
192 struct nfs_dns_ent key, *item;
197 if (buf[buflen-1] != '\n')
199 buf[buflen-1] = '\0';
201 len = qword_get(&buf, buf1, sizeof(buf1));
204 key.addrlen = rpc_pton(buf1, len,
205 (struct sockaddr *)&key.addr,
208 len = qword_get(&buf, buf1, sizeof(buf1));
214 memset(&key.h, 0, sizeof(key.h));
216 ttl = get_expiry(&buf);
219 key.h.expiry_time = ttl + get_seconds();
222 item = nfs_dns_lookup(cd, &key);
226 if (key.addrlen == 0)
227 set_bit(CACHE_NEGATIVE, &key.h.flags);
229 item = nfs_dns_update(cd, &key, item);
234 cache_put(&item->h, cd);
239 static struct cache_detail nfs_dns_resolve = {
240 .owner = THIS_MODULE,
241 .hash_size = NFS_DNS_HASHTBL_SIZE,
242 .hash_table = nfs_dns_table,
243 .name = "dns_resolve",
244 .cache_put = nfs_dns_ent_put,
245 .cache_upcall = nfs_dns_upcall,
246 .cache_parse = nfs_dns_parse,
247 .cache_show = nfs_dns_show,
248 .match = nfs_dns_match,
249 .init = nfs_dns_ent_init,
250 .update = nfs_dns_ent_update,
251 .alloc = nfs_dns_ent_alloc,
254 static int do_cache_lookup(struct cache_detail *cd,
255 struct nfs_dns_ent *key,
256 struct nfs_dns_ent **item,
257 struct nfs_cache_defer_req *dreq)
261 *item = nfs_dns_lookup(cd, key);
263 ret = cache_check(cd, &(*item)->h, &dreq->req);
270 static int do_cache_lookup_nowait(struct cache_detail *cd,
271 struct nfs_dns_ent *key,
272 struct nfs_dns_ent **item)
276 *item = nfs_dns_lookup(cd, key);
280 if (!test_bit(CACHE_VALID, &(*item)->h.flags)
281 || (*item)->h.expiry_time < get_seconds()
282 || cd->flush_time > (*item)->h.last_refresh)
285 if (test_bit(CACHE_NEGATIVE, &(*item)->h.flags))
289 cache_put(&(*item)->h, cd);
295 static int do_cache_lookup_wait(struct cache_detail *cd,
296 struct nfs_dns_ent *key,
297 struct nfs_dns_ent **item)
299 struct nfs_cache_defer_req *dreq;
302 dreq = nfs_cache_defer_req_alloc();
305 ret = do_cache_lookup(cd, key, item, dreq);
306 if (ret == -EAGAIN) {
307 ret = nfs_cache_wait_for_upcall(dreq);
309 ret = do_cache_lookup_nowait(cd, key, item);
311 nfs_cache_defer_req_put(dreq);
316 ssize_t nfs_dns_resolve_name(char *name, size_t namelen,
317 struct sockaddr *sa, size_t salen)
319 struct nfs_dns_ent key = {
323 struct nfs_dns_ent *item = NULL;
326 ret = do_cache_lookup_wait(&nfs_dns_resolve, &key, &item);
328 if (salen >= item->addrlen) {
329 memcpy(sa, &item->addr, item->addrlen);
333 cache_put(&item->h, &nfs_dns_resolve);
334 } else if (ret == -ENOENT)
339 int nfs_dns_resolver_init(void)
341 return nfs_cache_register(&nfs_dns_resolve);
344 void nfs_dns_resolver_destroy(void)
346 nfs_cache_unregister(&nfs_dns_resolve);