summaryrefslogtreecommitdiffstats
path: root/fs/sysv/balloc.c
blob: 5fa82929ba0d9aa4809ebef306e05a34fe78b933 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
/*
 *  linux/fs/sysv/balloc.c
 *
 *  minix/bitmap.c
 *  Copyright (C) 1991, 1992  Linus Torvalds
 *
 *  ext/freelists.c
 *  Copyright (C) 1992  Remy Card (card@masi.ibp.fr)
 *
 *  xenix/alloc.c
 *  Copyright (C) 1992  Doug Evans
 *
 *  coh/alloc.c
 *  Copyright (C) 1993  Pascal Haible, Bruno Haible
 *
 *  sysv/balloc.c
 *  Copyright (C) 1993  Bruno Haible
 *
 *  This file contains code for allocating/freeing blocks.
 */

#include <linux/kernel.h>
#include <linux/fs.h>
#include <linux/sysv_fs.h>
#include <linux/string.h>
#include <linux/locks.h>

/* We don't trust the value of
   sb->sv_sbd2->s_tfree = *sb->sv_sb_total_free_blocks
   but we nevertheless keep it up to date. */

void sysv_free_block(struct super_block * sb, unsigned int block)
{
	struct buffer_head * bh;
	char * bh_data;

	if (!sb) {
		printk("sysv_free_block: trying to free block on nonexistent device\n");
		return;
	}
	if (block < sb->sv_firstdatazone || block >= sb->sv_nzones) {
		printk("sysv_free_block: trying to free block not in datazone\n");
		return;
	}
	lock_super(sb);
	if (*sb->sv_sb_flc_count > sb->sv_flc_size) {
		printk("sysv_free_block: flc_count > flc_size\n");
		unlock_super(sb);
		return;
	}
	/* If the free list head in super-block is full, it is copied
	 * into this block being freed:
	 */
	if (*sb->sv_sb_flc_count == sb->sv_flc_size) {
		unsigned short * flc_count;
		unsigned long * flc_blocks;

		bh = sv_getblk(sb, sb->s_dev, block);
		if (!bh) {
			printk("sysv_free_block: getblk() failed\n");
			unlock_super(sb);
			return;
		}
		bh_data = bh->b_data;
		switch (sb->sv_type) {
			case FSTYPE_XENIX:
				flc_count = &((struct xenix_freelist_chunk *) bh_data)->fl_nfree;
				flc_blocks = &((struct xenix_freelist_chunk *) bh_data)->fl_free[0];
				break;
			case FSTYPE_SYSV4:
				flc_count = &((struct sysv4_freelist_chunk *) bh_data)->fl_nfree;
				flc_blocks = &((struct sysv4_freelist_chunk *) bh_data)->fl_free[0];
				break;
			case FSTYPE_SYSV2:
				flc_count = &((struct sysv2_freelist_chunk *) bh_data)->fl_nfree;
				flc_blocks = &((struct sysv2_freelist_chunk *) bh_data)->fl_free[0];
				break;
			case FSTYPE_COH:
				flc_count = &((struct coh_freelist_chunk *) bh_data)->fl_nfree;
				flc_blocks = &((struct coh_freelist_chunk *) bh_data)->fl_free[0];
				break;
			default: panic("sysv_free_block: invalid fs type\n");
		}
		*flc_count = *sb->sv_sb_flc_count; /* = sb->sv_flc_size */
		memcpy(flc_blocks, sb->sv_sb_flc_blocks, *flc_count * sizeof(sysv_zone_t));
		mark_buffer_dirty(bh, 1);
		mark_buffer_uptodate(bh, 1);
		brelse(bh);
		*sb->sv_sb_flc_count = 0;
	} else
	/* If the free list head in super-block is empty, create a new head
	 * in this block being freed:
	 */
	if (*sb->sv_sb_flc_count == 0) { /* Applies only to Coherent FS */
		bh = sv_getblk(sb, sb->s_dev, block);
		if (!bh) {
			printk("sysv_free_block: getblk() failed\n");
			unlock_super(sb);
			return;
		}
		memset(bh->b_data, 0, sb->sv_block_size);
		/* this implies ((struct ..._freelist_chunk *) bh->b_data)->flc_count = 0; */
		mark_buffer_dirty(bh, 1);
		mark_buffer_uptodate(bh, 1);
		brelse(bh);
		/* still *sb->sv_sb_flc_count = 0 */
	} else {
		/* Throw away block's contents */
		bh = sv_get_hash_table(sb, sb->s_dev, block);
		if (bh)
			mark_buffer_clean(bh);
		brelse(bh);
	}
	if (sb->sv_convert)
		block = to_coh_ulong(block);
	sb->sv_sb_flc_blocks[(*sb->sv_sb_flc_count)++] = block;
	if (sb->sv_convert)
		*sb->sv_sb_total_free_blocks =
		  to_coh_ulong(from_coh_ulong(*sb->sv_sb_total_free_blocks) + 1);
	else
		*sb->sv_sb_total_free_blocks = *sb->sv_sb_total_free_blocks + 1;
	mark_buffer_dirty(sb->sv_bh1, 1); /* super-block has been modified */
	if (sb->sv_bh1 != sb->sv_bh2) mark_buffer_dirty(sb->sv_bh2, 1);
	sb->s_dirt = 1; /* and needs time stamp */
	unlock_super(sb);
}

