swapfile.c 42 KB

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