xref: /linux/fs/minix/bitmap.c (revision f16d85cd3de9d0c761b74155b4cbe2f20d3dcf80)
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 /* Return the block that holds inode @ino or 0 if @ino is out of range. */
125 int minix_inode_block(struct super_block *sb, ino_t ino)
126 {
127 	struct minix_sb_info *sbi = minix_sb(sb);
128 	int inodes_per_block;
129 
130 	if (!ino || ino > sbi->s_ninodes) {
131 		printk("Bad inode number on dev %s: %ld is out of range\n",
132 		       sb->s_id, (long)ino);
133 		return 0;
134 	}
135 	if (sbi->s_version == MINIX_V1)
136 		inodes_per_block = MINIX_INODES_PER_BLOCK;
137 	else
138 		inodes_per_block = sb->s_blocksize / sizeof(struct minix2_inode);
139 	return 2 + sbi->s_imap_blocks + sbi->s_zmap_blocks +
140 		 (ino - 1) / inodes_per_block;
141 }
142 
143 struct minix_inode *
144 minix_V1_raw_inode(struct super_block *sb, ino_t ino, struct buffer_head **bh)
145 {
146 	int block;
147 	struct minix_inode *p;
148 
149 	block = minix_inode_block(sb, ino);
150 	if (!block)
151 		return NULL;
152 	ino--;
153 	*bh = sb_bread(sb, block);
154 	if (!*bh) {
155 		printk("Unable to read inode block\n");
156 		return NULL;
157 	}
158 	p = (void *)(*bh)->b_data;
159 	return p + ino % MINIX_INODES_PER_BLOCK;
160 }
161 
162 struct minix2_inode *
163 minix_V2_raw_inode(struct super_block *sb, ino_t ino, struct buffer_head **bh)
164 {
165 	int block;
166 	struct minix2_inode *p;
167 	int minix2_inodes_per_block = sb->s_blocksize / sizeof(struct minix2_inode);
168 
169 	*bh = NULL;
170 	block = minix_inode_block(sb, ino);
171 	if (!block)
172 		return NULL;
173 	ino--;
174 	*bh = sb_bread(sb, block);
175 	if (!*bh) {
176 		printk("Unable to read inode block\n");
177 		return NULL;
178 	}
179 	p = (void *)(*bh)->b_data;
180 	return p + ino % minix2_inodes_per_block;
181 }
182 
183 /* Clear the link count and mode of a deleted inode on disk. */
184 
185 static void minix_clear_inode(struct inode *inode)
186 {
187 	struct buffer_head *bh = NULL;
188 
189 	if (INODE_VERSION(inode) == MINIX_V1) {
190 		struct minix_inode *raw_inode;
191 		raw_inode = minix_V1_raw_inode(inode->i_sb, inode->i_ino, &bh);
192 		if (raw_inode) {
193 			raw_inode->i_nlinks = 0;
194 			raw_inode->i_mode = 0;
195 		}
196 	} else {
197 		struct minix2_inode *raw_inode;
198 		raw_inode = minix_V2_raw_inode(inode->i_sb, inode->i_ino, &bh);
199 		if (raw_inode) {
200 			raw_inode->i_nlinks = 0;
201 			raw_inode->i_mode = 0;
202 		}
203 	}
204 	if (bh) {
205 		mark_buffer_dirty(bh);
206 		brelse (bh);
207 	}
208 }
209 
210 void minix_free_inode(struct inode * inode)
211 {
212 	struct super_block *sb = inode->i_sb;
213 	struct minix_sb_info *sbi = minix_sb(inode->i_sb);
214 	struct buffer_head *bh;
215 	int k = sb->s_blocksize_bits + 3;
216 	unsigned long ino, bit;
217 
218 	ino = inode->i_ino;
219 	if (ino < 1 || ino > sbi->s_ninodes) {
220 		printk("minix_free_inode: inode 0 or nonexistent inode\n");
221 		return;
222 	}
223 	bit = ino & ((1<<k) - 1);
224 	ino >>= k;
225 	if (ino >= sbi->s_imap_blocks) {
226 		printk("minix_free_inode: nonexistent imap in superblock\n");
227 		return;
228 	}
229 
230 	minix_clear_inode(inode);	/* clear on-disk copy */
231 
232 	bh = sbi->s_imap[ino];
233 	spin_lock(&bitmap_lock);
234 	if (!minix_test_and_clear_bit(bit, bh->b_data))
235 		printk("minix_free_inode: bit %lu already cleared\n", bit);
236 	spin_unlock(&bitmap_lock);
237 	minix_mark_bitmap_dirty(bh);
238 }
239 
240 struct inode *minix_new_inode(const struct inode *dir, umode_t mode)
241 {
242 	struct super_block *sb = dir->i_sb;
243 	struct minix_sb_info *sbi = minix_sb(sb);
244 	struct inode *inode = new_inode(sb);
245 	struct buffer_head * bh;
246 	int bits_per_zone = 8 * sb->s_blocksize;
247 	unsigned long j;
248 	int i;
249 
250 	if (!inode)
251 		return ERR_PTR(-ENOMEM);
252 	j = bits_per_zone;
253 	bh = NULL;
254 	spin_lock(&bitmap_lock);
255 	for (i = 0; i < sbi->s_imap_blocks; i++) {
256 		bh = sbi->s_imap[i];
257 		j = minix_find_first_zero_bit(bh->b_data, bits_per_zone);
258 		if (j < bits_per_zone)
259 			break;
260 	}
261 	if (!bh || j >= bits_per_zone) {
262 		spin_unlock(&bitmap_lock);
263 		iput(inode);
264 		return ERR_PTR(-ENOSPC);
265 	}
266 	if (minix_test_and_set_bit(j, bh->b_data)) {	/* shouldn't happen */
267 		spin_unlock(&bitmap_lock);
268 		printk("minix_new_inode: bit already set\n");
269 		iput(inode);
270 		return ERR_PTR(-ENOSPC);
271 	}
272 	spin_unlock(&bitmap_lock);
273 	minix_mark_bitmap_dirty(bh);
274 	j += i * bits_per_zone;
275 	if (!j || j > sbi->s_ninodes) {
276 		iput(inode);
277 		return ERR_PTR(-EFSCORRUPTED);
278 	}
279 	inode_init_owner(&nop_mnt_idmap, inode, dir, mode);
280 	inode->i_ino = j;
281 	simple_inode_init_ts(inode);
282 	inode->i_blocks = 0;
283 	memset(&minix_i(inode)->u, 0, sizeof(minix_i(inode)->u));
284 	insert_inode_hash(inode);
285 	mark_inode_dirty(inode);
286 
287 	return inode;
288 }
289 
290 unsigned long minix_count_free_inodes(struct super_block *sb)
291 {
292 	struct minix_sb_info *sbi = minix_sb(sb);
293 	u32 bits = sbi->s_ninodes + 1;
294 
295 	return count_free(sbi->s_imap, sb->s_blocksize, bits);
296 }
297