patch-2_6_7-vs1_9_1_12
[linux-2.6.git] / mm / swapfile.c
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  */
7
8 #include <linux/config.h>
9 #include <linux/mm.h>
10 #include <linux/hugetlb.h>
11 #include <linux/mman.h>
12 #include <linux/slab.h>
13 #include <linux/kernel_stat.h>
14 #include <linux/swap.h>
15 #include <linux/vmalloc.h>
16 #include <linux/pagemap.h>
17 #include <linux/namei.h>
18 #include <linux/shm.h>
19 #include <linux/blkdev.h>
20 #include <linux/writeback.h>
21 #include <linux/proc_fs.h>
22 #include <linux/seq_file.h>
23 #include <linux/init.h>
24 #include <linux/module.h>
25 #include <linux/rmap.h>
26 #include <linux/security.h>
27 #include <linux/backing-dev.h>
28
29 #include <asm/pgtable.h>
30 #include <asm/tlbflush.h>
31 #include <linux/swapops.h>
32 #include <linux/vs_base.h>
33
34 spinlock_t swaplock = SPIN_LOCK_UNLOCKED;
35 unsigned int nr_swapfiles;
36 int total_swap_pages;
37 static int swap_overflow;
38
39 EXPORT_SYMBOL(total_swap_pages);
40
41 static const char Bad_file[] = "Bad swap file entry ";
42 static const char Unused_file[] = "Unused swap file entry ";
43 static const char Bad_offset[] = "Bad swap offset entry ";
44 static const char Unused_offset[] = "Unused swap offset entry ";
45
46 struct swap_list_t swap_list = {-1, -1};
47
48 struct swap_info_struct swap_info[MAX_SWAPFILES];
49
50 static DECLARE_MUTEX(swapon_sem);
51
52 /*
53  * We need this because the bdev->unplug_fn can sleep and we cannot
54  * hold swap_list_lock while calling the unplug_fn. And swap_list_lock
55  * cannot be turned into a semaphore.
56  */
57 static DECLARE_RWSEM(swap_unplug_sem);
58
59 #define SWAPFILE_CLUSTER 256
60
61 void swap_unplug_io_fn(struct backing_dev_info *unused_bdi, struct page *page)
62 {
63         swp_entry_t entry;
64
65         down_read(&swap_unplug_sem);
66         entry.val = page->private;
67         if (PageSwapCache(page)) {
68                 struct block_device *bdev = swap_info[swp_type(entry)].bdev;
69                 struct backing_dev_info *bdi;
70
71                 /*
72                  * If the page is removed from swapcache from under us (with a
73                  * racy try_to_unuse/swapoff) we need an additional reference
74                  * count to avoid reading garbage from page->private above. If
75                  * the WARN_ON triggers during a swapoff it maybe the race
76                  * condition and it's harmless. However if it triggers without
77                  * swapoff it signals a problem.
78                  */
79                 WARN_ON(page_count(page) <= 1);
80
81                 bdi = bdev->bd_inode->i_mapping->backing_dev_info;
82                 bdi->unplug_io_fn(bdi, page);
83         }
84         up_read(&swap_unplug_sem);
85 }
86
87 static inline int scan_swap_map(struct swap_info_struct *si)
88 {
89         unsigned long offset;
90         /* 
91          * We try to cluster swap pages by allocating them
92          * sequentially in swap.  Once we've allocated
93          * SWAPFILE_CLUSTER pages this way, however, we resort to
94          * first-free allocation, starting a new cluster.  This
95          * prevents us from scattering swap pages all over the entire
96          * swap partition, so that we reduce overall disk seek times
97          * between swap pages.  -- sct */
98         if (si->cluster_nr) {
99                 while (si->cluster_next <= si->highest_bit) {
100                         offset = si->cluster_next++;
101                         if (si->swap_map[offset])
102                                 continue;
103                         si->cluster_nr--;
104                         goto got_page;
105                 }
106         }
107         si->cluster_nr = SWAPFILE_CLUSTER;
108
109         /* try to find an empty (even not aligned) cluster. */
110         offset = si->lowest_bit;
111  check_next_cluster:
112         if (offset+SWAPFILE_CLUSTER-1 <= si->highest_bit)
113         {
114                 int nr;
115                 for (nr = offset; nr < offset+SWAPFILE_CLUSTER; nr++)
116                         if (si->swap_map[nr])
117                         {
118                                 offset = nr+1;
119                                 goto check_next_cluster;
120                         }
121                 /* We found a completly empty cluster, so start
122                  * using it.
123                  */
124                 goto got_page;
125         }
126         /* No luck, so now go finegrined as usual. -Andrea */
127         for (offset = si->lowest_bit; offset <= si->highest_bit ; offset++) {
128                 if (si->swap_map[offset])
129                         continue;
130                 si->lowest_bit = offset+1;
131         got_page:
132                 if (offset == si->lowest_bit)
133                         si->lowest_bit++;
134                 if (offset == si->highest_bit)
135                         si->highest_bit--;
136                 if (si->lowest_bit > si->highest_bit) {
137                         si->lowest_bit = si->max;
138                         si->highest_bit = 0;
139                 }
140                 si->swap_map[offset] = 1;
141                 si->inuse_pages++;
142                 nr_swap_pages--;
143                 si->cluster_next = offset+1;
144                 return offset;
145         }
146         si->lowest_bit = si->max;
147         si->highest_bit = 0;
148         return 0;
149 }
150
151 swp_entry_t get_swap_page(void)
152 {
153         struct swap_info_struct * p;
154         unsigned long offset;
155         swp_entry_t entry;
156         int type, wrapped = 0;
157
158         entry.val = 0;  /* Out of memory */
159         swap_list_lock();
160         type = swap_list.next;
161         if (type < 0)
162                 goto out;
163         if (nr_swap_pages <= 0)
164                 goto out;
165
166         while (1) {
167                 p = &swap_info[type];
168                 if ((p->flags & SWP_ACTIVE) == SWP_ACTIVE) {
169                         swap_device_lock(p);
170                         offset = scan_swap_map(p);
171                         swap_device_unlock(p);
172                         if (offset) {
173                                 entry = swp_entry(type,offset);
174                                 type = swap_info[type].next;
175                                 if (type < 0 ||
176                                         p->prio != swap_info[type].prio) {
177                                                 swap_list.next = swap_list.head;
178                                 } else {
179                                         swap_list.next = type;
180                                 }
181                                 goto out;
182                         }
183                 }
184                 type = p->next;
185                 if (!wrapped) {
186                         if (type < 0 || p->prio != swap_info[type].prio) {
187                                 type = swap_list.head;
188                                 wrapped = 1;
189                         }
190                 } else
191                         if (type < 0)
192                                 goto out;       /* out of swap space */
193         }
194 out:
195         swap_list_unlock();
196         return entry;
197 }
198
199 static struct swap_info_struct * swap_info_get(swp_entry_t entry)
200 {
201         struct swap_info_struct * p;
202         unsigned long offset, type;
203
204         if (!entry.val)
205                 goto out;
206         type = swp_type(entry);
207         if (type >= nr_swapfiles)
208                 goto bad_nofile;
209         p = & swap_info[type];
210         if (!(p->flags & SWP_USED))
211                 goto bad_device;
212         offset = swp_offset(entry);
213         if (offset >= p->max)
214                 goto bad_offset;
215         if (!p->swap_map[offset])
216                 goto bad_free;
217         swap_list_lock();
218         if (p->prio > swap_info[swap_list.next].prio)
219                 swap_list.next = type;
220         swap_device_lock(p);
221         return p;
222
223 bad_free:
224         printk(KERN_ERR "swap_free: %s%08lx\n", Unused_offset, entry.val);
225         goto out;
226 bad_offset:
227         printk(KERN_ERR "swap_free: %s%08lx\n", Bad_offset, entry.val);
228         goto out;
229 bad_device:
230         printk(KERN_ERR "swap_free: %s%08lx\n", Unused_file, entry.val);
231         goto out;
232 bad_nofile:
233         printk(KERN_ERR "swap_free: %s%08lx\n", Bad_file, entry.val);
234 out:
235         return NULL;
236 }       
237
238 static void swap_info_put(struct swap_info_struct * p)
239 {
240         swap_device_unlock(p);
241         swap_list_unlock();
242 }
243
244 static int swap_entry_free(struct swap_info_struct *p, unsigned long offset)
245 {
246         int count = p->swap_map[offset];
247
248         if (count < SWAP_MAP_MAX) {
249                 count--;
250                 p->swap_map[offset] = count;
251                 if (!count) {
252                         if (offset < p->lowest_bit)
253                                 p->lowest_bit = offset;
254                         if (offset > p->highest_bit)
255                                 p->highest_bit = offset;
256                         nr_swap_pages++;
257                         p->inuse_pages--;
258                 }
259         }
260         return count;
261 }
262
263 /*
264  * Caller has made sure that the swapdevice corresponding to entry
265  * is still around or has not been recycled.
266  */
267 void swap_free(swp_entry_t entry)
268 {
269         struct swap_info_struct * p;
270
271         p = swap_info_get(entry);
272         if (p) {
273                 swap_entry_free(p, swp_offset(entry));
274                 swap_info_put(p);
275         }
276 }
277
278 /*
279  * Check if we're the only user of a swap page,
280  * when the page is locked.
281  */
282 static int exclusive_swap_page(struct page *page)
283 {
284         int retval = 0;
285         struct swap_info_struct * p;
286         swp_entry_t entry;
287
288         entry.val = page->private;
289         p = swap_info_get(entry);
290         if (p) {
291                 /* Is the only swap cache user the cache itself? */
292                 if (p->swap_map[swp_offset(entry)] == 1) {
293                         /* Recheck the page count with the swapcache lock held.. */
294                         spin_lock_irq(&swapper_space.tree_lock);
295                         if (page_count(page) == 2)
296                                 retval = 1;
297                         spin_unlock_irq(&swapper_space.tree_lock);
298                 }
299                 swap_info_put(p);
300         }
301         return retval;
302 }
303
304 /*
305  * We can use this swap cache entry directly
306  * if there are no other references to it.
307  *
308  * Here "exclusive_swap_page()" does the real
309  * work, but we opportunistically check whether
310  * we need to get all the locks first..
311  */
312 int can_share_swap_page(struct page *page)
313 {
314         int retval = 0;
315
316         if (!PageLocked(page))
317                 BUG();
318         switch (page_count(page)) {
319         case 3:
320                 if (!PagePrivate(page))
321                         break;
322                 /* Fallthrough */
323         case 2:
324                 if (!PageSwapCache(page))
325                         break;
326                 retval = exclusive_swap_page(page);
327                 break;
328         case 1:
329                 if (PageReserved(page))
330                         break;
331                 retval = 1;
332         }
333         return retval;
334 }
335
336 /*
337  * Work out if there are any other processes sharing this
338  * swap cache page. Free it if you can. Return success.
339  */
340 int remove_exclusive_swap_page(struct page *page)
341 {
342         int retval;
343         struct swap_info_struct * p;
344         swp_entry_t entry;
345
346         BUG_ON(PagePrivate(page));
347         BUG_ON(!PageLocked(page));
348
349         if (!PageSwapCache(page))
350                 return 0;
351         if (PageWriteback(page))
352                 return 0;
353         if (page_count(page) != 2) /* 2: us + cache */
354                 return 0;
355
356         entry.val = page->private;
357         p = swap_info_get(entry);
358         if (!p)
359                 return 0;
360
361         /* Is the only swap cache user the cache itself? */
362         retval = 0;
363         if (p->swap_map[swp_offset(entry)] == 1) {
364                 /* Recheck the page count with the swapcache lock held.. */
365                 spin_lock_irq(&swapper_space.tree_lock);
366                 if ((page_count(page) == 2) && !PageWriteback(page)) {
367                         __delete_from_swap_cache(page);
368                         SetPageDirty(page);
369                         retval = 1;
370                 }
371                 spin_unlock_irq(&swapper_space.tree_lock);
372         }
373         swap_info_put(p);
374
375         if (retval) {
376                 swap_free(entry);
377                 page_cache_release(page);
378         }
379
380         return retval;
381 }
382
383 /*
384  * Free the swap entry like above, but also try to
385  * free the page cache entry if it is the last user.
386  */
387 void free_swap_and_cache(swp_entry_t entry)
388 {
389         struct swap_info_struct * p;
390         struct page *page = NULL;
391
392         p = swap_info_get(entry);
393         if (p) {
394                 if (swap_entry_free(p, swp_offset(entry)) == 1) {
395                         spin_lock_irq(&swapper_space.tree_lock);
396                         page = radix_tree_lookup(&swapper_space.page_tree,
397                                 entry.val);
398                         if (page && TestSetPageLocked(page))
399                                 page = NULL;
400                         spin_unlock_irq(&swapper_space.tree_lock);
401                 }
402                 swap_info_put(p);
403         }
404         if (page) {
405                 int one_user;
406
407                 BUG_ON(PagePrivate(page));
408                 page_cache_get(page);
409                 one_user = (page_count(page) == 2);
410                 /* Only cache user (+us), or swap space full? Free it! */
411                 if (!PageWriteback(page) && (one_user || vm_swap_full())) {
412                         delete_from_swap_cache(page);
413                         SetPageDirty(page);
414                 }
415                 unlock_page(page);
416                 page_cache_release(page);
417         }
418 }
419
420 /*
421  * The swap entry has been read in advance, and we return 1 to indicate
422  * that the page has been used or is no longer needed.
423  *
424  * Always set the resulting pte to be nowrite (the same as COW pages
425  * after one process has exited).  We don't know just how many PTEs will
426  * share this swap entry, so be cautious and let do_wp_page work out
427  * what to do if a write is requested later.
428  */
429 /* vma->vm_mm->page_table_lock is held */
430 static void
431 unuse_pte(struct vm_area_struct *vma, unsigned long address, pte_t *dir,
432         swp_entry_t entry, struct page *page)
433 {
434         // vma->vm_mm->rss++;
435         vx_rsspages_inc(vma->vm_mm);
436         get_page(page);
437         set_pte(dir, pte_mkold(mk_pte(page, vma->vm_page_prot)));
438         page_add_anon_rmap(page, vma, address);
439         swap_free(entry);
440 }
441
442 /* vma->vm_mm->page_table_lock is held */
443 static unsigned long unuse_pmd(struct vm_area_struct * vma, pmd_t *dir,
444         unsigned long address, unsigned long size, unsigned long offset,
445         swp_entry_t entry, struct page *page)
446 {
447         pte_t * pte;
448         unsigned long end;
449         pte_t swp_pte = swp_entry_to_pte(entry);
450
451         if (pmd_none(*dir))
452                 return 0;
453         if (pmd_bad(*dir)) {
454                 pmd_ERROR(*dir);
455                 pmd_clear(dir);
456                 return 0;
457         }
458         pte = pte_offset_map(dir, address);
459         offset += address & PMD_MASK;
460         address &= ~PMD_MASK;
461         end = address + size;
462         if (end > PMD_SIZE)
463                 end = PMD_SIZE;
464         do {
465                 /*
466                  * swapoff spends a _lot_ of time in this loop!
467                  * Test inline before going to call unuse_pte.
468                  */
469                 if (unlikely(pte_same(*pte, swp_pte))) {
470                         unuse_pte(vma, offset + address, pte, entry, page);
471                         pte_unmap(pte);
472                         /* add 1 since address may be 0 */
473                         return 1 + offset + address;
474                 }
475                 address += PAGE_SIZE;
476                 pte++;
477         } while (address && (address < end));
478         pte_unmap(pte - 1);
479         return 0;
480 }
481
482 /* vma->vm_mm->page_table_lock is held */
483 static unsigned long unuse_pgd(struct vm_area_struct * vma, pgd_t *dir,
484         unsigned long address, unsigned long size,
485         swp_entry_t entry, struct page *page)
486 {
487         pmd_t * pmd;
488         unsigned long offset, end;
489         unsigned long foundaddr;
490
491         if (pgd_none(*dir))
492                 return 0;
493         if (pgd_bad(*dir)) {
494                 pgd_ERROR(*dir);
495                 pgd_clear(dir);
496                 return 0;
497         }
498         pmd = pmd_offset(dir, address);
499         offset = address & PGDIR_MASK;
500         address &= ~PGDIR_MASK;
501         end = address + size;
502         if (end > PGDIR_SIZE)
503                 end = PGDIR_SIZE;
504         if (address >= end)
505                 BUG();
506         do {
507                 foundaddr = unuse_pmd(vma, pmd, address, end - address,
508                                                 offset, entry, page);
509                 if (foundaddr)
510                         return foundaddr;
511                 address = (address + PMD_SIZE) & PMD_MASK;
512                 pmd++;
513         } while (address && (address < end));
514         return 0;
515 }
516
517 /* vma->vm_mm->page_table_lock is held */
518 static unsigned long unuse_vma(struct vm_area_struct * vma, pgd_t *pgdir,
519         swp_entry_t entry, struct page *page)
520 {
521         unsigned long start = vma->vm_start, end = vma->vm_end;
522         unsigned long foundaddr;
523
524         if (start >= end)
525                 BUG();
526         do {
527                 foundaddr = unuse_pgd(vma, pgdir, start, end - start,
528                                                 entry, page);
529                 if (foundaddr)
530                         return foundaddr;
531                 start = (start + PGDIR_SIZE) & PGDIR_MASK;
532                 pgdir++;
533         } while (start && (start < end));
534         return 0;
535 }
536
537 static int unuse_process(struct mm_struct * mm,
538                         swp_entry_t entry, struct page* page)
539 {
540         struct vm_area_struct* vma;
541         unsigned long foundaddr = 0;
542
543         /*
544          * Go through process' page directory.
545          */
546         down_read(&mm->mmap_sem);
547         spin_lock(&mm->page_table_lock);
548         for (vma = mm->mmap; vma; vma = vma->vm_next) {
549                 if (!is_vm_hugetlb_page(vma)) {
550                         pgd_t * pgd = pgd_offset(mm, vma->vm_start);
551                         foundaddr = unuse_vma(vma, pgd, entry, page);
552                         if (foundaddr)
553                                 break;
554                 }
555         }
556         spin_unlock(&mm->page_table_lock);
557         up_read(&mm->mmap_sem);
558         /*
559          * Currently unuse_process cannot fail, but leave error handling
560          * at call sites for now, since we change it from time to time.
561          */
562         return 0;
563 }
564
565 /*
566  * Scan swap_map from current position to next entry still in use.
567  * Recycle to start on reaching the end, returning 0 when empty.
568  */
569 static int find_next_to_unuse(struct swap_info_struct *si, int prev)
570 {
571         int max = si->max;
572         int i = prev;
573         int count;
574
575         /*
576          * No need for swap_device_lock(si) here: we're just looking
577          * for whether an entry is in use, not modifying it; false
578          * hits are okay, and sys_swapoff() has already prevented new
579          * allocations from this area (while holding swap_list_lock()).
580          */
581         for (;;) {
582                 if (++i >= max) {
583                         if (!prev) {
584                                 i = 0;
585                                 break;
586                         }
587                         /*
588                          * No entries in use at top of swap_map,
589                          * loop back to start and recheck there.
590                          */
591                         max = prev + 1;
592                         prev = 0;
593                         i = 1;
594                 }
595                 count = si->swap_map[i];
596                 if (count && count != SWAP_MAP_BAD)
597                         break;
598         }
599         return i;
600 }
601
602 /*
603  * We completely avoid races by reading each swap page in advance,
604  * and then search for the process using it.  All the necessary
605  * page table adjustments can then be made atomically.
606  */
607 static int try_to_unuse(unsigned int type)
608 {
609         struct swap_info_struct * si = &swap_info[type];
610         struct mm_struct *start_mm;
611         unsigned short *swap_map;
612         unsigned short swcount;
613         struct page *page;
614         swp_entry_t entry;
615         int i = 0;
616         int retval = 0;
617         int reset_overflow = 0;
618         int shmem;
619
620         /*
621          * When searching mms for an entry, a good strategy is to
622          * start at the first mm we freed the previous entry from
623          * (though actually we don't notice whether we or coincidence
624          * freed the entry).  Initialize this start_mm with a hold.
625          *
626          * A simpler strategy would be to start at the last mm we
627          * freed the previous entry from; but that would take less
628          * advantage of mmlist ordering (now preserved by swap_out()),
629          * which clusters forked address spaces together, most recent
630          * child immediately after parent.  If we race with dup_mmap(),
631          * we very much want to resolve parent before child, otherwise
632          * we may miss some entries: using last mm would invert that.
633          */
634         start_mm = &init_mm;
635         atomic_inc(&init_mm.mm_users);
636
637         /*
638          * Keep on scanning until all entries have gone.  Usually,
639          * one pass through swap_map is enough, but not necessarily:
640          * mmput() removes mm from mmlist before exit_mmap() and its
641          * zap_page_range().  That's not too bad, those entries are
642          * on their way out, and handled faster there than here.
643          * do_munmap() behaves similarly, taking the range out of mm's
644          * vma list before zap_page_range().  But unfortunately, when
645          * unmapping a part of a vma, it takes the whole out first,
646          * then reinserts what's left after (might even reschedule if
647          * open() method called) - so swap entries may be invisible
648          * to swapoff for a while, then reappear - but that is rare.
649          */
650         while ((i = find_next_to_unuse(si, i))) {
651                 if (signal_pending(current)) {
652                         retval = -EINTR;
653                         break;
654                 }
655
656                 /* 
657                  * Get a page for the entry, using the existing swap
658                  * cache page if there is one.  Otherwise, get a clean
659                  * page and read the swap into it. 
660                  */
661                 swap_map = &si->swap_map[i];
662                 entry = swp_entry(type, i);
663                 page = read_swap_cache_async(entry, NULL, 0);
664                 if (!page) {
665                         /*
666                          * Either swap_duplicate() failed because entry
667                          * has been freed independently, and will not be
668                          * reused since sys_swapoff() already disabled
669                          * allocation from here, or alloc_page() failed.
670                          */
671                         if (!*swap_map)
672                                 continue;
673                         retval = -ENOMEM;
674                         break;
675                 }
676
677                 /*
678                  * Don't hold on to start_mm if it looks like exiting.
679                  */
680                 if (atomic_read(&start_mm->mm_users) == 1) {
681                         mmput(start_mm);
682                         start_mm = &init_mm;
683                         atomic_inc(&init_mm.mm_users);
684                 }
685
686                 /*
687                  * Wait for and lock page.  When do_swap_page races with
688                  * try_to_unuse, do_swap_page can handle the fault much
689                  * faster than try_to_unuse can locate the entry.  This
690                  * apparently redundant "wait_on_page_locked" lets try_to_unuse
691                  * defer to do_swap_page in such a case - in some tests,
692                  * do_swap_page and try_to_unuse repeatedly compete.
693                  */
694                 wait_on_page_locked(page);
695                 wait_on_page_writeback(page);
696                 lock_page(page);
697                 wait_on_page_writeback(page);
698
699                 /*
700                  * Remove all references to entry, without blocking.
701                  * Whenever we reach init_mm, there's no address space
702                  * to search, but use it as a reminder to search shmem.
703                  */
704                 shmem = 0;
705                 swcount = *swap_map;
706                 if (swcount > 1) {
707                         if (start_mm == &init_mm)
708                                 shmem = shmem_unuse(entry, page);
709                         else
710                                 retval = unuse_process(start_mm, entry, page);
711                 }
712                 if (*swap_map > 1) {
713                         int set_start_mm = (*swap_map >= swcount);
714                         struct list_head *p = &start_mm->mmlist;
715                         struct mm_struct *new_start_mm = start_mm;
716                         struct mm_struct *prev_mm = start_mm;
717                         struct mm_struct *mm;
718
719                         atomic_inc(&new_start_mm->mm_users);
720                         atomic_inc(&prev_mm->mm_users);
721                         spin_lock(&mmlist_lock);
722                         while (*swap_map > 1 && !retval &&
723                                         (p = p->next) != &start_mm->mmlist) {
724                                 mm = list_entry(p, struct mm_struct, mmlist);
725                                 atomic_inc(&mm->mm_users);
726                                 spin_unlock(&mmlist_lock);
727                                 mmput(prev_mm);
728                                 prev_mm = mm;
729
730                                 cond_resched();
731
732                                 swcount = *swap_map;
733                                 if (swcount <= 1)
734                                         ;
735                                 else if (mm == &init_mm) {
736                                         set_start_mm = 1;
737                                         shmem = shmem_unuse(entry, page);
738                                 } else
739                                         retval = unuse_process(mm, entry, page);
740                                 if (set_start_mm && *swap_map < swcount) {
741                                         mmput(new_start_mm);
742                                         atomic_inc(&mm->mm_users);
743                                         new_start_mm = mm;
744                                         set_start_mm = 0;
745                                 }
746                                 spin_lock(&mmlist_lock);
747                         }
748                         spin_unlock(&mmlist_lock);
749                         mmput(prev_mm);
750                         mmput(start_mm);
751                         start_mm = new_start_mm;
752                 }
753                 if (retval) {
754                         unlock_page(page);
755                         page_cache_release(page);
756                         break;
757                 }
758
759                 /*
760                  * How could swap count reach 0x7fff when the maximum
761                  * pid is 0x7fff, and there's no way to repeat a swap
762                  * page within an mm (except in shmem, where it's the
763                  * shared object which takes the reference count)?
764                  * We believe SWAP_MAP_MAX cannot occur in Linux 2.4.
765                  *
766                  * If that's wrong, then we should worry more about
767                  * exit_mmap() and do_munmap() cases described above:
768                  * we might be resetting SWAP_MAP_MAX too early here.
769                  * We know "Undead"s can happen, they're okay, so don't
770                  * report them; but do report if we reset SWAP_MAP_MAX.
771                  */
772                 if (*swap_map == SWAP_MAP_MAX) {
773                         swap_device_lock(si);
774                         *swap_map = 1;
775                         swap_device_unlock(si);
776                         reset_overflow = 1;
777                 }
778
779                 /*
780                  * If a reference remains (rare), we would like to leave
781                  * the page in the swap cache; but try_to_unmap could
782                  * then re-duplicate the entry once we drop page lock,
783                  * so we might loop indefinitely; also, that page could
784                  * not be swapped out to other storage meanwhile.  So:
785                  * delete from cache even if there's another reference,
786                  * after ensuring that the data has been saved to disk -
787                  * since if the reference remains (rarer), it will be
788                  * read from disk into another page.  Splitting into two
789                  * pages would be incorrect if swap supported "shared
790                  * private" pages, but they are handled by tmpfs files.
791                  *
792                  * Note shmem_unuse already deleted a swappage from
793                  * the swap cache, unless the move to filepage failed:
794                  * in which case it left swappage in cache, lowered its
795                  * swap count to pass quickly through the loops above,
796                  * and now we must reincrement count to try again later.
797                  */
798                 if ((*swap_map > 1) && PageDirty(page) && PageSwapCache(page)) {
799                         struct writeback_control wbc = {
800                                 .sync_mode = WB_SYNC_NONE,
801                         };
802
803                         swap_writepage(page, &wbc);
804                         lock_page(page);
805                         wait_on_page_writeback(page);
806                 }
807                 if (PageSwapCache(page)) {
808                         if (shmem)
809                                 swap_duplicate(entry);
810                         else
811                                 delete_from_swap_cache(page);
812                 }
813
814                 /*
815                  * So we could skip searching mms once swap count went
816                  * to 1, we did not mark any present ptes as dirty: must
817                  * mark page dirty so shrink_list will preserve it.
818                  */
819                 SetPageDirty(page);
820                 unlock_page(page);
821                 page_cache_release(page);
822
823                 /*
824                  * Make sure that we aren't completely killing
825                  * interactive performance.
826                  */
827                 cond_resched();
828         }
829
830         mmput(start_mm);
831         if (reset_overflow) {
832                 printk(KERN_WARNING "swapoff: cleared swap entry overflow\n");
833                 swap_overflow = 0;
834         }
835         return retval;
836 }
837
838 /*
839  * Use this swapdev's extent info to locate the (PAGE_SIZE) block which
840  * corresponds to page offset `offset'.
841  */
842 sector_t map_swap_page(struct swap_info_struct *sis, pgoff_t offset)
843 {
844         struct swap_extent *se = sis->curr_swap_extent;
845         struct swap_extent *start_se = se;
846
847         for ( ; ; ) {
848                 struct list_head *lh;
849
850                 if (se->start_page <= offset &&
851                                 offset < (se->start_page + se->nr_pages)) {
852                         return se->start_block + (offset - se->start_page);
853                 }
854                 lh = se->list.prev;
855                 if (lh == &sis->extent_list)
856                         lh = lh->prev;
857                 se = list_entry(lh, struct swap_extent, list);
858                 sis->curr_swap_extent = se;
859                 BUG_ON(se == start_se);         /* It *must* be present */
860         }
861 }
862
863 /*
864  * Free all of a swapdev's extent information
865  */
866 static void destroy_swap_extents(struct swap_info_struct *sis)
867 {
868         while (!list_empty(&sis->extent_list)) {
869                 struct swap_extent *se;
870
871                 se = list_entry(sis->extent_list.next,
872                                 struct swap_extent, list);
873                 list_del(&se->list);
874                 kfree(se);
875         }
876         sis->nr_extents = 0;
877 }
878
879 /*
880  * Add a block range (and the corresponding page range) into this swapdev's
881  * extent list.  The extent list is kept sorted in block order.
882  *
883  * This function rather assumes that it is called in ascending sector_t order.
884  * It doesn't look for extent coalescing opportunities.
885  */
886 static int
887 add_swap_extent(struct swap_info_struct *sis, unsigned long start_page,
888                 unsigned long nr_pages, sector_t start_block)
889 {
890         struct swap_extent *se;
891         struct swap_extent *new_se;
892         struct list_head *lh;
893
894         lh = sis->extent_list.next;     /* The highest-addressed block */
895         while (lh != &sis->extent_list) {
896                 se = list_entry(lh, struct swap_extent, list);
897                 if (se->start_block + se->nr_pages == start_block &&
898                     se->start_page  + se->nr_pages == start_page) {
899                         /* Merge it */
900                         se->nr_pages += nr_pages;
901                         return 0;
902                 }
903                 lh = lh->next;
904         }
905
906         /*
907          * No merge.  Insert a new extent, preserving ordering.
908          */
909         new_se = kmalloc(sizeof(*se), GFP_KERNEL);
910         if (new_se == NULL)
911                 return -ENOMEM;
912         new_se->start_page = start_page;
913         new_se->nr_pages = nr_pages;
914         new_se->start_block = start_block;
915
916         lh = sis->extent_list.prev;     /* The lowest block */
917         while (lh != &sis->extent_list) {
918                 se = list_entry(lh, struct swap_extent, list);
919                 if (se->start_block > start_block)
920                         break;
921                 lh = lh->prev;
922         }
923         list_add_tail(&new_se->list, lh);
924         sis->nr_extents++;
925         return 0;
926 }
927
928 /*
929  * A `swap extent' is a simple thing which maps a contiguous range of pages
930  * onto a contiguous range of disk blocks.  An ordered list of swap extents
931  * is built at swapon time and is then used at swap_writepage/swap_readpage
932  * time for locating where on disk a page belongs.
933  *
934  * If the swapfile is an S_ISBLK block device, a single extent is installed.
935  * This is done so that the main operating code can treat S_ISBLK and S_ISREG
936  * swap files identically.
937  *
938  * Whether the swapdev is an S_ISREG file or an S_ISBLK blockdev, the swap
939  * extent list operates in PAGE_SIZE disk blocks.  Both S_ISREG and S_ISBLK
940  * swapfiles are handled *identically* after swapon time.
941  *
942  * For S_ISREG swapfiles, setup_swap_extents() will walk all the file's blocks
943  * and will parse them into an ordered extent list, in PAGE_SIZE chunks.  If
944  * some stray blocks are found which do not fall within the PAGE_SIZE alignment
945  * requirements, they are simply tossed out - we will never use those blocks
946  * for swapping.
947  *
948  * For S_ISREG swapfiles we hold i_sem across the life of the swapon.  This
949  * prevents root from shooting her foot off by ftruncating an in-use swapfile,
950  * which will scribble on the fs.
951  *
952  * The amount of disk space which a single swap extent represents varies.
953  * Typically it is in the 1-4 megabyte range.  So we can have hundreds of
954  * extents in the list.  To avoid much list walking, we cache the previous
955  * search location in `curr_swap_extent', and start new searches from there.
956  * This is extremely effective.  The average number of iterations in
957  * map_swap_page() has been measured at about 0.3 per page.  - akpm.
958  */
959 static int setup_swap_extents(struct swap_info_struct *sis)
960 {
961         struct inode *inode;
962         unsigned blocks_per_page;
963         unsigned long page_no;
964         unsigned blkbits;
965         sector_t probe_block;
966         sector_t last_block;
967         int ret;
968
969         inode = sis->swap_file->f_mapping->host;
970         if (S_ISBLK(inode->i_mode)) {
971                 ret = add_swap_extent(sis, 0, sis->max, 0);
972                 goto done;
973         }
974
975         blkbits = inode->i_blkbits;
976         blocks_per_page = PAGE_SIZE >> blkbits;
977
978         /*
979          * Map all the blocks into the extent list.  This code doesn't try
980          * to be very smart.
981          */
982         probe_block = 0;
983         page_no = 0;
984         last_block = i_size_read(inode) >> blkbits;
985         while ((probe_block + blocks_per_page) <= last_block &&
986                         page_no < sis->max) {
987                 unsigned block_in_page;
988                 sector_t first_block;
989
990                 first_block = bmap(inode, probe_block);
991                 if (first_block == 0)
992                         goto bad_bmap;
993
994                 /*
995                  * It must be PAGE_SIZE aligned on-disk
996                  */
997                 if (first_block & (blocks_per_page - 1)) {
998                         probe_block++;
999                         goto reprobe;
1000                 }
1001
1002                 for (block_in_page = 1; block_in_page < blocks_per_page;
1003                                         block_in_page++) {
1004                         sector_t block;
1005
1006                         block = bmap(inode, probe_block + block_in_page);
1007                         if (block == 0)
1008                                 goto bad_bmap;
1009                         if (block != first_block + block_in_page) {
1010                                 /* Discontiguity */
1011                                 probe_block++;
1012                                 goto reprobe;
1013                         }
1014                 }
1015
1016                 /*
1017                  * We found a PAGE_SIZE-length, PAGE_SIZE-aligned run of blocks
1018                  */
1019                 ret = add_swap_extent(sis, page_no, 1,
1020                                 first_block >> (PAGE_SHIFT - blkbits));
1021                 if (ret)
1022                         goto out;
1023                 page_no++;
1024                 probe_block += blocks_per_page;
1025 reprobe:
1026                 continue;
1027         }
1028         ret = 0;
1029         if (page_no == 0)
1030                 ret = -EINVAL;
1031         sis->max = page_no;
1032         sis->highest_bit = page_no - 1;
1033 done:
1034         sis->curr_swap_extent = list_entry(sis->extent_list.prev,
1035                                         struct swap_extent, list);
1036         goto out;
1037 bad_bmap:
1038         printk(KERN_ERR "swapon: swapfile has holes\n");
1039         ret = -EINVAL;
1040 out:
1041         return ret;
1042 }
1043
1044 #if 0   /* We don't need this yet */
1045 #include <linux/backing-dev.h>
1046 int page_queue_congested(struct page *page)
1047 {
1048         struct backing_dev_info *bdi;
1049
1050         BUG_ON(!PageLocked(page));      /* It pins the swap_info_struct */
1051
1052         if (PageSwapCache(page)) {
1053                 swp_entry_t entry = { .val = page->private };
1054                 struct swap_info_struct *sis;
1055
1056                 sis = get_swap_info_struct(swp_type(entry));
1057                 bdi = sis->bdev->bd_inode->i_mapping->backing_dev_info;
1058         } else
1059                 bdi = page->mapping->backing_dev_info;
1060         return bdi_write_congested(bdi);
1061 }
1062 #endif
1063
1064 asmlinkage long sys_swapoff(const char __user * specialfile)
1065 {
1066         struct swap_info_struct * p = NULL;
1067         unsigned short *swap_map;
1068         struct file *swap_file, *victim;
1069         struct address_space *mapping;
1070         char * pathname;
1071         int i, type, prev;
1072         int err;
1073         
1074         if (!capable(CAP_SYS_ADMIN))
1075                 return -EPERM;
1076
1077         pathname = getname(specialfile);
1078         err = PTR_ERR(pathname);
1079         if (IS_ERR(pathname))
1080                 goto out;
1081
1082         victim = filp_open(pathname, O_RDWR, 0);
1083         putname(pathname);
1084         err = PTR_ERR(victim);
1085         if (IS_ERR(victim))
1086                 goto out;
1087
1088         mapping = victim->f_mapping;
1089         prev = -1;
1090         swap_list_lock();
1091         for (type = swap_list.head; type >= 0; type = swap_info[type].next) {
1092                 p = swap_info + type;
1093                 if ((p->flags & SWP_ACTIVE) == SWP_ACTIVE) {
1094                         if (p->swap_file->f_mapping == mapping)
1095                                 break;
1096                 }
1097                 prev = type;
1098         }
1099         if (type < 0) {
1100                 err = -EINVAL;
1101                 swap_list_unlock();
1102                 goto out_dput;
1103         }
1104         if (!security_vm_enough_memory(p->pages))
1105                 vm_unacct_memory(p->pages);
1106         else {
1107                 err = -ENOMEM;
1108                 swap_list_unlock();
1109                 goto out_dput;
1110         }
1111         if (prev < 0) {
1112                 swap_list.head = p->next;
1113         } else {
1114                 swap_info[prev].next = p->next;
1115         }
1116         if (type == swap_list.next) {
1117                 /* just pick something that's safe... */
1118                 swap_list.next = swap_list.head;
1119         }
1120         nr_swap_pages -= p->pages;
1121         total_swap_pages -= p->pages;
1122         p->flags &= ~SWP_WRITEOK;
1123         swap_list_unlock();
1124         current->flags |= PF_SWAPOFF;
1125         err = try_to_unuse(type);
1126         current->flags &= ~PF_SWAPOFF;
1127
1128         /* wait for any unplug function to finish */
1129         down_write(&swap_unplug_sem);
1130         up_write(&swap_unplug_sem);
1131
1132         if (err) {
1133                 /* re-insert swap space back into swap_list */
1134                 swap_list_lock();
1135                 for (prev = -1, i = swap_list.head; i >= 0; prev = i, i = swap_info[i].next)
1136                         if (p->prio >= swap_info[i].prio)
1137                                 break;
1138                 p->next = i;
1139                 if (prev < 0)
1140                         swap_list.head = swap_list.next = p - swap_info;
1141                 else
1142                         swap_info[prev].next = p - swap_info;
1143                 nr_swap_pages += p->pages;
1144                 total_swap_pages += p->pages;
1145                 p->flags |= SWP_WRITEOK;
1146                 swap_list_unlock();
1147                 goto out_dput;
1148         }
1149         down(&swapon_sem);
1150         swap_list_lock();
1151         swap_device_lock(p);
1152         swap_file = p->swap_file;
1153         p->swap_file = NULL;
1154         p->max = 0;
1155         swap_map = p->swap_map;
1156         p->swap_map = NULL;
1157         p->flags = 0;
1158         destroy_swap_extents(p);
1159         swap_device_unlock(p);
1160         swap_list_unlock();
1161         up(&swapon_sem);
1162         vfree(swap_map);
1163         if (S_ISBLK(mapping->host->i_mode)) {
1164                 struct block_device *bdev = I_BDEV(mapping->host);
1165                 set_blocksize(bdev, p->old_block_size);
1166                 bd_release(bdev);
1167         } else {
1168                 up(&mapping->host->i_sem);
1169         }
1170         filp_close(swap_file, NULL);
1171         err = 0;
1172
1173 out_dput:
1174         filp_close(victim, NULL);
1175 out:
1176         return err;
1177 }
1178
1179 #ifdef CONFIG_PROC_FS
1180 /* iterator */
1181 static void *swap_start(struct seq_file *swap, loff_t *pos)
1182 {
1183         struct swap_info_struct *ptr = swap_info;
1184         int i;
1185         loff_t l = *pos;
1186
1187         down(&swapon_sem);
1188
1189         for (i = 0; i < nr_swapfiles; i++, ptr++) {
1190                 if (!(ptr->flags & SWP_USED) || !ptr->swap_map)
1191                         continue;
1192                 if (!l--)
1193                         return ptr;
1194         }
1195
1196         return NULL;
1197 }
1198
1199 static void *swap_next(struct seq_file *swap, void *v, loff_t *pos)
1200 {
1201         struct swap_info_struct *ptr = v;
1202         struct swap_info_struct *endptr = swap_info + nr_swapfiles;
1203
1204         for (++ptr; ptr < endptr; ptr++) {
1205                 if (!(ptr->flags & SWP_USED) || !ptr->swap_map)
1206                         continue;
1207                 ++*pos;
1208                 return ptr;
1209         }
1210
1211         return NULL;
1212 }
1213
1214 static void swap_stop(struct seq_file *swap, void *v)
1215 {
1216         up(&swapon_sem);
1217 }
1218
1219 static int swap_show(struct seq_file *swap, void *v)
1220 {
1221         struct swap_info_struct *ptr = v;
1222         struct file *file;
1223         int len;
1224
1225         if (v == swap_info)
1226                 seq_puts(swap, "Filename\t\t\t\tType\t\tSize\tUsed\tPriority\n");
1227
1228         file = ptr->swap_file;
1229         len = seq_path(swap, file->f_vfsmnt, file->f_dentry, " \t\n\\");
1230         seq_printf(swap, "%*s%s\t%d\t%ld\t%d\n",
1231                        len < 40 ? 40 - len : 1, " ",
1232                        S_ISBLK(file->f_dentry->d_inode->i_mode) ?
1233                                 "partition" : "file\t",
1234                        ptr->pages << (PAGE_SHIFT - 10),
1235                        ptr->inuse_pages << (PAGE_SHIFT - 10),
1236                        ptr->prio);
1237         return 0;
1238 }
1239
1240 static struct seq_operations swaps_op = {
1241         .start =        swap_start,
1242         .next =         swap_next,
1243         .stop =         swap_stop,
1244         .show =         swap_show
1245 };
1246
1247 static int swaps_open(struct inode *inode, struct file *file)
1248 {
1249         return seq_open(file, &swaps_op);
1250 }
1251
1252 static struct file_operations proc_swaps_operations = {
1253         .open           = swaps_open,
1254         .read           = seq_read,
1255         .llseek         = seq_lseek,
1256         .release        = seq_release,
1257 };
1258
1259 static int __init procswaps_init(void)
1260 {
1261         struct proc_dir_entry *entry;
1262
1263         entry = create_proc_entry("swaps", 0, NULL);
1264         if (entry)
1265                 entry->proc_fops = &proc_swaps_operations;
1266         return 0;
1267 }
1268 __initcall(procswaps_init);
1269 #endif /* CONFIG_PROC_FS */
1270
1271 /*
1272  * Written 01/25/92 by Simmule Turner, heavily changed by Linus.
1273  *
1274  * The swapon system call
1275  */
1276 asmlinkage long sys_swapon(const char __user * specialfile, int swap_flags)
1277 {
1278         struct swap_info_struct * p;
1279         char *name = NULL;
1280         struct block_device *bdev = NULL;
1281         struct file *swap_file = NULL;
1282         struct address_space *mapping;
1283         unsigned int type;
1284         int i, prev;
1285         int error;
1286         static int least_priority;
1287         union swap_header *swap_header = 0;
1288         int swap_header_version;
1289         int nr_good_pages = 0;
1290         unsigned long maxpages = 1;
1291         int swapfilesize;
1292         unsigned short *swap_map;
1293         struct page *page = NULL;
1294         struct inode *inode = NULL;
1295         int did_down = 0;
1296
1297         if (!capable(CAP_SYS_ADMIN))
1298                 return -EPERM;
1299         swap_list_lock();
1300         p = swap_info;
1301         for (type = 0 ; type < nr_swapfiles ; type++,p++)
1302                 if (!(p->flags & SWP_USED))
1303                         break;
1304         error = -EPERM;
1305         /*
1306          * Test if adding another swap device is possible. There are
1307          * two limiting factors: 1) the number of bits for the swap
1308          * type swp_entry_t definition and 2) the number of bits for
1309          * the swap type in the swap ptes as defined by the different
1310          * architectures. To honor both limitations a swap entry
1311          * with swap offset 0 and swap type ~0UL is created, encoded
1312          * to a swap pte, decoded to a swp_entry_t again and finally
1313          * the swap type part is extracted. This will mask all bits
1314          * from the initial ~0UL that can't be encoded in either the
1315          * swp_entry_t or the architecture definition of a swap pte.
1316          */
1317         if (type > swp_type(pte_to_swp_entry(swp_entry_to_pte(swp_entry(~0UL,0))))) {
1318                 swap_list_unlock();
1319                 goto out;
1320         }
1321         if (type >= nr_swapfiles)
1322                 nr_swapfiles = type+1;
1323         INIT_LIST_HEAD(&p->extent_list);
1324         p->flags = SWP_USED;
1325         p->nr_extents = 0;
1326         p->swap_file = NULL;
1327         p->old_block_size = 0;
1328         p->swap_map = NULL;
1329         p->lowest_bit = 0;
1330         p->highest_bit = 0;
1331         p->cluster_nr = 0;
1332         p->inuse_pages = 0;
1333         p->sdev_lock = SPIN_LOCK_UNLOCKED;
1334         p->next = -1;
1335         if (swap_flags & SWAP_FLAG_PREFER) {
1336                 p->prio =
1337                   (swap_flags & SWAP_FLAG_PRIO_MASK)>>SWAP_FLAG_PRIO_SHIFT;
1338         } else {
1339                 p->prio = --least_priority;
1340         }
1341         swap_list_unlock();
1342         name = getname(specialfile);
1343         error = PTR_ERR(name);
1344         if (IS_ERR(name)) {
1345                 name = NULL;
1346                 goto bad_swap_2;
1347         }
1348         swap_file = filp_open(name, O_RDWR, 0);
1349         error = PTR_ERR(swap_file);
1350         if (IS_ERR(swap_file)) {
1351                 swap_file = NULL;
1352                 goto bad_swap_2;
1353         }
1354
1355         p->swap_file = swap_file;
1356         mapping = swap_file->f_mapping;
1357         inode = mapping->host;
1358
1359         error = -EBUSY;
1360         for (i = 0; i < nr_swapfiles; i++) {
1361                 struct swap_info_struct *q = &swap_info[i];
1362
1363                 if (i == type || !q->swap_file)
1364                         continue;
1365                 if (mapping == q->swap_file->f_mapping)
1366                         goto bad_swap;
1367         }
1368
1369         error = -EINVAL;
1370         if (S_ISBLK(inode->i_mode)) {
1371                 bdev = I_BDEV(inode);
1372                 error = bd_claim(bdev, sys_swapon);
1373                 if (error < 0) {
1374                         bdev = NULL;
1375                         goto bad_swap;
1376                 }
1377                 p->old_block_size = block_size(bdev);
1378                 error = set_blocksize(bdev, PAGE_SIZE);
1379                 if (error < 0)
1380                         goto bad_swap;
1381                 p->bdev = bdev;
1382         } else if (S_ISREG(inode->i_mode)) {
1383                 p->bdev = inode->i_sb->s_bdev;
1384                 down(&inode->i_sem);
1385                 did_down = 1;
1386         } else {
1387                 goto bad_swap;
1388         }
1389
1390         swapfilesize = i_size_read(inode) >> PAGE_SHIFT;
1391
1392         /*
1393          * Read the swap header.
1394          */
1395         if (!mapping->a_ops->readpage) {
1396                 error = -EINVAL;
1397                 goto bad_swap;
1398         }
1399         page = read_cache_page(mapping, 0,
1400                         (filler_t *)mapping->a_ops->readpage, swap_file);
1401         if (IS_ERR(page)) {
1402                 error = PTR_ERR(page);
1403                 goto bad_swap;
1404         }
1405         wait_on_page_locked(page);
1406         if (!PageUptodate(page))
1407                 goto bad_swap;
1408         kmap(page);
1409         swap_header = page_address(page);
1410
1411         if (!memcmp("SWAP-SPACE",swap_header->magic.magic,10))
1412                 swap_header_version = 1;
1413         else if (!memcmp("SWAPSPACE2",swap_header->magic.magic,10))
1414                 swap_header_version = 2;
1415         else {
1416                 printk("Unable to find swap-space signature\n");
1417                 error = -EINVAL;
1418                 goto bad_swap;
1419         }
1420         
1421         switch (swap_header_version) {
1422         case 1:
1423                 printk(KERN_ERR "version 0 swap is no longer supported. "
1424                         "Use mkswap -v1 %s\n", name);
1425                 error = -EINVAL;
1426                 goto bad_swap;
1427         case 2:
1428                 /* Check the swap header's sub-version and the size of
1429                    the swap file and bad block lists */
1430                 if (swap_header->info.version != 1) {
1431                         printk(KERN_WARNING
1432                                "Unable to handle swap header version %d\n",
1433                                swap_header->info.version);
1434                         error = -EINVAL;
1435                         goto bad_swap;
1436                 }
1437
1438                 p->lowest_bit  = 1;
1439                 /*
1440                  * Find out how many pages are allowed for a single swap
1441                  * device. There are two limiting factors: 1) the number of
1442                  * bits for the swap offset in the swp_entry_t type and
1443                  * 2) the number of bits in the a swap pte as defined by
1444                  * the different architectures. In order to find the
1445                  * largest possible bit mask a swap entry with swap type 0
1446                  * and swap offset ~0UL is created, encoded to a swap pte,
1447                  * decoded to a swp_entry_t again and finally the swap
1448                  * offset is extracted. This will mask all the bits from
1449                  * the initial ~0UL mask that can't be encoded in either
1450                  * the swp_entry_t or the architecture definition of a
1451                  * swap pte.
1452                  */
1453                 maxpages = swp_offset(pte_to_swp_entry(swp_entry_to_pte(swp_entry(0,~0UL)))) - 1;
1454                 if (maxpages > swap_header->info.last_page)
1455                         maxpages = swap_header->info.last_page;
1456                 p->highest_bit = maxpages - 1;
1457
1458                 error = -EINVAL;
1459                 if (swap_header->info.nr_badpages > MAX_SWAP_BADPAGES)
1460                         goto bad_swap;
1461                 
1462                 /* OK, set up the swap map and apply the bad block list */
1463                 if (!(p->swap_map = vmalloc(maxpages * sizeof(short)))) {
1464                         error = -ENOMEM;
1465                         goto bad_swap;
1466                 }
1467
1468                 error = 0;
1469                 memset(p->swap_map, 0, maxpages * sizeof(short));
1470                 for (i=0; i<swap_header->info.nr_badpages; i++) {
1471                         int page = swap_header->info.badpages[i];
1472                         if (page <= 0 || page >= swap_header->info.last_page)
1473                                 error = -EINVAL;
1474                         else
1475                                 p->swap_map[page] = SWAP_MAP_BAD;
1476                 }
1477                 nr_good_pages = swap_header->info.last_page -
1478                                 swap_header->info.nr_badpages -
1479                                 1 /* header page */;
1480                 if (error) 
1481                         goto bad_swap;
1482         }
1483         
1484         if (swapfilesize && maxpages > swapfilesize) {
1485                 printk(KERN_WARNING
1486                        "Swap area shorter than signature indicates\n");
1487                 error = -EINVAL;
1488                 goto bad_swap;
1489         }
1490         if (!nr_good_pages) {
1491                 printk(KERN_WARNING "Empty swap-file\n");
1492                 error = -EINVAL;
1493                 goto bad_swap;
1494         }
1495         p->swap_map[0] = SWAP_MAP_BAD;
1496         p->max = maxpages;
1497         p->pages = nr_good_pages;
1498
1499         error = setup_swap_extents(p);
1500         if (error)
1501                 goto bad_swap;
1502
1503         down(&swapon_sem);
1504         swap_list_lock();
1505         swap_device_lock(p);
1506         p->flags = SWP_ACTIVE;
1507         nr_swap_pages += nr_good_pages;
1508         total_swap_pages += nr_good_pages;
1509         printk(KERN_INFO "Adding %dk swap on %s.  Priority:%d extents:%d\n",
1510                 nr_good_pages<<(PAGE_SHIFT-10), name,
1511                 p->prio, p->nr_extents);
1512
1513         /* insert swap space into swap_list: */
1514         prev = -1;
1515         for (i = swap_list.head; i >= 0; i = swap_info[i].next) {
1516                 if (p->prio >= swap_info[i].prio) {
1517                         break;
1518                 }
1519                 prev = i;
1520         }
1521         p->next = i;
1522         if (prev < 0) {
1523                 swap_list.head = swap_list.next = p - swap_info;
1524         } else {
1525                 swap_info[prev].next = p - swap_info;
1526         }
1527         swap_device_unlock(p);
1528         swap_list_unlock();
1529         up(&swapon_sem);
1530         error = 0;
1531         goto out;
1532 bad_swap:
1533         if (bdev) {
1534                 set_blocksize(bdev, p->old_block_size);
1535                 bd_release(bdev);
1536         }
1537 bad_swap_2:
1538         swap_list_lock();
1539         swap_map = p->swap_map;
1540         p->swap_file = NULL;
1541         p->swap_map = NULL;
1542         p->flags = 0;
1543         if (!(swap_flags & SWAP_FLAG_PREFER))
1544                 ++least_priority;
1545         swap_list_unlock();
1546         destroy_swap_extents(p);
1547         if (swap_map)
1548                 vfree(swap_map);
1549         if (swap_file)
1550                 filp_close(swap_file, NULL);
1551 out:
1552         if (page && !IS_ERR(page)) {
1553                 kunmap(page);
1554                 page_cache_release(page);
1555         }
1556         if (name)
1557                 putname(name);
1558         if (error && did_down)
1559                 up(&inode->i_sem);
1560         return error;
1561 }
1562
1563 void si_swapinfo(struct sysinfo *val)
1564 {
1565         unsigned int i;
1566         unsigned long nr_to_be_unused = 0;
1567
1568         swap_list_lock();
1569         for (i = 0; i < nr_swapfiles; i++) {
1570                 if (!(swap_info[i].flags & SWP_USED) ||
1571                      (swap_info[i].flags & SWP_WRITEOK))
1572                         continue;
1573                 nr_to_be_unused += swap_info[i].inuse_pages;
1574         }
1575         val->freeswap = nr_swap_pages + nr_to_be_unused;
1576         val->totalswap = total_swap_pages + nr_to_be_unused;
1577         swap_list_unlock();
1578         if (vx_flags(VXF_VIRT_MEM, 0))
1579                 vx_vsi_swapinfo(val);
1580 }
1581
1582 /*
1583  * Verify that a swap entry is valid and increment its swap map count.
1584  *
1585  * Note: if swap_map[] reaches SWAP_MAP_MAX the entries are treated as
1586  * "permanent", but will be reclaimed by the next swapoff.
1587  */
1588 int swap_duplicate(swp_entry_t entry)
1589 {
1590         struct swap_info_struct * p;
1591         unsigned long offset, type;
1592         int result = 0;
1593
1594         type = swp_type(entry);
1595         if (type >= nr_swapfiles)
1596                 goto bad_file;
1597         p = type + swap_info;
1598         offset = swp_offset(entry);
1599
1600         swap_device_lock(p);
1601         if (offset < p->max && p->swap_map[offset]) {
1602                 if (p->swap_map[offset] < SWAP_MAP_MAX - 1) {
1603                         p->swap_map[offset]++;
1604                         result = 1;
1605                 } else if (p->swap_map[offset] <= SWAP_MAP_MAX) {
1606                         if (swap_overflow++ < 5)
1607                                 printk(KERN_WARNING "swap_dup: swap entry overflow\n");
1608                         p->swap_map[offset] = SWAP_MAP_MAX;
1609                         result = 1;
1610                 }
1611         }
1612         swap_device_unlock(p);
1613 out:
1614         return result;
1615
1616 bad_file:
1617         printk(KERN_ERR "swap_dup: %s%08lx\n", Bad_file, entry.val);
1618         goto out;
1619 }
1620
1621 struct swap_info_struct *
1622 get_swap_info_struct(unsigned type)
1623 {
1624         return &swap_info[type];
1625 }
1626
1627 /*
1628  * swap_device_lock prevents swap_map being freed. Don't grab an extra
1629  * reference on the swaphandle, it doesn't matter if it becomes unused.
1630  */
1631 int valid_swaphandles(swp_entry_t entry, unsigned long *offset)
1632 {
1633         int ret = 0, i = 1 << page_cluster;
1634         unsigned long toff;
1635         struct swap_info_struct *swapdev = swp_type(entry) + swap_info;
1636
1637         if (!page_cluster)      /* no readahead */
1638                 return 0;
1639         toff = (swp_offset(entry) >> page_cluster) << page_cluster;
1640         if (!toff)              /* first page is swap header */
1641                 toff++, i--;
1642         *offset = toff;
1643
1644         swap_device_lock(swapdev);
1645         do {
1646                 /* Don't read-ahead past the end of the swap area */
1647                 if (toff >= swapdev->max)
1648                         break;
1649                 /* Don't read in free or bad pages */
1650                 if (!swapdev->swap_map[toff])
1651                         break;
1652                 if (swapdev->swap_map[toff] == SWAP_MAP_BAD)
1653                         break;
1654                 toff++;
1655                 ret++;
1656         } while (--i);
1657         swap_device_unlock(swapdev);
1658         return ret;
1659 }