2 * mkfs.bfs - Create SCO BFS filesystem - aeb, 1999-09-07
4 * Usage: mkfs.bfs [-N nr-of-inodes] [-V volume-name] [-F fsname] device
20 #include "closestream.h"
26 #define BFS_ROOT_INO 2
27 #define BFS_NAMELEN 14
28 #define BFS_BLOCKSIZE 512
29 #define BFS_SUPER_MAGIC 0x1badface
31 /* superblock - 512 bytes */
34 uint32_t s_start
; /* byte offset of start of data */
35 uint32_t s_end
; /* sizeof(slice)-1 */
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
;
41 /* labels - may well contain garbage */
47 /* inode - 64 bytes */
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 */
56 uint32_t i_uid
, i_gid
;
58 uint32_t i_atime
, i_mtime
, i_ctime
;
59 unsigned char i_pad2
[16];
62 #define BFS_DIR_TYPE 2
64 /* directory entry - 16 bytes */
67 char d_name
[BFS_NAMELEN
];
70 static void __attribute__ ((__noreturn__
)) usage(FILE * out
)
73 _("Usage: %s [options] device [block-count]\n"),
74 program_invocation_short_name
);
75 fprintf(out
, _("\nOptions:\n"
76 " -N, --inodes=NUM specify desired number of inodes\n"
77 " -V, --vname=NAME specify volume name\n"
78 " -F, --fname=NAME specify file system name\n"
79 " -v, --verbose explain what is being done\n"
80 " -c this option is silently ignored\n"
81 " -l this option is silently ignored\n"
82 " -V, --version output version information and exit\n"
83 " -V as version must be only option\n"
84 " -h, --help display this help and exit\n\n"));
86 exit(out
== stderr
? EXIT_FAILURE
: EXIT_SUCCESS
);
89 static void __attribute__ ((__noreturn__
)) print_version(void)
91 printf(UTIL_LINUX_VERSION
);
95 int main(int argc
, char **argv
)
97 char *device
, *volume
, *fsname
;
99 unsigned long long total_blocks
, ino_bytes
, ino_blocks
, data_blocks
;
100 unsigned long long user_specified_total_blocks
= 0;
103 uint32_t first_block
;
111 enum { VERSION_OPTION
= CHAR_MAX
+ 1 };
112 static const struct option longopts
[] = {
113 {"inodes", required_argument
, NULL
, 'N'},
114 {"vname", required_argument
, NULL
, 'V'},
115 {"fname", required_argument
, NULL
, 'F'},
116 {"verbose", no_argument
, NULL
, 'v'},
117 {"version", no_argument
, NULL
, VERSION_OPTION
},
118 {"help", no_argument
, NULL
, 'h'},
122 setlocale(LC_ALL
, "");
123 bindtextdomain(PACKAGE
, LOCALEDIR
);
125 atexit(close_stdout
);
130 if (argc
== 2 && !strcmp(argv
[1], "-V"))
133 volume
= fsname
= " "; /* is there a default? */
136 while ((c
= getopt_long(argc
, argv
, "N:V:F:vhcl", longopts
, NULL
)) != -1) {
139 inodes
= strtol_or_err(optarg
, _("invalid number of inodes"));
143 len
= strlen(optarg
);
144 if (len
<= 0 || len
> 6)
145 errx(EXIT_FAILURE
, _("volume name too long"));
146 volume
= xstrdup(optarg
);
150 len
= strlen(optarg
);
151 if (len
<= 0 || len
> 6)
152 errx(EXIT_FAILURE
, _("fsname name too long"));
153 fsname
= xstrdup(optarg
);
162 /* when called via mkfs we may get options c,l,v */
177 device
= argv
[optind
++];
179 if (stat(device
, &statbuf
) < 0)
180 err(EXIT_FAILURE
, _("stat failed %s"), device
);
182 if (!S_ISBLK(statbuf
.st_mode
))
183 errx(EXIT_FAILURE
, _("%s is not a block special device"), device
);
185 fd
= open(device
, O_RDWR
| O_EXCL
);
187 err(EXIT_FAILURE
, _("cannot open %s"), device
);
189 if (optind
== argc
- 1)
190 user_specified_total_blocks
=
191 strtou64_or_err(argv
[optind
], _("invalid block-count"));
192 else if (optind
!= argc
)
195 if (blkdev_get_sectors(fd
, &total_blocks
) == -1) {
196 if (!user_specified_total_blocks
)
197 err(EXIT_FAILURE
, _("cannot get size of %s"), device
);
198 total_blocks
= user_specified_total_blocks
;
199 } else if (user_specified_total_blocks
) {
200 if (user_specified_total_blocks
> total_blocks
)
202 _("blocks argument too large, max is %llu"),
204 total_blocks
= user_specified_total_blocks
;
208 /* pick some reasonable default */
209 inodes
= 8 * (total_blocks
/ 800);
215 /* believe the user */
217 errx(EXIT_FAILURE
, _("too many inodes - max is 512"));
220 ino_bytes
= inodes
* sizeof(struct bfsi
);
221 ino_blocks
= (ino_bytes
+ BFS_BLOCKSIZE
- 1) / BFS_BLOCKSIZE
;
222 data_blocks
= total_blocks
- ino_blocks
- 1;
224 /* mimic the behaviour of SCO's mkfs - maybe this limit is needed */
225 if (data_blocks
< 32)
227 _("not enough space, need at least %llu blocks"),
230 memset(&sb
, 0, sizeof(sb
));
231 sb
.s_magic
= cpu_to_le32(BFS_SUPER_MAGIC
);
232 sb
.s_start
= cpu_to_le32(ino_bytes
+ sizeof(struct bfssb
));
233 sb
.s_end
= cpu_to_le32(total_blocks
* BFS_BLOCKSIZE
- 1);
234 sb
.s_from
= sb
.s_to
= sb
.s_backup_from
= sb
.s_backup_to
= -1;
235 memcpy(sb
.s_fsname
, fsname
, 6);
236 memcpy(sb
.s_volume
, volume
, 6);
239 fprintf(stderr
, _("Device: %s\n"), device
);
240 fprintf(stderr
, _("Volume: <%-6s>\n"), volume
);
241 fprintf(stderr
, _("FSname: <%-6s>\n"), fsname
);
242 fprintf(stderr
, _("BlockSize: %d\n"), BFS_BLOCKSIZE
);
244 fprintf(stderr
, _("Inodes: %lu (in 1 block)\n"),
247 fprintf(stderr
, _("Inodes: %lu (in %llu blocks)\n"),
249 fprintf(stderr
, _("Blocks: %lld\n"), total_blocks
);
250 fprintf(stderr
, _("Inode end: %d, Data end: %d\n"),
251 le32_to_cpu(sb
.s_start
) - 1, le32_to_cpu(sb
.s_end
));
254 if (write(fd
, &sb
, sizeof(sb
)) != sizeof(sb
))
255 err(EXIT_FAILURE
, _("error writing superblock"));
257 memset(&ri
, 0, sizeof(ri
));
258 ri
.i_ino
= cpu_to_le16(BFS_ROOT_INO
);
259 first_block
= 1 + ino_blocks
;
260 ri
.i_first_block
= cpu_to_le32(first_block
);
261 ri
.i_last_block
= cpu_to_le32(first_block
+
262 (inodes
* sizeof(de
) - 1) / BFS_BLOCKSIZE
);
263 ri
.i_bytes_to_end
= cpu_to_le32(first_block
* BFS_BLOCKSIZE
264 + 2 * sizeof(struct bfsde
) - 1);
265 ri
.i_type
= cpu_to_le32(BFS_DIR_TYPE
);
266 ri
.i_mode
= cpu_to_le32(S_IFDIR
| 0755); /* or just 0755 */
267 ri
.i_uid
= cpu_to_le32(0);
268 ri
.i_gid
= cpu_to_le32(1); /* random */
271 ri
.i_atime
= cpu_to_le32(now
);
272 ri
.i_mtime
= cpu_to_le32(now
);
273 ri
.i_ctime
= cpu_to_le32(now
);
275 if (write(fd
, &ri
, sizeof(ri
)) != sizeof(ri
))
276 err(EXIT_FAILURE
, _("error writing root inode"));
278 memset(&ri
, 0, sizeof(ri
));
279 for (i
= 1; i
< inodes
; i
++)
280 if (write(fd
, &ri
, sizeof(ri
)) != sizeof(ri
))
281 err(EXIT_FAILURE
, _("error writing inode"));
283 if (lseek(fd
, (1 + ino_blocks
) * BFS_BLOCKSIZE
, SEEK_SET
) == -1)
284 err(EXIT_FAILURE
, _("seek error"));
286 memset(&de
, 0, sizeof(de
));
287 de
.d_ino
= cpu_to_le16(BFS_ROOT_INO
);
288 memcpy(de
.d_name
, ".", 1);
289 if (write(fd
, &de
, sizeof(de
)) != sizeof(de
))
290 err(EXIT_FAILURE
, _("error writing . entry"));
292 memcpy(de
.d_name
, "..", 2);
293 if (write(fd
, &de
, sizeof(de
)) != sizeof(de
))
294 err(EXIT_FAILURE
, _("error writing .. entry"));
296 if (close_fd(fd
) != 0)
297 err(EXIT_FAILURE
, _("error closing %s"), device
);