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