swapfile.c 51 KB

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