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