bfs.c 27 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
/* bfs.c - The Bee File System.  */
/*
 *  GRUB  --  GRand Unified Bootloader
 *  Copyright (C) 2010,2011  Free Software Foundation, Inc.
 *
 *  GRUB is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  GRUB is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with GRUB.  If not, see <http://www.gnu.org/licenses/>.
 */
/*
  Based on the book "Practical File System Design by Dominic Giampaolo
  with corrections and completitions based on Haiku code.
*/

#include <grub/err.h>
#include <grub/file.h>
#include <grub/mm.h>
#include <grub/misc.h>
#include <grub/disk.h>
#include <grub/dl.h>
#include <grub/types.h>
31
#include <grub/i18n.h>
32
#include <grub/fshelp.h>
33 34 35

GRUB_MOD_LICENSE ("GPLv3+");

36 37 38 39 40 41 42 43
#ifdef MODE_AFS
#define BTREE_ALIGN 4
#define SUPERBLOCK  2
#else
#define BTREE_ALIGN 8
#define SUPERBLOCK  1
#endif

44 45 46
#define grub_bfs_to_cpu16 grub_le_to_cpu16
#define grub_bfs_to_cpu32 grub_le_to_cpu32
#define grub_bfs_to_cpu64 grub_le_to_cpu64
47
#define grub_cpu_to_bfs32_compile_time grub_cpu_to_le32_compile_time
48

49 50 51 52 53 54 55 56 57
#ifdef MODE_AFS
#define grub_bfs_to_cpu_treehead grub_bfs_to_cpu32
#else
#define grub_bfs_to_cpu_treehead grub_bfs_to_cpu16
#endif

#ifdef MODE_AFS
#define SUPER_BLOCK_MAGIC1 0x41465331
#else
58
#define SUPER_BLOCK_MAGIC1 0x42465331
59
#endif
60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76
#define SUPER_BLOCK_MAGIC2 0xdd121031
#define SUPER_BLOCK_MAGIC3 0x15b6830e
#define POINTER_INVALID 0xffffffffffffffffULL

#define ATTR_TYPE      0160000
#define ATTR_REG       0100000
#define ATTR_DIR       0040000
#define ATTR_LNK       0120000

#define DOUBLE_INDIRECT_SHIFT 2

#define LOG_EXTENT_SIZE 3
struct grub_bfs_extent
{
  grub_uint32_t ag;
  grub_uint16_t start;
  grub_uint16_t len;
77
} GRUB_PACKED;
78 79 80 81 82 83 84 85 86 87 88 89 90 91 92

struct grub_bfs_superblock
{
  char label[32];
  grub_uint32_t magic1;
  grub_uint32_t unused1;
  grub_uint32_t bsize;
  grub_uint32_t log2_bsize;
  grub_uint8_t unused[20];
  grub_uint32_t magic2;
  grub_uint32_t unused2;
  grub_uint32_t log2_ag_size;
  grub_uint8_t unused3[32];
  grub_uint32_t magic3;
  struct grub_bfs_extent root_dir;
93
} GRUB_PACKED;
94 95 96 97 98 99

struct grub_bfs_inode
{
  grub_uint8_t unused[20];
  grub_uint32_t mode;
  grub_uint32_t flags;
100 101 102
#ifdef MODE_AFS
  grub_uint8_t unused2[12];
#else
103
  grub_uint8_t unused2[8];
104
#endif
105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121
  grub_uint64_t mtime;
  grub_uint8_t unused3[8];
  struct grub_bfs_extent attr;
  grub_uint8_t unused4[12];

  union
  {
    struct
    {
      struct grub_bfs_extent direct[12];
      grub_uint64_t max_direct_range;
      struct grub_bfs_extent indirect;
      grub_uint64_t max_indirect_range;
      struct grub_bfs_extent double_indirect;
      grub_uint64_t max_double_indirect_range;
      grub_uint64_t size;
      grub_uint32_t pad[4];
122
    } GRUB_PACKED;
123
    char inplace_link[144];
124
  } GRUB_PACKED;
125
  grub_uint8_t small_data[0];
126
} GRUB_PACKED;
127 128 129 130 131 132 133 134 135 136 137

enum
{
  LONG_SYMLINK = 0x40
};

struct grub_bfs_small_data_element_header
{
  grub_uint32_t type;
  grub_uint16_t name_len;
  grub_uint16_t value_len;
138
} GRUB_PACKED;
139 140 141 142

struct grub_bfs_btree_header
{
  grub_uint32_t magic;
143 144 145 146 147 148
#ifdef MODE_AFS
  grub_uint64_t root;
  grub_uint32_t level;
  grub_uint32_t node_size;
  grub_uint32_t unused;
#else
149 150 151 152
  grub_uint32_t node_size;
  grub_uint32_t level;
  grub_uint32_t unused;
  grub_uint64_t root;
153
#endif
154
  grub_uint32_t unused2[2];
155
} GRUB_PACKED;
156 157 158 159 160 161

