root/mm/swap.c

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

DEFINITIONS

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

   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 
  21 #include <asm/system.h> /* for cli()/sti() */
  22 #include <asm/bitops.h>
  23 
  24 #define MAX_SWAPFILES 8
  25 
  26 #define SWP_USED        1
  27 #define SWP_WRITEOK     3
  28 
  29 #define SWP_TYPE(entry) (((entry) & 0xfe) >> 1)
  30 #define SWP_OFFSET(entry) ((entry) >> PAGE_SHIFT)
  31 #define SWP_ENTRY(type,offset) (((type) << 1) | ((offset) << PAGE_SHIFT))
  32 
  33 static int nr_swapfiles = 0;
  34 static struct wait_queue * lock_queue = NULL;
  35 
  36 static struct swap_info_struct {
  37         unsigned long flags;
  38         struct inode * swap_file;
  39         unsigned int swap_device;
  40         unsigned char * swap_map;
  41         unsigned char * swap_lockmap;
  42         int pages;
  43         int lowest_bit;
  44         int highest_bit;
  45         unsigned long max;
  46 } swap_info[MAX_SWAPFILES];
  47 
  48 extern int shm_swap (int);
  49 
  50 /*
  51  * The following are used to make sure we don't thrash too much...
  52  * NOTE!! NR_LAST_FREE_PAGES must be a power of 2...
  53  */
  54 #define NR_LAST_FREE_PAGES 32
  55 static unsigned long last_free_pages[NR_LAST_FREE_PAGES] = {0,};
  56 
  57 void rw_swap_page(int rw, unsigned long entry, char * buf)
     /* [previous][next][first][last][top][bottom][index][help] */
  58 {
  59         unsigned long type, offset;
  60         struct swap_info_struct * p;
  61 
  62         type = SWP_TYPE(entry);
  63         if (type >= nr_swapfiles) {
  64                 printk("Internal error: bad swap-device\n");
  65                 return;
  66         }
  67         p = &swap_info[type];
  68         offset = SWP_OFFSET(entry);
  69         if (offset >= p->max) {
  70                 printk("rw_swap_page: weirdness\n");
  71                 return;
  72         }
  73         if (!(p->flags & SWP_USED)) {
  74                 printk("Trying to swap to unused swap-device\n");
  75                 return;
  76         }
  77         while (set_bit(offset,p->swap_lockmap))
  78                 sleep_on(&lock_queue);
  79         if (rw == READ)
  80                 kstat.pswpin++;
  81         else
  82                 kstat.pswpout++;
  83         if (p->swap_device) {
  84                 ll_rw_page(rw,p->swap_device,offset,buf);
  85         } else if (p->swap_file) {
  86                 unsigned int zones[8];
  87                 unsigned int block;
  88                 int i, j;
  89 
  90                 block = offset << (12 - p->swap_file->i_sb->s_blocksize_bits);
  91 
  92                 for (i=0, j=0; j< PAGE_SIZE ; i++, j +=p->swap_file->i_sb->s_blocksize)
  93                         if (!(zones[i] = bmap(p->swap_file,block++))) {
  94                                 printk("rw_swap_page: bad swap file\n");
  95                                 return;
  96                         }
  97                 ll_rw_swap_file(rw,p->swap_file->i_dev, zones, i,buf);
  98         } else
  99                 printk("re_swap_page: no swap file or device\n");
 100         if (offset && !clear_bit(offset,p->swap_lockmap))
 101                 printk("rw_swap_page: lock already cleared\n");
 102         wake_up(&lock_queue);
 103 }
 104 
 105 unsigned int get_swap_page(void)
     /* [previous][next][first][last][top][bottom][index][help] */
 106 {
 107         struct swap_info_struct * p;
 108         unsigned int offset, type;
 109 
 110         p = swap_info;
 111         for (type = 0 ; type < nr_swapfiles ; type++,p++) {
 112                 if ((p->flags & SWP_WRITEOK) != SWP_WRITEOK)
 113                         continue;
 114                 for (offset = p->lowest_bit; offset <= p->highest_bit ; offset++) {
 115                         if (p->swap_map[offset])
 116                                 continue;
 117                         p->swap_map[offset] = 1;
 118                         nr_swap_pages--;
 119                         if (offset == p->highest_bit)
 120                                 p->highest_bit--;
 121                         p->lowest_bit = offset;
 122                         return SWP_ENTRY(type,offset);
 123                 }
 124         }
 125         return 0;
 126 }
 127 
 128 unsigned long swap_duplicate(unsigned long entry)
     /* [previous][next][first][last][top][bottom][index][help] */
 129 {
 130         struct swap_info_struct * p;
 131         unsigned long offset, type;
 132 
 133         if (!entry)
 134                 return 0;
 135         offset = SWP_OFFSET(entry);
 136         type = SWP_TYPE(entry);
 137         if (type == SHM_SWP_TYPE)
 138                 return entry;
 139         if (type >= nr_swapfiles) {
 140                 printk("Trying to duplicate nonexistent swap-page\n");
 141                 return 0;
 142         }
 143         p = type + swap_info;
 144         if (offset >= p->max) {
 145                 printk("swap_free: weirdness\n");
 146                 return 0;
 147         }
 148         if (!p->swap_map[offset]) {
 149                 printk("swap_duplicate: trying to duplicate unused page\n");
 150                 return 0;
 151         }
 152         p->swap_map[offset]++;
 153         return entry;
 154 }
 155 
 156 void swap_free(unsigned long entry)
     /* [previous][next][first][last][top][bottom][index][help] */
 157 {
 158         struct swap_info_struct * p;
 159         unsigned long offset, type;
 160 
 161         if (!entry)
 162                 return;
 163         type = SWP_TYPE(entry);
 164         if (type == SHM_SWP_TYPE)
 165                 return;
 166         if (type >= nr_swapfiles) {
 167                 printk("Trying to free nonexistent swap-page\n");
 168                 return;
 169         }
 170         p = & swap_info[type];
 171         offset = SWP_OFFSET(entry);
 172         if (offset >= p->max) {
 173                 printk("swap_free: weirdness\n");
 174                 return;
 175         }
 176         if (!(p->flags & SWP_USED)) {
 177                 printk("Trying to free swap from unused swap-device\n");
 178                 return;
 179         }
 180         while (set_bit(offset,p->swap_lockmap))
 181                 sleep_on(&lock_queue);
 182         if (offset < p->lowest_bit)
 183                 p->lowest_bit = offset;
 184         if (offset > p->highest_bit)
 185                 p->highest_bit = offset;
 186         if (!p->swap_map[offset])
 187                 printk("swap_free: swap-space map bad (entry %08lx)\n",entry);
 188         else
 189                 if (!--p->swap_map[offset])
 190                         nr_swap_pages++;
 191         if (!clear_bit(offset,p->swap_lockmap))
 192                 printk("swap_free: lock already cleared\n");
 193         wake_up(&lock_queue);
 194 }
 195 
 196 void swap_in(unsigned long *table_ptr)
     /* [previous][next][first][last][top][bottom][index][help] */
 197 {
 198         unsigned long entry;
 199         unsigned long page;
 200 
 201         entry = *table_ptr;
 202         if (PAGE_PRESENT & entry) {
 203                 printk("trying to swap in present page\n");
 204                 return;
 205         }
 206         if (!entry) {
 207                 printk("No swap page in swap_in\n");
 208                 return;
 209         }
 210         if (SWP_TYPE(entry) == SHM_SWP_TYPE) {
 211                 shm_no_page ((unsigned long *) table_ptr);
 212                 return;
 213         }
 214         if (!(page = get_free_page(GFP_KERNEL))) {
 215                 oom(current);
 216                 page = BAD_PAGE;
 217         } else  
 218                 read_swap_page(entry, (char *) page);
 219         if (*table_ptr != entry) {
 220                 free_page(page);
 221                 return;
 222         }
 223         *table_ptr = page | (PAGE_DIRTY | PAGE_PRIVATE);
 224         swap_free(entry);
 225 }
 226 
 227 static inline int try_to_swap_out(unsigned long * table_ptr)
     /* [previous][next][first][last][top][bottom][index][help] */
 228 {
 229         int i;
 230         unsigned long page;
 231         unsigned long entry;
 232 
 233         page = *table_ptr;
 234         if (!(PAGE_PRESENT & page))
 235                 return 0;
 236         if (page >= high_memory)
 237                 return 0;
 238         if (mem_map[MAP_NR(page)] & MAP_PAGE_RESERVED)
 239                 return 0;
 240         if (PAGE_ACCESSED & page) {
 241                 *table_ptr &= ~PAGE_ACCESSED;
 242                 return 0;
 243         }
 244         for (i = 0; i < NR_LAST_FREE_PAGES; i++)
 245                 if (last_free_pages[i] == (page & PAGE_MASK))
 246                         return 0;
 247         if (PAGE_DIRTY & page) {
 248                 page &= PAGE_MASK;
 249                 if (mem_map[MAP_NR(page)] != 1)
 250                         return 0;
 251                 if (!(entry = get_swap_page()))
 252                         return 0;
 253                 *table_ptr = entry;
 254                 invalidate();
 255                 write_swap_page(entry, (char *) page);
 256                 free_page(page);
 257                 return 1;
 258         }
 259         page &= PAGE_MASK;
 260         *table_ptr = 0;
 261         invalidate();
 262         free_page(page);
 263         return 1 + mem_map[MAP_NR(page)];
 264 }
 265 
 266 /*
 267  * sys_idle() does nothing much: it just searches for likely candidates for
 268  * swapping out or forgetting about. This speeds up the search when we
 269  * actually have to swap.
 270  */
 271 asmlinkage int sys_idle(void)
     /* [previous][next][first][last][top][bottom][index][help] */
 272 {
 273         need_resched = 1;
 274         return 0;
 275 }
 276 
 277 /*
 278  * A new implementation of swap_out().  We do not swap complete processes,
 279  * but only a small number of blocks, before we continue with the next
 280  * process.  The number of blocks actually swapped is determined on the
 281  * number of page faults, that this process actually had in the last time,
 282  * so we won't swap heavily used processes all the time ...
 283  *
 284  * Note: the priority argument is a hint on much CPU to waste with the
 285  *       swap block search, not a hint, of how much blocks to swap with
 286  *       each process.
 287  *
 288  * (C) 1993 Kai Petzke, wpp@marie.physik.tu-berlin.de
 289  */
 290 #ifdef NEW_SWAP
 291 /*
 292  * These are the miminum and maximum number of pages to swap from one process,
 293  * before proceeding to the next:
 294  */
 295 #define SWAP_MIN        4
 296 #define SWAP_MAX        32
 297 
 298 /*
 299  * The actual number of pages to swap is determined as:
 300  * SWAP_RATIO / (number of recent major page faults)
 301  */
 302 #define SWAP_RATIO      128
 303 
 304 static int swap_out(unsigned int priority)
     /* [previous][next][first][last][top][bottom][index][help] */
 305 {
 306     static int swap_task;
 307     int table;
 308     int page;
 309     long pg_table;
 310     int loop;
 311     int counter = NR_TASKS * 2 >> priority;
 312     struct task_struct *p;
 313 
 314     counter = NR_TASKS * 2 >> priority;
 315     for(; counter >= 0; counter--, swap_task++) {
 316         /*
 317          * Check that swap_task is suitable for swapping.  If not, look for
 318          * the next suitable process.
 319          */
 320         loop = 0;
 321         while(1) {
 322             if(swap_task >= NR_TASKS) {
 323                 swap_task = 1;
 324                 if(loop)
 325                     /* all processes are unswappable or already swapped out */
 326                     return 0;
 327                 loop = 1;
 328             }
 329 
 330             p = task[swap_task];
 331             if(p && p->swappable && p->rss)
 332                 break;
 333 
 334             swap_task++;
 335         }
 336 
 337         /*
 338          * Determine the number of pages to swap from this process.
 339          */
 340         if(! p -> swap_cnt) {
 341             p->dec_flt = (p->dec_flt * 3) / 4 + p->maj_flt - p->old_maj_flt;
 342             p->old_maj_flt = p->maj_flt;
 343 
 344             if(p->dec_flt >= SWAP_RATIO / SWAP_MIN) {
 345                 p->dec_flt = SWAP_RATIO / SWAP_MIN;
 346                 p->swap_cnt = SWAP_MIN;
 347             } else if(p->dec_flt <= SWAP_RATIO / SWAP_MAX)
 348                 p->swap_cnt = SWAP_MAX;
 349             else
 350                 p->swap_cnt = SWAP_RATIO / p->dec_flt;
 351         }
 352 
 353         /*
 354          * Go through process' page directory.
 355          */
 356         for(table = p->swap_table; table < 1024; table++) {
 357             pg_table = ((unsigned long *) p->tss.cr3)[table];
 358             if(pg_table >= high_memory)
 359                     continue;
 360             if(mem_map[MAP_NR(pg_table)] & MAP_PAGE_RESERVED)
 361                     continue;
 362             if(!(PAGE_PRESENT & pg_table)) {
 363                     printk("swap_out: bad page-table at pg_dir[%d]: %08lx\n",
 364                             table, pg_table);
 365                     ((unsigned long *) p->tss.cr3)[table] = 0;
 366                     continue;
 367             }
 368             pg_table &= 0xfffff000;
 369 
 370             /*
 371              * Go through this page table.
 372              */
 373             for(page = p->swap_page; page < 1024; page++) {
 374                 switch(try_to_swap_out(page + (unsigned long *) pg_table)) {
 375                     case 0:
 376                         break;
 377 
 378                     case 1:
 379                         p->rss--;
 380                         /* continue with the following page the next time */
 381                         p->swap_table = table;
 382                         p->swap_page  = page + 1;
 383                         if((--p->swap_cnt) == 0)
 384                             swap_task++;
 385                         return 1;
 386 
 387                     default:
 388                         p->rss--;
 389                         break;
 390                 }
 391             }
 392 
 393             p->swap_page = 0;
 394         }
 395 
 396         /*
 397          * Finish work with this process, if we reached the end of the page
 398          * directory.  Mark restart from the beginning the next time.
 399          */
 400         p->swap_table = 0;
 401     }
 402     return 0;
 403 }
 404 
 405 #else /* old swapping procedure */
 406 
 407 /*
 408  * Go through the page tables, searching for a user page that
 409  * we can swap out.
 410  * 
 411  * We now check that the process is swappable (normally only 'init'
 412  * is un-swappable), allowing high-priority processes which cannot be
 413  * swapped out (things like user-level device drivers (Not implemented)).
 414  */
 415 static int swap_out(unsigned int priority)
     /* [previous][next][first][last][top][bottom][index][help] */
 416 {
 417         static int swap_task = 1;
 418         static int swap_table = 0;
 419         static int swap_page = 0;
 420         int counter = NR_TASKS*8;
 421         int pg_table;
 422         struct task_struct * p;
 423 
 424         counter >>= priority;
 425 check_task:
 426         if (counter-- < 0)
 427                 return 0;
 428         if (swap_task >= NR_TASKS) {
 429                 swap_task = 1;
 430                 goto check_task;
 431         }
 432         p = task[swap_task];
 433         if (!p || !p->swappable) {
 434                 swap_task++;
 435                 goto check_task;
 436         }
 437 check_dir:
 438         if (swap_table >= PTRS_PER_PAGE) {
 439                 swap_table = 0;
 440                 swap_task++;
 441                 goto check_task;
 442         }
 443         pg_table = ((unsigned long *) p->tss.cr3)[swap_table];
 444         if (pg_table >= high_memory || (mem_map[MAP_NR(pg_table)] & MAP_PAGE_RESERVED)) {
 445                 swap_table++;
 446                 goto check_dir;
 447         }
 448         if (!(PAGE_PRESENT & pg_table)) {
 449                 printk("bad page-table at pg_dir[%d]: %08x\n",
 450                         swap_table,pg_table);
 451                 ((unsigned long *) p->tss.cr3)[swap_table] = 0;
 452                 swap_table++;
 453                 goto check_dir;
 454         }
 455         pg_table &= PAGE_MASK;
 456 check_table:
 457         if (swap_page >= PTRS_PER_PAGE) {
 458                 swap_page = 0;
 459                 swap_table++;
 460                 goto check_dir;
 461         }
 462         switch (try_to_swap_out(swap_page + (unsigned long *) pg_table)) {
 463                 case 0: break;
 464                 case 1: p->rss--; return 1;
 465                 default: p->rss--;
 466         }
 467         swap_page++;
 468         goto check_table;
 469 }
 470 
 471 #endif
 472 
 473 static int try_to_free_page(int priority)
     /* [previous][next][first][last][top][bottom][index][help] */
 474 {
 475         int i=6;
 476 
 477         while (i--) {
 478                 if (priority != GFP_NOBUFFER && shrink_buffers(i))
 479                         return 1;
 480                 if (shm_swap(i))
 481                         return 1;
 482                 if (swap_out(i))
 483                         return 1;
 484         }
 485         return 0;
 486 }
 487 
 488 static inline void add_mem_queue(struct mem_list * head, struct mem_list * entry)
     /* [previous][next][first][last][top][bottom][index][help] */
 489 {
 490         entry->prev = head;
 491         entry->next = head->next;
 492         entry->next->prev = entry;
 493         head->next = entry;
 494 }
 495 
 496 static inline void remove_mem_queue(struct mem_list * head, struct mem_list * entry)
     /* [previous][next][first][last][top][bottom][index][help] */
 497 {
 498         entry->next->prev = entry->prev;
 499         entry->prev->next = entry->next;
 500 }
 501 
 502 /*
 503  * Free_page() adds the page to the free lists. This is optimized for
 504  * fast normal cases (no error jumps taken normally).
 505  *
 506  * The way to optimize jumps for gcc-2.2.2 is to:
 507  *  - select the "normal" case and put it inside the if () { XXX }
 508  *  - no else-statements if you can avoid them
 509  *
 510  * With the above two rules, you get a straight-line execution path
 511  * for the normal case, giving better asm-code.
 512  */
 513 
 514 /*
 515  * Buddy system. Hairy. You really aren't expected to understand this
 516  */
 517 static inline void free_pages_ok(unsigned long addr, unsigned long order)
     /* [previous][next][first][last][top][bottom][index][help] */
 518 {
 519         unsigned long index = addr >> (PAGE_SHIFT + 1 + order);
 520         unsigned long mask = PAGE_MASK << order;
 521 
 522         addr &= mask;
 523         nr_free_pages += 1 << order;
 524         while (order < NR_MEM_LISTS-1) {
 525                 if (!change_bit(index, free_area_map[order]))
 526                         break;
 527                 remove_mem_queue(free_area_list+order, (struct mem_list *) (addr ^ (1+~mask)));
 528                 order++;
 529                 index >>= 1;
 530                 mask <<= 1;
 531                 addr &= mask;
 532         }
 533         add_mem_queue(free_area_list+order, (struct mem_list *) addr);
 534 }
 535 
 536 void free_pages(unsigned long addr, unsigned long order)
     /* [previous][next][first][last][top][bottom][index][help] */
 537 {
 538         if (addr < high_memory) {
 539                 unsigned long flag;
 540                 unsigned short * map = mem_map + MAP_NR(addr);
 541                 if (*map) {
 542                         if (!(*map & MAP_PAGE_RESERVED)) {
 543                                 save_flags(flag);
 544                                 cli();
 545                                 if (!--*map)
 546                                         free_pages_ok(addr, order);
 547                                 restore_flags(flag);
 548                                 if(*map == 1) {
 549                                   int j;
 550                                   struct buffer_head * bh, *tmp;
 551 
 552                                   bh = buffer_pages[MAP_NR(addr)];
 553                                   if(bh)
 554                                     for(j = 0, tmp = bh; tmp && (!j || tmp != bh); 
 555                                         tmp = tmp->b_this_page, j++)
 556                                       if(tmp->b_list == BUF_SHARED && tmp->b_dev != 0xffff)
 557                                         refile_buffer(tmp);
 558                                 }
 559                         }
 560                         return;
 561                 }
 562                 printk("Trying to free free memory (%08lx): memory probabably corrupted\n",addr);
 563                 printk("PC = %08lx\n",*(((unsigned long *)&addr)-1));
 564                 return;
 565         }
 566 }
 567 
 568 /*
 569  * Some ugly macros to speed up __get_free_pages()..
 570  */
 571 #define RMQUEUE(order) \
 572 do { struct mem_list * queue = free_area_list+order; \
 573      unsigned long new_order = order; \
 574         do { struct mem_list *next = queue->next; \
 575                 if (queue != next) { \
 576                         queue->next = next->next; \
 577                         next->next->prev = queue; \
 578                         mark_used((unsigned long) next, new_order); \
 579                         nr_free_pages -= 1 << order; \
 580                         restore_flags(flags); \
 581                         EXPAND(next, order, new_order); \
 582                         return (unsigned long) next; \
 583                 } new_order++; queue++; \
 584         } while (new_order < NR_MEM_LISTS); \
 585 } while (0)
 586 
 587 static inline int mark_used(unsigned long addr, unsigned long order)
     /* [previous][next][first][last][top][bottom][index][help] */
 588 {
 589         return change_bit(addr >> (PAGE_SHIFT+1+order), free_area_map[order]);
 590 }
 591 
 592 #define EXPAND(addr,low,high) \
 593 do { unsigned long size = PAGE_SIZE << high; \
 594         while (high > low) { \
 595                 high--; size >>= 1; cli(); \
 596                 add_mem_queue(free_area_list+high, addr); \
 597                 mark_used((unsigned long) addr, high); \
 598                 restore_flags(flags); \
 599                 addr = (struct mem_list *) (size + (unsigned long) addr); \
 600         } mem_map[MAP_NR((unsigned long) addr)] = 1; \
 601 } while (0)
 602 
 603 unsigned long __get_free_pages(int priority, unsigned long order)
     /* [previous][next][first][last][top][bottom][index][help] */
 604 {
 605         unsigned long flags;
 606 
 607         if (intr_count && priority != GFP_ATOMIC) {
 608                 static int count = 0;
 609                 if (++count < 5) {
 610                         printk("gfp called nonatomically from interrupt %08lx\n",
 611                                 ((unsigned long *)&priority)[-1]);
 612                         priority = GFP_ATOMIC;
 613                 }
 614         }
 615         save_flags(flags);
 616 repeat:
 617         cli();
 618         if ((priority==GFP_ATOMIC) || nr_free_pages > MAX_SECONDARY_PAGES) {
 619                 RMQUEUE(order);
 620                 restore_flags(flags);
 621                 return 0;
 622         }
 623         restore_flags(flags);
 624         if (priority != GFP_BUFFER && try_to_free_page(priority))
 625                 goto repeat;
 626         return 0;
 627 }
 628 
 629 /*
 630  * Show free area list (used inside shift_scroll-lock stuff)
 631  * We also calculate the percentage fragmentation. We do this by counting the
 632  * memory on each free list with the exception of the first item on the list.
 633  */
 634 void show_free_areas(void)
     /* [previous][next][first][last][top][bottom][index][help] */
 635 {
 636         unsigned long order, flags;
 637         unsigned long total = 0;
 638 
 639         printk("Free pages:      %6dkB\n ( ",nr_free_pages<<(PAGE_SHIFT-10));
 640         save_flags(flags);
 641         cli();
 642         for (order=0 ; order < NR_MEM_LISTS; order++) {
 643                 struct mem_list * tmp;
 644                 unsigned long nr = 0;
 645                 for (tmp = free_area_list[order].next ; tmp != free_area_list + order ; tmp = tmp->next) {
 646                         nr ++;
 647                 }
 648                 total += nr * (4 << order);
 649                 printk("%lu*%ukB ", nr, 4 << order);
 650         }
 651         restore_flags(flags);
 652         printk("= %lukB)\n", total);
 653 }
 654 
 655 /*
 656  * Trying to stop swapping from a file is fraught with races, so
 657  * we repeat quite a bit here when we have to pause. swapoff()
 658  * isn't exactly timing-critical, so who cares?
 659  */
 660 static int try_to_unuse(unsigned int type)
     /* [previous][next][first][last][top][bottom][index][help] */
 661 {
 662         int nr, pgt, pg;
 663         unsigned long page, *ppage;
 664         unsigned long tmp = 0;
 665         struct task_struct *p;
 666 
 667         nr = 0;
 668 /*
 669  * When we have to sleep, we restart the whole algorithm from the same
 670  * task we stopped in. That at least rids us of all races.
 671  */
 672 repeat:
 673         for (; nr < NR_TASKS ; nr++) {
 674                 p = task[nr];
 675                 if (!p)
 676                         continue;
 677                 for (pgt = 0 ; pgt < PTRS_PER_PAGE ; pgt++) {
 678                         ppage = pgt + ((unsigned long *) p->tss.cr3);
 679                         page = *ppage;
 680                         if (!page)
 681                                 continue;
 682                         if (!(page & PAGE_PRESENT) || (page >= high_memory))
 683                                 continue;
 684                         if (mem_map[MAP_NR(page)] & MAP_PAGE_RESERVED)
 685                                 continue;
 686                         ppage = (unsigned long *) (page & PAGE_MASK);   
 687                         for (pg = 0 ; pg < PTRS_PER_PAGE ; pg++,ppage++) {
 688                                 page = *ppage;
 689                                 if (!page)
 690                                         continue;
 691                                 if (page & PAGE_PRESENT)
 692                                         continue;
 693                                 if (SWP_TYPE(page) != type)
 694                                         continue;
 695                                 if (!tmp) {
 696                                         if (!(tmp = __get_free_page(GFP_KERNEL)))
 697                                                 return -ENOMEM;
 698                                         goto repeat;
 699                                 }
 700                                 read_swap_page(page, (char *) tmp);
 701                                 if (*ppage == page) {
 702                                         *ppage = tmp | (PAGE_DIRTY | PAGE_PRIVATE);
 703                                         ++p->rss;
 704                                         swap_free(page);
 705                                         tmp = 0;
 706                                 }
 707                                 goto repeat;
 708                         }
 709                 }
 710         }
 711         free_page(tmp);
 712         return 0;
 713 }
 714 
 715 asmlinkage int sys_swapoff(const char * specialfile)
     /* [previous][next][first][last][top][bottom][index][help] */
 716 {
 717         struct swap_info_struct * p;
 718         struct inode * inode;
 719         unsigned int type;
 720         int i;
 721 
 722         if (!suser())
 723                 return -EPERM;
 724         i = namei(specialfile,&inode);
 725         if (i)
 726                 return i;
 727         p = swap_info;
 728         for (type = 0 ; type < nr_swapfiles ; type++,p++) {
 729                 if ((p->flags & SWP_WRITEOK) != SWP_WRITEOK)
 730                         continue;
 731                 if (p->swap_file) {
 732                         if (p->swap_file == inode)
 733                                 break;
 734                 } else {
 735                         if (!S_ISBLK(inode->i_mode))
 736                                 continue;
 737                         if (p->swap_device == inode->i_rdev)
 738                                 break;
 739                 }
 740         }
 741         iput(inode);
 742         if (type >= nr_swapfiles)
 743                 return -EINVAL;
 744         p->flags = SWP_USED;
 745         i = try_to_unuse(type);
 746         if (i) {
 747                 p->flags = SWP_WRITEOK;
 748                 return i;
 749         }
 750         nr_swap_pages -= p->pages;
 751         iput(p->swap_file);
 752         p->swap_file = NULL;
 753         p->swap_device = 0;
 754         vfree(p->swap_map);
 755         p->swap_map = NULL;
 756         free_page((long) p->swap_lockmap);
 757         p->swap_lockmap = NULL;
 758         p->flags = 0;
 759         return 0;
 760 }
 761 
 762 /*
 763  * Written 01/25/92 by Simmule Turner, heavily changed by Linus.
 764  *
 765  * The swapon system call
 766  */
 767 asmlinkage int sys_swapon(const char * specialfile)
     /* [previous][next][first][last][top][bottom][index][help] */
 768 {
 769         struct swap_info_struct * p;
 770         struct inode * swap_inode;
 771         unsigned int type;
 772         int i,j;
 773         int error;
 774 
 775         if (!suser())
 776                 return -EPERM;
 777         p = swap_info;
 778         for (type = 0 ; type < nr_swapfiles ; type++,p++)
 779                 if (!(p->flags & SWP_USED))
 780                         break;
 781         if (type >= MAX_SWAPFILES)
 782                 return -EPERM;
 783         if (type >= nr_swapfiles)
 784                 nr_swapfiles = type+1;
 785         p->flags = SWP_USED;
 786         p->swap_file = NULL;
 787         p->swap_device = 0;
 788         p->swap_map = NULL;
 789         p->swap_lockmap = NULL;
 790         p->lowest_bit = 0;
 791         p->highest_bit = 0;
 792         p->max = 1;
 793         error = namei(specialfile,&swap_inode);
 794         if (error)
 795                 goto bad_swap;
 796         error = -EBUSY;
 797         if (swap_inode->i_count != 1)
 798                 goto bad_swap;
 799         error = -EINVAL;
 800         if (S_ISBLK(swap_inode->i_mode)) {
 801                 p->swap_device = swap_inode->i_rdev;
 802                 iput(swap_inode);
 803                 error = -ENODEV;
 804                 if (!p->swap_device)
 805                         goto bad_swap;
 806                 error = -EBUSY;
 807                 for (i = 0 ; i < nr_swapfiles ; i++) {
 808                         if (i == type)
 809                                 continue;
 810                         if (p->swap_device == swap_info[i].swap_device)
 811                                 goto bad_swap;
 812                 }
 813         } else if (S_ISREG(swap_inode->i_mode))
 814                 p->swap_file = swap_inode;
 815         else
 816                 goto bad_swap;
 817         p->swap_lockmap = (unsigned char *) get_free_page(GFP_USER);
 818         if (!p->swap_lockmap) {
 819                 printk("Unable to start swapping: out of memory :-)\n");
 820                 error = -ENOMEM;
 821                 goto bad_swap;
 822         }
 823         read_swap_page(SWP_ENTRY(type,0), (char *) p->swap_lockmap);
 824         if (memcmp("SWAP-SPACE",p->swap_lockmap+4086,10)) {
 825                 printk("Unable to find swap-space signature\n");
 826                 error = -EINVAL;
 827                 goto bad_swap;
 828         }
 829         memset(p->swap_lockmap+PAGE_SIZE-10,0,10);
 830         j = 0;
 831         p->lowest_bit = 0;
 832         p->highest_bit = 0;
 833         for (i = 1 ; i < 8*PAGE_SIZE ; i++) {
 834                 if (test_bit(i,p->swap_lockmap)) {
 835                         if (!p->lowest_bit)
 836                                 p->lowest_bit = i;
 837                         p->highest_bit = i;
 838                         p->max = i+1;
 839                         j++;
 840                 }
 841         }
 842         if (!j) {
 843                 printk("Empty swap-file\n");
 844                 error = -EINVAL;
 845                 goto bad_swap;
 846         }
 847         p->swap_map = (unsigned char *) vmalloc(p->max);
 848         if (!p->swap_map) {
 849                 error = -ENOMEM;
 850                 goto bad_swap;
 851         }
 852         for (i = 1 ; i < p->max ; i++) {
 853                 if (test_bit(i,p->swap_lockmap))
 854                         p->swap_map[i] = 0;
 855                 else
 856                         p->swap_map[i] = 0x80;
 857         }
 858         p->swap_map[0] = 0x80;
 859         memset(p->swap_lockmap,0,PAGE_SIZE);
 860         p->flags = SWP_WRITEOK;
 861         p->pages = j;
 862         nr_swap_pages += j;
 863         printk("Adding Swap: %dk swap-space\n",j<<2);
 864         return 0;
 865 bad_swap:
 866         free_page((long) p->swap_lockmap);
 867         vfree(p->swap_map);
 868         iput(p->swap_file);
 869         p->swap_device = 0;
 870         p->swap_file = NULL;
 871         p->swap_map = NULL;
 872         p->swap_lockmap = NULL;
 873         p->flags = 0;
 874         return error;
 875 }
 876 
 877 void si_swapinfo(struct sysinfo *val)
     /* [previous][next][first][last][top][bottom][index][help] */
 878 {
 879         unsigned int i, j;
 880 
 881         val->freeswap = val->totalswap = 0;
 882         for (i = 0; i < nr_swapfiles; i++) {
 883                 if (!(swap_info[i].flags & SWP_USED))
 884                         continue;
 885                 for (j = 0; j < swap_info[i].max; ++j)
 886                         switch (swap_info[i].swap_map[j]) {
 887                                 case 128:
 888                                         continue;
 889                                 case 0:
 890                                         ++val->freeswap;
 891                                 default:
 892                                         ++val->totalswap;
 893                         }
 894         }
 895         val->freeswap <<= PAGE_SHIFT;
 896         val->totalswap <<= PAGE_SHIFT;
 897         return;
 898 }

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