diff -ruNwb /tmp/sos-code-article9/drivers/devices.h ../sos-code-article9.5/drivers/devices.h
--- /tmp/sos-code-article9/drivers/devices.h	2005-12-28 11:44:56.000000000 +0100
+++ ../sos-code-article9.5/drivers/devices.h	2006-01-22 12:48:28.000000000 +0100
@@ -53,4 +53,11 @@
 #define SOS_IOCTLPARAM_TTY_CANON          1
 #define SOS_IOCTLPARAM_TTY_ECHO           2
 
+/** IDE major */
+#define SOS_BLOCKDEV_IDE_MAJOR            1
+
+/** Generic hardrive IOCTL: flush caches to disk */
+#define SOS_IOCTL_BLOCKDEV_SYNC           1
+
+
 #endif /* _SOS_DEVICE_H_ */
diff -ruNwb /tmp/sos-code-article9/drivers/fs_virtfs.c ../sos-code-article9.5/drivers/fs_virtfs.c
--- /tmp/sos-code-article9/drivers/fs_virtfs.c	2005-12-28 11:44:55.000000000 +0100
+++ ../sos-code-article9.5/drivers/fs_virtfs.c	2006-01-22 12:48:28.000000000 +0100
@@ -97,9 +97,12 @@
       /* Yes, the file can be mapped ! */
       struct sos_umem_vmm_mapped_resource mapres;
       sos_count_t num_mappings; /**< Forbids the region to be resized
-				   while it's being mapped, because this
-				   woould cause the underlying data area
-				   to be moved */
+				   while it's being mapped, because
+				   this woould cause the underlying
+				   data area to be moved (because of
+				   the implementation we chose, a new
+				   allocation is made each time a file
+				   is resized) */
     } file;
 
     /* A directory */
@@ -511,7 +514,8 @@
   /* Establish the major/minor fields */
   result->st_rdev.device_class    = 0;
   result->st_rdev.device_instance = 0;
-  if (this->type == SOS_FS_NODE_DEVICE_CHAR)
+  if ( (this->type == SOS_FS_NODE_DEVICE_CHAR)
+       || (this->type == SOS_FS_NODE_DEVICE_BLOCK) )
     {
       /* If the current node is a special device: get the major/minor
 	 directly from it */
@@ -602,7 +606,8 @@
   (struct sos_fs_node_ops_file){
     .truncate = virtfs_truncate,
     .stat     = virtfs_stat_node,
-    .chmod    = virtfs_chmod_node
+    .chmod    = virtfs_chmod_node,
+    .sync     = virtfs_sync_node
   };
 
 
@@ -791,7 +796,8 @@
   if ((type != SOS_FS_NODE_REGULAR_FILE)
       && (type != SOS_FS_NODE_SYMLINK)
       && (type != SOS_FS_NODE_DIRECTORY)
-      && (type != SOS_FS_NODE_DEVICE_CHAR))
+      && (type != SOS_FS_NODE_DEVICE_CHAR)
+      && (type != SOS_FS_NODE_DEVICE_BLOCK))
     return -SOS_ENOSUP;
 
   virtfsnode = (struct virtfs_node*) sos_kmalloc(sizeof(*virtfsnode), 0);
@@ -812,9 +818,9 @@
 
   /* The "file" functions are defined by the FS code only for
      non-special device files */
-  if (type != SOS_FS_NODE_DEVICE_CHAR)
+  if ((type != SOS_FS_NODE_DEVICE_CHAR)
+      && (type != SOS_FS_NODE_DEVICE_BLOCK))
     {
-      (*result)->sync              = virtfs_sync_node;
       (*result)->new_opened_file   = virtfs_new_opened_file;
       (*result)->close_opened_file = virtfs_close_opened_file;
     }
diff -ruNwb /tmp/sos-code-article9/drivers/fs_virtfs.h ../sos-code-article9.5/drivers/fs_virtfs.h
--- /tmp/sos-code-article9/drivers/fs_virtfs.h	2005-12-28 11:44:56.000000000 +0100
+++ ../sos-code-article9.5/drivers/fs_virtfs.h	2006-01-22 12:48:28.000000000 +0100
@@ -35,6 +35,6 @@
 /**
  * Solely register the "virtfs" FS type into the list of supported FS types
  */
-sos_ret_t sos_fs_virtfs_subsystem_setup();
+sos_ret_t sos_fs_virtfs_subsystem_setup(void);
 
 #endif
diff -ruNwb /tmp/sos-code-article9/drivers/ide.c ../sos-code-article9.5/drivers/ide.c
--- /tmp/sos-code-article9/drivers/ide.c	1970-01-01 01:00:00.000000000 +0100
+++ ../sos-code-article9.5/drivers/ide.c	2006-01-22 12:48:28.000000000 +0100
@@ -0,0 +1,785 @@
+/* Copyright (C) 2005  Thomas Petazzoni, David Decotigny
+
+   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., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
+   USA.
+*/
+
+#include <sos/types.h>
+#include <sos/errno.h>
+#include <sos/assert.h>
+#include <sos/ksynch.h>
+#include <drivers/devices.h>
+#include <drivers/bochs.h>
+#include <drivers/part.h>
+#include <hwcore/irq.h>
+#include <hwcore/ioports.h>
+
+
+/**
+ * @file ide.c
+ *
+ * Basic PIO IDE implementation based on the ATA standards
+ * http://www.t13.org/
+ */
+
+
+/**
+ * Busy-wait for a given time
+ *
+ * @param delay Delay to wait
+ *
+ * @todo Implement a calibration routine
+ */
+static void udelay(int delay)
+{
+  int i;
+
+  for (i = 0; i < (delay * 1000) ; i++)
+    {
+      i++; i--;
+    }
+}
+
+/*
+ * Each IDE controller is controlled through a set of 9 I/O ports,
+ * starting from a base address, which is different for each IDE
+ * controller. On a standard PC with two onboard IDE controllers, the
+ * I/O registers of the first controller start at 0x1F0
+ * (IDE_CONTROLLER_0_BASE), the I/O registers of the second controller
+ * start at 0x170 (IDE_CONTROLLER_1_BASE). These I/O registers are
+ * 8-bits wide, and can be read using the inb() macro, and written
+ * using the outb() macro.
+ *
+ * The first controller is generally known as "primary" controller,
+ * and the second one is know as "secondary" controller. Each of them
+ * can handle at most two devices : the "master" device and the
+ * "slave" device.
+ *
+ * The registers can be used to issue commands and send data to the
+ * IDE controller, for example to identify a device, to read or write
+ * a device. Then are different ways of transmitting data to the IDE
+ * controller :
+ *
+ * - program the IDE controller, send the data, and wait for the
+ *   completion of the request. This method is called "polling".
+ *
+ * - program the IDE controller, send the data, block the current
+ *   process and do something else. The completion of the request will
+ *   be signaled asynchronously by an interrupt (IRQ), which will
+ *   allow to restart the sleeping process.
+ *
+ * - program the IDE controller and the DMA controller. There's no
+ *   need to transfer the data to the IDE controller : the DMA
+ *   controller will do it. This allows to use the CPU to do something
+ *   useful during the transfer of data between the main memory and
+ *   the IDE controller.
+ *
+ * In this driver, we use the two first methods. The polling method is
+ * used to fetch the identification of the devices, while the IRQ
+ * method is used to read and write to devices.
+ */
+
+#define IDE_CONTROLLER_0_BASE           0x1F0
+#define IDE_CONTROLLER_1_BASE           0x170
+
+#define IDE_CONTROLLER_0_IRQ            14
+#define IDE_CONTROLLER_1_IRQ            15
+
+/*
+ * All defines below are relative to the base address of the I/O
+ * registers (IDE_CONTROLLER_0_BASE and IDE_CONTROLLER_1_BASE)
+ */
+
+/**
+ * Read/write register that allows to transfer the data to be written
+ * or to fetch the read data from the IDE controller.
+ */
+#define ATA_DATA                        0x00
+
+/**
+ * Read only register that gives information about errors that occured
+ * during operation
+ */
+#define ATA_ERROR                       0x01
+
+/**
+ * Write only register to set precompensation. It is in fact the same
+ * register as the ATA_ERROR register. It simply has a different
+ * behaviour when reading and writing it.
+ */
+#define ATA_PRECOMP                     0x01
+
+/**
+ * Write only register used to set the count of sectors on which the
+ * request applies.
+ */
+#define ATA_SECTOR_COUNT                0x02
+
+/**
+ * Write only register used to set the number of the starting sector
+ * of the request.
+ */
+#define ATA_SECTOR_NUMBER               0x03
+
+/**
+ * Write only register used to set the 8 lower bits of the starting
+ * cylinder number of the request
+ */
+#define ATA_CYL_LSB                     0x04
+
+/**
+ * Write only register used to set the 8 higher bits of the starting
+ * cylinder number of the request
+ */
+#define ATA_CYL_MSB                     0x05
+
+/**
+ * Write only register that allows to select whether the LBA mode
+ * should be used, and to select whether the request concerns the
+ * slave or master device.
+ */
+#define ATA_DRIVE                       0x06
+#define         ATA_D_IBM               0xa0    /* bits that must be set */
+#define         ATA_D_LBA               0x40    /* use LBA ? */
+#define         ATA_D_MASTER            0x00    /* select master */
+#define         ATA_D_SLAVE             0x10    /* select slave */
+
+/**
+ * Read only register that contains the status of the controller. Each
+ * bit of this register as a different signification.
+ */
+#define ATA_STATUS                      0x07
+#define         ATA_S_ERROR             0x01    /* error */
+#define         ATA_S_INDEX             0x02    /* index */
+#define         ATA_S_CORR              0x04    /* data corrected */
+#define         ATA_S_DRQ               0x08    /* data request */
+#define         ATA_S_DSC               0x10    /* drive Seek Completed */
+#define         ATA_S_DWF               0x20    /* drive write fault */
+#define         ATA_S_DRDY              0x40    /* drive ready */
+#define         ATA_S_BSY               0x80    /* busy */
+
+/**
+ * Write only register used to set the command that the IDE controller
+ * should process. In our driver, only ATA_C_ATA_IDENTIFY, ATA_C_READ
+ * and ATA_C_WRITE are used.
+ */
+#define ATA_CMD                         0x07
+#define         ATA_C_ATA_IDENTIFY      0xec    /* get ATA params */
+#define         ATA_C_ATAPI_IDENTIFY    0xa1    /* get ATAPI params*/
+#define         ATA_C_READ              0x20    /* read command */
+#define         ATA_C_WRITE             0x30    /* write command */
+#define         ATA_C_READ_MULTI        0xc4    /* read multi command */
+#define         ATA_C_WRITE_MULTI       0xc5    /* write multi command */
+#define         ATA_C_SET_MULTI         0xc6    /* set multi size command */
+#define         ATA_C_PACKET_CMD        0xa0    /* set multi size command */
+
+/**
+ * Read register that contains more information about the status of
+ * the controller
+ */
+#define ATA_ALTPORT                     0x206   /* (R) alternate
+                                                   Status register */
+
+/**
+ * Write only register that allows to control the controller
+ */
+#define ATA_DEVICE_CONTROL              0x206   /* (W) device control
+                                                   register */
+#define         ATA_A_nIEN              0x02    /* disable interrupts */
+#define         ATA_A_RESET             0x04    /* RESET controller */
+#define         ATA_A_4BIT              0x08    /* 4 head bits */
+
+/** Magic numbers used to detect ATAPI devices */
+#define ATAPI_MAGIC_LSB                 0x14
+#define ATAPI_MAGIC_MSB                 0xeb
+
+/* This structure describe the informations returned by the Identify
+   Device command (imposed by the ATA standard) */
+struct ide_device_info
+{
+  sos_ui16_t general_config_info;      /* 0 */
+  sos_ui16_t nb_logical_cylinders;     /* 1 */
+  sos_ui16_t reserved1;                /* 2 */
+  sos_ui16_t nb_logical_heads;         /* 3 */
+  sos_ui16_t unformatted_bytes_track;  /* 4 */
+  sos_ui16_t unformatted_bytes_sector; /* 5 */
+  sos_ui16_t nb_logical_sectors;       /* 6 */
+  sos_ui16_t vendor1[3];               /* 7-9 */
+  sos_ui8_t  serial_number[20];        /* 10-19 */
+  sos_ui16_t buffer_type;              /* 20 */
+  sos_ui16_t buffer_size;              /* 21 */
+  sos_ui16_t ecc_bytes;                /* 22 */
+  sos_ui8_t  firmware_revision[8];     /* 23-26 */
+  sos_ui8_t  model_number[40];         /* 27-46 */
+  sos_ui8_t  max_multisect;            /* 47 */
+  sos_ui8_t  vendor2;
+  sos_ui16_t dword_io;                 /* 48 */
+  sos_ui8_t  vendor3;                  /* 49 */
+  sos_ui8_t  capabilities;
+  sos_ui16_t reserved2;                /* 50 */
+  sos_ui8_t  vendor4;                  /* 51 */
+  sos_ui8_t  pio_trans_mode;
+  sos_ui8_t  vendor5;                  /* 52 */
+  sos_ui8_t  dma_trans_mode;
+  sos_ui16_t fields_valid;             /* 53 */
+  sos_ui16_t cur_logical_cylinders;    /* 54 */
+  sos_ui16_t cur_logical_heads;        /* 55 */
+  sos_ui16_t cur_logical_sectors;      /* 56 */
+  sos_ui16_t capacity1;                /* 57 */
+  sos_ui16_t capacity0;                /* 58 */
+  sos_ui8_t  multsect;                 /* 59 */
+  sos_ui8_t  multsect_valid;
+  sos_ui32_t lba_capacity;             /* 60-61 */
+  sos_ui16_t dma_1word;                /* 62 */
+  sos_ui16_t dma_multiword;            /* 63 */
+  sos_ui16_t pio_modes;                /* 64 */
+  sos_ui16_t min_mword_dma;            /* 65 */
+  sos_ui16_t recommended_mword_dma;    /* 66 */
+  sos_ui16_t min_pio_cycle_time;       /* 67 */
+  sos_ui16_t min_pio_cycle_time_iordy; /* 68 */
+  sos_ui16_t reserved3[11];            /* 69-79 */
+  sos_ui16_t major_version;            /* 80 */
+  sos_ui16_t minor_version;            /* 81 */
+  sos_ui16_t command_sets1;            /* 82 */
+  sos_ui16_t command_sets2;            /* 83 dixit lk : b14 (smart enabled) */
+  sos_ui16_t reserved4[4];             /* 84-87 */
+  sos_ui16_t dma_ultra;                /* 88 dixit lk */
+  sos_ui16_t reserved5[37];            /* 89-125 */
+  sos_ui16_t last_lun;                 /* 126 */
+  sos_ui16_t reserved6;                /* 127 */
+  sos_ui16_t security;                 /* 128 */
+  sos_ui16_t reserved7[127];
+} __attribute__((packed));
+
+#define MAX_IDE_CONTROLLERS 2
+#define MAX_IDE_DEVICES     2
+
+#define IDE_BLK_SIZE        512
+
+#define IDE_DEVICE(ctrl,device) (((ctrl) * 2) + (device))
+#define IDE_MINOR(ctrl,device)  (IDE_DEVICE(ctrl,device)*16)
+
+typedef enum { IDE_DEVICE_NONE,
+	       IDE_DEVICE_HARDDISK,
+	       IDE_DEVICE_CDROM } ide_device_type_t;
+
+struct ide_controller;
+struct ide_device {
+  int id;
+  ide_device_type_t type;
+  enum { IDE_DEVICE_MASTER, IDE_DEVICE_SLAVE } position;
+  int cyls;
+  int heads;
+  int sectors;
+  int blocks;
+  sos_bool_t support_lba;
+  struct ide_controller *ctrl;
+};
+
+struct ide_controller {
+  int id;
+  int ioaddr;
+  int irq;
+  enum { IDE_CTRL_NOT_PRESENT, IDE_CTRL_PRESENT } state;
+  struct ide_device devices[MAX_IDE_DEVICES];
+  struct sos_kmutex mutex;
+
+  struct sos_ksema  ack_io_sem;
+};
+
+struct ide_controller ide_controllers[MAX_IDE_CONTROLLERS] = {
+  {
+    .id     = 0,
+    .ioaddr = IDE_CONTROLLER_0_BASE,
+    .irq    = IDE_CONTROLLER_0_IRQ,
+    .state  = IDE_CTRL_NOT_PRESENT,
+  },
+  {
+    .id     = 1,
+    .ioaddr = IDE_CONTROLLER_1_BASE,
+    .irq    = IDE_CONTROLLER_1_IRQ,
+    .state  = IDE_CTRL_NOT_PRESENT
+  }
+};
+
+void ide_irq_handler (int irq_level)
+{
+  int i;
+  struct ide_controller *ctrl = NULL;
+
+  for (i = 0; i < MAX_IDE_CONTROLLERS ; i++)
+    {
+      if (ide_controllers[i].irq == irq_level)
+	{
+	  ctrl = & ide_controllers[i];
+	  break;
+	}
+    }
+
+  SOS_ASSERT_FATAL (ctrl != NULL);
+
+  sos_ksema_up (& ctrl->ack_io_sem);
+}
+
+static int ide_get_device_info (struct ide_device *dev)
+{
+  int devselect;
+  sos_ui16_t buffer[256];
+  struct ide_device_info *info;
+  int timeout, i;
+  int status;
+
+  SOS_ASSERT_FATAL (dev->type == IDE_DEVICE_HARDDISK);
+
+  if (dev->position == IDE_DEVICE_MASTER)
+    devselect = ATA_D_MASTER;
+  else
+    devselect = ATA_D_SLAVE;
+
+  /* Ask the controller to NOT send interrupts to acknowledge
+     commands */
+  outb(ATA_A_nIEN | ATA_A_4BIT, dev->ctrl->ioaddr + ATA_DEVICE_CONTROL);
+  udelay(1);
+
+  /* Select the device (master or slave) */
+  outb(ATA_D_IBM | devselect, dev->ctrl->ioaddr + ATA_DRIVE);
+
+  /* Send the IDENTIFY command */
+  outb(ATA_C_ATA_IDENTIFY, dev->ctrl->ioaddr + ATA_CMD);
+
+  /* Wait for command completion (wait while busy bit is set) */
+  for(timeout = 0; timeout < 30000; timeout++)
+    {
+      status = inb(dev->ctrl->ioaddr + ATA_STATUS);
+      if(!(status & ATA_S_BSY))
+	break;
+
+      udelay(1);
+    }
+
+  /* DRQ bit indicates that data is ready to be read. If it is not set
+     after an IDENTIFY command, there is a problem */
+  if(! (status & ATA_S_DRQ))
+    {
+      return SOS_EFATAL;
+    }
+
+  /* Read data from the controller buffer to a temporary buffer */
+  for(i = 0; i < 256; i++)
+    buffer[i] = inw(dev->ctrl->ioaddr + ATA_DATA);
+
+  /* The buffer contains an information structure, defined by ATA
+     specification. To ease its access, we use the previously defined
+     ide_device_info structure. */
+  info = (struct ide_device_info *) buffer;
+
+  /* Fetch intersting informations from the structure */
+  dev->heads   = info->nb_logical_heads;
+  dev->cyls    = info->nb_logical_cylinders;
+  dev->sectors = info->nb_logical_sectors;
+
+  dev->support_lba = FALSE;
+
+  /* Determines if device supports LBA. The method is a bit ugly, but
+     there's no other way */
+  if (info->capabilities & (1 << 1)
+      && info->major_version
+      && (info->fields_valid & 1)
+      && (info->lba_capacity & 1))
+    dev->support_lba = TRUE;
+
+  /* Determines the capacity of the device */
+  if (dev->heads   == 16 &&
+      dev->sectors == 63 &&
+      dev->cyls    == 16383)
+    {
+      if (dev->support_lba)
+	dev->blocks = info->lba_capacity;
+      else
+	return SOS_EFATAL;
+    }
+  else
+    dev->blocks = dev->cyls * dev->sectors * dev->heads;
+
+  return SOS_OK;
+}
+
+static ide_device_type_t ide_probe_device (struct ide_device *dev)
+{
+  int status;
+  int devselect;
+
+  if (dev->position == IDE_DEVICE_MASTER)
+    devselect = ATA_D_MASTER;
+  else
+    devselect = ATA_D_SLAVE;
+
+  /* Select the given device */
+  outb (ATA_D_IBM | devselect, dev->ctrl->ioaddr + ATA_DRIVE);
+
+  /* Read the status of the device */
+  status = inb(dev->ctrl->ioaddr + ATA_STATUS);
+
+  /* If status indicates a busy device, it means that there's no
+     device */
+  if (status & ATA_S_BSY)
+    return IDE_DEVICE_NONE;
+
+  /* If status indicates that drive is ready and drive has complete
+     seeking, then we've got an hard drive */
+  if (status & (ATA_S_DRDY | ATA_S_DSC))
+    return IDE_DEVICE_HARDDISK;
+
+  /* Detect CD-ROM drives by reading the cylinder low byte and
+     cylinder high byte, and check if they match magic values */
+  if(inb(dev->ctrl->ioaddr + ATA_CYL_LSB) == ATAPI_MAGIC_LSB &&
+     inb(dev->ctrl->ioaddr + ATA_CYL_MSB) == ATAPI_MAGIC_MSB)
+    return IDE_DEVICE_CDROM;
+
+  return IDE_DEVICE_NONE;
+}
+
+static sos_ret_t ide_probe_controller(struct ide_controller *ctrl)
+{
+  sos_bool_t hasdevice = FALSE;
+
+  sos_kmutex_init (& ctrl->mutex, "ide-mutex", SOS_KWQ_ORDER_FIFO);
+  sos_ksema_init  (& ctrl->ack_io_sem, "ide-sem", 0, SOS_KWQ_ORDER_FIFO);
+
+  /* Master */
+  ctrl->devices[0].id       = 0;
+  ctrl->devices[0].position = IDE_DEVICE_MASTER;
+  ctrl->devices[0].ctrl     = ctrl;
+  ctrl->devices[0].type     = ide_probe_device (& ctrl->devices[0]);
+
+  if (ctrl->devices[0].type == IDE_DEVICE_HARDDISK)
+    {
+      ide_get_device_info (& ctrl->devices[0]);
+      hasdevice = TRUE;
+    }
+
+  /* Slave */
+  ctrl->devices[1].id       = 1;
+  ctrl->devices[1].position = IDE_DEVICE_SLAVE;
+  ctrl->devices[1].ctrl     = ctrl;
+  ctrl->devices[1].type     = ide_probe_device (& ctrl->devices[1]);
+
+  if (ctrl->devices[1].type == IDE_DEVICE_HARDDISK)
+    {
+      ide_get_device_info (& ctrl->devices[1]);
+      hasdevice = TRUE;
+    }
+
+  if (hasdevice)
+    sos_irq_set_routine (ctrl->irq, ide_irq_handler);
+
+  return SOS_OK;
+}
+
+
+static sos_ret_t ide_io_operation (struct ide_device *dev,
+				   sos_vaddr_t buf, int block,
+				   sos_bool_t iswrite)
+{
+  sos_ui8_t cyl_lo, cyl_hi, sect, head, status;
+  int devselect, i;
+
+  SOS_ASSERT_FATAL (dev->type == IDE_DEVICE_HARDDISK);
+
+  if (block > dev->blocks)
+    return -SOS_EFATAL;
+
+  if (dev->position == IDE_DEVICE_MASTER)
+    devselect = ATA_D_MASTER;
+  else
+    devselect = ATA_D_SLAVE;
+
+  /* Compute the position of the block in the device in terms of
+     cylinders, sectors and heads. As cylinders must be sent in two
+     separate parts, we compute it that way. */
+  if (dev->support_lba)
+    {
+      sect   = (block & 0xff);
+      cyl_lo = (block >> 8) & 0xff;
+      cyl_hi = (block >> 16) & 0xff;
+      head   = ((block >> 24) & 0x7) | 0x40;
+    }
+  else
+    {
+      int cylinder = block /
+	(dev->heads * dev->sectors);
+      int temp = block %
+	(dev->heads * dev->sectors);
+      cyl_lo = cylinder & 0xff;
+      cyl_hi = (cylinder >> 8) & 0xff;
+      head   = temp / dev->sectors;
+      sect   = (temp % dev->sectors) + 1;
+    }
+
+  /* Make sure nobody is using the same controller at the same time */
+  sos_kmutex_lock (& dev->ctrl->mutex, NULL);
+
+  /* Select device */
+  outb(ATA_D_IBM | devselect, dev->ctrl->ioaddr + ATA_DRIVE);
+  udelay(100);
+
+  /* Write to registers */
+  outb(ATA_A_4BIT, dev->ctrl->ioaddr + ATA_DEVICE_CONTROL);
+  outb(1,          dev->ctrl->ioaddr + ATA_ERROR);
+  outb(0,          dev->ctrl->ioaddr + ATA_PRECOMP);
+  outb(1,          dev->ctrl->ioaddr + ATA_SECTOR_COUNT);
+  outb(sect,       dev->ctrl->ioaddr + ATA_SECTOR_NUMBER);
+  outb(cyl_lo,     dev->ctrl->ioaddr + ATA_CYL_LSB);
+  outb(cyl_hi,     dev->ctrl->ioaddr + ATA_CYL_MSB);
+  outb((ATA_D_IBM | devselect | head),
+       dev->ctrl->ioaddr + ATA_DRIVE);
+
+  /* Send the command, either read or write */
+  if (iswrite)
+    outb(ATA_C_WRITE, dev->ctrl->ioaddr + ATA_CMD);
+  else
+    outb(ATA_C_READ, dev->ctrl->ioaddr + ATA_CMD);
+
+  /* Wait for the device ready to transfer */
+  do { udelay(1); } while (inb(dev->ctrl->ioaddr + ATA_STATUS) & ATA_S_BSY);
+
+  /* If an error was detected, stop here */
+  if (inb(dev->ctrl->ioaddr + ATA_STATUS) & ATA_S_ERROR)
+    {
+      sos_kmutex_unlock (& dev->ctrl->mutex);
+      return -SOS_EFATAL;
+    }
+
+  /* If it's a write I/O, transfer the contents of the buffer provided
+     by the user to the controller internal buffer, so that the
+     controller can write the data to the disk */
+  if (iswrite)
+    {
+      /* Wait for the DRQ bit to be set */
+      while (1)
+	{
+	  status = inb(dev->ctrl->ioaddr + ATA_STATUS);
+	  if (status & ATA_S_ERROR)
+	    {
+	      sos_kmutex_unlock (& dev->ctrl->mutex);
+	      return -SOS_EFATAL;
+	    }
+	    
+	  if (!(status & ATA_S_BSY) && (status & ATA_S_DRQ))
+	    break;
+	}
+
+      /* Do the transfer to the device's buffer */
+      sos_ui16_t *buffer = (sos_ui16_t *) buf;
+      for (i = 0 ; i < 256 ; i++)
+	outw (buffer[i], dev->ctrl->ioaddr + ATA_DATA);
+
+      /* Wait for the device to have received all the data */
+      while (1)
+	{
+	  status = inb(dev->ctrl->ioaddr + ATA_STATUS);
+	  if (status & ATA_S_ERROR)
+	    {
+	      sos_kmutex_unlock (& dev->ctrl->mutex);
+	      return -SOS_EFATAL;
+	    }
+	    
+	  if (!(status & ATA_S_BSY) && !(status & ATA_S_DRQ))
+	    break;
+	}
+    }
+
+  /* Sleep until the IRQ wakes us up */
+  sos_ksema_down (& dev->ctrl->ack_io_sem, NULL);
+
+  /* ATA specs tell to read the alternate status reg and ignore its
+     result */
+  inb(dev->ctrl->ioaddr + ATA_ALTPORT);
+
+  if (! iswrite)
+    {
+      /* Wait for the DRQ bit to be set */
+      while (1)
+	{
+	  status = inb(dev->ctrl->ioaddr + ATA_STATUS);
+	  if (status & ATA_S_ERROR)
+	    {
+	      sos_kmutex_unlock (& dev->ctrl->mutex);
+	      return -SOS_EFATAL;
+	    }
+	    
+	  if (!(status & ATA_S_BSY) && (status & ATA_S_DRQ))
+	    break;
+	}
+
+      /* copy data from the controller internal buffer to the buffer
+	 provided by the user */
+      sos_ui16_t *buffer = (sos_ui16_t *) buf;
+      for (i = 0 ; i < 256 ; i++)
+	buffer [i] = inw (dev->ctrl->ioaddr + ATA_DATA);
+
+      /* ATA specs tell to read the alternate status reg and ignore its
+	 result */
+      inb(dev->ctrl->ioaddr + ATA_ALTPORT);
+    }
+
+  /* If an error was detected, stop here */
+  if (inb(dev->ctrl->ioaddr + ATA_STATUS) & ATA_S_ERROR)
+    {
+      sos_kmutex_unlock (& dev->ctrl->mutex);
+      return -SOS_EFATAL;
+    }
+
+  /* Someone else can safely use devices on this controller for other
+     requests */
+  sos_kmutex_unlock (& dev->ctrl->mutex);
+  return SOS_OK;
+}
+
+static sos_ret_t
+ide_read_device (void *blkdev_instance, sos_vaddr_t dest_buf,
+		 sos_luoffset_t block_offset)
+{
+  struct ide_device *dev;
+
+  dev = (struct ide_device *) blkdev_instance;
+
+  return ide_io_operation (dev, dest_buf, block_offset, FALSE);
+}
+
+static sos_ret_t
+ide_write_device (void *blkdev_instance, sos_vaddr_t src_buf,
+		  sos_luoffset_t block_offset)
+{
+  struct ide_device *dev;
+
+  dev = (struct ide_device *) blkdev_instance;
+
+  return ide_io_operation (dev, src_buf, block_offset, TRUE);
+}
+
+static struct sos_blockdev_operations ide_ops = {
+  .read_block  = ide_read_device,
+  .write_block = ide_write_device,
+  .ioctl       = NULL
+};
+
+
+static sos_ret_t
+ide_register_devices (void)
+{
+  int ctrl, dev;
+  sos_ret_t ret;
+
+  for (ctrl = 0; ctrl < MAX_IDE_CONTROLLERS ; ctrl++)
+    {
+      for (dev = 0; dev < MAX_IDE_DEVICES ; dev++)
+	{
+	  char name[16];
+	  struct ide_device *device;
+
+	  device = & ide_controllers[ctrl].devices[dev];
+
+	  snprintf (name, sizeof(name), "hd%c", ('a' + IDE_DEVICE(ctrl, dev)));
+
+	  if (device->type == IDE_DEVICE_HARDDISK)
+	    {
+	      sos_bochs_printf("%s: harddisk %d Mb <", name,
+			       (device->blocks * IDE_BLK_SIZE >> 20));
+	      ret = sos_blockdev_register_disk (SOS_BLOCKDEV_IDE_MAJOR,
+						IDE_MINOR(ctrl, dev),
+						IDE_BLK_SIZE, device->blocks,
+						128, & ide_ops, device);
+	      if (ret != SOS_OK)
+		{
+		  sos_bochs_printf ("Warning: could not register disk>\n");
+		  continue;
+		}
+
+	      ret = sos_part_detect (SOS_BLOCKDEV_IDE_MAJOR,
+				     IDE_MINOR(ctrl, dev), IDE_BLK_SIZE,
+				     name);
+	      if (ret != SOS_OK)
+		{
+		  sos_bochs_printf ("Warning could not detect partitions (%d)>\n",
+				    ret);
+		  continue;
+		}
+
+	      /* Finalize output */
+	      sos_bochs_printf (">\n");
+	    }
+
+	  else if (device->type == IDE_DEVICE_CDROM)
+	    sos_bochs_printf("%s: CDROM\n", name);
+	}
+    }
+
+  return SOS_OK;
+}
+
+static sos_ret_t
+ide_unregister_devices (void)
+{
+  int ctrl, dev;
+  sos_ret_t ret;
+
+  for (ctrl = 0; ctrl < MAX_IDE_CONTROLLERS ; ctrl++)
+    {
+      for (dev = 0; dev < MAX_IDE_DEVICES ; dev++)
+	{
+	  struct ide_device *device;
+
+	  device = & ide_controllers[ctrl].devices[dev];
+
+	  if (device->type != IDE_DEVICE_HARDDISK)
+	    continue;
+
+	  sos_part_undetect (SOS_BLOCKDEV_IDE_MAJOR, IDE_MINOR(ctrl,dev));
+
+	  ret = sos_blockdev_unregister_device (SOS_BLOCKDEV_IDE_MAJOR,
+						IDE_MINOR(ctrl,dev));
+	  if (ret != SOS_OK)
+	    sos_bochs_printf ("Error while unregistering device %d:%d\n",
+			      SOS_BLOCKDEV_IDE_MAJOR, IDE_MINOR(ctrl,dev));
+	}
+    }
+
+  return SOS_OK;
+}
+
+
+sos_ret_t sos_ide_subsystem_setup (void)
+{
+  sos_ret_t ret;
+
+  ret = ide_probe_controller(& ide_controllers[0]);
+  if (ret != SOS_OK)
+    sos_bochs_printf ("Error while probing IDE controller 0\n");
+
+  ret =ide_probe_controller(& ide_controllers[1]);
+  if (ret != SOS_OK)
+    sos_bochs_printf ("Error while probing IDE controller 1\n");
+
+  ide_register_devices ();
+
+  return SOS_OK;
+}
+
+sos_ret_t ide_driver_cleanup (void)
+{
+  return ide_unregister_devices ();
+}
+
diff -ruNwb /tmp/sos-code-article9/drivers/ide.h ../sos-code-article9.5/drivers/ide.h
--- /tmp/sos-code-article9/drivers/ide.h	1970-01-01 01:00:00.000000000 +0100
+++ ../sos-code-article9.5/drivers/ide.h	2006-01-22 12:48:28.000000000 +0100
@@ -0,0 +1,32 @@
+/* Copyright (C) 2005  Thomas Petazzoni
+
+   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., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
+   USA.
+*/
+#ifndef _SOS_DRV_IDE_H_
+#define _SOS_DRV_IDE_H_
+
+
+/**
+ * @file ide.h
+ *
+ * Basic PIO IDE implementation based on the ATA 3 & 4 standards
+ * http://www.t13.org/
+ */
+
+sos_ret_t sos_ide_subsystem_setup (void);
+
+
+#endif /* _SOS_DRV_IDE_H_ */
diff -ruNwb /tmp/sos-code-article9/drivers/mem.c ../sos-code-article9.5/drivers/mem.c
--- /tmp/sos-code-article9/drivers/mem.c	2005-12-28 11:44:56.000000000 +0100
+++ ../sos-code-article9.5/drivers/mem.c	2006-01-22 12:48:28.000000000 +0100
@@ -418,7 +418,7 @@
 
       accesslen += retval;
       /* If transfer was interrupted, stop here */
-      if (retval < accesslen_in_page)
+      if (retval < (sos_ret_t)accesslen_in_page)
 	break;
 
       /* Go on to next page */
diff -ruNwb /tmp/sos-code-article9/drivers/mem.h ../sos-code-article9.5/drivers/mem.h
--- /tmp/sos-code-article9/drivers/mem.h	2005-12-28 11:44:56.000000000 +0100
+++ ../sos-code-article9.5/drivers/mem.h	2006-01-22 12:48:28.000000000 +0100
@@ -31,6 +31,6 @@
 /**
  * Bind to the /dev/kmem and /dev/mem character devices
  */
-sos_ret_t sos_dev_mem_chardev_setup();
+sos_ret_t sos_dev_mem_chardev_setup(void);
 
 #endif /* _SOS_EXEC_ELF32_H_ */
