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