orphan.c (25c6d98fc4c245d164cf688815a7b259257ead2a) | orphan.c (02f310fcf47fa9311d6ba2946a8d19e7d7d11f37) |
---|---|
1/* 2 * Ext4 orphan inode handling 3 */ 4#include <linux/fs.h> 5#include <linux/quotaops.h> 6#include <linux/buffer_head.h> 7 8#include "ext4.h" 9#include "ext4_jbd2.h" 10 | 1/* 2 * Ext4 orphan inode handling 3 */ 4#include <linux/fs.h> 5#include <linux/quotaops.h> 6#include <linux/buffer_head.h> 7 8#include "ext4.h" 9#include "ext4_jbd2.h" 10 |
11static int ext4_orphan_file_add(handle_t *handle, struct inode *inode) 12{ 13 int i, j; 14 struct ext4_orphan_info *oi = &EXT4_SB(inode->i_sb)->s_orphan_info; 15 int ret = 0; 16 __le32 *bdata; 17 int inodes_per_ob = ext4_inodes_per_orphan_block(inode->i_sb); 18 19 spin_lock(&oi->of_lock); 20 for (i = 0; i < oi->of_blocks && !oi->of_binfo[i].ob_free_entries; i++); 21 if (i == oi->of_blocks) { 22 spin_unlock(&oi->of_lock); 23 /* 24 * For now we don't grow or shrink orphan file. We just use 25 * whatever was allocated at mke2fs time. The additional 26 * credits we would have to reserve for each orphan inode 27 * operation just don't seem worth it. 28 */ 29 return -ENOSPC; 30 } 31 oi->of_binfo[i].ob_free_entries--; 32 spin_unlock(&oi->of_lock); 33 34 /* 35 * Get access to orphan block. We have dropped of_lock but since we 36 * have decremented number of free entries we are guaranteed free entry 37 * in our block. 38 */ 39 ret = ext4_journal_get_write_access(handle, inode->i_sb, 40 oi->of_binfo[i].ob_bh, EXT4_JTR_ORPHAN_FILE); 41 if (ret) 42 return ret; 43 44 bdata = (__le32 *)(oi->of_binfo[i].ob_bh->b_data); 45 spin_lock(&oi->of_lock); 46 /* Find empty slot in a block */ 47 for (j = 0; j < inodes_per_ob && bdata[j]; j++); 48 BUG_ON(j == inodes_per_ob); 49 bdata[j] = cpu_to_le32(inode->i_ino); 50 EXT4_I(inode)->i_orphan_idx = i * inodes_per_ob + j; 51 ext4_set_inode_state(inode, EXT4_STATE_ORPHAN_FILE); 52 spin_unlock(&oi->of_lock); 53 54 return ext4_handle_dirty_metadata(handle, NULL, oi->of_binfo[i].ob_bh); 55} 56 |
|
11/* 12 * ext4_orphan_add() links an unlinked or truncated inode into a list of 13 * such inodes, starting at the superblock, in case we crash before the 14 * file is closed/deleted, or in case the inode truncate spans multiple 15 * transactions and the last transaction is not recovered after a crash. 16 * 17 * At filesystem recovery time, we walk this list deleting unlinked 18 * inodes and truncating linked inodes in ext4_orphan_cleanup(). --- 10 unchanged lines hidden (view full) --- 29 bool dirty = false; 30 31 if (!sbi->s_journal || is_bad_inode(inode)) 32 return 0; 33 34 WARN_ON_ONCE(!(inode->i_state & (I_NEW | I_FREEING)) && 35 !inode_is_locked(inode)); 36 /* | 57/* 58 * ext4_orphan_add() links an unlinked or truncated inode into a list of 59 * such inodes, starting at the superblock, in case we crash before the 60 * file is closed/deleted, or in case the inode truncate spans multiple 61 * transactions and the last transaction is not recovered after a crash. 62 * 63 * At filesystem recovery time, we walk this list deleting unlinked 64 * inodes and truncating linked inodes in ext4_orphan_cleanup(). --- 10 unchanged lines hidden (view full) --- 75 bool dirty = false; 76 77 if (!sbi->s_journal || is_bad_inode(inode)) 78 return 0; 79 80 WARN_ON_ONCE(!(inode->i_state & (I_NEW | I_FREEING)) && 81 !inode_is_locked(inode)); 82 /* |
37 * Exit early if inode already is on orphan list. This is a big speedup 38 * since we don't have to contend on the global s_orphan_lock. | 83 * Inode orphaned in orphan file or in orphan list? |
39 */ | 84 */ |
40 if (!list_empty(&EXT4_I(inode)->i_orphan)) | 85 if (ext4_test_inode_state(inode, EXT4_STATE_ORPHAN_FILE) || 86 !list_empty(&EXT4_I(inode)->i_orphan)) |
41 return 0; 42 43 /* 44 * Orphan handling is only valid for files with data blocks 45 * being truncated, or files being unlinked. Note that we either 46 * hold i_mutex, or the inode can not be referenced from outside, 47 * so i_nlink should not be bumped due to race 48 */ 49 ASSERT((S_ISREG(inode->i_mode) || S_ISDIR(inode->i_mode) || 50 S_ISLNK(inode->i_mode)) || inode->i_nlink == 0); 51 | 87 return 0; 88 89 /* 90 * Orphan handling is only valid for files with data blocks 91 * being truncated, or files being unlinked. Note that we either 92 * hold i_mutex, or the inode can not be referenced from outside, 93 * so i_nlink should not be bumped due to race 94 */ 95 ASSERT((S_ISREG(inode->i_mode) || S_ISDIR(inode->i_mode) || 96 S_ISLNK(inode->i_mode)) || inode->i_nlink == 0); 97 |
98 if (sbi->s_orphan_info.of_blocks) { 99 err = ext4_orphan_file_add(handle, inode); 100 /* 101 * Fallback to normal orphan list of orphan file is 102 * out of space 103 */ 104 if (err != -ENOSPC) 105 return err; 106 } 107 |
|
52 BUFFER_TRACE(sbi->s_sbh, "get_write_access"); 53 err = ext4_journal_get_write_access(handle, sb, sbi->s_sbh, 54 EXT4_JTR_NONE); 55 if (err) 56 goto out; 57 58 err = ext4_reserve_inode_write(handle, inode, &iloc); 59 if (err) --- 38 unchanged lines hidden (view full) --- 98 jbd_debug(4, "superblock will point to %lu\n", inode->i_ino); 99 jbd_debug(4, "orphan inode %lu will point to %d\n", 100 inode->i_ino, NEXT_ORPHAN(inode)); 101out: 102 ext4_std_error(sb, err); 103 return err; 104} 105 | 108 BUFFER_TRACE(sbi->s_sbh, "get_write_access"); 109 err = ext4_journal_get_write_access(handle, sb, sbi->s_sbh, 110 EXT4_JTR_NONE); 111 if (err) 112 goto out; 113 114 err = ext4_reserve_inode_write(handle, inode, &iloc); 115 if (err) --- 38 unchanged lines hidden (view full) --- 154 jbd_debug(4, "superblock will point to %lu\n", inode->i_ino); 155 jbd_debug(4, "orphan inode %lu will point to %d\n", 156 inode->i_ino, NEXT_ORPHAN(inode)); 157out: 158 ext4_std_error(sb, err); 159 return err; 160} 161 |
162static int ext4_orphan_file_del(handle_t *handle, struct inode *inode) 163{ 164 struct ext4_orphan_info *oi = &EXT4_SB(inode->i_sb)->s_orphan_info; 165 __le32 *bdata; 166 int blk, off; 167 int inodes_per_ob = ext4_inodes_per_orphan_block(inode->i_sb); 168 int ret = 0; 169 170 if (!handle) 171 goto out; 172 blk = EXT4_I(inode)->i_orphan_idx / inodes_per_ob; 173 off = EXT4_I(inode)->i_orphan_idx % inodes_per_ob; 174 if (WARN_ON_ONCE(blk >= oi->of_blocks)) 175 goto out; 176 177 ret = ext4_journal_get_write_access(handle, inode->i_sb, 178 oi->of_binfo[blk].ob_bh, EXT4_JTR_ORPHAN_FILE); 179 if (ret) 180 goto out; 181 182 bdata = (__le32 *)(oi->of_binfo[blk].ob_bh->b_data); 183 spin_lock(&oi->of_lock); 184 bdata[off] = 0; 185 oi->of_binfo[blk].ob_free_entries++; 186 spin_unlock(&oi->of_lock); 187 ret = ext4_handle_dirty_metadata(handle, NULL, oi->of_binfo[blk].ob_bh); 188out: 189 ext4_clear_inode_state(inode, EXT4_STATE_ORPHAN_FILE); 190 INIT_LIST_HEAD(&EXT4_I(inode)->i_orphan); 191 192 return ret; 193} 194 |
|
106/* 107 * ext4_orphan_del() removes an unlinked or truncated inode from the list 108 * of such inodes stored on disk, because it is finally being cleaned up. 109 */ 110int ext4_orphan_del(handle_t *handle, struct inode *inode) 111{ 112 struct list_head *prev; 113 struct ext4_inode_info *ei = EXT4_I(inode); 114 struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb); 115 __u32 ino_next; 116 struct ext4_iloc iloc; 117 int err = 0; 118 119 if (!sbi->s_journal && !(sbi->s_mount_state & EXT4_ORPHAN_FS)) 120 return 0; 121 122 WARN_ON_ONCE(!(inode->i_state & (I_NEW | I_FREEING)) && 123 !inode_is_locked(inode)); | 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 */ 199int 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 208 if (!sbi->s_journal && !(sbi->s_mount_state & EXT4_ORPHAN_FS)) 209 return 0; 210 211 WARN_ON_ONCE(!(inode->i_state & (I_NEW | I_FREEING)) && 212 !inode_is_locked(inode)); |
213 if (ext4_test_inode_state(inode, EXT4_STATE_ORPHAN_FILE)) 214 return ext4_orphan_file_del(handle, inode); 215 |
|
124 /* Do this quick check before taking global s_orphan_lock. */ 125 if (list_empty(&ei->i_orphan)) 126 return 0; 127 128 if (handle) { 129 /* Grab inode buffer early before taking global s_orphan_lock */ 130 err = ext4_reserve_inode_write(handle, inode, &iloc); 131 } --- 63 unchanged lines hidden (view full) --- 195{ 196 return dquot_quota_on_mount(sb, 197 rcu_dereference_protected(EXT4_SB(sb)->s_qf_names[type], 198 lockdep_is_held(&sb->s_umount)), 199 EXT4_SB(sb)->s_jquota_fmt, type); 200} 201#endif 202 | 216 /* Do this quick check before taking global s_orphan_lock. */ 217 if (list_empty(&ei->i_orphan)) 218 return 0; 219 220 if (handle) { 221 /* Grab inode buffer early before taking global s_orphan_lock */ 222 err = ext4_reserve_inode_write(handle, inode, &iloc); 223 } --- 63 unchanged lines hidden (view full) --- 287{ 288 return dquot_quota_on_mount(sb, 289 rcu_dereference_protected(EXT4_SB(sb)->s_qf_names[type], 290 lockdep_is_held(&sb->s_umount)), 291 EXT4_SB(sb)->s_jquota_fmt, type); 292} 293#endif 294 |
295static void ext4_process_orphan(struct inode *inode, 296 int *nr_truncates, int *nr_orphans) 297{ 298 struct super_block *sb = inode->i_sb; 299 int ret; 300 301 dquot_initialize(inode); 302 if (inode->i_nlink) { 303 if (test_opt(sb, DEBUG)) 304 ext4_msg(sb, KERN_DEBUG, 305 "%s: truncating inode %lu to %lld bytes", 306 __func__, inode->i_ino, inode->i_size); 307 jbd_debug(2, "truncating inode %lu to %lld bytes\n", 308 inode->i_ino, inode->i_size); 309 inode_lock(inode); 310 truncate_inode_pages(inode->i_mapping, inode->i_size); 311 ret = ext4_truncate(inode); 312 if (ret) { 313 /* 314 * We need to clean up the in-core orphan list 315 * manually if ext4_truncate() failed to get a 316 * transaction handle. 317 */ 318 ext4_orphan_del(NULL, inode); 319 ext4_std_error(inode->i_sb, ret); 320 } 321 inode_unlock(inode); 322 (*nr_truncates)++; 323 } else { 324 if (test_opt(sb, DEBUG)) 325 ext4_msg(sb, KERN_DEBUG, 326 "%s: deleting unreferenced inode %lu", 327 __func__, inode->i_ino); 328 jbd_debug(2, "deleting unreferenced inode %lu\n", 329 inode->i_ino); 330 (*nr_orphans)++; 331 } 332 iput(inode); /* The delete magic happens here! */ 333} 334 |
|
203/* ext4_orphan_cleanup() walks a singly-linked list of inodes (starting at 204 * the superblock) which were deleted from all directories, but held open by 205 * a process at the time of a crash. We walk the list and try to delete these 206 * inodes at recovery time (only with a read-write filesystem). 207 * 208 * In order to keep the orphan inode chain consistent during traversal (in 209 * case of crash during recovery), we link each inode into the superblock 210 * orphan list_head and handle it the same way as an inode deletion during --- 4 unchanged lines hidden (view full) --- 215 * can happen in this case is that we get a "bit already cleared" message from 216 * ext4_free_inode(). The only reason we would point at a wrong inode is if 217 * e2fsck was run on this filesystem, and it must have already done the orphan 218 * inode cleanup for us, so we can safely abort without any further action. 219 */ 220void ext4_orphan_cleanup(struct super_block *sb, struct ext4_super_block *es) 221{ 222 unsigned int s_flags = sb->s_flags; | 335/* ext4_orphan_cleanup() walks a singly-linked list of inodes (starting at 336 * the superblock) which were deleted from all directories, but held open by 337 * a process at the time of a crash. We walk the list and try to delete these 338 * inodes at recovery time (only with a read-write filesystem). 339 * 340 * In order to keep the orphan inode chain consistent during traversal (in 341 * case of crash during recovery), we link each inode into the superblock 342 * orphan list_head and handle it the same way as an inode deletion during --- 4 unchanged lines hidden (view full) --- 347 * can happen in this case is that we get a "bit already cleared" message from 348 * ext4_free_inode(). The only reason we would point at a wrong inode is if 349 * e2fsck was run on this filesystem, and it must have already done the orphan 350 * inode cleanup for us, so we can safely abort without any further action. 351 */ 352void ext4_orphan_cleanup(struct super_block *sb, struct ext4_super_block *es) 353{ 354 unsigned int s_flags = sb->s_flags; |
223 int ret, nr_orphans = 0, nr_truncates = 0; | 355 int nr_orphans = 0, nr_truncates = 0; 356 struct inode *inode; 357 int i, j; |
224#ifdef CONFIG_QUOTA 225 int quota_update = 0; | 358#ifdef CONFIG_QUOTA 359 int quota_update = 0; |
226 int i; | |
227#endif | 360#endif |
228 if (!es->s_last_orphan) { | 361 __le32 *bdata; 362 struct ext4_orphan_info *oi = &EXT4_SB(sb)->s_orphan_info; 363 int inodes_per_ob = ext4_inodes_per_orphan_block(sb); 364 365 if (!es->s_last_orphan && !oi->of_blocks) { |
229 jbd_debug(4, "no orphan inodes to clean up\n"); 230 return; 231 } 232 233 if (bdev_read_only(sb->s_bdev)) { 234 ext4_msg(sb, KERN_ERR, "write access " 235 "unavailable, skipping orphan cleanup"); 236 return; --- 47 unchanged lines hidden (view full) --- 284 ext4_msg(sb, KERN_ERR, 285 "Cannot turn on journaled " 286 "quota: type %d: error %d", i, ret); 287 } 288 } 289#endif 290 291 while (es->s_last_orphan) { | 366 jbd_debug(4, "no orphan inodes to clean up\n"); 367 return; 368 } 369 370 if (bdev_read_only(sb->s_bdev)) { 371 ext4_msg(sb, KERN_ERR, "write access " 372 "unavailable, skipping orphan cleanup"); 373 return; --- 47 unchanged lines hidden (view full) --- 421 ext4_msg(sb, KERN_ERR, 422 "Cannot turn on journaled " 423 "quota: type %d: error %d", i, ret); 424 } 425 } 426#endif 427 428 while (es->s_last_orphan) { |
292 struct inode *inode; 293 | |
294 /* 295 * We may have encountered an error during cleanup; if 296 * so, skip the rest. 297 */ 298 if (EXT4_SB(sb)->s_mount_state & EXT4_ERROR_FS) { 299 jbd_debug(1, "Skipping orphan recovery on fs with errors.\n"); 300 es->s_last_orphan = 0; 301 break; 302 } 303 304 inode = ext4_orphan_get(sb, le32_to_cpu(es->s_last_orphan)); 305 if (IS_ERR(inode)) { 306 es->s_last_orphan = 0; 307 break; 308 } 309 310 list_add(&EXT4_I(inode)->i_orphan, &EXT4_SB(sb)->s_orphan); | 429 /* 430 * We may have encountered an error during cleanup; if 431 * so, skip the rest. 432 */ 433 if (EXT4_SB(sb)->s_mount_state & EXT4_ERROR_FS) { 434 jbd_debug(1, "Skipping orphan recovery on fs with errors.\n"); 435 es->s_last_orphan = 0; 436 break; 437 } 438 439 inode = ext4_orphan_get(sb, le32_to_cpu(es->s_last_orphan)); 440 if (IS_ERR(inode)) { 441 es->s_last_orphan = 0; 442 break; 443 } 444 445 list_add(&EXT4_I(inode)->i_orphan, &EXT4_SB(sb)->s_orphan); |
311 dquot_initialize(inode); 312 if (inode->i_nlink) { 313 if (test_opt(sb, DEBUG)) 314 ext4_msg(sb, KERN_DEBUG, 315 "%s: truncating inode %lu to %lld bytes", 316 __func__, inode->i_ino, inode->i_size); 317 jbd_debug(2, "truncating inode %lu to %lld bytes\n", 318 inode->i_ino, inode->i_size); 319 inode_lock(inode); 320 truncate_inode_pages(inode->i_mapping, inode->i_size); 321 ret = ext4_truncate(inode); 322 if (ret) { 323 /* 324 * We need to clean up the in-core orphan list 325 * manually if ext4_truncate() failed to get a 326 * transaction handle. 327 */ 328 ext4_orphan_del(NULL, inode); 329 ext4_std_error(inode->i_sb, ret); 330 } 331 inode_unlock(inode); 332 nr_truncates++; 333 } else { 334 if (test_opt(sb, DEBUG)) 335 ext4_msg(sb, KERN_DEBUG, 336 "%s: deleting unreferenced inode %lu", 337 __func__, inode->i_ino); 338 jbd_debug(2, "deleting unreferenced inode %lu\n", 339 inode->i_ino); 340 nr_orphans++; | 446 ext4_process_orphan(inode, &nr_truncates, &nr_orphans); 447 } 448 449 for (i = 0; i < oi->of_blocks; i++) { 450 bdata = (__le32 *)(oi->of_binfo[i].ob_bh->b_data); 451 for (j = 0; j < inodes_per_ob; j++) { 452 if (!bdata[j]) 453 continue; 454 inode = ext4_orphan_get(sb, le32_to_cpu(bdata[j])); 455 if (IS_ERR(inode)) 456 continue; 457 ext4_set_inode_state(inode, EXT4_STATE_ORPHAN_FILE); 458 EXT4_I(inode)->i_orphan_idx = i * inodes_per_ob + j; 459 ext4_process_orphan(inode, &nr_truncates, &nr_orphans); |
341 } | 460 } |
342 iput(inode); /* The delete magic happens here! */ | |
343 } 344 345#define PLURAL(x) (x), ((x) == 1) ? "" : "s" 346 347 if (nr_orphans) 348 ext4_msg(sb, KERN_INFO, "%d orphan inode%s deleted", 349 PLURAL(nr_orphans)); 350 if (nr_truncates) --- 5 unchanged lines hidden (view full) --- 356 for (i = 0; i < EXT4_MAXQUOTAS; i++) { 357 if (sb_dqopt(sb)->files[i]) 358 dquot_quota_off(sb, i); 359 } 360 } 361#endif 362 sb->s_flags = s_flags; /* Restore SB_RDONLY status */ 363} | 461 } 462 463#define PLURAL(x) (x), ((x) == 1) ? "" : "s" 464 465 if (nr_orphans) 466 ext4_msg(sb, KERN_INFO, "%d orphan inode%s deleted", 467 PLURAL(nr_orphans)); 468 if (nr_truncates) --- 5 unchanged lines hidden (view full) --- 474 for (i = 0; i < EXT4_MAXQUOTAS; i++) { 475 if (sb_dqopt(sb)->files[i]) 476 dquot_quota_off(sb, i); 477 } 478 } 479#endif 480 sb->s_flags = s_flags; /* Restore SB_RDONLY status */ 481} |
482 483void ext4_release_orphan_info(struct super_block *sb) 484{ 485 int i; 486 struct ext4_orphan_info *oi = &EXT4_SB(sb)->s_orphan_info; 487 488 if (!oi->of_blocks) 489 return; 490 for (i = 0; i < oi->of_blocks; i++) 491 brelse(oi->of_binfo[i].ob_bh); 492 kfree(oi->of_binfo); 493} 494 495static struct ext4_orphan_block_tail *ext4_orphan_block_tail( 496 struct super_block *sb, 497 struct buffer_head *bh) 498{ 499 return (struct ext4_orphan_block_tail *)(bh->b_data + sb->s_blocksize - 500 sizeof(struct ext4_orphan_block_tail)); 501} 502 503static int ext4_orphan_file_block_csum_verify(struct super_block *sb, 504 struct buffer_head *bh) 505{ 506 __u32 calculated; 507 int inodes_per_ob = ext4_inodes_per_orphan_block(sb); 508 struct ext4_orphan_info *oi = &EXT4_SB(sb)->s_orphan_info; 509 struct ext4_orphan_block_tail *ot; 510 __le64 dsk_block_nr = cpu_to_le64(bh->b_blocknr); 511 512 if (!ext4_has_metadata_csum(sb)) 513 return 1; 514 515 ot = ext4_orphan_block_tail(sb, bh); 516 calculated = ext4_chksum(EXT4_SB(sb), oi->of_csum_seed, 517 (__u8 *)&dsk_block_nr, sizeof(dsk_block_nr)); 518 calculated = ext4_chksum(EXT4_SB(sb), calculated, (__u8 *)bh->b_data, 519 inodes_per_ob * sizeof(__u32)); 520 return le32_to_cpu(ot->ob_checksum) == calculated; 521} 522 523/* This gets called only when checksumming is enabled */ 524void ext4_orphan_file_block_trigger(struct jbd2_buffer_trigger_type *triggers, 525 struct buffer_head *bh, 526 void *data, size_t size) 527{ 528 struct super_block *sb = EXT4_TRIGGER(triggers)->sb; 529 __u32 csum; 530 int inodes_per_ob = ext4_inodes_per_orphan_block(sb); 531 struct ext4_orphan_info *oi = &EXT4_SB(sb)->s_orphan_info; 532 struct ext4_orphan_block_tail *ot; 533 __le64 dsk_block_nr = cpu_to_le64(bh->b_blocknr); 534 535 csum = ext4_chksum(EXT4_SB(sb), oi->of_csum_seed, 536 (__u8 *)&dsk_block_nr, sizeof(dsk_block_nr)); 537 csum = ext4_chksum(EXT4_SB(sb), csum, (__u8 *)data, 538 inodes_per_ob * sizeof(__u32)); 539 ot = ext4_orphan_block_tail(sb, bh); 540 ot->ob_checksum = cpu_to_le32(csum); 541} 542 543int ext4_init_orphan_info(struct super_block *sb) 544{ 545 struct ext4_orphan_info *oi = &EXT4_SB(sb)->s_orphan_info; 546 struct inode *inode; 547 int i, j; 548 int ret; 549 int free; 550 __le32 *bdata; 551 int inodes_per_ob = ext4_inodes_per_orphan_block(sb); 552 struct ext4_orphan_block_tail *ot; 553 ino_t orphan_ino = le32_to_cpu(EXT4_SB(sb)->s_es->s_orphan_file_inum); 554 555 spin_lock_init(&oi->of_lock); 556 557 if (!ext4_has_feature_orphan_file(sb)) 558 return 0; 559 560 inode = ext4_iget(sb, orphan_ino, EXT4_IGET_SPECIAL); 561 if (IS_ERR(inode)) { 562 ext4_msg(sb, KERN_ERR, "get orphan inode failed"); 563 return PTR_ERR(inode); 564 } 565 oi->of_blocks = inode->i_size >> sb->s_blocksize_bits; 566 oi->of_csum_seed = EXT4_I(inode)->i_csum_seed; 567 oi->of_binfo = kmalloc(oi->of_blocks*sizeof(struct ext4_orphan_block), 568 GFP_KERNEL); 569 if (!oi->of_binfo) { 570 ret = -ENOMEM; 571 goto out_put; 572 } 573 for (i = 0; i < oi->of_blocks; i++) { 574 oi->of_binfo[i].ob_bh = ext4_bread(NULL, inode, i, 0); 575 if (IS_ERR(oi->of_binfo[i].ob_bh)) { 576 ret = PTR_ERR(oi->of_binfo[i].ob_bh); 577 goto out_free; 578 } 579 if (!oi->of_binfo[i].ob_bh) { 580 ret = -EIO; 581 goto out_free; 582 } 583 ot = ext4_orphan_block_tail(sb, oi->of_binfo[i].ob_bh); 584 if (le32_to_cpu(ot->ob_magic) != EXT4_ORPHAN_BLOCK_MAGIC) { 585 ext4_error(sb, "orphan file block %d: bad magic", i); 586 ret = -EIO; 587 goto out_free; 588 } 589 if (!ext4_orphan_file_block_csum_verify(sb, 590 oi->of_binfo[i].ob_bh)) { 591 ext4_error(sb, "orphan file block %d: bad checksum", i); 592 ret = -EIO; 593 goto out_free; 594 } 595 bdata = (__le32 *)(oi->of_binfo[i].ob_bh->b_data); 596 free = 0; 597 for (j = 0; j < inodes_per_ob; j++) 598 if (bdata[j] == 0) 599 free++; 600 oi->of_binfo[i].ob_free_entries = free; 601 } 602 iput(inode); 603 return 0; 604out_free: 605 for (i--; i >= 0; i--) 606 brelse(oi->of_binfo[i].ob_bh); 607 kfree(oi->of_binfo); 608out_put: 609 iput(inode); 610 return ret; 611} 612 613int ext4_orphan_file_empty(struct super_block *sb) 614{ 615 struct ext4_orphan_info *oi = &EXT4_SB(sb)->s_orphan_info; 616 int i; 617 int inodes_per_ob = ext4_inodes_per_orphan_block(sb); 618 619 if (!ext4_has_feature_orphan_file(sb)) 620 return 1; 621 for (i = 0; i < oi->of_blocks; i++) 622 if (oi->of_binfo[i].ob_free_entries != inodes_per_ob) 623 return 0; 624 return 1; 625} |
|