include upstream ip1000a driver version 2.09f
[linux-2.4.git] / fs / qnx4 / bitmap.c
1 /*
2  * QNX4 file system, Linux implementation.
3  *
4  * Version : 0.2.1
5  *
6  * Using parts of the xiafs filesystem.
7  *
8  * History :
9  *
10  * 28-05-1998 by Richard Frowijn : first release.
11  * 20-06-1998 by Frank Denis : basic optimisations.
12  * 25-06-1998 by Frank Denis : qnx4_is_free, qnx4_set_bitmap, qnx4_bmap .
13  * 28-06-1998 by Frank Denis : qnx4_free_inode (to be fixed) .
14  */
15
16 #include <linux/config.h>
17 #include <linux/sched.h>
18 #include <linux/qnx4_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 int qnx4_new_block(struct super_block *sb)
26 {
27         return 0;
28 }
29
30 void count_bits(const register char *bmPart, register int size,
31                 int *const tf)
32 {
33         char b;
34         int tot = *tf;
35
36         if (size > QNX4_BLOCK_SIZE) {
37                 size = QNX4_BLOCK_SIZE;
38         }
39         do {
40                 b = *bmPart++;
41                 if ((b & 1) == 0)
42                         tot++;
43                 if ((b & 2) == 0)
44                         tot++;
45                 if ((b & 4) == 0)
46                         tot++;
47                 if ((b & 8) == 0)
48                         tot++;
49                 if ((b & 16) == 0)
50                         tot++;
51                 if ((b & 32) == 0)
52                         tot++;
53                 if ((b & 64) == 0)
54                         tot++;
55                 if ((b & 128) == 0)
56                         tot++;
57                 size--;
58         } while (size != 0);
59         *tf = tot;
60 }
61
62 unsigned long qnx4_count_free_blocks(struct super_block *sb)
63 {
64         int start = le32_to_cpu(sb->u.qnx4_sb.BitMap->di_first_xtnt.xtnt_blk) - 1;
65         int total = 0;
66         int total_free = 0;
67         int offset = 0;
68         int size = le32_to_cpu(sb->u.qnx4_sb.BitMap->di_size);
69         struct buffer_head *bh;
70
71         while (total < size) {
72                 if ((bh = sb_bread(sb, start + offset)) == NULL) {
73                         printk("qnx4: I/O error in counting free blocks\n");
74                         break;
75                 }
76                 count_bits(bh->b_data, size - total, &total_free);
77                 brelse(bh);
78                 total += QNX4_BLOCK_SIZE;
79                 offset++;
80         }
81
82         return total_free;
83 }
84
85 #ifdef CONFIG_QNX4FS_RW
86
87 int qnx4_is_free(struct super_block *sb, long block)
88 {
89         int start = le32_to_cpu(sb->u.qnx4_sb.BitMap->di_first_xtnt.xtnt_blk) - 1;
90         int size = le32_to_cpu(sb->u.qnx4_sb.BitMap->di_size);
91         struct buffer_head *bh;
92         const char *g;
93         int ret = -EIO;
94
95         start += block / (QNX4_BLOCK_SIZE * 8);
96         QNX4DEBUG(("qnx4: is_free requesting block [%lu], bitmap in block [%lu]\n",
97                    (unsigned long) block, (unsigned long) start));
98         (void) size;            /* CHECKME */
99         bh = sb_bread(sb, start);
100         if (bh == NULL) {
101                 return -EIO;
102         }
103         g = bh->b_data + (block % QNX4_BLOCK_SIZE);
104         if (((*g) & (1 << (block % 8))) == 0) {
105                 QNX4DEBUG(("qnx4: is_free -> block is free\n"));
106                 ret = 1;
107         } else {
108                 QNX4DEBUG(("qnx4: is_free -> block is busy\n"));
109                 ret = 0;
110         }
111         brelse(bh);
112
113         return ret;
114 }
115
116 int qnx4_set_bitmap(struct super_block *sb, long block, int busy)
117 {
118         int start = le32_to_cpu(sb->u.qnx4_sb.BitMap->di_first_xtnt.xtnt_blk) - 1;
119         int size = le32_to_cpu(sb->u.qnx4_sb.BitMap->di_size);
120         struct buffer_head *bh;
121         char *g;
122
123         start += block / (QNX4_BLOCK_SIZE * 8);
124         QNX4DEBUG(("qnx4: set_bitmap requesting block [%lu], bitmap in block [%lu]\n",
125                    (unsigned long) block, (unsigned long) start));
126         (void) size;            /* CHECKME */
127         bh = sb_bread(sb, start);
128         if (bh == NULL) {
129                 return -EIO;
130         }
131         g = bh->b_data + (block % QNX4_BLOCK_SIZE);
132         if (busy == 0) {
133                 (*g) &= ~(1 << (block % 8));
134         } else {
135                 (*g) |= (1 << (block % 8));
136         }
137         mark_buffer_dirty(bh);
138         brelse(bh);
139
140         return 0;
141 }
142
143 static void qnx4_clear_inode(struct inode *inode)
144 {
145         struct qnx4_inode_info *qnx4_ino = &inode->u.qnx4_i;
146
147         memset(qnx4_ino->i_reserved, 0, sizeof qnx4_ino->i_reserved);
148         qnx4_ino->i_size = 0;
149         qnx4_ino->i_num_xtnts = 0;
150         qnx4_ino->i_mode = 0;
151         qnx4_ino->i_status = 0;
152 }
153
154 void qnx4_free_inode(struct inode *inode)
155 {
156         if (inode->i_ino < 1) {
157                 printk("free_inode: inode 0 or nonexistent inode\n");
158                 return;
159         }
160         qnx4_clear_inode(inode);
161         clear_inode(inode);
162 }
163
164 #endif