swapfile.c 42 KB

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