1 // SPDX-License-Identifier: GPL-2.0+
3 * Copyright 2000-2002 by Hans Reiser, licensing governed by reiserfs/README
5 * GRUB -- GRand Unified Bootloader
6 * Copyright (C) 2000, 2001 Free Software Foundation, Inc.
8 * (C) Copyright 2003 - 2004
9 * Sysgo AG, <www.elinos.com>, Pavel Bartusek <pba@sysgo.com>
13 /* An implementation for the ReiserFS filesystem ported from GRUB.
14 * Some parts of this code (mainly the structures and defines) are
15 * from the original reiser fs code, as found in the linux kernel.
20 #include <linux/ctype.h>
21 #include <linux/time.h>
22 #include <asm/byteorder.h>
25 #include "reiserfs_private.h"
29 /* Some parts of this code (mainly the structures and defines) are
30 * from the original reiser fs code, as found in the linux kernel.
33 static char fsys_buf[FSYS_BUFLEN];
34 static reiserfs_error_t errnum = ERR_NONE;
35 static int print_possibilities;
36 static unsigned int filepos, filemax;
39 substring (const char *s1, const char *s2)
43 /* The strings match exactly. */
49 /* S1 is a substring of S2. */
53 /* S1 isn't a substring. */
57 static void sd_print_item (struct item_head * ih, char * item)
62 if (stat_data_v1 (ih)) {
63 struct stat_data_v1 * sd = (struct stat_data_v1 *)item;
64 ttime = sd_v1_mtime(sd);
65 ctime_r(&ttime, filetime);
66 printf ("%-10s %4hd %6d %6d %9d %24.24s",
67 bb_mode_string(sd_v1_mode(sd)), sd_v1_nlink(sd),sd_v1_uid(sd), sd_v1_gid(sd),
68 sd_v1_size(sd), filetime);
70 struct stat_data * sd = (struct stat_data *)item;
71 ttime = sd_v2_mtime(sd);
72 ctime_r(&ttime, filetime);
73 printf ("%-10s %4d %6d %6d %9d %24.24s",
74 bb_mode_string(sd_v2_mode(sd)), sd_v2_nlink(sd),sd_v2_uid(sd),sd_v2_gid(sd),
75 (__u32) sd_v2_size(sd), filetime);
80 journal_read (int block, int len, char *buffer)
82 return reiserfs_devread ((INFO->journal_block + block) << INFO->blocksize_shift,
86 /* Read a block from ReiserFS file system, taking the journal into
87 * account. If the block nr is in the journal, the block from the
91 block_read (unsigned int blockNr, int start, int len, char *buffer)
93 int transactions = INFO->journal_transactions;
94 int desc_block = INFO->journal_first_desc;
95 int journal_mask = INFO->journal_block_count - 1;
96 int translatedNr = blockNr;
97 __u32 *journal_table = JOURNAL_START;
98 while (transactions-- > 0)
102 if (__le32_to_cpu(*journal_table) != 0xffffffff)
104 /* Search for the blockNr in cached journal */
105 j_len = __le32_to_cpu(*journal_table++);
108 if (__le32_to_cpu(*journal_table++) == blockNr)
110 journal_table += j_len - i;
117 /* This is the end of cached journal marker. The remaining
118 * transactions are still on disk.
120 struct reiserfs_journal_desc desc;
121 struct reiserfs_journal_commit commit;
123 if (! journal_read (desc_block, sizeof (desc), (char *) &desc))
126 j_len = __le32_to_cpu(desc.j_len);
127 while (i < j_len && i < JOURNAL_TRANS_HALF)
128 if (__le32_to_cpu(desc.j_realblock[i++]) == blockNr)
131 if (j_len >= JOURNAL_TRANS_HALF)
133 int commit_block = (desc_block + 1 + j_len) & journal_mask;
134 if (! journal_read (commit_block,
135 sizeof (commit), (char *) &commit))
138 if (__le32_to_cpu(commit.j_realblock[i++ - JOURNAL_TRANS_HALF]) == blockNr)
145 translatedNr = INFO->journal_block + ((desc_block + i) & journal_mask);
147 printf ("block_read: block %d is mapped to journal block %d.\n",
148 blockNr, translatedNr - INFO->journal_block);
150 /* We must continue the search, as this block may be overwritten
151 * in later transactions.
154 desc_block = (desc_block + 2 + j_len) & journal_mask;
156 return reiserfs_devread (translatedNr << INFO->blocksize_shift, start, len, buffer);
159 /* Init the journal data structure. We try to cache as much as
160 * possible in the JOURNAL_START-JOURNAL_END space, but if it is full
161 * we can still read the rest from the disk on demand.
163 * The first number of valid transactions and the descriptor block of the
164 * first valid transaction are held in INFO. The transactions are all
165 * adjacent, but we must take care of the journal wrap around.
170 unsigned int block_count = INFO->journal_block_count;
171 unsigned int desc_block;
172 unsigned int commit_block;
173 unsigned int next_trans_id;
174 struct reiserfs_journal_header header;
175 struct reiserfs_journal_desc desc;
176 struct reiserfs_journal_commit commit;
177 __u32 *journal_table = JOURNAL_START;
179 journal_read (block_count, sizeof (header), (char *) &header);
180 desc_block = __le32_to_cpu(header.j_first_unflushed_offset);
181 if (desc_block >= block_count)
184 INFO->journal_first_desc = desc_block;
185 next_trans_id = __le32_to_cpu(header.j_last_flush_trans_id) + 1;
188 printf ("journal_init: last flushed %d\n",
189 __le32_to_cpu(header.j_last_flush_trans_id));
194 journal_read (desc_block, sizeof (desc), (char *) &desc);
195 if (substring (JOURNAL_DESC_MAGIC, desc.j_magic) > 0
196 || __le32_to_cpu(desc.j_trans_id) != next_trans_id
197 || __le32_to_cpu(desc.j_mount_id) != __le32_to_cpu(header.j_mount_id))
198 /* no more valid transactions */
201 commit_block = (desc_block + __le32_to_cpu(desc.j_len) + 1) & (block_count - 1);
202 journal_read (commit_block, sizeof (commit), (char *) &commit);
203 if (__le32_to_cpu(desc.j_trans_id) != commit.j_trans_id
204 || __le32_to_cpu(desc.j_len) != __le32_to_cpu(commit.j_len))
205 /* no more valid transactions */
209 printf ("Found valid transaction %d/%d at %d.\n",
210 __le32_to_cpu(desc.j_trans_id), __le32_to_cpu(desc.j_mount_id), desc_block);
214 if (journal_table < JOURNAL_END)
216 if ((journal_table + 1 + __le32_to_cpu(desc.j_len)) >= JOURNAL_END)
218 /* The table is almost full; mark the end of the cached
220 *journal_table = __cpu_to_le32(0xffffffff);
221 journal_table = JOURNAL_END;
226 /* Cache the length and the realblock numbers in the table.
227 * The block number of descriptor can easily be computed.
228 * and need not to be stored here.
231 /* both are in the little endian format */
232 *journal_table++ = desc.j_len;
233 for (i = 0; i < __le32_to_cpu(desc.j_len) && i < JOURNAL_TRANS_HALF; i++)
235 /* both are in the little endian format */
236 *journal_table++ = desc.j_realblock[i];
238 printf ("block %d is in journal %d.\n",
239 __le32_to_cpu(desc.j_realblock[i]), desc_block);
242 for ( ; i < __le32_to_cpu(desc.j_len); i++)
244 /* both are in the little endian format */
245 *journal_table++ = commit.j_realblock[i-JOURNAL_TRANS_HALF];
247 printf ("block %d is in journal %d.\n",
248 __le32_to_cpu(commit.j_realblock[i-JOURNAL_TRANS_HALF]),
254 desc_block = (commit_block + 1) & (block_count - 1);
257 printf ("Transaction %d/%d at %d isn't valid.\n",
258 __le32_to_cpu(desc.j_trans_id), __le32_to_cpu(desc.j_mount_id), desc_block);
261 INFO->journal_transactions
262 = next_trans_id - __le32_to_cpu(header.j_last_flush_trans_id) - 1;
266 /* check filesystem types and read superblock into memory buffer */
268 reiserfs_mount (unsigned part_length)
270 struct reiserfs_super_block super;
271 int superblock = REISERFS_DISK_OFFSET_IN_BYTES >> SECTOR_BITS;
274 if (part_length < superblock + (sizeof (super) >> SECTOR_BITS)
275 || ! reiserfs_devread (superblock, 0, sizeof (struct reiserfs_super_block),
277 || (substring (REISER3FS_SUPER_MAGIC_STRING, super.s_magic) > 0
278 && substring (REISER2FS_SUPER_MAGIC_STRING, super.s_magic) > 0
279 && substring (REISERFS_SUPER_MAGIC_STRING, super.s_magic) > 0)
280 || (/* check that this is not a copy inside the journal log */
281 sb_journal_block(&super) * sb_blocksize(&super)
282 <= REISERFS_DISK_OFFSET_IN_BYTES))
284 /* Try old super block position */
285 superblock = REISERFS_OLD_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),
291 if (substring (REISER2FS_SUPER_MAGIC_STRING, super.s_magic) > 0
292 && substring (REISERFS_SUPER_MAGIC_STRING, super.s_magic) > 0)
294 /* pre journaling super block ? */
295 if (substring (REISERFS_SUPER_MAGIC_STRING,
296 (char*) ((int) &super + 20)) > 0)
299 set_sb_blocksize(&super, REISERFS_OLD_BLOCKSIZE);
300 set_sb_journal_block(&super, 0);
301 set_sb_version(&super, 0);
305 /* check the version number. */
306 if (sb_version(&super) > REISERFS_MAX_SUPPORTED_VERSION)
309 INFO->version = sb_version(&super);
310 INFO->blocksize = sb_blocksize(&super);
311 INFO->fullblocksize_shift = log2 (sb_blocksize(&super));
312 INFO->blocksize_shift = INFO->fullblocksize_shift - SECTOR_BITS;
314 (FSYSREISER_CACHE_SIZE >> INFO->fullblocksize_shift) - 1;
317 printf ("reiserfs_mount: version=%d, blocksize=%d\n",
318 INFO->version, INFO->blocksize);
319 #endif /* REISERDEBUG */
321 /* Clear node cache. */
322 memset (INFO->blocks, 0, sizeof (INFO->blocks));
324 if (sb_blocksize(&super) < FSYSREISER_MIN_BLOCKSIZE
325 || sb_blocksize(&super) > FSYSREISER_MAX_BLOCKSIZE
326 || (SECTOR_SIZE << INFO->blocksize_shift) != sb_blocksize(&super))
329 /* Initialize journal code. If something fails we end with zero
330 * journal_transactions, so we don't access the journal at all.
332 INFO->journal_transactions = 0;
333 if (sb_journal_block(&super) != 0 && super.s_journal_dev == 0)
335 INFO->journal_block = sb_journal_block(&super);
336 INFO->journal_block_count = sb_journal_size(&super);
337 if (is_power_of_two (INFO->journal_block_count))
340 /* Read in super block again, maybe it is in the journal */
341 block_read (superblock >> INFO->blocksize_shift,
342 0, sizeof (struct reiserfs_super_block), (char *) &super);
345 if (! block_read (sb_root_block(&super), 0, INFO->blocksize, (char*) ROOT))
349 INFO->tree_depth = __le16_to_cpu(BLOCKHEAD (cache)->blk_level);
352 printf ("root read_in: block=%d, depth=%d\n",
353 sb_root_block(&super), INFO->tree_depth);
354 #endif /* REISERDEBUG */
356 if (INFO->tree_depth >= MAX_HEIGHT)
358 if (INFO->tree_depth == DISK_LEAF_NODE_LEVEL)
360 /* There is only one node in the whole filesystem,
361 * which is simultanously leaf and root */
362 memcpy (LEAF, ROOT, INFO->blocksize);
367 /***************** TREE ACCESSING METHODS *****************************/
369 /* I assume you are familiar with the ReiserFS tree, if not go to
370 * http://www.namesys.com/content_table.html
372 * My tree node cache is organized as following
374 * 1 LEAF node (if the ROOT is also a LEAF it is copied here
375 * 2-n other nodes on current path from bottom to top.
376 * if there is not enough space in the cache, the top most are
379 * I have only two methods to find a key in the tree:
380 * search_stat(dir_id, objectid) searches for the stat entry (always
381 * the first entry) of an object.
382 * next_key() gets the next key in tree order.
384 * This means, that I can only sequential reads of files are
385 * efficient, but this really doesn't hurt for grub.
388 /* Read in the node at the current path and depth into the node cache.
389 * You must set INFO->blocks[depth] before.
392 read_tree_node (unsigned int blockNr, int depth)
394 char* cache = CACHE(depth);
395 int num_cached = INFO->cached_slots;
396 if (depth < num_cached)
398 /* This is the cached part of the path. Check if same block is
401 if (blockNr == INFO->blocks[depth])
405 cache = CACHE(num_cached);
408 printf (" next read_in: block=%d (depth=%d)\n",
410 #endif /* REISERDEBUG */
411 if (! block_read (blockNr, 0, INFO->blocksize, cache))
413 /* Make sure it has the right node level */
414 if (__le16_to_cpu(BLOCKHEAD (cache)->blk_level) != depth)
416 errnum = ERR_FSYS_CORRUPT;
420 INFO->blocks[depth] = blockNr;
424 /* Get the next key, i.e. the key following the last retrieved key in
425 * tree order. INFO->current_ih and
426 * INFO->current_info are adapted accordingly. */
431 struct item_head *ih = INFO->current_ih + 1;
435 printf ("next_key:\n old ih: key %d:%d:%d:%d version:%d\n",
436 __le32_to_cpu(INFO->current_ih->ih_key.k_dir_id),
437 __le32_to_cpu(INFO->current_ih->ih_key.k_objectid),
438 __le32_to_cpu(INFO->current_ih->ih_key.u.v1.k_offset),
439 __le32_to_cpu(INFO->current_ih->ih_key.u.v1.k_uniqueness),
440 __le16_to_cpu(INFO->current_ih->ih_version));
441 #endif /* REISERDEBUG */
443 if (ih == &ITEMHEAD[__le16_to_cpu(BLOCKHEAD (LEAF)->blk_nr_item)])
445 depth = DISK_LEAF_NODE_LEVEL;
446 /* The last item, was the last in the leaf node.
447 * Read in the next block
451 if (depth == INFO->tree_depth)
453 /* There are no more keys at all.
454 * Return a dummy item with MAX_KEY */
455 ih = (struct item_head *) &BLOCKHEAD (LEAF)->blk_right_delim_key;
460 printf (" depth=%d, i=%d\n", depth, INFO->next_key_nr[depth]);
461 #endif /* REISERDEBUG */
463 while (INFO->next_key_nr[depth] == 0);
465 if (depth == INFO->tree_depth)
467 else if (depth <= INFO->cached_slots)
468 cache = CACHE (depth);
471 cache = read_tree_node (INFO->blocks[depth], depth);
478 int nr_item = __le16_to_cpu(BLOCKHEAD (cache)->blk_nr_item);
479 int key_nr = INFO->next_key_nr[depth]++;
481 printf (" depth=%d, i=%d/%d\n", depth, key_nr, nr_item);
482 #endif /* REISERDEBUG */
483 if (key_nr == nr_item)
484 /* This is the last item in this block, set the next_key_nr to 0 */
485 INFO->next_key_nr[depth] = 0;
487 cache = read_tree_node (dc_block_number(&(DC (cache)[key_nr])), --depth);
491 while (depth > DISK_LEAF_NODE_LEVEL);
496 INFO->current_ih = ih;
497 INFO->current_item = &LEAF[__le16_to_cpu(ih->ih_item_location)];
499 printf (" new ih: key %d:%d:%d:%d version:%d\n",
500 __le32_to_cpu(INFO->current_ih->ih_key.k_dir_id),
501 __le32_to_cpu(INFO->current_ih->ih_key.k_objectid),
502 __le32_to_cpu(INFO->current_ih->ih_key.u.v1.k_offset),
503 __le32_to_cpu(INFO->current_ih->ih_key.u.v1.k_uniqueness),
504 __le16_to_cpu(INFO->current_ih->ih_version));
505 #endif /* REISERDEBUG */
509 /* preconditions: reiserfs_mount already executed, therefore
510 * INFO block is valid
511 * returns: 0 if error (errnum is set),
512 * nonzero iff we were able to find the key successfully.
513 * postconditions: on a nonzero return, the current_ih and
514 * current_item fields describe the key that equals the
515 * searched key. INFO->next_key contains the next key after
517 * side effects: messes around with the cache.
520 search_stat (__u32 dir_id, __u32 objectid)
526 struct item_head *ih;
528 printf ("search_stat:\n key %d:%d:0:0\n", dir_id, objectid);
529 #endif /* REISERDEBUG */
531 depth = INFO->tree_depth;
534 while (depth > DISK_LEAF_NODE_LEVEL)
537 nr_item = __le16_to_cpu(BLOCKHEAD (cache)->blk_nr_item);
541 for (i = 0; i < nr_item; i++)
543 if (__le32_to_cpu(key->k_dir_id) > dir_id
544 || (__le32_to_cpu(key->k_dir_id) == dir_id
545 && (__le32_to_cpu(key->k_objectid) > objectid
546 || (__le32_to_cpu(key->k_objectid) == objectid
547 && (__le32_to_cpu(key->u.v1.k_offset)
548 | __le32_to_cpu(key->u.v1.k_uniqueness)) > 0))))
554 printf (" depth=%d, i=%d/%d\n", depth, i, nr_item);
555 #endif /* REISERDEBUG */
556 INFO->next_key_nr[depth] = (i == nr_item) ? 0 : i+1;
557 cache = read_tree_node (dc_block_number(&(DC (cache)[i])), --depth);
563 nr_item = __le16_to_cpu(BLOCKHEAD (LEAF)->blk_nr_item);
565 for (i = 0; i < nr_item; i++)
567 if (__le32_to_cpu(ih->ih_key.k_dir_id) == dir_id
568 && __le32_to_cpu(ih->ih_key.k_objectid) == objectid
569 && __le32_to_cpu(ih->ih_key.u.v1.k_offset) == 0
570 && __le32_to_cpu(ih->ih_key.u.v1.k_uniqueness) == 0)
573 printf (" depth=%d, i=%d/%d\n", depth, i, nr_item);
574 #endif /* REISERDEBUG */
575 INFO->current_ih = ih;
576 INFO->current_item = &LEAF[__le16_to_cpu(ih->ih_item_location)];
581 errnum = ERR_FSYS_CORRUPT;
586 reiserfs_read (char *buf, unsigned len)
588 unsigned int blocksize;
590 unsigned int to_read;
591 char *prev_buf = buf;
594 printf ("reiserfs_read: filepos=%d len=%d, offset=%Lx\n",
595 filepos, len, (__u64) IH_KEY_OFFSET (INFO->current_ih) - 1);
596 #endif /* REISERDEBUG */
598 if (__le32_to_cpu(INFO->current_ih->ih_key.k_objectid) != INFO->fileinfo.k_objectid
599 || IH_KEY_OFFSET (INFO->current_ih) > filepos + 1)
601 search_stat (INFO->fileinfo.k_dir_id, INFO->fileinfo.k_objectid);
607 if (__le32_to_cpu(INFO->current_ih->ih_key.k_objectid) != INFO->fileinfo.k_objectid) {
611 offset = filepos - IH_KEY_OFFSET (INFO->current_ih) + 1;
612 blocksize = __le16_to_cpu(INFO->current_ih->ih_item_len);
615 printf (" loop: filepos=%d len=%d, offset=%d blocksize=%d\n",
616 filepos, len, offset, blocksize);
617 #endif /* REISERDEBUG */
619 if (IH_KEY_ISTYPE(INFO->current_ih, TYPE_DIRECT)
620 && offset < blocksize)
623 printf ("direct_read: offset=%d, blocksize=%d\n",
625 #endif /* REISERDEBUG */
626 to_read = blocksize - offset;
630 memcpy (buf, INFO->current_item + offset, to_read);
633 else if (IH_KEY_ISTYPE(INFO->current_ih, TYPE_INDIRECT))
635 blocksize = (blocksize >> 2) << INFO->fullblocksize_shift;
637 printf ("indirect_read: offset=%d, blocksize=%d\n",
639 #endif /* REISERDEBUG */
641 while (offset < blocksize)
643 __u32 blocknr = __le32_to_cpu(((__u32 *) INFO->current_item)
644 [offset >> INFO->fullblocksize_shift]);
645 int blk_offset = offset & (INFO->blocksize-1);
646 to_read = INFO->blocksize - blk_offset;
650 /* Journal is only for meta data. Data blocks can be read
651 * directly without using block_read
653 reiserfs_devread (blocknr << INFO->blocksize_shift,
654 blk_offset, to_read, buf);
668 return errnum ? 0 : buf - prev_buf;
672 /* preconditions: reiserfs_mount already executed, therefore
673 * INFO block is valid
674 * returns: 0 if error, nonzero iff we were able to find the file successfully
675 * postconditions: on a nonzero return, INFO->fileinfo contains the info
676 * of the file we were trying to look up, filepos is 0 and filemax is
677 * the size of the file.
680 reiserfs_dir (char *dirname)
682 struct reiserfs_de_head *de_head;
684 __u32 dir_id, objectid, parent_dir_id = 0, parent_objectid = 0;
686 int do_possibilities = 0;
687 #endif /* ! STAGE1_5 */
688 char linkbuf[PATH_MAX]; /* buffer for following symbolic links */
692 dir_id = REISERFS_ROOT_PARENT_OBJECTID;
693 objectid = REISERFS_ROOT_OBJECTID;
698 printf ("dirname=%s\n", dirname);
699 #endif /* REISERDEBUG */
701 /* Search for the stat info first. */
702 if (! search_stat (dir_id, objectid))
706 printf ("sd_mode=%x sd_size=%d\n",
707 stat_data_v1(INFO->current_ih) ? sd_v1_mode((struct stat_data_v1 *) INFO->current_item) :
708 sd_v2_mode((struct stat_data *) (INFO->current_item)),
709 stat_data_v1(INFO->current_ih) ? sd_v1_size((struct stat_data_v1 *) INFO->current_item) :
710 sd_v2_size((struct stat_data *) INFO->current_item)
713 #endif /* REISERDEBUG */
714 mode = stat_data_v1(INFO->current_ih) ?
715 sd_v1_mode((struct stat_data_v1 *) INFO->current_item) :
716 sd_v2_mode((struct stat_data *) INFO->current_item);
718 /* If we've got a symbolic link, then chase it. */
722 if (++link_count > MAX_LINK_COUNT)
724 errnum = ERR_SYMLINK_LOOP;
728 /* Get the symlink size. */
729 filemax = stat_data_v1(INFO->current_ih) ?
730 sd_v1_size((struct stat_data_v1 *) INFO->current_item) :
731 sd_v2_size((struct stat_data *) INFO->current_item);
733 /* Find out how long our remaining name is. */
735 while (dirname[len] && !isspace (dirname[len]))
738 if (filemax + len > sizeof (linkbuf) - 1)
740 errnum = ERR_FILELENGTH;
744 /* Copy the remaining name to the end of the symlink data.
745 Note that DIRNAME and LINKBUF may overlap! */
746 memmove (linkbuf + filemax, dirname, len+1);
748 INFO->fileinfo.k_dir_id = dir_id;
749 INFO->fileinfo.k_objectid = objectid;
752 || reiserfs_read (linkbuf, filemax) != filemax)
755 errnum = ERR_FSYS_CORRUPT;
760 printf ("symlink=%s\n", linkbuf);
761 #endif /* REISERDEBUG */
766 /* It's an absolute link, so look it up in root. */
767 dir_id = REISERFS_ROOT_PARENT_OBJECTID;
768 objectid = REISERFS_ROOT_OBJECTID;
772 /* Relative, so look it up in our parent directory. */
773 dir_id = parent_dir_id;
774 objectid = parent_objectid;
777 /* Now lookup the new name. */
781 /* if we have a real file (and we're not just printing possibilities),
782 then this is where we want to exit */
784 if (! *dirname || isspace (*dirname))
786 if (! S_ISREG (mode))
788 errnum = ERR_BAD_FILETYPE;
793 filemax = stat_data_v1(INFO->current_ih) ?
794 sd_v1_size((struct stat_data_v1 *) INFO->current_item) :
795 sd_v2_size((struct stat_data *) INFO->current_item);
797 /* If this is a new stat data and size is > 4GB set filemax to
800 if (__le16_to_cpu(INFO->current_ih->ih_version) == ITEM_VERSION_2
801 && sd_size_hi((struct stat_data *) INFO->current_item) > 0)
802 filemax = 0xffffffff;
804 INFO->fileinfo.k_dir_id = dir_id;
805 INFO->fileinfo.k_objectid = objectid;
809 /* continue with the file/directory name interpretation */
810 while (*dirname == '/')
812 if (! S_ISDIR (mode))
814 errnum = ERR_BAD_FILETYPE;
817 for (rest = dirname; (ch = *rest) && ! isspace (ch) && ch != '/'; rest++);
821 if (print_possibilities && ch != '/')
822 do_possibilities = 1;
823 # endif /* ! STAGE1_5 */
833 printf ("ih: key %d:%d:%d:%d version:%d\n",
834 __le32_to_cpu(INFO->current_ih->ih_key.k_dir_id),
835 __le32_to_cpu(INFO->current_ih->ih_key.k_objectid),
836 __le32_to_cpu(INFO->current_ih->ih_key.u.v1.k_offset),
837 __le32_to_cpu(INFO->current_ih->ih_key.u.v1.k_uniqueness),
838 __le16_to_cpu(INFO->current_ih->ih_version));
839 #endif /* REISERDEBUG */
841 if (__le32_to_cpu(INFO->current_ih->ih_key.k_objectid) != objectid)
844 name_end = INFO->current_item + __le16_to_cpu(INFO->current_ih->ih_item_len);
845 de_head = (struct reiserfs_de_head *) INFO->current_item;
846 num_entries = __le16_to_cpu(INFO->current_ih->u.ih_entry_count);
847 while (num_entries > 0)
849 char *filename = INFO->current_item + deh_location(de_head);
850 char tmp = *name_end;
851 if ((deh_state(de_head) & DEH_Visible))
854 /* Directory names in ReiserFS are not null
855 * terminated. We write a temporary 0 behind it.
856 * NOTE: that this may overwrite the first block in
857 * the tree cache. That doesn't hurt as long as we
858 * don't call next_key () in between.
861 cmp = substring (dirname, filename);
864 if (do_possibilities)
869 struct fsys_reiser_info info_save;
871 if (print_possibilities > 0)
872 print_possibilities = -print_possibilities;
874 strcpy(fn, filename);
877 /* If NAME is "." or "..", do not count it. */
878 if (strcmp (fn, ".") != 0 && strcmp (fn, "..") != 0) {
879 memcpy(&info_save, INFO, sizeof(struct fsys_reiser_info));
880 search_stat (deh_dir_id(de_head), deh_objectid(de_head));
881 sd_print_item(INFO->current_ih, INFO->current_item);
883 search_stat (dir_id, objectid);
884 memcpy(INFO, &info_save, sizeof(struct fsys_reiser_info));
889 # endif /* ! STAGE1_5 */
893 /* The beginning of this name marks the end of the next name.
902 if (print_possibilities < 0)
904 # endif /* ! STAGE1_5 */
906 errnum = ERR_FILE_NOT_FOUND;
914 parent_dir_id = dir_id;
915 parent_objectid = objectid;
916 dir_id = deh_dir_id(de_head);
917 objectid = deh_objectid(de_head);
922 * U-Boot interface functions
926 * List given directory
928 * RETURN: 0 - OK, else grub_error_t errnum
931 reiserfs_ls (char *dirname)
937 dir_slash = malloc(strlen(dirname) + 1);
938 if (dir_slash == NULL) {
939 return ERR_NUMBER_OVERFLOW;
941 strcpy(dir_slash, dirname);
942 /* add "/" to the directory name */
943 strcat(dir_slash, "/");
945 print_possibilities = 1;
946 res = reiserfs_dir (dir_slash);
948 if (!res || errnum) {
956 * Open file for reading
958 * RETURN: >0 - OK, size of opened file
959 * <0 - ERROR -grub_error_t errnum
962 reiserfs_open (char *filename)
966 print_possibilities = 0;
967 if (!reiserfs_dir (filename) || errnum) {