swapfile.c 41 KB

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