Cleanup syscall code to look more like it's mips64 equivalent.
[linux-2.6/linux-mips.git] / mm / swapfile.c
blobe3203366686a50bddee0f0afe3b883642185b5d7
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/config.h>
9 #include <linux/mm.h>
10 #include <linux/mman.h>
11 #include <linux/slab.h>
12 #include <linux/kernel_stat.h>
13 #include <linux/swap.h>
14 #include <linux/vmalloc.h>
15 #include <linux/pagemap.h>
16 #include <linux/namei.h>
17 #include <linux/shm.h>
18 #include <linux/blkdev.h>
19 #include <linux/writeback.h>
20 #include <linux/proc_fs.h>
21 #include <linux/seq_file.h>
22 #include <linux/init.h>
23 #include <linux/module.h>
24 #include <linux/rmap-locking.h>
25 #include <linux/security.h>
27 #include <asm/pgtable.h>
28 #include <linux/swapops.h>
30 spinlock_t swaplock = SPIN_LOCK_UNLOCKED;
31 unsigned int nr_swapfiles;
32 int total_swap_pages;
33 static int swap_overflow;
35 EXPORT_SYMBOL(total_swap_pages);
37 static const char Bad_file[] = "Bad swap file entry ";
38 static const char Unused_file[] = "Unused swap file entry ";
39 static const char Bad_offset[] = "Bad swap offset entry ";
40 static const char Unused_offset[] = "Unused swap offset entry ";
42 struct swap_list_t swap_list = {-1, -1};
44 struct swap_info_struct swap_info[MAX_SWAPFILES];
46 #define SWAPFILE_CLUSTER 256
48 static inline int scan_swap_map(struct swap_info_struct *si)
50 unsigned long offset;
51 /*
52 * We try to cluster swap pages by allocating them
53 * sequentially in swap. Once we've allocated
54 * SWAPFILE_CLUSTER pages this way, however, we resort to
55 * first-free allocation, starting a new cluster. This
56 * prevents us from scattering swap pages all over the entire
57 * swap partition, so that we reduce overall disk seek times
58 * between swap pages. -- sct */
59 if (si->cluster_nr) {
60 while (si->cluster_next <= si->highest_bit) {
61 offset = si->cluster_next++;
62 if (si->swap_map[offset])
63 continue;
64 si->cluster_nr--;
65 goto got_page;
68 si->cluster_nr = SWAPFILE_CLUSTER;
70 /* try to find an empty (even not aligned) cluster. */
71 offset = si->lowest_bit;
72 check_next_cluster:
73 if (offset+SWAPFILE_CLUSTER-1 <= si->highest_bit)
75 int nr;
76 for (nr = offset; nr < offset+SWAPFILE_CLUSTER; nr++)
77 if (si->swap_map[nr])
79 offset = nr+1;
80 goto check_next_cluster;
82 /* We found a completly empty cluster, so start
83 * using it.
85 goto got_page;
87 /* No luck, so now go finegrined as usual. -Andrea */
88 for (offset = si->lowest_bit; offset <= si->highest_bit ; offset++) {
89 if (si->swap_map[offset])
90 continue;
91 si->lowest_bit = offset+1;
92 got_page:
93 if (offset == si->lowest_bit)
94 si->lowest_bit++;
95 if (offset == si->highest_bit)
96 si->highest_bit--;
97 if (si->lowest_bit > si->highest_bit) {
98 si->lowest_bit = si->max;
99 si->highest_bit = 0;
101 si->swap_map[offset] = 1;
102 si->inuse_pages++;
103 nr_swap_pages--;
104 si->cluster_next = offset+1;
105 return offset;
107 si->lowest_bit = si->max;
108 si->highest_bit = 0;
109 return 0;
112 swp_entry_t get_swap_page(void)
114 struct swap_info_struct * p;
115 unsigned long offset;
116 swp_entry_t entry;
117 int type, wrapped = 0;
119 entry.val = 0; /* Out of memory */
120 swap_list_lock();
121 type = swap_list.next;
122 if (type < 0)
123 goto out;
124 if (nr_swap_pages <= 0)
125 goto out;
127 while (1) {
128 p = &swap_info[type];
129 if ((p->flags & SWP_ACTIVE) == SWP_ACTIVE) {
130 swap_device_lock(p);
131 offset = scan_swap_map(p);
132 swap_device_unlock(p);
133 if (offset) {
134 entry = swp_entry(type,offset);
135 type = swap_info[type].next;
136 if (type < 0 ||
137 p->prio != swap_info[type].prio) {
138 swap_list.next = swap_list.head;
139 } else {
140 swap_list.next = type;
142 goto out;
145 type = p->next;
146 if (!wrapped) {
147 if (type < 0 || p->prio != swap_info[type].prio) {
148 type = swap_list.head;
149 wrapped = 1;
151 } else
152 if (type < 0)
153 goto out; /* out of swap space */
155 out:
156 swap_list_unlock();
157 return entry;
160 static struct swap_info_struct * swap_info_get(swp_entry_t entry)
162 struct swap_info_struct * p;
163 unsigned long offset, type;
165 if (!entry.val)
166 goto out;
167 type = swp_type(entry);
168 if (type >= nr_swapfiles)
169 goto bad_nofile;
170 p = & swap_info[type];
171 if (!(p->flags & SWP_USED))
172 goto bad_device;
173 offset = swp_offset(entry);
174 if (offset >= p->max)
175 goto bad_offset;
176 if (!p->swap_map[offset])
177 goto bad_free;
178 swap_list_lock();
179 if (p->prio > swap_info[swap_list.next].prio)
180 swap_list.next = type;
181 swap_device_lock(p);
182 return p;
184 bad_free:
185 printk(KERN_ERR "swap_free: %s%08lx\n", Unused_offset, entry.val);
186 goto out;
187 bad_offset:
188 printk(KERN_ERR "swap_free: %s%08lx\n", Bad_offset, entry.val);
189 goto out;
190 bad_device:
191 printk(KERN_ERR "swap_free: %s%08lx\n", Unused_file, entry.val);
192 goto out;
193 bad_nofile:
194 printk(KERN_ERR "swap_free: %s%08lx\n", Bad_file, entry.val);
195 out:
196 return NULL;
199 static void swap_info_put(struct swap_info_struct * p)
201 swap_device_unlock(p);
202 swap_list_unlock();
205 static int swap_entry_free(struct swap_info_struct *p, unsigned long offset)
207 int count = p->swap_map[offset];
209 if (count < SWAP_MAP_MAX) {
210 count--;
211 p->swap_map[offset] = count;
212 if (!count) {
213 if (offset < p->lowest_bit)
214 p->lowest_bit = offset;
215 if (offset > p->highest_bit)
216 p->highest_bit = offset;
217 nr_swap_pages++;
218 p->inuse_pages--;
221 return count;
225 * Caller has made sure that the swapdevice corresponding to entry
226 * is still around or has not been recycled.
228 void swap_free(swp_entry_t entry)
230 struct swap_info_struct * p;
232 p = swap_info_get(entry);
233 if (p) {
234 swap_entry_free(p, swp_offset(entry));
235 swap_info_put(p);
240 * Check if we're the only user of a swap page,
241 * when the page is locked.
243 static int exclusive_swap_page(struct page *page)
245 int retval = 0;
246 struct swap_info_struct * p;
247 swp_entry_t entry;
249 entry.val = page->index;
250 p = swap_info_get(entry);
251 if (p) {
252 /* Is the only swap cache user the cache itself? */
253 if (p->swap_map[swp_offset(entry)] == 1) {
254 /* Recheck the page count with the pagecache lock held.. */
255 spin_lock(&swapper_space.page_lock);
256 if (page_count(page) - !!PagePrivate(page) == 2)
257 retval = 1;
258 spin_unlock(&swapper_space.page_lock);
260 swap_info_put(p);
262 return retval;
266 * We can use this swap cache entry directly
267 * if there are no other references to it.
269 * Here "exclusive_swap_page()" does the real
270 * work, but we opportunistically check whether
271 * we need to get all the locks first..
273 int can_share_swap_page(struct page *page)
275 int retval = 0;
277 if (!PageLocked(page))
278 BUG();
279 switch (page_count(page)) {
280 case 3:
281 if (!PagePrivate(page))
282 break;
283 /* Fallthrough */
284 case 2:
285 if (!PageSwapCache(page))
286 break;
287 retval = exclusive_swap_page(page);
288 break;
289 case 1:
290 if (PageReserved(page))
291 break;
292 retval = 1;
294 return retval;
298 * Work out if there are any other processes sharing this
299 * swap cache page. Free it if you can. Return success.
301 int remove_exclusive_swap_page(struct page *page)
303 int retval;
304 struct swap_info_struct * p;
305 swp_entry_t entry;
307 BUG_ON(PagePrivate(page));
308 BUG_ON(!PageLocked(page));
310 if (!PageSwapCache(page))
311 return 0;
312 if (PageWriteback(page))
313 return 0;
314 if (page_count(page) != 2) /* 2: us + cache */
315 return 0;
317 entry.val = page->index;
318 p = swap_info_get(entry);
319 if (!p)
320 return 0;
322 /* Is the only swap cache user the cache itself? */
323 retval = 0;
324 if (p->swap_map[swp_offset(entry)] == 1) {
325 /* Recheck the page count with the pagecache lock held.. */
326 spin_lock(&swapper_space.page_lock);
327 if ((page_count(page) == 2) && !PageWriteback(page)) {
328 __delete_from_swap_cache(page);
329 SetPageDirty(page);
330 retval = 1;
332 spin_unlock(&swapper_space.page_lock);
334 swap_info_put(p);
336 if (retval) {
337 swap_free(entry);
338 page_cache_release(page);
341 return retval;
345 * Free the swap entry like above, but also try to
346 * free the page cache entry if it is the last user.
348 void free_swap_and_cache(swp_entry_t entry)
350 struct swap_info_struct * p;
351 struct page *page = NULL;
353 p = swap_info_get(entry);
354 if (p) {
355 if (swap_entry_free(p, swp_offset(entry)) == 1)
356 page = find_trylock_page(&swapper_space, entry.val);
357 swap_info_put(p);
359 if (page) {
360 int one_user;
362 BUG_ON(PagePrivate(page));
363 page_cache_get(page);
364 one_user = (page_count(page) == 2);
365 /* Only cache user (+us), or swap space full? Free it! */
366 if (!PageWriteback(page) && (one_user || vm_swap_full())) {
367 delete_from_swap_cache(page);
368 SetPageDirty(page);
370 unlock_page(page);
371 page_cache_release(page);
376 * The swap entry has been read in advance, and we return 1 to indicate
377 * that the page has been used or is no longer needed.
379 * Always set the resulting pte to be nowrite (the same as COW pages
380 * after one process has exited). We don't know just how many PTEs will
381 * share this swap entry, so be cautious and let do_wp_page work out
382 * what to do if a write is requested later.
384 /* vma->vm_mm->page_table_lock is held */
385 static void
386 unuse_pte(struct vm_area_struct *vma, unsigned long address, pte_t *dir,
387 swp_entry_t entry, struct page *page, struct pte_chain **pte_chainp)
389 vma->vm_mm->rss++;
390 get_page(page);
391 set_pte(dir, pte_mkold(mk_pte(page, vma->vm_page_prot)));
392 *pte_chainp = page_add_rmap(page, dir, *pte_chainp);
393 swap_free(entry);
396 /* vma->vm_mm->page_table_lock is held */
397 static int unuse_pmd(struct vm_area_struct * vma, pmd_t *dir,
398 unsigned long address, unsigned long size, unsigned long offset,
399 swp_entry_t entry, struct page *page, struct pte_chain **pte_chainp)
401 pte_t * pte;
402 unsigned long end;
403 pte_t swp_pte = swp_entry_to_pte(entry);
405 if (pmd_none(*dir))
406 return 0;
407 if (pmd_bad(*dir)) {
408 pmd_ERROR(*dir);
409 pmd_clear(dir);
410 return 0;
412 pte = pte_offset_map(dir, address);
413 offset += address & PMD_MASK;
414 address &= ~PMD_MASK;
415 end = address + size;
416 if (end > PMD_SIZE)
417 end = PMD_SIZE;
418 do {
420 * swapoff spends a _lot_ of time in this loop!
421 * Test inline before going to call unuse_pte.
423 if (unlikely(pte_same(*pte, swp_pte))) {
424 unuse_pte(vma, offset + address, pte,
425 entry, page, pte_chainp);
426 pte_unmap(pte);
427 return 1;
429 address += PAGE_SIZE;
430 pte++;
431 } while (address && (address < end));
432 pte_unmap(pte - 1);
433 return 0;
436 /* vma->vm_mm->page_table_lock is held */
437 static int unuse_pgd(struct vm_area_struct * vma, pgd_t *dir,
438 unsigned long address, unsigned long size,
439 swp_entry_t entry, struct page *page, struct pte_chain **pte_chainp)
441 pmd_t * pmd;
442 unsigned long offset, end;
444 if (pgd_none(*dir))
445 return 0;
446 if (pgd_bad(*dir)) {
447 pgd_ERROR(*dir);
448 pgd_clear(dir);
449 return 0;
451 pmd = pmd_offset(dir, address);
452 offset = address & PGDIR_MASK;
453 address &= ~PGDIR_MASK;
454 end = address + size;
455 if (end > PGDIR_SIZE)
456 end = PGDIR_SIZE;
457 if (address >= end)
458 BUG();
459 do {
460 if (unuse_pmd(vma, pmd, address, end - address,
461 offset, entry, page, pte_chainp))
462 return 1;
463 address = (address + PMD_SIZE) & PMD_MASK;
464 pmd++;
465 } while (address && (address < end));
466 return 0;
469 /* vma->vm_mm->page_table_lock is held */
470 static int unuse_vma(struct vm_area_struct * vma, pgd_t *pgdir,
471 swp_entry_t entry, struct page *page, struct pte_chain **pte_chainp)
473 unsigned long start = vma->vm_start, end = vma->vm_end;
475 if (start >= end)
476 BUG();
477 do {
478 if (unuse_pgd(vma, pgdir, start, end - start,
479 entry, page, pte_chainp))
480 return 1;
481 start = (start + PGDIR_SIZE) & PGDIR_MASK;
482 pgdir++;
483 } while (start && (start < end));
484 return 0;
487 static int unuse_process(struct mm_struct * mm,
488 swp_entry_t entry, struct page* page)
490 struct vm_area_struct* vma;
491 struct pte_chain *pte_chain;
493 pte_chain = pte_chain_alloc(GFP_KERNEL);
494 if (!pte_chain)
495 return -ENOMEM;
498 * Go through process' page directory.
500 spin_lock(&mm->page_table_lock);
501 for (vma = mm->mmap; vma; vma = vma->vm_next) {
502 pgd_t * pgd = pgd_offset(mm, vma->vm_start);
503 if (unuse_vma(vma, pgd, entry, page, &pte_chain))
504 break;
506 spin_unlock(&mm->page_table_lock);
507 pte_chain_free(pte_chain);
508 return 0;
512 * Scan swap_map from current position to next entry still in use.
513 * Recycle to start on reaching the end, returning 0 when empty.
515 static int find_next_to_unuse(struct swap_info_struct *si, int prev)
517 int max = si->max;
518 int i = prev;
519 int count;
522 * No need for swap_device_lock(si) here: we're just looking
523 * for whether an entry is in use, not modifying it; false
524 * hits are okay, and sys_swapoff() has already prevented new
525 * allocations from this area (while holding swap_list_lock()).
527 for (;;) {
528 if (++i >= max) {
529 if (!prev) {
530 i = 0;
531 break;
534 * No entries in use at top of swap_map,
535 * loop back to start and recheck there.
537 max = prev + 1;
538 prev = 0;
539 i = 1;
541 count = si->swap_map[i];
542 if (count && count != SWAP_MAP_BAD)
543 break;
545 return i;
549 * We completely avoid races by reading each swap page in advance,
550 * and then search for the process using it. All the necessary
551 * page table adjustments can then be made atomically.
553 static int try_to_unuse(unsigned int type)
555 struct swap_info_struct * si = &swap_info[type];
556 struct mm_struct *start_mm;
557 unsigned short *swap_map;
558 unsigned short swcount;
559 struct page *page;
560 swp_entry_t entry;
561 int i = 0;
562 int retval = 0;
563 int reset_overflow = 0;
564 int shmem;
567 * When searching mms for an entry, a good strategy is to
568 * start at the first mm we freed the previous entry from
569 * (though actually we don't notice whether we or coincidence
570 * freed the entry). Initialize this start_mm with a hold.
572 * A simpler strategy would be to start at the last mm we
573 * freed the previous entry from; but that would take less
574 * advantage of mmlist ordering (now preserved by swap_out()),
575 * which clusters forked address spaces together, most recent
576 * child immediately after parent. If we race with dup_mmap(),
577 * we very much want to resolve parent before child, otherwise
578 * we may miss some entries: using last mm would invert that.
580 start_mm = &init_mm;
581 atomic_inc(&init_mm.mm_users);
584 * Keep on scanning until all entries have gone. Usually,
585 * one pass through swap_map is enough, but not necessarily:
586 * mmput() removes mm from mmlist before exit_mmap() and its
587 * zap_page_range(). That's not too bad, those entries are
588 * on their way out, and handled faster there than here.
589 * do_munmap() behaves similarly, taking the range out of mm's
590 * vma list before zap_page_range(). But unfortunately, when
591 * unmapping a part of a vma, it takes the whole out first,
592 * then reinserts what's left after (might even reschedule if
593 * open() method called) - so swap entries may be invisible
594 * to swapoff for a while, then reappear - but that is rare.
596 while ((i = find_next_to_unuse(si, i))) {
597 if (signal_pending(current)) {
598 retval = -EINTR;
599 break;
603 * Get a page for the entry, using the existing swap
604 * cache page if there is one. Otherwise, get a clean
605 * page and read the swap into it.
607 swap_map = &si->swap_map[i];
608 entry = swp_entry(type, i);
609 page = read_swap_cache_async(entry);
610 if (!page) {
612 * Either swap_duplicate() failed because entry
613 * has been freed independently, and will not be
614 * reused since sys_swapoff() already disabled
615 * allocation from here, or alloc_page() failed.
617 if (!*swap_map)
618 continue;
619 retval = -ENOMEM;
620 break;
624 * Don't hold on to start_mm if it looks like exiting.
626 if (atomic_read(&start_mm->mm_users) == 1) {
627 mmput(start_mm);
628 start_mm = &init_mm;
629 atomic_inc(&init_mm.mm_users);
633 * Wait for and lock page. When do_swap_page races with
634 * try_to_unuse, do_swap_page can handle the fault much
635 * faster than try_to_unuse can locate the entry. This
636 * apparently redundant "wait_on_page_locked" lets try_to_unuse
637 * defer to do_swap_page in such a case - in some tests,
638 * do_swap_page and try_to_unuse repeatedly compete.
640 wait_on_page_locked(page);
641 wait_on_page_writeback(page);
642 lock_page(page);
643 wait_on_page_writeback(page);
646 * Remove all references to entry, without blocking.
647 * Whenever we reach init_mm, there's no address space
648 * to search, but use it as a reminder to search shmem.
650 shmem = 0;
651 swcount = *swap_map;
652 if (swcount > 1) {
653 if (start_mm == &init_mm)
654 shmem = shmem_unuse(entry, page);
655 else
656 retval = unuse_process(start_mm, entry, page);
658 if (*swap_map > 1) {
659 int set_start_mm = (*swap_map >= swcount);
660 struct list_head *p = &start_mm->mmlist;
661 struct mm_struct *new_start_mm = start_mm;
662 struct mm_struct *prev_mm = start_mm;
663 struct mm_struct *mm;
665 atomic_inc(&new_start_mm->mm_users);
666 atomic_inc(&prev_mm->mm_users);
667 spin_lock(&mmlist_lock);
668 while (*swap_map > 1 && !retval &&
669 (p = p->next) != &start_mm->mmlist) {
670 mm = list_entry(p, struct mm_struct, mmlist);
671 atomic_inc(&mm->mm_users);
672 spin_unlock(&mmlist_lock);
673 mmput(prev_mm);
674 prev_mm = mm;
676 cond_resched();
678 swcount = *swap_map;
679 if (swcount <= 1)
681 else if (mm == &init_mm) {
682 set_start_mm = 1;
683 shmem = shmem_unuse(entry, page);
684 } else
685 retval = unuse_process(mm, entry, page);
686 if (set_start_mm && *swap_map < swcount) {
687 mmput(new_start_mm);
688 atomic_inc(&mm->mm_users);
689 new_start_mm = mm;
690 set_start_mm = 0;
692 spin_lock(&mmlist_lock);
694 spin_unlock(&mmlist_lock);
695 mmput(prev_mm);
696 mmput(start_mm);
697 start_mm = new_start_mm;
699 if (retval) {
700 unlock_page(page);
701 page_cache_release(page);
702 break;
706 * How could swap count reach 0x7fff when the maximum
707 * pid is 0x7fff, and there's no way to repeat a swap
708 * page within an mm (except in shmem, where it's the
709 * shared object which takes the reference count)?
710 * We believe SWAP_MAP_MAX cannot occur in Linux 2.4.
712 * If that's wrong, then we should worry more about
713 * exit_mmap() and do_munmap() cases described above:
714 * we might be resetting SWAP_MAP_MAX too early here.
715 * We know "Undead"s can happen, they're okay, so don't
716 * report them; but do report if we reset SWAP_MAP_MAX.
718 if (*swap_map == SWAP_MAP_MAX) {
719 swap_device_lock(si);
720 *swap_map = 1;
721 swap_device_unlock(si);
722 reset_overflow = 1;
726 * If a reference remains (rare), we would like to leave
727 * the page in the swap cache; but try_to_unmap could
728 * then re-duplicate the entry once we drop page lock,
729 * so we might loop indefinitely; also, that page could
730 * not be swapped out to other storage meanwhile. So:
731 * delete from cache even if there's another reference,
732 * after ensuring that the data has been saved to disk -
733 * since if the reference remains (rarer), it will be
734 * read from disk into another page. Splitting into two
735 * pages would be incorrect if swap supported "shared
736 * private" pages, but they are handled by tmpfs files.
738 * Note shmem_unuse already deleted a swappage from
739 * the swap cache, unless the move to filepage failed:
740 * in which case it left swappage in cache, lowered its
741 * swap count to pass quickly through the loops above,
742 * and now we must reincrement count to try again later.
744 if ((*swap_map > 1) && PageDirty(page) && PageSwapCache(page)) {
745 struct writeback_control wbc = {
746 .sync_mode = WB_SYNC_NONE,
749 swap_writepage(page, &wbc);
750 lock_page(page);
751 wait_on_page_writeback(page);
753 if (PageSwapCache(page)) {
754 if (shmem)
755 swap_duplicate(entry);
756 else
757 delete_from_swap_cache(page);
761 * So we could skip searching mms once swap count went
762 * to 1, we did not mark any present ptes as dirty: must
763 * mark page dirty so shrink_list will preserve it.
765 SetPageDirty(page);
766 unlock_page(page);
767 page_cache_release(page);
770 * Make sure that we aren't completely killing
771 * interactive performance.
773 cond_resched();
776 mmput(start_mm);
777 if (reset_overflow) {
778 printk(KERN_WARNING "swapoff: cleared swap entry overflow\n");
779 swap_overflow = 0;
781 return retval;
785 * Use this swapdev's extent info to locate the (PAGE_SIZE) block which
786 * corresponds to page offset `offset'.
788 sector_t map_swap_page(struct swap_info_struct *sis, pgoff_t offset)
790 struct swap_extent *se = sis->curr_swap_extent;
791 struct swap_extent *start_se = se;
793 for ( ; ; ) {
794 struct list_head *lh;
796 if (se->start_page <= offset &&
797 offset < (se->start_page + se->nr_pages)) {
798 return se->start_block + (offset - se->start_page);
800 lh = se->list.prev;
801 if (lh == &sis->extent_list)
802 lh = lh->prev;
803 se = list_entry(lh, struct swap_extent, list);
804 sis->curr_swap_extent = se;
805 BUG_ON(se == start_se); /* It *must* be present */
810 * Free all of a swapdev's extent information
812 static void destroy_swap_extents(struct swap_info_struct *sis)
814 while (!list_empty(&sis->extent_list)) {
815 struct swap_extent *se;
817 se = list_entry(sis->extent_list.next,
818 struct swap_extent, list);
819 list_del(&se->list);
820 kfree(se);
822 sis->nr_extents = 0;
826 * Add a block range (and the corresponding page range) into this swapdev's
827 * extent list. The extent list is kept sorted in block order.
829 * This function rather assumes that it is called in ascending sector_t order.
830 * It doesn't look for extent coalescing opportunities.
832 static int
833 add_swap_extent(struct swap_info_struct *sis, unsigned long start_page,
834 unsigned long nr_pages, sector_t start_block)
836 struct swap_extent *se;
837 struct swap_extent *new_se;
838 struct list_head *lh;
840 lh = sis->extent_list.next; /* The highest-addressed block */
841 while (lh != &sis->extent_list) {
842 se = list_entry(lh, struct swap_extent, list);
843 if (se->start_block + se->nr_pages == start_block) {
844 /* Merge it */
845 se->nr_pages += nr_pages;
846 return 0;
848 lh = lh->next;
852 * No merge. Insert a new extent, preserving ordering.
854 new_se = kmalloc(sizeof(*se), GFP_KERNEL);
855 if (new_se == NULL)
856 return -ENOMEM;
857 new_se->start_page = start_page;
858 new_se->nr_pages = nr_pages;
859 new_se->start_block = start_block;
861 lh = sis->extent_list.prev; /* The lowest block */
862 while (lh != &sis->extent_list) {
863 se = list_entry(lh, struct swap_extent, list);
864 if (se->start_block > start_block)
865 break;
866 lh = lh->prev;
868 list_add_tail(&new_se->list, lh);
869 sis->nr_extents++;
870 return 0;
874 * A `swap extent' is a simple thing which maps a contiguous range of pages
875 * onto a contiguous range of disk blocks. An ordered list of swap extents
876 * is built at swapon time and is then used at swap_writepage/swap_readpage
877 * time for locating where on disk a page belongs.
879 * If the swapfile is an S_ISBLK block device, a single extent is installed.
880 * This is done so that the main operating code can treat S_ISBLK and S_ISREG
881 * swap files identically.
883 * Whether the swapdev is an S_ISREG file or an S_ISBLK blockdev, the swap
884 * extent list operates in PAGE_SIZE disk blocks. Both S_ISREG and S_ISBLK
885 * swapfiles are handled *identically* after swapon time.
887 * For S_ISREG swapfiles, setup_swap_extents() will walk all the file's blocks
888 * and will parse them into an ordered extent list, in PAGE_SIZE chunks. If
889 * some stray blocks are found which do not fall within the PAGE_SIZE alignment
890 * requirements, they are simply tossed out - we will never use those blocks
891 * for swapping.
893 * For S_ISREG swapfiles we hold i_sem across the life of the swapon. This
894 * prevents root from shooting her foot off by ftruncating an in-use swapfile,
895 * which will scribble on the fs.
897 * The amount of disk space which a single swap extent represents varies.
898 * Typically it is in the 1-4 megabyte range. So we can have hundreds of
899 * extents in the list. To avoid much list walking, we cache the previous
900 * search location in `curr_swap_extent', and start new searches from there.
901 * This is extremely effective. The average number of iterations in
902 * map_swap_page() has been measured at about 0.3 per page. - akpm.
904 static int setup_swap_extents(struct swap_info_struct *sis)
906 struct inode *inode;
907 unsigned blocks_per_page;
908 unsigned long page_no;
909 unsigned blkbits;
910 sector_t probe_block;
911 sector_t last_block;
912 int ret;
914 inode = sis->swap_file->f_dentry->d_inode;
915 if (S_ISBLK(inode->i_mode)) {
916 ret = add_swap_extent(sis, 0, sis->max, 0);
917 goto done;
920 blkbits = inode->i_blkbits;
921 blocks_per_page = PAGE_SIZE >> blkbits;
924 * Map all the blocks into the extent list. This code doesn't try
925 * to be very smart.
927 probe_block = 0;
928 page_no = 0;
929 last_block = i_size_read(inode) >> blkbits;
930 while ((probe_block + blocks_per_page) <= last_block &&
931 page_no < sis->max) {
932 unsigned block_in_page;
933 sector_t first_block;
935 first_block = bmap(inode, probe_block);
936 if (first_block == 0)
937 goto bad_bmap;
940 * It must be PAGE_SIZE aligned on-disk
942 if (first_block & (blocks_per_page - 1)) {
943 probe_block++;
944 goto reprobe;
947 for (block_in_page = 1; block_in_page < blocks_per_page;
948 block_in_page++) {
949 sector_t block;
951 block = bmap(inode, probe_block + block_in_page);
952 if (block == 0)
953 goto bad_bmap;
954 if (block != first_block + block_in_page) {
955 /* Discontiguity */
956 probe_block++;
957 goto reprobe;
962 * We found a PAGE_SIZE-length, PAGE_SIZE-aligned run of blocks
964 ret = add_swap_extent(sis, page_no, 1,
965 first_block >> (PAGE_SHIFT - blkbits));
966 if (ret)
967 goto out;
968 page_no++;
969 probe_block += blocks_per_page;
970 reprobe:
971 continue;
973 ret = 0;
974 if (page_no == 0)
975 ret = -EINVAL;
976 sis->max = page_no;
977 sis->highest_bit = page_no - 1;
978 done:
979 sis->curr_swap_extent = list_entry(sis->extent_list.prev,
980 struct swap_extent, list);
981 goto out;
982 bad_bmap:
983 printk(KERN_ERR "swapon: swapfile has holes\n");
984 ret = -EINVAL;
985 out:
986 return ret;
989 #if 0 /* We don't need this yet */
990 #include <linux/backing-dev.h>
991 int page_queue_congested(struct page *page)
993 struct backing_dev_info *bdi;
995 BUG_ON(!PageLocked(page)); /* It pins the swap_info_struct */
997 bdi = page->mapping->backing_dev_info;
998 if (PageSwapCache(page)) {
999 swp_entry_t entry = { .val = page->index };
1000 struct swap_info_struct *sis;
1002 sis = get_swap_info_struct(swp_type(entry));
1003 bdi = sis->bdev->bd_inode->i_mapping->backing_dev_info;
1005 return bdi_write_congested(bdi);
1007 #endif
1009 asmlinkage long sys_swapoff(const char __user * specialfile)
1011 struct swap_info_struct * p = NULL;
1012 unsigned short *swap_map;
1013 struct file *swap_file, *victim;
1014 struct address_space *mapping;
1015 char * pathname;
1016 int i, type, prev;
1017 int err;
1019 if (!capable(CAP_SYS_ADMIN))
1020 return -EPERM;
1022 pathname = getname(specialfile);
1023 err = PTR_ERR(pathname);
1024 if (IS_ERR(pathname))
1025 goto out;
1027 victim = filp_open(pathname, O_RDWR, 0);
1028 putname(pathname);
1029 err = PTR_ERR(victim);
1030 if (IS_ERR(victim))
1031 goto out;
1033 mapping = victim->f_dentry->d_inode->i_mapping;
1034 prev = -1;
1035 swap_list_lock();
1036 for (type = swap_list.head; type >= 0; type = swap_info[type].next) {
1037 p = swap_info + type;
1038 if ((p->flags & SWP_ACTIVE) == SWP_ACTIVE) {
1039 if (p->swap_file->f_dentry->d_inode->i_mapping==mapping)
1040 break;
1042 prev = type;
1044 if (type < 0) {
1045 err = -EINVAL;
1046 swap_list_unlock();
1047 goto out_dput;
1049 if (!security_vm_enough_memory(p->pages))
1050 vm_unacct_memory(p->pages);
1051 else {
1052 err = -ENOMEM;
1053 swap_list_unlock();
1054 goto out_dput;
1056 if (prev < 0) {
1057 swap_list.head = p->next;
1058 } else {
1059 swap_info[prev].next = p->next;
1061 if (type == swap_list.next) {
1062 /* just pick something that's safe... */
1063 swap_list.next = swap_list.head;
1065 nr_swap_pages -= p->pages;
1066 total_swap_pages -= p->pages;
1067 p->flags &= ~SWP_WRITEOK;
1068 swap_list_unlock();
1069 current->flags |= PF_SWAPOFF;
1070 err = try_to_unuse(type);
1071 current->flags &= ~PF_SWAPOFF;
1072 if (err) {
1073 /* re-insert swap space back into swap_list */
1074 swap_list_lock();
1075 for (prev = -1, i = swap_list.head; i >= 0; prev = i, i = swap_info[i].next)
1076 if (p->prio >= swap_info[i].prio)
1077 break;
1078 p->next = i;
1079 if (prev < 0)
1080 swap_list.head = swap_list.next = p - swap_info;
1081 else
1082 swap_info[prev].next = p - swap_info;
1083 nr_swap_pages += p->pages;
1084 total_swap_pages += p->pages;
1085 p->flags |= SWP_WRITEOK;
1086 swap_list_unlock();
1087 goto out_dput;
1089 swap_list_lock();
1090 swap_device_lock(p);
1091 swap_file = p->swap_file;
1092 p->swap_file = NULL;
1093 p->max = 0;
1094 swap_map = p->swap_map;
1095 p->swap_map = NULL;
1096 p->flags = 0;
1097 destroy_swap_extents(p);
1098 swap_device_unlock(p);
1099 swap_list_unlock();
1100 vfree(swap_map);
1101 if (S_ISBLK(swap_file->f_dentry->d_inode->i_mode)) {
1102 struct block_device *bdev;
1103 bdev = swap_file->f_dentry->d_inode->i_bdev;
1104 set_blocksize(bdev, p->old_block_size);
1105 bd_release(bdev);
1106 } else {
1107 up(&swap_file->f_dentry->d_inode->i_mapping->host->i_sem);
1109 filp_close(swap_file, NULL);
1110 err = 0;
1112 out_dput:
1113 filp_close(victim, NULL);
1114 out:
1115 return err;
1118 #ifdef CONFIG_PROC_FS
1119 /* iterator */
1120 static void *swap_start(struct seq_file *swap, loff_t *pos)
1122 struct swap_info_struct *ptr = swap_info;
1123 int i;
1124 loff_t l = *pos;
1126 swap_list_lock();
1128 for (i = 0; i < nr_swapfiles; i++, ptr++) {
1129 if (!(ptr->flags & SWP_USED) || !ptr->swap_map)
1130 continue;
1131 if (!l--)
1132 return ptr;
1135 return NULL;
1138 static void *swap_next(struct seq_file *swap, void *v, loff_t *pos)
1140 struct swap_info_struct *ptr = v;
1141 void *endptr = (void *) swap_info + nr_swapfiles * sizeof(struct swap_info_struct);
1143 for (++ptr; ptr < (struct swap_info_struct *) endptr; ptr++) {
1144 if (!(ptr->flags & SWP_USED) || !ptr->swap_map)
1145 continue;
1146 ++*pos;
1147 return ptr;
1150 return NULL;
1153 static void swap_stop(struct seq_file *swap, void *v)
1155 swap_list_unlock();
1158 static int swap_show(struct seq_file *swap, void *v)
1160 struct swap_info_struct *ptr = v;
1161 struct file *file;
1162 int len;
1164 if (v == swap_info)
1165 seq_puts(swap, "Filename\t\t\t\tType\t\tSize\tUsed\tPriority\n");
1167 file = ptr->swap_file;
1168 len = seq_path(swap, file->f_vfsmnt, file->f_dentry, " \t\n\\");
1169 seq_printf(swap, "%*s %s\t%d\t%ld\t%d\n",
1170 len < 40 ? 40 - len : 1, " ",
1171 S_ISBLK(file->f_dentry->d_inode->i_mode) ?
1172 "partition" : "file\t",
1173 ptr->pages << (PAGE_SHIFT - 10),
1174 ptr->inuse_pages << (PAGE_SHIFT - 10),
1175 ptr->prio);
1176 return 0;
1179 static struct seq_operations swaps_op = {
1180 .start = swap_start,
1181 .next = swap_next,
1182 .stop = swap_stop,
1183 .show = swap_show
1186 static int swaps_open(struct inode *inode, struct file *file)
1188 return seq_open(file, &swaps_op);
1191 static struct file_operations proc_swaps_operations = {
1192 .open = swaps_open,
1193 .read = seq_read,
1194 .llseek = seq_lseek,
1195 .release = seq_release,
1198 static int __init procswaps_init(void)
1200 struct proc_dir_entry *entry;
1202 entry = create_proc_entry("swaps", 0, NULL);
1203 if (entry)
1204 entry->proc_fops = &proc_swaps_operations;
1205 return 0;
1207 __initcall(procswaps_init);
1208 #endif /* CONFIG_PROC_FS */
1211 * Written 01/25/92 by Simmule Turner, heavily changed by Linus.
1213 * The swapon system call
1215 asmlinkage long sys_swapon(const char __user * specialfile, int swap_flags)
1217 struct swap_info_struct * p;
1218 char *name = NULL;
1219 struct block_device *bdev = NULL;
1220 struct file *swap_file = NULL;
1221 struct address_space *mapping;
1222 unsigned int type;
1223 int i, prev;
1224 int error;
1225 static int least_priority = 0;
1226 union swap_header *swap_header = 0;
1227 int swap_header_version;
1228 int nr_good_pages = 0;
1229 unsigned long maxpages = 1;
1230 int swapfilesize;
1231 unsigned short *swap_map;
1232 struct page *page = NULL;
1233 struct inode *inode;
1234 struct inode *downed_inode = NULL;
1236 if (!capable(CAP_SYS_ADMIN))
1237 return -EPERM;
1238 swap_list_lock();
1239 p = swap_info;
1240 for (type = 0 ; type < nr_swapfiles ; type++,p++)
1241 if (!(p->flags & SWP_USED))
1242 break;
1243 error = -EPERM;
1244 if (type >= MAX_SWAPFILES) {
1245 swap_list_unlock();
1246 goto out;
1248 if (type >= nr_swapfiles)
1249 nr_swapfiles = type+1;
1250 INIT_LIST_HEAD(&p->extent_list);
1251 p->flags = SWP_USED;
1252 p->nr_extents = 0;
1253 p->swap_file = NULL;
1254 p->old_block_size = 0;
1255 p->swap_map = NULL;
1256 p->lowest_bit = 0;
1257 p->highest_bit = 0;
1258 p->cluster_nr = 0;
1259 p->inuse_pages = 0;
1260 p->sdev_lock = SPIN_LOCK_UNLOCKED;
1261 p->next = -1;
1262 if (swap_flags & SWAP_FLAG_PREFER) {
1263 p->prio =
1264 (swap_flags & SWAP_FLAG_PRIO_MASK)>>SWAP_FLAG_PRIO_SHIFT;
1265 } else {
1266 p->prio = --least_priority;
1268 swap_list_unlock();
1269 name = getname(specialfile);
1270 error = PTR_ERR(name);
1271 if (IS_ERR(name))
1272 goto bad_swap_2;
1273 swap_file = filp_open(name, O_RDWR, 0);
1274 error = PTR_ERR(swap_file);
1275 if (IS_ERR(swap_file)) {
1276 swap_file = NULL;
1277 goto bad_swap_2;
1280 p->swap_file = swap_file;
1281 inode = swap_file->f_dentry->d_inode;
1282 mapping = swap_file->f_dentry->d_inode->i_mapping;
1284 error = -EBUSY;
1285 for (i = 0; i < nr_swapfiles; i++) {
1286 struct swap_info_struct *q = &swap_info[i];
1288 if (i == type || !q->swap_file)
1289 continue;
1290 if (mapping == q->swap_file->f_dentry->d_inode->i_mapping)
1291 goto bad_swap;
1294 error = -EINVAL;
1295 if (S_ISBLK(inode->i_mode)) {
1296 bdev = inode->i_bdev;
1297 error = bd_claim(bdev, sys_swapon);
1298 if (error < 0) {
1299 bdev = NULL;
1300 goto bad_swap;
1302 p->old_block_size = block_size(bdev);
1303 error = set_blocksize(inode->i_bdev, PAGE_SIZE);
1304 if (error < 0)
1305 goto bad_swap;
1306 p->bdev = bdev;
1307 } else if (S_ISREG(inode->i_mode)) {
1308 p->bdev = inode->i_sb->s_bdev;
1309 downed_inode = mapping->host;
1310 down(&downed_inode->i_sem);
1311 } else {
1312 goto bad_swap;
1315 swapfilesize = i_size_read(mapping->host) >> PAGE_SHIFT;
1318 * Read the swap header.
1320 page = read_cache_page(mapping, 0,
1321 (filler_t *)mapping->a_ops->readpage, swap_file);
1322 if (IS_ERR(page)) {
1323 error = PTR_ERR(page);
1324 goto bad_swap;
1326 wait_on_page_locked(page);
1327 if (!PageUptodate(page))
1328 goto bad_swap;
1329 kmap(page);
1330 swap_header = page_address(page);
1332 if (!memcmp("SWAP-SPACE",swap_header->magic.magic,10))
1333 swap_header_version = 1;
1334 else if (!memcmp("SWAPSPACE2",swap_header->magic.magic,10))
1335 swap_header_version = 2;
1336 else {
1337 printk("Unable to find swap-space signature\n");
1338 error = -EINVAL;
1339 goto bad_swap;
1342 switch (swap_header_version) {
1343 case 1:
1344 printk(KERN_ERR "version 0 swap is no longer supported. "
1345 "Use mkswap -v1 %s\n", name);
1346 error = -EINVAL;
1347 goto bad_swap;
1348 case 2:
1349 /* Check the swap header's sub-version and the size of
1350 the swap file and bad block lists */
1351 if (swap_header->info.version != 1) {
1352 printk(KERN_WARNING
1353 "Unable to handle swap header version %d\n",
1354 swap_header->info.version);
1355 error = -EINVAL;
1356 goto bad_swap;
1359 p->lowest_bit = 1;
1360 maxpages = swp_offset(swp_entry(0,~0UL)) - 1;
1361 if (maxpages > swap_header->info.last_page)
1362 maxpages = swap_header->info.last_page;
1363 p->highest_bit = maxpages - 1;
1365 error = -EINVAL;
1366 if (swap_header->info.nr_badpages > MAX_SWAP_BADPAGES)
1367 goto bad_swap;
1369 /* OK, set up the swap map and apply the bad block list */
1370 if (!(p->swap_map = vmalloc(maxpages * sizeof(short)))) {
1371 error = -ENOMEM;
1372 goto bad_swap;
1375 error = 0;
1376 memset(p->swap_map, 0, maxpages * sizeof(short));
1377 for (i=0; i<swap_header->info.nr_badpages; i++) {
1378 int page = swap_header->info.badpages[i];
1379 if (page <= 0 || page >= swap_header->info.last_page)
1380 error = -EINVAL;
1381 else
1382 p->swap_map[page] = SWAP_MAP_BAD;
1384 nr_good_pages = swap_header->info.last_page -
1385 swap_header->info.nr_badpages -
1386 1 /* header page */;
1387 if (error)
1388 goto bad_swap;
1391 if (swapfilesize && maxpages > swapfilesize) {
1392 printk(KERN_WARNING
1393 "Swap area shorter than signature indicates\n");
1394 error = -EINVAL;
1395 goto bad_swap;
1397 if (!nr_good_pages) {
1398 printk(KERN_WARNING "Empty swap-file\n");
1399 error = -EINVAL;
1400 goto bad_swap;
1402 p->swap_map[0] = SWAP_MAP_BAD;
1403 p->max = maxpages;
1404 p->pages = nr_good_pages;
1406 if (setup_swap_extents(p))
1407 goto bad_swap;
1409 swap_list_lock();
1410 swap_device_lock(p);
1411 p->flags = SWP_ACTIVE;
1412 nr_swap_pages += nr_good_pages;
1413 total_swap_pages += nr_good_pages;
1414 printk(KERN_INFO "Adding %dk swap on %s. Priority:%d extents:%d\n",
1415 nr_good_pages<<(PAGE_SHIFT-10), name,
1416 p->prio, p->nr_extents);
1418 /* insert swap space into swap_list: */
1419 prev = -1;
1420 for (i = swap_list.head; i >= 0; i = swap_info[i].next) {
1421 if (p->prio >= swap_info[i].prio) {
1422 break;
1424 prev = i;
1426 p->next = i;
1427 if (prev < 0) {
1428 swap_list.head = swap_list.next = p - swap_info;
1429 } else {
1430 swap_info[prev].next = p - swap_info;
1432 swap_device_unlock(p);
1433 swap_list_unlock();
1434 error = 0;
1435 goto out;
1436 bad_swap:
1437 if (bdev) {
1438 set_blocksize(bdev, p->old_block_size);
1439 bd_release(bdev);
1441 bad_swap_2:
1442 swap_list_lock();
1443 swap_map = p->swap_map;
1444 p->swap_file = NULL;
1445 p->swap_map = NULL;
1446 p->flags = 0;
1447 if (!(swap_flags & SWAP_FLAG_PREFER))
1448 ++least_priority;
1449 swap_list_unlock();
1450 destroy_swap_extents(p);
1451 if (swap_map)
1452 vfree(swap_map);
1453 if (swap_file && !IS_ERR(swap_file))
1454 filp_close(swap_file, NULL);
1455 out:
1456 if (page && !IS_ERR(page)) {
1457 kunmap(page);
1458 page_cache_release(page);
1460 if (name)
1461 putname(name);
1462 if (error && downed_inode)
1463 up(&downed_inode->i_sem);
1464 return error;
1467 void si_swapinfo(struct sysinfo *val)
1469 unsigned int i;
1470 unsigned long nr_to_be_unused = 0;
1472 swap_list_lock();
1473 for (i = 0; i < nr_swapfiles; i++) {
1474 if (!(swap_info[i].flags & SWP_USED) ||
1475 (swap_info[i].flags & SWP_WRITEOK))
1476 continue;
1477 nr_to_be_unused += swap_info[i].inuse_pages;
1479 val->freeswap = nr_swap_pages + nr_to_be_unused;
1480 val->totalswap = total_swap_pages + nr_to_be_unused;
1481 swap_list_unlock();
1485 * Verify that a swap entry is valid and increment its swap map count.
1487 * Note: if swap_map[] reaches SWAP_MAP_MAX the entries are treated as
1488 * "permanent", but will be reclaimed by the next swapoff.
1490 int swap_duplicate(swp_entry_t entry)
1492 struct swap_info_struct * p;
1493 unsigned long offset, type;
1494 int result = 0;
1496 type = swp_type(entry);
1497 if (type >= nr_swapfiles)
1498 goto bad_file;
1499 p = type + swap_info;
1500 offset = swp_offset(entry);
1502 swap_device_lock(p);
1503 if (offset < p->max && p->swap_map[offset]) {
1504 if (p->swap_map[offset] < SWAP_MAP_MAX - 1) {
1505 p->swap_map[offset]++;
1506 result = 1;
1507 } else if (p->swap_map[offset] <= SWAP_MAP_MAX) {
1508 if (swap_overflow++ < 5)
1509 printk(KERN_WARNING "swap_dup: swap entry overflow\n");
1510 p->swap_map[offset] = SWAP_MAP_MAX;
1511 result = 1;
1514 swap_device_unlock(p);
1515 out:
1516 return result;
1518 bad_file:
1519 printk(KERN_ERR "swap_dup: %s%08lx\n", Bad_file, entry.val);
1520 goto out;
1523 struct swap_info_struct *
1524 get_swap_info_struct(unsigned type)
1526 return &swap_info[type];
1530 * swap_device_lock prevents swap_map being freed. Don't grab an extra
1531 * reference on the swaphandle, it doesn't matter if it becomes unused.
1533 int valid_swaphandles(swp_entry_t entry, unsigned long *offset)
1535 int ret = 0, i = 1 << page_cluster;
1536 unsigned long toff;
1537 struct swap_info_struct *swapdev = swp_type(entry) + swap_info;
1539 if (!page_cluster) /* no readahead */
1540 return 0;
1541 toff = (swp_offset(entry) >> page_cluster) << page_cluster;
1542 if (!toff) /* first page is swap header */
1543 toff++, i--;
1544 *offset = toff;
1546 swap_device_lock(swapdev);
1547 do {
1548 /* Don't read-ahead past the end of the swap area */
1549 if (toff >= swapdev->max)
1550 break;
1551 /* Don't read in free or bad pages */
1552 if (!swapdev->swap_map[toff])
1553 break;
1554 if (swapdev->swap_map[toff] == SWAP_MAP_BAD)
1555 break;
1556 toff++;
1557 ret++;
1558 } while (--i);
1559 swap_device_unlock(swapdev);
1560 return ret;