struct grub_bfs_btree_node
{
  grub_uint64_t unused;
  grub_uint64_t right;
  grub_uint64_t overflow;
162 163 164 165
#ifdef MODE_AFS
  grub_uint32_t count_keys;
  grub_uint32_t total_key_len;
#else
166 167
  grub_uint16_t count_keys;
  grub_uint16_t total_key_len;
168
#endif
169
} GRUB_PACKED;
170 171 172 173

struct grub_bfs_data
{
  struct grub_bfs_superblock sb;
174
  struct grub_bfs_inode ino;
175 176
};

177 178 179 180 181 182 183 184 185
/* Context for grub_bfs_dir.  */
struct grub_bfs_dir_ctx
{
  grub_device_t device;
  grub_fs_dir_hook_t hook;
  void *hook_data;
  struct grub_bfs_superblock sb;
};

186 187 188 189
static grub_err_t
read_extent (grub_disk_t disk,
	     const struct grub_bfs_superblock *sb,
	     const struct grub_bfs_extent *in,
190
	     grub_off_t off, grub_off_t byteoff, void *buf, grub_size_t len)
191
{
192
#ifdef MODE_AFS
193
  return grub_disk_read (disk, ((grub_bfs_to_cpu32 (in->ag)
194 195 196 197 198 199 200 201 202 203 204 205 206 207
				 << (grub_bfs_to_cpu32 (sb->log2_ag_size)
				     - GRUB_DISK_SECTOR_BITS))
				+ ((grub_bfs_to_cpu16 (in->start) + off)
				   << (grub_bfs_to_cpu32 (sb->log2_bsize)
				       - GRUB_DISK_SECTOR_BITS))),
			 byteoff, len, buf);
#else
  return grub_disk_read (disk, (((grub_bfs_to_cpu32 (in->ag)
				  << grub_bfs_to_cpu32 (sb->log2_ag_size))
				 + grub_bfs_to_cpu16 (in->start) + off)
				<< (grub_bfs_to_cpu32 (sb->log2_bsize)
				    - GRUB_DISK_SECTOR_BITS)),
			 byteoff, len, buf);
#endif
208 209
}

210 211 212 213 214 215
#ifdef MODE_AFS
#define RANGE_SHIFT grub_bfs_to_cpu32 (sb->log2_bsize)
#else
#define RANGE_SHIFT 0
#endif

216 217 218 219 220
static grub_err_t
read_bfs_file (grub_disk_t disk,
	       const struct grub_bfs_superblock *sb,
	       const struct grub_bfs_inode *ino,
	       grub_off_t off, void *buf, grub_size_t len,
221
	       grub_disk_read_hook_t read_hook, void *read_hook_data)
