Home | History | Annotate | Line # | Download | only in libpuffs
null.c revision 1.25
      1 /*	$NetBSD: null.c,v 1.25 2008/08/12 19:44:39 pooka 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.25 2008/08/12 19:44:39 pooka 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/time.h>
     40 
     41 #include <assert.h>
     42 #include <dirent.h>
     43 #include <errno.h>
     44 #include <fcntl.h>
     45 #include <puffs.h>
     46 #include <stdio.h>
     47 #include <stdlib.h>
     48 #include <unistd.h>
     49 
     50 PUFFSOP_PROTOS(puffs_null)
     51 
     52 /*
     53  * set attributes to what is specified.  XXX: no rollback in case of failure
     54  */
     55 static int
     56 processvattr(const char *path, const struct vattr *va, int regular)
     57 {
     58 	struct timeval tv[2];
     59 
     60 	/* XXX: -1 == PUFFS_VNOVAL, but shouldn't trust that */
     61 	if (va->va_uid != (unsigned)-1 || va->va_gid != (unsigned)-1)
     62 		if (lchown(path, va->va_uid, va->va_gid) == -1)
     63 			return errno;
     64 
     65 	if (va->va_mode != (unsigned)PUFFS_VNOVAL)
     66 		if (lchmod(path, va->va_mode) == -1)
     67 			return errno;
     68 
     69 	/* sloppy */
     70 	if (va->va_atime.tv_sec != (unsigned)PUFFS_VNOVAL
     71 	    || va->va_mtime.tv_sec != (unsigned)PUFFS_VNOVAL) {
     72 		TIMESPEC_TO_TIMEVAL(&tv[0], &va->va_atime);
     73 		TIMESPEC_TO_TIMEVAL(&tv[1], &va->va_mtime);
     74 
     75 		if (lutimes(path, tv) == -1)
     76 			return errno;
     77 	}
     78 
     79 	if (regular && va->va_size != (u_quad_t)PUFFS_VNOVAL)
     80 		if (truncate(path, (off_t)va->va_size) == -1)
     81 			return errno;
     82 
     83 	return 0;
     84 }
     85 
     86 /*
     87  * Kludge to open files which aren't writable *any longer*.  This kinda
     88  * works because the vfs layer does validation checks based on the file's
     89  * permissions to allow writable opening before opening them.  However,
     90  * the problem arises if we want to create a file, write to it (cache),
     91  * adjust permissions and then flush the file.
     92  */
     93 static int
     94 writeableopen(const char *path)
     95 {
     96 	struct stat sb;
     97 	mode_t origmode;
     98 	int sverr = 0;
     99 	int fd;
    100 
    101 	fd = open(path, O_WRONLY);
    102 	if (fd == -1) {
    103 		if (errno == EACCES) {
    104 			if (stat(path, &sb) == -1)
    105 				return -1;
    106 			origmode = sb.st_mode & ALLPERMS;
    107 
    108 			if (chmod(path, 0200) == -1)
    109 				return -1;
    110 
    111 			fd = open(path, O_WRONLY);
    112 			if (fd == -1)
    113 				sverr = errno;
    114 
    115 			chmod(path, origmode);
    116 			if (sverr)
    117 				errno = sverr;
    118 		} else
    119 			return -1;
    120 	}
    121 
    122 	return fd;
    123 }
    124 
    125 /*ARGSUSED*/
    126 static void *
    127 inodecmp(struct puffs_usermount *pu, struct puffs_node *pn, void *arg)
    128 {
    129 	ino_t *cmpino = arg;
    130 
    131 	if (pn->pn_va.va_fileid == *cmpino)
    132 		return pn;
    133 	return NULL;
    134 }
    135 
    136 static int
    137 makenode(struct puffs_usermount *pu, struct puffs_newinfo *pni,
    138 	const struct puffs_cn *pcn, const struct vattr *va, int regular)
    139 {
    140 	struct puffs_node *pn;
    141 	struct stat sb;
    142 	int rv;
    143 
    144 	if ((rv = processvattr(PCNPATH(pcn), va, regular)) != 0)
    145 		return rv;
    146 
    147 	pn = puffs_pn_new(pu, NULL);
    148 	if (!pn)
    149 		return ENOMEM;
    150 	puffs_setvattr(&pn->pn_va, va);
    151 
    152 	if (lstat(PCNPATH(pcn), &sb) == -1)
    153 		return errno;
    154 	puffs_stat2vattr(&pn->pn_va, &sb);
    155 
    156 	puffs_newinfo_setcookie(pni, pn);
    157 	return 0;
    158 }
    159 
    160 /* This should be called first and overriden from the file system */
    161 void
    162 puffs_null_setops(struct puffs_ops *pops)
    163 {
    164 
    165 	PUFFSOP_SET(pops, puffs_null, fs, statvfs);
    166 	PUFFSOP_SETFSNOP(pops, unmount);
    167 	PUFFSOP_SETFSNOP(pops, sync);
    168 
    169 	PUFFSOP_SET(pops, puffs_null, node, lookup);
    170 	PUFFSOP_SET(pops, puffs_null, node, create);
    171 	PUFFSOP_SET(pops, puffs_null, node, mknod);
    172 	PUFFSOP_SET(pops, puffs_null, node, getattr);
    173 	PUFFSOP_SET(pops, puffs_null, node, setattr);
    174 	PUFFSOP_SET(pops, puffs_null, node, fsync);
    175 	PUFFSOP_SET(pops, puffs_null, node, remove);
    176 	PUFFSOP_SET(pops, puffs_null, node, link);
    177 	PUFFSOP_SET(pops, puffs_null, node, rename);
    178 	PUFFSOP_SET(pops, puffs_null, node, mkdir);
    179 	PUFFSOP_SET(pops, puffs_null, node, rmdir);
    180 	PUFFSOP_SET(pops, puffs_null, node, symlink);
    181 	PUFFSOP_SET(pops, puffs_null, node, readlink);
    182 	PUFFSOP_SET(pops, puffs_null, node, readdir);
    183 	PUFFSOP_SET(pops, puffs_null, node, read);
    184 	PUFFSOP_SET(pops, puffs_null, node, write);
    185 	PUFFSOP_SET(pops, puffs_genfs, node, reclaim);
    186 }
    187 
    188 /*ARGSUSED*/
    189 int
    190 puffs_null_fs_statvfs(struct puffs_usermount *pu, struct statvfs *svfsb)
    191 {
    192 
    193 	if (statvfs(PNPATH(puffs_getroot(pu)), svfsb) == -1)
    194 		return errno;
    195 
    196 	return 0;
    197 }
    198 
    199 int
    200 puffs_null_node_lookup(struct puffs_usermount *pu, puffs_cookie_t opc,
    201 	struct puffs_newinfo *pni, const struct puffs_cn *pcn)
    202 {
    203 	struct puffs_node *pn = opc, *pn_res;
    204 	struct stat sb;
    205 	int rv;
    206 
    207 	assert(pn->pn_va.va_type == VDIR);
    208 
    209 	/*
    210 	 * Note to whoever is copypasting this: you must first check
    211 	 * if the node is there and only then do nodewalk.  Alternatively
    212 	 * you could make sure that you don't return unlinked/rmdir'd
    213 	 * nodes in some other fashion
    214 	 */
    215 	rv = lstat(PCNPATH(pcn), &sb);
    216 	if (rv)
    217 		return errno;
    218 
    219 	/* XXX2: nodewalk is a bit too slow here */
    220 	pn_res = puffs_pn_nodewalk(pu, inodecmp, &sb.st_ino);
    221 
    222 	if (pn_res == NULL) {
    223 		pn_res = puffs_pn_new(pu, NULL);
    224 		if (pn_res == NULL)
    225 			return ENOMEM;
    226 		puffs_stat2vattr(&pn_res->pn_va, &sb);
    227 	}
    228 
    229 	puffs_newinfo_setcookie(pni, pn_res);
    230 	puffs_newinfo_setvtype(pni, pn_res->pn_va.va_type);
    231 	puffs_newinfo_setsize(pni, (voff_t)pn_res->pn_va.va_size);
    232 	puffs_newinfo_setrdev(pni, pn_res->pn_va.va_rdev);
    233 
    234 	return 0;
    235 }
    236 
    237 /*ARGSUSED*/
    238 int
    239 puffs_null_node_create(struct puffs_usermount *pu, puffs_cookie_t opc,
    240 	struct puffs_newinfo *pni, const struct puffs_cn *pcn,
    241 	const struct vattr *va)
    242 {
    243 	int fd, rv;
    244 
    245 	fd = open(PCNPATH(pcn), O_RDWR | O_CREAT | O_TRUNC);
    246 	if (fd == -1)
    247 		return errno;
    248 	close(fd);
    249 
    250 	rv = makenode(pu, pni, pcn, va, 1);
    251 	if (rv)
    252 		unlink(PCNPATH(pcn));
    253 	return rv;
    254 }
    255 
    256 /*ARGSUSED*/
    257 int
    258 puffs_null_node_mknod(struct puffs_usermount *pu, puffs_cookie_t opc,
    259 	struct puffs_newinfo *pni, const struct puffs_cn *pcn,
    260 	const struct vattr *va)
    261 {
    262 	mode_t mode;
    263 	int rv;
    264 
    265 	mode = puffs_addvtype2mode(va->va_mode, va->va_type);
    266 	if (mknod(PCNPATH(pcn), mode, va->va_rdev) == -1)
    267 		return errno;
    268 
    269 	rv = makenode(pu, pni, pcn, va, 0);
    270 	if (rv)
    271 		unlink(PCNPATH(pcn));
    272 	return rv;
    273 }
    274 
    275 /*ARGSUSED*/
    276 int
    277 puffs_null_node_getattr(struct puffs_usermount *pu, puffs_cookie_t opc,
    278 	struct vattr *va, const struct puffs_cred *pcred)
    279 {
    280 	struct puffs_node *pn = opc;
    281 	struct stat sb;
    282 
    283 	if (lstat(PNPATH(pn), &sb) == -1)
    284 		return errno;
    285 	puffs_stat2vattr(va, &sb);
    286 
    287 	return 0;
    288 }
    289 
    290 /*ARGSUSED*/
    291 int
    292 puffs_null_node_setattr(struct puffs_usermount *pu, puffs_cookie_t opc,
    293 	const struct vattr *va, const struct puffs_cred *pcred)
    294 {
    295 	struct puffs_node *pn = opc;
    296 	int rv;
    297 
    298 	rv = processvattr(PNPATH(pn), va, pn->pn_va.va_type == VREG);
    299 	if (rv)
    300 		return rv;
    301 
    302 	puffs_setvattr(&pn->pn_va, va);
    303 
    304 	return 0;
    305 }
    306 
    307 /*ARGSUSED*/
    308 int
    309 puffs_null_node_fsync(struct puffs_usermount *pu, puffs_cookie_t opc,
    310 	const struct puffs_cred *pcred, int how,
    311 	off_t offlo, off_t offhi)
    312 {
    313 	struct puffs_node *pn = opc;
    314 	int fd, rv;
    315 	int fflags;
    316 
    317 	rv = 0;
    318 	fd = writeableopen(PNPATH(pn));
    319 	if (fd == -1)
    320 		return errno;
    321 
    322 	if (how & PUFFS_FSYNC_DATAONLY)
    323 		fflags = FDATASYNC;
    324 	else
    325 		fflags = FFILESYNC;
    326 	if (how & PUFFS_FSYNC_CACHE)
    327 		fflags |= FDISKSYNC;
    328 
    329 	if (fsync_range(fd, fflags, offlo, offhi - offlo) == -1)
    330 		rv = errno;
    331 
    332 	close(fd);
    333 
    334 	return rv;
    335 }
    336 
    337 /*ARGSUSED*/
    338 int
    339 puffs_null_node_remove(struct puffs_usermount *pu, puffs_cookie_t opc,
    340 	puffs_cookie_t targ, const struct puffs_cn *pcn)
    341 {
    342 	struct puffs_node *pn_targ = targ;
    343 
    344 	if (unlink(PNPATH(pn_targ)) == -1)
    345 		return errno;
    346 	puffs_pn_remove(pn_targ);
    347 
    348 	return 0;
    349 }
    350 
    351 /*ARGSUSED*/
    352 int
    353 puffs_null_node_link(struct puffs_usermount *pu, puffs_cookie_t opc,
    354 	puffs_cookie_t targ, const struct puffs_cn *pcn)
    355 {
    356 	struct puffs_node *pn_targ = targ;
    357 
    358 	if (link(PNPATH(pn_targ), PCNPATH(pcn)) == -1)
    359 		return errno;
    360 
    361 	return 0;
    362 }
    363 
    364 /*ARGSUSED*/
    365 int
    366 puffs_null_node_rename(struct puffs_usermount *pu, puffs_cookie_t opc,
    367 	puffs_cookie_t src, const struct puffs_cn *pcn_src,
    368 	puffs_cookie_t targ_dir, puffs_cookie_t targ,
    369 	const struct puffs_cn *pcn_targ)
    370 {
    371 
    372 	if (rename(PCNPATH(pcn_src), PCNPATH(pcn_targ)) == -1)
    373 		return errno;
    374 
    375 	return 0;
    376 }
    377 
    378 /*ARGSUSED*/
    379 int
    380 puffs_null_node_mkdir(struct puffs_usermount *pu, puffs_cookie_t opc,
    381 	struct puffs_newinfo *pni, const struct puffs_cn *pcn,
    382 	const struct vattr *va)
    383 {
    384 	int rv;
    385 
    386 	if (mkdir(PCNPATH(pcn), va->va_mode) == -1)
    387 		return errno;
    388 
    389 	rv = makenode(pu, pni, pcn, va, 0);
    390 	if (rv)
    391 		rmdir(PCNPATH(pcn));
    392 	return rv;
    393 }
    394 
    395 /*ARGSUSED*/
    396 int
    397 puffs_null_node_rmdir(struct puffs_usermount *pu, puffs_cookie_t opc,
    398 	puffs_cookie_t targ, const struct puffs_cn *pcn)
    399 {
    400 	struct puffs_node *pn_targ = targ;
    401 
    402 	if (rmdir(PNPATH(pn_targ)) == -1)
    403 		return errno;
    404 	puffs_pn_remove(pn_targ);
    405 
    406 	return 0;
    407 }
    408 
    409 /*ARGSUSED*/
    410 int
    411 puffs_null_node_symlink(struct puffs_usermount *pu, puffs_cookie_t opc,
    412 	struct puffs_newinfo *pni, const struct puffs_cn *pcn,
    413 	const struct vattr *va, const char *linkname)
    414 {
    415 	int rv;
    416 
    417 	if (symlink(linkname, PCNPATH(pcn)) == -1)
    418 		return errno;
    419 
    420 	rv = makenode(pu, pni, pcn, va, 0);
    421 	if (rv)
    422 		unlink(PCNPATH(pcn));
    423 	return rv;
    424 }
    425 
    426 /*ARGSUSED*/
    427 int
    428 puffs_null_node_readlink(struct puffs_usermount *pu, puffs_cookie_t opc,
    429 	const struct puffs_cred *pcred, char *linkname, size_t *linklen)
    430 {
    431 	struct puffs_node *pn = opc;
    432 	ssize_t rv;
    433 
    434 	rv = readlink(PNPATH(pn), linkname, *linklen);
    435 	if (rv == -1)
    436 		return errno;
    437 
    438 	*linklen = rv;
    439 	return 0;
    440 }
    441 
    442 /*ARGSUSED*/
    443 int
    444 puffs_null_node_readdir(struct puffs_usermount *pu, puffs_cookie_t opc,
    445 	struct dirent *de, off_t *off, size_t *reslen,
    446 	const struct puffs_cred *pcred, int *eofflag, off_t *cookies,
    447 	size_t *ncookies)
    448 {
    449 	struct puffs_node *pn = opc;
    450 	struct dirent entry, *result;
    451 	DIR *dp;
    452 	off_t i;
    453 	int rv;
    454 
    455 	dp = opendir(PNPATH(pn));
    456 	if (dp == NULL)
    457 		return errno;
    458 
    459 	rv = 0;
    460 	i = *off;
    461 
    462 	/*
    463 	 * XXX: need to do trickery here, telldir/seekdir would be nice, but
    464 	 * then we'd need to keep state, which I'm too lazy to keep
    465 	 */
    466 	while (i--) {
    467 		rv = readdir_r(dp, &entry, &result);
    468 		if (rv || !result)
    469 			goto out;
    470 	}
    471 
    472 	for (;;) {
    473 		rv = readdir_r(dp, &entry, &result);
    474 		if (rv != 0)
    475 			goto out;
    476 
    477 		if (!result)
    478 			goto out;
    479 
    480 		if (_DIRENT_SIZE(result) > *reslen)
    481 			goto out;
    482 
    483 		*de = *result;
    484 		*reslen -= _DIRENT_SIZE(result);
    485 		de = _DIRENT_NEXT(de);
    486 
    487 		(*off)++;
    488 	}
    489 
    490  out:
    491 	closedir(dp);
    492 	return 0;
    493 }
    494 
    495 /*ARGSUSED*/
    496 int
    497 puffs_null_node_read(struct puffs_usermount *pu, puffs_cookie_t opc,
    498 	uint8_t *buf, off_t offset, size_t *buflen,
    499 	const struct puffs_cred *pcred, int ioflag)
    500 {
    501 	struct puffs_node *pn = opc;
    502 	ssize_t n;
    503 	off_t off;
    504 	int fd, rv;
    505 
    506 	rv = 0;
    507 	fd = open(PNPATH(pn), O_RDONLY);
    508 	if (fd == -1)
    509 		return errno;
    510 	off = lseek(fd, offset, SEEK_SET);
    511 	if (off == -1) {
    512 		rv = errno;
    513 		goto out;
    514 	}
    515 
    516 	n = read(fd, buf, *buflen);
    517 	if (n == -1)
    518 		rv = errno;
    519 	else
    520 		*buflen -= n;
    521 
    522  out:
    523 	close(fd);
    524 	return rv;
    525 }
    526 
    527 /*ARGSUSED*/
    528 int
    529 puffs_null_node_write(struct puffs_usermount *pu, puffs_cookie_t opc,
    530 	uint8_t *buf, off_t offset, size_t *buflen,
    531 	const struct puffs_cred *pcred, int ioflag)
    532 {
    533 	struct puffs_node *pn = opc;
    534 	ssize_t n;
    535 	off_t off;
    536 	int fd, rv;
    537 
    538 	rv = 0;
    539 	fd = writeableopen(PNPATH(pn));
    540 	if (fd == -1)
    541 		return errno;
    542 
    543 	off = lseek(fd, offset, SEEK_SET);
    544 	if (off == -1) {
    545 		rv = errno;
    546 		goto out;
    547 	}
    548 
    549 	n = write(fd, buf, *buflen);
    550 	if (n == -1)
    551 		rv = errno;
    552 	else
    553 		*buflen -= n;
    554 
    555  out:
    556 	close(fd);
    557 	return rv;
    558 }
    559