[ALSA] Fix a missing include
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / fs / hfsplus / super.c
blob8093351bd7c3b0a9cefcb73ce1879a0527655c6e
1 /*
2 * linux/fs/hfsplus/super.c
4 * Copyright (C) 2001
5 * Brad Boyer (flar@allandria.com)
6 * (C) 2003 Ardis Technologies <roman@ardistech.com>
8 */
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/vfs.h>
18 #include <linux/nls.h>
20 static struct inode *hfsplus_alloc_inode(struct super_block *sb);
21 static void hfsplus_destroy_inode(struct inode *inode);
23 #include "hfsplus_fs.h"
25 void hfsplus_inode_check(struct super_block *sb)
27 #if 0
28 u32 cnt = atomic_read(&HFSPLUS_SB(sb).inode_cnt);
29 u32 last_cnt = HFSPLUS_SB(sb).last_inode_cnt;
31 if (cnt <= (last_cnt / 2) ||
32 cnt >= (last_cnt * 2)) {
33 HFSPLUS_SB(sb).last_inode_cnt = cnt;
34 printk("inode_check: %u,%u,%u\n", cnt, last_cnt,
35 HFSPLUS_SB(sb).cat_tree ? HFSPLUS_SB(sb).cat_tree->node_hash_cnt : 0);
37 #endif
40 static void hfsplus_read_inode(struct inode *inode)
42 struct hfs_find_data fd;
43 struct hfsplus_vh *vhdr;
44 int err;
46 atomic_inc(&HFSPLUS_SB(inode->i_sb).inode_cnt);
47 hfsplus_inode_check(inode->i_sb);
48 INIT_LIST_HEAD(&HFSPLUS_I(inode).open_dir_list);
49 init_MUTEX(&HFSPLUS_I(inode).extents_lock);
50 HFSPLUS_I(inode).flags = 0;
51 HFSPLUS_I(inode).rsrc_inode = NULL;
52 atomic_set(&HFSPLUS_I(inode).opencnt, 0);
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;
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;
92 return;
94 bad_inode:
95 make_bad_inode(inode);
98 static int hfsplus_write_inode(struct inode *inode, int unused)
100 struct hfsplus_vh *vhdr;
101 int ret = 0;
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);
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;
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;
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;
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;
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;
148 hfsplus_inode_write_fork(inode, &vhdr->attr_file);
149 hfs_btree_write(HFSPLUS_SB(inode->i_sb).attr_tree);
150 break;
152 return ret;
155 static void hfsplus_clear_inode(struct inode *inode)
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);
163 hfsplus_inode_check(inode->i_sb);
166 static void hfsplus_write_super(struct super_block *sb)
168 struct hfsplus_vh *vhdr = HFSPLUS_SB(sb).s_vhdr;
170 dprint(DBG_SUPER, "hfsplus_write_super\n");
171 sb->s_dirt = 0;
172 if (sb->s_flags & MS_RDONLY)
173 /* warn? */
174 return;
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);
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;
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");
204 HFSPLUS_SB(sb).flags &= ~HFSPLUS_SB_WRITEBACKUP;
208 static void hfsplus_put_super(struct super_block *sb)
210 dprint(DBG_SUPER, "hfsplus_put_super\n");
211 if (!sb->s_fs_info)
212 return;
213 if (!(sb->s_flags & MS_RDONLY) && HFSPLUS_SB(sb).s_vhdr) {
214 struct hfsplus_vh *vhdr = HFSPLUS_SB(sb).s_vhdr;
216 vhdr->modify_date = hfsp_now2mt();
217 vhdr->attributes |= cpu_to_be32(HFSPLUS_VOL_UNMNT);
218 vhdr->attributes &= cpu_to_be32(~HFSPLUS_VOL_INCNSTNT);
219 mark_buffer_dirty(HFSPLUS_SB(sb).s_vhbh);
220 sync_dirty_buffer(HFSPLUS_SB(sb).s_vhbh);
223 hfs_btree_close(HFSPLUS_SB(sb).cat_tree);
224 hfs_btree_close(HFSPLUS_SB(sb).ext_tree);
225 iput(HFSPLUS_SB(sb).alloc_file);
226 iput(HFSPLUS_SB(sb).hidden_dir);
227 brelse(HFSPLUS_SB(sb).s_vhbh);
228 if (HFSPLUS_SB(sb).nls)
229 unload_nls(HFSPLUS_SB(sb).nls);
230 kfree(sb->s_fs_info);
231 sb->s_fs_info = NULL;
234 static int hfsplus_statfs(struct super_block *sb, struct kstatfs *buf)
236 buf->f_type = HFSPLUS_SUPER_MAGIC;
237 buf->f_bsize = sb->s_blocksize;
238 buf->f_blocks = HFSPLUS_SB(sb).total_blocks << HFSPLUS_SB(sb).fs_shift;
239 buf->f_bfree = HFSPLUS_SB(sb).free_blocks << HFSPLUS_SB(sb).fs_shift;
240 buf->f_bavail = buf->f_bfree;
241 buf->f_files = 0xFFFFFFFF;
242 buf->f_ffree = 0xFFFFFFFF - HFSPLUS_SB(sb).next_cnid;
243 buf->f_namelen = HFSPLUS_MAX_STRLEN;
245 return 0;
248 static int hfsplus_remount(struct super_block *sb, int *flags, char *data)
250 if ((*flags & MS_RDONLY) == (sb->s_flags & MS_RDONLY))
251 return 0;
252 if (!(*flags & MS_RDONLY)) {
253 struct hfsplus_vh *vhdr = HFSPLUS_SB(sb).s_vhdr;
254 struct hfsplus_sb_info sbi;
256 memset(&sbi, 0, sizeof(struct hfsplus_sb_info));
257 sbi.nls = HFSPLUS_SB(sb).nls;
258 if (!hfsplus_parse_options(data, &sbi))
259 return -EINVAL;
261 if (!(vhdr->attributes & cpu_to_be32(HFSPLUS_VOL_UNMNT))) {
262 printk("HFS+-fs warning: Filesystem was not cleanly unmounted, "
263 "running fsck.hfsplus is recommended. leaving read-only.\n");
264 sb->s_flags |= MS_RDONLY;
265 *flags |= MS_RDONLY;
266 } else if (sbi.flags & HFSPLUS_SB_FORCE) {
267 /* nothing */
268 } else if (vhdr->attributes & cpu_to_be32(HFSPLUS_VOL_SOFTLOCK)) {
269 printk("HFS+-fs: Filesystem is marked locked, leaving read-only.\n");
270 sb->s_flags |= MS_RDONLY;
271 *flags |= MS_RDONLY;
272 } else if (vhdr->attributes & cpu_to_be32(HFSPLUS_VOL_JOURNALED)) {
273 printk("HFS+-fs: Filesystem is marked journaled, leaving read-only.\n");
274 sb->s_flags |= MS_RDONLY;
275 *flags |= MS_RDONLY;
278 return 0;
281 static struct super_operations hfsplus_sops = {
282 .alloc_inode = hfsplus_alloc_inode,
283 .destroy_inode = hfsplus_destroy_inode,
284 .read_inode = hfsplus_read_inode,
285 .write_inode = hfsplus_write_inode,
286 .clear_inode = hfsplus_clear_inode,
287 .put_super = hfsplus_put_super,
288 .write_super = hfsplus_write_super,
289 .statfs = hfsplus_statfs,
290 .remount_fs = hfsplus_remount,
291 .show_options = hfsplus_show_options,
294 static int hfsplus_fill_super(struct super_block *sb, void *data, int silent)
296 struct hfsplus_vh *vhdr;
297 struct hfsplus_sb_info *sbi;
298 hfsplus_cat_entry entry;
299 struct hfs_find_data fd;
300 struct inode *root;
301 struct qstr str;
302 struct nls_table *nls = NULL;
303 int err = -EINVAL;
305 sbi = kmalloc(sizeof(struct hfsplus_sb_info), GFP_KERNEL);
306 if (!sbi)
307 return -ENOMEM;
309 memset(sbi, 0, sizeof(HFSPLUS_SB(sb)));
310 sb->s_fs_info = sbi;
311 INIT_HLIST_HEAD(&sbi->rsrc_inodes);
312 hfsplus_fill_defaults(sbi);
313 if (!hfsplus_parse_options(data, sbi)) {
314 if (!silent)
315 printk("HFS+-fs: unable to parse mount options\n");
316 err = -EINVAL;
317 goto cleanup;
320 /* temporarily use utf8 to correctly find the hidden dir below */
321 nls = sbi->nls;
322 sbi->nls = load_nls("utf8");
323 if (!nls) {
324 printk("HFS+: unable to load nls for utf8\n");
325 err = -EINVAL;
326 goto cleanup;
329 /* Grab the volume header */
330 if (hfsplus_read_wrapper(sb)) {
331 if (!silent)
332 printk("HFS+-fs: unable to find HFS+ superblock\n");
333 err = -EINVAL;
334 goto cleanup;
336 vhdr = HFSPLUS_SB(sb).s_vhdr;
338 /* Copy parts of the volume header into the superblock */
339 sb->s_magic = be16_to_cpu(vhdr->signature);
340 if (be16_to_cpu(vhdr->version) != HFSPLUS_CURRENT_VERSION) {
341 if (!silent)
342 printk("HFS+-fs: wrong filesystem version\n");
343 goto cleanup;
345 HFSPLUS_SB(sb).total_blocks = be32_to_cpu(vhdr->total_blocks);
346 HFSPLUS_SB(sb).free_blocks = be32_to_cpu(vhdr->free_blocks);
347 HFSPLUS_SB(sb).next_alloc = be32_to_cpu(vhdr->next_alloc);
348 HFSPLUS_SB(sb).next_cnid = be32_to_cpu(vhdr->next_cnid);
349 HFSPLUS_SB(sb).file_count = be32_to_cpu(vhdr->file_count);
350 HFSPLUS_SB(sb).folder_count = be32_to_cpu(vhdr->folder_count);
351 HFSPLUS_SB(sb).data_clump_blocks = be32_to_cpu(vhdr->data_clump_sz) >> HFSPLUS_SB(sb).alloc_blksz_shift;
352 if (!HFSPLUS_SB(sb).data_clump_blocks)
353 HFSPLUS_SB(sb).data_clump_blocks = 1;
354 HFSPLUS_SB(sb).rsrc_clump_blocks = be32_to_cpu(vhdr->rsrc_clump_sz) >> HFSPLUS_SB(sb).alloc_blksz_shift;
355 if (!HFSPLUS_SB(sb).rsrc_clump_blocks)
356 HFSPLUS_SB(sb).rsrc_clump_blocks = 1;
358 /* Set up operations so we can load metadata */
359 sb->s_op = &hfsplus_sops;
360 sb->s_maxbytes = MAX_LFS_FILESIZE;
362 if (!(vhdr->attributes & cpu_to_be32(HFSPLUS_VOL_UNMNT))) {
363 if (!silent)
364 printk("HFS+-fs warning: Filesystem was not cleanly unmounted, "
365 "running fsck.hfsplus is recommended. mounting read-only.\n");
366 sb->s_flags |= MS_RDONLY;
367 } else if (sbi->flags & HFSPLUS_SB_FORCE) {
368 /* nothing */
369 } else if (vhdr->attributes & cpu_to_be32(HFSPLUS_VOL_SOFTLOCK)) {
370 if (!silent)
371 printk("HFS+-fs: Filesystem is marked locked, mounting read-only.\n");
372 sb->s_flags |= MS_RDONLY;
373 } else if (vhdr->attributes & cpu_to_be32(HFSPLUS_VOL_JOURNALED)) {
374 if (!silent)
375 printk("HFS+-fs: write access to a jounaled filesystem is not supported, "
376 "use the force option at your own risk, mounting read-only.\n");
377 sb->s_flags |= MS_RDONLY;
379 sbi->flags &= ~HFSPLUS_SB_FORCE;
381 /* Load metadata objects (B*Trees) */
382 HFSPLUS_SB(sb).ext_tree = hfs_btree_open(sb, HFSPLUS_EXT_CNID);
383 if (!HFSPLUS_SB(sb).ext_tree) {
384 if (!silent)
385 printk("HFS+-fs: failed to load extents file\n");
386 goto cleanup;
388 HFSPLUS_SB(sb).cat_tree = hfs_btree_open(sb, HFSPLUS_CAT_CNID);
389 if (!HFSPLUS_SB(sb).cat_tree) {
390 if (!silent)
391 printk("HFS+-fs: failed to load catalog file\n");
392 goto cleanup;
395 HFSPLUS_SB(sb).alloc_file = iget(sb, HFSPLUS_ALLOC_CNID);
396 if (!HFSPLUS_SB(sb).alloc_file) {
397 if (!silent)
398 printk("HFS+-fs: failed to load allocation file\n");
399 goto cleanup;
402 /* Load the root directory */
403 root = iget(sb, HFSPLUS_ROOT_CNID);
404 sb->s_root = d_alloc_root(root);
405 if (!sb->s_root) {
406 if (!silent)
407 printk("HFS+-fs: failed to load root directory\n");
408 iput(root);
409 goto cleanup;
412 str.len = sizeof(HFSP_HIDDENDIR_NAME) - 1;
413 str.name = HFSP_HIDDENDIR_NAME;
414 hfs_find_init(HFSPLUS_SB(sb).cat_tree, &fd);
415 hfsplus_cat_build_key(sb, fd.search_key, HFSPLUS_ROOT_CNID, &str);
416 if (!hfs_brec_read(&fd, &entry, sizeof(entry))) {
417 hfs_find_exit(&fd);
418 if (entry.type != cpu_to_be16(HFSPLUS_FOLDER))
419 goto cleanup;
420 HFSPLUS_SB(sb).hidden_dir = iget(sb, be32_to_cpu(entry.folder.id));
421 if (!HFSPLUS_SB(sb).hidden_dir)
422 goto cleanup;
423 } else
424 hfs_find_exit(&fd);
426 if (sb->s_flags & MS_RDONLY)
427 goto out;
429 /* H+LX == hfsplusutils, H+Lx == this driver, H+lx is unused
430 * all three are registered with Apple for our use
432 vhdr->last_mount_vers = cpu_to_be32(HFSP_MOUNT_VERSION);
433 vhdr->modify_date = hfsp_now2mt();
434 vhdr->write_count = cpu_to_be32(be32_to_cpu(vhdr->write_count) + 1);
435 vhdr->attributes &= cpu_to_be32(~HFSPLUS_VOL_UNMNT);
436 vhdr->attributes |= cpu_to_be32(HFSPLUS_VOL_INCNSTNT);
437 mark_buffer_dirty(HFSPLUS_SB(sb).s_vhbh);
438 sync_dirty_buffer(HFSPLUS_SB(sb).s_vhbh);
440 if (!HFSPLUS_SB(sb).hidden_dir) {
441 printk("HFS+: create hidden dir...\n");
442 HFSPLUS_SB(sb).hidden_dir = hfsplus_new_inode(sb, S_IFDIR);
443 hfsplus_create_cat(HFSPLUS_SB(sb).hidden_dir->i_ino, sb->s_root->d_inode,
444 &str, HFSPLUS_SB(sb).hidden_dir);
445 mark_inode_dirty(HFSPLUS_SB(sb).hidden_dir);
447 out:
448 unload_nls(sbi->nls);
449 sbi->nls = nls;
450 return 0;
452 cleanup:
453 hfsplus_put_super(sb);
454 if (nls)
455 unload_nls(nls);
456 return err;
459 MODULE_AUTHOR("Brad Boyer");
460 MODULE_DESCRIPTION("Extended Macintosh Filesystem");
461 MODULE_LICENSE("GPL");
463 static kmem_cache_t *hfsplus_inode_cachep;
465 static struct inode *hfsplus_alloc_inode(struct super_block *sb)
467 struct hfsplus_inode_info *i;
469 i = kmem_cache_alloc(hfsplus_inode_cachep, SLAB_KERNEL);
470 return i ? &i->vfs_inode : NULL;
473 static void hfsplus_destroy_inode(struct inode *inode)
475 kmem_cache_free(hfsplus_inode_cachep, &HFSPLUS_I(inode));
478 #define HFSPLUS_INODE_SIZE sizeof(struct hfsplus_inode_info)
480 static struct super_block *hfsplus_get_sb(struct file_system_type *fs_type,
481 int flags, const char *dev_name, void *data)
483 return get_sb_bdev(fs_type, flags, dev_name, data, hfsplus_fill_super);
486 static struct file_system_type hfsplus_fs_type = {
487 .owner = THIS_MODULE,
488 .name = "hfsplus",
489 .get_sb = hfsplus_get_sb,
490 .kill_sb = kill_block_super,
491 .fs_flags = FS_REQUIRES_DEV,
494 static void hfsplus_init_once(void *p, kmem_cache_t *cachep, unsigned long flags)
496 struct hfsplus_inode_info *i = p;
498 if ((flags & (SLAB_CTOR_VERIFY|SLAB_CTOR_CONSTRUCTOR)) == SLAB_CTOR_CONSTRUCTOR)
499 inode_init_once(&i->vfs_inode);
502 static int __init init_hfsplus_fs(void)
504 int err;
506 hfsplus_inode_cachep = kmem_cache_create("hfsplus_icache",
507 HFSPLUS_INODE_SIZE, 0, SLAB_HWCACHE_ALIGN,
508 hfsplus_init_once, NULL);
509 if (!hfsplus_inode_cachep)
510 return -ENOMEM;
511 err = register_filesystem(&hfsplus_fs_type);
512 if (err)
513 kmem_cache_destroy(hfsplus_inode_cachep);
514 return err;
517 static void __exit exit_hfsplus_fs(void)
519 unregister_filesystem(&hfsplus_fs_type);
520 if (kmem_cache_destroy(hfsplus_inode_cachep))
521 printk(KERN_INFO "hfsplus_inode_cache: not all structures were freed\n");
524 module_init(init_hfsplus_fs)
525 module_exit(exit_hfsplus_fs)