222 223 224 225 226
{
  if (len == 0)
    return GRUB_ERR_NONE;

  if (off + len > grub_bfs_to_cpu64 (ino->size))
227
    return grub_error (GRUB_ERR_OUT_OF_RANGE,
228
		       N_("attempt to read past the end of file"));
229

230
  if (off < (grub_bfs_to_cpu64 (ino->max_direct_range) << RANGE_SHIFT))
231 232 233 234 235 236
    {
      unsigned i;
      grub_uint64_t pos = 0;
      for (i = 0; i < ARRAY_SIZE (ino->direct); i++)
	{
	  grub_uint64_t newpos;
237
	  newpos = pos + (((grub_uint64_t) grub_bfs_to_cpu16 (ino->direct[i].len))
238 239 240 241 242 243 244 245 246
			  << grub_bfs_to_cpu32 (sb->log2_bsize));
	  if (newpos > off)
	    {
	      grub_size_t read_size;
	      grub_err_t err;
	      read_size = newpos - off;
	      if (read_size > len)
		read_size = len;
	      disk->read_hook = read_hook;
247
	      disk->read_hook_data = read_hook_data;
248 249 250 251 252 253 254 255 256 257 258 259 260 261 262
	      err = read_extent (disk, sb, &ino->direct[i], 0, off - pos,
				 buf, read_size);
	      disk->read_hook = 0;
	      if (err)
		return err;
	      off += read_size;
	      len -= read_size;
	      buf = (char *) buf + read_size;
	      if (len == 0)
		return GRUB_ERR_NONE;
	    }
	  pos = newpos;
	}
    }

263
  if (off < (grub_bfs_to_cpu64 (ino->max_direct_range) << RANGE_SHIFT))
264 265
    return grub_error (GRUB_ERR_BAD_FS, "incorrect direct blocks");

266
  if (off < (grub_bfs_to_cpu64 (ino->max_indirect_range) << RANGE_SHIFT))
267 268 269 270 271
    {
      unsigned i;
      struct grub_bfs_extent *entries;
      grub_size_t nentries;
      grub_err_t err;
272 273
      grub_uint64_t pos = (grub_bfs_to_cpu64 (ino->max_direct_range)
			   << RANGE_SHIFT);
274
      nentries = (((grub_size_t) grub_bfs_to_cpu16 (ino->indirect.len))
275
		  << (grub_bfs_to_cpu32 (sb->log2_bsize) - LOG_EXTENT_SIZE));
276 277
      entries = grub_malloc (nentries << LOG_EXTENT_SIZE);
      if (!entries)
278
	return grub_errno;
279 280 281 282 283
      err = read_extent (disk, sb, &ino->indirect, 0, 0,
			 entries, nentries << LOG_EXTENT_SIZE);
      for (i = 0; i < nentries; i++)
	{
	  grub_uint64_t newpos;
284
	  newpos = pos + (((grub_uint64_t) grub_bfs_to_cpu16 (entries[i].len))
285 286 287 288 289 290 291 292
			  << grub_bfs_to_cpu32 (sb->log2_bsize));
	  if (newpos > off)
	    {
	      grub_size_t read_size;
	      read_size = newpos - off;
	      if (read_size > len)
		read_size = len;
	      disk->read_hook = read_hook;
293
	      disk->read_hook_data = read_hook_data;
294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315
	      err = read_extent (disk, sb, &entries[i], 0, off - pos,
				 buf, read_size);
	      disk->read_hook = 0;
	      if (err)
		{
		  grub_free (entries);
		  return err;
		}
	      off += read_size;
	      len -= read_size;
	      buf = (char *) buf + read_size;
	      if (len == 0)
		{
		  grub_free (entries);
		  return GRUB_ERR_NONE;
		}
	    }
	  pos = newpos;
	}
      grub_free (entries);
    }

316
  if (off < (grub_bfs_to_cpu64 (ino->max_indirect_range) << RANGE_SHIFT))
317 318 319 320 321 322 323
    return grub_error (GRUB_ERR_BAD_FS, "incorrect indirect blocks");

  {
    struct grub_bfs_extent *l1_entries, *l2_entries;
    grub_size_t nl1_entries, nl2_entries;
    grub_off_t last_l1n = ~0ULL;
    grub_err_t err;
324
    nl1_entries = (((grub_uint64_t) grub_bfs_to_cpu16 (ino->double_indirect.len))
325
		   << (grub_bfs_to_cpu32 (sb->log2_bsize) - LOG_EXTENT_SIZE));
326 327 328 329 330 331 332 333 334
    l1_entries = grub_malloc (nl1_entries << LOG_EXTENT_SIZE);
    if (!l1_entries)
      return grub_errno;
    nl2_entries = 0;
    l2_entries = grub_malloc (1 << (DOUBLE_INDIRECT_SHIFT
				    + grub_bfs_to_cpu32 (sb->log2_bsize)));
    if (!l2_entries)
      {
	grub_free (l1_entries);
335
	return grub_errno;
336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352
      }
    err = read_extent (disk, sb, &ino->double_indirect, 0, 0,
		       l1_entries, nl1_entries << LOG_EXTENT_SIZE);
    if (err)
      {
	grub_free (l1_entries);
	grub_free (l2_entries);
	return err;
      }

    while (len > 0)
      {
	grub_off_t boff, l2n, l1n;
	grub_size_t read_size;
	grub_off_t double_indirect_offset;
	double_indirect_offset = off
	  - grub_bfs_to_cpu64 (ino->max_indirect_range);
353
	boff = (double_indirect_offset
354 355 356 357 358
		& ((1 << (grub_bfs_to_cpu32 (sb->log2_bsize)
			  + DOUBLE_INDIRECT_SHIFT)) - 1));
	l2n = ((double_indirect_offset >> (grub_bfs_to_cpu32 (sb->log2_bsize)
					   + DOUBLE_INDIRECT_SHIFT))
	       & ((1 << (grub_bfs_to_cpu32 (sb->log2_bsize) - LOG_EXTENT_SIZE
359 360 361 362 363
			 + DOUBLE_INDIRECT_SHIFT)) - 1));
	l1n =
	  (double_indirect_offset >>
	   (2 * grub_bfs_to_cpu32 (sb->log2_bsize) - LOG_EXTENT_SIZE +
	    2 * DOUBLE_INDIRECT_SHIFT));
364 365 366 367 368 369 370 371 372
	if (l1n > nl1_entries)
	  {
	    grub_free (l1_entries);
	    grub_free (l2_entries);
	    return grub_error (GRUB_ERR_BAD_FS,
			       "incorrect double-indirect block");
	  }
	if (l1n != last_l1n)
	  {
373
	    nl2_entries = (((grub_uint64_t) grub_bfs_to_cpu16 (l1_entries[l1n].len))
374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404
			   << (grub_bfs_to_cpu32 (sb->log2_bsize)
			       - LOG_EXTENT_SIZE));
	    if (nl2_entries > (1U << (grub_bfs_to_cpu32 (sb->log2_bsize)
				      - LOG_EXTENT_SIZE
				      + DOUBLE_INDIRECT_SHIFT)))
	      nl2_entries = (1 << (grub_bfs_to_cpu32 (sb->log2_bsize)
				   - LOG_EXTENT_SIZE
				   + DOUBLE_INDIRECT_SHIFT));
	    err = read_extent (disk, sb, &l1_entries[l1n], 0, 0,
			       l2_entries, nl2_entries << LOG_EXTENT_SIZE);
	    if (err)
	      {
		grub_free (l1_entries);
		grub_free (l2_entries);
		return err;
	      }
	    last_l1n = l1n;
	  }
	if (l2n > nl2_entries)
	  {
	    grub_free (l1_entries);
	    grub_free (l2_entries);
	    return grub_error (GRUB_ERR_BAD_FS,
			       "incorrect double-indirect block");
	  }

	read_size = (1 << (grub_bfs_to_cpu32 (sb->log2_bsize)
			   + DOUBLE_INDIRECT_SHIFT)) - boff;
	if (read_size > len)
	  read_size = len;
	disk->read_hook = read_hook;
405
	disk->read_hook_data = read_hook_data;
406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422
	err = read_extent (disk, sb, &l2_entries[l2n], 0, boff,
			   buf, read_size);
	disk->read_hook = 0;
	if (err)
	  {
	    grub_free (l1_entries);
	    grub_free (l2_entries);
	    return err;
	  }
	off += read_size;
	len -= read_size;
	buf = (char *) buf + read_size;
      }
    return GRUB_ERR_NONE;
  }
}

