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