3 Copyright (C) 2001 Jeffrey Mahoney (jeffm@suse.com)
7 Copyright (C) 2000, 2001 Free Software Foundation, Inc.
9 This program is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 2 of the License, or
12 (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., 675 Mass Ave, Cambridge, MA 02139, USA.
31 #include "reiserfs/reiserfs.h"
33 /* Exported in struct fs_t */
34 static int reiserfs_open( struct boot_file_t *file, const char *dev_name,
35 struct partition_t *part, const char *file_name );
36 static int reiserfs_read( struct boot_file_t *file, unsigned int size,
39 static int reiserfs_seek( struct boot_file_t *file, unsigned int newpos );
40 static int reiserfs_close( struct boot_file_t *file );
42 struct fs_t reiserfs_filesystem = {
50 static int reiserfs_read_super( void );
51 static int reiserfs_open_file( char *dirname );
52 static int reiserfs_read_data( char *buf, __u32 len );
55 static struct reiserfs_state reiserfs;
56 static struct reiserfs_state *INFO = &reiserfs;
58 /* Adapted from GRUB: */
59 static char FSYS_BUF[FSYSREISER_CACHE_SIZE];
64 reiserfs_open( struct boot_file_t *file, const char *dev_name,
65 struct partition_t *part, const char *file_name )
67 static char buffer[1024];
72 memset( INFO, 0, sizeof(struct reiserfs_state) );
77 DEBUG_F( "Determining offset for partition %d\n", part->part_number );
78 INFO->partition_offset = ((__u64)(part->part_start)) * ((__u64)part->blocksize);
79 DEBUG_F( "%Lu = %lu * %hu\n", INFO->partition_offset,
84 INFO->partition_offset = 0;
86 sprintf( buffer, "%s:%d", dev_name, 0 ); /* 0 is full disk in OF */
87 file->of_device = prom_open( buffer );
88 DEBUG_F( "Trying to open dev_name=%s; filename=%s; partition offset=%Lu\n",
89 buffer, file_name, INFO->partition_offset );
91 if ( file->of_device == PROM_INVALID_HANDLE || file->of_device == NULL )
93 DEBUG_F( "Can't open device %p\n", file->of_device );
94 DEBUG_LEAVE(FILE_ERR_BADDEV);
95 return FILE_ERR_BADDEV;
98 DEBUG_F("%p was successfully opened\n", file->of_device);
100 if ( reiserfs_read_super() != 1 )
102 DEBUG_F( "Couldn't open ReiserFS @ %s/%Lu\n", buffer, INFO->partition_offset );
103 prom_close( file->of_device );
104 DEBUG_LEAVE(FILE_ERR_BAD_FSYS);
105 return FILE_ERR_BAD_FSYS;
108 DEBUG_F( "Attempting to open %s\n", file_name );
109 strcpy(buffer, file_name); /* reiserfs_open_file modifies argument */
110 if (reiserfs_open_file(buffer) == 0)
112 DEBUG_F( "reiserfs_open_file failed. errnum = %d\n", errnum );
113 prom_close( file->of_device );
114 DEBUG_LEAVE_F(errnum);
118 DEBUG_F( "Successfully opened %s\n", file_name );
120 DEBUG_LEAVE(FILE_ERR_OK);
126 reiserfs_read( struct boot_file_t *file, unsigned int size, void *buffer )
128 return reiserfs_read_data( buffer, size );
132 reiserfs_seek( struct boot_file_t *file, unsigned int newpos )
139 reiserfs_close( struct boot_file_t *file )
141 if( file->of_device )
143 prom_close(file->of_device);
145 DEBUG_F("reiserfs_close called\n");
151 static __inline__ __u32
155 while( word && (word & (1 << ++i)) == 0 );
159 static __inline__ int
160 is_power_of_two( unsigned long word )
162 return ( word & -word ) == word;
166 read_disk_block( struct boot_file_t *file, __u32 block, __u32 start,
167 __u32 length, void *buf )
169 __u16 fs_blocksize = INFO->blocksize == 0 ? REISERFS_OLD_BLOCKSIZE
171 unsigned long long pos = block * fs_blocksize;
172 pos += INFO->partition_offset + start;
173 DEBUG_F( "Reading %u bytes, starting at block %u, disk offset %Lu\n",
174 length, block, pos );
175 if (!prom_lseek( file->of_device, pos )) {
176 DEBUG_F("prom_lseek failed\n");
179 return prom_read( file->of_device, buf, length );
184 journal_read( __u32 block, __u32 len, char *buffer )
186 return read_disk_block( INFO->file,
187 (INFO->journal_block + block), 0,
191 /* Read a block from ReiserFS file system, taking the journal into
192 * account. If the block nr is in the journal, the block from the
196 block_read( __u32 blockNr, __u32 start, __u32 len, char *buffer )
198 __u32 transactions = INFO->journal_transactions;
199 __u32 desc_block = INFO->journal_first_desc;
200 __u32 journal_mask = INFO->journal_block_count - 1;
201 __u32 translatedNr = blockNr;
202 __u32 *journal_table = JOURNAL_START;
204 // DEBUG_F( "block_read( %u, %u, %u, ..)\n", blockNr, start, len );
206 while ( transactions-- > 0 )
211 if ( *journal_table != 0xffffffff )
213 /* Search for the blockNr in cached journal */
214 j_len = le32_to_cpu(*journal_table++);
215 while ( i++ < j_len )
217 if ( le32_to_cpu(*journal_table++) == blockNr )
219 journal_table += j_len - i;
226 /* This is the end of cached journal marker. The remaining
227 * transactions are still on disk. */
228 struct reiserfs_journal_desc desc;
229 struct reiserfs_journal_commit commit;
231 if ( !journal_read( desc_block, sizeof(desc), (char *) &desc ) )
234 j_len = le32_to_cpu(desc.j_len);
235 while ( i < j_len && i < JOURNAL_TRANS_HALF )
236 if ( le32_to_cpu(desc.j_realblock[i++]) == blockNr )
239 if ( j_len >= JOURNAL_TRANS_HALF )
241 int commit_block = ( desc_block + 1 + j_len ) & journal_mask;
243 if ( !journal_read( commit_block,
244 sizeof(commit), (char *) &commit ) )
248 if ( le32_to_cpu(commit.j_realblock[i++ - JOURNAL_TRANS_HALF]) == blockNr )
256 INFO->journal_block + ( ( desc_block + i ) & journal_mask );
258 DEBUG_F( "block_read: block %u is mapped to journal block %u.\n",
259 blockNr, translatedNr - INFO->journal_block );
261 /* We must continue the search, as this block may be overwritten in
262 * later transactions. */
264 desc_block = (desc_block + 2 + j_len) & journal_mask;
267 return read_disk_block( INFO->file, translatedNr, start, len, buffer );
270 /* Init the journal data structure. We try to cache as much as
271 * possible in the JOURNAL_START-JOURNAL_END space, but if it is full
272 * we can still read the rest from the disk on demand.
274 * The first number of valid transactions and the descriptor block of the
275 * first valid transaction are held in INFO. The transactions are all
276 * adjacent, but we must take care of the journal wrap around.
281 struct reiserfs_journal_header header;
282 struct reiserfs_journal_desc desc;
283 struct reiserfs_journal_commit commit;
284 __u32 block_count = INFO->journal_block_count;
288 __u32 *journal_table = JOURNAL_START;
290 journal_read( block_count, sizeof ( header ), ( char * ) &header );
291 desc_block = le32_to_cpu(header.j_first_unflushed_offset);
292 if ( desc_block >= block_count )
295 INFO->journal_transactions = 0;
296 INFO->journal_first_desc = desc_block;
297 next_trans_id = le32_to_cpu(header.j_last_flush_trans_id) + 1;
299 DEBUG_F( "journal_init: last flushed %u\n", le32_to_cpu(header.j_last_flush_trans_id) );
303 journal_read( desc_block, sizeof(desc), (char *) &desc );
304 if ( strcmp( JOURNAL_DESC_MAGIC, desc.j_magic ) != 0
305 || desc.j_trans_id != next_trans_id
306 || desc.j_mount_id != header.j_mount_id )
307 /* no more valid transactions */
310 commit_block = ( desc_block + le32_to_cpu(desc.j_len) + 1 ) & ( block_count - 1 );
311 journal_read( commit_block, sizeof(commit), (char *) &commit );
312 if ( desc.j_trans_id != commit.j_trans_id
313 || desc.j_len != commit.j_len )
314 /* no more valid transactions */
318 DEBUG_F( "Found valid transaction %u/%u at %u.\n",
319 le32_to_cpu(desc.j_trans_id), le32_to_cpu(desc.j_mount_id),
324 if ( journal_table < JOURNAL_END )
326 if ( ( journal_table + 1 + le32_to_cpu(desc.j_len) ) >= JOURNAL_END )
328 /* The table is almost full; mark the end of the cached * *
330 *journal_table = 0xffffffff;
331 journal_table = JOURNAL_END;
337 /* Cache the length and the realblock numbers in the table. *
338 * The block number of descriptor can easily be computed. *
339 * and need not to be stored here. */
340 *journal_table++ = desc.j_len;
341 for ( i = 0; i < le32_to_cpu(desc.j_len) && i < JOURNAL_TRANS_HALF; i++ )
343 *journal_table++ = desc.j_realblock[i];
345 DEBUG_F( "block %u is in journal %u.\n",
346 le32_to_cpu(desc.j_realblock[i]), desc_block );
349 for ( ; i < le32_to_cpu(desc.j_len); i++ )
352 commit.j_realblock[i - JOURNAL_TRANS_HALF];
354 DEBUG_F( "block %u is in journal %u.\n",
355 le32_to_cpu(commit.j_realblock[i - JOURNAL_TRANS_HALF]),
361 desc_block = (commit_block + 1) & (block_count - 1);
364 DEBUG_F( "Transaction %u/%u at %u isn't valid.\n",
365 le32_to_cpu(desc.j_trans_id), le32_to_cpu(desc.j_mount_id),
369 INFO->journal_transactions
370 = next_trans_id - le32_to_cpu(header.j_last_flush_trans_id) - 1;
371 return (errnum == 0);
374 /* check filesystem types and read superblock into memory buffer */
376 reiserfs_read_super( void )
378 struct reiserfs_super_block super;
379 __u64 superblock = REISERFS_SUPERBLOCK_BLOCK;
381 if (read_disk_block(INFO->file, superblock, 0, sizeof(super), &super) != sizeof(super)) {
382 DEBUG_F("read_disk_block failed!\n");
386 DEBUG_F( "Found super->magic: \"%s\"\n", super.s_magic );
388 if( strcmp( REISER2FS_SUPER_MAGIC_STRING, super.s_magic ) != 0 &&
389 strcmp( REISERFS_SUPER_MAGIC_STRING, super.s_magic ) != 0 )
391 /* Try old super block position */
392 superblock = REISERFS_OLD_SUPERBLOCK_BLOCK;
394 if (read_disk_block( INFO->file, superblock, 0, sizeof (super), &super ) != sizeof(super)) {
395 DEBUG_F("read_disk_block failed!\n");
399 if ( strcmp( REISER2FS_SUPER_MAGIC_STRING, super.s_magic ) != 0 &&
400 strcmp( REISERFS_SUPER_MAGIC_STRING, super.s_magic ) != 0 )
402 /* pre journaling super block - untested */
403 if ( strcmp( REISERFS_SUPER_MAGIC_STRING,
404 (char *) ((__u32) &super + 20 ) ) != 0 )
407 super.s_blocksize = cpu_to_le16(REISERFS_OLD_BLOCKSIZE);
408 super.s_journal_block = 0;
413 DEBUG_F( "ReiserFS superblock data:\n" );
414 DEBUG_F( "Block count: %u\n", le32_to_cpu(super.s_block_count) )
415 DEBUG_F( "Free blocks: %u\n", le32_to_cpu(super.s_free_blocks) );
416 DEBUG_F( "Journal block: %u\n", le32_to_cpu(super.s_journal_block) );
417 DEBUG_F( "Journal size (in blocks): %u\n",
418 le32_to_cpu(super.s_orig_journal_size) );
419 DEBUG_F( "Root block: %u\n\n", le32_to_cpu(super.s_root_block) );
422 INFO->version = le16_to_cpu(super.s_version);
423 INFO->blocksize = le16_to_cpu(super.s_blocksize);
424 INFO->blocksize_shift = log2( INFO->blocksize );
426 INFO->journal_block = le32_to_cpu(super.s_journal_block);
427 INFO->journal_block_count = le32_to_cpu(super.s_orig_journal_size);
429 INFO->cached_slots = (FSYSREISER_CACHE_SIZE >> INFO->blocksize_shift) - 1;
431 /* At this point, we've found a valid superblock. If we run into problems
432 * mounting the FS, the user should probably know. */
434 /* A few sanity checks ... */
435 if ( INFO->version > REISERFS_MAX_SUPPORTED_VERSION )
437 prom_printf( "ReiserFS: Unsupported version field: %u\n",
442 if ( INFO->blocksize < FSYSREISER_MIN_BLOCKSIZE
443 || INFO->blocksize > FSYSREISER_MAX_BLOCKSIZE )
445 prom_printf( "ReiserFS: Unsupported block size: %u\n",
450 /* Setup the journal.. */
451 if ( INFO->journal_block != 0 )
453 if ( !is_power_of_two( INFO->journal_block_count ) )
455 prom_printf( "ReiserFS: Unsupported journal size, "
456 "not a power of 2: %u\n",
457 INFO->journal_block_count );
462 /* Read in super block again, maybe it is in the journal */
463 block_read( superblock, 0, sizeof (struct reiserfs_super_block),
467 /* Read in the root block */
468 if ( !block_read( le32_to_cpu(super.s_root_block), 0,
469 INFO->blocksize, ROOT ) )
471 prom_printf( "ReiserFS: Failed to read in root block\n" );
475 /* The root node is always the "deepest", so we can
476 determine the hieght of the tree using it. */
477 INFO->tree_depth = blkh_level(BLOCKHEAD(ROOT));
480 DEBUG_F( "root read_in: block=%u, depth=%u\n",
481 le32_to_cpu(super.s_root_block), INFO->tree_depth );
483 if ( INFO->tree_depth >= REISERFS_MAX_TREE_HEIGHT )
485 prom_printf( "ReiserFS: Unsupported tree depth (too deep): %u\n",
490 if ( INFO->tree_depth == BLKH_LEVEL_LEAF )
492 /* There is only one node in the whole filesystem, which is
493 simultanously leaf and root */
494 memcpy( LEAF, ROOT, INFO->blocksize );
499 /***************** TREE ACCESSING METHODS *****************************/
501 /* I assume you are familiar with the ReiserFS tree, if not go to
502 * http://devlinux.com/projects/reiserfs/
504 * My tree node cache is organized as following
506 * 1 LEAF node (if the ROOT is also a LEAF it is copied here
507 * 2-n other nodes on current path from bottom to top.
508 * if there is not enough space in the cache, the top most are
511 * I have only two methods to find a key in the tree:
512 * search_stat(dir_id, objectid) searches for the stat entry (always
513 * the first entry) of an object.
514 * next_key() gets the next key in tree order.
516 * This means, that I can only sequential reads of files are
517 * efficient, but this really doesn't hurt for grub.
520 /* Read in the node at the current path and depth into the node cache.
521 * You must set INFO->blocks[depth] before.
524 read_tree_node( __u32 blockNr, __u16 depth )
526 char *cache = CACHE(depth);
527 int num_cached = INFO->cached_slots;
529 if ( depth < num_cached )
531 /* This is the cached part of the path.
532 Check if same block is needed. */
533 if ( blockNr == INFO->blocks[depth] )
537 cache = CACHE(num_cached);
539 DEBUG_F( " next read_in: block=%u (depth=%u)\n", blockNr, depth );
541 if ( !block_read( blockNr, 0, INFO->blocksize, cache ) )
543 DEBUG_F( "block_read failed\n" );
547 DEBUG_F( "FOUND: blk_level=%u, blk_nr_item=%u, blk_free_space=%u\n",
548 blkh_level(BLOCKHEAD(cache)),
549 blkh_nr_item(BLOCKHEAD(cache)),
550 le16_to_cpu(BLOCKHEAD(cache)->blk_free_space) );
552 /* Make sure it has the right node level */
553 if ( blkh_level(BLOCKHEAD(cache)) != depth )
555 DEBUG_F( "depth = %u != %u\n", blkh_level(BLOCKHEAD(cache)), depth );
556 DEBUG_LEAVE(FILE_ERR_BAD_FSYS);
557 errnum = FILE_ERR_BAD_FSYS;
561 INFO->blocks[depth] = blockNr;
565 /* Get the next key, i.e. the key following the last retrieved key in
566 * tree order. INFO->current_ih and
567 * INFO->current_info are adapted accordingly. */
572 struct item_head *ih = INFO->current_ih + 1;
576 DEBUG_F( "next_key:\n old ih: key %u:%u:%u:%u version:%u\n",
577 le32_to_cpu(INFO->current_ih->ih_key.k_dir_id),
578 le32_to_cpu(INFO->current_ih->ih_key.k_objectid),
579 le32_to_cpu(INFO->current_ih->ih_key.u.k_offset_v1.k_offset),
580 le32_to_cpu(INFO->current_ih->ih_key.u.k_offset_v1.k_uniqueness),
581 ih_version(INFO->current_ih) );
584 if ( ih == &ITEMHEAD[blkh_nr_item(BLOCKHEAD( LEAF ))] )
586 depth = BLKH_LEVEL_LEAF;
587 /* The last item, was the last in the leaf node. * Read in the next
591 if ( depth == INFO->tree_depth )
593 /* There are no more keys at all. * Return a dummy item with
596 ( struct item_head * )
597 &BLOCKHEAD( LEAF )->blk_right_delim_key;
602 DEBUG_F( " depth=%u, i=%u\n", depth, INFO->next_key_nr[depth] );
605 while ( INFO->next_key_nr[depth] == 0 );
607 if ( depth == INFO->tree_depth )
609 else if ( depth <= INFO->cached_slots )
610 cache = CACHE( depth );
613 cache = read_tree_node( INFO->blocks[depth], --depth );
620 __u16 nr_item = blkh_nr_item(BLOCKHEAD( cache ));
621 int key_nr = INFO->next_key_nr[depth]++;
624 DEBUG_F( " depth=%u, i=%u/%u\n", depth, key_nr, nr_item );
626 if ( key_nr == nr_item )
627 /* This is the last item in this block, set the next_key_nr *
629 INFO->next_key_nr[depth] = 0;
632 read_tree_node( dc_block_number( &(DC( cache )[key_nr])),
637 while ( depth > BLKH_LEVEL_LEAF );
642 INFO->current_ih = ih;
643 INFO->current_item = &LEAF[ih_location(ih)];
645 DEBUG_F( " new ih: key %u:%u:%u:%u version:%u\n",
646 le32_to_cpu(INFO->current_ih->ih_key.k_dir_id),
647 le32_to_cpu(INFO->current_ih->ih_key.k_objectid),
648 le32_to_cpu(INFO->current_ih->ih_key.u.k_offset_v1.k_offset),
649 le32_to_cpu(INFO->current_ih->ih_key.u.k_offset_v1.k_uniqueness),
650 ih_version(INFO->current_ih) );
655 /* preconditions: reiserfs_read_super already executed, therefore
656 * INFO block is valid
657 * returns: 0 if error (errnum is set),
658 * nonzero iff we were able to find the key successfully.
659 * postconditions: on a nonzero return, the current_ih and
660 * current_item fields describe the key that equals the
661 * searched key. INFO->next_key contains the next key after
663 * side effects: messes around with the cache.
666 search_stat( __u32 dir_id, __u32 objectid )
672 struct item_head *ih;
675 DEBUG_F( "search_stat:\n key %u:%u:0:0\n", le32_to_cpu(dir_id),
676 le32_to_cpu(objectid) );
679 depth = INFO->tree_depth;
682 DEBUG_F( "depth = %d\n", depth );
683 while ( depth > BLKH_LEVEL_LEAF )
687 nr_item = blkh_nr_item(BLOCKHEAD( cache ));
691 for ( i = 0; i < nr_item; i++ )
693 if (le32_to_cpu(key->k_dir_id) > le32_to_cpu(dir_id)
694 || (key->k_dir_id == dir_id
695 && (le32_to_cpu(key->k_objectid) > le32_to_cpu(objectid)
696 || (key->k_objectid == objectid
697 && (key->u.k_offset_v1.k_offset
698 | key->u.k_offset_v1.k_uniqueness) > 0))))
704 DEBUG_F( " depth=%d, i=%d/%d\n", depth, i, nr_item );
706 INFO->next_key_nr[depth] = ( i == nr_item ) ? 0 : i + 1;
707 cache = read_tree_node( dc_block_number(&(DC(cache)[i])), --depth );
713 nr_item = blkh_nr_item(BLOCKHEAD(LEAF));
715 DEBUG_F( "nr_item = %d\n", nr_item );
716 for ( i = 0; i < nr_item; i++ )
718 if ( ih->ih_key.k_dir_id == dir_id
719 && ih->ih_key.k_objectid == objectid
720 && ih->ih_key.u.k_offset_v1.k_offset == 0
721 && ih->ih_key.u.k_offset_v1.k_uniqueness == 0 )
724 DEBUG_F( " depth=%d, i=%d/%d\n", depth, i, nr_item );
726 INFO->current_ih = ih;
727 INFO->current_item = &LEAF[ih_location(ih)];
735 DEBUG_LEAVE(FILE_ERR_BAD_FSYS);
736 errnum = FILE_ERR_BAD_FSYS;
741 reiserfs_read_data( char *buf, __u32 len )
746 char *prev_buf = buf;
749 DEBUG_F( "reiserfs_read_data: INFO->file->pos=%Lu len=%u, offset=%Lu\n",
750 INFO->file->pos, len, (__u64) IH_KEY_OFFSET(INFO->current_ih) - 1 );
753 if ( INFO->current_ih->ih_key.k_objectid != INFO->fileinfo.k_objectid
754 || IH_KEY_OFFSET( INFO->current_ih ) > INFO->file->pos + 1 )
756 search_stat( INFO->fileinfo.k_dir_id, INFO->fileinfo.k_objectid );
760 while ( errnum == 0 )
762 if ( INFO->current_ih->ih_key.k_objectid != INFO->fileinfo.k_objectid )
765 offset = INFO->file->pos - IH_KEY_OFFSET( INFO->current_ih ) + 1;
766 blocksize = ih_item_len(INFO->current_ih);
769 DEBUG_F( " loop: INFO->file->pos=%Lu len=%u, offset=%u blocksize=%u\n",
770 INFO->file->pos, len, offset, blocksize );
773 if ( IH_KEY_ISTYPE( INFO->current_ih, TYPE_DIRECT )
774 && offset < blocksize )
776 to_read = blocksize - offset;
780 memcpy( buf, INFO->current_item + offset, to_read );
783 else if ( IH_KEY_ISTYPE( INFO->current_ih, TYPE_INDIRECT ) )
785 blocksize = ( blocksize >> 2 ) << INFO->blocksize_shift;
787 while ( offset < blocksize )
789 __u32 blocknr = le32_to_cpu(((__u32 *)
790 INFO->current_item)[offset >> INFO->blocksize_shift]);
792 int blk_offset = offset & (INFO->blocksize - 1);
794 to_read = INFO->blocksize - blk_offset;
798 /* Journal is only for meta data.
799 Data blocks can be read directly without using block_read */
800 read_disk_block( INFO->file, blocknr, blk_offset, to_read,
807 INFO->file->pos += to_read;
816 return (errnum != 0) ? 0 : buf - prev_buf;
820 /* preconditions: reiserfs_read_super already executed, therefore
821 * INFO block is valid
822 * returns: 0 if error, nonzero iff we were able to find the file successfully
823 * postconditions: on a nonzero return, INFO->fileinfo contains the info
824 * of the file we were trying to look up, filepos is 0 and filemax is
825 * the size of the file.
828 reiserfs_open_file( char *dirname )
830 struct reiserfs_de_head *de_head;
832 __u32 dir_id, objectid, parent_dir_id = 0, parent_objectid = 0;
834 char linkbuf[PATH_MAX]; /* buffer for following symbolic links */
838 dir_id = cpu_to_le32(REISERFS_ROOT_PARENT_OBJECTID);
839 objectid = cpu_to_le32(REISERFS_ROOT_OBJECTID);
844 DEBUG_F( "dirname=%s\n", dirname );
846 /* Search for the stat info first. */
847 if ( !search_stat( dir_id, objectid ) )
851 DEBUG_F( "sd_mode=0%o sd_size=%Lu\n",
852 sd_mode((struct stat_data *) INFO->current_item ),
853 sd_size(INFO->current_ih, INFO->current_item ));
856 mode = sd_mode((struct stat_data *)INFO->current_item);
858 /* If we've got a symbolic link, then chase it. */
859 if ( S_ISLNK( mode ) )
863 DEBUG_F("link count = %d\n", link_count);
865 if ( ++link_count > MAX_LINK_COUNT )
867 DEBUG_F("Symlink loop\n");
868 errnum = FILE_ERR_SYMLINK_LOOP;
872 /* Get the symlink size. */
873 INFO->file->len = sd_size(INFO->current_ih, INFO->current_item);
875 /* Find out how long our remaining name is. */
876 while ( dirname[len] && !isspace( dirname[len] ) )
879 if ( INFO->file->len + len > sizeof ( linkbuf ) - 1 )
881 errnum = FILE_ERR_LENGTH;
885 /* Copy the remaining name to the end of the symlink data. Note *
886 * that DIRNAME and LINKBUF may overlap! */
887 memmove( linkbuf + INFO->file->len, dirname, len + 1 );
889 INFO->fileinfo.k_dir_id = dir_id;
890 INFO->fileinfo.k_objectid = objectid;
893 || reiserfs_read_data( linkbuf, INFO->file->len ) != INFO->file->len ) {
894 DEBUG_F("reiserfs_open_file - if !next_key || reiserfs_read_data\n");
901 DEBUG_F( "symlink=%s\n", linkbuf );
905 if ( *dirname == '/' )
907 /* It's an absolute link, so look it up in root. */
908 dir_id = cpu_to_le32(REISERFS_ROOT_PARENT_OBJECTID);
909 objectid = cpu_to_le32(REISERFS_ROOT_OBJECTID);
913 /* Relative, so look it up in our parent directory. */
914 dir_id = parent_dir_id;
915 objectid = parent_objectid;
918 /* Now lookup the new name. */
922 /* if we have a real file (and we're not just printing *
923 * possibilities), then this is where we want to exit */
925 if ( !*dirname || isspace( *dirname ) )
927 if ( !S_ISREG( mode ) )
929 errnum = FILE_ERR_BAD_TYPE;
934 INFO->file->len = sd_size(INFO->current_ih, INFO->current_item);
936 INFO->fileinfo.k_dir_id = dir_id;
937 INFO->fileinfo.k_objectid = objectid;
941 /* continue with the file/directory name interpretation */
942 while ( *dirname == '/' )
944 if ( !S_ISDIR( mode ) )
946 errnum = FILE_ERR_NOTDIR;
949 for ( rest = dirname; ( ch = *rest ) && !isspace( ch ) && ch != '/';
961 if ( INFO->current_ih->ih_key.k_objectid != objectid )
964 name_end = INFO->current_item + ih_item_len(INFO->current_ih);
965 de_head = ( struct reiserfs_de_head * ) INFO->current_item;
966 num_entries = ih_entry_count(INFO->current_ih);
967 while ( num_entries > 0 )
969 char *filename = INFO->current_item + deh_location(de_head);
970 char tmp = *name_end;
972 if( deh_state(de_head) & (1 << DEH_Visible))
976 /* Directory names in ReiserFS are not null * terminated.
977 * We write a temporary 0 behind it. * NOTE: that this
978 * may overwrite the first block in * the tree cache.
979 * That doesn't hurt as long as we * don't call next_key
982 cmp = strcmp( dirname, filename );
987 /* The beginning of this name marks the end of the next name.
995 errnum = FILE_ERR_NOTFOUND;
1003 parent_dir_id = dir_id;
1004 parent_objectid = objectid;
1005 dir_id = de_head->deh_dir_id; /* LE */
1006 objectid = de_head->deh_objectid; /* LE */
1012 #ifndef __LITTLE_ENDIAN
1014 struct offset_v2 offset_v2;
1016 } offset_v2_esafe_overlay;
1019 offset_v2_k_type( struct offset_v2 *v2 )
1021 offset_v2_esafe_overlay tmp = *(offset_v2_esafe_overlay *)v2;
1022 tmp.linear = le64_to_cpu( tmp.linear );
1023 return tmp.offset_v2.k_type;
1027 offset_v2_k_offset( struct offset_v2 *v2 )
1029 offset_v2_esafe_overlay tmp = *(offset_v2_esafe_overlay *)v2;
1030 tmp.linear = le64_to_cpu( tmp.linear );
1031 return tmp.offset_v2.k_offset;
1036 uniqueness2type (__u32 uniqueness)
1038 switch (uniqueness) {
1039 case V1_SD_UNIQUENESS: return TYPE_STAT_DATA;
1040 case V1_INDIRECT_UNIQUENESS: return TYPE_INDIRECT;
1041 case V1_DIRECT_UNIQUENESS: return TYPE_DIRECT;
1042 case V1_DIRENTRY_UNIQUENESS: return TYPE_DIRENTRY;
1049 * c-file-style: "K&R"