423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479
static grub_err_t
read_b_node (grub_disk_t disk,
	     const struct grub_bfs_superblock *sb,
	     const struct grub_bfs_inode *ino,
	     grub_uint64_t node_off,
	     struct grub_bfs_btree_node **node,
	     char **key_data, grub_uint16_t **keylen_idx,
	     grub_unaligned_uint64_t **key_values)
{
  void *ret;
  struct grub_bfs_btree_node node_head;
  grub_size_t total_size;
  grub_err_t err;

  *node = NULL;
  *key_data = NULL;
  *keylen_idx = NULL;
  *key_values = NULL;

  err = read_bfs_file (disk, sb, ino, node_off, &node_head, sizeof (node_head),
		       0, 0);
  if (err)
    return err;

  total_size = ALIGN_UP (sizeof (node_head) +
			 grub_bfs_to_cpu_treehead
			 (node_head.total_key_len),
			 BTREE_ALIGN) +
    grub_bfs_to_cpu_treehead (node_head.count_keys) *
    sizeof (grub_uint16_t)
    + grub_bfs_to_cpu_treehead (node_head.count_keys) *
    sizeof (grub_uint64_t);

  ret = grub_malloc (total_size);
  if (!ret)
    return grub_errno;

  err = read_bfs_file (disk, sb, ino, node_off, ret, total_size, 0, 0);
  if (err)
    {
      grub_free (ret);
      return err;
    }

  *node = ret;
  *key_data = (char *) ret + sizeof (node_head);
  *keylen_idx = (grub_uint16_t *) ret
    + ALIGN_UP (sizeof (node_head) +
		grub_bfs_to_cpu_treehead (node_head.total_key_len),
		BTREE_ALIGN) / 2;
  *key_values = (grub_unaligned_uint64_t *)
    (*keylen_idx +
     grub_bfs_to_cpu_treehead (node_head.count_keys));

  return GRUB_ERR_NONE;
}

480 481 482 483
static int
iterate_in_b_tree (grub_disk_t disk,
		   const struct grub_bfs_superblock *sb,
		   const struct grub_bfs_inode *ino,
484 485 486
		   int (*hook) (const char *name, grub_uint64_t value,
				struct grub_bfs_dir_ctx *ctx),
		   struct grub_bfs_dir_ctx *ctx)
487 488 489 490 491 492
{
  struct grub_bfs_btree_header head;
  grub_err_t err;
  int level;
  grub_uint64_t node_off;

493
  err = read_bfs_file (disk, sb, ino, 0, &head, sizeof (head), 0, 0);
494 495 496 497 498 499 500 501 502
  if (err)
    return 0;
  node_off = grub_bfs_to_cpu64 (head.root);

  level = grub_bfs_to_cpu32 (head.level) - 1;
  while (level--)
    {
      struct grub_bfs_btree_node node;
      grub_uint64_t key_value;
503 504
      err = read_bfs_file (disk, sb, ino, node_off, &node, sizeof (node),
			   0, 0);
505 506 507
      if (err)
	return 0;
      err = read_bfs_file (disk, sb, ino, node_off
508
			   + ALIGN_UP (sizeof (node) +
509 510 511 512 513
				       grub_bfs_to_cpu_treehead (node.
								 total_key_len),
				       BTREE_ALIGN) +
			   grub_bfs_to_cpu_treehead (node.count_keys) *
			   sizeof (grub_uint16_t), &key_value,
514
			   sizeof (grub_uint64_t), 0, 0);
515 516 517 518 519 520 521 522
      if (err)
	return 0;

      node_off = grub_bfs_to_cpu64 (key_value);
    }

  while (1)
    {
523 524 525 526 527 528 529 530 531 532 533 534 535 536
      struct grub_bfs_btree_node *node;
      char *key_data;
      grub_uint16_t *keylen_idx;
      grub_unaligned_uint64_t *key_values;
      unsigned i;
      grub_uint16_t start = 0, end = 0;

      err = read_b_node (disk, sb, ino,
			 node_off,
			 &node,
			 &key_data, 
			 &keylen_idx,
			 &key_values);

537 538
      if (err)
	return 0;
539 540 541 542 543 544 545 546 547 548 549 550 551 552
      
      for (i = 0; i < grub_bfs_to_cpu_treehead (node->count_keys); i++)
	{
	  char c;
	  start = end;
	  end = grub_bfs_to_cpu16 (keylen_idx[i]);
	  if (grub_bfs_to_cpu_treehead (node->total_key_len) <= end)
	    end = grub_bfs_to_cpu_treehead (node->total_key_len);
	  c = key_data[end];
	  key_data[end] = 0;
	  if (hook (key_data + start, grub_bfs_to_cpu64 (key_values[i].val),
		    ctx))
	    {
	      grub_free (node);
553
	      return 1;
554
	    }
555 556
	    key_data[end] = c;
	  }
557 558
	node_off = grub_bfs_to_cpu64 (node->right);
	grub_free (node);
559 560 561 562 563
	if (node_off == POINTER_INVALID)
	  return 0;
    }
}

