Merge branch 'upstream' of git://ftp.linux-mips.org/pub/scm/upstream-linus
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / fs / minix / bitmap.c
blobc4a554df7b7e63bb77c3678c9fb3b9eb8ba725c3
1 /*
2 * linux/fs/minix/bitmap.c
4 * Copyright (C) 1991, 1992 Linus Torvalds
5 */
7 /*
8 * Modified for 680x0 by Hamish Macdonald
9 * Fixed for 680x0 by Andreas Schwab
12 /* bitmap.c contains the code that handles the inode and block bitmaps */
14 #include "minix.h"
15 #include <linux/smp_lock.h>
16 #include <linux/buffer_head.h>
17 #include <linux/bitops.h>
19 static int nibblemap[] = { 4,3,3,2,3,2,2,1,3,2,2,1,2,1,1,0 };
21 static unsigned long count_free(struct buffer_head *map[], unsigned numblocks, __u32 numbits)
23 unsigned i, j, sum = 0;
24 struct buffer_head *bh;
26 for (i=0; i<numblocks-1; i++) {
27 if (!(bh=map[i]))
28 return(0);
29 for (j=0; j<bh->b_size; j++)
30 sum += nibblemap[bh->b_data[j] & 0xf]
31 + nibblemap[(bh->b_data[j]>>4) & 0xf];
34 if (numblocks==0 || !(bh=map[numblocks-1]))
35 return(0);
36 i = ((numbits - (numblocks-1) * bh->b_size * 8) / 16) * 2;
37 for (j=0; j<i; j++) {
38 sum += nibblemap[bh->b_data[j] & 0xf]
39 + nibblemap[(bh->b_data[j]>>4) & 0xf];
42 i = numbits%16;
43 if (i!=0) {
44 i = *(__u16 *)(&bh->b_data[j]) | ~((1<<i) - 1);
45 sum += nibblemap[i & 0xf] + nibblemap[(i>>4) & 0xf];
46 sum += nibblemap[(i>>8) & 0xf] + nibblemap[(i>>12) & 0xf];
48 return(sum);
51 void minix_free_block(struct inode *inode, unsigned long block)
53 struct super_block *sb = inode->i_sb;
54 struct minix_sb_info *sbi = minix_sb(sb);
55 struct buffer_head *bh;
56 int k = sb->s_blocksize_bits + 3;
57 unsigned long bit, zone;
59 if (block < sbi->s_firstdatazone || block >= sbi->s_nzones) {
60 printk("Trying to free block not in datazone\n");
61 return;
63 zone = block - sbi->s_firstdatazone + 1;
64 bit = zone & ((1<<k) - 1);
65 zone >>= k;
66 if (zone >= sbi->s_zmap_blocks) {
67 printk("minix_free_block: nonexistent bitmap buffer\n");
68 return;
70 bh = sbi->s_zmap[zone];
71 lock_kernel();
72 if (!minix_test_and_clear_bit(bit, bh->b_data))
73 printk("minix_free_block (%s:%lu): bit already cleared\n",
74 sb->s_id, block);
75 unlock_kernel();
76 mark_buffer_dirty(bh);
77 return;
80 int minix_new_block(struct inode * inode)
82 struct minix_sb_info *sbi = minix_sb(inode->i_sb);
83 int bits_per_zone = 8 * inode->i_sb->s_blocksize;
84 int i;
86 for (i = 0; i < sbi->s_zmap_blocks; i++) {
87 struct buffer_head *bh = sbi->s_zmap[i];
88 int j;
90 lock_kernel();
91 j = minix_find_first_zero_bit(bh->b_data, bits_per_zone);
92 if (j < bits_per_zone) {
93 minix_set_bit(j, bh->b_data);
94 unlock_kernel();
95 mark_buffer_dirty(bh);
96 j += i * bits_per_zone + sbi->s_firstdatazone-1;
97 if (j < sbi->s_firstdatazone || j >= sbi->s_nzones)
98 break;
99 return j;
101 unlock_kernel();
103 return 0;
106 unsigned long minix_count_free_blocks(struct minix_sb_info *sbi)
108 return (count_free(sbi->s_zmap, sbi->s_zmap_blocks,
109 sbi->s_nzones - sbi->s_firstdatazone + 1)
110 << sbi->s_log_zone_size);
113 struct minix_inode *
114 minix_V1_raw_inode(struct super_block *sb, ino_t ino, struct buffer_head **bh)
116 int block;
117 struct minix_sb_info *sbi = minix_sb(sb);
118 struct minix_inode *p;
120 if (!ino || ino > sbi->s_ninodes) {
121 printk("Bad inode number on dev %s: %ld is out of range\n",
122 sb->s_id, (long)ino);
123 return NULL;
125 ino--;
126 block = 2 + sbi->s_imap_blocks + sbi->s_zmap_blocks +
127 ino / MINIX_INODES_PER_BLOCK;
128 *bh = sb_bread(sb, block);
129 if (!*bh) {
130 printk("Unable to read inode block\n");
131 return NULL;
133 p = (void *)(*bh)->b_data;
134 return p + ino % MINIX_INODES_PER_BLOCK;
137 struct minix2_inode *
138 minix_V2_raw_inode(struct super_block *sb, ino_t ino, struct buffer_head **bh)
140 int block;
141 struct minix_sb_info *sbi = minix_sb(sb);
142 struct minix2_inode *p;
143 int minix2_inodes_per_block = sb->s_blocksize / sizeof(struct minix2_inode);
145 *bh = NULL;
146 if (!ino || ino > sbi->s_ninodes) {
147 printk("Bad inode number on dev %s: %ld is out of range\n",
148 sb->s_id, (long)ino);
149 return NULL;
151 ino--;
152 block = 2 + sbi->s_imap_blocks + sbi->s_zmap_blocks +
153 ino / minix2_inodes_per_block;
154 *bh = sb_bread(sb, block);
155 if (!*bh) {
156 printk("Unable to read inode block\n");
157 return NULL;
159 p = (void *)(*bh)->b_data;
160 return p + ino % minix2_inodes_per_block;
163 /* Clear the link count and mode of a deleted inode on disk. */
165 static void minix_clear_inode(struct inode *inode)
167 struct buffer_head *bh = NULL;
169 if (INODE_VERSION(inode) == MINIX_V1) {
170 struct minix_inode *raw_inode;
171 raw_inode = minix_V1_raw_inode(inode->i_sb, inode->i_ino, &bh);
172 if (raw_inode) {
173 raw_inode->i_nlinks = 0;
174 raw_inode->i_mode = 0;
176 } else {
177 struct minix2_inode *raw_inode;
178 raw_inode = minix_V2_raw_inode(inode->i_sb, inode->i_ino, &bh);
179 if (raw_inode) {
180 raw_inode->i_nlinks = 0;
181 raw_inode->i_mode = 0;
184 if (bh) {
185 mark_buffer_dirty(bh);
186 brelse (bh);
190 void minix_free_inode(struct inode * inode)
192 struct super_block *sb = inode->i_sb;
193 struct minix_sb_info *sbi = minix_sb(inode->i_sb);
194 struct buffer_head *bh;
195 int k = sb->s_blocksize_bits + 3;
196 unsigned long ino, bit;
198 ino = inode->i_ino;
199 if (ino < 1 || ino > sbi->s_ninodes) {
200 printk("minix_free_inode: inode 0 or nonexistent inode\n");
201 goto out;
203 bit = ino & ((1<<k) - 1);
204 ino >>= k;
205 if (ino >= sbi->s_imap_blocks) {
206 printk("minix_free_inode: nonexistent imap in superblock\n");
207 goto out;
210 minix_clear_inode(inode); /* clear on-disk copy */
212 bh = sbi->s_imap[ino];
213 lock_kernel();
214 if (!minix_test_and_clear_bit(bit, bh->b_data))
215 printk("minix_free_inode: bit %lu already cleared\n", bit);
216 unlock_kernel();
217 mark_buffer_dirty(bh);
218 out:
219 clear_inode(inode); /* clear in-memory copy */
222 struct inode * minix_new_inode(const struct inode * dir, int * error)
224 struct super_block *sb = dir->i_sb;
225 struct minix_sb_info *sbi = minix_sb(sb);
226 struct inode *inode = new_inode(sb);
227 struct buffer_head * bh;
228 int bits_per_zone = 8 * sb->s_blocksize;
229 unsigned long j;
230 int i;
232 if (!inode) {
233 *error = -ENOMEM;
234 return NULL;
236 j = bits_per_zone;
237 bh = NULL;
238 *error = -ENOSPC;
239 lock_kernel();
240 for (i = 0; i < sbi->s_imap_blocks; i++) {
241 bh = sbi->s_imap[i];
242 j = minix_find_first_zero_bit(bh->b_data, bits_per_zone);
243 if (j < bits_per_zone)
244 break;
246 if (!bh || j >= bits_per_zone) {
247 unlock_kernel();
248 iput(inode);
249 return NULL;
251 if (minix_test_and_set_bit(j, bh->b_data)) { /* shouldn't happen */
252 unlock_kernel();
253 printk("minix_new_inode: bit already set\n");
254 iput(inode);
255 return NULL;
257 unlock_kernel();
258 mark_buffer_dirty(bh);
259 j += i * bits_per_zone;
260 if (!j || j > sbi->s_ninodes) {
261 iput(inode);
262 return NULL;
264 inode->i_uid = current->fsuid;
265 inode->i_gid = (dir->i_mode & S_ISGID) ? dir->i_gid : current->fsgid;
266 inode->i_ino = j;
267 inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME_SEC;
268 inode->i_blocks = 0;
269 memset(&minix_i(inode)->u, 0, sizeof(minix_i(inode)->u));
270 insert_inode_hash(inode);
271 mark_inode_dirty(inode);
273 *error = 0;
274 return inode;
277 unsigned long minix_count_free_inodes(struct minix_sb_info *sbi)
279 return count_free(sbi->s_imap, sbi->s_imap_blocks, sbi->s_ninodes + 1);