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