]> git.ipfire.org Git - thirdparty/util-linux.git/blame - disk-utils/mkfs.bfs.c
libblkid: make example more robust
[thirdparty/util-linux.git] / disk-utils / mkfs.bfs.c
CommitLineData
eb63b9b8 1/*
9e95aa12 2 * SPDX-License-Identifier: GPL-2.0-or-later
eb63b9b8 3 *
9e95aa12
KZ
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.
8 *
9 * mkfs.bfs - Create SCO BFS filesystem - aeb, 1999-09-07
10 *
11 * Copyright (C) 1999 Andries E. Brouwe
12 *
13 * Usage: mkfs.bfs [-N nr-of-inodes] [-V volume-name] [-F fsname] device
eb63b9b8 14 */
6496cb07
SK
15#include <errno.h>
16#include <fcntl.h>
17#include <getopt.h>
18#include <limits.h>
eb63b9b8
KZ
19#include <stdio.h>
20#include <stdlib.h>
eb63b9b8 21#include <string.h>
6496cb07 22#include <sys/stat.h>
eb63b9b8 23#include <time.h>
6496cb07 24#include <unistd.h>
c36f105d 25
6496cb07 26#include "blkdev.h"
c36f105d 27#include "c.h"
45ca68ec 28#include "closestream.h"
66ee8158 29#include "nls.h"
b7e27117 30#include "strutils.h"
e0c67b83 31#include "xalloc.h"
9d07f58d 32#include "bitops.h"
cc4fefc6 33#include "exitcodes.h"
eb63b9b8 34
eb63b9b8
KZ
35#define BFS_ROOT_INO 2
36#define BFS_NAMELEN 14
37#define BFS_BLOCKSIZE 512
38#define BFS_SUPER_MAGIC 0x1badface
39
40/* superblock - 512 bytes */
41struct bfssb {
9d07f58d 42 uint32_t s_magic;
43 uint32_t s_start; /* byte offset of start of data */
44 uint32_t s_end; /* sizeof(slice)-1 */
6496cb07
SK
45
46 /* for recovery during compaction */
9d07f58d 47 uint32_t s_from, s_to; /* src and dest block of current transfer */
48 int32_t s_backup_from, s_backup_to;
6496cb07
SK
49
50 /* labels - may well contain garbage */
51 char s_fsname[6];
52 char s_volume[6];
53 char s_pad[472];
eb63b9b8
KZ
54};
55
56/* inode - 64 bytes */
57struct bfsi {
9d07f58d 58 uint16_t i_ino;
6496cb07 59 unsigned char i_pad1[2];
9d07f58d 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 */
64 uint32_t i_mode;
65 uint32_t i_uid, i_gid;
66 uint32_t i_nlinks;
67 uint32_t i_atime, i_mtime, i_ctime;
6496cb07 68 unsigned char i_pad2[16];
eb63b9b8
KZ
69};
70
71#define BFS_DIR_TYPE 2
72
73/* directory entry - 16 bytes */
74struct bfsde {
9d07f58d 75 uint16_t d_ino;
de8b064b 76 char d_name[BFS_NAMELEN];
eb63b9b8
KZ
77};
78
6e1eda6f 79static void __attribute__((__noreturn__)) usage(void)
de8b064b 80{
6e1eda6f 81 FILE *out = stdout;
de8b064b
SK
82 fprintf(out,
83 _("Usage: %s [options] device [block-count]\n"),
84 program_invocation_short_name);
451dbcfa
BS
85
86 fputs(USAGE_SEPARATOR, out);
87 fputs(_("Make an SCO bfs filesystem.\n"), out);
88
de8b064b
SK
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"
bd0c8c85 96 " --lock[=<mode>] use exclusive device lock (yes, no or nonblock)\n"
b3054454 97 ));
bad4c729 98 fprintf(out, USAGE_HELP_OPTIONS(21));
de8b064b 99
bad4c729 100 fprintf(out, USAGE_MAN_TAIL("mkfs.bfs(8)"));
6e1eda6f 101 exit(EXIT_SUCCESS);
de8b064b 102}
eb63b9b8 103
6496cb07
SK
104int main(int argc, char **argv)
105{
eb63b9b8 106 char *device, *volume, *fsname;
cc4fefc6 107 char *lockmode = 0;
b7e27117 108 long inodes;
098fa6b1
ST
109 unsigned long long total_blocks, ino_bytes, ino_blocks, data_blocks;
110 unsigned long long user_specified_total_blocks = 0;
eb63b9b8
KZ
111 int verbose = 0;
112 int fd;
9d07f58d 113 uint32_t first_block;
eb63b9b8
KZ
114 struct bfssb sb;
115 struct bfsi ri;
116 struct bfsde de;
117 struct stat statbuf;
118 time_t now;
119 int c, i, len;
eb63b9b8 120
cc4fefc6 121 enum {
122 VERSION_OPTION = CHAR_MAX + 1,
123 OPT_LOCK
124 };
de8b064b
SK
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'},
cc4fefc6 132 {"lock", optional_argument, NULL, OPT_LOCK},
de8b064b
SK
133 {NULL, 0, NULL, 0}
134 };
eb63b9b8 135
88ffbf86
DB
136 setlocale(LC_ALL, "");
137 bindtextdomain(PACKAGE, LOCALEDIR);
138 textdomain(PACKAGE);
2c308875 139 close_stdout_atexit();
88ffbf86 140
6e1eda6f
RM
141 if (argc < 2) {
142 warnx(_("not enough arguments"));
143 errtryhelp(EXIT_FAILURE);
144 }
de8b064b 145 if (argc == 2 && !strcmp(argv[1], "-V"))
68224d10 146 print_version(EXIT_SUCCESS);
eb63b9b8
KZ
147
148 volume = fsname = " "; /* is there a default? */
149 inodes = 0;
150
de8b064b 151 while ((c = getopt_long(argc, argv, "N:V:F:vhcl", longopts, NULL)) != -1) {
eb63b9b8
KZ
152 switch (c) {
153 case 'N':
b7e27117 154 inodes = strtol_or_err(optarg, _("invalid number of inodes"));
eb63b9b8
KZ
155 break;
156
157 case 'V':
158 len = strlen(optarg);
159 if (len <= 0 || len > 6)
c36f105d 160 errx(EXIT_FAILURE, _("volume name too long"));
e0c67b83 161 volume = xstrdup(optarg);
eb63b9b8
KZ
162 break;
163
164 case 'F':
165 len = strlen(optarg);
166 if (len <= 0 || len > 6)
c36f105d 167 errx(EXIT_FAILURE, _("fsname name too long"));
e0c67b83 168 fsname = xstrdup(optarg);
eb63b9b8
KZ
169 break;
170
171 case 'v':
172 verbose = 1;
173 break;
174
eb63b9b8
KZ
175 case 'c':
176 case 'l':
6496cb07 177 /* when called via mkfs we may get options c,l,v */
eb63b9b8
KZ
178 break;
179
cc4fefc6 180 case OPT_LOCK:
181 lockmode = "1";
182 if (optarg) {
183 if (*optarg == '=')
184 optarg++;
185 lockmode = optarg;
186 }
187 break;
188
de8b064b 189 case VERSION_OPTION:
68224d10 190 print_version(EXIT_SUCCESS);
de8b064b 191 case 'h':
6e1eda6f 192 usage();
eb63b9b8 193 default:
677ec86c 194 errtryhelp(EXIT_FAILURE);
eb63b9b8
KZ
195 }
196 }
197
6e1eda6f
RM
198 if (optind == argc) {
199 warnx(_("no device specified"));
200 errtryhelp(EXIT_FAILURE);
201 }
eb63b9b8
KZ
202
203 device = argv[optind++];
204
6496cb07 205 if (stat(device, &statbuf) < 0)
fc14ceba 206 err(EXIT_FAILURE, _("stat of %s failed"), device);
eb63b9b8 207
5fc7694a 208 fd = open_blkdev_or_file(&statbuf, device, O_RDWR);
6496cb07 209 if (fd < 0)
c36f105d 210 err(EXIT_FAILURE, _("cannot open %s"), device);
eb63b9b8 211
cc4fefc6 212 if (blkdev_lock(fd, device, lockmode) != 0)
213 exit(MKFS_EX_ERROR);
214
6496cb07
SK
215 if (optind == argc - 1)
216 user_specified_total_blocks =
33fb5cfd 217 strtou64_or_err(argv[optind], _("invalid block-count"));
6e1eda6f
RM
218 else if (optind != argc) {
219 warnx(_("bad usage"));
220 errtryhelp(EXIT_FAILURE);
221 }
eb63b9b8 222
098fa6b1 223 if (blkdev_get_sectors(fd, &total_blocks) == -1) {
c36f105d
SK
224 if (!user_specified_total_blocks)
225 err(EXIT_FAILURE, _("cannot get size of %s"), device);
eb63b9b8
KZ
226 total_blocks = user_specified_total_blocks;
227 } else if (user_specified_total_blocks) {
228 if (user_specified_total_blocks > total_blocks)
6496cb07
SK
229 errx(EXIT_FAILURE,
230 _("blocks argument too large, max is %llu"),
231 total_blocks);
eb63b9b8
KZ
232 total_blocks = user_specified_total_blocks;
233 }
234
235 if (!inodes) {
236 /* pick some reasonable default */
6496cb07 237 inodes = 8 * (total_blocks / 800);
eb63b9b8
KZ
238 if (inodes < 48)
239 inodes = 48;
6496cb07 240 if (512 < inodes)
eb63b9b8
KZ
241 inodes = 512;
242 } else {
243 /* believe the user */
6496cb07 244 if (512 < inodes)
c36f105d 245 errx(EXIT_FAILURE, _("too many inodes - max is 512"));
eb63b9b8
KZ
246 }
247
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;
251
ee312c65 252 /* mimic the behavior of SCO's mkfs - maybe this limit is needed */
eb63b9b8 253 if (data_blocks < 32)
6496cb07
SK
254 errx(EXIT_FAILURE,
255 _("not enough space, need at least %llu blocks"),
256 ino_blocks + 33);
eb63b9b8
KZ
257
258 memset(&sb, 0, sizeof(sb));
9d07f58d 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);
eb63b9b8
KZ
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);
265
266 if (verbose) {
66ee8158
KZ
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);
6496cb07 271 if (ino_blocks == 1)
e3ca1312 272 fprintf(stderr, _("Inodes: %ld (in 1 block)\n"),
66ee8158
KZ
273 inodes);
274 else
e3ca1312 275 fprintf(stderr, _("Inodes: %ld (in %llu blocks)\n"),
66ee8158 276 inodes, ino_blocks);
e3ca1312 277 fprintf(stderr, _("Blocks: %llu\n"), total_blocks);
66ee8158 278 fprintf(stderr, _("Inode end: %d, Data end: %d\n"),
9d07f58d 279 le32_to_cpu(sb.s_start) - 1, le32_to_cpu(sb.s_end));
eb63b9b8
KZ
280 }
281
282 if (write(fd, &sb, sizeof(sb)) != sizeof(sb))
e5794f54 283 err(EXIT_FAILURE, _("error writing superblock"));
eb63b9b8
KZ
284
285 memset(&ri, 0, sizeof(ri));
9d07f58d 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 */
eb63b9b8
KZ
297 ri.i_nlinks = 2;
298 time(&now);
9d07f58d 299 ri.i_atime = cpu_to_le32(now);
300 ri.i_mtime = cpu_to_le32(now);
301 ri.i_ctime = cpu_to_le32(now);
eb63b9b8
KZ
302
303 if (write(fd, &ri, sizeof(ri)) != sizeof(ri))
e5794f54 304 err(EXIT_FAILURE, _("error writing root inode"));
eb63b9b8
KZ
305
306 memset(&ri, 0, sizeof(ri));
6496cb07 307 for (i = 1; i < inodes; i++)
eb63b9b8 308 if (write(fd, &ri, sizeof(ri)) != sizeof(ri))
e5794f54 309 err(EXIT_FAILURE, _("error writing inode"));
eb63b9b8 310
6496cb07 311 if (lseek(fd, (1 + ino_blocks) * BFS_BLOCKSIZE, SEEK_SET) == -1)
e5794f54 312 err(EXIT_FAILURE, _("seek error"));
eb63b9b8
KZ
313
314 memset(&de, 0, sizeof(de));
9d07f58d 315 de.d_ino = cpu_to_le16(BFS_ROOT_INO);
eb63b9b8
KZ
316 memcpy(de.d_name, ".", 1);
317 if (write(fd, &de, sizeof(de)) != sizeof(de))
e5794f54 318 err(EXIT_FAILURE, _("error writing . entry"));
eb63b9b8
KZ
319
320 memcpy(de.d_name, "..", 2);
321 if (write(fd, &de, sizeof(de)) != sizeof(de))
e5794f54 322 err(EXIT_FAILURE, _("error writing .. entry"));
eb63b9b8 323
0dfd626f 324 if (close_fd(fd) != 0)
c36f105d 325 err(EXIT_FAILURE, _("error closing %s"), device);
eb63b9b8 326
c36f105d 327 return EXIT_SUCCESS;
eb63b9b8 328}