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