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>
33 #include "pseudoflavors.h"
36 #include "blkid/blkid.h"
40 * Invoked by RPC service loop
42 void cache_set_fds(fd_set *fdset);
43 int cache_process_req(fd_set *readfds);
57 * Support routines for text-based upcalls.
58 * Fields are separated by spaces.
59 * Fields are either mangled to quote space tab newline slosh with slosh
60 * or a hexified with a leading \x
61 * Record is terminated with newline.
64 static int cache_export_ent(char *domain, struct exportent *exp, char *p);
66 #define INITIAL_MANAGED_GROUPS 100
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 + 1];
85 struct addrinfo *tmp = NULL;
86 if (readline(fileno(f), &lbuf, &lbuflen) != 1)
89 xlog(D_CALL, "auth_unix_ip: inbuf '%s'", lbuf);
93 if (qword_get(&cp, class, 20) <= 0 ||
94 strcmp(class, "nfsd") != 0)
97 if (qword_get(&cp, ipaddr, sizeof(ipaddr) - 1) <= 0)
100 tmp = host_pton(ipaddr);
106 /* addr is a valid, interesting address, find the domain name... */
108 struct addrinfo *ai = NULL;
110 ai = client_resolve(tmp->ai_addr);
112 client = client_compose(ai);
116 qword_print(f, "nfsd");
117 qword_print(f, ipaddr);
118 qword_printtimefrom(f, DEFAULT_TTL);
120 memmove(ipaddr + 1, ipaddr, strlen(ipaddr) + 1);
122 qword_print(f, ipaddr);
124 qword_print(f, *client?client:"DEFAULT");
126 xlog(D_CALL, "auth_unix_ip: client %p '%s'", client, client?client: "DEFAULT");
133 static void auth_unix_gid(FILE *f)
138 * uid expiry count list of group ids
142 static gid_t *groups = NULL;
143 static int groups_len = 0;
149 if (groups_len == 0) {
150 groups = malloc(sizeof(gid_t) * INITIAL_MANAGED_GROUPS);
154 groups_len = INITIAL_MANAGED_GROUPS;
157 ngroups = groups_len;
159 if (readline(fileno(f), &lbuf, &lbuflen) != 1)
163 if (qword_get_uint(&cp, &uid) != 0)
170 rv = getgrouplist(pw->pw_name, pw->pw_gid, groups, &ngroups);
171 if (rv == -1 && ngroups >= groups_len) {
172 more_groups = realloc(groups, sizeof(gid_t)*ngroups);
176 groups = more_groups;
177 groups_len = ngroups;
178 rv = getgrouplist(pw->pw_name, pw->pw_gid,
183 qword_printuint(f, uid);
184 qword_printtimefrom(f, DEFAULT_TTL);
186 qword_printuint(f, ngroups);
187 for (i=0; i<ngroups; i++)
188 qword_printuint(f, groups[i]);
190 qword_printuint(f, 0);
195 static const char *get_uuid_blkdev(char *path)
197 /* We set *safe if we know that we need the
198 * fsid from statfs too.
200 static blkid_cache cache = NULL;
203 blkid_tag_iterate iter;
206 const char *val, *uuid = NULL;
209 blkid_get_cache(&cache, NULL);
211 if (stat(path, &stb) != 0)
213 devname = blkid_devno_to_devname(stb.st_dev);
216 dev = blkid_get_dev(cache, devname, BLKID_DEV_NORMAL);
220 iter = blkid_tag_iterate_begin(dev);
223 while (blkid_tag_next(iter, &type, &val) == 0) {
224 if (strcmp(type, "UUID") == 0)
226 if (strcmp(type, "TYPE") == 0 &&
227 strcmp(val, "btrfs") == 0) {
232 blkid_tag_iterate_end(iter);
236 #define get_uuid_blkdev(path) (NULL)
239 static int get_uuid(const char *val, size_t uuidlen, char *u)
241 /* extract hex digits from uuidstr and compose a uuid
242 * of the given length (max 16), xoring bytes to make
247 memset(u, 0, uuidlen);
248 for ( ; *val ; val++) {
269 static int uuid_by_path(char *path, int type, size_t uuidlen, char *uuid)
271 /* get a uuid for the filesystem found at 'path'.
272 * There are several possible ways of generating the
274 * Type 0 is used for new filehandles, while other types
275 * may be used to interpret old filehandle - to ensure smooth
277 * We return 1 if a uuid was found (and it might be worth
278 * trying the next type) or 0 if no more uuid types can be
282 /* Possible sources of uuid are
286 * On some filesystems (e.g. vfat) the statfs64 uuid is simply an
287 * encoding of the device that the filesystem is mounted from, so
288 * it we be very bad to use that (as device numbers change). blkid
290 * On other filesystems (e.g. btrfs) the statfs64 uuid contains
291 * important info that the blkid uuid cannot contain: This happens
292 * when multiple subvolumes are exported (they have the same
293 * blkid uuid but different statfs64 uuids).
294 * We rely on get_uuid_blkdev *knowing* which is which and not returning
295 * a uuid for filesystems where the statfs64 uuid is better.
300 const char *blkid_val;
303 blkid_val = get_uuid_blkdev(path);
305 if (statfs64(path, &st) == 0 &&
306 (st.f_fsid.__val[0] || st.f_fsid.__val[1]))
307 snprintf(fsid_val, 17, "%08x%08x",
308 st.f_fsid.__val[0], st.f_fsid.__val[1]);
312 if (blkid_val && (type--) == 0)
314 else if (fsid_val[0] && (type--) == 0)
319 get_uuid(val, uuidlen, uuid);
323 /* Iterate through /etc/mtab, finding mountpoints
324 * at or below a given path
326 static char *next_mnt(void **v, char *p)
330 size_t l = strlen(p);
332 f = setmntent("/etc/mtab", "r");
336 while ((me = getmntent(f)) != NULL &&
337 (strncmp(me->mnt_dir, p, l) != 0 ||
338 me->mnt_dir[l] != '/'))
348 static int is_subdirectory(char *child, char *parent)
350 size_t l = strlen(parent);
352 if (strcmp(parent, "/") == 0)
355 return strcmp(child, parent) == 0
356 || (strncmp(child, parent, l) == 0 && child[l] == '/');
359 static int path_matches(nfs_export *exp, char *path)
361 if (exp->m_export.e_flags & NFSEXP_CROSSMOUNT)
362 return is_subdirectory(path, exp->m_export.e_path);
363 return strcmp(path, exp->m_export.e_path) == 0;
367 export_matches(nfs_export *exp, char *dom, char *path, struct addrinfo *ai)
369 return path_matches(exp, path) && client_matches(exp, dom, ai);
372 /* True iff e1 is a child of e2 and e2 has crossmnt set: */
373 static bool subexport(struct exportent *e1, struct exportent *e2)
375 char *p1 = e1->e_path, *p2 = e2->e_path;
376 size_t l2 = strlen(p2);
378 return e2->e_flags & NFSEXP_CROSSMOUNT
379 && strncmp(p1, p2, l2) == 0
385 /* We could use a union for this, but it would be more
386 * complicated; why bother? */
390 unsigned int fsidnum;
395 static int parse_fsid(int fsidtype, int fsidlen, char *fsid,
396 struct parsed_fsid *parsed)
399 unsigned long long inode64;
401 memset(parsed, 0, sizeof(*parsed));
402 parsed->fsidtype = fsidtype;
404 case FSID_DEV: /* 4 bytes: 2 major, 2 minor, 4 inode */
407 memcpy(&dev, fsid, 4);
408 memcpy(&parsed->inode, fsid+4, 4);
409 parsed->major = ntohl(dev)>>16;
410 parsed->minor = ntohl(dev) & 0xFFFF;
413 case FSID_NUM: /* 4 bytes - fsid */
416 memcpy(&parsed->fsidnum, fsid, 4);
419 case FSID_MAJOR_MINOR: /* 12 bytes: 4 major, 4 minor, 4 inode
420 * This format is never actually used but was
421 * an historical accident
425 memcpy(&dev, fsid, 4);
426 parsed->major = ntohl(dev);
427 memcpy(&dev, fsid+4, 4);
428 parsed->minor = ntohl(dev);
429 memcpy(&parsed->inode, fsid+8, 4);
432 case FSID_ENCODE_DEV: /* 8 bytes: 4 byte packed device number, 4 inode */
433 /* This is *host* endian, not net-byte-order, because
434 * no-one outside this host has any business interpreting it
438 memcpy(&dev, fsid, 4);
439 memcpy(&parsed->inode, fsid+4, 4);
440 parsed->major = (dev & 0xfff00) >> 8;
441 parsed->minor = (dev & 0xff) | ((dev >> 12) & 0xfff00);
444 case FSID_UUID4_INUM: /* 4 byte inode number and 4 byte uuid */
447 memcpy(&parsed->inode, fsid, 4);
449 parsed->fhuuid = fsid+4;
451 case FSID_UUID8: /* 8 byte uuid */
455 parsed->fhuuid = fsid;
457 case FSID_UUID16: /* 16 byte uuid */
460 parsed->uuidlen = 16;
461 parsed->fhuuid = fsid;
463 case FSID_UUID16_INUM: /* 8 byte inode number and 16 byte uuid */
466 memcpy(&inode64, fsid, 8);
467 parsed->inode = inode64;
468 parsed->uuidlen = 16;
469 parsed->fhuuid = fsid+8;
475 static bool match_fsid(struct parsed_fsid *parsed, nfs_export *exp, char *path)
481 if (stat(path, &stb) != 0)
483 if (!S_ISDIR(stb.st_mode) && !S_ISREG(stb.st_mode))
486 switch (parsed->fsidtype) {
488 case FSID_MAJOR_MINOR:
489 case FSID_ENCODE_DEV:
490 if (stb.st_ino != parsed->inode)
492 if (parsed->major != major(stb.st_dev) ||
493 parsed->minor != minor(stb.st_dev))
497 if (((exp->m_export.e_flags & NFSEXP_FSID) == 0 ||
498 exp->m_export.e_fsid != parsed->fsidnum))
501 case FSID_UUID4_INUM:
502 case FSID_UUID16_INUM:
503 if (stb.st_ino != parsed->inode)
508 if (!is_mountpoint(path))
511 if (exp->m_export.e_uuid)
512 get_uuid(exp->m_export.e_uuid, parsed->uuidlen, u);
515 uuid_by_path(path, type, parsed->uuidlen, u);
517 if (memcmp(u, parsed->fhuuid, parsed->uuidlen) == 0)
520 if (memcmp(u, parsed->fhuuid, parsed->uuidlen) != 0)
524 /* Well, unreachable, actually: */
528 static struct addrinfo *lookup_client_addr(char *dom)
530 struct addrinfo *ret;
531 struct addrinfo *tmp;
533 dom++; /* skip initial "$" */
535 tmp = host_pton(dom);
538 ret = client_resolve(tmp->ai_addr);
543 static void nfsd_fh(FILE *f)
546 * domain fsidtype fsid
547 * interpret fsid, find export point and options, and write:
548 * domain fsidtype fsid expiry path
555 struct parsed_fsid parsed;
556 struct exportent *found = NULL;
557 struct addrinfo *ai = NULL;
558 char *found_path = NULL;
563 if (readline(fileno(f), &lbuf, &lbuflen) != 1)
566 xlog(D_CALL, "nfsd_fh: inbuf '%s'", lbuf);
570 dom = malloc(strlen(cp));
573 if (qword_get(&cp, dom, strlen(cp)) <= 0)
575 if (qword_get_int(&cp, &fsidtype) != 0)
577 if (fsidtype < 0 || fsidtype > 7)
578 goto out; /* unknown type */
579 if ((fsidlen = qword_get(&cp, fsid, 32)) <= 0)
581 if (parse_fsid(fsidtype, fsidlen, fsid, &parsed))
586 if (is_ipaddr_client(dom)) {
587 ai = lookup_client_addr(dom);
592 /* Now determine export point for this fsid/domain */
593 for (i=0 ; i < MCL_MAXTYPES; i++) {
594 nfs_export *next_exp;
595 for (exp = exportlist[i].p_head; exp; exp = next_exp) {
598 if (exp->m_export.e_flags & NFSEXP_CROSSMOUNT) {
599 static nfs_export *prev = NULL;
600 static void *mnt = NULL;
604 path = next_mnt(&mnt, exp->m_export.e_path);
606 next_exp = exp->m_next;
614 path = exp->m_export.e_path;
618 path = exp->m_export.e_path;
619 next_exp = exp->m_next;
622 if (!is_ipaddr_client(dom)
623 && !namelist_client_matches(exp, dom))
625 if (exp->m_export.e_mountpoint &&
626 !is_mountpoint(exp->m_export.e_mountpoint[0]?
627 exp->m_export.e_mountpoint:
628 exp->m_export.e_path))
631 if (!match_fsid(&parsed, exp, path))
633 if (is_ipaddr_client(dom)
634 && !ipaddr_client_matches(exp, ai))
636 if (!found || subexport(&exp->m_export, found)) {
637 found = &exp->m_export;
639 found_path = strdup(path);
640 if (found_path == NULL)
642 } else if (strcmp(found->e_path, exp->m_export.e_path) != 0
643 && !subexport(found, &exp->m_export))
645 xlog(L_WARNING, "%s and %s have same filehandle for %s, using first",
646 found_path, path, dom);
648 /* same path, if one is V4ROOT, choose the other */
649 if (found->e_flags & NFSEXP_V4ROOT) {
650 found = &exp->m_export;
652 found_path = strdup(path);
653 if (found_path == NULL)
660 found->e_mountpoint &&
661 !is_mountpoint(found->e_mountpoint[0]?
664 /* Cannot export this yet
665 * should log a warning, but need to rate limit
666 xlog(L_WARNING, "%s not exported as %d not a mountpoint",
667 found->e_path, found->e_mountpoint);
669 /* FIXME we need to make sure we re-visit this later */
672 if (!found && dev_missing) {
673 /* The missing dev could be what we want, so just be
674 * quite rather than returning stale yet
680 if (cache_export_ent(dom, found, found_path) < 0)
684 qword_printint(f, fsidtype);
685 qword_printhex(f, fsid, fsidlen);
686 /* The fsid -> path lookup can be quite expensive as it
687 * potentially stats and reads lots of devices, and some of those
688 * might have spun-down. The Answer is not likely to
689 * change underneath us, and an 'exportfs -f' can always
690 * remove this from the kernel, so use a really log
691 * timeout. Maybe this should be configurable on the command
694 qword_printint(f, 0x7fffffff);
696 qword_print(f, found_path);
703 xlog(D_CALL, "nfsd_fh: found %p path %s", found, found ? found->e_path : NULL);
707 static void write_fsloc(FILE *f, struct exportent *ep)
709 struct servers *servers;
711 if (ep->e_fslocmethod == FSLOC_NONE)
714 servers = replicas_lookup(ep->e_fslocmethod, ep->e_fslocdata);
717 qword_print(f, "fsloc");
718 qword_printint(f, servers->h_num);
719 if (servers->h_num >= 0) {
721 for (i=0; i<servers->h_num; i++) {
722 qword_print(f, servers->h_mp[i]->h_host);
723 qword_print(f, servers->h_mp[i]->h_path);
726 qword_printint(f, servers->h_referral);
727 release_replicas(servers);
730 static void write_secinfo(FILE *f, struct exportent *ep, int flag_mask)
734 for (p = ep->e_secinfo; p->flav; p++)
736 if (p == ep->e_secinfo) {
737 /* There was no sec= option */
740 fix_pseudoflavor_flags(ep);
741 qword_print(f, "secinfo");
742 qword_printint(f, p - ep->e_secinfo);
743 for (p = ep->e_secinfo; p->flav; p++) {
744 qword_printint(f, p->flav->fnum);
745 qword_printint(f, p->flags & flag_mask);
750 static int dump_to_cache(FILE *f, char *domain, char *path, struct exportent *exp)
752 qword_print(f, domain);
753 qword_print(f, path);
755 int different_fs = strcmp(path, exp->e_path) != 0;
756 int flag_mask = different_fs ? ~NFSEXP_FSID : ~0;
758 qword_printtimefrom(f, exp->e_ttl);
759 qword_printint(f, exp->e_flags & flag_mask);
760 qword_printint(f, exp->e_anonuid);
761 qword_printint(f, exp->e_anongid);
762 qword_printint(f, exp->e_fsid);
764 write_secinfo(f, exp, flag_mask);
765 if (exp->e_uuid == NULL || different_fs) {
767 if (uuid_by_path(path, 0, 16, u)) {
768 qword_print(f, "uuid");
769 qword_printhex(f, u, 16);
773 get_uuid(exp->e_uuid, 16, u);
774 qword_print(f, "uuid");
775 qword_printhex(f, u, 16);
778 qword_printtimefrom(f, DEFAULT_TTL);
783 lookup_export(char *dom, char *path, struct addrinfo *ai)
786 nfs_export *found = NULL;
790 for (i=0 ; i < MCL_MAXTYPES; i++) {
791 for (exp = exportlist[i].p_head; exp; exp = exp->m_next) {
792 if (!export_matches(exp, dom, path, ai))
799 /* Always prefer non-V4ROOT exports */
800 if (exp->m_export.e_flags & NFSEXP_V4ROOT)
802 if (found->m_export.e_flags & NFSEXP_V4ROOT) {
808 /* If one is a CROSSMOUNT, then prefer the longest path */
809 if (((found->m_export.e_flags & NFSEXP_CROSSMOUNT) ||
810 (exp->m_export.e_flags & NFSEXP_CROSSMOUNT)) &&
811 strlen(found->m_export.e_path) !=
812 strlen(exp->m_export.e_path)) {
814 if (strlen(exp->m_export.e_path) >
815 strlen(found->m_export.e_path)) {
821 } else if (found_type == i && found->m_warned == 0) {
822 xlog(L_WARNING, "%s exported to both %s and %s, "
823 "arbitrarily choosing options from first",
824 path, found->m_client->m_hostname, exp->m_client->m_hostname,
833 #ifdef HAVE_NFS_PLUGIN_H
836 #include <nfs-plugin.h>
839 * Find the export entry for the parent of "pathname".
840 * Caller must not free returned exportent.
842 static struct exportent *lookup_parent_export(char *dom,
843 const char *pathname, struct addrinfo *ai)
845 char *parent, *slash;
848 parent = strdup(pathname);
849 if (parent == NULL) {
850 xlog(D_GENERAL, "%s: failed to allocate parent path buffer",
854 xlog(D_CALL, "%s: pathname = '%s'", __func__, pathname);
857 /* shorten pathname by one component */
858 slash = strrchr(parent, '/');
860 xlog(D_GENERAL, "%s: no slash found in pathname",
866 if (strlen(parent) == 0) {
867 result = lookup_export(dom, "/", ai);
868 if (result == NULL) {
869 xlog(L_ERROR, "%s: no root export found.", __func__);
875 result = lookup_export(dom, parent, ai);
876 if (result == NULL) {
877 xlog(D_GENERAL, "%s: lookup_export(%s) found nothing",
883 xlog(D_CALL, "%s: found export for %s", __func__, parent);
885 return &result->m_export;
889 return mkexportent("*", "/", "insecure");
893 * Walk through a set of FS locations and build an e_fslocdata string.
894 * Returns true if all went to plan; otherwise, false.
896 static bool locations_to_fslocdata(struct jp_ops *ops,
897 nfs_fsloc_set_t locations, char *fslocdata,
898 size_t remaining, int *ttl)
900 char *server, *last_path, *rootpath, *ptr;
910 enum jp_status status;
913 status = ops->jp_get_next_location(locations, &server,
915 if (status == JP_EMPTY)
917 if (status != JP_OK) {
918 xlog(D_GENERAL, "%s: failed to parse location: %s",
919 __func__, ops->jp_error(status));
922 xlog(D_GENERAL, "%s: Location: %s:%s",
923 __func__, server, rootpath);
925 if (last_path && strcmp(rootpath, last_path) == 0) {
926 len = snprintf(ptr, remaining, "+%s", server);
928 xlog(D_GENERAL, "%s: snprintf: %m", __func__);
931 if ((size_t)len >= remaining) {
932 xlog(D_GENERAL, "%s: fslocdata buffer overflow", __func__);
935 remaining -= (size_t)len;
938 if (last_path == NULL)
939 len = snprintf(ptr, remaining, "%s@%s",
942 len = snprintf(ptr, remaining, ":%s@%s",
945 xlog(D_GENERAL, "%s: snprintf: %m", __func__);
948 if ((size_t)len >= remaining) {
949 xlog(D_GENERAL, "%s: fslocdata buffer overflow",
953 remaining -= (size_t)len;
955 last_path = rootpath;
963 xlog(D_CALL, "%s: fslocdata='%s', ttl=%d",
964 __func__, fslocdata, *ttl);
974 * Duplicate the junction's parent's export options and graft in
975 * the fslocdata we constructed from the locations list.
977 static struct exportent *create_junction_exportent(struct exportent *parent,
978 const char *junction, const char *fslocdata, int ttl)
980 static struct exportent *eep;
982 eep = (struct exportent *)malloc(sizeof(*eep));
986 dupexportent(eep, parent);
987 strcpy(eep->e_path, junction);
988 eep->e_hostname = strdup(parent->e_hostname);
989 if (eep->e_hostname == NULL) {
995 eep->e_ttl = (unsigned int)ttl;
997 free(eep->e_fslocdata);
998 eep->e_fslocdata = strdup(fslocdata);
999 if (eep->e_fslocdata == NULL) {
1000 free(eep->e_hostname);
1004 eep->e_fslocmethod = FSLOC_REFER;
1008 xlog(L_ERROR, "%s: No memory", __func__);
1013 * Walk through the set of FS locations and build an exportent.
1014 * Returns pointer to an exportent if "junction" refers to a junction.
1016 static struct exportent *locations_to_export(struct jp_ops *ops,
1017 nfs_fsloc_set_t locations, const char *junction,
1018 struct exportent *parent)
1020 static char fslocdata[BUFSIZ];
1023 fslocdata[0] = '\0';
1024 if (!locations_to_fslocdata(ops, locations,
1025 fslocdata, sizeof(fslocdata), &ttl))
1027 return create_junction_exportent(parent, junction, fslocdata, ttl);
1031 * Retrieve locations information in "junction" and dump it to the
1032 * kernel. Returns pointer to an exportent if "junction" refers
1035 static struct exportent *invoke_junction_ops(void *handle, char *dom,
1036 const char *junction, struct addrinfo *ai)
1038 struct exportent *parent, *exp = NULL;
1039 nfs_fsloc_set_t locations;
1040 enum jp_status status;
1044 ops = (struct jp_ops *)dlsym(handle, "nfs_junction_ops");
1046 if (error != NULL) {
1047 xlog(D_GENERAL, "%s: dlsym(jp_junction_ops): %s",
1051 if (ops->jp_api_version != JP_API_VERSION) {
1052 xlog(D_GENERAL, "%s: unrecognized junction API version: %u",
1053 __func__, ops->jp_api_version);
1057 status = ops->jp_init(false);
1058 if (status != JP_OK) {
1059 xlog(D_GENERAL, "%s: failed to resolve %s: %s",
1060 __func__, junction, ops->jp_error(status));
1064 status = ops->jp_get_locations(junction, &locations);
1068 case JP_NOTJUNCTION:
1069 xlog(D_GENERAL, "%s: %s is not a junction",
1070 __func__, junction);
1073 xlog(L_WARNING, "Dangling junction %s: %s",
1074 junction, ops->jp_error(status));
1078 parent = lookup_parent_export(dom, junction, ai);
1082 exp = locations_to_export(ops, locations, junction, parent);
1084 ops->jp_put_locations(locations);
1092 * Load the junction plug-in, then try to resolve "pathname".
1093 * Returns pointer to an initialized exportent if "junction"
1094 * refers to a junction, or NULL if not.
1096 static struct exportent *lookup_junction(char *dom, const char *pathname,
1097 struct addrinfo *ai)
1099 struct exportent *exp;
1100 struct link_map *map;
1103 handle = dlopen("libnfsjunct.so", RTLD_NOW);
1104 if (handle == NULL) {
1105 xlog(D_GENERAL, "%s: dlopen: %s", __func__, dlerror());
1109 if (dlinfo(handle, RTLD_DI_LINKMAP, &map) == 0)
1110 xlog(D_GENERAL, "%s: loaded plug-in %s",
1111 __func__, map->l_name);
1113 (void)dlerror(); /* Clear any error */
1115 exp = invoke_junction_ops(handle, dom, pathname, ai);
1117 /* We could leave it loaded to make junction resolution
1118 * faster next time. However, if we want to replace the
1119 * library, that would require restarting mountd. */
1120 (void)dlclose(handle);
1124 static void lookup_nonexport(FILE *f, char *dom, char *path,
1125 struct addrinfo *ai)
1127 struct exportent *eep;
1129 eep = lookup_junction(dom, path, ai);
1130 dump_to_cache(f, dom, path, eep);
1133 exportent_release(eep);
1136 #else /* !HAVE_NFS_PLUGIN_H */
1137 static void lookup_nonexport(FILE *f, char *dom, char *path,
1138 struct addrinfo *UNUSED(ai))
1140 dump_to_cache(f, dom, path, NULL);
1142 #endif /* !HAVE_NFS_PLUGIN_H */
1144 static void nfsd_export(FILE *f)
1148 * determine export options and return:
1149 * domain path expiry flags anonuid anongid fsid
1154 nfs_export *found = NULL;
1155 struct addrinfo *ai = NULL;
1157 if (readline(fileno(f), &lbuf, &lbuflen) != 1)
1160 xlog(D_CALL, "nfsd_export: inbuf '%s'", lbuf);
1163 dom = malloc(strlen(cp));
1164 path = malloc(strlen(cp));
1169 if (qword_get(&cp, dom, strlen(lbuf)) <= 0)
1171 if (qword_get(&cp, path, strlen(lbuf)) <= 0)
1176 if (is_ipaddr_client(dom)) {
1177 ai = lookup_client_addr(dom);
1182 found = lookup_export(dom, path, ai);
1185 if (dump_to_cache(f, dom, path, &found->m_export) < 0) {
1187 "Cannot export %s, possibly unsupported filesystem"
1188 " or fsid= required", path);
1189 dump_to_cache(f, dom, path, NULL);
1192 lookup_nonexport(f, dom, path, ai);
1195 xlog(D_CALL, "nfsd_export: found %p path %s", found, path ? path : NULL);
1197 if (path) free(path);
1204 void (*cache_handle)(FILE *f);
1206 char vbuf[RPC_CHAN_BUF_SIZE];
1208 { "auth.unix.ip", auth_unix_ip, NULL, ""},
1209 { "auth.unix.gid", auth_unix_gid, NULL, ""},
1210 { "nfsd.export", nfsd_export, NULL, ""},
1211 { "nfsd.fh", nfsd_fh, NULL, ""},
1212 { NULL, NULL, NULL, ""}
1215 extern int manage_gids;
1218 * cache_open - prepare communications channels with kernel RPC caches
1221 void cache_open(void)
1224 for (i=0; cachelist[i].cache_name; i++ ) {
1226 if (!manage_gids && cachelist[i].cache_handle == auth_unix_gid)
1228 sprintf(path, "/proc/net/rpc/%s/channel", cachelist[i].cache_name);
1229 cachelist[i].f = fopen(path, "r+");
1230 if (cachelist[i].f != NULL) {
1231 setvbuf(cachelist[i].f, cachelist[i].vbuf, _IOLBF,
1238 * cache_set_fds - prepare cache file descriptors for one iteration of the service loop
1239 * @fdset: pointer to fd_set to prepare
1241 void cache_set_fds(fd_set *fdset)
1244 for (i=0; cachelist[i].cache_name; i++) {
1246 FD_SET(fileno(cachelist[i].f), fdset);
1251 * cache_process_req - process any active cache file descriptors during service loop iteration
1252 * @fdset: pointer to fd_set to examine for activity
1254 int cache_process_req(fd_set *readfds)
1258 for (i=0; cachelist[i].cache_name; i++) {
1259 if (cachelist[i].f != NULL &&
1260 FD_ISSET(fileno(cachelist[i].f), readfds)) {
1262 cachelist[i].cache_handle(cachelist[i].f);
1263 FD_CLR(fileno(cachelist[i].f), readfds);
1271 * Give IP->domain and domain+path->options to kernel
1272 * % echo nfsd $IP $[now+DEFAULT_TTL] $domain > /proc/net/rpc/auth.unix.ip/channel
1273 * % echo $domain $path $[now+DEFAULT_TTL] $options $anonuid $anongid $fsid > /proc/net/rpc/nfsd.export/channel
1276 static int cache_export_ent(char *domain, struct exportent *exp, char *path)
1279 FILE *f = fopen("/proc/net/rpc/nfsd.export/channel", "w");
1283 err = dump_to_cache(f, domain, exp->e_path, exp);
1286 "Cannot export %s, possibly unsupported filesystem or"
1287 " fsid= required", exp->e_path);
1290 while (err == 0 && (exp->e_flags & NFSEXP_CROSSMOUNT) && path) {
1291 /* really an 'if', but we can break out of
1292 * a 'while' more easily */
1293 /* Look along 'path' for other filesystems
1294 * and export them with the same options
1297 size_t l = strlen(exp->e_path);
1300 if (strlen(path) <= l || path[l] != '/' ||
1301 strncmp(exp->e_path, path, l) != 0)
1303 if (stat(exp->e_path, &stb) != 0)
1306 while(path[l] == '/') {
1308 /* errors for submount should fail whole filesystem */
1312 while (path[l] != '/' && path[l])
1316 err2 = lstat(path, &stb);
1320 if (stb.st_dev == dev)
1324 dump_to_cache(f, domain, path, exp);
1335 * cache_export - Inform kernel of a new nfs_export
1336 * @exp: target nfs_export
1337 * @path: NUL-terminated C string containing export path
1339 int cache_export(nfs_export *exp, char *path)
1341 char buf[INET6_ADDRSTRLEN];
1345 f = fopen("/proc/net/rpc/auth.unix.ip/channel", "w");
1350 qword_print(f, "nfsd");
1352 host_ntop(get_addrlist(exp->m_client, 0), buf, sizeof(buf)));
1353 qword_printtimefrom(f, exp->m_export.e_ttl);
1354 qword_print(f, exp->m_client->m_hostname);
1359 err = cache_export_ent(exp->m_client->m_hostname, &exp->m_export, path)
1365 * cache_get_filehandle - given an nfs_export, get its root filehandle
1366 * @exp: target nfs_export
1367 * @len: length of requested file handle
1368 * @p: NUL-terminated C string containing export path
1370 * Returns pointer to NFS file handle of root directory of export
1373 * echo $domain $path $length
1374 * read filehandle <&0
1375 * } <> /proc/fs/nfsd/filehandle
1378 cache_get_filehandle(nfs_export *exp, int len, char *p)
1380 FILE *f = fopen("/proc/fs/nfsd/filehandle", "r+");
1384 static struct nfs_fh_len fh;
1387 f = fopen("/proc/fs/nfs/filehandle", "r+");
1391 qword_print(f, exp->m_client->m_hostname);
1393 qword_printint(f, len);
1394 failed = qword_eol(f);
1397 failed = (fgets(buf, sizeof(buf), f) == NULL);
1401 memset(fh.fh_handle, 0, sizeof(fh.fh_handle));
1402 fh.fh_size = qword_get(&bp, (char *)fh.fh_handle, NFS3_FHSIZE);