2 * fs_ext2.c - an implementation for the Ext2/Ext3 filesystem
4 * Copyright (C) 2001, 2002 Ethan Benson
6 * Copyright (C) 1999 Benjamin Herrenschmidt
8 * Adapted from quik/silo
10 * Copyright (C) 1996 Maurizio Plaza
13 * This program is free software; you can redistribute it and/or modify
14 * it under the terms of the GNU General Public License as published by
15 * the Free Software Foundation; either version 2 of the License, or
16 * (at your option) any later version.
18 * This program is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU General Public License for more details.
23 * You should have received a copy of the GNU General Public License
24 * along with this program; if not, write to the Free Software
25 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
34 #include "partition.h"
41 #define MAX_READ_RANGE 256
45 #include "linux/ext2_fs.h"
46 #include "ext2fs/ext2fs.h"
48 static int ext2_open( struct boot_file_t* file,
49 struct partition_t* part,
50 struct boot_fspec_t* fspec);
51 static int ext2_read( struct boot_file_t* file,
54 static int ext2_seek( struct boot_file_t* file,
56 static int ext2_close( struct boot_file_t* file);
57 static unsigned int ext2_ino_size(struct boot_file_t *file);
59 struct fs_t ext2_filesystem =
69 /* IO manager structure for the ext2 library */
71 static errcode_t linux_open (const char *name, int flags, io_channel * channel);
72 static errcode_t linux_close (io_channel channel);
73 static errcode_t linux_set_blksize (io_channel channel, int blksize);
74 static errcode_t linux_read_blk (io_channel channel, unsigned long block, int count, void *data);
75 static errcode_t linux_write_blk (io_channel channel, unsigned long block, int count, const void *data);
76 static errcode_t linux_flush (io_channel channel);
78 static struct struct_io_manager struct_linux_manager =
80 EXT2_ET_MAGIC_IO_MANAGER,
90 static io_manager linux_io_manager = &struct_linux_manager;
92 /* Currently, we have a mess between what is in the file structure
93 * and what is stored globally here. I'll clean this up later
95 static int opened = 0; /* We can't open twice ! */
96 static unsigned int bs; /* Blocksize */
97 static unsigned long long doff; /* Byte offset where partition starts */
98 static unsigned long long dend; /* Byte offset where partition ends */
99 static ino_t root,cwd;
100 static ext2_filsys fs = 0;
101 static struct boot_file_t* cur_file;
102 static char *block_buffer = NULL;
105 static unsigned long read_range_start;
106 static unsigned long read_range_count;
107 static unsigned long read_last_logical;
108 static unsigned long read_total;
109 static unsigned long read_max;
110 static struct boot_file_t* read_cur_file;
111 static errcode_t read_result;
112 static char* read_buffer;
114 static int read_dump_range(void);
115 static int read_iterator(ext2_filsys fs, blk_t *blocknr, int lg_block, void *private);
116 #else /* FAST_VERSION */
117 static struct ext2_inode cur_inode;
118 #endif /* FAST_VERSION */
120 void com_err (const char *a, long i, const char *fmt,...)
122 prom_printf ((char *) fmt);
126 ext2_open( struct boot_file_t* file,
127 struct partition_t* part,
128 struct boot_fspec_t* fspec)
131 int error = FILE_ERR_NOTFOUND;
132 static char buffer[1024];
134 char *dev_name = fspec->dev;
135 char *file_name = fspec->file;
141 DEBUG_LEAVE(FILE_ERR_FSBUSY);
142 return FILE_ERR_FSBUSY;
144 if (file->device_kind != FILE_DEVICE_BLOCK) {
145 DEBUG_LEAVE(FILE_ERR_BADDEV);
146 return FILE_ERR_BADDEV;
151 /* We don't care too much about the device block size since we run
152 * thru the deblocker. We may have to change that is we plan to be
153 * compatible with older versions of OF
158 * On the other hand, we do care about the actual size of the
159 * partition, reads or seeks past the end may cause undefined
160 * behavior on some devices. A netapp that tries to seek and
161 * read past the end of the lun takes ~30 secs to recover per
166 doff = (unsigned long long)(part->part_start) * part->blocksize;
167 dend = doff + (unsigned long long)part->part_size * part->blocksize;
172 DEBUG_F("partition offset: %Lx, end: %Lx\n", doff, dend);
174 /* Open the OF device for the entire disk */
175 strncpy(buffer, dev_name, 1020);
176 if (_machine != _MACH_bplan)
177 strcat(buffer, ":0");
179 DEBUG_F("<%s>\n", buffer);
181 file->of_device = prom_open(buffer);
183 DEBUG_F("file->of_device = %p\n", file->of_device);
185 if (file->of_device == PROM_INVALID_HANDLE) {
187 DEBUG_F("Can't open device %p\n", file->of_device);
188 DEBUG_LEAVE(FILE_IOERR);
193 /* Open the ext2 filesystem */
194 result = ext2fs_open (buffer, EXT2_FLAG_RW, 0, 0, linux_io_manager, &fs);
197 if(result == EXT2_ET_BAD_MAGIC)
199 DEBUG_F( "ext2fs_open returned bad magic loading file %p\n",
204 DEBUG_F( "ext2fs_open error #%d while loading file %s\n",
207 error = FILE_ERR_BAD_FSYS;
211 /* Allocate the block buffer */
212 block_buffer = malloc(fs->blocksize * 2);
215 DEBUG_F("ext2fs: can't alloc block buffer (%d bytes)\n", fs->blocksize * 2);
220 /* Lookup file by pathname */
221 root = cwd = EXT2_ROOT_INO;
222 result = ext2fs_namei_follow(fs, root, cwd, file_name, &file->inode);
225 DEBUG_F("ext2fs_namei error #%d while loading file %s\n", result, file_name);
226 if (result == EXT2_ET_SYMLINK_LOOP)
227 error = FILE_ERR_SYMLINK_LOOP;
228 else if (result == EXT2_ET_FILE_NOT_FOUND)
229 error = FILE_ERR_NOTFOUND;
236 result = ext2fs_follow_link(fs, root, cwd, file->inode, &file->inode);
239 DEBUG_F("ext2fs_follow_link error #%d while loading file %s\n", result, file_name);
240 error = FILE_ERR_NOTFOUND;
246 result = ext2fs_read_inode(fs, file->inode, &cur_inode);
249 DEBUG_F("ext2fs_read_inode error #%d while loading file %s\n", result, file_name);
250 if (result == EXT2_ET_FILE_TOO_BIG)
251 error = FILE_ERR_LENGTH;
252 else if (result == EXT2_ET_LLSEEK_FAILED)
253 error = FILE_CANT_SEEK;
254 else if (result == EXT2_ET_FILE_NOT_FOUND)
255 error = FILE_ERR_NOTFOUND;
260 #endif /* FAST_VERSION */
270 prom_close(file->of_device);
276 DEBUG_LEAVE_F(error);
280 DEBUG_LEAVE(FILE_ERR_OK);
287 read_dump_range(void)
289 int count = read_range_count;
293 DEBUG_F(" dumping range: start: 0x%x count: 0x%x\n",
294 read_range_count, read_range_start);
296 /* Check if we need to handle a special case for the last block */
297 if ((count * bs) > read_max)
301 read_result = io_channel_read_blk(fs->io, read_range_start, count, read_buffer);
307 read_cur_file->pos += size;
308 read_range_count -= count;
309 read_range_start += count;
310 read_last_logical += count;
312 /* Handle remaining block */
313 if (read_max && read_range_count) {
314 read_result = io_channel_read_blk(fs->io, read_range_start, 1, block_buffer);
317 memcpy(read_buffer, block_buffer, read_max);
318 read_cur_file->pos += read_max;
319 read_total += read_max;
322 read_range_count = read_range_start = 0;
324 return (read_max == 0) ? BLOCK_ABORT : 0;
328 read_iterator(ext2_filsys fs, blk_t *blocknr, int lg_block, void *private)
331 DEBUG_F("read_it: p_bloc: 0x%x, l_bloc: 0x%x, f_pos: 0x%x, rng_pos: 0x%x ",
332 *blocknr, lg_block, read_cur_file->pos, read_last_logical);
336 DEBUG_F(" <skip lg>\n");
341 /* If we have not reached the start block yet, we skip */
342 if (lg_block < read_cur_file->pos / bs) {
344 DEBUG_F(" <skip pos>\n");
349 /* If block is contiguous to current range, just extend range,
350 * exit if we pass the remaining bytes count to read
352 if (read_range_start && read_range_count < MAX_READ_RANGE
353 && (*blocknr == read_range_start + read_range_count)
354 && (lg_block == read_last_logical + read_range_count)) {
356 DEBUG_F(" block in range\n");
359 return ((read_range_count * bs) >= read_max) ? BLOCK_ABORT : 0;
362 /* Range doesn't match. Dump existing range */
363 if (read_range_start) {
365 DEBUG_F(" calling dump range \n");
367 if (read_dump_range())
371 /* Here we handle holes in the file */
372 if (lg_block && lg_block != read_last_logical) {
375 DEBUG_F(" hole from lg_bloc 0x%x\n", read_last_logical);
377 if (read_cur_file->pos % bs) {
378 int offset = read_cur_file->pos % bs;
379 int size = bs - offset;
382 memset(read_buffer, 0, size);
386 read_cur_file->pos += size;
391 nzero = (lg_block - read_last_logical) * bs;
393 if (nzero > read_max)
395 memset(read_buffer, 0, nzero);
398 read_buffer += nzero;
399 read_cur_file->pos += nzero;
403 read_last_logical = lg_block;
406 /* If we are not aligned, handle that case */
407 if (read_cur_file->pos % bs) {
408 int offset = read_cur_file->pos % bs;
409 int size = bs - offset;
411 DEBUG_F(" handle unaligned start\n");
413 read_result = io_channel_read_blk(fs->io, *blocknr, 1, block_buffer);
418 memcpy(read_buffer, block_buffer + offset, size);
419 read_cur_file->pos += size;
423 read_last_logical = lg_block + 1;
424 return (read_max == 0) ? BLOCK_ABORT : 0;
427 /* If there is still a physical block to add, then create a new range */
430 DEBUG_F(" new range\n");
432 read_range_start = *blocknr;
433 read_range_count = 1;
434 return (bs >= read_max) ? BLOCK_ABORT : 0;
443 #endif /* FAST_VERSION */
446 ext2_read( struct boot_file_t* file,
457 DEBUG_F("ext_read() from pos 0x%Lx, size: 0x%ux\n", file->pos, size);
460 read_cur_file = file;
461 read_range_start = 0;
462 read_range_count = 0;
463 read_last_logical = file->pos / bs;
466 read_buffer = (unsigned char*)buffer;
469 retval = ext2fs_block_iterate(fs, file->inode, 0, 0, read_iterator, 0);
470 if (retval == BLOCK_ABORT)
471 retval = read_result;
472 if (!retval && read_range_start) {
474 DEBUG_F("on exit: range_start is 0x%x, calling dump...\n",
478 retval = read_result;
481 prom_printf ("ext2: i/o error %ld in read\n", (long) retval);
485 #else /* FAST_VERSION */
487 unsigned int read = 0;
493 DEBUG_F("ext_read() from pos 0x%x, size: 0x%x\n", file->pos, size);
497 blk_t fblock = file->pos / bs;
499 unsigned int blkorig, s, b;
502 status = ext2fs_bmap(fs, file->inode, &cur_inode,
503 block_buffer, 0, fblock, &pblock);
506 DEBUG_F("ext2fs_bmap(fblock:%d) return: %d\n", fblock, status);
509 blkorig = fblock * bs;
510 b = file->pos - blkorig;
511 s = ((bs - b) > size) ? size : (bs - b);
513 unsigned long long pos =
514 ((unsigned long long)pblock) * (unsigned long long)bs;
516 prom_lseek(file->of_device, pos);
517 status = prom_read(file->of_device, block_buffer, bs);
519 prom_printf("ext2: io error in read, ex: %d, got: %d\n",
524 memset(block_buffer, 0, bs);
526 memcpy(buffer, block_buffer + b, s);
533 #endif /* FAST_VERSION */
537 ext2_seek( struct boot_file_t* file,
541 return FILE_CANT_SEEK;
548 ext2_close( struct boot_file_t* file)
561 prom_close(file->of_device);
562 DEBUG_F("ext2_close called\n");
569 static unsigned int ext2_ino_size(struct boot_file_t *file)
571 struct ext2_inode ei;
573 if (ext2fs_read_inode(fs, file->inode, &ei))
579 static errcode_t linux_open (const char *name, int flags, io_channel * channel)
585 return EXT2_ET_BAD_DEVICE_NAME;
586 io = (io_channel) malloc (sizeof (struct struct_io_channel));
588 return EXT2_ET_BAD_DEVICE_NAME;
589 memset (io, 0, sizeof (struct struct_io_channel));
590 io->magic = EXT2_ET_MAGIC_IO_CHANNEL;
591 io->manager = linux_io_manager;
592 io->name = (char *) malloc (strlen (name) + 1);
593 strcpy (io->name, name);
602 static errcode_t linux_close (io_channel channel)
608 static errcode_t linux_set_blksize (io_channel channel, int blksize)
610 DEBUG_F("bs set to 0x%x\n", blksize);
611 channel->block_size = bs = blksize;
614 block_buffer = malloc(bs * 2);
619 static errcode_t linux_read_blk (io_channel channel, unsigned long block, int count, void *data)
622 unsigned long long tempb;
627 tempb = (((unsigned long long) block) *
628 ((unsigned long long)bs)) + (unsigned long long)doff;
630 * Only test tempb exceeding dend if dend is set to allow things
631 * like boot: hd:0,\xxxx
633 if (dend && tempb > dend) {
634 DEBUG_F("\nSeek error on block %lx, tempb=%Lx\n", block, tempb >> 9);
635 return EXT2_ET_LLSEEK_FAILED;
638 size = (count < 0) ? -count : count * bs;
639 prom_lseek(cur_file->of_device, tempb);
640 if (prom_read(cur_file->of_device, data, size) != size) {
641 DEBUG_F("\nRead error on block %ld\n", block);
642 return EXT2_ET_SHORT_READ;
647 static errcode_t linux_write_blk (io_channel channel, unsigned long block, int count, const void *data)
652 static errcode_t linux_flush (io_channel channel)
659 * c-file-style: "k&r"