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
11 #include <sys/types.h>
13 #include <sys/ioctl.h>
21 #define BFS_ROOT_INO 2
22 #define BFS_NAMELEN 14
23 #define BFS_BLOCKSIZE 512
24 #define BFS_SUPER_MAGIC 0x1badface
26 /* superblock - 512 bytes */
29 unsigned int s_start; /* byte offset of start of data */
30 unsigned int s_end; /* sizeof(slice)-1 */
32 /* for recovery during compaction */
33 int s_from, s_to; /* src and dest block of current transfer */
34 int s_backup_from, s_backup_to;
36 /* labels - may well contain garbage */
42 /* inode - 64 bytes */
45 unsigned char i_pad1[2];
46 unsigned long i_first_block;
47 unsigned long i_last_block;
48 unsigned long i_bytes_to_end;
49 unsigned long i_type; /* 1: file, 2: the unique dir */
51 unsigned long i_uid, i_gid;
52 unsigned long i_nlinks;
53 unsigned long i_atime, i_mtime, i_ctime;
54 unsigned char i_pad2[16];
57 #define BFS_DIR_TYPE 2
59 /* directory entry - 16 bytes */
62 char d_name[BFS_NAMELEN];
66 static char *progname;
74 fprintf(stderr, "\n%s: ", progname);
75 vfprintf(stderr, s, p);
77 fprintf(stderr, "\n");
84 "Usage: %s [-v] [-N nr-of-inodes] [-V volume-name]\n"
85 " [-F fsname] device [block-count]\n"),
91 main(int argc, char *argv[]) {
92 char *device, *volume, *fsname;
94 unsigned long long total_blocks, ino_bytes, ino_blocks, data_blocks;
95 unsigned long long user_specified_total_blocks = 0;
107 if ((p = strrchr(progname, '/')) != NULL)
114 (!strcmp(argv[1], "-V") || !strcmp(argv[1], "--version"))) {
115 printf(_("%s (%s)\n"), progname, PACKAGE_STRING);
119 volume = fsname = " "; /* is there a default? */
122 while ((c = getopt(argc, argv, "vF:N:V:cl:")) != -1) {
125 inodes = atol(optarg);
129 len = strlen(optarg);
130 if (len <= 0 || len > 6)
131 fatal(_("volume name too long"));
132 volume = strdup(optarg);
136 len = strlen(optarg);
137 if (len <= 0 || len > 6)
138 fatal(_("fsname name too long"));
139 fsname = strdup(optarg);
146 /* when called via mkfs we may get options c,l,v */
159 device = argv[optind++];
161 if (stat(device, &statbuf) == -1) {
163 fatal(_("cannot stat device %s"), device);
166 if (!S_ISBLK(statbuf.st_mode))
167 fatal(_("%s is not a block special device"), device);
169 fd = open(device, O_RDWR | O_EXCL);
172 fatal(_("cannot open %s"), device);
175 if (optind == argc-1)
176 user_specified_total_blocks = atoll(argv[optind]);
177 else if (optind != argc)
180 if (blkdev_get_sectors(fd, &total_blocks) == -1) {
181 if (!user_specified_total_blocks) {
182 perror("blkdev_get_sectors");
183 fatal(_("cannot get size of %s"), device);
185 total_blocks = user_specified_total_blocks;
186 } else if (user_specified_total_blocks) {
187 if (user_specified_total_blocks > total_blocks)
188 fatal(_("blocks argument too large, max is %llu"),
190 total_blocks = user_specified_total_blocks;
194 /* pick some reasonable default */
195 inodes = 8*(total_blocks/800);
201 /* believe the user */
203 fatal(_("too many inodes - max is 512"));
206 ino_bytes = inodes * sizeof(struct bfsi);
207 ino_blocks = (ino_bytes + BFS_BLOCKSIZE - 1) / BFS_BLOCKSIZE;
208 data_blocks = total_blocks - ino_blocks - 1;
210 /* mimic the behaviour of SCO's mkfs - maybe this limit is needed */
211 if (data_blocks < 32)
212 fatal(_("not enough space, need at least %llu blocks"),
215 memset(&sb, 0, sizeof(sb));
216 sb.s_magic = BFS_SUPER_MAGIC;
217 sb.s_start = ino_bytes + sizeof(struct bfssb);
218 sb.s_end = total_blocks * BFS_BLOCKSIZE - 1;
219 sb.s_from = sb.s_to = sb.s_backup_from = sb.s_backup_to = -1;
220 memcpy(sb.s_fsname, fsname, 6);
221 memcpy(sb.s_volume, volume, 6);
224 fprintf(stderr, _("Device: %s\n"), device);
225 fprintf(stderr, _("Volume: <%-6s>\n"), volume);
226 fprintf(stderr, _("FSname: <%-6s>\n"), fsname);
227 fprintf(stderr, _("BlockSize: %d\n"), BFS_BLOCKSIZE);
229 fprintf(stderr, _("Inodes: %d (in 1 block)\n"),
232 fprintf(stderr, _("Inodes: %d (in %lld blocks)\n"),
234 fprintf(stderr, _("Blocks: %lld\n"), total_blocks);
235 fprintf(stderr, _("Inode end: %d, Data end: %d\n"),
236 sb.s_start-1, sb.s_end);
239 if (write(fd, &sb, sizeof(sb)) != sizeof(sb))
240 fatal(_("error writing superblock"));
242 memset(&ri, 0, sizeof(ri));
243 ri.i_ino = BFS_ROOT_INO;
244 ri.i_first_block = 1 + ino_blocks;
245 ri.i_last_block = ri.i_first_block +
246 (inodes * sizeof(de) - 1) / BFS_BLOCKSIZE;
247 ri.i_bytes_to_end = ri.i_first_block * BFS_BLOCKSIZE
248 + 2 * sizeof(struct bfsde) - 1;
249 ri.i_type = BFS_DIR_TYPE;
250 ri.i_mode = S_IFDIR | 0755; /* or just 0755 */
252 ri.i_gid = 1; /* random */
259 if (write(fd, &ri, sizeof(ri)) != sizeof(ri))
260 fatal(_("error writing root inode"));
262 memset(&ri, 0, sizeof(ri));
263 for (i=1; i<inodes; i++)
264 if (write(fd, &ri, sizeof(ri)) != sizeof(ri))
265 fatal(_("error writing inode"));
267 if (lseek(fd, (1 + ino_blocks)*BFS_BLOCKSIZE, SEEK_SET) == -1)
268 fatal(_("seek error"));
270 memset(&de, 0, sizeof(de));
271 de.d_ino = BFS_ROOT_INO;
272 memcpy(de.d_name, ".", 1);
273 if (write(fd, &de, sizeof(de)) != sizeof(de))
274 fatal(_("error writing . entry"));
276 memcpy(de.d_name, "..", 2);
277 if (write(fd, &de, sizeof(de)) != sizeof(de))
278 fatal(_("error writing .. entry"));
280 if (close(fd) == -1) {
282 fatal(_("error closing %s"), device);