Import 2.2.7
[davej-history.git] / fs / qnx4 / bitmap.c
blobb73f36deb2e36387db773a6c854931d98ac917ae
1 /*
2 * QNX4 file system, Linux implementation.
3 *
4 * Version : 0.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) .
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>
23 #include <asm/bitops.h>
25 int qnx4_new_block(struct super_block *sb)
27 return 0;
30 void count_bits(const register char *bmPart, register int size,
31 int *const tf)
33 char b;
34 int tot = *tf;
36 if (size > QNX4_BLOCK_SIZE) {
37 size = QNX4_BLOCK_SIZE;
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;
62 unsigned long qnx4_count_free_blocks(struct super_block *sb)
64 int start = 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 = sb->u.qnx4_sb.BitMap->di_size;
69 struct buffer_head *bh;
71 while (total < size) {
72 if ((bh = bread(sb->s_dev, start + offset, QNX4_BLOCK_SIZE)) == NULL) {
73 printk("qnx4: I/O error in counting free blocks\n");
74 break;
76 count_bits(bh->b_data, size - total, &total_free);
77 brelse(bh);
78 total += QNX4_BLOCK_SIZE;
81 return total_free;
84 unsigned long qnx4_count_free_inodes(struct super_block *sb)
86 return qnx4_count_free_blocks(sb) * QNX4_INODES_PER_BLOCK; /* FIXME */
89 int qnx4_is_free(struct super_block *sb, int block)
91 int start = sb->u.qnx4_sb.BitMap->di_first_xtnt.xtnt_blk - 1;
92 int size = sb->u.qnx4_sb.BitMap->di_size;
93 struct buffer_head *bh;
94 const char *g;
95 int ret = -EIO;
97 start += block / (QNX4_BLOCK_SIZE * 8);
98 QNX4DEBUG(("qnx4: is_free requesting block [%lu], bitmap in block [%lu]\n",
99 (unsigned long) block, (unsigned long) start));
100 (void) size; /* CHECKME */
101 bh = bread(sb->s_dev, start, QNX4_BLOCK_SIZE);
102 if (bh == NULL) {
103 return -EIO;
105 g = bh->b_data + (block % QNX4_BLOCK_SIZE);
106 if (((*g) & (1 << (block % 8))) == 0) {
107 QNX4DEBUG(("qnx4: is_free -> block is free\n"));
108 ret = 1;
109 } else {
110 QNX4DEBUG(("qnx4: is_free -> block is busy\n"));
111 ret = 0;
113 brelse(bh);
115 return ret;
118 int qnx4_bmap(struct inode *inode, int block)
120 QNX4DEBUG(("qnx4: bmap on block [%d]\n", block));
121 if (block < 0) {
122 return 0;
124 return !qnx4_is_free(inode->i_sb, block);
127 #ifdef CONFIG_QNX4FS_RW
129 int qnx4_set_bitmap(struct super_block *sb, int block, int busy)
131 int start = sb->u.qnx4_sb.BitMap->di_first_xtnt.xtnt_blk - 1;
132 int size = sb->u.qnx4_sb.BitMap->di_size;
133 struct buffer_head *bh;
134 char *g;
136 start += block / (QNX4_BLOCK_SIZE * 8);
137 QNX4DEBUG(("qnx4: set_bitmap requesting block [%lu], bitmap in block [%lu]\n",
138 (unsigned long) block, (unsigned long) start));
139 (void) size; /* CHECKME */
140 bh = bread(sb->s_dev, start, QNX4_BLOCK_SIZE);
141 if (bh == NULL) {
142 return -EIO;
144 g = bh->b_data + (block % QNX4_BLOCK_SIZE);
145 if (busy == 0) {
146 (*g) &= ~(1 << (block % 8));
147 } else {
148 (*g) |= (1 << (block % 8));
150 mark_buffer_dirty(bh, 1);
151 brelse(bh);
153 return 0;
156 static void qnx4_clear_inode(struct inode *inode)
158 struct qnx4_inode_info *qnx4_ino = &inode->u.qnx4_i;
160 memset(qnx4_ino->i_reserved, 0, sizeof qnx4_ino->i_reserved);
161 qnx4_ino->i_size = 0;
162 qnx4_ino->i_num_xtnts = 0;
163 qnx4_ino->i_mode = 0;
164 qnx4_ino->i_status = 0;
167 void qnx4_free_inode(struct inode *inode)
169 if (!inode) {
170 return;
172 if (!inode->i_dev) {
173 printk("free_inode: inode has no device\n");
174 return;
176 if (inode->i_count > 1) {
177 printk("free_inode: inode has count=%d\n", inode->i_count);
178 return;
180 if (inode->i_nlink) {
181 printk("free_inode: inode has nlink=%d\n", inode->i_nlink);
182 return;
184 if (!inode->i_sb) {
185 printk("free_inode: inode on nonexistent device\n");
186 return;
188 if (inode->i_ino < 1) {
189 printk("free_inode: inode 0 or nonexistent inode\n");
190 return;
192 qnx4_clear_inode(inode);
193 clear_inode(inode);
196 #endif