diff -ruNwb /tmp/sos-code-article9/drivers/part.c ../sos-code-article9.5/drivers/part.c
--- /tmp/sos-code-article9/drivers/part.c	1970-01-01 01:00:00.000000000 +0100
+++ ../sos-code-article9.5/drivers/part.c	2006-01-22 12:48:28.000000000 +0100
@@ -0,0 +1,216 @@
+/* Copyright (C) 2005  Thomas Petazzoni
+
+   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., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
+   USA.
+*/
+
+#include <sos/kmalloc.h>
+#include <sos/blkdev.h>
+#include <drivers/bochs.h>
+
+/**
+ * This structure defines the structure of a partition entry
+ * (determined by the PC architecture)
+ */
+typedef struct partition_entry
+{
+  sos_ui8_t  active;
+  sos_ui8_t  start_dl;
+  sos_ui16_t start_cylinder;
+  sos_ui8_t  type;
+  sos_ui8_t  end_dl;
+  sos_ui16_t end_cylinder;
+  sos_ui32_t lba;
+  sos_ui32_t size;
+} partition_entry_t;
+
+/**
+ * Offset of the partition table inside the 512-byte sector.
+ */
+#define PART_TABLE_OFFSET 446
+
+/**
+ * The most common partition types. For a complete list, you can for
+ * example refer to
+ * http://www.win.tue.nl/~aeb/partitions/partition_types-1.html
+ */
+#define PART_TYPE_EXTENDED   0x5
+#define PART_TYPE_FAT16_1    0xe
+#define PART_TYPE_FAT16_2    0x6
+#define PART_TYPE_FAT32_1    0xb
+#define PART_TYPE_FAT32_2    0xc
+#define PART_TYPE_LINUX_SWAP 0x82
+#define PART_TYPE_LINUX      0x83
+
+/**
+ * Converts a partition type to a string
+ */
+static const char *
+sos_part_type_str (unsigned int type)
+{
+  switch (type)
+    {
+    case PART_TYPE_EXTENDED:
+      return "Extended";
+    case PART_TYPE_FAT16_1:
+    case PART_TYPE_FAT16_2:
+      return "FAT16";
+    case PART_TYPE_FAT32_1:
+    case PART_TYPE_FAT32_2:
+      return "FAT32";
+    case PART_TYPE_LINUX_SWAP:
+      return "Linux Swap";
+    case PART_TYPE_LINUX:
+      return "Linux";
+    default:
+      return "Unknown";
+    }
+}
+
+/**
+ * Detect the partitions on the given device, and registers them to
+ * the block device infrastructure.
+ */
+sos_ret_t
+sos_part_detect (sos_ui32_t disk_class, sos_ui32_t disk_instance,
+		 sos_size_t block_size, const char *name)
+{
+  sos_vaddr_t buffer;
+  struct sos_blockdev_instance *blkdev;
+  struct partition_entry *part_entry;
+  unsigned int extstart = 0, extsup = 0;
+  sos_ret_t ret;
+  sos_size_t size = block_size;
+  unsigned int partnum;
+
+  blkdev = sos_blockdev_ref_instance (disk_class, disk_instance);
+  if (blkdev == NULL)
+    return -SOS_ENOENT;
+
+  buffer = (sos_vaddr_t) sos_kmalloc (block_size, 0);
+  if (buffer == 0)
+    {
+      sos_blockdev_release_instance (blkdev);
+      return -SOS_ENOMEM;
+    }
+
+  ret = sos_blockdev_kernel_read (blkdev, 0, buffer, & size);
+  if (ret != SOS_OK)
+    {
+      sos_blockdev_release_instance (blkdev);
+      sos_kfree (buffer);
+      return ret;
+    }
+
+  if (size != block_size)
+    {
+      sos_blockdev_release_instance (blkdev);
+      sos_kfree (buffer);
+      return -SOS_EIO;
+    }
+
+  part_entry = (struct partition_entry *) (buffer + PART_TABLE_OFFSET);
+
+  /* Handle primary partitions */
+  for (partnum = 0; partnum < 4; partnum++)
+    {
+      if (part_entry [partnum].size == 0)
+	continue;
+
+      sos_bochs_printf ("%s%d (%lu Mb, %s) ", name, partnum+1,
+			(part_entry[partnum].size * block_size >> 20),
+			sos_part_type_str(part_entry[partnum].type));
+      if (part_entry [partnum].type == PART_TYPE_EXTENDED)
+	{
+	  if (extstart != 0)
+	    sos_bochs_printf ("Warning: two extended partitions detected\n");
+	  extstart = part_entry [partnum].lba;
+	  continue;
+	}
+
+      ret = sos_blockdev_register_partition (disk_class,
+					     disk_instance + partnum + 1,
+					     blkdev,
+					     part_entry[partnum].lba,
+					     part_entry[partnum].size, NULL);
+      if (ret != SOS_OK)
+	{
+	  sos_bochs_printf ("Could not register partition %d for disk %lu:%lu: error %d\n",
+			    partnum, disk_class, disk_instance, ret);
+	  continue;
+	}
+    }
+
+  while (extstart != 0 && partnum < 15)
+    {
+      ret = sos_blockdev_kernel_read (blkdev, (extstart + extsup) * block_size,
+				      (sos_luoffset_t) buffer, & size);
+      if (ret != SOS_OK)
+	{
+	  sos_blockdev_release_instance (blkdev);
+	  sos_kfree (buffer);
+	  return ret;
+	}
+
+      if (size != block_size)
+	{
+	  sos_blockdev_release_instance (blkdev);
+	  sos_kfree (buffer);
+	  return -SOS_EIO;
+	}
+
+      sos_bochs_printf ("%s%d (%lu Mb, %s) ", name, partnum+1,
+			(part_entry[0].size * block_size >> 20),
+			sos_part_type_str(part_entry[0].type));
+
+      ret = sos_blockdev_register_partition (disk_class,
+					     disk_instance + partnum + 1,
+					     blkdev,
+					     extstart + part_entry[0].lba,
+					     part_entry[0].size, NULL);
+      if (ret != SOS_OK)
+	sos_bochs_printf ("Could not register partition %d for disk %lu:%lu: error %d\n",
+			  partnum, disk_class, disk_instance, ret);
+
+      extsup = part_entry[1].lba;
+      partnum ++;
+      if (extsup == 0)
+	break;
+    }
+
+  sos_blockdev_release_instance (blkdev);
+  sos_kfree (buffer);
+  return SOS_OK;
+}
+
+/**
+ * Remove all detected partitions on given device
+ */
+sos_ret_t
+sos_part_undetect (sos_ui32_t disk_class, sos_ui32_t disk_instance)
+{
+  int partnum;
+  sos_ret_t ret;
+
+  for (partnum = 0; partnum < 15; partnum++)
+    {
+      ret = sos_blockdev_unregister_device (disk_class, disk_instance + partnum + 1);
+      if (ret != SOS_OK)
+	sos_bochs_printf ("Warning: could not unregister partition %d of device %lu:%lu\n",
+			  partnum, disk_class, disk_instance);
+    }
+
+  return SOS_OK;
+}
diff -ruNwb /tmp/sos-code-article9/drivers/part.h ../sos-code-article9.5/drivers/part.h
--- /tmp/sos-code-article9/drivers/part.h	1970-01-01 01:00:00.000000000 +0100
+++ ../sos-code-article9.5/drivers/part.h	2006-01-22 12:48:28.000000000 +0100
@@ -0,0 +1,34 @@
+/* Copyright (C) 2005  Thomas Petazzoni
+
+   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., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
+   USA.
+*/
+
+#ifndef _SOS_PART_H_
+#define _SOS_PART_H_
+
+/**
+ * @file part.h
+ *
+ * IBM PC partition "driver": in charge of registering each of the
+ * partition of the given block device
+ */
+
+sos_ret_t sos_part_detect (sos_ui32_t disk_class, sos_ui32_t disk_instance,
+			   sos_size_t block_size, const char *name);
+
+sos_ret_t sos_part_undetect (sos_ui32_t disk_class, sos_ui32_t disk_instance);
+
+#endif /* _SOS_PART_H_ */
diff -ruNwb /tmp/sos-code-article9/drivers/serial.c ../sos-code-article9.5/drivers/serial.c
--- /tmp/sos-code-article9/drivers/serial.c	2005-12-28 11:44:56.000000000 +0100
+++ ../sos-code-article9.5/drivers/serial.c	2006-01-22 12:48:28.000000000 +0100
@@ -195,7 +195,7 @@
 static sos_ret_t sos_serial_putchar (char c)
 {
   sos_ret_t ret;
-  int i;
+  unsigned int i;
 
   /* The serial port doesn't understand '\b', but requires ANSI
      commands instead. So we emulate '\b' by outputing "\e[D \e[D"
@@ -278,7 +278,6 @@
 {
   sos_ret_t ret;
 
-  /* FIXME */
   ret = tty_create (SOS_CHARDEV_SERIAL_MINOR, sos_serial_putchar,
 		    & _serial_config[0].tty);
   if (SOS_OK != ret)
diff -ruNwb /tmp/sos-code-article9/drivers/tty.c ../sos-code-article9.5/drivers/tty.c
--- /tmp/sos-code-article9/drivers/tty.c	2005-12-28 11:44:56.000000000 +0100
+++ ../sos-code-article9.5/drivers/tty.c	2006-01-22 12:48:28.000000000 +0100
@@ -138,7 +138,7 @@
 {
   struct tty_device *t;
   char c;
-  int i;
+  unsigned int i;
   sos_ret_t ret;
 
   t = (struct tty_device *) this->custom_data;
diff -ruNwb /tmp/sos-code-article9/drivers/x86_videomem.c ../sos-code-article9.5/drivers/x86_videomem.c
--- /tmp/sos-code-article9/drivers/x86_videomem.c	2005-12-28 11:44:56.000000000 +0100
+++ ../sos-code-article9.5/drivers/x86_videomem.c	2006-01-22 12:48:28.000000000 +0100
@@ -156,11 +156,11 @@
    through the sos_screen_putchar() function. */
 #define CONSOLE_ROW_START 12
 
-static void sos_screen_set_cursor (unsigned int row, unsigned int col)
+static void sos_screen_set_cursor (unsigned int _row, unsigned int _col)
 {
   unsigned int pos;
 
-  pos = (row * COLUMNS + col);
+  pos = (_row * COLUMNS + _col);
 
   outb(VGA_SET_CURSOR_HIGH, VGA_COMMAND_PORT);
   outb( (pos >> 8), VGA_DATA_PORT);
diff -ruNwb /tmp/sos-code-article9/drivers/zero.c ../sos-code-article9.5/drivers/zero.c
--- /tmp/sos-code-article9/drivers/zero.c	2005-12-28 11:44:56.000000000 +0100
+++ ../sos-code-article9.5/drivers/zero.c	2006-01-22 12:48:28.000000000 +0100
@@ -396,7 +396,8 @@
      increments) */
   for (rdlen = offs = 0 ; offs < *len ; offs += SOS_PAGE_SIZE)
     {
-      sos_size_t retval, memcpy_len = SOS_PAGE_SIZE;
+      sos_ret_t retval;
+      sos_size_t memcpy_len = SOS_PAGE_SIZE;
       if (offs + memcpy_len > *len)
 	memcpy_len = *len - offs;
       
@@ -406,7 +407,7 @@
 	break;
 
       rdlen += retval;
-      if (retval != memcpy_len)
+      if (retval != (sos_ret_t)memcpy_len)
 	break;
     }
 
diff -ruNwb /tmp/sos-code-article9/drivers/zero.h ../sos-code-article9.5/drivers/zero.h
--- /tmp/sos-code-article9/drivers/zero.h	2005-12-28 11:44:56.000000000 +0100
+++ ../sos-code-article9.5/drivers/zero.h	2006-01-22 12:48:28.000000000 +0100
@@ -29,7 +29,7 @@
 /**
  * Register the /dev/zero and /dev/null devices
  */
-sos_ret_t sos_dev_zero_subsystem_setup();
+sos_ret_t sos_dev_zero_subsystem_setup(void);
 
 
 /**
Binary files /tmp/sos-code-article9/extra/hd10M.img.gz and ../sos-code-article9.5/extra/hd10M.img.gz differ
diff -ruNwb /tmp/sos-code-article9/hwcore/cpu_context.c ../sos-code-article9.5/hwcore/cpu_context.c
--- /tmp/sos-code-article9/hwcore/cpu_context.c	2005-12-28 11:44:56.000000000 +0100
+++ ../sos-code-article9.5/hwcore/cpu_context.c	2006-01-22 12:48:30.000000000 +0100
@@ -530,7 +530,7 @@
 					   sos_size_t stack_size)
 {
   unsigned char *c;
-  int i;
+  unsigned int i;
 
   /* On SOS, "ctxt" corresponds to the address of the esp register of
      the saved context in Kernel mode (always, even for the interrupted
@@ -575,7 +575,7 @@
      was that of a kernel or user thread */
   if (TRUE == sos_cpu_context_is_in_user_mode(ctxt))
     {
-      struct sos_cpu_ustate * uctxt = (struct sos_cpu_ustate*)ctxt;
+      struct sos_cpu_ustate const* uctxt = (struct sos_cpu_ustate const*)ctxt;
       return uctxt->cpl3_esp;
     }
 
@@ -607,7 +607,7 @@
 	   (unsigned)ctxt->error_code);
   if (TRUE == sos_cpu_context_is_in_user_mode(ctxt))
     {
-      struct sos_cpu_ustate * uctxt = (struct sos_cpu_ustate*)ctxt;
+      struct sos_cpu_ustate const* uctxt = (struct sos_cpu_ustate const*)ctxt;
       snprintf(buf, sizeof(buf),
 	       "%s esp3=%x ss3=%x",
 	       buf, (unsigned)uctxt->cpl3_esp, (unsigned)uctxt->cpl3_ss);
@@ -897,7 +897,7 @@
 			 sos_backtrace_callback_t * backtracer,
 			 void *custom_arg)
 {
-  int depth;
+  unsigned int depth;
   sos_vaddr_t callee_PC, caller_frame;
 
   /* Cannot backtrace an interrupted user thread ! */
diff -ruNwb /tmp/sos-code-article9/hwcore/cpu_context.h ../sos-code-article9.5/hwcore/cpu_context.h
--- /tmp/sos-code-article9/hwcore/cpu_context.h	2005-12-28 11:44:56.000000000 +0100
+++ ../sos-code-article9.5/hwcore/cpu_context.h	2006-01-22 12:48:30.000000000 +0100
@@ -34,7 +34,7 @@
 /**
  * Prepare the system to deal with multiple CPU execution contexts
  */
-sos_ret_t sos_cpu_context_subsystem_setup();
+sos_ret_t sos_cpu_context_subsystem_setup(void);
 
 
 /**
diff -ruNwb /tmp/sos-code-article9/hwcore/exception.c ../sos-code-article9.5/hwcore/exception.c
--- /tmp/sos-code-article9/hwcore/exception.c	2005-12-28 11:44:56.000000000 +0100
+++ ../sos-code-article9.5/hwcore/exception.c	2006-01-22 12:48:30.000000000 +0100
@@ -70,6 +70,8 @@
 
 /* Catch-all exception handler */
 static void sos_generic_ex(int exid, struct sos_cpu_state *ctxt)
+     __attribute__((noreturn));
+static void sos_generic_ex(int exid, struct sos_cpu_state *ctxt)
 {
   const char *exname = sos_exception_get_name(exid);
 
diff -ruNwb /tmp/sos-code-article9/hwcore/irq.h ../sos-code-article9.5/hwcore/irq.h
--- /tmp/sos-code-article9/hwcore/irq.h	2005-12-28 11:44:57.000000000 +0100
+++ ../sos-code-article9.5/hwcore/irq.h	2006-01-22 12:48:30.000000000 +0100
@@ -84,7 +84,7 @@
 /**
  * Tell how many nested IRQ handler have been fired
  */
-sos_ui32_t sos_irq_get_nested_level();
+sos_ui32_t sos_irq_get_nested_level(void);
 
 
 /**
diff -ruNwb /tmp/sos-code-article9/hwcore/mm_context.c ../sos-code-article9.5/hwcore/mm_context.c
--- /tmp/sos-code-article9/hwcore/mm_context.c	2005-12-28 11:44:57.000000000 +0100
+++ ../sos-code-article9.5/hwcore/mm_context.c	2006-01-22 12:48:30.000000000 +0100
@@ -286,7 +286,7 @@
 }
 
 
-struct sos_mm_context *get_current_mm_context()
+struct sos_mm_context *get_current_mm_context(void)
 {
   SOS_ASSERT_FATAL(current_mm_context->ref_cnt > 0);
   return current_mm_context;
diff -ruNwb /tmp/sos-code-article9/hwcore/mm_context.h ../sos-code-article9.5/hwcore/mm_context.h
--- /tmp/sos-code-article9/hwcore/mm_context.h	2005-12-28 11:44:57.000000000 +0100
+++ ../sos-code-article9.5/hwcore/mm_context.h	2006-01-22 12:48:30.000000000 +0100
@@ -49,7 +49,7 @@
 /**
  * Setup the MMU context management subsystem
  */
-sos_ret_t sos_mm_context_subsystem_setup();
+sos_ret_t sos_mm_context_subsystem_setup(void);
 
 
 /**
diff -ruNwb /tmp/sos-code-article9/hwcore/paging.c ../sos-code-article9.5/hwcore/paging.c
--- /tmp/sos-code-article9/hwcore/paging.c	2005-12-28 11:44:57.000000000 +0100
+++ ../sos-code-article9.5/hwcore/paging.c	2006-01-22 12:48:30.000000000 +0100
@@ -610,6 +610,60 @@
 }
 
 
+sos_bool_t sos_paging_is_dirty(sos_vaddr_t vaddr)
+{
+  /* Get the page directory entry and table entry index for this
+     address */
+  unsigned index_in_pd = virt_to_pd_index(vaddr);
+  unsigned index_in_pt = virt_to_pt_index(vaddr);
+  
+  /* Get the PD of the current context */
+  struct x86_pde *pd = (struct x86_pde*)
+    (SOS_PAGING_MIRROR_VADDR
+     + SOS_PAGE_SIZE*virt_to_pd_index(SOS_PAGING_MIRROR_VADDR));
+
+  /* Address of the PT in the mirroring */
+  struct x86_pte * pt = (struct x86_pte*) (SOS_PAGING_MIRROR_VADDR
+					   + SOS_PAGE_SIZE*index_in_pd);
+
+  /* No page mapped at this address ? */
+  if (! pd[index_in_pd].present)
+    return FALSE;
+  if (! pt[index_in_pt].present)
+    return FALSE;
+
+  return (pt[index_in_pt].dirty != 0);
+}
+
+
+sos_ret_t sos_paging_set_dirty(sos_vaddr_t vaddr,
+			       sos_bool_t is_dirty)
+{
+  /* Get the page directory entry and table entry index for this
+     address */
+  unsigned index_in_pd = virt_to_pd_index(vaddr);
+  unsigned index_in_pt = virt_to_pt_index(vaddr);
+  
+  /* Get the PD of the current context */
+  struct x86_pde *pd = (struct x86_pde*)
+    (SOS_PAGING_MIRROR_VADDR
+     + SOS_PAGE_SIZE*virt_to_pd_index(SOS_PAGING_MIRROR_VADDR));
+
+  /* Address of the PT in the mirroring */
+  struct x86_pte * pt = (struct x86_pte*) (SOS_PAGING_MIRROR_VADDR
+					   + SOS_PAGE_SIZE*index_in_pd);
+
+  /* No page mapped at this address ? */
+  if (! pd[index_in_pd].present)
+    return -SOS_EFAULT;
+  if (! pt[index_in_pt].present)
+    return -SOS_EFAULT;
+
+  pt[index_in_pt].dirty = is_dirty;
+  return SOS_OK;
+}
+
+
 sos_paddr_t sos_paging_get_paddr(sos_vaddr_t vaddr)
 {
   /* Get the page directory entry and table entry index for this
@@ -642,7 +696,7 @@
  */
 
 
-sos_paddr_t sos_paging_get_current_PD_paddr()
+sos_paddr_t sos_paging_get_current_PD_paddr(void)
 {
   struct x86_pdbr pdbr;
   asm volatile("movl %%cr3, %0\n": "=r"(pdbr));
diff -ruNwb /tmp/sos-code-article9/hwcore/paging.h ../sos-code-article9.5/hwcore/paging.h
--- /tmp/sos-code-article9/hwcore/paging.h	2005-12-28 11:44:57.000000000 +0100
+++ ../sos-code-article9.5/hwcore/paging.h	2006-01-22 12:48:30.000000000 +0100
@@ -79,6 +79,7 @@
 sos_ret_t sos_paging_subsystem_setup(sos_paddr_t identity_mapping_base,
 				     sos_paddr_t identity_mapping_top);
 
+
 /**
  * Map the given physical page at the given virtual address in the
  * current address space.
@@ -134,6 +135,19 @@
 sos_ret_t sos_paging_set_prot(sos_vaddr_t vaddr,
 			      sos_ui32_t  new_prot);
 
+
+/**
+ * Get the "dirty" status of the page
+ */
+sos_bool_t sos_paging_is_dirty(sos_vaddr_t vaddr);
+
+/**
+ * Change the "dirty" status of the page
+ */
+sos_ret_t sos_paging_set_dirty(sos_vaddr_t vaddr,
+			       sos_bool_t is_dirty);
+
+
 /**
  * Change the access rights of the mapping from [vaddr .. vaddr +
  * size[ to the underlying physical pages (if any)
@@ -209,7 +223,7 @@
 /**
  * Retrieve the current physical address of the PD
  */
-sos_paddr_t sos_paging_get_current_PD_paddr();
+sos_paddr_t sos_paging_get_current_PD_paddr(void);
 
 
 /**
diff -ruNwb /tmp/sos-code-article9/hwcore/swintr.c ../sos-code-article9.5/hwcore/swintr.c
--- /tmp/sos-code-article9/hwcore/swintr.c	2005-12-28 11:44:57.000000000 +0100
+++ ../sos-code-article9.5/hwcore/swintr.c	2006-01-22 12:48:30.000000000 +0100
@@ -29,7 +29,7 @@
  * thread before calling the "C" function sos_do_syscall(), passing it
  * the correct arguments.
  */
-extern void sos_syscall_wrapper();
+extern void sos_syscall_wrapper(void);
 
 
 sos_ret_t sos_swintr_subsystem_setup(void)
diff -ruNwb /tmp/sos-code-article9/Makefile ../sos-code-article9.5/Makefile
--- /tmp/sos-code-article9/Makefile	2005-12-28 11:44:58.000000000 +0100
+++ ../sos-code-article9.5/Makefile	2006-01-22 12:48:32.000000000 +0100
@@ -40,10 +40,12 @@
 	  sos/umem_vmm.o sos/binfmt_elf32.o			\
 	  drivers/x86_videomem.o drivers/bochs.o		\
 	  drivers/zero.o drivers/mem.o				\
-	  drivers/kbd.o drivers/kbdmapfr.o			\
-	  drivers/tty.o drivers/serial.o drivers/console.o	\
+	  drivers/ide.o drivers/kbd.o drivers/kbdmapfr.o		\
+	  drivers/serial.o drivers/tty.o drivers/part.o	\
+	  drivers/console.o						\
 	  sos/hash.o sos/fs.o sos/fs_nscache.o			\
-	  drivers/fs_virtfs.o sos/chardev.o			\
+	  drivers/fs_virtfs.o sos/chardev.o sos/blkdev.o		\
+	  sos/blkcache.o sos/fs_pagecache.o				\
           userland/userprogs.kimg
 
 KERNEL_OBJ   = sos.elf
@@ -85,7 +87,7 @@
 
 # Clean directory
 clean:
-	$(RM) *.img *.o mtoolsrc *~ menu.txt *.img *.elf *.bin *.strip *.map
+	$(RM) fd*.img *.o mtoolsrc *~ menu.txt *.elf *.bin *.strip *.map
 	$(RM) *.log *.out bochs* sos.gz
 	$(RM) bootstrap/*.o bootstrap/*~
 	$(RM) drivers/*.o drivers/*~
diff -ruNwb /tmp/sos-code-article9/sos/binfmt_elf32.c ../sos-code-article9.5/sos/binfmt_elf32.c
--- /tmp/sos-code-article9/sos/binfmt_elf32.c	2005-12-28 11:44:57.000000000 +0100
+++ ../sos-code-article9.5/sos/binfmt_elf32.c	2006-01-22 12:48:30.000000000 +0100
@@ -149,7 +149,7 @@
       
       /* Copy the program in it */
       memcpy((void*)upage_uaddr,
-	     (void*)elf32prog_resource->vaddr + offset_in_prog,
+	     (void*)(elf32prog_resource->vaddr + offset_in_prog),
 	     size_to_copy);
       if (size_to_copy < SOS_PAGE_SIZE)
 	memset((void*)(upage_uaddr + size_to_copy), 0x0,
diff -ruNwb /tmp/sos-code-article9/sos/blkcache.c ../sos-code-article9.5/sos/blkcache.c
--- /tmp/sos-code-article9/sos/blkcache.c	1970-01-01 01:00:00.000000000 +0100
+++ ../sos-code-article9.5/sos/blkcache.c	2006-01-22 12:48:31.000000000 +0100
@@ -0,0 +1,500 @@
+/* Copyright (C) 2005,2006      David Decotigny
+
+   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., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
+   USA.
+*/
+
+#include <sos/ksynch.h>
+#include <sos/hash.h>
+#include <sos/kmem_slab.h>
+#include <sos/kmalloc.h>
+#include <sos/ksynch.h>
+#include <sos/assert.h>
+#include <sos/list.h>
+
+#include "blkcache.h"
+
+
+/**
+ * @note In the implementation below, we choose to transfer the blocks
+ * from list to list /before/ doing the disk access (if needed). The
+ * aim is to have the entries in the right list before any other
+ * thread can lock them. Actually, these "other threads" can lock the
+ * entry only during the disk accesses (kernel is non
+ * preemptible). So, putting the entries in the correct list /before/
+ * doing the actual disk read/write access will lower the probability
+ * for the thread to learn that, at the end, the entry it was waiting
+ * for has been transferred on another list.
+ *
+ * @note What is extremely important however, is that the
+ * sos_hash_insert/remove operation be carried out BEFORE the
+ * bocck_read/write operations. If not, then it may be possible to
+ * insert an entry already inserted (which causes the system to halt),
+ * etc.
+ */
+
+
+
+struct sos_block_cache_entry
+{
+  /** The key of the hash map */
+  sos_luoffset_t block_index;
+
+  /** Kernel address where the data for the block are stored */
+  sos_vaddr_t block_contents;
+
+  /** Is the block available, dirty or not ? */
+  enum { ENTRY_FREE, ENTRY_SYNC, ENTRY_DIRTY } state;
+  
+  /** Lock to protect the entry */
+  struct sos_kmutex lock;
+
+  /** Linkage structure to keep the cache block in the hash map */
+  struct sos_hash_linkage hlink;
+
+  /** Links to insert the bloc into the free/sync/dirty lists */
+  struct sos_block_cache_entry *prev, *next;
+};
+
+
+/** The entries of all the cache entries are stored in a single SLAB pool */
+static struct sos_kslab_cache * cache_of_bkcache_entries;
+
+
+struct sos_block_cache
+{
+  /** SLAB pool for the block contents */
+  struct sos_kslab_cache         * slab_cache;
+
+  /** Dictionary offset -> block cache entry */
+  struct sos_hash_table          * lookup_table;
+
+  /** Hardware read/write operations */
+  struct sos_blockdev_operations * operations;
+  void                           * blkdev_instance_custom_data;
+
+  /* Lists to look into in order to free a node. LRU is enforced */
+  struct sos_block_cache_entry * free_list;  /**< List of entries
+						  still available */
+  struct sos_block_cache_entry * sync_list;  /**< Blocks in sync with
+		       	  	                  disk (LRU at end) */
+  struct sos_block_cache_entry * dirty_list; /**< Dirty blocks (LRU
+						  last) */
+};
+
+
+sos_ret_t sos_blkcache_subsystem_setup()
+{
+  cache_of_bkcache_entries
+    = sos_kmem_cache_create("blkcache_entry",
+			    sizeof(struct sos_block_cache_entry),
+			    1, 0,
+			    SOS_KSLAB_CREATE_MAP | SOS_KSLAB_CREATE_ZERO);
+  if (NULL == cache_of_bkcache_entries)
+    return -SOS_ENOMEM;
+  return SOS_OK;
+}
+
+
+struct sos_block_cache *
+sos_blkcache_new_cache(void * blockdev_instance_custom_data,
+		       sos_size_t  block_size,
+		       sos_count_t cache_size_in_blocks,
+		       struct sos_blockdev_operations * blockdev_ops)
+{
+  sos_count_t idx;
+  struct sos_block_cache * blkcache;
+
+  SOS_ASSERT_FATAL(block_size > 0);
+  SOS_ASSERT_FATAL(cache_size_in_blocks > 0);
+
+  blkcache
+    = (struct sos_block_cache*) sos_kmalloc(sizeof(struct sos_block_cache), 0);
+  if (NULL == blkcache)
+    return NULL;
+
+  blkcache->blkdev_instance_custom_data = blockdev_instance_custom_data;
+  blkcache->operations                  = blockdev_ops;
+
+  /* Allocate the hash table */
+  blkcache->lookup_table = sos_hash_create("blkcache",
+					   struct sos_block_cache_entry,
+					   sos_hash_ui64,
+					   sos_hash_key_eq_ui64,
+					   17, block_index, hlink);
+  if (NULL == blkcache->lookup_table)
+    {
+      sos_kfree((sos_vaddr_t)blkcache);
+      return NULL;
+    }
+
+  /* Allocate the slab cache for the blocks */
+  blkcache->slab_cache = sos_kmem_cache_create("blkcache", block_size,
+					       4, 0,
+					       SOS_KSLAB_CREATE_MAP
+					       | SOS_KSLAB_CREATE_ZERO);
+  if (NULL == blkcache->slab_cache)
+    {
+      sos_hash_dispose(blkcache->lookup_table);
+      sos_kfree((sos_vaddr_t)blkcache);
+      return NULL;
+    }
+
+  /* Allocate the cache blocks in this slab cache, and mark them as
+     "free" */
+  for (idx = 0 ; idx < cache_size_in_blocks ; idx ++)
+    {
+      struct sos_block_cache_entry * bkcache_entry;
+      sos_vaddr_t bkcache_contents;
+
+      bkcache_entry
+	= (struct sos_block_cache_entry*)sos_kmem_cache_alloc(cache_of_bkcache_entries,
+						      0);
+      if (NULL == bkcache_entry)
+	{
+	  sos_blkcache_delete_cache(blkcache);
+	  return NULL;
+	}
+
+      bkcache_contents = sos_kmem_cache_alloc(blkcache->slab_cache, 0);
+      if (NULL == (void*)bkcache_contents)
+	{
+	  sos_kmem_cache_free((sos_vaddr_t)bkcache_entry);
+	  sos_blkcache_delete_cache(blkcache);
+	  return NULL;
+	}
+
+      /* Initialize the cache entry */
+      bkcache_entry->block_contents = bkcache_contents;
+      bkcache_entry->state = ENTRY_FREE;
+      SOS_ASSERT_FATAL(SOS_OK == sos_kmutex_init(& bkcache_entry->lock,
+						 "bkcache_lock",
+						 SOS_KWQ_ORDER_FIFO));
+      
+      /* Now add it in the free list */
+      list_add_head(blkcache->free_list, bkcache_entry);
+    }
+
+  return blkcache;
+}
+
+
+/** Helper function used to collapse the sync/free lists */
+static sos_ret_t blkcache_collapse_list(struct sos_block_cache_entry ** l)
+{
+  while (! list_is_empty(*l))
+    {
+      struct sos_block_cache_entry * entry = list_pop_head(*l);
+      if (NULL == entry)
+	break;
+
+      /* The delete_cache function should be called only when the
+	 block device is not currently in use */
+      SOS_ASSERT_FATAL(SOS_OK == sos_kmutex_dispose(& entry->lock));
+
+      if (NULL != (void*)entry->block_contents)
+	sos_kmem_cache_free(entry->block_contents);
+
+      sos_kmem_cache_free((sos_vaddr_t)entry);
+    }
+
+  return SOS_OK;
+}
+
+
+sos_ret_t
+sos_blkcache_delete_cache(struct sos_block_cache * bc)
+{
+  sos_blkcache_flush(bc); /* The dirty list is expected to be empty */
+
+  sos_hash_dispose(bc->lookup_table);
+  blkcache_collapse_list(& bc->sync_list);
+  blkcache_collapse_list(& bc->free_list);
+
+  sos_kfree((sos_vaddr_t)bc);
+
+  return SOS_OK;
+}
+
+
+/** Helper function used to transfer a dirty block to the
+    sync_list. @note the entry is expected to be locked */
+static sos_ret_t blkcache_flush_entry(struct sos_block_cache * bc,
+				      struct sos_block_cache_entry * entry,
+				      sos_bool_t mark_as_free)
+{
+  sos_ret_t retval = SOS_OK;
+
+  SOS_ASSERT_FATAL(TRUE == sos_kmutex_owned_by_me(& entry->lock));
+  SOS_ASSERT_FATAL(entry->state == ENTRY_DIRTY);
+
+  /* We prepare to transfer it to the sync/free list */
+  if (mark_as_free)
+    {
+      list_delete(bc->dirty_list, entry);
+      entry->state = ENTRY_FREE;
+      sos_hash_remove(bc->lookup_table, entry);
+      list_add_head(bc->free_list, entry);
+    }
+  else
+    {
+      list_delete(bc->dirty_list, entry);
+      entry->state = ENTRY_SYNC;
+      list_add_head(bc->sync_list, entry);
+    }
+
+  retval = bc->operations->write_block(bc->blkdev_instance_custom_data,
+				       entry->block_contents,
+				       entry->block_index);
+  if (SOS_OK != retval)
+    {
+      /*
+       * Putting the entry back to the dirty list may cause
+       * retrieve_block to loop infinitely (always trying to transfer
+       * this block to disk...) !... A solution would be to register
+       * this block in a dictionary of failing blocks and to force
+       * retrieve block to ignore requests on these blocks
+       */
+      SOS_FATAL_ERROR("Not implemented yet: we don't support failed disk access (see comments in source file)");
+    }
+
+  return retval;
+}
+
+
+struct sos_block_cache_entry *
+sos_blkcache_retrieve_block(struct sos_block_cache * bc,
+			    sos_luoffset_t block_index,
+			    sos_blkcache_access_type_t access_type,
+			    sos_vaddr_t */*out*/ block_contents)
+{
+  struct sos_block_cache_entry * entry = NULL;
+  *block_contents = (sos_vaddr_t)NULL;
+
+  /* Get and lock the entry */
+  while (TRUE)
+    {
+      entry = sos_hash_lookup(bc->lookup_table,
+			      & block_index);
+      if (NULL != entry)
+	{
+	  /* The block was already in the cache */
+	  
+	  sos_kmutex_lock(& entry->lock, NULL);
+	  
+	  /* We have the mutex. However, this entry can now be
+	     dedicated to another block: it was stolen by another
+	     thread during the blocking time */
+	  if ((ENTRY_FREE != entry->state)
+	      && (entry->block_index == block_index))
+	    {
+	      /* Great ! This entry is still the one we expect ! */
+
+	      /* Write-only access: force it to be dirty NOW ! */
+	      if ( (access_type == SOS_BLKCACHE_WRITE_ONLY)
+		   && (ENTRY_SYNC == entry->state) )
+		{
+		  list_delete(bc->sync_list, entry);
+		  entry->state = ENTRY_DIRTY;
+		  list_add_head(bc->dirty_list, entry);
+		}
+
+	      *block_contents = entry->block_contents;
+	      return entry;
+	    }
+	  
+	  /* Bad luck: someone stole the entry and used it in the
+	     meantime */
+	  sos_kmutex_unlock(& entry->lock);
+	  /* Go back and look it up again */
+	  continue;
+	}
+
+
+      /* Ok, the block is not yet in the cache: we need to allocate a new
+	 one */
+
+      /* The simplest: there is a free slot in the free list (take the
+	 most recently freed) */
+      if (! list_is_empty(bc->free_list))
+	{
+	  entry = list_get_head(bc->free_list);
+	  sos_kmutex_lock(& entry->lock, NULL);
+
+	  /* By the time we return from the lock, the entry could have
+	     be stolen */
+	  if ((ENTRY_FREE != entry->state)
+	      || (NULL != sos_hash_lookup(bc->lookup_table, & block_index)))
+	    {
+	      sos_kmutex_unlock(& entry->lock);
+	      continue;
+	    }
+
+	  /* Ok, we got a REALLY free entry ! */
+	  break;
+	}
+
+      /* A little more complicated: there is a non-dirty entry in
+	 synch with disk, transfer it to the free list. Take the least
+	 recently used */
+      if (! list_is_empty(bc->sync_list))
+	{
+	  entry = list_get_tail(bc->sync_list);
+	  sos_kmutex_lock(& entry->lock, NULL);
+
+	  /* During blocking time, the block is not in the sync list
+	     anymore ! We'd better redo a complete iteration to choose
+	     another block, just in case the block we are looking for
+	     was transferred in cache by someone else */
+	  if ((ENTRY_SYNC != entry->state)
+	      || (NULL != sos_hash_lookup(bc->lookup_table, & block_index)))
+	    {
+	      sos_kmutex_unlock(& entry->lock);
+	      continue;
+	    }
+
+	  list_delete(bc->sync_list, entry);
+	  sos_hash_remove(bc->lookup_table, entry);
+	  entry->state = ENTRY_FREE;
+	  list_add_head(bc->free_list, entry);
+
+	  /* Now we have a free entry for us ! */
+	  break;
+	}
+      
+      /*
+       * Bad luck: we have to flush a dirty entry back to disk
+       */
+      SOS_ASSERT_FATAL(! list_is_empty(bc->dirty_list));
+      entry = list_get_tail(bc->dirty_list);
+
+      sos_kmutex_lock(& entry->lock, NULL);
+
+      /* During blocking time, the block was transferred to the sync
+	 list ! We'd better redo a complete iteration to choose
+	 another block, if we want to keep this recently-accessed
+	 block inside the cache */
+      if ((ENTRY_DIRTY == entry->state)
+	  && (NULL == sos_hash_lookup(bc->lookup_table, & block_index)))
+	{
+	  /* entry still dirty: transfer it to the free list */
+
+	  if (SOS_OK == blkcache_flush_entry(bc, entry, TRUE))
+	      /* We can use it now ! */
+	      break;
+	}
+
+      sos_kmutex_unlock(& entry->lock);
+
+      /* We can now go for another iteration because we blocked: maybe
+	 the block was retrieved into the cache by someone else */
+    }
+
+  /* Here we have a locked block that needs to be initialised */
+
+  /* Remove it from the free list */
+  list_delete(bc->free_list, entry);
+
+  /* Prepare it to be inserted in the dictionnary */
+  entry->block_index = block_index;
+
+  /* Announce that we are preparing to add the block into the cache */
+  if (SOS_OK != sos_hash_insert(bc->lookup_table, entry))
+    SOS_FATAL_ERROR("Unexpected hash collision");
+
+
+  /* No need to do anything for a write-only block, simply mark it
+     dirty */
+  if (access_type == SOS_BLKCACHE_WRITE_ONLY)
+    {
+      entry->state = ENTRY_DIRTY;
+      list_add_head(bc->dirty_list, entry);
+    }
+
+  /* Otherwise retrieve the block from disk NOW ! */
+  else
+    {
+      entry->state = ENTRY_SYNC;
+      list_add_head(bc->sync_list, entry);
+
+      if (SOS_OK
+	  != bc->operations->read_block(bc->blkdev_instance_custom_data,
+					entry->block_contents,
+					entry->block_index))
+	{
+	  /* In case of failure, remove it from hash and return NULL */
+	  list_delete(bc->sync_list, entry);
+	  sos_hash_remove(bc->lookup_table, entry);
+	  entry->state = ENTRY_FREE;
+	  list_add_head(bc->free_list, entry);
+	  sos_kmutex_unlock(& entry->lock);
+	  return NULL;
+	}
+    }
+
+  *block_contents = entry->block_contents;
+  return entry;
+}
+
+
+sos_ret_t
+sos_blkcache_release_block(struct sos_block_cache * bc,
+			   struct sos_block_cache_entry * entry,
+			   sos_bool_t is_dirty,
+			   sos_bool_t force_flush)
+{
+  /* Enforce the least recently used policy */
+  if (ENTRY_SYNC == entry->state)
+    {
+      list_delete(bc->sync_list, entry);
+      /* Eventually transfer the block to the dirty list */
+      if (is_dirty)
+	entry->state = ENTRY_DIRTY;
+    }
+  else
+    list_delete(bc->dirty_list, entry);
+
+  if (ENTRY_SYNC == entry->state)
+    list_add_head(bc->sync_list, entry);
+  else
+    list_add_head(bc->dirty_list, entry);
+
+  if ( (ENTRY_DIRTY == entry->state) && force_flush)
+    blkcache_flush_entry(bc, entry, FALSE);
+
+  sos_kmutex_unlock(& entry->lock);
+  return SOS_OK;
+}
+
+
+sos_ret_t
+sos_blkcache_flush(struct sos_block_cache * bc)
+{
+  struct sos_block_cache_entry * entry;
+  while (NULL != (entry = list_get_head(bc->dirty_list)) )
+    {
+      sos_ret_t retval = SOS_OK;
+
+      sos_kmutex_lock(& entry->lock, NULL);
+      if (ENTRY_DIRTY == entry->state)
+	retval = blkcache_flush_entry(bc, entry, FALSE);
+      sos_kmutex_unlock(& entry->lock);
+
+      if (SOS_OK != retval)
+	return retval;
+    }
+
+  return SOS_OK;
+}
diff -ruNwb /tmp/sos-code-article9/sos/blkcache.h ../sos-code-article9.5/sos/blkcache.h
--- /tmp/sos-code-article9/sos/blkcache.h	1970-01-01 01:00:00.000000000 +0100
+++ ../sos-code-article9.5/sos/blkcache.h	2006-01-22 12:48:31.000000000 +0100
@@ -0,0 +1,97 @@
+/* Copyright (C) 2005,2006      David Decotigny
+
+   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., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
+   USA.
+*/
+#ifndef _SOS_BLKCACHE_H_
+#define _SOS_BLKCACHE_H_
+
+
+/**
+ * @file blkcache.h
+ *
+ * Simple block cache interface. This implementation is based on a
+ * fixed-size block cache, the size being set at construction time.
+ */
+#include <sos/errno.h>
+#include <sos/blkdev.h>
+
+
+/** Opaque structure holding a cache */
+struct sos_block_cache;
+
+/** Opaque structure holding a cache entry (used as a cookie from
+    blkcache_retrieve to blkcache_release) */
+struct sos_block_cache_entry;
+
+
+sos_ret_t sos_blkcache_subsystem_setup(void);
+
+
+/** Create a new block cache */
+struct sos_block_cache *
+sos_blkcache_new_cache(void * blockdev_instance_custom_data,
+		       sos_size_t  block_size,
+		       sos_count_t cache_size_in_blocks,
+		       struct sos_blockdev_operations * blockdev_ops);
+
+
+/** Delete a block cache */
+sos_ret_t
+sos_blkcache_delete_cache(struct sos_block_cache * bc);
+
+
+/**
+ * "Write-only" cached blocks are expected to be *completely*
+ * overwritten by the blkdev code
+ */
+typedef enum { SOS_BLKCACHE_READ_ONLY  = 0x4242,
+	       SOS_BLKCACHE_READ_WRITE = 0x2442,
+	       SOS_BLKCACHE_WRITE_ONLY = 0x4224 } sos_blkcache_access_type_t;
+
+
+/**
+ * Retrieve a block from the cache
+ *  @param block_contents is filled with the address of the kernel
+ *    buffer holding the data
+ *  @return opaque structure used by release_block, or NULL when the
+ *    block could not be retrieved from disk
+ *
+ * @note once used and/or modified, the block must be released.
+ */
+struct sos_block_cache_entry *
+sos_blkcache_retrieve_block(struct sos_block_cache * bc,
+			    sos_luoffset_t block_index,
+			    sos_blkcache_access_type_t access_type,
+			    sos_vaddr_t */*out*/ block_contents);
+
+
+/**
+ * Unreference a block previously retrieved. If is_dirty is TRUE, the
+ * block MUST have been retrieved with the "read_write" or
+ * "write_only" access type.
+ */
+sos_ret_t
+sos_blkcache_release_block(struct sos_block_cache * bc,
+			   struct sos_block_cache_entry * entry,
+			   sos_bool_t is_dirty,
+			   sos_bool_t force_flush);
+
+
+/** Flush any modified blocks to disk */
+sos_ret_t
+sos_blkcache_flush(struct sos_block_cache * bc);
+
+#endif
diff -ruNwb /tmp/sos-code-article9/sos/blkdev.c ../sos-code-article9.5/sos/blkdev.c
--- /tmp/sos-code-article9/sos/blkdev.c	1970-01-01 01:00:00.000000000 +0100
+++ ../sos-code-article9.5/sos/blkdev.c	2006-01-22 12:48:31.000000000 +0100
@@ -0,0 +1,1196 @@
+/* Copyright (C) 2005,2006      David Decotigny
+
+   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., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
+   USA.
+*/
+
+#include <sos/list.h>
+#include <sos/assert.h>
+#include <sos/kmalloc.h>
+#include <sos/uaccess.h>
+#include <sos/thread.h>
+#include <sos/time.h>
+#include <sos/blkcache.h>
+#include <sos/fs_pagecache.h>
+#include <sos/umem_vmm.h>
+#include <sos/physmem.h>
+#include <hwcore/paging.h>
+#include <drivers/devices.h>
+
+#include "blkdev.h"
+
+struct sos_blockdev_instance
+{
+  /**
+   * Unique identifier (blockdev-wide) of this block device used by
+   * the sync_all_blockdev function. This enables sync_all_blockdev to
+   * be resilient to other register_partition/disk calls
+   */
+  sos_ui64_t uid;
+
+
+  /**
+   * Size of a block in bytes.
+   */
+  sos_size_t     block_size;
+
+
+  /**
+   * Size of the device, in blocks
+   */
+  sos_lcount_t   number_of_blocks;
+
+
+  /**
+   * For a partition: reference to the master disk and index of the
+   * first block in it
+   */
+  struct sos_blockdev_instance *parent_blockdev;
+  sos_luoffset_t index_of_first_block;
+
+  /** Major/minor for the device */
+  struct sos_fs_dev_id_t dev_id;
+
+  struct sos_blockdev_operations * operations;
+
+  /**
+   * Cache of blocks for this device
+   * @note Shared between a disk and all its (sub-(sub-(...)))partitions
+   */
+  struct sos_block_cache   * blk_cache;
+
+  /**
+   * Cache of mapped pages for this device
+   *
+   * @note Proper to each disk and every
+   * (sub-(sub-(...)))partitions. It cannot be shared because the
+   * partitions on a disk may not start on a page boundary... but on a
+   * sector boundary.
+   */
+  struct sos_fs_pagecache  * map_cache;
+
+  /* Yes, a block device can be mapped ! */
+  struct sos_umem_vmm_mapped_resource mapres;
+
+  void * custom_data;
+
+  sos_count_t ref_cnt;
+  struct sos_blockdev_instance *next, *prev;
+};
+
+
+/** The list of all block devices registered */
+static struct sos_blockdev_instance *registered_blockdev_instances;
+
+
+/** Last UID delivered for the FS instances */
+static sos_ui64_t last_fs_instance_uid;
+
+
+/* Forward declarations */
+static sos_ret_t
+blockdev_sync_dirty_page(sos_luoffset_t offset,
+		       sos_vaddr_t dirty_page,
+		       void * custom_data);
+
+static sos_ret_t
+blockdev_helper_new_opened_file(struct sos_fs_node * this,
+				const struct sos_process * owner,
+				sos_ui32_t open_flags,
+				struct sos_fs_opened_file ** result_of);
+static sos_ret_t
+blockdev_helper_close_opened_file(struct sos_fs_node * this,
+				  struct sos_fs_opened_file * of);
+
+static sos_ret_t
+duplicate_opened_blockdev(struct sos_fs_opened_file *this,
+			  const struct sos_process  * for_owner,
+			  struct sos_fs_opened_file **result_of);
+
+static sos_ret_t blockdev_new_mapping(struct sos_umem_vmm_vr *);
+
+static struct sos_fs_ops_opened_file blockdev_ops_opened_file;
+static struct sos_fs_ops_opened_blockdev blockdev_ops_opened_blockdev;
+
+
+/** The thread routine called to flush the cache contents to disk */
+static void bdflush_thread(void * unused) __attribute__((noreturn));
+static void bdflush_thread(void * unused)
+{
+  while (1)
+    {
+      struct sos_time t = (struct sos_time) { .sec=30, .nanosec=0 };
+      sos_thread_sleep(&t);
+      sos_blockdev_sync_all_devices();
+    }
+}
+
+
+sos_ret_t sos_blockdev_subsystem_setup()
+{
+  sos_ret_t retval;
+  last_fs_instance_uid = 42;
+
+  retval = sos_blkcache_subsystem_setup();
+  if (SOS_OK != retval)
+    return retval;
+
+  /* Create the bdflush kernel thread */
+  if (NULL == sos_create_kernel_thread("bdflush",
+				       bdflush_thread,
+				       NULL,
+				       SOS_SCHED_PRIO_TS_LOWEST))
+    return -SOS_ENOMEM;
+
+  return SOS_OK;
+}
+
+
+/** Helper function used to increment the reference count for the
+    device */
+static sos_ret_t
+blockdev_use_instance(struct sos_blockdev_instance * blockdev)
+{
+  SOS_ASSERT_FATAL(blockdev->ref_cnt > 0);
+  blockdev->ref_cnt ++;
+  return SOS_OK;
+}
+
+
+/** Helper function used to decrement the reference count for the
+    device */
+sos_ret_t
+sos_blockdev_release_instance(struct sos_blockdev_instance * blockdev)
+{
+  SOS_ASSERT_FATAL(blockdev->ref_cnt > 1);
+  blockdev->ref_cnt --;
+  return SOS_OK;
+}
+
+
+/**
+ * Return the device instance structure for the corresponding device
+ * class/instance, or NULL when none found.
+ */
+static struct sos_blockdev_instance*
+lookup_blockdev_instance(sos_ui32_t device_class, sos_ui32_t device_instance)
+{
+  struct sos_blockdev_instance * blockdev;
+  int nb;
+
+  list_foreach (registered_blockdev_instances, blockdev, nb)
+    {
+      if (blockdev->dev_id.device_class != device_class)
+	continue;
+      if (blockdev->dev_id.device_instance != device_instance)
+	continue;
+
+      return blockdev;
+    }
+
+  return NULL;
+}
+
+
+sos_ret_t
+sos_blockdev_register_disk (sos_ui32_t     device_class,
+			    sos_ui32_t     device_instance,
+			    sos_size_t     block_size,
+			    sos_lcount_t   number_of_blocks,
+			    sos_count_t    blkcache_size_in_blocks,
+			    struct sos_blockdev_operations * blockdev_ops,
+			    void * blockdev_instance_custom_data)
+{
+  struct sos_blockdev_instance * blockdev;
+
+  blockdev = lookup_blockdev_instance(device_class, device_instance);
+  if (NULL != blockdev)
+    return -SOS_EBUSY;
+
+  if (block_size <= 0)
+    return -SOS_EINVAL;
+  if (number_of_blocks <= 0)
+    return -SOS_EINVAL;
+
+  blockdev = (struct sos_blockdev_instance*)
+    sos_kmalloc(sizeof(struct sos_blockdev_instance), 0);
+  if (NULL == blockdev)
+    return -SOS_ENOMEM;
+
+  /* Create a new blk_cache */
+  blockdev->blk_cache = sos_blkcache_new_cache(blockdev_instance_custom_data,
+					       block_size,
+					       blkcache_size_in_blocks,
+					       blockdev_ops);
+  if (NULL == blockdev->blk_cache)
+    {
+      sos_kfree((sos_vaddr_t) blockdev);
+      return -SOS_ENOMEM;
+    }
+
+  /* Create a new page cache for pages mapped */
+  blockdev->map_cache
+    = sos_fs_pagecache_new_cache((sos_fs_pagecache_sync_function_t)
+				   blockdev_sync_dirty_page,
+				 (void*)blockdev);
+  if (NULL == blockdev->map_cache)
+    {
+      sos_blkcache_delete_cache(blockdev->blk_cache);
+      sos_kfree((sos_vaddr_t) blockdev);
+      return -SOS_ENOMEM;
+    }  
+
+  /* Description of the device */
+  blockdev->dev_id.device_class    = device_class;
+  blockdev->dev_id.device_instance = device_instance;
+
+  /* Description of the storage */
+  blockdev->block_size             = block_size;
+  blockdev->number_of_blocks       = number_of_blocks;
+  blockdev->parent_blockdev        = NULL;
+  blockdev->index_of_first_block   = 0;
+
+  /* Prepare the blkcache related stuff */
+  blockdev->operations             = blockdev_ops;
+  blockdev->custom_data            = blockdev_instance_custom_data;
+
+  /* Prepare the mmap related stuff */
+  blockdev->mapres.allowed_access_rights = SOS_VM_MAP_PROT_READ
+                                           | SOS_VM_MAP_PROT_WRITE
+                                           | SOS_VM_MAP_PROT_EXEC;
+  blockdev->mapres.flags                 = 0;
+  list_init(blockdev->mapres.list_vr);
+  blockdev->mapres.custom_data           = (void*)blockdev;
+  blockdev->mapres.mmap                  = blockdev_new_mapping;
+
+  blockdev->ref_cnt                = 1;
+
+  blockdev->uid = last_fs_instance_uid ++;
+  list_add_tail(registered_blockdev_instances, blockdev);
+
+  return SOS_OK;
+}
+
+
+sos_ret_t
+sos_blockdev_register_partition(sos_ui32_t device_class,
+				sos_ui32_t device_instance,
+				struct sos_blockdev_instance * parent_bd,
+				sos_luoffset_t index_of_first_block,
+				sos_lcount_t number_of_blocks,
+				void * blockdev_instance_custom_data)
+{
+  struct sos_blockdev_instance * blockdev;
+
+  if (NULL == parent_bd)
+    return -SOS_EINVAL;
+
+  /* Make sure this partitions fits in parent partition or disk */
+  if (index_of_first_block + number_of_blocks >
+      parent_bd->number_of_blocks)
+    return -SOS_EINVAL;
+
+  blockdev = lookup_blockdev_instance(device_class, device_instance);
+  if (NULL != blockdev)
+    return -SOS_EBUSY;
+
+  blockdev = (struct sos_blockdev_instance*)
+    sos_kmalloc(sizeof(struct sos_blockdev_instance), 0);
+  if (NULL == blockdev)
+    return -SOS_ENOMEM;
+
+  /* Create the page cache for the partition */
+  blockdev->map_cache
+    = sos_fs_pagecache_new_cache((sos_fs_pagecache_sync_function_t)
+				 blockdev_sync_dirty_page,
+				 (void*)blockdev);
+  if (NULL == blockdev->map_cache)
+    {
+      sos_kfree((sos_vaddr_t) blockdev);
+      return -SOS_ENOMEM;
+    }
+
+  /* Increase parent's reference count */
+  blockdev_use_instance(parent_bd);
+
+  /* Description of the device */
+  blockdev->dev_id.device_class    = device_class;
+  blockdev->dev_id.device_instance = device_instance;
+
+  /* Description of the storage */
+  blockdev->block_size             = parent_bd->block_size;
+  blockdev->number_of_blocks       = number_of_blocks;
+  blockdev->parent_blockdev        = parent_bd;
+  blockdev->index_of_first_block
+    = parent_bd->index_of_first_block + index_of_first_block;
+
+  /* Prepare the blkcache related stuff */
+  blockdev->operations             = parent_bd->operations;
+  blockdev->blk_cache              = parent_bd->blk_cache;
+  blockdev->custom_data            = blockdev_instance_custom_data;
+
+  /* Prepare the mmap related stuff */
+  blockdev->mapres.allowed_access_rights = SOS_VM_MAP_PROT_READ
+                                           | SOS_VM_MAP_PROT_WRITE
+                                           | SOS_VM_MAP_PROT_EXEC;
+  blockdev->mapres.flags                 = 0;
+  list_init(blockdev->mapres.list_vr);
+  blockdev->mapres.custom_data           = (void*)blockdev;
+  blockdev->mapres.mmap                  = blockdev_new_mapping;
+
+  blockdev->ref_cnt                = 1;
+
+  blockdev->uid = last_fs_instance_uid ++;
+  list_add_tail(registered_blockdev_instances, blockdev);
+
+  return SOS_OK;
+}
+
+
+sos_ret_t sos_blockdev_unregister_device (sos_ui32_t device_class,
+					  sos_ui32_t device_instance)
+{
+  struct sos_blockdev_instance * blockdev;
+
+  blockdev = lookup_blockdev_instance(device_class, device_instance);
+  if (NULL == blockdev)
+    return -SOS_ENODEV;
+  
+  if (blockdev->ref_cnt != 1)
+    return -SOS_EBUSY;
+
+  /* Unallocate the page cache */
+  sos_fs_pagecache_delete_cache(blockdev->map_cache);
+
+  /* Unreference parent block device, if any */
+  if (NULL != blockdev->parent_blockdev)
+    {
+      blockdev->parent_blockdev->ref_cnt --;
+    }
+  else
+    {
+      /* Otherwise: we are the parent BD => unallocate the block cache */
+      sos_blkcache_delete_cache(blockdev->blk_cache);
+    }
+
+  list_delete(registered_blockdev_instances, blockdev);
+  return sos_kfree((sos_vaddr_t)blockdev);
+}
+
+
+/** Generic read function working both with kernel and userspace
+    addresses. The pagecache is tried first, and the block cache if
+    the page cache does not have the data yet */
+static sos_ret_t
+blockdev_generic_read(struct sos_blockdev_instance * blockdev,
+		      sos_luoffset_t offset_in_device,
+		      sos_genaddr_t buff_addr,
+		      sos_size_t * /* in/out */len,
+		      sos_bool_t bypass_pagecache)
+{
+  sos_size_t rdbytes = 0;
+
+  while (rdbytes < *len)
+    {
+      sos_ret_t retval;
+      sos_size_t offset_in_block, wrbytes;
+      /* Get the block at the current offset */
+      sos_luoffset_t block_id
+	= offset_in_device / blockdev->block_size;
+      SOS_GENADDR_DECL(gaddr, buff_addr.is_user, buff_addr.addr + rdbytes);
+
+      /* reaching the end of the device ? */
+      if (block_id >= blockdev->number_of_blocks)
+	break;
+
+      wrbytes = *len - rdbytes;
+
+      /* Trying to get (part of) the remaining from page cache */
+      if (! bypass_pagecache)
+	retval = sos_fs_pagecache_read(blockdev->map_cache,
+				       offset_in_device,
+				       gaddr,
+				       & wrbytes);
+      else
+	retval = -SOS_ENOENT;
+
+      if (-SOS_ENOENT != retval)
+	{
+	  /* (partial) success ! */
+	  rdbytes          += wrbytes;
+	  offset_in_device += wrbytes;
+
+	  if (SOS_OK == retval)
+	    continue; /* Complete block read */
+	  else
+	    break; /* Partial read */
+	}
+
+      /* Translating this block index into an offset inside the
+	 disk */
+      block_id += blockdev->index_of_first_block;
+
+      /* Retrieve the block from block cache */
+      sos_vaddr_t block_data;
+      struct sos_block_cache_entry * bkcache_entry
+	= sos_blkcache_retrieve_block(blockdev->blk_cache,
+				      block_id,
+				      SOS_BLKCACHE_READ_ONLY,
+				      & block_data);
+      if (NULL == bkcache_entry)
+	break;
+
+      /* Copy the data to user */
+      offset_in_block
+	= offset_in_device % blockdev->block_size;
+      wrbytes
+	= blockdev->block_size - offset_in_block;
+      if (*len - rdbytes < wrbytes)
+	wrbytes = *len - rdbytes;
+
+      retval = sos_memcpy_generic_to(gaddr,
+				     block_data + offset_in_block,
+				     wrbytes);
+      
+      /* Release this block back to the cache */
+      sos_blkcache_release_block(blockdev->blk_cache,
+				 bkcache_entry,
+				 FALSE, FALSE);
+      if (retval > 0)
+	{
+	  rdbytes          += retval;
+	  offset_in_device += retval;
+	}
+      if (retval != (sos_ret_t)wrbytes)
+	break; /* Partial read */
+    }
+
+  *len = rdbytes;
+  return SOS_OK;
+}
+
+
+/** Generic write function working both with kernel and userspace
+    addresses. The pagecache is tried first, and the block cache if
+    the page cache does not have the data yet */
+static sos_ret_t
+blockdev_generic_write(struct sos_blockdev_instance * blockdev,
+		       sos_luoffset_t offset_in_device,
+		       sos_genaddr_t buff_addr,
+		       sos_size_t * /* in/out */len,
+		       sos_bool_t synchronous_write,
+		       sos_bool_t bypass_pagecache)
+{
+  sos_size_t wrbytes = 0;
+
+  while (wrbytes < *len)
+    {
+      sos_ret_t retval;
+      sos_size_t offset_in_block, usrbytes;
+      sos_blkcache_access_type_t access_type;
+
+      /* Get the block at the current file offset */
+      sos_luoffset_t block_id
+	= offset_in_device / blockdev->block_size;
+      SOS_GENADDR_DECL(gaddr, buff_addr.is_user, buff_addr.addr + wrbytes);
+
+      /* reaching the end of the device ? */
+      if (block_id >= blockdev->number_of_blocks)
+	break;
+
+      usrbytes = *len - wrbytes;
+
+      /* Trying to write (part of) the remaining into page cache */
+      if (! bypass_pagecache)
+	retval = sos_fs_pagecache_write(blockdev->map_cache,
+					offset_in_device,
+					gaddr,
+					& usrbytes,
+					synchronous_write);
+      else
+	retval = -SOS_ENOENT;
+
+      if (-SOS_ENOENT != retval)
+	{
+	  /* Success ! */
+	  wrbytes          += usrbytes;
+	  offset_in_device += usrbytes;
+	  
+	  if (SOS_OK == retval)
+	    continue;
+	  else
+	    break; /* Partial write */
+	}
+
+      /* Translating this block index into an offset inside the
+	 disk */
+      block_id += blockdev->index_of_first_block;
+
+      /* Compute size of data to copy */
+      offset_in_block
+	= offset_in_device % blockdev->block_size;
+      usrbytes
+	= blockdev->block_size - offset_in_block;
+      if (*len - wrbytes < usrbytes)
+	usrbytes = *len - wrbytes;
+
+      if (usrbytes != blockdev->block_size)
+	/* Write partial block contents */
+	access_type = SOS_BLKCACHE_READ_WRITE;
+      else
+	/* Overwrite full block ! */
+	access_type = SOS_BLKCACHE_WRITE_ONLY;
+
+      /* Retrieve the block from block cache */
+      sos_vaddr_t block_data;
+      struct sos_block_cache_entry * bkcache_entry
+	= sos_blkcache_retrieve_block(blockdev->blk_cache,
+				      block_id, access_type,
+				      & block_data);
+      if (NULL == bkcache_entry)
+	break;
+
+      /* Copy the data to user */
+      retval = sos_memcpy_generic_from(block_data + offset_in_block,
+				       gaddr,
+				       usrbytes);
+
+      /* Release this block back to the blk_cache and/or to disk */
+      sos_blkcache_release_block(blockdev->blk_cache,
+				 bkcache_entry,
+				 TRUE,
+				 synchronous_write);
+
+      if (retval > 0)
+	{
+	  wrbytes          += retval;
+	  offset_in_device += retval;
+	}
+      if (retval != (sos_ret_t)usrbytes)
+	break; /* Partial write */
+    }
+
+  *len = wrbytes;
+  return SOS_OK;
+}
+
+
+struct sos_blockdev_instance *
+sos_blockdev_ref_instance(sos_ui32_t device_class,
+			  sos_ui32_t device_instance)
+{
+  struct sos_blockdev_instance * blockdev;
+  blockdev = lookup_blockdev_instance(device_class,
+				      device_instance);
+  if (NULL == blockdev)
+    return NULL;
+
+  blockdev_use_instance(blockdev);
+  return blockdev;
+}
+
+
+sos_ret_t sos_blockdev_kernel_read(struct sos_blockdev_instance * blockdev,
+				   sos_luoffset_t offset,
+				   sos_vaddr_t dest_buf,
+				   sos_size_t * /* in/out */len)
+{
+  sos_ret_t retval;
+  SOS_GENADDR_DECL(gaddr, FALSE, dest_buf);
+
+  blockdev_use_instance(blockdev);
+  retval = blockdev_generic_read(blockdev, offset, gaddr, len, FALSE);
+  sos_blockdev_release_instance(blockdev);
+
+  return retval;
+}
+
+
+sos_ret_t sos_blockdev_kernel_write(struct sos_blockdev_instance * blockdev,
+				    sos_luoffset_t offset,
+				    sos_vaddr_t src_buf,
+				    sos_size_t * /* in/out */len)
+{
+  sos_ret_t retval;
+  SOS_GENADDR_DECL(gaddr, FALSE, src_buf);
+
+  blockdev_use_instance(blockdev);
+  retval = blockdev_generic_write(blockdev, offset, gaddr, len, FALSE, FALSE);
+  sos_blockdev_release_instance(blockdev);
+
+  return retval;
+}
+
+
+/** Callback called by the pagecache to transfer a mapped page back to
+    disk */
+static sos_ret_t
+blockdev_sync_dirty_page(sos_luoffset_t offset,
+		       sos_vaddr_t dirty_page,
+		       void * custom_data)
+{
+  sos_ret_t retval;
+  struct sos_blockdev_instance * blockdev
+    = (struct sos_blockdev_instance*) custom_data;
+  sos_size_t len = SOS_PAGE_SIZE;
+  
+  SOS_GENADDR_DECL(gaddr, FALSE, dirty_page);
+
+  blockdev_use_instance(blockdev);
+  retval = blockdev_generic_write(blockdev, offset, gaddr, &len, FALSE, TRUE);
+  sos_blockdev_release_instance(blockdev);
+
+  if (SOS_OK != retval)
+    return retval;
+  
+  if (SOS_PAGE_SIZE != len)
+    return -SOS_EIO;
+
+  return SOS_OK;
+}
+
+
+sos_ret_t sos_blockdev_sync(struct sos_blockdev_instance * blockdev)
+{
+  sos_ret_t retval_bc, retval_pc;
+
+  blockdev_use_instance(blockdev);
+  retval_pc = sos_fs_pagecache_sync(blockdev->map_cache);
+  retval_bc = sos_blkcache_flush(blockdev->blk_cache);
+  sos_blockdev_release_instance(blockdev);
+
+  if (SOS_OK == retval_bc)
+    return retval_pc;
+  return retval_bc;
+}
+
+
+sos_ret_t sos_blockdev_sync_all_devices()
+{
+  int dummy = 0;
+  sos_ui64_t uid = 0;
+
+  /* This scan will be exhaustive and resilient to addition/removal of
+     devices as long as new devices are added with list_add_tail
+     (because the scan is "forward", ie in order head -> tail) */
+  while (1)
+    {
+      /* Iterate over the block devices */
+      struct sos_blockdev_instance *blockdev;
+      int nbd;
+
+      /* As long as we don't block, we can safely access the
+	 prev/next fields of the device instance */
+      list_foreach_forward(registered_blockdev_instances, blockdev, nbd)
+	{
+	  if (blockdev->uid <= uid)
+	    continue;
+
+	  uid = blockdev->uid;
+	  sos_blockdev_sync(blockdev);
+
+	  /* We must NOT continue the loops because the
+	     prev/next/current fs types/instances might have been
+	     removed or added (sync blocks, by definition) ! */
+	  goto lookup_next_bd;
+	}
+
+      /* Reached the end of the list */
+      break;
+
+    lookup_next_bd:
+      /* Loop over */
+      dummy ++;
+    }
+
+  return SOS_OK;
+}
+
+
+/* ***************************
+ * Callbacks needed by the VFS
+ */
+
+sos_ret_t sos_blockdev_helper_ref_new_fsnode(struct sos_fs_node * this)
+{
+  struct sos_blockdev_instance * blockdev;
+
+  blockdev = sos_blockdev_ref_instance(this->dev_id.device_class,
+				       this->dev_id.device_instance);
+  this->block_device      = blockdev;
+  this->new_opened_file   = blockdev_helper_new_opened_file;
+  this->close_opened_file = blockdev_helper_close_opened_file;
+  return SOS_OK;
+}
+
+
+sos_ret_t sos_blockdev_helper_release_fsnode(struct sos_fs_node * this)
+{
+  if (NULL != this->block_device)
+    return sos_blockdev_release_instance(this->block_device);
+
+  return SOS_OK;
+}
+
+
+sos_ret_t sos_blockdev_helper_sync_fsnode(struct sos_fs_node * this)
+{
+  if (NULL != this->block_device)
+    return sos_blockdev_sync(this->block_device);
+
+  return SOS_OK;  
+}
+
+
+static sos_ret_t
+blockdev_helper_new_opened_file(struct sos_fs_node * this,
+				const struct sos_process * owner,
+				sos_ui32_t open_flags,
+				struct sos_fs_opened_file ** result_of)
+{
+  /* Lookup the character device description structure */
+  struct sos_blockdev_instance * blockdev = this->block_device;
+  if (NULL == blockdev)
+    return -SOS_ENODEV;
+
+  /* Allocate the new "open file" description structure */
+  *result_of = (struct sos_fs_opened_file*)
+    sos_kmalloc(sizeof(struct sos_fs_opened_file), 0);
+  if (NULL == *result_of)
+    return -SOS_ENOMEM;
+
+  memset(*result_of, 0x0, sizeof(struct sos_fs_opened_file));
+
+  /* Initialize the read/write/seek callbacks */
+  (*result_of)->owner        = owner;
+  (*result_of)->open_flags   = open_flags;
+  (*result_of)->ops_file     = & blockdev_ops_opened_file;
+  (*result_of)->ops_blockdev = & blockdev_ops_opened_blockdev;
+
+  /* Specify the duplicate method */
+  (*result_of)->duplicate    = duplicate_opened_blockdev;
+
+  return SOS_OK;
+}
+
+
+static sos_ret_t
+blockdev_helper_close_opened_file(struct sos_fs_node * this,
+				  struct sos_fs_opened_file * of)
+{
+  return sos_kfree((sos_vaddr_t) of);
+}
+
+
+static sos_ret_t
+duplicate_opened_blockdev(struct sos_fs_opened_file *this,
+			 const struct sos_process  * for_owner,
+			 struct sos_fs_opened_file **result_of)
+{
+  /* Allocate the new "open file" description structure */
+  *result_of = (struct sos_fs_opened_file*)
+    sos_kmalloc(sizeof(struct sos_fs_opened_file), 0);
+  if (NULL == *result_of)
+    return -SOS_ENOMEM;
+
+  memcpy(*result_of, this, sizeof(struct sos_fs_opened_file));
+  (*result_of)->owner    = for_owner;
+  (*result_of)->direntry = NULL;
+
+  return SOS_OK;
+}
+
+
+void *
+sos_blockdev_get_instance_custom_data(struct sos_blockdev_instance * blockdev)
+{
+  return blockdev->custom_data;
+}
+
+
+/*
+ * FS generic block device wrapper functions
+ */
+
+static sos_ret_t blockdev_wrap_seek(struct sos_fs_opened_file *this,
+				   sos_lsoffset_t offset,
+				   sos_seek_whence_t whence,
+				   /* out */ sos_lsoffset_t * result_position)
+{
+  /* Make sure the device is supported by this driver */
+  struct sos_fs_node * fsnode = sos_fs_nscache_get_fs_node(this->direntry);
+
+  /* Artificiallly update the position in the "file" */
+  sos_lsoffset_t ref_offs;
+  sos_lsoffset_t dev_size
+    = fsnode->block_device->block_size
+      * fsnode->block_device->number_of_blocks;
+
+  *result_position = this->position;
+  switch (whence)
+    {
+    case SOS_SEEK_SET:
+      ref_offs = 0;
+      break;
+
+    case SOS_SEEK_CUR:
+      ref_offs = this->position;
+      break;
+
+    case SOS_SEEK_END:
+      ref_offs = dev_size;
+      break;
+
+    default:
+      return -SOS_EINVAL;
+    }
+
+  /* Forbid accesses "before" the start of the device */
+  if (offset < -ref_offs)
+    return -SOS_EINVAL;
+
+  /* Forbid accesses "after" the end of the device */
+  else if (ref_offs + offset > dev_size)
+    return -SOS_EINVAL;
+  
+  this->position = ref_offs + offset;
+  *result_position = this->position;
+  return SOS_OK;
+}
+
+
+static sos_ret_t blockdev_wrap_read(struct sos_fs_opened_file *this,
+				    sos_uaddr_t dest_buf,
+				    sos_size_t * /* in/out */len)
+{
+  struct sos_fs_node * fsnode = sos_fs_nscache_get_fs_node(this->direntry);
+  struct sos_blockdev_instance * blockdev = fsnode->block_device;
+
+  SOS_GENADDR_DECL(gaddr, TRUE, dest_buf);
+  sos_ret_t retval = blockdev_generic_read(blockdev, this->position,
+					   gaddr, len, FALSE);
+  this->position += *len;
+  return retval;
+}
+
+
+static sos_ret_t blockdev_wrap_write(struct sos_fs_opened_file *this,
+				     sos_uaddr_t src_buf,
+				     sos_size_t * /* in/out */len)
+{
+  struct sos_fs_node * fsnode = sos_fs_nscache_get_fs_node(this->direntry);
+  struct sos_blockdev_instance * blockdev = fsnode->block_device;
+
+  SOS_GENADDR_DECL(gaddr, TRUE, src_buf);
+  sos_ret_t retval
+    = blockdev_generic_write(blockdev, this->position,
+			     gaddr, len,
+			     this->open_flags & SOS_FS_OPEN_SYNC,
+			     FALSE);
+  this->position += *len;
+  return retval;
+}
+
+
+static sos_ret_t blockdev_wrap_mmap(struct sos_fs_opened_file *this,
+				   sos_uaddr_t *uaddr, sos_size_t size,
+				   sos_ui32_t access_rights,
+				   sos_ui32_t flags,
+				   sos_luoffset_t offset)
+{
+  struct sos_fs_node * fsnode = sos_fs_nscache_get_fs_node(this->direntry);
+  struct sos_blockdev_instance * blockdev = fsnode->block_device;
+
+  if (! SOS_IS_PAGE_ALIGNED(offset))
+    return -SOS_EINVAL;
+
+  return sos_umem_vmm_map(sos_process_get_address_space(this->owner),
+			  uaddr, size, access_rights,
+			  flags, & blockdev->mapres, offset);
+}
+
+
+
+static sos_ret_t blockdev_wrap_fcntl(struct sos_fs_opened_file *this,
+				    int req_id,
+				    sos_ui32_t req_arg)
+{
+  return -SOS_ENOSYS;
+}
+
+
+static sos_ret_t blockdev_wrap_ioctl(struct sos_fs_opened_file *this,
+				    int req_id,
+				    sos_ui32_t req_arg)
+{
+  struct sos_fs_node * fsnode = sos_fs_nscache_get_fs_node(this->direntry);
+  struct sos_blockdev_instance * blockdev = fsnode->block_device;
+
+  if (req_id == SOS_IOCTL_BLOCKDEV_SYNC)
+    return sos_blockdev_sync(blockdev);
+
+  if (blockdev->operations->ioctl)
+    return blockdev->operations->ioctl(this, req_id, req_arg);
+
+  return -SOS_ENOSYS;
+}
+
+
+static struct sos_fs_ops_opened_file blockdev_ops_opened_file
+  = (struct sos_fs_ops_opened_file) {
+    .seek  = blockdev_wrap_seek,
+    .read  = blockdev_wrap_read,
+    .write = blockdev_wrap_write,
+    .mmap  = blockdev_wrap_mmap,
+    .fcntl = blockdev_wrap_fcntl
+  };
+
+
+static struct sos_fs_ops_opened_blockdev blockdev_ops_opened_blockdev
+  = (struct sos_fs_ops_opened_blockdev) {
+    .ioctl = blockdev_wrap_ioctl
+  };
+
+
+/* ****************************
+ * Callbacks needed by umem_vmm
+ */
+
+inline static struct sos_blockdev_instance *
+get_blockdev_of_vr(struct sos_umem_vmm_vr * vr)
+{
+  struct sos_umem_vmm_mapped_resource *mr
+    = sos_umem_vmm_get_mapped_resource_of_vr(vr);
+
+  return (struct sos_blockdev_instance *)mr->custom_data;
+}
+
+
+static void blockdev_map_ref(struct sos_umem_vmm_vr * vr)
+{
+  struct sos_blockdev_instance * blockdev = get_blockdev_of_vr(vr);
+  blockdev_use_instance(blockdev);
+}
+
+
+static void blockdev_map_unref(struct sos_umem_vmm_vr * vr)
+{
+  struct sos_blockdev_instance * blockdev = get_blockdev_of_vr(vr);
+  sos_uaddr_t    vr_start     = sos_umem_vmm_get_start_of_vr(vr);
+  sos_uaddr_t    vr_size      = sos_umem_vmm_get_size_of_vr(vr);
+  sos_luoffset_t start_offset = sos_umem_vmm_get_offset_in_resource(vr);
+  sos_ui32_t     vr_flags     = sos_umem_vmm_get_flags_of_vr(vr);
+
+  /* For a shared mapping, un-reference the pagecache entries, if
+     any */
+  if (vr_flags & SOS_VR_MAP_SHARED)
+    {
+      sos_uaddr_t uaddr;
+      for (uaddr = vr_start ;
+	   uaddr < vr_start + vr_size ;
+	   uaddr += SOS_PAGE_SIZE)
+	{
+	  sos_paddr_t paddr;
+	  sos_luoffset_t blockdev_offset;
+	  paddr = sos_paging_get_paddr(uaddr);
+	  if (! paddr)
+	    /* No physical page mapped at this address yet */
+	    continue;
+
+	  /* Unreference the corresponding pagecache entry */
+	  blockdev_offset = SOS_PAGE_ALIGN_INF(uaddr) - vr_start;
+	  blockdev_offset += start_offset;
+
+	  /* Flush any change to disk */
+	  if (sos_paging_is_dirty(paddr))
+	    sos_fs_pagecache_set_dirty(blockdev->map_cache, blockdev_offset,
+				       FALSE);
+
+	  SOS_ASSERT_FATAL(SOS_OK ==
+			   sos_fs_pagecache_unref_page(blockdev->map_cache,
+						       blockdev_offset));
+	}
+    }
+
+  sos_blockdev_release_instance(blockdev);
+}
+
+
+static sos_ret_t blockdev_map_page_in(struct sos_umem_vmm_vr * vr,
+				    sos_uaddr_t uaddr,
+				    sos_bool_t write_access)
+{
+  struct sos_blockdev_instance * blockdev = get_blockdev_of_vr(vr);
+  sos_uaddr_t    vr_start     = sos_umem_vmm_get_start_of_vr(vr);
+  sos_uaddr_t    vr_size      = sos_umem_vmm_get_size_of_vr(vr);
+  sos_luoffset_t start_offset = sos_umem_vmm_get_offset_in_resource(vr);
+  sos_luoffset_t offset_in_device;
+  
+  sos_bool_t     read_from_blkcache = TRUE; /**< Need to fill the page
+					       from blkcache ? */
+  sos_vaddr_t    kernel_page; /** Use a (temporary) kernel-mapped page to
+				  store the contents of the page */
+  struct sos_fs_pagecache_entry * pagecache_entry = NULL;
+  sos_ret_t retval;
+
+  SOS_ASSERT_FATAL(vr_size > 0);
+  
+  /* Make sure the access is within the block device limits */
+  offset_in_device  = uaddr - vr_start + sizeof(int) - 1;
+  offset_in_device += start_offset;
+  offset_in_device /= blockdev->block_size;
+  if (offset_in_device >= blockdev->number_of_blocks)
+    return -SOS_EFAULT;
+
+  /*
+   * We have to map:
+   *  - shared mappings: a fresh page from disk if it is not yet in the
+   *    page cache
+   *  - private mappings: a fresh page from disk (always)
+   */
+
+  /* Compute block index of beginning of page */
+  offset_in_device  = SOS_PAGE_ALIGN_INF(uaddr) - vr_start;;
+  offset_in_device += start_offset;
+
+  if (sos_umem_vmm_get_flags_of_vr(vr) & SOS_VR_MAP_SHARED)
+    {
+      /* This is a shared mapping ! */
+      sos_bool_t  newly_allocated;
+      pagecache_entry = sos_fs_pagecache_ref_page(blockdev->map_cache,
+						  offset_in_device,
+						  & kernel_page,
+						  & newly_allocated);
+      if (! pagecache_entry)
+	return -SOS_EFAULT;
+
+      if (! newly_allocated)
+	read_from_blkcache = FALSE;
+    }
+  else
+    {
+      /* Allocate a temporary kernel page */
+      kernel_page = sos_kmem_vmm_alloc(1, SOS_KMEM_VMM_MAP);
+      if ((sos_vaddr_t)NULL == kernel_page)
+	return -SOS_EFAULT;
+    }
+
+  retval = SOS_OK;
+
+  /* Need to fill the kernel page ? */
+  if (read_from_blkcache)
+    {
+      SOS_GENADDR_DECL(gaddr, FALSE, kernel_page);
+      sos_size_t rdlen = SOS_PAGE_SIZE;
+      retval = blockdev_generic_read(blockdev,
+				     offset_in_device,
+				     gaddr,
+				     & rdlen,
+				     TRUE);
+      if (SOS_PAGE_SIZE != rdlen)
+	retval = -SOS_EIO;
+    }
+
+  /* Make sure nobody mapped anything in the meantime */
+  if ((sos_paddr_t)NULL != sos_paging_get_paddr(uaddr))
+    retval = -SOS_EBUSY;
+
+  if (SOS_OK == retval)
+    {
+      sos_paddr_t ppage_paddr = sos_paging_get_paddr(kernel_page);
+      if (0 != ppage_paddr)
+	retval = sos_paging_map(ppage_paddr,
+				SOS_PAGE_ALIGN_INF(uaddr),
+				TRUE,
+				sos_umem_vmm_get_prot_of_vr(vr));
+      else
+	retval = -SOS_EFAULT;
+    }
+
+  /* Shared mapping: unlock the entry and eventually remove it from
+     the cache in case of failure of the preceding operations */
+  if (NULL != pagecache_entry)
+    {
+      sos_fs_pagecache_unlock_page(blockdev->map_cache,
+				   pagecache_entry,
+				   (SOS_OK != retval));
+    }
+  /* Private mappings: remove the remporary kernel page */
+  else
+    sos_kmem_vmm_free((sos_vaddr_t)kernel_page);
+
+  return retval;
+}
+
+
+/**
+ * umem_vmm callback for msync
+ */
+static sos_ret_t blockdev_map_sync_page(struct sos_umem_vmm_vr * vr,
+				      sos_uaddr_t page_uaddr,
+				      sos_ui32_t flags)
+{
+  struct sos_blockdev_instance * blockdev = get_blockdev_of_vr(vr);
+  sos_uaddr_t    vr_start     = sos_umem_vmm_get_start_of_vr(vr);
+  sos_luoffset_t start_offset = sos_umem_vmm_get_offset_in_resource(vr);
+  sos_luoffset_t offset;
+
+  /* Make sure the access is within the block device limits */
+  offset  = page_uaddr - vr_start;
+  offset += start_offset;
+  if (offset >= blockdev->number_of_blocks*blockdev->block_size)
+    return -SOS_EFAULT;
+
+  /* Mark the page as non dirty now */
+  sos_paging_set_dirty(page_uaddr, FALSE);
+
+  /* Simply mark the corresponding pagecache entry as dirty */
+  return sos_fs_pagecache_set_dirty(blockdev->map_cache,
+				    offset,
+				    flags & SOS_MSYNC_SYNC);
+}
+
+
+static struct sos_umem_vmm_vr_ops blockdev_map_ops
+  = (struct sos_umem_vmm_vr_ops){
+    .ref       = blockdev_map_ref,
+    .unref     = blockdev_map_unref,
+    .page_in   = blockdev_map_page_in,
+    .sync_page = blockdev_map_sync_page
+  };
+
+
+/** Callback called each time an area of the device is mapped into
+    user space */
+static sos_ret_t blockdev_new_mapping(struct sos_umem_vmm_vr * vr)
+{
+  struct sos_blockdev_instance * blockdev = get_blockdev_of_vr(vr);
+  sos_luoffset_t start_offset = sos_umem_vmm_get_offset_in_resource(vr);
+  sos_size_t     map_size     = sos_umem_vmm_get_size_of_vr(vr);
+  sos_luoffset_t stop_offset;
+  sos_luoffset_t block_index;
+
+  if (map_size <= 0)
+    return -SOS_EINVAL;
+
+  /* Make sure request lies inside the device */
+  stop_offset = start_offset + map_size - 1;
+  block_index = stop_offset / blockdev->block_size;
+  if (block_index >= blockdev->number_of_blocks)
+    return -SOS_EINVAL;
+
+  return sos_umem_vmm_set_ops_of_vr(vr, &blockdev_map_ops);
+}
+
diff -ruNwb /tmp/sos-code-article9/sos/blkdev.h ../sos-code-article9.5/sos/blkdev.h
--- /tmp/sos-code-article9/sos/blkdev.h	1970-01-01 01:00:00.000000000 +0100
+++ ../sos-code-article9.5/sos/blkdev.h	2006-01-22 12:48:30.000000000 +0100
@@ -0,0 +1,225 @@
+/* Copyright (C) 2005,2006      David Decotigny
+
+   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., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
+   USA.
+*/
+#ifndef _SOS_BLKDEV_H_
+#define _SOS_BLKDEV_H_
+
+/**
+ * @file blkdev.h
+ *
+ * Interface between the VFS and the "block" devices (real devices and
+ * their partitions). The following functions provide the mechanisms
+ * to bind the "block device" nodes (@see mknod) to their device
+ * driver.
+ *
+ * The "blkdev" layer is to be perceived as a FS-agnostic layer both
+ * below and on top of the FS that binds the special "block device"
+ * nodes to a set of system-wide block_read/block_write functions and
+ * which supports the cache of blocks for the device.
+ *
+ * The differences between the char device and the block device layers are:
+ * - A character device is byte-stream-oriented: one can easily fetch
+ *   the data character by character. Theoretically, this stream has
+ *   no limitation in size: it has a beginning but not necessarly an
+ *   end. One can not necessarly rewind the stream, that is seek
+ *   anywhere in it (as for a stream of audio data coming from a
+ *   soundcard for example). But for some character devices, it is
+ *   possible, though (it is possible to seek anywhere in /dev/zero
+ *   for example). For some other devices, it is possible to seek only
+ *   in some authorized regions (as for /dev/kmem for example: only
+ *   the mapped kernel regions are available).
+ * - A block device is block-oriented: one can easily fetch fixed-size
+ *   blocks of data anywhere inside the device, possibly in a random
+ *   order. The capacity of the device is limited: there is a defined
+ *   number of contiguous blocks reachable in the device, one cannot
+ *   seek beyond its limits.
+ * - A block device is well suited to a cache layer: it is possible to
+ *   cache the most used blocks inside memory to speed up the accesses
+ *   to data. It doesn't make sense to do so on stream-oriented devices
+ *   like character devices.
+ * - A character device is limited to the interaction between
+ *   user-mode programs and the devices themselves. The kernel only acts
+ *   as an intermediary and does not take care of the operations
+ *   carried out.
+ * - A block device is used as the underlying data support for a
+ *   filesystem. As a result, it must be managed by the kernel itself
+ *   since the FS code resides inside the kernel. It must offer an API
+ *   compatible with kernel-space interaction.
+ *
+ * A partition has the same properties as a disk: both are block
+ * devices. The main difference is that a disk really "owns" its
+ * operations (ie the read/write/ioctl operations are defined by him)
+ * and its block cache, whereas a partition shares them with its
+ * parent disk/partition. The partitions may be nested: a disk can
+ * have partitions, which can have sub-partitions, which can have
+ * sub-sub-partittions, etc. As a consequence, a parent disk/partition
+ * cannot be unregistered as long as it owns any child partitions. The
+ * only constraint being that the partitions must fit completely
+ * inside their parent partition/disk. However, no check is made
+ * regarding partitions overlapping one another...
+ *
+ * This implementation is based on two subsystems:
+ * - the block cache to accelerate accesses to the hardware by caching
+ *   the most frequently used blocks in main memory
+ * - the page cache to guarantee consistency between read/write
+ *   accesses and MMU access of mapped pages (mmap API). This is a
+ *   simple dictionary of the device's mapped pages: offset -> mapped
+ *   page. The read/write vs MMU consistency is achieved in a simple
+ *   manner: before each read/write accesses, we first try to use the
+ *   page cache, and eventually the block cache if the data is not
+ *   mapped by anybody.
+ */
+
+/* Forward declaration */
+struct sos_blockdev_instance;
+
+#include <sos/fs.h>
+
+/**
+ * The fundamental callbacks for a real block device (ie not a
+ * partition).
+ */
+struct sos_blockdev_operations {
+
+  /** @note MANDATORY */
+  sos_ret_t (*read_block)(void * blockdev_instance_custom_data,
+			  sos_vaddr_t dest_buf /* Kernel address */,
+			  sos_luoffset_t block_offset);
+
+
+  /** @note Optional (may be NULL) */
+  sos_ret_t (*write_block)(void * blockdev_instance_custom_data,
+			   sos_vaddr_t src_buf /* Kernel address */,
+			   sos_luoffset_t block_offset);
+
+
+  /**
+   * @note Optional (may be NULL)
+   * @note Also called when an ioctl is made to a partition
+   */
+  sos_ret_t (*ioctl)(void * blockdev_instance_custom_data,
+		     int req_id,
+		     sos_ui32_t req_arg /* Usually: sos_uaddr_t */);
+};
+
+
+/*
+ * Functions restricted to block device driver code
+ */
+
+
+sos_ret_t sos_blockdev_subsystem_setup(void);
+
+
+/**
+ * Contrary to Character devices, block devices are registered
+ * individually: any single registration corresponds to a driver for a
+ * single device instance
+ */
+sos_ret_t
+sos_blockdev_register_disk (sos_ui32_t     device_class,
+			    sos_ui32_t     device_instance,
+			    sos_size_t     block_size,
+			    sos_lcount_t   number_of_blocks,
+			    sos_count_t    cache_size_in_blocks,
+			    struct sos_blockdev_operations * blockdev_ops,
+			    void * blockdev_instance_custom_data);
+
+
+/**
+ * @param index_of_first_block is the index of the first block
+ * relative to parent_bd, NOT relative to the top-most disk block
+ * device
+ */
+sos_ret_t
+sos_blockdev_register_partition(sos_ui32_t device_class,
+				sos_ui32_t device_instance,
+				struct sos_blockdev_instance * parent_bd,
+				sos_luoffset_t index_of_first_block,
+				sos_lcount_t number_of_blocks,
+				void * blockdev_instance_custom_data);
+
+
+sos_ret_t sos_blockdev_unregister_device (sos_ui32_t device_class,
+					  sos_ui32_t device_instance);
+
+
+/**
+ * Flush all caches of all devices to disk
+ */
+sos_ret_t sos_blockdev_sync_all_devices(void);
+
+
+/**
+ * Increments the instance's reference counter: this will make any
+ * blockdev_unregister return BUSY. As a consequence, the following
+ * operations on blockdev instances should be as fast as possible and
+ * call release_instance as early as possible
+ */
+struct sos_blockdev_instance *
+sos_blockdev_ref_instance(sos_ui32_t device_class,
+			  sos_ui32_t device_instance);
+
+
+sos_ret_t
+sos_blockdev_release_instance(struct sos_blockdev_instance * blockdev);
+
+
+/** Read data from disk and use it directly in the kernel. Mostly used
+    by the partition drivers to identify the partitions of a disk and
+    register them */
+sos_ret_t sos_blockdev_kernel_read(struct sos_blockdev_instance * blockdev,
+				   sos_luoffset_t offset,
+				   sos_vaddr_t dest_buf,
+				   sos_size_t * /* in/out */len);
+
+
+/**
+ * Write data to disk directly from the kernel. Mostly used by the
+ * partition drivers to modify the partitions of a disk and register
+ * them.
+ *
+ * @note The operation is normally NOT needed (fdisk is a userspace
+ * program) and is NOT synchronous
+ */
+sos_ret_t sos_blockdev_kernel_write(struct sos_blockdev_instance * blockdev,
+				    sos_luoffset_t offset,
+				    sos_vaddr_t src_buf,
+				    sos_size_t * /* in/out */len);
+
+
+/**
+ * Flush the modified blocks back to hardware (both block and page
+ * cache are flusehd)
+ */
+sos_ret_t sos_blockdev_sync(struct sos_blockdev_instance * blockdev);
+
+
+/*
+ * Callbacks and functions restricted to fs.c internals
+ */
+
+/**
+ * Update the FS node ops_blockdev callbacks after an FS
+ * allocate_new_node or fetch_node_from_disk, in order to point to
+ * the block layer API functions
+ */
+sos_ret_t sos_blockdev_helper_ref_new_fsnode(struct sos_fs_node * this);
+sos_ret_t sos_blockdev_helper_release_fsnode(struct sos_fs_node * this);
+sos_ret_t sos_blockdev_helper_sync_fsnode(struct sos_fs_node * this);
+
+#endif
diff -ruNwb /tmp/sos-code-article9/sos/calcload.c ../sos-code-article9.5/sos/calcload.c
--- /tmp/sos-code-article9/sos/calcload.c	2005-12-28 11:44:57.000000000 +0100
+++ ../sos-code-article9.5/sos/calcload.c	2006-01-22 12:48:30.000000000 +0100
@@ -125,7 +125,7 @@
 static void calcload_routine(struct sos_timeout_action *a);
 
 
-static void _reinit_load_subsystem()
+static void _reinit_load_subsystem(void)
 {
   memset(& recorded_loads, 0x0, sizeof(recorded_loads));
   memset(& current_load_entry, 0x0, sizeof(struct load_entry));
diff -ruNwb /tmp/sos-code-article9/sos/chardev.c ../sos-code-article9.5/sos/chardev.c
--- /tmp/sos-code-article9/sos/chardev.c	2005-12-28 11:44:57.000000000 +0100
+++ ../sos-code-article9.5/sos/chardev.c	2006-01-22 12:48:30.000000000 +0100
@@ -1,4 +1,4 @@
-/* Copyright (C) 2005      David Decotigny, Thomas Petazzoni
+/* Copyright (C) 2005      David Decotigny
 
    This program is free software; you can redistribute it and/or
    modify it under the terms of the GNU General Public License
@@ -62,8 +62,6 @@
 static struct sos_fs_ops_opened_chardev chardev_ops_opened_chardev;
 
 static sos_ret_t
-chardev_helper_sync(struct sos_fs_node * this);
-static sos_ret_t
 chardev_helper_new_opened_file(struct sos_fs_node * this,
 			       const struct sos_process * owner,
 			       sos_ui32_t open_flags,
@@ -147,7 +145,6 @@
 
 sos_ret_t sos_chardev_helper_ref_new_fsnode(struct sos_fs_node * this)
 {
-  this->sync              = chardev_helper_sync;
   this->new_opened_file   = chardev_helper_new_opened_file;
   this->close_opened_file = chardev_helper_close_opened_file;
 
@@ -161,14 +158,6 @@
 }
 
 
-/** No synchronization to anything needed for a character device */
-static sos_ret_t
-chardev_helper_sync(struct sos_fs_node * this)
-{
-  return SOS_OK;
-}
-
-
 /** Callback called each time an FS-node is opened by a user process:
     create a new sos_chardev_opened_file object associated to the
     correct major number, and call the device driver's open method */
diff -ruNwb /tmp/sos-code-article9/sos/fs.c ../sos-code-article9.5/sos/fs.c
--- /tmp/sos-code-article9/sos/fs.c	2005-12-28 11:44:57.000000000 +0100
+++ ../sos-code-article9.5/sos/fs.c	2006-01-22 12:48:30.000000000 +0100
@@ -1,4 +1,4 @@
-/* Copyright (C) 2005      David Decotigny
+/* Copyright (C) 2005,2006 David Decotigny
    Copyright (C) 2000-2005 The KOS Team (Thomas Petazzoni, David
                            Decotigny, Julien Munier)
 
@@ -183,6 +183,8 @@
     {
       if (SOS_FS_NODE_DEVICE_CHAR == node->type)
 	sos_chardev_helper_release_fsnode(node);
+      else if (SOS_FS_NODE_DEVICE_BLOCK == node->type)
+	sos_blockdev_helper_release_fsnode(node);
       sos_hash_remove(node->fs->nodecache, node);
       node->destructor(node);
     }
@@ -253,6 +255,10 @@
   if (SOS_FS_NODE_DEVICE_CHAR == (*result_fsnode)->type)
     SOS_ASSERT_FATAL(SOS_OK
 		     == sos_chardev_helper_ref_new_fsnode(*result_fsnode));
+  else if (SOS_FS_NODE_DEVICE_BLOCK == (*result_fsnode)->type)
+    SOS_ASSERT_FATAL(SOS_OK
+		     == sos_blockdev_helper_ref_new_fsnode(*result_fsnode));
+
   sos_hash_insert(fs->nodecache, *result_fsnode);
   return SOS_OK;
 }
@@ -292,6 +298,12 @@
       SOS_ASSERT_FATAL(SOS_OK
 		       == sos_chardev_helper_ref_new_fsnode(*result_fsnode));
     }
+  else if (SOS_FS_NODE_DEVICE_BLOCK == (*result_fsnode)->type)
+    {
+      SOS_ASSERT_FATAL(SOS_OK
+		       == sos_blockdev_helper_ref_new_fsnode(*result_fsnode));
+    }
+
 
   /* insert it in the node cache */
   retval = sos_hash_insert(fs->nodecache, *result_fsnode);
@@ -327,21 +339,7 @@
 			prev_dirty, next_dirty);
 
   if (force_sync || (fsnode->fs->flags & SOS_FS_MOUNT_SYNC))
-    {
-      /* Commit the node changes to the FS */
-      if (SOS_OK == sos_fs_sync_node(fsnode))
-	{
-	  /* Commit the FS changes to the device */
-	  if (SOS_OK
-	      == fsnode->fs->device->sync(fsnode->fs->device))
-	    return SOS_OK;
-
-	  /* We got a problem: FORCE re-add the node to the dirty list */
-	  was_dirty = FALSE;
-	  fsnode->dirty = TRUE;
-	  retval = -SOS_EBUSY;
-	}
-    }
+    return sos_fs_sync_node(fsnode);
 
   return retval;
 }
@@ -355,7 +353,7 @@
   if (! fsnode->dirty)
     return SOS_OK;
 
-  retval = fsnode->sync(fsnode);
+  retval = fsnode->ops_file->sync(fsnode);
   if (SOS_OK != retval)
     return retval;
 
@@ -364,6 +362,20 @@
 		    prev_dirty, next_dirty);
   fsnode->dirty = FALSE;
 
+  /* Commit the FS changes to the device */
+  if (fsnode->fs->device)
+    {
+      retval = sos_blockdev_helper_sync_fsnode(fsnode->fs->device);
+      if (SOS_OK != retval)
+	{
+	  /* We got a problem ! Mark the node dirty again */
+	  list_add_tail_named(fsnode->fs->dirty_nodes, fsnode,
+			      prev_dirty, next_dirty);
+	  fsnode->dirty = TRUE;
+	  return retval;
+	}
+    }
+
   return SOS_OK;
 }
 
@@ -381,9 +393,6 @@
 	return retval;
     }
   
-  if (NULL != fs->device)
-      return fs->device->sync(fs->device);
-
   return SOS_OK;
 }
 
