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

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