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>
19 #include <netinet/in.h>
20 #include <arpa/inet.h>
34 #include "pseudoflavors.h"
37 #include "blkid/blkid.h"
53 * Support routines for text-based upcalls.
54 * Fields are separated by spaces.
55 * Fields are either mangled to quote space tab newline slosh with slosh
56 * or a hexified with a leading \x
57 * Record is terminated with newline.
60 int cache_export_ent(char *domain, struct exportent *exp, char *p);
65 extern int use_ipaddr;
67 void auth_unix_ip(FILE *f)
71 * Ignore if class != "nfsd"
72 * Otherwise find domainname and write back:
74 * "nfsd" IP-ADDR expiry domainname
80 struct addrinfo *tmp = NULL;
81 struct addrinfo *ai = NULL;
82 if (readline(fileno(f), &lbuf, &lbuflen) != 1)
85 xlog(D_CALL, "auth_unix_ip: inbuf '%s'", lbuf);
89 if (qword_get(&cp, class, 20) <= 0 ||
90 strcmp(class, "nfsd") != 0)
93 if (qword_get(&cp, ipaddr, 20) <= 0)
96 tmp = host_pton(ipaddr);
102 /* addr is a valid, interesting address, find the domain name... */
104 ai = client_resolve(tmp->ai_addr);
105 client = client_compose(ai);
110 qword_print(f, "nfsd");
111 qword_print(f, ipaddr);
112 qword_printint(f, time(0)+30*60);
114 qword_print(f, ipaddr);
116 qword_print(f, *client?client:"DEFAULT");
118 xlog(D_CALL, "auth_unix_ip: client %p '%s'", client, client?client: "DEFAULT");
123 void auth_unix_gid(FILE *f)
128 * uid expiry count list of group ids
132 gid_t glist[100], *groups = glist;
137 if (readline(fileno(f), &lbuf, &lbuflen) != 1)
141 if (qword_get_uint(&cp, &uid) != 0)
148 rv = getgrouplist(pw->pw_name, pw->pw_gid, groups, &ngroups);
149 if (rv == -1 && ngroups >= 100) {
150 groups = malloc(sizeof(gid_t)*ngroups);
154 rv = getgrouplist(pw->pw_name, pw->pw_gid,
158 qword_printuint(f, uid);
159 qword_printuint(f, time(0)+30*60);
161 qword_printuint(f, ngroups);
162 for (i=0; i<ngroups; i++)
163 qword_printuint(f, groups[i]);
165 qword_printuint(f, 0);
173 static const char *get_uuid_blkdev(char *path)
175 /* We set *safe if we know that we need the
176 * fsid from statfs too.
178 static blkid_cache cache = NULL;
181 blkid_tag_iterate iter;
184 const char *val, *uuid = NULL;
187 blkid_get_cache(&cache, NULL);
189 if (stat(path, &stb) != 0)
191 devname = blkid_devno_to_devname(stb.st_dev);
194 dev = blkid_get_dev(cache, devname, BLKID_DEV_NORMAL);
198 iter = blkid_tag_iterate_begin(dev);
201 while (blkid_tag_next(iter, &type, &val) == 0) {
202 if (strcmp(type, "UUID") == 0)
204 if (strcmp(type, "TYPE") == 0 &&
205 strcmp(val, "btrfs") == 0) {
210 blkid_tag_iterate_end(iter);
214 #define get_uuid_blkdev(path) (NULL)
217 int get_uuid(const char *val, int uuidlen, char *u)
219 /* extract hex digits from uuidstr and compose a uuid
220 * of the given length (max 16), xoring bytes to make
225 memset(u, 0, uuidlen);
226 for ( ; *val ; val++) {
247 int uuid_by_path(char *path, int type, int uuidlen, char *uuid)
249 /* get a uuid for the filesystem found at 'path'.
250 * There are several possible ways of generating the
252 * Type 0 is used for new filehandles, while other types
253 * may be used to interpret old filehandle - to ensure smooth
255 * We return 1 if a uuid was found (and it might be worth
256 * trying the next type) or 0 if no more uuid types can be
260 /* Possible sources of uuid are
264 * On some filesystems (e.g. vfat) the statfs64 uuid is simply an
265 * encoding of the device that the filesystem is mounted from, so
266 * it we be very bad to use that (as device numbers change). blkid
268 * On other filesystems (e.g. btrfs) the statfs64 uuid contains
269 * important info that the blkid uuid cannot contain: This happens
270 * when multiple subvolumes are exported (they have the same
271 * blkid uuid but different statfs64 uuids).
272 * We rely on get_uuid_blkdev *knowing* which is which and not returning
273 * a uuid for filesystems where the statfs64 uuid is better.
278 const char *blkid_val;
281 blkid_val = get_uuid_blkdev(path);
283 if (statfs64(path, &st) == 0 &&
284 (st.f_fsid.__val[0] || st.f_fsid.__val[1]))
285 snprintf(fsid_val, 17, "%08x%08x",
286 st.f_fsid.__val[0], st.f_fsid.__val[1]);
290 if (blkid_val && (type--) == 0)
292 else if (fsid_val[0] && (type--) == 0)
297 get_uuid(val, uuidlen, uuid);
301 /* Iterate through /etc/mtab, finding mountpoints
302 * at or below a given path
304 static char *next_mnt(void **v, char *p)
310 f = setmntent("/etc/mtab", "r");
314 while ((me = getmntent(f)) != NULL &&
315 (strncmp(me->mnt_dir, p, l) != 0 ||
316 me->mnt_dir[l] != '/'))
326 void nfsd_fh(FILE *f)
329 * domain fsidtype fsid
330 * interpret fsid, find export point and options, and write:
331 * domain fsidtype fsid expiry path
337 unsigned int dev, major=0, minor=0;
338 unsigned int inode=0;
339 unsigned long long inode64;
340 unsigned int fsidnum=0;
342 struct exportent *found = NULL;
343 struct addrinfo *ai = NULL;
344 char *found_path = NULL;
351 if (readline(fileno(f), &lbuf, &lbuflen) != 1)
354 xlog(D_CALL, "nfsd_fh: inbuf '%s'", lbuf);
358 dom = malloc(strlen(cp));
361 if (qword_get(&cp, dom, strlen(cp)) <= 0)
363 if (qword_get_int(&cp, &fsidtype) != 0)
365 if (fsidtype < 0 || fsidtype > 7)
366 goto out; /* unknown type */
367 if ((fsidlen = qword_get(&cp, fsid, 32)) <= 0)
370 case FSID_DEV: /* 4 bytes: 2 major, 2 minor, 4 inode */
373 memcpy(&dev, fsid, 4);
374 memcpy(&inode, fsid+4, 4);
375 major = ntohl(dev)>>16;
376 minor = ntohl(dev) & 0xFFFF;
379 case FSID_NUM: /* 4 bytes - fsid */
382 memcpy(&fsidnum, fsid, 4);
385 case FSID_MAJOR_MINOR: /* 12 bytes: 4 major, 4 minor, 4 inode
386 * This format is never actually used but was
387 * an historical accident
391 memcpy(&dev, fsid, 4); major = ntohl(dev);
392 memcpy(&dev, fsid+4, 4); minor = ntohl(dev);
393 memcpy(&inode, fsid+8, 4);
396 case FSID_ENCODE_DEV: /* 8 bytes: 4 byte packed device number, 4 inode */
397 /* This is *host* endian, not net-byte-order, because
398 * no-one outside this host has any business interpreting it
402 memcpy(&dev, fsid, 4);
403 memcpy(&inode, fsid+4, 4);
404 major = (dev & 0xfff00) >> 8;
405 minor = (dev & 0xff) | ((dev >> 12) & 0xfff00);
408 case FSID_UUID4_INUM: /* 4 byte inode number and 4 byte uuid */
411 memcpy(&inode, fsid, 4);
415 case FSID_UUID8: /* 8 byte uuid */
421 case FSID_UUID16: /* 16 byte uuid */
427 case FSID_UUID16_INUM: /* 8 byte inode number and 16 byte uuid */
430 memcpy(&inode64, fsid, 8);
439 /* Now determine export point for this fsid/domain */
440 for (i=0 ; i < MCL_MAXTYPES; i++) {
441 nfs_export *next_exp;
442 for (exp = exportlist[i].p_head; exp; exp = next_exp) {
448 if (exp->m_export.e_flags & NFSEXP_CROSSMOUNT) {
449 static nfs_export *prev = NULL;
450 static void *mnt = NULL;
454 path = next_mnt(&mnt, exp->m_export.e_path);
456 next_exp = exp->m_next;
464 path = exp->m_export.e_path;
468 path = exp->m_export.e_path;
469 next_exp = exp->m_next;
472 if (!use_ipaddr && !client_member(dom, exp->m_client->m_hostname))
474 if (exp->m_export.e_mountpoint &&
475 !is_mountpoint(exp->m_export.e_mountpoint[0]?
476 exp->m_export.e_mountpoint:
477 exp->m_export.e_path))
479 if (stat(path, &stb) != 0)
481 if (!S_ISDIR(stb.st_mode) && !S_ISREG(stb.st_mode)) {
486 case FSID_MAJOR_MINOR:
487 case FSID_ENCODE_DEV:
488 if (stb.st_ino != inode)
490 if (major != major(stb.st_dev) ||
491 minor != minor(stb.st_dev))
495 if (((exp->m_export.e_flags & NFSEXP_FSID) == 0 ||
496 exp->m_export.e_fsid != fsidnum))
499 case FSID_UUID4_INUM:
500 case FSID_UUID16_INUM:
501 if (stb.st_ino != inode)
506 if (!is_mountpoint(path))
509 if (exp->m_export.e_uuid)
510 get_uuid(exp->m_export.e_uuid,
514 uuid_by_path(path, type, uuidlen, u);
516 if (memcmp(u, fhuuid, uuidlen) == 0)
519 if (memcmp(u, fhuuid, uuidlen) != 0)
525 struct addrinfo *tmp;
526 tmp = host_pton(dom);
529 ai = client_resolve(tmp->ai_addr);
532 if (!client_check(exp->m_client, ai))
535 /* It's a match !! */
537 found = &exp->m_export;
538 found_path = strdup(path);
539 if (found_path == NULL)
541 } else if (strcmp(found->e_path, exp->m_export.e_path)!= 0)
543 xlog(L_WARNING, "%s and %s have same filehandle for %s, using first",
544 found_path, path, dom);
549 found->e_mountpoint &&
550 !is_mountpoint(found->e_mountpoint[0]?
553 /* Cannot export this yet
554 * should log a warning, but need to rate limit
555 xlog(L_WARNING, "%s not exported as %d not a mountpoint",
556 found->e_path, found->e_mountpoint);
558 /* FIXME we need to make sure we re-visit this later */
561 if (!found && dev_missing) {
562 /* The missing dev could be what we want, so just be
563 * quite rather than returning stale yet
569 if (cache_export_ent(dom, found, found_path) < 0)
573 qword_printint(f, fsidtype);
574 qword_printhex(f, fsid, fsidlen);
575 /* The fsid -> path lookup can be quite expensive as it
576 * potentially stats and reads lots of devices, and some of those
577 * might have spun-down. The Answer is not likely to
578 * change underneath us, and an 'exportfs -f' can always
579 * remove this from the kernel, so use a really log
580 * timeout. Maybe this should be configurable on the command
583 qword_printint(f, 0x7fffffff);
585 qword_print(f, found_path);
592 xlog(D_CALL, "nfsd_fh: found %p path %s", found, found ? found->e_path : NULL);
596 static void write_fsloc(FILE *f, struct exportent *ep)
598 struct servers *servers;
600 if (ep->e_fslocmethod == FSLOC_NONE)
603 servers = replicas_lookup(ep->e_fslocmethod, ep->e_fslocdata);
606 qword_print(f, "fsloc");
607 qword_printint(f, servers->h_num);
608 if (servers->h_num >= 0) {
610 for (i=0; i<servers->h_num; i++) {
611 qword_print(f, servers->h_mp[i]->h_host);
612 qword_print(f, servers->h_mp[i]->h_path);
615 qword_printint(f, servers->h_referral);
616 release_replicas(servers);
619 static void write_secinfo(FILE *f, struct exportent *ep, int flag_mask)
623 for (p = ep->e_secinfo; p->flav; p++)
625 if (p == ep->e_secinfo) {
626 /* There was no sec= option */
629 qword_print(f, "secinfo");
630 qword_printint(f, p - ep->e_secinfo);
631 for (p = ep->e_secinfo; p->flav; p++) {
632 qword_printint(f, p->flav->fnum);
633 qword_printint(f, p->flags & flag_mask);
638 static int dump_to_cache(FILE *f, char *domain, char *path, struct exportent *exp)
640 qword_print(f, domain);
641 qword_print(f, path);
642 qword_printint(f, time(0)+30*60);
644 int different_fs = strcmp(path, exp->e_path) != 0;
645 int flag_mask = different_fs ? ~NFSEXP_FSID : ~0;
647 qword_printint(f, exp->e_flags & flag_mask);
648 qword_printint(f, exp->e_anonuid);
649 qword_printint(f, exp->e_anongid);
650 qword_printint(f, exp->e_fsid);
652 write_secinfo(f, exp, flag_mask);
653 if (exp->e_uuid == NULL || different_fs) {
655 if (uuid_by_path(path, 0, 16, u)) {
656 qword_print(f, "uuid");
657 qword_printhex(f, u, 16);
661 get_uuid(exp->e_uuid, 16, u);
662 qword_print(f, "uuid");
663 qword_printhex(f, u, 16);
669 static int is_subdirectory(char *child, char *parent)
671 int l = strlen(parent);
673 return strcmp(child, parent) == 0
674 || (strncmp(child, parent, l) == 0 && child[l] == '/');
677 static int path_matches(nfs_export *exp, char *path)
679 if (exp->m_export.e_flags & NFSEXP_CROSSMOUNT)
680 return is_subdirectory(path, exp->m_export.e_path);
681 return strcmp(path, exp->m_export.e_path) == 0;
685 client_matches(nfs_export *exp, char *dom, struct addrinfo *ai)
688 return client_check(exp->m_client, ai);
689 return client_member(dom, exp->m_client->m_hostname);
693 export_matches(nfs_export *exp, char *dom, char *path, struct addrinfo *ai)
695 return path_matches(exp, path) && client_matches(exp, dom, ai);
699 lookup_export(char *dom, char *path, struct addrinfo *ai)
702 nfs_export *found = NULL;
706 for (i=0 ; i < MCL_MAXTYPES; i++) {
707 for (exp = exportlist[i].p_head; exp; exp = exp->m_next) {
708 if (!export_matches(exp, dom, path, ai))
716 /* Always prefer non-V4ROOT mounts */
717 if (found->m_export.e_flags & NFSEXP_V4ROOT)
720 /* If one is a CROSSMOUNT, then prefer the longest path */
721 if (((found->m_export.e_flags & NFSEXP_CROSSMOUNT) ||
722 (exp->m_export.e_flags & NFSEXP_CROSSMOUNT)) &&
723 strlen(found->m_export.e_path) !=
724 strlen(exp->m_export.e_path)) {
726 if (strlen(exp->m_export.e_path) >
727 strlen(found->m_export.e_path)) {
733 } else if (found_type == i && found->m_warned == 0) {
734 xlog(L_WARNING, "%s exported to both %s and %s, "
735 "arbitrarily choosing options from first",
736 path, found->m_client->m_hostname, exp->m_client->m_hostname,
745 void nfsd_export(FILE *f)
749 * determine export options and return:
750 * domain path expiry flags anonuid anongid fsid
755 nfs_export *found = NULL;
756 struct addrinfo *ai = NULL;
758 if (readline(fileno(f), &lbuf, &lbuflen) != 1)
761 xlog(D_CALL, "nfsd_export: inbuf '%s'", lbuf);
764 dom = malloc(strlen(cp));
765 path = malloc(strlen(cp));
770 if (qword_get(&cp, dom, strlen(lbuf)) <= 0)
772 if (qword_get(&cp, path, strlen(lbuf)) <= 0)
778 struct addrinfo *tmp;
779 tmp = host_pton(dom);
782 ai = client_resolve(tmp->ai_addr);
787 found = lookup_export(dom, path, ai);
790 if (dump_to_cache(f, dom, path, &found->m_export) < 0) {
792 "Cannot export %s, possibly unsupported filesystem"
793 " or fsid= required", path);
794 dump_to_cache(f, dom, path, NULL);
797 dump_to_cache(f, dom, path, NULL);
800 xlog(D_CALL, "nfsd_export: found %p path %s", found, path ? path : NULL);
802 if (path) free(path);
809 void (*cache_handle)(FILE *f);
812 { "auth.unix.ip", auth_unix_ip, NULL},
813 { "auth.unix.gid", auth_unix_gid, NULL},
814 { "nfsd.export", nfsd_export, NULL},
815 { "nfsd.fh", nfsd_fh, NULL},
819 extern int manage_gids;
820 void cache_open(void)
823 for (i=0; cachelist[i].cache_name; i++ ) {
825 if (!manage_gids && cachelist[i].cache_handle == auth_unix_gid)
827 sprintf(path, "/proc/net/rpc/%s/channel", cachelist[i].cache_name);
828 cachelist[i].f = fopen(path, "r+");
832 void cache_set_fds(fd_set *fdset)
835 for (i=0; cachelist[i].cache_name; i++) {
837 FD_SET(fileno(cachelist[i].f), fdset);
841 int cache_process_req(fd_set *readfds)
845 for (i=0; cachelist[i].cache_name; i++) {
846 if (cachelist[i].f != NULL &&
847 FD_ISSET(fileno(cachelist[i].f), readfds)) {
849 cachelist[i].cache_handle(cachelist[i].f);
850 FD_CLR(fileno(cachelist[i].f), readfds);
858 * Give IP->domain and domain+path->options to kernel
859 * % echo nfsd $IP $[now+30*60] $domain > /proc/net/rpc/auth.unix.ip/channel
860 * % echo $domain $path $[now+30*60] $options $anonuid $anongid $fsid > /proc/net/rpc/nfsd.export/channel
863 int cache_export_ent(char *domain, struct exportent *exp, char *path)
866 FILE *f = fopen("/proc/net/rpc/nfsd.export/channel", "w");
870 err = dump_to_cache(f, domain, exp->e_path, exp);
873 "Cannot export %s, possibly unsupported filesystem or"
874 " fsid= required", exp->e_path);
877 while (err == 0 && (exp->e_flags & NFSEXP_CROSSMOUNT) && path) {
878 /* really an 'if', but we can break out of
879 * a 'while' more easily */
880 /* Look along 'path' for other filesystems
881 * and export them with the same options
884 size_t l = strlen(exp->e_path);
887 if (strlen(path) <= l || path[l] != '/' ||
888 strncmp(exp->e_path, path, l) != 0)
890 if (stat(exp->e_path, &stb) != 0)
893 while(path[l] == '/') {
895 /* errors for submount should fail whole filesystem */
899 while (path[l] != '/' && path[l])
903 err2 = lstat(path, &stb);
907 if (stb.st_dev == dev)
911 dump_to_cache(f, domain, path, exp);
921 int cache_export(nfs_export *exp, char *path)
923 char buf[INET_ADDRSTRLEN];
927 f = fopen("/proc/net/rpc/auth.unix.ip/channel", "w");
932 qword_print(f, "nfsd");
934 host_ntop(get_addrlist(exp->m_client, 0), buf, sizeof(buf)));
935 qword_printint(f, time(0)+30*60);
936 qword_print(f, exp->m_client->m_hostname);
941 err = cache_export_ent(exp->m_client->m_hostname, &exp->m_export, path)
948 * echo $domain $path $length
949 * read filehandle <&0
950 * } <> /proc/fs/nfsd/filehandle
953 cache_get_filehandle(nfs_export *exp, int len, char *p)
955 FILE *f = fopen("/proc/fs/nfsd/filehandle", "r+");
959 static struct nfs_fh_len fh;
962 f = fopen("/proc/fs/nfs/filehandle", "r+");
966 qword_print(f, exp->m_client->m_hostname);
968 qword_printint(f, len);
969 failed = qword_eol(f);
972 failed = (fgets(buf, sizeof(buf), f) == NULL);
976 memset(fh.fh_handle, 0, sizeof(fh.fh_handle));
977 fh.fh_size = qword_get(&bp, (char *)fh.fh_handle, NFS3_FHSIZE);