blob: c4a554df7b7e63bb77c3678c9fb3b9eb8ba725c3 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * linux/fs/minix/bitmap.c
3 *
4 * Copyright (C) 1991, 1992 Linus Torvalds
5 */
6
7/*
8 * Modified for 680x0 by Hamish Macdonald
9 * Fixed for 680x0 by Andreas Schwab
10 */
11
12/* bitmap.c contains the code that handles the inode and block bitmaps */
13
14#include "minix.h"
15#include <linux/smp_lock.h>
16#include <linux/buffer_head.h>
17#include <linux/bitops.h>
18
19static int nibblemap[] = { 4,3,3,2,3,2,2,1,3,2,2,1,2,1,1,0 };
20
21static unsigned long count_free(struct buffer_head *map[], unsigned numblocks, __u32 numbits)
22{
23 unsigned i, j, sum = 0;
24 struct buffer_head *bh;
25
26 for (i=0; i<numblocks-1; i++) {
27 if (!(bh=map[i]))
28 return(0);
Andries Brouwer939b00d2007-02-12 00:52:49 -080029 for (j=0; j<bh->b_size; j++)
Linus Torvalds1da177e2005-04-16 15:20:36 -070030 sum += nibblemap[bh->b_data[j] & 0xf]
31 + nibblemap[(bh->b_data[j]>>4) & 0xf];
32 }
33
34 if (numblocks==0 || !(bh=map[numblocks-1]))
35 return(0);
Andries Brouwer939b00d2007-02-12 00:52:49 -080036 i = ((numbits - (numblocks-1) * bh->b_size * 8) / 16) * 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -070037 for (j=0; j<i; j++) {
38 sum += nibblemap[bh->b_data[j] & 0xf]
39 + nibblemap[(bh->b_data[j]>>4) & 0xf];
40 }
41
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];
47 }
48 return(sum);
49}
50
Andries Brouwer939b00d2007-02-12 00:52:49 -080051void minix_free_block(struct inode *inode, unsigned long block)
Linus Torvalds1da177e2005-04-16 15:20:36 -070052{
Andries Brouwer939b00d2007-02-12 00:52:49 -080053 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;
Linus Torvalds1da177e2005-04-16 15:20:36 -070058
59 if (block < sbi->s_firstdatazone || block >= sbi->s_nzones) {
Denis Vlasenko11b84482006-03-25 03:07:42 -080060 printk("Trying to free block not in datazone\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -070061 return;
62 }
63 zone = block - sbi->s_firstdatazone + 1;
Andries Brouwer939b00d2007-02-12 00:52:49 -080064 bit = zone & ((1<<k) - 1);
65 zone >>= k;
Linus Torvalds1da177e2005-04-16 15:20:36 -070066 if (zone >= sbi->s_zmap_blocks) {
67 printk("minix_free_block: nonexistent bitmap buffer\n");
68 return;
69 }
70 bh = sbi->s_zmap[zone];
71 lock_kernel();
Andries Brouwer939b00d2007-02-12 00:52:49 -080072 if (!minix_test_and_clear_bit(bit, bh->b_data))
73 printk("minix_free_block (%s:%lu): bit already cleared\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -070074 sb->s_id, block);
75 unlock_kernel();
76 mark_buffer_dirty(bh);
77 return;
78}
79
80int minix_new_block(struct inode * inode)
81{
82 struct minix_sb_info *sbi = minix_sb(inode->i_sb);
Andries Brouwer939b00d2007-02-12 00:52:49 -080083 int bits_per_zone = 8 * inode->i_sb->s_blocksize;
Linus Torvalds1da177e2005-04-16 15:20:36 -070084 int i;
85
86 for (i = 0; i < sbi->s_zmap_blocks; i++) {
87 struct buffer_head *bh = sbi->s_zmap[i];
88 int j;
89
90 lock_kernel();
Andries Brouwer939b00d2007-02-12 00:52:49 -080091 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);
Linus Torvalds1da177e2005-04-16 15:20:36 -070094 unlock_kernel();
95 mark_buffer_dirty(bh);
Andries Brouwer939b00d2007-02-12 00:52:49 -080096 j += i * bits_per_zone + sbi->s_firstdatazone-1;
Linus Torvalds1da177e2005-04-16 15:20:36 -070097 if (j < sbi->s_firstdatazone || j >= sbi->s_nzones)
98 break;
99 return j;
100 }
101 unlock_kernel();
102 }
103 return 0;
104}
105
106unsigned long minix_count_free_blocks(struct minix_sb_info *sbi)
107{
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);
111}
112
113struct minix_inode *
114minix_V1_raw_inode(struct super_block *sb, ino_t ino, struct buffer_head **bh)
115{
116 int block;
117 struct minix_sb_info *sbi = minix_sb(sb);
118 struct minix_inode *p;
119
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;
124 }
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) {
Denis Vlasenko11b84482006-03-25 03:07:42 -0800130 printk("Unable to read inode block\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700131 return NULL;
132 }
133 p = (void *)(*bh)->b_data;
134 return p + ino % MINIX_INODES_PER_BLOCK;
135}
136
137struct minix2_inode *
138minix_V2_raw_inode(struct super_block *sb, ino_t ino, struct buffer_head **bh)
139{
140 int block;
141 struct minix_sb_info *sbi = minix_sb(sb);
142 struct minix2_inode *p;
Andries Brouwer939b00d2007-02-12 00:52:49 -0800143 int minix2_inodes_per_block = sb->s_blocksize / sizeof(struct minix2_inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700144
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;
150 }
151 ino--;
152 block = 2 + sbi->s_imap_blocks + sbi->s_zmap_blocks +
Andries Brouwer939b00d2007-02-12 00:52:49 -0800153 ino / minix2_inodes_per_block;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700154 *bh = sb_bread(sb, block);
155 if (!*bh) {
Denis Vlasenko11b84482006-03-25 03:07:42 -0800156 printk("Unable to read inode block\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700157 return NULL;
158 }
159 p = (void *)(*bh)->b_data;
Andries Brouwer939b00d2007-02-12 00:52:49 -0800160 return p + ino % minix2_inodes_per_block;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700161}
162
163/* Clear the link count and mode of a deleted inode on disk. */
164
165static void minix_clear_inode(struct inode *inode)
166{
167 struct buffer_head *bh = NULL;
168
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;
175 }
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;
182 }
183 }
184 if (bh) {
185 mark_buffer_dirty(bh);
186 brelse (bh);
187 }
188}
189
190void minix_free_inode(struct inode * inode)
191{
Andries Brouwer939b00d2007-02-12 00:52:49 -0800192 struct super_block *sb = inode->i_sb;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700193 struct minix_sb_info *sbi = minix_sb(inode->i_sb);
Andries Brouwer939b00d2007-02-12 00:52:49 -0800194 struct buffer_head *bh;
195 int k = sb->s_blocksize_bits + 3;
196 unsigned long ino, bit;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700197
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;
202 }
Andries Brouwer939b00d2007-02-12 00:52:49 -0800203 bit = ino & ((1<<k) - 1);
204 ino >>= k;
205 if (ino >= sbi->s_imap_blocks) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700206 printk("minix_free_inode: nonexistent imap in superblock\n");
207 goto out;
208 }
209
210 minix_clear_inode(inode); /* clear on-disk copy */
211
Andries Brouwer939b00d2007-02-12 00:52:49 -0800212 bh = sbi->s_imap[ino];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700213 lock_kernel();
Andries Brouwer939b00d2007-02-12 00:52:49 -0800214 if (!minix_test_and_clear_bit(bit, bh->b_data))
215 printk("minix_free_inode: bit %lu already cleared\n", bit);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700216 unlock_kernel();
217 mark_buffer_dirty(bh);
218 out:
219 clear_inode(inode); /* clear in-memory copy */
220}
221
222struct inode * minix_new_inode(const struct inode * dir, int * error)
223{
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;
Andries Brouwer939b00d2007-02-12 00:52:49 -0800228 int bits_per_zone = 8 * sb->s_blocksize;
229 unsigned long j;
230 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700231
232 if (!inode) {
233 *error = -ENOMEM;
234 return NULL;
235 }
Andries Brouwer939b00d2007-02-12 00:52:49 -0800236 j = bits_per_zone;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700237 bh = NULL;
238 *error = -ENOSPC;
239 lock_kernel();
240 for (i = 0; i < sbi->s_imap_blocks; i++) {
241 bh = sbi->s_imap[i];
Andries Brouwer939b00d2007-02-12 00:52:49 -0800242 j = minix_find_first_zero_bit(bh->b_data, bits_per_zone);
243 if (j < bits_per_zone)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700244 break;
245 }
Andries Brouwer939b00d2007-02-12 00:52:49 -0800246 if (!bh || j >= bits_per_zone) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700247 unlock_kernel();
248 iput(inode);
249 return NULL;
250 }
Andries Brouwer939b00d2007-02-12 00:52:49 -0800251 if (minix_test_and_set_bit(j, bh->b_data)) { /* shouldn't happen */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700252 unlock_kernel();
Andries Brouwer939b00d2007-02-12 00:52:49 -0800253 printk("minix_new_inode: bit already set\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700254 iput(inode);
255 return NULL;
256 }
257 unlock_kernel();
258 mark_buffer_dirty(bh);
Andries Brouwer939b00d2007-02-12 00:52:49 -0800259 j += i * bits_per_zone;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700260 if (!j || j > sbi->s_ninodes) {
261 iput(inode);
262 return NULL;
263 }
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;
Theodore Ts'oba52de12006-09-27 01:50:49 -0700268 inode->i_blocks = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700269 memset(&minix_i(inode)->u, 0, sizeof(minix_i(inode)->u));
270 insert_inode_hash(inode);
271 mark_inode_dirty(inode);
272
273 *error = 0;
274 return inode;
275}
276
277unsigned long minix_count_free_inodes(struct minix_sb_info *sbi)
278{
279 return count_free(sbi->s_imap, sbi->s_imap_blocks, sbi->s_ninodes + 1);
280}