]> git.proxmox.com Git - mirror_ubuntu-bionic-kernel.git/blame - fs/nfs/dns_resolve.c
iwlwifi: don't include iwl-dev.h from iwl-devtrace.h
[mirror_ubuntu-bionic-kernel.git] / fs / nfs / dns_resolve.c
CommitLineData
e571cbf1
TM
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#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>
19
20#include "dns_resolve.h"
21#include "cache_lib.h"
22
23#define NFS_DNS_HASHBITS 4
24#define NFS_DNS_HASHTBL_SIZE (1 << NFS_DNS_HASHBITS)
25
26static struct cache_head *nfs_dns_table[NFS_DNS_HASHTBL_SIZE];
27
28struct nfs_dns_ent {
29 struct cache_head h;
30
31 char *hostname;
32 size_t namelen;
33
34 struct sockaddr_storage addr;
35 size_t addrlen;
36};
37
38
ebed9203
TM
39static void nfs_dns_ent_update(struct cache_head *cnew,
40 struct cache_head *ckey)
41{
42 struct nfs_dns_ent *new;
43 struct nfs_dns_ent *key;
44
45 new = container_of(cnew, struct nfs_dns_ent, h);
46 key = container_of(ckey, struct nfs_dns_ent, h);
47
48 memcpy(&new->addr, &key->addr, key->addrlen);
49 new->addrlen = key->addrlen;
50}
51
e571cbf1
TM
52static void nfs_dns_ent_init(struct cache_head *cnew,
53 struct cache_head *ckey)
54{
55 struct nfs_dns_ent *new;
56 struct nfs_dns_ent *key;
57
58 new = container_of(cnew, struct nfs_dns_ent, h);
59 key = container_of(ckey, struct nfs_dns_ent, h);
60
61 kfree(new->hostname);
62 new->hostname = kstrndup(key->hostname, key->namelen, GFP_KERNEL);
63 if (new->hostname) {
64 new->namelen = key->namelen;
ebed9203 65 nfs_dns_ent_update(cnew, ckey);
e571cbf1
TM
66 } else {
67 new->namelen = 0;
68 new->addrlen = 0;
69 }
70}
71
72static void nfs_dns_ent_put(struct kref *ref)
73{
74 struct nfs_dns_ent *item;
75
76 item = container_of(ref, struct nfs_dns_ent, h.ref);
77 kfree(item->hostname);
78 kfree(item);
79}
80
81static struct cache_head *nfs_dns_ent_alloc(void)
82{
83 struct nfs_dns_ent *item = kmalloc(sizeof(*item), GFP_KERNEL);
84
85 if (item != NULL) {
86 item->hostname = NULL;
87 item->namelen = 0;
88 item->addrlen = 0;
89 return &item->h;
90 }
91 return NULL;
92};
93
94static unsigned int nfs_dns_hash(const struct nfs_dns_ent *key)
95{
96 return hash_str(key->hostname, NFS_DNS_HASHBITS);
97}
98
99static void nfs_dns_request(struct cache_detail *cd,
100 struct cache_head *ch,
101 char **bpp, int *blen)
102{
103 struct nfs_dns_ent *key = container_of(ch, struct nfs_dns_ent, h);
104
105 qword_add(bpp, blen, key->hostname);
106 (*bpp)[-1] = '\n';
107}
108
109static int nfs_dns_upcall(struct cache_detail *cd,
110 struct cache_head *ch)
111{
112 struct nfs_dns_ent *key = container_of(ch, struct nfs_dns_ent, h);
113 int ret;
114
115 ret = nfs_cache_upcall(cd, key->hostname);
116 if (ret)
117 ret = sunrpc_cache_pipe_upcall(cd, ch, nfs_dns_request);
118 return ret;
119}
120
121static int nfs_dns_match(struct cache_head *ca,
122 struct cache_head *cb)
123{
124 struct nfs_dns_ent *a;
125 struct nfs_dns_ent *b;
126
127 a = container_of(ca, struct nfs_dns_ent, h);
128 b = container_of(cb, struct nfs_dns_ent, h);
129
130 if (a->namelen == 0 || a->namelen != b->namelen)
131 return 0;
132 return memcmp(a->hostname, b->hostname, a->namelen) == 0;
133}
134
135static int nfs_dns_show(struct seq_file *m, struct cache_detail *cd,
136 struct cache_head *h)
137{
138 struct nfs_dns_ent *item;
139 long ttl;
140
141 if (h == NULL) {
142 seq_puts(m, "# ip address hostname ttl\n");
143 return 0;
144 }
145 item = container_of(h, struct nfs_dns_ent, h);
146 ttl = (long)item->h.expiry_time - (long)get_seconds();
147 if (ttl < 0)
148 ttl = 0;
149
150 if (!test_bit(CACHE_NEGATIVE, &h->flags)) {
151 char buf[INET6_ADDRSTRLEN+IPV6_SCOPE_ID_LEN+1];
152
153 rpc_ntop((struct sockaddr *)&item->addr, buf, sizeof(buf));
154 seq_printf(m, "%15s ", buf);
155 } else
156 seq_puts(m, "<none> ");
157 seq_printf(m, "%15s %ld\n", item->hostname, ttl);
158 return 0;
159}
160
0a6566ec 161static struct nfs_dns_ent *nfs_dns_lookup(struct cache_detail *cd,
e571cbf1
TM
162 struct nfs_dns_ent *key)
163{
164 struct cache_head *ch;
165
166 ch = sunrpc_cache_lookup(cd,
167 &key->h,
168 nfs_dns_hash(key));
169 if (!ch)
170 return NULL;
171 return container_of(ch, struct nfs_dns_ent, h);
172}
173
0a6566ec 174static struct nfs_dns_ent *nfs_dns_update(struct cache_detail *cd,
e571cbf1
TM
175 struct nfs_dns_ent *new,
176 struct nfs_dns_ent *key)
177{
178 struct cache_head *ch;
179
180 ch = sunrpc_cache_update(cd,
181 &new->h, &key->h,
182 nfs_dns_hash(key));
183 if (!ch)
184 return NULL;
185 return container_of(ch, struct nfs_dns_ent, h);
186}
187
188static int nfs_dns_parse(struct cache_detail *cd, char *buf, int buflen)
189{
190 char buf1[NFS_DNS_HOSTNAME_MAXLEN+1];
191 struct nfs_dns_ent key, *item;
192 unsigned long ttl;
193 ssize_t len;
194 int ret = -EINVAL;
195
196 if (buf[buflen-1] != '\n')
197 goto out;
198 buf[buflen-1] = '\0';
199
200 len = qword_get(&buf, buf1, sizeof(buf1));
201 if (len <= 0)
202 goto out;
203 key.addrlen = rpc_pton(buf1, len,
204 (struct sockaddr *)&key.addr,
205 sizeof(key.addr));
206
207 len = qword_get(&buf, buf1, sizeof(buf1));
208 if (len <= 0)
209 goto out;
210
211 key.hostname = buf1;
212 key.namelen = len;
213 memset(&key.h, 0, sizeof(key.h));
214
215 ttl = get_expiry(&buf);
216 if (ttl == 0)
217 goto out;
218 key.h.expiry_time = ttl + get_seconds();
219
220 ret = -ENOMEM;
221 item = nfs_dns_lookup(cd, &key);
222 if (item == NULL)
223 goto out;
224
225 if (key.addrlen == 0)
226 set_bit(CACHE_NEGATIVE, &key.h.flags);
227
228 item = nfs_dns_update(cd, &key, item);
229 if (item == NULL)
230 goto out;
231
232 ret = 0;
233 cache_put(&item->h, cd);
234out:
235 return ret;
236}
237
238static 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,
ebed9203 249 .update = nfs_dns_ent_update,
e571cbf1
TM
250 .alloc = nfs_dns_ent_alloc,
251};
252
253static 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)
257{
258 int ret = -ENOMEM;
259
260 *item = nfs_dns_lookup(cd, key);
261 if (*item) {
262 ret = cache_check(cd, &(*item)->h, &dreq->req);
263 if (ret)
264 *item = NULL;
265 }
266 return ret;
267}
268
269static int do_cache_lookup_nowait(struct cache_detail *cd,
270 struct nfs_dns_ent *key,
271 struct nfs_dns_ent **item)
272{
273 int ret = -ENOMEM;
274
275 *item = nfs_dns_lookup(cd, key);
276 if (!*item)
277 goto out_err;
278 ret = -ETIMEDOUT;
279 if (!test_bit(CACHE_VALID, &(*item)->h.flags)
280 || (*item)->h.expiry_time < get_seconds()
281 || cd->flush_time > (*item)->h.last_refresh)
282 goto out_put;
283 ret = -ENOENT;
284 if (test_bit(CACHE_NEGATIVE, &(*item)->h.flags))
285 goto out_put;
286 return 0;
287out_put:
288 cache_put(&(*item)->h, cd);
289out_err:
290 *item = NULL;
291 return ret;
292}
293
294static int do_cache_lookup_wait(struct cache_detail *cd,
295 struct nfs_dns_ent *key,
296 struct nfs_dns_ent **item)
297{
298 struct nfs_cache_defer_req *dreq;
299 int ret = -ENOMEM;
300
301 dreq = nfs_cache_defer_req_alloc();
302 if (!dreq)
303 goto out;
304 ret = do_cache_lookup(cd, key, item, dreq);
305 if (ret == -EAGAIN) {
306 ret = nfs_cache_wait_for_upcall(dreq);
307 if (!ret)
308 ret = do_cache_lookup_nowait(cd, key, item);
309 }
310 nfs_cache_defer_req_put(dreq);
311out:
312 return ret;
313}
314
315ssize_t nfs_dns_resolve_name(char *name, size_t namelen,
316 struct sockaddr *sa, size_t salen)
317{
318 struct nfs_dns_ent key = {
319 .hostname = name,
320 .namelen = namelen,
321 };
322 struct nfs_dns_ent *item = NULL;
323 ssize_t ret;
324
325 ret = do_cache_lookup_wait(&nfs_dns_resolve, &key, &item);
326 if (ret == 0) {
327 if (salen >= item->addrlen) {
328 memcpy(sa, &item->addr, item->addrlen);
329 ret = item->addrlen;
330 } else
331 ret = -EOVERFLOW;
332 cache_put(&item->h, &nfs_dns_resolve);
333 } else if (ret == -ENOENT)
334 ret = -ESRCH;
335 return ret;
336}
337
338int nfs_dns_resolver_init(void)
339{
340 return nfs_cache_register(&nfs_dns_resolve);
341}
342
343void nfs_dns_resolver_destroy(void)
344{
345 nfs_cache_unregister(&nfs_dns_resolve);
346}
347