swapfile.c 43 KB

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