xref: /netbsd-src/lib/libpuffs/null.c (revision 207defd0369f03b80f291e7c291be1f3b7ab3397)
1 /*	$NetBSD: null.c,v 1.36 2021/09/11 20:28:03 andvar Exp $	*/
2 
3 /*
4  * Copyright (c) 2007  Antti Kantee.  All Rights Reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  * 1. Redistributions of source code must retain the above copyright
10  *    notice, this list of conditions and the following disclaimer.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  *    notice, this list of conditions and the following disclaimer in the
13  *    documentation and/or other materials provided with the distribution.
14  *
15  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS
16  * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
17  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
18  * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
21  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25  * SUCH DAMAGE.
26  */
27 
28 #include <sys/cdefs.h>
29 #if !defined(lint)
30 __RCSID("$NetBSD: null.c,v 1.36 2021/09/11 20:28:03 andvar Exp $");
31 #endif /* !lint */
32 
33 /*
34  * A "nullfs" using puffs, i.e. maps one location in the hierarchy
35  * to another using standard system calls.
36  */
37 
38 #include <sys/types.h>
39 #include <sys/stat.h>
40 #include <sys/time.h>
41 
42 #include <assert.h>
43 #include <dirent.h>
44 #include <errno.h>
45 #include <fcntl.h>
46 #include <puffs.h>
47 #include <stdio.h>
48 #include <stdlib.h>
49 #include <time.h>
50 #include <unistd.h>
51 
PUFFSOP_PROTOS(puffs_null)52 PUFFSOP_PROTOS(puffs_null)
53 
54 /*
55  * set attributes to what is specified.  XXX: no rollback in case of failure
56  */
57 static int
58 processvattr(const char *path, const struct vattr *va, int regular)
59 {
60 	struct timeval tv[2];
61 
62 	/* XXX: -1 == PUFFS_VNOVAL, but shouldn't trust that */
63 	if (va->va_uid != (unsigned)-1 || va->va_gid != (unsigned)-1)
64 		if (lchown(path, va->va_uid, va->va_gid) == -1)
65 			return errno;
66 
67 	if (va->va_mode != (unsigned)PUFFS_VNOVAL)
68 		if (lchmod(path, va->va_mode) == -1)
69 			return errno;
70 
71 	/* sloppy */
72 	if (va->va_atime.tv_sec != (time_t)PUFFS_VNOVAL
73 	    || va->va_mtime.tv_sec != (time_t)PUFFS_VNOVAL) {
74 		TIMESPEC_TO_TIMEVAL(&tv[0], &va->va_atime);
75 		TIMESPEC_TO_TIMEVAL(&tv[1], &va->va_mtime);
76 
77 		if (lutimes(path, tv) == -1)
78 			return errno;
79 	}
80 
81 	if (regular && va->va_size != (u_quad_t)PUFFS_VNOVAL)
82 		if (truncate(path, (off_t)va->va_size) == -1)
83 			return errno;
84 
85 	return 0;
86 }
87 
88 /*
89  * Kludge to open files which aren't writable *any longer*.  This kinda
90  * works because the vfs layer does validation checks based on the file's
91  * permissions to allow writable opening before opening them.  However,
92  * the problem arises if we want to create a file, write to it (cache),
93  * adjust permissions and then flush the file.
94  */
95 static int
writeableopen(const char * path)96 writeableopen(const char *path)
97 {
98 	struct stat sb;
99 	mode_t origmode;
100 	int sverr = 0;
101 	int fd;
102 
103 	fd = open(path, O_WRONLY);
104 	if (fd == -1) {
105 		if (errno == EACCES) {
106 			if (stat(path, &sb) == -1)
107 				return -1;
108 			origmode = sb.st_mode & ALLPERMS;
109 
110 			if (chmod(path, 0200) == -1)
111 				return -1;
112 
113 			fd = open(path, O_WRONLY);
114 			if (fd == -1)
115 				sverr = errno;
116 
117 			chmod(path, origmode);
118 			if (sverr)
119 				errno = sverr;
120 		} else
121 			return -1;
122 	}
123 
124 	return fd;
125 }
126 
127 /*ARGSUSED*/
128 static void *
inodecmp(struct puffs_usermount * pu,struct puffs_node * pn,void * arg)129 inodecmp(struct puffs_usermount *pu, struct puffs_node *pn, void *arg)
130 {
131 	ino_t *cmpino = arg;
132 
133 	if (pn->pn_va.va_fileid == *cmpino)
134 		return pn;
135 	return NULL;
136 }
137 
138 static int
makenode(struct puffs_usermount * pu,struct puffs_newinfo * pni,const struct puffs_cn * pcn,const struct vattr * va,int regular)139 makenode(struct puffs_usermount *pu, struct puffs_newinfo *pni,
140 	const struct puffs_cn *pcn, const struct vattr *va, int regular)
141 {
142 	struct puffs_node *pn;
143 	struct stat sb;
144 	int rv;
145 
146 	if ((rv = processvattr(PCNPATH(pcn), va, regular)) != 0)
147 		return rv;
148 
149 	pn = puffs_pn_new(pu, NULL);
150 	if (!pn)
151 		return ENOMEM;
152 	puffs_setvattr(&pn->pn_va, va);
153 
154 	if (lstat(PCNPATH(pcn), &sb) == -1)
155 		return errno;
156 	puffs_stat2vattr(&pn->pn_va, &sb);
157 
158 	puffs_newinfo_setcookie(pni, pn);
159 	return 0;
160 }
161 
162 /* This should be called first and overridden from the file system */
163 void
puffs_null_setops(struct puffs_ops * pops)164 puffs_null_setops(struct puffs_ops *pops)
165 {
166 
167 	PUFFSOP_SET(pops, puffs_null, fs, statvfs);
168 	PUFFSOP_SETFSNOP(pops, unmount);
169 	PUFFSOP_SETFSNOP(pops, sync);
170 	PUFFSOP_SET(pops, puffs_null, fs, fhtonode);
171 	PUFFSOP_SET(pops, puffs_null, fs, nodetofh);
172 
173 	PUFFSOP_SET(pops, puffs_null, node, lookup);
174 	PUFFSOP_SET(pops, puffs_null, node, create);
175 	PUFFSOP_SET(pops, puffs_null, node, mknod);
176 	PUFFSOP_SET(pops, puffs_null, node, getattr);
177 	PUFFSOP_SET(pops, puffs_null, node, setattr);
178 	PUFFSOP_SET(pops, puffs_null, node, fsync);
179 	PUFFSOP_SET(pops, puffs_null, node, remove);
180 	PUFFSOP_SET(pops, puffs_null, node, link);
181 	PUFFSOP_SET(pops, puffs_null, node, rename);
182 	PUFFSOP_SET(pops, puffs_null, node, mkdir);
183 	PUFFSOP_SET(pops, puffs_null, node, rmdir);
184 	PUFFSOP_SET(pops, puffs_null, node, symlink);
185 	PUFFSOP_SET(pops, puffs_null, node, readlink);
186 	PUFFSOP_SET(pops, puffs_null, node, readdir);
187 	PUFFSOP_SET(pops, puffs_null, node, read);
188 	PUFFSOP_SET(pops, puffs_null, node, write);
189 	PUFFSOP_SET(pops, puffs_genfs, node, reclaim);
190 }
191 
192 /*ARGSUSED*/
193 int
puffs_null_fs_statvfs(struct puffs_usermount * pu,struct puffs_statvfs * svfsb)194 puffs_null_fs_statvfs(struct puffs_usermount *pu, struct puffs_statvfs *svfsb)
195 {
196 	struct statvfs sb;
197 	if (statvfs(PNPATH(puffs_getroot(pu)), &sb) == -1)
198 		return errno;
199 	statvfs_to_puffs_statvfs(&sb, svfsb);
200 
201 	return 0;
202 }
203 
204 /*
205  * XXX: this is the stupidest crap ever, but:
206  * getfh() returns the fhandle type, when we are expected to deliver
207  * the fid type.  Just adjust it a bit and stop whining.
208  *
209  * Yes, this really really needs fixing.  Yes, *REALLY*.
210  */
211 #define FHANDLE_HEADERLEN 8
212 struct kernfid {
213 	unsigned short	fid_len;		/* length of data in bytes */
214 	unsigned short	fid_reserved;		/* compat: historic align */
215 	char		fid_data[0];		/* data (variable length) */
216 };
217 
218 /*ARGSUSED*/
219 static void *
fhcmp(struct puffs_usermount * pu,struct puffs_node * pn,void * arg)220 fhcmp(struct puffs_usermount *pu, struct puffs_node *pn, void *arg)
221 {
222 	struct kernfid *kf1, *kf2;
223 
224 	if ((kf1 = pn->pn_data) == NULL)
225 		return NULL;
226 	kf2 = arg;
227 
228 	if (kf1->fid_len != kf2->fid_len)
229 		return NULL;
230 
231 	/*LINTED*/
232 	if (memcmp(kf1, kf2, kf1->fid_len) == 0)
233 		return pn;
234 	return NULL;
235 }
236 
237 /*
238  * This routine only supports file handles which have been issued while
239  * the server was alive.  Not really stable ones, that is.
240  */
241 /*ARGSUSED*/
242 int
puffs_null_fs_fhtonode(struct puffs_usermount * pu,void * fid,size_t fidsize,struct puffs_newinfo * pni)243 puffs_null_fs_fhtonode(struct puffs_usermount *pu, void *fid, size_t fidsize,
244 	struct puffs_newinfo *pni)
245 {
246 	struct puffs_node *pn_res;
247 
248 	pn_res = puffs_pn_nodewalk(pu, fhcmp, fid);
249 	if (pn_res == NULL)
250 		return ENOENT;
251 
252 	puffs_newinfo_setcookie(pni, pn_res);
253 	puffs_newinfo_setvtype(pni, pn_res->pn_va.va_type);
254 	puffs_newinfo_setsize(pni, (voff_t)pn_res->pn_va.va_size);
255 	puffs_newinfo_setrdev(pni, pn_res->pn_va.va_rdev);
256 	return 0;
257 }
258 
259 /*ARGSUSED*/
260 int
puffs_null_fs_nodetofh(struct puffs_usermount * pu,puffs_cookie_t opc,void * fid,size_t * fidsize)261 puffs_null_fs_nodetofh(struct puffs_usermount *pu, puffs_cookie_t opc,
262 	void *fid, size_t *fidsize)
263 {
264 	struct puffs_node *pn = opc;
265 	struct kernfid *kfid;
266 	void *bounce;
267 	int rv;
268 
269 	rv = 0;
270 	bounce = NULL;
271 	if (*fidsize) {
272 		bounce = malloc(*fidsize + FHANDLE_HEADERLEN);
273 		if (!bounce)
274 			return ENOMEM;
275 		*fidsize += FHANDLE_HEADERLEN;
276 	}
277 	if (getfh(PNPATH(pn), bounce, fidsize) == -1)
278 		rv = errno;
279 	else
280 		memcpy(fid, (uint8_t *)bounce + FHANDLE_HEADERLEN,
281 		    *fidsize - FHANDLE_HEADERLEN);
282 	kfid = fid;
283 	if (rv == 0) {
284 		*fidsize = kfid->fid_len;
285 		pn->pn_data = malloc(*fidsize);
286 		if (pn->pn_data == NULL)
287 			abort(); /* lazy */
288 		memcpy(pn->pn_data, fid, *fidsize);
289 	} else {
290 		*fidsize -= FHANDLE_HEADERLEN;
291 	}
292 	free(bounce);
293 
294 	return rv;
295 }
296 
297 int
puffs_null_node_lookup(struct puffs_usermount * pu,puffs_cookie_t opc,struct puffs_newinfo * pni,const struct puffs_cn * pcn)298 puffs_null_node_lookup(struct puffs_usermount *pu, puffs_cookie_t opc,
299 	struct puffs_newinfo *pni, const struct puffs_cn *pcn)
300 {
301 	struct puffs_node *pn = opc, *pn_res;
302 	struct stat sb;
303 	int rv;
304 
305 	assert(pn->pn_va.va_type == VDIR);
306 
307 	/*
308 	 * Note to whoever is copypasting this: you must first check
309 	 * if the node is there and only then do nodewalk.  Alternatively
310 	 * you could make sure that you don't return unlinked/rmdir'd
311 	 * nodes in some other fashion
312 	 */
313 	rv = lstat(PCNPATH(pcn), &sb);
314 	if (rv)
315 		return errno;
316 
317 	/* XXX2: nodewalk is a bit too slow here */
318 	pn_res = puffs_pn_nodewalk(pu, inodecmp, &sb.st_ino);
319 
320 	if (pn_res == NULL) {
321 		pn_res = puffs_pn_new(pu, NULL);
322 		if (pn_res == NULL)
323 			return ENOMEM;
324 		puffs_stat2vattr(&pn_res->pn_va, &sb);
325 	}
326 
327 	puffs_newinfo_setcookie(pni, pn_res);
328 	puffs_newinfo_setvtype(pni, pn_res->pn_va.va_type);
329 	puffs_newinfo_setsize(pni, (voff_t)pn_res->pn_va.va_size);
330 	puffs_newinfo_setrdev(pni, pn_res->pn_va.va_rdev);
331 
332 	return 0;
333 }
334 
335 /*ARGSUSED*/
336 int
puffs_null_node_create(struct puffs_usermount * pu,puffs_cookie_t opc,struct puffs_newinfo * pni,const struct puffs_cn * pcn,const struct vattr * va)337 puffs_null_node_create(struct puffs_usermount *pu, puffs_cookie_t opc,
338 	struct puffs_newinfo *pni, const struct puffs_cn *pcn,
339 	const struct vattr *va)
340 {
341 	int fd, rv;
342 
343 	fd = open(PCNPATH(pcn), O_RDWR | O_CREAT | O_TRUNC);
344 	if (fd == -1)
345 		return errno;
346 	close(fd);
347 
348 	rv = makenode(pu, pni, pcn, va, 1);
349 	if (rv)
350 		unlink(PCNPATH(pcn));
351 	return rv;
352 }
353 
354 /*ARGSUSED*/
355 int
puffs_null_node_mknod(struct puffs_usermount * pu,puffs_cookie_t opc,struct puffs_newinfo * pni,const struct puffs_cn * pcn,const struct vattr * va)356 puffs_null_node_mknod(struct puffs_usermount *pu, puffs_cookie_t opc,
357 	struct puffs_newinfo *pni, const struct puffs_cn *pcn,
358 	const struct vattr *va)
359 {
360 	mode_t mode;
361 	int rv;
362 
363 	mode = puffs_addvtype2mode(va->va_mode, va->va_type);
364 	if (mknod(PCNPATH(pcn), mode, va->va_rdev) == -1)
365 		return errno;
366 
367 	rv = makenode(pu, pni, pcn, va, 0);
368 	if (rv)
369 		unlink(PCNPATH(pcn));
370 	return rv;
371 }
372 
373 /*ARGSUSED*/
374 int
puffs_null_node_getattr(struct puffs_usermount * pu,puffs_cookie_t opc,struct vattr * va,const struct puffs_cred * pcred)375 puffs_null_node_getattr(struct puffs_usermount *pu, puffs_cookie_t opc,
376 	struct vattr *va, const struct puffs_cred *pcred)
377 {
378 	struct puffs_node *pn = opc;
379 	struct stat sb;
380 
381 	if (lstat(PNPATH(pn), &sb) == -1)
382 		return errno;
383 	puffs_stat2vattr(va, &sb);
384 
385 	return 0;
386 }
387 
388 /*ARGSUSED*/
389 int
puffs_null_node_setattr(struct puffs_usermount * pu,puffs_cookie_t opc,const struct vattr * va,const struct puffs_cred * pcred)390 puffs_null_node_setattr(struct puffs_usermount *pu, puffs_cookie_t opc,
391 	const struct vattr *va, const struct puffs_cred *pcred)
392 {
393 	struct puffs_node *pn = opc;
394 	int rv;
395 
396 	rv = processvattr(PNPATH(pn), va, pn->pn_va.va_type == VREG);
397 	if (rv)
398 		return rv;
399 
400 	puffs_setvattr(&pn->pn_va, va);
401 
402 	return 0;
403 }
404 
405 /*ARGSUSED*/
406 int
puffs_null_node_fsync(struct puffs_usermount * pu,puffs_cookie_t opc,const struct puffs_cred * pcred,int how,off_t offlo,off_t offhi)407 puffs_null_node_fsync(struct puffs_usermount *pu, puffs_cookie_t opc,
408 	const struct puffs_cred *pcred, int how,
409 	off_t offlo, off_t offhi)
410 {
411 	struct puffs_node *pn = opc;
412 	int fd, rv;
413 	int fflags;
414 	struct stat sb;
415 	DIR *dirp;
416 
417 	rv = 0;
418 	if (stat(PNPATH(pn), &sb) == -1)
419 		return errno;
420 	if (S_ISDIR(sb.st_mode)) {
421 		if ((dirp = opendir(PNPATH(pn))) == NULL)
422 			return errno;
423 		fd = dirfd(dirp);
424 		if (fd == -1) {
425 			rv = errno;
426 			closedir(dirp);
427 			return rv;
428 		}
429 
430 		if (fsync(fd) == -1)
431 			rv = errno;
432 
433 		closedir(dirp);
434 	} else {
435 		fd = writeableopen(PNPATH(pn));
436 		if (fd == -1)
437 			return errno;
438 
439 		if (how & PUFFS_FSYNC_DATAONLY)
440 			fflags = FDATASYNC;
441 		else
442 			fflags = FFILESYNC;
443 		if (how & PUFFS_FSYNC_CACHE)
444 			fflags |= FDISKSYNC;
445 
446 		if (fsync_range(fd, fflags, offlo, offhi - offlo) == -1)
447 			rv = errno;
448 
449 		close(fd);
450 	}
451 
452 	return rv;
453 }
454 
455 /*ARGSUSED*/
456 int
puffs_null_node_remove(struct puffs_usermount * pu,puffs_cookie_t opc,puffs_cookie_t targ,const struct puffs_cn * pcn)457 puffs_null_node_remove(struct puffs_usermount *pu, puffs_cookie_t opc,
458 	puffs_cookie_t targ, const struct puffs_cn *pcn)
459 {
460 	struct puffs_node *pn_targ = targ;
461 
462 	if (unlink(PNPATH(pn_targ)) == -1)
463 		return errno;
464 	puffs_pn_remove(pn_targ);
465 
466 	return 0;
467 }
468 
469 /*ARGSUSED*/
470 int
puffs_null_node_link(struct puffs_usermount * pu,puffs_cookie_t opc,puffs_cookie_t targ,const struct puffs_cn * pcn)471 puffs_null_node_link(struct puffs_usermount *pu, puffs_cookie_t opc,
472 	puffs_cookie_t targ, const struct puffs_cn *pcn)
473 {
474 	struct puffs_node *pn_targ = targ;
475 
476 	if (link(PNPATH(pn_targ), PCNPATH(pcn)) == -1)
477 		return errno;
478 
479 	return 0;
480 }
481 
482 /*ARGSUSED*/
483 int
puffs_null_node_rename(struct puffs_usermount * pu,puffs_cookie_t opc,puffs_cookie_t src,const struct puffs_cn * pcn_src,puffs_cookie_t targ_dir,puffs_cookie_t targ,const struct puffs_cn * pcn_targ)484 puffs_null_node_rename(struct puffs_usermount *pu, puffs_cookie_t opc,
485 	puffs_cookie_t src, const struct puffs_cn *pcn_src,
486 	puffs_cookie_t targ_dir, puffs_cookie_t targ,
487 	const struct puffs_cn *pcn_targ)
488 {
489 	struct puffs_node *pn_targ = targ;
490 
491 	if (rename(PCNPATH(pcn_src), PCNPATH(pcn_targ)) == -1)
492 		return errno;
493 
494         if (pn_targ)
495 		puffs_pn_remove(pn_targ);
496 
497 	return 0;
498 }
499 
500 /*ARGSUSED*/
501 int
puffs_null_node_mkdir(struct puffs_usermount * pu,puffs_cookie_t opc,struct puffs_newinfo * pni,const struct puffs_cn * pcn,const struct vattr * va)502 puffs_null_node_mkdir(struct puffs_usermount *pu, puffs_cookie_t opc,
503 	struct puffs_newinfo *pni, const struct puffs_cn *pcn,
504 	const struct vattr *va)
505 {
506 	int rv;
507 
508 	if (mkdir(PCNPATH(pcn), va->va_mode) == -1)
509 		return errno;
510 
511 	rv = makenode(pu, pni, pcn, va, 0);
512 	if (rv)
513 		rmdir(PCNPATH(pcn));
514 	return rv;
515 }
516 
517 /*ARGSUSED*/
518 int
puffs_null_node_rmdir(struct puffs_usermount * pu,puffs_cookie_t opc,puffs_cookie_t targ,const struct puffs_cn * pcn)519 puffs_null_node_rmdir(struct puffs_usermount *pu, puffs_cookie_t opc,
520 	puffs_cookie_t targ, const struct puffs_cn *pcn)
521 {
522 	struct puffs_node *pn_targ = targ;
523 
524 	if (rmdir(PNPATH(pn_targ)) == -1)
525 		return errno;
526 	puffs_pn_remove(pn_targ);
527 
528 	return 0;
529 }
530 
531 /*ARGSUSED*/
532 int
puffs_null_node_symlink(struct puffs_usermount * pu,puffs_cookie_t opc,struct puffs_newinfo * pni,const struct puffs_cn * pcn,const struct vattr * va,const char * linkname)533 puffs_null_node_symlink(struct puffs_usermount *pu, puffs_cookie_t opc,
534 	struct puffs_newinfo *pni, const struct puffs_cn *pcn,
535 	const struct vattr *va, const char *linkname)
536 {
537 	int rv;
538 
539 	if (symlink(linkname, PCNPATH(pcn)) == -1)
540 		return errno;
541 
542 	rv = makenode(pu, pni, pcn, va, 0);
543 	if (rv)
544 		unlink(PCNPATH(pcn));
545 	return rv;
546 }
547 
548 /*ARGSUSED*/
549 int
puffs_null_node_readlink(struct puffs_usermount * pu,puffs_cookie_t opc,const struct puffs_cred * pcred,char * linkname,size_t * linklen)550 puffs_null_node_readlink(struct puffs_usermount *pu, puffs_cookie_t opc,
551 	const struct puffs_cred *pcred, char *linkname, size_t *linklen)
552 {
553 	struct puffs_node *pn = opc;
554 	ssize_t rv;
555 
556 	rv = readlink(PNPATH(pn), linkname, *linklen);
557 	if (rv == -1)
558 		return errno;
559 
560 	*linklen = rv;
561 	return 0;
562 }
563 
564 /*ARGSUSED*/
565 int
puffs_null_node_readdir(struct puffs_usermount * pu,puffs_cookie_t opc,struct dirent * de,off_t * off,size_t * reslen,const struct puffs_cred * pcred,int * eofflag,off_t * cookies,size_t * ncookies)566 puffs_null_node_readdir(struct puffs_usermount *pu, puffs_cookie_t opc,
567 	struct dirent *de, off_t *off, size_t *reslen,
568 	const struct puffs_cred *pcred, int *eofflag, off_t *cookies,
569 	size_t *ncookies)
570 {
571 	struct puffs_node *pn = opc;
572 	struct dirent entry, *result;
573 	DIR *dp;
574 	off_t i;
575 	int rv;
576 
577 	*ncookies = 0;
578 	dp = opendir(PNPATH(pn));
579 	if (dp == NULL)
580 		return errno;
581 
582 	rv = 0;
583 	i = *off;
584 
585 	/*
586 	 * XXX: need to do trickery here, telldir/seekdir would be nice, but
587 	 * then we'd need to keep state, which I'm too lazy to keep
588 	 */
589 	while (i--) {
590 		rv = readdir_r(dp, &entry, &result);
591 		if (rv != 0)
592 			goto out;
593 
594 		if (!result) {
595 			*eofflag = 1;
596 			goto out;
597 		}
598 	}
599 
600 	for (;;) {
601 		rv = readdir_r(dp, &entry, &result);
602 		if (rv != 0)
603 			goto out;
604 
605 		if (!result) {
606 			*eofflag = 1;
607 			goto out;
608 		}
609 
610 		if (_DIRENT_SIZE(result) > *reslen)
611 			goto out;
612 
613 		*de = *result;
614 		*reslen -= _DIRENT_SIZE(result);
615 		de = _DIRENT_NEXT(de);
616 
617 		(*off)++;
618 		PUFFS_STORE_DCOOKIE(cookies, ncookies, *off);
619 	}
620 
621  out:
622 	closedir(dp);
623 	return 0;
624 }
625 
626 /*ARGSUSED*/
627 int
puffs_null_node_read(struct puffs_usermount * pu,puffs_cookie_t opc,uint8_t * buf,off_t offset,size_t * buflen,const struct puffs_cred * pcred,int ioflag)628 puffs_null_node_read(struct puffs_usermount *pu, puffs_cookie_t opc,
629 	uint8_t *buf, off_t offset, size_t *buflen,
630 	const struct puffs_cred *pcred, int ioflag)
631 {
632 	struct puffs_node *pn = opc;
633 	ssize_t n;
634 	off_t off;
635 	int fd, rv;
636 
637 	rv = 0;
638 	fd = open(PNPATH(pn), O_RDONLY);
639 	if (fd == -1)
640 		return errno;
641 	off = lseek(fd, offset, SEEK_SET);
642 	if (off == -1) {
643 		rv = errno;
644 		goto out;
645 	}
646 
647 	n = read(fd, buf, *buflen);
648 	if (n == -1)
649 		rv = errno;
650 	else
651 		*buflen -= n;
652 
653  out:
654 	close(fd);
655 	return rv;
656 }
657 
658 /*ARGSUSED*/
659 int
puffs_null_node_write(struct puffs_usermount * pu,puffs_cookie_t opc,uint8_t * buf,off_t offset,size_t * buflen,const struct puffs_cred * pcred,int ioflag)660 puffs_null_node_write(struct puffs_usermount *pu, puffs_cookie_t opc,
661 	uint8_t *buf, off_t offset, size_t *buflen,
662 	const struct puffs_cred *pcred, int ioflag)
663 {
664 	struct puffs_node *pn = opc;
665 	ssize_t n;
666 	off_t off;
667 	int fd, rv;
668 
669 	rv = 0;
670 	fd = writeableopen(PNPATH(pn));
671 	if (fd == -1)
672 		return errno;
673 
674 	off = lseek(fd, offset, SEEK_SET);
675 	if (off == -1) {
676 		rv = errno;
677 		goto out;
678 	}
679 
680 	n = write(fd, buf, *buflen);
681 	if (n == -1)
682 		rv = errno;
683 	else
684 		*buflen -= n;
685 
686  out:
687 	close(fd);
688 	return rv;
689 }
690