blob: 703cc35e04b94ce6126ae390f03f066c0fb26618 [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>
Alexey Dobriyane8edc6e2007-05-21 01:22:52 +040018#include <linux/sched.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070019
Philippe De Muyterfebfcf92007-10-16 23:26:15 -070020static const int nibblemap[] = { 4,3,3,2,3,2,2,1,3,2,2,1,2,1,1,0 };
Linus Torvalds1da177e2005-04-16 15:20:36 -070021
22static unsigned long count_free(struct buffer_head *map[], unsigned numblocks, __u32 numbits)
23{
24 unsigned i, j, sum = 0;
25 struct buffer_head *bh;
26
27 for (i=0; i<numblocks-1; i++) {
28 if (!(bh=map[i]))
29 return(0);
Andries Brouwer939b00d2007-02-12 00:52:49 -080030 for (j=0; j<bh->b_size; j++)
Linus Torvalds1da177e2005-04-16 15:20:36 -070031 sum += nibblemap[bh->b_data[j] & 0xf]
32 + nibblemap[(bh->b_data[j]>>4) & 0xf];
33 }
34
35 if (numblocks==0 || !(bh=map[numblocks-1]))
36 return(0);
Andries Brouwer939b00d2007-02-12 00:52:49 -080037 i = ((numbits - (numblocks-1) * bh->b_size * 8) / 16) * 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -070038 for (j=0; j<i; j++) {
39 sum += nibblemap[bh->b_data[j] & 0xf]
40 + nibblemap[(bh->b_data[j]>>4) & 0xf];
41 }
42
43 i = numbits%16;
44 if (i!=0) {
45 i = *(__u16 *)(&bh->b_data[j]) | ~((1<<i) - 1);
46 sum += nibblemap[i & 0xf] + nibblemap[(i>>4) & 0xf];
47 sum += nibblemap[(i>>8) & 0xf] + nibblemap[(i>>12) & 0xf];
48 }
49 return(sum);
50}
51
Andries Brouwer939b00d2007-02-12 00:52:49 -080052void minix_free_block(struct inode *inode, unsigned long block)
Linus Torvalds1da177e2005-04-16 15:20:36 -070053{
Andries Brouwer939b00d2007-02-12 00:52:49 -080054 struct super_block *sb = inode->i_sb;
55 struct minix_sb_info *sbi = minix_sb(sb);
56 struct buffer_head *bh;
57 int k = sb->s_blocksize_bits + 3;
58 unsigned long bit, zone;
Linus Torvalds1da177e2005-04-16 15:20:36 -070059
60 if (block < sbi->s_firstdatazone || block >= sbi->s_nzones) {
Denis Vlasenko11b84482006-03-25 03:07:42 -080061 printk("Trying to free block not in datazone\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -070062 return;
63 }
64 zone = block - sbi->s_firstdatazone + 1;
Andries Brouwer939b00d2007-02-12 00:52:49 -080065 bit = zone & ((1<<k) - 1);
66 zone >>= k;
Linus Torvalds1da177e2005-04-16 15:20:36 -070067 if (zone >= sbi->s_zmap_blocks) {
68 printk("minix_free_block: nonexistent bitmap buffer\n");
69 return;
70 }
71 bh = sbi->s_zmap[zone];
72 lock_kernel();
Andries Brouwer939b00d2007-02-12 00:52:49 -080073 if (!minix_test_and_clear_bit(bit, bh->b_data))
74 printk("minix_free_block (%s:%lu): bit already cleared\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -070075 sb->s_id, block);
76 unlock_kernel();
77 mark_buffer_dirty(bh);
78 return;
79}
80
81int minix_new_block(struct inode * inode)
82{
83 struct minix_sb_info *sbi = minix_sb(inode->i_sb);
Andries Brouwer939b00d2007-02-12 00:52:49 -080084 int bits_per_zone = 8 * inode->i_sb->s_blocksize;
Linus Torvalds1da177e2005-04-16 15:20:36 -070085 int i;
86
87 for (i = 0; i < sbi->s_zmap_blocks; i++) {
88 struct buffer_head *bh = sbi->s_zmap[i];
89 int j;
90
91 lock_kernel();
Andries Brouwer939b00d2007-02-12 00:52:49 -080092 j = minix_find_first_zero_bit(bh->b_data, bits_per_zone);
93 if (j < bits_per_zone) {
94 minix_set_bit(j, bh->b_data);
Linus Torvalds1da177e2005-04-16 15:20:36 -070095 unlock_kernel();
96 mark_buffer_dirty(bh);
Andries Brouwer939b00d2007-02-12 00:52:49 -080097 j += i * bits_per_zone + sbi->s_firstdatazone-1;
Linus Torvalds1da177e2005-04-16 15:20:36 -070098 if (j < sbi->s_firstdatazone || j >= sbi->s_nzones)
99 break;
100 return j;
101 }
102 unlock_kernel();
103 }
104 return 0;
105}
106
107unsigned long minix_count_free_blocks(struct minix_sb_info *sbi)
108{
109 return (count_free(sbi->s_zmap, sbi->s_zmap_blocks,
110 sbi->s_nzones - sbi->s_firstdatazone + 1)
111 << sbi->s_log_zone_size);
112}
113
114struct minix_inode *
115minix_V1_raw_inode(struct super_block *sb, ino_t ino, struct buffer_head **bh)
116{
117 int block;
118 struct minix_sb_info *sbi = minix_sb(sb);
119 struct minix_inode *p;
120
121 if (!ino || ino > sbi->s_ninodes) {
122 printk("Bad inode number on dev %s: %ld is out of range\n",
123 sb->s_id, (long)ino);
124 return NULL;
125 }
126 ino--;
127 block = 2 + sbi->s_imap_blocks + sbi->s_zmap_blocks +
128 ino / MINIX_INODES_PER_BLOCK;
129 *bh = sb_bread(sb, block);
130 if (!*bh) {
Denis Vlasenko11b84482006-03-25 03:07:42 -0800131 printk("Unable to read inode block\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700132 return NULL;
133 }
134 p = (void *)(*bh)->b_data;
135 return p + ino % MINIX_INODES_PER_BLOCK;
136}
137
138struct minix2_inode *
139minix_V2_raw_inode(struct super_block *sb, ino_t ino, struct buffer_head **bh)
140{
141 int block;
142 struct minix_sb_info *sbi = minix_sb(sb);
143 struct minix2_inode *p;
Andries Brouwer939b00d2007-02-12 00:52:49 -0800144 int minix2_inodes_per_block = sb->s_blocksize / sizeof(struct minix2_inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700145
146 *bh = NULL;
147 if (!ino || ino > sbi->s_ninodes) {
148 printk("Bad inode number on dev %s: %ld is out of range\n",
149 sb->s_id, (long)ino);
150 return NULL;
151 }
152 ino--;
153 block = 2 + sbi->s_imap_blocks + sbi->s_zmap_blocks +
Andries Brouwer939b00d2007-02-12 00:52:49 -0800154 ino / minix2_inodes_per_block;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700155 *bh = sb_bread(sb, block);
156 if (!*bh) {
Denis Vlasenko11b84482006-03-25 03:07:42 -0800157 printk("Unable to read inode block\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700158 return NULL;
159 }
160 p = (void *)(*bh)->b_data;
Andries Brouwer939b00d2007-02-12 00:52:49 -0800161 return p + ino % minix2_inodes_per_block;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700162}
163
164/* Clear the link count and mode of a deleted inode on disk. */
165
166static void minix_clear_inode(struct inode *inode)
167{
168 struct buffer_head *bh = NULL;
169
170 if (INODE_VERSION(inode) == MINIX_V1) {
171 struct minix_inode *raw_inode;
172 raw_inode = minix_V1_raw_inode(inode->i_sb, inode->i_ino, &bh);
173 if (raw_inode) {
174 raw_inode->i_nlinks = 0;
175 raw_inode->i_mode = 0;
176 }
177 } else {
178 struct minix2_inode *raw_inode;
179 raw_inode = minix_V2_raw_inode(inode->i_sb, inode->i_ino, &bh);
180 if (raw_inode) {
181 raw_inode->i_nlinks = 0;
182 raw_inode->i_mode = 0;
183 }
184 }
185 if (bh) {
186 mark_buffer_dirty(bh);
187 brelse (bh);
188 }
189}
190
191void minix_free_inode(struct inode * inode)
192{
Andries Brouwer939b00d2007-02-12 00:52:49 -0800193 struct super_block *sb = inode->i_sb;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700194 struct minix_sb_info *sbi = minix_sb(inode->i_sb);
Andries Brouwer939b00d2007-02-12 00:52:49 -0800195 struct buffer_head *bh;
196 int k = sb->s_blocksize_bits + 3;
197 unsigned long ino, bit;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700198
199 ino = inode->i_ino;
200 if (ino < 1 || ino > sbi->s_ninodes) {
201 printk("minix_free_inode: inode 0 or nonexistent inode\n");
202 goto out;
203 }
Andries Brouwer939b00d2007-02-12 00:52:49 -0800204 bit = ino & ((1<<k) - 1);
205 ino >>= k;
206 if (ino >= sbi->s_imap_blocks) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700207 printk("minix_free_inode: nonexistent imap in superblock\n");
208 goto out;
209 }
210
211 minix_clear_inode(inode); /* clear on-disk copy */
212
Andries Brouwer939b00d2007-02-12 00:52:49 -0800213 bh = sbi->s_imap[ino];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700214 lock_kernel();
Andries Brouwer939b00d2007-02-12 00:52:49 -0800215 if (!minix_test_and_clear_bit(bit, bh->b_data))
216 printk("minix_free_inode: bit %lu already cleared\n", bit);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700217 unlock_kernel();
218 mark_buffer_dirty(bh);
219 out:
220 clear_inode(inode); /* clear in-memory copy */
221}
222
223struct inode * minix_new_inode(const struct inode * dir, int * error)
224{
225 struct super_block *sb = dir->i_sb;
226 struct minix_sb_info *sbi = minix_sb(sb);
227 struct inode *inode = new_inode(sb);
228 struct buffer_head * bh;
Andries Brouwer939b00d2007-02-12 00:52:49 -0800229 int bits_per_zone = 8 * sb->s_blocksize;
230 unsigned long j;
231 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700232
233 if (!inode) {
234 *error = -ENOMEM;
235 return NULL;
236 }
Andries Brouwer939b00d2007-02-12 00:52:49 -0800237 j = bits_per_zone;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700238 bh = NULL;
239 *error = -ENOSPC;
240 lock_kernel();
241 for (i = 0; i < sbi->s_imap_blocks; i++) {
242 bh = sbi->s_imap[i];
Andries Brouwer939b00d2007-02-12 00:52:49 -0800243 j = minix_find_first_zero_bit(bh->b_data, bits_per_zone);
244 if (j < bits_per_zone)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700245 break;
246 }
Andries Brouwer939b00d2007-02-12 00:52:49 -0800247 if (!bh || j >= bits_per_zone) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700248 unlock_kernel();
249 iput(inode);
250 return NULL;
251 }
Andries Brouwer939b00d2007-02-12 00:52:49 -0800252 if (minix_test_and_set_bit(j, bh->b_data)) { /* shouldn't happen */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700253 unlock_kernel();
Andries Brouwer939b00d2007-02-12 00:52:49 -0800254 printk("minix_new_inode: bit already set\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700255 iput(inode);
256 return NULL;
257 }
258 unlock_kernel();
259 mark_buffer_dirty(bh);
Andries Brouwer939b00d2007-02-12 00:52:49 -0800260 j += i * bits_per_zone;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700261 if (!j || j > sbi->s_ninodes) {
262 iput(inode);
263 return NULL;
264 }
265 inode->i_uid = current->fsuid;
266 inode->i_gid = (dir->i_mode & S_ISGID) ? dir->i_gid : current->fsgid;
267 inode->i_ino = j;
268 inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME_SEC;
Theodore Ts'oba52de12006-09-27 01:50:49 -0700269 inode->i_blocks = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700270 memset(&minix_i(inode)->u, 0, sizeof(minix_i(inode)->u));
271 insert_inode_hash(inode);
272 mark_inode_dirty(inode);
273
274 *error = 0;
275 return inode;
276}
277
278unsigned long minix_count_free_inodes(struct minix_sb_info *sbi)
279{
280 return count_free(sbi->s_imap, sbi->s_imap_blocks, sbi->s_ninodes + 1);
281}