inode.c 14 KB

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