inode.c 13 KB

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