inode.c (2ed886852adfcb070bf350e66a0da0d98b2f3ab5) inode.c (24676da469f50f433baa347845639662c561d1f6)
1/*
2 * linux/fs/ext4/inode.c
3 *
4 * Copyright (C) 1992, 1993, 1994, 1995
5 * Remy Card (card@masi.ibp.fr)
6 * Laboratoire MASI - Institut Blaise Pascal
7 * Universite Pierre et Marie Curie (Paris VI)
8 *

--- 334 unchanged lines hidden (view full) ---

343 __le32 *bref = p;
344 unsigned int blk;
345
346 while (bref < p+max) {
347 blk = le32_to_cpu(*bref++);
348 if (blk &&
349 unlikely(!ext4_data_block_valid(EXT4_SB(inode->i_sb),
350 blk, 1))) {
1/*
2 * linux/fs/ext4/inode.c
3 *
4 * Copyright (C) 1992, 1993, 1994, 1995
5 * Remy Card (card@masi.ibp.fr)
6 * Laboratoire MASI - Institut Blaise Pascal
7 * Universite Pierre et Marie Curie (Paris VI)
8 *

--- 334 unchanged lines hidden (view full) ---

343 __le32 *bref = p;
344 unsigned int blk;
345
346 while (bref < p+max) {
347 blk = le32_to_cpu(*bref++);
348 if (blk &&
349 unlikely(!ext4_data_block_valid(EXT4_SB(inode->i_sb),
350 blk, 1))) {
351 __ext4_error(inode->i_sb, function,
352 "invalid block reference %u "
353 "in inode #%lu", blk, inode->i_ino);
351 ext4_error_inode(function, inode,
352 "invalid block reference %u", blk);
354 return -EIO;
355 }
356 }
357 return 0;
358}
359
360
361#define ext4_check_indirect_blockref(inode, bh) \

--- 762 unchanged lines hidden (view full) ---

1124 * there aren't any writers on the inode, we can discard the
1125 * inode's preallocations.
1126 */
1127 if ((ei->i_reserved_data_blocks == 0) &&
1128 (atomic_read(&inode->i_writecount) == 0))
1129 ext4_discard_preallocations(inode);
1130}
1131
353 return -EIO;
354 }
355 }
356 return 0;
357}
358
359
360#define ext4_check_indirect_blockref(inode, bh) \

--- 762 unchanged lines hidden (view full) ---

1123 * there aren't any writers on the inode, we can discard the
1124 * inode's preallocations.
1125 */
1126 if ((ei->i_reserved_data_blocks == 0) &&
1127 (atomic_read(&inode->i_writecount) == 0))
1128 ext4_discard_preallocations(inode);
1129}
1130
1132static int check_block_validity(struct inode *inode, const char *msg,
1133 sector_t logical, sector_t phys, int len)
1131static int check_block_validity(struct inode *inode, const char *func,
1132 struct ext4_map_blocks *map)
1134{
1133{
1135 if (!ext4_data_block_valid(EXT4_SB(inode->i_sb), phys, len)) {
1136 __ext4_error(inode->i_sb, msg,
1137 "inode #%lu logical block %llu mapped to %llu "
1138 "(size %d)", inode->i_ino,
1139 (unsigned long long) logical,
1140 (unsigned long long) phys, len);
1134 if (!ext4_data_block_valid(EXT4_SB(inode->i_sb), map->m_pblk,
1135 map->m_len)) {
1136 ext4_error_inode(func, inode,
1137 "lblock %lu mapped to illegal pblock %llu "
1138 "(length %d)", (unsigned long) map->m_lblk,
1139 map->m_pblk, map->m_len);
1141 return -EIO;
1142 }
1143 return 0;
1144}
1145
1146/*
1147 * Return the number of contiguous dirty pages in a given inode
1148 * starting at page frame idx.

--- 91 unchanged lines hidden (view full) ---

1240 if (EXT4_I(inode)->i_flags & EXT4_EXTENTS_FL) {
1241 retval = ext4_ext_map_blocks(handle, inode, map, 0);
1242 } else {
1243 retval = ext4_ind_map_blocks(handle, inode, map, 0);
1244 }
1245 up_read((&EXT4_I(inode)->i_data_sem));
1246
1247 if (retval > 0 && map->m_flags & EXT4_MAP_MAPPED) {
1140 return -EIO;
1141 }
1142 return 0;
1143}
1144
1145/*
1146 * Return the number of contiguous dirty pages in a given inode
1147 * starting at page frame idx.

--- 91 unchanged lines hidden (view full) ---

1239 if (EXT4_I(inode)->i_flags & EXT4_EXTENTS_FL) {
1240 retval = ext4_ext_map_blocks(handle, inode, map, 0);
1241 } else {
1242 retval = ext4_ind_map_blocks(handle, inode, map, 0);
1243 }
1244 up_read((&EXT4_I(inode)->i_data_sem));
1245
1246 if (retval > 0 && map->m_flags & EXT4_MAP_MAPPED) {
1248 int ret = check_block_validity(inode, "file system corruption",
1249 map->m_lblk, map->m_pblk, retval);
1247 int ret = check_block_validity(inode, __func__, map);
1250 if (ret != 0)
1251 return ret;
1252 }
1253
1254 /* If it is only a block(s) look up */
1255 if ((flags & EXT4_GET_BLOCKS_CREATE) == 0)
1256 return retval;
1257

--- 63 unchanged lines hidden (view full) ---

1321 (flags & EXT4_GET_BLOCKS_DELALLOC_RESERVE))
1322 ext4_da_update_reserve_space(inode, retval, 1);
1323 }
1324 if (flags & EXT4_GET_BLOCKS_DELALLOC_RESERVE)
1325 EXT4_I(inode)->i_delalloc_reserved_flag = 0;
1326
1327 up_write((&EXT4_I(inode)->i_data_sem));
1328 if (retval > 0 && map->m_flags & EXT4_MAP_MAPPED) {
1248 if (ret != 0)
1249 return ret;
1250 }
1251
1252 /* If it is only a block(s) look up */
1253 if ((flags & EXT4_GET_BLOCKS_CREATE) == 0)
1254 return retval;
1255

