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