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)
11 #include <sys/types.h>
12 #include <sys/select.h>
15 #include <netinet/in.h>
16 #include <arpa/inet.h>
28 * Support routines for text-based upcalls.
29 * Fields are separated by spaces.
30 * Fields are either mangled to quote space tab newline slosh with slosh
31 * or a hexified with a leading \x
32 * Record is terminated with newline.
35 void cache_export_ent(char *domain, struct exportent *exp);
41 void auth_unix_ip(FILE *f)
45 * Ignore if class != "nfsd"
46 * Otherwise find domainname and write back:
48 * "nfsd" IP-ADDR expiry domainname
55 if (readline(fileno(f), &lbuf, &lbuflen) != 1)
60 if (qword_get(&cp, class, 20) <= 0 ||
61 strcmp(class, "nfsd") != 0)
64 if (qword_get(&cp, ipaddr, 20) <= 0)
67 if (inet_aton(ipaddr, &addr)==0)
70 /* addr is a valid, interesting address, find the domain name... */
71 client = client_compose(addr);
74 qword_print(f, "nfsd");
75 qword_print(f, ipaddr);
76 qword_printint(f, time(0)+30*60);
78 qword_print(f, *client?client:"DEFAULT");
81 if (client && strcmp(ipaddr, client))
82 mountlist_add(ipaddr, *client?client:"DEFAULT");
84 if (client) free(client);
91 * domain fsidtype fsid
92 * interpret fsid, find export point and options, and write:
93 * domain fsidtype fsid expiry path
99 unsigned int dev, major=0, minor=0;
100 unsigned int inode=0;
101 unsigned int fsidnum=0;
103 struct exportent *found = NULL;
108 if (readline(fileno(f), &lbuf, &lbuflen) != 1)
113 dom = malloc(strlen(cp));
116 if (qword_get(&cp, dom, strlen(cp)) <= 0)
118 if (qword_get_int(&cp, &fsidtype) != 0)
120 if (fsidtype < 0 || fsidtype > 1)
121 goto out; /* unknown type */
122 if ((fsidlen = qword_get(&cp, fsid, 32)) <= 0)
125 case 0: /* 4 bytes: 2 major, 2 minor, 4 inode */
128 memcpy(&dev, fsid, 4);
129 memcpy(&inode, fsid+4, 4);
130 major = ntohl(dev)>>16;
131 minor = ntohl(dev) & 0xFFFF;
134 case 1: /* 4 bytes - fsid */
137 memcpy(&fsidnum, fsid, 4);
141 /* Now determine export point for this fsid/domain */
142 for (i=0 ; i < MCL_MAXTYPES; i++) {
143 for (exp = exportlist[i]; exp; exp = exp->m_next) {
144 if (!client_member(dom, exp->m_client->m_hostname))
147 ((exp->m_export.e_flags & NFSEXP_FSID) == 0 ||
148 exp->m_export.e_fsid != fsidnum))
152 if (exp->m_export.e_mountpoint &&
153 !is_mountpoint(exp->m_export.e_mountpoint[0]?
154 exp->m_export.e_mountpoint:
155 exp->m_export.e_path))
157 if (stat(exp->m_export.e_path, &stb) != 0)
159 if (stb.st_ino != inode)
161 if (major != major(stb.st_dev) ||
162 minor != minor(stb.st_dev))
165 /* It's a match !! */
167 found = &exp->m_export;
168 else if (strcmp(found->e_path, exp->m_export.e_path)!= 0)
170 xlog(L_WARNING, "%s and %s have same filehandle for %s, using first",
171 found->e_path, exp->m_export.e_path, dom);
176 found->e_mountpoint &&
177 !is_mountpoint(found->e_mountpoint[0]?
180 /* Cannot export this yet
181 * should log a warning, but need to rate limit
182 xlog(L_WARNING, "%s not exported as %d not a mountpoint",
183 found->e_path, found->e_mountpoint);
185 /* FIXME we need to make sure we re-visit this later */
188 if (!found && dev_missing) {
189 /* The missing dev could be what we want, so just be
190 * quite rather than returning stale yet
196 cache_export_ent(dom, found);
199 qword_printint(f, fsidtype);
200 qword_printhex(f, fsid, fsidlen);
201 qword_printint(f, time(0)+30*60);
203 qword_print(f, found->e_path);
210 void nfsd_export(FILE *f)
214 * determine export options and return:
215 * domain path expiry flags anonuid anongid fsid
221 nfs_export *exp, *found = NULL;
224 if (readline(fileno(f), &lbuf, &lbuflen) != 1)
228 dom = malloc(strlen(cp));
229 path = malloc(strlen(cp));
234 if (qword_get(&cp, dom, strlen(lbuf)) <= 0)
236 if (qword_get(&cp, path, strlen(lbuf)) <= 0)
239 /* now find flags for this export point in this domain */
240 for (i=0 ; i < MCL_MAXTYPES; i++) {
241 for (exp = exportlist[i]; exp; exp = exp->m_next) {
242 if (!client_member(dom, exp->m_client->m_hostname))
244 if (strcmp(path, exp->m_export.e_path))
249 xlog(L_WARNING, "%s exported to both %s and %s in %s",
250 path, exp->m_client->m_hostname, found->m_client->m_hostname,
257 qword_print(f, path);
258 qword_printint(f, time(0)+30*60);
260 qword_printint(f, found->m_export.e_flags);
261 qword_printint(f, found->m_export.e_anonuid);
262 qword_printint(f, found->m_export.e_anongid);
263 qword_printint(f, found->m_export.e_fsid);
264 mountlist_add(dom, path);
269 if (path) free(path);
275 void (*cache_handle)(FILE *f);
278 { "auth.unix.ip", auth_unix_ip},
279 { "nfsd.export", nfsd_export},
280 { "nfsd.fh", nfsd_fh},
284 void cache_open(void)
287 for (i=0; cachelist[i].cache_name; i++ ){
289 sprintf(path, "/proc/net/rpc/%s/channel", cachelist[i].cache_name);
290 cachelist[i].f = fopen(path, "r+");
294 void cache_set_fds(fd_set *fdset)
297 for (i=0; cachelist[i].cache_name; i++) {
299 FD_SET(fileno(cachelist[i].f), fdset);
303 int cache_process_req(fd_set *readfds)
307 for (i=0; cachelist[i].cache_name; i++) {
308 if (cachelist[i].f != NULL &&
309 FD_ISSET(fileno(cachelist[i].f), readfds)) {
311 cachelist[i].cache_handle(cachelist[i].f);
319 * Give IP->domain and domain+path->options to kernel
320 * % echo nfsd $IP $[now+30*60] $domain > /proc/net/rpc/auth.unix.ip/channel
321 * % echo $domain $path $[now+30*60] $options $anonuid $anongid $fsid > /proc/net/rpc/nfsd.export/channel
324 void cache_export_ent(char *domain, struct exportent *exp)
327 FILE *f = fopen("/proc/net/rpc/nfsd.export/channel", "w");
331 qword_print(f, domain);
332 qword_print(f, exp->e_path);
333 qword_printint(f, time(0)+30*60);
334 qword_printint(f, exp->e_flags);
335 qword_printint(f, exp->e_anonuid);
336 qword_printint(f, exp->e_anongid);
337 qword_printint(f, exp->e_fsid);
342 mountlist_add(domain, exp->e_path);
345 void cache_export(nfs_export *exp)
349 f = fopen("/proc/net/rpc/auth.unix.ip/channel", "w");
353 qword_print(f, "nfsd");
354 qword_print(f, inet_ntoa(exp->m_client->m_addrlist[0]));
355 qword_printint(f, time(0)+30*60);
356 qword_print(f, exp->m_client->m_hostname);
361 if (strcmp(inet_ntoa(exp->m_client->m_addrlist[0]), exp->m_client->m_hostname))
362 mountlist_add(inet_ntoa(exp->m_client->m_addrlist[0]), exp->m_client->m_hostname);
364 cache_export_ent(exp->m_client->m_hostname, &exp->m_export);
369 * echo $domain $path $length
370 * read filehandle <&0
371 * } <> /proc/fs/nfsd/filehandle
374 cache_get_filehandle(nfs_export *exp, int len, char *p)
376 FILE *f = fopen("/proc/fs/nfsd/filehandle", "r+");
380 static struct nfs_fh_len fh;
383 f = fopen("/proc/fs/nfs/filehandle", "r+");
387 qword_print(f, exp->m_client->m_hostname);
389 qword_printint(f, len);
392 failed = (fgets(buf, sizeof(buf), f) == NULL);
396 memset(fh.fh_handle, 0, sizeof(fh.fh_handle));
397 fh.fh_size = qword_get(&bp, fh.fh_handle, NFS3_FHSIZE);