swapfile.c 44 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777
  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. again:
  645. page = read_swap_cache_async(entry, NULL, 0);
  646. if (!page) {
  647. /*
  648. * Either swap_duplicate() failed because entry
  649. * has been freed independently, and will not be
  650. * reused since sys_swapoff() already disabled
  651. * allocation from here, or alloc_page() failed.
  652. */
  653. if (!*swap_map)
  654. continue;
  655. retval = -ENOMEM;
  656. break;
  657. }
  658. /*
  659. * Don't hold on to start_mm if it looks like exiting.
  660. */
  661. if (atomic_read(&start_mm->mm_users) == 1) {
  662. mmput(start_mm);
  663. start_mm = &init_mm;
  664. atomic_inc(&init_mm.mm_users);
  665. }
  666. /*
  667. * Wait for and lock page. When do_swap_page races with
  668. * try_to_unuse, do_swap_page can handle the fault much
  669. * faster than try_to_unuse can locate the entry. This
  670. * apparently redundant "wait_on_page_locked" lets try_to_unuse
  671. * defer to do_swap_page in such a case - in some tests,
  672. * do_swap_page and try_to_unuse repeatedly compete.
  673. */
  674. wait_on_page_locked(page);
  675. wait_on_page_writeback(page);
  676. lock_page(page);
  677. if (!PageSwapCache(page)) {
  678. /* Page migration has occured */
  679. unlock_page(page);
  680. page_cache_release(page);
  681. goto again;
  682. }
  683. wait_on_page_writeback(page);
  684. /*
  685. * Remove all references to entry.
  686. * Whenever we reach init_mm, there's no address space
  687. * to search, but use it as a reminder to search shmem.
  688. */
  689. shmem = 0;
  690. swcount = *swap_map;
  691. if (swcount > 1) {
  692. if (start_mm == &init_mm)
  693. shmem = shmem_unuse(entry, page);
  694. else
  695. retval = unuse_mm(start_mm, entry, page);
  696. }
  697. if (*swap_map > 1) {
  698. int set_start_mm = (*swap_map >= swcount);
  699. struct list_head *p = &start_mm->mmlist;
  700. struct mm_struct *new_start_mm = start_mm;
  701. struct mm_struct *prev_mm = start_mm;
  702. struct mm_struct *mm;
  703. atomic_inc(&new_start_mm->mm_users);
  704. atomic_inc(&prev_mm->mm_users);
  705. spin_lock(&mmlist_lock);
  706. while (*swap_map > 1 && !retval &&
  707. (p = p->next) != &start_mm->mmlist) {
  708. mm = list_entry(p, struct mm_struct, mmlist);
  709. if (atomic_inc_return(&mm->mm_users) == 1) {
  710. atomic_dec(&mm->mm_users);
  711. continue;
  712. }
  713. spin_unlock(&mmlist_lock);
  714. mmput(prev_mm);
  715. prev_mm = mm;
  716. cond_resched();
  717. swcount = *swap_map;
  718. if (swcount <= 1)
  719. ;
  720. else if (mm == &init_mm) {
  721. set_start_mm = 1;
  722. shmem = shmem_unuse(entry, page);
  723. } else
  724. retval = unuse_mm(mm, entry, page);
  725. if (set_start_mm && *swap_map < swcount) {
  726. mmput(new_start_mm);
  727. atomic_inc(&mm->mm_users);
  728. new_start_mm = mm;
  729. set_start_mm = 0;
  730. }
  731. spin_lock(&mmlist_lock);
  732. }
  733. spin_unlock(&mmlist_lock);
  734. mmput(prev_mm);
  735. mmput(start_mm);
  736. start_mm = new_start_mm;
  737. }
  738. if (retval) {
  739. unlock_page(page);
  740. page_cache_release(page);
  741. break;
  742. }
  743. /*
  744. * How could swap count reach 0x7fff when the maximum
  745. * pid is 0x7fff, and there's no way to repeat a swap
  746. * page within an mm (except in shmem, where it's the
  747. * shared object which takes the reference count)?
  748. * We believe SWAP_MAP_MAX cannot occur in Linux 2.4.
  749. *
  750. * If that's wrong, then we should worry more about
  751. * exit_mmap() and do_munmap() cases described above:
  752. * we might be resetting SWAP_MAP_MAX too early here.
  753. * We know "Undead"s can happen, they're okay, so don't
  754. * report them; but do report if we reset SWAP_MAP_MAX.
  755. */
  756. if (*swap_map == SWAP_MAP_MAX) {
  757. spin_lock(&swap_lock);
  758. *swap_map = 1;
  759. spin_unlock(&swap_lock);
  760. reset_overflow = 1;
  761. }
  762. /*
  763. * If a reference remains (rare), we would like to leave
  764. * the page in the swap cache; but try_to_unmap could
  765. * then re-duplicate the entry once we drop page lock,
  766. * so we might loop indefinitely; also, that page could
  767. * not be swapped out to other storage meanwhile. So:
  768. * delete from cache even if there's another reference,
  769. * after ensuring that the data has been saved to disk -
  770. * since if the reference remains (rarer), it will be
  771. * read from disk into another page. Splitting into two
  772. * pages would be incorrect if swap supported "shared
  773. * private" pages, but they are handled by tmpfs files.
  774. *
  775. * Note shmem_unuse already deleted a swappage from
  776. * the swap cache, unless the move to filepage failed:
  777. * in which case it left swappage in cache, lowered its
  778. * swap count to pass quickly through the loops above,
  779. * and now we must reincrement count to try again later.
  780. */
  781. if ((*swap_map > 1) && PageDirty(page) && PageSwapCache(page)) {
  782. struct writeback_control wbc = {
  783. .sync_mode = WB_SYNC_NONE,
  784. };
  785. swap_writepage(page, &wbc);
  786. lock_page(page);
  787. wait_on_page_writeback(page);
  788. }
  789. if (PageSwapCache(page)) {
  790. if (shmem)
  791. swap_duplicate(entry);
  792. else
  793. delete_from_swap_cache(page);
  794. }
  795. /*
  796. * So we could skip searching mms once swap count went
  797. * to 1, we did not mark any present ptes as dirty: must
  798. * mark page dirty so shrink_list will preserve it.
  799. */
  800. SetPageDirty(page);
  801. unlock_page(page);
  802. page_cache_release(page);
  803. /*
  804. * Make sure that we aren't completely killing
  805. * interactive performance.
  806. */
  807. cond_resched();
  808. }
  809. mmput(start_mm);
  810. if (reset_overflow) {
  811. printk(KERN_WARNING "swapoff: cleared swap entry overflow\n");
  812. swap_overflow = 0;
  813. }
  814. return retval;
  815. }
  816. /*
  817. * After a successful try_to_unuse, if no swap is now in use, we know
  818. * we can empty the mmlist. swap_lock must be held on entry and exit.
  819. * Note that mmlist_lock nests inside swap_lock, and an mm must be
  820. * added to the mmlist just after page_duplicate - before would be racy.
  821. */
  822. static void drain_mmlist(void)
  823. {
  824. struct list_head *p, *next;
  825. unsigned int i;
  826. for (i = 0; i < nr_swapfiles; i++)
  827. if (swap_info[i].inuse_pages)
  828. return;
  829. spin_lock(&mmlist_lock);
  830. list_for_each_safe(p, next, &init_mm.mmlist)
  831. list_del_init(p);
  832. spin_unlock(&mmlist_lock);
  833. }
  834. /*
  835. * Use this swapdev's extent info to locate the (PAGE_SIZE) block which
  836. * corresponds to page offset `offset'.
  837. */
  838. sector_t map_swap_page(struct swap_info_struct *sis, pgoff_t offset)
  839. {
  840. struct swap_extent *se = sis->curr_swap_extent;
  841. struct swap_extent *start_se = se;
  842. for ( ; ; ) {
  843. struct list_head *lh;
  844. if (se->start_page <= offset &&
  845. offset < (se->start_page + se->nr_pages)) {
  846. return se->start_block + (offset - se->start_page);
  847. }
  848. lh = se->list.next;
  849. if (lh == &sis->extent_list)
  850. lh = lh->next;
  851. se = list_entry(lh, struct swap_extent, list);
  852. sis->curr_swap_extent = se;
  853. BUG_ON(se == start_se); /* It *must* be present */
  854. }
  855. }
  856. /*
  857. * Free all of a swapdev's extent information
  858. */
  859. static void destroy_swap_extents(struct swap_info_struct *sis)
  860. {
  861. while (!list_empty(&sis->extent_list)) {
  862. struct swap_extent *se;
  863. se = list_entry(sis->extent_list.next,
  864. struct swap_extent, list);
  865. list_del(&se->list);
  866. kfree(se);
  867. }
  868. }
  869. /*
  870. * Add a block range (and the corresponding page range) into this swapdev's
  871. * extent list. The extent list is kept sorted in page order.
  872. *
  873. * This function rather assumes that it is called in ascending page order.
  874. */
  875. static int
  876. add_swap_extent(struct swap_info_struct *sis, unsigned long start_page,
  877. unsigned long nr_pages, sector_t start_block)
  878. {
  879. struct swap_extent *se;
  880. struct swap_extent *new_se;
  881. struct list_head *lh;
  882. lh = sis->extent_list.prev; /* The highest page extent */
  883. if (lh != &sis->extent_list) {
  884. se = list_entry(lh, struct swap_extent, list);
  885. BUG_ON(se->start_page + se->nr_pages != start_page);
  886. if (se->start_block + se->nr_pages == start_block) {
  887. /* Merge it */
  888. se->nr_pages += nr_pages;
  889. return 0;
  890. }
  891. }
  892. /*
  893. * No merge. Insert a new extent, preserving ordering.
  894. */
  895. new_se = kmalloc(sizeof(*se), GFP_KERNEL);
  896. if (new_se == NULL)
  897. return -ENOMEM;
  898. new_se->start_page = start_page;
  899. new_se->nr_pages = nr_pages;
  900. new_se->start_block = start_block;
  901. list_add_tail(&new_se->list, &sis->extent_list);
  902. return 1;
  903. }
  904. /*
  905. * A `swap extent' is a simple thing which maps a contiguous range of pages
  906. * onto a contiguous range of disk blocks. An ordered list of swap extents
  907. * is built at swapon time and is then used at swap_writepage/swap_readpage
  908. * time for locating where on disk a page belongs.
  909. *
  910. * If the swapfile is an S_ISBLK block device, a single extent is installed.
  911. * This is done so that the main operating code can treat S_ISBLK and S_ISREG
  912. * swap files identically.
  913. *
  914. * Whether the swapdev is an S_ISREG file or an S_ISBLK blockdev, the swap
  915. * extent list operates in PAGE_SIZE disk blocks. Both S_ISREG and S_ISBLK
  916. * swapfiles are handled *identically* after swapon time.
  917. *
  918. * For S_ISREG swapfiles, setup_swap_extents() will walk all the file's blocks
  919. * and will parse them into an ordered extent list, in PAGE_SIZE chunks. If
  920. * some stray blocks are found which do not fall within the PAGE_SIZE alignment
  921. * requirements, they are simply tossed out - we will never use those blocks
  922. * for swapping.
  923. *
  924. * For S_ISREG swapfiles we set S_SWAPFILE across the life of the swapon. This
  925. * prevents root from shooting her foot off by ftruncating an in-use swapfile,
  926. * which will scribble on the fs.
  927. *
  928. * The amount of disk space which a single swap extent represents varies.
  929. * Typically it is in the 1-4 megabyte range. So we can have hundreds of
  930. * extents in the list. To avoid much list walking, we cache the previous
  931. * search location in `curr_swap_extent', and start new searches from there.
  932. * This is extremely effective. The average number of iterations in
  933. * map_swap_page() has been measured at about 0.3 per page. - akpm.
  934. */
  935. static int setup_swap_extents(struct swap_info_struct *sis, sector_t *span)
  936. {
  937. struct inode *inode;
  938. unsigned blocks_per_page;
  939. unsigned long page_no;
  940. unsigned blkbits;
  941. sector_t probe_block;
  942. sector_t last_block;
  943. sector_t lowest_block = -1;
  944. sector_t highest_block = 0;
  945. int nr_extents = 0;
  946. int ret;
  947. inode = sis->swap_file->f_mapping->host;
  948. if (S_ISBLK(inode->i_mode)) {
  949. ret = add_swap_extent(sis, 0, sis->max, 0);
  950. *span = sis->pages;
  951. goto done;
  952. }
  953. blkbits = inode->i_blkbits;
  954. blocks_per_page = PAGE_SIZE >> blkbits;
  955. /*
  956. * Map all the blocks into the extent list. This code doesn't try
  957. * to be very smart.
  958. */
  959. probe_block = 0;
  960. page_no = 0;
  961. last_block = i_size_read(inode) >> blkbits;
  962. while ((probe_block + blocks_per_page) <= last_block &&
  963. page_no < sis->max) {
  964. unsigned block_in_page;
  965. sector_t first_block;
  966. first_block = bmap(inode, probe_block);
  967. if (first_block == 0)
  968. goto bad_bmap;
  969. /*
  970. * It must be PAGE_SIZE aligned on-disk
  971. */
  972. if (first_block & (blocks_per_page - 1)) {
  973. probe_block++;
  974. goto reprobe;
  975. }
  976. for (block_in_page = 1; block_in_page < blocks_per_page;
  977. block_in_page++) {
  978. sector_t block;
  979. block = bmap(inode, probe_block + block_in_page);
  980. if (block == 0)
  981. goto bad_bmap;
  982. if (block != first_block + block_in_page) {
  983. /* Discontiguity */
  984. probe_block++;
  985. goto reprobe;
  986. }
  987. }
  988. first_block >>= (PAGE_SHIFT - blkbits);
  989. if (page_no) { /* exclude the header page */
  990. if (first_block < lowest_block)
  991. lowest_block = first_block;
  992. if (first_block > highest_block)
  993. highest_block = first_block;
  994. }
  995. /*
  996. * We found a PAGE_SIZE-length, PAGE_SIZE-aligned run of blocks
  997. */
  998. ret = add_swap_extent(sis, page_no, 1, first_block);
  999. if (ret < 0)
  1000. goto out;
  1001. nr_extents += ret;
  1002. page_no++;
  1003. probe_block += blocks_per_page;
  1004. reprobe:
  1005. continue;
  1006. }
  1007. ret = nr_extents;
  1008. *span = 1 + highest_block - lowest_block;
  1009. if (page_no == 0)
  1010. page_no = 1; /* force Empty message */
  1011. sis->max = page_no;
  1012. sis->pages = page_no - 1;
  1013. sis->highest_bit = page_no - 1;
  1014. done:
  1015. sis->curr_swap_extent = list_entry(sis->extent_list.prev,
  1016. struct swap_extent, list);
  1017. goto out;
  1018. bad_bmap:
  1019. printk(KERN_ERR "swapon: swapfile has holes\n");
  1020. ret = -EINVAL;
  1021. out:
  1022. return ret;
  1023. }
  1024. #if 0 /* We don't need this yet */
  1025. #include <linux/backing-dev.h>
  1026. int page_queue_congested(struct page *page)
  1027. {
  1028. struct backing_dev_info *bdi;
  1029. BUG_ON(!PageLocked(page)); /* It pins the swap_info_struct */
  1030. if (PageSwapCache(page)) {
  1031. swp_entry_t entry = { .val = page_private(page) };
  1032. struct swap_info_struct *sis;
  1033. sis = get_swap_info_struct(swp_type(entry));
  1034. bdi = sis->bdev->bd_inode->i_mapping->backing_dev_info;
  1035. } else
  1036. bdi = page->mapping->backing_dev_info;
  1037. return bdi_write_congested(bdi);
  1038. }
  1039. #endif
  1040. asmlinkage long sys_swapoff(const char __user * specialfile)
  1041. {
  1042. struct swap_info_struct * p = NULL;
  1043. unsigned short *swap_map;
  1044. struct file *swap_file, *victim;
  1045. struct address_space *mapping;
  1046. struct inode *inode;
  1047. char * pathname;
  1048. int i, type, prev;
  1049. int err;
  1050. if (!capable(CAP_SYS_ADMIN))
  1051. return -EPERM;
  1052. pathname = getname(specialfile);
  1053. err = PTR_ERR(pathname);
  1054. if (IS_ERR(pathname))
  1055. goto out;
  1056. victim = filp_open(pathname, O_RDWR|O_LARGEFILE, 0);
  1057. putname(pathname);
  1058. err = PTR_ERR(victim);
  1059. if (IS_ERR(victim))
  1060. goto out;
  1061. mapping = victim->f_mapping;
  1062. prev = -1;
  1063. spin_lock(&swap_lock);
  1064. for (type = swap_list.head; type >= 0; type = swap_info[type].next) {
  1065. p = swap_info + type;
  1066. if ((p->flags & SWP_ACTIVE) == SWP_ACTIVE) {
  1067. if (p->swap_file->f_mapping == mapping)
  1068. break;
  1069. }
  1070. prev = type;
  1071. }
  1072. if (type < 0) {
  1073. err = -EINVAL;
  1074. spin_unlock(&swap_lock);
  1075. goto out_dput;
  1076. }
  1077. if (!security_vm_enough_memory(p->pages))
  1078. vm_unacct_memory(p->pages);
  1079. else {
  1080. err = -ENOMEM;
  1081. spin_unlock(&swap_lock);
  1082. goto out_dput;
  1083. }
  1084. if (prev < 0) {
  1085. swap_list.head = p->next;
  1086. } else {
  1087. swap_info[prev].next = p->next;
  1088. }
  1089. if (type == swap_list.next) {
  1090. /* just pick something that's safe... */
  1091. swap_list.next = swap_list.head;
  1092. }
  1093. nr_swap_pages -= p->pages;
  1094. total_swap_pages -= p->pages;
  1095. p->flags &= ~SWP_WRITEOK;
  1096. spin_unlock(&swap_lock);
  1097. current->flags |= PF_SWAPOFF;
  1098. err = try_to_unuse(type);
  1099. current->flags &= ~PF_SWAPOFF;
  1100. if (err) {
  1101. /* re-insert swap space back into swap_list */
  1102. spin_lock(&swap_lock);
  1103. for (prev = -1, i = swap_list.head; i >= 0; prev = i, i = swap_info[i].next)
  1104. if (p->prio >= swap_info[i].prio)
  1105. break;
  1106. p->next = i;
  1107. if (prev < 0)
  1108. swap_list.head = swap_list.next = p - swap_info;
  1109. else
  1110. swap_info[prev].next = p - swap_info;
  1111. nr_swap_pages += p->pages;
  1112. total_swap_pages += p->pages;
  1113. p->flags |= SWP_WRITEOK;
  1114. spin_unlock(&swap_lock);
  1115. goto out_dput;
  1116. }
  1117. /* wait for any unplug function to finish */
  1118. down_write(&swap_unplug_sem);
  1119. up_write(&swap_unplug_sem);
  1120. destroy_swap_extents(p);
  1121. mutex_lock(&swapon_mutex);
  1122. spin_lock(&swap_lock);
  1123. drain_mmlist();
  1124. /* wait for anyone still in scan_swap_map */
  1125. p->highest_bit = 0; /* cuts scans short */
  1126. while (p->flags >= SWP_SCANNING) {
  1127. spin_unlock(&swap_lock);
  1128. schedule_timeout_uninterruptible(1);
  1129. spin_lock(&swap_lock);
  1130. }
  1131. swap_file = p->swap_file;
  1132. p->swap_file = NULL;
  1133. p->max = 0;
  1134. swap_map = p->swap_map;
  1135. p->swap_map = NULL;
  1136. p->flags = 0;
  1137. spin_unlock(&swap_lock);
  1138. mutex_unlock(&swapon_mutex);
  1139. vfree(swap_map);
  1140. inode = mapping->host;
  1141. if (S_ISBLK(inode->i_mode)) {
  1142. struct block_device *bdev = I_BDEV(inode);
  1143. set_blocksize(bdev, p->old_block_size);
  1144. bd_release(bdev);
  1145. } else {
  1146. mutex_lock(&inode->i_mutex);
  1147. inode->i_flags &= ~S_SWAPFILE;
  1148. mutex_unlock(&inode->i_mutex);
  1149. }
  1150. filp_close(swap_file, NULL);
  1151. err = 0;
  1152. out_dput:
  1153. filp_close(victim, NULL);
  1154. out:
  1155. return err;
  1156. }
  1157. #ifdef CONFIG_PROC_FS
  1158. /* iterator */
  1159. static void *swap_start(struct seq_file *swap, loff_t *pos)
  1160. {
  1161. struct swap_info_struct *ptr = swap_info;
  1162. int i;
  1163. loff_t l = *pos;
  1164. mutex_lock(&swapon_mutex);
  1165. for (i = 0; i < nr_swapfiles; i++, ptr++) {
  1166. if (!(ptr->flags & SWP_USED) || !ptr->swap_map)
  1167. continue;
  1168. if (!l--)
  1169. return ptr;
  1170. }
  1171. return NULL;
  1172. }
  1173. static void *swap_next(struct seq_file *swap, void *v, loff_t *pos)
  1174. {
  1175. struct swap_info_struct *ptr = v;
  1176. struct swap_info_struct *endptr = swap_info + nr_swapfiles;
  1177. for (++ptr; ptr < endptr; ptr++) {
  1178. if (!(ptr->flags & SWP_USED) || !ptr->swap_map)
  1179. continue;
  1180. ++*pos;
  1181. return ptr;
  1182. }
  1183. return NULL;
  1184. }
  1185. static void swap_stop(struct seq_file *swap, void *v)
  1186. {
  1187. mutex_unlock(&swapon_mutex);
  1188. }
  1189. static int swap_show(struct seq_file *swap, void *v)
  1190. {
  1191. struct swap_info_struct *ptr = v;
  1192. struct file *file;
  1193. int len;
  1194. if (v == swap_info)
  1195. seq_puts(swap, "Filename\t\t\t\tType\t\tSize\tUsed\tPriority\n");
  1196. file = ptr->swap_file;
  1197. len = seq_path(swap, file->f_vfsmnt, file->f_dentry, " \t\n\\");
  1198. seq_printf(swap, "%*s%s\t%u\t%u\t%d\n",
  1199. len < 40 ? 40 - len : 1, " ",
  1200. S_ISBLK(file->f_dentry->d_inode->i_mode) ?
  1201. "partition" : "file\t",
  1202. ptr->pages << (PAGE_SHIFT - 10),
  1203. ptr->inuse_pages << (PAGE_SHIFT - 10),
  1204. ptr->prio);
  1205. return 0;
  1206. }
  1207. static struct seq_operations swaps_op = {
  1208. .start = swap_start,
  1209. .next = swap_next,
  1210. .stop = swap_stop,
  1211. .show = swap_show
  1212. };
  1213. static int swaps_open(struct inode *inode, struct file *file)
  1214. {
  1215. return seq_open(file, &swaps_op);
  1216. }
  1217. static struct file_operations proc_swaps_operations = {
  1218. .open = swaps_open,
  1219. .read = seq_read,
  1220. .llseek = seq_lseek,
  1221. .release = seq_release,
  1222. };
  1223. static int __init procswaps_init(void)
  1224. {
  1225. struct proc_dir_entry *entry;
  1226. entry = create_proc_entry("swaps", 0, NULL);
  1227. if (entry)
  1228. entry->proc_fops = &proc_swaps_operations;
  1229. return 0;
  1230. }
  1231. __initcall(procswaps_init);
  1232. #endif /* CONFIG_PROC_FS */
  1233. /*
  1234. * Written 01/25/92 by Simmule Turner, heavily changed by Linus.
  1235. *
  1236. * The swapon system call
  1237. */
  1238. asmlinkage long sys_swapon(const char __user * specialfile, int swap_flags)
  1239. {
  1240. struct swap_info_struct * p;
  1241. char *name = NULL;
  1242. struct block_device *bdev = NULL;
  1243. struct file *swap_file = NULL;
  1244. struct address_space *mapping;
  1245. unsigned int type;
  1246. int i, prev;
  1247. int error;
  1248. static int least_priority;
  1249. union swap_header *swap_header = NULL;
  1250. int swap_header_version;
  1251. unsigned int nr_good_pages = 0;
  1252. int nr_extents = 0;
  1253. sector_t span;
  1254. unsigned long maxpages = 1;
  1255. int swapfilesize;
  1256. unsigned short *swap_map;
  1257. struct page *page = NULL;
  1258. struct inode *inode = NULL;
  1259. int did_down = 0;
  1260. if (!capable(CAP_SYS_ADMIN))
  1261. return -EPERM;
  1262. spin_lock(&swap_lock);
  1263. p = swap_info;
  1264. for (type = 0 ; type < nr_swapfiles ; type++,p++)
  1265. if (!(p->flags & SWP_USED))
  1266. break;
  1267. error = -EPERM;
  1268. /*
  1269. * Test if adding another swap device is possible. There are
  1270. * two limiting factors: 1) the number of bits for the swap
  1271. * type swp_entry_t definition and 2) the number of bits for
  1272. * the swap type in the swap ptes as defined by the different
  1273. * architectures. To honor both limitations a swap entry
  1274. * with swap offset 0 and swap type ~0UL is created, encoded
  1275. * to a swap pte, decoded to a swp_entry_t again and finally
  1276. * the swap type part is extracted. This will mask all bits
  1277. * from the initial ~0UL that can't be encoded in either the
  1278. * swp_entry_t or the architecture definition of a swap pte.
  1279. */
  1280. if (type > swp_type(pte_to_swp_entry(swp_entry_to_pte(swp_entry(~0UL,0))))) {
  1281. spin_unlock(&swap_lock);
  1282. goto out;
  1283. }
  1284. if (type >= nr_swapfiles)
  1285. nr_swapfiles = type+1;
  1286. INIT_LIST_HEAD(&p->extent_list);
  1287. p->flags = SWP_USED;
  1288. p->swap_file = NULL;
  1289. p->old_block_size = 0;
  1290. p->swap_map = NULL;
  1291. p->lowest_bit = 0;
  1292. p->highest_bit = 0;
  1293. p->cluster_nr = 0;
  1294. p->inuse_pages = 0;
  1295. p->next = -1;
  1296. if (swap_flags & SWAP_FLAG_PREFER) {
  1297. p->prio =
  1298. (swap_flags & SWAP_FLAG_PRIO_MASK)>>SWAP_FLAG_PRIO_SHIFT;
  1299. } else {
  1300. p->prio = --least_priority;
  1301. }
  1302. spin_unlock(&swap_lock);
  1303. name = getname(specialfile);
  1304. error = PTR_ERR(name);
  1305. if (IS_ERR(name)) {
  1306. name = NULL;
  1307. goto bad_swap_2;
  1308. }
  1309. swap_file = filp_open(name, O_RDWR|O_LARGEFILE, 0);
  1310. error = PTR_ERR(swap_file);
  1311. if (IS_ERR(swap_file)) {
  1312. swap_file = NULL;
  1313. goto bad_swap_2;
  1314. }
  1315. p->swap_file = swap_file;
  1316. mapping = swap_file->f_mapping;
  1317. inode = mapping->host;
  1318. error = -EBUSY;
  1319. for (i = 0; i < nr_swapfiles; i++) {
  1320. struct swap_info_struct *q = &swap_info[i];
  1321. if (i == type || !q->swap_file)
  1322. continue;
  1323. if (mapping == q->swap_file->f_mapping)
  1324. goto bad_swap;
  1325. }
  1326. error = -EINVAL;
  1327. if (S_ISBLK(inode->i_mode)) {
  1328. bdev = I_BDEV(inode);
  1329. error = bd_claim(bdev, sys_swapon);
  1330. if (error < 0) {
  1331. bdev = NULL;
  1332. error = -EINVAL;
  1333. goto bad_swap;
  1334. }
  1335. p->old_block_size = block_size(bdev);
  1336. error = set_blocksize(bdev, PAGE_SIZE);
  1337. if (error < 0)
  1338. goto bad_swap;
  1339. p->bdev = bdev;
  1340. } else if (S_ISREG(inode->i_mode)) {
  1341. p->bdev = inode->i_sb->s_bdev;
  1342. mutex_lock(&inode->i_mutex);
  1343. did_down = 1;
  1344. if (IS_SWAPFILE(inode)) {
  1345. error = -EBUSY;
  1346. goto bad_swap;
  1347. }
  1348. } else {
  1349. goto bad_swap;
  1350. }
  1351. swapfilesize = i_size_read(inode) >> PAGE_SHIFT;
  1352. /*
  1353. * Read the swap header.
  1354. */
  1355. if (!mapping->a_ops->readpage) {
  1356. error = -EINVAL;
  1357. goto bad_swap;
  1358. }
  1359. page = read_cache_page(mapping, 0,
  1360. (filler_t *)mapping->a_ops->readpage, swap_file);
  1361. if (IS_ERR(page)) {
  1362. error = PTR_ERR(page);
  1363. goto bad_swap;
  1364. }
  1365. wait_on_page_locked(page);
  1366. if (!PageUptodate(page))
  1367. goto bad_swap;
  1368. kmap(page);
  1369. swap_header = page_address(page);
  1370. if (!memcmp("SWAP-SPACE",swap_header->magic.magic,10))
  1371. swap_header_version = 1;
  1372. else if (!memcmp("SWAPSPACE2",swap_header->magic.magic,10))
  1373. swap_header_version = 2;
  1374. else {
  1375. printk(KERN_ERR "Unable to find swap-space signature\n");
  1376. error = -EINVAL;
  1377. goto bad_swap;
  1378. }
  1379. switch (swap_header_version) {
  1380. case 1:
  1381. printk(KERN_ERR "version 0 swap is no longer supported. "
  1382. "Use mkswap -v1 %s\n", name);
  1383. error = -EINVAL;
  1384. goto bad_swap;
  1385. case 2:
  1386. /* Check the swap header's sub-version and the size of
  1387. the swap file and bad block lists */
  1388. if (swap_header->info.version != 1) {
  1389. printk(KERN_WARNING
  1390. "Unable to handle swap header version %d\n",
  1391. swap_header->info.version);
  1392. error = -EINVAL;
  1393. goto bad_swap;
  1394. }
  1395. p->lowest_bit = 1;
  1396. p->cluster_next = 1;
  1397. /*
  1398. * Find out how many pages are allowed for a single swap
  1399. * device. There are two limiting factors: 1) the number of
  1400. * bits for the swap offset in the swp_entry_t type and
  1401. * 2) the number of bits in the a swap pte as defined by
  1402. * the different architectures. In order to find the
  1403. * largest possible bit mask a swap entry with swap type 0
  1404. * and swap offset ~0UL is created, encoded to a swap pte,
  1405. * decoded to a swp_entry_t again and finally the swap
  1406. * offset is extracted. This will mask all the bits from
  1407. * the initial ~0UL mask that can't be encoded in either
  1408. * the swp_entry_t or the architecture definition of a
  1409. * swap pte.
  1410. */
  1411. maxpages = swp_offset(pte_to_swp_entry(swp_entry_to_pte(swp_entry(0,~0UL)))) - 1;
  1412. if (maxpages > swap_header->info.last_page)
  1413. maxpages = swap_header->info.last_page;
  1414. p->highest_bit = maxpages - 1;
  1415. error = -EINVAL;
  1416. if (!maxpages)
  1417. goto bad_swap;
  1418. if (swap_header->info.nr_badpages && S_ISREG(inode->i_mode))
  1419. goto bad_swap;
  1420. if (swap_header->info.nr_badpages > MAX_SWAP_BADPAGES)
  1421. goto bad_swap;
  1422. /* OK, set up the swap map and apply the bad block list */
  1423. if (!(p->swap_map = vmalloc(maxpages * sizeof(short)))) {
  1424. error = -ENOMEM;
  1425. goto bad_swap;
  1426. }
  1427. error = 0;
  1428. memset(p->swap_map, 0, maxpages * sizeof(short));
  1429. for (i = 0; i < swap_header->info.nr_badpages; i++) {
  1430. int page_nr = swap_header->info.badpages[i];
  1431. if (page_nr <= 0 || page_nr >= swap_header->info.last_page)
  1432. error = -EINVAL;
  1433. else
  1434. p->swap_map[page_nr] = SWAP_MAP_BAD;
  1435. }
  1436. nr_good_pages = swap_header->info.last_page -
  1437. swap_header->info.nr_badpages -
  1438. 1 /* header page */;
  1439. if (error)
  1440. goto bad_swap;
  1441. }
  1442. if (swapfilesize && maxpages > swapfilesize) {
  1443. printk(KERN_WARNING
  1444. "Swap area shorter than signature indicates\n");
  1445. error = -EINVAL;
  1446. goto bad_swap;
  1447. }
  1448. if (nr_good_pages) {
  1449. p->swap_map[0] = SWAP_MAP_BAD;
  1450. p->max = maxpages;
  1451. p->pages = nr_good_pages;
  1452. nr_extents = setup_swap_extents(p, &span);
  1453. if (nr_extents < 0) {
  1454. error = nr_extents;
  1455. goto bad_swap;
  1456. }
  1457. nr_good_pages = p->pages;
  1458. }
  1459. if (!nr_good_pages) {
  1460. printk(KERN_WARNING "Empty swap-file\n");
  1461. error = -EINVAL;
  1462. goto bad_swap;
  1463. }
  1464. mutex_lock(&swapon_mutex);
  1465. spin_lock(&swap_lock);
  1466. p->flags = SWP_ACTIVE;
  1467. nr_swap_pages += nr_good_pages;
  1468. total_swap_pages += nr_good_pages;
  1469. printk(KERN_INFO "Adding %uk swap on %s. "
  1470. "Priority:%d extents:%d across:%lluk\n",
  1471. nr_good_pages<<(PAGE_SHIFT-10), name, p->prio,
  1472. nr_extents, (unsigned long long)span<<(PAGE_SHIFT-10));
  1473. /* insert swap space into swap_list: */
  1474. prev = -1;
  1475. for (i = swap_list.head; i >= 0; i = swap_info[i].next) {
  1476. if (p->prio >= swap_info[i].prio) {
  1477. break;
  1478. }
  1479. prev = i;
  1480. }
  1481. p->next = i;
  1482. if (prev < 0) {
  1483. swap_list.head = swap_list.next = p - swap_info;
  1484. } else {
  1485. swap_info[prev].next = p - swap_info;
  1486. }
  1487. spin_unlock(&swap_lock);
  1488. mutex_unlock(&swapon_mutex);
  1489. error = 0;
  1490. goto out;
  1491. bad_swap:
  1492. if (bdev) {
  1493. set_blocksize(bdev, p->old_block_size);
  1494. bd_release(bdev);
  1495. }
  1496. destroy_swap_extents(p);
  1497. bad_swap_2:
  1498. spin_lock(&swap_lock);
  1499. swap_map = p->swap_map;
  1500. p->swap_file = NULL;
  1501. p->swap_map = NULL;
  1502. p->flags = 0;
  1503. if (!(swap_flags & SWAP_FLAG_PREFER))
  1504. ++least_priority;
  1505. spin_unlock(&swap_lock);
  1506. vfree(swap_map);
  1507. if (swap_file)
  1508. filp_close(swap_file, NULL);
  1509. out:
  1510. if (page && !IS_ERR(page)) {
  1511. kunmap(page);
  1512. page_cache_release(page);
  1513. }
  1514. if (name)
  1515. putname(name);
  1516. if (did_down) {
  1517. if (!error)
  1518. inode->i_flags |= S_SWAPFILE;
  1519. mutex_unlock(&inode->i_mutex);
  1520. }
  1521. return error;
  1522. }
  1523. void si_swapinfo(struct sysinfo *val)
  1524. {
  1525. unsigned int i;
  1526. unsigned long nr_to_be_unused = 0;
  1527. spin_lock(&swap_lock);
  1528. for (i = 0; i < nr_swapfiles; i++) {
  1529. if (!(swap_info[i].flags & SWP_USED) ||
  1530. (swap_info[i].flags & SWP_WRITEOK))
  1531. continue;
  1532. nr_to_be_unused += swap_info[i].inuse_pages;
  1533. }
  1534. val->freeswap = nr_swap_pages + nr_to_be_unused;
  1535. val->totalswap = total_swap_pages + nr_to_be_unused;
  1536. spin_unlock(&swap_lock);
  1537. }
  1538. /*
  1539. * Verify that a swap entry is valid and increment its swap map count.
  1540. *
  1541. * Note: if swap_map[] reaches SWAP_MAP_MAX the entries are treated as
  1542. * "permanent", but will be reclaimed by the next swapoff.
  1543. */
  1544. int swap_duplicate(swp_entry_t entry)
  1545. {
  1546. struct swap_info_struct * p;
  1547. unsigned long offset, type;
  1548. int result = 0;
  1549. type = swp_type(entry);
  1550. if (type >= nr_swapfiles)
  1551. goto bad_file;
  1552. p = type + swap_info;
  1553. offset = swp_offset(entry);
  1554. spin_lock(&swap_lock);
  1555. if (offset < p->max && p->swap_map[offset]) {
  1556. if (p->swap_map[offset] < SWAP_MAP_MAX - 1) {
  1557. p->swap_map[offset]++;
  1558. result = 1;
  1559. } else if (p->swap_map[offset] <= SWAP_MAP_MAX) {
  1560. if (swap_overflow++ < 5)
  1561. printk(KERN_WARNING "swap_dup: swap entry overflow\n");
  1562. p->swap_map[offset] = SWAP_MAP_MAX;
  1563. result = 1;
  1564. }
  1565. }
  1566. spin_unlock(&swap_lock);
  1567. out:
  1568. return result;
  1569. bad_file:
  1570. printk(KERN_ERR "swap_dup: %s%08lx\n", Bad_file, entry.val);
  1571. goto out;
  1572. }
  1573. struct swap_info_struct *
  1574. get_swap_info_struct(unsigned type)
  1575. {
  1576. return &swap_info[type];
  1577. }
  1578. /*
  1579. * swap_lock prevents swap_map being freed. Don't grab an extra
  1580. * reference on the swaphandle, it doesn't matter if it becomes unused.
  1581. */
  1582. int valid_swaphandles(swp_entry_t entry, unsigned long *offset)
  1583. {
  1584. int ret = 0, i = 1 << page_cluster;
  1585. unsigned long toff;
  1586. struct swap_info_struct *swapdev = swp_type(entry) + swap_info;
  1587. if (!page_cluster) /* no readahead */
  1588. return 0;
  1589. toff = (swp_offset(entry) >> page_cluster) << page_cluster;
  1590. if (!toff) /* first page is swap header */
  1591. toff++, i--;
  1592. *offset = toff;
  1593. spin_lock(&swap_lock);
  1594. do {
  1595. /* Don't read-ahead past the end of the swap area */
  1596. if (toff >= swapdev->max)
  1597. break;
  1598. /* Don't read in free or bad pages */
  1599. if (!swapdev->swap_map[toff])
  1600. break;
  1601. if (swapdev->swap_map[toff] == SWAP_MAP_BAD)
  1602. break;
  1603. toff++;
  1604. ret++;
  1605. } while (--i);
  1606. spin_unlock(&swap_lock);
  1607. return ret;
  1608. }