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