ia64/xen-unstable

view old/xenolinux-2.4.16-sparse/mm/swapfile.c @ 235:d7d0a23b2e07

bitkeeper revision 1.93 (3e5a4e6bkPheUp3x1uufN2MS3LAB7A)

Latest and Greatest version of XenoLinux based on the Linux-2.4.21-pre4
kernel.
author iap10@labyrinth.cl.cam.ac.uk
date Mon Feb 24 16:55:07 2003 +0000 (2003-02-24)
parents
children
line source
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 */
8 #include <linux/slab.h>
9 #include <linux/smp_lock.h>
10 #include <linux/kernel_stat.h>
11 #include <linux/swap.h>
12 #include <linux/swapctl.h>
13 #include <linux/blkdev.h> /* for blk_size */
14 #include <linux/vmalloc.h>
15 #include <linux/pagemap.h>
16 #include <linux/shm.h>
17 #include <linux/compiler.h>
19 #include <asm/pgtable.h>
21 spinlock_t swaplock = SPIN_LOCK_UNLOCKED;
22 unsigned int nr_swapfiles;
23 int total_swap_pages;
24 static int swap_overflow;
26 static const char Bad_file[] = "Bad swap file entry ";
27 static const char Unused_file[] = "Unused swap file entry ";
28 static const char Bad_offset[] = "Bad swap offset entry ";
29 static const char Unused_offset[] = "Unused swap offset entry ";
31 struct swap_list_t swap_list = {-1, -1};
33 struct swap_info_struct swap_info[MAX_SWAPFILES];
35 #define SWAPFILE_CLUSTER 256
37 static inline int scan_swap_map(struct swap_info_struct *si)
38 {
39 unsigned long offset;
40 /*
41 * We try to cluster swap pages by allocating them
42 * sequentially in swap. Once we've allocated
43 * SWAPFILE_CLUSTER pages this way, however, we resort to
44 * first-free allocation, starting a new cluster. This
45 * prevents us from scattering swap pages all over the entire
46 * swap partition, so that we reduce overall disk seek times
47 * between swap pages. -- sct */
48 if (si->cluster_nr) {
49 while (si->cluster_next <= si->highest_bit) {
50 offset = si->cluster_next++;
51 if (si->swap_map[offset])
52 continue;
53 si->cluster_nr--;
54 goto got_page;
55 }
56 }
57 si->cluster_nr = SWAPFILE_CLUSTER;
59 /* try to find an empty (even not aligned) cluster. */
60 offset = si->lowest_bit;
61 check_next_cluster:
62 if (offset+SWAPFILE_CLUSTER-1 <= si->highest_bit)
63 {
64 int nr;
65 for (nr = offset; nr < offset+SWAPFILE_CLUSTER; nr++)
66 if (si->swap_map[nr])
67 {
68 offset = nr+1;
69 goto check_next_cluster;
70 }
71 /* We found a completly empty cluster, so start
72 * using it.
73 */
74 goto got_page;
75 }
76 /* No luck, so now go finegrined as usual. -Andrea */
77 for (offset = si->lowest_bit; offset <= si->highest_bit ; offset++) {
78 if (si->swap_map[offset])
79 continue;
80 si->lowest_bit = offset+1;
81 got_page:
82 if (offset == si->lowest_bit)
83 si->lowest_bit++;
84 if (offset == si->highest_bit)
85 si->highest_bit--;
86 if (si->lowest_bit > si->highest_bit) {
87 si->lowest_bit = si->max;
88 si->highest_bit = 0;
89 }
90 si->swap_map[offset] = 1;
91 nr_swap_pages--;
92 si->cluster_next = offset+1;
93 return offset;
94 }
95 si->lowest_bit = si->max;
96 si->highest_bit = 0;
97 return 0;
98 }
100 swp_entry_t get_swap_page(void)
101 {
102 struct swap_info_struct * p;
103 unsigned long offset;
104 swp_entry_t entry;
105 int type, wrapped = 0;
107 entry.val = 0; /* Out of memory */
108 swap_list_lock();
109 type = swap_list.next;
110 if (type < 0)
111 goto out;
112 if (nr_swap_pages <= 0)
113 goto out;
115 while (1) {
116 p = &swap_info[type];
117 if ((p->flags & SWP_WRITEOK) == SWP_WRITEOK) {
118 swap_device_lock(p);
119 offset = scan_swap_map(p);
120 swap_device_unlock(p);
121 if (offset) {
122 entry = SWP_ENTRY(type,offset);
123 type = swap_info[type].next;
124 if (type < 0 ||
125 p->prio != swap_info[type].prio) {
126 swap_list.next = swap_list.head;
127 } else {
128 swap_list.next = type;
129 }
130 goto out;
131 }
132 }
133 type = p->next;
134 if (!wrapped) {
135 if (type < 0 || p->prio != swap_info[type].prio) {
136 type = swap_list.head;
137 wrapped = 1;
138 }
139 } else
140 if (type < 0)
141 goto out; /* out of swap space */
142 }
143 out:
144 swap_list_unlock();
145 return entry;
146 }
148 static struct swap_info_struct * swap_info_get(swp_entry_t entry)
149 {
150 struct swap_info_struct * p;
151 unsigned long offset, type;
153 if (!entry.val)
154 goto out;
155 type = SWP_TYPE(entry);
156 if (type >= nr_swapfiles)
157 goto bad_nofile;
158 p = & swap_info[type];
159 if (!(p->flags & SWP_USED))
160 goto bad_device;
161 offset = SWP_OFFSET(entry);
162 if (offset >= p->max)
163 goto bad_offset;
164 if (!p->swap_map[offset])
165 goto bad_free;
166 swap_list_lock();
167 if (p->prio > swap_info[swap_list.next].prio)
168 swap_list.next = type;
169 swap_device_lock(p);
170 return p;
172 bad_free:
173 printk(KERN_ERR "swap_free: %s%08lx\n", Unused_offset, entry.val);
174 goto out;
175 bad_offset:
176 printk(KERN_ERR "swap_free: %s%08lx\n", Bad_offset, entry.val);
177 goto out;
178 bad_device:
179 printk(KERN_ERR "swap_free: %s%08lx\n", Unused_file, entry.val);
180 goto out;
181 bad_nofile:
182 printk(KERN_ERR "swap_free: %s%08lx\n", Bad_file, entry.val);
183 out:
184 return NULL;
185 }
187 static void swap_info_put(struct swap_info_struct * p)
188 {
189 swap_device_unlock(p);
190 swap_list_unlock();
191 }
193 static int swap_entry_free(struct swap_info_struct *p, unsigned long offset)
194 {
195 int count = p->swap_map[offset];
197 if (count < SWAP_MAP_MAX) {
198 count--;
199 p->swap_map[offset] = count;
200 if (!count) {
201 if (offset < p->lowest_bit)
202 p->lowest_bit = offset;
203 if (offset > p->highest_bit)
204 p->highest_bit = offset;
205 nr_swap_pages++;
206 }
207 }
208 return count;
209 }
211 /*
212 * Caller has made sure that the swapdevice corresponding to entry
213 * is still around or has not been recycled.
214 */
215 void swap_free(swp_entry_t entry)
216 {
217 struct swap_info_struct * p;
219 p = swap_info_get(entry);
220 if (p) {
221 swap_entry_free(p, SWP_OFFSET(entry));
222 swap_info_put(p);
223 }
224 }
226 /*
227 * Check if we're the only user of a swap page,
228 * when the page is locked.
229 */
230 static int exclusive_swap_page(struct page *page)
231 {
232 int retval = 0;
233 struct swap_info_struct * p;
234 swp_entry_t entry;
236 entry.val = page->index;
237 p = swap_info_get(entry);
238 if (p) {
239 /* Is the only swap cache user the cache itself? */
240 if (p->swap_map[SWP_OFFSET(entry)] == 1) {
241 /* Recheck the page count with the pagecache lock held.. */
242 spin_lock(&pagecache_lock);
243 if (page_count(page) - !!page->buffers == 2)
244 retval = 1;
245 spin_unlock(&pagecache_lock);
246 }
247 swap_info_put(p);
248 }
249 return retval;
250 }
252 /*
253 * We can use this swap cache entry directly
254 * if there are no other references to it.
255 *
256 * Here "exclusive_swap_page()" does the real
257 * work, but we opportunistically check whether
258 * we need to get all the locks first..
259 */
260 int can_share_swap_page(struct page *page)
261 {
262 int retval = 0;
264 if (!PageLocked(page))
265 BUG();
266 switch (page_count(page)) {
267 case 3:
268 if (!page->buffers)
269 break;
270 /* Fallthrough */
271 case 2:
272 if (!PageSwapCache(page))
273 break;
274 retval = exclusive_swap_page(page);
275 break;
276 case 1:
277 if (PageReserved(page))
278 break;
279 retval = 1;
280 }
281 return retval;
282 }
284 /*
285 * Work out if there are any other processes sharing this
286 * swap cache page. Free it if you can. Return success.
287 */
288 int remove_exclusive_swap_page(struct page *page)
289 {
290 int retval;
291 struct swap_info_struct * p;
292 swp_entry_t entry;
294 if (!PageLocked(page))
295 BUG();
296 if (!PageSwapCache(page))
297 return 0;
298 if (page_count(page) - !!page->buffers != 2) /* 2: us + cache */
299 return 0;
301 entry.val = page->index;
302 p = swap_info_get(entry);
303 if (!p)
304 return 0;
306 /* Is the only swap cache user the cache itself? */
307 retval = 0;
308 if (p->swap_map[SWP_OFFSET(entry)] == 1) {
309 /* Recheck the page count with the pagecache lock held.. */
310 spin_lock(&pagecache_lock);
311 if (page_count(page) - !!page->buffers == 2) {
312 __delete_from_swap_cache(page);
313 SetPageDirty(page);
314 retval = 1;
315 }
316 spin_unlock(&pagecache_lock);
317 }
318 swap_info_put(p);
320 if (retval) {
321 block_flushpage(page, 0);
322 swap_free(entry);
323 page_cache_release(page);
324 }
326 return retval;
327 }
329 /*
330 * Free the swap entry like above, but also try to
331 * free the page cache entry if it is the last user.
332 */
333 void free_swap_and_cache(swp_entry_t entry)
334 {
335 struct swap_info_struct * p;
336 struct page *page = NULL;
338 p = swap_info_get(entry);
339 if (p) {
340 if (swap_entry_free(p, SWP_OFFSET(entry)) == 1)
341 page = find_trylock_page(&swapper_space, entry.val);
342 swap_info_put(p);
343 }
344 if (page) {
345 page_cache_get(page);
346 /* Only cache user (+us), or swap space full? Free it! */
347 if (page_count(page) == 2 || vm_swap_full()) {
348 delete_from_swap_cache(page);
349 SetPageDirty(page);
350 }
351 UnlockPage(page);
352 page_cache_release(page);
353 }
354 }
356 /*
357 * The swap entry has been read in advance, and we return 1 to indicate
358 * that the page has been used or is no longer needed.
359 *
360 * Always set the resulting pte to be nowrite (the same as COW pages
361 * after one process has exited). We don't know just how many PTEs will
362 * share this swap entry, so be cautious and let do_wp_page work out
363 * what to do if a write is requested later.
364 */
365 /* mmlist_lock and vma->vm_mm->page_table_lock are held */
366 static inline void unuse_pte(struct vm_area_struct * vma, unsigned long address,
367 pte_t *dir, swp_entry_t entry, struct page* page)
368 {
369 pte_t pte = *dir;
371 if (likely(pte_to_swp_entry(pte).val != entry.val))
372 return;
373 if (unlikely(pte_none(pte) || pte_present(pte)))
374 return;
375 get_page(page);
376 set_pte(dir, pte_mkold(mk_pte(page, vma->vm_page_prot)));
377 swap_free(entry);
378 ++vma->vm_mm->rss;
379 }
381 /* mmlist_lock and vma->vm_mm->page_table_lock are held */
382 static inline void unuse_pmd(struct vm_area_struct * vma, pmd_t *dir,
383 unsigned long address, unsigned long size, unsigned long offset,
384 swp_entry_t entry, struct page* page)
385 {
386 pte_t * pte;
387 unsigned long end;
389 if (pmd_none(*dir))
390 return;
391 if (pmd_bad(*dir)) {
392 pmd_ERROR(*dir);
393 pmd_clear(dir);
394 return;
395 }
396 pte = pte_offset(dir, address);
397 offset += address & PMD_MASK;
398 address &= ~PMD_MASK;
399 end = address + size;
400 if (end > PMD_SIZE)
401 end = PMD_SIZE;
402 do {
403 unuse_pte(vma, offset+address-vma->vm_start, pte, entry, page);
404 address += PAGE_SIZE;
405 pte++;
406 } while (address && (address < end));
407 }
409 /* mmlist_lock and vma->vm_mm->page_table_lock are held */
410 static inline void unuse_pgd(struct vm_area_struct * vma, pgd_t *dir,
411 unsigned long address, unsigned long size,
412 swp_entry_t entry, struct page* page)
413 {
414 pmd_t * pmd;
415 unsigned long offset, end;
417 if (pgd_none(*dir))
418 return;
419 if (pgd_bad(*dir)) {
420 pgd_ERROR(*dir);
421 pgd_clear(dir);
422 return;
423 }
424 pmd = pmd_offset(dir, address);
425 offset = address & PGDIR_MASK;
426 address &= ~PGDIR_MASK;
427 end = address + size;
428 if (end > PGDIR_SIZE)
429 end = PGDIR_SIZE;
430 if (address >= end)
431 BUG();
432 do {
433 unuse_pmd(vma, pmd, address, end - address, offset, entry,
434 page);
435 address = (address + PMD_SIZE) & PMD_MASK;
436 pmd++;
437 } while (address && (address < end));
438 }
440 /* mmlist_lock and vma->vm_mm->page_table_lock are held */
441 static void unuse_vma(struct vm_area_struct * vma, pgd_t *pgdir,
442 swp_entry_t entry, struct page* page)
443 {
444 unsigned long start = vma->vm_start, end = vma->vm_end;
446 if (start >= end)
447 BUG();
448 do {
449 unuse_pgd(vma, pgdir, start, end - start, entry, page);
450 start = (start + PGDIR_SIZE) & PGDIR_MASK;
451 pgdir++;
452 } while (start && (start < end));
453 }
455 static void unuse_process(struct mm_struct * mm,
456 swp_entry_t entry, struct page* page)
457 {
458 struct vm_area_struct* vma;
460 /*
461 * Go through process' page directory.
462 */
463 spin_lock(&mm->page_table_lock);
464 for (vma = mm->mmap; vma; vma = vma->vm_next) {
465 pgd_t * pgd = pgd_offset(mm, vma->vm_start);
466 unuse_vma(vma, pgd, entry, page);
467 }
468 XENO_flush_page_update_queue();
469 spin_unlock(&mm->page_table_lock);
470 return;
471 }
473 /*
474 * Scan swap_map from current position to next entry still in use.
475 * Recycle to start on reaching the end, returning 0 when empty.
476 */
477 static int find_next_to_unuse(struct swap_info_struct *si, int prev)
478 {
479 int max = si->max;
480 int i = prev;
481 int count;
483 /*
484 * No need for swap_device_lock(si) here: we're just looking
485 * for whether an entry is in use, not modifying it; false
486 * hits are okay, and sys_swapoff() has already prevented new
487 * allocations from this area (while holding swap_list_lock()).
488 */
489 for (;;) {
490 if (++i >= max) {
491 if (!prev) {
492 i = 0;
493 break;
494 }
495 /*
496 * No entries in use at top of swap_map,
497 * loop back to start and recheck there.
498 */
499 max = prev + 1;
500 prev = 0;
501 i = 1;
502 }
503 count = si->swap_map[i];
504 if (count && count != SWAP_MAP_BAD)
505 break;
506 }
507 return i;
508 }
510 /*
511 * We completely avoid races by reading each swap page in advance,
512 * and then search for the process using it. All the necessary
513 * page table adjustments can then be made atomically.
514 */
515 static int try_to_unuse(unsigned int type)
516 {
517 struct swap_info_struct * si = &swap_info[type];
518 struct mm_struct *start_mm;
519 unsigned short *swap_map;
520 unsigned short swcount;
521 struct page *page;
522 swp_entry_t entry;
523 int i = 0;
524 int retval = 0;
525 int reset_overflow = 0;
527 /*
528 * When searching mms for an entry, a good strategy is to
529 * start at the first mm we freed the previous entry from
530 * (though actually we don't notice whether we or coincidence
531 * freed the entry). Initialize this start_mm with a hold.
532 *
533 * A simpler strategy would be to start at the last mm we
534 * freed the previous entry from; but that would take less
535 * advantage of mmlist ordering (now preserved by swap_out()),
536 * which clusters forked address spaces together, most recent
537 * child immediately after parent. If we race with dup_mmap(),
538 * we very much want to resolve parent before child, otherwise
539 * we may miss some entries: using last mm would invert that.
540 */
541 start_mm = &init_mm;
542 atomic_inc(&init_mm.mm_users);
544 /*
545 * Keep on scanning until all entries have gone. Usually,
546 * one pass through swap_map is enough, but not necessarily:
547 * mmput() removes mm from mmlist before exit_mmap() and its
548 * zap_page_range(). That's not too bad, those entries are
549 * on their way out, and handled faster there than here.
550 * do_munmap() behaves similarly, taking the range out of mm's
551 * vma list before zap_page_range(). But unfortunately, when
552 * unmapping a part of a vma, it takes the whole out first,
553 * then reinserts what's left after (might even reschedule if
554 * open() method called) - so swap entries may be invisible
555 * to swapoff for a while, then reappear - but that is rare.
556 */
557 while ((i = find_next_to_unuse(si, i))) {
558 /*
559 * Get a page for the entry, using the existing swap
560 * cache page if there is one. Otherwise, get a clean
561 * page and read the swap into it.
562 */
563 swap_map = &si->swap_map[i];
564 entry = SWP_ENTRY(type, i);
565 page = read_swap_cache_async(entry);
566 if (!page) {
567 /*
568 * Either swap_duplicate() failed because entry
569 * has been freed independently, and will not be
570 * reused since sys_swapoff() already disabled
571 * allocation from here, or alloc_page() failed.
572 */
573 if (!*swap_map)
574 continue;
575 retval = -ENOMEM;
576 break;
577 }
579 /*
580 * Don't hold on to start_mm if it looks like exiting.
581 */
582 if (atomic_read(&start_mm->mm_users) == 1) {
583 mmput(start_mm);
584 start_mm = &init_mm;
585 atomic_inc(&init_mm.mm_users);
586 }
588 /*
589 * Wait for and lock page. When do_swap_page races with
590 * try_to_unuse, do_swap_page can handle the fault much
591 * faster than try_to_unuse can locate the entry. This
592 * apparently redundant "wait_on_page" lets try_to_unuse
593 * defer to do_swap_page in such a case - in some tests,
594 * do_swap_page and try_to_unuse repeatedly compete.
595 */
596 wait_on_page(page);
597 lock_page(page);
599 /*
600 * Remove all references to entry, without blocking.
601 * Whenever we reach init_mm, there's no address space
602 * to search, but use it as a reminder to search shmem.
603 */
604 swcount = *swap_map;
605 if (swcount > 1) {
606 flush_page_to_ram(page);
607 if (start_mm == &init_mm)
608 shmem_unuse(entry, page);
609 else
610 unuse_process(start_mm, entry, page);
611 }
612 if (*swap_map > 1) {
613 int set_start_mm = (*swap_map >= swcount);
614 struct list_head *p = &start_mm->mmlist;
615 struct mm_struct *new_start_mm = start_mm;
616 struct mm_struct *mm;
618 spin_lock(&mmlist_lock);
619 while (*swap_map > 1 &&
620 (p = p->next) != &start_mm->mmlist) {
621 mm = list_entry(p, struct mm_struct, mmlist);
622 swcount = *swap_map;
623 if (mm == &init_mm) {
624 set_start_mm = 1;
625 shmem_unuse(entry, page);
626 } else
627 unuse_process(mm, entry, page);
628 if (set_start_mm && *swap_map < swcount) {
629 new_start_mm = mm;
630 set_start_mm = 0;
631 }
632 }
633 atomic_inc(&new_start_mm->mm_users);
634 spin_unlock(&mmlist_lock);
635 mmput(start_mm);
636 start_mm = new_start_mm;
637 }
639 /*
640 * How could swap count reach 0x7fff when the maximum
641 * pid is 0x7fff, and there's no way to repeat a swap
642 * page within an mm (except in shmem, where it's the
643 * shared object which takes the reference count)?
644 * We believe SWAP_MAP_MAX cannot occur in Linux 2.4.
645 *
646 * If that's wrong, then we should worry more about
647 * exit_mmap() and do_munmap() cases described above:
648 * we might be resetting SWAP_MAP_MAX too early here.
649 * We know "Undead"s can happen, they're okay, so don't
650 * report them; but do report if we reset SWAP_MAP_MAX.
651 */
652 if (*swap_map == SWAP_MAP_MAX) {
653 swap_list_lock();
654 swap_device_lock(si);
655 nr_swap_pages++;
656 *swap_map = 1;
657 swap_device_unlock(si);
658 swap_list_unlock();
659 reset_overflow = 1;
660 }
662 /*
663 * If a reference remains (rare), we would like to leave
664 * the page in the swap cache; but try_to_swap_out could
665 * then re-duplicate the entry once we drop page lock,
666 * so we might loop indefinitely; also, that page could
667 * not be swapped out to other storage meanwhile. So:
668 * delete from cache even if there's another reference,
669 * after ensuring that the data has been saved to disk -
670 * since if the reference remains (rarer), it will be
671 * read from disk into another page. Splitting into two
672 * pages would be incorrect if swap supported "shared
673 * private" pages, but they are handled by tmpfs files.
674 * Note shmem_unuse already deleted its from swap cache.
675 */
676 swcount = *swap_map;
677 if ((swcount > 0) != PageSwapCache(page))
678 BUG();
679 if ((swcount > 1) && PageDirty(page)) {
680 rw_swap_page(WRITE, page);
681 lock_page(page);
682 }
683 if (PageSwapCache(page))
684 delete_from_swap_cache(page);
686 /*
687 * So we could skip searching mms once swap count went
688 * to 1, we did not mark any present ptes as dirty: must
689 * mark page dirty so try_to_swap_out will preserve it.
690 */
691 SetPageDirty(page);
692 UnlockPage(page);
693 page_cache_release(page);
695 /*
696 * Make sure that we aren't completely killing
697 * interactive performance. Interruptible check on
698 * signal_pending() would be nice, but changes the spec?
699 */
700 if (current->need_resched)
701 schedule();
702 }
704 mmput(start_mm);
705 if (reset_overflow) {
706 printk(KERN_WARNING "swapoff: cleared swap entry overflow\n");
707 swap_overflow = 0;
708 }
709 return retval;
710 }
712 asmlinkage long sys_swapoff(const char * specialfile)
713 {
714 struct swap_info_struct * p = NULL;
715 unsigned short *swap_map;
716 struct nameidata nd;
717 int i, type, prev;
718 int err;
720 if (!capable(CAP_SYS_ADMIN))
721 return -EPERM;
723 err = user_path_walk(specialfile, &nd);
724 if (err)
725 goto out;
727 lock_kernel();
728 prev = -1;
729 swap_list_lock();
730 for (type = swap_list.head; type >= 0; type = swap_info[type].next) {
731 p = swap_info + type;
732 if ((p->flags & SWP_WRITEOK) == SWP_WRITEOK) {
733 if (p->swap_file == nd.dentry)
734 break;
735 }
736 prev = type;
737 }
738 err = -EINVAL;
739 if (type < 0) {
740 swap_list_unlock();
741 goto out_dput;
742 }
744 if (prev < 0) {
745 swap_list.head = p->next;
746 } else {
747 swap_info[prev].next = p->next;
748 }
749 if (type == swap_list.next) {
750 /* just pick something that's safe... */
751 swap_list.next = swap_list.head;
752 }
753 nr_swap_pages -= p->pages;
754 total_swap_pages -= p->pages;
755 p->flags = SWP_USED;
756 swap_list_unlock();
757 unlock_kernel();
758 err = try_to_unuse(type);
759 lock_kernel();
760 if (err) {
761 /* re-insert swap space back into swap_list */
762 swap_list_lock();
763 for (prev = -1, i = swap_list.head; i >= 0; prev = i, i = swap_info[i].next)
764 if (p->prio >= swap_info[i].prio)
765 break;
766 p->next = i;
767 if (prev < 0)
768 swap_list.head = swap_list.next = p - swap_info;
769 else
770 swap_info[prev].next = p - swap_info;
771 nr_swap_pages += p->pages;
772 total_swap_pages += p->pages;
773 p->flags = SWP_WRITEOK;
774 swap_list_unlock();
775 goto out_dput;
776 }
777 if (p->swap_device)
778 blkdev_put(p->swap_file->d_inode->i_bdev, BDEV_SWAP);
779 path_release(&nd);
781 swap_list_lock();
782 swap_device_lock(p);
783 nd.mnt = p->swap_vfsmnt;
784 nd.dentry = p->swap_file;
785 p->swap_vfsmnt = NULL;
786 p->swap_file = NULL;
787 p->swap_device = 0;
788 p->max = 0;
789 swap_map = p->swap_map;
790 p->swap_map = NULL;
791 p->flags = 0;
792 swap_device_unlock(p);
793 swap_list_unlock();
794 vfree(swap_map);
795 err = 0;
797 out_dput:
798 unlock_kernel();
799 path_release(&nd);
800 out:
801 return err;
802 }
804 int get_swaparea_info(char *buf)
805 {
806 char * page = (char *) __get_free_page(GFP_KERNEL);
807 struct swap_info_struct *ptr = swap_info;
808 int i, j, len = 0, usedswap;
810 if (!page)
811 return -ENOMEM;
813 len += sprintf(buf, "Filename\t\t\tType\t\tSize\tUsed\tPriority\n");
814 for (i = 0 ; i < nr_swapfiles ; i++, ptr++) {
815 if ((ptr->flags & SWP_USED) && ptr->swap_map) {
816 char * path = d_path(ptr->swap_file, ptr->swap_vfsmnt,
817 page, PAGE_SIZE);
819 len += sprintf(buf + len, "%-31s ", path);
821 if (!ptr->swap_device)
822 len += sprintf(buf + len, "file\t\t");
823 else
824 len += sprintf(buf + len, "partition\t");
826 usedswap = 0;
827 for (j = 0; j < ptr->max; ++j)
828 switch (ptr->swap_map[j]) {
829 case SWAP_MAP_BAD:
830 case 0:
831 continue;
832 default:
833 usedswap++;
834 }
835 len += sprintf(buf + len, "%d\t%d\t%d\n", ptr->pages << (PAGE_SHIFT - 10),
836 usedswap << (PAGE_SHIFT - 10), ptr->prio);
837 }
838 }
839 free_page((unsigned long) page);
840 return len;
841 }
843 int is_swap_partition(kdev_t dev) {
844 struct swap_info_struct *ptr = swap_info;
845 int i;
847 for (i = 0 ; i < nr_swapfiles ; i++, ptr++) {
848 if (ptr->flags & SWP_USED)
849 if (ptr->swap_device == dev)
850 return 1;
851 }
852 return 0;
853 }
855 /*
856 * Written 01/25/92 by Simmule Turner, heavily changed by Linus.
857 *
858 * The swapon system call
859 */
860 asmlinkage long sys_swapon(const char * specialfile, int swap_flags)
861 {
862 struct swap_info_struct * p;
863 struct nameidata nd;
864 struct inode * swap_inode;
865 unsigned int type;
866 int i, j, prev;
867 int error;
868 static int least_priority = 0;
869 union swap_header *swap_header = 0;
870 int swap_header_version;
871 int nr_good_pages = 0;
872 unsigned long maxpages = 1;
873 int swapfilesize;
874 struct block_device *bdev = NULL;
875 unsigned short *swap_map;
877 if (!capable(CAP_SYS_ADMIN))
878 return -EPERM;
879 lock_kernel();
880 swap_list_lock();
881 p = swap_info;
882 for (type = 0 ; type < nr_swapfiles ; type++,p++)
883 if (!(p->flags & SWP_USED))
884 break;
885 error = -EPERM;
886 if (type >= MAX_SWAPFILES) {
887 swap_list_unlock();
888 goto out;
889 }
890 if (type >= nr_swapfiles)
891 nr_swapfiles = type+1;
892 p->flags = SWP_USED;
893 p->swap_file = NULL;
894 p->swap_vfsmnt = NULL;
895 p->swap_device = 0;
896 p->swap_map = NULL;
897 p->lowest_bit = 0;
898 p->highest_bit = 0;
899 p->cluster_nr = 0;
900 p->sdev_lock = SPIN_LOCK_UNLOCKED;
901 p->next = -1;
902 if (swap_flags & SWAP_FLAG_PREFER) {
903 p->prio =
904 (swap_flags & SWAP_FLAG_PRIO_MASK)>>SWAP_FLAG_PRIO_SHIFT;
905 } else {
906 p->prio = --least_priority;
907 }
908 swap_list_unlock();
909 error = user_path_walk(specialfile, &nd);
910 if (error)
911 goto bad_swap_2;
913 p->swap_file = nd.dentry;
914 p->swap_vfsmnt = nd.mnt;
915 swap_inode = nd.dentry->d_inode;
916 error = -EINVAL;
918 if (S_ISBLK(swap_inode->i_mode)) {
919 kdev_t dev = swap_inode->i_rdev;
920 struct block_device_operations *bdops;
922 p->swap_device = dev;
923 set_blocksize(dev, PAGE_SIZE);
925 bd_acquire(swap_inode);
926 bdev = swap_inode->i_bdev;
927 bdops = devfs_get_ops(devfs_get_handle_from_inode(swap_inode));
928 if (bdops) bdev->bd_op = bdops;
930 error = blkdev_get(bdev, FMODE_READ|FMODE_WRITE, 0, BDEV_SWAP);
931 if (error)
932 goto bad_swap_2;
933 set_blocksize(dev, PAGE_SIZE);
934 error = -ENODEV;
935 if (!dev || (blk_size[MAJOR(dev)] &&
936 !blk_size[MAJOR(dev)][MINOR(dev)]))
937 goto bad_swap;
938 swapfilesize = 0;
939 if (blk_size[MAJOR(dev)])
940 swapfilesize = blk_size[MAJOR(dev)][MINOR(dev)]
941 >> (PAGE_SHIFT - 10);
942 } else if (S_ISREG(swap_inode->i_mode))
943 swapfilesize = swap_inode->i_size >> PAGE_SHIFT;
944 else
945 goto bad_swap;
947 error = -EBUSY;
948 for (i = 0 ; i < nr_swapfiles ; i++) {
949 struct swap_info_struct *q = &swap_info[i];
950 if (i == type || !q->swap_file)
951 continue;
952 if (swap_inode->i_mapping == q->swap_file->d_inode->i_mapping)
953 goto bad_swap;
954 }
956 swap_header = (void *) __get_free_page(GFP_USER);
957 if (!swap_header) {
958 printk("Unable to start swapping: out of memory :-)\n");
959 error = -ENOMEM;
960 goto bad_swap;
961 }
963 lock_page(virt_to_page(swap_header));
964 rw_swap_page_nolock(READ, SWP_ENTRY(type,0), (char *) swap_header);
966 if (!memcmp("SWAP-SPACE",swap_header->magic.magic,10))
967 swap_header_version = 1;
968 else if (!memcmp("SWAPSPACE2",swap_header->magic.magic,10))
969 swap_header_version = 2;
970 else {
971 printk("Unable to find swap-space signature\n");
972 error = -EINVAL;
973 goto bad_swap;
974 }
976 switch (swap_header_version) {
977 case 1:
978 memset(((char *) swap_header)+PAGE_SIZE-10,0,10);
979 j = 0;
980 p->lowest_bit = 0;
981 p->highest_bit = 0;
982 for (i = 1 ; i < 8*PAGE_SIZE ; i++) {
983 if (test_bit(i,(char *) swap_header)) {
984 if (!p->lowest_bit)
985 p->lowest_bit = i;
986 p->highest_bit = i;
987 maxpages = i+1;
988 j++;
989 }
990 }
991 nr_good_pages = j;
992 p->swap_map = vmalloc(maxpages * sizeof(short));
993 if (!p->swap_map) {
994 error = -ENOMEM;
995 goto bad_swap;
996 }
997 for (i = 1 ; i < maxpages ; i++) {
998 if (test_bit(i,(char *) swap_header))
999 p->swap_map[i] = 0;
1000 else
1001 p->swap_map[i] = SWAP_MAP_BAD;
1003 break;
1005 case 2:
1006 /* Check the swap header's sub-version and the size of
1007 the swap file and bad block lists */
1008 if (swap_header->info.version != 1) {
1009 printk(KERN_WARNING
1010 "Unable to handle swap header version %d\n",
1011 swap_header->info.version);
1012 error = -EINVAL;
1013 goto bad_swap;
1016 p->lowest_bit = 1;
1017 maxpages = SWP_OFFSET(SWP_ENTRY(0,~0UL)) - 1;
1018 if (maxpages > swap_header->info.last_page)
1019 maxpages = swap_header->info.last_page;
1020 p->highest_bit = maxpages - 1;
1022 error = -EINVAL;
1023 if (swap_header->info.nr_badpages > MAX_SWAP_BADPAGES)
1024 goto bad_swap;
1026 /* OK, set up the swap map and apply the bad block list */
1027 if (!(p->swap_map = vmalloc(maxpages * sizeof(short)))) {
1028 error = -ENOMEM;
1029 goto bad_swap;
1032 error = 0;
1033 memset(p->swap_map, 0, maxpages * sizeof(short));
1034 for (i=0; i<swap_header->info.nr_badpages; i++) {
1035 int page = swap_header->info.badpages[i];
1036 if (page <= 0 || page >= swap_header->info.last_page)
1037 error = -EINVAL;
1038 else
1039 p->swap_map[page] = SWAP_MAP_BAD;
1041 nr_good_pages = swap_header->info.last_page -
1042 swap_header->info.nr_badpages -
1043 1 /* header page */;
1044 if (error)
1045 goto bad_swap;
1048 if (swapfilesize && maxpages > swapfilesize) {
1049 printk(KERN_WARNING
1050 "Swap area shorter than signature indicates\n");
1051 error = -EINVAL;
1052 goto bad_swap;
1054 if (!nr_good_pages) {
1055 printk(KERN_WARNING "Empty swap-file\n");
1056 error = -EINVAL;
1057 goto bad_swap;
1059 p->swap_map[0] = SWAP_MAP_BAD;
1060 swap_list_lock();
1061 swap_device_lock(p);
1062 p->max = maxpages;
1063 p->flags = SWP_WRITEOK;
1064 p->pages = nr_good_pages;
1065 nr_swap_pages += nr_good_pages;
1066 total_swap_pages += nr_good_pages;
1067 printk(KERN_INFO "Adding Swap: %dk swap-space (priority %d)\n",
1068 nr_good_pages<<(PAGE_SHIFT-10), p->prio);
1070 /* insert swap space into swap_list: */
1071 prev = -1;
1072 for (i = swap_list.head; i >= 0; i = swap_info[i].next) {
1073 if (p->prio >= swap_info[i].prio) {
1074 break;
1076 prev = i;
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;
1084 swap_device_unlock(p);
1085 swap_list_unlock();
1086 error = 0;
1087 goto out;
1088 bad_swap:
1089 if (bdev)
1090 blkdev_put(bdev, BDEV_SWAP);
1091 bad_swap_2:
1092 swap_list_lock();
1093 swap_map = p->swap_map;
1094 nd.mnt = p->swap_vfsmnt;
1095 nd.dentry = p->swap_file;
1096 p->swap_device = 0;
1097 p->swap_file = NULL;
1098 p->swap_vfsmnt = NULL;
1099 p->swap_map = NULL;
1100 p->flags = 0;
1101 if (!(swap_flags & SWAP_FLAG_PREFER))
1102 ++least_priority;
1103 swap_list_unlock();
1104 if (swap_map)
1105 vfree(swap_map);
1106 path_release(&nd);
1107 out:
1108 if (swap_header)
1109 free_page((long) swap_header);
1110 unlock_kernel();
1111 return error;
1114 void si_swapinfo(struct sysinfo *val)
1116 unsigned int i;
1117 unsigned long nr_to_be_unused = 0;
1119 swap_list_lock();
1120 for (i = 0; i < nr_swapfiles; i++) {
1121 unsigned int j;
1122 if (swap_info[i].flags != SWP_USED)
1123 continue;
1124 for (j = 0; j < swap_info[i].max; ++j) {
1125 switch (swap_info[i].swap_map[j]) {
1126 case 0:
1127 case SWAP_MAP_BAD:
1128 continue;
1129 default:
1130 nr_to_be_unused++;
1134 val->freeswap = nr_swap_pages + nr_to_be_unused;
1135 val->totalswap = total_swap_pages + nr_to_be_unused;
1136 swap_list_unlock();
1139 /*
1140 * Verify that a swap entry is valid and increment its swap map count.
1142 * Note: if swap_map[] reaches SWAP_MAP_MAX the entries are treated as
1143 * "permanent", but will be reclaimed by the next swapoff.
1144 */
1145 int swap_duplicate(swp_entry_t entry)
1147 struct swap_info_struct * p;
1148 unsigned long offset, type;
1149 int result = 0;
1151 type = SWP_TYPE(entry);
1152 if (type >= nr_swapfiles)
1153 goto bad_file;
1154 p = type + swap_info;
1155 offset = SWP_OFFSET(entry);
1157 swap_device_lock(p);
1158 if (offset < p->max && p->swap_map[offset]) {
1159 if (p->swap_map[offset] < SWAP_MAP_MAX - 1) {
1160 p->swap_map[offset]++;
1161 result = 1;
1162 } else if (p->swap_map[offset] <= SWAP_MAP_MAX) {
1163 if (swap_overflow++ < 5)
1164 printk(KERN_WARNING "swap_dup: swap entry overflow\n");
1165 p->swap_map[offset] = SWAP_MAP_MAX;
1166 result = 1;
1169 swap_device_unlock(p);
1170 out:
1171 return result;
1173 bad_file:
1174 printk(KERN_ERR "swap_dup: %s%08lx\n", Bad_file, entry.val);
1175 goto out;
1178 /*
1179 * Page lock needs to be held in all cases to prevent races with
1180 * swap file deletion.
1181 */
1182 int swap_count(struct page *page)
1184 struct swap_info_struct * p;
1185 unsigned long offset, type;
1186 swp_entry_t entry;
1187 int retval = 0;
1189 entry.val = page->index;
1190 if (!entry.val)
1191 goto bad_entry;
1192 type = SWP_TYPE(entry);
1193 if (type >= nr_swapfiles)
1194 goto bad_file;
1195 p = type + swap_info;
1196 offset = SWP_OFFSET(entry);
1197 if (offset >= p->max)
1198 goto bad_offset;
1199 if (!p->swap_map[offset])
1200 goto bad_unused;
1201 retval = p->swap_map[offset];
1202 out:
1203 return retval;
1205 bad_entry:
1206 printk(KERN_ERR "swap_count: null entry!\n");
1207 goto out;
1208 bad_file:
1209 printk(KERN_ERR "swap_count: %s%08lx\n", Bad_file, entry.val);
1210 goto out;
1211 bad_offset:
1212 printk(KERN_ERR "swap_count: %s%08lx\n", Bad_offset, entry.val);
1213 goto out;
1214 bad_unused:
1215 printk(KERN_ERR "swap_count: %s%08lx\n", Unused_offset, entry.val);
1216 goto out;
1219 /*
1220 * Prior swap_duplicate protects against swap device deletion.
1221 */
1222 void get_swaphandle_info(swp_entry_t entry, unsigned long *offset,
1223 kdev_t *dev, struct inode **swapf)
1225 unsigned long type;
1226 struct swap_info_struct *p;
1228 type = SWP_TYPE(entry);
1229 if (type >= nr_swapfiles) {
1230 printk(KERN_ERR "rw_swap_page: %s%08lx\n", Bad_file, entry.val);
1231 return;
1234 p = &swap_info[type];
1235 *offset = SWP_OFFSET(entry);
1236 if (*offset >= p->max && *offset != 0) {
1237 printk(KERN_ERR "rw_swap_page: %s%08lx\n", Bad_offset, entry.val);
1238 return;
1240 if (p->swap_map && !p->swap_map[*offset]) {
1241 printk(KERN_ERR "rw_swap_page: %s%08lx\n", Unused_offset, entry.val);
1242 return;
1244 if (!(p->flags & SWP_USED)) {
1245 printk(KERN_ERR "rw_swap_page: %s%08lx\n", Unused_file, entry.val);
1246 return;
1249 if (p->swap_device) {
1250 *dev = p->swap_device;
1251 } else if (p->swap_file) {
1252 *swapf = p->swap_file->d_inode;
1253 } else {
1254 printk(KERN_ERR "rw_swap_page: no swap file or device\n");
1256 return;
1259 /*
1260 * swap_device_lock prevents swap_map being freed. Don't grab an extra
1261 * reference on the swaphandle, it doesn't matter if it becomes unused.
1262 */
1263 int valid_swaphandles(swp_entry_t entry, unsigned long *offset)
1265 int ret = 0, i = 1 << page_cluster;
1266 unsigned long toff;
1267 struct swap_info_struct *swapdev = SWP_TYPE(entry) + swap_info;
1269 if (!page_cluster) /* no readahead */
1270 return 0;
1271 toff = (SWP_OFFSET(entry) >> page_cluster) << page_cluster;
1272 if (!toff) /* first page is swap header */
1273 toff++, i--;
1274 *offset = toff;
1276 swap_device_lock(swapdev);
1277 do {
1278 /* Don't read-ahead past the end of the swap area */
1279 if (toff >= swapdev->max)
1280 break;
1281 /* Don't read in free or bad pages */
1282 if (!swapdev->swap_map[toff])
1283 break;
1284 if (swapdev->swap_map[toff] == SWAP_MAP_BAD)
1285 break;
1286 toff++;
1287 ret++;
1288 } while (--i);
1289 swap_device_unlock(swapdev);
1290 return ret;