swapfile.c 42 KB

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