Home | History | Annotate | Line # | Download | only in hfs
hfs_subr.c revision 1.2
      1 /*	$NetBSD: hfs_subr.c,v 1.2 2007/03/06 11:28:48 dillo Exp $	*/
      2 
      3 /*-
      4  * Copyright (c) 2005, 2007 The NetBSD Foundation, Inc.
      5  * All rights reserved.
      6  *
      7  * This code is derived from software contributed to The NetBSD Foundation
      8  * by Yevgeny Binder and Dieter Baron.
      9  *
     10  * Redistribution and use in source and binary forms, with or without
     11  * modification, are permitted provided that the following conditions
     12  * are met:
     13  * 1. Redistributions of source code must retain the above copyright
     14  *    notice, this list of conditions and the following disclaimer.
     15  * 2. Redistributions in binary form must reproduce the above copyright
     16  *    notice, this list of conditions and the following disclaimer in the
     17  *    documentation and/or other materials provided with the distribution.
     18  *
     19  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     20  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     21  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     22  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     23  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     24  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     25  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     26  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     27  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     28  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     29  * POSSIBILITY OF SUCH DAMAGE.
     30  */
     31 
     32 #include <sys/cdefs.h>
     33 __KERNEL_RCSID(0, "$NetBSD: hfs_subr.c,v 1.2 2007/03/06 11:28:48 dillo Exp $");
     34 
     35 #include <sys/param.h>
     36 #include <sys/systm.h>
     37 #include <sys/time.h>
     38 #include <sys/kernel.h>
     39 #include <sys/proc.h>
     40 #include <sys/vnode.h>
     41 #include <sys/malloc.h>
     42 #include <sys/stat.h>
     43 #include <sys/file.h>
     44 #include <sys/filedesc.h>
     45 #include <sys/mount.h>
     46 #include <sys/disklabel.h>
     47 #include <sys/conf.h>
     48 #include <sys/kauth.h>
     49 
     50 #include <fs/hfs/hfs.h>
     51 
     52 /*
     53  * Initialize the vnode associated with a new hfsnode.
     54  */
     55 void
     56 hfs_vinit(struct mount *mp, int (**specops)(void *), int (**fifoops)(void *),
     57 	   struct vnode **vpp)
     58 {
     59 	struct hfsnode	*hp;
     60 	struct vnode	*vp;
     61 	struct vnode    *nvp;
     62 
     63 	vp = *vpp;
     64 	hp = VTOH(vp);
     65 
     66 	vp->v_type = hfs_catalog_keyed_record_vtype(
     67 		(hfs_catalog_keyed_record_t *)&hp->h_rec);
     68 
     69 	switch(vp->v_type) {
     70 		case VCHR:
     71 		case VBLK:
     72 			vp->v_op = specops;
     73 			if ((nvp = checkalias(vp,
     74 					      HFS_CONVERT_RDEV(hp->h_rec.file.bsd.special.raw_device),
     75 					      mp)) != NULL) {
     76 			    /*
     77 			     * Discard unneeded vnode, but save its inode.
     78 			     */
     79 			    nvp->v_data = vp->v_data;
     80 			    vp->v_data = NULL;
     81 			    /* XXX spec_vnodeops has no locking,
     82 			       do it explicitly */
     83 			    VOP_UNLOCK(vp, 0);
     84 			    vp->v_op = specops;
     85 			    vp->v_flag &= ~VLOCKSWORK;
     86 			    vrele(vp);
     87 			    vgone(vp);
     88 			    lockmgr(&nvp->v_lock, LK_EXCLUSIVE,
     89 				    &nvp->v_interlock);
     90 			    /*
     91 			     * Reinitialize aliased inode.
     92 			     */
     93 			    vp = nvp;
     94 			    hp->h_vnode = vp;
     95 			}
     96 			break;
     97 		case VFIFO:
     98 			vp->v_op = fifoops;
     99 			break;
    100 
    101 		case VNON:
    102 		case VBAD:
    103 		case VSOCK:
    104 		case VDIR:
    105 		case VREG:
    106 		case VLNK:
    107 			break;
    108 	}
    109 
    110 	if (hp->h_rec.cnid == HFS_CNID_ROOT_FOLDER)
    111 		vp->v_flag |= VROOT;
    112 
    113 	*vpp = vp;
    114 }
    115 
    116 /*
    117  * Callbacks for libhfs
    118  */
    119 
    120 void
    121 hfs_libcb_error(
    122 	const char* format,
    123 	const char* file,
    124 	int line,
    125 	va_list args)
    126 {
    127 #ifdef HFS_DEBUG
    128 	if (file != NULL)
    129 		printf("%s:%i: ", file, line);
    130 	else
    131 		printf("hfs: ");
    132 #else
    133 	printf("hfs: ");
    134 #endif
    135 
    136 	/* XXX Should we really display this if debugging is off? */
    137 	vprintf(format, args);
    138 	printf("\n");
    139 }
    140 
    141 /* XXX change malloc/realloc/free to use pools */
    142 
    143 void*
    144 hfs_libcb_malloc(size_t size, hfs_callback_args* cbargs)
    145 {
    146 	return malloc(size, /*M_HFSMNT*/ M_TEMP, M_WAITOK);
    147 }
    148 
    149 void*
    150 hfs_libcb_realloc(void* ptr, size_t size, hfs_callback_args* cbargs)
    151 {
    152 	return realloc(ptr, size, /*M_HFSMNT*/ M_TEMP, M_WAITOK);
    153 }
    154 
    155 void
    156 hfs_libcb_free(void* ptr, hfs_callback_args* cbargs)
    157 {
    158 	free(ptr, /*M_HFSMNT*/ M_TEMP);
    159 }
    160 
    161 /*
    162  * hfs_libcb_opendev()
    163  *
    164  * hfslib uses this callback to open a volume's device node by name. However,
    165  * by the time this is called here, the device node has already been opened by
    166  * VFS. So we are passed the vnode to this volume's block device and use that
    167  * instead of the device's name.
    168  */
    169 int
    170 hfs_libcb_opendev(
    171 	hfs_volume* vol,
    172 	const char* devname,
    173 	uint64_t voloffset,
    174 	hfs_callback_args* cbargs)
    175 {
    176 	hfs_libcb_data* cbdata = NULL;
    177 	hfs_libcb_argsopen* args;
    178 	struct partinfo dpart;
    179 	int result;
    180 
    181 	result = 0;
    182 	args = (hfs_libcb_argsopen*)(cbargs->openvol);
    183 
    184 	if (vol == NULL || devname == NULL) {
    185 		result = EINVAL;
    186 		goto error;
    187 	}
    188 
    189 	cbdata = malloc(sizeof(hfs_libcb_data), M_HFSMNT, M_WAITOK);
    190 	if (cbdata == NULL) {
    191 		result = ENOMEM;
    192 		goto error;
    193 	}
    194 	vol->cbdata = cbdata;
    195 
    196 	cbdata->offset = voloffset;
    197 	cbdata->devvp = NULL;
    198 
    199 	/* Open the device node. */
    200 	if ((result = VOP_OPEN(args->devvp, vol->readonly? FREAD : FREAD|FWRITE,
    201 		FSCRED, args->l)) != 0)
    202 		goto error;
    203 
    204 	/* Flush out any old buffers remaining from a previous use. */
    205 	vn_lock(args->devvp, LK_EXCLUSIVE | LK_RETRY);
    206 	result = vinvalbuf(args->devvp, V_SAVE, args->cred, args->l, 0, 0);
    207 	VOP_UNLOCK(args->devvp, 0);
    208 	if (result != 0)
    209 		goto error;
    210 
    211 	cbdata->devvp = args->devvp;
    212 
    213 	/* Determine the device's block size. Default to DEV_BSIZE if unavailable.*/
    214 	if (VOP_IOCTL(args->devvp, DIOCGPART, &dpart, FREAD, args->cred, args->l)
    215 		!= 0)
    216 		cbdata->devblksz = DEV_BSIZE;
    217 	else
    218 		cbdata->devblksz = dpart.disklab->d_secsize;
    219 
    220 	return 0;
    221 
    222 error:
    223 	if (cbdata != NULL) {
    224 		if (cbdata->devvp != NULL) {
    225 			vn_lock(cbdata->devvp, LK_EXCLUSIVE | LK_RETRY);
    226 			(void)VOP_CLOSE(cbdata->devvp, vol->readonly ? FREAD :
    227 				FREAD | FWRITE, NOCRED, args->l);
    228 			VOP_UNLOCK(cbdata->devvp, 0);
    229 		}
    230 		free(cbdata, M_HFSMNT);
    231 		vol->cbdata = NULL;
    232 	}
    233 
    234 	return result;
    235 }
    236 
    237 void
    238 hfs_libcb_closedev(hfs_volume* in_vol, hfs_callback_args* cbargs)
    239 {
    240 	struct vnode *devvp;
    241 
    242 	if (in_vol == NULL)
    243 		return;
    244 
    245 	if (in_vol->cbdata != NULL) {
    246 		devvp = ((hfs_libcb_data*)in_vol->cbdata)->devvp;
    247 		if (devvp != NULL) {
    248 			vn_lock(devvp, LK_EXCLUSIVE | LK_RETRY);
    249 			(void)VOP_CLOSE(devvp, in_vol->readonly ? FREAD : FREAD | FWRITE,
    250 				NOCRED, ((hfs_libcb_argsclose*)cbargs->closevol)->l);
    251 			/* XXX do we need a VOP_UNLOCK() here? */
    252 		}
    253 
    254 		free(in_vol->cbdata, M_HFSMNT);
    255 		in_vol->cbdata = NULL;
    256 	}
    257 }
    258 
    259 int
    260 hfs_libcb_read(
    261 	hfs_volume* vol,
    262 	void* outbytes,
    263 	uint64_t length,
    264 	uint64_t offset,
    265 	hfs_callback_args* cbargs)
    266 {
    267 	hfs_libcb_data *cbdata;
    268 	hfs_libcb_argsread* argsread;
    269 	kauth_cred_t cred;
    270 	uint64_t physoffset; /* physical offset from start of device(?) */
    271 
    272 	if (vol == NULL || outbytes == NULL)
    273 		return -1;
    274 
    275 	cbdata = (hfs_libcb_data*)vol->cbdata;
    276 
    277 	if (cbargs != NULL
    278 		&& (argsread = (hfs_libcb_argsread*)cbargs->read) != NULL
    279 		&& argsread->cred != NULL)
    280 		cred = argsread->cred;
    281 	else
    282 		cred = NOCRED;
    283 
    284 	/*
    285 	 * Since bread() only reads data in terms of integral blocks, it may have
    286 	 * read some data before and/or after our desired offset & length. So when
    287 	 * copying that data into the outgoing buffer, start at the actual desired
    288 	 * offset and only copy the desired length.
    289 	 */
    290 	physoffset = offset + /* XXX Temporary */ cbdata->offset;
    291 
    292 	return hfs_pread(cbdata->devvp, outbytes, cbdata->devblksz, physoffset,
    293 			length, cred);
    294 }
    295 
    296 /*
    297  * So it turns out that bread() is pretty shoddy. It not only requires the size
    298  * parameter to be an integral multiple of the device's block size, but also
    299  * requires the block number to be on a boundary of that same block size -- and
    300  * yet be given as an integral multiple of DEV_BSIZE! So after much toil and
    301  * bloodshed, hfs_pread() was written as a convenience (and a model of how sane
    302  * people take their bread()). Returns 0 on success.
    303  */
    304 int
    305 hfs_pread(struct vnode *vp, void *buf, size_t secsz, uint64_t off,
    306 	uint64_t len, kauth_cred_t cred)
    307 {
    308 	struct buf *bp;
    309 	uint64_t curoff; /* relative to 'start' variable */
    310 	uint64_t start;
    311 	int error;
    312 
    313 	if (vp == NULL || buf == NULL)
    314 		return EINVAL;
    315 
    316 	if (len == 0)
    317 		return 0;
    318 
    319 	curoff = 0;
    320 	error = 0;
    321 
    322 /* align offset to highest preceding sector boundary */
    323 #define ABSZ(x, bsz) (((x)/(bsz))*(bsz))
    324 
    325 /* round size up to integral # of block sizes */
    326 #define RBSZ(x, bsz) (((x) + (bsz) - 1) & ~((bsz) - 1))
    327 
    328 	start = ABSZ(off, secsz);
    329 	while (start + curoff < off + len)
    330 	{
    331 		bp = NULL;
    332 
    333 		/* XXX  Does the algorithm always do what's intended here when
    334 		 * XXX  start != off? Need to test this. */
    335 
    336 		error = bread(vp, (start + curoff) / DEV_BSIZE,/* no rounding involved*/
    337 		   RBSZ(min(len - curoff + (off - start), MAXBSIZE), secsz), cred, &bp);
    338 
    339 		if (error == 0)
    340 			memcpy((uint8_t*)buf + curoff, (uint8_t*)bp->b_data +
    341 				(off - start), min(len - curoff, MAXBSIZE - (off - start)));
    342 
    343 		if (bp != NULL)
    344 			brelse(bp);
    345 		if (error != 0)
    346 			return error;
    347 
    348 		curoff += MAXBSIZE;
    349 	}
    350 #undef ABSZ
    351 #undef RBSZ
    352 
    353 	return 0;
    354 }
    355 
    356 /* XXX Provide a routine to take a catalog record and return its proper BSD file
    357  * XXX or directory mode value */
    358 
    359 
    360 /* Convert from HFS+ time representation to UNIX time since epoch. */
    361 void
    362 hfs_time_to_timespec(uint32_t hfstime, struct timespec *unixtime)
    363 {
    364 	/*
    365 	 * HFS+ time is calculated in seconds since midnight, Jan 1st, 1904.
    366 	 * struct timespec counts from midnight, Jan 1st, 1970. Thus, there is
    367 	 * precisely a 66 year difference between them, which is equal to
    368 	 * 2,082,844,800 seconds. No, I didn't count them by hand.
    369 	 */
    370 
    371 	if (hfstime < 2082844800)
    372 		unixtime->tv_sec = 0; /* dates before 1970 are bs anyway, so use epoch*/
    373 	else
    374 		unixtime->tv_sec = hfstime - 2082844800;
    375 
    376 	unixtime->tv_nsec = 0; /* we don't have nanosecond resolution */
    377 }
    378 
    379 /*
    380  * Endian conversion with automatic pointer incrementation.
    381  */
    382 
    383 uint16_t be16tohp(void** inout_ptr)
    384 {
    385 	uint16_t	result;
    386 	uint16_t *ptr;
    387 
    388 	if(inout_ptr==NULL)
    389 		return 0;
    390 
    391 	ptr = *inout_ptr;
    392 
    393 	result = be16toh(*ptr);
    394 
    395 	ptr++;
    396 	*inout_ptr = ptr;
    397 
    398 	return result;
    399 }
    400 
    401 uint32_t be32tohp(void** inout_ptr)
    402 {
    403 	uint32_t	result;
    404 	uint32_t *ptr;
    405 
    406 	if(inout_ptr==NULL)
    407 		return 0;
    408 
    409 	ptr = *inout_ptr;
    410 
    411 	result = be32toh(*ptr);
    412 
    413 	ptr++;
    414 	*inout_ptr = ptr;
    415 
    416 	return result;
    417 }
    418 
    419 uint64_t be64tohp(void** inout_ptr)
    420 {
    421 	uint64_t	result;
    422 	uint64_t *ptr;
    423 
    424 	if(inout_ptr==NULL)
    425 		return 0;
    426 
    427 	ptr = *inout_ptr;
    428 
    429 	result = be64toh(*ptr);
    430 
    431 	ptr++;
    432 	*inout_ptr = ptr;
    433 
    434 	return result;
    435 }
    436 
    437 enum vtype
    438 hfs_catalog_keyed_record_vtype(const hfs_catalog_keyed_record_t *rec)
    439 {
    440     	if (rec->type == HFS_REC_FILE) {
    441 		uint32_t mode;
    442 
    443 		mode = ((const hfs_file_record_t *)rec)->bsd.file_mode;
    444 		if (mode != 0)
    445 			return IFTOVT(mode);
    446 		else
    447 			return VREG;
    448 	}
    449 	else
    450 		return VDIR;
    451 }
    452