0001
0002
0003
0004 #include <linux/fs.h>
0005 #include <linux/quotaops.h>
0006 #include <linux/buffer_head.h>
0007
0008 #include "ext4.h"
0009 #include "ext4_jbd2.h"
0010
0011 static int ext4_orphan_file_add(handle_t *handle, struct inode *inode)
0012 {
0013 int i, j, start;
0014 struct ext4_orphan_info *oi = &EXT4_SB(inode->i_sb)->s_orphan_info;
0015 int ret = 0;
0016 bool found = false;
0017 __le32 *bdata;
0018 int inodes_per_ob = ext4_inodes_per_orphan_block(inode->i_sb);
0019 int looped = 0;
0020
0021
0022
0023
0024
0025 start = raw_smp_processor_id()*13 % oi->of_blocks;
0026 i = start;
0027 do {
0028 if (atomic_dec_if_positive(&oi->of_binfo[i].ob_free_entries)
0029 >= 0) {
0030 found = true;
0031 break;
0032 }
0033 if (++i >= oi->of_blocks)
0034 i = 0;
0035 } while (i != start);
0036
0037 if (!found) {
0038
0039
0040
0041
0042
0043
0044 return -ENOSPC;
0045 }
0046
0047 ret = ext4_journal_get_write_access(handle, inode->i_sb,
0048 oi->of_binfo[i].ob_bh, EXT4_JTR_ORPHAN_FILE);
0049 if (ret) {
0050 atomic_inc(&oi->of_binfo[i].ob_free_entries);
0051 return ret;
0052 }
0053
0054 bdata = (__le32 *)(oi->of_binfo[i].ob_bh->b_data);
0055
0056 j = 0;
0057 do {
0058 if (looped) {
0059
0060
0061
0062
0063
0064
0065
0066 if (looped > 3) {
0067 atomic_inc(&oi->of_binfo[i].ob_free_entries);
0068 return -ENOSPC;
0069 }
0070 cond_resched();
0071 }
0072 while (bdata[j]) {
0073 if (++j >= inodes_per_ob) {
0074 j = 0;
0075 looped++;
0076 }
0077 }
0078 } while (cmpxchg(&bdata[j], (__le32)0, cpu_to_le32(inode->i_ino)) !=
0079 (__le32)0);
0080
0081 EXT4_I(inode)->i_orphan_idx = i * inodes_per_ob + j;
0082 ext4_set_inode_state(inode, EXT4_STATE_ORPHAN_FILE);
0083
0084 return ext4_handle_dirty_metadata(handle, NULL, oi->of_binfo[i].ob_bh);
0085 }
0086
0087
0088
0089
0090
0091
0092
0093
0094
0095
0096
0097
0098
0099 int ext4_orphan_add(handle_t *handle, struct inode *inode)
0100 {
0101 struct super_block *sb = inode->i_sb;
0102 struct ext4_sb_info *sbi = EXT4_SB(sb);
0103 struct ext4_iloc iloc;
0104 int err = 0, rc;
0105 bool dirty = false;
0106
0107 if (!sbi->s_journal || is_bad_inode(inode))
0108 return 0;
0109
0110 WARN_ON_ONCE(!(inode->i_state & (I_NEW | I_FREEING)) &&
0111 !inode_is_locked(inode));
0112
0113
0114
0115 if (ext4_test_inode_state(inode, EXT4_STATE_ORPHAN_FILE) ||
0116 !list_empty(&EXT4_I(inode)->i_orphan))
0117 return 0;
0118
0119
0120
0121
0122
0123
0124
0125 ASSERT((S_ISREG(inode->i_mode) || S_ISDIR(inode->i_mode) ||
0126 S_ISLNK(inode->i_mode)) || inode->i_nlink == 0);
0127
0128 if (sbi->s_orphan_info.of_blocks) {
0129 err = ext4_orphan_file_add(handle, inode);
0130
0131
0132
0133
0134 if (err != -ENOSPC)
0135 return err;
0136 }
0137
0138 BUFFER_TRACE(sbi->s_sbh, "get_write_access");
0139 err = ext4_journal_get_write_access(handle, sb, sbi->s_sbh,
0140 EXT4_JTR_NONE);
0141 if (err)
0142 goto out;
0143
0144 err = ext4_reserve_inode_write(handle, inode, &iloc);
0145 if (err)
0146 goto out;
0147
0148 mutex_lock(&sbi->s_orphan_lock);
0149
0150
0151
0152
0153 if (!NEXT_ORPHAN(inode) || NEXT_ORPHAN(inode) >
0154 (le32_to_cpu(sbi->s_es->s_inodes_count))) {
0155
0156 NEXT_ORPHAN(inode) = le32_to_cpu(sbi->s_es->s_last_orphan);
0157 lock_buffer(sbi->s_sbh);
0158 sbi->s_es->s_last_orphan = cpu_to_le32(inode->i_ino);
0159 ext4_superblock_csum_set(sb);
0160 unlock_buffer(sbi->s_sbh);
0161 dirty = true;
0162 }
0163 list_add(&EXT4_I(inode)->i_orphan, &sbi->s_orphan);
0164 mutex_unlock(&sbi->s_orphan_lock);
0165
0166 if (dirty) {
0167 err = ext4_handle_dirty_metadata(handle, NULL, sbi->s_sbh);
0168 rc = ext4_mark_iloc_dirty(handle, inode, &iloc);
0169 if (!err)
0170 err = rc;
0171 if (err) {
0172
0173
0174
0175
0176
0177 mutex_lock(&sbi->s_orphan_lock);
0178 list_del_init(&EXT4_I(inode)->i_orphan);
0179 mutex_unlock(&sbi->s_orphan_lock);
0180 }
0181 } else
0182 brelse(iloc.bh);
0183
0184 ext4_debug("superblock will point to %lu\n", inode->i_ino);
0185 ext4_debug("orphan inode %lu will point to %d\n",
0186 inode->i_ino, NEXT_ORPHAN(inode));
0187 out:
0188 ext4_std_error(sb, err);
0189 return err;
0190 }
0191
0192 static int ext4_orphan_file_del(handle_t *handle, struct inode *inode)
0193 {
0194 struct ext4_orphan_info *oi = &EXT4_SB(inode->i_sb)->s_orphan_info;
0195 __le32 *bdata;
0196 int blk, off;
0197 int inodes_per_ob = ext4_inodes_per_orphan_block(inode->i_sb);
0198 int ret = 0;
0199
0200 if (!handle)
0201 goto out;
0202 blk = EXT4_I(inode)->i_orphan_idx / inodes_per_ob;
0203 off = EXT4_I(inode)->i_orphan_idx % inodes_per_ob;
0204 if (WARN_ON_ONCE(blk >= oi->of_blocks))
0205 goto out;
0206
0207 ret = ext4_journal_get_write_access(handle, inode->i_sb,
0208 oi->of_binfo[blk].ob_bh, EXT4_JTR_ORPHAN_FILE);
0209 if (ret)
0210 goto out;
0211
0212 bdata = (__le32 *)(oi->of_binfo[blk].ob_bh->b_data);
0213 bdata[off] = 0;
0214 atomic_inc(&oi->of_binfo[blk].ob_free_entries);
0215 ret = ext4_handle_dirty_metadata(handle, NULL, oi->of_binfo[blk].ob_bh);
0216 out:
0217 ext4_clear_inode_state(inode, EXT4_STATE_ORPHAN_FILE);
0218 INIT_LIST_HEAD(&EXT4_I(inode)->i_orphan);
0219
0220 return ret;
0221 }
0222
0223
0224
0225
0226
0227 int ext4_orphan_del(handle_t *handle, struct inode *inode)
0228 {
0229 struct list_head *prev;
0230 struct ext4_inode_info *ei = EXT4_I(inode);
0231 struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb);
0232 __u32 ino_next;
0233 struct ext4_iloc iloc;
0234 int err = 0;
0235
0236 if (!sbi->s_journal && !(sbi->s_mount_state & EXT4_ORPHAN_FS))
0237 return 0;
0238
0239 WARN_ON_ONCE(!(inode->i_state & (I_NEW | I_FREEING)) &&
0240 !inode_is_locked(inode));
0241 if (ext4_test_inode_state(inode, EXT4_STATE_ORPHAN_FILE))
0242 return ext4_orphan_file_del(handle, inode);
0243
0244
0245 if (list_empty(&ei->i_orphan))
0246 return 0;
0247
0248 if (handle) {
0249
0250 err = ext4_reserve_inode_write(handle, inode, &iloc);
0251 }
0252
0253 mutex_lock(&sbi->s_orphan_lock);
0254 ext4_debug("remove inode %lu from orphan list\n", inode->i_ino);
0255
0256 prev = ei->i_orphan.prev;
0257 list_del_init(&ei->i_orphan);
0258
0259
0260
0261
0262
0263 if (!handle || err) {
0264 mutex_unlock(&sbi->s_orphan_lock);
0265 goto out_err;
0266 }
0267
0268 ino_next = NEXT_ORPHAN(inode);
0269 if (prev == &sbi->s_orphan) {
0270 ext4_debug("superblock will point to %u\n", ino_next);
0271 BUFFER_TRACE(sbi->s_sbh, "get_write_access");
0272 err = ext4_journal_get_write_access(handle, inode->i_sb,
0273 sbi->s_sbh, EXT4_JTR_NONE);
0274 if (err) {
0275 mutex_unlock(&sbi->s_orphan_lock);
0276 goto out_brelse;
0277 }
0278 lock_buffer(sbi->s_sbh);
0279 sbi->s_es->s_last_orphan = cpu_to_le32(ino_next);
0280 ext4_superblock_csum_set(inode->i_sb);
0281 unlock_buffer(sbi->s_sbh);
0282 mutex_unlock(&sbi->s_orphan_lock);
0283 err = ext4_handle_dirty_metadata(handle, NULL, sbi->s_sbh);
0284 } else {
0285 struct ext4_iloc iloc2;
0286 struct inode *i_prev =
0287 &list_entry(prev, struct ext4_inode_info, i_orphan)->vfs_inode;
0288
0289 ext4_debug("orphan inode %lu will point to %u\n",
0290 i_prev->i_ino, ino_next);
0291 err = ext4_reserve_inode_write(handle, i_prev, &iloc2);
0292 if (err) {
0293 mutex_unlock(&sbi->s_orphan_lock);
0294 goto out_brelse;
0295 }
0296 NEXT_ORPHAN(i_prev) = ino_next;
0297 err = ext4_mark_iloc_dirty(handle, i_prev, &iloc2);
0298 mutex_unlock(&sbi->s_orphan_lock);
0299 }
0300 if (err)
0301 goto out_brelse;
0302 NEXT_ORPHAN(inode) = 0;
0303 err = ext4_mark_iloc_dirty(handle, inode, &iloc);
0304 out_err:
0305 ext4_std_error(inode->i_sb, err);
0306 return err;
0307
0308 out_brelse:
0309 brelse(iloc.bh);
0310 goto out_err;
0311 }
0312
0313 #ifdef CONFIG_QUOTA
0314 static int ext4_quota_on_mount(struct super_block *sb, int type)
0315 {
0316 return dquot_quota_on_mount(sb,
0317 rcu_dereference_protected(EXT4_SB(sb)->s_qf_names[type],
0318 lockdep_is_held(&sb->s_umount)),
0319 EXT4_SB(sb)->s_jquota_fmt, type);
0320 }
0321 #endif
0322
0323 static void ext4_process_orphan(struct inode *inode,
0324 int *nr_truncates, int *nr_orphans)
0325 {
0326 struct super_block *sb = inode->i_sb;
0327 int ret;
0328
0329 dquot_initialize(inode);
0330 if (inode->i_nlink) {
0331 if (test_opt(sb, DEBUG))
0332 ext4_msg(sb, KERN_DEBUG,
0333 "%s: truncating inode %lu to %lld bytes",
0334 __func__, inode->i_ino, inode->i_size);
0335 ext4_debug("truncating inode %lu to %lld bytes\n",
0336 inode->i_ino, inode->i_size);
0337 inode_lock(inode);
0338 truncate_inode_pages(inode->i_mapping, inode->i_size);
0339 ret = ext4_truncate(inode);
0340 if (ret) {
0341
0342
0343
0344
0345
0346 ext4_orphan_del(NULL, inode);
0347 ext4_std_error(inode->i_sb, ret);
0348 }
0349 inode_unlock(inode);
0350 (*nr_truncates)++;
0351 } else {
0352 if (test_opt(sb, DEBUG))
0353 ext4_msg(sb, KERN_DEBUG,
0354 "%s: deleting unreferenced inode %lu",
0355 __func__, inode->i_ino);
0356 ext4_debug("deleting unreferenced inode %lu\n",
0357 inode->i_ino);
0358 (*nr_orphans)++;
0359 }
0360 iput(inode);
0361 }
0362
0363
0364
0365
0366
0367
0368
0369
0370
0371
0372
0373
0374
0375
0376
0377
0378
0379
0380 void ext4_orphan_cleanup(struct super_block *sb, struct ext4_super_block *es)
0381 {
0382 unsigned int s_flags = sb->s_flags;
0383 int nr_orphans = 0, nr_truncates = 0;
0384 struct inode *inode;
0385 int i, j;
0386 #ifdef CONFIG_QUOTA
0387 int quota_update = 0;
0388 #endif
0389 __le32 *bdata;
0390 struct ext4_orphan_info *oi = &EXT4_SB(sb)->s_orphan_info;
0391 int inodes_per_ob = ext4_inodes_per_orphan_block(sb);
0392
0393 if (!es->s_last_orphan && !oi->of_blocks) {
0394 ext4_debug("no orphan inodes to clean up\n");
0395 return;
0396 }
0397
0398 if (bdev_read_only(sb->s_bdev)) {
0399 ext4_msg(sb, KERN_ERR, "write access "
0400 "unavailable, skipping orphan cleanup");
0401 return;
0402 }
0403
0404
0405 if (!ext4_feature_set_ok(sb, 0)) {
0406 ext4_msg(sb, KERN_INFO, "Skipping orphan cleanup due to "
0407 "unknown ROCOMPAT features");
0408 return;
0409 }
0410
0411 if (EXT4_SB(sb)->s_mount_state & EXT4_ERROR_FS) {
0412
0413 if (es->s_last_orphan && !(s_flags & SB_RDONLY)) {
0414 ext4_msg(sb, KERN_INFO, "Errors on filesystem, "
0415 "clearing orphan list.\n");
0416 es->s_last_orphan = 0;
0417 }
0418 ext4_debug("Skipping orphan recovery on fs with errors.\n");
0419 return;
0420 }
0421
0422 if (s_flags & SB_RDONLY) {
0423 ext4_msg(sb, KERN_INFO, "orphan cleanup on readonly fs");
0424 sb->s_flags &= ~SB_RDONLY;
0425 }
0426 #ifdef CONFIG_QUOTA
0427
0428
0429
0430
0431 if (ext4_has_feature_quota(sb) && (s_flags & SB_RDONLY)) {
0432 int ret = ext4_enable_quotas(sb);
0433
0434 if (!ret)
0435 quota_update = 1;
0436 else
0437 ext4_msg(sb, KERN_ERR,
0438 "Cannot turn on quotas: error %d", ret);
0439 }
0440
0441
0442 for (i = 0; i < EXT4_MAXQUOTAS; i++) {
0443 if (EXT4_SB(sb)->s_qf_names[i]) {
0444 int ret = ext4_quota_on_mount(sb, i);
0445
0446 if (!ret)
0447 quota_update = 1;
0448 else
0449 ext4_msg(sb, KERN_ERR,
0450 "Cannot turn on journaled "
0451 "quota: type %d: error %d", i, ret);
0452 }
0453 }
0454 #endif
0455
0456 while (es->s_last_orphan) {
0457
0458
0459
0460
0461 if (EXT4_SB(sb)->s_mount_state & EXT4_ERROR_FS) {
0462 ext4_debug("Skipping orphan recovery on fs with errors.\n");
0463 es->s_last_orphan = 0;
0464 break;
0465 }
0466
0467 inode = ext4_orphan_get(sb, le32_to_cpu(es->s_last_orphan));
0468 if (IS_ERR(inode)) {
0469 es->s_last_orphan = 0;
0470 break;
0471 }
0472
0473 list_add(&EXT4_I(inode)->i_orphan, &EXT4_SB(sb)->s_orphan);
0474 ext4_process_orphan(inode, &nr_truncates, &nr_orphans);
0475 }
0476
0477 for (i = 0; i < oi->of_blocks; i++) {
0478 bdata = (__le32 *)(oi->of_binfo[i].ob_bh->b_data);
0479 for (j = 0; j < inodes_per_ob; j++) {
0480 if (!bdata[j])
0481 continue;
0482 inode = ext4_orphan_get(sb, le32_to_cpu(bdata[j]));
0483 if (IS_ERR(inode))
0484 continue;
0485 ext4_set_inode_state(inode, EXT4_STATE_ORPHAN_FILE);
0486 EXT4_I(inode)->i_orphan_idx = i * inodes_per_ob + j;
0487 ext4_process_orphan(inode, &nr_truncates, &nr_orphans);
0488 }
0489 }
0490
0491 #define PLURAL(x) (x), ((x) == 1) ? "" : "s"
0492
0493 if (nr_orphans)
0494 ext4_msg(sb, KERN_INFO, "%d orphan inode%s deleted",
0495 PLURAL(nr_orphans));
0496 if (nr_truncates)
0497 ext4_msg(sb, KERN_INFO, "%d truncate%s cleaned up",
0498 PLURAL(nr_truncates));
0499 #ifdef CONFIG_QUOTA
0500
0501 if (quota_update) {
0502 for (i = 0; i < EXT4_MAXQUOTAS; i++) {
0503 if (sb_dqopt(sb)->files[i])
0504 dquot_quota_off(sb, i);
0505 }
0506 }
0507 #endif
0508 sb->s_flags = s_flags;
0509 }
0510
0511 void ext4_release_orphan_info(struct super_block *sb)
0512 {
0513 int i;
0514 struct ext4_orphan_info *oi = &EXT4_SB(sb)->s_orphan_info;
0515
0516 if (!oi->of_blocks)
0517 return;
0518 for (i = 0; i < oi->of_blocks; i++)
0519 brelse(oi->of_binfo[i].ob_bh);
0520 kfree(oi->of_binfo);
0521 }
0522
0523 static struct ext4_orphan_block_tail *ext4_orphan_block_tail(
0524 struct super_block *sb,
0525 struct buffer_head *bh)
0526 {
0527 return (struct ext4_orphan_block_tail *)(bh->b_data + sb->s_blocksize -
0528 sizeof(struct ext4_orphan_block_tail));
0529 }
0530
0531 static int ext4_orphan_file_block_csum_verify(struct super_block *sb,
0532 struct buffer_head *bh)
0533 {
0534 __u32 calculated;
0535 int inodes_per_ob = ext4_inodes_per_orphan_block(sb);
0536 struct ext4_orphan_info *oi = &EXT4_SB(sb)->s_orphan_info;
0537 struct ext4_orphan_block_tail *ot;
0538 __le64 dsk_block_nr = cpu_to_le64(bh->b_blocknr);
0539
0540 if (!ext4_has_metadata_csum(sb))
0541 return 1;
0542
0543 ot = ext4_orphan_block_tail(sb, bh);
0544 calculated = ext4_chksum(EXT4_SB(sb), oi->of_csum_seed,
0545 (__u8 *)&dsk_block_nr, sizeof(dsk_block_nr));
0546 calculated = ext4_chksum(EXT4_SB(sb), calculated, (__u8 *)bh->b_data,
0547 inodes_per_ob * sizeof(__u32));
0548 return le32_to_cpu(ot->ob_checksum) == calculated;
0549 }
0550
0551
0552 void ext4_orphan_file_block_trigger(struct jbd2_buffer_trigger_type *triggers,
0553 struct buffer_head *bh,
0554 void *data, size_t size)
0555 {
0556 struct super_block *sb = EXT4_TRIGGER(triggers)->sb;
0557 __u32 csum;
0558 int inodes_per_ob = ext4_inodes_per_orphan_block(sb);
0559 struct ext4_orphan_info *oi = &EXT4_SB(sb)->s_orphan_info;
0560 struct ext4_orphan_block_tail *ot;
0561 __le64 dsk_block_nr = cpu_to_le64(bh->b_blocknr);
0562
0563 csum = ext4_chksum(EXT4_SB(sb), oi->of_csum_seed,
0564 (__u8 *)&dsk_block_nr, sizeof(dsk_block_nr));
0565 csum = ext4_chksum(EXT4_SB(sb), csum, (__u8 *)data,
0566 inodes_per_ob * sizeof(__u32));
0567 ot = ext4_orphan_block_tail(sb, bh);
0568 ot->ob_checksum = cpu_to_le32(csum);
0569 }
0570
0571 int ext4_init_orphan_info(struct super_block *sb)
0572 {
0573 struct ext4_orphan_info *oi = &EXT4_SB(sb)->s_orphan_info;
0574 struct inode *inode;
0575 int i, j;
0576 int ret;
0577 int free;
0578 __le32 *bdata;
0579 int inodes_per_ob = ext4_inodes_per_orphan_block(sb);
0580 struct ext4_orphan_block_tail *ot;
0581 ino_t orphan_ino = le32_to_cpu(EXT4_SB(sb)->s_es->s_orphan_file_inum);
0582
0583 if (!ext4_has_feature_orphan_file(sb))
0584 return 0;
0585
0586 inode = ext4_iget(sb, orphan_ino, EXT4_IGET_SPECIAL);
0587 if (IS_ERR(inode)) {
0588 ext4_msg(sb, KERN_ERR, "get orphan inode failed");
0589 return PTR_ERR(inode);
0590 }
0591 oi->of_blocks = inode->i_size >> sb->s_blocksize_bits;
0592 oi->of_csum_seed = EXT4_I(inode)->i_csum_seed;
0593 oi->of_binfo = kmalloc(oi->of_blocks*sizeof(struct ext4_orphan_block),
0594 GFP_KERNEL);
0595 if (!oi->of_binfo) {
0596 ret = -ENOMEM;
0597 goto out_put;
0598 }
0599 for (i = 0; i < oi->of_blocks; i++) {
0600 oi->of_binfo[i].ob_bh = ext4_bread(NULL, inode, i, 0);
0601 if (IS_ERR(oi->of_binfo[i].ob_bh)) {
0602 ret = PTR_ERR(oi->of_binfo[i].ob_bh);
0603 goto out_free;
0604 }
0605 if (!oi->of_binfo[i].ob_bh) {
0606 ret = -EIO;
0607 goto out_free;
0608 }
0609 ot = ext4_orphan_block_tail(sb, oi->of_binfo[i].ob_bh);
0610 if (le32_to_cpu(ot->ob_magic) != EXT4_ORPHAN_BLOCK_MAGIC) {
0611 ext4_error(sb, "orphan file block %d: bad magic", i);
0612 ret = -EIO;
0613 goto out_free;
0614 }
0615 if (!ext4_orphan_file_block_csum_verify(sb,
0616 oi->of_binfo[i].ob_bh)) {
0617 ext4_error(sb, "orphan file block %d: bad checksum", i);
0618 ret = -EIO;
0619 goto out_free;
0620 }
0621 bdata = (__le32 *)(oi->of_binfo[i].ob_bh->b_data);
0622 free = 0;
0623 for (j = 0; j < inodes_per_ob; j++)
0624 if (bdata[j] == 0)
0625 free++;
0626 atomic_set(&oi->of_binfo[i].ob_free_entries, free);
0627 }
0628 iput(inode);
0629 return 0;
0630 out_free:
0631 for (i--; i >= 0; i--)
0632 brelse(oi->of_binfo[i].ob_bh);
0633 kfree(oi->of_binfo);
0634 out_put:
0635 iput(inode);
0636 return ret;
0637 }
0638
0639 int ext4_orphan_file_empty(struct super_block *sb)
0640 {
0641 struct ext4_orphan_info *oi = &EXT4_SB(sb)->s_orphan_info;
0642 int i;
0643 int inodes_per_ob = ext4_inodes_per_orphan_block(sb);
0644
0645 if (!ext4_has_feature_orphan_file(sb))
0646 return 1;
0647 for (i = 0; i < oi->of_blocks; i++)
0648 if (atomic_read(&oi->of_binfo[i].ob_free_entries) !=
0649 inodes_per_ob)
0650 return 0;
0651 return 1;
0652 }