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>
18 #include <netinet/in.h>
19 #include <arpa/inet.h>
32 #include "blkid/blkid.h"
47 * Support routines for text-based upcalls.
48 * Fields are separated by spaces.
49 * Fields are either mangled to quote space tab newline slosh with slosh
50 * or a hexified with a leading \x
51 * Record is terminated with newline.
54 int cache_export_ent(char *domain, struct exportent *exp);
60 void auth_unix_ip(FILE *f)
64 * Ignore if class != "nfsd"
65 * Otherwise find domainname and write back:
67 * "nfsd" IP-ADDR expiry domainname
74 if (readline(fileno(f), &lbuf, &lbuflen) != 1)
79 if (qword_get(&cp, class, 20) <= 0 ||
80 strcmp(class, "nfsd") != 0)
83 if (qword_get(&cp, ipaddr, 20) <= 0)
86 if (inet_aton(ipaddr, &addr)==0)
91 /* addr is a valid, interesting address, find the domain name... */
92 client = client_compose(addr);
95 qword_print(f, "nfsd");
96 qword_print(f, ipaddr);
97 qword_printint(f, time(0)+30*60);
99 qword_print(f, *client?client:"DEFAULT");
102 if (client) free(client);
106 void auth_unix_gid(FILE *f)
111 * uid expiry count list of group ids
115 gid_t glist[100], *groups = glist;
120 if (readline(fileno(f), &lbuf, &lbuflen) != 1)
124 if (qword_get_int(&cp, &uid) != 0)
131 rv = getgrouplist(pw->pw_name, pw->pw_gid, groups, &ngroups);
132 if (rv == -1 && ngroups >= 100) {
133 groups = malloc(sizeof(gid_t)*ngroups);
137 rv = getgrouplist(pw->pw_name, pw->pw_gid,
141 qword_printint(f, uid);
142 qword_printint(f, time(0)+30*60);
144 qword_printint(f, ngroups);
145 for (i=0; i<ngroups; i++)
146 qword_printint(f, groups[i]);
153 int get_uuid(char *path, char *uuid, int uuidlen, char *u)
155 /* extract hex digits from uuidstr and compose a uuid
156 * of the given length (max 16), xoring bytes to make
157 * a smaller uuid. Then compare with uuid
163 static blkid_cache cache = NULL;
166 blkid_tag_iterate iter;
170 blkid_get_cache(&cache, NULL);
172 blkid_probe_all_new(cache);
174 if (stat(path, &stb) != 0)
176 devname = blkid_devno_to_devname(stb.st_dev);
179 dev = blkid_get_dev(cache, devname, BLKID_DEV_NORMAL);
183 iter = blkid_tag_iterate_begin(dev);
186 while (blkid_tag_next(iter, &type, &val) == 0)
187 if (strcmp(type, "UUID") == 0)
189 blkid_tag_iterate_end(iter);
196 memset(u, 0, uuidlen);
197 for ( ; *val ; val++) {
219 void nfsd_fh(FILE *f)
222 * domain fsidtype fsid
223 * interpret fsid, find export point and options, and write:
224 * domain fsidtype fsid expiry path
230 unsigned int dev, major=0, minor=0;
231 unsigned int inode=0;
232 unsigned long long inode64;
233 unsigned int fsidnum=0;
235 struct exportent *found = NULL;
242 if (readline(fileno(f), &lbuf, &lbuflen) != 1)
247 dom = malloc(strlen(cp));
250 if (qword_get(&cp, dom, strlen(cp)) <= 0)
252 if (qword_get_int(&cp, &fsidtype) != 0)
254 if (fsidtype < 0 || fsidtype > 7)
255 goto out; /* unknown type */
256 if ((fsidlen = qword_get(&cp, fsid, 32)) <= 0)
259 case FSID_DEV: /* 4 bytes: 2 major, 2 minor, 4 inode */
262 memcpy(&dev, fsid, 4);
263 memcpy(&inode, fsid+4, 4);
264 major = ntohl(dev)>>16;
265 minor = ntohl(dev) & 0xFFFF;
268 case FSID_NUM: /* 4 bytes - fsid */
271 memcpy(&fsidnum, fsid, 4);
274 case FSID_MAJOR_MINOR: /* 12 bytes: 4 major, 4 minor, 4 inode
275 * This format is never actually used but was
276 * an historical accident
280 memcpy(&dev, fsid, 4); major = ntohl(dev);
281 memcpy(&dev, fsid+4, 4); minor = ntohl(dev);
282 memcpy(&inode, fsid+8, 4);
285 case FSID_ENCODE_DEV: /* 8 bytes: 4 byte packed device number, 4 inode */
286 /* This is *host* endian, not net-byte-order, because
287 * no-one outside this host has any business interpreting it
291 memcpy(&dev, fsid, 4);
292 memcpy(&inode, fsid+4, 4);
293 major = (dev & 0xfff00) >> 8;
294 minor = (dev & 0xff) | ((dev >> 12) & 0xfff00);
297 case FSID_UUID4_INUM: /* 4 byte inode number and 4 byte uuid */
300 memcpy(&inode, fsid, 4);
304 case FSID_UUID8: /* 8 byte uuid */
310 case FSID_UUID16: /* 16 byte uuid */
316 case FSID_UUID16_INUM: /* 8 byte inode number and 16 byte uuid */
319 memcpy(&inode64, fsid, 8);
328 /* Now determine export point for this fsid/domain */
329 for (i=0 ; i < MCL_MAXTYPES; i++) {
330 for (exp = exportlist[i]; exp; exp = exp->m_next) {
334 if (!client_member(dom, exp->m_client->m_hostname))
336 if (exp->m_export.e_mountpoint &&
337 !is_mountpoint(exp->m_export.e_mountpoint[0]?
338 exp->m_export.e_mountpoint:
339 exp->m_export.e_path))
341 if (stat(exp->m_export.e_path, &stb) != 0)
345 case FSID_MAJOR_MINOR:
346 case FSID_ENCODE_DEV:
347 if (stb.st_ino != inode)
349 if (major != major(stb.st_dev) ||
350 minor != minor(stb.st_dev))
354 if (((exp->m_export.e_flags & NFSEXP_FSID) == 0 ||
355 exp->m_export.e_fsid != fsidnum))
358 case FSID_UUID4_INUM:
359 case FSID_UUID16_INUM:
360 if (stb.st_ino != inode)
365 if (!is_mountpoint(exp->m_export.e_path))
368 if (exp->m_export.e_uuid)
369 get_uuid(NULL, exp->m_export.e_uuid,
371 else if (get_uuid(exp->m_export.e_path, NULL,
375 if (memcmp(u, fhuuid, uuidlen) != 0)
379 /* It's a match !! */
381 found = &exp->m_export;
382 else if (strcmp(found->e_path, exp->m_export.e_path)!= 0)
384 xlog(L_WARNING, "%s and %s have same filehandle for %s, using first",
385 found->e_path, exp->m_export.e_path, dom);
390 found->e_mountpoint &&
391 !is_mountpoint(found->e_mountpoint[0]?
394 /* Cannot export this yet
395 * should log a warning, but need to rate limit
396 xlog(L_WARNING, "%s not exported as %d not a mountpoint",
397 found->e_path, found->e_mountpoint);
399 /* FIXME we need to make sure we re-visit this later */
402 if (!found && dev_missing) {
403 /* The missing dev could be what we want, so just be
404 * quite rather than returning stale yet
410 cache_export_ent(dom, found);
413 qword_printint(f, fsidtype);
414 qword_printhex(f, fsid, fsidlen);
415 qword_printint(f, time(0)+30*60);
417 qword_print(f, found->e_path);
424 static int dump_to_cache(FILE *f, char *domain, char *path, struct exportent *exp)
426 qword_print(f, domain);
427 qword_print(f, path);
428 qword_printint(f, time(0)+30*60);
430 qword_printint(f, exp->e_flags);
431 qword_printint(f, exp->e_anonuid);
432 qword_printint(f, exp->e_anongid);
433 qword_printint(f, exp->e_fsid);
434 if (exp->e_uuid == NULL) {
436 if (get_uuid(exp->e_path, NULL, 16, u)) {
437 qword_print(f, "uuid");
438 qword_printhex(f, u, 16);
440 } else if (exp->e_uuid) {
441 qword_print(f, "uuid");
442 qword_printhex(f, exp->e_uuid, 16);
448 void nfsd_export(FILE *f)
452 * determine export options and return:
453 * domain path expiry flags anonuid anongid fsid
459 nfs_export *exp, *found = NULL;
462 if (readline(fileno(f), &lbuf, &lbuflen) != 1)
466 dom = malloc(strlen(cp));
467 path = malloc(strlen(cp));
472 if (qword_get(&cp, dom, strlen(lbuf)) <= 0)
474 if (qword_get(&cp, path, strlen(lbuf)) <= 0)
479 /* now find flags for this export point in this domain */
480 for (i=0 ; i < MCL_MAXTYPES; i++) {
481 for (exp = exportlist[i]; exp; exp = exp->m_next) {
482 if (!client_member(dom, exp->m_client->m_hostname))
484 if (strcmp(path, exp->m_export.e_path))
489 xlog(L_WARNING, "%s exported to both %s and %s in %s",
490 path, exp->m_client->m_hostname, found->m_client->m_hostname,
497 dump_to_cache(f, dom, path, &found->m_export);
498 mountlist_add(dom, path);
500 dump_to_cache(f, dom, path, NULL);
504 if (path) free(path);
510 void (*cache_handle)(FILE *f);
513 { "auth.unix.ip", auth_unix_ip},
514 { "auth.unix.gid", auth_unix_gid},
515 { "nfsd.export", nfsd_export},
516 { "nfsd.fh", nfsd_fh},
520 extern int manage_gids;
521 void cache_open(void)
524 for (i=0; cachelist[i].cache_name; i++ ) {
526 if (!manage_gids && cachelist[i].f == auth_unix_gid)
528 sprintf(path, "/proc/net/rpc/%s/channel", cachelist[i].cache_name);
529 cachelist[i].f = fopen(path, "r+");
533 void cache_set_fds(fd_set *fdset)
536 for (i=0; cachelist[i].cache_name; i++) {
538 FD_SET(fileno(cachelist[i].f), fdset);
542 int cache_process_req(fd_set *readfds)
546 for (i=0; cachelist[i].cache_name; i++) {
547 if (cachelist[i].f != NULL &&
548 FD_ISSET(fileno(cachelist[i].f), readfds)) {
550 cachelist[i].cache_handle(cachelist[i].f);
551 FD_CLR(fileno(cachelist[i].f), readfds);
559 * Give IP->domain and domain+path->options to kernel
560 * % echo nfsd $IP $[now+30*60] $domain > /proc/net/rpc/auth.unix.ip/channel
561 * % echo $domain $path $[now+30*60] $options $anonuid $anongid $fsid > /proc/net/rpc/nfsd.export/channel
564 int cache_export_ent(char *domain, struct exportent *exp)
567 FILE *f = fopen("/proc/net/rpc/nfsd.export/channel", "w");
571 err = dump_to_cache(f, domain, exp->e_path, exp);
573 mountlist_add(domain, exp->e_path);
577 int cache_export(nfs_export *exp)
582 if (exp->m_export.e_maptype != CLE_MAP_IDENT) {
583 xlog(L_ERROR, "%s: unsupported mapping; kernel supports only 'identity' (default)",
584 exp->m_export.m_path);
588 f = fopen("/proc/net/rpc/auth.unix.ip/channel", "w");
592 qword_print(f, "nfsd");
593 qword_print(f, inet_ntoa(exp->m_client->m_addrlist[0]));
594 qword_printint(f, time(0)+30*60);
595 qword_print(f, exp->m_client->m_hostname);
600 err = cache_export_ent(exp->m_client->m_hostname, &exp->m_export)
607 * echo $domain $path $length
608 * read filehandle <&0
609 * } <> /proc/fs/nfsd/filehandle
612 cache_get_filehandle(nfs_export *exp, int len, char *p)
614 FILE *f = fopen("/proc/fs/nfsd/filehandle", "r+");
618 static struct nfs_fh_len fh;
621 f = fopen("/proc/fs/nfs/filehandle", "r+");
625 qword_print(f, exp->m_client->m_hostname);
627 qword_printint(f, len);
628 failed = qword_eol(f);
631 failed = (fgets(buf, sizeof(buf), f) == NULL);
635 memset(fh.fh_handle, 0, sizeof(fh.fh_handle));
636 fh.fh_size = qword_get(&bp, (char *)fh.fh_handle, NFS3_FHSIZE);