2 * mk_hugefiles.c -- create huge files
5 #define _XOPEN_SOURCE 600 /* for inclusion of PATH_MAX in Solaris */
6 #define _BSD_SOURCE /* for makedev() and major() */
17 #include <sys/utsname.h>
34 #include <sys/ioctl.h>
35 #include <sys/types.h>
39 #include <blkid/blkid.h>
41 #include "ext2fs/ext2_fs.h"
42 #include "ext2fs/ext2fsP.h"
43 #include "et/com_err.h"
44 #include "uuid/uuid.h"
46 #include "ext2fs/ext2fs.h"
50 #include "nls-enable.h"
55 static blk64_t num_blocks;
56 static blk64_t num_slack;
57 static unsigned long num_files;
59 static char *fn_prefix;
60 static int idx_digits;
62 static char *fn_numbuf;
63 int zero_hugefile = 1;
65 #define SYSFS_PATH_LEN 256
66 typedef char sysfs_path_t[SYSFS_PATH_LEN];
70 * We are very careful to avoid needing to worry about buffer
71 * overflows, so we don't really need to use snprintf() except as an
72 * additional safety check. So if snprintf() is not present, it's
73 * safe to fall back to vsprintf(). This provides portability since
74 * vsprintf() is guaranteed by C89, while snprintf() is only
75 * guaranteed by C99 --- which for example, Microsoft Visual Studio
76 * has *still* not bothered to implement. :-/ (Not that I expect
77 * mke2fs to be ported to MS Visual Studio any time soon, but
78 * libext2fs *does* get built on Microsoft platforms, and we might
79 * want to move this into libext2fs some day.)
81 static int my_snprintf(char *str, size_t size, const char *format, ...)
87 ret = vsprintf(str, format, ap);
92 #define snprintf my_snprintf
96 * Fall back to Linux's definitions of makedev and major are needed.
97 * The search_sysfs_block() function is highly unlikely to work on
98 * non-Linux systems anyway.
101 #define makedev(maj, min) (((maj) << 8) + (min))
104 static char *search_sysfs_block(dev_t devno, sysfs_path_t ret_path)
106 struct dirent *de, *p_de;
107 DIR *dir = NULL, *p_dir = NULL;
109 sysfs_path_t path, p_path;
110 unsigned int major, minor;
111 char *ret = ret_path;
114 if ((dir = opendir("/sys/block")) == NULL)
116 while ((de = readdir(dir)) != NULL) {
117 if (!strcmp(de->d_name, ".") || !strcmp(de->d_name, "..") ||
118 strlen(de->d_name) > sizeof(path)-32)
120 snprintf(path, SYSFS_PATH_LEN,
121 "/sys/block/%s/dev", de->d_name);
122 f = fopen(path, "r");
124 (fscanf(f, "%u:%u", &major, &minor) == 2)) {
126 if (makedev(major, minor) == devno) {
127 snprintf(ret_path, SYSFS_PATH_LEN,
128 "/sys/block/%s", de->d_name);
132 if (major(devno) != major)
139 snprintf(path, SYSFS_PATH_LEN, "/sys/block/%s", de->d_name);
143 if ((p_dir = opendir(path)) == NULL)
145 while ((p_de = readdir(p_dir)) != NULL) {
146 if (!strcmp(p_de->d_name, ".") ||
147 !strcmp(p_de->d_name, "..") ||
148 (strlen(p_de->d_name) >
149 SYSFS_PATH_LEN - strlen(path) - 32))
151 snprintf(p_path, SYSFS_PATH_LEN, "%s/%s/dev",
154 f = fopen(p_path, "r");
156 (fscanf(f, "%u:%u", &major, &minor) == 2) &&
157 (((major << 8) + minor) == devno)) {
159 snprintf(ret_path, SYSFS_PATH_LEN, "%s/%s",
176 static blk64_t get_partition_start(const char *device_name)
178 unsigned long long start;
185 if ((stat(device_name, &st) < 0) || !S_ISBLK(st.st_mode))
188 cp = search_sysfs_block(st.st_rdev, path);
191 strncat(path, "/start", SYSFS_PATH_LEN);
192 f = fopen(path, "r");
195 n = fscanf(f, "%llu", &start);
197 return (n == 1) ? start : 0;
200 static errcode_t create_directory(ext2_filsys fs, char *dir,
204 struct ext2_inode inode;
205 ext2_ino_t ino = EXT2_ROOT_INO;
207 errcode_t retval = 0;
208 char *fn, *cp, *next;
210 fn = malloc(strlen(dir) + 1);
217 next = strchr(cp, '/');
221 retval = ext2fs_new_inode(fs, ino, LINUX_S_IFDIR,
226 retval = ext2fs_mkdir(fs, ino, newdir, cp);
231 retval = ext2fs_read_inode(fs, ino, &inode);
235 inode.i_uid = uid & 0xFFFF;
236 ext2fs_set_i_uid_high(inode, (uid >> 16) & 0xffff);
237 inode.i_gid = gid & 0xFFFF;
238 ext2fs_set_i_gid_high(inode, (gid >> 16) & 0xffff);
239 retval = ext2fs_write_inode(fs, ino, &inode);
243 if (next == NULL || *next == '\0')
254 static errcode_t mk_hugefile(ext2_filsys fs, blk64_t num,
255 ext2_ino_t dir, unsigned long idx, ext2_ino_t *ino)
259 blk64_t lblk, bend = 0;
263 struct ext2_inode inode;
264 ext2_extent_handle_t handle;
266 retval = ext2fs_new_inode(fs, 0, LINUX_S_IFREG, NULL, ino);
270 memset(&inode, 0, sizeof(struct ext2_inode));
271 inode.i_mode = LINUX_S_IFREG | (0666 & ~fs->umask);
272 inode.i_links_count = 1;
273 inode.i_uid = uid & 0xFFFF;
274 ext2fs_set_i_uid_high(inode, (uid >> 16) & 0xffff);
275 inode.i_gid = gid & 0xFFFF;
276 ext2fs_set_i_gid_high(inode, (gid >> 16) & 0xffff);
278 retval = ext2fs_write_new_inode(fs, *ino, &inode);
282 ext2fs_inode_alloc_stats2(fs, *ino, +1, 0);
284 retval = ext2fs_extent_open2(fs, *ino, &inode, &handle);
289 left = num ? num : 1;
294 retval = ext2fs_find_first_zero_block_bitmap2(fs->block_map,
295 goal, ext2fs_blocks_count(fs->super) - 1, &end);
300 retval = ext2fs_find_first_set_block_bitmap2(fs->block_map, goal,
301 ext2fs_blocks_count(fs->super) - 1, &bend);
302 if (retval == ENOENT) {
303 bend = ext2fs_blocks_count(fs->super);
307 if (!num || bend - goal < left)
314 ext2fs_block_alloc_stats_range(fs, pblk, n, +1);
318 retval = ext2fs_zero_blocks2(fs, pblk, n,
322 com_err(program_name, retval,
323 _("while zeroing block %llu "
324 "for hugefile"), ret_blk);
329 struct ext2fs_extent newextent;
331 if (l > EXT_INIT_MAX_LEN)
332 l = EXT_INIT_MAX_LEN;
335 newextent.e_pblk = pblk;
336 newextent.e_lblk = lblk;
337 newextent.e_flags = 0;
339 retval = ext2fs_extent_insert(handle,
340 EXT2_EXTENT_INSERT_AFTER, &newextent);
349 retval = ext2fs_read_inode(fs, *ino, &inode);
353 retval = ext2fs_iblk_add_blocks(fs, &inode,
354 count / EXT2FS_CLUSTER_RATIO(fs));
357 size = (__u64) count * fs->blocksize;
358 inode.i_size = size & 0xffffffff;
359 inode.i_size_high = (size >> 32);
361 retval = ext2fs_write_new_inode(fs, *ino, &inode);
366 sprintf(fn_numbuf, "%0*lu", idx_digits, idx);
367 else if (num_files > 1)
368 sprintf(fn_numbuf, "%lu", idx);
371 retval = ext2fs_link(fs, dir, fn_buf, *ino, EXT2_FT_REG_FILE);
372 if (retval == EXT2_ET_DIR_NO_SPACE) {
373 retval = ext2fs_expand_dir(fs, dir);
384 ext2fs_extent_free(handle);
389 static blk64_t calc_overhead(ext2_filsys fs, blk64_t num)
391 blk64_t e_blocks, e_blocks2, e_blocks3, e_blocks4;
392 int extents_per_block;
393 int extents = (num + EXT_INIT_MAX_LEN - 1) / EXT_INIT_MAX_LEN;
399 * This calculation is due to the fact that we are inefficient
400 * in how handle extent splits when appending to the end of
401 * the extent tree. Sigh. We should fix this so that we can
402 * actually store 340 extents per 4k block, instead of only 170.
404 extents_per_block = ((fs->blocksize -
405 sizeof(struct ext3_extent_header)) /
406 sizeof(struct ext3_extent));
407 extents_per_block = (extents_per_block/ 2) - 1;
409 e_blocks = (extents + extents_per_block - 1) / extents_per_block;
410 e_blocks2 = (e_blocks + extents_per_block - 1) / extents_per_block;
411 e_blocks3 = (e_blocks2 + extents_per_block - 1) / extents_per_block;
412 e_blocks4 = (e_blocks3 + extents_per_block - 1) / extents_per_block;
413 return e_blocks + e_blocks2 + e_blocks3 + e_blocks4;
417 * Find the place where we should start allocating blocks for the huge
418 * files. Leave <slack> free blocks at the beginning of the file
419 * system for things like metadata blocks.
421 static blk64_t get_start_block(ext2_filsys fs, blk64_t slack)
424 blk64_t blk = fs->super->s_first_data_block, next;
425 blk64_t last_blk = ext2fs_blocks_count(fs->super) - 1;
428 retval = ext2fs_find_first_zero_block_bitmap2(fs->block_map,
429 blk, last_blk, &blk);
433 retval = ext2fs_find_first_set_block_bitmap2(fs->block_map,
434 blk, last_blk, &next);
439 if (next - blk > slack) {
444 slack -= (next - blk);
450 static blk64_t round_up_align(blk64_t b, unsigned long align,
457 part_offset = part_offset % align;
458 m = (b + part_offset) % align;
464 errcode_t mk_hugefiles(ext2_filsys fs, const char *device_name)
469 blk64_t fs_blocks, part_offset;
474 if (!get_bool_from_profile(fs_types, "make_hugefiles", 0))
477 uid = get_int_from_profile(fs_types, "hugefiles_uid", 0);
478 gid = get_int_from_profile(fs_types, "hugefiles_gid", 0);
479 fs->umask = get_int_from_profile(fs_types, "hugefiles_umask", 077);
480 num_files = get_int_from_profile(fs_types, "num_hugefiles", 0);
481 t = get_string_from_profile(fs_types, "hugefiles_slack", "1M");
482 num_slack = parse_num_blocks2(t, fs->super->s_log_block_size);
484 t = get_string_from_profile(fs_types, "hugefiles_size", "0");
485 num_blocks = parse_num_blocks2(t, fs->super->s_log_block_size);
487 t = get_string_from_profile(fs_types, "hugefiles_align", "0");
488 align = parse_num_blocks2(t, fs->super->s_log_block_size);
490 if (get_bool_from_profile(fs_types, "hugefiles_align_disk", 0))
491 part_offset = get_partition_start(device_name) /
492 (fs->blocksize / 512);
493 num_blocks = round_up_align(num_blocks, align, 0);
494 zero_hugefile = get_bool_from_profile(fs_types, "zero_hugefiles",
497 t = get_string_from_profile(fs_types, "hugefiles_dir", "/");
498 retval = create_directory(fs, t, &dir);
503 fn_prefix = get_string_from_profile(fs_types, "hugefiles_name",
505 idx_digits = get_int_from_profile(fs_types, "hugefiles_digits", 5);
506 d = int_log10(num_files) + 1;
509 dsize = strlen(fn_prefix) + d + 16;
510 fn_buf = malloc(dsize);
515 strcpy(fn_buf, fn_prefix);
516 fn_numbuf = fn_buf + strlen(fn_prefix);
519 fs_blocks = ext2fs_free_blocks_count(fs->super);
520 if (fs_blocks < num_slack + align)
522 fs_blocks -= num_slack + align;
523 if (num_blocks && num_blocks > fs_blocks)
525 if (num_blocks == 0 && num_files == 0)
528 if (num_files == 0 && num_blocks) {
529 num_files = fs_blocks / num_blocks;
530 fs_blocks -= (num_files / 16) + 1;
531 fs_blocks -= calc_overhead(fs, num_blocks) * num_files;
532 num_files = fs_blocks / num_blocks;
535 if (num_blocks == 0 && num_files > 1) {
536 num_blocks = fs_blocks / num_files;
537 fs_blocks -= (num_files / 16) + 1;
538 fs_blocks -= calc_overhead(fs, num_blocks) * num_files;
539 num_blocks = fs_blocks / num_files;
542 num_slack += calc_overhead(fs, num_blocks) * num_files;
543 num_slack += (num_files / 16) + 1; /* space for dir entries */
544 goal = get_start_block(fs, num_slack);
545 goal = round_up_align(goal, align, part_offset);
547 if ((num_blocks ? num_blocks : fs_blocks) >
548 (0x80000000UL / fs->blocksize))
549 fs->super->s_feature_ro_compat |=
550 EXT2_FEATURE_RO_COMPAT_LARGE_FILE;
553 if (zero_hugefile && verbose)
554 printf("%s", _("Huge files will be zero'ed\n"));
555 printf(_("Creating %lu huge file(s) "), num_files);
557 printf(_("with %llu blocks each"), num_blocks);
560 for (i=0; i < num_files; i++) {
563 retval = mk_hugefile(fs, num_blocks, dir, i, &ino);
565 com_err(program_name, retval,
566 _("while creating huge file %lu"), i);
571 fputs(_("done\n"), stdout);