util.c 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267
  1. /* vi: set sw=4 ts=4: */
  2. /*
  3. * util.c --- helper functions used by tune2fs and mke2fs
  4. *
  5. * Copyright 1995, 1996, 1997, 1998, 1999, 2000 by Theodore Ts'o.
  6. *
  7. * %Begin-Header%
  8. * This file may be redistributed under the terms of the GNU Public
  9. * License.
  10. * %End-Header%
  11. */
  12. #include <stdio.h>
  13. #include <string.h>
  14. #include <errno.h>
  15. #include <linux/major.h>
  16. #include <sys/stat.h>
  17. #include "e2fsbb.h"
  18. #include "e2p/e2p.h"
  19. #include "ext2fs/ext2_fs.h"
  20. #include "ext2fs/ext2fs.h"
  21. #include "blkid/blkid.h"
  22. #include "util.h"
  23. void proceed_question(void)
  24. {
  25. fputs("Proceed anyway? (y,n) ", stdout);
  26. if (bb_ask_confirmation() == 0)
  27. exit(1);
  28. }
  29. void check_plausibility(const char *device, int force)
  30. {
  31. int val;
  32. struct stat s;
  33. val = stat(device, &s);
  34. if (force)
  35. return;
  36. if(val == -1)
  37. bb_perror_msg_and_die("cannot stat %s", device);
  38. if (!S_ISBLK(s.st_mode)) {
  39. printf("%s is not a block special device.\n", device);
  40. proceed_question();
  41. return;
  42. }
  43. #ifdef HAVE_LINUX_MAJOR_H
  44. #ifndef MAJOR
  45. #define MAJOR(dev) ((dev)>>8)
  46. #define MINOR(dev) ((dev) & 0xff)
  47. #endif
  48. #ifndef SCSI_BLK_MAJOR
  49. #ifdef SCSI_DISK0_MAJOR
  50. #ifdef SCSI_DISK8_MAJOR
  51. #define SCSI_DISK_MAJOR(M) ((M) == SCSI_DISK0_MAJOR || \
  52. ((M) >= SCSI_DISK1_MAJOR && (M) <= SCSI_DISK7_MAJOR) || \
  53. ((M) >= SCSI_DISK8_MAJOR && (M) <= SCSI_DISK15_MAJOR))
  54. #else
  55. #define SCSI_DISK_MAJOR(M) ((M) == SCSI_DISK0_MAJOR || \
  56. ((M) >= SCSI_DISK1_MAJOR && (M) <= SCSI_DISK7_MAJOR))
  57. #endif /* defined(SCSI_DISK8_MAJOR) */
  58. #define SCSI_BLK_MAJOR(M) (SCSI_DISK_MAJOR((M)) || (M) == SCSI_CDROM_MAJOR)
  59. #else
  60. #define SCSI_BLK_MAJOR(M) ((M) == SCSI_DISK_MAJOR || (M) == SCSI_CDROM_MAJOR)
  61. #endif /* defined(SCSI_DISK0_MAJOR) */
  62. #endif /* defined(SCSI_BLK_MAJOR) */
  63. if (((MAJOR(s.st_rdev) == HD_MAJOR &&
  64. MINOR(s.st_rdev)%64 == 0) ||
  65. (SCSI_BLK_MAJOR(MAJOR(s.st_rdev)) &&
  66. MINOR(s.st_rdev)%16 == 0))) {
  67. printf("%s is entire device, not just one partition!\n", device);
  68. proceed_question();
  69. }
  70. #endif
  71. }
  72. void check_mount(const char *device, int force, const char *type)
  73. {
  74. errcode_t retval;
  75. int mount_flags;
  76. retval = ext2fs_check_if_mounted(device, &mount_flags);
  77. if (retval) {
  78. bb_error_msg("cannot determine if %s is mounted", device);
  79. return;
  80. }
  81. if (mount_flags & EXT2_MF_MOUNTED) {
  82. bb_error_msg("%s is mounted !", device);
  83. force_check:
  84. if (force)
  85. bb_error_msg("badblocks forced anyways");
  86. else
  87. bb_error_msg_and_die("it's not safe to run badblocks!");
  88. }
  89. if (mount_flags & EXT2_MF_BUSY) {
  90. bb_error_msg("%s is apparently in use by the system", device);
  91. goto force_check;
  92. }
  93. }
  94. void parse_journal_opts(char **journal_device, int *journal_flags,
  95. int *journal_size, const char *opts)
  96. {
  97. char *buf, *token, *next, *p, *arg;
  98. int journal_usage = 0;
  99. buf = xstrdup(opts);
  100. for (token = buf; token && *token; token = next) {
  101. p = strchr(token, ',');
  102. next = 0;
  103. if (p) {
  104. *p = 0;
  105. next = p+1;
  106. }
  107. arg = strchr(token, '=');
  108. if (arg) {
  109. *arg = 0;
  110. arg++;
  111. }
  112. if (strcmp(token, "device") == 0) {
  113. *journal_device = blkid_get_devname(NULL, arg, NULL);
  114. if (!journal_device) {
  115. journal_usage++;
  116. continue;
  117. }
  118. } else if (strcmp(token, "size") == 0) {
  119. if (!arg) {
  120. journal_usage++;
  121. continue;
  122. }
  123. (*journal_size) = strtoul(arg, &p, 0);
  124. if (*p)
  125. journal_usage++;
  126. } else if (strcmp(token, "v1_superblock") == 0) {
  127. (*journal_flags) |= EXT2_MKJOURNAL_V1_SUPER;
  128. continue;
  129. } else
  130. journal_usage++;
  131. }
  132. if (journal_usage)
  133. bb_error_msg_and_die(
  134. "\nBad journal options specified.\n\n"
  135. "Journal options are separated by commas, "
  136. "and may take an argument which\n"
  137. "\tis set off by an equals ('=') sign.\n\n"
  138. "Valid journal options are:\n"
  139. "\tsize=<journal size in megabytes>\n"
  140. "\tdevice=<journal device>\n\n"
  141. "The journal size must be between "
  142. "1024 and 102400 filesystem blocks.\n\n");
  143. }
  144. /*
  145. * Determine the number of journal blocks to use, either via
  146. * user-specified # of megabytes, or via some intelligently selected
  147. * defaults.
  148. *
  149. * Find a reasonable journal file size (in blocks) given the number of blocks
  150. * in the filesystem. For very small filesystems, it is not reasonable to
  151. * have a journal that fills more than half of the filesystem.
  152. */
  153. int figure_journal_size(int size, ext2_filsys fs)
  154. {
  155. blk_t j_blocks;
  156. if (fs->super->s_blocks_count < 2048) {
  157. bb_error_msg("Filesystem too small for a journal");
  158. return 0;
  159. }
  160. if (size >= 0) {
  161. j_blocks = size * 1024 / (fs->blocksize / 1024);
  162. if (j_blocks < 1024 || j_blocks > 102400)
  163. bb_error_msg_and_die("\nThe requested journal "
  164. "size is %d blocks;\n it must be "
  165. "between 1024 and 102400 blocks; Aborting",
  166. j_blocks);
  167. if (j_blocks > fs->super->s_free_blocks_count)
  168. bb_error_msg_and_die("Journal size too big for filesystem");
  169. return j_blocks;
  170. }
  171. if (fs->super->s_blocks_count < 32768)
  172. j_blocks = 1024;
  173. else if (fs->super->s_blocks_count < 256*1024)
  174. j_blocks = 4096;
  175. else if (fs->super->s_blocks_count < 512*1024)
  176. j_blocks = 8192;
  177. else if (fs->super->s_blocks_count < 1024*1024)
  178. j_blocks = 16384;
  179. else
  180. j_blocks = 32768;
  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. }
  192. void make_journal_device(char *journal_device, ext2_filsys fs, int quiet, int force)
  193. {
  194. errcode_t retval;
  195. ext2_filsys jfs;
  196. io_manager io_ptr;
  197. check_plausibility(journal_device, force);
  198. check_mount(journal_device, force, "journal");
  199. io_ptr = unix_io_manager;
  200. retval = ext2fs_open(journal_device, EXT2_FLAG_RW|
  201. EXT2_FLAG_JOURNAL_DEV_OK, 0,
  202. fs->blocksize, io_ptr, &jfs);
  203. if (retval)
  204. bb_error_msg_and_die("cannot journal device %s", journal_device);
  205. if(!quiet)
  206. printf("Adding journal to device %s: ", journal_device);
  207. fflush(stdout);
  208. retval = ext2fs_add_journal_device(fs, jfs);
  209. if(retval)
  210. bb_error_msg_and_die("\nFailed to add journal to device %s", journal_device);
  211. if(!quiet)
  212. puts("done");
  213. ext2fs_close(jfs);
  214. }
  215. void make_journal_blocks(ext2_filsys fs, int journal_size, int journal_flags, int quiet)
  216. {
  217. unsigned long journal_blocks;
  218. errcode_t retval;
  219. journal_blocks = figure_journal_size(journal_size, fs);
  220. if (!journal_blocks) {
  221. fs->super->s_feature_compat &=
  222. ~EXT3_FEATURE_COMPAT_HAS_JOURNAL;
  223. return;
  224. }
  225. if(!quiet)
  226. printf("Creating journal (%ld blocks): ", journal_blocks);
  227. fflush(stdout);
  228. retval = ext2fs_add_journal_inode(fs, journal_blocks,
  229. journal_flags);
  230. if(retval)
  231. bb_error_msg_and_die("cannot create journal");
  232. if(!quiet)
  233. puts("done");
  234. }
  235. char *e2fs_set_sbin_path(void)
  236. {
  237. char *oldpath = getenv("PATH");
  238. /* Update our PATH to include /sbin */
  239. #define PATH_SET "/sbin"
  240. if (oldpath)
  241. oldpath = xasprintf("%s:%s", PATH_SET, oldpath);
  242. else
  243. oldpath = PATH_SET;
  244. putenv (oldpath);
  245. return oldpath;
  246. }