@@ -1231,6 +1240,13 @@
 
       return of->ops_chardev->ioctl(of, req_id, req_arg);
     }
+  else if (fsnode->type == SOS_FS_NODE_DEVICE_BLOCK)
+    {
+      if (! of->ops_blockdev->ioctl)
+	return -SOS_ENOSYS;
+
+      return of->ops_blockdev->ioctl(of, req_id, req_arg);
+    }
   
   return -SOS_ENOSYS;
 }
@@ -1719,7 +1735,8 @@
   struct sos_fs_nscache_node * nsnode;
   struct sos_fs_node * fsnode;
 
-  if (type != SOS_FS_NODE_DEVICE_CHAR)
+  if ((type != SOS_FS_NODE_DEVICE_BLOCK)
+      && (type != SOS_FS_NODE_DEVICE_CHAR))
     return -SOS_EINVAL;
 
   path.contents = _path;
@@ -1786,7 +1803,7 @@
 sos_ret_t sos_fs_fsync(struct sos_fs_opened_file * of)
 {
   struct sos_fs_node * fsnode = sos_fs_nscache_get_fs_node(of->direntry);
-  return fsnode->sync(fsnode);
+  return sos_fs_sync_node(fsnode);
 }
 
 
@@ -1898,7 +1915,14 @@
 	  struct sos_fs_manager_instance * fs;
 	  int ninst;
 
