xref: /netbsd-src/lib/libpuffs/null.c (revision 1b9578b8c2c1f848eeb16dabbfd7d1f0d9fdefbd)
1 /*	$NetBSD: null.c,v 1.30 2011/06/27 12:06:19 manu 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.30 2011/06/27 12:06:19 manu 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 
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
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 *
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
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 overriden from the file system */
163 void
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
194 puffs_null_fs_statvfs(struct puffs_usermount *pu, struct statvfs *svfsb)
195 {
196 
197 	if (statvfs(PNPATH(puffs_getroot(pu)), svfsb) == -1)
198 		return errno;
199 
200 	return 0;
201 }
202 
203 /*
204  * XXX: this is the stupidest crap ever, but:
205  * getfh() returns the fhandle type, when we are expected to deliver
206  * the fid type.  Just adjust it a bit and stop whining.
207  *
208  * Yes, this really really needs fixing.  Yes, *REALLY*.
209  */
210 #define FHANDLE_HEADERLEN 8
211 struct kernfid {
212 	unsigned short	fid_len;		/* length of data in bytes */
213 	unsigned short	fid_reserved;		/* compat: historic align */
214 	char		fid_data[0];		/* data (variable length) */
215 };
216 
217 /*ARGSUSED*/
218 static void *
219 fhcmp(struct puffs_usermount *pu, struct puffs_node *pn, void *arg)
220 {
221 	struct kernfid *kf1, *kf2;
222 
223 	if ((kf1 = pn->pn_data) == NULL)
224 		return NULL;
225 	kf2 = arg;
226 
227 	if (kf1->fid_len != kf2->fid_len)
228 		return NULL;
229 
230 	/*LINTED*/
231 	if (memcmp(kf1, kf2, kf1->fid_len) == 0)
232 		return pn;
233 	return NULL;
234 }
235 
236 /*
237  * This routine only supports file handles which have been issued while
238  * the server was alive.  Not really stable ones, that is.
239  */
240 /*ARGSUSED*/
241 int
242 puffs_null_fs_fhtonode(struct puffs_usermount *pu, void *fid, size_t fidsize,
243 	struct puffs_newinfo *pni)
244 {
245 	struct puffs_node *pn_res;
246 
247 	pn_res = puffs_pn_nodewalk(pu, fhcmp, fid);
248 	if (pn_res == NULL)
249 		return ENOENT;
250 
251 	puffs_newinfo_setcookie(pni, pn_res);
252 	puffs_newinfo_setvtype(pni, pn_res->pn_va.va_type);
253 	puffs_newinfo_setsize(pni, (voff_t)pn_res->pn_va.va_size);
254 	puffs_newinfo_setrdev(pni, pn_res->pn_va.va_rdev);
255 	return 0;
256 }
257 
258 /*ARGSUSED*/
259 int
260 puffs_null_fs_nodetofh(struct puffs_usermount *pu, puffs_cookie_t opc,
261 	void *fid, size_t *fidsize)
262 {
263 	struct puffs_node *pn = opc;
264 	struct kernfid *kfid;
265 	void *bounce;
266 	int rv;
267 
268 	rv = 0;
269 	bounce = NULL;
270 	if (*fidsize) {
271 		bounce = malloc(*fidsize + FHANDLE_HEADERLEN);
272 		if (!bounce)
273 			return ENOMEM;
274 		*fidsize += FHANDLE_HEADERLEN;
275 	}
276 	if (getfh(PNPATH(pn), bounce, fidsize) == -1)
277 		rv = errno;
278 	else
279 		memcpy(fid, (uint8_t *)bounce + FHANDLE_HEADERLEN,
280 		    *fidsize - FHANDLE_HEADERLEN);
281 	kfid = fid;
282 	if (rv == 0) {
283 		*fidsize = kfid->fid_len;
284 		pn->pn_data = malloc(*fidsize);
285 		if (pn->pn_data == NULL)
286 			abort(); /* lazy */
287 		memcpy(pn->pn_data, fid, *fidsize);
288 	} else {
289 		*fidsize -= FHANDLE_HEADERLEN;
290 	}
291 	free(bounce);
292 
293 	return rv;
294 }
295 
296 int
297 puffs_null_node_lookup(struct puffs_usermount *pu, puffs_cookie_t opc,
298 	struct puffs_newinfo *pni, const struct puffs_cn *pcn)
299 {
300 	struct puffs_node *pn = opc, *pn_res;
301 	struct stat sb;
302 	int rv;
303 
304 	assert(pn->pn_va.va_type == VDIR);
305 
306 	/*
307 	 * Note to whoever is copypasting this: you must first check
308 	 * if the node is there and only then do nodewalk.  Alternatively
309 	 * you could make sure that you don't return unlinked/rmdir'd
310 	 * nodes in some other fashion
311 	 */
312 	rv = lstat(PCNPATH(pcn), &sb);
313 	if (rv)
314 		return errno;
315 
316 	/* XXX2: nodewalk is a bit too slow here */
317 	pn_res = puffs_pn_nodewalk(pu, inodecmp, &sb.st_ino);
318 
319 	if (pn_res == NULL) {
320 		pn_res = puffs_pn_new(pu, NULL);
321 		if (pn_res == NULL)
322 			return ENOMEM;
323 		puffs_stat2vattr(&pn_res->pn_va, &sb);
324 	}
325 
326 	puffs_newinfo_setcookie(pni, pn_res);
327 	puffs_newinfo_setvtype(pni, pn_res->pn_va.va_type);
328 	puffs_newinfo_setsize(pni, (voff_t)pn_res->pn_va.va_size);
329 	puffs_newinfo_setrdev(pni, pn_res->pn_va.va_rdev);
330 
331 	return 0;
332 }
333 
334 /*ARGSUSED*/
335 int
336 puffs_null_node_create(struct puffs_usermount *pu, puffs_cookie_t opc,
337 	struct puffs_newinfo *pni, const struct puffs_cn *pcn,
338 	const struct vattr *va)
339 {
340 	int fd, rv;
341 
342 	fd = open(PCNPATH(pcn), O_RDWR | O_CREAT | O_TRUNC);
343 	if (fd == -1)
344 		return errno;
345 	close(fd);
346 
347 	rv = makenode(pu, pni, pcn, va, 1);
348 	if (rv)
349 		unlink(PCNPATH(pcn));
350 	return rv;
351 }
352 
353 /*ARGSUSED*/
354 int
355 puffs_null_node_mknod(struct puffs_usermount *pu, puffs_cookie_t opc,
356 	struct puffs_newinfo *pni, const struct puffs_cn *pcn,
357 	const struct vattr *va)
358 {
359 	mode_t mode;
360 	int rv;
361 
362 	mode = puffs_addvtype2mode(va->va_mode, va->va_type);
363 	if (mknod(PCNPATH(pcn), mode, va->va_rdev) == -1)
364 		return errno;
365 
366 	rv = makenode(pu, pni, pcn, va, 0);
367 	if (rv)
368 		unlink(PCNPATH(pcn));
369 	return rv;
370 }
371 
372 /*ARGSUSED*/
373 int
374 puffs_null_node_getattr(struct puffs_usermount *pu, puffs_cookie_t opc,
375 	struct vattr *va, const struct puffs_cred *pcred)
376 {
377 	struct puffs_node *pn = opc;
378 	struct stat sb;
379 
380 	if (lstat(PNPATH(pn), &sb) == -1)
381 		return errno;
382 	puffs_stat2vattr(va, &sb);
383 
384 	return 0;
385 }
386 
387 /*ARGSUSED*/
388 int
389 puffs_null_node_setattr(struct puffs_usermount *pu, puffs_cookie_t opc,
390 	const struct vattr *va, const struct puffs_cred *pcred)
391 {
392 	struct puffs_node *pn = opc;
393 	int rv;
394 
395 	rv = processvattr(PNPATH(pn), va, pn->pn_va.va_type == VREG);
396 	if (rv)
397 		return rv;
398 
399 	puffs_setvattr(&pn->pn_va, va);
400 
401 	return 0;
402 }
403 
404 /*ARGSUSED*/
405 int
406 puffs_null_node_fsync(struct puffs_usermount *pu, puffs_cookie_t opc,
407 	const struct puffs_cred *pcred, int how,
408 	off_t offlo, off_t offhi)
409 {
410 	struct puffs_node *pn = opc;
411 	int fd, rv;
412 	int fflags;
413 	struct stat sb;
414 
415 	rv = 0;
416 	if (stat(PNPATH(pn), &sb) == -1)
417 		return errno;
418 	if (S_ISDIR(sb.st_mode)) {
419 		DIR *dirp;
420 		if ((dirp = opendir(PNPATH(pn))) == 0)
421 			return errno;
422 		fd = dirfd(dirp);
423 		if (fd == -1)
424 			return errno;
425 
426 		if (fsync(fd) == -1)
427 			rv = errno;
428 	} else {
429 		fd = writeableopen(PNPATH(pn));
430 		if (fd == -1)
431 			return errno;
432 
433 		if (how & PUFFS_FSYNC_DATAONLY)
434 			fflags = FDATASYNC;
435 		else
436 			fflags = FFILESYNC;
437 		if (how & PUFFS_FSYNC_CACHE)
438 			fflags |= FDISKSYNC;
439 
440 		if (fsync_range(fd, fflags, offlo, offhi - offlo) == -1)
441 			rv = errno;
442 	}
443 
444 	close(fd);
445 
446 	return rv;
447 }
448 
449 /*ARGSUSED*/
450 int
451 puffs_null_node_remove(struct puffs_usermount *pu, puffs_cookie_t opc,
452 	puffs_cookie_t targ, const struct puffs_cn *pcn)
453 {
454 	struct puffs_node *pn_targ = targ;
455 
456 	if (unlink(PNPATH(pn_targ)) == -1)
457 		return errno;
458 	puffs_pn_remove(pn_targ);
459 
460 	return 0;
461 }
462 
463 /*ARGSUSED*/
464 int
465 puffs_null_node_link(struct puffs_usermount *pu, puffs_cookie_t opc,
466 	puffs_cookie_t targ, const struct puffs_cn *pcn)
467 {
468 	struct puffs_node *pn_targ = targ;
469 
470 	if (link(PNPATH(pn_targ), PCNPATH(pcn)) == -1)
471 		return errno;
472 
473 	return 0;
474 }
475 
476 /*ARGSUSED*/
477 int
478 puffs_null_node_rename(struct puffs_usermount *pu, puffs_cookie_t opc,
479 	puffs_cookie_t src, const struct puffs_cn *pcn_src,
480 	puffs_cookie_t targ_dir, puffs_cookie_t targ,
481 	const struct puffs_cn *pcn_targ)
482 {
483 
484 	if (rename(PCNPATH(pcn_src), PCNPATH(pcn_targ)) == -1)
485 		return errno;
486 
487 	return 0;
488 }
489 
490 /*ARGSUSED*/
491 int
492 puffs_null_node_mkdir(struct puffs_usermount *pu, puffs_cookie_t opc,
493 	struct puffs_newinfo *pni, const struct puffs_cn *pcn,
494 	const struct vattr *va)
495 {
496 	int rv;
497 
498 	if (mkdir(PCNPATH(pcn), va->va_mode) == -1)
499 		return errno;
500 
501 	rv = makenode(pu, pni, pcn, va, 0);
502 	if (rv)
503 		rmdir(PCNPATH(pcn));
504 	return rv;
505 }
506 
507 /*ARGSUSED*/
508 int
509 puffs_null_node_rmdir(struct puffs_usermount *pu, puffs_cookie_t opc,
510 	puffs_cookie_t targ, const struct puffs_cn *pcn)
511 {
512 	struct puffs_node *pn_targ = targ;
513 
514 	if (rmdir(PNPATH(pn_targ)) == -1)
515 		return errno;
516 	puffs_pn_remove(pn_targ);
517 
518 	return 0;
519 }
520 
521 /*ARGSUSED*/
522 int
523 puffs_null_node_symlink(struct puffs_usermount *pu, puffs_cookie_t opc,
524 	struct puffs_newinfo *pni, const struct puffs_cn *pcn,
525 	const struct vattr *va, const char *linkname)
526 {
527 	int rv;
528 
529 	if (symlink(linkname, PCNPATH(pcn)) == -1)
530 		return errno;
531 
532 	rv = makenode(pu, pni, pcn, va, 0);
533 	if (rv)
534 		unlink(PCNPATH(pcn));
535 	return rv;
536 }
537 
538 /*ARGSUSED*/
539 int
540 puffs_null_node_readlink(struct puffs_usermount *pu, puffs_cookie_t opc,
541 	const struct puffs_cred *pcred, char *linkname, size_t *linklen)
542 {
543 	struct puffs_node *pn = opc;
544 	ssize_t rv;
545 
546 	rv = readlink(PNPATH(pn), linkname, *linklen);
547 	if (rv == -1)
548 		return errno;
549 
550 	*linklen = rv;
551 	return 0;
552 }
553 
554 /*ARGSUSED*/
555 int
556 puffs_null_node_readdir(struct puffs_usermount *pu, puffs_cookie_t opc,
557 	struct dirent *de, off_t *off, size_t *reslen,
558 	const struct puffs_cred *pcred, int *eofflag, off_t *cookies,
559 	size_t *ncookies)
560 {
561 	struct puffs_node *pn = opc;
562 	struct dirent entry, *result;
563 	DIR *dp;
564 	off_t i;
565 	int rv;
566 
567 	*ncookies = 0;
568 	dp = opendir(PNPATH(pn));
569 	if (dp == NULL)
570 		return errno;
571 
572 	rv = 0;
573 	i = *off;
574 
575 	/*
576 	 * XXX: need to do trickery here, telldir/seekdir would be nice, but
577 	 * then we'd need to keep state, which I'm too lazy to keep
578 	 */
579 	while (i--) {
580 		rv = readdir_r(dp, &entry, &result);
581 		if (rv != 0)
582 			goto out;
583 
584 		if (!result) {
585 			*eofflag = 1;
586 			goto out;
587 		}
588 	}
589 
590 	for (;;) {
591 		rv = readdir_r(dp, &entry, &result);
592 		if (rv != 0)
593 			goto out;
594 
595 		if (!result) {
596 			*eofflag = 1;
597 			goto out;
598 		}
599 
600 		if (_DIRENT_SIZE(result) > *reslen)
601 			goto out;
602 
603 		*de = *result;
604 		*reslen -= _DIRENT_SIZE(result);
605 		de = _DIRENT_NEXT(de);
606 
607 		(*off)++;
608 		PUFFS_STORE_DCOOKIE(cookies, ncookies, *off);
609 	}
610 
611  out:
612 	closedir(dp);
613 	return 0;
614 }
615 
616 /*ARGSUSED*/
617 int
618 puffs_null_node_read(struct puffs_usermount *pu, puffs_cookie_t opc,
619 	uint8_t *buf, off_t offset, size_t *buflen,
620 	const struct puffs_cred *pcred, int ioflag)
621 {
622 	struct puffs_node *pn = opc;
623 	ssize_t n;
624 	off_t off;
625 	int fd, rv;
626 
627 	rv = 0;
628 	fd = open(PNPATH(pn), O_RDONLY);
629 	if (fd == -1)
630 		return errno;
631 	off = lseek(fd, offset, SEEK_SET);
632 	if (off == -1) {
633 		rv = errno;
634 		goto out;
635 	}
636 
637 	n = read(fd, buf, *buflen);
638 	if (n == -1)
639 		rv = errno;
640 	else
641 		*buflen -= n;
642 
643  out:
644 	close(fd);
645 	return rv;
646 }
647 
648 /*ARGSUSED*/
649 int
650 puffs_null_node_write(struct puffs_usermount *pu, puffs_cookie_t opc,
651 	uint8_t *buf, off_t offset, size_t *buflen,
652 	const struct puffs_cred *pcred, int ioflag)
653 {
654 	struct puffs_node *pn = opc;
655 	ssize_t n;
656 	off_t off;
657 	int fd, rv;
658 
659 	rv = 0;
660 	fd = writeableopen(PNPATH(pn));
661 	if (fd == -1)
662 		return errno;
663 
664 	off = lseek(fd, offset, SEEK_SET);
665 	if (off == -1) {
666 		rv = errno;
667 		goto out;
668 	}
669 
670 	n = write(fd, buf, *buflen);
671 	if (n == -1)
672 		rv = errno;
673 	else
674 		*buflen -= n;
675 
676  out:
677 	close(fd);
678 	return rv;
679 }
680