2 * linux/fs/sysv/balloc.c
5 * Copyright (C) 1991, 1992 Linus Torvalds
8 * Copyright (C) 1992 Remy Card (card@masi.ibp.fr)
11 * Copyright (C) 1992 Doug Evans
14 * Copyright (C) 1993 Pascal Haible, Bruno Haible
17 * Copyright (C) 1993 Bruno Haible
19 * This file contains code for allocating/freeing blocks.
23 #include <linux/sysv_fs.h>
24 #include <linux/locks.h>
26 /* We don't trust the value of
27 sb->sv_sbd2->s_tfree = *sb->sv_free_blocks
28 but we nevertheless keep it up to date. */
30 static inline u32 *get_chunk(struct super_block *sb, struct buffer_head *bh)
32 char *bh_data = bh->b_data;
34 if (sb->sv_type == FSTYPE_SYSV4)
35 return (u32*)(bh_data+4);
37 return (u32*)(bh_data+2);
40 /* NOTE NOTE NOTE: nr is a block number _as_ _stored_ _on_ _disk_ */
42 void sysv_free_block(struct super_block * sb, u32 nr)
44 struct buffer_head * bh;
45 u32 *blocks = sb->sv_bcache;
47 unsigned block = fs32_to_cpu(sb, nr);
50 * This code does not work at all for AFS (it has a bitmap
51 * free list). As AFS is supposed to be read-only no one
52 * should call this for an AFS filesystem anyway...
54 if (sb->sv_type == FSTYPE_AFS)
57 if (block < sb->sv_firstdatazone || block >= sb->sv_nzones) {
58 printk("sysv_free_block: trying to free block not in datazone\n");
63 count = fs16_to_cpu(sb, *sb->sv_bcache_count);
65 if (count > sb->sv_flc_size) {
66 printk("sysv_free_block: flc_count > flc_size\n");
70 /* If the free list head in super-block is full, it is copied
71 * into this block being freed, ditto if it's completely empty
72 * (applies only on Coherent).
74 if (count == sb->sv_flc_size || count == 0) {
75 block += sb->sv_block_base;
76 bh = sb_getblk(sb, block);
78 printk("sysv_free_block: getblk() failed\n");
82 memset(bh->b_data, 0, sb->s_blocksize);
83 *(u16*)bh->b_data = cpu_to_fs16(sb, count);
84 memcpy(get_chunk(sb,bh), blocks, count * sizeof(sysv_zone_t));
85 mark_buffer_dirty(bh);
86 mark_buffer_uptodate(bh, 1);
90 sb->sv_bcache[count++] = nr;
92 *sb->sv_bcache_count = cpu_to_fs16(sb, count);
93 fs32_add(sb, sb->sv_free_blocks, 1);
98 u32 sysv_new_block(struct super_block * sb)
102 struct buffer_head * bh;
106 count = fs16_to_cpu(sb, *sb->sv_bcache_count);
108 if (count == 0) /* Applies only to Coherent FS */
110 nr = sb->sv_bcache[--count];
111 if (nr == 0) /* Applies only to Xenix FS, SystemV FS */
114 block = fs32_to_cpu(sb, nr);
116 *sb->sv_bcache_count = cpu_to_fs16(sb, count);
118 if (block < sb->sv_firstdatazone || block >= sb->sv_nzones) {
119 printk("sysv_new_block: new block %d is not in data zone\n",
124 if (count == 0) { /* the last block continues the free list */
127 block += sb->sv_block_base;
128 if (!(bh = sb_bread(sb, block))) {
129 printk("sysv_new_block: cannot read free-list block\n");
130 /* retry this same block next time */
131 *sb->sv_bcache_count = cpu_to_fs16(sb, 1);
134 count = fs16_to_cpu(sb, *(u16*)bh->b_data);
135 if (count > sb->sv_flc_size) {
136 printk("sysv_new_block: free-list block with >flc_size entries\n");
140 *sb->sv_bcache_count = cpu_to_fs16(sb, count);
141 memcpy(sb->sv_bcache, get_chunk(sb, bh),
142 count * sizeof(sysv_zone_t));
145 /* Now the free list head in the superblock is valid again. */
146 fs32_add(sb, sb->sv_free_blocks, -1);
156 unsigned long sysv_count_free_blocks(struct super_block * sb)
160 struct buffer_head * bh = NULL;
166 * This code does not work at all for AFS (it has a bitmap
167 * free list). As AFS is supposed to be read-only we just
168 * lie and say it has no free block at all.
170 if (sb->sv_type == FSTYPE_AFS)
174 sb_count = fs32_to_cpu(sb, *sb->sv_free_blocks);
179 /* this causes a lot of disk traffic ... */
181 n = fs16_to_cpu(sb, *sb->sv_bcache_count);
182 blocks = sb->sv_bcache;
184 if (n > sb->sv_flc_size)
187 while (n && (block = blocks[--n]) != 0)
192 block = fs32_to_cpu(sb, block);
196 if (block < sb->sv_firstdatazone || block >= sb->sv_nzones)
198 block += sb->sv_block_base;
199 bh = sb_bread(sb, block);
202 n = fs16_to_cpu(sb, *(u16*)bh->b_data);
203 blocks = get_chunk(sb, bh);
207 if (count != sb_count)
214 printk("sysv_count_free_blocks: new block %d is not in data zone\n",
218 printk("sysv_count_free_blocks: cannot read free-list block\n");
221 printk("sysv_count_free_blocks: >flc_size entries in free-list block\n");
228 printk("sysv_count_free_blocks: free block count was %d, "
229 "correcting to %d\n", sb_count, count);
230 if (!(sb->s_flags & MS_RDONLY)) {
231 *sb->sv_free_blocks = cpu_to_fs32(sb, count);