3
0

mkswap.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418
  1. /* vi: set sw=4 ts=4: */
  2. /*
  3. * mkswap.c - set up a linux swap device
  4. *
  5. * (C) 1991 Linus Torvalds. This file may be redistributed as per
  6. * the Linux copyright.
  7. */
  8. /*
  9. * 20.12.91 - time began. Got VM working yesterday by doing this by hand.
  10. *
  11. * Usage: mkswap [-c] [-vN] [-f] device [size-in-blocks]
  12. *
  13. * -c for readability checking. (Use it unless you are SURE!)
  14. * -vN for swap areas version N. (Only N=0,1 known today.)
  15. * -f for forcing swap creation even if it would smash partition table.
  16. *
  17. * The device may be a block device or an image of one, but this isn't
  18. * enforced (but it's not much fun on a character device :-).
  19. *
  20. * Patches from jaggy@purplet.demon.co.uk (Mike Jagdis) to make the
  21. * size-in-blocks parameter optional added Wed Feb 8 10:33:43 1995.
  22. *
  23. * Version 1 swap area code (for kernel 2.1.117), aeb, 981010.
  24. *
  25. * Sparc fixes, jj@ultra.linux.cz (Jakub Jelinek), 981201 - mangled by aeb.
  26. * V1_MAX_PAGES fixes, jj, 990325.
  27. *
  28. * 1999-02-22 Arkadiusz Mi¶kiewicz <misiek@misiek.eu.org>
  29. * - added Native Language Support
  30. *
  31. * from util-linux -- adapted for busybox by
  32. * Erik Andersen <andersen@codepoet.org>. I ripped out Native Language
  33. * Support, made some stuff smaller, and fitted for life in busybox.
  34. *
  35. */
  36. #include <stdio.h>
  37. #include <unistd.h>
  38. #include <string.h>
  39. #include <fcntl.h>
  40. #include <stdlib.h>
  41. #include <sys/ioctl.h> /* for _IO */
  42. #include <sys/utsname.h>
  43. #include <asm/page.h> /* for PAGE_SIZE and PAGE_SHIFT */
  44. /* we also get PAGE_SIZE via getpagesize() */
  45. #include "busybox.h"
  46. #ifndef _IO
  47. /* pre-1.3.45 */
  48. static const int BLKGETSIZE = 0x1260;
  49. #else
  50. /* same on i386, m68k, arm; different on alpha, mips, sparc, ppc */
  51. #define BLKGETSIZE _IO(0x12,96)
  52. #endif
  53. static char *device_name = NULL;
  54. static int DEV = -1;
  55. static long PAGES = 0;
  56. static int check = 0;
  57. static int badpages = 0;
  58. static int version = -1;
  59. #define MAKE_VERSION(p,q,r) (65536*(p) + 256*(q) + (r))
  60. /*
  61. * The definition of the union swap_header uses the constant PAGE_SIZE.
  62. * Unfortunately, on some architectures this depends on the hardware model,
  63. * and can only be found at run time -- we use getpagesize().
  64. */
  65. static int pagesize;
  66. static int *signature_page;
  67. static struct swap_header_v1 {
  68. char bootbits[1024]; /* Space for disklabel etc. */
  69. unsigned int version;
  70. unsigned int last_page;
  71. unsigned int nr_badpages;
  72. unsigned int padding[125];
  73. unsigned int badpages[1];
  74. } *p;
  75. static inline void init_signature_page(void)
  76. {
  77. pagesize = getpagesize();
  78. #ifdef PAGE_SIZE
  79. if (pagesize != PAGE_SIZE)
  80. bb_error_msg("Assuming pages of size %d", pagesize);
  81. #endif
  82. signature_page = (int *) xmalloc(pagesize);
  83. memset(signature_page, 0, pagesize);
  84. p = (struct swap_header_v1 *) signature_page;
  85. }
  86. static inline void write_signature(char *sig)
  87. {
  88. char *sp = (char *) signature_page;
  89. strncpy(sp + pagesize - 10, sig, 10);
  90. }
  91. #define V0_MAX_PAGES (8 * (pagesize - 10))
  92. /* Before 2.2.0pre9 */
  93. #define V1_OLD_MAX_PAGES ((0x7fffffff / pagesize) - 1)
  94. /* Since 2.2.0pre9:
  95. error if nr of pages >= SWP_OFFSET(SWP_ENTRY(0,~0UL))
  96. with variations on
  97. #define SWP_ENTRY(type,offset) (((type) << 1) | ((offset) << 8))
  98. #define SWP_OFFSET(entry) ((entry) >> 8)
  99. on the various architectures. Below the result - yuk.
  100. Machine pagesize SWP_ENTRY SWP_OFFSET bound+1 oldbound+2
  101. i386 2^12 o<<8 e>>8 1<<24 1<<19
  102. mips 2^12 o<<15 e>>15 1<<17 1<<19
  103. alpha 2^13 o<<40 e>>40 1<<24 1<<18
  104. m68k 2^12 o<<12 e>>12 1<<20 1<<19
  105. sparc 2^{12,13} (o&0x3ffff)<<9 (e>>9)&0x3ffff 1<<18 1<<{19,18}
  106. sparc64 2^13 o<<13 e>>13 1<<51 1<<18
  107. ppc 2^12 o<<8 e>>8 1<<24 1<<19
  108. armo 2^{13,14,15} o<<8 e>>8 1<<24 1<<{18,17,16}
  109. armv 2^12 o<<9 e>>9 1<<23 1<<19
  110. assuming that longs have 64 bits on alpha and sparc64 and 32 bits elsewhere.
  111. The bad part is that we need to know this since the kernel will
  112. refuse a swap space if it is too large.
  113. */
  114. /* patch from jj - why does this differ from the above? */
  115. #if defined(__alpha__)
  116. #define V1_MAX_PAGES ((1 << 24) - 1)
  117. #elif defined(__mips__)
  118. #define V1_MAX_PAGES ((1 << 17) - 1)
  119. #elif defined(__sparc_v9__)
  120. #define V1_MAX_PAGES ((3 << 29) - 1)
  121. #elif defined(__sparc__)
  122. #define V1_MAX_PAGES (pagesize == 8192 ? ((3 << 29) - 1) : ((1 << 18) - 1))
  123. #else
  124. #define V1_MAX_PAGES V1_OLD_MAX_PAGES
  125. #endif
  126. /* man page now says:
  127. The maximum useful size of a swap area now depends on the architecture.
  128. It is roughly 2GB on i386, PPC, m68k, ARM, 1GB on sparc, 512MB on mips,
  129. 128GB on alpha and 3TB on sparc64.
  130. */
  131. #define MAX_BADPAGES ((pagesize-1024-128*sizeof(int)-10)/sizeof(int))
  132. static inline void bit_set(unsigned int *addr, unsigned int nr)
  133. {
  134. unsigned int r, m;
  135. addr += nr / (8 * sizeof(int));
  136. r = *addr;
  137. m = 1 << (nr & (8 * sizeof(int) - 1));
  138. *addr = r | m;
  139. }
  140. static int bit_test_and_clear(unsigned int *addr, unsigned int nr)
  141. {
  142. unsigned int r, m;
  143. addr += nr / (8 * sizeof(int));
  144. r = *addr;
  145. m = 1 << (nr & (8 * sizeof(int) - 1));
  146. *addr = r & ~m;
  147. return (r & m) != 0;
  148. }
  149. static void page_ok(int page)
  150. {
  151. if (version == 0)
  152. bit_set(signature_page, page);
  153. }
  154. static inline void page_bad(int page)
  155. {
  156. if (version == 0)
  157. bit_test_and_clear(signature_page, page);
  158. else {
  159. if (badpages == MAX_BADPAGES)
  160. bb_error_msg_and_die("too many bad pages");
  161. p->badpages[badpages] = page;
  162. }
  163. badpages++;
  164. }
  165. static void check_blocks(void)
  166. {
  167. unsigned int current_page;
  168. int do_seek = 1;
  169. char *buffer;
  170. buffer = xmalloc(pagesize);
  171. current_page = 0;
  172. while (current_page < PAGES) {
  173. if (!check) {
  174. page_ok(current_page++);
  175. continue;
  176. }
  177. if (do_seek && lseek(DEV, current_page * pagesize, SEEK_SET) !=
  178. current_page * pagesize)
  179. bb_error_msg_and_die("seek failed in check_blocks");
  180. if ((do_seek = (pagesize != read(DEV, buffer, pagesize)))) {
  181. page_bad(current_page++);
  182. continue;
  183. }
  184. page_ok(current_page++);
  185. }
  186. if (badpages == 1)
  187. printf("one bad page\n");
  188. else if (badpages > 1)
  189. printf("%d bad pages\n", badpages);
  190. }
  191. static long valid_offset(int fd, int offset)
  192. {
  193. char ch;
  194. if (lseek(fd, offset, 0) < 0)
  195. return 0;
  196. if (read(fd, &ch, 1) < 1)
  197. return 0;
  198. return 1;
  199. }
  200. static int find_size(int fd)
  201. {
  202. unsigned int high, low;
  203. low = 0;
  204. for (high = 1; high > 0 && valid_offset(fd, high); high *= 2)
  205. low = high;
  206. while (low < high - 1) {
  207. const int mid = (low + high) / 2;
  208. if (valid_offset(fd, mid))
  209. low = mid;
  210. else
  211. high = mid;
  212. }
  213. return (low + 1);
  214. }
  215. /* return size in pages, to avoid integer overflow */
  216. static long get_size(const char *file)
  217. {
  218. int fd;
  219. long size;
  220. if ((fd = open(file, O_RDONLY)) < 0)
  221. bb_perror_msg_and_die("%s", file);
  222. if (ioctl(fd, BLKGETSIZE, &size) >= 0) {
  223. int sectors_per_page = pagesize / 512;
  224. size /= sectors_per_page;
  225. } else {
  226. size = find_size(fd) / pagesize;
  227. }
  228. close(fd);
  229. return size;
  230. }
  231. int mkswap_main(int argc, char **argv)
  232. {
  233. char *tmp;
  234. struct stat statbuf;
  235. int sz;
  236. int maxpages;
  237. int goodpages;
  238. int offset;
  239. int force = 0;
  240. init_signature_page(); /* get pagesize */
  241. while (argc-- > 1) {
  242. argv++;
  243. if (argv[0][0] != '-') {
  244. if (device_name) {
  245. int blocks_per_page = pagesize / 1024;
  246. PAGES = strtol(argv[0], &tmp, 0) / blocks_per_page;
  247. if (*tmp)
  248. bb_show_usage();
  249. } else
  250. device_name = argv[0];
  251. } else {
  252. switch (argv[0][1]) {
  253. case 'c':
  254. check = 1;
  255. break;
  256. case 'f':
  257. force = 1;
  258. break;
  259. case 'v':
  260. version = atoi(argv[0] + 2);
  261. break;
  262. default:
  263. bb_show_usage();
  264. }
  265. }
  266. }
  267. if (!device_name) {
  268. bb_error_msg("error: Nowhere to set up swap on?");
  269. bb_show_usage();
  270. }
  271. sz = get_size(device_name);
  272. if (!PAGES) {
  273. PAGES = sz;
  274. } else if (PAGES > sz && !force) {
  275. bb_error_msg("error: size %ld is larger than device size %d",
  276. PAGES * (pagesize / 1024), sz * (pagesize / 1024));
  277. return EXIT_FAILURE;
  278. }
  279. if (version == -1) {
  280. if (get_kernel_revision() < MAKE_VERSION(2, 1, 117))
  281. version = 0;
  282. else
  283. version = 1;
  284. }
  285. if (version != 0 && version != 1) {
  286. bb_error_msg("error: unknown version %d", version);
  287. bb_show_usage();
  288. }
  289. if (PAGES < 10) {
  290. bb_error_msg("error: swap area needs to be at least %ldkB",
  291. (long) (10 * pagesize / 1024));
  292. bb_show_usage();
  293. }
  294. #if 0
  295. maxpages = ((version == 0) ? V0_MAX_PAGES : V1_MAX_PAGES);
  296. #else
  297. if (!version)
  298. maxpages = V0_MAX_PAGES;
  299. else if (get_kernel_revision() >= MAKE_VERSION(2, 2, 1))
  300. maxpages = V1_MAX_PAGES;
  301. else {
  302. maxpages = V1_OLD_MAX_PAGES;
  303. if (maxpages > V1_MAX_PAGES)
  304. maxpages = V1_MAX_PAGES;
  305. }
  306. #endif
  307. if (PAGES > maxpages) {
  308. PAGES = maxpages;
  309. bb_error_msg("warning: truncating swap area to %ldkB",
  310. PAGES * pagesize / 1024);
  311. }
  312. DEV = open(device_name, O_RDWR);
  313. if (DEV < 0 || fstat(DEV, &statbuf) < 0)
  314. bb_perror_msg_and_die("%s", device_name);
  315. if (!S_ISBLK(statbuf.st_mode))
  316. check = 0;
  317. else if (statbuf.st_rdev == 0x0300 || statbuf.st_rdev == 0x0340)
  318. bb_error_msg_and_die("Will not try to make swapdevice on '%s'", device_name);
  319. #ifdef __sparc__
  320. if (!force && version == 0) {
  321. /* Don't overwrite partition table unless forced */
  322. unsigned char *buffer = (unsigned char *) signature_page;
  323. unsigned short *q, sum;
  324. if (read(DEV, buffer, 512) != 512)
  325. bb_error_msg_and_die("fatal: first page unreadable");
  326. if (buffer[508] == 0xDA && buffer[509] == 0xBE) {
  327. q = (unsigned short *) (buffer + 510);
  328. for (sum = 0; q >= (unsigned short *) buffer;)
  329. sum ^= *q--;
  330. if (!sum) {
  331. bb_error_msg("Device '%s' contains a valid Sun disklabel.\n"
  332. "This probably means creating v0 swap would destroy your partition table\n"
  333. "No swap created. If you really want to create swap v0 on that device, use\n"
  334. "the -f option to force it.", device_name);
  335. return EXIT_FAILURE;
  336. }
  337. }
  338. }
  339. #endif
  340. if (version == 0 || check)
  341. check_blocks();
  342. if (version == 0 && !bit_test_and_clear(signature_page, 0))
  343. bb_error_msg_and_die("fatal: first page unreadable");
  344. if (version == 1) {
  345. p->version = version;
  346. p->last_page = PAGES - 1;
  347. p->nr_badpages = badpages;
  348. }
  349. goodpages = PAGES - badpages - 1;
  350. if (goodpages <= 0)
  351. bb_error_msg_and_die("Unable to set up swap-space: unreadable");
  352. printf("Setting up swapspace version %d, size = %ld bytes\n",
  353. version, (long) (goodpages * pagesize));
  354. write_signature((version == 0) ? "SWAP-SPACE" : "SWAPSPACE2");
  355. offset = ((version == 0) ? 0 : 1024);
  356. if (lseek(DEV, offset, SEEK_SET) != offset)
  357. bb_error_msg_and_die("unable to rewind swap-device");
  358. if (write(DEV, (char *) signature_page + offset, pagesize - offset)
  359. != pagesize - offset)
  360. bb_error_msg_and_die("unable to write signature page");
  361. /*
  362. * A subsequent swapon() will fail if the signature
  363. * is not actually on disk. (This is a kernel bug.)
  364. */
  365. if (fsync(DEV))
  366. bb_error_msg_and_die("fsync failed");
  367. return EXIT_SUCCESS;
  368. }