Home | History | Annotate | Line # | Download | only in libpuffs
null.c revision 1.19
      1 /*	$NetBSD: null.c,v 1.19 2007/06/25 07:52:01 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.19 2007/06/25 07:52:01 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, void **newnode,
    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 	*newnode = 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_null, node, reclaim);
    186 }
    187 
    188 /*ARGSUSED*/
    189 int
    190 puffs_null_fs_statvfs(struct puffs_cc *pcc, struct statvfs *svfsb, pid_t pid)
    191 {
    192 	struct puffs_usermount *pu = puffs_cc_getusermount(pcc);
    193 
    194 	if (statvfs(PNPATH(puffs_getroot(pu)), svfsb) == -1)
    195 		return errno;
    196 
    197 	return 0;
    198 }
    199 
    200 int
    201 puffs_null_node_lookup(struct puffs_cc *pcc, void *opc, void **newnode,
    202 	enum vtype *newtype, voff_t *newsize, dev_t *newrdev,
    203 	const struct puffs_cn *pcn)
    204 {
    205 	struct puffs_usermount *pu = puffs_cc_getusermount(pcc);
    206 	struct puffs_node *pn = opc, *pn_res;
    207 	struct stat sb;
    208 	int rv;
    209 
    210 	assert(pn->pn_va.va_type == VDIR);
    211 
    212 	/*
    213 	 * Note to whoever is copypasting this: you must first check
    214 	 * if the node is there and only then do nodewalk.  Alternatively
    215 	 * you could make sure that you don't return unlinked/rmdir'd
    216 	 * nodes in some other fashion
    217 	 */
    218 	rv = lstat(PCNPATH(pcn), &sb);
    219 	if (rv)
    220 		return errno;
    221 
    222 	/* XXX2: nodewalk is a bit too slow here */
    223 	pn_res = puffs_pn_nodewalk(pu, inodecmp, &sb.st_ino);
    224 
    225 	if (pn_res == NULL) {
    226 		pn_res = puffs_pn_new(pu, NULL);
    227 		if (pn_res == NULL)
    228 			return ENOMEM;
    229 		puffs_stat2vattr(&pn_res->pn_va, &sb);
    230 	}
    231 
    232 	*newnode = pn_res;
    233 	*newtype = pn_res->pn_va.va_type;
    234 	*newsize = pn_res->pn_va.va_size;
    235 	*newrdev = pn_res->pn_va.va_rdev;
    236 
    237 	return 0;
    238 }
    239 
    240 /*ARGSUSED*/
    241 int
    242 puffs_null_node_create(struct puffs_cc *pcc, void *opc, void **newnode,
    243 	const struct puffs_cn *pcn, const struct vattr *va)
    244 {
    245 	struct puffs_usermount *pu = puffs_cc_getusermount(pcc);
    246 	int fd, rv;
    247 
    248 	fd = open(PCNPATH(pcn), O_RDWR | O_CREAT | O_TRUNC);
    249 	if (fd == -1)
    250 		return errno;
    251 	close(fd);
    252 
    253 	rv = makenode(pu, newnode, pcn, va, 1);
    254 	if (rv)
    255 		unlink(PCNPATH(pcn));
    256 	return rv;
    257 }
    258 
    259 /*ARGSUSED*/
    260 int
    261 puffs_null_node_mknod(struct puffs_cc *pcc, void *opc, void **newnode,
    262 	const struct puffs_cn *pcn, const struct vattr *va)
    263 {
    264 	struct puffs_usermount *pu = puffs_cc_getusermount(pcc);
    265 	mode_t mode;
    266 	int rv;
    267 
    268 	mode = puffs_addvtype2mode(va->va_mode, va->va_type);
    269 	if (mknod(PCNPATH(pcn), mode, va->va_rdev) == -1)
    270 		return errno;
    271 
    272 	rv = makenode(pu, newnode, pcn, va, 0);
    273 	if (rv)
    274 		unlink(PCNPATH(pcn));
    275 	return rv;
    276 }
    277 
    278 /*ARGSUSED*/
    279 int
    280 puffs_null_node_getattr(struct puffs_cc *pcc, void *opc, struct vattr *va,
    281 	const struct puffs_cred *pcred, pid_t pid)
    282 {
    283 	struct puffs_node *pn = opc;
    284 	struct stat sb;
    285 
    286 	if (lstat(PNPATH(pn), &sb) == -1)
    287 		return errno;
    288 	puffs_stat2vattr(va, &sb);
    289 
    290 	return 0;
    291 }
    292 
    293 /*ARGSUSED*/
    294 int
    295 puffs_null_node_setattr(struct puffs_cc *pcc, void *opc,
    296 	const struct vattr *va, const struct puffs_cred *pcred, pid_t pid)
    297 {
    298 	struct puffs_node *pn = opc;
    299 	int rv;
    300 
    301 	rv = processvattr(PNPATH(pn), va, pn->pn_va.va_type == VREG);
    302 	if (rv)
    303 		return rv;
    304 
    305 	puffs_setvattr(&pn->pn_va, va);
    306 
    307 	return 0;
    308 }
    309 
    310 /*ARGSUSED*/
    311 int
    312 puffs_null_node_fsync(struct puffs_cc *pcc, void *opc,
    313 	const struct puffs_cred *pcred, int how,
    314 	off_t offlo, off_t offhi, pid_t pid)
    315 {
    316 	struct puffs_node *pn = opc;
    317 	int fd, rv;
    318 	int fflags;
    319 
    320 	rv = 0;
    321 	fd = writeableopen(PNPATH(pn));
    322 	if (fd == -1)
    323 		return errno;
    324 
    325 	if (how & PUFFS_FSYNC_DATAONLY)
    326 		fflags = FDATASYNC;
    327 	else
    328 		fflags = FFILESYNC;
    329 	if (how & PUFFS_FSYNC_CACHE)
    330 		fflags |= FDISKSYNC;
    331 
    332 	if (fsync_range(fd, fflags, offlo, offhi - offlo) == -1)
    333 		rv = errno;
    334 
    335 	close(fd);
    336 
    337 	return rv;
    338 }
    339 
    340 /*ARGSUSED*/
    341 int
    342 puffs_null_node_remove(struct puffs_cc *pcc, void *opc, void *targ,
    343 	const struct puffs_cn *pcn)
    344 {
    345 	struct puffs_node *pn_targ = targ;
    346 
    347 	if (unlink(PNPATH(pn_targ)) == -1)
    348 		return errno;
    349 	puffs_pn_remove(pn_targ);
    350 
    351 	return 0;
    352 }
    353 
    354 /*ARGSUSED*/
    355 int
    356 puffs_null_node_link(struct puffs_cc *pcc, void *opc, void *targ,
    357 	const struct puffs_cn *pcn)
    358 {
    359 	struct puffs_node *pn_targ = targ;
    360 
    361 	if (link(PNPATH(pn_targ), PCNPATH(pcn)) == -1)
    362 		return errno;
    363 
    364 	return 0;
    365 }
    366 
    367 /*ARGSUSED*/
    368 int
    369 puffs_null_node_rename(struct puffs_cc *pcc, void *opc, void *src,
    370 	const struct puffs_cn *pcn_src, void *targ_dir, void *targ,
    371 	const struct puffs_cn *pcn_targ)
    372 {
    373 
    374 	if (rename(PCNPATH(pcn_src), PCNPATH(pcn_targ)) == -1)
    375 		return errno;
    376 
    377 	return 0;
    378 }
    379 
    380 /*ARGSUSED*/
    381 int
    382 puffs_null_node_mkdir(struct puffs_cc *pcc, void *opc, void **newnode,
    383 	const struct puffs_cn *pcn, const struct vattr *va)
    384 {
    385 	struct puffs_usermount *pu = puffs_cc_getusermount(pcc);
    386 	int rv;
    387 
    388 	if (mkdir(PCNPATH(pcn), va->va_mode) == -1)
    389 		return errno;
    390 
    391 	rv = makenode(pu, newnode, pcn, va, 0);
    392 	if (rv)
    393 		rmdir(PCNPATH(pcn));
    394 	return rv;
    395 }
    396 
    397 /*ARGSUSED*/
    398 int
    399 puffs_null_node_rmdir(struct puffs_cc *pcc, void *opc, void *targ,
    400 	const struct puffs_cn *pcn)
    401 {
    402 	struct puffs_node *pn_targ = targ;
    403 
    404 	if (rmdir(PNPATH(pn_targ)) == -1)
    405 		return errno;
    406 	puffs_pn_remove(pn_targ);
    407 
    408 	return 0;
    409 }
    410 
    411 /*ARGSUSED*/
    412 int
    413 puffs_null_node_symlink(struct puffs_cc *pcc, void *opc, void **newnode,
    414 	const struct puffs_cn *pcn, const struct vattr *va,
    415 	const char *linkname)
    416 {
    417 	struct puffs_usermount *pu = puffs_cc_getusermount(pcc);
    418 	int rv;
    419 
    420 	if (symlink(linkname, PCNPATH(pcn)) == -1)
    421 		return errno;
    422 
    423 	rv = makenode(pu, newnode, pcn, va, 0);
    424 	if (rv)
    425 		unlink(PCNPATH(pcn));
    426 	return rv;
    427 }
    428 
    429 /*ARGSUSED*/
    430 int
    431 puffs_null_node_readlink(struct puffs_cc *pcc, void *opc,
    432 	const struct puffs_cred *pcred, char *linkname, size_t *linklen)
    433 {
    434 	struct puffs_node *pn = opc;
    435 	ssize_t rv;
    436 
    437 	rv = readlink(PNPATH(pn), linkname, *linklen);
    438 	if (rv == -1)
    439 		return errno;
    440 
    441 	*linklen = rv;
    442 	return 0;
    443 }
    444 
    445 /*ARGSUSED*/
    446 int
    447 puffs_null_node_readdir(struct puffs_cc *pcc, void *opc, struct dirent *de,
    448 	off_t *off, size_t *reslen, const struct puffs_cred *pcred,
    449 	int *eofflag, off_t *cookies, size_t *ncookies)
    450 {
    451 	struct puffs_node *pn = opc;
    452 	struct dirent entry, *result;
    453 	DIR *dp;
    454 	off_t i;
    455 	int rv;
    456 
    457 	dp = opendir(PNPATH(pn));
    458 	if (dp == NULL)
    459 		return errno;
    460 
    461 	rv = 0;
    462 	i = *off;
    463 
    464 	/*
    465 	 * XXX: need to do trickery here, telldir/seekdir would be nice, but
    466 	 * then we'd need to keep state, which I'm too lazy to keep
    467 	 */
    468 	while (i--) {
    469 		rv = readdir_r(dp, &entry, &result);
    470 		if (rv || !result)
    471 			goto out;
    472 	}
    473 
    474 	for (;;) {
    475 		rv = readdir_r(dp, &entry, &result);
    476 		if (rv != 0)
    477 			goto out;
    478 
    479 		if (!result)
    480 			goto out;
    481 
    482 		if (_DIRENT_SIZE(result) > *reslen)
    483 			goto out;
    484 
    485 		*de = *result;
    486 		*reslen -= _DIRENT_SIZE(result);
    487 		de = _DIRENT_NEXT(de);
    488 
    489 		(*off)++;
    490 	}
    491 
    492  out:
    493 	closedir(dp);
    494 	return 0;
    495 }
    496 
    497 /*ARGSUSED*/
    498 int
    499 puffs_null_node_read(struct puffs_cc *pcc, void *opc, uint8_t *buf,
    500 	off_t offset, size_t *buflen, const struct puffs_cred *pcred,
    501 	int ioflag)
    502 {
    503 	struct puffs_node *pn = opc;
    504 	ssize_t n;
    505 	off_t off;
    506 	int fd, rv;
    507 
    508 	rv = 0;
    509 	fd = open(PNPATH(pn), O_RDONLY);
    510 	if (fd == -1)
    511 		return errno;
    512 	off = lseek(fd, offset, SEEK_SET);
    513 	if (off == -1) {
    514 		rv = errno;
    515 		goto out;
    516 	}
    517 
    518 	n = read(fd, buf, *buflen);
    519 	if (n == -1)
    520 		rv = errno;
    521 	else
    522 		*buflen -= n;
    523 
    524  out:
    525 	close(fd);
    526 	return rv;
    527 }
    528 
    529 /*ARGSUSED*/
    530 int
    531 puffs_null_node_write(struct puffs_cc *pcc, void *opc, uint8_t *buf,
    532 	off_t offset, size_t *buflen, const struct puffs_cred *pcred,
    533 	int ioflag)
    534 {
    535 	struct puffs_node *pn = opc;
    536 	ssize_t n;
    537 	off_t off;
    538 	int fd, rv;
    539 
    540 	rv = 0;
    541 	fd = writeableopen(PNPATH(pn));
    542 	if (fd == -1)
    543 		return errno;
    544 
    545 	off = lseek(fd, offset, SEEK_SET);
    546 	if (off == -1) {
    547 		rv = errno;
    548 		goto out;
    549 	}
    550 
    551 	n = write(fd, buf, *buflen);
    552 	if (n == -1)
    553 		rv = errno;
    554 	else
    555 		*buflen -= n;
    556 
    557  out:
    558 	close(fd);
    559 	return rv;
    560 }
    561 
    562 /*ARGSUSED*/
    563 int
    564 puffs_null_node_reclaim(struct puffs_cc *pcc, void *opc, pid_t pid)
    565 {
    566 
    567 	return puffs_genfs_node_reclaim(pcc, opc, pid);
    568 }
    569