All of lore.kernel.org
 help / color / mirror / Atom feed
From: "Yoshinori K. Okuji" <okuji@enbug.org>
To: grub-devel@gnu.org
Subject: blklist.c
Date: Sun, 30 Apr 2006 15:18:27 +0200	[thread overview]
Message-ID: <200604301518.29064.okuji@enbug.org> (raw)

[-- Attachment #1: Type: text/plain, Size: 371 bytes --]

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

[-- Attachment #2: blklist.c --]
[-- Type: text/x-csrc, Size: 2968 bytes --]

/* blklist.c - display block lists of files */
/*
 *  Copyright (C) 2006  Yoshinori Okuji <okuji@enbug.org>
 *
 *  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;
}

             reply	other threads:[~2006-04-30 13:18 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2006-04-30 13:18 Yoshinori K. Okuji [this message]
2006-04-30 15:49 ` blklist.c Marco Gerards
2006-04-30 16:31   ` blklist.c Yoshinori K. Okuji
2006-04-30 16:36     ` blklist.c Marco Gerards

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=200604301518.29064.okuji@enbug.org \
    --to=okuji@enbug.org \
    --cc=grub-devel@gnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.