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