inode.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Optimized MPEG FS - inode and super operations.
  4. * Copyright (C) 2006 Bob Copeland <me@bobcopeland.com>
  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(&nop_mnt_idmap, inode, NULL, mode);
  45. inode->i_mapping->a_ops = &omfs_aops;
  46. simple_inode_init_ts(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_get_ctime_sec(inode) * 1000LL +
  118. ((inode_get_ctime_nsec(inode) + 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_set_atime(inode, ctime, nsecs);
  196. inode_set_mtime(inode, ctime, nsecs);
  197. inode_set_ctime(inode, ctime, 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 = u64_to_fsid(id);
  240. buf->f_bfree = buf->f_bavail = buf->f_ffree =
  241. omfs_count_free(s);
  242. return 0;
  243. }
  244. /*
  245. * Display the mount options in /proc/mounts.
  246. */
  247. static int omfs_show_options(struct seq_file *m, struct dentry *root)
  248. {
  249. struct omfs_sb_info *sbi = OMFS_SB(root->d_sb);
  250. umode_t cur_umask = current_umask();
  251. if (!uid_eq(sbi->s_uid, current_uid()))
  252. seq_printf(m, ",uid=%u",
  253. from_kuid_munged(&init_user_ns, sbi->s_uid));
  254. if (!gid_eq(sbi->s_gid, current_gid()))
  255. seq_printf(m, ",gid=%u",
  256. from_kgid_munged(&init_user_ns, sbi->s_gid));
  257. if (sbi->s_dmask == sbi->s_fmask) {
  258. if (sbi->s_fmask != cur_umask)
  259. seq_printf(m, ",umask=%o", sbi->s_fmask);
  260. } else {
  261. if (sbi->s_dmask != cur_umask)
  262. seq_printf(m, ",dmask=%o", sbi->s_dmask);
  263. if (sbi->s_fmask != cur_umask)
  264. seq_printf(m, ",fmask=%o", sbi->s_fmask);
  265. }
  266. return 0;
  267. }
  268. static const struct super_operations omfs_sops = {
  269. .write_inode = omfs_write_inode,
  270. .evict_inode = omfs_evict_inode,
  271. .put_super = omfs_put_super,
  272. .statfs = omfs_statfs,
  273. .show_options = omfs_show_options,
  274. };
  275. /*
  276. * For Rio Karma, there is an on-disk free bitmap whose location is
  277. * stored in the root block. For ReplayTV, there is no such free bitmap
  278. * so we have to walk the tree. Both inodes and file data are allocated
  279. * from the same map. This array can be big (300k) so we allocate
  280. * in units of the blocksize.
  281. */
  282. static int omfs_get_imap(struct super_block *sb)
  283. {
  284. unsigned int bitmap_size, array_size;
  285. int count;
  286. struct omfs_sb_info *sbi = OMFS_SB(sb);
  287. struct buffer_head *bh;
  288. unsigned long **ptr;
  289. sector_t block;
  290. bitmap_size = DIV_ROUND_UP(sbi->s_num_blocks, 8);
  291. array_size = DIV_ROUND_UP(bitmap_size, sb->s_blocksize);
  292. if (sbi->s_bitmap_ino == ~0ULL)
  293. goto out;
  294. sbi->s_imap_size = array_size;
  295. sbi->s_imap = kcalloc(array_size, sizeof(unsigned long *), GFP_KERNEL);
  296. if (!sbi->s_imap)
  297. goto nomem;
  298. block = clus_to_blk(sbi, sbi->s_bitmap_ino);
  299. if (block >= sbi->s_num_blocks)
  300. goto nomem;
  301. ptr = sbi->s_imap;
  302. for (count = bitmap_size; count > 0; count -= sb->s_blocksize) {
  303. bh = sb_bread(sb, block++);
  304. if (!bh)
  305. goto nomem_free;
  306. *ptr = kmemdup(bh->b_data, sb->s_blocksize, GFP_KERNEL);
  307. if (!*ptr) {
  308. brelse(bh);
  309. goto nomem_free;
  310. }
  311. if (count < sb->s_blocksize)
  312. memset((void *)*ptr + count, 0xff,
  313. sb->s_blocksize - count);
  314. brelse(bh);
  315. ptr++;
  316. }
  317. out:
  318. return 0;
  319. nomem_free:
  320. for (count = 0; count < array_size; count++)
  321. kfree(sbi->s_imap[count]);
  322. kfree(sbi->s_imap);
  323. nomem:
  324. sbi->s_imap = NULL;
  325. sbi->s_imap_size = 0;
  326. return -ENOMEM;
  327. }
  328. enum {
  329. Opt_uid, Opt_gid, Opt_umask, Opt_dmask, Opt_fmask, Opt_err
  330. };
  331. static const match_table_t tokens = {
  332. {Opt_uid, "uid=%u"},
  333. {Opt_gid, "gid=%u"},
  334. {Opt_umask, "umask=%o"},
  335. {Opt_dmask, "dmask=%o"},
  336. {Opt_fmask, "fmask=%o"},
  337. {Opt_err, NULL},
  338. };
  339. static int parse_options(char *options, struct omfs_sb_info *sbi)
  340. {
  341. char *p;
  342. substring_t args[MAX_OPT_ARGS];
  343. int option;
  344. if (!options)
  345. return 1;
  346. while ((p = strsep(&options, ",")) != NULL) {
  347. int token;
  348. if (!*p)
  349. continue;
  350. token = match_token(p, tokens, args);
  351. switch (token) {
  352. case Opt_uid:
  353. if (match_int(&args[0], &option))
  354. return 0;
  355. sbi->s_uid = make_kuid(current_user_ns(), option);
  356. if (!uid_valid(sbi->s_uid))
  357. return 0;
  358. break;
  359. case Opt_gid:
  360. if (match_int(&args[0], &option))
  361. return 0;
  362. sbi->s_gid = make_kgid(current_user_ns(), option);
  363. if (!gid_valid(sbi->s_gid))
  364. return 0;
  365. break;
  366. case Opt_umask:
  367. if (match_octal(&args[0], &option))
  368. return 0;
  369. sbi->s_fmask = sbi->s_dmask = option;
  370. break;
  371. case Opt_dmask:
  372. if (match_octal(&args[0], &option))
  373. return 0;
  374. sbi->s_dmask = option;
  375. break;
  376. case Opt_fmask:
  377. if (match_octal(&args[0], &option))
  378. return 0;
  379. sbi->s_fmask = option;
  380. break;
  381. default:
  382. return 0;
  383. }
  384. }
  385. return 1;
  386. }
  387. static int omfs_fill_super(struct super_block *sb, void *data, int silent)
  388. {
  389. struct buffer_head *bh, *bh2;
  390. struct omfs_super_block *omfs_sb;
  391. struct omfs_root_block *omfs_rb;
  392. struct omfs_sb_info *sbi;
  393. struct inode *root;
  394. int ret = -EINVAL;
  395. sbi = kzalloc(sizeof(struct omfs_sb_info), GFP_KERNEL);
  396. if (!sbi)
  397. return -ENOMEM;
  398. sb->s_fs_info = sbi;
  399. sbi->s_uid = current_uid();
  400. sbi->s_gid = current_gid();
  401. sbi->s_dmask = sbi->s_fmask = current_umask();
  402. if (!parse_options((char *) data, sbi))
  403. goto end;
  404. sb->s_maxbytes = 0xffffffff;
  405. sb->s_time_gran = NSEC_PER_MSEC;
  406. sb->s_time_min = 0;
  407. sb->s_time_max = U64_MAX / MSEC_PER_SEC;
  408. sb_set_blocksize(sb, 0x200);
  409. bh = sb_bread(sb, 0);
  410. if (!bh)
  411. goto end;
  412. omfs_sb = (struct omfs_super_block *)bh->b_data;
  413. if (omfs_sb->s_magic != cpu_to_be32(OMFS_MAGIC)) {
  414. if (!silent)
  415. printk(KERN_ERR "omfs: Invalid superblock (%x)\n",
  416. omfs_sb->s_magic);
  417. goto out_brelse_bh;
  418. }
  419. sb->s_magic = OMFS_MAGIC;
  420. sbi->s_num_blocks = be64_to_cpu(omfs_sb->s_num_blocks);
  421. sbi->s_blocksize = be32_to_cpu(omfs_sb->s_blocksize);
  422. sbi->s_mirrors = be32_to_cpu(omfs_sb->s_mirrors);
  423. sbi->s_root_ino = be64_to_cpu(omfs_sb->s_root_block);
  424. sbi->s_sys_blocksize = be32_to_cpu(omfs_sb->s_sys_blocksize);
  425. mutex_init(&sbi->s_bitmap_lock);
  426. if (sbi->s_num_blocks > OMFS_MAX_BLOCKS) {
  427. printk(KERN_ERR "omfs: sysblock number (%llx) is out of range\n",
  428. (unsigned long long)sbi->s_num_blocks);
  429. goto out_brelse_bh;
  430. }
  431. if (sbi->s_sys_blocksize > PAGE_SIZE) {
  432. printk(KERN_ERR "omfs: sysblock size (%d) is out of range\n",
  433. sbi->s_sys_blocksize);
  434. goto out_brelse_bh;
  435. }
  436. if (sbi->s_blocksize < sbi->s_sys_blocksize ||
  437. sbi->s_blocksize > OMFS_MAX_BLOCK_SIZE) {
  438. printk(KERN_ERR "omfs: block size (%d) is out of range\n",
  439. sbi->s_blocksize);
  440. goto out_brelse_bh;
  441. }
  442. /*
  443. * Use sys_blocksize as the fs block since it is smaller than a
  444. * page while the fs blocksize can be larger.
  445. */
  446. sb_set_blocksize(sb, sbi->s_sys_blocksize);
  447. /*
  448. * ...and the difference goes into a shift. sys_blocksize is always
  449. * a power of two factor of blocksize.
  450. */
  451. sbi->s_block_shift = get_bitmask_order(sbi->s_blocksize) -
  452. get_bitmask_order(sbi->s_sys_blocksize);
  453. bh2 = omfs_bread(sb, be64_to_cpu(omfs_sb->s_root_block));
  454. if (!bh2)
  455. goto out_brelse_bh;
  456. omfs_rb = (struct omfs_root_block *)bh2->b_data;
  457. sbi->s_bitmap_ino = be64_to_cpu(omfs_rb->r_bitmap);
  458. sbi->s_clustersize = be32_to_cpu(omfs_rb->r_clustersize);
  459. if (sbi->s_num_blocks != be64_to_cpu(omfs_rb->r_num_blocks)) {
  460. printk(KERN_ERR "omfs: block count discrepancy between "
  461. "super and root blocks (%llx, %llx)\n",
  462. (unsigned long long)sbi->s_num_blocks,
  463. (unsigned long long)be64_to_cpu(omfs_rb->r_num_blocks));
  464. goto out_brelse_bh2;
  465. }
  466. if (sbi->s_bitmap_ino != ~0ULL &&
  467. sbi->s_bitmap_ino > sbi->s_num_blocks) {
  468. printk(KERN_ERR "omfs: free space bitmap location is corrupt "
  469. "(%llx, total blocks %llx)\n",
  470. (unsigned long long) sbi->s_bitmap_ino,
  471. (unsigned long long) sbi->s_num_blocks);
  472. goto out_brelse_bh2;
  473. }
  474. if (sbi->s_clustersize < 1 ||
  475. sbi->s_clustersize > OMFS_MAX_CLUSTER_SIZE) {
  476. printk(KERN_ERR "omfs: cluster size out of range (%d)",
  477. sbi->s_clustersize);
  478. goto out_brelse_bh2;
  479. }
  480. ret = omfs_get_imap(sb);
  481. if (ret)
  482. goto out_brelse_bh2;
  483. sb->s_op = &omfs_sops;
  484. root = omfs_iget(sb, be64_to_cpu(omfs_rb->r_root_dir));
  485. if (IS_ERR(root)) {
  486. ret = PTR_ERR(root);
  487. goto out_brelse_bh2;
  488. }
  489. sb->s_root = d_make_root(root);
  490. if (!sb->s_root) {
  491. ret = -ENOMEM;
  492. goto out_brelse_bh2;
  493. }
  494. printk(KERN_DEBUG "omfs: Mounted volume %s\n", omfs_rb->r_name);
  495. ret = 0;
  496. out_brelse_bh2:
  497. brelse(bh2);
  498. out_brelse_bh:
  499. brelse(bh);
  500. end:
  501. if (ret)
  502. kfree(sbi);
  503. return ret;
  504. }
  505. static struct dentry *omfs_mount(struct file_system_type *fs_type,
  506. int flags, const char *dev_name, void *data)
  507. {
  508. return mount_bdev(fs_type, flags, dev_name, data, omfs_fill_super);
  509. }
  510. static struct file_system_type omfs_fs_type = {
  511. .owner = THIS_MODULE,
  512. .name = "omfs",
  513. .mount = omfs_mount,
  514. .kill_sb = kill_block_super,
  515. .fs_flags = FS_REQUIRES_DEV,
  516. };
  517. MODULE_ALIAS_FS("omfs");
  518. static int __init init_omfs_fs(void)
  519. {
  520. return register_filesystem(&omfs_fs_type);
  521. }
  522. static void __exit exit_omfs_fs(void)
  523. {
  524. unregister_filesystem(&omfs_fs_type);
  525. }
  526. module_init(init_omfs_fs);
  527. module_exit(exit_omfs_fs);