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/module.h>
13 #include <linux/socket.h>
14 #include <linux/seq_file.h>
15 #include <linux/inet.h>
16 #include <linux/sunrpc/clnt.h>
17 #include <linux/sunrpc/cache.h>
18 #include <linux/sunrpc/svcauth.h>
20 #include "dns_resolve.h"
21 #include "cache_lib.h"
23 #define NFS_DNS_HASHBITS 4
24 #define NFS_DNS_HASHTBL_SIZE (1 << NFS_DNS_HASHBITS)
26 static struct cache_head *nfs_dns_table[NFS_DNS_HASHTBL_SIZE];
34 struct sockaddr_storage addr;
39 static void nfs_dns_ent_update(struct cache_head *cnew,
40 struct cache_head *ckey)
42 struct nfs_dns_ent *new;
43 struct nfs_dns_ent *key;
45 new = container_of(cnew, struct nfs_dns_ent, h);
46 key = container_of(ckey, struct nfs_dns_ent, h);
48 memcpy(&new->addr, &key->addr, key->addrlen);
49 new->addrlen = key->addrlen;
52 static void nfs_dns_ent_init(struct cache_head *cnew,
53 struct cache_head *ckey)
55 struct nfs_dns_ent *new;
56 struct nfs_dns_ent *key;
58 new = container_of(cnew, struct nfs_dns_ent, h);
59 key = container_of(ckey, struct nfs_dns_ent, h);
62 new->hostname = kstrndup(key->hostname, key->namelen, GFP_KERNEL);
64 new->namelen = key->namelen;
65 nfs_dns_ent_update(cnew, ckey);
72 static void nfs_dns_ent_put(struct kref *ref)
74 struct nfs_dns_ent *item;
76 item = container_of(ref, struct nfs_dns_ent, h.ref);
77 kfree(item->hostname);
81 static struct cache_head *nfs_dns_ent_alloc(void)
83 struct nfs_dns_ent *item = kmalloc(sizeof(*item), GFP_KERNEL);
86 item->hostname = NULL;
94 static unsigned int nfs_dns_hash(const struct nfs_dns_ent *key)
96 return hash_str(key->hostname, NFS_DNS_HASHBITS);
99 static void nfs_dns_request(struct cache_detail *cd,
100 struct cache_head *ch,
101 char **bpp, int *blen)
103 struct nfs_dns_ent *key = container_of(ch, struct nfs_dns_ent, h);
105 qword_add(bpp, blen, key->hostname);
109 static int nfs_dns_upcall(struct cache_detail *cd,
110 struct cache_head *ch)
112 struct nfs_dns_ent *key = container_of(ch, struct nfs_dns_ent, h);
115 ret = nfs_cache_upcall(cd, key->hostname);
117 ret = sunrpc_cache_pipe_upcall(cd, ch, nfs_dns_request);
121 static int nfs_dns_match(struct cache_head *ca,
122 struct cache_head *cb)
124 struct nfs_dns_ent *a;
125 struct nfs_dns_ent *b;
127 a = container_of(ca, struct nfs_dns_ent, h);
128 b = container_of(cb, struct nfs_dns_ent, h);
130 if (a->namelen == 0 || a->namelen != b->namelen)
132 return memcmp(a->hostname, b->hostname, a->namelen) == 0;
135 static int nfs_dns_show(struct seq_file *m, struct cache_detail *cd,
136 struct cache_head *h)
138 struct nfs_dns_ent *item;
142 seq_puts(m, "# ip address hostname ttl\n");
145 item = container_of(h, struct nfs_dns_ent, h);
146 ttl = (long)item->h.expiry_time - (long)get_seconds();
150 if (!test_bit(CACHE_NEGATIVE, &h->flags)) {
151 char buf[INET6_ADDRSTRLEN+IPV6_SCOPE_ID_LEN+1];
153 rpc_ntop((struct sockaddr *)&item->addr, buf, sizeof(buf));
154 seq_printf(m, "%15s ", buf);
156 seq_puts(m, "<none> ");
157 seq_printf(m, "%15s %ld\n", item->hostname, ttl);
161 static struct nfs_dns_ent *nfs_dns_lookup(struct cache_detail *cd,
162 struct nfs_dns_ent *key)
164 struct cache_head *ch;
166 ch = sunrpc_cache_lookup(cd,
171 return container_of(ch, struct nfs_dns_ent, h);
174 static struct nfs_dns_ent *nfs_dns_update(struct cache_detail *cd,
175 struct nfs_dns_ent *new,
176 struct nfs_dns_ent *key)
178 struct cache_head *ch;
180 ch = sunrpc_cache_update(cd,
185 return container_of(ch, struct nfs_dns_ent, h);
188 static int nfs_dns_parse(struct cache_detail *cd, char *buf, int buflen)
190 char buf1[NFS_DNS_HOSTNAME_MAXLEN+1];
191 struct nfs_dns_ent key, *item;
196 if (buf[buflen-1] != '\n')
198 buf[buflen-1] = '\0';
200 len = qword_get(&buf, buf1, sizeof(buf1));
203 key.addrlen = rpc_pton(buf1, len,
204 (struct sockaddr *)&key.addr,
207 len = qword_get(&buf, buf1, sizeof(buf1));
213 memset(&key.h, 0, sizeof(key.h));
215 ttl = get_expiry(&buf);
218 key.h.expiry_time = ttl + get_seconds();
221 item = nfs_dns_lookup(cd, &key);
225 if (key.addrlen == 0)
226 set_bit(CACHE_NEGATIVE, &key.h.flags);
228 item = nfs_dns_update(cd, &key, item);
233 cache_put(&item->h, cd);
238 static struct cache_detail nfs_dns_resolve = {
239 .owner = THIS_MODULE,
240 .hash_size = NFS_DNS_HASHTBL_SIZE,
241 .hash_table = nfs_dns_table,
242 .name = "dns_resolve",
243 .cache_put = nfs_dns_ent_put,
244 .cache_upcall = nfs_dns_upcall,
245 .cache_parse = nfs_dns_parse,
246 .cache_show = nfs_dns_show,
247 .match = nfs_dns_match,
248 .init = nfs_dns_ent_init,
249 .update = nfs_dns_ent_update,
250 .alloc = nfs_dns_ent_alloc,
253 static int do_cache_lookup(struct cache_detail *cd,
254 struct nfs_dns_ent *key,
255 struct nfs_dns_ent **item,
256 struct nfs_cache_defer_req *dreq)
260 *item = nfs_dns_lookup(cd, key);
262 ret = cache_check(cd, &(*item)->h, &dreq->req);
269 static int do_cache_lookup_nowait(struct cache_detail *cd,
270 struct nfs_dns_ent *key,
271 struct nfs_dns_ent **item)
275 *item = nfs_dns_lookup(cd, key);
279 if (!test_bit(CACHE_VALID, &(*item)->h.flags)
280 || (*item)->h.expiry_time < get_seconds()
281 || cd->flush_time > (*item)->h.last_refresh)
284 if (test_bit(CACHE_NEGATIVE, &(*item)->h.flags))
288 cache_put(&(*item)->h, cd);
294 static int do_cache_lookup_wait(struct cache_detail *cd,
295 struct nfs_dns_ent *key,
296 struct nfs_dns_ent **item)
298 struct nfs_cache_defer_req *dreq;
301 dreq = nfs_cache_defer_req_alloc();
304 ret = do_cache_lookup(cd, key, item, dreq);
305 if (ret == -EAGAIN) {
306 ret = nfs_cache_wait_for_upcall(dreq);
308 ret = do_cache_lookup_nowait(cd, key, item);
310 nfs_cache_defer_req_put(dreq);
315 ssize_t nfs_dns_resolve_name(char *name, size_t namelen,
316 struct sockaddr *sa, size_t salen)
318 struct nfs_dns_ent key = {
322 struct nfs_dns_ent *item = NULL;
325 ret = do_cache_lookup_wait(&nfs_dns_resolve, &key, &item);
327 if (salen >= item->addrlen) {
328 memcpy(sa, &item->addr, item->addrlen);
332 cache_put(&item->h, &nfs_dns_resolve);
333 } else if (ret == -ENOENT)
338 int nfs_dns_resolver_init(void)
340 return nfs_cache_register(&nfs_dns_resolve);
343 void nfs_dns_resolver_destroy(void)
345 nfs_cache_unregister(&nfs_dns_resolve);