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 = INITIAL_MANAGED_GROUPS;
153 ngroups = groups_len;
155 if (readline(fileno(f), &lbuf, &lbuflen) != 1)
159 if (qword_get_uint(&cp, &uid) != 0)
166 rv = getgrouplist(pw->pw_name, pw->pw_gid, groups, &ngroups);
167 if (rv == -1 && ngroups >= groups_len) {
168 more_groups = realloc(groups, sizeof(gid_t)*ngroups);
172 groups = more_groups;
173 groups_len = ngroups;
174 rv = getgrouplist(pw->pw_name, pw->pw_gid,
179 qword_printuint(f, uid);
180 qword_printuint(f, time(0) + DEFAULT_TTL);
182 qword_printuint(f, ngroups);
183 for (i=0; i<ngroups; i++)
184 qword_printuint(f, groups[i]);
186 qword_printuint(f, 0);
191 static const char *get_uuid_blkdev(char *path)
193 /* We set *safe if we know that we need the
194 * fsid from statfs too.
196 static blkid_cache cache = NULL;
199 blkid_tag_iterate iter;
202 const char *val, *uuid = NULL;
205 blkid_get_cache(&cache, NULL);
207 if (stat(path, &stb) != 0)
209 devname = blkid_devno_to_devname(stb.st_dev);
212 dev = blkid_get_dev(cache, devname, BLKID_DEV_NORMAL);
216 iter = blkid_tag_iterate_begin(dev);
219 while (blkid_tag_next(iter, &type, &val) == 0) {
220 if (strcmp(type, "UUID") == 0)
222 if (strcmp(type, "TYPE") == 0 &&
223 strcmp(val, "btrfs") == 0) {
228 blkid_tag_iterate_end(iter);
232 #define get_uuid_blkdev(path) (NULL)
235 static int get_uuid(const char *val, int uuidlen, char *u)
237 /* extract hex digits from uuidstr and compose a uuid
238 * of the given length (max 16), xoring bytes to make
243 memset(u, 0, uuidlen);
244 for ( ; *val ; val++) {
265 static int uuid_by_path(char *path, int type, int uuidlen, char *uuid)
267 /* get a uuid for the filesystem found at 'path'.
268 * There are several possible ways of generating the
270 * Type 0 is used for new filehandles, while other types
271 * may be used to interpret old filehandle - to ensure smooth
273 * We return 1 if a uuid was found (and it might be worth
274 * trying the next type) or 0 if no more uuid types can be
278 /* Possible sources of uuid are
282 * On some filesystems (e.g. vfat) the statfs64 uuid is simply an
283 * encoding of the device that the filesystem is mounted from, so
284 * it we be very bad to use that (as device numbers change). blkid
286 * On other filesystems (e.g. btrfs) the statfs64 uuid contains
287 * important info that the blkid uuid cannot contain: This happens
288 * when multiple subvolumes are exported (they have the same
289 * blkid uuid but different statfs64 uuids).
290 * We rely on get_uuid_blkdev *knowing* which is which and not returning
291 * a uuid for filesystems where the statfs64 uuid is better.
296 const char *blkid_val;
299 blkid_val = get_uuid_blkdev(path);
301 if (statfs64(path, &st) == 0 &&
302 (st.f_fsid.__val[0] || st.f_fsid.__val[1]))
303 snprintf(fsid_val, 17, "%08x%08x",
304 st.f_fsid.__val[0], st.f_fsid.__val[1]);
308 if (blkid_val && (type--) == 0)
310 else if (fsid_val[0] && (type--) == 0)
315 get_uuid(val, uuidlen, uuid);
319 /* Iterate through /etc/mtab, finding mountpoints
320 * at or below a given path
322 static char *next_mnt(void **v, char *p)
328 f = setmntent("/etc/mtab", "r");
332 while ((me = getmntent(f)) != NULL &&
333 (strncmp(me->mnt_dir, p, l) != 0 ||
334 me->mnt_dir[l] != '/'))
344 /* True iff e1 is a child of e2 and e2 has crossmnt set: */
345 static bool subexport(struct exportent *e1, struct exportent *e2)
347 char *p1 = e1->e_path, *p2 = e2->e_path;
350 return e2->e_flags & NFSEXP_CROSSMOUNT
351 && strncmp(p1, p2, l2) == 0
357 /* We could use a union for this, but it would be more
358 * complicated; why bother? */
362 unsigned int fsidnum;
367 int parse_fsid(int fsidtype, int fsidlen, char *fsid, struct parsed_fsid *parsed)
370 unsigned long long inode64;
372 parsed->fsidtype = fsidtype;
374 case FSID_DEV: /* 4 bytes: 2 major, 2 minor, 4 inode */
377 memcpy(&dev, fsid, 4);
378 memcpy(&parsed->inode, fsid+4, 4);
379 parsed->major = ntohl(dev)>>16;
380 parsed->minor = ntohl(dev) & 0xFFFF;
383 case FSID_NUM: /* 4 bytes - fsid */
386 memcpy(&parsed->fsidnum, fsid, 4);
389 case FSID_MAJOR_MINOR: /* 12 bytes: 4 major, 4 minor, 4 inode
390 * This format is never actually used but was
391 * an historical accident
395 memcpy(&dev, fsid, 4);
396 parsed->major = ntohl(dev);
397 memcpy(&dev, fsid+4, 4);
398 parsed->minor = ntohl(dev);
399 memcpy(&parsed->inode, fsid+8, 4);
402 case FSID_ENCODE_DEV: /* 8 bytes: 4 byte packed device number, 4 inode */
403 /* This is *host* endian, not net-byte-order, because
404 * no-one outside this host has any business interpreting it
408 memcpy(&dev, fsid, 4);
409 memcpy(&parsed->inode, fsid+4, 4);
410 parsed->major = (dev & 0xfff00) >> 8;
411 parsed->minor = (dev & 0xff) | ((dev >> 12) & 0xfff00);
414 case FSID_UUID4_INUM: /* 4 byte inode number and 4 byte uuid */
417 memcpy(&parsed->inode, fsid, 4);
419 parsed->fhuuid = fsid+4;
421 case FSID_UUID8: /* 8 byte uuid */
425 parsed->fhuuid = fsid;
427 case FSID_UUID16: /* 16 byte uuid */
430 parsed->uuidlen = 16;
431 parsed->fhuuid = fsid;
433 case FSID_UUID16_INUM: /* 8 byte inode number and 16 byte uuid */
436 memcpy(&inode64, fsid, 8);
437 parsed->inode = inode64;
438 parsed->uuidlen = 16;
439 parsed->fhuuid = fsid+8;
445 static bool match_fsid(struct parsed_fsid *parsed, nfs_export *exp, char *path)
451 if (stat(path, &stb) != 0)
453 if (!S_ISDIR(stb.st_mode) && !S_ISREG(stb.st_mode))
456 switch (parsed->fsidtype) {
458 case FSID_MAJOR_MINOR:
459 case FSID_ENCODE_DEV:
460 if (stb.st_ino != parsed->inode)
462 if (parsed->major != major(stb.st_dev) ||
463 parsed->minor != minor(stb.st_dev))
467 if (((exp->m_export.e_flags & NFSEXP_FSID) == 0 ||
468 exp->m_export.e_fsid != parsed->fsidnum))
471 case FSID_UUID4_INUM:
472 case FSID_UUID16_INUM:
473 if (stb.st_ino != parsed->inode)
478 if (!is_mountpoint(path))
481 if (exp->m_export.e_uuid)
482 get_uuid(exp->m_export.e_uuid, parsed->uuidlen, u);
485 uuid_by_path(path, type, parsed->uuidlen, u);
487 if (memcmp(u, parsed->fhuuid, parsed->uuidlen) == 0)
490 if (memcmp(u, parsed->fhuuid, parsed->uuidlen) != 0)
494 /* Well, unreachable, actually: */
498 struct addrinfo *lookup_client_addr(char *dom)
500 struct addrinfo *ret;
501 struct addrinfo *tmp;
503 tmp = host_pton(dom);
506 ret = client_resolve(tmp->ai_addr);
511 static void nfsd_fh(FILE *f)
514 * domain fsidtype fsid
515 * interpret fsid, find export point and options, and write:
516 * domain fsidtype fsid expiry path
523 struct parsed_fsid parsed;
524 struct exportent *found = NULL;
525 struct addrinfo *ai = NULL;
526 char *found_path = NULL;
531 if (readline(fileno(f), &lbuf, &lbuflen) != 1)
534 xlog(D_CALL, "nfsd_fh: inbuf '%s'", lbuf);
538 dom = malloc(strlen(cp));
541 if (qword_get(&cp, dom, strlen(cp)) <= 0)
543 if (qword_get_int(&cp, &fsidtype) != 0)
545 if (fsidtype < 0 || fsidtype > 7)
546 goto out; /* unknown type */
547 if ((fsidlen = qword_get(&cp, fsid, 32)) <= 0)
549 if (parse_fsid(fsidtype, fsidlen, fsid, &parsed))
555 ai = lookup_client_addr(dom);
560 /* Now determine export point for this fsid/domain */
561 for (i=0 ; i < MCL_MAXTYPES; i++) {
562 nfs_export *next_exp;
563 for (exp = exportlist[i].p_head; exp; exp = next_exp) {
566 if (exp->m_export.e_flags & NFSEXP_CROSSMOUNT) {
567 static nfs_export *prev = NULL;
568 static void *mnt = NULL;
572 path = next_mnt(&mnt, exp->m_export.e_path);
574 next_exp = exp->m_next;
582 path = exp->m_export.e_path;
586 path = exp->m_export.e_path;
587 next_exp = exp->m_next;
590 if (!use_ipaddr && !client_member(dom, exp->m_client->m_hostname))
592 if (exp->m_export.e_mountpoint &&
593 !is_mountpoint(exp->m_export.e_mountpoint[0]?
594 exp->m_export.e_mountpoint:
595 exp->m_export.e_path))
598 if (!match_fsid(&parsed, exp, path))
600 if (use_ipaddr && !client_check(exp->m_client, ai))
602 if (!found || subexport(&exp->m_export, found)) {
603 found = &exp->m_export;
605 found_path = strdup(path);
606 if (found_path == NULL)
608 } else if (strcmp(found->e_path, exp->m_export.e_path) != 0
609 && !subexport(found, &exp->m_export))
611 xlog(L_WARNING, "%s and %s have same filehandle for %s, using first",
612 found_path, path, dom);
614 /* same path, if one is V4ROOT, choose the other */
615 if (found->e_flags & NFSEXP_V4ROOT) {
616 found = &exp->m_export;
618 found_path = strdup(path);
619 if (found_path == NULL)
626 found->e_mountpoint &&
627 !is_mountpoint(found->e_mountpoint[0]?
630 /* Cannot export this yet
631 * should log a warning, but need to rate limit
632 xlog(L_WARNING, "%s not exported as %d not a mountpoint",
633 found->e_path, found->e_mountpoint);
635 /* FIXME we need to make sure we re-visit this later */
638 if (!found && dev_missing) {
639 /* The missing dev could be what we want, so just be
640 * quite rather than returning stale yet
646 if (cache_export_ent(dom, found, found_path) < 0)
650 qword_printint(f, fsidtype);
651 qword_printhex(f, fsid, fsidlen);
652 /* The fsid -> path lookup can be quite expensive as it
653 * potentially stats and reads lots of devices, and some of those
654 * might have spun-down. The Answer is not likely to
655 * change underneath us, and an 'exportfs -f' can always
656 * remove this from the kernel, so use a really log
657 * timeout. Maybe this should be configurable on the command
660 qword_printint(f, 0x7fffffff);
662 qword_print(f, found_path);
669 xlog(D_CALL, "nfsd_fh: found %p path %s", found, found ? found->e_path : NULL);
673 static void write_fsloc(FILE *f, struct exportent *ep)
675 struct servers *servers;
677 if (ep->e_fslocmethod == FSLOC_NONE)
680 servers = replicas_lookup(ep->e_fslocmethod, ep->e_fslocdata);
683 qword_print(f, "fsloc");
684 qword_printint(f, servers->h_num);
685 if (servers->h_num >= 0) {
687 for (i=0; i<servers->h_num; i++) {
688 qword_print(f, servers->h_mp[i]->h_host);
689 qword_print(f, servers->h_mp[i]->h_path);
692 qword_printint(f, servers->h_referral);
693 release_replicas(servers);
696 static void write_secinfo(FILE *f, struct exportent *ep, int flag_mask)
700 for (p = ep->e_secinfo; p->flav; p++)
702 if (p == ep->e_secinfo) {
703 /* There was no sec= option */
706 qword_print(f, "secinfo");
707 qword_printint(f, p - ep->e_secinfo);
708 for (p = ep->e_secinfo; p->flav; p++) {
709 qword_printint(f, p->flav->fnum);
710 qword_printint(f, p->flags & flag_mask);
715 static int dump_to_cache(FILE *f, char *domain, char *path, struct exportent *exp)
717 qword_print(f, domain);
718 qword_print(f, path);
720 int different_fs = strcmp(path, exp->e_path) != 0;
721 int flag_mask = different_fs ? ~NFSEXP_FSID : ~0;
723 qword_printuint(f, time(0) + exp->e_ttl);
724 qword_printint(f, exp->e_flags & flag_mask);
725 qword_printint(f, exp->e_anonuid);
726 qword_printint(f, exp->e_anongid);
727 qword_printint(f, exp->e_fsid);
729 write_secinfo(f, exp, flag_mask);
730 if (exp->e_uuid == NULL || different_fs) {
732 if (uuid_by_path(path, 0, 16, u)) {
733 qword_print(f, "uuid");
734 qword_printhex(f, u, 16);
738 get_uuid(exp->e_uuid, 16, u);
739 qword_print(f, "uuid");
740 qword_printhex(f, u, 16);
743 qword_printuint(f, time(0) + DEFAULT_TTL);
747 static int is_subdirectory(char *child, char *parent)
749 int l = strlen(parent);
751 return strcmp(child, parent) == 0
752 || (strncmp(child, parent, l) == 0 && child[l] == '/');
755 static int path_matches(nfs_export *exp, char *path)
757 if (exp->m_export.e_flags & NFSEXP_CROSSMOUNT)
758 return is_subdirectory(path, exp->m_export.e_path);
759 return strcmp(path, exp->m_export.e_path) == 0;
763 client_matches(nfs_export *exp, char *dom, struct addrinfo *ai)
766 return client_check(exp->m_client, ai);
767 return client_member(dom, exp->m_client->m_hostname);
771 export_matches(nfs_export *exp, char *dom, char *path, struct addrinfo *ai)
773 return path_matches(exp, path) && client_matches(exp, dom, ai);
777 lookup_export(char *dom, char *path, struct addrinfo *ai)
780 nfs_export *found = NULL;
784 for (i=0 ; i < MCL_MAXTYPES; i++) {
785 for (exp = exportlist[i].p_head; exp; exp = exp->m_next) {
786 if (!export_matches(exp, dom, path, ai))
793 /* Always prefer non-V4ROOT exports */
794 if (exp->m_export.e_flags & NFSEXP_V4ROOT)
796 if (found->m_export.e_flags & NFSEXP_V4ROOT) {
802 /* If one is a CROSSMOUNT, then prefer the longest path */
803 if (((found->m_export.e_flags & NFSEXP_CROSSMOUNT) ||
804 (exp->m_export.e_flags & NFSEXP_CROSSMOUNT)) &&
805 strlen(found->m_export.e_path) !=
806 strlen(exp->m_export.e_path)) {
808 if (strlen(exp->m_export.e_path) >
809 strlen(found->m_export.e_path)) {
815 } else if (found_type == i && found->m_warned == 0) {
816 xlog(L_WARNING, "%s exported to both %s and %s, "
817 "arbitrarily choosing options from first",
818 path, found->m_client->m_hostname, exp->m_client->m_hostname,
827 #ifdef HAVE_NFS_PLUGIN_H
829 #include <nfs-plugin.h>
832 * Walk through a set of FS locations and build a set of export options.
833 * Returns true if all went to plan; otherwise, false.
836 locations_to_options(struct jp_ops *ops, nfs_fsloc_set_t locations,
837 char *options, size_t remaining, int *ttl)
839 char *server, *last_path, *rootpath, *ptr;
849 enum jp_status status;
852 status = ops->jp_get_next_location(locations, &server,
854 if (status == JP_EMPTY)
856 if (status != JP_OK) {
857 xlog(D_GENERAL, "%s: failed to parse location: %s",
858 __func__, ops->jp_error(status));
861 xlog(D_GENERAL, "%s: Location: %s:%s",
862 __func__, server, rootpath);
864 if (last_path && strcmp(rootpath, last_path) == 0) {
865 len = snprintf(ptr, remaining, "+%s", server);
867 xlog(D_GENERAL, "%s: snprintf: %m", __func__);
870 if ((size_t)len >= remaining) {
871 xlog(D_GENERAL, "%s: options buffer overflow", __func__);
874 remaining -= (size_t)len;
877 if (last_path == NULL)
878 len = snprintf(ptr, remaining, "refer=%s@%s",
881 len = snprintf(ptr, remaining, ":%s@%s",
884 xlog(D_GENERAL, "%s: snprintf: %m", __func__);
887 if ((size_t)len >= remaining) {
888 xlog(D_GENERAL, "%s: options buffer overflow",
892 remaining -= (size_t)len;
894 last_path = rootpath;
902 xlog(D_CALL, "%s: options='%s', ttl=%d",
903 __func__, options, *ttl);
913 * Walk through the set of FS locations and build an exportent.
914 * Returns pointer to an exportent if "junction" refers to a junction.
916 * Returned exportent points to static memory.
918 static struct exportent *do_locations_to_export(struct jp_ops *ops,
919 nfs_fsloc_set_t locations, const char *junction,
920 char *options, size_t options_len)
922 struct exportent *exp;
925 if (!locations_to_options(ops, locations, options, options_len, &ttl))
928 exp = mkexportent("*", (char *)junction, options);
930 xlog(L_ERROR, "%s: Failed to construct exportent", __func__);
940 * Convert set of FS locations to an exportent. Returns pointer to
941 * an exportent if "junction" refers to a junction.
943 * Returned exportent points to static memory.
945 static struct exportent *locations_to_export(struct jp_ops *ops,
946 nfs_fsloc_set_t locations, const char *junction)
948 struct exportent *exp;
951 options = malloc(BUFSIZ);
952 if (options == NULL) {
953 xlog(D_GENERAL, "%s: failed to allocate options buffer",
959 exp = do_locations_to_export(ops, locations, junction,
967 * Retrieve locations information in "junction" and dump it to the
968 * kernel. Returns pointer to an exportent if "junction" refers
971 * Returned exportent points to static memory.
973 static struct exportent *invoke_junction_ops(void *handle,
974 const char *junction)
976 nfs_fsloc_set_t locations;
977 struct exportent *exp;
978 enum jp_status status;
982 ops = (struct jp_ops *)dlsym(handle, "nfs_junction_ops");
985 xlog(D_GENERAL, "%s: dlsym(jp_junction_ops): %s",
989 if (ops->jp_api_version != JP_API_VERSION) {
990 xlog(D_GENERAL, "%s: unrecognized junction API version: %u",
991 __func__, ops->jp_api_version);
995 status = ops->jp_init(false);
996 if (status != JP_OK) {
997 xlog(D_GENERAL, "%s: failed to resolve %s: %s",
998 __func__, junction, ops->jp_error(status));
1002 status = ops->jp_get_locations(junction, &locations);
1003 if (status != JP_OK) {
1004 xlog(D_GENERAL, "%s: failed to resolve %s: %s",
1005 __func__, junction, ops->jp_error(status));
1009 exp = locations_to_export(ops, locations, junction);
1011 ops->jp_put_locations(locations);
1017 * Load the junction plug-in, then try to resolve "pathname".
1018 * Returns pointer to an initialized exportent if "junction"
1019 * refers to a junction, or NULL if not.
1021 * Returned exportent points to static memory.
1023 static struct exportent *lookup_junction(const char *pathname)
1025 struct exportent *exp;
1028 handle = dlopen("libnfsjunct.so", RTLD_NOW);
1029 if (handle == NULL) {
1030 xlog(D_GENERAL, "%s: dlopen: %s", __func__, dlerror());
1033 (void)dlerror(); /* Clear any error */
1035 exp = invoke_junction_ops(handle, pathname);
1037 /* We could leave it loaded to make junction resolution
1038 * faster next time. However, if we want to replace the
1039 * library, that would require restarting mountd. */
1040 (void)dlclose(handle);
1043 #else /* !HAVE_NFS_PLUGIN_H */
1044 static inline struct exportent *lookup_junction(const char *UNUSED(pathname))
1048 #endif /* !HAVE_NFS_PLUGIN_H */
1050 static void nfsd_export(FILE *f)
1054 * determine export options and return:
1055 * domain path expiry flags anonuid anongid fsid
1060 nfs_export *found = NULL;
1061 struct addrinfo *ai = NULL;
1063 if (readline(fileno(f), &lbuf, &lbuflen) != 1)
1066 xlog(D_CALL, "nfsd_export: inbuf '%s'", lbuf);
1069 dom = malloc(strlen(cp));
1070 path = malloc(strlen(cp));
1075 if (qword_get(&cp, dom, strlen(lbuf)) <= 0)
1077 if (qword_get(&cp, path, strlen(lbuf)) <= 0)
1083 ai = lookup_client_addr(dom);
1088 found = lookup_export(dom, path, ai);
1091 if (dump_to_cache(f, dom, path, &found->m_export) < 0) {
1093 "Cannot export %s, possibly unsupported filesystem"
1094 " or fsid= required", path);
1095 dump_to_cache(f, dom, path, NULL);
1098 dump_to_cache(f, dom, path, lookup_junction(path));
1101 xlog(D_CALL, "nfsd_export: found %p path %s", found, path ? path : NULL);
1103 if (path) free(path);
1110 void (*cache_handle)(FILE *f);
1112 char vbuf[RPC_CHAN_BUF_SIZE];
1114 { "auth.unix.ip", auth_unix_ip, NULL, ""},
1115 { "auth.unix.gid", auth_unix_gid, NULL, ""},
1116 { "nfsd.export", nfsd_export, NULL, ""},
1117 { "nfsd.fh", nfsd_fh, NULL, ""},
1118 { NULL, NULL, NULL, ""}
1121 extern int manage_gids;
1124 * cache_open - prepare communications channels with kernel RPC caches
1127 void cache_open(void)
1130 for (i=0; cachelist[i].cache_name; i++ ) {
1132 if (!manage_gids && cachelist[i].cache_handle == auth_unix_gid)
1134 sprintf(path, "/proc/net/rpc/%s/channel", cachelist[i].cache_name);
1135 cachelist[i].f = fopen(path, "r+");
1136 if (cachelist[i].f != NULL) {
1137 setvbuf(cachelist[i].f, cachelist[i].vbuf, _IOLBF,
1144 * cache_set_fds - prepare cache file descriptors for one iteration of the service loop
1145 * @fdset: pointer to fd_set to prepare
1147 void cache_set_fds(fd_set *fdset)
1150 for (i=0; cachelist[i].cache_name; i++) {
1152 FD_SET(fileno(cachelist[i].f), fdset);
1157 * cache_process_req - process any active cache file descriptors during service loop iteration
1158 * @fdset: pointer to fd_set to examine for activity
1160 int cache_process_req(fd_set *readfds)
1164 for (i=0; cachelist[i].cache_name; i++) {
1165 if (cachelist[i].f != NULL &&
1166 FD_ISSET(fileno(cachelist[i].f), readfds)) {
1168 cachelist[i].cache_handle(cachelist[i].f);
1169 FD_CLR(fileno(cachelist[i].f), readfds);
1177 * Give IP->domain and domain+path->options to kernel
1178 * % echo nfsd $IP $[now+DEFAULT_TTL] $domain > /proc/net/rpc/auth.unix.ip/channel
1179 * % echo $domain $path $[now+DEFAULT_TTL] $options $anonuid $anongid $fsid > /proc/net/rpc/nfsd.export/channel
1182 static int cache_export_ent(char *domain, struct exportent *exp, char *path)
1185 FILE *f = fopen("/proc/net/rpc/nfsd.export/channel", "w");
1189 err = dump_to_cache(f, domain, exp->e_path, exp);
1192 "Cannot export %s, possibly unsupported filesystem or"
1193 " fsid= required", exp->e_path);
1196 while (err == 0 && (exp->e_flags & NFSEXP_CROSSMOUNT) && path) {
1197 /* really an 'if', but we can break out of
1198 * a 'while' more easily */
1199 /* Look along 'path' for other filesystems
1200 * and export them with the same options
1203 size_t l = strlen(exp->e_path);
1206 if (strlen(path) <= l || path[l] != '/' ||
1207 strncmp(exp->e_path, path, l) != 0)
1209 if (stat(exp->e_path, &stb) != 0)
1212 while(path[l] == '/') {
1214 /* errors for submount should fail whole filesystem */
1218 while (path[l] != '/' && path[l])
1222 err2 = lstat(path, &stb);
1226 if (stb.st_dev == dev)
1230 dump_to_cache(f, domain, path, exp);
1241 * cache_export - Inform kernel of a new nfs_export
1242 * @exp: target nfs_export
1243 * @path: NUL-terminated C string containing export path
1245 int cache_export(nfs_export *exp, char *path)
1247 char buf[INET6_ADDRSTRLEN];
1251 f = fopen("/proc/net/rpc/auth.unix.ip/channel", "w");
1256 qword_print(f, "nfsd");
1258 host_ntop(get_addrlist(exp->m_client, 0), buf, sizeof(buf)));
1259 qword_printuint(f, time(0) + exp->m_export.e_ttl);
1260 qword_print(f, exp->m_client->m_hostname);
1265 err = cache_export_ent(exp->m_client->m_hostname, &exp->m_export, path)
1271 * cache_get_filehandle - given an nfs_export, get its root filehandle
1272 * @exp: target nfs_export
1273 * @len: length of requested file handle
1274 * @p: NUL-terminated C string containing export path
1276 * Returns pointer to NFS file handle of root directory of export
1279 * echo $domain $path $length
1280 * read filehandle <&0
1281 * } <> /proc/fs/nfsd/filehandle
1284 cache_get_filehandle(nfs_export *exp, int len, char *p)
1286 FILE *f = fopen("/proc/fs/nfsd/filehandle", "r+");
1290 static struct nfs_fh_len fh;
1293 f = fopen("/proc/fs/nfs/filehandle", "r+");
1297 qword_print(f, exp->m_client->m_hostname);
1299 qword_printint(f, len);
1300 failed = qword_eol(f);
1303 failed = (fgets(buf, sizeof(buf), f) == NULL);
1307 memset(fh.fh_handle, 0, sizeof(fh.fh_handle));
1308 fh.fh_size = qword_get(&bp, (char *)fh.fh_handle, NFS3_FHSIZE);