patch-2.6.6-vs1.9.0
[linux-2.6.git] / mm / swapfile.c
1 /*
2  *  linux/mm/swapfile.c
3  *
4  *  Copyright (C) 1991, 1992, 1993, 1994  Linus Torvalds
5  *  Swap reorganised 29.12.95, Stephen Tweedie
6  */
7
8 #include <linux/config.h>
9 #include <linux/mm.h>
10 #include <linux/mman.h>
11 #include <linux/slab.h>
12 #include <linux/kernel_stat.h>
13 #include <linux/swap.h>
14 #include <linux/vmalloc.h>
15 #include <linux/pagemap.h>
16 #include <linux/namei.h>
17 #include <linux/shm.h>
18 #include <linux/blkdev.h>
19 #include <linux/writeback.h>
20 #include <linux/proc_fs.h>
21 #include <linux/seq_file.h>
22 #include <linux/init.h>
23 #include <linux/module.h>
24 #include <linux/rmap.h>
25 #include <linux/security.h>
26 #include <linux/backing-dev.h>
27
28 #include <asm/pgtable.h>
29 #include <asm/tlbflush.h>
30 #include <linux/swapops.h>
31
32 spinlock_t swaplock = SPIN_LOCK_UNLOCKED;
33 unsigned int nr_swapfiles;
34 int total_swap_pages;
35 static int swap_overflow;
36
37 EXPORT_SYMBOL(total_swap_pages);
38
39 static const char Bad_file[] = "Bad swap file entry ";
40 static const char Unused_file[] = "Unused swap file entry ";
41 static const char Bad_offset[] = "Bad swap offset entry ";
42 static const char Unused_offset[] = "Unused swap offset entry ";
43
44 struct swap_list_t swap_list = {-1, -1};
45
46 struct swap_info_struct swap_info[MAX_SWAPFILES];
47
48 static DECLARE_MUTEX(swapon_sem);
49
50 /*
51  * Array of backing blockdevs, for swap_unplug_fn.  We need this because the
52  * bdev->unplug_fn can sleep and we cannot hold swap_list_lock while calling
53  * the unplug_fn.  And swap_list_lock cannot be turned into a semaphore.
54  */
55 static DECLARE_MUTEX(swap_bdevs_sem);
56 static struct block_device *swap_bdevs[MAX_SWAPFILES];
57
58 #define SWAPFILE_CLUSTER 256
59
60 /*
61  * Caller holds swap_bdevs_sem
62  */
63 static void install_swap_bdev(struct block_device *bdev)
64 {
65         int i;
66
67         for (i = 0; i < MAX_SWAPFILES; i++) {
68                 if (swap_bdevs[i] == NULL) {
69                         swap_bdevs[i] = bdev;
70                         return;
71                 }
72         }
73         BUG();
74 }
75
76 static void remove_swap_bdev(struct block_device *bdev)
77 {
78         int i;
79
80         for (i = 0; i < MAX_SWAPFILES; i++) {
81                 if (swap_bdevs[i] == bdev) {
82                         memcpy(&swap_bdevs[i], &swap_bdevs[i + 1],
83                                 (MAX_SWAPFILES - i - 1) * sizeof(*swap_bdevs));
84                         swap_bdevs[MAX_SWAPFILES - 1] = NULL;
85                         return;
86                 }
87         }
88         BUG();
89 }
90
91 /*
92  * Unlike a standard unplug_io_fn, swap_unplug_io_fn is never called
93  * through swap's backing_dev_info (which is only used by shrink_list),
94  * but directly from sync_page when PageSwapCache: and takes the page
95  * as argument, so that it can find the right device from swp_entry_t.
96  */
97 void swap_unplug_io_fn(struct page *page)
98 {
99         swp_entry_t entry;
100
101         down(&swap_bdevs_sem);
102         entry.val = page->private;
103         if (PageSwapCache(page)) {
104                 struct block_device *bdev = swap_bdevs[swp_type(entry)];
105                 struct backing_dev_info *bdi;
106
107                 if (bdev) {
108                         bdi = bdev->bd_inode->i_mapping->backing_dev_info;
109                         (*bdi->unplug_io_fn)(bdi);
110                 }
111         }
112         up(&swap_bdevs_sem);
113 }
114
115 static inline int scan_swap_map(struct swap_info_struct *si)
116 {
117         unsigned long offset;
118         /* 
119          * We try to cluster swap pages by allocating them
120          * sequentially in swap.  Once we've allocated
121          * SWAPFILE_CLUSTER pages this way, however, we resort to
122          * first-free allocation, starting a new cluster.  This
123          * prevents us from scattering swap pages all over the entire
124          * swap partition, so that we reduce overall disk seek times
125          * between swap pages.  -- sct */
126         if (si->cluster_nr) {
127                 while (si->cluster_next <= si->highest_bit) {
128                         offset = si->cluster_next++;
129                         if (si->swap_map[offset])
130                                 continue;
131                         si->cluster_nr--;
132                         goto got_page;
133                 }
134         }
135         si->cluster_nr = SWAPFILE_CLUSTER;
136
137         /* try to find an empty (even not aligned) cluster. */
138         offset = si->lowest_bit;
139  check_next_cluster:
140         if (offset+SWAPFILE_CLUSTER-1 <= si->highest_bit)
141         {
142                 int nr;
143                 for (nr = offset; nr < offset+SWAPFILE_CLUSTER; nr++)
144                         if (si->swap_map[nr])
145                         {
146                                 offset = nr+1;
147                                 goto check_next_cluster;
148                         }
149                 /* We found a completly empty cluster, so start
150                  * using it.
151                  */
152                 goto got_page;
153         }
154         /* No luck, so now go finegrined as usual. -Andrea */
155         for (offset = si->lowest_bit; offset <= si->highest_bit ; offset++) {
156                 if (si->swap_map[offset])
157                         continue;
158                 si->lowest_bit = offset+1;
159         got_page:
160                 if (offset == si->lowest_bit)
161                         si->lowest_bit++;
162                 if (offset == si->highest_bit)
163                         si->highest_bit--;
164                 if (si->lowest_bit > si->highest_bit) {
165                         si->lowest_bit = si->max;
166                         si->highest_bit = 0;
167                 }
168                 si->swap_map[offset] = 1;
169                 si->inuse_pages++;
170                 nr_swap_pages--;
171                 si->cluster_next = offset+1;
172                 return offset;
173         }
174         si->lowest_bit = si->max;
175         si->highest_bit = 0;
176         return 0;
177 }
178
179 swp_entry_t get_swap_page(void)
180 {
181         struct swap_info_struct * p;
182         unsigned long offset;
183         swp_entry_t entry;
184         int type, wrapped = 0;
185
186         entry.val = 0;  /* Out of memory */
187         swap_list_lock();
188         type = swap_list.next;
189         if (type < 0)
190                 goto out;
191         if (nr_swap_pages <= 0)
192                 goto out;
193
194         while (1) {
195                 p = &swap_info[type];
196                 if ((p->flags & SWP_ACTIVE) == SWP_ACTIVE) {
197                         swap_device_lock(p);
198                         offset = scan_swap_map(p);
199                         swap_device_unlock(p);
200                         if (offset) {
201                                 entry = swp_entry(type,offset);
202                                 type = swap_info[type].next;
203                                 if (type < 0 ||
204                                         p->prio != swap_info[type].prio) {
205                                                 swap_list.next = swap_list.head;
206                                 } else {
207                                         swap_list.next = type;
208                                 }
209                                 goto out;
210                         }
211                 }
212                 type = p->next;
213                 if (!wrapped) {
214                         if (type < 0 || p->prio != swap_info[type].prio) {
215                                 type = swap_list.head;
216                                 wrapped = 1;
217                         }
218                 } else
219                         if (type < 0)
220                                 goto out;       /* out of swap space */
221         }
222 out:
223         swap_list_unlock();
224         return entry;
225 }
226
227 static struct swap_info_struct * swap_info_get(swp_entry_t entry)
228 {
229         struct swap_info_struct * p;
230         unsigned long offset, type;
231
232         if (!entry.val)
233                 goto out;
234         type = swp_type(entry);
235         if (type >= nr_swapfiles)
236                 goto bad_nofile;
237         p = & swap_info[type];
238         if (!(p->flags & SWP_USED))
239                 goto bad_device;
240         offset = swp_offset(entry);
241         if (offset >= p->max)
242                 goto bad_offset;
243         if (!p->swap_map[offset])
244                 goto bad_free;
245         swap_list_lock();
246         if (p->prio > swap_info[swap_list.next].prio)
247                 swap_list.next = type;
248         swap_device_lock(p);
249         return p;
250
251 bad_free:
252         printk(KERN_ERR "swap_free: %s%08lx\n", Unused_offset, entry.val);
253         goto out;
254 bad_offset:
255         printk(KERN_ERR "swap_free: %s%08lx\n", Bad_offset, entry.val);
256         goto out;
257 bad_device:
258         printk(KERN_ERR "swap_free: %s%08lx\n", Unused_file, entry.val);
259         goto out;
260 bad_nofile:
261         printk(KERN_ERR "swap_free: %s%08lx\n", Bad_file, entry.val);
262 out:
263         return NULL;
264 }       
265
266 static void swap_info_put(struct swap_info_struct * p)
267 {
268         swap_device_unlock(p);
269         swap_list_unlock();
270 }
271
272 static int swap_entry_free(struct swap_info_struct *p, unsigned long offset)
273 {
274         int count = p->swap_map[offset];
275
276         if (count < SWAP_MAP_MAX) {
277                 count--;
278                 p->swap_map[offset] = count;
279                 if (!count) {
280                         if (offset < p->lowest_bit)
281                                 p->lowest_bit = offset;
282                         if (offset > p->highest_bit)
283                                 p->highest_bit = offset;
284                         nr_swap_pages++;
285                         p->inuse_pages--;
286                 }
287         }
288         return count;
289 }
290
291 /*
292  * Caller has made sure that the swapdevice corresponding to entry
293  * is still around or has not been recycled.
294  */
295 void swap_free(swp_entry_t entry)
296 {
297         struct swap_info_struct * p;
298
299         p = swap_info_get(entry);
300         if (p) {
301                 swap_entry_free(p, swp_offset(entry));
302                 swap_info_put(p);
303         }
304 }
305
306 /*
307  * Check if we're the only user of a swap page,
308  * when the page is locked.
309  */
310 static int exclusive_swap_page(struct page *page)
311 {
312         int retval = 0;
313         struct swap_info_struct * p;
314         swp_entry_t entry;
315
316         entry.val = page->private;
317         p = swap_info_get(entry);
318         if (p) {
319                 /* Is the only swap cache user the cache itself? */
320                 if (p->swap_map[swp_offset(entry)] == 1) {
321                         /* Recheck the page count with the swapcache lock held.. */
322                         spin_lock(&swapper_space.tree_lock);
323                         if (page_count(page) == 2)
324                                 retval = 1;
325                         spin_unlock(&swapper_space.tree_lock);
326                 }
327                 swap_info_put(p);
328         }
329         return retval;
330 }
331
332 /*
333  * We can use this swap cache entry directly
334  * if there are no other references to it.
335  *
336  * Here "exclusive_swap_page()" does the real
337  * work, but we opportunistically check whether
338  * we need to get all the locks first..
339  */
340 int can_share_swap_page(struct page *page)
341 {
342         int retval = 0;
343
344         if (!PageLocked(page))
345                 BUG();
346         switch (page_count(page)) {
347         case 3:
348                 if (!PagePrivate(page))
349                         break;
350                 /* Fallthrough */
351         case 2:
352                 if (!PageSwapCache(page))
353                         break;
354                 retval = exclusive_swap_page(page);
355                 break;
356         case 1:
357                 if (PageReserved(page))
358                         break;
359                 retval = 1;
360         }
361         return retval;
362 }
363
364 /*
365  * Work out if there are any other processes sharing this
366  * swap cache page. Free it if you can. Return success.
367  */
368 int remove_exclusive_swap_page(struct page *page)
369 {
370         int retval;
371         struct swap_info_struct * p;
372         swp_entry_t entry;
373
374         BUG_ON(PagePrivate(page));
375         BUG_ON(!PageLocked(page));
376
377         if (!PageSwapCache(page))
378                 return 0;
379         if (PageWriteback(page))
380                 return 0;
381         if (page_count(page) != 2) /* 2: us + cache */
382                 return 0;
383
384         entry.val = page->private;
385         p = swap_info_get(entry);
386         if (!p)
387                 return 0;
388
389         /* Is the only swap cache user the cache itself? */
390         retval = 0;
391         if (p->swap_map[swp_offset(entry)] == 1) {
392                 /* Recheck the page count with the swapcache lock held.. */
393                 spin_lock(&swapper_space.tree_lock);
394                 if ((page_count(page) == 2) && !PageWriteback(page)) {
395                         __delete_from_swap_cache(page);
396                         SetPageDirty(page);
397                         retval = 1;
398                 }
399                 spin_unlock(&swapper_space.tree_lock);
400         }
401         swap_info_put(p);
402
403         if (retval) {
404                 swap_free(entry);
405                 page_cache_release(page);
406         }
407
408         return retval;
409 }
410
411 /*
412  * Free the swap entry like above, but also try to
413  * free the page cache entry if it is the last user.
414  */
415 void free_swap_and_cache(swp_entry_t entry)
416 {
417         struct swap_info_struct * p;
418         struct page *page = NULL;
419
420         p = swap_info_get(entry);
421         if (p) {
422                 if (swap_entry_free(p, swp_offset(entry)) == 1) {
423                         spin_lock(&swapper_space.tree_lock);
424                         page = radix_tree_lookup(&swapper_space.page_tree,
425                                 entry.val);
426                         if (page && TestSetPageLocked(page))
427                                 page = NULL;
428                         spin_unlock(&swapper_space.tree_lock);
429                 }
430                 swap_info_put(p);
431         }
432         if (page) {
433                 int one_user;
434
435                 BUG_ON(PagePrivate(page));
436                 page_cache_get(page);
437                 one_user = (page_count(page) == 2);
438                 /* Only cache user (+us), or swap space full? Free it! */
439                 if (!PageWriteback(page) && (one_user || vm_swap_full())) {
440                         delete_from_swap_cache(page);
441                         SetPageDirty(page);
442                 }
443                 unlock_page(page);
444                 page_cache_release(page);
445         }
446 }
447
448 /*
449  * The swap entry has been read in advance, and we return 1 to indicate
450  * that the page has been used or is no longer needed.
451  *
452  * Always set the resulting pte to be nowrite (the same as COW pages
453  * after one process has exited).  We don't know just how many PTEs will
454  * share this swap entry, so be cautious and let do_wp_page work out
455  * what to do if a write is requested later.
456  */
457 /* vma->vm_mm->page_table_lock is held */
458 static void
459 unuse_pte(struct vm_area_struct *vma, unsigned long address, pte_t *dir,
460         swp_entry_t entry, struct page *page, struct pte_chain **pte_chainp)
461 {
462         // vma->vm_mm->rss++;
463         vx_rsspages_inc(vma->vm_mm);
464         get_page(page);
465         set_pte(dir, pte_mkold(mk_pte(page, vma->vm_page_prot)));
466         *pte_chainp = page_add_rmap(page, dir, *pte_chainp);
467         swap_free(entry);
468 }
469
470 /* vma->vm_mm->page_table_lock is held */
471 static int unuse_pmd(struct vm_area_struct * vma, pmd_t *dir,
472         unsigned long address, unsigned long size, unsigned long offset,
473         swp_entry_t entry, struct page *page, struct pte_chain **pte_chainp)
474 {
475         pte_t * pte;
476         unsigned long end;
477         pte_t swp_pte = swp_entry_to_pte(entry);
478
479         if (pmd_none(*dir))
480                 return 0;
481         if (pmd_bad(*dir)) {
482                 pmd_ERROR(*dir);
483                 pmd_clear(dir);
484                 return 0;
485         }
486         pte = pte_offset_map(dir, address);
487         offset += address & PMD_MASK;
488         address &= ~PMD_MASK;
489         end = address + size;
490         if (end > PMD_SIZE)
491                 end = PMD_SIZE;
492         do {
493                 /*
494                  * swapoff spends a _lot_ of time in this loop!
495                  * Test inline before going to call unuse_pte.
496                  */
497                 if (unlikely(pte_same(*pte, swp_pte))) {
498                         unuse_pte(vma, offset + address, pte,
499                                         entry, page, pte_chainp);
500                         pte_unmap(pte);
501                         return 1;
502                 }
503                 address += PAGE_SIZE;
504                 pte++;
505         } while (address && (address < end));
506         pte_unmap(pte - 1);
507         return 0;
508 }
509
510 /* vma->vm_mm->page_table_lock is held */
511 static int unuse_pgd(struct vm_area_struct * vma, pgd_t *dir,
512         unsigned long address, unsigned long size,
513         swp_entry_t entry, struct page *page, struct pte_chain **pte_chainp)
514 {
515         pmd_t * pmd;
516         unsigned long offset, end;
517
518         if (pgd_none(*dir))
519                 return 0;
520         if (pgd_bad(*dir)) {
521                 pgd_ERROR(*dir);
522                 pgd_clear(dir);
523                 return 0;
524         }
525         pmd = pmd_offset(dir, address);
526         offset = address & PGDIR_MASK;
527         address &= ~PGDIR_MASK;
528         end = address + size;
529         if (end > PGDIR_SIZE)
530                 end = PGDIR_SIZE;
531         if (address >= end)
532                 BUG();
533         do {
534                 if (unuse_pmd(vma, pmd, address, end - address,
535                                 offset, entry, page, pte_chainp))
536                         return 1;
537                 address = (address + PMD_SIZE) & PMD_MASK;
538                 pmd++;
539         } while (address && (address < end));
540         return 0;
541 }
542
543 /* vma->vm_mm->page_table_lock is held */
544 static int unuse_vma(struct vm_area_struct * vma, pgd_t *pgdir,
545         swp_entry_t entry, struct page *page, struct pte_chain **pte_chainp)
546 {
547         unsigned long start = vma->vm_start, end = vma->vm_end;
548
549         if (start >= end)
550                 BUG();
551         do {
552                 if (unuse_pgd(vma, pgdir, start, end - start,
553                                 entry, page, pte_chainp))
554                         return 1;
555                 start = (start + PGDIR_SIZE) & PGDIR_MASK;
556                 pgdir++;
557         } while (start && (start < end));
558         return 0;
559 }
560
561 static int unuse_process(struct mm_struct * mm,
562                         swp_entry_t entry, struct page* page)
563 {
564         struct vm_area_struct* vma;
565         struct pte_chain *pte_chain;
566
567         pte_chain = pte_chain_alloc(GFP_KERNEL);
568         if (!pte_chain)
569                 return -ENOMEM;
570
571         /*
572          * Go through process' page directory.
573          */
574         spin_lock(&mm->page_table_lock);
575         for (vma = mm->mmap; vma; vma = vma->vm_next) {
576                 pgd_t * pgd = pgd_offset(mm, vma->vm_start);
577                 if (unuse_vma(vma, pgd, entry, page, &pte_chain))
578                         break;
579         }
580         spin_unlock(&mm->page_table_lock);
581         pte_chain_free(pte_chain);
582         return 0;
583 }
584
585 /*
586  * Scan swap_map from current position to next entry still in use.
587  * Recycle to start on reaching the end, returning 0 when empty.
588  */
589 static int find_next_to_unuse(struct swap_info_struct *si, int prev)
590 {
591         int max = si->max;
592         int i = prev;
593         int count;
594
595         /*
596          * No need for swap_device_lock(si) here: we're just looking
597          * for whether an entry is in use, not modifying it; false
598          * hits are okay, and sys_swapoff() has already prevented new
599          * allocations from this area (while holding swap_list_lock()).
600          */
601         for (;;) {
602                 if (++i >= max) {
603                         if (!prev) {
604                                 i = 0;
605                                 break;
606                         }
607                         /*
608                          * No entries in use at top of swap_map,
609                          * loop back to start and recheck there.
610                          */
611                         max = prev + 1;
612                         prev = 0;
613                         i = 1;
614                 }
615                 count = si->swap_map[i];
616                 if (count && count != SWAP_MAP_BAD)
617                         break;
618         }
619         return i;
620 }
621
622 /*
623  * We completely avoid races by reading each swap page in advance,
624  * and then search for the process using it.  All the necessary
625  * page table adjustments can then be made atomically.
626  */
627 static int try_to_unuse(unsigned int type)
628 {
629         struct swap_info_struct * si = &swap_info[type];
630         struct mm_struct *start_mm;
631         unsigned short *swap_map;
632         unsigned short swcount;
633         struct page *page;
634         swp_entry_t entry;
635         int i = 0;
636         int retval = 0;
637         int reset_overflow = 0;
638         int shmem;
639
640         /*
641          * When searching mms for an entry, a good strategy is to
642          * start at the first mm we freed the previous entry from
643          * (though actually we don't notice whether we or coincidence
644          * freed the entry).  Initialize this start_mm with a hold.
645          *
646          * A simpler strategy would be to start at the last mm we
647          * freed the previous entry from; but that would take less
648          * advantage of mmlist ordering (now preserved by swap_out()),
649          * which clusters forked address spaces together, most recent
650          * child immediately after parent.  If we race with dup_mmap(),
651          * we very much want to resolve parent before child, otherwise
652          * we may miss some entries: using last mm would invert that.
653          */
654         start_mm = &init_mm;
655         atomic_inc(&init_mm.mm_users);
656
657         /*
658          * Keep on scanning until all entries have gone.  Usually,
659          * one pass through swap_map is enough, but not necessarily:
660          * mmput() removes mm from mmlist before exit_mmap() and its
661          * zap_page_range().  That's not too bad, those entries are
662          * on their way out, and handled faster there than here.
663          * do_munmap() behaves similarly, taking the range out of mm's
664          * vma list before zap_page_range().  But unfortunately, when
665          * unmapping a part of a vma, it takes the whole out first,
666          * then reinserts what's left after (might even reschedule if
667          * open() method called) - so swap entries may be invisible
668          * to swapoff for a while, then reappear - but that is rare.
669          */
670         while ((i = find_next_to_unuse(si, i))) {
671                 if (signal_pending(current)) {
672                         retval = -EINTR;
673                         break;
674                 }
675
676                 /* 
677                  * Get a page for the entry, using the existing swap
678                  * cache page if there is one.  Otherwise, get a clean
679                  * page and read the swap into it. 
680                  */
681                 swap_map = &si->swap_map[i];
682                 entry = swp_entry(type, i);
683                 page = read_swap_cache_async(entry);
684                 if (!page) {
685                         /*
686                          * Either swap_duplicate() failed because entry
687                          * has been freed independently, and will not be
688                          * reused since sys_swapoff() already disabled
689                          * allocation from here, or alloc_page() failed.
690                          */
691                         if (!*swap_map)
692                                 continue;
693                         retval = -ENOMEM;
694                         break;
695                 }
696
697                 /*
698                  * Don't hold on to start_mm if it looks like exiting.
699                  */
700                 if (atomic_read(&start_mm->mm_users) == 1) {
701                         mmput(start_mm);
702                         start_mm = &init_mm;
703                         atomic_inc(&init_mm.mm_users);
704                 }
705
706                 /*
707                  * Wait for and lock page.  When do_swap_page races with
708                  * try_to_unuse, do_swap_page can handle the fault much
709                  * faster than try_to_unuse can locate the entry.  This
710                  * apparently redundant "wait_on_page_locked" lets try_to_unuse
711                  * defer to do_swap_page in such a case - in some tests,
712                  * do_swap_page and try_to_unuse repeatedly compete.
713                  */
714                 wait_on_page_locked(page);
715                 wait_on_page_writeback(page);
716                 lock_page(page);
717                 wait_on_page_writeback(page);
718
719                 /*
720                  * Remove all references to entry, without blocking.
721                  * Whenever we reach init_mm, there's no address space
722                  * to search, but use it as a reminder to search shmem.
723                  */
724                 shmem = 0;
725                 swcount = *swap_map;
726                 if (swcount > 1) {
727                         if (start_mm == &init_mm)
728                                 shmem = shmem_unuse(entry, page);
729                         else
730                                 retval = unuse_process(start_mm, entry, page);
731                 }
732                 if (*swap_map > 1) {
733                         int set_start_mm = (*swap_map >= swcount);
734                         struct list_head *p = &start_mm->mmlist;
735                         struct mm_struct *new_start_mm = start_mm;
736                         struct mm_struct *prev_mm = start_mm;
737                         struct mm_struct *mm;
738
739                         atomic_inc(&new_start_mm->mm_users);
740                         atomic_inc(&prev_mm->mm_users);
741                         spin_lock(&mmlist_lock);
742                         while (*swap_map > 1 && !retval &&
743                                         (p = p->next) != &start_mm->mmlist) {
744                                 mm = list_entry(p, struct mm_struct, mmlist);
745                                 atomic_inc(&mm->mm_users);
746                                 spin_unlock(&mmlist_lock);
747                                 mmput(prev_mm);
748                                 prev_mm = mm;
749
750                                 cond_resched();
751
752                                 swcount = *swap_map;
753                                 if (swcount <= 1)
754                                         ;
755                                 else if (mm == &init_mm) {
756                                         set_start_mm = 1;
757                                         shmem = shmem_unuse(entry, page);
758                                 } else
759                                         retval = unuse_process(mm, entry, page);
760                                 if (set_start_mm && *swap_map < swcount) {
761                                         mmput(new_start_mm);
762                                         atomic_inc(&mm->mm_users);
763                                         new_start_mm = mm;
764                                         set_start_mm = 0;
765                                 }
766                                 spin_lock(&mmlist_lock);
767                         }
768                         spin_unlock(&mmlist_lock);
769                         mmput(prev_mm);
770                         mmput(start_mm);
771                         start_mm = new_start_mm;
772                 }
773                 if (retval) {
774                         unlock_page(page);
775                         page_cache_release(page);
776                         break;
777                 }
778
779                 /*
780                  * How could swap count reach 0x7fff when the maximum
781                  * pid is 0x7fff, and there's no way to repeat a swap
782                  * page within an mm (except in shmem, where it's the
783                  * shared object which takes the reference count)?
784                  * We believe SWAP_MAP_MAX cannot occur in Linux 2.4.
785                  *
786                  * If that's wrong, then we should worry more about
787                  * exit_mmap() and do_munmap() cases described above:
788                  * we might be resetting SWAP_MAP_MAX too early here.
789                  * We know "Undead"s can happen, they're okay, so don't
790                  * report them; but do report if we reset SWAP_MAP_MAX.
791                  */
792                 if (*swap_map == SWAP_MAP_MAX) {
793                         swap_device_lock(si);
794                         *swap_map = 1;
795                         swap_device_unlock(si);
796                         reset_overflow = 1;
797                 }
798
799                 /*
800                  * If a reference remains (rare), we would like to leave
801                  * the page in the swap cache; but try_to_unmap could
802                  * then re-duplicate the entry once we drop page lock,
803                  * so we might loop indefinitely; also, that page could
804                  * not be swapped out to other storage meanwhile.  So:
805                  * delete from cache even if there's another reference,
806                  * after ensuring that the data has been saved to disk -
807                  * since if the reference remains (rarer), it will be
808                  * read from disk into another page.  Splitting into two
809                  * pages would be incorrect if swap supported "shared
810                  * private" pages, but they are handled by tmpfs files.
811                  *
812                  * Note shmem_unuse already deleted a swappage from
813                  * the swap cache, unless the move to filepage failed:
814                  * in which case it left swappage in cache, lowered its
815                  * swap count to pass quickly through the loops above,
816                  * and now we must reincrement count to try again later.
817                  */
818                 if ((*swap_map > 1) && PageDirty(page) && PageSwapCache(page)) {
819                         struct writeback_control wbc = {
820                                 .sync_mode = WB_SYNC_NONE,
821                         };
822
823                         swap_writepage(page, &wbc);
824                         lock_page(page);
825                         wait_on_page_writeback(page);
826                 }
827                 if (PageSwapCache(page)) {
828                         if (shmem)
829                                 swap_duplicate(entry);
830                         else
831                                 delete_from_swap_cache(page);
832                 }
833
834                 /*
835                  * So we could skip searching mms once swap count went
836                  * to 1, we did not mark any present ptes as dirty: must
837                  * mark page dirty so shrink_list will preserve it.
838                  */
839                 SetPageDirty(page);
840                 unlock_page(page);
841                 page_cache_release(page);
842
843                 /*
844                  * Make sure that we aren't completely killing
845                  * interactive performance.
846                  */
847                 cond_resched();
848         }
849
850         mmput(start_mm);
851         if (reset_overflow) {
852                 printk(KERN_WARNING "swapoff: cleared swap entry overflow\n");
853                 swap_overflow = 0;
854         }
855         return retval;
856 }
857
858 /*
859  * Use this swapdev's extent info to locate the (PAGE_SIZE) block which
860  * corresponds to page offset `offset'.
861  */
862 sector_t map_swap_page(struct swap_info_struct *sis, pgoff_t offset)
863 {
864         struct swap_extent *se = sis->curr_swap_extent;
865         struct swap_extent *start_se = se;
866
867         for ( ; ; ) {
868                 struct list_head *lh;
869
870                 if (se->start_page <= offset &&
871                                 offset < (se->start_page + se->nr_pages)) {
872                         return se->start_block + (offset - se->start_page);
873                 }
874                 lh = se->list.prev;
875                 if (lh == &sis->extent_list)
876                         lh = lh->prev;
877                 se = list_entry(lh, struct swap_extent, list);
878                 sis->curr_swap_extent = se;
879                 BUG_ON(se == start_se);         /* It *must* be present */
880         }
881 }
882
883 /*
884  * Free all of a swapdev's extent information
885  */
886 static void destroy_swap_extents(struct swap_info_struct *sis)
887 {
888         while (!list_empty(&sis->extent_list)) {
889                 struct swap_extent *se;
890
891                 se = list_entry(sis->extent_list.next,
892                                 struct swap_extent, list);
893                 list_del(&se->list);
894                 kfree(se);
895         }
896         sis->nr_extents = 0;
897 }
898
899 /*
900  * Add a block range (and the corresponding page range) into this swapdev's
901  * extent list.  The extent list is kept sorted in block order.
902  *
903  * This function rather assumes that it is called in ascending sector_t order.
904  * It doesn't look for extent coalescing opportunities.
905  */
906 static int
907 add_swap_extent(struct swap_info_struct *sis, unsigned long start_page,
908                 unsigned long nr_pages, sector_t start_block)
909 {
910         struct swap_extent *se;
911         struct swap_extent *new_se;
912         struct list_head *lh;
913
914         lh = sis->extent_list.next;     /* The highest-addressed block */
915         while (lh != &sis->extent_list) {
916                 se = list_entry(lh, struct swap_extent, list);
917                 if (se->start_block + se->nr_pages == start_block &&
918                     se->start_page  + se->nr_pages == start_page) {
919                         /* Merge it */
920                         se->nr_pages += nr_pages;
921                         return 0;
922                 }
923                 lh = lh->next;
924         }
925
926         /*
927          * No merge.  Insert a new extent, preserving ordering.
928          */
929         new_se = kmalloc(sizeof(*se), GFP_KERNEL);
930         if (new_se == NULL)
931                 return -ENOMEM;
932         new_se->start_page = start_page;
933         new_se->nr_pages = nr_pages;
934         new_se->start_block = start_block;
935
936         lh = sis->extent_list.prev;     /* The lowest block */
937         while (lh != &sis->extent_list) {
938                 se = list_entry(lh, struct swap_extent, list);
939                 if (se->start_block > start_block)
940                         break;
941                 lh = lh->prev;
942         }
943         list_add_tail(&new_se->list, lh);
944         sis->nr_extents++;
945         return 0;
946 }
947
948 /*
949  * A `swap extent' is a simple thing which maps a contiguous range of pages
950  * onto a contiguous range of disk blocks.  An ordered list of swap extents
951  * is built at swapon time and is then used at swap_writepage/swap_readpage
952  * time for locating where on disk a page belongs.
953  *
954  * If the swapfile is an S_ISBLK block device, a single extent is installed.
955  * This is done so that the main operating code can treat S_ISBLK and S_ISREG
956  * swap files identically.
957  *
958  * Whether the swapdev is an S_ISREG file or an S_ISBLK blockdev, the swap
959  * extent list operates in PAGE_SIZE disk blocks.  Both S_ISREG and S_ISBLK
960  * swapfiles are handled *identically* after swapon time.
961  *
962  * For S_ISREG swapfiles, setup_swap_extents() will walk all the file's blocks
963  * and will parse them into an ordered extent list, in PAGE_SIZE chunks.  If
964  * some stray blocks are found which do not fall within the PAGE_SIZE alignment
965  * requirements, they are simply tossed out - we will never use those blocks
966  * for swapping.
967  *
968  * For S_ISREG swapfiles we hold i_sem across the life of the swapon.  This
969  * prevents root from shooting her foot off by ftruncating an in-use swapfile,
970  * which will scribble on the fs.
971  *
972  * The amount of disk space which a single swap extent represents varies.
973  * Typically it is in the 1-4 megabyte range.  So we can have hundreds of
974  * extents in the list.  To avoid much list walking, we cache the previous
975  * search location in `curr_swap_extent', and start new searches from there.
976  * This is extremely effective.  The average number of iterations in
977  * map_swap_page() has been measured at about 0.3 per page.  - akpm.
978  */
979 static int setup_swap_extents(struct swap_info_struct *sis)
980 {
981         struct inode *inode;
982         unsigned blocks_per_page;
983         unsigned long page_no;
984         unsigned blkbits;
985         sector_t probe_block;
986         sector_t last_block;
987         int ret;
988
989         inode = sis->swap_file->f_mapping->host;
990         if (S_ISBLK(inode->i_mode)) {
991                 ret = add_swap_extent(sis, 0, sis->max, 0);
992                 goto done;
993         }
994
995         blkbits = inode->i_blkbits;
996         blocks_per_page = PAGE_SIZE >> blkbits;
997
998         /*
999          * Map all the blocks into the extent list.  This code doesn't try
1000          * to be very smart.
1001          */
1002         probe_block = 0;
1003         page_no = 0;
1004         last_block = i_size_read(inode) >> blkbits;
1005         while ((probe_block + blocks_per_page) <= last_block &&
1006                         page_no < sis->max) {
1007                 unsigned block_in_page;
1008                 sector_t first_block;
1009
1010                 first_block = bmap(inode, probe_block);
1011                 if (first_block == 0)
1012                         goto bad_bmap;
1013
1014                 /*
1015                  * It must be PAGE_SIZE aligned on-disk
1016                  */
1017                 if (first_block & (blocks_per_page - 1)) {
1018                         probe_block++;
1019                         goto reprobe;
1020                 }
1021
1022                 for (block_in_page = 1; block_in_page < blocks_per_page;
1023                                         block_in_page++) {
1024                         sector_t block;
1025
1026                         block = bmap(inode, probe_block + block_in_page);
1027                         if (block == 0)
1028                                 goto bad_bmap;
1029                         if (block != first_block + block_in_page) {
1030                                 /* Discontiguity */
1031                                 probe_block++;
1032                                 goto reprobe;
1033                         }
1034                 }
1035
1036                 /*
1037                  * We found a PAGE_SIZE-length, PAGE_SIZE-aligned run of blocks
1038                  */
1039                 ret = add_swap_extent(sis, page_no, 1,
1040                                 first_block >> (PAGE_SHIFT - blkbits));
1041                 if (ret)
1042                         goto out;
1043                 page_no++;
1044                 probe_block += blocks_per_page;
1045 reprobe:
1046                 continue;
1047         }
1048         ret = 0;
1049         if (page_no == 0)
1050                 ret = -EINVAL;
1051         sis->max = page_no;
1052         sis->highest_bit = page_no - 1;
1053 done:
1054         sis->curr_swap_extent = list_entry(sis->extent_list.prev,
1055                                         struct swap_extent, list);
1056         goto out;
1057 bad_bmap:
1058         printk(KERN_ERR "swapon: swapfile has holes\n");
1059         ret = -EINVAL;
1060 out:
1061         return ret;
1062 }
1063
1064 #if 0   /* We don't need this yet */
1065 #include <linux/backing-dev.h>
1066 int page_queue_congested(struct page *page)
1067 {
1068         struct backing_dev_info *bdi;
1069
1070         BUG_ON(!PageLocked(page));      /* It pins the swap_info_struct */
1071
1072         if (PageSwapCache(page)) {
1073                 swp_entry_t entry = { .val = page->private };
1074                 struct swap_info_struct *sis;
1075
1076                 sis = get_swap_info_struct(swp_type(entry));
1077                 bdi = sis->bdev->bd_inode->i_mapping->backing_dev_info;
1078         } else
1079                 bdi = page->mapping->backing_dev_info;
1080         return bdi_write_congested(bdi);
1081 }
1082 #endif
1083
1084 asmlinkage long sys_swapoff(const char __user * specialfile)
1085 {
1086         struct swap_info_struct * p = NULL;
1087         unsigned short *swap_map;
1088         struct file *swap_file, *victim;
1089         struct address_space *mapping;
1090         char * pathname;
1091         int i, type, prev;
1092         int err;
1093         
1094         if (!capable(CAP_SYS_ADMIN))
1095                 return -EPERM;
1096
1097         pathname = getname(specialfile);
1098         err = PTR_ERR(pathname);
1099         if (IS_ERR(pathname))
1100                 goto out;
1101
1102         victim = filp_open(pathname, O_RDWR, 0);
1103         putname(pathname);
1104         err = PTR_ERR(victim);
1105         if (IS_ERR(victim))
1106                 goto out;
1107
1108         mapping = victim->f_mapping;
1109         prev = -1;
1110         swap_list_lock();
1111         for (type = swap_list.head; type >= 0; type = swap_info[type].next) {
1112                 p = swap_info + type;
1113                 if ((p->flags & SWP_ACTIVE) == SWP_ACTIVE) {
1114                         if (p->swap_file->f_mapping == mapping)
1115                                 break;
1116                 }
1117                 prev = type;
1118         }
1119         if (type < 0) {
1120                 err = -EINVAL;
1121                 swap_list_unlock();
1122                 goto out_dput;
1123         }
1124         if (!security_vm_enough_memory(p->pages))
1125                 vm_unacct_memory(p->pages);
1126         else {
1127                 err = -ENOMEM;
1128                 swap_list_unlock();
1129                 goto out_dput;
1130         }
1131         if (prev < 0) {
1132                 swap_list.head = p->next;
1133         } else {
1134                 swap_info[prev].next = p->next;
1135         }
1136         if (type == swap_list.next) {
1137                 /* just pick something that's safe... */
1138                 swap_list.next = swap_list.head;
1139         }
1140         nr_swap_pages -= p->pages;
1141         total_swap_pages -= p->pages;
1142         p->flags &= ~SWP_WRITEOK;
1143         swap_list_unlock();
1144         current->flags |= PF_SWAPOFF;
1145         err = try_to_unuse(type);
1146         current->flags &= ~PF_SWAPOFF;
1147         if (err) {
1148                 /* re-insert swap space back into swap_list */
1149                 swap_list_lock();
1150                 for (prev = -1, i = swap_list.head; i >= 0; prev = i, i = swap_info[i].next)
1151                         if (p->prio >= swap_info[i].prio)
1152                                 break;
1153                 p->next = i;
1154                 if (prev < 0)
1155                         swap_list.head = swap_list.next = p - swap_info;
1156                 else
1157                         swap_info[prev].next = p - swap_info;
1158                 nr_swap_pages += p->pages;
1159                 total_swap_pages += p->pages;
1160                 p->flags |= SWP_WRITEOK;
1161                 swap_list_unlock();
1162                 goto out_dput;
1163         }
1164         down(&swapon_sem);
1165         down(&swap_bdevs_sem);
1166         swap_list_lock();
1167         swap_device_lock(p);
1168         swap_file = p->swap_file;
1169         p->swap_file = NULL;
1170         p->max = 0;
1171         swap_map = p->swap_map;
1172         p->swap_map = NULL;
1173         p->flags = 0;
1174         destroy_swap_extents(p);
1175         swap_device_unlock(p);
1176         swap_list_unlock();
1177         remove_swap_bdev(p->bdev);
1178         up(&swap_bdevs_sem);
1179         up(&swapon_sem);
1180         vfree(swap_map);
1181         if (S_ISBLK(mapping->host->i_mode)) {
1182                 struct block_device *bdev = I_BDEV(mapping->host);
1183                 set_blocksize(bdev, p->old_block_size);
1184                 bd_release(bdev);
1185         } else {
1186                 up(&mapping->host->i_sem);
1187         }
1188         filp_close(swap_file, NULL);
1189         err = 0;
1190
1191 out_dput:
1192         filp_close(victim, NULL);
1193 out:
1194         return err;
1195 }
1196
1197 #ifdef CONFIG_PROC_FS
1198 /* iterator */
1199 static void *swap_start(struct seq_file *swap, loff_t *pos)
1200 {
1201         struct swap_info_struct *ptr = swap_info;
1202         int i;
1203         loff_t l = *pos;
1204
1205         down(&swapon_sem);
1206
1207         for (i = 0; i < nr_swapfiles; i++, ptr++) {
1208                 if (!(ptr->flags & SWP_USED) || !ptr->swap_map)
1209                         continue;
1210                 if (!l--)
1211                         return ptr;
1212         }
1213
1214         return NULL;
1215 }
1216
1217 static void *swap_next(struct seq_file *swap, void *v, loff_t *pos)
1218 {
1219         struct swap_info_struct *ptr = v;
1220         struct swap_info_struct *endptr = swap_info + nr_swapfiles;
1221
1222         for (++ptr; ptr < endptr; ptr++) {
1223                 if (!(ptr->flags & SWP_USED) || !ptr->swap_map)
1224                         continue;
1225                 ++*pos;
1226                 return ptr;
1227         }
1228
1229         return NULL;
1230 }
1231
1232 static void swap_stop(struct seq_file *swap, void *v)
1233 {
1234         up(&swapon_sem);
1235 }
1236
1237 static int swap_show(struct seq_file *swap, void *v)
1238 {
1239         struct swap_info_struct *ptr = v;
1240         struct file *file;
1241         int len;
1242
1243         if (v == swap_info)
1244                 seq_puts(swap, "Filename\t\t\t\tType\t\tSize\tUsed\tPriority\n");
1245
1246         file = ptr->swap_file;
1247         len = seq_path(swap, file->f_vfsmnt, file->f_dentry, " \t\n\\");
1248         seq_printf(swap, "%*s%s\t%d\t%ld\t%d\n",
1249                        len < 40 ? 40 - len : 1, " ",
1250                        S_ISBLK(file->f_dentry->d_inode->i_mode) ?
1251                                 "partition" : "file\t",
1252                        ptr->pages << (PAGE_SHIFT - 10),
1253                        ptr->inuse_pages << (PAGE_SHIFT - 10),
1254                        ptr->prio);
1255         return 0;
1256 }
1257
1258 static struct seq_operations swaps_op = {
1259         .start =        swap_start,
1260         .next =         swap_next,
1261         .stop =         swap_stop,
1262         .show =         swap_show
1263 };
1264
1265 static int swaps_open(struct inode *inode, struct file *file)
1266 {
1267         return seq_open(file, &swaps_op);
1268 }
1269
1270 static struct file_operations proc_swaps_operations = {
1271         .open           = swaps_open,
1272         .read           = seq_read,
1273         .llseek         = seq_lseek,
1274         .release        = seq_release,
1275 };
1276
1277 static int __init procswaps_init(void)
1278 {
1279         struct proc_dir_entry *entry;
1280
1281         entry = create_proc_entry("swaps", 0, NULL);
1282         if (entry)
1283                 entry->proc_fops = &proc_swaps_operations;
1284         return 0;
1285 }
1286 __initcall(procswaps_init);
1287 #endif /* CONFIG_PROC_FS */
1288
1289 /*
1290  * Written 01/25/92 by Simmule Turner, heavily changed by Linus.
1291  *
1292  * The swapon system call
1293  */
1294 asmlinkage long sys_swapon(const char __user * specialfile, int swap_flags)
1295 {
1296         struct swap_info_struct * p;
1297         char *name = NULL;
1298         struct block_device *bdev = NULL;
1299         struct file *swap_file = NULL;
1300         struct address_space *mapping;
1301         unsigned int type;
1302         int i, prev;
1303         int error;
1304         static int least_priority;
1305         union swap_header *swap_header = 0;
1306         int swap_header_version;
1307         int nr_good_pages = 0;
1308         unsigned long maxpages = 1;
1309         int swapfilesize;
1310         unsigned short *swap_map;
1311         struct page *page = NULL;
1312         struct inode *inode = NULL;
1313         int did_down = 0;
1314
1315         if (!capable(CAP_SYS_ADMIN))
1316                 return -EPERM;
1317         swap_list_lock();
1318         p = swap_info;
1319         for (type = 0 ; type < nr_swapfiles ; type++,p++)
1320                 if (!(p->flags & SWP_USED))
1321                         break;
1322         error = -EPERM;
1323         /*
1324          * Test if adding another swap device is possible. There are
1325          * two limiting factors: 1) the number of bits for the swap
1326          * type swp_entry_t definition and 2) the number of bits for
1327          * the swap type in the swap ptes as defined by the different
1328          * architectures. To honor both limitations a swap entry
1329          * with swap offset 0 and swap type ~0UL is created, encoded
1330          * to a swap pte, decoded to a swp_entry_t again and finally
1331          * the swap type part is extracted. This will mask all bits
1332          * from the initial ~0UL that can't be encoded in either the
1333          * swp_entry_t or the architecture definition of a swap pte.
1334          */
1335         if (type > swp_type(pte_to_swp_entry(swp_entry_to_pte(swp_entry(~0UL,0))))) {
1336                 swap_list_unlock();
1337                 goto out;
1338         }
1339         if (type >= nr_swapfiles)
1340                 nr_swapfiles = type+1;
1341         INIT_LIST_HEAD(&p->extent_list);
1342         p->flags = SWP_USED;
1343         p->nr_extents = 0;
1344         p->swap_file = NULL;
1345         p->old_block_size = 0;
1346         p->swap_map = NULL;
1347         p->lowest_bit = 0;
1348         p->highest_bit = 0;
1349         p->cluster_nr = 0;
1350         p->inuse_pages = 0;
1351         p->sdev_lock = SPIN_LOCK_UNLOCKED;
1352         p->next = -1;
1353         if (swap_flags & SWAP_FLAG_PREFER) {
1354                 p->prio =
1355                   (swap_flags & SWAP_FLAG_PRIO_MASK)>>SWAP_FLAG_PRIO_SHIFT;
1356         } else {
1357                 p->prio = --least_priority;
1358         }
1359         swap_list_unlock();
1360         name = getname(specialfile);
1361         error = PTR_ERR(name);
1362         if (IS_ERR(name)) {
1363                 name = NULL;
1364                 goto bad_swap_2;
1365         }
1366         swap_file = filp_open(name, O_RDWR, 0);
1367         error = PTR_ERR(swap_file);
1368         if (IS_ERR(swap_file)) {
1369                 swap_file = NULL;
1370                 goto bad_swap_2;
1371         }
1372
1373         p->swap_file = swap_file;
1374         mapping = swap_file->f_mapping;
1375         inode = mapping->host;
1376
1377         error = -EBUSY;
1378         for (i = 0; i < nr_swapfiles; i++) {
1379                 struct swap_info_struct *q = &swap_info[i];
1380
1381                 if (i == type || !q->swap_file)
1382                         continue;
1383                 if (mapping == q->swap_file->f_mapping)
1384                         goto bad_swap;
1385         }
1386
1387         error = -EINVAL;
1388         if (S_ISBLK(inode->i_mode)) {
1389                 bdev = I_BDEV(inode);
1390                 error = bd_claim(bdev, sys_swapon);
1391                 if (error < 0) {
1392                         bdev = NULL;
1393                         goto bad_swap;
1394                 }
1395                 p->old_block_size = block_size(bdev);
1396                 error = set_blocksize(bdev, PAGE_SIZE);
1397                 if (error < 0)
1398                         goto bad_swap;
1399                 p->bdev = bdev;
1400         } else if (S_ISREG(inode->i_mode)) {
1401                 p->bdev = inode->i_sb->s_bdev;
1402                 down(&inode->i_sem);
1403                 did_down = 1;
1404         } else {
1405                 goto bad_swap;
1406         }
1407
1408         swapfilesize = i_size_read(inode) >> PAGE_SHIFT;
1409
1410         /*
1411          * Read the swap header.
1412          */
1413         if (!mapping->a_ops->readpage) {
1414                 error = -EINVAL;
1415                 goto bad_swap;
1416         }
1417         page = read_cache_page(mapping, 0,
1418                         (filler_t *)mapping->a_ops->readpage, swap_file);
1419         if (IS_ERR(page)) {
1420                 error = PTR_ERR(page);
1421                 goto bad_swap;
1422         }
1423         wait_on_page_locked(page);
1424         if (!PageUptodate(page))
1425                 goto bad_swap;
1426         kmap(page);
1427         swap_header = page_address(page);
1428
1429         if (!memcmp("SWAP-SPACE",swap_header->magic.magic,10))
1430                 swap_header_version = 1;
1431         else if (!memcmp("SWAPSPACE2",swap_header->magic.magic,10))
1432                 swap_header_version = 2;
1433         else {
1434                 printk("Unable to find swap-space signature\n");
1435                 error = -EINVAL;
1436                 goto bad_swap;
1437         }
1438         
1439         switch (swap_header_version) {
1440         case 1:
1441                 printk(KERN_ERR "version 0 swap is no longer supported. "
1442                         "Use mkswap -v1 %s\n", name);
1443                 error = -EINVAL;
1444                 goto bad_swap;
1445         case 2:
1446                 /* Check the swap header's sub-version and the size of
1447                    the swap file and bad block lists */
1448                 if (swap_header->info.version != 1) {
1449                         printk(KERN_WARNING
1450                                "Unable to handle swap header version %d\n",
1451                                swap_header->info.version);
1452                         error = -EINVAL;
1453                         goto bad_swap;
1454                 }
1455
1456                 p->lowest_bit  = 1;
1457                 /*
1458                  * Find out how many pages are allowed for a single swap
1459                  * device. There are two limiting factors: 1) the number of
1460                  * bits for the swap offset in the swp_entry_t type and
1461                  * 2) the number of bits in the a swap pte as defined by
1462                  * the different architectures. In order to find the
1463                  * largest possible bit mask a swap entry with swap type 0
1464                  * and swap offset ~0UL is created, encoded to a swap pte,
1465                  * decoded to a swp_entry_t again and finally the swap
1466                  * offset is extracted. This will mask all the bits from
1467                  * the initial ~0UL mask that can't be encoded in either
1468                  * the swp_entry_t or the architecture definition of a
1469                  * swap pte.
1470                  */
1471                 maxpages = swp_offset(pte_to_swp_entry(swp_entry_to_pte(swp_entry(0,~0UL)))) - 1;
1472                 if (maxpages > swap_header->info.last_page)
1473                         maxpages = swap_header->info.last_page;
1474                 p->highest_bit = maxpages - 1;
1475
1476                 error = -EINVAL;
1477                 if (swap_header->info.nr_badpages > MAX_SWAP_BADPAGES)
1478                         goto bad_swap;
1479                 
1480                 /* OK, set up the swap map and apply the bad block list */
1481                 if (!(p->swap_map = vmalloc(maxpages * sizeof(short)))) {
1482                         error = -ENOMEM;
1483                         goto bad_swap;
1484                 }
1485
1486                 error = 0;
1487                 memset(p->swap_map, 0, maxpages * sizeof(short));
1488                 for (i=0; i<swap_header->info.nr_badpages; i++) {
1489                         int page = swap_header->info.badpages[i];
1490                         if (page <= 0 || page >= swap_header->info.last_page)
1491                                 error = -EINVAL;
1492                         else
1493                                 p->swap_map[page] = SWAP_MAP_BAD;
1494                 }
1495                 nr_good_pages = swap_header->info.last_page -
1496                                 swap_header->info.nr_badpages -
1497                                 1 /* header page */;
1498                 if (error) 
1499                         goto bad_swap;
1500         }
1501         
1502         if (swapfilesize && maxpages > swapfilesize) {
1503                 printk(KERN_WARNING
1504                        "Swap area shorter than signature indicates\n");
1505                 error = -EINVAL;
1506                 goto bad_swap;
1507         }
1508         if (!nr_good_pages) {
1509                 printk(KERN_WARNING "Empty swap-file\n");
1510                 error = -EINVAL;
1511                 goto bad_swap;
1512         }
1513         p->swap_map[0] = SWAP_MAP_BAD;
1514         p->max = maxpages;
1515         p->pages = nr_good_pages;
1516
1517         error = setup_swap_extents(p);
1518         if (error)
1519                 goto bad_swap;
1520
1521         down(&swapon_sem);
1522         down(&swap_bdevs_sem);
1523         swap_list_lock();
1524         swap_device_lock(p);
1525         p->flags = SWP_ACTIVE;
1526         nr_swap_pages += nr_good_pages;
1527         total_swap_pages += nr_good_pages;
1528         printk(KERN_INFO "Adding %dk swap on %s.  Priority:%d extents:%d\n",
1529                 nr_good_pages<<(PAGE_SHIFT-10), name,
1530                 p->prio, p->nr_extents);
1531
1532         /* insert swap space into swap_list: */
1533         prev = -1;
1534         for (i = swap_list.head; i >= 0; i = swap_info[i].next) {
1535                 if (p->prio >= swap_info[i].prio) {
1536                         break;
1537                 }
1538                 prev = i;
1539         }
1540         p->next = i;
1541         if (prev < 0) {
1542                 swap_list.head = swap_list.next = p - swap_info;
1543         } else {
1544                 swap_info[prev].next = p - swap_info;
1545         }
1546         swap_device_unlock(p);
1547         swap_list_unlock();
1548         install_swap_bdev(p->bdev);
1549         up(&swap_bdevs_sem);
1550         up(&swapon_sem);
1551         error = 0;
1552         goto out;
1553 bad_swap:
1554         if (bdev) {
1555                 set_blocksize(bdev, p->old_block_size);
1556                 bd_release(bdev);
1557         }
1558 bad_swap_2:
1559         swap_list_lock();
1560         swap_map = p->swap_map;
1561         p->swap_file = NULL;
1562         p->swap_map = NULL;
1563         p->flags = 0;
1564         if (!(swap_flags & SWAP_FLAG_PREFER))
1565                 ++least_priority;
1566         swap_list_unlock();
1567         destroy_swap_extents(p);
1568         if (swap_map)
1569                 vfree(swap_map);
1570         if (swap_file)
1571                 filp_close(swap_file, NULL);
1572 out:
1573         if (page && !IS_ERR(page)) {
1574                 kunmap(page);
1575                 page_cache_release(page);
1576         }
1577         if (name)
1578                 putname(name);
1579         if (error && did_down)
1580                 up(&inode->i_sem);
1581         return error;
1582 }
1583
1584 void si_swapinfo(struct sysinfo *val)
1585 {
1586         unsigned int i;
1587         unsigned long nr_to_be_unused = 0;
1588
1589         swap_list_lock();
1590         for (i = 0; i < nr_swapfiles; i++) {
1591                 if (!(swap_info[i].flags & SWP_USED) ||
1592                      (swap_info[i].flags & SWP_WRITEOK))
1593                         continue;
1594                 nr_to_be_unused += swap_info[i].inuse_pages;
1595         }
1596         val->freeswap = nr_swap_pages + nr_to_be_unused;
1597         val->totalswap = total_swap_pages + nr_to_be_unused;
1598         swap_list_unlock();
1599         if (vx_flags(VXF_VIRT_MEM, 0))
1600                 vx_vsi_swapinfo(val);
1601 }
1602
1603 /*
1604  * Verify that a swap entry is valid and increment its swap map count.
1605  *
1606  * Note: if swap_map[] reaches SWAP_MAP_MAX the entries are treated as
1607  * "permanent", but will be reclaimed by the next swapoff.
1608  */
1609 int swap_duplicate(swp_entry_t entry)
1610 {
1611         struct swap_info_struct * p;
1612         unsigned long offset, type;
1613         int result = 0;
1614
1615         type = swp_type(entry);
1616         if (type >= nr_swapfiles)
1617                 goto bad_file;
1618         p = type + swap_info;
1619         offset = swp_offset(entry);
1620
1621         swap_device_lock(p);
1622         if (offset < p->max && p->swap_map[offset]) {
1623                 if (p->swap_map[offset] < SWAP_MAP_MAX - 1) {
1624                         p->swap_map[offset]++;
1625                         result = 1;
1626                 } else if (p->swap_map[offset] <= SWAP_MAP_MAX) {
1627                         if (swap_overflow++ < 5)
1628                                 printk(KERN_WARNING "swap_dup: swap entry overflow\n");
1629                         p->swap_map[offset] = SWAP_MAP_MAX;
1630                         result = 1;
1631                 }
1632         }
1633         swap_device_unlock(p);
1634 out:
1635         return result;
1636
1637 bad_file:
1638         printk(KERN_ERR "swap_dup: %s%08lx\n", Bad_file, entry.val);
1639         goto out;
1640 }
1641
1642 struct swap_info_struct *
1643 get_swap_info_struct(unsigned type)
1644 {
1645         return &swap_info[type];
1646 }
1647
1648 /*
1649  * swap_device_lock prevents swap_map being freed. Don't grab an extra
1650  * reference on the swaphandle, it doesn't matter if it becomes unused.
1651  */
1652 int valid_swaphandles(swp_entry_t entry, unsigned long *offset)
1653 {
1654         int ret = 0, i = 1 << page_cluster;
1655         unsigned long toff;
1656         struct swap_info_struct *swapdev = swp_type(entry) + swap_info;
1657
1658         if (!page_cluster)      /* no readahead */
1659                 return 0;
1660         toff = (swp_offset(entry) >> page_cluster) << page_cluster;
1661         if (!toff)              /* first page is swap header */
1662                 toff++, i--;
1663         *offset = toff;
1664
1665         swap_device_lock(swapdev);
1666         do {
1667                 /* Don't read-ahead past the end of the swap area */
1668                 if (toff >= swapdev->max)
1669                         break;
1670                 /* Don't read in free or bad pages */
1671                 if (!swapdev->swap_map[toff])
1672                         break;
1673                 if (swapdev->swap_map[toff] == SWAP_MAP_BAD)
1674                         break;
1675                 toff++;
1676                 ret++;
1677         } while (--i);
1678         swap_device_unlock(swapdev);
1679         return ret;
1680 }