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>
17 /*================ File-local data types ================*/
20 * The HFS Master Directory Block (MDB).
22 * Also known as the Volume Information Block (VIB), this structure is
23 * the HFS equivalent of a superblock.
25 * Reference: _Inside Macintosh: Files_ pages 2-59 through 2-62
27 * modified for HFS Extended
30 static int hfs_get_last_session(struct super_block
*sb
,
31 sector_t
*start
, sector_t
*size
)
33 struct cdrom_multisession ms_info
;
34 struct cdrom_tocentry te
;
39 *size
= sb
->s_bdev
->bd_inode
->i_size
>> 9;
41 if (HFS_SB(sb
)->session
>= 0) {
42 te
.cdte_track
= HFS_SB(sb
)->session
;
43 te
.cdte_format
= CDROM_LBA
;
44 res
= ioctl_by_bdev(sb
->s_bdev
, CDROMREADTOCENTRY
, (unsigned long)&te
);
45 if (!res
&& (te
.cdte_ctrl
& CDROM_DATA_TRACK
) == 4) {
46 *start
= (sector_t
)te
.cdte_addr
.lba
<< 2;
49 printk(KERN_ERR
"HFS: Invalid session number or type of track\n");
52 ms_info
.addr_format
= CDROM_LBA
;
53 res
= ioctl_by_bdev(sb
->s_bdev
, CDROMMULTISESSION
, (unsigned long)&ms_info
);
54 if (!res
&& ms_info
.xa_flag
)
55 *start
= (sector_t
)ms_info
.addr
.lba
<< 2;
62 * Build the in-core MDB for a filesystem, including
63 * the B-trees and the volume bitmap.
65 int hfs_mdb_get(struct super_block
*sb
)
67 struct buffer_head
*bh
;
68 struct hfs_mdb
*mdb
, *mdb2
;
71 int off2
, len
, size
, sect
;
72 sector_t part_start
, part_size
;
76 /* set the device driver to 512-byte blocks */
77 size
= sb_min_blocksize(sb
, HFS_SECTOR_SIZE
);
81 if (hfs_get_last_session(sb
, &part_start
, &part_size
))
84 /* See if this is an HFS filesystem */
85 bh
= sb_bread512(sb
, part_start
+ HFS_MDB_BLK
, mdb
);
89 if (mdb
->drSigWord
== cpu_to_be16(HFS_SUPER_MAGIC
))
93 /* check for a partition block
94 * (should do this only for cdrom/loop though)
96 if (hfs_part_find(sb
, &part_start
, &part_size
))
100 HFS_SB(sb
)->alloc_blksz
= size
= be32_to_cpu(mdb
->drAlBlkSiz
);
101 if (!size
|| (size
& (HFS_SECTOR_SIZE
- 1))) {
102 hfs_warn("hfs_fs: bad allocation block size %d\n", size
);
106 size
= min(HFS_SB(sb
)->alloc_blksz
, (u32
)PAGE_SIZE
);
107 /* size must be a multiple of 512 */
108 while (size
& (size
- 1))
109 size
-= HFS_SECTOR_SIZE
;
110 sect
= be16_to_cpu(mdb
->drAlBlSt
) + part_start
;
111 /* align block size to first sector */
112 while (sect
& ((size
- 1) >> HFS_SECTOR_SIZE_BITS
))
114 /* align block size to weird alloc size */
115 while (HFS_SB(sb
)->alloc_blksz
& (size
- 1))
118 if (!sb_set_blocksize(sb
, size
)) {
119 printk("hfs_fs: unable to set blocksize to %u\n", size
);
123 bh
= sb_bread512(sb
, part_start
+ HFS_MDB_BLK
, mdb
);
126 if (mdb
->drSigWord
!= cpu_to_be16(HFS_SUPER_MAGIC
))
129 HFS_SB(sb
)->mdb_bh
= bh
;
130 HFS_SB(sb
)->mdb
= mdb
;
132 /* These parameters are read from the MDB, and never written */
133 HFS_SB(sb
)->part_start
= part_start
;
134 HFS_SB(sb
)->fs_ablocks
= be16_to_cpu(mdb
->drNmAlBlks
);
135 HFS_SB(sb
)->fs_div
= HFS_SB(sb
)->alloc_blksz
>> sb
->s_blocksize_bits
;
136 HFS_SB(sb
)->clumpablks
= be32_to_cpu(mdb
->drClpSiz
) /
137 HFS_SB(sb
)->alloc_blksz
;
138 if (!HFS_SB(sb
)->clumpablks
)
139 HFS_SB(sb
)->clumpablks
= 1;
140 HFS_SB(sb
)->fs_start
= (be16_to_cpu(mdb
->drAlBlSt
) + part_start
) >>
141 (sb
->s_blocksize_bits
- HFS_SECTOR_SIZE_BITS
);
143 /* These parameters are read from and written to the MDB */
144 HFS_SB(sb
)->free_ablocks
= be16_to_cpu(mdb
->drFreeBks
);
145 HFS_SB(sb
)->next_id
= be32_to_cpu(mdb
->drNxtCNID
);
146 HFS_SB(sb
)->root_files
= be16_to_cpu(mdb
->drNmFls
);
147 HFS_SB(sb
)->root_dirs
= be16_to_cpu(mdb
->drNmRtDirs
);
148 HFS_SB(sb
)->file_count
= be32_to_cpu(mdb
->drFilCnt
);
149 HFS_SB(sb
)->folder_count
= be32_to_cpu(mdb
->drDirCnt
);
151 /* TRY to get the alternate (backup) MDB. */
152 sect
= part_start
+ part_size
- 2;
153 bh
= sb_bread512(sb
, sect
, mdb2
);
155 if (mdb2
->drSigWord
== cpu_to_be16(HFS_SUPER_MAGIC
)) {
156 HFS_SB(sb
)->alt_mdb_bh
= bh
;
157 HFS_SB(sb
)->alt_mdb
= mdb2
;
162 if (!HFS_SB(sb
)->alt_mdb
) {
163 hfs_warn("hfs_fs: unable to locate alternate MDB\n");
164 hfs_warn("hfs_fs: continuing without an alternate MDB\n");
167 HFS_SB(sb
)->bitmap
= (__be32
*)__get_free_pages(GFP_KERNEL
, PAGE_SIZE
< 8192 ? 1 : 0);
168 if (!HFS_SB(sb
)->bitmap
)
171 /* read in the bitmap */
172 block
= be16_to_cpu(mdb
->drVBMSt
) + part_start
;
173 off
= (loff_t
)block
<< HFS_SECTOR_SIZE_BITS
;
174 size
= (HFS_SB(sb
)->fs_ablocks
+ 8) / 8;
175 ptr
= (u8
*)HFS_SB(sb
)->bitmap
;
177 bh
= sb_bread(sb
, off
>> sb
->s_blocksize_bits
);
179 hfs_warn("hfs_fs: unable to read volume bitmap\n");
182 off2
= off
& (sb
->s_blocksize
- 1);
183 len
= min((int)sb
->s_blocksize
- off2
, size
);
184 memcpy(ptr
, bh
->b_data
+ off2
, len
);
191 HFS_SB(sb
)->ext_tree
= hfs_btree_open(sb
, HFS_EXT_CNID
, hfs_ext_keycmp
);
192 if (!HFS_SB(sb
)->ext_tree
) {
193 hfs_warn("hfs_fs: unable to open extent tree\n");
196 HFS_SB(sb
)->cat_tree
= hfs_btree_open(sb
, HFS_CAT_CNID
, hfs_cat_keycmp
);
197 if (!HFS_SB(sb
)->cat_tree
) {
198 hfs_warn("hfs_fs: unable to open catalog tree\n");
202 attrib
= mdb
->drAtrb
;
203 if (!(attrib
& cpu_to_be16(HFS_SB_ATTRIB_UNMNT
))) {
204 hfs_warn("HFS-fs warning: Filesystem was not cleanly unmounted, "
205 "running fsck.hfs is recommended. mounting read-only.\n");
206 sb
->s_flags
|= MS_RDONLY
;
208 if ((attrib
& cpu_to_be16(HFS_SB_ATTRIB_SLOCK
))) {
209 hfs_warn("HFS-fs: Filesystem is marked locked, mounting read-only.\n");
210 sb
->s_flags
|= MS_RDONLY
;
212 if (!(sb
->s_flags
& MS_RDONLY
)) {
213 /* Mark the volume uncleanly unmounted in case we crash */
214 attrib
&= cpu_to_be16(~HFS_SB_ATTRIB_UNMNT
);
215 attrib
|= cpu_to_be16(HFS_SB_ATTRIB_INCNSTNT
);
216 mdb
->drAtrb
= attrib
;
217 mdb
->drWrCnt
= cpu_to_be32(be32_to_cpu(mdb
->drWrCnt
) + 1);
218 mdb
->drLsMod
= hfs_mtime();
220 mark_buffer_dirty(HFS_SB(sb
)->mdb_bh
);
221 hfs_buffer_sync(HFS_SB(sb
)->mdb_bh
);
237 * This updates the MDB on disk (look also at hfs_write_super()).
238 * It does not check, if the superblock has been modified, or
239 * if the filesystem has been mounted read-only. It is mainly
240 * called by hfs_write_super() and hfs_btree_extend().
242 * struct hfs_mdb *mdb: Pointer to the hfs MDB
244 * Output Variable(s):
249 * 'mdb' points to a "valid" (struct hfs_mdb).
251 * The HFS MDB and on disk will be updated, by copying the possibly
252 * modified fields from the in memory MDB (in native byte order) to
253 * the disk block buffer.
254 * If 'backup' is non-zero then the alternate MDB is also written
255 * and the function doesn't return until it is actually on disk.
257 void hfs_mdb_commit(struct super_block
*sb
)
259 struct hfs_mdb
*mdb
= HFS_SB(sb
)->mdb
;
261 if (test_and_clear_bit(HFS_FLG_MDB_DIRTY
, &HFS_SB(sb
)->flags
)) {
262 /* These parameters may have been modified, so write them back */
263 mdb
->drLsMod
= hfs_mtime();
264 mdb
->drFreeBks
= cpu_to_be16(HFS_SB(sb
)->free_ablocks
);
265 mdb
->drNxtCNID
= cpu_to_be32(HFS_SB(sb
)->next_id
);
266 mdb
->drNmFls
= cpu_to_be16(HFS_SB(sb
)->root_files
);
267 mdb
->drNmRtDirs
= cpu_to_be16(HFS_SB(sb
)->root_dirs
);
268 mdb
->drFilCnt
= cpu_to_be32(HFS_SB(sb
)->file_count
);
269 mdb
->drDirCnt
= cpu_to_be32(HFS_SB(sb
)->folder_count
);
271 /* write MDB to disk */
272 mark_buffer_dirty(HFS_SB(sb
)->mdb_bh
);
275 /* write the backup MDB, not returning until it is written.
276 * we only do this when either the catalog or extents overflow
278 if (test_and_clear_bit(HFS_FLG_ALT_MDB_DIRTY
, &HFS_SB(sb
)->flags
) &&
279 HFS_SB(sb
)->alt_mdb
) {
280 hfs_inode_write_fork(HFS_SB(sb
)->ext_tree
->inode
, mdb
->drXTExtRec
,
281 &mdb
->drXTFlSize
, NULL
);
282 hfs_inode_write_fork(HFS_SB(sb
)->cat_tree
->inode
, mdb
->drCTExtRec
,
283 &mdb
->drCTFlSize
, NULL
);
284 memcpy(HFS_SB(sb
)->alt_mdb
, HFS_SB(sb
)->mdb
, HFS_SECTOR_SIZE
);
285 HFS_SB(sb
)->alt_mdb
->drAtrb
|= cpu_to_be16(HFS_SB_ATTRIB_UNMNT
);
286 HFS_SB(sb
)->alt_mdb
->drAtrb
&= cpu_to_be16(~HFS_SB_ATTRIB_INCNSTNT
);
287 mark_buffer_dirty(HFS_SB(sb
)->alt_mdb_bh
);
288 hfs_buffer_sync(HFS_SB(sb
)->alt_mdb_bh
);
291 if (test_and_clear_bit(HFS_FLG_BITMAP_DIRTY
, &HFS_SB(sb
)->flags
)) {
292 struct buffer_head
*bh
;
297 block
= be16_to_cpu(HFS_SB(sb
)->mdb
->drVBMSt
) + HFS_SB(sb
)->part_start
;
298 off
= (block
<< HFS_SECTOR_SIZE_BITS
) & (sb
->s_blocksize
- 1);
299 block
>>= sb
->s_blocksize_bits
- HFS_SECTOR_SIZE_BITS
;
300 size
= (HFS_SB(sb
)->fs_ablocks
+ 7) / 8;
301 ptr
= (u8
*)HFS_SB(sb
)->bitmap
;
303 bh
= sb_bread(sb
, block
);
305 hfs_warn("hfs_fs: unable to read volume bitmap\n");
308 len
= min((int)sb
->s_blocksize
- off
, size
);
309 memcpy(bh
->b_data
+ off
, ptr
, len
);
310 mark_buffer_dirty(bh
);
320 void hfs_mdb_close(struct super_block
*sb
)
322 /* update volume attributes */
323 if (sb
->s_flags
& MS_RDONLY
)
325 HFS_SB(sb
)->mdb
->drAtrb
|= cpu_to_be16(HFS_SB_ATTRIB_UNMNT
);
326 HFS_SB(sb
)->mdb
->drAtrb
&= cpu_to_be16(~HFS_SB_ATTRIB_INCNSTNT
);
327 mark_buffer_dirty(HFS_SB(sb
)->mdb_bh
);
333 * Release the resources associated with the in-core MDB. */
334 void hfs_mdb_put(struct super_block
*sb
)
338 /* free the B-trees */
339 hfs_btree_close(HFS_SB(sb
)->ext_tree
);
340 hfs_btree_close(HFS_SB(sb
)->cat_tree
);
342 /* free the buffers holding the primary and alternate MDBs */
343 brelse(HFS_SB(sb
)->mdb_bh
);
344 brelse(HFS_SB(sb
)->alt_mdb_bh
);
347 sb
->s_fs_info
= NULL
;