Loading...
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 | /* * linux/fs/xiafs/bitmap.c * * Copyright (C) Q. Frank Xia, 1993. * * Based on Linus' minix/bitmap.c * Copyright (C) Linus Torvalds, 1991, 1992. * * This software may be redistributed per Linux Copyright. */ /* bitmap.c contains the code that handles the inode and block bitmaps */ #include <linux/sched.h> #include <linux/locks.h> #include <linux/xia_fs.h> #include <linux/stat.h> #include <linux/kernel.h> #include <linux/string.h> #include "xiafs_mac.h" #define clear_bit(nr,addr) ({\ char res; \ __asm__ __volatile__("btrl %1,%2\n\tsetnb %0": \ "=q" (res):"r" (nr),"m" (*(addr))); \ res;}) char internal_error_message[]="XIA-FS: internal error %s %d\n"; static int find_first_zero(struct buffer_head *bh, int start_bit, int end_bit) { /* This routine searches first 0 bit from (start_bit) to (end_bit-1). * If found the bit is set to 1 and the bit # is returned, otherwise, * -1 is returned. Race condition is avoid by using "btsl" and * "goto repeat". ---Frank. */ int end, i, j, tmp; u_long *bmap; char res; bmap=(u_long *)bh->b_data; end = end_bit >> 5; repeat: i=start_bit >> 5; if ( (tmp=(~bmap[i]) & (0xffffffff << (start_bit & 31))) ) goto zone_found; while (++i < end) if (~bmap[i]) { tmp=~bmap[i]; goto zone_found; } if ( !(tmp=~bmap[i] & ((1 << (end_bit & 31)) -1)) ) return -1; zone_found: for (j=0; j < 32; j++) if (tmp & (1 << j)) break; __asm__ ("btsl %1,%2\n\tsetb %0": \ "=q" (res):"r" (j),"m" (bmap[i])); if (res) { start_bit=j + (i << 5) + 1; goto repeat; } mark_buffer_dirty(bh, 1); return j + (i << 5); } static void clear_buf(struct buffer_head * bh) { register int i; register long * lp; lp=(long *)bh->b_data; for (i= bh->b_size >> 2; i-- > 0; ) *lp++=0; } static void que(struct buffer_head * bmap[], int bznr[], int pos) { struct buffer_head * tbh; int tmp; int i; tbh=bmap[pos]; tmp=bznr[pos]; for (i=pos; i > 0; i--) { bmap[i]=bmap[i-1]; bznr[i]=bznr[i-1]; } bmap[0]=tbh; bznr[0]=tmp; } #define get_imap_zone(sb, bit_nr, not_que) \ get__map_zone((sb), (sb)->u.xiafs_sb.s_imap_buf, \ (sb)->u.xiafs_sb.s_imap_iznr, \ (sb)->u.xiafs_sb.s_imap_cached, 1, \ (sb)->u.xiafs_sb.s_imap_zones, _XIAFS_IMAP_SLOTS, \ bit_nr, not_que) #define get_zmap_zone(sb, bit_nr, not_que) \ get__map_zone((sb), (sb)->u.xiafs_sb.s_zmap_buf, \ (sb)->u.xiafs_sb.s_zmap_zznr, \ (sb)->u.xiafs_sb.s_zmap_cached, \ 1+(sb)->u.xiafs_sb.s_imap_zones, \ (sb)->u.xiafs_sb.s_zmap_zones, _XIAFS_ZMAP_SLOTS, \ bit_nr, not_que) static struct buffer_head * get__map_zone(struct super_block *sb, struct buffer_head * bmap_buf[], int bznr[], u_char cache, int first_zone, int bmap_zones, int slots, u_long bit_nr, int * not_que) { struct buffer_head * tmp_bh; int z_nr, i; z_nr = bit_nr >> XIAFS_BITS_PER_Z_BITS(sb); if (z_nr >= bmap_zones) { printk("XIA-FS: bad inode/zone number (%s %d)\n", WHERE_ERR); return NULL; } if (!cache) return bmap_buf[z_nr]; lock_super(sb); for (i=0; i < slots; i++) if (bznr[i]==z_nr) break; if (i < slots) { /* cache hit */ if (not_que) { *not_que=i; return bmap_buf[i]; } else { que(bmap_buf, bznr, i); return bmap_buf[0]; } } tmp_bh=bread(sb->s_dev, z_nr+first_zone, XIAFS_ZSIZE(sb)); /* cache not hit */ if (!tmp_bh) { printk("XIA-FS: read bitmap failed (%s %d)\n", WHERE_ERR); unlock_super(sb); return NULL; } brelse(bmap_buf[slots-1]); bmap_buf[slots-1]=tmp_bh; bznr[slots-1]=z_nr; if (not_que) *not_que=slots-1; else que(bmap_buf, bznr, slots-1); return tmp_bh; } #define xiafs_unlock_super(sb, cache) if (cache) unlock_super(sb); #define get_free_ibit(sb, prev_bit) \ get_free__bit(sb, sb->u.xiafs_sb.s_imap_buf, \ sb->u.xiafs_sb.s_imap_iznr, \ sb->u.xiafs_sb.s_imap_cached, \ 1, sb->u.xiafs_sb.s_imap_zones, \ _XIAFS_IMAP_SLOTS, prev_bit); #define get_free_zbit(sb, prev_bit) \ get_free__bit(sb, sb->u.xiafs_sb.s_zmap_buf, \ sb->u.xiafs_sb.s_zmap_zznr, \ sb->u.xiafs_sb.s_zmap_cached, \ 1 + sb->u.xiafs_sb.s_imap_zones, \ sb->u.xiafs_sb.s_zmap_zones, \ _XIAFS_ZMAP_SLOTS, prev_bit); static u_long get_free__bit(struct super_block *sb, struct buffer_head * bmap_buf[], int bznr[], u_char cache, int first_zone, int bmap_zones, int slots, u_long prev_bit) { struct buffer_head * bh; int not_done=0; u_long pos, start_bit, end_bit, total_bits; int z_nr, tmp; total_bits=bmap_zones << XIAFS_BITS_PER_Z_BITS(sb); if (prev_bit >= total_bits) prev_bit=0; pos=prev_bit+1; end_bit=XIAFS_BITS_PER_Z(sb); do { if (pos >= total_bits) pos=0; if (!not_done) { /* first time */ not_done=1; start_bit= pos & (end_bit-1); } else start_bit=0; if ( pos < prev_bit && pos+end_bit >= prev_bit) { /* last time */ not_done=0; end_bit=prev_bit & (end_bit-1); /* only here end_bit modified */ } bh = get__map_zone(sb, bmap_buf, bznr, cache, first_zone, bmap_zones, slots, pos, &z_nr); if (!bh) return 0; tmp=find_first_zero(bh, start_bit, end_bit); if (tmp >= 0) break; xiafs_unlock_super(sb, sb->u.xiafs_sb.s_zmap_cached); pos=(pos & ~(end_bit-1))+end_bit; } while (not_done); if (tmp < 0) return 0; if (cache) que(bmap_buf, bznr, z_nr); xiafs_unlock_super(sb, cache); return (pos & ~(XIAFS_BITS_PER_Z(sb)-1))+tmp; } void xiafs_free_zone(struct super_block * sb, int d_addr) { struct buffer_head * bh; unsigned int bit, offset; if (!sb) { printk(INTERN_ERR); return; } if (d_addr < sb->u.xiafs_sb.s_firstdatazone || d_addr >= sb->u.xiafs_sb.s_nzones) { printk("XIA-FS: bad zone number (%s %d)\n", WHERE_ERR); return; } bh = get_hash_table(sb->s_dev, d_addr, XIAFS_ZSIZE(sb)); if (bh) bh->b_dirt=0; brelse(bh); bit=d_addr - sb->u.xiafs_sb.s_firstdatazone + 1; bh = get_zmap_zone(sb, bit, NULL); if (!bh) return; offset = bit & (XIAFS_BITS_PER_Z(sb) -1); if (clear_bit(offset, bh->b_data)) printk("XIA-FS: dev %04x" " block bit %u (0x%x) already cleared (%s %d)\n", sb->s_dev, bit, bit, WHERE_ERR); mark_buffer_dirty(bh, 1); xiafs_unlock_super(sb, sb->u.xiafs_sb.s_zmap_cached); } int xiafs_new_zone(struct super_block * sb, u_long prev_addr) { struct buffer_head * bh; int prev_znr, tmp; if (!sb) { printk(INTERN_ERR); return 0; } if (prev_addr < sb->u.xiafs_sb.s_firstdatazone || prev_addr >= sb->u.xiafs_sb.s_nzones) { prev_addr=sb->u.xiafs_sb.s_firstdatazone; } prev_znr=prev_addr-sb->u.xiafs_sb.s_firstdatazone+1; tmp=get_free_zbit(sb, prev_znr); if (!tmp) return 0; tmp += sb->u.xiafs_sb.s_firstdatazone -1; if (!(bh = getblk(sb->s_dev, tmp, XIAFS_ZSIZE(sb)))) { printk("XIA-FS: I/O error (%s %d)\n", WHERE_ERR); return 0; } if (bh->b_count != 1) { printk(INTERN_ERR); return 0; } clear_buf(bh); bh->b_uptodate = 1; mark_buffer_dirty(bh, 1); brelse(bh); return tmp; } void xiafs_free_inode(struct inode * inode) { struct buffer_head * bh; struct super_block * sb; unsigned long ino; if (!inode) return; if (!inode->i_dev || inode->i_count!=1 || inode->i_nlink || !inode->i_sb || inode->i_ino < 3 || inode->i_ino > inode->i_sb->u.xiafs_sb.s_ninodes) { printk("XIA-FS: bad inode (%s %d)\n", WHERE_ERR); return; } sb = inode->i_sb; ino = inode->i_ino; bh = get_imap_zone(sb, ino, NULL); if (!bh) return; clear_inode(inode); if (clear_bit(ino & (XIAFS_BITS_PER_Z(sb)-1), bh->b_data)) printk("XIA-FS: dev %04x" "inode bit %ld (0x%lx) already cleared (%s %d)\n", inode->i_dev, ino, ino, WHERE_ERR); mark_buffer_dirty(bh, 1); xiafs_unlock_super(sb, sb->u.xiafs_sb.s_imap_cached); } struct inode * xiafs_new_inode(struct inode * dir) { struct super_block * sb; struct inode * inode; ino_t tmp; sb = dir->i_sb; if (!dir || !(inode = get_empty_inode())) return NULL; inode->i_sb = sb; inode->i_flags = inode->i_sb->s_flags; tmp=get_free_ibit(sb, dir->i_ino); if (!tmp) { iput(inode); return NULL; } inode->i_count = 1; inode->i_nlink = 1; inode->i_dev = sb->s_dev; inode->i_uid = current->euid; inode->i_gid = (dir->i_mode & S_ISGID) ? dir->i_gid : current->egid; inode->i_dirt = 1; inode->i_ino = tmp; inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME; inode->i_op = NULL; inode->i_blocks = 0; inode->i_blksize = XIAFS_ZSIZE(inode->i_sb); insert_inode_hash(inode); return inode; } static int nibblemap[] = { 0,1,1,2,1,2,2,3,1,2,2,3,2,3,3,4 }; static u_long count_zone(struct buffer_head * bh) { int i, tmp; u_long sum; sum=0; for (i=bh->b_size; i-- > 0; ) { tmp=bh->b_data[i]; sum += nibblemap[tmp & 0xf] + nibblemap[(tmp & 0xff) >> 4]; } return sum; } unsigned long xiafs_count_free_inodes(struct super_block *sb) { struct buffer_head * bh; int izones, i, not_que; u_long sum; sum=0; izones=sb->u.xiafs_sb.s_imap_zones; for (i=0; i < izones; i++) { bh=get_imap_zone(sb, i << XIAFS_BITS_PER_Z_BITS(sb), ¬_que); if (bh) { sum += count_zone(bh); xiafs_unlock_super(sb, sb->u.xiafs_sb.s_imap_cached); } } i=izones << XIAFS_BITS_PER_Z_BITS(sb); return i - sum; } unsigned long xiafs_count_free_zones(struct super_block *sb) { struct buffer_head * bh; int zzones, i, not_que; u_long sum; sum=0; zzones=sb->u.xiafs_sb.s_zmap_zones; for (i=0; i < zzones; i++) { bh=get_zmap_zone(sb, i << XIAFS_BITS_PER_Z_BITS(sb), ¬_que); if (bh) { sum += count_zone(bh); xiafs_unlock_super(sb, sb->u.xiafs_sb.s_zmap_cached); } } i=zzones << XIAFS_BITS_PER_Z_BITS(sb); return i - sum; } |