swapfile.c 41 KB

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