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.
22 #include <linux/buffer_head.h>
23 #include <linux/string.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 sysv_zone_t
*get_chunk(struct super_block
*sb
, struct buffer_head
*bh
)
32 char *bh_data
= bh
->b_data
;
34 if (SYSV_SB(sb
)->s_type
== FSTYPE_SYSV4
)
35 return (sysv_zone_t
*)(bh_data
+4);
37 return (sysv_zone_t
*)(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
, sysv_zone_t nr
)
44 struct sysv_sb_info
* sbi
= SYSV_SB(sb
);
45 struct buffer_head
* bh
;
46 sysv_zone_t
*blocks
= sbi
->s_bcache
;
48 unsigned block
= fs32_to_cpu(sbi
, nr
);
51 * This code does not work at all for AFS (it has a bitmap
52 * free list). As AFS is supposed to be read-only no one
53 * should call this for an AFS filesystem anyway...
55 if (sbi
->s_type
== FSTYPE_AFS
)
58 if (block
< sbi
->s_firstdatazone
|| block
>= sbi
->s_nzones
) {
59 printk("sysv_free_block: trying to free block not in datazone\n");
64 count
= fs16_to_cpu(sbi
, *sbi
->s_bcache_count
);
66 if (count
> sbi
->s_flc_size
) {
67 printk("sysv_free_block: flc_count > flc_size\n");
71 /* If the free list head in super-block is full, it is copied
72 * into this block being freed, ditto if it's completely empty
73 * (applies only on Coherent).
75 if (count
== sbi
->s_flc_size
|| count
== 0) {
76 block
+= sbi
->s_block_base
;
77 bh
= sb_getblk(sb
, block
);
79 printk("sysv_free_block: getblk() failed\n");
83 memset(bh
->b_data
, 0, sb
->s_blocksize
);
84 *(__fs16
*)bh
->b_data
= cpu_to_fs16(sbi
, count
);
85 memcpy(get_chunk(sb
,bh
), blocks
, count
* sizeof(sysv_zone_t
));
86 mark_buffer_dirty(bh
);
87 set_buffer_uptodate(bh
);
91 sbi
->s_bcache
[count
++] = nr
;
93 *sbi
->s_bcache_count
= cpu_to_fs16(sbi
, count
);
94 fs32_add(sbi
, sbi
->s_free_blocks
, 1);
99 sysv_zone_t
sysv_new_block(struct super_block
* sb
)
101 struct sysv_sb_info
*sbi
= SYSV_SB(sb
);
104 struct buffer_head
* bh
;
108 count
= fs16_to_cpu(sbi
, *sbi
->s_bcache_count
);
110 if (count
== 0) /* Applies only to Coherent FS */
112 nr
= sbi
->s_bcache
[--count
];
113 if (nr
== 0) /* Applies only to Xenix FS, SystemV FS */
116 block
= fs32_to_cpu(sbi
, nr
);
118 *sbi
->s_bcache_count
= cpu_to_fs16(sbi
, count
);
120 if (block
< sbi
->s_firstdatazone
|| block
>= sbi
->s_nzones
) {
121 printk("sysv_new_block: new block %d is not in data zone\n",
126 if (count
== 0) { /* the last block continues the free list */
129 block
+= sbi
->s_block_base
;
130 if (!(bh
= sb_bread(sb
, block
))) {
131 printk("sysv_new_block: cannot read free-list block\n");
132 /* retry this same block next time */
133 *sbi
->s_bcache_count
= cpu_to_fs16(sbi
, 1);
136 count
= fs16_to_cpu(sbi
, *(__fs16
*)bh
->b_data
);
137 if (count
> sbi
->s_flc_size
) {
138 printk("sysv_new_block: free-list block with >flc_size entries\n");
142 *sbi
->s_bcache_count
= cpu_to_fs16(sbi
, count
);
143 memcpy(sbi
->s_bcache
, get_chunk(sb
, bh
),
144 count
* sizeof(sysv_zone_t
));
147 /* Now the free list head in the superblock is valid again. */
148 fs32_add(sbi
, sbi
->s_free_blocks
, -1);
158 unsigned long sysv_count_free_blocks(struct super_block
* sb
)
160 struct sysv_sb_info
* sbi
= SYSV_SB(sb
);
163 struct buffer_head
* bh
= NULL
;
169 * This code does not work at all for AFS (it has a bitmap
170 * free list). As AFS is supposed to be read-only we just
171 * lie and say it has no free block at all.
173 if (sbi
->s_type
== FSTYPE_AFS
)
177 sb_count
= fs32_to_cpu(sbi
, *sbi
->s_free_blocks
);
182 /* this causes a lot of disk traffic ... */
184 n
= fs16_to_cpu(sbi
, *sbi
->s_bcache_count
);
185 blocks
= sbi
->s_bcache
;
188 if (n
> sbi
->s_flc_size
)
191 while (n
&& (zone
= blocks
[--n
]) != 0)
196 block
= fs32_to_cpu(sbi
, zone
);
200 if (block
< sbi
->s_firstdatazone
|| block
>= sbi
->s_nzones
)
202 block
+= sbi
->s_block_base
;
203 bh
= sb_bread(sb
, block
);
206 n
= fs16_to_cpu(sbi
, *(__fs16
*)bh
->b_data
);
207 blocks
= get_chunk(sb
, bh
);
211 if (count
!= sb_count
)
218 printk("sysv_count_free_blocks: new block %d is not in data zone\n",
222 printk("sysv_count_free_blocks: cannot read free-list block\n");
225 printk("sysv_count_free_blocks: >flc_size entries in free-list block\n");
232 printk("sysv_count_free_blocks: free block count was %d, "
233 "correcting to %d\n", sb_count
, count
);
234 if (!(sb
->s_flags
& MS_RDONLY
)) {
235 *sbi
->s_free_blocks
= cpu_to_fs32(sbi
, count
);