xref: /linux/fs/minix/bitmap.c (revision 486b257357c23743d9729ea715136d105fff461e)
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  *  linux/fs/minix/bitmap.c
4  *
5  *  Copyright (C) 1991, 1992  Linus Torvalds
6  */
7 
8 /*
9  * Modified for 680x0 by Hamish Macdonald
10  * Fixed for 680x0 by Andreas Schwab
11  */
12 
13 /* bitmap.c contains the code that handles the inode and block bitmaps */
14 
15 #include "minix.h"
16 #include <linux/buffer_head.h>
17 #include <linux/bitops.h>
18 #include <linux/sched.h>
19 
20 static DEFINE_SPINLOCK(bitmap_lock);
21 
22 /*
23  * bitmap consists of blocks filled with 16bit words
24  * bit set == busy, bit clear == free
25  * endianness is a mess, but for counting zero bits it really doesn't matter...
26  */
27 static __u32 count_free(struct buffer_head *map[], unsigned blocksize, __u32 numbits)
28 {
29 	__u32 sum = 0;
30 	unsigned blocks = DIV_ROUND_UP(numbits, blocksize * 8);
31 
32 	while (blocks--) {
33 		unsigned words = blocksize / 2;
34 		__u16 *p = (__u16 *)(*map++)->b_data;
35 		while (words--)
36 			sum += 16 - hweight16(*p++);
37 	}
38 
39 	return sum;
40 }
41 
42 /*
43  * The bitmap buffers stay pinned for the lifetime of the mount and their
44  * in-memory contents are authoritative.  A failed writeback clears
45  * BH_Uptodate on them, so restore it before redirtying the buffer instead
46  * of tripping the warning in mark_buffer_dirty().  The buffer lock keeps
47  * this from racing with a write that is still in flight.
48  */
49 static void minix_mark_bitmap_dirty(struct buffer_head *bh)
50 {
51 	lock_buffer(bh);
52 	if (!buffer_uptodate(bh)) {
53 		clear_buffer_write_io_error(bh);
54 		set_buffer_uptodate(bh);
55 	}
56 	mark_buffer_dirty(bh);
57 	unlock_buffer(bh);
58 }
59 
60 void minix_free_block(struct inode *inode, unsigned long block)
61 {
62 	struct super_block *sb = inode->i_sb;
63 	struct minix_sb_info *sbi = minix_sb(sb);
64 	struct buffer_head *bh;
65 	int k = sb->s_blocksize_bits + 3;
66 	unsigned long bit, zone;
67 
68 	if (block < sbi->s_firstdatazone || block >= sbi->s_nzones) {
69 		printk("Trying to free block not in datazone\n");
70 		return;
71 	}
72 	zone = block - sbi->s_firstdatazone + 1;
73 	bit = zone & ((1<<k) - 1);
74 	zone >>= k;
75 	if (zone >= sbi->s_zmap_blocks) {
76 		printk("minix_free_block: nonexistent bitmap buffer\n");
77 		return;
78 	}
79 	bh = sbi->s_zmap[zone];
80 	spin_lock(&bitmap_lock);
81 	if (!minix_test_and_clear_bit(bit, bh->b_data))
82 		printk("minix_free_block (%s:%lu): bit already cleared\n",
83 		       sb->s_id, block);
84 	spin_unlock(&bitmap_lock);
85 	minix_mark_bitmap_dirty(bh);
86 	return;
87 }
88 
89 int minix_new_block(struct inode * inode)
90 {
91 	struct minix_sb_info *sbi = minix_sb(inode->i_sb);
92 	int bits_per_zone = 8 * inode->i_sb->s_blocksize;
93 	int i;
94 
95 	for (i = 0; i < sbi->s_zmap_blocks; i++) {
96 		struct buffer_head *bh = sbi->s_zmap[i];
97 		int j;
98 
99 		spin_lock(&bitmap_lock);
100 		j = minix_find_first_zero_bit(bh->b_data, bits_per_zone);
101 		if (j < bits_per_zone) {
102 			minix_set_bit(j, bh->b_data);
103 			spin_unlock(&bitmap_lock);
104 			minix_mark_bitmap_dirty(bh);
105 			j += i * bits_per_zone + sbi->s_firstdatazone-1;
106 			if (j < sbi->s_firstdatazone || j >= sbi->s_nzones)
107 				break;
108 			return j;
109 		}
110 		spin_unlock(&bitmap_lock);
111 	}
112 	return 0;
113 }
114 
115 unsigned long minix_count_free_blocks(struct super_block *sb)
116 {
117 	struct minix_sb_info *sbi = minix_sb(sb);
118 	u32 bits = sbi->s_nzones - sbi->s_firstdatazone + 1;
119 
120 	return (count_free(sbi->s_zmap, sb->s_blocksize, bits)
121 		<< sbi->s_log_zone_size);
122 }
123 
124 struct minix_inode *
125 minix_V1_raw_inode(struct super_block *sb, ino_t ino, struct buffer_head **bh)
126 {
127 	int block;
128 	struct minix_sb_info *sbi = minix_sb(sb);
129 	struct minix_inode *p;
130 
131 	if (!ino || ino > sbi->s_ninodes) {
132 		printk("Bad inode number on dev %s: %ld is out of range\n",
133 		       sb->s_id, (long)ino);
134 		return NULL;
135 	}
136 	ino--;
137 	block = 2 + sbi->s_imap_blocks + sbi->s_zmap_blocks +
138 		 ino / MINIX_INODES_PER_BLOCK;
139 	*bh = sb_bread(sb, block);
140 	if (!*bh) {
141 		printk("Unable to read inode block\n");
142 		return NULL;
143 	}
144 	p = (void *)(*bh)->b_data;
145 	return p + ino % MINIX_INODES_PER_BLOCK;
146 }
147 
148 struct minix2_inode *
149 minix_V2_raw_inode(struct super_block *sb, ino_t ino, struct buffer_head **bh)
150 {
151 	int block;
152 	struct minix_sb_info *sbi = minix_sb(sb);
153 	struct minix2_inode *p;
154 	int minix2_inodes_per_block = sb->s_blocksize / sizeof(struct minix2_inode);
155 
156 	*bh = NULL;
157 	if (!ino || ino > sbi->s_ninodes) {
158 		printk("Bad inode number on dev %s: %ld is out of range\n",
159 		       sb->s_id, (long)ino);
160 		return NULL;
161 	}
162 	ino--;
163 	block = 2 + sbi->s_imap_blocks + sbi->s_zmap_blocks +
164 		 ino / minix2_inodes_per_block;
165 	*bh = sb_bread(sb, block);
166 	if (!*bh) {
167 		printk("Unable to read inode block\n");
168 		return NULL;
169 	}
170 	p = (void *)(*bh)->b_data;
171 	return p + ino % minix2_inodes_per_block;
172 }
173 
174 /* Clear the link count and mode of a deleted inode on disk. */
175 
176 static void minix_clear_inode(struct inode *inode)
177 {
178 	struct buffer_head *bh = NULL;
179 
180 	if (INODE_VERSION(inode) == MINIX_V1) {
181 		struct minix_inode *raw_inode;
182 		raw_inode = minix_V1_raw_inode(inode->i_sb, inode->i_ino, &bh);
183 		if (raw_inode) {
184 			raw_inode->i_nlinks = 0;
185 			raw_inode->i_mode = 0;
186 		}
187 	} else {
188 		struct minix2_inode *raw_inode;
189 		raw_inode = minix_V2_raw_inode(inode->i_sb, inode->i_ino, &bh);
190 		if (raw_inode) {
191 			raw_inode->i_nlinks = 0;
192 			raw_inode->i_mode = 0;
193 		}
194 	}
195 	if (bh) {
196 		mark_buffer_dirty(bh);
197 		brelse (bh);
198 	}
199 }
200 
201 void minix_free_inode(struct inode * inode)
202 {
203 	struct super_block *sb = inode->i_sb;
204 	struct minix_sb_info *sbi = minix_sb(inode->i_sb);
205 	struct buffer_head *bh;
206 	int k = sb->s_blocksize_bits + 3;
207 	unsigned long ino, bit;
208 
209 	ino = inode->i_ino;
210 	if (ino < 1 || ino > sbi->s_ninodes) {
211 		printk("minix_free_inode: inode 0 or nonexistent inode\n");
212 		return;
213 	}
214 	bit = ino & ((1<<k) - 1);
215 	ino >>= k;
216 	if (ino >= sbi->s_imap_blocks) {
217 		printk("minix_free_inode: nonexistent imap in superblock\n");
218 		return;
219 	}
220 
221 	minix_clear_inode(inode);	/* clear on-disk copy */
222 
223 	bh = sbi->s_imap[ino];
224 	spin_lock(&bitmap_lock);
225 	if (!minix_test_and_clear_bit(bit, bh->b_data))
226 		printk("minix_free_inode: bit %lu already cleared\n", bit);
227 	spin_unlock(&bitmap_lock);
228 	minix_mark_bitmap_dirty(bh);
229 }
230 
231 struct inode *minix_new_inode(const struct inode *dir, umode_t mode)
232 {
233 	struct super_block *sb = dir->i_sb;
234 	struct minix_sb_info *sbi = minix_sb(sb);
235 	struct inode *inode = new_inode(sb);
236 	struct buffer_head * bh;
237 	int bits_per_zone = 8 * sb->s_blocksize;
238 	unsigned long j;
239 	int i;
240 
241 	if (!inode)
242 		return ERR_PTR(-ENOMEM);
243 	j = bits_per_zone;
244 	bh = NULL;
245 	spin_lock(&bitmap_lock);
246 	for (i = 0; i < sbi->s_imap_blocks; i++) {
247 		bh = sbi->s_imap[i];
248 		j = minix_find_first_zero_bit(bh->b_data, bits_per_zone);
249 		if (j < bits_per_zone)
250 			break;
251 	}
252 	if (!bh || j >= bits_per_zone) {
253 		spin_unlock(&bitmap_lock);
254 		iput(inode);
255 		return ERR_PTR(-ENOSPC);
256 	}
257 	if (minix_test_and_set_bit(j, bh->b_data)) {	/* shouldn't happen */
258 		spin_unlock(&bitmap_lock);
259 		printk("minix_new_inode: bit already set\n");
260 		iput(inode);
261 		return ERR_PTR(-ENOSPC);
262 	}
263 	spin_unlock(&bitmap_lock);
264 	minix_mark_bitmap_dirty(bh);
265 	j += i * bits_per_zone;
266 	if (!j || j > sbi->s_ninodes) {
267 		iput(inode);
268 		return ERR_PTR(-EFSCORRUPTED);
269 	}
270 	inode_init_owner(&nop_mnt_idmap, inode, dir, mode);
271 	inode->i_ino = j;
272 	simple_inode_init_ts(inode);
273 	inode->i_blocks = 0;
274 	memset(&minix_i(inode)->u, 0, sizeof(minix_i(inode)->u));
275 	insert_inode_hash(inode);
276 	mark_inode_dirty(inode);
277 
278 	return inode;
279 }
280 
281 unsigned long minix_count_free_inodes(struct super_block *sb)
282 {
283 	struct minix_sb_info *sbi = minix_sb(sb);
284 	u32 bits = sbi->s_ninodes + 1;
285 
286 	return count_free(sbi->s_imap, sb->s_blocksize, bits);
287 }
288