2 * Copyright (c) 2000-2001 Apple Computer, Inc. All rights reserved.
4 * @APPLE_LICENSE_HEADER_START@
6 * The contents of this file constitute Original Code as defined in and
7 * are subject to the Apple Public Source License Version 1.1 (the
8 * "License"). You may not use this file except in compliance with the
9 * License. Please obtain a copy of the License at
10 * http://www.apple.com/publicsource and read it before using this file.
12 * This Original Code and all software distributed under the License are
13 * distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, EITHER
14 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
15 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT. Please see the
17 * License for the specific language governing rights and limitations
20 * @APPLE_LICENSE_HEADER_END@
22 /* @(#)MacOSStubs.c 4.0
24 * (c) 1997-1999 Apple Computer, Inc. All Rights Reserved
26 * MacOSStubs.c -- Contains routines called by MacOS code, that is not defined.
29 * 9-9-99 Don Brady Don't use MNT_WAIT in C_FlushMDB.
30 * 9-Mar-1999 Don Brady Remove more obsolete routines, add ClearMemory(bzero).
31 * 20-Nov-1998 Don Brady Remove UFSToHFSStr and HFSToUFSStr routines (obsolete).
32 * 31-Aug-1998 Don Brady Move DST adjustments to GetTimeLocal (radar #2265075).
33 * 28-Jul-1998 Don Brady Add GetDiskBlocks routine (radar #2258148).
34 * 23-Jul-1998 Don Brady Use bdwrite instead of bwrite for default in RelBlock_glue (radar #2257225).
35 * 7-Jul-1998 Don Brady Remove character mappings from/to hfs (ufs_hfs and hfs_ufs tables).
36 * 22-Jun-1998 Pat Dirks Added the vice versa mappings in ufs_hfs and hfs_ufs to more
37 * thoroughly interchange ":" and "/" in name strings.
38 * 4-Jun-1998 Pat Dirks Changed to do all B*-Tree writes synchronously (FORCESYNCBTREEWRITES = 1)
39 * 4-jun-1998 Don Brady Use VPUT macro instead of vput.
40 * 6-may-1998 Don Brady Bump h_devvp refcount in GetInitializedVNode (radar #2232480).
41 * 27-apr-1998 Don Brady Change printf to kprintf.
42 * 23-Apr-1998 Pat Dirks Cleaned up GetBlock_glue to add brelse on I/O errors from bread.
43 * 23-apr-1998 Don Brady Add '/' to ':' mapping and vice versa to mapping tables.
44 * 21-apr-1998 Don Brady Clean up time/date conversion routines.
45 * 11-apr-1998 Don Brady Add RequireFileLock routine.
46 * 8-apr-1998 Don Brady C_FlushMDB now calls hfs_flushvolumeheader and hfs_flushMDB.
47 * 12-nov-1997 Scott Roberts
48 * Initially created file.
51 #include <sys/param.h>
52 #include <sys/systm.h>
53 #include <sys/kernel.h>
54 #include <sys/vnode.h>
56 #include <sys/malloc.h>
57 #include <sys/mount.h>
58 #include <sys/types.h>
65 #include "hfscommon/headers/FileMgrInternal.h"
67 extern int (**hfs_vnodeop_p
)(void *);
71 * gTimeZone should only be used for HFS volumes!
72 * It is initialized when an HFS volume is mounted.
74 struct timezone gTimeZone
= {8*60,1};
78 /* Creates a new vnode to hold a psuedo file like an extents tree file */
81 OSStatus
GetInitializedVNode(struct hfsmount
*hfsmp
, struct vnode
**tmpvnode
)
85 struct vnode
*vp
= NULL
;
88 DBG_ASSERT(hfsmp
!= NULL
);
89 DBG_ASSERT(tmpvnode
!= NULL
);
91 /* Allocate a new hfsnode. */
93 * Must do malloc() before getnewvnode(), since malloc() can block
94 * and could cause other part of the system to access v_data
95 * which has not been initialized yet
97 MALLOC_ZONE(hp
, struct hfsnode
*, sizeof(struct hfsnode
), M_HFSNODE
, M_WAITOK
);
102 bzero((caddr_t
)hp
, sizeof(struct hfsnode
));
103 lockinit(&hp
->h_lock
, PINOD
, "hfsnode", 0, 0);
105 MALLOC_ZONE(hp
->h_meta
, struct hfsfilemeta
*,
106 sizeof(struct hfsfilemeta
), M_HFSFMETA
, M_WAITOK
);
107 /* Allocate a new vnode. */
108 if ((rtn
= getnewvnode(VT_HFS
, HFSTOVFS(hfsmp
), hfs_vnodeop_p
, &vp
))) {
109 FREE_ZONE(hp
->h_meta
, sizeof(struct hfsfilemeta
), M_HFSFMETA
);
110 FREE_ZONE(hp
, sizeof(struct hfsnode
), M_HFSNODE
);
114 /* Init the structure */
115 bzero(hp
->h_meta
, sizeof(struct hfsfilemeta
));
117 hp
->h_vp
= vp
; /* Make HFSTOV work */
118 hp
->h_meta
->h_devvp
= hfsmp
->hfs_devvp
;
119 hp
->h_meta
->h_dev
= hfsmp
->hfs_raw_dev
;
120 hp
->h_meta
->h_usecount
++;
121 hp
->h_nodeflags
|= IN_ACCESS
| IN_CHANGE
| IN_UPDATE
;
122 rl_init(&hp
->h_invalidranges
);
124 hp
->h_valid
= HFS_VNODE_MAGIC
;
126 vp
->v_data
= hp
; /* Make VTOH work */
129 * Metadata files are VREG but not available for IO
130 * through mapped IO as will as POSIX IO APIs.
131 * Hence we do not initialize UBC for those files
133 vp
->v_ubcinfo
= UBC_NOINFO
;
137 VREF(hp
->h_meta
->h_devvp
);
148 OSErr
C_FlushMDB( ExtendedVCB
*volume
)
152 if (volume
->vcbSigWord
== kHFSPlusSigWord
)
153 err
= hfs_flushvolumeheader(VCBTOHFS(volume
), 0);
155 err
= hfs_flushMDB(VCBTOHFS(volume
), 0);
162 * GetTimeUTC - get the GMT Mac OS time (in seconds since 1/1/1904)
164 * called by the Catalog Manager when creating/updating HFS Plus records
166 UInt32
GetTimeUTC(void)
168 return (time
.tv_sec
+ MAC_GMT_FACTOR
);
172 * GetTimeLocal - get the local Mac OS time (in seconds since 1/1/1904)
174 * called by the Catalog Manager when creating/updating HFS records
176 UInt32
GetTimeLocal(Boolean forHFS
)
180 localTime
= UTCToLocal(GetTimeUTC());
182 if (forHFS
&& gTimeZone
.tz_dsttime
)
189 * LocalToUTC - convert from Mac OS local time to Mac OS GMT time.
190 * This should only be called for HFS volumes (not for HFS Plus).
192 UInt32
LocalToUTC(UInt32 localTime
)
194 UInt32 gtime
= localTime
;
197 gtime
+= (gTimeZone
.tz_minuteswest
* 60);
199 * We no longer do DST adjustments here since we don't
200 * know if time supplied needs adjustment!
202 * if (gTimeZone.tz_dsttime)
210 * UTCToLocal - convert from Mac OS GMT time to Mac OS local time.
211 * This should only be called for HFS volumes (not for HFS Plus).
213 UInt32
UTCToLocal(UInt32 utcTime
)
215 UInt32 ltime
= utcTime
;
218 ltime
-= (gTimeZone
.tz_minuteswest
* 60);
220 * We no longer do DST adjustments here since we don't
221 * know if time supplied needs adjustment!
223 * if (gTimeZone.tz_dsttime)
231 * to_bsd_time - convert from Mac OS time (seconds since 1/1/1904)
232 * to BSD time (seconds since 1/1/1970)
234 u_int32_t
to_bsd_time(u_int32_t hfs_time
)
236 u_int32_t gmt
= hfs_time
;
238 if (gmt
> MAC_GMT_FACTOR
)
239 gmt
-= MAC_GMT_FACTOR
;
241 gmt
= 0; /* don't let date go negative! */
247 * to_hfs_time - convert from BSD time (seconds since 1/1/1970)
248 * to Mac OS time (seconds since 1/1/1904)
250 u_int32_t
to_hfs_time(u_int32_t bsd_time
)
252 u_int32_t hfs_time
= bsd_time
;
254 /* don't adjust zero - treat as uninitialzed */
256 hfs_time
+= MAC_GMT_FACTOR
;
262 void BlockMoveData (const void *srcPtr
, void *destPtr
, Size byteCount
)
264 bcopy(srcPtr
, destPtr
, byteCount
);
268 Ptr
NewPtrSysClear (Size byteCount
)
271 MALLOC (tmptr
, Ptr
, byteCount
, M_TEMP
, M_WAITOK
);
273 bzero(tmptr
, byteCount
);
279 Ptr
NewPtr (Size byteCount
)
282 MALLOC (tmptr
, Ptr
, byteCount
, M_TEMP
, M_WAITOK
);
287 void DisposePtr (Ptr p
)
293 void DebugStr (ConstStr255Param debuggerMsg
)
295 kprintf ("*** Mac OS Debugging Message: %s\n", &debuggerMsg
[1]);
299 OSErr
MemError (void)
305 void ClearMemory( void* start
, UInt32 length
)
307 bzero(start
, (size_t)length
);
314 * Check to see if a vnode is locked in the current context
315 * This is to be used for debugging purposes only!!
318 void RequireFileLock(FileReference vp
, int shareable
)
320 struct lock__bsd__
*lkp
;
325 pid
= current_proc()->p_pid
;
326 self
= (void *) current_thread();
327 lkp
= &VTOH(vp
)->h_lock
;
331 simple_lock(&lkp
->lk_interlock
);
333 if (shareable
&& (lkp
->lk_sharecount
> 0) && (lkp
->lk_lockholder
== LK_NOPROC
))
335 else if ((lkp
->lk_exclusivecount
> 0) && (lkp
->lk_lockholder
== pid
) && (lkp
->lk_lockthread
== self
))
338 simple_unlock(&lkp
->lk_interlock
);
341 DBG_VFS((" # context... self=0x%0X, pid=0x%0X, proc=0x%0X\n", (int)self
, pid
, (int)current_proc()));
342 DBG_VFS((" # lock state... thread=0x%0X, holder=0x%0X, ex=%d, sh=%d\n", (int)lkp
->lk_lockthread
, lkp
->lk_lockholder
, lkp
->lk_exclusivecount
, lkp
->lk_sharecount
));
344 switch (H_FILEID(VTOH(vp
))) {
346 DEBUG_BREAK_MSG((" #\n # RequireFileLock: extent btree vnode not locked! v: 0x%08X\n #\n", (u_int
)vp
));
350 DEBUG_BREAK_MSG((" #\n # RequireFileLock: catalog btree vnode not locked! v: 0x%08X\n #\n", (u_int
)vp
));
354 DEBUG_BREAK_MSG((" #\n # RequireFileLock: file (%d) not locked! v: 0x%08X\n #\n", H_FILEID(VTOH(vp
)), (u_int
)vp
));