3 * Handle communication with knfsd internal cache
5 * We open /proc/net/rpc/{auth.unix.ip,nfsd.export,nfsd.fh}/channel
6 * and listen for requests (using my_svc_run)
11 #include <sys/types.h>
12 #include <sys/select.h>
15 #include <netinet/in.h>
16 #include <arpa/inet.h>
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.
35 void cache_export_ent(char *domain, struct exportent *exp);
41 void auth_unix_ip(FILE *f)
45 * Ignore if class != "nfsd"
46 * Otherwise find domainname and write back:
48 * "nfsd" IP-ADDR expiry domainname
55 if (readline(fileno(f), &lbuf, &lbuflen) != 1)
60 if (qword_get(&cp, class, 20) <= 0 ||
61 strcmp(class, "nfsd") != 0)
64 if (qword_get(&cp, ipaddr, 20) <= 0)
67 if (inet_aton(ipaddr, &addr)==0)
72 /* addr is a valid, interesting address, find the domain name... */
73 client = client_compose(addr);
76 qword_print(f, "nfsd");
77 qword_print(f, ipaddr);
78 qword_printint(f, time(0)+30*60);
80 qword_print(f, *client?client:"DEFAULT");
83 if (client && strcmp(ipaddr, client))
84 mountlist_add(ipaddr, *client?client:"DEFAULT");
86 if (client) free(client);
93 * domain fsidtype fsid
94 * interpret fsid, find export point and options, and write:
95 * domain fsidtype fsid expiry path
101 unsigned int dev, major=0, minor=0;
102 unsigned int inode=0;
103 unsigned int fsidnum=0;
105 struct exportent *found = NULL;
110 if (readline(fileno(f), &lbuf, &lbuflen) != 1)
115 dom = malloc(strlen(cp));
118 if (qword_get(&cp, dom, strlen(cp)) <= 0)
120 if (qword_get_int(&cp, &fsidtype) != 0)
122 if (fsidtype < 0 || fsidtype > 1)
123 goto out; /* unknown type */
124 if ((fsidlen = qword_get(&cp, fsid, 32)) <= 0)
127 case 0: /* 4 bytes: 2 major, 2 minor, 4 inode */
130 memcpy(&dev, fsid, 4);
131 memcpy(&inode, fsid+4, 4);
132 major = ntohl(dev)>>16;
133 minor = ntohl(dev) & 0xFFFF;
136 case 1: /* 4 bytes - fsid */
139 memcpy(&fsidnum, fsid, 4);
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) {
150 if (!client_member(dom, exp->m_client->m_hostname))
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))
157 if (stat(exp->m_export.e_path, &stb) != 0)
160 ((exp->m_export.e_flags & NFSEXP_FSID) == 0 ||
161 exp->m_export.e_fsid != fsidnum))
164 if (stb.st_ino != inode)
166 if (major != major(stb.st_dev) ||
167 minor != minor(stb.st_dev))
170 /* It's a match !! */
172 found = &exp->m_export;
173 else if (strcmp(found->e_path, exp->m_export.e_path)!= 0)
175 xlog(L_WARNING, "%s and %s have same filehandle for %s, using first",
176 found->e_path, exp->m_export.e_path, dom);
181 found->e_mountpoint &&
182 !is_mountpoint(found->e_mountpoint[0]?
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);
190 /* FIXME we need to make sure we re-visit this later */
193 if (!found && dev_missing) {
194 /* The missing dev could be what we want, so just be
195 * quite rather than returning stale yet
201 cache_export_ent(dom, found);
204 qword_printint(f, fsidtype);
205 qword_printhex(f, fsid, fsidlen);
206 qword_printint(f, time(0)+30*60);
208 qword_print(f, found->e_path);
215 void nfsd_export(FILE *f)
219 * determine export options and return:
220 * domain path expiry flags anonuid anongid fsid
226 nfs_export *exp, *found = NULL;
229 if (readline(fileno(f), &lbuf, &lbuflen) != 1)
233 dom = malloc(strlen(cp));
234 path = malloc(strlen(cp));
239 if (qword_get(&cp, dom, strlen(lbuf)) <= 0)
241 if (qword_get(&cp, path, strlen(lbuf)) <= 0)
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))
251 if (strcmp(path, exp->m_export.e_path))
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,
264 qword_print(f, path);
265 qword_printint(f, time(0)+30*60);
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);
276 if (path) free(path);
282 void (*cache_handle)(FILE *f);
285 { "auth.unix.ip", auth_unix_ip},
286 { "nfsd.export", nfsd_export},
287 { "nfsd.fh", nfsd_fh},
291 void cache_open(void)
294 for (i=0; cachelist[i].cache_name; i++ ){
296 sprintf(path, "/proc/net/rpc/%s/channel", cachelist[i].cache_name);
297 cachelist[i].f = fopen(path, "r+");
301 void cache_set_fds(fd_set *fdset)
304 for (i=0; cachelist[i].cache_name; i++) {
306 FD_SET(fileno(cachelist[i].f), fdset);
310 int cache_process_req(fd_set *readfds)
314 for (i=0; cachelist[i].cache_name; i++) {
315 if (cachelist[i].f != NULL &&
316 FD_ISSET(fileno(cachelist[i].f), readfds)) {
318 cachelist[i].cache_handle(cachelist[i].f);
319 FD_CLR(fileno(cachelist[i].f), readfds);
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
332 void cache_export_ent(char *domain, struct exportent *exp)
335 FILE *f = fopen("/proc/net/rpc/nfsd.export/channel", "w");
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);
350 mountlist_add(domain, exp->e_path);
353 void cache_export(nfs_export *exp)
357 f = fopen("/proc/net/rpc/auth.unix.ip/channel", "w");
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);
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);
372 cache_export_ent(exp->m_client->m_hostname, &exp->m_export);
377 * echo $domain $path $length
378 * read filehandle <&0
379 * } <> /proc/fs/nfsd/filehandle
382 cache_get_filehandle(nfs_export *exp, int len, char *p)
384 FILE *f = fopen("/proc/fs/nfsd/filehandle", "r+");
388 static struct nfs_fh_len fh;
391 f = fopen("/proc/fs/nfs/filehandle", "r+");
395 qword_print(f, exp->m_client->m_hostname);
397 qword_printint(f, len);
400 failed = (fgets(buf, sizeof(buf), f) == NULL);
404 memset(fh.fh_handle, 0, sizeof(fh.fh_handle));
405 fh.fh_size = qword_get(&bp, fh.fh_handle, NFS3_FHSIZE);