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

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