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