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>
29 void qword_add(char **bpp, int *lp, char *str)
37 while ((c=*str++) && len)
45 *bp++ = '0' + ((c & 0300)>>6);
46 *bp++ = '0' + ((c & 0070)>>3);
47 *bp++ = '0' + ((c & 0007)>>0);
55 if (c || len <1) len = -1;
64 void qword_addhex(char **bpp, int *lp, char *buf, int blen)
75 while (blen && len >= 2) {
76 unsigned char c = *buf++;
77 *bp++ = '0' + ((c&0xf0)>>4) + (c>=0xa0)*('a'-'9'-1);
78 *bp++ = '0' + (c&0x0f) + ((c&0x0f)>=0x0a)*('a'-'9'-1);
83 if (blen || len<1) len = -1;
92 void qword_addint(char **bpp, int *lp, int n)
96 len = snprintf(*bpp, *lp, "%d ", n);
103 void qword_adduint(char **bpp, int *lp, unsigned int n)
107 len = snprintf(*bpp, *lp, "%u ", n);
114 void qword_addeol(char **bpp, int *lp)
123 static char qword_buf[8192];
124 void qword_print(FILE *f, char *str)
126 char *bp = qword_buf;
127 int len = sizeof(qword_buf);
128 qword_add(&bp, &len, str);
129 if (fwrite(qword_buf, bp-qword_buf, 1, f) != 1) {
130 xlog_warn("qword_print: fwrite failed: errno %d (%s)",
131 errno, strerror(errno));
135 void qword_printhex(FILE *f, char *str, int slen)
137 char *bp = qword_buf;
138 int len = sizeof(qword_buf);
139 qword_addhex(&bp, &len, str, slen);
140 if (fwrite(qword_buf, bp-qword_buf, 1, f) != 1) {
141 xlog_warn("qword_printhex: fwrite failed: errno %d (%s)",
142 errno, strerror(errno));
146 void qword_printint(FILE *f, int num)
148 fprintf(f, "%d ", num);
151 void qword_printuint(FILE *f, unsigned int num)
153 fprintf(f, "%u ", num);
156 int qword_eol(FILE *f)
163 xlog_warn("qword_eol: fflush failed: errno %d (%s)",
164 errno, strerror(errno));
167 * We must send one line (and one line only) in a single write
168 * call. In case of a write error, libc may accumulate the
169 * unwritten data and try to write it again later, resulting in a
170 * multi-line write. So we must explicitly ask it to throw away
171 * any such cached data. But we return any original error
172 * indication to the caller.
181 #define isodigit(c) (isdigit(c) && c <= '7')
182 int qword_get(char **bpp, char *dest, int bufsize)
184 /* return bytes copied, or -1 on error */
188 while (*bp == ' ') bp++;
190 if (bp[0] == '\\' && bp[1] == 'x') {
193 while (isxdigit(bp[0]) && isxdigit(bp[1]) && len < bufsize) {
194 int byte = isdigit(*bp) ? *bp-'0' : toupper(*bp)-'A'+10;
197 byte |= isdigit(*bp) ? *bp-'0' : toupper(*bp)-'A'+10;
203 /* text with \nnn octal quoting */
204 while (*bp != ' ' && *bp != '\n' && *bp && len < bufsize-1) {
206 isodigit(bp[1]) && (bp[1] <= '3') &&
209 int byte = (*++bp -'0');
211 byte = (byte << 3) | (*bp++ - '0');
212 byte = (byte << 3) | (*bp++ - '0');
222 if (*bp != ' ' && *bp != '\n' && *bp != '\0')
224 while (*bp == ' ') bp++;
230 int qword_get_int(char **bpp, int *anint)
235 int len = qword_get(bpp, buf, 50);
236 if (len < 0) return -1;
237 if (len ==0) return -1;
238 rv = strtol(buf, &ep, 0);
244 int qword_get_uint(char **bpp, unsigned int *anint)
249 int len = qword_get(bpp, buf, 50);
250 if (len < 0) return -1;
251 if (len ==0) return -1;
252 rv = strtoul(buf, &ep, 0);
258 #define READLINE_BUFFER_INCREMENT 2048
260 int readline(int fd, char **buf, int *lenp)
262 /* read a line into *buf, which is malloced *len long
263 * realloc if needed until we find a \n
264 * nul out the \n and return
265 * 0 on eof, 1 on success
270 char *b = malloc(READLINE_BUFFER_INCREMENT);
274 *lenp = READLINE_BUFFER_INCREMENT;
276 len = read(fd, *buf, *lenp);
279 while ((*buf)[len-1] != '\n') {
280 /* now the less common case. There was no newline,
281 * so we have to keep reading after re-alloc
285 *lenp += READLINE_BUFFER_INCREMENT;
286 new = realloc(*buf, *lenp);
290 nl = read(fd, *buf + len, *lenp - len);
295 (*buf)[len-1] = '\0';
300 /* Check if we should use the new caching interface
301 * This succeeds iff the "nfsd" filesystem is mounted on
305 check_new_cache(void)
307 return (access("/proc/fs/nfs/filehandle", F_OK) == 0) ||
308 (access("/proc/fs/nfsd/filehandle", F_OK) == 0);
312 /* flush the kNFSd caches.
313 * Set the flush time to the mtime of _PATH_ETAB or
315 * the caches to flush are:
316 * auth.unix.ip nfsd.export nfsd.fh
320 cache_flush(int force)
327 /* Note: the order of these caches is important.
328 * They need to be flushed in dependancy order. So
329 * a cache that references items in another cache,
330 * as nfsd.fh entries reference items in nfsd.export,
331 * must be flushed before the cache that it references.
333 static char *cachelist[] = {
342 stat(_PATH_ETAB, &stb) != 0 ||
344 stb.st_mtime = time(0);
346 sprintf(stime, "%ld\n", stb.st_mtime);
347 for (c=0; cachelist[c]; c++) {
349 sprintf(path, "/proc/net/rpc/%s/flush", cachelist[c]);
350 fd = open(path, O_RDWR);
352 if (write(fd, stime, strlen(stime)) != (ssize_t)strlen(stime)) {
353 xlog_warn("Writing to '%s' failed: errno %d (%s)",
354 path, errno, strerror(errno));