swapfile.c 41 KB

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