4 * Copyright (C) 1995-1997 Paul H. Hargrove
5 * This file may be distributed under the terms of the GNU General Public License.
7 * This file contains hfs_read_super(), some of the super_ops and
8 * init_module() and cleanup_module(). The remaining super_ops are in
9 * inode.c since they deal with inodes.
11 * Based on the minix file system code, (C) 1991, 1992 by Linus Torvalds
13 * "XXX" in a comment is a note to myself to consider changing something.
15 * In function preconditions the term "valid" applied to a pointer to
16 * a structure means that the pointer is non-NULL and the structure it
17 * points to has all fields initialized to consistent values.
19 * The code in this file initializes some structures which contain
20 * pointers by calling memset(&foo, 0, sizeof(foo)).
21 * This produces the desired behavior only due to the non-ANSI
22 * assumption that the machine representation of NULL is all zeros.
26 #include <linux/hfs_fs_sb.h>
27 #include <linux/hfs_fs_i.h>
28 #include <linux/hfs_fs.h>
30 #include <linux/config.h> /* for CONFIG_MAC_PARTITION */
31 #include <linux/blkdev.h>
32 #include <linux/module.h>
33 #include <linux/init.h>
35 MODULE_LICENSE("GPL");
37 /*================ Forward declarations ================*/
39 static void hfs_read_inode(struct inode *);
40 static void hfs_put_super(struct super_block *);
41 static int hfs_statfs(struct super_block *, struct statfs *);
42 static void hfs_write_super(struct super_block *);
44 /*================ Global variables ================*/
46 static struct super_operations hfs_super_operations = {
47 read_inode: hfs_read_inode,
48 put_inode: hfs_put_inode,
49 put_super: hfs_put_super,
50 write_super: hfs_write_super,
54 /*================ File-local variables ================*/
56 static DECLARE_FSTYPE_DEV(hfs_fs, "hfs", hfs_read_super);
58 /*================ File-local functions ================*/
63 * this doesn't actually do much. hfs_iget actually fills in the
64 * necessary inode information.
66 static void hfs_read_inode(struct inode *inode)
75 * This function is called by the VFS only. When the filesystem
76 * is mounted r/w it updates the MDB on disk.
78 * struct super_block *sb: Pointer to the hfs superblock
84 * 'sb' points to a "valid" (struct super_block).
86 * The MDB is marked 'unsuccessfully unmounted' by clearing bit 8 of drAtrb
87 * (hfs_put_super() must set this flag!). Some MDB fields are updated
88 * and the MDB buffer is written to disk by calling hfs_mdb_commit().
90 static void hfs_write_super(struct super_block *sb)
92 struct hfs_mdb *mdb = HFS_SB(sb)->s_mdb;
94 /* is this a valid hfs superblock? */
95 if (!sb || sb->s_magic != HFS_SUPER_MAGIC) {
99 if (!(sb->s_flags & MS_RDONLY)) {
100 /* sync everything to the buffers */
101 hfs_mdb_commit(mdb, 0);
109 * This is the put_super() entry in the super_operations structure for
110 * HFS filesystems. The purpose is to release the resources
111 * associated with the superblock sb.
113 static void hfs_put_super(struct super_block *sb)
115 struct hfs_mdb *mdb = HFS_SB(sb)->s_mdb;
117 if (!(sb->s_flags & MS_RDONLY)) {
118 hfs_mdb_commit(mdb, 0);
122 /* release the MDB's resources */
123 hfs_mdb_put(mdb, sb->s_flags & MS_RDONLY);
125 /* restore default blocksize for the device */
126 set_blocksize(sb->s_dev, BLOCK_SIZE);
132 * This is the statfs() entry in the super_operations structure for
133 * HFS filesystems. The purpose is to return various data about the
136 * changed f_files/f_ffree to reflect the fs_ablock/free_ablocks.
138 static int hfs_statfs(struct super_block *sb, struct statfs *buf)
140 struct hfs_mdb *mdb = HFS_SB(sb)->s_mdb;
142 buf->f_type = HFS_SUPER_MAGIC;
143 buf->f_bsize = HFS_SECTOR_SIZE;
144 buf->f_blocks = mdb->alloc_blksz * mdb->fs_ablocks;
145 buf->f_bfree = mdb->alloc_blksz * mdb->free_ablocks;
146 buf->f_bavail = buf->f_bfree;
147 buf->f_files = mdb->fs_ablocks;
148 buf->f_ffree = mdb->free_ablocks;
149 buf->f_namelen = HFS_NAMELEN;
157 * adapted from linux/fs/msdos/inode.c written 1992,93 by Werner Almesberger
158 * This function is called by hfs_read_super() to parse the mount options.
160 static int parse_options(char *options, struct hfs_sb_info *hsb, int *part)
162 char *this_char, *value;
165 /* initialize the sb with defaults */
166 memset(hsb, 0, sizeof(*hsb));
167 hsb->magic = HFS_SB_MAGIC;
168 hsb->s_uid = current->uid;
169 hsb->s_gid = current->gid;
170 hsb->s_umask = current->fs->umask;
171 hsb->s_type = 0x3f3f3f3f; /* == '????' */
172 hsb->s_creator = 0x3f3f3f3f; /* == '????' */
173 hsb->s_lowercase = 0;
176 /* default version. 0 just selects the defaults */
186 for (this_char = strtok(options,","); this_char;
187 this_char = strtok(NULL,",")) {
188 if ((value = strchr(this_char,'=')) != NULL) {
191 /* Numeric-valued options */
192 if (!strcmp(this_char, "version")) {
193 if (!value || !*value) {
196 hsb->s_version = simple_strtoul(value,&value,0);
200 } else if (!strcmp(this_char,"uid")) {
201 if (!value || !*value) {
204 hsb->s_uid = simple_strtoul(value,&value,0);
208 } else if (!strcmp(this_char,"gid")) {
209 if (!value || !*value) {
212 hsb->s_gid = simple_strtoul(value,&value,0);
216 } else if (!strcmp(this_char,"umask")) {
217 if (!value || !*value) {
220 hsb->s_umask = simple_strtoul(value,&value,8);
224 } else if (!strcmp(this_char,"part")) {
225 if (!value || !*value) {
228 *part = simple_strtoul(value,&value,0);
232 /* String-valued options */
233 } else if (!strcmp(this_char,"type") && value) {
234 if (strlen(value) != 4) {
237 hsb->s_type = hfs_get_nl(value);
238 } else if (!strcmp(this_char,"creator") && value) {
239 if (strlen(value) != 4) {
242 hsb->s_creator = hfs_get_nl(value);
243 /* Boolean-valued options */
244 } else if (!strcmp(this_char,"quiet")) {
249 } else if (!strcmp(this_char,"afpd")) {
254 /* Multiple choice options */
255 } else if (!strcmp(this_char,"names") && value) {
256 if ((*value && !value[1] && strchr("ntal78c",*value)) ||
257 !strcmp(value,"netatalk") ||
258 !strcmp(value,"trivial") ||
259 !strcmp(value,"alpha") ||
260 !strcmp(value,"latin") ||
261 !strcmp(value,"7bit") ||
262 !strcmp(value,"8bit") ||
263 !strcmp(value,"cap")) {
268 } else if (!strcmp(this_char,"fork") && value) {
269 if ((*value && !value[1] && strchr("nsdc",*value)) ||
270 !strcmp(value,"netatalk") ||
271 !strcmp(value,"single") ||
272 !strcmp(value,"double") ||
273 !strcmp(value,"cap")) {
278 } else if (!strcmp(this_char,"case") && value) {
279 if ((*value && !value[1] && strchr("la",*value)) ||
280 !strcmp(value,"lower") ||
281 !strcmp(value,"asis")) {
282 hsb->s_lowercase = (*value == 'l');
286 } else if (!strcmp(this_char,"conv") && value) {
287 if ((*value && !value[1] && strchr("bta",*value)) ||
288 !strcmp(value,"binary") ||
289 !strcmp(value,"text") ||
290 !strcmp(value,"auto")) {
291 hsb->s_conv = *value;
301 /* Parse the "fork" and "names" options */
303 fork = hsb->s_afpd ? 'n' : 'c';
308 hsb->s_ifill = hfs_cap_ifill;
309 hsb->s_reserved1 = hfs_cap_reserved1;
310 hsb->s_reserved2 = hfs_cap_reserved2;
314 hfs_warn("hfs_fs: AppleSingle not yet implemented.\n");
319 hsb->s_ifill = hfs_dbl_ifill;
320 hsb->s_reserved1 = hfs_dbl_reserved1;
321 hsb->s_reserved2 = hfs_dbl_reserved2;
325 hsb->s_ifill = hfs_nat_ifill;
326 hsb->s_reserved1 = hfs_nat_reserved1;
327 hsb->s_reserved2 = hfs_nat_reserved2;
337 hsb->s_nameout = hfs_colon2mac;
338 hsb->s_namein = hfs_mac2nat;
342 hsb->s_nameout = hfs_colon2mac;
343 hsb->s_namein = hfs_mac2cap;
347 hsb->s_nameout = hfs_triv2mac;
348 hsb->s_namein = hfs_mac2triv;
352 hsb->s_nameout = hfs_prcnt2mac;
353 hsb->s_namein = hfs_mac2seven;
357 hsb->s_nameout = hfs_prcnt2mac;
358 hsb->s_namein = hfs_mac2eight;
362 hsb->s_nameout = hfs_latin2mac;
363 hsb->s_namein = hfs_mac2latin;
366 case 'a': /* 's' and 'd' are unadvertised aliases for 'alpha', */
367 case 's': /* since 'alpha' is the default if fork=s or fork=d. */
368 case 'd': /* (It is also helpful for poor typists!) */
369 hsb->s_nameout = hfs_prcnt2mac;
370 hsb->s_namein = hfs_mac2alpha;
377 /*================ Global functions ================*/
382 * This is the function that is responsible for mounting an HFS
383 * filesystem. It performs all the tasks necessary to get enough data
384 * from the disk to read the root inode. This includes parsing the
385 * mount options, dealing with Macintosh partitions, reading the
386 * superblock and the allocation bitmap blocks, calling
387 * hfs_btree_init() to get the necessary data about the extents and
388 * catalog B-trees and, finally, reading the root inode into memory.
390 struct super_block *hfs_read_super(struct super_block *s, void *data,
394 struct hfs_cat_key key;
395 kdev_t dev = s->s_dev;
396 hfs_s32 part_size, part_start;
397 struct inode *root_inode;
400 if (!parse_options((char *)data, HFS_SB(s), &part)) {
401 hfs_warn("hfs_fs: unable to parse mount options.\n");
405 /* set the device driver to 512-byte blocks */
406 set_blocksize(dev, HFS_SECTOR_SIZE);
407 s->s_blocksize_bits = HFS_SECTOR_SIZE_BITS;
408 s->s_blocksize = HFS_SECTOR_SIZE;
410 #ifdef CONFIG_MAC_PARTITION
411 /* check to see if we're in a partition */
412 mdb = hfs_mdb_get(s, s->s_flags & MS_RDONLY, 0);
414 /* erk. try parsing the partition table ourselves */
416 if (hfs_part_find(s, part, silent, &part_size, &part_start)) {
419 mdb = hfs_mdb_get(s, s->s_flags & MS_RDONLY, part_start);
422 if (hfs_part_find(s, part, silent, &part_size, &part_start)) {
426 mdb = hfs_mdb_get(s, s->s_flags & MS_RDONLY, part_start);
431 hfs_warn("VFS: Can't find a HFS filesystem on dev %s.\n",
437 HFS_SB(s)->s_mdb = mdb;
438 if (HFS_ITYPE(mdb->next_id) != 0) {
439 hfs_warn("hfs_fs: too many files.\n");
443 s->s_magic = HFS_SUPER_MAGIC;
444 s->s_op = &hfs_super_operations;
446 /* try to get the root inode */
447 hfs_cat_build_key(htonl(HFS_POR_CNID),
448 (struct hfs_name *)(mdb->vname), &key);
450 root_inode = hfs_iget(hfs_cat_get(mdb, &key), HFS_ITYPE_NORM, NULL);
454 s->s_root = d_alloc_root(root_inode);
458 /* fix up pointers. */
459 HFS_I(root_inode)->entry->sys_entry[HFS_ITYPE_TO_INT(HFS_ITYPE_NORM)] =
461 s->s_root->d_op = &hfs_dentry_operations;
463 /* everything's okay */
467 hfs_warn("hfs_fs: get root inode failed.\n");
470 hfs_mdb_put(mdb, s->s_flags & MS_RDONLY);
472 set_blocksize(dev, BLOCK_SIZE);
477 static int __init init_hfs_fs(void)
480 return register_filesystem(&hfs_fs);
483 static void __exit exit_hfs_fs(void) {
485 unregister_filesystem(&hfs_fs);
488 module_init(init_hfs_fs)
489 module_exit(exit_hfs_fs)
491 #if defined(DEBUG_ALL) || defined(DEBUG_MEM)
492 long int hfs_alloc = 0;