NFS: DNS resolver cache per network namespace context introduced
[deliverable/linux.git] / fs / nfs / dns_resolve.c
1 /*
2 * linux/fs/nfs/dns_resolve.c
3 *
4 * Copyright (c) 2009 Trond Myklebust <Trond.Myklebust@netapp.com>
5 *
6 * Resolves DNS hostnames into valid ip addresses
7 */
8
9 #ifdef CONFIG_NFS_USE_KERNEL_DNS
10
11 #include <linux/sunrpc/clnt.h>
12 #include <linux/dns_resolver.h>
13
14 ssize_t nfs_dns_resolve_name(struct net *net, char *name, size_t namelen,
15 struct sockaddr *sa, size_t salen)
16 {
17 ssize_t ret;
18 char *ip_addr = NULL;
19 int ip_len;
20
21 ip_len = dns_query(NULL, name, namelen, NULL, &ip_addr, NULL);
22 if (ip_len > 0)
23 ret = rpc_pton(ip_addr, ip_len, sa, salen);
24 else
25 ret = -ESRCH;
26 kfree(ip_addr);
27 return ret;
28 }
29
30 #else
31
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>
43
44 #include "dns_resolve.h"
45 #include "cache_lib.h"
46 #include "netns.h"
47
48 #define NFS_DNS_HASHBITS 4
49 #define NFS_DNS_HASHTBL_SIZE (1 << NFS_DNS_HASHBITS)
50
51 struct nfs_dns_ent {
52 struct cache_head h;
53
54 char *hostname;
55 size_t namelen;
56
57 struct sockaddr_storage addr;
58 size_t addrlen;
59 };
60
61
62 static void nfs_dns_ent_update(struct cache_head *cnew,
63 struct cache_head *ckey)
64 {
65 struct nfs_dns_ent *new;
66 struct nfs_dns_ent *key;
67
68 new = container_of(cnew, struct nfs_dns_ent, h);
69 key = container_of(ckey, struct nfs_dns_ent, h);
70
71 memcpy(&new->addr, &key->addr, key->addrlen);
72 new->addrlen = key->addrlen;
73 }
74
75 static void nfs_dns_ent_init(struct cache_head *cnew,
76 struct cache_head *ckey)
77 {
78 struct nfs_dns_ent *new;
79 struct nfs_dns_ent *key;
80
81 new = container_of(cnew, struct nfs_dns_ent, h);
82 key = container_of(ckey, struct nfs_dns_ent, h);
83
84 kfree(new->hostname);
85 new->hostname = kstrndup(key->hostname, key->namelen, GFP_KERNEL);
86 if (new->hostname) {
87 new->namelen = key->namelen;
88 nfs_dns_ent_update(cnew, ckey);
89 } else {
90 new->namelen = 0;
91 new->addrlen = 0;
92 }
93 }
94
95 static void nfs_dns_ent_put(struct kref *ref)
96 {
97 struct nfs_dns_ent *item;
98
99 item = container_of(ref, struct nfs_dns_ent, h.ref);
100 kfree(item->hostname);
101 kfree(item);
102 }
103
104 static struct cache_head *nfs_dns_ent_alloc(void)
105 {
106 struct nfs_dns_ent *item = kmalloc(sizeof(*item), GFP_KERNEL);
107
108 if (item != NULL) {
109 item->hostname = NULL;
110 item->namelen = 0;
111 item->addrlen = 0;
112 return &item->h;
113 }
114 return NULL;
115 };
116
117 static unsigned int nfs_dns_hash(const struct nfs_dns_ent *key)
118 {
119 return hash_str(key->hostname, NFS_DNS_HASHBITS);
120 }
121
122 static void nfs_dns_request(struct cache_detail *cd,
123 struct cache_head *ch,
124 char **bpp, int *blen)
125 {
126 struct nfs_dns_ent *key = container_of(ch, struct nfs_dns_ent, h);
127
128 qword_add(bpp, blen, key->hostname);
129 (*bpp)[-1] = '\n';
130 }
131
132 static int nfs_dns_upcall(struct cache_detail *cd,
133 struct cache_head *ch)
134 {
135 struct nfs_dns_ent *key = container_of(ch, struct nfs_dns_ent, h);
136 int ret;
137
138 ret = nfs_cache_upcall(cd, key->hostname);
139 if (ret)
140 ret = sunrpc_cache_pipe_upcall(cd, ch, nfs_dns_request);
141 return ret;
142 }
143
144 static int nfs_dns_match(struct cache_head *ca,
145 struct cache_head *cb)
146 {
147 struct nfs_dns_ent *a;
148 struct nfs_dns_ent *b;
149
150 a = container_of(ca, struct nfs_dns_ent, h);
151 b = container_of(cb, struct nfs_dns_ent, h);
152
153 if (a->namelen == 0 || a->namelen != b->namelen)
154 return 0;
155 return memcmp(a->hostname, b->hostname, a->namelen) == 0;
156 }
157
158 static int nfs_dns_show(struct seq_file *m, struct cache_detail *cd,
159 struct cache_head *h)
160 {
161 struct nfs_dns_ent *item;
162 long ttl;
163
164 if (h == NULL) {
165 seq_puts(m, "# ip address hostname ttl\n");
166 return 0;
167 }
168 item = container_of(h, struct nfs_dns_ent, h);
169 ttl = item->h.expiry_time - seconds_since_boot();
170 if (ttl < 0)
171 ttl = 0;
172
173 if (!test_bit(CACHE_NEGATIVE, &h->flags)) {
174 char buf[INET6_ADDRSTRLEN+IPV6_SCOPE_ID_LEN+1];
175
176 rpc_ntop((struct sockaddr *)&item->addr, buf, sizeof(buf));
177 seq_printf(m, "%15s ", buf);
178 } else
179 seq_puts(m, "<none> ");
180 seq_printf(m, "%15s %ld\n", item->hostname, ttl);
181 return 0;
182 }
183
184 static struct nfs_dns_ent *nfs_dns_lookup(struct cache_detail *cd,
185 struct nfs_dns_ent *key)
186 {
187 struct cache_head *ch;
188
189 ch = sunrpc_cache_lookup(cd,
190 &key->h,
191 nfs_dns_hash(key));
192 if (!ch)
193 return NULL;
194 return container_of(ch, struct nfs_dns_ent, h);
195 }
196
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)
200 {
201 struct cache_head *ch;
202
203 ch = sunrpc_cache_update(cd,
204 &new->h, &key->h,
205 nfs_dns_hash(key));
206 if (!ch)
207 return NULL;
208 return container_of(ch, struct nfs_dns_ent, h);
209 }
210
211 static int nfs_dns_parse(struct cache_detail *cd, char *buf, int buflen)
212 {
213 char buf1[NFS_DNS_HOSTNAME_MAXLEN+1];
214 struct nfs_dns_ent key, *item;
215 unsigned long ttl;
216 ssize_t len;
217 int ret = -EINVAL;
218
219 if (buf[buflen-1] != '\n')
220 goto out;
221 buf[buflen-1] = '\0';
222
223 len = qword_get(&buf, buf1, sizeof(buf1));
224 if (len <= 0)
225 goto out;
226 key.addrlen = rpc_pton(buf1, len,
227 (struct sockaddr *)&key.addr,
228 sizeof(key.addr));
229
230 len = qword_get(&buf, buf1, sizeof(buf1));
231 if (len <= 0)
232 goto out;
233
234 key.hostname = buf1;
235 key.namelen = len;
236 memset(&key.h, 0, sizeof(key.h));
237
238 ttl = get_expiry(&buf);
239 if (ttl == 0)
240 goto out;
241 key.h.expiry_time = ttl + seconds_since_boot();
242
243 ret = -ENOMEM;
244 item = nfs_dns_lookup(cd, &key);
245 if (item == NULL)
246 goto out;
247
248 if (key.addrlen == 0)
249 set_bit(CACHE_NEGATIVE, &key.h.flags);
250
251 item = nfs_dns_update(cd, &key, item);
252 if (item == NULL)
253 goto out;
254
255 ret = 0;
256 cache_put(&item->h, cd);
257 out:
258 return ret;
259 }
260
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)
265 {
266 int ret = -ENOMEM;
267
268 *item = nfs_dns_lookup(cd, key);
269 if (*item) {
270 ret = cache_check(cd, &(*item)->h, &dreq->req);
271 if (ret)
272 *item = NULL;
273 }
274 return ret;
275 }
276
277 static int do_cache_lookup_nowait(struct cache_detail *cd,
278 struct nfs_dns_ent *key,
279 struct nfs_dns_ent **item)
280 {
281 int ret = -ENOMEM;
282
283 *item = nfs_dns_lookup(cd, key);
284 if (!*item)
285 goto out_err;
286 ret = -ETIMEDOUT;
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)
290 goto out_put;
291 ret = -ENOENT;
292 if (test_bit(CACHE_NEGATIVE, &(*item)->h.flags))
293 goto out_put;
294 return 0;
295 out_put:
296 cache_put(&(*item)->h, cd);
297 out_err:
298 *item = NULL;
299 return ret;
300 }
301
302 static int do_cache_lookup_wait(struct cache_detail *cd,
303 struct nfs_dns_ent *key,
304 struct nfs_dns_ent **item)
305 {
306 struct nfs_cache_defer_req *dreq;
307 int ret = -ENOMEM;
308
309 dreq = nfs_cache_defer_req_alloc();
310 if (!dreq)
311 goto out;
312 ret = do_cache_lookup(cd, key, item, dreq);
313 if (ret == -EAGAIN) {
314 ret = nfs_cache_wait_for_upcall(dreq);
315 if (!ret)
316 ret = do_cache_lookup_nowait(cd, key, item);
317 }
318 nfs_cache_defer_req_put(dreq);
319 out:
320 return ret;
321 }
322
323 ssize_t nfs_dns_resolve_name(struct net *net, char *name,
324 size_t namelen, struct sockaddr *sa, size_t salen)
325 {
326 struct nfs_dns_ent key = {
327 .hostname = name,
328 .namelen = namelen,
329 };
330 struct nfs_dns_ent *item = NULL;
331 ssize_t ret;
332 struct nfs_net *nn = net_generic(net, nfs_net_id);
333
334 ret = do_cache_lookup_wait(nn->nfs_dns_resolve, &key, &item);
335 if (ret == 0) {
336 if (salen >= item->addrlen) {
337 memcpy(sa, &item->addr, item->addrlen);
338 ret = item->addrlen;
339 } else
340 ret = -EOVERFLOW;
341 cache_put(&item->h, nn->nfs_dns_resolve);
342 } else if (ret == -ENOENT)
343 ret = -ESRCH;
344 return ret;
345 }
346
347 int nfs_dns_resolver_cache_init(struct net *net)
348 {
349 int err = -ENOMEM;
350 struct nfs_net *nn = net_generic(net, nfs_net_id);
351 struct cache_detail *cd;
352 struct cache_head **tbl;
353
354 cd = kzalloc(sizeof(struct cache_detail), GFP_KERNEL);
355 if (cd == NULL)
356 goto err_cd;
357
358 tbl = kzalloc(NFS_DNS_HASHTBL_SIZE * sizeof(struct cache_head *),
359 GFP_KERNEL);
360 if (tbl == NULL)
361 goto err_tbl;
362
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,
375
376 nfs_cache_init(cd);
377 err = nfs_cache_register_net(net, cd);
378 if (err)
379 goto err_reg;
380 nn->nfs_dns_resolve = cd;
381 return 0;
382
383 err_reg:
384 nfs_cache_destroy(cd);
385 kfree(cd->hash_table);
386 err_tbl:
387 kfree(cd);
388 err_cd:
389 return err;
390 }
391
392 void nfs_dns_resolver_cache_destroy(struct net *net)
393 {
394 struct nfs_net *nn = net_generic(net, nfs_net_id);
395 struct cache_detail *cd = nn->nfs_dns_resolve;
396
397 nfs_cache_unregister_net(net, cd);
398 nfs_cache_destroy(cd);
399 kfree(cd->hash_table);
400 kfree(cd);
401 }
402
403 int nfs_dns_resolver_init(void)
404 {
405 return 0;
406 }
407
408 void nfs_dns_resolver_destroy(void)
409 {
410 }
411 #endif
This page took 0.041812 seconds and 5 git commands to generate.