-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathblock.c
More file actions
313 lines (243 loc) · 7.33 KB
/
block.c
File metadata and controls
313 lines (243 loc) · 7.33 KB
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
/* -*- mode: C -*- Time-stamp: "2013-09-01 15:04:13 holzplatten"
*
* File: block.c
* Author: Pedro J. Ruiz Lopez (holzplatten@es.gnu.org)
* Date: Sun Jun 1 19:31:18 2013
*
* Manejo de los bloques de bajo nivel.
*
*/
/*
Copyright (C) 2013 Pedro J. Ruiz López <holzplatten@es.gnu.org>
This file is part of GnordoFS.
GnordoFS 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 3 of the License, or
(at your option) any later version.
GnordoFS 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 GnordoFS. If not, see <http://www.gnu.org/licenses/>.
*/
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <block.h>
#include <inode.h>
#include <superblock.h>
/*-
* Routine: allocblk
*
* Purpose:
* Reserva un nuevo bloque de datos.
* Conditions:
* dev debe corresponder a un gnordofs válido.
* sb debe apuntar a un superblock válido.
* Returns:
* Un entero con el número de bloque absoluto.
* -1 on error.
*
*/
long
allocblk(int dev, superblock_t * const sb)
{
block_t * buff;
int block;
block = sb->free_block_list[sb->free_block_index];
/* Si la lista parcial de bloques libres sólo contiene un bloque, anotar
su número y cargar la lista con lo que haya en el bloque al que apunta. */
if (sb->free_block_index == 0)
{
buff = getblk(dev, sb, block);
if (!buff)
return -1;
memcpy(sb->free_block_list, buff, FREE_BLOCK_LIST_SIZE*sizeof(unsigned long));
sb->free_block_index = FREE_BLOCK_LIST_SIZE;
free(buff);
}
sb->free_block_index--;
return block;
}
/*-
* Routine: freeblk
*
* Purpose:
* Añade a la lista de libres un bloque de datos.
* Conditions:
* dev debe corresponder a un gnordofs válido.
* sb debe apuntar a un superblock válido.
* block debe apuntar a un bloque absoluto.
* Returns:
* -1 on error.
*
*/
int
freeblk(int dev, superblock_t * const sb, long block)
{
block_t buff;
sb->free_block_index++;
/* Si la lista parcial de bloques libres está llena, guardarla en el nuevo
bloque libre. */
if (sb->free_block_index == FREE_BLOCK_LIST_SIZE)
{
memset(&buff, 0, sizeof(block_t));
memcpy(&buff, sb->free_block_list, FREE_BLOCK_LIST_SIZE*sizeof(unsigned long));
if (writeblk(dev, sb, block, &buff) < 0)
return -1;
sb->free_block_index = 0;
}
sb->free_block_list[sb->free_block_index] = block;
return 0;
}
/*-
* Routine: getblk
*
* Purpose:
* Lee un bloque de datos de disco.
* Conditions:
* dev debe corresponder a un gnordofs válido.
* sb debe apuntar a un superblock válido.
* n debe ser un número de bloque no negativo y VÁLIDO.
* Returns:
* Un bloque de datos.
* NULL on error.
*
*/
block_t *
getblk(int dev, superblock_t *sb, long n)
{
int offset;
block_t *datablock;
if (n<0)
return NULL;
offset = sb->block_zone_base + n * sizeof(struct block);
if (lseek(dev, offset, SEEK_SET) < 0)
return NULL;
datablock = malloc(sizeof(struct block));
if (datablock == NULL)
return NULL;
memset(datablock, 0, sizeof(struct block));
if (read(dev, datablock, sizeof(struct block)) < sizeof(struct block))
{
free(datablock);
return NULL;
}
return datablock;
}
/*-
* Routine: writeblk
*
* Purpose:
* Escribe un bloque de datos de disco.
* Conditions:
* dev debe corresponder a un gnordofs válido.
* sb debe apuntar a un superblock válido.
* n debe ser un número de bloque no negativo y VÁLIDO.
* datablock debe apuntar a un block_t válido.
* Returns:
* -1 on error.
*
*/
int
writeblk(int dev, superblock_t *sb, long n, block_t *datablock)
{
int offset;
if (n<0 || datablock==NULL)
return -1;
offset = sb->block_zone_base + n * sizeof(struct block);
if (lseek(dev, offset, SEEK_SET) < 0)
return -1;
if (write(dev, datablock, sizeof(struct block)) < sizeof(struct block))
return -1;
return 0;
}
/*-
* Routine: free_block_list_init
*
* Purpose:
* Inicializa una lista de inodos.
* Conditions:
* fd debe corresponder a un fichero abierto para escritura.
* sb debe apuntar a un superblock_t inicializado.
* Returns:
* 0 on success.
* -1 on error.
*
*/
int
free_block_list_init(int fd, const superblock_t * const sb)
{
unsigned int i;
unsigned int offset, block_count;
unsigned long sublist[FREE_BLOCK_LIST_SIZE], acc;
offset = sb->block_zone_base + sb->free_block_list[0] * sizeof(struct block);
acc = sb->free_block_list[0];
block_count = sb->block_count;
while(acc < block_count)
{
for (i=1; i<=FREE_BLOCK_LIST_SIZE; i++)
{
acc++;
sublist[FREE_BLOCK_LIST_SIZE-i] = acc;
}
lseek(fd, offset , SEEK_SET);
if (write(fd, &sublist, sizeof(sublist)) < sizeof(sublist))
return -1;
offset += FREE_BLOCK_LIST_SIZE * sizeof(struct block);
}
return 0;
}
/*-
* Routine: print_free_block_list
*
* Purpose:
* Muestra en pantalla un volcado de la lista de nodos libres.
* Conditions:
* fd debe corresponder a un fichero abierto para lectura y
* que corresponda a un sistema de archivos inicializado.
* sb debe apuntar al superblock_t correspondiente al sistema
* de archivos de fd.
* Returns:
* none
*
*/
void
print_free_block_list(int fd, const superblock_t * const sb)
{
unsigned int i;
unsigned int offset, block_count, next;
unsigned int block_zone_base;
unsigned long sublist[FREE_BLOCK_LIST_SIZE];
block_zone_base = sb->block_zone_base;
printf("Superblock free list: ");
for (i=0; i<FREE_BLOCK_LIST_SIZE/8-1; i++)
{
printf("%d,", sb->free_block_list[i]);
}
printf("%d...\n", sb->free_block_list[i]);
block_count = sb->block_count - FREE_BLOCK_LIST_SIZE;
next = sb->free_block_list[0];
offset = block_zone_base + next * sizeof(struct block);
while (block_count > 0)
{
printf("LEYENDO BLOQUE %u (0x%x): ", next, offset );
lseek(fd, offset, SEEK_SET);
if (read(fd, &sublist, sizeof(sublist)) < sizeof(sublist))
{
printf("########## Error! #########\n");
return;
}
for (i=0; i<FREE_BLOCK_LIST_SIZE/8-1; i++)
{
printf("%u,", sublist[i]);
}
printf("%u...\n", sublist[i]);
next = sublist[0];
offset = block_zone_base + next * sizeof(struct block);
block_count -= FREE_BLOCK_LIST_SIZE;
}
printf("End of list\n");
}