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>
34 #include "blkid/blkid.h"
49 * Support routines for text-based upcalls.
50 * Fields are separated by spaces.
51 * Fields are either mangled to quote space tab newline slosh with slosh
52 * or a hexified with a leading \x
53 * Record is terminated with newline.
56 int cache_export_ent(char *domain, struct exportent *exp, char *p);
62 void auth_unix_ip(FILE *f)
66 * Ignore if class != "nfsd"
67 * Otherwise find domainname and write back:
69 * "nfsd" IP-ADDR expiry domainname
76 if (readline(fileno(f), &lbuf, &lbuflen) != 1)
81 if (qword_get(&cp, class, 20) <= 0 ||
82 strcmp(class, "nfsd") != 0)
85 if (qword_get(&cp, ipaddr, 20) <= 0)
88 if (inet_aton(ipaddr, &addr)==0)
93 /* addr is a valid, interesting address, find the domain name... */
94 client = client_compose(addr);
97 qword_print(f, "nfsd");
98 qword_print(f, ipaddr);
99 qword_printint(f, time(0)+30*60);
101 qword_print(f, *client?client:"DEFAULT");
104 if (client) free(client);
108 void auth_unix_gid(FILE *f)
113 * uid expiry count list of group ids
117 gid_t glist[100], *groups = glist;
122 if (readline(fileno(f), &lbuf, &lbuflen) != 1)
126 if (qword_get_int(&cp, &uid) != 0)
133 rv = getgrouplist(pw->pw_name, pw->pw_gid, groups, &ngroups);
134 if (rv == -1 && ngroups >= 100) {
135 groups = malloc(sizeof(gid_t)*ngroups);
139 rv = getgrouplist(pw->pw_name, pw->pw_gid,
143 qword_printint(f, uid);
144 qword_printint(f, time(0)+30*60);
146 qword_printint(f, ngroups);
147 for (i=0; i<ngroups; i++)
148 qword_printint(f, groups[i]);
155 int get_uuid(char *path, char *uuid, int uuidlen, char *u)
157 /* extract hex digits from uuidstr and compose a uuid
158 * of the given length (max 16), xoring bytes to make
159 * a smaller uuid. Then compare with uuid
165 static blkid_cache cache = NULL;
168 blkid_tag_iterate iter;
172 blkid_get_cache(&cache, NULL);
174 blkid_probe_all_new(cache);
176 if (stat(path, &stb) != 0)
178 devname = blkid_devno_to_devname(stb.st_dev);
181 dev = blkid_get_dev(cache, devname, BLKID_DEV_NORMAL);
185 iter = blkid_tag_iterate_begin(dev);
188 while (blkid_tag_next(iter, &type, &val) == 0)
189 if (strcmp(type, "UUID") == 0)
191 blkid_tag_iterate_end(iter);
198 memset(u, 0, uuidlen);
199 for ( ; *val ; val++) {
220 /* Iterate through /etc/mtab, finding mountpoints
221 * at or below a given path
223 static char *next_mnt(void **v, char *p)
229 f = setmntent("/etc/mtab", "r");
233 while ((me = getmntent(f)) != NULL &&
234 (strncmp(me->mnt_dir, p, l) != 0 ||
235 me->mnt_dir[l] != '/'))
245 void nfsd_fh(FILE *f)
248 * domain fsidtype fsid
249 * interpret fsid, find export point and options, and write:
250 * domain fsidtype fsid expiry path
256 unsigned int dev, major=0, minor=0;
257 unsigned int inode=0;
258 unsigned long long inode64;
259 unsigned int fsidnum=0;
261 struct exportent *found = NULL;
262 char *found_path = NULL;
269 if (readline(fileno(f), &lbuf, &lbuflen) != 1)
274 dom = malloc(strlen(cp));
277 if (qword_get(&cp, dom, strlen(cp)) <= 0)
279 if (qword_get_int(&cp, &fsidtype) != 0)
281 if (fsidtype < 0 || fsidtype > 7)
282 goto out; /* unknown type */
283 if ((fsidlen = qword_get(&cp, fsid, 32)) <= 0)
286 case FSID_DEV: /* 4 bytes: 2 major, 2 minor, 4 inode */
289 memcpy(&dev, fsid, 4);
290 memcpy(&inode, fsid+4, 4);
291 major = ntohl(dev)>>16;
292 minor = ntohl(dev) & 0xFFFF;
295 case FSID_NUM: /* 4 bytes - fsid */
298 memcpy(&fsidnum, fsid, 4);
301 case FSID_MAJOR_MINOR: /* 12 bytes: 4 major, 4 minor, 4 inode
302 * This format is never actually used but was
303 * an historical accident
307 memcpy(&dev, fsid, 4); major = ntohl(dev);
308 memcpy(&dev, fsid+4, 4); minor = ntohl(dev);
309 memcpy(&inode, fsid+8, 4);
312 case FSID_ENCODE_DEV: /* 8 bytes: 4 byte packed device number, 4 inode */
313 /* This is *host* endian, not net-byte-order, because
314 * no-one outside this host has any business interpreting it
318 memcpy(&dev, fsid, 4);
319 memcpy(&inode, fsid+4, 4);
320 major = (dev & 0xfff00) >> 8;
321 minor = (dev & 0xff) | ((dev >> 12) & 0xfff00);
324 case FSID_UUID4_INUM: /* 4 byte inode number and 4 byte uuid */
327 memcpy(&inode, fsid, 4);
331 case FSID_UUID8: /* 8 byte uuid */
337 case FSID_UUID16: /* 16 byte uuid */
343 case FSID_UUID16_INUM: /* 8 byte inode number and 16 byte uuid */
346 memcpy(&inode64, fsid, 8);
355 /* Now determine export point for this fsid/domain */
356 for (i=0 ; i < MCL_MAXTYPES; i++) {
357 nfs_export *next_exp;
358 for (exp = exportlist[i]; exp; exp = next_exp) {
363 if (exp->m_export.e_flags & NFSEXP_CROSSMOUNT) {
364 static nfs_export *prev = NULL;
365 static void *mnt = NULL;
369 path = next_mnt(&mnt, exp->m_export.e_path);
371 next_exp = exp->m_next;
379 path = exp->m_export.e_path;
383 path = exp->m_export.e_path;
384 next_exp = exp->m_next;
387 if (!client_member(dom, exp->m_client->m_hostname))
389 if (exp->m_export.e_mountpoint &&
390 !is_mountpoint(exp->m_export.e_mountpoint[0]?
391 exp->m_export.e_mountpoint:
392 exp->m_export.e_path))
394 if (stat(path, &stb) != 0)
398 case FSID_MAJOR_MINOR:
399 case FSID_ENCODE_DEV:
400 if (stb.st_ino != inode)
402 if (major != major(stb.st_dev) ||
403 minor != minor(stb.st_dev))
407 if (((exp->m_export.e_flags & NFSEXP_FSID) == 0 ||
408 exp->m_export.e_fsid != fsidnum))
411 case FSID_UUID4_INUM:
412 case FSID_UUID16_INUM:
413 if (stb.st_ino != inode)
418 if (!is_mountpoint(path))
421 if (exp->m_export.e_uuid)
422 get_uuid(NULL, exp->m_export.e_uuid,
424 else if (get_uuid(path, NULL,
428 if (memcmp(u, fhuuid, uuidlen) != 0)
432 /* It's a match !! */
434 found = &exp->m_export;
435 found_path = strdup(path);
436 } else if (strcmp(found->e_path, exp->m_export.e_path)!= 0)
438 xlog(L_WARNING, "%s and %s have same filehandle for %s, using first",
439 found_path, path, dom);
444 found->e_mountpoint &&
445 !is_mountpoint(found->e_mountpoint[0]?
448 /* Cannot export this yet
449 * should log a warning, but need to rate limit
450 xlog(L_WARNING, "%s not exported as %d not a mountpoint",
451 found->e_path, found->e_mountpoint);
453 /* FIXME we need to make sure we re-visit this later */
456 if (!found && dev_missing) {
457 /* The missing dev could be what we want, so just be
458 * quite rather than returning stale yet
464 cache_export_ent(dom, found, found_path);
467 qword_printint(f, fsidtype);
468 qword_printhex(f, fsid, fsidlen);
469 /* The fsid -> path lookup can be quite expensive as it
470 * potentially stats and reads lots of devices, and some of those
471 * might have spun-down. The Answer is not likely to
472 * change underneath us, and an 'exportfs -f' can always
473 * remove this from the kernel, so use a really log
474 * timeout. Maybe this should be configurable on the command
477 qword_printint(f, 0x7fffffff);
479 qword_print(f, found->e_path);
486 static void write_fsloc(FILE *f, struct exportent *ep, char *path)
488 struct servers *servers;
490 if (ep->e_fslocmethod == FSLOC_NONE)
493 servers = replicas_lookup(ep->e_fslocmethod, ep->e_fslocdata, path);
496 qword_print(f, "fsloc");
497 qword_printint(f, servers->h_num);
498 if (servers->h_num >= 0) {
500 for (i=0; i<servers->h_num; i++) {
501 qword_print(f, servers->h_mp[i]->h_host);
502 qword_print(f, servers->h_mp[i]->h_path);
505 qword_printint(f, servers->h_referral);
506 release_replicas(servers);
509 static int dump_to_cache(FILE *f, char *domain, char *path, struct exportent *exp)
511 qword_print(f, domain);
512 qword_print(f, path);
513 qword_printint(f, time(0)+30*60);
515 qword_printint(f, exp->e_flags);
516 qword_printint(f, exp->e_anonuid);
517 qword_printint(f, exp->e_anongid);
518 qword_printint(f, exp->e_fsid);
519 write_fsloc(f, exp, path);
520 if (exp->e_uuid == NULL) {
522 if (get_uuid(path, NULL, 16, u)) {
523 qword_print(f, "uuid");
524 qword_printhex(f, u, 16);
526 } else if (exp->e_uuid) {
527 qword_print(f, "uuid");
528 qword_printhex(f, exp->e_uuid, 16);
534 void nfsd_export(FILE *f)
538 * determine export options and return:
539 * domain path expiry flags anonuid anongid fsid
545 nfs_export *exp, *found = NULL;
549 if (readline(fileno(f), &lbuf, &lbuflen) != 1)
553 dom = malloc(strlen(cp));
554 path = malloc(strlen(cp));
559 if (qword_get(&cp, dom, strlen(lbuf)) <= 0)
561 if (qword_get(&cp, path, strlen(lbuf)) <= 0)
566 /* now find flags for this export point in this domain */
567 for (i=0 ; i < MCL_MAXTYPES; i++) {
568 for (exp = exportlist[i]; exp; exp = exp->m_next) {
569 if (!client_member(dom, exp->m_client->m_hostname))
571 if (exp->m_export.e_flags & NFSEXP_CROSSMOUNT) {
572 /* if path is a mountpoint below e_path, then OK */
573 int l = strlen(exp->m_export.e_path);
574 if (strcmp(path, exp->m_export.e_path) == 0 ||
575 (strncmp(path, exp->m_export.e_path, l) == 0 &&
577 is_mountpoint(path)))
581 } else if (strcmp(path, exp->m_export.e_path) != 0)
588 /* If one is a CROSSMOUNT, then prefer the longest path */
589 if (((found->m_export.e_flags & NFSEXP_CROSSMOUNT) ||
590 (found->m_export.e_flags & NFSEXP_CROSSMOUNT)) &&
591 strlen(found->m_export.e_path) !=
592 strlen(found->m_export.e_path)) {
594 if (strlen(exp->m_export.e_path) >
595 strlen(found->m_export.e_path)) {
601 } else if (found_type == i && found->m_warned == 0) {
602 xlog(L_WARNING, "%s exported to both %s and %s, "
603 "arbitrarily choosing options from first",
604 path, found->m_client->m_hostname, exp->m_client->m_hostname,
612 dump_to_cache(f, dom, path, &found->m_export);
613 mountlist_add(dom, path);
615 dump_to_cache(f, dom, path, NULL);
619 if (path) free(path);
625 void (*cache_handle)(FILE *f);
628 { "auth.unix.ip", auth_unix_ip},
629 { "auth.unix.gid", auth_unix_gid},
630 { "nfsd.export", nfsd_export},
631 { "nfsd.fh", nfsd_fh},
635 extern int manage_gids;
636 void cache_open(void)
639 for (i=0; cachelist[i].cache_name; i++ ) {
641 if (!manage_gids && cachelist[i].cache_handle == auth_unix_gid)
643 sprintf(path, "/proc/net/rpc/%s/channel", cachelist[i].cache_name);
644 cachelist[i].f = fopen(path, "r+");
648 void cache_set_fds(fd_set *fdset)
651 for (i=0; cachelist[i].cache_name; i++) {
653 FD_SET(fileno(cachelist[i].f), fdset);
657 int cache_process_req(fd_set *readfds)
661 for (i=0; cachelist[i].cache_name; i++) {
662 if (cachelist[i].f != NULL &&
663 FD_ISSET(fileno(cachelist[i].f), readfds)) {
665 cachelist[i].cache_handle(cachelist[i].f);
666 FD_CLR(fileno(cachelist[i].f), readfds);
674 * Give IP->domain and domain+path->options to kernel
675 * % echo nfsd $IP $[now+30*60] $domain > /proc/net/rpc/auth.unix.ip/channel
676 * % echo $domain $path $[now+30*60] $options $anonuid $anongid $fsid > /proc/net/rpc/nfsd.export/channel
679 int cache_export_ent(char *domain, struct exportent *exp, char *path)
682 FILE *f = fopen("/proc/net/rpc/nfsd.export/channel", "w");
686 err = dump_to_cache(f, domain, exp->e_path, exp);
687 mountlist_add(domain, exp->e_path);
689 while ((exp->e_flags & NFSEXP_CROSSMOUNT) && path) {
690 /* really an 'if', but we can break out of
691 * a 'while' more easily */
692 /* Look along 'path' for other filesystems
693 * and export them with the same options
696 int l = strlen(exp->e_path);
699 if (strlen(path) <= l || path[l] != '/' ||
700 strncmp(exp->e_path, path, l) != 0)
702 if (stat(exp->e_path, &stb) != 0)
705 while(path[l] == '/') {
710 while (path[l] != '/' && path[l])
714 err = lstat(path, &stb);
718 if (stb.st_dev == dev)
722 dump_to_cache(f, domain, path, exp);
732 int cache_export(nfs_export *exp, char *path)
737 f = fopen("/proc/net/rpc/auth.unix.ip/channel", "w");
741 qword_print(f, "nfsd");
742 qword_print(f, inet_ntoa(exp->m_client->m_addrlist[0]));
743 qword_printint(f, time(0)+30*60);
744 qword_print(f, exp->m_client->m_hostname);
749 err = cache_export_ent(exp->m_client->m_hostname, &exp->m_export, path)
756 * echo $domain $path $length
757 * read filehandle <&0
758 * } <> /proc/fs/nfsd/filehandle
761 cache_get_filehandle(nfs_export *exp, int len, char *p)
763 FILE *f = fopen("/proc/fs/nfsd/filehandle", "r+");
767 static struct nfs_fh_len fh;
770 f = fopen("/proc/fs/nfs/filehandle", "r+");
774 qword_print(f, exp->m_client->m_hostname);
776 qword_printint(f, len);
777 failed = qword_eol(f);
780 failed = (fgets(buf, sizeof(buf), f) == NULL);
784 memset(fh.fh_handle, 0, sizeof(fh.fh_handle));
785 fh.fh_size = qword_get(&bp, (char *)fh.fh_handle, NFS3_FHSIZE);