inode.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561
  1. /*
  2. * Optimized MPEG FS - inode and super operations.
  3. * Copyright (C) 2006 Bob Copeland <me@bobcopeland.com>
  4. * Released under GPL v2.
  5. */
  6. #include <linux/module.h>
  7. #include <linux/sched.h>
  8. #include <linux/slab.h>
  9. #include <linux/fs.h>
  10. #include <linux/vfs.h>
  11. #include <linux/parser.h>
  12. #include <linux/buffer_head.h>
  13. #include <linux/vmalloc.h>
  14. #include <linux/writeback.h>
  15. #include <linux/crc-itu-t.h>
  16. #include "omfs.h"
  17. MODULE_AUTHOR("Bob Copeland <me@bobcopeland.com>");
  18. MODULE_DESCRIPTION("OMFS (ReplayTV/Karma) Filesystem for Linux");
  19. MODULE_LICENSE("GPL");
  20. struct inode *omfs_new_inode(struct inode *dir, int mode)
  21. {
  22. struct inode *inode;
  23. u64 new_block;
  24. int err;
  25. int len;
  26. struct omfs_sb_info *sbi = OMFS_SB(dir->i_sb);
  27. inode = new_inode(dir->i_sb);
  28. if (!inode)
  29. return ERR_PTR(-ENOMEM);
  30. err = omfs_allocate_range(dir->i_sb, sbi->s_mirrors, sbi->s_mirrors,
  31. &new_block, &len);
  32. if (err)
  33. goto fail;
  34. inode->i_ino = new_block;
  35. inode_init_owner(inode, NULL, mode);
  36. inode->i_mapping->a_ops = &omfs_aops;
  37. inode->i_atime = inode->i_mtime = inode->i_ctime = CURRENT_TIME;
  38. switch (mode & S_IFMT) {
  39. case S_IFDIR:
  40. inode->i_op = &omfs_dir_inops;
  41. inode->i_fop = &omfs_dir_operations;
  42. inode->i_size = sbi->s_sys_blocksize;
  43. inc_nlink(inode);
  44. break;
  45. case S_IFREG:
  46. inode->i_op = &omfs_file_inops;
  47. inode->i_fop = &omfs_file_operations;
  48. inode->i_size = 0;
  49. break;
  50. }
  51. insert_inode_hash(inode);
  52. mark_inode_dirty(inode);
  53. return inode;
  54. fail:
  55. make_bad_inode(inode);
  56. iput(inode);
  57. return ERR_PTR(err);
  58. }
  59. /*
  60. * Update the header checksums for a dirty inode based on its contents.
  61. * Caller is expected to hold the buffer head underlying oi and mark it
  62. * dirty.
  63. */
  64. static void omfs_update_checksums(struct omfs_inode *oi)
  65. {
  66. int xor, i, ofs = 0, count;
  67. u16 crc = 0;
  68. unsigned char *ptr = (unsigned char *) oi;
  69. count = be32_to_cpu(oi->i_head.h_body_size);
  70. ofs = sizeof(struct omfs_header);
  71. crc = crc_itu_t(crc, ptr + ofs, count);
  72. oi->i_head.h_crc = cpu_to_be16(crc);
  73. xor = ptr[0];
  74. for (i = 1; i < OMFS_XOR_COUNT; i++)
  75. xor ^= ptr[i];
  76. oi->i_head.h_check_xor = xor;
  77. }
  78. static int __omfs_write_inode(struct inode *inode, int wait)
  79. {
  80. struct omfs_inode *oi;
  81. struct omfs_sb_info *sbi = OMFS_SB(inode->i_sb);
  82. struct buffer_head *bh, *bh2;
  83. unsigned int block;
  84. u64 ctime;
  85. int i;
  86. int ret = -EIO;
  87. int sync_failed = 0;
  88. /* get current inode since we may have written sibling ptrs etc. */
  89. block = clus_to_blk(sbi, inode->i_ino);
  90. bh = sb_bread(inode->i_sb, block);
  91. if (!bh)
  92. goto out;
  93. oi = (struct omfs_inode *) bh->b_data;
  94. oi->i_head.h_self = cpu_to_be64(inode->i_ino);
  95. if (S_ISDIR(inode->i_mode))
  96. oi->i_type = OMFS_DIR;
  97. else if (S_ISREG(inode->i_mode))
  98. oi->i_type = OMFS_FILE;
  99. else {
  100. printk(KERN_WARNING "omfs: unknown file type: %d\n",
  101. inode->i_mode);
  102. goto out_brelse;
  103. }
  104. oi->i_head.h_body_size = cpu_to_be32(sbi->s_sys_blocksize -
  105. sizeof(struct omfs_header));
  106. oi->i_head.h_version = 1;
  107. oi->i_head.h_type = OMFS_INODE_NORMAL;
  108. oi->i_head.h_magic = OMFS_IMAGIC;
  109. oi->i_size = cpu_to_be64(inode->i_size);
  110. ctime = inode->i_ctime.tv_sec * 1000LL +
  111. ((inode->i_ctime.tv_nsec + 999)/1000);
  112. oi->i_ctime = cpu_to_be64(ctime);
  113. omfs_update_checksums(oi);
  114. mark_buffer_dirty(bh);
  115. if (wait) {
  116. sync_dirty_buffer(bh);
  117. if (buffer_req(bh) && !buffer_uptodate(bh))
  118. sync_failed = 1;
  119. }
  120. /* if mirroring writes, copy to next fsblock */
  121. for (i = 1; i < sbi->s_mirrors; i++) {
  122. bh2 = sb_bread(inode->i_sb, block + i *
  123. (sbi->s_blocksize / sbi->s_sys_blocksize));
  124. if (!bh2)
  125. goto out_brelse;
  126. memcpy(bh2->b_data, bh->b_data, bh->b_size);
  127. mark_buffer_dirty(bh2);
  128. if (wait) {
  129. sync_dirty_buffer(bh2);
  130. if (buffer_req(bh2) && !buffer_uptodate(bh2))
  131. sync_failed = 1;
  132. }
  133. brelse(bh2);
  134. }
  135. ret = (sync_failed) ? -EIO : 0;
  136. out_brelse:
  137. brelse(bh);
  138. out:
  139. return ret;
  140. }
  141. static int omfs_write_inode(struct inode *inode, struct writeback_control *wbc)
  142. {
  143. return __omfs_write_inode(inode, wbc->sync_mode == WB_SYNC_ALL);
  144. }
  145. int omfs_sync_inode(struct inode *inode)
  146. {
  147. return __omfs_write_inode(inode, 1);
  148. }
  149. /*
  150. * called when an entry is deleted, need to clear the bits in the
  151. * bitmaps.
  152. */
  153. static void omfs_delete_inode(struct inode *inode)
  154. {
  155. truncate_inode_pages(&inode->i_data, 0);
  156. if (S_ISREG(inode->i_mode)) {
  157. inode->i_size = 0;
  158. omfs_shrink_inode(inode);
  159. }
  160. omfs_clear_range(inode->i_sb, inode->i_ino, 2);
  161. clear_inode(inode);
  162. }
  163. struct inode *omfs_iget(struct super_block *sb, ino_t ino)
  164. {
  165. struct omfs_sb_info *sbi = OMFS_SB(sb);
  166. struct omfs_inode *oi;
  167. struct buffer_head *bh;
  168. unsigned int block;
  169. u64 ctime;
  170. unsigned long nsecs;
  171. struct inode *inode;
  172. inode = iget_locked(sb, ino);
  173. if (!inode)
  174. return ERR_PTR(-ENOMEM);
  175. if (!(inode->i_state & I_NEW))
  176. return inode;
  177. block = clus_to_blk(sbi, ino);
  178. bh = sb_bread(inode->i_sb, block);
  179. if (!bh)
  180. goto iget_failed;
  181. oi = (struct omfs_inode *)bh->b_data;
  182. /* check self */
  183. if (ino != be64_to_cpu(oi->i_head.h_self))
  184. goto fail_bh;
  185. inode->i_uid = sbi->s_uid;
  186. inode->i_gid = sbi->s_gid;
  187. ctime = be64_to_cpu(oi->i_ctime);
  188. nsecs = do_div(ctime, 1000) * 1000L;
  189. inode->i_atime.tv_sec = ctime;
  190. inode->i_mtime.tv_sec = ctime;
  191. inode->i_ctime.tv_sec = ctime;
  192. inode->i_atime.tv_nsec = nsecs;
  193. inode->i_mtime.tv_nsec = nsecs;
  194. inode->i_ctime.tv_nsec = nsecs;
  195. inode->i_mapping->a_ops = &omfs_aops;
  196. switch (oi->i_type) {
  197. case OMFS_DIR:
  198. inode->i_mode = S_IFDIR | (S_IRWXUGO & ~sbi->s_dmask);
  199. inode->i_op = &omfs_dir_inops;
  200. inode->i_fop = &omfs_dir_operations;
  201. inode->i_size = sbi->s_sys_blocksize;
  202. inc_nlink(inode);
  203. break;
  204. case OMFS_FILE:
  205. inode->i_mode = S_IFREG | (S_IRWXUGO & ~sbi->s_fmask);
  206. inode->i_fop = &omfs_file_operations;
  207. inode->i_size = be64_to_cpu(oi->i_size);
  208. break;
  209. }
  210. brelse(bh);
  211. unlock_new_inode(inode);
  212. return inode;
  213. fail_bh:
  214. brelse(bh);
  215. iget_failed:
  216. iget_failed(inode);
  217. return ERR_PTR(-EIO);
  218. }
  219. static void omfs_put_super(struct super_block *sb)
  220. {
  221. struct omfs_sb_info *sbi = OMFS_SB(sb);
  222. kfree(sbi->s_imap);
  223. kfree(sbi);
  224. sb->s_fs_info = NULL;
  225. }
  226. static int omfs_statfs(struct dentry *dentry, struct kstatfs *buf)
  227. {
  228. struct super_block *s = dentry->d_sb;
  229. struct omfs_sb_info *sbi = OMFS_SB(s);
  230. u64 id = huge_encode_dev(s->s_bdev->bd_dev);
  231. buf->f_type = OMFS_MAGIC;
  232. buf->f_bsize = sbi->s_blocksize;
  233. buf->f_blocks = sbi->s_num_blocks;
  234. buf->f_files = sbi->s_num_blocks;
  235. buf->f_namelen = OMFS_NAMELEN;
  236. buf->f_fsid.val[0] = (u32)id;
  237. buf->f_fsid.val[1] = (u32)(id >> 32);
  238. buf->f_bfree = buf->f_bavail = buf->f_ffree =
  239. omfs_count_free(s);
  240. return 0;
  241. }
  242. static const struct super_operations omfs_sops = {
  243. .write_inode = omfs_write_inode,
  244. .delete_inode = omfs_delete_inode,
  245. .put_super = omfs_put_super,
  246. .statfs = omfs_statfs,
  247. .show_options = generic_show_options,
  248. };
  249. /*
  250. * For Rio Karma, there is an on-disk free bitmap whose location is
  251. * stored in the root block. For ReplayTV, there is no such free bitmap
  252. * so we have to walk the tree. Both inodes and file data are allocated
  253. * from the same map. This array can be big (300k) so we allocate
  254. * in units of the blocksize.
  255. */
  256. static int omfs_get_imap(struct super_block *sb)
  257. {
  258. int bitmap_size;
  259. int array_size;
  260. int count;
  261. struct omfs_sb_info *sbi = OMFS_SB(sb);
  262. struct buffer_head *bh;
  263. unsigned long **ptr;
  264. sector_t block;
  265. bitmap_size = DIV_ROUND_UP(sbi->s_num_blocks, 8);
  266. array_size = DIV_ROUND_UP(bitmap_size, sb->s_blocksize);
  267. if (sbi->s_bitmap_ino == ~0ULL)
  268. goto out;
  269. sbi->s_imap_size = array_size;
  270. sbi->s_imap = kzalloc(array_size * sizeof(unsigned long *), GFP_KERNEL);
  271. if (!sbi->s_imap)
  272. goto nomem;
  273. block = clus_to_blk(sbi, sbi->s_bitmap_ino);
  274. ptr = sbi->s_imap;
  275. for (count = bitmap_size; count > 0; count -= sb->s_blocksize) {
  276. bh = sb_bread(sb, block++);
  277. if (!bh)
  278. goto nomem_free;
  279. *ptr = kmalloc(sb->s_blocksize, GFP_KERNEL);
  280. if (!*ptr) {
  281. brelse(bh);
  282. goto nomem_free;
  283. }
  284. memcpy(*ptr, bh->b_data, sb->s_blocksize);
  285. if (count < sb->s_blocksize)
  286. memset((void *)*ptr + count, 0xff,
  287. sb->s_blocksize - count);
  288. brelse(bh);
  289. ptr++;
  290. }
  291. out:
  292. return 0;
  293. nomem_free:
  294. for (count = 0; count < array_size; count++)
  295. kfree(sbi->s_imap[count]);
  296. kfree(sbi->s_imap);
  297. nomem:
  298. sbi->s_imap = NULL;
  299. sbi->s_imap_size = 0;
  300. return -ENOMEM;
  301. }
  302. enum {
  303. Opt_uid, Opt_gid, Opt_umask, Opt_dmask, Opt_fmask
  304. };
  305. static const match_table_t tokens = {
  306. {Opt_uid, "uid=%u"},
  307. {Opt_gid, "gid=%u"},
  308. {Opt_umask, "umask=%o"},
  309. {Opt_dmask, "dmask=%o"},
  310. {Opt_fmask, "fmask=%o"},
  311. };
  312. static int parse_options(char *options, struct omfs_sb_info *sbi)
  313. {
  314. char *p;
  315. substring_t args[MAX_OPT_ARGS];
  316. int option;
  317. if (!options)
  318. return 1;
  319. while ((p = strsep(&options, ",")) != NULL) {
  320. int token;
  321. if (!*p)
  322. continue;
  323. token = match_token(p, tokens, args);
  324. switch (token) {
  325. case Opt_uid:
  326. if (match_int(&args[0], &option))
  327. return 0;
  328. sbi->s_uid = option;
  329. break;
  330. case Opt_gid:
  331. if (match_int(&args[0], &option))
  332. return 0;
  333. sbi->s_gid = option;
  334. break;
  335. case Opt_umask:
  336. if (match_octal(&args[0], &option))
  337. return 0;
  338. sbi->s_fmask = sbi->s_dmask = option;
  339. break;
  340. case Opt_dmask:
  341. if (match_octal(&args[0], &option))
  342. return 0;
  343. sbi->s_dmask = option;
  344. break;
  345. case Opt_fmask:
  346. if (match_octal(&args[0], &option))
  347. return 0;
  348. sbi->s_fmask = option;
  349. break;
  350. default:
  351. return 0;
  352. }
  353. }
  354. return 1;
  355. }
  356. static int omfs_fill_super(struct super_block *sb, void *data, int silent)
  357. {
  358. struct buffer_head *bh, *bh2;
  359. struct omfs_super_block *omfs_sb;
  360. struct omfs_root_block *omfs_rb;
  361. struct omfs_sb_info *sbi;
  362. struct inode *root;
  363. sector_t start;
  364. int ret = -EINVAL;
  365. save_mount_options(sb, (char *) data);
  366. sbi = kzalloc(sizeof(struct omfs_sb_info), GFP_KERNEL);
  367. if (!sbi)
  368. return -ENOMEM;
  369. sb->s_fs_info = sbi;
  370. sbi->s_uid = current_uid();
  371. sbi->s_gid = current_gid();
  372. sbi->s_dmask = sbi->s_fmask = current_umask();
  373. if (!parse_options((char *) data, sbi))
  374. goto end;
  375. sb->s_maxbytes = 0xffffffff;
  376. sb_set_blocksize(sb, 0x200);
  377. bh = sb_bread(sb, 0);
  378. if (!bh)
  379. goto end;
  380. omfs_sb = (struct omfs_super_block *)bh->b_data;
  381. if (omfs_sb->s_magic != cpu_to_be32(OMFS_MAGIC)) {
  382. if (!silent)
  383. printk(KERN_ERR "omfs: Invalid superblock (%x)\n",
  384. omfs_sb->s_magic);
  385. goto out_brelse_bh;
  386. }
  387. sb->s_magic = OMFS_MAGIC;
  388. sbi->s_num_blocks = be64_to_cpu(omfs_sb->s_num_blocks);
  389. sbi->s_blocksize = be32_to_cpu(omfs_sb->s_blocksize);
  390. sbi->s_mirrors = be32_to_cpu(omfs_sb->s_mirrors);
  391. sbi->s_root_ino = be64_to_cpu(omfs_sb->s_root_block);
  392. sbi->s_sys_blocksize = be32_to_cpu(omfs_sb->s_sys_blocksize);
  393. mutex_init(&sbi->s_bitmap_lock);
  394. if (sbi->s_sys_blocksize > PAGE_SIZE) {
  395. printk(KERN_ERR "omfs: sysblock size (%d) is out of range\n",
  396. sbi->s_sys_blocksize);
  397. goto out_brelse_bh;
  398. }
  399. if (sbi->s_blocksize < sbi->s_sys_blocksize ||
  400. sbi->s_blocksize > OMFS_MAX_BLOCK_SIZE) {
  401. printk(KERN_ERR "omfs: block size (%d) is out of range\n",
  402. sbi->s_blocksize);
  403. goto out_brelse_bh;
  404. }
  405. /*
  406. * Use sys_blocksize as the fs block since it is smaller than a
  407. * page while the fs blocksize can be larger.
  408. */
  409. sb_set_blocksize(sb, sbi->s_sys_blocksize);
  410. /*
  411. * ...and the difference goes into a shift. sys_blocksize is always
  412. * a power of two factor of blocksize.
  413. */
  414. sbi->s_block_shift = get_bitmask_order(sbi->s_blocksize) -
  415. get_bitmask_order(sbi->s_sys_blocksize);
  416. start = clus_to_blk(sbi, be64_to_cpu(omfs_sb->s_root_block));
  417. bh2 = sb_bread(sb, start);
  418. if (!bh2)
  419. goto out_brelse_bh;
  420. omfs_rb = (struct omfs_root_block *)bh2->b_data;
  421. sbi->s_bitmap_ino = be64_to_cpu(omfs_rb->r_bitmap);
  422. sbi->s_clustersize = be32_to_cpu(omfs_rb->r_clustersize);
  423. if (sbi->s_num_blocks != be64_to_cpu(omfs_rb->r_num_blocks)) {
  424. printk(KERN_ERR "omfs: block count discrepancy between "
  425. "super and root blocks (%llx, %llx)\n",
  426. (unsigned long long)sbi->s_num_blocks,
  427. (unsigned long long)be64_to_cpu(omfs_rb->r_num_blocks));
  428. goto out_brelse_bh2;
  429. }
  430. ret = omfs_get_imap(sb);
  431. if (ret)
  432. goto out_brelse_bh2;
  433. sb->s_op = &omfs_sops;
  434. root = omfs_iget(sb, be64_to_cpu(omfs_rb->r_root_dir));
  435. if (IS_ERR(root)) {
  436. ret = PTR_ERR(root);
  437. goto out_brelse_bh2;
  438. }
  439. sb->s_root = d_alloc_root(root);
  440. if (!sb->s_root) {
  441. iput(root);
  442. goto out_brelse_bh2;
  443. }
  444. printk(KERN_DEBUG "omfs: Mounted volume %s\n", omfs_rb->r_name);
  445. ret = 0;
  446. out_brelse_bh2:
  447. brelse(bh2);
  448. out_brelse_bh:
  449. brelse(bh);
  450. end:
  451. return ret;
  452. }
  453. static int omfs_get_sb(struct file_system_type *fs_type,
  454. int flags, const char *dev_name,
  455. void *data, struct vfsmount *m)
  456. {
  457. return get_sb_bdev(fs_type, flags, dev_name, data, omfs_fill_super, m);
  458. }
  459. static struct file_system_type omfs_fs_type = {
  460. .owner = THIS_MODULE,
  461. .name = "omfs",
  462. .get_sb = omfs_get_sb,
  463. .kill_sb = kill_block_super,
  464. .fs_flags = FS_REQUIRES_DEV,
  465. };
  466. static int __init init_omfs_fs(void)
  467. {
  468. return register_filesystem(&omfs_fs_type);
  469. }
  470. static void __exit exit_omfs_fs(void)
  471. {
  472. unregister_filesystem(&omfs_fs_type);
  473. }
  474. module_init(init_omfs_fs);
  475. module_exit(exit_omfs_fs);