Merge with 2.3.99-pre9.
[linux-2.6/linux-mips.git] / mm / swapfile.c
blob55ef476a38a077f24aa18a5f70dbbdebfc6c5bc7
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 lock_kernel();
411 err = user_path_walk(specialfile, &nd);
412 if (err)
413 goto out;
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 path_release(&nd);
482 out:
483 unlock_kernel();
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;
558 char *name;
560 if (!capable(CAP_SYS_ADMIN))
561 return -EPERM;
562 lock_kernel();
563 p = swap_info;
564 for (type = 0 ; type < nr_swapfiles ; type++,p++)
565 if (!(p->flags & SWP_USED))
566 break;
567 error = -EPERM;
568 if (type >= MAX_SWAPFILES)
569 goto out;
570 if (type >= nr_swapfiles)
571 nr_swapfiles = type+1;
572 p->flags = SWP_USED;
573 p->swap_file = NULL;
574 p->swap_vfsmnt = NULL;
575 p->swap_device = 0;
576 p->swap_map = NULL;
577 p->lowest_bit = 0;
578 p->highest_bit = 0;
579 p->cluster_nr = 0;
580 p->sdev_lock = SPIN_LOCK_UNLOCKED;
581 p->max = 1;
582 p->next = -1;
583 if (swap_flags & SWAP_FLAG_PREFER) {
584 p->prio =
585 (swap_flags & SWAP_FLAG_PRIO_MASK)>>SWAP_FLAG_PRIO_SHIFT;
586 } else {
587 p->prio = --least_priority;
589 name = getname(specialfile);
590 error = PTR_ERR(name);
591 if (IS_ERR(name))
592 goto bad_swap_2;
593 error = 0;
594 if (path_init(name, LOOKUP_FOLLOW|LOOKUP_POSITIVE, &nd))
595 error = path_walk(name, &nd);
596 putname(name);
597 if (error)
598 goto bad_swap_2;
600 p->swap_file = nd.dentry;
601 p->swap_vfsmnt = nd.mnt;
602 swap_inode = nd.dentry->d_inode;
603 error = -EINVAL;
605 if (S_ISBLK(swap_inode->i_mode)) {
606 kdev_t dev = swap_inode->i_rdev;
607 struct block_device_operations *bdops;
609 p->swap_device = dev;
610 set_blocksize(dev, PAGE_SIZE);
612 bdev = swap_inode->i_bdev;
613 bdops = devfs_get_ops(devfs_get_handle_from_inode(swap_inode));
614 if (bdops) bdev->bd_op = bdops;
616 error = blkdev_get(bdev, FMODE_READ|FMODE_WRITE, 0, BDEV_SWAP);
617 if (error)
618 goto bad_swap_2;
619 set_blocksize(dev, PAGE_SIZE);
620 error = -ENODEV;
621 if (!dev || (blk_size[MAJOR(dev)] &&
622 !blk_size[MAJOR(dev)][MINOR(dev)]))
623 goto bad_swap;
624 error = -EBUSY;
625 for (i = 0 ; i < nr_swapfiles ; i++) {
626 if (i == type)
627 continue;
628 if (dev == swap_info[i].swap_device)
629 goto bad_swap;
631 swapfilesize = 0;
632 if (blk_size[MAJOR(dev)])
633 swapfilesize = blk_size[MAJOR(dev)][MINOR(dev)]
634 >> (PAGE_SHIFT - 10);
635 } else if (S_ISREG(swap_inode->i_mode)) {
636 error = -EBUSY;
637 for (i = 0 ; i < nr_swapfiles ; i++) {
638 if (i == type || !swap_info[i].swap_file)
639 continue;
640 if (swap_inode == swap_info[i].swap_file->d_inode)
641 goto bad_swap;
643 swapfilesize = swap_inode->i_size >> PAGE_SHIFT;
644 } else
645 goto bad_swap;
647 swap_header = (void *) __get_free_page(GFP_USER);
648 if (!swap_header) {
649 printk("Unable to start swapping: out of memory :-)\n");
650 error = -ENOMEM;
651 goto bad_swap;
654 lock_page(mem_map + MAP_NR(swap_header));
655 rw_swap_page_nolock(READ, SWP_ENTRY(type,0), (char *) swap_header, 1);
657 if (!memcmp("SWAP-SPACE",swap_header->magic.magic,10))
658 swap_header_version = 1;
659 else if (!memcmp("SWAPSPACE2",swap_header->magic.magic,10))
660 swap_header_version = 2;
661 else {
662 printk("Unable to find swap-space signature\n");
663 error = -EINVAL;
664 goto bad_swap;
667 switch (swap_header_version) {
668 case 1:
669 memset(((char *) swap_header)+PAGE_SIZE-10,0,10);
670 j = 0;
671 p->lowest_bit = 0;
672 p->highest_bit = 0;
673 for (i = 1 ; i < 8*PAGE_SIZE ; i++) {
674 if (test_bit(i,(char *) swap_header)) {
675 if (!p->lowest_bit)
676 p->lowest_bit = i;
677 p->highest_bit = i;
678 p->max = i+1;
679 j++;
682 nr_good_pages = j;
683 p->swap_map = vmalloc(p->max * sizeof(short));
684 if (!p->swap_map) {
685 error = -ENOMEM;
686 goto bad_swap;
688 for (i = 1 ; i < p->max ; i++) {
689 if (test_bit(i,(char *) swap_header))
690 p->swap_map[i] = 0;
691 else
692 p->swap_map[i] = SWAP_MAP_BAD;
694 break;
696 case 2:
697 /* Check the swap header's sub-version and the size of
698 the swap file and bad block lists */
699 if (swap_header->info.version != 1) {
700 printk(KERN_WARNING
701 "Unable to handle swap header version %d\n",
702 swap_header->info.version);
703 error = -EINVAL;
704 goto bad_swap;
707 p->lowest_bit = 1;
708 p->highest_bit = swap_header->info.last_page - 1;
709 p->max = swap_header->info.last_page;
711 maxpages = SWP_OFFSET(SWP_ENTRY(0,~0UL));
712 if (p->max >= maxpages)
713 p->max = maxpages-1;
715 error = -EINVAL;
716 if (swap_header->info.nr_badpages > MAX_SWAP_BADPAGES)
717 goto bad_swap;
719 /* OK, set up the swap map and apply the bad block list */
720 if (!(p->swap_map = vmalloc (p->max * sizeof(short)))) {
721 error = -ENOMEM;
722 goto bad_swap;
725 error = 0;
726 memset(p->swap_map, 0, p->max * sizeof(short));
727 for (i=0; i<swap_header->info.nr_badpages; i++) {
728 int page = swap_header->info.badpages[i];
729 if (page <= 0 || page >= swap_header->info.last_page)
730 error = -EINVAL;
731 else
732 p->swap_map[page] = SWAP_MAP_BAD;
734 nr_good_pages = swap_header->info.last_page -
735 swap_header->info.nr_badpages -
736 1 /* header page */;
737 if (error)
738 goto bad_swap;
741 if (swapfilesize && p->max > swapfilesize) {
742 printk(KERN_WARNING
743 "Swap area shorter than signature indicates\n");
744 error = -EINVAL;
745 goto bad_swap;
747 if (!nr_good_pages) {
748 printk(KERN_WARNING "Empty swap-file\n");
749 error = -EINVAL;
750 goto bad_swap;
752 p->swap_map[0] = SWAP_MAP_BAD;
753 p->flags = SWP_WRITEOK;
754 p->pages = nr_good_pages;
755 swap_list_lock();
756 nr_swap_pages += nr_good_pages;
757 printk(KERN_INFO "Adding Swap: %dk swap-space (priority %d)\n",
758 nr_good_pages<<(PAGE_SHIFT-10), p->prio);
760 /* insert swap space into swap_list: */
761 prev = -1;
762 for (i = swap_list.head; i >= 0; i = swap_info[i].next) {
763 if (p->prio >= swap_info[i].prio) {
764 break;
766 prev = i;
768 p->next = i;
769 if (prev < 0) {
770 swap_list.head = swap_list.next = p - swap_info;
771 } else {
772 swap_info[prev].next = p - swap_info;
774 swap_list_unlock();
775 error = 0;
776 goto out;
777 bad_swap:
778 if (bdev)
779 blkdev_put(bdev, BDEV_SWAP);
780 bad_swap_2:
781 if (p->swap_map)
782 vfree(p->swap_map);
783 nd.mnt = p->swap_vfsmnt;
784 nd.dentry = p->swap_file;
785 p->swap_device = 0;
786 p->swap_file = NULL;
787 p->swap_vfsmnt = NULL;
788 p->swap_map = NULL;
789 p->flags = 0;
790 if (!(swap_flags & SWAP_FLAG_PREFER))
791 ++least_priority;
792 path_release(&nd);
793 out:
794 if (swap_header)
795 free_page((long) swap_header);
796 unlock_kernel();
797 return error;
800 void si_swapinfo(struct sysinfo *val)
802 unsigned int i;
803 unsigned long freeswap = 0;
804 unsigned long totalswap = 0;
806 for (i = 0; i < nr_swapfiles; i++) {
807 unsigned int j;
808 if ((swap_info[i].flags & SWP_WRITEOK) != SWP_WRITEOK)
809 continue;
810 for (j = 0; j < swap_info[i].max; ++j) {
811 switch (swap_info[i].swap_map[j]) {
812 case SWAP_MAP_BAD:
813 continue;
814 case 0:
815 freeswap++;
816 default:
817 totalswap++;
821 val->freeswap = freeswap;
822 val->totalswap = totalswap;
823 return;
827 * Verify that a swap entry is valid and increment its swap map count.
828 * Kernel_lock is held, which guarantees existance of swap device.
830 * Note: if swap_map[] reaches SWAP_MAP_MAX the entries are treated as
831 * "permanent", but will be reclaimed by the next swapoff.
833 int swap_duplicate(swp_entry_t entry)
835 struct swap_info_struct * p;
836 unsigned long offset, type;
837 int result = 0;
839 /* Swap entry 0 is illegal */
840 if (!entry.val)
841 goto out;
842 type = SWP_TYPE(entry);
843 if (type >= nr_swapfiles)
844 goto bad_file;
845 p = type + swap_info;
846 offset = SWP_OFFSET(entry);
847 if (offset >= p->max)
848 goto bad_offset;
849 if (!p->swap_map[offset])
850 goto bad_unused;
852 * Entry is valid, so increment the map count.
854 swap_device_lock(p);
855 if (p->swap_map[offset] < SWAP_MAP_MAX)
856 p->swap_map[offset]++;
857 else {
858 static int overflow = 0;
859 if (overflow++ < 5)
860 printk("VM: swap entry overflow\n");
861 p->swap_map[offset] = SWAP_MAP_MAX;
863 swap_device_unlock(p);
864 result = 1;
865 out:
866 return result;
868 bad_file:
869 printk("Bad swap file entry %08lx\n", entry.val);
870 goto out;
871 bad_offset:
872 printk("Bad swap offset entry %08lx\n", entry.val);
873 goto out;
874 bad_unused:
875 printk("Unused swap offset entry in swap_dup %08lx\n", entry.val);
876 goto out;
880 * Page lock needs to be held in all cases to prevent races with
881 * swap file deletion.
883 int swap_count(struct page *page)
885 struct swap_info_struct * p;
886 unsigned long offset, type;
887 swp_entry_t entry;
888 int retval = 0;
890 entry.val = page->index;
891 if (!entry.val)
892 goto bad_entry;
893 type = SWP_TYPE(entry);
894 if (type >= nr_swapfiles)
895 goto bad_file;
896 p = type + swap_info;
897 offset = SWP_OFFSET(entry);
898 if (offset >= p->max)
899 goto bad_offset;
900 if (!p->swap_map[offset])
901 goto bad_unused;
902 retval = p->swap_map[offset];
903 out:
904 return retval;
906 bad_entry:
907 printk(KERN_ERR "swap_count: null entry!\n");
908 goto out;
909 bad_file:
910 printk("Bad swap file entry %08lx\n", entry.val);
911 goto out;
912 bad_offset:
913 printk("Bad swap offset entry %08lx\n", entry.val);
914 goto out;
915 bad_unused:
916 printk("Unused swap offset entry in swap_count %08lx\n", entry.val);
917 goto out;
921 * Kernel_lock protects against swap device deletion.
923 void get_swaphandle_info(swp_entry_t entry, unsigned long *offset,
924 kdev_t *dev, struct inode **swapf)
926 unsigned long type;
927 struct swap_info_struct *p;
929 type = SWP_TYPE(entry);
930 if (type >= nr_swapfiles) {
931 printk("Internal error: bad swap-device\n");
932 return;
935 p = &swap_info[type];
936 *offset = SWP_OFFSET(entry);
937 if (*offset >= p->max) {
938 printk("rw_swap_page: weirdness\n");
939 return;
941 if (p->swap_map && !p->swap_map[*offset]) {
942 printk("VM: Bad swap entry %08lx\n", entry.val);
943 return;
945 if (!(p->flags & SWP_USED)) {
946 printk(KERN_ERR "rw_swap_page: "
947 "Trying to swap to unused swap-device\n");
948 return;
951 if (p->swap_device) {
952 *dev = p->swap_device;
953 } else if (p->swap_file) {
954 *swapf = p->swap_file->d_inode;
955 } else {
956 printk(KERN_ERR "rw_swap_page: no swap file or device\n");
958 return;
962 * Kernel_lock protects against swap device deletion. Grab an extra
963 * reference on the swaphandle so that it dos not become unused.
965 int valid_swaphandles(swp_entry_t entry, unsigned long *offset)
967 int ret = 0, i = 1 << page_cluster;
968 unsigned long toff;
969 struct swap_info_struct *swapdev = SWP_TYPE(entry) + swap_info;
971 *offset = SWP_OFFSET(entry);
972 toff = *offset = (*offset >> page_cluster) << page_cluster;
974 swap_device_lock(swapdev);
975 do {
976 /* Don't read-ahead past the end of the swap area */
977 if (toff >= swapdev->max)
978 break;
979 /* Don't read in bad or busy pages */
980 if (!swapdev->swap_map[toff])
981 break;
982 if (swapdev->swap_map[toff] == SWAP_MAP_BAD)
983 break;
984 swapdev->swap_map[toff]++;
985 toff++;
986 ret++;
987 } while (--i);
988 swap_device_unlock(swapdev);
989 return ret;