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 static char qword_buf[8192];
92 void qword_print(FILE *f, char *str)
95 int len = sizeof(qword_buf);
96 qword_add(&bp, &len, str);
97 fwrite(qword_buf, bp-qword_buf, 1, f);
100 void qword_printhex(FILE *f, char *str, int slen)
102 char *bp = qword_buf;
103 int len = sizeof(qword_buf);
104 qword_addhex(&bp, &len, str, slen);
105 fwrite(qword_buf, bp-qword_buf, 1, f);
108 void qword_printint(FILE *f, int num)
110 fprintf(f, "%d ", num);
113 int qword_eol(FILE *f)
120 * We must send one line (and one line only) in a single write
121 * call. In case of a write error, libc may accumulate the
122 * unwritten data and try to write it again later, resulting in a
123 * multi-line write. So we must explicitly ask it to throw away
124 * any such cached data:
132 #define isodigit(c) (isdigit(c) && c <= '7')
133 int qword_get(char **bpp, char *dest, int bufsize)
135 /* return bytes copied, or -1 on error */
139 while (*bp == ' ') bp++;
141 if (bp[0] == '\\' && bp[1] == 'x') {
144 while (isxdigit(bp[0]) && isxdigit(bp[1]) && len < bufsize) {
145 int byte = isdigit(*bp) ? *bp-'0' : toupper(*bp)-'A'+10;
148 byte |= isdigit(*bp) ? *bp-'0' : toupper(*bp)-'A'+10;
154 /* text with \nnn octal quoting */
155 while (*bp != ' ' && *bp != '\n' && *bp && len < bufsize-1) {
157 isodigit(bp[1]) && (bp[1] <= '3') &&
160 int byte = (*++bp -'0');
162 byte = (byte << 3) | (*bp++ - '0');
163 byte = (byte << 3) | (*bp++ - '0');
173 if (*bp != ' ' && *bp != '\n' && *bp != '\0')
175 while (*bp == ' ') bp++;
181 int qword_get_int(char **bpp, int *anint)
186 int len = qword_get(bpp, buf, 50);
187 if (len < 0) return -1;
188 if (len ==0) return -1;
189 rv = strtol(buf, &ep, 0);
195 int readline(int fd, char **buf, int *lenp)
197 /* read a line into *buf, which is malloced *len long
198 * realloc if needed until we find a \n
199 * nul out the \n and return
200 * 0 of eof, 1 of success
205 char *b = malloc(128);
211 len = read(fd, *buf, len);
214 while ((*buf)[len-1] != '\n') {
215 /* now the less common case. There was no newline,
216 * so we have to keep reading after re-alloc
221 new = realloc(*buf, *lenp);
225 nl = read(fd, *buf + len, *lenp - len);
230 (*buf)[len-1] = '\0';
235 /* Check if we should use the new caching interface
236 * This succeeds iff the "nfsd" filesystem is mounted on
240 check_new_cache(void)
243 return (stat("/proc/fs/nfs/filehandle", &stb) == 0) ||
244 (stat("/proc/fs/nfsd/filehandle", &stb) == 0);
248 /* flush the kNFSd caches.
249 * Set the flush time to the mtime of _PATH_ETAB or
251 * the caches to flush are:
252 * auth.unix.ip nfsd.export nfsd.fh
256 cache_flush(int force)
263 /* Note: the order of these caches is important.
264 * They need to be flushed in dependancy order. So
265 * a cache that references items in another cache,
266 * as nfsd.fh entries reference items in nfsd.export,
267 * must be flushed before the cache that it references.
269 static char *cachelist[] = {
278 stat(_PATH_ETAB, &stb) != 0 ||
280 stb.st_mtime = time(0);
282 sprintf(stime, "%ld\n", stb.st_mtime);
283 for (c=0; cachelist[c]; c++) {
285 sprintf(path, "/proc/net/rpc/%s/flush", cachelist[c]);
286 fd = open(path, O_RDWR);
288 write(fd, stime, strlen(stime));