3 * ext2 file system driver code.
7 * Copyright (c) 2006 Christoph Pfisterer
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.
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.
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.
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
);
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
);
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
);
46 static fsw_status_t
fsw_ext2_readlink(struct fsw_ext2_volume
*vol
, struct fsw_ext2_dnode
*dno
,
47 struct fsw_string
*link
);
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
),
58 fsw_ext2_volume_mount
,
71 * Mount an ext2 volume. Reads the superblock and constructs the
72 * root directory dnode.
75 static fsw_status_t
fsw_ext2_volume_mount(struct fsw_ext2_volume
*vol
)
80 fsw_u32 groupcnt
, groupno
, gdesc_per_block
, gdesc_bno
, gdesc_index
;
81 struct ext2_group_desc
*gdesc
;
85 // allocate memory to keep the superblock around
86 status
= fsw_alloc(sizeof(struct ext2_super_block
), &vol
->sb
);
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
);
95 fsw_memcpy(vol
->sb
, buffer
, sizeof(struct ext2_super_block
));
96 fsw_block_release(vol
, EXT2_SUPERBLOCK_BLOCKNO
, buffer
);
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
;
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");
114 // set real blocksize
115 blocksize
= EXT2_BLOCK_SIZE(vol
->sb
);
116 fsw_set_blocksize(vol
, blocksize
, blocksize
);
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
);
123 for (i
= 0; i
< 16; i
++)
124 if (vol
->sb
->s_volume_name
[i
] == 0)
126 s
.type
= FSW_STRING_TYPE_ISO88591
;
128 s
.data
= vol
->sb
->s_volume_name
;
129 status
= fsw_strdup_coerce(&vol
->g
.label
, vol
->g
.host_string_type
, &s
);
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
));
137 status
= fsw_alloc(sizeof(fsw_u32
) * groupcnt
, &vol
->inotab_bno
);
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
);
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
);
152 // setup the root dnode
153 status
= fsw_dnode_create_root(vol
, EXT2_ROOT_INO
, &vol
->g
.root
);
157 FSW_MSG_DEBUG((FSW_MSGSTR("fsw_ext2_volume_mount: success, blocksize %d\n"), blocksize
));
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.
168 static void fsw_ext2_volume_free(struct fsw_ext2_volume
*vol
)
173 fsw_free(vol
->inotab_bno
);
177 * Get in-depth information on a volume.
180 static fsw_status_t
fsw_ext2_volume_stat(struct fsw_ext2_volume
*vol
, struct fsw_volume_stat
*sb
)
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
;
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.
195 static fsw_status_t
fsw_ext2_dnode_fill(struct fsw_ext2_volume
*vol
, struct fsw_ext2_dnode
*dno
)
198 fsw_u32 groupno
, ino_in_group
, ino_bno
, ino_index
;
204 FSW_MSG_DEBUG((FSW_MSGSTR("fsw_ext2_dnode_fill: inode %d\n"), dno
->g
.dnode_id
));
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
);
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
);
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
;
232 dno
->g
.type
= FSW_DNODE_TYPE_SPECIAL
;
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.
243 static void fsw_ext2_dnode_free(struct fsw_ext2_volume
*vol
, struct fsw_ext2_dnode
*dno
)
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.
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
)
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
);
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.
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.
281 static fsw_status_t
fsw_ext2_get_extent(struct fsw_ext2_volume
*vol
, struct fsw_ext2_dnode
*dno
,
282 struct fsw_extent
*extent
)
285 fsw_u32 bno
, release_bno
, buf_bcnt
, file_bcnt
;
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.
293 extent
->type
= FSW_EXTENT_TYPE_PHYSBLOCK
;
294 extent
->log_count
= 1;
295 bno
= extent
->log_start
;
297 // try direct block pointers in the inode
298 if (bno
< EXT2_NDIR_BLOCKS
) {
302 bno
-= EXT2_NDIR_BLOCKS
;
304 // try indirect block
305 if (bno
< vol
->ind_bcnt
) {
306 path
[0] = EXT2_IND_BLOCK
;
310 bno
-= vol
->ind_bcnt
;
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
;
319 bno
-= vol
->dind_bcnt
;
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
;
331 // follow the indirection path
332 buffer
= dno
->raw
->i_block
;
333 buf_bcnt
= EXT2_NDIR_BLOCKS
;
336 bno
= buffer
[path
[i
]];
338 extent
->type
= FSW_EXTENT_TYPE_SPARSE
;
340 fsw_block_release(vol
, release_bno
, buffer
);
347 fsw_block_release(vol
, release_bno
, buffer
);
348 status
= fsw_block_get(vol
, bno
, 1, (void **)&buffer
);
352 buf_bcnt
= vol
->ind_bcnt
;
354 extent
->phys_start
= bno
;
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)
367 fsw_block_release(vol
, release_bno
, buffer
);
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.
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
)
382 struct fsw_shandle shand
;
384 struct ext2_dir_entry entry
;
385 struct fsw_string entry_name
;
387 // Preconditions: The caller has checked that dno is a directory node.
389 entry_name
.type
= FSW_STRING_TYPE_ISO88591
;
391 // setup handle to read the directory
392 status
= fsw_shandle_open(dno
, &shand
);
396 // scan the directory for the file
398 while (child_ino
== 0) {
400 status
= fsw_ext2_read_dentry(&shand
, &entry
);
403 if (entry
.inode
== 0) {
404 // end of directory reached
405 status
= FSW_NOT_FOUND
;
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
;
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
);
422 fsw_shandle_close(&shand
);
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.
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
)
438 struct ext2_dir_entry entry
;
439 struct fsw_string entry_name
;
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
447 status
= fsw_ext2_read_dentry(shand
, &entry
);
450 if (entry
.inode
== 0) // end of directory
451 return FSW_NOT_FOUND
;
454 if ((entry
.name_len
== 1 && entry
.name
[0] == '.') ||
455 (entry
.name_len
== 2 && entry
.name
[0] == '.' && entry
.name
[1] == '.'))
461 entry_name
.type
= FSW_STRING_TYPE_ISO88591
;
462 entry_name
.len
= entry_name
.size
= entry
.name_len
;
463 entry_name
.data
= entry
.name
;
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
);
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.
477 static fsw_status_t
fsw_ext2_read_dentry(struct fsw_shandle
*shand
, struct ext2_dir_entry
*entry
)
483 // read dir_entry header (fixed length)
485 status
= fsw_shandle_read(shand
, &buffer_size
, entry
);
489 if (buffer_size
< 8 || entry
->rec_len
== 0) {
490 // end of directory reached
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
;
503 // valid, but unused entry, skip it
504 shand
->pos
+= entry
->rec_len
- 8;
507 // read file name (variable length)
508 buffer_size
= entry
->name_len
;
509 status
= fsw_shandle_read(shand
, &buffer_size
, entry
->name
);
512 if (buffer_size
< entry
->name_len
)
513 return FSW_VOLUME_CORRUPTED
;
515 // skip any remaining padding
516 shand
->pos
+= entry
->rec_len
- (8 + entry
->name_len
);
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.
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.
532 static fsw_status_t
fsw_ext2_readlink(struct fsw_ext2_volume
*vol
, struct fsw_ext2_dnode
*dno
,
533 struct fsw_string
*link_target
)
539 if (dno
->g
.size
> FSW_PATH_MAX
)
540 return FSW_VOLUME_CORRUPTED
;
542 ea_blocks
= dno
->raw
->i_file_acl
? (vol
->g
.log_blocksize
>> 9) : 0;
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
);
551 // "slow" symlink, path is stored in normal inode data
552 status
= fsw_dnode_readlink_data(dno
, link_target
);