Home | History | Annotate | Line # | Download | only in newfs_sysvbfs
newfs_sysvbfs.c revision 1.2
      1  1.2   martin /*	$NetBSD: newfs_sysvbfs.c,v 1.2 2008/04/28 20:23:09 martin Exp $	*/
      2  1.1  tsutsui 
      3  1.1  tsutsui /*-
      4  1.1  tsutsui  * Copyright (c) 2004 The NetBSD Foundation, Inc.
      5  1.1  tsutsui  * All rights reserved.
      6  1.1  tsutsui  *
      7  1.1  tsutsui  * This code is derived from software contributed to The NetBSD Foundation
      8  1.1  tsutsui  * by UCHIYAMA Yasushi.
      9  1.1  tsutsui  *
     10  1.1  tsutsui  * Redistribution and use in source and binary forms, with or without
     11  1.1  tsutsui  * modification, are permitted provided that the following conditions
     12  1.1  tsutsui  * are met:
     13  1.1  tsutsui  * 1. Redistributions of source code must retain the above copyright
     14  1.1  tsutsui  *    notice, this list of conditions and the following disclaimer.
     15  1.1  tsutsui  * 2. Redistributions in binary form must reproduce the above copyright
     16  1.1  tsutsui  *    notice, this list of conditions and the following disclaimer in the
     17  1.1  tsutsui  *    documentation and/or other materials provided with the distribution.
     18  1.1  tsutsui  *
     19  1.1  tsutsui  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     20  1.1  tsutsui  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     21  1.1  tsutsui  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     22  1.1  tsutsui  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     23  1.1  tsutsui  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     24  1.1  tsutsui  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     25  1.1  tsutsui  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     26  1.1  tsutsui  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     27  1.1  tsutsui  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     28  1.1  tsutsui  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     29  1.1  tsutsui  * POSSIBILITY OF SUCH DAMAGE.
     30  1.1  tsutsui  */
     31  1.1  tsutsui 
     32  1.1  tsutsui #include <sys/types.h>
     33  1.1  tsutsui #include <sys/param.h>
     34  1.1  tsutsui #include <stdio.h>
     35  1.1  tsutsui #include <stdlib.h>
     36  1.1  tsutsui #include <string.h>
     37  1.1  tsutsui #include <unistd.h>
     38  1.1  tsutsui #include <fcntl.h>
     39  1.1  tsutsui #include <time.h>
     40  1.1  tsutsui #include <assert.h>
     41  1.1  tsutsui #include <sys/errno.h>
     42  1.1  tsutsui #include <sys/ioctl.h>
     43  1.1  tsutsui #include <sys/disklabel.h>
     44  1.1  tsutsui 
     45  1.1  tsutsui #include <fs/sysvbfs/bfs.h>
     46  1.1  tsutsui 
     47  1.1  tsutsui static void usage(void);
     48  1.1  tsutsui static int bfs_newfs(int, uint32_t);
     49  1.1  tsutsui 
     50  1.1  tsutsui int
     51  1.1  tsutsui main(int argc, char **argv)
     52  1.1  tsutsui {
     53  1.1  tsutsui 	const char *device;
     54  1.1  tsutsui 	struct disklabel d;
     55  1.1  tsutsui 	struct partition *p;
     56  1.1  tsutsui 	struct stat st;
     57  1.1  tsutsui 	int part;
     58  1.1  tsutsui 	int fd;
     59  1.1  tsutsui 
     60  1.1  tsutsui 	if (argc != 2)
     61  1.1  tsutsui 		usage();
     62  1.1  tsutsui 	device = argv[1];
     63  1.1  tsutsui 
     64  1.1  tsutsui 	if ((fd = open(device, O_RDWR)) == -1) {
     65  1.1  tsutsui 		perror("open device");
     66  1.1  tsutsui 		exit(EXIT_FAILURE);
     67  1.1  tsutsui 	}
     68  1.1  tsutsui 	if (fstat(fd, &st) != 0) {
     69  1.1  tsutsui 		perror("device stat");
     70  1.1  tsutsui 		goto err_exit;
     71  1.1  tsutsui 	}
     72  1.1  tsutsui 	if (!S_ISCHR(st.st_mode)) {
     73  1.1  tsutsui 		fprintf(stderr, "WARNING: not a raw device.\n");
     74  1.1  tsutsui 	}
     75  1.1  tsutsui 
     76  1.1  tsutsui 	part = DISKPART(st.st_rdev);
     77  1.1  tsutsui 
     78  1.1  tsutsui 	if (ioctl(fd, DIOCGDINFO, &d) == -1) {
     79  1.1  tsutsui 		perror("disklabel");
     80  1.1  tsutsui 		goto err_exit;
     81  1.1  tsutsui 	}
     82  1.1  tsutsui 	p = &d.d_partitions[part];
     83  1.1  tsutsui 	printf("partition = %d\n", part);
     84  1.1  tsutsui 	printf("size=%d offset=%d fstype=%d secsize=%d\n",
     85  1.1  tsutsui 	    p->p_size, p->p_offset, p->p_fstype, d.d_secsize);
     86  1.1  tsutsui 
     87  1.1  tsutsui 	if (p->p_fstype != FS_SYSVBFS) {
     88  1.1  tsutsui 		fprintf(stderr, "not a SysVBFS partition.\n");
     89  1.1  tsutsui 		goto err_exit;
     90  1.1  tsutsui 	}
     91  1.1  tsutsui 
     92  1.1  tsutsui 	if (bfs_newfs(fd, p->p_size) != 0)
     93  1.1  tsutsui 		goto err_exit;
     94  1.1  tsutsui 
     95  1.1  tsutsui 	close(fd);
     96  1.1  tsutsui 
     97  1.1  tsutsui 	return 0;
     98  1.1  tsutsui  err_exit:
     99  1.1  tsutsui 	close(fd);
    100  1.1  tsutsui 	exit(EXIT_FAILURE);
    101  1.1  tsutsui }
    102  1.1  tsutsui 
    103  1.1  tsutsui int
    104  1.1  tsutsui bfs_newfs(int fd, uint32_t nsectors)
    105  1.1  tsutsui {
    106  1.1  tsutsui 	uint8_t buf[DEV_BSIZE];
    107  1.1  tsutsui 	struct bfs_super_block *bfs = (void *)buf;
    108  1.1  tsutsui 	struct bfs_inode *inode = (void *)buf;
    109  1.1  tsutsui 	struct bfs_dirent *dirent = (void *)buf;
    110  1.1  tsutsui 	time_t t = time(0);
    111  1.1  tsutsui 	int err;
    112  1.1  tsutsui 
    113  1.1  tsutsui 	/* Super block */
    114  1.1  tsutsui 	memset(buf, 0, DEV_BSIZE);
    115  1.1  tsutsui 	bfs->header.magic = BFS_MAGIC;
    116  1.1  tsutsui 	bfs->header.data_start_byte = DEV_BSIZE * 2; /* super block + inode */
    117  1.1  tsutsui 	bfs->header.data_end_byte = nsectors * BFS_BSIZE - 1;
    118  1.1  tsutsui 	bfs->compaction.from = 0xffffffff;
    119  1.1  tsutsui 	bfs->compaction.to = 0xffffffff;
    120  1.1  tsutsui 	bfs->compaction.from_backup = 0xffffffff;
    121  1.1  tsutsui 	bfs->compaction.to_backup = 0xffffffff;
    122  1.1  tsutsui 
    123  1.1  tsutsui 	if ((err = lseek(fd, 0, SEEK_SET)) == -1) {
    124  1.1  tsutsui 		perror("seek super block");
    125  1.1  tsutsui 		return -1;
    126  1.1  tsutsui 	}
    127  1.1  tsutsui 	if (write(fd, buf, BFS_BSIZE) < 0) {
    128  1.1  tsutsui 		perror("write super block");
    129  1.1  tsutsui 		return -1;
    130  1.1  tsutsui 	}
    131  1.1  tsutsui 
    132  1.1  tsutsui 	/* i-node table */
    133  1.1  tsutsui 	memset(buf, 0, BFS_BSIZE);
    134  1.1  tsutsui 	inode->number = BFS_ROOT_INODE;
    135  1.1  tsutsui 	inode->start_sector = 2;
    136  1.1  tsutsui 	inode->end_sector = 2;
    137  1.1  tsutsui 	inode->eof_offset_byte = sizeof(struct bfs_dirent) +
    138  1.1  tsutsui 	    inode->start_sector * BFS_BSIZE;
    139  1.1  tsutsui 	inode->attr.atime = t;
    140  1.1  tsutsui 	inode->attr.mtime = t;
    141  1.1  tsutsui 	inode->attr.ctime = t;
    142  1.1  tsutsui 	inode->attr.mode = 0755;
    143  1.1  tsutsui 	inode->attr.type = 2;	/* DIR */
    144  1.1  tsutsui 	inode->attr.nlink = 2;	/* . + .. */
    145  1.1  tsutsui 	if (write(fd, buf, BFS_BSIZE) < 0) {
    146  1.1  tsutsui 		perror("write i-node");
    147  1.1  tsutsui 		return -1;
    148  1.1  tsutsui 	}
    149  1.1  tsutsui 
    150  1.1  tsutsui 	/* dirent table */
    151  1.1  tsutsui 	memset(buf, 0, BFS_BSIZE);
    152  1.1  tsutsui 	dirent->inode = BFS_ROOT_INODE;
    153  1.1  tsutsui 	sprintf(dirent->name, ".");
    154  1.1  tsutsui 	dirent++;
    155  1.1  tsutsui 	dirent->inode = BFS_ROOT_INODE;
    156  1.1  tsutsui 	sprintf(dirent->name, "..");
    157  1.1  tsutsui 	if (write(fd, buf, BFS_BSIZE) < 0) {
    158  1.1  tsutsui 		perror("write dirent");
    159  1.1  tsutsui 		return -1;
    160  1.1  tsutsui 	}
    161  1.1  tsutsui 
    162  1.1  tsutsui 	return 0;
    163  1.1  tsutsui }
    164  1.1  tsutsui 
    165  1.1  tsutsui void
    166  1.1  tsutsui usage(void)
    167  1.1  tsutsui {
    168  1.1  tsutsui 
    169  1.1  tsutsui 	(void)fprintf(stderr, "usage: %s special-device\n", getprogname());
    170  1.1  tsutsui 	exit(EXIT_FAILURE);
    171  1.1  tsutsui }
    172