kern

diff fstools/mkfs/mkfs.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
children 083849df660b
line diff
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/fstools/mkfs/mkfs.c	Fri Dec 09 13:44:15 2011 +0200
     1.3 @@ -0,0 +1,147 @@
     1.4 +#include <stdio.h>
     1.5 +#include <stdlib.h>
     1.6 +#include <string.h>
     1.7 +#include <errno.h>
     1.8 +#include <inttypes.h>
     1.9 +#include <unistd.h>
    1.10 +#include <fcntl.h>
    1.11 +#include <sys/ioctl.h>
    1.12 +#include <sys/stat.h>
    1.13 +#ifdef __linux__
    1.14 +#include <linux/fs.h>
    1.15 +#endif
    1.16 +#ifdef __darwin__
    1.17 +#include <dev/disk.h>
    1.18 +#endif
    1.19 +#include "fs.h"
    1.20 +
    1.21 +int mkfs(int fd, int blksize, uint32_t nblocks);
    1.22 +uint32_t get_block_count(int fd, int blksize);
    1.23 +int user_readblock(int dev, uint32_t blk, void *buf);
    1.24 +int user_writeblock(int dev, uint32_t blk, void *buf);
    1.25 +int parse_args(int argc, char **argv);
    1.26 +
    1.27 +int fd;
    1.28 +uint32_t num_blocks;
    1.29 +
    1.30 +int main(int argc, char **argv)
    1.31 +{
    1.32 +	if(parse_args(argc, argv) == -1) {
    1.33 +		return 1;
    1.34 +	}
    1.35 +
    1.36 +	if((num_blocks = get_block_count(fd, BLKSZ)) == 0) {
    1.37 +		fprintf(stderr, "could not determine the number of blocks\n");
    1.38 +		return 1;
    1.39 +	}
    1.40 +	printf("total blocks: %u\n", (unsigned int)num_blocks);
    1.41 +
    1.42 +	if(mkfs(fd, num_blocks) == -1) {
    1.43 +		return 1;
    1.44 +	}
    1.45 +
    1.46 +	return 0;
    1.47 +}
    1.48 +
    1.49 +int mkfs(int fd, int blksize, uint32_t nblocks)
    1.50 +{
    1.51 +	struct superblock *sb;
    1.52 +
    1.53 +	if(!(sb = malloc(BLKSZ))) {
    1.54 +		perror("failed to allocate memory");
    1.55 +		return -1;
    1.56 +	}
    1.57 +}
    1.58 +
    1.59 +uint32_t get_block_count(int fd, int blksize)
    1.60 +{
    1.61 +	unsigned long sz = 0;
    1.62 +	uint64_t sz64 = 0;
    1.63 +	struct stat st;
    1.64 +
    1.65 +#ifdef BLKGETSIZE64
    1.66 +	if(ioctl(fd, BLKGETSIZE64, &sz64) != -1) {
    1.67 +		return sz64 / blksize;
    1.68 +	}
    1.69 +#endif
    1.70 +
    1.71 +#ifdef BLKGETSIZE
    1.72 +	if(ioctl(fd, BLKGETSIZE, &sz) != -1) {
    1.73 +		return sz / (blksize / 512);
    1.74 +	}
    1.75 +#endif
    1.76 +
    1.77 +#ifdef DKIOCGETBLOCKCOUNT
    1.78 +	if(ioctl(fd, DKIOCGETBLOCKCOUNT, &sz64) != -1) {
    1.79 +		return sz64 / (blksize / 512);
    1.80 +	}
    1.81 +#endif
    1.82 +
    1.83 +	if(fstat(fd, &st) != -1 && S_ISREG(st.st_mode)) {
    1.84 +		return st.st_size / blksize;
    1.85 +	}
    1.86 +
    1.87 +	return 0;
    1.88 +}
    1.89 +
    1.90 +int user_readblock(int dev, uint32_t blk, void *buf)
    1.91 +{
    1.92 +	if(lseek(fd, blk * BLKSZ, SEEK_SET) == -1) {
    1.93 +		return -1;
    1.94 +	}
    1.95 +	if(read(fd, buf, BLKSZ) < BLKSZ) {
    1.96 +		return -1;
    1.97 +	}
    1.98 +	return 0;
    1.99 +}
   1.100 +
   1.101 +int user_writeblock(int dev, uint32_t blk, void *buf)
   1.102 +{
   1.103 +	if(lseek(fd, blk * BLKSZ, SEEK_SET) == -1) {
   1.104 +		return -1;
   1.105 +	}
   1.106 +	if(write(fd, buf, BLKSZ) < BLKSZ) {
   1.107 +		return -1;
   1.108 +	}
   1.109 +	return 0;
   1.110 +}
   1.111 +
   1.112 +int parse_args(int argc, char **argv)
   1.113 +{
   1.114 +	int i;
   1.115 +
   1.116 +	fd = -1;
   1.117 +
   1.118 +	for(i=1; i<argc; i++) {
   1.119 +		if(argv[i][0] == '-' && argv[i][2] == 0) {
   1.120 +			switch(argv[i][1]) {
   1.121 +			case 'h':
   1.122 +				printf("usage: %s <device file>\n", argv[0]);
   1.123 +				exit(0);
   1.124 +
   1.125 +			default:
   1.126 +				goto invalid;
   1.127 +			}
   1.128 +		} else {
   1.129 +			if(fd != -1) {
   1.130 +				goto invalid;
   1.131 +			}
   1.132 +
   1.133 +			if((fd = open(argv[i], O_RDWR)) == -1) {
   1.134 +				fprintf(stderr, "failed to open %s: %s\n", argv[i], strerror(errno));
   1.135 +				return -1;
   1.136 +			}
   1.137 +		}
   1.138 +	}
   1.139 +
   1.140 +	if(fd == -1) {
   1.141 +		fprintf(stderr, "you must specify a device or image file\n");
   1.142 +		return -1;
   1.143 +	}
   1.144 +
   1.145 +	return 0;
   1.146 +
   1.147 +invalid:
   1.148 +	fprintf(stderr, "invalid argument: %s\n", argv[i]);
   1.149 +	return -1;
   1.150 +}