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) free(client);
88 * domain fsidtype fsid
89 * interpret fsid, find export point and options, and write:
90 * domain fsidtype fsid expiry path
96 unsigned int dev, major=0, minor=0;
98 unsigned int fsidnum=0;
100 struct exportent *found = NULL;
104 if (readline(fileno(f), &lbuf, &lbuflen) != 1)
109 dom = malloc(strlen(cp));
112 if (qword_get(&cp, dom, strlen(cp)) <= 0)
114 if (qword_get_int(&cp, &fsidtype) != 0)
116 if (fsidtype < 0 || fsidtype > 1)
117 goto out; /* unknown type */
118 if ((fsidlen = qword_get(&cp, fsid, 32)) <= 0)
121 case 0: /* 4 bytes: 2 major, 2 minor, 4 inode */
124 memcpy(&dev, fsid, 4);
125 memcpy(&inode, fsid+4, 4);
126 major = ntohl(dev)>>16;
127 minor = ntohl(dev) & 0xFFFF;
130 case 1: /* 4 bytes - fsid */
133 memcpy(&fsidnum, fsid, 4);
137 /* Now determine export point for this fsid/domain */
138 for (i=0 ; i < MCL_MAXTYPES; i++) {
139 for (exp = exportlist[i]; exp; exp = exp->m_next) {
140 if (!client_member(dom, exp->m_client->m_hostname))
143 ((exp->m_export.e_flags & NFSEXP_FSID) == 0 ||
144 exp->m_export.e_fsid != fsidnum))
148 if (stat(exp->m_export.e_path, &stb) != 0)
150 if (stb.st_ino != inode)
152 if (major != major(stb.st_dev) ||
153 minor != minor(stb.st_dev))
156 /* It's a match !! */
158 found = &exp->m_export;
159 else if (strcmp(found->e_path, exp->m_export.e_path)!= 0)
161 xlog(L_WARNING, "%s and %s have name filehandle for %s, using first",
162 found->e_path, exp->m_export.e_path, dom);
166 cache_export_ent(dom, found);
169 qword_printint(f, fsidtype);
170 qword_printhex(f, fsid, fsidlen);
171 qword_printint(f, time(0)+30*60);
173 qword_print(f, found->e_path);
180 void nfsd_export(FILE *f)
184 * determine export options and return:
185 * domain path expiry flags anonuid anongid fsid
191 nfs_export *exp, *found = NULL;
194 if (readline(fileno(f), &lbuf, &lbuflen) != 1)
198 dom = malloc(strlen(cp));
199 path = malloc(strlen(cp));
204 if (qword_get(&cp, dom, strlen(lbuf)) <= 0)
206 if (qword_get(&cp, path, strlen(lbuf)) <= 0)
209 /* now find flags for this export point in this domain */
210 for (i=0 ; i < MCL_MAXTYPES; i++) {
211 for (exp = exportlist[i]; exp; exp = exp->m_next) {
212 if (!client_member(dom, exp->m_client->m_hostname))
214 if (strcmp(path, exp->m_export.e_path))
219 xlog(L_WARNING, "%s exported to both %s and %s in %s",
220 path, exp->m_client->m_hostname, found->m_client->m_hostname,
227 qword_print(f, path);
228 qword_printint(f, time(0)+30*60);
230 qword_printint(f, found->m_export.e_flags);
231 qword_printint(f, found->m_export.e_anonuid);
232 qword_printint(f, found->m_export.e_anongid);
233 qword_printint(f, found->m_export.e_fsid);
238 if (path) free(path);
244 void (*cache_handle)(FILE *f);
247 { "auth.unix.ip", auth_unix_ip},
248 { "nfsd.export", nfsd_export},
249 { "nfsd.fh", nfsd_fh},
253 void cache_open(void)
256 for (i=0; cachelist[i].cache_name; i++ ){
258 sprintf(path, "/proc/net/rpc/%s/channel", cachelist[i].cache_name);
259 cachelist[i].f = fopen(path, "r+");
263 void cache_set_fds(fd_set *fdset)
266 for (i=0; cachelist[i].cache_name; i++) {
268 FD_SET(fileno(cachelist[i].f), fdset);
272 int cache_process_req(fd_set *readfds)
276 for (i=0; cachelist[i].cache_name; i++) {
277 if (cachelist[i].f != NULL &&
278 FD_ISSET(fileno(cachelist[i].f), readfds)) {
280 cachelist[i].cache_handle(cachelist[i].f);
288 * Give IP->domain and domain+path->options to kernel
289 * % echo nfsd $IP $[now+30*60] $domain > /proc/net/rpc/auth.unix.ip/channel
290 * % echo $domain $path $[now+30*60] $options $anonuid $anongid $fsid > /proc/net/rpc/nfsd.export/channel
293 void cache_export_ent(char *domain, struct exportent *exp)
296 FILE *f = fopen("/proc/net/rpc/nfsd.export/channel", "r+");
300 qword_print(f, domain);
301 qword_print(f, exp->e_path);
302 qword_printint(f, time(0)+30*60);
303 qword_printint(f, exp->e_flags);
304 qword_printint(f, exp->e_anonuid);
305 qword_printint(f, exp->e_anongid);
306 qword_printint(f, exp->e_fsid);
312 void cache_export(nfs_export *exp)
316 f = fopen("/proc/net/rpc/auth.unix.ip/channel", "r+");
320 qword_print(f, "nfsd");
321 qword_print(f, inet_ntoa(exp->m_client->m_addrlist[0]));
322 qword_printint(f, time(0)+30*60);
323 qword_print(f, exp->m_client->m_hostname);
328 cache_export_ent(exp->m_client->m_hostname, &exp->m_export);
333 * echo $domain $path $length
334 * read filehandle <&0
335 * } <> /proc/fs/nfs/filehandle
338 cache_get_filehandle(nfs_export *exp, int len, char *p)
340 FILE *f = fopen("/proc/fs/nfs/filehandle", "r+");
343 static struct nfs_fh_len fh;
347 qword_print(f, exp->m_client->m_hostname);
349 qword_printint(f, len);
352 if (fgets(buf, sizeof(buf), f) == NULL)
354 memset(fh.fh_handle, 0, sizeof(fh.fh_handle));
355 fh.fh_size = qword_get(&bp, fh.fh_handle, NFS3_FHSIZE);