kern

diff src/fs_sys.c @ 93:083849df660b

split the system call implementations out of fs.c into fs_sys.c
author John Tsiombikas <nuclear@member.fsf.org>
date Sun, 11 Dec 2011 10:17:58 +0200
parents
children
line diff
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/src/fs_sys.c	Sun Dec 11 10:17:58 2011 +0200
     1.3 @@ -0,0 +1,105 @@
     1.4 +/* implementation of the filesystem-related syscalls */
     1.5 +
     1.6 +#include <stdio.h>
     1.7 +#include <stdlib.h>
     1.8 +#include <string.h>
     1.9 +#include <assert.h>
    1.10 +#include <errno.h>
    1.11 +#include "fs.h"
    1.12 +#include "part.h"
    1.13 +#include "panic.h"
    1.14 +#include "bdev.h"
    1.15 +
    1.16 +static dev_t find_rootfs(void);
    1.17 +
    1.18 +/* list of mounted filesystems
    1.19 + * XXX currently only one, the root filesystem
    1.20 + */
    1.21 +static struct filesys *fslist;
    1.22 +
    1.23 +
    1.24 +int sys_mount(char *mtpt, char *devname, unsigned int flags)
    1.25 +{
    1.26 +	dev_t dev;
    1.27 +	int err;
    1.28 +	struct filesys *fs;
    1.29 +
    1.30 +	if(strcmp(mtpt, "/") != 0) {
    1.31 +		printf("only root can be mounted at the moment\n");
    1.32 +		return -EBUG;
    1.33 +	}
    1.34 +
    1.35 +	/* mounting root filesystem */
    1.36 +	if(fslist) {
    1.37 +		printf("root already mounted\n");
    1.38 +		return -EBUSY;
    1.39 +	}
    1.40 +
    1.41 +	if(devname) {
    1.42 +		dev = bdev_by_name(devname);
    1.43 +	} else {
    1.44 +		/* try to autodetect it */
    1.45 +		dev = find_rootfs();
    1.46 +	}
    1.47 +	if(!dev) {
    1.48 +		err = -ENOENT;
    1.49 +		goto rootfail;
    1.50 +	}
    1.51 +
    1.52 +	if(!(fs = malloc(sizeof *fslist))) {
    1.53 +		err = -ENOMEM;
    1.54 +		goto rootfail;
    1.55 +	}
    1.56 +	if((err = openfs(fs, dev)) != 0) {
    1.57 +		free(fs);
    1.58 +		goto rootfail;
    1.59 +	}
    1.60 +
    1.61 +	fslist = fs;
    1.62 +	return 0;
    1.63 +
    1.64 +rootfail:
    1.65 +	panic("failed to mount root filesystem: %d\n", -err);
    1.66 +	return err;	/* unreachable */
    1.67 +}
    1.68 +
    1.69 +#define PART_TYPE	0xcc
    1.70 +static dev_t find_rootfs(void)
    1.71 +{
    1.72 +	dev_t dev = 0;
    1.73 +	int i, num_dev, partid;
    1.74 +	struct partition *plist, *p;
    1.75 +	struct superblock *sb = malloc(BLKSZ);
    1.76 +	char name[16];
    1.77 +
    1.78 +	assert(sb);
    1.79 +
    1.80 +	num_dev = ata_num_devices();
    1.81 +	for(i=0; i<num_dev; i++) {
    1.82 +		plist = p = get_part_list(i);
    1.83 +
    1.84 +		partid = 0;
    1.85 +		while(p) {
    1.86 +			if(get_part_type(p) == PART_TYPE) {
    1.87 +				/* found the correct partition, now read the superblock
    1.88 +				 * and make sure it's got the correct magic id
    1.89 +				 */
    1.90 +				blk_read(i, p->start_sect / 2 + 1, BLKSZ, sb);
    1.91 +
    1.92 +				if(sb->magic == MAGIC) {
    1.93 +					sprintf(name, "ata%dp%d", i, partid);
    1.94 +					printf("found root: %s\n", name);
    1.95 +					dev = bdev_by_name(name);
    1.96 +					break;
    1.97 +				}
    1.98 +			}
    1.99 +			p = p->next;
   1.100 +			partid++;
   1.101 +		}
   1.102 +		free_part_list(plist);
   1.103 +		if(dev) break;
   1.104 +	}
   1.105 +
   1.106 +	free(sb);
   1.107 +	return dev;
   1.108 +}