xref: /linux/fs/hfs/super.c (revision 5e8d780d745c1619aba81fe7166c5a4b5cad2b84)
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/mount.h>
19 #include <linux/init.h>
20 #include <linux/nls.h>
21 #include <linux/parser.h>
22 #include <linux/seq_file.h>
23 #include <linux/vfs.h>
24 
25 #include "hfs_fs.h"
26 #include "btree.h"
27 
28 static kmem_cache_t *hfs_inode_cachep;
29 
30 MODULE_LICENSE("GPL");
31 
32 /*
33  * hfs_write_super()
34  *
35  * Description:
36  *   This function is called by the VFS only. When the filesystem
37  *   is mounted r/w it updates the MDB on disk.
38  * Input Variable(s):
39  *   struct super_block *sb: Pointer to the hfs superblock
40  * Output Variable(s):
41  *   NONE
42  * Returns:
43  *   void
44  * Preconditions:
45  *   'sb' points to a "valid" (struct super_block).
46  * Postconditions:
47  *   The MDB is marked 'unsuccessfully unmounted' by clearing bit 8 of drAtrb
48  *   (hfs_put_super() must set this flag!). Some MDB fields are updated
49  *   and the MDB buffer is written to disk by calling hfs_mdb_commit().
50  */
51 static void hfs_write_super(struct super_block *sb)
52 {
53 	sb->s_dirt = 0;
54 	if (sb->s_flags & MS_RDONLY)
55 		return;
56 	/* sync everything to the buffers */
57 	hfs_mdb_commit(sb);
58 }
59 
60 /*
61  * hfs_put_super()
62  *
63  * This is the put_super() entry in the super_operations structure for
64  * HFS filesystems.  The purpose is to release the resources
65  * associated with the superblock sb.
66  */
67 static void hfs_put_super(struct super_block *sb)
68 {
69 	hfs_mdb_close(sb);
70 	/* release the MDB's resources */
71 	hfs_mdb_put(sb);
72 }
73 
74 /*
75  * hfs_statfs()
76  *
77  * This is the statfs() entry in the super_operations structure for
78  * HFS filesystems.  The purpose is to return various data about the
79  * filesystem.
80  *
81  * changed f_files/f_ffree to reflect the fs_ablock/free_ablocks.
82  */
83 static int hfs_statfs(struct dentry *dentry, struct kstatfs *buf)
84 {
85 	struct super_block *sb = dentry->d_sb;
86 
87 	buf->f_type = HFS_SUPER_MAGIC;
88 	buf->f_bsize = sb->s_blocksize;
89 	buf->f_blocks = (u32)HFS_SB(sb)->fs_ablocks * HFS_SB(sb)->fs_div;
90 	buf->f_bfree = (u32)HFS_SB(sb)->free_ablocks * HFS_SB(sb)->fs_div;
91 	buf->f_bavail = buf->f_bfree;
92 	buf->f_files = HFS_SB(sb)->fs_ablocks;
93 	buf->f_ffree = HFS_SB(sb)->free_ablocks;
94 	buf->f_namelen = HFS_NAMELEN;
95 
96 	return 0;
97 }
98 
99 static int hfs_remount(struct super_block *sb, int *flags, char *data)
100 {
101 	*flags |= MS_NODIRATIME;
102 	if ((*flags & MS_RDONLY) == (sb->s_flags & MS_RDONLY))
103 		return 0;
104 	if (!(*flags & MS_RDONLY)) {
105 		if (!(HFS_SB(sb)->mdb->drAtrb & cpu_to_be16(HFS_SB_ATTRIB_UNMNT))) {
106 			printk(KERN_WARNING "hfs: filesystem was not cleanly unmounted, "
107 			       "running fsck.hfs is recommended.  leaving read-only.\n");
108 			sb->s_flags |= MS_RDONLY;
109 			*flags |= MS_RDONLY;
110 		} else if (HFS_SB(sb)->mdb->drAtrb & cpu_to_be16(HFS_SB_ATTRIB_SLOCK)) {
111 			printk(KERN_WARNING "hfs: filesystem is marked locked, leaving read-only.\n");
112 			sb->s_flags |= MS_RDONLY;
113 			*flags |= MS_RDONLY;
114 		}
115 	}
116 	return 0;
117 }
118 
119 static int hfs_show_options(struct seq_file *seq, struct vfsmount *mnt)
120 {
121 	struct hfs_sb_info *sbi = HFS_SB(mnt->mnt_sb);
122 
123 	if (sbi->s_creator != cpu_to_be32(0x3f3f3f3f))
124 		seq_printf(seq, ",creator=%.4s", (char *)&sbi->s_creator);
125 	if (sbi->s_type != cpu_to_be32(0x3f3f3f3f))
126 		seq_printf(seq, ",type=%.4s", (char *)&sbi->s_type);
127 	seq_printf(seq, ",uid=%u,gid=%u", sbi->s_uid, sbi->s_gid);
128 	if (sbi->s_file_umask != 0133)
129 		seq_printf(seq, ",file_umask=%o", sbi->s_file_umask);
130 	if (sbi->s_dir_umask != 0022)
131 		seq_printf(seq, ",dir_umask=%o", sbi->s_dir_umask);
132 	if (sbi->part >= 0)
133 		seq_printf(seq, ",part=%u", sbi->part);
134 	if (sbi->session >= 0)
135 		seq_printf(seq, ",session=%u", sbi->session);
136 	if (sbi->nls_disk)
137 		seq_printf(seq, ",codepage=%s", sbi->nls_disk->charset);
138 	if (sbi->nls_io)
139 		seq_printf(seq, ",iocharset=%s", sbi->nls_io->charset);
140 	if (sbi->s_quiet)
141 		seq_printf(seq, ",quiet");
142 	return 0;
143 }
144 
145 static struct inode *hfs_alloc_inode(struct super_block *sb)
146 {
147 	struct hfs_inode_info *i;
148 
149 	i = kmem_cache_alloc(hfs_inode_cachep, SLAB_KERNEL);
150 	return i ? &i->vfs_inode : NULL;
151 }
152 
153 static void hfs_destroy_inode(struct inode *inode)
154 {
155 	kmem_cache_free(hfs_inode_cachep, HFS_I(inode));
156 }
157 
158 static struct super_operations hfs_super_operations = {
159 	.alloc_inode	= hfs_alloc_inode,
160 	.destroy_inode	= hfs_destroy_inode,
161 	.write_inode	= hfs_write_inode,
162 	.clear_inode	= hfs_clear_inode,
163 	.put_super	= hfs_put_super,
164 	.write_super	= hfs_write_super,
165 	.statfs		= hfs_statfs,
166 	.remount_fs     = hfs_remount,
167 	.show_options	= hfs_show_options,
168 };
169 
170 enum {
171 	opt_uid, opt_gid, opt_umask, opt_file_umask, opt_dir_umask,
172 	opt_part, opt_session, opt_type, opt_creator, opt_quiet,
173 	opt_codepage, opt_iocharset,
174 	opt_err
175 };
176 
177 static match_table_t tokens = {
178 	{ opt_uid, "uid=%u" },
179 	{ opt_gid, "gid=%u" },
180 	{ opt_umask, "umask=%o" },
181 	{ opt_file_umask, "file_umask=%o" },
182 	{ opt_dir_umask, "dir_umask=%o" },
183 	{ opt_part, "part=%u" },
184 	{ opt_session, "session=%u" },
185 	{ opt_type, "type=%s" },
186 	{ opt_creator, "creator=%s" },
187 	{ opt_quiet, "quiet" },
188 	{ opt_codepage, "codepage=%s" },
189 	{ opt_iocharset, "iocharset=%s" },
190 	{ opt_err, NULL }
191 };
192 
193 static inline int match_fourchar(substring_t *arg, u32 *result)
194 {
195 	if (arg->to - arg->from != 4)
196 		return -EINVAL;
197 	memcpy(result, arg->from, 4);
198 	return 0;
199 }
200 
201 /*
202  * parse_options()
203  *
204  * adapted from linux/fs/msdos/inode.c written 1992,93 by Werner Almesberger
205  * This function is called by hfs_read_super() to parse the mount options.
206  */
207 static int parse_options(char *options, struct hfs_sb_info *hsb)
208 {
209 	char *p;
210 	substring_t args[MAX_OPT_ARGS];
211 	int tmp, token;
212 
213 	/* initialize the sb with defaults */
214 	hsb->s_uid = current->uid;
215 	hsb->s_gid = current->gid;
216 	hsb->s_file_umask = 0133;
217 	hsb->s_dir_umask = 0022;
218 	hsb->s_type = hsb->s_creator = cpu_to_be32(0x3f3f3f3f);	/* == '????' */
219 	hsb->s_quiet = 0;
220 	hsb->part = -1;
221 	hsb->session = -1;
222 
223 	if (!options)
224 		return 1;
225 
226 	while ((p = strsep(&options, ",")) != NULL) {
227 		if (!*p)
228 			continue;
229 
230 		token = match_token(p, tokens, args);
231 		switch (token) {
232 		case opt_uid:
233 			if (match_int(&args[0], &tmp)) {
234 				printk(KERN_ERR "hfs: uid requires an argument\n");
235 				return 0;
236 			}
237 			hsb->s_uid = (uid_t)tmp;
238 			break;
239 		case opt_gid:
240 			if (match_int(&args[0], &tmp)) {
241 				printk(KERN_ERR "hfs: gid requires an argument\n");
242 				return 0;
243 			}
244 			hsb->s_gid = (gid_t)tmp;
245 			break;
246 		case opt_umask:
247 			if (match_octal(&args[0], &tmp)) {
248 				printk(KERN_ERR "hfs: umask requires a value\n");
249 				return 0;
250 			}
251 			hsb->s_file_umask = (umode_t)tmp;
252 			hsb->s_dir_umask = (umode_t)tmp;
253 			break;
254 		case opt_file_umask:
255 			if (match_octal(&args[0], &tmp)) {
256 				printk(KERN_ERR "hfs: file_umask requires a value\n");
257 				return 0;
258 			}
259 			hsb->s_file_umask = (umode_t)tmp;
260 			break;
261 		case opt_dir_umask:
262 			if (match_octal(&args[0], &tmp)) {
263 				printk(KERN_ERR "hfs: dir_umask requires a value\n");
264 				return 0;
265 			}
266 			hsb->s_dir_umask = (umode_t)tmp;
267 			break;
268 		case opt_part:
269 			if (match_int(&args[0], &hsb->part)) {
270 				printk(KERN_ERR "hfs: part requires an argument\n");
271 				return 0;
272 			}
273 			break;
274 		case opt_session:
275 			if (match_int(&args[0], &hsb->session)) {
276 				printk(KERN_ERR "hfs: session requires an argument\n");
277 				return 0;
278 			}
279 			break;
280 		case opt_type:
281 			if (match_fourchar(&args[0], &hsb->s_type)) {
282 				printk(KERN_ERR "hfs: type requires a 4 character value\n");
283 				return 0;
284 			}
285 			break;
286 		case opt_creator:
287 			if (match_fourchar(&args[0], &hsb->s_creator)) {
288 				printk(KERN_ERR "hfs: creator requires a 4 character value\n");
289 				return 0;
290 			}
291 			break;
292 		case opt_quiet:
293 			hsb->s_quiet = 1;
294 			break;
295 		case opt_codepage:
296 			if (hsb->nls_disk) {
297 				printk(KERN_ERR "hfs: unable to change codepage\n");
298 				return 0;
299 			}
300 			p = match_strdup(&args[0]);
301 			hsb->nls_disk = load_nls(p);
302 			if (!hsb->nls_disk) {
303 				printk(KERN_ERR "hfs: unable to load codepage \"%s\"\n", p);
304 				kfree(p);
305 				return 0;
306 			}
307 			kfree(p);
308 			break;
309 		case opt_iocharset:
310 			if (hsb->nls_io) {
311 				printk(KERN_ERR "hfs: unable to change iocharset\n");
312 				return 0;
313 			}
314 			p = match_strdup(&args[0]);
315 			hsb->nls_io = load_nls(p);
316 			if (!hsb->nls_io) {
317 				printk(KERN_ERR "hfs: unable to load iocharset \"%s\"\n", p);
318 				kfree(p);
319 				return 0;
320 			}
321 			kfree(p);
322 			break;
323 		default:
324 			return 0;
325 		}
326 	}
327 
328 	if (hsb->nls_disk && !hsb->nls_io) {
329 		hsb->nls_io = load_nls_default();
330 		if (!hsb->nls_io) {
331 			printk(KERN_ERR "hfs: unable to load default iocharset\n");
332 			return 0;
333 		}
334 	}
335 	hsb->s_dir_umask &= 0777;
336 	hsb->s_file_umask &= 0577;
337 
338 	return 1;
339 }
340 
341 /*
342  * hfs_read_super()
343  *
344  * This is the function that is responsible for mounting an HFS
345  * filesystem.	It performs all the tasks necessary to get enough data
346  * from the disk to read the root inode.  This includes parsing the
347  * mount options, dealing with Macintosh partitions, reading the
348  * superblock and the allocation bitmap blocks, calling
349  * hfs_btree_init() to get the necessary data about the extents and
350  * catalog B-trees and, finally, reading the root inode into memory.
351  */
352 static int hfs_fill_super(struct super_block *sb, void *data, int silent)
353 {
354 	struct hfs_sb_info *sbi;
355 	struct hfs_find_data fd;
356 	hfs_cat_rec rec;
357 	struct inode *root_inode;
358 	int res;
359 
360 	sbi = kmalloc(sizeof(struct hfs_sb_info), GFP_KERNEL);
361 	if (!sbi)
362 		return -ENOMEM;
363 	sb->s_fs_info = sbi;
364 	memset(sbi, 0, sizeof(struct hfs_sb_info));
365 	INIT_HLIST_HEAD(&sbi->rsrc_inodes);
366 
367 	res = -EINVAL;
368 	if (!parse_options((char *)data, sbi)) {
369 		printk(KERN_ERR "hfs: unable to parse mount options.\n");
370 		goto bail;
371 	}
372 
373 	sb->s_op = &hfs_super_operations;
374 	sb->s_flags |= MS_NODIRATIME;
375 	init_MUTEX(&sbi->bitmap_lock);
376 
377 	res = hfs_mdb_get(sb);
378 	if (res) {
379 		if (!silent)
380 			printk(KERN_WARNING "hfs: can't find a HFS filesystem on dev %s.\n",
381 				hfs_mdb_name(sb));
382 		res = -EINVAL;
383 		goto bail;
384 	}
385 
386 	/* try to get the root inode */
387 	hfs_find_init(HFS_SB(sb)->cat_tree, &fd);
388 	res = hfs_cat_find_brec(sb, HFS_ROOT_CNID, &fd);
389 	if (!res)
390 		hfs_bnode_read(fd.bnode, &rec, fd.entryoffset, fd.entrylength);
391 	if (res) {
392 		hfs_find_exit(&fd);
393 		goto bail_no_root;
394 	}
395 	root_inode = hfs_iget(sb, &fd.search_key->cat, &rec);
396 	hfs_find_exit(&fd);
397 	if (!root_inode)
398 		goto bail_no_root;
399 
400 	sb->s_root = d_alloc_root(root_inode);
401 	if (!sb->s_root)
402 		goto bail_iput;
403 
404 	sb->s_root->d_op = &hfs_dentry_operations;
405 
406 	/* everything's okay */
407 	return 0;
408 
409 bail_iput:
410 	iput(root_inode);
411 bail_no_root:
412 	printk(KERN_ERR "hfs: get root inode failed.\n");
413 bail:
414 	hfs_mdb_put(sb);
415 	return res;
416 }
417 
418 static int hfs_get_sb(struct file_system_type *fs_type,
419 		      int flags, const char *dev_name, void *data,
420 		      struct vfsmount *mnt)
421 {
422 	return get_sb_bdev(fs_type, flags, dev_name, data, hfs_fill_super, mnt);
423 }
424 
425 static struct file_system_type hfs_fs_type = {
426 	.owner		= THIS_MODULE,
427 	.name		= "hfs",
428 	.get_sb		= hfs_get_sb,
429 	.kill_sb	= kill_block_super,
430 	.fs_flags	= FS_REQUIRES_DEV,
431 };
432 
433 static void hfs_init_once(void *p, kmem_cache_t *cachep, unsigned long flags)
434 {
435 	struct hfs_inode_info *i = p;
436 
437 	if ((flags & (SLAB_CTOR_VERIFY|SLAB_CTOR_CONSTRUCTOR)) == SLAB_CTOR_CONSTRUCTOR)
438 		inode_init_once(&i->vfs_inode);
439 }
440 
441 static int __init init_hfs_fs(void)
442 {
443 	int err;
444 
445 	hfs_inode_cachep = kmem_cache_create("hfs_inode_cache",
446 		sizeof(struct hfs_inode_info), 0, SLAB_HWCACHE_ALIGN,
447 		hfs_init_once, NULL);
448 	if (!hfs_inode_cachep)
449 		return -ENOMEM;
450 	err = register_filesystem(&hfs_fs_type);
451 	if (err)
452 		kmem_cache_destroy(hfs_inode_cachep);
453 	return err;
454 }
455 
456 static void __exit exit_hfs_fs(void)
457 {
458 	unregister_filesystem(&hfs_fs_type);
459 	if (kmem_cache_destroy(hfs_inode_cachep))
460 		printk(KERN_ERR "hfs_inode_cache: not all structures were freed\n");
461 }
462 
463 module_init(init_hfs_fs)
464 module_exit(exit_hfs_fs)
465