3 * mkswap.c - set up a linux swap device
5 * (C) 1991 Linus Torvalds. This file may be redistributed as per
10 * 20.12.91 - time began. Got VM working yesterday by doing this by hand.
12 * Usage: mkswap [-c] device [size-in-blocks]
14 * -c for readablility checking (use it unless you are SURE!)
16 * The device may be a block device or a image of one, but this isn't
17 * enforced (but it's not much fun on a character device :-).
19 * Patches from jaggy@purplet.demon.co.uk (Mike Jagdis) to make the
20 * size-in-blocks parameter optional added Wed Feb 8 10:33:43 1995.
29 #include <sys/ioctl.h>
38 #define TEST_BUFFER_PAGES 8
40 const char mkswap_usage[] = "mkswap [-c] partition [block-count]\n"
42 "\tPrepare a disk partition to be used as a swap partition.\n"
43 "\tThe default block count is the size of the entire partition.\n"
45 "\t-c:\tCheck for read-ability.\n"
46 "\tblock-count\tUse only this many blocks.\n";
48 static const char * program_name = "mkswap";
49 static const char * device_name = NULL;
51 static long PAGES = 0;
52 static int do_check = 0;
53 static int badpages = 0;
56 static long bit_test_and_set (unsigned int *addr, unsigned int nr)
60 addr += nr / (8 * sizeof(int));
62 m = 1 << (nr & (8 * sizeof(int) - 1));
67 static int bit_test_and_clear (unsigned int *addr, unsigned int nr)
71 addr += nr / (8 * sizeof(int));
73 m = 1 << (nr & (8 * sizeof(int) - 1));
79 * Volatile to let gcc know that this doesn't return. When trying
80 * to compile this under minix, volatile gives a warning, as
81 * exit() isn't defined as volatile under minix.
83 volatile void fatal_error(const char * fmt_string)
85 fprintf(stderr,fmt_string,program_name,device_name);
89 #define die(str) fatal_error("%s: " str "\n")
91 static void check_blocks(int * signature_page)
93 unsigned int current_page;
95 char buffer[PAGE_SIZE];
98 while (current_page < PAGES) {
100 bit_test_and_set(signature_page,current_page++);
103 printf("\r%d", current_page);
105 if (do_seek && lseek(DEV,current_page*PAGE_SIZE,SEEK_SET) !=
106 current_page*PAGE_SIZE)
107 die("seek failed in check_blocks");
108 if ( (do_seek = (PAGE_SIZE != read(DEV, buffer, PAGE_SIZE))) ) {
109 bit_test_and_clear(signature_page,current_page++);
113 bit_test_and_set(signature_page,current_page++);
118 printf("%d bad page%s\n",badpages,(badpages>1)?"s":"");
121 static long valid_offset (int fd, int offset)
125 if (lseek (fd, offset, 0) < 0)
127 if (read (fd, &ch, 1) < 1)
132 static int count_blocks (int fd)
137 for (high = 1; valid_offset (fd, high); high *= 2)
139 while (low < high - 1)
141 const int mid = (low + high) / 2;
143 if (valid_offset (fd, mid))
148 valid_offset (fd, 0);
152 static int get_size(const char *file)
157 fd = open(file, O_RDWR);
162 if (ioctl(fd, BLKGETSIZE, &size) >= 0) {
167 size = count_blocks(fd);
173 mkswap(const char *device_name, int pages, int check)
177 int signature_page[PAGE_SIZE/sizeof(int)];
182 memset(signature_page,0,PAGE_SIZE);
184 if (device_name && !PAGES) {
185 PAGES = get_size(device_name) / PAGE_SIZE;
187 if (!device_name || PAGES<10) {
189 "%s: error: swap area needs to be at least %ldkB\n",
190 program_name, 10 * PAGE_SIZE / 1024);
191 /* usage(mkswap_usage); */
194 if (PAGES > 8 * (PAGE_SIZE - 10)) {
195 PAGES = 8 * (PAGE_SIZE - 10);
196 fprintf(stderr, "%s: warning: truncating swap area to %ldkB\n",
197 program_name, PAGES * PAGE_SIZE / 1024);
199 DEV = open(device_name,O_RDWR);
200 if (DEV < 0 || fstat(DEV, &statbuf) < 0) {
204 if (!S_ISBLK(statbuf.st_mode))
206 else if (statbuf.st_rdev == 0x0300 || statbuf.st_rdev == 0x0340)
207 die("Will not try to make swapdevice on '%s'");
208 check_blocks(signature_page);
209 if (!bit_test_and_clear(signature_page,0))
210 die("fatal: first page unreadable");
211 goodpages = PAGES - badpages - 1;
213 die("Unable to set up swap-space: unreadable");
214 printf("Setting up swapspace, size = %ld bytes\n",goodpages*PAGE_SIZE);
215 strncpy((char*)signature_page+PAGE_SIZE-10,"SWAP-SPACE",10);
216 if (lseek(DEV, 0, SEEK_SET))
217 die("unable to rewind swap-device");
218 if (PAGE_SIZE != write(DEV, signature_page, PAGE_SIZE))
219 die("unable to write signature page");
225 int mkswap_main(int argc, char ** argv)
232 program_name = *argv;
236 if (argv[0][0] != '-')
238 pages = strtol(argv[0],&tmp,0)>>(PAGE_SHIFT-10);
240 usage (mkswap_usage);
243 device_name = argv[0];
244 else while (*++argv[0])
245 switch (argv[0][0]) {
246 case 'c': check=1; break;
247 default: usage (mkswap_usage);
251 exit( mkswap(device_name, pages, check));