564
static int
565
bfs_strcmp (const char *a, const char *b, grub_size_t alen)
566
{
567
  char ac, bc;
568
  while (*b && alen)
569 570 571 572 573 574 575 576 577
    {
      if (*a != *b)
	break;

      a++;
      b++;
      alen--;
    }

578
  ac = alen ? *a : 0;
579
  bc = *b;
580

581 582
#ifdef MODE_AFS
  return (int) (grub_int8_t) ac - (int) (grub_int8_t) bc;
583
#else
584
  return (int) (grub_uint8_t) ac - (int) (grub_uint8_t) bc;
585
#endif
586 587
}

588 589 590 591
static grub_err_t
find_in_b_tree (grub_disk_t disk,
		const struct grub_bfs_superblock *sb,
		const struct grub_bfs_inode *ino, const char *name,
592
		grub_uint64_t * res)
593 594 595 596 597 598
{
  struct grub_bfs_btree_header head;
  grub_err_t err;
  int level;
  grub_uint64_t node_off;

599
  err = read_bfs_file (disk, sb, ino, 0, &head, sizeof (head), 0, 0);
600 601 602 603 604 605 606
  if (err)
    return err;
  node_off = grub_bfs_to_cpu64 (head.root);

  level = grub_bfs_to_cpu32 (head.level) - 1;
  while (1)
    {
607 608 609 610 611 612 613 614
      struct grub_bfs_btree_node *node;
      char *key_data;
      grub_uint16_t *keylen_idx;
      grub_unaligned_uint64_t *key_values;
      int lg, j;
      unsigned i;

      err = read_b_node (disk, sb, ino, node_off, &node, &key_data, &keylen_idx, &key_values);
615 616 617
      if (err)
	return err;

618 619 620 621 622 623
      if (node->count_keys == 0)
	{
	  grub_free (node);
	  return grub_error (GRUB_ERR_FILE_NOT_FOUND, N_("file `%s' not found"),
			     name);
	}
624

625
      for (lg = 0; grub_bfs_to_cpu_treehead (node->count_keys) >> lg; lg++);
626

627
      i = 0;
628

629 630 631 632 633 634 635 636 637 638
      for (j = lg - 1; j >= 0; j--)
	{
	  int cmp;
	  grub_uint16_t start = 0, end = 0;
	  if ((i | (1 << j)) >= grub_bfs_to_cpu_treehead (node->count_keys))
	    continue;
	  start = grub_bfs_to_cpu16 (keylen_idx[(i | (1 << j)) - 1]);
	  end = grub_bfs_to_cpu16 (keylen_idx[(i | (1 << j))]);
	  if (grub_bfs_to_cpu_treehead (node->total_key_len) <= end)
	    end = grub_bfs_to_cpu_treehead (node->total_key_len);
639
	  cmp = bfs_strcmp (key_data + start, name, end - start);
640 641 642 643 644 645
	  if (cmp == 0 && level == 0)
	    {
	      *res = grub_bfs_to_cpu64 (key_values[i | (1 << j)].val);
	      grub_free (node);
	      return GRUB_ERR_NONE;
	    }
646
#ifdef MODE_AFS
647
	  if (cmp <= 0)
648
#else
649
	  if (cmp < 0)
650
#endif
651 652 653 654 655 656 657 658 659
	    i |= (1 << j);
	}
      if (i == 0)
	{
	  grub_uint16_t end = 0;
	  int cmp;
	  end = grub_bfs_to_cpu16 (keylen_idx[0]);
	  if (grub_bfs_to_cpu_treehead (node->total_key_len) <= end)
	    end = grub_bfs_to_cpu_treehead (node->total_key_len);
660
	  cmp = bfs_strcmp (key_data, name, end);
661 662 663 664 665 666
	  if (cmp == 0 && level == 0)
	    {
	      *res = grub_bfs_to_cpu64 (key_values[0].val);
	      grub_free (node);
	      return GRUB_ERR_NONE;
	    }
667
#ifdef MODE_AFS
668
	  if (cmp > 0 && level != 0)
669
#else
670
	    if (cmp >= 0 && level != 0)
671
#endif
672
	      {
673
		node_off = grub_bfs_to_cpu64 (key_values[0].val);
674
		level--;
675
		grub_free (node);
676
		continue;
677
	      }
678
	    else if (level != 0
679
		     && grub_bfs_to_cpu_treehead (node->count_keys) >= 2)
680
	      {
681
		node_off = grub_bfs_to_cpu64 (key_values[1].val);
682
		level--;
683
		grub_free (node);
684 685
		continue;
	      }	      
686
	  }
687
	else if (level != 0
688
		 && i + 1 < grub_bfs_to_cpu_treehead (node->count_keys))
689
	  {
690
	    node_off = grub_bfs_to_cpu64 (key_values[i + 1].val);
691
	    level--;
692
	    grub_free (node);
693 694
	    continue;
	  }
695
	if (node->overflow != POINTER_INVALID)
696
	  {
697
	    node_off = grub_bfs_to_cpu64 (node->overflow);
698 699
	    /* This level-- isn't specified but is needed.  */
	    level--;
700
	    grub_free (node);
701 702
	    continue;
	  }
703
	grub_free (node);
704
	return grub_error (GRUB_ERR_FILE_NOT_FOUND, N_("file `%s' not found"),
705 706 707 708
			   name);
    }
}

