blob: 9115948c624ee43fff95c56f9f3da71ff7ef8bcf [file] [log] [blame]
Greg Kroah-Hartmanb2441312017-11-01 15:07:57 +01001// SPDX-License-Identifier: GPL-2.0
Linus Torvalds1da177e2005-04-16 15:20:36 -07002/*
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"
Linus Torvalds1da177e2005-04-16 15:20:36 -070016#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
Al Virocc467592009-06-16 23:47:45 -040020static DEFINE_SPINLOCK(bitmap_lock);
21
Al Virof1fd3062011-08-26 22:38:50 -040022/*
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 */
27static __u32 count_free(struct buffer_head *map[], unsigned blocksize, __u32 numbits)
Linus Torvalds1da177e2005-04-16 15:20:36 -070028{
Al Virof1fd3062011-08-26 22:38:50 -040029 __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++);
Linus Torvalds1da177e2005-04-16 15:20:36 -070037 }
38
Al Virof1fd3062011-08-26 22:38:50 -040039 return sum;
Linus Torvalds1da177e2005-04-16 15:20:36 -070040}
41
Andries Brouwer939b00d2007-02-12 00:52:49 -080042void minix_free_block(struct inode *inode, unsigned long block)
Linus Torvalds1da177e2005-04-16 15:20:36 -070043{
Andries Brouwer939b00d2007-02-12 00:52:49 -080044 struct super_block *sb = inode->i_sb;
45 struct minix_sb_info *sbi = minix_sb(sb);
46 struct buffer_head *bh;
47 int k = sb->s_blocksize_bits + 3;
48 unsigned long bit, zone;
Linus Torvalds1da177e2005-04-16 15:20:36 -070049
50 if (block < sbi->s_firstdatazone || block >= sbi->s_nzones) {
Denis Vlasenko11b84482006-03-25 03:07:42 -080051 printk("Trying to free block not in datazone\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -070052 return;
53 }
54 zone = block - sbi->s_firstdatazone + 1;
Andries Brouwer939b00d2007-02-12 00:52:49 -080055 bit = zone & ((1<<k) - 1);
56 zone >>= k;
Linus Torvalds1da177e2005-04-16 15:20:36 -070057 if (zone >= sbi->s_zmap_blocks) {
58 printk("minix_free_block: nonexistent bitmap buffer\n");
59 return;
60 }
61 bh = sbi->s_zmap[zone];
Al Virocc467592009-06-16 23:47:45 -040062 spin_lock(&bitmap_lock);
Andries Brouwer939b00d2007-02-12 00:52:49 -080063 if (!minix_test_and_clear_bit(bit, bh->b_data))
64 printk("minix_free_block (%s:%lu): bit already cleared\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -070065 sb->s_id, block);
Al Virocc467592009-06-16 23:47:45 -040066 spin_unlock(&bitmap_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070067 mark_buffer_dirty(bh);
68 return;
69}
70
71int minix_new_block(struct inode * inode)
72{
73 struct minix_sb_info *sbi = minix_sb(inode->i_sb);
Andries Brouwer939b00d2007-02-12 00:52:49 -080074 int bits_per_zone = 8 * inode->i_sb->s_blocksize;
Linus Torvalds1da177e2005-04-16 15:20:36 -070075 int i;
76
77 for (i = 0; i < sbi->s_zmap_blocks; i++) {
78 struct buffer_head *bh = sbi->s_zmap[i];
79 int j;
80
Al Virocc467592009-06-16 23:47:45 -040081 spin_lock(&bitmap_lock);
Andries Brouwer939b00d2007-02-12 00:52:49 -080082 j = minix_find_first_zero_bit(bh->b_data, bits_per_zone);
83 if (j < bits_per_zone) {
84 minix_set_bit(j, bh->b_data);
Al Virocc467592009-06-16 23:47:45 -040085 spin_unlock(&bitmap_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070086 mark_buffer_dirty(bh);
Andries Brouwer939b00d2007-02-12 00:52:49 -080087 j += i * bits_per_zone + sbi->s_firstdatazone-1;
Linus Torvalds1da177e2005-04-16 15:20:36 -070088 if (j < sbi->s_firstdatazone || j >= sbi->s_nzones)
89 break;
90 return j;
91 }
Al Virocc467592009-06-16 23:47:45 -040092 spin_unlock(&bitmap_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070093 }
94 return 0;
95}
96
Josh Boyer016e8d42011-08-19 14:50:26 -040097unsigned long minix_count_free_blocks(struct super_block *sb)
Linus Torvalds1da177e2005-04-16 15:20:36 -070098{
Josh Boyer016e8d42011-08-19 14:50:26 -040099 struct minix_sb_info *sbi = minix_sb(sb);
Qi Yong6d6747f2014-08-08 14:20:29 -0700100 u32 bits = sbi->s_nzones - sbi->s_firstdatazone + 1;
Josh Boyer016e8d42011-08-19 14:50:26 -0400101
102 return (count_free(sbi->s_zmap, sb->s_blocksize, bits)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700103 << sbi->s_log_zone_size);
104}
105
106struct minix_inode *
107minix_V1_raw_inode(struct super_block *sb, ino_t ino, struct buffer_head **bh)
108{
109 int block;
110 struct minix_sb_info *sbi = minix_sb(sb);
111 struct minix_inode *p;
112
113 if (!ino || ino > sbi->s_ninodes) {
114 printk("Bad inode number on dev %s: %ld is out of range\n",
115 sb->s_id, (long)ino);
116 return NULL;
117 }
118 ino--;
119 block = 2 + sbi->s_imap_blocks + sbi->s_zmap_blocks +
120 ino / MINIX_INODES_PER_BLOCK;
121 *bh = sb_bread(sb, block);
122 if (!*bh) {
Denis Vlasenko11b84482006-03-25 03:07:42 -0800123 printk("Unable to read inode block\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124 return NULL;
125 }
126 p = (void *)(*bh)->b_data;
127 return p + ino % MINIX_INODES_PER_BLOCK;
128}
129
130struct minix2_inode *
131minix_V2_raw_inode(struct super_block *sb, ino_t ino, struct buffer_head **bh)
132{
133 int block;
134 struct minix_sb_info *sbi = minix_sb(sb);
135 struct minix2_inode *p;
Andries Brouwer939b00d2007-02-12 00:52:49 -0800136 int minix2_inodes_per_block = sb->s_blocksize / sizeof(struct minix2_inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137
138 *bh = NULL;
139 if (!ino || ino > sbi->s_ninodes) {
140 printk("Bad inode number on dev %s: %ld is out of range\n",
141 sb->s_id, (long)ino);
142 return NULL;
143 }
144 ino--;
145 block = 2 + sbi->s_imap_blocks + sbi->s_zmap_blocks +
Andries Brouwer939b00d2007-02-12 00:52:49 -0800146 ino / minix2_inodes_per_block;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700147 *bh = sb_bread(sb, block);
148 if (!*bh) {
Denis Vlasenko11b84482006-03-25 03:07:42 -0800149 printk("Unable to read inode block\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700150 return NULL;
151 }
152 p = (void *)(*bh)->b_data;
Andries Brouwer939b00d2007-02-12 00:52:49 -0800153 return p + ino % minix2_inodes_per_block;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700154}
155
156/* Clear the link count and mode of a deleted inode on disk. */
157
158static void minix_clear_inode(struct inode *inode)
159{
160 struct buffer_head *bh = NULL;
161
162 if (INODE_VERSION(inode) == MINIX_V1) {
163 struct minix_inode *raw_inode;
164 raw_inode = minix_V1_raw_inode(inode->i_sb, inode->i_ino, &bh);
165 if (raw_inode) {
166 raw_inode->i_nlinks = 0;
167 raw_inode->i_mode = 0;
168 }
169 } else {
170 struct minix2_inode *raw_inode;
171 raw_inode = minix_V2_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 }
177 if (bh) {
178 mark_buffer_dirty(bh);
179 brelse (bh);
180 }
181}
182
183void minix_free_inode(struct inode * inode)
184{
Andries Brouwer939b00d2007-02-12 00:52:49 -0800185 struct super_block *sb = inode->i_sb;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186 struct minix_sb_info *sbi = minix_sb(inode->i_sb);
Andries Brouwer939b00d2007-02-12 00:52:49 -0800187 struct buffer_head *bh;
188 int k = sb->s_blocksize_bits + 3;
189 unsigned long ino, bit;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190
191 ino = inode->i_ino;
192 if (ino < 1 || ino > sbi->s_ninodes) {
193 printk("minix_free_inode: inode 0 or nonexistent inode\n");
Al Viro5ccb4a72010-06-04 22:27:38 -0400194 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700195 }
Andries Brouwer939b00d2007-02-12 00:52:49 -0800196 bit = ino & ((1<<k) - 1);
197 ino >>= k;
198 if (ino >= sbi->s_imap_blocks) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199 printk("minix_free_inode: nonexistent imap in superblock\n");
Al Viro5ccb4a72010-06-04 22:27:38 -0400200 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700201 }
202
203 minix_clear_inode(inode); /* clear on-disk copy */
204
Andries Brouwer939b00d2007-02-12 00:52:49 -0800205 bh = sbi->s_imap[ino];
Al Virocc467592009-06-16 23:47:45 -0400206 spin_lock(&bitmap_lock);
Andries Brouwer939b00d2007-02-12 00:52:49 -0800207 if (!minix_test_and_clear_bit(bit, bh->b_data))
208 printk("minix_free_inode: bit %lu already cleared\n", bit);
Al Virocc467592009-06-16 23:47:45 -0400209 spin_unlock(&bitmap_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700210 mark_buffer_dirty(bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700211}
212
Al Viro4f45ba3d2011-07-26 02:48:39 -0400213struct inode *minix_new_inode(const struct inode *dir, umode_t mode, int *error)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700214{
215 struct super_block *sb = dir->i_sb;
216 struct minix_sb_info *sbi = minix_sb(sb);
217 struct inode *inode = new_inode(sb);
218 struct buffer_head * bh;
Andries Brouwer939b00d2007-02-12 00:52:49 -0800219 int bits_per_zone = 8 * sb->s_blocksize;
220 unsigned long j;
221 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700222
223 if (!inode) {
224 *error = -ENOMEM;
225 return NULL;
226 }
Andries Brouwer939b00d2007-02-12 00:52:49 -0800227 j = bits_per_zone;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700228 bh = NULL;
229 *error = -ENOSPC;
Al Virocc467592009-06-16 23:47:45 -0400230 spin_lock(&bitmap_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700231 for (i = 0; i < sbi->s_imap_blocks; i++) {
232 bh = sbi->s_imap[i];
Andries Brouwer939b00d2007-02-12 00:52:49 -0800233 j = minix_find_first_zero_bit(bh->b_data, bits_per_zone);
234 if (j < bits_per_zone)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700235 break;
236 }
Andries Brouwer939b00d2007-02-12 00:52:49 -0800237 if (!bh || j >= bits_per_zone) {
Al Virocc467592009-06-16 23:47:45 -0400238 spin_unlock(&bitmap_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700239 iput(inode);
240 return NULL;
241 }
Andries Brouwer939b00d2007-02-12 00:52:49 -0800242 if (minix_test_and_set_bit(j, bh->b_data)) { /* shouldn't happen */
Al Virocc467592009-06-16 23:47:45 -0400243 spin_unlock(&bitmap_lock);
Andries Brouwer939b00d2007-02-12 00:52:49 -0800244 printk("minix_new_inode: bit already set\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700245 iput(inode);
246 return NULL;
247 }
Al Virocc467592009-06-16 23:47:45 -0400248 spin_unlock(&bitmap_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700249 mark_buffer_dirty(bh);
Andries Brouwer939b00d2007-02-12 00:52:49 -0800250 j += i * bits_per_zone;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700251 if (!j || j > sbi->s_ninodes) {
252 iput(inode);
253 return NULL;
254 }
Christian Brauner21cb47b2021-01-21 14:19:25 +0100255 inode_init_owner(&init_user_ns, inode, dir, mode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700256 inode->i_ino = j;
Deepa Dinamani02027d42016-09-14 07:48:05 -0700257 inode->i_mtime = inode->i_atime = inode->i_ctime = current_time(inode);
Theodore Ts'oba52de12006-09-27 01:50:49 -0700258 inode->i_blocks = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700259 memset(&minix_i(inode)->u, 0, sizeof(minix_i(inode)->u));
260 insert_inode_hash(inode);
261 mark_inode_dirty(inode);
262
263 *error = 0;
264 return inode;
265}
266
Josh Boyer016e8d42011-08-19 14:50:26 -0400267unsigned long minix_count_free_inodes(struct super_block *sb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700268{
Josh Boyer016e8d42011-08-19 14:50:26 -0400269 struct minix_sb_info *sbi = minix_sb(sb);
270 u32 bits = sbi->s_ninodes + 1;
271
272 return count_free(sbi->s_imap, sb->s_blocksize, bits);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700273}