swapfile.c 42 KB

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