inode.c 14 KB

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