root/fs/minix/bitmap.c

/* [previous][next][first][last][top][bottom][index][help] */

DEFINITIONS

This source file includes following definitions.
  1. count_used
  2. minix_free_block
  3. minix_new_block
  4. minix_count_free_blocks
  5. minix_free_inode
  6. minix_new_inode
  7. minix_count_free_inodes

   1 /*
   2  *  linux/fs/minix/bitmap.c
   3  *
   4  *  Copyright (C) 1991, 1992  Linus Torvalds
   5  */
   6 
   7 /* bitmap.c contains the code that handles the inode and block bitmaps */
   8 
   9 #include <linux/sched.h>
  10 #include <linux/minix_fs.h>
  11 #include <linux/stat.h>
  12 #include <linux/kernel.h>
  13 #include <linux/string.h>
  14 
  15 #include <asm/bitops.h>
  16 
  17 static int nibblemap[] = { 0,1,1,2,1,2,2,3,1,2,2,3,2,3,3,4 };
  18 
  19 static unsigned long count_used(struct buffer_head *map[], unsigned numblocks,
     /* [previous][next][first][last][top][bottom][index][help] */
  20         unsigned numbits)
  21 {
  22         unsigned i, j, end, sum = 0;
  23         struct buffer_head *bh;
  24   
  25         for (i=0; (i<numblocks) && numbits; i++) {
  26                 if (!(bh=map[i])) 
  27                         return(0);
  28                 if (numbits >= (8*BLOCK_SIZE)) { 
  29                         end = BLOCK_SIZE;
  30                         numbits -= 8*BLOCK_SIZE;
  31                 } else {
  32                         int tmp;
  33                         end = numbits >> 3;
  34                         numbits &= 0x7;
  35                         tmp = bh->b_data[end] & ((1<<numbits)-1);
  36                         sum += nibblemap[tmp&0xf] + nibblemap[(tmp>>4)&0xf];
  37                         numbits = 0;
  38                 }  
  39                 for (j=0; j<end; j++)
  40                         sum += nibblemap[bh->b_data[j] & 0xf] 
  41                                 + nibblemap[(bh->b_data[j]>>4)&0xf];
  42         }
  43         return(sum);
  44 }
  45 
  46 void minix_free_block(struct super_block * sb, int block)
     /* [previous][next][first][last][top][bottom][index][help] */
  47 {
  48         struct buffer_head * bh;
  49         unsigned int bit,zone;
  50 
  51         if (!sb) {
  52                 printk("trying to free block on nonexistent device\n");
  53                 return;
  54         }
  55         if (block < sb->u.minix_sb.s_firstdatazone ||
  56             block >= sb->u.minix_sb.s_nzones) {
  57                 printk("trying to free block not in datazone\n");
  58                 return;
  59         }
  60         bh = get_hash_table(sb->s_dev,block,BLOCK_SIZE);
  61         if (bh)
  62                 clear_bit(BH_Dirty, &bh->b_state);
  63         brelse(bh);
  64         zone = block - sb->u.minix_sb.s_firstdatazone + 1;
  65         bit = zone & 8191;
  66         zone >>= 13;
  67         bh = sb->u.minix_sb.s_zmap[zone];
  68         if (!bh) {
  69                 printk("minix_free_block: nonexistent bitmap buffer\n");
  70                 return;
  71         }
  72         if (!clear_bit(bit,bh->b_data))
  73                 printk("free_block (%s:%d): bit already cleared\n",
  74                        kdevname(sb->s_dev), block);
  75         mark_buffer_dirty(bh, 1);
  76         return;
  77 }
  78 
  79 int minix_new_block(struct super_block * sb)
     /* [previous][next][first][last][top][bottom][index][help] */
  80 {
  81         struct buffer_head * bh;
  82         int i,j;
  83 
  84         if (!sb) {
  85                 printk("trying to get new block from nonexistent device\n");
  86                 return 0;
  87         }
  88 repeat:
  89         j = 8192;
  90         for (i=0 ; i<8 ; i++)
  91                 if ((bh=sb->u.minix_sb.s_zmap[i]) != NULL)
  92                         if ((j=find_first_zero_bit(bh->b_data, 8192)) < 8192)
  93                                 break;
  94         if (i>=8 || !bh || j>=8192)
  95                 return 0;
  96         if (set_bit(j,bh->b_data)) {
  97                 printk("new_block: bit already set");
  98                 goto repeat;
  99         }
 100         mark_buffer_dirty(bh, 1);
 101         j += i*8192 + sb->u.minix_sb.s_firstdatazone-1;
 102         if (j < sb->u.minix_sb.s_firstdatazone ||
 103             j >= sb->u.minix_sb.s_nzones)
 104                 return 0;
 105         if (!(bh = getblk(sb->s_dev,j,BLOCK_SIZE))) {
 106                 printk("new_block: cannot get block");
 107                 return 0;
 108         }
 109         memset(bh->b_data, 0, BLOCK_SIZE);
 110         mark_buffer_uptodate(bh, 1);
 111         mark_buffer_dirty(bh, 1);
 112         brelse(bh);
 113         return j;
 114 }
 115 
 116 unsigned long minix_count_free_blocks(struct super_block *sb)
     /* [previous][next][first][last][top][bottom][index][help] */
 117 {
 118         return (sb->u.minix_sb.s_nzones - count_used(sb->u.minix_sb.s_zmap,sb->u.minix_sb.s_zmap_blocks,sb->u.minix_sb.s_nzones))
 119                  << sb->u.minix_sb.s_log_zone_size;
 120 }
 121 
 122 void minix_free_inode(struct inode * inode)
     /* [previous][next][first][last][top][bottom][index][help] */
 123 {
 124         struct buffer_head * bh;
 125         unsigned long ino;
 126 
 127         if (!inode)
 128                 return;
 129         if (!inode->i_dev) {
 130                 printk("free_inode: inode has no device\n");
 131                 return;
 132         }
 133         if (inode->i_count != 1) {
 134                 printk("free_inode: inode has count=%d\n",inode->i_count);
 135                 return;
 136         }
 137         if (inode->i_nlink) {
 138                 printk("free_inode: inode has nlink=%d\n",inode->i_nlink);
 139                 return;
 140         }
 141         if (!inode->i_sb) {
 142                 printk("free_inode: inode on nonexistent device\n");
 143                 return;
 144         }
 145         if (inode->i_ino < 1 || inode->i_ino >= inode->i_sb->u.minix_sb.s_ninodes) {
 146                 printk("free_inode: inode 0 or nonexistent inode\n");
 147                 return;
 148         }
 149         ino = inode->i_ino;
 150         if (!(bh=inode->i_sb->u.minix_sb.s_imap[ino >> 13])) {
 151                 printk("free_inode: nonexistent imap in superblock\n");
 152                 return;
 153         }
 154         clear_inode(inode);
 155         if (!clear_bit(ino & 8191, bh->b_data))
 156                 printk("free_inode: bit %lu already cleared.\n",ino);
 157         mark_buffer_dirty(bh, 1);
 158 }
 159 
 160 struct inode * minix_new_inode(const struct inode * dir)
     /* [previous][next][first][last][top][bottom][index][help] */
 161 {
 162         struct super_block * sb;
 163         struct inode * inode;
 164         struct buffer_head * bh;
 165         int i,j;
 166 
 167         if (!dir || !(inode = get_empty_inode()))
 168                 return NULL;
 169         sb = dir->i_sb;
 170         inode->i_sb = sb;
 171         inode->i_flags = inode->i_sb->s_flags;
 172         j = 8192;
 173         for (i=0 ; i<8 ; i++)
 174                 if ((bh = inode->i_sb->u.minix_sb.s_imap[i]) != NULL)
 175                         if ((j=find_first_zero_bit(bh->b_data, 8192)) < 8192)
 176                                 break;
 177         if (!bh || j >= 8192) {
 178                 iput(inode);
 179                 return NULL;
 180         }
 181         if (set_bit(j,bh->b_data)) {    /* shouldn't happen */
 182                 printk("new_inode: bit already set");
 183                 iput(inode);
 184                 return NULL;
 185         }
 186         mark_buffer_dirty(bh, 1);
 187         j += i*8192;
 188         if (!j || j >= inode->i_sb->u.minix_sb.s_ninodes) {
 189                 iput(inode);
 190                 return NULL;
 191         }
 192         inode->i_count = 1;
 193         inode->i_nlink = 1;
 194         inode->i_dev = sb->s_dev;
 195         inode->i_uid = current->fsuid;
 196         inode->i_gid = (dir->i_mode & S_ISGID) ? dir->i_gid : current->fsgid;
 197         inode->i_dirt = 1;
 198         inode->i_ino = j;
 199         inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
 200         inode->i_op = NULL;
 201         inode->i_blocks = inode->i_blksize = 0;
 202         insert_inode_hash(inode);
 203         return inode;
 204 }
 205 
 206 unsigned long minix_count_free_inodes(struct super_block *sb)
     /* [previous][next][first][last][top][bottom][index][help] */
 207 {
 208         return sb->u.minix_sb.s_ninodes - count_used(sb->u.minix_sb.s_imap,sb->u.minix_sb.s_imap_blocks,sb->u.minix_sb.s_ninodes);
 209 }

/* [previous][next][first][last][top][bottom][index][help] */