2 * Copyright 2000-2002 by Hans Reiser, licensing governed by reiserfs/README
4 * GRUB -- GRand Unified Bootloader
5 * Copyright (C) 2000, 2001 Free Software Foundation, Inc.
7 * (C) Copyright 2003 - 2004
8 * Sysgo AG, <www.elinos.com>, Pavel Bartusek <pba@sysgo.com>
11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2 of the License, or
14 * (at your option) any later version.
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
21 * You should have received a copy of the GNU General Public License
22 * along with this program; if not, write to the Free Software
23 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
26 /* An implementation for the ReiserFS filesystem ported from GRUB.
27 * Some parts of this code (mainly the structures and defines) are
28 * from the original reiser fs code, as found in the linux kernel.
33 #include <linux/ctype.h>
34 #include <linux/time.h>
35 #include <asm/byteorder.h>
38 #include "reiserfs_private.h"
42 /* Some parts of this code (mainly the structures and defines) are
43 * from the original reiser fs code, as found in the linux kernel.
46 static char fsys_buf[FSYS_BUFLEN];
47 static reiserfs_error_t errnum = ERR_NONE;
48 static int print_possibilities;
49 static unsigned int filepos, filemax;
52 substring (const char *s1, const char *s2)
56 /* The strings match exactly. */
62 /* S1 is a substring of S2. */
66 /* S1 isn't a substring. */
70 static void sd_print_item (struct item_head * ih, char * item)
75 if (stat_data_v1 (ih)) {
76 struct stat_data_v1 * sd = (struct stat_data_v1 *)item;
77 ttime = sd_v1_mtime(sd);
78 ctime_r(&ttime, filetime);
79 printf ("%-10s %4hd %6d %6d %9d %24.24s",
80 bb_mode_string(sd_v1_mode(sd)), sd_v1_nlink(sd),sd_v1_uid(sd), sd_v1_gid(sd),
81 sd_v1_size(sd), filetime);
83 struct stat_data * sd = (struct stat_data *)item;
84 ttime = sd_v2_mtime(sd);
85 ctime_r(&ttime, filetime);
86 printf ("%-10s %4d %6d %6d %9d %24.24s",
87 bb_mode_string(sd_v2_mode(sd)), sd_v2_nlink(sd),sd_v2_uid(sd),sd_v2_gid(sd),
88 (__u32) sd_v2_size(sd), filetime);
93 journal_read (int block, int len, char *buffer)
95 return reiserfs_devread ((INFO->journal_block + block) << INFO->blocksize_shift,
99 /* Read a block from ReiserFS file system, taking the journal into
100 * account. If the block nr is in the journal, the block from the
104 block_read (unsigned int blockNr, int start, int len, char *buffer)
106 int transactions = INFO->journal_transactions;
107 int desc_block = INFO->journal_first_desc;
108 int journal_mask = INFO->journal_block_count - 1;
109 int translatedNr = blockNr;
110 __u32 *journal_table = JOURNAL_START;
111 while (transactions-- > 0)
115 if (__le32_to_cpu(*journal_table) != 0xffffffff)
117 /* Search for the blockNr in cached journal */
118 j_len = __le32_to_cpu(*journal_table++);
121 if (__le32_to_cpu(*journal_table++) == blockNr)
123 journal_table += j_len - i;
130 /* This is the end of cached journal marker. The remaining
131 * transactions are still on disk.
133 struct reiserfs_journal_desc desc;
134 struct reiserfs_journal_commit commit;
136 if (! journal_read (desc_block, sizeof (desc), (char *) &desc))
139 j_len = __le32_to_cpu(desc.j_len);
140 while (i < j_len && i < JOURNAL_TRANS_HALF)
141 if (__le32_to_cpu(desc.j_realblock[i++]) == blockNr)
144 if (j_len >= JOURNAL_TRANS_HALF)
146 int commit_block = (desc_block + 1 + j_len) & journal_mask;
147 if (! journal_read (commit_block,
148 sizeof (commit), (char *) &commit))
151 if (__le32_to_cpu(commit.j_realblock[i++ - JOURNAL_TRANS_HALF]) == blockNr)
158 translatedNr = INFO->journal_block + ((desc_block + i) & journal_mask);
160 printf ("block_read: block %d is mapped to journal block %d.\n",
161 blockNr, translatedNr - INFO->journal_block);
163 /* We must continue the search, as this block may be overwritten
164 * in later transactions.
167 desc_block = (desc_block + 2 + j_len) & journal_mask;
169 return reiserfs_devread (translatedNr << INFO->blocksize_shift, start, len, buffer);
172 /* Init the journal data structure. We try to cache as much as
173 * possible in the JOURNAL_START-JOURNAL_END space, but if it is full
174 * we can still read the rest from the disk on demand.
176 * The first number of valid transactions and the descriptor block of the
177 * first valid transaction are held in INFO. The transactions are all
178 * adjacent, but we must take care of the journal wrap around.
183 unsigned int block_count = INFO->journal_block_count;
184 unsigned int desc_block;
185 unsigned int commit_block;
186 unsigned int next_trans_id;
187 struct reiserfs_journal_header header;
188 struct reiserfs_journal_desc desc;
189 struct reiserfs_journal_commit commit;
190 __u32 *journal_table = JOURNAL_START;
192 journal_read (block_count, sizeof (header), (char *) &header);
193 desc_block = __le32_to_cpu(header.j_first_unflushed_offset);
194 if (desc_block >= block_count)
197 INFO->journal_first_desc = desc_block;
198 next_trans_id = __le32_to_cpu(header.j_last_flush_trans_id) + 1;
201 printf ("journal_init: last flushed %d\n",
202 __le32_to_cpu(header.j_last_flush_trans_id));
207 journal_read (desc_block, sizeof (desc), (char *) &desc);
208 if (substring (JOURNAL_DESC_MAGIC, desc.j_magic) > 0
209 || __le32_to_cpu(desc.j_trans_id) != next_trans_id
210 || __le32_to_cpu(desc.j_mount_id) != __le32_to_cpu(header.j_mount_id))
211 /* no more valid transactions */
214 commit_block = (desc_block + __le32_to_cpu(desc.j_len) + 1) & (block_count - 1);
215 journal_read (commit_block, sizeof (commit), (char *) &commit);
216 if (__le32_to_cpu(desc.j_trans_id) != commit.j_trans_id
217 || __le32_to_cpu(desc.j_len) != __le32_to_cpu(commit.j_len))
218 /* no more valid transactions */
222 printf ("Found valid transaction %d/%d at %d.\n",
223 __le32_to_cpu(desc.j_trans_id), __le32_to_cpu(desc.j_mount_id), desc_block);
227 if (journal_table < JOURNAL_END)
229 if ((journal_table + 1 + __le32_to_cpu(desc.j_len)) >= JOURNAL_END)
231 /* The table is almost full; mark the end of the cached
233 *journal_table = __cpu_to_le32(0xffffffff);
234 journal_table = JOURNAL_END;
239 /* Cache the length and the realblock numbers in the table.
240 * The block number of descriptor can easily be computed.
241 * and need not to be stored here.
244 /* both are in the little endian format */
245 *journal_table++ = desc.j_len;
246 for (i = 0; i < __le32_to_cpu(desc.j_len) && i < JOURNAL_TRANS_HALF; i++)
248 /* both are in the little endian format */
249 *journal_table++ = desc.j_realblock[i];
251 printf ("block %d is in journal %d.\n",
252 __le32_to_cpu(desc.j_realblock[i]), desc_block);
255 for ( ; i < __le32_to_cpu(desc.j_len); i++)
257 /* both are in the little endian format */
258 *journal_table++ = commit.j_realblock[i-JOURNAL_TRANS_HALF];
260 printf ("block %d is in journal %d.\n",
261 __le32_to_cpu(commit.j_realblock[i-JOURNAL_TRANS_HALF]),
267 desc_block = (commit_block + 1) & (block_count - 1);
270 printf ("Transaction %d/%d at %d isn't valid.\n",
271 __le32_to_cpu(desc.j_trans_id), __le32_to_cpu(desc.j_mount_id), desc_block);
274 INFO->journal_transactions
275 = next_trans_id - __le32_to_cpu(header.j_last_flush_trans_id) - 1;
279 /* check filesystem types and read superblock into memory buffer */
281 reiserfs_mount (unsigned part_length)
283 struct reiserfs_super_block super;
284 int superblock = REISERFS_DISK_OFFSET_IN_BYTES >> SECTOR_BITS;
286 if (part_length < superblock + (sizeof (super) >> SECTOR_BITS)
287 || ! reiserfs_devread (superblock, 0, sizeof (struct reiserfs_super_block),
289 || (substring (REISER3FS_SUPER_MAGIC_STRING, super.s_magic) > 0
290 && substring (REISER2FS_SUPER_MAGIC_STRING, super.s_magic) > 0
291 && substring (REISERFS_SUPER_MAGIC_STRING, super.s_magic) > 0)
292 || (/* check that this is not a copy inside the journal log */
293 sb_journal_block(&super) * sb_blocksize(&super)
294 <= REISERFS_DISK_OFFSET_IN_BYTES))
296 /* Try old super block position */
297 superblock = REISERFS_OLD_DISK_OFFSET_IN_BYTES >> SECTOR_BITS;
298 if (part_length < superblock + (sizeof (super) >> SECTOR_BITS)
299 || ! reiserfs_devread (superblock, 0, sizeof (struct reiserfs_super_block),
303 if (substring (REISER2FS_SUPER_MAGIC_STRING, super.s_magic) > 0
304 && substring (REISERFS_SUPER_MAGIC_STRING, super.s_magic) > 0)
306 /* pre journaling super block ? */
307 if (substring (REISERFS_SUPER_MAGIC_STRING,
308 (char*) ((int) &super + 20)) > 0)
311 set_sb_blocksize(&super, REISERFS_OLD_BLOCKSIZE);
312 set_sb_journal_block(&super, 0);
313 set_sb_version(&super, 0);
317 /* check the version number. */
318 if (sb_version(&super) > REISERFS_MAX_SUPPORTED_VERSION)
321 INFO->version = sb_version(&super);
322 INFO->blocksize = sb_blocksize(&super);
323 INFO->fullblocksize_shift = log2 (sb_blocksize(&super));
324 INFO->blocksize_shift = INFO->fullblocksize_shift - SECTOR_BITS;
326 (FSYSREISER_CACHE_SIZE >> INFO->fullblocksize_shift) - 1;
329 printf ("reiserfs_mount: version=%d, blocksize=%d\n",
330 INFO->version, INFO->blocksize);
331 #endif /* REISERDEBUG */
333 /* Clear node cache. */
334 memset (INFO->blocks, 0, sizeof (INFO->blocks));
336 if (sb_blocksize(&super) < FSYSREISER_MIN_BLOCKSIZE
337 || sb_blocksize(&super) > FSYSREISER_MAX_BLOCKSIZE
338 || (SECTOR_SIZE << INFO->blocksize_shift) != sb_blocksize(&super))
341 /* Initialize journal code. If something fails we end with zero
342 * journal_transactions, so we don't access the journal at all.
344 INFO->journal_transactions = 0;
345 if (sb_journal_block(&super) != 0 && super.s_journal_dev == 0)
347 INFO->journal_block = sb_journal_block(&super);
348 INFO->journal_block_count = sb_journal_size(&super);
349 if (is_power_of_two (INFO->journal_block_count))
352 /* Read in super block again, maybe it is in the journal */
353 block_read (superblock >> INFO->blocksize_shift,
354 0, sizeof (struct reiserfs_super_block), (char *) &super);
357 if (! block_read (sb_root_block(&super), 0, INFO->blocksize, (char*) ROOT))
360 INFO->tree_depth = __le16_to_cpu(BLOCKHEAD (ROOT)->blk_level);
363 printf ("root read_in: block=%d, depth=%d\n",
364 sb_root_block(&super), INFO->tree_depth);
365 #endif /* REISERDEBUG */
367 if (INFO->tree_depth >= MAX_HEIGHT)
369 if (INFO->tree_depth == DISK_LEAF_NODE_LEVEL)
371 /* There is only one node in the whole filesystem,
372 * which is simultanously leaf and root */
373 memcpy (LEAF, ROOT, INFO->blocksize);
378 /***************** TREE ACCESSING METHODS *****************************/
380 /* I assume you are familiar with the ReiserFS tree, if not go to
381 * http://www.namesys.com/content_table.html
383 * My tree node cache is organized as following
385 * 1 LEAF node (if the ROOT is also a LEAF it is copied here
386 * 2-n other nodes on current path from bottom to top.
387 * if there is not enough space in the cache, the top most are
390 * I have only two methods to find a key in the tree:
391 * search_stat(dir_id, objectid) searches for the stat entry (always
392 * the first entry) of an object.
393 * next_key() gets the next key in tree order.
395 * This means, that I can only sequential reads of files are
396 * efficient, but this really doesn't hurt for grub.
399 /* Read in the node at the current path and depth into the node cache.
400 * You must set INFO->blocks[depth] before.
403 read_tree_node (unsigned int blockNr, int depth)
405 char* cache = CACHE(depth);
406 int num_cached = INFO->cached_slots;
407 if (depth < num_cached)
409 /* This is the cached part of the path. Check if same block is
412 if (blockNr == INFO->blocks[depth])
416 cache = CACHE(num_cached);
419 printf (" next read_in: block=%d (depth=%d)\n",
421 #endif /* REISERDEBUG */
422 if (! block_read (blockNr, 0, INFO->blocksize, cache))
424 /* Make sure it has the right node level */
425 if (__le16_to_cpu(BLOCKHEAD (cache)->blk_level) != depth)
427 errnum = ERR_FSYS_CORRUPT;
431 INFO->blocks[depth] = blockNr;
435 /* Get the next key, i.e. the key following the last retrieved key in
436 * tree order. INFO->current_ih and
437 * INFO->current_info are adapted accordingly. */
442 struct item_head *ih = INFO->current_ih + 1;
446 printf ("next_key:\n old ih: key %d:%d:%d:%d version:%d\n",
447 __le32_to_cpu(INFO->current_ih->ih_key.k_dir_id),
448 __le32_to_cpu(INFO->current_ih->ih_key.k_objectid),
449 __le32_to_cpu(INFO->current_ih->ih_key.u.v1.k_offset),
450 __le32_to_cpu(INFO->current_ih->ih_key.u.v1.k_uniqueness),
451 __le16_to_cpu(INFO->current_ih->ih_version));
452 #endif /* REISERDEBUG */
454 if (ih == &ITEMHEAD[__le16_to_cpu(BLOCKHEAD (LEAF)->blk_nr_item)])
456 depth = DISK_LEAF_NODE_LEVEL;
457 /* The last item, was the last in the leaf node.
458 * Read in the next block
462 if (depth == INFO->tree_depth)
464 /* There are no more keys at all.
465 * Return a dummy item with MAX_KEY */
466 ih = (struct item_head *) &BLOCKHEAD (LEAF)->blk_right_delim_key;
471 printf (" depth=%d, i=%d\n", depth, INFO->next_key_nr[depth]);
472 #endif /* REISERDEBUG */
474 while (INFO->next_key_nr[depth] == 0);
476 if (depth == INFO->tree_depth)
478 else if (depth <= INFO->cached_slots)
479 cache = CACHE (depth);
482 cache = read_tree_node (INFO->blocks[depth], depth);
489 int nr_item = __le16_to_cpu(BLOCKHEAD (cache)->blk_nr_item);
490 int key_nr = INFO->next_key_nr[depth]++;
492 printf (" depth=%d, i=%d/%d\n", depth, key_nr, nr_item);
493 #endif /* REISERDEBUG */
494 if (key_nr == nr_item)
495 /* This is the last item in this block, set the next_key_nr to 0 */
496 INFO->next_key_nr[depth] = 0;
498 cache = read_tree_node (dc_block_number(&(DC (cache)[key_nr])), --depth);
502 while (depth > DISK_LEAF_NODE_LEVEL);
507 INFO->current_ih = ih;
508 INFO->current_item = &LEAF[__le16_to_cpu(ih->ih_item_location)];
510 printf (" new ih: key %d:%d:%d:%d version:%d\n",
511 __le32_to_cpu(INFO->current_ih->ih_key.k_dir_id),
512 __le32_to_cpu(INFO->current_ih->ih_key.k_objectid),
513 __le32_to_cpu(INFO->current_ih->ih_key.u.v1.k_offset),
514 __le32_to_cpu(INFO->current_ih->ih_key.u.v1.k_uniqueness),
515 __le16_to_cpu(INFO->current_ih->ih_version));
516 #endif /* REISERDEBUG */
520 /* preconditions: reiserfs_mount already executed, therefore
521 * INFO block is valid
522 * returns: 0 if error (errnum is set),
523 * nonzero iff we were able to find the key successfully.
524 * postconditions: on a nonzero return, the current_ih and
525 * current_item fields describe the key that equals the
526 * searched key. INFO->next_key contains the next key after
528 * side effects: messes around with the cache.
531 search_stat (__u32 dir_id, __u32 objectid)
537 struct item_head *ih;
539 printf ("search_stat:\n key %d:%d:0:0\n", dir_id, objectid);
540 #endif /* REISERDEBUG */
542 depth = INFO->tree_depth;
545 while (depth > DISK_LEAF_NODE_LEVEL)
548 nr_item = __le16_to_cpu(BLOCKHEAD (cache)->blk_nr_item);
552 for (i = 0; i < nr_item; i++)
554 if (__le32_to_cpu(key->k_dir_id) > dir_id
555 || (__le32_to_cpu(key->k_dir_id) == dir_id
556 && (__le32_to_cpu(key->k_objectid) > objectid
557 || (__le32_to_cpu(key->k_objectid) == objectid
558 && (__le32_to_cpu(key->u.v1.k_offset)
559 | __le32_to_cpu(key->u.v1.k_uniqueness)) > 0))))
565 printf (" depth=%d, i=%d/%d\n", depth, i, nr_item);
566 #endif /* REISERDEBUG */
567 INFO->next_key_nr[depth] = (i == nr_item) ? 0 : i+1;
568 cache = read_tree_node (dc_block_number(&(DC (cache)[i])), --depth);
574 nr_item = __le16_to_cpu(BLOCKHEAD (LEAF)->blk_nr_item);
576 for (i = 0; i < nr_item; i++)
578 if (__le32_to_cpu(ih->ih_key.k_dir_id) == dir_id
579 && __le32_to_cpu(ih->ih_key.k_objectid) == objectid
580 && __le32_to_cpu(ih->ih_key.u.v1.k_offset) == 0
581 && __le32_to_cpu(ih->ih_key.u.v1.k_uniqueness) == 0)
584 printf (" depth=%d, i=%d/%d\n", depth, i, nr_item);
585 #endif /* REISERDEBUG */
586 INFO->current_ih = ih;
587 INFO->current_item = &LEAF[__le16_to_cpu(ih->ih_item_location)];
592 errnum = ERR_FSYS_CORRUPT;
597 reiserfs_read (char *buf, unsigned len)
599 unsigned int blocksize;
601 unsigned int to_read;
602 char *prev_buf = buf;
605 printf ("reiserfs_read: filepos=%d len=%d, offset=%Lx\n",
606 filepos, len, (__u64) IH_KEY_OFFSET (INFO->current_ih) - 1);
607 #endif /* REISERDEBUG */
609 if (__le32_to_cpu(INFO->current_ih->ih_key.k_objectid) != INFO->fileinfo.k_objectid
610 || IH_KEY_OFFSET (INFO->current_ih) > filepos + 1)
612 search_stat (INFO->fileinfo.k_dir_id, INFO->fileinfo.k_objectid);
618 if (__le32_to_cpu(INFO->current_ih->ih_key.k_objectid) != INFO->fileinfo.k_objectid) {
622 offset = filepos - IH_KEY_OFFSET (INFO->current_ih) + 1;
623 blocksize = __le16_to_cpu(INFO->current_ih->ih_item_len);
626 printf (" loop: filepos=%d len=%d, offset=%d blocksize=%d\n",
627 filepos, len, offset, blocksize);
628 #endif /* REISERDEBUG */
630 if (IH_KEY_ISTYPE(INFO->current_ih, TYPE_DIRECT)
631 && offset < blocksize)
634 printf ("direct_read: offset=%d, blocksize=%d\n",
636 #endif /* REISERDEBUG */
637 to_read = blocksize - offset;
641 memcpy (buf, INFO->current_item + offset, to_read);
644 else if (IH_KEY_ISTYPE(INFO->current_ih, TYPE_INDIRECT))
646 blocksize = (blocksize >> 2) << INFO->fullblocksize_shift;
648 printf ("indirect_read: offset=%d, blocksize=%d\n",
650 #endif /* REISERDEBUG */
652 while (offset < blocksize)
654 __u32 blocknr = __le32_to_cpu(((__u32 *) INFO->current_item)
655 [offset >> INFO->fullblocksize_shift]);
656 int blk_offset = offset & (INFO->blocksize-1);
657 to_read = INFO->blocksize - blk_offset;
661 /* Journal is only for meta data. Data blocks can be read
662 * directly without using block_read
664 reiserfs_devread (blocknr << INFO->blocksize_shift,
665 blk_offset, to_read, buf);
679 return errnum ? 0 : buf - prev_buf;
683 /* preconditions: reiserfs_mount already executed, therefore
684 * INFO block is valid
685 * returns: 0 if error, nonzero iff we were able to find the file successfully
686 * postconditions: on a nonzero return, INFO->fileinfo contains the info
687 * of the file we were trying to look up, filepos is 0 and filemax is
688 * the size of the file.
691 reiserfs_dir (char *dirname)
693 struct reiserfs_de_head *de_head;
695 __u32 dir_id, objectid, parent_dir_id = 0, parent_objectid = 0;
697 int do_possibilities = 0;
698 #endif /* ! STAGE1_5 */
699 char linkbuf[PATH_MAX]; /* buffer for following symbolic links */
703 dir_id = REISERFS_ROOT_PARENT_OBJECTID;
704 objectid = REISERFS_ROOT_OBJECTID;
709 printf ("dirname=%s\n", dirname);
710 #endif /* REISERDEBUG */
712 /* Search for the stat info first. */
713 if (! search_stat (dir_id, objectid))
717 printf ("sd_mode=%x sd_size=%d\n",
718 stat_data_v1(INFO->current_ih) ? sd_v1_mode((struct stat_data_v1 *) INFO->current_item) :
719 sd_v2_mode((struct stat_data *) (INFO->current_item)),
720 stat_data_v1(INFO->current_ih) ? sd_v1_size((struct stat_data_v1 *) INFO->current_item) :
721 sd_v2_size((struct stat_data *) INFO->current_item)
724 #endif /* REISERDEBUG */
725 mode = stat_data_v1(INFO->current_ih) ?
726 sd_v1_mode((struct stat_data_v1 *) INFO->current_item) :
727 sd_v2_mode((struct stat_data *) INFO->current_item);
729 /* If we've got a symbolic link, then chase it. */
733 if (++link_count > MAX_LINK_COUNT)
735 errnum = ERR_SYMLINK_LOOP;
739 /* Get the symlink size. */
740 filemax = stat_data_v1(INFO->current_ih) ?
741 sd_v1_size((struct stat_data_v1 *) INFO->current_item) :
742 sd_v2_size((struct stat_data *) INFO->current_item);
744 /* Find out how long our remaining name is. */
746 while (dirname[len] && !isspace (dirname[len]))
749 if (filemax + len > sizeof (linkbuf) - 1)
751 errnum = ERR_FILELENGTH;
755 /* Copy the remaining name to the end of the symlink data.
756 Note that DIRNAME and LINKBUF may overlap! */
757 memmove (linkbuf + filemax, dirname, len+1);
759 INFO->fileinfo.k_dir_id = dir_id;
760 INFO->fileinfo.k_objectid = objectid;
763 || reiserfs_read (linkbuf, filemax) != filemax)
766 errnum = ERR_FSYS_CORRUPT;
771 printf ("symlink=%s\n", linkbuf);
772 #endif /* REISERDEBUG */
777 /* It's an absolute link, so look it up in root. */
778 dir_id = REISERFS_ROOT_PARENT_OBJECTID;
779 objectid = REISERFS_ROOT_OBJECTID;
783 /* Relative, so look it up in our parent directory. */
784 dir_id = parent_dir_id;
785 objectid = parent_objectid;
788 /* Now lookup the new name. */
792 /* if we have a real file (and we're not just printing possibilities),
793 then this is where we want to exit */
795 if (! *dirname || isspace (*dirname))
797 if (! S_ISREG (mode))
799 errnum = ERR_BAD_FILETYPE;
804 filemax = stat_data_v1(INFO->current_ih) ?
805 sd_v1_size((struct stat_data_v1 *) INFO->current_item) :
806 sd_v2_size((struct stat_data *) INFO->current_item);
808 /* If this is a new stat data and size is > 4GB set filemax to
811 if (__le16_to_cpu(INFO->current_ih->ih_version) == ITEM_VERSION_2
812 && sd_size_hi((struct stat_data *) INFO->current_item) > 0)
813 filemax = 0xffffffff;
815 INFO->fileinfo.k_dir_id = dir_id;
816 INFO->fileinfo.k_objectid = objectid;
820 /* continue with the file/directory name interpretation */
821 while (*dirname == '/')
823 if (! S_ISDIR (mode))
825 errnum = ERR_BAD_FILETYPE;
828 for (rest = dirname; (ch = *rest) && ! isspace (ch) && ch != '/'; rest++);
832 if (print_possibilities && ch != '/')
833 do_possibilities = 1;
834 # endif /* ! STAGE1_5 */
844 printf ("ih: key %d:%d:%d:%d version:%d\n",
845 __le32_to_cpu(INFO->current_ih->ih_key.k_dir_id),
846 __le32_to_cpu(INFO->current_ih->ih_key.k_objectid),
847 __le32_to_cpu(INFO->current_ih->ih_key.u.v1.k_offset),
848 __le32_to_cpu(INFO->current_ih->ih_key.u.v1.k_uniqueness),
849 __le16_to_cpu(INFO->current_ih->ih_version));
850 #endif /* REISERDEBUG */
852 if (__le32_to_cpu(INFO->current_ih->ih_key.k_objectid) != objectid)
855 name_end = INFO->current_item + __le16_to_cpu(INFO->current_ih->ih_item_len);
856 de_head = (struct reiserfs_de_head *) INFO->current_item;
857 num_entries = __le16_to_cpu(INFO->current_ih->u.ih_entry_count);
858 while (num_entries > 0)
860 char *filename = INFO->current_item + deh_location(de_head);
861 char tmp = *name_end;
862 if ((deh_state(de_head) & DEH_Visible))
865 /* Directory names in ReiserFS are not null
866 * terminated. We write a temporary 0 behind it.
867 * NOTE: that this may overwrite the first block in
868 * the tree cache. That doesn't hurt as long as we
869 * don't call next_key () in between.
872 cmp = substring (dirname, filename);
875 if (do_possibilities)
880 struct fsys_reiser_info info_save;
882 if (print_possibilities > 0)
883 print_possibilities = -print_possibilities;
885 strcpy(fn, filename);
888 /* If NAME is "." or "..", do not count it. */
889 if (strcmp (fn, ".") != 0 && strcmp (fn, "..") != 0) {
890 memcpy(&info_save, INFO, sizeof(struct fsys_reiser_info));
891 search_stat (deh_dir_id(de_head), deh_objectid(de_head));
892 sd_print_item(INFO->current_ih, INFO->current_item);
894 search_stat (dir_id, objectid);
895 memcpy(INFO, &info_save, sizeof(struct fsys_reiser_info));
900 # endif /* ! STAGE1_5 */
904 /* The beginning of this name marks the end of the next name.
913 if (print_possibilities < 0)
915 # endif /* ! STAGE1_5 */
917 errnum = ERR_FILE_NOT_FOUND;
925 parent_dir_id = dir_id;
926 parent_objectid = objectid;
927 dir_id = deh_dir_id(de_head);
928 objectid = deh_objectid(de_head);
933 * U-Boot interface functions
937 * List given directory
939 * RETURN: 0 - OK, else grub_error_t errnum
942 reiserfs_ls (char *dirname)
948 dir_slash = malloc(strlen(dirname) + 1);
949 if (dir_slash == NULL) {
950 return ERR_NUMBER_OVERFLOW;
952 strcpy(dir_slash, dirname);
953 /* add "/" to the directory name */
954 strcat(dir_slash, "/");
956 print_possibilities = 1;
957 res = reiserfs_dir (dir_slash);
959 if (!res || errnum) {
967 * Open file for reading
969 * RETURN: >0 - OK, size of opened file
970 * <0 - ERROR -grub_error_t errnum
973 reiserfs_open (char *filename)
977 print_possibilities = 0;
978 if (!reiserfs_dir (filename) || errnum) {