-	  list_foreach(fstype->instances, fs, ninst)
+	  /* This scan will be exhaustive and resilient to
+	     addition/removal of file systems as long as new FS are
+	     added with list_add_tail (because the scan is "forward",
+	     ie in order head -> tail) */
+
+	  /* As long as we don't block, we can safely access the
+	     prev/next fields of the FS instance */
+	  list_foreach_forward(fstype->instances, fs, ninst)
 	    {
 	      if (fs->uid <= uid)
 		continue;
@@ -1913,12 +1937,16 @@
 	    }
 	}
 
+      /* Reached the end of the list */
+      break;
+
     lookup_next_fs:
       /* Loop over */
       dummy ++;
     }
 
-  return SOS_OK;
+  /* Now flush all the block devices to disk */
+  return sos_blockdev_sync_all_devices();
 }
 
 
diff -ruNwb /tmp/sos-code-article9/sos/fs.h ../sos-code-article9.5/sos/fs.h
--- /tmp/sos-code-article9/sos/fs.h	2005-12-28 11:44:57.000000000 +0100
+++ ../sos-code-article9.5/sos/fs.h	2006-01-22 12:48:30.000000000 +0100
@@ -1,4 +1,4 @@
-/* Copyright (C) 2005      David Decotigny
+/* Copyright (C) 2005,2006 David Decotigny
    Copyright (C) 2000-2005 The KOS Team (Thomas Petazzoni, David
                            Decotigny, Julien Munier)
 
@@ -161,6 +161,7 @@
 struct sos_fs_stat;
 
 #include "fs_nscache.h"
+#include <sos/blkdev.h>
 
 /**
  * The type of filesystem object.
@@ -172,6 +173,7 @@
   SOS_FS_NODE_DIRECTORY    = 0x24,
   SOS_FS_NODE_SYMLINK      = 0x84,
   SOS_FS_NODE_DEVICE_CHAR  = 0x48,
+  SOS_FS_NODE_DEVICE_BLOCK = 0x12
 } sos_fs_node_type_t;
 
 
@@ -445,20 +447,20 @@
     /** when type == SOS_FS_NODE_DIRECTORY */
     struct sos_fs_node_ops_dir      *ops_dir;
 
+    /**
+     * when type == SOS_FS_NODE_DEVICE_BLOCK
+     * The FS node has a link to some data pertaining to the device,
+     * not to any special operations
+     * @see blkdev.c for a definition of this structure
+     */
+    struct sos_blockdev_instance *block_device;
+
     /** when type == SOS_FS_NODE_SYMLINK */
     struct sos_fs_node_ops_symlink  *ops_symlink;
   }; /* Anonymous union (gcc extension) */
 
 
   /**
-   * Flush any change to disk
-   *
-   * @note Mandatory, may block. Appropriate locking MUST be implemented
-   */
-  sos_ret_t (*sync)(struct sos_fs_node *this);
-
-
-  /**
    * Simply free this FS node from the kernel memory: it does NOT
    * mean that the corresponding on-disk node is free ! Actually, the
    * corresponding ON-DISK node is free iff ondisk_lnk_cnt == 0. No
@@ -544,6 +546,14 @@
    */
   sos_ret_t (*chmod)(struct sos_fs_node * this,
 		     sos_ui32_t new_access_rights);
+
+
+  /**
+   * Flush any change to the node back to the file system backing store
+   *
+   * @note Mandatory, may block. Appropriate locking MUST be implemented
+   */
+  sos_ret_t (*sync)(struct sos_fs_node *this);
 };
 
 
@@ -674,6 +684,9 @@
 
     /** when direntry->fs_node->type == SOS_FS_NODE_DEVICE_CHAR */
     struct sos_fs_ops_opened_chardev  * ops_chardev;
