Whamcloud - gitweb
libext2fs: use fallocate for creating journals and hugefiles
[tools/e2fsprogs.git] / misc / mk_hugefiles.c
1 /*
2  * mk_hugefiles.c -- create huge files
3  */
4
5 #define _XOPEN_SOURCE 600 /* for inclusion of PATH_MAX in Solaris */
6 #define _BSD_SOURCE       /* for makedev() and major() */
7
8 #include "config.h"
9 #include <stdio.h>
10 #include <stdarg.h>
11 #include <string.h>
12 #include <strings.h>
13 #include <fcntl.h>
14 #include <ctype.h>
15 #include <time.h>
16 #ifdef __linux__
17 #include <sys/utsname.h>
18 #endif
19 #ifdef HAVE_GETOPT_H
20 #include <getopt.h>
21 #else
22 extern char *optarg;
23 extern int optind;
24 #endif
25 #ifdef HAVE_UNISTD_H
26 #include <unistd.h>
27 #endif
28 #ifdef HAVE_STDLIB_H
29 #include <stdlib.h>
30 #endif
31 #ifdef HAVE_ERRNO_H
32 #include <errno.h>
33 #endif
34 #include <sys/ioctl.h>
35 #include <sys/types.h>
36 #include <sys/stat.h>
37 #include <libgen.h>
38 #include <limits.h>
39 #include <blkid/blkid.h>
40
41 #include "ext2fs/ext2_fs.h"
42 #include "ext2fs/ext2fsP.h"
43 #include "et/com_err.h"
44 #include "uuid/uuid.h"
45 #include "e2p/e2p.h"
46 #include "ext2fs/ext2fs.h"
47 #include "util.h"
48 #include "profile.h"
49 #include "prof_err.h"
50 #include "nls-enable.h"
51 #include "mke2fs.h"
52
53 static int uid;
54 static int gid;
55 static blk64_t num_blocks;
56 static blk64_t num_slack;
57 static unsigned long num_files;
58 static blk64_t goal;
59 static char *fn_prefix;
60 static int idx_digits;
61 static char *fn_buf;
62 static char *fn_numbuf;
63 int zero_hugefile = 1;
64
65 #define SYSFS_PATH_LEN 256
66 typedef char sysfs_path_t[SYSFS_PATH_LEN];
67
68 #ifndef HAVE_SNPRINTF
69 /*
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.)
80  */
81 static int my_snprintf(char *str, size_t size, const char *format, ...)
82 {
83         va_list ap;
84         int ret;
85
86         va_start(ap, format);
87         ret = vsprintf(str, format, ap);
88         va_end(ap);
89         return ret;
90 }
91
92 #define snprintf my_snprintf
93 #endif
94
95 /*
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.
99  */
100 #ifndef makedev
101 #define makedev(maj, min) (((maj) << 8) + (min))
102 #endif
103
104 static char *search_sysfs_block(dev_t devno, sysfs_path_t ret_path)
105 {
106         struct dirent   *de, *p_de;
107         DIR             *dir = NULL, *p_dir = NULL;
108         FILE            *f;
109         sysfs_path_t    path, p_path;
110         unsigned int    major, minor;
111         char            *ret = ret_path;
112
113         ret_path[0] = 0;
114         if ((dir = opendir("/sys/block")) == NULL)
115                 return 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)
119                         continue;
120                 snprintf(path, SYSFS_PATH_LEN,
121                          "/sys/block/%s/dev", de->d_name);
122                 f = fopen(path, "r");
123                 if (f &&
124                     (fscanf(f, "%u:%u", &major, &minor) == 2)) {
125                         fclose(f); f = NULL;
126                         if (makedev(major, minor) == devno) {
127                                 snprintf(ret_path, SYSFS_PATH_LEN,
128                                          "/sys/block/%s", de->d_name);
129                                 goto success;
130                         }
131 #ifdef major
132                         if (major(devno) != major)
133                                 continue;
134 #endif
135                 }
136                 if (f)
137                         fclose(f);
138
139                 snprintf(path, SYSFS_PATH_LEN, "/sys/block/%s", de->d_name);
140
141                 if (p_dir)
142                         closedir(p_dir);
143                 if ((p_dir = opendir(path)) == NULL)
144                         continue;
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))
150                                 continue;
151                         snprintf(p_path, SYSFS_PATH_LEN, "%s/%s/dev",
152                                  path, p_de->d_name);
153
154                         f = fopen(p_path, "r");
155                         if (f &&
156                             (fscanf(f, "%u:%u", &major, &minor) == 2) &&
157                             (((major << 8) + minor) == devno)) {
158                                 fclose(f);
159                                 snprintf(ret_path, SYSFS_PATH_LEN, "%s/%s",
160                                          path, p_de->d_name);
161                                 goto success;
162                         }
163                         if (f)
164                                 fclose(f);
165                 }
166         }
167         ret = NULL;
168 success:
169         if (dir)
170                 closedir(dir);
171         if (p_dir)
172                 closedir(p_dir);
173         return ret;
174 }
175
176 static blk64_t get_partition_start(const char *device_name)
177 {
178         unsigned long long start;
179         sysfs_path_t    path;
180         struct stat     st;
181         FILE            *f;
182         char            *cp;
183         int             n;
184
185         if ((stat(device_name, &st) < 0) || !S_ISBLK(st.st_mode))
186                 return 0;
187
188         cp = search_sysfs_block(st.st_rdev, path);
189         if (!cp)
190                 return 0;
191         if (strlen(path) > SYSFS_PATH_LEN - sizeof("/start"))
192                 return 0;
193         strcat(path, "/start");
194         f = fopen(path, "r");
195         if (!f)
196                 return 0;
197         n = fscanf(f, "%llu", &start);
198         fclose(f);
199         return (n == 1) ? start : 0;
200 }
201
202 static errcode_t create_directory(ext2_filsys fs, char *dir,
203                                   ext2_ino_t *ret_ino)
204
205 {
206         struct ext2_inode       inode;
207         ext2_ino_t              ino = EXT2_ROOT_INO;
208         ext2_ino_t              newdir;
209         errcode_t               retval = 0;
210         char                    *fn, *cp, *next;
211
212         fn = malloc(strlen(dir) + 1);
213         if (fn == NULL)
214                 return ENOMEM;
215
216         strcpy(fn, dir);
217         cp = fn;
218         while(1) {
219                 next = strchr(cp, '/');
220                 if (next)
221                         *next++ = 0;
222                 if (*cp) {
223                         retval = ext2fs_new_inode(fs, ino, LINUX_S_IFDIR,
224                                                   NULL, &newdir);
225                         if (retval)
226                                 goto errout;
227
228                         retval = ext2fs_mkdir(fs, ino, newdir, cp);
229                         if (retval)
230                                 goto errout;
231
232                         ino = newdir;
233                         retval = ext2fs_read_inode(fs, ino, &inode);
234                         if (retval)
235                                 goto errout;
236
237                         inode.i_uid = uid & 0xFFFF;
238                         ext2fs_set_i_uid_high(inode, (uid >> 16) & 0xffff);
239                         inode.i_gid = gid & 0xFFFF;
240                         ext2fs_set_i_gid_high(inode, (gid >> 16) & 0xffff);
241                         retval = ext2fs_write_inode(fs, ino, &inode);
242                         if (retval)
243                                 goto errout;
244                 }
245                 if (next == NULL || *next == '\0')
246                         break;
247                 cp = next;
248         }
249 errout:
250         free(fn);
251         if (retval == 0)
252                 *ret_ino = ino;
253         return retval;
254 }
255
256 static errcode_t mk_hugefile(ext2_filsys fs, blk64_t num,
257                              ext2_ino_t dir, unsigned long idx, ext2_ino_t *ino)
258
259 {
260         errcode_t               retval;
261         struct ext2_inode       inode;
262
263         retval = ext2fs_new_inode(fs, 0, LINUX_S_IFREG, NULL, ino);
264         if (retval)
265                 return retval;
266
267         memset(&inode, 0, sizeof(struct ext2_inode));
268         inode.i_mode = LINUX_S_IFREG | (0666 & ~fs->umask);
269         inode.i_links_count = 1;
270         inode.i_uid = uid & 0xFFFF;
271         ext2fs_set_i_uid_high(inode, (uid >> 16) & 0xffff);
272         inode.i_gid = gid & 0xFFFF;
273         ext2fs_set_i_gid_high(inode, (gid >> 16) & 0xffff);
274
275         retval = ext2fs_write_new_inode(fs, *ino, &inode);
276         if (retval)
277                 return retval;
278
279         ext2fs_inode_alloc_stats2(fs, *ino, +1, 0);
280
281         if (EXT2_HAS_INCOMPAT_FEATURE(fs->super,
282                                       EXT3_FEATURE_INCOMPAT_EXTENTS))
283                 inode.i_flags |= EXT4_EXTENTS_FL;
284         retval = ext2fs_fallocate(fs,
285                                   EXT2_FALLOCATE_FORCE_INIT |
286                                   EXT2_FALLOCATE_ZERO_BLOCKS,
287                                   *ino, &inode, goal, 0, num);
288         if (retval)
289                 return retval;
290         retval = ext2fs_inode_size_set(fs, &inode, num * fs->blocksize);
291         if (retval)
292                 return retval;
293
294         retval = ext2fs_write_inode(fs, *ino, &inode);
295         if (retval)
296                 goto errout;
297
298         if (idx_digits)
299                 sprintf(fn_numbuf, "%0*lu", idx_digits, idx);
300         else if (num_files > 1)
301                 sprintf(fn_numbuf, "%lu", idx);
302
303 retry:
304         retval = ext2fs_link(fs, dir, fn_buf, *ino, EXT2_FT_REG_FILE);
305         if (retval == EXT2_ET_DIR_NO_SPACE) {
306                 retval = ext2fs_expand_dir(fs, dir);
307                 if (retval)
308                         goto errout;
309                 goto retry;
310         }
311
312 errout:
313         return retval;
314 }
315
316 static blk64_t calc_overhead(ext2_filsys fs, blk64_t num)
317 {
318         blk64_t e_blocks, e_blocks2, e_blocks3, e_blocks4;
319         int extents_per_block;
320         int extents = (num + EXT_INIT_MAX_LEN - 1) / EXT_INIT_MAX_LEN;
321
322         if (extents <= 4)
323                 return 0;
324
325         /*
326          * This calculation is due to the fact that we are inefficient
327          * in how handle extent splits when appending to the end of
328          * the extent tree.  Sigh.  We should fix this so that we can
329          * actually store 340 extents per 4k block, instead of only 170.
330          */
331         extents_per_block = ((fs->blocksize -
332                               sizeof(struct ext3_extent_header)) /
333                              sizeof(struct ext3_extent));
334         extents_per_block = (extents_per_block/ 2) - 1;
335
336         e_blocks = (extents + extents_per_block - 1) / extents_per_block;
337         e_blocks2 = (e_blocks + extents_per_block - 1) / extents_per_block;
338         e_blocks3 = (e_blocks2 + extents_per_block - 1) / extents_per_block;
339         e_blocks4 = (e_blocks3 + extents_per_block - 1) / extents_per_block;
340         return e_blocks + e_blocks2 + e_blocks3 + e_blocks4;
341 }
342
343 /*
344  * Find the place where we should start allocating blocks for the huge
345  * files.  Leave <slack> free blocks at the beginning of the file
346  * system for things like metadata blocks.
347  */
348 static blk64_t get_start_block(ext2_filsys fs, blk64_t slack)
349 {
350         errcode_t retval;
351         blk64_t blk = fs->super->s_first_data_block, next;
352         blk64_t last_blk = ext2fs_blocks_count(fs->super) - 1;
353
354         while (slack) {
355                 retval = ext2fs_find_first_zero_block_bitmap2(fs->block_map,
356                                                 blk, last_blk, &blk);
357                 if (retval)
358                         break;
359
360                 retval = ext2fs_find_first_set_block_bitmap2(fs->block_map,
361                                                 blk, last_blk, &next);
362                 if (retval)
363                         next = last_blk;
364
365                 if (next - blk > slack) {
366                         blk += slack;
367                         break;
368                 }
369
370                 slack -= (next - blk);
371                 blk = next;
372         }
373         return blk;
374 }
375
376 static blk64_t round_up_align(blk64_t b, unsigned long align,
377                               blk64_t part_offset)
378 {
379         unsigned long m;
380
381         if (align == 0)
382                 return b;
383         part_offset = part_offset % align;
384         m = (b + part_offset) % align;
385         if (m)
386                 b += align - m;
387         return b;
388 }
389
390 errcode_t mk_hugefiles(ext2_filsys fs, const char *device_name)
391 {
392         unsigned long   i;
393         ext2_ino_t      dir;
394         errcode_t       retval;
395         blk64_t         fs_blocks, part_offset = 0;
396         unsigned long   align;
397         int             d, dsize;
398         char            *t;
399
400         if (!get_bool_from_profile(fs_types, "make_hugefiles", 0))
401                 return 0;
402
403         if (!EXT2_HAS_INCOMPAT_FEATURE(fs->super,
404                                        EXT3_FEATURE_INCOMPAT_EXTENTS))
405                 return EXT2_ET_EXTENT_NOT_SUPPORTED;
406
407         uid = get_int_from_profile(fs_types, "hugefiles_uid", 0);
408         gid = get_int_from_profile(fs_types, "hugefiles_gid", 0);
409         fs->umask = get_int_from_profile(fs_types, "hugefiles_umask", 077);
410         num_files = get_int_from_profile(fs_types, "num_hugefiles", 0);
411         t = get_string_from_profile(fs_types, "hugefiles_slack", "1M");
412         num_slack = parse_num_blocks2(t, fs->super->s_log_block_size);
413         free(t);
414         t = get_string_from_profile(fs_types, "hugefiles_size", "0");
415         num_blocks = parse_num_blocks2(t, fs->super->s_log_block_size);
416         free(t);
417         t = get_string_from_profile(fs_types, "hugefiles_align", "0");
418         align = parse_num_blocks2(t, fs->super->s_log_block_size);
419         free(t);
420         if (get_bool_from_profile(fs_types, "hugefiles_align_disk", 0)) {
421                 part_offset = get_partition_start(device_name) /
422                         (fs->blocksize / 512);
423                 if (part_offset % EXT2FS_CLUSTER_RATIO(fs)) {
424                         fprintf(stderr,
425                                 _("Partition offset of %llu (%uk) blocks "
426                                   "not compatible with cluster size %u.\n"),
427                                 part_offset, fs->blocksize,
428                                 EXT2_CLUSTER_SIZE(fs->super));
429                         exit(1);
430                 }
431         }
432         num_blocks = round_up_align(num_blocks, align, 0);
433         zero_hugefile = get_bool_from_profile(fs_types, "zero_hugefiles",
434                                               zero_hugefile);
435
436         t = get_string_from_profile(fs_types, "hugefiles_dir", "/");
437         retval = create_directory(fs, t, &dir);
438         free(t);
439         if (retval)
440                 return retval;
441
442         fn_prefix = get_string_from_profile(fs_types, "hugefiles_name",
443                                             "hugefile");
444         idx_digits = get_int_from_profile(fs_types, "hugefiles_digits", 5);
445         d = int_log10(num_files) + 1;
446         if (idx_digits > d)
447                 d = idx_digits;
448         dsize = strlen(fn_prefix) + d + 16;
449         fn_buf = malloc(dsize);
450         if (!fn_buf) {
451                 free(fn_prefix);
452                 return ENOMEM;
453         }
454         strcpy(fn_buf, fn_prefix);
455         fn_numbuf = fn_buf + strlen(fn_prefix);
456         free(fn_prefix);
457
458         fs_blocks = ext2fs_free_blocks_count(fs->super);
459         if (fs_blocks < num_slack + align)
460                 return ENOSPC;
461         fs_blocks -= num_slack + align;
462         if (num_blocks && num_blocks > fs_blocks)
463                 return ENOSPC;
464         if (num_blocks == 0 && num_files == 0)
465                 num_files = 1;
466
467         if (num_files == 0 && num_blocks) {
468                 num_files = fs_blocks / num_blocks;
469                 fs_blocks -= (num_files / 16) + 1;
470                 fs_blocks -= calc_overhead(fs, num_blocks) * num_files;
471                 num_files = fs_blocks / num_blocks;
472         }
473
474         if (num_blocks == 0 && num_files > 1) {
475                 num_blocks = fs_blocks / num_files;
476                 fs_blocks -= (num_files / 16) + 1;
477                 fs_blocks -= calc_overhead(fs, num_blocks) * num_files;
478                 num_blocks = fs_blocks / num_files;
479         }
480
481         num_slack += calc_overhead(fs, num_blocks) * num_files;
482         num_slack += (num_files / 16) + 1; /* space for dir entries */
483         goal = get_start_block(fs, num_slack);
484         goal = round_up_align(goal, align, part_offset);
485
486         if ((num_blocks ? num_blocks : fs_blocks) >
487             (0x80000000UL / fs->blocksize))
488                 fs->super->s_feature_ro_compat |=
489                         EXT2_FEATURE_RO_COMPAT_LARGE_FILE;
490
491         if (!quiet) {
492                 if (zero_hugefile && verbose)
493                         printf("%s", _("Huge files will be zero'ed\n"));
494                 printf(_("Creating %lu huge file(s) "), num_files);
495                 if (num_blocks)
496                         printf(_("with %llu blocks each"), num_blocks);
497                 fputs(": ", stdout);
498         }
499         if (num_blocks == 0)
500                 num_blocks = ext2fs_blocks_count(fs->super) - goal;
501         for (i=0; i < num_files; i++) {
502                 ext2_ino_t ino;
503
504                 retval = mk_hugefile(fs, num_blocks, dir, i, &ino);
505                 if (retval) {
506                         com_err(program_name, retval,
507                                 _("while creating huge file %lu"), i);
508                         goto errout;
509                 }
510         }
511         if (!quiet)
512                 fputs(_("done\n"), stdout);
513
514 errout:
515         free(fn_buf);
516         return retval;
517 }