blob: 6ac693faae494929ced6e7e8c2a77eb3559d1888 [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"
Linus Torvalds1da177e2005-04-16 15:20:36 -070015#include <linux/buffer_head.h>
16#include <linux/bitops.h>
Alexey Dobriyane8edc6e2007-05-21 01:22:52 +040017#include <linux/sched.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070018
Philippe De Muyterfebfcf92007-10-16 23:26:15 -070019static 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 -070020
Al Virocc467592009-06-16 23:47:45 -040021static DEFINE_SPINLOCK(bitmap_lock);
22
Linus Torvalds1da177e2005-04-16 15:20:36 -070023static unsigned long count_free(struct buffer_head *map[], unsigned numblocks, __u32 numbits)
24{
25 unsigned i, j, sum = 0;
26 struct buffer_head *bh;
27
28 for (i=0; i<numblocks-1; i++) {
29 if (!(bh=map[i]))
30 return(0);
Andries Brouwer939b00d2007-02-12 00:52:49 -080031 for (j=0; j<bh->b_size; j++)
Linus Torvalds1da177e2005-04-16 15:20:36 -070032 sum += nibblemap[bh->b_data[j] & 0xf]
33 + nibblemap[(bh->b_data[j]>>4) & 0xf];
34 }
35
36 if (numblocks==0 || !(bh=map[numblocks-1]))
37 return(0);
Andries Brouwer939b00d2007-02-12 00:52:49 -080038 i = ((numbits - (numblocks-1) * bh->b_size * 8) / 16) * 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -070039 for (j=0; j<i; j++) {
40 sum += nibblemap[bh->b_data[j] & 0xf]
41 + nibblemap[(bh->b_data[j]>>4) & 0xf];
42 }
43
44 i = numbits%16;
45 if (i!=0) {
46 i = *(__u16 *)(&bh->b_data[j]) | ~((1<<i) - 1);
47 sum += nibblemap[i & 0xf] + nibblemap[(i>>4) & 0xf];
48 sum += nibblemap[(i>>8) & 0xf] + nibblemap[(i>>12) & 0xf];
49 }
50 return(sum);
51}
52
Andries Brouwer939b00d2007-02-12 00:52:49 -080053void minix_free_block(struct inode *inode, unsigned long block)
Linus Torvalds1da177e2005-04-16 15:20:36 -070054{
Andries Brouwer939b00d2007-02-12 00:52:49 -080055 struct super_block *sb = inode->i_sb;
56 struct minix_sb_info *sbi = minix_sb(sb);
57 struct buffer_head *bh;
58 int k = sb->s_blocksize_bits + 3;
59 unsigned long bit, zone;
Linus Torvalds1da177e2005-04-16 15:20:36 -070060
61 if (block < sbi->s_firstdatazone || block >= sbi->s_nzones) {
Denis Vlasenko11b84482006-03-25 03:07:42 -080062 printk("Trying to free block not in datazone\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -070063 return;
64 }
65 zone = block - sbi->s_firstdatazone + 1;
Andries Brouwer939b00d2007-02-12 00:52:49 -080066 bit = zone & ((1<<k) - 1);
67 zone >>= k;
Linus Torvalds1da177e2005-04-16 15:20:36 -070068 if (zone >= sbi->s_zmap_blocks) {
69 printk("minix_free_block: nonexistent bitmap buffer\n");
70 return;
71 }
72 bh = sbi->s_zmap[zone];
Al Virocc467592009-06-16 23:47:45 -040073 spin_lock(&bitmap_lock);
Andries Brouwer939b00d2007-02-12 00:52:49 -080074 if (!minix_test_and_clear_bit(bit, bh->b_data))
75 printk("minix_free_block (%s:%lu): bit already cleared\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -070076 sb->s_id, block);
Al Virocc467592009-06-16 23:47:45 -040077 spin_unlock(&bitmap_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070078 mark_buffer_dirty(bh);
79 return;
80}
81
82int minix_new_block(struct inode * inode)
83{
84 struct minix_sb_info *sbi = minix_sb(inode->i_sb);
Andries Brouwer939b00d2007-02-12 00:52:49 -080085 int bits_per_zone = 8 * inode->i_sb->s_blocksize;
Linus Torvalds1da177e2005-04-16 15:20:36 -070086 int i;
87
88 for (i = 0; i < sbi->s_zmap_blocks; i++) {
89 struct buffer_head *bh = sbi->s_zmap[i];
90 int j;
91
Al Virocc467592009-06-16 23:47:45 -040092 spin_lock(&bitmap_lock);
Andries Brouwer939b00d2007-02-12 00:52:49 -080093 j = minix_find_first_zero_bit(bh->b_data, bits_per_zone);
94 if (j < bits_per_zone) {
95 minix_set_bit(j, bh->b_data);
Al Virocc467592009-06-16 23:47:45 -040096 spin_unlock(&bitmap_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070097 mark_buffer_dirty(bh);
Andries Brouwer939b00d2007-02-12 00:52:49 -080098 j += i * bits_per_zone + sbi->s_firstdatazone-1;
Linus Torvalds1da177e2005-04-16 15:20:36 -070099 if (j < sbi->s_firstdatazone || j >= sbi->s_nzones)
100 break;
101 return j;
102 }
Al Virocc467592009-06-16 23:47:45 -0400103 spin_unlock(&bitmap_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104 }
105 return 0;
106}
107
108unsigned long minix_count_free_blocks(struct minix_sb_info *sbi)
109{
110 return (count_free(sbi->s_zmap, sbi->s_zmap_blocks,
111 sbi->s_nzones - sbi->s_firstdatazone + 1)
112 << sbi->s_log_zone_size);
113}
114
115struct minix_inode *
116minix_V1_raw_inode(struct super_block *sb, ino_t ino, struct buffer_head **bh)
117{
118 int block;
119 struct minix_sb_info *sbi = minix_sb(sb);
120 struct minix_inode *p;
121
122 if (!ino || ino > sbi->s_ninodes) {
123 printk("Bad inode number on dev %s: %ld is out of range\n",
124 sb->s_id, (long)ino);
125 return NULL;
126 }
127 ino--;
128 block = 2 + sbi->s_imap_blocks + sbi->s_zmap_blocks +
129 ino / MINIX_INODES_PER_BLOCK;
130 *bh = sb_bread(sb, block);
131 if (!*bh) {
Denis Vlasenko11b84482006-03-25 03:07:42 -0800132 printk("Unable to read inode block\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700133 return NULL;
134 }
135 p = (void *)(*bh)->b_data;
136 return p + ino % MINIX_INODES_PER_BLOCK;
137}
138
139struct minix2_inode *
140minix_V2_raw_inode(struct super_block *sb, ino_t ino, struct buffer_head **bh)
141{
142 int block;
143 struct minix_sb_info *sbi = minix_sb(sb);
144 struct minix2_inode *p;
Andries Brouwer939b00d2007-02-12 00:52:49 -0800145 int minix2_inodes_per_block = sb->s_blocksize / sizeof(struct minix2_inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700146
147 *bh = NULL;
148 if (!ino || ino > sbi->s_ninodes) {
149 printk("Bad inode number on dev %s: %ld is out of range\n",
150 sb->s_id, (long)ino);
151 return NULL;
152 }
153 ino--;
154 block = 2 + sbi->s_imap_blocks + sbi->s_zmap_blocks +
Andries Brouwer939b00d2007-02-12 00:52:49 -0800155 ino / minix2_inodes_per_block;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700156 *bh = sb_bread(sb, block);
157 if (!*bh) {
Denis Vlasenko11b84482006-03-25 03:07:42 -0800158 printk("Unable to read inode block\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700159 return NULL;
160 }
161 p = (void *)(*bh)->b_data;
Andries Brouwer939b00d2007-02-12 00:52:49 -0800162 return p + ino % minix2_inodes_per_block;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700163}
164
165/* Clear the link count and mode of a deleted inode on disk. */
166
167static void minix_clear_inode(struct inode *inode)
168{
169 struct buffer_head *bh = NULL;
170
171 if (INODE_VERSION(inode) == MINIX_V1) {
172 struct minix_inode *raw_inode;
173 raw_inode = minix_V1_raw_inode(inode->i_sb, inode->i_ino, &bh);
174 if (raw_inode) {
175 raw_inode->i_nlinks = 0;
176 raw_inode->i_mode = 0;
177 }
178 } else {
179 struct minix2_inode *raw_inode;
180 raw_inode = minix_V2_raw_inode(inode->i_sb, inode->i_ino, &bh);
181 if (raw_inode) {
182 raw_inode->i_nlinks = 0;
183 raw_inode->i_mode = 0;
184 }
185 }
186 if (bh) {
187 mark_buffer_dirty(bh);
188 brelse (bh);
189 }
190}
191
192void minix_free_inode(struct inode * inode)
193{
Andries Brouwer939b00d2007-02-12 00:52:49 -0800194 struct super_block *sb = inode->i_sb;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700195 struct minix_sb_info *sbi = minix_sb(inode->i_sb);
Andries Brouwer939b00d2007-02-12 00:52:49 -0800196 struct buffer_head *bh;
197 int k = sb->s_blocksize_bits + 3;
198 unsigned long ino, bit;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199
200 ino = inode->i_ino;
201 if (ino < 1 || ino > sbi->s_ninodes) {
202 printk("minix_free_inode: inode 0 or nonexistent inode\n");
203 goto out;
204 }
Andries Brouwer939b00d2007-02-12 00:52:49 -0800205 bit = ino & ((1<<k) - 1);
206 ino >>= k;
207 if (ino >= sbi->s_imap_blocks) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700208 printk("minix_free_inode: nonexistent imap in superblock\n");
209 goto out;
210 }
211
212 minix_clear_inode(inode); /* clear on-disk copy */
213
Andries Brouwer939b00d2007-02-12 00:52:49 -0800214 bh = sbi->s_imap[ino];
Al Virocc467592009-06-16 23:47:45 -0400215 spin_lock(&bitmap_lock);
Andries Brouwer939b00d2007-02-12 00:52:49 -0800216 if (!minix_test_and_clear_bit(bit, bh->b_data))
217 printk("minix_free_inode: bit %lu already cleared\n", bit);
Al Virocc467592009-06-16 23:47:45 -0400218 spin_unlock(&bitmap_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700219 mark_buffer_dirty(bh);
220 out:
221 clear_inode(inode); /* clear in-memory copy */
222}
223
224struct inode * minix_new_inode(const struct inode * dir, int * error)
225{
226 struct super_block *sb = dir->i_sb;
227 struct minix_sb_info *sbi = minix_sb(sb);
228 struct inode *inode = new_inode(sb);
229 struct buffer_head * bh;
Andries Brouwer939b00d2007-02-12 00:52:49 -0800230 int bits_per_zone = 8 * sb->s_blocksize;
231 unsigned long j;
232 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700233
234 if (!inode) {
235 *error = -ENOMEM;
236 return NULL;
237 }
Andries Brouwer939b00d2007-02-12 00:52:49 -0800238 j = bits_per_zone;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700239 bh = NULL;
240 *error = -ENOSPC;
Al Virocc467592009-06-16 23:47:45 -0400241 spin_lock(&bitmap_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700242 for (i = 0; i < sbi->s_imap_blocks; i++) {
243 bh = sbi->s_imap[i];
Andries Brouwer939b00d2007-02-12 00:52:49 -0800244 j = minix_find_first_zero_bit(bh->b_data, bits_per_zone);
245 if (j < bits_per_zone)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700246 break;
247 }
Andries Brouwer939b00d2007-02-12 00:52:49 -0800248 if (!bh || j >= bits_per_zone) {
Al Virocc467592009-06-16 23:47:45 -0400249 spin_unlock(&bitmap_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700250 iput(inode);
251 return NULL;
252 }
Andries Brouwer939b00d2007-02-12 00:52:49 -0800253 if (minix_test_and_set_bit(j, bh->b_data)) { /* shouldn't happen */
Al Virocc467592009-06-16 23:47:45 -0400254 spin_unlock(&bitmap_lock);
Andries Brouwer939b00d2007-02-12 00:52:49 -0800255 printk("minix_new_inode: bit already set\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700256 iput(inode);
257 return NULL;
258 }
Al Virocc467592009-06-16 23:47:45 -0400259 spin_unlock(&bitmap_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700260 mark_buffer_dirty(bh);
Andries Brouwer939b00d2007-02-12 00:52:49 -0800261 j += i * bits_per_zone;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700262 if (!j || j > sbi->s_ninodes) {
263 iput(inode);
264 return NULL;
265 }
David Howells922c0302008-11-14 10:38:57 +1100266 inode->i_uid = current_fsuid();
267 inode->i_gid = (dir->i_mode & S_ISGID) ? dir->i_gid : current_fsgid();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700268 inode->i_ino = j;
269 inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME_SEC;
Theodore Ts'oba52de12006-09-27 01:50:49 -0700270 inode->i_blocks = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700271 memset(&minix_i(inode)->u, 0, sizeof(minix_i(inode)->u));
272 insert_inode_hash(inode);
273 mark_inode_dirty(inode);
274
275 *error = 0;
276 return inode;
277}
278
279unsigned long minix_count_free_inodes(struct minix_sb_info *sbi)
280{
281 return count_free(sbi->s_imap, sbi->s_imap_blocks, sbi->s_ninodes + 1);
282}