--- 63 unchanged lines hidden (view full) ---

1319 (flags & EXT4_GET_BLOCKS_DELALLOC_RESERVE))
1320 ext4_da_update_reserve_space(inode, retval, 1);
1321 }
1322 if (flags & EXT4_GET_BLOCKS_DELALLOC_RESERVE)
1323 EXT4_I(inode)->i_delalloc_reserved_flag = 0;
1324
1325 up_write((&EXT4_I(inode)->i_data_sem));
1326 if (retval > 0 && map->m_flags & EXT4_MAP_MAPPED) {
1329 int ret = check_block_validity(inode, "file system "
1330 "corruption after allocation",
1331 map->m_lblk, map->m_pblk,
1332 retval);
1327 int ret = check_block_validity(inode,
1328 "ext4_map_blocks_after_alloc",
1329 map);
1333 if (ret != 0)
1334 return ret;
1335 }
1336 return retval;
1337}
1338
1339/* Maximum number of blocks we map for direct IO at once. */
1340#define DIO_MAX_BLOCKS 4096

--- 2981 unchanged lines hidden (view full) ---

4322 __le32 *p;
4323 int flags = EXT4_FREE_BLOCKS_FORGET | EXT4_FREE_BLOCKS_VALIDATED;
4324
4325 if (S_ISDIR(inode->i_mode) || S_ISLNK(inode->i_mode))
4326 flags |= EXT4_FREE_BLOCKS_METADATA;
4327
4328 if (!ext4_data_block_valid(EXT4_SB(inode->i_sb), block_to_free,
4329 count)) {
1330 if (ret != 0)
1331 return ret;
1332 }
1333 return retval;
1334}
1335
1336/* Maximum number of blocks we map for direct IO at once. */
1337#define DIO_MAX_BLOCKS 4096

--- 2981 unchanged lines hidden (view full) ---

