4 * Copyright (C) 1995-1997 Paul H. Hargrove
5 * (C) 2003 Ardis Technologies <roman@ardistech.com>
6 * This file may be distributed under the terms of the GNU General Public License.
8 * This file contains functions for reading/writing the MDB.
11 #include <linux/cdrom.h>
12 #include <linux/genhd.h>
13 #include <linux/nls.h>
14 #include <linux/slab.h>
19 /*================ File-local data types ================*/
22 * The HFS Master Directory Block (MDB).
24 * Also known as the Volume Information Block (VIB), this structure is
25 * the HFS equivalent of a superblock.
27 * Reference: _Inside Macintosh: Files_ pages 2-59 through 2-62
29 * modified for HFS Extended
32 static int hfs_get_last_session(struct super_block
*sb
,
33 sector_t
*start
, sector_t
*size
)
35 struct cdrom_multisession ms_info
;
36 struct cdrom_tocentry te
;
41 *size
= sb
->s_bdev
->bd_inode
->i_size
>> 9;
43 if (HFS_SB(sb
)->session
>= 0) {
44 te
.cdte_track
= HFS_SB(sb
)->session
;
45 te
.cdte_format
= CDROM_LBA
;
46 res
= ioctl_by_bdev(sb
->s_bdev
, CDROMREADTOCENTRY
, (unsigned long)&te
);
47 if (!res
&& (te
.cdte_ctrl
& CDROM_DATA_TRACK
) == 4) {
48 *start
= (sector_t
)te
.cdte_addr
.lba
<< 2;
51 printk(KERN_ERR
"hfs: invalid session number or type of track\n");
54 ms_info
.addr_format
= CDROM_LBA
;
55 res
= ioctl_by_bdev(sb
->s_bdev
, CDROMMULTISESSION
, (unsigned long)&ms_info
);
56 if (!res
&& ms_info
.xa_flag
)
57 *start
= (sector_t
)ms_info
.addr
.lba
<< 2;
64 * Build the in-core MDB for a filesystem, including
65 * the B-trees and the volume bitmap.
67 int hfs_mdb_get(struct super_block
*sb
)
69 struct buffer_head
*bh
;
70 struct hfs_mdb
*mdb
, *mdb2
;
73 int off2
, len
, size
, sect
;
74 sector_t part_start
, part_size
;
78 /* set the device driver to 512-byte blocks */
79 size
= sb_min_blocksize(sb
, HFS_SECTOR_SIZE
);
83 if (hfs_get_last_session(sb
, &part_start
, &part_size
))
86 /* See if this is an HFS filesystem */
87 bh
= sb_bread512(sb
, part_start
+ HFS_MDB_BLK
, mdb
);
91 if (mdb
->drSigWord
== cpu_to_be16(HFS_SUPER_MAGIC
))
95 /* check for a partition block
96 * (should do this only for cdrom/loop though)
98 if (hfs_part_find(sb
, &part_start
, &part_size
))
102 HFS_SB(sb
)->alloc_blksz
= size
= be32_to_cpu(mdb
->drAlBlkSiz
);
103 if (!size
|| (size
& (HFS_SECTOR_SIZE
- 1))) {
104 printk(KERN_ERR
"hfs: bad allocation block size %d\n", size
);
108 size
= min(HFS_SB(sb
)->alloc_blksz
, (u32
)PAGE_SIZE
);
109 /* size must be a multiple of 512 */
110 while (size
& (size
- 1))
111 size
-= HFS_SECTOR_SIZE
;
112 sect
= be16_to_cpu(mdb
->drAlBlSt
) + part_start
;
113 /* align block size to first sector */
114 while (sect
& ((size
- 1) >> HFS_SECTOR_SIZE_BITS
))
116 /* align block size to weird alloc size */
117 while (HFS_SB(sb
)->alloc_blksz
& (size
- 1))
120 if (!sb_set_blocksize(sb
, size
)) {
121 printk(KERN_ERR
"hfs: unable to set blocksize to %u\n", size
);
125 bh
= sb_bread512(sb
, part_start
+ HFS_MDB_BLK
, mdb
);
128 if (mdb
->drSigWord
!= cpu_to_be16(HFS_SUPER_MAGIC
))
131 HFS_SB(sb
)->mdb_bh
= bh
;
132 HFS_SB(sb
)->mdb
= mdb
;
134 /* These parameters are read from the MDB, and never written */
135 HFS_SB(sb
)->part_start
= part_start
;
136 HFS_SB(sb
)->fs_ablocks
= be16_to_cpu(mdb
->drNmAlBlks
);
137 HFS_SB(sb
)->fs_div
= HFS_SB(sb
)->alloc_blksz
>> sb
->s_blocksize_bits
;
138 HFS_SB(sb
)->clumpablks
= be32_to_cpu(mdb
->drClpSiz
) /
139 HFS_SB(sb
)->alloc_blksz
;
140 if (!HFS_SB(sb
)->clumpablks
)
141 HFS_SB(sb
)->clumpablks
= 1;
142 HFS_SB(sb
)->fs_start
= (be16_to_cpu(mdb
->drAlBlSt
) + part_start
) >>
143 (sb
->s_blocksize_bits
- HFS_SECTOR_SIZE_BITS
);
145 /* These parameters are read from and written to the MDB */
146 HFS_SB(sb
)->free_ablocks
= be16_to_cpu(mdb
->drFreeBks
);
147 HFS_SB(sb
)->next_id
= be32_to_cpu(mdb
->drNxtCNID
);
148 HFS_SB(sb
)->root_files
= be16_to_cpu(mdb
->drNmFls
);
149 HFS_SB(sb
)->root_dirs
= be16_to_cpu(mdb
->drNmRtDirs
);
150 HFS_SB(sb
)->file_count
= be32_to_cpu(mdb
->drFilCnt
);
151 HFS_SB(sb
)->folder_count
= be32_to_cpu(mdb
->drDirCnt
);
153 /* TRY to get the alternate (backup) MDB. */
154 sect
= part_start
+ part_size
- 2;
155 bh
= sb_bread512(sb
, sect
, mdb2
);
157 if (mdb2
->drSigWord
== cpu_to_be16(HFS_SUPER_MAGIC
)) {
158 HFS_SB(sb
)->alt_mdb_bh
= bh
;
159 HFS_SB(sb
)->alt_mdb
= mdb2
;
164 if (!HFS_SB(sb
)->alt_mdb
) {
165 printk(KERN_WARNING
"hfs: unable to locate alternate MDB\n");
166 printk(KERN_WARNING
"hfs: continuing without an alternate MDB\n");
169 HFS_SB(sb
)->bitmap
= (__be32
*)__get_free_pages(GFP_KERNEL
, PAGE_SIZE
< 8192 ? 1 : 0);
170 if (!HFS_SB(sb
)->bitmap
)
173 /* read in the bitmap */
174 block
= be16_to_cpu(mdb
->drVBMSt
) + part_start
;
175 off
= (loff_t
)block
<< HFS_SECTOR_SIZE_BITS
;
176 size
= (HFS_SB(sb
)->fs_ablocks
+ 8) / 8;
177 ptr
= (u8
*)HFS_SB(sb
)->bitmap
;
179 bh
= sb_bread(sb
, off
>> sb
->s_blocksize_bits
);
181 printk(KERN_ERR
"hfs: unable to read volume bitmap\n");
184 off2
= off
& (sb
->s_blocksize
- 1);
185 len
= min((int)sb
->s_blocksize
- off2
, size
);
186 memcpy(ptr
, bh
->b_data
+ off2
, len
);
193 HFS_SB(sb
)->ext_tree
= hfs_btree_open(sb
, HFS_EXT_CNID
, hfs_ext_keycmp
);
194 if (!HFS_SB(sb
)->ext_tree
) {
195 printk(KERN_ERR
"hfs: unable to open extent tree\n");
198 HFS_SB(sb
)->cat_tree
= hfs_btree_open(sb
, HFS_CAT_CNID
, hfs_cat_keycmp
);
199 if (!HFS_SB(sb
)->cat_tree
) {
200 printk(KERN_ERR
"hfs: unable to open catalog tree\n");
204 attrib
= mdb
->drAtrb
;
205 if (!(attrib
& cpu_to_be16(HFS_SB_ATTRIB_UNMNT
))) {
206 printk(KERN_WARNING
"hfs: filesystem was not cleanly unmounted, "
207 "running fsck.hfs is recommended. mounting read-only.\n");
208 sb
->s_flags
|= MS_RDONLY
;
210 if ((attrib
& cpu_to_be16(HFS_SB_ATTRIB_SLOCK
))) {
211 printk(KERN_WARNING
"hfs: filesystem is marked locked, mounting read-only.\n");
212 sb
->s_flags
|= MS_RDONLY
;
214 if (!(sb
->s_flags
& MS_RDONLY
)) {
215 /* Mark the volume uncleanly unmounted in case we crash */
216 attrib
&= cpu_to_be16(~HFS_SB_ATTRIB_UNMNT
);
217 attrib
|= cpu_to_be16(HFS_SB_ATTRIB_INCNSTNT
);
218 mdb
->drAtrb
= attrib
;
219 be32_add_cpu(&mdb
->drWrCnt
, 1);
220 mdb
->drLsMod
= hfs_mtime();
222 mark_buffer_dirty(HFS_SB(sb
)->mdb_bh
);
223 sync_dirty_buffer(HFS_SB(sb
)->mdb_bh
);
239 * This updates the MDB on disk (look also at hfs_write_super()).
240 * It does not check, if the superblock has been modified, or
241 * if the filesystem has been mounted read-only. It is mainly
242 * called by hfs_write_super() and hfs_btree_extend().
244 * struct hfs_mdb *mdb: Pointer to the hfs MDB
246 * Output Variable(s):
251 * 'mdb' points to a "valid" (struct hfs_mdb).
253 * The HFS MDB and on disk will be updated, by copying the possibly
254 * modified fields from the in memory MDB (in native byte order) to
255 * the disk block buffer.
256 * If 'backup' is non-zero then the alternate MDB is also written
257 * and the function doesn't return until it is actually on disk.
259 void hfs_mdb_commit(struct super_block
*sb
)
261 struct hfs_mdb
*mdb
= HFS_SB(sb
)->mdb
;
263 if (test_and_clear_bit(HFS_FLG_MDB_DIRTY
, &HFS_SB(sb
)->flags
)) {
264 /* These parameters may have been modified, so write them back */
265 mdb
->drLsMod
= hfs_mtime();
266 mdb
->drFreeBks
= cpu_to_be16(HFS_SB(sb
)->free_ablocks
);
267 mdb
->drNxtCNID
= cpu_to_be32(HFS_SB(sb
)->next_id
);
268 mdb
->drNmFls
= cpu_to_be16(HFS_SB(sb
)->root_files
);
269 mdb
->drNmRtDirs
= cpu_to_be16(HFS_SB(sb
)->root_dirs
);
270 mdb
->drFilCnt
= cpu_to_be32(HFS_SB(sb
)->file_count
);
271 mdb
->drDirCnt
= cpu_to_be32(HFS_SB(sb
)->folder_count
);
273 /* write MDB to disk */
274 mark_buffer_dirty(HFS_SB(sb
)->mdb_bh
);
277 /* write the backup MDB, not returning until it is written.
278 * we only do this when either the catalog or extents overflow
280 if (test_and_clear_bit(HFS_FLG_ALT_MDB_DIRTY
, &HFS_SB(sb
)->flags
) &&
281 HFS_SB(sb
)->alt_mdb
) {
282 hfs_inode_write_fork(HFS_SB(sb
)->ext_tree
->inode
, mdb
->drXTExtRec
,
283 &mdb
->drXTFlSize
, NULL
);
284 hfs_inode_write_fork(HFS_SB(sb
)->cat_tree
->inode
, mdb
->drCTExtRec
,
285 &mdb
->drCTFlSize
, NULL
);
286 memcpy(HFS_SB(sb
)->alt_mdb
, HFS_SB(sb
)->mdb
, HFS_SECTOR_SIZE
);
287 HFS_SB(sb
)->alt_mdb
->drAtrb
|= cpu_to_be16(HFS_SB_ATTRIB_UNMNT
);
288 HFS_SB(sb
)->alt_mdb
->drAtrb
&= cpu_to_be16(~HFS_SB_ATTRIB_INCNSTNT
);
289 mark_buffer_dirty(HFS_SB(sb
)->alt_mdb_bh
);
290 sync_dirty_buffer(HFS_SB(sb
)->alt_mdb_bh
);
293 if (test_and_clear_bit(HFS_FLG_BITMAP_DIRTY
, &HFS_SB(sb
)->flags
)) {
294 struct buffer_head
*bh
;
299 block
= be16_to_cpu(HFS_SB(sb
)->mdb
->drVBMSt
) + HFS_SB(sb
)->part_start
;
300 off
= (block
<< HFS_SECTOR_SIZE_BITS
) & (sb
->s_blocksize
- 1);
301 block
>>= sb
->s_blocksize_bits
- HFS_SECTOR_SIZE_BITS
;
302 size
= (HFS_SB(sb
)->fs_ablocks
+ 7) / 8;
303 ptr
= (u8
*)HFS_SB(sb
)->bitmap
;
305 bh
= sb_bread(sb
, block
);
307 printk(KERN_ERR
"hfs: unable to read volume bitmap\n");
310 len
= min((int)sb
->s_blocksize
- off
, size
);
311 memcpy(bh
->b_data
+ off
, ptr
, len
);
312 mark_buffer_dirty(bh
);
322 void hfs_mdb_close(struct super_block
*sb
)
324 /* update volume attributes */
325 if (sb
->s_flags
& MS_RDONLY
)
327 HFS_SB(sb
)->mdb
->drAtrb
|= cpu_to_be16(HFS_SB_ATTRIB_UNMNT
);
328 HFS_SB(sb
)->mdb
->drAtrb
&= cpu_to_be16(~HFS_SB_ATTRIB_INCNSTNT
);
329 mark_buffer_dirty(HFS_SB(sb
)->mdb_bh
);
335 * Release the resources associated with the in-core MDB. */
336 void hfs_mdb_put(struct super_block
*sb
)
340 /* free the B-trees */
341 hfs_btree_close(HFS_SB(sb
)->ext_tree
);
342 hfs_btree_close(HFS_SB(sb
)->cat_tree
);
344 /* free the buffers holding the primary and alternate MDBs */
345 brelse(HFS_SB(sb
)->mdb_bh
);
346 brelse(HFS_SB(sb
)->alt_mdb_bh
);
348 unload_nls(HFS_SB(sb
)->nls_io
);
349 unload_nls(HFS_SB(sb
)->nls_disk
);
351 free_pages((unsigned long)HFS_SB(sb
)->bitmap
, PAGE_SIZE
< 8192 ? 1 : 0);
353 sb
->s_fs_info
= NULL
;