inode.c 12 KB

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