swapfile.c 45 KB

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