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"
41 * Invoked by RPC service loop
43 void cache_set_fds(fd_set *fdset);
44 int cache_process_req(fd_set *readfds);
58 * Support routines for text-based upcalls.
59 * Fields are separated by spaces.
60 * Fields are either mangled to quote space tab newline slosh with slosh
61 * or a hexified with a leading \x
62 * Record is terminated with newline.
65 static int cache_export_ent(char *domain, struct exportent *exp, char *p);
67 #define INITIAL_MANAGED_GROUPS 100
71 extern int use_ipaddr;
73 static void auth_unix_ip(FILE *f)
77 * Ignore if class != "nfsd"
78 * Otherwise find domainname and write back:
80 * "nfsd" IP-ADDR expiry domainname
84 char ipaddr[INET6_ADDRSTRLEN];
86 struct addrinfo *tmp = NULL;
87 struct addrinfo *ai = NULL;
88 if (readline(fileno(f), &lbuf, &lbuflen) != 1)
91 xlog(D_CALL, "auth_unix_ip: inbuf '%s'", lbuf);
95 if (qword_get(&cp, class, 20) <= 0 ||
96 strcmp(class, "nfsd") != 0)
99 if (qword_get(&cp, ipaddr, sizeof(ipaddr)) <= 0)
102 tmp = host_pton(ipaddr);
108 /* addr is a valid, interesting address, find the domain name... */
110 ai = client_resolve(tmp->ai_addr);
111 client = client_compose(ai);
116 qword_print(f, "nfsd");
117 qword_print(f, ipaddr);
118 qword_printuint(f, time(0) + DEFAULT_TTL);
120 qword_print(f, ipaddr);
122 qword_print(f, *client?client:"DEFAULT");
124 xlog(D_CALL, "auth_unix_ip: client %p '%s'", client, client?client: "DEFAULT");
129 static void auth_unix_gid(FILE *f)
134 * uid expiry count list of group ids
138 static gid_t *groups = NULL;
139 static int groups_len = 0;
145 if (groups_len == 0) {
146 groups = malloc(sizeof(gid_t) * INITIAL_MANAGED_GROUPS);
150 groups_len = ngroups = INITIAL_MANAGED_GROUPS;
153 if (readline(fileno(f), &lbuf, &lbuflen) != 1)
157 if (qword_get_uint(&cp, &uid) != 0)
164 rv = getgrouplist(pw->pw_name, pw->pw_gid, groups, &ngroups);
165 if (rv == -1 && ngroups >= groups_len) {
166 more_groups = realloc(groups, sizeof(gid_t)*ngroups);
170 groups = more_groups;
171 groups_len = ngroups;
172 rv = getgrouplist(pw->pw_name, pw->pw_gid,
177 qword_printuint(f, uid);
178 qword_printuint(f, time(0) + DEFAULT_TTL);
180 qword_printuint(f, ngroups);
181 for (i=0; i<ngroups; i++)
182 qword_printuint(f, groups[i]);
184 qword_printuint(f, 0);
189 static const char *get_uuid_blkdev(char *path)
191 /* We set *safe if we know that we need the
192 * fsid from statfs too.
194 static blkid_cache cache = NULL;
197 blkid_tag_iterate iter;
200 const char *val, *uuid = NULL;
203 blkid_get_cache(&cache, NULL);
205 if (stat(path, &stb) != 0)
207 devname = blkid_devno_to_devname(stb.st_dev);
210 dev = blkid_get_dev(cache, devname, BLKID_DEV_NORMAL);
214 iter = blkid_tag_iterate_begin(dev);
217 while (blkid_tag_next(iter, &type, &val) == 0) {
218 if (strcmp(type, "UUID") == 0)
220 if (strcmp(type, "TYPE") == 0 &&
221 strcmp(val, "btrfs") == 0) {
226 blkid_tag_iterate_end(iter);
230 #define get_uuid_blkdev(path) (NULL)
233 static int get_uuid(const char *val, int uuidlen, char *u)
235 /* extract hex digits from uuidstr and compose a uuid
236 * of the given length (max 16), xoring bytes to make
241 memset(u, 0, uuidlen);
242 for ( ; *val ; val++) {
263 static int uuid_by_path(char *path, int type, int uuidlen, char *uuid)
265 /* get a uuid for the filesystem found at 'path'.
266 * There are several possible ways of generating the
268 * Type 0 is used for new filehandles, while other types
269 * may be used to interpret old filehandle - to ensure smooth
271 * We return 1 if a uuid was found (and it might be worth
272 * trying the next type) or 0 if no more uuid types can be
276 /* Possible sources of uuid are
280 * On some filesystems (e.g. vfat) the statfs64 uuid is simply an
281 * encoding of the device that the filesystem is mounted from, so
282 * it we be very bad to use that (as device numbers change). blkid
284 * On other filesystems (e.g. btrfs) the statfs64 uuid contains
285 * important info that the blkid uuid cannot contain: This happens
286 * when multiple subvolumes are exported (they have the same
287 * blkid uuid but different statfs64 uuids).
288 * We rely on get_uuid_blkdev *knowing* which is which and not returning
289 * a uuid for filesystems where the statfs64 uuid is better.
294 const char *blkid_val;
297 blkid_val = get_uuid_blkdev(path);
299 if (statfs64(path, &st) == 0 &&
300 (st.f_fsid.__val[0] || st.f_fsid.__val[1]))
301 snprintf(fsid_val, 17, "%08x%08x",
302 st.f_fsid.__val[0], st.f_fsid.__val[1]);
306 if (blkid_val && (type--) == 0)
308 else if (fsid_val[0] && (type--) == 0)
313 get_uuid(val, uuidlen, uuid);
317 /* Iterate through /etc/mtab, finding mountpoints
318 * at or below a given path
320 static char *next_mnt(void **v, char *p)
326 f = setmntent("/etc/mtab", "r");
330 while ((me = getmntent(f)) != NULL &&
331 (strncmp(me->mnt_dir, p, l) != 0 ||
332 me->mnt_dir[l] != '/'))
342 static void nfsd_fh(FILE *f)
345 * domain fsidtype fsid
346 * interpret fsid, find export point and options, and write:
347 * domain fsidtype fsid expiry path
353 unsigned int dev, major=0, minor=0;
354 unsigned int inode=0;
355 unsigned long long inode64;
356 unsigned int fsidnum=0;
358 struct exportent *found = NULL;
359 struct addrinfo *ai = NULL;
360 char *found_path = NULL;
367 if (readline(fileno(f), &lbuf, &lbuflen) != 1)
370 xlog(D_CALL, "nfsd_fh: inbuf '%s'", lbuf);
374 dom = malloc(strlen(cp));
377 if (qword_get(&cp, dom, strlen(cp)) <= 0)
379 if (qword_get_int(&cp, &fsidtype) != 0)
381 if (fsidtype < 0 || fsidtype > 7)
382 goto out; /* unknown type */
383 if ((fsidlen = qword_get(&cp, fsid, 32)) <= 0)
386 case FSID_DEV: /* 4 bytes: 2 major, 2 minor, 4 inode */
389 memcpy(&dev, fsid, 4);
390 memcpy(&inode, fsid+4, 4);
391 major = ntohl(dev)>>16;
392 minor = ntohl(dev) & 0xFFFF;
395 case FSID_NUM: /* 4 bytes - fsid */
398 memcpy(&fsidnum, fsid, 4);
401 case FSID_MAJOR_MINOR: /* 12 bytes: 4 major, 4 minor, 4 inode
402 * This format is never actually used but was
403 * an historical accident
407 memcpy(&dev, fsid, 4); major = ntohl(dev);
408 memcpy(&dev, fsid+4, 4); minor = ntohl(dev);
409 memcpy(&inode, fsid+8, 4);
412 case FSID_ENCODE_DEV: /* 8 bytes: 4 byte packed device number, 4 inode */
413 /* This is *host* endian, not net-byte-order, because
414 * no-one outside this host has any business interpreting it
418 memcpy(&dev, fsid, 4);
419 memcpy(&inode, fsid+4, 4);
420 major = (dev & 0xfff00) >> 8;
421 minor = (dev & 0xff) | ((dev >> 12) & 0xfff00);
424 case FSID_UUID4_INUM: /* 4 byte inode number and 4 byte uuid */
427 memcpy(&inode, fsid, 4);
431 case FSID_UUID8: /* 8 byte uuid */
437 case FSID_UUID16: /* 16 byte uuid */
443 case FSID_UUID16_INUM: /* 8 byte inode number and 16 byte uuid */
446 memcpy(&inode64, fsid, 8);
455 /* Now determine export point for this fsid/domain */
456 for (i=0 ; i < MCL_MAXTYPES; i++) {
457 nfs_export *next_exp;
458 for (exp = exportlist[i].p_head; exp; exp = next_exp) {
464 if (exp->m_export.e_flags & NFSEXP_CROSSMOUNT) {
465 static nfs_export *prev = NULL;
466 static void *mnt = NULL;
470 path = next_mnt(&mnt, exp->m_export.e_path);
472 next_exp = exp->m_next;
480 path = exp->m_export.e_path;
484 path = exp->m_export.e_path;
485 next_exp = exp->m_next;
488 if (!use_ipaddr && !client_member(dom, exp->m_client->m_hostname))
490 if (exp->m_export.e_mountpoint &&
491 !is_mountpoint(exp->m_export.e_mountpoint[0]?
492 exp->m_export.e_mountpoint:
493 exp->m_export.e_path))
495 if (stat(path, &stb) != 0)
497 if (!S_ISDIR(stb.st_mode) && !S_ISREG(stb.st_mode)) {
502 case FSID_MAJOR_MINOR:
503 case FSID_ENCODE_DEV:
504 if (stb.st_ino != inode)
506 if (major != major(stb.st_dev) ||
507 minor != minor(stb.st_dev))
511 if (((exp->m_export.e_flags & NFSEXP_FSID) == 0 ||
512 exp->m_export.e_fsid != fsidnum))
515 case FSID_UUID4_INUM:
516 case FSID_UUID16_INUM:
517 if (stb.st_ino != inode)
522 if (!is_mountpoint(path))
525 if (exp->m_export.e_uuid)
526 get_uuid(exp->m_export.e_uuid,
530 uuid_by_path(path, type, uuidlen, u);
532 if (memcmp(u, fhuuid, uuidlen) == 0)
535 if (memcmp(u, fhuuid, uuidlen) != 0)
541 struct addrinfo *tmp;
542 tmp = host_pton(dom);
545 ai = client_resolve(tmp->ai_addr);
548 if (!client_check(exp->m_client, ai))
551 /* It's a match !! */
553 found = &exp->m_export;
554 found_path = strdup(path);
555 if (found_path == NULL)
557 } else if (strcmp(found->e_path, exp->m_export.e_path)!= 0)
559 xlog(L_WARNING, "%s and %s have same filehandle for %s, using first",
560 found_path, path, dom);
565 found->e_mountpoint &&
566 !is_mountpoint(found->e_mountpoint[0]?
569 /* Cannot export this yet
570 * should log a warning, but need to rate limit
571 xlog(L_WARNING, "%s not exported as %d not a mountpoint",
572 found->e_path, found->e_mountpoint);
574 /* FIXME we need to make sure we re-visit this later */
577 if (!found && dev_missing) {
578 /* The missing dev could be what we want, so just be
579 * quite rather than returning stale yet
585 if (cache_export_ent(dom, found, found_path) < 0)
589 qword_printint(f, fsidtype);
590 qword_printhex(f, fsid, fsidlen);
591 /* The fsid -> path lookup can be quite expensive as it
592 * potentially stats and reads lots of devices, and some of those
593 * might have spun-down. The Answer is not likely to
594 * change underneath us, and an 'exportfs -f' can always
595 * remove this from the kernel, so use a really log
596 * timeout. Maybe this should be configurable on the command
599 qword_printint(f, 0x7fffffff);
601 qword_print(f, found_path);
608 xlog(D_CALL, "nfsd_fh: found %p path %s", found, found ? found->e_path : NULL);
612 static void write_fsloc(FILE *f, struct exportent *ep)
614 struct servers *servers;
616 if (ep->e_fslocmethod == FSLOC_NONE)
619 servers = replicas_lookup(ep->e_fslocmethod, ep->e_fslocdata);
622 qword_print(f, "fsloc");
623 qword_printint(f, servers->h_num);
624 if (servers->h_num >= 0) {
626 for (i=0; i<servers->h_num; i++) {
627 qword_print(f, servers->h_mp[i]->h_host);
628 qword_print(f, servers->h_mp[i]->h_path);
631 qword_printint(f, servers->h_referral);
632 release_replicas(servers);
635 static void write_secinfo(FILE *f, struct exportent *ep, int flag_mask)
639 for (p = ep->e_secinfo; p->flav; p++)
641 if (p == ep->e_secinfo) {
642 /* There was no sec= option */
645 qword_print(f, "secinfo");
646 qword_printint(f, p - ep->e_secinfo);
647 for (p = ep->e_secinfo; p->flav; p++) {
648 qword_printint(f, p->flav->fnum);
649 qword_printint(f, p->flags & flag_mask);
654 static int dump_to_cache(FILE *f, char *domain, char *path, struct exportent *exp)
656 qword_print(f, domain);
657 qword_print(f, path);
659 int different_fs = strcmp(path, exp->e_path) != 0;
660 int flag_mask = different_fs ? ~NFSEXP_FSID : ~0;
662 qword_printuint(f, time(0) + exp->e_ttl);
663 qword_printint(f, exp->e_flags & flag_mask);
664 qword_printint(f, exp->e_anonuid);
665 qword_printint(f, exp->e_anongid);
666 qword_printint(f, exp->e_fsid);
668 write_secinfo(f, exp, flag_mask);
669 if (exp->e_uuid == NULL || different_fs) {
671 if (uuid_by_path(path, 0, 16, u)) {
672 qword_print(f, "uuid");
673 qword_printhex(f, u, 16);
677 get_uuid(exp->e_uuid, 16, u);
678 qword_print(f, "uuid");
679 qword_printhex(f, u, 16);
682 qword_printuint(f, time(0) + DEFAULT_TTL);
686 static int is_subdirectory(char *child, char *parent)
688 int l = strlen(parent);
690 return strcmp(child, parent) == 0
691 || (strncmp(child, parent, l) == 0 && child[l] == '/');
694 static int path_matches(nfs_export *exp, char *path)
696 if (exp->m_export.e_flags & NFSEXP_CROSSMOUNT)
697 return is_subdirectory(path, exp->m_export.e_path);
698 return strcmp(path, exp->m_export.e_path) == 0;
702 client_matches(nfs_export *exp, char *dom, struct addrinfo *ai)
705 return client_check(exp->m_client, ai);
706 return client_member(dom, exp->m_client->m_hostname);
710 export_matches(nfs_export *exp, char *dom, char *path, struct addrinfo *ai)
712 return path_matches(exp, path) && client_matches(exp, dom, ai);
716 lookup_export(char *dom, char *path, struct addrinfo *ai)
719 nfs_export *found = NULL;
723 for (i=0 ; i < MCL_MAXTYPES; i++) {
724 for (exp = exportlist[i].p_head; exp; exp = exp->m_next) {
725 if (!export_matches(exp, dom, path, ai))
733 /* Always prefer non-V4ROOT mounts */
734 if (found->m_export.e_flags & NFSEXP_V4ROOT)
737 /* If one is a CROSSMOUNT, then prefer the longest path */
738 if (((found->m_export.e_flags & NFSEXP_CROSSMOUNT) ||
739 (exp->m_export.e_flags & NFSEXP_CROSSMOUNT)) &&
740 strlen(found->m_export.e_path) !=
741 strlen(exp->m_export.e_path)) {
743 if (strlen(exp->m_export.e_path) >
744 strlen(found->m_export.e_path)) {
750 } else if (found_type == i && found->m_warned == 0) {
751 xlog(L_WARNING, "%s exported to both %s and %s, "
752 "arbitrarily choosing options from first",
753 path, found->m_client->m_hostname, exp->m_client->m_hostname,
762 static void nfsd_export(FILE *f)
766 * determine export options and return:
767 * domain path expiry flags anonuid anongid fsid
772 nfs_export *found = NULL;
773 struct addrinfo *ai = NULL;
775 if (readline(fileno(f), &lbuf, &lbuflen) != 1)
778 xlog(D_CALL, "nfsd_export: inbuf '%s'", lbuf);
781 dom = malloc(strlen(cp));
782 path = malloc(strlen(cp));
787 if (qword_get(&cp, dom, strlen(lbuf)) <= 0)
789 if (qword_get(&cp, path, strlen(lbuf)) <= 0)
795 struct addrinfo *tmp;
796 tmp = host_pton(dom);
799 ai = client_resolve(tmp->ai_addr);
804 found = lookup_export(dom, path, ai);
807 if (dump_to_cache(f, dom, path, &found->m_export) < 0) {
809 "Cannot export %s, possibly unsupported filesystem"
810 " or fsid= required", path);
811 dump_to_cache(f, dom, path, NULL);
814 dump_to_cache(f, dom, path, NULL);
817 xlog(D_CALL, "nfsd_export: found %p path %s", found, path ? path : NULL);
819 if (path) free(path);
826 void (*cache_handle)(FILE *f);
829 { "auth.unix.ip", auth_unix_ip, NULL},
830 { "auth.unix.gid", auth_unix_gid, NULL},
831 { "nfsd.export", nfsd_export, NULL},
832 { "nfsd.fh", nfsd_fh, NULL},
836 extern int manage_gids;
839 * cache_open - prepare communications channels with kernel RPC caches
842 void cache_open(void)
845 for (i=0; cachelist[i].cache_name; i++ ) {
847 if (!manage_gids && cachelist[i].cache_handle == auth_unix_gid)
849 sprintf(path, "/proc/net/rpc/%s/channel", cachelist[i].cache_name);
850 cachelist[i].f = fopen(path, "r+");
855 * cache_set_fds - prepare cache file descriptors for one iteration of the service loop
856 * @fdset: pointer to fd_set to prepare
858 void cache_set_fds(fd_set *fdset)
861 for (i=0; cachelist[i].cache_name; i++) {
863 FD_SET(fileno(cachelist[i].f), fdset);
868 * cache_process_req - process any active cache file descriptors during service loop iteration
869 * @fdset: pointer to fd_set to examine for activity
871 int cache_process_req(fd_set *readfds)
875 for (i=0; cachelist[i].cache_name; i++) {
876 if (cachelist[i].f != NULL &&
877 FD_ISSET(fileno(cachelist[i].f), readfds)) {
879 cachelist[i].cache_handle(cachelist[i].f);
880 FD_CLR(fileno(cachelist[i].f), readfds);
888 * Give IP->domain and domain+path->options to kernel
889 * % echo nfsd $IP $[now+DEFAULT_TTL] $domain > /proc/net/rpc/auth.unix.ip/channel
890 * % echo $domain $path $[now+DEFAULT_TTL] $options $anonuid $anongid $fsid > /proc/net/rpc/nfsd.export/channel
893 static int cache_export_ent(char *domain, struct exportent *exp, char *path)
896 FILE *f = fopen("/proc/net/rpc/nfsd.export/channel", "w");
900 err = dump_to_cache(f, domain, exp->e_path, exp);
903 "Cannot export %s, possibly unsupported filesystem or"
904 " fsid= required", exp->e_path);
907 while (err == 0 && (exp->e_flags & NFSEXP_CROSSMOUNT) && path) {
908 /* really an 'if', but we can break out of
909 * a 'while' more easily */
910 /* Look along 'path' for other filesystems
911 * and export them with the same options
914 size_t l = strlen(exp->e_path);
917 if (strlen(path) <= l || path[l] != '/' ||
918 strncmp(exp->e_path, path, l) != 0)
920 if (stat(exp->e_path, &stb) != 0)
923 while(path[l] == '/') {
925 /* errors for submount should fail whole filesystem */
929 while (path[l] != '/' && path[l])
933 err2 = lstat(path, &stb);
937 if (stb.st_dev == dev)
941 dump_to_cache(f, domain, path, exp);
952 * cache_export - Inform kernel of a new nfs_export
953 * @exp: target nfs_export
954 * @path: NUL-terminated C string containing export path
956 int cache_export(nfs_export *exp, char *path)
958 char buf[INET6_ADDRSTRLEN];
962 f = fopen("/proc/net/rpc/auth.unix.ip/channel", "w");
967 qword_print(f, "nfsd");
969 host_ntop(get_addrlist(exp->m_client, 0), buf, sizeof(buf)));
970 qword_printuint(f, time(0) + exp->m_export.e_ttl);
971 qword_print(f, exp->m_client->m_hostname);
976 err = cache_export_ent(exp->m_client->m_hostname, &exp->m_export, path)
982 * cache_get_filehandle - given an nfs_export, get its root filehandle
983 * @exp: target nfs_export
984 * @len: length of requested file handle
985 * @p: NUL-terminated C string containing export path
987 * Returns pointer to NFS file handle of root directory of export
990 * echo $domain $path $length
991 * read filehandle <&0
992 * } <> /proc/fs/nfsd/filehandle
995 cache_get_filehandle(nfs_export *exp, int len, char *p)
997 FILE *f = fopen("/proc/fs/nfsd/filehandle", "r+");
1001 static struct nfs_fh_len fh;
1004 f = fopen("/proc/fs/nfs/filehandle", "r+");
1008 qword_print(f, exp->m_client->m_hostname);
1010 qword_printint(f, len);
1011 failed = qword_eol(f);
1014 failed = (fgets(buf, sizeof(buf), f) == NULL);
1018 memset(fh.fh_handle, 0, sizeof(fh.fh_handle));
1019 fh.fh_size = qword_get(&bp, (char *)fh.fh_handle, NFS3_FHSIZE);