kern

annotate fstools/mkfs/mkfs.c @ 97:8717eb590727

ok stopping with the filesystem to manage to write the article in time
author John Tsiombikas <nuclear@member.fsf.org>
date Sat, 17 Dec 2011 14:09:17 +0200
parents 7ff2b4971216
children
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@93 50 sb = malloc(BLKSZ);
nuclear@93 51 assert(sb);
nuclear@93 52
nuclear@93 53 sb->magic = MAGIC;
nuclear@93 54 sb->ver = 0;
nuclear@93 55 sb->num_blocks = nblocks;
nuclear@90 56 }
nuclear@90 57
nuclear@90 58 uint32_t get_block_count(int fd, int blksize)
nuclear@90 59 {
nuclear@90 60 unsigned long sz = 0;
nuclear@90 61 uint64_t sz64 = 0;
nuclear@90 62 struct stat st;
nuclear@90 63
nuclear@90 64 #ifdef BLKGETSIZE64
nuclear@90 65 if(ioctl(fd, BLKGETSIZE64, &sz64) != -1) {
nuclear@90 66 return sz64 / blksize;
nuclear@90 67 }
nuclear@90 68 #endif
nuclear@90 69
nuclear@90 70 #ifdef BLKGETSIZE
nuclear@90 71 if(ioctl(fd, BLKGETSIZE, &sz) != -1) {
nuclear@90 72 return sz / (blksize / 512);
nuclear@90 73 }
nuclear@90 74 #endif
nuclear@90 75
nuclear@90 76 #ifdef DKIOCGETBLOCKCOUNT
nuclear@90 77 if(ioctl(fd, DKIOCGETBLOCKCOUNT, &sz64) != -1) {
nuclear@90 78 return sz64 / (blksize / 512);
nuclear@90 79 }
nuclear@90 80 #endif
nuclear@90 81
nuclear@90 82 if(fstat(fd, &st) != -1 && S_ISREG(st.st_mode)) {
nuclear@90 83 return st.st_size / blksize;
nuclear@90 84 }
nuclear@90 85
nuclear@90 86 return 0;
nuclear@90 87 }
nuclear@90 88
nuclear@93 89 int blk_read(void*, uint32_t blk, int count, void *buf)
nuclear@90 90 {
nuclear@90 91 if(lseek(fd, blk * BLKSZ, SEEK_SET) == -1) {
nuclear@90 92 return -1;
nuclear@90 93 }
nuclear@93 94 if(read(fd, buf, BLKSZ * count) < BLKSZ * count) {
nuclear@90 95 return -1;
nuclear@90 96 }
nuclear@90 97 return 0;
nuclear@90 98 }
nuclear@90 99
nuclear@93 100 int blk_write(void*, uint32_t blk, int count, void *buf)
nuclear@90 101 {
nuclear@90 102 if(lseek(fd, blk * BLKSZ, SEEK_SET) == -1) {
nuclear@90 103 return -1;
nuclear@90 104 }
nuclear@93 105 if(write(fd, buf, BLKSZ * count) < BLKSZ * count) {
nuclear@90 106 return -1;
nuclear@90 107 }
nuclear@90 108 return 0;
nuclear@90 109 }
nuclear@90 110
nuclear@90 111 int parse_args(int argc, char **argv)
nuclear@90 112 {
nuclear@90 113 int i;
nuclear@90 114
nuclear@90 115 fd = -1;
nuclear@90 116
nuclear@90 117 for(i=1; i<argc; i++) {
nuclear@90 118 if(argv[i][0] == '-' && argv[i][2] == 0) {
nuclear@90 119 switch(argv[i][1]) {
nuclear@90 120 case 'h':
nuclear@90 121 printf("usage: %s <device file>\n", argv[0]);
nuclear@90 122 exit(0);
nuclear@90 123
nuclear@90 124 default:
nuclear@90 125 goto invalid;
nuclear@90 126 }
nuclear@90 127 } else {
nuclear@90 128 if(fd != -1) {
nuclear@90 129 goto invalid;
nuclear@90 130 }
nuclear@90 131
nuclear@90 132 if((fd = open(argv[i], O_RDWR)) == -1) {
nuclear@90 133 fprintf(stderr, "failed to open %s: %s\n", argv[i], strerror(errno));
nuclear@90 134 return -1;
nuclear@90 135 }
nuclear@90 136 }
nuclear@90 137 }
nuclear@90 138
nuclear@90 139 if(fd == -1) {
nuclear@90 140 fprintf(stderr, "you must specify a device or image file\n");
nuclear@90 141 return -1;
nuclear@90 142 }
nuclear@90 143
nuclear@90 144 return 0;
nuclear@90 145
nuclear@90 146 invalid:
nuclear@90 147 fprintf(stderr, "invalid argument: %s\n", argv[i]);
nuclear@90 148 return -1;
nuclear@90 149 }