709 710 711 712 713 714 715
struct grub_fshelp_node
{
  grub_disk_t disk;
  const struct grub_bfs_superblock *sb;
  struct grub_bfs_inode ino;
};

716
static grub_err_t
717 718 719 720
lookup_file (grub_fshelp_node_t dir,
	     const char *name,
	     grub_fshelp_node_t *foundnode,
	     enum grub_fshelp_filetype *foundtype)
721 722
{
  grub_err_t err;
723
  struct grub_bfs_inode *new_ino;
724
  grub_uint64_t res = 0;
725

726
  err = find_in_b_tree (dir->disk, dir->sb, &dir->ino, name, &res);
727 728 729
  if (err)
    return err;

730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759
  *foundnode = grub_malloc (sizeof (struct grub_fshelp_node));
  if (!*foundnode)
    return grub_errno;

  (*foundnode)->disk = dir->disk;
  (*foundnode)->sb = dir->sb;
  new_ino = &(*foundnode)->ino;

  if (grub_disk_read (dir->disk, res
		      << (grub_bfs_to_cpu32 (dir->sb->log2_bsize)
			  - GRUB_DISK_SECTOR_BITS), 0,
		      sizeof (*new_ino), (char *) new_ino))
    {
      grub_free (*foundnode);
      return grub_errno;
    }
  switch (grub_bfs_to_cpu32 (new_ino->mode) & ATTR_TYPE)
    {
    default:
    case ATTR_REG:
      *foundtype = GRUB_FSHELP_REG;
      break;
    case ATTR_DIR:
      *foundtype = GRUB_FSHELP_DIR;
      break;
    case ATTR_LNK:
      *foundtype = GRUB_FSHELP_SYMLINK;
      break;
    }
  return GRUB_ERR_NONE;
760 761
}

762 763
static char *
read_symlink (grub_fshelp_node_t node)
764 765 766 767
{
  char *alloc = NULL;
  grub_err_t err;

768 769
#ifndef MODE_AFS
  if (!(grub_bfs_to_cpu32 (node->ino.flags) & LONG_SYMLINK))
770
    {
771 772
      alloc = grub_malloc (sizeof (node->ino.inplace_link) + 1);
      if (!alloc)
773
	{
774
	  return NULL;
775
	}
776 777 778 779 780 781 782 783 784 785 786 787
      grub_memcpy (alloc, node->ino.inplace_link,
		   sizeof (node->ino.inplace_link));
      alloc[sizeof (node->ino.inplace_link)] = 0;
    }
  else
#endif
    {
      grub_size_t symsize = grub_bfs_to_cpu64 (node->ino.size);
      alloc = grub_malloc (symsize + 1);
      if (!alloc)
	return NULL;
      err = read_bfs_file (node->disk, node->sb, &node->ino, 0, alloc, symsize, 0, 0);
788 789
      if (err)
	{
790 791
	  grub_free (alloc);
	  return NULL;
792
	}
793
      alloc[symsize] = 0;
794
    }
795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821

  return alloc;
}

static grub_err_t
find_file (const char *path, grub_disk_t disk,
	   const struct grub_bfs_superblock *sb, struct grub_bfs_inode *ino,
	   enum grub_fshelp_filetype exptype)
{
  grub_err_t err;
  struct grub_fshelp_node root = {
    .disk = disk,
    .sb = sb,
  };
  struct grub_fshelp_node *found;

  err = read_extent (disk, sb, &sb->root_dir, 0, 0, &root.ino,
		     sizeof (root.ino));
  if (err)
    return err;
  err = grub_fshelp_find_file_lookup (path, &root, &found, lookup_file, read_symlink, exptype);
  if (!err)
    grub_memcpy (ino, &found->ino, sizeof (*ino));

  if (&root != found)
    grub_free (found);
  return err;
822 823 824 825 826 827
}