+
+    /** when direntry->fs_node->type == SOS_FS_NODE_DEVICE_BLOCK */
+    struct sos_fs_ops_opened_blockdev * ops_blockdev;
   }; /* Anonymous union (gcc extension) */
 
   /**
@@ -777,6 +790,25 @@
 };
 
 
+/**
+ * The list of methods implementing the basic VFS opened block device
+ * operations
+ *
+ * @see sos_fs_opened_file::ops_file
+ */
+struct sos_fs_ops_opened_blockdev
+{
+  /**
+   * @note Optional (might be NULL), may block. Appropriate locking
+   * MUST be implemented
+   * @note Please call sos_fs_mark_dirty() if disk contents is changed
+   */
+  sos_ret_t (*ioctl)(struct sos_fs_opened_file *this,
+		     int req_id,
+		     sos_ui32_t req_arg /* Usually: sos_uaddr_t */);
+};
+
+
 /** Data structure that is to be filled by readdir */
 struct sos_fs_dirent
 {
@@ -889,7 +921,7 @@
 /**
  * Flush all the dirty nodes of all the FS to disk
  */
-sos_ret_t sos_fs_sync_all_fs();
+sos_ret_t sos_fs_sync_all_fs(void);
 
 /**
  * Retrieve filesystem status, or return -SOS_ENOSYS if filesystem
diff -ruNwb /tmp/sos-code-article9/sos/fs_nscache.h ../sos-code-article9.5/sos/fs_nscache.h
--- /tmp/sos-code-article9/sos/fs_nscache.h	2005-12-28 11:44:57.000000000 +0100
+++ ../sos-code-article9.5/sos/fs_nscache.h	2006-01-22 12:48:31.000000000 +0100
@@ -99,7 +99,7 @@
 };
 
 
-sos_ret_t sos_fs_nscache_subsystem_setup();
+sos_ret_t sos_fs_nscache_subsystem_setup(void);
 
 
 /**
diff -ruNwb /tmp/sos-code-article9/sos/fs_pagecache.c ../sos-code-article9.5/sos/fs_pagecache.c
--- /tmp/sos-code-article9/sos/fs_pagecache.c	1970-01-01 01:00:00.000000000 +0100
+++ ../sos-code-article9.5/sos/fs_pagecache.c	2006-01-22 12:48:31.000000000 +0100
@@ -0,0 +1,642 @@
+/* Copyright (C) 2005,2006      David Decotigny
+
+   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., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
+   USA.
+*/
+#include <sos/ksynch.h>
+#include <sos/kmem_slab.h>
+#include <sos/hash.h>
+#include <sos/physmem.h> /* For SOS_PAGE_MASK */
+#include <sos/list.h>
+#include <sos/assert.h>
+#include <sos/uaccess.h>
+#include <sos/kmalloc.h>
+
+#include "fs_pagecache.h"
+
+
+#define SOS_OFFSET64_PAGE_ALIGN_INF(offs64) \
+  ( ((sos_luoffset_t)(offs64)) & (~ ((sos_luoffset_t)(SOS_PAGE_MASK))) )
+
+
+#define SOS_OFFSET64_IS_PAGE_ALIGNED(offs64) \
+  ( ( ((sos_luoffset_t)(offs64)) & (((sos_luoffset_t)(SOS_PAGE_MASK))) ) == 0 )
+
+
+/**
+ * Definition of an object holding a reference to a shared mapping of
+ * a file/device-mapped cache page.
+ *
+ * @note This structure is huge. We can shrink it largely by removing the
+ * "name" field from the lock structure (32 bytes).
+ */
+struct sos_fs_pagecache_entry
+{
+  /** offset of the cached page in the file or device */
+  sos_luoffset_t file_offset;
+
+  /** Address of the cached page for this offset */
+  sos_vaddr_t    kernel_vaddr;
+
+  struct sos_kmutex lock;
+  sos_count_t ref_cnt;
+
+  sos_bool_t initial_fill_aborted; /**< True when the page could not
+				      be correctly filled */
+
+  /**
+   * When 0: the page is clean wrt to read/write syscalls, ie the disk
+   * contents reflect the contents of the page since the last
+   * read/write operation. However, the disk may NOT be in sync wrt to
+   * mmap() operations: if mmap() operations occured in the meantime,
+   * the disk may NOT be up to date, and the pagecache entry may even
+   * NOT be considered dirty. This is because we do not trace each of
+   * the read/write MMU operations from every processes (this would
+   * need to catch all writes even on read/write mapped pages) and we
+   * don't have a reverse mapping available to set the page read-only
+   * in every mappings once it has been synched to disk (to
+   * effectively trace the dirty state relative to mmap operations).
+   *
+   * When ">0": at least one process changed the contents of the page
+   * through read/write syscalls since last sync operation.
+   *
+   * @note A boolean is enough for 99% of the code. But we need a real
+   * counter for the sos_fs_pagecache_sync operation to make sure we
+   * don't iterate 2 times over the same page.
+   */
+  sos_lcount_t rw_dirty_order;
+#define ENTRY_IS_RW_DIRTY(e) ((e)->rw_dirty_order > 0)
+
+  /** Linkage structure to keep the cache entry in the hash map */
+  struct sos_hash_linkage hlink;
+
+  /** Links to insert the entry into the rw_sync/rw_dirty lists */
+  struct sos_fs_pagecache_entry *prev, *next;
+};
+
+
+struct sos_fs_pagecache
+{
+  /** The operation used to synchronize the mapped pages with the
+      backing store */
+  sos_fs_pagecache_sync_function_t sync_fct;
+  void * sync_fct_custom_data;
+
+  /** The dictionary offset -> pagecache_entry */
+  struct sos_hash_table  * lookup_table;
+
+  /* Lists to look into in order to free a node */
+  struct sos_fs_pagecache_entry * rw_sync_list;  /**< Pages in sync
+						      with disk wrt
+						      read/write API
+						      (LRU at end) */
+  struct sos_fs_pagecache_entry * rw_dirty_list; /**< Dirty pages wrt
+						      read/write API
+						      (LRU last) */
+
+  /** The "timestamp" high watermark used to iterate over the dirty
+      pages in the sync function */
+  sos_lcount_t top_rw_dirty_order;
+};
+
+
+/** The slab cache for pagecache */
+static struct sos_kslab_cache * cache_of_pagecache;
+
+
+/** The slab cache for pagecache entries */
+static struct sos_kslab_cache * cache_of_pagecache_entry;
+
+
+sos_ret_t sos_fs_pagecache_subsystem_setup()
+{
+  /* Allocate the slab caches for the page caches and page cache
+     entries */
+
+  cache_of_pagecache =
+    sos_kmem_cache_create("pagecache", sizeof(struct sos_fs_pagecache),
+			  2, 0, SOS_KSLAB_CREATE_MAP | SOS_KSLAB_CREATE_ZERO);
+  if (NULL == cache_of_pagecache)
+    return -SOS_ENOMEM;
+
+  cache_of_pagecache_entry =
+    sos_kmem_cache_create("pagecache_entry",
+			  sizeof(struct sos_fs_pagecache_entry),
+			  2, 0, SOS_KSLAB_CREATE_MAP | SOS_KSLAB_CREATE_ZERO);
+  if (NULL == cache_of_pagecache_entry)
+    {
+      sos_kmem_cache_destroy(cache_of_pagecache);
+      return -SOS_ENOMEM;
+    }
+
+  return SOS_OK;
+}
+
+
+struct sos_fs_pagecache *
+sos_fs_pagecache_new_cache(sos_fs_pagecache_sync_function_t sync_fct,
+			   void * sync_fct_custom_data)
+{
+  struct sos_fs_pagecache * pagecache
+    = (struct sos_fs_pagecache*) sos_kmem_cache_alloc(cache_of_pagecache,
+						      0);
+  if (NULL == pagecache)
+    return NULL;
+
+  pagecache->lookup_table = sos_hash_create("pagecache",
+					    struct sos_fs_pagecache_entry,
+					    sos_hash_ui64,
+					    sos_hash_key_eq_ui64,
+					    127, file_offset, hlink);
+  if (NULL == pagecache->lookup_table)
+    {
+      sos_kmem_cache_free((sos_vaddr_t) pagecache);
+      return NULL;
+    }
+
+  pagecache->sync_fct             = sync_fct;
+  pagecache->sync_fct_custom_data = sync_fct_custom_data;
+  pagecache->top_rw_dirty_order   = 0x24;
+
+  return pagecache;
+}
+
+
+sos_ret_t
+sos_fs_pagecache_delete_cache(struct sos_fs_pagecache * pc)
+{
+  /* The cache is EXPECTED to be empty ! */
+
+  if (!list_is_empty(pc->rw_dirty_list))
+    SOS_FATAL_ERROR("Non empty dirty list");
+  if (!list_is_empty(pc->rw_sync_list))
+    SOS_FATAL_ERROR("Non empty sync list");
+
+  sos_hash_dispose(pc->lookup_table);
+  return sos_kmem_cache_free((sos_vaddr_t)pc);
+}
+
+
+/** Helper function to flush a page to disk. Expects the entry to be
+    locked */
+static sos_ret_t pagecache_sync_page(struct sos_fs_pagecache * pc,
+				     struct sos_fs_pagecache_entry * entry)
+{
+  sos_ret_t retval;
+
+  if (! ENTRY_IS_RW_DIRTY(entry))
+    return SOS_OK;
+
+  /* Now do the real transfer to backing store */
+  retval = pc->sync_fct(entry->file_offset, entry->kernel_vaddr,
+			pc->sync_fct_custom_data);
+  if (SOS_OK != retval)
+    return retval;
+
+  /* Transfer page to the sync list */
+  list_delete(pc->rw_dirty_list, entry);
+  entry->rw_dirty_order = 0;
+  list_add_head(pc->rw_sync_list, entry);
+
+  return SOS_OK;
+}
+
+
+/** Helper function to correctly lock an entry */
+static sos_ret_t pagecache_use(struct sos_fs_pagecache * pc,
+			       struct sos_fs_pagecache_entry * entry)
+{
+  entry->ref_cnt ++;
+  return sos_kmutex_lock(& entry->lock, NULL);
+}
+
+
+/**
+ * Helper function to transfer a page to the dirty r/w list
+ */
+static sos_ret_t pagecache_set_rw_dirty(struct sos_fs_pagecache * pc,
+					struct sos_fs_pagecache_entry * entry)
+{
+  if (ENTRY_IS_RW_DIRTY(entry))
+    return SOS_OK; /* Nothing to do */
+
+  list_delete(pc->rw_sync_list, entry);
+  entry->rw_dirty_order = ++ pc->top_rw_dirty_order;
+  list_add_head(pc->rw_dirty_list, entry);
+
+  return SOS_OK;
+}
+
+
+/** Helper function to correctly unlock an entry, flushing it to disk
+    if needed */
+static sos_ret_t pagecache_release(struct sos_fs_pagecache * pc,
+				   struct sos_fs_pagecache_entry * entry)
+{
+  if (entry->ref_cnt > 1)
+    {
+      entry->ref_cnt --;
+      sos_kmutex_unlock(& entry->lock);
+      return SOS_OK;
+    }
+
+  /*
+   * The cached page is now referenced ONLY by US, we can try to
+   * remove it from the cache
+   */
+
+  /* Flush any change to disk, at least if we are sure that its
+     content is legal, ie that the page_in callback did success in
+     filling it */
+  if (! entry->initial_fill_aborted)
+    pagecache_sync_page(pc, entry);
+
+  /* Ok, now WE are not interested by this entry anymore */
+  entry->ref_cnt --;
+
+  /* During blocking time, another thread could have asked for the
+     entry. In this case, stop here */
+  if (entry->ref_cnt > 0)
+    {
+      sos_kmutex_unlock(& entry->lock);
+      return SOS_OK;
+    }
+
+  /* Remove it from the lists */
+  sos_hash_remove(pc->lookup_table, entry);
+  if (ENTRY_IS_RW_DIRTY(entry))
+    list_delete(pc->rw_dirty_list, entry);
+  else
+    list_delete(pc->rw_sync_list, entry);
+
+  /* We can safely erase it now ! */
+  sos_kmutex_unlock(& entry->lock);
+  SOS_ASSERT_FATAL(SOS_OK == sos_kmutex_dispose(& entry->lock)); /* No thread are waiting */
+  sos_kfree(entry->kernel_vaddr);
+  sos_kmem_cache_free((sos_vaddr_t)entry);
+
+  return SOS_OK;
+}
+
+
+/**
+ * Helper function to look up an entry from the cache and lock it. If
+ * the entry does not exist (yet), return NULL.
+ */
+static struct sos_fs_pagecache_entry *
+pagecache_lookup_and_lock(struct sos_fs_pagecache * pc,
+			  sos_luoffset_t offset)
+{
+  sos_luoffset_t pgoffs = SOS_OFFSET64_PAGE_ALIGN_INF(offset);
+  struct sos_fs_pagecache_entry * entry = NULL;
+
+  while (TRUE)
+    {
+      entry
+	= (struct sos_fs_pagecache_entry*) sos_hash_lookup(pc->lookup_table,
+							   & pgoffs);
+      if (! entry)
+	break;
+
+      /* Lock it now */
+      SOS_ASSERT_FATAL(SOS_OK == pagecache_use(pc, entry));
+
+      /*
+       * Entry is now locked
+       */
+      
+      /* Make sure it contains legal contents: if we were blocked
+	 because of the page_in operations reading it from disk, an
+	 error could have been occured. In this case, we must consider
+	 that this entry is not yet inserted in the cache */
+      if (entry->initial_fill_aborted)
+	{
+	  pagecache_release(pc, entry);
+	  continue;
+	}
+
+      /* Ok, we have the entry and it is correctly initialized ! */
+      break;
+    }
+  
+  return entry;
+}
+
+
+sos_ret_t
+sos_fs_pagecache_read(struct sos_fs_pagecache * pc,
+		      sos_luoffset_t offset,
+		      sos_genaddr_t dest_buf,
+		      sos_size_t * /* in/out */len)
+{
+  sos_ret_t retval;
+  sos_luoffset_t pgoffs = SOS_OFFSET64_PAGE_ALIGN_INF(offset);
+  sos_luoffset_t endpos = offset + *len;
+  struct sos_fs_pagecache_entry * entry;
+
+  entry = pagecache_lookup_and_lock(pc, pgoffs);
+  if (NULL == entry)
+    return -SOS_ENOENT;
+
+  /* Great ! Found the entry in the cache ! */
+
+  /* Read only up to the end of the page */
+  if (endpos - pgoffs > SOS_PAGE_SIZE)
+    endpos = pgoffs + SOS_PAGE_SIZE;
+
+  /* Copy page contents to destination buffer */
+  retval = sos_memcpy_generic_to(dest_buf,
+				 entry->kernel_vaddr + (offset - pgoffs),
+				 endpos - offset);
+  pagecache_release(pc, entry);
+
+  if (retval < 0)
+    {
+      *len = 0;
+      return retval;
+    }
+
+  *len = retval;
+  if ((sos_luoffset_t)retval != endpos - offset)
+    return -SOS_EFAULT;
+
+  return SOS_OK;
+}
+
+
+sos_ret_t
+sos_fs_pagecache_write(struct sos_fs_pagecache * pc,
+		       sos_luoffset_t offset,
+		       sos_genaddr_t src_buf,
+		       sos_size_t * /* in/out */len,
+		       sos_bool_t synchronous_write)
+{
+  sos_ret_t retval;
+  sos_luoffset_t pgoffs = SOS_OFFSET64_PAGE_ALIGN_INF(offset);
+  sos_luoffset_t endpos = offset + *len;
+  struct sos_fs_pagecache_entry * entry;
+
+  entry = pagecache_lookup_and_lock(pc, pgoffs);
+  if (NULL == entry)
+    return -SOS_ENOENT;
+
+  /* Great ! Found the entry in the cache ! */
+
+  /* Read only up to the end of the page */
+  if (endpos - pgoffs > SOS_PAGE_SIZE)
+    endpos = pgoffs + SOS_PAGE_SIZE;
+
+  /* Copy page contents to destination buffer */
+  retval = sos_memcpy_generic_from(entry->kernel_vaddr + (offset - pgoffs),
+				   src_buf,
+				   endpos - offset);
+  /* Transfer the entry in the dirty list if needed */
+  if (retval >= 0)
+    pagecache_set_rw_dirty(pc, entry);
+
+  if (retval < 0)
+    {
+      *len = 0;
+      pagecache_release(pc, entry);
+      return retval;
+    }
+
+  *len = retval;
+  if ((sos_luoffset_t)retval != endpos - offset)
+    retval = -SOS_EFAULT;
+  else
+    retval = SOS_OK;
+
+  /* Flush to disk if needed */
+  if (synchronous_write)
+    {
+      sos_ret_t ret = pagecache_sync_page(pc, entry);
+      if (SOS_OK == retval)
+	retval = ret;
+    }
+
+  pagecache_release(pc, entry);
+  return retval;
+}
+
+
+sos_ret_t sos_fs_pagecache_set_dirty(struct sos_fs_pagecache * pc,
+				     sos_luoffset_t offset,
+				     sos_bool_t sync_backing_store)
+{
+  sos_luoffset_t pgoffs = SOS_OFFSET64_PAGE_ALIGN_INF(offset);
+  struct sos_fs_pagecache_entry * entry;
+
+  entry = pagecache_lookup_and_lock(pc, pgoffs);
+  if (NULL == entry)
+    return -SOS_ENOENT;
+
+  /* Great ! Found the entry in the cache ! */
+  pagecache_set_rw_dirty(pc, entry);
+
+  /* Synchronize to backing store if needed */
+  if (sync_backing_store)
+    pagecache_sync_page(pc, entry);
+
+  pagecache_release(pc, entry);
+  return SOS_OK;
+}
+
+
+struct sos_fs_pagecache_entry *
+sos_fs_pagecache_ref_page(struct sos_fs_pagecache * pc,
+			  sos_luoffset_t offset,
+			  sos_vaddr_t * /* out */ kernel_vaddr,
+			  sos_bool_t * /* out */ newly_allocated)
+{
+  sos_luoffset_t pgoffs = SOS_OFFSET64_PAGE_ALIGN_INF(offset);
+  struct sos_fs_pagecache_entry * entry;
+
+  /* The offset is expected to be page-aligned */
+  if (pgoffs != offset)
+    return NULL;
+
+  entry = pagecache_lookup_and_lock(pc, pgoffs);
+  if (NULL != entry)
+    {
+      /* Found it ! No need to go further */
+      *newly_allocated = FALSE;
+      *kernel_vaddr = entry->kernel_vaddr;
+      return entry;
+    }
+
+
+  /*
+   * Need to allocate a new kernel page
+   */
+
+  entry = (struct sos_fs_pagecache_entry*)
+    sos_kmem_cache_alloc(cache_of_pagecache_entry, 0);
+  if (NULL == entry)
+    return (sos_vaddr_t)NULL;
+
+  if (SOS_OK != sos_kmutex_init(& entry->lock, "pagecache_entry",
+				SOS_KWQ_ORDER_FIFO))
+    {
+      sos_kmem_cache_free((sos_vaddr_t)entry);
+      return NULL;
+    }
+
+  /* Initial state of the page correspond to an erroneous
+     initialization */
+  entry->file_offset          = pgoffs;
+  entry->initial_fill_aborted = TRUE;
+  entry->ref_cnt              = 1;
+
+  /* Allocate the page */
+  entry->kernel_vaddr = sos_kmalloc(SOS_PAGE_SIZE, 0);
+  if (((sos_vaddr_t)NULL) == entry->kernel_vaddr)
+    {
+      sos_kmutex_dispose(& entry->lock);
+      sos_kmem_cache_free((sos_vaddr_t)entry);
+      return NULL;
+    }
+
+  /* Own the mutex */
+  SOS_ASSERT_FATAL(SOS_OK == sos_kmutex_lock(& entry->lock, NULL));
+  
+  /* Try to insert it into the hash table. Might fail if the page was
+     already inserted, which could be possible because the allocation
+     routines might have blocked */
+  if (SOS_OK != sos_hash_insert(pc->lookup_table, entry))
+    {
+      /* entry was inserted during allocations, undo the new entry */
+      sos_kmutex_unlock(& entry->lock);
+      sos_kmutex_dispose(& entry->lock);
+      sos_kfree(entry->kernel_vaddr);
+      sos_kmem_cache_free((sos_vaddr_t)entry);
+
+      /* Get the real entry */
+      entry = pagecache_lookup_and_lock(pc, offset);
+      SOS_ASSERT_FATAL(NULL != entry);
+      *kernel_vaddr = entry->kernel_vaddr;
+      *newly_allocated = FALSE;
+      return entry;
+    }
+
+  /* Now register the entry in the sync list */
+  entry->rw_dirty_order = 0;
+  list_add_head(pc->rw_sync_list, entry);
+
+  *newly_allocated = TRUE;
+  *kernel_vaddr = entry->kernel_vaddr;
+  return entry;
+}
+
+
+sos_ret_t
+sos_fs_pagecache_unlock_page(struct sos_fs_pagecache * pc,
+			     struct sos_fs_pagecache_entry * entry,
+			     sos_bool_t initial_fill_aborted)
+{
+
+  entry->initial_fill_aborted = initial_fill_aborted;
+
+  if (initial_fill_aborted)
+    return pagecache_release(pc, entry);
+
+  return sos_kmutex_unlock(& entry->lock);
+}
+
+
+sos_ret_t
+sos_fs_pagecache_unref_page(struct sos_fs_pagecache * pc,
+			    sos_luoffset_t offset)
+{
+  sos_luoffset_t pgoffs = SOS_OFFSET64_PAGE_ALIGN_INF(offset);
+  struct sos_fs_pagecache_entry * entry;
+
+  /* The offset is expected to be page-aligned */
+  if (pgoffs != offset)
+    return -SOS_EINVAL;
+
+  entry
+    = (struct sos_fs_pagecache_entry*) sos_hash_lookup(pc->lookup_table,
+						& pgoffs);
+  SOS_ASSERT_FATAL(NULL != entry);
+  SOS_ASSERT_FATAL(SOS_OK == sos_kmutex_lock(& entry->lock, NULL));
+  return pagecache_release(pc, entry);
+}
+
+
+sos_ret_t
+sos_fs_pagecache_sync(struct sos_fs_pagecache * pc)
+{
+  sos_ret_t retval = SOS_OK;
+  int dummy = 0;
+  sos_lcount_t rw_dirty_order = 0;
+
+  /** High watermark telling "you won't take the pages added
+      afterwards into account" */
+  sos_lcount_t top_rw_dirty_order = pc->top_rw_dirty_order;
+
+  if (list_is_empty(pc->rw_dirty_list))
+    return SOS_OK;
+
+  /* This scan will be exhaustive and resilient to addition/removal of
+     devices as long as new devices are added with list_add_tail
+     (because the scan is "forward", ie in order head -> tail) */
+  while (TRUE)
+    {
+      struct sos_fs_pagecache_entry * entry = NULL;
+      int ndirty;
+
+      /* As long as we don't block, we can safely access the
+	 prev/next fields of the page descriptor */
+      list_foreach_backward(pc->rw_dirty_list, entry, ndirty)
+	{
+	  sos_ret_t ret = SOS_OK;
+	  struct sos_fs_pagecache_entry * prev_entry = NULL;
+
+	  /* Reached the initial high watermark ? Don't take the
+	     additional pages into account */
+	  if (entry->rw_dirty_order > top_rw_dirty_order)
+	    break;
+
+	  if (entry->rw_dirty_order <= rw_dirty_order)
+	    continue;
+
+	  rw_dirty_order = entry->rw_dirty_order;
+	  prev_entry     = entry->prev;
+
+	  SOS_ASSERT_FATAL(SOS_OK == pagecache_use(pc, entry));
+	  if (! entry->initial_fill_aborted)
+	    ret = pagecache_sync_page(pc, entry);
+	  if (SOS_OK != ret)
+	    retval = ret;
+	  pagecache_release(pc, entry);
+
+	  /* We must NOT continue the loops because the prev/next page
+	     cache entry might have been removed or added (sync pages,
+	     by definition) ! */
+	  if (prev_entry != entry->prev)
+	    goto lookup_next_ent;
+	}
+
+      /* Reached the end of the list */
+      break;
+
+    lookup_next_ent:
+      /* Loop over */
+      dummy ++;
+    }
+
+  return retval;
+}
diff -ruNwb /tmp/sos-code-article9/sos/fs_pagecache.h ../sos-code-article9.5/sos/fs_pagecache.h
--- /tmp/sos-code-article9/sos/fs_pagecache.h	1970-01-01 01:00:00.000000000 +0100
+++ ../sos-code-article9.5/sos/fs_pagecache.h	2006-01-22 12:48:30.000000000 +0100
@@ -0,0 +1,214 @@
+/* Copyright (C) 2005,2006      David Decotigny
+
+   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., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
+   USA.
+*/
+#ifndef _SOS_FSPAGECACHE_H_
+#define _SOS_FSPAGECACHE_H_
+
+
+/**
+ * @file fs_pagecache.h
+ *
+ * Simple page cache interface. Used to automate the synchronization
+ * between the rad/write operations and mmap. A "FS page cache" is
+ * simply a set of pages mapping a file in memory. A file may not be
+ * entirely mapped into memory: its pages are mapped only if any user
+ * thread invoked an mmap and page-faulted inside the mapped region to
+ * map these pages into memory. Contrary to some other caches in SOS
+ * (eg the block cache), this one is not limited in size. As many
+ * pages as needed will be allocated for it, as permitted by the
+ * available RAM. With a pageout mechanism, this cache will be
+ * shrinked when needed: some of its pages will be transferred back to
+ * disk and unmapped.
+ *
+ * A page cache is used both to cache memory mapped files of an FS,
+ * and memory mapped block devices. Hence:
+ * - there is one SINGLE page cache for each block device (proper to
+ *   each disk, to each partition)
+ * - there is one SINGLE page cache for each file of a file system
+ *
+ * For block devices, the page cache automatically synchronizes the
+ * pages with the block cache as long as these mapped pages are
+ * accessed through the read/write API. However, <b>NO <i>automatic
+ * and accurate</i> synchronization</b> between the in-memory modified
+ * pages (accessed through the MMU) and the block cache is provided
+ * because we have no way to collect the accurate list of pages
+ * modified through MMU write accesses (this would require either to
+ * catch all the MMU write operations [too inefficient !], or to have
+ * a reverse-mapping system in order to look at the dirty bit of all
+ * the mappings).  Hence, to enforce blkcache/pagecache
+ * synchronization, the msync/munmap API must be used manually. Thus,
+ * the page cache is accurately synchronized with the block cache:
+ * - automatically: with the read/write/sync operations
+ * - manually: with the msync and munmap (and of course: exit) operations
+ *
+ * Nevertheless, from the viewpoint of "blkdev.c", the
+ * blockdev_read/write operations are always in sync with the MMU
+ * because the pagecache is accessed prior to the blkcache: any
+ * divergence between the pagecache and the blkcache is hence
+ * <i>hidden</i>. But keep in mind that if you want the disk to
+ * accurately reflect the contents of the mapped pages, you have to
+ * eventually call msync, munmap, or to destroy the address space (ie
+ * exit the process).
+ *
+ * A side effect: if you map /dev/hda and /dev/hda1, both mappings
+ * will be inconsistent and may also be inconsistent with read/write
+ * accesses. This is because the partitions have their own page cache
+ * while they share the block cache with the disk device. A solution
+ * would be to share the page cache between the disk device and all
+ * its partitions. But, due to the fact that partitions are not
+ * necessarily page-aligned in the disk, this would impose some pages
+ * to not correspond to a page-aligned offset inside a partition,
+ * requiring either to have an odd semantic of the mmap syscall (the
+ * allowed device "offset" would depend on the disk partitioning) if
+ * we want to share the mapped pages between the cache and userspace,
+ * or to allocate other pages for the required userspace mappings and
+ * keep them in sync with the page cache pages. Both solutions seem
+ * ugly to me, and not worth implementing since the page cache is
+ * aimed at being generic enough to be used for file mappings: files
+ * don't have sub-files (as do disk devices that have partitions). So
+ * solving the problem is non pertinent for files. And who will ever
+ * need /dev/hda mappings to be consistent with those of /dev/hda1 ?...
+ */
+#include <sos/errno.h>
+#include <sos/uaccess.h>
+
+
+/** Opaque structure holding a page cache */
+struct sos_fs_pagecache;
+
+/** Opaque structure holding a page of the cache */
+struct sos_fs_pagecache_entry;
+
+
+sos_ret_t sos_fs_pagecache_subsystem_setup(void);
+
+
+/**
+ * Function called to flush the dirty pages to backing store
+ */
+typedef sos_ret_t
+(*sos_fs_pagecache_sync_function_t)(sos_luoffset_t offset,
+				    sos_vaddr_t dirty_page,
+				    void * custom_data);
+
+
+/**
+ * Create a new pagecache.
+ *
+ * @param sync_fct, the function used to flush the dirty pages to
+ *   backing store.  may be NULL
+ */
+struct sos_fs_pagecache *
+sos_fs_pagecache_new_cache(sos_fs_pagecache_sync_function_t sync_fct,
+			   void * sync_fct_custom_data);
+
+
+/**
+ * Delete the page cache.
+ *
+ * The page cache is expected to be already flushed to backing store
+ */
+sos_ret_t
+sos_fs_pagecache_delete_cache(struct sos_fs_pagecache * pc);
+
+
+/**
+ * Read from the given offset from the cache, if present.
+ * @return ENOENT when no page for the given offset is mapped, return
+ * EFAULT when the contents could not be completely copied to
+ * destination buffer
+ */
+sos_ret_t
+sos_fs_pagecache_read(struct sos_fs_pagecache * pc,
+		      sos_luoffset_t offset,
+		      sos_genaddr_t dest_buf,
+		      sos_size_t * /* in/out */len);
+
+
+/**
+ * Write at the given offset from the cache, if present
+ * @return ENOENT when no page for the given offset is mapped, return
+ * EFAULT when the contents could not be completely copied from
+ * source buffer
+ */
+sos_ret_t
+sos_fs_pagecache_write(struct sos_fs_pagecache * pc,
+		       sos_luoffset_t offset,
+		       sos_genaddr_t src_buf,
+		       sos_size_t * /* in/out */len,
+		       sos_bool_t synchronous_write);
+
+
+/**
+ * Function reserved to blkdev.c and FS code: used by the msync
+ * callback to mark a pagecache page dirty
+ *
+ * @param sync_backing_store When TRUE, then the page must be flushed
+ * to backing store.
+ */
+sos_ret_t sos_fs_pagecache_set_dirty(struct sos_fs_pagecache * pc,
+				     sos_luoffset_t offset,
+				     sos_bool_t sync_backing_store);
+
+
+/**
+ * Prepare a page to be mapped: get a NEW reference to the page
+ * (kernel address) of the page to be mapped, which is also locked in
+ * order to be used. If the page is not yet present in the cache,
+ * allocate it and prepare it to be filled
+ *
+ * @param offset MUST be page-aligned
+ * @param newly_allocated TRUE when the page was not already mapped by
+ * someone: the contents of the page is then IRRELEVANT
+ *
+ * @return NULL on error
+ *
+ * @note The page is also LOCKED, use unlock to unlock it before
+ *  unreferencing it
+ */
+struct sos_fs_pagecache_entry *
+sos_fs_pagecache_ref_page(struct sos_fs_pagecache * pc,
+			  sos_luoffset_t offset,
+			  sos_vaddr_t * /* out */ kernel_vaddr,
+			  sos_bool_t * /* out */ newly_allocated);
+
+
+/** Called by the blkdev.c and FS page_in callback to unlock the entry
+    after it has been initialized. */
+sos_ret_t
+sos_fs_pagecache_unlock_page(struct sos_fs_pagecache * pc,
+			     struct sos_fs_pagecache_entry * entry,
+			     sos_bool_t initial_fill_aborted);
+
+
+/**
+ * Called when the page is unmapped from a user process space
+ *   @param offset MUST be page-aligned
+ *
+ * @note the page is expected to be present in the cache
+ * @note the entry is expected NOT to be locked !
+ */
+sos_ret_t
+sos_fs_pagecache_unref_page(struct sos_fs_pagecache * pc,
+			    sos_luoffset_t offset);
+
+
+/** Call the sync function on each dirty page */
+sos_ret_t
+sos_fs_pagecache_sync(struct sos_fs_pagecache * pc);
+
+#endif
diff -ruNwb /tmp/sos-code-article9/sos/hash.c ../sos-code-article9.5/sos/hash.c
--- /tmp/sos-code-article9/sos/hash.c	2005-12-28 11:44:57.000000000 +0100
+++ ../sos-code-article9.5/sos/hash.c	2006-01-22 12:48:31.000000000 +0100
@@ -120,7 +120,7 @@
 
 sos_ret_t sos_hash_dispose(struct sos_hash_table *h)
 {
-  int i;
+  unsigned int i;
   for (i = 0 ; i < h->nbuckets ; i++)
     {
       struct sos_hash_linkage * elt;
diff -ruNwb /tmp/sos-code-article9/sos/hash.h ../sos-code-article9.5/sos/hash.h
--- /tmp/sos-code-article9/sos/hash.h	2005-12-28 11:44:57.000000000 +0100
+++ ../sos-code-article9.5/sos/hash.h	2006-01-22 12:48:31.000000000 +0100
@@ -66,9 +66,9 @@
  * @param name_key_field The name of the field in the element type
  *                       that hold the prev/next hash linkage data
  */
-#define sos_hash_create(name,elt_type,hfunc,hcmp,nbuckets,\
+#define sos_hash_create(name,elt_type,hfunc,eqfunc,nbuckets,\
                         name_key_field,name_h_linkage)    \
-  _sos_hash_create_FULL(name, hfunc, hcmp, nbuckets,           \
+  _sos_hash_create_FULL(name, hfunc, eqfunc, nbuckets,           \
                         offsetof(elt_type, name_key_field),    \
                         offsetof(elt_type, name_h_linkage))
 
@@ -98,7 +98,8 @@
  * Insert the element in the hash, associating it with the key that it
  * embeds
  *
- * Makes sure the element is not already in the hash */
+ * Makes sure the element is not already in the hash
+ */
 sos_ret_t sos_hash_insert(struct sos_hash_table *h,
 			  void *elt_with_key);
 
diff -ruNwb /tmp/sos-code-article9/sos/klibc.c ../sos-code-article9.5/sos/klibc.c
--- /tmp/sos-code-article9/sos/klibc.c	2005-12-28 11:44:57.000000000 +0100
+++ ../sos-code-article9.5/sos/klibc.c	2006-01-22 12:48:31.000000000 +0100
@@ -185,7 +185,7 @@
   sos_bool_t prefix_long = FALSE;
   sos_bool_t prefix_long_long = FALSE;
   
-  if (!buff || !format || (len < 0))
+  if (!buff || !format || (len <= 0))
     return -1;
   
 #define PUTCHAR(thechar) \
@@ -322,7 +322,7 @@
 	  
 	case 's':
 	  {
-	    char *string = va_arg(ap,char *);
+	    char const*string = va_arg(ap,char const*);
 	    if (! string)
 	      string = "(null)";
 	    for( ; *string != '\0' ; string++)
@@ -338,16 +338,16 @@
 	  {
 	    unsigned long long int hexa;
 	    unsigned long long int nb;
-	    int i, had_nonzero = 0;
+	    int x, had_nonzero = 0;
 	    
 	    if (prefix_long_long)
 	      hexa = va_arg(ap,unsigned long long int);
 	    else
 	      hexa = va_arg(ap,unsigned int);
 	    
-	    for(i=0 ; i < 16 ; i++)
+	    for(x=0 ; x < 16 ; x++)
 	      {
-		nb = (unsigned long long int)(hexa << (i*4));
+		nb = (unsigned long long int)(hexa << (x*4));
 		nb = (nb >> 60) & 0xf;
 		// Skip the leading zeros
 		if (nb == 0)
diff -ruNwb /tmp/sos-code-article9/sos/kmem_slab.c ../sos-code-article9.5/sos/kmem_slab.c
--- /tmp/sos-code-article9/sos/kmem_slab.c	2005-12-28 11:44:57.000000000 +0100
+++ ../sos-code-article9.5/sos/kmem_slab.c	2006-01-22 12:48:31.000000000 +0100
@@ -33,7 +33,7 @@
 /** The structure of a slab cache */
 struct sos_kslab_cache
 {
-  char *name;
+  char const* name;
 
   /* non mutable characteristics of this slab */
   sos_size_t  original_obj_size; /* asked object size */
@@ -135,7 +135,7 @@
 
   /* Fills in the cache structure */
   memset(the_cache, 0x0, sizeof(struct sos_kslab_cache));
-  the_cache->name              = (char*)name;
+  the_cache->name              = name;
   the_cache->flags             = cache_flags;
   the_cache->original_obj_size = obj_size;
   the_cache->alloc_obj_size    = alloc_obj_size;
@@ -177,7 +177,7 @@
 	       sos_vaddr_t vaddr_slab,
 	       struct sos_kslab *slab)
 {
-  int i;
+  unsigned int i;
 
   /* Setup the slab structure */
   memset(slab, 0x0, sizeof(struct sos_kslab));
@@ -565,6 +565,8 @@
 {
   struct sos_kslab_cache *new_cache;
 
+  SOS_ASSERT_FATAL(obj_size > 0);
+
   /* Allocate the new cache */
   new_cache = (struct sos_kslab_cache*)
     sos_kmem_cache_alloc(cache_of_struct_kslab_cache,
diff -ruNwb /tmp/sos-code-article9/sos/kmem_vmm.c ../sos-code-article9.5/sos/kmem_vmm.c
--- /tmp/sos-code-article9/sos/kmem_vmm.c	2005-12-28 11:44:57.000000000 +0100
+++ ../sos-code-article9.5/sos/kmem_vmm.c	2006-01-22 12:48:31.000000000 +0100
@@ -377,7 +377,7 @@
   /* If mapping of physical pages is needed, map them now */
   if (flags & SOS_KMEM_VMM_MAP)
     {
-      int i;
+      unsigned int i;
       for (i = 0 ; i < nb_pages ; i ++)
 	{
 	  /* Get a new physical page */
@@ -430,7 +430,6 @@
 
 sos_ret_t sos_kmem_vmm_del_range(struct sos_kmem_range *range)
 {
-  int i;
   struct sos_kmem_range *ranges_to_free;
   list_init(ranges_to_free);
 
@@ -458,6 +457,8 @@
    */
   do
     {
+      unsigned int i;
+
       /* Ok, we got the range. Now, insert this range in the free list */
       kmem_free_range_list = insert_range(kmem_free_range_list, range);
 
diff -ruNwb /tmp/sos-code-article9/sos/ksynch.c ../sos-code-article9.5/sos/ksynch.c
--- /tmp/sos-code-article9/sos/ksynch.c	2005-12-28 11:44:57.000000000 +0100
+++ ../sos-code-article9.5/sos/ksynch.c	2006-01-22 12:48:31.000000000 +0100
@@ -18,6 +18,7 @@
 
 
 #include <hwcore/irq.h>
+#include <sos/assert.h>
 
 
 #include "ksynch.h"
@@ -169,6 +170,19 @@
 }
 
 
+sos_bool_t sos_kmutex_owned_by_me(struct sos_kmutex const* mutex)
+{
+  sos_ui32_t flags;
+  sos_bool_t retval;
+
+  sos_disable_IRQs(flags);
+  retval = (sos_thread_get_current() == mutex->owner);
+  sos_restore_IRQs(flags);
+
+  return retval;
+}
+
+
 sos_ret_t sos_kmutex_trylock(struct sos_kmutex *mutex)
 {
   sos_ui32_t flags;
@@ -223,10 +237,17 @@
        *   mutex->owner = thread_that_is_woken_up;
        * But the real Id of the next thread owning the mutex is not
        * that important. What is important here is that mutex->owner
-       * IS NOT NULL. Otherwise there will be a possibility for the
+       * IS NOT NULL and does not correspond to any existing thread
+       * (address 0x43 is a good candidate because, in SOS, addresses
+       * below 4kB are never mapped in order to catch invalid
+       * pointers). Otherwise there will be a possibility for the
        * thread woken up here to have the mutex stolen by a thread
        * locking the mutex in the meantime.
        */
+#define MUTEX_STILL_LOCKED ((struct sos_thread*) 0x43)
+      mutex->owner = MUTEX_STILL_LOCKED;
+
+      /* We wake up ONE thread ONLY */
       retval = sos_kwaitq_wakeup(& mutex->kwaitq, 1, SOS_OK);
     } 
  
diff -ruNwb /tmp/sos-code-article9/sos/ksynch.h ../sos-code-article9.5/sos/ksynch.h
--- /tmp/sos-code-article9/sos/ksynch.h	2005-12-28 11:44:57.000000000 +0100
+++ ../sos-code-article9.5/sos/ksynch.h	2006-01-22 12:48:31.000000000 +0100
@@ -161,6 +161,12 @@
 
 
 /**
+ * Predicate returns TRUE when the current thread owns the mutex
+ */
+sos_bool_t sos_kmutex_owned_by_me(struct sos_kmutex const* mutex);
+
+
+/**
  * Unlock the mutex, eventually waking up a thread
  *
  * @return -SOS_EPERM when the calling thread is NOT the owner of the
diff -ruNwb /tmp/sos-code-article9/sos/main.c ../sos-code-article9.5/sos/main.c
--- /tmp/sos-code-article9/sos/main.c	2005-12-28 11:44:57.000000000 +0100
+++ ../sos-code-article9.5/sos/main.c	2006-01-22 12:48:31.000000000 +0100
@@ -51,6 +51,9 @@
 #include <drivers/tty.h>
 #include <drivers/console.h>
 #include <drivers/serial.h>
+#include <sos/blkdev.h>
+#include <sos/fs_pagecache.h>
+#include <drivers/ide.h>
 
 /* Helper function to display each bits of a 32bits integer on the
    screen as dark or light carrets */
@@ -136,7 +139,8 @@
       if (need_to_setup_mmu)
 	sos_thread_end_user_space_access();
 
-      sos_bochs_printf("Unresolved USER page Fault at instruction 0x%x on access to address 0x%x (info=%x)!\n",
+      sos_bochs_printf("\e[35mTHR %p: Unresolved USER page Fault at instruction 0x%x on access to address 0x%x (info=%x)!\e[m\n",
+		       (void*)sos_thread_get_current(),
 		       sos_cpu_context_get_PC(ctxt),
 		       (unsigned)faulting_vaddr,
 		       (unsigned)sos_cpu_context_get_EX_info(ctxt));
@@ -193,7 +197,8 @@
  * An operating system MUST always have a ready thread ! Otherwise:
  * what would the CPU have to execute ?!
  */
-static void idle_thread()
+static void idle_thread(void* unused) __attribute__((noreturn));
+static void idle_thread(void* unused)
 {
   sos_ui32_t idle_twiddle = 0;
 
@@ -218,7 +223,8 @@
  */
 #define LOAD_DISPLAY_BASELINE  4
 #define LOAD_DISPLAY_STARTROW 34
-static void stat_thread()
+static void stat_thread(void * unused) __attribute__((noreturn));
+static void stat_thread(void * unused)
 {
   while (1)
     {
@@ -371,12 +377,14 @@
 }
 
 
+
 /* ======================================================================
  * The C entry point of our operating system
  */
 void sos_main(unsigned long magic, unsigned long arg)
+     __attribute__((noreturn));
+void sos_main(unsigned long magic, unsigned long arg)
 {
-  unsigned i;
   sos_paddr_t sos_kernel_core_base_paddr, sos_kernel_core_top_paddr;
   struct sos_time tick_resolution;
   struct sos_fs_manager_instance * rootfs;
@@ -406,7 +414,7 @@
       sos_x86_videomem_printf(1, 0,
 			      SOS_X86_VIDEO_FG_YELLOW | SOS_X86_VIDEO_BG_BLUE,
 			      "Welcome From GRUB to %s%c RAM is %dMB (upper mem = 0x%x kB)",
-			      "SOS article 9", ',',
+			      "SOS article 9.5", ',',
 			      (unsigned)(upper_mem >> 10) + 1,
 			      (unsigned)upper_mem);
     }
@@ -417,7 +425,7 @@
       sos_x86_videomem_printf(1, 0,
 			      SOS_X86_VIDEO_FG_YELLOW | SOS_X86_VIDEO_BG_BLUE,
 			      "Welcome to %s%c RAM is %dMB (upper mem = 0x%x kB)",
-			      "SOS article 9", ',',
+			      "SOS article 9.5", ',',
 			      (unsigned)(upper_mem >> 10) + 1,
 			      (unsigned)upper_mem);
     }
@@ -425,9 +433,9 @@
     /* Not loaded with grub, not from an enhanced bootsect */
     sos_x86_videomem_printf(1, 0,
 			    SOS_X86_VIDEO_FG_YELLOW | SOS_X86_VIDEO_BG_BLUE,
-			    "Welcome to SOS article 9");
+			    "Welcome to SOS article 9.5");
 
-  sos_bochs_putstring("Message in a bochs: This is SOS article 9.\n");
+  sos_bochs_putstring("Message in a bochs: This is SOS article 9.5.\n");
 
   /* Setup CPU segmentation and IRQ subsystem */
   sos_gdt_subsystem_setup();
@@ -548,6 +556,10 @@
    */
   sos_umem_vmm_subsystem_setup();
   sos_dev_zero_subsystem_setup();
+  
+  /* Initialize the page and block cache subsystems */
+  sos_fs_pagecache_subsystem_setup();
+  sos_blockdev_subsystem_setup();
   sos_dev_mem_chardev_setup();
 
   /*
@@ -573,6 +585,8 @@
   sos_console_subsystem_setup();
 
 
+  sos_ide_subsystem_setup();
+
 
 
   /* Start the 'init' process, which in turns launches the other
diff -ruNwb /tmp/sos-code-article9/sos/process.c ../sos-code-article9.5/sos/process.c
--- /tmp/sos-code-article9/sos/process.c	2005-12-28 11:44:57.000000000 +0100
+++ ../sos-code-article9.5/sos/process.c	2006-01-22 12:48:31.000000000 +0100
@@ -75,7 +75,7 @@
 /**
  * Helper function for debugging purposes
  */
-void sos_process_dumplist()
+void sos_process_dumplist(void)
 {
   struct sos_thread * cur_thr = sos_thread_get_current();
   struct sos_process * proc;
diff -ruNwb /tmp/sos-code-article9/sos/process.h ../sos-code-article9.5/sos/process.h
--- /tmp/sos-code-article9/sos/process.h	2005-12-28 11:44:57.000000000 +0100
+++ ../sos-code-article9.5/sos/process.h	2006-01-22 12:48:31.000000000 +0100
@@ -55,7 +55,7 @@
 /**
  * Initialization of the process subsystem
  */
-sos_ret_t sos_process_subsystem_setup();
+sos_ret_t sos_process_subsystem_setup(void);
 
 
 /**
diff -ruNwb /tmp/sos-code-article9/sos/sched.h ../sos-code-article9.5/sos/sched.h
--- /tmp/sos-code-article9/sos/sched.h	2005-12-28 11:44:57.000000000 +0100
+++ ../sos-code-article9.5/sos/sched.h	2006-01-22 12:48:31.000000000 +0100
@@ -111,7 +111,7 @@
  *
  * @note: The use of this function is RESERVED
  */
-sos_ret_t sos_sched_subsystem_setup();
+sos_ret_t sos_sched_subsystem_setup(void);
 
 
 /**
@@ -153,6 +153,6 @@
  *
  * @note The use of this function is RESERVED (to timer IRQ)
  */
-sos_ret_t sos_sched_do_timer_tick();
+sos_ret_t sos_sched_do_timer_tick(void);
 
 #endif /* _SOS_WAITQUEUE_H_ */
diff -ruNwb /tmp/sos-code-article9/sos/syscall.c ../sos-code-article9.5/sos/syscall.c
--- /tmp/sos-code-article9/sos/syscall.c	2005-12-28 11:44:57.000000000 +0100
+++ ../sos-code-article9.5/sos/syscall.c	2006-01-22 12:48:31.000000000 +0100
@@ -15,6 +15,7 @@
    Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
    USA. 
 */
+#include <sos/assert.h>
 #include <sos/thread.h>
 #include <sos/kmalloc.h>
 #include <sos/klibc.h>
@@ -30,6 +31,13 @@
 #include <sos/uaccess.h>
 #include "syscall.h"
 
+
+/** To get rid of gcc's "dereferencing type-punned pointer will break
+    strict-aliasing rules" warning */
+#define SYSCALL_VAR32_PTR(ui32_variable) \
+  ((void*)&(ui32_variable))
+
+
 /**
  * THE syscall entry point
  */
@@ -200,8 +208,8 @@
 	  = sos_process_get_address_space(sos_thread_get_current()->process);
 
 	retval = sos_syscall_get2args(user_ctxt,
-				      (unsigned int*)& start_uaddr,
-				      (unsigned int*)& size);
+				      SYSCALL_VAR32_PTR(start_uaddr),
+				      SYSCALL_VAR32_PTR(size));
 	if (SOS_OK != retval)
 	  break;
 
@@ -220,9 +228,9 @@
 	  = sos_process_get_address_space(sos_thread_get_current()->process);
 
 	retval = sos_syscall_get3args(user_ctxt,
-				      (unsigned int*)& start_uaddr,
-				      (unsigned int*)& size,
-				      (unsigned int*)& new_access_rights);
+				      SYSCALL_VAR32_PTR(start_uaddr),
+				      SYSCALL_VAR32_PTR(size),
+				      SYSCALL_VAR32_PTR(new_access_rights));
 	if (SOS_OK != retval)
 	  break;
 
@@ -251,11 +259,11 @@
 	  = sos_process_get_address_space(sos_thread_get_current()->process);
 
 	retval = sos_syscall_get5args(user_ctxt,
-				      (unsigned int*)& old_uaddr,
-				      (unsigned int*)& old_size,
-				      (unsigned int*)& uptr_new_uaddr,
-				      (unsigned int*)& new_size,
-				      (unsigned int*)& flags);
+				      SYSCALL_VAR32_PTR(old_uaddr),
+				      SYSCALL_VAR32_PTR(old_size),
+				      SYSCALL_VAR32_PTR(uptr_new_uaddr),
+				      SYSCALL_VAR32_PTR(new_size),
+				      SYSCALL_VAR32_PTR(flags));
 	if (SOS_OK != retval)
 	  break;
 
@@ -289,6 +297,31 @@
       }
       break;
 
+    case SOS_SYSCALL_ID_MSYNC:
+      {
+	sos_uaddr_t start_uaddr;
+	sos_size_t  size;
+	sos_ui32_t  flags;
+	struct sos_umem_vmm_as * my_as;
+
+	my_as
+	  = sos_process_get_address_space(sos_thread_get_current()->process);
+
+	retval = sos_syscall_get3args(user_ctxt,
+				      SYSCALL_VAR32_PTR(start_uaddr),
+				      SYSCALL_VAR32_PTR(size),
+				      SYSCALL_VAR32_PTR(flags));
+	if (SOS_OK != retval)
+	  break;
+
+	retval = sos_thread_prepare_user_space_access(NULL, (sos_vaddr_t)NULL);
+	if (SOS_OK != retval)
+	  break;
+	retval = sos_umem_vmm_sync(my_as, start_uaddr, size, flags);
+	sos_thread_end_user_space_access();
+      }
+      break;
+
     case SOS_SYSCALL_ID_NEW_THREAD:
       {
 	sos_uaddr_t start_func;
@@ -303,10 +336,10 @@
 	  = sos_process_get_address_space(sos_thread_get_current()->process);
 
 	retval = sos_syscall_get4args(user_ctxt,
-				      (unsigned int*)& start_func,
-				      (unsigned int*)& start_arg1,
-				      (unsigned int*)& start_arg2,
-				      (unsigned int*)& stack_size);
+				      SYSCALL_VAR32_PTR(start_func),
+				      SYSCALL_VAR32_PTR(start_arg1),
+				      SYSCALL_VAR32_PTR(start_arg2),
+				      SYSCALL_VAR32_PTR(stack_size));
 	if (SOS_OK != retval)
 	  break;
 
@@ -349,8 +382,8 @@
 	struct sos_time delay;
 
 	retval = sos_syscall_get2args(user_ctxt,
-				      (unsigned int*)& delay.sec,
-				      (unsigned int*)& delay.nanosec);
+				      SYSCALL_VAR32_PTR(delay.sec),
+				      SYSCALL_VAR32_PTR(delay.nanosec));
 	if (SOS_OK != retval)
 	  break;
 
@@ -367,7 +400,7 @@
 	  = sos_process_get_address_space(sos_thread_get_current()->process);
 
 	retval = sos_syscall_get1arg(user_ctxt,
-				     (unsigned int*)& new_top_heap);
+				     SYSCALL_VAR32_PTR(new_top_heap));
 	if (SOS_OK != retval)
 	  break;
 
@@ -401,13 +434,13 @@
 
 	proc = sos_thread_get_current()->process;
 	retval = sos_syscall_get7args(user_ctxt,
-				      (unsigned int*)& user_src,
-				      (unsigned int*)& srclen,
-				      (unsigned int*)& user_dst,
-				      (unsigned int*)& dstlen,
-				      (unsigned int*)& user_fstype,
-				      (unsigned int*)& mountflags,
-				      (unsigned int*)& user_args);
+				      SYSCALL_VAR32_PTR(user_src),
+				      SYSCALL_VAR32_PTR(srclen),
+				      SYSCALL_VAR32_PTR(user_dst),
+				      SYSCALL_VAR32_PTR(dstlen),
+				      SYSCALL_VAR32_PTR(user_fstype),
+				      SYSCALL_VAR32_PTR(mountflags),
+				      SYSCALL_VAR32_PTR(user_args));
 	if (SOS_OK != retval)
 	  break;
 
@@ -472,8 +505,8 @@
 	
 	proc = sos_thread_get_current()->process;
 	retval = sos_syscall_get2args(user_ctxt,
-				      (unsigned int*)& user_str,
-				      (unsigned int*)& len);
+				      SYSCALL_VAR32_PTR(user_str),
+				      SYSCALL_VAR32_PTR(len));
 	if (SOS_OK != retval)
 	  break;
 	
@@ -505,9 +538,9 @@
 
 	proc = sos_thread_get_current()->process;
 	retval = sos_syscall_get3args(user_ctxt,
-				      (unsigned int*)& user_str,
-				      (unsigned int*)& len,
-				      (unsigned int*)& user_vfstat_struct);
+				      SYSCALL_VAR32_PTR(user_str),
+				      SYSCALL_VAR32_PTR(len),
+				      SYSCALL_VAR32_PTR(user_vfstat_struct));
 	if (SOS_OK != retval)
 	  break;
 
@@ -540,10 +573,10 @@
 
 	proc = sos_thread_get_current()->process;
 	retval = sos_syscall_get4args(user_ctxt,
-				      (unsigned int*)& user_str,
-				      (unsigned int*)& len,
-				      (unsigned int*)& open_flags,
-				      (unsigned int*)& access_rights);
+				      SYSCALL_VAR32_PTR(user_str),
+				      SYSCALL_VAR32_PTR(len),
+				      SYSCALL_VAR32_PTR(open_flags),
+				      SYSCALL_VAR32_PTR(access_rights));
 	if (SOS_OK != retval)
 	  break;
 
@@ -577,7 +610,7 @@
 
 	proc = sos_thread_get_current()->process;
 	retval = sos_syscall_get1arg(user_ctxt,
-				     (unsigned int*)& fd);
+				     SYSCALL_VAR32_PTR(fd));
 	if (SOS_OK != retval)
 	  break;
 
@@ -607,9 +640,9 @@
 
 	proc = sos_thread_get_current()->process;
 	retval = sos_syscall_get3args(user_ctxt,
-				      (unsigned int*)& fd,
-				      (unsigned int*)& uaddr_buf,
-				      (unsigned int*)& uaddr_buflen);
+				      SYSCALL_VAR32_PTR(fd),
+				      SYSCALL_VAR32_PTR(uaddr_buf),
+				      SYSCALL_VAR32_PTR(uaddr_buflen));
 	if (SOS_OK != retval)
 	  break;
 
@@ -650,8 +683,8 @@
 
 	proc = sos_thread_get_current()->process;
 	retval = sos_syscall_get2args(user_ctxt,
-				      (unsigned int*)& fd,
-				      (unsigned int*)& uaddr_direntry);
+				      SYSCALL_VAR32_PTR(fd),
+				      SYSCALL_VAR32_PTR(uaddr_direntry));
 	if (SOS_OK != retval)
 	  break;
 
@@ -686,9 +719,9 @@
 
 	proc = sos_thread_get_current()->process;
 	retval = sos_syscall_get3args(user_ctxt,
-				      (unsigned int*)& fd,
-				      (unsigned int*)& uaddr_buf,
-				      (unsigned int*)& uaddr_buflen);
+				      SYSCALL_VAR32_PTR(fd),
+				      SYSCALL_VAR32_PTR(uaddr_buf),
+				      SYSCALL_VAR32_PTR(uaddr_buflen));
 	if (SOS_OK != retval)
 	  break;
 
