added 2.6.29.6 aldebaran kernel
[nao-ulib.git] / kernel / 2.6.29.6-aldebaran-rt / mm / swapfile.c
blobce29a24d29b9a6cbdd4560a9d63d5b4c1588e4a8
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/random.h>
20 #include <linux/writeback.h>
21 #include <linux/proc_fs.h>
22 #include <linux/seq_file.h>
23 #include <linux/init.h>
24 #include <linux/module.h>
25 #include <linux/rmap.h>
26 #include <linux/security.h>
27 #include <linux/backing-dev.h>
28 #include <linux/mutex.h>
29 #include <linux/capability.h>
30 #include <linux/syscalls.h>
31 #include <linux/memcontrol.h>
33 #include <asm/pgtable.h>
34 #include <asm/tlbflush.h>
35 #include <linux/swapops.h>
36 #include <linux/page_cgroup.h>
38 static DEFINE_SPINLOCK(swap_lock);
39 static unsigned int nr_swapfiles;
40 long nr_swap_pages;
41 long total_swap_pages;
42 static int swap_overflow;
43 static int least_priority;
45 static const char Bad_file[] = "Bad swap file entry ";
46 static const char Unused_file[] = "Unused swap file entry ";
47 static const char Bad_offset[] = "Bad swap offset entry ";
48 static const char Unused_offset[] = "Unused swap offset entry ";
50 static struct swap_list_t swap_list = {-1, -1};
52 static struct swap_info_struct swap_info[MAX_SWAPFILES];
54 static DEFINE_MUTEX(swapon_mutex);
57 * We need this because the bdev->unplug_fn can sleep and we cannot
58 * hold swap_lock while calling the unplug_fn. And swap_lock
59 * cannot be turned into a mutex.
61 static DECLARE_RWSEM(swap_unplug_sem);
63 void swap_unplug_io_fn(struct backing_dev_info *unused_bdi, struct page *page)
65 swp_entry_t entry;
67 down_read(&swap_unplug_sem);
68 entry.val = page_private(page);
69 if (PageSwapCache(page)) {
70 struct block_device *bdev = swap_info[swp_type(entry)].bdev;
71 struct backing_dev_info *bdi;
74 * If the page is removed from swapcache from under us (with a
75 * racy try_to_unuse/swapoff) we need an additional reference
76 * count to avoid reading garbage from page_private(page) above.
77 * If the WARN_ON triggers during a swapoff it maybe the race
78 * condition and it's harmless. However if it triggers without
79 * swapoff it signals a problem.
81 WARN_ON(page_count(page) <= 1);
83 bdi = bdev->bd_inode->i_mapping->backing_dev_info;
84 blk_run_backing_dev(bdi, page);
86 up_read(&swap_unplug_sem);
90 * swapon tell device that all the old swap contents can be discarded,
91 * to allow the swap device to optimize its wear-levelling.
93 static int discard_swap(struct swap_info_struct *si)
95 struct swap_extent *se;
96 int err = 0;
98 list_for_each_entry(se, &si->extent_list, list) {
99 sector_t start_block = se->start_block << (PAGE_SHIFT - 9);
100 sector_t nr_blocks = (sector_t)se->nr_pages << (PAGE_SHIFT - 9);
102 if (se->start_page == 0) {
103 /* Do not discard the swap header page! */
104 start_block += 1 << (PAGE_SHIFT - 9);
105 nr_blocks -= 1 << (PAGE_SHIFT - 9);
106 if (!nr_blocks)
107 continue;
110 err = blkdev_issue_discard(si->bdev, start_block,
111 nr_blocks, GFP_KERNEL);
112 if (err)
113 break;
115 cond_resched();
117 return err; /* That will often be -EOPNOTSUPP */
121 * swap allocation tell device that a cluster of swap can now be discarded,
122 * to allow the swap device to optimize its wear-levelling.
124 static void discard_swap_cluster(struct swap_info_struct *si,
125 pgoff_t start_page, pgoff_t nr_pages)
127 struct swap_extent *se = si->curr_swap_extent;
128 int found_extent = 0;
130 while (nr_pages) {
131 struct list_head *lh;
133 if (se->start_page <= start_page &&
134 start_page < se->start_page + se->nr_pages) {
135 pgoff_t offset = start_page - se->start_page;
136 sector_t start_block = se->start_block + offset;
137 sector_t nr_blocks = se->nr_pages - offset;
139 if (nr_blocks > nr_pages)
140 nr_blocks = nr_pages;
141 start_page += nr_blocks;
142 nr_pages -= nr_blocks;
144 if (!found_extent++)
145 si->curr_swap_extent = se;
147 start_block <<= PAGE_SHIFT - 9;
148 nr_blocks <<= PAGE_SHIFT - 9;
149 if (blkdev_issue_discard(si->bdev, start_block,
150 nr_blocks, GFP_NOIO))
151 break;
154 lh = se->list.next;
155 if (lh == &si->extent_list)
156 lh = lh->next;
157 se = list_entry(lh, struct swap_extent, list);
161 static int wait_for_discard(void *word)
163 schedule();
164 return 0;
167 #define SWAPFILE_CLUSTER 256
168 #define LATENCY_LIMIT 256
170 static inline unsigned long scan_swap_map(struct swap_info_struct *si)
172 unsigned long offset;
173 unsigned long scan_base;
174 unsigned long last_in_cluster = 0;
175 int latency_ration = LATENCY_LIMIT;
176 int found_free_cluster = 0;
179 * We try to cluster swap pages by allocating them sequentially
180 * in swap. Once we've allocated SWAPFILE_CLUSTER pages this
181 * way, however, we resort to first-free allocation, starting
182 * a new cluster. This prevents us from scattering swap pages
183 * all over the entire swap partition, so that we reduce
184 * overall disk seek times between swap pages. -- sct
185 * But we do now try to find an empty cluster. -Andrea
186 * And we let swap pages go all over an SSD partition. Hugh
189 si->flags += SWP_SCANNING;
190 scan_base = offset = si->cluster_next;
192 if (unlikely(!si->cluster_nr--)) {
193 if (si->pages - si->inuse_pages < SWAPFILE_CLUSTER) {
194 si->cluster_nr = SWAPFILE_CLUSTER - 1;
195 goto checks;
197 if (si->flags & SWP_DISCARDABLE) {
199 * Start range check on racing allocations, in case
200 * they overlap the cluster we eventually decide on
201 * (we scan without swap_lock to allow preemption).
202 * It's hardly conceivable that cluster_nr could be
203 * wrapped during our scan, but don't depend on it.
205 if (si->lowest_alloc)
206 goto checks;
207 si->lowest_alloc = si->max;
208 si->highest_alloc = 0;
210 spin_unlock(&swap_lock);
213 * If seek is expensive, start searching for new cluster from
214 * start of partition, to minimize the span of allocated swap.
215 * But if seek is cheap, search from our current position, so
216 * that swap is allocated from all over the partition: if the
217 * Flash Translation Layer only remaps within limited zones,
218 * we don't want to wear out the first zone too quickly.
220 if (!(si->flags & SWP_SOLIDSTATE))
221 scan_base = offset = si->lowest_bit;
222 last_in_cluster = offset + SWAPFILE_CLUSTER - 1;
224 /* Locate the first empty (unaligned) cluster */
225 for (; last_in_cluster <= si->highest_bit; offset++) {
226 if (si->swap_map[offset])
227 last_in_cluster = offset + SWAPFILE_CLUSTER;
228 else if (offset == last_in_cluster) {
229 spin_lock(&swap_lock);
230 offset -= SWAPFILE_CLUSTER - 1;
231 si->cluster_next = offset;
232 si->cluster_nr = SWAPFILE_CLUSTER - 1;
233 found_free_cluster = 1;
234 goto checks;
236 if (unlikely(--latency_ration < 0)) {
237 cond_resched();
238 latency_ration = LATENCY_LIMIT;
242 offset = si->lowest_bit;
243 last_in_cluster = offset + SWAPFILE_CLUSTER - 1;
245 /* Locate the first empty (unaligned) cluster */
246 for (; last_in_cluster < scan_base; offset++) {
247 if (si->swap_map[offset])
248 last_in_cluster = offset + SWAPFILE_CLUSTER;
249 else if (offset == last_in_cluster) {
250 spin_lock(&swap_lock);
251 offset -= SWAPFILE_CLUSTER - 1;
252 si->cluster_next = offset;
253 si->cluster_nr = SWAPFILE_CLUSTER - 1;
254 found_free_cluster = 1;
255 goto checks;
257 if (unlikely(--latency_ration < 0)) {
258 cond_resched();
259 latency_ration = LATENCY_LIMIT;
263 offset = scan_base;
264 spin_lock(&swap_lock);
265 si->cluster_nr = SWAPFILE_CLUSTER - 1;
266 si->lowest_alloc = 0;
269 checks:
270 if (!(si->flags & SWP_WRITEOK))
271 goto no_page;
272 if (!si->highest_bit)
273 goto no_page;
274 if (offset > si->highest_bit)
275 scan_base = offset = si->lowest_bit;
276 if (si->swap_map[offset])
277 goto scan;
279 if (offset == si->lowest_bit)
280 si->lowest_bit++;
281 if (offset == si->highest_bit)
282 si->highest_bit--;
283 si->inuse_pages++;
284 if (si->inuse_pages == si->pages) {
285 si->lowest_bit = si->max;
286 si->highest_bit = 0;
288 si->swap_map[offset] = 1;
289 si->cluster_next = offset + 1;
290 si->flags -= SWP_SCANNING;
292 if (si->lowest_alloc) {
294 * Only set when SWP_DISCARDABLE, and there's a scan
295 * for a free cluster in progress or just completed.
297 if (found_free_cluster) {
299 * To optimize wear-levelling, discard the
300 * old data of the cluster, taking care not to
301 * discard any of its pages that have already
302 * been allocated by racing tasks (offset has
303 * already stepped over any at the beginning).
305 if (offset < si->highest_alloc &&
306 si->lowest_alloc <= last_in_cluster)
307 last_in_cluster = si->lowest_alloc - 1;
308 si->flags |= SWP_DISCARDING;
309 spin_unlock(&swap_lock);
311 if (offset < last_in_cluster)
312 discard_swap_cluster(si, offset,
313 last_in_cluster - offset + 1);
315 spin_lock(&swap_lock);
316 si->lowest_alloc = 0;
317 si->flags &= ~SWP_DISCARDING;
319 smp_mb(); /* wake_up_bit advises this */
320 wake_up_bit(&si->flags, ilog2(SWP_DISCARDING));
322 } else if (si->flags & SWP_DISCARDING) {
324 * Delay using pages allocated by racing tasks
325 * until the whole discard has been issued. We
326 * could defer that delay until swap_writepage,
327 * but it's easier to keep this self-contained.
329 spin_unlock(&swap_lock);
330 wait_on_bit(&si->flags, ilog2(SWP_DISCARDING),
331 wait_for_discard, TASK_UNINTERRUPTIBLE);
332 spin_lock(&swap_lock);
333 } else {
335 * Note pages allocated by racing tasks while
336 * scan for a free cluster is in progress, so
337 * that its final discard can exclude them.
339 if (offset < si->lowest_alloc)
340 si->lowest_alloc = offset;
341 if (offset > si->highest_alloc)
342 si->highest_alloc = offset;
345 return offset;
347 scan:
348 spin_unlock(&swap_lock);
349 while (++offset <= si->highest_bit) {
350 if (!si->swap_map[offset]) {
351 spin_lock(&swap_lock);
352 goto checks;
354 if (unlikely(--latency_ration < 0)) {
355 cond_resched();
356 latency_ration = LATENCY_LIMIT;
359 offset = si->lowest_bit;
360 while (++offset < scan_base) {
361 if (!si->swap_map[offset]) {
362 spin_lock(&swap_lock);
363 goto checks;
365 if (unlikely(--latency_ration < 0)) {
366 cond_resched();
367 latency_ration = LATENCY_LIMIT;
370 spin_lock(&swap_lock);
372 no_page:
373 si->flags -= SWP_SCANNING;
374 return 0;
377 swp_entry_t get_swap_page(void)
379 struct swap_info_struct *si;
380 pgoff_t offset;
381 int type, next;
382 int wrapped = 0;
384 spin_lock(&swap_lock);
385 if (nr_swap_pages <= 0)
386 goto noswap;
387 nr_swap_pages--;
389 for (type = swap_list.next; type >= 0 && wrapped < 2; type = next) {
390 si = swap_info + type;
391 next = si->next;
392 if (next < 0 ||
393 (!wrapped && si->prio != swap_info[next].prio)) {
394 next = swap_list.head;
395 wrapped++;
398 if (!si->highest_bit)
399 continue;
400 if (!(si->flags & SWP_WRITEOK))
401 continue;
403 swap_list.next = next;
404 offset = scan_swap_map(si);
405 if (offset) {
406 spin_unlock(&swap_lock);
407 return swp_entry(type, offset);
409 next = swap_list.next;
412 nr_swap_pages++;
413 noswap:
414 spin_unlock(&swap_lock);
415 return (swp_entry_t) {0};
418 swp_entry_t get_swap_page_of_type(int type)
420 struct swap_info_struct *si;
421 pgoff_t offset;
423 spin_lock(&swap_lock);
424 si = swap_info + type;
425 if (si->flags & SWP_WRITEOK) {
426 nr_swap_pages--;
427 offset = scan_swap_map(si);
428 if (offset) {
429 spin_unlock(&swap_lock);
430 return swp_entry(type, offset);
432 nr_swap_pages++;
434 spin_unlock(&swap_lock);
435 return (swp_entry_t) {0};
438 static struct swap_info_struct * swap_info_get(swp_entry_t entry)
440 struct swap_info_struct * p;
441 unsigned long offset, type;
443 if (!entry.val)
444 goto out;
445 type = swp_type(entry);
446 if (type >= nr_swapfiles)
447 goto bad_nofile;
448 p = & swap_info[type];
449 if (!(p->flags & SWP_USED))
450 goto bad_device;
451 offset = swp_offset(entry);
452 if (offset >= p->max)
453 goto bad_offset;
454 if (!p->swap_map[offset])
455 goto bad_free;
456 spin_lock(&swap_lock);
457 return p;
459 bad_free:
460 printk(KERN_ERR "swap_free: %s%08lx\n", Unused_offset, entry.val);
461 goto out;
462 bad_offset:
463 printk(KERN_ERR "swap_free: %s%08lx\n", Bad_offset, entry.val);
464 goto out;
465 bad_device:
466 printk(KERN_ERR "swap_free: %s%08lx\n", Unused_file, entry.val);
467 goto out;
468 bad_nofile:
469 printk(KERN_ERR "swap_free: %s%08lx\n", Bad_file, entry.val);
470 out:
471 return NULL;
474 static int swap_entry_free(struct swap_info_struct *p, swp_entry_t ent)
476 unsigned long offset = swp_offset(ent);
477 int count = p->swap_map[offset];
479 if (count < SWAP_MAP_MAX) {
480 count--;
481 p->swap_map[offset] = count;
482 if (!count) {
483 if (offset < p->lowest_bit)
484 p->lowest_bit = offset;
485 if (offset > p->highest_bit)
486 p->highest_bit = offset;
487 if (p->prio > swap_info[swap_list.next].prio)
488 swap_list.next = p - swap_info;
489 nr_swap_pages++;
490 p->inuse_pages--;
491 mem_cgroup_uncharge_swap(ent);
494 return count;
498 * Caller has made sure that the swapdevice corresponding to entry
499 * is still around or has not been recycled.
501 void swap_free(swp_entry_t entry)
503 struct swap_info_struct * p;
505 p = swap_info_get(entry);
506 if (p) {
507 swap_entry_free(p, entry);
508 spin_unlock(&swap_lock);
513 * How many references to page are currently swapped out?
515 static inline int page_swapcount(struct page *page)
517 int count = 0;
518 struct swap_info_struct *p;
519 swp_entry_t entry;
521 entry.val = page_private(page);
522 p = swap_info_get(entry);
523 if (p) {
524 /* Subtract the 1 for the swap cache itself */
525 count = p->swap_map[swp_offset(entry)] - 1;
526 spin_unlock(&swap_lock);
528 return count;
532 * We can write to an anon page without COW if there are no other references
533 * to it. And as a side-effect, free up its swap: because the old content
534 * on disk will never be read, and seeking back there to write new content
535 * later would only waste time away from clustering.
537 int reuse_swap_page(struct page *page)
539 int count;
541 VM_BUG_ON(!PageLocked(page));
542 count = page_mapcount(page);
543 if (count <= 1 && PageSwapCache(page)) {
544 count += page_swapcount(page);
545 if (count == 1 && !PageWriteback(page)) {
546 delete_from_swap_cache(page);
547 SetPageDirty(page);
550 return count == 1;
554 * If swap is getting full, or if there are no more mappings of this page,
555 * then try_to_free_swap is called to free its swap space.
557 int try_to_free_swap(struct page *page)
559 VM_BUG_ON(!PageLocked(page));
561 if (!PageSwapCache(page))
562 return 0;
563 if (PageWriteback(page))
564 return 0;
565 if (page_swapcount(page))
566 return 0;
568 delete_from_swap_cache(page);
569 SetPageDirty(page);
570 return 1;
574 * Free the swap entry like above, but also try to
575 * free the page cache entry if it is the last user.
577 int free_swap_and_cache(swp_entry_t entry)
579 struct swap_info_struct *p;
580 struct page *page = NULL;
582 if (is_migration_entry(entry))
583 return 1;
585 p = swap_info_get(entry);
586 if (p) {
587 if (swap_entry_free(p, entry) == 1) {
588 spin_unlock(&swap_lock);
589 page = find_get_page(&swapper_space, entry.val);
590 if (page && !trylock_page(page)) {
591 page_cache_release(page);
592 page = NULL;
594 } else
595 spin_unlock(&swap_lock);
597 if (page) {
599 * Not mapped elsewhere, or swap space full? Free it!
600 * Also recheck PageSwapCache now page is locked (above).
602 if (PageSwapCache(page) && !PageWriteback(page) &&
603 (!page_mapped(page) || vm_swap_full())) {
604 delete_from_swap_cache(page);
605 SetPageDirty(page);
607 unlock_page(page);
608 page_cache_release(page);
610 return p != NULL;
613 #ifdef CONFIG_HIBERNATION
615 * Find the swap type that corresponds to given device (if any).
617 * @offset - number of the PAGE_SIZE-sized block of the device, starting
618 * from 0, in which the swap header is expected to be located.
620 * This is needed for the suspend to disk (aka swsusp).
622 int swap_type_of(dev_t device, sector_t offset, struct block_device **bdev_p)
624 struct block_device *bdev = NULL;
625 int i;
627 if (device)
628 bdev = bdget(device);
630 spin_lock(&swap_lock);
631 for (i = 0; i < nr_swapfiles; i++) {
632 struct swap_info_struct *sis = swap_info + i;
634 if (!(sis->flags & SWP_WRITEOK))
635 continue;
637 if (!bdev) {
638 if (bdev_p)
639 *bdev_p = bdget(sis->bdev->bd_dev);
641 spin_unlock(&swap_lock);
642 return i;
644 if (bdev == sis->bdev) {
645 struct swap_extent *se;
647 se = list_entry(sis->extent_list.next,
648 struct swap_extent, list);
649 if (se->start_block == offset) {
650 if (bdev_p)
651 *bdev_p = bdget(sis->bdev->bd_dev);
653 spin_unlock(&swap_lock);
654 bdput(bdev);
655 return i;
659 spin_unlock(&swap_lock);
660 if (bdev)
661 bdput(bdev);
663 return -ENODEV;
667 * Return either the total number of swap pages of given type, or the number
668 * of free pages of that type (depending on @free)
670 * This is needed for software suspend
672 unsigned int count_swap_pages(int type, int free)
674 unsigned int n = 0;
676 if (type < nr_swapfiles) {
677 spin_lock(&swap_lock);
678 if (swap_info[type].flags & SWP_WRITEOK) {
679 n = swap_info[type].pages;
680 if (free)
681 n -= swap_info[type].inuse_pages;
683 spin_unlock(&swap_lock);
685 return n;
687 #endif
690 * No need to decide whether this PTE shares the swap entry with others,
691 * just let do_wp_page work it out if a write is requested later - to
692 * force COW, vm_page_prot omits write permission from any private vma.
694 static int unuse_pte(struct vm_area_struct *vma, pmd_t *pmd,
695 unsigned long addr, swp_entry_t entry, struct page *page)
697 struct mem_cgroup *ptr = NULL;
698 spinlock_t *ptl;
699 pte_t *pte;
700 int ret = 1;
702 if (mem_cgroup_try_charge_swapin(vma->vm_mm, page, GFP_KERNEL, &ptr)) {
703 ret = -ENOMEM;
704 goto out_nolock;
707 pte = pte_offset_map_lock(vma->vm_mm, pmd, addr, &ptl);
708 if (unlikely(!pte_same(*pte, swp_entry_to_pte(entry)))) {
709 if (ret > 0)
710 mem_cgroup_cancel_charge_swapin(ptr);
711 ret = 0;
712 goto out;
715 inc_mm_counter(vma->vm_mm, anon_rss);
716 get_page(page);
717 set_pte_at(vma->vm_mm, addr, pte,
718 pte_mkold(mk_pte(page, vma->vm_page_prot)));
719 page_add_anon_rmap(page, vma, addr);
720 mem_cgroup_commit_charge_swapin(page, ptr);
721 swap_free(entry);
723 * Move the page to the active list so it is not
724 * immediately swapped out again after swapon.
726 activate_page(page);
727 out:
728 pte_unmap_unlock(pte, ptl);
729 out_nolock:
730 return ret;
733 static int unuse_pte_range(struct vm_area_struct *vma, pmd_t *pmd,
734 unsigned long addr, unsigned long end,
735 swp_entry_t entry, struct page *page)
737 pte_t swp_pte = swp_entry_to_pte(entry);
738 pte_t *pte;
739 int ret = 0;
742 * We don't actually need pte lock while scanning for swp_pte: since
743 * we hold page lock and mmap_sem, swp_pte cannot be inserted into the
744 * page table while we're scanning; though it could get zapped, and on
745 * some architectures (e.g. x86_32 with PAE) we might catch a glimpse
746 * of unmatched parts which look like swp_pte, so unuse_pte must
747 * recheck under pte lock. Scanning without pte lock lets it be
748 * preemptible whenever CONFIG_PREEMPT but not CONFIG_HIGHPTE.
750 pte = pte_offset_map(pmd, addr);
751 do {
753 * swapoff spends a _lot_ of time in this loop!
754 * Test inline before going to call unuse_pte.
756 if (unlikely(pte_same(*pte, swp_pte))) {
757 pte_unmap(pte);
758 ret = unuse_pte(vma, pmd, addr, entry, page);
759 if (ret)
760 goto out;
761 pte = pte_offset_map(pmd, addr);
763 } while (pte++, addr += PAGE_SIZE, addr != end);
764 pte_unmap(pte - 1);
765 out:
766 return ret;
769 static inline int unuse_pmd_range(struct vm_area_struct *vma, pud_t *pud,
770 unsigned long addr, unsigned long end,
771 swp_entry_t entry, struct page *page)
773 pmd_t *pmd;
774 unsigned long next;
775 int ret;
777 pmd = pmd_offset(pud, addr);
778 do {
779 next = pmd_addr_end(addr, end);
780 if (pmd_none_or_clear_bad(pmd))
781 continue;
782 ret = unuse_pte_range(vma, pmd, addr, next, entry, page);
783 if (ret)
784 return ret;
785 } while (pmd++, addr = next, addr != end);
786 return 0;
789 static inline int unuse_pud_range(struct vm_area_struct *vma, pgd_t *pgd,
790 unsigned long addr, unsigned long end,
791 swp_entry_t entry, struct page *page)
793 pud_t *pud;
794 unsigned long next;
795 int ret;
797 pud = pud_offset(pgd, addr);
798 do {
799 next = pud_addr_end(addr, end);
800 if (pud_none_or_clear_bad(pud))
801 continue;
802 ret = unuse_pmd_range(vma, pud, addr, next, entry, page);
803 if (ret)
804 return ret;
805 } while (pud++, addr = next, addr != end);
806 return 0;
809 static int unuse_vma(struct vm_area_struct *vma,
810 swp_entry_t entry, struct page *page)
812 pgd_t *pgd;
813 unsigned long addr, end, next;
814 int ret;
816 if (page->mapping) {
817 addr = page_address_in_vma(page, vma);
818 if (addr == -EFAULT)
819 return 0;
820 else
821 end = addr + PAGE_SIZE;
822 } else {
823 addr = vma->vm_start;
824 end = vma->vm_end;
827 pgd = pgd_offset(vma->vm_mm, addr);
828 do {
829 next = pgd_addr_end(addr, end);
830 if (pgd_none_or_clear_bad(pgd))
831 continue;
832 ret = unuse_pud_range(vma, pgd, addr, next, entry, page);
833 if (ret)
834 return ret;
835 } while (pgd++, addr = next, addr != end);
836 return 0;
839 static int unuse_mm(struct mm_struct *mm,
840 swp_entry_t entry, struct page *page)
842 struct vm_area_struct *vma;
843 int ret = 0;
845 if (!down_read_trylock(&mm->mmap_sem)) {
847 * Activate page so shrink_inactive_list is unlikely to unmap
848 * its ptes while lock is dropped, so swapoff can make progress.
850 activate_page(page);
851 unlock_page(page);
852 down_read(&mm->mmap_sem);
853 lock_page(page);
855 for (vma = mm->mmap; vma; vma = vma->vm_next) {
856 if (vma->anon_vma && (ret = unuse_vma(vma, entry, page)))
857 break;
859 up_read(&mm->mmap_sem);
860 return (ret < 0)? ret: 0;
864 * Scan swap_map from current position to next entry still in use.
865 * Recycle to start on reaching the end, returning 0 when empty.
867 static unsigned int find_next_to_unuse(struct swap_info_struct *si,
868 unsigned int prev)
870 unsigned int max = si->max;
871 unsigned int i = prev;
872 int count;
875 * No need for swap_lock here: we're just looking
876 * for whether an entry is in use, not modifying it; false
877 * hits are okay, and sys_swapoff() has already prevented new
878 * allocations from this area (while holding swap_lock).
880 for (;;) {
881 if (++i >= max) {
882 if (!prev) {
883 i = 0;
884 break;
887 * No entries in use at top of swap_map,
888 * loop back to start and recheck there.
890 max = prev + 1;
891 prev = 0;
892 i = 1;
894 count = si->swap_map[i];
895 if (count && count != SWAP_MAP_BAD)
896 break;
898 return i;
902 * We completely avoid races by reading each swap page in advance,
903 * and then search for the process using it. All the necessary
904 * page table adjustments can then be made atomically.
906 static int try_to_unuse(unsigned int type)
908 struct swap_info_struct * si = &swap_info[type];
909 struct mm_struct *start_mm;
910 unsigned short *swap_map;
911 unsigned short swcount;
912 struct page *page;
913 swp_entry_t entry;
914 unsigned int i = 0;
915 int retval = 0;
916 int reset_overflow = 0;
917 int shmem;
920 * When searching mms for an entry, a good strategy is to
921 * start at the first mm we freed the previous entry from
922 * (though actually we don't notice whether we or coincidence
923 * freed the entry). Initialize this start_mm with a hold.
925 * A simpler strategy would be to start at the last mm we
926 * freed the previous entry from; but that would take less
927 * advantage of mmlist ordering, which clusters forked mms
928 * together, child after parent. If we race with dup_mmap(), we
929 * prefer to resolve parent before child, lest we miss entries
930 * duplicated after we scanned child: using last mm would invert
931 * that. Though it's only a serious concern when an overflowed
932 * swap count is reset from SWAP_MAP_MAX, preventing a rescan.
934 start_mm = &init_mm;
935 atomic_inc(&init_mm.mm_users);
938 * Keep on scanning until all entries have gone. Usually,
939 * one pass through swap_map is enough, but not necessarily:
940 * there are races when an instance of an entry might be missed.
942 while ((i = find_next_to_unuse(si, i)) != 0) {
943 if (signal_pending(current)) {
944 retval = -EINTR;
945 break;
949 * Get a page for the entry, using the existing swap
950 * cache page if there is one. Otherwise, get a clean
951 * page and read the swap into it.
953 swap_map = &si->swap_map[i];
954 entry = swp_entry(type, i);
955 page = read_swap_cache_async(entry,
956 GFP_HIGHUSER_MOVABLE, NULL, 0);
957 if (!page) {
959 * Either swap_duplicate() failed because entry
960 * has been freed independently, and will not be
961 * reused since sys_swapoff() already disabled
962 * allocation from here, or alloc_page() failed.
964 if (!*swap_map)
965 continue;
966 retval = -ENOMEM;
967 break;
971 * Don't hold on to start_mm if it looks like exiting.
973 if (atomic_read(&start_mm->mm_users) == 1) {
974 mmput(start_mm);
975 start_mm = &init_mm;
976 atomic_inc(&init_mm.mm_users);
980 * Wait for and lock page. When do_swap_page races with
981 * try_to_unuse, do_swap_page can handle the fault much
982 * faster than try_to_unuse can locate the entry. This
983 * apparently redundant "wait_on_page_locked" lets try_to_unuse
984 * defer to do_swap_page in such a case - in some tests,
985 * do_swap_page and try_to_unuse repeatedly compete.
987 wait_on_page_locked(page);
988 wait_on_page_writeback(page);
989 lock_page(page);
990 wait_on_page_writeback(page);
993 * Remove all references to entry.
994 * Whenever we reach init_mm, there's no address space
995 * to search, but use it as a reminder to search shmem.
997 shmem = 0;
998 swcount = *swap_map;
999 if (swcount > 1) {
1000 if (start_mm == &init_mm)
1001 shmem = shmem_unuse(entry, page);
1002 else
1003 retval = unuse_mm(start_mm, entry, page);
1005 if (*swap_map > 1) {
1006 int set_start_mm = (*swap_map >= swcount);
1007 struct list_head *p = &start_mm->mmlist;
1008 struct mm_struct *new_start_mm = start_mm;
1009 struct mm_struct *prev_mm = start_mm;
1010 struct mm_struct *mm;
1012 atomic_inc(&new_start_mm->mm_users);
1013 atomic_inc(&prev_mm->mm_users);
1014 spin_lock(&mmlist_lock);
1015 while (*swap_map > 1 && !retval && !shmem &&
1016 (p = p->next) != &start_mm->mmlist) {
1017 mm = list_entry(p, struct mm_struct, mmlist);
1018 if (!atomic_inc_not_zero(&mm->mm_users))
1019 continue;
1020 spin_unlock(&mmlist_lock);
1021 mmput(prev_mm);
1022 prev_mm = mm;
1024 cond_resched();
1026 swcount = *swap_map;
1027 if (swcount <= 1)
1029 else if (mm == &init_mm) {
1030 set_start_mm = 1;
1031 shmem = shmem_unuse(entry, page);
1032 } else
1033 retval = unuse_mm(mm, entry, page);
1034 if (set_start_mm && *swap_map < swcount) {
1035 mmput(new_start_mm);
1036 atomic_inc(&mm->mm_users);
1037 new_start_mm = mm;
1038 set_start_mm = 0;
1040 spin_lock(&mmlist_lock);
1042 spin_unlock(&mmlist_lock);
1043 mmput(prev_mm);
1044 mmput(start_mm);
1045 start_mm = new_start_mm;
1047 if (shmem) {
1048 /* page has already been unlocked and released */
1049 if (shmem > 0)
1050 continue;
1051 retval = shmem;
1052 break;
1054 if (retval) {
1055 unlock_page(page);
1056 page_cache_release(page);
1057 break;
1061 * How could swap count reach 0x7fff when the maximum
1062 * pid is 0x7fff, and there's no way to repeat a swap
1063 * page within an mm (except in shmem, where it's the
1064 * shared object which takes the reference count)?
1065 * We believe SWAP_MAP_MAX cannot occur in Linux 2.4.
1067 * If that's wrong, then we should worry more about
1068 * exit_mmap() and do_munmap() cases described above:
1069 * we might be resetting SWAP_MAP_MAX too early here.
1070 * We know "Undead"s can happen, they're okay, so don't
1071 * report them; but do report if we reset SWAP_MAP_MAX.
1073 if (*swap_map == SWAP_MAP_MAX) {
1074 spin_lock(&swap_lock);
1075 *swap_map = 1;
1076 spin_unlock(&swap_lock);
1077 reset_overflow = 1;
1081 * If a reference remains (rare), we would like to leave
1082 * the page in the swap cache; but try_to_unmap could
1083 * then re-duplicate the entry once we drop page lock,
1084 * so we might loop indefinitely; also, that page could
1085 * not be swapped out to other storage meanwhile. So:
1086 * delete from cache even if there's another reference,
1087 * after ensuring that the data has been saved to disk -
1088 * since if the reference remains (rarer), it will be
1089 * read from disk into another page. Splitting into two
1090 * pages would be incorrect if swap supported "shared
1091 * private" pages, but they are handled by tmpfs files.
1093 if ((*swap_map > 1) && PageDirty(page) && PageSwapCache(page)) {
1094 struct writeback_control wbc = {
1095 .sync_mode = WB_SYNC_NONE,
1098 swap_writepage(page, &wbc);
1099 lock_page(page);
1100 wait_on_page_writeback(page);
1104 * It is conceivable that a racing task removed this page from
1105 * swap cache just before we acquired the page lock at the top,
1106 * or while we dropped it in unuse_mm(). The page might even
1107 * be back in swap cache on another swap area: that we must not
1108 * delete, since it may not have been written out to swap yet.
1110 if (PageSwapCache(page) &&
1111 likely(page_private(page) == entry.val))
1112 delete_from_swap_cache(page);
1115 * So we could skip searching mms once swap count went
1116 * to 1, we did not mark any present ptes as dirty: must
1117 * mark page dirty so shrink_page_list will preserve it.
1119 SetPageDirty(page);
1120 unlock_page(page);
1121 page_cache_release(page);
1124 * Make sure that we aren't completely killing
1125 * interactive performance.
1127 cond_resched();
1130 mmput(start_mm);
1131 if (reset_overflow) {
1132 printk(KERN_WARNING "swapoff: cleared swap entry overflow\n");
1133 swap_overflow = 0;
1135 return retval;
1139 * After a successful try_to_unuse, if no swap is now in use, we know
1140 * we can empty the mmlist. swap_lock must be held on entry and exit.
1141 * Note that mmlist_lock nests inside swap_lock, and an mm must be
1142 * added to the mmlist just after page_duplicate - before would be racy.
1144 static void drain_mmlist(void)
1146 struct list_head *p, *next;
1147 unsigned int i;
1149 for (i = 0; i < nr_swapfiles; i++)
1150 if (swap_info[i].inuse_pages)
1151 return;
1152 spin_lock(&mmlist_lock);
1153 list_for_each_safe(p, next, &init_mm.mmlist)
1154 list_del_init(p);
1155 spin_unlock(&mmlist_lock);
1159 * Use this swapdev's extent info to locate the (PAGE_SIZE) block which
1160 * corresponds to page offset `offset'.
1162 sector_t map_swap_page(struct swap_info_struct *sis, pgoff_t offset)
1164 struct swap_extent *se = sis->curr_swap_extent;
1165 struct swap_extent *start_se = se;
1167 for ( ; ; ) {
1168 struct list_head *lh;
1170 if (se->start_page <= offset &&
1171 offset < (se->start_page + se->nr_pages)) {
1172 return se->start_block + (offset - se->start_page);
1174 lh = se->list.next;
1175 if (lh == &sis->extent_list)
1176 lh = lh->next;
1177 se = list_entry(lh, struct swap_extent, list);
1178 sis->curr_swap_extent = se;
1179 BUG_ON(se == start_se); /* It *must* be present */
1183 #ifdef CONFIG_HIBERNATION
1185 * Get the (PAGE_SIZE) block corresponding to given offset on the swapdev
1186 * corresponding to given index in swap_info (swap type).
1188 sector_t swapdev_block(int swap_type, pgoff_t offset)
1190 struct swap_info_struct *sis;
1192 if (swap_type >= nr_swapfiles)
1193 return 0;
1195 sis = swap_info + swap_type;
1196 return (sis->flags & SWP_WRITEOK) ? map_swap_page(sis, offset) : 0;
1198 #endif /* CONFIG_HIBERNATION */
1201 * Free all of a swapdev's extent information
1203 static void destroy_swap_extents(struct swap_info_struct *sis)
1205 while (!list_empty(&sis->extent_list)) {
1206 struct swap_extent *se;
1208 se = list_entry(sis->extent_list.next,
1209 struct swap_extent, list);
1210 list_del(&se->list);
1211 kfree(se);
1216 * Add a block range (and the corresponding page range) into this swapdev's
1217 * extent list. The extent list is kept sorted in page order.
1219 * This function rather assumes that it is called in ascending page order.
1221 static int
1222 add_swap_extent(struct swap_info_struct *sis, unsigned long start_page,
1223 unsigned long nr_pages, sector_t start_block)
1225 struct swap_extent *se;
1226 struct swap_extent *new_se;
1227 struct list_head *lh;
1229 lh = sis->extent_list.prev; /* The highest page extent */
1230 if (lh != &sis->extent_list) {
1231 se = list_entry(lh, struct swap_extent, list);
1232 BUG_ON(se->start_page + se->nr_pages != start_page);
1233 if (se->start_block + se->nr_pages == start_block) {
1234 /* Merge it */
1235 se->nr_pages += nr_pages;
1236 return 0;
1241 * No merge. Insert a new extent, preserving ordering.
1243 new_se = kmalloc(sizeof(*se), GFP_KERNEL);
1244 if (new_se == NULL)
1245 return -ENOMEM;
1246 new_se->start_page = start_page;
1247 new_se->nr_pages = nr_pages;
1248 new_se->start_block = start_block;
1250 list_add_tail(&new_se->list, &sis->extent_list);
1251 return 1;
1255 * A `swap extent' is a simple thing which maps a contiguous range of pages
1256 * onto a contiguous range of disk blocks. An ordered list of swap extents
1257 * is built at swapon time and is then used at swap_writepage/swap_readpage
1258 * time for locating where on disk a page belongs.
1260 * If the swapfile is an S_ISBLK block device, a single extent is installed.
1261 * This is done so that the main operating code can treat S_ISBLK and S_ISREG
1262 * swap files identically.
1264 * Whether the swapdev is an S_ISREG file or an S_ISBLK blockdev, the swap
1265 * extent list operates in PAGE_SIZE disk blocks. Both S_ISREG and S_ISBLK
1266 * swapfiles are handled *identically* after swapon time.
1268 * For S_ISREG swapfiles, setup_swap_extents() will walk all the file's blocks
1269 * and will parse them into an ordered extent list, in PAGE_SIZE chunks. If
1270 * some stray blocks are found which do not fall within the PAGE_SIZE alignment
1271 * requirements, they are simply tossed out - we will never use those blocks
1272 * for swapping.
1274 * For S_ISREG swapfiles we set S_SWAPFILE across the life of the swapon. This
1275 * prevents root from shooting her foot off by ftruncating an in-use swapfile,
1276 * which will scribble on the fs.
1278 * The amount of disk space which a single swap extent represents varies.
1279 * Typically it is in the 1-4 megabyte range. So we can have hundreds of
1280 * extents in the list. To avoid much list walking, we cache the previous
1281 * search location in `curr_swap_extent', and start new searches from there.
1282 * This is extremely effective. The average number of iterations in
1283 * map_swap_page() has been measured at about 0.3 per page. - akpm.
1285 static int setup_swap_extents(struct swap_info_struct *sis, sector_t *span)
1287 struct inode *inode;
1288 unsigned blocks_per_page;
1289 unsigned long page_no;
1290 unsigned blkbits;
1291 sector_t probe_block;
1292 sector_t last_block;
1293 sector_t lowest_block = -1;
1294 sector_t highest_block = 0;
1295 int nr_extents = 0;
1296 int ret;
1298 inode = sis->swap_file->f_mapping->host;
1299 if (S_ISBLK(inode->i_mode)) {
1300 ret = add_swap_extent(sis, 0, sis->max, 0);
1301 *span = sis->pages;
1302 goto done;
1305 blkbits = inode->i_blkbits;
1306 blocks_per_page = PAGE_SIZE >> blkbits;
1309 * Map all the blocks into the extent list. This code doesn't try
1310 * to be very smart.
1312 probe_block = 0;
1313 page_no = 0;
1314 last_block = i_size_read(inode) >> blkbits;
1315 while ((probe_block + blocks_per_page) <= last_block &&
1316 page_no < sis->max) {
1317 unsigned block_in_page;
1318 sector_t first_block;
1320 first_block = bmap(inode, probe_block);
1321 if (first_block == 0)
1322 goto bad_bmap;
1325 * It must be PAGE_SIZE aligned on-disk
1327 if (first_block & (blocks_per_page - 1)) {
1328 probe_block++;
1329 goto reprobe;
1332 for (block_in_page = 1; block_in_page < blocks_per_page;
1333 block_in_page++) {
1334 sector_t block;
1336 block = bmap(inode, probe_block + block_in_page);
1337 if (block == 0)
1338 goto bad_bmap;
1339 if (block != first_block + block_in_page) {
1340 /* Discontiguity */
1341 probe_block++;
1342 goto reprobe;
1346 first_block >>= (PAGE_SHIFT - blkbits);
1347 if (page_no) { /* exclude the header page */
1348 if (first_block < lowest_block)
1349 lowest_block = first_block;
1350 if (first_block > highest_block)
1351 highest_block = first_block;
1355 * We found a PAGE_SIZE-length, PAGE_SIZE-aligned run of blocks
1357 ret = add_swap_extent(sis, page_no, 1, first_block);
1358 if (ret < 0)
1359 goto out;
1360 nr_extents += ret;
1361 page_no++;
1362 probe_block += blocks_per_page;
1363 reprobe:
1364 continue;
1366 ret = nr_extents;
1367 *span = 1 + highest_block - lowest_block;
1368 if (page_no == 0)
1369 page_no = 1; /* force Empty message */
1370 sis->max = page_no;
1371 sis->pages = page_no - 1;
1372 sis->highest_bit = page_no - 1;
1373 done:
1374 sis->curr_swap_extent = list_entry(sis->extent_list.prev,
1375 struct swap_extent, list);
1376 goto out;
1377 bad_bmap:
1378 printk(KERN_ERR "swapon: swapfile has holes\n");
1379 ret = -EINVAL;
1380 out:
1381 return ret;
1384 SYSCALL_DEFINE1(swapoff, const char __user *, specialfile)
1386 struct swap_info_struct * p = NULL;
1387 unsigned short *swap_map;
1388 struct file *swap_file, *victim;
1389 struct address_space *mapping;
1390 struct inode *inode;
1391 char * pathname;
1392 int i, type, prev;
1393 int err;
1395 if (!capable(CAP_SYS_ADMIN))
1396 return -EPERM;
1398 pathname = getname(specialfile);
1399 err = PTR_ERR(pathname);
1400 if (IS_ERR(pathname))
1401 goto out;
1403 victim = filp_open(pathname, O_RDWR|O_LARGEFILE, 0);
1404 putname(pathname);
1405 err = PTR_ERR(victim);
1406 if (IS_ERR(victim))
1407 goto out;
1409 mapping = victim->f_mapping;
1410 prev = -1;
1411 spin_lock(&swap_lock);
1412 for (type = swap_list.head; type >= 0; type = swap_info[type].next) {
1413 p = swap_info + type;
1414 if (p->flags & SWP_WRITEOK) {
1415 if (p->swap_file->f_mapping == mapping)
1416 break;
1418 prev = type;
1420 if (type < 0) {
1421 err = -EINVAL;
1422 spin_unlock(&swap_lock);
1423 goto out_dput;
1425 if (!security_vm_enough_memory(p->pages))
1426 vm_unacct_memory(p->pages);
1427 else {
1428 err = -ENOMEM;
1429 spin_unlock(&swap_lock);
1430 goto out_dput;
1432 if (prev < 0) {
1433 swap_list.head = p->next;
1434 } else {
1435 swap_info[prev].next = p->next;
1437 if (type == swap_list.next) {
1438 /* just pick something that's safe... */
1439 swap_list.next = swap_list.head;
1441 if (p->prio < 0) {
1442 for (i = p->next; i >= 0; i = swap_info[i].next)
1443 swap_info[i].prio = p->prio--;
1444 least_priority++;
1446 nr_swap_pages -= p->pages;
1447 total_swap_pages -= p->pages;
1448 p->flags &= ~SWP_WRITEOK;
1449 spin_unlock(&swap_lock);
1451 current->flags |= PF_SWAPOFF;
1452 err = try_to_unuse(type);
1453 current->flags &= ~PF_SWAPOFF;
1455 if (err) {
1456 /* re-insert swap space back into swap_list */
1457 spin_lock(&swap_lock);
1458 if (p->prio < 0)
1459 p->prio = --least_priority;
1460 prev = -1;
1461 for (i = swap_list.head; i >= 0; i = swap_info[i].next) {
1462 if (p->prio >= swap_info[i].prio)
1463 break;
1464 prev = i;
1466 p->next = i;
1467 if (prev < 0)
1468 swap_list.head = swap_list.next = p - swap_info;
1469 else
1470 swap_info[prev].next = p - swap_info;
1471 nr_swap_pages += p->pages;
1472 total_swap_pages += p->pages;
1473 p->flags |= SWP_WRITEOK;
1474 spin_unlock(&swap_lock);
1475 goto out_dput;
1478 /* wait for any unplug function to finish */
1479 down_write(&swap_unplug_sem);
1480 up_write(&swap_unplug_sem);
1482 destroy_swap_extents(p);
1483 mutex_lock(&swapon_mutex);
1484 spin_lock(&swap_lock);
1485 drain_mmlist();
1487 /* wait for anyone still in scan_swap_map */
1488 p->highest_bit = 0; /* cuts scans short */
1489 while (p->flags >= SWP_SCANNING) {
1490 spin_unlock(&swap_lock);
1491 schedule_timeout_uninterruptible(1);
1492 spin_lock(&swap_lock);
1495 swap_file = p->swap_file;
1496 p->swap_file = NULL;
1497 p->max = 0;
1498 swap_map = p->swap_map;
1499 p->swap_map = NULL;
1500 p->flags = 0;
1501 spin_unlock(&swap_lock);
1502 mutex_unlock(&swapon_mutex);
1503 vfree(swap_map);
1504 /* Destroy swap account informatin */
1505 swap_cgroup_swapoff(type);
1507 inode = mapping->host;
1508 if (S_ISBLK(inode->i_mode)) {
1509 struct block_device *bdev = I_BDEV(inode);
1510 set_blocksize(bdev, p->old_block_size);
1511 bd_release(bdev);
1512 } else {
1513 mutex_lock(&inode->i_mutex);
1514 inode->i_flags &= ~S_SWAPFILE;
1515 mutex_unlock(&inode->i_mutex);
1517 filp_close(swap_file, NULL);
1518 err = 0;
1520 out_dput:
1521 filp_close(victim, NULL);
1522 out:
1523 return err;
1526 #ifdef CONFIG_PROC_FS
1527 /* iterator */
1528 static void *swap_start(struct seq_file *swap, loff_t *pos)
1530 struct swap_info_struct *ptr = swap_info;
1531 int i;
1532 loff_t l = *pos;
1534 mutex_lock(&swapon_mutex);
1536 if (!l)
1537 return SEQ_START_TOKEN;
1539 for (i = 0; i < nr_swapfiles; i++, ptr++) {
1540 if (!(ptr->flags & SWP_USED) || !ptr->swap_map)
1541 continue;
1542 if (!--l)
1543 return ptr;
1546 return NULL;
1549 static void *swap_next(struct seq_file *swap, void *v, loff_t *pos)
1551 struct swap_info_struct *ptr;
1552 struct swap_info_struct *endptr = swap_info + nr_swapfiles;
1554 if (v == SEQ_START_TOKEN)
1555 ptr = swap_info;
1556 else {
1557 ptr = v;
1558 ptr++;
1561 for (; ptr < endptr; ptr++) {
1562 if (!(ptr->flags & SWP_USED) || !ptr->swap_map)
1563 continue;
1564 ++*pos;
1565 return ptr;
1568 return NULL;
1571 static void swap_stop(struct seq_file *swap, void *v)
1573 mutex_unlock(&swapon_mutex);
1576 static int swap_show(struct seq_file *swap, void *v)
1578 struct swap_info_struct *ptr = v;
1579 struct file *file;
1580 int len;
1582 if (ptr == SEQ_START_TOKEN) {
1583 seq_puts(swap,"Filename\t\t\t\tType\t\tSize\tUsed\tPriority\n");
1584 return 0;
1587 file = ptr->swap_file;
1588 len = seq_path(swap, &file->f_path, " \t\n\\");
1589 seq_printf(swap, "%*s%s\t%u\t%u\t%d\n",
1590 len < 40 ? 40 - len : 1, " ",
1591 S_ISBLK(file->f_path.dentry->d_inode->i_mode) ?
1592 "partition" : "file\t",
1593 ptr->pages << (PAGE_SHIFT - 10),
1594 ptr->inuse_pages << (PAGE_SHIFT - 10),
1595 ptr->prio);
1596 return 0;
1599 static const struct seq_operations swaps_op = {
1600 .start = swap_start,
1601 .next = swap_next,
1602 .stop = swap_stop,
1603 .show = swap_show
1606 static int swaps_open(struct inode *inode, struct file *file)
1608 return seq_open(file, &swaps_op);
1611 static const struct file_operations proc_swaps_operations = {
1612 .open = swaps_open,
1613 .read = seq_read,
1614 .llseek = seq_lseek,
1615 .release = seq_release,
1618 static int __init procswaps_init(void)
1620 proc_create("swaps", 0, NULL, &proc_swaps_operations);
1621 return 0;
1623 __initcall(procswaps_init);
1624 #endif /* CONFIG_PROC_FS */
1626 #ifdef MAX_SWAPFILES_CHECK
1627 static int __init max_swapfiles_check(void)
1629 MAX_SWAPFILES_CHECK();
1630 return 0;
1632 late_initcall(max_swapfiles_check);
1633 #endif
1636 * Written 01/25/92 by Simmule Turner, heavily changed by Linus.
1638 * The swapon system call
1640 SYSCALL_DEFINE2(swapon, const char __user *, specialfile, int, swap_flags)
1642 struct swap_info_struct * p;
1643 char *name = NULL;
1644 struct block_device *bdev = NULL;
1645 struct file *swap_file = NULL;
1646 struct address_space *mapping;
1647 unsigned int type;
1648 int i, prev;
1649 int error;
1650 union swap_header *swap_header = NULL;
1651 unsigned int nr_good_pages = 0;
1652 int nr_extents = 0;
1653 sector_t uninitialized_var(span);
1654 unsigned long maxpages = 1;
1655 unsigned long swapfilepages;
1656 unsigned short *swap_map = NULL;
1657 struct page *page = NULL;
1658 struct inode *inode = NULL;
1659 int did_down = 0;
1661 if (!capable(CAP_SYS_ADMIN))
1662 return -EPERM;
1663 spin_lock(&swap_lock);
1664 p = swap_info;
1665 for (type = 0 ; type < nr_swapfiles ; type++,p++)
1666 if (!(p->flags & SWP_USED))
1667 break;
1668 error = -EPERM;
1669 if (type >= MAX_SWAPFILES) {
1670 spin_unlock(&swap_lock);
1671 goto out;
1673 if (type >= nr_swapfiles)
1674 nr_swapfiles = type+1;
1675 memset(p, 0, sizeof(*p));
1676 INIT_LIST_HEAD(&p->extent_list);
1677 p->flags = SWP_USED;
1678 p->next = -1;
1679 spin_unlock(&swap_lock);
1680 name = getname(specialfile);
1681 error = PTR_ERR(name);
1682 if (IS_ERR(name)) {
1683 name = NULL;
1684 goto bad_swap_2;
1686 swap_file = filp_open(name, O_RDWR|O_LARGEFILE, 0);
1687 error = PTR_ERR(swap_file);
1688 if (IS_ERR(swap_file)) {
1689 swap_file = NULL;
1690 goto bad_swap_2;
1693 p->swap_file = swap_file;
1694 mapping = swap_file->f_mapping;
1695 inode = mapping->host;
1697 error = -EBUSY;
1698 for (i = 0; i < nr_swapfiles; i++) {
1699 struct swap_info_struct *q = &swap_info[i];
1701 if (i == type || !q->swap_file)
1702 continue;
1703 if (mapping == q->swap_file->f_mapping)
1704 goto bad_swap;
1707 error = -EINVAL;
1708 if (S_ISBLK(inode->i_mode)) {
1709 bdev = I_BDEV(inode);
1710 error = bd_claim(bdev, sys_swapon);
1711 if (error < 0) {
1712 bdev = NULL;
1713 error = -EINVAL;
1714 goto bad_swap;
1716 p->old_block_size = block_size(bdev);
1717 error = set_blocksize(bdev, PAGE_SIZE);
1718 if (error < 0)
1719 goto bad_swap;
1720 p->bdev = bdev;
1721 } else if (S_ISREG(inode->i_mode)) {
1722 p->bdev = inode->i_sb->s_bdev;
1723 mutex_lock(&inode->i_mutex);
1724 did_down = 1;
1725 if (IS_SWAPFILE(inode)) {
1726 error = -EBUSY;
1727 goto bad_swap;
1729 } else {
1730 goto bad_swap;
1733 swapfilepages = i_size_read(inode) >> PAGE_SHIFT;
1736 * Read the swap header.
1738 if (!mapping->a_ops->readpage) {
1739 error = -EINVAL;
1740 goto bad_swap;
1742 page = read_mapping_page(mapping, 0, swap_file);
1743 if (IS_ERR(page)) {
1744 error = PTR_ERR(page);
1745 goto bad_swap;
1747 swap_header = kmap(page);
1749 if (memcmp("SWAPSPACE2", swap_header->magic.magic, 10)) {
1750 printk(KERN_ERR "Unable to find swap-space signature\n");
1751 error = -EINVAL;
1752 goto bad_swap;
1755 /* swap partition endianess hack... */
1756 if (swab32(swap_header->info.version) == 1) {
1757 swab32s(&swap_header->info.version);
1758 swab32s(&swap_header->info.last_page);
1759 swab32s(&swap_header->info.nr_badpages);
1760 for (i = 0; i < swap_header->info.nr_badpages; i++)
1761 swab32s(&swap_header->info.badpages[i]);
1763 /* Check the swap header's sub-version */
1764 if (swap_header->info.version != 1) {
1765 printk(KERN_WARNING
1766 "Unable to handle swap header version %d\n",
1767 swap_header->info.version);
1768 error = -EINVAL;
1769 goto bad_swap;
1772 p->lowest_bit = 1;
1773 p->cluster_next = 1;
1776 * Find out how many pages are allowed for a single swap
1777 * device. There are two limiting factors: 1) the number of
1778 * bits for the swap offset in the swp_entry_t type and
1779 * 2) the number of bits in the a swap pte as defined by
1780 * the different architectures. In order to find the
1781 * largest possible bit mask a swap entry with swap type 0
1782 * and swap offset ~0UL is created, encoded to a swap pte,
1783 * decoded to a swp_entry_t again and finally the swap
1784 * offset is extracted. This will mask all the bits from
1785 * the initial ~0UL mask that can't be encoded in either
1786 * the swp_entry_t or the architecture definition of a
1787 * swap pte.
1789 maxpages = swp_offset(pte_to_swp_entry(
1790 swp_entry_to_pte(swp_entry(0, ~0UL)))) - 1;
1791 if (maxpages > swap_header->info.last_page)
1792 maxpages = swap_header->info.last_page;
1793 p->highest_bit = maxpages - 1;
1795 error = -EINVAL;
1796 if (!maxpages)
1797 goto bad_swap;
1798 if (swapfilepages && maxpages > swapfilepages) {
1799 printk(KERN_WARNING
1800 "Swap area shorter than signature indicates\n");
1801 goto bad_swap;
1803 if (swap_header->info.nr_badpages && S_ISREG(inode->i_mode))
1804 goto bad_swap;
1805 if (swap_header->info.nr_badpages > MAX_SWAP_BADPAGES)
1806 goto bad_swap;
1808 /* OK, set up the swap map and apply the bad block list */
1809 swap_map = vmalloc(maxpages * sizeof(short));
1810 if (!swap_map) {
1811 error = -ENOMEM;
1812 goto bad_swap;
1815 memset(swap_map, 0, maxpages * sizeof(short));
1816 for (i = 0; i < swap_header->info.nr_badpages; i++) {
1817 int page_nr = swap_header->info.badpages[i];
1818 if (page_nr <= 0 || page_nr >= swap_header->info.last_page) {
1819 error = -EINVAL;
1820 goto bad_swap;
1822 swap_map[page_nr] = SWAP_MAP_BAD;
1825 error = swap_cgroup_swapon(type, maxpages);
1826 if (error)
1827 goto bad_swap;
1829 nr_good_pages = swap_header->info.last_page -
1830 swap_header->info.nr_badpages -
1831 1 /* header page */;
1833 if (nr_good_pages) {
1834 swap_map[0] = SWAP_MAP_BAD;
1835 p->max = maxpages;
1836 p->pages = nr_good_pages;
1837 nr_extents = setup_swap_extents(p, &span);
1838 if (nr_extents < 0) {
1839 error = nr_extents;
1840 goto bad_swap;
1842 nr_good_pages = p->pages;
1844 if (!nr_good_pages) {
1845 printk(KERN_WARNING "Empty swap-file\n");
1846 error = -EINVAL;
1847 goto bad_swap;
1850 if (blk_queue_nonrot(bdev_get_queue(p->bdev))) {
1851 p->flags |= SWP_SOLIDSTATE;
1852 p->cluster_next = 1 + (random32() % p->highest_bit);
1854 if (discard_swap(p) == 0)
1855 p->flags |= SWP_DISCARDABLE;
1857 mutex_lock(&swapon_mutex);
1858 spin_lock(&swap_lock);
1859 if (swap_flags & SWAP_FLAG_PREFER)
1860 p->prio =
1861 (swap_flags & SWAP_FLAG_PRIO_MASK) >> SWAP_FLAG_PRIO_SHIFT;
1862 else
1863 p->prio = --least_priority;
1864 p->swap_map = swap_map;
1865 p->flags |= SWP_WRITEOK;
1866 nr_swap_pages += nr_good_pages;
1867 total_swap_pages += nr_good_pages;
1869 printk(KERN_INFO "Adding %uk swap on %s. "
1870 "Priority:%d extents:%d across:%lluk %s%s\n",
1871 nr_good_pages<<(PAGE_SHIFT-10), name, p->prio,
1872 nr_extents, (unsigned long long)span<<(PAGE_SHIFT-10),
1873 (p->flags & SWP_SOLIDSTATE) ? "SS" : "",
1874 (p->flags & SWP_DISCARDABLE) ? "D" : "");
1876 /* insert swap space into swap_list: */
1877 prev = -1;
1878 for (i = swap_list.head; i >= 0; i = swap_info[i].next) {
1879 if (p->prio >= swap_info[i].prio) {
1880 break;
1882 prev = i;
1884 p->next = i;
1885 if (prev < 0) {
1886 swap_list.head = swap_list.next = p - swap_info;
1887 } else {
1888 swap_info[prev].next = p - swap_info;
1890 spin_unlock(&swap_lock);
1891 mutex_unlock(&swapon_mutex);
1892 error = 0;
1893 goto out;
1894 bad_swap:
1895 if (bdev) {
1896 set_blocksize(bdev, p->old_block_size);
1897 bd_release(bdev);
1899 destroy_swap_extents(p);
1900 swap_cgroup_swapoff(type);
1901 bad_swap_2:
1902 spin_lock(&swap_lock);
1903 p->swap_file = NULL;
1904 p->flags = 0;
1905 spin_unlock(&swap_lock);
1906 vfree(swap_map);
1907 if (swap_file)
1908 filp_close(swap_file, NULL);
1909 out:
1910 if (page && !IS_ERR(page)) {
1911 kunmap(page);
1912 page_cache_release(page);
1914 if (name)
1915 putname(name);
1916 if (did_down) {
1917 if (!error)
1918 inode->i_flags |= S_SWAPFILE;
1919 mutex_unlock(&inode->i_mutex);
1921 return error;
1924 void si_swapinfo(struct sysinfo *val)
1926 unsigned int i;
1927 unsigned long nr_to_be_unused = 0;
1929 spin_lock(&swap_lock);
1930 for (i = 0; i < nr_swapfiles; i++) {
1931 if (!(swap_info[i].flags & SWP_USED) ||
1932 (swap_info[i].flags & SWP_WRITEOK))
1933 continue;
1934 nr_to_be_unused += swap_info[i].inuse_pages;
1936 val->freeswap = nr_swap_pages + nr_to_be_unused;
1937 val->totalswap = total_swap_pages + nr_to_be_unused;
1938 spin_unlock(&swap_lock);
1942 * Verify that a swap entry is valid and increment its swap map count.
1944 * Note: if swap_map[] reaches SWAP_MAP_MAX the entries are treated as
1945 * "permanent", but will be reclaimed by the next swapoff.
1947 int swap_duplicate(swp_entry_t entry)
1949 struct swap_info_struct * p;
1950 unsigned long offset, type;
1951 int result = 0;
1953 if (is_migration_entry(entry))
1954 return 1;
1956 type = swp_type(entry);
1957 if (type >= nr_swapfiles)
1958 goto bad_file;
1959 p = type + swap_info;
1960 offset = swp_offset(entry);
1962 spin_lock(&swap_lock);
1963 if (offset < p->max && p->swap_map[offset]) {
1964 if (p->swap_map[offset] < SWAP_MAP_MAX - 1) {
1965 p->swap_map[offset]++;
1966 result = 1;
1967 } else if (p->swap_map[offset] <= SWAP_MAP_MAX) {
1968 if (swap_overflow++ < 5)
1969 printk(KERN_WARNING "swap_dup: swap entry overflow\n");
1970 p->swap_map[offset] = SWAP_MAP_MAX;
1971 result = 1;
1974 spin_unlock(&swap_lock);
1975 out:
1976 return result;
1978 bad_file:
1979 printk(KERN_ERR "swap_dup: %s%08lx\n", Bad_file, entry.val);
1980 goto out;
1983 struct swap_info_struct *
1984 get_swap_info_struct(unsigned type)
1986 return &swap_info[type];
1990 * swap_lock prevents swap_map being freed. Don't grab an extra
1991 * reference on the swaphandle, it doesn't matter if it becomes unused.
1993 int valid_swaphandles(swp_entry_t entry, unsigned long *offset)
1995 struct swap_info_struct *si;
1996 int our_page_cluster = page_cluster;
1997 pgoff_t target, toff;
1998 pgoff_t base, end;
1999 int nr_pages = 0;
2001 if (!our_page_cluster) /* no readahead */
2002 return 0;
2004 si = &swap_info[swp_type(entry)];
2005 target = swp_offset(entry);
2006 base = (target >> our_page_cluster) << our_page_cluster;
2007 end = base + (1 << our_page_cluster);
2008 if (!base) /* first page is swap header */
2009 base++;
2011 spin_lock(&swap_lock);
2012 if (end > si->max) /* don't go beyond end of map */
2013 end = si->max;
2015 /* Count contiguous allocated slots above our target */
2016 for (toff = target; ++toff < end; nr_pages++) {
2017 /* Don't read in free or bad pages */
2018 if (!si->swap_map[toff])
2019 break;
2020 if (si->swap_map[toff] == SWAP_MAP_BAD)
2021 break;
2023 /* Count contiguous allocated slots below our target */
2024 for (toff = target; --toff >= base; nr_pages++) {
2025 /* Don't read in free or bad pages */
2026 if (!si->swap_map[toff])
2027 break;
2028 if (si->swap_map[toff] == SWAP_MAP_BAD)
2029 break;
2031 spin_unlock(&swap_lock);
2034 * Indicate starting offset, and return number of pages to get:
2035 * if only 1, say 0, since there's then no readahead to be done.
2037 *offset = ++toff;
2038 return nr_pages? ++nr_pages: 0;