- The lmfs_get_block*(3) API calls may now return an error. The idea is to encourage a next generation of file system services to do a better job at dealing with block read errors than the MFS-derived implementations do. These existing file systems have been changed to panic immediately upon getting a block read error, in order to let unchecked errors cause corruption. Note that libbdev already retries failing I/O operations a few times first. - The libminixfs block device I/O module (bio.c) now deals properly with end-of-file conditions on block devices. Since a device or partition size may not be a multiple of the root file system's block size, support for partial block retrival has been added, with a new internal lmfs_get_partial_block(3) call. A new test program, test85, tests the new handling of EOF conditions when reading, writing, and memory-mapping a block device. Change-Id: I05e35b6b8851488328a2679da635ebba0c6d08ce
		
			
				
	
	
		
			205 lines
		
	
	
		
			5.4 KiB
		
	
	
	
		
			C
		
	
	
	
	
	
			
		
		
	
	
			205 lines
		
	
	
		
			5.4 KiB
		
	
	
	
		
			C
		
	
	
	
	
	
/* Created (MFS based):
 | 
						|
 *   February 2010 (Evgeniy Ivanov)
 | 
						|
 */
 | 
						|
 | 
						|
#include "fs.h"
 | 
						|
#include "buf.h"
 | 
						|
#include "inode.h"
 | 
						|
#include "super.h"
 | 
						|
 | 
						|
 | 
						|
/*===========================================================================*
 | 
						|
 *				get_block				     *
 | 
						|
 *===========================================================================*/
 | 
						|
struct buf *get_block(dev_t dev, block_t block, int how)
 | 
						|
{
 | 
						|
/* Wrapper routine for lmfs_get_block(). This ext2 implementation does not deal
 | 
						|
 * well with block read errors pretty much anywhere. To prevent corruption due
 | 
						|
 * to unchecked error conditions, we panic upon an I/O failure here.
 | 
						|
 */
 | 
						|
  struct buf *bp;
 | 
						|
  int r;
 | 
						|
 | 
						|
  if ((r = lmfs_get_block(&bp, dev, block, how)) != OK && r != ENOENT)
 | 
						|
	panic("ext2: error getting block (%llu,%u): %d", dev, block, r);
 | 
						|
 | 
						|
  assert(r == OK || how == PEEK);
 | 
						|
 | 
						|
  return (r == OK) ? bp : NULL;
 | 
						|
}
 | 
						|
 | 
						|
 | 
						|
 | 
						|
/*===========================================================================*
 | 
						|
 *				conv2					     *
 | 
						|
 *===========================================================================*/
 | 
						|
unsigned conv2(norm, w)
 | 
						|
int norm;			/* TRUE if no swap, FALSE for byte swap */
 | 
						|
int w;				/* promotion of 16-bit word to be swapped */
 | 
						|
{
 | 
						|
/* Possibly swap a 16-bit word between 8086 and 68000 byte order. */
 | 
						|
  if (norm) return( (unsigned) w & 0xFFFF);
 | 
						|
  return( ((w&BYTE) << 8) | ( (w>>8) & BYTE));
 | 
						|
}
 | 
						|
 | 
						|
 | 
						|
/*===========================================================================*
 | 
						|
 *				conv4					     *
 | 
						|
 *===========================================================================*/
 | 
						|
long conv4(norm, x)
 | 
						|
int norm;			/* TRUE if no swap, FALSE for byte swap */
 | 
						|
long x;				/* 32-bit long to be byte swapped */
 | 
						|
{
 | 
						|
/* Possibly swap a 32-bit long between 8086 and 68000 byte order. */
 | 
						|
  unsigned lo, hi;
 | 
						|
  long l;
 | 
						|
 | 
						|
  if (norm) return(x);			/* byte order was already ok */
 | 
						|
  lo = conv2(FALSE, (int) x & 0xFFFF);	/* low-order half, byte swapped */
 | 
						|
  hi = conv2(FALSE, (int) (x>>16) & 0xFFFF);	/* high-order half, swapped */
 | 
						|
  l = ( (long) lo <<16) | hi;
 | 
						|
  return(l);
 | 
						|
}
 | 
						|
 | 
						|
 | 
						|
/*===========================================================================*
 | 
						|
 *				ansi_strcmp				     *
 | 
						|
 *===========================================================================*/
 | 
						|
int ansi_strcmp(register const char* ansi_s, register const char *s2,
 | 
						|
			register size_t ansi_s_length)
 | 
						|
{
 | 
						|
/* Compare non null-terminated string ansi_s (length=ansi_s_length)
 | 
						|
 * with C-string s2.
 | 
						|
 * It returns 0 if strings are equal, otherwise -1 is returned.
 | 
						|
 */
 | 
						|
	if (ansi_s_length) {
 | 
						|
		do {
 | 
						|
			if (*s2 == '\0')
 | 
						|
				return -1;
 | 
						|
			if (*ansi_s++ != *s2++)
 | 
						|
				return -1;
 | 
						|
		} while (--ansi_s_length > 0);
 | 
						|
 | 
						|
		if (*s2 == '\0')
 | 
						|
			return 0;
 | 
						|
		else
 | 
						|
			return -1;
 | 
						|
	}
 | 
						|
	return 0;
 | 
						|
}
 | 
						|
 | 
						|
 | 
						|
