]> git.decadent.org.uk Git - nfs-utils.git/blob - utils/mountd/cache.c
2005-08-26 Kevin Coffman <kwc@citi.umich.edu>
[nfs-utils.git] / utils / mountd / cache.c
1
2 /*
3  * Handle communication with knfsd internal cache
4  *
5  * We open /proc/net/rpc/{auth.unix.ip,nfsd.export,nfsd.fh}/channel
6  * and listen for requests (using my_svc_run)
7  * 
8  */
9 #include "config.h"
10
11 #include <sys/types.h>
12 #include <sys/select.h>
13 #include <sys/stat.h>
14 #include <time.h>
15 #include <netinet/in.h>
16 #include <arpa/inet.h>
17 #include <unistd.h>
18 #include <fcntl.h>
19 #include <errno.h>
20 #include <ctype.h>
21 #include "misc.h"
22 #include "nfslib.h"
23 #include "exportfs.h"
24 #include "mountd.h"
25 #include "xmalloc.h"
26
27 /*
28  * Support routines for text-based upcalls.
29  * Fields are separated by spaces.
30  * Fields are either mangled to quote space tab newline slosh with slosh
31  * or a hexified with a leading \x
32  * Record is terminated with newline.
33  *
34  */
35 void cache_export_ent(char *domain, struct exportent *exp);
36
37
38 char *lbuf  = NULL;
39 int lbuflen = 0;
40
41 void auth_unix_ip(FILE *f)
42 {
43         /* requests are
44          *  class IP-ADDR
45          * Ignore if class != "nfsd"
46          * Otherwise find domainname and write back:
47          *
48          *  "nfsd" IP-ADDR expiry domainname
49          */
50         char *cp;
51         char class[20];
52         char ipaddr[20];
53         char *client;
54         struct in_addr addr;
55         if (readline(fileno(f), &lbuf, &lbuflen) != 1)
56                 return;
57
58         cp = lbuf;
59
60         if (qword_get(&cp, class, 20) <= 0 ||
61             strcmp(class, "nfsd") != 0)
62                 return;
63
64         if (qword_get(&cp, ipaddr, 20) <= 0)
65                 return;
66
67         if (inet_aton(ipaddr, &addr)==0)
68                 return;
69
70         auth_reload();
71
72         /* addr is a valid, interesting address, find the domain name... */
73         client = client_compose(addr);
74
75         
76         qword_print(f, "nfsd");
77         qword_print(f, ipaddr);
78         qword_printint(f, time(0)+30*60);
79         if (client)
80                 qword_print(f, *client?client:"DEFAULT");
81         qword_eol(f);
82
83         if (client && strcmp(ipaddr, client))
84                 mountlist_add(ipaddr, *client?client:"DEFAULT");
85
86         if (client) free(client);
87         
88 }
89
90 void nfsd_fh(FILE *f)
91 {
92         /* request are:
93          *  domain fsidtype fsid
94          * interpret fsid, find export point and options, and write:
95          *  domain fsidtype fsid expiry path
96          */
97         char *cp;
98         char *dom;
99         int fsidtype;
100         int fsidlen;
101         unsigned int dev, major=0, minor=0;
102         unsigned int inode=0;
103         unsigned int fsidnum=0;
104         char fsid[32];
105         struct exportent *found = NULL;
106         nfs_export *exp;
107         int i;
108         int dev_missing = 0;
109
110         if (readline(fileno(f), &lbuf, &lbuflen) != 1)
111                 return;
112
113         cp = lbuf;
114
115         dom = malloc(strlen(cp));
116         if (dom == NULL)
117                 return;
118         if (qword_get(&cp, dom, strlen(cp)) <= 0)
119                 goto out;
120         if (qword_get_int(&cp, &fsidtype) != 0)
121                 goto out;
122         if (fsidtype < 0 || fsidtype > 1)
123                 goto out; /* unknown type */
124         if ((fsidlen = qword_get(&cp, fsid, 32)) <= 0)
125                 goto out;
126         switch(fsidtype) {
127         case 0: /* 4 bytes: 2 major, 2 minor, 4 inode */
128                 if (fsidlen != 8)
129                         goto out;
130                 memcpy(&dev, fsid, 4);
131                 memcpy(&inode, fsid+4, 4);
132                 major = ntohl(dev)>>16;
133                 minor = ntohl(dev) & 0xFFFF;
134                 break;
135
136         case 1: /* 4 bytes - fsid */
137                 if (fsidlen != 4)
138                         goto out;
139                 memcpy(&fsidnum, fsid, 4);
140                 break;
141         }
142
143         auth_reload();
144
145         /* Now determine export point for this fsid/domain */
146         for (i=0 ; i < MCL_MAXTYPES; i++) {
147                 for (exp = exportlist[i]; exp; exp = exp->m_next) {
148                         struct stat stb;
149
150                         if (!client_member(dom, exp->m_client->m_hostname))
151                                 continue;
152                         if (exp->m_export.e_mountpoint &&
153                             !is_mountpoint(exp->m_export.e_mountpoint[0]?
154                                            exp->m_export.e_mountpoint:
155                                            exp->m_export.e_path))
156                                 dev_missing ++;
157                         if (stat(exp->m_export.e_path, &stb) != 0)
158                                 continue;
159                         if (fsidtype == 1 &&
160                             ((exp->m_export.e_flags & NFSEXP_FSID) == 0 ||
161                              exp->m_export.e_fsid != fsidnum))
162                                 continue;
163                         if (fsidtype == 0) {
164                                 if (stb.st_ino != inode)
165                                         continue;
166                                 if (major != major(stb.st_dev) ||
167                                     minor != minor(stb.st_dev))
168                                         continue;
169                         }
170                         /* It's a match !! */
171                         if (!found)
172                                 found = &exp->m_export;
173                         else if (strcmp(found->e_path, exp->m_export.e_path)!= 0)
174                         {
175                                 xlog(L_WARNING, "%s and %s have same filehandle for %s, using first",
176                                      found->e_path, exp->m_export.e_path, dom);
177                         }
178                 }
179         }
180         if (found && 
181             found->e_mountpoint &&
182             !is_mountpoint(found->e_mountpoint[0]?
183                            found->e_mountpoint:
184                            found->e_path)) {
185                 /* Cannot export this yet 
186                  * should log a warning, but need to rate limit
187                    xlog(L_WARNING, "%s not exported as %d not a mountpoint",
188                    found->e_path, found->e_mountpoint);
189                  */
190                 /* FIXME we need to make sure we re-visit this later */
191                 goto out;
192         }
193         if (!found && dev_missing) {
194                 /* The missing dev could be what we want, so just be
195                  * quite rather than returning stale yet
196                  */
197                 goto out;
198         }
199
200         if (found)
201                 cache_export_ent(dom, found);
202
203         qword_print(f, dom);
204         qword_printint(f, fsidtype);
205         qword_printhex(f, fsid, fsidlen);
206         qword_printint(f, time(0)+30*60);
207         if (found)
208                 qword_print(f, found->e_path);
209         qword_eol(f);
210  out:
211         free(dom);
212         return;         
213 }
214
215 void nfsd_export(FILE *f)
216 {
217         /* requests are:
218          *  domain path
219          * determine export options and return:
220          *  domain path expiry flags anonuid anongid fsid
221          */
222
223         char *cp;
224         int i;
225         char *dom, *path;
226         nfs_export *exp, *found = NULL;
227
228
229         if (readline(fileno(f), &lbuf, &lbuflen) != 1)
230                 return;
231
232         cp = lbuf;
233         dom = malloc(strlen(cp));
234         path = malloc(strlen(cp));
235
236         if (!dom || !path)
237                 goto out;
238
239         if (qword_get(&cp, dom, strlen(lbuf)) <= 0)
240                 goto out;
241         if (qword_get(&cp, path, strlen(lbuf)) <= 0)
242                 goto out;
243
244         auth_reload();
245
246         /* now find flags for this export point in this domain */
247         for (i=0 ; i < MCL_MAXTYPES; i++) {
248                 for (exp = exportlist[i]; exp; exp = exp->m_next) {
249                         if (!client_member(dom, exp->m_client->m_hostname))
250                                 continue;
251                         if (strcmp(path, exp->m_export.e_path))
252                                 continue;
253                         if (!found)
254                                 found = exp;
255                         else {
256                                 xlog(L_WARNING, "%s exported to both %s and %s in %s",
257                                      path, exp->m_client->m_hostname, found->m_client->m_hostname,
258                                      dom);
259                         }
260                 }
261         }
262
263         qword_print(f, dom);
264         qword_print(f, path);
265         qword_printint(f, time(0)+30*60);
266         if (found) {
267                 qword_printint(f, found->m_export.e_flags);
268                 qword_printint(f, found->m_export.e_anonuid);
269                 qword_printint(f, found->m_export.e_anongid);
270                 qword_printint(f, found->m_export.e_fsid);
271                 mountlist_add(dom, path);
272         }
273         qword_eol(f);
274  out:
275         if (dom) free(dom);
276         if (path) free(path);
277 }
278
279
280 struct {
281         char *cache_name;
282         void (*cache_handle)(FILE *f);
283         FILE *f;
284 } cachelist[] = {
285         { "auth.unix.ip", auth_unix_ip},
286         { "nfsd.export", nfsd_export},
287         { "nfsd.fh", nfsd_fh},
288         { NULL, NULL }
289 };
290
291 void cache_open(void) 
292 {
293         int i;
294         for (i=0; cachelist[i].cache_name; i++ ){
295                 char path[100];
296                 sprintf(path, "/proc/net/rpc/%s/channel", cachelist[i].cache_name);
297                 cachelist[i].f = fopen(path, "r+");
298         }
299 }
300
301 void cache_set_fds(fd_set *fdset)
302 {
303         int i;
304         for (i=0; cachelist[i].cache_name; i++) {
305                 if (cachelist[i].f)
306                         FD_SET(fileno(cachelist[i].f), fdset);
307         }
308 }
309
310 int cache_process_req(fd_set *readfds) 
311 {
312         int i;
313         int cnt = 0;
314         for (i=0; cachelist[i].cache_name; i++) {
315                 if (cachelist[i].f != NULL &&
316                     FD_ISSET(fileno(cachelist[i].f), readfds)) {
317                         cnt++;
318                         cachelist[i].cache_handle(cachelist[i].f);
319                         FD_CLR(fileno(cachelist[i].f), readfds);
320                 }
321         }
322         return cnt;
323 }
324
325
326 /*
327  * Give IP->domain and domain+path->options to kernel
328  * % echo nfsd $IP  $[now+30*60] $domain > /proc/net/rpc/auth.unix.ip/channel
329  * % echo $domain $path $[now+30*60] $options $anonuid $anongid $fsid > /proc/net/rpc/nfsd.export/channel
330  */
331
332 void cache_export_ent(char *domain, struct exportent *exp)
333 {
334
335         FILE *f = fopen("/proc/net/rpc/nfsd.export/channel", "w");
336         if (!f)
337                 return;
338
339         qword_print(f, domain);
340         qword_print(f, exp->e_path);
341         qword_printint(f, time(0)+30*60);
342         qword_printint(f, exp->e_flags);
343         qword_printint(f, exp->e_anonuid);
344         qword_printint(f, exp->e_anongid);
345         qword_printint(f, exp->e_fsid);
346         qword_eol(f);
347
348         fclose(f);
349
350         mountlist_add(domain, exp->e_path);
351 }
352
353 void cache_export(nfs_export *exp)
354 {
355         FILE *f;
356
357         f = fopen("/proc/net/rpc/auth.unix.ip/channel", "w");
358         if (!f)
359                 return;
360
361         qword_print(f, "nfsd");
362         qword_print(f, inet_ntoa(exp->m_client->m_addrlist[0]));
363         qword_printint(f, time(0)+30*60);
364         qword_print(f, exp->m_client->m_hostname);
365         qword_eol(f);
366         
367         fclose(f);
368
369         if (strcmp(inet_ntoa(exp->m_client->m_addrlist[0]), exp->m_client->m_hostname))
370                 mountlist_add(inet_ntoa(exp->m_client->m_addrlist[0]), exp->m_client->m_hostname);
371
372         cache_export_ent(exp->m_client->m_hostname, &exp->m_export);
373 }
374
375 /* Get a filehandle.
376  * { 
377  *   echo $domain $path $length 
378  *   read filehandle <&0
379  * } <> /proc/fs/nfsd/filehandle
380  */
381 struct nfs_fh_len *
382 cache_get_filehandle(nfs_export *exp, int len, char *p)
383 {
384         FILE *f = fopen("/proc/fs/nfsd/filehandle", "r+");
385         char buf[200];
386         char *bp = buf;
387         int failed;
388         static struct nfs_fh_len fh;
389
390         if (!f)
391                 f = fopen("/proc/fs/nfs/filehandle", "r+");
392         if (!f)
393                 return NULL;
394
395         qword_print(f, exp->m_client->m_hostname);
396         qword_print(f, p);
397         qword_printint(f, len); 
398         qword_eol(f);
399         
400         failed = (fgets(buf, sizeof(buf), f) == NULL);
401         fclose(f);
402         if (failed)
403                 return NULL;
404         memset(fh.fh_handle, 0, sizeof(fh.fh_handle));
405         fh.fh_size = qword_get(&bp, fh.fh_handle, NFS3_FHSIZE);
406         return &fh;
407 }
408