Home | History | Annotate | Line # | Download | only in libpuffs
subr.c revision 1.8
      1  1.8     pooka /*	$NetBSD: subr.c,v 1.8 2006/11/14 11:45:03 pooka Exp $	*/
      2  1.1     pooka 
      3  1.1     pooka /*
      4  1.1     pooka  * Copyright (c) 2006 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.8     pooka __RCSID("$NetBSD: subr.c,v 1.8 2006/11/14 11:45:03 pooka Exp $");
     34  1.1     pooka #endif /* !lint */
     35  1.1     pooka 
     36  1.1     pooka #include <sys/types.h>
     37  1.1     pooka #include <sys/time.h>
     38  1.1     pooka #include <sys/vnode.h>
     39  1.1     pooka #include <sys/dirent.h>
     40  1.1     pooka 
     41  1.1     pooka #include <assert.h>
     42  1.1     pooka #include <puffs.h>
     43  1.1     pooka #include <stdlib.h>
     44  1.1     pooka #include <stdio.h>
     45  1.1     pooka #include <string.h>
     46  1.1     pooka 
     47  1.1     pooka /*
     48  1.1     pooka  * Well, you're probably wondering why this isn't optimized.
     49  1.1     pooka  * The reason is simple: my available time is not optimized for
     50  1.1     pooka  * size ... so please be patient ;)
     51  1.1     pooka  */
     52  1.1     pooka struct puffs_node *
     53  1.1     pooka puffs_newpnode(struct puffs_usermount *puser, void *privdata, enum vtype type)
     54  1.1     pooka {
     55  1.1     pooka 	struct puffs_node *pn;
     56  1.1     pooka 
     57  1.1     pooka 	pn = calloc(1, sizeof(struct puffs_node));
     58  1.1     pooka 	if (pn == NULL)
     59  1.1     pooka 		return NULL;
     60  1.1     pooka 
     61  1.1     pooka 	pn->pn_mnt = puser;
     62  1.1     pooka 	pn->pn_data = privdata;
     63  1.1     pooka 	pn->pn_type = type;
     64  1.1     pooka 
     65  1.1     pooka 	/* technically not needed */
     66  1.1     pooka 	memset(&pn->pn_va, 0, sizeof(struct vattr));
     67  1.1     pooka 	pn->pn_va.va_type = type;
     68  1.1     pooka 
     69  1.1     pooka 	LIST_INSERT_HEAD(&puser->pu_pnodelst, pn, pn_entries);
     70  1.1     pooka 
     71  1.1     pooka 	return pn;
     72  1.1     pooka }
     73  1.1     pooka 
     74  1.1     pooka void
     75  1.1     pooka puffs_putpnode(struct puffs_node *pn)
     76  1.1     pooka {
     77  1.1     pooka 
     78  1.1     pooka 	if (pn == NULL)
     79  1.1     pooka 		return;
     80  1.1     pooka 
     81  1.1     pooka 	LIST_REMOVE(pn, pn_entries);
     82  1.1     pooka 	free(pn);
     83  1.1     pooka }
     84  1.1     pooka 
     85  1.1     pooka int
     86  1.1     pooka puffs_gendotdent(struct dirent **dent, ino_t id, int dotdot, size_t *reslen)
     87  1.1     pooka {
     88  1.1     pooka 	const char *name;
     89  1.1     pooka 
     90  1.1     pooka 	assert(dotdot == 0 || dotdot == 1);
     91  1.1     pooka 	name = dotdot == 0 ? "." : "..";
     92  1.1     pooka 
     93  1.1     pooka 	return puffs_nextdent(dent, name, id, DT_DIR, reslen);
     94  1.1     pooka }
     95  1.1     pooka 
     96  1.1     pooka int
     97  1.1     pooka puffs_nextdent(struct dirent **dent, const char *name, ino_t id, uint8_t dtype,
     98  1.1     pooka 	size_t *reslen)
     99  1.1     pooka {
    100  1.1     pooka 	struct dirent *d = *dent;
    101  1.1     pooka 
    102  1.1     pooka 	/* check if we have enough room for our dent-aligned dirent */
    103  1.1     pooka 	if (_DIRENT_RECLEN(d, strlen(name)) > *reslen)
    104  1.1     pooka 		return 0;
    105  1.1     pooka 
    106  1.1     pooka 	d->d_fileno = id;
    107  1.1     pooka 	d->d_type = dtype;
    108  1.6       mrg 	d->d_namlen = (uint16_t)strlen(name);
    109  1.2  christos 	(void)memcpy(&d->d_name, name, (size_t)d->d_namlen);
    110  1.1     pooka 	d->d_name[d->d_namlen] = '\0';
    111  1.6       mrg 	d->d_reclen = (uint16_t)_DIRENT_SIZE(d);
    112  1.1     pooka 
    113  1.1     pooka 	*reslen -= d->d_reclen;
    114  1.1     pooka 	*dent = _DIRENT_NEXT(d);
    115  1.1     pooka 
    116  1.1     pooka 	return 1;
    117  1.1     pooka }
    118  1.1     pooka 
    119  1.7     pooka /*ARGSUSED*/
    120  1.7     pooka int
    121  1.8     pooka puffs_vfsnop_unmount(struct puffs_usermount *pu, int flags, pid_t pid)
    122  1.7     pooka {
    123  1.7     pooka 
    124  1.7     pooka 	/* would you like to see puffs rule again, my friend? */
    125  1.7     pooka 	return 0;
    126  1.7     pooka }
    127  1.7     pooka 
    128  1.7     pooka /*ARGSUSED*/
    129  1.7     pooka int
    130  1.8     pooka puffs_vfsnop_sync(struct puffs_usermount *pu, int waitfor,
    131  1.7     pooka 	const struct puffs_cred *cred, pid_t pid)
    132  1.7     pooka {
    133  1.7     pooka 
    134  1.7     pooka 	return 0;
    135  1.7     pooka }
    136  1.7     pooka 
    137  1.7     pooka /*ARGSUSED*/
    138  1.7     pooka int
    139  1.8     pooka puffs_vfsnop_statvfs(struct puffs_usermount *pu, struct statvfs *sbp, pid_t pid)
    140  1.7     pooka {
    141  1.7     pooka 
    142  1.7     pooka 	sbp->f_bsize = sbp->f_frsize = sbp->f_iosize = 512;
    143  1.7     pooka 
    144  1.7     pooka 	sbp->f_bfree=sbp->f_bavail=sbp->f_bresvd=sbp->f_blocks = (fsblkcnt_t)0;
    145  1.7     pooka 	sbp->f_ffree=sbp->f_favail=sbp->f_fresvd=sbp->f_files = (fsfilcnt_t)0;
    146  1.7     pooka 	sbp->f_fsidx = pu->pu_fsidx;
    147  1.7     pooka 
    148  1.7     pooka 	return 0;
    149  1.7     pooka }
    150  1.1     pooka 
    151  1.1     pooka /*
    152  1.1     pooka  * Set vattr values for those applicable (i.e. not PUFFS_VNOVAL).
    153  1.1     pooka  */
    154  1.1     pooka void
    155  1.1     pooka puffs_setvattr(struct vattr *vap, const struct vattr *sva)
    156  1.1     pooka {
    157  1.1     pooka 
    158  1.2  christos #define SETIFVAL(a, t) if (sva->a != (t)PUFFS_VNOVAL) vap->a = sva->a
    159  1.1     pooka 	if (sva->va_type != VNON)
    160  1.1     pooka 		vap->va_type = sva->va_type;
    161  1.2  christos 	SETIFVAL(va_mode, mode_t);
    162  1.2  christos 	SETIFVAL(va_nlink, nlink_t);
    163  1.2  christos 	SETIFVAL(va_uid, uid_t);
    164  1.2  christos 	SETIFVAL(va_gid, gid_t);
    165  1.2  christos 	SETIFVAL(va_fsid, long);
    166  1.2  christos 	SETIFVAL(va_size, u_quad_t);
    167  1.2  christos 	SETIFVAL(va_blocksize, long);
    168  1.2  christos 	SETIFVAL(va_atime.tv_sec, time_t);
    169  1.2  christos 	SETIFVAL(va_ctime.tv_sec, time_t);
    170  1.2  christos 	SETIFVAL(va_mtime.tv_sec, time_t);
    171  1.2  christos 	SETIFVAL(va_birthtime.tv_sec, time_t);
    172  1.2  christos 	SETIFVAL(va_atime.tv_nsec, long);
    173  1.2  christos 	SETIFVAL(va_ctime.tv_nsec, long);
    174  1.2  christos 	SETIFVAL(va_mtime.tv_nsec, long);
    175  1.2  christos 	SETIFVAL(va_birthtime.tv_nsec, long);
    176  1.2  christos 	SETIFVAL(va_gen, u_long);
    177  1.2  christos 	SETIFVAL(va_flags, u_long);
    178  1.2  christos 	SETIFVAL(va_rdev, dev_t);
    179  1.2  christos 	SETIFVAL(va_bytes, u_quad_t);
    180  1.1     pooka #undef SETIFVAL
    181  1.1     pooka 	/* ignore va->va_vaflags */
    182  1.1     pooka }
    183  1.1     pooka 
    184  1.1     pooka 
    185  1.1     pooka static int vdmap[] = {
    186  1.1     pooka 	DT_UNKNOWN,	/* VNON */
    187  1.1     pooka 	DT_REG,		/* VREG */
    188  1.1     pooka 	DT_DIR,		/* VDIR */
    189  1.1     pooka 	DT_BLK,		/* VBLK */
    190  1.1     pooka 	DT_CHR,		/* VCHR */
    191  1.1     pooka 	DT_LNK,		/* VLNK */
    192  1.1     pooka 	DT_SOCK,	/* VSUCK*/
    193  1.1     pooka 	DT_FIFO,	/* VFIFO*/
    194  1.1     pooka 	DT_UNKNOWN	/* VBAD */
    195  1.1     pooka };
    196  1.1     pooka /* XXX: DT_WHT ? */
    197  1.1     pooka int
    198  1.1     pooka puffs_vtype2dt(enum vtype vt)
    199  1.1     pooka {
    200  1.1     pooka 
    201  1.5  christos 	if ((int)vt >= VNON && vt < (sizeof(vdmap)/sizeof(vdmap[0])))
    202  1.3     pooka 		return vdmap[vt];
    203  1.1     pooka 
    204  1.3     pooka 	return DT_UNKNOWN;
    205  1.1     pooka }
    206