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