swapfile.c 50 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023
  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. struct mem_cgroup *ptr = NULL;
  613. spinlock_t *ptl;
  614. pte_t *pte;
  615. int ret = 1;
  616. if (mem_cgroup_try_charge(vma->vm_mm, GFP_KERNEL, &ptr))
  617. ret = -ENOMEM;
  618. pte = pte_offset_map_lock(vma->vm_mm, pmd, addr, &ptl);
  619. if (unlikely(!pte_same(*pte, swp_entry_to_pte(entry)))) {
  620. if (ret > 0)
  621. mem_cgroup_cancel_charge_swapin(ptr);
  622. ret = 0;
  623. goto out;
  624. }
  625. inc_mm_counter(vma->vm_mm, anon_rss);
  626. get_page(page);
  627. set_pte_at(vma->vm_mm, addr, pte,
  628. pte_mkold(mk_pte(page, vma->vm_page_prot)));
  629. page_add_anon_rmap(page, vma, addr);
  630. mem_cgroup_commit_charge_swapin(page, ptr);
  631. swap_free(entry);
  632. /*
  633. * Move the page to the active list so it is not
  634. * immediately swapped out again after swapon.
  635. */
  636. activate_page(page);
  637. out:
  638. pte_unmap_unlock(pte, ptl);
  639. return ret;
  640. }
  641. static int unuse_pte_range(struct vm_area_struct *vma, pmd_t *pmd,
  642. unsigned long addr, unsigned long end,
  643. swp_entry_t entry, struct page *page)
  644. {
  645. pte_t swp_pte = swp_entry_to_pte(entry);
  646. pte_t *pte;
  647. int ret = 0;
  648. /*
  649. * We don't actually need pte lock while scanning for swp_pte: since
  650. * we hold page lock and mmap_sem, swp_pte cannot be inserted into the
  651. * page table while we're scanning; though it could get zapped, and on
  652. * some architectures (e.g. x86_32 with PAE) we might catch a glimpse
  653. * of unmatched parts which look like swp_pte, so unuse_pte must
  654. * recheck under pte lock. Scanning without pte lock lets it be
  655. * preemptible whenever CONFIG_PREEMPT but not CONFIG_HIGHPTE.
  656. */
  657. pte = pte_offset_map(pmd, addr);
  658. do {
  659. /*
  660. * swapoff spends a _lot_ of time in this loop!
  661. * Test inline before going to call unuse_pte.
  662. */
  663. if (unlikely(pte_same(*pte, swp_pte))) {
  664. pte_unmap(pte);
  665. ret = unuse_pte(vma, pmd, addr, entry, page);
  666. if (ret)
  667. goto out;
  668. pte = pte_offset_map(pmd, addr);
  669. }
  670. } while (pte++, addr += PAGE_SIZE, addr != end);
  671. pte_unmap(pte - 1);
  672. out:
  673. return ret;
  674. }
  675. static inline int unuse_pmd_range(struct vm_area_struct *vma, pud_t *pud,
  676. unsigned long addr, unsigned long end,
  677. swp_entry_t entry, struct page *page)
  678. {
  679. pmd_t *pmd;
  680. unsigned long next;
  681. int ret;
  682. pmd = pmd_offset(pud, addr);
  683. do {
  684. next = pmd_addr_end(addr, end);
  685. if (pmd_none_or_clear_bad(pmd))
  686. continue;
  687. ret = unuse_pte_range(vma, pmd, addr, next, entry, page);
  688. if (ret)
  689. return ret;
  690. } while (pmd++, addr = next, addr != end);
  691. return 0;
  692. }
  693. static inline int unuse_pud_range(struct vm_area_struct *vma, pgd_t *pgd,
  694. unsigned long addr, unsigned long end,
  695. swp_entry_t entry, struct page *page)
  696. {
  697. pud_t *pud;
  698. unsigned long next;
  699. int ret;
  700. pud = pud_offset(pgd, addr);
  701. do {
  702. next = pud_addr_end(addr, end);
  703. if (pud_none_or_clear_bad(pud))
  704. continue;
  705. ret = unuse_pmd_range(vma, pud, addr, next, entry, page);
  706. if (ret)
  707. return ret;
  708. } while (pud++, addr = next, addr != end);
  709. return 0;
  710. }
  711. static int unuse_vma(struct vm_area_struct *vma,
  712. swp_entry_t entry, struct page *page)
  713. {
  714. pgd_t *pgd;
  715. unsigned long addr, end, next;
  716. int ret;
  717. if (page->mapping) {
  718. addr = page_address_in_vma(page, vma);
  719. if (addr == -EFAULT)
  720. return 0;
  721. else
  722. end = addr + PAGE_SIZE;
  723. } else {
  724. addr = vma->vm_start;
  725. end = vma->vm_end;
  726. }
  727. pgd = pgd_offset(vma->vm_mm, addr);
  728. do {
  729. next = pgd_addr_end(addr, end);
  730. if (pgd_none_or_clear_bad(pgd))
  731. continue;
  732. ret = unuse_pud_range(vma, pgd, addr, next, entry, page);
  733. if (ret)
  734. return ret;
  735. } while (pgd++, addr = next, addr != end);
  736. return 0;
  737. }
  738. static int unuse_mm(struct mm_struct *mm,
  739. swp_entry_t entry, struct page *page)
  740. {
  741. struct vm_area_struct *vma;
  742. int ret = 0;
  743. if (!down_read_trylock(&mm->mmap_sem)) {
  744. /*
  745. * Activate page so shrink_inactive_list is unlikely to unmap
  746. * its ptes while lock is dropped, so swapoff can make progress.
  747. */
  748. activate_page(page);
  749. unlock_page(page);
  750. down_read(&mm->mmap_sem);
  751. lock_page(page);
  752. }
  753. for (vma = mm->mmap; vma; vma = vma->vm_next) {
  754. if (vma->anon_vma && (ret = unuse_vma(vma, entry, page)))
  755. break;
  756. }
  757. up_read(&mm->mmap_sem);
  758. return (ret < 0)? ret: 0;
  759. }
  760. /*
  761. * Scan swap_map from current position to next entry still in use.
  762. * Recycle to start on reaching the end, returning 0 when empty.
  763. */
  764. static unsigned int find_next_to_unuse(struct swap_info_struct *si,
  765. unsigned int prev)
  766. {
  767. unsigned int max = si->max;
  768. unsigned int i = prev;
  769. int count;
  770. /*
  771. * No need for swap_lock here: we're just looking
  772. * for whether an entry is in use, not modifying it; false
  773. * hits are okay, and sys_swapoff() has already prevented new
  774. * allocations from this area (while holding swap_lock).
  775. */
  776. for (;;) {
  777. if (++i >= max) {
  778. if (!prev) {
  779. i = 0;
  780. break;
  781. }
  782. /*
  783. * No entries in use at top of swap_map,
  784. * loop back to start and recheck there.
  785. */
  786. max = prev + 1;
  787. prev = 0;
  788. i = 1;
  789. }
  790. count = si->swap_map[i];
  791. if (count && count != SWAP_MAP_BAD)
  792. break;
  793. }
  794. return i;
  795. }
  796. /*
  797. * We completely avoid races by reading each swap page in advance,
  798. * and then search for the process using it. All the necessary
  799. * page table adjustments can then be made atomically.
  800. */
  801. static int try_to_unuse(unsigned int type)
  802. {
  803. struct swap_info_struct * si = &swap_info[type];
  804. struct mm_struct *start_mm;
  805. unsigned short *swap_map;
  806. unsigned short swcount;
  807. struct page *page;
  808. swp_entry_t entry;
  809. unsigned int i = 0;
  810. int retval = 0;
  811. int reset_overflow = 0;
  812. int shmem;
  813. /*
  814. * When searching mms for an entry, a good strategy is to
  815. * start at the first mm we freed the previous entry from
  816. * (though actually we don't notice whether we or coincidence
  817. * freed the entry). Initialize this start_mm with a hold.
  818. *
  819. * A simpler strategy would be to start at the last mm we
  820. * freed the previous entry from; but that would take less
  821. * advantage of mmlist ordering, which clusters forked mms
  822. * together, child after parent. If we race with dup_mmap(), we
  823. * prefer to resolve parent before child, lest we miss entries
  824. * duplicated after we scanned child: using last mm would invert
  825. * that. Though it's only a serious concern when an overflowed
  826. * swap count is reset from SWAP_MAP_MAX, preventing a rescan.
  827. */
  828. start_mm = &init_mm;
  829. atomic_inc(&init_mm.mm_users);
  830. /*
  831. * Keep on scanning until all entries have gone. Usually,
  832. * one pass through swap_map is enough, but not necessarily:
  833. * there are races when an instance of an entry might be missed.
  834. */
  835. while ((i = find_next_to_unuse(si, i)) != 0) {
  836. if (signal_pending(current)) {
  837. retval = -EINTR;
  838. break;
  839. }
  840. /*
  841. * Get a page for the entry, using the existing swap
  842. * cache page if there is one. Otherwise, get a clean
  843. * page and read the swap into it.
  844. */
  845. swap_map = &si->swap_map[i];
  846. entry = swp_entry(type, i);
  847. page = read_swap_cache_async(entry,
  848. GFP_HIGHUSER_MOVABLE, NULL, 0);
  849. if (!page) {
  850. /*
  851. * Either swap_duplicate() failed because entry
  852. * has been freed independently, and will not be
  853. * reused since sys_swapoff() already disabled
  854. * allocation from here, or alloc_page() failed.
  855. */
  856. if (!*swap_map)
  857. continue;
  858. retval = -ENOMEM;
  859. break;
  860. }
  861. /*
  862. * Don't hold on to start_mm if it looks like exiting.
  863. */
  864. if (atomic_read(&start_mm->mm_users) == 1) {
  865. mmput(start_mm);
  866. start_mm = &init_mm;
  867. atomic_inc(&init_mm.mm_users);
  868. }
  869. /*
  870. * Wait for and lock page. When do_swap_page races with
  871. * try_to_unuse, do_swap_page can handle the fault much
  872. * faster than try_to_unuse can locate the entry. This
  873. * apparently redundant "wait_on_page_locked" lets try_to_unuse
  874. * defer to do_swap_page in such a case - in some tests,
  875. * do_swap_page and try_to_unuse repeatedly compete.
  876. */
  877. wait_on_page_locked(page);
  878. wait_on_page_writeback(page);
  879. lock_page(page);
  880. wait_on_page_writeback(page);
  881. /*
  882. * Remove all references to entry.
  883. * Whenever we reach init_mm, there's no address space
  884. * to search, but use it as a reminder to search shmem.
  885. */
  886. shmem = 0;
  887. swcount = *swap_map;
  888. if (swcount > 1) {
  889. if (start_mm == &init_mm)
  890. shmem = shmem_unuse(entry, page);
  891. else
  892. retval = unuse_mm(start_mm, entry, page);
  893. }
  894. if (*swap_map > 1) {
  895. int set_start_mm = (*swap_map >= swcount);
  896. struct list_head *p = &start_mm->mmlist;
  897. struct mm_struct *new_start_mm = start_mm;
  898. struct mm_struct *prev_mm = start_mm;
  899. struct mm_struct *mm;
  900. atomic_inc(&new_start_mm->mm_users);
  901. atomic_inc(&prev_mm->mm_users);
  902. spin_lock(&mmlist_lock);
  903. while (*swap_map > 1 && !retval && !shmem &&
  904. (p = p->next) != &start_mm->mmlist) {
  905. mm = list_entry(p, struct mm_struct, mmlist);
  906. if (!atomic_inc_not_zero(&mm->mm_users))
  907. continue;
  908. spin_unlock(&mmlist_lock);
  909. mmput(prev_mm);
  910. prev_mm = mm;
  911. cond_resched();
  912. swcount = *swap_map;
  913. if (swcount <= 1)
  914. ;
  915. else if (mm == &init_mm) {
  916. set_start_mm = 1;
  917. shmem = shmem_unuse(entry, page);
  918. } else
  919. retval = unuse_mm(mm, entry, page);
  920. if (set_start_mm && *swap_map < swcount) {
  921. mmput(new_start_mm);
  922. atomic_inc(&mm->mm_users);
  923. new_start_mm = mm;
  924. set_start_mm = 0;
  925. }
  926. spin_lock(&mmlist_lock);
  927. }
  928. spin_unlock(&mmlist_lock);
  929. mmput(prev_mm);
  930. mmput(start_mm);
  931. start_mm = new_start_mm;
  932. }
  933. if (shmem) {
  934. /* page has already been unlocked and released */
  935. if (shmem > 0)
  936. continue;
  937. retval = shmem;
  938. break;
  939. }
  940. if (retval) {
  941. unlock_page(page);
  942. page_cache_release(page);
  943. break;
  944. }
  945. /*
  946. * How could swap count reach 0x7fff when the maximum
  947. * pid is 0x7fff, and there's no way to repeat a swap
  948. * page within an mm (except in shmem, where it's the
  949. * shared object which takes the reference count)?
  950. * We believe SWAP_MAP_MAX cannot occur in Linux 2.4.
  951. *
  952. * If that's wrong, then we should worry more about
  953. * exit_mmap() and do_munmap() cases described above:
  954. * we might be resetting SWAP_MAP_MAX too early here.
  955. * We know "Undead"s can happen, they're okay, so don't
  956. * report them; but do report if we reset SWAP_MAP_MAX.
  957. */
  958. if (*swap_map == SWAP_MAP_MAX) {
  959. spin_lock(&swap_lock);
  960. *swap_map = 1;
  961. spin_unlock(&swap_lock);
  962. reset_overflow = 1;
  963. }
  964. /*
  965. * If a reference remains (rare), we would like to leave
  966. * the page in the swap cache; but try_to_unmap could
  967. * then re-duplicate the entry once we drop page lock,
  968. * so we might loop indefinitely; also, that page could
  969. * not be swapped out to other storage meanwhile. So:
  970. * delete from cache even if there's another reference,
  971. * after ensuring that the data has been saved to disk -
  972. * since if the reference remains (rarer), it will be
  973. * read from disk into another page. Splitting into two
  974. * pages would be incorrect if swap supported "shared
  975. * private" pages, but they are handled by tmpfs files.
  976. */
  977. if ((*swap_map > 1) && PageDirty(page) && PageSwapCache(page)) {
  978. struct writeback_control wbc = {
  979. .sync_mode = WB_SYNC_NONE,
  980. };
  981. swap_writepage(page, &wbc);
  982. lock_page(page);
  983. wait_on_page_writeback(page);
  984. }
  985. /*
  986. * It is conceivable that a racing task removed this page from
  987. * swap cache just before we acquired the page lock at the top,
  988. * or while we dropped it in unuse_mm(). The page might even
  989. * be back in swap cache on another swap area: that we must not
  990. * delete, since it may not have been written out to swap yet.
  991. */
  992. if (PageSwapCache(page) &&
  993. likely(page_private(page) == entry.val))
  994. delete_from_swap_cache(page);
  995. /*
  996. * So we could skip searching mms once swap count went
  997. * to 1, we did not mark any present ptes as dirty: must
  998. * mark page dirty so shrink_page_list will preserve it.
  999. */
  1000. SetPageDirty(page);
  1001. unlock_page(page);
  1002. page_cache_release(page);
  1003. /*
  1004. * Make sure that we aren't completely killing
  1005. * interactive performance.
  1006. */
  1007. cond_resched();
  1008. }
  1009. mmput(start_mm);
  1010. if (reset_overflow) {
  1011. printk(KERN_WARNING "swapoff: cleared swap entry overflow\n");
  1012. swap_overflow = 0;
  1013. }
  1014. return retval;
  1015. }
  1016. /*
  1017. * After a successful try_to_unuse, if no swap is now in use, we know
  1018. * we can empty the mmlist. swap_lock must be held on entry and exit.
  1019. * Note that mmlist_lock nests inside swap_lock, and an mm must be
  1020. * added to the mmlist just after page_duplicate - before would be racy.
  1021. */
  1022. static void drain_mmlist(void)
  1023. {
  1024. struct list_head *p, *next;
  1025. unsigned int i;
  1026. for (i = 0; i < nr_swapfiles; i++)
  1027. if (swap_info[i].inuse_pages)
  1028. return;
  1029. spin_lock(&mmlist_lock);
  1030. list_for_each_safe(p, next, &init_mm.mmlist)
  1031. list_del_init(p);
  1032. spin_unlock(&mmlist_lock);
  1033. }
  1034. /*
  1035. * Use this swapdev's extent info to locate the (PAGE_SIZE) block which
  1036. * corresponds to page offset `offset'.
  1037. */
  1038. sector_t map_swap_page(struct swap_info_struct *sis, pgoff_t offset)
  1039. {
  1040. struct swap_extent *se = sis->curr_swap_extent;
  1041. struct swap_extent *start_se = se;
  1042. for ( ; ; ) {
  1043. struct list_head *lh;
  1044. if (se->start_page <= offset &&
  1045. offset < (se->start_page + se->nr_pages)) {
  1046. return se->start_block + (offset - se->start_page);
  1047. }
  1048. lh = se->list.next;
  1049. if (lh == &sis->extent_list)
  1050. lh = lh->next;
  1051. se = list_entry(lh, struct swap_extent, list);
  1052. sis->curr_swap_extent = se;
  1053. BUG_ON(se == start_se); /* It *must* be present */
  1054. }
  1055. }
  1056. #ifdef CONFIG_HIBERNATION
  1057. /*
  1058. * Get the (PAGE_SIZE) block corresponding to given offset on the swapdev
  1059. * corresponding to given index in swap_info (swap type).
  1060. */
  1061. sector_t swapdev_block(int swap_type, pgoff_t offset)
  1062. {
  1063. struct swap_info_struct *sis;
  1064. if (swap_type >= nr_swapfiles)
  1065. return 0;
  1066. sis = swap_info + swap_type;
  1067. return (sis->flags & SWP_WRITEOK) ? map_swap_page(sis, offset) : 0;
  1068. }
  1069. #endif /* CONFIG_HIBERNATION */
  1070. /*
  1071. * Free all of a swapdev's extent information
  1072. */
  1073. static void destroy_swap_extents(struct swap_info_struct *sis)
  1074. {
  1075. while (!list_empty(&sis->extent_list)) {
  1076. struct swap_extent *se;
  1077. se = list_entry(sis->extent_list.next,
  1078. struct swap_extent, list);
  1079. list_del(&se->list);
  1080. kfree(se);
  1081. }
  1082. }
  1083. /*
  1084. * Add a block range (and the corresponding page range) into this swapdev's
  1085. * extent list. The extent list is kept sorted in page order.
  1086. *
  1087. * This function rather assumes that it is called in ascending page order.
  1088. */
  1089. static int
  1090. add_swap_extent(struct swap_info_struct *sis, unsigned long start_page,
  1091. unsigned long nr_pages, sector_t start_block)
  1092. {
  1093. struct swap_extent *se;
  1094. struct swap_extent *new_se;
  1095. struct list_head *lh;
  1096. lh = sis->extent_list.prev; /* The highest page extent */
  1097. if (lh != &sis->extent_list) {
  1098. se = list_entry(lh, struct swap_extent, list);
  1099. BUG_ON(se->start_page + se->nr_pages != start_page);
  1100. if (se->start_block + se->nr_pages == start_block) {
  1101. /* Merge it */
  1102. se->nr_pages += nr_pages;
  1103. return 0;
  1104. }
  1105. }
  1106. /*
  1107. * No merge. Insert a new extent, preserving ordering.
  1108. */
  1109. new_se = kmalloc(sizeof(*se), GFP_KERNEL);
  1110. if (new_se == NULL)
  1111. return -ENOMEM;
  1112. new_se->start_page = start_page;
  1113. new_se->nr_pages = nr_pages;
  1114. new_se->start_block = start_block;
  1115. list_add_tail(&new_se->list, &sis->extent_list);
  1116. return 1;
  1117. }
  1118. /*
  1119. * A `swap extent' is a simple thing which maps a contiguous range of pages
  1120. * onto a contiguous range of disk blocks. An ordered list of swap extents
  1121. * is built at swapon time and is then used at swap_writepage/swap_readpage
  1122. * time for locating where on disk a page belongs.
  1123. *
  1124. * If the swapfile is an S_ISBLK block device, a single extent is installed.
  1125. * This is done so that the main operating code can treat S_ISBLK and S_ISREG
  1126. * swap files identically.
  1127. *
  1128. * Whether the swapdev is an S_ISREG file or an S_ISBLK blockdev, the swap
  1129. * extent list operates in PAGE_SIZE disk blocks. Both S_ISREG and S_ISBLK
  1130. * swapfiles are handled *identically* after swapon time.
  1131. *
  1132. * For S_ISREG swapfiles, setup_swap_extents() will walk all the file's blocks
  1133. * and will parse them into an ordered extent list, in PAGE_SIZE chunks. If
  1134. * some stray blocks are found which do not fall within the PAGE_SIZE alignment
  1135. * requirements, they are simply tossed out - we will never use those blocks
  1136. * for swapping.
  1137. *
  1138. * For S_ISREG swapfiles we set S_SWAPFILE across the life of the swapon. This
  1139. * prevents root from shooting her foot off by ftruncating an in-use swapfile,
  1140. * which will scribble on the fs.
  1141. *
  1142. * The amount of disk space which a single swap extent represents varies.
  1143. * Typically it is in the 1-4 megabyte range. So we can have hundreds of
  1144. * extents in the list. To avoid much list walking, we cache the previous
  1145. * search location in `curr_swap_extent', and start new searches from there.
  1146. * This is extremely effective. The average number of iterations in
  1147. * map_swap_page() has been measured at about 0.3 per page. - akpm.
  1148. */
  1149. static int setup_swap_extents(struct swap_info_struct *sis, sector_t *span)
  1150. {
  1151. struct inode *inode;
  1152. unsigned blocks_per_page;
  1153. unsigned long page_no;
  1154. unsigned blkbits;
  1155. sector_t probe_block;
  1156. sector_t last_block;
  1157. sector_t lowest_block = -1;
  1158. sector_t highest_block = 0;
  1159. int nr_extents = 0;
  1160. int ret;
  1161. inode = sis->swap_file->f_mapping->host;
  1162. if (S_ISBLK(inode->i_mode)) {
  1163. ret = add_swap_extent(sis, 0, sis->max, 0);
  1164. *span = sis->pages;
  1165. goto done;
  1166. }
  1167. blkbits = inode->i_blkbits;
  1168. blocks_per_page = PAGE_SIZE >> blkbits;
  1169. /*
  1170. * Map all the blocks into the extent list. This code doesn't try
  1171. * to be very smart.
  1172. */
  1173. probe_block = 0;
  1174. page_no = 0;
  1175. last_block = i_size_read(inode) >> blkbits;
  1176. while ((probe_block + blocks_per_page) <= last_block &&
  1177. page_no < sis->max) {
  1178. unsigned block_in_page;
  1179. sector_t first_block;
  1180. first_block = bmap(inode, probe_block);
  1181. if (first_block == 0)
  1182. goto bad_bmap;
  1183. /*
  1184. * It must be PAGE_SIZE aligned on-disk
  1185. */
  1186. if (first_block & (blocks_per_page - 1)) {
  1187. probe_block++;
  1188. goto reprobe;
  1189. }
  1190. for (block_in_page = 1; block_in_page < blocks_per_page;
  1191. block_in_page++) {
  1192. sector_t block;
  1193. block = bmap(inode, probe_block + block_in_page);
  1194. if (block == 0)
  1195. goto bad_bmap;
  1196. if (block != first_block + block_in_page) {
  1197. /* Discontiguity */
  1198. probe_block++;
  1199. goto reprobe;
  1200. }
  1201. }
  1202. first_block >>= (PAGE_SHIFT - blkbits);
  1203. if (page_no) { /* exclude the header page */
  1204. if (first_block < lowest_block)
  1205. lowest_block = first_block;
  1206. if (first_block > highest_block)
  1207. highest_block = first_block;
  1208. }
  1209. /*
  1210. * We found a PAGE_SIZE-length, PAGE_SIZE-aligned run of blocks
  1211. */
  1212. ret = add_swap_extent(sis, page_no, 1, first_block);
  1213. if (ret < 0)
  1214. goto out;
  1215. nr_extents += ret;
  1216. page_no++;
  1217. probe_block += blocks_per_page;
  1218. reprobe:
  1219. continue;
  1220. }
  1221. ret = nr_extents;
  1222. *span = 1 + highest_block - lowest_block;
  1223. if (page_no == 0)
  1224. page_no = 1; /* force Empty message */
  1225. sis->max = page_no;
  1226. sis->pages = page_no - 1;
  1227. sis->highest_bit = page_no - 1;
  1228. done:
  1229. sis->curr_swap_extent = list_entry(sis->extent_list.prev,
  1230. struct swap_extent, list);
  1231. goto out;
  1232. bad_bmap:
  1233. printk(KERN_ERR "swapon: swapfile has holes\n");
  1234. ret = -EINVAL;
  1235. out:
  1236. return ret;
  1237. }
  1238. asmlinkage long sys_swapoff(const char __user * specialfile)
  1239. {
  1240. struct swap_info_struct * p = NULL;
  1241. unsigned short *swap_map;
  1242. struct file *swap_file, *victim;
  1243. struct address_space *mapping;
  1244. struct inode *inode;
  1245. char * pathname;
  1246. int i, type, prev;
  1247. int err;
  1248. if (!capable(CAP_SYS_ADMIN))
  1249. return -EPERM;
  1250. pathname = getname(specialfile);
  1251. err = PTR_ERR(pathname);
  1252. if (IS_ERR(pathname))
  1253. goto out;
  1254. victim = filp_open(pathname, O_RDWR|O_LARGEFILE, 0);
  1255. putname(pathname);
  1256. err = PTR_ERR(victim);
  1257. if (IS_ERR(victim))
  1258. goto out;
  1259. mapping = victim->f_mapping;
  1260. prev = -1;
  1261. spin_lock(&swap_lock);
  1262. for (type = swap_list.head; type >= 0; type = swap_info[type].next) {
  1263. p = swap_info + type;
  1264. if (p->flags & SWP_WRITEOK) {
  1265. if (p->swap_file->f_mapping == mapping)
  1266. break;
  1267. }
  1268. prev = type;
  1269. }
  1270. if (type < 0) {
  1271. err = -EINVAL;
  1272. spin_unlock(&swap_lock);
  1273. goto out_dput;
  1274. }
  1275. if (!security_vm_enough_memory(p->pages))
  1276. vm_unacct_memory(p->pages);
  1277. else {
  1278. err = -ENOMEM;
  1279. spin_unlock(&swap_lock);
  1280. goto out_dput;
  1281. }
  1282. if (prev < 0) {
  1283. swap_list.head = p->next;
  1284. } else {
  1285. swap_info[prev].next = p->next;
  1286. }
  1287. if (type == swap_list.next) {
  1288. /* just pick something that's safe... */
  1289. swap_list.next = swap_list.head;
  1290. }
  1291. if (p->prio < 0) {
  1292. for (i = p->next; i >= 0; i = swap_info[i].next)
  1293. swap_info[i].prio = p->prio--;
  1294. least_priority++;
  1295. }
  1296. nr_swap_pages -= p->pages;
  1297. total_swap_pages -= p->pages;
  1298. p->flags &= ~SWP_WRITEOK;
  1299. spin_unlock(&swap_lock);
  1300. current->flags |= PF_SWAPOFF;
  1301. err = try_to_unuse(type);
  1302. current->flags &= ~PF_SWAPOFF;
  1303. if (err) {
  1304. /* re-insert swap space back into swap_list */
  1305. spin_lock(&swap_lock);
  1306. if (p->prio < 0)
  1307. p->prio = --least_priority;
  1308. prev = -1;
  1309. for (i = swap_list.head; i >= 0; i = swap_info[i].next) {
  1310. if (p->prio >= swap_info[i].prio)
  1311. break;
  1312. prev = i;
  1313. }
  1314. p->next = i;
  1315. if (prev < 0)
  1316. swap_list.head = swap_list.next = p - swap_info;
  1317. else
  1318. swap_info[prev].next = p - swap_info;
  1319. nr_swap_pages += p->pages;
  1320. total_swap_pages += p->pages;
  1321. p->flags |= SWP_WRITEOK;
  1322. spin_unlock(&swap_lock);
  1323. goto out_dput;
  1324. }
  1325. /* wait for any unplug function to finish */
  1326. down_write(&swap_unplug_sem);
  1327. up_write(&swap_unplug_sem);
  1328. destroy_swap_extents(p);
  1329. mutex_lock(&swapon_mutex);
  1330. spin_lock(&swap_lock);
  1331. drain_mmlist();
  1332. /* wait for anyone still in scan_swap_map */
  1333. p->highest_bit = 0; /* cuts scans short */
  1334. while (p->flags >= SWP_SCANNING) {
  1335. spin_unlock(&swap_lock);
  1336. schedule_timeout_uninterruptible(1);
  1337. spin_lock(&swap_lock);
  1338. }
  1339. swap_file = p->swap_file;
  1340. p->swap_file = NULL;
  1341. p->max = 0;
  1342. swap_map = p->swap_map;
  1343. p->swap_map = NULL;
  1344. p->flags = 0;
  1345. spin_unlock(&swap_lock);
  1346. mutex_unlock(&swapon_mutex);
  1347. vfree(swap_map);
  1348. inode = mapping->host;
  1349. if (S_ISBLK(inode->i_mode)) {
  1350. struct block_device *bdev = I_BDEV(inode);
  1351. set_blocksize(bdev, p->old_block_size);
  1352. bd_release(bdev);
  1353. } else {
  1354. mutex_lock(&inode->i_mutex);
  1355. inode->i_flags &= ~S_SWAPFILE;
  1356. mutex_unlock(&inode->i_mutex);
  1357. }
  1358. filp_close(swap_file, NULL);
  1359. err = 0;
  1360. out_dput:
  1361. filp_close(victim, NULL);
  1362. out:
  1363. return err;
  1364. }
  1365. #ifdef CONFIG_PROC_FS
  1366. /* iterator */
  1367. static void *swap_start(struct seq_file *swap, loff_t *pos)
  1368. {
  1369. struct swap_info_struct *ptr = swap_info;
  1370. int i;
  1371. loff_t l = *pos;
  1372. mutex_lock(&swapon_mutex);
  1373. if (!l)
  1374. return SEQ_START_TOKEN;
  1375. for (i = 0; i < nr_swapfiles; i++, ptr++) {
  1376. if (!(ptr->flags & SWP_USED) || !ptr->swap_map)
  1377. continue;
  1378. if (!--l)
  1379. return ptr;
  1380. }
  1381. return NULL;
  1382. }
  1383. static void *swap_next(struct seq_file *swap, void *v, loff_t *pos)
  1384. {
  1385. struct swap_info_struct *ptr;
  1386. struct swap_info_struct *endptr = swap_info + nr_swapfiles;
  1387. if (v == SEQ_START_TOKEN)
  1388. ptr = swap_info;
  1389. else {
  1390. ptr = v;
  1391. ptr++;
  1392. }
  1393. for (; ptr < endptr; ptr++) {
  1394. if (!(ptr->flags & SWP_USED) || !ptr->swap_map)
  1395. continue;
  1396. ++*pos;
  1397. return ptr;
  1398. }
  1399. return NULL;
  1400. }
  1401. static void swap_stop(struct seq_file *swap, void *v)
  1402. {
  1403. mutex_unlock(&swapon_mutex);
  1404. }
  1405. static int swap_show(struct seq_file *swap, void *v)
  1406. {
  1407. struct swap_info_struct *ptr = v;
  1408. struct file *file;
  1409. int len;
  1410. if (ptr == SEQ_START_TOKEN) {
  1411. seq_puts(swap,"Filename\t\t\t\tType\t\tSize\tUsed\tPriority\n");
  1412. return 0;
  1413. }
  1414. file = ptr->swap_file;
  1415. len = seq_path(swap, &file->f_path, " \t\n\\");
  1416. seq_printf(swap, "%*s%s\t%u\t%u\t%d\n",
  1417. len < 40 ? 40 - len : 1, " ",
  1418. S_ISBLK(file->f_path.dentry->d_inode->i_mode) ?
  1419. "partition" : "file\t",
  1420. ptr->pages << (PAGE_SHIFT - 10),
  1421. ptr->inuse_pages << (PAGE_SHIFT - 10),
  1422. ptr->prio);
  1423. return 0;
  1424. }
  1425. static const struct seq_operations swaps_op = {
  1426. .start = swap_start,
  1427. .next = swap_next,
  1428. .stop = swap_stop,
  1429. .show = swap_show
  1430. };
  1431. static int swaps_open(struct inode *inode, struct file *file)
  1432. {
  1433. return seq_open(file, &swaps_op);
  1434. }
  1435. static const struct file_operations proc_swaps_operations = {
  1436. .open = swaps_open,
  1437. .read = seq_read,
  1438. .llseek = seq_lseek,
  1439. .release = seq_release,
  1440. };
  1441. static int __init procswaps_init(void)
  1442. {
  1443. proc_create("swaps", 0, NULL, &proc_swaps_operations);
  1444. return 0;
  1445. }
  1446. __initcall(procswaps_init);
  1447. #endif /* CONFIG_PROC_FS */
  1448. #ifdef MAX_SWAPFILES_CHECK
  1449. static int __init max_swapfiles_check(void)
  1450. {
  1451. MAX_SWAPFILES_CHECK();
  1452. return 0;
  1453. }
  1454. late_initcall(max_swapfiles_check);
  1455. #endif
  1456. /*
  1457. * Written 01/25/92 by Simmule Turner, heavily changed by Linus.
  1458. *
  1459. * The swapon system call
  1460. */
  1461. asmlinkage long sys_swapon(const char __user * specialfile, int swap_flags)
  1462. {
  1463. struct swap_info_struct * p;
  1464. char *name = NULL;
  1465. struct block_device *bdev = NULL;
  1466. struct file *swap_file = NULL;
  1467. struct address_space *mapping;
  1468. unsigned int type;
  1469. int i, prev;
  1470. int error;
  1471. union swap_header *swap_header = NULL;
  1472. unsigned int nr_good_pages = 0;
  1473. int nr_extents = 0;
  1474. sector_t span;
  1475. unsigned long maxpages = 1;
  1476. unsigned long swapfilepages;
  1477. unsigned short *swap_map = NULL;
  1478. struct page *page = NULL;
  1479. struct inode *inode = NULL;
  1480. int did_down = 0;
  1481. if (!capable(CAP_SYS_ADMIN))
  1482. return -EPERM;
  1483. spin_lock(&swap_lock);
  1484. p = swap_info;
  1485. for (type = 0 ; type < nr_swapfiles ; type++,p++)
  1486. if (!(p->flags & SWP_USED))
  1487. break;
  1488. error = -EPERM;
  1489. if (type >= MAX_SWAPFILES) {
  1490. spin_unlock(&swap_lock);
  1491. goto out;
  1492. }
  1493. if (type >= nr_swapfiles)
  1494. nr_swapfiles = type+1;
  1495. memset(p, 0, sizeof(*p));
  1496. INIT_LIST_HEAD(&p->extent_list);
  1497. p->flags = SWP_USED;
  1498. p->next = -1;
  1499. spin_unlock(&swap_lock);
  1500. name = getname(specialfile);
  1501. error = PTR_ERR(name);
  1502. if (IS_ERR(name)) {
  1503. name = NULL;
  1504. goto bad_swap_2;
  1505. }
  1506. swap_file = filp_open(name, O_RDWR|O_LARGEFILE, 0);
  1507. error = PTR_ERR(swap_file);
  1508. if (IS_ERR(swap_file)) {
  1509. swap_file = NULL;
  1510. goto bad_swap_2;
  1511. }
  1512. p->swap_file = swap_file;
  1513. mapping = swap_file->f_mapping;
  1514. inode = mapping->host;
  1515. error = -EBUSY;
  1516. for (i = 0; i < nr_swapfiles; i++) {
  1517. struct swap_info_struct *q = &swap_info[i];
  1518. if (i == type || !q->swap_file)
  1519. continue;
  1520. if (mapping == q->swap_file->f_mapping)
  1521. goto bad_swap;
  1522. }
  1523. error = -EINVAL;
  1524. if (S_ISBLK(inode->i_mode)) {
  1525. bdev = I_BDEV(inode);
  1526. error = bd_claim(bdev, sys_swapon);
  1527. if (error < 0) {
  1528. bdev = NULL;
  1529. error = -EINVAL;
  1530. goto bad_swap;
  1531. }
  1532. p->old_block_size = block_size(bdev);
  1533. error = set_blocksize(bdev, PAGE_SIZE);
  1534. if (error < 0)
  1535. goto bad_swap;
  1536. p->bdev = bdev;
  1537. } else if (S_ISREG(inode->i_mode)) {
  1538. p->bdev = inode->i_sb->s_bdev;
  1539. mutex_lock(&inode->i_mutex);
  1540. did_down = 1;
  1541. if (IS_SWAPFILE(inode)) {
  1542. error = -EBUSY;
  1543. goto bad_swap;
  1544. }
  1545. } else {
  1546. goto bad_swap;
  1547. }
  1548. swapfilepages = i_size_read(inode) >> PAGE_SHIFT;
  1549. /*
  1550. * Read the swap header.
  1551. */
  1552. if (!mapping->a_ops->readpage) {
  1553. error = -EINVAL;
  1554. goto bad_swap;
  1555. }
  1556. page = read_mapping_page(mapping, 0, swap_file);
  1557. if (IS_ERR(page)) {
  1558. error = PTR_ERR(page);
  1559. goto bad_swap;
  1560. }
  1561. swap_header = kmap(page);
  1562. if (memcmp("SWAPSPACE2", swap_header->magic.magic, 10)) {
  1563. printk(KERN_ERR "Unable to find swap-space signature\n");
  1564. error = -EINVAL;
  1565. goto bad_swap;
  1566. }
  1567. /* swap partition endianess hack... */
  1568. if (swab32(swap_header->info.version) == 1) {
  1569. swab32s(&swap_header->info.version);
  1570. swab32s(&swap_header->info.last_page);
  1571. swab32s(&swap_header->info.nr_badpages);
  1572. for (i = 0; i < swap_header->info.nr_badpages; i++)
  1573. swab32s(&swap_header->info.badpages[i]);
  1574. }
  1575. /* Check the swap header's sub-version */
  1576. if (swap_header->info.version != 1) {
  1577. printk(KERN_WARNING
  1578. "Unable to handle swap header version %d\n",
  1579. swap_header->info.version);
  1580. error = -EINVAL;
  1581. goto bad_swap;
  1582. }
  1583. p->lowest_bit = 1;
  1584. p->cluster_next = 1;
  1585. /*
  1586. * Find out how many pages are allowed for a single swap
  1587. * device. There are two limiting factors: 1) the number of
  1588. * bits for the swap offset in the swp_entry_t type and
  1589. * 2) the number of bits in the a swap pte as defined by
  1590. * the different architectures. In order to find the
  1591. * largest possible bit mask a swap entry with swap type 0
  1592. * and swap offset ~0UL is created, encoded to a swap pte,
  1593. * decoded to a swp_entry_t again and finally the swap
  1594. * offset is extracted. This will mask all the bits from
  1595. * the initial ~0UL mask that can't be encoded in either
  1596. * the swp_entry_t or the architecture definition of a
  1597. * swap pte.
  1598. */
  1599. maxpages = swp_offset(pte_to_swp_entry(
  1600. swp_entry_to_pte(swp_entry(0, ~0UL)))) - 1;
  1601. if (maxpages > swap_header->info.last_page)
  1602. maxpages = swap_header->info.last_page;
  1603. p->highest_bit = maxpages - 1;
  1604. error = -EINVAL;
  1605. if (!maxpages)
  1606. goto bad_swap;
  1607. if (swapfilepages && maxpages > swapfilepages) {
  1608. printk(KERN_WARNING
  1609. "Swap area shorter than signature indicates\n");
  1610. goto bad_swap;
  1611. }
  1612. if (swap_header->info.nr_badpages && S_ISREG(inode->i_mode))
  1613. goto bad_swap;
  1614. if (swap_header->info.nr_badpages > MAX_SWAP_BADPAGES)
  1615. goto bad_swap;
  1616. /* OK, set up the swap map and apply the bad block list */
  1617. swap_map = vmalloc(maxpages * sizeof(short));
  1618. if (!swap_map) {
  1619. error = -ENOMEM;
  1620. goto bad_swap;
  1621. }
  1622. memset(swap_map, 0, maxpages * sizeof(short));
  1623. for (i = 0; i < swap_header->info.nr_badpages; i++) {
  1624. int page_nr = swap_header->info.badpages[i];
  1625. if (page_nr <= 0 || page_nr >= swap_header->info.last_page) {
  1626. error = -EINVAL;
  1627. goto bad_swap;
  1628. }
  1629. swap_map[page_nr] = SWAP_MAP_BAD;
  1630. }
  1631. nr_good_pages = swap_header->info.last_page -
  1632. swap_header->info.nr_badpages -
  1633. 1 /* header page */;
  1634. if (nr_good_pages) {
  1635. swap_map[0] = SWAP_MAP_BAD;
  1636. p->max = maxpages;
  1637. p->pages = nr_good_pages;
  1638. nr_extents = setup_swap_extents(p, &span);
  1639. if (nr_extents < 0) {
  1640. error = nr_extents;
  1641. goto bad_swap;
  1642. }
  1643. nr_good_pages = p->pages;
  1644. }
  1645. if (!nr_good_pages) {
  1646. printk(KERN_WARNING "Empty swap-file\n");
  1647. error = -EINVAL;
  1648. goto bad_swap;
  1649. }
  1650. if (blk_queue_nonrot(bdev_get_queue(p->bdev))) {
  1651. p->flags |= SWP_SOLIDSTATE;
  1652. p->cluster_next = 1 + (random32() % p->highest_bit);
  1653. }
  1654. if (discard_swap(p) == 0)
  1655. p->flags |= SWP_DISCARDABLE;
  1656. mutex_lock(&swapon_mutex);
  1657. spin_lock(&swap_lock);
  1658. if (swap_flags & SWAP_FLAG_PREFER)
  1659. p->prio =
  1660. (swap_flags & SWAP_FLAG_PRIO_MASK) >> SWAP_FLAG_PRIO_SHIFT;
  1661. else
  1662. p->prio = --least_priority;
  1663. p->swap_map = swap_map;
  1664. p->flags |= SWP_WRITEOK;
  1665. nr_swap_pages += nr_good_pages;
  1666. total_swap_pages += nr_good_pages;
  1667. printk(KERN_INFO "Adding %uk swap on %s. "
  1668. "Priority:%d extents:%d across:%lluk %s%s\n",
  1669. nr_good_pages<<(PAGE_SHIFT-10), name, p->prio,
  1670. nr_extents, (unsigned long long)span<<(PAGE_SHIFT-10),
  1671. (p->flags & SWP_SOLIDSTATE) ? "SS" : "",
  1672. (p->flags & SWP_DISCARDABLE) ? "D" : "");
  1673. /* insert swap space into swap_list: */
  1674. prev = -1;
  1675. for (i = swap_list.head; i >= 0; i = swap_info[i].next) {
  1676. if (p->prio >= swap_info[i].prio) {
  1677. break;
  1678. }
  1679. prev = i;
  1680. }
  1681. p->next = i;
  1682. if (prev < 0) {
  1683. swap_list.head = swap_list.next = p - swap_info;
  1684. } else {
  1685. swap_info[prev].next = p - swap_info;
  1686. }
  1687. spin_unlock(&swap_lock);
  1688. mutex_unlock(&swapon_mutex);
  1689. error = 0;
  1690. goto out;
  1691. bad_swap:
  1692. if (bdev) {
  1693. set_blocksize(bdev, p->old_block_size);
  1694. bd_release(bdev);
  1695. }
  1696. destroy_swap_extents(p);
  1697. bad_swap_2:
  1698. spin_lock(&swap_lock);
  1699. p->swap_file = NULL;
  1700. p->flags = 0;
  1701. spin_unlock(&swap_lock);
  1702. vfree(swap_map);
  1703. if (swap_file)
  1704. filp_close(swap_file, NULL);
  1705. out:
  1706. if (page && !IS_ERR(page)) {
  1707. kunmap(page);
  1708. page_cache_release(page);
  1709. }
  1710. if (name)
  1711. putname(name);
  1712. if (did_down) {
  1713. if (!error)
  1714. inode->i_flags |= S_SWAPFILE;
  1715. mutex_unlock(&inode->i_mutex);
  1716. }
  1717. return error;
  1718. }
  1719. void si_swapinfo(struct sysinfo *val)
  1720. {
  1721. unsigned int i;
  1722. unsigned long nr_to_be_unused = 0;
  1723. spin_lock(&swap_lock);
  1724. for (i = 0; i < nr_swapfiles; i++) {
  1725. if (!(swap_info[i].flags & SWP_USED) ||
  1726. (swap_info[i].flags & SWP_WRITEOK))
  1727. continue;
  1728. nr_to_be_unused += swap_info[i].inuse_pages;
  1729. }
  1730. val->freeswap = nr_swap_pages + nr_to_be_unused;
  1731. val->totalswap = total_swap_pages + nr_to_be_unused;
  1732. spin_unlock(&swap_lock);
  1733. }
  1734. /*
  1735. * Verify that a swap entry is valid and increment its swap map count.
  1736. *
  1737. * Note: if swap_map[] reaches SWAP_MAP_MAX the entries are treated as
  1738. * "permanent", but will be reclaimed by the next swapoff.
  1739. */
  1740. int swap_duplicate(swp_entry_t entry)
  1741. {
  1742. struct swap_info_struct * p;
  1743. unsigned long offset, type;
  1744. int result = 0;
  1745. if (is_migration_entry(entry))
  1746. return 1;
  1747. type = swp_type(entry);
  1748. if (type >= nr_swapfiles)
  1749. goto bad_file;
  1750. p = type + swap_info;
  1751. offset = swp_offset(entry);
  1752. spin_lock(&swap_lock);
  1753. if (offset < p->max && p->swap_map[offset]) {
  1754. if (p->swap_map[offset] < SWAP_MAP_MAX - 1) {
  1755. p->swap_map[offset]++;
  1756. result = 1;
  1757. } else if (p->swap_map[offset] <= SWAP_MAP_MAX) {
  1758. if (swap_overflow++ < 5)
  1759. printk(KERN_WARNING "swap_dup: swap entry overflow\n");
  1760. p->swap_map[offset] = SWAP_MAP_MAX;
  1761. result = 1;
  1762. }
  1763. }
  1764. spin_unlock(&swap_lock);
  1765. out:
  1766. return result;
  1767. bad_file:
  1768. printk(KERN_ERR "swap_dup: %s%08lx\n", Bad_file, entry.val);
  1769. goto out;
  1770. }
  1771. struct swap_info_struct *
  1772. get_swap_info_struct(unsigned type)
  1773. {
  1774. return &swap_info[type];
  1775. }
  1776. /*
  1777. * swap_lock prevents swap_map being freed. Don't grab an extra
  1778. * reference on the swaphandle, it doesn't matter if it becomes unused.
  1779. */
  1780. int valid_swaphandles(swp_entry_t entry, unsigned long *offset)
  1781. {
  1782. struct swap_info_struct *si;
  1783. int our_page_cluster = page_cluster;
  1784. pgoff_t target, toff;
  1785. pgoff_t base, end;
  1786. int nr_pages = 0;
  1787. if (!our_page_cluster) /* no readahead */
  1788. return 0;
  1789. si = &swap_info[swp_type(entry)];
  1790. target = swp_offset(entry);
  1791. base = (target >> our_page_cluster) << our_page_cluster;
  1792. end = base + (1 << our_page_cluster);
  1793. if (!base) /* first page is swap header */
  1794. base++;
  1795. spin_lock(&swap_lock);
  1796. if (end > si->max) /* don't go beyond end of map */
  1797. end = si->max;
  1798. /* Count contiguous allocated slots above our target */
  1799. for (toff = target; ++toff < end; nr_pages++) {
  1800. /* Don't read in free or bad pages */
  1801. if (!si->swap_map[toff])
  1802. break;
  1803. if (si->swap_map[toff] == SWAP_MAP_BAD)
  1804. break;
  1805. }
  1806. /* Count contiguous allocated slots below our target */
  1807. for (toff = target; --toff >= base; nr_pages++) {
  1808. /* Don't read in free or bad pages */
  1809. if (!si->swap_map[toff])
  1810. break;
  1811. if (si->swap_map[toff] == SWAP_MAP_BAD)
  1812. break;
  1813. }
  1814. spin_unlock(&swap_lock);
  1815. /*
  1816. * Indicate starting offset, and return number of pages to get:
  1817. * if only 1, say 0, since there's then no readahead to be done.
  1818. */
  1819. *offset = ++toff;
  1820. return nr_pages? ++nr_pages: 0;
  1821. }