kern

diff src/fs.c @ 90:7ff2b4971216

started work on the filesystem
author John Tsiombikas <nuclear@member.fsf.org>
date Fri, 09 Dec 2011 13:44:15 +0200
parents 2f555c81ae67
children 083849df660b
line diff
     1.1 --- a/src/fs.c	Thu Dec 08 18:19:35 2011 +0200
     1.2 +++ b/src/fs.c	Fri Dec 09 13:44:15 2011 +0200
     1.3 @@ -1,48 +1,61 @@
     1.4 +/* This code is used by the kernel AND by userspace filesystem-related tools.
     1.5 + * The kernel-specific parts are conditionally compiled in #ifdef KERNEL blocks
     1.6 + * the rest of the code should be independent.
     1.7 + */
     1.8  #include <stdio.h>
     1.9  #include <stdlib.h>
    1.10  #include <assert.h>
    1.11 +#include <errno.h>
    1.12  #include "fs.h"
    1.13 +#include "part.h"
    1.14 +
    1.15 +#ifdef KERNEL
    1.16  #include "ata.h"
    1.17 -#include "part.h"
    1.18  #include "panic.h"
    1.19 -
    1.20 -#define MAGIC	0xccf5ccf5
    1.21 -#define BLKSZ	1024
    1.22 -
    1.23 -typedef uint32_t blkid;
    1.24 -
    1.25 -struct superblock {
    1.26 -	uint32_t magic;
    1.27 -
    1.28 -	blkid istart;
    1.29 -	unsigned int icount;
    1.30 -
    1.31 -	blkid dstart;
    1.32 -	unsigned int dcount;
    1.33 -};
    1.34 +#endif
    1.35  
    1.36  struct filesys {
    1.37  	int dev;
    1.38  	struct partition part;
    1.39  
    1.40  	struct superblock *sb;
    1.41 +
    1.42 +	struct filesys *next;
    1.43  };
    1.44  
    1.45  static int find_rootfs(struct filesys *fs);
    1.46 +static int readblock(int dev, uint32_t blk, void *buf);
    1.47 +static int writeblock(int dev, uint32_t blk, void *buf);
    1.48  
    1.49  /* root device & partition */
    1.50 -static struct filesys root;
    1.51 +static struct filesys *fslist;
    1.52 +
    1.53 +int sys_mount(char *mtpt, char *devname, unsigned int flags)
    1.54 +{
    1.55 +	if(strcmp(mtpt, "/") != 0) {
    1.56 +		printf("mount: only root can be mounted at the moment\n");
    1.57 +		return -EBUG;
    1.58 +	}
    1.59 +
    1.60 +	/* mounting root filesystem */
    1.61 +	if(fslist) {
    1.62 +
    1.63 +}
    1.64  
    1.65  void init_fs(void)
    1.66  {
    1.67  	root.sb = malloc(512);
    1.68  	assert(root.sb);
    1.69  
    1.70 +#ifdef KERNEL
    1.71  	if(find_rootfs(&root) == -1) {
    1.72  		panic("can't find root filesystem\n");
    1.73  	}
    1.74 +#endif
    1.75  }
    1.76  
    1.77 +
    1.78 +#ifdef KERNEL
    1.79  #define PART_TYPE	0xcc
    1.80  static int find_rootfs(struct filesys *fs)
    1.81  {
    1.82 @@ -59,7 +72,7 @@
    1.83  				/* found the correct partition, now read the superblock
    1.84  				 * and make sure it's got the correct magic id
    1.85  				 */
    1.86 -				ata_read_pio(i, p->start_sect + 2, fs->sb);
    1.87 +				readblock(i, p->start_sect / 2 + 1, fs->sb);
    1.88  
    1.89  				if(fs->sb->magic == MAGIC) {
    1.90  					printf("found root ata%dp%d\n", i, partid);
    1.91 @@ -75,3 +88,33 @@
    1.92  	}
    1.93  	return -1;
    1.94  }
    1.95 +
    1.96 +#define NSECT	(BLKSZ / 512)
    1.97 +
    1.98 +static int readblock(struct block_device *bdev, uint32_t blk, void *buf)
    1.99 +{
   1.100 +	return blk_read(bdev, blk, buf);
   1.101 +}
   1.102 +
   1.103 +static int writeblock(struct block_device *bdev, uint32_t blk, void *buf)
   1.104 +{
   1.105 +	return blk_write(bdev, blk, buf);
   1.106 +}
   1.107 +#else
   1.108 +
   1.109 +/* if this is compiled as part of the user-space tools instead of the kernel
   1.110 + * forward the call to a user read/write block function supplied by the app.
   1.111 + */
   1.112 +int user_readblock(uint32_t, void*);
   1.113 +int user_writeblock(uint32_t, void*);
   1.114 +
   1.115 +static int readblock(struct block_device *bdev, uint32_t blk, void *buf)
   1.116 +{
   1.117 +	return user_readblock(blk, buf);
   1.118 +}
   1.119 +
   1.120 +static int writeblock(struct block_device *bdev, uint32_t blk, void *buf)
   1.121 +{
   1.122 +	return user_writeblock(blk, buf);
   1.123 +}
   1.124 +#endif	/* KERNEL */