@@ -730,9 +763,9 @@
 
 	proc = sos_thread_get_current()->process;
 	retval = sos_syscall_get3args(user_ctxt,
-				      (unsigned int*)& fd,
-				      (unsigned int*)& uaddr_offset,
-				      (unsigned int*)& whence);
+				      SYSCALL_VAR32_PTR(fd),
+				      SYSCALL_VAR32_PTR(uaddr_offset),
+				      SYSCALL_VAR32_PTR(whence));
 	if (SOS_OK != retval)
 	  break;
 
@@ -772,8 +805,8 @@
 
 	proc = sos_thread_get_current()->process;
 	retval = sos_syscall_get2args(user_ctxt,
-				      (unsigned int*)& fd,
-				      (unsigned int*)& length);
+				      SYSCALL_VAR32_PTR(fd),
+				      SYSCALL_VAR32_PTR(length));
 	if (SOS_OK != retval)
 	  break;
 
@@ -805,13 +838,13 @@
 	
 	proc = sos_thread_get_current()->process;
 	retval = sos_syscall_get7args(user_ctxt,
-				      (unsigned int*)& ptr_hint_uaddr,
-				      (unsigned int*)& length,
-				      (unsigned int*)& prot,
-				      (unsigned int*)& flags,
-				      (unsigned int*)& fd,
-				      (unsigned int*)& offs64_hi,
-				      (unsigned int*)& offs64_lo);
+				      SYSCALL_VAR32_PTR(ptr_hint_uaddr),
+				      SYSCALL_VAR32_PTR(length),
+				      SYSCALL_VAR32_PTR(prot),
+				      SYSCALL_VAR32_PTR(flags),
+				      SYSCALL_VAR32_PTR(fd),
+				      SYSCALL_VAR32_PTR(offs64_hi),
+				      SYSCALL_VAR32_PTR(offs64_lo));
 	if (SOS_OK != retval)
 	  break;
 
@@ -862,7 +895,7 @@
 
 	proc = sos_thread_get_current()->process;
 	retval = sos_syscall_get1arg(user_ctxt,
-				     (unsigned int*)& fd);
+				     SYSCALL_VAR32_PTR(fd));
 	if (SOS_OK != retval)
 	  break;
 
@@ -887,9 +920,9 @@
 
 	proc = sos_thread_get_current()->process;
 	retval = sos_syscall_get3args(user_ctxt,
-				      (unsigned int*)& fd,
-				      (unsigned int*)& cmd,
-				      (unsigned int*)& arg);
+				      SYSCALL_VAR32_PTR(fd),
+				      SYSCALL_VAR32_PTR(cmd),
+				      SYSCALL_VAR32_PTR(arg));
 	if (SOS_OK != retval)
 	  break;
 
@@ -914,9 +947,9 @@
 
 	proc = sos_thread_get_current()->process;
 	retval = sos_syscall_get3args(user_ctxt,
-				      (unsigned int*)& fd,
-				      (unsigned int*)& cmd,
-				      (unsigned int*)& arg);
+				      SYSCALL_VAR32_PTR(fd),
+				      SYSCALL_VAR32_PTR(cmd),
+				      SYSCALL_VAR32_PTR(arg));
 	if (SOS_OK != retval)
 	  break;
 
@@ -942,9 +975,9 @@
 
 	proc = sos_thread_get_current()->process;
 	retval = sos_syscall_get3args(user_ctxt,
-				      (unsigned int*)& user_str,
-				      (unsigned int*)& len,
-				      (unsigned int*)& access_rights);
+				      SYSCALL_VAR32_PTR(user_str),
+				      SYSCALL_VAR32_PTR(len),
+				      SYSCALL_VAR32_PTR(access_rights));
 	if (SOS_OK != retval)
 	  break;
 
@@ -969,10 +1002,10 @@
 
 	proc = sos_thread_get_current()->process;
 	retval = sos_syscall_get4args(user_ctxt,
-				      (unsigned int*)& user_oldpath,
-				      (unsigned int*)& oldpathlen,
-				      (unsigned int*)& user_newpath,
-				      (unsigned int*)& newpathlen);
+				      SYSCALL_VAR32_PTR(user_oldpath),
+				      SYSCALL_VAR32_PTR(oldpathlen),
+				      SYSCALL_VAR32_PTR(user_newpath),
+				      SYSCALL_VAR32_PTR(newpathlen));
 	if (SOS_OK != retval)
 	  break;
 
@@ -1013,8 +1046,8 @@
 
 	proc = sos_thread_get_current()->process;
 	retval = sos_syscall_get2args(user_ctxt,
-				      (unsigned int*)& user_str,
-				      (unsigned int*)& len);
+				      SYSCALL_VAR32_PTR(user_str),
+				      SYSCALL_VAR32_PTR(len));
 	if (SOS_OK != retval)
 	  break;
 
@@ -1037,10 +1070,10 @@
 
 	proc = sos_thread_get_current()->process;
 	retval = sos_syscall_get4args(user_ctxt,
-				      (unsigned int*)& user_path,
-				      (unsigned int*)& pathlen,
-				      (unsigned int*)& user_targetpath,
-				      (unsigned int*)& targetpathlen);
+				      SYSCALL_VAR32_PTR(user_path),
+				      SYSCALL_VAR32_PTR(pathlen),
+				      SYSCALL_VAR32_PTR(user_targetpath),
+				      SYSCALL_VAR32_PTR(targetpathlen));
 	if (SOS_OK != retval)
 	  break;
 
@@ -1069,12 +1102,12 @@
 
 	proc = sos_thread_get_current()->process;
 	retval = sos_syscall_get6args(user_ctxt,
-				      (unsigned int*)& user_str,
-				      (unsigned int*)& len,
-				      (unsigned int*)& type,
-				      (unsigned int*)& access_rights,
-				      (unsigned int*)& dev_id.device_class,
-				      (unsigned int*)& dev_id.device_instance);
+				      SYSCALL_VAR32_PTR(user_str),
+				      SYSCALL_VAR32_PTR(len),
+				      SYSCALL_VAR32_PTR(type),
+				      SYSCALL_VAR32_PTR(access_rights),
+				      SYSCALL_VAR32_PTR(dev_id.device_class),
+				      SYSCALL_VAR32_PTR(dev_id.device_instance));
 	if (SOS_OK != retval)
 	  break;
 
@@ -1097,6 +1130,7 @@
 	    break;
 
 	  case SOS_FS_NODE_DEVICE_CHAR:
+	  case SOS_FS_NODE_DEVICE_BLOCK:
 	    retval = sos_fs_mknod(proc,
 				  path, len, type, access_rights, &dev_id);
 	    break;
@@ -1120,9 +1154,9 @@
 
 	proc = sos_thread_get_current()->process;
 	retval = sos_syscall_get3args(user_ctxt,
-				      (unsigned int*)& user_str,
-				      (unsigned int*)& len,
-				      (unsigned int*)& access_rights);
+				      SYSCALL_VAR32_PTR(user_str),
+				      SYSCALL_VAR32_PTR(len),
+				      SYSCALL_VAR32_PTR(access_rights));
 	if (SOS_OK != retval)
 	  break;
 
@@ -1145,8 +1179,8 @@
 
 	proc = sos_thread_get_current()->process;
 	retval = sos_syscall_get2args(user_ctxt,
-				      (unsigned int*)& user_str,
-				      (unsigned int*)& len);
+				      SYSCALL_VAR32_PTR(user_str),
+				      SYSCALL_VAR32_PTR(len));
 	if (SOS_OK != retval)
 	  break;
 
@@ -1169,9 +1203,9 @@
 
 	proc = sos_thread_get_current()->process;
 	retval = sos_syscall_get3args(user_ctxt,
-				      (unsigned int*)& user_str,
-				      (unsigned int*)& len,
-				      (unsigned int*)& access_rights);
+				      SYSCALL_VAR32_PTR(user_str),
+				      SYSCALL_VAR32_PTR(len),
+				      SYSCALL_VAR32_PTR(access_rights));
 	if (SOS_OK != retval)
 	  break;
 
@@ -1196,10 +1230,10 @@
 
 	proc = sos_thread_get_current()->process;
 	retval = sos_syscall_get4args(user_ctxt,
-				      (unsigned int*)& user_str,
-				      (unsigned int*)& len,
-				      (unsigned int*)& nofollow,
-				      (unsigned int*)& user_stat_struct);
+				      SYSCALL_VAR32_PTR(user_str),
+				      SYSCALL_VAR32_PTR(len),
+				      SYSCALL_VAR32_PTR(nofollow),
+				      SYSCALL_VAR32_PTR(user_stat_struct));
 	if (SOS_OK != retval)
 	  break;
 
@@ -1231,8 +1265,8 @@
 
 	proc = sos_thread_get_current()->process;
 	retval = sos_syscall_get2args(user_ctxt,
-				      (unsigned int*)& user_str,
-				      (unsigned int*)& len);
+				      SYSCALL_VAR32_PTR(user_str),
+				      SYSCALL_VAR32_PTR(len));
 	if (SOS_OK != retval)
 	  break;
 
@@ -1272,7 +1306,7 @@
 
 	proc = sos_thread_get_current()->process;
 	retval = sos_syscall_get1arg(user_ctxt,
-				     (unsigned int*)& fd);
+				     SYSCALL_VAR32_PTR(fd));
 	if (SOS_OK != retval)
 	  break;
 
@@ -1344,22 +1378,13 @@
 	str = (unsigned char*)sos_kmalloc(len, 0);
 	if (str)
 	  {
-	    int i;
-	    sos_bochs_printf("Hexdump(0x%x, %d):\n", user_str, len);
+	    sos_bochs_printf("THR %p, Hexdump(0x%x, %d):\n",
+			     sos_thread_get_current(), user_str, len);
 	    retval = sos_memcpy_from_user((sos_vaddr_t) str, user_str, len);
 	    sos_bochs_printf("  (Successfully copied %d out of %d)\n",
 			     retval, len);
-
-	    for (i = 0 ; i < retval ; i++)
-	      {
-		if ((i % 32) == 0)
-		  sos_bochs_printf("%x:", i);
-		sos_bochs_printf(" %x", str[i]);
-		if (((i+1) % 32) == 0)
-		  sos_bochs_printf("\n");
-	      }
-	    if (i % 32)
-	      sos_bochs_printf("\n");
+	    if (retval > 0)
+	      sos_bochs_hexdump(str, retval);
 
 	    sos_kfree((sos_vaddr_t)str);
 	  }
diff -ruNwb /tmp/sos-code-article9/sos/syscall.h ../sos-code-article9.5/sos/syscall.h
--- /tmp/sos-code-article9/sos/syscall.h	2005-12-28 11:44:57.000000000 +0100
+++ ../sos-code-article9.5/sos/syscall.h	2006-01-22 12:48:31.000000000 +0100
@@ -47,12 +47,12 @@
 #define SOS_SYSCALL_ID_MUNMAP      260 /**< Args: start_uaddr size, retval=errno */
 #define SOS_SYSCALL_ID_MPROTECT    261 /**< Args: start_uaddr size access_rights, retval=errno */
 #define SOS_SYSCALL_ID_MRESIZE     262 /**< Args: old_uaddr old_size ptr_new_uaddr new_size flags, retval=errno */
-
+#define SOS_SYSCALL_ID_MSYNC       263 /**< Args: start_uaddr size flags, retval=errno */
 
 /**
  * Heap management
  */
-#define SOS_SYSCALL_ID_BRK         263 /**< Args: 0/new_top_heap, retval=top_heap */
+#define SOS_SYSCALL_ID_BRK         303 /**< Args: 0/new_top_heap, retval=top_heap */
 
 /**
  * File system interface
diff -ruNwb /tmp/sos-code-article9/sos/thread.h ../sos-code-article9.5/sos/thread.h
--- /tmp/sos-code-article9/sos/thread.h	2005-12-28 11:44:57.000000000 +0100
+++ ../sos-code-article9.5/sos/thread.h	2006-01-22 12:48:31.000000000 +0100
@@ -254,14 +254,14 @@
  * Terminate the execution of the current thread. For kernel threads,
  * it is called by default when the start routine returns.
  */
-void sos_thread_exit() __attribute__((noreturn));
+void sos_thread_exit(void) __attribute__((noreturn));
 
 
 /**
  * Get the identifier of the thread currently running on CPU. Trivial
  * function.
  */
-struct sos_thread *sos_thread_get_current();
+struct sos_thread *sos_thread_get_current(void);
 
 
 /**
@@ -296,7 +296,7 @@
  *
  * @note This is a BLOCKING FUNCTION
  */
-sos_ret_t sos_thread_yield();
+sos_ret_t sos_thread_yield(void);
 
 
 /**
diff -ruNwb /tmp/sos-code-article9/sos/time.h ../sos-code-article9.5/sos/time.h
--- /tmp/sos-code-article9/sos/time.h	2005-12-28 11:44:57.000000000 +0100
+++ ../sos-code-article9.5/sos/time.h	2006-01-22 12:48:31.000000000 +0100
@@ -216,7 +216,7 @@
  *
  * @note The use of this function is RESERVED (to timer IRQ)
  */
-sos_ret_t sos_time_do_tick();
+sos_ret_t sos_time_do_tick(void);
 
 
 #endif /* _SOS_TIME_H_ */
diff -ruNwb /tmp/sos-code-article9/sos/types.h ../sos-code-article9.5/sos/types.h
--- /tmp/sos-code-article9/sos/types.h	2005-12-28 11:44:57.000000000 +0100
+++ ../sos-code-article9.5/sos/types.h	2006-01-22 12:48:31.000000000 +0100
@@ -33,9 +33,6 @@
 /** User virtual address */
 typedef unsigned int           sos_uaddr_t;
 
-/** Generic (Kernel or User) virtual address */
-typedef unsigned int           sos_genaddr_t;
-
 /** Memory size of an object (positive) */
 typedef unsigned int           sos_size_t;
 /** Generic positive offset into objects */
diff -ruNwb /tmp/sos-code-article9/sos/uaccess.c ../sos-code-article9.5/sos/uaccess.c
--- /tmp/sos-code-article9/sos/uaccess.c	2005-12-28 11:44:57.000000000 +0100
+++ ../sos-code-article9.5/sos/uaccess.c	2006-01-22 12:48:31.000000000 +0100
@@ -127,7 +127,7 @@
     return -SOS_ENOMEM;
 
   retval = sos_memcpy_from_user(*kernel_to, from_user, length);
-  if (length != retval)
+  if ((sos_ret_t)length != retval)
     {
       sos_kfree((sos_vaddr_t)*kernel_to);
       *kernel_to = (sos_vaddr_t) NULL;
@@ -300,3 +300,25 @@
 
   return retval;
 }
+
+
+sos_ret_t sos_memcpy_generic_to(sos_genaddr_t to_addr,
+				sos_vaddr_t kernel_from,
+				sos_size_t size)
+{
+  if (to_addr.is_user)
+    return sos_memcpy_to_user(to_addr.addr, kernel_from, size);
+  memcpy((void*)to_addr.addr, (const void*)kernel_from, size);
+  return size;
+}
+
+
+sos_ret_t sos_memcpy_generic_from(sos_vaddr_t kernel_from,
+				  sos_genaddr_t from_addr,
+				  sos_size_t size)
+{
+  if (from_addr.is_user)
+    return sos_memcpy_from_user(kernel_from, from_addr.addr, size);
+  memcpy((void*)kernel_from, (const void*)from_addr.addr, size);
+  return size;
+}
diff -ruNwb /tmp/sos-code-article9/sos/uaccess.h ../sos-code-article9.5/sos/uaccess.h
--- /tmp/sos-code-article9/sos/uaccess.h	2005-12-28 11:44:57.000000000 +0100
+++ ../sos-code-article9.5/sos/uaccess.h	2006-01-22 12:48:31.000000000 +0100
@@ -106,4 +106,24 @@
 				sos_size_t max_len,
 				sos_ui32_t kmalloc_flags);
 
