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>
35 #include "blkid/blkid.h"
51 * Support routines for text-based upcalls.
52 * Fields are separated by spaces.
53 * Fields are either mangled to quote space tab newline slosh with slosh
54 * or a hexified with a leading \x
55 * Record is terminated with newline.
58 int cache_export_ent(char *domain, struct exportent *exp, char *p);
64 void auth_unix_ip(FILE *f)
68 * Ignore if class != "nfsd"
69 * Otherwise find domainname and write back:
71 * "nfsd" IP-ADDR expiry domainname
78 if (readline(fileno(f), &lbuf, &lbuflen) != 1)
83 if (qword_get(&cp, class, 20) <= 0 ||
84 strcmp(class, "nfsd") != 0)
87 if (qword_get(&cp, ipaddr, 20) <= 0)
90 if (inet_aton(ipaddr, &addr)==0)
95 /* addr is a valid, interesting address, find the domain name... */
96 client = client_compose(addr);
99 qword_print(f, "nfsd");
100 qword_print(f, ipaddr);
101 qword_printint(f, time(0)+30*60);
103 qword_print(f, *client?client:"DEFAULT");
106 if (client) free(client);
110 void auth_unix_gid(FILE *f)
115 * uid expiry count list of group ids
119 gid_t glist[100], *groups = glist;
124 if (readline(fileno(f), &lbuf, &lbuflen) != 1)
128 if (qword_get_int(&cp, &uid) != 0)
135 rv = getgrouplist(pw->pw_name, pw->pw_gid, groups, &ngroups);
136 if (rv == -1 && ngroups >= 100) {
137 groups = malloc(sizeof(gid_t)*ngroups);
141 rv = getgrouplist(pw->pw_name, pw->pw_gid,
145 qword_printint(f, uid);
146 qword_printint(f, time(0)+30*60);
148 qword_printint(f, ngroups);
149 for (i=0; i<ngroups; i++)
150 qword_printint(f, groups[i]);
158 int get_uuid(char *path, char *uuid, int uuidlen, char *u)
160 /* extract hex digits from uuidstr and compose a uuid
161 * of the given length (max 16), xoring bytes to make
162 * a smaller uuid. Then compare with uuid
168 static blkid_cache cache = NULL;
171 blkid_tag_iterate iter;
175 blkid_get_cache(&cache, NULL);
177 blkid_probe_all_new(cache);
179 if (stat(path, &stb) != 0)
181 devname = blkid_devno_to_devname(stb.st_dev);
184 dev = blkid_get_dev(cache, devname, BLKID_DEV_NORMAL);
188 iter = blkid_tag_iterate_begin(dev);
191 while (blkid_tag_next(iter, &type, &val) == 0)
192 if (strcmp(type, "UUID") == 0)
194 blkid_tag_iterate_end(iter);
201 memset(u, 0, uuidlen);
202 for ( ; *val ; val++) {
224 /* Iterate through /etc/mtab, finding mountpoints
225 * at or below a given path
227 static char *next_mnt(void **v, char *p)
233 f = setmntent("/etc/mtab", "r");
237 while ((me = getmntent(f)) != NULL &&
238 (strncmp(me->mnt_dir, p, l) != 0 ||
239 me->mnt_dir[l] != '/'))
249 void nfsd_fh(FILE *f)
252 * domain fsidtype fsid
253 * interpret fsid, find export point and options, and write:
254 * domain fsidtype fsid expiry path
260 unsigned int dev, major=0, minor=0;
261 unsigned int inode=0;
262 unsigned long long inode64;
263 unsigned int fsidnum=0;
265 struct exportent *found = NULL;
266 char *found_path = NULL;
273 if (readline(fileno(f), &lbuf, &lbuflen) != 1)
278 dom = malloc(strlen(cp));
281 if (qword_get(&cp, dom, strlen(cp)) <= 0)
283 if (qword_get_int(&cp, &fsidtype) != 0)
285 if (fsidtype < 0 || fsidtype > 7)
286 goto out; /* unknown type */
287 if ((fsidlen = qword_get(&cp, fsid, 32)) <= 0)
290 case FSID_DEV: /* 4 bytes: 2 major, 2 minor, 4 inode */
293 memcpy(&dev, fsid, 4);
294 memcpy(&inode, fsid+4, 4);
295 major = ntohl(dev)>>16;
296 minor = ntohl(dev) & 0xFFFF;
299 case FSID_NUM: /* 4 bytes - fsid */
302 memcpy(&fsidnum, fsid, 4);
305 case FSID_MAJOR_MINOR: /* 12 bytes: 4 major, 4 minor, 4 inode
306 * This format is never actually used but was
307 * an historical accident
311 memcpy(&dev, fsid, 4); major = ntohl(dev);
312 memcpy(&dev, fsid+4, 4); minor = ntohl(dev);
313 memcpy(&inode, fsid+8, 4);
316 case FSID_ENCODE_DEV: /* 8 bytes: 4 byte packed device number, 4 inode */
317 /* This is *host* endian, not net-byte-order, because
318 * no-one outside this host has any business interpreting it
322 memcpy(&dev, fsid, 4);
323 memcpy(&inode, fsid+4, 4);
324 major = (dev & 0xfff00) >> 8;
325 minor = (dev & 0xff) | ((dev >> 12) & 0xfff00);
328 case FSID_UUID4_INUM: /* 4 byte inode number and 4 byte uuid */
331 memcpy(&inode, fsid, 4);
335 case FSID_UUID8: /* 8 byte uuid */
341 case FSID_UUID16: /* 16 byte uuid */
347 case FSID_UUID16_INUM: /* 8 byte inode number and 16 byte uuid */
350 memcpy(&inode64, fsid, 8);
359 /* Now determine export point for this fsid/domain */
360 for (i=0 ; i < MCL_MAXTYPES; i++) {
361 nfs_export *next_exp;
362 for (exp = exportlist[i]; exp; exp = next_exp) {
367 if (exp->m_export.e_flags & NFSEXP_CROSSMOUNT) {
368 static nfs_export *prev = NULL;
369 static void *mnt = NULL;
373 path = next_mnt(&mnt, exp->m_export.e_path);
375 next_exp = exp->m_next;
383 path = exp->m_export.e_path;
387 path = exp->m_export.e_path;
388 next_exp = exp->m_next;
391 if (!client_member(dom, exp->m_client->m_hostname))
393 if (exp->m_export.e_mountpoint &&
394 !is_mountpoint(exp->m_export.e_mountpoint[0]?
395 exp->m_export.e_mountpoint:
396 exp->m_export.e_path))
398 if (stat(path, &stb) != 0)
400 if (!S_ISDIR(stb.st_mode) && !S_ISREG(stb.st_mode)) {
405 case FSID_MAJOR_MINOR:
406 case FSID_ENCODE_DEV:
407 if (stb.st_ino != inode)
409 if (major != major(stb.st_dev) ||
410 minor != minor(stb.st_dev))
414 if (((exp->m_export.e_flags & NFSEXP_FSID) == 0 ||
415 exp->m_export.e_fsid != fsidnum))
418 case FSID_UUID4_INUM:
419 case FSID_UUID16_INUM:
420 if (stb.st_ino != inode)
425 if (!is_mountpoint(path))
429 if (exp->m_export.e_uuid)
430 get_uuid(NULL, exp->m_export.e_uuid,
432 else if (get_uuid(path, NULL,
436 if (memcmp(u, fhuuid, uuidlen) != 0)
443 /* It's a match !! */
445 found = &exp->m_export;
446 found_path = strdup(path);
447 } else if (strcmp(found->e_path, exp->m_export.e_path)!= 0)
449 xlog(L_WARNING, "%s and %s have same filehandle for %s, using first",
450 found_path, path, dom);
455 found->e_mountpoint &&
456 !is_mountpoint(found->e_mountpoint[0]?
459 /* Cannot export this yet
460 * should log a warning, but need to rate limit
461 xlog(L_WARNING, "%s not exported as %d not a mountpoint",
462 found->e_path, found->e_mountpoint);
464 /* FIXME we need to make sure we re-visit this later */
467 if (!found && dev_missing) {
468 /* The missing dev could be what we want, so just be
469 * quite rather than returning stale yet
475 if (cache_export_ent(dom, found, found_path) < 0)
479 qword_printint(f, fsidtype);
480 qword_printhex(f, fsid, fsidlen);
481 /* The fsid -> path lookup can be quite expensive as it
482 * potentially stats and reads lots of devices, and some of those
483 * might have spun-down. The Answer is not likely to
484 * change underneath us, and an 'exportfs -f' can always
485 * remove this from the kernel, so use a really log
486 * timeout. Maybe this should be configurable on the command
489 qword_printint(f, 0x7fffffff);
491 qword_print(f, found->e_path);
498 static void write_fsloc(FILE *f, struct exportent *ep, char *path)
500 struct servers *servers;
502 if (ep->e_fslocmethod == FSLOC_NONE)
505 servers = replicas_lookup(ep->e_fslocmethod, ep->e_fslocdata, path);
508 qword_print(f, "fsloc");
509 qword_printint(f, servers->h_num);
510 if (servers->h_num >= 0) {
512 for (i=0; i<servers->h_num; i++) {
513 qword_print(f, servers->h_mp[i]->h_host);
514 qword_print(f, servers->h_mp[i]->h_path);
517 qword_printint(f, servers->h_referral);
518 release_replicas(servers);
521 static int dump_to_cache(FILE *f, char *domain, char *path, struct exportent *exp)
523 qword_print(f, domain);
524 qword_print(f, path);
525 qword_printint(f, time(0)+30*60);
527 qword_printint(f, exp->e_flags);
528 qword_printint(f, exp->e_anonuid);
529 qword_printint(f, exp->e_anongid);
530 qword_printint(f, exp->e_fsid);
531 write_fsloc(f, exp, path);
533 if (exp->e_uuid == NULL) {
535 if (get_uuid(path, NULL, 16, u)) {
536 qword_print(f, "uuid");
537 qword_printhex(f, u, 16);
539 } else if (exp->e_uuid) {
540 qword_print(f, "uuid");
541 qword_printhex(f, exp->e_uuid, 16);
548 void nfsd_export(FILE *f)
552 * determine export options and return:
553 * domain path expiry flags anonuid anongid fsid
559 nfs_export *exp, *found = NULL;
563 if (readline(fileno(f), &lbuf, &lbuflen) != 1)
567 dom = malloc(strlen(cp));
568 path = malloc(strlen(cp));
573 if (qword_get(&cp, dom, strlen(lbuf)) <= 0)
575 if (qword_get(&cp, path, strlen(lbuf)) <= 0)
580 /* now find flags for this export point in this domain */
581 for (i=0 ; i < MCL_MAXTYPES; i++) {
582 for (exp = exportlist[i]; exp; exp = exp->m_next) {
583 if (!client_member(dom, exp->m_client->m_hostname))
585 if (exp->m_export.e_flags & NFSEXP_CROSSMOUNT) {
586 /* if path is a mountpoint below e_path, then OK */
587 int l = strlen(exp->m_export.e_path);
588 if (strcmp(path, exp->m_export.e_path) == 0 ||
589 (strncmp(path, exp->m_export.e_path, l) == 0 &&
591 is_mountpoint(path)))
595 } else if (strcmp(path, exp->m_export.e_path) != 0)
602 /* If one is a CROSSMOUNT, then prefer the longest path */
603 if (((found->m_export.e_flags & NFSEXP_CROSSMOUNT) ||
604 (found->m_export.e_flags & NFSEXP_CROSSMOUNT)) &&
605 strlen(found->m_export.e_path) !=
606 strlen(found->m_export.e_path)) {
608 if (strlen(exp->m_export.e_path) >
609 strlen(found->m_export.e_path)) {
615 } else if (found_type == i && found->m_warned == 0) {
616 xlog(L_WARNING, "%s exported to both %s and %s, "
617 "arbitrarily choosing options from first",
618 path, found->m_client->m_hostname, exp->m_client->m_hostname,
626 if (dump_to_cache(f, dom, path, &found->m_export) < 0) {
628 "Cannot export %s, possibly unsupported filesystem"
629 " or fsid= required", path);
630 dump_to_cache(f, dom, path, NULL);
632 mountlist_add(dom, path);
634 dump_to_cache(f, dom, path, NULL);
638 if (path) free(path);
644 void (*cache_handle)(FILE *f);
647 { "auth.unix.ip", auth_unix_ip},
648 { "auth.unix.gid", auth_unix_gid},
649 { "nfsd.export", nfsd_export},
650 { "nfsd.fh", nfsd_fh},
654 extern int manage_gids;
655 void cache_open(void)
658 for (i=0; cachelist[i].cache_name; i++ ) {
660 if (!manage_gids && cachelist[i].cache_handle == auth_unix_gid)
662 sprintf(path, "/proc/net/rpc/%s/channel", cachelist[i].cache_name);
663 cachelist[i].f = fopen(path, "r+");
667 void cache_set_fds(fd_set *fdset)
670 for (i=0; cachelist[i].cache_name; i++) {
672 FD_SET(fileno(cachelist[i].f), fdset);
676 int cache_process_req(fd_set *readfds)
680 for (i=0; cachelist[i].cache_name; i++) {
681 if (cachelist[i].f != NULL &&
682 FD_ISSET(fileno(cachelist[i].f), readfds)) {
684 cachelist[i].cache_handle(cachelist[i].f);
685 FD_CLR(fileno(cachelist[i].f), readfds);
693 * Give IP->domain and domain+path->options to kernel
694 * % echo nfsd $IP $[now+30*60] $domain > /proc/net/rpc/auth.unix.ip/channel
695 * % echo $domain $path $[now+30*60] $options $anonuid $anongid $fsid > /proc/net/rpc/nfsd.export/channel
698 int cache_export_ent(char *domain, struct exportent *exp, char *path)
701 FILE *f = fopen("/proc/net/rpc/nfsd.export/channel", "w");
705 err = dump_to_cache(f, domain, exp->e_path, exp);
708 "Cannot export %s, possibly unsupported filesystem or"
709 " fsid= required", exp->e_path);
711 mountlist_add(domain, exp->e_path);
713 while (err == 0 && (exp->e_flags & NFSEXP_CROSSMOUNT) && path) {
714 /* really an 'if', but we can break out of
715 * a 'while' more easily */
716 /* Look along 'path' for other filesystems
717 * and export them with the same options
720 int l = strlen(exp->e_path);
723 if (strlen(path) <= l || path[l] != '/' ||
724 strncmp(exp->e_path, path, l) != 0)
726 if (stat(exp->e_path, &stb) != 0)
729 while(path[l] == '/') {
731 /* errors for submount should fail whole filesystem */
735 while (path[l] != '/' && path[l])
739 err2 = lstat(path, &stb);
743 if (stb.st_dev == dev)
747 dump_to_cache(f, domain, path, exp);
757 int cache_export(nfs_export *exp, char *path)
762 f = fopen("/proc/net/rpc/auth.unix.ip/channel", "w");
766 qword_print(f, "nfsd");
767 qword_print(f, inet_ntoa(exp->m_client->m_addrlist[0]));
768 qword_printint(f, time(0)+30*60);
769 qword_print(f, exp->m_client->m_hostname);
774 err = cache_export_ent(exp->m_client->m_hostname, &exp->m_export, path)
781 * echo $domain $path $length
782 * read filehandle <&0
783 * } <> /proc/fs/nfsd/filehandle
786 cache_get_filehandle(nfs_export *exp, int len, char *p)
788 FILE *f = fopen("/proc/fs/nfsd/filehandle", "r+");
792 static struct nfs_fh_len fh;
795 f = fopen("/proc/fs/nfs/filehandle", "r+");
799 qword_print(f, exp->m_client->m_hostname);
801 qword_printint(f, len);
802 failed = qword_eol(f);
805 failed = (fgets(buf, sizeof(buf), f) == NULL);
809 memset(fh.fh_handle, 0, sizeof(fh.fh_handle));
810 fh.fh_size = qword_get(&bp, (char *)fh.fh_handle, NFS3_FHSIZE);