4319 __le32 *p;
4320 int flags = EXT4_FREE_BLOCKS_FORGET | EXT4_FREE_BLOCKS_VALIDATED;
4321
4322 if (S_ISDIR(inode->i_mode) || S_ISLNK(inode->i_mode))
4323 flags |= EXT4_FREE_BLOCKS_METADATA;
4324
4325 if (!ext4_data_block_valid(EXT4_SB(inode->i_sb), block_to_free,
4326 count)) {
4330 ext4_error(inode->i_sb, "inode #%lu: "
4331 "attempt to clear blocks %llu len %lu, invalid",
4332 inode->i_ino, (unsigned long long) block_to_free,
4333 count);
4327 EXT4_ERROR_INODE(inode, "attempt to clear invalid "
4328 "blocks %llu len %lu",
4329 (unsigned long long) block_to_free, count);
4334 return 1;
4335 }
4336
4337 if (try_to_extend_transaction(handle, inode)) {
4338 if (bh) {
4339 BUFFER_TRACE(bh, "call ext4_handle_dirty_metadata");
4340 ext4_handle_dirty_metadata(handle, inode, bh);
4341 }

--- 88 unchanged lines hidden (view full) ---

4430 * The buffer head should have an attached journal head at this
4431 * point. However, if the data is corrupted and an indirect
4432 * block pointed to itself, it would have been detached when
4433 * the block was cleared. Check for this instead of OOPSing.
4434 */
4435 if ((EXT4_JOURNAL(inode) == NULL) || bh2jh(this_bh))
4436 ext4_handle_dirty_metadata(handle, inode, this_bh);
4437 else
4330 return 1;
4331 }
4332
4333 if (try_to_extend_transaction(handle, inode)) {
4334 if (bh) {
4335 BUFFER_TRACE(bh, "call ext4_handle_dirty_metadata");
4336 ext4_handle_dirty_metadata(handle, inode, bh);
4337 }

--- 88 unchanged lines hidden (view full) ---

4426 * The buffer head should have an attached journal head at this
4427 * point. However, if the data is corrupted and an indirect
4428 * block pointed to itself, it would have been detached when
4429 * the block was cleared. Check for this instead of OOPSing.
4430 */
4431 if ((EXT4_JOURNAL(inode) == NULL) || bh2jh(this_bh))
4432 ext4_handle_dirty_metadata(handle, inode, this_bh);
4433 else
4438 ext4_error(inode->i_sb,
4439 "circular indirect block detected, "
4440 "inode=%lu, block=%llu",
4441 inode->i_ino,
4442 (unsigned long long) this_bh->b_blocknr);
4434 EXT4_ERROR_INODE(inode,
4435 "circular indirect block detected at "
4436 "block %llu",
4437 (unsigned long long) this_bh->b_blocknr);
4443 }
4444}
4445
4446/**
4447 * ext4_free_branches - free an array of branches
4448 * @handle: JBD handle for this transaction
4449 * @inode: inode we are dealing with
4450 * @parent_bh: the buffer_head which contains *@first and *@last

--- 21 unchanged lines hidden (view full) ---

4472 p = last;
4473 while (--p >= first) {
4474 nr = le32_to_cpu(*p);
4475 if (!nr)
4476 continue; /* A hole */
4477
4478 if (!ext4_data_block_valid(EXT4_SB(inode->i_sb),
4479 nr, 1)) {
4438 }
4439}
4440
4441/**
4442 * ext4_free_branches - free an array of branches
4443 * @handle: JBD handle for this transaction
4444 * @inode: inode we are dealing with
4445 * @parent_bh: the buffer_head which contains *@first and *@last

--- 21 unchanged lines hidden (view full) ---

4467 p = last;
4468 while (--p >= first) {
4469 nr = le32_to_cpu(*p);
4470 if (!nr)
4471 continue; /* A hole */
4472
4473 if (!ext4_data_block_valid(EXT4_SB(inode->i_sb),
4474 nr, 1)) {
4480 ext4_error(inode->i_sb,
4481 "indirect mapped block in inode "
4482 "#%lu invalid (level %d, blk #%lu)",
4483 inode->i_ino, depth,
4484 (unsigned long) nr);
4475 EXT4_ERROR_INODE(inode,
4476 "invalid indirect mapped "
4477 "block %lu (level %d)",
4478 (unsigned long) nr, depth);
4485 break;
4486 }
4487
4488 /* Go read the buffer for the next level down */
4489 bh = sb_bread(inode->i_sb, nr);
4490
4491 /*
4492 * A read failure? Report error and clear slot
4493 * (should be rare).
4494 */
4495 if (!bh) {
4479 break;
4480 }
4481
4482 /* Go read the buffer for the next level down */
4483 bh = sb_bread(inode->i_sb, nr);
4484
4485 /*
4486 * A read failure? Report error and clear slot
4487 * (should be rare).
4488 */
4489 if (!bh) {
4496 ext4_error(inode->i_sb,
4497 "Read failure, inode=%lu, block=%llu",
4498 inode->i_ino, nr);
4490 EXT4_ERROR_INODE(inode,
4491 "Read failure block=%llu",
4492 (unsigned long long) nr);
4499 continue;
4500 }
4501
4502 /* This zaps the entire block. Bottom up. */
4503 BUFFER_TRACE(bh, "free child branches");
4504 ext4_free_branches(handle, inode, bh,
4505 (__le32 *) bh->b_data,
4506 (__le32 *) bh->b_data + addr_per_block,

--- 298 unchanged lines hidden (view full) ---

4805 inodes_per_block = (EXT4_BLOCK_SIZE(sb) / EXT4_INODE_SIZE(sb));
4806 inode_offset = ((inode->i_ino - 1) %
4807 EXT4_INODES_PER_GROUP(sb));
4808 block = ext4_inode_table(sb, gdp) + (inode_offset / inodes_per_block);
4809 iloc->offset = (inode_offset % inodes_per_block) * EXT4_INODE_SIZE(sb);
4810
4811 bh = sb_getblk(sb, block);
4812 if (!bh) {
4493 continue;
4494 }
4495
4496 /* This zaps the entire block. Bottom up. */
4497 BUFFER_TRACE(bh, "free child branches");
4498 ext4_free_branches(handle, inode, bh,
4499 (__le32 *) bh->b_data,
4500 (__le32 *) bh->b_data + addr_per_block,

--- 298 unchanged lines hidden (view full) ---

4799 inodes_per_block = (EXT4_BLOCK_SIZE(sb) / EXT4_INODE_SIZE(sb));
4800 inode_offset = ((inode->i_ino - 1) %
4801 EXT4_INODES_PER_GROUP(sb));
4802 block = ext4_inode_table(sb, gdp) + (inode_offset / inodes_per_block);
4803 iloc->offset = (inode_offset % inodes_per_block) * EXT4_INODE_SIZE(sb);
4804
4805 bh = sb_getblk(sb, block);
4806 if (!bh) {
4813 ext4_error(sb, "unable to read inode block - "
4814 "inode=%lu, block=%llu", inode->i_ino, block);
4807 EXT4_ERROR_INODE(inode, "unable to read inode block - "
4808 "block %llu", block);
4815 return -EIO;
4816 }
4817 if (!buffer_uptodate(bh)) {
4818 lock_buffer(bh);
4819
4820 /*
4821 * If the buffer has the write error flag, we have failed
4822 * to write out another inode in the same block. In this

--- 81 unchanged lines hidden (view full) ---

4904 * has in-inode xattrs, or we don't have this inode in memory.
4905 * Read the block from disk.
4906 */
4907 get_bh(bh);
4908 bh->b_end_io = end_buffer_read_sync;
4909 submit_bh(READ_META, bh);
4910 wait_on_buffer(bh);
4911 if (!buffer_uptodate(bh)) {
4809 return -EIO;
4810 }
4811 if (!buffer_uptodate(bh)) {
4812 lock_buffer(bh);
4813
4814 /*
4815 * If the buffer has the write error flag, we have failed
4816 * to write out another inode in the same block. In this

--- 81 unchanged lines hidden (view full) ---

4898 * has in-inode xattrs, or we don't have this inode in memory.
4899 * Read the block from disk.
4900 */
4901 get_bh(bh);
4902 bh->b_end_io = end_buffer_read_sync;
4903 submit_bh(READ_META, bh);
4904 wait_on_buffer(bh);
4905 if (!buffer_uptodate(bh)) {
4912 ext4_error(sb, "unable to read inode block - inode=%lu,"
4913 " block=%llu", inode->i_ino, block);
4906 EXT4_ERROR_INODE(inode, "unable to read inode "
4907 "block %llu", block);
4914 brelse(bh);
4915 return -EIO;
4916 }
4917 }
4918has_buffer:
4919 iloc->bh = bh;
4920 return 0;
4921}

--- 194 unchanged lines hidden (view full) ---

5116 if (EXT4_FITS_IN_INODE(raw_inode, ei, i_version_hi))
5117 inode->i_version |=
5118 (__u64)(le32_to_cpu(raw_inode->i_version_hi)) << 32;
5119 }
5120
5121 ret = 0;
5122 if (ei->i_file_acl &&
5123 !ext4_data_block_valid(EXT4_SB(sb), ei->i_file_acl, 1)) {
4908 brelse(bh);
4909 return -EIO;
4910 }
4911 }
4912has_buffer:
4913 iloc->bh = bh;
4914 return 0;
4915}

