1 /* $NetBSD: newfs_sysvbfs.c,v 1.6 2009/04/11 07:16:30 lukem Exp $ */
4 * Copyright (c) 2004 The NetBSD Foundation, Inc.
7 * This code is derived from software contributed to The NetBSD Foundation
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
19 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 * POSSIBILITY OF SUCH DAMAGE.
32 #include <sys/types.h>
33 #include <sys/param.h>
42 #include <sys/errno.h>
43 #include <sys/ioctl.h>
44 #include <sys/disklabel.h>
46 #include <fs/sysvbfs/bfs.h>
48 static void usage(void);
49 static int bfs_newfs(int, uint32_t);
52 main(int argc
, char **argv
)
66 Fflag
= Zflag
= partsize
= 0;
67 while ((ch
= getopt(argc
, argv
, "Fs:Z")) != -1) {
73 partsize
= atoi(optarg
);
91 if ((fd
= open(device
, O_RDWR
)) == -1) {
92 perror("open device");
95 if (fstat(fd
, &st
) != 0) {
96 perror("device stat");
99 if (!S_ISCHR(st
.st_mode
)) {
100 fprintf(stderr
, "WARNING: not a raw device.\n");
103 part
= DISKPART(st
.st_rdev
);
105 if (ioctl(fd
, DIOCGDINFO
, &d
) == -1) {
109 p
= &d
.d_partitions
[part
];
110 printf("partition = %d\n", part
);
111 printf("size=%d offset=%d fstype=%d secsize=%d\n",
112 p
->p_size
, p
->p_offset
, p
->p_fstype
, d
.d_secsize
);
114 if (p
->p_fstype
!= FS_SYSVBFS
) {
115 fprintf(stderr
, "not a SysVBFS partition.\n");
118 partsize
= p
->p_size
;
121 uint8_t zbuf
[8192] = {0, };
124 warnx("-F requires -s");
128 filesize
= partsize
<< BFS_BSHIFT
;
130 fd
= open(device
, O_RDWR
|O_CREAT
|O_TRUNC
, 0666);
137 while (filesize
> 0) {
138 size_t writenow
= MIN(filesize
, (off_t
)sizeof(zbuf
));
140 if ((size_t)write(fd
, zbuf
, writenow
) != writenow
) {
144 filesize
-= writenow
;
147 if (lseek(fd
, filesize
-1, SEEK_SET
) == -1) {
151 if (write(fd
, zbuf
, 1) != 1) {
155 if (lseek(fd
, 0, SEEK_SET
) == -1) {
162 if (bfs_newfs(fd
, partsize
) != 0)
174 bfs_newfs(int fd
, uint32_t nsectors
)
176 uint8_t buf
[DEV_BSIZE
];
177 struct bfs_super_block
*bfs
= (void *)buf
;
178 struct bfs_inode
*inode
= (void *)buf
;
179 struct bfs_dirent
*dirent
= (void *)buf
;
184 memset(buf
, 0, DEV_BSIZE
);
185 bfs
->header
.magic
= BFS_MAGIC
;
186 bfs
->header
.data_start_byte
= DEV_BSIZE
* 2; /* super block + inode */
187 bfs
->header
.data_end_byte
= nsectors
* BFS_BSIZE
- 1;
188 bfs
->compaction
.from
= 0xffffffff;
189 bfs
->compaction
.to
= 0xffffffff;
190 bfs
->compaction
.from_backup
= 0xffffffff;
191 bfs
->compaction
.to_backup
= 0xffffffff;
193 if ((error
= lseek(fd
, 0, SEEK_SET
)) == -1) {
194 perror("seek super block");
197 if (write(fd
, buf
, BFS_BSIZE
) < 0) {
198 perror("write super block");
203 memset(buf
, 0, BFS_BSIZE
);
204 inode
->number
= BFS_ROOT_INODE
;
205 inode
->start_sector
= 2;
206 inode
->end_sector
= 2;
207 inode
->eof_offset_byte
= sizeof(struct bfs_dirent
) +
208 inode
->start_sector
* BFS_BSIZE
;
209 inode
->attr
.atime
= t
;
210 inode
->attr
.mtime
= t
;
211 inode
->attr
.ctime
= t
;
212 inode
->attr
.mode
= 0755;
213 inode
->attr
.type
= 2; /* DIR */
214 inode
->attr
.nlink
= 2; /* . + .. */
215 if (write(fd
, buf
, BFS_BSIZE
) < 0) {
216 perror("write i-node");
221 memset(buf
, 0, BFS_BSIZE
);
222 dirent
->inode
= BFS_ROOT_INODE
;
223 sprintf(dirent
->name
, ".");
225 dirent
->inode
= BFS_ROOT_INODE
;
226 sprintf(dirent
->name
, "..");
227 if (write(fd
, buf
, BFS_BSIZE
) < 0) {
228 perror("write dirent");
239 (void)fprintf(stderr
, "usage: %s [-FZ] [-s sectors] special-device\n",