util.c 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  1. /*
  2. * util.c --- helper functions used by tune2fs and mke2fs
  3. *
  4. * Copyright 1995, 1996, 1997, 1998, 1999, 2000 by Theodore Ts'o.
  5. *
  6. * %Begin-Header%
  7. * This file may be redistributed under the terms of the GNU Public
  8. * License.
  9. * %End-Header%
  10. */
  11. #include <stdio.h>
  12. #include <string.h>
  13. #include <errno.h>
  14. #include <linux/major.h>
  15. #include <sys/stat.h>
  16. #include "e2fsbb.h"
  17. #include "e2p/e2p.h"
  18. #include "ext2fs/ext2_fs.h"
  19. #include "ext2fs/ext2fs.h"
  20. #include "blkid/blkid.h"
  21. #include "util.h"
  22. void proceed_question(void)
  23. {
  24. fputs("Proceed anyway? (y,n) ", stdout);
  25. if (bb_ask_confirmation() == 0)
  26. exit(1);
  27. }
  28. void check_plausibility(const char *device)
  29. {
  30. int val;
  31. #ifdef CONFIG_LFS
  32. struct stat64 s;
  33. val = stat64(device, &s);
  34. #else
  35. struct stat s;
  36. val = stat(device, &s);
  37. #endif
  38. if(val == -1)
  39. bb_perror_msg_and_die("Could not stat %s", device);
  40. if (!S_ISBLK(s.st_mode)) {
  41. printf("%s is not a block special device.\n", device);
  42. proceed_question();
  43. return;
  44. }
  45. #ifdef HAVE_LINUX_MAJOR_H
  46. #ifndef MAJOR
  47. #define MAJOR(dev) ((dev)>>8)
  48. #define MINOR(dev) ((dev) & 0xff)
  49. #endif
  50. #ifndef SCSI_BLK_MAJOR
  51. #ifdef SCSI_DISK0_MAJOR
  52. #ifdef SCSI_DISK8_MAJOR
  53. #define SCSI_DISK_MAJOR(M) ((M) == SCSI_DISK0_MAJOR || \
  54. ((M) >= SCSI_DISK1_MAJOR && (M) <= SCSI_DISK7_MAJOR) || \
  55. ((M) >= SCSI_DISK8_MAJOR && (M) <= SCSI_DISK15_MAJOR))
  56. #else
  57. #define SCSI_DISK_MAJOR(M) ((M) == SCSI_DISK0_MAJOR || \
  58. ((M) >= SCSI_DISK1_MAJOR && (M) <= SCSI_DISK7_MAJOR))
  59. #endif /* defined(SCSI_DISK8_MAJOR) */
  60. #define SCSI_BLK_MAJOR(M) (SCSI_DISK_MAJOR((M)) || (M) == SCSI_CDROM_MAJOR)
  61. #else
  62. #define SCSI_BLK_MAJOR(M) ((M) == SCSI_DISK_MAJOR || (M) == SCSI_CDROM_MAJOR)
  63. #endif /* defined(SCSI_DISK0_MAJOR) */
  64. #endif /* defined(SCSI_BLK_MAJOR) */
  65. if (((MAJOR(s.st_rdev) == HD_MAJOR &&
  66. MINOR(s.st_rdev)%64 == 0) ||
  67. (SCSI_BLK_MAJOR(MAJOR(s.st_rdev)) &&
  68. MINOR(s.st_rdev)%16 == 0))) {
  69. printf("%s is entire device, not just one partition!\n", device);
  70. proceed_question();
  71. }
  72. #endif
  73. }
  74. void check_mount(const char *device, int force, const char *type)
  75. {
  76. errcode_t retval;
  77. int mount_flags;
  78. retval = ext2fs_check_if_mounted(device, &mount_flags);
  79. if (retval) {
  80. bb_error_msg("Could not determine if %s is mounted", device);
  81. return;
  82. }
  83. if (!(mount_flags & EXT2_MF_MOUNTED))
  84. return;
  85. bb_error_msg("%s is mounted !", device);
  86. if (force)
  87. bb_error_msg("forcing anyways and ignoring /etc/mtab status");
  88. else
  89. bb_error_msg_and_die("will not make a %s here!", type);
  90. }
  91. void parse_journal_opts(char **journal_device, int *journal_flags,
  92. int *journal_size, const char *opts)
  93. {
  94. char *buf, *token, *next, *p, *arg;
  95. int journal_usage = 0;
  96. #if 0
  97. int len;
  98. len = strlen(opts);
  99. buf = xmalloc(len+1);
  100. strcpy(buf, opts);
  101. #else
  102. buf = bb_xstrdup(opts);
  103. #endif
  104. for (token = buf; token && *token; token = next) {
  105. p = strchr(token, ',');
  106. next = 0;
  107. if (p) {
  108. *p = 0;
  109. next = p+1;
  110. }
  111. arg = strchr(token, '=');
  112. if (arg) {
  113. *arg = 0;
  114. arg++;
  115. }
  116. if (strcmp(token, "device") == 0) {
  117. *journal_device = blkid_get_devname(NULL, arg, NULL);
  118. if (!journal_device) {
  119. journal_usage++;
  120. continue;
  121. }
  122. } else if (strcmp(token, "size") == 0) {
  123. if (!arg) {
  124. journal_usage++;
  125. continue;
  126. }
  127. (*journal_size) = strtoul(arg, &p, 0);
  128. if (*p)
  129. journal_usage++;
  130. } else if (strcmp(token, "v1_superblock") == 0) {
  131. (*journal_flags) |= EXT2_MKJOURNAL_V1_SUPER;
  132. continue;
  133. } else
  134. journal_usage++;
  135. }
  136. if (journal_usage)
  137. bb_error_msg_and_die(
  138. "\nBad journal options specified.\n\n"
  139. "Journal options are separated by commas, "
  140. "and may take an argument which\n"
  141. "\tis set off by an equals ('=') sign.\n\n"
  142. "Valid journal options are:\n"
  143. "\tsize=<journal size in megabytes>\n"
  144. "\tdevice=<journal device>\n\n"
  145. "The journal size must be between "
  146. "1024 and 102400 filesystem blocks.\n\n");
  147. }
  148. /*
  149. * Determine the number of journal blocks to use, either via
  150. * user-specified # of megabytes, or via some intelligently selected
  151. * defaults.
  152. *
  153. * Find a reasonable journal file size (in blocks) given the number of blocks
  154. * in the filesystem. For very small filesystems, it is not reasonable to
  155. * have a journal that fills more than half of the filesystem.
  156. */
  157. int figure_journal_size(int size, ext2_filsys fs)
  158. {
  159. blk_t j_blocks;
  160. if (fs->super->s_blocks_count < 2048) {
  161. bb_error_msg("Filesystem too small for a journal");
  162. return 0;
  163. }
  164. if (size >= 0) {
  165. j_blocks = size * 1024 / (fs->blocksize / 1024);
  166. if (j_blocks < 1024 || j_blocks > 102400)
  167. bb_error_msg_and_die("\nThe requested journal "
  168. "size is %d blocks;\n it must be "
  169. "between 1024 and 102400 blocks; Aborting",
  170. j_blocks);
  171. if (j_blocks > fs->super->s_free_blocks_count)
  172. bb_error_msg_and_die("Journal size too big for filesystem");
  173. return j_blocks;
  174. }
  175. if (fs->super->s_blocks_count < 32768)
  176. j_blocks = 1024;
  177. else if (fs->super->s_blocks_count < 262144)
  178. j_blocks = 4096;
  179. else
  180. j_blocks = 8192;
  181. return j_blocks;
  182. }
  183. void print_check_message(ext2_filsys fs)
  184. {
  185. printf("This filesystem will be automatically "
  186. "checked every %d mounts or\n"
  187. "%g days, whichever comes first. "
  188. "Use tune2fs -c or -i to override.\n",
  189. fs->super->s_max_mnt_count,
  190. (double)fs->super->s_checkinterval / (3600 * 24));
  191. }