1 // SPDX-License-Identifier: GPL-2.0
3 * linux/fs/sysv/balloc.c
6 * Copyright (C) 1991, 1992 Linus Torvalds
9 * Copyright (C) 1992 Remy Card (card@masi.ibp.fr)
12 * Copyright (C) 1992 Doug Evans
15 * Copyright (C) 1993 Pascal Haible, Bruno Haible
18 * Copyright (C) 1993 Bruno Haible
20 * This file contains code for allocating/freeing blocks.
23 #include <linux/buffer_head.h>
24 #include <linux/string.h>
27 /* We don't trust the value of
28 sb->sv_sbd2->s_tfree = *sb->sv_free_blocks
29 but we nevertheless keep it up to date. */
31 static inline sysv_zone_t *get_chunk(struct super_block *sb, struct buffer_head *bh)
33 char *bh_data = bh->b_data;
35 if (SYSV_SB(sb)->s_type == FSTYPE_SYSV4)
36 return (sysv_zone_t*)(bh_data+4);
38 return (sysv_zone_t*)(bh_data+2);
41 /* NOTE NOTE NOTE: nr is a block number _as_ _stored_ _on_ _disk_ */
43 void sysv_free_block(struct super_block * sb, sysv_zone_t nr)
45 struct sysv_sb_info * sbi = SYSV_SB(sb);
46 struct buffer_head * bh;
47 sysv_zone_t *blocks = sbi->s_bcache;
49 unsigned block = fs32_to_cpu(sbi, nr);
52 * This code does not work at all for AFS (it has a bitmap
53 * free list). As AFS is supposed to be read-only no one
54 * should call this for an AFS filesystem anyway...
56 if (sbi->s_type == FSTYPE_AFS)
59 if (block < sbi->s_firstdatazone || block >= sbi->s_nzones) {
60 printk("sysv_free_block: trying to free block not in datazone\n");
64 mutex_lock(&sbi->s_lock);
65 count = fs16_to_cpu(sbi, *sbi->s_bcache_count);
67 if (count > sbi->s_flc_size) {
68 printk("sysv_free_block: flc_count > flc_size\n");
69 mutex_unlock(&sbi->s_lock);
72 /* If the free list head in super-block is full, it is copied
73 * into this block being freed, ditto if it's completely empty
74 * (applies only on Coherent).
76 if (count == sbi->s_flc_size || count == 0) {
77 block += sbi->s_block_base;
78 bh = sb_getblk(sb, block);
80 printk("sysv_free_block: getblk() failed\n");
81 mutex_unlock(&sbi->s_lock);
84 memset(bh->b_data, 0, sb->s_blocksize);
85 *(__fs16*)bh->b_data = cpu_to_fs16(sbi, count);
86 memcpy(get_chunk(sb,bh), blocks, count * sizeof(sysv_zone_t));
87 mark_buffer_dirty(bh);
88 set_buffer_uptodate(bh);
92 sbi->s_bcache[count++] = nr;
94 *sbi->s_bcache_count = cpu_to_fs16(sbi, count);
95 fs32_add(sbi, sbi->s_free_blocks, 1);
97 mutex_unlock(&sbi->s_lock);
100 sysv_zone_t sysv_new_block(struct super_block * sb)
102 struct sysv_sb_info *sbi = SYSV_SB(sb);
105 struct buffer_head * bh;
108 mutex_lock(&sbi->s_lock);
109 count = fs16_to_cpu(sbi, *sbi->s_bcache_count);
111 if (count == 0) /* Applies only to Coherent FS */
113 nr = sbi->s_bcache[--count];
114 if (nr == 0) /* Applies only to Xenix FS, SystemV FS */
117 block = fs32_to_cpu(sbi, nr);
119 *sbi->s_bcache_count = cpu_to_fs16(sbi, count);
121 if (block < sbi->s_firstdatazone || block >= sbi->s_nzones) {
122 printk("sysv_new_block: new block %d is not in data zone\n",
127 if (count == 0) { /* the last block continues the free list */
130 block += sbi->s_block_base;
131 if (!(bh = sb_bread(sb, block))) {
132 printk("sysv_new_block: cannot read free-list block\n");
133 /* retry this same block next time */
134 *sbi->s_bcache_count = cpu_to_fs16(sbi, 1);
137 count = fs16_to_cpu(sbi, *(__fs16*)bh->b_data);
138 if (count > sbi->s_flc_size) {
139 printk("sysv_new_block: free-list block with >flc_size entries\n");
143 *sbi->s_bcache_count = cpu_to_fs16(sbi, count);
144 memcpy(sbi->s_bcache, get_chunk(sb, bh),
145 count * sizeof(sysv_zone_t));
148 /* Now the free list head in the superblock is valid again. */
149 fs32_add(sbi, sbi->s_free_blocks, -1);
151 mutex_unlock(&sbi->s_lock);
155 mutex_unlock(&sbi->s_lock);
159 unsigned long sysv_count_free_blocks(struct super_block * sb)
161 struct sysv_sb_info * sbi = SYSV_SB(sb);
164 struct buffer_head * bh = NULL;
170 * This code does not work at all for AFS (it has a bitmap
171 * free list). As AFS is supposed to be read-only we just
172 * lie and say it has no free block at all.
174 if (sbi->s_type == FSTYPE_AFS)
177 mutex_lock(&sbi->s_lock);
178 sb_count = fs32_to_cpu(sbi, *sbi->s_free_blocks);
183 /* this causes a lot of disk traffic ... */
185 n = fs16_to_cpu(sbi, *sbi->s_bcache_count);
186 blocks = sbi->s_bcache;
189 if (n > sbi->s_flc_size)
192 while (n && (zone = blocks[--n]) != 0)
197 block = fs32_to_cpu(sbi, zone);
201 if (block < sbi->s_firstdatazone || block >= sbi->s_nzones)
203 block += sbi->s_block_base;
204 bh = sb_bread(sb, block);
207 n = fs16_to_cpu(sbi, *(__fs16*)bh->b_data);
208 blocks = get_chunk(sb, bh);
212 if (count != sb_count)
215 mutex_unlock(&sbi->s_lock);
219 printk("sysv_count_free_blocks: new block %d is not in data zone\n",
223 printk("sysv_count_free_blocks: cannot read free-list block\n");
226 printk("sysv_count_free_blocks: >flc_size entries in free-list block\n");
233 printk("sysv_count_free_blocks: free block count was %d, "
234 "correcting to %d\n", sb_count, count);
235 if (!sb_rdonly(sb)) {
236 *sbi->s_free_blocks = cpu_to_fs32(sbi, count);