swapfile.c 51 KB

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