2 * SPDX-License-Identifier: GPL-2.0-or-later
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
9 * mkfs.bfs - Create SCO BFS filesystem - aeb, 1999-09-07
11 * Copyright (C) 1999 Andries E. Brouwe
13 * Usage: mkfs.bfs [-N nr-of-inodes] [-V volume-name] [-F fsname] device
28 #include "closestream.h"
33 #include "exitcodes.h"
35 #define BFS_ROOT_INO 2
36 #define BFS_NAMELEN 14
37 #define BFS_BLOCKSIZE 512
38 #define BFS_SUPER_MAGIC 0x1badface
40 /* superblock - 512 bytes */
43 uint32_t s_start
; /* byte offset of start of data */
44 uint32_t s_end
; /* sizeof(slice)-1 */
46 /* for recovery during compaction */
47 uint32_t s_from
, s_to
; /* src and dest block of current transfer */
48 int32_t s_backup_from
, s_backup_to
;
50 /* labels - may well contain garbage */
56 /* inode - 64 bytes */
59 unsigned char i_pad1
[2];
60 uint32_t i_first_block
;
61 uint32_t i_last_block
;
62 uint32_t i_bytes_to_end
;
63 uint32_t i_type
; /* 1: file, 2: the unique dir */
65 uint32_t i_uid
, i_gid
;
67 uint32_t i_atime
, i_mtime
, i_ctime
;
68 unsigned char i_pad2
[16];
71 #define BFS_DIR_TYPE 2
73 /* directory entry - 16 bytes */
76 char d_name
[BFS_NAMELEN
];
79 static void __attribute__((__noreturn__
)) usage(void)
83 _("Usage: %s [options] device [block-count]\n"),
84 program_invocation_short_name
);
86 fputs(USAGE_SEPARATOR
, out
);
87 fputs(_("Make an SCO bfs filesystem.\n"), out
);
89 fprintf(out
, _("\nOptions:\n"
90 " -N, --inodes=NUM specify desired number of inodes\n"
91 " -V, --vname=NAME specify volume name\n"
92 " -F, --fname=NAME specify file system name\n"
93 " -v, --verbose explain what is being done\n"
94 " -c this option is silently ignored\n"
95 " -l this option is silently ignored\n"
96 " --lock[=<mode>] use exclusive device lock (yes, no or nonblock)\n"
98 fprintf(out
, USAGE_HELP_OPTIONS(21));
100 fprintf(out
, USAGE_MAN_TAIL("mkfs.bfs(8)"));
104 int main(int argc
, char **argv
)
106 char *device
, *volume
, *fsname
;
109 unsigned long long total_blocks
, ino_bytes
, ino_blocks
, data_blocks
;
110 unsigned long long user_specified_total_blocks
= 0;
113 uint32_t first_block
;
122 VERSION_OPTION
= CHAR_MAX
+ 1,
125 static const struct option longopts
[] = {
126 {"inodes", required_argument
, NULL
, 'N'},
127 {"vname", required_argument
, NULL
, 'V'},
128 {"fname", required_argument
, NULL
, 'F'},
129 {"verbose", no_argument
, NULL
, 'v'},
130 {"version", no_argument
, NULL
, VERSION_OPTION
},
131 {"help", no_argument
, NULL
, 'h'},
132 {"lock", optional_argument
, NULL
, OPT_LOCK
},
136 setlocale(LC_ALL
, "");
137 bindtextdomain(PACKAGE
, LOCALEDIR
);
139 close_stdout_atexit();
142 warnx(_("not enough arguments"));
143 errtryhelp(EXIT_FAILURE
);
145 if (argc
== 2 && !strcmp(argv
[1], "-V"))
146 print_version(EXIT_SUCCESS
);
148 volume
= fsname
= " "; /* is there a default? */
151 while ((c
= getopt_long(argc
, argv
, "N:V:F:vhcl", longopts
, NULL
)) != -1) {
154 inodes
= strtol_or_err(optarg
, _("invalid number of inodes"));
158 len
= strlen(optarg
);
159 if (len
<= 0 || len
> 6)
160 errx(EXIT_FAILURE
, _("volume name too long"));
161 volume
= xstrdup(optarg
);
165 len
= strlen(optarg
);
166 if (len
<= 0 || len
> 6)
167 errx(EXIT_FAILURE
, _("fsname name too long"));
168 fsname
= xstrdup(optarg
);
177 /* when called via mkfs we may get options c,l,v */
190 print_version(EXIT_SUCCESS
);
194 errtryhelp(EXIT_FAILURE
);
198 if (optind
== argc
) {
199 warnx(_("no device specified"));
200 errtryhelp(EXIT_FAILURE
);
203 device
= argv
[optind
++];
205 if (stat(device
, &statbuf
) < 0)
206 err(EXIT_FAILURE
, _("stat of %s failed"), device
);
208 fd
= open_blkdev_or_file(&statbuf
, device
, O_RDWR
);
210 err(EXIT_FAILURE
, _("cannot open %s"), device
);
212 if (blkdev_lock(fd
, device
, lockmode
) != 0)
215 if (optind
== argc
- 1)
216 user_specified_total_blocks
=
217 strtou64_or_err(argv
[optind
], _("invalid block-count"));
218 else if (optind
!= argc
) {
219 warnx(_("bad usage"));
220 errtryhelp(EXIT_FAILURE
);
223 if (blkdev_get_sectors(fd
, &total_blocks
) == -1) {
224 if (!user_specified_total_blocks
)
225 err(EXIT_FAILURE
, _("cannot get size of %s"), device
);
226 total_blocks
= user_specified_total_blocks
;
227 } else if (user_specified_total_blocks
) {
228 if (user_specified_total_blocks
> total_blocks
)
230 _("blocks argument too large, max is %llu"),
232 total_blocks
= user_specified_total_blocks
;
236 /* pick some reasonable default */
237 inodes
= 8 * (total_blocks
/ 800);
243 /* believe the user */
245 errx(EXIT_FAILURE
, _("too many inodes - max is 512"));
248 ino_bytes
= inodes
* sizeof(struct bfsi
);
249 ino_blocks
= (ino_bytes
+ BFS_BLOCKSIZE
- 1) / BFS_BLOCKSIZE
;
250 data_blocks
= total_blocks
- ino_blocks
- 1;
252 /* mimic the behavior of SCO's mkfs - maybe this limit is needed */
253 if (data_blocks
< 32)
255 _("not enough space, need at least %llu blocks"),
258 memset(&sb
, 0, sizeof(sb
));
259 sb
.s_magic
= cpu_to_le32(BFS_SUPER_MAGIC
);
260 sb
.s_start
= cpu_to_le32(ino_bytes
+ sizeof(struct bfssb
));
261 sb
.s_end
= cpu_to_le32(total_blocks
* BFS_BLOCKSIZE
- 1);
262 sb
.s_from
= sb
.s_to
= sb
.s_backup_from
= sb
.s_backup_to
= -1;
263 memcpy(sb
.s_fsname
, fsname
, 6);
264 memcpy(sb
.s_volume
, volume
, 6);
267 fprintf(stderr
, _("Device: %s\n"), device
);
268 fprintf(stderr
, _("Volume: <%-6s>\n"), volume
);
269 fprintf(stderr
, _("FSname: <%-6s>\n"), fsname
);
270 fprintf(stderr
, _("BlockSize: %d\n"), BFS_BLOCKSIZE
);
272 fprintf(stderr
, _("Inodes: %ld (in 1 block)\n"),
275 fprintf(stderr
, _("Inodes: %ld (in %llu blocks)\n"),
277 fprintf(stderr
, _("Blocks: %llu\n"), total_blocks
);
278 fprintf(stderr
, _("Inode end: %d, Data end: %d\n"),
279 le32_to_cpu(sb
.s_start
) - 1, le32_to_cpu(sb
.s_end
));
282 if (write(fd
, &sb
, sizeof(sb
)) != sizeof(sb
))
283 err(EXIT_FAILURE
, _("error writing superblock"));
285 memset(&ri
, 0, sizeof(ri
));
286 ri
.i_ino
= cpu_to_le16(BFS_ROOT_INO
);
287 first_block
= 1 + ino_blocks
;
288 ri
.i_first_block
= cpu_to_le32(first_block
);
289 ri
.i_last_block
= cpu_to_le32(first_block
+
290 (inodes
* sizeof(de
) - 1) / BFS_BLOCKSIZE
);
291 ri
.i_bytes_to_end
= cpu_to_le32(first_block
* BFS_BLOCKSIZE
292 + 2 * sizeof(struct bfsde
) - 1);
293 ri
.i_type
= cpu_to_le32(BFS_DIR_TYPE
);
294 ri
.i_mode
= cpu_to_le32(S_IFDIR
| 0755); /* or just 0755 */
295 ri
.i_uid
= cpu_to_le32(0);
296 ri
.i_gid
= cpu_to_le32(1); /* random */
299 ri
.i_atime
= cpu_to_le32(now
);
300 ri
.i_mtime
= cpu_to_le32(now
);
301 ri
.i_ctime
= cpu_to_le32(now
);
303 if (write(fd
, &ri
, sizeof(ri
)) != sizeof(ri
))
304 err(EXIT_FAILURE
, _("error writing root inode"));
306 memset(&ri
, 0, sizeof(ri
));
307 for (i
= 1; i
< inodes
; i
++)
308 if (write(fd
, &ri
, sizeof(ri
)) != sizeof(ri
))
309 err(EXIT_FAILURE
, _("error writing inode"));
311 if (lseek(fd
, (1 + ino_blocks
) * BFS_BLOCKSIZE
, SEEK_SET
) == -1)
312 err(EXIT_FAILURE
, _("seek error"));
314 memset(&de
, 0, sizeof(de
));
315 de
.d_ino
= cpu_to_le16(BFS_ROOT_INO
);
316 memcpy(de
.d_name
, ".", 1);
317 if (write(fd
, &de
, sizeof(de
)) != sizeof(de
))
318 err(EXIT_FAILURE
, _("error writing . entry"));
320 memcpy(de
.d_name
, "..", 2);
321 if (write(fd
, &de
, sizeof(de
)) != sizeof(de
))
322 err(EXIT_FAILURE
, _("error writing .. entry"));
324 if (close_fd(fd
) != 0)
325 err(EXIT_FAILURE
, _("error closing %s"), device
);