/*===========================================================================*
 | 
						|
 *				setbit   				     *
 | 
						|
 *===========================================================================*/
 | 
						|
bit_t setbit(bitchunk_t *bitmap, bit_t max_bits, unsigned int word)
 | 
						|
{
 | 
						|
  /* Find free bit in bitmap and set. Return number of the bit,
 | 
						|
   * if failed return -1.
 | 
						|
   */
 | 
						|
  bitchunk_t *wptr, *wlim;
 | 
						|
  bit_t b = -1;
 | 
						|
 | 
						|
  /* TODO: do we need to add 1? I saw a situation, when it was
 | 
						|
   * required, and since we check bit number with max_bits it
 | 
						|
   * should be safe.
 | 
						|
   */
 | 
						|
  wlim = &bitmap[FS_BITMAP_CHUNKS(max_bits >> 3)];
 | 
						|
 | 
						|
  /* Iterate over the words in block. */
 | 
						|
  for (wptr = &bitmap[word]; wptr < wlim; wptr++) {
 | 
						|
	bit_t i;
 | 
						|
	bitchunk_t k;
 | 
						|
 | 
						|
	/* Does this word contain a free bit? */
 | 
						|
	if (*wptr == (bitchunk_t) ~0)
 | 
						|
		continue;
 | 
						|
 | 
						|
	/* Find and allocate the free bit. */
 | 
						|
	k = (int) *wptr;
 | 
						|
	for (i = 0; (k & (1 << i)) != 0; ++i) {}
 | 
						|
 | 
						|
	/* Bit number from the start of the bit map. */
 | 
						|
	b = (wptr - &bitmap[0]) * FS_BITCHUNK_BITS + i;
 | 
						|
 | 
						|
	/* Don't allocate bits beyond the end of the map. */
 | 
						|
	if (b >= max_bits) {
 | 
						|
		b = -1;
 | 
						|
		continue;
 | 
						|
	}
 | 
						|
 | 
						|
	/* Allocate bit number. */
 | 
						|
	k |= 1 << i;
 | 
						|
	*wptr = (int) k;
 | 
						|
	break;
 | 
						|
  }
 | 
						|
 | 
						|
  return b;
 | 
						|
}
 | 
						|
 | 
						|
 | 
						|
/*===========================================================================*
 | 
						|
 *				setbyte   				     *
 | 
						|
 *===========================================================================*/
 | 
						|
bit_t setbyte(bitchunk_t *bitmap, bit_t max_bits)
 | 
						|
{
 | 
						|
  /* Find free byte in bitmap and set it. Return number of the starting bit,
 | 
						|
   * if failed return -1.
 | 
						|
   */
 | 
						|
  unsigned char *wptr, *wlim;
 | 
						|
  bit_t b = -1;
 | 
						|
 | 
						|
  wptr = (unsigned char*) &bitmap[0];
 | 
						|
  /* TODO: do we need to add 1? I saw a situation, when it was
 | 
						|
   * required, and since we check bit number with max_bits it
 | 
						|
   * should be safe.
 | 
						|
   */
 | 
						|
  wlim = &wptr[(max_bits >> 3)];
 | 
						|
 | 
						|
  /* Iterate over the words in block. */
 | 
						|
  for ( ; wptr < wlim; wptr++) {
 | 
						|
	/* Is it a free byte? */
 | 
						|
	if (*wptr | 0)
 | 
						|
		continue;
 | 
						|
 | 
						|
	/* Bit number from the start of the bit map. */
 | 
						|
	b = (wptr - (unsigned char*) &bitmap[0]) * CHAR_BIT;
 | 
						|
 | 
						|
	/* Don't allocate bits beyond the end of the map. */
 | 
						|
	if (b + CHAR_BIT >= max_bits) {
 | 
						|
		b = -1;
 | 
						|
		continue;
 | 
						|
	}
 | 
						|
 | 
						|
	/* Allocate byte number. */
 | 
						|
	*wptr = (unsigned char) ~0;
 | 
						|
	break;
 | 
						|
  }
 | 
						|
  return b;
 | 
						|
}
 | 
						|
 | 
						|
 | 
						|
/*===========================================================================*
 | 
						|
 *				unsetbit   				     *
 | 
						|
 *===========================================================================*/
 | 
						|
int unsetbit(bitchunk_t *bitmap, bit_t bit)
 | 
						|
{
 | 
						|
  /* Unset specified bit. If requested bit is already free return -1,
 | 
						|
   * otherwise return 0.
 | 
						|
   */
 | 
						|
  unsigned int word;		/* bit_returned word in bitmap */
 | 
						|
  bitchunk_t k, mask;
 | 
						|
 | 
						|
  word = bit / FS_BITCHUNK_BITS;
 | 
						|
  bit = bit % FS_BITCHUNK_BITS; /* index in word */
 | 
						|
  mask = 1 << bit;
 | 
						|
 | 
						|
  k = (int) bitmap[word];
 | 
						|
  if (!(k & mask))
 | 
						|
	return -1;
 | 
						|
 | 
						|
  k &= ~mask;
 | 
						|
  bitmap[word] = (int) k;
 | 
						|
  return 0;
 | 
						|
}
 |