Optimize andes_clear_page() and andes_copy_page() with prefetch
[linux-2.6/linux-mips.git] / mm / swapfile.c
blob5d3a7f23e80232122e8c509dcacc2f0e10b59101
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/malloc.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;
23 struct swap_list_t swap_list = {-1, -1};
25 struct swap_info_struct swap_info[MAX_SWAPFILES];
27 #define SWAPFILE_CLUSTER 256
29 static inline int scan_swap_map(struct swap_info_struct *si, unsigned short count)
31 unsigned long offset;
32 /*
33 * We try to cluster swap pages by allocating them
34 * sequentially in swap. Once we've allocated
35 * SWAPFILE_CLUSTER pages this way, however, we resort to
36 * first-free allocation, starting a new cluster. This
37 * prevents us from scattering swap pages all over the entire
38 * swap partition, so that we reduce overall disk seek times
39 * between swap pages. -- sct */
40 if (si->cluster_nr) {
41 while (si->cluster_next <= si->highest_bit) {
42 offset = si->cluster_next++;
43 if (si->swap_map[offset])
44 continue;
45 si->cluster_nr--;
46 goto got_page;
49 si->cluster_nr = SWAPFILE_CLUSTER;
51 /* try to find an empty (even not aligned) cluster. */
52 offset = si->lowest_bit;
53 check_next_cluster:
54 if (offset+SWAPFILE_CLUSTER-1 <= si->highest_bit)
56 int nr;
57 for (nr = offset; nr < offset+SWAPFILE_CLUSTER; nr++)
58 if (si->swap_map[nr])
60 offset = nr+1;
61 goto check_next_cluster;
63 /* We found a completly empty cluster, so start
64 * using it.
66 goto got_page;
68 /* No luck, so now go finegrined as usual. -Andrea */
69 for (offset = si->lowest_bit; offset <= si->highest_bit ; offset++) {
70 if (si->swap_map[offset])
71 continue;
72 got_page:
73 if (offset == si->lowest_bit)
74 si->lowest_bit++;
75 if (offset == si->highest_bit)
76 si->highest_bit--;
77 si->swap_map[offset] = count;
78 nr_swap_pages--;
79 si->cluster_next = offset+1;
80 return offset;
82 return 0;
85 swp_entry_t __get_swap_page(unsigned short count)
87 struct swap_info_struct * p;
88 unsigned long offset;
89 swp_entry_t entry;
90 int type, wrapped = 0;
92 entry.val = 0; /* Out of memory */
93 if (count >= SWAP_MAP_MAX)
94 goto bad_count;
95 swap_list_lock();
96 type = swap_list.next;
97 if (type < 0)
98 goto out;
99 if (nr_swap_pages == 0)
100 goto out;
102 while (1) {
103 p = &swap_info[type];
104 if ((p->flags & SWP_WRITEOK) == SWP_WRITEOK) {
105 swap_device_lock(p);
106 offset = scan_swap_map(p, count);
107 swap_device_unlock(p);
108 if (offset) {
109 entry = SWP_ENTRY(type,offset);
110 type = swap_info[type].next;
111 if (type < 0 ||
112 p->prio != swap_info[type].prio) {
113 swap_list.next = swap_list.head;
114 } else {
115 swap_list.next = type;
117 goto out;
120 type = p->next;
121 if (!wrapped) {
122 if (type < 0 || p->prio != swap_info[type].prio) {
123 type = swap_list.head;
124 wrapped = 1;
126 } else
127 if (type < 0)
128 goto out; /* out of swap space */
130 out:
131 swap_list_unlock();
132 return entry;
134 bad_count:
135 printk(KERN_ERR "get_swap_page: bad count %hd from %p\n",
136 count, __builtin_return_address(0));
137 goto out;
142 * Caller has made sure that the swapdevice corresponding to entry
143 * is still around or has not been recycled.
145 void __swap_free(swp_entry_t entry, unsigned short count)
147 struct swap_info_struct * p;
148 unsigned long offset, type;
150 if (!entry.val)
151 goto out;
153 type = SWP_TYPE(entry);
154 if (type >= nr_swapfiles)
155 goto bad_nofile;
156 p = & swap_info[type];
157 if (!(p->flags & SWP_USED))
158 goto bad_device;
159 offset = SWP_OFFSET(entry);
160 if (offset >= p->max)
161 goto bad_offset;
162 if (!p->swap_map[offset])
163 goto bad_free;
164 swap_list_lock();
165 if (p->prio > swap_info[swap_list.next].prio)
166 swap_list.next = type;
167 swap_device_lock(p);
168 if (p->swap_map[offset] < SWAP_MAP_MAX) {
169 if (p->swap_map[offset] < count)
170 goto bad_count;
171 if (!(p->swap_map[offset] -= count)) {
172 if (offset < p->lowest_bit)
173 p->lowest_bit = offset;
174 if (offset > p->highest_bit)
175 p->highest_bit = offset;
176 nr_swap_pages++;
179 swap_device_unlock(p);
180 swap_list_unlock();
181 out:
182 return;
184 bad_nofile:
185 printk("swap_free: Trying to free nonexistent swap-page\n");
186 goto out;
187 bad_device:
188 printk("swap_free: Trying to free swap from unused swap-device\n");
189 goto out;
190 bad_offset:
191 printk("swap_free: offset exceeds max\n");
192 goto out;
193 bad_free:
194 printk("VM: Bad swap entry %08lx\n", entry.val);
195 goto out;
196 bad_count:
197 swap_device_unlock(p);
198 swap_list_unlock();
199 printk(KERN_ERR "VM: Bad count %hd current count %hd\n", count, p->swap_map[offset]);
200 goto out;
204 * The swap entry has been read in advance, and we return 1 to indicate
205 * that the page has been used or is no longer needed.
207 * Always set the resulting pte to be nowrite (the same as COW pages
208 * after one process has exited). We don't know just how many PTEs will
209 * share this swap entry, so be cautious and let do_wp_page work out
210 * what to do if a write is requested later.
212 static inline void unuse_pte(struct vm_area_struct * vma, unsigned long address,
213 pte_t *dir, swp_entry_t entry, struct page* page)
215 pte_t pte = *dir;
217 if (pte_none(pte))
218 return;
219 if (pte_present(pte)) {
220 /* If this entry is swap-cached, then page must already
221 hold the right address for any copies in physical
222 memory */
223 if (pte_page(pte) != page)
224 return;
225 /* We will be removing the swap cache in a moment, so... */
226 set_pte(dir, pte_mkdirty(pte));
227 return;
229 if (pte_val(pte) != entry.val)
230 return;
231 set_pte(dir, pte_mkdirty(mk_pte(page, vma->vm_page_prot)));
232 swap_free(entry);
233 get_page(page);
234 ++vma->vm_mm->rss;
237 static inline void unuse_pmd(struct vm_area_struct * vma, pmd_t *dir,
238 unsigned long address, unsigned long size, unsigned long offset,
239 swp_entry_t entry, struct page* page)
241 pte_t * pte;
242 unsigned long end;
244 if (pmd_none(*dir))
245 return;
246 if (pmd_bad(*dir)) {
247 pmd_ERROR(*dir);
248 pmd_clear(dir);
249 return;
251 pte = pte_offset(dir, address);
252 offset += address & PMD_MASK;
253 address &= ~PMD_MASK;
254 end = address + size;
255 if (end > PMD_SIZE)
256 end = PMD_SIZE;
257 do {
258 unuse_pte(vma, offset+address-vma->vm_start, pte, entry, page);
259 address += PAGE_SIZE;
260 pte++;
261 } while (address && (address < end));
264 static inline void unuse_pgd(struct vm_area_struct * vma, pgd_t *dir,
265 unsigned long address, unsigned long size,
266 swp_entry_t entry, struct page* page)
268 pmd_t * pmd;
269 unsigned long offset, end;
271 if (pgd_none(*dir))
272 return;
273 if (pgd_bad(*dir)) {
274 pgd_ERROR(*dir);
275 pgd_clear(dir);
276 return;
278 pmd = pmd_offset(dir, address);
279 offset = address & PGDIR_MASK;
280 address &= ~PGDIR_MASK;
281 end = address + size;
282 if (end > PGDIR_SIZE)
283 end = PGDIR_SIZE;
284 if (address >= end)
285 BUG();
286 do {
287 unuse_pmd(vma, pmd, address, end - address, offset, entry,
288 page);
289 address = (address + PMD_SIZE) & PMD_MASK;
290 pmd++;
291 } while (address && (address < end));
294 static void unuse_vma(struct vm_area_struct * vma, pgd_t *pgdir,
295 swp_entry_t entry, struct page* page)
297 unsigned long start = vma->vm_start, end = vma->vm_end;
299 if (start >= end)
300 BUG();
301 do {
302 unuse_pgd(vma, pgdir, start, end - start, entry, page);
303 start = (start + PGDIR_SIZE) & PGDIR_MASK;
304 pgdir++;
305 } while (start && (start < end));
308 static void unuse_process(struct mm_struct * mm,
309 swp_entry_t entry, struct page* page)
311 struct vm_area_struct* vma;
314 * Go through process' page directory.
316 if (!mm)
317 return;
318 for (vma = mm->mmap; vma; vma = vma->vm_next) {
319 pgd_t * pgd = pgd_offset(mm, vma->vm_start);
320 unuse_vma(vma, pgd, entry, page);
322 return;
326 * We completely avoid races by reading each swap page in advance,
327 * and then search for the process using it. All the necessary
328 * page table adjustments can then be made atomically.
330 static int try_to_unuse(unsigned int type)
332 struct swap_info_struct * si = &swap_info[type];
333 struct task_struct *p;
334 struct page *page;
335 swp_entry_t entry;
336 int i;
338 while (1) {
340 * Find a swap page in use and read it in.
342 swap_device_lock(si);
343 for (i = 1; i < si->max ; i++) {
344 if (si->swap_map[i] > 0 && si->swap_map[i] != SWAP_MAP_BAD) {
346 * Prevent swaphandle from being completely
347 * unused by swap_free while we are trying
348 * to read in the page - this prevents warning
349 * messages from rw_swap_page_base.
351 if (si->swap_map[i] != SWAP_MAP_MAX)
352 si->swap_map[i]++;
353 swap_device_unlock(si);
354 goto found_entry;
357 swap_device_unlock(si);
358 break;
360 found_entry:
361 entry = SWP_ENTRY(type, i);
363 /* Get a page for the entry, using the existing swap
364 cache page if there is one. Otherwise, get a clean
365 page and read the swap into it. */
366 page = read_swap_cache(entry);
367 if (!page) {
368 swap_free(entry);
369 return -ENOMEM;
371 read_lock(&tasklist_lock);
372 for_each_task(p)
373 unuse_process(p->mm, entry, page);
374 read_unlock(&tasklist_lock);
375 shm_unuse(entry, page);
376 /* Now get rid of the extra reference to the temporary
377 page we've been using. */
378 if (PageSwapCache(page))
379 delete_from_swap_cache(page);
380 page_cache_release(page);
382 * Check for and clear any overflowed swap map counts.
384 swap_free(entry);
385 swap_list_lock();
386 swap_device_lock(si);
387 if (si->swap_map[i] > 0) {
388 if (si->swap_map[i] != SWAP_MAP_MAX)
389 printk("VM: Undead swap entry %08lx\n",
390 entry.val);
391 nr_swap_pages++;
392 si->swap_map[i] = 0;
394 swap_device_unlock(si);
395 swap_list_unlock();
397 return 0;
400 asmlinkage long sys_swapoff(const char * specialfile)
402 struct swap_info_struct * p = NULL;
403 struct nameidata nd;
404 int i, type, prev;
405 int err;
407 if (!capable(CAP_SYS_ADMIN))
408 return -EPERM;
410 err = user_path_walk(specialfile, &nd);
411 if (err)
412 goto out;
414 lock_kernel();
415 prev = -1;
416 swap_list_lock();
417 for (type = swap_list.head; type >= 0; type = swap_info[type].next) {
418 p = swap_info + type;
419 if ((p->flags & SWP_WRITEOK) == SWP_WRITEOK) {
420 if (p->swap_file) {
421 if (p->swap_file == nd.dentry)
422 break;
423 } else {
424 if (S_ISBLK(nd.dentry->d_inode->i_mode)
425 && (p->swap_device == nd.dentry->d_inode->i_rdev))
426 break;
429 prev = type;
431 err = -EINVAL;
432 if (type < 0) {
433 swap_list_unlock();
434 goto out_dput;
437 if (prev < 0) {
438 swap_list.head = p->next;
439 } else {
440 swap_info[prev].next = p->next;
442 if (type == swap_list.next) {
443 /* just pick something that's safe... */
444 swap_list.next = swap_list.head;
446 nr_swap_pages -= p->pages;
447 swap_list_unlock();
448 p->flags = SWP_USED;
449 err = try_to_unuse(type);
450 if (err) {
451 /* re-insert swap space back into swap_list */
452 swap_list_lock();
453 for (prev = -1, i = swap_list.head; i >= 0; prev = i, i = swap_info[i].next)
454 if (p->prio >= swap_info[i].prio)
455 break;
456 p->next = i;
457 if (prev < 0)
458 swap_list.head = swap_list.next = p - swap_info;
459 else
460 swap_info[prev].next = p - swap_info;
461 nr_swap_pages += p->pages;
462 swap_list_unlock();
463 p->flags = SWP_WRITEOK;
464 goto out_dput;
466 if (p->swap_device)
467 blkdev_put(nd.dentry->d_inode->i_bdev, BDEV_SWAP);
468 path_release(&nd);
470 nd.dentry = p->swap_file;
471 p->swap_file = NULL;
472 nd.mnt = p->swap_vfsmnt;
473 p->swap_vfsmnt = NULL;
474 p->swap_device = 0;
475 vfree(p->swap_map);
476 p->swap_map = NULL;
477 p->flags = 0;
478 err = 0;
480 out_dput:
481 unlock_kernel();
482 path_release(&nd);
483 out:
484 return err;
487 int get_swaparea_info(char *buf)
489 char * page = (char *) __get_free_page(GFP_KERNEL);
490 struct swap_info_struct *ptr = swap_info;
491 int i, j, len = 0, usedswap;
493 if (!page)
494 return -ENOMEM;
496 len += sprintf(buf, "Filename\t\t\tType\t\tSize\tUsed\tPriority\n");
497 for (i = 0 ; i < nr_swapfiles ; i++, ptr++) {
498 if (ptr->flags & SWP_USED) {
499 char * path = d_path(ptr->swap_file, ptr->swap_vfsmnt,
500 page, PAGE_SIZE);
502 len += sprintf(buf + len, "%-31s ", path);
504 if (!ptr->swap_device)
505 len += sprintf(buf + len, "file\t\t");
506 else
507 len += sprintf(buf + len, "partition\t");
509 usedswap = 0;
510 for (j = 0; j < ptr->max; ++j)
511 switch (ptr->swap_map[j]) {
512 case SWAP_MAP_BAD:
513 case 0:
514 continue;
515 default:
516 usedswap++;
518 len += sprintf(buf + len, "%d\t%d\t%d\n", ptr->pages << (PAGE_SHIFT - 10),
519 usedswap << (PAGE_SHIFT - 10), ptr->prio);
522 free_page((unsigned long) page);
523 return len;
526 int is_swap_partition(kdev_t dev) {
527 struct swap_info_struct *ptr = swap_info;
528 int i;
530 for (i = 0 ; i < nr_swapfiles ; i++, ptr++) {
531 if (ptr->flags & SWP_USED)
532 if (ptr->swap_device == dev)
533 return 1;
535 return 0;
539 * Written 01/25/92 by Simmule Turner, heavily changed by Linus.
541 * The swapon system call
543 asmlinkage long sys_swapon(const char * specialfile, int swap_flags)
545 struct swap_info_struct * p;
546 struct nameidata nd;
547 struct inode * swap_inode;
548 unsigned int type;
549 int i, j, prev;
550 int error;
551 static int least_priority = 0;
552 union swap_header *swap_header = 0;
553 int swap_header_version;
554 int nr_good_pages = 0;
555 unsigned long maxpages;
556 int swapfilesize;
557 struct block_device *bdev = NULL;
559 if (!capable(CAP_SYS_ADMIN))
560 return -EPERM;
561 lock_kernel();
562 p = swap_info;
563 for (type = 0 ; type < nr_swapfiles ; type++,p++)
564 if (!(p->flags & SWP_USED))
565 break;
566 error = -EPERM;
567 if (type >= MAX_SWAPFILES)
568 goto out;
569 if (type >= nr_swapfiles)
570 nr_swapfiles = type+1;
571 p->flags = SWP_USED;
572 p->swap_file = NULL;
573 p->swap_vfsmnt = NULL;
574 p->swap_device = 0;
575 p->swap_map = NULL;
576 p->lowest_bit = 0;
577 p->highest_bit = 0;
578 p->cluster_nr = 0;
579 p->sdev_lock = SPIN_LOCK_UNLOCKED;
580 p->max = 1;
581 p->next = -1;
582 if (swap_flags & SWAP_FLAG_PREFER) {
583 p->prio =
584 (swap_flags & SWAP_FLAG_PRIO_MASK)>>SWAP_FLAG_PRIO_SHIFT;
585 } else {
586 p->prio = --least_priority;
588 error = user_path_walk(specialfile, &nd);
589 if (error)
590 goto bad_swap_2;
592 p->swap_file = nd.dentry;
593 p->swap_vfsmnt = nd.mnt;
594 swap_inode = nd.dentry->d_inode;
595 error = -EINVAL;
597 if (S_ISBLK(swap_inode->i_mode)) {
598 kdev_t dev = swap_inode->i_rdev;
599 struct block_device_operations *bdops;
601 p->swap_device = dev;
602 set_blocksize(dev, PAGE_SIZE);
604 bdev = swap_inode->i_bdev;
605 bdops = devfs_get_ops(devfs_get_handle_from_inode(swap_inode));
606 if (bdops) bdev->bd_op = bdops;
608 error = blkdev_get(bdev, FMODE_READ|FMODE_WRITE, 0, BDEV_SWAP);
609 if (error)
610 goto bad_swap_2;
611 set_blocksize(dev, PAGE_SIZE);
612 error = -ENODEV;
613 if (!dev || (blk_size[MAJOR(dev)] &&
614 !blk_size[MAJOR(dev)][MINOR(dev)]))
615 goto bad_swap;
616 error = -EBUSY;
617 for (i = 0 ; i < nr_swapfiles ; i++) {
618 if (i == type)
619 continue;
620 if (dev == swap_info[i].swap_device)
621 goto bad_swap;
623 swapfilesize = 0;
624 if (blk_size[MAJOR(dev)])
625 swapfilesize = blk_size[MAJOR(dev)][MINOR(dev)]
626 >> (PAGE_SHIFT - 10);
627 } else if (S_ISREG(swap_inode->i_mode)) {
628 error = -EBUSY;
629 for (i = 0 ; i < nr_swapfiles ; i++) {
630 if (i == type || !swap_info[i].swap_file)
631 continue;
632 if (swap_inode == swap_info[i].swap_file->d_inode)
633 goto bad_swap;
635 swapfilesize = swap_inode->i_size >> PAGE_SHIFT;
636 } else
637 goto bad_swap;
639 swap_header = (void *) __get_free_page(GFP_USER);
640 if (!swap_header) {
641 printk("Unable to start swapping: out of memory :-)\n");
642 error = -ENOMEM;
643 goto bad_swap;
646 lock_page(mem_map + MAP_NR(swap_header));
647 rw_swap_page_nolock(READ, SWP_ENTRY(type,0), (char *) swap_header, 1);
649 if (!memcmp("SWAP-SPACE",swap_header->magic.magic,10))
650 swap_header_version = 1;
651 else if (!memcmp("SWAPSPACE2",swap_header->magic.magic,10))
652 swap_header_version = 2;
653 else {
654 printk("Unable to find swap-space signature\n");
655 error = -EINVAL;
656 goto bad_swap;
659 switch (swap_header_version) {
660 case 1:
661 memset(((char *) swap_header)+PAGE_SIZE-10,0,10);
662 j = 0;
663 p->lowest_bit = 0;
664 p->highest_bit = 0;
665 for (i = 1 ; i < 8*PAGE_SIZE ; i++) {
666 if (test_bit(i,(char *) swap_header)) {
667 if (!p->lowest_bit)
668 p->lowest_bit = i;
669 p->highest_bit = i;
670 p->max = i+1;
671 j++;
674 nr_good_pages = j;
675 p->swap_map = vmalloc(p->max * sizeof(short));
676 if (!p->swap_map) {
677 error = -ENOMEM;
678 goto bad_swap;
680 for (i = 1 ; i < p->max ; i++) {
681 if (test_bit(i,(char *) swap_header))
682 p->swap_map[i] = 0;
683 else
684 p->swap_map[i] = SWAP_MAP_BAD;
686 break;
688 case 2:
689 /* Check the swap header's sub-version and the size of
690 the swap file and bad block lists */
691 if (swap_header->info.version != 1) {
692 printk(KERN_WARNING
693 "Unable to handle swap header version %d\n",
694 swap_header->info.version);
695 error = -EINVAL;
696 goto bad_swap;
699 p->lowest_bit = 1;
700 p->highest_bit = swap_header->info.last_page - 1;
701 p->max = swap_header->info.last_page;
703 maxpages = SWP_OFFSET(SWP_ENTRY(0,~0UL));
704 if (p->max >= maxpages)
705 p->max = maxpages-1;
707 error = -EINVAL;
708 if (swap_header->info.nr_badpages > MAX_SWAP_BADPAGES)
709 goto bad_swap;
711 /* OK, set up the swap map and apply the bad block list */
712 if (!(p->swap_map = vmalloc (p->max * sizeof(short)))) {
713 error = -ENOMEM;
714 goto bad_swap;
717 error = 0;
718 memset(p->swap_map, 0, p->max * sizeof(short));
719 for (i=0; i<swap_header->info.nr_badpages; i++) {
720 int page = swap_header->info.badpages[i];
721 if (page <= 0 || page >= swap_header->info.last_page)
722 error = -EINVAL;
723 else
724 p->swap_map[page] = SWAP_MAP_BAD;
726 nr_good_pages = swap_header->info.last_page -
727 swap_header->info.nr_badpages -
728 1 /* header page */;
729 if (error)
730 goto bad_swap;
733 if (swapfilesize && p->max > swapfilesize) {
734 printk(KERN_WARNING
735 "Swap area shorter than signature indicates\n");
736 error = -EINVAL;
737 goto bad_swap;
739 if (!nr_good_pages) {
740 printk(KERN_WARNING "Empty swap-file\n");
741 error = -EINVAL;
742 goto bad_swap;
744 p->swap_map[0] = SWAP_MAP_BAD;
745 p->flags = SWP_WRITEOK;
746 p->pages = nr_good_pages;
747 swap_list_lock();
748 nr_swap_pages += nr_good_pages;
749 printk(KERN_INFO "Adding Swap: %dk swap-space (priority %d)\n",
750 nr_good_pages<<(PAGE_SHIFT-10), p->prio);
752 /* insert swap space into swap_list: */
753 prev = -1;
754 for (i = swap_list.head; i >= 0; i = swap_info[i].next) {
755 if (p->prio >= swap_info[i].prio) {
756 break;
758 prev = i;
760 p->next = i;
761 if (prev < 0) {
762 swap_list.head = swap_list.next = p - swap_info;
763 } else {
764 swap_info[prev].next = p - swap_info;
766 swap_list_unlock();
767 error = 0;
768 goto out;
769 bad_swap:
770 if (bdev)
771 blkdev_put(bdev, BDEV_SWAP);
772 bad_swap_2:
773 if (p->swap_map)
774 vfree(p->swap_map);
775 nd.mnt = p->swap_vfsmnt;
776 nd.dentry = p->swap_file;
777 p->swap_device = 0;
778 p->swap_file = NULL;
779 p->swap_vfsmnt = NULL;
780 p->swap_map = NULL;
781 p->flags = 0;
782 if (!(swap_flags & SWAP_FLAG_PREFER))
783 ++least_priority;
784 path_release(&nd);
785 out:
786 if (swap_header)
787 free_page((long) swap_header);
788 unlock_kernel();
789 return error;
792 void si_swapinfo(struct sysinfo *val)
794 unsigned int i;
795 unsigned long freeswap = 0;
796 unsigned long totalswap = 0;
798 for (i = 0; i < nr_swapfiles; i++) {
799 unsigned int j;
800 if ((swap_info[i].flags & SWP_WRITEOK) != SWP_WRITEOK)
801 continue;
802 for (j = 0; j < swap_info[i].max; ++j) {
803 switch (swap_info[i].swap_map[j]) {
804 case SWAP_MAP_BAD:
805 continue;
806 case 0:
807 freeswap++;
808 default:
809 totalswap++;
813 val->freeswap = freeswap;
814 val->totalswap = totalswap;
815 return;
819 * Verify that a swap entry is valid and increment its swap map count.
820 * Kernel_lock is held, which guarantees existance of swap device.
822 * Note: if swap_map[] reaches SWAP_MAP_MAX the entries are treated as
823 * "permanent", but will be reclaimed by the next swapoff.
825 int swap_duplicate(swp_entry_t entry)
827 struct swap_info_struct * p;
828 unsigned long offset, type;
829 int result = 0;
831 /* Swap entry 0 is illegal */
832 if (!entry.val)
833 goto out;
834 type = SWP_TYPE(entry);
835 if (type >= nr_swapfiles)
836 goto bad_file;
837 p = type + swap_info;
838 offset = SWP_OFFSET(entry);
839 if (offset >= p->max)
840 goto bad_offset;
841 if (!p->swap_map[offset])
842 goto bad_unused;
844 * Entry is valid, so increment the map count.
846 swap_device_lock(p);
847 if (p->swap_map[offset] < SWAP_MAP_MAX)
848 p->swap_map[offset]++;
849 else {
850 static int overflow = 0;
851 if (overflow++ < 5)
852 printk("VM: swap entry overflow\n");
853 p->swap_map[offset] = SWAP_MAP_MAX;
855 swap_device_unlock(p);
856 result = 1;
857 out:
858 return result;
860 bad_file:
861 printk("Bad swap file entry %08lx\n", entry.val);
862 goto out;
863 bad_offset:
864 printk("Bad swap offset entry %08lx\n", entry.val);
865 goto out;
866 bad_unused:
867 printk("Unused swap offset entry in swap_dup %08lx\n", entry.val);
868 goto out;
872 * Page lock needs to be held in all cases to prevent races with
873 * swap file deletion.
875 int swap_count(struct page *page)
877 struct swap_info_struct * p;
878 unsigned long offset, type;
879 swp_entry_t entry;
880 int retval = 0;
882 entry.val = page->index;
883 if (!entry.val)
884 goto bad_entry;
885 type = SWP_TYPE(entry);
886 if (type >= nr_swapfiles)
887 goto bad_file;
888 p = type + swap_info;
889 offset = SWP_OFFSET(entry);
890 if (offset >= p->max)
891 goto bad_offset;
892 if (!p->swap_map[offset])
893 goto bad_unused;
894 retval = p->swap_map[offset];
895 out:
896 return retval;
898 bad_entry:
899 printk(KERN_ERR "swap_count: null entry!\n");
900 goto out;
901 bad_file:
902 printk("Bad swap file entry %08lx\n", entry.val);
903 goto out;
904 bad_offset:
905 printk("Bad swap offset entry %08lx\n", entry.val);
906 goto out;
907 bad_unused:
908 printk("Unused swap offset entry in swap_count %08lx\n", entry.val);
909 goto out;
913 * Kernel_lock protects against swap device deletion.
915 void get_swaphandle_info(swp_entry_t entry, unsigned long *offset,
916 kdev_t *dev, struct inode **swapf)
918 unsigned long type;
919 struct swap_info_struct *p;
921 type = SWP_TYPE(entry);
922 if (type >= nr_swapfiles) {
923 printk("Internal error: bad swap-device\n");
924 return;
927 p = &swap_info[type];
928 *offset = SWP_OFFSET(entry);
929 if (*offset >= p->max) {
930 printk("rw_swap_page: weirdness\n");
931 return;
933 if (p->swap_map && !p->swap_map[*offset]) {
934 printk("VM: Bad swap entry %08lx\n", entry.val);
935 return;
937 if (!(p->flags & SWP_USED)) {
938 printk(KERN_ERR "rw_swap_page: "
939 "Trying to swap to unused swap-device\n");
940 return;
943 if (p->swap_device) {
944 *dev = p->swap_device;
945 } else if (p->swap_file) {
946 *swapf = p->swap_file->d_inode;
947 } else {
948 printk(KERN_ERR "rw_swap_page: no swap file or device\n");
950 return;
954 * Kernel_lock protects against swap device deletion. Grab an extra
955 * reference on the swaphandle so that it dos not become unused.
957 int valid_swaphandles(swp_entry_t entry, unsigned long *offset)
959 int ret = 0, i = 1 << page_cluster;
960 unsigned long toff;
961 struct swap_info_struct *swapdev = SWP_TYPE(entry) + swap_info;
963 *offset = SWP_OFFSET(entry);
964 toff = *offset = (*offset >> page_cluster) << page_cluster;
966 swap_device_lock(swapdev);
967 do {
968 /* Don't read-ahead past the end of the swap area */
969 if (toff >= swapdev->max)
970 break;
971 /* Don't read in bad or busy pages */
972 if (!swapdev->swap_map[toff])
973 break;
974 if (swapdev->swap_map[toff] == SWAP_MAP_BAD)
975 break;
976 swapdev->swap_map[toff]++;
977 toff++;
978 ret++;
979 } while (--i);
980 swap_device_unlock(swapdev);
981 return ret;