+
+/*
+ * Special functions to access both kernel/user space
+ */
+/** Generic (Kernel or User) virtual address. We define a structure to
+    force the compiler to signal when we call this special function
+    with a usual address... */
+typedef struct sos_genaddr { sos_bool_t is_user;
+                             sos_ui32_t addr; } sos_genaddr_t;
+#define SOS_GENADDR_DECL(name,_is_user,_addr) \
+  sos_genaddr_t name = (struct sos_genaddr) { .is_user=_is_user, .addr=_addr }
+
+sos_ret_t sos_memcpy_generic_to(sos_genaddr_t to_addr,
+				sos_vaddr_t kernel_from,
+				sos_size_t size);
+
+sos_ret_t sos_memcpy_generic_from(sos_vaddr_t kernel_from,
+				  sos_genaddr_t from_addr,
+				  sos_size_t size);
+
 #endif /* _SOS_UACCESS_H_ */
diff -ruNwb /tmp/sos-code-article9/sos/umem_vmm.c ../sos-code-article9.5/sos/umem_vmm.c
--- /tmp/sos-code-article9/sos/umem_vmm.c	2005-12-28 11:44:58.000000000 +0100
+++ ../sos-code-article9.5/sos/umem_vmm.c	2006-01-22 12:48:31.000000000 +0100
@@ -1,4 +1,4 @@
-/* Copyright (C) 2005 David Decotigny
+/* Copyright (C) 2005,2006 David Decotigny
 
    This program is free software; you can redistribute it and/or
    modify it under the terms of the GNU General Public License
@@ -670,6 +670,7 @@
 				prev_in_as, next_in_as);
       else
 	list_add_head_named(as->list_vr, vr, prev_in_as, next_in_as);
+
       list_add_tail_named(vr->mapped_resource->list_vr, vr,
 			  prev_in_mapped_resource,
 			  next_in_mapped_resource);
@@ -882,7 +883,7 @@
 							     (sos_vaddr_t)
 							       NULL));
   {
-    sos_size_t sz_unmapped = sos_paging_unmap_interval(uaddr, size);
+    sos_ret_t sz_unmapped = sos_paging_unmap_interval(uaddr, size);
     SOS_ASSERT_FATAL(sz_unmapped >= 0);
     as->phys_total -= sz_unmapped;
   }
@@ -1212,6 +1213,70 @@
 
 
 sos_ret_t
+sos_umem_vmm_sync(struct sos_umem_vmm_as * as,
+		  sos_uaddr_t uaddr, sos_size_t size,
+		  sos_ui32_t flags)
+{
+  if (! SOS_IS_PAGE_ALIGNED(uaddr))
+    return -SOS_EINVAL;
+  if (size <= 0)
+    return -SOS_EINVAL;
+  size = SOS_PAGE_ALIGN_SUP(size);
+
+  /* Make sure the uaddr is valid */
+  if (uaddr < SOS_PAGING_BASE_USER_ADDRESS)
+    return -SOS_EINVAL;
+  if (uaddr > SOS_PAGING_TOP_USER_ADDRESS - size)
+    return -SOS_EINVAL;
+
+  /* Go from page to page, and for each dirty page in the region, call
+     the sync_page method */
+  while (TRUE)
+    {
+      struct sos_umem_vmm_vr *vr;
+
+      if (size <= 0)
+	break;
+
+      /* Find any VR intersecting with the given interval */
+      vr = find_first_intersecting_vr(as, uaddr, size);
+      if (NULL == vr)
+	break;
+
+      /* For private or anonymous mappings => no backing store */
+      if ( !(vr->flags & SOS_VR_MAP_SHARED)
+	   || (vr->mapped_resource->flags & SOS_MAPPED_RESOURCE_ANONYMOUS)
+
+	   /* Likewise for non msync-able regions */
+	   || ! vr->ops->sync_page )
+	{
+	  if (size <= vr->size)
+	    break;
+
+	  uaddr += vr->size;
+	  size  -= vr->size;
+	}
+
+      /* Find the next dirty page in this VR */
+      for ( ; (size > 0)
+	      && (uaddr - vr->start < vr->size) ;
+	    uaddr += SOS_PAGE_SIZE,
+	      size -= SOS_PAGE_SIZE)
+	if (sos_paging_is_dirty(uaddr))
+	  {
+	    /* Synchronize it with its backing store */
+	    vr->ops->sync_page(vr, uaddr, flags);
+	    uaddr += SOS_PAGE_SIZE;
+	    size  -= SOS_PAGE_SIZE;
+	    break;
+	  }
+    }
+
+  return SOS_OK;
+}
+
+
+sos_ret_t
 sos_umem_vmm_resize(struct sos_umem_vmm_as * as,
 		    sos_uaddr_t old_uaddr, sos_size_t old_size,
 		    sos_uaddr_t *new_uaddr, sos_size_t new_size,
diff -ruNwb /tmp/sos-code-article9/sos/umem_vmm.h ../sos-code-article9.5/sos/umem_vmm.h
--- /tmp/sos-code-article9/sos/umem_vmm.h	2005-12-28 11:44:58.000000000 +0100
+++ ../sos-code-article9.5/sos/umem_vmm.h	2006-01-22 12:48:31.000000000 +0100
@@ -1,4 +1,4 @@
-/* Copyright (C) 2005 David Decotigny
+/* Copyright (C) 2005,2006 David Decotigny
 
    This program is free software; you can redistribute it and/or
    modify it under the terms of the GNU General Public License
@@ -205,6 +205,22 @@
   sos_ret_t (*page_in)(struct sos_umem_vmm_vr * vr,
 		       sos_uaddr_t uaddr,
 		       sos_bool_t write_access);
+
+
+  /**
+   * Called to synchronize the contents of the given page with its
+   * backing store. This method is responsible for resetting the dirty
+   * flag of the page (@see sos_paging_set_dirty). Appropriate locking
+   * MUST be handled internally by this method.
+   *
+   * @note Optional
+   */
+  sos_ret_t (*sync_page)(struct sos_umem_vmm_vr * vr,
+			 sos_uaddr_t page_uaddr,
+			 sos_ui32_t flags);
+  /** Flags for msync */
+#define SOS_MSYNC_SYNC  (1 << 0)
+#define SOS_MSYNC_ASYNC (0 << 0)
 };
 
 
@@ -237,7 +253,7 @@
 
   /**
    * Custom data that the user is free to define: the umem_vmm
-   * subsystem won't ever look at it or change it
+   * subsystem won't ever look at it or change it.
    */
   void *custom_data;
 };
@@ -265,7 +281,7 @@
 /**
  * Setup the umem_vmm subsystem.
  */
-sos_ret_t sos_umem_vmm_subsystem_setup();
+sos_ret_t sos_umem_vmm_subsystem_setup(void);
 
 
 /**
@@ -444,6 +460,20 @@
 
 
 /**
+ * Flush the given pages to backing store. Call the sync_page method
+ * for each of the dirty pages. The MMU is expected to be configured
+ * for the given AS !
+ *
+ * @note MAKE SURE YOU CALL
+ * sos_thread_prepare_user_space_access()/sos_thread_end_user_space_access()
+ */
+sos_ret_t
+sos_umem_vmm_sync(struct sos_umem_vmm_as * as,
+		  sos_uaddr_t uaddr, sos_size_t size,
+		  sos_ui32_t flags);
+
+
+/**
  * Change the access rights of the given address interval. This might
  * concern 0, 1 or more VRs, and result in the splitting in 1 or 2 VRs
  * if they are partially concerned by the change in protection.. Same
diff -ruNwb /tmp/sos-code-article9/userland/banner.c ../sos-code-article9.5/userland/banner.c
--- /tmp/sos-code-article9/userland/banner.c	2005-12-28 11:44:58.000000000 +0100
+++ ../sos-code-article9.5/userland/banner.c	2006-01-22 12:48:31.000000000 +0100
@@ -118,7 +118,7 @@
 }
 
 static struct thread_param p1, p2;
-int main()
+int main(void)
 {
   int fd;
 
diff -ruNwb /tmp/sos-code-article9/userland/blktest.c ../sos-code-article9.5/userland/blktest.c
--- /tmp/sos-code-article9/userland/blktest.c	1970-01-01 01:00:00.000000000 +0100
+++ ../sos-code-article9.5/userland/blktest.c	2006-01-22 12:48:31.000000000 +0100
@@ -0,0 +1,208 @@
+/* Copyright (C) 2005,2006 David Decotigny
+
+   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., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
+   USA. 
+*/
+
+#include <crt.h>
+#include <libc.h>
+#include <stdarg.h>
+#include <string.h>
+#include <debug.h>
+#include <drivers/devices.h>
+
+#include "fstest_utils.h"
+
+/**
+ * @file blktest.c
+ *
+ * Block devices tests
+ */
+static int test_device(char const* path, char magic)
+{
+  char wrbuff[32767];
+  char rdbuff[32767];
+  int fd, fdcheck, i;
+  char * mem;
+
+  fd = open(path, O_RDWR);
+  if (fd < 0)
+    {
+      bochs_printf("Cannot open %s: error %d\n", path, fd);
+      return -1;
+    }
+  
+  bochs_printf("Testing %s...\n", path);
+
+  /* Basic seek tests */
+  TEST_EXPECT_CONDITION(lseek(fd, 2*512, SEEK_SET), RETVAL == 2*512);
+  TEST_EXPECT_CONDITION(write(fd, wrbuff, 1), RETVAL == 1);
+  TEST_EXPECT_CONDITION(lseek(fd, 4*512, SEEK_SET), RETVAL == 4*512);
+  TEST_EXPECT_CONDITION(read(fd, rdbuff, 1), RETVAL == 1);
+
+  /* Prepare to write a large chunk */
+  for (i = 0 ; i < sizeof(wrbuff) ; i ++)
+    wrbuff[i] = magic + i;
+  memset(rdbuff, 0xaa, sizeof(wrbuff));
+  TEST_EXPECT_CONDITION(lseek(fd, 3*512 + 43, SEEK_SET), RETVAL == 3*512 + 43);
+  TEST_EXPECT_CONDITION(write(fd, wrbuff, sizeof(wrbuff)),
+			RETVAL == sizeof(wrbuff));
+  TEST_EXPECT_CONDITION(write(fd, rdbuff, sizeof(rdbuff)),
+			RETVAL == sizeof(rdbuff));
+
+  memset(rdbuff, 0xbb, sizeof(rdbuff));
+  TEST_EXPECT_CONDITION(lseek(fd, 3*512 + 47, SEEK_SET), RETVAL == 3*512 + 47);
+  TEST_EXPECT_CONDITION(read(fd, rdbuff, sizeof(wrbuff)),
+			RETVAL == sizeof(wrbuff));
+  TEST_EXPECT_CONDITION(memcmp(rdbuff, wrbuff+4, sizeof(wrbuff)-4),
+			RETVAL == 0);
+
+  fdcheck = open(path, O_RDONLY);
+
+#define MAPOFFS (50*4096)
+
+  /* Test mmap private mappings */
+  TEST_EXPECT_CONDITION(mem = mmap(NULL, sizeof(wrbuff), PROT_READ,
+				   MAP_PRIVATE, fd, MAPOFFS + 3),
+			(void*)RETVAL == NULL);
+  TEST_EXPECT_CONDITION(mem = mmap(NULL, sizeof(wrbuff),
+				   PROT_READ | PROT_WRITE, MAP_PRIVATE,
+				   fd, MAPOFFS),
+			(void*)RETVAL != NULL);
+
+  for (i = 0 ; i < sizeof(wrbuff) ; i ++)
+    wrbuff[i] = magic + i;
+  TEST_EXPECT_CONDITION(1, 1); /* Print a debug line */
+
+  memset(rdbuff, 0xaa, sizeof(wrbuff));
+  TEST_EXPECT_CONDITION(lseek(fd, MAPOFFS, SEEK_SET), RETVAL == MAPOFFS);
+  TEST_EXPECT_CONDITION(write(fd, wrbuff, sizeof(wrbuff)),
+			RETVAL == sizeof(wrbuff));
+  TEST_EXPECT_CONDITION(memcmp(wrbuff, mem, sizeof(wrbuff)),
+			RETVAL == 0);
+
+  for (i = 0 ; i < sizeof(wrbuff) ; i ++)
+    mem[i] = wrbuff[i]*i;
+  TEST_EXPECT_CONDITION(1, 1); /* Print a debug line */
+
+  memset(rdbuff, 0xaa, sizeof(rdbuff));
+  TEST_EXPECT_CONDITION(lseek(fdcheck, MAPOFFS, SEEK_SET), RETVAL == MAPOFFS);
+  TEST_EXPECT_CONDITION(read(fdcheck, rdbuff, sizeof(wrbuff)),
+			RETVAL == sizeof(wrbuff));
+  TEST_EXPECT_CONDITION(memcmp(wrbuff, rdbuff, sizeof(wrbuff)), RETVAL == 0);
+
+  TEST_EXPECT_CONDITION(msync(mem, sizeof(wrbuff), MS_SYNC), RETVAL == 0);
+  TEST_EXPECT_CONDITION(munmap(mem, sizeof(wrbuff)), RETVAL == 0);
+
+  /* Test mmap shared mappings */
+  TEST_EXPECT_CONDITION(mem = mmap(mem, sizeof(wrbuff), PROT_READ,
+				   MAP_SHARED, fd, MAPOFFS + 3),
+			(void*)RETVAL == NULL);
+  TEST_EXPECT_CONDITION(mem = mmap(mem, sizeof(wrbuff),
+				   PROT_READ | PROT_WRITE, MAP_SHARED,
+				   fd, MAPOFFS),
+			(void*)RETVAL != NULL);
+  for (i = 0 ; i < sizeof(wrbuff) ; i ++)
+    wrbuff[i] = magic + i;
+  memset(rdbuff, 0xaa, sizeof(rdbuff));
+  TEST_EXPECT_CONDITION(lseek(fd, MAPOFFS, SEEK_SET), RETVAL == MAPOFFS);
+  TEST_EXPECT_CONDITION(write(fd, wrbuff, sizeof(wrbuff)),
+			RETVAL == sizeof(wrbuff));
+  TEST_EXPECT_CONDITION(memcmp(wrbuff, mem, sizeof(wrbuff)), RETVAL == 0);
+
+  for (i = 0 ; i < sizeof(wrbuff) ; i ++)
+    mem[i] = wrbuff[i]*i;
+  memset(rdbuff, 0xaa, sizeof(rdbuff));
+  TEST_EXPECT_CONDITION(lseek(fdcheck, MAPOFFS, SEEK_SET), RETVAL == MAPOFFS);
+  TEST_EXPECT_CONDITION(read(fdcheck, rdbuff, sizeof(wrbuff)),
+			RETVAL == sizeof(wrbuff));
+  TEST_EXPECT_CONDITION(memcmp(wrbuff, rdbuff, sizeof(wrbuff)), RETVAL != 0);
+  TEST_EXPECT_CONDITION(memcmp(mem, rdbuff, sizeof(wrbuff)), RETVAL == 0);
+
+  TEST_EXPECT_CONDITION(msync(mem, sizeof(wrbuff), MS_SYNC), RETVAL == 0);
+  TEST_EXPECT_CONDITION(munmap(mem, sizeof(wrbuff)), RETVAL == 0);
+
+#define STARTOFFS (3*512 + 43 + sizeof(wrbuff) + sizeof(rdbuff))
+
+  TEST_EXPECT_CONDITION(lseek(fd, STARTOFFS, SEEK_SET), RETVAL == STARTOFFS);
+  TEST_EXPECT_CONDITION(lseek(fdcheck, STARTOFFS, SEEK_SET),
+			RETVAL == STARTOFFS);
+  int wrlen = 0;
+  while (1)
+    {
+      int sz;
+      for (i = 0 ; i < sizeof(wrbuff) ; i ++)
+	wrbuff[i] = magic*magic*i + wrlen*wrlen + i;
+
+      TEST_EXPECT_CONDITION(sz = write(fd, wrbuff, sizeof(wrbuff)),
+			    (RETVAL >= 0) && (RETVAL <= sizeof(wrbuff)));
+      if (sz <= 0)
+	break;
+      wrlen += sz;
+
+      memset(rdbuff, 0xcc, sizeof(wrbuff));
+      TEST_EXPECT_CONDITION(read(fdcheck, rdbuff, sizeof(wrbuff)), RETVAL == sz);
+      TEST_EXPECT_CONDITION(memcmp(rdbuff, wrbuff, sz), RETVAL == 0);
+    }
+
+  /* Check the data was correctly written */
+  TEST_EXPECT_CONDITION(lseek(fd, STARTOFFS, SEEK_SET), RETVAL == STARTOFFS);
+  int rdlen = 0;
+  while (1)
+    {
+      int sz;
+      for (i = 0 ; i < sizeof(wrbuff) ; i ++)
+	wrbuff[i] = magic*magic*i + rdlen*rdlen + i;
+
+      memset(rdbuff, 0xdd, sizeof(wrbuff));
+      TEST_EXPECT_CONDITION(sz = read(fd, rdbuff, sizeof(wrbuff)),
+			    (RETVAL >= 0) && (RETVAL <= sizeof(wrbuff)));
+      if (sz <= 0)
+	break;
+      rdlen += sz;
+      TEST_EXPECT_CONDITION(memcmp(rdbuff, wrbuff, sz), RETVAL == 0);
+    }
+  TEST_EXPECT_CONDITION(rdlen, wrlen == rdlen);
+
+  bochs_printf("Synching %s...\n", path);
+  ioctl(fd, SOS_IOCTL_BLOCKDEV_SYNC, 0);
+
+  close(fd);
+  bochs_printf("End of test for %s.\n", path);
+
+  return 0;
+}
+
+
+int main(void)
+{
+  int i;
+  bochs_printf("Hi from blktest\n");
+
+  /* Test partitions access */
+  for (i = 1 ; i <= 15 ; i ++)
+    {
+      char path[64];
+      snprintf(path, sizeof(path), "/dev/hda%d", i);
+      if (fork() == 0)
+	{
+	  test_device(path, i);
+	  return 0;
+	}
+    }
+
+  bochs_printf("Bye from blktest\n");
+  return 0;
+}
diff -ruNwb /tmp/sos-code-article9/userland/chartest.c ../sos-code-article9.5/userland/chartest.c
--- /tmp/sos-code-article9/userland/chartest.c	1970-01-01 01:00:00.000000000 +0100
+++ ../sos-code-article9.5/userland/chartest.c	2006-01-22 12:48:31.000000000 +0100
@@ -0,0 +1,248 @@
+/* Copyright (C) 2005 David Decotigny
+
+   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., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
+   USA. 
+*/
+
+#include <crt.h>
+#include <libc.h>
+#include <stdarg.h>
+#include <string.h>
+#include <debug.h>
+
+#include "fstest_utils.h"
+
+/**
+ * @file chartest.c
+ *
+ * Character devices tests
+ */
+
+int main(void)
+{
+  int fd, len;
+  unsigned int sum, mem_size;
+  char buff[256];
+  char *uaddr;
+
+  bochs_printf("Hi from chartest\n");
+  bochs_printf("WARNING: This test will eventually write 0 on kernel code !\n");
+  bochs_printf("This WILL crash the kernel (as expected...) !\n");
+  printf("WARNING: This test will eventually write 0 on kernel code !\n");
+  printf("This WILL crash the kernel (as expected...) !\n");
+
+  /* Make things more complicated */
+  fork();
+  fork();
+
+  /*
+   * /dev/zero
+   */
+
+  TEST_EXPECT_CONDITION(fd = open("/dev/zero", O_RDWR), RETVAL == 3);
+
+  /* Make things more complicated: spawn and stay in child */
+  if (fork() > 0)
+    return 0;
+
+  strzcpy(buff, "Garbage", sizeof(buff));
+  TEST_EXPECT_CONDITION(len = read(fd, buff, sizeof(buff)),
+			RETVAL == sizeof(buff));
+
+  /* Make sure it is full with 0 */
+  for (len = 0 ; len < sizeof(buff) ; len ++)
+    if (buff[len])
+      break;
+  TEST_EXPECT_CONDITION(len, RETVAL == sizeof(buff));
+
+  TEST_EXPECT_CONDITION(len = write(fd, 0, 123456789), RETVAL == 123456789);
+  TEST_EXPECT_CONDITION(len = lseek(fd, 1234, SEEK_SET), RETVAL == 1234);
+
+  /* Map it once */
+  TEST_EXPECT_CONDITION(uaddr = mmap(NULL, 8192,
+				     PROT_READ | PROT_WRITE, 0,
+				     fd, 4096), RETVAL != (int)NULL);
+
+  /* Make sure it is full with 0 */
+  for (len = 0 ; len < 8192 ; len ++)
+    if (uaddr[len])
+      break;
+  TEST_EXPECT_CONDITION(len, RETVAL == 8192);
+
+  strzcpy(uaddr, "Hello /dev/zero", 8192);
+  TEST_EXPECT_CONDITION(strcmp(uaddr, "Hello /dev/zero"), RETVAL == 0);
+
+  /* Map it again */
+  TEST_EXPECT_CONDITION(uaddr = mmap(NULL, 8192,
+				     PROT_READ | PROT_WRITE, 0,
+				     fd, 4096), RETVAL != (int)NULL);
+  /* Make sure it is full with 0 */
+  for (len = 0 ; len < 8192 ; len ++)
+    if (uaddr[len])
+      break;
+  TEST_EXPECT_CONDITION(len, RETVAL == 8192);
+
+  strzcpy(uaddr, "Hello /dev/zero", 8192);
+  TEST_EXPECT_CONDITION(strcmp(uaddr, "Hello /dev/zero"), RETVAL == 0);
+
+  TEST_EXPECT_CONDITION(close(fd), RETVAL == 0);
+
+
+  /*
+   * /dev/null
+   */
+
+  TEST_EXPECT_CONDITION(fd = open("/dev/null", O_RDWR), RETVAL == 3);
+
+  /* Make things more complicated: spawn and stay in child */
+  if (fork() > 0)
+    return 0;
+
+  strzcpy(buff, "Garbage", sizeof(buff));
+  TEST_EXPECT_CONDITION(len = read(fd, buff, sizeof(buff)),
+			RETVAL == 0);
+
+  /* Make sure buffer did not change */
+  TEST_EXPECT_CONDITION(strcmp("Garbage", buff), RETVAL == 0);
+
+  TEST_EXPECT_CONDITION(len = write(fd, 0, 123456789), RETVAL == 123456789);
+  TEST_EXPECT_CONDITION(len = lseek(fd, 1234, SEEK_SET), RETVAL == 1234);
+
+  /* Map it once */
+  TEST_EXPECT_CONDITION(uaddr = mmap(NULL, 8192,
+				     PROT_READ | PROT_WRITE, 0,
+				     fd, 4096), RETVAL != (int)NULL);
+  /* Make sure it is full with 0 */
+  for (len = 0 ; len < 8192 ; len ++)
+    if (uaddr[len])
+      break;
+  TEST_EXPECT_CONDITION(len, RETVAL == 8192);
+
+  strzcpy(uaddr, "Hello /dev/null", 8192);
+  TEST_EXPECT_CONDITION(strcmp(uaddr, "Hello /dev/null"), RETVAL == 0);
+
+  /* Map it again */
+  TEST_EXPECT_CONDITION(uaddr = mmap(NULL, 8192,
+				     PROT_READ | PROT_WRITE, 0,
+				     fd, 4096), RETVAL != (int)NULL);
+  /* Make sure it is full with 0 */
+  for (len = 0 ; len < 8192 ; len ++)
+    if (uaddr[len])
+      break;
+  TEST_EXPECT_CONDITION(len, RETVAL == 8192);
+
+  strzcpy(uaddr, "Hello /dev/null", 8192);
+  TEST_EXPECT_CONDITION(strcmp(uaddr, "Hello /dev/null"), RETVAL == 0);
+
+  TEST_EXPECT_CONDITION(close(fd), RETVAL == 0);
+
+  /*
+   * Invalid device instance of zero.c
+   */
+  TEST_EXPECT_CONDITION(mknod("/dev/invalid_zero", S_IRUSR | S_IWUSR,
+			      S_IFCHR, 1, 42), RETVAL == 0);
+  ls("/", 1, 1);
+  TEST_EXPECT_CONDITION(fd = open("/dev/invalid_zero", O_RDWR), RETVAL < 0);
+  TEST_EXPECT_CONDITION(unlink("/dev/invalid_zero"), RETVAL == 0);
+
+
+  /*
+   * Invalid device class / device instance
+   */
+  TEST_EXPECT_CONDITION(mknod("/dev/invalid_devclass", S_IRUSR | S_IWUSR,
+			      S_IFCHR, 54654, 476576), RETVAL == 0);
+  ls("/", 1, 1);
+  TEST_EXPECT_CONDITION(fd = open("/dev/invalid_devclass",
+				  O_RDWR), RETVAL < 0);
+  TEST_EXPECT_CONDITION(unlink("/dev/invalid_devclass"), RETVAL == 0);
+
+
+  /*
+   * Make a checksum of all physical memory (/dev/mem)
+   */
+  TEST_EXPECT_CONDITION(fd = open("/dev/mem", O_RDONLY), RETVAL == 3);
+
+  /* Make things more complicated: spawn and stay in child */
+  if (fork() > 0)
+    return 0;
+
+  for (mem_size = 0, sum = 0 ;
+       len = read(fd, buff, sizeof(buff)), len > 0 ;
+       mem_size += len)
+    {
+      char *c;
+      if (((mem_size / sizeof(buff)) % 4000) == 0)
+	printf("Read %d MB of physical memory\n", mem_size >> 20);
+
+      for (c = buff ; c - buff < len ; c++)
+	sum += *c;
+    }
+  TEST_EXPECT_CONDITION(mem_size, RETVAL > 0);
+  printf("Checkum of RAM (%d MB) is %x\n", mem_size >> 20, sum);
+  TEST_EXPECT_CONDITION(len = lseek(fd, 0, SEEK_SET), len == 0);
+  TEST_EXPECT_CONDITION(len = lseek(fd, 0, SEEK_END), len == mem_size);
+  TEST_EXPECT_CONDITION(close(fd), RETVAL == 0);
+
+  /*
+   * Make a checksum of main kernel memory (/dev/kmem)
+   */
+  TEST_EXPECT_CONDITION(fd = open("/dev/kmem", O_RDONLY), RETVAL == 3);
+
+  /* Make things more complicated: spawn and stay in child */
+  if (fork() > 0)
+    return 0;
+
+  /* Seek to beginning of main .text section */
+  TEST_EXPECT_CONDITION(lseek(fd, 0x201000, SEEK_SET), RETVAL == 0x201000);
+  for (mem_size = 0, sum = 0 ;
+       len = read(fd, buff, sizeof(buff)), len > 0 ;
+       mem_size += len)
+    {
+      char *c;
+      if (((mem_size / sizeof(buff)) % 400) == 0)
+	printf("Read %d kB of kernel memory\n", mem_size >> 10);
+
+      for (c = buff ; c - buff < len ; c++)
+	sum += *c;
+    }
+  TEST_EXPECT_CONDITION(mem_size, RETVAL > 0);
+  printf("Checkum of main kernel area (%d kB) is %x\n",
+	 mem_size >> 10, sum);
+  TEST_EXPECT_CONDITION(len = lseek(fd, 0, SEEK_SET), len == 0);
+  TEST_EXPECT_CONDITION(len = lseek(fd, 0, SEEK_END), len == 0x40000000);
+  TEST_EXPECT_CONDITION(close(fd), RETVAL == 0);
+
+  printf("Bye from chartest\n");
+  ls("/", 1, 1);
+
+  /* Crash the whole kernel */
+  bochs_printf("[31;1mNOW Crashing the whole thing !...[m\n");
+
+  {
+    int i;
+    for (i = 5 ; i >= 0 ; i --)
+      {
+	printf("Crashing the whole thing in %ds !...     \r", i);
+	sleep(1);
+      }
+  }
+  printf("\nGO !\n");
+  TEST_EXPECT_CONDITION(fd = open("/dev/mem", O_WRONLY), RETVAL == 3);
+  memset(buff, 0xcc, sizeof(buff));
+  while (write(fd, buff, sizeof(buff)))
+    continue;  
+
+  return 0;
+}
diff -ruNwb /tmp/sos-code-article9/userland/crt.c ../sos-code-article9.5/userland/crt.c
--- /tmp/sos-code-article9/userland/crt.c	2005-12-28 11:44:58.000000000 +0100
+++ ../sos-code-article9.5/userland/crt.c	2006-01-22 12:48:31.000000000 +0100
@@ -32,11 +32,11 @@
 /**
  * Starter function !
  */
-void _start() __attribute__((noreturn));
-void _start()
+void _start(void) __attribute__((noreturn));
+void _start(void)
 {
   /* This starter function expects a main() function somewhere */
-  extern int main();
+  extern int main(void);
 
   /* Reset the bss section */
   extern char _bbss, _ebss;
@@ -226,6 +226,15 @@
 }
 
 
+int _sos_msync(void *start, size_t length, int flags)
+{
+  return _sos_syscall3(SOS_SYSCALL_ID_MSYNC,
+		       (unsigned int)start,
+		       length,
+		       flags);  
+}
+
+
 /**
  * Helper function that represents the start routine of a new user
  * thread created from user space (syscall new_thread). It takes 2
@@ -233,7 +242,7 @@
  * cpu_ustate_init() in hwcore/cpu_context.c): the start function of
  * the new thread (eax), the argument passed to it (ebx).
  */
-static void thread_routine()
+static void thread_routine(void)
 {
   /* NOTE: variables as named registers is a gcc extension */
   register unsigned long int reg_arg1 asm("%eax");
diff -ruNwb /tmp/sos-code-article9/userland/crt.h ../sos-code-article9.5/userland/crt.h
--- /tmp/sos-code-article9/userland/crt.h	2005-12-28 11:44:58.000000000 +0100
+++ ../sos-code-article9.5/userland/crt.h	2006-01-22 12:48:31.000000000 +0100
@@ -140,6 +140,15 @@
 		 unsigned long flags);
 
 
+/**
+ * Syscall to synchronize caches with the contents of the mapped memory
+ */
+int _sos_msync(void *start, size_t length, int flags);
+
+
+/**
+ * Definition of a thread routine
+ */
 typedef void (sos_thread_func_t(unsigned long int arg));
 
 /**
diff -ruNwb /tmp/sos-code-article9/userland/devtest.c ../sos-code-article9.5/userland/devtest.c
--- /tmp/sos-code-article9/userland/devtest.c	2005-12-28 11:44:58.000000000 +0100
+++ ../sos-code-article9.5/userland/devtest.c	1970-01-01 01:00:00.000000000 +0100
@@ -1,251 +0,0 @@
-/* Copyright (C) 2005 David Decotigny
-
-   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., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
-   USA. 
-*/
-
-#include <crt.h>
-#include <libc.h>
-#include <stdarg.h>
-#include <string.h>
-#include <debug.h>
-
-#include "fstest_utils.h"
-
-/**
- * @file devtest.c
- *
- * Block/character devices tests
- */
-
-int main()
-{
-  int fd, len;
-  unsigned int sum, mem_size;
-  char buff[256];
-  char *uaddr;
-
-  bochs_printf("Hi from devtest\n");
-
-  /* Make things more complicated */
-  fork();
-  fork();
-
-  /*
-   * /dev/zero
-   */
-
-  TEST_EXPECT_CONDITION(fd = open("/dev/zero", O_RDWR), RETVAL == 3);
-
-  /* Make things more complicated: spawn and stay in child */
-  if (fork() > 0)
-    return 0;
-
-  strzcpy(buff, "Garbage", sizeof(buff));
-  TEST_EXPECT_CONDITION(len = read(fd, buff, sizeof(buff)),
-			RETVAL == sizeof(buff));
-
-  /* Make sure it is full with 0 */
-  for (len = 0 ; len < sizeof(buff) ; len ++)
-    if (buff[len])
-      break;
-  TEST_EXPECT_CONDITION(len, RETVAL == sizeof(buff));
-
-  TEST_EXPECT_CONDITION(len = write(fd, 0, 123456789), RETVAL == 123456789);
-  TEST_EXPECT_CONDITION(len = lseek(fd, 1234, SEEK_SET), RETVAL == 1234);
-
-  /* Map it once */
-  TEST_EXPECT_CONDITION(uaddr = mmap(NULL, 8192,
-				     PROT_READ | PROT_WRITE, 0,
-				     fd, 4096), RETVAL != (int)NULL);
-
-  /* Make sure it is full with 0 */
-  for (len = 0 ; len < 8192 ; len ++)
-    if (uaddr[len])
-      break;
-  TEST_EXPECT_CONDITION(len, RETVAL == 8192);
-
-  strzcpy(uaddr, "Hello /dev/zero", 8192);
-  TEST_EXPECT_CONDITION(strcmp(uaddr, "Hello /dev/zero"), RETVAL == 0);
-
-  /* Map it again */
-  TEST_EXPECT_CONDITION(uaddr = mmap(NULL, 8192,
-				     PROT_READ | PROT_WRITE, 0,
-				     fd, 4096), RETVAL != (int)NULL);
-  /* Make sure it is full with 0 */
-  for (len = 0 ; len < 8192 ; len ++)
-    if (uaddr[len])
-      break;
-  TEST_EXPECT_CONDITION(len, RETVAL == 8192);
-
-  strzcpy(uaddr, "Hello /dev/zero", 8192);
-  TEST_EXPECT_CONDITION(strcmp(uaddr, "Hello /dev/zero"), RETVAL == 0);
-
-  TEST_EXPECT_CONDITION(close(fd), RETVAL == 0);
-
-
-  /*
-   * /dev/null
-   */
-
-  TEST_EXPECT_CONDITION(fd = open("/dev/null", O_RDWR), RETVAL == 3);
-
-  /* Make things more complicated: spawn and stay in child */
-  if (fork() > 0)
-    return 0;
-
-  strzcpy(buff, "Garbage", sizeof(buff));
-  TEST_EXPECT_CONDITION(len = read(fd, buff, sizeof(buff)),
-			RETVAL == 0);
-
-  /* Make sure buffer did not change */
-  TEST_EXPECT_CONDITION(strcmp("Garbage", buff), RETVAL == 0);
-
-  TEST_EXPECT_CONDITION(len = write(fd, 0, 123456789), RETVAL == 123456789);
-  TEST_EXPECT_CONDITION(len = lseek(fd, 1234, SEEK_SET), RETVAL == 1234);
-
-  /* Map it once */
-  TEST_EXPECT_CONDITION(uaddr = mmap(NULL, 8192,
-				     PROT_READ | PROT_WRITE, 0,
-				     fd, 4096), RETVAL != (int)NULL);
-  /* Make sure it is full with 0 */
-  for (len = 0 ; len < 8192 ; len ++)
-    if (uaddr[len])
-      break;
-  TEST_EXPECT_CONDITION(len, RETVAL == 8192);
-
-  strzcpy(uaddr, "Hello /dev/null", 8192);
-  TEST_EXPECT_CONDITION(strcmp(uaddr, "Hello /dev/null"), RETVAL == 0);
-
-  /* Map it again */
-  TEST_EXPECT_CONDITION(uaddr = mmap(NULL, 8192,
-				     PROT_READ | PROT_WRITE, 0,
-				     fd, 4096), RETVAL != (int)NULL);
-  /* Make sure it is full with 0 */
-  for (len = 0 ; len < 8192 ; len ++)
-    if (uaddr[len])
-      break;
-  TEST_EXPECT_CONDITION(len, RETVAL == 8192);
-
-  strzcpy(uaddr, "Hello /dev/null", 8192);
-  TEST_EXPECT_CONDITION(strcmp(uaddr, "Hello /dev/null"), RETVAL == 0);
-
-  TEST_EXPECT_CONDITION(close(fd), RETVAL == 0);
-
-  /*
-   * Invalid device instance of zero.c
-   */
-  TEST_EXPECT_CONDITION(mknod("/dev/invalid_zero", S_IRUSR | S_IWUSR,
-			      S_IFCHR, 1, 42), RETVAL == 0);
-  ls("/", 1, 1);
-  TEST_EXPECT_CONDITION(fd = open("/dev/invalid_zero", O_RDWR), RETVAL < 0);
-  TEST_EXPECT_CONDITION(unlink("/dev/invalid_zero"), RETVAL == 0);
-
-
-  /*
-   * Invalid device class / device instance
-   */
-  TEST_EXPECT_CONDITION(mknod("/dev/invalid_devclass", S_IRUSR | S_IWUSR,
-			      S_IFCHR, 54654, 476576), RETVAL == 0);
-  ls("/", 1, 1);
-  TEST_EXPECT_CONDITION(fd = open("/dev/invalid_devclass",
-				  O_RDWR), RETVAL < 0);
-  TEST_EXPECT_CONDITION(unlink("/dev/invalid_devclass"), RETVAL == 0);
-
-
-  /*
-   * Test Block devices
-   */
-  TEST_EXPECT_CONDITION(fd = open("/dev/hda", O_RDONLY), RETVAL < 0);
-  ls("/", 1, 1);
-  TEST_EXPECT_CONDITION(close(fd), RETVAL < 0);
-
-  /*
-   * Make a checksum of all physical memory (/dev/mem)
-   */
-  TEST_EXPECT_CONDITION(fd = open("/dev/mem", O_RDONLY), RETVAL == 3);
-
-  /* Make things more complicated: spawn and stay in child */
-  if (fork() > 0)
-    return 0;
-
-  for (mem_size = 0, sum = 0 ;
-       len = read(fd, buff, sizeof(buff)), len > 0 ;
-       mem_size += len)
-    {
-      char *c;
-      if (((mem_size / sizeof(buff)) % 4000) == 0)
-	printf("Read %d MB of physical memory\n", mem_size >> 20);
-
-      for (c = buff ; c - buff < len ; c++)
-	sum += *c;
-    }
-  TEST_EXPECT_CONDITION(mem_size, RETVAL > 0);
-  printf("Checkum of RAM (%d MB) is %x\n", mem_size >> 20, sum);
-  TEST_EXPECT_CONDITION(len = lseek(fd, 0, SEEK_SET), len == 0);
-  TEST_EXPECT_CONDITION(len = lseek(fd, 0, SEEK_END), len == mem_size);
-  TEST_EXPECT_CONDITION(close(fd), RETVAL == 0);
-
-  /*
-   * Make a checksum of main kernel memory (/dev/kmem)
-   */
-  TEST_EXPECT_CONDITION(fd = open("/dev/kmem", O_RDONLY), RETVAL == 3);
-
-  /* Make things more complicated: spawn and stay in child */
-  if (fork() > 0)
-    return 0;
-
-  /* Seek to beginning of main .text section */
-  TEST_EXPECT_CONDITION(lseek(fd, 0x201000, SEEK_SET), RETVAL == 0x201000);
-  for (mem_size = 0, sum = 0 ;
-       len = read(fd, buff, sizeof(buff)), len > 0 ;
-       mem_size += len)
-    {
-      char *c;
-      if (((mem_size / sizeof(buff)) % 400) == 0)
-	printf("Read %d kB of kernel memory\n", mem_size >> 10);
-
-      for (c = buff ; c - buff < len ; c++)
-	sum += *c;
-    }
-  TEST_EXPECT_CONDITION(mem_size, RETVAL > 0);
-  printf("Checkum of main kernel area (%d kB) is %x\n",
-	 mem_size >> 10, sum);
-  TEST_EXPECT_CONDITION(len = lseek(fd, 0, SEEK_SET), len == 0);
-  TEST_EXPECT_CONDITION(len = lseek(fd, 0, SEEK_END), len == 0x40000000);
-  TEST_EXPECT_CONDITION(close(fd), RETVAL == 0);
-
-  printf("Bye from devtest\n");
-  ls("/", 1, 1);
-
-  /* Crash the whole kernel */
-  bochs_printf("[31;1mNOW Crashing the whole thing !...[m\n");
-
-  {
-    int i;
-    for (i = 5 ; i >= 0 ; i --)
-      {
-	printf("Crashing the whole thing in %ds !...     \r", i);
-	sleep(1);
-      }
-  }
-  printf("\nGO !\n");
-  TEST_EXPECT_CONDITION(fd = open("/dev/mem", O_WRONLY), RETVAL == 3);
-  memset(buff, 0xcc, sizeof(buff));
-  while (write(fd, buff, sizeof(buff)))
-    continue;  
-
-  return 0;
-}
diff -ruNwb /tmp/sos-code-article9/userland/fstest.c ../sos-code-article9.5/userland/fstest.c
--- /tmp/sos-code-article9/userland/fstest.c	2005-12-28 11:44:58.000000000 +0100
+++ ../sos-code-article9.5/userland/fstest.c	2006-01-22 12:48:31.000000000 +0100
@@ -32,7 +32,7 @@
  * File-system tests
  */
 
