Home | History | Annotate | Line # | Download | only in libpuffs
null.c revision 1.17
      1 /*	$NetBSD: null.c,v 1.17 2007/06/24 18:42:25 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.17 2007/06/24 18:42:25 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 /* This should be called first and overriden from the file system */
    137 void
    138 puffs_null_setops(struct puffs_ops *pops)
    139 {
    140 
    141 	PUFFSOP_SET(pops, puffs_null, fs, statvfs);
    142 	PUFFSOP_SETFSNOP(pops, unmount);
    143 	PUFFSOP_SETFSNOP(pops, sync);
    144 
    145 	PUFFSOP_SET(pops, puffs_null, node, lookup);
    146 	PUFFSOP_SET(pops, puffs_null, node, create);
    147 	PUFFSOP_SET(pops, puffs_null, node, mknod);
    148 	PUFFSOP_SET(pops, puffs_null, node, getattr);
    149 	PUFFSOP_SET(pops, puffs_null, node, setattr);
    150 	PUFFSOP_SET(pops, puffs_null, node, fsync);
    151 	PUFFSOP_SET(pops, puffs_null, node, remove);
    152 	PUFFSOP_SET(pops, puffs_null, node, link);
    153 	PUFFSOP_SET(pops, puffs_null, node, rename);
    154 	PUFFSOP_SET(pops, puffs_null, node, mkdir);
    155 	PUFFSOP_SET(pops, puffs_null, node, rmdir);
    156 	PUFFSOP_SET(pops, puffs_null, node, symlink);
    157 	PUFFSOP_SET(pops, puffs_null, node, readlink);
    158 	PUFFSOP_SET(pops, puffs_null, node, readdir);
    159 	PUFFSOP_SET(pops, puffs_null, node, read);
    160 	PUFFSOP_SET(pops, puffs_null, node, write);
    161 	PUFFSOP_SET(pops, puffs_null, node, reclaim);
    162 }
    163 
    164 /*ARGSUSED*/
    165 int
    166 puffs_null_fs_statvfs(struct puffs_cc *pcc, struct statvfs *svfsb, pid_t pid)
    167 {
    168 	struct puffs_usermount *pu = puffs_cc_getusermount(pcc);
    169 
    170 	if (statvfs(PNPATH(puffs_getroot(pu)), svfsb) == -1)
    171 		return errno;
    172 
    173 	return 0;
    174 }
    175 
    176 int
    177 puffs_null_node_lookup(struct puffs_cc *pcc, void *opc, void **newnode,
    178 	enum vtype *newtype, voff_t *newsize, dev_t *newrdev,
    179 	const struct puffs_cn *pcn)
    180 {
    181 	struct puffs_usermount *pu = puffs_cc_getusermount(pcc);
    182 	struct puffs_node *pn = opc, *pn_res;
    183 	struct stat sb;
    184 	int rv;
    185 
    186 	assert(pn->pn_va.va_type == VDIR);
    187 
    188 	/*
    189 	 * Note to whoever is copypasting this: you must first check
    190 	 * if the node is there and only then do nodewalk.  Alternatively
    191 	 * you could make sure that you don't return unlinked/rmdir'd
    192 	 * nodes in some other fashion
    193 	 */
    194 	rv = lstat(PCNPATH(pcn), &sb);
    195 	if (rv)
    196 		return errno;
    197 
    198 	/* XXX2: nodewalk is a bit too slow here */
    199 	pn_res = puffs_pn_nodewalk(pu, inodecmp, &sb.st_ino);
    200 
    201 	if (pn_res == NULL) {
    202 		pn_res = puffs_pn_new(pu, NULL);
    203 		if (pn_res == NULL)
    204 			return ENOMEM;
    205 		puffs_stat2vattr(&pn_res->pn_va, &sb);
    206 	}
    207 
    208 	*newnode = pn_res;
    209 	*newtype = pn_res->pn_va.va_type;
    210 	*newsize = pn_res->pn_va.va_size;
    211 	*newrdev = pn_res->pn_va.va_rdev;
    212 
    213 	return 0;
    214 }
    215 
    216 /*ARGSUSED*/
    217 int
    218 puffs_null_node_create(struct puffs_cc *pcc, void *opc, void **newnode,
    219 	const struct puffs_cn *pcn, const struct vattr *va)
    220 {
    221 	struct puffs_usermount *pu = puffs_cc_getusermount(pcc);
    222 	struct puffs_node *pn;
    223 	int fd, rv;
    224 
    225 	fd = open(PCNPATH(pcn), O_RDWR | O_CREAT | O_TRUNC);
    226 	if (fd == -1)
    227 		return errno;
    228 	close(fd);
    229 	if ((rv = processvattr(PCNPATH(pcn), va, 1)) != 0) {
    230 		unlink(PCNPATH(pcn));
    231 		return rv;
    232 	}
    233 
    234 	pn = puffs_pn_new(pu, NULL);
    235 	if (!pn) {
    236 		unlink(PCNPATH(pcn));
    237 		return ENOMEM;
    238 	}
    239 	puffs_setvattr(&pn->pn_va, va);
    240 
    241 	*newnode = pn;
    242 	return 0;
    243 }
    244 
    245 /*ARGSUSED*/
    246 int
    247 puffs_null_node_mknod(struct puffs_cc *pcc, void *opc, void **newnode,
    248 	const struct puffs_cn *pcn, const struct vattr *va)
    249 {
    250 	struct puffs_usermount *pu = puffs_cc_getusermount(pcc);
    251 	struct puffs_node *pn;
    252 	mode_t mode;
    253 	int rv;
    254 
    255 	mode = puffs_addvtype2mode(va->va_mode, va->va_type);
    256 	if (mknod(PCNPATH(pcn), mode, va->va_rdev) == -1)
    257 		return errno;
    258 
    259 	if ((rv = processvattr(PCNPATH(pcn), va, 0)) != 0) {
    260 		unlink(PCNPATH(pcn));
    261 		return rv;
    262 	}
    263 
    264 	pn = puffs_pn_new(pu, NULL);
    265 	if (!pn) {
    266 		unlink(PCNPATH(pcn));
    267 		return ENOMEM;
    268 	}
    269 	puffs_setvattr(&pn->pn_va, va);
    270 
    271 	*newnode = pn;
    272 	return 0;
    273 }
    274 
    275 /*ARGSUSED*/
    276 int
    277 puffs_null_node_getattr(struct puffs_cc *pcc, void *opc, struct vattr *va,
    278 	const struct puffs_cred *pcred, pid_t pid)
    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_cc *pcc, void *opc,
    293 	const struct vattr *va, const struct puffs_cred *pcred, pid_t pid)
    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_cc *pcc, void *opc,
    310 	const struct puffs_cred *pcred, int how,
    311 	off_t offlo, off_t offhi, pid_t pid)
    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_cc *pcc, void *opc, void *targ,
    340 	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_cc *pcc, void *opc, void *targ,
    354 	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_cc *pcc, void *opc, void *src,
    367 	const struct puffs_cn *pcn_src, void *targ_dir, void *targ,
    368 	const struct puffs_cn *pcn_targ)
    369 {
    370 
    371 	if (rename(PCNPATH(pcn_src), PCNPATH(pcn_targ)) == -1)
    372 		return errno;
    373 
    374 	return 0;
    375 }
    376 
    377 /*ARGSUSED*/
    378 int
    379 puffs_null_node_mkdir(struct puffs_cc *pcc, void *opc, void **newnode,
    380 	const struct puffs_cn *pcn, const struct vattr *va)
    381 {
    382 	struct puffs_usermount *pu = puffs_cc_getusermount(pcc);
    383 	struct puffs_node *pn;
    384 	int rv;
    385 
    386 	if (mkdir(PCNPATH(pcn), va->va_mode) == -1)
    387 		return errno;
    388 
    389 	if ((rv = processvattr(PCNPATH(pcn), va, 0)) != 0) {
    390 		rmdir(PCNPATH(pcn));
    391 		return rv;
    392 	}
    393 
    394 	pn = puffs_pn_new(pu, NULL);
    395 	if (pn == NULL) {
    396 		rmdir(PCNPATH(pcn));
    397 		return ENOMEM;
    398 	}
    399 	puffs_setvattr(&pn->pn_va, va);
    400 
    401 	*newnode = pn;
    402 	return 0;
    403 }
    404 
    405 /*ARGSUSED*/
    406 int
    407 puffs_null_node_rmdir(struct puffs_cc *pcc, void *opc, void *targ,
    408 	const struct puffs_cn *pcn)
    409 {
    410 	struct puffs_node *pn_targ = targ;
    411 
    412 	if (rmdir(PNPATH(pn_targ)) == -1)
    413 		return errno;
    414 	puffs_pn_remove(pn_targ);
    415 
    416 	return 0;
    417 }
    418 
    419 /*ARGSUSED*/
    420 int
    421 puffs_null_node_symlink(struct puffs_cc *pcc, void *opc, void **newnode,
    422 	const struct puffs_cn *pcn, const struct vattr *va,
    423 	const char *linkname)
    424 {
    425 	struct puffs_usermount *pu = puffs_cc_getusermount(pcc);
    426 	struct puffs_node *pn;
    427 	int rv;
    428 
    429 	if (symlink(linkname, PCNPATH(pcn)) == -1)
    430 		return errno;
    431 
    432 	if ((rv = processvattr(PCNPATH(pcn), va, 0)) != 0) {
    433 		unlink(PCNPATH(pcn));
    434 		return rv;
    435 	}
    436 
    437 	pn = puffs_pn_new(pu, NULL);
    438 	if (pn == NULL) {
    439 		rmdir(PCNPATH(pcn));
    440 		return ENOMEM;
    441 	}
    442 	puffs_setvattr(&pn->pn_va, va);
    443 
    444 	*newnode = pn;
    445 	return 0;
    446 }
    447 
    448 /*ARGSUSED*/
    449 int
    450 puffs_null_node_readlink(struct puffs_cc *pcc, void *opc,
    451 	const struct puffs_cred *pcred, char *linkname, size_t *linklen)
    452 {
    453 	struct puffs_node *pn = opc;
    454 	ssize_t rv;
    455 
    456 	rv = readlink(PNPATH(pn), linkname, *linklen);
    457 	if (rv == -1)
    458 		return errno;
    459 
    460 	*linklen = rv;
    461 	return 0;
    462 }
    463 
    464 /*ARGSUSED*/
    465 int
    466 puffs_null_node_readdir(struct puffs_cc *pcc, void *opc, struct dirent *de,
    467 	off_t *off, size_t *reslen, const struct puffs_cred *pcred,
    468 	int *eofflag, off_t *cookies, size_t *ncookies)
    469 {
    470 	struct puffs_node *pn = opc;
    471 	struct dirent entry, *result;
    472 	DIR *dp;
    473 	off_t i;
    474 	int rv;
    475 
    476 	dp = opendir(PNPATH(pn));
    477 	if (dp == NULL)
    478 		return errno;
    479 
    480 	rv = 0;
    481 	i = *off;
    482 
    483 	/*
    484 	 * XXX: need to do trickery here, telldir/seekdir would be nice, but
    485 	 * then we'd need to keep state, which I'm too lazy to keep
    486 	 */
    487 	while (i--) {
    488 		rv = readdir_r(dp, &entry, &result);
    489 		if (rv || !result)
    490 			goto out;
    491 	}
    492 
    493 	for (;;) {
    494 		rv = readdir_r(dp, &entry, &result);
    495 		if (rv != 0)
    496 			goto out;
    497 
    498 		if (!result)
    499 			goto out;
    500 
    501 		if (_DIRENT_SIZE(result) > *reslen)
    502 			goto out;
    503 
    504 		*de = *result;
    505 		*reslen -= _DIRENT_SIZE(result);
    506 		de = _DIRENT_NEXT(de);
    507 
    508 		(*off)++;
    509 	}
    510 
    511  out:
    512 	closedir(dp);
    513 	return 0;
    514 }
    515 
    516 /*ARGSUSED*/
    517 int
    518 puffs_null_node_read(struct puffs_cc *pcc, void *opc, uint8_t *buf,
    519 	off_t offset, size_t *buflen, const struct puffs_cred *pcred,
    520 	int ioflag)
    521 {
    522 	struct puffs_node *pn = opc;
    523 	ssize_t n;
    524 	off_t off;
    525 	int fd, rv;
    526 
    527 	rv = 0;
    528 	fd = open(PNPATH(pn), O_RDONLY);
    529 	if (fd == -1)
    530 		return errno;
    531 	off = lseek(fd, offset, SEEK_SET);
    532 	if (off == -1) {
    533 		rv = errno;
    534 		goto out;
    535 	}
    536 
    537 	n = read(fd, buf, *buflen);
    538 	if (n == -1)
    539 		rv = errno;
    540 	else
    541 		*buflen -= n;
    542 
    543  out:
    544 	close(fd);
    545 	return rv;
    546 }
    547 
    548 /*ARGSUSED*/
    549 int
    550 puffs_null_node_write(struct puffs_cc *pcc, void *opc, uint8_t *buf,
    551 	off_t offset, size_t *buflen, const struct puffs_cred *pcred,
    552 	int ioflag)
    553 {
    554 	struct puffs_node *pn = opc;
    555 	ssize_t n;
    556 	off_t off;
    557 	int fd, rv;
    558 
    559 	rv = 0;
    560 	fd = writeableopen(PNPATH(pn));
    561 	if (fd == -1)
    562 		return errno;
    563 
    564 	off = lseek(fd, offset, SEEK_SET);
    565 	if (off == -1) {
    566 		rv = errno;
    567 		goto out;
    568 	}
    569 
    570 	n = write(fd, buf, *buflen);
    571 	if (n == -1)
    572 		rv = errno;
    573 	else
    574 		*buflen -= n;
    575 
    576  out:
    577 	close(fd);
    578 	return rv;
    579 }
    580 
    581 /*ARGSUSED*/
    582 int
    583 puffs_null_node_reclaim(struct puffs_cc *pcc, void *opc, pid_t pid)
    584 {
    585 
    586 	return puffs_genfs_node_reclaim(pcc, opc, pid);
    587 }
    588