swapfile.c 49 KB

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