summaryrefslogtreecommitdiffstats
path: root/fs/minix/fsync.c
blob: 44606d2600ae9f01688c9ad2c830a8f4923b70f9 (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
/*
 *  linux/fs/minix/fsync.c
 *
 *  Copyright (C) 1993 Stephen Tweedie (sct@dcs.ed.ac.uk)
 *  from
 *  Copyright (C) 1991, 1992 Linus Torvalds
 *
 *  Copyright (C) 1996 Gertjan van Wingerde (gertjan@cs.vu.nl)
 *	Minix V2 fs support
 *
 *  minix fsync primitive
 */

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

#include <linux/fs.h>
#include <linux/minix_fs.h>

#include <asm/uaccess.h>
#include <asm/system.h>

#define blocksize BLOCK_SIZE

/*
 * The functions for minix V1 fs file synchronization.
 */
static int V1_sync_block (struct inode * inode, unsigned short * block, int wait)
{
	struct buffer_head * bh;
	unsigned short tmp;
	
	if (!*block)
		return 0;
	tmp = *block;
	bh = get_hash_table(inode->i_dev, *block, blocksize);
	if (!bh)
		return 0;
	if (*block != tmp) {
		brelse (bh);
		return 1;
	}
	if (wait && buffer_req(bh) && !buffer_uptodate(bh)) {
		brelse(bh);
		return -1;
	}
	if (wait || !buffer_uptodate(bh) || !buffer_dirty(bh))
	{
		brelse(bh);
		return 0;
	}
	ll_rw_block(WRITE, 1, &bh);
	bh->b_count--;
	return 0;
}

static int V1_sync_iblock (struct inode * inode, unsigned short * iblock, 
			struct buffer_head **bh, int wait) 
{
	int rc;
	unsigned short tmp;
	
	*bh = NULL;
	tmp = *iblock;
	if (!tmp)
		return 0;
	rc = V1_sync_block (inode, iblock, wait);
	if (rc)
		return rc;
	*bh = bread(inode->i_dev, tmp, blocksize);
	if (tmp != *iblock) {
		brelse(*bh);
		*bh = NULL;
		return 1;
	}
	if (!*bh)
		return -1;
	return 0;
}

static int V1_sync_direct(struct inode *inode, int wait)
{
	int i;
	int rc, err = 0;

	for (i = 0; i < 7; i++) {
		rc = V1_sync_block (inode, 
		     (unsigned short *) inode->u.minix_i.u.i1_data + i, wait);
		if (rc > 0)
			break;
		if (rc)
			err = rc;
	}
	return err;
}

static int V1_sync_indirect(struct inode *inode, unsigned short *iblock, int wait)
{
	int i;
	struct buffer_head * ind_bh;
	int rc, err = 0;

	rc = V1_sync_iblock (inode, iblock, &ind_bh, wait);
	if (rc || !ind_bh)
		return rc;

	for (i = 0; i < 512; i++) {
		rc = V1_sync_block (inode, 
				    ((unsigned short *) ind_bh->b_data) + i, 
				    wait);
		if (rc > 0)
			break;
		if (rc)
			err = rc;
	}
	brelse(ind_bh);
	return err;
}

static int V1_sync_dindirect(struct inode *inode, unsigned short *diblock,
			  int wait)
{
	int i;
	struct buffer_head * dind_bh;
	int rc, err = 0;

	rc = V1_sync_iblock (inode, diblock, &dind_bh, wait);
	if (rc || !dind_bh)
		return rc;

	for (i = 0; i < 512; i++) {
		rc = V1_sync_indirect (inode,
				       ((unsigned short *) dind_bh->b_data) + i, 
				       wait);
		if (rc > 0)
			break;
		if (rc)
			err = rc;
	}
	brelse(dind_bh);
	return err;
}

int V1_minix_sync_file(struct inode * inode, struct file * file)
{
	int wait, err = 0;
	
	if (!(S_ISREG(inode->i_mode) || S_ISDIR(inode->i_mode) ||
	     S_ISLNK(inode->i_mode)))
		return -EINVAL;

	for (wait=0; wait<=1; wait++)
	{
		err |= V1_sync_direct(inode, wait);
		err |= V1_sync_indirect(inode, inode->u.minix_i.u.i1_data + 7, wait);
		err |= V1_sync_dindirect(inode, inode->u.minix_i.u.i1_data + 8, wait);
	}
	err |= minix_sync_inode (inode);
	return (err < 0) ? -EIO : 0;
}

/* 
 * The functions for minix V2 fs file synchronization.
 */
static int V2_sync_block (struct inode * inode, unsigned long * block, int wait)
{
	struct buffer_head * bh;
	unsigned long tmp;
	
	if (!*block)
		return 0;
	tmp = *block;
	bh = get_hash_table(inode->i_dev, *block, blocksize);
	if (!bh)
		return 0;
	if (*block != tmp) {
		brelse (bh);
		return 1;
	}
	if (wait && buffer_req(bh) && !buffer_uptodate(bh)) {
		brelse(bh);
		return -1;
	}
	if (wait || !buffer_uptodate(bh) || !buffer_dirty(bh))
	{
		brelse(bh);
		return 0;
	}
	ll_rw_block(WRITE, 1, &bh);
	bh->b_count--;
	return 0;
}

static int V2_sync_iblock (struct inode * inode, unsigned long * iblock, 
			struct buffer_head **bh, int wait) 
{
	int rc;
	unsigned long tmp;
	
	*bh = NULL;
	tmp = *iblock;
	if (!tmp)
		return 0;
	rc = V2_sync_block (inode, iblock, wait);
	if (rc)
		return rc;
	*bh = bread(inode->i_dev, tmp, blocksize);
	if (tmp != *iblock) {
		brelse(*bh);
		*bh = NULL;
		return 1;
	}
	if (!*bh)
		return -1;
	return 0;
}

static int V2_sync_direct(struct inode *inode, int wait)
{
	int i;
	int rc, err = 0;

	for (i = 0; i < 7; i++) {
		rc = V2_sync_block (inode, 
			(unsigned long *)inode->u.minix_i.u.i2_data + i, wait);
		if (rc > 0)
			break;
		if (rc)
			err = rc;
	}
	return err;
}

static int V2_sync_indirect(struct inode *inode, unsigned long *iblock, int wait)
{
	int i;
	struct buffer_head * ind_bh;
	int rc, err = 0;

	rc = V2_sync_iblock (inode, iblock, &ind_bh, wait);
	if (rc || !ind_bh)
		return rc;

	for (i = 0; i < 256; i++) {
		rc = V2_sync_block (inode, 
				    ((unsigned long *) ind_bh->b_data) + i, 
				    wait);
		if (rc > 0)
			break;
		if (rc)
			err = rc;
	}
	brelse(ind_bh);
	return err;
}

static int V2_sync_dindirect(struct inode *inode, unsigned long *diblock,
			  int wait)
{
	int i;
	struct buffer_head * dind_bh;
	int rc, err = 0;

	rc = V2_sync_iblock (inode, diblock, &dind_bh, wait);
	if (rc || !dind_bh)
		return rc;

	for (i = 0; i < 256; i++) {
		rc = V2_sync_indirect (inode,
				       ((unsigned long *) dind_bh->b_data) + i, 
				       wait);
		if (rc > 0)
			break;
		if (rc)
			err = rc;
	}
	brelse(dind_bh);
	return err;
}

static int V2_sync_tindirect(struct inode *inode, unsigned long *tiblock,
			  int wait)
{
	int i;
	struct buffer_head * tind_bh;
	int rc, err = 0;

	rc = V2_sync_iblock (inode, tiblock, &tind_bh, wait);
	if (rc || !tind_bh)
		return rc;

	for (i = 0; i < 256; i++) {
		rc = V2_sync_dindirect (inode,
					((unsigned long *) tind_bh->b_data) + i, 
					wait);
		if (rc > 0)
			break;
		if (rc)
			err = rc;
	}
	brelse(tind_bh);
	return err;
}

int V2_minix_sync_file(struct inode * inode, struct file * file)
{
	int wait, err = 0;
	
	if (!(S_ISREG(inode->i_mode) || S_ISDIR(inode->i_mode) ||
	     S_ISLNK(inode->i_mode)))
		return -EINVAL;

	for (wait=0; wait<=1; wait++)
	{
		err |= V2_sync_direct(inode, wait);
		err |= V2_sync_indirect(inode, 
		      (unsigned long *) inode->u.minix_i.u.i2_data + 7, wait);
		err |= V2_sync_dindirect(inode, 
		      (unsigned long *) inode->u.minix_i.u.i2_data + 8, wait);
		err |= V2_sync_tindirect(inode, 
		      (unsigned long *) inode->u.minix_i.u.i2_data + 9, wait);
	}
	err |= minix_sync_inode (inode);
	return (err < 0) ? -EIO : 0;
}

/*
 *	The function which is called for file synchronization. File may be
 *	NULL
 */
 
int minix_sync_file(struct file * file, struct dentry *dentry)
{
	struct inode *inode = dentry->d_inode;
	
	if (INODE_VERSION(inode) == MINIX_V1)
		return V1_minix_sync_file(inode, file);
	else
		return V2_minix_sync_file(inode, file);
}