This source file includes following definitions.
- show_swap_cache_info
- add_to_swap_cache
- init_swap_cache
- rw_swap_page
- get_swap_page
- swap_duplicate
- swap_free
- swap_in
- try_to_swap_out
- swap_out_process
- swap_out
- try_to_free_page
- add_mem_queue
- remove_mem_queue
- free_pages_ok
- check_free_buffers
- free_pages
- mark_used
- __get_free_pages
- show_free_areas
- try_to_unuse
- sys_swapoff
- sys_swapon
- si_swapinfo
- free_area_init
1
2
3
4
5
6
7
8
9
10
11
12 #include <linux/mm.h>
13 #include <linux/sched.h>
14 #include <linux/head.h>
15 #include <linux/kernel.h>
16 #include <linux/kernel_stat.h>
17 #include <linux/errno.h>
18 #include <linux/string.h>
19 #include <linux/stat.h>
20 #include <linux/fs.h>
21
22 #include <asm/system.h>
23 #include <asm/bitops.h>
24
25 #define MAX_SWAPFILES 8
26
27 #define SWP_USED 1
28 #define SWP_WRITEOK 3
29
30 #define SWP_TYPE(entry) (((entry) & 0xfe) >> 1)
31 #define SWP_OFFSET(entry) ((entry) >> PAGE_SHIFT)
32 #define SWP_ENTRY(type,offset) (((type) << 1) | ((offset) << PAGE_SHIFT))
33
34 static int nr_swapfiles = 0;
35 static struct wait_queue * lock_queue = NULL;
36
37 static struct swap_info_struct {
38 unsigned long flags;
39 struct inode * swap_file;
40 unsigned int swap_device;
41 unsigned char * swap_map;
42 unsigned char * swap_lockmap;
43 int pages;
44 int lowest_bit;
45 int highest_bit;
46 unsigned long max;
47 } swap_info[MAX_SWAPFILES];
48
49 extern int shm_swap (int);
50
51 unsigned long *swap_cache;
52
53 #ifdef SWAP_CACHE_INFO
54 unsigned long swap_cache_add_total = 0;
55 unsigned long swap_cache_add_success = 0;
56 unsigned long swap_cache_del_total = 0;
57 unsigned long swap_cache_del_success = 0;
58 unsigned long swap_cache_find_total = 0;
59 unsigned long swap_cache_find_success = 0;
60
61 extern inline void show_swap_cache_info(void)
62 {
63 printk("Swap cache: add %ld/%ld, delete %ld/%ld, find %ld/%ld\n",
64 swap_cache_add_total, swap_cache_add_success,
65 swap_cache_del_total, swap_cache_del_success,
66 swap_cache_find_total, swap_cache_find_success);
67 }
68 #endif
69
70 extern inline int add_to_swap_cache(unsigned long addr, unsigned long entry)
71 {
72 struct swap_info_struct * p = &swap_info[SWP_TYPE(entry)];
73
74 #ifdef SWAP_CACHE_INFO
75 swap_cache_add_total++;
76 #endif
77 if ((p->flags & SWP_WRITEOK) == SWP_WRITEOK) {
78 __asm__ __volatile__ (
79 "xchgl %0,%1\n"
80 : "=m" (swap_cache[addr >> PAGE_SHIFT]),
81 "=r" (entry)
82 : "0" (swap_cache[addr >> PAGE_SHIFT]),
83 "1" (entry)
84 );
85 if (entry) {
86 printk("swap_cache: replacing non-NULL entry\n");
87 }
88 #ifdef SWAP_CACHE_INFO
89 swap_cache_add_success++;
90 #endif
91 return 1;
92 }
93 return 0;
94 }
95
96 static unsigned long init_swap_cache(unsigned long mem_start,
97 unsigned long mem_end)
98 {
99 unsigned long swap_cache_size;
100
101 mem_start = (mem_start + 15) & ~15;
102 swap_cache = (unsigned long *) mem_start;
103 swap_cache_size = mem_end >> PAGE_SHIFT;
104 memset(swap_cache, 0, swap_cache_size * sizeof (unsigned long));
105 return (unsigned long) (swap_cache + swap_cache_size);
106 }
107
108 void rw_swap_page(int rw, unsigned long entry, char * buf)
109 {
110 unsigned long type, offset;
111 struct swap_info_struct * p;
112
113 type = SWP_TYPE(entry);
114 if (type >= nr_swapfiles) {
115 printk("Internal error: bad swap-device\n");
116 return;
117 }
118 p = &swap_info[type];
119 offset = SWP_OFFSET(entry);
120 if (offset >= p->max) {
121 printk("rw_swap_page: weirdness\n");
122 return;
123 }
124 if (!(p->flags & SWP_USED)) {
125 printk("Trying to swap to unused swap-device\n");
126 return;
127 }
128 while (set_bit(offset,p->swap_lockmap))
129 sleep_on(&lock_queue);
130 if (rw == READ)
131 kstat.pswpin++;
132 else
133 kstat.pswpout++;
134 if (p->swap_device) {
135 ll_rw_page(rw,p->swap_device,offset,buf);
136 } else if (p->swap_file) {
137 struct inode *swapf = p->swap_file;
138 unsigned int zones[8];
139 int i;
140 if (swapf->i_op->bmap == NULL
141 && swapf->i_op->smap != NULL){
142
143
144
145
146
147
148
149
150
151
152
153
154 int j;
155 unsigned int block = offset << 3;
156
157 for (i=0, j=0; j< PAGE_SIZE ; i++, j += 512){
158 if (!(zones[i] = swapf->i_op->smap(swapf,block++))) {
159 printk("rw_swap_page: bad swap file\n");
160 return;
161 }
162 }
163 }else{
164 int j;
165 unsigned int block = offset
166 << (12 - swapf->i_sb->s_blocksize_bits);
167
168 for (i=0, j=0; j< PAGE_SIZE ; i++, j +=swapf->i_sb->s_blocksize)
169 if (!(zones[i] = bmap(swapf,block++))) {
170 printk("rw_swap_page: bad swap file\n");
171 return;
172 }
173 }
174 ll_rw_swap_file(rw,swapf->i_dev, zones, i,buf);
175 } else
176 printk("re_swap_page: no swap file or device\n");
177 if (offset && !clear_bit(offset,p->swap_lockmap))
178 printk("rw_swap_page: lock already cleared\n");
179 wake_up(&lock_queue);
180 }
181
182 unsigned int get_swap_page(void)
183 {
184 struct swap_info_struct * p;
185 unsigned int offset, type;
186
187 p = swap_info;
188 for (type = 0 ; type < nr_swapfiles ; type++,p++) {
189 if ((p->flags & SWP_WRITEOK) != SWP_WRITEOK)
190 continue;
191 for (offset = p->lowest_bit; offset <= p->highest_bit ; offset++) {
192 if (p->swap_map[offset])
193 continue;
194 p->swap_map[offset] = 1;
195 nr_swap_pages--;
196 if (offset == p->highest_bit)
197 p->highest_bit--;
198 p->lowest_bit = offset;
199 return SWP_ENTRY(type,offset);
200 }
201 }
202 return 0;
203 }
204
205 unsigned long swap_duplicate(unsigned long entry)
206 {
207 struct swap_info_struct * p;
208 unsigned long offset, type;
209
210 if (!entry)
211 return 0;
212 offset = SWP_OFFSET(entry);
213 type = SWP_TYPE(entry);
214 if (type == SHM_SWP_TYPE)
215 return entry;
216 if (type >= nr_swapfiles) {
217 printk("Trying to duplicate nonexistent swap-page\n");
218 return 0;
219 }
220 p = type + swap_info;
221 if (offset >= p->max) {
222 printk("swap_duplicate: weirdness\n");
223 return 0;
224 }
225 if (!p->swap_map[offset]) {
226 printk("swap_duplicate: trying to duplicate unused page\n");
227 return 0;
228 }
229 p->swap_map[offset]++;
230 return entry;
231 }
232
233 void swap_free(unsigned long entry)
234 {
235 struct swap_info_struct * p;
236 unsigned long offset, type;
237
238 if (!entry)
239 return;
240 type = SWP_TYPE(entry);
241 if (type == SHM_SWP_TYPE)
242 return;
243 if (type >= nr_swapfiles) {
244 printk("Trying to free nonexistent swap-page\n");
245 return;
246 }
247 p = & swap_info[type];
248 offset = SWP_OFFSET(entry);
249 if (offset >= p->max) {
250 printk("swap_free: weirdness\n");
251 return;
252 }
253 if (!(p->flags & SWP_USED)) {
254 printk("Trying to free swap from unused swap-device\n");
255 return;
256 }
257 while (set_bit(offset,p->swap_lockmap))
258 sleep_on(&lock_queue);
259 if (offset < p->lowest_bit)
260 p->lowest_bit = offset;
261 if (offset > p->highest_bit)
262 p->highest_bit = offset;
263 if (!p->swap_map[offset])
264 printk("swap_free: swap-space map bad (entry %08lx)\n",entry);
265 else
266 if (!--p->swap_map[offset])
267 nr_swap_pages++;
268 if (!clear_bit(offset,p->swap_lockmap))
269 printk("swap_free: lock already cleared\n");
270 wake_up(&lock_queue);
271 }
272
273 unsigned long swap_in(unsigned long entry)
274 {
275 unsigned long page;
276
277 if (!(page = get_free_page(GFP_KERNEL))) {
278 oom(current);
279 return BAD_PAGE;
280 }
281 read_swap_page(entry, (char *) page);
282 if (add_to_swap_cache(page, entry))
283 return page | PAGE_PRESENT;
284 swap_free(entry);
285 return page | PAGE_DIRTY | PAGE_PRESENT;
286 }
287
288 static inline int try_to_swap_out(unsigned long * table_ptr)
289 {
290 unsigned long page, entry;
291
292 page = *table_ptr;
293 if (!(PAGE_PRESENT & page))
294 return 0;
295 if (page >= high_memory)
296 return 0;
297 if (mem_map[MAP_NR(page)] & MAP_PAGE_RESERVED)
298 return 0;
299
300 if ((PAGE_DIRTY & page) && delete_from_swap_cache(page)) {
301 *table_ptr &= ~PAGE_ACCESSED;
302 return 0;
303 }
304 if (PAGE_ACCESSED & page) {
305 *table_ptr &= ~PAGE_ACCESSED;
306 return 0;
307 }
308 if (PAGE_DIRTY & page) {
309 page &= PAGE_MASK;
310 if (mem_map[MAP_NR(page)] != 1)
311 return 0;
312 if (!(entry = get_swap_page()))
313 return 0;
314 *table_ptr = entry;
315 invalidate();
316 write_swap_page(entry, (char *) page);
317 free_page(page);
318 return 1;
319 }
320 if ((entry = find_in_swap_cache(page))) {
321 if (mem_map[MAP_NR(page)] != 1) {
322 *table_ptr |= PAGE_DIRTY;
323 printk("Aiee.. duplicated cached swap-cache entry\n");
324 return 0;
325 }
326 *table_ptr = entry;
327 invalidate();
328 free_page(page & PAGE_MASK);
329 return 1;
330 }
331 page &= PAGE_MASK;
332 *table_ptr = 0;
333 invalidate();
334 free_page(page);
335 return 1 + mem_map[MAP_NR(page)];
336 }
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356 #define SWAP_MIN 4
357 #define SWAP_MAX 32
358
359
360
361
362
363 #define SWAP_RATIO 128
364
365 static int swap_out_process(struct task_struct * p)
366 {
367 unsigned long address;
368 unsigned long offset;
369 unsigned long *pgdir;
370 unsigned long pg_table;
371
372
373
374
375 address = p->mm->swap_address;
376 pgdir = (address >> PGDIR_SHIFT) + (unsigned long *) p->tss.cr3;
377 offset = address & ~PGDIR_MASK;
378 address &= PGDIR_MASK;
379 for ( ; address < TASK_SIZE ;
380 pgdir++, address = address + PGDIR_SIZE, offset = 0) {
381 pg_table = *pgdir;
382 if (pg_table >= high_memory)
383 continue;
384 if (mem_map[MAP_NR(pg_table)] & MAP_PAGE_RESERVED)
385 continue;
386 if (!(PAGE_PRESENT & pg_table)) {
387 printk("swap_out_process (%s): bad page-table at vm %08lx: %08lx\n",
388 p->comm, address + offset, pg_table);
389 *pgdir = 0;
390 continue;
391 }
392 pg_table &= 0xfffff000;
393
394
395
396
397 for( ; offset < ~PGDIR_MASK ; offset += PAGE_SIZE) {
398 switch(try_to_swap_out((unsigned long *) (pg_table + (offset >> 10)))) {
399 case 0:
400 break;
401
402 case 1:
403 p->mm->rss--;
404
405 p->mm->swap_address = address + offset + PAGE_SIZE;
406 return 1;
407
408 default:
409 p->mm->rss--;
410 break;
411 }
412 }
413 }
414
415
416
417
418 p->mm->swap_address = 0;
419 return 0;
420 }
421
422 static int swap_out(unsigned int priority)
423 {
424 static int swap_task;
425 int loop;
426 int counter = NR_TASKS * 2 >> priority;
427 struct task_struct *p;
428
429 counter = NR_TASKS * 2 >> priority;
430 for(; counter >= 0; counter--, swap_task++) {
431
432
433
434
435 loop = 0;
436 while(1) {
437 if (swap_task >= NR_TASKS) {
438 swap_task = 1;
439 if (loop)
440
441 return 0;
442 loop = 1;
443 }
444
445 p = task[swap_task];
446 if (p && p->mm->swappable && p->mm->rss)
447 break;
448
449 swap_task++;
450 }
451
452
453
454
455 if (!p->mm->swap_cnt) {
456 p->mm->dec_flt = (p->mm->dec_flt * 3) / 4 + p->mm->maj_flt - p->mm->old_maj_flt;
457 p->mm->old_maj_flt = p->mm->maj_flt;
458
459 if (p->mm->dec_flt >= SWAP_RATIO / SWAP_MIN) {
460 p->mm->dec_flt = SWAP_RATIO / SWAP_MIN;
461 p->mm->swap_cnt = SWAP_MIN;
462 } else if (p->mm->dec_flt <= SWAP_RATIO / SWAP_MAX)
463 p->mm->swap_cnt = SWAP_MAX;
464 else
465 p->mm->swap_cnt = SWAP_RATIO / p->mm->dec_flt;
466 }
467 if (swap_out_process(p)) {
468 if ((--p->mm->swap_cnt) == 0)
469 swap_task++;
470 return 1;
471 }
472 }
473 return 0;
474 }
475
476 static int try_to_free_page(int priority)
477 {
478 int i=6;
479
480 while (i--) {
481 if (priority != GFP_NOBUFFER && shrink_buffers(i))
482 return 1;
483 if (shm_swap(i))
484 return 1;
485 if (swap_out(i))
486 return 1;
487 }
488 return 0;
489 }
490
491 static inline void add_mem_queue(struct mem_list * head, struct mem_list * entry)
492 {
493 entry->prev = head;
494 entry->next = head->next;
495 entry->next->prev = entry;
496 head->next = entry;
497 }
498
499 static inline void remove_mem_queue(struct mem_list * head, struct mem_list * entry)
500 {
501 entry->next->prev = entry->prev;
502 entry->prev->next = entry->next;
503 }
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520 static inline void free_pages_ok(unsigned long addr, unsigned long order)
521 {
522 unsigned long index = addr >> (PAGE_SHIFT + 1 + order);
523 unsigned long mask = PAGE_MASK << order;
524
525 addr &= mask;
526 nr_free_pages += 1 << order;
527 while (order < NR_MEM_LISTS-1) {
528 if (!change_bit(index, free_area_map[order]))
529 break;
530 remove_mem_queue(free_area_list+order, (struct mem_list *) (addr ^ (1+~mask)));
531 order++;
532 index >>= 1;
533 mask <<= 1;
534 addr &= mask;
535 }
536 add_mem_queue(free_area_list+order, (struct mem_list *) addr);
537 }
538
539 static inline void check_free_buffers(unsigned long addr)
540 {
541 struct buffer_head * bh;
542
543 bh = buffer_pages[MAP_NR(addr)];
544 if (bh) {
545 struct buffer_head *tmp = bh;
546 do {
547 if (tmp->b_list == BUF_SHARED && tmp->b_dev != 0xffff)
548 refile_buffer(tmp);
549 tmp = tmp->b_this_page;
550 } while (tmp != bh);
551 }
552 }
553
554 void free_pages(unsigned long addr, unsigned long order)
555 {
556 if (addr < high_memory) {
557 unsigned long flag;
558 unsigned short * map = mem_map + MAP_NR(addr);
559 if (*map) {
560 if (!(*map & MAP_PAGE_RESERVED)) {
561 save_flags(flag);
562 cli();
563 if (!--*map) {
564 free_pages_ok(addr, order);
565 delete_from_swap_cache(addr);
566 }
567 restore_flags(flag);
568 if (*map == 1)
569 check_free_buffers(addr);
570 }
571 return;
572 }
573 printk("Trying to free free memory (%08lx): memory probably corrupted\n",addr);
574 printk("PC = %08lx\n",*(((unsigned long *)&addr)-1));
575 return;
576 }
577 }
578
579
580
581
582 #define RMQUEUE(order) \
583 do { struct mem_list * queue = free_area_list+order; \
584 unsigned long new_order = order; \
585 do { struct mem_list *next = queue->next; \
586 if (queue != next) { \
587 queue->next = next->next; \
588 next->next->prev = queue; \
589 mark_used((unsigned long) next, new_order); \
590 nr_free_pages -= 1 << order; \
591 restore_flags(flags); \
592 EXPAND(next, order, new_order); \
593 return (unsigned long) next; \
594 } new_order++; queue++; \
595 } while (new_order < NR_MEM_LISTS); \
596 } while (0)
597
598 static inline int mark_used(unsigned long addr, unsigned long order)
599 {
600 return change_bit(addr >> (PAGE_SHIFT+1+order), free_area_map[order]);
601 }
602
603 #define EXPAND(addr,low,high) \
604 do { unsigned long size = PAGE_SIZE << high; \
605 while (high > low) { \
606 high--; size >>= 1; cli(); \
607 add_mem_queue(free_area_list+high, addr); \
608 mark_used((unsigned long) addr, high); \
609 restore_flags(flags); \
610 addr = (struct mem_list *) (size + (unsigned long) addr); \
611 } mem_map[MAP_NR((unsigned long) addr)] = 1; \
612 } while (0)
613
614 unsigned long __get_free_pages(int priority, unsigned long order)
615 {
616 unsigned long flags;
617
618 if (intr_count && priority != GFP_ATOMIC) {
619 static int count = 0;
620 if (++count < 5) {
621 printk("gfp called nonatomically from interrupt %08lx\n",
622 ((unsigned long *)&priority)[-1]);
623 priority = GFP_ATOMIC;
624 }
625 }
626 save_flags(flags);
627 repeat:
628 cli();
629 if ((priority==GFP_ATOMIC) || nr_free_pages > MAX_SECONDARY_PAGES) {
630 RMQUEUE(order);
631 restore_flags(flags);
632 return 0;
633 }
634 restore_flags(flags);
635 if (priority != GFP_BUFFER && try_to_free_page(priority))
636 goto repeat;
637 return 0;
638 }
639
640
641
642
643
644
645 void show_free_areas(void)
646 {
647 unsigned long order, flags;
648 unsigned long total = 0;
649
650 printk("Free pages: %6dkB\n ( ",nr_free_pages<<(PAGE_SHIFT-10));
651 save_flags(flags);
652 cli();
653 for (order=0 ; order < NR_MEM_LISTS; order++) {
654 struct mem_list * tmp;
655 unsigned long nr = 0;
656 for (tmp = free_area_list[order].next ; tmp != free_area_list + order ; tmp = tmp->next) {
657 nr ++;
658 }
659 total += nr * (4 << order);
660 printk("%lu*%ukB ", nr, 4 << order);
661 }
662 restore_flags(flags);
663 printk("= %lukB)\n", total);
664 #ifdef SWAP_CACHE_INFO
665 show_swap_cache_info();
666 #endif
667 }
668
669
670
671
672
673
674 static int try_to_unuse(unsigned int type)
675 {
676 int nr, pgt, pg;
677 unsigned long page, *ppage;
678 unsigned long tmp = 0;
679 struct task_struct *p;
680
681 nr = 0;
682
683
684
685
686
687 repeat:
688 for (; nr < NR_TASKS ; nr++) {
689 p = task[nr];
690 if (!p)
691 continue;
692 for (pgt = 0 ; pgt < PTRS_PER_PAGE ; pgt++) {
693 ppage = pgt + ((unsigned long *) p->tss.cr3);
694 page = *ppage;
695 if (!page)
696 continue;
697 if (!(page & PAGE_PRESENT) || (page >= high_memory))
698 continue;
699 if (mem_map[MAP_NR(page)] & MAP_PAGE_RESERVED)
700 continue;
701 ppage = (unsigned long *) (page & PAGE_MASK);
702 for (pg = 0 ; pg < PTRS_PER_PAGE ; pg++,ppage++) {
703 page = *ppage;
704 if (!page)
705 continue;
706 if (page & PAGE_PRESENT) {
707 if (!(page = in_swap_cache(page)))
708 continue;
709 if (SWP_TYPE(page) != type)
710 continue;
711 *ppage |= PAGE_DIRTY;
712 delete_from_swap_cache(*ppage);
713 continue;
714 }
715 if (SWP_TYPE(page) != type)
716 continue;
717 if (!tmp) {
718 if (!(tmp = __get_free_page(GFP_KERNEL)))
719 return -ENOMEM;
720 goto repeat;
721 }
722 read_swap_page(page, (char *) tmp);
723 if (*ppage == page) {
724 *ppage = tmp | (PAGE_DIRTY | PAGE_PRIVATE);
725 ++p->mm->rss;
726 swap_free(page);
727 tmp = 0;
728 }
729 goto repeat;
730 }
731 }
732 }
733 free_page(tmp);
734 return 0;
735 }
736
737 asmlinkage int sys_swapoff(const char * specialfile)
738 {
739 struct swap_info_struct * p;
740 struct inode * inode;
741 unsigned int type;
742 int i;
743
744 if (!suser())
745 return -EPERM;
746 i = namei(specialfile,&inode);
747 if (i)
748 return i;
749 p = swap_info;
750 for (type = 0 ; type < nr_swapfiles ; type++,p++) {
751 if ((p->flags & SWP_WRITEOK) != SWP_WRITEOK)
752 continue;
753 if (p->swap_file) {
754 if (p->swap_file == inode)
755 break;
756 } else {
757 if (!S_ISBLK(inode->i_mode))
758 continue;
759 if (p->swap_device == inode->i_rdev)
760 break;
761 }
762 }
763 iput(inode);
764 if (type >= nr_swapfiles)
765 return -EINVAL;
766 p->flags = SWP_USED;
767 i = try_to_unuse(type);
768 if (i) {
769 p->flags = SWP_WRITEOK;
770 return i;
771 }
772 nr_swap_pages -= p->pages;
773 iput(p->swap_file);
774 p->swap_file = NULL;
775 p->swap_device = 0;
776 vfree(p->swap_map);
777 p->swap_map = NULL;
778 free_page((long) p->swap_lockmap);
779 p->swap_lockmap = NULL;
780 p->flags = 0;
781 return 0;
782 }
783
784
785
786
787
788
789 asmlinkage int sys_swapon(const char * specialfile)
790 {
791 struct swap_info_struct * p;
792 struct inode * swap_inode;
793 unsigned int type;
794 int i,j;
795 int error;
796
797 if (!suser())
798 return -EPERM;
799 p = swap_info;
800 for (type = 0 ; type < nr_swapfiles ; type++,p++)
801 if (!(p->flags & SWP_USED))
802 break;
803 if (type >= MAX_SWAPFILES)
804 return -EPERM;
805 if (type >= nr_swapfiles)
806 nr_swapfiles = type+1;
807 p->flags = SWP_USED;
808 p->swap_file = NULL;
809 p->swap_device = 0;
810 p->swap_map = NULL;
811 p->swap_lockmap = NULL;
812 p->lowest_bit = 0;
813 p->highest_bit = 0;
814 p->max = 1;
815 error = namei(specialfile,&swap_inode);
816 if (error)
817 goto bad_swap;
818 p->swap_file = swap_inode;
819 error = -EBUSY;
820 if (swap_inode->i_count != 1)
821 goto bad_swap;
822 error = -EINVAL;
823 if (S_ISBLK(swap_inode->i_mode)) {
824 p->swap_device = swap_inode->i_rdev;
825 p->swap_file = NULL;
826 iput(swap_inode);
827 error = -ENODEV;
828 if (!p->swap_device)
829 goto bad_swap;
830 error = -EBUSY;
831 for (i = 0 ; i < nr_swapfiles ; i++) {
832 if (i == type)
833 continue;
834 if (p->swap_device == swap_info[i].swap_device)
835 goto bad_swap;
836 }
837 } else if (!S_ISREG(swap_inode->i_mode))
838 goto bad_swap;
839 p->swap_lockmap = (unsigned char *) get_free_page(GFP_USER);
840 if (!p->swap_lockmap) {
841 printk("Unable to start swapping: out of memory :-)\n");
842 error = -ENOMEM;
843 goto bad_swap;
844 }
845 read_swap_page(SWP_ENTRY(type,0), (char *) p->swap_lockmap);
846 if (memcmp("SWAP-SPACE",p->swap_lockmap+4086,10)) {
847 printk("Unable to find swap-space signature\n");
848 error = -EINVAL;
849 goto bad_swap;
850 }
851 memset(p->swap_lockmap+PAGE_SIZE-10,0,10);
852 j = 0;
853 p->lowest_bit = 0;
854 p->highest_bit = 0;
855 for (i = 1 ; i < 8*PAGE_SIZE ; i++) {
856 if (test_bit(i,p->swap_lockmap)) {
857 if (!p->lowest_bit)
858 p->lowest_bit = i;
859 p->highest_bit = i;
860 p->max = i+1;
861 j++;
862 }
863 }
864 if (!j) {
865 printk("Empty swap-file\n");
866 error = -EINVAL;
867 goto bad_swap;
868 }
869 p->swap_map = (unsigned char *) vmalloc(p->max);
870 if (!p->swap_map) {
871 error = -ENOMEM;
872 goto bad_swap;
873 }
874 for (i = 1 ; i < p->max ; i++) {
875 if (test_bit(i,p->swap_lockmap))
876 p->swap_map[i] = 0;
877 else
878 p->swap_map[i] = 0x80;
879 }
880 p->swap_map[0] = 0x80;
881 memset(p->swap_lockmap,0,PAGE_SIZE);
882 p->flags = SWP_WRITEOK;
883 p->pages = j;
884 nr_swap_pages += j;
885 printk("Adding Swap: %dk swap-space\n",j<<2);
886 return 0;
887 bad_swap:
888 free_page((long) p->swap_lockmap);
889 vfree(p->swap_map);
890 iput(p->swap_file);
891 p->swap_device = 0;
892 p->swap_file = NULL;
893 p->swap_map = NULL;
894 p->swap_lockmap = NULL;
895 p->flags = 0;
896 return error;
897 }
898
899 void si_swapinfo(struct sysinfo *val)
900 {
901 unsigned int i, j;
902
903 val->freeswap = val->totalswap = 0;
904 for (i = 0; i < nr_swapfiles; i++) {
905 if ((swap_info[i].flags & SWP_WRITEOK) != SWP_WRITEOK)
906 continue;
907 for (j = 0; j < swap_info[i].max; ++j)
908 switch (swap_info[i].swap_map[j]) {
909 case 128:
910 continue;
911 case 0:
912 ++val->freeswap;
913 default:
914 ++val->totalswap;
915 }
916 }
917 val->freeswap <<= PAGE_SHIFT;
918 val->totalswap <<= PAGE_SHIFT;
919 return;
920 }
921
922
923
924
925
926
927
928 unsigned long free_area_init(unsigned long start_mem, unsigned long end_mem)
929 {
930 unsigned short * p;
931 unsigned long mask = PAGE_MASK;
932 int i;
933
934 start_mem = init_swap_cache(start_mem, end_mem);
935 mem_map = (unsigned short *) start_mem;
936 p = mem_map + MAP_NR(end_mem);
937 start_mem = (unsigned long) p;
938 while (p > mem_map)
939 *--p = MAP_PAGE_RESERVED;
940
941 for (i = 0 ; i < NR_MEM_LISTS ; i++, mask <<= 1) {
942 unsigned long bitmap_size;
943 free_area_list[i].prev = free_area_list[i].next = &free_area_list[i];
944 end_mem = (end_mem + ~mask) & mask;
945 bitmap_size = end_mem >> (PAGE_SHIFT + i);
946 bitmap_size = (bitmap_size + 7) >> 3;
947 free_area_map[i] = (unsigned char *) start_mem;
948 memset((void *) start_mem, 0, bitmap_size);
949 start_mem += bitmap_size;
950 }
951 return start_mem;
952 }