static grub_err_t
mount (grub_disk_t disk, struct grub_bfs_superblock *sb)
{
  grub_err_t err;
828
  err = grub_disk_read (disk, SUPERBLOCK, 0, sizeof (*sb), sb);
829
  if (err == GRUB_ERR_OUT_OF_RANGE)
830 831 832 833 834 835 836
    return grub_error (GRUB_ERR_BAD_FS, 
#ifdef MODE_AFS
		       "not an AFS filesystem"
#else
		       "not a BFS filesystem"
#endif
		       );
837 838
  if (err)
    return err;
839 840 841 842
  if (sb->magic1 != grub_cpu_to_bfs32_compile_time (SUPER_BLOCK_MAGIC1)
      || sb->magic2 != grub_cpu_to_bfs32_compile_time (SUPER_BLOCK_MAGIC2)
      || sb->magic3 != grub_cpu_to_bfs32_compile_time (SUPER_BLOCK_MAGIC3)
      || sb->bsize == 0
843
      || (grub_bfs_to_cpu32 (sb->bsize)
844 845
	  != (1U << grub_bfs_to_cpu32 (sb->log2_bsize)))
      || grub_bfs_to_cpu32 (sb->log2_bsize) < GRUB_DISK_SECTOR_BITS)
846 847 848 849 850 851 852
    return grub_error (GRUB_ERR_BAD_FS, 
#ifdef MODE_AFS
		       "not an AFS filesystem"
#else
		       "not a BFS filesystem"
#endif
		       );
853 854 855
  return GRUB_ERR_NONE;
}

856 857 858 859
/* Helper for grub_bfs_dir.  */
static int
grub_bfs_dir_iter (const char *name, grub_uint64_t value,
		   struct grub_bfs_dir_ctx *ctx)
860
{
861
  grub_err_t err2;
862
  struct grub_bfs_inode ino;
863
  struct grub_dirhook_info info;
864

865 866 867
  err2 = grub_disk_read (ctx->device->disk, value
			 << (grub_bfs_to_cpu32 (ctx->sb.log2_bsize)
			     - GRUB_DISK_SECTOR_BITS), 0,
868
			 sizeof (ino), (char *) &ino);
869 870 871 872 873
  if (err2)
    {
      grub_print_error ();
      return 0;
    }
874

875
  info.mtimeset = 1;
876
#ifdef MODE_AFS
877
  info.mtime =
878
    grub_divmod64 (grub_bfs_to_cpu64 (ino.mtime), 1000000, 0);
879
#else
880
  info.mtime = grub_bfs_to_cpu64 (ino.mtime) >> 16;
881
#endif
882
  info.dir = ((grub_bfs_to_cpu32 (ino.mode) & ATTR_TYPE) == ATTR_DIR);
883 884 885 886 887 888 889 890 891 892 893 894 895 896 897
  return ctx->hook (name, &info, ctx->hook_data);
}

static grub_err_t
grub_bfs_dir (grub_device_t device, const char *path,
	      grub_fs_dir_hook_t hook, void *hook_data)
{
  struct grub_bfs_dir_ctx ctx = {
    .device = device,
    .hook = hook,
    .hook_data = hook_data
  };
  grub_err_t err;

  err = mount (device->disk, &ctx.sb);
898 899 900 901
  if (err)
    return err;

  {
902
    struct grub_bfs_inode ino;
903
    err = find_file (path, device->disk, &ctx.sb, &ino, GRUB_FSHELP_DIR);
904 905
    if (err)
      return err;
906
    iterate_in_b_tree (device->disk, &ctx.sb, &ino, grub_bfs_dir_iter,
907
		       &ctx);
908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923
  }

  return grub_errno;
}

static grub_err_t
grub_bfs_open (struct grub_file *file, const char *name)
{
  struct grub_bfs_superblock sb;
  grub_err_t err;

  err = mount (file->device->disk, &sb);
  if (err)
    return err;

  {
924
    struct grub_bfs_inode ino;
925
    struct grub_bfs_data *data;
926
    err = find_file (name, file->device->disk, &sb, &ino, GRUB_FSHELP_REG);
927 928 929
    if (err)
      return err;

930
    data = grub_zalloc (sizeof (struct grub_bfs_data));
931 932 933
    if (!data)
      return grub_errno;
    data->sb = sb;
934
    grub_memcpy (&data->ino, &ino, sizeof (data->ino));
935
    file->data = data;
936
    file->size = grub_bfs_to_cpu64 (ino.size);
937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954
  }

  return GRUB_ERR_NONE;
}

static grub_err_t
grub_bfs_close (grub_file_t file)
{
  grub_free (file->data);

  return GRUB_ERR_NONE;
}

static grub_ssize_t
grub_bfs_read (grub_file_t file, char *buf, grub_size_t len)
{
  grub_err_t err;
  struct grub_bfs_data *data = file->data;
955

956
  err = read_bfs_file (file->device->disk, &data->sb,
957
		       &data->ino, file->offset, buf, len,
958
		       file->read_hook, file->read_hook_data);
959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979
  if (err)
    return -1;
  return len;
}

static grub_err_t
grub_bfs_label (grub_device_t device, char **label)
{
  struct grub_bfs_superblock sb;
  grub_err_t err;

  *label = 0;

  err = mount (device->disk, &sb);
  if (err)
    return err;

  *label = grub_strndup (sb.label, sizeof (sb.label));
  return GRUB_ERR_NONE;
}

980
#ifndef MODE_AFS
981 982 983
static grub_ssize_t
read_bfs_attr (grub_disk_t disk,
	       const struct grub_bfs_superblock *sb,
984
	       struct grub_bfs_inode *ino,
985
	       const char *name, void *buf, grub_size_t len)
