root/mm/swap.c

/* [previous][next][first][last][top][bottom][index][help] */

DEFINITIONS

This source file includes following definitions.
  1. show_swap_cache_info
  2. add_to_swap_cache
  3. init_swap_cache
  4. rw_swap_page
  5. get_swap_page
  6. swap_duplicate
  7. swap_free
  8. swap_in
  9. try_to_swap_out
  10. swap_out_process
  11. swap_out
  12. try_to_free_page
  13. add_mem_queue
  14. remove_mem_queue
  15. free_pages_ok
  16. check_free_buffers
  17. free_pages
  18. mark_used
  19. __get_free_pages
  20. show_free_areas
  21. try_to_unuse
  22. sys_swapoff
  23. sys_swapon
  24. si_swapinfo
  25. free_area_init

   1 /*
   2  *  linux/mm/swap.c
   3  *
   4  *  Copyright (C) 1991, 1992, 1993, 1994  Linus Torvalds
   5  */
   6 
   7 /*
   8  * This file should contain most things doing the swapping from/to disk.
   9  * Started 18.12.91
  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> /* for cli()/sti() */
  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)
     /* [previous][next][first][last][top][bottom][index][help] */
  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)
     /* [previous][next][first][last][top][bottom][index][help] */
  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,
     /* [previous][next][first][last][top][bottom][index][help] */
  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)
     /* [previous][next][first][last][top][bottom][index][help] */
 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                                 With MsDOS, we use msdos_smap which return
 144                                 a sector number (not a cluster or block number).
 145                                 It is a patch to enable the UMSDOS project.
 146                                 Other people are working on better solution.
 147 
 148                                 It sounds like ll_rw_swap_file defined
 149                                 it operation size (sector size) based on
 150                                 PAGE_SIZE and the number of block to read.
 151                                 So using bmap or smap should work even if
 152                                 smap will require more blocks.
 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)
     /* [previous][next][first][last][top][bottom][index][help] */
 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)
     /* [previous][next][first][last][top][bottom][index][help] */
 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)
     /* [previous][next][first][last][top][bottom][index][help] */
 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)
     /* [previous][next][first][last][top][bottom][index][help] */
 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_PRIVATE;
 284         swap_free(entry);
 285         return page | PAGE_DIRTY | PAGE_PRIVATE;
 286 }
 287 
 288 static inline int try_to_swap_out(unsigned long * table_ptr)
     /* [previous][next][first][last][top][bottom][index][help] */
 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  * A new implementation of swap_out().  We do not swap complete processes,
 340  * but only a small number of blocks, before we continue with the next
 341  * process.  The number of blocks actually swapped is determined on the
 342  * number of page faults, that this process actually had in the last time,
 343  * so we won't swap heavily used processes all the time ...
 344  *
 345  * Note: the priority argument is a hint on much CPU to waste with the
 346  *       swap block search, not a hint, of how much blocks to swap with
 347  *       each process.
 348  *
 349  * (C) 1993 Kai Petzke, wpp@marie.physik.tu-berlin.de
 350  */
 351 
 352 /*
 353  * These are the minimum and maximum number of pages to swap from one process,
 354  * before proceeding to the next:
 355  */
 356 #define SWAP_MIN        4
 357 #define SWAP_MAX        32
 358 
 359 /*
 360  * The actual number of pages to swap is determined as:
 361  * SWAP_RATIO / (number of recent major page faults)
 362  */
 363 #define SWAP_RATIO      128
 364 
 365 static int swap_out_process(struct task_struct * p)
     /* [previous][next][first][last][top][bottom][index][help] */
 366 {
 367         unsigned long address;
 368         unsigned long offset;
 369         unsigned long *pgdir;
 370         unsigned long pg_table;
 371 
 372         /*
 373          * Go through process' page directory.
 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                  * Go through this page table.
 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                                         /* continue with the following page the next time */
 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          * Finish work with this process, if we reached the end of the page
 416          * directory.  Mark restart from the beginning the next time.
 417          */
 418         p->mm->swap_address = 0;
 419         return 0;
 420 }
 421 
 422 static int swap_out(unsigned int priority)
     /* [previous][next][first][last][top][bottom][index][help] */
 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                  * Check that swap_task is suitable for swapping.  If not, look for
 433                  * the next suitable process.
 434                  */
 435                 loop = 0;
 436                 while(1) {
 437                         if (swap_task >= NR_TASKS) {
 438                                 swap_task = 1;
 439                                 if (loop)
 440                                         /* all processes are unswappable or already swapped out */
 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                  * Determine the number of pages to swap from this process.
 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)
     /* [previous][next][first][last][top][bottom][index][help] */
 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)
     /* [previous][next][first][last][top][bottom][index][help] */
 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)
     /* [previous][next][first][last][top][bottom][index][help] */
 500 {
 501         entry->next->prev = entry->prev;
 502         entry->prev->next = entry->next;
 503 }
 504 
 505 /*
 506  * Free_page() adds the page to the free lists. This is optimized for
 507  * fast normal cases (no error jumps taken normally).
 508  *
 509  * The way to optimize jumps for gcc-2.2.2 is to:
 510  *  - select the "normal" case and put it inside the if () { XXX }
 511  *  - no else-statements if you can avoid them
 512  *
 513  * With the above two rules, you get a straight-line execution path
 514  * for the normal case, giving better asm-code.
 515  */
 516 
 517 /*
 518  * Buddy system. Hairy. You really aren't expected to understand this
 519  */
 520 static inline void free_pages_ok(unsigned long addr, unsigned long order)
     /* [previous][next][first][last][top][bottom][index][help] */
 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)
     /* [previous][next][first][last][top][bottom][index][help] */
 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)
     /* [previous][next][first][last][top][bottom][index][help] */
 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  * Some ugly macros to speed up __get_free_pages()..
 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)
     /* [previous][next][first][last][top][bottom][index][help] */
 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)
     /* [previous][next][first][last][top][bottom][index][help] */
 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  * Show free area list (used inside shift_scroll-lock stuff)
 642  * We also calculate the percentage fragmentation. We do this by counting the
 643  * memory on each free list with the exception of the first item on the list.
 644  */
 645 void show_free_areas(void)
     /* [previous][next][first][last][top][bottom][index][help] */
 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  * Trying to stop swapping from a file is fraught with races, so
 671  * we repeat quite a bit here when we have to pause. swapoff()
 672  * isn't exactly timing-critical, so who cares?
 673  */
 674 static int try_to_unuse(unsigned int type)
     /* [previous][next][first][last][top][bottom][index][help] */
 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  * When we have to sleep, we restart the whole algorithm from the same
 685  * task we stopped in. That at least rids us of all races.
 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)
     /* [previous][next][first][last][top][bottom][index][help] */
 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  * Written 01/25/92 by Simmule Turner, heavily changed by Linus.
 786  *
 787  * The swapon system call
 788  */
 789 asmlinkage int sys_swapon(const char * specialfile)
     /* [previous][next][first][last][top][bottom][index][help] */
 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)
     /* [previous][next][first][last][top][bottom][index][help] */
 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  * set up the free-area data structures:
 924  *   - mark all pages MAP_PAGE_RESERVED
 925  *   - mark all memory queues empty
 926  *   - clear the memory bitmaps
 927  */
 928 unsigned long free_area_init(unsigned long start_mem, unsigned long end_mem)
     /* [previous][next][first][last][top][bottom][index][help] */
 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 }

/* [previous][next][first][last][top][bottom][index][help] */