2 * mkfs.bfs - Create SCO BFS filesystem - aeb, 1999-09-07
19 #include "closestream.h"
24 #define BFS_ROOT_INO 2
25 #define BFS_NAMELEN 14
26 #define BFS_BLOCKSIZE 512
27 #define BFS_SUPER_MAGIC 0x1badface
29 /* superblock - 512 bytes */
32 unsigned int s_start
; /* byte offset of start of data */
33 unsigned int s_end
; /* sizeof(slice)-1 */
35 /* for recovery during compaction */
36 int s_from
, s_to
; /* src and dest block of current transfer */
37 int s_backup_from
, s_backup_to
;
39 /* labels - may well contain garbage */
45 /* inode - 64 bytes */
48 unsigned char i_pad1
[2];
49 unsigned long i_first_block
;
50 unsigned long i_last_block
;
51 unsigned long i_bytes_to_end
;
52 unsigned long i_type
; /* 1: file, 2: the unique dir */
54 unsigned long i_uid
, i_gid
;
55 unsigned long i_nlinks
;
56 unsigned long i_atime
, i_mtime
, i_ctime
;
57 unsigned char i_pad2
[16];
60 #define BFS_DIR_TYPE 2
62 /* directory entry - 16 bytes */
65 char d_name
[BFS_NAMELEN
];
68 static void __attribute__ ((__noreturn__
)) usage(FILE * out
)
71 _("Usage: %s [options] device [block-count]\n"),
72 program_invocation_short_name
);
73 fprintf(out
, _("\nOptions:\n"
74 " -N, --inodes=NUM specify desired number of inodes\n"
75 " -V, --vname=NAME specify volume name\n"
76 " -F, --fname=NAME specify file system name\n"
77 " -v, --verbose explain what is being done\n"
78 " -c this option is silently ignored\n"
79 " -l this option is silently ignored\n"
80 " -V, --version output version information and exit\n"
81 " -V as version must be only option\n"
82 " -h, --help display this help and exit\n\n"));
84 exit(out
== stderr
? EXIT_FAILURE
: EXIT_SUCCESS
);
87 static void __attribute__ ((__noreturn__
)) print_version(void)
89 printf(UTIL_LINUX_VERSION
);
93 int main(int argc
, char **argv
)
95 char *device
, *volume
, *fsname
;
97 unsigned long long total_blocks
, ino_bytes
, ino_blocks
, data_blocks
;
98 unsigned long long user_specified_total_blocks
= 0;
108 enum { VERSION_OPTION
= CHAR_MAX
+ 1 };
109 static const struct option longopts
[] = {
110 {"inodes", required_argument
, NULL
, 'N'},
111 {"vname", required_argument
, NULL
, 'V'},
112 {"fname", required_argument
, NULL
, 'F'},
113 {"verbose", no_argument
, NULL
, 'v'},
114 {"version", no_argument
, NULL
, VERSION_OPTION
},
115 {"help", no_argument
, NULL
, 'h'},
119 setlocale(LC_ALL
, "");
120 bindtextdomain(PACKAGE
, LOCALEDIR
);
122 atexit(close_stdout
);
127 if (argc
== 2 && !strcmp(argv
[1], "-V"))
130 volume
= fsname
= " "; /* is there a default? */
133 while ((c
= getopt_long(argc
, argv
, "N:V:F:vhcl", longopts
, NULL
)) != -1) {
136 inodes
= strtol_or_err(optarg
, _("invalid number of inodes"));
140 len
= strlen(optarg
);
141 if (len
<= 0 || len
> 6)
142 errx(EXIT_FAILURE
, _("volume name too long"));
143 volume
= xstrdup(optarg
);
147 len
= strlen(optarg
);
148 if (len
<= 0 || len
> 6)
149 errx(EXIT_FAILURE
, _("fsname name too long"));
150 fsname
= xstrdup(optarg
);
159 /* when called via mkfs we may get options c,l,v */
174 device
= argv
[optind
++];
176 if (stat(device
, &statbuf
) < 0)
177 err(EXIT_FAILURE
, _("stat failed %s"), device
);
179 if (!S_ISBLK(statbuf
.st_mode
))
180 errx(EXIT_FAILURE
, _("%s is not a block special device"), device
);
182 fd
= open(device
, O_RDWR
| O_EXCL
);
184 err(EXIT_FAILURE
, _("cannot open %s"), device
);
186 if (optind
== argc
- 1)
187 user_specified_total_blocks
=
188 strtou64_or_err(argv
[optind
], _("invalid block-count"));
189 else if (optind
!= argc
)
192 if (blkdev_get_sectors(fd
, &total_blocks
) == -1) {
193 if (!user_specified_total_blocks
)
194 err(EXIT_FAILURE
, _("cannot get size of %s"), device
);
195 total_blocks
= user_specified_total_blocks
;
196 } else if (user_specified_total_blocks
) {
197 if (user_specified_total_blocks
> total_blocks
)
199 _("blocks argument too large, max is %llu"),
201 total_blocks
= user_specified_total_blocks
;
205 /* pick some reasonable default */
206 inodes
= 8 * (total_blocks
/ 800);
212 /* believe the user */
214 errx(EXIT_FAILURE
, _("too many inodes - max is 512"));
217 ino_bytes
= inodes
* sizeof(struct bfsi
);
218 ino_blocks
= (ino_bytes
+ BFS_BLOCKSIZE
- 1) / BFS_BLOCKSIZE
;
219 data_blocks
= total_blocks
- ino_blocks
- 1;
221 /* mimic the behaviour of SCO's mkfs - maybe this limit is needed */
222 if (data_blocks
< 32)
224 _("not enough space, need at least %llu blocks"),
227 memset(&sb
, 0, sizeof(sb
));
228 sb
.s_magic
= BFS_SUPER_MAGIC
;
229 sb
.s_start
= ino_bytes
+ sizeof(struct bfssb
);
230 sb
.s_end
= total_blocks
* BFS_BLOCKSIZE
- 1;
231 sb
.s_from
= sb
.s_to
= sb
.s_backup_from
= sb
.s_backup_to
= -1;
232 memcpy(sb
.s_fsname
, fsname
, 6);
233 memcpy(sb
.s_volume
, volume
, 6);
236 fprintf(stderr
, _("Device: %s\n"), device
);
237 fprintf(stderr
, _("Volume: <%-6s>\n"), volume
);
238 fprintf(stderr
, _("FSname: <%-6s>\n"), fsname
);
239 fprintf(stderr
, _("BlockSize: %d\n"), BFS_BLOCKSIZE
);
241 fprintf(stderr
, _("Inodes: %lu (in 1 block)\n"),
244 fprintf(stderr
, _("Inodes: %lu (in %llu blocks)\n"),
246 fprintf(stderr
, _("Blocks: %lld\n"), total_blocks
);
247 fprintf(stderr
, _("Inode end: %d, Data end: %d\n"),
248 sb
.s_start
- 1, sb
.s_end
);
251 if (write(fd
, &sb
, sizeof(sb
)) != sizeof(sb
))
252 err(EXIT_FAILURE
, _("error writing superblock"));
254 memset(&ri
, 0, sizeof(ri
));
255 ri
.i_ino
= BFS_ROOT_INO
;
256 ri
.i_first_block
= 1 + ino_blocks
;
257 ri
.i_last_block
= ri
.i_first_block
+
258 (inodes
* sizeof(de
) - 1) / BFS_BLOCKSIZE
;
259 ri
.i_bytes_to_end
= ri
.i_first_block
* BFS_BLOCKSIZE
260 + 2 * sizeof(struct bfsde
) - 1;
261 ri
.i_type
= BFS_DIR_TYPE
;
262 ri
.i_mode
= S_IFDIR
| 0755; /* or just 0755 */
264 ri
.i_gid
= 1; /* random */
271 if (write(fd
, &ri
, sizeof(ri
)) != sizeof(ri
))
272 err(EXIT_FAILURE
, _("error writing root inode"));
274 memset(&ri
, 0, sizeof(ri
));
275 for (i
= 1; i
< inodes
; i
++)
276 if (write(fd
, &ri
, sizeof(ri
)) != sizeof(ri
))
277 err(EXIT_FAILURE
, _("error writing inode"));
279 if (lseek(fd
, (1 + ino_blocks
) * BFS_BLOCKSIZE
, SEEK_SET
) == -1)
280 err(EXIT_FAILURE
, _("seek error"));
282 memset(&de
, 0, sizeof(de
));
283 de
.d_ino
= BFS_ROOT_INO
;
284 memcpy(de
.d_name
, ".", 1);
285 if (write(fd
, &de
, sizeof(de
)) != sizeof(de
))
286 err(EXIT_FAILURE
, _("error writing . entry"));
288 memcpy(de
.d_name
, "..", 2);
289 if (write(fd
, &de
, sizeof(de
)) != sizeof(de
))
290 err(EXIT_FAILURE
, _("error writing .. entry"));
293 err(EXIT_FAILURE
, _("error closing %s"), device
);