swapfile.c 44 KB

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