-int main()
+int main(void)
 {
   int fd, len;
   char buff[256];
diff -ruNwb /tmp/sos-code-article9/userland/fstest_utils.c ../sos-code-article9.5/userland/fstest_utils.c
--- /tmp/sos-code-article9/userland/fstest_utils.c	2005-12-28 11:44:58.000000000 +0100
+++ ../sos-code-article9.5/userland/fstest_utils.c	2006-01-22 12:48:31.000000000 +0100
@@ -60,6 +60,7 @@
 	case S_IFDIR: entrychar='d'; entrysuffix="/"; break;
 	case S_IFLNK: entrychar='l'; entrysuffix="@"; break;
 	case S_IFCHR: entrychar='c'; entrysuffix=NULL; break;
+	case S_IFBLK: entrychar='b'; entrysuffix=NULL; break;
 	default: entrychar='?'; entrysuffix="?!?"; break;
 	}
 
@@ -86,7 +87,7 @@
 		  close(fd);
 		}
 	    }
-	  else if (stat.st_type == S_IFCHR)
+	  else if ( (stat.st_type == S_IFCHR) || (stat.st_type == S_IFBLK) )
 	    {
 	      snprintf(majorminor, sizeof(majorminor), " %d,%d",
 		       stat.st_rdev_major, stat.st_rdev_minor);
@@ -110,7 +111,7 @@
 		       tab, dirent->name, entrysuffix);
 
       /* Next iteration */
-      if (recursive)
+      if (recursive && (dirent->type == S_IFDIR))
 	{
 	  int fd_here = dirfd(here);
 	  if (chdir(dirent->name))
diff -ruNwb /tmp/sos-code-article9/userland/init.c ../sos-code-article9.5/userland/init.c
--- /tmp/sos-code-article9/userland/init.c	2005-12-28 11:44:58.000000000 +0100
+++ ../sos-code-article9.5/userland/init.c	2006-01-22 12:48:31.000000000 +0100
@@ -26,7 +26,7 @@
  * Test fork and exec()
  */
 
-int main()
+int main(void)
 {
   bochs_printf("init: Welcome in userland !\n");
 
@@ -46,6 +46,27 @@
   TEST_EXPECT_CONDITION(mknod("/dev/ttyS", S_IRUSR | S_IWUSR,
 			      S_IFCHR, SOS_CHARDEV_TTY_MAJOR, SOS_CHARDEV_SERIAL_MINOR), RETVAL == 0);
 
+  TEST_EXPECT_CONDITION(mknod("/dev/hda", S_IRUSR | S_IWUSR,
+			      S_IFBLK, SOS_BLOCKDEV_IDE_MAJOR, 0), RETVAL == 0);
+  TEST_EXPECT_CONDITION(mknod("/dev/hda1", S_IRUSR | S_IWUSR,
+			      S_IFBLK, SOS_BLOCKDEV_IDE_MAJOR, 1), RETVAL == 0);
+  TEST_EXPECT_CONDITION(mknod("/dev/hda2", S_IRUSR | S_IWUSR,
+			      S_IFBLK, SOS_BLOCKDEV_IDE_MAJOR, 2), RETVAL == 0);
+  TEST_EXPECT_CONDITION(mknod("/dev/hda3", S_IRUSR | S_IWUSR,
+			      S_IFBLK, SOS_BLOCKDEV_IDE_MAJOR, 3), RETVAL == 0);
+  TEST_EXPECT_CONDITION(mknod("/dev/hda4", S_IRUSR | S_IWUSR,
+			      S_IFBLK, SOS_BLOCKDEV_IDE_MAJOR, 4), RETVAL == 0);
+  TEST_EXPECT_CONDITION(mknod("/dev/hda5", S_IRUSR | S_IWUSR,
+			      S_IFBLK, SOS_BLOCKDEV_IDE_MAJOR, 5), RETVAL == 0);
+  TEST_EXPECT_CONDITION(mknod("/dev/hda6", S_IRUSR | S_IWUSR,
+			      S_IFBLK, SOS_BLOCKDEV_IDE_MAJOR, 6), RETVAL == 0);
+  TEST_EXPECT_CONDITION(mknod("/dev/hda7", S_IRUSR | S_IWUSR,
+			      S_IFBLK, SOS_BLOCKDEV_IDE_MAJOR, 7), RETVAL == 0);
+  TEST_EXPECT_CONDITION(mknod("/dev/hdb", S_IRUSR | S_IWUSR,
+			      S_IFBLK, SOS_BLOCKDEV_IDE_MAJOR, 16), RETVAL == 0);
+  TEST_EXPECT_CONDITION(mknod("/dev/hdb1", S_IRUSR | S_IWUSR,
+			      S_IFBLK, SOS_BLOCKDEV_IDE_MAJOR, 17), RETVAL == 0);
+
   ls("/", 1, 1);
 
   /* Set up the shell on the console */
@@ -73,6 +94,7 @@
   close (0);
 
 
+
   bochs_printf("init: The end\n");
   return 0;
 }
diff -ruNwb /tmp/sos-code-article9/userland/libc.c ../sos-code-article9.5/userland/libc.c
--- /tmp/sos-code-article9/userland/libc.c	2005-12-28 11:44:58.000000000 +0100
+++ ../sos-code-article9.5/userland/libc.c	2006-01-22 12:48:32.000000000 +0100
@@ -78,6 +78,12 @@
 }
 
 
+int msync(void *start, size_t length, int flags)
+{
+  return _sos_msync(start, length, flags);
+}
+
+
 /**
  * The presence of this global variable without any protected access
  * to it explains why the "brk/sbrk" functions below are MT-unsafe !
diff -ruNwb /tmp/sos-code-article9/userland/libc.h ../sos-code-article9.5/userland/libc.h
--- /tmp/sos-code-article9/userland/libc.h	2005-12-28 11:44:58.000000000 +0100
+++ ../sos-code-article9.5/userland/libc.h	2006-01-22 12:48:32.000000000 +0100
@@ -62,6 +62,7 @@
 #define PROT_WRITE (1<<1)
 #define PROT_EXEC  (1<<2) /* Not supported on IA32 */
 
+#define MAP_PRIVATE (0 << 0)
 #define MAP_SHARED (1 << 0)
 #define MAP_FIXED  (1 << 31)
 
@@ -91,6 +92,14 @@
 	      unsigned long flags);
 
 
+/** Flags for msync */
+#define MS_SYNC  (1 << 0)
+#define MS_ASYNC (0 << 0)
+
+/** Synchronize cache contents with mapped memory */
+int msync(void *start, size_t length, int flags);
+
+
 /**
  * Standard heap management API
  */
@@ -191,6 +200,7 @@
 #define S_IFDIR 0x24
 #define S_IFLNK 0x84
 #define S_IFCHR 0x48
+#define S_IFBLK 0x12
 int mknod(const char *pathname, mode_t mode,
 	  int type,
 	  unsigned int major, unsigned minor);
diff -ruNwb /tmp/sos-code-article9/userland/Makefile ../sos-code-article9.5/userland/Makefile
--- /tmp/sos-code-article9/userland/Makefile	2005-12-28 11:44:58.000000000 +0100
+++ ../sos-code-article9.5/userland/Makefile	2006-01-22 12:48:31.000000000 +0100
@@ -31,12 +31,13 @@
 
 PROGS := init myprog1 myprog2 myprog3 myprog4 myprog5 myprog6 \
          myprog7 myprog8 myprog9 myprog10 myprog11 myprog12   \
-	 myprog13 myprog14 banner fstest devtest shell
+	 myprog13 myprog14 banner fstest chartest blktest shell
 
 # Build dependencies of the programs
 init: fstest_utils.o
 fstest: fstest_utils.o
-devtest: fstest_utils.o
+chartest: fstest_utils.o
+blktest: fstest_utils.o
 shell: fstest_utils.o
 $(PROGS) : % : %.o crt.o libc.a
 
diff -ruNwb /tmp/sos-code-article9/userland/myprog10.c ../sos-code-article9.5/userland/myprog10.c
--- /tmp/sos-code-article9/userland/myprog10.c	2005-12-28 11:44:58.000000000 +0100
+++ ../sos-code-article9.5/userland/myprog10.c	2006-01-22 12:48:32.000000000 +0100
@@ -30,7 +30,7 @@
  * thread's address space
  */
 
-int main()
+int main(void)
 {
   char * zoup;
   int fd;
diff -ruNwb /tmp/sos-code-article9/userland/myprog11.c ../sos-code-article9.5/userland/myprog11.c
--- /tmp/sos-code-article9/userland/myprog11.c	2005-12-28 11:44:58.000000000 +0100
+++ ../sos-code-article9.5/userland/myprog11.c	2006-01-22 12:48:32.000000000 +0100
@@ -32,7 +32,7 @@
  */
 
 
-int main()
+int main(void)
 {
   void * moved;
   char * zoup;
diff -ruNwb /tmp/sos-code-article9/userland/myprog12.c ../sos-code-article9.5/userland/myprog12.c
--- /tmp/sos-code-article9/userland/myprog12.c	2005-12-28 11:44:58.000000000 +0100
+++ ../sos-code-article9.5/userland/myprog12.c	2006-01-22 12:48:32.000000000 +0100
@@ -30,7 +30,7 @@
  */
 
 
-int main()
+int main(void)
 {
   char *zoup;
 
diff -ruNwb /tmp/sos-code-article9/userland/myprog13.c ../sos-code-article9.5/userland/myprog13.c
--- /tmp/sos-code-article9/userland/myprog13.c	2005-12-28 11:44:58.000000000 +0100
+++ ../sos-code-article9.5/userland/myprog13.c	2006-01-22 12:48:32.000000000 +0100
@@ -27,7 +27,7 @@
  * Test brk (and, hence: mremap)
  */
 
-int main()
+int main(void)
 {
   char * zoup;
 
diff -ruNwb /tmp/sos-code-article9/userland/myprog14.c ../sos-code-article9.5/userland/myprog14.c
--- /tmp/sos-code-article9/userland/myprog14.c	2005-12-28 11:44:58.000000000 +0100
+++ ../sos-code-article9.5/userland/myprog14.c	2006-01-22 12:48:32.000000000 +0100
@@ -35,7 +35,7 @@
  * call user_memcpy and do some page faults.
  */
 
-int main()
+int main(void)
 {
   char * zoup;
   int fd;
diff -ruNwb /tmp/sos-code-article9/userland/myprog1.c ../sos-code-article9.5/userland/myprog1.c
--- /tmp/sos-code-article9/userland/myprog1.c	2005-12-28 11:44:58.000000000 +0100
+++ ../sos-code-article9.5/userland/myprog1.c	2006-01-22 12:48:32.000000000 +0100
@@ -23,7 +23,7 @@
  * Basic print/exit test
  */
 
-int main()
+int main(void)
 {
   int retval;
 
diff -ruNwb /tmp/sos-code-article9/userland/myprog2.c ../sos-code-article9.5/userland/myprog2.c
--- /tmp/sos-code-article9/userland/myprog2.c	2005-12-28 11:44:58.000000000 +0100
+++ ../sos-code-article9.5/userland/myprog2.c	2006-01-22 12:48:32.000000000 +0100
@@ -23,7 +23,7 @@
  * Basic print/pause/exit test
  */
 
-int main()
+int main(void)
 {
   int i;
   bochs_printf("Prog2: Hello world A !\n");
diff -ruNwb /tmp/sos-code-article9/userland/myprog3.c ../sos-code-article9.5/userland/myprog3.c
--- /tmp/sos-code-article9/userland/myprog3.c	2005-12-28 11:44:58.000000000 +0100
+++ ../sos-code-article9.5/userland/myprog3.c	2006-01-22 12:48:32.000000000 +0100
@@ -23,7 +23,7 @@
  * Test unresolved page fault (write to kernel space)
  */
 
-int main()
+int main(void)
 {
   char *c;
   int i;
diff -ruNwb /tmp/sos-code-article9/userland/myprog4.c ../sos-code-article9.5/userland/myprog4.c
--- /tmp/sos-code-article9/userland/myprog4.c	2005-12-28 11:44:58.000000000 +0100
+++ ../sos-code-article9.5/userland/myprog4.c	2006-01-22 12:48:32.000000000 +0100
@@ -23,7 +23,7 @@
  * Test unresolved page fault (write to unmapped page in user space)
  */
 
-int main()
+int main(void)
 {
   char *c;
   int i;
diff -ruNwb /tmp/sos-code-article9/userland/myprog5.c ../sos-code-article9.5/userland/myprog5.c
--- /tmp/sos-code-article9/userland/myprog5.c	2005-12-28 11:44:58.000000000 +0100
+++ ../sos-code-article9.5/userland/myprog5.c	2006-01-22 12:48:32.000000000 +0100
@@ -23,7 +23,7 @@
  * Test inifite loop (preemptive scheduling of threads in user mode)
  */
 
-int main()
+int main(void)
 {
   int retval;
 
diff -ruNwb /tmp/sos-code-article9/userland/myprog6.c ../sos-code-article9.5/userland/myprog6.c
--- /tmp/sos-code-article9/userland/myprog6.c	2005-12-28 11:44:58.000000000 +0100
+++ ../sos-code-article9.5/userland/myprog6.c	2006-01-22 12:48:32.000000000 +0100
@@ -23,7 +23,7 @@
  * Test execution of (forbiden) supervisor instructions
  */
 
-int main()
+int main(void)
 {
   int i;
   int retval = 0;
diff -ruNwb /tmp/sos-code-article9/userland/myprog7.c ../sos-code-article9.5/userland/myprog7.c
--- /tmp/sos-code-article9/userland/myprog7.c	2005-12-28 11:44:58.000000000 +0100
+++ ../sos-code-article9.5/userland/myprog7.c	2006-01-22 12:48:32.000000000 +0100
@@ -38,7 +38,7 @@
 }
 
 
-int main()
+int main(void)
 {
   int i, level;
   
diff -ruNwb /tmp/sos-code-article9/userland/myprog8.c ../sos-code-article9.5/userland/myprog8.c
--- /tmp/sos-code-article9/userland/myprog8.c	2005-12-28 11:44:58.000000000 +0100
+++ ../sos-code-article9.5/userland/myprog8.c	2006-01-22 12:48:32.000000000 +0100
@@ -27,7 +27,7 @@
  * really shared between father and child
  */
 
-int main()
+int main(void)
 {
   int i;
   char *zoup;
diff -ruNwb /tmp/sos-code-article9/userland/myprog9.c ../sos-code-article9.5/userland/myprog9.c
--- /tmp/sos-code-article9/userland/myprog9.c	2005-12-28 11:44:58.000000000 +0100
+++ ../sos-code-article9.5/userland/myprog9.c	2006-01-22 12:48:32.000000000 +0100
@@ -30,7 +30,7 @@
  * thread's address space
  */
 
-int main()
+int main(void)
 {
   char *zoup;
   int fd;
diff -ruNwb /tmp/sos-code-article9/userland/shell.c ../sos-code-article9.5/userland/shell.c
--- /tmp/sos-code-article9/userland/shell.c	2005-12-28 11:44:58.000000000 +0100
+++ ../sos-code-article9.5/userland/shell.c	2006-01-22 12:48:31.000000000 +0100
@@ -1,4 +1,4 @@
-/* Copyright (C) 2005 David Decotigny, Thomas Petazzoni
+/* Copyright (C) 2005 Thomas Petazzoni, David Decotigny
 
    This program is free software; you can redistribute it and/or
    modify it under the terms of the GNU General Public License
@@ -33,7 +33,7 @@
 
 static int shell_uname (int argc, char *argv[])
 {
-  printf ("SOS article 9\n");
+  printf ("SOS article 9.5\n");
   return 0;
 }
 
@@ -72,6 +72,7 @@
 	case S_IFDIR: entrychar='d'; entrysuffix="/"; break;
 	case S_IFLNK: entrychar='l'; entrysuffix="@"; break;
 	case S_IFCHR: entrychar='c'; entrysuffix=NULL; break;
+	case S_IFBLK: entrychar='b'; entrysuffix=NULL; break;
 	default: entrychar='?'; entrysuffix="?!?"; break;
 	}
 
@@ -98,7 +99,7 @@
 		  close(fd);
 		}
 	    }
-	  else if (stat.st_type == S_IFCHR)
+	  else if ( (stat.st_type == S_IFCHR) || (stat.st_type == S_IFBLK) )
 	    {
 	      snprintf(majorminor, sizeof(majorminor), " %d,%d",
 		       stat.st_rdev_major, stat.st_rdev_minor);
@@ -286,25 +287,317 @@
 }
 
 
-static int shell_devtest (int argc, char *argv[])
+static int shell_spawn (int argc, char *argv[])
 {
-  bochs_printf("WARNING: This test will eventually write 0 on kernel code !\n");
-  bochs_printf("This WILL crash the kernel (as expected...) !\n");
-  printf("WARNING: This test will eventually write 0 on kernel code !\n");
-  printf("This WILL crash the kernel (as expected...) !\n");
-  if (fork() == 0)
-    exec("devtest");
+  int retval;
+  if (argc < 2)
+    {
+      printf("Usage: spawn prog_name\n");
+      return -1;
+    }
+  retval = fork();
+  if (retval > 0)
+    return 0; /* father returns */
+  if (retval < 0)
+    {
+      printf("Could not fork (%d)\n", retval);
+      return -1;
+    }
+  retval = exec(argv[1]);
+  printf("Could not exec %s: error %d\n", argv[1], retval);
+  return 0;
+}
+
+
+static int shell_rm (int argc, char *argv[])
+{
+  int i;
+
+  for (i = 1; i < argc; i++)
+    {
+      int ret;
+
+      ret = unlink (argv[i]);
+      if (ret < 0)
+	printf ("Cannot remove '%s', %d\n", argv[i], ret);
+    }
+
   return 0;
 }
 
 
-static int shell_fstest (int argc, char *argv[])
+static int shell_dd (int argc, char *argv[])
+{
+  char *infile = NULL;
+  char *outfile = NULL;
+  long bs = 1;
+  long count = -1;
+  int i;
+  int infd, outfd;
+  char *buffer;
+  int ret;
+
+  for (i = 1; i < argc; i++)
+    {
+      if (strncmp (argv[i], "if=", 3) == 0)
+	infile = argv[i] + 3;
+      else if (strncmp (argv[i], "of=", 3) == 0)
+	outfile = argv[i] + 3;
+      else if (strncmp (argv[i], "bs=", 3) == 0)
+	{
+	  char *tmp = argv[i] + 3;
+	  char *multiplier_char;
+	  int multiplier = 1;
+	  int j;
+
+	  multiplier_char = & tmp[strlen(tmp)-1];
+	  if (*multiplier_char == 'k' ||
+	      *multiplier_char == 'K')
+	    {
+	      multiplier = 1024;
+	      *multiplier_char = '\0';
+	    }
+	  else if (*multiplier_char == 'm' ||
+		   *multiplier_char == 'M')
+	    {
+	      multiplier = 1024 * 1024;
+	      *multiplier_char = '\0';
+	    }
+
+	  for (j = 0; j < strlen(tmp); j++)
+	    if (! isdigit(tmp[j]))
+	      {
+		printf ("Syntax error in block size\n");
+		return -1;
+	      }
+
+	  bs = atol(tmp) * multiplier;
+	}
+      else if (strncmp (argv[i], "count=", 6) == 0)
+	{
+	  char *tmp = argv[i] + 6;
+	  int j;
+
+	  for (j = 0; j < strlen(tmp); j++)
+	    if (! isdigit(tmp[j]))
+	      {
+		printf ("Syntax error in block count\n");
+		return -1;
+	      }
+
+	  count = atol(tmp);
+	}
+      else
+	{
+	  printf ("Syntax error\n");
+	  return -1;
+	}
+    }
+
+  infd = open(infile, O_RDONLY);
+  if (infd < 0)
+    {
+      printf ("Cannot open '%s', %d\n", infile, infd);
+      return infd;
+    }
+
+  outfd = open(outfile, O_RDWR | O_CREAT | O_TRUNC, S_IRUSR | S_IWUSR);
+  if (outfd < 0)
+    {
+      printf ("Cannot open '%s', %d\n", outfile, outfd);
+      return outfd;
+    }
+
+  buffer = malloc (bs);
+  if (! buffer)
+    return -1;
+
+  for (i = 0; (count < 0) || (i < count) ; i++)
 {
-  if (fork() == 0)
-    exec("fstest");
+      int len;
+
+      ret = read (infd, buffer, bs);
+      if (ret < 0)
+	{
+	  printf ("Input error from '%s' at %d\n", infile, i);
+	  return -1;
+	}
+      if (ret == 0)
+	break;
+
+      len = ret;
+
+      ret = write (outfd, buffer, len);
+      if (ret != len)
+	{
+	  printf ("Output error to '%s' at %d (%d)\n", outfile, i, ret);
+	  return -1;
+	}
+    }
+
   return 0;
 }
 
+
+static int shell_partdump (int argc, char *argv[])
+{
+  /**
+   * This structure defines the structure of a partition entry
+   * (determined by the PC architecture)
+   */
+  struct partition_entry
+  {
+    unsigned char  active;
+    unsigned char  start_dl;
+    unsigned short start_cylinder;
+    unsigned char  type;
+    unsigned char  end_dl;
+    unsigned short end_cylinder;
+    unsigned long  lba;
+    unsigned long  size;
+  };
+
+  /**
+   * Offset of the partition table inside the 512-byte sector.
+   */
+#define PART_TABLE_OFFSET 446
+
+  /**
+   * The most common partition types. For a complete list, you can for
+   * example refer to
+   * http://www.win.tue.nl/~aeb/partitions/partition_types-1.html
+   */
+#define PART_TYPE_EXTENDED   0x5
+#define PART_TYPE_FAT16_1    0xe
+#define PART_TYPE_FAT16_2    0x6
+#define PART_TYPE_FAT32_1    0xb
+#define PART_TYPE_FAT32_2    0xc
+#define PART_TYPE_LINUX_SWAP 0x82
+#define PART_TYPE_LINUX      0x83
+
+  /**
+   * Converts a partition type to a string
+   */
+  const char *
+    sos_part_type_str (unsigned int type)
+    {
+      switch (type)
+	{
+	case PART_TYPE_EXTENDED:
+	  return "Extended";
+	case PART_TYPE_FAT16_1:
+	case PART_TYPE_FAT16_2:
+	  return "FAT16";
+	case PART_TYPE_FAT32_1:
+	case PART_TYPE_FAT32_2:
+	  return "FAT32";
+	case PART_TYPE_LINUX_SWAP:
+	  return "Linux Swap";
+	case PART_TYPE_LINUX:
+	  return "Linux";
+	default:
+	  return "Unknown";
+	}
+    }
+
+  int fd;
+  /* We assume the block size is 512 bytes. The clean way would be to
+     have a BLKGETSIZE ioctl() on block devices */
+#define BLKSIZE 512
+  char buffer[BLKSIZE];
+  int ret;
+  struct partition_entry *part_entry;
+  int partnum;
+  unsigned int extstart = 0, extsup = 0;
+
+  if (argc != 2)
+    {
+      printf ("Usage: partdump /dev/device\n");
+      return -1;
+    }
+
+  fd = open (argv[1], O_RDONLY);
+  if (fd < 0)
+    {
+      printf ("Cannot open '%s', %d\n", argv[1], fd);
+      return -1;
+    }
+
+  ret = read (fd, buffer, sizeof(buffer));
+  if (ret != sizeof(buffer))
+    {
+      printf ("Read error in '%s', %d\n", argv[1], fd);
+      return -1;
+    }
+
+  part_entry = (struct partition_entry *) (buffer + PART_TABLE_OFFSET);
+
+  printf ("Partitions in %s:\n", argv[1]);
+
+  /* Handle primary partitions */
+  for (partnum = 0; partnum < 4; partnum++)
+    {
+      if (part_entry [partnum].size == 0)
+	continue;
+
+      if (part_entry [partnum].type == PART_TYPE_EXTENDED)
+	{
+	  if (extstart != 0)
+	    printf ("Warning: two extended partitions detected\n");
+	  extstart = part_entry [partnum].lba;
+	  continue;
+	}
+
+      /* When size is >= 1 Mb, display size in Mb, otherwise display
+	 size in Kb */
+      if (part_entry[partnum].size * BLKSIZE >= (1024*1024))
+	printf (" - %s%d (primary, %lu Mb, %s)\n", argv[1], partnum+1,
+		(part_entry[partnum].size * BLKSIZE >> 20),
+		sos_part_type_str(part_entry[partnum].type));
+      else
+	printf (" - %s%d (primary, %lu Kb, %s)\n", argv[1], partnum+1,
+		(part_entry[partnum].size * BLKSIZE >> 10),
+		sos_part_type_str(part_entry[partnum].type));
+    }
+
+  while (extstart != 0)
+    {
+      ret = lseek (fd, (extstart + extsup) * BLKSIZE, SEEK_SET);
+      if (ret != (extstart + extsup) * BLKSIZE)
+	{
+	  printf ("Cannot seek at position %d in '%s', %d\n",
+		  (extstart + extsup) * BLKSIZE, argv[1], ret);
+	  return -1;
+	}
+
+      ret = read (fd, buffer, BLKSIZE);
+      if (ret != BLKSIZE)
+	{
+	  printf ("Read error in '%s', %d\n", argv[1], ret);
+	  break;
+	}
+
+      /* When size is >= 1 Mb, display size in Mb, otherwise display
+	 size in Kb */
+      if (part_entry[0].size * BLKSIZE >= (1024*1024))
+	printf (" - %s%d (logical volume, %lu Mb, %s)\n", argv[1], partnum+1,
+		(part_entry[0].size * BLKSIZE >> 20),
+		sos_part_type_str(part_entry[0].type));
+      else
+	printf (" - %s%d (logical volume, %lu Kb, %s)\n", argv[1], partnum+1,
+		(part_entry[0].size * BLKSIZE >> 10),
+		sos_part_type_str(part_entry[0].type));
+
+      extsup = part_entry[1].lba;
+      partnum ++;
+      if (extsup == 0)
+	break;
+    }
+
+  return 0;
+}
+
+
 void command_exec (char * cmd)
 {
   char *c;
@@ -367,15 +660,22 @@
     {
       shell_test (argc, argv);
     }
-  else if (! strcmp (argv[0], "devtest"))
+  else if (! strcmp (argv[0], "spawn"))
     {
-      shell_devtest (argc, argv);
+      shell_spawn (argc, argv);
     }
-  else if (! strcmp (argv[0], "fstest"))
+  else if (! strcmp (argv[0], "dd"))
     {
-      shell_fstest (argc, argv);
+      shell_dd (argc, argv);
+    }
+  else if (! strcmp (argv[0], "rm"))
+    {
+      shell_rm (argc, argv);
+    }
+  else if (! strcmp (argv[0], "partdump"))
+    {
+      shell_partdump (argc, argv);
     }
-
 
   else
     printf ("Command not found\n");
@@ -383,7 +683,7 @@
   free (argv);
 }
 
-int main()
+int main(void)
 {
   char buffer[256];
   int i;
diff -ruNwb /tmp/sos-code-article9/userland/string.c ../sos-code-article9.5/userland/string.c
--- /tmp/sos-code-article9/userland/string.c	2005-12-28 11:44:58.000000000 +0100
+++ ../sos-code-article9.5/userland/string.c	2006-01-22 12:48:32.000000000 +0100
@@ -142,3 +142,71 @@
 }
 
 
+long long int strtoll(const char *nptr, char **endptr, int base)
+{
+  long long int result;
+
+#define RETURN(res) ({ if (endptr) *endptr = (char*)nptr; return res; })
+
+  if (base > 36)
+    {
+      RETURN(0);
+    }
+
+  /* Skip leading  spaces */
+  for ( ; *nptr != '\0' && isspace(*nptr) ; nptr++)
+    continue;
+
+  if (nptr[0] == '0')
+    {
+      if (nptr[1] == 'x')
+	{
+	  base = 16;
+	  nptr += 2;
+	}
+      else
+	{
+	  base = 8;
+	  nptr ++;
+	}
+    }
+
+  result = 0;
+  for ( ; *nptr != '\0' ; nptr ++)
+    {
+      int digit;
+      if (*nptr >= '0' && *nptr <=  '9')
+	digit = *nptr - '0';
+      else if (*nptr >= 'a' && *nptr <=  'z')
+	digit = 10 + *nptr - 'a';
+      else if (*nptr >= 'A' && *nptr <=  'Z')
+	digit = 10 + *nptr - 'A';
+      else
+	RETURN(result);
+      if (digit >= base)
+	{
+	  RETURN(result);
+	}
+      result *= base;
+      result += digit;
+    }
+
+  RETURN(result);
+}
+
+long int strtol(const char *nptr, char **endptr, int base)
+{
+  return strtoll(nptr, endptr, base);
+}
+
+long long atoll(const char *nptr)
+{
+  return strtoll(nptr, NULL, 10);
+}
+
+long atol(const char *nptr)
+{
+  return strtol(nptr, NULL, 10);
+}
+
+
diff -ruNwb /tmp/sos-code-article9/userland/string.h ../sos-code-article9.5/userland/string.h
--- /tmp/sos-code-article9/userland/string.h	2005-12-28 11:44:58.000000000 +0100
+++ ../sos-code-article9.5/userland/string.h	2006-01-22 12:48:32.000000000 +0100
@@ -59,4 +59,18 @@
 	    register int len );
 
 
+#define islower(c)  (('a' <= (c)) && ((c) <= 'z'))
+#define isupper(c)  (('A' <= (c)) && ((c) <= 'Z'))
+#define isdigit(c)  (('0' <= (c)) && ((c) <= '9'))
+#define isspace(c)  (((c) == ' ') || ((c) == '\t') || \
+                    ((c) == '\f') || ((c) == '\n') || \
+                    ((c) == '\r') || ((c) == '\v'))
+#define isprint(c)  ((' ' <= (c)) && ((c) <= '~'))
+
+long long int strtoll(const char *nptr, char **endptr, int base);
+long int strtol(const char *nptr, char **endptr, int base);
+long long atoll(const char *nptr);
+long atol(const char *nptr);
+
+
 #endif /* _SOS_LIBC_STRING_H_ */
diff -ruNwb /tmp/sos-code-article9/VERSION ../sos-code-article9.5/VERSION
--- /tmp/sos-code-article9/VERSION	2005-12-28 11:44:58.000000000 +0100
+++ ../sos-code-article9.5/VERSION	2006-01-22 12:48:32.000000000 +0100
@@ -1,10 +1,10 @@
 SOS -- Simple OS
 Copyright (C) 2003,2004,2005 The SOS Team (David Decotigny & Thomas Petazzoni)
 
-Version "Article 9 (1st part)" -- User interactions with the devices drivers
-                                  through the VFS: character devices, ttys,
-                                  console and serial drivers, /dev/mem and
-                                  /dev/zero devices
+Version "Article 9 (2nd part)" -- User interactions with the devices drivers
+                                  through the VFS: block devices, block cache,
+                                  page cache and IDE driver with PC-style
+                                  partitions support
 
    This program is free software; you can redistribute it and/or
    modify it under the terms of the GNU General Public License
