super.c 9.7 KB

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