]> code.delx.au - refind/blob - filesystems/fsw_ext2.c
Modified SIP/CSR feature to work ON MACS when the csr-active-config
[refind] / filesystems / fsw_ext2.c
1 /**
2 * \file fsw_ext2.c
3 * ext2 file system driver code.
4 */
5
6 /*-
7 * Copyright (c) 2006 Christoph Pfisterer
8 *
9 * This program is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU General Public License
11 * as published by the Free Software Foundation; either version 2
12 * of the License, or (at your option) any later version.
13 *
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
22 */
23
24 #include "fsw_ext2.h"
25
26
27 // functions
28
29 static fsw_status_t fsw_ext2_volume_mount(struct fsw_ext2_volume *vol);
30 static void fsw_ext2_volume_free(struct fsw_ext2_volume *vol);
31 static fsw_status_t fsw_ext2_volume_stat(struct fsw_ext2_volume *vol, struct fsw_volume_stat *sb);
32
33 static fsw_status_t fsw_ext2_dnode_fill(struct fsw_ext2_volume *vol, struct fsw_ext2_dnode *dno);
34 static void fsw_ext2_dnode_free(struct fsw_ext2_volume *vol, struct fsw_ext2_dnode *dno);
35 static fsw_status_t fsw_ext2_dnode_stat(struct fsw_ext2_volume *vol, struct fsw_ext2_dnode *dno,
36 struct fsw_dnode_stat *sb);
37 static fsw_status_t fsw_ext2_get_extent(struct fsw_ext2_volume *vol, struct fsw_ext2_dnode *dno,
38 struct fsw_extent *extent);
39
40 static fsw_status_t fsw_ext2_dir_lookup(struct fsw_ext2_volume *vol, struct fsw_ext2_dnode *dno,
41 struct fsw_string *lookup_name, struct fsw_ext2_dnode **child_dno);
42 static fsw_status_t fsw_ext2_dir_read(struct fsw_ext2_volume *vol, struct fsw_ext2_dnode *dno,
43 struct fsw_shandle *shand, struct fsw_ext2_dnode **child_dno);
44 static fsw_status_t fsw_ext2_read_dentry(struct fsw_shandle *shand, struct ext2_dir_entry *entry);
45
46 static fsw_status_t fsw_ext2_readlink(struct fsw_ext2_volume *vol, struct fsw_ext2_dnode *dno,
47 struct fsw_string *link);
48
49 //
50 // Dispatch Table
51 //
52
53 struct fsw_fstype_table FSW_FSTYPE_TABLE_NAME(ext2) = {
54 { FSW_STRING_TYPE_ISO88591, 4, 4, "ext2" },
55 sizeof(struct fsw_ext2_volume),
56 sizeof(struct fsw_ext2_dnode),
57
58 fsw_ext2_volume_mount,
59 fsw_ext2_volume_free,
60 fsw_ext2_volume_stat,
61 fsw_ext2_dnode_fill,
62 fsw_ext2_dnode_free,
63 fsw_ext2_dnode_stat,
64 fsw_ext2_get_extent,
65 fsw_ext2_dir_lookup,
66 fsw_ext2_dir_read,
67 fsw_ext2_readlink,
68 };
69
70 /**
71 * Mount an ext2 volume. Reads the superblock and constructs the
72 * root directory dnode.
73 */
74
75 static fsw_status_t fsw_ext2_volume_mount(struct fsw_ext2_volume *vol)
76 {
77 fsw_status_t status;
78 void *buffer;
79 fsw_u32 blocksize;
80 fsw_u32 groupcnt, groupno, gdesc_per_block, gdesc_bno, gdesc_index;
81 struct ext2_group_desc *gdesc;
82 int i;
83 struct fsw_string s;
84
85 // allocate memory to keep the superblock around
86 status = fsw_alloc(sizeof(struct ext2_super_block), &vol->sb);
87 if (status)
88 return status;
89
90 // read the superblock into its buffer
91 fsw_set_blocksize(vol, EXT2_SUPERBLOCK_BLOCKSIZE, EXT2_SUPERBLOCK_BLOCKSIZE);
92 status = fsw_block_get(vol, EXT2_SUPERBLOCK_BLOCKNO, 0, &buffer);
93 if (status)
94 return status;
95 fsw_memcpy(vol->sb, buffer, sizeof(struct ext2_super_block));
96 fsw_block_release(vol, EXT2_SUPERBLOCK_BLOCKNO, buffer);
97
98 // check the superblock
99 if (vol->sb->s_magic != EXT2_SUPER_MAGIC)
100 return FSW_UNSUPPORTED;
101 if (vol->sb->s_rev_level != EXT2_GOOD_OLD_REV &&
102 vol->sb->s_rev_level != EXT2_DYNAMIC_REV)
103 return FSW_UNSUPPORTED;
104 if (vol->sb->s_rev_level == EXT2_DYNAMIC_REV &&
105 (vol->sb->s_feature_incompat & ~(EXT2_FEATURE_INCOMPAT_FILETYPE | EXT3_FEATURE_INCOMPAT_RECOVER)))
106 return FSW_UNSUPPORTED;
107
108 /*
109 if (vol->sb->s_rev_level == EXT2_DYNAMIC_REV &&
110 (vol->sb->s_feature_incompat & EXT3_FEATURE_INCOMPAT_RECOVER))
111 Print(L"Ext2 WARNING: This ext3 file system needs recovery, trying to use it anyway.\n");
112 */
113
114 // set real blocksize
115 blocksize = EXT2_BLOCK_SIZE(vol->sb);
116 fsw_set_blocksize(vol, blocksize, blocksize);
117
118 // get other info from superblock
119 vol->ind_bcnt = EXT2_ADDR_PER_BLOCK(vol->sb);
120 vol->dind_bcnt = vol->ind_bcnt * vol->ind_bcnt;
121 vol->inode_size = EXT2_INODE_SIZE(vol->sb);
122
123 for (i = 0; i < 16; i++)
124 if (vol->sb->s_volume_name[i] == 0)
125 break;
126 s.type = FSW_STRING_TYPE_ISO88591;
127 s.size = s.len = i;
128 s.data = vol->sb->s_volume_name;
129 status = fsw_strdup_coerce(&vol->g.label, vol->g.host_string_type, &s);
130 if (status)
131 return status;
132
133 // read the group descriptors to get inode table offsets
134 groupcnt = ((vol->sb->s_inodes_count - 2) / vol->sb->s_inodes_per_group) + 1;
135 gdesc_per_block = (vol->g.phys_blocksize / sizeof(struct ext2_group_desc));
136
137 status = fsw_alloc(sizeof(fsw_u32) * groupcnt, &vol->inotab_bno);
138 if (status)
139 return status;
140 for (groupno = 0; groupno < groupcnt; groupno++) {
141 // get the block group descriptor
142 gdesc_bno = (vol->sb->s_first_data_block + 1) + groupno / gdesc_per_block;
143 gdesc_index = groupno % gdesc_per_block;
144 status = fsw_block_get(vol, gdesc_bno, 1, (void **)&buffer);
145 if (status)
146 return status;
147 gdesc = ((struct ext2_group_desc *)(buffer)) + gdesc_index;
148 vol->inotab_bno[groupno] = gdesc->bg_inode_table;
149 fsw_block_release(vol, gdesc_bno, buffer);
150 }
151
152 // setup the root dnode
153 status = fsw_dnode_create_root(vol, EXT2_ROOT_INO, &vol->g.root);
154 if (status)
155 return status;
156
157 FSW_MSG_DEBUG((FSW_MSGSTR("fsw_ext2_volume_mount: success, blocksize %d\n"), blocksize));
158
159 return FSW_SUCCESS;
160 }
161
162 /**
163 * Free the volume data structure. Called by the core after an unmount or after
164 * an unsuccessful mount to release the memory used by the file system type specific
165 * part of the volume structure.
166 */
167
168 static void fsw_ext2_volume_free(struct fsw_ext2_volume *vol)
169 {
170 if (vol->sb)
171 fsw_free(vol->sb);
172 if (vol->inotab_bno)
173 fsw_free(vol->inotab_bno);
174 }
175
176 /**
177 * Get in-depth information on a volume.
178 */
179
180 static fsw_status_t fsw_ext2_volume_stat(struct fsw_ext2_volume *vol, struct fsw_volume_stat *sb)
181 {
182 sb->total_bytes = (fsw_u64)vol->sb->s_blocks_count * vol->g.log_blocksize;
183 sb->free_bytes = (fsw_u64)vol->sb->s_free_blocks_count * vol->g.log_blocksize;
184 return FSW_SUCCESS;
185 }
186
187 /**
188 * Get full information on a dnode from disk. This function is called by the core
189 * whenever it needs to access fields in the dnode structure that may not
190 * be filled immediately upon creation of the dnode. In the case of ext2, we
191 * delay fetching of the inode structure until dnode_fill is called. The size and
192 * type fields are invalid until this function has been called.
193 */
194
195 static fsw_status_t fsw_ext2_dnode_fill(struct fsw_ext2_volume *vol, struct fsw_ext2_dnode *dno)
196 {
197 fsw_status_t status;
198 fsw_u32 groupno, ino_in_group, ino_bno, ino_index;
199 fsw_u8 *buffer;
200
201 if (dno->raw)
202 return FSW_SUCCESS;
203
204 FSW_MSG_DEBUG((FSW_MSGSTR("fsw_ext2_dnode_fill: inode %d\n"), dno->g.dnode_id));
205
206 // read the inode block
207 groupno = (fsw_u32) (dno->g.dnode_id - 1) / vol->sb->s_inodes_per_group;
208 ino_in_group = (fsw_u32) (dno->g.dnode_id - 1) % vol->sb->s_inodes_per_group;
209 ino_bno = vol->inotab_bno[groupno] +
210 ino_in_group / (vol->g.phys_blocksize / vol->inode_size);
211 ino_index = ino_in_group % (vol->g.phys_blocksize / vol->inode_size);
212 status = fsw_block_get(vol, ino_bno, 2, (void **)&buffer);
213 if (status)
214 return status;
215
216 // keep our inode around
217 status = fsw_memdup((void **)&dno->raw, buffer + ino_index * vol->inode_size, vol->inode_size);
218 fsw_block_release(vol, ino_bno, buffer);
219 if (status)
220 return status;
221
222 // get info from the inode
223 dno->g.size = dno->raw->i_size;
224 // TODO: check docs for 64-bit sized files
225 if (S_ISREG(dno->raw->i_mode))
226 dno->g.type = FSW_DNODE_TYPE_FILE;
227 else if (S_ISDIR(dno->raw->i_mode))
228 dno->g.type = FSW_DNODE_TYPE_DIR;
229 else if (S_ISLNK(dno->raw->i_mode))
230 dno->g.type = FSW_DNODE_TYPE_SYMLINK;
231 else
232 dno->g.type = FSW_DNODE_TYPE_SPECIAL;
233
234 return FSW_SUCCESS;
235 }
236
237 /**
238 * Free the dnode data structure. Called by the core when deallocating a dnode
239 * structure to release the memory used by the file system type specific part
240 * of the dnode structure.
241 */
242
243 static void fsw_ext2_dnode_free(struct fsw_ext2_volume *vol, struct fsw_ext2_dnode *dno)
244 {
245 if (dno->raw)
246 fsw_free(dno->raw);
247 }
248
249 /**
250 * Get in-depth information on a dnode. The core makes sure that fsw_ext2_dnode_fill
251 * has been called on the dnode before this function is called. Note that some
252 * data is not directly stored into the structure, but passed to a host-specific
253 * callback that converts it to the host-specific format.
254 */
255
256 static fsw_status_t fsw_ext2_dnode_stat(struct fsw_ext2_volume *vol, struct fsw_ext2_dnode *dno,
257 struct fsw_dnode_stat *sb)
258 {
259 sb->used_bytes = dno->raw->i_blocks * 512; // very, very strange...
260 fsw_store_time_posix(sb, FSW_DNODE_STAT_CTIME, dno->raw->i_ctime);
261 fsw_store_time_posix(sb, FSW_DNODE_STAT_ATIME, dno->raw->i_atime);
262 fsw_store_time_posix(sb, FSW_DNODE_STAT_MTIME, dno->raw->i_mtime);
263 fsw_store_attr_posix(sb, dno->raw->i_mode);
264
265 return FSW_SUCCESS;
266 }
267
268 /**
269 * Retrieve file data mapping information. This function is called by the core when
270 * fsw_shandle_read needs to know where on the disk the required piece of the file's
271 * data can be found. The core makes sure that fsw_ext2_dnode_fill has been called
272 * on the dnode before. Our task here is to get the physical disk block number for
273 * the requested logical block number.
274 *
275 * The ext2 file system does not use extents, but stores a list of block numbers
276 * using the usual direct, indirect, double-indirect, triple-indirect scheme. To
277 * optimize access, this function checks if the following file blocks are mapped
278 * to consecutive disk blocks and returns a combined extent if possible.
279 */
280
281 static fsw_status_t fsw_ext2_get_extent(struct fsw_ext2_volume *vol, struct fsw_ext2_dnode *dno,
282 struct fsw_extent *extent)
283 {
284 fsw_status_t status;
285 fsw_u32 bno, release_bno, buf_bcnt, file_bcnt;
286 fsw_u32 *buffer;
287 int path[5], i;
288
289 // Preconditions: The caller has checked that the requested logical block
290 // is within the file's size. The dnode has complete information, i.e.
291 // fsw_ext2_dnode_read_info was called successfully on it.
292
293 extent->type = FSW_EXTENT_TYPE_PHYSBLOCK;
294 extent->log_count = 1;
295 bno = extent->log_start;
296
297 // try direct block pointers in the inode
298 if (bno < EXT2_NDIR_BLOCKS) {
299 path[0] = bno;
300 path[1] = -1;
301 } else {
302 bno -= EXT2_NDIR_BLOCKS;
303
304 // try indirect block
305 if (bno < vol->ind_bcnt) {
306 path[0] = EXT2_IND_BLOCK;
307 path[1] = bno;
308 path[2] = -1;
309 } else {
310 bno -= vol->ind_bcnt;
311
312 // try double-indirect block
313 if (bno < vol->dind_bcnt) {
314 path[0] = EXT2_DIND_BLOCK;
315 path[1] = bno / vol->ind_bcnt;
316 path[2] = bno % vol->ind_bcnt;
317 path[3] = -1;
318 } else {
319 bno -= vol->dind_bcnt;
320
321 // use the triple-indirect block
322 path[0] = EXT2_TIND_BLOCK;
323 path[1] = bno / vol->dind_bcnt;
324 path[2] = (bno / vol->ind_bcnt) % vol->ind_bcnt;
325 path[3] = bno % vol->ind_bcnt;
326 path[4] = -1;
327 }
328 }
329 }
330
331 // follow the indirection path
332 buffer = dno->raw->i_block;
333 buf_bcnt = EXT2_NDIR_BLOCKS;
334 release_bno = 0;
335 for (i = 0; ; i++) {
336 bno = buffer[path[i]];
337 if (bno == 0) {
338 extent->type = FSW_EXTENT_TYPE_SPARSE;
339 if (release_bno)
340 fsw_block_release(vol, release_bno, buffer);
341 return FSW_SUCCESS;
342 }
343 if (path[i+1] < 0)
344 break;
345
346 if (release_bno)
347 fsw_block_release(vol, release_bno, buffer);
348 status = fsw_block_get(vol, bno, 1, (void **)&buffer);
349 if (status)
350 return status;
351 release_bno = bno;
352 buf_bcnt = vol->ind_bcnt;
353 }
354 extent->phys_start = bno;
355
356 // check if the following blocks can be aggregated into one extent
357 file_bcnt = (fsw_u32)((dno->g.size + vol->g.log_blocksize - 1) & (vol->g.log_blocksize - 1));
358 while (path[i] + extent->log_count < buf_bcnt && // indirect block has more block pointers
359 extent->log_start + extent->log_count < file_bcnt) { // file has more blocks
360 if (buffer[path[i] + extent->log_count] == buffer[path[i] + extent->log_count - 1] + 1)
361 extent->log_count++;
362 else
363 break;
364 }
365
366 if (release_bno)
367 fsw_block_release(vol, release_bno, buffer);
368 return FSW_SUCCESS;
369 }
370
371 /**
372 * Lookup a directory's child dnode by name. This function is called on a directory
373 * to retrieve the directory entry with the given name. A dnode is constructed for
374 * this entry and returned. The core makes sure that fsw_ext2_dnode_fill has been called
375 * and the dnode is actually a directory.
376 */
377
378 static fsw_status_t fsw_ext2_dir_lookup(struct fsw_ext2_volume *vol, struct fsw_ext2_dnode *dno,
379 struct fsw_string *lookup_name, struct fsw_ext2_dnode **child_dno_out)
380 {
381 fsw_status_t status;
382 struct fsw_shandle shand;
383 fsw_u32 child_ino;
384 struct ext2_dir_entry entry;
385 struct fsw_string entry_name;
386
387 // Preconditions: The caller has checked that dno is a directory node.
388
389 entry_name.type = FSW_STRING_TYPE_ISO88591;
390
391 // setup handle to read the directory
392 status = fsw_shandle_open(dno, &shand);
393 if (status)
394 return status;
395
396 // scan the directory for the file
397 child_ino = 0;
398 while (child_ino == 0) {
399 // read next entry
400 status = fsw_ext2_read_dentry(&shand, &entry);
401 if (status)
402 goto errorexit;
403 if (entry.inode == 0) {
404 // end of directory reached
405 status = FSW_NOT_FOUND;
406 goto errorexit;
407 }
408
409 // compare name
410 entry_name.len = entry_name.size = entry.name_len;
411 entry_name.data = entry.name;
412 if (fsw_streq(lookup_name, &entry_name)) {
413 child_ino = entry.inode;
414 break;
415 }
416 }
417
418 // setup a dnode for the child item
419 status = fsw_dnode_create(dno, child_ino, FSW_DNODE_TYPE_UNKNOWN, &entry_name, child_dno_out);
420
421 errorexit:
422 fsw_shandle_close(&shand);
423 return status;
424 }
425
426 /**
427 * Get the next directory entry when reading a directory. This function is called during
428 * directory iteration to retrieve the next directory entry. A dnode is constructed for
429 * the entry and returned. The core makes sure that fsw_ext2_dnode_fill has been called
430 * and the dnode is actually a directory. The shandle provided by the caller is used to
431 * record the position in the directory between calls.
432 */
433
434 static fsw_status_t fsw_ext2_dir_read(struct fsw_ext2_volume *vol, struct fsw_ext2_dnode *dno,
435 struct fsw_shandle *shand, struct fsw_ext2_dnode **child_dno_out)
436 {
437 fsw_status_t status;
438 struct ext2_dir_entry entry;
439 struct fsw_string entry_name;
440
441 // Preconditions: The caller has checked that dno is a directory node. The caller
442 // has opened a storage handle to the directory's storage and keeps it around between
443 // calls.
444
445 while (1) {
446 // read next entry
447 status = fsw_ext2_read_dentry(shand, &entry);
448 if (status)
449 return status;
450 if (entry.inode == 0) // end of directory
451 return FSW_NOT_FOUND;
452
453 // skip . and ..
454 if ((entry.name_len == 1 && entry.name[0] == '.') ||
455 (entry.name_len == 2 && entry.name[0] == '.' && entry.name[1] == '.'))
456 continue;
457 break;
458 }
459
460 // setup name
461 entry_name.type = FSW_STRING_TYPE_ISO88591;
462 entry_name.len = entry_name.size = entry.name_len;
463 entry_name.data = entry.name;
464
465 // setup a dnode for the child item
466 status = fsw_dnode_create(dno, entry.inode, FSW_DNODE_TYPE_UNKNOWN, &entry_name, child_dno_out);
467
468 return status;
469 }
470
471 /**
472 * Read a directory entry from the directory's raw data. This internal function is used
473 * to read a raw ext2 directory entry into memory. The shandle's position pointer is adjusted
474 * to point to the next entry.
475 */
476
477 static fsw_status_t fsw_ext2_read_dentry(struct fsw_shandle *shand, struct ext2_dir_entry *entry)
478 {
479 fsw_status_t status;
480 fsw_u32 buffer_size;
481
482 while (1) {
483 // read dir_entry header (fixed length)
484 buffer_size = 8;
485 status = fsw_shandle_read(shand, &buffer_size, entry);
486 if (status)
487 return status;
488
489 if (buffer_size < 8 || entry->rec_len == 0) {
490 // end of directory reached
491 entry->inode = 0;
492 return FSW_SUCCESS;
493 }
494 if (entry->rec_len < 8)
495 return FSW_VOLUME_CORRUPTED;
496 if (entry->inode != 0) {
497 // this entry is used
498 if (entry->rec_len < 8 + entry->name_len)
499 return FSW_VOLUME_CORRUPTED;
500 break;
501 }
502
503 // valid, but unused entry, skip it
504 shand->pos += entry->rec_len - 8;
505 }
506
507 // read file name (variable length)
508 buffer_size = entry->name_len;
509 status = fsw_shandle_read(shand, &buffer_size, entry->name);
510 if (status)
511 return status;
512 if (buffer_size < entry->name_len)
513 return FSW_VOLUME_CORRUPTED;
514
515 // skip any remaining padding
516 shand->pos += entry->rec_len - (8 + entry->name_len);
517
518 return FSW_SUCCESS;
519 }
520
521 /**
522 * Get the target path of a symbolic link. This function is called when a symbolic
523 * link needs to be resolved. The core makes sure that the fsw_ext2_dnode_fill has been
524 * called on the dnode and that it really is a symlink.
525 *
526 * For ext2, the target path can be stored inline in the inode structure (in the space
527 * otherwise occupied by the block pointers) or in the inode's data. There is no flag
528 * indicating this, only the number of blocks entry (i_blocks) can be used as an
529 * indication. The check used here comes from the Linux kernel.
530 */
531
532 static fsw_status_t fsw_ext2_readlink(struct fsw_ext2_volume *vol, struct fsw_ext2_dnode *dno,
533 struct fsw_string *link_target)
534 {
535 fsw_status_t status;
536 int ea_blocks;
537 struct fsw_string s;
538
539 if (dno->g.size > FSW_PATH_MAX)
540 return FSW_VOLUME_CORRUPTED;
541
542 ea_blocks = dno->raw->i_file_acl ? (vol->g.log_blocksize >> 9) : 0;
543
544 if (dno->raw->i_blocks - ea_blocks == 0) {
545 // "fast" symlink, path is stored inside the inode
546 s.type = FSW_STRING_TYPE_ISO88591;
547 s.size = s.len = (int)dno->g.size;
548 s.data = dno->raw->i_block;
549 status = fsw_strdup_coerce(link_target, vol->g.host_string_type, &s);
550 } else {
551 // "slow" symlink, path is stored in normal inode data
552 status = fsw_dnode_readlink_data(dno, link_target);
553 }
554
555 return status;
556 }
557
558 // EOF