Skip to contentSkip to conversation

blklist.c

DISCUSSION Yoshinori K. Okuji 2 participants 4 messages Apr 30, 2006

lore
Share & tools
lore
#0 lore raw

Here is a simple utility to display a block list based on the knowledge of Linux's but not of GRUB's. I wrote this to aid the debugging of a filesystem. The output format is similar to GRUB's block list, but not completely identical (because GRUB always treats sectors instead of filesystem blocks). If you think this is useful, I can put this code into GRUB.

Okuji

/* blklist.c - display block lists of files */ /*
* Copyright (C) 2006 Yoshinori Okuji <[email protected]> *

 *  This program 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 2 of the License, or
 *  (at your option) any later version.
 *
 *  This program 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 this program; if not, write to the Free Software
 *  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.

*/

#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <stdlib.h>

#ifdef __linux__

# include <sys/ioctl.h>
# define FIBMAP		_IO(0x00,1)
# define FIGETBSZ	_IO(0x00,2)
#else /* ! __linux__ */
# error "This code is specific to Linux at the moment."
#endif /* ! __linux__ */

struct block
{
int start;
int len;
struct block *next;
};

static void
show_blklist (const char *filename)
{
int fd;
int blocksize;
struct stat st;
int i;
struct block head = { 0, 0, 0 };
struct block *current = &head;

  fd = open (filename, O_RDONLY);
  if (fd < 0)
    {
      printf ("cannot open %s\n", filename);
      goto fail;
    }

  if (ioctl (fd, FIGETBSZ, &blocksize) < 0)
    {
      printf ("cannot get the block size of %s\n", filename);
      goto fail;
    }

  if (fstat (fd, &st) < 0)
    {
      printf ("cannot get the size of %s\n", filename);
      goto fail;
    }

  for (i = 0; i < (st.st_size + blocksize - 1) / blocksize; i++)
    {
      int block = i;
      if (ioctl (fd, FIBMAP, &block) < 0)
	{
	  printf ("cannot get the block %d of %s\n", i, filename);
	  goto fail;
	}

      if (current->start + current->len == block)
	current->len++;
      else
	{
	  struct block *b;

	  b = malloc (sizeof (*b));
	  if (! b)
	    {
	      printf ("cannot allocate memory\n");
	      goto fail;
	    }

	  b->start = block;
	  b->len = 1;
	  b->next = 0;
	  current->next = b;
	  current = b;
	}
    }

printf ("%s: ", filename);
for (current = head.next; current; current = current->next)

    {
      printf ("%d+%d", current->start, current->len);
      if (current->next)
	printf (",");
    }

printf (" (block size = %d)\n", blocksize);

fail:

  for (current = head.next; current; current = head.next)
    {
      head.next = current->next;
      free (current);
    }

if (fd >= 0)

    close (fd);
}

int
main (int argc, char *argv[])
{
if (argc < 2)

    {
      printf ("Display the list of blocks for given files\n");
      printf ("Usage: blklist FILE...\n");
      return 1;
    }
  while (*++argv)
    show_blklist (*argv);

return 0;
}

#1 lore raw
Quoted 5 lines · Yoshinori K. Okuji

Here is a simple utility to display a block list based on the knowledge of Linux's but not of GRUB's. I wrote this to aid the debugging of a filesystem. The output format is similar to GRUB's block list, but not completely identical (because GRUB always treats sectors instead of filesystem blocks). If you think this is useful, I can put this code into GRUB.

Perhaps we can turn it into a test program for automated testing?

--
Marco

#2 lore raw
Quoted 9 lines · Marco Gerards
Quoted 6 lines · Yoshinori K. Okuji

Here is a simple utility to display a block list based on the knowledge of Linux's but not of GRUB's. I wrote this to aid the debugging of a filesystem. The output format is similar to GRUB's block list, but not completely identical (because GRUB always treats sectors instead of filesystem blocks). If you think this is useful, I can put this code into GRUB.

Perhaps we can turn it into a test program for automated testing?

I'd appreciate if you can do it. It would be significantly useful to have regression tests, in particular for filesystems.

Okuji

#3 lore raw
Quoted 9 lines · Yoshinori K. Okuji
Quoted 8 lines · Marco Gerards
Quoted 6 lines · Yoshinori K. Okuji

Here is a simple utility to display a block list based on the knowledge of Linux's but not of GRUB's. I wrote this to aid the debugging of a filesystem. The output format is similar to GRUB's block list, but not completely identical (because GRUB always treats sectors instead of filesystem blocks). If you think this is useful, I can put this code into GRUB.

Quoted 4 lines
Quoted 1 line

Perhaps we can turn it into a test program for automated testing?

I'd appreciate if you can do it. It would be significantly useful to have regression tests, in particular for filesystems.

I completely agree. Perhaps I even created a task for this already? But I can not make any promise for the short term. My todo list is huge already.

--
Marco