blob: fa5f10b9c28b0b6366f742c10c72b91f03374a71 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * linux/mm/swapfile.c
3 *
4 * Copyright (C) 1991, 1992, 1993, 1994 Linus Torvalds
5 * Swap reorganised 29.12.95, Stephen Tweedie
6 */
7
Linus Torvalds1da177e2005-04-16 15:20:36 -07008#include <linux/mm.h>
9#include <linux/hugetlb.h>
10#include <linux/mman.h>
11#include <linux/slab.h>
12#include <linux/kernel_stat.h>
13#include <linux/swap.h>
14#include <linux/vmalloc.h>
15#include <linux/pagemap.h>
16#include <linux/namei.h>
17#include <linux/shm.h>
18#include <linux/blkdev.h>
Hugh Dickins20137a42009-01-06 14:39:54 -080019#include <linux/random.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070020#include <linux/writeback.h>
21#include <linux/proc_fs.h>
22#include <linux/seq_file.h>
23#include <linux/init.h>
24#include <linux/module.h>
25#include <linux/rmap.h>
26#include <linux/security.h>
27#include <linux/backing-dev.h>
Ingo Molnarfc0abb12006-01-18 17:42:33 -080028#include <linux/mutex.h>
Randy.Dunlapc59ede72006-01-11 12:17:46 -080029#include <linux/capability.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070030#include <linux/syscalls.h>
Balbir Singh8a9f3cc2008-02-07 00:13:53 -080031#include <linux/memcontrol.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070032
33#include <asm/pgtable.h>
34#include <asm/tlbflush.h>
35#include <linux/swapops.h>
KAMEZAWA Hiroyuki27a7faa2009-01-07 18:07:58 -080036#include <linux/page_cgroup.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070037
Adrian Bunk7c363b82008-07-25 19:46:24 -070038static DEFINE_SPINLOCK(swap_lock);
39static unsigned int nr_swapfiles;
Hugh Dickinsb9627162009-01-06 14:39:41 -080040long nr_swap_pages;
Linus Torvalds1da177e2005-04-16 15:20:36 -070041long total_swap_pages;
42static int swap_overflow;
Hugh Dickins78ecba02008-07-23 21:28:23 -070043static int least_priority;
Linus Torvalds1da177e2005-04-16 15:20:36 -070044
Linus Torvalds1da177e2005-04-16 15:20:36 -070045static const char Bad_file[] = "Bad swap file entry ";
46static const char Unused_file[] = "Unused swap file entry ";
47static const char Bad_offset[] = "Bad swap offset entry ";
48static const char Unused_offset[] = "Unused swap offset entry ";
49
Adrian Bunk7c363b82008-07-25 19:46:24 -070050static struct swap_list_t swap_list = {-1, -1};
Linus Torvalds1da177e2005-04-16 15:20:36 -070051
Hugh Dickinsefa90a92009-12-14 17:58:41 -080052static struct swap_info_struct *swap_info[MAX_SWAPFILES];
Linus Torvalds1da177e2005-04-16 15:20:36 -070053
Ingo Molnarfc0abb12006-01-18 17:42:33 -080054static DEFINE_MUTEX(swapon_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -070055
KAMEZAWA Hiroyuki355cfa72009-06-16 15:32:53 -070056/* For reference count accounting in swap_map */
57/* enum for swap_map[] handling. internal use only */
58enum {
59 SWAP_MAP = 0, /* ops for reference from swap users */
60 SWAP_CACHE, /* ops for reference from swap cache */
61};
62
63static inline int swap_count(unsigned short ent)
64{
65 return ent & SWAP_COUNT_MASK;
66}
67
68static inline bool swap_has_cache(unsigned short ent)
69{
70 return !!(ent & SWAP_HAS_CACHE);
71}
72
73static inline unsigned short encode_swapmap(int count, bool has_cache)
74{
75 unsigned short ret = count;
76
77 if (has_cache)
78 return SWAP_HAS_CACHE | ret;
79 return ret;
80}
81
Hugh Dickinsefa90a92009-12-14 17:58:41 -080082/* returns 1 if swap entry is freed */
KAMEZAWA Hiroyukic9e44412009-06-16 15:32:54 -070083static int
84__try_to_reclaim_swap(struct swap_info_struct *si, unsigned long offset)
85{
Hugh Dickinsefa90a92009-12-14 17:58:41 -080086 swp_entry_t entry = swp_entry(si->type, offset);
KAMEZAWA Hiroyukic9e44412009-06-16 15:32:54 -070087 struct page *page;
88 int ret = 0;
89
90 page = find_get_page(&swapper_space, entry.val);
91 if (!page)
92 return 0;
93 /*
94 * This function is called from scan_swap_map() and it's called
95 * by vmscan.c at reclaiming pages. So, we hold a lock on a page, here.
96 * We have to use trylock for avoiding deadlock. This is a special
97 * case and you should use try_to_free_swap() with explicit lock_page()
98 * in usual operations.
99 */
100 if (trylock_page(page)) {
101 ret = try_to_free_swap(page);
102 unlock_page(page);
103 }
104 page_cache_release(page);
105 return ret;
106}
KAMEZAWA Hiroyuki355cfa72009-06-16 15:32:53 -0700107
Linus Torvalds1da177e2005-04-16 15:20:36 -0700108/*
109 * We need this because the bdev->unplug_fn can sleep and we cannot
Hugh Dickins5d337b92005-09-03 15:54:41 -0700110 * hold swap_lock while calling the unplug_fn. And swap_lock
Ingo Molnarfc0abb12006-01-18 17:42:33 -0800111 * cannot be turned into a mutex.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112 */
113static DECLARE_RWSEM(swap_unplug_sem);
114
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115void swap_unplug_io_fn(struct backing_dev_info *unused_bdi, struct page *page)
116{
117 swp_entry_t entry;
118
119 down_read(&swap_unplug_sem);
Hugh Dickins4c21e2f2005-10-29 18:16:40 -0700120 entry.val = page_private(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700121 if (PageSwapCache(page)) {
Hugh Dickinsefa90a92009-12-14 17:58:41 -0800122 struct block_device *bdev = swap_info[swp_type(entry)]->bdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700123 struct backing_dev_info *bdi;
124
125 /*
126 * If the page is removed from swapcache from under us (with a
127 * racy try_to_unuse/swapoff) we need an additional reference
Hugh Dickins4c21e2f2005-10-29 18:16:40 -0700128 * count to avoid reading garbage from page_private(page) above.
129 * If the WARN_ON triggers during a swapoff it maybe the race
Linus Torvalds1da177e2005-04-16 15:20:36 -0700130 * condition and it's harmless. However if it triggers without
131 * swapoff it signals a problem.
132 */
133 WARN_ON(page_count(page) <= 1);
134
135 bdi = bdev->bd_inode->i_mapping->backing_dev_info;
McMullan, Jasonba323112005-05-16 21:53:40 -0700136 blk_run_backing_dev(bdi, page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137 }
138 up_read(&swap_unplug_sem);
139}
140
Hugh Dickins6a6ba832009-01-06 14:39:51 -0800141/*
142 * swapon tell device that all the old swap contents can be discarded,
143 * to allow the swap device to optimize its wear-levelling.
144 */
145static int discard_swap(struct swap_info_struct *si)
146{
147 struct swap_extent *se;
Hugh Dickins9625a5f2009-12-14 17:58:42 -0800148 sector_t start_block;
149 sector_t nr_blocks;
Hugh Dickins6a6ba832009-01-06 14:39:51 -0800150 int err = 0;
151
Hugh Dickins9625a5f2009-12-14 17:58:42 -0800152 /* Do not discard the swap header page! */
153 se = &si->first_swap_extent;
154 start_block = (se->start_block + 1) << (PAGE_SHIFT - 9);
155 nr_blocks = ((sector_t)se->nr_pages - 1) << (PAGE_SHIFT - 9);
156 if (nr_blocks) {
157 err = blkdev_issue_discard(si->bdev, start_block,
158 nr_blocks, GFP_KERNEL, DISCARD_FL_BARRIER);
159 if (err)
160 return err;
161 cond_resched();
162 }
Hugh Dickins6a6ba832009-01-06 14:39:51 -0800163
Hugh Dickins9625a5f2009-12-14 17:58:42 -0800164 list_for_each_entry(se, &si->first_swap_extent.list, list) {
165 start_block = se->start_block << (PAGE_SHIFT - 9);
166 nr_blocks = (sector_t)se->nr_pages << (PAGE_SHIFT - 9);
Hugh Dickins6a6ba832009-01-06 14:39:51 -0800167
168 err = blkdev_issue_discard(si->bdev, start_block,
Hugh Dickins9625a5f2009-12-14 17:58:42 -0800169 nr_blocks, GFP_KERNEL, DISCARD_FL_BARRIER);
Hugh Dickins6a6ba832009-01-06 14:39:51 -0800170 if (err)
171 break;
172
173 cond_resched();
174 }
175 return err; /* That will often be -EOPNOTSUPP */
176}
177
Hugh Dickins7992fde2009-01-06 14:39:53 -0800178/*
179 * swap allocation tell device that a cluster of swap can now be discarded,
180 * to allow the swap device to optimize its wear-levelling.
181 */
182static void discard_swap_cluster(struct swap_info_struct *si,
183 pgoff_t start_page, pgoff_t nr_pages)
184{
185 struct swap_extent *se = si->curr_swap_extent;
186 int found_extent = 0;
187
188 while (nr_pages) {
189 struct list_head *lh;
190
191 if (se->start_page <= start_page &&
192 start_page < se->start_page + se->nr_pages) {
193 pgoff_t offset = start_page - se->start_page;
194 sector_t start_block = se->start_block + offset;
Hugh Dickins858a29902009-01-06 14:39:56 -0800195 sector_t nr_blocks = se->nr_pages - offset;
Hugh Dickins7992fde2009-01-06 14:39:53 -0800196
197 if (nr_blocks > nr_pages)
198 nr_blocks = nr_pages;
199 start_page += nr_blocks;
200 nr_pages -= nr_blocks;
201
202 if (!found_extent++)
203 si->curr_swap_extent = se;
204
205 start_block <<= PAGE_SHIFT - 9;
206 nr_blocks <<= PAGE_SHIFT - 9;
207 if (blkdev_issue_discard(si->bdev, start_block,
Hugh Dickins9625a5f2009-12-14 17:58:42 -0800208 nr_blocks, GFP_NOIO, DISCARD_FL_BARRIER))
Hugh Dickins7992fde2009-01-06 14:39:53 -0800209 break;
210 }
211
212 lh = se->list.next;
Hugh Dickins7992fde2009-01-06 14:39:53 -0800213 se = list_entry(lh, struct swap_extent, list);
214 }
215}
216
217static int wait_for_discard(void *word)
218{
219 schedule();
220 return 0;
221}
222
Hugh Dickins048c27f2005-09-03 15:54:40 -0700223#define SWAPFILE_CLUSTER 256
224#define LATENCY_LIMIT 256
225
KAMEZAWA Hiroyuki355cfa72009-06-16 15:32:53 -0700226static inline unsigned long scan_swap_map(struct swap_info_struct *si,
227 int cache)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700228{
Hugh Dickinsebebbbe2009-01-06 14:39:50 -0800229 unsigned long offset;
Hugh Dickinsc60aa172009-01-06 14:39:55 -0800230 unsigned long scan_base;
Hugh Dickins7992fde2009-01-06 14:39:53 -0800231 unsigned long last_in_cluster = 0;
Hugh Dickins048c27f2005-09-03 15:54:40 -0700232 int latency_ration = LATENCY_LIMIT;
Hugh Dickins7992fde2009-01-06 14:39:53 -0800233 int found_free_cluster = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700234
Hugh Dickins886bb7e2009-01-06 14:39:48 -0800235 /*
Hugh Dickins7dfad412005-09-03 15:54:38 -0700236 * We try to cluster swap pages by allocating them sequentially
237 * in swap. Once we've allocated SWAPFILE_CLUSTER pages this
238 * way, however, we resort to first-free allocation, starting
239 * a new cluster. This prevents us from scattering swap pages
240 * all over the entire swap partition, so that we reduce
241 * overall disk seek times between swap pages. -- sct
242 * But we do now try to find an empty cluster. -Andrea
Hugh Dickinsc60aa172009-01-06 14:39:55 -0800243 * And we let swap pages go all over an SSD partition. Hugh
Hugh Dickins7dfad412005-09-03 15:54:38 -0700244 */
245
Hugh Dickins52b7efdb2005-09-03 15:54:39 -0700246 si->flags += SWP_SCANNING;
Hugh Dickinsc60aa172009-01-06 14:39:55 -0800247 scan_base = offset = si->cluster_next;
Hugh Dickinsebebbbe2009-01-06 14:39:50 -0800248
249 if (unlikely(!si->cluster_nr--)) {
250 if (si->pages - si->inuse_pages < SWAPFILE_CLUSTER) {
251 si->cluster_nr = SWAPFILE_CLUSTER - 1;
252 goto checks;
253 }
Hugh Dickins7992fde2009-01-06 14:39:53 -0800254 if (si->flags & SWP_DISCARDABLE) {
255 /*
256 * Start range check on racing allocations, in case
257 * they overlap the cluster we eventually decide on
258 * (we scan without swap_lock to allow preemption).
259 * It's hardly conceivable that cluster_nr could be
260 * wrapped during our scan, but don't depend on it.
261 */
262 if (si->lowest_alloc)
263 goto checks;
264 si->lowest_alloc = si->max;
265 si->highest_alloc = 0;
266 }
Hugh Dickins5d337b92005-09-03 15:54:41 -0700267 spin_unlock(&swap_lock);
Hugh Dickins7dfad412005-09-03 15:54:38 -0700268
Hugh Dickinsc60aa172009-01-06 14:39:55 -0800269 /*
270 * If seek is expensive, start searching for new cluster from
271 * start of partition, to minimize the span of allocated swap.
272 * But if seek is cheap, search from our current position, so
273 * that swap is allocated from all over the partition: if the
274 * Flash Translation Layer only remaps within limited zones,
275 * we don't want to wear out the first zone too quickly.
276 */
277 if (!(si->flags & SWP_SOLIDSTATE))
278 scan_base = offset = si->lowest_bit;
Hugh Dickins7dfad412005-09-03 15:54:38 -0700279 last_in_cluster = offset + SWAPFILE_CLUSTER - 1;
280
281 /* Locate the first empty (unaligned) cluster */
282 for (; last_in_cluster <= si->highest_bit; offset++) {
283 if (si->swap_map[offset])
284 last_in_cluster = offset + SWAPFILE_CLUSTER;
285 else if (offset == last_in_cluster) {
Hugh Dickins5d337b92005-09-03 15:54:41 -0700286 spin_lock(&swap_lock);
Hugh Dickinsebebbbe2009-01-06 14:39:50 -0800287 offset -= SWAPFILE_CLUSTER - 1;
288 si->cluster_next = offset;
289 si->cluster_nr = SWAPFILE_CLUSTER - 1;
Hugh Dickins7992fde2009-01-06 14:39:53 -0800290 found_free_cluster = 1;
Hugh Dickinsebebbbe2009-01-06 14:39:50 -0800291 goto checks;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700292 }
Hugh Dickins048c27f2005-09-03 15:54:40 -0700293 if (unlikely(--latency_ration < 0)) {
294 cond_resched();
295 latency_ration = LATENCY_LIMIT;
296 }
Hugh Dickins7dfad412005-09-03 15:54:38 -0700297 }
Hugh Dickinsebebbbe2009-01-06 14:39:50 -0800298
299 offset = si->lowest_bit;
Hugh Dickinsc60aa172009-01-06 14:39:55 -0800300 last_in_cluster = offset + SWAPFILE_CLUSTER - 1;
301
302 /* Locate the first empty (unaligned) cluster */
303 for (; last_in_cluster < scan_base; offset++) {
304 if (si->swap_map[offset])
305 last_in_cluster = offset + SWAPFILE_CLUSTER;
306 else if (offset == last_in_cluster) {
307 spin_lock(&swap_lock);
308 offset -= SWAPFILE_CLUSTER - 1;
309 si->cluster_next = offset;
310 si->cluster_nr = SWAPFILE_CLUSTER - 1;
311 found_free_cluster = 1;
312 goto checks;
313 }
314 if (unlikely(--latency_ration < 0)) {
315 cond_resched();
316 latency_ration = LATENCY_LIMIT;
317 }
318 }
319
320 offset = scan_base;
Hugh Dickins5d337b92005-09-03 15:54:41 -0700321 spin_lock(&swap_lock);
Hugh Dickinsebebbbe2009-01-06 14:39:50 -0800322 si->cluster_nr = SWAPFILE_CLUSTER - 1;
Hugh Dickins7992fde2009-01-06 14:39:53 -0800323 si->lowest_alloc = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700324 }
Hugh Dickins7dfad412005-09-03 15:54:38 -0700325
Hugh Dickinsebebbbe2009-01-06 14:39:50 -0800326checks:
327 if (!(si->flags & SWP_WRITEOK))
Hugh Dickins52b7efdb2005-09-03 15:54:39 -0700328 goto no_page;
Hugh Dickins7dfad412005-09-03 15:54:38 -0700329 if (!si->highest_bit)
330 goto no_page;
Hugh Dickinsebebbbe2009-01-06 14:39:50 -0800331 if (offset > si->highest_bit)
Hugh Dickinsc60aa172009-01-06 14:39:55 -0800332 scan_base = offset = si->lowest_bit;
KAMEZAWA Hiroyukic9e44412009-06-16 15:32:54 -0700333
334 /* reuse swap entry of cache-only swap if not busy. */
335 if (vm_swap_full() && si->swap_map[offset] == SWAP_HAS_CACHE) {
336 int swap_was_freed;
337 spin_unlock(&swap_lock);
338 swap_was_freed = __try_to_reclaim_swap(si, offset);
339 spin_lock(&swap_lock);
340 /* entry was freed successfully, try to use this again */
341 if (swap_was_freed)
342 goto checks;
343 goto scan; /* check next one */
344 }
345
Hugh Dickinsebebbbe2009-01-06 14:39:50 -0800346 if (si->swap_map[offset])
347 goto scan;
Hugh Dickins7dfad412005-09-03 15:54:38 -0700348
Hugh Dickinsebebbbe2009-01-06 14:39:50 -0800349 if (offset == si->lowest_bit)
350 si->lowest_bit++;
351 if (offset == si->highest_bit)
352 si->highest_bit--;
353 si->inuse_pages++;
354 if (si->inuse_pages == si->pages) {
355 si->lowest_bit = si->max;
356 si->highest_bit = 0;
357 }
KAMEZAWA Hiroyuki355cfa72009-06-16 15:32:53 -0700358 if (cache == SWAP_CACHE) /* at usual swap-out via vmscan.c */
359 si->swap_map[offset] = encode_swapmap(0, true);
360 else /* at suspend */
361 si->swap_map[offset] = encode_swapmap(1, false);
Hugh Dickinsebebbbe2009-01-06 14:39:50 -0800362 si->cluster_next = offset + 1;
363 si->flags -= SWP_SCANNING;
Hugh Dickins7992fde2009-01-06 14:39:53 -0800364
365 if (si->lowest_alloc) {
366 /*
367 * Only set when SWP_DISCARDABLE, and there's a scan
368 * for a free cluster in progress or just completed.
369 */
370 if (found_free_cluster) {
371 /*
372 * To optimize wear-levelling, discard the
373 * old data of the cluster, taking care not to
374 * discard any of its pages that have already
375 * been allocated by racing tasks (offset has
376 * already stepped over any at the beginning).
377 */
378 if (offset < si->highest_alloc &&
379 si->lowest_alloc <= last_in_cluster)
380 last_in_cluster = si->lowest_alloc - 1;
381 si->flags |= SWP_DISCARDING;
382 spin_unlock(&swap_lock);
383
384 if (offset < last_in_cluster)
385 discard_swap_cluster(si, offset,
386 last_in_cluster - offset + 1);
387
388 spin_lock(&swap_lock);
389 si->lowest_alloc = 0;
390 si->flags &= ~SWP_DISCARDING;
391
392 smp_mb(); /* wake_up_bit advises this */
393 wake_up_bit(&si->flags, ilog2(SWP_DISCARDING));
394
395 } else if (si->flags & SWP_DISCARDING) {
396 /*
397 * Delay using pages allocated by racing tasks
398 * until the whole discard has been issued. We
399 * could defer that delay until swap_writepage,
400 * but it's easier to keep this self-contained.
401 */
402 spin_unlock(&swap_lock);
403 wait_on_bit(&si->flags, ilog2(SWP_DISCARDING),
404 wait_for_discard, TASK_UNINTERRUPTIBLE);
405 spin_lock(&swap_lock);
406 } else {
407 /*
408 * Note pages allocated by racing tasks while
409 * scan for a free cluster is in progress, so
410 * that its final discard can exclude them.
411 */
412 if (offset < si->lowest_alloc)
413 si->lowest_alloc = offset;
414 if (offset > si->highest_alloc)
415 si->highest_alloc = offset;
416 }
417 }
Hugh Dickinsebebbbe2009-01-06 14:39:50 -0800418 return offset;
419
420scan:
Hugh Dickins5d337b92005-09-03 15:54:41 -0700421 spin_unlock(&swap_lock);
Hugh Dickins7dfad412005-09-03 15:54:38 -0700422 while (++offset <= si->highest_bit) {
Hugh Dickins52b7efdb2005-09-03 15:54:39 -0700423 if (!si->swap_map[offset]) {
Hugh Dickins5d337b92005-09-03 15:54:41 -0700424 spin_lock(&swap_lock);
Hugh Dickins52b7efdb2005-09-03 15:54:39 -0700425 goto checks;
426 }
KAMEZAWA Hiroyukic9e44412009-06-16 15:32:54 -0700427 if (vm_swap_full() && si->swap_map[offset] == SWAP_HAS_CACHE) {
428 spin_lock(&swap_lock);
429 goto checks;
430 }
Hugh Dickins048c27f2005-09-03 15:54:40 -0700431 if (unlikely(--latency_ration < 0)) {
432 cond_resched();
433 latency_ration = LATENCY_LIMIT;
434 }
Hugh Dickins7dfad412005-09-03 15:54:38 -0700435 }
Hugh Dickinsc60aa172009-01-06 14:39:55 -0800436 offset = si->lowest_bit;
437 while (++offset < scan_base) {
438 if (!si->swap_map[offset]) {
439 spin_lock(&swap_lock);
440 goto checks;
441 }
KAMEZAWA Hiroyukic9e44412009-06-16 15:32:54 -0700442 if (vm_swap_full() && si->swap_map[offset] == SWAP_HAS_CACHE) {
443 spin_lock(&swap_lock);
444 goto checks;
445 }
Hugh Dickinsc60aa172009-01-06 14:39:55 -0800446 if (unlikely(--latency_ration < 0)) {
447 cond_resched();
448 latency_ration = LATENCY_LIMIT;
449 }
450 }
Hugh Dickins5d337b92005-09-03 15:54:41 -0700451 spin_lock(&swap_lock);
Hugh Dickins7dfad412005-09-03 15:54:38 -0700452
453no_page:
Hugh Dickins52b7efdb2005-09-03 15:54:39 -0700454 si->flags -= SWP_SCANNING;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700455 return 0;
456}
457
458swp_entry_t get_swap_page(void)
459{
Hugh Dickinsfb4f88d2005-09-03 15:54:37 -0700460 struct swap_info_struct *si;
461 pgoff_t offset;
462 int type, next;
463 int wrapped = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700464
Hugh Dickins5d337b92005-09-03 15:54:41 -0700465 spin_lock(&swap_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700466 if (nr_swap_pages <= 0)
Hugh Dickinsfb4f88d2005-09-03 15:54:37 -0700467 goto noswap;
468 nr_swap_pages--;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700469
Hugh Dickinsfb4f88d2005-09-03 15:54:37 -0700470 for (type = swap_list.next; type >= 0 && wrapped < 2; type = next) {
Hugh Dickinsefa90a92009-12-14 17:58:41 -0800471 si = swap_info[type];
Hugh Dickinsfb4f88d2005-09-03 15:54:37 -0700472 next = si->next;
473 if (next < 0 ||
Hugh Dickinsefa90a92009-12-14 17:58:41 -0800474 (!wrapped && si->prio != swap_info[next]->prio)) {
Hugh Dickinsfb4f88d2005-09-03 15:54:37 -0700475 next = swap_list.head;
476 wrapped++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700477 }
Hugh Dickinsfb4f88d2005-09-03 15:54:37 -0700478
479 if (!si->highest_bit)
480 continue;
481 if (!(si->flags & SWP_WRITEOK))
482 continue;
483
484 swap_list.next = next;
KAMEZAWA Hiroyuki355cfa72009-06-16 15:32:53 -0700485 /* This is called for allocating swap entry for cache */
486 offset = scan_swap_map(si, SWAP_CACHE);
Hugh Dickins5d337b92005-09-03 15:54:41 -0700487 if (offset) {
488 spin_unlock(&swap_lock);
Hugh Dickinsfb4f88d2005-09-03 15:54:37 -0700489 return swp_entry(type, offset);
Hugh Dickins5d337b92005-09-03 15:54:41 -0700490 }
Hugh Dickinsfb4f88d2005-09-03 15:54:37 -0700491 next = swap_list.next;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700492 }
Hugh Dickinsfb4f88d2005-09-03 15:54:37 -0700493
494 nr_swap_pages++;
495noswap:
Hugh Dickins5d337b92005-09-03 15:54:41 -0700496 spin_unlock(&swap_lock);
Hugh Dickinsfb4f88d2005-09-03 15:54:37 -0700497 return (swp_entry_t) {0};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700498}
499
KAMEZAWA Hiroyuki355cfa72009-06-16 15:32:53 -0700500/* The only caller of this function is now susupend routine */
Rafael J. Wysocki3a291a22006-01-06 00:16:37 -0800501swp_entry_t get_swap_page_of_type(int type)
502{
503 struct swap_info_struct *si;
504 pgoff_t offset;
505
506 spin_lock(&swap_lock);
Hugh Dickinsefa90a92009-12-14 17:58:41 -0800507 si = swap_info[type];
508 if (si && (si->flags & SWP_WRITEOK)) {
Rafael J. Wysocki3a291a22006-01-06 00:16:37 -0800509 nr_swap_pages--;
KAMEZAWA Hiroyuki355cfa72009-06-16 15:32:53 -0700510 /* This is called for allocating swap entry, not cache */
511 offset = scan_swap_map(si, SWAP_MAP);
Rafael J. Wysocki3a291a22006-01-06 00:16:37 -0800512 if (offset) {
513 spin_unlock(&swap_lock);
514 return swp_entry(type, offset);
515 }
516 nr_swap_pages++;
517 }
518 spin_unlock(&swap_lock);
519 return (swp_entry_t) {0};
520}
521
Hugh Dickins73c34b62009-12-14 17:58:43 -0800522static struct swap_info_struct *swap_info_get(swp_entry_t entry)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700523{
Hugh Dickins73c34b62009-12-14 17:58:43 -0800524 struct swap_info_struct *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700525 unsigned long offset, type;
526
527 if (!entry.val)
528 goto out;
529 type = swp_type(entry);
530 if (type >= nr_swapfiles)
531 goto bad_nofile;
Hugh Dickinsefa90a92009-12-14 17:58:41 -0800532 p = swap_info[type];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700533 if (!(p->flags & SWP_USED))
534 goto bad_device;
535 offset = swp_offset(entry);
536 if (offset >= p->max)
537 goto bad_offset;
538 if (!p->swap_map[offset])
539 goto bad_free;
Hugh Dickins5d337b92005-09-03 15:54:41 -0700540 spin_lock(&swap_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700541 return p;
542
543bad_free:
544 printk(KERN_ERR "swap_free: %s%08lx\n", Unused_offset, entry.val);
545 goto out;
546bad_offset:
547 printk(KERN_ERR "swap_free: %s%08lx\n", Bad_offset, entry.val);
548 goto out;
549bad_device:
550 printk(KERN_ERR "swap_free: %s%08lx\n", Unused_file, entry.val);
551 goto out;
552bad_nofile:
553 printk(KERN_ERR "swap_free: %s%08lx\n", Bad_file, entry.val);
554out:
555 return NULL;
Hugh Dickins886bb7e2009-01-06 14:39:48 -0800556}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700557
KAMEZAWA Hiroyuki355cfa72009-06-16 15:32:53 -0700558static int swap_entry_free(struct swap_info_struct *p,
559 swp_entry_t ent, int cache)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700560{
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -0800561 unsigned long offset = swp_offset(ent);
KAMEZAWA Hiroyuki355cfa72009-06-16 15:32:53 -0700562 int count = swap_count(p->swap_map[offset]);
563 bool has_cache;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700564
KAMEZAWA Hiroyuki355cfa72009-06-16 15:32:53 -0700565 has_cache = swap_has_cache(p->swap_map[offset]);
566
567 if (cache == SWAP_MAP) { /* dropping usage count of swap */
568 if (count < SWAP_MAP_MAX) {
569 count--;
570 p->swap_map[offset] = encode_swapmap(count, has_cache);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700571 }
KAMEZAWA Hiroyuki355cfa72009-06-16 15:32:53 -0700572 } else { /* dropping swap cache flag */
573 VM_BUG_ON(!has_cache);
574 p->swap_map[offset] = encode_swapmap(count, false);
575
576 }
577 /* return code. */
578 count = p->swap_map[offset];
579 /* free if no reference */
580 if (!count) {
581 if (offset < p->lowest_bit)
582 p->lowest_bit = offset;
583 if (offset > p->highest_bit)
584 p->highest_bit = offset;
Hugh Dickinsefa90a92009-12-14 17:58:41 -0800585 if (swap_list.next >= 0 &&
586 p->prio > swap_info[swap_list.next]->prio)
587 swap_list.next = p->type;
KAMEZAWA Hiroyuki355cfa72009-06-16 15:32:53 -0700588 nr_swap_pages++;
589 p->inuse_pages--;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700590 }
KAMEZAWA Hiroyuki8a9478c2009-06-17 16:27:17 -0700591 if (!swap_count(count))
592 mem_cgroup_uncharge_swap(ent);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700593 return count;
594}
595
596/*
597 * Caller has made sure that the swapdevice corresponding to entry
598 * is still around or has not been recycled.
599 */
600void swap_free(swp_entry_t entry)
601{
Hugh Dickins73c34b62009-12-14 17:58:43 -0800602 struct swap_info_struct *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700603
604 p = swap_info_get(entry);
605 if (p) {
KAMEZAWA Hiroyuki355cfa72009-06-16 15:32:53 -0700606 swap_entry_free(p, entry, SWAP_MAP);
Hugh Dickins5d337b92005-09-03 15:54:41 -0700607 spin_unlock(&swap_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700608 }
609}
610
611/*
KAMEZAWA Hiroyukicb4b86b2009-06-16 15:32:52 -0700612 * Called after dropping swapcache to decrease refcnt to swap entries.
613 */
614void swapcache_free(swp_entry_t entry, struct page *page)
615{
KAMEZAWA Hiroyuki355cfa72009-06-16 15:32:53 -0700616 struct swap_info_struct *p;
KAMEZAWA Hiroyuki8a9478c2009-06-17 16:27:17 -0700617 int ret;
KAMEZAWA Hiroyuki355cfa72009-06-16 15:32:53 -0700618
KAMEZAWA Hiroyuki355cfa72009-06-16 15:32:53 -0700619 p = swap_info_get(entry);
620 if (p) {
KAMEZAWA Hiroyuki8a9478c2009-06-17 16:27:17 -0700621 ret = swap_entry_free(p, entry, SWAP_CACHE);
622 if (page) {
623 bool swapout;
624 if (ret)
625 swapout = true; /* the end of swap out */
626 else
627 swapout = false; /* no more swap users! */
628 mem_cgroup_uncharge_swapcache(page, entry, swapout);
629 }
KAMEZAWA Hiroyuki355cfa72009-06-16 15:32:53 -0700630 spin_unlock(&swap_lock);
631 }
KAMEZAWA Hiroyukicb4b86b2009-06-16 15:32:52 -0700632}
633
634/*
Hugh Dickinsc475a8a2005-06-21 17:15:12 -0700635 * How many references to page are currently swapped out?
Linus Torvalds1da177e2005-04-16 15:20:36 -0700636 */
Hugh Dickinsc475a8a2005-06-21 17:15:12 -0700637static inline int page_swapcount(struct page *page)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700638{
Hugh Dickinsc475a8a2005-06-21 17:15:12 -0700639 int count = 0;
640 struct swap_info_struct *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700641 swp_entry_t entry;
642
Hugh Dickins4c21e2f2005-10-29 18:16:40 -0700643 entry.val = page_private(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700644 p = swap_info_get(entry);
645 if (p) {
KAMEZAWA Hiroyuki355cfa72009-06-16 15:32:53 -0700646 count = swap_count(p->swap_map[swp_offset(entry)]);
Hugh Dickins5d337b92005-09-03 15:54:41 -0700647 spin_unlock(&swap_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700648 }
Hugh Dickinsc475a8a2005-06-21 17:15:12 -0700649 return count;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700650}
651
652/*
Hugh Dickins7b1fe592009-01-06 14:39:34 -0800653 * We can write to an anon page without COW if there are no other references
654 * to it. And as a side-effect, free up its swap: because the old content
655 * on disk will never be read, and seeking back there to write new content
656 * later would only waste time away from clustering.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700657 */
Hugh Dickins7b1fe592009-01-06 14:39:34 -0800658int reuse_swap_page(struct page *page)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700659{
Hugh Dickinsc475a8a2005-06-21 17:15:12 -0700660 int count;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700661
Hugh Dickins51726b12009-01-06 14:39:25 -0800662 VM_BUG_ON(!PageLocked(page));
Hugh Dickinsc475a8a2005-06-21 17:15:12 -0700663 count = page_mapcount(page);
Hugh Dickins7b1fe592009-01-06 14:39:34 -0800664 if (count <= 1 && PageSwapCache(page)) {
Hugh Dickinsc475a8a2005-06-21 17:15:12 -0700665 count += page_swapcount(page);
Hugh Dickins7b1fe592009-01-06 14:39:34 -0800666 if (count == 1 && !PageWriteback(page)) {
667 delete_from_swap_cache(page);
668 SetPageDirty(page);
669 }
670 }
Hugh Dickinsc475a8a2005-06-21 17:15:12 -0700671 return count == 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700672}
673
674/*
Hugh Dickinsa2c43ee2009-01-06 14:39:36 -0800675 * If swap is getting full, or if there are no more mappings of this page,
676 * then try_to_free_swap is called to free its swap space.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700677 */
Hugh Dickinsa2c43ee2009-01-06 14:39:36 -0800678int try_to_free_swap(struct page *page)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700679{
Hugh Dickins51726b12009-01-06 14:39:25 -0800680 VM_BUG_ON(!PageLocked(page));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700681
682 if (!PageSwapCache(page))
683 return 0;
684 if (PageWriteback(page))
685 return 0;
Hugh Dickinsa2c43ee2009-01-06 14:39:36 -0800686 if (page_swapcount(page))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700687 return 0;
688
Hugh Dickinsa2c43ee2009-01-06 14:39:36 -0800689 delete_from_swap_cache(page);
690 SetPageDirty(page);
691 return 1;
Rik van Riel68a223942008-10-18 20:26:23 -0700692}
693
694/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700695 * Free the swap entry like above, but also try to
696 * free the page cache entry if it is the last user.
697 */
Hugh Dickins2509ef22009-01-06 14:40:10 -0800698int free_swap_and_cache(swp_entry_t entry)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700699{
Hugh Dickins2509ef22009-01-06 14:40:10 -0800700 struct swap_info_struct *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700701 struct page *page = NULL;
702
Andi Kleena7420aa2009-09-16 11:50:05 +0200703 if (non_swap_entry(entry))
Hugh Dickins2509ef22009-01-06 14:40:10 -0800704 return 1;
Christoph Lameter06972122006-06-23 02:03:35 -0700705
Linus Torvalds1da177e2005-04-16 15:20:36 -0700706 p = swap_info_get(entry);
707 if (p) {
KAMEZAWA Hiroyuki355cfa72009-06-16 15:32:53 -0700708 if (swap_entry_free(p, entry, SWAP_MAP) == SWAP_HAS_CACHE) {
Nick Piggin93fac702006-03-31 02:29:56 -0800709 page = find_get_page(&swapper_space, entry.val);
Nick Piggin8413ac92008-10-18 20:26:59 -0700710 if (page && !trylock_page(page)) {
Nick Piggin93fac702006-03-31 02:29:56 -0800711 page_cache_release(page);
712 page = NULL;
713 }
714 }
Hugh Dickins5d337b92005-09-03 15:54:41 -0700715 spin_unlock(&swap_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700716 }
717 if (page) {
Hugh Dickinsa2c43ee2009-01-06 14:39:36 -0800718 /*
719 * Not mapped elsewhere, or swap space full? Free it!
720 * Also recheck PageSwapCache now page is locked (above).
721 */
Nick Piggin93fac702006-03-31 02:29:56 -0800722 if (PageSwapCache(page) && !PageWriteback(page) &&
Hugh Dickinsa2c43ee2009-01-06 14:39:36 -0800723 (!page_mapped(page) || vm_swap_full())) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700724 delete_from_swap_cache(page);
725 SetPageDirty(page);
726 }
727 unlock_page(page);
728 page_cache_release(page);
729 }
Hugh Dickins2509ef22009-01-06 14:40:10 -0800730 return p != NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700731}
732
Rafael J. Wysockib0cb1a12007-07-29 23:24:36 +0200733#ifdef CONFIG_HIBERNATION
Rafael J. Wysockif577eb32006-03-23 02:59:59 -0800734/*
Rafael J. Wysocki915bae92006-12-06 20:34:07 -0800735 * Find the swap type that corresponds to given device (if any).
Rafael J. Wysockif577eb32006-03-23 02:59:59 -0800736 *
Rafael J. Wysocki915bae92006-12-06 20:34:07 -0800737 * @offset - number of the PAGE_SIZE-sized block of the device, starting
738 * from 0, in which the swap header is expected to be located.
739 *
740 * This is needed for the suspend to disk (aka swsusp).
Rafael J. Wysockif577eb32006-03-23 02:59:59 -0800741 */
Rafael J. Wysocki7bf23682007-01-05 16:36:28 -0800742int swap_type_of(dev_t device, sector_t offset, struct block_device **bdev_p)
Rafael J. Wysockif577eb32006-03-23 02:59:59 -0800743{
Rafael J. Wysocki915bae92006-12-06 20:34:07 -0800744 struct block_device *bdev = NULL;
Hugh Dickinsefa90a92009-12-14 17:58:41 -0800745 int type;
Rafael J. Wysockif577eb32006-03-23 02:59:59 -0800746
Rafael J. Wysocki915bae92006-12-06 20:34:07 -0800747 if (device)
748 bdev = bdget(device);
749
Rafael J. Wysockif577eb32006-03-23 02:59:59 -0800750 spin_lock(&swap_lock);
Hugh Dickinsefa90a92009-12-14 17:58:41 -0800751 for (type = 0; type < nr_swapfiles; type++) {
752 struct swap_info_struct *sis = swap_info[type];
Rafael J. Wysockif577eb32006-03-23 02:59:59 -0800753
Rafael J. Wysocki915bae92006-12-06 20:34:07 -0800754 if (!(sis->flags & SWP_WRITEOK))
Rafael J. Wysockif577eb32006-03-23 02:59:59 -0800755 continue;
Rafael J. Wysockib6b5bce2006-08-27 01:23:25 -0700756
Rafael J. Wysocki915bae92006-12-06 20:34:07 -0800757 if (!bdev) {
Rafael J. Wysocki7bf23682007-01-05 16:36:28 -0800758 if (bdev_p)
Alan Jenkinsdddac6a2009-07-29 21:07:55 +0200759 *bdev_p = bdgrab(sis->bdev);
Rafael J. Wysocki7bf23682007-01-05 16:36:28 -0800760
Rafael J. Wysocki6e1819d2006-03-23 03:00:03 -0800761 spin_unlock(&swap_lock);
Hugh Dickinsefa90a92009-12-14 17:58:41 -0800762 return type;
Rafael J. Wysocki6e1819d2006-03-23 03:00:03 -0800763 }
Rafael J. Wysocki915bae92006-12-06 20:34:07 -0800764 if (bdev == sis->bdev) {
Hugh Dickins9625a5f2009-12-14 17:58:42 -0800765 struct swap_extent *se = &sis->first_swap_extent;
Rafael J. Wysocki915bae92006-12-06 20:34:07 -0800766
Rafael J. Wysocki915bae92006-12-06 20:34:07 -0800767 if (se->start_block == offset) {
Rafael J. Wysocki7bf23682007-01-05 16:36:28 -0800768 if (bdev_p)
Alan Jenkinsdddac6a2009-07-29 21:07:55 +0200769 *bdev_p = bdgrab(sis->bdev);
Rafael J. Wysocki7bf23682007-01-05 16:36:28 -0800770
Rafael J. Wysocki915bae92006-12-06 20:34:07 -0800771 spin_unlock(&swap_lock);
772 bdput(bdev);
Hugh Dickinsefa90a92009-12-14 17:58:41 -0800773 return type;
Rafael J. Wysocki915bae92006-12-06 20:34:07 -0800774 }
Rafael J. Wysockif577eb32006-03-23 02:59:59 -0800775 }
776 }
777 spin_unlock(&swap_lock);
Rafael J. Wysocki915bae92006-12-06 20:34:07 -0800778 if (bdev)
779 bdput(bdev);
780
Rafael J. Wysockif577eb32006-03-23 02:59:59 -0800781 return -ENODEV;
782}
783
784/*
Hugh Dickins73c34b62009-12-14 17:58:43 -0800785 * Get the (PAGE_SIZE) block corresponding to given offset on the swapdev
786 * corresponding to given index in swap_info (swap type).
787 */
788sector_t swapdev_block(int type, pgoff_t offset)
789{
790 struct block_device *bdev;
791
792 if ((unsigned int)type >= nr_swapfiles)
793 return 0;
794 if (!(swap_info[type]->flags & SWP_WRITEOK))
795 return 0;
796 return map_swap_page(swp_entry(type, offset), &bdev);
797}
798
799/*
Rafael J. Wysockif577eb32006-03-23 02:59:59 -0800800 * Return either the total number of swap pages of given type, or the number
801 * of free pages of that type (depending on @free)
802 *
803 * This is needed for software suspend
804 */
805unsigned int count_swap_pages(int type, int free)
806{
807 unsigned int n = 0;
808
Hugh Dickinsefa90a92009-12-14 17:58:41 -0800809 spin_lock(&swap_lock);
810 if ((unsigned int)type < nr_swapfiles) {
811 struct swap_info_struct *sis = swap_info[type];
812
813 if (sis->flags & SWP_WRITEOK) {
814 n = sis->pages;
Rafael J. Wysockif577eb32006-03-23 02:59:59 -0800815 if (free)
Hugh Dickinsefa90a92009-12-14 17:58:41 -0800816 n -= sis->inuse_pages;
Rafael J. Wysockif577eb32006-03-23 02:59:59 -0800817 }
Rafael J. Wysockif577eb32006-03-23 02:59:59 -0800818 }
Hugh Dickinsefa90a92009-12-14 17:58:41 -0800819 spin_unlock(&swap_lock);
Rafael J. Wysockif577eb32006-03-23 02:59:59 -0800820 return n;
821}
Hugh Dickins73c34b62009-12-14 17:58:43 -0800822#endif /* CONFIG_HIBERNATION */
Rafael J. Wysockif577eb32006-03-23 02:59:59 -0800823
Linus Torvalds1da177e2005-04-16 15:20:36 -0700824/*
Hugh Dickins72866f62005-10-29 18:15:55 -0700825 * No need to decide whether this PTE shares the swap entry with others,
826 * just let do_wp_page work it out if a write is requested later - to
827 * force COW, vm_page_prot omits write permission from any private vma.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700828 */
Hugh Dickins044d66c2008-02-07 00:14:04 -0800829static int unuse_pte(struct vm_area_struct *vma, pmd_t *pmd,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700830 unsigned long addr, swp_entry_t entry, struct page *page)
831{
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -0800832 struct mem_cgroup *ptr = NULL;
Hugh Dickins044d66c2008-02-07 00:14:04 -0800833 spinlock_t *ptl;
834 pte_t *pte;
835 int ret = 1;
836
KAMEZAWA Hiroyuki85d9fc82009-01-29 14:25:13 -0800837 if (mem_cgroup_try_charge_swapin(vma->vm_mm, page, GFP_KERNEL, &ptr)) {
Hugh Dickins044d66c2008-02-07 00:14:04 -0800838 ret = -ENOMEM;
KAMEZAWA Hiroyuki85d9fc82009-01-29 14:25:13 -0800839 goto out_nolock;
840 }
Hugh Dickins044d66c2008-02-07 00:14:04 -0800841
842 pte = pte_offset_map_lock(vma->vm_mm, pmd, addr, &ptl);
843 if (unlikely(!pte_same(*pte, swp_entry_to_pte(entry)))) {
844 if (ret > 0)
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -0800845 mem_cgroup_cancel_charge_swapin(ptr);
Hugh Dickins044d66c2008-02-07 00:14:04 -0800846 ret = 0;
847 goto out;
848 }
Balbir Singh8a9f3cc2008-02-07 00:13:53 -0800849
Hugh Dickins42946212005-10-29 18:16:05 -0700850 inc_mm_counter(vma->vm_mm, anon_rss);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700851 get_page(page);
852 set_pte_at(vma->vm_mm, addr, pte,
853 pte_mkold(mk_pte(page, vma->vm_page_prot)));
854 page_add_anon_rmap(page, vma, addr);
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -0800855 mem_cgroup_commit_charge_swapin(page, ptr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700856 swap_free(entry);
857 /*
858 * Move the page to the active list so it is not
859 * immediately swapped out again after swapon.
860 */
861 activate_page(page);
Hugh Dickins044d66c2008-02-07 00:14:04 -0800862out:
863 pte_unmap_unlock(pte, ptl);
KAMEZAWA Hiroyuki85d9fc82009-01-29 14:25:13 -0800864out_nolock:
Hugh Dickins044d66c2008-02-07 00:14:04 -0800865 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700866}
867
868static int unuse_pte_range(struct vm_area_struct *vma, pmd_t *pmd,
869 unsigned long addr, unsigned long end,
870 swp_entry_t entry, struct page *page)
871{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700872 pte_t swp_pte = swp_entry_to_pte(entry);
Hugh Dickins705e87c2005-10-29 18:16:27 -0700873 pte_t *pte;
Balbir Singh8a9f3cc2008-02-07 00:13:53 -0800874 int ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700875
Hugh Dickins044d66c2008-02-07 00:14:04 -0800876 /*
877 * We don't actually need pte lock while scanning for swp_pte: since
878 * we hold page lock and mmap_sem, swp_pte cannot be inserted into the
879 * page table while we're scanning; though it could get zapped, and on
880 * some architectures (e.g. x86_32 with PAE) we might catch a glimpse
881 * of unmatched parts which look like swp_pte, so unuse_pte must
882 * recheck under pte lock. Scanning without pte lock lets it be
883 * preemptible whenever CONFIG_PREEMPT but not CONFIG_HIGHPTE.
884 */
885 pte = pte_offset_map(pmd, addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700886 do {
887 /*
888 * swapoff spends a _lot_ of time in this loop!
889 * Test inline before going to call unuse_pte.
890 */
891 if (unlikely(pte_same(*pte, swp_pte))) {
Hugh Dickins044d66c2008-02-07 00:14:04 -0800892 pte_unmap(pte);
893 ret = unuse_pte(vma, pmd, addr, entry, page);
894 if (ret)
895 goto out;
896 pte = pte_offset_map(pmd, addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700897 }
898 } while (pte++, addr += PAGE_SIZE, addr != end);
Hugh Dickins044d66c2008-02-07 00:14:04 -0800899 pte_unmap(pte - 1);
900out:
Balbir Singh8a9f3cc2008-02-07 00:13:53 -0800901 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700902}
903
904static inline int unuse_pmd_range(struct vm_area_struct *vma, pud_t *pud,
905 unsigned long addr, unsigned long end,
906 swp_entry_t entry, struct page *page)
907{
908 pmd_t *pmd;
909 unsigned long next;
Balbir Singh8a9f3cc2008-02-07 00:13:53 -0800910 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700911
912 pmd = pmd_offset(pud, addr);
913 do {
914 next = pmd_addr_end(addr, end);
915 if (pmd_none_or_clear_bad(pmd))
916 continue;
Balbir Singh8a9f3cc2008-02-07 00:13:53 -0800917 ret = unuse_pte_range(vma, pmd, addr, next, entry, page);
918 if (ret)
919 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700920 } while (pmd++, addr = next, addr != end);
921 return 0;
922}
923
924static inline int unuse_pud_range(struct vm_area_struct *vma, pgd_t *pgd,
925 unsigned long addr, unsigned long end,
926 swp_entry_t entry, struct page *page)
927{
928 pud_t *pud;
929 unsigned long next;
Balbir Singh8a9f3cc2008-02-07 00:13:53 -0800930 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700931
932 pud = pud_offset(pgd, addr);
933 do {
934 next = pud_addr_end(addr, end);
935 if (pud_none_or_clear_bad(pud))
936 continue;
Balbir Singh8a9f3cc2008-02-07 00:13:53 -0800937 ret = unuse_pmd_range(vma, pud, addr, next, entry, page);
938 if (ret)
939 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700940 } while (pud++, addr = next, addr != end);
941 return 0;
942}
943
944static int unuse_vma(struct vm_area_struct *vma,
945 swp_entry_t entry, struct page *page)
946{
947 pgd_t *pgd;
948 unsigned long addr, end, next;
Balbir Singh8a9f3cc2008-02-07 00:13:53 -0800949 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700950
951 if (page->mapping) {
952 addr = page_address_in_vma(page, vma);
953 if (addr == -EFAULT)
954 return 0;
955 else
956 end = addr + PAGE_SIZE;
957 } else {
958 addr = vma->vm_start;
959 end = vma->vm_end;
960 }
961
962 pgd = pgd_offset(vma->vm_mm, addr);
963 do {
964 next = pgd_addr_end(addr, end);
965 if (pgd_none_or_clear_bad(pgd))
966 continue;
Balbir Singh8a9f3cc2008-02-07 00:13:53 -0800967 ret = unuse_pud_range(vma, pgd, addr, next, entry, page);
968 if (ret)
969 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700970 } while (pgd++, addr = next, addr != end);
971 return 0;
972}
973
974static int unuse_mm(struct mm_struct *mm,
975 swp_entry_t entry, struct page *page)
976{
977 struct vm_area_struct *vma;
Balbir Singh8a9f3cc2008-02-07 00:13:53 -0800978 int ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700979
980 if (!down_read_trylock(&mm->mmap_sem)) {
981 /*
Fernando Luis Vazquez Cao7d034312008-07-29 22:33:41 -0700982 * Activate page so shrink_inactive_list is unlikely to unmap
983 * its ptes while lock is dropped, so swapoff can make progress.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700984 */
Hugh Dickinsc475a8a2005-06-21 17:15:12 -0700985 activate_page(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700986 unlock_page(page);
987 down_read(&mm->mmap_sem);
988 lock_page(page);
989 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700990 for (vma = mm->mmap; vma; vma = vma->vm_next) {
Balbir Singh8a9f3cc2008-02-07 00:13:53 -0800991 if (vma->anon_vma && (ret = unuse_vma(vma, entry, page)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700992 break;
993 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700994 up_read(&mm->mmap_sem);
Balbir Singh8a9f3cc2008-02-07 00:13:53 -0800995 return (ret < 0)? ret: 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700996}
997
998/*
999 * Scan swap_map from current position to next entry still in use.
1000 * Recycle to start on reaching the end, returning 0 when empty.
1001 */
Hugh Dickins6eb396d2005-09-03 15:54:35 -07001002static unsigned int find_next_to_unuse(struct swap_info_struct *si,
1003 unsigned int prev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001004{
Hugh Dickins6eb396d2005-09-03 15:54:35 -07001005 unsigned int max = si->max;
1006 unsigned int i = prev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001007 int count;
1008
1009 /*
Hugh Dickins5d337b92005-09-03 15:54:41 -07001010 * No need for swap_lock here: we're just looking
Linus Torvalds1da177e2005-04-16 15:20:36 -07001011 * for whether an entry is in use, not modifying it; false
1012 * hits are okay, and sys_swapoff() has already prevented new
Hugh Dickins5d337b92005-09-03 15:54:41 -07001013 * allocations from this area (while holding swap_lock).
Linus Torvalds1da177e2005-04-16 15:20:36 -07001014 */
1015 for (;;) {
1016 if (++i >= max) {
1017 if (!prev) {
1018 i = 0;
1019 break;
1020 }
1021 /*
1022 * No entries in use at top of swap_map,
1023 * loop back to start and recheck there.
1024 */
1025 max = prev + 1;
1026 prev = 0;
1027 i = 1;
1028 }
1029 count = si->swap_map[i];
KAMEZAWA Hiroyuki355cfa72009-06-16 15:32:53 -07001030 if (count && swap_count(count) != SWAP_MAP_BAD)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001031 break;
1032 }
1033 return i;
1034}
1035
1036/*
1037 * We completely avoid races by reading each swap page in advance,
1038 * and then search for the process using it. All the necessary
1039 * page table adjustments can then be made atomically.
1040 */
1041static int try_to_unuse(unsigned int type)
1042{
Hugh Dickinsefa90a92009-12-14 17:58:41 -08001043 struct swap_info_struct *si = swap_info[type];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001044 struct mm_struct *start_mm;
1045 unsigned short *swap_map;
1046 unsigned short swcount;
1047 struct page *page;
1048 swp_entry_t entry;
Hugh Dickins6eb396d2005-09-03 15:54:35 -07001049 unsigned int i = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001050 int retval = 0;
1051 int reset_overflow = 0;
1052 int shmem;
1053
1054 /*
1055 * When searching mms for an entry, a good strategy is to
1056 * start at the first mm we freed the previous entry from
1057 * (though actually we don't notice whether we or coincidence
1058 * freed the entry). Initialize this start_mm with a hold.
1059 *
1060 * A simpler strategy would be to start at the last mm we
1061 * freed the previous entry from; but that would take less
1062 * advantage of mmlist ordering, which clusters forked mms
1063 * together, child after parent. If we race with dup_mmap(), we
1064 * prefer to resolve parent before child, lest we miss entries
1065 * duplicated after we scanned child: using last mm would invert
1066 * that. Though it's only a serious concern when an overflowed
1067 * swap count is reset from SWAP_MAP_MAX, preventing a rescan.
1068 */
1069 start_mm = &init_mm;
1070 atomic_inc(&init_mm.mm_users);
1071
1072 /*
1073 * Keep on scanning until all entries have gone. Usually,
1074 * one pass through swap_map is enough, but not necessarily:
1075 * there are races when an instance of an entry might be missed.
1076 */
1077 while ((i = find_next_to_unuse(si, i)) != 0) {
1078 if (signal_pending(current)) {
1079 retval = -EINTR;
1080 break;
1081 }
1082
Hugh Dickins886bb7e2009-01-06 14:39:48 -08001083 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001084 * Get a page for the entry, using the existing swap
1085 * cache page if there is one. Otherwise, get a clean
Hugh Dickins886bb7e2009-01-06 14:39:48 -08001086 * page and read the swap into it.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001087 */
1088 swap_map = &si->swap_map[i];
1089 entry = swp_entry(type, i);
Hugh Dickins02098fe2008-02-04 22:28:42 -08001090 page = read_swap_cache_async(entry,
1091 GFP_HIGHUSER_MOVABLE, NULL, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001092 if (!page) {
1093 /*
1094 * Either swap_duplicate() failed because entry
1095 * has been freed independently, and will not be
1096 * reused since sys_swapoff() already disabled
1097 * allocation from here, or alloc_page() failed.
1098 */
1099 if (!*swap_map)
1100 continue;
1101 retval = -ENOMEM;
1102 break;
1103 }
1104
1105 /*
1106 * Don't hold on to start_mm if it looks like exiting.
1107 */
1108 if (atomic_read(&start_mm->mm_users) == 1) {
1109 mmput(start_mm);
1110 start_mm = &init_mm;
1111 atomic_inc(&init_mm.mm_users);
1112 }
1113
1114 /*
1115 * Wait for and lock page. When do_swap_page races with
1116 * try_to_unuse, do_swap_page can handle the fault much
1117 * faster than try_to_unuse can locate the entry. This
1118 * apparently redundant "wait_on_page_locked" lets try_to_unuse
1119 * defer to do_swap_page in such a case - in some tests,
1120 * do_swap_page and try_to_unuse repeatedly compete.
1121 */
1122 wait_on_page_locked(page);
1123 wait_on_page_writeback(page);
1124 lock_page(page);
1125 wait_on_page_writeback(page);
1126
1127 /*
1128 * Remove all references to entry.
1129 * Whenever we reach init_mm, there's no address space
1130 * to search, but use it as a reminder to search shmem.
1131 */
1132 shmem = 0;
1133 swcount = *swap_map;
KAMEZAWA Hiroyuki355cfa72009-06-16 15:32:53 -07001134 if (swap_count(swcount)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001135 if (start_mm == &init_mm)
1136 shmem = shmem_unuse(entry, page);
1137 else
1138 retval = unuse_mm(start_mm, entry, page);
1139 }
KAMEZAWA Hiroyuki355cfa72009-06-16 15:32:53 -07001140 if (swap_count(*swap_map)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001141 int set_start_mm = (*swap_map >= swcount);
1142 struct list_head *p = &start_mm->mmlist;
1143 struct mm_struct *new_start_mm = start_mm;
1144 struct mm_struct *prev_mm = start_mm;
1145 struct mm_struct *mm;
1146
1147 atomic_inc(&new_start_mm->mm_users);
1148 atomic_inc(&prev_mm->mm_users);
1149 spin_lock(&mmlist_lock);
KAMEZAWA Hiroyuki355cfa72009-06-16 15:32:53 -07001150 while (swap_count(*swap_map) && !retval && !shmem &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07001151 (p = p->next) != &start_mm->mmlist) {
1152 mm = list_entry(p, struct mm_struct, mmlist);
Hugh Dickins70af7c52006-06-23 02:03:44 -07001153 if (!atomic_inc_not_zero(&mm->mm_users))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001154 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001155 spin_unlock(&mmlist_lock);
1156 mmput(prev_mm);
1157 prev_mm = mm;
1158
1159 cond_resched();
1160
1161 swcount = *swap_map;
KAMEZAWA Hiroyuki355cfa72009-06-16 15:32:53 -07001162 if (!swap_count(swcount)) /* any usage ? */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001163 ;
1164 else if (mm == &init_mm) {
1165 set_start_mm = 1;
1166 shmem = shmem_unuse(entry, page);
1167 } else
1168 retval = unuse_mm(mm, entry, page);
KAMEZAWA Hiroyuki355cfa72009-06-16 15:32:53 -07001169
Bo Liu32c5fc12009-11-02 16:50:33 +00001170 if (set_start_mm && *swap_map < swcount) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001171 mmput(new_start_mm);
1172 atomic_inc(&mm->mm_users);
1173 new_start_mm = mm;
1174 set_start_mm = 0;
1175 }
1176 spin_lock(&mmlist_lock);
1177 }
1178 spin_unlock(&mmlist_lock);
1179 mmput(prev_mm);
1180 mmput(start_mm);
1181 start_mm = new_start_mm;
1182 }
Hugh Dickins2e0e26c2008-02-04 22:28:53 -08001183 if (shmem) {
1184 /* page has already been unlocked and released */
1185 if (shmem > 0)
1186 continue;
1187 retval = shmem;
1188 break;
1189 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001190 if (retval) {
1191 unlock_page(page);
1192 page_cache_release(page);
1193 break;
1194 }
1195
1196 /*
KAMEZAWA Hiroyuki355cfa72009-06-16 15:32:53 -07001197 * How could swap count reach 0x7ffe ?
1198 * There's no way to repeat a swap page within an mm
1199 * (except in shmem, where it's the shared object which takes
1200 * the reference count)?
1201 * We believe SWAP_MAP_MAX cannot occur.(if occur, unsigned
1202 * short is too small....)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001203 * If that's wrong, then we should worry more about
1204 * exit_mmap() and do_munmap() cases described above:
1205 * we might be resetting SWAP_MAP_MAX too early here.
1206 * We know "Undead"s can happen, they're okay, so don't
1207 * report them; but do report if we reset SWAP_MAP_MAX.
1208 */
KAMEZAWA Hiroyuki355cfa72009-06-16 15:32:53 -07001209 /* We might release the lock_page() in unuse_mm(). */
1210 if (!PageSwapCache(page) || page_private(page) != entry.val)
1211 goto retry;
1212
1213 if (swap_count(*swap_map) == SWAP_MAP_MAX) {
Hugh Dickins5d337b92005-09-03 15:54:41 -07001214 spin_lock(&swap_lock);
KAMEZAWA Hiroyuki355cfa72009-06-16 15:32:53 -07001215 *swap_map = encode_swapmap(0, true);
Hugh Dickins5d337b92005-09-03 15:54:41 -07001216 spin_unlock(&swap_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001217 reset_overflow = 1;
1218 }
1219
1220 /*
1221 * If a reference remains (rare), we would like to leave
1222 * the page in the swap cache; but try_to_unmap could
1223 * then re-duplicate the entry once we drop page lock,
1224 * so we might loop indefinitely; also, that page could
1225 * not be swapped out to other storage meanwhile. So:
1226 * delete from cache even if there's another reference,
1227 * after ensuring that the data has been saved to disk -
1228 * since if the reference remains (rarer), it will be
1229 * read from disk into another page. Splitting into two
1230 * pages would be incorrect if swap supported "shared
1231 * private" pages, but they are handled by tmpfs files.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001232 */
KAMEZAWA Hiroyuki355cfa72009-06-16 15:32:53 -07001233 if (swap_count(*swap_map) &&
1234 PageDirty(page) && PageSwapCache(page)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001235 struct writeback_control wbc = {
1236 .sync_mode = WB_SYNC_NONE,
1237 };
1238
1239 swap_writepage(page, &wbc);
1240 lock_page(page);
1241 wait_on_page_writeback(page);
1242 }
Hugh Dickins68bdc8d2009-01-06 14:39:37 -08001243
1244 /*
1245 * It is conceivable that a racing task removed this page from
1246 * swap cache just before we acquired the page lock at the top,
1247 * or while we dropped it in unuse_mm(). The page might even
1248 * be back in swap cache on another swap area: that we must not
1249 * delete, since it may not have been written out to swap yet.
1250 */
1251 if (PageSwapCache(page) &&
1252 likely(page_private(page) == entry.val))
Hugh Dickins2e0e26c2008-02-04 22:28:53 -08001253 delete_from_swap_cache(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001254
1255 /*
1256 * So we could skip searching mms once swap count went
1257 * to 1, we did not mark any present ptes as dirty: must
Anderson Briglia2706a1b2007-07-15 23:38:09 -07001258 * mark page dirty so shrink_page_list will preserve it.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001259 */
1260 SetPageDirty(page);
KAMEZAWA Hiroyuki355cfa72009-06-16 15:32:53 -07001261retry:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001262 unlock_page(page);
1263 page_cache_release(page);
1264
1265 /*
1266 * Make sure that we aren't completely killing
1267 * interactive performance.
1268 */
1269 cond_resched();
1270 }
1271
1272 mmput(start_mm);
1273 if (reset_overflow) {
1274 printk(KERN_WARNING "swapoff: cleared swap entry overflow\n");
1275 swap_overflow = 0;
1276 }
1277 return retval;
1278}
1279
1280/*
Hugh Dickins5d337b92005-09-03 15:54:41 -07001281 * After a successful try_to_unuse, if no swap is now in use, we know
1282 * we can empty the mmlist. swap_lock must be held on entry and exit.
1283 * Note that mmlist_lock nests inside swap_lock, and an mm must be
Linus Torvalds1da177e2005-04-16 15:20:36 -07001284 * added to the mmlist just after page_duplicate - before would be racy.
1285 */
1286static void drain_mmlist(void)
1287{
1288 struct list_head *p, *next;
Hugh Dickinsefa90a92009-12-14 17:58:41 -08001289 unsigned int type;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001290
Hugh Dickinsefa90a92009-12-14 17:58:41 -08001291 for (type = 0; type < nr_swapfiles; type++)
1292 if (swap_info[type]->inuse_pages)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001293 return;
1294 spin_lock(&mmlist_lock);
1295 list_for_each_safe(p, next, &init_mm.mmlist)
1296 list_del_init(p);
1297 spin_unlock(&mmlist_lock);
1298}
1299
1300/*
1301 * Use this swapdev's extent info to locate the (PAGE_SIZE) block which
Hugh Dickinsf29ad6a2009-12-14 17:58:40 -08001302 * corresponds to page offset `offset'. Note that the type of this function
1303 * is sector_t, but it returns page offset into the bdev, not sector offset.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001304 */
Hugh Dickinsf29ad6a2009-12-14 17:58:40 -08001305sector_t map_swap_page(swp_entry_t entry, struct block_device **bdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001306{
Hugh Dickinsf29ad6a2009-12-14 17:58:40 -08001307 struct swap_info_struct *sis;
1308 struct swap_extent *start_se;
1309 struct swap_extent *se;
1310 pgoff_t offset;
1311
Hugh Dickinsefa90a92009-12-14 17:58:41 -08001312 sis = swap_info[swp_type(entry)];
Hugh Dickinsf29ad6a2009-12-14 17:58:40 -08001313 *bdev = sis->bdev;
1314
1315 offset = swp_offset(entry);
1316 start_se = sis->curr_swap_extent;
1317 se = start_se;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001318
1319 for ( ; ; ) {
1320 struct list_head *lh;
1321
1322 if (se->start_page <= offset &&
1323 offset < (se->start_page + se->nr_pages)) {
1324 return se->start_block + (offset - se->start_page);
1325 }
Hugh Dickins11d31882005-09-03 15:54:34 -07001326 lh = se->list.next;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001327 se = list_entry(lh, struct swap_extent, list);
1328 sis->curr_swap_extent = se;
1329 BUG_ON(se == start_se); /* It *must* be present */
1330 }
1331}
1332
1333/*
1334 * Free all of a swapdev's extent information
1335 */
1336static void destroy_swap_extents(struct swap_info_struct *sis)
1337{
Hugh Dickins9625a5f2009-12-14 17:58:42 -08001338 while (!list_empty(&sis->first_swap_extent.list)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001339 struct swap_extent *se;
1340
Hugh Dickins9625a5f2009-12-14 17:58:42 -08001341 se = list_entry(sis->first_swap_extent.list.next,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001342 struct swap_extent, list);
1343 list_del(&se->list);
1344 kfree(se);
1345 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001346}
1347
1348/*
1349 * Add a block range (and the corresponding page range) into this swapdev's
Hugh Dickins11d31882005-09-03 15:54:34 -07001350 * extent list. The extent list is kept sorted in page order.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001351 *
Hugh Dickins11d31882005-09-03 15:54:34 -07001352 * This function rather assumes that it is called in ascending page order.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001353 */
1354static int
1355add_swap_extent(struct swap_info_struct *sis, unsigned long start_page,
1356 unsigned long nr_pages, sector_t start_block)
1357{
1358 struct swap_extent *se;
1359 struct swap_extent *new_se;
1360 struct list_head *lh;
1361
Hugh Dickins9625a5f2009-12-14 17:58:42 -08001362 if (start_page == 0) {
1363 se = &sis->first_swap_extent;
1364 sis->curr_swap_extent = se;
1365 se->start_page = 0;
1366 se->nr_pages = nr_pages;
1367 se->start_block = start_block;
1368 return 1;
1369 } else {
1370 lh = sis->first_swap_extent.list.prev; /* Highest extent */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001371 se = list_entry(lh, struct swap_extent, list);
Hugh Dickins11d31882005-09-03 15:54:34 -07001372 BUG_ON(se->start_page + se->nr_pages != start_page);
1373 if (se->start_block + se->nr_pages == start_block) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001374 /* Merge it */
1375 se->nr_pages += nr_pages;
1376 return 0;
1377 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001378 }
1379
1380 /*
1381 * No merge. Insert a new extent, preserving ordering.
1382 */
1383 new_se = kmalloc(sizeof(*se), GFP_KERNEL);
1384 if (new_se == NULL)
1385 return -ENOMEM;
1386 new_se->start_page = start_page;
1387 new_se->nr_pages = nr_pages;
1388 new_se->start_block = start_block;
1389
Hugh Dickins9625a5f2009-12-14 17:58:42 -08001390 list_add_tail(&new_se->list, &sis->first_swap_extent.list);
Hugh Dickins53092a72005-09-03 15:54:34 -07001391 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001392}
1393
1394/*
1395 * A `swap extent' is a simple thing which maps a contiguous range of pages
1396 * onto a contiguous range of disk blocks. An ordered list of swap extents
1397 * is built at swapon time and is then used at swap_writepage/swap_readpage
1398 * time for locating where on disk a page belongs.
1399 *
1400 * If the swapfile is an S_ISBLK block device, a single extent is installed.
1401 * This is done so that the main operating code can treat S_ISBLK and S_ISREG
1402 * swap files identically.
1403 *
1404 * Whether the swapdev is an S_ISREG file or an S_ISBLK blockdev, the swap
1405 * extent list operates in PAGE_SIZE disk blocks. Both S_ISREG and S_ISBLK
1406 * swapfiles are handled *identically* after swapon time.
1407 *
1408 * For S_ISREG swapfiles, setup_swap_extents() will walk all the file's blocks
1409 * and will parse them into an ordered extent list, in PAGE_SIZE chunks. If
1410 * some stray blocks are found which do not fall within the PAGE_SIZE alignment
1411 * requirements, they are simply tossed out - we will never use those blocks
1412 * for swapping.
1413 *
Hugh Dickinsb0d9bcd2005-09-03 15:54:31 -07001414 * For S_ISREG swapfiles we set S_SWAPFILE across the life of the swapon. This
Linus Torvalds1da177e2005-04-16 15:20:36 -07001415 * prevents root from shooting her foot off by ftruncating an in-use swapfile,
1416 * which will scribble on the fs.
1417 *
1418 * The amount of disk space which a single swap extent represents varies.
1419 * Typically it is in the 1-4 megabyte range. So we can have hundreds of
1420 * extents in the list. To avoid much list walking, we cache the previous
1421 * search location in `curr_swap_extent', and start new searches from there.
1422 * This is extremely effective. The average number of iterations in
1423 * map_swap_page() has been measured at about 0.3 per page. - akpm.
1424 */
Hugh Dickins53092a72005-09-03 15:54:34 -07001425static int setup_swap_extents(struct swap_info_struct *sis, sector_t *span)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001426{
1427 struct inode *inode;
1428 unsigned blocks_per_page;
1429 unsigned long page_no;
1430 unsigned blkbits;
1431 sector_t probe_block;
1432 sector_t last_block;
Hugh Dickins53092a72005-09-03 15:54:34 -07001433 sector_t lowest_block = -1;
1434 sector_t highest_block = 0;
1435 int nr_extents = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001436 int ret;
1437
1438 inode = sis->swap_file->f_mapping->host;
1439 if (S_ISBLK(inode->i_mode)) {
1440 ret = add_swap_extent(sis, 0, sis->max, 0);
Hugh Dickins53092a72005-09-03 15:54:34 -07001441 *span = sis->pages;
Hugh Dickins9625a5f2009-12-14 17:58:42 -08001442 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001443 }
1444
1445 blkbits = inode->i_blkbits;
1446 blocks_per_page = PAGE_SIZE >> blkbits;
1447
1448 /*
1449 * Map all the blocks into the extent list. This code doesn't try
1450 * to be very smart.
1451 */
1452 probe_block = 0;
1453 page_no = 0;
1454 last_block = i_size_read(inode) >> blkbits;
1455 while ((probe_block + blocks_per_page) <= last_block &&
1456 page_no < sis->max) {
1457 unsigned block_in_page;
1458 sector_t first_block;
1459
1460 first_block = bmap(inode, probe_block);
1461 if (first_block == 0)
1462 goto bad_bmap;
1463
1464 /*
1465 * It must be PAGE_SIZE aligned on-disk
1466 */
1467 if (first_block & (blocks_per_page - 1)) {
1468 probe_block++;
1469 goto reprobe;
1470 }
1471
1472 for (block_in_page = 1; block_in_page < blocks_per_page;
1473 block_in_page++) {
1474 sector_t block;
1475
1476 block = bmap(inode, probe_block + block_in_page);
1477 if (block == 0)
1478 goto bad_bmap;
1479 if (block != first_block + block_in_page) {
1480 /* Discontiguity */
1481 probe_block++;
1482 goto reprobe;
1483 }
1484 }
1485
Hugh Dickins53092a72005-09-03 15:54:34 -07001486 first_block >>= (PAGE_SHIFT - blkbits);
1487 if (page_no) { /* exclude the header page */
1488 if (first_block < lowest_block)
1489 lowest_block = first_block;
1490 if (first_block > highest_block)
1491 highest_block = first_block;
1492 }
1493
Linus Torvalds1da177e2005-04-16 15:20:36 -07001494 /*
1495 * We found a PAGE_SIZE-length, PAGE_SIZE-aligned run of blocks
1496 */
Hugh Dickins53092a72005-09-03 15:54:34 -07001497 ret = add_swap_extent(sis, page_no, 1, first_block);
1498 if (ret < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001499 goto out;
Hugh Dickins53092a72005-09-03 15:54:34 -07001500 nr_extents += ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001501 page_no++;
1502 probe_block += blocks_per_page;
1503reprobe:
1504 continue;
1505 }
Hugh Dickins53092a72005-09-03 15:54:34 -07001506 ret = nr_extents;
1507 *span = 1 + highest_block - lowest_block;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001508 if (page_no == 0)
Hugh Dickinse2244ec2005-09-03 15:54:32 -07001509 page_no = 1; /* force Empty message */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001510 sis->max = page_no;
Hugh Dickinse2244ec2005-09-03 15:54:32 -07001511 sis->pages = page_no - 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001512 sis->highest_bit = page_no - 1;
Hugh Dickins9625a5f2009-12-14 17:58:42 -08001513out:
1514 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001515bad_bmap:
1516 printk(KERN_ERR "swapon: swapfile has holes\n");
1517 ret = -EINVAL;
Hugh Dickins9625a5f2009-12-14 17:58:42 -08001518 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001519}
1520
Heiko Carstensc4ea37c2009-01-14 14:14:28 +01001521SYSCALL_DEFINE1(swapoff, const char __user *, specialfile)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001522{
Hugh Dickins73c34b62009-12-14 17:58:43 -08001523 struct swap_info_struct *p = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001524 unsigned short *swap_map;
1525 struct file *swap_file, *victim;
1526 struct address_space *mapping;
1527 struct inode *inode;
Hugh Dickins73c34b62009-12-14 17:58:43 -08001528 char *pathname;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001529 int i, type, prev;
1530 int err;
Hugh Dickins886bb7e2009-01-06 14:39:48 -08001531
Linus Torvalds1da177e2005-04-16 15:20:36 -07001532 if (!capable(CAP_SYS_ADMIN))
1533 return -EPERM;
1534
1535 pathname = getname(specialfile);
1536 err = PTR_ERR(pathname);
1537 if (IS_ERR(pathname))
1538 goto out;
1539
1540 victim = filp_open(pathname, O_RDWR|O_LARGEFILE, 0);
1541 putname(pathname);
1542 err = PTR_ERR(victim);
1543 if (IS_ERR(victim))
1544 goto out;
1545
1546 mapping = victim->f_mapping;
1547 prev = -1;
Hugh Dickins5d337b92005-09-03 15:54:41 -07001548 spin_lock(&swap_lock);
Hugh Dickinsefa90a92009-12-14 17:58:41 -08001549 for (type = swap_list.head; type >= 0; type = swap_info[type]->next) {
1550 p = swap_info[type];
Hugh Dickins22c6f8f2009-01-06 14:39:48 -08001551 if (p->flags & SWP_WRITEOK) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001552 if (p->swap_file->f_mapping == mapping)
1553 break;
1554 }
1555 prev = type;
1556 }
1557 if (type < 0) {
1558 err = -EINVAL;
Hugh Dickins5d337b92005-09-03 15:54:41 -07001559 spin_unlock(&swap_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001560 goto out_dput;
1561 }
1562 if (!security_vm_enough_memory(p->pages))
1563 vm_unacct_memory(p->pages);
1564 else {
1565 err = -ENOMEM;
Hugh Dickins5d337b92005-09-03 15:54:41 -07001566 spin_unlock(&swap_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001567 goto out_dput;
1568 }
Hugh Dickinsefa90a92009-12-14 17:58:41 -08001569 if (prev < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001570 swap_list.head = p->next;
Hugh Dickinsefa90a92009-12-14 17:58:41 -08001571 else
1572 swap_info[prev]->next = p->next;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001573 if (type == swap_list.next) {
1574 /* just pick something that's safe... */
1575 swap_list.next = swap_list.head;
1576 }
Hugh Dickins78ecba02008-07-23 21:28:23 -07001577 if (p->prio < 0) {
Hugh Dickinsefa90a92009-12-14 17:58:41 -08001578 for (i = p->next; i >= 0; i = swap_info[i]->next)
1579 swap_info[i]->prio = p->prio--;
Hugh Dickins78ecba02008-07-23 21:28:23 -07001580 least_priority++;
1581 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001582 nr_swap_pages -= p->pages;
1583 total_swap_pages -= p->pages;
1584 p->flags &= ~SWP_WRITEOK;
Hugh Dickins5d337b92005-09-03 15:54:41 -07001585 spin_unlock(&swap_lock);
Hugh Dickinsfb4f88d2005-09-03 15:54:37 -07001586
Hugh Dickins35451be2009-09-21 17:02:27 -07001587 current->flags |= PF_OOM_ORIGIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001588 err = try_to_unuse(type);
Hugh Dickins35451be2009-09-21 17:02:27 -07001589 current->flags &= ~PF_OOM_ORIGIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001590
Linus Torvalds1da177e2005-04-16 15:20:36 -07001591 if (err) {
1592 /* re-insert swap space back into swap_list */
Hugh Dickins5d337b92005-09-03 15:54:41 -07001593 spin_lock(&swap_lock);
Hugh Dickins78ecba02008-07-23 21:28:23 -07001594 if (p->prio < 0)
1595 p->prio = --least_priority;
1596 prev = -1;
Hugh Dickinsefa90a92009-12-14 17:58:41 -08001597 for (i = swap_list.head; i >= 0; i = swap_info[i]->next) {
1598 if (p->prio >= swap_info[i]->prio)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001599 break;
Hugh Dickins78ecba02008-07-23 21:28:23 -07001600 prev = i;
1601 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001602 p->next = i;
1603 if (prev < 0)
Hugh Dickinsefa90a92009-12-14 17:58:41 -08001604 swap_list.head = swap_list.next = type;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001605 else
Hugh Dickinsefa90a92009-12-14 17:58:41 -08001606 swap_info[prev]->next = type;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001607 nr_swap_pages += p->pages;
1608 total_swap_pages += p->pages;
1609 p->flags |= SWP_WRITEOK;
Hugh Dickins5d337b92005-09-03 15:54:41 -07001610 spin_unlock(&swap_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001611 goto out_dput;
1612 }
Hugh Dickins52b7efdb2005-09-03 15:54:39 -07001613
1614 /* wait for any unplug function to finish */
1615 down_write(&swap_unplug_sem);
1616 up_write(&swap_unplug_sem);
1617
Hugh Dickins4cd3bb12005-09-03 15:54:33 -07001618 destroy_swap_extents(p);
Ingo Molnarfc0abb12006-01-18 17:42:33 -08001619 mutex_lock(&swapon_mutex);
Hugh Dickins5d337b92005-09-03 15:54:41 -07001620 spin_lock(&swap_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001621 drain_mmlist();
Hugh Dickins5d337b92005-09-03 15:54:41 -07001622
1623 /* wait for anyone still in scan_swap_map */
1624 p->highest_bit = 0; /* cuts scans short */
1625 while (p->flags >= SWP_SCANNING) {
1626 spin_unlock(&swap_lock);
Nishanth Aravamudan13e4b572005-09-10 00:27:25 -07001627 schedule_timeout_uninterruptible(1);
Hugh Dickins5d337b92005-09-03 15:54:41 -07001628 spin_lock(&swap_lock);
1629 }
1630
Linus Torvalds1da177e2005-04-16 15:20:36 -07001631 swap_file = p->swap_file;
1632 p->swap_file = NULL;
1633 p->max = 0;
1634 swap_map = p->swap_map;
1635 p->swap_map = NULL;
1636 p->flags = 0;
Hugh Dickins5d337b92005-09-03 15:54:41 -07001637 spin_unlock(&swap_lock);
Ingo Molnarfc0abb12006-01-18 17:42:33 -08001638 mutex_unlock(&swapon_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001639 vfree(swap_map);
KAMEZAWA Hiroyuki27a7faa2009-01-07 18:07:58 -08001640 /* Destroy swap account informatin */
1641 swap_cgroup_swapoff(type);
1642
Linus Torvalds1da177e2005-04-16 15:20:36 -07001643 inode = mapping->host;
1644 if (S_ISBLK(inode->i_mode)) {
1645 struct block_device *bdev = I_BDEV(inode);
1646 set_blocksize(bdev, p->old_block_size);
1647 bd_release(bdev);
1648 } else {
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08001649 mutex_lock(&inode->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001650 inode->i_flags &= ~S_SWAPFILE;
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08001651 mutex_unlock(&inode->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001652 }
1653 filp_close(swap_file, NULL);
1654 err = 0;
1655
1656out_dput:
1657 filp_close(victim, NULL);
1658out:
1659 return err;
1660}
1661
1662#ifdef CONFIG_PROC_FS
1663/* iterator */
1664static void *swap_start(struct seq_file *swap, loff_t *pos)
1665{
Hugh Dickinsefa90a92009-12-14 17:58:41 -08001666 struct swap_info_struct *si;
1667 int type;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001668 loff_t l = *pos;
1669
Ingo Molnarfc0abb12006-01-18 17:42:33 -08001670 mutex_lock(&swapon_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001671
Suleiman Souhlal881e4aa2006-12-06 20:32:28 -08001672 if (!l)
1673 return SEQ_START_TOKEN;
1674
Hugh Dickinsefa90a92009-12-14 17:58:41 -08001675 for (type = 0; type < nr_swapfiles; type++) {
1676 smp_rmb(); /* read nr_swapfiles before swap_info[type] */
1677 si = swap_info[type];
1678 if (!(si->flags & SWP_USED) || !si->swap_map)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001679 continue;
Suleiman Souhlal881e4aa2006-12-06 20:32:28 -08001680 if (!--l)
Hugh Dickinsefa90a92009-12-14 17:58:41 -08001681 return si;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001682 }
1683
1684 return NULL;
1685}
1686
1687static void *swap_next(struct seq_file *swap, void *v, loff_t *pos)
1688{
Hugh Dickinsefa90a92009-12-14 17:58:41 -08001689 struct swap_info_struct *si = v;
1690 int type;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001691
Suleiman Souhlal881e4aa2006-12-06 20:32:28 -08001692 if (v == SEQ_START_TOKEN)
Hugh Dickinsefa90a92009-12-14 17:58:41 -08001693 type = 0;
1694 else
1695 type = si->type + 1;
Suleiman Souhlal881e4aa2006-12-06 20:32:28 -08001696
Hugh Dickinsefa90a92009-12-14 17:58:41 -08001697 for (; type < nr_swapfiles; type++) {
1698 smp_rmb(); /* read nr_swapfiles before swap_info[type] */
1699 si = swap_info[type];
1700 if (!(si->flags & SWP_USED) || !si->swap_map)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001701 continue;
1702 ++*pos;
Hugh Dickinsefa90a92009-12-14 17:58:41 -08001703 return si;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001704 }
1705
1706 return NULL;
1707}
1708
1709static void swap_stop(struct seq_file *swap, void *v)
1710{
Ingo Molnarfc0abb12006-01-18 17:42:33 -08001711 mutex_unlock(&swapon_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001712}
1713
1714static int swap_show(struct seq_file *swap, void *v)
1715{
Hugh Dickinsefa90a92009-12-14 17:58:41 -08001716 struct swap_info_struct *si = v;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001717 struct file *file;
1718 int len;
1719
Hugh Dickinsefa90a92009-12-14 17:58:41 -08001720 if (si == SEQ_START_TOKEN) {
Suleiman Souhlal881e4aa2006-12-06 20:32:28 -08001721 seq_puts(swap,"Filename\t\t\t\tType\t\tSize\tUsed\tPriority\n");
1722 return 0;
1723 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001724
Hugh Dickinsefa90a92009-12-14 17:58:41 -08001725 file = si->swap_file;
Jan Blunckc32c2f62008-02-14 19:38:43 -08001726 len = seq_path(swap, &file->f_path, " \t\n\\");
Hugh Dickins6eb396d2005-09-03 15:54:35 -07001727 seq_printf(swap, "%*s%s\t%u\t%u\t%d\n",
Hugh Dickins886bb7e2009-01-06 14:39:48 -08001728 len < 40 ? 40 - len : 1, " ",
1729 S_ISBLK(file->f_path.dentry->d_inode->i_mode) ?
Linus Torvalds1da177e2005-04-16 15:20:36 -07001730 "partition" : "file\t",
Hugh Dickinsefa90a92009-12-14 17:58:41 -08001731 si->pages << (PAGE_SHIFT - 10),
1732 si->inuse_pages << (PAGE_SHIFT - 10),
1733 si->prio);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001734 return 0;
1735}
1736
Helge Deller15ad7cd2006-12-06 20:40:36 -08001737static const struct seq_operations swaps_op = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001738 .start = swap_start,
1739 .next = swap_next,
1740 .stop = swap_stop,
1741 .show = swap_show
1742};
1743
1744static int swaps_open(struct inode *inode, struct file *file)
1745{
1746 return seq_open(file, &swaps_op);
1747}
1748
Helge Deller15ad7cd2006-12-06 20:40:36 -08001749static const struct file_operations proc_swaps_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001750 .open = swaps_open,
1751 .read = seq_read,
1752 .llseek = seq_lseek,
1753 .release = seq_release,
1754};
1755
1756static int __init procswaps_init(void)
1757{
Denis V. Lunev3d71f862008-04-29 01:02:13 -07001758 proc_create("swaps", 0, NULL, &proc_swaps_operations);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001759 return 0;
1760}
1761__initcall(procswaps_init);
1762#endif /* CONFIG_PROC_FS */
1763
Jan Beulich17963162008-12-16 11:35:24 +00001764#ifdef MAX_SWAPFILES_CHECK
1765static int __init max_swapfiles_check(void)
1766{
1767 MAX_SWAPFILES_CHECK();
1768 return 0;
1769}
1770late_initcall(max_swapfiles_check);
1771#endif
1772
Linus Torvalds1da177e2005-04-16 15:20:36 -07001773/*
1774 * Written 01/25/92 by Simmule Turner, heavily changed by Linus.
1775 *
1776 * The swapon system call
1777 */
Heiko Carstensc4ea37c2009-01-14 14:14:28 +01001778SYSCALL_DEFINE2(swapon, const char __user *, specialfile, int, swap_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001779{
Hugh Dickins73c34b62009-12-14 17:58:43 -08001780 struct swap_info_struct *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001781 char *name = NULL;
1782 struct block_device *bdev = NULL;
1783 struct file *swap_file = NULL;
1784 struct address_space *mapping;
1785 unsigned int type;
1786 int i, prev;
1787 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001788 union swap_header *swap_header = NULL;
Hugh Dickins6eb396d2005-09-03 15:54:35 -07001789 unsigned int nr_good_pages = 0;
1790 int nr_extents = 0;
Hugh Dickins53092a72005-09-03 15:54:34 -07001791 sector_t span;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001792 unsigned long maxpages = 1;
Hugh Dickins73fd8742009-01-06 14:39:47 -08001793 unsigned long swapfilepages;
Hugh Dickins78ecba02008-07-23 21:28:23 -07001794 unsigned short *swap_map = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001795 struct page *page = NULL;
1796 struct inode *inode = NULL;
1797 int did_down = 0;
1798
1799 if (!capable(CAP_SYS_ADMIN))
1800 return -EPERM;
Hugh Dickinsefa90a92009-12-14 17:58:41 -08001801
1802 p = kzalloc(sizeof(*p), GFP_KERNEL);
1803 if (!p)
1804 return -ENOMEM;
1805
Hugh Dickins5d337b92005-09-03 15:54:41 -07001806 spin_lock(&swap_lock);
Hugh Dickinsefa90a92009-12-14 17:58:41 -08001807 for (type = 0; type < nr_swapfiles; type++) {
1808 if (!(swap_info[type]->flags & SWP_USED))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001809 break;
Hugh Dickinsefa90a92009-12-14 17:58:41 -08001810 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001811 error = -EPERM;
Christoph Lameter06972122006-06-23 02:03:35 -07001812 if (type >= MAX_SWAPFILES) {
Hugh Dickins5d337b92005-09-03 15:54:41 -07001813 spin_unlock(&swap_lock);
Hugh Dickinsefa90a92009-12-14 17:58:41 -08001814 kfree(p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001815 goto out;
1816 }
Hugh Dickinsefa90a92009-12-14 17:58:41 -08001817 if (type >= nr_swapfiles) {
1818 p->type = type;
1819 swap_info[type] = p;
1820 /*
1821 * Write swap_info[type] before nr_swapfiles, in case a
1822 * racing procfs swap_start() or swap_next() is reading them.
1823 * (We never shrink nr_swapfiles, we never free this entry.)
1824 */
1825 smp_wmb();
1826 nr_swapfiles++;
1827 } else {
1828 kfree(p);
1829 p = swap_info[type];
1830 /*
1831 * Do not memset this entry: a racing procfs swap_next()
1832 * would be relying on p->type to remain valid.
1833 */
1834 }
Hugh Dickins9625a5f2009-12-14 17:58:42 -08001835 INIT_LIST_HEAD(&p->first_swap_extent.list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001836 p->flags = SWP_USED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001837 p->next = -1;
Hugh Dickins5d337b92005-09-03 15:54:41 -07001838 spin_unlock(&swap_lock);
Hugh Dickinsefa90a92009-12-14 17:58:41 -08001839
Linus Torvalds1da177e2005-04-16 15:20:36 -07001840 name = getname(specialfile);
1841 error = PTR_ERR(name);
1842 if (IS_ERR(name)) {
1843 name = NULL;
1844 goto bad_swap_2;
1845 }
1846 swap_file = filp_open(name, O_RDWR|O_LARGEFILE, 0);
1847 error = PTR_ERR(swap_file);
1848 if (IS_ERR(swap_file)) {
1849 swap_file = NULL;
1850 goto bad_swap_2;
1851 }
1852
1853 p->swap_file = swap_file;
1854 mapping = swap_file->f_mapping;
1855 inode = mapping->host;
1856
1857 error = -EBUSY;
1858 for (i = 0; i < nr_swapfiles; i++) {
Hugh Dickinsefa90a92009-12-14 17:58:41 -08001859 struct swap_info_struct *q = swap_info[i];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001860
1861 if (i == type || !q->swap_file)
1862 continue;
1863 if (mapping == q->swap_file->f_mapping)
1864 goto bad_swap;
1865 }
1866
1867 error = -EINVAL;
1868 if (S_ISBLK(inode->i_mode)) {
1869 bdev = I_BDEV(inode);
1870 error = bd_claim(bdev, sys_swapon);
1871 if (error < 0) {
1872 bdev = NULL;
Rob Landleyf7b3a432005-09-22 21:44:27 -07001873 error = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001874 goto bad_swap;
1875 }
1876 p->old_block_size = block_size(bdev);
1877 error = set_blocksize(bdev, PAGE_SIZE);
1878 if (error < 0)
1879 goto bad_swap;
1880 p->bdev = bdev;
1881 } else if (S_ISREG(inode->i_mode)) {
1882 p->bdev = inode->i_sb->s_bdev;
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08001883 mutex_lock(&inode->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001884 did_down = 1;
1885 if (IS_SWAPFILE(inode)) {
1886 error = -EBUSY;
1887 goto bad_swap;
1888 }
1889 } else {
1890 goto bad_swap;
1891 }
1892
Hugh Dickins73fd8742009-01-06 14:39:47 -08001893 swapfilepages = i_size_read(inode) >> PAGE_SHIFT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001894
1895 /*
1896 * Read the swap header.
1897 */
1898 if (!mapping->a_ops->readpage) {
1899 error = -EINVAL;
1900 goto bad_swap;
1901 }
Pekka Enberg090d2b12006-06-23 02:05:08 -07001902 page = read_mapping_page(mapping, 0, swap_file);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001903 if (IS_ERR(page)) {
1904 error = PTR_ERR(page);
1905 goto bad_swap;
1906 }
Hugh Dickins81e33972009-01-06 14:39:49 -08001907 swap_header = kmap(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001908
Hugh Dickins81e33972009-01-06 14:39:49 -08001909 if (memcmp("SWAPSPACE2", swap_header->magic.magic, 10)) {
Jesper Juhle97a3112006-01-11 01:50:28 +01001910 printk(KERN_ERR "Unable to find swap-space signature\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001911 error = -EINVAL;
1912 goto bad_swap;
1913 }
Hugh Dickins886bb7e2009-01-06 14:39:48 -08001914
Hugh Dickins81e33972009-01-06 14:39:49 -08001915 /* swap partition endianess hack... */
1916 if (swab32(swap_header->info.version) == 1) {
1917 swab32s(&swap_header->info.version);
1918 swab32s(&swap_header->info.last_page);
1919 swab32s(&swap_header->info.nr_badpages);
1920 for (i = 0; i < swap_header->info.nr_badpages; i++)
1921 swab32s(&swap_header->info.badpages[i]);
1922 }
1923 /* Check the swap header's sub-version */
1924 if (swap_header->info.version != 1) {
1925 printk(KERN_WARNING
1926 "Unable to handle swap header version %d\n",
1927 swap_header->info.version);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001928 error = -EINVAL;
1929 goto bad_swap;
Hugh Dickins81e33972009-01-06 14:39:49 -08001930 }
1931
1932 p->lowest_bit = 1;
1933 p->cluster_next = 1;
Hugh Dickinsefa90a92009-12-14 17:58:41 -08001934 p->cluster_nr = 0;
Hugh Dickins81e33972009-01-06 14:39:49 -08001935
1936 /*
1937 * Find out how many pages are allowed for a single swap
1938 * device. There are two limiting factors: 1) the number of
1939 * bits for the swap offset in the swp_entry_t type and
1940 * 2) the number of bits in the a swap pte as defined by
1941 * the different architectures. In order to find the
1942 * largest possible bit mask a swap entry with swap type 0
1943 * and swap offset ~0UL is created, encoded to a swap pte,
1944 * decoded to a swp_entry_t again and finally the swap
1945 * offset is extracted. This will mask all the bits from
1946 * the initial ~0UL mask that can't be encoded in either
1947 * the swp_entry_t or the architecture definition of a
1948 * swap pte.
1949 */
1950 maxpages = swp_offset(pte_to_swp_entry(
1951 swp_entry_to_pte(swp_entry(0, ~0UL)))) - 1;
1952 if (maxpages > swap_header->info.last_page)
1953 maxpages = swap_header->info.last_page;
1954 p->highest_bit = maxpages - 1;
1955
1956 error = -EINVAL;
1957 if (!maxpages)
1958 goto bad_swap;
1959 if (swapfilepages && maxpages > swapfilepages) {
1960 printk(KERN_WARNING
1961 "Swap area shorter than signature indicates\n");
1962 goto bad_swap;
1963 }
1964 if (swap_header->info.nr_badpages && S_ISREG(inode->i_mode))
1965 goto bad_swap;
1966 if (swap_header->info.nr_badpages > MAX_SWAP_BADPAGES)
1967 goto bad_swap;
1968
1969 /* OK, set up the swap map and apply the bad block list */
1970 swap_map = vmalloc(maxpages * sizeof(short));
1971 if (!swap_map) {
1972 error = -ENOMEM;
1973 goto bad_swap;
1974 }
1975
1976 memset(swap_map, 0, maxpages * sizeof(short));
1977 for (i = 0; i < swap_header->info.nr_badpages; i++) {
1978 int page_nr = swap_header->info.badpages[i];
1979 if (page_nr <= 0 || page_nr >= swap_header->info.last_page) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001980 error = -EINVAL;
1981 goto bad_swap;
1982 }
Hugh Dickins81e33972009-01-06 14:39:49 -08001983 swap_map[page_nr] = SWAP_MAP_BAD;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001984 }
KAMEZAWA Hiroyuki27a7faa2009-01-07 18:07:58 -08001985
1986 error = swap_cgroup_swapon(type, maxpages);
1987 if (error)
1988 goto bad_swap;
1989
Hugh Dickins81e33972009-01-06 14:39:49 -08001990 nr_good_pages = swap_header->info.last_page -
1991 swap_header->info.nr_badpages -
1992 1 /* header page */;
Hugh Dickinse2244ec2005-09-03 15:54:32 -07001993
Hugh Dickinse2244ec2005-09-03 15:54:32 -07001994 if (nr_good_pages) {
Hugh Dickins78ecba02008-07-23 21:28:23 -07001995 swap_map[0] = SWAP_MAP_BAD;
Hugh Dickinse2244ec2005-09-03 15:54:32 -07001996 p->max = maxpages;
1997 p->pages = nr_good_pages;
Hugh Dickins53092a72005-09-03 15:54:34 -07001998 nr_extents = setup_swap_extents(p, &span);
1999 if (nr_extents < 0) {
2000 error = nr_extents;
Hugh Dickinse2244ec2005-09-03 15:54:32 -07002001 goto bad_swap;
Hugh Dickins53092a72005-09-03 15:54:34 -07002002 }
Hugh Dickinse2244ec2005-09-03 15:54:32 -07002003 nr_good_pages = p->pages;
2004 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002005 if (!nr_good_pages) {
2006 printk(KERN_WARNING "Empty swap-file\n");
2007 error = -EINVAL;
2008 goto bad_swap;
2009 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002010
Suresh Jayaraman3bd0f0c2009-09-30 10:53:48 +02002011 if (p->bdev) {
2012 if (blk_queue_nonrot(bdev_get_queue(p->bdev))) {
2013 p->flags |= SWP_SOLIDSTATE;
2014 p->cluster_next = 1 + (random32() % p->highest_bit);
2015 }
2016 if (discard_swap(p) == 0)
2017 p->flags |= SWP_DISCARDABLE;
Hugh Dickins20137a42009-01-06 14:39:54 -08002018 }
Hugh Dickins6a6ba832009-01-06 14:39:51 -08002019
Ingo Molnarfc0abb12006-01-18 17:42:33 -08002020 mutex_lock(&swapon_mutex);
Hugh Dickins5d337b92005-09-03 15:54:41 -07002021 spin_lock(&swap_lock);
Hugh Dickins78ecba02008-07-23 21:28:23 -07002022 if (swap_flags & SWAP_FLAG_PREFER)
2023 p->prio =
2024 (swap_flags & SWAP_FLAG_PRIO_MASK) >> SWAP_FLAG_PRIO_SHIFT;
2025 else
2026 p->prio = --least_priority;
2027 p->swap_map = swap_map;
Hugh Dickins22c6f8f2009-01-06 14:39:48 -08002028 p->flags |= SWP_WRITEOK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002029 nr_swap_pages += nr_good_pages;
2030 total_swap_pages += nr_good_pages;
Hugh Dickins53092a72005-09-03 15:54:34 -07002031
Hugh Dickins6eb396d2005-09-03 15:54:35 -07002032 printk(KERN_INFO "Adding %uk swap on %s. "
Hugh Dickins20137a42009-01-06 14:39:54 -08002033 "Priority:%d extents:%d across:%lluk %s%s\n",
Hugh Dickins53092a72005-09-03 15:54:34 -07002034 nr_good_pages<<(PAGE_SHIFT-10), name, p->prio,
Hugh Dickins6a6ba832009-01-06 14:39:51 -08002035 nr_extents, (unsigned long long)span<<(PAGE_SHIFT-10),
Hugh Dickins20137a42009-01-06 14:39:54 -08002036 (p->flags & SWP_SOLIDSTATE) ? "SS" : "",
2037 (p->flags & SWP_DISCARDABLE) ? "D" : "");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002038
2039 /* insert swap space into swap_list: */
2040 prev = -1;
Hugh Dickinsefa90a92009-12-14 17:58:41 -08002041 for (i = swap_list.head; i >= 0; i = swap_info[i]->next) {
2042 if (p->prio >= swap_info[i]->prio)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002043 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002044 prev = i;
2045 }
2046 p->next = i;
Hugh Dickinsefa90a92009-12-14 17:58:41 -08002047 if (prev < 0)
2048 swap_list.head = swap_list.next = type;
2049 else
2050 swap_info[prev]->next = type;
Hugh Dickins5d337b92005-09-03 15:54:41 -07002051 spin_unlock(&swap_lock);
Ingo Molnarfc0abb12006-01-18 17:42:33 -08002052 mutex_unlock(&swapon_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002053 error = 0;
2054 goto out;
2055bad_swap:
2056 if (bdev) {
2057 set_blocksize(bdev, p->old_block_size);
2058 bd_release(bdev);
2059 }
Hugh Dickins4cd3bb12005-09-03 15:54:33 -07002060 destroy_swap_extents(p);
KAMEZAWA Hiroyuki27a7faa2009-01-07 18:07:58 -08002061 swap_cgroup_swapoff(type);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002062bad_swap_2:
Hugh Dickins5d337b92005-09-03 15:54:41 -07002063 spin_lock(&swap_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002064 p->swap_file = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002065 p->flags = 0;
Hugh Dickins5d337b92005-09-03 15:54:41 -07002066 spin_unlock(&swap_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002067 vfree(swap_map);
2068 if (swap_file)
2069 filp_close(swap_file, NULL);
2070out:
2071 if (page && !IS_ERR(page)) {
2072 kunmap(page);
2073 page_cache_release(page);
2074 }
2075 if (name)
2076 putname(name);
2077 if (did_down) {
2078 if (!error)
2079 inode->i_flags |= S_SWAPFILE;
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08002080 mutex_unlock(&inode->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002081 }
2082 return error;
2083}
2084
2085void si_swapinfo(struct sysinfo *val)
2086{
Hugh Dickinsefa90a92009-12-14 17:58:41 -08002087 unsigned int type;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002088 unsigned long nr_to_be_unused = 0;
2089
Hugh Dickins5d337b92005-09-03 15:54:41 -07002090 spin_lock(&swap_lock);
Hugh Dickinsefa90a92009-12-14 17:58:41 -08002091 for (type = 0; type < nr_swapfiles; type++) {
2092 struct swap_info_struct *si = swap_info[type];
2093
2094 if ((si->flags & SWP_USED) && !(si->flags & SWP_WRITEOK))
2095 nr_to_be_unused += si->inuse_pages;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002096 }
2097 val->freeswap = nr_swap_pages + nr_to_be_unused;
2098 val->totalswap = total_swap_pages + nr_to_be_unused;
Hugh Dickins5d337b92005-09-03 15:54:41 -07002099 spin_unlock(&swap_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002100}
2101
2102/*
2103 * Verify that a swap entry is valid and increment its swap map count.
2104 *
2105 * Note: if swap_map[] reaches SWAP_MAP_MAX the entries are treated as
2106 * "permanent", but will be reclaimed by the next swapoff.
KAMEZAWA Hiroyuki355cfa72009-06-16 15:32:53 -07002107 * Returns error code in following case.
2108 * - success -> 0
2109 * - swp_entry is invalid -> EINVAL
2110 * - swp_entry is migration entry -> EINVAL
2111 * - swap-cache reference is requested but there is already one. -> EEXIST
2112 * - swap-cache reference is requested but the entry is not used. -> ENOENT
Linus Torvalds1da177e2005-04-16 15:20:36 -07002113 */
KAMEZAWA Hiroyuki355cfa72009-06-16 15:32:53 -07002114static int __swap_duplicate(swp_entry_t entry, bool cache)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002115{
Hugh Dickins73c34b62009-12-14 17:58:43 -08002116 struct swap_info_struct *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002117 unsigned long offset, type;
KAMEZAWA Hiroyuki355cfa72009-06-16 15:32:53 -07002118 int result = -EINVAL;
2119 int count;
2120 bool has_cache;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002121
Andi Kleena7420aa2009-09-16 11:50:05 +02002122 if (non_swap_entry(entry))
KAMEZAWA Hiroyuki355cfa72009-06-16 15:32:53 -07002123 return -EINVAL;
Christoph Lameter06972122006-06-23 02:03:35 -07002124
Linus Torvalds1da177e2005-04-16 15:20:36 -07002125 type = swp_type(entry);
2126 if (type >= nr_swapfiles)
2127 goto bad_file;
Hugh Dickinsefa90a92009-12-14 17:58:41 -08002128 p = swap_info[type];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002129 offset = swp_offset(entry);
2130
Hugh Dickins5d337b92005-09-03 15:54:41 -07002131 spin_lock(&swap_lock);
KAMEZAWA Hiroyuki355cfa72009-06-16 15:32:53 -07002132
2133 if (unlikely(offset >= p->max))
2134 goto unlock_out;
2135
2136 count = swap_count(p->swap_map[offset]);
2137 has_cache = swap_has_cache(p->swap_map[offset]);
2138
2139 if (cache == SWAP_CACHE) { /* called for swapcache/swapin-readahead */
2140
2141 /* set SWAP_HAS_CACHE if there is no cache and entry is used */
2142 if (!has_cache && count) {
2143 p->swap_map[offset] = encode_swapmap(count, true);
2144 result = 0;
2145 } else if (has_cache) /* someone added cache */
2146 result = -EEXIST;
2147 else if (!count) /* no users */
2148 result = -ENOENT;
2149
2150 } else if (count || has_cache) {
2151 if (count < SWAP_MAP_MAX - 1) {
2152 p->swap_map[offset] = encode_swapmap(count + 1,
2153 has_cache);
2154 result = 0;
2155 } else if (count <= SWAP_MAP_MAX) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002156 if (swap_overflow++ < 5)
KAMEZAWA Hiroyuki355cfa72009-06-16 15:32:53 -07002157 printk(KERN_WARNING
2158 "swap_dup: swap entry overflow\n");
2159 p->swap_map[offset] = encode_swapmap(SWAP_MAP_MAX,
2160 has_cache);
2161 result = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002162 }
KAMEZAWA Hiroyuki355cfa72009-06-16 15:32:53 -07002163 } else
2164 result = -ENOENT; /* unused swap entry */
2165unlock_out:
Hugh Dickins5d337b92005-09-03 15:54:41 -07002166 spin_unlock(&swap_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002167out:
2168 return result;
2169
2170bad_file:
2171 printk(KERN_ERR "swap_dup: %s%08lx\n", Bad_file, entry.val);
2172 goto out;
2173}
KAMEZAWA Hiroyuki355cfa72009-06-16 15:32:53 -07002174/*
2175 * increase reference count of swap entry by 1.
2176 */
2177void swap_duplicate(swp_entry_t entry)
2178{
2179 __swap_duplicate(entry, SWAP_MAP);
2180}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002181
KAMEZAWA Hiroyukicb4b86b2009-06-16 15:32:52 -07002182/*
KAMEZAWA Hiroyuki355cfa72009-06-16 15:32:53 -07002183 * @entry: swap entry for which we allocate swap cache.
2184 *
Hugh Dickins73c34b62009-12-14 17:58:43 -08002185 * Called when allocating swap cache for existing swap entry,
KAMEZAWA Hiroyuki355cfa72009-06-16 15:32:53 -07002186 * This can return error codes. Returns 0 at success.
2187 * -EBUSY means there is a swap cache.
2188 * Note: return code is different from swap_duplicate().
KAMEZAWA Hiroyukicb4b86b2009-06-16 15:32:52 -07002189 */
2190int swapcache_prepare(swp_entry_t entry)
2191{
KAMEZAWA Hiroyuki355cfa72009-06-16 15:32:53 -07002192 return __swap_duplicate(entry, SWAP_CACHE);
KAMEZAWA Hiroyukicb4b86b2009-06-16 15:32:52 -07002193}
2194
Linus Torvalds1da177e2005-04-16 15:20:36 -07002195/*
Hugh Dickins5d337b92005-09-03 15:54:41 -07002196 * swap_lock prevents swap_map being freed. Don't grab an extra
Linus Torvalds1da177e2005-04-16 15:20:36 -07002197 * reference on the swaphandle, it doesn't matter if it becomes unused.
2198 */
2199int valid_swaphandles(swp_entry_t entry, unsigned long *offset)
2200{
Hugh Dickins89528982008-02-04 22:28:45 -08002201 struct swap_info_struct *si;
Hugh Dickins3f9e7942006-09-29 02:01:26 -07002202 int our_page_cluster = page_cluster;
Hugh Dickins89528982008-02-04 22:28:45 -08002203 pgoff_t target, toff;
2204 pgoff_t base, end;
2205 int nr_pages = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002206
Hugh Dickins3f9e7942006-09-29 02:01:26 -07002207 if (!our_page_cluster) /* no readahead */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002208 return 0;
Hugh Dickins89528982008-02-04 22:28:45 -08002209
Hugh Dickinsefa90a92009-12-14 17:58:41 -08002210 si = swap_info[swp_type(entry)];
Hugh Dickins89528982008-02-04 22:28:45 -08002211 target = swp_offset(entry);
2212 base = (target >> our_page_cluster) << our_page_cluster;
2213 end = base + (1 << our_page_cluster);
2214 if (!base) /* first page is swap header */
2215 base++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002216
Hugh Dickins5d337b92005-09-03 15:54:41 -07002217 spin_lock(&swap_lock);
Hugh Dickins89528982008-02-04 22:28:45 -08002218 if (end > si->max) /* don't go beyond end of map */
2219 end = si->max;
2220
2221 /* Count contiguous allocated slots above our target */
2222 for (toff = target; ++toff < end; nr_pages++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002223 /* Don't read in free or bad pages */
Hugh Dickins89528982008-02-04 22:28:45 -08002224 if (!si->swap_map[toff])
Linus Torvalds1da177e2005-04-16 15:20:36 -07002225 break;
KAMEZAWA Hiroyuki355cfa72009-06-16 15:32:53 -07002226 if (swap_count(si->swap_map[toff]) == SWAP_MAP_BAD)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002227 break;
Hugh Dickins89528982008-02-04 22:28:45 -08002228 }
2229 /* Count contiguous allocated slots below our target */
2230 for (toff = target; --toff >= base; nr_pages++) {
2231 /* Don't read in free or bad pages */
2232 if (!si->swap_map[toff])
2233 break;
KAMEZAWA Hiroyuki355cfa72009-06-16 15:32:53 -07002234 if (swap_count(si->swap_map[toff]) == SWAP_MAP_BAD)
Hugh Dickins89528982008-02-04 22:28:45 -08002235 break;
2236 }
Hugh Dickins5d337b92005-09-03 15:54:41 -07002237 spin_unlock(&swap_lock);
Hugh Dickins89528982008-02-04 22:28:45 -08002238
2239 /*
2240 * Indicate starting offset, and return number of pages to get:
2241 * if only 1, say 0, since there's then no readahead to be done.
2242 */
2243 *offset = ++toff;
2244 return nr_pages? ++nr_pages: 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002245}