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);
70 extern int use_ipaddr;
72 static void auth_unix_ip(FILE *f)
76 * Ignore if class != "nfsd"
77 * Otherwise find domainname and write back:
79 * "nfsd" IP-ADDR expiry domainname
83 char ipaddr[INET6_ADDRSTRLEN];
85 struct addrinfo *tmp = NULL;
86 struct addrinfo *ai = NULL;
87 if (readline(fileno(f), &lbuf, &lbuflen) != 1)
90 xlog(D_CALL, "auth_unix_ip: inbuf '%s'", lbuf);
94 if (qword_get(&cp, class, 20) <= 0 ||
95 strcmp(class, "nfsd") != 0)
98 if (qword_get(&cp, ipaddr, sizeof(ipaddr)) <= 0)
101 tmp = host_pton(ipaddr);
107 /* addr is a valid, interesting address, find the domain name... */
109 ai = client_resolve(tmp->ai_addr);
110 client = client_compose(ai);
115 qword_print(f, "nfsd");
116 qword_print(f, ipaddr);
117 qword_printuint(f, time(0) + DEFAULT_TTL);
119 qword_print(f, ipaddr);
121 qword_print(f, *client?client:"DEFAULT");
123 xlog(D_CALL, "auth_unix_ip: client %p '%s'", client, client?client: "DEFAULT");
128 static void auth_unix_gid(FILE *f)
133 * uid expiry count list of group ids
137 gid_t glist[100], *groups = glist;
142 if (readline(fileno(f), &lbuf, &lbuflen) != 1)
146 if (qword_get_uint(&cp, &uid) != 0)
153 rv = getgrouplist(pw->pw_name, pw->pw_gid, groups, &ngroups);
154 if (rv == -1 && ngroups >= 100) {
155 groups = malloc(sizeof(gid_t)*ngroups);
159 rv = getgrouplist(pw->pw_name, pw->pw_gid,
163 qword_printuint(f, uid);
164 qword_printuint(f, time(0) + DEFAULT_TTL);
166 qword_printuint(f, ngroups);
167 for (i=0; i<ngroups; i++)
168 qword_printuint(f, groups[i]);
170 qword_printuint(f, 0);
178 static const char *get_uuid_blkdev(char *path)
180 /* We set *safe if we know that we need the
181 * fsid from statfs too.
183 static blkid_cache cache = NULL;
186 blkid_tag_iterate iter;
189 const char *val, *uuid = NULL;
192 blkid_get_cache(&cache, NULL);
194 if (stat(path, &stb) != 0)
196 devname = blkid_devno_to_devname(stb.st_dev);
199 dev = blkid_get_dev(cache, devname, BLKID_DEV_NORMAL);
203 iter = blkid_tag_iterate_begin(dev);
206 while (blkid_tag_next(iter, &type, &val) == 0) {
207 if (strcmp(type, "UUID") == 0)
209 if (strcmp(type, "TYPE") == 0 &&
210 strcmp(val, "btrfs") == 0) {
215 blkid_tag_iterate_end(iter);
219 #define get_uuid_blkdev(path) (NULL)
222 static int get_uuid(const char *val, int uuidlen, char *u)
224 /* extract hex digits from uuidstr and compose a uuid
225 * of the given length (max 16), xoring bytes to make
230 memset(u, 0, uuidlen);
231 for ( ; *val ; val++) {
252 static int uuid_by_path(char *path, int type, int uuidlen, char *uuid)
254 /* get a uuid for the filesystem found at 'path'.
255 * There are several possible ways of generating the
257 * Type 0 is used for new filehandles, while other types
258 * may be used to interpret old filehandle - to ensure smooth
260 * We return 1 if a uuid was found (and it might be worth
261 * trying the next type) or 0 if no more uuid types can be
265 /* Possible sources of uuid are
269 * On some filesystems (e.g. vfat) the statfs64 uuid is simply an
270 * encoding of the device that the filesystem is mounted from, so
271 * it we be very bad to use that (as device numbers change). blkid
273 * On other filesystems (e.g. btrfs) the statfs64 uuid contains
274 * important info that the blkid uuid cannot contain: This happens
275 * when multiple subvolumes are exported (they have the same
276 * blkid uuid but different statfs64 uuids).
277 * We rely on get_uuid_blkdev *knowing* which is which and not returning
278 * a uuid for filesystems where the statfs64 uuid is better.
283 const char *blkid_val;
286 blkid_val = get_uuid_blkdev(path);
288 if (statfs64(path, &st) == 0 &&
289 (st.f_fsid.__val[0] || st.f_fsid.__val[1]))
290 snprintf(fsid_val, 17, "%08x%08x",
291 st.f_fsid.__val[0], st.f_fsid.__val[1]);
295 if (blkid_val && (type--) == 0)
297 else if (fsid_val[0] && (type--) == 0)
302 get_uuid(val, uuidlen, uuid);
306 /* Iterate through /etc/mtab, finding mountpoints
307 * at or below a given path
309 static char *next_mnt(void **v, char *p)
315 f = setmntent("/etc/mtab", "r");
319 while ((me = getmntent(f)) != NULL &&
320 (strncmp(me->mnt_dir, p, l) != 0 ||
321 me->mnt_dir[l] != '/'))
331 static void nfsd_fh(FILE *f)
334 * domain fsidtype fsid
335 * interpret fsid, find export point and options, and write:
336 * domain fsidtype fsid expiry path
342 unsigned int dev, major=0, minor=0;
343 unsigned int inode=0;
344 unsigned long long inode64;
345 unsigned int fsidnum=0;
347 struct exportent *found = NULL;
348 struct addrinfo *ai = NULL;
349 char *found_path = NULL;
356 if (readline(fileno(f), &lbuf, &lbuflen) != 1)
359 xlog(D_CALL, "nfsd_fh: inbuf '%s'", lbuf);
363 dom = malloc(strlen(cp));
366 if (qword_get(&cp, dom, strlen(cp)) <= 0)
368 if (qword_get_int(&cp, &fsidtype) != 0)
370 if (fsidtype < 0 || fsidtype > 7)
371 goto out; /* unknown type */
372 if ((fsidlen = qword_get(&cp, fsid, 32)) <= 0)
375 case FSID_DEV: /* 4 bytes: 2 major, 2 minor, 4 inode */
378 memcpy(&dev, fsid, 4);
379 memcpy(&inode, fsid+4, 4);
380 major = ntohl(dev)>>16;
381 minor = ntohl(dev) & 0xFFFF;
384 case FSID_NUM: /* 4 bytes - fsid */
387 memcpy(&fsidnum, fsid, 4);
390 case FSID_MAJOR_MINOR: /* 12 bytes: 4 major, 4 minor, 4 inode
391 * This format is never actually used but was
392 * an historical accident
396 memcpy(&dev, fsid, 4); major = ntohl(dev);
397 memcpy(&dev, fsid+4, 4); minor = ntohl(dev);
398 memcpy(&inode, fsid+8, 4);
401 case FSID_ENCODE_DEV: /* 8 bytes: 4 byte packed device number, 4 inode */
402 /* This is *host* endian, not net-byte-order, because
403 * no-one outside this host has any business interpreting it
407 memcpy(&dev, fsid, 4);
408 memcpy(&inode, fsid+4, 4);
409 major = (dev & 0xfff00) >> 8;
410 minor = (dev & 0xff) | ((dev >> 12) & 0xfff00);
413 case FSID_UUID4_INUM: /* 4 byte inode number and 4 byte uuid */
416 memcpy(&inode, fsid, 4);
420 case FSID_UUID8: /* 8 byte uuid */
426 case FSID_UUID16: /* 16 byte uuid */
432 case FSID_UUID16_INUM: /* 8 byte inode number and 16 byte uuid */
435 memcpy(&inode64, fsid, 8);
444 /* Now determine export point for this fsid/domain */
445 for (i=0 ; i < MCL_MAXTYPES; i++) {
446 nfs_export *next_exp;
447 for (exp = exportlist[i].p_head; exp; exp = next_exp) {
453 if (exp->m_export.e_flags & NFSEXP_CROSSMOUNT) {
454 static nfs_export *prev = NULL;
455 static void *mnt = NULL;
459 path = next_mnt(&mnt, exp->m_export.e_path);
461 next_exp = exp->m_next;
469 path = exp->m_export.e_path;
473 path = exp->m_export.e_path;
474 next_exp = exp->m_next;
477 if (!use_ipaddr && !client_member(dom, exp->m_client->m_hostname))
479 if (exp->m_export.e_mountpoint &&
480 !is_mountpoint(exp->m_export.e_mountpoint[0]?
481 exp->m_export.e_mountpoint:
482 exp->m_export.e_path))
484 if (stat(path, &stb) != 0)
486 if (!S_ISDIR(stb.st_mode) && !S_ISREG(stb.st_mode)) {
491 case FSID_MAJOR_MINOR:
492 case FSID_ENCODE_DEV:
493 if (stb.st_ino != inode)
495 if (major != major(stb.st_dev) ||
496 minor != minor(stb.st_dev))
500 if (((exp->m_export.e_flags & NFSEXP_FSID) == 0 ||
501 exp->m_export.e_fsid != fsidnum))
504 case FSID_UUID4_INUM:
505 case FSID_UUID16_INUM:
506 if (stb.st_ino != inode)
511 if (!is_mountpoint(path))
514 if (exp->m_export.e_uuid)
515 get_uuid(exp->m_export.e_uuid,
519 uuid_by_path(path, type, uuidlen, u);
521 if (memcmp(u, fhuuid, uuidlen) == 0)
524 if (memcmp(u, fhuuid, uuidlen) != 0)
530 struct addrinfo *tmp;
531 tmp = host_pton(dom);
534 ai = client_resolve(tmp->ai_addr);
537 if (!client_check(exp->m_client, ai))
540 /* It's a match !! */
542 found = &exp->m_export;
543 found_path = strdup(path);
544 if (found_path == NULL)
546 } else if (strcmp(found->e_path, exp->m_export.e_path)!= 0)
548 xlog(L_WARNING, "%s and %s have same filehandle for %s, using first",
549 found_path, path, dom);
554 found->e_mountpoint &&
555 !is_mountpoint(found->e_mountpoint[0]?
558 /* Cannot export this yet
559 * should log a warning, but need to rate limit
560 xlog(L_WARNING, "%s not exported as %d not a mountpoint",
561 found->e_path, found->e_mountpoint);
563 /* FIXME we need to make sure we re-visit this later */
566 if (!found && dev_missing) {
567 /* The missing dev could be what we want, so just be
568 * quite rather than returning stale yet
574 if (cache_export_ent(dom, found, found_path) < 0)
578 qword_printint(f, fsidtype);
579 qword_printhex(f, fsid, fsidlen);
580 /* The fsid -> path lookup can be quite expensive as it
581 * potentially stats and reads lots of devices, and some of those
582 * might have spun-down. The Answer is not likely to
583 * change underneath us, and an 'exportfs -f' can always
584 * remove this from the kernel, so use a really log
585 * timeout. Maybe this should be configurable on the command
588 qword_printint(f, 0x7fffffff);
590 qword_print(f, found_path);
597 xlog(D_CALL, "nfsd_fh: found %p path %s", found, found ? found->e_path : NULL);
601 static void write_fsloc(FILE *f, struct exportent *ep)
603 struct servers *servers;
605 if (ep->e_fslocmethod == FSLOC_NONE)
608 servers = replicas_lookup(ep->e_fslocmethod, ep->e_fslocdata);
611 qword_print(f, "fsloc");
612 qword_printint(f, servers->h_num);
613 if (servers->h_num >= 0) {
615 for (i=0; i<servers->h_num; i++) {
616 qword_print(f, servers->h_mp[i]->h_host);
617 qword_print(f, servers->h_mp[i]->h_path);
620 qword_printint(f, servers->h_referral);
621 release_replicas(servers);
624 static void write_secinfo(FILE *f, struct exportent *ep, int flag_mask)
628 for (p = ep->e_secinfo; p->flav; p++)
630 if (p == ep->e_secinfo) {
631 /* There was no sec= option */
634 qword_print(f, "secinfo");
635 qword_printint(f, p - ep->e_secinfo);
636 for (p = ep->e_secinfo; p->flav; p++) {
637 qword_printint(f, p->flav->fnum);
638 qword_printint(f, p->flags & flag_mask);
643 static int dump_to_cache(FILE *f, char *domain, char *path, struct exportent *exp)
645 qword_print(f, domain);
646 qword_print(f, path);
648 int different_fs = strcmp(path, exp->e_path) != 0;
649 int flag_mask = different_fs ? ~NFSEXP_FSID : ~0;
651 qword_printuint(f, time(0) + exp->e_ttl);
652 qword_printint(f, exp->e_flags & flag_mask);
653 qword_printint(f, exp->e_anonuid);
654 qword_printint(f, exp->e_anongid);
655 qword_printint(f, exp->e_fsid);
657 write_secinfo(f, exp, flag_mask);
658 if (exp->e_uuid == NULL || different_fs) {
660 if (uuid_by_path(path, 0, 16, u)) {
661 qword_print(f, "uuid");
662 qword_printhex(f, u, 16);
666 get_uuid(exp->e_uuid, 16, u);
667 qword_print(f, "uuid");
668 qword_printhex(f, u, 16);
671 qword_printuint(f, time(0) + DEFAULT_TTL);
675 static int is_subdirectory(char *child, char *parent)
677 int l = strlen(parent);
679 return strcmp(child, parent) == 0
680 || (strncmp(child, parent, l) == 0 && child[l] == '/');
683 static int path_matches(nfs_export *exp, char *path)
685 if (exp->m_export.e_flags & NFSEXP_CROSSMOUNT)
686 return is_subdirectory(path, exp->m_export.e_path);
687 return strcmp(path, exp->m_export.e_path) == 0;
691 client_matches(nfs_export *exp, char *dom, struct addrinfo *ai)
694 return client_check(exp->m_client, ai);
695 return client_member(dom, exp->m_client->m_hostname);
699 export_matches(nfs_export *exp, char *dom, char *path, struct addrinfo *ai)
701 return path_matches(exp, path) && client_matches(exp, dom, ai);
705 lookup_export(char *dom, char *path, struct addrinfo *ai)
708 nfs_export *found = NULL;
712 for (i=0 ; i < MCL_MAXTYPES; i++) {
713 for (exp = exportlist[i].p_head; exp; exp = exp->m_next) {
714 if (!export_matches(exp, dom, path, ai))
722 /* Always prefer non-V4ROOT mounts */
723 if (found->m_export.e_flags & NFSEXP_V4ROOT)
726 /* If one is a CROSSMOUNT, then prefer the longest path */
727 if (((found->m_export.e_flags & NFSEXP_CROSSMOUNT) ||
728 (exp->m_export.e_flags & NFSEXP_CROSSMOUNT)) &&
729 strlen(found->m_export.e_path) !=
730 strlen(exp->m_export.e_path)) {
732 if (strlen(exp->m_export.e_path) >
733 strlen(found->m_export.e_path)) {
739 } else if (found_type == i && found->m_warned == 0) {
740 xlog(L_WARNING, "%s exported to both %s and %s, "
741 "arbitrarily choosing options from first",
742 path, found->m_client->m_hostname, exp->m_client->m_hostname,
751 static void nfsd_export(FILE *f)
755 * determine export options and return:
756 * domain path expiry flags anonuid anongid fsid
761 nfs_export *found = NULL;
762 struct addrinfo *ai = NULL;
764 if (readline(fileno(f), &lbuf, &lbuflen) != 1)
767 xlog(D_CALL, "nfsd_export: inbuf '%s'", lbuf);
770 dom = malloc(strlen(cp));
771 path = malloc(strlen(cp));
776 if (qword_get(&cp, dom, strlen(lbuf)) <= 0)
778 if (qword_get(&cp, path, strlen(lbuf)) <= 0)
784 struct addrinfo *tmp;
785 tmp = host_pton(dom);
788 ai = client_resolve(tmp->ai_addr);
793 found = lookup_export(dom, path, ai);
796 if (dump_to_cache(f, dom, path, &found->m_export) < 0) {
798 "Cannot export %s, possibly unsupported filesystem"
799 " or fsid= required", path);
800 dump_to_cache(f, dom, path, NULL);
803 dump_to_cache(f, dom, path, NULL);
806 xlog(D_CALL, "nfsd_export: found %p path %s", found, path ? path : NULL);
808 if (path) free(path);
815 void (*cache_handle)(FILE *f);
818 { "auth.unix.ip", auth_unix_ip, NULL},
819 { "auth.unix.gid", auth_unix_gid, NULL},
820 { "nfsd.export", nfsd_export, NULL},
821 { "nfsd.fh", nfsd_fh, NULL},
825 extern int manage_gids;
828 * cache_open - prepare communications channels with kernel RPC caches
831 void cache_open(void)
834 for (i=0; cachelist[i].cache_name; i++ ) {
836 if (!manage_gids && cachelist[i].cache_handle == auth_unix_gid)
838 sprintf(path, "/proc/net/rpc/%s/channel", cachelist[i].cache_name);
839 cachelist[i].f = fopen(path, "r+");
844 * cache_set_fds - prepare cache file descriptors for one iteration of the service loop
845 * @fdset: pointer to fd_set to prepare
847 void cache_set_fds(fd_set *fdset)
850 for (i=0; cachelist[i].cache_name; i++) {
852 FD_SET(fileno(cachelist[i].f), fdset);
857 * cache_process_req - process any active cache file descriptors during service loop iteration
858 * @fdset: pointer to fd_set to examine for activity
860 int cache_process_req(fd_set *readfds)
864 for (i=0; cachelist[i].cache_name; i++) {
865 if (cachelist[i].f != NULL &&
866 FD_ISSET(fileno(cachelist[i].f), readfds)) {
868 cachelist[i].cache_handle(cachelist[i].f);
869 FD_CLR(fileno(cachelist[i].f), readfds);
877 * Give IP->domain and domain+path->options to kernel
878 * % echo nfsd $IP $[now+DEFAULT_TTL] $domain > /proc/net/rpc/auth.unix.ip/channel
879 * % echo $domain $path $[now+DEFAULT_TTL] $options $anonuid $anongid $fsid > /proc/net/rpc/nfsd.export/channel
882 static int cache_export_ent(char *domain, struct exportent *exp, char *path)
885 FILE *f = fopen("/proc/net/rpc/nfsd.export/channel", "w");
889 err = dump_to_cache(f, domain, exp->e_path, exp);
892 "Cannot export %s, possibly unsupported filesystem or"
893 " fsid= required", exp->e_path);
896 while (err == 0 && (exp->e_flags & NFSEXP_CROSSMOUNT) && path) {
897 /* really an 'if', but we can break out of
898 * a 'while' more easily */
899 /* Look along 'path' for other filesystems
900 * and export them with the same options
903 size_t l = strlen(exp->e_path);
906 if (strlen(path) <= l || path[l] != '/' ||
907 strncmp(exp->e_path, path, l) != 0)
909 if (stat(exp->e_path, &stb) != 0)
912 while(path[l] == '/') {
914 /* errors for submount should fail whole filesystem */
918 while (path[l] != '/' && path[l])
922 err2 = lstat(path, &stb);
926 if (stb.st_dev == dev)
930 dump_to_cache(f, domain, path, exp);
941 * cache_export - Inform kernel of a new nfs_export
942 * @exp: target nfs_export
943 * @path: NUL-terminated C string containing export path
945 int cache_export(nfs_export *exp, char *path)
947 char buf[INET6_ADDRSTRLEN];
951 f = fopen("/proc/net/rpc/auth.unix.ip/channel", "w");
956 qword_print(f, "nfsd");
958 host_ntop(get_addrlist(exp->m_client, 0), buf, sizeof(buf)));
959 qword_printuint(f, time(0) + exp->m_export.e_ttl);
960 qword_print(f, exp->m_client->m_hostname);
965 err = cache_export_ent(exp->m_client->m_hostname, &exp->m_export, path)
971 * cache_get_filehandle - given an nfs_export, get its root filehandle
972 * @exp: target nfs_export
973 * @len: length of requested file handle
974 * @p: NUL-terminated C string containing export path
976 * Returns pointer to NFS file handle of root directory of export
979 * echo $domain $path $length
980 * read filehandle <&0
981 * } <> /proc/fs/nfsd/filehandle
984 cache_get_filehandle(nfs_export *exp, int len, char *p)
986 FILE *f = fopen("/proc/fs/nfsd/filehandle", "r+");
990 static struct nfs_fh_len fh;
993 f = fopen("/proc/fs/nfs/filehandle", "r+");
997 qword_print(f, exp->m_client->m_hostname);
999 qword_printint(f, len);
1000 failed = qword_eol(f);
1003 failed = (fgets(buf, sizeof(buf), f) == NULL);
1007 memset(fh.fh_handle, 0, sizeof(fh.fh_handle));
1008 fh.fh_size = qword_get(&bp, (char *)fh.fh_handle, NFS3_FHSIZE);