Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 1 | /* |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 2 | * linux/fs/ext4/ialloc.c |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 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 | * |
| 9 | * BSD ufs-inspired inode and directory allocation by |
| 10 | * Stephen Tweedie (sct@redhat.com), 1993 |
| 11 | * Big-endian to little-endian byte-swapping/bitmaps by |
| 12 | * David S. Miller (davem@caip.rutgers.edu), 1995 |
| 13 | */ |
| 14 | |
| 15 | #include <linux/time.h> |
| 16 | #include <linux/fs.h> |
Mingming Cao | dab291a | 2006-10-11 01:21:01 -0700 | [diff] [blame] | 17 | #include <linux/jbd2.h> |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 18 | #include <linux/stat.h> |
| 19 | #include <linux/string.h> |
| 20 | #include <linux/quotaops.h> |
| 21 | #include <linux/buffer_head.h> |
| 22 | #include <linux/random.h> |
| 23 | #include <linux/bitops.h> |
Mingming Cao | 3a5b2ec | 2006-10-11 01:21:05 -0700 | [diff] [blame] | 24 | #include <linux/blkdev.h> |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 25 | #include <asm/byteorder.h> |
Christoph Hellwig | 3dcf545 | 2008-04-29 18:13:32 -0400 | [diff] [blame] | 26 | #include "ext4.h" |
| 27 | #include "ext4_jbd2.h" |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 28 | #include "xattr.h" |
| 29 | #include "acl.h" |
Andreas Dilger | 717d50e | 2007-10-16 18:38:25 -0400 | [diff] [blame] | 30 | #include "group.h" |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 31 | |
| 32 | /* |
| 33 | * ialloc.c contains the inodes allocation and deallocation routines |
| 34 | */ |
| 35 | |
| 36 | /* |
| 37 | * The free inodes are managed by bitmaps. A file system contains several |
| 38 | * blocks groups. Each group contains 1 bitmap block for blocks, 1 bitmap |
| 39 | * block for inodes, N blocks for the inode table and data blocks. |
| 40 | * |
| 41 | * The file system contains group descriptors which are located after the |
| 42 | * super block. Each descriptor contains the number of the bitmap block and |
| 43 | * the free blocks count in the block. |
| 44 | */ |
| 45 | |
Andreas Dilger | 717d50e | 2007-10-16 18:38:25 -0400 | [diff] [blame] | 46 | /* |
| 47 | * To avoid calling the atomic setbit hundreds or thousands of times, we only |
| 48 | * need to use it within a single byte (to ensure we get endianness right). |
| 49 | * We can use memset for the rest of the bitmap as there are no other users. |
| 50 | */ |
| 51 | void mark_bitmap_end(int start_bit, int end_bit, char *bitmap) |
| 52 | { |
| 53 | int i; |
| 54 | |
| 55 | if (start_bit >= end_bit) |
| 56 | return; |
| 57 | |
| 58 | ext4_debug("mark end bits +%d through +%d used\n", start_bit, end_bit); |
| 59 | for (i = start_bit; i < ((start_bit + 7) & ~7UL); i++) |
| 60 | ext4_set_bit(i, bitmap); |
| 61 | if (i < end_bit) |
| 62 | memset(bitmap + (i >> 3), 0xff, (end_bit - i) >> 3); |
| 63 | } |
| 64 | |
| 65 | /* Initializes an uninitialized inode bitmap */ |
Avantika Mathur | fd2d429 | 2008-01-28 23:58:27 -0500 | [diff] [blame] | 66 | unsigned ext4_init_inode_bitmap(struct super_block *sb, struct buffer_head *bh, |
| 67 | ext4_group_t block_group, |
Andreas Dilger | 717d50e | 2007-10-16 18:38:25 -0400 | [diff] [blame] | 68 | struct ext4_group_desc *gdp) |
| 69 | { |
| 70 | struct ext4_sb_info *sbi = EXT4_SB(sb); |
| 71 | |
| 72 | J_ASSERT_BH(bh, buffer_locked(bh)); |
| 73 | |
| 74 | /* If checksum is bad mark all blocks and inodes use to prevent |
| 75 | * allocation, essentially implementing a per-group read-only flag. */ |
| 76 | if (!ext4_group_desc_csum_verify(sbi, block_group, gdp)) { |
Theodore Ts'o | a9df9a4 | 2009-01-05 22:18:16 -0500 | [diff] [blame] | 77 | ext4_error(sb, __func__, "Checksum bad for group %u", |
Andreas Dilger | 717d50e | 2007-10-16 18:38:25 -0400 | [diff] [blame] | 78 | block_group); |
Aneesh Kumar K.V | 560671a | 2009-01-05 22:20:24 -0500 | [diff] [blame] | 79 | ext4_free_blks_set(sb, gdp, 0); |
| 80 | ext4_free_inodes_set(sb, gdp, 0); |
| 81 | ext4_itable_unused_set(sb, gdp, 0); |
Andreas Dilger | 717d50e | 2007-10-16 18:38:25 -0400 | [diff] [blame] | 82 | memset(bh->b_data, 0xff, sb->s_blocksize); |
| 83 | return 0; |
| 84 | } |
| 85 | |
| 86 | memset(bh->b_data, 0, (EXT4_INODES_PER_GROUP(sb) + 7) / 8); |
Aneesh Kumar K.V | 648f587 | 2009-01-05 21:46:04 -0500 | [diff] [blame] | 87 | mark_bitmap_end(EXT4_INODES_PER_GROUP(sb), sb->s_blocksize * 8, |
Andreas Dilger | 717d50e | 2007-10-16 18:38:25 -0400 | [diff] [blame] | 88 | bh->b_data); |
| 89 | |
| 90 | return EXT4_INODES_PER_GROUP(sb); |
| 91 | } |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 92 | |
| 93 | /* |
| 94 | * Read the inode allocation bitmap for a given block_group, reading |
| 95 | * into the specified slot in the superblock's bitmap cache. |
| 96 | * |
| 97 | * Return buffer_head of bitmap on success or NULL. |
| 98 | */ |
| 99 | static struct buffer_head * |
Eric Sandeen | e29d1cd | 2008-08-02 21:21:02 -0400 | [diff] [blame] | 100 | ext4_read_inode_bitmap(struct super_block *sb, ext4_group_t block_group) |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 101 | { |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 102 | struct ext4_group_desc *desc; |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 103 | struct buffer_head *bh = NULL; |
Eric Sandeen | e29d1cd | 2008-08-02 21:21:02 -0400 | [diff] [blame] | 104 | ext4_fsblk_t bitmap_blk; |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 105 | |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 106 | desc = ext4_get_group_desc(sb, block_group, NULL); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 107 | if (!desc) |
Eric Sandeen | e29d1cd | 2008-08-02 21:21:02 -0400 | [diff] [blame] | 108 | return NULL; |
| 109 | bitmap_blk = ext4_inode_bitmap(sb, desc); |
| 110 | bh = sb_getblk(sb, bitmap_blk); |
| 111 | if (unlikely(!bh)) { |
| 112 | ext4_error(sb, __func__, |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 113 | "Cannot read inode bitmap - " |
Theodore Ts'o | a9df9a4 | 2009-01-05 22:18:16 -0500 | [diff] [blame] | 114 | "block_group = %u, inode_bitmap = %llu", |
Eric Sandeen | e29d1cd | 2008-08-02 21:21:02 -0400 | [diff] [blame] | 115 | block_group, bitmap_blk); |
| 116 | return NULL; |
| 117 | } |
Aneesh Kumar K.V | 2ccb5fb | 2009-01-05 21:49:55 -0500 | [diff] [blame] | 118 | if (bitmap_uptodate(bh)) |
Eric Sandeen | e29d1cd | 2008-08-02 21:21:02 -0400 | [diff] [blame] | 119 | return bh; |
| 120 | |
Frederic Bohe | c806e68 | 2008-10-10 08:09:18 -0400 | [diff] [blame] | 121 | lock_buffer(bh); |
Aneesh Kumar K.V | 2ccb5fb | 2009-01-05 21:49:55 -0500 | [diff] [blame] | 122 | if (bitmap_uptodate(bh)) { |
| 123 | unlock_buffer(bh); |
| 124 | return bh; |
| 125 | } |
Eric Sandeen | b5f10ee | 2008-08-02 21:21:08 -0400 | [diff] [blame] | 126 | spin_lock(sb_bgl_lock(EXT4_SB(sb), block_group)); |
Eric Sandeen | e29d1cd | 2008-08-02 21:21:02 -0400 | [diff] [blame] | 127 | if (desc->bg_flags & cpu_to_le16(EXT4_BG_INODE_UNINIT)) { |
| 128 | ext4_init_inode_bitmap(sb, bh, block_group, desc); |
Aneesh Kumar K.V | 2ccb5fb | 2009-01-05 21:49:55 -0500 | [diff] [blame] | 129 | set_bitmap_uptodate(bh); |
Eric Sandeen | e29d1cd | 2008-08-02 21:21:02 -0400 | [diff] [blame] | 130 | set_buffer_uptodate(bh); |
Eric Sandeen | b5f10ee | 2008-08-02 21:21:08 -0400 | [diff] [blame] | 131 | spin_unlock(sb_bgl_lock(EXT4_SB(sb), block_group)); |
Aneesh Kumar K.V | 3300bed | 2009-01-03 22:33:39 -0500 | [diff] [blame] | 132 | unlock_buffer(bh); |
Eric Sandeen | e29d1cd | 2008-08-02 21:21:02 -0400 | [diff] [blame] | 133 | return bh; |
| 134 | } |
Eric Sandeen | b5f10ee | 2008-08-02 21:21:08 -0400 | [diff] [blame] | 135 | spin_unlock(sb_bgl_lock(EXT4_SB(sb), block_group)); |
Aneesh Kumar K.V | 2ccb5fb | 2009-01-05 21:49:55 -0500 | [diff] [blame] | 136 | if (buffer_uptodate(bh)) { |
| 137 | /* |
| 138 | * if not uninit if bh is uptodate, |
| 139 | * bitmap is also uptodate |
| 140 | */ |
| 141 | set_bitmap_uptodate(bh); |
| 142 | unlock_buffer(bh); |
| 143 | return bh; |
| 144 | } |
| 145 | /* |
| 146 | * submit the buffer_head for read. We can |
| 147 | * safely mark the bitmap as uptodate now. |
| 148 | * We do it here so the bitmap uptodate bit |
| 149 | * get set with buffer lock held. |
| 150 | */ |
| 151 | set_bitmap_uptodate(bh); |
Eric Sandeen | e29d1cd | 2008-08-02 21:21:02 -0400 | [diff] [blame] | 152 | if (bh_submit_read(bh) < 0) { |
| 153 | put_bh(bh); |
| 154 | ext4_error(sb, __func__, |
| 155 | "Cannot read inode bitmap - " |
Theodore Ts'o | a9df9a4 | 2009-01-05 22:18:16 -0500 | [diff] [blame] | 156 | "block_group = %u, inode_bitmap = %llu", |
Eric Sandeen | e29d1cd | 2008-08-02 21:21:02 -0400 | [diff] [blame] | 157 | block_group, bitmap_blk); |
| 158 | return NULL; |
| 159 | } |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 160 | return bh; |
| 161 | } |
| 162 | |
| 163 | /* |
| 164 | * NOTE! When we get the inode, we're the only people |
| 165 | * that have access to it, and as such there are no |
| 166 | * race conditions we have to worry about. The inode |
| 167 | * is not on the hash-lists, and it cannot be reached |
| 168 | * through the filesystem because the directory entry |
| 169 | * has been deleted earlier. |
| 170 | * |
| 171 | * HOWEVER: we must make sure that we get no aliases, |
| 172 | * which means that we have to call "clear_inode()" |
| 173 | * _before_ we mark the inode not in use in the inode |
| 174 | * bitmaps. Otherwise a newly created file might use |
| 175 | * the same inode number (not actually the same pointer |
| 176 | * though), and then we'd have two inodes sharing the |
| 177 | * same inode number and space on the harddisk. |
| 178 | */ |
Theodore Ts'o | af5bc92 | 2008-09-08 22:25:24 -0400 | [diff] [blame] | 179 | void ext4_free_inode(handle_t *handle, struct inode *inode) |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 180 | { |
Theodore Ts'o | af5bc92 | 2008-09-08 22:25:24 -0400 | [diff] [blame] | 181 | struct super_block *sb = inode->i_sb; |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 182 | int is_directory; |
| 183 | unsigned long ino; |
| 184 | struct buffer_head *bitmap_bh = NULL; |
| 185 | struct buffer_head *bh2; |
Avantika Mathur | fd2d429 | 2008-01-28 23:58:27 -0500 | [diff] [blame] | 186 | ext4_group_t block_group; |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 187 | unsigned long bit; |
Theodore Ts'o | af5bc92 | 2008-09-08 22:25:24 -0400 | [diff] [blame] | 188 | struct ext4_group_desc *gdp; |
| 189 | struct ext4_super_block *es; |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 190 | struct ext4_sb_info *sbi; |
Eric Sandeen | 7ce9d5d | 2009-03-04 18:38:18 -0500 | [diff] [blame] | 191 | int fatal = 0, err, count, cleared; |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 192 | |
| 193 | if (atomic_read(&inode->i_count) > 1) { |
Theodore Ts'o | 4776004 | 2008-09-08 23:00:52 -0400 | [diff] [blame] | 194 | printk(KERN_ERR "ext4_free_inode: inode has count=%d\n", |
| 195 | atomic_read(&inode->i_count)); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 196 | return; |
| 197 | } |
| 198 | if (inode->i_nlink) { |
Theodore Ts'o | 4776004 | 2008-09-08 23:00:52 -0400 | [diff] [blame] | 199 | printk(KERN_ERR "ext4_free_inode: inode has nlink=%d\n", |
| 200 | inode->i_nlink); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 201 | return; |
| 202 | } |
| 203 | if (!sb) { |
Theodore Ts'o | 4776004 | 2008-09-08 23:00:52 -0400 | [diff] [blame] | 204 | printk(KERN_ERR "ext4_free_inode: inode on " |
| 205 | "nonexistent device\n"); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 206 | return; |
| 207 | } |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 208 | sbi = EXT4_SB(sb); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 209 | |
| 210 | ino = inode->i_ino; |
Theodore Ts'o | af5bc92 | 2008-09-08 22:25:24 -0400 | [diff] [blame] | 211 | ext4_debug("freeing inode %lu\n", ino); |
Theodore Ts'o | ba80b10 | 2009-01-03 20:03:21 -0500 | [diff] [blame] | 212 | trace_mark(ext4_free_inode, |
| 213 | "dev %s ino %lu mode %d uid %lu gid %lu bocks %llu", |
| 214 | sb->s_id, inode->i_ino, inode->i_mode, |
| 215 | (unsigned long) inode->i_uid, (unsigned long) inode->i_gid, |
| 216 | (unsigned long long) inode->i_blocks); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 217 | |
| 218 | /* |
| 219 | * Note: we must free any quota before locking the superblock, |
| 220 | * as writing the quota to disk may need the lock as well. |
| 221 | */ |
Jan Kara | a269eb1 | 2009-01-26 17:04:39 +0100 | [diff] [blame] | 222 | vfs_dq_init(inode); |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 223 | ext4_xattr_delete_inode(handle, inode); |
Jan Kara | a269eb1 | 2009-01-26 17:04:39 +0100 | [diff] [blame] | 224 | vfs_dq_free_inode(inode); |
| 225 | vfs_dq_drop(inode); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 226 | |
| 227 | is_directory = S_ISDIR(inode->i_mode); |
| 228 | |
| 229 | /* Do this BEFORE marking the inode not in use or returning an error */ |
Theodore Ts'o | af5bc92 | 2008-09-08 22:25:24 -0400 | [diff] [blame] | 230 | clear_inode(inode); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 231 | |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 232 | es = EXT4_SB(sb)->s_es; |
| 233 | if (ino < EXT4_FIRST_INO(sb) || ino > le32_to_cpu(es->s_inodes_count)) { |
Theodore Ts'o | af5bc92 | 2008-09-08 22:25:24 -0400 | [diff] [blame] | 234 | ext4_error(sb, "ext4_free_inode", |
| 235 | "reserved or nonexistent inode %lu", ino); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 236 | goto error_return; |
| 237 | } |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 238 | block_group = (ino - 1) / EXT4_INODES_PER_GROUP(sb); |
| 239 | bit = (ino - 1) % EXT4_INODES_PER_GROUP(sb); |
Eric Sandeen | e29d1cd | 2008-08-02 21:21:02 -0400 | [diff] [blame] | 240 | bitmap_bh = ext4_read_inode_bitmap(sb, block_group); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 241 | if (!bitmap_bh) |
| 242 | goto error_return; |
| 243 | |
| 244 | BUFFER_TRACE(bitmap_bh, "get_write_access"); |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 245 | fatal = ext4_journal_get_write_access(handle, bitmap_bh); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 246 | if (fatal) |
| 247 | goto error_return; |
| 248 | |
| 249 | /* Ok, now we can actually update the inode bitmaps.. */ |
Eric Sandeen | 7ce9d5d | 2009-03-04 18:38:18 -0500 | [diff] [blame] | 250 | spin_lock(sb_bgl_lock(sbi, block_group)); |
| 251 | cleared = ext4_clear_bit(bit, bitmap_bh->b_data); |
| 252 | spin_unlock(sb_bgl_lock(sbi, block_group)); |
| 253 | if (!cleared) |
Theodore Ts'o | af5bc92 | 2008-09-08 22:25:24 -0400 | [diff] [blame] | 254 | ext4_error(sb, "ext4_free_inode", |
| 255 | "bit already cleared for inode %lu", ino); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 256 | else { |
Theodore Ts'o | af5bc92 | 2008-09-08 22:25:24 -0400 | [diff] [blame] | 257 | gdp = ext4_get_group_desc(sb, block_group, &bh2); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 258 | |
| 259 | BUFFER_TRACE(bh2, "get_write_access"); |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 260 | fatal = ext4_journal_get_write_access(handle, bh2); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 261 | if (fatal) goto error_return; |
| 262 | |
| 263 | if (gdp) { |
| 264 | spin_lock(sb_bgl_lock(sbi, block_group)); |
Aneesh Kumar K.V | 560671a | 2009-01-05 22:20:24 -0500 | [diff] [blame] | 265 | count = ext4_free_inodes_count(sb, gdp) + 1; |
| 266 | ext4_free_inodes_set(sb, gdp, count); |
| 267 | if (is_directory) { |
| 268 | count = ext4_used_dirs_count(sb, gdp) - 1; |
| 269 | ext4_used_dirs_set(sb, gdp, count); |
Theodore Ts'o | 7d39db1 | 2009-03-04 19:31:53 -0500 | [diff] [blame] | 270 | if (sbi->s_log_groups_per_flex) { |
| 271 | ext4_group_t f; |
| 272 | |
| 273 | f = ext4_flex_group(sbi, block_group); |
| 274 | atomic_dec(&sbi->s_flex_groups[f].free_inodes); |
| 275 | } |
| 276 | |
Aneesh Kumar K.V | 560671a | 2009-01-05 22:20:24 -0500 | [diff] [blame] | 277 | } |
Andreas Dilger | 717d50e | 2007-10-16 18:38:25 -0400 | [diff] [blame] | 278 | gdp->bg_checksum = ext4_group_desc_csum(sbi, |
| 279 | block_group, gdp); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 280 | spin_unlock(sb_bgl_lock(sbi, block_group)); |
| 281 | percpu_counter_inc(&sbi->s_freeinodes_counter); |
| 282 | if (is_directory) |
| 283 | percpu_counter_dec(&sbi->s_dirs_counter); |
| 284 | |
Jose R. Santos | 772cb7c | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 285 | if (sbi->s_log_groups_per_flex) { |
Theodore Ts'o | 9f24e42 | 2009-03-04 19:09:10 -0500 | [diff] [blame] | 286 | ext4_group_t f; |
| 287 | |
| 288 | f = ext4_flex_group(sbi, block_group); |
| 289 | atomic_inc(&sbi->s_flex_groups[f].free_inodes); |
Jose R. Santos | 772cb7c | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 290 | } |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 291 | } |
Frank Mayhar | 0390131 | 2009-01-07 00:06:22 -0500 | [diff] [blame] | 292 | BUFFER_TRACE(bh2, "call ext4_handle_dirty_metadata"); |
| 293 | err = ext4_handle_dirty_metadata(handle, NULL, bh2); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 294 | if (!fatal) fatal = err; |
| 295 | } |
Frank Mayhar | 0390131 | 2009-01-07 00:06:22 -0500 | [diff] [blame] | 296 | BUFFER_TRACE(bitmap_bh, "call ext4_handle_dirty_metadata"); |
| 297 | err = ext4_handle_dirty_metadata(handle, NULL, bitmap_bh); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 298 | if (!fatal) |
| 299 | fatal = err; |
| 300 | sb->s_dirt = 1; |
| 301 | error_return: |
| 302 | brelse(bitmap_bh); |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 303 | ext4_std_error(sb, fatal); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 304 | } |
| 305 | |
| 306 | /* |
| 307 | * There are two policies for allocating an inode. If the new inode is |
| 308 | * a directory, then a forward search is made for a block group with both |
| 309 | * free space and a low directory-to-inode ratio; if that fails, then of |
| 310 | * the groups with above-average free space, that group with the fewest |
| 311 | * directories already is chosen. |
| 312 | * |
| 313 | * For other inodes, search forward from the parent directory\'s block |
| 314 | * group to find a free inode. |
| 315 | */ |
Avantika Mathur | 2aa9fc4 | 2008-01-28 23:58:27 -0500 | [diff] [blame] | 316 | static int find_group_dir(struct super_block *sb, struct inode *parent, |
| 317 | ext4_group_t *best_group) |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 318 | { |
Avantika Mathur | fd2d429 | 2008-01-28 23:58:27 -0500 | [diff] [blame] | 319 | ext4_group_t ngroups = EXT4_SB(sb)->s_groups_count; |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 320 | unsigned int freei, avefreei; |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 321 | struct ext4_group_desc *desc, *best_desc = NULL; |
Avantika Mathur | 2aa9fc4 | 2008-01-28 23:58:27 -0500 | [diff] [blame] | 322 | ext4_group_t group; |
| 323 | int ret = -1; |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 324 | |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 325 | freei = percpu_counter_read_positive(&EXT4_SB(sb)->s_freeinodes_counter); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 326 | avefreei = freei / ngroups; |
| 327 | |
| 328 | for (group = 0; group < ngroups; group++) { |
Theodore Ts'o | af5bc92 | 2008-09-08 22:25:24 -0400 | [diff] [blame] | 329 | desc = ext4_get_group_desc(sb, group, NULL); |
Aneesh Kumar K.V | 560671a | 2009-01-05 22:20:24 -0500 | [diff] [blame] | 330 | if (!desc || !ext4_free_inodes_count(sb, desc)) |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 331 | continue; |
Aneesh Kumar K.V | 560671a | 2009-01-05 22:20:24 -0500 | [diff] [blame] | 332 | if (ext4_free_inodes_count(sb, desc) < avefreei) |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 333 | continue; |
| 334 | if (!best_desc || |
Aneesh Kumar K.V | 560671a | 2009-01-05 22:20:24 -0500 | [diff] [blame] | 335 | (ext4_free_blks_count(sb, desc) > |
| 336 | ext4_free_blks_count(sb, best_desc))) { |
Avantika Mathur | 2aa9fc4 | 2008-01-28 23:58:27 -0500 | [diff] [blame] | 337 | *best_group = group; |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 338 | best_desc = desc; |
Avantika Mathur | 2aa9fc4 | 2008-01-28 23:58:27 -0500 | [diff] [blame] | 339 | ret = 0; |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 340 | } |
| 341 | } |
Avantika Mathur | 2aa9fc4 | 2008-01-28 23:58:27 -0500 | [diff] [blame] | 342 | return ret; |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 343 | } |
| 344 | |
Jose R. Santos | 772cb7c | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 345 | #define free_block_ratio 10 |
| 346 | |
| 347 | static int find_group_flex(struct super_block *sb, struct inode *parent, |
| 348 | ext4_group_t *best_group) |
| 349 | { |
| 350 | struct ext4_sb_info *sbi = EXT4_SB(sb); |
| 351 | struct ext4_group_desc *desc; |
| 352 | struct buffer_head *bh; |
| 353 | struct flex_groups *flex_group = sbi->s_flex_groups; |
| 354 | ext4_group_t parent_group = EXT4_I(parent)->i_block_group; |
| 355 | ext4_group_t parent_fbg_group = ext4_flex_group(sbi, parent_group); |
| 356 | ext4_group_t ngroups = sbi->s_groups_count; |
| 357 | int flex_size = ext4_flex_bg_size(sbi); |
| 358 | ext4_group_t best_flex = parent_fbg_group; |
| 359 | int blocks_per_flex = sbi->s_blocks_per_group * flex_size; |
| 360 | int flexbg_free_blocks; |
| 361 | int flex_freeb_ratio; |
| 362 | ext4_group_t n_fbg_groups; |
| 363 | ext4_group_t i; |
| 364 | |
| 365 | n_fbg_groups = (sbi->s_groups_count + flex_size - 1) >> |
| 366 | sbi->s_log_groups_per_flex; |
| 367 | |
| 368 | find_close_to_parent: |
Theodore Ts'o | 9f24e42 | 2009-03-04 19:09:10 -0500 | [diff] [blame] | 369 | flexbg_free_blocks = atomic_read(&flex_group[best_flex].free_blocks); |
Jose R. Santos | 772cb7c | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 370 | flex_freeb_ratio = flexbg_free_blocks * 100 / blocks_per_flex; |
Theodore Ts'o | 9f24e42 | 2009-03-04 19:09:10 -0500 | [diff] [blame] | 371 | if (atomic_read(&flex_group[best_flex].free_inodes) && |
Jose R. Santos | 772cb7c | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 372 | flex_freeb_ratio > free_block_ratio) |
| 373 | goto found_flexbg; |
| 374 | |
| 375 | if (best_flex && best_flex == parent_fbg_group) { |
| 376 | best_flex--; |
| 377 | goto find_close_to_parent; |
| 378 | } |
| 379 | |
| 380 | for (i = 0; i < n_fbg_groups; i++) { |
| 381 | if (i == parent_fbg_group || i == parent_fbg_group - 1) |
| 382 | continue; |
| 383 | |
Theodore Ts'o | 9f24e42 | 2009-03-04 19:09:10 -0500 | [diff] [blame] | 384 | flexbg_free_blocks = atomic_read(&flex_group[i].free_blocks); |
Jose R. Santos | 772cb7c | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 385 | flex_freeb_ratio = flexbg_free_blocks * 100 / blocks_per_flex; |
| 386 | |
| 387 | if (flex_freeb_ratio > free_block_ratio && |
Theodore Ts'o | 9f24e42 | 2009-03-04 19:09:10 -0500 | [diff] [blame] | 388 | (atomic_read(&flex_group[i].free_inodes))) { |
Jose R. Santos | 772cb7c | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 389 | best_flex = i; |
| 390 | goto found_flexbg; |
| 391 | } |
| 392 | |
Theodore Ts'o | 9f24e42 | 2009-03-04 19:09:10 -0500 | [diff] [blame] | 393 | if ((atomic_read(&flex_group[best_flex].free_inodes) == 0) || |
| 394 | ((atomic_read(&flex_group[i].free_blocks) > |
| 395 | atomic_read(&flex_group[best_flex].free_blocks)) && |
| 396 | atomic_read(&flex_group[i].free_inodes))) |
Jose R. Santos | 772cb7c | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 397 | best_flex = i; |
| 398 | } |
| 399 | |
Theodore Ts'o | 9f24e42 | 2009-03-04 19:09:10 -0500 | [diff] [blame] | 400 | if (!atomic_read(&flex_group[best_flex].free_inodes) || |
| 401 | !atomic_read(&flex_group[best_flex].free_blocks)) |
Jose R. Santos | 772cb7c | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 402 | return -1; |
| 403 | |
| 404 | found_flexbg: |
| 405 | for (i = best_flex * flex_size; i < ngroups && |
| 406 | i < (best_flex + 1) * flex_size; i++) { |
| 407 | desc = ext4_get_group_desc(sb, i, &bh); |
Aneesh Kumar K.V | 560671a | 2009-01-05 22:20:24 -0500 | [diff] [blame] | 408 | if (ext4_free_inodes_count(sb, desc)) { |
Jose R. Santos | 772cb7c | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 409 | *best_group = i; |
| 410 | goto out; |
| 411 | } |
| 412 | } |
| 413 | |
| 414 | return -1; |
| 415 | out: |
| 416 | return 0; |
| 417 | } |
| 418 | |
Theodore Ts'o | a491212 | 2009-03-12 12:18:34 -0400 | [diff] [blame] | 419 | struct orlov_stats { |
| 420 | __u32 free_inodes; |
| 421 | __u32 free_blocks; |
| 422 | __u32 used_dirs; |
| 423 | }; |
| 424 | |
| 425 | /* |
| 426 | * Helper function for Orlov's allocator; returns critical information |
| 427 | * for a particular block group or flex_bg. If flex_size is 1, then g |
| 428 | * is a block group number; otherwise it is flex_bg number. |
| 429 | */ |
| 430 | void get_orlov_stats(struct super_block *sb, ext4_group_t g, |
| 431 | int flex_size, struct orlov_stats *stats) |
| 432 | { |
| 433 | struct ext4_group_desc *desc; |
Theodore Ts'o | 7d39db1 | 2009-03-04 19:31:53 -0500 | [diff] [blame] | 434 | struct flex_groups *flex_group = EXT4_SB(sb)->s_flex_groups; |
Theodore Ts'o | a491212 | 2009-03-12 12:18:34 -0400 | [diff] [blame] | 435 | |
Theodore Ts'o | 7d39db1 | 2009-03-04 19:31:53 -0500 | [diff] [blame] | 436 | if (flex_size > 1) { |
| 437 | stats->free_inodes = atomic_read(&flex_group[g].free_inodes); |
| 438 | stats->free_blocks = atomic_read(&flex_group[g].free_blocks); |
| 439 | stats->used_dirs = atomic_read(&flex_group[g].used_dirs); |
| 440 | return; |
| 441 | } |
Theodore Ts'o | a491212 | 2009-03-12 12:18:34 -0400 | [diff] [blame] | 442 | |
Theodore Ts'o | 7d39db1 | 2009-03-04 19:31:53 -0500 | [diff] [blame] | 443 | desc = ext4_get_group_desc(sb, g, NULL); |
| 444 | if (desc) { |
| 445 | stats->free_inodes = ext4_free_inodes_count(sb, desc); |
| 446 | stats->free_blocks = ext4_free_blks_count(sb, desc); |
| 447 | stats->used_dirs = ext4_used_dirs_count(sb, desc); |
| 448 | } else { |
| 449 | stats->free_inodes = 0; |
| 450 | stats->free_blocks = 0; |
| 451 | stats->used_dirs = 0; |
Theodore Ts'o | a491212 | 2009-03-12 12:18:34 -0400 | [diff] [blame] | 452 | } |
| 453 | } |
| 454 | |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 455 | /* |
| 456 | * Orlov's allocator for directories. |
| 457 | * |
| 458 | * We always try to spread first-level directories. |
| 459 | * |
| 460 | * If there are blockgroups with both free inodes and free blocks counts |
| 461 | * not worse than average we return one with smallest directory count. |
| 462 | * Otherwise we simply return a random group. |
| 463 | * |
| 464 | * For the rest rules look so: |
| 465 | * |
| 466 | * It's OK to put directory into a group unless |
| 467 | * it has too many directories already (max_dirs) or |
| 468 | * it has too few free inodes left (min_inodes) or |
| 469 | * it has too few free blocks left (min_blocks) or |
Benoit Boissinot | 1cc8dcf | 2008-04-21 22:45:55 +0000 | [diff] [blame] | 470 | * Parent's group is preferred, if it doesn't satisfy these |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 471 | * conditions we search cyclically through the rest. If none |
| 472 | * of the groups look good we just look for a group with more |
| 473 | * free inodes than average (starting at parent's group). |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 474 | */ |
| 475 | |
Avantika Mathur | 2aa9fc4 | 2008-01-28 23:58:27 -0500 | [diff] [blame] | 476 | static int find_group_orlov(struct super_block *sb, struct inode *parent, |
Theodore Ts'o | a491212 | 2009-03-12 12:18:34 -0400 | [diff] [blame] | 477 | ext4_group_t *group, int mode) |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 478 | { |
Avantika Mathur | fd2d429 | 2008-01-28 23:58:27 -0500 | [diff] [blame] | 479 | ext4_group_t parent_group = EXT4_I(parent)->i_block_group; |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 480 | struct ext4_sb_info *sbi = EXT4_SB(sb); |
Avantika Mathur | fd2d429 | 2008-01-28 23:58:27 -0500 | [diff] [blame] | 481 | ext4_group_t ngroups = sbi->s_groups_count; |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 482 | int inodes_per_group = EXT4_INODES_PER_GROUP(sb); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 483 | unsigned int freei, avefreei; |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 484 | ext4_fsblk_t freeb, avefreeb; |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 485 | unsigned int ndirs; |
Theodore Ts'o | a491212 | 2009-03-12 12:18:34 -0400 | [diff] [blame] | 486 | int max_dirs, min_inodes; |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 487 | ext4_grpblk_t min_blocks; |
Theodore Ts'o | a491212 | 2009-03-12 12:18:34 -0400 | [diff] [blame] | 488 | ext4_group_t i, grp, g; |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 489 | struct ext4_group_desc *desc; |
Theodore Ts'o | a491212 | 2009-03-12 12:18:34 -0400 | [diff] [blame] | 490 | struct orlov_stats stats; |
| 491 | int flex_size = ext4_flex_bg_size(sbi); |
| 492 | |
| 493 | if (flex_size > 1) { |
| 494 | ngroups = (ngroups + flex_size - 1) >> |
| 495 | sbi->s_log_groups_per_flex; |
| 496 | parent_group >>= sbi->s_log_groups_per_flex; |
| 497 | } |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 498 | |
| 499 | freei = percpu_counter_read_positive(&sbi->s_freeinodes_counter); |
| 500 | avefreei = freei / ngroups; |
| 501 | freeb = percpu_counter_read_positive(&sbi->s_freeblocks_counter); |
Mingming Cao | 3a5b2ec | 2006-10-11 01:21:05 -0700 | [diff] [blame] | 502 | avefreeb = freeb; |
Andrew Morton | f4e5bc2 | 2006-10-11 01:21:19 -0700 | [diff] [blame] | 503 | do_div(avefreeb, ngroups); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 504 | ndirs = percpu_counter_read_positive(&sbi->s_dirs_counter); |
| 505 | |
Theodore Ts'o | a491212 | 2009-03-12 12:18:34 -0400 | [diff] [blame] | 506 | if (S_ISDIR(mode) && |
| 507 | ((parent == sb->s_root->d_inode) || |
| 508 | (EXT4_I(parent)->i_flags & EXT4_TOPDIR_FL))) { |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 509 | int best_ndir = inodes_per_group; |
Avantika Mathur | 2aa9fc4 | 2008-01-28 23:58:27 -0500 | [diff] [blame] | 510 | int ret = -1; |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 511 | |
Avantika Mathur | 2aa9fc4 | 2008-01-28 23:58:27 -0500 | [diff] [blame] | 512 | get_random_bytes(&grp, sizeof(grp)); |
| 513 | parent_group = (unsigned)grp % ngroups; |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 514 | for (i = 0; i < ngroups; i++) { |
Theodore Ts'o | a491212 | 2009-03-12 12:18:34 -0400 | [diff] [blame] | 515 | g = (parent_group + i) % ngroups; |
| 516 | get_orlov_stats(sb, g, flex_size, &stats); |
| 517 | if (!stats.free_inodes) |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 518 | continue; |
Theodore Ts'o | a491212 | 2009-03-12 12:18:34 -0400 | [diff] [blame] | 519 | if (stats.used_dirs >= best_ndir) |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 520 | continue; |
Theodore Ts'o | a491212 | 2009-03-12 12:18:34 -0400 | [diff] [blame] | 521 | if (stats.free_inodes < avefreei) |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 522 | continue; |
Theodore Ts'o | a491212 | 2009-03-12 12:18:34 -0400 | [diff] [blame] | 523 | if (stats.free_blocks < avefreeb) |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 524 | continue; |
Theodore Ts'o | a491212 | 2009-03-12 12:18:34 -0400 | [diff] [blame] | 525 | grp = g; |
Avantika Mathur | 2aa9fc4 | 2008-01-28 23:58:27 -0500 | [diff] [blame] | 526 | ret = 0; |
Theodore Ts'o | a491212 | 2009-03-12 12:18:34 -0400 | [diff] [blame] | 527 | best_ndir = stats.used_dirs; |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 528 | } |
Theodore Ts'o | a491212 | 2009-03-12 12:18:34 -0400 | [diff] [blame] | 529 | if (ret) |
| 530 | goto fallback; |
| 531 | found_flex_bg: |
| 532 | if (flex_size == 1) { |
| 533 | *group = grp; |
| 534 | return 0; |
| 535 | } |
| 536 | |
| 537 | /* |
| 538 | * We pack inodes at the beginning of the flexgroup's |
| 539 | * inode tables. Block allocation decisions will do |
| 540 | * something similar, although regular files will |
| 541 | * start at 2nd block group of the flexgroup. See |
| 542 | * ext4_ext_find_goal() and ext4_find_near(). |
| 543 | */ |
| 544 | grp *= flex_size; |
| 545 | for (i = 0; i < flex_size; i++) { |
| 546 | if (grp+i >= sbi->s_groups_count) |
| 547 | break; |
| 548 | desc = ext4_get_group_desc(sb, grp+i, NULL); |
| 549 | if (desc && ext4_free_inodes_count(sb, desc)) { |
| 550 | *group = grp+i; |
| 551 | return 0; |
| 552 | } |
| 553 | } |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 554 | goto fallback; |
| 555 | } |
| 556 | |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 557 | max_dirs = ndirs / ngroups + inodes_per_group / 16; |
Theodore Ts'o | a491212 | 2009-03-12 12:18:34 -0400 | [diff] [blame] | 558 | min_inodes = avefreei - inodes_per_group*flex_size / 4; |
| 559 | if (min_inodes < 1) |
| 560 | min_inodes = 1; |
| 561 | min_blocks = avefreeb - EXT4_BLOCKS_PER_GROUP(sb)*flex_size / 4; |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 562 | |
Theodore Ts'o | a491212 | 2009-03-12 12:18:34 -0400 | [diff] [blame] | 563 | /* |
| 564 | * Start looking in the flex group where we last allocated an |
| 565 | * inode for this parent directory |
| 566 | */ |
| 567 | if (EXT4_I(parent)->i_last_alloc_group != ~0) { |
| 568 | parent_group = EXT4_I(parent)->i_last_alloc_group; |
| 569 | if (flex_size > 1) |
| 570 | parent_group >>= sbi->s_log_groups_per_flex; |
| 571 | } |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 572 | |
| 573 | for (i = 0; i < ngroups; i++) { |
Theodore Ts'o | a491212 | 2009-03-12 12:18:34 -0400 | [diff] [blame] | 574 | grp = (parent_group + i) % ngroups; |
| 575 | get_orlov_stats(sb, grp, flex_size, &stats); |
| 576 | if (stats.used_dirs >= max_dirs) |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 577 | continue; |
Theodore Ts'o | a491212 | 2009-03-12 12:18:34 -0400 | [diff] [blame] | 578 | if (stats.free_inodes < min_inodes) |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 579 | continue; |
Theodore Ts'o | a491212 | 2009-03-12 12:18:34 -0400 | [diff] [blame] | 580 | if (stats.free_blocks < min_blocks) |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 581 | continue; |
Theodore Ts'o | a491212 | 2009-03-12 12:18:34 -0400 | [diff] [blame] | 582 | goto found_flex_bg; |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 583 | } |
| 584 | |
| 585 | fallback: |
Theodore Ts'o | a491212 | 2009-03-12 12:18:34 -0400 | [diff] [blame] | 586 | ngroups = sbi->s_groups_count; |
| 587 | avefreei = freei / ngroups; |
Theodore Ts'o | b5451f7 | 2009-04-22 21:00:36 -0400 | [diff] [blame] | 588 | fallback_retry: |
Theodore Ts'o | a491212 | 2009-03-12 12:18:34 -0400 | [diff] [blame] | 589 | parent_group = EXT4_I(parent)->i_block_group; |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 590 | for (i = 0; i < ngroups; i++) { |
Theodore Ts'o | a491212 | 2009-03-12 12:18:34 -0400 | [diff] [blame] | 591 | grp = (parent_group + i) % ngroups; |
| 592 | desc = ext4_get_group_desc(sb, grp, NULL); |
Aneesh Kumar K.V | 560671a | 2009-01-05 22:20:24 -0500 | [diff] [blame] | 593 | if (desc && ext4_free_inodes_count(sb, desc) && |
Theodore Ts'o | a491212 | 2009-03-12 12:18:34 -0400 | [diff] [blame] | 594 | ext4_free_inodes_count(sb, desc) >= avefreei) { |
| 595 | *group = grp; |
Avantika Mathur | 2aa9fc4 | 2008-01-28 23:58:27 -0500 | [diff] [blame] | 596 | return 0; |
Theodore Ts'o | a491212 | 2009-03-12 12:18:34 -0400 | [diff] [blame] | 597 | } |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 598 | } |
| 599 | |
| 600 | if (avefreei) { |
| 601 | /* |
| 602 | * The free-inodes counter is approximate, and for really small |
| 603 | * filesystems the above test can fail to find any blockgroups |
| 604 | */ |
| 605 | avefreei = 0; |
Theodore Ts'o | b5451f7 | 2009-04-22 21:00:36 -0400 | [diff] [blame] | 606 | goto fallback_retry; |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 607 | } |
| 608 | |
| 609 | return -1; |
| 610 | } |
| 611 | |
Avantika Mathur | 2aa9fc4 | 2008-01-28 23:58:27 -0500 | [diff] [blame] | 612 | static int find_group_other(struct super_block *sb, struct inode *parent, |
Theodore Ts'o | a491212 | 2009-03-12 12:18:34 -0400 | [diff] [blame] | 613 | ext4_group_t *group, int mode) |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 614 | { |
Avantika Mathur | fd2d429 | 2008-01-28 23:58:27 -0500 | [diff] [blame] | 615 | ext4_group_t parent_group = EXT4_I(parent)->i_block_group; |
| 616 | ext4_group_t ngroups = EXT4_SB(sb)->s_groups_count; |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 617 | struct ext4_group_desc *desc; |
Theodore Ts'o | a491212 | 2009-03-12 12:18:34 -0400 | [diff] [blame] | 618 | ext4_group_t i, last; |
| 619 | int flex_size = ext4_flex_bg_size(EXT4_SB(sb)); |
| 620 | |
| 621 | /* |
| 622 | * Try to place the inode is the same flex group as its |
| 623 | * parent. If we can't find space, use the Orlov algorithm to |
| 624 | * find another flex group, and store that information in the |
| 625 | * parent directory's inode information so that use that flex |
| 626 | * group for future allocations. |
| 627 | */ |
| 628 | if (flex_size > 1) { |
| 629 | int retry = 0; |
| 630 | |
| 631 | try_again: |
| 632 | parent_group &= ~(flex_size-1); |
| 633 | last = parent_group + flex_size; |
| 634 | if (last > ngroups) |
| 635 | last = ngroups; |
| 636 | for (i = parent_group; i < last; i++) { |
| 637 | desc = ext4_get_group_desc(sb, i, NULL); |
| 638 | if (desc && ext4_free_inodes_count(sb, desc)) { |
| 639 | *group = i; |
| 640 | return 0; |
| 641 | } |
| 642 | } |
| 643 | if (!retry && EXT4_I(parent)->i_last_alloc_group != ~0) { |
| 644 | retry = 1; |
| 645 | parent_group = EXT4_I(parent)->i_last_alloc_group; |
| 646 | goto try_again; |
| 647 | } |
| 648 | /* |
| 649 | * If this didn't work, use the Orlov search algorithm |
| 650 | * to find a new flex group; we pass in the mode to |
| 651 | * avoid the topdir algorithms. |
| 652 | */ |
| 653 | *group = parent_group + flex_size; |
| 654 | if (*group > ngroups) |
| 655 | *group = 0; |
| 656 | return find_group_orlov(sb, parent, group, mode); |
| 657 | } |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 658 | |
| 659 | /* |
| 660 | * Try to place the inode in its parent directory |
| 661 | */ |
Avantika Mathur | 2aa9fc4 | 2008-01-28 23:58:27 -0500 | [diff] [blame] | 662 | *group = parent_group; |
| 663 | desc = ext4_get_group_desc(sb, *group, NULL); |
Aneesh Kumar K.V | 560671a | 2009-01-05 22:20:24 -0500 | [diff] [blame] | 664 | if (desc && ext4_free_inodes_count(sb, desc) && |
| 665 | ext4_free_blks_count(sb, desc)) |
Avantika Mathur | 2aa9fc4 | 2008-01-28 23:58:27 -0500 | [diff] [blame] | 666 | return 0; |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 667 | |
| 668 | /* |
| 669 | * We're going to place this inode in a different blockgroup from its |
| 670 | * parent. We want to cause files in a common directory to all land in |
| 671 | * the same blockgroup. But we want files which are in a different |
| 672 | * directory which shares a blockgroup with our parent to land in a |
| 673 | * different blockgroup. |
| 674 | * |
| 675 | * So add our directory's i_ino into the starting point for the hash. |
| 676 | */ |
Avantika Mathur | 2aa9fc4 | 2008-01-28 23:58:27 -0500 | [diff] [blame] | 677 | *group = (*group + parent->i_ino) % ngroups; |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 678 | |
| 679 | /* |
| 680 | * Use a quadratic hash to find a group with a free inode and some free |
| 681 | * blocks. |
| 682 | */ |
| 683 | for (i = 1; i < ngroups; i <<= 1) { |
Avantika Mathur | 2aa9fc4 | 2008-01-28 23:58:27 -0500 | [diff] [blame] | 684 | *group += i; |
| 685 | if (*group >= ngroups) |
| 686 | *group -= ngroups; |
| 687 | desc = ext4_get_group_desc(sb, *group, NULL); |
Aneesh Kumar K.V | 560671a | 2009-01-05 22:20:24 -0500 | [diff] [blame] | 688 | if (desc && ext4_free_inodes_count(sb, desc) && |
| 689 | ext4_free_blks_count(sb, desc)) |
Avantika Mathur | 2aa9fc4 | 2008-01-28 23:58:27 -0500 | [diff] [blame] | 690 | return 0; |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 691 | } |
| 692 | |
| 693 | /* |
| 694 | * That failed: try linear search for a free inode, even if that group |
| 695 | * has no free blocks. |
| 696 | */ |
Avantika Mathur | 2aa9fc4 | 2008-01-28 23:58:27 -0500 | [diff] [blame] | 697 | *group = parent_group; |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 698 | for (i = 0; i < ngroups; i++) { |
Avantika Mathur | 2aa9fc4 | 2008-01-28 23:58:27 -0500 | [diff] [blame] | 699 | if (++*group >= ngroups) |
| 700 | *group = 0; |
| 701 | desc = ext4_get_group_desc(sb, *group, NULL); |
Aneesh Kumar K.V | 560671a | 2009-01-05 22:20:24 -0500 | [diff] [blame] | 702 | if (desc && ext4_free_inodes_count(sb, desc)) |
Avantika Mathur | 2aa9fc4 | 2008-01-28 23:58:27 -0500 | [diff] [blame] | 703 | return 0; |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 704 | } |
| 705 | |
| 706 | return -1; |
| 707 | } |
| 708 | |
| 709 | /* |
Aneesh Kumar K.V | 3934186 | 2009-01-05 21:38:14 -0500 | [diff] [blame] | 710 | * claim the inode from the inode bitmap. If the group |
| 711 | * is uninit we need to take the groups's sb_bgl_lock |
| 712 | * and clear the uninit flag. The inode bitmap update |
| 713 | * and group desc uninit flag clear should be done |
| 714 | * after holding sb_bgl_lock so that ext4_read_inode_bitmap |
| 715 | * doesn't race with the ext4_claim_inode |
| 716 | */ |
| 717 | static int ext4_claim_inode(struct super_block *sb, |
| 718 | struct buffer_head *inode_bitmap_bh, |
| 719 | unsigned long ino, ext4_group_t group, int mode) |
| 720 | { |
| 721 | int free = 0, retval = 0, count; |
| 722 | struct ext4_sb_info *sbi = EXT4_SB(sb); |
| 723 | struct ext4_group_desc *gdp = ext4_get_group_desc(sb, group, NULL); |
| 724 | |
| 725 | spin_lock(sb_bgl_lock(sbi, group)); |
| 726 | if (ext4_set_bit(ino, inode_bitmap_bh->b_data)) { |
| 727 | /* not a free inode */ |
| 728 | retval = 1; |
| 729 | goto err_ret; |
| 730 | } |
| 731 | ino++; |
| 732 | if ((group == 0 && ino < EXT4_FIRST_INO(sb)) || |
| 733 | ino > EXT4_INODES_PER_GROUP(sb)) { |
| 734 | spin_unlock(sb_bgl_lock(sbi, group)); |
| 735 | ext4_error(sb, __func__, |
| 736 | "reserved inode or inode > inodes count - " |
| 737 | "block_group = %u, inode=%lu", group, |
| 738 | ino + group * EXT4_INODES_PER_GROUP(sb)); |
| 739 | return 1; |
| 740 | } |
| 741 | /* If we didn't allocate from within the initialized part of the inode |
| 742 | * table then we need to initialize up to this inode. */ |
| 743 | if (EXT4_HAS_RO_COMPAT_FEATURE(sb, EXT4_FEATURE_RO_COMPAT_GDT_CSUM)) { |
| 744 | |
| 745 | if (gdp->bg_flags & cpu_to_le16(EXT4_BG_INODE_UNINIT)) { |
| 746 | gdp->bg_flags &= cpu_to_le16(~EXT4_BG_INODE_UNINIT); |
| 747 | /* When marking the block group with |
| 748 | * ~EXT4_BG_INODE_UNINIT we don't want to depend |
| 749 | * on the value of bg_itable_unused even though |
| 750 | * mke2fs could have initialized the same for us. |
| 751 | * Instead we calculated the value below |
| 752 | */ |
| 753 | |
| 754 | free = 0; |
| 755 | } else { |
| 756 | free = EXT4_INODES_PER_GROUP(sb) - |
| 757 | ext4_itable_unused_count(sb, gdp); |
| 758 | } |
| 759 | |
| 760 | /* |
| 761 | * Check the relative inode number against the last used |
| 762 | * relative inode number in this group. if it is greater |
| 763 | * we need to update the bg_itable_unused count |
| 764 | * |
| 765 | */ |
| 766 | if (ino > free) |
| 767 | ext4_itable_unused_set(sb, gdp, |
| 768 | (EXT4_INODES_PER_GROUP(sb) - ino)); |
| 769 | } |
| 770 | count = ext4_free_inodes_count(sb, gdp) - 1; |
| 771 | ext4_free_inodes_set(sb, gdp, count); |
| 772 | if (S_ISDIR(mode)) { |
| 773 | count = ext4_used_dirs_count(sb, gdp) + 1; |
| 774 | ext4_used_dirs_set(sb, gdp, count); |
Theodore Ts'o | 7d39db1 | 2009-03-04 19:31:53 -0500 | [diff] [blame] | 775 | if (sbi->s_log_groups_per_flex) { |
| 776 | ext4_group_t f = ext4_flex_group(sbi, group); |
| 777 | |
| 778 | atomic_inc(&sbi->s_flex_groups[f].free_inodes); |
| 779 | } |
Aneesh Kumar K.V | 3934186 | 2009-01-05 21:38:14 -0500 | [diff] [blame] | 780 | } |
| 781 | gdp->bg_checksum = ext4_group_desc_csum(sbi, group, gdp); |
| 782 | err_ret: |
| 783 | spin_unlock(sb_bgl_lock(sbi, group)); |
| 784 | return retval; |
| 785 | } |
| 786 | |
| 787 | /* |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 788 | * There are two policies for allocating an inode. If the new inode is |
| 789 | * a directory, then a forward search is made for a block group with both |
| 790 | * free space and a low directory-to-inode ratio; if that fails, then of |
| 791 | * the groups with above-average free space, that group with the fewest |
| 792 | * directories already is chosen. |
| 793 | * |
| 794 | * For other inodes, search forward from the parent directory's block |
| 795 | * group to find a free inode. |
| 796 | */ |
Theodore Ts'o | af5bc92 | 2008-09-08 22:25:24 -0400 | [diff] [blame] | 797 | struct inode *ext4_new_inode(handle_t *handle, struct inode *dir, int mode) |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 798 | { |
| 799 | struct super_block *sb; |
Aneesh Kumar K.V | 3300bed | 2009-01-03 22:33:39 -0500 | [diff] [blame] | 800 | struct buffer_head *inode_bitmap_bh = NULL; |
| 801 | struct buffer_head *group_desc_bh; |
Avantika Mathur | 2aa9fc4 | 2008-01-28 23:58:27 -0500 | [diff] [blame] | 802 | ext4_group_t group = 0; |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 803 | unsigned long ino = 0; |
Theodore Ts'o | af5bc92 | 2008-09-08 22:25:24 -0400 | [diff] [blame] | 804 | struct inode *inode; |
| 805 | struct ext4_group_desc *gdp = NULL; |
| 806 | struct ext4_super_block *es; |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 807 | struct ext4_inode_info *ei; |
| 808 | struct ext4_sb_info *sbi; |
Aneesh Kumar K.V | 3934186 | 2009-01-05 21:38:14 -0500 | [diff] [blame] | 809 | int ret2, err = 0; |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 810 | struct inode *ret; |
Avantika Mathur | 2aa9fc4 | 2008-01-28 23:58:27 -0500 | [diff] [blame] | 811 | ext4_group_t i; |
| 812 | int free = 0; |
Theodore Ts'o | 2842c3b | 2009-03-12 12:20:01 -0400 | [diff] [blame] | 813 | static int once = 1; |
Jose R. Santos | 772cb7c | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 814 | ext4_group_t flex_group; |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 815 | |
| 816 | /* Cannot create files in a deleted directory */ |
| 817 | if (!dir || !dir->i_nlink) |
| 818 | return ERR_PTR(-EPERM); |
| 819 | |
| 820 | sb = dir->i_sb; |
Theodore Ts'o | ba80b10 | 2009-01-03 20:03:21 -0500 | [diff] [blame] | 821 | trace_mark(ext4_request_inode, "dev %s dir %lu mode %d", sb->s_id, |
| 822 | dir->i_ino, mode); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 823 | inode = new_inode(sb); |
| 824 | if (!inode) |
| 825 | return ERR_PTR(-ENOMEM); |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 826 | ei = EXT4_I(inode); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 827 | |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 828 | sbi = EXT4_SB(sb); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 829 | es = sbi->s_es; |
Jose R. Santos | 772cb7c | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 830 | |
Theodore Ts'o | a491212 | 2009-03-12 12:18:34 -0400 | [diff] [blame] | 831 | if (sbi->s_log_groups_per_flex && test_opt(sb, OLDALLOC)) { |
Jose R. Santos | 772cb7c | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 832 | ret2 = find_group_flex(sb, dir, &group); |
Theodore Ts'o | 05bf9e8 | 2009-02-21 12:13:24 -0500 | [diff] [blame] | 833 | if (ret2 == -1) { |
Theodore Ts'o | a491212 | 2009-03-12 12:18:34 -0400 | [diff] [blame] | 834 | ret2 = find_group_other(sb, dir, &group, mode); |
Chuck Ebbert | 6b82f3c | 2009-04-14 07:37:40 -0400 | [diff] [blame] | 835 | if (ret2 == 0 && once) { |
Theodore Ts'o | 2842c3b | 2009-03-12 12:20:01 -0400 | [diff] [blame] | 836 | once = 0; |
Theodore Ts'o | 05bf9e8 | 2009-02-21 12:13:24 -0500 | [diff] [blame] | 837 | printk(KERN_NOTICE "ext4: find_group_flex " |
| 838 | "failed, fallback succeeded dir %lu\n", |
| 839 | dir->i_ino); |
Chuck Ebbert | 6b82f3c | 2009-04-14 07:37:40 -0400 | [diff] [blame] | 840 | } |
Theodore Ts'o | 05bf9e8 | 2009-02-21 12:13:24 -0500 | [diff] [blame] | 841 | } |
Jose R. Santos | 772cb7c | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 842 | goto got_group; |
| 843 | } |
| 844 | |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 845 | if (S_ISDIR(mode)) { |
Theodore Ts'o | af5bc92 | 2008-09-08 22:25:24 -0400 | [diff] [blame] | 846 | if (test_opt(sb, OLDALLOC)) |
Avantika Mathur | 2aa9fc4 | 2008-01-28 23:58:27 -0500 | [diff] [blame] | 847 | ret2 = find_group_dir(sb, dir, &group); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 848 | else |
Theodore Ts'o | a491212 | 2009-03-12 12:18:34 -0400 | [diff] [blame] | 849 | ret2 = find_group_orlov(sb, dir, &group, mode); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 850 | } else |
Theodore Ts'o | a491212 | 2009-03-12 12:18:34 -0400 | [diff] [blame] | 851 | ret2 = find_group_other(sb, dir, &group, mode); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 852 | |
Jose R. Santos | 772cb7c | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 853 | got_group: |
Theodore Ts'o | a491212 | 2009-03-12 12:18:34 -0400 | [diff] [blame] | 854 | EXT4_I(dir)->i_last_alloc_group = group; |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 855 | err = -ENOSPC; |
Avantika Mathur | 2aa9fc4 | 2008-01-28 23:58:27 -0500 | [diff] [blame] | 856 | if (ret2 == -1) |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 857 | goto out; |
| 858 | |
| 859 | for (i = 0; i < sbi->s_groups_count; i++) { |
| 860 | err = -EIO; |
| 861 | |
Aneesh Kumar K.V | 3300bed | 2009-01-03 22:33:39 -0500 | [diff] [blame] | 862 | gdp = ext4_get_group_desc(sb, group, &group_desc_bh); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 863 | if (!gdp) |
| 864 | goto fail; |
| 865 | |
Aneesh Kumar K.V | 3300bed | 2009-01-03 22:33:39 -0500 | [diff] [blame] | 866 | brelse(inode_bitmap_bh); |
| 867 | inode_bitmap_bh = ext4_read_inode_bitmap(sb, group); |
| 868 | if (!inode_bitmap_bh) |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 869 | goto fail; |
| 870 | |
| 871 | ino = 0; |
| 872 | |
| 873 | repeat_in_this_group: |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 874 | ino = ext4_find_next_zero_bit((unsigned long *) |
Aneesh Kumar K.V | 3300bed | 2009-01-03 22:33:39 -0500 | [diff] [blame] | 875 | inode_bitmap_bh->b_data, |
| 876 | EXT4_INODES_PER_GROUP(sb), ino); |
| 877 | |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 878 | if (ino < EXT4_INODES_PER_GROUP(sb)) { |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 879 | |
Aneesh Kumar K.V | 3300bed | 2009-01-03 22:33:39 -0500 | [diff] [blame] | 880 | BUFFER_TRACE(inode_bitmap_bh, "get_write_access"); |
| 881 | err = ext4_journal_get_write_access(handle, |
| 882 | inode_bitmap_bh); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 883 | if (err) |
| 884 | goto fail; |
| 885 | |
Aneesh Kumar K.V | 3934186 | 2009-01-05 21:38:14 -0500 | [diff] [blame] | 886 | BUFFER_TRACE(group_desc_bh, "get_write_access"); |
| 887 | err = ext4_journal_get_write_access(handle, |
| 888 | group_desc_bh); |
| 889 | if (err) |
| 890 | goto fail; |
| 891 | if (!ext4_claim_inode(sb, inode_bitmap_bh, |
| 892 | ino, group, mode)) { |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 893 | /* we won it */ |
Aneesh Kumar K.V | 3300bed | 2009-01-03 22:33:39 -0500 | [diff] [blame] | 894 | BUFFER_TRACE(inode_bitmap_bh, |
Frank Mayhar | 0390131 | 2009-01-07 00:06:22 -0500 | [diff] [blame] | 895 | "call ext4_handle_dirty_metadata"); |
| 896 | err = ext4_handle_dirty_metadata(handle, |
Aneesh Kumar K.V | 3300bed | 2009-01-03 22:33:39 -0500 | [diff] [blame] | 897 | inode, |
| 898 | inode_bitmap_bh); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 899 | if (err) |
| 900 | goto fail; |
Aneesh Kumar K.V | 3934186 | 2009-01-05 21:38:14 -0500 | [diff] [blame] | 901 | /* zero bit is inode number 1*/ |
| 902 | ino++; |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 903 | goto got; |
| 904 | } |
| 905 | /* we lost it */ |
Aneesh Kumar K.V | 3300bed | 2009-01-03 22:33:39 -0500 | [diff] [blame] | 906 | ext4_handle_release_buffer(handle, inode_bitmap_bh); |
Aneesh Kumar K.V | 3934186 | 2009-01-05 21:38:14 -0500 | [diff] [blame] | 907 | ext4_handle_release_buffer(handle, group_desc_bh); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 908 | |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 909 | if (++ino < EXT4_INODES_PER_GROUP(sb)) |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 910 | goto repeat_in_this_group; |
| 911 | } |
| 912 | |
| 913 | /* |
| 914 | * This case is possible in concurrent environment. It is very |
| 915 | * rare. We cannot repeat the find_group_xxx() call because |
| 916 | * that will simply return the same blockgroup, because the |
| 917 | * group descriptor metadata has not yet been updated. |
| 918 | * So we just go onto the next blockgroup. |
| 919 | */ |
| 920 | if (++group == sbi->s_groups_count) |
| 921 | group = 0; |
| 922 | } |
| 923 | err = -ENOSPC; |
| 924 | goto out; |
| 925 | |
| 926 | got: |
Andreas Dilger | 717d50e | 2007-10-16 18:38:25 -0400 | [diff] [blame] | 927 | /* We may have to initialize the block bitmap if it isn't already */ |
| 928 | if (EXT4_HAS_RO_COMPAT_FEATURE(sb, EXT4_FEATURE_RO_COMPAT_GDT_CSUM) && |
| 929 | gdp->bg_flags & cpu_to_le16(EXT4_BG_BLOCK_UNINIT)) { |
Aneesh Kumar K.V | 3300bed | 2009-01-03 22:33:39 -0500 | [diff] [blame] | 930 | struct buffer_head *block_bitmap_bh; |
Andreas Dilger | 717d50e | 2007-10-16 18:38:25 -0400 | [diff] [blame] | 931 | |
Aneesh Kumar K.V | 3300bed | 2009-01-03 22:33:39 -0500 | [diff] [blame] | 932 | block_bitmap_bh = ext4_read_block_bitmap(sb, group); |
| 933 | BUFFER_TRACE(block_bitmap_bh, "get block bitmap access"); |
| 934 | err = ext4_journal_get_write_access(handle, block_bitmap_bh); |
Andreas Dilger | 717d50e | 2007-10-16 18:38:25 -0400 | [diff] [blame] | 935 | if (err) { |
Aneesh Kumar K.V | 3300bed | 2009-01-03 22:33:39 -0500 | [diff] [blame] | 936 | brelse(block_bitmap_bh); |
Andreas Dilger | 717d50e | 2007-10-16 18:38:25 -0400 | [diff] [blame] | 937 | goto fail; |
| 938 | } |
| 939 | |
| 940 | free = 0; |
| 941 | spin_lock(sb_bgl_lock(sbi, group)); |
| 942 | /* recheck and clear flag under lock if we still need to */ |
| 943 | if (gdp->bg_flags & cpu_to_le16(EXT4_BG_BLOCK_UNINIT)) { |
Andreas Dilger | 717d50e | 2007-10-16 18:38:25 -0400 | [diff] [blame] | 944 | free = ext4_free_blocks_after_init(sb, group, gdp); |
Aneesh Kumar K.V | 3300bed | 2009-01-03 22:33:39 -0500 | [diff] [blame] | 945 | gdp->bg_flags &= cpu_to_le16(~EXT4_BG_BLOCK_UNINIT); |
Aneesh Kumar K.V | 560671a | 2009-01-05 22:20:24 -0500 | [diff] [blame] | 946 | ext4_free_blks_set(sb, gdp, free); |
Frederic Bohe | 23712a9 | 2008-11-07 09:21:01 -0500 | [diff] [blame] | 947 | gdp->bg_checksum = ext4_group_desc_csum(sbi, group, |
| 948 | gdp); |
Andreas Dilger | 717d50e | 2007-10-16 18:38:25 -0400 | [diff] [blame] | 949 | } |
| 950 | spin_unlock(sb_bgl_lock(sbi, group)); |
| 951 | |
| 952 | /* Don't need to dirty bitmap block if we didn't change it */ |
| 953 | if (free) { |
Aneesh Kumar K.V | 3300bed | 2009-01-03 22:33:39 -0500 | [diff] [blame] | 954 | BUFFER_TRACE(block_bitmap_bh, "dirty block bitmap"); |
Frank Mayhar | 0390131 | 2009-01-07 00:06:22 -0500 | [diff] [blame] | 955 | err = ext4_handle_dirty_metadata(handle, |
Aneesh Kumar K.V | 3300bed | 2009-01-03 22:33:39 -0500 | [diff] [blame] | 956 | NULL, block_bitmap_bh); |
Andreas Dilger | 717d50e | 2007-10-16 18:38:25 -0400 | [diff] [blame] | 957 | } |
| 958 | |
Aneesh Kumar K.V | 3300bed | 2009-01-03 22:33:39 -0500 | [diff] [blame] | 959 | brelse(block_bitmap_bh); |
Andreas Dilger | 717d50e | 2007-10-16 18:38:25 -0400 | [diff] [blame] | 960 | if (err) |
| 961 | goto fail; |
| 962 | } |
Aneesh Kumar K.V | 3300bed | 2009-01-03 22:33:39 -0500 | [diff] [blame] | 963 | BUFFER_TRACE(group_desc_bh, "call ext4_handle_dirty_metadata"); |
| 964 | err = ext4_handle_dirty_metadata(handle, NULL, group_desc_bh); |
Aneesh Kumar K.V | 3934186 | 2009-01-05 21:38:14 -0500 | [diff] [blame] | 965 | if (err) |
| 966 | goto fail; |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 967 | |
| 968 | percpu_counter_dec(&sbi->s_freeinodes_counter); |
| 969 | if (S_ISDIR(mode)) |
| 970 | percpu_counter_inc(&sbi->s_dirs_counter); |
| 971 | sb->s_dirt = 1; |
| 972 | |
Jose R. Santos | 772cb7c | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 973 | if (sbi->s_log_groups_per_flex) { |
| 974 | flex_group = ext4_flex_group(sbi, group); |
Theodore Ts'o | 9f24e42 | 2009-03-04 19:09:10 -0500 | [diff] [blame] | 975 | atomic_dec(&sbi->s_flex_groups[flex_group].free_inodes); |
Jose R. Santos | 772cb7c | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 976 | } |
| 977 | |
David Howells | 4c9c544 | 2008-11-14 10:38:51 +1100 | [diff] [blame] | 978 | inode->i_uid = current_fsuid(); |
Theodore Ts'o | af5bc92 | 2008-09-08 22:25:24 -0400 | [diff] [blame] | 979 | if (test_opt(sb, GRPID)) |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 980 | inode->i_gid = dir->i_gid; |
| 981 | else if (dir->i_mode & S_ISGID) { |
| 982 | inode->i_gid = dir->i_gid; |
| 983 | if (S_ISDIR(mode)) |
| 984 | mode |= S_ISGID; |
| 985 | } else |
David Howells | 4c9c544 | 2008-11-14 10:38:51 +1100 | [diff] [blame] | 986 | inode->i_gid = current_fsgid(); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 987 | inode->i_mode = mode; |
| 988 | |
Andreas Dilger | 717d50e | 2007-10-16 18:38:25 -0400 | [diff] [blame] | 989 | inode->i_ino = ino + group * EXT4_INODES_PER_GROUP(sb); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 990 | /* This is the optimal IO size (for stat), not the fs block size */ |
| 991 | inode->i_blocks = 0; |
Kalpak Shah | ef7f383 | 2007-07-18 09:15:20 -0400 | [diff] [blame] | 992 | inode->i_mtime = inode->i_atime = inode->i_ctime = ei->i_crtime = |
| 993 | ext4_current_time(inode); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 994 | |
| 995 | memset(ei->i_data, 0, sizeof(ei->i_data)); |
| 996 | ei->i_dir_start_lookup = 0; |
| 997 | ei->i_disksize = 0; |
| 998 | |
Aneesh Kumar K.V | 42bf038 | 2008-02-25 16:38:03 -0500 | [diff] [blame] | 999 | /* |
Duane Griffin | 2dc6b0d | 2009-02-15 18:09:20 -0500 | [diff] [blame] | 1000 | * Don't inherit extent flag from directory, amongst others. We set |
| 1001 | * extent flag on newly created directory and file only if -o extent |
| 1002 | * mount option is specified |
Aneesh Kumar K.V | 42bf038 | 2008-02-25 16:38:03 -0500 | [diff] [blame] | 1003 | */ |
Duane Griffin | 2dc6b0d | 2009-02-15 18:09:20 -0500 | [diff] [blame] | 1004 | ei->i_flags = |
| 1005 | ext4_mask_flags(mode, EXT4_I(dir)->i_flags & EXT4_FL_INHERITED); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 1006 | ei->i_file_acl = 0; |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 1007 | ei->i_dtime = 0; |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 1008 | ei->i_block_group = group; |
Theodore Ts'o | a491212 | 2009-03-12 12:18:34 -0400 | [diff] [blame] | 1009 | ei->i_last_alloc_group = ~0; |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 1010 | |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 1011 | ext4_set_inode_flags(inode); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 1012 | if (IS_DIRSYNC(inode)) |
Frank Mayhar | 0390131 | 2009-01-07 00:06:22 -0500 | [diff] [blame] | 1013 | ext4_handle_sync(handle); |
Al Viro | 6b38e84 | 2008-12-30 02:03:31 -0500 | [diff] [blame] | 1014 | if (insert_inode_locked(inode) < 0) { |
| 1015 | err = -EINVAL; |
| 1016 | goto fail_drop; |
| 1017 | } |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 1018 | spin_lock(&sbi->s_next_gen_lock); |
| 1019 | inode->i_generation = sbi->s_next_generation++; |
| 1020 | spin_unlock(&sbi->s_next_gen_lock); |
| 1021 | |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 1022 | ei->i_state = EXT4_STATE_NEW; |
Kalpak Shah | ef7f383 | 2007-07-18 09:15:20 -0400 | [diff] [blame] | 1023 | |
| 1024 | ei->i_extra_isize = EXT4_SB(sb)->s_want_extra_isize; |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 1025 | |
| 1026 | ret = inode; |
Jan Kara | a269eb1 | 2009-01-26 17:04:39 +0100 | [diff] [blame] | 1027 | if (vfs_dq_alloc_inode(inode)) { |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 1028 | err = -EDQUOT; |
| 1029 | goto fail_drop; |
| 1030 | } |
| 1031 | |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 1032 | err = ext4_init_acl(handle, inode, dir); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 1033 | if (err) |
| 1034 | goto fail_free_drop; |
| 1035 | |
Theodore Ts'o | af5bc92 | 2008-09-08 22:25:24 -0400 | [diff] [blame] | 1036 | err = ext4_init_security(handle, inode, dir); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 1037 | if (err) |
| 1038 | goto fail_free_drop; |
| 1039 | |
Theodore Ts'o | 83982b6 | 2009-01-06 14:53:16 -0500 | [diff] [blame] | 1040 | if (EXT4_HAS_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_EXTENTS)) { |
Eric Sandeen | e4079a1 | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 1041 | /* set extent flag only for directory, file and normal symlink*/ |
Aneesh Kumar K.V | e65187e | 2008-04-29 08:11:12 -0400 | [diff] [blame] | 1042 | if (S_ISDIR(mode) || S_ISREG(mode) || S_ISLNK(mode)) { |
Aneesh Kumar K.V | 42bf038 | 2008-02-25 16:38:03 -0500 | [diff] [blame] | 1043 | EXT4_I(inode)->i_flags |= EXT4_EXTENTS_FL; |
| 1044 | ext4_ext_tree_init(handle, inode); |
Aneesh Kumar K.V | 42bf038 | 2008-02-25 16:38:03 -0500 | [diff] [blame] | 1045 | } |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 1046 | } |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 1047 | |
Aneesh Kumar K.V | 8753e88 | 2008-04-29 22:00:36 -0400 | [diff] [blame] | 1048 | err = ext4_mark_inode_dirty(handle, inode); |
| 1049 | if (err) { |
| 1050 | ext4_std_error(sb, err); |
| 1051 | goto fail_free_drop; |
| 1052 | } |
| 1053 | |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 1054 | ext4_debug("allocating inode %lu\n", inode->i_ino); |
Theodore Ts'o | ba80b10 | 2009-01-03 20:03:21 -0500 | [diff] [blame] | 1055 | trace_mark(ext4_allocate_inode, "dev %s ino %lu dir %lu mode %d", |
| 1056 | sb->s_id, inode->i_ino, dir->i_ino, mode); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 1057 | goto really_out; |
| 1058 | fail: |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 1059 | ext4_std_error(sb, err); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 1060 | out: |
| 1061 | iput(inode); |
| 1062 | ret = ERR_PTR(err); |
| 1063 | really_out: |
Aneesh Kumar K.V | 3300bed | 2009-01-03 22:33:39 -0500 | [diff] [blame] | 1064 | brelse(inode_bitmap_bh); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 1065 | return ret; |
| 1066 | |
| 1067 | fail_free_drop: |
Jan Kara | a269eb1 | 2009-01-26 17:04:39 +0100 | [diff] [blame] | 1068 | vfs_dq_free_inode(inode); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 1069 | |
| 1070 | fail_drop: |
Jan Kara | a269eb1 | 2009-01-26 17:04:39 +0100 | [diff] [blame] | 1071 | vfs_dq_drop(inode); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 1072 | inode->i_flags |= S_NOQUOTA; |
| 1073 | inode->i_nlink = 0; |
Al Viro | 6b38e84 | 2008-12-30 02:03:31 -0500 | [diff] [blame] | 1074 | unlock_new_inode(inode); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 1075 | iput(inode); |
Aneesh Kumar K.V | 3300bed | 2009-01-03 22:33:39 -0500 | [diff] [blame] | 1076 | brelse(inode_bitmap_bh); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 1077 | return ERR_PTR(err); |
| 1078 | } |
| 1079 | |
| 1080 | /* Verify that we are loading a valid orphan from disk */ |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 1081 | struct inode *ext4_orphan_get(struct super_block *sb, unsigned long ino) |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 1082 | { |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 1083 | unsigned long max_ino = le32_to_cpu(EXT4_SB(sb)->s_es->s_inodes_count); |
Avantika Mathur | fd2d429 | 2008-01-28 23:58:27 -0500 | [diff] [blame] | 1084 | ext4_group_t block_group; |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 1085 | int bit; |
David Howells | 1d1fe1e | 2008-02-07 00:15:37 -0800 | [diff] [blame] | 1086 | struct buffer_head *bitmap_bh; |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 1087 | struct inode *inode = NULL; |
David Howells | 1d1fe1e | 2008-02-07 00:15:37 -0800 | [diff] [blame] | 1088 | long err = -EIO; |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 1089 | |
| 1090 | /* Error cases - e2fsck has already cleaned up for us */ |
| 1091 | if (ino > max_ino) { |
Harvey Harrison | 46e665e | 2008-04-17 10:38:59 -0400 | [diff] [blame] | 1092 | ext4_warning(sb, __func__, |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 1093 | "bad orphan ino %lu! e2fsck was run?", ino); |
David Howells | 1d1fe1e | 2008-02-07 00:15:37 -0800 | [diff] [blame] | 1094 | goto error; |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 1095 | } |
| 1096 | |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 1097 | block_group = (ino - 1) / EXT4_INODES_PER_GROUP(sb); |
| 1098 | bit = (ino - 1) % EXT4_INODES_PER_GROUP(sb); |
Eric Sandeen | e29d1cd | 2008-08-02 21:21:02 -0400 | [diff] [blame] | 1099 | bitmap_bh = ext4_read_inode_bitmap(sb, block_group); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 1100 | if (!bitmap_bh) { |
Harvey Harrison | 46e665e | 2008-04-17 10:38:59 -0400 | [diff] [blame] | 1101 | ext4_warning(sb, __func__, |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 1102 | "inode bitmap error for orphan %lu", ino); |
David Howells | 1d1fe1e | 2008-02-07 00:15:37 -0800 | [diff] [blame] | 1103 | goto error; |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 1104 | } |
| 1105 | |
| 1106 | /* Having the inode bit set should be a 100% indicator that this |
| 1107 | * is a valid orphan (no e2fsck run on fs). Orphans also include |
| 1108 | * inodes that were being truncated, so we can't check i_nlink==0. |
| 1109 | */ |
David Howells | 1d1fe1e | 2008-02-07 00:15:37 -0800 | [diff] [blame] | 1110 | if (!ext4_test_bit(bit, bitmap_bh->b_data)) |
| 1111 | goto bad_orphan; |
| 1112 | |
| 1113 | inode = ext4_iget(sb, ino); |
| 1114 | if (IS_ERR(inode)) |
| 1115 | goto iget_failed; |
| 1116 | |
Duane Griffin | 91ef4ca | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 1117 | /* |
| 1118 | * If the orphans has i_nlinks > 0 then it should be able to be |
| 1119 | * truncated, otherwise it won't be removed from the orphan list |
| 1120 | * during processing and an infinite loop will result. |
| 1121 | */ |
| 1122 | if (inode->i_nlink && !ext4_can_truncate(inode)) |
| 1123 | goto bad_orphan; |
| 1124 | |
David Howells | 1d1fe1e | 2008-02-07 00:15:37 -0800 | [diff] [blame] | 1125 | if (NEXT_ORPHAN(inode) > max_ino) |
| 1126 | goto bad_orphan; |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 1127 | brelse(bitmap_bh); |
| 1128 | return inode; |
David Howells | 1d1fe1e | 2008-02-07 00:15:37 -0800 | [diff] [blame] | 1129 | |
| 1130 | iget_failed: |
| 1131 | err = PTR_ERR(inode); |
| 1132 | inode = NULL; |
| 1133 | bad_orphan: |
Harvey Harrison | 46e665e | 2008-04-17 10:38:59 -0400 | [diff] [blame] | 1134 | ext4_warning(sb, __func__, |
David Howells | 1d1fe1e | 2008-02-07 00:15:37 -0800 | [diff] [blame] | 1135 | "bad orphan inode %lu! e2fsck was run?", ino); |
| 1136 | printk(KERN_NOTICE "ext4_test_bit(bit=%d, block=%llu) = %d\n", |
| 1137 | bit, (unsigned long long)bitmap_bh->b_blocknr, |
| 1138 | ext4_test_bit(bit, bitmap_bh->b_data)); |
| 1139 | printk(KERN_NOTICE "inode=%p\n", inode); |
| 1140 | if (inode) { |
| 1141 | printk(KERN_NOTICE "is_bad_inode(inode)=%d\n", |
| 1142 | is_bad_inode(inode)); |
| 1143 | printk(KERN_NOTICE "NEXT_ORPHAN(inode)=%u\n", |
| 1144 | NEXT_ORPHAN(inode)); |
| 1145 | printk(KERN_NOTICE "max_ino=%lu\n", max_ino); |
Duane Griffin | 91ef4ca | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 1146 | printk(KERN_NOTICE "i_nlink=%u\n", inode->i_nlink); |
David Howells | 1d1fe1e | 2008-02-07 00:15:37 -0800 | [diff] [blame] | 1147 | /* Avoid freeing blocks if we got a bad deleted inode */ |
| 1148 | if (inode->i_nlink == 0) |
| 1149 | inode->i_blocks = 0; |
| 1150 | iput(inode); |
| 1151 | } |
| 1152 | brelse(bitmap_bh); |
| 1153 | error: |
| 1154 | return ERR_PTR(err); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 1155 | } |
| 1156 | |
Theodore Ts'o | af5bc92 | 2008-09-08 22:25:24 -0400 | [diff] [blame] | 1157 | unsigned long ext4_count_free_inodes(struct super_block *sb) |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 1158 | { |
| 1159 | unsigned long desc_count; |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 1160 | struct ext4_group_desc *gdp; |
Avantika Mathur | fd2d429 | 2008-01-28 23:58:27 -0500 | [diff] [blame] | 1161 | ext4_group_t i; |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 1162 | #ifdef EXT4FS_DEBUG |
| 1163 | struct ext4_super_block *es; |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 1164 | unsigned long bitmap_count, x; |
| 1165 | struct buffer_head *bitmap_bh = NULL; |
| 1166 | |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 1167 | es = EXT4_SB(sb)->s_es; |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 1168 | desc_count = 0; |
| 1169 | bitmap_count = 0; |
| 1170 | gdp = NULL; |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 1171 | for (i = 0; i < EXT4_SB(sb)->s_groups_count; i++) { |
Theodore Ts'o | af5bc92 | 2008-09-08 22:25:24 -0400 | [diff] [blame] | 1172 | gdp = ext4_get_group_desc(sb, i, NULL); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 1173 | if (!gdp) |
| 1174 | continue; |
Aneesh Kumar K.V | 560671a | 2009-01-05 22:20:24 -0500 | [diff] [blame] | 1175 | desc_count += ext4_free_inodes_count(sb, gdp); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 1176 | brelse(bitmap_bh); |
Eric Sandeen | e29d1cd | 2008-08-02 21:21:02 -0400 | [diff] [blame] | 1177 | bitmap_bh = ext4_read_inode_bitmap(sb, i); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 1178 | if (!bitmap_bh) |
| 1179 | continue; |
| 1180 | |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 1181 | x = ext4_count_free(bitmap_bh, EXT4_INODES_PER_GROUP(sb) / 8); |
Eric Sandeen | c549a95 | 2008-01-28 23:58:27 -0500 | [diff] [blame] | 1182 | printk(KERN_DEBUG "group %lu: stored = %d, counted = %lu\n", |
Aneesh Kumar K.V | 560671a | 2009-01-05 22:20:24 -0500 | [diff] [blame] | 1183 | i, ext4_free_inodes_count(sb, gdp), x); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 1184 | bitmap_count += x; |
| 1185 | } |
| 1186 | brelse(bitmap_bh); |
Theodore Ts'o | 4776004 | 2008-09-08 23:00:52 -0400 | [diff] [blame] | 1187 | printk(KERN_DEBUG "ext4_count_free_inodes: " |
| 1188 | "stored = %u, computed = %lu, %lu\n", |
| 1189 | le32_to_cpu(es->s_free_inodes_count), desc_count, bitmap_count); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 1190 | return desc_count; |
| 1191 | #else |
| 1192 | desc_count = 0; |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 1193 | for (i = 0; i < EXT4_SB(sb)->s_groups_count; i++) { |
Theodore Ts'o | af5bc92 | 2008-09-08 22:25:24 -0400 | [diff] [blame] | 1194 | gdp = ext4_get_group_desc(sb, i, NULL); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 1195 | if (!gdp) |
| 1196 | continue; |
Aneesh Kumar K.V | 560671a | 2009-01-05 22:20:24 -0500 | [diff] [blame] | 1197 | desc_count += ext4_free_inodes_count(sb, gdp); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 1198 | cond_resched(); |
| 1199 | } |
| 1200 | return desc_count; |
| 1201 | #endif |
| 1202 | } |
| 1203 | |
| 1204 | /* Called at mount-time, super-block is locked */ |
Theodore Ts'o | af5bc92 | 2008-09-08 22:25:24 -0400 | [diff] [blame] | 1205 | unsigned long ext4_count_dirs(struct super_block * sb) |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 1206 | { |
| 1207 | unsigned long count = 0; |
Avantika Mathur | fd2d429 | 2008-01-28 23:58:27 -0500 | [diff] [blame] | 1208 | ext4_group_t i; |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 1209 | |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 1210 | for (i = 0; i < EXT4_SB(sb)->s_groups_count; i++) { |
Theodore Ts'o | af5bc92 | 2008-09-08 22:25:24 -0400 | [diff] [blame] | 1211 | struct ext4_group_desc *gdp = ext4_get_group_desc(sb, i, NULL); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 1212 | if (!gdp) |
| 1213 | continue; |
Aneesh Kumar K.V | 560671a | 2009-01-05 22:20:24 -0500 | [diff] [blame] | 1214 | count += ext4_used_dirs_count(sb, gdp); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 1215 | } |
| 1216 | return count; |
| 1217 | } |