hfs_subr.c revision 1.7 1 /* $NetBSD: hfs_subr.c,v 1.7 2008/01/02 11:48:41 ad 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.7 2008/01/02 11:48:41 ad 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 vp->v_vflag &= ~VV_LOCKSWORK;
84 VOP_UNLOCK(vp, 0);
85 vp->v_op = specops;
86 vgone(vp);
87 lockmgr(&nvp->v_lock, LK_EXCLUSIVE,
88 &nvp->v_interlock);
89 /*
90 * Reinitialize aliased inode.
91 */
92 vp = nvp;
93 hp->h_vnode = vp;
94 }
95 break;
96 case VFIFO:
97 vp->v_op = fifoops;
98 break;
99
100 case VNON:
101 case VBAD:
102 case VSOCK:
103 case VDIR:
104 case VREG:
105 case VLNK:
106 break;
107 }
108
109 if (hp->h_rec.cnid == HFS_CNID_ROOT_FOLDER)
110 vp->v_vflag |= VV_ROOT;
111
112 *vpp = vp;
113 }
114
115 /*
116 * Callbacks for libhfs
117 */
118
119 void
120 hfs_libcb_error(
121 const char* format,
122 const char* file,
123 int line,
124 va_list args)
125 {
126 #ifdef HFS_DEBUG
127 if (file != NULL)
128 printf("%s:%i: ", file, line);
129 else
130 printf("hfs: ");
131 #else
132 printf("hfs: ");
133 #endif
134
135 /* XXX Should we really display this if debugging is off? */
136 vprintf(format, args);
137 printf("\n");
138 }
139
140 /* XXX change malloc/realloc/free to use pools */
141
142 void*
143 hfs_libcb_malloc(size_t size, hfs_callback_args* cbargs)
144 {
145 return malloc(size, /*M_HFSMNT*/ M_TEMP, M_WAITOK);
146 }
147
148 void*
149 hfs_libcb_realloc(void* ptr, size_t size, hfs_callback_args* cbargs)
150 {
151 return realloc(ptr, size, /*M_HFSMNT*/ M_TEMP, M_WAITOK);
152 }
153
154 void
155 hfs_libcb_free(void* ptr, hfs_callback_args* cbargs)
156 {
157 free(ptr, /*M_HFSMNT*/ M_TEMP);
158 }
159
160 /*
161 * hfs_libcb_opendev()
162 *
163 * hfslib uses this callback to open a volume's device node by name. However,
164 * by the time this is called here, the device node has already been opened by
165 * VFS. So we are passed the vnode to this volume's block device and use that
166 * instead of the device's name.
167 */
168 int
169 hfs_libcb_opendev(
170 hfs_volume* vol,
171 const char* devname,
172 hfs_callback_args* cbargs)
173 {
174 hfs_libcb_data* cbdata = NULL;
175 hfs_libcb_argsopen* args;
176 struct partinfo dpart;
177 int result;
178
179 result = 0;
180 args = (hfs_libcb_argsopen*)(cbargs->openvol);
181
182 if (vol == NULL || devname == NULL) {
183 result = EINVAL;
184 goto error;
185 }
186
187 cbdata = malloc(sizeof(hfs_libcb_data), M_HFSMNT, M_WAITOK);
188 if (cbdata == NULL) {
189 result = ENOMEM;
190 goto error;
191 }
192 vol->cbdata = cbdata;
193
194 cbdata->devvp = NULL;
195
196 /* Open the device node. */
197 if ((result = VOP_OPEN(args->devvp, vol->readonly? FREAD : FREAD|FWRITE,
198 FSCRED)) != 0)
199 goto error;
200
201 /* Flush out any old buffers remaining from a previous use. */
202 vn_lock(args->devvp, LK_EXCLUSIVE | LK_RETRY);
203 result = vinvalbuf(args->devvp, V_SAVE, args->cred, args->l, 0, 0);
204 VOP_UNLOCK(args->devvp, 0);
205 if (result != 0)
206 goto error;
207
208 cbdata->devvp = args->devvp;
209
210 /* Determine the device's block size. Default to DEV_BSIZE if unavailable.*/
211 if (VOP_IOCTL(args->devvp, DIOCGPART, &dpart, FREAD, args->cred)
212 != 0)
213 cbdata->devblksz = DEV_BSIZE;
214 else
215 cbdata->devblksz = dpart.disklab->d_secsize;
216
217 return 0;
218
219 error:
220 if (cbdata != NULL) {
221 if (cbdata->devvp != NULL) {
222 vn_lock(cbdata->devvp, LK_EXCLUSIVE | LK_RETRY);
223 (void)VOP_CLOSE(cbdata->devvp, vol->readonly ? FREAD :
224 FREAD | FWRITE, NOCRED);
225 VOP_UNLOCK(cbdata->devvp, 0);
226 }
227 free(cbdata, M_HFSMNT);
228 vol->cbdata = NULL;
229 }
230
231 return result;
232 }
233
234 void
235 hfs_libcb_closedev(hfs_volume* in_vol, hfs_callback_args* cbargs)
236 {
237 struct vnode *devvp;
238
239 if (in_vol == NULL)
240 return;
241
242 if (in_vol->cbdata != NULL) {
243 devvp = ((hfs_libcb_data*)in_vol->cbdata)->devvp;
244 if (devvp != NULL) {
245 vn_lock(devvp, LK_EXCLUSIVE | LK_RETRY);
246 (void)VOP_CLOSE(devvp,
247 in_vol->readonly ? FREAD : FREAD | FWRITE, NOCRED);
248 /* XXX do we need a VOP_UNLOCK() here? */
249 }
250
251 free(in_vol->cbdata, M_HFSMNT);
252 in_vol->cbdata = NULL;
253 }
254 }
255
256 int
257 hfs_libcb_read(
258 hfs_volume* vol,
259 void* outbytes,
260 uint64_t length,
261 uint64_t offset,
262 hfs_callback_args* cbargs)
263 {
264 hfs_libcb_data *cbdata;
265 hfs_libcb_argsread* argsread;
266 kauth_cred_t cred;
267 uint64_t physoffset; /* physical offset from start of device(?) */
268
269 if (vol == NULL || outbytes == NULL)
270 return -1;
271
272 cbdata = (hfs_libcb_data*)vol->cbdata;
273
274 if (cbargs != NULL
275 && (argsread = (hfs_libcb_argsread*)cbargs->read) != NULL
276 && argsread->cred != NULL)
277 cred = argsread->cred;
278 else
279 cred = NOCRED;
280
281 /*
282 * Since bread() only reads data in terms of integral blocks, it may have
283 * read some data before and/or after our desired offset & length. So when
284 * copying that data into the outgoing buffer, start at the actual desired
285 * offset and only copy the desired length.
286 */
287 physoffset = offset + vol->offset;
288
289 return hfs_pread(cbdata->devvp, outbytes, cbdata->devblksz, physoffset,
290 length, cred);
291 }
292
293 /*
294 * So it turns out that bread() is pretty shoddy. It not only requires the size
295 * parameter to be an integral multiple of the device's block size, but also
296 * requires the block number to be on a boundary of that same block size -- and
297 * yet be given as an integral multiple of DEV_BSIZE! So after much toil and
298 * bloodshed, hfs_pread() was written as a convenience (and a model of how sane
299 * people take their bread()). Returns 0 on success.
300 */
301 int
302 hfs_pread(struct vnode *vp, void *buf, size_t secsz, uint64_t off,
303 uint64_t len, kauth_cred_t cred)
304 {
305 struct buf *bp;
306 uint64_t curoff; /* relative to 'start' variable */
307 uint64_t start;
308 int error;
309
310 if (vp == NULL || buf == NULL)
311 return EINVAL;
312
313 if (len == 0)
314 return 0;
315
316 curoff = 0;
317 error = 0;
318
319 /* align offset to highest preceding sector boundary */
320 #define ABSZ(x, bsz) (((x)/(bsz))*(bsz))
321
322 /* round size up to integral # of block sizes */
323 #define RBSZ(x, bsz) (((x) + (bsz) - 1) & ~((bsz) - 1))
324
325 start = ABSZ(off, secsz);
326 while (start + curoff < off + len)
327 {
328 bp = NULL;
329
330 /* XXX Does the algorithm always do what's intended here when
331 * XXX start != off? Need to test this. */
332
333 error = bread(vp, (start + curoff) / DEV_BSIZE,/* no rounding involved*/
334 RBSZ(min(len - curoff + (off - start), MAXBSIZE), secsz), cred, &bp);
335
336 if (error == 0)
337 memcpy((uint8_t*)buf + curoff, (uint8_t*)bp->b_data +
338 (off - start), min(len - curoff, MAXBSIZE - (off - start)));
339
340 if (bp != NULL)
341 brelse(bp, 0);
342 if (error != 0)
343 return error;
344
345 curoff += MAXBSIZE;
346 }
347 #undef ABSZ
348 #undef RBSZ
349
350 return 0;
351 }
352
353 /* XXX Provide a routine to take a catalog record and return its proper BSD file
354 * XXX or directory mode value */
355
356
357 /* Convert from HFS+ time representation to UNIX time since epoch. */
358 void
359 hfs_time_to_timespec(uint32_t hfstime, struct timespec *unixtime)
360 {
361 /*
362 * HFS+ time is calculated in seconds since midnight, Jan 1st, 1904.
363 * struct timespec counts from midnight, Jan 1st, 1970. Thus, there is
364 * precisely a 66 year difference between them, which is equal to
365 * 2,082,844,800 seconds. No, I didn't count them by hand.
366 */
367
368 if (hfstime < 2082844800)
369 unixtime->tv_sec = 0; /* dates before 1970 are bs anyway, so use epoch*/
370 else
371 unixtime->tv_sec = hfstime - 2082844800;
372
373 unixtime->tv_nsec = 0; /* we don't have nanosecond resolution */
374 }
375
376 /*
377 * Endian conversion with automatic pointer incrementation.
378 */
379
380 uint16_t be16tohp(void** inout_ptr)
381 {
382 uint16_t result;
383 uint16_t *ptr;
384
385 if(inout_ptr==NULL)
386 return 0;
387
388 ptr = *inout_ptr;
389
390 result = be16toh(*ptr);
391
392 ptr++;
393 *inout_ptr = ptr;
394
395 return result;
396 }
397
398 uint32_t be32tohp(void** inout_ptr)
399 {
400 uint32_t result;
401 uint32_t *ptr;
402
403 if(inout_ptr==NULL)
404 return 0;
405
406 ptr = *inout_ptr;
407
408 result = be32toh(*ptr);
409
410 ptr++;
411 *inout_ptr = ptr;
412
413 return result;
414 }
415
416 uint64_t be64tohp(void** inout_ptr)
417 {
418 uint64_t result;
419 uint64_t *ptr;
420
421 if(inout_ptr==NULL)
422 return 0;
423
424 ptr = *inout_ptr;
425
426 result = be64toh(*ptr);
427
428 ptr++;
429 *inout_ptr = ptr;
430
431 return result;
432 }
433
434 enum vtype
435 hfs_catalog_keyed_record_vtype(const hfs_catalog_keyed_record_t *rec)
436 {
437 if (rec->type == HFS_REC_FILE) {
438 uint32_t mode;
439
440 mode = ((const hfs_file_record_t *)rec)->bsd.file_mode;
441 if (mode != 0)
442 return IFTOVT(mode);
443 else
444 return VREG;
445 }
446 else
447 return VDIR;
448 }
449