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)
14 #include <sys/types.h>
15 #include <sys/select.h>
18 #include <netinet/in.h>
19 #include <arpa/inet.h>
31 * Support routines for text-based upcalls.
32 * Fields are separated by spaces.
33 * Fields are either mangled to quote space tab newline slosh with slosh
34 * or a hexified with a leading \x
35 * Record is terminated with newline.
38 void cache_export_ent(char *domain, struct exportent *exp);
44 void auth_unix_ip(FILE *f)
48 * Ignore if class != "nfsd"
49 * Otherwise find domainname and write back:
51 * "nfsd" IP-ADDR expiry domainname
58 if (readline(fileno(f), &lbuf, &lbuflen) != 1)
63 if (qword_get(&cp, class, 20) <= 0 ||
64 strcmp(class, "nfsd") != 0)
67 if (qword_get(&cp, ipaddr, 20) <= 0)
70 if (inet_aton(ipaddr, &addr)==0)
75 /* addr is a valid, interesting address, find the domain name... */
76 client = client_compose(addr);
79 qword_print(f, "nfsd");
80 qword_print(f, ipaddr);
81 qword_printint(f, time(0)+30*60);
83 qword_print(f, *client?client:"DEFAULT");
86 if (client && strcmp(ipaddr, client))
87 mountlist_add(ipaddr, *client?client:"DEFAULT");
89 if (client) free(client);
96 * domain fsidtype fsid
97 * interpret fsid, find export point and options, and write:
98 * domain fsidtype fsid expiry path
104 unsigned int dev, major=0, minor=0;
105 unsigned int inode=0;
106 unsigned int fsidnum=0;
108 struct exportent *found = NULL;
113 if (readline(fileno(f), &lbuf, &lbuflen) != 1)
118 dom = malloc(strlen(cp));
121 if (qword_get(&cp, dom, strlen(cp)) <= 0)
123 if (qword_get_int(&cp, &fsidtype) != 0)
125 if (fsidtype < 0 || fsidtype > 3)
126 goto out; /* unknown type */
127 if ((fsidlen = qword_get(&cp, fsid, 32)) <= 0)
130 case 0: /* 4 bytes: 2 major, 2 minor, 4 inode */
133 memcpy(&dev, fsid, 4);
134 memcpy(&inode, fsid+4, 4);
135 major = ntohl(dev)>>16;
136 minor = ntohl(dev) & 0xFFFF;
139 case 1: /* 4 bytes - fsid */
142 memcpy(&fsidnum, fsid, 4);
145 case 2: /* 12 bytes: 4 major, 4 minor, 4 inode
146 * This format is never actually used but was
147 * an historical accident
151 memcpy(&dev, fsid, 4); major = ntohl(dev);
152 memcpy(&dev, fsid+4, 4); minor = ntohl(dev);
153 memcpy(&inode, fsid+8, 4);
156 case 3: /* 8 bytes: 4 byte packed device number, 4 inode */
157 /* This is *host* endian, not net-byte-order, because
158 * no-one outside this host has any business interpreting it
162 memcpy(&dev, fsid, 4);
163 memcpy(&inode, fsid+4, 4);
164 major = (dev & 0xfff00) >> 8;
165 minor = (dev & 0xff) | ((dev >> 12) & 0xfff00);
172 /* Now determine export point for this fsid/domain */
173 for (i=0 ; i < MCL_MAXTYPES; i++) {
174 for (exp = exportlist[i]; exp; exp = exp->m_next) {
177 if (!client_member(dom, exp->m_client->m_hostname))
179 if (exp->m_export.e_mountpoint &&
180 !is_mountpoint(exp->m_export.e_mountpoint[0]?
181 exp->m_export.e_mountpoint:
182 exp->m_export.e_path))
184 if (stat(exp->m_export.e_path, &stb) != 0)
187 ((exp->m_export.e_flags & NFSEXP_FSID) == 0 ||
188 exp->m_export.e_fsid != fsidnum))
191 if (stb.st_ino != inode)
193 if (major != major(stb.st_dev) ||
194 minor != minor(stb.st_dev))
197 /* It's a match !! */
199 found = &exp->m_export;
200 else if (strcmp(found->e_path, exp->m_export.e_path)!= 0)
202 xlog(L_WARNING, "%s and %s have same filehandle for %s, using first",
203 found->e_path, exp->m_export.e_path, dom);
208 found->e_mountpoint &&
209 !is_mountpoint(found->e_mountpoint[0]?
212 /* Cannot export this yet
213 * should log a warning, but need to rate limit
214 xlog(L_WARNING, "%s not exported as %d not a mountpoint",
215 found->e_path, found->e_mountpoint);
217 /* FIXME we need to make sure we re-visit this later */
220 if (!found && dev_missing) {
221 /* The missing dev could be what we want, so just be
222 * quite rather than returning stale yet
228 cache_export_ent(dom, found);
231 qword_printint(f, fsidtype);
232 qword_printhex(f, fsid, fsidlen);
233 qword_printint(f, time(0)+30*60);
235 qword_print(f, found->e_path);
242 void nfsd_export(FILE *f)
246 * determine export options and return:
247 * domain path expiry flags anonuid anongid fsid
253 nfs_export *exp, *found = NULL;
256 if (readline(fileno(f), &lbuf, &lbuflen) != 1)
260 dom = malloc(strlen(cp));
261 path = malloc(strlen(cp));
266 if (qword_get(&cp, dom, strlen(lbuf)) <= 0)
268 if (qword_get(&cp, path, strlen(lbuf)) <= 0)
273 /* now find flags for this export point in this domain */
274 for (i=0 ; i < MCL_MAXTYPES; i++) {
275 for (exp = exportlist[i]; exp; exp = exp->m_next) {
276 if (!client_member(dom, exp->m_client->m_hostname))
278 if (strcmp(path, exp->m_export.e_path))
283 xlog(L_WARNING, "%s exported to both %s and %s in %s",
284 path, exp->m_client->m_hostname, found->m_client->m_hostname,
291 qword_print(f, path);
292 qword_printint(f, time(0)+30*60);
294 qword_printint(f, found->m_export.e_flags);
295 qword_printint(f, found->m_export.e_anonuid);
296 qword_printint(f, found->m_export.e_anongid);
297 qword_printint(f, found->m_export.e_fsid);
298 mountlist_add(dom, path);
303 if (path) free(path);
309 void (*cache_handle)(FILE *f);
312 { "auth.unix.ip", auth_unix_ip},
313 { "nfsd.export", nfsd_export},
314 { "nfsd.fh", nfsd_fh},
318 void cache_open(void)
321 for (i=0; cachelist[i].cache_name; i++ ){
323 sprintf(path, "/proc/net/rpc/%s/channel", cachelist[i].cache_name);
324 cachelist[i].f = fopen(path, "r+");
328 void cache_set_fds(fd_set *fdset)
331 for (i=0; cachelist[i].cache_name; i++) {
333 FD_SET(fileno(cachelist[i].f), fdset);
337 int cache_process_req(fd_set *readfds)
341 for (i=0; cachelist[i].cache_name; i++) {
342 if (cachelist[i].f != NULL &&
343 FD_ISSET(fileno(cachelist[i].f), readfds)) {
345 cachelist[i].cache_handle(cachelist[i].f);
346 FD_CLR(fileno(cachelist[i].f), readfds);
354 * Give IP->domain and domain+path->options to kernel
355 * % echo nfsd $IP $[now+30*60] $domain > /proc/net/rpc/auth.unix.ip/channel
356 * % echo $domain $path $[now+30*60] $options $anonuid $anongid $fsid > /proc/net/rpc/nfsd.export/channel
359 void cache_export_ent(char *domain, struct exportent *exp)
362 FILE *f = fopen("/proc/net/rpc/nfsd.export/channel", "w");
366 qword_print(f, domain);
367 qword_print(f, exp->e_path);
368 qword_printint(f, time(0)+30*60);
369 qword_printint(f, exp->e_flags);
370 qword_printint(f, exp->e_anonuid);
371 qword_printint(f, exp->e_anongid);
372 qword_printint(f, exp->e_fsid);
377 mountlist_add(domain, exp->e_path);
380 void cache_export(nfs_export *exp)
384 f = fopen("/proc/net/rpc/auth.unix.ip/channel", "w");
388 qword_print(f, "nfsd");
389 qword_print(f, inet_ntoa(exp->m_client->m_addrlist[0]));
390 qword_printint(f, time(0)+30*60);
391 qword_print(f, exp->m_client->m_hostname);
396 if (strcmp(inet_ntoa(exp->m_client->m_addrlist[0]), exp->m_client->m_hostname))
397 mountlist_add(inet_ntoa(exp->m_client->m_addrlist[0]), exp->m_client->m_hostname);
399 cache_export_ent(exp->m_client->m_hostname, &exp->m_export);
404 * echo $domain $path $length
405 * read filehandle <&0
406 * } <> /proc/fs/nfsd/filehandle
409 cache_get_filehandle(nfs_export *exp, int len, char *p)
411 FILE *f = fopen("/proc/fs/nfsd/filehandle", "r+");
415 static struct nfs_fh_len fh;
418 f = fopen("/proc/fs/nfs/filehandle", "r+");
422 qword_print(f, exp->m_client->m_hostname);
424 qword_printint(f, len);
427 failed = (fgets(buf, sizeof(buf), f) == NULL);
431 memset(fh.fh_handle, 0, sizeof(fh.fh_handle));
432 fh.fh_size = qword_get(&bp, (char *)fh.fh_handle, NFS3_FHSIZE);