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