Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
| 2 | * linux/fs/minix/bitmap.c |
| 3 | * |
| 4 | * Copyright (C) 1991, 1992 Linus Torvalds |
| 5 | */ |
| 6 | |
| 7 | /* |
| 8 | * Modified for 680x0 by Hamish Macdonald |
| 9 | * Fixed for 680x0 by Andreas Schwab |
| 10 | */ |
| 11 | |
| 12 | /* bitmap.c contains the code that handles the inode and block bitmaps */ |
| 13 | |
| 14 | #include "minix.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 15 | #include <linux/buffer_head.h> |
| 16 | #include <linux/bitops.h> |
Alexey Dobriyan | e8edc6e | 2007-05-21 01:22:52 +0400 | [diff] [blame] | 17 | #include <linux/sched.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 18 | |
Philippe De Muyter | febfcf9 | 2007-10-16 23:26:15 -0700 | [diff] [blame] | 19 | static const int nibblemap[] = { 4,3,3,2,3,2,2,1,3,2,2,1,2,1,1,0 }; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 20 | |
Al Viro | cc46759 | 2009-06-16 23:47:45 -0400 | [diff] [blame] | 21 | static DEFINE_SPINLOCK(bitmap_lock); |
| 22 | |
Josh Boyer | 016e8d4 | 2011-08-19 14:50:26 -0400 | [diff] [blame^] | 23 | static unsigned long count_free(struct buffer_head *map[], unsigned blocksize, __u32 numbits) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 24 | { |
| 25 | unsigned i, j, sum = 0; |
| 26 | struct buffer_head *bh; |
Josh Boyer | 016e8d4 | 2011-08-19 14:50:26 -0400 | [diff] [blame^] | 27 | unsigned numblocks = minix_blocks_needed(numbits, blocksize); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 28 | |
| 29 | for (i=0; i<numblocks-1; i++) { |
| 30 | if (!(bh=map[i])) |
| 31 | return(0); |
Andries Brouwer | 939b00d | 2007-02-12 00:52:49 -0800 | [diff] [blame] | 32 | for (j=0; j<bh->b_size; j++) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 33 | sum += nibblemap[bh->b_data[j] & 0xf] |
| 34 | + nibblemap[(bh->b_data[j]>>4) & 0xf]; |
| 35 | } |
| 36 | |
| 37 | if (numblocks==0 || !(bh=map[numblocks-1])) |
| 38 | return(0); |
Andries Brouwer | 939b00d | 2007-02-12 00:52:49 -0800 | [diff] [blame] | 39 | i = ((numbits - (numblocks-1) * bh->b_size * 8) / 16) * 2; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 40 | for (j=0; j<i; j++) { |
| 41 | sum += nibblemap[bh->b_data[j] & 0xf] |
| 42 | + nibblemap[(bh->b_data[j]>>4) & 0xf]; |
| 43 | } |
| 44 | |
| 45 | i = numbits%16; |
| 46 | if (i!=0) { |
| 47 | i = *(__u16 *)(&bh->b_data[j]) | ~((1<<i) - 1); |
| 48 | sum += nibblemap[i & 0xf] + nibblemap[(i>>4) & 0xf]; |
| 49 | sum += nibblemap[(i>>8) & 0xf] + nibblemap[(i>>12) & 0xf]; |
| 50 | } |
| 51 | return(sum); |
| 52 | } |
| 53 | |
Andries Brouwer | 939b00d | 2007-02-12 00:52:49 -0800 | [diff] [blame] | 54 | void minix_free_block(struct inode *inode, unsigned long block) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 55 | { |
Andries Brouwer | 939b00d | 2007-02-12 00:52:49 -0800 | [diff] [blame] | 56 | struct super_block *sb = inode->i_sb; |
| 57 | struct minix_sb_info *sbi = minix_sb(sb); |
| 58 | struct buffer_head *bh; |
| 59 | int k = sb->s_blocksize_bits + 3; |
| 60 | unsigned long bit, zone; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 61 | |
| 62 | if (block < sbi->s_firstdatazone || block >= sbi->s_nzones) { |
Denis Vlasenko | 11b8448 | 2006-03-25 03:07:42 -0800 | [diff] [blame] | 63 | printk("Trying to free block not in datazone\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 64 | return; |
| 65 | } |
| 66 | zone = block - sbi->s_firstdatazone + 1; |
Andries Brouwer | 939b00d | 2007-02-12 00:52:49 -0800 | [diff] [blame] | 67 | bit = zone & ((1<<k) - 1); |
| 68 | zone >>= k; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 69 | if (zone >= sbi->s_zmap_blocks) { |
| 70 | printk("minix_free_block: nonexistent bitmap buffer\n"); |
| 71 | return; |
| 72 | } |
| 73 | bh = sbi->s_zmap[zone]; |
Al Viro | cc46759 | 2009-06-16 23:47:45 -0400 | [diff] [blame] | 74 | spin_lock(&bitmap_lock); |
Andries Brouwer | 939b00d | 2007-02-12 00:52:49 -0800 | [diff] [blame] | 75 | if (!minix_test_and_clear_bit(bit, bh->b_data)) |
| 76 | printk("minix_free_block (%s:%lu): bit already cleared\n", |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 77 | sb->s_id, block); |
Al Viro | cc46759 | 2009-06-16 23:47:45 -0400 | [diff] [blame] | 78 | spin_unlock(&bitmap_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 79 | mark_buffer_dirty(bh); |
| 80 | return; |
| 81 | } |
| 82 | |
| 83 | int minix_new_block(struct inode * inode) |
| 84 | { |
| 85 | struct minix_sb_info *sbi = minix_sb(inode->i_sb); |
Andries Brouwer | 939b00d | 2007-02-12 00:52:49 -0800 | [diff] [blame] | 86 | int bits_per_zone = 8 * inode->i_sb->s_blocksize; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 87 | int i; |
| 88 | |
| 89 | for (i = 0; i < sbi->s_zmap_blocks; i++) { |
| 90 | struct buffer_head *bh = sbi->s_zmap[i]; |
| 91 | int j; |
| 92 | |
Al Viro | cc46759 | 2009-06-16 23:47:45 -0400 | [diff] [blame] | 93 | spin_lock(&bitmap_lock); |
Andries Brouwer | 939b00d | 2007-02-12 00:52:49 -0800 | [diff] [blame] | 94 | j = minix_find_first_zero_bit(bh->b_data, bits_per_zone); |
| 95 | if (j < bits_per_zone) { |
| 96 | minix_set_bit(j, bh->b_data); |
Al Viro | cc46759 | 2009-06-16 23:47:45 -0400 | [diff] [blame] | 97 | spin_unlock(&bitmap_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 98 | mark_buffer_dirty(bh); |
Andries Brouwer | 939b00d | 2007-02-12 00:52:49 -0800 | [diff] [blame] | 99 | j += i * bits_per_zone + sbi->s_firstdatazone-1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 100 | if (j < sbi->s_firstdatazone || j >= sbi->s_nzones) |
| 101 | break; |
| 102 | return j; |
| 103 | } |
Al Viro | cc46759 | 2009-06-16 23:47:45 -0400 | [diff] [blame] | 104 | spin_unlock(&bitmap_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 105 | } |
| 106 | return 0; |
| 107 | } |
| 108 | |
Josh Boyer | 016e8d4 | 2011-08-19 14:50:26 -0400 | [diff] [blame^] | 109 | unsigned long minix_count_free_blocks(struct super_block *sb) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 110 | { |
Josh Boyer | 016e8d4 | 2011-08-19 14:50:26 -0400 | [diff] [blame^] | 111 | struct minix_sb_info *sbi = minix_sb(sb); |
| 112 | u32 bits = sbi->s_nzones - (sbi->s_firstdatazone + 1); |
| 113 | |
| 114 | return (count_free(sbi->s_zmap, sb->s_blocksize, bits) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 115 | << sbi->s_log_zone_size); |
| 116 | } |
| 117 | |
| 118 | struct minix_inode * |
| 119 | minix_V1_raw_inode(struct super_block *sb, ino_t ino, struct buffer_head **bh) |
| 120 | { |
| 121 | int block; |
| 122 | struct minix_sb_info *sbi = minix_sb(sb); |
| 123 | struct minix_inode *p; |
| 124 | |
| 125 | if (!ino || ino > sbi->s_ninodes) { |
| 126 | printk("Bad inode number on dev %s: %ld is out of range\n", |
| 127 | sb->s_id, (long)ino); |
| 128 | return NULL; |
| 129 | } |
| 130 | ino--; |
| 131 | block = 2 + sbi->s_imap_blocks + sbi->s_zmap_blocks + |
| 132 | ino / MINIX_INODES_PER_BLOCK; |
| 133 | *bh = sb_bread(sb, block); |
| 134 | if (!*bh) { |
Denis Vlasenko | 11b8448 | 2006-03-25 03:07:42 -0800 | [diff] [blame] | 135 | printk("Unable to read inode block\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 136 | return NULL; |
| 137 | } |
| 138 | p = (void *)(*bh)->b_data; |
| 139 | return p + ino % MINIX_INODES_PER_BLOCK; |
| 140 | } |
| 141 | |
| 142 | struct minix2_inode * |
| 143 | minix_V2_raw_inode(struct super_block *sb, ino_t ino, struct buffer_head **bh) |
| 144 | { |
| 145 | int block; |
| 146 | struct minix_sb_info *sbi = minix_sb(sb); |
| 147 | struct minix2_inode *p; |
Andries Brouwer | 939b00d | 2007-02-12 00:52:49 -0800 | [diff] [blame] | 148 | int minix2_inodes_per_block = sb->s_blocksize / sizeof(struct minix2_inode); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 149 | |
| 150 | *bh = NULL; |
| 151 | if (!ino || ino > sbi->s_ninodes) { |
| 152 | printk("Bad inode number on dev %s: %ld is out of range\n", |
| 153 | sb->s_id, (long)ino); |
| 154 | return NULL; |
| 155 | } |
| 156 | ino--; |
| 157 | block = 2 + sbi->s_imap_blocks + sbi->s_zmap_blocks + |
Andries Brouwer | 939b00d | 2007-02-12 00:52:49 -0800 | [diff] [blame] | 158 | ino / minix2_inodes_per_block; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 159 | *bh = sb_bread(sb, block); |
| 160 | if (!*bh) { |
Denis Vlasenko | 11b8448 | 2006-03-25 03:07:42 -0800 | [diff] [blame] | 161 | printk("Unable to read inode block\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 162 | return NULL; |
| 163 | } |
| 164 | p = (void *)(*bh)->b_data; |
Andries Brouwer | 939b00d | 2007-02-12 00:52:49 -0800 | [diff] [blame] | 165 | return p + ino % minix2_inodes_per_block; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 166 | } |
| 167 | |
| 168 | /* Clear the link count and mode of a deleted inode on disk. */ |
| 169 | |
| 170 | static void minix_clear_inode(struct inode *inode) |
| 171 | { |
| 172 | struct buffer_head *bh = NULL; |
| 173 | |
| 174 | if (INODE_VERSION(inode) == MINIX_V1) { |
| 175 | struct minix_inode *raw_inode; |
| 176 | raw_inode = minix_V1_raw_inode(inode->i_sb, inode->i_ino, &bh); |
| 177 | if (raw_inode) { |
| 178 | raw_inode->i_nlinks = 0; |
| 179 | raw_inode->i_mode = 0; |
| 180 | } |
| 181 | } else { |
| 182 | struct minix2_inode *raw_inode; |
| 183 | raw_inode = minix_V2_raw_inode(inode->i_sb, inode->i_ino, &bh); |
| 184 | if (raw_inode) { |
| 185 | raw_inode->i_nlinks = 0; |
| 186 | raw_inode->i_mode = 0; |
| 187 | } |
| 188 | } |
| 189 | if (bh) { |
| 190 | mark_buffer_dirty(bh); |
| 191 | brelse (bh); |
| 192 | } |
| 193 | } |
| 194 | |
| 195 | void minix_free_inode(struct inode * inode) |
| 196 | { |
Andries Brouwer | 939b00d | 2007-02-12 00:52:49 -0800 | [diff] [blame] | 197 | struct super_block *sb = inode->i_sb; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 198 | struct minix_sb_info *sbi = minix_sb(inode->i_sb); |
Andries Brouwer | 939b00d | 2007-02-12 00:52:49 -0800 | [diff] [blame] | 199 | struct buffer_head *bh; |
| 200 | int k = sb->s_blocksize_bits + 3; |
| 201 | unsigned long ino, bit; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 202 | |
| 203 | ino = inode->i_ino; |
| 204 | if (ino < 1 || ino > sbi->s_ninodes) { |
| 205 | printk("minix_free_inode: inode 0 or nonexistent inode\n"); |
Al Viro | 5ccb4a7 | 2010-06-04 22:27:38 -0400 | [diff] [blame] | 206 | return; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 207 | } |
Andries Brouwer | 939b00d | 2007-02-12 00:52:49 -0800 | [diff] [blame] | 208 | bit = ino & ((1<<k) - 1); |
| 209 | ino >>= k; |
| 210 | if (ino >= sbi->s_imap_blocks) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 211 | printk("minix_free_inode: nonexistent imap in superblock\n"); |
Al Viro | 5ccb4a7 | 2010-06-04 22:27:38 -0400 | [diff] [blame] | 212 | return; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 213 | } |
| 214 | |
| 215 | minix_clear_inode(inode); /* clear on-disk copy */ |
| 216 | |
Andries Brouwer | 939b00d | 2007-02-12 00:52:49 -0800 | [diff] [blame] | 217 | bh = sbi->s_imap[ino]; |
Al Viro | cc46759 | 2009-06-16 23:47:45 -0400 | [diff] [blame] | 218 | spin_lock(&bitmap_lock); |
Andries Brouwer | 939b00d | 2007-02-12 00:52:49 -0800 | [diff] [blame] | 219 | if (!minix_test_and_clear_bit(bit, bh->b_data)) |
| 220 | printk("minix_free_inode: bit %lu already cleared\n", bit); |
Al Viro | cc46759 | 2009-06-16 23:47:45 -0400 | [diff] [blame] | 221 | spin_unlock(&bitmap_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 222 | mark_buffer_dirty(bh); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 223 | } |
| 224 | |
Dmitry Monakhov | 9eed1fb | 2010-03-04 17:32:14 +0300 | [diff] [blame] | 225 | struct inode *minix_new_inode(const struct inode *dir, int mode, int *error) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 226 | { |
| 227 | struct super_block *sb = dir->i_sb; |
| 228 | struct minix_sb_info *sbi = minix_sb(sb); |
| 229 | struct inode *inode = new_inode(sb); |
| 230 | struct buffer_head * bh; |
Andries Brouwer | 939b00d | 2007-02-12 00:52:49 -0800 | [diff] [blame] | 231 | int bits_per_zone = 8 * sb->s_blocksize; |
| 232 | unsigned long j; |
| 233 | int i; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 234 | |
| 235 | if (!inode) { |
| 236 | *error = -ENOMEM; |
| 237 | return NULL; |
| 238 | } |
Andries Brouwer | 939b00d | 2007-02-12 00:52:49 -0800 | [diff] [blame] | 239 | j = bits_per_zone; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 240 | bh = NULL; |
| 241 | *error = -ENOSPC; |
Al Viro | cc46759 | 2009-06-16 23:47:45 -0400 | [diff] [blame] | 242 | spin_lock(&bitmap_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 243 | for (i = 0; i < sbi->s_imap_blocks; i++) { |
| 244 | bh = sbi->s_imap[i]; |
Andries Brouwer | 939b00d | 2007-02-12 00:52:49 -0800 | [diff] [blame] | 245 | j = minix_find_first_zero_bit(bh->b_data, bits_per_zone); |
| 246 | if (j < bits_per_zone) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 247 | break; |
| 248 | } |
Andries Brouwer | 939b00d | 2007-02-12 00:52:49 -0800 | [diff] [blame] | 249 | if (!bh || j >= bits_per_zone) { |
Al Viro | cc46759 | 2009-06-16 23:47:45 -0400 | [diff] [blame] | 250 | spin_unlock(&bitmap_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 251 | iput(inode); |
| 252 | return NULL; |
| 253 | } |
Andries Brouwer | 939b00d | 2007-02-12 00:52:49 -0800 | [diff] [blame] | 254 | if (minix_test_and_set_bit(j, bh->b_data)) { /* shouldn't happen */ |
Al Viro | cc46759 | 2009-06-16 23:47:45 -0400 | [diff] [blame] | 255 | spin_unlock(&bitmap_lock); |
Andries Brouwer | 939b00d | 2007-02-12 00:52:49 -0800 | [diff] [blame] | 256 | printk("minix_new_inode: bit already set\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 257 | iput(inode); |
| 258 | return NULL; |
| 259 | } |
Al Viro | cc46759 | 2009-06-16 23:47:45 -0400 | [diff] [blame] | 260 | spin_unlock(&bitmap_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 261 | mark_buffer_dirty(bh); |
Andries Brouwer | 939b00d | 2007-02-12 00:52:49 -0800 | [diff] [blame] | 262 | j += i * bits_per_zone; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 263 | if (!j || j > sbi->s_ninodes) { |
| 264 | iput(inode); |
| 265 | return NULL; |
| 266 | } |
Dmitry Monakhov | 9eed1fb | 2010-03-04 17:32:14 +0300 | [diff] [blame] | 267 | inode_init_owner(inode, dir, mode); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 268 | inode->i_ino = j; |
| 269 | inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME_SEC; |
Theodore Ts'o | ba52de1 | 2006-09-27 01:50:49 -0700 | [diff] [blame] | 270 | inode->i_blocks = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 271 | memset(&minix_i(inode)->u, 0, sizeof(minix_i(inode)->u)); |
| 272 | insert_inode_hash(inode); |
| 273 | mark_inode_dirty(inode); |
| 274 | |
| 275 | *error = 0; |
| 276 | return inode; |
| 277 | } |
| 278 | |
Josh Boyer | 016e8d4 | 2011-08-19 14:50:26 -0400 | [diff] [blame^] | 279 | unsigned long minix_count_free_inodes(struct super_block *sb) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 280 | { |
Josh Boyer | 016e8d4 | 2011-08-19 14:50:26 -0400 | [diff] [blame^] | 281 | struct minix_sb_info *sbi = minix_sb(sb); |
| 282 | u32 bits = sbi->s_ninodes + 1; |
| 283 | |
| 284 | return count_free(sbi->s_imap, sb->s_blocksize, bits); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 285 | } |