Linux kernel & device driver programming

Cross-Referenced Linux and Device Driver Code

[ source navigation ] [ diff markup ] [ identifier search ] [ freetext search ] [ file search ]
Version: [ 2.6.11.8 ] [ 2.6.25 ] [ 2.6.25.8 ] [ 2.6.31.13 ] Architecture: [ i386 ]
  1 /*
  2  *  linux/fs/hfs/mdb.c
  3  *
  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.
  7  *
  8  * This file contains functions for reading/writing the MDB.
  9  */
 10 
 11 #include <linux/cdrom.h>
 12 #include <linux/genhd.h>
 13 
 14 #include "hfs_fs.h"
 15 #include "btree.h"
 16 
 17 /*================ File-local data types ================*/
 18 
 19 /*
 20  * The HFS Master Directory Block (MDB).
 21  *
 22  * Also known as the Volume Information Block (VIB), this structure is
 23  * the HFS equivalent of a superblock.
 24  *
 25  * Reference: _Inside Macintosh: Files_ pages 2-59 through 2-62
 26  *
 27  * modified for HFS Extended
 28  */
 29 
 30 static int hfs_get_last_session(struct super_block *sb,
 31                                 sector_t *start, sector_t *size)
 32 {
 33         struct cdrom_multisession ms_info;
 34         struct cdrom_tocentry te;
 35         int res;
 36 
 37         /* default values */
 38         *start = 0;
 39         *size = sb->s_bdev->bd_inode->i_size >> 9;
 40 
 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;
 47                         return 0;
 48                 }
 49                 printk(KERN_ERR "HFS: Invalid session number or type of track\n");
 50                 return -EINVAL;
 51         }
 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;
 56         return 0;
 57 }
 58 
 59 /*
 60  * hfs_mdb_get()
 61  *
 62  * Build the in-core MDB for a filesystem, including
 63  * the B-trees and the volume bitmap.
 64  */
 65 int hfs_mdb_get(struct super_block *sb)
 66 {
 67         struct buffer_head *bh;
 68         struct hfs_mdb *mdb, *mdb2;
 69         unsigned int block;
 70         char *ptr;
 71         int off2, len, size, sect;
 72         sector_t part_start, part_size;
 73         loff_t off;
 74         __be16 attrib;
 75 
 76         /* set the device driver to 512-byte blocks */
 77         size = sb_min_blocksize(sb, HFS_SECTOR_SIZE);
 78         if (!size)
 79                 return -EINVAL;
 80 
 81         if (hfs_get_last_session(sb, &part_start, &part_size))
 82                 return -EINVAL;
 83         while (1) {
 84                 /* See if this is an HFS filesystem */
 85                 bh = sb_bread512(sb, part_start + HFS_MDB_BLK, mdb);
 86                 if (!bh)
 87                         goto out;
 88 
 89                 if (mdb->drSigWord == cpu_to_be16(HFS_SUPER_MAGIC))
 90                         break;
 91                 brelse(bh);
 92 
 93                 /* check for a partition block
 94                  * (should do this only for cdrom/loop though)
 95                  */
 96                 if (hfs_part_find(sb, &part_start, &part_size))
 97                         goto out;
 98         }
 99 
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);
103                 goto out_bh;
104         }
105 
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))
113                 size >>= 1;
114         /* align block size to weird alloc size */
115         while (HFS_SB(sb)->alloc_blksz & (size - 1))
116                 size >>= 1;
117         brelse(bh);
118         if (!sb_set_blocksize(sb, size)) {
119                 printk("hfs_fs: unable to set blocksize to %u\n", size);
120                 goto out;
121         }
122 
123         bh = sb_bread512(sb, part_start + HFS_MDB_BLK, mdb);
124         if (!bh)
125                 goto out;
126         if (mdb->drSigWord != cpu_to_be16(HFS_SUPER_MAGIC))
127                 goto out_bh;
128 
129         HFS_SB(sb)->mdb_bh = bh;
130         HFS_SB(sb)->mdb = mdb;
131 
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);
142 
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);
150 
151         /* TRY to get the alternate (backup) MDB. */
152         sect = part_start + part_size - 2;
153         bh = sb_bread512(sb, sect, mdb2);
154         if (bh) {
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;
158                 } else
159                         brelse(bh);
160         }
161 
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");
165         }
166 
167         HFS_SB(sb)->bitmap = (__be32 *)__get_free_pages(GFP_KERNEL, PAGE_SIZE < 8192 ? 1 : 0);
168         if (!HFS_SB(sb)->bitmap)
169                 goto out;
170 
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;
176         while (size) {
177                 bh = sb_bread(sb, off >> sb->s_blocksize_bits);
178                 if (!bh) {
179                         hfs_warn("hfs_fs: unable to read volume bitmap\n");
180                         goto out;
181                 }
182                 off2 = off & (sb->s_blocksize - 1);
183                 len = min((int)sb->s_blocksize - off2, size);
184                 memcpy(ptr, bh->b_data + off2, len);
185                 brelse(bh);
186                 ptr += len;
187                 off += len;
188                 size -= len;
189         }
190 
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");
194                 goto out;
195         }
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");
199                 goto out;
200         }
201 
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;
207         }
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;
211         }
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();
219 
220                 mark_buffer_dirty(HFS_SB(sb)->mdb_bh);
221                 hfs_buffer_sync(HFS_SB(sb)->mdb_bh);
222         }
223 
224         return 0;
225 
226 out_bh:
227         brelse(bh);
228 out:
229         hfs_mdb_put(sb);
230         return -EIO;
231 }
232 
233 /*
234  * hfs_mdb_commit()
235  *
236  * Description:
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().
241  * Input Variable(s):
242  *   struct hfs_mdb *mdb: Pointer to the hfs MDB
243  *   int backup;
244  * Output Variable(s):
245  *   NONE
246  * Returns:
247  *   void
248  * Preconditions:
249  *   'mdb' points to a "valid" (struct hfs_mdb).
250  * Postconditions:
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.
256  */
257 void hfs_mdb_commit(struct super_block *sb)
258 {
259         struct hfs_mdb *mdb = HFS_SB(sb)->mdb;
260 
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);
270 
271                 /* write MDB to disk */
272                 mark_buffer_dirty(HFS_SB(sb)->mdb_bh);
273         }
274 
275         /* write the backup MDB, not returning until it is written.
276          * we only do this when either the catalog or extents overflow
277          * files grow. */
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);
289         }
290 
291         if (test_and_clear_bit(HFS_FLG_BITMAP_DIRTY, &HFS_SB(sb)->flags)) {
292                 struct buffer_head *bh;
293                 sector_t block;
294                 char *ptr;
295                 int off, size, len;
296 
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;
302                 while (size) {
303                         bh = sb_bread(sb, block);
304                         if (!bh) {
305                                 hfs_warn("hfs_fs: unable to read volume bitmap\n");
306                                 break;
307                         }
308                         len = min((int)sb->s_blocksize - off, size);
309                         memcpy(bh->b_data + off, ptr, len);
310                         mark_buffer_dirty(bh);
311                         brelse(bh);
312                         block++;
313                         off = 0;
314                         ptr += len;
315                         size -= len;
316                 }
317         }
318 }
319 
320 void hfs_mdb_close(struct super_block *sb)
321 {
322         /* update volume attributes */
323         if (sb->s_flags & MS_RDONLY)
324                 return;
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);
328 }
329 
330 /*
331  * hfs_mdb_put()
332  *
333  * Release the resources associated with the in-core MDB.  */
334 void hfs_mdb_put(struct super_block *sb)
335 {
336         /* free the B-trees */
337         hfs_btree_close(HFS_SB(sb)->ext_tree);
338         hfs_btree_close(HFS_SB(sb)->cat_tree);
339 
340         /* free the buffers holding the primary and alternate MDBs */
341         brelse(HFS_SB(sb)->mdb_bh);
342         brelse(HFS_SB(sb)->alt_mdb_bh);
343 }
344 
  This page was automatically generated by the LXR engine.