swapfile.c 41 KB

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