swapfile.c 42 KB

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