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/sunrpc/clnt.h>
12 #include <linux/dns_resolver.h>
14 ssize_t
nfs_dns_resolve_name(struct net
*net
, char *name
, size_t namelen
,
15 struct sockaddr
*sa
, size_t salen
)
21 ip_len
= dns_query(NULL
, name
, namelen
, NULL
, &ip_addr
, NULL
);
23 ret
= rpc_pton(ip_addr
, ip_len
, sa
, salen
);
32 #include <linux/hash.h>
33 #include <linux/string.h>
34 #include <linux/kmod.h>
35 #include <linux/slab.h>
36 #include <linux/module.h>
37 #include <linux/socket.h>
38 #include <linux/seq_file.h>
39 #include <linux/inet.h>
40 #include <linux/sunrpc/clnt.h>
41 #include <linux/sunrpc/cache.h>
42 #include <linux/sunrpc/svcauth.h>
44 #include "dns_resolve.h"
45 #include "cache_lib.h"
48 #define NFS_DNS_HASHBITS 4
49 #define NFS_DNS_HASHTBL_SIZE (1 << NFS_DNS_HASHBITS)
57 struct sockaddr_storage addr
;
62 static void nfs_dns_ent_update(struct cache_head
*cnew
,
63 struct cache_head
*ckey
)
65 struct nfs_dns_ent
*new;
66 struct nfs_dns_ent
*key
;
68 new = container_of(cnew
, struct nfs_dns_ent
, h
);
69 key
= container_of(ckey
, struct nfs_dns_ent
, h
);
71 memcpy(&new->addr
, &key
->addr
, key
->addrlen
);
72 new->addrlen
= key
->addrlen
;
75 static void nfs_dns_ent_init(struct cache_head
*cnew
,
76 struct cache_head
*ckey
)
78 struct nfs_dns_ent
*new;
79 struct nfs_dns_ent
*key
;
81 new = container_of(cnew
, struct nfs_dns_ent
, h
);
82 key
= container_of(ckey
, struct nfs_dns_ent
, h
);
85 new->hostname
= kstrndup(key
->hostname
, key
->namelen
, GFP_KERNEL
);
87 new->namelen
= key
->namelen
;
88 nfs_dns_ent_update(cnew
, ckey
);
95 static void nfs_dns_ent_put(struct kref
*ref
)
97 struct nfs_dns_ent
*item
;
99 item
= container_of(ref
, struct nfs_dns_ent
, h
.ref
);
100 kfree(item
->hostname
);
104 static struct cache_head
*nfs_dns_ent_alloc(void)
106 struct nfs_dns_ent
*item
= kmalloc(sizeof(*item
), GFP_KERNEL
);
109 item
->hostname
= NULL
;
117 static unsigned int nfs_dns_hash(const struct nfs_dns_ent
*key
)
119 return hash_str(key
->hostname
, NFS_DNS_HASHBITS
);
122 static void nfs_dns_request(struct cache_detail
*cd
,
123 struct cache_head
*ch
,
124 char **bpp
, int *blen
)
126 struct nfs_dns_ent
*key
= container_of(ch
, struct nfs_dns_ent
, h
);
128 qword_add(bpp
, blen
, key
->hostname
);
132 static int nfs_dns_upcall(struct cache_detail
*cd
,
133 struct cache_head
*ch
)
135 struct nfs_dns_ent
*key
= container_of(ch
, struct nfs_dns_ent
, h
);
138 ret
= nfs_cache_upcall(cd
, key
->hostname
);
140 ret
= sunrpc_cache_pipe_upcall(cd
, ch
, nfs_dns_request
);
144 static int nfs_dns_match(struct cache_head
*ca
,
145 struct cache_head
*cb
)
147 struct nfs_dns_ent
*a
;
148 struct nfs_dns_ent
*b
;
150 a
= container_of(ca
, struct nfs_dns_ent
, h
);
151 b
= container_of(cb
, struct nfs_dns_ent
, h
);
153 if (a
->namelen
== 0 || a
->namelen
!= b
->namelen
)
155 return memcmp(a
->hostname
, b
->hostname
, a
->namelen
) == 0;
158 static int nfs_dns_show(struct seq_file
*m
, struct cache_detail
*cd
,
159 struct cache_head
*h
)
161 struct nfs_dns_ent
*item
;
165 seq_puts(m
, "# ip address hostname ttl\n");
168 item
= container_of(h
, struct nfs_dns_ent
, h
);
169 ttl
= item
->h
.expiry_time
- seconds_since_boot();
173 if (!test_bit(CACHE_NEGATIVE
, &h
->flags
)) {
174 char buf
[INET6_ADDRSTRLEN
+IPV6_SCOPE_ID_LEN
+1];
176 rpc_ntop((struct sockaddr
*)&item
->addr
, buf
, sizeof(buf
));
177 seq_printf(m
, "%15s ", buf
);
179 seq_puts(m
, "<none> ");
180 seq_printf(m
, "%15s %ld\n", item
->hostname
, ttl
);
184 static struct nfs_dns_ent
*nfs_dns_lookup(struct cache_detail
*cd
,
185 struct nfs_dns_ent
*key
)
187 struct cache_head
*ch
;
189 ch
= sunrpc_cache_lookup(cd
,
194 return container_of(ch
, struct nfs_dns_ent
, h
);
197 static struct nfs_dns_ent
*nfs_dns_update(struct cache_detail
*cd
,
198 struct nfs_dns_ent
*new,
199 struct nfs_dns_ent
*key
)
201 struct cache_head
*ch
;
203 ch
= sunrpc_cache_update(cd
,
208 return container_of(ch
, struct nfs_dns_ent
, h
);
211 static int nfs_dns_parse(struct cache_detail
*cd
, char *buf
, int buflen
)
213 char buf1
[NFS_DNS_HOSTNAME_MAXLEN
+1];
214 struct nfs_dns_ent key
, *item
;
219 if (buf
[buflen
-1] != '\n')
221 buf
[buflen
-1] = '\0';
223 len
= qword_get(&buf
, buf1
, sizeof(buf1
));
226 key
.addrlen
= rpc_pton(buf1
, len
,
227 (struct sockaddr
*)&key
.addr
,
230 len
= qword_get(&buf
, buf1
, sizeof(buf1
));
236 memset(&key
.h
, 0, sizeof(key
.h
));
238 ttl
= get_expiry(&buf
);
241 key
.h
.expiry_time
= ttl
+ seconds_since_boot();
244 item
= nfs_dns_lookup(cd
, &key
);
248 if (key
.addrlen
== 0)
249 set_bit(CACHE_NEGATIVE
, &key
.h
.flags
);
251 item
= nfs_dns_update(cd
, &key
, item
);
256 cache_put(&item
->h
, cd
);
261 static int do_cache_lookup(struct cache_detail
*cd
,
262 struct nfs_dns_ent
*key
,
263 struct nfs_dns_ent
**item
,
264 struct nfs_cache_defer_req
*dreq
)
268 *item
= nfs_dns_lookup(cd
, key
);
270 ret
= cache_check(cd
, &(*item
)->h
, &dreq
->req
);
277 static int do_cache_lookup_nowait(struct cache_detail
*cd
,
278 struct nfs_dns_ent
*key
,
279 struct nfs_dns_ent
**item
)
283 *item
= nfs_dns_lookup(cd
, key
);
287 if (!test_bit(CACHE_VALID
, &(*item
)->h
.flags
)
288 || (*item
)->h
.expiry_time
< seconds_since_boot()
289 || cd
->flush_time
> (*item
)->h
.last_refresh
)
292 if (test_bit(CACHE_NEGATIVE
, &(*item
)->h
.flags
))
296 cache_put(&(*item
)->h
, cd
);
302 static int do_cache_lookup_wait(struct cache_detail
*cd
,
303 struct nfs_dns_ent
*key
,
304 struct nfs_dns_ent
**item
)
306 struct nfs_cache_defer_req
*dreq
;
309 dreq
= nfs_cache_defer_req_alloc();
312 ret
= do_cache_lookup(cd
, key
, item
, dreq
);
313 if (ret
== -EAGAIN
) {
314 ret
= nfs_cache_wait_for_upcall(dreq
);
316 ret
= do_cache_lookup_nowait(cd
, key
, item
);
318 nfs_cache_defer_req_put(dreq
);
323 ssize_t
nfs_dns_resolve_name(struct net
*net
, char *name
,
324 size_t namelen
, struct sockaddr
*sa
, size_t salen
)
326 struct nfs_dns_ent key
= {
330 struct nfs_dns_ent
*item
= NULL
;
332 struct nfs_net
*nn
= net_generic(net
, nfs_net_id
);
334 ret
= do_cache_lookup_wait(nn
->nfs_dns_resolve
, &key
, &item
);
336 if (salen
>= item
->addrlen
) {
337 memcpy(sa
, &item
->addr
, item
->addrlen
);
341 cache_put(&item
->h
, nn
->nfs_dns_resolve
);
342 } else if (ret
== -ENOENT
)
347 int nfs_dns_resolver_cache_init(struct net
*net
)
350 struct nfs_net
*nn
= net_generic(net
, nfs_net_id
);
351 struct cache_detail
*cd
;
352 struct cache_head
**tbl
;
354 cd
= kzalloc(sizeof(struct cache_detail
), GFP_KERNEL
);
358 tbl
= kzalloc(NFS_DNS_HASHTBL_SIZE
* sizeof(struct cache_head
*),
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
,
377 err
= nfs_cache_register_net(net
, cd
);
380 nn
->nfs_dns_resolve
= cd
;
384 nfs_cache_destroy(cd
);
385 kfree(cd
->hash_table
);
392 void nfs_dns_resolver_cache_destroy(struct net
*net
)
394 struct nfs_net
*nn
= net_generic(net
, nfs_net_id
);
395 struct cache_detail
*cd
= nn
->nfs_dns_resolve
;
397 nfs_cache_unregister_net(net
, cd
);
398 nfs_cache_destroy(cd
);
399 kfree(cd
->hash_table
);
403 int nfs_dns_resolver_init(void)
408 void nfs_dns_resolver_destroy(void)
This page took 0.041812 seconds and 5 git commands to generate.