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"
53 * Support routines for text-based upcalls.
54 * Fields are separated by spaces.
55 * Fields are either mangled to quote space tab newline slosh with slosh
56 * or a hexified with a leading \x
57 * Record is terminated with newline.
60 int cache_export_ent(char *domain, struct exportent *exp, char *p);
65 extern int use_ipaddr;
67 void auth_unix_ip(FILE *f)
71 * Ignore if class != "nfsd"
72 * Otherwise find domainname and write back:
74 * "nfsd" IP-ADDR expiry domainname
81 struct hostent *he = NULL;
82 if (readline(fileno(f), &lbuf, &lbuflen) != 1)
85 xlog(D_CALL, "auth_unix_ip: inbuf '%s'", lbuf);
89 if (qword_get(&cp, class, 20) <= 0 ||
90 strcmp(class, "nfsd") != 0)
93 if (qword_get(&cp, ipaddr, 20) <= 0)
96 if (inet_aton(ipaddr, &addr)==0)
101 /* addr is a valid, interesting address, find the domain name... */
103 he = client_resolve(addr);
104 client = client_compose(he);
107 qword_print(f, "nfsd");
108 qword_print(f, ipaddr);
109 qword_printint(f, time(0)+30*60);
111 qword_print(f, ipaddr);
113 qword_print(f, *client?client:"DEFAULT");
115 xlog(D_CALL, "auth_unix_ip: client %p '%s'", client, client?client: "DEFAULT");
117 if (client) free(client);
121 void auth_unix_gid(FILE *f)
126 * uid expiry count list of group ids
130 gid_t glist[100], *groups = glist;
135 if (readline(fileno(f), &lbuf, &lbuflen) != 1)
139 if (qword_get_uint(&cp, &uid) != 0)
146 rv = getgrouplist(pw->pw_name, pw->pw_gid, groups, &ngroups);
147 if (rv == -1 && ngroups >= 100) {
148 groups = malloc(sizeof(gid_t)*ngroups);
152 rv = getgrouplist(pw->pw_name, pw->pw_gid,
156 qword_printuint(f, uid);
157 qword_printuint(f, time(0)+30*60);
159 qword_printuint(f, ngroups);
160 for (i=0; i<ngroups; i++)
161 qword_printuint(f, groups[i]);
163 qword_printuint(f, 0);
171 static const char *get_uuid_blkdev(char *path)
173 /* We set *safe if we know that we need the
174 * fsid from statfs too.
176 static blkid_cache cache = NULL;
179 blkid_tag_iterate iter;
182 const char *val, *uuid = NULL;
185 blkid_get_cache(&cache, NULL);
187 if (stat(path, &stb) != 0)
189 devname = blkid_devno_to_devname(stb.st_dev);
192 dev = blkid_get_dev(cache, devname, BLKID_DEV_NORMAL);
196 iter = blkid_tag_iterate_begin(dev);
199 while (blkid_tag_next(iter, &type, &val) == 0) {
200 if (strcmp(type, "UUID") == 0)
202 if (strcmp(type, "TYPE") == 0 &&
203 strcmp(val, "btrfs") == 0) {
208 blkid_tag_iterate_end(iter);
212 #define get_uuid_blkdev(path) (NULL)
215 int get_uuid(const char *val, int uuidlen, char *u)
217 /* extract hex digits from uuidstr and compose a uuid
218 * of the given length (max 16), xoring bytes to make
223 memset(u, 0, uuidlen);
224 for ( ; *val ; val++) {
245 int uuid_by_path(char *path, int type, int uuidlen, char *uuid)
247 /* get a uuid for the filesystem found at 'path'.
248 * There are several possible ways of generating the
250 * Type 0 is used for new filehandles, while other types
251 * may be used to interpret old filehandle - to ensure smooth
253 * We return 1 if a uuid was found (and it might be worth
254 * trying the next type) or 0 if no more uuid types can be
258 /* Possible sources of uuid are
262 * On some filesystems (e.g. vfat) the statfs64 uuid is simply an
263 * encoding of the device that the filesystem is mounted from, so
264 * it we be very bad to use that (as device numbers change). blkid
266 * On other filesystems (e.g. btrfs) the statfs64 uuid contains
267 * important info that the blkid uuid cannot contain: This happens
268 * when multiple subvolumes are exported (they have the same
269 * blkid uuid but different statfs64 uuids).
270 * We rely on get_uuid_blkdev *knowing* which is which and not returning
271 * a uuid for filesystems where the statfs64 uuid is better.
276 const char *blkid_val;
279 blkid_val = get_uuid_blkdev(path);
281 if (statfs64(path, &st) == 0 &&
282 (st.f_fsid.__val[0] || st.f_fsid.__val[1]))
283 snprintf(fsid_val, 17, "%08x%08x",
284 st.f_fsid.__val[0], st.f_fsid.__val[1]);
288 if (blkid_val && (type--) == 0)
290 else if (fsid_val[0] && (type--) == 0)
295 get_uuid(val, uuidlen, uuid);
299 /* Iterate through /etc/mtab, finding mountpoints
300 * at or below a given path
302 static char *next_mnt(void **v, char *p)
308 f = setmntent("/etc/mtab", "r");
312 while ((me = getmntent(f)) != NULL &&
313 (strncmp(me->mnt_dir, p, l) != 0 ||
314 me->mnt_dir[l] != '/'))
324 void nfsd_fh(FILE *f)
327 * domain fsidtype fsid
328 * interpret fsid, find export point and options, and write:
329 * domain fsidtype fsid expiry path
335 unsigned int dev, major=0, minor=0;
336 unsigned int inode=0;
337 unsigned long long inode64;
338 unsigned int fsidnum=0;
340 struct exportent *found = NULL;
341 struct hostent *he = NULL;
343 char *found_path = NULL;
350 if (readline(fileno(f), &lbuf, &lbuflen) != 1)
353 xlog(D_CALL, "nfsd_fh: inbuf '%s'", lbuf);
357 dom = malloc(strlen(cp));
360 if (qword_get(&cp, dom, strlen(cp)) <= 0)
362 if (qword_get_int(&cp, &fsidtype) != 0)
364 if (fsidtype < 0 || fsidtype > 7)
365 goto out; /* unknown type */
366 if ((fsidlen = qword_get(&cp, fsid, 32)) <= 0)
369 case FSID_DEV: /* 4 bytes: 2 major, 2 minor, 4 inode */
372 memcpy(&dev, fsid, 4);
373 memcpy(&inode, fsid+4, 4);
374 major = ntohl(dev)>>16;
375 minor = ntohl(dev) & 0xFFFF;
378 case FSID_NUM: /* 4 bytes - fsid */
381 memcpy(&fsidnum, fsid, 4);
384 case FSID_MAJOR_MINOR: /* 12 bytes: 4 major, 4 minor, 4 inode
385 * This format is never actually used but was
386 * an historical accident
390 memcpy(&dev, fsid, 4); major = ntohl(dev);
391 memcpy(&dev, fsid+4, 4); minor = ntohl(dev);
392 memcpy(&inode, fsid+8, 4);
395 case FSID_ENCODE_DEV: /* 8 bytes: 4 byte packed device number, 4 inode */
396 /* This is *host* endian, not net-byte-order, because
397 * no-one outside this host has any business interpreting it
401 memcpy(&dev, fsid, 4);
402 memcpy(&inode, fsid+4, 4);
403 major = (dev & 0xfff00) >> 8;
404 minor = (dev & 0xff) | ((dev >> 12) & 0xfff00);
407 case FSID_UUID4_INUM: /* 4 byte inode number and 4 byte uuid */
410 memcpy(&inode, fsid, 4);
414 case FSID_UUID8: /* 8 byte uuid */
420 case FSID_UUID16: /* 16 byte uuid */
426 case FSID_UUID16_INUM: /* 8 byte inode number and 16 byte uuid */
429 memcpy(&inode64, fsid, 8);
438 /* Now determine export point for this fsid/domain */
439 for (i=0 ; i < MCL_MAXTYPES; i++) {
440 nfs_export *next_exp;
441 for (exp = exportlist[i].p_head; exp; exp = next_exp) {
447 if (exp->m_export.e_flags & NFSEXP_CROSSMOUNT) {
448 static nfs_export *prev = NULL;
449 static void *mnt = NULL;
453 path = next_mnt(&mnt, exp->m_export.e_path);
455 next_exp = exp->m_next;
463 path = exp->m_export.e_path;
467 path = exp->m_export.e_path;
468 next_exp = exp->m_next;
471 if (!use_ipaddr && !client_member(dom, exp->m_client->m_hostname))
473 if (exp->m_export.e_mountpoint &&
474 !is_mountpoint(exp->m_export.e_mountpoint[0]?
475 exp->m_export.e_mountpoint:
476 exp->m_export.e_path))
478 if (stat(path, &stb) != 0)
480 if (!S_ISDIR(stb.st_mode) && !S_ISREG(stb.st_mode)) {
485 case FSID_MAJOR_MINOR:
486 case FSID_ENCODE_DEV:
487 if (stb.st_ino != inode)
489 if (major != major(stb.st_dev) ||
490 minor != minor(stb.st_dev))
494 if (((exp->m_export.e_flags & NFSEXP_FSID) == 0 ||
495 exp->m_export.e_fsid != fsidnum))
498 case FSID_UUID4_INUM:
499 case FSID_UUID16_INUM:
500 if (stb.st_ino != inode)
505 if (!is_mountpoint(path))
508 if (exp->m_export.e_uuid)
509 get_uuid(exp->m_export.e_uuid,
513 uuid_by_path(path, type, uuidlen, u);
515 if (memcmp(u, fhuuid, uuidlen) != 0)
518 if (memcmp(u, fhuuid, uuidlen) != 0)
524 if (!inet_aton(dom, &addr))
526 he = client_resolve(addr);
528 if (!client_check(exp->m_client, he))
531 /* It's a match !! */
533 found = &exp->m_export;
534 found_path = strdup(path);
535 if (found_path == NULL)
537 } else if (strcmp(found->e_path, exp->m_export.e_path)!= 0)
539 xlog(L_WARNING, "%s and %s have same filehandle for %s, using first",
540 found_path, path, dom);
545 found->e_mountpoint &&
546 !is_mountpoint(found->e_mountpoint[0]?
549 /* Cannot export this yet
550 * should log a warning, but need to rate limit
551 xlog(L_WARNING, "%s not exported as %d not a mountpoint",
552 found->e_path, found->e_mountpoint);
554 /* FIXME we need to make sure we re-visit this later */
557 if (!found && dev_missing) {
558 /* The missing dev could be what we want, so just be
559 * quite rather than returning stale yet
565 if (cache_export_ent(dom, found, found_path) < 0)
569 qword_printint(f, fsidtype);
570 qword_printhex(f, fsid, fsidlen);
571 /* The fsid -> path lookup can be quite expensive as it
572 * potentially stats and reads lots of devices, and some of those
573 * might have spun-down. The Answer is not likely to
574 * change underneath us, and an 'exportfs -f' can always
575 * remove this from the kernel, so use a really log
576 * timeout. Maybe this should be configurable on the command
579 qword_printint(f, 0x7fffffff);
581 qword_print(f, found_path);
589 xlog(D_CALL, "nfsd_fh: found %p path %s", found, found ? found->e_path : NULL);
593 static void write_fsloc(FILE *f, struct exportent *ep, char *path)
595 struct servers *servers;
597 if (ep->e_fslocmethod == FSLOC_NONE)
600 servers = replicas_lookup(ep->e_fslocmethod, ep->e_fslocdata, path);
603 qword_print(f, "fsloc");
604 qword_printint(f, servers->h_num);
605 if (servers->h_num >= 0) {
607 for (i=0; i<servers->h_num; i++) {
608 qword_print(f, servers->h_mp[i]->h_host);
609 qword_print(f, servers->h_mp[i]->h_path);
612 qword_printint(f, servers->h_referral);
613 release_replicas(servers);
616 static void write_secinfo(FILE *f, struct exportent *ep, int flag_mask)
620 for (p = ep->e_secinfo; p->flav; p++)
622 if (p == ep->e_secinfo) {
623 /* There was no sec= option */
626 qword_print(f, "secinfo");
627 qword_printint(f, p - ep->e_secinfo);
628 for (p = ep->e_secinfo; p->flav; p++) {
629 qword_printint(f, p->flav->fnum);
630 qword_printint(f, p->flags & flag_mask);
635 static int dump_to_cache(FILE *f, char *domain, char *path, struct exportent *exp)
637 qword_print(f, domain);
638 qword_print(f, path);
639 qword_printint(f, time(0)+30*60);
641 int different_fs = strcmp(path, exp->e_path) != 0;
642 int flag_mask = different_fs ? ~NFSEXP_FSID : ~0;
644 qword_printint(f, exp->e_flags & flag_mask);
645 qword_printint(f, exp->e_anonuid);
646 qword_printint(f, exp->e_anongid);
647 qword_printint(f, exp->e_fsid);
648 write_fsloc(f, exp, path);
649 write_secinfo(f, exp, flag_mask);
650 if (exp->e_uuid == NULL || different_fs) {
652 if (uuid_by_path(path, 0, 16, u)) {
653 qword_print(f, "uuid");
654 qword_printhex(f, u, 16);
658 get_uuid(exp->e_uuid, 16, u);
659 qword_print(f, "uuid");
660 qword_printhex(f, u, 16);
666 static int is_subdirectory(char *child, char *parent)
668 int l = strlen(parent);
670 return strcmp(child, parent) == 0
671 || (strncmp(child, parent, l) == 0 && child[l] == '/');
674 static int path_matches(nfs_export *exp, char *path)
676 if (exp->m_export.e_flags & NFSEXP_CROSSMOUNT)
677 return is_subdirectory(path, exp->m_export.e_path);
678 return strcmp(path, exp->m_export.e_path) == 0;
681 static int client_matches(nfs_export *exp, char *dom, struct hostent *he)
684 return client_check(exp->m_client, he);
685 return client_member(dom, exp->m_client->m_hostname);
688 static int export_matches(nfs_export *exp, char *dom, char *path, struct hostent *he)
690 return path_matches(exp, path) && client_matches(exp, dom, he);
693 static nfs_export *lookup_export(char *dom, char *path, struct hostent *he)
696 nfs_export *found = NULL;
700 for (i=0 ; i < MCL_MAXTYPES; i++) {
701 for (exp = exportlist[i].p_head; exp; exp = exp->m_next) {
702 if (!export_matches(exp, dom, path, he))
710 /* Always prefer non-V4ROOT mounts */
711 if (found->m_export.e_flags & NFSEXP_V4ROOT)
714 /* If one is a CROSSMOUNT, then prefer the longest path */
715 if (((found->m_export.e_flags & NFSEXP_CROSSMOUNT) ||
716 (exp->m_export.e_flags & NFSEXP_CROSSMOUNT)) &&
717 strlen(found->m_export.e_path) !=
718 strlen(exp->m_export.e_path)) {
720 if (strlen(exp->m_export.e_path) >
721 strlen(found->m_export.e_path)) {
727 } else if (found_type == i && found->m_warned == 0) {
728 xlog(L_WARNING, "%s exported to both %s and %s, "
729 "arbitrarily choosing options from first",
730 path, found->m_client->m_hostname, exp->m_client->m_hostname,
739 void nfsd_export(FILE *f)
743 * determine export options and return:
744 * domain path expiry flags anonuid anongid fsid
749 nfs_export *found = NULL;
751 struct hostent *he = NULL;
754 if (readline(fileno(f), &lbuf, &lbuflen) != 1)
757 xlog(D_CALL, "nfsd_export: inbuf '%s'", lbuf);
760 dom = malloc(strlen(cp));
761 path = malloc(strlen(cp));
766 if (qword_get(&cp, dom, strlen(lbuf)) <= 0)
768 if (qword_get(&cp, path, strlen(lbuf)) <= 0)
774 if (!inet_aton(dom, &addr))
776 he = client_resolve(addr);
779 found = lookup_export(dom, path, he);
782 if (dump_to_cache(f, dom, path, &found->m_export) < 0) {
784 "Cannot export %s, possibly unsupported filesystem"
785 " or fsid= required", path);
786 dump_to_cache(f, dom, path, NULL);
789 dump_to_cache(f, dom, path, NULL);
792 xlog(D_CALL, "nfsd_export: found %p path %s", found, path ? path : NULL);
794 if (path) free(path);
801 void (*cache_handle)(FILE *f);
804 { "auth.unix.ip", auth_unix_ip},
805 { "auth.unix.gid", auth_unix_gid},
806 { "nfsd.export", nfsd_export},
807 { "nfsd.fh", nfsd_fh},
811 extern int manage_gids;
812 void cache_open(void)
815 for (i=0; cachelist[i].cache_name; i++ ) {
817 if (!manage_gids && cachelist[i].cache_handle == auth_unix_gid)
819 sprintf(path, "/proc/net/rpc/%s/channel", cachelist[i].cache_name);
820 cachelist[i].f = fopen(path, "r+");
824 void cache_set_fds(fd_set *fdset)
827 for (i=0; cachelist[i].cache_name; i++) {
829 FD_SET(fileno(cachelist[i].f), fdset);
833 int cache_process_req(fd_set *readfds)
837 for (i=0; cachelist[i].cache_name; i++) {
838 if (cachelist[i].f != NULL &&
839 FD_ISSET(fileno(cachelist[i].f), readfds)) {
841 cachelist[i].cache_handle(cachelist[i].f);
842 FD_CLR(fileno(cachelist[i].f), readfds);
850 * Give IP->domain and domain+path->options to kernel
851 * % echo nfsd $IP $[now+30*60] $domain > /proc/net/rpc/auth.unix.ip/channel
852 * % echo $domain $path $[now+30*60] $options $anonuid $anongid $fsid > /proc/net/rpc/nfsd.export/channel
855 int cache_export_ent(char *domain, struct exportent *exp, char *path)
858 FILE *f = fopen("/proc/net/rpc/nfsd.export/channel", "w");
862 err = dump_to_cache(f, domain, exp->e_path, exp);
865 "Cannot export %s, possibly unsupported filesystem or"
866 " fsid= required", exp->e_path);
869 while (err == 0 && (exp->e_flags & NFSEXP_CROSSMOUNT) && path) {
870 /* really an 'if', but we can break out of
871 * a 'while' more easily */
872 /* Look along 'path' for other filesystems
873 * and export them with the same options
876 int l = strlen(exp->e_path);
879 if (strlen(path) <= l || path[l] != '/' ||
880 strncmp(exp->e_path, path, l) != 0)
882 if (stat(exp->e_path, &stb) != 0)
885 while(path[l] == '/') {
887 /* errors for submount should fail whole filesystem */
891 while (path[l] != '/' && path[l])
895 err2 = lstat(path, &stb);
899 if (stb.st_dev == dev)
903 dump_to_cache(f, domain, path, exp);
913 int cache_export(nfs_export *exp, char *path)
915 struct sockaddr_in *sin = get_addrlist_in(exp->m_client, 0);
919 f = fopen("/proc/net/rpc/auth.unix.ip/channel", "w");
923 qword_print(f, "nfsd");
924 qword_print(f, inet_ntoa(sin->sin_addr));
925 qword_printint(f, time(0)+30*60);
926 qword_print(f, exp->m_client->m_hostname);
931 err = cache_export_ent(exp->m_client->m_hostname, &exp->m_export, path)
938 * echo $domain $path $length
939 * read filehandle <&0
940 * } <> /proc/fs/nfsd/filehandle
943 cache_get_filehandle(nfs_export *exp, int len, char *p)
945 FILE *f = fopen("/proc/fs/nfsd/filehandle", "r+");
949 static struct nfs_fh_len fh;
952 f = fopen("/proc/fs/nfs/filehandle", "r+");
956 qword_print(f, exp->m_client->m_hostname);
958 qword_printint(f, len);
959 failed = qword_eol(f);
962 failed = (fgets(buf, sizeof(buf), f) == NULL);
966 memset(fh.fh_handle, 0, sizeof(fh.fh_handle));
967 fh.fh_size = qword_get(&bp, (char *)fh.fh_handle, NFS3_FHSIZE);