super.c 12 KB

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