1 /* 2 * linux/fs/hfsplus/super.c 3 * 4 * Copyright (C) 2001 5 * Brad Boyer (flar@allandria.com) 6 * (C) 2003 Ardis Technologies <roman@ardistech.com> 7 * 8 */ 9 10 #include <linux/config.h> 11 #include <linux/module.h> 12 #include <linux/init.h> 13 #include <linux/pagemap.h> 14 #include <linux/fs.h> 15 #include <linux/sched.h> 16 #include <linux/slab.h> 17 #include <linux/version.h> 18 #include <linux/vfs.h> 19 #include <linux/nls.h> 20 21 static struct inode *hfsplus_alloc_inode(struct super_block *sb); 22 static void hfsplus_destroy_inode(struct inode *inode); 23 24 #include "hfsplus_fs.h" 25 26 void hfsplus_inode_check(struct super_block *sb) 27 { 28 #if 0 29 u32 cnt = atomic_read(&HFSPLUS_SB(sb).inode_cnt); 30 u32 last_cnt = HFSPLUS_SB(sb).last_inode_cnt; 31 32 if (cnt <= (last_cnt / 2) || 33 cnt >= (last_cnt * 2)) { 34 HFSPLUS_SB(sb).last_inode_cnt = cnt; 35 printk("inode_check: %u,%u,%u\n", cnt, last_cnt, 36 HFSPLUS_SB(sb).cat_tree ? HFSPLUS_SB(sb).cat_tree->node_hash_cnt : 0); 37 } 38 #endif 39 } 40 41 static void hfsplus_read_inode(struct inode *inode) 42 { 43 struct hfs_find_data fd; 44 struct hfsplus_vh *vhdr; 45 int err; 46 47 atomic_inc(&HFSPLUS_SB(inode->i_sb).inode_cnt); 48 hfsplus_inode_check(inode->i_sb); 49 INIT_LIST_HEAD(&HFSPLUS_I(inode).open_dir_list); 50 init_MUTEX(&HFSPLUS_I(inode).extents_lock); 51 HFSPLUS_I(inode).flags = 0; 52 HFSPLUS_I(inode).rsrc_inode = NULL; 53 54 if (inode->i_ino >= HFSPLUS_FIRSTUSER_CNID) { 55 read_inode: 56 hfs_find_init(HFSPLUS_SB(inode->i_sb).cat_tree, &fd); 57 err = hfsplus_find_cat(inode->i_sb, inode->i_ino, &fd); 58 if (!err) 59 err = hfsplus_cat_read_inode(inode, &fd); 60 hfs_find_exit(&fd); 61 if (err) 62 goto bad_inode; 63 return; 64 } 65 vhdr = HFSPLUS_SB(inode->i_sb).s_vhdr; 66 switch(inode->i_ino) { 67 case HFSPLUS_ROOT_CNID: 68 goto read_inode; 69 case HFSPLUS_EXT_CNID: 70 hfsplus_inode_read_fork(inode, &vhdr->ext_file); 71 inode->i_mapping->a_ops = &hfsplus_btree_aops; 72 break; 73 case HFSPLUS_CAT_CNID: 74 hfsplus_inode_read_fork(inode, &vhdr->cat_file); 75 inode->i_mapping->a_ops = &hfsplus_btree_aops; 76 break; 77 case HFSPLUS_ALLOC_CNID: 78 hfsplus_inode_read_fork(inode, &vhdr->alloc_file); 79 inode->i_mapping->a_ops = &hfsplus_aops; 80 break; 81 case HFSPLUS_START_CNID: 82 hfsplus_inode_read_fork(inode, &vhdr->start_file); 83 break; 84 case HFSPLUS_ATTR_CNID: 85 hfsplus_inode_read_fork(inode, &vhdr->attr_file); 86 inode->i_mapping->a_ops = &hfsplus_btree_aops; 87 break; 88 default: 89 goto bad_inode; 90 } 91 92 return; 93 94 bad_inode: 95 make_bad_inode(inode); 96 } 97 98 static int hfsplus_write_inode(struct inode *inode, int unused) 99 { 100 struct hfsplus_vh *vhdr; 101 int ret = 0; 102 103 dprint(DBG_INODE, "hfsplus_write_inode: %lu\n", inode->i_ino); 104 hfsplus_ext_write_extent(inode); 105 if (inode->i_ino >= HFSPLUS_FIRSTUSER_CNID) { 106 return hfsplus_cat_write_inode(inode); 107 } 108 vhdr = HFSPLUS_SB(inode->i_sb).s_vhdr; 109 switch (inode->i_ino) { 110 case HFSPLUS_ROOT_CNID: 111 ret = hfsplus_cat_write_inode(inode); 112 break; 113 case HFSPLUS_EXT_CNID: 114 if (vhdr->ext_file.total_size != cpu_to_be64(inode->i_size)) { 115 HFSPLUS_SB(inode->i_sb).flags |= HFSPLUS_SB_WRITEBACKUP; 116 inode->i_sb->s_dirt = 1; 117 } 118 hfsplus_inode_write_fork(inode, &vhdr->ext_file); 119 hfs_btree_write(HFSPLUS_SB(inode->i_sb).ext_tree); 120 break; 121 case HFSPLUS_CAT_CNID: 122 if (vhdr->cat_file.total_size != cpu_to_be64(inode->i_size)) { 123 HFSPLUS_SB(inode->i_sb).flags |= HFSPLUS_SB_WRITEBACKUP; 124 inode->i_sb->s_dirt = 1; 125 } 126 hfsplus_inode_write_fork(inode, &vhdr->cat_file); 127 hfs_btree_write(HFSPLUS_SB(inode->i_sb).cat_tree); 128 break; 129 case HFSPLUS_ALLOC_CNID: 130 if (vhdr->alloc_file.total_size != cpu_to_be64(inode->i_size)) { 131 HFSPLUS_SB(inode->i_sb).flags |= HFSPLUS_SB_WRITEBACKUP; 132 inode->i_sb->s_dirt = 1; 133 } 134 hfsplus_inode_write_fork(inode, &vhdr->alloc_file); 135 break; 136 case HFSPLUS_START_CNID: 137 if (vhdr->start_file.total_size != cpu_to_be64(inode->i_size)) { 138 HFSPLUS_SB(inode->i_sb).flags |= HFSPLUS_SB_WRITEBACKUP; 139 inode->i_sb->s_dirt = 1; 140 } 141 hfsplus_inode_write_fork(inode, &vhdr->start_file); 142 break; 143 case HFSPLUS_ATTR_CNID: 144 if (vhdr->attr_file.total_size != cpu_to_be64(inode->i_size)) { 145 HFSPLUS_SB(inode->i_sb).flags |= HFSPLUS_SB_WRITEBACKUP; 146 inode->i_sb->s_dirt = 1; 147 } 148 hfsplus_inode_write_fork(inode, &vhdr->attr_file); 149 hfs_btree_write(HFSPLUS_SB(inode->i_sb).attr_tree); 150 break; 151 } 152 return ret; 153 } 154 155 static void hfsplus_clear_inode(struct inode *inode) 156 { 157 dprint(DBG_INODE, "hfsplus_clear_inode: %lu\n", inode->i_ino); 158 atomic_dec(&HFSPLUS_SB(inode->i_sb).inode_cnt); 159 if (HFSPLUS_IS_RSRC(inode)) { 160 HFSPLUS_I(HFSPLUS_I(inode).rsrc_inode).rsrc_inode = NULL; 161 iput(HFSPLUS_I(inode).rsrc_inode); 162 } 163 hfsplus_inode_check(inode->i_sb); 164 } 165 166 static void hfsplus_write_super(struct super_block *sb) 167 { 168 struct hfsplus_vh *vhdr = HFSPLUS_SB(sb).s_vhdr; 169 170 dprint(DBG_SUPER, "hfsplus_write_super\n"); 171 sb->s_dirt = 0; 172 if (sb->s_flags & MS_RDONLY) 173 /* warn? */ 174 return; 175 176 vhdr->free_blocks = cpu_to_be32(HFSPLUS_SB(sb).free_blocks); 177 vhdr->next_alloc = cpu_to_be32(HFSPLUS_SB(sb).next_alloc); 178 vhdr->next_cnid = cpu_to_be32(HFSPLUS_SB(sb).next_cnid); 179 vhdr->folder_count = cpu_to_be32(HFSPLUS_SB(sb).folder_count); 180 vhdr->file_count = cpu_to_be32(HFSPLUS_SB(sb).file_count); 181 182 mark_buffer_dirty(HFSPLUS_SB(sb).s_vhbh); 183 if (HFSPLUS_SB(sb).flags & HFSPLUS_SB_WRITEBACKUP) { 184 if (HFSPLUS_SB(sb).sect_count) { 185 struct buffer_head *bh; 186 u32 block, offset; 187 188 block = HFSPLUS_SB(sb).blockoffset; 189 block += (HFSPLUS_SB(sb).sect_count - 2) >> (sb->s_blocksize_bits - 9); 190 offset = ((HFSPLUS_SB(sb).sect_count - 2) << 9) & (sb->s_blocksize - 1); 191 printk("backup: %u,%u,%u,%u\n", HFSPLUS_SB(sb).blockoffset, 192 HFSPLUS_SB(sb).sect_count, block, offset); 193 bh = sb_bread(sb, block); 194 if (bh) { 195 vhdr = (struct hfsplus_vh *)(bh->b_data + offset); 196 if (be16_to_cpu(vhdr->signature) == HFSPLUS_VOLHEAD_SIG) { 197 memcpy(vhdr, HFSPLUS_SB(sb).s_vhdr, sizeof(*vhdr)); 198 mark_buffer_dirty(bh); 199 brelse(bh); 200 } else 201 printk("backup not found!\n"); 202 } 203 } 204 HFSPLUS_SB(sb).flags &= ~HFSPLUS_SB_WRITEBACKUP; 205 } 206 } 207 208 static void hfsplus_put_super(struct super_block *sb) 209 { 210 dprint(DBG_SUPER, "hfsplus_put_super\n"); 211 if (!(sb->s_flags & MS_RDONLY)) { 212 struct hfsplus_vh *vhdr = HFSPLUS_SB(sb).s_vhdr; 213 214 vhdr->modify_date = hfsp_now2mt(); 215 vhdr->attributes |= cpu_to_be32(HFSPLUS_VOL_UNMNT); 216 vhdr->attributes &= cpu_to_be32(~HFSPLUS_VOL_INCNSTNT); 217 mark_buffer_dirty(HFSPLUS_SB(sb).s_vhbh); 218 ll_rw_block(WRITE, 1, &HFSPLUS_SB(sb).s_vhbh); 219 wait_on_buffer(HFSPLUS_SB(sb).s_vhbh); 220 } 221 222 hfs_btree_close(HFSPLUS_SB(sb).cat_tree); 223 hfs_btree_close(HFSPLUS_SB(sb).ext_tree); 224 iput(HFSPLUS_SB(sb).alloc_file); 225 iput(HFSPLUS_SB(sb).hidden_dir); 226 brelse(HFSPLUS_SB(sb).s_vhbh); 227 if (HFSPLUS_SB(sb).nls) 228 unload_nls(HFSPLUS_SB(sb).nls); 229 } 230 231 static int hfsplus_statfs(struct super_block *sb, struct kstatfs *buf) 232 { 233 buf->f_type = HFSPLUS_SUPER_MAGIC; 234 buf->f_bsize = sb->s_blocksize; 235 buf->f_blocks = HFSPLUS_SB(sb).total_blocks << HFSPLUS_SB(sb).fs_shift; 236 buf->f_bfree = HFSPLUS_SB(sb).free_blocks << HFSPLUS_SB(sb).fs_shift; 237 buf->f_bavail = buf->f_bfree; 238 buf->f_files = 0xFFFFFFFF; 239 buf->f_ffree = 0xFFFFFFFF - HFSPLUS_SB(sb).next_cnid; 240 buf->f_namelen = HFSPLUS_MAX_STRLEN; 241 242 return 0; 243 } 244 245 static int hfsplus_remount(struct super_block *sb, int *flags, char *data) 246 { 247 if ((*flags & MS_RDONLY) == (sb->s_flags & MS_RDONLY)) 248 return 0; 249 if (!(*flags & MS_RDONLY)) { 250 struct hfsplus_vh *vhdr = HFSPLUS_SB(sb).s_vhdr; 251 252 if (!(vhdr->attributes & cpu_to_be32(HFSPLUS_VOL_UNMNT))) { 253 printk("HFS+-fs warning: Filesystem was not cleanly unmounted, " 254 "running fsck.hfsplus is recommended. leaving read-only.\n"); 255 sb->s_flags |= MS_RDONLY; 256 *flags |= MS_RDONLY; 257 } else if (vhdr->attributes & cpu_to_be32(HFSPLUS_VOL_SOFTLOCK)) { 258 printk("HFS+-fs: Filesystem is marked locked, leaving read-only.\n"); 259 sb->s_flags |= MS_RDONLY; 260 *flags |= MS_RDONLY; 261 } 262 } 263 return 0; 264 } 265 266 static struct super_operations hfsplus_sops = { 267 .alloc_inode = hfsplus_alloc_inode, 268 .destroy_inode = hfsplus_destroy_inode, 269 .read_inode = hfsplus_read_inode, 270 .write_inode = hfsplus_write_inode, 271 .clear_inode = hfsplus_clear_inode, 272 .put_super = hfsplus_put_super, 273 .write_super = hfsplus_write_super, 274 .statfs = hfsplus_statfs, 275 .remount_fs = hfsplus_remount, 276 }; 277 278 static int hfsplus_fill_super(struct super_block *sb, void *data, int silent) 279 { 280 struct hfsplus_vh *vhdr; 281 struct hfsplus_sb_info *sbi; 282 hfsplus_cat_entry entry; 283 struct hfs_find_data fd; 284 struct inode *root; 285 struct qstr str; 286 struct nls_table *nls = NULL; 287 int err = -EINVAL; 288 289 sbi = kmalloc(sizeof(struct hfsplus_sb_info), GFP_KERNEL); 290 if (!sbi) 291 return -ENOMEM; 292 293 memset(sbi, 0, sizeof(HFSPLUS_SB(sb))); 294 sb->s_fs_info = sbi; 295 INIT_HLIST_HEAD(&sbi->rsrc_inodes); 296 fill_defaults(sbi); 297 if (!parse_options(data, sbi)) { 298 if (!silent) 299 printk("HFS+-fs: unable to parse mount options\n"); 300 err = -EINVAL; 301 goto cleanup; 302 } 303 304 /* temporarily use utf8 to correctly find the hidden dir below */ 305 nls = sbi->nls; 306 sbi->nls = load_nls("utf8"); 307 if (!nls) { 308 printk("HFS+: unable to load nls for utf8\n"); 309 err = -EINVAL; 310 goto cleanup; 311 } 312 313 /* Grab the volume header */ 314 if (hfsplus_read_wrapper(sb)) { 315 if (!silent) 316 printk("HFS+-fs: unable to find HFS+ superblock\n"); 317 err = -EINVAL; 318 goto cleanup; 319 } 320 vhdr = HFSPLUS_SB(sb).s_vhdr; 321 322 /* Copy parts of the volume header into the superblock */ 323 sb->s_magic = be16_to_cpu(vhdr->signature); 324 if (be16_to_cpu(vhdr->version) != HFSPLUS_CURRENT_VERSION) { 325 if (!silent) 326 printk("HFS+-fs: wrong filesystem version\n"); 327 goto cleanup; 328 } 329 HFSPLUS_SB(sb).total_blocks = be32_to_cpu(vhdr->total_blocks); 330 HFSPLUS_SB(sb).free_blocks = be32_to_cpu(vhdr->free_blocks); 331 HFSPLUS_SB(sb).next_alloc = be32_to_cpu(vhdr->next_alloc); 332 HFSPLUS_SB(sb).next_cnid = be32_to_cpu(vhdr->next_cnid); 333 HFSPLUS_SB(sb).file_count = be32_to_cpu(vhdr->file_count); 334 HFSPLUS_SB(sb).folder_count = be32_to_cpu(vhdr->folder_count); 335 HFSPLUS_SB(sb).data_clump_blocks = be32_to_cpu(vhdr->data_clump_sz) >> HFSPLUS_SB(sb).alloc_blksz_shift; 336 if (!HFSPLUS_SB(sb).data_clump_blocks) 337 HFSPLUS_SB(sb).data_clump_blocks = 1; 338 HFSPLUS_SB(sb).rsrc_clump_blocks = be32_to_cpu(vhdr->rsrc_clump_sz) >> HFSPLUS_SB(sb).alloc_blksz_shift; 339 if (!HFSPLUS_SB(sb).rsrc_clump_blocks) 340 HFSPLUS_SB(sb).rsrc_clump_blocks = 1; 341 342 /* Set up operations so we can load metadata */ 343 sb->s_op = &hfsplus_sops; 344 sb->s_maxbytes = MAX_LFS_FILESIZE; 345 346 if (!(vhdr->attributes & cpu_to_be32(HFSPLUS_VOL_UNMNT))) { 347 if (!silent) 348 printk("HFS+-fs warning: Filesystem was not cleanly unmounted, " 349 "running fsck.hfsplus is recommended. mounting read-only.\n"); 350 sb->s_flags |= MS_RDONLY; 351 } else if (vhdr->attributes & cpu_to_be32(HFSPLUS_VOL_SOFTLOCK)) { 352 if (!silent) 353 printk("HFS+-fs: Filesystem is marked locked, mounting read-only.\n"); 354 sb->s_flags |= MS_RDONLY; 355 } 356 357 /* Load metadata objects (B*Trees) */ 358 HFSPLUS_SB(sb).ext_tree = hfs_btree_open(sb, HFSPLUS_EXT_CNID); 359 if (!HFSPLUS_SB(sb).ext_tree) { 360 if (!silent) 361 printk("HFS+-fs: failed to load extents file\n"); 362 goto cleanup; 363 } 364 HFSPLUS_SB(sb).cat_tree = hfs_btree_open(sb, HFSPLUS_CAT_CNID); 365 if (!HFSPLUS_SB(sb).cat_tree) { 366 if (!silent) 367 printk("HFS+-fs: failed to load catalog file\n"); 368 goto cleanup; 369 } 370 371 HFSPLUS_SB(sb).alloc_file = iget(sb, HFSPLUS_ALLOC_CNID); 372 if (!HFSPLUS_SB(sb).alloc_file) { 373 if (!silent) 374 printk("HFS+-fs: failed to load allocation file\n"); 375 goto cleanup; 376 } 377 378 /* Load the root directory */ 379 root = iget(sb, HFSPLUS_ROOT_CNID); 380 sb->s_root = d_alloc_root(root); 381 if (!sb->s_root) { 382 if (!silent) 383 printk("HFS+-fs: failed to load root directory\n"); 384 iput(root); 385 goto cleanup; 386 } 387 388 str.len = sizeof(HFSP_HIDDENDIR_NAME) - 1; 389 str.name = HFSP_HIDDENDIR_NAME; 390 hfs_find_init(HFSPLUS_SB(sb).cat_tree, &fd); 391 hfsplus_cat_build_key(sb, fd.search_key, HFSPLUS_ROOT_CNID, &str); 392 if (!hfs_brec_read(&fd, &entry, sizeof(entry))) { 393 hfs_find_exit(&fd); 394 if (entry.type != cpu_to_be16(HFSPLUS_FOLDER)) 395 goto cleanup; 396 HFSPLUS_SB(sb).hidden_dir = iget(sb, be32_to_cpu(entry.folder.id)); 397 if (!HFSPLUS_SB(sb).hidden_dir) 398 goto cleanup; 399 } else 400 hfs_find_exit(&fd); 401 402 if (sb->s_flags & MS_RDONLY) 403 goto out; 404 405 /* H+LX == hfsplusutils, H+Lx == this driver, H+lx is unused 406 * all three are registered with Apple for our use 407 */ 408 vhdr->last_mount_vers = cpu_to_be32(HFSP_MOUNT_VERSION); 409 vhdr->modify_date = hfsp_now2mt(); 410 vhdr->write_count = cpu_to_be32(be32_to_cpu(vhdr->write_count) + 1); 411 vhdr->attributes &= cpu_to_be32(~HFSPLUS_VOL_UNMNT); 412 vhdr->attributes |= cpu_to_be32(HFSPLUS_VOL_INCNSTNT); 413 mark_buffer_dirty(HFSPLUS_SB(sb).s_vhbh); 414 ll_rw_block(WRITE, 1, &HFSPLUS_SB(sb).s_vhbh); 415 wait_on_buffer(HFSPLUS_SB(sb).s_vhbh); 416 417 if (!HFSPLUS_SB(sb).hidden_dir) { 418 printk("HFS+: create hidden dir...\n"); 419 HFSPLUS_SB(sb).hidden_dir = hfsplus_new_inode(sb, S_IFDIR); 420 hfsplus_create_cat(HFSPLUS_SB(sb).hidden_dir->i_ino, sb->s_root->d_inode, 421 &str, HFSPLUS_SB(sb).hidden_dir); 422 mark_inode_dirty(HFSPLUS_SB(sb).hidden_dir); 423 } 424 out: 425 unload_nls(sbi->nls); 426 sbi->nls = nls; 427 return 0; 428 429 cleanup: 430 hfsplus_put_super(sb); 431 if (nls) 432 unload_nls(nls); 433 return err; 434 } 435 436 MODULE_AUTHOR("Brad Boyer"); 437 MODULE_DESCRIPTION("Extended Macintosh Filesystem"); 438 MODULE_LICENSE("GPL"); 439 440 static kmem_cache_t *hfsplus_inode_cachep; 441 442 static struct inode *hfsplus_alloc_inode(struct super_block *sb) 443 { 444 struct hfsplus_inode_info *i; 445 446 i = kmem_cache_alloc(hfsplus_inode_cachep, SLAB_KERNEL); 447 return i ? &i->vfs_inode : NULL; 448 } 449 450 static void hfsplus_destroy_inode(struct inode *inode) 451 { 452 kmem_cache_free(hfsplus_inode_cachep, &HFSPLUS_I(inode)); 453 } 454 455 #define HFSPLUS_INODE_SIZE sizeof(struct hfsplus_inode_info) 456 457 static struct super_block *hfsplus_get_sb(struct file_system_type *fs_type, 458 int flags, const char *dev_name, void *data) 459 { 460 return get_sb_bdev(fs_type, flags, dev_name, data, hfsplus_fill_super); 461 } 462 463 static struct file_system_type hfsplus_fs_type = { 464 .owner = THIS_MODULE, 465 .name = "hfsplus", 466 .get_sb = hfsplus_get_sb, 467 .kill_sb = kill_block_super, 468 .fs_flags = FS_REQUIRES_DEV, 469 }; 470 471 static void hfsplus_init_once(void *p, kmem_cache_t *cachep, unsigned long flags) 472 { 473 struct hfsplus_inode_info *i = p; 474 475 if ((flags & (SLAB_CTOR_VERIFY|SLAB_CTOR_CONSTRUCTOR)) == SLAB_CTOR_CONSTRUCTOR) 476 inode_init_once(&i->vfs_inode); 477 } 478 479 static int __init init_hfsplus_fs(void) 480 { 481 int err; 482 483 hfsplus_inode_cachep = kmem_cache_create("hfsplus_icache", 484 HFSPLUS_INODE_SIZE, 0, SLAB_HWCACHE_ALIGN, 485 hfsplus_init_once, NULL); 486 if (!hfsplus_inode_cachep) 487 return -ENOMEM; 488 err = register_filesystem(&hfsplus_fs_type); 489 if (err) 490 kmem_cache_destroy(hfsplus_inode_cachep); 491 return err; 492 } 493 494 static void __exit exit_hfsplus_fs(void) 495 { 496 unregister_filesystem(&hfsplus_fs_type); 497 if (kmem_cache_destroy(hfsplus_inode_cachep)) 498 printk(KERN_INFO "hfsplus_inode_cache: not all structures were freed\n"); 499 } 500 501 module_init(init_hfsplus_fs) 502 module_exit(exit_hfsplus_fs) 503