]> git.ipfire.org Git - thirdparty/util-linux.git/blob - disk-utils/mkfs.bfs.c
hexdump: check blocksize when display data
[thirdparty/util-linux.git] / disk-utils / mkfs.bfs.c
1 /*
2 * SPDX-License-Identifier: GPL-2.0-or-later
3 *
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
14 */
15 #include <errno.h>
16 #include <fcntl.h>
17 #include <getopt.h>
18 #include <limits.h>
19 #include <stdio.h>
20 #include <stdlib.h>
21 #include <string.h>
22 #include <sys/stat.h>
23 #include <time.h>
24 #include <unistd.h>
25
26 #include "blkdev.h"
27 #include "c.h"
28 #include "closestream.h"
29 #include "nls.h"
30 #include "strutils.h"
31 #include "xalloc.h"
32 #include "bitops.h"
33 #include "exitcodes.h"
34
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 */
41 struct bfssb {
42 uint32_t s_magic;
43 uint32_t s_start; /* byte offset of start of data */
44 uint32_t s_end; /* sizeof(slice)-1 */
45
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;
49
50 /* labels - may well contain garbage */
51 char s_fsname[6];
52 char s_volume[6];
53 char s_pad[472];
54 };
55
56 /* inode - 64 bytes */
57 struct bfsi {
58 uint16_t i_ino;
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 */
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;
68 unsigned char i_pad2[16];
69 };
70
71 #define BFS_DIR_TYPE 2
72
73 /* directory entry - 16 bytes */
74 struct bfsde {
75 uint16_t d_ino;
76 char d_name[BFS_NAMELEN];
77 };
78
79 static void __attribute__((__noreturn__)) usage(void)
80 {
81 FILE *out = stdout;
82 fprintf(out,
83 _("Usage: %s [options] device [block-count]\n"),
84 program_invocation_short_name);
85
86 fputs(USAGE_SEPARATOR, out);
87 fputs(_("Make an SCO bfs filesystem.\n"), out);
88
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"
97 ));
98 fprintf(out, USAGE_HELP_OPTIONS(21));
99
100 fprintf(out, USAGE_MAN_TAIL("mkfs.bfs(8)"));
101 exit(EXIT_SUCCESS);
102 }
103
104 int main(int argc, char **argv)
105 {
106 char *device, *volume, *fsname;
107 char *lockmode = 0;
108 long inodes;
109 unsigned long long total_blocks, ino_bytes, ino_blocks, data_blocks;
110 unsigned long long user_specified_total_blocks = 0;
111 int verbose = 0;
112 int fd;
113 uint32_t first_block;
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;
120
121 enum {
122 VERSION_OPTION = CHAR_MAX + 1,
123 OPT_LOCK
124 };
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},
133 {NULL, 0, NULL, 0}
134 };
135
136 setlocale(LC_ALL, "");
137 bindtextdomain(PACKAGE, LOCALEDIR);
138 textdomain(PACKAGE);
139 close_stdout_atexit();
140
141 if (argc < 2) {
142 warnx(_("not enough arguments"));
143 errtryhelp(EXIT_FAILURE);
144 }
145 if (argc == 2 && !strcmp(argv[1], "-V"))
146 print_version(EXIT_SUCCESS);
147
148 volume = fsname = " "; /* is there a default? */
149 inodes = 0;
150
151 while ((c = getopt_long(argc, argv, "N:V:F:vhcl", longopts, NULL)) != -1) {
152 switch (c) {
153 case 'N':
154 inodes = strtol_or_err(optarg, _("invalid number of inodes"));
155 break;
156
157 case 'V':
158 len = strlen(optarg);
159 if (len <= 0 || len > 6)
160 errx(EXIT_FAILURE, _("volume name too long"));
161 volume = xstrdup(optarg);
162 break;
163
164 case 'F':
165 len = strlen(optarg);
166 if (len <= 0 || len > 6)
167 errx(EXIT_FAILURE, _("fsname name too long"));
168 fsname = xstrdup(optarg);
169 break;
170
171 case 'v':
172 verbose = 1;
173 break;
174
175 case 'c':
176 case 'l':
177 /* when called via mkfs we may get options c,l,v */
178 break;
179
180 case OPT_LOCK:
181 lockmode = "1";
182 if (optarg) {
183 if (*optarg == '=')
184 optarg++;
185 lockmode = optarg;
186 }
187 break;
188
189 case VERSION_OPTION:
190 print_version(EXIT_SUCCESS);
191 case 'h':
192 usage();
193 default:
194 errtryhelp(EXIT_FAILURE);
195 }
196 }
197
198 if (optind == argc) {
199 warnx(_("no device specified"));
200 errtryhelp(EXIT_FAILURE);
201 }
202
203 device = argv[optind++];
204
205 if (stat(device, &statbuf) < 0)
206 err(EXIT_FAILURE, _("stat of %s failed"), device);
207
208 fd = open_blkdev_or_file(&statbuf, device, O_RDWR);
209 if (fd < 0)
210 err(EXIT_FAILURE, _("cannot open %s"), device);
211
212 if (blkdev_lock(fd, device, lockmode) != 0)
213 exit(MKFS_EX_ERROR);
214
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);
221 }
222
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)
229 errx(EXIT_FAILURE,
230 _("blocks argument too large, max is %llu"),
231 total_blocks);
232 total_blocks = user_specified_total_blocks;
233 }
234
235 if (!inodes) {
236 /* pick some reasonable default */
237 inodes = 8 * (total_blocks / 800);
238 if (inodes < 48)
239 inodes = 48;
240 if (512 < inodes)
241 inodes = 512;
242 } else {
243 /* believe the user */
244 if (512 < inodes)
245 errx(EXIT_FAILURE, _("too many inodes - max is 512"));
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
252 /* mimic the behavior of SCO's mkfs - maybe this limit is needed */
253 if (data_blocks < 32)
254 errx(EXIT_FAILURE,
255 _("not enough space, need at least %llu blocks"),
256 ino_blocks + 33);
257
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);
265
266 if (verbose) {
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);
271 if (ino_blocks == 1)
272 fprintf(stderr, _("Inodes: %ld (in 1 block)\n"),
273 inodes);
274 else
275 fprintf(stderr, _("Inodes: %ld (in %llu blocks)\n"),
276 inodes, ino_blocks);
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));
280 }
281
282 if (write(fd, &sb, sizeof(sb)) != sizeof(sb))
283 err(EXIT_FAILURE, _("error writing superblock"));
284
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 */
297 ri.i_nlinks = 2;
298 time(&now);
299 ri.i_atime = cpu_to_le32(now);
300 ri.i_mtime = cpu_to_le32(now);
301 ri.i_ctime = cpu_to_le32(now);
302
303 if (write(fd, &ri, sizeof(ri)) != sizeof(ri))
304 err(EXIT_FAILURE, _("error writing root inode"));
305
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"));
310
311 if (lseek(fd, (1 + ino_blocks) * BFS_BLOCKSIZE, SEEK_SET) == -1)
312 err(EXIT_FAILURE, _("seek error"));
313
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"));
319
320 memcpy(de.d_name, "..", 2);
321 if (write(fd, &de, sizeof(de)) != sizeof(de))
322 err(EXIT_FAILURE, _("error writing .. entry"));
323
324 if (close_fd(fd) != 0)
325 err(EXIT_FAILURE, _("error closing %s"), device);
326
327 return EXIT_SUCCESS;
328 }