int sysv_new_block(struct super_block * sb)
{
	unsigned int block;
	struct buffer_head * bh;
	char * bh_data;

	if (!sb) {
		printk("sysv_new_block: trying to get new block from nonexistent device\n");
		return 0;
	}
	lock_super(sb);
	if (*sb->sv_sb_flc_count == 0) { /* Applies only to Coherent FS */
		unlock_super(sb);
		return 0;		/* no blocks available */
	}
	block = sb->sv_sb_flc_blocks[(*sb->sv_sb_flc_count)-1];
	if (sb->sv_convert)
		block = from_coh_ulong(block);
	if (block == 0) { /* Applies only to Xenix FS, SystemV FS */
		unlock_super(sb);
		return 0;		/* no blocks available */
	}
	(*sb->sv_sb_flc_count)--;
	if (block < sb->sv_firstdatazone || block >= sb->sv_nzones) {
		printk("sysv_new_block: new block %d is not in data zone\n",block);
		unlock_super(sb);
		return 0;
	}
	if (*sb->sv_sb_flc_count == 0) { /* the last block continues the free list */
		unsigned short * flc_count;
		unsigned long * flc_blocks;

		if (!(bh = sv_bread(sb, sb->s_dev, block))) {
			printk("sysv_new_block: cannot read free-list block\n");
			/* retry this same block next time */
			(*sb->sv_sb_flc_count)++;
			unlock_super(sb);
			return 0;
		}
		bh_data = bh->b_data;
		switch (sb->sv_type) {
			case FSTYPE_XENIX:
				flc_count = &((struct xenix_freelist_chunk *) bh_data)->fl_nfree;
				flc_blocks = &((struct xenix_freelist_chunk *) bh_data)->fl_free[0];
				break;
			case FSTYPE_SYSV4:
				flc_count = &((struct sysv4_freelist_chunk *) bh_data)->fl_nfree;
				flc_blocks = &((struct sysv4_freelist_chunk *) bh_data)->fl_free[0];
				break;
			case FSTYPE_SYSV2:
				flc_count = &((struct sysv2_freelist_chunk *) bh_data)->fl_nfree;
				flc_blocks = &((struct sysv2_freelist_chunk *) bh_data)->fl_free[0];
				break;
			case FSTYPE_COH:
				flc_count = &((struct coh_freelist_chunk *) bh_data)->fl_nfree;
				flc_blocks = &((struct coh_freelist_chunk *) bh_data)->fl_free[0];
				break;
			default: panic("sysv_new_block: invalid fs type\n");
		}
		if (*flc_count > sb->sv_flc_size) {
			printk("sysv_new_block: free-list block with >flc_size entries\n");
			brelse(bh);
			unlock_super(sb);
			return 0;
		}
		*sb->sv_sb_flc_count = *flc_count;
		memcpy(sb->sv_sb_flc_blocks, flc_blocks, *flc_count * sizeof(sysv_zone_t));
		brelse(bh);
	}
	/* Now the free list head in the superblock is valid again. */
	bh = sv_getblk(sb, sb->s_dev, block);
	if (!bh) {
		printk("sysv_new_block: getblk() failed\n");
		unlock_super(sb);
		return 0;
	}
	if (bh->b_count != 1) {
		printk("sysv_new_block: block already in use\n");
		unlock_super(sb);
		return 0;
	}
	memset(bh->b_data, 0, sb->sv_block_size);
	mark_buffer_dirty(bh, 1);
	mark_buffer_uptodate(bh, 1);
	brelse(bh);
	if (sb->sv_convert)
		*sb->sv_sb_total_free_blocks =
		  to_coh_ulong(from_coh_ulong(*sb->sv_sb_total_free_blocks) - 1);
	else
		*sb->sv_sb_total_free_blocks = *sb->sv_sb_total_free_blocks - 1;
	mark_buffer_dirty(sb->sv_bh1, 1); /* super-block has been modified */
	if (sb->sv_bh1 != sb->sv_bh2) mark_buffer_dirty(sb->sv_bh2, 1);
	sb->s_dirt = 1; /* and needs time stamp */
	unlock_super(sb);
	return block;
}

