Home | History | Annotate | Line # | Download | only in libpuffs
subr.c revision 1.12
      1 /*	$NetBSD: subr.c,v 1.12 2007/01/06 18:22:09 pooka Exp $	*/
      2 
      3 /*
      4  * Copyright (c) 2006 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: subr.c,v 1.12 2007/01/06 18:22:09 pooka Exp $");
     34 #endif /* !lint */
     35 
     36 #include <sys/types.h>
     37 #include <sys/time.h>
     38 #include <sys/vnode.h>
     39 #include <sys/dirent.h>
     40 
     41 #include <assert.h>
     42 #include <puffs.h>
     43 #include <stdlib.h>
     44 #include <stdio.h>
     45 #include <string.h>
     46 #include <unistd.h>
     47 
     48 
     49 int
     50 puffs_gendotdent(struct dirent **dent, ino_t id, int dotdot, size_t *reslen)
     51 {
     52 	const char *name;
     53 
     54 	assert(dotdot == 0 || dotdot == 1);
     55 	name = dotdot == 0 ? "." : "..";
     56 
     57 	return puffs_nextdent(dent, name, id, DT_DIR, reslen);
     58 }
     59 
     60 int
     61 puffs_nextdent(struct dirent **dent, const char *name, ino_t id, uint8_t dtype,
     62 	size_t *reslen)
     63 {
     64 	struct dirent *d = *dent;
     65 
     66 	/* check if we have enough room for our dent-aligned dirent */
     67 	if (_DIRENT_RECLEN(d, strlen(name)) > *reslen)
     68 		return 0;
     69 
     70 	d->d_fileno = id;
     71 	d->d_type = dtype;
     72 	d->d_namlen = (uint16_t)strlen(name);
     73 	(void)memcpy(&d->d_name, name, (size_t)d->d_namlen);
     74 	d->d_name[d->d_namlen] = '\0';
     75 	d->d_reclen = (uint16_t)_DIRENT_SIZE(d);
     76 
     77 	*reslen -= d->d_reclen;
     78 	*dent = _DIRENT_NEXT(d);
     79 
     80 	return 1;
     81 }
     82 
     83 /*ARGSUSED*/
     84 int
     85 puffs_fsnop_unmount(struct puffs_cc *dontuse1, int dontuse2, pid_t dontuse3)
     86 {
     87 
     88 	/* would you like to see puffs rule again, my friend? */
     89 	return 0;
     90 }
     91 
     92 /*ARGSUSED*/
     93 int
     94 puffs_fsnop_sync(struct puffs_cc *dontuse1, int dontuse2,
     95 	const struct puffs_cred *dontuse3, pid_t dontuse4)
     96 {
     97 
     98 	return 0;
     99 }
    100 
    101 /*ARGSUSED*/
    102 int
    103 puffs_fsnop_statvfs(struct puffs_cc *dontuse1, struct statvfs *sbp,
    104 	pid_t dontuse2)
    105 {
    106 
    107 	sbp->f_bsize = sbp->f_frsize = sbp->f_iosize = 512;
    108 
    109 	sbp->f_bfree=sbp->f_bavail=sbp->f_bresvd=sbp->f_blocks = (fsblkcnt_t)0;
    110 	sbp->f_ffree=sbp->f_favail=sbp->f_fresvd=sbp->f_files = (fsfilcnt_t)0;
    111 
    112 	return 0;
    113 }
    114 
    115 /*
    116  * Just a wrapper to make calling the above nicer without having to pass
    117  * NULLs from application code
    118  */
    119 void
    120 puffs_zerostatvfs(struct statvfs *sbp)
    121 {
    122 
    123 	puffs_fsnop_statvfs(NULL, sbp, 0);
    124 }
    125 
    126 /*
    127  * Set vattr values for those applicable (i.e. not PUFFS_PUFFS_VNOVAL).
    128  */
    129 void
    130 puffs_setvattr(struct vattr *vap, const struct vattr *sva)
    131 {
    132 
    133 #define SETIFVAL(a, t) if (sva->a != (t)PUFFS_VNOVAL) vap->a = sva->a
    134 	if (sva->va_type != VNON)
    135 		vap->va_type = sva->va_type;
    136 	SETIFVAL(va_mode, mode_t);
    137 	SETIFVAL(va_nlink, nlink_t);
    138 	SETIFVAL(va_uid, uid_t);
    139 	SETIFVAL(va_gid, gid_t);
    140 	SETIFVAL(va_fsid, long);
    141 	SETIFVAL(va_size, u_quad_t);
    142 	SETIFVAL(va_fileid, ino_t);
    143 	SETIFVAL(va_blocksize, long);
    144 	SETIFVAL(va_atime.tv_sec, time_t);
    145 	SETIFVAL(va_ctime.tv_sec, time_t);
    146 	SETIFVAL(va_mtime.tv_sec, time_t);
    147 	SETIFVAL(va_birthtime.tv_sec, time_t);
    148 	SETIFVAL(va_atime.tv_nsec, long);
    149 	SETIFVAL(va_ctime.tv_nsec, long);
    150 	SETIFVAL(va_mtime.tv_nsec, long);
    151 	SETIFVAL(va_birthtime.tv_nsec, long);
    152 	SETIFVAL(va_gen, u_long);
    153 	SETIFVAL(va_flags, u_long);
    154 	SETIFVAL(va_rdev, dev_t);
    155 	SETIFVAL(va_bytes, u_quad_t);
    156 #undef SETIFVAL
    157 	/* ignore va->va_vaflags */
    158 }
    159 
    160 void
    161 puffs_vattr_null(struct vattr *vap)
    162 {
    163 
    164 	vap->va_type = VNON;
    165 
    166 	/*
    167 	 * Assign individually so that it is safe even if size and
    168 	 * sign of each member are varied.
    169 	 */
    170 	vap->va_mode = (mode_t)PUFFS_VNOVAL;
    171 	vap->va_nlink = (nlink_t)PUFFS_VNOVAL;
    172 	vap->va_uid = (uid_t)PUFFS_VNOVAL;
    173 	vap->va_gid = (gid_t)PUFFS_VNOVAL;
    174 	vap->va_fsid = PUFFS_VNOVAL;
    175 	vap->va_fileid = (ino_t)PUFFS_VNOVAL;
    176 	vap->va_size = (u_quad_t)PUFFS_VNOVAL;
    177 	vap->va_blocksize = sysconf(_SC_PAGESIZE);
    178 	    vap->va_atime.tv_sec =
    179 	    vap->va_mtime.tv_sec =
    180 	    vap->va_ctime.tv_sec =
    181 	vap->va_birthtime.tv_sec = PUFFS_VNOVAL;
    182 	    vap->va_atime.tv_nsec =
    183 	    vap->va_mtime.tv_nsec =
    184 	    vap->va_ctime.tv_nsec =
    185 	vap->va_birthtime.tv_nsec = PUFFS_VNOVAL;
    186 	vap->va_rdev = (dev_t)PUFFS_VNOVAL;
    187 	vap->va_bytes = (u_quad_t)PUFFS_VNOVAL;
    188 
    189 	vap->va_flags = 0;
    190 	vap->va_gen = 0;
    191 	vap->va_vaflags = 0;
    192 }
    193 
    194 static int vdmap[] = {
    195 	DT_UNKNOWN,	/* VNON */
    196 	DT_REG,		/* VREG */
    197 	DT_DIR,		/* VDIR */
    198 	DT_BLK,		/* VBLK */
    199 	DT_CHR,		/* VCHR */
    200 	DT_LNK,		/* VLNK */
    201 	DT_SOCK,	/* VSUCK*/
    202 	DT_FIFO,	/* VFIFO*/
    203 	DT_UNKNOWN	/* VBAD */
    204 };
    205 /* XXX: DT_WHT ? */
    206 int
    207 puffs_vtype2dt(enum vtype vt)
    208 {
    209 
    210 	if ((int)vt >= VNON && vt < (sizeof(vdmap)/sizeof(vdmap[0])))
    211 		return vdmap[vt];
    212 
    213 	return DT_UNKNOWN;
    214 }
    215 
    216 enum vtype
    217 puffs_mode2vt(mode_t mode)
    218 {
    219 
    220 	switch (mode & S_IFMT) {
    221 	case S_IFIFO:
    222 		return VFIFO;
    223 	case S_IFCHR:
    224 		return VCHR;
    225 	case S_IFDIR:
    226 		return VDIR;
    227 	case S_IFBLK:
    228 		return VBLK;
    229 	case S_IFREG:
    230 		return VREG;
    231 	case S_IFLNK:
    232 		return VLNK;
    233 	case S_IFSOCK:
    234 		return VSOCK;
    235 	default:
    236 		return VBAD; /* XXX: not really true, but ... */
    237 	}
    238 }
    239