swapfile.c 50 KB

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