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 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 struct addrinfo *ai = NULL;
111 ai = client_resolve(tmp->ai_addr);
114 client = client_compose(ai);
119 qword_print(f, "nfsd");
120 qword_print(f, ipaddr);
121 qword_printuint(f, time(0) + DEFAULT_TTL);
123 qword_print(f, ipaddr);
125 qword_print(f, *client?client:"DEFAULT");
127 xlog(D_CALL, "auth_unix_ip: client %p '%s'", client, client?client: "DEFAULT");
135 static void auth_unix_gid(FILE *f)
140 * uid expiry count list of group ids
144 static gid_t *groups = NULL;
145 static int groups_len = 0;
151 if (groups_len == 0) {
152 groups = malloc(sizeof(gid_t) * INITIAL_MANAGED_GROUPS);
156 groups_len = INITIAL_MANAGED_GROUPS;
159 ngroups = groups_len;
161 if (readline(fileno(f), &lbuf, &lbuflen) != 1)
165 if (qword_get_uint(&cp, &uid) != 0)
172 rv = getgrouplist(pw->pw_name, pw->pw_gid, groups, &ngroups);
173 if (rv == -1 && ngroups >= groups_len) {
174 more_groups = realloc(groups, sizeof(gid_t)*ngroups);
178 groups = more_groups;
179 groups_len = ngroups;
180 rv = getgrouplist(pw->pw_name, pw->pw_gid,
185 qword_printuint(f, uid);
186 qword_printuint(f, time(0) + DEFAULT_TTL);
188 qword_printuint(f, ngroups);
189 for (i=0; i<ngroups; i++)
190 qword_printuint(f, groups[i]);
192 qword_printuint(f, 0);
197 static const char *get_uuid_blkdev(char *path)
199 /* We set *safe if we know that we need the
200 * fsid from statfs too.
202 static blkid_cache cache = NULL;
205 blkid_tag_iterate iter;
208 const char *val, *uuid = NULL;
211 blkid_get_cache(&cache, NULL);
213 if (stat(path, &stb) != 0)
215 devname = blkid_devno_to_devname(stb.st_dev);
218 dev = blkid_get_dev(cache, devname, BLKID_DEV_NORMAL);
222 iter = blkid_tag_iterate_begin(dev);
225 while (blkid_tag_next(iter, &type, &val) == 0) {
226 if (strcmp(type, "UUID") == 0)
228 if (strcmp(type, "TYPE") == 0 &&
229 strcmp(val, "btrfs") == 0) {
234 blkid_tag_iterate_end(iter);
238 #define get_uuid_blkdev(path) (NULL)
241 static int get_uuid(const char *val, int uuidlen, char *u)
243 /* extract hex digits from uuidstr and compose a uuid
244 * of the given length (max 16), xoring bytes to make
249 memset(u, 0, uuidlen);
250 for ( ; *val ; val++) {
271 static int uuid_by_path(char *path, int type, int uuidlen, char *uuid)
273 /* get a uuid for the filesystem found at 'path'.
274 * There are several possible ways of generating the
276 * Type 0 is used for new filehandles, while other types
277 * may be used to interpret old filehandle - to ensure smooth
279 * We return 1 if a uuid was found (and it might be worth
280 * trying the next type) or 0 if no more uuid types can be
284 /* Possible sources of uuid are
288 * On some filesystems (e.g. vfat) the statfs64 uuid is simply an
289 * encoding of the device that the filesystem is mounted from, so
290 * it we be very bad to use that (as device numbers change). blkid
292 * On other filesystems (e.g. btrfs) the statfs64 uuid contains
293 * important info that the blkid uuid cannot contain: This happens
294 * when multiple subvolumes are exported (they have the same
295 * blkid uuid but different statfs64 uuids).
296 * We rely on get_uuid_blkdev *knowing* which is which and not returning
297 * a uuid for filesystems where the statfs64 uuid is better.
302 const char *blkid_val;
305 blkid_val = get_uuid_blkdev(path);
307 if (statfs64(path, &st) == 0 &&
308 (st.f_fsid.__val[0] || st.f_fsid.__val[1]))
309 snprintf(fsid_val, 17, "%08x%08x",
310 st.f_fsid.__val[0], st.f_fsid.__val[1]);
314 if (blkid_val && (type--) == 0)
316 else if (fsid_val[0] && (type--) == 0)
321 get_uuid(val, uuidlen, uuid);
325 /* Iterate through /etc/mtab, finding mountpoints
326 * at or below a given path
328 static char *next_mnt(void **v, char *p)
334 f = setmntent("/etc/mtab", "r");
338 while ((me = getmntent(f)) != NULL &&
339 (strncmp(me->mnt_dir, p, l) != 0 ||
340 me->mnt_dir[l] != '/'))
350 /* True iff e1 is a child of e2 and e2 has crossmnt set: */
351 static bool subexport(struct exportent *e1, struct exportent *e2)
353 char *p1 = e1->e_path, *p2 = e2->e_path;
356 return e2->e_flags & NFSEXP_CROSSMOUNT
357 && strncmp(p1, p2, l2) == 0
363 /* We could use a union for this, but it would be more
364 * complicated; why bother? */
368 unsigned int fsidnum;
373 int parse_fsid(int fsidtype, int fsidlen, char *fsid, struct parsed_fsid *parsed)
376 unsigned long long inode64;
378 parsed->fsidtype = fsidtype;
380 case FSID_DEV: /* 4 bytes: 2 major, 2 minor, 4 inode */
383 memcpy(&dev, fsid, 4);
384 memcpy(&parsed->inode, fsid+4, 4);
385 parsed->major = ntohl(dev)>>16;
386 parsed->minor = ntohl(dev) & 0xFFFF;
389 case FSID_NUM: /* 4 bytes - fsid */
392 memcpy(&parsed->fsidnum, fsid, 4);
395 case FSID_MAJOR_MINOR: /* 12 bytes: 4 major, 4 minor, 4 inode
396 * This format is never actually used but was
397 * an historical accident
401 memcpy(&dev, fsid, 4);
402 parsed->major = ntohl(dev);
403 memcpy(&dev, fsid+4, 4);
404 parsed->minor = ntohl(dev);
405 memcpy(&parsed->inode, fsid+8, 4);
408 case FSID_ENCODE_DEV: /* 8 bytes: 4 byte packed device number, 4 inode */
409 /* This is *host* endian, not net-byte-order, because
410 * no-one outside this host has any business interpreting it
414 memcpy(&dev, fsid, 4);
415 memcpy(&parsed->inode, fsid+4, 4);
416 parsed->major = (dev & 0xfff00) >> 8;
417 parsed->minor = (dev & 0xff) | ((dev >> 12) & 0xfff00);
420 case FSID_UUID4_INUM: /* 4 byte inode number and 4 byte uuid */
423 memcpy(&parsed->inode, fsid, 4);
425 parsed->fhuuid = fsid+4;
427 case FSID_UUID8: /* 8 byte uuid */
431 parsed->fhuuid = fsid;
433 case FSID_UUID16: /* 16 byte uuid */
436 parsed->uuidlen = 16;
437 parsed->fhuuid = fsid;
439 case FSID_UUID16_INUM: /* 8 byte inode number and 16 byte uuid */
442 memcpy(&inode64, fsid, 8);
443 parsed->inode = inode64;
444 parsed->uuidlen = 16;
445 parsed->fhuuid = fsid+8;
451 static bool match_fsid(struct parsed_fsid *parsed, nfs_export *exp, char *path)
457 if (stat(path, &stb) != 0)
459 if (!S_ISDIR(stb.st_mode) && !S_ISREG(stb.st_mode))
462 switch (parsed->fsidtype) {
464 case FSID_MAJOR_MINOR:
465 case FSID_ENCODE_DEV:
466 if (stb.st_ino != parsed->inode)
468 if (parsed->major != major(stb.st_dev) ||
469 parsed->minor != minor(stb.st_dev))
473 if (((exp->m_export.e_flags & NFSEXP_FSID) == 0 ||
474 exp->m_export.e_fsid != parsed->fsidnum))
477 case FSID_UUID4_INUM:
478 case FSID_UUID16_INUM:
479 if (stb.st_ino != parsed->inode)
484 if (!is_mountpoint(path))
487 if (exp->m_export.e_uuid)
488 get_uuid(exp->m_export.e_uuid, parsed->uuidlen, u);
491 uuid_by_path(path, type, parsed->uuidlen, u);
493 if (memcmp(u, parsed->fhuuid, parsed->uuidlen) == 0)
496 if (memcmp(u, parsed->fhuuid, parsed->uuidlen) != 0)
500 /* Well, unreachable, actually: */
504 struct addrinfo *lookup_client_addr(char *dom)
506 struct addrinfo *ret;
507 struct addrinfo *tmp;
509 dom++; /* skip initial "$" */
511 tmp = host_pton(dom);
514 ret = client_resolve(tmp->ai_addr);
519 static void nfsd_fh(FILE *f)
522 * domain fsidtype fsid
523 * interpret fsid, find export point and options, and write:
524 * domain fsidtype fsid expiry path
531 struct parsed_fsid parsed;
532 struct exportent *found = NULL;
533 struct addrinfo *ai = NULL;
534 char *found_path = NULL;
539 if (readline(fileno(f), &lbuf, &lbuflen) != 1)
542 xlog(D_CALL, "nfsd_fh: inbuf '%s'", lbuf);
546 dom = malloc(strlen(cp));
549 if (qword_get(&cp, dom, strlen(cp)) <= 0)
551 if (qword_get_int(&cp, &fsidtype) != 0)
553 if (fsidtype < 0 || fsidtype > 7)
554 goto out; /* unknown type */
555 if ((fsidlen = qword_get(&cp, fsid, 32)) <= 0)
557 if (parse_fsid(fsidtype, fsidlen, fsid, &parsed))
562 if (is_ipaddr_client(dom)) {
563 ai = lookup_client_addr(dom);
568 /* Now determine export point for this fsid/domain */
569 for (i=0 ; i < MCL_MAXTYPES; i++) {
570 nfs_export *next_exp;
571 for (exp = exportlist[i].p_head; exp; exp = next_exp) {
574 if (exp->m_export.e_flags & NFSEXP_CROSSMOUNT) {
575 static nfs_export *prev = NULL;
576 static void *mnt = NULL;
580 path = next_mnt(&mnt, exp->m_export.e_path);
582 next_exp = exp->m_next;
590 path = exp->m_export.e_path;
594 path = exp->m_export.e_path;
595 next_exp = exp->m_next;
598 if (!is_ipaddr_client(dom)
599 && !namelist_client_matches(exp, dom))
601 if (exp->m_export.e_mountpoint &&
602 !is_mountpoint(exp->m_export.e_mountpoint[0]?
603 exp->m_export.e_mountpoint:
604 exp->m_export.e_path))
607 if (!match_fsid(&parsed, exp, path))
609 if (is_ipaddr_client(dom)
610 && !ipaddr_client_matches(exp, ai))
612 if (!found || subexport(&exp->m_export, found)) {
613 found = &exp->m_export;
615 found_path = strdup(path);
616 if (found_path == NULL)
618 } else if (strcmp(found->e_path, exp->m_export.e_path) != 0
619 && !subexport(found, &exp->m_export))
621 xlog(L_WARNING, "%s and %s have same filehandle for %s, using first",
622 found_path, path, dom);
624 /* same path, if one is V4ROOT, choose the other */
625 if (found->e_flags & NFSEXP_V4ROOT) {
626 found = &exp->m_export;
628 found_path = strdup(path);
629 if (found_path == NULL)
636 found->e_mountpoint &&
637 !is_mountpoint(found->e_mountpoint[0]?
640 /* Cannot export this yet
641 * should log a warning, but need to rate limit
642 xlog(L_WARNING, "%s not exported as %d not a mountpoint",
643 found->e_path, found->e_mountpoint);
645 /* FIXME we need to make sure we re-visit this later */
648 if (!found && dev_missing) {
649 /* The missing dev could be what we want, so just be
650 * quite rather than returning stale yet
656 if (cache_export_ent(dom, found, found_path) < 0)
660 qword_printint(f, fsidtype);
661 qword_printhex(f, fsid, fsidlen);
662 /* The fsid -> path lookup can be quite expensive as it
663 * potentially stats and reads lots of devices, and some of those
664 * might have spun-down. The Answer is not likely to
665 * change underneath us, and an 'exportfs -f' can always
666 * remove this from the kernel, so use a really log
667 * timeout. Maybe this should be configurable on the command
670 qword_printint(f, 0x7fffffff);
672 qword_print(f, found_path);
679 xlog(D_CALL, "nfsd_fh: found %p path %s", found, found ? found->e_path : NULL);
683 static void write_fsloc(FILE *f, struct exportent *ep)
685 struct servers *servers;
687 if (ep->e_fslocmethod == FSLOC_NONE)
690 servers = replicas_lookup(ep->e_fslocmethod, ep->e_fslocdata);
693 qword_print(f, "fsloc");
694 qword_printint(f, servers->h_num);
695 if (servers->h_num >= 0) {
697 for (i=0; i<servers->h_num; i++) {
698 qword_print(f, servers->h_mp[i]->h_host);
699 qword_print(f, servers->h_mp[i]->h_path);
702 qword_printint(f, servers->h_referral);
703 release_replicas(servers);
706 static void write_secinfo(FILE *f, struct exportent *ep, int flag_mask)
710 for (p = ep->e_secinfo; p->flav; p++)
712 if (p == ep->e_secinfo) {
713 /* There was no sec= option */
716 qword_print(f, "secinfo");
717 qword_printint(f, p - ep->e_secinfo);
718 for (p = ep->e_secinfo; p->flav; p++) {
719 qword_printint(f, p->flav->fnum);
720 qword_printint(f, p->flags & flag_mask);
725 static int dump_to_cache(FILE *f, char *domain, char *path, struct exportent *exp)
727 qword_print(f, domain);
728 qword_print(f, path);
730 int different_fs = strcmp(path, exp->e_path) != 0;
731 int flag_mask = different_fs ? ~NFSEXP_FSID : ~0;
733 qword_printuint(f, time(0) + exp->e_ttl);
734 qword_printint(f, exp->e_flags & flag_mask);
735 qword_printint(f, exp->e_anonuid);
736 qword_printint(f, exp->e_anongid);
737 qword_printint(f, exp->e_fsid);
739 write_secinfo(f, exp, flag_mask);
740 if (exp->e_uuid == NULL || different_fs) {
742 if (uuid_by_path(path, 0, 16, u)) {
743 qword_print(f, "uuid");
744 qword_printhex(f, u, 16);
748 get_uuid(exp->e_uuid, 16, u);
749 qword_print(f, "uuid");
750 qword_printhex(f, u, 16);
753 qword_printuint(f, time(0) + DEFAULT_TTL);
757 static int is_subdirectory(char *child, char *parent)
759 int l = strlen(parent);
761 return strcmp(child, parent) == 0
762 || (strncmp(child, parent, l) == 0 && child[l] == '/');
765 static int path_matches(nfs_export *exp, char *path)
767 if (exp->m_export.e_flags & NFSEXP_CROSSMOUNT)
768 return is_subdirectory(path, exp->m_export.e_path);
769 return strcmp(path, exp->m_export.e_path) == 0;
773 export_matches(nfs_export *exp, char *dom, char *path, struct addrinfo *ai)
775 return path_matches(exp, path) && client_matches(exp, dom, ai);
779 lookup_export(char *dom, char *path, struct addrinfo *ai)
782 nfs_export *found = NULL;
786 for (i=0 ; i < MCL_MAXTYPES; i++) {
787 for (exp = exportlist[i].p_head; exp; exp = exp->m_next) {
788 if (!export_matches(exp, dom, path, ai))
795 /* Always prefer non-V4ROOT exports */
796 if (exp->m_export.e_flags & NFSEXP_V4ROOT)
798 if (found->m_export.e_flags & NFSEXP_V4ROOT) {
804 /* If one is a CROSSMOUNT, then prefer the longest path */
805 if (((found->m_export.e_flags & NFSEXP_CROSSMOUNT) ||
806 (exp->m_export.e_flags & NFSEXP_CROSSMOUNT)) &&
807 strlen(found->m_export.e_path) !=
808 strlen(exp->m_export.e_path)) {
810 if (strlen(exp->m_export.e_path) >
811 strlen(found->m_export.e_path)) {
817 } else if (found_type == i && found->m_warned == 0) {
818 xlog(L_WARNING, "%s exported to both %s and %s, "
819 "arbitrarily choosing options from first",
820 path, found->m_client->m_hostname, exp->m_client->m_hostname,
829 #ifdef HAVE_NFS_PLUGIN_H
831 #include <nfs-plugin.h>
834 * Walk through a set of FS locations and build a set of export options.
835 * Returns true if all went to plan; otherwise, false.
838 locations_to_options(struct jp_ops *ops, nfs_fsloc_set_t locations,
839 char *options, size_t remaining, int *ttl)
841 char *server, *last_path, *rootpath, *ptr;
851 enum jp_status status;
854 status = ops->jp_get_next_location(locations, &server,
856 if (status == JP_EMPTY)
858 if (status != JP_OK) {
859 xlog(D_GENERAL, "%s: failed to parse location: %s",
860 __func__, ops->jp_error(status));
863 xlog(D_GENERAL, "%s: Location: %s:%s",
864 __func__, server, rootpath);
866 if (last_path && strcmp(rootpath, last_path) == 0) {
867 len = snprintf(ptr, remaining, "+%s", server);
869 xlog(D_GENERAL, "%s: snprintf: %m", __func__);
872 if ((size_t)len >= remaining) {
873 xlog(D_GENERAL, "%s: options buffer overflow", __func__);
876 remaining -= (size_t)len;
879 if (last_path == NULL)
880 len = snprintf(ptr, remaining, "refer=%s@%s",
883 len = snprintf(ptr, remaining, ":%s@%s",
886 xlog(D_GENERAL, "%s: snprintf: %m", __func__);
889 if ((size_t)len >= remaining) {
890 xlog(D_GENERAL, "%s: options buffer overflow",
894 remaining -= (size_t)len;
896 last_path = rootpath;
904 xlog(D_CALL, "%s: options='%s', ttl=%d",
905 __func__, options, *ttl);
915 * Walk through the set of FS locations and build an exportent.
916 * Returns pointer to an exportent if "junction" refers to a junction.
918 * Returned exportent points to static memory.
920 static struct exportent *do_locations_to_export(struct jp_ops *ops,
921 nfs_fsloc_set_t locations, const char *junction,
922 char *options, size_t options_len)
924 struct exportent *exp;
927 if (!locations_to_options(ops, locations, options, options_len, &ttl))
930 exp = mkexportent("*", (char *)junction, options);
932 xlog(L_ERROR, "%s: Failed to construct exportent", __func__);
942 * Convert set of FS locations to an exportent. Returns pointer to
943 * an exportent if "junction" refers to a junction.
945 * Returned exportent points to static memory.
947 static struct exportent *locations_to_export(struct jp_ops *ops,
948 nfs_fsloc_set_t locations, const char *junction)
950 struct exportent *exp;
953 options = malloc(BUFSIZ);
954 if (options == NULL) {
955 xlog(D_GENERAL, "%s: failed to allocate options buffer",
961 exp = do_locations_to_export(ops, locations, junction,
969 * Retrieve locations information in "junction" and dump it to the
970 * kernel. Returns pointer to an exportent if "junction" refers
973 * Returned exportent points to static memory.
975 static struct exportent *invoke_junction_ops(void *handle,
976 const char *junction)
978 nfs_fsloc_set_t locations;
979 struct exportent *exp;
980 enum jp_status status;
984 ops = (struct jp_ops *)dlsym(handle, "nfs_junction_ops");
987 xlog(D_GENERAL, "%s: dlsym(jp_junction_ops): %s",
991 if (ops->jp_api_version != JP_API_VERSION) {
992 xlog(D_GENERAL, "%s: unrecognized junction API version: %u",
993 __func__, ops->jp_api_version);
997 status = ops->jp_init(false);
998 if (status != JP_OK) {
999 xlog(D_GENERAL, "%s: failed to resolve %s: %s",
1000 __func__, junction, ops->jp_error(status));
1004 status = ops->jp_get_locations(junction, &locations);
1005 if (status != JP_OK) {
1006 xlog(D_GENERAL, "%s: failed to resolve %s: %s",
1007 __func__, junction, ops->jp_error(status));
1011 exp = locations_to_export(ops, locations, junction);
1013 ops->jp_put_locations(locations);
1019 * Load the junction plug-in, then try to resolve "pathname".
1020 * Returns pointer to an initialized exportent if "junction"
1021 * refers to a junction, or NULL if not.
1023 * Returned exportent points to static memory.
1025 static struct exportent *lookup_junction(const char *pathname)
1027 struct exportent *exp;
1030 handle = dlopen("libnfsjunct.so", RTLD_NOW);
1031 if (handle == NULL) {
1032 xlog(D_GENERAL, "%s: dlopen: %s", __func__, dlerror());
1035 (void)dlerror(); /* Clear any error */
1037 exp = invoke_junction_ops(handle, pathname);
1039 /* We could leave it loaded to make junction resolution
1040 * faster next time. However, if we want to replace the
1041 * library, that would require restarting mountd. */
1042 (void)dlclose(handle);
1045 #else /* !HAVE_NFS_PLUGIN_H */
1046 static inline struct exportent *lookup_junction(const char *UNUSED(pathname))
1050 #endif /* !HAVE_NFS_PLUGIN_H */
1052 static void nfsd_export(FILE *f)
1056 * determine export options and return:
1057 * domain path expiry flags anonuid anongid fsid
1062 nfs_export *found = NULL;
1063 struct addrinfo *ai = NULL;
1065 if (readline(fileno(f), &lbuf, &lbuflen) != 1)
1068 xlog(D_CALL, "nfsd_export: inbuf '%s'", lbuf);
1071 dom = malloc(strlen(cp));
1072 path = malloc(strlen(cp));
1077 if (qword_get(&cp, dom, strlen(lbuf)) <= 0)
1079 if (qword_get(&cp, path, strlen(lbuf)) <= 0)
1084 if (is_ipaddr_client(dom)) {
1085 ai = lookup_client_addr(dom);
1090 found = lookup_export(dom, path, ai);
1093 if (dump_to_cache(f, dom, path, &found->m_export) < 0) {
1095 "Cannot export %s, possibly unsupported filesystem"
1096 " or fsid= required", path);
1097 dump_to_cache(f, dom, path, NULL);
1100 dump_to_cache(f, dom, path, lookup_junction(path));
1103 xlog(D_CALL, "nfsd_export: found %p path %s", found, path ? path : NULL);
1105 if (path) free(path);
1112 void (*cache_handle)(FILE *f);
1114 char vbuf[RPC_CHAN_BUF_SIZE];
1116 { "auth.unix.ip", auth_unix_ip, NULL, ""},
1117 { "auth.unix.gid", auth_unix_gid, NULL, ""},
1118 { "nfsd.export", nfsd_export, NULL, ""},
1119 { "nfsd.fh", nfsd_fh, NULL, ""},
1120 { NULL, NULL, NULL, ""}
1123 extern int manage_gids;
1126 * cache_open - prepare communications channels with kernel RPC caches
1129 void cache_open(void)
1132 for (i=0; cachelist[i].cache_name; i++ ) {
1134 if (!manage_gids && cachelist[i].cache_handle == auth_unix_gid)
1136 sprintf(path, "/proc/net/rpc/%s/channel", cachelist[i].cache_name);
1137 cachelist[i].f = fopen(path, "r+");
1138 if (cachelist[i].f != NULL) {
1139 setvbuf(cachelist[i].f, cachelist[i].vbuf, _IOLBF,
1146 * cache_set_fds - prepare cache file descriptors for one iteration of the service loop
1147 * @fdset: pointer to fd_set to prepare
1149 void cache_set_fds(fd_set *fdset)
1152 for (i=0; cachelist[i].cache_name; i++) {
1154 FD_SET(fileno(cachelist[i].f), fdset);
1159 * cache_process_req - process any active cache file descriptors during service loop iteration
1160 * @fdset: pointer to fd_set to examine for activity
1162 int cache_process_req(fd_set *readfds)
1166 for (i=0; cachelist[i].cache_name; i++) {
1167 if (cachelist[i].f != NULL &&
1168 FD_ISSET(fileno(cachelist[i].f), readfds)) {
1170 cachelist[i].cache_handle(cachelist[i].f);
1171 FD_CLR(fileno(cachelist[i].f), readfds);
1179 * Give IP->domain and domain+path->options to kernel
1180 * % echo nfsd $IP $[now+DEFAULT_TTL] $domain > /proc/net/rpc/auth.unix.ip/channel
1181 * % echo $domain $path $[now+DEFAULT_TTL] $options $anonuid $anongid $fsid > /proc/net/rpc/nfsd.export/channel
1184 static int cache_export_ent(char *domain, struct exportent *exp, char *path)
1187 FILE *f = fopen("/proc/net/rpc/nfsd.export/channel", "w");
1191 err = dump_to_cache(f, domain, exp->e_path, exp);
1194 "Cannot export %s, possibly unsupported filesystem or"
1195 " fsid= required", exp->e_path);
1198 while (err == 0 && (exp->e_flags & NFSEXP_CROSSMOUNT) && path) {
1199 /* really an 'if', but we can break out of
1200 * a 'while' more easily */
1201 /* Look along 'path' for other filesystems
1202 * and export them with the same options
1205 size_t l = strlen(exp->e_path);
1208 if (strlen(path) <= l || path[l] != '/' ||
1209 strncmp(exp->e_path, path, l) != 0)
1211 if (stat(exp->e_path, &stb) != 0)
1214 while(path[l] == '/') {
1216 /* errors for submount should fail whole filesystem */
1220 while (path[l] != '/' && path[l])
1224 err2 = lstat(path, &stb);
1228 if (stb.st_dev == dev)
1232 dump_to_cache(f, domain, path, exp);
1243 * cache_export - Inform kernel of a new nfs_export
1244 * @exp: target nfs_export
1245 * @path: NUL-terminated C string containing export path
1247 int cache_export(nfs_export *exp, char *path)
1249 char buf[INET6_ADDRSTRLEN];
1253 f = fopen("/proc/net/rpc/auth.unix.ip/channel", "w");
1258 qword_print(f, "nfsd");
1260 host_ntop(get_addrlist(exp->m_client, 0), buf, sizeof(buf)));
1261 qword_printuint(f, time(0) + exp->m_export.e_ttl);
1262 qword_print(f, exp->m_client->m_hostname);
1267 err = cache_export_ent(exp->m_client->m_hostname, &exp->m_export, path)
1273 * cache_get_filehandle - given an nfs_export, get its root filehandle
1274 * @exp: target nfs_export
1275 * @len: length of requested file handle
1276 * @p: NUL-terminated C string containing export path
1278 * Returns pointer to NFS file handle of root directory of export
1281 * echo $domain $path $length
1282 * read filehandle <&0
1283 * } <> /proc/fs/nfsd/filehandle
1286 cache_get_filehandle(nfs_export *exp, int len, char *p)
1288 FILE *f = fopen("/proc/fs/nfsd/filehandle", "r+");
1292 static struct nfs_fh_len fh;
1295 f = fopen("/proc/fs/nfs/filehandle", "r+");
1299 qword_print(f, exp->m_client->m_hostname);
1301 qword_printint(f, len);
1302 failed = qword_eol(f);
1305 failed = (fgets(buf, sizeof(buf), f) == NULL);
1309 memset(fh.fh_handle, 0, sizeof(fh.fh_handle));
1310 fh.fh_size = qword_get(&bp, (char *)fh.fh_handle, NFS3_FHSIZE);