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