Home | History | Annotate | Line # | Download | only in libpuffs
null.c revision 1.3
      1  1.3  pooka /*	$NetBSD: null.c,v 1.3 2007/01/11 17:48:21 pooka Exp $	*/
      2  1.1  pooka 
      3  1.1  pooka /*
      4  1.1  pooka  * Copyright (c) 2007  Antti Kantee.  All Rights Reserved.
      5  1.1  pooka  *
      6  1.1  pooka  * Redistribution and use in source and binary forms, with or without
      7  1.1  pooka  * modification, are permitted provided that the following conditions
      8  1.1  pooka  * are met:
      9  1.1  pooka  * 1. Redistributions of source code must retain the above copyright
     10  1.1  pooka  *    notice, this list of conditions and the following disclaimer.
     11  1.1  pooka  * 2. Redistributions in binary form must reproduce the above copyright
     12  1.1  pooka  *    notice, this list of conditions and the following disclaimer in the
     13  1.1  pooka  *    documentation and/or other materials provided with the distribution.
     14  1.1  pooka  * 3. The name of the company nor the name of the author may be used to
     15  1.1  pooka  *    endorse or promote products derived from this software without specific
     16  1.1  pooka  *    prior written permission.
     17  1.1  pooka  *
     18  1.1  pooka  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS
     19  1.1  pooka  * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
     20  1.1  pooka  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
     21  1.1  pooka  * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
     22  1.1  pooka  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     23  1.1  pooka  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
     24  1.1  pooka  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     25  1.1  pooka  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     26  1.1  pooka  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     27  1.1  pooka  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     28  1.1  pooka  * SUCH DAMAGE.
     29  1.1  pooka  */
     30  1.1  pooka 
     31  1.1  pooka #include <sys/cdefs.h>
     32  1.1  pooka #if !defined(lint)
     33  1.3  pooka __RCSID("$NetBSD: null.c,v 1.3 2007/01/11 17:48:21 pooka Exp $");
     34  1.1  pooka #endif /* !lint */
     35  1.1  pooka 
     36  1.1  pooka /*
     37  1.1  pooka  * A "nullfs" using puffs, i.e. maps one location in the hierarchy
     38  1.1  pooka  * to another using standard system calls.
     39  1.1  pooka  */
     40  1.1  pooka 
     41  1.1  pooka #include <sys/types.h>
     42  1.1  pooka #include <sys/time.h>
     43  1.1  pooka 
     44  1.1  pooka #include <assert.h>
     45  1.1  pooka #include <dirent.h>
     46  1.1  pooka #include <errno.h>
     47  1.1  pooka #include <fcntl.h>
     48  1.1  pooka #include <puffs.h>
     49  1.1  pooka #include <stdio.h>
     50  1.1  pooka #include <unistd.h>
     51  1.1  pooka 
     52  1.1  pooka PUFFSOP_PROTOS(puffs_null)
     53  1.1  pooka 
     54  1.1  pooka static void *
     55  1.1  pooka pathcmp(struct puffs_node *pn, void *arg)
     56  1.1  pooka {
     57  1.1  pooka 
     58  1.1  pooka 	if (strcmp(arg, pn->pn_path) == 0)
     59  1.1  pooka 		return pn;
     60  1.1  pooka 	return NULL;
     61  1.1  pooka }
     62  1.1  pooka 
     63  1.1  pooka /*
     64  1.1  pooka  * set attributes to what is specified.  XXX: no rollback in case of failure
     65  1.1  pooka  */
     66  1.1  pooka static int
     67  1.2  pooka processvattr(const char *path, const struct vattr *va, int regular)
     68  1.1  pooka {
     69  1.1  pooka 	struct timeval tv[2];
     70  1.1  pooka 
     71  1.1  pooka 	/* XXX: -1 == PUFFS_VNOVAL, but shouldn't trust that */
     72  1.1  pooka 	if (va->va_uid != (unsigned)-1 || va->va_gid != (unsigned)-1)
     73  1.2  pooka 		if (lchown(path, va->va_uid, va->va_gid) == -1)
     74  1.1  pooka 			return errno;
     75  1.1  pooka 
     76  1.1  pooka 	if (va->va_mode != (unsigned)PUFFS_VNOVAL)
     77  1.2  pooka 		if (lchmod(path, va->va_mode) == -1)
     78  1.1  pooka 			return errno;
     79  1.1  pooka 
     80  1.1  pooka 	/* sloppy */
     81  1.1  pooka 	if (va->va_atime.tv_sec != (unsigned)PUFFS_VNOVAL
     82  1.1  pooka 	    || va->va_mtime.tv_sec != (unsigned)PUFFS_VNOVAL) {
     83  1.1  pooka 		TIMESPEC_TO_TIMEVAL(&tv[0], &va->va_atime);
     84  1.1  pooka 		TIMESPEC_TO_TIMEVAL(&tv[1], &va->va_mtime);
     85  1.1  pooka 
     86  1.1  pooka 		if (lutimes(path, tv) == -1)
     87  1.1  pooka 			return errno;
     88  1.1  pooka 	}
     89  1.1  pooka 
     90  1.2  pooka 	if (regular && va->va_size != (u_quad_t)PUFFS_VNOVAL)
     91  1.1  pooka 		if (truncate(path, (off_t)va->va_size) == -1)
     92  1.1  pooka 			return errno;
     93  1.1  pooka 
     94  1.1  pooka 	return 0;
     95  1.1  pooka }
     96  1.1  pooka 
     97  1.3  pooka /*
     98  1.3  pooka  * Kludge to open files which aren't writable *any longer*.  This kinda
     99  1.3  pooka  * works because the vfs layer does validation checks based on the file's
    100  1.3  pooka  * permissions to allow writable opening before opening them.  However,
    101  1.3  pooka  * the problem arises if we want to create a file, write to it (cache),
    102  1.3  pooka  * adjust permissions and then flush the file.
    103  1.3  pooka  */
    104  1.3  pooka static int
    105  1.3  pooka writeableopen(const char *path)
    106  1.3  pooka {
    107  1.3  pooka 	struct stat sb;
    108  1.3  pooka 	mode_t origmode;
    109  1.3  pooka 	int sverr = 0;
    110  1.3  pooka 	int fd;
    111  1.3  pooka 
    112  1.3  pooka 	fd = open(path, O_WRONLY);
    113  1.3  pooka 	if (fd == -1) {
    114  1.3  pooka 		if (errno == EACCES) {
    115  1.3  pooka 			if (stat(path, &sb) == -1)
    116  1.3  pooka 				return errno;
    117  1.3  pooka 			origmode = sb.st_mode & ALLPERMS;
    118  1.3  pooka 
    119  1.3  pooka 			if (chmod(path, 0200) == -1)
    120  1.3  pooka 				return errno;
    121  1.3  pooka 
    122  1.3  pooka 			fd = open(path, O_WRONLY);
    123  1.3  pooka 			if (fd == -1)
    124  1.3  pooka 				sverr = errno;
    125  1.3  pooka 
    126  1.3  pooka 			chmod(path, origmode);
    127  1.3  pooka 			if (sverr)
    128  1.3  pooka 				errno = sverr;
    129  1.3  pooka 		} else
    130  1.3  pooka 			return errno;
    131  1.3  pooka 	}
    132  1.3  pooka 
    133  1.3  pooka 	return fd;
    134  1.3  pooka }
    135  1.3  pooka 
    136  1.1  pooka /*ARGSUSED*/
    137  1.1  pooka int
    138  1.1  pooka puffs_null_fs_statvfs(struct puffs_cc *pcc, struct statvfs *svfsb, pid_t pid)
    139  1.1  pooka {
    140  1.1  pooka 	struct puffs_usermount *pu = puffs_cc_getusermount(pcc);
    141  1.1  pooka 
    142  1.1  pooka 	if (statvfs(pu->pu_pn_root->pn_path, svfsb) == -1)
    143  1.1  pooka 		return errno;
    144  1.1  pooka 
    145  1.1  pooka 	return 0;
    146  1.1  pooka }
    147  1.1  pooka 
    148  1.1  pooka /*
    149  1.1  pooka  * Wow, this kinda looks like a real lookup since it bounces all
    150  1.1  pooka  * over the place
    151  1.1  pooka  */
    152  1.1  pooka int
    153  1.1  pooka puffs_null_node_lookup(struct puffs_cc *pcc, void *opc, void **newnode,
    154  1.1  pooka 	enum vtype *newtype, voff_t *newsize, dev_t *newrdev,
    155  1.1  pooka 	const struct puffs_cn *pcn)
    156  1.1  pooka {
    157  1.1  pooka 	struct puffs_usermount *pu = puffs_cc_getusermount(pcc);
    158  1.1  pooka 	struct puffs_node *pn = opc, *pn_res;
    159  1.1  pooka 	struct dirent entry, *result;
    160  1.1  pooka 	char dpath[MAXPATHLEN+1];
    161  1.1  pooka 	char *npath;
    162  1.1  pooka 	struct stat sb;
    163  1.1  pooka 	DIR *dp;
    164  1.1  pooka 	int rv;
    165  1.1  pooka 
    166  1.1  pooka 	assert(pn->pn_va.va_type == VDIR);
    167  1.1  pooka 
    168  1.1  pooka 	if (pcn->pcn_flags & PUFFS_ISDOTDOT) {
    169  1.1  pooka 		char *p;
    170  1.1  pooka 		strcpy(dpath, pn->pn_path);
    171  1.1  pooka 		p = strrchr(dpath, '/');
    172  1.1  pooka 		assert(p != NULL);
    173  1.1  pooka 		*p = '\0';
    174  1.1  pooka 
    175  1.1  pooka 		pn_res = puffs_pn_nodewalk(pu, pathcmp, dpath);
    176  1.1  pooka 		if (pn_res)
    177  1.1  pooka 			goto success;
    178  1.1  pooka 		npath = dpath;
    179  1.1  pooka 		goto getnew;
    180  1.1  pooka 	}
    181  1.1  pooka 
    182  1.1  pooka 	dp = opendir(pn->pn_path);
    183  1.1  pooka 	if (dp == NULL)
    184  1.1  pooka 		return errno;
    185  1.1  pooka 
    186  1.1  pooka 	/* more readable than for(), IMHO */
    187  1.1  pooka 	rv = readdir_r(dp, &entry, &result);
    188  1.1  pooka 	while (rv == 0 && result) {
    189  1.1  pooka 		if (strcmp(result->d_name, pcn->pcn_name) == 0)
    190  1.1  pooka 			break;
    191  1.1  pooka 		rv = readdir_r(dp, &entry, &result);
    192  1.1  pooka 	}
    193  1.1  pooka 	closedir(dp);
    194  1.1  pooka 
    195  1.1  pooka 	if (rv)
    196  1.1  pooka 		return rv;
    197  1.1  pooka 	if (!result)
    198  1.1  pooka 		return ENOENT;
    199  1.1  pooka 
    200  1.1  pooka 	pn_res = puffs_pn_nodewalk(pu, pathcmp, pcn->pcn_fullpath);
    201  1.1  pooka 	if (pn_res)
    202  1.1  pooka 		goto success;
    203  1.1  pooka 	npath = pcn->pcn_fullpath;
    204  1.1  pooka 
    205  1.1  pooka  getnew:
    206  1.1  pooka 	rv = lstat(npath, &sb);
    207  1.1  pooka 	if (rv)
    208  1.1  pooka 		return rv;
    209  1.1  pooka 	pn_res = puffs_pn_new(pu, NULL);
    210  1.1  pooka 	if (pn_res == NULL)
    211  1.1  pooka 		return ENOMEM;
    212  1.1  pooka 	puffs_stat2vattr(&pn_res->pn_va, &sb);
    213  1.1  pooka 
    214  1.1  pooka  success:
    215  1.1  pooka 	*newnode = pn_res;
    216  1.1  pooka 	*newtype = pn_res->pn_va.va_type;
    217  1.1  pooka 	*newsize = pn_res->pn_va.va_size;
    218  1.1  pooka 	*newrdev = pn_res->pn_va.va_rdev;
    219  1.1  pooka 
    220  1.1  pooka 	return 0;
    221  1.1  pooka }
    222  1.1  pooka 
    223  1.1  pooka /*ARGSUSED*/
    224  1.1  pooka int
    225  1.1  pooka puffs_null_node_create(struct puffs_cc *pcc, void *opc, void **newnode,
    226  1.1  pooka 	const struct puffs_cn *pcn, const struct vattr *va)
    227  1.1  pooka {
    228  1.1  pooka 	struct puffs_usermount *pu = puffs_cc_getusermount(pcc);
    229  1.1  pooka 	struct puffs_node *pn;
    230  1.1  pooka 	int fd, rv;
    231  1.1  pooka 
    232  1.1  pooka 	fd = open(pcn->pcn_fullpath, O_RDWR | O_CREAT | O_TRUNC);
    233  1.1  pooka 	if (fd == -1)
    234  1.1  pooka 		return errno;
    235  1.1  pooka 	close(fd);
    236  1.2  pooka 	if ((rv = processvattr(pcn->pcn_fullpath, va, 1)) != 0) {
    237  1.1  pooka 		unlink(pcn->pcn_fullpath);
    238  1.1  pooka 		return rv;
    239  1.1  pooka 	}
    240  1.1  pooka 
    241  1.1  pooka 	pn = puffs_pn_new(pu, NULL);
    242  1.1  pooka 	if (!pn) {
    243  1.1  pooka 		unlink(pcn->pcn_fullpath);
    244  1.1  pooka 		return ENOMEM;
    245  1.1  pooka 	}
    246  1.1  pooka 	puffs_setvattr(&pn->pn_va, va);
    247  1.1  pooka 
    248  1.1  pooka 	*newnode = pn;
    249  1.1  pooka 	return 0;
    250  1.1  pooka }
    251  1.1  pooka 
    252  1.1  pooka /*ARGSUSED*/
    253  1.1  pooka int
    254  1.1  pooka puffs_null_node_mknod(struct puffs_cc *pcc, void *opc, void **newnode,
    255  1.1  pooka 	const struct puffs_cn *pcn, const struct vattr *va)
    256  1.1  pooka {
    257  1.1  pooka 	struct puffs_usermount *pu = puffs_cc_getusermount(pcc);
    258  1.1  pooka 	struct puffs_node *pn;
    259  1.1  pooka 	int rv;
    260  1.1  pooka 
    261  1.1  pooka 	if (mknod(pcn->pcn_fullpath, va->va_mode, va->va_rdev) == -1)
    262  1.1  pooka 		return errno;
    263  1.1  pooka 
    264  1.2  pooka 	if ((rv = processvattr(pcn->pcn_fullpath, va, 0)) != 0) {
    265  1.1  pooka 		unlink(pcn->pcn_fullpath);
    266  1.1  pooka 		return rv;
    267  1.1  pooka 	}
    268  1.1  pooka 
    269  1.1  pooka 	pn = puffs_pn_new(pu, NULL);
    270  1.1  pooka 	if (!pn) {
    271  1.1  pooka 		unlink(pcn->pcn_fullpath);
    272  1.1  pooka 		return ENOMEM;
    273  1.1  pooka 	}
    274  1.1  pooka 	puffs_setvattr(&pn->pn_va, va);
    275  1.1  pooka 
    276  1.1  pooka 	*newnode = pn;
    277  1.1  pooka 	return 0;
    278  1.1  pooka }
    279  1.1  pooka 
    280  1.1  pooka /*ARGSUSED*/
    281  1.1  pooka int
    282  1.1  pooka puffs_null_node_getattr(struct puffs_cc *pcc, void *opc, struct vattr *va,
    283  1.1  pooka 	const struct puffs_cred *pcred, pid_t pid)
    284  1.1  pooka {
    285  1.1  pooka 	struct puffs_node *pn = opc;
    286  1.1  pooka 	struct stat sb;
    287  1.1  pooka 
    288  1.1  pooka 	if (lstat(pn->pn_path, &sb) == -1)
    289  1.1  pooka 		return errno;
    290  1.1  pooka 	puffs_stat2vattr(va, &sb);
    291  1.1  pooka 
    292  1.1  pooka 	return 0;
    293  1.1  pooka }
    294  1.1  pooka 
    295  1.1  pooka /*ARGSUSED*/
    296  1.1  pooka int
    297  1.1  pooka puffs_null_node_setattr(struct puffs_cc *pcc, void *opc,
    298  1.1  pooka 	const struct vattr *va, const struct puffs_cred *pcred, pid_t pid)
    299  1.1  pooka {
    300  1.1  pooka 	struct puffs_node *pn = opc;
    301  1.1  pooka 	int rv;
    302  1.1  pooka 
    303  1.2  pooka 	rv = processvattr(pn->pn_path, va, pn->pn_va.va_type == VREG);
    304  1.1  pooka 	if (rv)
    305  1.1  pooka 		return rv;
    306  1.1  pooka 
    307  1.1  pooka 	puffs_setvattr(&pn->pn_va, va);
    308  1.1  pooka 
    309  1.1  pooka 	return 0;
    310  1.1  pooka }
    311  1.1  pooka 
    312  1.1  pooka /*ARGSUSED*/
    313  1.1  pooka int
    314  1.3  pooka puffs_null_node_fsync(struct puffs_cc *pcc, void *opc,
    315  1.3  pooka 	const struct puffs_cred *pcred, int how,
    316  1.3  pooka 	off_t offlo, off_t offhi, pid_t pid)
    317  1.3  pooka {
    318  1.3  pooka 	struct puffs_node *pn = opc;
    319  1.3  pooka 	int fd, rv;
    320  1.3  pooka 	int fflags;
    321  1.3  pooka 
    322  1.3  pooka 	rv = 0;
    323  1.3  pooka 	fd = writeableopen(pn->pn_path);
    324  1.3  pooka 	if (fd == -1)
    325  1.3  pooka 		return errno;
    326  1.3  pooka 
    327  1.3  pooka 	if (how & PUFFS_FSYNC_DATAONLY)
    328  1.3  pooka 		fflags = FDATASYNC;
    329  1.3  pooka 	else
    330  1.3  pooka 		fflags = FFILESYNC;
    331  1.3  pooka 	if (how & PUFFS_FSYNC_CACHE)
    332  1.3  pooka 		fflags |= FDISKSYNC;
    333  1.3  pooka 
    334  1.3  pooka 	if (fsync_range(fd, fflags, offlo, offhi - offlo) == -1)
    335  1.3  pooka 		rv = errno;
    336  1.3  pooka 
    337  1.3  pooka 	close(fd);
    338  1.3  pooka 
    339  1.3  pooka 	return rv;
    340  1.3  pooka }
    341  1.3  pooka 
    342  1.3  pooka /*ARGSUSED*/
    343  1.3  pooka int
    344  1.1  pooka puffs_null_node_remove(struct puffs_cc *pcc, void *opc, void *targ,
    345  1.1  pooka 	const struct puffs_cn *pcn)
    346  1.1  pooka {
    347  1.1  pooka 	struct puffs_node *pn_targ = targ;
    348  1.1  pooka 
    349  1.1  pooka 	if (unlink(pn_targ->pn_path) == -1)
    350  1.1  pooka 		return errno;
    351  1.1  pooka 
    352  1.1  pooka 	return 0;
    353  1.1  pooka }
    354  1.1  pooka 
    355  1.1  pooka /*ARGSUSED*/
    356  1.1  pooka int
    357  1.1  pooka puffs_null_node_link(struct puffs_cc *pcc, void *opc, void *targ,
    358  1.1  pooka 	const struct puffs_cn *pcn)
    359  1.1  pooka {
    360  1.1  pooka 	struct puffs_node *pn_targ = targ;
    361  1.1  pooka 
    362  1.1  pooka 	if (link(pn_targ->pn_path, pcn->pcn_fullpath) == -1)
    363  1.1  pooka 		return errno;
    364  1.1  pooka 
    365  1.1  pooka 	return 0;
    366  1.1  pooka }
    367  1.1  pooka 
    368  1.1  pooka /*ARGSUSED*/
    369  1.1  pooka int
    370  1.1  pooka puffs_null_node_rename(struct puffs_cc *pcc, void *opc, void *src,
    371  1.1  pooka 	const struct puffs_cn *pcn_src, void *targ_dir, void *targ,
    372  1.1  pooka 	const struct puffs_cn *pcn_targ)
    373  1.1  pooka {
    374  1.1  pooka 
    375  1.1  pooka 	if (rename(pcn_src->pcn_fullpath, pcn_targ->pcn_fullpath) == -1)
    376  1.1  pooka 		return errno;
    377  1.1  pooka 
    378  1.1  pooka 	return 0;
    379  1.1  pooka }
    380  1.1  pooka 
    381  1.1  pooka /*ARGSUSED*/
    382  1.1  pooka int
    383  1.1  pooka puffs_null_node_mkdir(struct puffs_cc *pcc, void *opc, void **newnode,
    384  1.1  pooka 	const struct puffs_cn *pcn, const struct vattr *va)
    385  1.1  pooka {
    386  1.1  pooka 	struct puffs_usermount *pu = puffs_cc_getusermount(pcc);
    387  1.1  pooka 	struct puffs_node *pn;
    388  1.1  pooka 	int rv;
    389  1.1  pooka 
    390  1.1  pooka 	if (mkdir(pcn->pcn_fullpath, va->va_mode) == -1)
    391  1.1  pooka 		return errno;
    392  1.1  pooka 
    393  1.2  pooka 	if ((rv = processvattr(pcn->pcn_fullpath, va, 0)) != 0) {
    394  1.1  pooka 		unlink(pcn->pcn_fullpath);
    395  1.1  pooka 		return rv;
    396  1.1  pooka 	}
    397  1.1  pooka 
    398  1.1  pooka 	pn = puffs_pn_new(pu, NULL);
    399  1.1  pooka 	if (pn == NULL) {
    400  1.1  pooka 		rmdir(pcn->pcn_fullpath);
    401  1.1  pooka 		return ENOMEM;
    402  1.1  pooka 	}
    403  1.1  pooka 	puffs_setvattr(&pn->pn_va, va);
    404  1.1  pooka 
    405  1.1  pooka 	*newnode = pn;
    406  1.1  pooka 	return 0;
    407  1.1  pooka }
    408  1.1  pooka 
    409  1.1  pooka /*ARGSUSED*/
    410  1.1  pooka int
    411  1.1  pooka puffs_null_node_rmdir(struct puffs_cc *pcc, void *opc, void *targ,
    412  1.1  pooka 	const struct puffs_cn *pcn)
    413  1.1  pooka {
    414  1.1  pooka 	struct puffs_node *pn_targ = targ;
    415  1.1  pooka 
    416  1.1  pooka 	if (rmdir(pn_targ->pn_path) == -1)
    417  1.1  pooka 		return errno;
    418  1.1  pooka 
    419  1.1  pooka 	return 0;
    420  1.1  pooka }
    421  1.1  pooka 
    422  1.1  pooka /*ARGSUSED*/
    423  1.1  pooka int
    424  1.1  pooka puffs_null_node_symlink(struct puffs_cc *pcc, void *opc, void **newnode,
    425  1.1  pooka 	const struct puffs_cn *pcn, const struct vattr *va,
    426  1.1  pooka 	const char *linkname)
    427  1.1  pooka {
    428  1.1  pooka 	struct puffs_usermount *pu = puffs_cc_getusermount(pcc);
    429  1.1  pooka 	struct puffs_node *pn;
    430  1.1  pooka 	int rv;
    431  1.1  pooka 
    432  1.1  pooka 	if (symlink(linkname, pcn->pcn_fullpath) == -1)
    433  1.1  pooka 		return errno;
    434  1.1  pooka 
    435  1.2  pooka 	if ((rv = processvattr(pcn->pcn_fullpath, va, 0)) != 0) {
    436  1.1  pooka 		unlink(pcn->pcn_fullpath);
    437  1.1  pooka 		return rv;
    438  1.1  pooka 	}
    439  1.1  pooka 
    440  1.1  pooka 	pn = puffs_pn_new(pu, NULL);
    441  1.1  pooka 	if (pn == NULL) {
    442  1.1  pooka 		rmdir(pcn->pcn_fullpath);
    443  1.1  pooka 		return ENOMEM;
    444  1.1  pooka 	}
    445  1.1  pooka 	puffs_setvattr(&pn->pn_va, va);
    446  1.1  pooka 
    447  1.1  pooka 	*newnode = pn;
    448  1.1  pooka 	return 0;
    449  1.1  pooka }
    450  1.1  pooka 
    451  1.1  pooka /*ARGSUSED*/
    452  1.1  pooka int
    453  1.1  pooka puffs_null_node_readlink(struct puffs_cc *pcc, void *opc,
    454  1.1  pooka 	const struct puffs_cred *pcred, char *linkname, size_t *linklen)
    455  1.1  pooka {
    456  1.1  pooka 	struct puffs_node *pn = opc;
    457  1.1  pooka 	ssize_t rv;
    458  1.1  pooka 
    459  1.1  pooka 	rv = readlink(pn->pn_path, linkname, *linklen);
    460  1.1  pooka 	if (rv == -1)
    461  1.1  pooka 		return errno;
    462  1.1  pooka 
    463  1.1  pooka 	*linklen -= rv;
    464  1.1  pooka 	return 0;
    465  1.1  pooka }
    466  1.1  pooka 
    467  1.1  pooka /*ARGSUSED*/
    468  1.1  pooka int
    469  1.1  pooka puffs_null_node_readdir(struct puffs_cc *pcc, void *opc, struct dirent *de,
    470  1.1  pooka 	const struct puffs_cred *pcred, off_t *off, size_t *reslen)
    471  1.1  pooka {
    472  1.1  pooka 	struct puffs_node *pn = opc;
    473  1.1  pooka 	struct dirent entry, *result;
    474  1.1  pooka 	DIR *dp;
    475  1.1  pooka 	off_t i;
    476  1.1  pooka 	int rv;
    477  1.1  pooka 
    478  1.1  pooka 	dp = opendir(pn->pn_path);
    479  1.1  pooka 	if (dp == NULL)
    480  1.1  pooka 		return errno;
    481  1.1  pooka 
    482  1.1  pooka 	rv = 0;
    483  1.1  pooka 	i = *off;
    484  1.1  pooka 
    485  1.1  pooka 	/*
    486  1.1  pooka 	 * XXX: need to do trickery here, telldir/seekdir would be nice, but
    487  1.1  pooka 	 * then we'd need to keep state, which I'm too lazy to keep
    488  1.1  pooka 	 */
    489  1.1  pooka 	while (i--) {
    490  1.1  pooka 		rv = readdir_r(dp, &entry, &result);
    491  1.1  pooka 		if (rv || !result)
    492  1.1  pooka 			goto out;
    493  1.1  pooka 	}
    494  1.1  pooka 
    495  1.1  pooka 	for (;;) {
    496  1.1  pooka 		rv = readdir_r(dp, &entry, &result);
    497  1.1  pooka 		if (rv != 0)
    498  1.1  pooka 			goto out;
    499  1.1  pooka 
    500  1.1  pooka 		if (!result)
    501  1.1  pooka 			goto out;
    502  1.1  pooka 
    503  1.1  pooka 		if (_DIRENT_SIZE(result) > *reslen)
    504  1.1  pooka 			goto out;
    505  1.1  pooka 
    506  1.1  pooka 		*de = *result;
    507  1.1  pooka 		*reslen -= _DIRENT_SIZE(result);
    508  1.1  pooka 		de = _DIRENT_NEXT(de);
    509  1.1  pooka 
    510  1.1  pooka 		(*off)++;
    511  1.1  pooka 	}
    512  1.1  pooka 
    513  1.1  pooka  out:
    514  1.1  pooka 	closedir(dp);
    515  1.1  pooka 	return 0;
    516  1.1  pooka }
    517  1.1  pooka 
    518  1.1  pooka /*ARGSUSED*/
    519  1.1  pooka int
    520  1.1  pooka puffs_null_node_read(struct puffs_cc *pcc, void *opc, uint8_t *buf,
    521  1.1  pooka 	off_t offset, size_t *buflen, const struct puffs_cred *pcred,
    522  1.1  pooka 	int ioflag)
    523  1.1  pooka {
    524  1.1  pooka 	struct puffs_node *pn = opc;
    525  1.1  pooka 	ssize_t n;
    526  1.1  pooka 	off_t off;
    527  1.1  pooka 	int fd, rv;
    528  1.1  pooka 
    529  1.1  pooka 	rv = 0;
    530  1.1  pooka 	fd = open(pn->pn_path, O_RDONLY);
    531  1.1  pooka 	if (fd == -1)
    532  1.1  pooka 		return errno;
    533  1.1  pooka 	off = lseek(fd, offset, SEEK_SET);
    534  1.1  pooka 	if (off == -1) {
    535  1.1  pooka 		rv = errno;
    536  1.1  pooka 		goto out;
    537  1.1  pooka 	}
    538  1.1  pooka 
    539  1.1  pooka 	n = read(fd, buf, *buflen);
    540  1.1  pooka 	if (n == -1)
    541  1.1  pooka 		rv = errno;
    542  1.1  pooka 	else
    543  1.1  pooka 		*buflen -= n;
    544  1.1  pooka 
    545  1.1  pooka  out:
    546  1.1  pooka 	close(fd);
    547  1.1  pooka 	return rv;
    548  1.1  pooka }
    549  1.1  pooka 
    550  1.1  pooka /*ARGSUSED*/
    551  1.1  pooka int
    552  1.1  pooka puffs_null_node_write(struct puffs_cc *pcc, void *opc, uint8_t *buf,
    553  1.1  pooka 	off_t offset, size_t *buflen, const struct puffs_cred *pcred,
    554  1.1  pooka 	int ioflag)
    555  1.1  pooka {
    556  1.1  pooka 	struct puffs_node *pn = opc;
    557  1.1  pooka 	ssize_t n;
    558  1.1  pooka 	off_t off;
    559  1.1  pooka 	int fd, rv;
    560  1.1  pooka 
    561  1.1  pooka 	rv = 0;
    562  1.3  pooka 	fd = writeableopen(pn->pn_path);
    563  1.3  pooka 	if (fd == -1)
    564  1.3  pooka 		return errno;
    565  1.2  pooka 
    566  1.1  pooka 	off = lseek(fd, offset, SEEK_SET);
    567  1.1  pooka 	if (off == -1) {
    568  1.1  pooka 		rv = errno;
    569  1.1  pooka 		goto out;
    570  1.1  pooka 	}
    571  1.1  pooka 
    572  1.1  pooka 	n = write(fd, buf, *buflen);
    573  1.1  pooka 	if (n == -1)
    574  1.1  pooka 		rv = errno;
    575  1.1  pooka 	else
    576  1.1  pooka 		*buflen -= n;
    577  1.1  pooka 
    578  1.1  pooka  out:
    579  1.1  pooka 	close(fd);
    580  1.1  pooka 	return rv;
    581  1.1  pooka }
    582