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); |
| 87 | mark_bitmap_end(EXT4_INODES_PER_GROUP(sb), EXT4_BLOCKS_PER_GROUP(sb), |
| 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 | } |
Frederic Bohe | c806e68 | 2008-10-10 08:09:18 -0400 | [diff] [blame] | 118 | if (buffer_uptodate(bh) && |
| 119 | !(desc->bg_flags & cpu_to_le16(EXT4_BG_INODE_UNINIT))) |
Eric Sandeen | e29d1cd | 2008-08-02 21:21:02 -0400 | [diff] [blame] | 120 | return bh; |
| 121 | |
Frederic Bohe | c806e68 | 2008-10-10 08:09:18 -0400 | [diff] [blame] | 122 | lock_buffer(bh); |
Eric Sandeen | b5f10ee | 2008-08-02 21:21:08 -0400 | [diff] [blame] | 123 | spin_lock(sb_bgl_lock(EXT4_SB(sb), block_group)); |
Eric Sandeen | e29d1cd | 2008-08-02 21:21:02 -0400 | [diff] [blame] | 124 | if (desc->bg_flags & cpu_to_le16(EXT4_BG_INODE_UNINIT)) { |
| 125 | ext4_init_inode_bitmap(sb, bh, block_group, desc); |
| 126 | set_buffer_uptodate(bh); |
Eric Sandeen | b5f10ee | 2008-08-02 21:21:08 -0400 | [diff] [blame] | 127 | spin_unlock(sb_bgl_lock(EXT4_SB(sb), block_group)); |
Aneesh Kumar K.V | 3300bed | 2009-01-03 22:33:39 -0500 | [diff] [blame^] | 128 | unlock_buffer(bh); |
Eric Sandeen | e29d1cd | 2008-08-02 21:21:02 -0400 | [diff] [blame] | 129 | return bh; |
| 130 | } |
Eric Sandeen | b5f10ee | 2008-08-02 21:21:08 -0400 | [diff] [blame] | 131 | spin_unlock(sb_bgl_lock(EXT4_SB(sb), block_group)); |
Eric Sandeen | e29d1cd | 2008-08-02 21:21:02 -0400 | [diff] [blame] | 132 | if (bh_submit_read(bh) < 0) { |
| 133 | put_bh(bh); |
| 134 | ext4_error(sb, __func__, |
| 135 | "Cannot read inode bitmap - " |
Theodore Ts'o | a9df9a4 | 2009-01-05 22:18:16 -0500 | [diff] [blame] | 136 | "block_group = %u, inode_bitmap = %llu", |
Eric Sandeen | e29d1cd | 2008-08-02 21:21:02 -0400 | [diff] [blame] | 137 | block_group, bitmap_blk); |
| 138 | return NULL; |
| 139 | } |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 140 | return bh; |
| 141 | } |
| 142 | |
| 143 | /* |
| 144 | * NOTE! When we get the inode, we're the only people |
| 145 | * that have access to it, and as such there are no |
| 146 | * race conditions we have to worry about. The inode |
| 147 | * is not on the hash-lists, and it cannot be reached |
| 148 | * through the filesystem because the directory entry |
| 149 | * has been deleted earlier. |
| 150 | * |
| 151 | * HOWEVER: we must make sure that we get no aliases, |
| 152 | * which means that we have to call "clear_inode()" |
| 153 | * _before_ we mark the inode not in use in the inode |
| 154 | * bitmaps. Otherwise a newly created file might use |
| 155 | * the same inode number (not actually the same pointer |
| 156 | * though), and then we'd have two inodes sharing the |
| 157 | * same inode number and space on the harddisk. |
| 158 | */ |
Theodore Ts'o | af5bc92 | 2008-09-08 22:25:24 -0400 | [diff] [blame] | 159 | void ext4_free_inode(handle_t *handle, struct inode *inode) |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 160 | { |
Theodore Ts'o | af5bc92 | 2008-09-08 22:25:24 -0400 | [diff] [blame] | 161 | struct super_block *sb = inode->i_sb; |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 162 | int is_directory; |
| 163 | unsigned long ino; |
| 164 | struct buffer_head *bitmap_bh = NULL; |
| 165 | struct buffer_head *bh2; |
Avantika Mathur | fd2d429 | 2008-01-28 23:58:27 -0500 | [diff] [blame] | 166 | ext4_group_t block_group; |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 167 | unsigned long bit; |
Theodore Ts'o | af5bc92 | 2008-09-08 22:25:24 -0400 | [diff] [blame] | 168 | struct ext4_group_desc *gdp; |
| 169 | struct ext4_super_block *es; |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 170 | struct ext4_sb_info *sbi; |
Aneesh Kumar K.V | 560671a | 2009-01-05 22:20:24 -0500 | [diff] [blame] | 171 | int fatal = 0, err, count; |
Jose R. Santos | 772cb7c | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 172 | ext4_group_t flex_group; |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 173 | |
| 174 | if (atomic_read(&inode->i_count) > 1) { |
Theodore Ts'o | 4776004f | 2008-09-08 23:00:52 -0400 | [diff] [blame] | 175 | printk(KERN_ERR "ext4_free_inode: inode has count=%d\n", |
| 176 | atomic_read(&inode->i_count)); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 177 | return; |
| 178 | } |
| 179 | if (inode->i_nlink) { |
Theodore Ts'o | 4776004f | 2008-09-08 23:00:52 -0400 | [diff] [blame] | 180 | printk(KERN_ERR "ext4_free_inode: inode has nlink=%d\n", |
| 181 | inode->i_nlink); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 182 | return; |
| 183 | } |
| 184 | if (!sb) { |
Theodore Ts'o | 4776004f | 2008-09-08 23:00:52 -0400 | [diff] [blame] | 185 | printk(KERN_ERR "ext4_free_inode: inode on " |
| 186 | "nonexistent device\n"); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 187 | return; |
| 188 | } |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 189 | sbi = EXT4_SB(sb); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 190 | |
| 191 | ino = inode->i_ino; |
Theodore Ts'o | af5bc92 | 2008-09-08 22:25:24 -0400 | [diff] [blame] | 192 | ext4_debug("freeing inode %lu\n", ino); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 193 | |
| 194 | /* |
| 195 | * Note: we must free any quota before locking the superblock, |
| 196 | * as writing the quota to disk may need the lock as well. |
| 197 | */ |
| 198 | DQUOT_INIT(inode); |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 199 | ext4_xattr_delete_inode(handle, inode); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 200 | DQUOT_FREE_INODE(inode); |
| 201 | DQUOT_DROP(inode); |
| 202 | |
| 203 | is_directory = S_ISDIR(inode->i_mode); |
| 204 | |
| 205 | /* 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] | 206 | clear_inode(inode); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 207 | |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 208 | es = EXT4_SB(sb)->s_es; |
| 209 | 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] | 210 | ext4_error(sb, "ext4_free_inode", |
| 211 | "reserved or nonexistent inode %lu", ino); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 212 | goto error_return; |
| 213 | } |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 214 | block_group = (ino - 1) / EXT4_INODES_PER_GROUP(sb); |
| 215 | bit = (ino - 1) % EXT4_INODES_PER_GROUP(sb); |
Eric Sandeen | e29d1cd | 2008-08-02 21:21:02 -0400 | [diff] [blame] | 216 | bitmap_bh = ext4_read_inode_bitmap(sb, block_group); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 217 | if (!bitmap_bh) |
| 218 | goto error_return; |
| 219 | |
| 220 | BUFFER_TRACE(bitmap_bh, "get_write_access"); |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 221 | fatal = ext4_journal_get_write_access(handle, bitmap_bh); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 222 | if (fatal) |
| 223 | goto error_return; |
| 224 | |
| 225 | /* Ok, now we can actually update the inode bitmaps.. */ |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 226 | if (!ext4_clear_bit_atomic(sb_bgl_lock(sbi, block_group), |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 227 | bit, bitmap_bh->b_data)) |
Theodore Ts'o | af5bc92 | 2008-09-08 22:25:24 -0400 | [diff] [blame] | 228 | ext4_error(sb, "ext4_free_inode", |
| 229 | "bit already cleared for inode %lu", ino); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 230 | else { |
Theodore Ts'o | af5bc92 | 2008-09-08 22:25:24 -0400 | [diff] [blame] | 231 | gdp = ext4_get_group_desc(sb, block_group, &bh2); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 232 | |
| 233 | BUFFER_TRACE(bh2, "get_write_access"); |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 234 | fatal = ext4_journal_get_write_access(handle, bh2); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 235 | if (fatal) goto error_return; |
| 236 | |
| 237 | if (gdp) { |
| 238 | spin_lock(sb_bgl_lock(sbi, block_group)); |
Aneesh Kumar K.V | 560671a | 2009-01-05 22:20:24 -0500 | [diff] [blame] | 239 | count = ext4_free_inodes_count(sb, gdp) + 1; |
| 240 | ext4_free_inodes_set(sb, gdp, count); |
| 241 | if (is_directory) { |
| 242 | count = ext4_used_dirs_count(sb, gdp) - 1; |
| 243 | ext4_used_dirs_set(sb, gdp, count); |
| 244 | } |
Andreas Dilger | 717d50e | 2007-10-16 18:38:25 -0400 | [diff] [blame] | 245 | gdp->bg_checksum = ext4_group_desc_csum(sbi, |
| 246 | block_group, gdp); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 247 | spin_unlock(sb_bgl_lock(sbi, block_group)); |
| 248 | percpu_counter_inc(&sbi->s_freeinodes_counter); |
| 249 | if (is_directory) |
| 250 | percpu_counter_dec(&sbi->s_dirs_counter); |
| 251 | |
Jose R. Santos | 772cb7c | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 252 | if (sbi->s_log_groups_per_flex) { |
| 253 | flex_group = ext4_flex_group(sbi, block_group); |
| 254 | spin_lock(sb_bgl_lock(sbi, flex_group)); |
| 255 | sbi->s_flex_groups[flex_group].free_inodes++; |
| 256 | spin_unlock(sb_bgl_lock(sbi, flex_group)); |
| 257 | } |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 258 | } |
Frank Mayhar | 0390131 | 2009-01-07 00:06:22 -0500 | [diff] [blame] | 259 | BUFFER_TRACE(bh2, "call ext4_handle_dirty_metadata"); |
| 260 | err = ext4_handle_dirty_metadata(handle, NULL, bh2); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 261 | if (!fatal) fatal = err; |
| 262 | } |
Frank Mayhar | 0390131 | 2009-01-07 00:06:22 -0500 | [diff] [blame] | 263 | BUFFER_TRACE(bitmap_bh, "call ext4_handle_dirty_metadata"); |
| 264 | err = ext4_handle_dirty_metadata(handle, NULL, bitmap_bh); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 265 | if (!fatal) |
| 266 | fatal = err; |
| 267 | sb->s_dirt = 1; |
| 268 | error_return: |
| 269 | brelse(bitmap_bh); |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 270 | ext4_std_error(sb, fatal); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 271 | } |
| 272 | |
| 273 | /* |
| 274 | * There are two policies for allocating an inode. If the new inode is |
| 275 | * a directory, then a forward search is made for a block group with both |
| 276 | * free space and a low directory-to-inode ratio; if that fails, then of |
| 277 | * the groups with above-average free space, that group with the fewest |
| 278 | * directories already is chosen. |
| 279 | * |
| 280 | * For other inodes, search forward from the parent directory\'s block |
| 281 | * group to find a free inode. |
| 282 | */ |
Avantika Mathur | 2aa9fc4 | 2008-01-28 23:58:27 -0500 | [diff] [blame] | 283 | static int find_group_dir(struct super_block *sb, struct inode *parent, |
| 284 | ext4_group_t *best_group) |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 285 | { |
Avantika Mathur | fd2d429 | 2008-01-28 23:58:27 -0500 | [diff] [blame] | 286 | ext4_group_t ngroups = EXT4_SB(sb)->s_groups_count; |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 287 | unsigned int freei, avefreei; |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 288 | struct ext4_group_desc *desc, *best_desc = NULL; |
Avantika Mathur | 2aa9fc4 | 2008-01-28 23:58:27 -0500 | [diff] [blame] | 289 | ext4_group_t group; |
| 290 | int ret = -1; |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 291 | |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 292 | freei = percpu_counter_read_positive(&EXT4_SB(sb)->s_freeinodes_counter); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 293 | avefreei = freei / ngroups; |
| 294 | |
| 295 | for (group = 0; group < ngroups; group++) { |
Theodore Ts'o | af5bc92 | 2008-09-08 22:25:24 -0400 | [diff] [blame] | 296 | desc = ext4_get_group_desc(sb, group, NULL); |
Aneesh Kumar K.V | 560671a | 2009-01-05 22:20:24 -0500 | [diff] [blame] | 297 | if (!desc || !ext4_free_inodes_count(sb, desc)) |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 298 | continue; |
Aneesh Kumar K.V | 560671a | 2009-01-05 22:20:24 -0500 | [diff] [blame] | 299 | if (ext4_free_inodes_count(sb, desc) < avefreei) |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 300 | continue; |
| 301 | if (!best_desc || |
Aneesh Kumar K.V | 560671a | 2009-01-05 22:20:24 -0500 | [diff] [blame] | 302 | (ext4_free_blks_count(sb, desc) > |
| 303 | ext4_free_blks_count(sb, best_desc))) { |
Avantika Mathur | 2aa9fc4 | 2008-01-28 23:58:27 -0500 | [diff] [blame] | 304 | *best_group = group; |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 305 | best_desc = desc; |
Avantika Mathur | 2aa9fc4 | 2008-01-28 23:58:27 -0500 | [diff] [blame] | 306 | ret = 0; |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 307 | } |
| 308 | } |
Avantika Mathur | 2aa9fc4 | 2008-01-28 23:58:27 -0500 | [diff] [blame] | 309 | return ret; |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 310 | } |
| 311 | |
Jose R. Santos | 772cb7c | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 312 | #define free_block_ratio 10 |
| 313 | |
| 314 | static int find_group_flex(struct super_block *sb, struct inode *parent, |
| 315 | ext4_group_t *best_group) |
| 316 | { |
| 317 | struct ext4_sb_info *sbi = EXT4_SB(sb); |
| 318 | struct ext4_group_desc *desc; |
| 319 | struct buffer_head *bh; |
| 320 | struct flex_groups *flex_group = sbi->s_flex_groups; |
| 321 | ext4_group_t parent_group = EXT4_I(parent)->i_block_group; |
| 322 | ext4_group_t parent_fbg_group = ext4_flex_group(sbi, parent_group); |
| 323 | ext4_group_t ngroups = sbi->s_groups_count; |
| 324 | int flex_size = ext4_flex_bg_size(sbi); |
| 325 | ext4_group_t best_flex = parent_fbg_group; |
| 326 | int blocks_per_flex = sbi->s_blocks_per_group * flex_size; |
| 327 | int flexbg_free_blocks; |
| 328 | int flex_freeb_ratio; |
| 329 | ext4_group_t n_fbg_groups; |
| 330 | ext4_group_t i; |
| 331 | |
| 332 | n_fbg_groups = (sbi->s_groups_count + flex_size - 1) >> |
| 333 | sbi->s_log_groups_per_flex; |
| 334 | |
| 335 | find_close_to_parent: |
| 336 | flexbg_free_blocks = flex_group[best_flex].free_blocks; |
| 337 | flex_freeb_ratio = flexbg_free_blocks * 100 / blocks_per_flex; |
| 338 | if (flex_group[best_flex].free_inodes && |
| 339 | flex_freeb_ratio > free_block_ratio) |
| 340 | goto found_flexbg; |
| 341 | |
| 342 | if (best_flex && best_flex == parent_fbg_group) { |
| 343 | best_flex--; |
| 344 | goto find_close_to_parent; |
| 345 | } |
| 346 | |
| 347 | for (i = 0; i < n_fbg_groups; i++) { |
| 348 | if (i == parent_fbg_group || i == parent_fbg_group - 1) |
| 349 | continue; |
| 350 | |
| 351 | flexbg_free_blocks = flex_group[i].free_blocks; |
| 352 | flex_freeb_ratio = flexbg_free_blocks * 100 / blocks_per_flex; |
| 353 | |
| 354 | if (flex_freeb_ratio > free_block_ratio && |
| 355 | flex_group[i].free_inodes) { |
| 356 | best_flex = i; |
| 357 | goto found_flexbg; |
| 358 | } |
| 359 | |
Eric Sandeen | c001077 | 2008-08-19 22:19:50 -0400 | [diff] [blame] | 360 | if (flex_group[best_flex].free_inodes == 0 || |
Jose R. Santos | 772cb7c | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 361 | (flex_group[i].free_blocks > |
| 362 | flex_group[best_flex].free_blocks && |
| 363 | flex_group[i].free_inodes)) |
| 364 | best_flex = i; |
| 365 | } |
| 366 | |
| 367 | if (!flex_group[best_flex].free_inodes || |
| 368 | !flex_group[best_flex].free_blocks) |
| 369 | return -1; |
| 370 | |
| 371 | found_flexbg: |
| 372 | for (i = best_flex * flex_size; i < ngroups && |
| 373 | i < (best_flex + 1) * flex_size; i++) { |
| 374 | desc = ext4_get_group_desc(sb, i, &bh); |
Aneesh Kumar K.V | 560671a | 2009-01-05 22:20:24 -0500 | [diff] [blame] | 375 | if (ext4_free_inodes_count(sb, desc)) { |
Jose R. Santos | 772cb7c | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 376 | *best_group = i; |
| 377 | goto out; |
| 378 | } |
| 379 | } |
| 380 | |
| 381 | return -1; |
| 382 | out: |
| 383 | return 0; |
| 384 | } |
| 385 | |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 386 | /* |
| 387 | * Orlov's allocator for directories. |
| 388 | * |
| 389 | * We always try to spread first-level directories. |
| 390 | * |
| 391 | * If there are blockgroups with both free inodes and free blocks counts |
| 392 | * not worse than average we return one with smallest directory count. |
| 393 | * Otherwise we simply return a random group. |
| 394 | * |
| 395 | * For the rest rules look so: |
| 396 | * |
| 397 | * It's OK to put directory into a group unless |
| 398 | * it has too many directories already (max_dirs) or |
| 399 | * it has too few free inodes left (min_inodes) or |
| 400 | * it has too few free blocks left (min_blocks) or |
| 401 | * it's already running too large debt (max_debt). |
Benoit Boissinot | 1cc8dcf | 2008-04-21 22:45:55 +0000 | [diff] [blame] | 402 | * Parent's group is preferred, if it doesn't satisfy these |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 403 | * conditions we search cyclically through the rest. If none |
| 404 | * of the groups look good we just look for a group with more |
| 405 | * free inodes than average (starting at parent's group). |
| 406 | * |
| 407 | * Debt is incremented each time we allocate a directory and decremented |
| 408 | * when we allocate an inode, within 0--255. |
| 409 | */ |
| 410 | |
| 411 | #define INODE_COST 64 |
| 412 | #define BLOCK_COST 256 |
| 413 | |
Avantika Mathur | 2aa9fc4 | 2008-01-28 23:58:27 -0500 | [diff] [blame] | 414 | static int find_group_orlov(struct super_block *sb, struct inode *parent, |
| 415 | ext4_group_t *group) |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 416 | { |
Avantika Mathur | fd2d429 | 2008-01-28 23:58:27 -0500 | [diff] [blame] | 417 | ext4_group_t parent_group = EXT4_I(parent)->i_block_group; |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 418 | struct ext4_sb_info *sbi = EXT4_SB(sb); |
| 419 | struct ext4_super_block *es = sbi->s_es; |
Avantika Mathur | fd2d429 | 2008-01-28 23:58:27 -0500 | [diff] [blame] | 420 | ext4_group_t ngroups = sbi->s_groups_count; |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 421 | int inodes_per_group = EXT4_INODES_PER_GROUP(sb); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 422 | unsigned int freei, avefreei; |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 423 | ext4_fsblk_t freeb, avefreeb; |
| 424 | ext4_fsblk_t blocks_per_dir; |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 425 | unsigned int ndirs; |
| 426 | int max_debt, max_dirs, min_inodes; |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 427 | ext4_grpblk_t min_blocks; |
Avantika Mathur | 2aa9fc4 | 2008-01-28 23:58:27 -0500 | [diff] [blame] | 428 | ext4_group_t i; |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 429 | struct ext4_group_desc *desc; |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 430 | |
| 431 | freei = percpu_counter_read_positive(&sbi->s_freeinodes_counter); |
| 432 | avefreei = freei / ngroups; |
| 433 | freeb = percpu_counter_read_positive(&sbi->s_freeblocks_counter); |
Mingming Cao | 3a5b2ec | 2006-10-11 01:21:05 -0700 | [diff] [blame] | 434 | avefreeb = freeb; |
Andrew Morton | f4e5bc2 | 2006-10-11 01:21:19 -0700 | [diff] [blame] | 435 | do_div(avefreeb, ngroups); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 436 | ndirs = percpu_counter_read_positive(&sbi->s_dirs_counter); |
| 437 | |
| 438 | if ((parent == sb->s_root->d_inode) || |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 439 | (EXT4_I(parent)->i_flags & EXT4_TOPDIR_FL)) { |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 440 | int best_ndir = inodes_per_group; |
Avantika Mathur | 2aa9fc4 | 2008-01-28 23:58:27 -0500 | [diff] [blame] | 441 | ext4_group_t grp; |
| 442 | int ret = -1; |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 443 | |
Avantika Mathur | 2aa9fc4 | 2008-01-28 23:58:27 -0500 | [diff] [blame] | 444 | get_random_bytes(&grp, sizeof(grp)); |
| 445 | parent_group = (unsigned)grp % ngroups; |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 446 | for (i = 0; i < ngroups; i++) { |
Avantika Mathur | 2aa9fc4 | 2008-01-28 23:58:27 -0500 | [diff] [blame] | 447 | grp = (parent_group + i) % ngroups; |
| 448 | desc = ext4_get_group_desc(sb, grp, NULL); |
Aneesh Kumar K.V | 560671a | 2009-01-05 22:20:24 -0500 | [diff] [blame] | 449 | if (!desc || !ext4_free_inodes_count(sb, desc)) |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 450 | continue; |
Aneesh Kumar K.V | 560671a | 2009-01-05 22:20:24 -0500 | [diff] [blame] | 451 | if (ext4_used_dirs_count(sb, desc) >= best_ndir) |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 452 | continue; |
Aneesh Kumar K.V | 560671a | 2009-01-05 22:20:24 -0500 | [diff] [blame] | 453 | if (ext4_free_inodes_count(sb, desc) < avefreei) |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 454 | continue; |
Aneesh Kumar K.V | 560671a | 2009-01-05 22:20:24 -0500 | [diff] [blame] | 455 | if (ext4_free_blks_count(sb, desc) < avefreeb) |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 456 | continue; |
Avantika Mathur | 2aa9fc4 | 2008-01-28 23:58:27 -0500 | [diff] [blame] | 457 | *group = grp; |
| 458 | ret = 0; |
Aneesh Kumar K.V | 560671a | 2009-01-05 22:20:24 -0500 | [diff] [blame] | 459 | best_ndir = ext4_used_dirs_count(sb, desc); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 460 | } |
Avantika Mathur | 2aa9fc4 | 2008-01-28 23:58:27 -0500 | [diff] [blame] | 461 | if (ret == 0) |
| 462 | return ret; |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 463 | goto fallback; |
| 464 | } |
| 465 | |
Laurent Vivier | bd81d8e | 2006-10-11 01:21:10 -0700 | [diff] [blame] | 466 | blocks_per_dir = ext4_blocks_count(es) - freeb; |
Andrew Morton | f4e5bc2 | 2006-10-11 01:21:19 -0700 | [diff] [blame] | 467 | do_div(blocks_per_dir, ndirs); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 468 | |
| 469 | max_dirs = ndirs / ngroups + inodes_per_group / 16; |
| 470 | min_inodes = avefreei - inodes_per_group / 4; |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 471 | min_blocks = avefreeb - EXT4_BLOCKS_PER_GROUP(sb) / 4; |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 472 | |
Mingming Cao | 3a5b2ec | 2006-10-11 01:21:05 -0700 | [diff] [blame] | 473 | max_debt = EXT4_BLOCKS_PER_GROUP(sb); |
Andrew Morton | f4e5bc2 | 2006-10-11 01:21:19 -0700 | [diff] [blame] | 474 | max_debt /= max_t(int, blocks_per_dir, BLOCK_COST); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 475 | if (max_debt * INODE_COST > inodes_per_group) |
| 476 | max_debt = inodes_per_group / INODE_COST; |
| 477 | if (max_debt > 255) |
| 478 | max_debt = 255; |
| 479 | if (max_debt == 0) |
| 480 | max_debt = 1; |
| 481 | |
| 482 | for (i = 0; i < ngroups; i++) { |
Avantika Mathur | 2aa9fc4 | 2008-01-28 23:58:27 -0500 | [diff] [blame] | 483 | *group = (parent_group + i) % ngroups; |
| 484 | desc = ext4_get_group_desc(sb, *group, NULL); |
Aneesh Kumar K.V | 560671a | 2009-01-05 22:20:24 -0500 | [diff] [blame] | 485 | if (!desc || !ext4_free_inodes_count(sb, desc)) |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 486 | continue; |
Aneesh Kumar K.V | 560671a | 2009-01-05 22:20:24 -0500 | [diff] [blame] | 487 | if (ext4_used_dirs_count(sb, desc) >= max_dirs) |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 488 | continue; |
Aneesh Kumar K.V | 560671a | 2009-01-05 22:20:24 -0500 | [diff] [blame] | 489 | if (ext4_free_inodes_count(sb, desc) < min_inodes) |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 490 | continue; |
Aneesh Kumar K.V | 560671a | 2009-01-05 22:20:24 -0500 | [diff] [blame] | 491 | if (ext4_free_blks_count(sb, desc) < min_blocks) |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 492 | continue; |
Avantika Mathur | 2aa9fc4 | 2008-01-28 23:58:27 -0500 | [diff] [blame] | 493 | return 0; |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 494 | } |
| 495 | |
| 496 | fallback: |
| 497 | for (i = 0; i < ngroups; i++) { |
Avantika Mathur | 2aa9fc4 | 2008-01-28 23:58:27 -0500 | [diff] [blame] | 498 | *group = (parent_group + i) % ngroups; |
| 499 | desc = ext4_get_group_desc(sb, *group, NULL); |
Aneesh Kumar K.V | 560671a | 2009-01-05 22:20:24 -0500 | [diff] [blame] | 500 | if (desc && ext4_free_inodes_count(sb, desc) && |
| 501 | ext4_free_inodes_count(sb, desc) >= avefreei) |
Avantika Mathur | 2aa9fc4 | 2008-01-28 23:58:27 -0500 | [diff] [blame] | 502 | return 0; |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 503 | } |
| 504 | |
| 505 | if (avefreei) { |
| 506 | /* |
| 507 | * The free-inodes counter is approximate, and for really small |
| 508 | * filesystems the above test can fail to find any blockgroups |
| 509 | */ |
| 510 | avefreei = 0; |
| 511 | goto fallback; |
| 512 | } |
| 513 | |
| 514 | return -1; |
| 515 | } |
| 516 | |
Avantika Mathur | 2aa9fc4 | 2008-01-28 23:58:27 -0500 | [diff] [blame] | 517 | static int find_group_other(struct super_block *sb, struct inode *parent, |
| 518 | ext4_group_t *group) |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 519 | { |
Avantika Mathur | fd2d429 | 2008-01-28 23:58:27 -0500 | [diff] [blame] | 520 | ext4_group_t parent_group = EXT4_I(parent)->i_block_group; |
| 521 | ext4_group_t ngroups = EXT4_SB(sb)->s_groups_count; |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 522 | struct ext4_group_desc *desc; |
Avantika Mathur | 2aa9fc4 | 2008-01-28 23:58:27 -0500 | [diff] [blame] | 523 | ext4_group_t i; |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 524 | |
| 525 | /* |
| 526 | * Try to place the inode in its parent directory |
| 527 | */ |
Avantika Mathur | 2aa9fc4 | 2008-01-28 23:58:27 -0500 | [diff] [blame] | 528 | *group = parent_group; |
| 529 | desc = ext4_get_group_desc(sb, *group, NULL); |
Aneesh Kumar K.V | 560671a | 2009-01-05 22:20:24 -0500 | [diff] [blame] | 530 | if (desc && ext4_free_inodes_count(sb, desc) && |
| 531 | ext4_free_blks_count(sb, desc)) |
Avantika Mathur | 2aa9fc4 | 2008-01-28 23:58:27 -0500 | [diff] [blame] | 532 | return 0; |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 533 | |
| 534 | /* |
| 535 | * We're going to place this inode in a different blockgroup from its |
| 536 | * parent. We want to cause files in a common directory to all land in |
| 537 | * the same blockgroup. But we want files which are in a different |
| 538 | * directory which shares a blockgroup with our parent to land in a |
| 539 | * different blockgroup. |
| 540 | * |
| 541 | * So add our directory's i_ino into the starting point for the hash. |
| 542 | */ |
Avantika Mathur | 2aa9fc4 | 2008-01-28 23:58:27 -0500 | [diff] [blame] | 543 | *group = (*group + parent->i_ino) % ngroups; |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 544 | |
| 545 | /* |
| 546 | * Use a quadratic hash to find a group with a free inode and some free |
| 547 | * blocks. |
| 548 | */ |
| 549 | for (i = 1; i < ngroups; i <<= 1) { |
Avantika Mathur | 2aa9fc4 | 2008-01-28 23:58:27 -0500 | [diff] [blame] | 550 | *group += i; |
| 551 | if (*group >= ngroups) |
| 552 | *group -= ngroups; |
| 553 | desc = ext4_get_group_desc(sb, *group, NULL); |
Aneesh Kumar K.V | 560671a | 2009-01-05 22:20:24 -0500 | [diff] [blame] | 554 | if (desc && ext4_free_inodes_count(sb, desc) && |
| 555 | ext4_free_blks_count(sb, desc)) |
Avantika Mathur | 2aa9fc4 | 2008-01-28 23:58:27 -0500 | [diff] [blame] | 556 | return 0; |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 557 | } |
| 558 | |
| 559 | /* |
| 560 | * That failed: try linear search for a free inode, even if that group |
| 561 | * has no free blocks. |
| 562 | */ |
Avantika Mathur | 2aa9fc4 | 2008-01-28 23:58:27 -0500 | [diff] [blame] | 563 | *group = parent_group; |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 564 | for (i = 0; i < ngroups; i++) { |
Avantika Mathur | 2aa9fc4 | 2008-01-28 23:58:27 -0500 | [diff] [blame] | 565 | if (++*group >= ngroups) |
| 566 | *group = 0; |
| 567 | desc = ext4_get_group_desc(sb, *group, NULL); |
Aneesh Kumar K.V | 560671a | 2009-01-05 22:20:24 -0500 | [diff] [blame] | 568 | if (desc && ext4_free_inodes_count(sb, desc)) |
Avantika Mathur | 2aa9fc4 | 2008-01-28 23:58:27 -0500 | [diff] [blame] | 569 | return 0; |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 570 | } |
| 571 | |
| 572 | return -1; |
| 573 | } |
| 574 | |
| 575 | /* |
| 576 | * There are two policies for allocating an inode. If the new inode is |
| 577 | * a directory, then a forward search is made for a block group with both |
| 578 | * free space and a low directory-to-inode ratio; if that fails, then of |
| 579 | * the groups with above-average free space, that group with the fewest |
| 580 | * directories already is chosen. |
| 581 | * |
| 582 | * For other inodes, search forward from the parent directory's block |
| 583 | * group to find a free inode. |
| 584 | */ |
Theodore Ts'o | af5bc92 | 2008-09-08 22:25:24 -0400 | [diff] [blame] | 585 | 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] | 586 | { |
| 587 | struct super_block *sb; |
Aneesh Kumar K.V | 3300bed | 2009-01-03 22:33:39 -0500 | [diff] [blame^] | 588 | struct buffer_head *inode_bitmap_bh = NULL; |
| 589 | struct buffer_head *group_desc_bh; |
Avantika Mathur | 2aa9fc4 | 2008-01-28 23:58:27 -0500 | [diff] [blame] | 590 | ext4_group_t group = 0; |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 591 | unsigned long ino = 0; |
Theodore Ts'o | af5bc92 | 2008-09-08 22:25:24 -0400 | [diff] [blame] | 592 | struct inode *inode; |
| 593 | struct ext4_group_desc *gdp = NULL; |
| 594 | struct ext4_super_block *es; |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 595 | struct ext4_inode_info *ei; |
| 596 | struct ext4_sb_info *sbi; |
Aneesh Kumar K.V | 560671a | 2009-01-05 22:20:24 -0500 | [diff] [blame] | 597 | int ret2, err = 0, count; |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 598 | struct inode *ret; |
Avantika Mathur | 2aa9fc4 | 2008-01-28 23:58:27 -0500 | [diff] [blame] | 599 | ext4_group_t i; |
| 600 | int free = 0; |
Jose R. Santos | 772cb7c | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 601 | ext4_group_t flex_group; |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 602 | |
| 603 | /* Cannot create files in a deleted directory */ |
| 604 | if (!dir || !dir->i_nlink) |
| 605 | return ERR_PTR(-EPERM); |
| 606 | |
| 607 | sb = dir->i_sb; |
| 608 | inode = new_inode(sb); |
| 609 | if (!inode) |
| 610 | return ERR_PTR(-ENOMEM); |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 611 | ei = EXT4_I(inode); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 612 | |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 613 | sbi = EXT4_SB(sb); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 614 | es = sbi->s_es; |
Jose R. Santos | 772cb7c | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 615 | |
| 616 | if (sbi->s_log_groups_per_flex) { |
| 617 | ret2 = find_group_flex(sb, dir, &group); |
| 618 | goto got_group; |
| 619 | } |
| 620 | |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 621 | if (S_ISDIR(mode)) { |
Theodore Ts'o | af5bc92 | 2008-09-08 22:25:24 -0400 | [diff] [blame] | 622 | if (test_opt(sb, OLDALLOC)) |
Avantika Mathur | 2aa9fc4 | 2008-01-28 23:58:27 -0500 | [diff] [blame] | 623 | ret2 = find_group_dir(sb, dir, &group); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 624 | else |
Avantika Mathur | 2aa9fc4 | 2008-01-28 23:58:27 -0500 | [diff] [blame] | 625 | ret2 = find_group_orlov(sb, dir, &group); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 626 | } else |
Avantika Mathur | 2aa9fc4 | 2008-01-28 23:58:27 -0500 | [diff] [blame] | 627 | ret2 = find_group_other(sb, dir, &group); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 628 | |
Jose R. Santos | 772cb7c | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 629 | got_group: |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 630 | err = -ENOSPC; |
Avantika Mathur | 2aa9fc4 | 2008-01-28 23:58:27 -0500 | [diff] [blame] | 631 | if (ret2 == -1) |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 632 | goto out; |
| 633 | |
| 634 | for (i = 0; i < sbi->s_groups_count; i++) { |
| 635 | err = -EIO; |
| 636 | |
Aneesh Kumar K.V | 3300bed | 2009-01-03 22:33:39 -0500 | [diff] [blame^] | 637 | gdp = ext4_get_group_desc(sb, group, &group_desc_bh); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 638 | if (!gdp) |
| 639 | goto fail; |
| 640 | |
Aneesh Kumar K.V | 3300bed | 2009-01-03 22:33:39 -0500 | [diff] [blame^] | 641 | brelse(inode_bitmap_bh); |
| 642 | inode_bitmap_bh = ext4_read_inode_bitmap(sb, group); |
| 643 | if (!inode_bitmap_bh) |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 644 | goto fail; |
| 645 | |
| 646 | ino = 0; |
| 647 | |
| 648 | repeat_in_this_group: |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 649 | ino = ext4_find_next_zero_bit((unsigned long *) |
Aneesh Kumar K.V | 3300bed | 2009-01-03 22:33:39 -0500 | [diff] [blame^] | 650 | inode_bitmap_bh->b_data, |
| 651 | EXT4_INODES_PER_GROUP(sb), ino); |
| 652 | |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 653 | if (ino < EXT4_INODES_PER_GROUP(sb)) { |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 654 | |
Aneesh Kumar K.V | 3300bed | 2009-01-03 22:33:39 -0500 | [diff] [blame^] | 655 | BUFFER_TRACE(inode_bitmap_bh, "get_write_access"); |
| 656 | err = ext4_journal_get_write_access(handle, |
| 657 | inode_bitmap_bh); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 658 | if (err) |
| 659 | goto fail; |
| 660 | |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 661 | if (!ext4_set_bit_atomic(sb_bgl_lock(sbi, group), |
Aneesh Kumar K.V | 3300bed | 2009-01-03 22:33:39 -0500 | [diff] [blame^] | 662 | ino, inode_bitmap_bh->b_data)) { |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 663 | /* we won it */ |
Aneesh Kumar K.V | 3300bed | 2009-01-03 22:33:39 -0500 | [diff] [blame^] | 664 | BUFFER_TRACE(inode_bitmap_bh, |
Frank Mayhar | 0390131 | 2009-01-07 00:06:22 -0500 | [diff] [blame] | 665 | "call ext4_handle_dirty_metadata"); |
| 666 | err = ext4_handle_dirty_metadata(handle, |
Aneesh Kumar K.V | 3300bed | 2009-01-03 22:33:39 -0500 | [diff] [blame^] | 667 | inode, |
| 668 | inode_bitmap_bh); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 669 | if (err) |
| 670 | goto fail; |
| 671 | goto got; |
| 672 | } |
| 673 | /* we lost it */ |
Aneesh Kumar K.V | 3300bed | 2009-01-03 22:33:39 -0500 | [diff] [blame^] | 674 | ext4_handle_release_buffer(handle, inode_bitmap_bh); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 675 | |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 676 | if (++ino < EXT4_INODES_PER_GROUP(sb)) |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 677 | goto repeat_in_this_group; |
| 678 | } |
| 679 | |
| 680 | /* |
| 681 | * This case is possible in concurrent environment. It is very |
| 682 | * rare. We cannot repeat the find_group_xxx() call because |
| 683 | * that will simply return the same blockgroup, because the |
| 684 | * group descriptor metadata has not yet been updated. |
| 685 | * So we just go onto the next blockgroup. |
| 686 | */ |
| 687 | if (++group == sbi->s_groups_count) |
| 688 | group = 0; |
| 689 | } |
| 690 | err = -ENOSPC; |
| 691 | goto out; |
| 692 | |
| 693 | got: |
Andreas Dilger | 717d50e | 2007-10-16 18:38:25 -0400 | [diff] [blame] | 694 | ino++; |
| 695 | if ((group == 0 && ino < EXT4_FIRST_INO(sb)) || |
| 696 | ino > EXT4_INODES_PER_GROUP(sb)) { |
Harvey Harrison | 46e665e | 2008-04-17 10:38:59 -0400 | [diff] [blame] | 697 | ext4_error(sb, __func__, |
Andreas Dilger | 717d50e | 2007-10-16 18:38:25 -0400 | [diff] [blame] | 698 | "reserved inode or inode > inodes count - " |
Theodore Ts'o | a9df9a4 | 2009-01-05 22:18:16 -0500 | [diff] [blame] | 699 | "block_group = %u, inode=%lu", group, |
Andreas Dilger | 717d50e | 2007-10-16 18:38:25 -0400 | [diff] [blame] | 700 | ino + group * EXT4_INODES_PER_GROUP(sb)); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 701 | err = -EIO; |
| 702 | goto fail; |
| 703 | } |
| 704 | |
Aneesh Kumar K.V | 3300bed | 2009-01-03 22:33:39 -0500 | [diff] [blame^] | 705 | BUFFER_TRACE(group_desc_bh, "get_write_access"); |
| 706 | err = ext4_journal_get_write_access(handle, group_desc_bh); |
| 707 | if (err) |
| 708 | goto fail; |
Andreas Dilger | 717d50e | 2007-10-16 18:38:25 -0400 | [diff] [blame] | 709 | |
| 710 | /* We may have to initialize the block bitmap if it isn't already */ |
| 711 | if (EXT4_HAS_RO_COMPAT_FEATURE(sb, EXT4_FEATURE_RO_COMPAT_GDT_CSUM) && |
| 712 | gdp->bg_flags & cpu_to_le16(EXT4_BG_BLOCK_UNINIT)) { |
Aneesh Kumar K.V | 3300bed | 2009-01-03 22:33:39 -0500 | [diff] [blame^] | 713 | struct buffer_head *block_bitmap_bh; |
Andreas Dilger | 717d50e | 2007-10-16 18:38:25 -0400 | [diff] [blame] | 714 | |
Aneesh Kumar K.V | 3300bed | 2009-01-03 22:33:39 -0500 | [diff] [blame^] | 715 | block_bitmap_bh = ext4_read_block_bitmap(sb, group); |
| 716 | BUFFER_TRACE(block_bitmap_bh, "get block bitmap access"); |
| 717 | err = ext4_journal_get_write_access(handle, block_bitmap_bh); |
Andreas Dilger | 717d50e | 2007-10-16 18:38:25 -0400 | [diff] [blame] | 718 | if (err) { |
Aneesh Kumar K.V | 3300bed | 2009-01-03 22:33:39 -0500 | [diff] [blame^] | 719 | brelse(block_bitmap_bh); |
Andreas Dilger | 717d50e | 2007-10-16 18:38:25 -0400 | [diff] [blame] | 720 | goto fail; |
| 721 | } |
| 722 | |
| 723 | free = 0; |
| 724 | spin_lock(sb_bgl_lock(sbi, group)); |
| 725 | /* recheck and clear flag under lock if we still need to */ |
| 726 | if (gdp->bg_flags & cpu_to_le16(EXT4_BG_BLOCK_UNINIT)) { |
Andreas Dilger | 717d50e | 2007-10-16 18:38:25 -0400 | [diff] [blame] | 727 | free = ext4_free_blocks_after_init(sb, group, gdp); |
Aneesh Kumar K.V | 3300bed | 2009-01-03 22:33:39 -0500 | [diff] [blame^] | 728 | gdp->bg_flags &= cpu_to_le16(~EXT4_BG_BLOCK_UNINIT); |
Aneesh Kumar K.V | 560671a | 2009-01-05 22:20:24 -0500 | [diff] [blame] | 729 | ext4_free_blks_set(sb, gdp, free); |
Frederic Bohe | 23712a9 | 2008-11-07 09:21:01 -0500 | [diff] [blame] | 730 | gdp->bg_checksum = ext4_group_desc_csum(sbi, group, |
| 731 | gdp); |
Andreas Dilger | 717d50e | 2007-10-16 18:38:25 -0400 | [diff] [blame] | 732 | } |
| 733 | spin_unlock(sb_bgl_lock(sbi, group)); |
| 734 | |
| 735 | /* Don't need to dirty bitmap block if we didn't change it */ |
| 736 | if (free) { |
Aneesh Kumar K.V | 3300bed | 2009-01-03 22:33:39 -0500 | [diff] [blame^] | 737 | BUFFER_TRACE(block_bitmap_bh, "dirty block bitmap"); |
Frank Mayhar | 0390131 | 2009-01-07 00:06:22 -0500 | [diff] [blame] | 738 | err = ext4_handle_dirty_metadata(handle, |
Aneesh Kumar K.V | 3300bed | 2009-01-03 22:33:39 -0500 | [diff] [blame^] | 739 | NULL, block_bitmap_bh); |
Andreas Dilger | 717d50e | 2007-10-16 18:38:25 -0400 | [diff] [blame] | 740 | } |
| 741 | |
Aneesh Kumar K.V | 3300bed | 2009-01-03 22:33:39 -0500 | [diff] [blame^] | 742 | brelse(block_bitmap_bh); |
Andreas Dilger | 717d50e | 2007-10-16 18:38:25 -0400 | [diff] [blame] | 743 | if (err) |
| 744 | goto fail; |
| 745 | } |
| 746 | |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 747 | spin_lock(sb_bgl_lock(sbi, group)); |
Andreas Dilger | 717d50e | 2007-10-16 18:38:25 -0400 | [diff] [blame] | 748 | /* If we didn't allocate from within the initialized part of the inode |
| 749 | * table then we need to initialize up to this inode. */ |
| 750 | if (EXT4_HAS_RO_COMPAT_FEATURE(sb, EXT4_FEATURE_RO_COMPAT_GDT_CSUM)) { |
| 751 | if (gdp->bg_flags & cpu_to_le16(EXT4_BG_INODE_UNINIT)) { |
| 752 | gdp->bg_flags &= cpu_to_le16(~EXT4_BG_INODE_UNINIT); |
| 753 | |
| 754 | /* When marking the block group with |
| 755 | * ~EXT4_BG_INODE_UNINIT we don't want to depend |
Eric Sandeen | b5f10ee | 2008-08-02 21:21:08 -0400 | [diff] [blame] | 756 | * on the value of bg_itable_unused even though |
Andreas Dilger | 717d50e | 2007-10-16 18:38:25 -0400 | [diff] [blame] | 757 | * mke2fs could have initialized the same for us. |
| 758 | * Instead we calculated the value below |
| 759 | */ |
| 760 | |
| 761 | free = 0; |
| 762 | } else { |
| 763 | free = EXT4_INODES_PER_GROUP(sb) - |
Aneesh Kumar K.V | 560671a | 2009-01-05 22:20:24 -0500 | [diff] [blame] | 764 | ext4_itable_unused_count(sb, gdp); |
Andreas Dilger | 717d50e | 2007-10-16 18:38:25 -0400 | [diff] [blame] | 765 | } |
| 766 | |
| 767 | /* |
| 768 | * Check the relative inode number against the last used |
| 769 | * relative inode number in this group. if it is greater |
| 770 | * we need to update the bg_itable_unused count |
| 771 | * |
| 772 | */ |
| 773 | if (ino > free) |
Aneesh Kumar K.V | 560671a | 2009-01-05 22:20:24 -0500 | [diff] [blame] | 774 | ext4_itable_unused_set(sb, gdp, |
| 775 | (EXT4_INODES_PER_GROUP(sb) - ino)); |
Andreas Dilger | 717d50e | 2007-10-16 18:38:25 -0400 | [diff] [blame] | 776 | } |
| 777 | |
Aneesh Kumar K.V | 560671a | 2009-01-05 22:20:24 -0500 | [diff] [blame] | 778 | count = ext4_free_inodes_count(sb, gdp) - 1; |
| 779 | ext4_free_inodes_set(sb, gdp, count); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 780 | if (S_ISDIR(mode)) { |
Aneesh Kumar K.V | 560671a | 2009-01-05 22:20:24 -0500 | [diff] [blame] | 781 | count = ext4_used_dirs_count(sb, gdp) + 1; |
| 782 | ext4_used_dirs_set(sb, gdp, count); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 783 | } |
Andreas Dilger | 717d50e | 2007-10-16 18:38:25 -0400 | [diff] [blame] | 784 | gdp->bg_checksum = ext4_group_desc_csum(sbi, group, gdp); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 785 | spin_unlock(sb_bgl_lock(sbi, group)); |
Aneesh Kumar K.V | 3300bed | 2009-01-03 22:33:39 -0500 | [diff] [blame^] | 786 | BUFFER_TRACE(group_desc_bh, "call ext4_handle_dirty_metadata"); |
| 787 | err = ext4_handle_dirty_metadata(handle, NULL, group_desc_bh); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 788 | if (err) goto fail; |
| 789 | |
| 790 | percpu_counter_dec(&sbi->s_freeinodes_counter); |
| 791 | if (S_ISDIR(mode)) |
| 792 | percpu_counter_inc(&sbi->s_dirs_counter); |
| 793 | sb->s_dirt = 1; |
| 794 | |
Jose R. Santos | 772cb7c | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 795 | if (sbi->s_log_groups_per_flex) { |
| 796 | flex_group = ext4_flex_group(sbi, group); |
| 797 | spin_lock(sb_bgl_lock(sbi, flex_group)); |
| 798 | sbi->s_flex_groups[flex_group].free_inodes--; |
| 799 | spin_unlock(sb_bgl_lock(sbi, flex_group)); |
| 800 | } |
| 801 | |
David Howells | 4c9c544 | 2008-11-14 10:38:51 +1100 | [diff] [blame] | 802 | inode->i_uid = current_fsuid(); |
Theodore Ts'o | af5bc92 | 2008-09-08 22:25:24 -0400 | [diff] [blame] | 803 | if (test_opt(sb, GRPID)) |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 804 | inode->i_gid = dir->i_gid; |
| 805 | else if (dir->i_mode & S_ISGID) { |
| 806 | inode->i_gid = dir->i_gid; |
| 807 | if (S_ISDIR(mode)) |
| 808 | mode |= S_ISGID; |
| 809 | } else |
David Howells | 4c9c544 | 2008-11-14 10:38:51 +1100 | [diff] [blame] | 810 | inode->i_gid = current_fsgid(); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 811 | inode->i_mode = mode; |
| 812 | |
Andreas Dilger | 717d50e | 2007-10-16 18:38:25 -0400 | [diff] [blame] | 813 | inode->i_ino = ino + group * EXT4_INODES_PER_GROUP(sb); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 814 | /* This is the optimal IO size (for stat), not the fs block size */ |
| 815 | inode->i_blocks = 0; |
Kalpak Shah | ef7f383 | 2007-07-18 09:15:20 -0400 | [diff] [blame] | 816 | inode->i_mtime = inode->i_atime = inode->i_ctime = ei->i_crtime = |
| 817 | ext4_current_time(inode); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 818 | |
| 819 | memset(ei->i_data, 0, sizeof(ei->i_data)); |
| 820 | ei->i_dir_start_lookup = 0; |
| 821 | ei->i_disksize = 0; |
| 822 | |
Aneesh Kumar K.V | 42bf038 | 2008-02-25 16:38:03 -0500 | [diff] [blame] | 823 | /* |
| 824 | * Don't inherit extent flag from directory. We set extent flag on |
| 825 | * newly created directory and file only if -o extent mount option is |
| 826 | * specified |
| 827 | */ |
| 828 | 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] | 829 | if (S_ISLNK(mode)) |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 830 | ei->i_flags &= ~(EXT4_IMMUTABLE_FL|EXT4_APPEND_FL); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 831 | /* dirsync only applies to directories */ |
| 832 | if (!S_ISDIR(mode)) |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 833 | ei->i_flags &= ~EXT4_DIRSYNC_FL; |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 834 | ei->i_file_acl = 0; |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 835 | ei->i_dtime = 0; |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 836 | ei->i_block_group = group; |
| 837 | |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 838 | ext4_set_inode_flags(inode); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 839 | if (IS_DIRSYNC(inode)) |
Frank Mayhar | 0390131 | 2009-01-07 00:06:22 -0500 | [diff] [blame] | 840 | ext4_handle_sync(handle); |
Al Viro | 6b38e84 | 2008-12-30 02:03:31 -0500 | [diff] [blame] | 841 | if (insert_inode_locked(inode) < 0) { |
| 842 | err = -EINVAL; |
| 843 | goto fail_drop; |
| 844 | } |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 845 | spin_lock(&sbi->s_next_gen_lock); |
| 846 | inode->i_generation = sbi->s_next_generation++; |
| 847 | spin_unlock(&sbi->s_next_gen_lock); |
| 848 | |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 849 | ei->i_state = EXT4_STATE_NEW; |
Kalpak Shah | ef7f383 | 2007-07-18 09:15:20 -0400 | [diff] [blame] | 850 | |
| 851 | ei->i_extra_isize = EXT4_SB(sb)->s_want_extra_isize; |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 852 | |
| 853 | ret = inode; |
Theodore Ts'o | af5bc92 | 2008-09-08 22:25:24 -0400 | [diff] [blame] | 854 | if (DQUOT_ALLOC_INODE(inode)) { |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 855 | err = -EDQUOT; |
| 856 | goto fail_drop; |
| 857 | } |
| 858 | |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 859 | err = ext4_init_acl(handle, inode, dir); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 860 | if (err) |
| 861 | goto fail_free_drop; |
| 862 | |
Theodore Ts'o | af5bc92 | 2008-09-08 22:25:24 -0400 | [diff] [blame] | 863 | err = ext4_init_security(handle, inode, dir); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 864 | if (err) |
| 865 | goto fail_free_drop; |
| 866 | |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 867 | if (test_opt(sb, EXTENTS)) { |
Eric Sandeen | e4079a1 | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 868 | /* set extent flag only for directory, file and normal symlink*/ |
Aneesh Kumar K.V | e65187e | 2008-04-29 08:11:12 -0400 | [diff] [blame] | 869 | if (S_ISDIR(mode) || S_ISREG(mode) || S_ISLNK(mode)) { |
Aneesh Kumar K.V | 42bf038 | 2008-02-25 16:38:03 -0500 | [diff] [blame] | 870 | EXT4_I(inode)->i_flags |= EXT4_EXTENTS_FL; |
| 871 | ext4_ext_tree_init(handle, inode); |
Aneesh Kumar K.V | 42bf038 | 2008-02-25 16:38:03 -0500 | [diff] [blame] | 872 | } |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 873 | } |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 874 | |
Aneesh Kumar K.V | 8753e88 | 2008-04-29 22:00:36 -0400 | [diff] [blame] | 875 | err = ext4_mark_inode_dirty(handle, inode); |
| 876 | if (err) { |
| 877 | ext4_std_error(sb, err); |
| 878 | goto fail_free_drop; |
| 879 | } |
| 880 | |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 881 | ext4_debug("allocating inode %lu\n", inode->i_ino); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 882 | goto really_out; |
| 883 | fail: |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 884 | ext4_std_error(sb, err); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 885 | out: |
| 886 | iput(inode); |
| 887 | ret = ERR_PTR(err); |
| 888 | really_out: |
Aneesh Kumar K.V | 3300bed | 2009-01-03 22:33:39 -0500 | [diff] [blame^] | 889 | brelse(inode_bitmap_bh); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 890 | return ret; |
| 891 | |
| 892 | fail_free_drop: |
| 893 | DQUOT_FREE_INODE(inode); |
| 894 | |
| 895 | fail_drop: |
| 896 | DQUOT_DROP(inode); |
| 897 | inode->i_flags |= S_NOQUOTA; |
| 898 | inode->i_nlink = 0; |
Al Viro | 6b38e84 | 2008-12-30 02:03:31 -0500 | [diff] [blame] | 899 | unlock_new_inode(inode); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 900 | iput(inode); |
Aneesh Kumar K.V | 3300bed | 2009-01-03 22:33:39 -0500 | [diff] [blame^] | 901 | brelse(inode_bitmap_bh); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 902 | return ERR_PTR(err); |
| 903 | } |
| 904 | |
| 905 | /* Verify that we are loading a valid orphan from disk */ |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 906 | struct inode *ext4_orphan_get(struct super_block *sb, unsigned long ino) |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 907 | { |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 908 | 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] | 909 | ext4_group_t block_group; |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 910 | int bit; |
David Howells | 1d1fe1e | 2008-02-07 00:15:37 -0800 | [diff] [blame] | 911 | struct buffer_head *bitmap_bh; |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 912 | struct inode *inode = NULL; |
David Howells | 1d1fe1e | 2008-02-07 00:15:37 -0800 | [diff] [blame] | 913 | long err = -EIO; |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 914 | |
| 915 | /* Error cases - e2fsck has already cleaned up for us */ |
| 916 | if (ino > max_ino) { |
Harvey Harrison | 46e665e | 2008-04-17 10:38:59 -0400 | [diff] [blame] | 917 | ext4_warning(sb, __func__, |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 918 | "bad orphan ino %lu! e2fsck was run?", ino); |
David Howells | 1d1fe1e | 2008-02-07 00:15:37 -0800 | [diff] [blame] | 919 | goto error; |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 920 | } |
| 921 | |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 922 | block_group = (ino - 1) / EXT4_INODES_PER_GROUP(sb); |
| 923 | bit = (ino - 1) % EXT4_INODES_PER_GROUP(sb); |
Eric Sandeen | e29d1cd | 2008-08-02 21:21:02 -0400 | [diff] [blame] | 924 | bitmap_bh = ext4_read_inode_bitmap(sb, block_group); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 925 | if (!bitmap_bh) { |
Harvey Harrison | 46e665e | 2008-04-17 10:38:59 -0400 | [diff] [blame] | 926 | ext4_warning(sb, __func__, |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 927 | "inode bitmap error for orphan %lu", ino); |
David Howells | 1d1fe1e | 2008-02-07 00:15:37 -0800 | [diff] [blame] | 928 | goto error; |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 929 | } |
| 930 | |
| 931 | /* Having the inode bit set should be a 100% indicator that this |
| 932 | * is a valid orphan (no e2fsck run on fs). Orphans also include |
| 933 | * inodes that were being truncated, so we can't check i_nlink==0. |
| 934 | */ |
David Howells | 1d1fe1e | 2008-02-07 00:15:37 -0800 | [diff] [blame] | 935 | if (!ext4_test_bit(bit, bitmap_bh->b_data)) |
| 936 | goto bad_orphan; |
| 937 | |
| 938 | inode = ext4_iget(sb, ino); |
| 939 | if (IS_ERR(inode)) |
| 940 | goto iget_failed; |
| 941 | |
Duane Griffin | 91ef4ca | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 942 | /* |
| 943 | * If the orphans has i_nlinks > 0 then it should be able to be |
| 944 | * truncated, otherwise it won't be removed from the orphan list |
| 945 | * during processing and an infinite loop will result. |
| 946 | */ |
| 947 | if (inode->i_nlink && !ext4_can_truncate(inode)) |
| 948 | goto bad_orphan; |
| 949 | |
David Howells | 1d1fe1e | 2008-02-07 00:15:37 -0800 | [diff] [blame] | 950 | if (NEXT_ORPHAN(inode) > max_ino) |
| 951 | goto bad_orphan; |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 952 | brelse(bitmap_bh); |
| 953 | return inode; |
David Howells | 1d1fe1e | 2008-02-07 00:15:37 -0800 | [diff] [blame] | 954 | |
| 955 | iget_failed: |
| 956 | err = PTR_ERR(inode); |
| 957 | inode = NULL; |
| 958 | bad_orphan: |
Harvey Harrison | 46e665e | 2008-04-17 10:38:59 -0400 | [diff] [blame] | 959 | ext4_warning(sb, __func__, |
David Howells | 1d1fe1e | 2008-02-07 00:15:37 -0800 | [diff] [blame] | 960 | "bad orphan inode %lu! e2fsck was run?", ino); |
| 961 | printk(KERN_NOTICE "ext4_test_bit(bit=%d, block=%llu) = %d\n", |
| 962 | bit, (unsigned long long)bitmap_bh->b_blocknr, |
| 963 | ext4_test_bit(bit, bitmap_bh->b_data)); |
| 964 | printk(KERN_NOTICE "inode=%p\n", inode); |
| 965 | if (inode) { |
| 966 | printk(KERN_NOTICE "is_bad_inode(inode)=%d\n", |
| 967 | is_bad_inode(inode)); |
| 968 | printk(KERN_NOTICE "NEXT_ORPHAN(inode)=%u\n", |
| 969 | NEXT_ORPHAN(inode)); |
| 970 | printk(KERN_NOTICE "max_ino=%lu\n", max_ino); |
Duane Griffin | 91ef4ca | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 971 | printk(KERN_NOTICE "i_nlink=%u\n", inode->i_nlink); |
David Howells | 1d1fe1e | 2008-02-07 00:15:37 -0800 | [diff] [blame] | 972 | /* Avoid freeing blocks if we got a bad deleted inode */ |
| 973 | if (inode->i_nlink == 0) |
| 974 | inode->i_blocks = 0; |
| 975 | iput(inode); |
| 976 | } |
| 977 | brelse(bitmap_bh); |
| 978 | error: |
| 979 | return ERR_PTR(err); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 980 | } |
| 981 | |
Theodore Ts'o | af5bc92 | 2008-09-08 22:25:24 -0400 | [diff] [blame] | 982 | unsigned long ext4_count_free_inodes(struct super_block *sb) |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 983 | { |
| 984 | unsigned long desc_count; |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 985 | struct ext4_group_desc *gdp; |
Avantika Mathur | fd2d429 | 2008-01-28 23:58:27 -0500 | [diff] [blame] | 986 | ext4_group_t i; |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 987 | #ifdef EXT4FS_DEBUG |
| 988 | struct ext4_super_block *es; |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 989 | unsigned long bitmap_count, x; |
| 990 | struct buffer_head *bitmap_bh = NULL; |
| 991 | |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 992 | es = EXT4_SB(sb)->s_es; |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 993 | desc_count = 0; |
| 994 | bitmap_count = 0; |
| 995 | gdp = NULL; |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 996 | for (i = 0; i < EXT4_SB(sb)->s_groups_count; i++) { |
Theodore Ts'o | af5bc92 | 2008-09-08 22:25:24 -0400 | [diff] [blame] | 997 | gdp = ext4_get_group_desc(sb, i, NULL); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 998 | if (!gdp) |
| 999 | continue; |
Aneesh Kumar K.V | 560671a | 2009-01-05 22:20:24 -0500 | [diff] [blame] | 1000 | desc_count += ext4_free_inodes_count(sb, gdp); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 1001 | brelse(bitmap_bh); |
Eric Sandeen | e29d1cd | 2008-08-02 21:21:02 -0400 | [diff] [blame] | 1002 | bitmap_bh = ext4_read_inode_bitmap(sb, i); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 1003 | if (!bitmap_bh) |
| 1004 | continue; |
| 1005 | |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 1006 | x = ext4_count_free(bitmap_bh, EXT4_INODES_PER_GROUP(sb) / 8); |
Eric Sandeen | c549a95 | 2008-01-28 23:58:27 -0500 | [diff] [blame] | 1007 | printk(KERN_DEBUG "group %lu: stored = %d, counted = %lu\n", |
Aneesh Kumar K.V | 560671a | 2009-01-05 22:20:24 -0500 | [diff] [blame] | 1008 | i, ext4_free_inodes_count(sb, gdp), x); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 1009 | bitmap_count += x; |
| 1010 | } |
| 1011 | brelse(bitmap_bh); |
Theodore Ts'o | 4776004f | 2008-09-08 23:00:52 -0400 | [diff] [blame] | 1012 | printk(KERN_DEBUG "ext4_count_free_inodes: " |
| 1013 | "stored = %u, computed = %lu, %lu\n", |
| 1014 | le32_to_cpu(es->s_free_inodes_count), desc_count, bitmap_count); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 1015 | return desc_count; |
| 1016 | #else |
| 1017 | desc_count = 0; |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 1018 | for (i = 0; i < EXT4_SB(sb)->s_groups_count; i++) { |
Theodore Ts'o | af5bc92 | 2008-09-08 22:25:24 -0400 | [diff] [blame] | 1019 | gdp = ext4_get_group_desc(sb, i, NULL); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 1020 | if (!gdp) |
| 1021 | continue; |
Aneesh Kumar K.V | 560671a | 2009-01-05 22:20:24 -0500 | [diff] [blame] | 1022 | desc_count += ext4_free_inodes_count(sb, gdp); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 1023 | cond_resched(); |
| 1024 | } |
| 1025 | return desc_count; |
| 1026 | #endif |
| 1027 | } |
| 1028 | |
| 1029 | /* Called at mount-time, super-block is locked */ |
Theodore Ts'o | af5bc92 | 2008-09-08 22:25:24 -0400 | [diff] [blame] | 1030 | unsigned long ext4_count_dirs(struct super_block * sb) |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 1031 | { |
| 1032 | unsigned long count = 0; |
Avantika Mathur | fd2d429 | 2008-01-28 23:58:27 -0500 | [diff] [blame] | 1033 | ext4_group_t i; |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 1034 | |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 1035 | for (i = 0; i < EXT4_SB(sb)->s_groups_count; i++) { |
Theodore Ts'o | af5bc92 | 2008-09-08 22:25:24 -0400 | [diff] [blame] | 1036 | struct ext4_group_desc *gdp = ext4_get_group_desc(sb, i, NULL); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 1037 | if (!gdp) |
| 1038 | continue; |
Aneesh Kumar K.V | 560671a | 2009-01-05 22:20:24 -0500 | [diff] [blame] | 1039 | count += ext4_used_dirs_count(sb, gdp); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 1040 | } |
| 1041 | return count; |
| 1042 | } |