0001
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013
0014
0015 #include <linux/module.h>
0016 #include <linux/blkdev.h>
0017 #include <linux/backing-dev.h>
0018 #include <linux/mount.h>
0019 #include <linux/init.h>
0020 #include <linux/nls.h>
0021 #include <linux/parser.h>
0022 #include <linux/seq_file.h>
0023 #include <linux/slab.h>
0024 #include <linux/vfs.h>
0025
0026 #include "hfs_fs.h"
0027 #include "btree.h"
0028
0029 static struct kmem_cache *hfs_inode_cachep;
0030
0031 MODULE_LICENSE("GPL");
0032
0033 static int hfs_sync_fs(struct super_block *sb, int wait)
0034 {
0035 hfs_mdb_commit(sb);
0036 return 0;
0037 }
0038
0039
0040
0041
0042
0043
0044
0045
0046 static void hfs_put_super(struct super_block *sb)
0047 {
0048 cancel_delayed_work_sync(&HFS_SB(sb)->mdb_work);
0049 hfs_mdb_close(sb);
0050
0051 hfs_mdb_put(sb);
0052 }
0053
0054 static void flush_mdb(struct work_struct *work)
0055 {
0056 struct hfs_sb_info *sbi;
0057 struct super_block *sb;
0058
0059 sbi = container_of(work, struct hfs_sb_info, mdb_work.work);
0060 sb = sbi->sb;
0061
0062 spin_lock(&sbi->work_lock);
0063 sbi->work_queued = 0;
0064 spin_unlock(&sbi->work_lock);
0065
0066 hfs_mdb_commit(sb);
0067 }
0068
0069 void hfs_mark_mdb_dirty(struct super_block *sb)
0070 {
0071 struct hfs_sb_info *sbi = HFS_SB(sb);
0072 unsigned long delay;
0073
0074 if (sb_rdonly(sb))
0075 return;
0076
0077 spin_lock(&sbi->work_lock);
0078 if (!sbi->work_queued) {
0079 delay = msecs_to_jiffies(dirty_writeback_interval * 10);
0080 queue_delayed_work(system_long_wq, &sbi->mdb_work, delay);
0081 sbi->work_queued = 1;
0082 }
0083 spin_unlock(&sbi->work_lock);
0084 }
0085
0086
0087
0088
0089
0090
0091
0092
0093
0094
0095 static int hfs_statfs(struct dentry *dentry, struct kstatfs *buf)
0096 {
0097 struct super_block *sb = dentry->d_sb;
0098 u64 id = huge_encode_dev(sb->s_bdev->bd_dev);
0099
0100 buf->f_type = HFS_SUPER_MAGIC;
0101 buf->f_bsize = sb->s_blocksize;
0102 buf->f_blocks = (u32)HFS_SB(sb)->fs_ablocks * HFS_SB(sb)->fs_div;
0103 buf->f_bfree = (u32)HFS_SB(sb)->free_ablocks * HFS_SB(sb)->fs_div;
0104 buf->f_bavail = buf->f_bfree;
0105 buf->f_files = HFS_SB(sb)->fs_ablocks;
0106 buf->f_ffree = HFS_SB(sb)->free_ablocks;
0107 buf->f_fsid = u64_to_fsid(id);
0108 buf->f_namelen = HFS_NAMELEN;
0109
0110 return 0;
0111 }
0112
0113 static int hfs_remount(struct super_block *sb, int *flags, char *data)
0114 {
0115 sync_filesystem(sb);
0116 *flags |= SB_NODIRATIME;
0117 if ((bool)(*flags & SB_RDONLY) == sb_rdonly(sb))
0118 return 0;
0119 if (!(*flags & SB_RDONLY)) {
0120 if (!(HFS_SB(sb)->mdb->drAtrb & cpu_to_be16(HFS_SB_ATTRIB_UNMNT))) {
0121 pr_warn("filesystem was not cleanly unmounted, running fsck.hfs is recommended. leaving read-only.\n");
0122 sb->s_flags |= SB_RDONLY;
0123 *flags |= SB_RDONLY;
0124 } else if (HFS_SB(sb)->mdb->drAtrb & cpu_to_be16(HFS_SB_ATTRIB_SLOCK)) {
0125 pr_warn("filesystem is marked locked, leaving read-only.\n");
0126 sb->s_flags |= SB_RDONLY;
0127 *flags |= SB_RDONLY;
0128 }
0129 }
0130 return 0;
0131 }
0132
0133 static int hfs_show_options(struct seq_file *seq, struct dentry *root)
0134 {
0135 struct hfs_sb_info *sbi = HFS_SB(root->d_sb);
0136
0137 if (sbi->s_creator != cpu_to_be32(0x3f3f3f3f))
0138 seq_show_option_n(seq, "creator", (char *)&sbi->s_creator, 4);
0139 if (sbi->s_type != cpu_to_be32(0x3f3f3f3f))
0140 seq_show_option_n(seq, "type", (char *)&sbi->s_type, 4);
0141 seq_printf(seq, ",uid=%u,gid=%u",
0142 from_kuid_munged(&init_user_ns, sbi->s_uid),
0143 from_kgid_munged(&init_user_ns, sbi->s_gid));
0144 if (sbi->s_file_umask != 0133)
0145 seq_printf(seq, ",file_umask=%o", sbi->s_file_umask);
0146 if (sbi->s_dir_umask != 0022)
0147 seq_printf(seq, ",dir_umask=%o", sbi->s_dir_umask);
0148 if (sbi->part >= 0)
0149 seq_printf(seq, ",part=%u", sbi->part);
0150 if (sbi->session >= 0)
0151 seq_printf(seq, ",session=%u", sbi->session);
0152 if (sbi->nls_disk)
0153 seq_printf(seq, ",codepage=%s", sbi->nls_disk->charset);
0154 if (sbi->nls_io)
0155 seq_printf(seq, ",iocharset=%s", sbi->nls_io->charset);
0156 if (sbi->s_quiet)
0157 seq_printf(seq, ",quiet");
0158 return 0;
0159 }
0160
0161 static struct inode *hfs_alloc_inode(struct super_block *sb)
0162 {
0163 struct hfs_inode_info *i;
0164
0165 i = alloc_inode_sb(sb, hfs_inode_cachep, GFP_KERNEL);
0166 return i ? &i->vfs_inode : NULL;
0167 }
0168
0169 static void hfs_free_inode(struct inode *inode)
0170 {
0171 kmem_cache_free(hfs_inode_cachep, HFS_I(inode));
0172 }
0173
0174 static const struct super_operations hfs_super_operations = {
0175 .alloc_inode = hfs_alloc_inode,
0176 .free_inode = hfs_free_inode,
0177 .write_inode = hfs_write_inode,
0178 .evict_inode = hfs_evict_inode,
0179 .put_super = hfs_put_super,
0180 .sync_fs = hfs_sync_fs,
0181 .statfs = hfs_statfs,
0182 .remount_fs = hfs_remount,
0183 .show_options = hfs_show_options,
0184 };
0185
0186 enum {
0187 opt_uid, opt_gid, opt_umask, opt_file_umask, opt_dir_umask,
0188 opt_part, opt_session, opt_type, opt_creator, opt_quiet,
0189 opt_codepage, opt_iocharset,
0190 opt_err
0191 };
0192
0193 static const match_table_t tokens = {
0194 { opt_uid, "uid=%u" },
0195 { opt_gid, "gid=%u" },
0196 { opt_umask, "umask=%o" },
0197 { opt_file_umask, "file_umask=%o" },
0198 { opt_dir_umask, "dir_umask=%o" },
0199 { opt_part, "part=%u" },
0200 { opt_session, "session=%u" },
0201 { opt_type, "type=%s" },
0202 { opt_creator, "creator=%s" },
0203 { opt_quiet, "quiet" },
0204 { opt_codepage, "codepage=%s" },
0205 { opt_iocharset, "iocharset=%s" },
0206 { opt_err, NULL }
0207 };
0208
0209 static inline int match_fourchar(substring_t *arg, u32 *result)
0210 {
0211 if (arg->to - arg->from != 4)
0212 return -EINVAL;
0213 memcpy(result, arg->from, 4);
0214 return 0;
0215 }
0216
0217
0218
0219
0220
0221
0222
0223 static int parse_options(char *options, struct hfs_sb_info *hsb)
0224 {
0225 char *p;
0226 substring_t args[MAX_OPT_ARGS];
0227 int tmp, token;
0228
0229
0230 hsb->s_uid = current_uid();
0231 hsb->s_gid = current_gid();
0232 hsb->s_file_umask = 0133;
0233 hsb->s_dir_umask = 0022;
0234 hsb->s_type = hsb->s_creator = cpu_to_be32(0x3f3f3f3f);
0235 hsb->s_quiet = 0;
0236 hsb->part = -1;
0237 hsb->session = -1;
0238
0239 if (!options)
0240 return 1;
0241
0242 while ((p = strsep(&options, ",")) != NULL) {
0243 if (!*p)
0244 continue;
0245
0246 token = match_token(p, tokens, args);
0247 switch (token) {
0248 case opt_uid:
0249 if (match_int(&args[0], &tmp)) {
0250 pr_err("uid requires an argument\n");
0251 return 0;
0252 }
0253 hsb->s_uid = make_kuid(current_user_ns(), (uid_t)tmp);
0254 if (!uid_valid(hsb->s_uid)) {
0255 pr_err("invalid uid %d\n", tmp);
0256 return 0;
0257 }
0258 break;
0259 case opt_gid:
0260 if (match_int(&args[0], &tmp)) {
0261 pr_err("gid requires an argument\n");
0262 return 0;
0263 }
0264 hsb->s_gid = make_kgid(current_user_ns(), (gid_t)tmp);
0265 if (!gid_valid(hsb->s_gid)) {
0266 pr_err("invalid gid %d\n", tmp);
0267 return 0;
0268 }
0269 break;
0270 case opt_umask:
0271 if (match_octal(&args[0], &tmp)) {
0272 pr_err("umask requires a value\n");
0273 return 0;
0274 }
0275 hsb->s_file_umask = (umode_t)tmp;
0276 hsb->s_dir_umask = (umode_t)tmp;
0277 break;
0278 case opt_file_umask:
0279 if (match_octal(&args[0], &tmp)) {
0280 pr_err("file_umask requires a value\n");
0281 return 0;
0282 }
0283 hsb->s_file_umask = (umode_t)tmp;
0284 break;
0285 case opt_dir_umask:
0286 if (match_octal(&args[0], &tmp)) {
0287 pr_err("dir_umask requires a value\n");
0288 return 0;
0289 }
0290 hsb->s_dir_umask = (umode_t)tmp;
0291 break;
0292 case opt_part:
0293 if (match_int(&args[0], &hsb->part)) {
0294 pr_err("part requires an argument\n");
0295 return 0;
0296 }
0297 break;
0298 case opt_session:
0299 if (match_int(&args[0], &hsb->session)) {
0300 pr_err("session requires an argument\n");
0301 return 0;
0302 }
0303 break;
0304 case opt_type:
0305 if (match_fourchar(&args[0], &hsb->s_type)) {
0306 pr_err("type requires a 4 character value\n");
0307 return 0;
0308 }
0309 break;
0310 case opt_creator:
0311 if (match_fourchar(&args[0], &hsb->s_creator)) {
0312 pr_err("creator requires a 4 character value\n");
0313 return 0;
0314 }
0315 break;
0316 case opt_quiet:
0317 hsb->s_quiet = 1;
0318 break;
0319 case opt_codepage:
0320 if (hsb->nls_disk) {
0321 pr_err("unable to change codepage\n");
0322 return 0;
0323 }
0324 p = match_strdup(&args[0]);
0325 if (p)
0326 hsb->nls_disk = load_nls(p);
0327 if (!hsb->nls_disk) {
0328 pr_err("unable to load codepage \"%s\"\n", p);
0329 kfree(p);
0330 return 0;
0331 }
0332 kfree(p);
0333 break;
0334 case opt_iocharset:
0335 if (hsb->nls_io) {
0336 pr_err("unable to change iocharset\n");
0337 return 0;
0338 }
0339 p = match_strdup(&args[0]);
0340 if (p)
0341 hsb->nls_io = load_nls(p);
0342 if (!hsb->nls_io) {
0343 pr_err("unable to load iocharset \"%s\"\n", p);
0344 kfree(p);
0345 return 0;
0346 }
0347 kfree(p);
0348 break;
0349 default:
0350 return 0;
0351 }
0352 }
0353
0354 if (hsb->nls_disk && !hsb->nls_io) {
0355 hsb->nls_io = load_nls_default();
0356 if (!hsb->nls_io) {
0357 pr_err("unable to load default iocharset\n");
0358 return 0;
0359 }
0360 }
0361 hsb->s_dir_umask &= 0777;
0362 hsb->s_file_umask &= 0577;
0363
0364 return 1;
0365 }
0366
0367
0368
0369
0370
0371
0372
0373
0374
0375
0376
0377
0378 static int hfs_fill_super(struct super_block *sb, void *data, int silent)
0379 {
0380 struct hfs_sb_info *sbi;
0381 struct hfs_find_data fd;
0382 hfs_cat_rec rec;
0383 struct inode *root_inode;
0384 int res;
0385
0386 sbi = kzalloc(sizeof(struct hfs_sb_info), GFP_KERNEL);
0387 if (!sbi)
0388 return -ENOMEM;
0389
0390 sbi->sb = sb;
0391 sb->s_fs_info = sbi;
0392 spin_lock_init(&sbi->work_lock);
0393 INIT_DELAYED_WORK(&sbi->mdb_work, flush_mdb);
0394
0395 res = -EINVAL;
0396 if (!parse_options((char *)data, sbi)) {
0397 pr_err("unable to parse mount options\n");
0398 goto bail;
0399 }
0400
0401 sb->s_op = &hfs_super_operations;
0402 sb->s_xattr = hfs_xattr_handlers;
0403 sb->s_flags |= SB_NODIRATIME;
0404 mutex_init(&sbi->bitmap_lock);
0405
0406 res = hfs_mdb_get(sb);
0407 if (res) {
0408 if (!silent)
0409 pr_warn("can't find a HFS filesystem on dev %s\n",
0410 hfs_mdb_name(sb));
0411 res = -EINVAL;
0412 goto bail;
0413 }
0414
0415
0416 res = hfs_find_init(HFS_SB(sb)->cat_tree, &fd);
0417 if (res)
0418 goto bail_no_root;
0419 res = hfs_cat_find_brec(sb, HFS_ROOT_CNID, &fd);
0420 if (!res) {
0421 if (fd.entrylength > sizeof(rec) || fd.entrylength < 0) {
0422 res = -EIO;
0423 goto bail_hfs_find;
0424 }
0425 hfs_bnode_read(fd.bnode, &rec, fd.entryoffset, fd.entrylength);
0426 }
0427 if (res)
0428 goto bail_hfs_find;
0429 res = -EINVAL;
0430 root_inode = hfs_iget(sb, &fd.search_key->cat, &rec);
0431 hfs_find_exit(&fd);
0432 if (!root_inode)
0433 goto bail_no_root;
0434
0435 sb->s_d_op = &hfs_dentry_operations;
0436 res = -ENOMEM;
0437 sb->s_root = d_make_root(root_inode);
0438 if (!sb->s_root)
0439 goto bail_no_root;
0440
0441
0442 return 0;
0443
0444 bail_hfs_find:
0445 hfs_find_exit(&fd);
0446 bail_no_root:
0447 pr_err("get root inode failed\n");
0448 bail:
0449 hfs_mdb_put(sb);
0450 return res;
0451 }
0452
0453 static struct dentry *hfs_mount(struct file_system_type *fs_type,
0454 int flags, const char *dev_name, void *data)
0455 {
0456 return mount_bdev(fs_type, flags, dev_name, data, hfs_fill_super);
0457 }
0458
0459 static struct file_system_type hfs_fs_type = {
0460 .owner = THIS_MODULE,
0461 .name = "hfs",
0462 .mount = hfs_mount,
0463 .kill_sb = kill_block_super,
0464 .fs_flags = FS_REQUIRES_DEV,
0465 };
0466 MODULE_ALIAS_FS("hfs");
0467
0468 static void hfs_init_once(void *p)
0469 {
0470 struct hfs_inode_info *i = p;
0471
0472 inode_init_once(&i->vfs_inode);
0473 }
0474
0475 static int __init init_hfs_fs(void)
0476 {
0477 int err;
0478
0479 hfs_inode_cachep = kmem_cache_create("hfs_inode_cache",
0480 sizeof(struct hfs_inode_info), 0,
0481 SLAB_HWCACHE_ALIGN|SLAB_ACCOUNT, hfs_init_once);
0482 if (!hfs_inode_cachep)
0483 return -ENOMEM;
0484 err = register_filesystem(&hfs_fs_type);
0485 if (err)
0486 kmem_cache_destroy(hfs_inode_cachep);
0487 return err;
0488 }
0489
0490 static void __exit exit_hfs_fs(void)
0491 {
0492 unregister_filesystem(&hfs_fs_type);
0493
0494
0495
0496
0497
0498 rcu_barrier();
0499 kmem_cache_destroy(hfs_inode_cachep);
0500 }
0501
0502 module_init(init_hfs_fs)
0503 module_exit(exit_hfs_fs)