newfs_sysvbfs.c revision 1.5 1 1.5 wiz /* $NetBSD: newfs_sysvbfs.c,v 1.5 2009/04/10 13:48:00 wiz 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.4 pooka #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.4 pooka uint32_t partsize;
59 1.4 pooka int Fflag, Zflag;
60 1.1 tsutsui int part;
61 1.4 pooka int fd, ch;
62 1.1 tsutsui
63 1.4 pooka if (argc < 2)
64 1.1 tsutsui usage();
65 1.1 tsutsui
66 1.4 pooka Fflag = Zflag = partsize = 0;
67 1.4 pooka while ((ch = getopt(argc, argv, "Fs:Z")) != -1) {
68 1.4 pooka switch (ch) {
69 1.4 pooka case 'F':
70 1.4 pooka Fflag = 1;
71 1.4 pooka break;
72 1.4 pooka case 's':
73 1.4 pooka partsize = atoi(optarg);
74 1.4 pooka break;
75 1.4 pooka case 'Z':
76 1.4 pooka Zflag = 1;
77 1.4 pooka break;
78 1.4 pooka default:
79 1.4 pooka usage();
80 1.4 pooka /*NOTREACHED*/
81 1.4 pooka }
82 1.1 tsutsui }
83 1.4 pooka argc -= optind;
84 1.4 pooka argv += optind;
85 1.1 tsutsui
86 1.4 pooka if (argc != 1)
87 1.4 pooka usage();
88 1.4 pooka device = argv[0];
89 1.1 tsutsui
90 1.4 pooka if (!Fflag) {
91 1.4 pooka if ((fd = open(device, O_RDWR)) == -1) {
92 1.4 pooka perror("open device");
93 1.4 pooka exit(EXIT_FAILURE);
94 1.4 pooka }
95 1.4 pooka if (fstat(fd, &st) != 0) {
96 1.4 pooka perror("device stat");
97 1.4 pooka goto err_exit;
98 1.4 pooka }
99 1.4 pooka if (!S_ISCHR(st.st_mode)) {
100 1.4 pooka fprintf(stderr, "WARNING: not a raw device.\n");
101 1.4 pooka }
102 1.4 pooka
103 1.4 pooka part = DISKPART(st.st_rdev);
104 1.4 pooka
105 1.4 pooka if (ioctl(fd, DIOCGDINFO, &d) == -1) {
106 1.4 pooka perror("disklabel");
107 1.4 pooka goto err_exit;
108 1.4 pooka }
109 1.4 pooka p = &d.d_partitions[part];
110 1.4 pooka printf("partition = %d\n", part);
111 1.4 pooka printf("size=%d offset=%d fstype=%d secsize=%d\n",
112 1.4 pooka p->p_size, p->p_offset, p->p_fstype, d.d_secsize);
113 1.4 pooka
114 1.4 pooka if (p->p_fstype != FS_SYSVBFS) {
115 1.4 pooka fprintf(stderr, "not a SysVBFS partition.\n");
116 1.4 pooka goto err_exit;
117 1.4 pooka }
118 1.4 pooka partsize = p->p_size;
119 1.4 pooka } else {
120 1.4 pooka off_t filesize;
121 1.4 pooka uint8_t zbuf[8192] = {0, };
122 1.4 pooka
123 1.4 pooka if (partsize == 0) {
124 1.4 pooka warnx("-F requires -s");
125 1.4 pooka exit(EXIT_FAILURE);
126 1.4 pooka }
127 1.4 pooka
128 1.4 pooka filesize = partsize << BFS_BSHIFT;
129 1.4 pooka
130 1.4 pooka fd = open(device, O_RDWR|O_CREAT|O_TRUNC, 0666);
131 1.4 pooka if (fd == -1) {
132 1.4 pooka perror("open file");
133 1.4 pooka exit(EXIT_FAILURE);
134 1.4 pooka }
135 1.4 pooka
136 1.4 pooka if (Zflag) {
137 1.4 pooka while (filesize > 0) {
138 1.4 pooka size_t writenow = MIN(filesize, sizeof(zbuf));
139 1.4 pooka
140 1.4 pooka if (write(fd, zbuf, writenow) != writenow) {
141 1.4 pooka perror("zwrite");
142 1.4 pooka exit(EXIT_FAILURE);
143 1.4 pooka }
144 1.4 pooka filesize -= writenow;
145 1.4 pooka }
146 1.4 pooka } else {
147 1.4 pooka if (lseek(fd, filesize-1, SEEK_SET) == -1) {
148 1.4 pooka perror("lseek");
149 1.4 pooka exit(EXIT_FAILURE);
150 1.4 pooka }
151 1.4 pooka if (write(fd, zbuf, 1) != 1) {
152 1.4 pooka perror("write");
153 1.4 pooka exit(EXIT_FAILURE);
154 1.4 pooka }
155 1.4 pooka if (lseek(fd, 0, SEEK_SET) == -1) {
156 1.4 pooka perror("lseek 2");
157 1.4 pooka exit(EXIT_FAILURE);
158 1.4 pooka }
159 1.4 pooka }
160 1.1 tsutsui }
161 1.1 tsutsui
162 1.4 pooka 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.3 joerg 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.4 pooka 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.4 pooka 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.3 joerg static void
236 1.1 tsutsui usage(void)
237 1.1 tsutsui {
238 1.1 tsutsui
239 1.5 wiz (void)fprintf(stderr, "usage: %s [-FZ] [-s sectors] special-device\n",
240 1.4 pooka getprogname());
241 1.1 tsutsui exit(EXIT_FAILURE);
242 1.1 tsutsui }
243