kern

annotate 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
rev   line source
nuclear@90 1 #include <stdio.h>
nuclear@90 2 #include <stdlib.h>
nuclear@90 3 #include <string.h>
nuclear@90 4 #include <errno.h>
nuclear@90 5 #include <inttypes.h>
nuclear@90 6 #include <unistd.h>
nuclear@90 7 #include <fcntl.h>
nuclear@90 8 #include <sys/ioctl.h>
nuclear@90 9 #include <sys/stat.h>
nuclear@90 10 #ifdef __linux__
nuclear@90 11 #include <linux/fs.h>
nuclear@90 12 #endif
nuclear@90 13 #ifdef __darwin__
nuclear@90 14 #include <dev/disk.h>
nuclear@90 15 #endif
nuclear@90 16 #include "fs.h"
nuclear@90 17
nuclear@90 18 int mkfs(int fd, int blksize, uint32_t nblocks);
nuclear@90 19 uint32_t get_block_count(int fd, int blksize);
nuclear@90 20 int user_readblock(int dev, uint32_t blk, void *buf);
nuclear@90 21 int user_writeblock(int dev, uint32_t blk, void *buf);
nuclear@90 22 int parse_args(int argc, char **argv);
nuclear@90 23
nuclear@90 24 int fd;
nuclear@90 25 uint32_t num_blocks;
nuclear@90 26
nuclear@90 27 int main(int argc, char **argv)
nuclear@90 28 {
nuclear@90 29 if(parse_args(argc, argv) == -1) {
nuclear@90 30 return 1;
nuclear@90 31 }
nuclear@90 32
nuclear@90 33 if((num_blocks = get_block_count(fd, BLKSZ)) == 0) {
nuclear@90 34 fprintf(stderr, "could not determine the number of blocks\n");
nuclear@90 35 return 1;
nuclear@90 36 }
nuclear@90 37 printf("total blocks: %u\n", (unsigned int)num_blocks);
nuclear@90 38
nuclear@90 39 if(mkfs(fd, num_blocks) == -1) {
nuclear@90 40 return 1;
nuclear@90 41 }
nuclear@90 42
nuclear@90 43 return 0;
nuclear@90 44 }
nuclear@90 45
nuclear@90 46 int mkfs(int fd, int blksize, uint32_t nblocks)
nuclear@90 47 {
nuclear@90 48 struct superblock *sb;
nuclear@90 49
nuclear@90 50 if(!(sb = malloc(BLKSZ))) {
nuclear@90 51 perror("failed to allocate memory");
nuclear@90 52 return -1;
nuclear@90 53 }
nuclear@90 54 }
nuclear@90 55
nuclear@90 56 uint32_t get_block_count(int fd, int blksize)
nuclear@90 57 {
nuclear@90 58 unsigned long sz = 0;
nuclear@90 59 uint64_t sz64 = 0;
nuclear@90 60 struct stat st;
nuclear@90 61
nuclear@90 62 #ifdef BLKGETSIZE64
nuclear@90 63 if(ioctl(fd, BLKGETSIZE64, &sz64) != -1) {
nuclear@90 64 return sz64 / blksize;
nuclear@90 65 }
nuclear@90 66 #endif
nuclear@90 67
nuclear@90 68 #ifdef BLKGETSIZE
nuclear@90 69 if(ioctl(fd, BLKGETSIZE, &sz) != -1) {
nuclear@90 70 return sz / (blksize / 512);
nuclear@90 71 }
nuclear@90 72 #endif
nuclear@90 73
nuclear@90 74 #ifdef DKIOCGETBLOCKCOUNT
nuclear@90 75 if(ioctl(fd, DKIOCGETBLOCKCOUNT, &sz64) != -1) {
nuclear@90 76 return sz64 / (blksize / 512);
nuclear@90 77 }
nuclear@90 78 #endif
nuclear@90 79
nuclear@90 80 if(fstat(fd, &st) != -1 && S_ISREG(st.st_mode)) {
nuclear@90 81 return st.st_size / blksize;
nuclear@90 82 }
nuclear@90 83
nuclear@90 84 return 0;
nuclear@90 85 }
nuclear@90 86
nuclear@90 87 int user_readblock(int dev, uint32_t blk, void *buf)
nuclear@90 88 {
nuclear@90 89 if(lseek(fd, blk * BLKSZ, SEEK_SET) == -1) {
nuclear@90 90 return -1;
nuclear@90 91 }
nuclear@90 92 if(read(fd, buf, BLKSZ) < BLKSZ) {
nuclear@90 93 return -1;
nuclear@90 94 }
nuclear@90 95 return 0;
nuclear@90 96 }
nuclear@90 97
nuclear@90 98 int user_writeblock(int dev, uint32_t blk, void *buf)
nuclear@90 99 {
nuclear@90 100 if(lseek(fd, blk * BLKSZ, SEEK_SET) == -1) {
nuclear@90 101 return -1;
nuclear@90 102 }
nuclear@90 103 if(write(fd, buf, BLKSZ) < BLKSZ) {
nuclear@90 104 return -1;
nuclear@90 105 }
nuclear@90 106 return 0;
nuclear@90 107 }
nuclear@90 108
nuclear@90 109 int parse_args(int argc, char **argv)
nuclear@90 110 {
nuclear@90 111 int i;
nuclear@90 112
nuclear@90 113 fd = -1;
nuclear@90 114
nuclear@90 115 for(i=1; i<argc; i++) {
nuclear@90 116 if(argv[i][0] == '-' && argv[i][2] == 0) {
nuclear@90 117 switch(argv[i][1]) {
nuclear@90 118 case 'h':
nuclear@90 119 printf("usage: %s <device file>\n", argv[0]);
nuclear@90 120 exit(0);
nuclear@90 121
nuclear@90 122 default:
nuclear@90 123 goto invalid;
nuclear@90 124 }
nuclear@90 125 } else {
nuclear@90 126 if(fd != -1) {
nuclear@90 127 goto invalid;
nuclear@90 128 }
nuclear@90 129
nuclear@90 130 if((fd = open(argv[i], O_RDWR)) == -1) {
nuclear@90 131 fprintf(stderr, "failed to open %s: %s\n", argv[i], strerror(errno));
nuclear@90 132 return -1;
nuclear@90 133 }
nuclear@90 134 }
nuclear@90 135 }
nuclear@90 136
nuclear@90 137 if(fd == -1) {
nuclear@90 138 fprintf(stderr, "you must specify a device or image file\n");
nuclear@90 139 return -1;
nuclear@90 140 }
nuclear@90 141
nuclear@90 142 return 0;
nuclear@90 143
nuclear@90 144 invalid:
nuclear@90 145 fprintf(stderr, "invalid argument: %s\n", argv[i]);
nuclear@90 146 return -1;
nuclear@90 147 }