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>
33 #include "blkid/blkid.h"
48 * Support routines for text-based upcalls.
49 * Fields are separated by spaces.
50 * Fields are either mangled to quote space tab newline slosh with slosh
51 * or a hexified with a leading \x
52 * Record is terminated with newline.
55 int cache_export_ent(char *domain, struct exportent *exp);
61 void auth_unix_ip(FILE *f)
65 * Ignore if class != "nfsd"
66 * Otherwise find domainname and write back:
68 * "nfsd" IP-ADDR expiry domainname
75 if (readline(fileno(f), &lbuf, &lbuflen) != 1)
80 if (qword_get(&cp, class, 20) <= 0 ||
81 strcmp(class, "nfsd") != 0)
84 if (qword_get(&cp, ipaddr, 20) <= 0)
87 if (inet_aton(ipaddr, &addr)==0)
92 /* addr is a valid, interesting address, find the domain name... */
93 client = client_compose(addr);
96 qword_print(f, "nfsd");
97 qword_print(f, ipaddr);
98 qword_printint(f, time(0)+30*60);
100 qword_print(f, *client?client:"DEFAULT");
103 if (client) free(client);
107 void auth_unix_gid(FILE *f)
112 * uid expiry count list of group ids
116 gid_t glist[100], *groups = glist;
121 if (readline(fileno(f), &lbuf, &lbuflen) != 1)
125 if (qword_get_int(&cp, &uid) != 0)
132 rv = getgrouplist(pw->pw_name, pw->pw_gid, groups, &ngroups);
133 if (rv == -1 && ngroups >= 100) {
134 groups = malloc(sizeof(gid_t)*ngroups);
138 rv = getgrouplist(pw->pw_name, pw->pw_gid,
142 qword_printint(f, uid);
143 qword_printint(f, time(0)+30*60);
145 qword_printint(f, ngroups);
146 for (i=0; i<ngroups; i++)
147 qword_printint(f, groups[i]);
154 int get_uuid(char *path, char *uuid, int uuidlen, char *u)
156 /* extract hex digits from uuidstr and compose a uuid
157 * of the given length (max 16), xoring bytes to make
158 * a smaller uuid. Then compare with uuid
164 static blkid_cache cache = NULL;
167 blkid_tag_iterate iter;
171 blkid_get_cache(&cache, NULL);
173 blkid_probe_all_new(cache);
175 if (stat(path, &stb) != 0)
177 devname = blkid_devno_to_devname(stb.st_dev);
180 dev = blkid_get_dev(cache, devname, BLKID_DEV_NORMAL);
184 iter = blkid_tag_iterate_begin(dev);
187 while (blkid_tag_next(iter, &type, &val) == 0)
188 if (strcmp(type, "UUID") == 0)
190 blkid_tag_iterate_end(iter);
197 memset(u, 0, uuidlen);
198 for ( ; *val ; val++) {
220 void nfsd_fh(FILE *f)
223 * domain fsidtype fsid
224 * interpret fsid, find export point and options, and write:
225 * domain fsidtype fsid expiry path
231 unsigned int dev, major=0, minor=0;
232 unsigned int inode=0;
233 unsigned long long inode64;
234 unsigned int fsidnum=0;
236 struct exportent *found = NULL;
243 if (readline(fileno(f), &lbuf, &lbuflen) != 1)
248 dom = malloc(strlen(cp));
251 if (qword_get(&cp, dom, strlen(cp)) <= 0)
253 if (qword_get_int(&cp, &fsidtype) != 0)
255 if (fsidtype < 0 || fsidtype > 7)
256 goto out; /* unknown type */
257 if ((fsidlen = qword_get(&cp, fsid, 32)) <= 0)
260 case FSID_DEV: /* 4 bytes: 2 major, 2 minor, 4 inode */
263 memcpy(&dev, fsid, 4);
264 memcpy(&inode, fsid+4, 4);
265 major = ntohl(dev)>>16;
266 minor = ntohl(dev) & 0xFFFF;
269 case FSID_NUM: /* 4 bytes - fsid */
272 memcpy(&fsidnum, fsid, 4);
275 case FSID_MAJOR_MINOR: /* 12 bytes: 4 major, 4 minor, 4 inode
276 * This format is never actually used but was
277 * an historical accident
281 memcpy(&dev, fsid, 4); major = ntohl(dev);
282 memcpy(&dev, fsid+4, 4); minor = ntohl(dev);
283 memcpy(&inode, fsid+8, 4);
286 case FSID_ENCODE_DEV: /* 8 bytes: 4 byte packed device number, 4 inode */
287 /* This is *host* endian, not net-byte-order, because
288 * no-one outside this host has any business interpreting it
292 memcpy(&dev, fsid, 4);
293 memcpy(&inode, fsid+4, 4);
294 major = (dev & 0xfff00) >> 8;
295 minor = (dev & 0xff) | ((dev >> 12) & 0xfff00);
298 case FSID_UUID4_INUM: /* 4 byte inode number and 4 byte uuid */
301 memcpy(&inode, fsid, 4);
305 case FSID_UUID8: /* 8 byte uuid */
311 case FSID_UUID16: /* 16 byte uuid */
317 case FSID_UUID16_INUM: /* 8 byte inode number and 16 byte uuid */
320 memcpy(&inode64, fsid, 8);
329 /* Now determine export point for this fsid/domain */
330 for (i=0 ; i < MCL_MAXTYPES; i++) {
331 for (exp = exportlist[i]; exp; exp = exp->m_next) {
335 if (!client_member(dom, exp->m_client->m_hostname))
337 if (exp->m_export.e_mountpoint &&
338 !is_mountpoint(exp->m_export.e_mountpoint[0]?
339 exp->m_export.e_mountpoint:
340 exp->m_export.e_path))
342 if (stat(exp->m_export.e_path, &stb) != 0)
346 case FSID_MAJOR_MINOR:
347 case FSID_ENCODE_DEV:
348 if (stb.st_ino != inode)
350 if (major != major(stb.st_dev) ||
351 minor != minor(stb.st_dev))
355 if (((exp->m_export.e_flags & NFSEXP_FSID) == 0 ||
356 exp->m_export.e_fsid != fsidnum))
359 case FSID_UUID4_INUM:
360 case FSID_UUID16_INUM:
361 if (stb.st_ino != inode)
366 if (!is_mountpoint(exp->m_export.e_path))
369 if (exp->m_export.e_uuid)
370 get_uuid(NULL, exp->m_export.e_uuid,
372 else if (get_uuid(exp->m_export.e_path, NULL,
376 if (memcmp(u, fhuuid, uuidlen) != 0)
380 /* It's a match !! */
382 found = &exp->m_export;
383 else if (strcmp(found->e_path, exp->m_export.e_path)!= 0)
385 xlog(L_WARNING, "%s and %s have same filehandle for %s, using first",
386 found->e_path, exp->m_export.e_path, dom);
391 found->e_mountpoint &&
392 !is_mountpoint(found->e_mountpoint[0]?
395 /* Cannot export this yet
396 * should log a warning, but need to rate limit
397 xlog(L_WARNING, "%s not exported as %d not a mountpoint",
398 found->e_path, found->e_mountpoint);
400 /* FIXME we need to make sure we re-visit this later */
403 if (!found && dev_missing) {
404 /* The missing dev could be what we want, so just be
405 * quite rather than returning stale yet
411 cache_export_ent(dom, found);
414 qword_printint(f, fsidtype);
415 qword_printhex(f, fsid, fsidlen);
416 /* The fsid -> path lookup can be quite expensive as it
417 * potentially stats and reads lots of devices, and some of those
418 * might have spun-down. The Answer is not likely to
419 * change underneath us, and an 'exportfs -f' can always
420 * remove this from the kernel, so use a really log
421 * timeout. Maybe this should be configurable on the command
424 qword_printint(f, 0x7fffffff);
426 qword_print(f, found->e_path);
433 static void write_fsloc(FILE *f, struct exportent *ep, char *path)
435 struct servers *servers;
437 if (ep->e_fslocmethod == FSLOC_NONE)
440 servers = replicas_lookup(ep->e_fslocmethod, ep->e_fslocdata, path);
443 qword_print(f, "fsloc");
444 qword_printint(f, servers->h_num);
445 if (servers->h_num >= 0) {
447 for (i=0; i<servers->h_num; i++) {
448 qword_print(f, servers->h_mp[i]->h_host);
449 qword_print(f, servers->h_mp[i]->h_path);
452 qword_printint(f, servers->h_referral);
453 release_replicas(servers);
456 static int dump_to_cache(FILE *f, char *domain, char *path, struct exportent *exp)
458 qword_print(f, domain);
459 qword_print(f, path);
460 qword_printint(f, time(0)+30*60);
462 qword_printint(f, exp->e_flags);
463 qword_printint(f, exp->e_anonuid);
464 qword_printint(f, exp->e_anongid);
465 qword_printint(f, exp->e_fsid);
466 write_fsloc(f, exp, path);
467 if (exp->e_uuid == NULL) {
469 if (get_uuid(exp->e_path, NULL, 16, u)) {
470 qword_print(f, "uuid");
471 qword_printhex(f, u, 16);
473 } else if (exp->e_uuid) {
474 qword_print(f, "uuid");
475 qword_printhex(f, exp->e_uuid, 16);
481 void nfsd_export(FILE *f)
485 * determine export options and return:
486 * domain path expiry flags anonuid anongid fsid
492 nfs_export *exp, *found = NULL;
495 if (readline(fileno(f), &lbuf, &lbuflen) != 1)
499 dom = malloc(strlen(cp));
500 path = malloc(strlen(cp));
505 if (qword_get(&cp, dom, strlen(lbuf)) <= 0)
507 if (qword_get(&cp, path, strlen(lbuf)) <= 0)
512 /* now find flags for this export point in this domain */
513 for (i=0 ; i < MCL_MAXTYPES; i++) {
514 for (exp = exportlist[i]; exp; exp = exp->m_next) {
515 if (!client_member(dom, exp->m_client->m_hostname))
517 if (strcmp(path, exp->m_export.e_path))
522 xlog(L_WARNING, "%s exported to both %s and %s in %s",
523 path, exp->m_client->m_hostname, found->m_client->m_hostname,
530 dump_to_cache(f, dom, path, &found->m_export);
531 mountlist_add(dom, path);
533 dump_to_cache(f, dom, path, NULL);
537 if (path) free(path);
543 void (*cache_handle)(FILE *f);
546 { "auth.unix.ip", auth_unix_ip},
547 { "auth.unix.gid", auth_unix_gid},
548 { "nfsd.export", nfsd_export},
549 { "nfsd.fh", nfsd_fh},
553 extern int manage_gids;
554 void cache_open(void)
557 for (i=0; cachelist[i].cache_name; i++ ) {
559 if (!manage_gids && cachelist[i].cache_handle == auth_unix_gid)
561 sprintf(path, "/proc/net/rpc/%s/channel", cachelist[i].cache_name);
562 cachelist[i].f = fopen(path, "r+");
566 void cache_set_fds(fd_set *fdset)
569 for (i=0; cachelist[i].cache_name; i++) {
571 FD_SET(fileno(cachelist[i].f), fdset);
575 int cache_process_req(fd_set *readfds)
579 for (i=0; cachelist[i].cache_name; i++) {
580 if (cachelist[i].f != NULL &&
581 FD_ISSET(fileno(cachelist[i].f), readfds)) {
583 cachelist[i].cache_handle(cachelist[i].f);
584 FD_CLR(fileno(cachelist[i].f), readfds);
592 * Give IP->domain and domain+path->options to kernel
593 * % echo nfsd $IP $[now+30*60] $domain > /proc/net/rpc/auth.unix.ip/channel
594 * % echo $domain $path $[now+30*60] $options $anonuid $anongid $fsid > /proc/net/rpc/nfsd.export/channel
597 int cache_export_ent(char *domain, struct exportent *exp)
600 FILE *f = fopen("/proc/net/rpc/nfsd.export/channel", "w");
604 err = dump_to_cache(f, domain, exp->e_path, exp);
606 mountlist_add(domain, exp->e_path);
610 int cache_export(nfs_export *exp)
615 f = fopen("/proc/net/rpc/auth.unix.ip/channel", "w");
619 qword_print(f, "nfsd");
620 qword_print(f, inet_ntoa(exp->m_client->m_addrlist[0]));
621 qword_printint(f, time(0)+30*60);
622 qword_print(f, exp->m_client->m_hostname);
627 err = cache_export_ent(exp->m_client->m_hostname, &exp->m_export)
634 * echo $domain $path $length
635 * read filehandle <&0
636 * } <> /proc/fs/nfsd/filehandle
639 cache_get_filehandle(nfs_export *exp, int len, char *p)
641 FILE *f = fopen("/proc/fs/nfsd/filehandle", "r+");
645 static struct nfs_fh_len fh;
648 f = fopen("/proc/fs/nfs/filehandle", "r+");
652 qword_print(f, exp->m_client->m_hostname);
654 qword_printint(f, len);
655 failed = qword_eol(f);
658 failed = (fgets(buf, sizeof(buf), f) == NULL);
662 memset(fh.fh_handle, 0, sizeof(fh.fh_handle));
663 fh.fh_size = qword_get(&bp, (char *)fh.fh_handle, NFS3_FHSIZE);