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)
402 case FSID_MAJOR_MINOR:
403 case FSID_ENCODE_DEV:
404 if (stb.st_ino != inode)
406 if (major != major(stb.st_dev) ||
407 minor != minor(stb.st_dev))
411 if (((exp->m_export.e_flags & NFSEXP_FSID) == 0 ||
412 exp->m_export.e_fsid != fsidnum))
415 case FSID_UUID4_INUM:
416 case FSID_UUID16_INUM:
417 if (stb.st_ino != inode)
422 if (!is_mountpoint(path))
426 if (exp->m_export.e_uuid)
427 get_uuid(NULL, exp->m_export.e_uuid,
429 else if (get_uuid(path, NULL,
433 if (memcmp(u, fhuuid, uuidlen) != 0)
440 /* It's a match !! */
442 found = &exp->m_export;
443 found_path = strdup(path);
444 } else if (strcmp(found->e_path, exp->m_export.e_path)!= 0)
446 xlog(L_WARNING, "%s and %s have same filehandle for %s, using first",
447 found_path, path, dom);
452 found->e_mountpoint &&
453 !is_mountpoint(found->e_mountpoint[0]?
456 /* Cannot export this yet
457 * should log a warning, but need to rate limit
458 xlog(L_WARNING, "%s not exported as %d not a mountpoint",
459 found->e_path, found->e_mountpoint);
461 /* FIXME we need to make sure we re-visit this later */
464 if (!found && dev_missing) {
465 /* The missing dev could be what we want, so just be
466 * quite rather than returning stale yet
472 cache_export_ent(dom, found, found_path);
475 qword_printint(f, fsidtype);
476 qword_printhex(f, fsid, fsidlen);
477 /* The fsid -> path lookup can be quite expensive as it
478 * potentially stats and reads lots of devices, and some of those
479 * might have spun-down. The Answer is not likely to
480 * change underneath us, and an 'exportfs -f' can always
481 * remove this from the kernel, so use a really log
482 * timeout. Maybe this should be configurable on the command
485 qword_printint(f, 0x7fffffff);
487 qword_print(f, found->e_path);
494 static void write_fsloc(FILE *f, struct exportent *ep, char *path)
496 struct servers *servers;
498 if (ep->e_fslocmethod == FSLOC_NONE)
501 servers = replicas_lookup(ep->e_fslocmethod, ep->e_fslocdata, path);
504 qword_print(f, "fsloc");
505 qword_printint(f, servers->h_num);
506 if (servers->h_num >= 0) {
508 for (i=0; i<servers->h_num; i++) {
509 qword_print(f, servers->h_mp[i]->h_host);
510 qword_print(f, servers->h_mp[i]->h_path);
513 qword_printint(f, servers->h_referral);
514 release_replicas(servers);
517 static int dump_to_cache(FILE *f, char *domain, char *path, struct exportent *exp)
519 qword_print(f, domain);
520 qword_print(f, path);
521 qword_printint(f, time(0)+30*60);
523 qword_printint(f, exp->e_flags);
524 qword_printint(f, exp->e_anonuid);
525 qword_printint(f, exp->e_anongid);
526 qword_printint(f, exp->e_fsid);
527 write_fsloc(f, exp, path);
529 if (exp->e_uuid == NULL) {
531 if (get_uuid(path, NULL, 16, u)) {
532 qword_print(f, "uuid");
533 qword_printhex(f, u, 16);
535 } else if (exp->e_uuid) {
536 qword_print(f, "uuid");
537 qword_printhex(f, exp->e_uuid, 16);
544 void nfsd_export(FILE *f)
548 * determine export options and return:
549 * domain path expiry flags anonuid anongid fsid
555 nfs_export *exp, *found = NULL;
559 if (readline(fileno(f), &lbuf, &lbuflen) != 1)
563 dom = malloc(strlen(cp));
564 path = malloc(strlen(cp));
569 if (qword_get(&cp, dom, strlen(lbuf)) <= 0)
571 if (qword_get(&cp, path, strlen(lbuf)) <= 0)
576 /* now find flags for this export point in this domain */
577 for (i=0 ; i < MCL_MAXTYPES; i++) {
578 for (exp = exportlist[i]; exp; exp = exp->m_next) {
579 if (!client_member(dom, exp->m_client->m_hostname))
581 if (exp->m_export.e_flags & NFSEXP_CROSSMOUNT) {
582 /* if path is a mountpoint below e_path, then OK */
583 int l = strlen(exp->m_export.e_path);
584 if (strcmp(path, exp->m_export.e_path) == 0 ||
585 (strncmp(path, exp->m_export.e_path, l) == 0 &&
587 is_mountpoint(path)))
591 } else if (strcmp(path, exp->m_export.e_path) != 0)
598 /* If one is a CROSSMOUNT, then prefer the longest path */
599 if (((found->m_export.e_flags & NFSEXP_CROSSMOUNT) ||
600 (found->m_export.e_flags & NFSEXP_CROSSMOUNT)) &&
601 strlen(found->m_export.e_path) !=
602 strlen(found->m_export.e_path)) {
604 if (strlen(exp->m_export.e_path) >
605 strlen(found->m_export.e_path)) {
611 } else if (found_type == i && found->m_warned == 0) {
612 xlog(L_WARNING, "%s exported to both %s and %s, "
613 "arbitrarily choosing options from first",
614 path, found->m_client->m_hostname, exp->m_client->m_hostname,
622 dump_to_cache(f, dom, path, &found->m_export);
623 mountlist_add(dom, path);
625 dump_to_cache(f, dom, path, NULL);
629 if (path) free(path);
635 void (*cache_handle)(FILE *f);
638 { "auth.unix.ip", auth_unix_ip},
639 { "auth.unix.gid", auth_unix_gid},
640 { "nfsd.export", nfsd_export},
641 { "nfsd.fh", nfsd_fh},
645 extern int manage_gids;
646 void cache_open(void)
649 for (i=0; cachelist[i].cache_name; i++ ) {
651 if (!manage_gids && cachelist[i].cache_handle == auth_unix_gid)
653 sprintf(path, "/proc/net/rpc/%s/channel", cachelist[i].cache_name);
654 cachelist[i].f = fopen(path, "r+");
658 void cache_set_fds(fd_set *fdset)
661 for (i=0; cachelist[i].cache_name; i++) {
663 FD_SET(fileno(cachelist[i].f), fdset);
667 int cache_process_req(fd_set *readfds)
671 for (i=0; cachelist[i].cache_name; i++) {
672 if (cachelist[i].f != NULL &&
673 FD_ISSET(fileno(cachelist[i].f), readfds)) {
675 cachelist[i].cache_handle(cachelist[i].f);
676 FD_CLR(fileno(cachelist[i].f), readfds);
684 * Give IP->domain and domain+path->options to kernel
685 * % echo nfsd $IP $[now+30*60] $domain > /proc/net/rpc/auth.unix.ip/channel
686 * % echo $domain $path $[now+30*60] $options $anonuid $anongid $fsid > /proc/net/rpc/nfsd.export/channel
689 int cache_export_ent(char *domain, struct exportent *exp, char *path)
692 FILE *f = fopen("/proc/net/rpc/nfsd.export/channel", "w");
696 err = dump_to_cache(f, domain, exp->e_path, exp);
697 mountlist_add(domain, exp->e_path);
699 while ((exp->e_flags & NFSEXP_CROSSMOUNT) && path) {
700 /* really an 'if', but we can break out of
701 * a 'while' more easily */
702 /* Look along 'path' for other filesystems
703 * and export them with the same options
706 int l = strlen(exp->e_path);
709 if (strlen(path) <= l || path[l] != '/' ||
710 strncmp(exp->e_path, path, l) != 0)
712 if (stat(exp->e_path, &stb) != 0)
715 while(path[l] == '/') {
720 while (path[l] != '/' && path[l])
724 err = lstat(path, &stb);
728 if (stb.st_dev == dev)
732 dump_to_cache(f, domain, path, exp);
742 int cache_export(nfs_export *exp, char *path)
747 f = fopen("/proc/net/rpc/auth.unix.ip/channel", "w");
751 qword_print(f, "nfsd");
752 qword_print(f, inet_ntoa(exp->m_client->m_addrlist[0]));
753 qword_printint(f, time(0)+30*60);
754 qword_print(f, exp->m_client->m_hostname);
759 err = cache_export_ent(exp->m_client->m_hostname, &exp->m_export, path)
766 * echo $domain $path $length
767 * read filehandle <&0
768 * } <> /proc/fs/nfsd/filehandle
771 cache_get_filehandle(nfs_export *exp, int len, char *p)
773 FILE *f = fopen("/proc/fs/nfsd/filehandle", "r+");
777 static struct nfs_fh_len fh;
780 f = fopen("/proc/fs/nfs/filehandle", "r+");
784 qword_print(f, exp->m_client->m_hostname);
786 qword_printint(f, len);
787 failed = qword_eol(f);
790 failed = (fgets(buf, sizeof(buf), f) == NULL);
794 memset(fh.fh_handle, 0, sizeof(fh.fh_handle));
795 fh.fh_size = qword_get(&bp, (char *)fh.fh_handle, NFS3_FHSIZE);