ftp://ftp.kernel.org/pub/linux/kernel/v2.6/linux-2.6.6.tar.bz2
[linux-2.6.git] / fs / hfs / super.c
1 /*
2  *  linux/fs/hfs/super.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 hfs_read_super(), some of the super_ops and
9  * init_module() and cleanup_module().  The remaining super_ops are in
10  * inode.c since they deal with inodes.
11  *
12  * Based on the minix file system code, (C) 1991, 1992 by Linus Torvalds
13  */
14
15 #include <linux/config.h>
16 #include <linux/module.h>
17 #include <linux/blkdev.h>
18 #include <linux/init.h>
19 #include <linux/vfs.h>
20
21 #include "hfs_fs.h"
22 #include "btree.h"
23
24 const char hfs_version[]="0.96";
25
26 static kmem_cache_t *hfs_inode_cachep;
27
28 MODULE_LICENSE("GPL");
29
30 /*
31  * hfs_write_super()
32  *
33  * Description:
34  *   This function is called by the VFS only. When the filesystem
35  *   is mounted r/w it updates the MDB on disk.
36  * Input Variable(s):
37  *   struct super_block *sb: Pointer to the hfs superblock
38  * Output Variable(s):
39  *   NONE
40  * Returns:
41  *   void
42  * Preconditions:
43  *   'sb' points to a "valid" (struct super_block).
44  * Postconditions:
45  *   The MDB is marked 'unsuccessfully unmounted' by clearing bit 8 of drAtrb
46  *   (hfs_put_super() must set this flag!). Some MDB fields are updated
47  *   and the MDB buffer is written to disk by calling hfs_mdb_commit().
48  */
49 static void hfs_write_super(struct super_block *sb)
50 {
51         sb->s_dirt = 0;
52         if (sb->s_flags & MS_RDONLY)
53                 return;
54         /* sync everything to the buffers */
55         hfs_mdb_commit(sb);
56 }
57
58 /*
59  * hfs_put_super()
60  *
61  * This is the put_super() entry in the super_operations structure for
62  * HFS filesystems.  The purpose is to release the resources
63  * associated with the superblock sb.
64  */
65 static void hfs_put_super(struct super_block *sb)
66 {
67         hfs_mdb_close(sb);
68         /* release the MDB's resources */
69         hfs_mdb_put(sb);
70 }
71
72 /*
73  * hfs_statfs()
74  *
75  * This is the statfs() entry in the super_operations structure for
76  * HFS filesystems.  The purpose is to return various data about the
77  * filesystem.
78  *
79  * changed f_files/f_ffree to reflect the fs_ablock/free_ablocks.
80  */
81 static int hfs_statfs(struct super_block *sb, struct kstatfs *buf)
82 {
83         buf->f_type = HFS_SUPER_MAGIC;
84         buf->f_bsize = sb->s_blocksize;
85         buf->f_blocks = (u32)HFS_SB(sb)->fs_ablocks * HFS_SB(sb)->fs_div;
86         buf->f_bfree = (u32)HFS_SB(sb)->free_ablocks * HFS_SB(sb)->fs_div;
87         buf->f_bavail = buf->f_bfree;
88         buf->f_files = HFS_SB(sb)->fs_ablocks;
89         buf->f_ffree = HFS_SB(sb)->free_ablocks;
90         buf->f_namelen = HFS_NAMELEN;
91
92         return 0;
93 }
94
95 int hfs_remount(struct super_block *sb, int *flags, char *data)
96 {
97         *flags |= MS_NODIRATIME;
98         if ((*flags & MS_RDONLY) == (sb->s_flags & MS_RDONLY))
99                 return 0;
100         if (!(*flags & MS_RDONLY)) {
101                 if (!(HFS_SB(sb)->mdb->drAtrb & cpu_to_be16(HFS_SB_ATTRIB_UNMNT))
102                     || (HFS_SB(sb)->mdb->drAtrb & cpu_to_be16(HFS_SB_ATTRIB_INCNSTNT))) {
103                         printk("HFS-fs warning: Filesystem was not cleanly unmounted, "
104                                "running fsck.hfs is recommended.  leaving read-only.\n");
105                         sb->s_flags |= MS_RDONLY;
106                         *flags |= MS_RDONLY;
107                 } else if (HFS_SB(sb)->mdb->drAtrb & cpu_to_be16(HFS_SB_ATTRIB_SLOCK)) {
108                         printk("HFS-fs: Filesystem is marked locked, leaving read-only.\n");
109                         sb->s_flags |= MS_RDONLY;
110                         *flags |= MS_RDONLY;
111                 }
112         }
113         return 0;
114 }
115
116 static struct inode *hfs_alloc_inode(struct super_block *sb)
117 {
118         struct hfs_inode_info *i;
119
120         i = kmem_cache_alloc(hfs_inode_cachep, SLAB_KERNEL);
121         return i ? &i->vfs_inode : NULL;
122 }
123
124 static void hfs_destroy_inode(struct inode *inode)
125 {
126         kmem_cache_free(hfs_inode_cachep, HFS_I(inode));
127 }
128
129 static struct super_operations hfs_super_operations = {
130         .alloc_inode    = hfs_alloc_inode,
131         .destroy_inode  = hfs_destroy_inode,
132         .write_inode    = hfs_write_inode,
133         .clear_inode    = hfs_clear_inode,
134         .put_super      = hfs_put_super,
135         .write_super    = hfs_write_super,
136         .statfs         = hfs_statfs,
137         .remount_fs     = hfs_remount,
138 };
139
140 /*
141  * parse_options()
142  *
143  * adapted from linux/fs/msdos/inode.c written 1992,93 by Werner Almesberger
144  * This function is called by hfs_read_super() to parse the mount options.
145  */
146 static int parse_options(char *options, struct hfs_sb_info *hsb)
147 {
148         char *this_char, *value;
149
150         /* initialize the sb with defaults */
151         hsb->s_uid = current->uid;
152         hsb->s_gid = current->gid;
153         hsb->s_file_umask = 0644;
154         hsb->s_dir_umask = 0755;
155         hsb->s_type = 0x3f3f3f3f;       /* == '????' */
156         hsb->s_creator = 0x3f3f3f3f;    /* == '????' */
157         hsb->s_quiet = 0;
158         hsb->part = -1;
159         hsb->session = -1;
160
161         if (!options)
162                 return 1;
163
164         while ((this_char = strsep(&options, ","))) {
165                 if (!*this_char)
166                         continue;
167                 value = strchr(this_char, '=');
168                 if (value)
169                         *value++ = 0;
170
171         /* Numeric-valued options */
172                 if (!strcmp(this_char, "uid")) {
173                         if (!value || !*value)
174                                 return 0;
175                         hsb->s_uid = simple_strtoul(value, &value, 0);
176                         if (*value)
177                                 return 0;
178                 } else if (!strcmp(this_char, "gid")) {
179                         if (!value || !*value)
180                                 return 0;
181                         hsb->s_gid = simple_strtoul(value, &value, 0);
182                         if (*value)
183                                 return 0;
184                 } else if (!strcmp(this_char, "umask")) {
185                         if (!value || !*value)
186                                 return 0;
187                         hsb->s_file_umask = simple_strtoul(value, &value, 8);
188                         hsb->s_dir_umask = hsb->s_file_umask;
189                         if (*value)
190                                 return 0;
191                 } else if (!strcmp(this_char, "file_umask")) {
192                         if (!value || !*value)
193                                 return 0;
194                         hsb->s_file_umask = simple_strtoul(value, &value, 8);
195                         if (*value)
196                                 return 0;
197                 } else if (!strcmp(this_char, "dir_umask")) {
198                         if (!value || !*value)
199                                 return 0;
200                         hsb->s_dir_umask = simple_strtoul(value, &value, 8);
201                         if (*value)
202                                 return 0;
203                 } else if (!strcmp(this_char, "part")) {
204                         if (!value || !*value)
205                                 return 0;
206                         hsb->part = simple_strtoul(value, &value, 0);
207                         if (*value)
208                                 return 0;
209                 } else if (!strcmp(this_char, "session")) {
210                         if (!value || !*value)
211                                 return 0;
212                         hsb->session = simple_strtoul(value, &value, 0);
213                         if (*value)
214                                 return 0;
215         /* String-valued options */
216                 } else if (!strcmp(this_char, "type") && value) {
217                         if (strlen(value) != 4)
218                                 return 0;
219                         hsb->s_type = *(u32 *)value;
220                 } else if (!strcmp(this_char, "creator") && value) {
221                         if (strlen(value) != 4)
222                                 return 0;
223                         hsb->s_creator = *(u32 *)value;
224         /* Boolean-valued options */
225                 } else if (!strcmp(this_char, "quiet")) {
226                         if (value)
227                                 return 0;
228                         hsb->s_quiet = 1;
229                 } else
230                         return 0;
231         }
232
233         hsb->s_dir_umask &= 0777;
234         hsb->s_file_umask &= 0777;
235
236         return 1;
237 }
238
239 /*
240  * hfs_read_super()
241  *
242  * This is the function that is responsible for mounting an HFS
243  * filesystem.  It performs all the tasks necessary to get enough data
244  * from the disk to read the root inode.  This includes parsing the
245  * mount options, dealing with Macintosh partitions, reading the
246  * superblock and the allocation bitmap blocks, calling
247  * hfs_btree_init() to get the necessary data about the extents and
248  * catalog B-trees and, finally, reading the root inode into memory.
249  */
250 static int hfs_fill_super(struct super_block *sb, void *data, int silent)
251 {
252         struct hfs_sb_info *sbi;
253         struct hfs_find_data fd;
254         hfs_cat_rec rec;
255         struct inode *root_inode;
256         int res;
257
258         sbi = kmalloc(sizeof(struct hfs_sb_info), GFP_KERNEL);
259         if (!sbi)
260                 return -ENOMEM;
261         sb->s_fs_info = sbi;
262         memset(sbi, 0, sizeof(struct hfs_sb_info));
263         INIT_HLIST_HEAD(&sbi->rsrc_inodes);
264
265         res = -EINVAL;
266         if (!parse_options((char *)data, sbi)) {
267                 hfs_warn("hfs_fs: unable to parse mount options.\n");
268                 goto bail3;
269         }
270
271         sb->s_op = &hfs_super_operations;
272         sb->s_flags |= MS_NODIRATIME;
273         init_MUTEX(&sbi->bitmap_lock);
274
275         res = hfs_mdb_get(sb);
276         if (res) {
277                 if (!silent)
278                         hfs_warn("VFS: Can't find a HFS filesystem on dev %s.\n",
279                                 hfs_mdb_name(sb));
280                 res = -EINVAL;
281                 goto bail2;
282         }
283
284         /* try to get the root inode */
285         hfs_find_init(HFS_SB(sb)->cat_tree, &fd);
286         res = hfs_cat_find_brec(sb, HFS_ROOT_CNID, &fd);
287         if (!res)
288                 hfs_bnode_read(fd.bnode, &rec, fd.entryoffset, fd.entrylength);
289         if (res) {
290                 hfs_find_exit(&fd);
291                 goto bail_no_root;
292         }
293         root_inode = hfs_iget(sb, &fd.search_key->cat, &rec);
294         hfs_find_exit(&fd);
295         if (!root_inode)
296                 goto bail_no_root;
297
298         sb->s_root = d_alloc_root(root_inode);
299         if (!sb->s_root)
300                 goto bail_iput;
301
302         sb->s_root->d_op = &hfs_dentry_operations;
303
304         /* everything's okay */
305         return 0;
306
307 bail_iput:
308         iput(root_inode);
309 bail_no_root:
310         hfs_warn("hfs_fs: get root inode failed.\n");
311         hfs_mdb_put(sb);
312 bail2:
313 bail3:
314         kfree(sbi);
315         return res;
316 }
317
318 static struct super_block *hfs_get_sb(struct file_system_type *fs_type,
319                                       int flags, const char *dev_name, void *data)
320 {
321         return get_sb_bdev(fs_type, flags, dev_name, data, hfs_fill_super);
322 }
323
324 static struct file_system_type hfs_fs_type = {
325         .owner          = THIS_MODULE,
326         .name           = "hfs",
327         .get_sb         = hfs_get_sb,
328         .kill_sb        = kill_block_super,
329         .fs_flags       = FS_REQUIRES_DEV,
330 };
331
332 static void hfs_init_once(void *p, kmem_cache_t *cachep, unsigned long flags)
333 {
334         struct hfs_inode_info *i = p;
335
336         if ((flags & (SLAB_CTOR_VERIFY|SLAB_CTOR_CONSTRUCTOR)) == SLAB_CTOR_CONSTRUCTOR)
337                 inode_init_once(&i->vfs_inode);
338 }
339
340 static int __init init_hfs_fs(void)
341 {
342         int err;
343
344         hfs_inode_cachep = kmem_cache_create("hfs_inode_cache",
345                 sizeof(struct hfs_inode_info), 0, SLAB_HWCACHE_ALIGN,
346                 hfs_init_once, NULL);
347         if (!hfs_inode_cachep)
348                 return -ENOMEM;
349         err = register_filesystem(&hfs_fs_type);
350         if (err)
351                 kmem_cache_destroy(hfs_inode_cachep);
352         return err;
353 }
354
355 static void __exit exit_hfs_fs(void)
356 {
357         unregister_filesystem(&hfs_fs_type);
358         if (kmem_cache_destroy(hfs_inode_cachep))
359                 printk(KERN_INFO "hfs_inode_cache: not all structures were freed\n");
360 }
361
362 module_init(init_hfs_fs)
363 module_exit(exit_hfs_fs)