inode.c 14 KB

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