summaryrefslogtreecommitdiffstats
path: root/fs/minix/bitmap.c
diff options
context:
space:
mode:
authorLinus Torvalds <torvalds@linux-foundation.org>2026-08-23 13:28:57 -0700
committerLinus Torvalds <torvalds@linux-foundation.org>2026-08-23 13:28:57 -0700
commit4352b8aee98005853aa63f57d6377282de17a33f (patch)
tree27614d7a9ef05c32d089004e104364ce42324837 /fs/minix/bitmap.c
downloadlinux-stable-4352b8aee98005853aa63f57d6377282de17a33f.tar.gz
linux-stable-4352b8aee98005853aa63f57d6377282de17a33f.zip
Merge tag 'i3c/for-7.3' of git://git.kernel.org/pub/scm/linux/kernel/git/i3c/linuxgrafted
Pull I3C updates from Alexandre Belloni: "A new driver this cycle, for the AMD AXI I3C controller. There are also new features such as IBI wakeup support, SETAASA device discovery and ACPI support for the the DAA methods, meaning we can now communicate with the SPD devices on DDR5 modules. New driver: - AMD AXI I3C master controller Subsystem: - I3C Common Command Code (CCC) handling improvements, especially around GET CCCs - SETAASA device discovery support - ACPI support for all existing DAA methods like SETDASA, SETNEWDA as well as I2C devices on I3C bus - IBI-based wakeup support Drivers: - dw: SETAASA support - mipi-i3c-hci: advertise IBI wakeup capability, AMD_PT I3C controller support, PIO queue management support for HCI v1.2 - renesas: improve suspend to RAM support, add runtime PM support" * tag 'i3c/for-7.3' of git://git.kernel.org/pub/scm/linux/kernel/git/i3c/linux: (70 commits) i3c: dw: reduce do_daa time if there's no client i3c: mipi-i3c-hci: Add support for AMD_PT I3C controller i3c: mipi-i3c-hci: Add PIO queue management support for HCI v1.2 i3c: mipi-i3c-hci: Fix missing STAT_IBI_STATUS_THLD in PIO mode i3c: dw: rename "pclk" to "apb" to match dt-binding i3c: dw: make struct dw_i3c_cmd smaller i3c: dw: use COMMAND_PORT_TRANSFER_ARG instead of hardcoding i3c: renesas: Don't register devices when ENTDAA times out i3c: master: dw-i3c-master: fix OD timing for first broadcast i3c: mipi-i3c-hci: Advertise IBI wakeup capability i3c: mipi-i3c-hci: Factor out i3c_hci_sysdev() i3c: mipi-i3c-hci-pci: Propagate I3C wakeup requirements to PCI i3c: master: Reject IBI requests from non-IBI-capable devices i3c: master: Add helper to query bus wakeup requirements i3c: master: Report wakeup events for IBIs i3c: master: Support IBI-based wakeup capability i3c: master: Fix potential UAF in i3c_device_match() i3c: master: Fix potential UAF in i3c_device_uevent() i3c: Make dev->desc locking assumptions explicit i3c: master: Fix use-after-free of master->this ...
Diffstat (limited to 'fs/minix/bitmap.c')
-rw-r--r--fs/minix/bitmap.c269
1 files changed, 269 insertions, 0 deletions
diff --git a/fs/minix/bitmap.c b/fs/minix/bitmap.c
new file mode 100644
index 000000000..abec43833
--- /dev/null
+++ b/fs/minix/bitmap.c
@@ -0,0 +1,269 @@
+// SPDX-License-Identifier: GPL-2.0
+/*
+ * linux/fs/minix/bitmap.c
+ *
+ * Copyright (C) 1991, 1992 Linus Torvalds
+ */
+
+/*
+ * Modified for 680x0 by Hamish Macdonald
+ * Fixed for 680x0 by Andreas Schwab
+ */
+
+/* bitmap.c contains the code that handles the inode and block bitmaps */
+
+#include "minix.h"
+#include <linux/buffer_head.h>
+#include <linux/bitops.h>
+#include <linux/sched.h>
+
+static DEFINE_SPINLOCK(bitmap_lock);
+
+/*
+ * bitmap consists of blocks filled with 16bit words
+ * bit set == busy, bit clear == free
+ * endianness is a mess, but for counting zero bits it really doesn't matter...
+ */
+static __u32 count_free(struct buffer_head *map[], unsigned blocksize, __u32 numbits)
+{
+ __u32 sum = 0;
+ unsigned blocks = DIV_ROUND_UP(numbits, blocksize * 8);
+
+ while (blocks--) {
+ unsigned words = blocksize / 2;
+ __u16 *p = (__u16 *)(*map++)->b_data;
+ while (words--)
+ sum += 16 - hweight16(*p++);
+ }
+
+ return sum;
+}
+
+void minix_free_block(struct inode *inode, unsigned long block)
+{
+ struct super_block *sb = inode->i_sb;
+ struct minix_sb_info *sbi = minix_sb(sb);
+ struct buffer_head *bh;
+ int k = sb->s_blocksize_bits + 3;
+ unsigned long bit, zone;
+
+ if (block < sbi->s_firstdatazone || block >= sbi->s_nzones) {
+ printk("Trying to free block not in datazone\n");
+ return;
+ }
+ zone = block - sbi->s_firstdatazone + 1;
+ bit = zone & ((1<<k) - 1);
+ zone >>= k;
+ if (zone >= sbi->s_zmap_blocks) {
+ printk("minix_free_block: nonexistent bitmap buffer\n");
+ return;
+ }
+ bh = sbi->s_zmap[zone];
+ spin_lock(&bitmap_lock);
+ if (!minix_test_and_clear_bit(bit, bh->b_data))
+ printk("minix_free_block (%s:%lu): bit already cleared\n",
+ sb->s_id, block);
+ spin_unlock(&bitmap_lock);
+ mark_buffer_dirty(bh);
+ return;
+}
+
+int minix_new_block(struct inode * inode)
+{
+ struct minix_sb_info *sbi = minix_sb(inode->i_sb);
+ int bits_per_zone = 8 * inode->i_sb->s_blocksize;
+ int i;
+
+ for (i = 0; i < sbi->s_zmap_blocks; i++) {
+ struct buffer_head *bh = sbi->s_zmap[i];
+ int j;
+
+ spin_lock(&bitmap_lock);
+ j = minix_find_first_zero_bit(bh->b_data, bits_per_zone);
+ if (j < bits_per_zone) {
+ minix_set_bit(j, bh->b_data);
+ spin_unlock(&bitmap_lock);
+ mark_buffer_dirty(bh);
+ j += i * bits_per_zone + sbi->s_firstdatazone-1;
+ if (j < sbi->s_firstdatazone || j >= sbi->s_nzones)
+ break;
+ return j;
+ }
+ spin_unlock(&bitmap_lock);
+ }
+ return 0;
+}
+
+unsigned long minix_count_free_blocks(struct super_block *sb)
+{
+ struct minix_sb_info *sbi = minix_sb(sb);
+ u32 bits = sbi->s_nzones - sbi->s_firstdatazone + 1;
+
+ return (count_free(sbi->s_zmap, sb->s_blocksize, bits)
+ << sbi->s_log_zone_size);
+}
+
+struct minix_inode *
+minix_V1_raw_inode(struct super_block *sb, ino_t ino, struct buffer_head **bh)
+{
+ int block;
+ struct minix_sb_info *sbi = minix_sb(sb);
+ struct minix_inode *p;
+
+ if (!ino || ino > sbi->s_ninodes) {
+ printk("Bad inode number on dev %s: %ld is out of range\n",
+ sb->s_id, (long)ino);
+ return NULL;
+ }
+ ino--;
+ block = 2 + sbi->s_imap_blocks + sbi->s_zmap_blocks +
+ ino / MINIX_INODES_PER_BLOCK;
+ *bh = sb_bread(sb, block);
+ if (!*bh) {
+ printk("Unable to read inode block\n");
+ return NULL;
+ }
+ p = (void *)(*bh)->b_data;
+ return p + ino % MINIX_INODES_PER_BLOCK;
+}
+
+struct minix2_inode *
+minix_V2_raw_inode(struct super_block *sb, ino_t ino, struct buffer_head **bh)
+{
+ int block;
+ struct minix_sb_info *sbi = minix_sb(sb);
+ struct minix2_inode *p;
+ int minix2_inodes_per_block = sb->s_blocksize / sizeof(struct minix2_inode);
+
+ *bh = NULL;
+ if (!ino || ino > sbi->s_ninodes) {
+ printk("Bad inode number on dev %s: %ld is out of range\n",
+ sb->s_id, (long)ino);
+ return NULL;
+ }
+ ino--;
+ block = 2 + sbi->s_imap_blocks + sbi->s_zmap_blocks +
+ ino / minix2_inodes_per_block;
+ *bh = sb_bread(sb, block);
+ if (!*bh) {
+ printk("Unable to read inode block\n");
+ return NULL;
+ }
+ p = (void *)(*bh)->b_data;
+ return p + ino % minix2_inodes_per_block;
+}
+
+/* Clear the link count and mode of a deleted inode on disk. */
+
+static void minix_clear_inode(struct inode *inode)
+{
+ struct buffer_head *bh = NULL;
+
+ if (INODE_VERSION(inode) == MINIX_V1) {
+ struct minix_inode *raw_inode;
+ raw_inode = minix_V1_raw_inode(inode->i_sb, inode->i_ino, &bh);
+ if (raw_inode) {
+ raw_inode->i_nlinks = 0;
+ raw_inode->i_mode = 0;
+ }
+ } else {
+ struct minix2_inode *raw_inode;
+ raw_inode = minix_V2_raw_inode(inode->i_sb, inode->i_ino, &bh);
+ if (raw_inode) {
+ raw_inode->i_nlinks = 0;
+ raw_inode->i_mode = 0;
+ }
+ }
+ if (bh) {
+ mark_buffer_dirty(bh);
+ brelse (bh);
+ }
+}
+
+void minix_free_inode(struct inode * inode)
+{
+ struct super_block *sb = inode->i_sb;
+ struct minix_sb_info *sbi = minix_sb(inode->i_sb);
+ struct buffer_head *bh;
+ int k = sb->s_blocksize_bits + 3;
+ unsigned long ino, bit;
+
+ ino = inode->i_ino;
+ if (ino < 1 || ino > sbi->s_ninodes) {
+ printk("minix_free_inode: inode 0 or nonexistent inode\n");
+ return;
+ }
+ bit = ino & ((1<<k) - 1);
+ ino >>= k;
+ if (ino >= sbi->s_imap_blocks) {
+ printk("minix_free_inode: nonexistent imap in superblock\n");
+ return;
+ }
+
+ minix_clear_inode(inode); /* clear on-disk copy */
+
+ bh = sbi->s_imap[ino];
+ spin_lock(&bitmap_lock);
+ if (!minix_test_and_clear_bit(bit, bh->b_data))
+ printk("minix_free_inode: bit %lu already cleared\n", bit);
+ spin_unlock(&bitmap_lock);
+ mark_buffer_dirty(bh);
+}
+
+struct inode *minix_new_inode(const struct inode *dir, umode_t mode)
+{
+ struct super_block *sb = dir->i_sb;
+ struct minix_sb_info *sbi = minix_sb(sb);
+ struct inode *inode = new_inode(sb);
+ struct buffer_head * bh;
+ int bits_per_zone = 8 * sb->s_blocksize;
+ unsigned long j;
+ int i;
+
+ if (!inode)
+ return ERR_PTR(-ENOMEM);
+ j = bits_per_zone;
+ bh = NULL;
+ spin_lock(&bitmap_lock);
+ for (i = 0; i < sbi->s_imap_blocks; i++) {
+ bh = sbi->s_imap[i];
+ j = minix_find_first_zero_bit(bh->b_data, bits_per_zone);
+ if (j < bits_per_zone)
+ break;
+ }
+ if (!bh || j >= bits_per_zone) {
+ spin_unlock(&bitmap_lock);
+ iput(inode);
+ return ERR_PTR(-ENOSPC);
+ }
+ if (minix_test_and_set_bit(j, bh->b_data)) { /* shouldn't happen */
+ spin_unlock(&bitmap_lock);
+ printk("minix_new_inode: bit already set\n");
+ iput(inode);
+ return ERR_PTR(-ENOSPC);
+ }
+ spin_unlock(&bitmap_lock);
+ mark_buffer_dirty(bh);
+ j += i * bits_per_zone;
+ if (!j || j > sbi->s_ninodes) {
+ iput(inode);
+ return ERR_PTR(-EFSCORRUPTED);
+ }
+ inode_init_owner(&nop_mnt_idmap, inode, dir, mode);
+ inode->i_ino = j;
+ simple_inode_init_ts(inode);
+ inode->i_blocks = 0;
+ memset(&minix_i(inode)->u, 0, sizeof(minix_i(inode)->u));
+ insert_inode_hash(inode);
+ mark_inode_dirty(inode);
+
+ return inode;
+}
+
+unsigned long minix_count_free_inodes(struct super_block *sb)
+{
+ struct minix_sb_info *sbi = minix_sb(sb);
+ u32 bits = sbi->s_ninodes + 1;
+
+ return count_free(sbi->s_imap, sb->s_blocksize, bits);
+}