xref: /openbmc/linux/mm/swapfile.c (revision 78ecba081224a2db5876b6b81cfed0b78f58adc7)
11da177e4SLinus Torvalds /*
21da177e4SLinus Torvalds  *  linux/mm/swapfile.c
31da177e4SLinus Torvalds  *
41da177e4SLinus Torvalds  *  Copyright (C) 1991, 1992, 1993, 1994  Linus Torvalds
51da177e4SLinus Torvalds  *  Swap reorganised 29.12.95, Stephen Tweedie
61da177e4SLinus Torvalds  */
71da177e4SLinus Torvalds 
81da177e4SLinus Torvalds #include <linux/mm.h>
91da177e4SLinus Torvalds #include <linux/hugetlb.h>
101da177e4SLinus Torvalds #include <linux/mman.h>
111da177e4SLinus Torvalds #include <linux/slab.h>
121da177e4SLinus Torvalds #include <linux/kernel_stat.h>
131da177e4SLinus Torvalds #include <linux/swap.h>
141da177e4SLinus Torvalds #include <linux/vmalloc.h>
151da177e4SLinus Torvalds #include <linux/pagemap.h>
161da177e4SLinus Torvalds #include <linux/namei.h>
171da177e4SLinus Torvalds #include <linux/shm.h>
181da177e4SLinus Torvalds #include <linux/blkdev.h>
191da177e4SLinus Torvalds #include <linux/writeback.h>
201da177e4SLinus Torvalds #include <linux/proc_fs.h>
211da177e4SLinus Torvalds #include <linux/seq_file.h>
221da177e4SLinus Torvalds #include <linux/init.h>
231da177e4SLinus Torvalds #include <linux/module.h>
241da177e4SLinus Torvalds #include <linux/rmap.h>
251da177e4SLinus Torvalds #include <linux/security.h>
261da177e4SLinus Torvalds #include <linux/backing-dev.h>
27fc0abb14SIngo Molnar #include <linux/mutex.h>
28c59ede7bSRandy.Dunlap #include <linux/capability.h>
291da177e4SLinus Torvalds #include <linux/syscalls.h>
308a9f3ccdSBalbir Singh #include <linux/memcontrol.h>
311da177e4SLinus Torvalds 
321da177e4SLinus Torvalds #include <asm/pgtable.h>
331da177e4SLinus Torvalds #include <asm/tlbflush.h>
341da177e4SLinus Torvalds #include <linux/swapops.h>
351da177e4SLinus Torvalds 
365d337b91SHugh Dickins DEFINE_SPINLOCK(swap_lock);
371da177e4SLinus Torvalds unsigned int nr_swapfiles;
381da177e4SLinus Torvalds long total_swap_pages;
391da177e4SLinus Torvalds static int swap_overflow;
40*78ecba08SHugh Dickins static int least_priority;
411da177e4SLinus Torvalds 
421da177e4SLinus Torvalds static const char Bad_file[] = "Bad swap file entry ";
431da177e4SLinus Torvalds static const char Unused_file[] = "Unused swap file entry ";
441da177e4SLinus Torvalds static const char Bad_offset[] = "Bad swap offset entry ";
451da177e4SLinus Torvalds static const char Unused_offset[] = "Unused swap offset entry ";
461da177e4SLinus Torvalds 
471da177e4SLinus Torvalds struct swap_list_t swap_list = {-1, -1};
481da177e4SLinus Torvalds 
49f577eb30SRafael J. Wysocki static struct swap_info_struct swap_info[MAX_SWAPFILES];
501da177e4SLinus Torvalds 
51fc0abb14SIngo Molnar static DEFINE_MUTEX(swapon_mutex);
521da177e4SLinus Torvalds 
531da177e4SLinus Torvalds /*
541da177e4SLinus Torvalds  * We need this because the bdev->unplug_fn can sleep and we cannot
555d337b91SHugh Dickins  * hold swap_lock while calling the unplug_fn. And swap_lock
56fc0abb14SIngo Molnar  * cannot be turned into a mutex.
571da177e4SLinus Torvalds  */
581da177e4SLinus Torvalds static DECLARE_RWSEM(swap_unplug_sem);
591da177e4SLinus Torvalds 
601da177e4SLinus Torvalds void swap_unplug_io_fn(struct backing_dev_info *unused_bdi, struct page *page)
611da177e4SLinus Torvalds {
621da177e4SLinus Torvalds 	swp_entry_t entry;
631da177e4SLinus Torvalds 
641da177e4SLinus Torvalds 	down_read(&swap_unplug_sem);
654c21e2f2SHugh Dickins 	entry.val = page_private(page);
661da177e4SLinus Torvalds 	if (PageSwapCache(page)) {
671da177e4SLinus Torvalds 		struct block_device *bdev = swap_info[swp_type(entry)].bdev;
681da177e4SLinus Torvalds 		struct backing_dev_info *bdi;
691da177e4SLinus Torvalds 
701da177e4SLinus Torvalds 		/*
711da177e4SLinus Torvalds 		 * If the page is removed from swapcache from under us (with a
721da177e4SLinus Torvalds 		 * racy try_to_unuse/swapoff) we need an additional reference
734c21e2f2SHugh Dickins 		 * count to avoid reading garbage from page_private(page) above.
744c21e2f2SHugh Dickins 		 * If the WARN_ON triggers during a swapoff it maybe the race
751da177e4SLinus Torvalds 		 * condition and it's harmless. However if it triggers without
761da177e4SLinus Torvalds 		 * swapoff it signals a problem.
771da177e4SLinus Torvalds 		 */
781da177e4SLinus Torvalds 		WARN_ON(page_count(page) <= 1);
791da177e4SLinus Torvalds 
801da177e4SLinus Torvalds 		bdi = bdev->bd_inode->i_mapping->backing_dev_info;
81ba32311eSMcMullan, Jason 		blk_run_backing_dev(bdi, page);
821da177e4SLinus Torvalds 	}
831da177e4SLinus Torvalds 	up_read(&swap_unplug_sem);
841da177e4SLinus Torvalds }
851da177e4SLinus Torvalds 
86048c27fdSHugh Dickins #define SWAPFILE_CLUSTER	256
87048c27fdSHugh Dickins #define LATENCY_LIMIT		256
88048c27fdSHugh Dickins 
896eb396dcSHugh Dickins static inline unsigned long scan_swap_map(struct swap_info_struct *si)
901da177e4SLinus Torvalds {
917dfad418SHugh Dickins 	unsigned long offset, last_in_cluster;
92048c27fdSHugh Dickins 	int latency_ration = LATENCY_LIMIT;
931da177e4SLinus Torvalds 
947dfad418SHugh Dickins 	/*
957dfad418SHugh Dickins 	 * We try to cluster swap pages by allocating them sequentially
967dfad418SHugh Dickins 	 * in swap.  Once we've allocated SWAPFILE_CLUSTER pages this
977dfad418SHugh Dickins 	 * way, however, we resort to first-free allocation, starting
987dfad418SHugh Dickins 	 * a new cluster.  This prevents us from scattering swap pages
997dfad418SHugh Dickins 	 * all over the entire swap partition, so that we reduce
1007dfad418SHugh Dickins 	 * overall disk seek times between swap pages.  -- sct
1017dfad418SHugh Dickins 	 * But we do now try to find an empty cluster.  -Andrea
1021da177e4SLinus Torvalds 	 */
1037dfad418SHugh Dickins 
10452b7efdbSHugh Dickins 	si->flags += SWP_SCANNING;
1057dfad418SHugh Dickins 	if (unlikely(!si->cluster_nr)) {
1067dfad418SHugh Dickins 		si->cluster_nr = SWAPFILE_CLUSTER - 1;
1077dfad418SHugh Dickins 		if (si->pages - si->inuse_pages < SWAPFILE_CLUSTER)
1087dfad418SHugh Dickins 			goto lowest;
1095d337b91SHugh Dickins 		spin_unlock(&swap_lock);
1107dfad418SHugh Dickins 
1117dfad418SHugh Dickins 		offset = si->lowest_bit;
1127dfad418SHugh Dickins 		last_in_cluster = offset + SWAPFILE_CLUSTER - 1;
1137dfad418SHugh Dickins 
1147dfad418SHugh Dickins 		/* Locate the first empty (unaligned) cluster */
1157dfad418SHugh Dickins 		for (; last_in_cluster <= si->highest_bit; offset++) {
1161da177e4SLinus Torvalds 			if (si->swap_map[offset])
1177dfad418SHugh Dickins 				last_in_cluster = offset + SWAPFILE_CLUSTER;
1187dfad418SHugh Dickins 			else if (offset == last_in_cluster) {
1195d337b91SHugh Dickins 				spin_lock(&swap_lock);
1209b65ef59SAkinobu Mita 				si->cluster_next = offset-SWAPFILE_CLUSTER+1;
1217dfad418SHugh Dickins 				goto cluster;
1227dfad418SHugh Dickins 			}
123048c27fdSHugh Dickins 			if (unlikely(--latency_ration < 0)) {
124048c27fdSHugh Dickins 				cond_resched();
125048c27fdSHugh Dickins 				latency_ration = LATENCY_LIMIT;
126048c27fdSHugh Dickins 			}
1277dfad418SHugh Dickins 		}
1285d337b91SHugh Dickins 		spin_lock(&swap_lock);
1297dfad418SHugh Dickins 		goto lowest;
1307dfad418SHugh Dickins 	}
1317dfad418SHugh Dickins 
1327dfad418SHugh Dickins 	si->cluster_nr--;
1337dfad418SHugh Dickins cluster:
1347dfad418SHugh Dickins 	offset = si->cluster_next;
1357dfad418SHugh Dickins 	if (offset > si->highest_bit)
1367dfad418SHugh Dickins lowest:		offset = si->lowest_bit;
13752b7efdbSHugh Dickins checks:	if (!(si->flags & SWP_WRITEOK))
13852b7efdbSHugh Dickins 		goto no_page;
1397dfad418SHugh Dickins 	if (!si->highest_bit)
1407dfad418SHugh Dickins 		goto no_page;
1417dfad418SHugh Dickins 	if (!si->swap_map[offset]) {
14252b7efdbSHugh Dickins 		if (offset == si->lowest_bit)
1431da177e4SLinus Torvalds 			si->lowest_bit++;
1441da177e4SLinus Torvalds 		if (offset == si->highest_bit)
1451da177e4SLinus Torvalds 			si->highest_bit--;
1467dfad418SHugh Dickins 		si->inuse_pages++;
1477dfad418SHugh Dickins 		if (si->inuse_pages == si->pages) {
1481da177e4SLinus Torvalds 			si->lowest_bit = si->max;
1491da177e4SLinus Torvalds 			si->highest_bit = 0;
1501da177e4SLinus Torvalds 		}
1511da177e4SLinus Torvalds 		si->swap_map[offset] = 1;
1521da177e4SLinus Torvalds 		si->cluster_next = offset + 1;
15352b7efdbSHugh Dickins 		si->flags -= SWP_SCANNING;
1541da177e4SLinus Torvalds 		return offset;
1551da177e4SLinus Torvalds 	}
1567dfad418SHugh Dickins 
1575d337b91SHugh Dickins 	spin_unlock(&swap_lock);
1587dfad418SHugh Dickins 	while (++offset <= si->highest_bit) {
15952b7efdbSHugh Dickins 		if (!si->swap_map[offset]) {
1605d337b91SHugh Dickins 			spin_lock(&swap_lock);
16152b7efdbSHugh Dickins 			goto checks;
1627dfad418SHugh Dickins 		}
163048c27fdSHugh Dickins 		if (unlikely(--latency_ration < 0)) {
164048c27fdSHugh Dickins 			cond_resched();
165048c27fdSHugh Dickins 			latency_ration = LATENCY_LIMIT;
166048c27fdSHugh Dickins 		}
16752b7efdbSHugh Dickins 	}
1685d337b91SHugh Dickins 	spin_lock(&swap_lock);
1697dfad418SHugh Dickins 	goto lowest;
1707dfad418SHugh Dickins 
1717dfad418SHugh Dickins no_page:
17252b7efdbSHugh Dickins 	si->flags -= SWP_SCANNING;
1731da177e4SLinus Torvalds 	return 0;
1741da177e4SLinus Torvalds }
1751da177e4SLinus Torvalds 
1761da177e4SLinus Torvalds swp_entry_t get_swap_page(void)
1771da177e4SLinus Torvalds {
178fb4f88dcSHugh Dickins 	struct swap_info_struct *si;
179fb4f88dcSHugh Dickins 	pgoff_t offset;
180fb4f88dcSHugh Dickins 	int type, next;
181fb4f88dcSHugh Dickins 	int wrapped = 0;
1821da177e4SLinus Torvalds 
1835d337b91SHugh Dickins 	spin_lock(&swap_lock);
1841da177e4SLinus Torvalds 	if (nr_swap_pages <= 0)
185fb4f88dcSHugh Dickins 		goto noswap;
186fb4f88dcSHugh Dickins 	nr_swap_pages--;
1871da177e4SLinus Torvalds 
188fb4f88dcSHugh Dickins 	for (type = swap_list.next; type >= 0 && wrapped < 2; type = next) {
189fb4f88dcSHugh Dickins 		si = swap_info + type;
190fb4f88dcSHugh Dickins 		next = si->next;
191fb4f88dcSHugh Dickins 		if (next < 0 ||
192fb4f88dcSHugh Dickins 		    (!wrapped && si->prio != swap_info[next].prio)) {
193fb4f88dcSHugh Dickins 			next = swap_list.head;
194fb4f88dcSHugh Dickins 			wrapped++;
1951da177e4SLinus Torvalds 		}
196fb4f88dcSHugh Dickins 
197fb4f88dcSHugh Dickins 		if (!si->highest_bit)
198fb4f88dcSHugh Dickins 			continue;
199fb4f88dcSHugh Dickins 		if (!(si->flags & SWP_WRITEOK))
200fb4f88dcSHugh Dickins 			continue;
201fb4f88dcSHugh Dickins 
202fb4f88dcSHugh Dickins 		swap_list.next = next;
203fb4f88dcSHugh Dickins 		offset = scan_swap_map(si);
2045d337b91SHugh Dickins 		if (offset) {
2055d337b91SHugh Dickins 			spin_unlock(&swap_lock);
206fb4f88dcSHugh Dickins 			return swp_entry(type, offset);
2075d337b91SHugh Dickins 		}
208fb4f88dcSHugh Dickins 		next = swap_list.next;
209fb4f88dcSHugh Dickins 	}
210fb4f88dcSHugh Dickins 
211fb4f88dcSHugh Dickins 	nr_swap_pages++;
212fb4f88dcSHugh Dickins noswap:
2135d337b91SHugh Dickins 	spin_unlock(&swap_lock);
214fb4f88dcSHugh Dickins 	return (swp_entry_t) {0};
2151da177e4SLinus Torvalds }
2161da177e4SLinus Torvalds 
2173a291a20SRafael J. Wysocki swp_entry_t get_swap_page_of_type(int type)
2183a291a20SRafael J. Wysocki {
2193a291a20SRafael J. Wysocki 	struct swap_info_struct *si;
2203a291a20SRafael J. Wysocki 	pgoff_t offset;
2213a291a20SRafael J. Wysocki 
2223a291a20SRafael J. Wysocki 	spin_lock(&swap_lock);
2233a291a20SRafael J. Wysocki 	si = swap_info + type;
2243a291a20SRafael J. Wysocki 	if (si->flags & SWP_WRITEOK) {
2253a291a20SRafael J. Wysocki 		nr_swap_pages--;
2263a291a20SRafael J. Wysocki 		offset = scan_swap_map(si);
2273a291a20SRafael J. Wysocki 		if (offset) {
2283a291a20SRafael J. Wysocki 			spin_unlock(&swap_lock);
2293a291a20SRafael J. Wysocki 			return swp_entry(type, offset);
2303a291a20SRafael J. Wysocki 		}
2313a291a20SRafael J. Wysocki 		nr_swap_pages++;
2323a291a20SRafael J. Wysocki 	}
2333a291a20SRafael J. Wysocki 	spin_unlock(&swap_lock);
2343a291a20SRafael J. Wysocki 	return (swp_entry_t) {0};
2353a291a20SRafael J. Wysocki }
2363a291a20SRafael J. Wysocki 
2371da177e4SLinus Torvalds static struct swap_info_struct * swap_info_get(swp_entry_t entry)
2381da177e4SLinus Torvalds {
2391da177e4SLinus Torvalds 	struct swap_info_struct * p;
2401da177e4SLinus Torvalds 	unsigned long offset, type;
2411da177e4SLinus Torvalds 
2421da177e4SLinus Torvalds 	if (!entry.val)
2431da177e4SLinus Torvalds 		goto out;
2441da177e4SLinus Torvalds 	type = swp_type(entry);
2451da177e4SLinus Torvalds 	if (type >= nr_swapfiles)
2461da177e4SLinus Torvalds 		goto bad_nofile;
2471da177e4SLinus Torvalds 	p = & swap_info[type];
2481da177e4SLinus Torvalds 	if (!(p->flags & SWP_USED))
2491da177e4SLinus Torvalds 		goto bad_device;
2501da177e4SLinus Torvalds 	offset = swp_offset(entry);
2511da177e4SLinus Torvalds 	if (offset >= p->max)
2521da177e4SLinus Torvalds 		goto bad_offset;
2531da177e4SLinus Torvalds 	if (!p->swap_map[offset])
2541da177e4SLinus Torvalds 		goto bad_free;
2555d337b91SHugh Dickins 	spin_lock(&swap_lock);
2561da177e4SLinus Torvalds 	return p;
2571da177e4SLinus Torvalds 
2581da177e4SLinus Torvalds bad_free:
2591da177e4SLinus Torvalds 	printk(KERN_ERR "swap_free: %s%08lx\n", Unused_offset, entry.val);
2601da177e4SLinus Torvalds 	goto out;
2611da177e4SLinus Torvalds bad_offset:
2621da177e4SLinus Torvalds 	printk(KERN_ERR "swap_free: %s%08lx\n", Bad_offset, entry.val);
2631da177e4SLinus Torvalds 	goto out;
2641da177e4SLinus Torvalds bad_device:
2651da177e4SLinus Torvalds 	printk(KERN_ERR "swap_free: %s%08lx\n", Unused_file, entry.val);
2661da177e4SLinus Torvalds 	goto out;
2671da177e4SLinus Torvalds bad_nofile:
2681da177e4SLinus Torvalds 	printk(KERN_ERR "swap_free: %s%08lx\n", Bad_file, entry.val);
2691da177e4SLinus Torvalds out:
2701da177e4SLinus Torvalds 	return NULL;
2711da177e4SLinus Torvalds }
2721da177e4SLinus Torvalds 
2731da177e4SLinus Torvalds static int swap_entry_free(struct swap_info_struct *p, unsigned long offset)
2741da177e4SLinus Torvalds {
2751da177e4SLinus Torvalds 	int count = p->swap_map[offset];
2761da177e4SLinus Torvalds 
2771da177e4SLinus Torvalds 	if (count < SWAP_MAP_MAX) {
2781da177e4SLinus Torvalds 		count--;
2791da177e4SLinus Torvalds 		p->swap_map[offset] = count;
2801da177e4SLinus Torvalds 		if (!count) {
2811da177e4SLinus Torvalds 			if (offset < p->lowest_bit)
2821da177e4SLinus Torvalds 				p->lowest_bit = offset;
2831da177e4SLinus Torvalds 			if (offset > p->highest_bit)
2841da177e4SLinus Torvalds 				p->highest_bit = offset;
28589d09a2cSHugh Dickins 			if (p->prio > swap_info[swap_list.next].prio)
28689d09a2cSHugh Dickins 				swap_list.next = p - swap_info;
2871da177e4SLinus Torvalds 			nr_swap_pages++;
2881da177e4SLinus Torvalds 			p->inuse_pages--;
2891da177e4SLinus Torvalds 		}
2901da177e4SLinus Torvalds 	}
2911da177e4SLinus Torvalds 	return count;
2921da177e4SLinus Torvalds }
2931da177e4SLinus Torvalds 
2941da177e4SLinus Torvalds /*
2951da177e4SLinus Torvalds  * Caller has made sure that the swapdevice corresponding to entry
2961da177e4SLinus Torvalds  * is still around or has not been recycled.
2971da177e4SLinus Torvalds  */
2981da177e4SLinus Torvalds void swap_free(swp_entry_t entry)
2991da177e4SLinus Torvalds {
3001da177e4SLinus Torvalds 	struct swap_info_struct * p;
3011da177e4SLinus Torvalds 
3021da177e4SLinus Torvalds 	p = swap_info_get(entry);
3031da177e4SLinus Torvalds 	if (p) {
3041da177e4SLinus Torvalds 		swap_entry_free(p, swp_offset(entry));
3055d337b91SHugh Dickins 		spin_unlock(&swap_lock);
3061da177e4SLinus Torvalds 	}
3071da177e4SLinus Torvalds }
3081da177e4SLinus Torvalds 
3091da177e4SLinus Torvalds /*
310c475a8abSHugh Dickins  * How many references to page are currently swapped out?
3111da177e4SLinus Torvalds  */
312c475a8abSHugh Dickins static inline int page_swapcount(struct page *page)
3131da177e4SLinus Torvalds {
314c475a8abSHugh Dickins 	int count = 0;
3151da177e4SLinus Torvalds 	struct swap_info_struct *p;
3161da177e4SLinus Torvalds 	swp_entry_t entry;
3171da177e4SLinus Torvalds 
3184c21e2f2SHugh Dickins 	entry.val = page_private(page);
3191da177e4SLinus Torvalds 	p = swap_info_get(entry);
3201da177e4SLinus Torvalds 	if (p) {
321c475a8abSHugh Dickins 		/* Subtract the 1 for the swap cache itself */
322c475a8abSHugh Dickins 		count = p->swap_map[swp_offset(entry)] - 1;
3235d337b91SHugh Dickins 		spin_unlock(&swap_lock);
3241da177e4SLinus Torvalds 	}
325c475a8abSHugh Dickins 	return count;
3261da177e4SLinus Torvalds }
3271da177e4SLinus Torvalds 
3281da177e4SLinus Torvalds /*
3291da177e4SLinus Torvalds  * We can use this swap cache entry directly
3301da177e4SLinus Torvalds  * if there are no other references to it.
3311da177e4SLinus Torvalds  */
3321da177e4SLinus Torvalds int can_share_swap_page(struct page *page)
3331da177e4SLinus Torvalds {
334c475a8abSHugh Dickins 	int count;
3351da177e4SLinus Torvalds 
336c475a8abSHugh Dickins 	BUG_ON(!PageLocked(page));
337c475a8abSHugh Dickins 	count = page_mapcount(page);
338c475a8abSHugh Dickins 	if (count <= 1 && PageSwapCache(page))
339c475a8abSHugh Dickins 		count += page_swapcount(page);
340c475a8abSHugh Dickins 	return count == 1;
3411da177e4SLinus Torvalds }
3421da177e4SLinus Torvalds 
3431da177e4SLinus Torvalds /*
3441da177e4SLinus Torvalds  * Work out if there are any other processes sharing this
3451da177e4SLinus Torvalds  * swap cache page. Free it if you can. Return success.
3461da177e4SLinus Torvalds  */
3471da177e4SLinus Torvalds int remove_exclusive_swap_page(struct page *page)
3481da177e4SLinus Torvalds {
3491da177e4SLinus Torvalds 	int retval;
3501da177e4SLinus Torvalds 	struct swap_info_struct * p;
3511da177e4SLinus Torvalds 	swp_entry_t entry;
3521da177e4SLinus Torvalds 
3531da177e4SLinus Torvalds 	BUG_ON(PagePrivate(page));
3541da177e4SLinus Torvalds 	BUG_ON(!PageLocked(page));
3551da177e4SLinus Torvalds 
3561da177e4SLinus Torvalds 	if (!PageSwapCache(page))
3571da177e4SLinus Torvalds 		return 0;
3581da177e4SLinus Torvalds 	if (PageWriteback(page))
3591da177e4SLinus Torvalds 		return 0;
3601da177e4SLinus Torvalds 	if (page_count(page) != 2) /* 2: us + cache */
3611da177e4SLinus Torvalds 		return 0;
3621da177e4SLinus Torvalds 
3634c21e2f2SHugh Dickins 	entry.val = page_private(page);
3641da177e4SLinus Torvalds 	p = swap_info_get(entry);
3651da177e4SLinus Torvalds 	if (!p)
3661da177e4SLinus Torvalds 		return 0;
3671da177e4SLinus Torvalds 
3681da177e4SLinus Torvalds 	/* Is the only swap cache user the cache itself? */
3691da177e4SLinus Torvalds 	retval = 0;
3701da177e4SLinus Torvalds 	if (p->swap_map[swp_offset(entry)] == 1) {
3711da177e4SLinus Torvalds 		/* Recheck the page count with the swapcache lock held.. */
3721da177e4SLinus Torvalds 		write_lock_irq(&swapper_space.tree_lock);
3731da177e4SLinus Torvalds 		if ((page_count(page) == 2) && !PageWriteback(page)) {
3741da177e4SLinus Torvalds 			__delete_from_swap_cache(page);
3751da177e4SLinus Torvalds 			SetPageDirty(page);
3761da177e4SLinus Torvalds 			retval = 1;
3771da177e4SLinus Torvalds 		}
3781da177e4SLinus Torvalds 		write_unlock_irq(&swapper_space.tree_lock);
3791da177e4SLinus Torvalds 	}
3805d337b91SHugh Dickins 	spin_unlock(&swap_lock);
3811da177e4SLinus Torvalds 
3821da177e4SLinus Torvalds 	if (retval) {
3831da177e4SLinus Torvalds 		swap_free(entry);
3841da177e4SLinus Torvalds 		page_cache_release(page);
3851da177e4SLinus Torvalds 	}
3861da177e4SLinus Torvalds 
3871da177e4SLinus Torvalds 	return retval;
3881da177e4SLinus Torvalds }
3891da177e4SLinus Torvalds 
3901da177e4SLinus Torvalds /*
3911da177e4SLinus Torvalds  * Free the swap entry like above, but also try to
3921da177e4SLinus Torvalds  * free the page cache entry if it is the last user.
3931da177e4SLinus Torvalds  */
3941da177e4SLinus Torvalds void free_swap_and_cache(swp_entry_t entry)
3951da177e4SLinus Torvalds {
3961da177e4SLinus Torvalds 	struct swap_info_struct * p;
3971da177e4SLinus Torvalds 	struct page *page = NULL;
3981da177e4SLinus Torvalds 
3990697212aSChristoph Lameter 	if (is_migration_entry(entry))
4000697212aSChristoph Lameter 		return;
4010697212aSChristoph Lameter 
4021da177e4SLinus Torvalds 	p = swap_info_get(entry);
4031da177e4SLinus Torvalds 	if (p) {
40493fac704SNick Piggin 		if (swap_entry_free(p, swp_offset(entry)) == 1) {
40593fac704SNick Piggin 			page = find_get_page(&swapper_space, entry.val);
40693fac704SNick Piggin 			if (page && unlikely(TestSetPageLocked(page))) {
40793fac704SNick Piggin 				page_cache_release(page);
40893fac704SNick Piggin 				page = NULL;
40993fac704SNick Piggin 			}
41093fac704SNick Piggin 		}
4115d337b91SHugh Dickins 		spin_unlock(&swap_lock);
4121da177e4SLinus Torvalds 	}
4131da177e4SLinus Torvalds 	if (page) {
4141da177e4SLinus Torvalds 		int one_user;
4151da177e4SLinus Torvalds 
4161da177e4SLinus Torvalds 		BUG_ON(PagePrivate(page));
4171da177e4SLinus Torvalds 		one_user = (page_count(page) == 2);
4181da177e4SLinus Torvalds 		/* Only cache user (+us), or swap space full? Free it! */
41993fac704SNick Piggin 		/* Also recheck PageSwapCache after page is locked (above) */
42093fac704SNick Piggin 		if (PageSwapCache(page) && !PageWriteback(page) &&
42193fac704SNick Piggin 					(one_user || vm_swap_full())) {
4221da177e4SLinus Torvalds 			delete_from_swap_cache(page);
4231da177e4SLinus Torvalds 			SetPageDirty(page);
4241da177e4SLinus Torvalds 		}
4251da177e4SLinus Torvalds 		unlock_page(page);
4261da177e4SLinus Torvalds 		page_cache_release(page);
4271da177e4SLinus Torvalds 	}
4281da177e4SLinus Torvalds }
4291da177e4SLinus Torvalds 
430b0cb1a19SRafael J. Wysocki #ifdef CONFIG_HIBERNATION
431f577eb30SRafael J. Wysocki /*
432915bae9eSRafael J. Wysocki  * Find the swap type that corresponds to given device (if any).
433f577eb30SRafael J. Wysocki  *
434915bae9eSRafael J. Wysocki  * @offset - number of the PAGE_SIZE-sized block of the device, starting
435915bae9eSRafael J. Wysocki  * from 0, in which the swap header is expected to be located.
436915bae9eSRafael J. Wysocki  *
437915bae9eSRafael J. Wysocki  * This is needed for the suspend to disk (aka swsusp).
438f577eb30SRafael J. Wysocki  */
4397bf23687SRafael J. Wysocki int swap_type_of(dev_t device, sector_t offset, struct block_device **bdev_p)
440f577eb30SRafael J. Wysocki {
441915bae9eSRafael J. Wysocki 	struct block_device *bdev = NULL;
442f577eb30SRafael J. Wysocki 	int i;
443f577eb30SRafael J. Wysocki 
444915bae9eSRafael J. Wysocki 	if (device)
445915bae9eSRafael J. Wysocki 		bdev = bdget(device);
446915bae9eSRafael J. Wysocki 
447f577eb30SRafael J. Wysocki 	spin_lock(&swap_lock);
448f577eb30SRafael J. Wysocki 	for (i = 0; i < nr_swapfiles; i++) {
449915bae9eSRafael J. Wysocki 		struct swap_info_struct *sis = swap_info + i;
450f577eb30SRafael J. Wysocki 
451915bae9eSRafael J. Wysocki 		if (!(sis->flags & SWP_WRITEOK))
452f577eb30SRafael J. Wysocki 			continue;
453b6b5bce3SRafael J. Wysocki 
454915bae9eSRafael J. Wysocki 		if (!bdev) {
4557bf23687SRafael J. Wysocki 			if (bdev_p)
4567bf23687SRafael J. Wysocki 				*bdev_p = sis->bdev;
4577bf23687SRafael J. Wysocki 
4586e1819d6SRafael J. Wysocki 			spin_unlock(&swap_lock);
4596e1819d6SRafael J. Wysocki 			return i;
4606e1819d6SRafael J. Wysocki 		}
461915bae9eSRafael J. Wysocki 		if (bdev == sis->bdev) {
462915bae9eSRafael J. Wysocki 			struct swap_extent *se;
463915bae9eSRafael J. Wysocki 
464915bae9eSRafael J. Wysocki 			se = list_entry(sis->extent_list.next,
465915bae9eSRafael J. Wysocki 					struct swap_extent, list);
466915bae9eSRafael J. Wysocki 			if (se->start_block == offset) {
4677bf23687SRafael J. Wysocki 				if (bdev_p)
4687bf23687SRafael J. Wysocki 					*bdev_p = sis->bdev;
4697bf23687SRafael J. Wysocki 
470f577eb30SRafael J. Wysocki 				spin_unlock(&swap_lock);
471915bae9eSRafael J. Wysocki 				bdput(bdev);
472f577eb30SRafael J. Wysocki 				return i;
473f577eb30SRafael J. Wysocki 			}
474f577eb30SRafael J. Wysocki 		}
475915bae9eSRafael J. Wysocki 	}
476f577eb30SRafael J. Wysocki 	spin_unlock(&swap_lock);
477915bae9eSRafael J. Wysocki 	if (bdev)
478915bae9eSRafael J. Wysocki 		bdput(bdev);
479915bae9eSRafael J. Wysocki 
480f577eb30SRafael J. Wysocki 	return -ENODEV;
481f577eb30SRafael J. Wysocki }
482f577eb30SRafael J. Wysocki 
483f577eb30SRafael J. Wysocki /*
484f577eb30SRafael J. Wysocki  * Return either the total number of swap pages of given type, or the number
485f577eb30SRafael J. Wysocki  * of free pages of that type (depending on @free)
486f577eb30SRafael J. Wysocki  *
487f577eb30SRafael J. Wysocki  * This is needed for software suspend
488f577eb30SRafael J. Wysocki  */
489f577eb30SRafael J. Wysocki unsigned int count_swap_pages(int type, int free)
490f577eb30SRafael J. Wysocki {
491f577eb30SRafael J. Wysocki 	unsigned int n = 0;
492f577eb30SRafael J. Wysocki 
493f577eb30SRafael J. Wysocki 	if (type < nr_swapfiles) {
494f577eb30SRafael J. Wysocki 		spin_lock(&swap_lock);
495f577eb30SRafael J. Wysocki 		if (swap_info[type].flags & SWP_WRITEOK) {
496f577eb30SRafael J. Wysocki 			n = swap_info[type].pages;
497f577eb30SRafael J. Wysocki 			if (free)
498f577eb30SRafael J. Wysocki 				n -= swap_info[type].inuse_pages;
499f577eb30SRafael J. Wysocki 		}
500f577eb30SRafael J. Wysocki 		spin_unlock(&swap_lock);
501f577eb30SRafael J. Wysocki 	}
502f577eb30SRafael J. Wysocki 	return n;
503f577eb30SRafael J. Wysocki }
504f577eb30SRafael J. Wysocki #endif
505f577eb30SRafael J. Wysocki 
5061da177e4SLinus Torvalds /*
50772866f6fSHugh Dickins  * No need to decide whether this PTE shares the swap entry with others,
50872866f6fSHugh Dickins  * just let do_wp_page work it out if a write is requested later - to
50972866f6fSHugh Dickins  * force COW, vm_page_prot omits write permission from any private vma.
5101da177e4SLinus Torvalds  */
511044d66c1SHugh Dickins static int unuse_pte(struct vm_area_struct *vma, pmd_t *pmd,
5121da177e4SLinus Torvalds 		unsigned long addr, swp_entry_t entry, struct page *page)
5131da177e4SLinus Torvalds {
514044d66c1SHugh Dickins 	spinlock_t *ptl;
515044d66c1SHugh Dickins 	pte_t *pte;
516044d66c1SHugh Dickins 	int ret = 1;
517044d66c1SHugh Dickins 
518e1a1cd59SBalbir Singh 	if (mem_cgroup_charge(page, vma->vm_mm, GFP_KERNEL))
519044d66c1SHugh Dickins 		ret = -ENOMEM;
520044d66c1SHugh Dickins 
521044d66c1SHugh Dickins 	pte = pte_offset_map_lock(vma->vm_mm, pmd, addr, &ptl);
522044d66c1SHugh Dickins 	if (unlikely(!pte_same(*pte, swp_entry_to_pte(entry)))) {
523044d66c1SHugh Dickins 		if (ret > 0)
524044d66c1SHugh Dickins 			mem_cgroup_uncharge_page(page);
525044d66c1SHugh Dickins 		ret = 0;
526044d66c1SHugh Dickins 		goto out;
527044d66c1SHugh Dickins 	}
5288a9f3ccdSBalbir Singh 
5294294621fSHugh Dickins 	inc_mm_counter(vma->vm_mm, anon_rss);
5301da177e4SLinus Torvalds 	get_page(page);
5311da177e4SLinus Torvalds 	set_pte_at(vma->vm_mm, addr, pte,
5321da177e4SLinus Torvalds 		   pte_mkold(mk_pte(page, vma->vm_page_prot)));
5331da177e4SLinus Torvalds 	page_add_anon_rmap(page, vma, addr);
5341da177e4SLinus Torvalds 	swap_free(entry);
5351da177e4SLinus Torvalds 	/*
5361da177e4SLinus Torvalds 	 * Move the page to the active list so it is not
5371da177e4SLinus Torvalds 	 * immediately swapped out again after swapon.
5381da177e4SLinus Torvalds 	 */
5391da177e4SLinus Torvalds 	activate_page(page);
540044d66c1SHugh Dickins out:
541044d66c1SHugh Dickins 	pte_unmap_unlock(pte, ptl);
542044d66c1SHugh Dickins 	return ret;
5431da177e4SLinus Torvalds }
5441da177e4SLinus Torvalds 
5451da177e4SLinus Torvalds static int unuse_pte_range(struct vm_area_struct *vma, pmd_t *pmd,
5461da177e4SLinus Torvalds 				unsigned long addr, unsigned long end,
5471da177e4SLinus Torvalds 				swp_entry_t entry, struct page *page)
5481da177e4SLinus Torvalds {
5491da177e4SLinus Torvalds 	pte_t swp_pte = swp_entry_to_pte(entry);
550705e87c0SHugh Dickins 	pte_t *pte;
5518a9f3ccdSBalbir Singh 	int ret = 0;
5521da177e4SLinus Torvalds 
553044d66c1SHugh Dickins 	/*
554044d66c1SHugh Dickins 	 * We don't actually need pte lock while scanning for swp_pte: since
555044d66c1SHugh Dickins 	 * we hold page lock and mmap_sem, swp_pte cannot be inserted into the
556044d66c1SHugh Dickins 	 * page table while we're scanning; though it could get zapped, and on
557044d66c1SHugh Dickins 	 * some architectures (e.g. x86_32 with PAE) we might catch a glimpse
558044d66c1SHugh Dickins 	 * of unmatched parts which look like swp_pte, so unuse_pte must
559044d66c1SHugh Dickins 	 * recheck under pte lock.  Scanning without pte lock lets it be
560044d66c1SHugh Dickins 	 * preemptible whenever CONFIG_PREEMPT but not CONFIG_HIGHPTE.
561044d66c1SHugh Dickins 	 */
562044d66c1SHugh Dickins 	pte = pte_offset_map(pmd, addr);
5631da177e4SLinus Torvalds 	do {
5641da177e4SLinus Torvalds 		/*
5651da177e4SLinus Torvalds 		 * swapoff spends a _lot_ of time in this loop!
5661da177e4SLinus Torvalds 		 * Test inline before going to call unuse_pte.
5671da177e4SLinus Torvalds 		 */
5681da177e4SLinus Torvalds 		if (unlikely(pte_same(*pte, swp_pte))) {
569044d66c1SHugh Dickins 			pte_unmap(pte);
570044d66c1SHugh Dickins 			ret = unuse_pte(vma, pmd, addr, entry, page);
571044d66c1SHugh Dickins 			if (ret)
572044d66c1SHugh Dickins 				goto out;
573044d66c1SHugh Dickins 			pte = pte_offset_map(pmd, addr);
5741da177e4SLinus Torvalds 		}
5751da177e4SLinus Torvalds 	} while (pte++, addr += PAGE_SIZE, addr != end);
576044d66c1SHugh Dickins 	pte_unmap(pte - 1);
577044d66c1SHugh Dickins out:
5788a9f3ccdSBalbir Singh 	return ret;
5791da177e4SLinus Torvalds }
5801da177e4SLinus Torvalds 
5811da177e4SLinus Torvalds static inline int unuse_pmd_range(struct vm_area_struct *vma, pud_t *pud,
5821da177e4SLinus Torvalds 				unsigned long addr, unsigned long end,
5831da177e4SLinus Torvalds 				swp_entry_t entry, struct page *page)
5841da177e4SLinus Torvalds {
5851da177e4SLinus Torvalds 	pmd_t *pmd;
5861da177e4SLinus Torvalds 	unsigned long next;
5878a9f3ccdSBalbir Singh 	int ret;
5881da177e4SLinus Torvalds 
5891da177e4SLinus Torvalds 	pmd = pmd_offset(pud, addr);
5901da177e4SLinus Torvalds 	do {
5911da177e4SLinus Torvalds 		next = pmd_addr_end(addr, end);
5921da177e4SLinus Torvalds 		if (pmd_none_or_clear_bad(pmd))
5931da177e4SLinus Torvalds 			continue;
5948a9f3ccdSBalbir Singh 		ret = unuse_pte_range(vma, pmd, addr, next, entry, page);
5958a9f3ccdSBalbir Singh 		if (ret)
5968a9f3ccdSBalbir Singh 			return ret;
5971da177e4SLinus Torvalds 	} while (pmd++, addr = next, addr != end);
5981da177e4SLinus Torvalds 	return 0;
5991da177e4SLinus Torvalds }
6001da177e4SLinus Torvalds 
6011da177e4SLinus Torvalds static inline int unuse_pud_range(struct vm_area_struct *vma, pgd_t *pgd,
6021da177e4SLinus Torvalds 				unsigned long addr, unsigned long end,
6031da177e4SLinus Torvalds 				swp_entry_t entry, struct page *page)
6041da177e4SLinus Torvalds {
6051da177e4SLinus Torvalds 	pud_t *pud;
6061da177e4SLinus Torvalds 	unsigned long next;
6078a9f3ccdSBalbir Singh 	int ret;
6081da177e4SLinus Torvalds 
6091da177e4SLinus Torvalds 	pud = pud_offset(pgd, addr);
6101da177e4SLinus Torvalds 	do {
6111da177e4SLinus Torvalds 		next = pud_addr_end(addr, end);
6121da177e4SLinus Torvalds 		if (pud_none_or_clear_bad(pud))
6131da177e4SLinus Torvalds 			continue;
6148a9f3ccdSBalbir Singh 		ret = unuse_pmd_range(vma, pud, addr, next, entry, page);
6158a9f3ccdSBalbir Singh 		if (ret)
6168a9f3ccdSBalbir Singh 			return ret;
6171da177e4SLinus Torvalds 	} while (pud++, addr = next, addr != end);
6181da177e4SLinus Torvalds 	return 0;
6191da177e4SLinus Torvalds }
6201da177e4SLinus Torvalds 
6211da177e4SLinus Torvalds static int unuse_vma(struct vm_area_struct *vma,
6221da177e4SLinus Torvalds 				swp_entry_t entry, struct page *page)
6231da177e4SLinus Torvalds {
6241da177e4SLinus Torvalds 	pgd_t *pgd;
6251da177e4SLinus Torvalds 	unsigned long addr, end, next;
6268a9f3ccdSBalbir Singh 	int ret;
6271da177e4SLinus Torvalds 
6281da177e4SLinus Torvalds 	if (page->mapping) {
6291da177e4SLinus Torvalds 		addr = page_address_in_vma(page, vma);
6301da177e4SLinus Torvalds 		if (addr == -EFAULT)
6311da177e4SLinus Torvalds 			return 0;
6321da177e4SLinus Torvalds 		else
6331da177e4SLinus Torvalds 			end = addr + PAGE_SIZE;
6341da177e4SLinus Torvalds 	} else {
6351da177e4SLinus Torvalds 		addr = vma->vm_start;
6361da177e4SLinus Torvalds 		end = vma->vm_end;
6371da177e4SLinus Torvalds 	}
6381da177e4SLinus Torvalds 
6391da177e4SLinus Torvalds 	pgd = pgd_offset(vma->vm_mm, addr);
6401da177e4SLinus Torvalds 	do {
6411da177e4SLinus Torvalds 		next = pgd_addr_end(addr, end);
6421da177e4SLinus Torvalds 		if (pgd_none_or_clear_bad(pgd))
6431da177e4SLinus Torvalds 			continue;
6448a9f3ccdSBalbir Singh 		ret = unuse_pud_range(vma, pgd, addr, next, entry, page);
6458a9f3ccdSBalbir Singh 		if (ret)
6468a9f3ccdSBalbir Singh 			return ret;
6471da177e4SLinus Torvalds 	} while (pgd++, addr = next, addr != end);
6481da177e4SLinus Torvalds 	return 0;
6491da177e4SLinus Torvalds }
6501da177e4SLinus Torvalds 
6511da177e4SLinus Torvalds static int unuse_mm(struct mm_struct *mm,
6521da177e4SLinus Torvalds 				swp_entry_t entry, struct page *page)
6531da177e4SLinus Torvalds {
6541da177e4SLinus Torvalds 	struct vm_area_struct *vma;
6558a9f3ccdSBalbir Singh 	int ret = 0;
6561da177e4SLinus Torvalds 
6571da177e4SLinus Torvalds 	if (!down_read_trylock(&mm->mmap_sem)) {
6581da177e4SLinus Torvalds 		/*
659c475a8abSHugh Dickins 		 * Activate page so shrink_cache is unlikely to unmap its
660c475a8abSHugh Dickins 		 * ptes while lock is dropped, so swapoff can make progress.
6611da177e4SLinus Torvalds 		 */
662c475a8abSHugh Dickins 		activate_page(page);
6631da177e4SLinus Torvalds 		unlock_page(page);
6641da177e4SLinus Torvalds 		down_read(&mm->mmap_sem);
6651da177e4SLinus Torvalds 		lock_page(page);
6661da177e4SLinus Torvalds 	}
6671da177e4SLinus Torvalds 	for (vma = mm->mmap; vma; vma = vma->vm_next) {
6688a9f3ccdSBalbir Singh 		if (vma->anon_vma && (ret = unuse_vma(vma, entry, page)))
6691da177e4SLinus Torvalds 			break;
6701da177e4SLinus Torvalds 	}
6711da177e4SLinus Torvalds 	up_read(&mm->mmap_sem);
6728a9f3ccdSBalbir Singh 	return (ret < 0)? ret: 0;
6731da177e4SLinus Torvalds }
6741da177e4SLinus Torvalds 
6751da177e4SLinus Torvalds /*
6761da177e4SLinus Torvalds  * Scan swap_map from current position to next entry still in use.
6771da177e4SLinus Torvalds  * Recycle to start on reaching the end, returning 0 when empty.
6781da177e4SLinus Torvalds  */
6796eb396dcSHugh Dickins static unsigned int find_next_to_unuse(struct swap_info_struct *si,
6806eb396dcSHugh Dickins 					unsigned int prev)
6811da177e4SLinus Torvalds {
6826eb396dcSHugh Dickins 	unsigned int max = si->max;
6836eb396dcSHugh Dickins 	unsigned int i = prev;
6841da177e4SLinus Torvalds 	int count;
6851da177e4SLinus Torvalds 
6861da177e4SLinus Torvalds 	/*
6875d337b91SHugh Dickins 	 * No need for swap_lock here: we're just looking
6881da177e4SLinus Torvalds 	 * for whether an entry is in use, not modifying it; false
6891da177e4SLinus Torvalds 	 * hits are okay, and sys_swapoff() has already prevented new
6905d337b91SHugh Dickins 	 * allocations from this area (while holding swap_lock).
6911da177e4SLinus Torvalds 	 */
6921da177e4SLinus Torvalds 	for (;;) {
6931da177e4SLinus Torvalds 		if (++i >= max) {
6941da177e4SLinus Torvalds 			if (!prev) {
6951da177e4SLinus Torvalds 				i = 0;
6961da177e4SLinus Torvalds 				break;
6971da177e4SLinus Torvalds 			}
6981da177e4SLinus Torvalds 			/*
6991da177e4SLinus Torvalds 			 * No entries in use at top of swap_map,
7001da177e4SLinus Torvalds 			 * loop back to start and recheck there.
7011da177e4SLinus Torvalds 			 */
7021da177e4SLinus Torvalds 			max = prev + 1;
7031da177e4SLinus Torvalds 			prev = 0;
7041da177e4SLinus Torvalds 			i = 1;
7051da177e4SLinus Torvalds 		}
7061da177e4SLinus Torvalds 		count = si->swap_map[i];
7071da177e4SLinus Torvalds 		if (count && count != SWAP_MAP_BAD)
7081da177e4SLinus Torvalds 			break;
7091da177e4SLinus Torvalds 	}
7101da177e4SLinus Torvalds 	return i;
7111da177e4SLinus Torvalds }
7121da177e4SLinus Torvalds 
7131da177e4SLinus Torvalds /*
7141da177e4SLinus Torvalds  * We completely avoid races by reading each swap page in advance,
7151da177e4SLinus Torvalds  * and then search for the process using it.  All the necessary
7161da177e4SLinus Torvalds  * page table adjustments can then be made atomically.
7171da177e4SLinus Torvalds  */
7181da177e4SLinus Torvalds static int try_to_unuse(unsigned int type)
7191da177e4SLinus Torvalds {
7201da177e4SLinus Torvalds 	struct swap_info_struct * si = &swap_info[type];
7211da177e4SLinus Torvalds 	struct mm_struct *start_mm;
7221da177e4SLinus Torvalds 	unsigned short *swap_map;
7231da177e4SLinus Torvalds 	unsigned short swcount;
7241da177e4SLinus Torvalds 	struct page *page;
7251da177e4SLinus Torvalds 	swp_entry_t entry;
7266eb396dcSHugh Dickins 	unsigned int i = 0;
7271da177e4SLinus Torvalds 	int retval = 0;
7281da177e4SLinus Torvalds 	int reset_overflow = 0;
7291da177e4SLinus Torvalds 	int shmem;
7301da177e4SLinus Torvalds 
7311da177e4SLinus Torvalds 	/*
7321da177e4SLinus Torvalds 	 * When searching mms for an entry, a good strategy is to
7331da177e4SLinus Torvalds 	 * start at the first mm we freed the previous entry from
7341da177e4SLinus Torvalds 	 * (though actually we don't notice whether we or coincidence
7351da177e4SLinus Torvalds 	 * freed the entry).  Initialize this start_mm with a hold.
7361da177e4SLinus Torvalds 	 *
7371da177e4SLinus Torvalds 	 * A simpler strategy would be to start at the last mm we
7381da177e4SLinus Torvalds 	 * freed the previous entry from; but that would take less
7391da177e4SLinus Torvalds 	 * advantage of mmlist ordering, which clusters forked mms
7401da177e4SLinus Torvalds 	 * together, child after parent.  If we race with dup_mmap(), we
7411da177e4SLinus Torvalds 	 * prefer to resolve parent before child, lest we miss entries
7421da177e4SLinus Torvalds 	 * duplicated after we scanned child: using last mm would invert
7431da177e4SLinus Torvalds 	 * that.  Though it's only a serious concern when an overflowed
7441da177e4SLinus Torvalds 	 * swap count is reset from SWAP_MAP_MAX, preventing a rescan.
7451da177e4SLinus Torvalds 	 */
7461da177e4SLinus Torvalds 	start_mm = &init_mm;
7471da177e4SLinus Torvalds 	atomic_inc(&init_mm.mm_users);
7481da177e4SLinus Torvalds 
7491da177e4SLinus Torvalds 	/*
7501da177e4SLinus Torvalds 	 * Keep on scanning until all entries have gone.  Usually,
7511da177e4SLinus Torvalds 	 * one pass through swap_map is enough, but not necessarily:
7521da177e4SLinus Torvalds 	 * there are races when an instance of an entry might be missed.
7531da177e4SLinus Torvalds 	 */
7541da177e4SLinus Torvalds 	while ((i = find_next_to_unuse(si, i)) != 0) {
7551da177e4SLinus Torvalds 		if (signal_pending(current)) {
7561da177e4SLinus Torvalds 			retval = -EINTR;
7571da177e4SLinus Torvalds 			break;
7581da177e4SLinus Torvalds 		}
7591da177e4SLinus Torvalds 
7601da177e4SLinus Torvalds 		/*
7611da177e4SLinus Torvalds 		 * Get a page for the entry, using the existing swap
7621da177e4SLinus Torvalds 		 * cache page if there is one.  Otherwise, get a clean
7631da177e4SLinus Torvalds 		 * page and read the swap into it.
7641da177e4SLinus Torvalds 		 */
7651da177e4SLinus Torvalds 		swap_map = &si->swap_map[i];
7661da177e4SLinus Torvalds 		entry = swp_entry(type, i);
76702098feaSHugh Dickins 		page = read_swap_cache_async(entry,
76802098feaSHugh Dickins 					GFP_HIGHUSER_MOVABLE, NULL, 0);
7691da177e4SLinus Torvalds 		if (!page) {
7701da177e4SLinus Torvalds 			/*
7711da177e4SLinus Torvalds 			 * Either swap_duplicate() failed because entry
7721da177e4SLinus Torvalds 			 * has been freed independently, and will not be
7731da177e4SLinus Torvalds 			 * reused since sys_swapoff() already disabled
7741da177e4SLinus Torvalds 			 * allocation from here, or alloc_page() failed.
7751da177e4SLinus Torvalds 			 */
7761da177e4SLinus Torvalds 			if (!*swap_map)
7771da177e4SLinus Torvalds 				continue;
7781da177e4SLinus Torvalds 			retval = -ENOMEM;
7791da177e4SLinus Torvalds 			break;
7801da177e4SLinus Torvalds 		}
7811da177e4SLinus Torvalds 
7821da177e4SLinus Torvalds 		/*
7831da177e4SLinus Torvalds 		 * Don't hold on to start_mm if it looks like exiting.
7841da177e4SLinus Torvalds 		 */
7851da177e4SLinus Torvalds 		if (atomic_read(&start_mm->mm_users) == 1) {
7861da177e4SLinus Torvalds 			mmput(start_mm);
7871da177e4SLinus Torvalds 			start_mm = &init_mm;
7881da177e4SLinus Torvalds 			atomic_inc(&init_mm.mm_users);
7891da177e4SLinus Torvalds 		}
7901da177e4SLinus Torvalds 
7911da177e4SLinus Torvalds 		/*
7921da177e4SLinus Torvalds 		 * Wait for and lock page.  When do_swap_page races with
7931da177e4SLinus Torvalds 		 * try_to_unuse, do_swap_page can handle the fault much
7941da177e4SLinus Torvalds 		 * faster than try_to_unuse can locate the entry.  This
7951da177e4SLinus Torvalds 		 * apparently redundant "wait_on_page_locked" lets try_to_unuse
7961da177e4SLinus Torvalds 		 * defer to do_swap_page in such a case - in some tests,
7971da177e4SLinus Torvalds 		 * do_swap_page and try_to_unuse repeatedly compete.
7981da177e4SLinus Torvalds 		 */
7991da177e4SLinus Torvalds 		wait_on_page_locked(page);
8001da177e4SLinus Torvalds 		wait_on_page_writeback(page);
8011da177e4SLinus Torvalds 		lock_page(page);
8021da177e4SLinus Torvalds 		wait_on_page_writeback(page);
8031da177e4SLinus Torvalds 
8041da177e4SLinus Torvalds 		/*
8051da177e4SLinus Torvalds 		 * Remove all references to entry.
8061da177e4SLinus Torvalds 		 * Whenever we reach init_mm, there's no address space
8071da177e4SLinus Torvalds 		 * to search, but use it as a reminder to search shmem.
8081da177e4SLinus Torvalds 		 */
8091da177e4SLinus Torvalds 		shmem = 0;
8101da177e4SLinus Torvalds 		swcount = *swap_map;
8111da177e4SLinus Torvalds 		if (swcount > 1) {
8121da177e4SLinus Torvalds 			if (start_mm == &init_mm)
8131da177e4SLinus Torvalds 				shmem = shmem_unuse(entry, page);
8141da177e4SLinus Torvalds 			else
8151da177e4SLinus Torvalds 				retval = unuse_mm(start_mm, entry, page);
8161da177e4SLinus Torvalds 		}
8171da177e4SLinus Torvalds 		if (*swap_map > 1) {
8181da177e4SLinus Torvalds 			int set_start_mm = (*swap_map >= swcount);
8191da177e4SLinus Torvalds 			struct list_head *p = &start_mm->mmlist;
8201da177e4SLinus Torvalds 			struct mm_struct *new_start_mm = start_mm;
8211da177e4SLinus Torvalds 			struct mm_struct *prev_mm = start_mm;
8221da177e4SLinus Torvalds 			struct mm_struct *mm;
8231da177e4SLinus Torvalds 
8241da177e4SLinus Torvalds 			atomic_inc(&new_start_mm->mm_users);
8251da177e4SLinus Torvalds 			atomic_inc(&prev_mm->mm_users);
8261da177e4SLinus Torvalds 			spin_lock(&mmlist_lock);
8272e0e26c7SHugh Dickins 			while (*swap_map > 1 && !retval && !shmem &&
8281da177e4SLinus Torvalds 					(p = p->next) != &start_mm->mmlist) {
8291da177e4SLinus Torvalds 				mm = list_entry(p, struct mm_struct, mmlist);
83070af7c5cSHugh Dickins 				if (!atomic_inc_not_zero(&mm->mm_users))
8311da177e4SLinus Torvalds 					continue;
8321da177e4SLinus Torvalds 				spin_unlock(&mmlist_lock);
8331da177e4SLinus Torvalds 				mmput(prev_mm);
8341da177e4SLinus Torvalds 				prev_mm = mm;
8351da177e4SLinus Torvalds 
8361da177e4SLinus Torvalds 				cond_resched();
8371da177e4SLinus Torvalds 
8381da177e4SLinus Torvalds 				swcount = *swap_map;
8391da177e4SLinus Torvalds 				if (swcount <= 1)
8401da177e4SLinus Torvalds 					;
8411da177e4SLinus Torvalds 				else if (mm == &init_mm) {
8421da177e4SLinus Torvalds 					set_start_mm = 1;
8431da177e4SLinus Torvalds 					shmem = shmem_unuse(entry, page);
8441da177e4SLinus Torvalds 				} else
8451da177e4SLinus Torvalds 					retval = unuse_mm(mm, entry, page);
8461da177e4SLinus Torvalds 				if (set_start_mm && *swap_map < swcount) {
8471da177e4SLinus Torvalds 					mmput(new_start_mm);
8481da177e4SLinus Torvalds 					atomic_inc(&mm->mm_users);
8491da177e4SLinus Torvalds 					new_start_mm = mm;
8501da177e4SLinus Torvalds 					set_start_mm = 0;
8511da177e4SLinus Torvalds 				}
8521da177e4SLinus Torvalds 				spin_lock(&mmlist_lock);
8531da177e4SLinus Torvalds 			}
8541da177e4SLinus Torvalds 			spin_unlock(&mmlist_lock);
8551da177e4SLinus Torvalds 			mmput(prev_mm);
8561da177e4SLinus Torvalds 			mmput(start_mm);
8571da177e4SLinus Torvalds 			start_mm = new_start_mm;
8581da177e4SLinus Torvalds 		}
8592e0e26c7SHugh Dickins 		if (shmem) {
8602e0e26c7SHugh Dickins 			/* page has already been unlocked and released */
8612e0e26c7SHugh Dickins 			if (shmem > 0)
8622e0e26c7SHugh Dickins 				continue;
8632e0e26c7SHugh Dickins 			retval = shmem;
8642e0e26c7SHugh Dickins 			break;
8652e0e26c7SHugh Dickins 		}
8661da177e4SLinus Torvalds 		if (retval) {
8671da177e4SLinus Torvalds 			unlock_page(page);
8681da177e4SLinus Torvalds 			page_cache_release(page);
8691da177e4SLinus Torvalds 			break;
8701da177e4SLinus Torvalds 		}
8711da177e4SLinus Torvalds 
8721da177e4SLinus Torvalds 		/*
8731da177e4SLinus Torvalds 		 * How could swap count reach 0x7fff when the maximum
8741da177e4SLinus Torvalds 		 * pid is 0x7fff, and there's no way to repeat a swap
8751da177e4SLinus Torvalds 		 * page within an mm (except in shmem, where it's the
8761da177e4SLinus Torvalds 		 * shared object which takes the reference count)?
8771da177e4SLinus Torvalds 		 * We believe SWAP_MAP_MAX cannot occur in Linux 2.4.
8781da177e4SLinus Torvalds 		 *
8791da177e4SLinus Torvalds 		 * If that's wrong, then we should worry more about
8801da177e4SLinus Torvalds 		 * exit_mmap() and do_munmap() cases described above:
8811da177e4SLinus Torvalds 		 * we might be resetting SWAP_MAP_MAX too early here.
8821da177e4SLinus Torvalds 		 * We know "Undead"s can happen, they're okay, so don't
8831da177e4SLinus Torvalds 		 * report them; but do report if we reset SWAP_MAP_MAX.
8841da177e4SLinus Torvalds 		 */
8851da177e4SLinus Torvalds 		if (*swap_map == SWAP_MAP_MAX) {
8865d337b91SHugh Dickins 			spin_lock(&swap_lock);
8871da177e4SLinus Torvalds 			*swap_map = 1;
8885d337b91SHugh Dickins 			spin_unlock(&swap_lock);
8891da177e4SLinus Torvalds 			reset_overflow = 1;
8901da177e4SLinus Torvalds 		}
8911da177e4SLinus Torvalds 
8921da177e4SLinus Torvalds 		/*
8931da177e4SLinus Torvalds 		 * If a reference remains (rare), we would like to leave
8941da177e4SLinus Torvalds 		 * the page in the swap cache; but try_to_unmap could
8951da177e4SLinus Torvalds 		 * then re-duplicate the entry once we drop page lock,
8961da177e4SLinus Torvalds 		 * so we might loop indefinitely; also, that page could
8971da177e4SLinus Torvalds 		 * not be swapped out to other storage meanwhile.  So:
8981da177e4SLinus Torvalds 		 * delete from cache even if there's another reference,
8991da177e4SLinus Torvalds 		 * after ensuring that the data has been saved to disk -
9001da177e4SLinus Torvalds 		 * since if the reference remains (rarer), it will be
9011da177e4SLinus Torvalds 		 * read from disk into another page.  Splitting into two
9021da177e4SLinus Torvalds 		 * pages would be incorrect if swap supported "shared
9031da177e4SLinus Torvalds 		 * private" pages, but they are handled by tmpfs files.
9041da177e4SLinus Torvalds 		 */
9051da177e4SLinus Torvalds 		if ((*swap_map > 1) && PageDirty(page) && PageSwapCache(page)) {
9061da177e4SLinus Torvalds 			struct writeback_control wbc = {
9071da177e4SLinus Torvalds 				.sync_mode = WB_SYNC_NONE,
9081da177e4SLinus Torvalds 			};
9091da177e4SLinus Torvalds 
9101da177e4SLinus Torvalds 			swap_writepage(page, &wbc);
9111da177e4SLinus Torvalds 			lock_page(page);
9121da177e4SLinus Torvalds 			wait_on_page_writeback(page);
9131da177e4SLinus Torvalds 		}
9142e0e26c7SHugh Dickins 		if (PageSwapCache(page))
9151da177e4SLinus Torvalds 			delete_from_swap_cache(page);
9161da177e4SLinus Torvalds 
9171da177e4SLinus Torvalds 		/*
9181da177e4SLinus Torvalds 		 * So we could skip searching mms once swap count went
9191da177e4SLinus Torvalds 		 * to 1, we did not mark any present ptes as dirty: must
9202706a1b8SAnderson Briglia 		 * mark page dirty so shrink_page_list will preserve it.
9211da177e4SLinus Torvalds 		 */
9221da177e4SLinus Torvalds 		SetPageDirty(page);
9231da177e4SLinus Torvalds 		unlock_page(page);
9241da177e4SLinus Torvalds 		page_cache_release(page);
9251da177e4SLinus Torvalds 
9261da177e4SLinus Torvalds 		/*
9271da177e4SLinus Torvalds 		 * Make sure that we aren't completely killing
9281da177e4SLinus Torvalds 		 * interactive performance.
9291da177e4SLinus Torvalds 		 */
9301da177e4SLinus Torvalds 		cond_resched();
9311da177e4SLinus Torvalds 	}
9321da177e4SLinus Torvalds 
9331da177e4SLinus Torvalds 	mmput(start_mm);
9341da177e4SLinus Torvalds 	if (reset_overflow) {
9351da177e4SLinus Torvalds 		printk(KERN_WARNING "swapoff: cleared swap entry overflow\n");
9361da177e4SLinus Torvalds 		swap_overflow = 0;
9371da177e4SLinus Torvalds 	}
9381da177e4SLinus Torvalds 	return retval;
9391da177e4SLinus Torvalds }
9401da177e4SLinus Torvalds 
9411da177e4SLinus Torvalds /*
9425d337b91SHugh Dickins  * After a successful try_to_unuse, if no swap is now in use, we know
9435d337b91SHugh Dickins  * we can empty the mmlist.  swap_lock must be held on entry and exit.
9445d337b91SHugh Dickins  * Note that mmlist_lock nests inside swap_lock, and an mm must be
9451da177e4SLinus Torvalds  * added to the mmlist just after page_duplicate - before would be racy.
9461da177e4SLinus Torvalds  */
9471da177e4SLinus Torvalds static void drain_mmlist(void)
9481da177e4SLinus Torvalds {
9491da177e4SLinus Torvalds 	struct list_head *p, *next;
9501da177e4SLinus Torvalds 	unsigned int i;
9511da177e4SLinus Torvalds 
9521da177e4SLinus Torvalds 	for (i = 0; i < nr_swapfiles; i++)
9531da177e4SLinus Torvalds 		if (swap_info[i].inuse_pages)
9541da177e4SLinus Torvalds 			return;
9551da177e4SLinus Torvalds 	spin_lock(&mmlist_lock);
9561da177e4SLinus Torvalds 	list_for_each_safe(p, next, &init_mm.mmlist)
9571da177e4SLinus Torvalds 		list_del_init(p);
9581da177e4SLinus Torvalds 	spin_unlock(&mmlist_lock);
9591da177e4SLinus Torvalds }
9601da177e4SLinus Torvalds 
9611da177e4SLinus Torvalds /*
9621da177e4SLinus Torvalds  * Use this swapdev's extent info to locate the (PAGE_SIZE) block which
9631da177e4SLinus Torvalds  * corresponds to page offset `offset'.
9641da177e4SLinus Torvalds  */
9651da177e4SLinus Torvalds sector_t map_swap_page(struct swap_info_struct *sis, pgoff_t offset)
9661da177e4SLinus Torvalds {
9671da177e4SLinus Torvalds 	struct swap_extent *se = sis->curr_swap_extent;
9681da177e4SLinus Torvalds 	struct swap_extent *start_se = se;
9691da177e4SLinus Torvalds 
9701da177e4SLinus Torvalds 	for ( ; ; ) {
9711da177e4SLinus Torvalds 		struct list_head *lh;
9721da177e4SLinus Torvalds 
9731da177e4SLinus Torvalds 		if (se->start_page <= offset &&
9741da177e4SLinus Torvalds 				offset < (se->start_page + se->nr_pages)) {
9751da177e4SLinus Torvalds 			return se->start_block + (offset - se->start_page);
9761da177e4SLinus Torvalds 		}
97711d31886SHugh Dickins 		lh = se->list.next;
9781da177e4SLinus Torvalds 		if (lh == &sis->extent_list)
97911d31886SHugh Dickins 			lh = lh->next;
9801da177e4SLinus Torvalds 		se = list_entry(lh, struct swap_extent, list);
9811da177e4SLinus Torvalds 		sis->curr_swap_extent = se;
9821da177e4SLinus Torvalds 		BUG_ON(se == start_se);		/* It *must* be present */
9831da177e4SLinus Torvalds 	}
9841da177e4SLinus Torvalds }
9851da177e4SLinus Torvalds 
986b0cb1a19SRafael J. Wysocki #ifdef CONFIG_HIBERNATION
9873aef83e0SRafael J. Wysocki /*
9883aef83e0SRafael J. Wysocki  * Get the (PAGE_SIZE) block corresponding to given offset on the swapdev
9893aef83e0SRafael J. Wysocki  * corresponding to given index in swap_info (swap type).
9903aef83e0SRafael J. Wysocki  */
9913aef83e0SRafael J. Wysocki sector_t swapdev_block(int swap_type, pgoff_t offset)
9923aef83e0SRafael J. Wysocki {
9933aef83e0SRafael J. Wysocki 	struct swap_info_struct *sis;
9943aef83e0SRafael J. Wysocki 
9953aef83e0SRafael J. Wysocki 	if (swap_type >= nr_swapfiles)
9963aef83e0SRafael J. Wysocki 		return 0;
9973aef83e0SRafael J. Wysocki 
9983aef83e0SRafael J. Wysocki 	sis = swap_info + swap_type;
9993aef83e0SRafael J. Wysocki 	return (sis->flags & SWP_WRITEOK) ? map_swap_page(sis, offset) : 0;
10003aef83e0SRafael J. Wysocki }
1001b0cb1a19SRafael J. Wysocki #endif /* CONFIG_HIBERNATION */
10023aef83e0SRafael J. Wysocki 
10031da177e4SLinus Torvalds /*
10041da177e4SLinus Torvalds  * Free all of a swapdev's extent information
10051da177e4SLinus Torvalds  */
10061da177e4SLinus Torvalds static void destroy_swap_extents(struct swap_info_struct *sis)
10071da177e4SLinus Torvalds {
10081da177e4SLinus Torvalds 	while (!list_empty(&sis->extent_list)) {
10091da177e4SLinus Torvalds 		struct swap_extent *se;
10101da177e4SLinus Torvalds 
10111da177e4SLinus Torvalds 		se = list_entry(sis->extent_list.next,
10121da177e4SLinus Torvalds 				struct swap_extent, list);
10131da177e4SLinus Torvalds 		list_del(&se->list);
10141da177e4SLinus Torvalds 		kfree(se);
10151da177e4SLinus Torvalds 	}
10161da177e4SLinus Torvalds }
10171da177e4SLinus Torvalds 
10181da177e4SLinus Torvalds /*
10191da177e4SLinus Torvalds  * Add a block range (and the corresponding page range) into this swapdev's
102011d31886SHugh Dickins  * extent list.  The extent list is kept sorted in page order.
10211da177e4SLinus Torvalds  *
102211d31886SHugh Dickins  * This function rather assumes that it is called in ascending page order.
10231da177e4SLinus Torvalds  */
10241da177e4SLinus Torvalds static int
10251da177e4SLinus Torvalds add_swap_extent(struct swap_info_struct *sis, unsigned long start_page,
10261da177e4SLinus Torvalds 		unsigned long nr_pages, sector_t start_block)
10271da177e4SLinus Torvalds {
10281da177e4SLinus Torvalds 	struct swap_extent *se;
10291da177e4SLinus Torvalds 	struct swap_extent *new_se;
10301da177e4SLinus Torvalds 	struct list_head *lh;
10311da177e4SLinus Torvalds 
103211d31886SHugh Dickins 	lh = sis->extent_list.prev;	/* The highest page extent */
103311d31886SHugh Dickins 	if (lh != &sis->extent_list) {
10341da177e4SLinus Torvalds 		se = list_entry(lh, struct swap_extent, list);
103511d31886SHugh Dickins 		BUG_ON(se->start_page + se->nr_pages != start_page);
103611d31886SHugh Dickins 		if (se->start_block + se->nr_pages == start_block) {
10371da177e4SLinus Torvalds 			/* Merge it */
10381da177e4SLinus Torvalds 			se->nr_pages += nr_pages;
10391da177e4SLinus Torvalds 			return 0;
10401da177e4SLinus Torvalds 		}
10411da177e4SLinus Torvalds 	}
10421da177e4SLinus Torvalds 
10431da177e4SLinus Torvalds 	/*
10441da177e4SLinus Torvalds 	 * No merge.  Insert a new extent, preserving ordering.
10451da177e4SLinus Torvalds 	 */
10461da177e4SLinus Torvalds 	new_se = kmalloc(sizeof(*se), GFP_KERNEL);
10471da177e4SLinus Torvalds 	if (new_se == NULL)
10481da177e4SLinus Torvalds 		return -ENOMEM;
10491da177e4SLinus Torvalds 	new_se->start_page = start_page;
10501da177e4SLinus Torvalds 	new_se->nr_pages = nr_pages;
10511da177e4SLinus Torvalds 	new_se->start_block = start_block;
10521da177e4SLinus Torvalds 
105311d31886SHugh Dickins 	list_add_tail(&new_se->list, &sis->extent_list);
105453092a74SHugh Dickins 	return 1;
10551da177e4SLinus Torvalds }
10561da177e4SLinus Torvalds 
10571da177e4SLinus Torvalds /*
10581da177e4SLinus Torvalds  * A `swap extent' is a simple thing which maps a contiguous range of pages
10591da177e4SLinus Torvalds  * onto a contiguous range of disk blocks.  An ordered list of swap extents
10601da177e4SLinus Torvalds  * is built at swapon time and is then used at swap_writepage/swap_readpage
10611da177e4SLinus Torvalds  * time for locating where on disk a page belongs.
10621da177e4SLinus Torvalds  *
10631da177e4SLinus Torvalds  * If the swapfile is an S_ISBLK block device, a single extent is installed.
10641da177e4SLinus Torvalds  * This is done so that the main operating code can treat S_ISBLK and S_ISREG
10651da177e4SLinus Torvalds  * swap files identically.
10661da177e4SLinus Torvalds  *
10671da177e4SLinus Torvalds  * Whether the swapdev is an S_ISREG file or an S_ISBLK blockdev, the swap
10681da177e4SLinus Torvalds  * extent list operates in PAGE_SIZE disk blocks.  Both S_ISREG and S_ISBLK
10691da177e4SLinus Torvalds  * swapfiles are handled *identically* after swapon time.
10701da177e4SLinus Torvalds  *
10711da177e4SLinus Torvalds  * For S_ISREG swapfiles, setup_swap_extents() will walk all the file's blocks
10721da177e4SLinus Torvalds  * and will parse them into an ordered extent list, in PAGE_SIZE chunks.  If
10731da177e4SLinus Torvalds  * some stray blocks are found which do not fall within the PAGE_SIZE alignment
10741da177e4SLinus Torvalds  * requirements, they are simply tossed out - we will never use those blocks
10751da177e4SLinus Torvalds  * for swapping.
10761da177e4SLinus Torvalds  *
1077b0d9bcd4SHugh Dickins  * For S_ISREG swapfiles we set S_SWAPFILE across the life of the swapon.  This
10781da177e4SLinus Torvalds  * prevents root from shooting her foot off by ftruncating an in-use swapfile,
10791da177e4SLinus Torvalds  * which will scribble on the fs.
10801da177e4SLinus Torvalds  *
10811da177e4SLinus Torvalds  * The amount of disk space which a single swap extent represents varies.
10821da177e4SLinus Torvalds  * Typically it is in the 1-4 megabyte range.  So we can have hundreds of
10831da177e4SLinus Torvalds  * extents in the list.  To avoid much list walking, we cache the previous
10841da177e4SLinus Torvalds  * search location in `curr_swap_extent', and start new searches from there.
10851da177e4SLinus Torvalds  * This is extremely effective.  The average number of iterations in
10861da177e4SLinus Torvalds  * map_swap_page() has been measured at about 0.3 per page.  - akpm.
10871da177e4SLinus Torvalds  */
108853092a74SHugh Dickins static int setup_swap_extents(struct swap_info_struct *sis, sector_t *span)
10891da177e4SLinus Torvalds {
10901da177e4SLinus Torvalds 	struct inode *inode;
10911da177e4SLinus Torvalds 	unsigned blocks_per_page;
10921da177e4SLinus Torvalds 	unsigned long page_no;
10931da177e4SLinus Torvalds 	unsigned blkbits;
10941da177e4SLinus Torvalds 	sector_t probe_block;
10951da177e4SLinus Torvalds 	sector_t last_block;
109653092a74SHugh Dickins 	sector_t lowest_block = -1;
109753092a74SHugh Dickins 	sector_t highest_block = 0;
109853092a74SHugh Dickins 	int nr_extents = 0;
10991da177e4SLinus Torvalds 	int ret;
11001da177e4SLinus Torvalds 
11011da177e4SLinus Torvalds 	inode = sis->swap_file->f_mapping->host;
11021da177e4SLinus Torvalds 	if (S_ISBLK(inode->i_mode)) {
11031da177e4SLinus Torvalds 		ret = add_swap_extent(sis, 0, sis->max, 0);
110453092a74SHugh Dickins 		*span = sis->pages;
11051da177e4SLinus Torvalds 		goto done;
11061da177e4SLinus Torvalds 	}
11071da177e4SLinus Torvalds 
11081da177e4SLinus Torvalds 	blkbits = inode->i_blkbits;
11091da177e4SLinus Torvalds 	blocks_per_page = PAGE_SIZE >> blkbits;
11101da177e4SLinus Torvalds 
11111da177e4SLinus Torvalds 	/*
11121da177e4SLinus Torvalds 	 * Map all the blocks into the extent list.  This code doesn't try
11131da177e4SLinus Torvalds 	 * to be very smart.
11141da177e4SLinus Torvalds 	 */
11151da177e4SLinus Torvalds 	probe_block = 0;
11161da177e4SLinus Torvalds 	page_no = 0;
11171da177e4SLinus Torvalds 	last_block = i_size_read(inode) >> blkbits;
11181da177e4SLinus Torvalds 	while ((probe_block + blocks_per_page) <= last_block &&
11191da177e4SLinus Torvalds 			page_no < sis->max) {
11201da177e4SLinus Torvalds 		unsigned block_in_page;
11211da177e4SLinus Torvalds 		sector_t first_block;
11221da177e4SLinus Torvalds 
11231da177e4SLinus Torvalds 		first_block = bmap(inode, probe_block);
11241da177e4SLinus Torvalds 		if (first_block == 0)
11251da177e4SLinus Torvalds 			goto bad_bmap;
11261da177e4SLinus Torvalds 
11271da177e4SLinus Torvalds 		/*
11281da177e4SLinus Torvalds 		 * It must be PAGE_SIZE aligned on-disk
11291da177e4SLinus Torvalds 		 */
11301da177e4SLinus Torvalds 		if (first_block & (blocks_per_page - 1)) {
11311da177e4SLinus Torvalds 			probe_block++;
11321da177e4SLinus Torvalds 			goto reprobe;
11331da177e4SLinus Torvalds 		}
11341da177e4SLinus Torvalds 
11351da177e4SLinus Torvalds 		for (block_in_page = 1; block_in_page < blocks_per_page;
11361da177e4SLinus Torvalds 					block_in_page++) {
11371da177e4SLinus Torvalds 			sector_t block;
11381da177e4SLinus Torvalds 
11391da177e4SLinus Torvalds 			block = bmap(inode, probe_block + block_in_page);
11401da177e4SLinus Torvalds 			if (block == 0)
11411da177e4SLinus Torvalds 				goto bad_bmap;
11421da177e4SLinus Torvalds 			if (block != first_block + block_in_page) {
11431da177e4SLinus Torvalds 				/* Discontiguity */
11441da177e4SLinus Torvalds 				probe_block++;
11451da177e4SLinus Torvalds 				goto reprobe;
11461da177e4SLinus Torvalds 			}
11471da177e4SLinus Torvalds 		}
11481da177e4SLinus Torvalds 
114953092a74SHugh Dickins 		first_block >>= (PAGE_SHIFT - blkbits);
115053092a74SHugh Dickins 		if (page_no) {	/* exclude the header page */
115153092a74SHugh Dickins 			if (first_block < lowest_block)
115253092a74SHugh Dickins 				lowest_block = first_block;
115353092a74SHugh Dickins 			if (first_block > highest_block)
115453092a74SHugh Dickins 				highest_block = first_block;
115553092a74SHugh Dickins 		}
115653092a74SHugh Dickins 
11571da177e4SLinus Torvalds 		/*
11581da177e4SLinus Torvalds 		 * We found a PAGE_SIZE-length, PAGE_SIZE-aligned run of blocks
11591da177e4SLinus Torvalds 		 */
116053092a74SHugh Dickins 		ret = add_swap_extent(sis, page_no, 1, first_block);
116153092a74SHugh Dickins 		if (ret < 0)
11621da177e4SLinus Torvalds 			goto out;
116353092a74SHugh Dickins 		nr_extents += ret;
11641da177e4SLinus Torvalds 		page_no++;
11651da177e4SLinus Torvalds 		probe_block += blocks_per_page;
11661da177e4SLinus Torvalds reprobe:
11671da177e4SLinus Torvalds 		continue;
11681da177e4SLinus Torvalds 	}
116953092a74SHugh Dickins 	ret = nr_extents;
117053092a74SHugh Dickins 	*span = 1 + highest_block - lowest_block;
11711da177e4SLinus Torvalds 	if (page_no == 0)
1172e2244ec2SHugh Dickins 		page_no = 1;	/* force Empty message */
11731da177e4SLinus Torvalds 	sis->max = page_no;
1174e2244ec2SHugh Dickins 	sis->pages = page_no - 1;
11751da177e4SLinus Torvalds 	sis->highest_bit = page_no - 1;
11761da177e4SLinus Torvalds done:
11771da177e4SLinus Torvalds 	sis->curr_swap_extent = list_entry(sis->extent_list.prev,
11781da177e4SLinus Torvalds 					struct swap_extent, list);
11791da177e4SLinus Torvalds 	goto out;
11801da177e4SLinus Torvalds bad_bmap:
11811da177e4SLinus Torvalds 	printk(KERN_ERR "swapon: swapfile has holes\n");
11821da177e4SLinus Torvalds 	ret = -EINVAL;
11831da177e4SLinus Torvalds out:
11841da177e4SLinus Torvalds 	return ret;
11851da177e4SLinus Torvalds }
11861da177e4SLinus Torvalds 
11871da177e4SLinus Torvalds #if 0	/* We don't need this yet */
11881da177e4SLinus Torvalds #include <linux/backing-dev.h>
11891da177e4SLinus Torvalds int page_queue_congested(struct page *page)
11901da177e4SLinus Torvalds {
11911da177e4SLinus Torvalds 	struct backing_dev_info *bdi;
11921da177e4SLinus Torvalds 
11931da177e4SLinus Torvalds 	BUG_ON(!PageLocked(page));	/* It pins the swap_info_struct */
11941da177e4SLinus Torvalds 
11951da177e4SLinus Torvalds 	if (PageSwapCache(page)) {
11964c21e2f2SHugh Dickins 		swp_entry_t entry = { .val = page_private(page) };
11971da177e4SLinus Torvalds 		struct swap_info_struct *sis;
11981da177e4SLinus Torvalds 
11991da177e4SLinus Torvalds 		sis = get_swap_info_struct(swp_type(entry));
12001da177e4SLinus Torvalds 		bdi = sis->bdev->bd_inode->i_mapping->backing_dev_info;
12011da177e4SLinus Torvalds 	} else
12021da177e4SLinus Torvalds 		bdi = page->mapping->backing_dev_info;
12031da177e4SLinus Torvalds 	return bdi_write_congested(bdi);
12041da177e4SLinus Torvalds }
12051da177e4SLinus Torvalds #endif
12061da177e4SLinus Torvalds 
12071da177e4SLinus Torvalds asmlinkage long sys_swapoff(const char __user * specialfile)
12081da177e4SLinus Torvalds {
12091da177e4SLinus Torvalds 	struct swap_info_struct * p = NULL;
12101da177e4SLinus Torvalds 	unsigned short *swap_map;
12111da177e4SLinus Torvalds 	struct file *swap_file, *victim;
12121da177e4SLinus Torvalds 	struct address_space *mapping;
12131da177e4SLinus Torvalds 	struct inode *inode;
12141da177e4SLinus Torvalds 	char * pathname;
12151da177e4SLinus Torvalds 	int i, type, prev;
12161da177e4SLinus Torvalds 	int err;
12171da177e4SLinus Torvalds 
12181da177e4SLinus Torvalds 	if (!capable(CAP_SYS_ADMIN))
12191da177e4SLinus Torvalds 		return -EPERM;
12201da177e4SLinus Torvalds 
12211da177e4SLinus Torvalds 	pathname = getname(specialfile);
12221da177e4SLinus Torvalds 	err = PTR_ERR(pathname);
12231da177e4SLinus Torvalds 	if (IS_ERR(pathname))
12241da177e4SLinus Torvalds 		goto out;
12251da177e4SLinus Torvalds 
12261da177e4SLinus Torvalds 	victim = filp_open(pathname, O_RDWR|O_LARGEFILE, 0);
12271da177e4SLinus Torvalds 	putname(pathname);
12281da177e4SLinus Torvalds 	err = PTR_ERR(victim);
12291da177e4SLinus Torvalds 	if (IS_ERR(victim))
12301da177e4SLinus Torvalds 		goto out;
12311da177e4SLinus Torvalds 
12321da177e4SLinus Torvalds 	mapping = victim->f_mapping;
12331da177e4SLinus Torvalds 	prev = -1;
12345d337b91SHugh Dickins 	spin_lock(&swap_lock);
12351da177e4SLinus Torvalds 	for (type = swap_list.head; type >= 0; type = swap_info[type].next) {
12361da177e4SLinus Torvalds 		p = swap_info + type;
12371da177e4SLinus Torvalds 		if ((p->flags & SWP_ACTIVE) == SWP_ACTIVE) {
12381da177e4SLinus Torvalds 			if (p->swap_file->f_mapping == mapping)
12391da177e4SLinus Torvalds 				break;
12401da177e4SLinus Torvalds 		}
12411da177e4SLinus Torvalds 		prev = type;
12421da177e4SLinus Torvalds 	}
12431da177e4SLinus Torvalds 	if (type < 0) {
12441da177e4SLinus Torvalds 		err = -EINVAL;
12455d337b91SHugh Dickins 		spin_unlock(&swap_lock);
12461da177e4SLinus Torvalds 		goto out_dput;
12471da177e4SLinus Torvalds 	}
12481da177e4SLinus Torvalds 	if (!security_vm_enough_memory(p->pages))
12491da177e4SLinus Torvalds 		vm_unacct_memory(p->pages);
12501da177e4SLinus Torvalds 	else {
12511da177e4SLinus Torvalds 		err = -ENOMEM;
12525d337b91SHugh Dickins 		spin_unlock(&swap_lock);
12531da177e4SLinus Torvalds 		goto out_dput;
12541da177e4SLinus Torvalds 	}
12551da177e4SLinus Torvalds 	if (prev < 0) {
12561da177e4SLinus Torvalds 		swap_list.head = p->next;
12571da177e4SLinus Torvalds 	} else {
12581da177e4SLinus Torvalds 		swap_info[prev].next = p->next;
12591da177e4SLinus Torvalds 	}
12601da177e4SLinus Torvalds 	if (type == swap_list.next) {
12611da177e4SLinus Torvalds 		/* just pick something that's safe... */
12621da177e4SLinus Torvalds 		swap_list.next = swap_list.head;
12631da177e4SLinus Torvalds 	}
1264*78ecba08SHugh Dickins 	if (p->prio < 0) {
1265*78ecba08SHugh Dickins 		for (i = p->next; i >= 0; i = swap_info[i].next)
1266*78ecba08SHugh Dickins 			swap_info[i].prio = p->prio--;
1267*78ecba08SHugh Dickins 		least_priority++;
1268*78ecba08SHugh Dickins 	}
12691da177e4SLinus Torvalds 	nr_swap_pages -= p->pages;
12701da177e4SLinus Torvalds 	total_swap_pages -= p->pages;
12711da177e4SLinus Torvalds 	p->flags &= ~SWP_WRITEOK;
12725d337b91SHugh Dickins 	spin_unlock(&swap_lock);
1273fb4f88dcSHugh Dickins 
12741da177e4SLinus Torvalds 	current->flags |= PF_SWAPOFF;
12751da177e4SLinus Torvalds 	err = try_to_unuse(type);
12761da177e4SLinus Torvalds 	current->flags &= ~PF_SWAPOFF;
12771da177e4SLinus Torvalds 
12781da177e4SLinus Torvalds 	if (err) {
12791da177e4SLinus Torvalds 		/* re-insert swap space back into swap_list */
12805d337b91SHugh Dickins 		spin_lock(&swap_lock);
1281*78ecba08SHugh Dickins 		if (p->prio < 0)
1282*78ecba08SHugh Dickins 			p->prio = --least_priority;
1283*78ecba08SHugh Dickins 		prev = -1;
1284*78ecba08SHugh Dickins 		for (i = swap_list.head; i >= 0; i = swap_info[i].next) {
12851da177e4SLinus Torvalds 			if (p->prio >= swap_info[i].prio)
12861da177e4SLinus Torvalds 				break;
1287*78ecba08SHugh Dickins 			prev = i;
1288*78ecba08SHugh Dickins 		}
12891da177e4SLinus Torvalds 		p->next = i;
12901da177e4SLinus Torvalds 		if (prev < 0)
12911da177e4SLinus Torvalds 			swap_list.head = swap_list.next = p - swap_info;
12921da177e4SLinus Torvalds 		else
12931da177e4SLinus Torvalds 			swap_info[prev].next = p - swap_info;
12941da177e4SLinus Torvalds 		nr_swap_pages += p->pages;
12951da177e4SLinus Torvalds 		total_swap_pages += p->pages;
12961da177e4SLinus Torvalds 		p->flags |= SWP_WRITEOK;
12975d337b91SHugh Dickins 		spin_unlock(&swap_lock);
12981da177e4SLinus Torvalds 		goto out_dput;
12991da177e4SLinus Torvalds 	}
130052b7efdbSHugh Dickins 
130152b7efdbSHugh Dickins 	/* wait for any unplug function to finish */
130252b7efdbSHugh Dickins 	down_write(&swap_unplug_sem);
130352b7efdbSHugh Dickins 	up_write(&swap_unplug_sem);
130452b7efdbSHugh Dickins 
13054cd3bb10SHugh Dickins 	destroy_swap_extents(p);
1306fc0abb14SIngo Molnar 	mutex_lock(&swapon_mutex);
13075d337b91SHugh Dickins 	spin_lock(&swap_lock);
13081da177e4SLinus Torvalds 	drain_mmlist();
13095d337b91SHugh Dickins 
13105d337b91SHugh Dickins 	/* wait for anyone still in scan_swap_map */
13115d337b91SHugh Dickins 	p->highest_bit = 0;		/* cuts scans short */
13125d337b91SHugh Dickins 	while (p->flags >= SWP_SCANNING) {
13135d337b91SHugh Dickins 		spin_unlock(&swap_lock);
131413e4b57fSNishanth Aravamudan 		schedule_timeout_uninterruptible(1);
13155d337b91SHugh Dickins 		spin_lock(&swap_lock);
13165d337b91SHugh Dickins 	}
13175d337b91SHugh Dickins 
13181da177e4SLinus Torvalds 	swap_file = p->swap_file;
13191da177e4SLinus Torvalds 	p->swap_file = NULL;
13201da177e4SLinus Torvalds 	p->max = 0;
13211da177e4SLinus Torvalds 	swap_map = p->swap_map;
13221da177e4SLinus Torvalds 	p->swap_map = NULL;
13231da177e4SLinus Torvalds 	p->flags = 0;
13245d337b91SHugh Dickins 	spin_unlock(&swap_lock);
1325fc0abb14SIngo Molnar 	mutex_unlock(&swapon_mutex);
13261da177e4SLinus Torvalds 	vfree(swap_map);
13271da177e4SLinus Torvalds 	inode = mapping->host;
13281da177e4SLinus Torvalds 	if (S_ISBLK(inode->i_mode)) {
13291da177e4SLinus Torvalds 		struct block_device *bdev = I_BDEV(inode);
13301da177e4SLinus Torvalds 		set_blocksize(bdev, p->old_block_size);
13311da177e4SLinus Torvalds 		bd_release(bdev);
13321da177e4SLinus Torvalds 	} else {
13331b1dcc1bSJes Sorensen 		mutex_lock(&inode->i_mutex);
13341da177e4SLinus Torvalds 		inode->i_flags &= ~S_SWAPFILE;
13351b1dcc1bSJes Sorensen 		mutex_unlock(&inode->i_mutex);
13361da177e4SLinus Torvalds 	}
13371da177e4SLinus Torvalds 	filp_close(swap_file, NULL);
13381da177e4SLinus Torvalds 	err = 0;
13391da177e4SLinus Torvalds 
13401da177e4SLinus Torvalds out_dput:
13411da177e4SLinus Torvalds 	filp_close(victim, NULL);
13421da177e4SLinus Torvalds out:
13431da177e4SLinus Torvalds 	return err;
13441da177e4SLinus Torvalds }
13451da177e4SLinus Torvalds 
13461da177e4SLinus Torvalds #ifdef CONFIG_PROC_FS
13471da177e4SLinus Torvalds /* iterator */
13481da177e4SLinus Torvalds static void *swap_start(struct seq_file *swap, loff_t *pos)
13491da177e4SLinus Torvalds {
13501da177e4SLinus Torvalds 	struct swap_info_struct *ptr = swap_info;
13511da177e4SLinus Torvalds 	int i;
13521da177e4SLinus Torvalds 	loff_t l = *pos;
13531da177e4SLinus Torvalds 
1354fc0abb14SIngo Molnar 	mutex_lock(&swapon_mutex);
13551da177e4SLinus Torvalds 
1356881e4aabSSuleiman Souhlal 	if (!l)
1357881e4aabSSuleiman Souhlal 		return SEQ_START_TOKEN;
1358881e4aabSSuleiman Souhlal 
13591da177e4SLinus Torvalds 	for (i = 0; i < nr_swapfiles; i++, ptr++) {
13601da177e4SLinus Torvalds 		if (!(ptr->flags & SWP_USED) || !ptr->swap_map)
13611da177e4SLinus Torvalds 			continue;
1362881e4aabSSuleiman Souhlal 		if (!--l)
13631da177e4SLinus Torvalds 			return ptr;
13641da177e4SLinus Torvalds 	}
13651da177e4SLinus Torvalds 
13661da177e4SLinus Torvalds 	return NULL;
13671da177e4SLinus Torvalds }
13681da177e4SLinus Torvalds 
13691da177e4SLinus Torvalds static void *swap_next(struct seq_file *swap, void *v, loff_t *pos)
13701da177e4SLinus Torvalds {
1371881e4aabSSuleiman Souhlal 	struct swap_info_struct *ptr;
13721da177e4SLinus Torvalds 	struct swap_info_struct *endptr = swap_info + nr_swapfiles;
13731da177e4SLinus Torvalds 
1374881e4aabSSuleiman Souhlal 	if (v == SEQ_START_TOKEN)
1375881e4aabSSuleiman Souhlal 		ptr = swap_info;
1376881e4aabSSuleiman Souhlal 	else {
1377881e4aabSSuleiman Souhlal 		ptr = v;
1378881e4aabSSuleiman Souhlal 		ptr++;
1379881e4aabSSuleiman Souhlal 	}
1380881e4aabSSuleiman Souhlal 
1381881e4aabSSuleiman Souhlal 	for (; ptr < endptr; ptr++) {
13821da177e4SLinus Torvalds 		if (!(ptr->flags & SWP_USED) || !ptr->swap_map)
13831da177e4SLinus Torvalds 			continue;
13841da177e4SLinus Torvalds 		++*pos;
13851da177e4SLinus Torvalds 		return ptr;
13861da177e4SLinus Torvalds 	}
13871da177e4SLinus Torvalds 
13881da177e4SLinus Torvalds 	return NULL;
13891da177e4SLinus Torvalds }
13901da177e4SLinus Torvalds 
13911da177e4SLinus Torvalds static void swap_stop(struct seq_file *swap, void *v)
13921da177e4SLinus Torvalds {
1393fc0abb14SIngo Molnar 	mutex_unlock(&swapon_mutex);
13941da177e4SLinus Torvalds }
13951da177e4SLinus Torvalds 
13961da177e4SLinus Torvalds static int swap_show(struct seq_file *swap, void *v)
13971da177e4SLinus Torvalds {
13981da177e4SLinus Torvalds 	struct swap_info_struct *ptr = v;
13991da177e4SLinus Torvalds 	struct file *file;
14001da177e4SLinus Torvalds 	int len;
14011da177e4SLinus Torvalds 
1402881e4aabSSuleiman Souhlal 	if (ptr == SEQ_START_TOKEN) {
14031da177e4SLinus Torvalds 		seq_puts(swap,"Filename\t\t\t\tType\t\tSize\tUsed\tPriority\n");
1404881e4aabSSuleiman Souhlal 		return 0;
1405881e4aabSSuleiman Souhlal 	}
14061da177e4SLinus Torvalds 
14071da177e4SLinus Torvalds 	file = ptr->swap_file;
1408c32c2f63SJan Blunck 	len = seq_path(swap, &file->f_path, " \t\n\\");
14096eb396dcSHugh Dickins 	seq_printf(swap, "%*s%s\t%u\t%u\t%d\n",
14101da177e4SLinus Torvalds 		       len < 40 ? 40 - len : 1, " ",
1411d3ac7f89SJosef "Jeff" Sipek 		       S_ISBLK(file->f_path.dentry->d_inode->i_mode) ?
14121da177e4SLinus Torvalds 				"partition" : "file\t",
14131da177e4SLinus Torvalds 		       ptr->pages << (PAGE_SHIFT - 10),
14141da177e4SLinus Torvalds 		       ptr->inuse_pages << (PAGE_SHIFT - 10),
14151da177e4SLinus Torvalds 		       ptr->prio);
14161da177e4SLinus Torvalds 	return 0;
14171da177e4SLinus Torvalds }
14181da177e4SLinus Torvalds 
141915ad7cdcSHelge Deller static const struct seq_operations swaps_op = {
14201da177e4SLinus Torvalds 	.start =	swap_start,
14211da177e4SLinus Torvalds 	.next =		swap_next,
14221da177e4SLinus Torvalds 	.stop =		swap_stop,
14231da177e4SLinus Torvalds 	.show =		swap_show
14241da177e4SLinus Torvalds };
14251da177e4SLinus Torvalds 
14261da177e4SLinus Torvalds static int swaps_open(struct inode *inode, struct file *file)
14271da177e4SLinus Torvalds {
14281da177e4SLinus Torvalds 	return seq_open(file, &swaps_op);
14291da177e4SLinus Torvalds }
14301da177e4SLinus Torvalds 
143115ad7cdcSHelge Deller static const struct file_operations proc_swaps_operations = {
14321da177e4SLinus Torvalds 	.open		= swaps_open,
14331da177e4SLinus Torvalds 	.read		= seq_read,
14341da177e4SLinus Torvalds 	.llseek		= seq_lseek,
14351da177e4SLinus Torvalds 	.release	= seq_release,
14361da177e4SLinus Torvalds };
14371da177e4SLinus Torvalds 
14381da177e4SLinus Torvalds static int __init procswaps_init(void)
14391da177e4SLinus Torvalds {
14403d71f86fSDenis V. Lunev 	proc_create("swaps", 0, NULL, &proc_swaps_operations);
14411da177e4SLinus Torvalds 	return 0;
14421da177e4SLinus Torvalds }
14431da177e4SLinus Torvalds __initcall(procswaps_init);
14441da177e4SLinus Torvalds #endif /* CONFIG_PROC_FS */
14451da177e4SLinus Torvalds 
14461da177e4SLinus Torvalds /*
14471da177e4SLinus Torvalds  * Written 01/25/92 by Simmule Turner, heavily changed by Linus.
14481da177e4SLinus Torvalds  *
14491da177e4SLinus Torvalds  * The swapon system call
14501da177e4SLinus Torvalds  */
14511da177e4SLinus Torvalds asmlinkage long sys_swapon(const char __user * specialfile, int swap_flags)
14521da177e4SLinus Torvalds {
14531da177e4SLinus Torvalds 	struct swap_info_struct * p;
14541da177e4SLinus Torvalds 	char *name = NULL;
14551da177e4SLinus Torvalds 	struct block_device *bdev = NULL;
14561da177e4SLinus Torvalds 	struct file *swap_file = NULL;
14571da177e4SLinus Torvalds 	struct address_space *mapping;
14581da177e4SLinus Torvalds 	unsigned int type;
14591da177e4SLinus Torvalds 	int i, prev;
14601da177e4SLinus Torvalds 	int error;
14611da177e4SLinus Torvalds 	union swap_header *swap_header = NULL;
14621da177e4SLinus Torvalds 	int swap_header_version;
14636eb396dcSHugh Dickins 	unsigned int nr_good_pages = 0;
14646eb396dcSHugh Dickins 	int nr_extents = 0;
146553092a74SHugh Dickins 	sector_t span;
14661da177e4SLinus Torvalds 	unsigned long maxpages = 1;
14671da177e4SLinus Torvalds 	int swapfilesize;
1468*78ecba08SHugh Dickins 	unsigned short *swap_map = NULL;
14691da177e4SLinus Torvalds 	struct page *page = NULL;
14701da177e4SLinus Torvalds 	struct inode *inode = NULL;
14711da177e4SLinus Torvalds 	int did_down = 0;
14721da177e4SLinus Torvalds 
14731da177e4SLinus Torvalds 	if (!capable(CAP_SYS_ADMIN))
14741da177e4SLinus Torvalds 		return -EPERM;
14755d337b91SHugh Dickins 	spin_lock(&swap_lock);
14761da177e4SLinus Torvalds 	p = swap_info;
14771da177e4SLinus Torvalds 	for (type = 0 ; type < nr_swapfiles ; type++,p++)
14781da177e4SLinus Torvalds 		if (!(p->flags & SWP_USED))
14791da177e4SLinus Torvalds 			break;
14801da177e4SLinus Torvalds 	error = -EPERM;
14810697212aSChristoph Lameter 	if (type >= MAX_SWAPFILES) {
14825d337b91SHugh Dickins 		spin_unlock(&swap_lock);
14831da177e4SLinus Torvalds 		goto out;
14841da177e4SLinus Torvalds 	}
14851da177e4SLinus Torvalds 	if (type >= nr_swapfiles)
14861da177e4SLinus Torvalds 		nr_swapfiles = type+1;
1487*78ecba08SHugh Dickins 	memset(p, 0, sizeof(*p));
14881da177e4SLinus Torvalds 	INIT_LIST_HEAD(&p->extent_list);
14891da177e4SLinus Torvalds 	p->flags = SWP_USED;
14901da177e4SLinus Torvalds 	p->next = -1;
14915d337b91SHugh Dickins 	spin_unlock(&swap_lock);
14921da177e4SLinus Torvalds 	name = getname(specialfile);
14931da177e4SLinus Torvalds 	error = PTR_ERR(name);
14941da177e4SLinus Torvalds 	if (IS_ERR(name)) {
14951da177e4SLinus Torvalds 		name = NULL;
14961da177e4SLinus Torvalds 		goto bad_swap_2;
14971da177e4SLinus Torvalds 	}
14981da177e4SLinus Torvalds 	swap_file = filp_open(name, O_RDWR|O_LARGEFILE, 0);
14991da177e4SLinus Torvalds 	error = PTR_ERR(swap_file);
15001da177e4SLinus Torvalds 	if (IS_ERR(swap_file)) {
15011da177e4SLinus Torvalds 		swap_file = NULL;
15021da177e4SLinus Torvalds 		goto bad_swap_2;
15031da177e4SLinus Torvalds 	}
15041da177e4SLinus Torvalds 
15051da177e4SLinus Torvalds 	p->swap_file = swap_file;
15061da177e4SLinus Torvalds 	mapping = swap_file->f_mapping;
15071da177e4SLinus Torvalds 	inode = mapping->host;
15081da177e4SLinus Torvalds 
15091da177e4SLinus Torvalds 	error = -EBUSY;
15101da177e4SLinus Torvalds 	for (i = 0; i < nr_swapfiles; i++) {
15111da177e4SLinus Torvalds 		struct swap_info_struct *q = &swap_info[i];
15121da177e4SLinus Torvalds 
15131da177e4SLinus Torvalds 		if (i == type || !q->swap_file)
15141da177e4SLinus Torvalds 			continue;
15151da177e4SLinus Torvalds 		if (mapping == q->swap_file->f_mapping)
15161da177e4SLinus Torvalds 			goto bad_swap;
15171da177e4SLinus Torvalds 	}
15181da177e4SLinus Torvalds 
15191da177e4SLinus Torvalds 	error = -EINVAL;
15201da177e4SLinus Torvalds 	if (S_ISBLK(inode->i_mode)) {
15211da177e4SLinus Torvalds 		bdev = I_BDEV(inode);
15221da177e4SLinus Torvalds 		error = bd_claim(bdev, sys_swapon);
15231da177e4SLinus Torvalds 		if (error < 0) {
15241da177e4SLinus Torvalds 			bdev = NULL;
1525f7b3a435SRob Landley 			error = -EINVAL;
15261da177e4SLinus Torvalds 			goto bad_swap;
15271da177e4SLinus Torvalds 		}
15281da177e4SLinus Torvalds 		p->old_block_size = block_size(bdev);
15291da177e4SLinus Torvalds 		error = set_blocksize(bdev, PAGE_SIZE);
15301da177e4SLinus Torvalds 		if (error < 0)
15311da177e4SLinus Torvalds 			goto bad_swap;
15321da177e4SLinus Torvalds 		p->bdev = bdev;
15331da177e4SLinus Torvalds 	} else if (S_ISREG(inode->i_mode)) {
15341da177e4SLinus Torvalds 		p->bdev = inode->i_sb->s_bdev;
15351b1dcc1bSJes Sorensen 		mutex_lock(&inode->i_mutex);
15361da177e4SLinus Torvalds 		did_down = 1;
15371da177e4SLinus Torvalds 		if (IS_SWAPFILE(inode)) {
15381da177e4SLinus Torvalds 			error = -EBUSY;
15391da177e4SLinus Torvalds 			goto bad_swap;
15401da177e4SLinus Torvalds 		}
15411da177e4SLinus Torvalds 	} else {
15421da177e4SLinus Torvalds 		goto bad_swap;
15431da177e4SLinus Torvalds 	}
15441da177e4SLinus Torvalds 
15451da177e4SLinus Torvalds 	swapfilesize = i_size_read(inode) >> PAGE_SHIFT;
15461da177e4SLinus Torvalds 
15471da177e4SLinus Torvalds 	/*
15481da177e4SLinus Torvalds 	 * Read the swap header.
15491da177e4SLinus Torvalds 	 */
15501da177e4SLinus Torvalds 	if (!mapping->a_ops->readpage) {
15511da177e4SLinus Torvalds 		error = -EINVAL;
15521da177e4SLinus Torvalds 		goto bad_swap;
15531da177e4SLinus Torvalds 	}
1554090d2b18SPekka Enberg 	page = read_mapping_page(mapping, 0, swap_file);
15551da177e4SLinus Torvalds 	if (IS_ERR(page)) {
15561da177e4SLinus Torvalds 		error = PTR_ERR(page);
15571da177e4SLinus Torvalds 		goto bad_swap;
15581da177e4SLinus Torvalds 	}
15591da177e4SLinus Torvalds 	kmap(page);
15601da177e4SLinus Torvalds 	swap_header = page_address(page);
15611da177e4SLinus Torvalds 
15621da177e4SLinus Torvalds 	if (!memcmp("SWAP-SPACE",swap_header->magic.magic,10))
15631da177e4SLinus Torvalds 		swap_header_version = 1;
15641da177e4SLinus Torvalds 	else if (!memcmp("SWAPSPACE2",swap_header->magic.magic,10))
15651da177e4SLinus Torvalds 		swap_header_version = 2;
15661da177e4SLinus Torvalds 	else {
1567e97a3111SJesper Juhl 		printk(KERN_ERR "Unable to find swap-space signature\n");
15681da177e4SLinus Torvalds 		error = -EINVAL;
15691da177e4SLinus Torvalds 		goto bad_swap;
15701da177e4SLinus Torvalds 	}
15711da177e4SLinus Torvalds 
15721da177e4SLinus Torvalds 	switch (swap_header_version) {
15731da177e4SLinus Torvalds 	case 1:
15741da177e4SLinus Torvalds 		printk(KERN_ERR "version 0 swap is no longer supported. "
15751da177e4SLinus Torvalds 			"Use mkswap -v1 %s\n", name);
15761da177e4SLinus Torvalds 		error = -EINVAL;
15771da177e4SLinus Torvalds 		goto bad_swap;
15781da177e4SLinus Torvalds 	case 2:
1579797df574SChris Dearman 		/* swap partition endianess hack... */
1580797df574SChris Dearman 		if (swab32(swap_header->info.version) == 1) {
1581797df574SChris Dearman 			swab32s(&swap_header->info.version);
1582797df574SChris Dearman 			swab32s(&swap_header->info.last_page);
1583797df574SChris Dearman 			swab32s(&swap_header->info.nr_badpages);
1584797df574SChris Dearman 			for (i = 0; i < swap_header->info.nr_badpages; i++)
1585797df574SChris Dearman 				swab32s(&swap_header->info.badpages[i]);
1586797df574SChris Dearman 		}
15871da177e4SLinus Torvalds 		/* Check the swap header's sub-version and the size of
15881da177e4SLinus Torvalds                    the swap file and bad block lists */
15891da177e4SLinus Torvalds 		if (swap_header->info.version != 1) {
15901da177e4SLinus Torvalds 			printk(KERN_WARNING
15911da177e4SLinus Torvalds 			       "Unable to handle swap header version %d\n",
15921da177e4SLinus Torvalds 			       swap_header->info.version);
15931da177e4SLinus Torvalds 			error = -EINVAL;
15941da177e4SLinus Torvalds 			goto bad_swap;
15951da177e4SLinus Torvalds 		}
15961da177e4SLinus Torvalds 
15971da177e4SLinus Torvalds 		p->lowest_bit  = 1;
159852b7efdbSHugh Dickins 		p->cluster_next = 1;
159952b7efdbSHugh Dickins 
16001da177e4SLinus Torvalds 		/*
16011da177e4SLinus Torvalds 		 * Find out how many pages are allowed for a single swap
16021da177e4SLinus Torvalds 		 * device. There are two limiting factors: 1) the number of
16031da177e4SLinus Torvalds 		 * bits for the swap offset in the swp_entry_t type and
16041da177e4SLinus Torvalds 		 * 2) the number of bits in the a swap pte as defined by
16051da177e4SLinus Torvalds 		 * the different architectures. In order to find the
16061da177e4SLinus Torvalds 		 * largest possible bit mask a swap entry with swap type 0
16071da177e4SLinus Torvalds 		 * and swap offset ~0UL is created, encoded to a swap pte,
16081da177e4SLinus Torvalds 		 * decoded to a swp_entry_t again and finally the swap
16091da177e4SLinus Torvalds 		 * offset is extracted. This will mask all the bits from
16101da177e4SLinus Torvalds 		 * the initial ~0UL mask that can't be encoded in either
16111da177e4SLinus Torvalds 		 * the swp_entry_t or the architecture definition of a
16121da177e4SLinus Torvalds 		 * swap pte.
16131da177e4SLinus Torvalds 		 */
16141da177e4SLinus Torvalds 		maxpages = swp_offset(pte_to_swp_entry(swp_entry_to_pte(swp_entry(0,~0UL)))) - 1;
16151da177e4SLinus Torvalds 		if (maxpages > swap_header->info.last_page)
16161da177e4SLinus Torvalds 			maxpages = swap_header->info.last_page;
16171da177e4SLinus Torvalds 		p->highest_bit = maxpages - 1;
16181da177e4SLinus Torvalds 
16191da177e4SLinus Torvalds 		error = -EINVAL;
1620e2244ec2SHugh Dickins 		if (!maxpages)
1621e2244ec2SHugh Dickins 			goto bad_swap;
16225d1854e1SEric Sandeen 		if (swapfilesize && maxpages > swapfilesize) {
16235d1854e1SEric Sandeen 			printk(KERN_WARNING
16245d1854e1SEric Sandeen 			       "Swap area shorter than signature indicates\n");
16255d1854e1SEric Sandeen 			goto bad_swap;
16265d1854e1SEric Sandeen 		}
1627e2244ec2SHugh Dickins 		if (swap_header->info.nr_badpages && S_ISREG(inode->i_mode))
1628e2244ec2SHugh Dickins 			goto bad_swap;
16291da177e4SLinus Torvalds 		if (swap_header->info.nr_badpages > MAX_SWAP_BADPAGES)
16301da177e4SLinus Torvalds 			goto bad_swap;
16311da177e4SLinus Torvalds 
16321da177e4SLinus Torvalds 		/* OK, set up the swap map and apply the bad block list */
1633*78ecba08SHugh Dickins 		swap_map = vmalloc(maxpages * sizeof(short));
1634*78ecba08SHugh Dickins 		if (!swap_map) {
16351da177e4SLinus Torvalds 			error = -ENOMEM;
16361da177e4SLinus Torvalds 			goto bad_swap;
16371da177e4SLinus Torvalds 		}
16381da177e4SLinus Torvalds 
16391da177e4SLinus Torvalds 		error = 0;
1640*78ecba08SHugh Dickins 		memset(swap_map, 0, maxpages * sizeof(short));
16411da177e4SLinus Torvalds 		for (i = 0; i < swap_header->info.nr_badpages; i++) {
1642cd105df4STobias Klauser 			int page_nr = swap_header->info.badpages[i];
1643cd105df4STobias Klauser 			if (page_nr <= 0 || page_nr >= swap_header->info.last_page)
16441da177e4SLinus Torvalds 				error = -EINVAL;
16451da177e4SLinus Torvalds 			else
1646*78ecba08SHugh Dickins 				swap_map[page_nr] = SWAP_MAP_BAD;
16471da177e4SLinus Torvalds 		}
16481da177e4SLinus Torvalds 		nr_good_pages = swap_header->info.last_page -
16491da177e4SLinus Torvalds 				swap_header->info.nr_badpages -
16501da177e4SLinus Torvalds 				1 /* header page */;
16511da177e4SLinus Torvalds 		if (error)
16521da177e4SLinus Torvalds 			goto bad_swap;
16531da177e4SLinus Torvalds 	}
16541da177e4SLinus Torvalds 
1655e2244ec2SHugh Dickins 	if (nr_good_pages) {
1656*78ecba08SHugh Dickins 		swap_map[0] = SWAP_MAP_BAD;
1657e2244ec2SHugh Dickins 		p->max = maxpages;
1658e2244ec2SHugh Dickins 		p->pages = nr_good_pages;
165953092a74SHugh Dickins 		nr_extents = setup_swap_extents(p, &span);
166053092a74SHugh Dickins 		if (nr_extents < 0) {
166153092a74SHugh Dickins 			error = nr_extents;
1662e2244ec2SHugh Dickins 			goto bad_swap;
166353092a74SHugh Dickins 		}
1664e2244ec2SHugh Dickins 		nr_good_pages = p->pages;
1665e2244ec2SHugh Dickins 	}
16661da177e4SLinus Torvalds 	if (!nr_good_pages) {
16671da177e4SLinus Torvalds 		printk(KERN_WARNING "Empty swap-file\n");
16681da177e4SLinus Torvalds 		error = -EINVAL;
16691da177e4SLinus Torvalds 		goto bad_swap;
16701da177e4SLinus Torvalds 	}
16711da177e4SLinus Torvalds 
1672fc0abb14SIngo Molnar 	mutex_lock(&swapon_mutex);
16735d337b91SHugh Dickins 	spin_lock(&swap_lock);
1674*78ecba08SHugh Dickins 	if (swap_flags & SWAP_FLAG_PREFER)
1675*78ecba08SHugh Dickins 		p->prio =
1676*78ecba08SHugh Dickins 		  (swap_flags & SWAP_FLAG_PRIO_MASK) >> SWAP_FLAG_PRIO_SHIFT;
1677*78ecba08SHugh Dickins 	else
1678*78ecba08SHugh Dickins 		p->prio = --least_priority;
1679*78ecba08SHugh Dickins 	p->swap_map = swap_map;
16801da177e4SLinus Torvalds 	p->flags = SWP_ACTIVE;
16811da177e4SLinus Torvalds 	nr_swap_pages += nr_good_pages;
16821da177e4SLinus Torvalds 	total_swap_pages += nr_good_pages;
168353092a74SHugh Dickins 
16846eb396dcSHugh Dickins 	printk(KERN_INFO "Adding %uk swap on %s.  "
168553092a74SHugh Dickins 			"Priority:%d extents:%d across:%lluk\n",
168653092a74SHugh Dickins 		nr_good_pages<<(PAGE_SHIFT-10), name, p->prio,
168753092a74SHugh Dickins 		nr_extents, (unsigned long long)span<<(PAGE_SHIFT-10));
16881da177e4SLinus Torvalds 
16891da177e4SLinus Torvalds 	/* insert swap space into swap_list: */
16901da177e4SLinus Torvalds 	prev = -1;
16911da177e4SLinus Torvalds 	for (i = swap_list.head; i >= 0; i = swap_info[i].next) {
16921da177e4SLinus Torvalds 		if (p->prio >= swap_info[i].prio) {
16931da177e4SLinus Torvalds 			break;
16941da177e4SLinus Torvalds 		}
16951da177e4SLinus Torvalds 		prev = i;
16961da177e4SLinus Torvalds 	}
16971da177e4SLinus Torvalds 	p->next = i;
16981da177e4SLinus Torvalds 	if (prev < 0) {
16991da177e4SLinus Torvalds 		swap_list.head = swap_list.next = p - swap_info;
17001da177e4SLinus Torvalds 	} else {
17011da177e4SLinus Torvalds 		swap_info[prev].next = p - swap_info;
17021da177e4SLinus Torvalds 	}
17035d337b91SHugh Dickins 	spin_unlock(&swap_lock);
1704fc0abb14SIngo Molnar 	mutex_unlock(&swapon_mutex);
17051da177e4SLinus Torvalds 	error = 0;
17061da177e4SLinus Torvalds 	goto out;
17071da177e4SLinus Torvalds bad_swap:
17081da177e4SLinus Torvalds 	if (bdev) {
17091da177e4SLinus Torvalds 		set_blocksize(bdev, p->old_block_size);
17101da177e4SLinus Torvalds 		bd_release(bdev);
17111da177e4SLinus Torvalds 	}
17124cd3bb10SHugh Dickins 	destroy_swap_extents(p);
17131da177e4SLinus Torvalds bad_swap_2:
17145d337b91SHugh Dickins 	spin_lock(&swap_lock);
17151da177e4SLinus Torvalds 	p->swap_file = NULL;
17161da177e4SLinus Torvalds 	p->flags = 0;
17175d337b91SHugh Dickins 	spin_unlock(&swap_lock);
17181da177e4SLinus Torvalds 	vfree(swap_map);
17191da177e4SLinus Torvalds 	if (swap_file)
17201da177e4SLinus Torvalds 		filp_close(swap_file, NULL);
17211da177e4SLinus Torvalds out:
17221da177e4SLinus Torvalds 	if (page && !IS_ERR(page)) {
17231da177e4SLinus Torvalds 		kunmap(page);
17241da177e4SLinus Torvalds 		page_cache_release(page);
17251da177e4SLinus Torvalds 	}
17261da177e4SLinus Torvalds 	if (name)
17271da177e4SLinus Torvalds 		putname(name);
17281da177e4SLinus Torvalds 	if (did_down) {
17291da177e4SLinus Torvalds 		if (!error)
17301da177e4SLinus Torvalds 			inode->i_flags |= S_SWAPFILE;
17311b1dcc1bSJes Sorensen 		mutex_unlock(&inode->i_mutex);
17321da177e4SLinus Torvalds 	}
17331da177e4SLinus Torvalds 	return error;
17341da177e4SLinus Torvalds }
17351da177e4SLinus Torvalds 
17361da177e4SLinus Torvalds void si_swapinfo(struct sysinfo *val)
17371da177e4SLinus Torvalds {
17381da177e4SLinus Torvalds 	unsigned int i;
17391da177e4SLinus Torvalds 	unsigned long nr_to_be_unused = 0;
17401da177e4SLinus Torvalds 
17415d337b91SHugh Dickins 	spin_lock(&swap_lock);
17421da177e4SLinus Torvalds 	for (i = 0; i < nr_swapfiles; i++) {
17431da177e4SLinus Torvalds 		if (!(swap_info[i].flags & SWP_USED) ||
17441da177e4SLinus Torvalds 		     (swap_info[i].flags & SWP_WRITEOK))
17451da177e4SLinus Torvalds 			continue;
17461da177e4SLinus Torvalds 		nr_to_be_unused += swap_info[i].inuse_pages;
17471da177e4SLinus Torvalds 	}
17481da177e4SLinus Torvalds 	val->freeswap = nr_swap_pages + nr_to_be_unused;
17491da177e4SLinus Torvalds 	val->totalswap = total_swap_pages + nr_to_be_unused;
17505d337b91SHugh Dickins 	spin_unlock(&swap_lock);
17511da177e4SLinus Torvalds }
17521da177e4SLinus Torvalds 
17531da177e4SLinus Torvalds /*
17541da177e4SLinus Torvalds  * Verify that a swap entry is valid and increment its swap map count.
17551da177e4SLinus Torvalds  *
17561da177e4SLinus Torvalds  * Note: if swap_map[] reaches SWAP_MAP_MAX the entries are treated as
17571da177e4SLinus Torvalds  * "permanent", but will be reclaimed by the next swapoff.
17581da177e4SLinus Torvalds  */
17591da177e4SLinus Torvalds int swap_duplicate(swp_entry_t entry)
17601da177e4SLinus Torvalds {
17611da177e4SLinus Torvalds 	struct swap_info_struct * p;
17621da177e4SLinus Torvalds 	unsigned long offset, type;
17631da177e4SLinus Torvalds 	int result = 0;
17641da177e4SLinus Torvalds 
17650697212aSChristoph Lameter 	if (is_migration_entry(entry))
17660697212aSChristoph Lameter 		return 1;
17670697212aSChristoph Lameter 
17681da177e4SLinus Torvalds 	type = swp_type(entry);
17691da177e4SLinus Torvalds 	if (type >= nr_swapfiles)
17701da177e4SLinus Torvalds 		goto bad_file;
17711da177e4SLinus Torvalds 	p = type + swap_info;
17721da177e4SLinus Torvalds 	offset = swp_offset(entry);
17731da177e4SLinus Torvalds 
17745d337b91SHugh Dickins 	spin_lock(&swap_lock);
17751da177e4SLinus Torvalds 	if (offset < p->max && p->swap_map[offset]) {
17761da177e4SLinus Torvalds 		if (p->swap_map[offset] < SWAP_MAP_MAX - 1) {
17771da177e4SLinus Torvalds 			p->swap_map[offset]++;
17781da177e4SLinus Torvalds 			result = 1;
17791da177e4SLinus Torvalds 		} else if (p->swap_map[offset] <= SWAP_MAP_MAX) {
17801da177e4SLinus Torvalds 			if (swap_overflow++ < 5)
17811da177e4SLinus Torvalds 				printk(KERN_WARNING "swap_dup: swap entry overflow\n");
17821da177e4SLinus Torvalds 			p->swap_map[offset] = SWAP_MAP_MAX;
17831da177e4SLinus Torvalds 			result = 1;
17841da177e4SLinus Torvalds 		}
17851da177e4SLinus Torvalds 	}
17865d337b91SHugh Dickins 	spin_unlock(&swap_lock);
17871da177e4SLinus Torvalds out:
17881da177e4SLinus Torvalds 	return result;
17891da177e4SLinus Torvalds 
17901da177e4SLinus Torvalds bad_file:
17911da177e4SLinus Torvalds 	printk(KERN_ERR "swap_dup: %s%08lx\n", Bad_file, entry.val);
17921da177e4SLinus Torvalds 	goto out;
17931da177e4SLinus Torvalds }
17941da177e4SLinus Torvalds 
17951da177e4SLinus Torvalds struct swap_info_struct *
17961da177e4SLinus Torvalds get_swap_info_struct(unsigned type)
17971da177e4SLinus Torvalds {
17981da177e4SLinus Torvalds 	return &swap_info[type];
17991da177e4SLinus Torvalds }
18001da177e4SLinus Torvalds 
18011da177e4SLinus Torvalds /*
18025d337b91SHugh Dickins  * swap_lock prevents swap_map being freed. Don't grab an extra
18031da177e4SLinus Torvalds  * reference on the swaphandle, it doesn't matter if it becomes unused.
18041da177e4SLinus Torvalds  */
18051da177e4SLinus Torvalds int valid_swaphandles(swp_entry_t entry, unsigned long *offset)
18061da177e4SLinus Torvalds {
18078952898bSHugh Dickins 	struct swap_info_struct *si;
18083f9e7949SHugh Dickins 	int our_page_cluster = page_cluster;
18098952898bSHugh Dickins 	pgoff_t target, toff;
18108952898bSHugh Dickins 	pgoff_t base, end;
18118952898bSHugh Dickins 	int nr_pages = 0;
18121da177e4SLinus Torvalds 
18133f9e7949SHugh Dickins 	if (!our_page_cluster)	/* no readahead */
18141da177e4SLinus Torvalds 		return 0;
18158952898bSHugh Dickins 
18168952898bSHugh Dickins 	si = &swap_info[swp_type(entry)];
18178952898bSHugh Dickins 	target = swp_offset(entry);
18188952898bSHugh Dickins 	base = (target >> our_page_cluster) << our_page_cluster;
18198952898bSHugh Dickins 	end = base + (1 << our_page_cluster);
18208952898bSHugh Dickins 	if (!base)		/* first page is swap header */
18218952898bSHugh Dickins 		base++;
18221da177e4SLinus Torvalds 
18235d337b91SHugh Dickins 	spin_lock(&swap_lock);
18248952898bSHugh Dickins 	if (end > si->max)	/* don't go beyond end of map */
18258952898bSHugh Dickins 		end = si->max;
18268952898bSHugh Dickins 
18278952898bSHugh Dickins 	/* Count contiguous allocated slots above our target */
18288952898bSHugh Dickins 	for (toff = target; ++toff < end; nr_pages++) {
18291da177e4SLinus Torvalds 		/* Don't read in free or bad pages */
18308952898bSHugh Dickins 		if (!si->swap_map[toff])
18311da177e4SLinus Torvalds 			break;
18328952898bSHugh Dickins 		if (si->swap_map[toff] == SWAP_MAP_BAD)
18331da177e4SLinus Torvalds 			break;
18348952898bSHugh Dickins 	}
18358952898bSHugh Dickins 	/* Count contiguous allocated slots below our target */
18368952898bSHugh Dickins 	for (toff = target; --toff >= base; nr_pages++) {
18378952898bSHugh Dickins 		/* Don't read in free or bad pages */
18388952898bSHugh Dickins 		if (!si->swap_map[toff])
18398952898bSHugh Dickins 			break;
18408952898bSHugh Dickins 		if (si->swap_map[toff] == SWAP_MAP_BAD)
18418952898bSHugh Dickins 			break;
18428952898bSHugh Dickins 	}
18435d337b91SHugh Dickins 	spin_unlock(&swap_lock);
18448952898bSHugh Dickins 
18458952898bSHugh Dickins 	/*
18468952898bSHugh Dickins 	 * Indicate starting offset, and return number of pages to get:
18478952898bSHugh Dickins 	 * if only 1, say 0, since there's then no readahead to be done.
18488952898bSHugh Dickins 	 */
18498952898bSHugh Dickins 	*offset = ++toff;
18508952898bSHugh Dickins 	return nr_pages? ++nr_pages: 0;
18511da177e4SLinus Torvalds }
1852