]>
Commit | Line | Data |
---|---|---|
a1596438 US |
1 | /* |
2 | * (C) Copyright 2011 - 2012 Samsung Electronics | |
3 | * EXT4 filesystem implementation in Uboot by | |
4 | * Uma Shankar <[email protected]> | |
5 | * Manjunatha C Achar <[email protected]> | |
6 | * | |
7 | * ext4ls and ext4load : Based on ext2 ls load support in Uboot. | |
8 | * | |
9 | * (C) Copyright 2004 | |
10 | * esd gmbh <www.esd-electronics.com> | |
11 | * Reinhard Arlt <[email protected]> | |
12 | * | |
13 | * based on code from grub2 fs/ext2.c and fs/fshelp.c by | |
14 | * GRUB -- GRand Unified Bootloader | |
15 | * Copyright (C) 2003, 2004 Free Software Foundation, Inc. | |
16 | * | |
ed34f34d US |
17 | * ext4write : Based on generic ext4 protocol. |
18 | * | |
1a459660 | 19 | * SPDX-License-Identifier: GPL-2.0+ |
a1596438 US |
20 | */ |
21 | ||
22 | #include <common.h> | |
23 | #include <ext_common.h> | |
24 | #include <ext4fs.h> | |
aac618a3 | 25 | #include <inttypes.h> |
a1596438 | 26 | #include <malloc.h> |
cf92e05c | 27 | #include <memalign.h> |
a1596438 US |
28 | #include <stddef.h> |
29 | #include <linux/stat.h> | |
30 | #include <linux/time.h> | |
31 | #include <asm/byteorder.h> | |
32 | #include "ext4_common.h" | |
33 | ||
34 | struct ext2_data *ext4fs_root; | |
35 | struct ext2fs_node *ext4fs_file; | |
36 | uint32_t *ext4fs_indir1_block; | |
37 | int ext4fs_indir1_size; | |
38 | int ext4fs_indir1_blkno = -1; | |
39 | uint32_t *ext4fs_indir2_block; | |
40 | int ext4fs_indir2_size; | |
41 | int ext4fs_indir2_blkno = -1; | |
42 | ||
43 | uint32_t *ext4fs_indir3_block; | |
44 | int ext4fs_indir3_size; | |
45 | int ext4fs_indir3_blkno = -1; | |
46 | struct ext2_inode *g_parent_inode; | |
47 | static int symlinknest; | |
48 | ||
03e2ecf6 | 49 | #if defined(CONFIG_EXT4_WRITE) |
ed34f34d US |
50 | uint32_t ext4fs_div_roundup(uint32_t size, uint32_t n) |
51 | { | |
52 | uint32_t res = size / n; | |
53 | if (res * n != size) | |
54 | res++; | |
55 | ||
56 | return res; | |
57 | } | |
58 | ||
59 | void put_ext4(uint64_t off, void *buf, uint32_t size) | |
60 | { | |
61 | uint64_t startblock; | |
62 | uint64_t remainder; | |
63 | unsigned char *temp_ptr = NULL; | |
ed34f34d | 64 | struct ext_filesystem *fs = get_fs(); |
50ce4c07 EE |
65 | int log2blksz = fs->dev_desc->log2blksz; |
66 | ALLOC_CACHE_ALIGN_BUFFER(unsigned char, sec_buf, fs->dev_desc->blksz); | |
ed34f34d | 67 | |
50ce4c07 | 68 | startblock = off >> log2blksz; |
ed34f34d | 69 | startblock += part_offset; |
50ce4c07 | 70 | remainder = off & (uint64_t)(fs->dev_desc->blksz - 1); |
ed34f34d US |
71 | |
72 | if (fs->dev_desc == NULL) | |
73 | return; | |
74 | ||
50ce4c07 | 75 | if ((startblock + (size >> log2blksz)) > |
ed34f34d | 76 | (part_offset + fs->total_sect)) { |
04735e9c | 77 | printf("part_offset is " LBAFU "\n", part_offset); |
aac618a3 | 78 | printf("total_sector is %" PRIu64 "\n", fs->total_sect); |
ed34f34d US |
79 | printf("error: overflow occurs\n"); |
80 | return; | |
81 | } | |
82 | ||
83 | if (remainder) { | |
84 | if (fs->dev_desc->block_read) { | |
85 | fs->dev_desc->block_read(fs->dev_desc->dev, | |
86 | startblock, 1, sec_buf); | |
87 | temp_ptr = sec_buf; | |
88 | memcpy((temp_ptr + remainder), | |
89 | (unsigned char *)buf, size); | |
90 | fs->dev_desc->block_write(fs->dev_desc->dev, | |
91 | startblock, 1, sec_buf); | |
92 | } | |
93 | } else { | |
50ce4c07 | 94 | if (size >> log2blksz != 0) { |
ed34f34d US |
95 | fs->dev_desc->block_write(fs->dev_desc->dev, |
96 | startblock, | |
50ce4c07 | 97 | size >> log2blksz, |
ed34f34d US |
98 | (unsigned long *)buf); |
99 | } else { | |
100 | fs->dev_desc->block_read(fs->dev_desc->dev, | |
101 | startblock, 1, sec_buf); | |
102 | temp_ptr = sec_buf; | |
103 | memcpy(temp_ptr, buf, size); | |
104 | fs->dev_desc->block_write(fs->dev_desc->dev, | |
105 | startblock, 1, | |
106 | (unsigned long *)sec_buf); | |
107 | } | |
108 | } | |
109 | } | |
110 | ||
111 | static int _get_new_inode_no(unsigned char *buffer) | |
112 | { | |
113 | struct ext_filesystem *fs = get_fs(); | |
114 | unsigned char input; | |
115 | int operand, status; | |
116 | int count = 1; | |
117 | int j = 0; | |
118 | ||
119 | /* get the blocksize of the filesystem */ | |
120 | unsigned char *ptr = buffer; | |
121 | while (*ptr == 255) { | |
122 | ptr++; | |
123 | count += 8; | |
124 | if (count > ext4fs_root->sblock.inodes_per_group) | |
125 | return -1; | |
126 | } | |
127 | ||
128 | for (j = 0; j < fs->blksz; j++) { | |
129 | input = *ptr; | |
130 | int i = 0; | |
131 | while (i <= 7) { | |
132 | operand = 1 << i; | |
133 | status = input & operand; | |
134 | if (status) { | |
135 | i++; | |
136 | count++; | |
137 | } else { | |
138 | *ptr |= operand; | |
139 | return count; | |
140 | } | |
141 | } | |
142 | ptr = ptr + 1; | |
143 | } | |
144 | ||
145 | return -1; | |
146 | } | |
147 | ||
148 | static int _get_new_blk_no(unsigned char *buffer) | |
149 | { | |
150 | unsigned char input; | |
151 | int operand, status; | |
152 | int count = 0; | |
153 | int j = 0; | |
154 | unsigned char *ptr = buffer; | |
155 | struct ext_filesystem *fs = get_fs(); | |
156 | ||
157 | if (fs->blksz != 1024) | |
158 | count = 0; | |
159 | else | |
160 | count = 1; | |
161 | ||
162 | while (*ptr == 255) { | |
163 | ptr++; | |
164 | count += 8; | |
165 | if (count == (fs->blksz * 8)) | |
166 | return -1; | |
167 | } | |
168 | ||
169 | for (j = 0; j < fs->blksz; j++) { | |
170 | input = *ptr; | |
171 | int i = 0; | |
172 | while (i <= 7) { | |
173 | operand = 1 << i; | |
174 | status = input & operand; | |
175 | if (status) { | |
176 | i++; | |
177 | count++; | |
178 | } else { | |
179 | *ptr |= operand; | |
180 | return count; | |
181 | } | |
182 | } | |
183 | ptr = ptr + 1; | |
184 | } | |
185 | ||
186 | return -1; | |
187 | } | |
188 | ||
189 | int ext4fs_set_block_bmap(long int blockno, unsigned char *buffer, int index) | |
190 | { | |
191 | int i, remainder, status; | |
192 | unsigned char *ptr = buffer; | |
193 | unsigned char operand; | |
194 | i = blockno / 8; | |
195 | remainder = blockno % 8; | |
196 | int blocksize = EXT2_BLOCK_SIZE(ext4fs_root); | |
197 | ||
198 | i = i - (index * blocksize); | |
199 | if (blocksize != 1024) { | |
200 | ptr = ptr + i; | |
201 | operand = 1 << remainder; | |
202 | status = *ptr & operand; | |
203 | if (status) | |
204 | return -1; | |
205 | ||
206 | *ptr = *ptr | operand; | |
207 | return 0; | |
208 | } else { | |
209 | if (remainder == 0) { | |
210 | ptr = ptr + i - 1; | |
211 | operand = (1 << 7); | |
212 | } else { | |
213 | ptr = ptr + i; | |
214 | operand = (1 << (remainder - 1)); | |
215 | } | |
216 | status = *ptr & operand; | |
217 | if (status) | |
218 | return -1; | |
219 | ||
220 | *ptr = *ptr | operand; | |
221 | return 0; | |
222 | } | |
223 | } | |
224 | ||
225 | void ext4fs_reset_block_bmap(long int blockno, unsigned char *buffer, int index) | |
226 | { | |
227 | int i, remainder, status; | |
228 | unsigned char *ptr = buffer; | |
229 | unsigned char operand; | |
230 | i = blockno / 8; | |
231 | remainder = blockno % 8; | |
232 | int blocksize = EXT2_BLOCK_SIZE(ext4fs_root); | |
233 | ||
234 | i = i - (index * blocksize); | |
235 | if (blocksize != 1024) { | |
236 | ptr = ptr + i; | |
237 | operand = (1 << remainder); | |
238 | status = *ptr & operand; | |
239 | if (status) | |
240 | *ptr = *ptr & ~(operand); | |
241 | } else { | |
242 | if (remainder == 0) { | |
243 | ptr = ptr + i - 1; | |
244 | operand = (1 << 7); | |
245 | } else { | |
246 | ptr = ptr + i; | |
247 | operand = (1 << (remainder - 1)); | |
248 | } | |
249 | status = *ptr & operand; | |
250 | if (status) | |
251 | *ptr = *ptr & ~(operand); | |
252 | } | |
253 | } | |
254 | ||
255 | int ext4fs_set_inode_bmap(int inode_no, unsigned char *buffer, int index) | |
256 | { | |
257 | int i, remainder, status; | |
258 | unsigned char *ptr = buffer; | |
259 | unsigned char operand; | |
260 | ||
261 | inode_no -= (index * ext4fs_root->sblock.inodes_per_group); | |
262 | i = inode_no / 8; | |
263 | remainder = inode_no % 8; | |
264 | if (remainder == 0) { | |
265 | ptr = ptr + i - 1; | |
266 | operand = (1 << 7); | |
267 | } else { | |
268 | ptr = ptr + i; | |
269 | operand = (1 << (remainder - 1)); | |
270 | } | |
271 | status = *ptr & operand; | |
272 | if (status) | |
273 | return -1; | |
274 | ||
275 | *ptr = *ptr | operand; | |
276 | ||
277 | return 0; | |
278 | } | |
279 | ||
280 | void ext4fs_reset_inode_bmap(int inode_no, unsigned char *buffer, int index) | |
281 | { | |
282 | int i, remainder, status; | |
283 | unsigned char *ptr = buffer; | |
284 | unsigned char operand; | |
285 | ||
286 | inode_no -= (index * ext4fs_root->sblock.inodes_per_group); | |
287 | i = inode_no / 8; | |
288 | remainder = inode_no % 8; | |
289 | if (remainder == 0) { | |
290 | ptr = ptr + i - 1; | |
291 | operand = (1 << 7); | |
292 | } else { | |
293 | ptr = ptr + i; | |
294 | operand = (1 << (remainder - 1)); | |
295 | } | |
296 | status = *ptr & operand; | |
297 | if (status) | |
298 | *ptr = *ptr & ~(operand); | |
299 | } | |
300 | ||
301 | int ext4fs_checksum_update(unsigned int i) | |
302 | { | |
303 | struct ext2_block_group *desc; | |
304 | struct ext_filesystem *fs = get_fs(); | |
305 | __u16 crc = 0; | |
306 | ||
73c15c63 | 307 | desc = (struct ext2_block_group *)&fs->bgd[i]; |
ed34f34d US |
308 | if (fs->sb->feature_ro_compat & EXT4_FEATURE_RO_COMPAT_GDT_CSUM) { |
309 | int offset = offsetof(struct ext2_block_group, bg_checksum); | |
310 | ||
311 | crc = ext2fs_crc16(~0, fs->sb->unique_id, | |
312 | sizeof(fs->sb->unique_id)); | |
313 | crc = ext2fs_crc16(crc, &i, sizeof(i)); | |
314 | crc = ext2fs_crc16(crc, desc, offset); | |
315 | offset += sizeof(desc->bg_checksum); /* skip checksum */ | |
316 | assert(offset == sizeof(*desc)); | |
317 | } | |
318 | ||
319 | return crc; | |
320 | } | |
321 | ||
322 | static int check_void_in_dentry(struct ext2_dirent *dir, char *filename) | |
323 | { | |
324 | int dentry_length; | |
325 | int sizeof_void_space; | |
326 | int new_entry_byte_reqd; | |
327 | short padding_factor = 0; | |
328 | ||
329 | if (dir->namelen % 4 != 0) | |
330 | padding_factor = 4 - (dir->namelen % 4); | |
331 | ||
332 | dentry_length = sizeof(struct ext2_dirent) + | |
333 | dir->namelen + padding_factor; | |
334 | sizeof_void_space = dir->direntlen - dentry_length; | |
335 | if (sizeof_void_space == 0) | |
336 | return 0; | |
337 | ||
338 | padding_factor = 0; | |
339 | if (strlen(filename) % 4 != 0) | |
340 | padding_factor = 4 - (strlen(filename) % 4); | |
341 | ||
342 | new_entry_byte_reqd = strlen(filename) + | |
343 | sizeof(struct ext2_dirent) + padding_factor; | |
344 | if (sizeof_void_space >= new_entry_byte_reqd) { | |
345 | dir->direntlen = dentry_length; | |
346 | return sizeof_void_space; | |
347 | } | |
348 | ||
349 | return 0; | |
350 | } | |
351 | ||
352 | void ext4fs_update_parent_dentry(char *filename, int *p_ino, int file_type) | |
353 | { | |
354 | unsigned int *zero_buffer = NULL; | |
355 | char *root_first_block_buffer = NULL; | |
356 | int direct_blk_idx; | |
357 | long int root_blknr; | |
358 | long int first_block_no_of_root = 0; | |
359 | long int previous_blknr = -1; | |
360 | int totalbytes = 0; | |
361 | short int padding_factor = 0; | |
362 | unsigned int new_entry_byte_reqd; | |
363 | unsigned int last_entry_dirlen; | |
364 | int sizeof_void_space = 0; | |
365 | int templength = 0; | |
366 | int inodeno; | |
367 | int status; | |
368 | struct ext_filesystem *fs = get_fs(); | |
369 | /* directory entry */ | |
370 | struct ext2_dirent *dir; | |
ed34f34d US |
371 | char *temp_dir = NULL; |
372 | ||
373 | zero_buffer = zalloc(fs->blksz); | |
374 | if (!zero_buffer) { | |
375 | printf("No Memory\n"); | |
376 | return; | |
377 | } | |
378 | root_first_block_buffer = zalloc(fs->blksz); | |
379 | if (!root_first_block_buffer) { | |
380 | free(zero_buffer); | |
381 | printf("No Memory\n"); | |
382 | return; | |
383 | } | |
384 | restart: | |
385 | ||
386 | /* read the block no allocated to a file */ | |
387 | for (direct_blk_idx = 0; direct_blk_idx < INDIRECT_BLOCKS; | |
388 | direct_blk_idx++) { | |
389 | root_blknr = read_allocated_block(g_parent_inode, | |
390 | direct_blk_idx); | |
391 | if (root_blknr == 0) { | |
392 | first_block_no_of_root = previous_blknr; | |
393 | break; | |
394 | } | |
395 | previous_blknr = root_blknr; | |
396 | } | |
397 | ||
04735e9c | 398 | status = ext4fs_devread((lbaint_t)first_block_no_of_root |
ed34f34d US |
399 | * fs->sect_perblk, |
400 | 0, fs->blksz, root_first_block_buffer); | |
401 | if (status == 0) | |
402 | goto fail; | |
403 | ||
404 | if (ext4fs_log_journal(root_first_block_buffer, first_block_no_of_root)) | |
405 | goto fail; | |
406 | dir = (struct ext2_dirent *)root_first_block_buffer; | |
ed34f34d US |
407 | totalbytes = 0; |
408 | while (dir->direntlen > 0) { | |
409 | /* | |
410 | * blocksize-totalbytes because last directory length | |
411 | * i.e. dir->direntlen is free availble space in the | |
412 | * block that means it is a last entry of directory | |
413 | * entry | |
414 | */ | |
415 | ||
416 | /* traversing the each directory entry */ | |
417 | if (fs->blksz - totalbytes == dir->direntlen) { | |
418 | if (strlen(filename) % 4 != 0) | |
419 | padding_factor = 4 - (strlen(filename) % 4); | |
420 | ||
421 | new_entry_byte_reqd = strlen(filename) + | |
422 | sizeof(struct ext2_dirent) + padding_factor; | |
423 | padding_factor = 0; | |
424 | /* | |
425 | * update last directory entry length to its | |
426 | * length because we are creating new directory | |
427 | * entry | |
428 | */ | |
429 | if (dir->namelen % 4 != 0) | |
430 | padding_factor = 4 - (dir->namelen % 4); | |
431 | ||
432 | last_entry_dirlen = dir->namelen + | |
433 | sizeof(struct ext2_dirent) + padding_factor; | |
434 | if ((fs->blksz - totalbytes - last_entry_dirlen) < | |
435 | new_entry_byte_reqd) { | |
436 | printf("1st Block Full:Allocate new block\n"); | |
437 | ||
438 | if (direct_blk_idx == INDIRECT_BLOCKS - 1) { | |
439 | printf("Directory exceeds limit\n"); | |
440 | goto fail; | |
441 | } | |
442 | g_parent_inode->b.blocks.dir_blocks | |
443 | [direct_blk_idx] = ext4fs_get_new_blk_no(); | |
444 | if (g_parent_inode->b.blocks.dir_blocks | |
445 | [direct_blk_idx] == -1) { | |
446 | printf("no block left to assign\n"); | |
447 | goto fail; | |
448 | } | |
449 | put_ext4(((uint64_t) | |
0550870b | 450 | ((uint64_t)g_parent_inode->b. |
ed34f34d | 451 | blocks.dir_blocks[direct_blk_idx] * |
0550870b | 452 | (uint64_t)fs->blksz)), zero_buffer, fs->blksz); |
ed34f34d US |
453 | g_parent_inode->size = |
454 | g_parent_inode->size + fs->blksz; | |
455 | g_parent_inode->blockcnt = | |
456 | g_parent_inode->blockcnt + fs->sect_perblk; | |
457 | if (ext4fs_put_metadata | |
458 | (root_first_block_buffer, | |
459 | first_block_no_of_root)) | |
460 | goto fail; | |
461 | goto restart; | |
462 | } | |
463 | dir->direntlen = last_entry_dirlen; | |
464 | break; | |
465 | } | |
466 | ||
467 | templength = dir->direntlen; | |
468 | totalbytes = totalbytes + templength; | |
469 | sizeof_void_space = check_void_in_dentry(dir, filename); | |
470 | if (sizeof_void_space) | |
471 | break; | |
472 | ||
473 | dir = (struct ext2_dirent *)((char *)dir + templength); | |
ed34f34d US |
474 | } |
475 | ||
476 | /* make a pointer ready for creating next directory entry */ | |
477 | templength = dir->direntlen; | |
478 | totalbytes = totalbytes + templength; | |
479 | dir = (struct ext2_dirent *)((char *)dir + templength); | |
ed34f34d US |
480 | |
481 | /* get the next available inode number */ | |
482 | inodeno = ext4fs_get_new_inode_no(); | |
483 | if (inodeno == -1) { | |
484 | printf("no inode left to assign\n"); | |
485 | goto fail; | |
486 | } | |
487 | dir->inode = inodeno; | |
488 | if (sizeof_void_space) | |
489 | dir->direntlen = sizeof_void_space; | |
490 | else | |
491 | dir->direntlen = fs->blksz - totalbytes; | |
492 | ||
493 | dir->namelen = strlen(filename); | |
494 | dir->filetype = FILETYPE_REG; /* regular file */ | |
495 | temp_dir = (char *)dir; | |
496 | temp_dir = temp_dir + sizeof(struct ext2_dirent); | |
497 | memcpy(temp_dir, filename, strlen(filename)); | |
498 | ||
499 | *p_ino = inodeno; | |
500 | ||
501 | /* update or write the 1st block of root inode */ | |
502 | if (ext4fs_put_metadata(root_first_block_buffer, | |
503 | first_block_no_of_root)) | |
504 | goto fail; | |
505 | ||
506 | fail: | |
507 | free(zero_buffer); | |
508 | free(root_first_block_buffer); | |
509 | } | |
510 | ||
511 | static int search_dir(struct ext2_inode *parent_inode, char *dirname) | |
512 | { | |
513 | int status; | |
514 | int inodeno; | |
515 | int totalbytes; | |
516 | int templength; | |
517 | int direct_blk_idx; | |
518 | long int blknr; | |
519 | int found = 0; | |
520 | char *ptr = NULL; | |
521 | unsigned char *block_buffer = NULL; | |
522 | struct ext2_dirent *dir = NULL; | |
523 | struct ext2_dirent *previous_dir = NULL; | |
524 | struct ext_filesystem *fs = get_fs(); | |
525 | ||
526 | /* read the block no allocated to a file */ | |
527 | for (direct_blk_idx = 0; direct_blk_idx < INDIRECT_BLOCKS; | |
528 | direct_blk_idx++) { | |
529 | blknr = read_allocated_block(parent_inode, direct_blk_idx); | |
530 | if (blknr == 0) | |
531 | goto fail; | |
532 | ||
533 | /* read the blocks of parenet inode */ | |
534 | block_buffer = zalloc(fs->blksz); | |
535 | if (!block_buffer) | |
536 | goto fail; | |
537 | ||
04735e9c | 538 | status = ext4fs_devread((lbaint_t)blknr * fs->sect_perblk, |
ed34f34d US |
539 | 0, fs->blksz, (char *)block_buffer); |
540 | if (status == 0) | |
541 | goto fail; | |
542 | ||
543 | dir = (struct ext2_dirent *)block_buffer; | |
544 | ptr = (char *)dir; | |
545 | totalbytes = 0; | |
546 | while (dir->direntlen >= 0) { | |
547 | /* | |
548 | * blocksize-totalbytes because last directory | |
549 | * length i.e.,*dir->direntlen is free availble | |
550 | * space in the block that means | |
551 | * it is a last entry of directory entry | |
552 | */ | |
553 | if (strlen(dirname) == dir->namelen) { | |
554 | if (strncmp(dirname, ptr + | |
555 | sizeof(struct ext2_dirent), | |
556 | dir->namelen) == 0) { | |
557 | previous_dir->direntlen += | |
558 | dir->direntlen; | |
559 | inodeno = dir->inode; | |
560 | dir->inode = 0; | |
561 | found = 1; | |
562 | break; | |
563 | } | |
564 | } | |
565 | ||
566 | if (fs->blksz - totalbytes == dir->direntlen) | |
567 | break; | |
568 | ||
569 | /* traversing the each directory entry */ | |
570 | templength = dir->direntlen; | |
571 | totalbytes = totalbytes + templength; | |
572 | previous_dir = dir; | |
573 | dir = (struct ext2_dirent *)((char *)dir + templength); | |
574 | ptr = (char *)dir; | |
575 | } | |
576 | ||
577 | if (found == 1) { | |
578 | free(block_buffer); | |
579 | block_buffer = NULL; | |
580 | return inodeno; | |
581 | } | |
582 | ||
583 | free(block_buffer); | |
584 | block_buffer = NULL; | |
585 | } | |
586 | ||
587 | fail: | |
588 | free(block_buffer); | |
589 | ||
590 | return -1; | |
591 | } | |
592 | ||
593 | static int find_dir_depth(char *dirname) | |
594 | { | |
595 | char *token = strtok(dirname, "/"); | |
596 | int count = 0; | |
597 | while (token != NULL) { | |
598 | token = strtok(NULL, "/"); | |
599 | count++; | |
600 | } | |
601 | return count + 1 + 1; | |
602 | /* | |
603 | * for example for string /home/temp | |
604 | * depth=home(1)+temp(1)+1 extra for NULL; | |
605 | * so count is 4; | |
606 | */ | |
607 | } | |
608 | ||
609 | static int parse_path(char **arr, char *dirname) | |
610 | { | |
611 | char *token = strtok(dirname, "/"); | |
612 | int i = 0; | |
613 | ||
614 | /* add root */ | |
615 | arr[i] = zalloc(strlen("/") + 1); | |
616 | if (!arr[i]) | |
617 | return -ENOMEM; | |
618 | ||
619 | arr[i++] = "/"; | |
620 | ||
621 | /* add each path entry after root */ | |
622 | while (token != NULL) { | |
623 | arr[i] = zalloc(strlen(token) + 1); | |
624 | if (!arr[i]) | |
625 | return -ENOMEM; | |
626 | memcpy(arr[i++], token, strlen(token)); | |
627 | token = strtok(NULL, "/"); | |
628 | } | |
629 | arr[i] = NULL; | |
630 | ||
631 | return 0; | |
632 | } | |
633 | ||
634 | int ext4fs_iget(int inode_no, struct ext2_inode *inode) | |
635 | { | |
636 | if (ext4fs_read_inode(ext4fs_root, inode_no, inode) == 0) | |
637 | return -1; | |
638 | ||
639 | return 0; | |
640 | } | |
641 | ||
642 | /* | |
643 | * Function: ext4fs_get_parent_inode_num | |
644 | * Return Value: inode Number of the parent directory of file/Directory to be | |
645 | * created | |
646 | * dirname : Input parmater, input path name of the file/directory to be created | |
647 | * dname : Output parameter, to be filled with the name of the directory | |
648 | * extracted from dirname | |
649 | */ | |
650 | int ext4fs_get_parent_inode_num(const char *dirname, char *dname, int flags) | |
651 | { | |
652 | int i; | |
653 | int depth = 0; | |
654 | int matched_inode_no; | |
655 | int result_inode_no = -1; | |
656 | char **ptr = NULL; | |
657 | char *depth_dirname = NULL; | |
658 | char *parse_dirname = NULL; | |
659 | struct ext2_inode *parent_inode = NULL; | |
660 | struct ext2_inode *first_inode = NULL; | |
661 | struct ext2_inode temp_inode; | |
662 | ||
663 | if (*dirname != '/') { | |
664 | printf("Please supply Absolute path\n"); | |
665 | return -1; | |
666 | } | |
667 | ||
668 | /* TODO: input validation make equivalent to linux */ | |
669 | depth_dirname = zalloc(strlen(dirname) + 1); | |
670 | if (!depth_dirname) | |
671 | return -ENOMEM; | |
672 | ||
673 | memcpy(depth_dirname, dirname, strlen(dirname)); | |
674 | depth = find_dir_depth(depth_dirname); | |
675 | parse_dirname = zalloc(strlen(dirname) + 1); | |
676 | if (!parse_dirname) | |
677 | goto fail; | |
678 | memcpy(parse_dirname, dirname, strlen(dirname)); | |
679 | ||
680 | /* allocate memory for each directory level */ | |
681 | ptr = zalloc((depth) * sizeof(char *)); | |
682 | if (!ptr) | |
683 | goto fail; | |
684 | if (parse_path(ptr, parse_dirname)) | |
685 | goto fail; | |
686 | parent_inode = zalloc(sizeof(struct ext2_inode)); | |
687 | if (!parent_inode) | |
688 | goto fail; | |
689 | first_inode = zalloc(sizeof(struct ext2_inode)); | |
690 | if (!first_inode) | |
691 | goto fail; | |
692 | memcpy(parent_inode, ext4fs_root->inode, sizeof(struct ext2_inode)); | |
693 | memcpy(first_inode, parent_inode, sizeof(struct ext2_inode)); | |
694 | if (flags & F_FILE) | |
695 | result_inode_no = EXT2_ROOT_INO; | |
696 | for (i = 1; i < depth; i++) { | |
697 | matched_inode_no = search_dir(parent_inode, ptr[i]); | |
698 | if (matched_inode_no == -1) { | |
699 | if (ptr[i + 1] == NULL && i == 1) { | |
700 | result_inode_no = EXT2_ROOT_INO; | |
701 | goto end; | |
702 | } else { | |
703 | if (ptr[i + 1] == NULL) | |
704 | break; | |
705 | printf("Invalid path\n"); | |
706 | result_inode_no = -1; | |
707 | goto fail; | |
708 | } | |
709 | } else { | |
710 | if (ptr[i + 1] != NULL) { | |
711 | memset(parent_inode, '\0', | |
712 | sizeof(struct ext2_inode)); | |
713 | if (ext4fs_iget(matched_inode_no, | |
714 | parent_inode)) { | |
715 | result_inode_no = -1; | |
716 | goto fail; | |
717 | } | |
718 | result_inode_no = matched_inode_no; | |
719 | } else { | |
720 | break; | |
721 | } | |
722 | } | |
723 | } | |
724 | ||
725 | end: | |
726 | if (i == 1) | |
727 | matched_inode_no = search_dir(first_inode, ptr[i]); | |
728 | else | |
729 | matched_inode_no = search_dir(parent_inode, ptr[i]); | |
730 | ||
731 | if (matched_inode_no != -1) { | |
732 | ext4fs_iget(matched_inode_no, &temp_inode); | |
733 | if (temp_inode.mode & S_IFDIR) { | |
734 | printf("It is a Directory\n"); | |
735 | result_inode_no = -1; | |
736 | goto fail; | |
737 | } | |
738 | } | |
739 | ||
740 | if (strlen(ptr[i]) > 256) { | |
741 | result_inode_no = -1; | |
742 | goto fail; | |
743 | } | |
744 | memcpy(dname, ptr[i], strlen(ptr[i])); | |
745 | ||
746 | fail: | |
747 | free(depth_dirname); | |
748 | free(parse_dirname); | |
749 | free(ptr); | |
750 | free(parent_inode); | |
751 | free(first_inode); | |
752 | ||
753 | return result_inode_no; | |
754 | } | |
755 | ||
756 | static int check_filename(char *filename, unsigned int blknr) | |
757 | { | |
758 | unsigned int first_block_no_of_root; | |
759 | int totalbytes = 0; | |
760 | int templength = 0; | |
761 | int status, inodeno; | |
762 | int found = 0; | |
763 | char *root_first_block_buffer = NULL; | |
764 | char *root_first_block_addr = NULL; | |
765 | struct ext2_dirent *dir = NULL; | |
766 | struct ext2_dirent *previous_dir = NULL; | |
767 | char *ptr = NULL; | |
768 | struct ext_filesystem *fs = get_fs(); | |
769 | ||
770 | /* get the first block of root */ | |
771 | first_block_no_of_root = blknr; | |
772 | root_first_block_buffer = zalloc(fs->blksz); | |
773 | if (!root_first_block_buffer) | |
774 | return -ENOMEM; | |
775 | root_first_block_addr = root_first_block_buffer; | |
04735e9c | 776 | status = ext4fs_devread((lbaint_t)first_block_no_of_root * |
ed34f34d US |
777 | fs->sect_perblk, 0, |
778 | fs->blksz, root_first_block_buffer); | |
779 | if (status == 0) | |
780 | goto fail; | |
781 | ||
782 | if (ext4fs_log_journal(root_first_block_buffer, first_block_no_of_root)) | |
783 | goto fail; | |
784 | dir = (struct ext2_dirent *)root_first_block_buffer; | |
785 | ptr = (char *)dir; | |
786 | totalbytes = 0; | |
787 | while (dir->direntlen >= 0) { | |
788 | /* | |
789 | * blocksize-totalbytes because last | |
790 | * directory length i.e., *dir->direntlen | |
791 | * is free availble space in the block that | |
792 | * means it is a last entry of directory entry | |
793 | */ | |
794 | if (strlen(filename) == dir->namelen) { | |
795 | if (strncmp(filename, ptr + sizeof(struct ext2_dirent), | |
796 | dir->namelen) == 0) { | |
797 | printf("file found deleting\n"); | |
798 | previous_dir->direntlen += dir->direntlen; | |
799 | inodeno = dir->inode; | |
800 | dir->inode = 0; | |
801 | found = 1; | |
802 | break; | |
803 | } | |
804 | } | |
805 | ||
806 | if (fs->blksz - totalbytes == dir->direntlen) | |
807 | break; | |
808 | ||
809 | /* traversing the each directory entry */ | |
810 | templength = dir->direntlen; | |
811 | totalbytes = totalbytes + templength; | |
812 | previous_dir = dir; | |
813 | dir = (struct ext2_dirent *)((char *)dir + templength); | |
814 | ptr = (char *)dir; | |
815 | } | |
816 | ||
817 | ||
818 | if (found == 1) { | |
819 | if (ext4fs_put_metadata(root_first_block_addr, | |
820 | first_block_no_of_root)) | |
821 | goto fail; | |
822 | return inodeno; | |
823 | } | |
824 | fail: | |
825 | free(root_first_block_buffer); | |
826 | ||
827 | return -1; | |
828 | } | |
829 | ||
830 | int ext4fs_filename_check(char *filename) | |
831 | { | |
832 | short direct_blk_idx = 0; | |
833 | long int blknr = -1; | |
834 | int inodeno = -1; | |
835 | ||
836 | /* read the block no allocated to a file */ | |
837 | for (direct_blk_idx = 0; direct_blk_idx < INDIRECT_BLOCKS; | |
838 | direct_blk_idx++) { | |
839 | blknr = read_allocated_block(g_parent_inode, direct_blk_idx); | |
840 | if (blknr == 0) | |
841 | break; | |
842 | inodeno = check_filename(filename, blknr); | |
843 | if (inodeno != -1) | |
844 | return inodeno; | |
845 | } | |
846 | ||
847 | return -1; | |
848 | } | |
849 | ||
850 | long int ext4fs_get_new_blk_no(void) | |
851 | { | |
852 | short i; | |
853 | short status; | |
854 | int remainder; | |
855 | unsigned int bg_idx; | |
856 | static int prev_bg_bitmap_index = -1; | |
857 | unsigned int blk_per_grp = ext4fs_root->sblock.blocks_per_group; | |
858 | struct ext_filesystem *fs = get_fs(); | |
859 | char *journal_buffer = zalloc(fs->blksz); | |
860 | char *zero_buffer = zalloc(fs->blksz); | |
861 | if (!journal_buffer || !zero_buffer) | |
862 | goto fail; | |
73c15c63 | 863 | struct ext2_block_group *bgd = (struct ext2_block_group *)fs->gdtable; |
ed34f34d US |
864 | |
865 | if (fs->first_pass_bbmap == 0) { | |
866 | for (i = 0; i < fs->no_blkgrp; i++) { | |
73c15c63 SG |
867 | if (bgd[i].free_blocks) { |
868 | if (bgd[i].bg_flags & EXT4_BG_BLOCK_UNINIT) { | |
0550870b MH |
869 | put_ext4(((uint64_t) ((uint64_t)bgd[i].block_id * |
870 | (uint64_t)fs->blksz)), | |
ed34f34d | 871 | zero_buffer, fs->blksz); |
73c15c63 SG |
872 | bgd[i].bg_flags = |
873 | bgd[i]. | |
ed34f34d US |
874 | bg_flags & ~EXT4_BG_BLOCK_UNINIT; |
875 | memcpy(fs->blk_bmaps[i], zero_buffer, | |
876 | fs->blksz); | |
877 | } | |
878 | fs->curr_blkno = | |
879 | _get_new_blk_no(fs->blk_bmaps[i]); | |
880 | if (fs->curr_blkno == -1) | |
881 | /* if block bitmap is completely fill */ | |
882 | continue; | |
883 | fs->curr_blkno = fs->curr_blkno + | |
884 | (i * fs->blksz * 8); | |
885 | fs->first_pass_bbmap++; | |
73c15c63 | 886 | bgd[i].free_blocks--; |
ed34f34d | 887 | fs->sb->free_blocks--; |
04735e9c FL |
888 | status = ext4fs_devread((lbaint_t) |
889 | bgd[i].block_id * | |
ed34f34d US |
890 | fs->sect_perblk, 0, |
891 | fs->blksz, | |
892 | journal_buffer); | |
893 | if (status == 0) | |
894 | goto fail; | |
895 | if (ext4fs_log_journal(journal_buffer, | |
73c15c63 | 896 | bgd[i].block_id)) |
ed34f34d US |
897 | goto fail; |
898 | goto success; | |
899 | } else { | |
900 | debug("no space left on block group %d\n", i); | |
901 | } | |
902 | } | |
903 | ||
904 | goto fail; | |
905 | } else { | |
906 | restart: | |
907 | fs->curr_blkno++; | |
908 | /* get the blockbitmap index respective to blockno */ | |
35dd055b ŁM |
909 | bg_idx = fs->curr_blkno / blk_per_grp; |
910 | if (fs->blksz == 1024) { | |
ed34f34d US |
911 | remainder = fs->curr_blkno % blk_per_grp; |
912 | if (!remainder) | |
913 | bg_idx--; | |
914 | } | |
915 | ||
916 | /* | |
917 | * To skip completely filled block group bitmaps | |
918 | * Optimize the block allocation | |
919 | */ | |
920 | if (bg_idx >= fs->no_blkgrp) | |
921 | goto fail; | |
922 | ||
73c15c63 | 923 | if (bgd[bg_idx].free_blocks == 0) { |
ed34f34d US |
924 | debug("block group %u is full. Skipping\n", bg_idx); |
925 | fs->curr_blkno = fs->curr_blkno + blk_per_grp; | |
926 | fs->curr_blkno--; | |
927 | goto restart; | |
928 | } | |
929 | ||
73c15c63 | 930 | if (bgd[bg_idx].bg_flags & EXT4_BG_BLOCK_UNINIT) { |
ed34f34d | 931 | memset(zero_buffer, '\0', fs->blksz); |
0550870b MH |
932 | put_ext4(((uint64_t) ((uint64_t)bgd[bg_idx].block_id * |
933 | (uint64_t)fs->blksz)), zero_buffer, fs->blksz); | |
ed34f34d | 934 | memcpy(fs->blk_bmaps[bg_idx], zero_buffer, fs->blksz); |
73c15c63 | 935 | bgd[bg_idx].bg_flags = bgd[bg_idx].bg_flags & |
ed34f34d US |
936 | ~EXT4_BG_BLOCK_UNINIT; |
937 | } | |
938 | ||
939 | if (ext4fs_set_block_bmap(fs->curr_blkno, fs->blk_bmaps[bg_idx], | |
940 | bg_idx) != 0) { | |
941 | debug("going for restart for the block no %ld %u\n", | |
942 | fs->curr_blkno, bg_idx); | |
943 | goto restart; | |
944 | } | |
945 | ||
946 | /* journal backup */ | |
947 | if (prev_bg_bitmap_index != bg_idx) { | |
948 | memset(journal_buffer, '\0', fs->blksz); | |
04735e9c | 949 | status = ext4fs_devread((lbaint_t)bgd[bg_idx].block_id |
ed34f34d US |
950 | * fs->sect_perblk, |
951 | 0, fs->blksz, journal_buffer); | |
952 | if (status == 0) | |
953 | goto fail; | |
954 | if (ext4fs_log_journal(journal_buffer, | |
73c15c63 | 955 | bgd[bg_idx].block_id)) |
ed34f34d US |
956 | goto fail; |
957 | ||
958 | prev_bg_bitmap_index = bg_idx; | |
959 | } | |
73c15c63 | 960 | bgd[bg_idx].free_blocks--; |
ed34f34d US |
961 | fs->sb->free_blocks--; |
962 | goto success; | |
963 | } | |
964 | success: | |
965 | free(journal_buffer); | |
966 | free(zero_buffer); | |
967 | ||
968 | return fs->curr_blkno; | |
969 | fail: | |
970 | free(journal_buffer); | |
971 | free(zero_buffer); | |
972 | ||
973 | return -1; | |
974 | } | |
975 | ||
976 | int ext4fs_get_new_inode_no(void) | |
977 | { | |
978 | short i; | |
979 | short status; | |
980 | unsigned int ibmap_idx; | |
981 | static int prev_inode_bitmap_index = -1; | |
982 | unsigned int inodes_per_grp = ext4fs_root->sblock.inodes_per_group; | |
983 | struct ext_filesystem *fs = get_fs(); | |
984 | char *journal_buffer = zalloc(fs->blksz); | |
985 | char *zero_buffer = zalloc(fs->blksz); | |
986 | if (!journal_buffer || !zero_buffer) | |
987 | goto fail; | |
73c15c63 | 988 | struct ext2_block_group *bgd = (struct ext2_block_group *)fs->gdtable; |
ed34f34d US |
989 | |
990 | if (fs->first_pass_ibmap == 0) { | |
991 | for (i = 0; i < fs->no_blkgrp; i++) { | |
73c15c63 SG |
992 | if (bgd[i].free_inodes) { |
993 | if (bgd[i].bg_itable_unused != | |
994 | bgd[i].free_inodes) | |
995 | bgd[i].bg_itable_unused = | |
996 | bgd[i].free_inodes; | |
997 | if (bgd[i].bg_flags & EXT4_BG_INODE_UNINIT) { | |
ed34f34d | 998 | put_ext4(((uint64_t) |
0550870b MH |
999 | ((uint64_t)bgd[i].inode_id * |
1000 | (uint64_t)fs->blksz)), | |
ed34f34d | 1001 | zero_buffer, fs->blksz); |
73c15c63 | 1002 | bgd[i].bg_flags = bgd[i].bg_flags & |
ed34f34d US |
1003 | ~EXT4_BG_INODE_UNINIT; |
1004 | memcpy(fs->inode_bmaps[i], | |
1005 | zero_buffer, fs->blksz); | |
1006 | } | |
1007 | fs->curr_inode_no = | |
1008 | _get_new_inode_no(fs->inode_bmaps[i]); | |
1009 | if (fs->curr_inode_no == -1) | |
1010 | /* if block bitmap is completely fill */ | |
1011 | continue; | |
1012 | fs->curr_inode_no = fs->curr_inode_no + | |
1013 | (i * inodes_per_grp); | |
1014 | fs->first_pass_ibmap++; | |
73c15c63 SG |
1015 | bgd[i].free_inodes--; |
1016 | bgd[i].bg_itable_unused--; | |
ed34f34d | 1017 | fs->sb->free_inodes--; |
04735e9c FL |
1018 | status = ext4fs_devread((lbaint_t) |
1019 | bgd[i].inode_id * | |
ed34f34d US |
1020 | fs->sect_perblk, 0, |
1021 | fs->blksz, | |
1022 | journal_buffer); | |
1023 | if (status == 0) | |
1024 | goto fail; | |
1025 | if (ext4fs_log_journal(journal_buffer, | |
73c15c63 | 1026 | bgd[i].inode_id)) |
ed34f34d US |
1027 | goto fail; |
1028 | goto success; | |
1029 | } else | |
1030 | debug("no inode left on block group %d\n", i); | |
1031 | } | |
1032 | goto fail; | |
1033 | } else { | |
1034 | restart: | |
1035 | fs->curr_inode_no++; | |
1036 | /* get the blockbitmap index respective to blockno */ | |
1037 | ibmap_idx = fs->curr_inode_no / inodes_per_grp; | |
73c15c63 | 1038 | if (bgd[ibmap_idx].bg_flags & EXT4_BG_INODE_UNINIT) { |
ed34f34d | 1039 | memset(zero_buffer, '\0', fs->blksz); |
0550870b MH |
1040 | put_ext4(((uint64_t) ((uint64_t)bgd[ibmap_idx].inode_id * |
1041 | (uint64_t)fs->blksz)), zero_buffer, | |
ed34f34d | 1042 | fs->blksz); |
73c15c63 SG |
1043 | bgd[ibmap_idx].bg_flags = |
1044 | bgd[ibmap_idx].bg_flags & ~EXT4_BG_INODE_UNINIT; | |
ed34f34d US |
1045 | memcpy(fs->inode_bmaps[ibmap_idx], zero_buffer, |
1046 | fs->blksz); | |
1047 | } | |
1048 | ||
1049 | if (ext4fs_set_inode_bmap(fs->curr_inode_no, | |
1050 | fs->inode_bmaps[ibmap_idx], | |
1051 | ibmap_idx) != 0) { | |
1052 | debug("going for restart for the block no %d %u\n", | |
1053 | fs->curr_inode_no, ibmap_idx); | |
1054 | goto restart; | |
1055 | } | |
1056 | ||
1057 | /* journal backup */ | |
1058 | if (prev_inode_bitmap_index != ibmap_idx) { | |
1059 | memset(journal_buffer, '\0', fs->blksz); | |
04735e9c FL |
1060 | status = ext4fs_devread((lbaint_t) |
1061 | bgd[ibmap_idx].inode_id | |
ed34f34d US |
1062 | * fs->sect_perblk, |
1063 | 0, fs->blksz, journal_buffer); | |
1064 | if (status == 0) | |
1065 | goto fail; | |
1066 | if (ext4fs_log_journal(journal_buffer, | |
73c15c63 | 1067 | bgd[ibmap_idx].inode_id)) |
ed34f34d US |
1068 | goto fail; |
1069 | prev_inode_bitmap_index = ibmap_idx; | |
1070 | } | |
73c15c63 SG |
1071 | if (bgd[ibmap_idx].bg_itable_unused != |
1072 | bgd[ibmap_idx].free_inodes) | |
1073 | bgd[ibmap_idx].bg_itable_unused = | |
1074 | bgd[ibmap_idx].free_inodes; | |
1075 | bgd[ibmap_idx].free_inodes--; | |
1076 | bgd[ibmap_idx].bg_itable_unused--; | |
ed34f34d US |
1077 | fs->sb->free_inodes--; |
1078 | goto success; | |
1079 | } | |
1080 | ||
1081 | success: | |
1082 | free(journal_buffer); | |
1083 | free(zero_buffer); | |
1084 | ||
1085 | return fs->curr_inode_no; | |
1086 | fail: | |
1087 | free(journal_buffer); | |
1088 | free(zero_buffer); | |
1089 | ||
1090 | return -1; | |
1091 | ||
1092 | } | |
1093 | ||
1094 | ||
1095 | static void alloc_single_indirect_block(struct ext2_inode *file_inode, | |
1096 | unsigned int *total_remaining_blocks, | |
1097 | unsigned int *no_blks_reqd) | |
1098 | { | |
1099 | short i; | |
1100 | short status; | |
1101 | long int actual_block_no; | |
1102 | long int si_blockno; | |
1103 | /* si :single indirect */ | |
1104 | unsigned int *si_buffer = NULL; | |
1105 | unsigned int *si_start_addr = NULL; | |
1106 | struct ext_filesystem *fs = get_fs(); | |
1107 | ||
1108 | if (*total_remaining_blocks != 0) { | |
1109 | si_buffer = zalloc(fs->blksz); | |
1110 | if (!si_buffer) { | |
1111 | printf("No Memory\n"); | |
1112 | return; | |
1113 | } | |
1114 | si_start_addr = si_buffer; | |
1115 | si_blockno = ext4fs_get_new_blk_no(); | |
1116 | if (si_blockno == -1) { | |
1117 | printf("no block left to assign\n"); | |
1118 | goto fail; | |
1119 | } | |
1120 | (*no_blks_reqd)++; | |
1121 | debug("SIPB %ld: %u\n", si_blockno, *total_remaining_blocks); | |
1122 | ||
04735e9c | 1123 | status = ext4fs_devread((lbaint_t)si_blockno * fs->sect_perblk, |
ed34f34d US |
1124 | 0, fs->blksz, (char *)si_buffer); |
1125 | memset(si_buffer, '\0', fs->blksz); | |
1126 | if (status == 0) | |
1127 | goto fail; | |
1128 | ||
1129 | for (i = 0; i < (fs->blksz / sizeof(int)); i++) { | |
1130 | actual_block_no = ext4fs_get_new_blk_no(); | |
1131 | if (actual_block_no == -1) { | |
1132 | printf("no block left to assign\n"); | |
1133 | goto fail; | |
1134 | } | |
1135 | *si_buffer = actual_block_no; | |
1136 | debug("SIAB %u: %u\n", *si_buffer, | |
1137 | *total_remaining_blocks); | |
1138 | ||
1139 | si_buffer++; | |
1140 | (*total_remaining_blocks)--; | |
1141 | if (*total_remaining_blocks == 0) | |
1142 | break; | |
1143 | } | |
1144 | ||
1145 | /* write the block to disk */ | |
0550870b | 1146 | put_ext4(((uint64_t) ((uint64_t)si_blockno * (uint64_t)fs->blksz)), |
ed34f34d US |
1147 | si_start_addr, fs->blksz); |
1148 | file_inode->b.blocks.indir_block = si_blockno; | |
1149 | } | |
1150 | fail: | |
1151 | free(si_start_addr); | |
1152 | } | |
1153 | ||
1154 | static void alloc_double_indirect_block(struct ext2_inode *file_inode, | |
1155 | unsigned int *total_remaining_blocks, | |
1156 | unsigned int *no_blks_reqd) | |
1157 | { | |
1158 | short i; | |
1159 | short j; | |
1160 | short status; | |
1161 | long int actual_block_no; | |
1162 | /* di:double indirect */ | |
1163 | long int di_blockno_parent; | |
1164 | long int di_blockno_child; | |
1165 | unsigned int *di_parent_buffer = NULL; | |
1166 | unsigned int *di_child_buff = NULL; | |
1167 | unsigned int *di_block_start_addr = NULL; | |
1168 | unsigned int *di_child_buff_start = NULL; | |
1169 | struct ext_filesystem *fs = get_fs(); | |
1170 | ||
1171 | if (*total_remaining_blocks != 0) { | |
1172 | /* double indirect parent block connecting to inode */ | |
1173 | di_blockno_parent = ext4fs_get_new_blk_no(); | |
1174 | if (di_blockno_parent == -1) { | |
1175 | printf("no block left to assign\n"); | |
1176 | goto fail; | |
1177 | } | |
1178 | di_parent_buffer = zalloc(fs->blksz); | |
1179 | if (!di_parent_buffer) | |
1180 | goto fail; | |
1181 | ||
1182 | di_block_start_addr = di_parent_buffer; | |
1183 | (*no_blks_reqd)++; | |
1184 | debug("DIPB %ld: %u\n", di_blockno_parent, | |
1185 | *total_remaining_blocks); | |
1186 | ||
04735e9c | 1187 | status = ext4fs_devread((lbaint_t)di_blockno_parent * |
ed34f34d US |
1188 | fs->sect_perblk, 0, |
1189 | fs->blksz, (char *)di_parent_buffer); | |
d429ca0d ŁM |
1190 | |
1191 | if (!status) { | |
1192 | printf("%s: Device read error!\n", __func__); | |
1193 | goto fail; | |
1194 | } | |
ed34f34d US |
1195 | memset(di_parent_buffer, '\0', fs->blksz); |
1196 | ||
1197 | /* | |
1198 | * start:for each double indirect parent | |
1199 | * block create one more block | |
1200 | */ | |
1201 | for (i = 0; i < (fs->blksz / sizeof(int)); i++) { | |
1202 | di_blockno_child = ext4fs_get_new_blk_no(); | |
1203 | if (di_blockno_child == -1) { | |
1204 | printf("no block left to assign\n"); | |
1205 | goto fail; | |
1206 | } | |
1207 | di_child_buff = zalloc(fs->blksz); | |
1208 | if (!di_child_buff) | |
1209 | goto fail; | |
1210 | ||
1211 | di_child_buff_start = di_child_buff; | |
1212 | *di_parent_buffer = di_blockno_child; | |
1213 | di_parent_buffer++; | |
1214 | (*no_blks_reqd)++; | |
1215 | debug("DICB %ld: %u\n", di_blockno_child, | |
1216 | *total_remaining_blocks); | |
1217 | ||
04735e9c | 1218 | status = ext4fs_devread((lbaint_t)di_blockno_child * |
ed34f34d US |
1219 | fs->sect_perblk, 0, |
1220 | fs->blksz, | |
1221 | (char *)di_child_buff); | |
d429ca0d ŁM |
1222 | |
1223 | if (!status) { | |
1224 | printf("%s: Device read error!\n", __func__); | |
1225 | goto fail; | |
1226 | } | |
ed34f34d US |
1227 | memset(di_child_buff, '\0', fs->blksz); |
1228 | /* filling of actual datablocks for each child */ | |
1229 | for (j = 0; j < (fs->blksz / sizeof(int)); j++) { | |
1230 | actual_block_no = ext4fs_get_new_blk_no(); | |
1231 | if (actual_block_no == -1) { | |
1232 | printf("no block left to assign\n"); | |
1233 | goto fail; | |
1234 | } | |
1235 | *di_child_buff = actual_block_no; | |
1236 | debug("DIAB %ld: %u\n", actual_block_no, | |
1237 | *total_remaining_blocks); | |
1238 | ||
1239 | di_child_buff++; | |
1240 | (*total_remaining_blocks)--; | |
1241 | if (*total_remaining_blocks == 0) | |
1242 | break; | |
1243 | } | |
1244 | /* write the block table */ | |
0550870b | 1245 | put_ext4(((uint64_t) ((uint64_t)di_blockno_child * (uint64_t)fs->blksz)), |
ed34f34d US |
1246 | di_child_buff_start, fs->blksz); |
1247 | free(di_child_buff_start); | |
1248 | di_child_buff_start = NULL; | |
1249 | ||
1250 | if (*total_remaining_blocks == 0) | |
1251 | break; | |
1252 | } | |
0550870b | 1253 | put_ext4(((uint64_t) ((uint64_t)di_blockno_parent * (uint64_t)fs->blksz)), |
ed34f34d US |
1254 | di_block_start_addr, fs->blksz); |
1255 | file_inode->b.blocks.double_indir_block = di_blockno_parent; | |
1256 | } | |
1257 | fail: | |
1258 | free(di_block_start_addr); | |
1259 | } | |
1260 | ||
1261 | static void alloc_triple_indirect_block(struct ext2_inode *file_inode, | |
1262 | unsigned int *total_remaining_blocks, | |
1263 | unsigned int *no_blks_reqd) | |
1264 | { | |
1265 | short i; | |
1266 | short j; | |
1267 | short k; | |
1268 | long int actual_block_no; | |
1269 | /* ti: Triple Indirect */ | |
1270 | long int ti_gp_blockno; | |
1271 | long int ti_parent_blockno; | |
1272 | long int ti_child_blockno; | |
1273 | unsigned int *ti_gp_buff = NULL; | |
1274 | unsigned int *ti_parent_buff = NULL; | |
1275 | unsigned int *ti_child_buff = NULL; | |
1276 | unsigned int *ti_gp_buff_start_addr = NULL; | |
1277 | unsigned int *ti_pbuff_start_addr = NULL; | |
1278 | unsigned int *ti_cbuff_start_addr = NULL; | |
1279 | struct ext_filesystem *fs = get_fs(); | |
1280 | if (*total_remaining_blocks != 0) { | |
1281 | /* triple indirect grand parent block connecting to inode */ | |
1282 | ti_gp_blockno = ext4fs_get_new_blk_no(); | |
1283 | if (ti_gp_blockno == -1) { | |
1284 | printf("no block left to assign\n"); | |
1285 | goto fail; | |
1286 | } | |
1287 | ti_gp_buff = zalloc(fs->blksz); | |
1288 | if (!ti_gp_buff) | |
1289 | goto fail; | |
1290 | ||
1291 | ti_gp_buff_start_addr = ti_gp_buff; | |
1292 | (*no_blks_reqd)++; | |
1293 | debug("TIGPB %ld: %u\n", ti_gp_blockno, | |
1294 | *total_remaining_blocks); | |
1295 | ||
1296 | /* for each 4 byte grand parent entry create one more block */ | |
1297 | for (i = 0; i < (fs->blksz / sizeof(int)); i++) { | |
1298 | ti_parent_blockno = ext4fs_get_new_blk_no(); | |
1299 | if (ti_parent_blockno == -1) { | |
1300 | printf("no block left to assign\n"); | |
1301 | goto fail; | |
1302 | } | |
1303 | ti_parent_buff = zalloc(fs->blksz); | |
1304 | if (!ti_parent_buff) | |
1305 | goto fail; | |
1306 | ||
1307 | ti_pbuff_start_addr = ti_parent_buff; | |
1308 | *ti_gp_buff = ti_parent_blockno; | |
1309 | ti_gp_buff++; | |
1310 | (*no_blks_reqd)++; | |
1311 | debug("TIPB %ld: %u\n", ti_parent_blockno, | |
1312 | *total_remaining_blocks); | |
1313 | ||
1314 | /* for each 4 byte entry parent create one more block */ | |
1315 | for (j = 0; j < (fs->blksz / sizeof(int)); j++) { | |
1316 | ti_child_blockno = ext4fs_get_new_blk_no(); | |
1317 | if (ti_child_blockno == -1) { | |
1318 | printf("no block left assign\n"); | |
1319 | goto fail; | |
1320 | } | |
1321 | ti_child_buff = zalloc(fs->blksz); | |
1322 | if (!ti_child_buff) | |
1323 | goto fail; | |
1324 | ||
1325 | ti_cbuff_start_addr = ti_child_buff; | |
1326 | *ti_parent_buff = ti_child_blockno; | |
1327 | ti_parent_buff++; | |
1328 | (*no_blks_reqd)++; | |
1329 | debug("TICB %ld: %u\n", ti_parent_blockno, | |
1330 | *total_remaining_blocks); | |
1331 | ||
1332 | /* fill actual datablocks for each child */ | |
1333 | for (k = 0; k < (fs->blksz / sizeof(int)); | |
1334 | k++) { | |
1335 | actual_block_no = | |
1336 | ext4fs_get_new_blk_no(); | |
1337 | if (actual_block_no == -1) { | |
1338 | printf("no block left\n"); | |
1339 | goto fail; | |
1340 | } | |
1341 | *ti_child_buff = actual_block_no; | |
1342 | debug("TIAB %ld: %u\n", actual_block_no, | |
1343 | *total_remaining_blocks); | |
1344 | ||
1345 | ti_child_buff++; | |
1346 | (*total_remaining_blocks)--; | |
1347 | if (*total_remaining_blocks == 0) | |
1348 | break; | |
1349 | } | |
1350 | /* write the child block */ | |
0550870b MH |
1351 | put_ext4(((uint64_t) ((uint64_t)ti_child_blockno * |
1352 | (uint64_t)fs->blksz)), | |
ed34f34d US |
1353 | ti_cbuff_start_addr, fs->blksz); |
1354 | free(ti_cbuff_start_addr); | |
1355 | ||
1356 | if (*total_remaining_blocks == 0) | |
1357 | break; | |
1358 | } | |
1359 | /* write the parent block */ | |
0550870b | 1360 | put_ext4(((uint64_t) ((uint64_t)ti_parent_blockno * (uint64_t)fs->blksz)), |
ed34f34d US |
1361 | ti_pbuff_start_addr, fs->blksz); |
1362 | free(ti_pbuff_start_addr); | |
1363 | ||
1364 | if (*total_remaining_blocks == 0) | |
1365 | break; | |
1366 | } | |
1367 | /* write the grand parent block */ | |
0550870b | 1368 | put_ext4(((uint64_t) ((uint64_t)ti_gp_blockno * (uint64_t)fs->blksz)), |
ed34f34d US |
1369 | ti_gp_buff_start_addr, fs->blksz); |
1370 | file_inode->b.blocks.triple_indir_block = ti_gp_blockno; | |
1371 | } | |
1372 | fail: | |
1373 | free(ti_gp_buff_start_addr); | |
1374 | } | |
1375 | ||
1376 | void ext4fs_allocate_blocks(struct ext2_inode *file_inode, | |
1377 | unsigned int total_remaining_blocks, | |
1378 | unsigned int *total_no_of_block) | |
1379 | { | |
1380 | short i; | |
1381 | long int direct_blockno; | |
1382 | unsigned int no_blks_reqd = 0; | |
1383 | ||
1384 | /* allocation of direct blocks */ | |
d0180280 | 1385 | for (i = 0; total_remaining_blocks && i < INDIRECT_BLOCKS; i++) { |
ed34f34d US |
1386 | direct_blockno = ext4fs_get_new_blk_no(); |
1387 | if (direct_blockno == -1) { | |
1388 | printf("no block left to assign\n"); | |
1389 | return; | |
1390 | } | |
1391 | file_inode->b.blocks.dir_blocks[i] = direct_blockno; | |
1392 | debug("DB %ld: %u\n", direct_blockno, total_remaining_blocks); | |
1393 | ||
1394 | total_remaining_blocks--; | |
ed34f34d US |
1395 | } |
1396 | ||
1397 | alloc_single_indirect_block(file_inode, &total_remaining_blocks, | |
1398 | &no_blks_reqd); | |
1399 | alloc_double_indirect_block(file_inode, &total_remaining_blocks, | |
1400 | &no_blks_reqd); | |
1401 | alloc_triple_indirect_block(file_inode, &total_remaining_blocks, | |
1402 | &no_blks_reqd); | |
1403 | *total_no_of_block += no_blks_reqd; | |
1404 | } | |
1405 | ||
1406 | #endif | |
1407 | ||
a1596438 US |
1408 | static struct ext4_extent_header *ext4fs_get_extent_block |
1409 | (struct ext2_data *data, char *buf, | |
1410 | struct ext4_extent_header *ext_block, | |
1411 | uint32_t fileblock, int log2_blksz) | |
1412 | { | |
1413 | struct ext4_extent_idx *index; | |
1414 | unsigned long long block; | |
47017327 | 1415 | int blksz = EXT2_BLOCK_SIZE(data); |
a1596438 US |
1416 | int i; |
1417 | ||
1418 | while (1) { | |
1419 | index = (struct ext4_extent_idx *)(ext_block + 1); | |
1420 | ||
8b415f70 | 1421 | if (le16_to_cpu(ext_block->eh_magic) != EXT4_EXT_MAGIC) |
a1596438 US |
1422 | return 0; |
1423 | ||
1424 | if (ext_block->eh_depth == 0) | |
1425 | return ext_block; | |
1426 | i = -1; | |
1427 | do { | |
1428 | i++; | |
8b415f70 | 1429 | if (i >= le16_to_cpu(ext_block->eh_entries)) |
a1596438 | 1430 | break; |
b5bbac1a | 1431 | } while (fileblock >= le32_to_cpu(index[i].ei_block)); |
a1596438 US |
1432 | |
1433 | if (--i < 0) | |
1434 | return 0; | |
1435 | ||
8b415f70 | 1436 | block = le16_to_cpu(index[i].ei_leaf_hi); |
a1596438 US |
1437 | block = (block << 32) + le32_to_cpu(index[i].ei_leaf_lo); |
1438 | ||
47017327 | 1439 | if (ext4fs_devread((lbaint_t)block << log2_blksz, 0, blksz, |
04735e9c | 1440 | buf)) |
a1596438 US |
1441 | ext_block = (struct ext4_extent_header *)buf; |
1442 | else | |
1443 | return 0; | |
1444 | } | |
1445 | } | |
1446 | ||
1447 | static int ext4fs_blockgroup | |
1448 | (struct ext2_data *data, int group, struct ext2_block_group *blkgrp) | |
1449 | { | |
1450 | long int blkno; | |
1451 | unsigned int blkoff, desc_per_blk; | |
50ce4c07 | 1452 | int log2blksz = get_fs()->dev_desc->log2blksz; |
a1596438 US |
1453 | |
1454 | desc_per_blk = EXT2_BLOCK_SIZE(data) / sizeof(struct ext2_block_group); | |
1455 | ||
1456 | blkno = __le32_to_cpu(data->sblock.first_data_block) + 1 + | |
1457 | group / desc_per_blk; | |
1458 | blkoff = (group % desc_per_blk) * sizeof(struct ext2_block_group); | |
1459 | ||
1460 | debug("ext4fs read %d group descriptor (blkno %ld blkoff %u)\n", | |
1461 | group, blkno, blkoff); | |
1462 | ||
04735e9c FL |
1463 | return ext4fs_devread((lbaint_t)blkno << |
1464 | (LOG2_BLOCK_SIZE(data) - log2blksz), | |
a1596438 US |
1465 | blkoff, sizeof(struct ext2_block_group), |
1466 | (char *)blkgrp); | |
1467 | } | |
1468 | ||
1469 | int ext4fs_read_inode(struct ext2_data *data, int ino, struct ext2_inode *inode) | |
1470 | { | |
1471 | struct ext2_block_group blkgrp; | |
1472 | struct ext2_sblock *sblock = &data->sblock; | |
1473 | struct ext_filesystem *fs = get_fs(); | |
50ce4c07 | 1474 | int log2blksz = get_fs()->dev_desc->log2blksz; |
a1596438 US |
1475 | int inodes_per_block, status; |
1476 | long int blkno; | |
1477 | unsigned int blkoff; | |
1478 | ||
1479 | /* It is easier to calculate if the first inode is 0. */ | |
1480 | ino--; | |
1481 | status = ext4fs_blockgroup(data, ino / __le32_to_cpu | |
1482 | (sblock->inodes_per_group), &blkgrp); | |
1483 | if (status == 0) | |
1484 | return 0; | |
1485 | ||
1486 | inodes_per_block = EXT2_BLOCK_SIZE(data) / fs->inodesz; | |
1487 | blkno = __le32_to_cpu(blkgrp.inode_table_id) + | |
1488 | (ino % __le32_to_cpu(sblock->inodes_per_group)) / inodes_per_block; | |
1489 | blkoff = (ino % inodes_per_block) * fs->inodesz; | |
1490 | /* Read the inode. */ | |
04735e9c FL |
1491 | status = ext4fs_devread((lbaint_t)blkno << (LOG2_BLOCK_SIZE(data) - |
1492 | log2blksz), blkoff, | |
a1596438 US |
1493 | sizeof(struct ext2_inode), (char *)inode); |
1494 | if (status == 0) | |
1495 | return 0; | |
1496 | ||
1497 | return 1; | |
1498 | } | |
1499 | ||
1500 | long int read_allocated_block(struct ext2_inode *inode, int fileblock) | |
1501 | { | |
1502 | long int blknr; | |
1503 | int blksz; | |
1504 | int log2_blksz; | |
1505 | int status; | |
1506 | long int rblock; | |
1507 | long int perblock_parent; | |
1508 | long int perblock_child; | |
1509 | unsigned long long start; | |
1510 | /* get the blocksize of the filesystem */ | |
1511 | blksz = EXT2_BLOCK_SIZE(ext4fs_root); | |
50ce4c07 EE |
1512 | log2_blksz = LOG2_BLOCK_SIZE(ext4fs_root) |
1513 | - get_fs()->dev_desc->log2blksz; | |
1514 | ||
a1596438 US |
1515 | if (le32_to_cpu(inode->flags) & EXT4_EXTENTS_FL) { |
1516 | char *buf = zalloc(blksz); | |
1517 | if (!buf) | |
1518 | return -ENOMEM; | |
1519 | struct ext4_extent_header *ext_block; | |
1520 | struct ext4_extent *extent; | |
1521 | int i = -1; | |
50ce4c07 EE |
1522 | ext_block = |
1523 | ext4fs_get_extent_block(ext4fs_root, buf, | |
1524 | (struct ext4_extent_header *) | |
1525 | inode->b.blocks.dir_blocks, | |
1526 | fileblock, log2_blksz); | |
a1596438 US |
1527 | if (!ext_block) { |
1528 | printf("invalid extent block\n"); | |
1529 | free(buf); | |
1530 | return -EINVAL; | |
1531 | } | |
1532 | ||
1533 | extent = (struct ext4_extent *)(ext_block + 1); | |
1534 | ||
1535 | do { | |
1536 | i++; | |
8b415f70 | 1537 | if (i >= le16_to_cpu(ext_block->eh_entries)) |
a1596438 US |
1538 | break; |
1539 | } while (fileblock >= le32_to_cpu(extent[i].ee_block)); | |
1540 | if (--i >= 0) { | |
1541 | fileblock -= le32_to_cpu(extent[i].ee_block); | |
8b415f70 | 1542 | if (fileblock >= le16_to_cpu(extent[i].ee_len)) { |
a1596438 US |
1543 | free(buf); |
1544 | return 0; | |
1545 | } | |
1546 | ||
8b415f70 | 1547 | start = le16_to_cpu(extent[i].ee_start_hi); |
a1596438 US |
1548 | start = (start << 32) + |
1549 | le32_to_cpu(extent[i].ee_start_lo); | |
1550 | free(buf); | |
1551 | return fileblock + start; | |
1552 | } | |
1553 | ||
1554 | printf("Extent Error\n"); | |
1555 | free(buf); | |
1556 | return -1; | |
1557 | } | |
1558 | ||
1559 | /* Direct blocks. */ | |
1560 | if (fileblock < INDIRECT_BLOCKS) | |
1561 | blknr = __le32_to_cpu(inode->b.blocks.dir_blocks[fileblock]); | |
1562 | ||
1563 | /* Indirect. */ | |
1564 | else if (fileblock < (INDIRECT_BLOCKS + (blksz / 4))) { | |
1565 | if (ext4fs_indir1_block == NULL) { | |
1566 | ext4fs_indir1_block = zalloc(blksz); | |
1567 | if (ext4fs_indir1_block == NULL) { | |
1568 | printf("** SI ext2fs read block (indir 1)" | |
1569 | "malloc failed. **\n"); | |
1570 | return -1; | |
1571 | } | |
1572 | ext4fs_indir1_size = blksz; | |
1573 | ext4fs_indir1_blkno = -1; | |
1574 | } | |
1575 | if (blksz != ext4fs_indir1_size) { | |
1576 | free(ext4fs_indir1_block); | |
1577 | ext4fs_indir1_block = NULL; | |
1578 | ext4fs_indir1_size = 0; | |
1579 | ext4fs_indir1_blkno = -1; | |
1580 | ext4fs_indir1_block = zalloc(blksz); | |
1581 | if (ext4fs_indir1_block == NULL) { | |
1582 | printf("** SI ext2fs read block (indir 1):" | |
1583 | "malloc failed. **\n"); | |
1584 | return -1; | |
1585 | } | |
1586 | ext4fs_indir1_size = blksz; | |
1587 | } | |
1588 | if ((__le32_to_cpu(inode->b.blocks.indir_block) << | |
1589 | log2_blksz) != ext4fs_indir1_blkno) { | |
1590 | status = | |
04735e9c | 1591 | ext4fs_devread((lbaint_t)__le32_to_cpu |
a1596438 US |
1592 | (inode->b.blocks. |
1593 | indir_block) << log2_blksz, 0, | |
1594 | blksz, (char *)ext4fs_indir1_block); | |
1595 | if (status == 0) { | |
1596 | printf("** SI ext2fs read block (indir 1)" | |
1597 | "failed. **\n"); | |
1598 | return 0; | |
1599 | } | |
1600 | ext4fs_indir1_blkno = | |
1601 | __le32_to_cpu(inode->b.blocks. | |
1602 | indir_block) << log2_blksz; | |
1603 | } | |
1604 | blknr = __le32_to_cpu(ext4fs_indir1_block | |
1605 | [fileblock - INDIRECT_BLOCKS]); | |
1606 | } | |
1607 | /* Double indirect. */ | |
1608 | else if (fileblock < (INDIRECT_BLOCKS + (blksz / 4 * | |
1609 | (blksz / 4 + 1)))) { | |
1610 | ||
1611 | long int perblock = blksz / 4; | |
1612 | long int rblock = fileblock - (INDIRECT_BLOCKS + blksz / 4); | |
1613 | ||
1614 | if (ext4fs_indir1_block == NULL) { | |
1615 | ext4fs_indir1_block = zalloc(blksz); | |
1616 | if (ext4fs_indir1_block == NULL) { | |
1617 | printf("** DI ext2fs read block (indir 2 1)" | |
1618 | "malloc failed. **\n"); | |
1619 | return -1; | |
1620 | } | |
1621 | ext4fs_indir1_size = blksz; | |
1622 | ext4fs_indir1_blkno = -1; | |
1623 | } | |
1624 | if (blksz != ext4fs_indir1_size) { | |
1625 | free(ext4fs_indir1_block); | |
1626 | ext4fs_indir1_block = NULL; | |
1627 | ext4fs_indir1_size = 0; | |
1628 | ext4fs_indir1_blkno = -1; | |
1629 | ext4fs_indir1_block = zalloc(blksz); | |
1630 | if (ext4fs_indir1_block == NULL) { | |
1631 | printf("** DI ext2fs read block (indir 2 1)" | |
1632 | "malloc failed. **\n"); | |
1633 | return -1; | |
1634 | } | |
1635 | ext4fs_indir1_size = blksz; | |
1636 | } | |
1637 | if ((__le32_to_cpu(inode->b.blocks.double_indir_block) << | |
1638 | log2_blksz) != ext4fs_indir1_blkno) { | |
1639 | status = | |
04735e9c | 1640 | ext4fs_devread((lbaint_t)__le32_to_cpu |
a1596438 US |
1641 | (inode->b.blocks. |
1642 | double_indir_block) << log2_blksz, | |
1643 | 0, blksz, | |
1644 | (char *)ext4fs_indir1_block); | |
1645 | if (status == 0) { | |
1646 | printf("** DI ext2fs read block (indir 2 1)" | |
1647 | "failed. **\n"); | |
1648 | return -1; | |
1649 | } | |
1650 | ext4fs_indir1_blkno = | |
1651 | __le32_to_cpu(inode->b.blocks.double_indir_block) << | |
1652 | log2_blksz; | |
1653 | } | |
1654 | ||
1655 | if (ext4fs_indir2_block == NULL) { | |
1656 | ext4fs_indir2_block = zalloc(blksz); | |
1657 | if (ext4fs_indir2_block == NULL) { | |
1658 | printf("** DI ext2fs read block (indir 2 2)" | |
1659 | "malloc failed. **\n"); | |
1660 | return -1; | |
1661 | } | |
1662 | ext4fs_indir2_size = blksz; | |
1663 | ext4fs_indir2_blkno = -1; | |
1664 | } | |
1665 | if (blksz != ext4fs_indir2_size) { | |
1666 | free(ext4fs_indir2_block); | |
1667 | ext4fs_indir2_block = NULL; | |
1668 | ext4fs_indir2_size = 0; | |
1669 | ext4fs_indir2_blkno = -1; | |
1670 | ext4fs_indir2_block = zalloc(blksz); | |
1671 | if (ext4fs_indir2_block == NULL) { | |
1672 | printf("** DI ext2fs read block (indir 2 2)" | |
1673 | "malloc failed. **\n"); | |
1674 | return -1; | |
1675 | } | |
1676 | ext4fs_indir2_size = blksz; | |
1677 | } | |
1678 | if ((__le32_to_cpu(ext4fs_indir1_block[rblock / perblock]) << | |
1679 | log2_blksz) != ext4fs_indir2_blkno) { | |
04735e9c | 1680 | status = ext4fs_devread((lbaint_t)__le32_to_cpu |
a1596438 US |
1681 | (ext4fs_indir1_block |
1682 | [rblock / | |
1683 | perblock]) << log2_blksz, 0, | |
1684 | blksz, | |
1685 | (char *)ext4fs_indir2_block); | |
1686 | if (status == 0) { | |
1687 | printf("** DI ext2fs read block (indir 2 2)" | |
1688 | "failed. **\n"); | |
1689 | return -1; | |
1690 | } | |
1691 | ext4fs_indir2_blkno = | |
1692 | __le32_to_cpu(ext4fs_indir1_block[rblock | |
1693 | / | |
1694 | perblock]) << | |
1695 | log2_blksz; | |
1696 | } | |
1697 | blknr = __le32_to_cpu(ext4fs_indir2_block[rblock % perblock]); | |
1698 | } | |
1699 | /* Tripple indirect. */ | |
1700 | else { | |
1701 | rblock = fileblock - (INDIRECT_BLOCKS + blksz / 4 + | |
1702 | (blksz / 4 * blksz / 4)); | |
1703 | perblock_child = blksz / 4; | |
1704 | perblock_parent = ((blksz / 4) * (blksz / 4)); | |
1705 | ||
1706 | if (ext4fs_indir1_block == NULL) { | |
1707 | ext4fs_indir1_block = zalloc(blksz); | |
1708 | if (ext4fs_indir1_block == NULL) { | |
1709 | printf("** TI ext2fs read block (indir 2 1)" | |
1710 | "malloc failed. **\n"); | |
1711 | return -1; | |
1712 | } | |
1713 | ext4fs_indir1_size = blksz; | |
1714 | ext4fs_indir1_blkno = -1; | |
1715 | } | |
1716 | if (blksz != ext4fs_indir1_size) { | |
1717 | free(ext4fs_indir1_block); | |
1718 | ext4fs_indir1_block = NULL; | |
1719 | ext4fs_indir1_size = 0; | |
1720 | ext4fs_indir1_blkno = -1; | |
1721 | ext4fs_indir1_block = zalloc(blksz); | |
1722 | if (ext4fs_indir1_block == NULL) { | |
1723 | printf("** TI ext2fs read block (indir 2 1)" | |
1724 | "malloc failed. **\n"); | |
1725 | return -1; | |
1726 | } | |
1727 | ext4fs_indir1_size = blksz; | |
1728 | } | |
1729 | if ((__le32_to_cpu(inode->b.blocks.triple_indir_block) << | |
1730 | log2_blksz) != ext4fs_indir1_blkno) { | |
1731 | status = ext4fs_devread | |
04735e9c FL |
1732 | ((lbaint_t) |
1733 | __le32_to_cpu(inode->b.blocks.triple_indir_block) | |
a1596438 US |
1734 | << log2_blksz, 0, blksz, |
1735 | (char *)ext4fs_indir1_block); | |
1736 | if (status == 0) { | |
1737 | printf("** TI ext2fs read block (indir 2 1)" | |
1738 | "failed. **\n"); | |
1739 | return -1; | |
1740 | } | |
1741 | ext4fs_indir1_blkno = | |
1742 | __le32_to_cpu(inode->b.blocks.triple_indir_block) << | |
1743 | log2_blksz; | |
1744 | } | |
1745 | ||
1746 | if (ext4fs_indir2_block == NULL) { | |
1747 | ext4fs_indir2_block = zalloc(blksz); | |
1748 | if (ext4fs_indir2_block == NULL) { | |
1749 | printf("** TI ext2fs read block (indir 2 2)" | |
1750 | "malloc failed. **\n"); | |
1751 | return -1; | |
1752 | } | |
1753 | ext4fs_indir2_size = blksz; | |
1754 | ext4fs_indir2_blkno = -1; | |
1755 | } | |
1756 | if (blksz != ext4fs_indir2_size) { | |
1757 | free(ext4fs_indir2_block); | |
1758 | ext4fs_indir2_block = NULL; | |
1759 | ext4fs_indir2_size = 0; | |
1760 | ext4fs_indir2_blkno = -1; | |
1761 | ext4fs_indir2_block = zalloc(blksz); | |
1762 | if (ext4fs_indir2_block == NULL) { | |
1763 | printf("** TI ext2fs read block (indir 2 2)" | |
1764 | "malloc failed. **\n"); | |
1765 | return -1; | |
1766 | } | |
1767 | ext4fs_indir2_size = blksz; | |
1768 | } | |
1769 | if ((__le32_to_cpu(ext4fs_indir1_block[rblock / | |
1770 | perblock_parent]) << | |
1771 | log2_blksz) | |
1772 | != ext4fs_indir2_blkno) { | |
04735e9c | 1773 | status = ext4fs_devread((lbaint_t)__le32_to_cpu |
a1596438 US |
1774 | (ext4fs_indir1_block |
1775 | [rblock / | |
1776 | perblock_parent]) << | |
1777 | log2_blksz, 0, blksz, | |
1778 | (char *)ext4fs_indir2_block); | |
1779 | if (status == 0) { | |
1780 | printf("** TI ext2fs read block (indir 2 2)" | |
1781 | "failed. **\n"); | |
1782 | return -1; | |
1783 | } | |
1784 | ext4fs_indir2_blkno = | |
1785 | __le32_to_cpu(ext4fs_indir1_block[rblock / | |
1786 | perblock_parent]) | |
1787 | << log2_blksz; | |
1788 | } | |
1789 | ||
1790 | if (ext4fs_indir3_block == NULL) { | |
1791 | ext4fs_indir3_block = zalloc(blksz); | |
1792 | if (ext4fs_indir3_block == NULL) { | |
1793 | printf("** TI ext2fs read block (indir 2 2)" | |
1794 | "malloc failed. **\n"); | |
1795 | return -1; | |
1796 | } | |
1797 | ext4fs_indir3_size = blksz; | |
1798 | ext4fs_indir3_blkno = -1; | |
1799 | } | |
1800 | if (blksz != ext4fs_indir3_size) { | |
1801 | free(ext4fs_indir3_block); | |
1802 | ext4fs_indir3_block = NULL; | |
1803 | ext4fs_indir3_size = 0; | |
1804 | ext4fs_indir3_blkno = -1; | |
1805 | ext4fs_indir3_block = zalloc(blksz); | |
1806 | if (ext4fs_indir3_block == NULL) { | |
1807 | printf("** TI ext2fs read block (indir 2 2)" | |
1808 | "malloc failed. **\n"); | |
1809 | return -1; | |
1810 | } | |
1811 | ext4fs_indir3_size = blksz; | |
1812 | } | |
1813 | if ((__le32_to_cpu(ext4fs_indir2_block[rblock | |
1814 | / | |
1815 | perblock_child]) << | |
1816 | log2_blksz) != ext4fs_indir3_blkno) { | |
1817 | status = | |
04735e9c | 1818 | ext4fs_devread((lbaint_t)__le32_to_cpu |
a1596438 US |
1819 | (ext4fs_indir2_block |
1820 | [(rblock / perblock_child) | |
1821 | % (blksz / 4)]) << log2_blksz, 0, | |
1822 | blksz, (char *)ext4fs_indir3_block); | |
1823 | if (status == 0) { | |
1824 | printf("** TI ext2fs read block (indir 2 2)" | |
1825 | "failed. **\n"); | |
1826 | return -1; | |
1827 | } | |
1828 | ext4fs_indir3_blkno = | |
1829 | __le32_to_cpu(ext4fs_indir2_block[(rblock / | |
1830 | perblock_child) % | |
1831 | (blksz / | |
1832 | 4)]) << | |
1833 | log2_blksz; | |
1834 | } | |
1835 | ||
1836 | blknr = __le32_to_cpu(ext4fs_indir3_block | |
1837 | [rblock % perblock_child]); | |
1838 | } | |
50ce4c07 | 1839 | debug("read_allocated_block %ld\n", blknr); |
a1596438 US |
1840 | |
1841 | return blknr; | |
1842 | } | |
1843 | ||
8b454eee ŁM |
1844 | /** |
1845 | * ext4fs_reinit_global() - Reinitialize values of ext4 write implementation's | |
1846 | * global pointers | |
1847 | * | |
1848 | * This function assures that for a file with the same name but different size | |
1849 | * the sequential store on the ext4 filesystem will be correct. | |
1850 | * | |
1851 | * In this function the global data, responsible for internal representation | |
1852 | * of the ext4 data are initialized to the reset state. Without this, during | |
1853 | * replacement of the smaller file with the bigger truncation of new file was | |
1854 | * performed. | |
1855 | */ | |
1856 | void ext4fs_reinit_global(void) | |
a1596438 | 1857 | { |
a1596438 US |
1858 | if (ext4fs_indir1_block != NULL) { |
1859 | free(ext4fs_indir1_block); | |
1860 | ext4fs_indir1_block = NULL; | |
1861 | ext4fs_indir1_size = 0; | |
1862 | ext4fs_indir1_blkno = -1; | |
1863 | } | |
1864 | if (ext4fs_indir2_block != NULL) { | |
1865 | free(ext4fs_indir2_block); | |
1866 | ext4fs_indir2_block = NULL; | |
1867 | ext4fs_indir2_size = 0; | |
1868 | ext4fs_indir2_blkno = -1; | |
1869 | } | |
1870 | if (ext4fs_indir3_block != NULL) { | |
1871 | free(ext4fs_indir3_block); | |
1872 | ext4fs_indir3_block = NULL; | |
1873 | ext4fs_indir3_size = 0; | |
1874 | ext4fs_indir3_blkno = -1; | |
1875 | } | |
1876 | } | |
8b454eee ŁM |
1877 | void ext4fs_close(void) |
1878 | { | |
1879 | if ((ext4fs_file != NULL) && (ext4fs_root != NULL)) { | |
1880 | ext4fs_free_node(ext4fs_file, &ext4fs_root->diropen); | |
1881 | ext4fs_file = NULL; | |
1882 | } | |
1883 | if (ext4fs_root != NULL) { | |
1884 | free(ext4fs_root); | |
1885 | ext4fs_root = NULL; | |
1886 | } | |
1887 | ||
1888 | ext4fs_reinit_global(); | |
1889 | } | |
a1596438 US |
1890 | |
1891 | int ext4fs_iterate_dir(struct ext2fs_node *dir, char *name, | |
1892 | struct ext2fs_node **fnode, int *ftype) | |
1893 | { | |
1894 | unsigned int fpos = 0; | |
1895 | int status; | |
9f12cd0e | 1896 | loff_t actread; |
a1596438 US |
1897 | struct ext2fs_node *diro = (struct ext2fs_node *) dir; |
1898 | ||
1899 | #ifdef DEBUG | |
1900 | if (name != NULL) | |
1901 | printf("Iterate dir %s\n", name); | |
1902 | #endif /* of DEBUG */ | |
1903 | if (!diro->inode_read) { | |
1904 | status = ext4fs_read_inode(diro->data, diro->ino, &diro->inode); | |
1905 | if (status == 0) | |
1906 | return 0; | |
1907 | } | |
1908 | /* Search the file. */ | |
1909 | while (fpos < __le32_to_cpu(diro->inode.size)) { | |
1910 | struct ext2_dirent dirent; | |
1911 | ||
1912 | status = ext4fs_read_file(diro, fpos, | |
1913 | sizeof(struct ext2_dirent), | |
9f12cd0e SR |
1914 | (char *)&dirent, &actread); |
1915 | if (status < 0) | |
a1596438 US |
1916 | return 0; |
1917 | ||
1918 | if (dirent.namelen != 0) { | |
1919 | char filename[dirent.namelen + 1]; | |
1920 | struct ext2fs_node *fdiro; | |
1921 | int type = FILETYPE_UNKNOWN; | |
1922 | ||
1923 | status = ext4fs_read_file(diro, | |
1924 | fpos + | |
1925 | sizeof(struct ext2_dirent), | |
9f12cd0e SR |
1926 | dirent.namelen, filename, |
1927 | &actread); | |
1928 | if (status < 0) | |
a1596438 US |
1929 | return 0; |
1930 | ||
1931 | fdiro = zalloc(sizeof(struct ext2fs_node)); | |
1932 | if (!fdiro) | |
1933 | return 0; | |
1934 | ||
1935 | fdiro->data = diro->data; | |
1936 | fdiro->ino = __le32_to_cpu(dirent.inode); | |
1937 | ||
1938 | filename[dirent.namelen] = '\0'; | |
1939 | ||
1940 | if (dirent.filetype != FILETYPE_UNKNOWN) { | |
1941 | fdiro->inode_read = 0; | |
1942 | ||
1943 | if (dirent.filetype == FILETYPE_DIRECTORY) | |
1944 | type = FILETYPE_DIRECTORY; | |
1945 | else if (dirent.filetype == FILETYPE_SYMLINK) | |
1946 | type = FILETYPE_SYMLINK; | |
1947 | else if (dirent.filetype == FILETYPE_REG) | |
1948 | type = FILETYPE_REG; | |
1949 | } else { | |
1950 | status = ext4fs_read_inode(diro->data, | |
1951 | __le32_to_cpu | |
1952 | (dirent.inode), | |
1953 | &fdiro->inode); | |
1954 | if (status == 0) { | |
1955 | free(fdiro); | |
1956 | return 0; | |
1957 | } | |
1958 | fdiro->inode_read = 1; | |
1959 | ||
1960 | if ((__le16_to_cpu(fdiro->inode.mode) & | |
1961 | FILETYPE_INO_MASK) == | |
1962 | FILETYPE_INO_DIRECTORY) { | |
1963 | type = FILETYPE_DIRECTORY; | |
1964 | } else if ((__le16_to_cpu(fdiro->inode.mode) | |
1965 | & FILETYPE_INO_MASK) == | |
1966 | FILETYPE_INO_SYMLINK) { | |
1967 | type = FILETYPE_SYMLINK; | |
1968 | } else if ((__le16_to_cpu(fdiro->inode.mode) | |
1969 | & FILETYPE_INO_MASK) == | |
1970 | FILETYPE_INO_REG) { | |
1971 | type = FILETYPE_REG; | |
1972 | } | |
1973 | } | |
1974 | #ifdef DEBUG | |
1975 | printf("iterate >%s<\n", filename); | |
1976 | #endif /* of DEBUG */ | |
1977 | if ((name != NULL) && (fnode != NULL) | |
1978 | && (ftype != NULL)) { | |
1979 | if (strcmp(filename, name) == 0) { | |
1980 | *ftype = type; | |
1981 | *fnode = fdiro; | |
1982 | return 1; | |
1983 | } | |
1984 | } else { | |
1985 | if (fdiro->inode_read == 0) { | |
1986 | status = ext4fs_read_inode(diro->data, | |
1987 | __le32_to_cpu( | |
1988 | dirent.inode), | |
1989 | &fdiro->inode); | |
1990 | if (status == 0) { | |
1991 | free(fdiro); | |
1992 | return 0; | |
1993 | } | |
1994 | fdiro->inode_read = 1; | |
1995 | } | |
1996 | switch (type) { | |
1997 | case FILETYPE_DIRECTORY: | |
1998 | printf("<DIR> "); | |
1999 | break; | |
2000 | case FILETYPE_SYMLINK: | |
2001 | printf("<SYM> "); | |
2002 | break; | |
2003 | case FILETYPE_REG: | |
2004 | printf(" "); | |
2005 | break; | |
2006 | default: | |
2007 | printf("< ? > "); | |
2008 | break; | |
2009 | } | |
9f12cd0e SR |
2010 | printf("%10u %s\n", |
2011 | __le32_to_cpu(fdiro->inode.size), | |
a1596438 US |
2012 | filename); |
2013 | } | |
2014 | free(fdiro); | |
2015 | } | |
2016 | fpos += __le16_to_cpu(dirent.direntlen); | |
2017 | } | |
2018 | return 0; | |
2019 | } | |
2020 | ||
2021 | static char *ext4fs_read_symlink(struct ext2fs_node *node) | |
2022 | { | |
2023 | char *symlink; | |
2024 | struct ext2fs_node *diro = node; | |
2025 | int status; | |
9f12cd0e | 2026 | loff_t actread; |
a1596438 US |
2027 | |
2028 | if (!diro->inode_read) { | |
2029 | status = ext4fs_read_inode(diro->data, diro->ino, &diro->inode); | |
2030 | if (status == 0) | |
2031 | return 0; | |
2032 | } | |
2033 | symlink = zalloc(__le32_to_cpu(diro->inode.size) + 1); | |
2034 | if (!symlink) | |
2035 | return 0; | |
2036 | ||
2037 | if (__le32_to_cpu(diro->inode.size) <= 60) { | |
2038 | strncpy(symlink, diro->inode.b.symlink, | |
2039 | __le32_to_cpu(diro->inode.size)); | |
2040 | } else { | |
2041 | status = ext4fs_read_file(diro, 0, | |
2042 | __le32_to_cpu(diro->inode.size), | |
9f12cd0e | 2043 | symlink, &actread); |
a1596438 US |
2044 | if (status == 0) { |
2045 | free(symlink); | |
2046 | return 0; | |
2047 | } | |
2048 | } | |
2049 | symlink[__le32_to_cpu(diro->inode.size)] = '\0'; | |
2050 | return symlink; | |
2051 | } | |
2052 | ||
2053 | static int ext4fs_find_file1(const char *currpath, | |
2054 | struct ext2fs_node *currroot, | |
2055 | struct ext2fs_node **currfound, int *foundtype) | |
2056 | { | |
2057 | char fpath[strlen(currpath) + 1]; | |
2058 | char *name = fpath; | |
2059 | char *next; | |
2060 | int status; | |
2061 | int type = FILETYPE_DIRECTORY; | |
2062 | struct ext2fs_node *currnode = currroot; | |
2063 | struct ext2fs_node *oldnode = currroot; | |
2064 | ||
2065 | strncpy(fpath, currpath, strlen(currpath) + 1); | |
2066 | ||
2067 | /* Remove all leading slashes. */ | |
2068 | while (*name == '/') | |
2069 | name++; | |
2070 | ||
2071 | if (!*name) { | |
2072 | *currfound = currnode; | |
2073 | return 1; | |
2074 | } | |
2075 | ||
2076 | for (;;) { | |
2077 | int found; | |
2078 | ||
2079 | /* Extract the actual part from the pathname. */ | |
2080 | next = strchr(name, '/'); | |
2081 | if (next) { | |
2082 | /* Remove all leading slashes. */ | |
2083 | while (*next == '/') | |
2084 | *(next++) = '\0'; | |
2085 | } | |
2086 | ||
2087 | if (type != FILETYPE_DIRECTORY) { | |
2088 | ext4fs_free_node(currnode, currroot); | |
2089 | return 0; | |
2090 | } | |
2091 | ||
2092 | oldnode = currnode; | |
2093 | ||
2094 | /* Iterate over the directory. */ | |
2095 | found = ext4fs_iterate_dir(currnode, name, &currnode, &type); | |
2096 | if (found == 0) | |
2097 | return 0; | |
2098 | ||
2099 | if (found == -1) | |
2100 | break; | |
2101 | ||
2102 | /* Read in the symlink and follow it. */ | |
2103 | if (type == FILETYPE_SYMLINK) { | |
2104 | char *symlink; | |
2105 | ||
2106 | /* Test if the symlink does not loop. */ | |
2107 | if (++symlinknest == 8) { | |
2108 | ext4fs_free_node(currnode, currroot); | |
2109 | ext4fs_free_node(oldnode, currroot); | |
2110 | return 0; | |
2111 | } | |
2112 | ||
2113 | symlink = ext4fs_read_symlink(currnode); | |
2114 | ext4fs_free_node(currnode, currroot); | |
2115 | ||
2116 | if (!symlink) { | |
2117 | ext4fs_free_node(oldnode, currroot); | |
2118 | return 0; | |
2119 | } | |
2120 | ||
2121 | debug("Got symlink >%s<\n", symlink); | |
2122 | ||
2123 | if (symlink[0] == '/') { | |
2124 | ext4fs_free_node(oldnode, currroot); | |
2125 | oldnode = &ext4fs_root->diropen; | |
2126 | } | |
2127 | ||
2128 | /* Lookup the node the symlink points to. */ | |
2129 | status = ext4fs_find_file1(symlink, oldnode, | |
2130 | &currnode, &type); | |
2131 | ||
2132 | free(symlink); | |
2133 | ||
2134 | if (status == 0) { | |
2135 | ext4fs_free_node(oldnode, currroot); | |
2136 | return 0; | |
2137 | } | |
2138 | } | |
2139 | ||
2140 | ext4fs_free_node(oldnode, currroot); | |
2141 | ||
2142 | /* Found the node! */ | |
2143 | if (!next || *next == '\0') { | |
2144 | *currfound = currnode; | |
2145 | *foundtype = type; | |
2146 | return 1; | |
2147 | } | |
2148 | name = next; | |
2149 | } | |
2150 | return -1; | |
2151 | } | |
2152 | ||
2153 | int ext4fs_find_file(const char *path, struct ext2fs_node *rootnode, | |
2154 | struct ext2fs_node **foundnode, int expecttype) | |
2155 | { | |
2156 | int status; | |
2157 | int foundtype = FILETYPE_DIRECTORY; | |
2158 | ||
2159 | symlinknest = 0; | |
2160 | if (!path) | |
2161 | return 0; | |
2162 | ||
2163 | status = ext4fs_find_file1(path, rootnode, foundnode, &foundtype); | |
2164 | if (status == 0) | |
2165 | return 0; | |
2166 | ||
2167 | /* Check if the node that was found was of the expected type. */ | |
2168 | if ((expecttype == FILETYPE_REG) && (foundtype != expecttype)) | |
2169 | return 0; | |
2170 | else if ((expecttype == FILETYPE_DIRECTORY) | |
2171 | && (foundtype != expecttype)) | |
2172 | return 0; | |
2173 | ||
2174 | return 1; | |
2175 | } | |
2176 | ||
9f12cd0e | 2177 | int ext4fs_open(const char *filename, loff_t *len) |
a1596438 US |
2178 | { |
2179 | struct ext2fs_node *fdiro = NULL; | |
2180 | int status; | |
a1596438 US |
2181 | |
2182 | if (ext4fs_root == NULL) | |
2183 | return -1; | |
2184 | ||
2185 | ext4fs_file = NULL; | |
2186 | status = ext4fs_find_file(filename, &ext4fs_root->diropen, &fdiro, | |
2187 | FILETYPE_REG); | |
2188 | if (status == 0) | |
2189 | goto fail; | |
2190 | ||
2191 | if (!fdiro->inode_read) { | |
2192 | status = ext4fs_read_inode(fdiro->data, fdiro->ino, | |
2193 | &fdiro->inode); | |
2194 | if (status == 0) | |
2195 | goto fail; | |
2196 | } | |
9f12cd0e | 2197 | *len = __le32_to_cpu(fdiro->inode.size); |
a1596438 US |
2198 | ext4fs_file = fdiro; |
2199 | ||
9f12cd0e | 2200 | return 0; |
a1596438 US |
2201 | fail: |
2202 | ext4fs_free_node(fdiro, &ext4fs_root->diropen); | |
2203 | ||
2204 | return -1; | |
2205 | } | |
2206 | ||
2207 | int ext4fs_mount(unsigned part_length) | |
2208 | { | |
2209 | struct ext2_data *data; | |
2210 | int status; | |
2211 | struct ext_filesystem *fs = get_fs(); | |
50ce4c07 | 2212 | data = zalloc(SUPERBLOCK_SIZE); |
a1596438 US |
2213 | if (!data) |
2214 | return 0; | |
2215 | ||
2216 | /* Read the superblock. */ | |
50ce4c07 | 2217 | status = ext4_read_superblock((char *)&data->sblock); |
a1596438 US |
2218 | |
2219 | if (status == 0) | |
2220 | goto fail; | |
2221 | ||
2222 | /* Make sure this is an ext2 filesystem. */ | |
2223 | if (__le16_to_cpu(data->sblock.magic) != EXT2_MAGIC) | |
2224 | goto fail; | |
2225 | ||
2226 | if (__le32_to_cpu(data->sblock.revision_level == 0)) | |
2227 | fs->inodesz = 128; | |
2228 | else | |
2229 | fs->inodesz = __le16_to_cpu(data->sblock.inode_size); | |
2230 | ||
2231 | debug("EXT2 rev %d, inode_size %d\n", | |
2232 | __le32_to_cpu(data->sblock.revision_level), fs->inodesz); | |
2233 | ||
2234 | data->diropen.data = data; | |
2235 | data->diropen.ino = 2; | |
2236 | data->diropen.inode_read = 1; | |
2237 | data->inode = &data->diropen.inode; | |
2238 | ||
2239 | status = ext4fs_read_inode(data, 2, data->inode); | |
2240 | if (status == 0) | |
2241 | goto fail; | |
2242 | ||
2243 | ext4fs_root = data; | |
2244 | ||
2245 | return 1; | |
2246 | fail: | |
2247 | printf("Failed to mount ext2 filesystem...\n"); | |
2248 | free(data); | |
2249 | ext4fs_root = NULL; | |
2250 | ||
2251 | return 0; | |
2252 | } |