xref: /OK3568_Linux_fs/u-boot/fs/reiserfs/reiserfs.c (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1*4882a593Smuzhiyun /*
2*4882a593Smuzhiyun  *  Copyright 2000-2002 by Hans Reiser, licensing governed by reiserfs/README
3*4882a593Smuzhiyun  *
4*4882a593Smuzhiyun  *  GRUB  --  GRand Unified Bootloader
5*4882a593Smuzhiyun  *  Copyright (C) 2000, 2001  Free Software Foundation, Inc.
6*4882a593Smuzhiyun  *
7*4882a593Smuzhiyun  *  (C) Copyright 2003 - 2004
8*4882a593Smuzhiyun  *  Sysgo AG, <www.elinos.com>, Pavel Bartusek <pba@sysgo.com>
9*4882a593Smuzhiyun  *
10*4882a593Smuzhiyun  *
11*4882a593Smuzhiyun  * SPDX-License-Identifier:	GPL-2.0+
12*4882a593Smuzhiyun  */
13*4882a593Smuzhiyun 
14*4882a593Smuzhiyun /* An implementation for the ReiserFS filesystem ported from GRUB.
15*4882a593Smuzhiyun  * Some parts of this code (mainly the structures and defines) are
16*4882a593Smuzhiyun  * from the original reiser fs code, as found in the linux kernel.
17*4882a593Smuzhiyun  */
18*4882a593Smuzhiyun 
19*4882a593Smuzhiyun #include <common.h>
20*4882a593Smuzhiyun #include <malloc.h>
21*4882a593Smuzhiyun #include <linux/ctype.h>
22*4882a593Smuzhiyun #include <linux/time.h>
23*4882a593Smuzhiyun #include <asm/byteorder.h>
24*4882a593Smuzhiyun #include <reiserfs.h>
25*4882a593Smuzhiyun 
26*4882a593Smuzhiyun #include "reiserfs_private.h"
27*4882a593Smuzhiyun 
28*4882a593Smuzhiyun #undef REISERDEBUG
29*4882a593Smuzhiyun 
30*4882a593Smuzhiyun /* Some parts of this code (mainly the structures and defines) are
31*4882a593Smuzhiyun  * from the original reiser fs code, as found in the linux kernel.
32*4882a593Smuzhiyun  */
33*4882a593Smuzhiyun 
34*4882a593Smuzhiyun static char fsys_buf[FSYS_BUFLEN];
35*4882a593Smuzhiyun static reiserfs_error_t errnum = ERR_NONE;
36*4882a593Smuzhiyun static int print_possibilities;
37*4882a593Smuzhiyun static unsigned int filepos, filemax;
38*4882a593Smuzhiyun 
39*4882a593Smuzhiyun static int
substring(const char * s1,const char * s2)40*4882a593Smuzhiyun substring (const char *s1, const char *s2)
41*4882a593Smuzhiyun {
42*4882a593Smuzhiyun   while (*s1 == *s2)
43*4882a593Smuzhiyun     {
44*4882a593Smuzhiyun       /* The strings match exactly. */
45*4882a593Smuzhiyun       if (! *(s1++))
46*4882a593Smuzhiyun 	return 0;
47*4882a593Smuzhiyun       s2 ++;
48*4882a593Smuzhiyun     }
49*4882a593Smuzhiyun 
50*4882a593Smuzhiyun   /* S1 is a substring of S2. */
51*4882a593Smuzhiyun   if (*s1 == 0)
52*4882a593Smuzhiyun     return -1;
53*4882a593Smuzhiyun 
54*4882a593Smuzhiyun   /* S1 isn't a substring. */
55*4882a593Smuzhiyun   return 1;
56*4882a593Smuzhiyun }
57*4882a593Smuzhiyun 
sd_print_item(struct item_head * ih,char * item)58*4882a593Smuzhiyun static void sd_print_item (struct item_head * ih, char * item)
59*4882a593Smuzhiyun {
60*4882a593Smuzhiyun     char filetime[30];
61*4882a593Smuzhiyun     time_t ttime;
62*4882a593Smuzhiyun 
63*4882a593Smuzhiyun     if (stat_data_v1 (ih)) {
64*4882a593Smuzhiyun 	struct stat_data_v1 * sd = (struct stat_data_v1 *)item;
65*4882a593Smuzhiyun 	ttime = sd_v1_mtime(sd);
66*4882a593Smuzhiyun 	ctime_r(&ttime, filetime);
67*4882a593Smuzhiyun 	printf ("%-10s %4hd %6d %6d %9d %24.24s",
68*4882a593Smuzhiyun 		 bb_mode_string(sd_v1_mode(sd)), sd_v1_nlink(sd),sd_v1_uid(sd), sd_v1_gid(sd),
69*4882a593Smuzhiyun 		 sd_v1_size(sd), filetime);
70*4882a593Smuzhiyun     } else {
71*4882a593Smuzhiyun 	struct stat_data * sd = (struct stat_data *)item;
72*4882a593Smuzhiyun 	ttime = sd_v2_mtime(sd);
73*4882a593Smuzhiyun 	ctime_r(&ttime, filetime);
74*4882a593Smuzhiyun 	printf ("%-10s %4d %6d %6d %9d %24.24s",
75*4882a593Smuzhiyun 		 bb_mode_string(sd_v2_mode(sd)), sd_v2_nlink(sd),sd_v2_uid(sd),sd_v2_gid(sd),
76*4882a593Smuzhiyun 		 (__u32) sd_v2_size(sd), filetime);
77*4882a593Smuzhiyun     }
78*4882a593Smuzhiyun }
79*4882a593Smuzhiyun 
80*4882a593Smuzhiyun static int
journal_read(int block,int len,char * buffer)81*4882a593Smuzhiyun journal_read (int block, int len, char *buffer)
82*4882a593Smuzhiyun {
83*4882a593Smuzhiyun   return reiserfs_devread ((INFO->journal_block + block) << INFO->blocksize_shift,
84*4882a593Smuzhiyun 			   0, len, buffer);
85*4882a593Smuzhiyun }
86*4882a593Smuzhiyun 
87*4882a593Smuzhiyun /* Read a block from ReiserFS file system, taking the journal into
88*4882a593Smuzhiyun  * account.  If the block nr is in the journal, the block from the
89*4882a593Smuzhiyun  * journal taken.
90*4882a593Smuzhiyun  */
91*4882a593Smuzhiyun static int
block_read(unsigned int blockNr,int start,int len,char * buffer)92*4882a593Smuzhiyun block_read (unsigned int blockNr, int start, int len, char *buffer)
93*4882a593Smuzhiyun {
94*4882a593Smuzhiyun   int transactions = INFO->journal_transactions;
95*4882a593Smuzhiyun   int desc_block = INFO->journal_first_desc;
96*4882a593Smuzhiyun   int journal_mask = INFO->journal_block_count - 1;
97*4882a593Smuzhiyun   int translatedNr = blockNr;
98*4882a593Smuzhiyun   __u32 *journal_table = JOURNAL_START;
99*4882a593Smuzhiyun   while (transactions-- > 0)
100*4882a593Smuzhiyun     {
101*4882a593Smuzhiyun       int i = 0;
102*4882a593Smuzhiyun       int j_len;
103*4882a593Smuzhiyun       if (__le32_to_cpu(*journal_table) != 0xffffffff)
104*4882a593Smuzhiyun 	{
105*4882a593Smuzhiyun 	  /* Search for the blockNr in cached journal */
106*4882a593Smuzhiyun 	  j_len = __le32_to_cpu(*journal_table++);
107*4882a593Smuzhiyun 	  while (i++ < j_len)
108*4882a593Smuzhiyun 	    {
109*4882a593Smuzhiyun 	      if (__le32_to_cpu(*journal_table++) == blockNr)
110*4882a593Smuzhiyun 		{
111*4882a593Smuzhiyun 		  journal_table += j_len - i;
112*4882a593Smuzhiyun 		  goto found;
113*4882a593Smuzhiyun 		}
114*4882a593Smuzhiyun 	    }
115*4882a593Smuzhiyun 	}
116*4882a593Smuzhiyun       else
117*4882a593Smuzhiyun 	{
118*4882a593Smuzhiyun 	  /* This is the end of cached journal marker.  The remaining
119*4882a593Smuzhiyun 	   * transactions are still on disk.
120*4882a593Smuzhiyun 	   */
121*4882a593Smuzhiyun 	  struct reiserfs_journal_desc   desc;
122*4882a593Smuzhiyun 	  struct reiserfs_journal_commit commit;
123*4882a593Smuzhiyun 
124*4882a593Smuzhiyun 	  if (! journal_read (desc_block, sizeof (desc), (char *) &desc))
125*4882a593Smuzhiyun 	    return 0;
126*4882a593Smuzhiyun 
127*4882a593Smuzhiyun 	  j_len = __le32_to_cpu(desc.j_len);
128*4882a593Smuzhiyun 	  while (i < j_len && i < JOURNAL_TRANS_HALF)
129*4882a593Smuzhiyun 	    if (__le32_to_cpu(desc.j_realblock[i++]) == blockNr)
130*4882a593Smuzhiyun 	      goto found;
131*4882a593Smuzhiyun 
132*4882a593Smuzhiyun 	  if (j_len >= JOURNAL_TRANS_HALF)
133*4882a593Smuzhiyun 	    {
134*4882a593Smuzhiyun 	      int commit_block = (desc_block + 1 + j_len) & journal_mask;
135*4882a593Smuzhiyun 	      if (! journal_read (commit_block,
136*4882a593Smuzhiyun 				  sizeof (commit), (char *) &commit))
137*4882a593Smuzhiyun 		return 0;
138*4882a593Smuzhiyun 	      while (i < j_len)
139*4882a593Smuzhiyun 		if (__le32_to_cpu(commit.j_realblock[i++ - JOURNAL_TRANS_HALF]) == blockNr)
140*4882a593Smuzhiyun 		  goto found;
141*4882a593Smuzhiyun 	    }
142*4882a593Smuzhiyun 	}
143*4882a593Smuzhiyun       goto not_found;
144*4882a593Smuzhiyun 
145*4882a593Smuzhiyun     found:
146*4882a593Smuzhiyun       translatedNr = INFO->journal_block + ((desc_block + i) & journal_mask);
147*4882a593Smuzhiyun #ifdef REISERDEBUG
148*4882a593Smuzhiyun       printf ("block_read: block %d is mapped to journal block %d.\n",
149*4882a593Smuzhiyun 	      blockNr, translatedNr - INFO->journal_block);
150*4882a593Smuzhiyun #endif
151*4882a593Smuzhiyun       /* We must continue the search, as this block may be overwritten
152*4882a593Smuzhiyun        * in later transactions.
153*4882a593Smuzhiyun        */
154*4882a593Smuzhiyun     not_found:
155*4882a593Smuzhiyun       desc_block = (desc_block + 2 + j_len) & journal_mask;
156*4882a593Smuzhiyun     }
157*4882a593Smuzhiyun   return reiserfs_devread (translatedNr << INFO->blocksize_shift, start, len, buffer);
158*4882a593Smuzhiyun }
159*4882a593Smuzhiyun 
160*4882a593Smuzhiyun /* Init the journal data structure.  We try to cache as much as
161*4882a593Smuzhiyun  * possible in the JOURNAL_START-JOURNAL_END space, but if it is full
162*4882a593Smuzhiyun  * we can still read the rest from the disk on demand.
163*4882a593Smuzhiyun  *
164*4882a593Smuzhiyun  * The first number of valid transactions and the descriptor block of the
165*4882a593Smuzhiyun  * first valid transaction are held in INFO.  The transactions are all
166*4882a593Smuzhiyun  * adjacent, but we must take care of the journal wrap around.
167*4882a593Smuzhiyun  */
168*4882a593Smuzhiyun static int
journal_init(void)169*4882a593Smuzhiyun journal_init (void)
170*4882a593Smuzhiyun {
171*4882a593Smuzhiyun   unsigned int block_count = INFO->journal_block_count;
172*4882a593Smuzhiyun   unsigned int desc_block;
173*4882a593Smuzhiyun   unsigned int commit_block;
174*4882a593Smuzhiyun   unsigned int next_trans_id;
175*4882a593Smuzhiyun   struct reiserfs_journal_header header;
176*4882a593Smuzhiyun   struct reiserfs_journal_desc   desc;
177*4882a593Smuzhiyun   struct reiserfs_journal_commit commit;
178*4882a593Smuzhiyun   __u32 *journal_table = JOURNAL_START;
179*4882a593Smuzhiyun 
180*4882a593Smuzhiyun   journal_read (block_count, sizeof (header), (char *) &header);
181*4882a593Smuzhiyun   desc_block = __le32_to_cpu(header.j_first_unflushed_offset);
182*4882a593Smuzhiyun   if (desc_block >= block_count)
183*4882a593Smuzhiyun     return 0;
184*4882a593Smuzhiyun 
185*4882a593Smuzhiyun   INFO->journal_first_desc = desc_block;
186*4882a593Smuzhiyun   next_trans_id = __le32_to_cpu(header.j_last_flush_trans_id) + 1;
187*4882a593Smuzhiyun 
188*4882a593Smuzhiyun #ifdef REISERDEBUG
189*4882a593Smuzhiyun   printf ("journal_init: last flushed %d\n",
190*4882a593Smuzhiyun 	  __le32_to_cpu(header.j_last_flush_trans_id));
191*4882a593Smuzhiyun #endif
192*4882a593Smuzhiyun 
193*4882a593Smuzhiyun   while (1)
194*4882a593Smuzhiyun     {
195*4882a593Smuzhiyun       journal_read (desc_block, sizeof (desc), (char *) &desc);
196*4882a593Smuzhiyun       if (substring (JOURNAL_DESC_MAGIC, desc.j_magic) > 0
197*4882a593Smuzhiyun 	  || __le32_to_cpu(desc.j_trans_id) != next_trans_id
198*4882a593Smuzhiyun 	  || __le32_to_cpu(desc.j_mount_id) != __le32_to_cpu(header.j_mount_id))
199*4882a593Smuzhiyun 	/* no more valid transactions */
200*4882a593Smuzhiyun 	break;
201*4882a593Smuzhiyun 
202*4882a593Smuzhiyun       commit_block = (desc_block + __le32_to_cpu(desc.j_len) + 1) & (block_count - 1);
203*4882a593Smuzhiyun       journal_read (commit_block, sizeof (commit), (char *) &commit);
204*4882a593Smuzhiyun       if (__le32_to_cpu(desc.j_trans_id) != commit.j_trans_id
205*4882a593Smuzhiyun 	  || __le32_to_cpu(desc.j_len) != __le32_to_cpu(commit.j_len))
206*4882a593Smuzhiyun 	/* no more valid transactions */
207*4882a593Smuzhiyun 	break;
208*4882a593Smuzhiyun 
209*4882a593Smuzhiyun #ifdef REISERDEBUG
210*4882a593Smuzhiyun       printf ("Found valid transaction %d/%d at %d.\n",
211*4882a593Smuzhiyun 	      __le32_to_cpu(desc.j_trans_id), __le32_to_cpu(desc.j_mount_id), desc_block);
212*4882a593Smuzhiyun #endif
213*4882a593Smuzhiyun 
214*4882a593Smuzhiyun       next_trans_id++;
215*4882a593Smuzhiyun       if (journal_table < JOURNAL_END)
216*4882a593Smuzhiyun 	{
217*4882a593Smuzhiyun 	  if ((journal_table + 1 + __le32_to_cpu(desc.j_len)) >= JOURNAL_END)
218*4882a593Smuzhiyun 	    {
219*4882a593Smuzhiyun 	      /* The table is almost full; mark the end of the cached
220*4882a593Smuzhiyun 	       * journal.*/
221*4882a593Smuzhiyun 	      *journal_table = __cpu_to_le32(0xffffffff);
222*4882a593Smuzhiyun 	      journal_table = JOURNAL_END;
223*4882a593Smuzhiyun 	    }
224*4882a593Smuzhiyun 	  else
225*4882a593Smuzhiyun 	    {
226*4882a593Smuzhiyun 	      unsigned int i;
227*4882a593Smuzhiyun 	      /* Cache the length and the realblock numbers in the table.
228*4882a593Smuzhiyun 	       * The block number of descriptor can easily be computed.
229*4882a593Smuzhiyun 	       * and need not to be stored here.
230*4882a593Smuzhiyun 	       */
231*4882a593Smuzhiyun 
232*4882a593Smuzhiyun 	      /* both are in the little endian format */
233*4882a593Smuzhiyun 	      *journal_table++ = desc.j_len;
234*4882a593Smuzhiyun 	      for (i = 0; i < __le32_to_cpu(desc.j_len) && i < JOURNAL_TRANS_HALF; i++)
235*4882a593Smuzhiyun 		{
236*4882a593Smuzhiyun 		  /* both are in the little endian format */
237*4882a593Smuzhiyun 		  *journal_table++ = desc.j_realblock[i];
238*4882a593Smuzhiyun #ifdef REISERDEBUG
239*4882a593Smuzhiyun 		  printf ("block %d is in journal %d.\n",
240*4882a593Smuzhiyun 			  __le32_to_cpu(desc.j_realblock[i]), desc_block);
241*4882a593Smuzhiyun #endif
242*4882a593Smuzhiyun 		}
243*4882a593Smuzhiyun 	      for (     ; i < __le32_to_cpu(desc.j_len); i++)
244*4882a593Smuzhiyun 		{
245*4882a593Smuzhiyun 		  /* both are in the little endian format */
246*4882a593Smuzhiyun 		  *journal_table++ = commit.j_realblock[i-JOURNAL_TRANS_HALF];
247*4882a593Smuzhiyun #ifdef REISERDEBUG
248*4882a593Smuzhiyun 		  printf ("block %d is in journal %d.\n",
249*4882a593Smuzhiyun 			  __le32_to_cpu(commit.j_realblock[i-JOURNAL_TRANS_HALF]),
250*4882a593Smuzhiyun 			  desc_block);
251*4882a593Smuzhiyun #endif
252*4882a593Smuzhiyun 		}
253*4882a593Smuzhiyun 	    }
254*4882a593Smuzhiyun 	}
255*4882a593Smuzhiyun       desc_block = (commit_block + 1) & (block_count - 1);
256*4882a593Smuzhiyun     }
257*4882a593Smuzhiyun #ifdef REISERDEBUG
258*4882a593Smuzhiyun   printf ("Transaction %d/%d at %d isn't valid.\n",
259*4882a593Smuzhiyun 	  __le32_to_cpu(desc.j_trans_id), __le32_to_cpu(desc.j_mount_id), desc_block);
260*4882a593Smuzhiyun #endif
261*4882a593Smuzhiyun 
262*4882a593Smuzhiyun   INFO->journal_transactions
263*4882a593Smuzhiyun     = next_trans_id - __le32_to_cpu(header.j_last_flush_trans_id) - 1;
264*4882a593Smuzhiyun   return errnum == 0;
265*4882a593Smuzhiyun }
266*4882a593Smuzhiyun 
267*4882a593Smuzhiyun /* check filesystem types and read superblock into memory buffer */
268*4882a593Smuzhiyun int
reiserfs_mount(unsigned part_length)269*4882a593Smuzhiyun reiserfs_mount (unsigned part_length)
270*4882a593Smuzhiyun {
271*4882a593Smuzhiyun   struct reiserfs_super_block super;
272*4882a593Smuzhiyun   int superblock = REISERFS_DISK_OFFSET_IN_BYTES >> SECTOR_BITS;
273*4882a593Smuzhiyun   char *cache;
274*4882a593Smuzhiyun 
275*4882a593Smuzhiyun   if (part_length < superblock + (sizeof (super) >> SECTOR_BITS)
276*4882a593Smuzhiyun       || ! reiserfs_devread (superblock, 0, sizeof (struct reiserfs_super_block),
277*4882a593Smuzhiyun 			     (char *) &super)
278*4882a593Smuzhiyun       || (substring (REISER3FS_SUPER_MAGIC_STRING, super.s_magic) > 0
279*4882a593Smuzhiyun 	  && substring (REISER2FS_SUPER_MAGIC_STRING, super.s_magic) > 0
280*4882a593Smuzhiyun 	  && substring (REISERFS_SUPER_MAGIC_STRING, super.s_magic) > 0)
281*4882a593Smuzhiyun       || (/* check that this is not a copy inside the journal log */
282*4882a593Smuzhiyun 	  sb_journal_block(&super) * sb_blocksize(&super)
283*4882a593Smuzhiyun 	  <= REISERFS_DISK_OFFSET_IN_BYTES))
284*4882a593Smuzhiyun     {
285*4882a593Smuzhiyun       /* Try old super block position */
286*4882a593Smuzhiyun       superblock = REISERFS_OLD_DISK_OFFSET_IN_BYTES >> SECTOR_BITS;
287*4882a593Smuzhiyun       if (part_length < superblock + (sizeof (super) >> SECTOR_BITS)
288*4882a593Smuzhiyun 	  || ! reiserfs_devread (superblock, 0, sizeof (struct reiserfs_super_block),
289*4882a593Smuzhiyun 				 (char *) &super))
290*4882a593Smuzhiyun 	return 0;
291*4882a593Smuzhiyun 
292*4882a593Smuzhiyun       if (substring (REISER2FS_SUPER_MAGIC_STRING, super.s_magic) > 0
293*4882a593Smuzhiyun 	  && substring (REISERFS_SUPER_MAGIC_STRING, super.s_magic) > 0)
294*4882a593Smuzhiyun 	{
295*4882a593Smuzhiyun 	  /* pre journaling super block ? */
296*4882a593Smuzhiyun 	  if (substring (REISERFS_SUPER_MAGIC_STRING,
297*4882a593Smuzhiyun 			 (char*) ((int) &super + 20)) > 0)
298*4882a593Smuzhiyun 	    return 0;
299*4882a593Smuzhiyun 
300*4882a593Smuzhiyun 	  set_sb_blocksize(&super, REISERFS_OLD_BLOCKSIZE);
301*4882a593Smuzhiyun 	  set_sb_journal_block(&super, 0);
302*4882a593Smuzhiyun 	  set_sb_version(&super, 0);
303*4882a593Smuzhiyun 	}
304*4882a593Smuzhiyun     }
305*4882a593Smuzhiyun 
306*4882a593Smuzhiyun   /* check the version number.  */
307*4882a593Smuzhiyun   if (sb_version(&super) > REISERFS_MAX_SUPPORTED_VERSION)
308*4882a593Smuzhiyun     return 0;
309*4882a593Smuzhiyun 
310*4882a593Smuzhiyun   INFO->version = sb_version(&super);
311*4882a593Smuzhiyun   INFO->blocksize = sb_blocksize(&super);
312*4882a593Smuzhiyun   INFO->fullblocksize_shift = log2 (sb_blocksize(&super));
313*4882a593Smuzhiyun   INFO->blocksize_shift = INFO->fullblocksize_shift - SECTOR_BITS;
314*4882a593Smuzhiyun   INFO->cached_slots =
315*4882a593Smuzhiyun     (FSYSREISER_CACHE_SIZE >> INFO->fullblocksize_shift) - 1;
316*4882a593Smuzhiyun 
317*4882a593Smuzhiyun #ifdef REISERDEBUG
318*4882a593Smuzhiyun   printf ("reiserfs_mount: version=%d, blocksize=%d\n",
319*4882a593Smuzhiyun 	  INFO->version, INFO->blocksize);
320*4882a593Smuzhiyun #endif /* REISERDEBUG */
321*4882a593Smuzhiyun 
322*4882a593Smuzhiyun   /* Clear node cache. */
323*4882a593Smuzhiyun   memset (INFO->blocks, 0, sizeof (INFO->blocks));
324*4882a593Smuzhiyun 
325*4882a593Smuzhiyun   if (sb_blocksize(&super) < FSYSREISER_MIN_BLOCKSIZE
326*4882a593Smuzhiyun       || sb_blocksize(&super) > FSYSREISER_MAX_BLOCKSIZE
327*4882a593Smuzhiyun       || (SECTOR_SIZE << INFO->blocksize_shift) != sb_blocksize(&super))
328*4882a593Smuzhiyun     return 0;
329*4882a593Smuzhiyun 
330*4882a593Smuzhiyun   /* Initialize journal code.  If something fails we end with zero
331*4882a593Smuzhiyun    * journal_transactions, so we don't access the journal at all.
332*4882a593Smuzhiyun    */
333*4882a593Smuzhiyun   INFO->journal_transactions = 0;
334*4882a593Smuzhiyun   if (sb_journal_block(&super) != 0 && super.s_journal_dev == 0)
335*4882a593Smuzhiyun     {
336*4882a593Smuzhiyun       INFO->journal_block = sb_journal_block(&super);
337*4882a593Smuzhiyun       INFO->journal_block_count = sb_journal_size(&super);
338*4882a593Smuzhiyun       if (is_power_of_two (INFO->journal_block_count))
339*4882a593Smuzhiyun 	journal_init ();
340*4882a593Smuzhiyun 
341*4882a593Smuzhiyun       /* Read in super block again, maybe it is in the journal */
342*4882a593Smuzhiyun       block_read (superblock >> INFO->blocksize_shift,
343*4882a593Smuzhiyun 		  0, sizeof (struct reiserfs_super_block), (char *) &super);
344*4882a593Smuzhiyun     }
345*4882a593Smuzhiyun 
346*4882a593Smuzhiyun   if (! block_read (sb_root_block(&super), 0, INFO->blocksize, (char*) ROOT))
347*4882a593Smuzhiyun     return 0;
348*4882a593Smuzhiyun 
349*4882a593Smuzhiyun   cache = ROOT;
350*4882a593Smuzhiyun   INFO->tree_depth = __le16_to_cpu(BLOCKHEAD (cache)->blk_level);
351*4882a593Smuzhiyun 
352*4882a593Smuzhiyun #ifdef REISERDEBUG
353*4882a593Smuzhiyun   printf ("root read_in: block=%d, depth=%d\n",
354*4882a593Smuzhiyun 	  sb_root_block(&super), INFO->tree_depth);
355*4882a593Smuzhiyun #endif /* REISERDEBUG */
356*4882a593Smuzhiyun 
357*4882a593Smuzhiyun   if (INFO->tree_depth >= MAX_HEIGHT)
358*4882a593Smuzhiyun     return 0;
359*4882a593Smuzhiyun   if (INFO->tree_depth == DISK_LEAF_NODE_LEVEL)
360*4882a593Smuzhiyun     {
361*4882a593Smuzhiyun       /* There is only one node in the whole filesystem,
362*4882a593Smuzhiyun        * which is simultanously leaf and root */
363*4882a593Smuzhiyun       memcpy (LEAF, ROOT, INFO->blocksize);
364*4882a593Smuzhiyun     }
365*4882a593Smuzhiyun   return 1;
366*4882a593Smuzhiyun }
367*4882a593Smuzhiyun 
368*4882a593Smuzhiyun /***************** TREE ACCESSING METHODS *****************************/
369*4882a593Smuzhiyun 
370*4882a593Smuzhiyun /* I assume you are familiar with the ReiserFS tree, if not go to
371*4882a593Smuzhiyun  * http://www.namesys.com/content_table.html
372*4882a593Smuzhiyun  *
373*4882a593Smuzhiyun  * My tree node cache is organized as following
374*4882a593Smuzhiyun  *   0   ROOT node
375*4882a593Smuzhiyun  *   1   LEAF node  (if the ROOT is also a LEAF it is copied here
376*4882a593Smuzhiyun  *   2-n other nodes on current path from bottom to top.
377*4882a593Smuzhiyun  *       if there is not enough space in the cache, the top most are
378*4882a593Smuzhiyun  *       omitted.
379*4882a593Smuzhiyun  *
380*4882a593Smuzhiyun  * I have only two methods to find a key in the tree:
381*4882a593Smuzhiyun  *   search_stat(dir_id, objectid) searches for the stat entry (always
382*4882a593Smuzhiyun  *       the first entry) of an object.
383*4882a593Smuzhiyun  *   next_key() gets the next key in tree order.
384*4882a593Smuzhiyun  *
385*4882a593Smuzhiyun  * This means, that I can only sequential reads of files are
386*4882a593Smuzhiyun  * efficient, but this really doesn't hurt for grub.
387*4882a593Smuzhiyun  */
388*4882a593Smuzhiyun 
389*4882a593Smuzhiyun /* Read in the node at the current path and depth into the node cache.
390*4882a593Smuzhiyun  * You must set INFO->blocks[depth] before.
391*4882a593Smuzhiyun  */
392*4882a593Smuzhiyun static char *
read_tree_node(unsigned int blockNr,int depth)393*4882a593Smuzhiyun read_tree_node (unsigned int blockNr, int depth)
394*4882a593Smuzhiyun {
395*4882a593Smuzhiyun   char* cache = CACHE(depth);
396*4882a593Smuzhiyun   int num_cached = INFO->cached_slots;
397*4882a593Smuzhiyun   if (depth < num_cached)
398*4882a593Smuzhiyun     {
399*4882a593Smuzhiyun       /* This is the cached part of the path.  Check if same block is
400*4882a593Smuzhiyun        * needed.
401*4882a593Smuzhiyun        */
402*4882a593Smuzhiyun       if (blockNr == INFO->blocks[depth])
403*4882a593Smuzhiyun 	return cache;
404*4882a593Smuzhiyun     }
405*4882a593Smuzhiyun   else
406*4882a593Smuzhiyun     cache = CACHE(num_cached);
407*4882a593Smuzhiyun 
408*4882a593Smuzhiyun #ifdef REISERDEBUG
409*4882a593Smuzhiyun   printf ("  next read_in: block=%d (depth=%d)\n",
410*4882a593Smuzhiyun 	  blockNr, depth);
411*4882a593Smuzhiyun #endif /* REISERDEBUG */
412*4882a593Smuzhiyun   if (! block_read (blockNr, 0, INFO->blocksize, cache))
413*4882a593Smuzhiyun     return 0;
414*4882a593Smuzhiyun   /* Make sure it has the right node level */
415*4882a593Smuzhiyun   if (__le16_to_cpu(BLOCKHEAD (cache)->blk_level) != depth)
416*4882a593Smuzhiyun     {
417*4882a593Smuzhiyun       errnum = ERR_FSYS_CORRUPT;
418*4882a593Smuzhiyun       return 0;
419*4882a593Smuzhiyun     }
420*4882a593Smuzhiyun 
421*4882a593Smuzhiyun   INFO->blocks[depth] = blockNr;
422*4882a593Smuzhiyun   return cache;
423*4882a593Smuzhiyun }
424*4882a593Smuzhiyun 
425*4882a593Smuzhiyun /* Get the next key, i.e. the key following the last retrieved key in
426*4882a593Smuzhiyun  * tree order.  INFO->current_ih and
427*4882a593Smuzhiyun  * INFO->current_info are adapted accordingly.  */
428*4882a593Smuzhiyun static int
next_key(void)429*4882a593Smuzhiyun next_key (void)
430*4882a593Smuzhiyun {
431*4882a593Smuzhiyun   int depth;
432*4882a593Smuzhiyun   struct item_head *ih = INFO->current_ih + 1;
433*4882a593Smuzhiyun   char *cache;
434*4882a593Smuzhiyun 
435*4882a593Smuzhiyun #ifdef REISERDEBUG
436*4882a593Smuzhiyun   printf ("next_key:\n  old ih: key %d:%d:%d:%d version:%d\n",
437*4882a593Smuzhiyun 	  __le32_to_cpu(INFO->current_ih->ih_key.k_dir_id),
438*4882a593Smuzhiyun 	  __le32_to_cpu(INFO->current_ih->ih_key.k_objectid),
439*4882a593Smuzhiyun 	  __le32_to_cpu(INFO->current_ih->ih_key.u.v1.k_offset),
440*4882a593Smuzhiyun 	  __le32_to_cpu(INFO->current_ih->ih_key.u.v1.k_uniqueness),
441*4882a593Smuzhiyun 	  __le16_to_cpu(INFO->current_ih->ih_version));
442*4882a593Smuzhiyun #endif /* REISERDEBUG */
443*4882a593Smuzhiyun 
444*4882a593Smuzhiyun   if (ih == &ITEMHEAD[__le16_to_cpu(BLOCKHEAD (LEAF)->blk_nr_item)])
445*4882a593Smuzhiyun     {
446*4882a593Smuzhiyun       depth = DISK_LEAF_NODE_LEVEL;
447*4882a593Smuzhiyun       /* The last item, was the last in the leaf node.
448*4882a593Smuzhiyun        * Read in the next block
449*4882a593Smuzhiyun        */
450*4882a593Smuzhiyun       do
451*4882a593Smuzhiyun 	{
452*4882a593Smuzhiyun 	  if (depth == INFO->tree_depth)
453*4882a593Smuzhiyun 	    {
454*4882a593Smuzhiyun 	      /* There are no more keys at all.
455*4882a593Smuzhiyun 	       * Return a dummy item with MAX_KEY */
456*4882a593Smuzhiyun 	      ih = (struct item_head *) &BLOCKHEAD (LEAF)->blk_right_delim_key;
457*4882a593Smuzhiyun 	      goto found;
458*4882a593Smuzhiyun 	    }
459*4882a593Smuzhiyun 	  depth++;
460*4882a593Smuzhiyun #ifdef REISERDEBUG
461*4882a593Smuzhiyun 	  printf ("  depth=%d, i=%d\n", depth, INFO->next_key_nr[depth]);
462*4882a593Smuzhiyun #endif /* REISERDEBUG */
463*4882a593Smuzhiyun 	}
464*4882a593Smuzhiyun       while (INFO->next_key_nr[depth] == 0);
465*4882a593Smuzhiyun 
466*4882a593Smuzhiyun       if (depth == INFO->tree_depth)
467*4882a593Smuzhiyun 	cache = ROOT;
468*4882a593Smuzhiyun       else if (depth <= INFO->cached_slots)
469*4882a593Smuzhiyun 	cache = CACHE (depth);
470*4882a593Smuzhiyun       else
471*4882a593Smuzhiyun 	{
472*4882a593Smuzhiyun 	  cache = read_tree_node (INFO->blocks[depth], depth);
473*4882a593Smuzhiyun 	  if (! cache)
474*4882a593Smuzhiyun 	    return 0;
475*4882a593Smuzhiyun 	}
476*4882a593Smuzhiyun 
477*4882a593Smuzhiyun       do
478*4882a593Smuzhiyun 	{
479*4882a593Smuzhiyun 	  int nr_item = __le16_to_cpu(BLOCKHEAD (cache)->blk_nr_item);
480*4882a593Smuzhiyun 	  int key_nr = INFO->next_key_nr[depth]++;
481*4882a593Smuzhiyun #ifdef REISERDEBUG
482*4882a593Smuzhiyun 	  printf ("  depth=%d, i=%d/%d\n", depth, key_nr, nr_item);
483*4882a593Smuzhiyun #endif /* REISERDEBUG */
484*4882a593Smuzhiyun 	  if (key_nr == nr_item)
485*4882a593Smuzhiyun 	    /* This is the last item in this block, set the next_key_nr to 0 */
486*4882a593Smuzhiyun 	    INFO->next_key_nr[depth] = 0;
487*4882a593Smuzhiyun 
488*4882a593Smuzhiyun 	  cache = read_tree_node (dc_block_number(&(DC (cache)[key_nr])), --depth);
489*4882a593Smuzhiyun 	  if (! cache)
490*4882a593Smuzhiyun 	    return 0;
491*4882a593Smuzhiyun 	}
492*4882a593Smuzhiyun       while (depth > DISK_LEAF_NODE_LEVEL);
493*4882a593Smuzhiyun 
494*4882a593Smuzhiyun       ih = ITEMHEAD;
495*4882a593Smuzhiyun     }
496*4882a593Smuzhiyun  found:
497*4882a593Smuzhiyun   INFO->current_ih   = ih;
498*4882a593Smuzhiyun   INFO->current_item = &LEAF[__le16_to_cpu(ih->ih_item_location)];
499*4882a593Smuzhiyun #ifdef REISERDEBUG
500*4882a593Smuzhiyun   printf ("  new ih: key %d:%d:%d:%d version:%d\n",
501*4882a593Smuzhiyun 	  __le32_to_cpu(INFO->current_ih->ih_key.k_dir_id),
502*4882a593Smuzhiyun 	  __le32_to_cpu(INFO->current_ih->ih_key.k_objectid),
503*4882a593Smuzhiyun 	  __le32_to_cpu(INFO->current_ih->ih_key.u.v1.k_offset),
504*4882a593Smuzhiyun 	  __le32_to_cpu(INFO->current_ih->ih_key.u.v1.k_uniqueness),
505*4882a593Smuzhiyun 	  __le16_to_cpu(INFO->current_ih->ih_version));
506*4882a593Smuzhiyun #endif /* REISERDEBUG */
507*4882a593Smuzhiyun   return 1;
508*4882a593Smuzhiyun }
509*4882a593Smuzhiyun 
510*4882a593Smuzhiyun /* preconditions: reiserfs_mount already executed, therefore
511*4882a593Smuzhiyun  *   INFO block is valid
512*4882a593Smuzhiyun  * returns: 0 if error (errnum is set),
513*4882a593Smuzhiyun  *   nonzero iff we were able to find the key successfully.
514*4882a593Smuzhiyun  * postconditions: on a nonzero return, the current_ih and
515*4882a593Smuzhiyun  *   current_item fields describe the key that equals the
516*4882a593Smuzhiyun  *   searched key.  INFO->next_key contains the next key after
517*4882a593Smuzhiyun  *   the searched key.
518*4882a593Smuzhiyun  * side effects: messes around with the cache.
519*4882a593Smuzhiyun  */
520*4882a593Smuzhiyun static int
search_stat(__u32 dir_id,__u32 objectid)521*4882a593Smuzhiyun search_stat (__u32 dir_id, __u32 objectid)
522*4882a593Smuzhiyun {
523*4882a593Smuzhiyun   char *cache;
524*4882a593Smuzhiyun   int depth;
525*4882a593Smuzhiyun   int nr_item;
526*4882a593Smuzhiyun   int i;
527*4882a593Smuzhiyun   struct item_head *ih;
528*4882a593Smuzhiyun #ifdef REISERDEBUG
529*4882a593Smuzhiyun   printf ("search_stat:\n  key %d:%d:0:0\n", dir_id, objectid);
530*4882a593Smuzhiyun #endif /* REISERDEBUG */
531*4882a593Smuzhiyun 
532*4882a593Smuzhiyun   depth = INFO->tree_depth;
533*4882a593Smuzhiyun   cache = ROOT;
534*4882a593Smuzhiyun 
535*4882a593Smuzhiyun   while (depth > DISK_LEAF_NODE_LEVEL)
536*4882a593Smuzhiyun     {
537*4882a593Smuzhiyun       struct key *key;
538*4882a593Smuzhiyun       nr_item = __le16_to_cpu(BLOCKHEAD (cache)->blk_nr_item);
539*4882a593Smuzhiyun 
540*4882a593Smuzhiyun       key = KEY (cache);
541*4882a593Smuzhiyun 
542*4882a593Smuzhiyun       for (i = 0; i < nr_item; i++)
543*4882a593Smuzhiyun 	{
544*4882a593Smuzhiyun 	  if (__le32_to_cpu(key->k_dir_id) > dir_id
545*4882a593Smuzhiyun 	      || (__le32_to_cpu(key->k_dir_id) == dir_id
546*4882a593Smuzhiyun 		  && (__le32_to_cpu(key->k_objectid) > objectid
547*4882a593Smuzhiyun 		      || (__le32_to_cpu(key->k_objectid) == objectid
548*4882a593Smuzhiyun 			  && (__le32_to_cpu(key->u.v1.k_offset)
549*4882a593Smuzhiyun 			      | __le32_to_cpu(key->u.v1.k_uniqueness)) > 0))))
550*4882a593Smuzhiyun 	    break;
551*4882a593Smuzhiyun 	  key++;
552*4882a593Smuzhiyun 	}
553*4882a593Smuzhiyun 
554*4882a593Smuzhiyun #ifdef REISERDEBUG
555*4882a593Smuzhiyun       printf ("  depth=%d, i=%d/%d\n", depth, i, nr_item);
556*4882a593Smuzhiyun #endif /* REISERDEBUG */
557*4882a593Smuzhiyun       INFO->next_key_nr[depth] = (i == nr_item) ? 0 : i+1;
558*4882a593Smuzhiyun       cache = read_tree_node (dc_block_number(&(DC (cache)[i])), --depth);
559*4882a593Smuzhiyun       if (! cache)
560*4882a593Smuzhiyun 	return 0;
561*4882a593Smuzhiyun     }
562*4882a593Smuzhiyun 
563*4882a593Smuzhiyun   /* cache == LEAF */
564*4882a593Smuzhiyun   nr_item = __le16_to_cpu(BLOCKHEAD (LEAF)->blk_nr_item);
565*4882a593Smuzhiyun   ih = ITEMHEAD;
566*4882a593Smuzhiyun   for (i = 0; i < nr_item; i++)
567*4882a593Smuzhiyun     {
568*4882a593Smuzhiyun       if (__le32_to_cpu(ih->ih_key.k_dir_id) == dir_id
569*4882a593Smuzhiyun 	  && __le32_to_cpu(ih->ih_key.k_objectid) == objectid
570*4882a593Smuzhiyun 	  && __le32_to_cpu(ih->ih_key.u.v1.k_offset) == 0
571*4882a593Smuzhiyun 	  && __le32_to_cpu(ih->ih_key.u.v1.k_uniqueness) == 0)
572*4882a593Smuzhiyun 	{
573*4882a593Smuzhiyun #ifdef REISERDEBUG
574*4882a593Smuzhiyun 	  printf ("  depth=%d, i=%d/%d\n", depth, i, nr_item);
575*4882a593Smuzhiyun #endif /* REISERDEBUG */
576*4882a593Smuzhiyun 	  INFO->current_ih   = ih;
577*4882a593Smuzhiyun 	  INFO->current_item = &LEAF[__le16_to_cpu(ih->ih_item_location)];
578*4882a593Smuzhiyun 	  return 1;
579*4882a593Smuzhiyun 	}
580*4882a593Smuzhiyun       ih++;
581*4882a593Smuzhiyun     }
582*4882a593Smuzhiyun   errnum = ERR_FSYS_CORRUPT;
583*4882a593Smuzhiyun   return 0;
584*4882a593Smuzhiyun }
585*4882a593Smuzhiyun 
586*4882a593Smuzhiyun int
reiserfs_read(char * buf,unsigned len)587*4882a593Smuzhiyun reiserfs_read (char *buf, unsigned len)
588*4882a593Smuzhiyun {
589*4882a593Smuzhiyun   unsigned int blocksize;
590*4882a593Smuzhiyun   unsigned int offset;
591*4882a593Smuzhiyun   unsigned int to_read;
592*4882a593Smuzhiyun   char *prev_buf = buf;
593*4882a593Smuzhiyun 
594*4882a593Smuzhiyun #ifdef REISERDEBUG
595*4882a593Smuzhiyun   printf ("reiserfs_read: filepos=%d len=%d, offset=%Lx\n",
596*4882a593Smuzhiyun 	  filepos, len, (__u64) IH_KEY_OFFSET (INFO->current_ih) - 1);
597*4882a593Smuzhiyun #endif /* REISERDEBUG */
598*4882a593Smuzhiyun 
599*4882a593Smuzhiyun   if (__le32_to_cpu(INFO->current_ih->ih_key.k_objectid) != INFO->fileinfo.k_objectid
600*4882a593Smuzhiyun       || IH_KEY_OFFSET (INFO->current_ih) > filepos + 1)
601*4882a593Smuzhiyun     {
602*4882a593Smuzhiyun       search_stat (INFO->fileinfo.k_dir_id, INFO->fileinfo.k_objectid);
603*4882a593Smuzhiyun       goto get_next_key;
604*4882a593Smuzhiyun     }
605*4882a593Smuzhiyun 
606*4882a593Smuzhiyun   while (! errnum)
607*4882a593Smuzhiyun     {
608*4882a593Smuzhiyun       if (__le32_to_cpu(INFO->current_ih->ih_key.k_objectid) != INFO->fileinfo.k_objectid) {
609*4882a593Smuzhiyun 	break;
610*4882a593Smuzhiyun       }
611*4882a593Smuzhiyun 
612*4882a593Smuzhiyun       offset = filepos - IH_KEY_OFFSET (INFO->current_ih) + 1;
613*4882a593Smuzhiyun       blocksize = __le16_to_cpu(INFO->current_ih->ih_item_len);
614*4882a593Smuzhiyun 
615*4882a593Smuzhiyun #ifdef REISERDEBUG
616*4882a593Smuzhiyun       printf ("  loop: filepos=%d len=%d, offset=%d blocksize=%d\n",
617*4882a593Smuzhiyun 	      filepos, len, offset, blocksize);
618*4882a593Smuzhiyun #endif /* REISERDEBUG */
619*4882a593Smuzhiyun 
620*4882a593Smuzhiyun       if (IH_KEY_ISTYPE(INFO->current_ih, TYPE_DIRECT)
621*4882a593Smuzhiyun 	  && offset < blocksize)
622*4882a593Smuzhiyun 	{
623*4882a593Smuzhiyun #ifdef REISERDEBUG
624*4882a593Smuzhiyun 	  printf ("direct_read: offset=%d, blocksize=%d\n",
625*4882a593Smuzhiyun 		  offset, blocksize);
626*4882a593Smuzhiyun #endif /* REISERDEBUG */
627*4882a593Smuzhiyun 	  to_read = blocksize - offset;
628*4882a593Smuzhiyun 	  if (to_read > len)
629*4882a593Smuzhiyun 	    to_read = len;
630*4882a593Smuzhiyun 
631*4882a593Smuzhiyun 	  memcpy (buf, INFO->current_item + offset, to_read);
632*4882a593Smuzhiyun 	  goto update_buf_len;
633*4882a593Smuzhiyun 	}
634*4882a593Smuzhiyun       else if (IH_KEY_ISTYPE(INFO->current_ih, TYPE_INDIRECT))
635*4882a593Smuzhiyun 	{
636*4882a593Smuzhiyun 	  blocksize = (blocksize >> 2) << INFO->fullblocksize_shift;
637*4882a593Smuzhiyun #ifdef REISERDEBUG
638*4882a593Smuzhiyun 	  printf ("indirect_read: offset=%d, blocksize=%d\n",
639*4882a593Smuzhiyun 		  offset, blocksize);
640*4882a593Smuzhiyun #endif /* REISERDEBUG */
641*4882a593Smuzhiyun 
642*4882a593Smuzhiyun 	  while (offset < blocksize)
643*4882a593Smuzhiyun 	    {
644*4882a593Smuzhiyun 	      __u32 blocknr = __le32_to_cpu(((__u32 *) INFO->current_item)
645*4882a593Smuzhiyun 		[offset >> INFO->fullblocksize_shift]);
646*4882a593Smuzhiyun 	      int blk_offset = offset & (INFO->blocksize-1);
647*4882a593Smuzhiyun 	      to_read = INFO->blocksize - blk_offset;
648*4882a593Smuzhiyun 	      if (to_read > len)
649*4882a593Smuzhiyun 		to_read = len;
650*4882a593Smuzhiyun 
651*4882a593Smuzhiyun 	      /* Journal is only for meta data.  Data blocks can be read
652*4882a593Smuzhiyun 	       * directly without using block_read
653*4882a593Smuzhiyun 	       */
654*4882a593Smuzhiyun 	      reiserfs_devread (blocknr << INFO->blocksize_shift,
655*4882a593Smuzhiyun 				blk_offset, to_read, buf);
656*4882a593Smuzhiyun 	    update_buf_len:
657*4882a593Smuzhiyun 	      len -= to_read;
658*4882a593Smuzhiyun 	      buf += to_read;
659*4882a593Smuzhiyun 	      offset += to_read;
660*4882a593Smuzhiyun 	      filepos += to_read;
661*4882a593Smuzhiyun 	      if (len == 0)
662*4882a593Smuzhiyun 		goto done;
663*4882a593Smuzhiyun 	    }
664*4882a593Smuzhiyun 	}
665*4882a593Smuzhiyun     get_next_key:
666*4882a593Smuzhiyun       next_key ();
667*4882a593Smuzhiyun     }
668*4882a593Smuzhiyun  done:
669*4882a593Smuzhiyun   return errnum ? 0 : buf - prev_buf;
670*4882a593Smuzhiyun }
671*4882a593Smuzhiyun 
672*4882a593Smuzhiyun 
673*4882a593Smuzhiyun /* preconditions: reiserfs_mount already executed, therefore
674*4882a593Smuzhiyun  *   INFO block is valid
675*4882a593Smuzhiyun  * returns: 0 if error, nonzero iff we were able to find the file successfully
676*4882a593Smuzhiyun  * postconditions: on a nonzero return, INFO->fileinfo contains the info
677*4882a593Smuzhiyun  *   of the file we were trying to look up, filepos is 0 and filemax is
678*4882a593Smuzhiyun  *   the size of the file.
679*4882a593Smuzhiyun  */
680*4882a593Smuzhiyun static int
reiserfs_dir(char * dirname)681*4882a593Smuzhiyun reiserfs_dir (char *dirname)
682*4882a593Smuzhiyun {
683*4882a593Smuzhiyun   struct reiserfs_de_head *de_head;
684*4882a593Smuzhiyun   char *rest, ch;
685*4882a593Smuzhiyun   __u32 dir_id, objectid, parent_dir_id = 0, parent_objectid = 0;
686*4882a593Smuzhiyun #ifndef STAGE1_5
687*4882a593Smuzhiyun   int do_possibilities = 0;
688*4882a593Smuzhiyun #endif /* ! STAGE1_5 */
689*4882a593Smuzhiyun   char linkbuf[PATH_MAX];	/* buffer for following symbolic links */
690*4882a593Smuzhiyun   int link_count = 0;
691*4882a593Smuzhiyun   int mode;
692*4882a593Smuzhiyun 
693*4882a593Smuzhiyun   dir_id = REISERFS_ROOT_PARENT_OBJECTID;
694*4882a593Smuzhiyun   objectid = REISERFS_ROOT_OBJECTID;
695*4882a593Smuzhiyun 
696*4882a593Smuzhiyun   while (1)
697*4882a593Smuzhiyun     {
698*4882a593Smuzhiyun #ifdef REISERDEBUG
699*4882a593Smuzhiyun       printf ("dirname=%s\n", dirname);
700*4882a593Smuzhiyun #endif /* REISERDEBUG */
701*4882a593Smuzhiyun 
702*4882a593Smuzhiyun       /* Search for the stat info first. */
703*4882a593Smuzhiyun       if (! search_stat (dir_id, objectid))
704*4882a593Smuzhiyun 	return 0;
705*4882a593Smuzhiyun 
706*4882a593Smuzhiyun #ifdef REISERDEBUG
707*4882a593Smuzhiyun        printf ("sd_mode=%x sd_size=%d\n",
708*4882a593Smuzhiyun 	       stat_data_v1(INFO->current_ih) ? sd_v1_mode((struct stat_data_v1 *) INFO->current_item) :
709*4882a593Smuzhiyun 						sd_v2_mode((struct stat_data *) (INFO->current_item)),
710*4882a593Smuzhiyun 	       stat_data_v1(INFO->current_ih) ? sd_v1_size((struct stat_data_v1 *) INFO->current_item) :
711*4882a593Smuzhiyun 						sd_v2_size((struct stat_data *) INFO->current_item)
712*4882a593Smuzhiyun 	      );
713*4882a593Smuzhiyun 
714*4882a593Smuzhiyun #endif /* REISERDEBUG */
715*4882a593Smuzhiyun       mode = stat_data_v1(INFO->current_ih) ?
716*4882a593Smuzhiyun 	       sd_v1_mode((struct stat_data_v1 *) INFO->current_item) :
717*4882a593Smuzhiyun 	       sd_v2_mode((struct stat_data *) INFO->current_item);
718*4882a593Smuzhiyun 
719*4882a593Smuzhiyun       /* If we've got a symbolic link, then chase it. */
720*4882a593Smuzhiyun       if (S_ISLNK (mode))
721*4882a593Smuzhiyun 	{
722*4882a593Smuzhiyun 	  unsigned int len;
723*4882a593Smuzhiyun 	  if (++link_count > MAX_LINK_COUNT)
724*4882a593Smuzhiyun 	    {
725*4882a593Smuzhiyun 	      errnum = ERR_SYMLINK_LOOP;
726*4882a593Smuzhiyun 	      return 0;
727*4882a593Smuzhiyun 	    }
728*4882a593Smuzhiyun 
729*4882a593Smuzhiyun 	  /* Get the symlink size. */
730*4882a593Smuzhiyun 	  filemax = stat_data_v1(INFO->current_ih) ?
731*4882a593Smuzhiyun 		     sd_v1_size((struct stat_data_v1 *) INFO->current_item) :
732*4882a593Smuzhiyun 		     sd_v2_size((struct stat_data *) INFO->current_item);
733*4882a593Smuzhiyun 
734*4882a593Smuzhiyun 	  /* Find out how long our remaining name is. */
735*4882a593Smuzhiyun 	  len = 0;
736*4882a593Smuzhiyun 	  while (dirname[len] && !isspace (dirname[len]))
737*4882a593Smuzhiyun 	    len++;
738*4882a593Smuzhiyun 
739*4882a593Smuzhiyun 	  if (filemax + len > sizeof (linkbuf) - 1)
740*4882a593Smuzhiyun 	    {
741*4882a593Smuzhiyun 	      errnum = ERR_FILELENGTH;
742*4882a593Smuzhiyun 	      return 0;
743*4882a593Smuzhiyun 	    }
744*4882a593Smuzhiyun 
745*4882a593Smuzhiyun 	  /* Copy the remaining name to the end of the symlink data.
746*4882a593Smuzhiyun 	     Note that DIRNAME and LINKBUF may overlap! */
747*4882a593Smuzhiyun 	  memmove (linkbuf + filemax, dirname, len+1);
748*4882a593Smuzhiyun 
749*4882a593Smuzhiyun 	  INFO->fileinfo.k_dir_id = dir_id;
750*4882a593Smuzhiyun 	  INFO->fileinfo.k_objectid = objectid;
751*4882a593Smuzhiyun 	  filepos = 0;
752*4882a593Smuzhiyun 	  if (! next_key ()
753*4882a593Smuzhiyun 	      || reiserfs_read (linkbuf, filemax) != filemax)
754*4882a593Smuzhiyun 	    {
755*4882a593Smuzhiyun 	      if (! errnum)
756*4882a593Smuzhiyun 		errnum = ERR_FSYS_CORRUPT;
757*4882a593Smuzhiyun 	      return 0;
758*4882a593Smuzhiyun 	    }
759*4882a593Smuzhiyun 
760*4882a593Smuzhiyun #ifdef REISERDEBUG
761*4882a593Smuzhiyun 	  printf ("symlink=%s\n", linkbuf);
762*4882a593Smuzhiyun #endif /* REISERDEBUG */
763*4882a593Smuzhiyun 
764*4882a593Smuzhiyun 	  dirname = linkbuf;
765*4882a593Smuzhiyun 	  if (*dirname == '/')
766*4882a593Smuzhiyun 	    {
767*4882a593Smuzhiyun 	      /* It's an absolute link, so look it up in root. */
768*4882a593Smuzhiyun 	      dir_id = REISERFS_ROOT_PARENT_OBJECTID;
769*4882a593Smuzhiyun 	      objectid = REISERFS_ROOT_OBJECTID;
770*4882a593Smuzhiyun 	    }
771*4882a593Smuzhiyun 	  else
772*4882a593Smuzhiyun 	    {
773*4882a593Smuzhiyun 	      /* Relative, so look it up in our parent directory. */
774*4882a593Smuzhiyun 	      dir_id   = parent_dir_id;
775*4882a593Smuzhiyun 	      objectid = parent_objectid;
776*4882a593Smuzhiyun 	    }
777*4882a593Smuzhiyun 
778*4882a593Smuzhiyun 	  /* Now lookup the new name. */
779*4882a593Smuzhiyun 	  continue;
780*4882a593Smuzhiyun 	}
781*4882a593Smuzhiyun 
782*4882a593Smuzhiyun       /* if we have a real file (and we're not just printing possibilities),
783*4882a593Smuzhiyun 	 then this is where we want to exit */
784*4882a593Smuzhiyun 
785*4882a593Smuzhiyun       if (! *dirname || isspace (*dirname))
786*4882a593Smuzhiyun 	{
787*4882a593Smuzhiyun 	  if (! S_ISREG (mode))
788*4882a593Smuzhiyun 	    {
789*4882a593Smuzhiyun 	      errnum = ERR_BAD_FILETYPE;
790*4882a593Smuzhiyun 	      return 0;
791*4882a593Smuzhiyun 	    }
792*4882a593Smuzhiyun 
793*4882a593Smuzhiyun 	  filepos = 0;
794*4882a593Smuzhiyun 	  filemax = stat_data_v1(INFO->current_ih) ?
795*4882a593Smuzhiyun 		      sd_v1_size((struct stat_data_v1 *) INFO->current_item) :
796*4882a593Smuzhiyun 		      sd_v2_size((struct stat_data *) INFO->current_item);
797*4882a593Smuzhiyun #if 0
798*4882a593Smuzhiyun 	  /* If this is a new stat data and size is > 4GB set filemax to
799*4882a593Smuzhiyun 	   * maximum
800*4882a593Smuzhiyun 	   */
801*4882a593Smuzhiyun 	  if (__le16_to_cpu(INFO->current_ih->ih_version) == ITEM_VERSION_2
802*4882a593Smuzhiyun 	      && sd_size_hi((struct stat_data *) INFO->current_item) > 0)
803*4882a593Smuzhiyun 	    filemax = 0xffffffff;
804*4882a593Smuzhiyun #endif
805*4882a593Smuzhiyun 	  INFO->fileinfo.k_dir_id = dir_id;
806*4882a593Smuzhiyun 	  INFO->fileinfo.k_objectid = objectid;
807*4882a593Smuzhiyun 	  return next_key ();
808*4882a593Smuzhiyun 	}
809*4882a593Smuzhiyun 
810*4882a593Smuzhiyun       /* continue with the file/directory name interpretation */
811*4882a593Smuzhiyun       while (*dirname == '/')
812*4882a593Smuzhiyun 	dirname++;
813*4882a593Smuzhiyun       if (! S_ISDIR (mode))
814*4882a593Smuzhiyun 	{
815*4882a593Smuzhiyun 	  errnum = ERR_BAD_FILETYPE;
816*4882a593Smuzhiyun 	  return 0;
817*4882a593Smuzhiyun 	}
818*4882a593Smuzhiyun       for (rest = dirname; (ch = *rest) && ! isspace (ch) && ch != '/'; rest++);
819*4882a593Smuzhiyun       *rest = 0;
820*4882a593Smuzhiyun 
821*4882a593Smuzhiyun # ifndef STAGE1_5
822*4882a593Smuzhiyun       if (print_possibilities && ch != '/')
823*4882a593Smuzhiyun 	do_possibilities = 1;
824*4882a593Smuzhiyun # endif /* ! STAGE1_5 */
825*4882a593Smuzhiyun 
826*4882a593Smuzhiyun       while (1)
827*4882a593Smuzhiyun 	{
828*4882a593Smuzhiyun 	  char *name_end;
829*4882a593Smuzhiyun 	  int num_entries;
830*4882a593Smuzhiyun 
831*4882a593Smuzhiyun 	  if (! next_key ())
832*4882a593Smuzhiyun 	    return 0;
833*4882a593Smuzhiyun #ifdef REISERDEBUG
834*4882a593Smuzhiyun 	  printf ("ih: key %d:%d:%d:%d version:%d\n",
835*4882a593Smuzhiyun 		  __le32_to_cpu(INFO->current_ih->ih_key.k_dir_id),
836*4882a593Smuzhiyun 		  __le32_to_cpu(INFO->current_ih->ih_key.k_objectid),
837*4882a593Smuzhiyun 		  __le32_to_cpu(INFO->current_ih->ih_key.u.v1.k_offset),
838*4882a593Smuzhiyun 		  __le32_to_cpu(INFO->current_ih->ih_key.u.v1.k_uniqueness),
839*4882a593Smuzhiyun 		  __le16_to_cpu(INFO->current_ih->ih_version));
840*4882a593Smuzhiyun #endif /* REISERDEBUG */
841*4882a593Smuzhiyun 
842*4882a593Smuzhiyun 	  if (__le32_to_cpu(INFO->current_ih->ih_key.k_objectid) != objectid)
843*4882a593Smuzhiyun 	    break;
844*4882a593Smuzhiyun 
845*4882a593Smuzhiyun 	  name_end = INFO->current_item + __le16_to_cpu(INFO->current_ih->ih_item_len);
846*4882a593Smuzhiyun 	  de_head = (struct reiserfs_de_head *) INFO->current_item;
847*4882a593Smuzhiyun 	  num_entries = __le16_to_cpu(INFO->current_ih->u.ih_entry_count);
848*4882a593Smuzhiyun 	  while (num_entries > 0)
849*4882a593Smuzhiyun 	    {
850*4882a593Smuzhiyun 	      char *filename = INFO->current_item + deh_location(de_head);
851*4882a593Smuzhiyun 	      char  tmp = *name_end;
852*4882a593Smuzhiyun 	      if ((deh_state(de_head) & DEH_Visible))
853*4882a593Smuzhiyun 		{
854*4882a593Smuzhiyun 		  int cmp;
855*4882a593Smuzhiyun 		  /* Directory names in ReiserFS are not null
856*4882a593Smuzhiyun 		   * terminated.  We write a temporary 0 behind it.
857*4882a593Smuzhiyun 		   * NOTE: that this may overwrite the first block in
858*4882a593Smuzhiyun 		   * the tree cache.  That doesn't hurt as long as we
859*4882a593Smuzhiyun 		   * don't call next_key () in between.
860*4882a593Smuzhiyun 		   */
861*4882a593Smuzhiyun 		  *name_end = 0;
862*4882a593Smuzhiyun 		  cmp = substring (dirname, filename);
863*4882a593Smuzhiyun 		  *name_end = tmp;
864*4882a593Smuzhiyun # ifndef STAGE1_5
865*4882a593Smuzhiyun 		  if (do_possibilities)
866*4882a593Smuzhiyun 		    {
867*4882a593Smuzhiyun 		      if (cmp <= 0)
868*4882a593Smuzhiyun 			{
869*4882a593Smuzhiyun 			  char fn[PATH_MAX];
870*4882a593Smuzhiyun 			  struct fsys_reiser_info info_save;
871*4882a593Smuzhiyun 
872*4882a593Smuzhiyun 			  if (print_possibilities > 0)
873*4882a593Smuzhiyun 			    print_possibilities = -print_possibilities;
874*4882a593Smuzhiyun 			  *name_end = 0;
875*4882a593Smuzhiyun 			  strcpy(fn, filename);
876*4882a593Smuzhiyun 			  *name_end = tmp;
877*4882a593Smuzhiyun 
878*4882a593Smuzhiyun 			  /* If NAME is "." or "..", do not count it.  */
879*4882a593Smuzhiyun 			  if (strcmp (fn, ".") != 0 && strcmp (fn, "..") != 0) {
880*4882a593Smuzhiyun 			    memcpy(&info_save, INFO, sizeof(struct fsys_reiser_info));
881*4882a593Smuzhiyun 			    search_stat (deh_dir_id(de_head), deh_objectid(de_head));
882*4882a593Smuzhiyun 			    sd_print_item(INFO->current_ih, INFO->current_item);
883*4882a593Smuzhiyun 			    printf(" %s\n", fn);
884*4882a593Smuzhiyun 			    search_stat (dir_id, objectid);
885*4882a593Smuzhiyun 			    memcpy(INFO, &info_save, sizeof(struct fsys_reiser_info));
886*4882a593Smuzhiyun 			  }
887*4882a593Smuzhiyun 			}
888*4882a593Smuzhiyun 		    }
889*4882a593Smuzhiyun 		  else
890*4882a593Smuzhiyun # endif /* ! STAGE1_5 */
891*4882a593Smuzhiyun 		    if (cmp == 0)
892*4882a593Smuzhiyun 		      goto found;
893*4882a593Smuzhiyun 		}
894*4882a593Smuzhiyun 	      /* The beginning of this name marks the end of the next name.
895*4882a593Smuzhiyun 	       */
896*4882a593Smuzhiyun 	      name_end = filename;
897*4882a593Smuzhiyun 	      de_head++;
898*4882a593Smuzhiyun 	      num_entries--;
899*4882a593Smuzhiyun 	    }
900*4882a593Smuzhiyun 	}
901*4882a593Smuzhiyun 
902*4882a593Smuzhiyun # ifndef STAGE1_5
903*4882a593Smuzhiyun       if (print_possibilities < 0)
904*4882a593Smuzhiyun 	return 1;
905*4882a593Smuzhiyun # endif /* ! STAGE1_5 */
906*4882a593Smuzhiyun 
907*4882a593Smuzhiyun       errnum = ERR_FILE_NOT_FOUND;
908*4882a593Smuzhiyun       *rest = ch;
909*4882a593Smuzhiyun       return 0;
910*4882a593Smuzhiyun 
911*4882a593Smuzhiyun     found:
912*4882a593Smuzhiyun       *rest = ch;
913*4882a593Smuzhiyun       dirname = rest;
914*4882a593Smuzhiyun 
915*4882a593Smuzhiyun       parent_dir_id = dir_id;
916*4882a593Smuzhiyun       parent_objectid = objectid;
917*4882a593Smuzhiyun       dir_id = deh_dir_id(de_head);
918*4882a593Smuzhiyun       objectid = deh_objectid(de_head);
919*4882a593Smuzhiyun     }
920*4882a593Smuzhiyun }
921*4882a593Smuzhiyun 
922*4882a593Smuzhiyun /*
923*4882a593Smuzhiyun  * U-Boot interface functions
924*4882a593Smuzhiyun  */
925*4882a593Smuzhiyun 
926*4882a593Smuzhiyun /*
927*4882a593Smuzhiyun  * List given directory
928*4882a593Smuzhiyun  *
929*4882a593Smuzhiyun  * RETURN: 0 - OK, else grub_error_t errnum
930*4882a593Smuzhiyun  */
931*4882a593Smuzhiyun int
reiserfs_ls(char * dirname)932*4882a593Smuzhiyun reiserfs_ls (char *dirname)
933*4882a593Smuzhiyun {
934*4882a593Smuzhiyun 	char *dir_slash;
935*4882a593Smuzhiyun 	int res;
936*4882a593Smuzhiyun 
937*4882a593Smuzhiyun 	errnum = 0;
938*4882a593Smuzhiyun 	dir_slash = malloc(strlen(dirname) + 1);
939*4882a593Smuzhiyun 	if (dir_slash == NULL) {
940*4882a593Smuzhiyun 		return ERR_NUMBER_OVERFLOW;
941*4882a593Smuzhiyun 	}
942*4882a593Smuzhiyun 	strcpy(dir_slash, dirname);
943*4882a593Smuzhiyun 	/* add "/" to the directory name */
944*4882a593Smuzhiyun 	strcat(dir_slash, "/");
945*4882a593Smuzhiyun 
946*4882a593Smuzhiyun 	print_possibilities = 1;
947*4882a593Smuzhiyun 	res = reiserfs_dir (dir_slash);
948*4882a593Smuzhiyun 	free(dir_slash);
949*4882a593Smuzhiyun 	if (!res || errnum) {
950*4882a593Smuzhiyun 		return errnum;
951*4882a593Smuzhiyun 	}
952*4882a593Smuzhiyun 
953*4882a593Smuzhiyun 	return 0;
954*4882a593Smuzhiyun }
955*4882a593Smuzhiyun 
956*4882a593Smuzhiyun /*
957*4882a593Smuzhiyun  * Open file for reading
958*4882a593Smuzhiyun  *
959*4882a593Smuzhiyun  * RETURN: >0 - OK, size of opened file
960*4882a593Smuzhiyun  *         <0 - ERROR  -grub_error_t errnum
961*4882a593Smuzhiyun  */
962*4882a593Smuzhiyun int
reiserfs_open(char * filename)963*4882a593Smuzhiyun reiserfs_open (char *filename)
964*4882a593Smuzhiyun {
965*4882a593Smuzhiyun 	/* open the file */
966*4882a593Smuzhiyun 	errnum = 0;
967*4882a593Smuzhiyun 	print_possibilities = 0;
968*4882a593Smuzhiyun 	if (!reiserfs_dir (filename) || errnum) {
969*4882a593Smuzhiyun 		return -errnum;
970*4882a593Smuzhiyun 	}
971*4882a593Smuzhiyun 	return filemax;
972*4882a593Smuzhiyun }
973