Home | History | Annotate | Line # | Download | only in libpuffs
null.c revision 1.7
      1 /*	$NetBSD: null.c,v 1.7 2007/02/15 17:05: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  * 3. The name of the company nor the name of the author may be used to
     15  *    endorse or promote products derived from this software without specific
     16  *    prior written permission.
     17  *
     18  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS
     19  * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
     20  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
     21  * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
     22  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     23  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
     24  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     25  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     26  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     27  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     28  * SUCH DAMAGE.
     29  */
     30 
     31 #include <sys/cdefs.h>
     32 #if !defined(lint)
     33 __RCSID("$NetBSD: null.c,v 1.7 2007/02/15 17:05:25 pooka Exp $");
     34 #endif /* !lint */
     35 
     36 /*
     37  * A "nullfs" using puffs, i.e. maps one location in the hierarchy
     38  * to another using standard system calls.
     39  */
     40 
     41 #include <sys/types.h>
     42 #include <sys/time.h>
     43 
     44 #include <assert.h>
     45 #include <dirent.h>
     46 #include <errno.h>
     47 #include <fcntl.h>
     48 #include <puffs.h>
     49 #include <stdio.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 != (unsigned)PUFFS_VNOVAL
     73 	    || va->va_mtime.tv_sec != (unsigned)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 errno;
    108 			origmode = sb.st_mode & ALLPERMS;
    109 
    110 			if (chmod(path, 0200) == -1)
    111 				return errno;
    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 errno;
    122 	}
    123 
    124 	return fd;
    125 }
    126 
    127 /*ARGSUSED*/
    128 int
    129 puffs_null_fs_statvfs(struct puffs_cc *pcc, struct statvfs *svfsb, pid_t pid)
    130 {
    131 	struct puffs_usermount *pu = puffs_cc_getusermount(pcc);
    132 
    133 	if (statvfs(PNPATH(pu->pu_pn_root), svfsb) == -1)
    134 		return errno;
    135 
    136 	return 0;
    137 }
    138 
    139 int
    140 puffs_null_node_lookup(struct puffs_cc *pcc, void *opc, void **newnode,
    141 	enum vtype *newtype, voff_t *newsize, dev_t *newrdev,
    142 	const struct puffs_cn *pcn)
    143 {
    144 	struct puffs_usermount *pu = puffs_cc_getusermount(pcc);
    145 	struct puffs_node *pn = opc, *pn_res;
    146 	struct stat sb;
    147 	int rv;
    148 
    149 	assert(pn->pn_va.va_type == VDIR);
    150 
    151 	/*
    152 	 * Note to whoever is copypasting this: you must first check
    153 	 * if the node is there and only then do nodewalk.  Alternatively
    154 	 * you could make sure that you don't return unlinked/rmdir'd
    155 	 * nodes in some other fashion
    156 	 */
    157 	rv = lstat(PCNPATH(pcn), &sb);
    158 	if (rv)
    159 		return rv;
    160 
    161 	/* XXX: UNCONST is wrong, fix the interface */
    162 	/* XXX2: nodewalk is a bit too slow here */
    163 	pn_res = puffs_pn_nodewalk(pu, puffs_path_walkcmp,
    164 	    __UNCONST(&pcn->pcn_po_full));
    165 
    166 	if (pn_res == NULL) {
    167 		pn_res = puffs_pn_new(pu, NULL);
    168 		if (pn_res == NULL)
    169 			return ENOMEM;
    170 		puffs_stat2vattr(&pn_res->pn_va, &sb);
    171 	}
    172 
    173 	*newnode = pn_res;
    174 	*newtype = pn_res->pn_va.va_type;
    175 	*newsize = pn_res->pn_va.va_size;
    176 	*newrdev = pn_res->pn_va.va_rdev;
    177 
    178 	return 0;
    179 }
    180 
    181 /*ARGSUSED*/
    182 int
    183 puffs_null_node_create(struct puffs_cc *pcc, void *opc, void **newnode,
    184 	const struct puffs_cn *pcn, const struct vattr *va)
    185 {
    186 	struct puffs_usermount *pu = puffs_cc_getusermount(pcc);
    187 	struct puffs_node *pn;
    188 	int fd, rv;
    189 
    190 	fd = open(PCNPATH(pcn), O_RDWR | O_CREAT | O_TRUNC);
    191 	if (fd == -1)
    192 		return errno;
    193 	close(fd);
    194 	if ((rv = processvattr(PCNPATH(pcn), va, 1)) != 0) {
    195 		unlink(PCNPATH(pcn));
    196 		return rv;
    197 	}
    198 
    199 	pn = puffs_pn_new(pu, NULL);
    200 	if (!pn) {
    201 		unlink(PCNPATH(pcn));
    202 		return ENOMEM;
    203 	}
    204 	puffs_setvattr(&pn->pn_va, va);
    205 
    206 	*newnode = pn;
    207 	return 0;
    208 }
    209 
    210 /*ARGSUSED*/
    211 int
    212 puffs_null_node_mknod(struct puffs_cc *pcc, void *opc, void **newnode,
    213 	const struct puffs_cn *pcn, const struct vattr *va)
    214 {
    215 	struct puffs_usermount *pu = puffs_cc_getusermount(pcc);
    216 	struct puffs_node *pn;
    217 	int rv;
    218 
    219 	if (mknod(PCNPATH(pcn), va->va_mode, va->va_rdev) == -1)
    220 		return errno;
    221 
    222 	if ((rv = processvattr(PCNPATH(pcn), va, 0)) != 0) {
    223 		unlink(PCNPATH(pcn));
    224 		return rv;
    225 	}
    226 
    227 	pn = puffs_pn_new(pu, NULL);
    228 	if (!pn) {
    229 		unlink(PCNPATH(pcn));
    230 		return ENOMEM;
    231 	}
    232 	puffs_setvattr(&pn->pn_va, va);
    233 
    234 	*newnode = pn;
    235 	return 0;
    236 }
    237 
    238 /*ARGSUSED*/
    239 int
    240 puffs_null_node_getattr(struct puffs_cc *pcc, void *opc, struct vattr *va,
    241 	const struct puffs_cred *pcred, pid_t pid)
    242 {
    243 	struct puffs_node *pn = opc;
    244 	struct stat sb;
    245 
    246 	if (lstat(PNPATH(pn), &sb) == -1)
    247 		return errno;
    248 	puffs_stat2vattr(va, &sb);
    249 
    250 	return 0;
    251 }
    252 
    253 /*ARGSUSED*/
    254 int
    255 puffs_null_node_setattr(struct puffs_cc *pcc, void *opc,
    256 	const struct vattr *va, const struct puffs_cred *pcred, pid_t pid)
    257 {
    258 	struct puffs_node *pn = opc;
    259 	int rv;
    260 
    261 	rv = processvattr(PNPATH(pn), va, pn->pn_va.va_type == VREG);
    262 	if (rv)
    263 		return rv;
    264 
    265 	puffs_setvattr(&pn->pn_va, va);
    266 
    267 	return 0;
    268 }
    269 
    270 /*ARGSUSED*/
    271 int
    272 puffs_null_node_fsync(struct puffs_cc *pcc, void *opc,
    273 	const struct puffs_cred *pcred, int how,
    274 	off_t offlo, off_t offhi, pid_t pid)
    275 {
    276 	struct puffs_node *pn = opc;
    277 	int fd, rv;
    278 	int fflags;
    279 
    280 	rv = 0;
    281 	fd = writeableopen(PNPATH(pn));
    282 	if (fd == -1)
    283 		return errno;
    284 
    285 	if (how & PUFFS_FSYNC_DATAONLY)
    286 		fflags = FDATASYNC;
    287 	else
    288 		fflags = FFILESYNC;
    289 	if (how & PUFFS_FSYNC_CACHE)
    290 		fflags |= FDISKSYNC;
    291 
    292 	if (fsync_range(fd, fflags, offlo, offhi - offlo) == -1)
    293 		rv = errno;
    294 
    295 	close(fd);
    296 
    297 	return rv;
    298 }
    299 
    300 /*ARGSUSED*/
    301 int
    302 puffs_null_node_remove(struct puffs_cc *pcc, void *opc, void *targ,
    303 	const struct puffs_cn *pcn)
    304 {
    305 	struct puffs_node *pn_targ = targ;
    306 
    307 	if (unlink(PNPATH(pn_targ)) == -1)
    308 		return errno;
    309 
    310 	return 0;
    311 }
    312 
    313 /*ARGSUSED*/
    314 int
    315 puffs_null_node_link(struct puffs_cc *pcc, void *opc, void *targ,
    316 	const struct puffs_cn *pcn)
    317 {
    318 	struct puffs_node *pn_targ = targ;
    319 
    320 	if (link(PNPATH(pn_targ), PCNPATH(pcn)) == -1)
    321 		return errno;
    322 
    323 	return 0;
    324 }
    325 
    326 /*ARGSUSED*/
    327 int
    328 puffs_null_node_rename(struct puffs_cc *pcc, void *opc, void *src,
    329 	const struct puffs_cn *pcn_src, void *targ_dir, void *targ,
    330 	const struct puffs_cn *pcn_targ)
    331 {
    332 
    333 	if (rename(PCNPATH(pcn_src), PCNPATH(pcn_targ)) == -1)
    334 		return errno;
    335 
    336 	return 0;
    337 }
    338 
    339 /*ARGSUSED*/
    340 int
    341 puffs_null_node_mkdir(struct puffs_cc *pcc, void *opc, void **newnode,
    342 	const struct puffs_cn *pcn, const struct vattr *va)
    343 {
    344 	struct puffs_usermount *pu = puffs_cc_getusermount(pcc);
    345 	struct puffs_node *pn;
    346 	int rv;
    347 
    348 	if (mkdir(PCNPATH(pcn), va->va_mode) == -1)
    349 		return errno;
    350 
    351 	if ((rv = processvattr(PCNPATH(pcn), va, 0)) != 0) {
    352 		rmdir(PCNPATH(pcn));
    353 		return rv;
    354 	}
    355 
    356 	pn = puffs_pn_new(pu, NULL);
    357 	if (pn == NULL) {
    358 		rmdir(PCNPATH(pcn));
    359 		return ENOMEM;
    360 	}
    361 	puffs_setvattr(&pn->pn_va, va);
    362 
    363 	*newnode = pn;
    364 	return 0;
    365 }
    366 
    367 /*ARGSUSED*/
    368 int
    369 puffs_null_node_rmdir(struct puffs_cc *pcc, void *opc, void *targ,
    370 	const struct puffs_cn *pcn)
    371 {
    372 	struct puffs_node *pn_targ = targ;
    373 
    374 	if (rmdir(PNPATH(pn_targ)) == -1)
    375 		return errno;
    376 
    377 	return 0;
    378 }
    379 
    380 /*ARGSUSED*/
    381 int
    382 puffs_null_node_symlink(struct puffs_cc *pcc, void *opc, void **newnode,
    383 	const struct puffs_cn *pcn, const struct vattr *va,
    384 	const char *linkname)
    385 {
    386 	struct puffs_usermount *pu = puffs_cc_getusermount(pcc);
    387 	struct puffs_node *pn;
    388 	int rv;
    389 
    390 	if (symlink(linkname, PCNPATH(pcn)) == -1)
    391 		return errno;
    392 
    393 	if ((rv = processvattr(PCNPATH(pcn), va, 0)) != 0) {
    394 		unlink(PCNPATH(pcn));
    395 		return rv;
    396 	}
    397 
    398 	pn = puffs_pn_new(pu, NULL);
    399 	if (pn == NULL) {
    400 		rmdir(PCNPATH(pcn));
    401 		return ENOMEM;
    402 	}
    403 	puffs_setvattr(&pn->pn_va, va);
    404 
    405 	*newnode = pn;
    406 	return 0;
    407 }
    408 
    409 /*ARGSUSED*/
    410 int
    411 puffs_null_node_readlink(struct puffs_cc *pcc, void *opc,
    412 	const struct puffs_cred *pcred, char *linkname, size_t *linklen)
    413 {
    414 	struct puffs_node *pn = opc;
    415 	ssize_t rv;
    416 
    417 	rv = readlink(PNPATH(pn), linkname, *linklen);
    418 	if (rv == -1)
    419 		return errno;
    420 
    421 	*linklen -= rv;
    422 	return 0;
    423 }
    424 
    425 /*ARGSUSED*/
    426 int
    427 puffs_null_node_readdir(struct puffs_cc *pcc, void *opc, struct dirent *de,
    428 	const struct puffs_cred *pcred, off_t *off, size_t *reslen)
    429 {
    430 	struct puffs_node *pn = opc;
    431 	struct dirent entry, *result;
    432 	DIR *dp;
    433 	off_t i;
    434 	int rv;
    435 
    436 	dp = opendir(PNPATH(pn));
    437 	if (dp == NULL)
    438 		return errno;
    439 
    440 	rv = 0;
    441 	i = *off;
    442 
    443 	/*
    444 	 * XXX: need to do trickery here, telldir/seekdir would be nice, but
    445 	 * then we'd need to keep state, which I'm too lazy to keep
    446 	 */
    447 	while (i--) {
    448 		rv = readdir_r(dp, &entry, &result);
    449 		if (rv || !result)
    450 			goto out;
    451 	}
    452 
    453 	for (;;) {
    454 		rv = readdir_r(dp, &entry, &result);
    455 		if (rv != 0)
    456 			goto out;
    457 
    458 		if (!result)
    459 			goto out;
    460 
    461 		if (_DIRENT_SIZE(result) > *reslen)
    462 			goto out;
    463 
    464 		*de = *result;
    465 		*reslen -= _DIRENT_SIZE(result);
    466 		de = _DIRENT_NEXT(de);
    467 
    468 		(*off)++;
    469 	}
    470 
    471  out:
    472 	closedir(dp);
    473 	return 0;
    474 }
    475 
    476 /*ARGSUSED*/
    477 int
    478 puffs_null_node_read(struct puffs_cc *pcc, void *opc, uint8_t *buf,
    479 	off_t offset, size_t *buflen, const struct puffs_cred *pcred,
    480 	int ioflag)
    481 {
    482 	struct puffs_node *pn = opc;
    483 	ssize_t n;
    484 	off_t off;
    485 	int fd, rv;
    486 
    487 	rv = 0;
    488 	fd = open(PNPATH(pn), O_RDONLY);
    489 	if (fd == -1)
    490 		return errno;
    491 	off = lseek(fd, offset, SEEK_SET);
    492 	if (off == -1) {
    493 		rv = errno;
    494 		goto out;
    495 	}
    496 
    497 	n = read(fd, buf, *buflen);
    498 	if (n == -1)
    499 		rv = errno;
    500 	else
    501 		*buflen -= n;
    502 
    503  out:
    504 	close(fd);
    505 	return rv;
    506 }
    507 
    508 /*ARGSUSED*/
    509 int
    510 puffs_null_node_write(struct puffs_cc *pcc, void *opc, uint8_t *buf,
    511 	off_t offset, size_t *buflen, const struct puffs_cred *pcred,
    512 	int ioflag)
    513 {
    514 	struct puffs_node *pn = opc;
    515 	ssize_t n;
    516 	off_t off;
    517 	int fd, rv;
    518 
    519 	rv = 0;
    520 	fd = writeableopen(PNPATH(pn));
    521 	if (fd == -1)
    522 		return errno;
    523 
    524 	off = lseek(fd, offset, SEEK_SET);
    525 	if (off == -1) {
    526 		rv = errno;
    527 		goto out;
    528 	}
    529 
    530 	n = write(fd, buf, *buflen);
    531 	if (n == -1)
    532 		rv = errno;
    533 	else
    534 		*buflen -= n;
    535 
    536  out:
    537 	close(fd);
    538 	return rv;
    539 }
    540 
    541 /*ARGSUSED*/
    542 int
    543 puffs_null_node_reclaim(struct puffs_cc *pcc, void *opc, pid_t pid)
    544 {
    545 
    546 	return puffs_genfs_node_reclaim(pcc, opc, pid);
    547 }
    548