Whamcloud - gitweb
LU-5307 build: fix unused/uninitialized variable warnings
[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         strncat(path, "/start", SYSFS_PATH_LEN-1);
192         f = fopen(path, "r");
193         if (!f)
194                 return 0;
195         n = fscanf(f, "%llu", &start);
196         fclose(f);
197         return (n == 1) ? start : 0;
198 }
199
200 static errcode_t create_directory(ext2_filsys fs, char *dir,
201                                   ext2_ino_t *ret_ino)
202
203 {
204         struct ext2_inode       inode;
205         ext2_ino_t              ino = EXT2_ROOT_INO;
206         ext2_ino_t              newdir;
207         errcode_t               retval = 0;
208         char                    *fn, *cp, *next;
209
210         fn = malloc(strlen(dir) + 1);
211         if (fn == NULL)
212                 return ENOMEM;
213
214         strcpy(fn, dir);
215         cp = fn;
216         while(1) {
217                 next = strchr(cp, '/');
218                 if (next)
219                         *next++ = 0;
220                 if (*cp) {
221                         retval = ext2fs_new_inode(fs, ino, LINUX_S_IFDIR,
222                                                   NULL, &newdir);
223                         if (retval)
224                                 goto errout;
225
226                         retval = ext2fs_mkdir(fs, ino, newdir, cp);
227                         if (retval)
228                                 goto errout;
229
230                         ino = newdir;
231                         retval = ext2fs_read_inode(fs, ino, &inode);
232                         if (retval)
233                                 goto errout;
234
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);
240                         if (retval)
241                                 goto errout;
242                 }
243                 if (next == NULL || *next == '\0')
244                         break;
245                 cp = next;
246         }
247 errout:
248         free(fn);
249         if (retval == 0)
250                 *ret_ino = ino;
251         return retval;
252 }
253
254 static errcode_t mk_hugefile(ext2_filsys fs, blk64_t num,
255                              ext2_ino_t dir, unsigned long idx, ext2_ino_t *ino)
256
257 {
258         errcode_t               retval;
259         blk64_t                 lblk, bend = 0;
260         __u64                   size;
261         blk64_t                 left;
262         blk64_t                 count = 0;
263         struct ext2_inode       inode;
264         ext2_extent_handle_t    handle;
265
266         retval = ext2fs_new_inode(fs, 0, LINUX_S_IFREG, NULL, ino);
267         if (retval)
268                 return retval;
269
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);
277
278         retval = ext2fs_write_new_inode(fs, *ino, &inode);
279         if (retval)
280                 return retval;
281
282         ext2fs_inode_alloc_stats2(fs, *ino, +1, 0);
283
284         retval = ext2fs_extent_open2(fs, *ino, &inode, &handle);
285         if (retval)
286                 return retval;
287
288         lblk = 0;
289         left = num ? num : 1;
290         while (left) {
291                 blk64_t pblk, end;
292                 blk64_t n = left;
293
294                 retval =  ext2fs_find_first_zero_block_bitmap2(fs->block_map,
295                         goal, ext2fs_blocks_count(fs->super) - 1, &end);
296                 if (retval)
297                         goto errout;
298                 goal = end;
299
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);
304                         if (num == 0)
305                                 left = 0;
306                 }
307                 if (!num || bend - goal < left)
308                         n = bend - goal;
309                 pblk = goal;
310                 if (num)
311                         left -= n;
312                 goal += n;
313                 count += n;
314                 ext2fs_block_alloc_stats_range(fs, pblk, n, +1);
315
316                 if (zero_hugefile) {
317                         blk64_t ret_blk;
318                         retval = ext2fs_zero_blocks2(fs, pblk, n,
319                                                      &ret_blk, NULL);
320
321                         if (retval)
322                                 com_err(program_name, retval,
323                                         _("while zeroing block %llu "
324                                           "for hugefile"), ret_blk);
325                 }
326
327                 while (n) {
328                         blk64_t l = n;
329                         struct ext2fs_extent newextent;
330
331                         if (l > EXT_INIT_MAX_LEN)
332                                 l = EXT_INIT_MAX_LEN;
333
334                         newextent.e_len = l;
335                         newextent.e_pblk = pblk;
336                         newextent.e_lblk = lblk;
337                         newextent.e_flags = 0;
338
339                         retval = ext2fs_extent_insert(handle,
340                                         EXT2_EXTENT_INSERT_AFTER, &newextent);
341                         if (retval)
342                                 return retval;
343                         pblk += l;
344                         lblk += l;
345                         n -= l;
346                 }
347         }
348
349         retval = ext2fs_read_inode(fs, *ino, &inode);
350         if (retval)
351                 goto errout;
352
353         retval = ext2fs_iblk_add_blocks(fs, &inode,
354                                         count / EXT2FS_CLUSTER_RATIO(fs));
355         if (retval)
356                 goto errout;
357         size = (__u64) count * fs->blocksize;
358         inode.i_size = size & 0xffffffff;
359         inode.i_size_high = (size >> 32);
360
361         retval = ext2fs_write_new_inode(fs, *ino, &inode);
362         if (retval)
363                 goto errout;
364
365         if (idx_digits)
366                 sprintf(fn_numbuf, "%0*lu", idx_digits, idx);
367         else if (num_files > 1)
368                 sprintf(fn_numbuf, "%lu", idx);
369
370 retry:
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);
374                 if (retval)
375                         goto errout;
376                 goto retry;
377         }
378
379         if (retval)
380                 goto errout;
381
382 errout:
383         if (handle)
384                 ext2fs_extent_free(handle);
385
386         return retval;
387 }
388
389 static blk64_t calc_overhead(ext2_filsys fs, blk64_t num)
390 {
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;
394
395         if (extents <= 4)
396                 return 0;
397
398         /*
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.
403          */
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;
408
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;
414 }
415
416 /*
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.
420  */
421 static blk64_t get_start_block(ext2_filsys fs, blk64_t slack)
422 {
423         errcode_t retval;
424         blk64_t blk = fs->super->s_first_data_block, next;
425         blk64_t last_blk = ext2fs_blocks_count(fs->super) - 1;
426
427         while (slack) {
428                 retval = ext2fs_find_first_zero_block_bitmap2(fs->block_map,
429                                                 blk, last_blk, &blk);
430                 if (retval)
431                         break;
432
433                 retval = ext2fs_find_first_set_block_bitmap2(fs->block_map,
434                                                 blk, last_blk, &next);
435                 if (retval)
436                         next = last_blk;
437                 next--;
438
439                 if (next - blk > slack) {
440                         blk += slack;
441                         break;
442                 }
443
444                 slack -= (next - blk);
445                 blk = next;
446         }
447         return blk;
448 }
449
450 static blk64_t round_up_align(blk64_t b, unsigned long align,
451                               blk64_t part_offset)
452 {
453         unsigned long m;
454
455         if (align == 0)
456                 return b;
457         part_offset = part_offset % align;
458         m = (b + part_offset) % align;
459         if (m)
460                 b += align - m;
461         return b;
462 }
463
464 errcode_t mk_hugefiles(ext2_filsys fs, const char *device_name)
465 {
466         unsigned long   i;
467         ext2_ino_t      dir;
468         errcode_t       retval;
469         blk64_t         fs_blocks, part_offset = 0;
470         unsigned long   align;
471         int             d, dsize;
472         char            *t;
473
474         if (!get_bool_from_profile(fs_types, "make_hugefiles", 0))
475                 return 0;
476
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);
483         free(t);
484         t = get_string_from_profile(fs_types, "hugefiles_size", "0");
485         num_blocks = parse_num_blocks2(t, fs->super->s_log_block_size);
486         free(t);
487         t = get_string_from_profile(fs_types, "hugefiles_align", "0");
488         align = parse_num_blocks2(t, fs->super->s_log_block_size);
489         free(t);
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",
495                                               zero_hugefile);
496
497         t = get_string_from_profile(fs_types, "hugefiles_dir", "/");
498         retval = create_directory(fs, t, &dir);
499         free(t);
500         if (retval)
501                 return retval;
502
503         fn_prefix = get_string_from_profile(fs_types, "hugefiles_name",
504                                             "hugefile");
505         idx_digits = get_int_from_profile(fs_types, "hugefiles_digits", 5);
506         d = int_log10(num_files) + 1;
507         if (idx_digits > d)
508                 d = idx_digits;
509         dsize = strlen(fn_prefix) + d + 16;
510         fn_buf = malloc(dsize);
511         if (!fn_buf) {
512                 free(fn_prefix);
513                 return ENOMEM;
514         }
515         strcpy(fn_buf, fn_prefix);
516         fn_numbuf = fn_buf + strlen(fn_prefix);
517         free(fn_prefix);
518
519         fs_blocks = ext2fs_free_blocks_count(fs->super);
520         if (fs_blocks < num_slack + align)
521                 return ENOMEM;
522         fs_blocks -= num_slack + align;
523         if (num_blocks && num_blocks > fs_blocks)
524                 return ENOMEM;
525         if (num_blocks == 0 && num_files == 0)
526                 num_files = 1;
527
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;
533         }
534
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;
540         }
541
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);
546
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;
551
552         if (!quiet) {
553                 if (zero_hugefile && verbose)
554                         printf("%s", _("Huge files will be zero'ed\n"));
555                 printf(_("Creating %lu huge file(s) "), num_files);
556                 if (num_blocks)
557                         printf(_("with %llu blocks each"), num_blocks);
558                 fputs(": ", stdout);
559         }
560         for (i=0; i < num_files; i++) {
561                 ext2_ino_t ino;
562
563                 retval = mk_hugefile(fs, num_blocks, dir, i, &ino);
564                 if (retval) {
565                         com_err(program_name, retval,
566                                 _("while creating huge file %lu"), i);
567                         goto errout;
568                 }
569         }
570         if (!quiet)
571                 fputs(_("done\n"), stdout);
572
573 errout:
574         free(fn_buf);
575         return retval;
576 }