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