home *** CD-ROM | disk | FTP | other *** search
/ Whiteline: Alpha / Whiteline Alpha.iso / linux / atari / source / source.lzh / atari-linux-0.01pl3 / fs / minix / bitmap.c next >
Encoding:
C/C++ Source or Header  |  1994-06-05  |  5.5 KB  |  241 lines

  1. /*
  2.  *  linux/fs/minix/bitmap.c
  3.  *
  4.  *  Copyright (C) 1991, 1992  Linus Torvalds
  5.  *
  6.  * This file is subject to the terms and conditions of the GNU General Public
  7.  * License.  See the file README.legal in the main directory of this archive
  8.  * for more details.
  9.  */
  10.  
  11. /*
  12.  * Modified for 680x0 by Hamish Macdonald
  13.  */
  14.  
  15. /* bitmap.c contains the code that handles the inode and block bitmaps */
  16.  
  17. #include <linux/sched.h>
  18. #include <linux/minix_fs.h>
  19. #include <linux/stat.h>
  20. #include <linux/kernel.h>
  21. #include <linux/string.h>
  22.  
  23. #include <asm/bitops.h>
  24.  
  25. #define clear_block(addr) (memset(addr,0,BLOCK_SIZE))
  26.  
  27. static inline int
  28. find_first_zero (char *addr)
  29. {
  30.   unsigned long res;
  31.   char *p = addr;
  32.   __asm__ __volatile__
  33.     ("    moveq #-1,d0\n\t"
  34.      "1:"
  35.      "    cmpw  %1@+,d0\n\t"
  36.      "    bne   2f\n\t"
  37.      "    subql #1,%0\n\t"
  38.      "    bne   1b\n\t"
  39.      "    bra   5f\n\t"
  40.      "2:"
  41.      "    movew %1@-,d0\n\t"
  42.      "    notw  d0\n\t"
  43.      "    movew d0,%0\n\t"
  44.      "    negw  d0\n\t"
  45.      "    andw  %0,d0\n\t"
  46.      "    bfffo d0{#16,#16},%0\n\t"
  47.      "5:"
  48.      : "=d" (res), "=a" (p)
  49.      : "0" (8192 >> 4), "1" (addr)
  50.      : "d0");
  51.   return (p - addr) * 8 + 31 - res;
  52. }
  53.  
  54. #ifdef __mc68000__
  55. #define BIT(bit) ((bit) ^ 15)
  56. #else
  57. #define BIT(bit) (bit)
  58. #endif
  59.  
  60. static int nibblemap[] = { 4,3,3,2,3,2,2,1,3,2,2,1,2,1,1,0 };
  61.  
  62. static unsigned long count_free(struct buffer_head *map[], unsigned numblocks)
  63. {
  64.     unsigned i, j, sum = 0;
  65.     struct buffer_head *bh;
  66.  
  67.     for (i=0; i < numblocks; i++) {
  68.         if (!(bh=map[i]))
  69.             return(0);
  70.         for (j=0; j < BLOCK_SIZE; j++)
  71.             sum += nibblemap[bh->b_data[j] & 0xf]
  72.                 + nibblemap[(bh->b_data[j]>>4)&0xf];
  73.     }
  74.     return(sum);
  75. }
  76.  
  77. void minix_free_block(struct super_block * sb, int block)
  78. {
  79.     struct buffer_head * bh;
  80.     unsigned int bit,zone;
  81.  
  82.     if (!sb) {
  83.         printk("trying to free block on nonexistent device\n");
  84.         return;
  85.     }
  86.     if ((ulong)block < sb->u.minix_sb.s_firstdatazone ||
  87.         (ulong)block >= sb->u.minix_sb.s_nzones) {
  88.         printk("trying to free block not in datazone\n");
  89.         return;
  90.     }
  91.     bh = get_hash_table(sb->s_dev,block,BLOCK_SIZE);
  92.     if (bh)
  93.         bh->b_dirt=0;
  94.     brelse(bh);
  95.     zone = block - sb->u.minix_sb.s_firstdatazone + 1;
  96.     bit = zone & 8191;
  97.     zone >>= 13;
  98.     bh = sb->u.minix_sb.s_zmap[zone];
  99.     if (!bh) {
  100.         printk("minix_free_block: nonexistent bitmap buffer\n");
  101.         return;
  102.     }
  103.     if (!clear_bit(BIT(bit),bh->b_data))
  104.         printk("free_block (%04x:%d): bit already cleared\n",sb->s_dev,block);
  105.     bh->b_dirt = 1;
  106.     return;
  107. }
  108.  
  109. int minix_new_block(struct super_block * sb)
  110. {
  111.     struct buffer_head * bh;
  112.     int i,j;
  113.  
  114.     if (!sb) {
  115.         printk("trying to get new block from nonexistent device\n");
  116.         return 0;
  117.     }
  118. repeat:
  119.     j = 8192;
  120.     for (i=0 ; i<8 ; i++)
  121.         if ((bh=sb->u.minix_sb.s_zmap[i]) != NULL)
  122.             if ((j=find_first_zero(bh->b_data))<8192)
  123.                 break;
  124.     if (i>=8 || !bh || j>=8192)
  125.         return 0;
  126.     if (set_bit(BIT(j),bh->b_data)) {
  127.         printk("new_block: bit already set (%d)\n", j);
  128.         goto repeat;
  129.     }
  130.     bh->b_dirt = 1;
  131.     j += i*8192 + sb->u.minix_sb.s_firstdatazone-1;
  132.     if ((ulong)j < sb->u.minix_sb.s_firstdatazone ||
  133.         (ulong)j >= sb->u.minix_sb.s_nzones)
  134.         return 0;
  135.     if (!(bh = getblk(sb->s_dev,j,BLOCK_SIZE))) {
  136.         printk("new_block: cannot get block\n");
  137.         return 0;
  138.     }
  139.     clear_block(bh->b_data);
  140.     bh->b_uptodate = 1;
  141.     bh->b_dirt = 1;
  142.     brelse(bh);
  143.     return j;
  144. }
  145.  
  146. unsigned long minix_count_free_blocks(struct super_block *sb)
  147. {
  148.     return (count_free(sb->u.minix_sb.s_zmap,sb->u.minix_sb.s_zmap_blocks)
  149.         << sb->u.minix_sb.s_log_zone_size);
  150. }
  151.  
  152. void minix_free_inode(struct inode * inode)
  153. {
  154.     struct buffer_head * bh;
  155.     unsigned long ino;
  156.  
  157.     if (!inode)
  158.         return;
  159.     if (!inode->i_dev) {
  160.         printk("free_inode: inode has no device\n");
  161.         return;
  162.     }
  163.     if (inode->i_count != 1) {
  164.         printk("free_inode: inode has count=%d\n",inode->i_count);
  165.         return;
  166.     }
  167.     if (inode->i_nlink) {
  168.         printk("free_inode: inode has nlink=%d\n",inode->i_nlink);
  169.         return;
  170.     }
  171.     if (!inode->i_sb) {
  172.         printk("free_inode: inode on nonexistent device\n");
  173.         return;
  174.     }
  175.     if (inode->i_ino < 1 || inode->i_ino >= inode->i_sb->u.minix_sb.s_ninodes) {
  176.         printk("free_inode: inode 0 or nonexistent inode\n");
  177.         return;
  178.     }
  179.     ino = inode->i_ino;
  180.     if (!(bh=inode->i_sb->u.minix_sb.s_imap[ino >> 13])) {
  181.         printk("free_inode: nonexistent imap in superblock\n");
  182.         return;
  183.     }
  184.     minix_clear_inode (inode);
  185.     clear_inode(inode);
  186.     if (!clear_bit(BIT(ino & 8191), bh->b_data))
  187.         printk("free_inode: bit %lu already cleared.\n",ino);
  188.     bh->b_dirt = 1;
  189. }
  190.  
  191. struct inode * minix_new_inode(const struct inode * dir)
  192. {
  193.     struct super_block * sb;
  194.     struct inode * inode;
  195.     struct buffer_head * bh;
  196.     int i,j;
  197.  
  198.     if (!dir || !(inode = get_empty_inode()))
  199.         return NULL;
  200.     sb = dir->i_sb;
  201.     inode->i_sb = sb;
  202.     inode->i_flags = inode->i_sb->s_flags;
  203.     j = 8192;
  204.     for (i=0 ; i<8 ; i++)
  205.         if ((bh = inode->i_sb->u.minix_sb.s_imap[i]) != NULL)
  206.             if ((j=find_first_zero(bh->b_data))<8192)
  207.                 break;
  208.     if (!bh || j >= 8192) {
  209.         iput(inode);
  210.         return NULL;
  211.     }
  212.     if (set_bit(BIT(j),bh->b_data)) {    /* shouldn't happen */
  213.         printk("new_inode: bit %u already set\n", j);
  214.         iput(inode);
  215.         return NULL;
  216.     }
  217.     bh->b_dirt = 1;
  218.     j += i*8192;
  219.     if (!j || (ulong)j >= inode->i_sb->u.minix_sb.s_ninodes) {
  220.         iput(inode);
  221.         return NULL;
  222.     }
  223.     inode->i_count = 1;
  224.     inode->i_nlink = 1;
  225.     inode->i_dev = sb->s_dev;
  226.     inode->i_uid = current->euid;
  227.     inode->i_gid = (dir->i_mode & S_ISGID) ? dir->i_gid : current->egid;
  228.     inode->i_dirt = 1;
  229.     inode->i_ino = j;
  230.     inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
  231.     inode->i_op = NULL;
  232.     inode->i_blocks = inode->i_blksize = 0;
  233.     insert_inode_hash(inode);
  234.     return inode;
  235. }
  236.  
  237. unsigned long minix_count_free_inodes(struct super_block *sb)
  238. {
  239.     return count_free(sb->u.minix_sb.s_imap,sb->u.minix_sb.s_imap_blocks);
  240. }
  241.