--- 194 unchanged lines hidden (view full) ---

5110 if (EXT4_FITS_IN_INODE(raw_inode, ei, i_version_hi))
5111 inode->i_version |=
5112 (__u64)(le32_to_cpu(raw_inode->i_version_hi)) << 32;
5113 }
5114
5115 ret = 0;
5116 if (ei->i_file_acl &&
5117 !ext4_data_block_valid(EXT4_SB(sb), ei->i_file_acl, 1)) {
5124 ext4_error(sb, "bad extended attribute block %llu inode #%lu",
5125 ei->i_file_acl, inode->i_ino);
5118 EXT4_ERROR_INODE(inode, "bad extended attribute block %llu",
5119 ei->i_file_acl);
5126 ret = -EIO;
5127 goto bad_inode;
5128 } else if (ei->i_flags & EXT4_EXTENTS_FL) {
5129 if (S_ISREG(inode->i_mode) || S_ISDIR(inode->i_mode) ||
5130 (S_ISLNK(inode->i_mode) &&
5131 !ext4_inode_is_fast_symlink(inode)))
5132 /* Validate extent which is part of inode */
5133 ret = ext4_ext_check_inode(inode);

--- 28 unchanged lines hidden (view full) ---

5162 if (raw_inode->i_block[0])
5163 init_special_inode(inode, inode->i_mode,
5164 old_decode_dev(le32_to_cpu(raw_inode->i_block[0])));
5165 else
5166 init_special_inode(inode, inode->i_mode,
5167 new_decode_dev(le32_to_cpu(raw_inode->i_block[1])));
5168 } else {
5169 ret = -EIO;
5120 ret = -EIO;
5121 goto bad_inode;
5122 } else if (ei->i_flags & EXT4_EXTENTS_FL) {
5123 if (S_ISREG(inode->i_mode) || S_ISDIR(inode->i_mode) ||
5124 (S_ISLNK(inode->i_mode) &&
5125 !ext4_inode_is_fast_symlink(inode)))
5126 /* Validate extent which is part of inode */
5127 ret = ext4_ext_check_inode(inode);

--- 28 unchanged lines hidden (view full) ---

5156 if (raw_inode->i_block[0])
5157 init_special_inode(inode, inode->i_mode,
5158 old_decode_dev(le32_to_cpu(raw_inode->i_block[0])));
5159 else
5160 init_special_inode(inode, inode->i_mode,
5161 new_decode_dev(le32_to_cpu(raw_inode->i_block[1])));
5162 } else {
5163 ret = -EIO;
5170 ext4_error(inode->i_sb, "bogus i_mode (%o) for inode=%lu",
5171 inode->i_mode, inode->i_ino);
5164 EXT4_ERROR_INODE(inode, "bogus i_mode (%o)", inode->i_mode);
5172 goto bad_inode;
5173 }
5174 brelse(iloc.bh);
5175 ext4_set_inode_flags(inode);
5176 unlock_new_inode(inode);
5177 return inode;
5178
5179bad_inode:

