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