orphan.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660
  1. /*
  2. * Ext4 orphan inode handling
  3. */
  4. #include <linux/fs.h>
  5. #include <linux/quotaops.h>
  6. #include <linux/buffer_head.h>
  7. #include "ext4.h"
  8. #include "ext4_jbd2.h"
  9. static int ext4_orphan_file_add(handle_t *handle, struct inode *inode)
  10. {
  11. int i, j, start;
  12. struct ext4_orphan_info *oi = &EXT4_SB(inode->i_sb)->s_orphan_info;
  13. int ret = 0;
  14. bool found = false;
  15. __le32 *bdata;
  16. int inodes_per_ob = ext4_inodes_per_orphan_block(inode->i_sb);
  17. int looped = 0;
  18. /*
  19. * Find block with free orphan entry. Use CPU number for a naive hash
  20. * for a search start in the orphan file
  21. */
  22. start = raw_smp_processor_id()*13 % oi->of_blocks;
  23. i = start;
  24. do {
  25. if (atomic_dec_if_positive(&oi->of_binfo[i].ob_free_entries)
  26. >= 0) {
  27. found = true;
  28. break;
  29. }
  30. if (++i >= oi->of_blocks)
  31. i = 0;
  32. } while (i != start);
  33. if (!found) {
  34. /*
  35. * For now we don't grow or shrink orphan file. We just use
  36. * whatever was allocated at mke2fs time. The additional
  37. * credits we would have to reserve for each orphan inode
  38. * operation just don't seem worth it.
  39. */
  40. return -ENOSPC;
  41. }
  42. ret = ext4_journal_get_write_access(handle, inode->i_sb,
  43. oi->of_binfo[i].ob_bh, EXT4_JTR_ORPHAN_FILE);
  44. if (ret) {
  45. atomic_inc(&oi->of_binfo[i].ob_free_entries);
  46. return ret;
  47. }
  48. bdata = (__le32 *)(oi->of_binfo[i].ob_bh->b_data);
  49. /* Find empty slot in a block */
  50. j = 0;
  51. do {
  52. if (looped) {
  53. /*
  54. * Did we walk through the block several times without
  55. * finding free entry? It is theoretically possible
  56. * if entries get constantly allocated and freed or
  57. * if the block is corrupted. Avoid indefinite looping
  58. * and bail. We'll use orphan list instead.
  59. */
  60. if (looped > 3) {
  61. atomic_inc(&oi->of_binfo[i].ob_free_entries);
  62. return -ENOSPC;
  63. }
  64. cond_resched();
  65. }
  66. while (bdata[j]) {
  67. if (++j >= inodes_per_ob) {
  68. j = 0;
  69. looped++;
  70. }
  71. }
  72. } while (cmpxchg(&bdata[j], (__le32)0, cpu_to_le32(inode->i_ino)) !=
  73. (__le32)0);
  74. EXT4_I(inode)->i_orphan_idx = i * inodes_per_ob + j;
  75. ext4_set_inode_state(inode, EXT4_STATE_ORPHAN_FILE);
  76. return ext4_handle_dirty_metadata(handle, NULL, oi->of_binfo[i].ob_bh);
  77. }
  78. /*
  79. * ext4_orphan_add() links an unlinked or truncated inode into a list of
  80. * such inodes, starting at the superblock, in case we crash before the
  81. * file is closed/deleted, or in case the inode truncate spans multiple
  82. * transactions and the last transaction is not recovered after a crash.
  83. *
  84. * At filesystem recovery time, we walk this list deleting unlinked
  85. * inodes and truncating linked inodes in ext4_orphan_cleanup().
  86. *
  87. * Orphan list manipulation functions must be called under i_rwsem unless
  88. * we are just creating the inode or deleting it.
  89. */
  90. int ext4_orphan_add(handle_t *handle, struct inode *inode)
  91. {
  92. struct super_block *sb = inode->i_sb;
  93. struct ext4_sb_info *sbi = EXT4_SB(sb);
  94. struct ext4_iloc iloc;
  95. int err = 0, rc;
  96. bool dirty = false;
  97. if (!sbi->s_journal || is_bad_inode(inode))
  98. return 0;
  99. WARN_ON_ONCE(!(inode->i_state & (I_NEW | I_FREEING)) &&
  100. !inode_is_locked(inode));
  101. if (ext4_inode_orphan_tracked(inode))
  102. return 0;
  103. /*
  104. * Orphan handling is only valid for files with data blocks
  105. * being truncated, or files being unlinked. Note that we either
  106. * hold i_rwsem, or the inode can not be referenced from outside,
  107. * so i_nlink should not be bumped due to race
  108. */
  109. ASSERT((S_ISREG(inode->i_mode) || S_ISDIR(inode->i_mode) ||
  110. S_ISLNK(inode->i_mode)) || inode->i_nlink == 0);
  111. if (sbi->s_orphan_info.of_blocks) {
  112. err = ext4_orphan_file_add(handle, inode);
  113. /*
  114. * Fallback to normal orphan list of orphan file is
  115. * out of space
  116. */
  117. if (err != -ENOSPC)
  118. return err;
  119. }
  120. BUFFER_TRACE(sbi->s_sbh, "get_write_access");
  121. err = ext4_journal_get_write_access(handle, sb, sbi->s_sbh,
  122. EXT4_JTR_NONE);
  123. if (err)
  124. goto out;
  125. err = ext4_reserve_inode_write(handle, inode, &iloc);
  126. if (err)
  127. goto out;
  128. mutex_lock(&sbi->s_orphan_lock);
  129. /*
  130. * Due to previous errors inode may be already a part of on-disk
  131. * orphan list. If so skip on-disk list modification.
  132. */
  133. if (!NEXT_ORPHAN(inode) || NEXT_ORPHAN(inode) >
  134. (le32_to_cpu(sbi->s_es->s_inodes_count))) {
  135. /* Insert this inode at the head of the on-disk orphan list */
  136. NEXT_ORPHAN(inode) = le32_to_cpu(sbi->s_es->s_last_orphan);
  137. lock_buffer(sbi->s_sbh);
  138. sbi->s_es->s_last_orphan = cpu_to_le32(inode->i_ino);
  139. ext4_superblock_csum_set(sb);
  140. unlock_buffer(sbi->s_sbh);
  141. dirty = true;
  142. }
  143. list_add(&EXT4_I(inode)->i_orphan, &sbi->s_orphan);
  144. mutex_unlock(&sbi->s_orphan_lock);
  145. if (dirty) {
  146. err = ext4_handle_dirty_metadata(handle, NULL, sbi->s_sbh);
  147. rc = ext4_mark_iloc_dirty(handle, inode, &iloc);
  148. if (!err)
  149. err = rc;
  150. if (err) {
  151. /*
  152. * We have to remove inode from in-memory list if
  153. * addition to on disk orphan list failed. Stray orphan
  154. * list entries can cause panics at unmount time.
  155. */
  156. mutex_lock(&sbi->s_orphan_lock);
  157. list_del_init(&EXT4_I(inode)->i_orphan);
  158. mutex_unlock(&sbi->s_orphan_lock);
  159. }
  160. } else
  161. brelse(iloc.bh);
  162. ext4_debug("superblock will point to %lu\n", inode->i_ino);
  163. ext4_debug("orphan inode %lu will point to %d\n",
  164. inode->i_ino, NEXT_ORPHAN(inode));
  165. out:
  166. ext4_std_error(sb, err);
  167. return err;
  168. }
  169. static int ext4_orphan_file_del(handle_t *handle, struct inode *inode)
  170. {
  171. struct ext4_orphan_info *oi = &EXT4_SB(inode->i_sb)->s_orphan_info;
  172. __le32 *bdata;
  173. int blk, off;
  174. int inodes_per_ob = ext4_inodes_per_orphan_block(inode->i_sb);
  175. int ret = 0;
  176. if (!handle)
  177. goto out;
  178. blk = EXT4_I(inode)->i_orphan_idx / inodes_per_ob;
  179. off = EXT4_I(inode)->i_orphan_idx % inodes_per_ob;
  180. if (WARN_ON_ONCE(blk >= oi->of_blocks))
  181. goto out;
  182. ret = ext4_journal_get_write_access(handle, inode->i_sb,
  183. oi->of_binfo[blk].ob_bh, EXT4_JTR_ORPHAN_FILE);
  184. if (ret)
  185. goto out;
  186. bdata = (__le32 *)(oi->of_binfo[blk].ob_bh->b_data);
  187. bdata[off] = 0;
  188. atomic_inc(&oi->of_binfo[blk].ob_free_entries);
  189. ret = ext4_handle_dirty_metadata(handle, NULL, oi->of_binfo[blk].ob_bh);
  190. out:
  191. ext4_clear_inode_state(inode, EXT4_STATE_ORPHAN_FILE);
  192. INIT_LIST_HEAD(&EXT4_I(inode)->i_orphan);
  193. return ret;
  194. }
  195. /*
  196. * ext4_orphan_del() removes an unlinked or truncated inode from the list
  197. * of such inodes stored on disk, because it is finally being cleaned up.
  198. */
  199. int ext4_orphan_del(handle_t *handle, struct inode *inode)
  200. {
  201. struct list_head *prev;
  202. struct ext4_inode_info *ei = EXT4_I(inode);
  203. struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb);
  204. __u32 ino_next;
  205. struct ext4_iloc iloc;
  206. int err = 0;
  207. if (!sbi->s_journal && !(sbi->s_mount_state & EXT4_ORPHAN_FS))
  208. return 0;
  209. WARN_ON_ONCE(!(inode->i_state & (I_NEW | I_FREEING)) &&
  210. !inode_is_locked(inode));
  211. if (ext4_test_inode_state(inode, EXT4_STATE_ORPHAN_FILE))
  212. return ext4_orphan_file_del(handle, inode);
  213. /* Do this quick check before taking global s_orphan_lock. */
  214. if (list_empty(&ei->i_orphan))
  215. return 0;
  216. if (handle) {
  217. /* Grab inode buffer early before taking global s_orphan_lock */
  218. err = ext4_reserve_inode_write(handle, inode, &iloc);
  219. }
  220. mutex_lock(&sbi->s_orphan_lock);
  221. ext4_debug("remove inode %lu from orphan list\n", inode->i_ino);
  222. prev = ei->i_orphan.prev;
  223. list_del_init(&ei->i_orphan);
  224. /* If we're on an error path, we may not have a valid
  225. * transaction handle with which to update the orphan list on
  226. * disk, but we still need to remove the inode from the linked
  227. * list in memory. */
  228. if (!handle || err) {
  229. mutex_unlock(&sbi->s_orphan_lock);
  230. goto out_err;
  231. }
  232. ino_next = NEXT_ORPHAN(inode);
  233. if (prev == &sbi->s_orphan) {
  234. ext4_debug("superblock will point to %u\n", ino_next);
  235. BUFFER_TRACE(sbi->s_sbh, "get_write_access");
  236. err = ext4_journal_get_write_access(handle, inode->i_sb,
  237. sbi->s_sbh, EXT4_JTR_NONE);
  238. if (err) {
  239. mutex_unlock(&sbi->s_orphan_lock);
  240. goto out_brelse;
  241. }
  242. lock_buffer(sbi->s_sbh);
  243. sbi->s_es->s_last_orphan = cpu_to_le32(ino_next);
  244. ext4_superblock_csum_set(inode->i_sb);
  245. unlock_buffer(sbi->s_sbh);
  246. mutex_unlock(&sbi->s_orphan_lock);
  247. err = ext4_handle_dirty_metadata(handle, NULL, sbi->s_sbh);
  248. } else {
  249. struct ext4_iloc iloc2;
  250. struct inode *i_prev =
  251. &list_entry(prev, struct ext4_inode_info, i_orphan)->vfs_inode;
  252. ext4_debug("orphan inode %lu will point to %u\n",
  253. i_prev->i_ino, ino_next);
  254. err = ext4_reserve_inode_write(handle, i_prev, &iloc2);
  255. if (err) {
  256. mutex_unlock(&sbi->s_orphan_lock);
  257. goto out_brelse;
  258. }
  259. NEXT_ORPHAN(i_prev) = ino_next;
  260. err = ext4_mark_iloc_dirty(handle, i_prev, &iloc2);
  261. mutex_unlock(&sbi->s_orphan_lock);
  262. }
  263. if (err)
  264. goto out_brelse;
  265. NEXT_ORPHAN(inode) = 0;
  266. err = ext4_mark_iloc_dirty(handle, inode, &iloc);
  267. out_err:
  268. ext4_std_error(inode->i_sb, err);
  269. return err;
  270. out_brelse:
  271. brelse(iloc.bh);
  272. goto out_err;
  273. }
  274. #ifdef CONFIG_QUOTA
  275. static int ext4_quota_on_mount(struct super_block *sb, int type)
  276. {
  277. return dquot_quota_on_mount(sb,
  278. rcu_dereference_protected(EXT4_SB(sb)->s_qf_names[type],
  279. lockdep_is_held(&sb->s_umount)),
  280. EXT4_SB(sb)->s_jquota_fmt, type);
  281. }
  282. #endif
  283. static void ext4_process_orphan(struct inode *inode,
  284. int *nr_truncates, int *nr_orphans)
  285. {
  286. struct super_block *sb = inode->i_sb;
  287. int ret;
  288. dquot_initialize(inode);
  289. if (inode->i_nlink) {
  290. if (test_opt(sb, DEBUG))
  291. ext4_msg(sb, KERN_DEBUG,
  292. "%s: truncating inode %lu to %lld bytes",
  293. __func__, inode->i_ino, inode->i_size);
  294. ext4_debug("truncating inode %lu to %lld bytes\n",
  295. inode->i_ino, inode->i_size);
  296. inode_lock(inode);
  297. truncate_inode_pages(inode->i_mapping, inode->i_size);
  298. ret = ext4_truncate(inode);
  299. if (ret) {
  300. /*
  301. * We need to clean up the in-core orphan list
  302. * manually if ext4_truncate() failed to get a
  303. * transaction handle.
  304. */
  305. ext4_orphan_del(NULL, inode);
  306. ext4_std_error(inode->i_sb, ret);
  307. }
  308. inode_unlock(inode);
  309. (*nr_truncates)++;
  310. } else {
  311. if (test_opt(sb, DEBUG))
  312. ext4_msg(sb, KERN_DEBUG,
  313. "%s: deleting unreferenced inode %lu",
  314. __func__, inode->i_ino);
  315. ext4_debug("deleting unreferenced inode %lu\n",
  316. inode->i_ino);
  317. (*nr_orphans)++;
  318. }
  319. iput(inode); /* The delete magic happens here! */
  320. }
  321. /* ext4_orphan_cleanup() walks a singly-linked list of inodes (starting at
  322. * the superblock) which were deleted from all directories, but held open by
  323. * a process at the time of a crash. We walk the list and try to delete these
  324. * inodes at recovery time (only with a read-write filesystem).
  325. *
  326. * In order to keep the orphan inode chain consistent during traversal (in
  327. * case of crash during recovery), we link each inode into the superblock
  328. * orphan list_head and handle it the same way as an inode deletion during
  329. * normal operation (which journals the operations for us).
  330. *
  331. * We only do an iget() and an iput() on each inode, which is very safe if we
  332. * accidentally point at an in-use or already deleted inode. The worst that
  333. * can happen in this case is that we get a "bit already cleared" message from
  334. * ext4_free_inode(). The only reason we would point at a wrong inode is if
  335. * e2fsck was run on this filesystem, and it must have already done the orphan
  336. * inode cleanup for us, so we can safely abort without any further action.
  337. */
  338. void ext4_orphan_cleanup(struct super_block *sb, struct ext4_super_block *es)
  339. {
  340. unsigned int s_flags = sb->s_flags;
  341. int nr_orphans = 0, nr_truncates = 0;
  342. struct inode *inode;
  343. int i, j;
  344. #ifdef CONFIG_QUOTA
  345. int quota_update = 0;
  346. #endif
  347. __le32 *bdata;
  348. struct ext4_orphan_info *oi = &EXT4_SB(sb)->s_orphan_info;
  349. int inodes_per_ob = ext4_inodes_per_orphan_block(sb);
  350. if (!es->s_last_orphan && !oi->of_blocks) {
  351. ext4_debug("no orphan inodes to clean up\n");
  352. return;
  353. }
  354. if (bdev_read_only(sb->s_bdev)) {
  355. ext4_msg(sb, KERN_ERR, "write access "
  356. "unavailable, skipping orphan cleanup");
  357. return;
  358. }
  359. /* Check if feature set would not allow a r/w mount */
  360. if (!ext4_feature_set_ok(sb, 0)) {
  361. ext4_msg(sb, KERN_INFO, "Skipping orphan cleanup due to "
  362. "unknown ROCOMPAT features");
  363. return;
  364. }
  365. if (EXT4_SB(sb)->s_mount_state & EXT4_ERROR_FS) {
  366. /* don't clear list on RO mount w/ errors */
  367. if (es->s_last_orphan && !(s_flags & SB_RDONLY)) {
  368. ext4_msg(sb, KERN_INFO, "Errors on filesystem, "
  369. "clearing orphan list.");
  370. es->s_last_orphan = 0;
  371. }
  372. ext4_debug("Skipping orphan recovery on fs with errors.\n");
  373. return;
  374. }
  375. if (s_flags & SB_RDONLY) {
  376. ext4_msg(sb, KERN_INFO, "orphan cleanup on readonly fs");
  377. sb->s_flags &= ~SB_RDONLY;
  378. }
  379. #ifdef CONFIG_QUOTA
  380. /*
  381. * Turn on quotas which were not enabled for read-only mounts if
  382. * filesystem has quota feature, so that they are updated correctly.
  383. */
  384. if (ext4_has_feature_quota(sb) && (s_flags & SB_RDONLY)) {
  385. int ret = ext4_enable_quotas(sb);
  386. if (!ret)
  387. quota_update = 1;
  388. else
  389. ext4_msg(sb, KERN_ERR,
  390. "Cannot turn on quotas: error %d", ret);
  391. }
  392. /* Turn on journaled quotas used for old sytle */
  393. for (i = 0; i < EXT4_MAXQUOTAS; i++) {
  394. if (EXT4_SB(sb)->s_qf_names[i]) {
  395. int ret = ext4_quota_on_mount(sb, i);
  396. if (!ret)
  397. quota_update = 1;
  398. else
  399. ext4_msg(sb, KERN_ERR,
  400. "Cannot turn on journaled "
  401. "quota: type %d: error %d", i, ret);
  402. }
  403. }
  404. #endif
  405. while (es->s_last_orphan) {
  406. /*
  407. * We may have encountered an error during cleanup; if
  408. * so, skip the rest.
  409. */
  410. if (EXT4_SB(sb)->s_mount_state & EXT4_ERROR_FS) {
  411. ext4_debug("Skipping orphan recovery on fs with errors.\n");
  412. es->s_last_orphan = 0;
  413. break;
  414. }
  415. inode = ext4_orphan_get(sb, le32_to_cpu(es->s_last_orphan));
  416. if (IS_ERR(inode)) {
  417. es->s_last_orphan = 0;
  418. break;
  419. }
  420. list_add(&EXT4_I(inode)->i_orphan, &EXT4_SB(sb)->s_orphan);
  421. ext4_process_orphan(inode, &nr_truncates, &nr_orphans);
  422. }
  423. for (i = 0; i < oi->of_blocks; i++) {
  424. bdata = (__le32 *)(oi->of_binfo[i].ob_bh->b_data);
  425. for (j = 0; j < inodes_per_ob; j++) {
  426. if (!bdata[j])
  427. continue;
  428. inode = ext4_orphan_get(sb, le32_to_cpu(bdata[j]));
  429. if (IS_ERR(inode))
  430. continue;
  431. ext4_set_inode_state(inode, EXT4_STATE_ORPHAN_FILE);
  432. EXT4_I(inode)->i_orphan_idx = i * inodes_per_ob + j;
  433. ext4_process_orphan(inode, &nr_truncates, &nr_orphans);
  434. }
  435. }
  436. #define PLURAL(x) (x), ((x) == 1) ? "" : "s"
  437. if (nr_orphans)
  438. ext4_msg(sb, KERN_INFO, "%d orphan inode%s deleted",
  439. PLURAL(nr_orphans));
  440. if (nr_truncates)
  441. ext4_msg(sb, KERN_INFO, "%d truncate%s cleaned up",
  442. PLURAL(nr_truncates));
  443. #ifdef CONFIG_QUOTA
  444. /* Turn off quotas if they were enabled for orphan cleanup */
  445. if (quota_update) {
  446. for (i = 0; i < EXT4_MAXQUOTAS; i++) {
  447. if (sb_dqopt(sb)->files[i])
  448. dquot_quota_off(sb, i);
  449. }
  450. }
  451. #endif
  452. sb->s_flags = s_flags; /* Restore SB_RDONLY status */
  453. }
  454. void ext4_release_orphan_info(struct super_block *sb)
  455. {
  456. int i;
  457. struct ext4_orphan_info *oi = &EXT4_SB(sb)->s_orphan_info;
  458. if (!oi->of_blocks)
  459. return;
  460. for (i = 0; i < oi->of_blocks; i++)
  461. brelse(oi->of_binfo[i].ob_bh);
  462. kvfree(oi->of_binfo);
  463. }
  464. static struct ext4_orphan_block_tail *ext4_orphan_block_tail(
  465. struct super_block *sb,
  466. struct buffer_head *bh)
  467. {
  468. return (struct ext4_orphan_block_tail *)(bh->b_data + sb->s_blocksize -
  469. sizeof(struct ext4_orphan_block_tail));
  470. }
  471. static int ext4_orphan_file_block_csum_verify(struct super_block *sb,
  472. struct buffer_head *bh)
  473. {
  474. __u32 calculated;
  475. int inodes_per_ob = ext4_inodes_per_orphan_block(sb);
  476. struct ext4_orphan_info *oi = &EXT4_SB(sb)->s_orphan_info;
  477. struct ext4_orphan_block_tail *ot;
  478. __le64 dsk_block_nr = cpu_to_le64(bh->b_blocknr);
  479. if (!ext4_has_metadata_csum(sb))
  480. return 1;
  481. ot = ext4_orphan_block_tail(sb, bh);
  482. calculated = ext4_chksum(EXT4_SB(sb), oi->of_csum_seed,
  483. (__u8 *)&dsk_block_nr, sizeof(dsk_block_nr));
  484. calculated = ext4_chksum(EXT4_SB(sb), calculated, (__u8 *)bh->b_data,
  485. inodes_per_ob * sizeof(__u32));
  486. return le32_to_cpu(ot->ob_checksum) == calculated;
  487. }
  488. /* This gets called only when checksumming is enabled */
  489. void ext4_orphan_file_block_trigger(struct jbd2_buffer_trigger_type *triggers,
  490. struct buffer_head *bh,
  491. void *data, size_t size)
  492. {
  493. struct super_block *sb = EXT4_TRIGGER(triggers)->sb;
  494. __u32 csum;
  495. int inodes_per_ob = ext4_inodes_per_orphan_block(sb);
  496. struct ext4_orphan_info *oi = &EXT4_SB(sb)->s_orphan_info;
  497. struct ext4_orphan_block_tail *ot;
  498. __le64 dsk_block_nr = cpu_to_le64(bh->b_blocknr);
  499. csum = ext4_chksum(EXT4_SB(sb), oi->of_csum_seed,
  500. (__u8 *)&dsk_block_nr, sizeof(dsk_block_nr));
  501. csum = ext4_chksum(EXT4_SB(sb), csum, (__u8 *)data,
  502. inodes_per_ob * sizeof(__u32));
  503. ot = ext4_orphan_block_tail(sb, bh);
  504. ot->ob_checksum = cpu_to_le32(csum);
  505. }
  506. int ext4_init_orphan_info(struct super_block *sb)
  507. {
  508. struct ext4_orphan_info *oi = &EXT4_SB(sb)->s_orphan_info;
  509. struct inode *inode;
  510. int i, j;
  511. int ret;
  512. int free;
  513. __le32 *bdata;
  514. int inodes_per_ob = ext4_inodes_per_orphan_block(sb);
  515. struct ext4_orphan_block_tail *ot;
  516. ino_t orphan_ino = le32_to_cpu(EXT4_SB(sb)->s_es->s_orphan_file_inum);
  517. if (!ext4_has_feature_orphan_file(sb))
  518. return 0;
  519. inode = ext4_iget(sb, orphan_ino, EXT4_IGET_SPECIAL);
  520. if (IS_ERR(inode)) {
  521. ext4_msg(sb, KERN_ERR, "get orphan inode failed");
  522. return PTR_ERR(inode);
  523. }
  524. /*
  525. * This is just an artificial limit to prevent corrupted fs from
  526. * consuming absurd amounts of memory when pinning blocks of orphan
  527. * file in memory.
  528. */
  529. if (inode->i_size > 8 << 20) {
  530. ext4_msg(sb, KERN_ERR, "orphan file too big: %llu",
  531. (unsigned long long)inode->i_size);
  532. ret = -EFSCORRUPTED;
  533. goto out_put;
  534. }
  535. oi->of_blocks = inode->i_size >> sb->s_blocksize_bits;
  536. oi->of_csum_seed = EXT4_I(inode)->i_csum_seed;
  537. oi->of_binfo = kvmalloc_array(oi->of_blocks,
  538. sizeof(struct ext4_orphan_block),
  539. GFP_KERNEL);
  540. if (!oi->of_binfo) {
  541. ret = -ENOMEM;
  542. goto out_put;
  543. }
  544. for (i = 0; i < oi->of_blocks; i++) {
  545. oi->of_binfo[i].ob_bh = ext4_bread(NULL, inode, i, 0);
  546. if (IS_ERR(oi->of_binfo[i].ob_bh)) {
  547. ret = PTR_ERR(oi->of_binfo[i].ob_bh);
  548. goto out_free;
  549. }
  550. if (!oi->of_binfo[i].ob_bh) {
  551. ret = -EIO;
  552. goto out_free;
  553. }
  554. ot = ext4_orphan_block_tail(sb, oi->of_binfo[i].ob_bh);
  555. if (le32_to_cpu(ot->ob_magic) != EXT4_ORPHAN_BLOCK_MAGIC) {
  556. ext4_error(sb, "orphan file block %d: bad magic", i);
  557. ret = -EIO;
  558. goto out_free;
  559. }
  560. if (!ext4_orphan_file_block_csum_verify(sb,
  561. oi->of_binfo[i].ob_bh)) {
  562. ext4_error(sb, "orphan file block %d: bad checksum", i);
  563. ret = -EIO;
  564. goto out_free;
  565. }
  566. bdata = (__le32 *)(oi->of_binfo[i].ob_bh->b_data);
  567. free = 0;
  568. for (j = 0; j < inodes_per_ob; j++)
  569. if (bdata[j] == 0)
  570. free++;
  571. atomic_set(&oi->of_binfo[i].ob_free_entries, free);
  572. }
  573. iput(inode);
  574. return 0;
  575. out_free:
  576. for (i--; i >= 0; i--)
  577. brelse(oi->of_binfo[i].ob_bh);
  578. kvfree(oi->of_binfo);
  579. out_put:
  580. iput(inode);
  581. return ret;
  582. }
  583. int ext4_orphan_file_empty(struct super_block *sb)
  584. {
  585. struct ext4_orphan_info *oi = &EXT4_SB(sb)->s_orphan_info;
  586. int i;
  587. int inodes_per_ob = ext4_inodes_per_orphan_block(sb);
  588. if (!ext4_has_feature_orphan_file(sb))
  589. return 1;
  590. for (i = 0; i < oi->of_blocks; i++)
  591. if (atomic_read(&oi->of_binfo[i].ob_free_entries) !=
  592. inodes_per_ob)
  593. return 0;
  594. return 1;
  595. }