swapfile.c 43 KB

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