inode.c 12 KB

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