--- 221 unchanged lines hidden (view full) ---

5401 struct ext4_iloc iloc;
5402
5403 err = __ext4_get_inode_loc(inode, &iloc, 0);
5404 if (err)
5405 return err;
5406 if (wbc->sync_mode == WB_SYNC_ALL)
5407 sync_dirty_buffer(iloc.bh);
5408 if (buffer_req(iloc.bh) && !buffer_uptodate(iloc.bh)) {
5165 goto bad_inode;
5166 }
5167 brelse(iloc.bh);
5168 ext4_set_inode_flags(inode);
5169 unlock_new_inode(inode);
5170 return inode;
5171
5172bad_inode:

--- 221 unchanged lines hidden (view full) ---

5394 struct ext4_iloc iloc;
5395
5396 err = __ext4_get_inode_loc(inode, &iloc, 0);
5397 if (err)
5398 return err;
5399 if (wbc->sync_mode == WB_SYNC_ALL)
5400 sync_dirty_buffer(iloc.bh);
5401 if (buffer_req(iloc.bh) && !buffer_uptodate(iloc.bh)) {
5409 ext4_error(inode->i_sb, "IO error syncing inode, "
5410 "inode=%lu, block=%llu", inode->i_ino,
5411 (unsigned long long)iloc.bh->b_blocknr);
5402 EXT4_ERROR_INODE(inode,
5403 "IO error syncing inode (block=%llu)",
5404 (unsigned long long) iloc.bh->b_blocknr);
5412 err = -EIO;
5413 }
5414 brelse(iloc.bh);
5415 }
5416 return err;
5417}
5418
5419/*

--- 613 unchanged lines hidden ---
5405 err = -EIO;
5406 }
5407 brelse(iloc.bh);
5408 }
5409 return err;
5410}
5411
5412/*

--- 613 unchanged lines hidden ---