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