unsigned long sysv_count_free_blocks(struct super_block * sb)
{
#if 1 /* test */
	int count, old_count;
	unsigned int block;
	struct buffer_head * bh;
	char * bh_data;
	int i;

	/* this causes a lot of disk traffic ... */
	count = 0;
	lock_super(sb);
	if (*sb->sv_sb_flc_count > 0) {
		for (i = *sb->sv_sb_flc_count ; /* i > 0 */ ; ) {
			block = sb->sv_sb_flc_blocks[--i];
			if (sb->sv_convert)
				block = from_coh_ulong(block);
			if (block == 0) /* block 0 terminates list */
				goto done;
			count++;
			if (i == 0)
				break;
		}
		/* block = sb->sv_sb_flc_blocks[0], the last block continues the free list */
		while (1) {
			unsigned short * flc_count;
			unsigned long * flc_blocks;

			if (block < sb->sv_firstdatazone || block >= sb->sv_nzones) {
				printk("sysv_count_free_blocks: new block %d is not in data zone\n",block);
				break;
			}
			if (!(bh = sv_bread(sb, sb->s_dev, block))) {
				printk("sysv_count_free_blocks: cannot read free-list block\n");
				break;
			}
			bh_data = bh->b_data;
			switch (sb->sv_type) {
				case FSTYPE_XENIX:
					flc_count = &((struct xenix_freelist_chunk *) bh_data)->fl_nfree;
					flc_blocks = &((struct xenix_freelist_chunk *) bh_data)->fl_free[0];
					break;
				case FSTYPE_SYSV4:
					flc_count = &((struct sysv4_freelist_chunk *) bh_data)->fl_nfree;
					flc_blocks = &((struct sysv4_freelist_chunk *) bh_data)->fl_free[0];
					break;
				case FSTYPE_SYSV2:
					flc_count = &((struct sysv2_freelist_chunk *) bh_data)->fl_nfree;
					flc_blocks = &((struct sysv2_freelist_chunk *) bh_data)->fl_free[0];
					break;
				case FSTYPE_COH:
					flc_count = &((struct coh_freelist_chunk *) bh_data)->fl_nfree;
					flc_blocks = &((struct coh_freelist_chunk *) bh_data)->fl_free[0];
					break;
				default: panic("sysv_count_free_blocks: invalid fs type\n");
			}
			if (*flc_count > sb->sv_flc_size) {
				printk("sysv_count_free_blocks: free-list block with >flc_size entries\n");
				brelse(bh);
				break;
			}
			if (*flc_count == 0) { /* Applies only to Coherent FS */
				brelse(bh);
				break;
			}
			for (i = *flc_count ; /* i > 0 */ ; ) {
				block = flc_blocks[--i];
				if (sb->sv_convert)
					block = from_coh_ulong(block);
				if (block == 0) /* block 0 terminates list */
					break;
				count++;
				if (i == 0)
					break;
			}
			/* block = flc_blocks[0], the last block continues the free list */
			brelse(bh);
			if (block == 0) /* Applies only to Xenix FS and SystemV FS */
				break;
		}
		done: ;
	}
	old_count = *sb->sv_sb_total_free_blocks;
	if (sb->sv_convert)
		old_count = from_coh_ulong(old_count);
	if (count != old_count) {
		printk("sysv_count_free_blocks: free block count was %d, correcting to %d\n",old_count,count);
		if (!(sb->s_flags & MS_RDONLY)) {
			*sb->sv_sb_total_free_blocks = (sb->sv_convert ? to_coh_ulong(count) : count);
			mark_buffer_dirty(sb->sv_bh2, 1); /* super-block has been modified */
			sb->s_dirt = 1; /* and needs time stamp */
		}
	}
	unlock_super(sb);
	return count;
#else
	int count;

	count = *sb->sv_sb_total_free_blocks;
	if (sb->sv_convert)
		count = from_coh_ulong(count);
	return count;
#endif
}