swapfile.c 42 KB

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