direct-io.hg

view xenolinux-2.4.21-pre4-sparse/mm/swapfile.c @ 239:6e72ef415573

bitkeeper revision 1.97 (3e5a5dbcRcV73TYDcXAlNFfJfUqd9g)

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