-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathgetblk.c
400 lines (322 loc) · 11 KB
/
getblk.c
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
/* getblk.c - automatically generated from getblk.c.in */
/* by function-tailoring.sh on mer 7 feb 2018, 09.43.26, UTC */
#line 1 "getblk.c.in"
/* File block to disk block mapping routines -*-C-*-
NOTE: This file is processed by `function-tailoring.sh' to get
`getblk.c'.
Copyright (C) 1995,96,99,2000 Free Software Foundation, Inc.
Converted to work under the hurd by Miles Bader <[email protected]>
Modified for the MINIX file system from the original for the ext2
file system by Roberto Reale <[email protected]>.
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License as
published by the Free Software Foundation; either version 2, or (at
your option) any later version.
This program is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */
#include "minixfs.h"
#line 29 "getblk.c.in"
static error_t
inode_getblk_V1 (struct node *node, int nr, int create, int zero,
block_t boffs, block_t *result)
{
zone_t zone_result;
minixfs_debug ("looking up disk zone referenced by in-inode pointer %d "
"in node %Ld (CREATE flag == %s)", nr, node->cache_id,
create ? "true" : "false");
zone_result = (zone_t) node->dn->info.i_zone_V1[nr];
if (zone_result)
{
assert (zone_result >= sblock->s_firstdatazone
&& zone_result < sblock_info->s_nzones);
minixfs_debug ("in-inode pointer at index %d in node %Ld "
"references zone %u", nr, node->cache_id,
zone_result);
goto got_it;
}
if (!create)
return EINVAL;
zone_result = minixfs_new_zone (node, zero);
if (!zone_result)
return ENOSPC;
node->dn->info.i_zone_V1[nr] = (uint16_t) zone_result;
minixfs_debug ("new zone %u allocated and stored into in-inode "
"pointer at index %d in node %Ld",
zone_result, nr, node->cache_id);
node->dn_set_ctime = node->dn_set_mtime = 1;
node->dn_stat.st_blocks += 1 <<
(log2_stat_blocks_per_fs_block + log2_fs_blocks_per_zone);
node->dn_stat_dirty = 1;
if (diskfs_synchronous)
diskfs_node_update (node, 1);
got_it:
*result = (zone_result << log2_fs_blocks_per_zone) + boffs;
return 0;
}
static error_t
zone_getblk_V1 (struct node *node, zone_t zone, int nr, int create,
int zero, block_t boffs, zone_t *result)
{
zone_t zone_result;
char *bh = zptr (zone);
minixfs_debug ("looking up disk zone referenced by pointer %d "
"in the zone of indirection %u (node %Ld, "
"CREATE flag == %s)", nr, zone, node->cache_id,
create ? "true" : "false");
zone_result = (zone_t) *(((uint16_t *) bh) + nr);
if (zone_result)
{
assert (zone_result >= sblock->s_firstdatazone
&& zone_result < sblock_info->s_nzones);
minixfs_debug ("pointer at index %d in the zone of indirection "
"%u (node %Ld) references zone %u",
nr, zone, node->cache_id, zone_result);
goto got_it;
}
if (!create)
return EINVAL;
zone_result = minixfs_new_zone (node, zero);
if (!zone_result)
return ENOSPC;
*(((uint16_t *) bh) + nr) = (uint16_t) zone_result;
minixfs_debug ("node %Ld: new zone %u allocated and stored into pointer "
"at index %d in the zone of indirection %u",
node->cache_id, zone_result, nr, zone);
if (diskfs_synchronous)
sync_global_ptr (bh, 1);
else
record_indir_poke (node, bh);
node->dn_set_ctime = node->dn_set_mtime = 1;
node->dn_stat.st_blocks += 1 <<
(log2_stat_blocks_per_fs_block + log2_fs_blocks_per_zone);
node->dn_stat_dirty = 1;
got_it:
*result = (zone_result << log2_fs_blocks_per_zone) + boffs;
return 0;
}
static error_t
minixfs_getblk_V1 (struct node *node, zone_t zone, int boffs,
int create, block_t *disk_block)
{
error_t err;
block_t indir;
if (zone < MINIXFS_NDIR_ZONES)
return inode_getblk_V1 (node, (int) zone, create, 0, boffs,
disk_block);
zone -= MINIXFS_NDIR_ZONES;
if (zone < ADDR_PER_BLOCK)
{
err = inode_getblk_V1 (node, MINIXFS_IND_ZONE, create, 1, 0,
&indir);
if (!err)
err = zone_getblk_V1 (node, indir >> log2_fs_blocks_per_zone,
zone, create, 0, boffs, disk_block);
return err;
}
zone -= ADDR_PER_BLOCK;
if (zone < ADDR_PER_BLOCK * ADDR_PER_BLOCK)
{
err = inode_getblk_V1 (node, MINIXFS_DIND_ZONE, create, 1, 0,
&indir);
if (!err)
err = zone_getblk_V1 (node, indir >> log2_fs_blocks_per_zone,
zone / ADDR_PER_BLOCK, create, 1, 0,
&indir);
if (!err)
err = zone_getblk_V1 (node, indir >> log2_fs_blocks_per_zone,
zone & (ADDR_PER_BLOCK - 1), create, 0,
boffs, disk_block);
return err;
}
if (! sblock_info->s_has_tind)
{
/* If we expect to find a block that cannot exist, we are undoubtedly
mistaken. */
assert (create);
return EFBIG; /* XXX */
}
zone -= ADDR_PER_BLOCK * ADDR_PER_BLOCK;
err = inode_getblk_V1 (node, MINIXFS_TIND_ZONE, create, 1, 0,
&indir);
if (!err)
err = zone_getblk_V1 (node, indir >> log2_fs_blocks_per_zone,
zone / (ADDR_PER_BLOCK * ADDR_PER_BLOCK),
create, 1, 0, &indir);
if (!err)
err = zone_getblk_V1 (node, indir >> log2_fs_blocks_per_zone,
((zone / ADDR_PER_BLOCK)
& (ADDR_PER_BLOCK - 1)),
create, 1, 0, &indir);
if (!err)
err = zone_getblk_V1 (node, indir >> log2_fs_blocks_per_zone,
zone & (ADDR_PER_BLOCK - 1), create, 0,
boffs, disk_block);
return err;
}
#line 29 "getblk.c.in"
static error_t
inode_getblk_V2 (struct node *node, int nr, int create, int zero,
block_t boffs, block_t *result)
{
zone_t zone_result;
minixfs_debug ("looking up disk zone referenced by in-inode pointer %d "
"in node %Ld (CREATE flag == %s)", nr, node->cache_id,
create ? "true" : "false");
zone_result = (zone_t) node->dn->info.i_zone_V2[nr];
if (zone_result)
{
assert (zone_result >= sblock->s_firstdatazone
&& zone_result < sblock_info->s_nzones);
minixfs_debug ("in-inode pointer at index %d in node %Ld "
"references zone %u", nr, node->cache_id,
zone_result);
goto got_it;
}
if (!create)
return EINVAL;
zone_result = minixfs_new_zone (node, zero);
if (!zone_result)
return ENOSPC;
node->dn->info.i_zone_V2[nr] = (uint32_t) zone_result;
minixfs_debug ("new zone %u allocated and stored into in-inode "
"pointer at index %d in node %Ld",
zone_result, nr, node->cache_id);
node->dn_set_ctime = node->dn_set_mtime = 1;
node->dn_stat.st_blocks += 1 <<
(log2_stat_blocks_per_fs_block + log2_fs_blocks_per_zone);
node->dn_stat_dirty = 1;
if (diskfs_synchronous)
diskfs_node_update (node, 1);
got_it:
*result = (zone_result << log2_fs_blocks_per_zone) + boffs;
return 0;
}
static error_t
zone_getblk_V2 (struct node *node, zone_t zone, int nr, int create,
int zero, block_t boffs, zone_t *result)
{
zone_t zone_result;
char *bh = zptr (zone);
minixfs_debug ("looking up disk zone referenced by pointer %d "
"in the zone of indirection %u (node %Ld, "
"CREATE flag == %s)", nr, zone, node->cache_id,
create ? "true" : "false");
zone_result = (zone_t) *(((uint32_t *) bh) + nr);
if (zone_result)
{
assert (zone_result >= sblock->s_firstdatazone
&& zone_result < sblock_info->s_nzones);
minixfs_debug ("pointer at index %d in the zone of indirection "
"%u (node %Ld) references zone %u",
nr, zone, node->cache_id, zone_result);
goto got_it;
}
if (!create)
return EINVAL;
zone_result = minixfs_new_zone (node, zero);
if (!zone_result)
return ENOSPC;
*(((uint32_t *) bh) + nr) = (uint32_t) zone_result;
minixfs_debug ("node %Ld: new zone %u allocated and stored into pointer "
"at index %d in the zone of indirection %u",
node->cache_id, zone_result, nr, zone);
if (diskfs_synchronous)
sync_global_ptr (bh, 1);
else
record_indir_poke (node, bh);
node->dn_set_ctime = node->dn_set_mtime = 1;
node->dn_stat.st_blocks += 1 <<
(log2_stat_blocks_per_fs_block + log2_fs_blocks_per_zone);
node->dn_stat_dirty = 1;
got_it:
*result = (zone_result << log2_fs_blocks_per_zone) + boffs;
return 0;
}
static error_t
minixfs_getblk_V2 (struct node *node, zone_t zone, int boffs,
int create, block_t *disk_block)
{
error_t err;
block_t indir;
if (zone < MINIXFS_NDIR_ZONES)
return inode_getblk_V2 (node, (int) zone, create, 0, boffs,
disk_block);
zone -= MINIXFS_NDIR_ZONES;
if (zone < ADDR_PER_BLOCK)
{
err = inode_getblk_V2 (node, MINIXFS_IND_ZONE, create, 1, 0,
&indir);
if (!err)
err = zone_getblk_V2 (node, indir >> log2_fs_blocks_per_zone,
zone, create, 0, boffs, disk_block);
return err;
}
zone -= ADDR_PER_BLOCK;
if (zone < ADDR_PER_BLOCK * ADDR_PER_BLOCK)
{
err = inode_getblk_V2 (node, MINIXFS_DIND_ZONE, create, 1, 0,
&indir);
if (!err)
err = zone_getblk_V2 (node, indir >> log2_fs_blocks_per_zone,
zone / ADDR_PER_BLOCK, create, 1, 0,
&indir);
if (!err)
err = zone_getblk_V2 (node, indir >> log2_fs_blocks_per_zone,
zone & (ADDR_PER_BLOCK - 1), create, 0,
boffs, disk_block);
return err;
}
if (! sblock_info->s_has_tind)
{
/* If we expect to find a block that cannot exist, we are undoubtedly
mistaken. */
assert (create);
return EFBIG; /* XXX */
}
zone -= ADDR_PER_BLOCK * ADDR_PER_BLOCK;
err = inode_getblk_V2 (node, MINIXFS_TIND_ZONE, create, 1, 0,
&indir);
if (!err)
err = zone_getblk_V2 (node, indir >> log2_fs_blocks_per_zone,
zone / (ADDR_PER_BLOCK * ADDR_PER_BLOCK),
create, 1, 0, &indir);
if (!err)
err = zone_getblk_V2 (node, indir >> log2_fs_blocks_per_zone,
((zone / ADDR_PER_BLOCK)
& (ADDR_PER_BLOCK - 1)),
create, 1, 0, &indir);
if (!err)
err = zone_getblk_V2 (node, indir >> log2_fs_blocks_per_zone,
zone & (ADDR_PER_BLOCK - 1), create, 0,
boffs, disk_block);
return err;
}
/* Returns in DISK_BLOCK the disk block corresponding to BLOCK in NODE. If
there is no such block yet, but CREATE is true, then it is created,
otherwise EINVAL is returned. */
error_t
minixfs_getblk (struct node *node, block_t block, int create,
block_t *disk_block)
{
zone_t zone = block >> log2_fs_blocks_per_zone;
/* relative offset of our block in its zone */
int boffs = block & ((1 << log2_fs_blocks_per_zone) - 1);
minixfs_debug ("looking up disk block corresponding to block %u "
"in node %Ld (zone %u, block %d) - CREATE flag == %s",
block, node->cache_id, zone, boffs,
create ? "true" : "false");
if (zone > sblock_info->s_max_addressable_zone)
{
minixfs_warning ("zone index %u > max", zone);
return EIO;
}
if (sblock_info->s_version == MINIX_V1)
return minixfs_getblk_V1 (node, zone, boffs, create, disk_block);
else
return minixfs_getblk_V2 (node, zone, boffs, create, disk_block);
}