986 987
{
  grub_uint8_t *ptr = (grub_uint8_t *) ino->small_data;
988
  grub_uint8_t *end = ((grub_uint8_t *) ino + grub_bfs_to_cpu32 (sb->bsize));
989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013

  while (ptr + sizeof (struct grub_bfs_small_data_element_header) < end)
    {
      struct grub_bfs_small_data_element_header *el;
      char *el_name;
      grub_uint8_t *data;
      el = (struct grub_bfs_small_data_element_header *) ptr;
      if (el->name_len == 0)
	break;
      el_name = (char *) (el + 1);
      data = (grub_uint8_t *) el_name + grub_bfs_to_cpu16 (el->name_len) + 3;
      ptr = data + grub_bfs_to_cpu16 (el->value_len) + 1;
      if (grub_memcmp (name, el_name, grub_bfs_to_cpu16 (el->name_len)) == 0
	  && name[el->name_len] == 0)
	{
	  grub_size_t copy;
	  copy = len;
	  if (grub_bfs_to_cpu16 (el->value_len) > copy)
	    copy = grub_bfs_to_cpu16 (el->value_len);
	  grub_memcpy (buf, data, copy);
	  return copy;
	}
    }

  if (ino->attr.len != 0)
1014 1015 1016 1017
    {
      grub_size_t read;
      grub_err_t err;
      grub_uint64_t res;
1018

1019
      err = read_extent (disk, sb, &ino->attr, 0, 0, ino,
1020 1021 1022
			 grub_bfs_to_cpu32 (sb->bsize));
      if (err)
	return -1;
1023

1024
      err = find_in_b_tree (disk, sb, ino, name, &res);
1025 1026 1027
      if (err)
	return -1;
      grub_disk_read (disk, res
1028 1029
		      << (grub_bfs_to_cpu32 (sb->log2_bsize)
			  - GRUB_DISK_SECTOR_BITS), 0,
1030 1031
		      grub_bfs_to_cpu32 (sb->bsize), (char *) ino);
      read = grub_bfs_to_cpu64 (ino->size);
1032 1033 1034
      if (read > len)
	read = len;

1035
      err = read_bfs_file (disk, sb, ino, 0, buf, read, 0, 0);
1036 1037 1038 1039
      if (err)
	return -1;
      return read;
    }
1040 1041 1042 1043 1044 1045 1046 1047
  return -1;
}

static grub_err_t
grub_bfs_uuid (grub_device_t device, char **uuid)
{
  struct grub_bfs_superblock sb;
  grub_err_t err;
1048 1049
  struct grub_bfs_inode *ino;
  grub_uint64_t vid;
1050 1051 1052 1053 1054 1055 1056

  *uuid = 0;

  err = mount (device->disk, &sb);
  if (err)
    return err;

1057 1058 1059
  ino = grub_malloc (grub_bfs_to_cpu32 (sb.bsize));
  if (!ino)
    return grub_errno;
1060

1061 1062 1063 1064 1065
  err = read_extent (device->disk, &sb, &sb.root_dir, 0, 0,
		     ino, grub_bfs_to_cpu32 (sb.bsize));
  if (err)
    {
      grub_free (ino);
1066
      return err;
1067 1068 1069 1070 1071 1072 1073 1074
    }
  if (read_bfs_attr (device->disk, &sb, ino, "be:volume_id",
		     &vid, sizeof (vid)) == sizeof (vid))
    *uuid =
      grub_xasprintf ("%016" PRIxGRUB_UINT64_T, grub_bfs_to_cpu64 (vid));

  grub_free (ino);

1075 1076
  return GRUB_ERR_NONE;
}
1077
#endif
1078 1079

static struct grub_fs grub_bfs_fs = {
1080 1081 1082
#ifdef MODE_AFS
  .name = "afs",
#else
1083
  .name = "bfs",
1084
#endif
1085 1086 1087 1088 1089
  .dir = grub_bfs_dir,
  .open = grub_bfs_open,
  .read = grub_bfs_read,
  .close = grub_bfs_close,
  .label = grub_bfs_label,
1090
#ifndef MODE_AFS
1091
  .uuid = grub_bfs_uuid,
1092
#endif
1093 1094
#ifdef GRUB_UTIL
  .reserved_first_sector = 1,
1095
  .blocklist_install = 1,
1096 1097 1098
#endif
};

1099 1100 1101
#ifdef MODE_AFS
GRUB_MOD_INIT (afs)
#else
1102
GRUB_MOD_INIT (bfs)
1103
#endif
1104
{
1105 1106
  COMPILE_TIME_ASSERT (1 << LOG_EXTENT_SIZE ==
		       sizeof (struct grub_bfs_extent));
1107 1108 1109
  grub_fs_register (&grub_bfs_fs);
}

1110 1111 1112
#ifdef MODE_AFS
GRUB_MOD_FINI (afs)
#else
1113
GRUB_MOD_FINI (bfs)
1114
#endif
1115 1116 1117
{
  grub_fs_unregister (&grub_bfs_fs);
}