inode.c 15 KB

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