summaryrefslogtreecommitdiffstats
path: root/fs/minix/truncate.c
blob: f26aa086cba2dca10ffa8a89501de8056566ea9d (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
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
/*
 *  linux/fs/truncate.c
 *
 *  Copyright (C) 1991, 1992  Linus Torvalds
 *
 *  Copyright (C) 1996  Gertjan van Wingerde (gertjan@cs.vu.nl)
 *	Minix V2 fs support.
 */

#include <linux/errno.h>
#include <linux/sched.h>
#include <linux/minix_fs.h>
#include <linux/stat.h>
#include <linux/fcntl.h>

#define DIRECT_BLOCK		((inode->i_size + 1023) >> 10)
#define INDIRECT_BLOCK(offset)	(DIRECT_BLOCK-offset)
#define V1_DINDIRECT_BLOCK(offset) ((DIRECT_BLOCK-offset)>>9)
#define V2_DINDIRECT_BLOCK(offset) ((DIRECT_BLOCK-offset)>>8)
#define TINDIRECT_BLOCK(offset) ((DIRECT_BLOCK-(offset))>>8)

/*
 * Truncate has the most races in the whole filesystem: coding it is
 * a pain in the a**, especially as I don't do any locking.
 *
 * The code may look a bit weird, but that's just because I've tried to
 * handle things like file-size changes in a somewhat graceful manner.
 * Anyway, truncating a file at the same time somebody else writes to it
 * is likely to result in pretty weird behaviour...
 *
 * The new code handles normal truncates (size = 0) as well as the more
 * general case (size = XXX). I hope.
 */

#define DATA_BUFFER_USED(bh) \
	(atomic_read(&bh->b_count) || buffer_locked(bh))

/*
 * The functions for minix V1 fs truncation.
 */
static int V1_trunc_direct(struct inode * inode)
{
	unsigned short * p;
	struct buffer_head * bh;
	int i, tmp;
	int retry = 0;

repeat:
	for (i = DIRECT_BLOCK ; i < 7 ; i++) {
		p = i + inode->u.minix_i.u.i1_data;
		if (!(tmp = *p))
			continue;
		bh = get_hash_table(inode->i_dev,tmp,BLOCK_SIZE);
		if (i < DIRECT_BLOCK) {
			brelse(bh);
			goto repeat;
		}
		if ((bh && DATA_BUFFER_USED(bh)) || tmp != *p) {
			retry = 1;
			brelse(bh);
			continue;
		}
		*p = 0;
		mark_inode_dirty(inode);
		bforget(bh);
		minix_free_block(inode,tmp);
	}
	return retry;
}

static int V1_trunc_indirect(struct inode * inode, int offset, unsigned short * p)
{
	struct buffer_head * bh;
	int i, tmp;
	struct buffer_head * ind_bh;
	unsigned short * ind;
	int retry = 0;

	tmp = *p;
	if (!tmp)
		return 0;
	ind_bh = bread(inode->i_dev, tmp, BLOCK_SIZE);
	if (tmp != *p) {
		brelse(ind_bh);
		return 1;
	}
	if (!ind_bh) {
		*p = 0;
		return 0;
	}
repeat:
	for (i = INDIRECT_BLOCK(offset) ; i < 512 ; i++) {
		if (i < 0)
			i = 0;
		if (i < INDIRECT_BLOCK(offset))
			goto repeat;
		ind = i+(unsigned short *) ind_bh->b_data;
		tmp = *ind;
		if (!tmp)
			continue;
		bh = get_hash_table(inode->i_dev,tmp,BLOCK_SIZE);
		if (i < INDIRECT_BLOCK(offset)) {
			brelse(bh);
			goto repeat;
		}
		if ((bh && DATA_BUFFER_USED(bh)) || tmp != *ind) {
			retry = 1;
			brelse(bh);
			continue;
		}
		*ind = 0;
		mark_buffer_dirty(ind_bh, 1);
		bforget(bh);
		minix_free_block(inode,tmp);
	}
	ind = (unsigned short *) ind_bh->b_data;
	for (i = 0; i < 512; i++)
		if (*(ind++))
			break;
	if (i >= 512) {
		if (atomic_read(&ind_bh->b_count) != 1)
			retry = 1;
		else {
			tmp = *p;
			*p = 0;
			minix_free_block(inode,tmp);
		}
	}
	brelse(ind_bh);
	return retry;
}

static int V1_trunc_dindirect(struct inode * inode, int offset, unsigned short *p)
{
	int i, tmp;
	struct buffer_head * dind_bh;
	unsigned short * dind;
	int retry = 0;

	if (!(tmp = *p))
		return 0;
	dind_bh = bread(inode->i_dev, tmp, BLOCK_SIZE);
	if (tmp != *p) {
		brelse(dind_bh);
		return 1;
	}
	if (!dind_bh) {
		*p = 0;
		return 0;
	}
repeat:
	for (i = V1_DINDIRECT_BLOCK(offset) ; i < 512 ; i ++) {
		if (i < 0)
			i = 0;
		if (i < V1_DINDIRECT_BLOCK(offset))
			goto repeat;
		dind = i+(unsigned short *) dind_bh->b_data;
		retry |= V1_trunc_indirect(inode,offset+(i<<9),dind);
		mark_buffer_dirty(dind_bh, 1);
	}
	dind = (unsigned short *) dind_bh->b_data;
	for (i = 0; i < 512; i++)
		if (*(dind++))
			break;
	if (i >= 512) {
		if (atomic_read(&dind_bh->b_count) != 1)
			retry = 1;
		else {
			tmp = *p;
			*p = 0;
			mark_inode_dirty(inode);
			minix_free_block(inode,tmp);
		}
	}
	brelse(dind_bh);
	return retry;
}

static void V1_minix_truncate(struct inode * inode)
{
	int retry;

	if (!(S_ISREG(inode->i_mode) || S_ISDIR(inode->i_mode) ||
	     S_ISLNK(inode->i_mode)))
		return;
	while (1) {
		retry = V1_trunc_direct(inode);
		retry |= V1_trunc_indirect(inode, 7, inode->u.minix_i.u.i1_data + 7);
		retry |= V1_trunc_dindirect(inode, 7+512, inode->u.minix_i.u.i1_data + 8);
		if (!retry)
			break;
		current->counter = 0;
		schedule();
	}
	inode->i_mtime = inode->i_ctime = CURRENT_TIME;
	mark_inode_dirty(inode);
}

/*
 * The functions for minix V2 fs truncation.
 */
static int V2_trunc_direct(struct inode * inode)
{
	unsigned long * p;
	struct buffer_head * bh;
	int i, tmp;
	int retry = 0;

repeat:
	for (i = DIRECT_BLOCK ; i < 7 ; i++) {
		p = (unsigned long *) inode->u.minix_i.u.i2_data + i;
		if (!(tmp = *p))
			continue;
		bh = get_hash_table(inode->i_dev,tmp,BLOCK_SIZE);
		if (i < DIRECT_BLOCK) {
			brelse(bh);
			goto repeat;
		}
		if ((bh && DATA_BUFFER_USED(bh)) || tmp != *p) {
			retry = 1;
			brelse(bh);
			continue;
		}
		*p = 0;
		mark_inode_dirty(inode);
		bforget(bh);
		minix_free_block(inode,tmp);
	}
	return retry;
}

static int V2_trunc_indirect(struct inode * inode, int offset, unsigned long * p)
{
	struct buffer_head * bh;
	int i, tmp;
	struct buffer_head * ind_bh;
	unsigned long * ind;
	int retry = 0;

	tmp = *p;
	if (!tmp)
		return 0;
	ind_bh = bread(inode->i_dev, tmp, BLOCK_SIZE);
	if (tmp != *p) {
		brelse(ind_bh);
		return 1;
	}
	if (!ind_bh) {
		*p = 0;
		return 0;
	}
repeat:
	for (i = INDIRECT_BLOCK(offset) ; i < 256 ; i++) {
		if (i < 0)
			i = 0;
		if (i < INDIRECT_BLOCK(offset))
			goto repeat;
		ind = i+(unsigned long *) ind_bh->b_data;
		tmp = *ind;
		if (!tmp)
			continue;
		bh = get_hash_table(inode->i_dev,tmp,BLOCK_SIZE);
		if (i < INDIRECT_BLOCK(offset)) {
			brelse(bh);
			goto repeat;
		}
		if ((bh && DATA_BUFFER_USED(bh)) || tmp != *ind) {
			retry = 1;
			brelse(bh);
			continue;
		}
		*ind = 0;
		mark_buffer_dirty(ind_bh, 1);
		bforget(bh);
		minix_free_block(inode,tmp);
	}
	ind = (unsigned long *) ind_bh->b_data;
	for (i = 0; i < 256; i++)
		if (*(ind++))
			break;
	if (i >= 256) {
		if (atomic_read(&ind_bh->b_count) != 1)
			retry = 1;
		else {
			tmp = *p;
			*p = 0;
			minix_free_block(inode,tmp);
		}
	}
	brelse(ind_bh);
	return retry;
}

static int V2_trunc_dindirect(struct inode * inode, int offset, unsigned long *p)
{
	int i, tmp;
	struct buffer_head * dind_bh;
	unsigned long * dind;
	int retry = 0;

	if (!(tmp = *p))
		return 0;
	dind_bh = bread(inode->i_dev, tmp, BLOCK_SIZE);
	if (tmp != *p) {
		brelse(dind_bh);
		return 1;
	}
	if (!dind_bh) {
		*p = 0;
		return 0;
	}
repeat:
	for (i = V2_DINDIRECT_BLOCK(offset) ; i < 256 ; i ++) {
		if (i < 0)
			i = 0;
		if (i < V2_DINDIRECT_BLOCK(offset))
			goto repeat;
		dind = i+(unsigned long *) dind_bh->b_data;
		retry |= V2_trunc_indirect(inode,offset+(i<<8),dind);
		mark_buffer_dirty(dind_bh, 1);
	}
	dind = (unsigned long *) dind_bh->b_data;
	for (i = 0; i < 256; i++)
		if (*(dind++))
			break;
	if (i >= 256) {
		if (atomic_read(&dind_bh->b_count) != 1)
			retry = 1;
		else {
			tmp = *p;
			*p = 0;
			mark_inode_dirty(inode);
			minix_free_block(inode,tmp);
		}
	}
	brelse(dind_bh);
	return retry;
}

static int V2_trunc_tindirect(struct inode * inode, int offset, unsigned long * p)
{
        int i, tmp;
        struct buffer_head * tind_bh;
        unsigned long * tind;
        int retry = 0;

        if (!(tmp = *p))
                return 0;
        tind_bh = bread(inode->i_dev, tmp, BLOCK_SIZE);
        if (tmp != *p) {
                brelse(tind_bh);
                return 1;
	}
        if (!tind_bh) {
                *p = 0;
                return 0;
	}
repeat:
        for (i = TINDIRECT_BLOCK(offset) ; i < 256 ; i ++) {
                if (i < 0)
                        i = 0;
                if (i < TINDIRECT_BLOCK(offset))
                        goto repeat;
                tind = i+(unsigned long *) tind_bh->b_data;
                retry |= V2_trunc_dindirect(inode,offset+(i<<8),tind);
                mark_buffer_dirty(tind_bh, 1);
	}
        tind = (unsigned long *) tind_bh->b_data;
        for (i = 0; i < 256; i++)
                if (*(tind++))
                        break;
        if (i >= 256) {
                if (atomic_read(&tind_bh->b_count) != 1)
                        retry = 1;
                else {
                        tmp = *p;
                        *p = 0;
                        mark_inode_dirty(inode);
                        minix_free_block(inode,tmp);
		}
	}
        brelse(tind_bh);
        return retry;
}

static void V2_minix_truncate(struct inode * inode)
{
	int retry;

	if (!(S_ISREG(inode->i_mode) || S_ISDIR(inode->i_mode) ||
	     S_ISLNK(inode->i_mode)))
		return;
	while (1) {
		retry = V2_trunc_direct(inode);
		retry |= V2_trunc_indirect(inode,7,
			(unsigned long *) inode->u.minix_i.u.i2_data + 7);
		retry |= V2_trunc_dindirect(inode, 7+256, 
			(unsigned long *) inode->u.minix_i.u.i2_data + 8);
		retry |= V2_trunc_tindirect(inode, 7+256+256*256, 
			(unsigned long *) inode->u.minix_i.u.i2_data + 9);
		if (!retry)
			break;
		run_task_queue(&tq_disk);
		current->policy |= SCHED_YIELD;
		schedule();
	}
	inode->i_mtime = inode->i_ctime = CURRENT_TIME;
	mark_inode_dirty(inode);
}

/*
 * The function that is called for file truncation.
 */
void minix_truncate(struct inode * inode)
{
	if (INODE_VERSION(inode) == MINIX_V1)
		V1_minix_truncate(inode);
	else
		V2_minix_truncate(inode);
}