2 * support/nfs/cacheio.c
3 * support IO on the cache channel files in 2.5 and beyond.
4 * These use 'qwords' which are like words, but with a little quoting.
10 * Support routines for text-based upcalls.
11 * Fields are separated by spaces.
12 * Fields are either mangled to quote space tab newline slosh with slosh
13 * or a hexified with a leading \x
14 * Record is terminated with newline.
20 #include <stdio_ext.h>
23 #include <sys/types.h>
28 void qword_add(char **bpp, int *lp, char *str)
36 while ((c=*str++) && len)
44 *bp++ = '0' + ((c & 0300)>>6);
45 *bp++ = '0' + ((c & 0070)>>3);
46 *bp++ = '0' + ((c & 0007)>>0);
54 if (c || len <1) len = -1;
63 void qword_addhex(char **bpp, int *lp, char *buf, int blen)
74 while (blen && len >= 2) {
75 unsigned char c = *buf++;
76 *bp++ = '0' + ((c&0xf0)>>4) + (c>=0xa0)*('a'-'9'-1);
77 *bp++ = '0' + (c&0x0f) + ((c&0x0f)>=0x0a)*('a'-'9'-1);
82 if (blen || len<1) len = -1;
91 void qword_addint(char **bpp, int *lp, int n)
95 len = snprintf(*bpp, *lp, "%d ", n);
102 void qword_adduint(char **bpp, int *lp, unsigned int n)
106 len = snprintf(*bpp, *lp, "%u ", n);
113 void qword_addeol(char **bpp, int *lp)
122 static char qword_buf[8192];
123 void qword_print(FILE *f, char *str)
125 char *bp = qword_buf;
126 int len = sizeof(qword_buf);
127 qword_add(&bp, &len, str);
128 fwrite(qword_buf, bp-qword_buf, 1, f);
131 void qword_printhex(FILE *f, char *str, int slen)
133 char *bp = qword_buf;
134 int len = sizeof(qword_buf);
135 qword_addhex(&bp, &len, str, slen);
136 fwrite(qword_buf, bp-qword_buf, 1, f);
139 void qword_printint(FILE *f, int num)
141 fprintf(f, "%d ", num);
144 int qword_eol(FILE *f)
151 * We must send one line (and one line only) in a single write
152 * call. In case of a write error, libc may accumulate the
153 * unwritten data and try to write it again later, resulting in a
154 * multi-line write. So we must explicitly ask it to throw away
155 * any such cached data:
163 #define isodigit(c) (isdigit(c) && c <= '7')
164 int qword_get(char **bpp, char *dest, int bufsize)
166 /* return bytes copied, or -1 on error */
170 while (*bp == ' ') bp++;
172 if (bp[0] == '\\' && bp[1] == 'x') {
175 while (isxdigit(bp[0]) && isxdigit(bp[1]) && len < bufsize) {
176 int byte = isdigit(*bp) ? *bp-'0' : toupper(*bp)-'A'+10;
179 byte |= isdigit(*bp) ? *bp-'0' : toupper(*bp)-'A'+10;
185 /* text with \nnn octal quoting */
186 while (*bp != ' ' && *bp != '\n' && *bp && len < bufsize-1) {
188 isodigit(bp[1]) && (bp[1] <= '3') &&
191 int byte = (*++bp -'0');
193 byte = (byte << 3) | (*bp++ - '0');
194 byte = (byte << 3) | (*bp++ - '0');
204 if (*bp != ' ' && *bp != '\n' && *bp != '\0')
206 while (*bp == ' ') bp++;
212 int qword_get_int(char **bpp, int *anint)
217 int len = qword_get(bpp, buf, 50);
218 if (len < 0) return -1;
219 if (len ==0) return -1;
220 rv = strtol(buf, &ep, 0);
226 #define READLINE_BUFFER_INCREMENT 2048
228 int readline(int fd, char **buf, int *lenp)
230 /* read a line into *buf, which is malloced *len long
231 * realloc if needed until we find a \n
232 * nul out the \n and return
233 * 0 on eof, 1 on success
238 char *b = malloc(READLINE_BUFFER_INCREMENT);
242 *lenp = READLINE_BUFFER_INCREMENT;
244 len = read(fd, *buf, *lenp);
247 while ((*buf)[len-1] != '\n') {
248 /* now the less common case. There was no newline,
249 * so we have to keep reading after re-alloc
253 *lenp += READLINE_BUFFER_INCREMENT;
254 new = realloc(*buf, *lenp);
258 nl = read(fd, *buf + len, *lenp - len);
263 (*buf)[len-1] = '\0';
268 /* Check if we should use the new caching interface
269 * This succeeds iff the "nfsd" filesystem is mounted on
273 check_new_cache(void)
276 return (stat("/proc/fs/nfs/filehandle", &stb) == 0) ||
277 (stat("/proc/fs/nfsd/filehandle", &stb) == 0);
281 /* flush the kNFSd caches.
282 * Set the flush time to the mtime of _PATH_ETAB or
284 * the caches to flush are:
285 * auth.unix.ip nfsd.export nfsd.fh
289 cache_flush(int force)
296 /* Note: the order of these caches is important.
297 * They need to be flushed in dependancy order. So
298 * a cache that references items in another cache,
299 * as nfsd.fh entries reference items in nfsd.export,
300 * must be flushed before the cache that it references.
302 static char *cachelist[] = {
311 stat(_PATH_ETAB, &stb) != 0 ||
313 stb.st_mtime = time(0);
315 sprintf(stime, "%ld\n", stb.st_mtime);
316 for (c=0; cachelist[c]; c++) {
318 sprintf(path, "/proc/net/rpc/%s/flush", cachelist[c]);
319 fd = open(path, O_RDWR);
321 write(fd, stime, strlen(stime));