swapfile.c 49 KB

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