swapfile.c 43 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778
  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_SOFTWARE_SUSPEND
  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)
  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. spin_unlock(&swap_lock);
  398. return i;
  399. }
  400. if (bdev == sis->bdev) {
  401. struct swap_extent *se;
  402. se = list_entry(sis->extent_list.next,
  403. struct swap_extent, list);
  404. if (se->start_block == offset) {
  405. spin_unlock(&swap_lock);
  406. bdput(bdev);
  407. return i;
  408. }
  409. }
  410. }
  411. spin_unlock(&swap_lock);
  412. if (bdev)
  413. bdput(bdev);
  414. return -ENODEV;
  415. }
  416. /*
  417. * Return either the total number of swap pages of given type, or the number
  418. * of free pages of that type (depending on @free)
  419. *
  420. * This is needed for software suspend
  421. */
  422. unsigned int count_swap_pages(int type, int free)
  423. {
  424. unsigned int n = 0;
  425. if (type < nr_swapfiles) {
  426. spin_lock(&swap_lock);
  427. if (swap_info[type].flags & SWP_WRITEOK) {
  428. n = swap_info[type].pages;
  429. if (free)
  430. n -= swap_info[type].inuse_pages;
  431. }
  432. spin_unlock(&swap_lock);
  433. }
  434. return n;
  435. }
  436. #endif
  437. /*
  438. * No need to decide whether this PTE shares the swap entry with others,
  439. * just let do_wp_page work it out if a write is requested later - to
  440. * force COW, vm_page_prot omits write permission from any private vma.
  441. */
  442. static void unuse_pte(struct vm_area_struct *vma, pte_t *pte,
  443. unsigned long addr, swp_entry_t entry, struct page *page)
  444. {
  445. inc_mm_counter(vma->vm_mm, anon_rss);
  446. get_page(page);
  447. set_pte_at(vma->vm_mm, addr, pte,
  448. pte_mkold(mk_pte(page, vma->vm_page_prot)));
  449. page_add_anon_rmap(page, vma, addr);
  450. swap_free(entry);
  451. /*
  452. * Move the page to the active list so it is not
  453. * immediately swapped out again after swapon.
  454. */
  455. activate_page(page);
  456. }
  457. static int unuse_pte_range(struct vm_area_struct *vma, pmd_t *pmd,
  458. unsigned long addr, unsigned long end,
  459. swp_entry_t entry, struct page *page)
  460. {
  461. pte_t swp_pte = swp_entry_to_pte(entry);
  462. pte_t *pte;
  463. spinlock_t *ptl;
  464. int found = 0;
  465. pte = pte_offset_map_lock(vma->vm_mm, pmd, addr, &ptl);
  466. do {
  467. /*
  468. * swapoff spends a _lot_ of time in this loop!
  469. * Test inline before going to call unuse_pte.
  470. */
  471. if (unlikely(pte_same(*pte, swp_pte))) {
  472. unuse_pte(vma, pte++, addr, entry, page);
  473. found = 1;
  474. break;
  475. }
  476. } while (pte++, addr += PAGE_SIZE, addr != end);
  477. pte_unmap_unlock(pte - 1, ptl);
  478. return found;
  479. }
  480. static inline int unuse_pmd_range(struct vm_area_struct *vma, pud_t *pud,
  481. unsigned long addr, unsigned long end,
  482. swp_entry_t entry, struct page *page)
  483. {
  484. pmd_t *pmd;
  485. unsigned long next;
  486. pmd = pmd_offset(pud, addr);
  487. do {
  488. next = pmd_addr_end(addr, end);
  489. if (pmd_none_or_clear_bad(pmd))
  490. continue;
  491. if (unuse_pte_range(vma, pmd, addr, next, entry, page))
  492. return 1;
  493. } while (pmd++, addr = next, addr != end);
  494. return 0;
  495. }
  496. static inline int unuse_pud_range(struct vm_area_struct *vma, pgd_t *pgd,
  497. unsigned long addr, unsigned long end,
  498. swp_entry_t entry, struct page *page)
  499. {
  500. pud_t *pud;
  501. unsigned long next;
  502. pud = pud_offset(pgd, addr);
  503. do {
  504. next = pud_addr_end(addr, end);
  505. if (pud_none_or_clear_bad(pud))
  506. continue;
  507. if (unuse_pmd_range(vma, pud, addr, next, entry, page))
  508. return 1;
  509. } while (pud++, addr = next, addr != end);
  510. return 0;
  511. }
  512. static int unuse_vma(struct vm_area_struct *vma,
  513. swp_entry_t entry, struct page *page)
  514. {
  515. pgd_t *pgd;
  516. unsigned long addr, end, next;
  517. if (page->mapping) {
  518. addr = page_address_in_vma(page, vma);
  519. if (addr == -EFAULT)
  520. return 0;
  521. else
  522. end = addr + PAGE_SIZE;
  523. } else {
  524. addr = vma->vm_start;
  525. end = vma->vm_end;
  526. }
  527. pgd = pgd_offset(vma->vm_mm, addr);
  528. do {
  529. next = pgd_addr_end(addr, end);
  530. if (pgd_none_or_clear_bad(pgd))
  531. continue;
  532. if (unuse_pud_range(vma, pgd, addr, next, entry, page))
  533. return 1;
  534. } while (pgd++, addr = next, addr != end);
  535. return 0;
  536. }
  537. static int unuse_mm(struct mm_struct *mm,
  538. swp_entry_t entry, struct page *page)
  539. {
  540. struct vm_area_struct *vma;
  541. if (!down_read_trylock(&mm->mmap_sem)) {
  542. /*
  543. * Activate page so shrink_cache is unlikely to unmap its
  544. * ptes while lock is dropped, so swapoff can make progress.
  545. */
  546. activate_page(page);
  547. unlock_page(page);
  548. down_read(&mm->mmap_sem);
  549. lock_page(page);
  550. }
  551. for (vma = mm->mmap; vma; vma = vma->vm_next) {
  552. if (vma->anon_vma && unuse_vma(vma, entry, page))
  553. break;
  554. }
  555. up_read(&mm->mmap_sem);
  556. /*
  557. * Currently unuse_mm cannot fail, but leave error handling
  558. * at call sites for now, since we change it from time to time.
  559. */
  560. return 0;
  561. }
  562. /*
  563. * Scan swap_map from current position to next entry still in use.
  564. * Recycle to start on reaching the end, returning 0 when empty.
  565. */
  566. static unsigned int find_next_to_unuse(struct swap_info_struct *si,
  567. unsigned int prev)
  568. {
  569. unsigned int max = si->max;
  570. unsigned int i = prev;
  571. int count;
  572. /*
  573. * No need for swap_lock here: we're just looking
  574. * for whether an entry is in use, not modifying it; false
  575. * hits are okay, and sys_swapoff() has already prevented new
  576. * allocations from this area (while holding swap_lock).
  577. */
  578. for (;;) {
  579. if (++i >= max) {
  580. if (!prev) {
  581. i = 0;
  582. break;
  583. }
  584. /*
  585. * No entries in use at top of swap_map,
  586. * loop back to start and recheck there.
  587. */
  588. max = prev + 1;
  589. prev = 0;
  590. i = 1;
  591. }
  592. count = si->swap_map[i];
  593. if (count && count != SWAP_MAP_BAD)
  594. break;
  595. }
  596. return i;
  597. }
  598. /*
  599. * We completely avoid races by reading each swap page in advance,
  600. * and then search for the process using it. All the necessary
  601. * page table adjustments can then be made atomically.
  602. */
  603. static int try_to_unuse(unsigned int type)
  604. {
  605. struct swap_info_struct * si = &swap_info[type];
  606. struct mm_struct *start_mm;
  607. unsigned short *swap_map;
  608. unsigned short swcount;
  609. struct page *page;
  610. swp_entry_t entry;
  611. unsigned int i = 0;
  612. int retval = 0;
  613. int reset_overflow = 0;
  614. int shmem;
  615. /*
  616. * When searching mms for an entry, a good strategy is to
  617. * start at the first mm we freed the previous entry from
  618. * (though actually we don't notice whether we or coincidence
  619. * freed the entry). Initialize this start_mm with a hold.
  620. *
  621. * A simpler strategy would be to start at the last mm we
  622. * freed the previous entry from; but that would take less
  623. * advantage of mmlist ordering, which clusters forked mms
  624. * together, child after parent. If we race with dup_mmap(), we
  625. * prefer to resolve parent before child, lest we miss entries
  626. * duplicated after we scanned child: using last mm would invert
  627. * that. Though it's only a serious concern when an overflowed
  628. * swap count is reset from SWAP_MAP_MAX, preventing a rescan.
  629. */
  630. start_mm = &init_mm;
  631. atomic_inc(&init_mm.mm_users);
  632. /*
  633. * Keep on scanning until all entries have gone. Usually,
  634. * one pass through swap_map is enough, but not necessarily:
  635. * there are races when an instance of an entry might be missed.
  636. */
  637. while ((i = find_next_to_unuse(si, i)) != 0) {
  638. if (signal_pending(current)) {
  639. retval = -EINTR;
  640. break;
  641. }
  642. /*
  643. * Get a page for the entry, using the existing swap
  644. * cache page if there is one. Otherwise, get a clean
  645. * page and read the swap into it.
  646. */
  647. swap_map = &si->swap_map[i];
  648. entry = swp_entry(type, i);
  649. page = read_swap_cache_async(entry, NULL, 0);
  650. if (!page) {
  651. /*
  652. * Either swap_duplicate() failed because entry
  653. * has been freed independently, and will not be
  654. * reused since sys_swapoff() already disabled
  655. * allocation from here, or alloc_page() failed.
  656. */
  657. if (!*swap_map)
  658. continue;
  659. retval = -ENOMEM;
  660. break;
  661. }
  662. /*
  663. * Don't hold on to start_mm if it looks like exiting.
  664. */
  665. if (atomic_read(&start_mm->mm_users) == 1) {
  666. mmput(start_mm);
  667. start_mm = &init_mm;
  668. atomic_inc(&init_mm.mm_users);
  669. }
  670. /*
  671. * Wait for and lock page. When do_swap_page races with
  672. * try_to_unuse, do_swap_page can handle the fault much
  673. * faster than try_to_unuse can locate the entry. This
  674. * apparently redundant "wait_on_page_locked" lets try_to_unuse
  675. * defer to do_swap_page in such a case - in some tests,
  676. * do_swap_page and try_to_unuse repeatedly compete.
  677. */
  678. wait_on_page_locked(page);
  679. wait_on_page_writeback(page);
  680. lock_page(page);
  681. wait_on_page_writeback(page);
  682. /*
  683. * Remove all references to entry.
  684. * Whenever we reach init_mm, there's no address space
  685. * to search, but use it as a reminder to search shmem.
  686. */
  687. shmem = 0;
  688. swcount = *swap_map;
  689. if (swcount > 1) {
  690. if (start_mm == &init_mm)
  691. shmem = shmem_unuse(entry, page);
  692. else
  693. retval = unuse_mm(start_mm, entry, page);
  694. }
  695. if (*swap_map > 1) {
  696. int set_start_mm = (*swap_map >= swcount);
  697. struct list_head *p = &start_mm->mmlist;
  698. struct mm_struct *new_start_mm = start_mm;
  699. struct mm_struct *prev_mm = start_mm;
  700. struct mm_struct *mm;
  701. atomic_inc(&new_start_mm->mm_users);
  702. atomic_inc(&prev_mm->mm_users);
  703. spin_lock(&mmlist_lock);
  704. while (*swap_map > 1 && !retval &&
  705. (p = p->next) != &start_mm->mmlist) {
  706. mm = list_entry(p, struct mm_struct, mmlist);
  707. if (!atomic_inc_not_zero(&mm->mm_users))
  708. continue;
  709. spin_unlock(&mmlist_lock);
  710. mmput(prev_mm);
  711. prev_mm = mm;
  712. cond_resched();
  713. swcount = *swap_map;
  714. if (swcount <= 1)
  715. ;
  716. else if (mm == &init_mm) {
  717. set_start_mm = 1;
  718. shmem = shmem_unuse(entry, page);
  719. } else
  720. retval = unuse_mm(mm, entry, page);
  721. if (set_start_mm && *swap_map < swcount) {
  722. mmput(new_start_mm);
  723. atomic_inc(&mm->mm_users);
  724. new_start_mm = mm;
  725. set_start_mm = 0;
  726. }
  727. spin_lock(&mmlist_lock);
  728. }
  729. spin_unlock(&mmlist_lock);
  730. mmput(prev_mm);
  731. mmput(start_mm);
  732. start_mm = new_start_mm;
  733. }
  734. if (retval) {
  735. unlock_page(page);
  736. page_cache_release(page);
  737. break;
  738. }
  739. /*
  740. * How could swap count reach 0x7fff when the maximum
  741. * pid is 0x7fff, and there's no way to repeat a swap
  742. * page within an mm (except in shmem, where it's the
  743. * shared object which takes the reference count)?
  744. * We believe SWAP_MAP_MAX cannot occur in Linux 2.4.
  745. *
  746. * If that's wrong, then we should worry more about
  747. * exit_mmap() and do_munmap() cases described above:
  748. * we might be resetting SWAP_MAP_MAX too early here.
  749. * We know "Undead"s can happen, they're okay, so don't
  750. * report them; but do report if we reset SWAP_MAP_MAX.
  751. */
  752. if (*swap_map == SWAP_MAP_MAX) {
  753. spin_lock(&swap_lock);
  754. *swap_map = 1;
  755. spin_unlock(&swap_lock);
  756. reset_overflow = 1;
  757. }
  758. /*
  759. * If a reference remains (rare), we would like to leave
  760. * the page in the swap cache; but try_to_unmap could
  761. * then re-duplicate the entry once we drop page lock,
  762. * so we might loop indefinitely; also, that page could
  763. * not be swapped out to other storage meanwhile. So:
  764. * delete from cache even if there's another reference,
  765. * after ensuring that the data has been saved to disk -
  766. * since if the reference remains (rarer), it will be
  767. * read from disk into another page. Splitting into two
  768. * pages would be incorrect if swap supported "shared
  769. * private" pages, but they are handled by tmpfs files.
  770. *
  771. * Note shmem_unuse already deleted a swappage from
  772. * the swap cache, unless the move to filepage failed:
  773. * in which case it left swappage in cache, lowered its
  774. * swap count to pass quickly through the loops above,
  775. * and now we must reincrement count to try again later.
  776. */
  777. if ((*swap_map > 1) && PageDirty(page) && PageSwapCache(page)) {
  778. struct writeback_control wbc = {
  779. .sync_mode = WB_SYNC_NONE,
  780. };
  781. swap_writepage(page, &wbc);
  782. lock_page(page);
  783. wait_on_page_writeback(page);
  784. }
  785. if (PageSwapCache(page)) {
  786. if (shmem)
  787. swap_duplicate(entry);
  788. else
  789. delete_from_swap_cache(page);
  790. }
  791. /*
  792. * So we could skip searching mms once swap count went
  793. * to 1, we did not mark any present ptes as dirty: must
  794. * mark page dirty so shrink_list will preserve it.
  795. */
  796. SetPageDirty(page);
  797. unlock_page(page);
  798. page_cache_release(page);
  799. /*
  800. * Make sure that we aren't completely killing
  801. * interactive performance.
  802. */
  803. cond_resched();
  804. }
  805. mmput(start_mm);
  806. if (reset_overflow) {
  807. printk(KERN_WARNING "swapoff: cleared swap entry overflow\n");
  808. swap_overflow = 0;
  809. }
  810. return retval;
  811. }
  812. /*
  813. * After a successful try_to_unuse, if no swap is now in use, we know
  814. * we can empty the mmlist. swap_lock must be held on entry and exit.
  815. * Note that mmlist_lock nests inside swap_lock, and an mm must be
  816. * added to the mmlist just after page_duplicate - before would be racy.
  817. */
  818. static void drain_mmlist(void)
  819. {
  820. struct list_head *p, *next;
  821. unsigned int i;
  822. for (i = 0; i < nr_swapfiles; i++)
  823. if (swap_info[i].inuse_pages)
  824. return;
  825. spin_lock(&mmlist_lock);
  826. list_for_each_safe(p, next, &init_mm.mmlist)
  827. list_del_init(p);
  828. spin_unlock(&mmlist_lock);
  829. }
  830. /*
  831. * Use this swapdev's extent info to locate the (PAGE_SIZE) block which
  832. * corresponds to page offset `offset'.
  833. */
  834. sector_t map_swap_page(struct swap_info_struct *sis, pgoff_t offset)
  835. {
  836. struct swap_extent *se = sis->curr_swap_extent;
  837. struct swap_extent *start_se = se;
  838. for ( ; ; ) {
  839. struct list_head *lh;
  840. if (se->start_page <= offset &&
  841. offset < (se->start_page + se->nr_pages)) {
  842. return se->start_block + (offset - se->start_page);
  843. }
  844. lh = se->list.next;
  845. if (lh == &sis->extent_list)
  846. lh = lh->next;
  847. se = list_entry(lh, struct swap_extent, list);
  848. sis->curr_swap_extent = se;
  849. BUG_ON(se == start_se); /* It *must* be present */
  850. }
  851. }
  852. /*
  853. * Free all of a swapdev's extent information
  854. */
  855. static void destroy_swap_extents(struct swap_info_struct *sis)
  856. {
  857. while (!list_empty(&sis->extent_list)) {
  858. struct swap_extent *se;
  859. se = list_entry(sis->extent_list.next,
  860. struct swap_extent, list);
  861. list_del(&se->list);
  862. kfree(se);
  863. }
  864. }
  865. /*
  866. * Add a block range (and the corresponding page range) into this swapdev's
  867. * extent list. The extent list is kept sorted in page order.
  868. *
  869. * This function rather assumes that it is called in ascending page order.
  870. */
  871. static int
  872. add_swap_extent(struct swap_info_struct *sis, unsigned long start_page,
  873. unsigned long nr_pages, sector_t start_block)
  874. {
  875. struct swap_extent *se;
  876. struct swap_extent *new_se;
  877. struct list_head *lh;
  878. lh = sis->extent_list.prev; /* The highest page extent */
  879. if (lh != &sis->extent_list) {
  880. se = list_entry(lh, struct swap_extent, list);
  881. BUG_ON(se->start_page + se->nr_pages != start_page);
  882. if (se->start_block + se->nr_pages == start_block) {
  883. /* Merge it */
  884. se->nr_pages += nr_pages;
  885. return 0;
  886. }
  887. }
  888. /*
  889. * No merge. Insert a new extent, preserving ordering.
  890. */
  891. new_se = kmalloc(sizeof(*se), GFP_KERNEL);
  892. if (new_se == NULL)
  893. return -ENOMEM;
  894. new_se->start_page = start_page;
  895. new_se->nr_pages = nr_pages;
  896. new_se->start_block = start_block;
  897. list_add_tail(&new_se->list, &sis->extent_list);
  898. return 1;
  899. }
  900. /*
  901. * A `swap extent' is a simple thing which maps a contiguous range of pages
  902. * onto a contiguous range of disk blocks. An ordered list of swap extents
  903. * is built at swapon time and is then used at swap_writepage/swap_readpage
  904. * time for locating where on disk a page belongs.
  905. *
  906. * If the swapfile is an S_ISBLK block device, a single extent is installed.
  907. * This is done so that the main operating code can treat S_ISBLK and S_ISREG
  908. * swap files identically.
  909. *
  910. * Whether the swapdev is an S_ISREG file or an S_ISBLK blockdev, the swap
  911. * extent list operates in PAGE_SIZE disk blocks. Both S_ISREG and S_ISBLK
  912. * swapfiles are handled *identically* after swapon time.
  913. *
  914. * For S_ISREG swapfiles, setup_swap_extents() will walk all the file's blocks
  915. * and will parse them into an ordered extent list, in PAGE_SIZE chunks. If
  916. * some stray blocks are found which do not fall within the PAGE_SIZE alignment
  917. * requirements, they are simply tossed out - we will never use those blocks
  918. * for swapping.
  919. *
  920. * For S_ISREG swapfiles we set S_SWAPFILE across the life of the swapon. This
  921. * prevents root from shooting her foot off by ftruncating an in-use swapfile,
  922. * which will scribble on the fs.
  923. *
  924. * The amount of disk space which a single swap extent represents varies.
  925. * Typically it is in the 1-4 megabyte range. So we can have hundreds of
  926. * extents in the list. To avoid much list walking, we cache the previous
  927. * search location in `curr_swap_extent', and start new searches from there.
  928. * This is extremely effective. The average number of iterations in
  929. * map_swap_page() has been measured at about 0.3 per page. - akpm.
  930. */
  931. static int setup_swap_extents(struct swap_info_struct *sis, sector_t *span)
  932. {
  933. struct inode *inode;
  934. unsigned blocks_per_page;
  935. unsigned long page_no;
  936. unsigned blkbits;
  937. sector_t probe_block;
  938. sector_t last_block;
  939. sector_t lowest_block = -1;
  940. sector_t highest_block = 0;
  941. int nr_extents = 0;
  942. int ret;
  943. inode = sis->swap_file->f_mapping->host;
  944. if (S_ISBLK(inode->i_mode)) {
  945. ret = add_swap_extent(sis, 0, sis->max, 0);
  946. *span = sis->pages;
  947. goto done;
  948. }
  949. blkbits = inode->i_blkbits;
  950. blocks_per_page = PAGE_SIZE >> blkbits;
  951. /*
  952. * Map all the blocks into the extent list. This code doesn't try
  953. * to be very smart.
  954. */
  955. probe_block = 0;
  956. page_no = 0;
  957. last_block = i_size_read(inode) >> blkbits;
  958. while ((probe_block + blocks_per_page) <= last_block &&
  959. page_no < sis->max) {
  960. unsigned block_in_page;
  961. sector_t first_block;
  962. first_block = bmap(inode, probe_block);
  963. if (first_block == 0)
  964. goto bad_bmap;
  965. /*
  966. * It must be PAGE_SIZE aligned on-disk
  967. */
  968. if (first_block & (blocks_per_page - 1)) {
  969. probe_block++;
  970. goto reprobe;
  971. }
  972. for (block_in_page = 1; block_in_page < blocks_per_page;
  973. block_in_page++) {
  974. sector_t block;
  975. block = bmap(inode, probe_block + block_in_page);
  976. if (block == 0)
  977. goto bad_bmap;
  978. if (block != first_block + block_in_page) {
  979. /* Discontiguity */
  980. probe_block++;
  981. goto reprobe;
  982. }
  983. }
  984. first_block >>= (PAGE_SHIFT - blkbits);
  985. if (page_no) { /* exclude the header page */
  986. if (first_block < lowest_block)
  987. lowest_block = first_block;
  988. if (first_block > highest_block)
  989. highest_block = first_block;
  990. }
  991. /*
  992. * We found a PAGE_SIZE-length, PAGE_SIZE-aligned run of blocks
  993. */
  994. ret = add_swap_extent(sis, page_no, 1, first_block);
  995. if (ret < 0)
  996. goto out;
  997. nr_extents += ret;
  998. page_no++;
  999. probe_block += blocks_per_page;
  1000. reprobe:
  1001. continue;
  1002. }
  1003. ret = nr_extents;
  1004. *span = 1 + highest_block - lowest_block;
  1005. if (page_no == 0)
  1006. page_no = 1; /* force Empty message */
  1007. sis->max = page_no;
  1008. sis->pages = page_no - 1;
  1009. sis->highest_bit = page_no - 1;
  1010. done:
  1011. sis->curr_swap_extent = list_entry(sis->extent_list.prev,
  1012. struct swap_extent, list);
  1013. goto out;
  1014. bad_bmap:
  1015. printk(KERN_ERR "swapon: swapfile has holes\n");
  1016. ret = -EINVAL;
  1017. out:
  1018. return ret;
  1019. }
  1020. #if 0 /* We don't need this yet */
  1021. #include <linux/backing-dev.h>
  1022. int page_queue_congested(struct page *page)
  1023. {
  1024. struct backing_dev_info *bdi;
  1025. BUG_ON(!PageLocked(page)); /* It pins the swap_info_struct */
  1026. if (PageSwapCache(page)) {
  1027. swp_entry_t entry = { .val = page_private(page) };
  1028. struct swap_info_struct *sis;
  1029. sis = get_swap_info_struct(swp_type(entry));
  1030. bdi = sis->bdev->bd_inode->i_mapping->backing_dev_info;
  1031. } else
  1032. bdi = page->mapping->backing_dev_info;
  1033. return bdi_write_congested(bdi);
  1034. }
  1035. #endif
  1036. asmlinkage long sys_swapoff(const char __user * specialfile)
  1037. {
  1038. struct swap_info_struct * p = NULL;
  1039. unsigned short *swap_map;
  1040. struct file *swap_file, *victim;
  1041. struct address_space *mapping;
  1042. struct inode *inode;
  1043. char * pathname;
  1044. int i, type, prev;
  1045. int err;
  1046. if (!capable(CAP_SYS_ADMIN))
  1047. return -EPERM;
  1048. pathname = getname(specialfile);
  1049. err = PTR_ERR(pathname);
  1050. if (IS_ERR(pathname))
  1051. goto out;
  1052. victim = filp_open(pathname, O_RDWR|O_LARGEFILE, 0);
  1053. putname(pathname);
  1054. err = PTR_ERR(victim);
  1055. if (IS_ERR(victim))
  1056. goto out;
  1057. mapping = victim->f_mapping;
  1058. prev = -1;
  1059. spin_lock(&swap_lock);
  1060. for (type = swap_list.head; type >= 0; type = swap_info[type].next) {
  1061. p = swap_info + type;
  1062. if ((p->flags & SWP_ACTIVE) == SWP_ACTIVE) {
  1063. if (p->swap_file->f_mapping == mapping)
  1064. break;
  1065. }
  1066. prev = type;
  1067. }
  1068. if (type < 0) {
  1069. err = -EINVAL;
  1070. spin_unlock(&swap_lock);
  1071. goto out_dput;
  1072. }
  1073. if (!security_vm_enough_memory(p->pages))
  1074. vm_unacct_memory(p->pages);
  1075. else {
  1076. err = -ENOMEM;
  1077. spin_unlock(&swap_lock);
  1078. goto out_dput;
  1079. }
  1080. if (prev < 0) {
  1081. swap_list.head = p->next;
  1082. } else {
  1083. swap_info[prev].next = p->next;
  1084. }
  1085. if (type == swap_list.next) {
  1086. /* just pick something that's safe... */
  1087. swap_list.next = swap_list.head;
  1088. }
  1089. nr_swap_pages -= p->pages;
  1090. total_swap_pages -= p->pages;
  1091. p->flags &= ~SWP_WRITEOK;
  1092. spin_unlock(&swap_lock);
  1093. current->flags |= PF_SWAPOFF;
  1094. err = try_to_unuse(type);
  1095. current->flags &= ~PF_SWAPOFF;
  1096. if (err) {
  1097. /* re-insert swap space back into swap_list */
  1098. spin_lock(&swap_lock);
  1099. for (prev = -1, i = swap_list.head; i >= 0; prev = i, i = swap_info[i].next)
  1100. if (p->prio >= swap_info[i].prio)
  1101. break;
  1102. p->next = i;
  1103. if (prev < 0)
  1104. swap_list.head = swap_list.next = p - swap_info;
  1105. else
  1106. swap_info[prev].next = p - swap_info;
  1107. nr_swap_pages += p->pages;
  1108. total_swap_pages += p->pages;
  1109. p->flags |= SWP_WRITEOK;
  1110. spin_unlock(&swap_lock);
  1111. goto out_dput;
  1112. }
  1113. /* wait for any unplug function to finish */
  1114. down_write(&swap_unplug_sem);
  1115. up_write(&swap_unplug_sem);
  1116. destroy_swap_extents(p);
  1117. mutex_lock(&swapon_mutex);
  1118. spin_lock(&swap_lock);
  1119. drain_mmlist();
  1120. /* wait for anyone still in scan_swap_map */
  1121. p->highest_bit = 0; /* cuts scans short */
  1122. while (p->flags >= SWP_SCANNING) {
  1123. spin_unlock(&swap_lock);
  1124. schedule_timeout_uninterruptible(1);
  1125. spin_lock(&swap_lock);
  1126. }
  1127. swap_file = p->swap_file;
  1128. p->swap_file = NULL;
  1129. p->max = 0;
  1130. swap_map = p->swap_map;
  1131. p->swap_map = NULL;
  1132. p->flags = 0;
  1133. spin_unlock(&swap_lock);
  1134. mutex_unlock(&swapon_mutex);
  1135. vfree(swap_map);
  1136. inode = mapping->host;
  1137. if (S_ISBLK(inode->i_mode)) {
  1138. struct block_device *bdev = I_BDEV(inode);
  1139. set_blocksize(bdev, p->old_block_size);
  1140. bd_release(bdev);
  1141. } else {
  1142. mutex_lock(&inode->i_mutex);
  1143. inode->i_flags &= ~S_SWAPFILE;
  1144. mutex_unlock(&inode->i_mutex);
  1145. }
  1146. filp_close(swap_file, NULL);
  1147. err = 0;
  1148. out_dput:
  1149. filp_close(victim, NULL);
  1150. out:
  1151. return err;
  1152. }
  1153. #ifdef CONFIG_PROC_FS
  1154. /* iterator */
  1155. static void *swap_start(struct seq_file *swap, loff_t *pos)
  1156. {
  1157. struct swap_info_struct *ptr = swap_info;
  1158. int i;
  1159. loff_t l = *pos;
  1160. mutex_lock(&swapon_mutex);
  1161. if (!l)
  1162. return SEQ_START_TOKEN;
  1163. for (i = 0; i < nr_swapfiles; i++, ptr++) {
  1164. if (!(ptr->flags & SWP_USED) || !ptr->swap_map)
  1165. continue;
  1166. if (!--l)
  1167. return ptr;
  1168. }
  1169. return NULL;
  1170. }
  1171. static void *swap_next(struct seq_file *swap, void *v, loff_t *pos)
  1172. {
  1173. struct swap_info_struct *ptr;
  1174. struct swap_info_struct *endptr = swap_info + nr_swapfiles;
  1175. if (v == SEQ_START_TOKEN)
  1176. ptr = swap_info;
  1177. else {
  1178. ptr = v;
  1179. ptr++;
  1180. }
  1181. for (; ptr < endptr; ptr++) {
  1182. if (!(ptr->flags & SWP_USED) || !ptr->swap_map)
  1183. continue;
  1184. ++*pos;
  1185. return ptr;
  1186. }
  1187. return NULL;
  1188. }
  1189. static void swap_stop(struct seq_file *swap, void *v)
  1190. {
  1191. mutex_unlock(&swapon_mutex);
  1192. }
  1193. static int swap_show(struct seq_file *swap, void *v)
  1194. {
  1195. struct swap_info_struct *ptr = v;
  1196. struct file *file;
  1197. int len;
  1198. if (ptr == SEQ_START_TOKEN) {
  1199. seq_puts(swap,"Filename\t\t\t\tType\t\tSize\tUsed\tPriority\n");
  1200. return 0;
  1201. }
  1202. file = ptr->swap_file;
  1203. len = seq_path(swap, file->f_vfsmnt, file->f_dentry, " \t\n\\");
  1204. seq_printf(swap, "%*s%s\t%u\t%u\t%d\n",
  1205. len < 40 ? 40 - len : 1, " ",
  1206. S_ISBLK(file->f_dentry->d_inode->i_mode) ?
  1207. "partition" : "file\t",
  1208. ptr->pages << (PAGE_SHIFT - 10),
  1209. ptr->inuse_pages << (PAGE_SHIFT - 10),
  1210. ptr->prio);
  1211. return 0;
  1212. }
  1213. static struct seq_operations swaps_op = {
  1214. .start = swap_start,
  1215. .next = swap_next,
  1216. .stop = swap_stop,
  1217. .show = swap_show
  1218. };
  1219. static int swaps_open(struct inode *inode, struct file *file)
  1220. {
  1221. return seq_open(file, &swaps_op);
  1222. }
  1223. static struct file_operations proc_swaps_operations = {
  1224. .open = swaps_open,
  1225. .read = seq_read,
  1226. .llseek = seq_lseek,
  1227. .release = seq_release,
  1228. };
  1229. static int __init procswaps_init(void)
  1230. {
  1231. struct proc_dir_entry *entry;
  1232. entry = create_proc_entry("swaps", 0, NULL);
  1233. if (entry)
  1234. entry->proc_fops = &proc_swaps_operations;
  1235. return 0;
  1236. }
  1237. __initcall(procswaps_init);
  1238. #endif /* CONFIG_PROC_FS */
  1239. /*
  1240. * Written 01/25/92 by Simmule Turner, heavily changed by Linus.
  1241. *
  1242. * The swapon system call
  1243. */
  1244. asmlinkage long sys_swapon(const char __user * specialfile, int swap_flags)
  1245. {
  1246. struct swap_info_struct * p;
  1247. char *name = NULL;
  1248. struct block_device *bdev = NULL;
  1249. struct file *swap_file = NULL;
  1250. struct address_space *mapping;
  1251. unsigned int type;
  1252. int i, prev;
  1253. int error;
  1254. static int least_priority;
  1255. union swap_header *swap_header = NULL;
  1256. int swap_header_version;
  1257. unsigned int nr_good_pages = 0;
  1258. int nr_extents = 0;
  1259. sector_t span;
  1260. unsigned long maxpages = 1;
  1261. int swapfilesize;
  1262. unsigned short *swap_map;
  1263. struct page *page = NULL;
  1264. struct inode *inode = NULL;
  1265. int did_down = 0;
  1266. if (!capable(CAP_SYS_ADMIN))
  1267. return -EPERM;
  1268. spin_lock(&swap_lock);
  1269. p = swap_info;
  1270. for (type = 0 ; type < nr_swapfiles ; type++,p++)
  1271. if (!(p->flags & SWP_USED))
  1272. break;
  1273. error = -EPERM;
  1274. if (type >= MAX_SWAPFILES) {
  1275. spin_unlock(&swap_lock);
  1276. goto out;
  1277. }
  1278. if (type >= nr_swapfiles)
  1279. nr_swapfiles = type+1;
  1280. INIT_LIST_HEAD(&p->extent_list);
  1281. p->flags = SWP_USED;
  1282. p->swap_file = NULL;
  1283. p->old_block_size = 0;
  1284. p->swap_map = NULL;
  1285. p->lowest_bit = 0;
  1286. p->highest_bit = 0;
  1287. p->cluster_nr = 0;
  1288. p->inuse_pages = 0;
  1289. p->next = -1;
  1290. if (swap_flags & SWAP_FLAG_PREFER) {
  1291. p->prio =
  1292. (swap_flags & SWAP_FLAG_PRIO_MASK)>>SWAP_FLAG_PRIO_SHIFT;
  1293. } else {
  1294. p->prio = --least_priority;
  1295. }
  1296. spin_unlock(&swap_lock);
  1297. name = getname(specialfile);
  1298. error = PTR_ERR(name);
  1299. if (IS_ERR(name)) {
  1300. name = NULL;
  1301. goto bad_swap_2;
  1302. }
  1303. swap_file = filp_open(name, O_RDWR|O_LARGEFILE, 0);
  1304. error = PTR_ERR(swap_file);
  1305. if (IS_ERR(swap_file)) {
  1306. swap_file = NULL;
  1307. goto bad_swap_2;
  1308. }
  1309. p->swap_file = swap_file;
  1310. mapping = swap_file->f_mapping;
  1311. inode = mapping->host;
  1312. error = -EBUSY;
  1313. for (i = 0; i < nr_swapfiles; i++) {
  1314. struct swap_info_struct *q = &swap_info[i];
  1315. if (i == type || !q->swap_file)
  1316. continue;
  1317. if (mapping == q->swap_file->f_mapping)
  1318. goto bad_swap;
  1319. }
  1320. error = -EINVAL;
  1321. if (S_ISBLK(inode->i_mode)) {
  1322. bdev = I_BDEV(inode);
  1323. error = bd_claim(bdev, sys_swapon);
  1324. if (error < 0) {
  1325. bdev = NULL;
  1326. error = -EINVAL;
  1327. goto bad_swap;
  1328. }
  1329. p->old_block_size = block_size(bdev);
  1330. error = set_blocksize(bdev, PAGE_SIZE);
  1331. if (error < 0)
  1332. goto bad_swap;
  1333. p->bdev = bdev;
  1334. } else if (S_ISREG(inode->i_mode)) {
  1335. p->bdev = inode->i_sb->s_bdev;
  1336. mutex_lock(&inode->i_mutex);
  1337. did_down = 1;
  1338. if (IS_SWAPFILE(inode)) {
  1339. error = -EBUSY;
  1340. goto bad_swap;
  1341. }
  1342. } else {
  1343. goto bad_swap;
  1344. }
  1345. swapfilesize = i_size_read(inode) >> PAGE_SHIFT;
  1346. /*
  1347. * Read the swap header.
  1348. */
  1349. if (!mapping->a_ops->readpage) {
  1350. error = -EINVAL;
  1351. goto bad_swap;
  1352. }
  1353. page = read_mapping_page(mapping, 0, swap_file);
  1354. if (IS_ERR(page)) {
  1355. error = PTR_ERR(page);
  1356. goto bad_swap;
  1357. }
  1358. wait_on_page_locked(page);
  1359. if (!PageUptodate(page))
  1360. goto bad_swap;
  1361. kmap(page);
  1362. swap_header = page_address(page);
  1363. if (!memcmp("SWAP-SPACE",swap_header->magic.magic,10))
  1364. swap_header_version = 1;
  1365. else if (!memcmp("SWAPSPACE2",swap_header->magic.magic,10))
  1366. swap_header_version = 2;
  1367. else {
  1368. printk(KERN_ERR "Unable to find swap-space signature\n");
  1369. error = -EINVAL;
  1370. goto bad_swap;
  1371. }
  1372. switch (swap_header_version) {
  1373. case 1:
  1374. printk(KERN_ERR "version 0 swap is no longer supported. "
  1375. "Use mkswap -v1 %s\n", name);
  1376. error = -EINVAL;
  1377. goto bad_swap;
  1378. case 2:
  1379. /* Check the swap header's sub-version and the size of
  1380. the swap file and bad block lists */
  1381. if (swap_header->info.version != 1) {
  1382. printk(KERN_WARNING
  1383. "Unable to handle swap header version %d\n",
  1384. swap_header->info.version);
  1385. error = -EINVAL;
  1386. goto bad_swap;
  1387. }
  1388. p->lowest_bit = 1;
  1389. p->cluster_next = 1;
  1390. /*
  1391. * Find out how many pages are allowed for a single swap
  1392. * device. There are two limiting factors: 1) the number of
  1393. * bits for the swap offset in the swp_entry_t type and
  1394. * 2) the number of bits in the a swap pte as defined by
  1395. * the different architectures. In order to find the
  1396. * largest possible bit mask a swap entry with swap type 0
  1397. * and swap offset ~0UL is created, encoded to a swap pte,
  1398. * decoded to a swp_entry_t again and finally the swap
  1399. * offset is extracted. This will mask all the bits from
  1400. * the initial ~0UL mask that can't be encoded in either
  1401. * the swp_entry_t or the architecture definition of a
  1402. * swap pte.
  1403. */
  1404. maxpages = swp_offset(pte_to_swp_entry(swp_entry_to_pte(swp_entry(0,~0UL)))) - 1;
  1405. if (maxpages > swap_header->info.last_page)
  1406. maxpages = swap_header->info.last_page;
  1407. p->highest_bit = maxpages - 1;
  1408. error = -EINVAL;
  1409. if (!maxpages)
  1410. goto bad_swap;
  1411. if (swapfilesize && maxpages > swapfilesize) {
  1412. printk(KERN_WARNING
  1413. "Swap area shorter than signature indicates\n");
  1414. goto bad_swap;
  1415. }
  1416. if (swap_header->info.nr_badpages && S_ISREG(inode->i_mode))
  1417. goto bad_swap;
  1418. if (swap_header->info.nr_badpages > MAX_SWAP_BADPAGES)
  1419. goto bad_swap;
  1420. /* OK, set up the swap map and apply the bad block list */
  1421. if (!(p->swap_map = vmalloc(maxpages * sizeof(short)))) {
  1422. error = -ENOMEM;
  1423. goto bad_swap;
  1424. }
  1425. error = 0;
  1426. memset(p->swap_map, 0, maxpages * sizeof(short));
  1427. for (i = 0; i < swap_header->info.nr_badpages; i++) {
  1428. int page_nr = swap_header->info.badpages[i];
  1429. if (page_nr <= 0 || page_nr >= swap_header->info.last_page)
  1430. error = -EINVAL;
  1431. else
  1432. p->swap_map[page_nr] = SWAP_MAP_BAD;
  1433. }
  1434. nr_good_pages = swap_header->info.last_page -
  1435. swap_header->info.nr_badpages -
  1436. 1 /* header page */;
  1437. if (error)
  1438. goto bad_swap;
  1439. }
  1440. if (nr_good_pages) {
  1441. p->swap_map[0] = SWAP_MAP_BAD;
  1442. p->max = maxpages;
  1443. p->pages = nr_good_pages;
  1444. nr_extents = setup_swap_extents(p, &span);
  1445. if (nr_extents < 0) {
  1446. error = nr_extents;
  1447. goto bad_swap;
  1448. }
  1449. nr_good_pages = p->pages;
  1450. }
  1451. if (!nr_good_pages) {
  1452. printk(KERN_WARNING "Empty swap-file\n");
  1453. error = -EINVAL;
  1454. goto bad_swap;
  1455. }
  1456. mutex_lock(&swapon_mutex);
  1457. spin_lock(&swap_lock);
  1458. p->flags = SWP_ACTIVE;
  1459. nr_swap_pages += nr_good_pages;
  1460. total_swap_pages += nr_good_pages;
  1461. printk(KERN_INFO "Adding %uk swap on %s. "
  1462. "Priority:%d extents:%d across:%lluk\n",
  1463. nr_good_pages<<(PAGE_SHIFT-10), name, p->prio,
  1464. nr_extents, (unsigned long long)span<<(PAGE_SHIFT-10));
  1465. /* insert swap space into swap_list: */
  1466. prev = -1;
  1467. for (i = swap_list.head; i >= 0; i = swap_info[i].next) {
  1468. if (p->prio >= swap_info[i].prio) {
  1469. break;
  1470. }
  1471. prev = i;
  1472. }
  1473. p->next = i;
  1474. if (prev < 0) {
  1475. swap_list.head = swap_list.next = p - swap_info;
  1476. } else {
  1477. swap_info[prev].next = p - swap_info;
  1478. }
  1479. spin_unlock(&swap_lock);
  1480. mutex_unlock(&swapon_mutex);
  1481. error = 0;
  1482. goto out;
  1483. bad_swap:
  1484. if (bdev) {
  1485. set_blocksize(bdev, p->old_block_size);
  1486. bd_release(bdev);
  1487. }
  1488. destroy_swap_extents(p);
  1489. bad_swap_2:
  1490. spin_lock(&swap_lock);
  1491. swap_map = p->swap_map;
  1492. p->swap_file = NULL;
  1493. p->swap_map = NULL;
  1494. p->flags = 0;
  1495. if (!(swap_flags & SWAP_FLAG_PREFER))
  1496. ++least_priority;
  1497. spin_unlock(&swap_lock);
  1498. vfree(swap_map);
  1499. if (swap_file)
  1500. filp_close(swap_file, NULL);
  1501. out:
  1502. if (page && !IS_ERR(page)) {
  1503. kunmap(page);
  1504. page_cache_release(page);
  1505. }
  1506. if (name)
  1507. putname(name);
  1508. if (did_down) {
  1509. if (!error)
  1510. inode->i_flags |= S_SWAPFILE;
  1511. mutex_unlock(&inode->i_mutex);
  1512. }
  1513. return error;
  1514. }
  1515. void si_swapinfo(struct sysinfo *val)
  1516. {
  1517. unsigned int i;
  1518. unsigned long nr_to_be_unused = 0;
  1519. spin_lock(&swap_lock);
  1520. for (i = 0; i < nr_swapfiles; i++) {
  1521. if (!(swap_info[i].flags & SWP_USED) ||
  1522. (swap_info[i].flags & SWP_WRITEOK))
  1523. continue;
  1524. nr_to_be_unused += swap_info[i].inuse_pages;
  1525. }
  1526. val->freeswap = nr_swap_pages + nr_to_be_unused;
  1527. val->totalswap = total_swap_pages + nr_to_be_unused;
  1528. spin_unlock(&swap_lock);
  1529. }
  1530. /*
  1531. * Verify that a swap entry is valid and increment its swap map count.
  1532. *
  1533. * Note: if swap_map[] reaches SWAP_MAP_MAX the entries are treated as
  1534. * "permanent", but will be reclaimed by the next swapoff.
  1535. */
  1536. int swap_duplicate(swp_entry_t entry)
  1537. {
  1538. struct swap_info_struct * p;
  1539. unsigned long offset, type;
  1540. int result = 0;
  1541. if (is_migration_entry(entry))
  1542. return 1;
  1543. type = swp_type(entry);
  1544. if (type >= nr_swapfiles)
  1545. goto bad_file;
  1546. p = type + swap_info;
  1547. offset = swp_offset(entry);
  1548. spin_lock(&swap_lock);
  1549. if (offset < p->max && p->swap_map[offset]) {
  1550. if (p->swap_map[offset] < SWAP_MAP_MAX - 1) {
  1551. p->swap_map[offset]++;
  1552. result = 1;
  1553. } else if (p->swap_map[offset] <= SWAP_MAP_MAX) {
  1554. if (swap_overflow++ < 5)
  1555. printk(KERN_WARNING "swap_dup: swap entry overflow\n");
  1556. p->swap_map[offset] = SWAP_MAP_MAX;
  1557. result = 1;
  1558. }
  1559. }
  1560. spin_unlock(&swap_lock);
  1561. out:
  1562. return result;
  1563. bad_file:
  1564. printk(KERN_ERR "swap_dup: %s%08lx\n", Bad_file, entry.val);
  1565. goto out;
  1566. }
  1567. struct swap_info_struct *
  1568. get_swap_info_struct(unsigned type)
  1569. {
  1570. return &swap_info[type];
  1571. }
  1572. /*
  1573. * swap_lock prevents swap_map being freed. Don't grab an extra
  1574. * reference on the swaphandle, it doesn't matter if it becomes unused.
  1575. */
  1576. int valid_swaphandles(swp_entry_t entry, unsigned long *offset)
  1577. {
  1578. int our_page_cluster = page_cluster;
  1579. int ret = 0, i = 1 << our_page_cluster;
  1580. unsigned long toff;
  1581. struct swap_info_struct *swapdev = swp_type(entry) + swap_info;
  1582. if (!our_page_cluster) /* no readahead */
  1583. return 0;
  1584. toff = (swp_offset(entry) >> our_page_cluster) << our_page_cluster;
  1585. if (!toff) /* first page is swap header */
  1586. toff++, i--;
  1587. *offset = toff;
  1588. spin_lock(&swap_lock);
  1589. do {
  1590. /* Don't read-ahead past the end of the swap area */
  1591. if (toff >= swapdev->max)
  1592. break;
  1593. /* Don't read in free or bad pages */
  1594. if (!swapdev->swap_map[toff])
  1595. break;
  1596. if (swapdev->swap_map[toff] == SWAP_MAP_BAD)
  1597. break;
  1598. toff++;
  1599. ret++;
  1600. } while (--i);
  1601. spin_unlock(&swap_lock);
  1602. return ret;
  1603. }