]> code.delx.au - refind/blob - filesystems/fsw_ext4.c
Improved extent support, read sequential
[refind] / filesystems / fsw_ext4.c
1 /**
2 * \file fsw_ext4.c
3 * ext4 file system driver code.
4 */
5
6 /*-
7 * Copyright (c) 2012 Stefan Agner
8 * Portions Copyright (c) 2006 Christoph Pfisterer
9 *
10 * This program is free software; you can redistribute it and/or
11 * modify it under the terms of the GNU General Public License
12 * as published by the Free Software Foundation; either version 2
13 * of the License, or (at your option) any later version.
14 *
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
19 *
20 * You should have received a copy of the GNU General Public License
21 * along with this program; if not, write to the Free Software
22 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
23 */
24
25 #include "fsw_ext4.h"
26
27
28 // functions
29
30 static fsw_status_t fsw_ext4_volume_mount(struct fsw_ext4_volume *vol);
31 static void fsw_ext4_volume_free(struct fsw_ext4_volume *vol);
32 static fsw_status_t fsw_ext4_volume_stat(struct fsw_ext4_volume *vol, struct fsw_volume_stat *sb);
33
34 static fsw_status_t fsw_ext4_dnode_fill(struct fsw_ext4_volume *vol, struct fsw_ext4_dnode *dno);
35 static void fsw_ext4_dnode_free(struct fsw_ext4_volume *vol, struct fsw_ext4_dnode *dno);
36 static fsw_status_t fsw_ext4_dnode_stat(struct fsw_ext4_volume *vol, struct fsw_ext4_dnode *dno,
37 struct fsw_dnode_stat *sb);
38 static fsw_status_t fsw_ext4_get_extent(struct fsw_ext4_volume *vol, struct fsw_ext4_dnode *dno,
39 struct fsw_extent *extent);
40 static fsw_status_t fsw_ext4_get_by_blkaddr(struct fsw_ext4_volume *vol, struct fsw_ext4_dnode *dno,
41 struct fsw_extent *extent);
42 static fsw_status_t fsw_ext4_get_by_extent(struct fsw_ext4_volume *vol, struct fsw_ext4_dnode *dno,
43 struct fsw_extent *extent);
44
45 static fsw_status_t fsw_ext4_dir_lookup(struct fsw_ext4_volume *vol, struct fsw_ext4_dnode *dno,
46 struct fsw_string *lookup_name, struct fsw_ext4_dnode **child_dno);
47 static fsw_status_t fsw_ext4_dir_read(struct fsw_ext4_volume *vol, struct fsw_ext4_dnode *dno,
48 struct fsw_shandle *shand, struct fsw_ext4_dnode **child_dno);
49 static fsw_status_t fsw_ext4_read_dentry(struct fsw_shandle *shand, struct ext4_dir_entry *entry);
50
51 static fsw_status_t fsw_ext4_readlink(struct fsw_ext4_volume *vol, struct fsw_ext4_dnode *dno,
52 struct fsw_string *link);
53
54 //
55 // Dispatch Table
56 //
57
58 struct fsw_fstype_table FSW_FSTYPE_TABLE_NAME(ext4) = {
59 { FSW_STRING_TYPE_ISO88591, 4, 4, "ext4" },
60 sizeof(struct fsw_ext4_volume),
61 sizeof(struct fsw_ext4_dnode),
62
63 fsw_ext4_volume_mount,
64 fsw_ext4_volume_free,
65 fsw_ext4_volume_stat,
66 fsw_ext4_dnode_fill,
67 fsw_ext4_dnode_free,
68 fsw_ext4_dnode_stat,
69 fsw_ext4_get_extent,
70 fsw_ext4_dir_lookup,
71 fsw_ext4_dir_read,
72 fsw_ext4_readlink,
73 };
74
75 /**
76 * Mount an ext4 volume. Reads the superblock and constructs the
77 * root directory dnode.
78 */
79
80 static fsw_status_t fsw_ext4_volume_mount(struct fsw_ext4_volume *vol)
81 {
82 fsw_status_t status;
83 void *buffer;
84 fsw_u32 blocksize;
85 fsw_u32 groupcnt, groupno, gdesc_per_block, gdesc_bno, gdesc_index;
86 struct ext4_group_desc *gdesc;
87 int i;
88 struct fsw_string s;
89
90 // allocate memory to keep the superblock around
91 status = fsw_alloc(sizeof(struct ext4_super_block), &vol->sb);
92 if (status)
93 return status;
94
95 // read the superblock into its buffer
96 fsw_set_blocksize(vol, EXT4_SUPERBLOCK_BLOCKSIZE, EXT4_SUPERBLOCK_BLOCKSIZE);
97 status = fsw_block_get(vol, EXT4_SUPERBLOCK_BLOCKNO, 0, &buffer);
98 if (status)
99 return status;
100 fsw_memcpy(vol->sb, buffer, sizeof(struct ext4_super_block));
101 fsw_block_release(vol, EXT4_SUPERBLOCK_BLOCKNO, buffer);
102
103 // check the superblock
104 if (vol->sb->s_magic != EXT4_SUPER_MAGIC)
105 return FSW_UNSUPPORTED;
106 if (vol->sb->s_rev_level != EXT4_GOOD_OLD_REV &&
107 vol->sb->s_rev_level != EXT4_DYNAMIC_REV)
108 return FSW_UNSUPPORTED;
109
110 FSW_MSG_DEBUG((FSW_MSGSTR("fsw_ext4_volume_mount: Incompat flag %x\n"), vol->sb->s_feature_incompat));
111
112 if (vol->sb->s_rev_level == EXT4_DYNAMIC_REV &&
113 (vol->sb->s_feature_incompat & ~(EXT4_FEATURE_INCOMPAT_FILETYPE | EXT4_FEATURE_INCOMPAT_RECOVER |
114 EXT4_FEATURE_INCOMPAT_EXTENTS)))
115 return FSW_UNSUPPORTED;
116
117
118 if (vol->sb->s_rev_level == EXT4_DYNAMIC_REV &&
119 (vol->sb->s_feature_incompat & EXT4_FEATURE_INCOMPAT_RECOVER))
120 {
121 FSW_MSG_DEBUG((FSW_MSGSTR("fsw_ext4_volume_mount: This ext3 file system needs recovery\n")));
122 // Print(L"Ext4 WARNING: This file system needs recovery, trying to use it anyway.\n");
123 }
124
125 blocksize = EXT4_BLOCK_SIZE(vol->sb);
126 if (blocksize < EXT4_MIN_BLOCK_SIZE || blocksize > EXT4_MAX_BLOCK_SIZE)
127 return FSW_UNSUPPORTED;
128
129 // set real blocksize
130 fsw_set_blocksize(vol, blocksize, blocksize);
131
132 // get other info from superblock
133 vol->ind_bcnt = EXT4_ADDR_PER_BLOCK(vol->sb);
134 vol->dind_bcnt = vol->ind_bcnt * vol->ind_bcnt;
135 vol->inode_size = vol->sb->s_inode_size;//EXT4_INODE_SIZE(vol->sb);
136
137 for (i = 0; i < 16; i++)
138 if (vol->sb->s_volume_name[i] == 0)
139 break;
140 s.type = FSW_STRING_TYPE_ISO88591;
141 s.size = s.len = i;
142 s.data = vol->sb->s_volume_name;
143 status = fsw_strdup_coerce(&vol->g.label, vol->g.host_string_type, &s);
144 if (status)
145 return status;
146
147 // size of group descriptor depends on feature....
148 if (!(vol->sb->s_feature_incompat & EXT4_FEATURE_INCOMPAT_64BIT)) {
149 // Default minimal group descriptor size...
150 vol->sb->s_desc_size = EXT4_MIN_DESC_SIZE;
151 }
152
153 // Calculate group descriptor count the way the kernel does it...
154 groupcnt = (vol->sb->s_blocks_count_lo - vol->sb->s_first_data_block +
155 vol->sb->s_blocks_per_group - 1) / vol->sb->s_blocks_per_group;
156 // Descriptors in one block... s_desc_size needs to be set!
157 gdesc_per_block = EXT4_DESC_PER_BLOCK(vol->sb);
158
159 // Read the group descriptors to get inode table offsets
160 status = fsw_alloc(sizeof(fsw_u32) * groupcnt, &vol->inotab_bno);
161 if (status)
162 return status;
163 for (groupno = 0; groupno < groupcnt; groupno++) {
164 // get the block group descriptor
165 gdesc_bno = (vol->sb->s_first_data_block + 1) + groupno / gdesc_per_block;
166 gdesc_index = groupno % gdesc_per_block;
167 status = fsw_block_get(vol, gdesc_bno, 1, (void **)&buffer);
168 if (status)
169 return status;
170 gdesc = (struct ext4_group_desc *)(buffer + gdesc_index * vol->sb->s_desc_size);
171 vol->inotab_bno[groupno] = gdesc->bg_inode_table_lo;
172 fsw_block_release(vol, gdesc_bno, buffer);
173 }
174
175 // setup the root dnode
176 status = fsw_dnode_create_root(vol, EXT4_ROOT_INO, &vol->g.root);
177 if (status)
178 return status;
179
180 FSW_MSG_DEBUG((FSW_MSGSTR("fsw_ext4_volume_mount: success, blocksize %d\n"), blocksize));
181
182 return FSW_SUCCESS;
183 }
184
185 /**
186 * Free the volume data structure. Called by the core after an unmount or after
187 * an unsuccessful mount to release the memory used by the file system type specific
188 * part of the volume structure.
189 */
190
191 static void fsw_ext4_volume_free(struct fsw_ext4_volume *vol)
192 {
193 if (vol->sb)
194 fsw_free(vol->sb);
195 if (vol->inotab_bno)
196 fsw_free(vol->inotab_bno);
197 }
198
199 /**
200 * Get in-depth information on a volume.
201 */
202
203 static fsw_status_t fsw_ext4_volume_stat(struct fsw_ext4_volume *vol, struct fsw_volume_stat *sb)
204 {
205 sb->total_bytes = (fsw_u64)vol->sb->s_blocks_count_lo * vol->g.log_blocksize;
206 sb->free_bytes = (fsw_u64)vol->sb->s_free_blocks_count_lo * vol->g.log_blocksize;
207 return FSW_SUCCESS;
208 }
209
210 /**
211 * Get full information on a dnode from disk. This function is called by the core
212 * whenever it needs to access fields in the dnode structure that may not
213 * be filled immediately upon creation of the dnode. In the case of ext4, we
214 * delay fetching of the inode structure until dnode_fill is called. The size and
215 * type fields are invalid until this function has been called.
216 */
217
218 static fsw_status_t fsw_ext4_dnode_fill(struct fsw_ext4_volume *vol, struct fsw_ext4_dnode *dno)
219 {
220 fsw_status_t status;
221 fsw_u32 groupno, ino_in_group, ino_bno, ino_index;
222 fsw_u8 *buffer;
223
224 if (dno->raw)
225 return FSW_SUCCESS;
226
227
228 // read the inode block
229 groupno = (dno->g.dnode_id - 1) / vol->sb->s_inodes_per_group;
230 ino_in_group = (dno->g.dnode_id - 1) % vol->sb->s_inodes_per_group;
231 ino_bno = vol->inotab_bno[groupno] +
232 ino_in_group / (vol->g.phys_blocksize / vol->inode_size);
233 ino_index = ino_in_group % (vol->g.phys_blocksize / vol->inode_size);
234 status = fsw_block_get(vol, ino_bno, 2, (void **)&buffer);
235
236 if (status)
237 return status;
238
239 // keep our inode around
240 status = fsw_memdup((void **)&dno->raw, buffer + ino_index * vol->inode_size, vol->inode_size);
241 fsw_block_release(vol, ino_bno, buffer);
242 if (status)
243 return status;
244
245 // get info from the inode
246 dno->g.size = dno->raw->i_size_lo; // TODO: check docs for 64-bit sized files
247
248 if (S_ISREG(dno->raw->i_mode))
249 dno->g.type = FSW_DNODE_TYPE_FILE;
250 else if (S_ISDIR(dno->raw->i_mode))
251 dno->g.type = FSW_DNODE_TYPE_DIR;
252 else if (S_ISLNK(dno->raw->i_mode))
253 dno->g.type = FSW_DNODE_TYPE_SYMLINK;
254 else
255 dno->g.type = FSW_DNODE_TYPE_SPECIAL;
256
257 FSW_MSG_DEBUG((FSW_MSGSTR("fsw_ext4_dnode_fill: inode flags %x\n"), dno->raw->i_flags));
258 FSW_MSG_DEBUG((FSW_MSGSTR("fsw_ext4_dnode_fill: i_mode %x\n"), dno->raw->i_mode));
259 return FSW_SUCCESS;
260 }
261
262 /**
263 * Free the dnode data structure. Called by the core when deallocating a dnode
264 * structure to release the memory used by the file system type specific part
265 * of the dnode structure.
266 */
267
268 static void fsw_ext4_dnode_free(struct fsw_ext4_volume *vol, struct fsw_ext4_dnode *dno)
269 {
270 if (dno->raw)
271 fsw_free(dno->raw);
272 }
273
274 /**
275 * Get in-depth information on a dnode. The core makes sure that fsw_ext4_dnode_fill
276 * has been called on the dnode before this function is called. Note that some
277 * data is not directly stored into the structure, but passed to a host-specific
278 * callback that converts it to the host-specific format.
279 */
280
281 static fsw_status_t fsw_ext4_dnode_stat(struct fsw_ext4_volume *vol, struct fsw_ext4_dnode *dno,
282 struct fsw_dnode_stat *sb)
283 {
284 sb->used_bytes = dno->raw->i_blocks_lo * EXT4_BLOCK_SIZE(vol->sb); // very, very strange...
285 sb->store_time_posix(sb, FSW_DNODE_STAT_CTIME, dno->raw->i_ctime);
286 sb->store_time_posix(sb, FSW_DNODE_STAT_ATIME, dno->raw->i_atime);
287 sb->store_time_posix(sb, FSW_DNODE_STAT_MTIME, dno->raw->i_mtime);
288 sb->store_attr_posix(sb, dno->raw->i_mode);
289
290 return FSW_SUCCESS;
291 }
292
293 /**
294 * Retrieve file data mapping information. This function is called by the core when
295 * fsw_shandle_read needs to know where on the disk the required piece of the file's
296 * data can be found. The core makes sure that fsw_ext4_dnode_fill has been called
297 * on the dnode before. Our task here is to get the physical disk block number for
298 * the requested logical block number.
299 *
300 * The ext4 file system usually uses extents do to store those disk block numbers.
301 * However, since ext4 is backward compatible, depending on inode flags the old direct
302 * and indirect addressing scheme can still be in place...
303 */
304
305 static fsw_status_t fsw_ext4_get_extent(struct fsw_ext4_volume *vol, struct fsw_ext4_dnode *dno,
306 struct fsw_extent *extent)
307 {
308 // Preconditions: The caller has checked that the requested logical block
309 // is within the file's size. The dnode has complete information, i.e.
310 // fsw_ext4_dnode_read_info was called successfully on it.
311 FSW_MSG_DEBUG((FSW_MSGSTR("fsw_ext4_get_extent: inode %d, block %d\n"), dno->g.dnode_id, extent->log_start));
312 extent->type = FSW_EXTENT_TYPE_PHYSBLOCK;
313 extent->log_count = 1;
314
315 if(dno->raw->i_flags & 1 << EXT4_INODE_EXTENTS)
316 {
317 FSW_MSG_DEBUG((FSW_MSGSTR("fsw_ext4_get_extent: inode %d uses extents\n"), dno->g.dnode_id));
318 return fsw_ext4_get_by_extent(vol, dno, extent);
319 }
320 else
321 {
322 FSW_MSG_DEBUG((FSW_MSGSTR("fsw_ext4_get_extent: inode %d uses direct/indirect block addressing\n"),
323 dno->g.dnode_id));
324 return fsw_ext4_get_by_blkaddr(vol, dno, extent);
325 }
326 }
327
328 /**
329 * New ext4 extents...
330 */
331 static fsw_status_t fsw_ext4_get_by_extent(struct fsw_ext4_volume *vol, struct fsw_ext4_dnode *dno,
332 struct fsw_extent *extent)
333 {
334 fsw_status_t status;
335 fsw_u32 bno, release_bno, buf_bcnt, buf_offset, file_bcnt;
336 int ext_cnt;
337 int ext_depth;
338 void *buffer;
339
340 struct ext4_extent_header *ext4_extent_header;
341 struct ext4_extent *ext4_extent;
342
343 // Logical block requested by core...
344 bno = extent->log_start;
345
346 // First buffer is the i_block field from inode...
347 buffer = (void *)dno->raw->i_block;
348 buf_bcnt = EXT4_NDIR_BLOCKS;
349 buf_offset = 0;
350
351 ext4_extent_header = (struct ext4_extent_header *)buffer + buf_offset;
352 buf_offset += sizeof(struct ext4_extent_header);
353 FSW_MSG_DEBUG((FSW_MSGSTR("fsw_ext4_get_by_extent: extent header magic %x\n"),
354 ext4_extent_header->eh_magic));
355 if(ext4_extent_header->eh_magic != EXT4_EXT_MAGIC)
356 return FSW_VOLUME_CORRUPTED;
357
358 if(ext4_extent_header->eh_depth == 0)
359 {
360 // Leaf node, the header follows actual extents
361 FSW_MSG_DEBUG((FSW_MSGSTR("fsw_ext4_get_by_extent: leaf extent with %d extents\n"),
362 ext4_extent_header->eh_entries));
363
364 for(ext_cnt = 0;ext_cnt < ext4_extent_header->eh_entries;ext_cnt++)
365 {
366 ext4_extent = (struct ext4_extent *)(buffer + buf_offset);
367 buf_offset += sizeof(struct ext4_extent);
368 FSW_MSG_DEBUG((FSW_MSGSTR("fsw_ext4_get_by_extent: extent with %d len\n"), ext4_extent->ee_len));
369 FSW_MSG_DEBUG((FSW_MSGSTR("fsw_ext4_get_by_extent: extent with %d start_hi\n"), ext4_extent->ee_start_hi));
370 FSW_MSG_DEBUG((FSW_MSGSTR("fsw_ext4_get_by_extent: extent with %d start_lo\n"), ext4_extent->ee_start_lo));
371 // Is the requested block in this extent?
372 if(bno >= ext4_extent->ee_block && bno < ext4_extent->ee_block + ext4_extent->ee_len)
373 {
374 extent->phys_start = ext4_extent->ee_start_lo + bno;
375 extent->log_count = ext4_extent->ee_len - (bno - ext4_extent->ee_block);
376 return FSW_SUCCESS;
377 }
378 }
379 }
380 else
381 {
382 // Follow extent tree...
383 }
384
385 return FSW_NOT_FOUND;
386 }
387
388 /**
389 * The ext2/ext3 file system does not use extents, but stores a list of block numbers
390 * using the usual direct, indirect, double-indirect, triple-indirect scheme. To
391 * optimize access, this function checks if the following file blocks are mapped
392 * to consecutive disk blocks and returns a combined extent if possible.
393 */
394 static fsw_status_t fsw_ext4_get_by_blkaddr(struct fsw_ext4_volume *vol, struct fsw_ext4_dnode *dno,
395 struct fsw_extent *extent)
396 {
397 fsw_status_t status;
398 fsw_u32 bno, release_bno, buf_bcnt, file_bcnt;
399 int path[5], i;
400 fsw_u32 *buffer;
401 bno = extent->log_start;
402
403 // try direct block pointers in the inode
404 if (bno < EXT4_NDIR_BLOCKS) {
405 path[0] = bno;
406 path[1] = -1;
407 } else {
408 bno -= EXT4_NDIR_BLOCKS;
409
410 // try indirect block
411 if (bno < vol->ind_bcnt) {
412 path[0] = EXT4_IND_BLOCK;
413 path[1] = bno;
414 path[2] = -1;
415 } else {
416 bno -= vol->ind_bcnt;
417
418 // try double-indirect block
419 if (bno < vol->dind_bcnt) {
420 path[0] = EXT4_DIND_BLOCK;
421 path[1] = bno / vol->ind_bcnt;
422 path[2] = bno % vol->ind_bcnt;
423 path[3] = -1;
424 } else {
425 bno -= vol->dind_bcnt;
426
427 // use the triple-indirect block
428 path[0] = EXT4_TIND_BLOCK;
429 path[1] = bno / vol->dind_bcnt;
430 path[2] = (bno / vol->ind_bcnt) % vol->ind_bcnt;
431 path[3] = bno % vol->ind_bcnt;
432 path[4] = -1;
433 }
434 }
435 }
436
437 // follow the indirection path
438 buffer = dno->raw->i_block;
439 buf_bcnt = EXT4_NDIR_BLOCKS;
440 release_bno = 0;
441 for (i = 0; ; i++) {
442 bno = buffer[path[i]];
443 if (bno == 0) {
444 extent->type = FSW_EXTENT_TYPE_SPARSE;
445 if (release_bno)
446 fsw_block_release(vol, release_bno, buffer);
447 return FSW_SUCCESS;
448 }
449 if (path[i+1] < 0)
450 break;
451
452 if (release_bno)
453 fsw_block_release(vol, release_bno, buffer);
454 status = fsw_block_get(vol, bno, 1, (void **)&buffer);
455 if (status)
456 return status;
457 release_bno = bno;
458 buf_bcnt = vol->ind_bcnt;
459 }
460 extent->phys_start = bno;
461
462 // check if the following blocks can be aggregated into one extent
463 file_bcnt = (fsw_u32)((dno->g.size + vol->g.log_blocksize - 1) & (vol->g.log_blocksize - 1));
464 while (path[i] + extent->log_count < buf_bcnt && // indirect block has more block pointers
465 extent->log_start + extent->log_count < file_bcnt) { // file has more blocks
466 if (buffer[path[i] + extent->log_count] == buffer[path[i] + extent->log_count - 1] + 1)
467 extent->log_count++;
468 else
469 break;
470 }
471
472 if (release_bno)
473 fsw_block_release(vol, release_bno, buffer);
474 return FSW_SUCCESS;
475 }
476
477 /**
478 * Lookup a directory's child dnode by name. This function is called on a directory
479 * to retrieve the directory entry with the given name. A dnode is constructed for
480 * this entry and returned. The core makes sure that fsw_ext4_dnode_fill has been called
481 * and the dnode is actually a directory.
482 */
483
484 static fsw_status_t fsw_ext4_dir_lookup(struct fsw_ext4_volume *vol, struct fsw_ext4_dnode *dno,
485 struct fsw_string *lookup_name, struct fsw_ext4_dnode **child_dno_out)
486 {
487 fsw_status_t status;
488 struct fsw_shandle shand;
489 fsw_u32 child_ino;
490 struct ext4_dir_entry entry;
491 struct fsw_string entry_name;
492
493 // Preconditions: The caller has checked that dno is a directory node.
494
495 entry_name.type = FSW_STRING_TYPE_ISO88591;
496
497 // setup handle to read the directory
498 status = fsw_shandle_open(dno, &shand);
499 if (status)
500 return status;
501
502 // scan the directory for the file
503 child_ino = 0;
504 while (child_ino == 0) {
505 // read next entry
506 status = fsw_ext4_read_dentry(&shand, &entry);
507 if (status)
508 goto errorexit;
509 if (entry.inode == 0) {
510 // end of directory reached
511 status = FSW_NOT_FOUND;
512 goto errorexit;
513 }
514
515 // compare name
516 entry_name.len = entry_name.size = entry.name_len;
517 entry_name.data = entry.name;
518 if (fsw_streq(lookup_name, &entry_name)) {
519 child_ino = entry.inode;
520 break;
521 }
522 }
523
524 // setup a dnode for the child item
525 status = fsw_dnode_create(dno, child_ino, FSW_DNODE_TYPE_UNKNOWN, &entry_name, child_dno_out);
526
527 errorexit:
528 fsw_shandle_close(&shand);
529 return status;
530 }
531
532 /**
533 * Get the next directory entry when reading a directory. This function is called during
534 * directory iteration to retrieve the next directory entry. A dnode is constructed for
535 * the entry and returned. The core makes sure that fsw_ext4_dnode_fill has been called
536 * and the dnode is actually a directory. The shandle provided by the caller is used to
537 * record the position in the directory between calls.
538 */
539
540 static fsw_status_t fsw_ext4_dir_read(struct fsw_ext4_volume *vol, struct fsw_ext4_dnode *dno,
541 struct fsw_shandle *shand, struct fsw_ext4_dnode **child_dno_out)
542 {
543 fsw_status_t status;
544 struct ext4_dir_entry entry;
545 struct fsw_string entry_name;
546
547 // Preconditions: The caller has checked that dno is a directory node. The caller
548 // has opened a storage handle to the directory's storage and keeps it around between
549 // calls.
550 FSW_MSG_DEBUG((FSW_MSGSTR("fsw_ext4_dir_read: started reading dir\n")));
551
552 while (1) {
553 // read next entry
554 status = fsw_ext4_read_dentry(shand, &entry);
555 if (status)
556 return status;
557 if (entry.inode == 0) // end of directory
558 return FSW_NOT_FOUND;
559
560 // skip . and ..
561 if ((entry.name_len == 1 && entry.name[0] == '.') ||
562 (entry.name_len == 2 && entry.name[0] == '.' && entry.name[1] == '.'))
563 continue;
564 break;
565 }
566
567 // setup name
568 entry_name.type = FSW_STRING_TYPE_ISO88591;
569 entry_name.len = entry_name.size = entry.name_len;
570 entry_name.data = entry.name;
571
572 // setup a dnode for the child item
573 status = fsw_dnode_create(dno, entry.inode, FSW_DNODE_TYPE_UNKNOWN, &entry_name, child_dno_out);
574
575 return status;
576 }
577
578 /**
579 * Read a directory entry from the directory's raw data. This internal function is used
580 * to read a raw ext2 directory entry into memory. The shandle's position pointer is adjusted
581 * to point to the next entry.
582 */
583
584 static fsw_status_t fsw_ext4_read_dentry(struct fsw_shandle *shand, struct ext4_dir_entry *entry)
585 {
586 fsw_status_t status;
587 fsw_u32 buffer_size;
588
589 while (1) {
590 // read dir_entry header (fixed length)
591 buffer_size = 8;
592 status = fsw_shandle_read(shand, &buffer_size, entry);
593 if (status)
594 return status;
595
596 if (buffer_size < 8 || entry->rec_len == 0) {
597 // end of directory reached
598 entry->inode = 0;
599 return FSW_SUCCESS;
600 }
601 if (entry->rec_len < 8)
602 return FSW_VOLUME_CORRUPTED;
603 if (entry->inode != 0) {
604 // this entry is used
605 if (entry->rec_len < 8 + entry->name_len)
606 return FSW_VOLUME_CORRUPTED;
607 break;
608 }
609
610 // valid, but unused entry, skip it
611 shand->pos += entry->rec_len - 8;
612 }
613
614 // read file name (variable length)
615 buffer_size = entry->name_len;
616 status = fsw_shandle_read(shand, &buffer_size, entry->name);
617 if (status)
618 return status;
619 if (buffer_size < entry->name_len)
620 return FSW_VOLUME_CORRUPTED;
621
622 // skip any remaining padding
623 shand->pos += entry->rec_len - (8 + entry->name_len);
624
625 return FSW_SUCCESS;
626 }
627
628 /**
629 * Get the target path of a symbolic link. This function is called when a symbolic
630 * link needs to be resolved. The core makes sure that the fsw_ext4_dnode_fill has been
631 * called on the dnode and that it really is a symlink.
632 *
633 * For ext4, the target path can be stored inline in the inode structure (in the space
634 * otherwise occupied by the block pointers) or in the inode's data. There is no flag
635 * indicating this, only the number of blocks entry (i_blocks) can be used as an
636 * indication. The check used here comes from the Linux kernel.
637 */
638
639 static fsw_status_t fsw_ext4_readlink(struct fsw_ext4_volume *vol, struct fsw_ext4_dnode *dno,
640 struct fsw_string *link_target)
641 {
642 fsw_status_t status;
643 int ea_blocks;
644 struct fsw_string s;
645
646 if (dno->g.size > FSW_PATH_MAX)
647 return FSW_VOLUME_CORRUPTED;
648
649 /* Linux kernels ext4_inode_is_fast_symlink... */
650 ea_blocks = dno->raw->i_file_acl_lo ? (vol->g.log_blocksize >> 9) : 0;
651
652 if (dno->raw->i_blocks_lo - ea_blocks == 0) {
653 // "fast" symlink, path is stored inside the inode
654 s.type = FSW_STRING_TYPE_ISO88591;
655 s.size = s.len = (int)dno->g.size;
656 s.data = dno->raw->i_block;
657 status = fsw_strdup_coerce(link_target, vol->g.host_string_type, &s);
658 } else {
659 // "slow" symlink, path is stored in normal inode data
660 status = fsw_dnode_readlink_data(dno, link_target);
661 }
662
663 return status;
664 }
665
666 // EOF