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>
31 * Support routines for text-based upcalls.
32 * Fields are separated by spaces.
33 * Fields are either mangled to quote space tab newline slosh with slosh
34 * or a hexified with a leading \x
35 * Record is terminated with newline.
38 int cache_export_ent(char *domain, struct exportent *exp);
44 void auth_unix_ip(FILE *f)
48 * Ignore if class != "nfsd"
49 * Otherwise find domainname and write back:
51 * "nfsd" IP-ADDR expiry domainname
58 if (readline(fileno(f), &lbuf, &lbuflen) != 1)
63 if (qword_get(&cp, class, 20) <= 0 ||
64 strcmp(class, "nfsd") != 0)
67 if (qword_get(&cp, ipaddr, 20) <= 0)
70 if (inet_aton(ipaddr, &addr)==0)
75 /* addr is a valid, interesting address, find the domain name... */
76 client = client_compose(addr);
79 qword_print(f, "nfsd");
80 qword_print(f, ipaddr);
81 qword_printint(f, time(0)+30*60);
83 qword_print(f, *client?client:"DEFAULT");
86 if (client) free(client);
93 * domain fsidtype fsid
94 * interpret fsid, find export point and options, and write:
95 * domain fsidtype fsid expiry path
101 unsigned int dev, major=0, minor=0;
102 unsigned int inode=0;
103 unsigned int fsidnum=0;
105 struct exportent *found = NULL;
110 if (readline(fileno(f), &lbuf, &lbuflen) != 1)
115 dom = malloc(strlen(cp));
118 if (qword_get(&cp, dom, strlen(cp)) <= 0)
120 if (qword_get_int(&cp, &fsidtype) != 0)
122 if (fsidtype < 0 || fsidtype > 3)
123 goto out; /* unknown type */
124 if ((fsidlen = qword_get(&cp, fsid, 32)) <= 0)
127 case 0: /* 4 bytes: 2 major, 2 minor, 4 inode */
130 memcpy(&dev, fsid, 4);
131 memcpy(&inode, fsid+4, 4);
132 major = ntohl(dev)>>16;
133 minor = ntohl(dev) & 0xFFFF;
136 case 1: /* 4 bytes - fsid */
139 memcpy(&fsidnum, fsid, 4);
142 case 2: /* 12 bytes: 4 major, 4 minor, 4 inode
143 * This format is never actually used but was
144 * an historical accident
148 memcpy(&dev, fsid, 4); major = ntohl(dev);
149 memcpy(&dev, fsid+4, 4); minor = ntohl(dev);
150 memcpy(&inode, fsid+8, 4);
153 case 3: /* 8 bytes: 4 byte packed device number, 4 inode */
154 /* This is *host* endian, not net-byte-order, because
155 * no-one outside this host has any business interpreting it
159 memcpy(&dev, fsid, 4);
160 memcpy(&inode, fsid+4, 4);
161 major = (dev & 0xfff00) >> 8;
162 minor = (dev & 0xff) | ((dev >> 12) & 0xfff00);
169 /* Now determine export point for this fsid/domain */
170 for (i=0 ; i < MCL_MAXTYPES; i++) {
171 for (exp = exportlist[i]; exp; exp = exp->m_next) {
174 if (!client_member(dom, exp->m_client->m_hostname))
176 if (exp->m_export.e_mountpoint &&
177 !is_mountpoint(exp->m_export.e_mountpoint[0]?
178 exp->m_export.e_mountpoint:
179 exp->m_export.e_path))
181 if (stat(exp->m_export.e_path, &stb) != 0)
184 ((exp->m_export.e_flags & NFSEXP_FSID) == 0 ||
185 exp->m_export.e_fsid != fsidnum))
188 if (stb.st_ino != inode)
190 if (major != major(stb.st_dev) ||
191 minor != minor(stb.st_dev))
194 /* It's a match !! */
196 found = &exp->m_export;
197 else if (strcmp(found->e_path, exp->m_export.e_path)!= 0)
199 xlog(L_WARNING, "%s and %s have same filehandle for %s, using first",
200 found->e_path, exp->m_export.e_path, dom);
205 found->e_mountpoint &&
206 !is_mountpoint(found->e_mountpoint[0]?
209 /* Cannot export this yet
210 * should log a warning, but need to rate limit
211 xlog(L_WARNING, "%s not exported as %d not a mountpoint",
212 found->e_path, found->e_mountpoint);
214 /* FIXME we need to make sure we re-visit this later */
217 if (!found && dev_missing) {
218 /* The missing dev could be what we want, so just be
219 * quite rather than returning stale yet
225 cache_export_ent(dom, found);
228 qword_printint(f, fsidtype);
229 qword_printhex(f, fsid, fsidlen);
230 qword_printint(f, time(0)+30*60);
232 qword_print(f, found->e_path);
239 void nfsd_export(FILE *f)
243 * determine export options and return:
244 * domain path expiry flags anonuid anongid fsid
250 nfs_export *exp, *found = NULL;
253 if (readline(fileno(f), &lbuf, &lbuflen) != 1)
257 dom = malloc(strlen(cp));
258 path = malloc(strlen(cp));
263 if (qword_get(&cp, dom, strlen(lbuf)) <= 0)
265 if (qword_get(&cp, path, strlen(lbuf)) <= 0)
270 /* now find flags for this export point in this domain */
271 for (i=0 ; i < MCL_MAXTYPES; i++) {
272 for (exp = exportlist[i]; exp; exp = exp->m_next) {
273 if (!client_member(dom, exp->m_client->m_hostname))
275 if (strcmp(path, exp->m_export.e_path))
280 xlog(L_WARNING, "%s exported to both %s and %s in %s",
281 path, exp->m_client->m_hostname, found->m_client->m_hostname,
288 qword_print(f, path);
289 qword_printint(f, time(0)+30*60);
291 qword_printint(f, found->m_export.e_flags);
292 qword_printint(f, found->m_export.e_anonuid);
293 qword_printint(f, found->m_export.e_anongid);
294 qword_printint(f, found->m_export.e_fsid);
299 if (path) free(path);
305 void (*cache_handle)(FILE *f);
308 { "auth.unix.ip", auth_unix_ip},
309 { "nfsd.export", nfsd_export},
310 { "nfsd.fh", nfsd_fh},
314 void cache_open(void)
317 for (i=0; cachelist[i].cache_name; i++ ){
319 sprintf(path, "/proc/net/rpc/%s/channel", cachelist[i].cache_name);
320 cachelist[i].f = fopen(path, "r+");
324 void cache_set_fds(fd_set *fdset)
327 for (i=0; cachelist[i].cache_name; i++) {
329 FD_SET(fileno(cachelist[i].f), fdset);
333 int cache_process_req(fd_set *readfds)
337 for (i=0; cachelist[i].cache_name; i++) {
338 if (cachelist[i].f != NULL &&
339 FD_ISSET(fileno(cachelist[i].f), readfds)) {
341 cachelist[i].cache_handle(cachelist[i].f);
342 FD_CLR(fileno(cachelist[i].f), readfds);
350 * Give IP->domain and domain+path->options to kernel
351 * % echo nfsd $IP $[now+30*60] $domain > /proc/net/rpc/auth.unix.ip/channel
352 * % echo $domain $path $[now+30*60] $options $anonuid $anongid $fsid > /proc/net/rpc/nfsd.export/channel
355 int cache_export_ent(char *domain, struct exportent *exp)
358 FILE *f = fopen("/proc/net/rpc/nfsd.export/channel", "w");
362 qword_print(f, domain);
363 qword_print(f, exp->e_path);
364 qword_printint(f, time(0)+30*60);
365 qword_printint(f, exp->e_flags);
366 qword_printint(f, exp->e_anonuid);
367 qword_printint(f, exp->e_anongid);
368 qword_printint(f, exp->e_fsid);
375 int cache_export(nfs_export *exp)
380 if (exp->m_export.e_maptype != CLE_MAP_IDENT) {
381 xlog(L_ERROR, "%s: unsupported mapping; kernel supports only 'identity' (default)",
382 exp->m_export.m_path);
386 f = fopen("/proc/net/rpc/auth.unix.ip/channel", "w");
390 qword_print(f, "nfsd");
391 qword_print(f, inet_ntoa(exp->m_client->m_addrlist[0]));
392 qword_printint(f, time(0)+30*60);
393 qword_print(f, exp->m_client->m_hostname);
398 err = cache_export_ent(exp->m_client->m_hostname, &exp->m_export)
405 * echo $domain $path $length
406 * read filehandle <&0
407 * } <> /proc/fs/nfsd/filehandle
410 cache_get_filehandle(nfs_export *exp, int len, char *p)
412 FILE *f = fopen("/proc/fs/nfsd/filehandle", "r+");
416 static struct nfs_fh_len fh;
419 f = fopen("/proc/fs/nfs/filehandle", "r+");
423 qword_print(f, exp->m_client->m_hostname);
425 qword_printint(f, len);
426 failed = qword_eol(f);
429 failed = (fgets(buf, sizeof(buf), f) == NULL);
433 memset(fh.fh_handle, 0, sizeof(fh.fh_handle));
434 fh.fh_size = qword_get(&bp, (char *)fh.fh_handle, NFS3_FHSIZE);