1b2441318SGreg Kroah-Hartman // SPDX-License-Identifier: GPL-2.0
2e571cbf1STrond Myklebust /*
3e571cbf1STrond Myklebust * linux/fs/nfs/dns_resolve.c
4e571cbf1STrond Myklebust *
5e571cbf1STrond Myklebust * Copyright (c) 2009 Trond Myklebust <Trond.Myklebust@netapp.com>
6e571cbf1STrond Myklebust *
7e571cbf1STrond Myklebust * Resolves DNS hostnames into valid ip addresses
8e571cbf1STrond Myklebust */
9e571cbf1STrond Myklebust
1089d77c8fSBryan Schumaker #include <linux/module.h>
11c2e8139cSBryan Schumaker #include <linux/sunrpc/clnt.h>
125976687aSJeff Layton #include <linux/sunrpc/addr.h>
13*08be82baSGUO Zihua
1417280175STrond Myklebust #include "dns_resolve.h"
15c2e8139cSBryan Schumaker
16*08be82baSGUO Zihua #ifdef CONFIG_NFS_USE_KERNEL_DNS
17*08be82baSGUO Zihua
18*08be82baSGUO Zihua #include <linux/dns_resolver.h>
19*08be82baSGUO Zihua
nfs_dns_resolve_name(struct net * net,char * name,size_t namelen,struct sockaddr_storage * ss,size_t salen)201b340d01SStanislav Kinsbursky ssize_t nfs_dns_resolve_name(struct net *net, char *name, size_t namelen,
21cf0d7e7fSKees Cook struct sockaddr_storage *ss, size_t salen)
22c2e8139cSBryan Schumaker {
23cf0d7e7fSKees Cook struct sockaddr *sa = (struct sockaddr *)ss;
24c2e8139cSBryan Schumaker ssize_t ret;
25c2e8139cSBryan Schumaker char *ip_addr = NULL;
26c2e8139cSBryan Schumaker int ip_len;
27c2e8139cSBryan Schumaker
28a58946c1SDavid Howells ip_len = dns_query(net, NULL, name, namelen, NULL, &ip_addr, NULL,
29a58946c1SDavid Howells false);
30c2e8139cSBryan Schumaker if (ip_len > 0)
31bc224f53SStanislav Kinsbursky ret = rpc_pton(net, ip_addr, ip_len, sa, salen);
32c2e8139cSBryan Schumaker else
33c2e8139cSBryan Schumaker ret = -ESRCH;
34c2e8139cSBryan Schumaker kfree(ip_addr);
35c2e8139cSBryan Schumaker return ret;
36c2e8139cSBryan Schumaker }
37c2e8139cSBryan Schumaker
38c2e8139cSBryan Schumaker #else
39c2e8139cSBryan Schumaker
40e571cbf1STrond Myklebust #include <linux/hash.h>
41e571cbf1STrond Myklebust #include <linux/string.h>
42e571cbf1STrond Myklebust #include <linux/kmod.h>
435a0e3ad6STejun Heo #include <linux/slab.h>
44e571cbf1STrond Myklebust #include <linux/socket.h>
45e571cbf1STrond Myklebust #include <linux/seq_file.h>
46e571cbf1STrond Myklebust #include <linux/inet.h>
47e571cbf1STrond Myklebust #include <linux/sunrpc/cache.h>
48e571cbf1STrond Myklebust #include <linux/sunrpc/svcauth.h>
499df69c81SStanislav Kinsbursky #include <linux/sunrpc/rpc_pipe_fs.h>
50694e096fSAnna Schumaker #include <linux/nfs_fs.h>
51e571cbf1STrond Myklebust
52694e096fSAnna Schumaker #include "nfs4_fs.h"
53e571cbf1STrond Myklebust #include "cache_lib.h"
541b340d01SStanislav Kinsbursky #include "netns.h"
55e571cbf1STrond Myklebust
56e571cbf1STrond Myklebust #define NFS_DNS_HASHBITS 4
57e571cbf1STrond Myklebust #define NFS_DNS_HASHTBL_SIZE (1 << NFS_DNS_HASHBITS)
58e571cbf1STrond Myklebust
59e571cbf1STrond Myklebust struct nfs_dns_ent {
60e571cbf1STrond Myklebust struct cache_head h;
61e571cbf1STrond Myklebust
62e571cbf1STrond Myklebust char *hostname;
63e571cbf1STrond Myklebust size_t namelen;
64e571cbf1STrond Myklebust
65e571cbf1STrond Myklebust struct sockaddr_storage addr;
66e571cbf1STrond Myklebust size_t addrlen;
67437f9145STrond Myklebust struct rcu_head rcu_head;
68e571cbf1STrond Myklebust };
69e571cbf1STrond Myklebust
70e571cbf1STrond Myklebust
nfs_dns_ent_update(struct cache_head * cnew,struct cache_head * ckey)71ebed9203STrond Myklebust static void nfs_dns_ent_update(struct cache_head *cnew,
72ebed9203STrond Myklebust struct cache_head *ckey)
73ebed9203STrond Myklebust {
74ebed9203STrond Myklebust struct nfs_dns_ent *new;
75ebed9203STrond Myklebust struct nfs_dns_ent *key;
76ebed9203STrond Myklebust
77ebed9203STrond Myklebust new = container_of(cnew, struct nfs_dns_ent, h);
78ebed9203STrond Myklebust key = container_of(ckey, struct nfs_dns_ent, h);
79ebed9203STrond Myklebust
80ebed9203STrond Myklebust memcpy(&new->addr, &key->addr, key->addrlen);
81ebed9203STrond Myklebust new->addrlen = key->addrlen;
82ebed9203STrond Myklebust }
83ebed9203STrond Myklebust
nfs_dns_ent_init(struct cache_head * cnew,struct cache_head * ckey)84e571cbf1STrond Myklebust static void nfs_dns_ent_init(struct cache_head *cnew,
85e571cbf1STrond Myklebust struct cache_head *ckey)
86e571cbf1STrond Myklebust {
87e571cbf1STrond Myklebust struct nfs_dns_ent *new;
88e571cbf1STrond Myklebust struct nfs_dns_ent *key;
89e571cbf1STrond Myklebust
90e571cbf1STrond Myklebust new = container_of(cnew, struct nfs_dns_ent, h);
91e571cbf1STrond Myklebust key = container_of(ckey, struct nfs_dns_ent, h);
92e571cbf1STrond Myklebust
93e571cbf1STrond Myklebust kfree(new->hostname);
94a8bd9ddfSTrond Myklebust new->hostname = kmemdup_nul(key->hostname, key->namelen, GFP_KERNEL);
95e571cbf1STrond Myklebust if (new->hostname) {
96e571cbf1STrond Myklebust new->namelen = key->namelen;
97ebed9203STrond Myklebust nfs_dns_ent_update(cnew, ckey);
98e571cbf1STrond Myklebust } else {
99e571cbf1STrond Myklebust new->namelen = 0;
100e571cbf1STrond Myklebust new->addrlen = 0;
101e571cbf1STrond Myklebust }
102e571cbf1STrond Myklebust }
103e571cbf1STrond Myklebust
nfs_dns_ent_free_rcu(struct rcu_head * head)104437f9145STrond Myklebust static void nfs_dns_ent_free_rcu(struct rcu_head *head)
105437f9145STrond Myklebust {
106437f9145STrond Myklebust struct nfs_dns_ent *item;
107437f9145STrond Myklebust
108437f9145STrond Myklebust item = container_of(head, struct nfs_dns_ent, rcu_head);
109437f9145STrond Myklebust kfree(item->hostname);
110437f9145STrond Myklebust kfree(item);
111437f9145STrond Myklebust }
112437f9145STrond Myklebust
nfs_dns_ent_put(struct kref * ref)113e571cbf1STrond Myklebust static void nfs_dns_ent_put(struct kref *ref)
114e571cbf1STrond Myklebust {
115e571cbf1STrond Myklebust struct nfs_dns_ent *item;
116e571cbf1STrond Myklebust
117e571cbf1STrond Myklebust item = container_of(ref, struct nfs_dns_ent, h.ref);
118a6482733STrond Myklebust call_rcu(&item->rcu_head, nfs_dns_ent_free_rcu);
119e571cbf1STrond Myklebust }
120e571cbf1STrond Myklebust
nfs_dns_ent_alloc(void)121e571cbf1STrond Myklebust static struct cache_head *nfs_dns_ent_alloc(void)
122e571cbf1STrond Myklebust {
123e571cbf1STrond Myklebust struct nfs_dns_ent *item = kmalloc(sizeof(*item), GFP_KERNEL);
124e571cbf1STrond Myklebust
125e571cbf1STrond Myklebust if (item != NULL) {
126e571cbf1STrond Myklebust item->hostname = NULL;
127e571cbf1STrond Myklebust item->namelen = 0;
128e571cbf1STrond Myklebust item->addrlen = 0;
129e571cbf1STrond Myklebust return &item->h;
130e571cbf1STrond Myklebust }
131e571cbf1STrond Myklebust return NULL;
132e571cbf1STrond Myklebust };
133e571cbf1STrond Myklebust
nfs_dns_hash(const struct nfs_dns_ent * key)134e571cbf1STrond Myklebust static unsigned int nfs_dns_hash(const struct nfs_dns_ent *key)
135e571cbf1STrond Myklebust {
136e571cbf1STrond Myklebust return hash_str(key->hostname, NFS_DNS_HASHBITS);
137e571cbf1STrond Myklebust }
138e571cbf1STrond Myklebust
nfs_dns_request(struct cache_detail * cd,struct cache_head * ch,char ** bpp,int * blen)139e571cbf1STrond Myklebust static void nfs_dns_request(struct cache_detail *cd,
140e571cbf1STrond Myklebust struct cache_head *ch,
141e571cbf1STrond Myklebust char **bpp, int *blen)
142e571cbf1STrond Myklebust {
143e571cbf1STrond Myklebust struct nfs_dns_ent *key = container_of(ch, struct nfs_dns_ent, h);
144e571cbf1STrond Myklebust
145e571cbf1STrond Myklebust qword_add(bpp, blen, key->hostname);
146e571cbf1STrond Myklebust (*bpp)[-1] = '\n';
147e571cbf1STrond Myklebust }
148e571cbf1STrond Myklebust
nfs_dns_upcall(struct cache_detail * cd,struct cache_head * ch)149e571cbf1STrond Myklebust static int nfs_dns_upcall(struct cache_detail *cd,
150e571cbf1STrond Myklebust struct cache_head *ch)
151e571cbf1STrond Myklebust {
152e571cbf1STrond Myklebust struct nfs_dns_ent *key = container_of(ch, struct nfs_dns_ent, h);
153e571cbf1STrond Myklebust
15465286b88STrond Myklebust if (test_and_set_bit(CACHE_PENDING, &ch->flags))
15565286b88STrond Myklebust return 0;
15665286b88STrond Myklebust if (!nfs_cache_upcall(cd, key->hostname))
15765286b88STrond Myklebust return 0;
15865286b88STrond Myklebust clear_bit(CACHE_PENDING, &ch->flags);
15965286b88STrond Myklebust return sunrpc_cache_pipe_upcall_timeout(cd, ch);
160e571cbf1STrond Myklebust }
161e571cbf1STrond Myklebust
nfs_dns_match(struct cache_head * ca,struct cache_head * cb)162e571cbf1STrond Myklebust static int nfs_dns_match(struct cache_head *ca,
163e571cbf1STrond Myklebust struct cache_head *cb)
164e571cbf1STrond Myklebust {
165e571cbf1STrond Myklebust struct nfs_dns_ent *a;
166e571cbf1STrond Myklebust struct nfs_dns_ent *b;
167e571cbf1STrond Myklebust
168e571cbf1STrond Myklebust a = container_of(ca, struct nfs_dns_ent, h);
169e571cbf1STrond Myklebust b = container_of(cb, struct nfs_dns_ent, h);
170e571cbf1STrond Myklebust
171e571cbf1STrond Myklebust if (a->namelen == 0 || a->namelen != b->namelen)
172e571cbf1STrond Myklebust return 0;
173e571cbf1STrond Myklebust return memcmp(a->hostname, b->hostname, a->namelen) == 0;
174e571cbf1STrond Myklebust }
175e571cbf1STrond Myklebust
nfs_dns_show(struct seq_file * m,struct cache_detail * cd,struct cache_head * h)176e571cbf1STrond Myklebust static int nfs_dns_show(struct seq_file *m, struct cache_detail *cd,
177e571cbf1STrond Myklebust struct cache_head *h)
178e571cbf1STrond Myklebust {
179e571cbf1STrond Myklebust struct nfs_dns_ent *item;
180e571cbf1STrond Myklebust long ttl;
181e571cbf1STrond Myklebust
182e571cbf1STrond Myklebust if (h == NULL) {
183e571cbf1STrond Myklebust seq_puts(m, "# ip address hostname ttl\n");
184e571cbf1STrond Myklebust return 0;
185e571cbf1STrond Myklebust }
186e571cbf1STrond Myklebust item = container_of(h, struct nfs_dns_ent, h);
187c5b29f88SNeilBrown ttl = item->h.expiry_time - seconds_since_boot();
188e571cbf1STrond Myklebust if (ttl < 0)
189e571cbf1STrond Myklebust ttl = 0;
190e571cbf1STrond Myklebust
191e571cbf1STrond Myklebust if (!test_bit(CACHE_NEGATIVE, &h->flags)) {
192e571cbf1STrond Myklebust char buf[INET6_ADDRSTRLEN+IPV6_SCOPE_ID_LEN+1];
193e571cbf1STrond Myklebust
194e571cbf1STrond Myklebust rpc_ntop((struct sockaddr *)&item->addr, buf, sizeof(buf));
195e571cbf1STrond Myklebust seq_printf(m, "%15s ", buf);
196e571cbf1STrond Myklebust } else
197e571cbf1STrond Myklebust seq_puts(m, "<none> ");
198e571cbf1STrond Myklebust seq_printf(m, "%15s %ld\n", item->hostname, ttl);
199e571cbf1STrond Myklebust return 0;
200e571cbf1STrond Myklebust }
201e571cbf1STrond Myklebust
nfs_dns_lookup(struct cache_detail * cd,struct nfs_dns_ent * key)2020a6566ecSTrond Myklebust static struct nfs_dns_ent *nfs_dns_lookup(struct cache_detail *cd,
203e571cbf1STrond Myklebust struct nfs_dns_ent *key)
204e571cbf1STrond Myklebust {
205e571cbf1STrond Myklebust struct cache_head *ch;
206e571cbf1STrond Myklebust
207437f9145STrond Myklebust ch = sunrpc_cache_lookup_rcu(cd,
208e571cbf1STrond Myklebust &key->h,
209e571cbf1STrond Myklebust nfs_dns_hash(key));
210e571cbf1STrond Myklebust if (!ch)
211e571cbf1STrond Myklebust return NULL;
212e571cbf1STrond Myklebust return container_of(ch, struct nfs_dns_ent, h);
213e571cbf1STrond Myklebust }
214e571cbf1STrond Myklebust
nfs_dns_update(struct cache_detail * cd,struct nfs_dns_ent * new,struct nfs_dns_ent * key)2150a6566ecSTrond Myklebust static struct nfs_dns_ent *nfs_dns_update(struct cache_detail *cd,
216e571cbf1STrond Myklebust struct nfs_dns_ent *new,
217e571cbf1STrond Myklebust struct nfs_dns_ent *key)
218e571cbf1STrond Myklebust {
219e571cbf1STrond Myklebust struct cache_head *ch;
220e571cbf1STrond Myklebust
221e571cbf1STrond Myklebust ch = sunrpc_cache_update(cd,
222e571cbf1STrond Myklebust &new->h, &key->h,
223e571cbf1STrond Myklebust nfs_dns_hash(key));
224e571cbf1STrond Myklebust if (!ch)
225e571cbf1STrond Myklebust return NULL;
226e571cbf1STrond Myklebust return container_of(ch, struct nfs_dns_ent, h);
227e571cbf1STrond Myklebust }
228e571cbf1STrond Myklebust
nfs_dns_parse(struct cache_detail * cd,char * buf,int buflen)229e571cbf1STrond Myklebust static int nfs_dns_parse(struct cache_detail *cd, char *buf, int buflen)
230e571cbf1STrond Myklebust {
231e571cbf1STrond Myklebust char buf1[NFS_DNS_HOSTNAME_MAXLEN+1];
232e571cbf1STrond Myklebust struct nfs_dns_ent key, *item;
2338d96b106SNeilBrown unsigned int ttl;
234e571cbf1STrond Myklebust ssize_t len;
235e571cbf1STrond Myklebust int ret = -EINVAL;
236e571cbf1STrond Myklebust
237e571cbf1STrond Myklebust if (buf[buflen-1] != '\n')
238e571cbf1STrond Myklebust goto out;
239e571cbf1STrond Myklebust buf[buflen-1] = '\0';
240e571cbf1STrond Myklebust
241e571cbf1STrond Myklebust len = qword_get(&buf, buf1, sizeof(buf1));
242e571cbf1STrond Myklebust if (len <= 0)
243e571cbf1STrond Myklebust goto out;
244599ec129SStanislav Kinsbursky key.addrlen = rpc_pton(cd->net, buf1, len,
245e571cbf1STrond Myklebust (struct sockaddr *)&key.addr,
246e571cbf1STrond Myklebust sizeof(key.addr));
247e571cbf1STrond Myklebust
248e571cbf1STrond Myklebust len = qword_get(&buf, buf1, sizeof(buf1));
249e571cbf1STrond Myklebust if (len <= 0)
250e571cbf1STrond Myklebust goto out;
251e571cbf1STrond Myklebust
252e571cbf1STrond Myklebust key.hostname = buf1;
253e571cbf1STrond Myklebust key.namelen = len;
254e571cbf1STrond Myklebust memset(&key.h, 0, sizeof(key.h));
255e571cbf1STrond Myklebust
2568d96b106SNeilBrown if (get_uint(&buf, &ttl) < 0)
2578d96b106SNeilBrown goto out;
258e571cbf1STrond Myklebust if (ttl == 0)
259e571cbf1STrond Myklebust goto out;
260c5b29f88SNeilBrown key.h.expiry_time = ttl + seconds_since_boot();
261e571cbf1STrond Myklebust
262e571cbf1STrond Myklebust ret = -ENOMEM;
263e571cbf1STrond Myklebust item = nfs_dns_lookup(cd, &key);
264e571cbf1STrond Myklebust if (item == NULL)
265e571cbf1STrond Myklebust goto out;
266e571cbf1STrond Myklebust
267e571cbf1STrond Myklebust if (key.addrlen == 0)
268e571cbf1STrond Myklebust set_bit(CACHE_NEGATIVE, &key.h.flags);
269e571cbf1STrond Myklebust
270e571cbf1STrond Myklebust item = nfs_dns_update(cd, &key, item);
271e571cbf1STrond Myklebust if (item == NULL)
272e571cbf1STrond Myklebust goto out;
273e571cbf1STrond Myklebust
274e571cbf1STrond Myklebust ret = 0;
275e571cbf1STrond Myklebust cache_put(&item->h, cd);
276e571cbf1STrond Myklebust out:
277e571cbf1STrond Myklebust return ret;
278e571cbf1STrond Myklebust }
279e571cbf1STrond Myklebust
do_cache_lookup(struct cache_detail * cd,struct nfs_dns_ent * key,struct nfs_dns_ent ** item,struct nfs_cache_defer_req * dreq)280e571cbf1STrond Myklebust static int do_cache_lookup(struct cache_detail *cd,
281e571cbf1STrond Myklebust struct nfs_dns_ent *key,
282e571cbf1STrond Myklebust struct nfs_dns_ent **item,
283e571cbf1STrond Myklebust struct nfs_cache_defer_req *dreq)
284e571cbf1STrond Myklebust {
285e571cbf1STrond Myklebust int ret = -ENOMEM;
286e571cbf1STrond Myklebust
287e571cbf1STrond Myklebust *item = nfs_dns_lookup(cd, key);
288e571cbf1STrond Myklebust if (*item) {
289e571cbf1STrond Myklebust ret = cache_check(cd, &(*item)->h, &dreq->req);
290e571cbf1STrond Myklebust if (ret)
291e571cbf1STrond Myklebust *item = NULL;
292e571cbf1STrond Myklebust }
293e571cbf1STrond Myklebust return ret;
294e571cbf1STrond Myklebust }
295e571cbf1STrond Myklebust
do_cache_lookup_nowait(struct cache_detail * cd,struct nfs_dns_ent * key,struct nfs_dns_ent ** item)296e571cbf1STrond Myklebust static int do_cache_lookup_nowait(struct cache_detail *cd,
297e571cbf1STrond Myklebust struct nfs_dns_ent *key,
298e571cbf1STrond Myklebust struct nfs_dns_ent **item)
299e571cbf1STrond Myklebust {
300e571cbf1STrond Myklebust int ret = -ENOMEM;
301e571cbf1STrond Myklebust
302e571cbf1STrond Myklebust *item = nfs_dns_lookup(cd, key);
303e571cbf1STrond Myklebust if (!*item)
304e571cbf1STrond Myklebust goto out_err;
305e571cbf1STrond Myklebust ret = -ETIMEDOUT;
306e571cbf1STrond Myklebust if (!test_bit(CACHE_VALID, &(*item)->h.flags)
307c5b29f88SNeilBrown || (*item)->h.expiry_time < seconds_since_boot()
308e571cbf1STrond Myklebust || cd->flush_time > (*item)->h.last_refresh)
309e571cbf1STrond Myklebust goto out_put;
310e571cbf1STrond Myklebust ret = -ENOENT;
311e571cbf1STrond Myklebust if (test_bit(CACHE_NEGATIVE, &(*item)->h.flags))
312e571cbf1STrond Myklebust goto out_put;
313e571cbf1STrond Myklebust return 0;
314e571cbf1STrond Myklebust out_put:
315e571cbf1STrond Myklebust cache_put(&(*item)->h, cd);
316e571cbf1STrond Myklebust out_err:
317e571cbf1STrond Myklebust *item = NULL;
318e571cbf1STrond Myklebust return ret;
319e571cbf1STrond Myklebust }
320e571cbf1STrond Myklebust
do_cache_lookup_wait(struct cache_detail * cd,struct nfs_dns_ent * key,struct nfs_dns_ent ** item)321e571cbf1STrond Myklebust static int do_cache_lookup_wait(struct cache_detail *cd,
322e571cbf1STrond Myklebust struct nfs_dns_ent *key,
323e571cbf1STrond Myklebust struct nfs_dns_ent **item)
324e571cbf1STrond Myklebust {
325e571cbf1STrond Myklebust struct nfs_cache_defer_req *dreq;
326e571cbf1STrond Myklebust int ret = -ENOMEM;
327e571cbf1STrond Myklebust
328e571cbf1STrond Myklebust dreq = nfs_cache_defer_req_alloc();
329e571cbf1STrond Myklebust if (!dreq)
330e571cbf1STrond Myklebust goto out;
331e571cbf1STrond Myklebust ret = do_cache_lookup(cd, key, item, dreq);
332e571cbf1STrond Myklebust if (ret == -EAGAIN) {
333e571cbf1STrond Myklebust ret = nfs_cache_wait_for_upcall(dreq);
334e571cbf1STrond Myklebust if (!ret)
335e571cbf1STrond Myklebust ret = do_cache_lookup_nowait(cd, key, item);
336e571cbf1STrond Myklebust }
337e571cbf1STrond Myklebust nfs_cache_defer_req_put(dreq);
338e571cbf1STrond Myklebust out:
339e571cbf1STrond Myklebust return ret;
340e571cbf1STrond Myklebust }
341e571cbf1STrond Myklebust
nfs_dns_resolve_name(struct net * net,char * name,size_t namelen,struct sockaddr_storage * ss,size_t salen)3421b340d01SStanislav Kinsbursky ssize_t nfs_dns_resolve_name(struct net *net, char *name,
343cf0d7e7fSKees Cook size_t namelen, struct sockaddr_storage *ss, size_t salen)
344e571cbf1STrond Myklebust {
345e571cbf1STrond Myklebust struct nfs_dns_ent key = {
346e571cbf1STrond Myklebust .hostname = name,
347e571cbf1STrond Myklebust .namelen = namelen,
348e571cbf1STrond Myklebust };
349e571cbf1STrond Myklebust struct nfs_dns_ent *item = NULL;
350e571cbf1STrond Myklebust ssize_t ret;
3511b340d01SStanislav Kinsbursky struct nfs_net *nn = net_generic(net, nfs_net_id);
352e571cbf1STrond Myklebust
3531b340d01SStanislav Kinsbursky ret = do_cache_lookup_wait(nn->nfs_dns_resolve, &key, &item);
354e571cbf1STrond Myklebust if (ret == 0) {
355e571cbf1STrond Myklebust if (salen >= item->addrlen) {
356cf0d7e7fSKees Cook memcpy(ss, &item->addr, item->addrlen);
357e571cbf1STrond Myklebust ret = item->addrlen;
358e571cbf1STrond Myklebust } else
359e571cbf1STrond Myklebust ret = -EOVERFLOW;
3601b340d01SStanislav Kinsbursky cache_put(&item->h, nn->nfs_dns_resolve);
361e571cbf1STrond Myklebust } else if (ret == -ENOENT)
362e571cbf1STrond Myklebust ret = -ESRCH;
363e571cbf1STrond Myklebust return ret;
364e571cbf1STrond Myklebust }
365e571cbf1STrond Myklebust
366483479c2SStanislav Kinsbursky static struct cache_detail nfs_dns_resolve_template = {
367483479c2SStanislav Kinsbursky .owner = THIS_MODULE,
368483479c2SStanislav Kinsbursky .hash_size = NFS_DNS_HASHTBL_SIZE,
369483479c2SStanislav Kinsbursky .name = "dns_resolve",
370483479c2SStanislav Kinsbursky .cache_put = nfs_dns_ent_put,
371483479c2SStanislav Kinsbursky .cache_upcall = nfs_dns_upcall,
37273fb847aSStanislav Kinsbursky .cache_request = nfs_dns_request,
373483479c2SStanislav Kinsbursky .cache_parse = nfs_dns_parse,
374483479c2SStanislav Kinsbursky .cache_show = nfs_dns_show,
375483479c2SStanislav Kinsbursky .match = nfs_dns_match,
376483479c2SStanislav Kinsbursky .init = nfs_dns_ent_init,
377483479c2SStanislav Kinsbursky .update = nfs_dns_ent_update,
378483479c2SStanislav Kinsbursky .alloc = nfs_dns_ent_alloc,
379483479c2SStanislav Kinsbursky };
380483479c2SStanislav Kinsbursky
381483479c2SStanislav Kinsbursky
nfs_dns_resolver_cache_init(struct net * net)3821b340d01SStanislav Kinsbursky int nfs_dns_resolver_cache_init(struct net *net)
383e571cbf1STrond Myklebust {
384483479c2SStanislav Kinsbursky int err;
3851b340d01SStanislav Kinsbursky struct nfs_net *nn = net_generic(net, nfs_net_id);
3869222b955SStanislav Kinsbursky
387483479c2SStanislav Kinsbursky nn->nfs_dns_resolve = cache_create_net(&nfs_dns_resolve_template, net);
388483479c2SStanislav Kinsbursky if (IS_ERR(nn->nfs_dns_resolve))
389483479c2SStanislav Kinsbursky return PTR_ERR(nn->nfs_dns_resolve);
3901b340d01SStanislav Kinsbursky
391483479c2SStanislav Kinsbursky err = nfs_cache_register_net(net, nn->nfs_dns_resolve);
3921b340d01SStanislav Kinsbursky if (err)
3931b340d01SStanislav Kinsbursky goto err_reg;
3941b340d01SStanislav Kinsbursky return 0;
3951b340d01SStanislav Kinsbursky
3961b340d01SStanislav Kinsbursky err_reg:
397483479c2SStanislav Kinsbursky cache_destroy_net(nn->nfs_dns_resolve, net);
3989222b955SStanislav Kinsbursky return err;
3999222b955SStanislav Kinsbursky }
4001b340d01SStanislav Kinsbursky
nfs_dns_resolver_cache_destroy(struct net * net)4011b340d01SStanislav Kinsbursky void nfs_dns_resolver_cache_destroy(struct net *net)
4021b340d01SStanislav Kinsbursky {
4031b340d01SStanislav Kinsbursky struct nfs_net *nn = net_generic(net, nfs_net_id);
4041b340d01SStanislav Kinsbursky
405462b8f6bSStanislav Kinsbursky nfs_cache_unregister_net(net, nn->nfs_dns_resolve);
406483479c2SStanislav Kinsbursky cache_destroy_net(nn->nfs_dns_resolve, net);
4071b340d01SStanislav Kinsbursky }
4081b340d01SStanislav Kinsbursky
nfs4_dns_net_init(struct net * net)409c8d74d9bSTrond Myklebust static int nfs4_dns_net_init(struct net *net)
410c8d74d9bSTrond Myklebust {
411c8d74d9bSTrond Myklebust return nfs_dns_resolver_cache_init(net);
412c8d74d9bSTrond Myklebust }
413c8d74d9bSTrond Myklebust
nfs4_dns_net_exit(struct net * net)414c8d74d9bSTrond Myklebust static void nfs4_dns_net_exit(struct net *net)
415c8d74d9bSTrond Myklebust {
416c8d74d9bSTrond Myklebust nfs_dns_resolver_cache_destroy(net);
417c8d74d9bSTrond Myklebust }
418c8d74d9bSTrond Myklebust
419c8d74d9bSTrond Myklebust static struct pernet_operations nfs4_dns_resolver_ops = {
420c8d74d9bSTrond Myklebust .init = nfs4_dns_net_init,
421c8d74d9bSTrond Myklebust .exit = nfs4_dns_net_exit,
422c8d74d9bSTrond Myklebust };
423c8d74d9bSTrond Myklebust
rpc_pipefs_event(struct notifier_block * nb,unsigned long event,void * ptr)4249df69c81SStanislav Kinsbursky static int rpc_pipefs_event(struct notifier_block *nb, unsigned long event,
4259df69c81SStanislav Kinsbursky void *ptr)
4269df69c81SStanislav Kinsbursky {
4279df69c81SStanislav Kinsbursky struct super_block *sb = ptr;
4289df69c81SStanislav Kinsbursky struct net *net = sb->s_fs_info;
4299df69c81SStanislav Kinsbursky struct nfs_net *nn = net_generic(net, nfs_net_id);
4309df69c81SStanislav Kinsbursky struct cache_detail *cd = nn->nfs_dns_resolve;
4319df69c81SStanislav Kinsbursky int ret = 0;
4329df69c81SStanislav Kinsbursky
4339df69c81SStanislav Kinsbursky if (cd == NULL)
4349df69c81SStanislav Kinsbursky return 0;
4359df69c81SStanislav Kinsbursky
4369df69c81SStanislav Kinsbursky if (!try_module_get(THIS_MODULE))
4379df69c81SStanislav Kinsbursky return 0;
4389df69c81SStanislav Kinsbursky
4399df69c81SStanislav Kinsbursky switch (event) {
4409df69c81SStanislav Kinsbursky case RPC_PIPEFS_MOUNT:
4419df69c81SStanislav Kinsbursky ret = nfs_cache_register_sb(sb, cd);
4429df69c81SStanislav Kinsbursky break;
4439df69c81SStanislav Kinsbursky case RPC_PIPEFS_UMOUNT:
4449df69c81SStanislav Kinsbursky nfs_cache_unregister_sb(sb, cd);
4459df69c81SStanislav Kinsbursky break;
4469df69c81SStanislav Kinsbursky default:
4479df69c81SStanislav Kinsbursky ret = -ENOTSUPP;
4489df69c81SStanislav Kinsbursky break;
4499df69c81SStanislav Kinsbursky }
4509df69c81SStanislav Kinsbursky module_put(THIS_MODULE);
4519df69c81SStanislav Kinsbursky return ret;
4529df69c81SStanislav Kinsbursky }
4539df69c81SStanislav Kinsbursky
4549df69c81SStanislav Kinsbursky static struct notifier_block nfs_dns_resolver_block = {
4559df69c81SStanislav Kinsbursky .notifier_call = rpc_pipefs_event,
4569df69c81SStanislav Kinsbursky };
4579df69c81SStanislav Kinsbursky
nfs_dns_resolver_init(void)4581b340d01SStanislav Kinsbursky int nfs_dns_resolver_init(void)
4591b340d01SStanislav Kinsbursky {
460c8d74d9bSTrond Myklebust int err;
461c8d74d9bSTrond Myklebust
462c8d74d9bSTrond Myklebust err = register_pernet_subsys(&nfs4_dns_resolver_ops);
463c8d74d9bSTrond Myklebust if (err < 0)
464c8d74d9bSTrond Myklebust goto out;
465c8d74d9bSTrond Myklebust err = rpc_pipefs_notifier_register(&nfs_dns_resolver_block);
466c8d74d9bSTrond Myklebust if (err < 0)
467c8d74d9bSTrond Myklebust goto out1;
468c8d74d9bSTrond Myklebust return 0;
469c8d74d9bSTrond Myklebust out1:
470c8d74d9bSTrond Myklebust unregister_pernet_subsys(&nfs4_dns_resolver_ops);
471c8d74d9bSTrond Myklebust out:
472c8d74d9bSTrond Myklebust return err;
473e571cbf1STrond Myklebust }
474e571cbf1STrond Myklebust
nfs_dns_resolver_destroy(void)475e571cbf1STrond Myklebust void nfs_dns_resolver_destroy(void)
476e571cbf1STrond Myklebust {
4779df69c81SStanislav Kinsbursky rpc_pipefs_notifier_unregister(&nfs_dns_resolver_block);
478c8d74d9bSTrond Myklebust unregister_pernet_subsys(&nfs4_dns_resolver_ops);
479e571cbf1STrond Myklebust }
480c2e8139cSBryan Schumaker #endif
481