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> |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 18 | #include <linux/ext4_fs.h> |
Mingming Cao | dab291a | 2006-10-11 01:21:01 -0700 | [diff] [blame] | 19 | #include <linux/ext4_jbd2.h> |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 20 | #include <linux/stat.h> |
| 21 | #include <linux/string.h> |
| 22 | #include <linux/quotaops.h> |
| 23 | #include <linux/buffer_head.h> |
| 24 | #include <linux/random.h> |
| 25 | #include <linux/bitops.h> |
Mingming Cao | 3a5b2ec | 2006-10-11 01:21:05 -0700 | [diff] [blame] | 26 | #include <linux/blkdev.h> |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 27 | #include <asm/byteorder.h> |
| 28 | |
| 29 | #include "xattr.h" |
| 30 | #include "acl.h" |
Andreas Dilger | 717d50e | 2007-10-16 18:38:25 -0400 | [diff] [blame] | 31 | #include "group.h" |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 32 | |
| 33 | /* |
| 34 | * ialloc.c contains the inodes allocation and deallocation routines |
| 35 | */ |
| 36 | |
| 37 | /* |
| 38 | * The free inodes are managed by bitmaps. A file system contains several |
| 39 | * blocks groups. Each group contains 1 bitmap block for blocks, 1 bitmap |
| 40 | * block for inodes, N blocks for the inode table and data blocks. |
| 41 | * |
| 42 | * The file system contains group descriptors which are located after the |
| 43 | * super block. Each descriptor contains the number of the bitmap block and |
| 44 | * the free blocks count in the block. |
| 45 | */ |
| 46 | |
Andreas Dilger | 717d50e | 2007-10-16 18:38:25 -0400 | [diff] [blame] | 47 | /* |
| 48 | * To avoid calling the atomic setbit hundreds or thousands of times, we only |
| 49 | * need to use it within a single byte (to ensure we get endianness right). |
| 50 | * We can use memset for the rest of the bitmap as there are no other users. |
| 51 | */ |
| 52 | void mark_bitmap_end(int start_bit, int end_bit, char *bitmap) |
| 53 | { |
| 54 | int i; |
| 55 | |
| 56 | if (start_bit >= end_bit) |
| 57 | return; |
| 58 | |
| 59 | ext4_debug("mark end bits +%d through +%d used\n", start_bit, end_bit); |
| 60 | for (i = start_bit; i < ((start_bit + 7) & ~7UL); i++) |
| 61 | ext4_set_bit(i, bitmap); |
| 62 | if (i < end_bit) |
| 63 | memset(bitmap + (i >> 3), 0xff, (end_bit - i) >> 3); |
| 64 | } |
| 65 | |
| 66 | /* Initializes an uninitialized inode bitmap */ |
Avantika Mathur | fd2d429 | 2008-01-28 23:58:27 -0500 | [diff] [blame] | 67 | unsigned ext4_init_inode_bitmap(struct super_block *sb, struct buffer_head *bh, |
| 68 | ext4_group_t block_group, |
Andreas Dilger | 717d50e | 2007-10-16 18:38:25 -0400 | [diff] [blame] | 69 | struct ext4_group_desc *gdp) |
| 70 | { |
| 71 | struct ext4_sb_info *sbi = EXT4_SB(sb); |
| 72 | |
| 73 | J_ASSERT_BH(bh, buffer_locked(bh)); |
| 74 | |
| 75 | /* If checksum is bad mark all blocks and inodes use to prevent |
| 76 | * allocation, essentially implementing a per-group read-only flag. */ |
| 77 | if (!ext4_group_desc_csum_verify(sbi, block_group, gdp)) { |
Harvey Harrison | 46e665e | 2008-04-17 10:38:59 -0400 | [diff] [blame^] | 78 | ext4_error(sb, __func__, "Checksum bad for group %lu\n", |
Andreas Dilger | 717d50e | 2007-10-16 18:38:25 -0400 | [diff] [blame] | 79 | block_group); |
| 80 | gdp->bg_free_blocks_count = 0; |
| 81 | gdp->bg_free_inodes_count = 0; |
| 82 | gdp->bg_itable_unused = 0; |
| 83 | memset(bh->b_data, 0xff, sb->s_blocksize); |
| 84 | return 0; |
| 85 | } |
| 86 | |
| 87 | memset(bh->b_data, 0, (EXT4_INODES_PER_GROUP(sb) + 7) / 8); |
| 88 | mark_bitmap_end(EXT4_INODES_PER_GROUP(sb), EXT4_BLOCKS_PER_GROUP(sb), |
| 89 | bh->b_data); |
| 90 | |
| 91 | return EXT4_INODES_PER_GROUP(sb); |
| 92 | } |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 93 | |
| 94 | /* |
| 95 | * Read the inode allocation bitmap for a given block_group, reading |
| 96 | * into the specified slot in the superblock's bitmap cache. |
| 97 | * |
| 98 | * Return buffer_head of bitmap on success or NULL. |
| 99 | */ |
| 100 | static struct buffer_head * |
Avantika Mathur | fd2d429 | 2008-01-28 23:58:27 -0500 | [diff] [blame] | 101 | read_inode_bitmap(struct super_block *sb, ext4_group_t block_group) |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 102 | { |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 103 | struct ext4_group_desc *desc; |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 104 | struct buffer_head *bh = NULL; |
| 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) |
| 108 | goto error_out; |
Andreas Dilger | 717d50e | 2007-10-16 18:38:25 -0400 | [diff] [blame] | 109 | if (desc->bg_flags & cpu_to_le16(EXT4_BG_INODE_UNINIT)) { |
| 110 | bh = sb_getblk(sb, ext4_inode_bitmap(sb, desc)); |
| 111 | if (!buffer_uptodate(bh)) { |
| 112 | lock_buffer(bh); |
| 113 | if (!buffer_uptodate(bh)) { |
| 114 | ext4_init_inode_bitmap(sb, bh, block_group, |
| 115 | desc); |
| 116 | set_buffer_uptodate(bh); |
| 117 | } |
| 118 | unlock_buffer(bh); |
| 119 | } |
| 120 | } else { |
| 121 | bh = sb_bread(sb, ext4_inode_bitmap(sb, desc)); |
| 122 | } |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 123 | if (!bh) |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 124 | ext4_error(sb, "read_inode_bitmap", |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 125 | "Cannot read inode bitmap - " |
Laurent Vivier | bd81d8e | 2006-10-11 01:21:10 -0700 | [diff] [blame] | 126 | "block_group = %lu, inode_bitmap = %llu", |
Alexandre Ratchov | 8fadc14 | 2006-10-11 01:21:15 -0700 | [diff] [blame] | 127 | block_group, ext4_inode_bitmap(sb, desc)); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 128 | error_out: |
| 129 | return bh; |
| 130 | } |
| 131 | |
| 132 | /* |
| 133 | * NOTE! When we get the inode, we're the only people |
| 134 | * that have access to it, and as such there are no |
| 135 | * race conditions we have to worry about. The inode |
| 136 | * is not on the hash-lists, and it cannot be reached |
| 137 | * through the filesystem because the directory entry |
| 138 | * has been deleted earlier. |
| 139 | * |
| 140 | * HOWEVER: we must make sure that we get no aliases, |
| 141 | * which means that we have to call "clear_inode()" |
| 142 | * _before_ we mark the inode not in use in the inode |
| 143 | * bitmaps. Otherwise a newly created file might use |
| 144 | * the same inode number (not actually the same pointer |
| 145 | * though), and then we'd have two inodes sharing the |
| 146 | * same inode number and space on the harddisk. |
| 147 | */ |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 148 | void ext4_free_inode (handle_t *handle, struct inode * inode) |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 149 | { |
| 150 | struct super_block * sb = inode->i_sb; |
| 151 | int is_directory; |
| 152 | unsigned long ino; |
| 153 | struct buffer_head *bitmap_bh = NULL; |
| 154 | struct buffer_head *bh2; |
Avantika Mathur | fd2d429 | 2008-01-28 23:58:27 -0500 | [diff] [blame] | 155 | ext4_group_t block_group; |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 156 | unsigned long bit; |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 157 | struct ext4_group_desc * gdp; |
| 158 | struct ext4_super_block * es; |
| 159 | struct ext4_sb_info *sbi; |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 160 | int fatal = 0, err; |
| 161 | |
| 162 | if (atomic_read(&inode->i_count) > 1) { |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 163 | printk ("ext4_free_inode: inode has count=%d\n", |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 164 | atomic_read(&inode->i_count)); |
| 165 | return; |
| 166 | } |
| 167 | if (inode->i_nlink) { |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 168 | printk ("ext4_free_inode: inode has nlink=%d\n", |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 169 | inode->i_nlink); |
| 170 | return; |
| 171 | } |
| 172 | if (!sb) { |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 173 | printk("ext4_free_inode: inode on nonexistent device\n"); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 174 | return; |
| 175 | } |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 176 | sbi = EXT4_SB(sb); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 177 | |
| 178 | ino = inode->i_ino; |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 179 | ext4_debug ("freeing inode %lu\n", ino); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 180 | |
| 181 | /* |
| 182 | * Note: we must free any quota before locking the superblock, |
| 183 | * as writing the quota to disk may need the lock as well. |
| 184 | */ |
| 185 | DQUOT_INIT(inode); |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 186 | ext4_xattr_delete_inode(handle, inode); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 187 | DQUOT_FREE_INODE(inode); |
| 188 | DQUOT_DROP(inode); |
| 189 | |
| 190 | is_directory = S_ISDIR(inode->i_mode); |
| 191 | |
| 192 | /* Do this BEFORE marking the inode not in use or returning an error */ |
| 193 | clear_inode (inode); |
| 194 | |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 195 | es = EXT4_SB(sb)->s_es; |
| 196 | if (ino < EXT4_FIRST_INO(sb) || ino > le32_to_cpu(es->s_inodes_count)) { |
| 197 | ext4_error (sb, "ext4_free_inode", |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 198 | "reserved or nonexistent inode %lu", ino); |
| 199 | goto error_return; |
| 200 | } |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 201 | block_group = (ino - 1) / EXT4_INODES_PER_GROUP(sb); |
| 202 | bit = (ino - 1) % EXT4_INODES_PER_GROUP(sb); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 203 | bitmap_bh = read_inode_bitmap(sb, block_group); |
| 204 | if (!bitmap_bh) |
| 205 | goto error_return; |
| 206 | |
| 207 | BUFFER_TRACE(bitmap_bh, "get_write_access"); |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 208 | fatal = ext4_journal_get_write_access(handle, bitmap_bh); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 209 | if (fatal) |
| 210 | goto error_return; |
| 211 | |
| 212 | /* Ok, now we can actually update the inode bitmaps.. */ |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 213 | if (!ext4_clear_bit_atomic(sb_bgl_lock(sbi, block_group), |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 214 | bit, bitmap_bh->b_data)) |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 215 | ext4_error (sb, "ext4_free_inode", |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 216 | "bit already cleared for inode %lu", ino); |
| 217 | else { |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 218 | gdp = ext4_get_group_desc (sb, block_group, &bh2); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 219 | |
| 220 | BUFFER_TRACE(bh2, "get_write_access"); |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 221 | fatal = ext4_journal_get_write_access(handle, bh2); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 222 | if (fatal) goto error_return; |
| 223 | |
| 224 | if (gdp) { |
| 225 | spin_lock(sb_bgl_lock(sbi, block_group)); |
Marcin Slusarz | e8546d0 | 2008-04-17 10:38:59 -0400 | [diff] [blame] | 226 | le16_add_cpu(&gdp->bg_free_inodes_count, 1); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 227 | if (is_directory) |
Marcin Slusarz | e8546d0 | 2008-04-17 10:38:59 -0400 | [diff] [blame] | 228 | le16_add_cpu(&gdp->bg_used_dirs_count, -1); |
Andreas Dilger | 717d50e | 2007-10-16 18:38:25 -0400 | [diff] [blame] | 229 | gdp->bg_checksum = ext4_group_desc_csum(sbi, |
| 230 | block_group, gdp); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 231 | spin_unlock(sb_bgl_lock(sbi, block_group)); |
| 232 | percpu_counter_inc(&sbi->s_freeinodes_counter); |
| 233 | if (is_directory) |
| 234 | percpu_counter_dec(&sbi->s_dirs_counter); |
| 235 | |
| 236 | } |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 237 | BUFFER_TRACE(bh2, "call ext4_journal_dirty_metadata"); |
| 238 | err = ext4_journal_dirty_metadata(handle, bh2); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 239 | if (!fatal) fatal = err; |
| 240 | } |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 241 | BUFFER_TRACE(bitmap_bh, "call ext4_journal_dirty_metadata"); |
| 242 | err = ext4_journal_dirty_metadata(handle, bitmap_bh); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 243 | if (!fatal) |
| 244 | fatal = err; |
| 245 | sb->s_dirt = 1; |
| 246 | error_return: |
| 247 | brelse(bitmap_bh); |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 248 | ext4_std_error(sb, fatal); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 249 | } |
| 250 | |
| 251 | /* |
| 252 | * There are two policies for allocating an inode. If the new inode is |
| 253 | * a directory, then a forward search is made for a block group with both |
| 254 | * free space and a low directory-to-inode ratio; if that fails, then of |
| 255 | * the groups with above-average free space, that group with the fewest |
| 256 | * directories already is chosen. |
| 257 | * |
| 258 | * For other inodes, search forward from the parent directory\'s block |
| 259 | * group to find a free inode. |
| 260 | */ |
Avantika Mathur | 2aa9fc4 | 2008-01-28 23:58:27 -0500 | [diff] [blame] | 261 | static int find_group_dir(struct super_block *sb, struct inode *parent, |
| 262 | ext4_group_t *best_group) |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 263 | { |
Avantika Mathur | fd2d429 | 2008-01-28 23:58:27 -0500 | [diff] [blame] | 264 | ext4_group_t ngroups = EXT4_SB(sb)->s_groups_count; |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 265 | unsigned int freei, avefreei; |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 266 | struct ext4_group_desc *desc, *best_desc = NULL; |
Avantika Mathur | 2aa9fc4 | 2008-01-28 23:58:27 -0500 | [diff] [blame] | 267 | ext4_group_t group; |
| 268 | int ret = -1; |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 269 | |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 270 | freei = percpu_counter_read_positive(&EXT4_SB(sb)->s_freeinodes_counter); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 271 | avefreei = freei / ngroups; |
| 272 | |
| 273 | for (group = 0; group < ngroups; group++) { |
Eric Sandeen | ef2fb67 | 2007-10-16 23:26:30 -0700 | [diff] [blame] | 274 | desc = ext4_get_group_desc (sb, group, NULL); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 275 | if (!desc || !desc->bg_free_inodes_count) |
| 276 | continue; |
| 277 | if (le16_to_cpu(desc->bg_free_inodes_count) < avefreei) |
| 278 | continue; |
| 279 | if (!best_desc || |
| 280 | (le16_to_cpu(desc->bg_free_blocks_count) > |
| 281 | le16_to_cpu(best_desc->bg_free_blocks_count))) { |
Avantika Mathur | 2aa9fc4 | 2008-01-28 23:58:27 -0500 | [diff] [blame] | 282 | *best_group = group; |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 283 | best_desc = desc; |
Avantika Mathur | 2aa9fc4 | 2008-01-28 23:58:27 -0500 | [diff] [blame] | 284 | ret = 0; |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 285 | } |
| 286 | } |
Avantika Mathur | 2aa9fc4 | 2008-01-28 23:58:27 -0500 | [diff] [blame] | 287 | return ret; |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 288 | } |
| 289 | |
| 290 | /* |
| 291 | * Orlov's allocator for directories. |
| 292 | * |
| 293 | * We always try to spread first-level directories. |
| 294 | * |
| 295 | * If there are blockgroups with both free inodes and free blocks counts |
| 296 | * not worse than average we return one with smallest directory count. |
| 297 | * Otherwise we simply return a random group. |
| 298 | * |
| 299 | * For the rest rules look so: |
| 300 | * |
| 301 | * It's OK to put directory into a group unless |
| 302 | * it has too many directories already (max_dirs) or |
| 303 | * it has too few free inodes left (min_inodes) or |
| 304 | * it has too few free blocks left (min_blocks) or |
| 305 | * it's already running too large debt (max_debt). |
Benoit Boissinot | 1cc8dcf | 2008-04-21 22:45:55 +0000 | [diff] [blame] | 306 | * Parent's group is preferred, if it doesn't satisfy these |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 307 | * conditions we search cyclically through the rest. If none |
| 308 | * of the groups look good we just look for a group with more |
| 309 | * free inodes than average (starting at parent's group). |
| 310 | * |
| 311 | * Debt is incremented each time we allocate a directory and decremented |
| 312 | * when we allocate an inode, within 0--255. |
| 313 | */ |
| 314 | |
| 315 | #define INODE_COST 64 |
| 316 | #define BLOCK_COST 256 |
| 317 | |
Avantika Mathur | 2aa9fc4 | 2008-01-28 23:58:27 -0500 | [diff] [blame] | 318 | static int find_group_orlov(struct super_block *sb, struct inode *parent, |
| 319 | ext4_group_t *group) |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 320 | { |
Avantika Mathur | fd2d429 | 2008-01-28 23:58:27 -0500 | [diff] [blame] | 321 | ext4_group_t parent_group = EXT4_I(parent)->i_block_group; |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 322 | struct ext4_sb_info *sbi = EXT4_SB(sb); |
| 323 | struct ext4_super_block *es = sbi->s_es; |
Avantika Mathur | fd2d429 | 2008-01-28 23:58:27 -0500 | [diff] [blame] | 324 | ext4_group_t ngroups = sbi->s_groups_count; |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 325 | int inodes_per_group = EXT4_INODES_PER_GROUP(sb); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 326 | unsigned int freei, avefreei; |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 327 | ext4_fsblk_t freeb, avefreeb; |
| 328 | ext4_fsblk_t blocks_per_dir; |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 329 | unsigned int ndirs; |
| 330 | int max_debt, max_dirs, min_inodes; |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 331 | ext4_grpblk_t min_blocks; |
Avantika Mathur | 2aa9fc4 | 2008-01-28 23:58:27 -0500 | [diff] [blame] | 332 | ext4_group_t i; |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 333 | struct ext4_group_desc *desc; |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 334 | |
| 335 | freei = percpu_counter_read_positive(&sbi->s_freeinodes_counter); |
| 336 | avefreei = freei / ngroups; |
| 337 | freeb = percpu_counter_read_positive(&sbi->s_freeblocks_counter); |
Mingming Cao | 3a5b2ec | 2006-10-11 01:21:05 -0700 | [diff] [blame] | 338 | avefreeb = freeb; |
Andrew Morton | f4e5bc2 | 2006-10-11 01:21:19 -0700 | [diff] [blame] | 339 | do_div(avefreeb, ngroups); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 340 | ndirs = percpu_counter_read_positive(&sbi->s_dirs_counter); |
| 341 | |
| 342 | if ((parent == sb->s_root->d_inode) || |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 343 | (EXT4_I(parent)->i_flags & EXT4_TOPDIR_FL)) { |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 344 | int best_ndir = inodes_per_group; |
Avantika Mathur | 2aa9fc4 | 2008-01-28 23:58:27 -0500 | [diff] [blame] | 345 | ext4_group_t grp; |
| 346 | int ret = -1; |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 347 | |
Avantika Mathur | 2aa9fc4 | 2008-01-28 23:58:27 -0500 | [diff] [blame] | 348 | get_random_bytes(&grp, sizeof(grp)); |
| 349 | parent_group = (unsigned)grp % ngroups; |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 350 | for (i = 0; i < ngroups; i++) { |
Avantika Mathur | 2aa9fc4 | 2008-01-28 23:58:27 -0500 | [diff] [blame] | 351 | grp = (parent_group + i) % ngroups; |
| 352 | desc = ext4_get_group_desc(sb, grp, NULL); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 353 | if (!desc || !desc->bg_free_inodes_count) |
| 354 | continue; |
| 355 | if (le16_to_cpu(desc->bg_used_dirs_count) >= best_ndir) |
| 356 | continue; |
| 357 | if (le16_to_cpu(desc->bg_free_inodes_count) < avefreei) |
| 358 | continue; |
| 359 | if (le16_to_cpu(desc->bg_free_blocks_count) < avefreeb) |
| 360 | continue; |
Avantika Mathur | 2aa9fc4 | 2008-01-28 23:58:27 -0500 | [diff] [blame] | 361 | *group = grp; |
| 362 | ret = 0; |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 363 | best_ndir = le16_to_cpu(desc->bg_used_dirs_count); |
| 364 | } |
Avantika Mathur | 2aa9fc4 | 2008-01-28 23:58:27 -0500 | [diff] [blame] | 365 | if (ret == 0) |
| 366 | return ret; |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 367 | goto fallback; |
| 368 | } |
| 369 | |
Laurent Vivier | bd81d8e | 2006-10-11 01:21:10 -0700 | [diff] [blame] | 370 | blocks_per_dir = ext4_blocks_count(es) - freeb; |
Andrew Morton | f4e5bc2 | 2006-10-11 01:21:19 -0700 | [diff] [blame] | 371 | do_div(blocks_per_dir, ndirs); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 372 | |
| 373 | max_dirs = ndirs / ngroups + inodes_per_group / 16; |
| 374 | min_inodes = avefreei - inodes_per_group / 4; |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 375 | min_blocks = avefreeb - EXT4_BLOCKS_PER_GROUP(sb) / 4; |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 376 | |
Mingming Cao | 3a5b2ec | 2006-10-11 01:21:05 -0700 | [diff] [blame] | 377 | max_debt = EXT4_BLOCKS_PER_GROUP(sb); |
Andrew Morton | f4e5bc2 | 2006-10-11 01:21:19 -0700 | [diff] [blame] | 378 | max_debt /= max_t(int, blocks_per_dir, BLOCK_COST); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 379 | if (max_debt * INODE_COST > inodes_per_group) |
| 380 | max_debt = inodes_per_group / INODE_COST; |
| 381 | if (max_debt > 255) |
| 382 | max_debt = 255; |
| 383 | if (max_debt == 0) |
| 384 | max_debt = 1; |
| 385 | |
| 386 | for (i = 0; i < ngroups; i++) { |
Avantika Mathur | 2aa9fc4 | 2008-01-28 23:58:27 -0500 | [diff] [blame] | 387 | *group = (parent_group + i) % ngroups; |
| 388 | desc = ext4_get_group_desc(sb, *group, NULL); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 389 | if (!desc || !desc->bg_free_inodes_count) |
| 390 | continue; |
| 391 | if (le16_to_cpu(desc->bg_used_dirs_count) >= max_dirs) |
| 392 | continue; |
| 393 | if (le16_to_cpu(desc->bg_free_inodes_count) < min_inodes) |
| 394 | continue; |
| 395 | if (le16_to_cpu(desc->bg_free_blocks_count) < min_blocks) |
| 396 | continue; |
Avantika Mathur | 2aa9fc4 | 2008-01-28 23:58:27 -0500 | [diff] [blame] | 397 | return 0; |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 398 | } |
| 399 | |
| 400 | fallback: |
| 401 | for (i = 0; i < ngroups; i++) { |
Avantika Mathur | 2aa9fc4 | 2008-01-28 23:58:27 -0500 | [diff] [blame] | 402 | *group = (parent_group + i) % ngroups; |
| 403 | desc = ext4_get_group_desc(sb, *group, NULL); |
| 404 | if (desc && desc->bg_free_inodes_count && |
| 405 | le16_to_cpu(desc->bg_free_inodes_count) >= avefreei) |
| 406 | return 0; |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 407 | } |
| 408 | |
| 409 | if (avefreei) { |
| 410 | /* |
| 411 | * The free-inodes counter is approximate, and for really small |
| 412 | * filesystems the above test can fail to find any blockgroups |
| 413 | */ |
| 414 | avefreei = 0; |
| 415 | goto fallback; |
| 416 | } |
| 417 | |
| 418 | return -1; |
| 419 | } |
| 420 | |
Avantika Mathur | 2aa9fc4 | 2008-01-28 23:58:27 -0500 | [diff] [blame] | 421 | static int find_group_other(struct super_block *sb, struct inode *parent, |
| 422 | ext4_group_t *group) |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 423 | { |
Avantika Mathur | fd2d429 | 2008-01-28 23:58:27 -0500 | [diff] [blame] | 424 | ext4_group_t parent_group = EXT4_I(parent)->i_block_group; |
| 425 | ext4_group_t ngroups = EXT4_SB(sb)->s_groups_count; |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 426 | struct ext4_group_desc *desc; |
Avantika Mathur | 2aa9fc4 | 2008-01-28 23:58:27 -0500 | [diff] [blame] | 427 | ext4_group_t i; |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 428 | |
| 429 | /* |
| 430 | * Try to place the inode in its parent directory |
| 431 | */ |
Avantika Mathur | 2aa9fc4 | 2008-01-28 23:58:27 -0500 | [diff] [blame] | 432 | *group = parent_group; |
| 433 | desc = ext4_get_group_desc(sb, *group, NULL); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 434 | if (desc && le16_to_cpu(desc->bg_free_inodes_count) && |
| 435 | le16_to_cpu(desc->bg_free_blocks_count)) |
Avantika Mathur | 2aa9fc4 | 2008-01-28 23:58:27 -0500 | [diff] [blame] | 436 | return 0; |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 437 | |
| 438 | /* |
| 439 | * We're going to place this inode in a different blockgroup from its |
| 440 | * parent. We want to cause files in a common directory to all land in |
| 441 | * the same blockgroup. But we want files which are in a different |
| 442 | * directory which shares a blockgroup with our parent to land in a |
| 443 | * different blockgroup. |
| 444 | * |
| 445 | * So add our directory's i_ino into the starting point for the hash. |
| 446 | */ |
Avantika Mathur | 2aa9fc4 | 2008-01-28 23:58:27 -0500 | [diff] [blame] | 447 | *group = (*group + parent->i_ino) % ngroups; |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 448 | |
| 449 | /* |
| 450 | * Use a quadratic hash to find a group with a free inode and some free |
| 451 | * blocks. |
| 452 | */ |
| 453 | for (i = 1; i < ngroups; i <<= 1) { |
Avantika Mathur | 2aa9fc4 | 2008-01-28 23:58:27 -0500 | [diff] [blame] | 454 | *group += i; |
| 455 | if (*group >= ngroups) |
| 456 | *group -= ngroups; |
| 457 | desc = ext4_get_group_desc(sb, *group, NULL); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 458 | if (desc && le16_to_cpu(desc->bg_free_inodes_count) && |
| 459 | le16_to_cpu(desc->bg_free_blocks_count)) |
Avantika Mathur | 2aa9fc4 | 2008-01-28 23:58:27 -0500 | [diff] [blame] | 460 | return 0; |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 461 | } |
| 462 | |
| 463 | /* |
| 464 | * That failed: try linear search for a free inode, even if that group |
| 465 | * has no free blocks. |
| 466 | */ |
Avantika Mathur | 2aa9fc4 | 2008-01-28 23:58:27 -0500 | [diff] [blame] | 467 | *group = parent_group; |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 468 | for (i = 0; i < ngroups; i++) { |
Avantika Mathur | 2aa9fc4 | 2008-01-28 23:58:27 -0500 | [diff] [blame] | 469 | if (++*group >= ngroups) |
| 470 | *group = 0; |
| 471 | desc = ext4_get_group_desc(sb, *group, NULL); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 472 | if (desc && le16_to_cpu(desc->bg_free_inodes_count)) |
Avantika Mathur | 2aa9fc4 | 2008-01-28 23:58:27 -0500 | [diff] [blame] | 473 | return 0; |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 474 | } |
| 475 | |
| 476 | return -1; |
| 477 | } |
| 478 | |
| 479 | /* |
| 480 | * There are two policies for allocating an inode. If the new inode is |
| 481 | * a directory, then a forward search is made for a block group with both |
| 482 | * free space and a low directory-to-inode ratio; if that fails, then of |
| 483 | * the groups with above-average free space, that group with the fewest |
| 484 | * directories already is chosen. |
| 485 | * |
| 486 | * For other inodes, search forward from the parent directory's block |
| 487 | * group to find a free inode. |
| 488 | */ |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 489 | 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] | 490 | { |
| 491 | struct super_block *sb; |
| 492 | struct buffer_head *bitmap_bh = NULL; |
| 493 | struct buffer_head *bh2; |
Avantika Mathur | 2aa9fc4 | 2008-01-28 23:58:27 -0500 | [diff] [blame] | 494 | ext4_group_t group = 0; |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 495 | unsigned long ino = 0; |
| 496 | struct inode * inode; |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 497 | struct ext4_group_desc * gdp = NULL; |
| 498 | struct ext4_super_block * es; |
| 499 | struct ext4_inode_info *ei; |
| 500 | struct ext4_sb_info *sbi; |
Avantika Mathur | 2aa9fc4 | 2008-01-28 23:58:27 -0500 | [diff] [blame] | 501 | int ret2, err = 0; |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 502 | struct inode *ret; |
Avantika Mathur | 2aa9fc4 | 2008-01-28 23:58:27 -0500 | [diff] [blame] | 503 | ext4_group_t i; |
| 504 | int free = 0; |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 505 | |
| 506 | /* Cannot create files in a deleted directory */ |
| 507 | if (!dir || !dir->i_nlink) |
| 508 | return ERR_PTR(-EPERM); |
| 509 | |
| 510 | sb = dir->i_sb; |
| 511 | inode = new_inode(sb); |
| 512 | if (!inode) |
| 513 | return ERR_PTR(-ENOMEM); |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 514 | ei = EXT4_I(inode); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 515 | |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 516 | sbi = EXT4_SB(sb); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 517 | es = sbi->s_es; |
| 518 | if (S_ISDIR(mode)) { |
| 519 | if (test_opt (sb, OLDALLOC)) |
Avantika Mathur | 2aa9fc4 | 2008-01-28 23:58:27 -0500 | [diff] [blame] | 520 | ret2 = find_group_dir(sb, dir, &group); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 521 | else |
Avantika Mathur | 2aa9fc4 | 2008-01-28 23:58:27 -0500 | [diff] [blame] | 522 | ret2 = find_group_orlov(sb, dir, &group); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 523 | } else |
Avantika Mathur | 2aa9fc4 | 2008-01-28 23:58:27 -0500 | [diff] [blame] | 524 | ret2 = find_group_other(sb, dir, &group); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 525 | |
| 526 | err = -ENOSPC; |
Avantika Mathur | 2aa9fc4 | 2008-01-28 23:58:27 -0500 | [diff] [blame] | 527 | if (ret2 == -1) |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 528 | goto out; |
| 529 | |
| 530 | for (i = 0; i < sbi->s_groups_count; i++) { |
| 531 | err = -EIO; |
| 532 | |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 533 | gdp = ext4_get_group_desc(sb, group, &bh2); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 534 | if (!gdp) |
| 535 | goto fail; |
| 536 | |
| 537 | brelse(bitmap_bh); |
| 538 | bitmap_bh = read_inode_bitmap(sb, group); |
| 539 | if (!bitmap_bh) |
| 540 | goto fail; |
| 541 | |
| 542 | ino = 0; |
| 543 | |
| 544 | repeat_in_this_group: |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 545 | ino = ext4_find_next_zero_bit((unsigned long *) |
| 546 | bitmap_bh->b_data, EXT4_INODES_PER_GROUP(sb), ino); |
| 547 | if (ino < EXT4_INODES_PER_GROUP(sb)) { |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 548 | |
| 549 | BUFFER_TRACE(bitmap_bh, "get_write_access"); |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 550 | err = ext4_journal_get_write_access(handle, bitmap_bh); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 551 | if (err) |
| 552 | goto fail; |
| 553 | |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 554 | if (!ext4_set_bit_atomic(sb_bgl_lock(sbi, group), |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 555 | ino, bitmap_bh->b_data)) { |
| 556 | /* we won it */ |
| 557 | BUFFER_TRACE(bitmap_bh, |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 558 | "call ext4_journal_dirty_metadata"); |
| 559 | err = ext4_journal_dirty_metadata(handle, |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 560 | bitmap_bh); |
| 561 | if (err) |
| 562 | goto fail; |
| 563 | goto got; |
| 564 | } |
| 565 | /* we lost it */ |
Mingming Cao | dab291a | 2006-10-11 01:21:01 -0700 | [diff] [blame] | 566 | jbd2_journal_release_buffer(handle, bitmap_bh); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 567 | |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 568 | if (++ino < EXT4_INODES_PER_GROUP(sb)) |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 569 | goto repeat_in_this_group; |
| 570 | } |
| 571 | |
| 572 | /* |
| 573 | * This case is possible in concurrent environment. It is very |
| 574 | * rare. We cannot repeat the find_group_xxx() call because |
| 575 | * that will simply return the same blockgroup, because the |
| 576 | * group descriptor metadata has not yet been updated. |
| 577 | * So we just go onto the next blockgroup. |
| 578 | */ |
| 579 | if (++group == sbi->s_groups_count) |
| 580 | group = 0; |
| 581 | } |
| 582 | err = -ENOSPC; |
| 583 | goto out; |
| 584 | |
| 585 | got: |
Andreas Dilger | 717d50e | 2007-10-16 18:38:25 -0400 | [diff] [blame] | 586 | ino++; |
| 587 | if ((group == 0 && ino < EXT4_FIRST_INO(sb)) || |
| 588 | ino > EXT4_INODES_PER_GROUP(sb)) { |
Harvey Harrison | 46e665e | 2008-04-17 10:38:59 -0400 | [diff] [blame^] | 589 | ext4_error(sb, __func__, |
Andreas Dilger | 717d50e | 2007-10-16 18:38:25 -0400 | [diff] [blame] | 590 | "reserved inode or inode > inodes count - " |
Avantika Mathur | fd2d429 | 2008-01-28 23:58:27 -0500 | [diff] [blame] | 591 | "block_group = %lu, inode=%lu", group, |
Andreas Dilger | 717d50e | 2007-10-16 18:38:25 -0400 | [diff] [blame] | 592 | ino + group * EXT4_INODES_PER_GROUP(sb)); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 593 | err = -EIO; |
| 594 | goto fail; |
| 595 | } |
| 596 | |
| 597 | BUFFER_TRACE(bh2, "get_write_access"); |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 598 | err = ext4_journal_get_write_access(handle, bh2); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 599 | if (err) goto fail; |
Andreas Dilger | 717d50e | 2007-10-16 18:38:25 -0400 | [diff] [blame] | 600 | |
| 601 | /* We may have to initialize the block bitmap if it isn't already */ |
| 602 | if (EXT4_HAS_RO_COMPAT_FEATURE(sb, EXT4_FEATURE_RO_COMPAT_GDT_CSUM) && |
| 603 | gdp->bg_flags & cpu_to_le16(EXT4_BG_BLOCK_UNINIT)) { |
| 604 | struct buffer_head *block_bh = read_block_bitmap(sb, group); |
| 605 | |
| 606 | BUFFER_TRACE(block_bh, "get block bitmap access"); |
| 607 | err = ext4_journal_get_write_access(handle, block_bh); |
| 608 | if (err) { |
| 609 | brelse(block_bh); |
| 610 | goto fail; |
| 611 | } |
| 612 | |
| 613 | free = 0; |
| 614 | spin_lock(sb_bgl_lock(sbi, group)); |
| 615 | /* recheck and clear flag under lock if we still need to */ |
| 616 | if (gdp->bg_flags & cpu_to_le16(EXT4_BG_BLOCK_UNINIT)) { |
| 617 | gdp->bg_flags &= cpu_to_le16(~EXT4_BG_BLOCK_UNINIT); |
| 618 | free = ext4_free_blocks_after_init(sb, group, gdp); |
| 619 | gdp->bg_free_blocks_count = cpu_to_le16(free); |
| 620 | } |
| 621 | spin_unlock(sb_bgl_lock(sbi, group)); |
| 622 | |
| 623 | /* Don't need to dirty bitmap block if we didn't change it */ |
| 624 | if (free) { |
| 625 | BUFFER_TRACE(block_bh, "dirty block bitmap"); |
| 626 | err = ext4_journal_dirty_metadata(handle, block_bh); |
| 627 | } |
| 628 | |
| 629 | brelse(block_bh); |
| 630 | if (err) |
| 631 | goto fail; |
| 632 | } |
| 633 | |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 634 | spin_lock(sb_bgl_lock(sbi, group)); |
Andreas Dilger | 717d50e | 2007-10-16 18:38:25 -0400 | [diff] [blame] | 635 | /* If we didn't allocate from within the initialized part of the inode |
| 636 | * table then we need to initialize up to this inode. */ |
| 637 | if (EXT4_HAS_RO_COMPAT_FEATURE(sb, EXT4_FEATURE_RO_COMPAT_GDT_CSUM)) { |
| 638 | if (gdp->bg_flags & cpu_to_le16(EXT4_BG_INODE_UNINIT)) { |
| 639 | gdp->bg_flags &= cpu_to_le16(~EXT4_BG_INODE_UNINIT); |
| 640 | |
| 641 | /* When marking the block group with |
| 642 | * ~EXT4_BG_INODE_UNINIT we don't want to depend |
| 643 | * on the value of bg_itable_unsed even though |
| 644 | * mke2fs could have initialized the same for us. |
| 645 | * Instead we calculated the value below |
| 646 | */ |
| 647 | |
| 648 | free = 0; |
| 649 | } else { |
| 650 | free = EXT4_INODES_PER_GROUP(sb) - |
| 651 | le16_to_cpu(gdp->bg_itable_unused); |
| 652 | } |
| 653 | |
| 654 | /* |
| 655 | * Check the relative inode number against the last used |
| 656 | * relative inode number in this group. if it is greater |
| 657 | * we need to update the bg_itable_unused count |
| 658 | * |
| 659 | */ |
| 660 | if (ino > free) |
| 661 | gdp->bg_itable_unused = |
| 662 | cpu_to_le16(EXT4_INODES_PER_GROUP(sb) - ino); |
| 663 | } |
| 664 | |
Marcin Slusarz | e8546d0 | 2008-04-17 10:38:59 -0400 | [diff] [blame] | 665 | le16_add_cpu(&gdp->bg_free_inodes_count, -1); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 666 | if (S_ISDIR(mode)) { |
Marcin Slusarz | e8546d0 | 2008-04-17 10:38:59 -0400 | [diff] [blame] | 667 | le16_add_cpu(&gdp->bg_used_dirs_count, 1); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 668 | } |
Andreas Dilger | 717d50e | 2007-10-16 18:38:25 -0400 | [diff] [blame] | 669 | gdp->bg_checksum = ext4_group_desc_csum(sbi, group, gdp); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 670 | spin_unlock(sb_bgl_lock(sbi, group)); |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 671 | BUFFER_TRACE(bh2, "call ext4_journal_dirty_metadata"); |
| 672 | err = ext4_journal_dirty_metadata(handle, bh2); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 673 | if (err) goto fail; |
| 674 | |
| 675 | percpu_counter_dec(&sbi->s_freeinodes_counter); |
| 676 | if (S_ISDIR(mode)) |
| 677 | percpu_counter_inc(&sbi->s_dirs_counter); |
| 678 | sb->s_dirt = 1; |
| 679 | |
| 680 | inode->i_uid = current->fsuid; |
| 681 | if (test_opt (sb, GRPID)) |
| 682 | inode->i_gid = dir->i_gid; |
| 683 | else if (dir->i_mode & S_ISGID) { |
| 684 | inode->i_gid = dir->i_gid; |
| 685 | if (S_ISDIR(mode)) |
| 686 | mode |= S_ISGID; |
| 687 | } else |
| 688 | inode->i_gid = current->fsgid; |
| 689 | inode->i_mode = mode; |
| 690 | |
Andreas Dilger | 717d50e | 2007-10-16 18:38:25 -0400 | [diff] [blame] | 691 | inode->i_ino = ino + group * EXT4_INODES_PER_GROUP(sb); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 692 | /* This is the optimal IO size (for stat), not the fs block size */ |
| 693 | inode->i_blocks = 0; |
Kalpak Shah | ef7f383 | 2007-07-18 09:15:20 -0400 | [diff] [blame] | 694 | inode->i_mtime = inode->i_atime = inode->i_ctime = ei->i_crtime = |
| 695 | ext4_current_time(inode); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 696 | |
| 697 | memset(ei->i_data, 0, sizeof(ei->i_data)); |
| 698 | ei->i_dir_start_lookup = 0; |
| 699 | ei->i_disksize = 0; |
| 700 | |
Aneesh Kumar K.V | 42bf038 | 2008-02-25 16:38:03 -0500 | [diff] [blame] | 701 | /* |
| 702 | * Don't inherit extent flag from directory. We set extent flag on |
| 703 | * newly created directory and file only if -o extent mount option is |
| 704 | * specified |
| 705 | */ |
| 706 | ei->i_flags = EXT4_I(dir)->i_flags & ~(EXT4_INDEX_FL|EXT4_EXTENTS_FL); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 707 | if (S_ISLNK(mode)) |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 708 | ei->i_flags &= ~(EXT4_IMMUTABLE_FL|EXT4_APPEND_FL); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 709 | /* dirsync only applies to directories */ |
| 710 | if (!S_ISDIR(mode)) |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 711 | ei->i_flags &= ~EXT4_DIRSYNC_FL; |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 712 | ei->i_file_acl = 0; |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 713 | ei->i_dtime = 0; |
| 714 | ei->i_block_alloc_info = NULL; |
| 715 | ei->i_block_group = group; |
| 716 | |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 717 | ext4_set_inode_flags(inode); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 718 | if (IS_DIRSYNC(inode)) |
| 719 | handle->h_sync = 1; |
| 720 | insert_inode_hash(inode); |
| 721 | spin_lock(&sbi->s_next_gen_lock); |
| 722 | inode->i_generation = sbi->s_next_generation++; |
| 723 | spin_unlock(&sbi->s_next_gen_lock); |
| 724 | |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 725 | ei->i_state = EXT4_STATE_NEW; |
Kalpak Shah | ef7f383 | 2007-07-18 09:15:20 -0400 | [diff] [blame] | 726 | |
| 727 | ei->i_extra_isize = EXT4_SB(sb)->s_want_extra_isize; |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 728 | |
| 729 | ret = inode; |
| 730 | if(DQUOT_ALLOC_INODE(inode)) { |
| 731 | err = -EDQUOT; |
| 732 | goto fail_drop; |
| 733 | } |
| 734 | |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 735 | err = ext4_init_acl(handle, inode, dir); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 736 | if (err) |
| 737 | goto fail_free_drop; |
| 738 | |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 739 | err = ext4_init_security(handle,inode, dir); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 740 | if (err) |
| 741 | goto fail_free_drop; |
| 742 | |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 743 | err = ext4_mark_inode_dirty(handle, inode); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 744 | if (err) { |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 745 | ext4_std_error(sb, err); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 746 | goto fail_free_drop; |
| 747 | } |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 748 | if (test_opt(sb, EXTENTS)) { |
Aneesh Kumar K.V | e65187e | 2008-04-29 08:11:12 -0400 | [diff] [blame] | 749 | /* set extent flag only for diretory, file and normal symlink*/ |
| 750 | if (S_ISDIR(mode) || S_ISREG(mode) || S_ISLNK(mode)) { |
Aneesh Kumar K.V | 42bf038 | 2008-02-25 16:38:03 -0500 | [diff] [blame] | 751 | EXT4_I(inode)->i_flags |= EXT4_EXTENTS_FL; |
| 752 | ext4_ext_tree_init(handle, inode); |
| 753 | err = ext4_update_incompat_feature(handle, sb, |
| 754 | EXT4_FEATURE_INCOMPAT_EXTENTS); |
| 755 | if (err) |
| 756 | goto fail; |
| 757 | } |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 758 | } |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 759 | |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 760 | ext4_debug("allocating inode %lu\n", inode->i_ino); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 761 | goto really_out; |
| 762 | fail: |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 763 | ext4_std_error(sb, err); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 764 | out: |
| 765 | iput(inode); |
| 766 | ret = ERR_PTR(err); |
| 767 | really_out: |
| 768 | brelse(bitmap_bh); |
| 769 | return ret; |
| 770 | |
| 771 | fail_free_drop: |
| 772 | DQUOT_FREE_INODE(inode); |
| 773 | |
| 774 | fail_drop: |
| 775 | DQUOT_DROP(inode); |
| 776 | inode->i_flags |= S_NOQUOTA; |
| 777 | inode->i_nlink = 0; |
| 778 | iput(inode); |
| 779 | brelse(bitmap_bh); |
| 780 | return ERR_PTR(err); |
| 781 | } |
| 782 | |
| 783 | /* Verify that we are loading a valid orphan from disk */ |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 784 | struct inode *ext4_orphan_get(struct super_block *sb, unsigned long ino) |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 785 | { |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 786 | 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] | 787 | ext4_group_t block_group; |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 788 | int bit; |
David Howells | 1d1fe1e | 2008-02-07 00:15:37 -0800 | [diff] [blame] | 789 | struct buffer_head *bitmap_bh; |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 790 | struct inode *inode = NULL; |
David Howells | 1d1fe1e | 2008-02-07 00:15:37 -0800 | [diff] [blame] | 791 | long err = -EIO; |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 792 | |
| 793 | /* Error cases - e2fsck has already cleaned up for us */ |
| 794 | if (ino > max_ino) { |
Harvey Harrison | 46e665e | 2008-04-17 10:38:59 -0400 | [diff] [blame^] | 795 | ext4_warning(sb, __func__, |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 796 | "bad orphan ino %lu! e2fsck was run?", ino); |
David Howells | 1d1fe1e | 2008-02-07 00:15:37 -0800 | [diff] [blame] | 797 | goto error; |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 798 | } |
| 799 | |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 800 | block_group = (ino - 1) / EXT4_INODES_PER_GROUP(sb); |
| 801 | bit = (ino - 1) % EXT4_INODES_PER_GROUP(sb); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 802 | bitmap_bh = read_inode_bitmap(sb, block_group); |
| 803 | if (!bitmap_bh) { |
Harvey Harrison | 46e665e | 2008-04-17 10:38:59 -0400 | [diff] [blame^] | 804 | ext4_warning(sb, __func__, |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 805 | "inode bitmap error for orphan %lu", ino); |
David Howells | 1d1fe1e | 2008-02-07 00:15:37 -0800 | [diff] [blame] | 806 | goto error; |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 807 | } |
| 808 | |
| 809 | /* Having the inode bit set should be a 100% indicator that this |
| 810 | * is a valid orphan (no e2fsck run on fs). Orphans also include |
| 811 | * inodes that were being truncated, so we can't check i_nlink==0. |
| 812 | */ |
David Howells | 1d1fe1e | 2008-02-07 00:15:37 -0800 | [diff] [blame] | 813 | if (!ext4_test_bit(bit, bitmap_bh->b_data)) |
| 814 | goto bad_orphan; |
| 815 | |
| 816 | inode = ext4_iget(sb, ino); |
| 817 | if (IS_ERR(inode)) |
| 818 | goto iget_failed; |
| 819 | |
| 820 | if (NEXT_ORPHAN(inode) > max_ino) |
| 821 | goto bad_orphan; |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 822 | brelse(bitmap_bh); |
| 823 | return inode; |
David Howells | 1d1fe1e | 2008-02-07 00:15:37 -0800 | [diff] [blame] | 824 | |
| 825 | iget_failed: |
| 826 | err = PTR_ERR(inode); |
| 827 | inode = NULL; |
| 828 | bad_orphan: |
Harvey Harrison | 46e665e | 2008-04-17 10:38:59 -0400 | [diff] [blame^] | 829 | ext4_warning(sb, __func__, |
David Howells | 1d1fe1e | 2008-02-07 00:15:37 -0800 | [diff] [blame] | 830 | "bad orphan inode %lu! e2fsck was run?", ino); |
| 831 | printk(KERN_NOTICE "ext4_test_bit(bit=%d, block=%llu) = %d\n", |
| 832 | bit, (unsigned long long)bitmap_bh->b_blocknr, |
| 833 | ext4_test_bit(bit, bitmap_bh->b_data)); |
| 834 | printk(KERN_NOTICE "inode=%p\n", inode); |
| 835 | if (inode) { |
| 836 | printk(KERN_NOTICE "is_bad_inode(inode)=%d\n", |
| 837 | is_bad_inode(inode)); |
| 838 | printk(KERN_NOTICE "NEXT_ORPHAN(inode)=%u\n", |
| 839 | NEXT_ORPHAN(inode)); |
| 840 | printk(KERN_NOTICE "max_ino=%lu\n", max_ino); |
| 841 | /* Avoid freeing blocks if we got a bad deleted inode */ |
| 842 | if (inode->i_nlink == 0) |
| 843 | inode->i_blocks = 0; |
| 844 | iput(inode); |
| 845 | } |
| 846 | brelse(bitmap_bh); |
| 847 | error: |
| 848 | return ERR_PTR(err); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 849 | } |
| 850 | |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 851 | unsigned long ext4_count_free_inodes (struct super_block * sb) |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 852 | { |
| 853 | unsigned long desc_count; |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 854 | struct ext4_group_desc *gdp; |
Avantika Mathur | fd2d429 | 2008-01-28 23:58:27 -0500 | [diff] [blame] | 855 | ext4_group_t i; |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 856 | #ifdef EXT4FS_DEBUG |
| 857 | struct ext4_super_block *es; |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 858 | unsigned long bitmap_count, x; |
| 859 | struct buffer_head *bitmap_bh = NULL; |
| 860 | |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 861 | es = EXT4_SB(sb)->s_es; |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 862 | desc_count = 0; |
| 863 | bitmap_count = 0; |
| 864 | gdp = NULL; |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 865 | for (i = 0; i < EXT4_SB(sb)->s_groups_count; i++) { |
| 866 | gdp = ext4_get_group_desc (sb, i, NULL); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 867 | if (!gdp) |
| 868 | continue; |
| 869 | desc_count += le16_to_cpu(gdp->bg_free_inodes_count); |
| 870 | brelse(bitmap_bh); |
| 871 | bitmap_bh = read_inode_bitmap(sb, i); |
| 872 | if (!bitmap_bh) |
| 873 | continue; |
| 874 | |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 875 | x = ext4_count_free(bitmap_bh, EXT4_INODES_PER_GROUP(sb) / 8); |
Eric Sandeen | c549a95 | 2008-01-28 23:58:27 -0500 | [diff] [blame] | 876 | printk(KERN_DEBUG "group %lu: stored = %d, counted = %lu\n", |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 877 | i, le16_to_cpu(gdp->bg_free_inodes_count), x); |
| 878 | bitmap_count += x; |
| 879 | } |
| 880 | brelse(bitmap_bh); |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 881 | printk("ext4_count_free_inodes: stored = %u, computed = %lu, %lu\n", |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 882 | le32_to_cpu(es->s_free_inodes_count), desc_count, bitmap_count); |
| 883 | return desc_count; |
| 884 | #else |
| 885 | desc_count = 0; |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 886 | for (i = 0; i < EXT4_SB(sb)->s_groups_count; i++) { |
| 887 | gdp = ext4_get_group_desc (sb, i, NULL); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 888 | if (!gdp) |
| 889 | continue; |
| 890 | desc_count += le16_to_cpu(gdp->bg_free_inodes_count); |
| 891 | cond_resched(); |
| 892 | } |
| 893 | return desc_count; |
| 894 | #endif |
| 895 | } |
| 896 | |
| 897 | /* Called at mount-time, super-block is locked */ |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 898 | unsigned long ext4_count_dirs (struct super_block * sb) |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 899 | { |
| 900 | unsigned long count = 0; |
Avantika Mathur | fd2d429 | 2008-01-28 23:58:27 -0500 | [diff] [blame] | 901 | ext4_group_t i; |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 902 | |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 903 | for (i = 0; i < EXT4_SB(sb)->s_groups_count; i++) { |
| 904 | struct ext4_group_desc *gdp = ext4_get_group_desc (sb, i, NULL); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 905 | if (!gdp) |
| 906 | continue; |
| 907 | count += le16_to_cpu(gdp->bg_used_dirs_count); |
| 908 | } |
| 909 | return count; |
| 910 | } |
| 911 | |