Home | History | Annotate | Line # | Download | only in newfs_sysvbfs
newfs_sysvbfs.c revision 1.2.6.1
      1  1.2.6.1      jym /*	$NetBSD: newfs_sysvbfs.c,v 1.2.6.1 2009/05/13 19:19:04 jym 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.2.6.1      jym #include <err.h>
     35      1.1  tsutsui #include <stdio.h>
     36      1.1  tsutsui #include <stdlib.h>
     37      1.1  tsutsui #include <string.h>
     38      1.1  tsutsui #include <unistd.h>
     39      1.1  tsutsui #include <fcntl.h>
     40      1.1  tsutsui #include <time.h>
     41      1.1  tsutsui #include <assert.h>
     42      1.1  tsutsui #include <sys/errno.h>
     43      1.1  tsutsui #include <sys/ioctl.h>
     44      1.1  tsutsui #include <sys/disklabel.h>
     45      1.1  tsutsui 
     46      1.1  tsutsui #include <fs/sysvbfs/bfs.h>
     47      1.1  tsutsui 
     48      1.1  tsutsui static void usage(void);
     49      1.1  tsutsui static int bfs_newfs(int, uint32_t);
     50      1.1  tsutsui 
     51      1.1  tsutsui int
     52      1.1  tsutsui main(int argc, char **argv)
     53      1.1  tsutsui {
     54      1.1  tsutsui 	const char *device;
     55      1.1  tsutsui 	struct disklabel d;
     56      1.1  tsutsui 	struct partition *p;
     57      1.1  tsutsui 	struct stat st;
     58  1.2.6.1      jym 	uint32_t partsize;
     59  1.2.6.1      jym 	int Fflag, Zflag;
     60      1.1  tsutsui 	int part;
     61  1.2.6.1      jym 	int fd, ch;
     62      1.1  tsutsui 
     63  1.2.6.1      jym 	if (argc < 2)
     64      1.1  tsutsui 		usage();
     65      1.1  tsutsui 
     66  1.2.6.1      jym 	Fflag = Zflag = partsize = 0;
     67  1.2.6.1      jym 	while ((ch = getopt(argc, argv, "Fs:Z")) != -1) {
     68  1.2.6.1      jym 		switch (ch) {
     69  1.2.6.1      jym 		case 'F':
     70  1.2.6.1      jym 			Fflag = 1;
     71  1.2.6.1      jym 			break;
     72  1.2.6.1      jym 		case 's':
     73  1.2.6.1      jym 			partsize = atoi(optarg);
     74  1.2.6.1      jym 			break;
     75  1.2.6.1      jym 		case 'Z':
     76  1.2.6.1      jym 			Zflag = 1;
     77  1.2.6.1      jym 			break;
     78  1.2.6.1      jym 		default:
     79  1.2.6.1      jym 			usage();
     80  1.2.6.1      jym 			/*NOTREACHED*/
     81  1.2.6.1      jym 		}
     82      1.1  tsutsui 	}
     83  1.2.6.1      jym 	argc -= optind;
     84  1.2.6.1      jym 	argv += optind;
     85      1.1  tsutsui 
     86  1.2.6.1      jym 	if (argc != 1)
     87  1.2.6.1      jym 		usage();
     88  1.2.6.1      jym 	device = argv[0];
     89      1.1  tsutsui 
     90  1.2.6.1      jym 	if (!Fflag) {
     91  1.2.6.1      jym 		if ((fd = open(device, O_RDWR)) == -1) {
     92  1.2.6.1      jym 			perror("open device");
     93  1.2.6.1      jym 			exit(EXIT_FAILURE);
     94  1.2.6.1      jym 		}
     95  1.2.6.1      jym 		if (fstat(fd, &st) != 0) {
     96  1.2.6.1      jym 			perror("device stat");
     97  1.2.6.1      jym 			goto err_exit;
     98  1.2.6.1      jym 		}
     99  1.2.6.1      jym 		if (!S_ISCHR(st.st_mode)) {
    100  1.2.6.1      jym 			fprintf(stderr, "WARNING: not a raw device.\n");
    101  1.2.6.1      jym 		}
    102  1.2.6.1      jym 
    103  1.2.6.1      jym 		part = DISKPART(st.st_rdev);
    104  1.2.6.1      jym 
    105  1.2.6.1      jym 		if (ioctl(fd, DIOCGDINFO, &d) == -1) {
    106  1.2.6.1      jym 			perror("disklabel");
    107  1.2.6.1      jym 			goto err_exit;
    108  1.2.6.1      jym 		}
    109  1.2.6.1      jym 		p = &d.d_partitions[part];
    110  1.2.6.1      jym 		printf("partition = %d\n", part);
    111  1.2.6.1      jym 		printf("size=%d offset=%d fstype=%d secsize=%d\n",
    112  1.2.6.1      jym 		    p->p_size, p->p_offset, p->p_fstype, d.d_secsize);
    113  1.2.6.1      jym 
    114  1.2.6.1      jym 		if (p->p_fstype != FS_SYSVBFS) {
    115  1.2.6.1      jym 			fprintf(stderr, "not a SysVBFS partition.\n");
    116  1.2.6.1      jym 			goto err_exit;
    117  1.2.6.1      jym 		}
    118  1.2.6.1      jym 		partsize = p->p_size;
    119  1.2.6.1      jym 	} else {
    120  1.2.6.1      jym 		off_t filesize;
    121  1.2.6.1      jym 		uint8_t zbuf[8192] = {0, };
    122  1.2.6.1      jym 
    123  1.2.6.1      jym 		if (partsize == 0) {
    124  1.2.6.1      jym 			warnx("-F requires -s");
    125  1.2.6.1      jym 			exit(EXIT_FAILURE);
    126  1.2.6.1      jym 		}
    127  1.2.6.1      jym 
    128  1.2.6.1      jym 		filesize = partsize << BFS_BSHIFT;
    129  1.2.6.1      jym 
    130  1.2.6.1      jym 		fd = open(device, O_RDWR|O_CREAT|O_TRUNC, 0666);
    131  1.2.6.1      jym 		if (fd == -1) {
    132  1.2.6.1      jym 			perror("open file");
    133  1.2.6.1      jym 			exit(EXIT_FAILURE);
    134  1.2.6.1      jym 		}
    135  1.2.6.1      jym 
    136  1.2.6.1      jym 		if (Zflag) {
    137  1.2.6.1      jym 			while (filesize > 0) {
    138  1.2.6.1      jym 				size_t writenow = MIN(filesize, (off_t)sizeof(zbuf));
    139  1.2.6.1      jym 
    140  1.2.6.1      jym 				if ((size_t)write(fd, zbuf, writenow) != writenow) {
    141  1.2.6.1      jym 					perror("zwrite");
    142  1.2.6.1      jym 					exit(EXIT_FAILURE);
    143  1.2.6.1      jym 				}
    144  1.2.6.1      jym 				filesize -= writenow;
    145  1.2.6.1      jym 			}
    146  1.2.6.1      jym 		} else {
    147  1.2.6.1      jym 			if (lseek(fd, filesize-1, SEEK_SET) == -1) {
    148  1.2.6.1      jym 				perror("lseek");
    149  1.2.6.1      jym 				exit(EXIT_FAILURE);
    150  1.2.6.1      jym 			}
    151  1.2.6.1      jym 			if (write(fd, zbuf, 1) != 1) {
    152  1.2.6.1      jym 				perror("write");
    153  1.2.6.1      jym 				exit(EXIT_FAILURE);
    154  1.2.6.1      jym 			}
    155  1.2.6.1      jym 			if (lseek(fd, 0, SEEK_SET) == -1) {
    156  1.2.6.1      jym 				perror("lseek 2");
    157  1.2.6.1      jym 				exit(EXIT_FAILURE);
    158  1.2.6.1      jym 			}
    159  1.2.6.1      jym 		}
    160      1.1  tsutsui 	}
    161      1.1  tsutsui 
    162  1.2.6.1      jym 	if (bfs_newfs(fd, partsize) != 0)
    163      1.1  tsutsui 		goto err_exit;
    164      1.1  tsutsui 
    165      1.1  tsutsui 	close(fd);
    166      1.1  tsutsui 
    167      1.1  tsutsui 	return 0;
    168      1.1  tsutsui  err_exit:
    169      1.1  tsutsui 	close(fd);
    170      1.1  tsutsui 	exit(EXIT_FAILURE);
    171      1.1  tsutsui }
    172      1.1  tsutsui 
    173  1.2.6.1      jym static int
    174      1.1  tsutsui bfs_newfs(int fd, uint32_t nsectors)
    175      1.1  tsutsui {
    176      1.1  tsutsui 	uint8_t buf[DEV_BSIZE];
    177      1.1  tsutsui 	struct bfs_super_block *bfs = (void *)buf;
    178      1.1  tsutsui 	struct bfs_inode *inode = (void *)buf;
    179      1.1  tsutsui 	struct bfs_dirent *dirent = (void *)buf;
    180      1.1  tsutsui 	time_t t = time(0);
    181  1.2.6.1      jym 	int error;
    182      1.1  tsutsui 
    183      1.1  tsutsui 	/* Super block */
    184      1.1  tsutsui 	memset(buf, 0, DEV_BSIZE);
    185      1.1  tsutsui 	bfs->header.magic = BFS_MAGIC;
    186      1.1  tsutsui 	bfs->header.data_start_byte = DEV_BSIZE * 2; /* super block + inode */
    187      1.1  tsutsui 	bfs->header.data_end_byte = nsectors * BFS_BSIZE - 1;
    188      1.1  tsutsui 	bfs->compaction.from = 0xffffffff;
    189      1.1  tsutsui 	bfs->compaction.to = 0xffffffff;
    190      1.1  tsutsui 	bfs->compaction.from_backup = 0xffffffff;
    191      1.1  tsutsui 	bfs->compaction.to_backup = 0xffffffff;
    192      1.1  tsutsui 
    193  1.2.6.1      jym 	if ((error = lseek(fd, 0, SEEK_SET)) == -1) {
    194      1.1  tsutsui 		perror("seek super block");
    195      1.1  tsutsui 		return -1;
    196      1.1  tsutsui 	}
    197      1.1  tsutsui 	if (write(fd, buf, BFS_BSIZE) < 0) {
    198      1.1  tsutsui 		perror("write super block");
    199      1.1  tsutsui 		return -1;
    200      1.1  tsutsui 	}
    201      1.1  tsutsui 
    202      1.1  tsutsui 	/* i-node table */
    203      1.1  tsutsui 	memset(buf, 0, BFS_BSIZE);
    204      1.1  tsutsui 	inode->number = BFS_ROOT_INODE;
    205      1.1  tsutsui 	inode->start_sector = 2;
    206      1.1  tsutsui 	inode->end_sector = 2;
    207      1.1  tsutsui 	inode->eof_offset_byte = sizeof(struct bfs_dirent) +
    208      1.1  tsutsui 	    inode->start_sector * BFS_BSIZE;
    209      1.1  tsutsui 	inode->attr.atime = t;
    210      1.1  tsutsui 	inode->attr.mtime = t;
    211      1.1  tsutsui 	inode->attr.ctime = t;
    212      1.1  tsutsui 	inode->attr.mode = 0755;
    213      1.1  tsutsui 	inode->attr.type = 2;	/* DIR */
    214      1.1  tsutsui 	inode->attr.nlink = 2;	/* . + .. */
    215      1.1  tsutsui 	if (write(fd, buf, BFS_BSIZE) < 0) {
    216      1.1  tsutsui 		perror("write i-node");
    217      1.1  tsutsui 		return -1;
    218      1.1  tsutsui 	}
    219      1.1  tsutsui 
    220      1.1  tsutsui 	/* dirent table */
    221      1.1  tsutsui 	memset(buf, 0, BFS_BSIZE);
    222      1.1  tsutsui 	dirent->inode = BFS_ROOT_INODE;
    223      1.1  tsutsui 	sprintf(dirent->name, ".");
    224      1.1  tsutsui 	dirent++;
    225      1.1  tsutsui 	dirent->inode = BFS_ROOT_INODE;
    226      1.1  tsutsui 	sprintf(dirent->name, "..");
    227      1.1  tsutsui 	if (write(fd, buf, BFS_BSIZE) < 0) {
    228      1.1  tsutsui 		perror("write dirent");
    229      1.1  tsutsui 		return -1;
    230      1.1  tsutsui 	}
    231      1.1  tsutsui 
    232      1.1  tsutsui 	return 0;
    233      1.1  tsutsui }
    234      1.1  tsutsui 
    235  1.2.6.1      jym static void
    236      1.1  tsutsui usage(void)
    237      1.1  tsutsui {
    238      1.1  tsutsui 
    239  1.2.6.1      jym 	(void)fprintf(stderr, "usage: %s [-FZ] [-s sectors] special-device\n",
    240  1.2.6.1      jym 	    getprogname());
    241      1.1  tsutsui 	exit(EXIT_FAILURE);
    242      1.1  tsutsui }
    243