page-writeback.c 37 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339
  1. /*
  2. * mm/page-writeback.c
  3. *
  4. * Copyright (C) 2002, Linus Torvalds.
  5. * Copyright (C) 2007 Red Hat, Inc., Peter Zijlstra <pzijlstr@redhat.com>
  6. *
  7. * Contains functions related to writing back dirty pages at the
  8. * address_space level.
  9. *
  10. * 10Apr2002 Andrew Morton
  11. * Initial version
  12. */
  13. #include <linux/kernel.h>
  14. #include <linux/module.h>
  15. #include <linux/spinlock.h>
  16. #include <linux/fs.h>
  17. #include <linux/mm.h>
  18. #include <linux/swap.h>
  19. #include <linux/slab.h>
  20. #include <linux/pagemap.h>
  21. #include <linux/writeback.h>
  22. #include <linux/init.h>
  23. #include <linux/backing-dev.h>
  24. #include <linux/task_io_accounting_ops.h>
  25. #include <linux/blkdev.h>
  26. #include <linux/mpage.h>
  27. #include <linux/rmap.h>
  28. #include <linux/percpu.h>
  29. #include <linux/notifier.h>
  30. #include <linux/smp.h>
  31. #include <linux/sysctl.h>
  32. #include <linux/cpu.h>
  33. #include <linux/syscalls.h>
  34. #include <linux/buffer_head.h>
  35. #include <linux/pagevec.h>
  36. /*
  37. * The maximum number of pages to writeout in a single bdflush/kupdate
  38. * operation. We do this so we don't hold I_SYNC against an inode for
  39. * enormous amounts of time, which would block a userspace task which has
  40. * been forced to throttle against that inode. Also, the code reevaluates
  41. * the dirty each time it has written this many pages.
  42. */
  43. #define MAX_WRITEBACK_PAGES 1024
  44. /*
  45. * After a CPU has dirtied this many pages, balance_dirty_pages_ratelimited
  46. * will look to see if it needs to force writeback or throttling.
  47. */
  48. static long ratelimit_pages = 32;
  49. /*
  50. * When balance_dirty_pages decides that the caller needs to perform some
  51. * non-background writeback, this is how many pages it will attempt to write.
  52. * It should be somewhat larger than RATELIMIT_PAGES to ensure that reasonably
  53. * large amounts of I/O are submitted.
  54. */
  55. static inline long sync_writeback_pages(void)
  56. {
  57. return ratelimit_pages + ratelimit_pages / 2;
  58. }
  59. /* The following parameters are exported via /proc/sys/vm */
  60. /*
  61. * Start background writeback (via pdflush) at this percentage
  62. */
  63. int dirty_background_ratio = 5;
  64. /*
  65. * free highmem will not be subtracted from the total free memory
  66. * for calculating free ratios if vm_highmem_is_dirtyable is true
  67. */
  68. int vm_highmem_is_dirtyable;
  69. /*
  70. * The generator of dirty data starts writeback at this percentage
  71. */
  72. int vm_dirty_ratio = 10;
  73. /*
  74. * The interval between `kupdate'-style writebacks, in jiffies
  75. */
  76. int dirty_writeback_interval = 5 * HZ;
  77. /*
  78. * The longest number of jiffies for which data is allowed to remain dirty
  79. */
  80. int dirty_expire_interval = 30 * HZ;
  81. /*
  82. * Flag that makes the machine dump writes/reads and block dirtyings.
  83. */
  84. int block_dump;
  85. /*
  86. * Flag that puts the machine in "laptop mode". Doubles as a timeout in jiffies:
  87. * a full sync is triggered after this time elapses without any disk activity.
  88. */
  89. int laptop_mode;
  90. EXPORT_SYMBOL(laptop_mode);
  91. /* End of sysctl-exported parameters */
  92. static void background_writeout(unsigned long _min_pages);
  93. /*
  94. * Scale the writeback cache size proportional to the relative writeout speeds.
  95. *
  96. * We do this by keeping a floating proportion between BDIs, based on page
  97. * writeback completions [end_page_writeback()]. Those devices that write out
  98. * pages fastest will get the larger share, while the slower will get a smaller
  99. * share.
  100. *
  101. * We use page writeout completions because we are interested in getting rid of
  102. * dirty pages. Having them written out is the primary goal.
  103. *
  104. * We introduce a concept of time, a period over which we measure these events,
  105. * because demand can/will vary over time. The length of this period itself is
  106. * measured in page writeback completions.
  107. *
  108. */
  109. static struct prop_descriptor vm_completions;
  110. static struct prop_descriptor vm_dirties;
  111. /*
  112. * couple the period to the dirty_ratio:
  113. *
  114. * period/2 ~ roundup_pow_of_two(dirty limit)
  115. */
  116. static int calc_period_shift(void)
  117. {
  118. unsigned long dirty_total;
  119. dirty_total = (vm_dirty_ratio * determine_dirtyable_memory()) / 100;
  120. return 2 + ilog2(dirty_total - 1);
  121. }
  122. /*
  123. * update the period when the dirty ratio changes.
  124. */
  125. int dirty_ratio_handler(struct ctl_table *table, int write,
  126. struct file *filp, void __user *buffer, size_t *lenp,
  127. loff_t *ppos)
  128. {
  129. int old_ratio = vm_dirty_ratio;
  130. int ret = proc_dointvec_minmax(table, write, filp, buffer, lenp, ppos);
  131. if (ret == 0 && write && vm_dirty_ratio != old_ratio) {
  132. int shift = calc_period_shift();
  133. prop_change_shift(&vm_completions, shift);
  134. prop_change_shift(&vm_dirties, shift);
  135. }
  136. return ret;
  137. }
  138. /*
  139. * Increment the BDI's writeout completion count and the global writeout
  140. * completion count. Called from test_clear_page_writeback().
  141. */
  142. static inline void __bdi_writeout_inc(struct backing_dev_info *bdi)
  143. {
  144. __prop_inc_percpu_max(&vm_completions, &bdi->completions,
  145. bdi->max_prop_frac);
  146. }
  147. void bdi_writeout_inc(struct backing_dev_info *bdi)
  148. {
  149. unsigned long flags;
  150. local_irq_save(flags);
  151. __bdi_writeout_inc(bdi);
  152. local_irq_restore(flags);
  153. }
  154. EXPORT_SYMBOL_GPL(bdi_writeout_inc);
  155. static inline void task_dirty_inc(struct task_struct *tsk)
  156. {
  157. prop_inc_single(&vm_dirties, &tsk->dirties);
  158. }
  159. /*
  160. * Obtain an accurate fraction of the BDI's portion.
  161. */
  162. static void bdi_writeout_fraction(struct backing_dev_info *bdi,
  163. long *numerator, long *denominator)
  164. {
  165. if (bdi_cap_writeback_dirty(bdi)) {
  166. prop_fraction_percpu(&vm_completions, &bdi->completions,
  167. numerator, denominator);
  168. } else {
  169. *numerator = 0;
  170. *denominator = 1;
  171. }
  172. }
  173. /*
  174. * Clip the earned share of dirty pages to that which is actually available.
  175. * This avoids exceeding the total dirty_limit when the floating averages
  176. * fluctuate too quickly.
  177. */
  178. static void
  179. clip_bdi_dirty_limit(struct backing_dev_info *bdi, long dirty, long *pbdi_dirty)
  180. {
  181. long avail_dirty;
  182. avail_dirty = dirty -
  183. (global_page_state(NR_FILE_DIRTY) +
  184. global_page_state(NR_WRITEBACK) +
  185. global_page_state(NR_UNSTABLE_NFS) +
  186. global_page_state(NR_WRITEBACK_TEMP));
  187. if (avail_dirty < 0)
  188. avail_dirty = 0;
  189. avail_dirty += bdi_stat(bdi, BDI_RECLAIMABLE) +
  190. bdi_stat(bdi, BDI_WRITEBACK);
  191. *pbdi_dirty = min(*pbdi_dirty, avail_dirty);
  192. }
  193. static inline void task_dirties_fraction(struct task_struct *tsk,
  194. long *numerator, long *denominator)
  195. {
  196. prop_fraction_single(&vm_dirties, &tsk->dirties,
  197. numerator, denominator);
  198. }
  199. /*
  200. * scale the dirty limit
  201. *
  202. * task specific dirty limit:
  203. *
  204. * dirty -= (dirty/8) * p_{t}
  205. */
  206. static void task_dirty_limit(struct task_struct *tsk, long *pdirty)
  207. {
  208. long numerator, denominator;
  209. long dirty = *pdirty;
  210. u64 inv = dirty >> 3;
  211. task_dirties_fraction(tsk, &numerator, &denominator);
  212. inv *= numerator;
  213. do_div(inv, denominator);
  214. dirty -= inv;
  215. if (dirty < *pdirty/2)
  216. dirty = *pdirty/2;
  217. *pdirty = dirty;
  218. }
  219. /*
  220. *
  221. */
  222. static DEFINE_SPINLOCK(bdi_lock);
  223. static unsigned int bdi_min_ratio;
  224. int bdi_set_min_ratio(struct backing_dev_info *bdi, unsigned int min_ratio)
  225. {
  226. int ret = 0;
  227. unsigned long flags;
  228. spin_lock_irqsave(&bdi_lock, flags);
  229. if (min_ratio > bdi->max_ratio) {
  230. ret = -EINVAL;
  231. } else {
  232. min_ratio -= bdi->min_ratio;
  233. if (bdi_min_ratio + min_ratio < 100) {
  234. bdi_min_ratio += min_ratio;
  235. bdi->min_ratio += min_ratio;
  236. } else {
  237. ret = -EINVAL;
  238. }
  239. }
  240. spin_unlock_irqrestore(&bdi_lock, flags);
  241. return ret;
  242. }
  243. int bdi_set_max_ratio(struct backing_dev_info *bdi, unsigned max_ratio)
  244. {
  245. unsigned long flags;
  246. int ret = 0;
  247. if (max_ratio > 100)
  248. return -EINVAL;
  249. spin_lock_irqsave(&bdi_lock, flags);
  250. if (bdi->min_ratio > max_ratio) {
  251. ret = -EINVAL;
  252. } else {
  253. bdi->max_ratio = max_ratio;
  254. bdi->max_prop_frac = (PROP_FRAC_BASE * max_ratio) / 100;
  255. }
  256. spin_unlock_irqrestore(&bdi_lock, flags);
  257. return ret;
  258. }
  259. EXPORT_SYMBOL(bdi_set_max_ratio);
  260. /*
  261. * Work out the current dirty-memory clamping and background writeout
  262. * thresholds.
  263. *
  264. * The main aim here is to lower them aggressively if there is a lot of mapped
  265. * memory around. To avoid stressing page reclaim with lots of unreclaimable
  266. * pages. It is better to clamp down on writers than to start swapping, and
  267. * performing lots of scanning.
  268. *
  269. * We only allow 1/2 of the currently-unmapped memory to be dirtied.
  270. *
  271. * We don't permit the clamping level to fall below 5% - that is getting rather
  272. * excessive.
  273. *
  274. * We make sure that the background writeout level is below the adjusted
  275. * clamping level.
  276. */
  277. static unsigned long highmem_dirtyable_memory(unsigned long total)
  278. {
  279. #ifdef CONFIG_HIGHMEM
  280. int node;
  281. unsigned long x = 0;
  282. for_each_node_state(node, N_HIGH_MEMORY) {
  283. struct zone *z =
  284. &NODE_DATA(node)->node_zones[ZONE_HIGHMEM];
  285. x += zone_page_state(z, NR_FREE_PAGES) + zone_lru_pages(z);
  286. }
  287. /*
  288. * Make sure that the number of highmem pages is never larger
  289. * than the number of the total dirtyable memory. This can only
  290. * occur in very strange VM situations but we want to make sure
  291. * that this does not occur.
  292. */
  293. return min(x, total);
  294. #else
  295. return 0;
  296. #endif
  297. }
  298. /**
  299. * determine_dirtyable_memory - amount of memory that may be used
  300. *
  301. * Returns the numebr of pages that can currently be freed and used
  302. * by the kernel for direct mappings.
  303. */
  304. unsigned long determine_dirtyable_memory(void)
  305. {
  306. unsigned long x;
  307. x = global_page_state(NR_FREE_PAGES) + global_lru_pages();
  308. if (!vm_highmem_is_dirtyable)
  309. x -= highmem_dirtyable_memory(x);
  310. return x + 1; /* Ensure that we never return 0 */
  311. }
  312. void
  313. get_dirty_limits(long *pbackground, long *pdirty, long *pbdi_dirty,
  314. struct backing_dev_info *bdi)
  315. {
  316. int background_ratio; /* Percentages */
  317. int dirty_ratio;
  318. long background;
  319. long dirty;
  320. unsigned long available_memory = determine_dirtyable_memory();
  321. struct task_struct *tsk;
  322. dirty_ratio = vm_dirty_ratio;
  323. if (dirty_ratio < 5)
  324. dirty_ratio = 5;
  325. background_ratio = dirty_background_ratio;
  326. if (background_ratio >= dirty_ratio)
  327. background_ratio = dirty_ratio / 2;
  328. background = (background_ratio * available_memory) / 100;
  329. dirty = (dirty_ratio * available_memory) / 100;
  330. tsk = current;
  331. if (tsk->flags & PF_LESS_THROTTLE || rt_task(tsk)) {
  332. background += background / 4;
  333. dirty += dirty / 4;
  334. }
  335. *pbackground = background;
  336. *pdirty = dirty;
  337. if (bdi) {
  338. u64 bdi_dirty;
  339. long numerator, denominator;
  340. /*
  341. * Calculate this BDI's share of the dirty ratio.
  342. */
  343. bdi_writeout_fraction(bdi, &numerator, &denominator);
  344. bdi_dirty = (dirty * (100 - bdi_min_ratio)) / 100;
  345. bdi_dirty *= numerator;
  346. do_div(bdi_dirty, denominator);
  347. bdi_dirty += (dirty * bdi->min_ratio) / 100;
  348. if (bdi_dirty > (dirty * bdi->max_ratio) / 100)
  349. bdi_dirty = dirty * bdi->max_ratio / 100;
  350. *pbdi_dirty = bdi_dirty;
  351. clip_bdi_dirty_limit(bdi, dirty, pbdi_dirty);
  352. task_dirty_limit(current, pbdi_dirty);
  353. }
  354. }
  355. /*
  356. * balance_dirty_pages() must be called by processes which are generating dirty
  357. * data. It looks at the number of dirty pages in the machine and will force
  358. * the caller to perform writeback if the system is over `vm_dirty_ratio'.
  359. * If we're over `background_thresh' then pdflush is woken to perform some
  360. * writeout.
  361. */
  362. static void balance_dirty_pages(struct address_space *mapping)
  363. {
  364. long nr_reclaimable, bdi_nr_reclaimable;
  365. long nr_writeback, bdi_nr_writeback;
  366. long background_thresh;
  367. long dirty_thresh;
  368. long bdi_thresh;
  369. unsigned long pages_written = 0;
  370. unsigned long write_chunk = sync_writeback_pages();
  371. struct backing_dev_info *bdi = mapping->backing_dev_info;
  372. for (;;) {
  373. struct writeback_control wbc = {
  374. .bdi = bdi,
  375. .sync_mode = WB_SYNC_NONE,
  376. .older_than_this = NULL,
  377. .nr_to_write = write_chunk,
  378. .range_cyclic = 1,
  379. };
  380. get_dirty_limits(&background_thresh, &dirty_thresh,
  381. &bdi_thresh, bdi);
  382. nr_reclaimable = global_page_state(NR_FILE_DIRTY) +
  383. global_page_state(NR_UNSTABLE_NFS);
  384. nr_writeback = global_page_state(NR_WRITEBACK);
  385. bdi_nr_reclaimable = bdi_stat(bdi, BDI_RECLAIMABLE);
  386. bdi_nr_writeback = bdi_stat(bdi, BDI_WRITEBACK);
  387. if (bdi_nr_reclaimable + bdi_nr_writeback <= bdi_thresh)
  388. break;
  389. /*
  390. * Throttle it only when the background writeback cannot
  391. * catch-up. This avoids (excessively) small writeouts
  392. * when the bdi limits are ramping up.
  393. */
  394. if (nr_reclaimable + nr_writeback <
  395. (background_thresh + dirty_thresh) / 2)
  396. break;
  397. if (!bdi->dirty_exceeded)
  398. bdi->dirty_exceeded = 1;
  399. /* Note: nr_reclaimable denotes nr_dirty + nr_unstable.
  400. * Unstable writes are a feature of certain networked
  401. * filesystems (i.e. NFS) in which data may have been
  402. * written to the server's write cache, but has not yet
  403. * been flushed to permanent storage.
  404. */
  405. if (bdi_nr_reclaimable) {
  406. writeback_inodes(&wbc);
  407. pages_written += write_chunk - wbc.nr_to_write;
  408. get_dirty_limits(&background_thresh, &dirty_thresh,
  409. &bdi_thresh, bdi);
  410. }
  411. /*
  412. * In order to avoid the stacked BDI deadlock we need
  413. * to ensure we accurately count the 'dirty' pages when
  414. * the threshold is low.
  415. *
  416. * Otherwise it would be possible to get thresh+n pages
  417. * reported dirty, even though there are thresh-m pages
  418. * actually dirty; with m+n sitting in the percpu
  419. * deltas.
  420. */
  421. if (bdi_thresh < 2*bdi_stat_error(bdi)) {
  422. bdi_nr_reclaimable = bdi_stat_sum(bdi, BDI_RECLAIMABLE);
  423. bdi_nr_writeback = bdi_stat_sum(bdi, BDI_WRITEBACK);
  424. } else if (bdi_nr_reclaimable) {
  425. bdi_nr_reclaimable = bdi_stat(bdi, BDI_RECLAIMABLE);
  426. bdi_nr_writeback = bdi_stat(bdi, BDI_WRITEBACK);
  427. }
  428. if (bdi_nr_reclaimable + bdi_nr_writeback <= bdi_thresh)
  429. break;
  430. if (pages_written >= write_chunk)
  431. break; /* We've done our duty */
  432. congestion_wait(WRITE, HZ/10);
  433. }
  434. if (bdi_nr_reclaimable + bdi_nr_writeback < bdi_thresh &&
  435. bdi->dirty_exceeded)
  436. bdi->dirty_exceeded = 0;
  437. if (writeback_in_progress(bdi))
  438. return; /* pdflush is already working this queue */
  439. /*
  440. * In laptop mode, we wait until hitting the higher threshold before
  441. * starting background writeout, and then write out all the way down
  442. * to the lower threshold. So slow writers cause minimal disk activity.
  443. *
  444. * In normal mode, we start background writeout at the lower
  445. * background_thresh, to keep the amount of dirty memory low.
  446. */
  447. if ((laptop_mode && pages_written) ||
  448. (!laptop_mode && (global_page_state(NR_FILE_DIRTY)
  449. + global_page_state(NR_UNSTABLE_NFS)
  450. > background_thresh)))
  451. pdflush_operation(background_writeout, 0);
  452. }
  453. void set_page_dirty_balance(struct page *page, int page_mkwrite)
  454. {
  455. if (set_page_dirty(page) || page_mkwrite) {
  456. struct address_space *mapping = page_mapping(page);
  457. if (mapping)
  458. balance_dirty_pages_ratelimited(mapping);
  459. }
  460. }
  461. /**
  462. * balance_dirty_pages_ratelimited_nr - balance dirty memory state
  463. * @mapping: address_space which was dirtied
  464. * @nr_pages_dirtied: number of pages which the caller has just dirtied
  465. *
  466. * Processes which are dirtying memory should call in here once for each page
  467. * which was newly dirtied. The function will periodically check the system's
  468. * dirty state and will initiate writeback if needed.
  469. *
  470. * On really big machines, get_writeback_state is expensive, so try to avoid
  471. * calling it too often (ratelimiting). But once we're over the dirty memory
  472. * limit we decrease the ratelimiting by a lot, to prevent individual processes
  473. * from overshooting the limit by (ratelimit_pages) each.
  474. */
  475. void balance_dirty_pages_ratelimited_nr(struct address_space *mapping,
  476. unsigned long nr_pages_dirtied)
  477. {
  478. static DEFINE_PER_CPU(unsigned long, ratelimits) = 0;
  479. unsigned long ratelimit;
  480. unsigned long *p;
  481. ratelimit = ratelimit_pages;
  482. if (mapping->backing_dev_info->dirty_exceeded)
  483. ratelimit = 8;
  484. /*
  485. * Check the rate limiting. Also, we do not want to throttle real-time
  486. * tasks in balance_dirty_pages(). Period.
  487. */
  488. preempt_disable();
  489. p = &__get_cpu_var(ratelimits);
  490. *p += nr_pages_dirtied;
  491. if (unlikely(*p >= ratelimit)) {
  492. *p = 0;
  493. preempt_enable();
  494. balance_dirty_pages(mapping);
  495. return;
  496. }
  497. preempt_enable();
  498. }
  499. EXPORT_SYMBOL(balance_dirty_pages_ratelimited_nr);
  500. void throttle_vm_writeout(gfp_t gfp_mask)
  501. {
  502. long background_thresh;
  503. long dirty_thresh;
  504. for ( ; ; ) {
  505. get_dirty_limits(&background_thresh, &dirty_thresh, NULL, NULL);
  506. /*
  507. * Boost the allowable dirty threshold a bit for page
  508. * allocators so they don't get DoS'ed by heavy writers
  509. */
  510. dirty_thresh += dirty_thresh / 10; /* wheeee... */
  511. if (global_page_state(NR_UNSTABLE_NFS) +
  512. global_page_state(NR_WRITEBACK) <= dirty_thresh)
  513. break;
  514. congestion_wait(WRITE, HZ/10);
  515. /*
  516. * The caller might hold locks which can prevent IO completion
  517. * or progress in the filesystem. So we cannot just sit here
  518. * waiting for IO to complete.
  519. */
  520. if ((gfp_mask & (__GFP_FS|__GFP_IO)) != (__GFP_FS|__GFP_IO))
  521. break;
  522. }
  523. }
  524. /*
  525. * writeback at least _min_pages, and keep writing until the amount of dirty
  526. * memory is less than the background threshold, or until we're all clean.
  527. */
  528. static void background_writeout(unsigned long _min_pages)
  529. {
  530. long min_pages = _min_pages;
  531. struct writeback_control wbc = {
  532. .bdi = NULL,
  533. .sync_mode = WB_SYNC_NONE,
  534. .older_than_this = NULL,
  535. .nr_to_write = 0,
  536. .nonblocking = 1,
  537. .range_cyclic = 1,
  538. };
  539. for ( ; ; ) {
  540. long background_thresh;
  541. long dirty_thresh;
  542. get_dirty_limits(&background_thresh, &dirty_thresh, NULL, NULL);
  543. if (global_page_state(NR_FILE_DIRTY) +
  544. global_page_state(NR_UNSTABLE_NFS) < background_thresh
  545. && min_pages <= 0)
  546. break;
  547. wbc.more_io = 0;
  548. wbc.encountered_congestion = 0;
  549. wbc.nr_to_write = MAX_WRITEBACK_PAGES;
  550. wbc.pages_skipped = 0;
  551. writeback_inodes(&wbc);
  552. min_pages -= MAX_WRITEBACK_PAGES - wbc.nr_to_write;
  553. if (wbc.nr_to_write > 0 || wbc.pages_skipped > 0) {
  554. /* Wrote less than expected */
  555. if (wbc.encountered_congestion || wbc.more_io)
  556. congestion_wait(WRITE, HZ/10);
  557. else
  558. break;
  559. }
  560. }
  561. }
  562. /*
  563. * Start writeback of `nr_pages' pages. If `nr_pages' is zero, write back
  564. * the whole world. Returns 0 if a pdflush thread was dispatched. Returns
  565. * -1 if all pdflush threads were busy.
  566. */
  567. int wakeup_pdflush(long nr_pages)
  568. {
  569. if (nr_pages == 0)
  570. nr_pages = global_page_state(NR_FILE_DIRTY) +
  571. global_page_state(NR_UNSTABLE_NFS);
  572. return pdflush_operation(background_writeout, nr_pages);
  573. }
  574. static void wb_timer_fn(unsigned long unused);
  575. static void laptop_timer_fn(unsigned long unused);
  576. static DEFINE_TIMER(wb_timer, wb_timer_fn, 0, 0);
  577. static DEFINE_TIMER(laptop_mode_wb_timer, laptop_timer_fn, 0, 0);
  578. /*
  579. * Periodic writeback of "old" data.
  580. *
  581. * Define "old": the first time one of an inode's pages is dirtied, we mark the
  582. * dirtying-time in the inode's address_space. So this periodic writeback code
  583. * just walks the superblock inode list, writing back any inodes which are
  584. * older than a specific point in time.
  585. *
  586. * Try to run once per dirty_writeback_interval. But if a writeback event
  587. * takes longer than a dirty_writeback_interval interval, then leave a
  588. * one-second gap.
  589. *
  590. * older_than_this takes precedence over nr_to_write. So we'll only write back
  591. * all dirty pages if they are all attached to "old" mappings.
  592. */
  593. static void wb_kupdate(unsigned long arg)
  594. {
  595. unsigned long oldest_jif;
  596. unsigned long start_jif;
  597. unsigned long next_jif;
  598. long nr_to_write;
  599. struct writeback_control wbc = {
  600. .bdi = NULL,
  601. .sync_mode = WB_SYNC_NONE,
  602. .older_than_this = &oldest_jif,
  603. .nr_to_write = 0,
  604. .nonblocking = 1,
  605. .for_kupdate = 1,
  606. .range_cyclic = 1,
  607. };
  608. sync_supers();
  609. oldest_jif = jiffies - dirty_expire_interval;
  610. start_jif = jiffies;
  611. next_jif = start_jif + dirty_writeback_interval;
  612. nr_to_write = global_page_state(NR_FILE_DIRTY) +
  613. global_page_state(NR_UNSTABLE_NFS) +
  614. (inodes_stat.nr_inodes - inodes_stat.nr_unused);
  615. while (nr_to_write > 0) {
  616. wbc.more_io = 0;
  617. wbc.encountered_congestion = 0;
  618. wbc.nr_to_write = MAX_WRITEBACK_PAGES;
  619. writeback_inodes(&wbc);
  620. if (wbc.nr_to_write > 0) {
  621. if (wbc.encountered_congestion || wbc.more_io)
  622. congestion_wait(WRITE, HZ/10);
  623. else
  624. break; /* All the old data is written */
  625. }
  626. nr_to_write -= MAX_WRITEBACK_PAGES - wbc.nr_to_write;
  627. }
  628. if (time_before(next_jif, jiffies + HZ))
  629. next_jif = jiffies + HZ;
  630. if (dirty_writeback_interval)
  631. mod_timer(&wb_timer, next_jif);
  632. }
  633. /*
  634. * sysctl handler for /proc/sys/vm/dirty_writeback_centisecs
  635. */
  636. int dirty_writeback_centisecs_handler(ctl_table *table, int write,
  637. struct file *file, void __user *buffer, size_t *length, loff_t *ppos)
  638. {
  639. proc_dointvec_userhz_jiffies(table, write, file, buffer, length, ppos);
  640. if (dirty_writeback_interval)
  641. mod_timer(&wb_timer, jiffies + dirty_writeback_interval);
  642. else
  643. del_timer(&wb_timer);
  644. return 0;
  645. }
  646. static void wb_timer_fn(unsigned long unused)
  647. {
  648. if (pdflush_operation(wb_kupdate, 0) < 0)
  649. mod_timer(&wb_timer, jiffies + HZ); /* delay 1 second */
  650. }
  651. static void laptop_flush(unsigned long unused)
  652. {
  653. sys_sync();
  654. }
  655. static void laptop_timer_fn(unsigned long unused)
  656. {
  657. pdflush_operation(laptop_flush, 0);
  658. }
  659. /*
  660. * We've spun up the disk and we're in laptop mode: schedule writeback
  661. * of all dirty data a few seconds from now. If the flush is already scheduled
  662. * then push it back - the user is still using the disk.
  663. */
  664. void laptop_io_completion(void)
  665. {
  666. mod_timer(&laptop_mode_wb_timer, jiffies + laptop_mode);
  667. }
  668. /*
  669. * We're in laptop mode and we've just synced. The sync's writes will have
  670. * caused another writeback to be scheduled by laptop_io_completion.
  671. * Nothing needs to be written back anymore, so we unschedule the writeback.
  672. */
  673. void laptop_sync_completion(void)
  674. {
  675. del_timer(&laptop_mode_wb_timer);
  676. }
  677. /*
  678. * If ratelimit_pages is too high then we can get into dirty-data overload
  679. * if a large number of processes all perform writes at the same time.
  680. * If it is too low then SMP machines will call the (expensive)
  681. * get_writeback_state too often.
  682. *
  683. * Here we set ratelimit_pages to a level which ensures that when all CPUs are
  684. * dirtying in parallel, we cannot go more than 3% (1/32) over the dirty memory
  685. * thresholds before writeback cuts in.
  686. *
  687. * But the limit should not be set too high. Because it also controls the
  688. * amount of memory which the balance_dirty_pages() caller has to write back.
  689. * If this is too large then the caller will block on the IO queue all the
  690. * time. So limit it to four megabytes - the balance_dirty_pages() caller
  691. * will write six megabyte chunks, max.
  692. */
  693. void writeback_set_ratelimit(void)
  694. {
  695. ratelimit_pages = vm_total_pages / (num_online_cpus() * 32);
  696. if (ratelimit_pages < 16)
  697. ratelimit_pages = 16;
  698. if (ratelimit_pages * PAGE_CACHE_SIZE > 4096 * 1024)
  699. ratelimit_pages = (4096 * 1024) / PAGE_CACHE_SIZE;
  700. }
  701. static int __cpuinit
  702. ratelimit_handler(struct notifier_block *self, unsigned long u, void *v)
  703. {
  704. writeback_set_ratelimit();
  705. return NOTIFY_DONE;
  706. }
  707. static struct notifier_block __cpuinitdata ratelimit_nb = {
  708. .notifier_call = ratelimit_handler,
  709. .next = NULL,
  710. };
  711. /*
  712. * Called early on to tune the page writeback dirty limits.
  713. *
  714. * We used to scale dirty pages according to how total memory
  715. * related to pages that could be allocated for buffers (by
  716. * comparing nr_free_buffer_pages() to vm_total_pages.
  717. *
  718. * However, that was when we used "dirty_ratio" to scale with
  719. * all memory, and we don't do that any more. "dirty_ratio"
  720. * is now applied to total non-HIGHPAGE memory (by subtracting
  721. * totalhigh_pages from vm_total_pages), and as such we can't
  722. * get into the old insane situation any more where we had
  723. * large amounts of dirty pages compared to a small amount of
  724. * non-HIGHMEM memory.
  725. *
  726. * But we might still want to scale the dirty_ratio by how
  727. * much memory the box has..
  728. */
  729. void __init page_writeback_init(void)
  730. {
  731. int shift;
  732. mod_timer(&wb_timer, jiffies + dirty_writeback_interval);
  733. writeback_set_ratelimit();
  734. register_cpu_notifier(&ratelimit_nb);
  735. shift = calc_period_shift();
  736. prop_descriptor_init(&vm_completions, shift);
  737. prop_descriptor_init(&vm_dirties, shift);
  738. }
  739. /**
  740. * write_cache_pages - walk the list of dirty pages of the given address space and write all of them.
  741. * @mapping: address space structure to write
  742. * @wbc: subtract the number of written pages from *@wbc->nr_to_write
  743. * @writepage: function called for each page
  744. * @data: data passed to writepage function
  745. *
  746. * If a page is already under I/O, write_cache_pages() skips it, even
  747. * if it's dirty. This is desirable behaviour for memory-cleaning writeback,
  748. * but it is INCORRECT for data-integrity system calls such as fsync(). fsync()
  749. * and msync() need to guarantee that all the data which was dirty at the time
  750. * the call was made get new I/O started against them. If wbc->sync_mode is
  751. * WB_SYNC_ALL then we were called for data integrity and we must wait for
  752. * existing IO to complete.
  753. */
  754. int write_cache_pages(struct address_space *mapping,
  755. struct writeback_control *wbc, writepage_t writepage,
  756. void *data)
  757. {
  758. struct backing_dev_info *bdi = mapping->backing_dev_info;
  759. int ret = 0;
  760. int done = 0;
  761. struct pagevec pvec;
  762. int nr_pages;
  763. pgoff_t uninitialized_var(writeback_index);
  764. pgoff_t index;
  765. pgoff_t end; /* Inclusive */
  766. pgoff_t done_index;
  767. int cycled;
  768. int range_whole = 0;
  769. long nr_to_write = wbc->nr_to_write;
  770. if (wbc->nonblocking && bdi_write_congested(bdi)) {
  771. wbc->encountered_congestion = 1;
  772. return 0;
  773. }
  774. pagevec_init(&pvec, 0);
  775. if (wbc->range_cyclic) {
  776. writeback_index = mapping->writeback_index; /* prev offset */
  777. index = writeback_index;
  778. if (index == 0)
  779. cycled = 1;
  780. else
  781. cycled = 0;
  782. end = -1;
  783. } else {
  784. index = wbc->range_start >> PAGE_CACHE_SHIFT;
  785. end = wbc->range_end >> PAGE_CACHE_SHIFT;
  786. if (wbc->range_start == 0 && wbc->range_end == LLONG_MAX)
  787. range_whole = 1;
  788. cycled = 1; /* ignore range_cyclic tests */
  789. }
  790. retry:
  791. done_index = index;
  792. while (!done && (index <= end) &&
  793. (nr_pages = pagevec_lookup_tag(&pvec, mapping, &index,
  794. PAGECACHE_TAG_DIRTY,
  795. min(end - index, (pgoff_t)PAGEVEC_SIZE-1) + 1))) {
  796. unsigned i;
  797. for (i = 0; i < nr_pages; i++) {
  798. struct page *page = pvec.pages[i];
  799. done_index = page->index + 1;
  800. /*
  801. * At this point we hold neither mapping->tree_lock nor
  802. * lock on the page itself: the page may be truncated or
  803. * invalidated (changing page->mapping to NULL), or even
  804. * swizzled back from swapper_space to tmpfs file
  805. * mapping
  806. */
  807. lock_page(page);
  808. if (unlikely(page->mapping != mapping)) {
  809. unlock_page(page);
  810. continue;
  811. }
  812. if (page->index > end) {
  813. /*
  814. * can't be range_cyclic (1st pass) because
  815. * end == -1 in that case.
  816. */
  817. done = 1;
  818. unlock_page(page);
  819. continue;
  820. }
  821. if (wbc->sync_mode != WB_SYNC_NONE)
  822. wait_on_page_writeback(page);
  823. if (PageWriteback(page) ||
  824. !clear_page_dirty_for_io(page)) {
  825. unlock_page(page);
  826. continue;
  827. }
  828. ret = (*writepage)(page, wbc, data);
  829. if (unlikely(ret == AOP_WRITEPAGE_ACTIVATE)) {
  830. unlock_page(page);
  831. ret = 0;
  832. }
  833. if (ret || (--nr_to_write <= 0))
  834. done = 1;
  835. if (wbc->nonblocking && bdi_write_congested(bdi)) {
  836. wbc->encountered_congestion = 1;
  837. done = 1;
  838. }
  839. }
  840. pagevec_release(&pvec);
  841. cond_resched();
  842. }
  843. if (!cycled) {
  844. /*
  845. * range_cyclic:
  846. * We hit the last page and there is more work to be done: wrap
  847. * back to the start of the file
  848. */
  849. cycled = 1;
  850. index = 0;
  851. end = writeback_index - 1;
  852. goto retry;
  853. }
  854. if (!wbc->no_nrwrite_index_update) {
  855. if (wbc->range_cyclic || (range_whole && nr_to_write > 0))
  856. mapping->writeback_index = done_index;
  857. wbc->nr_to_write = nr_to_write;
  858. }
  859. return ret;
  860. }
  861. EXPORT_SYMBOL(write_cache_pages);
  862. /*
  863. * Function used by generic_writepages to call the real writepage
  864. * function and set the mapping flags on error
  865. */
  866. static int __writepage(struct page *page, struct writeback_control *wbc,
  867. void *data)
  868. {
  869. struct address_space *mapping = data;
  870. int ret = mapping->a_ops->writepage(page, wbc);
  871. mapping_set_error(mapping, ret);
  872. return ret;
  873. }
  874. /**
  875. * generic_writepages - walk the list of dirty pages of the given address space and writepage() all of them.
  876. * @mapping: address space structure to write
  877. * @wbc: subtract the number of written pages from *@wbc->nr_to_write
  878. *
  879. * This is a library function, which implements the writepages()
  880. * address_space_operation.
  881. */
  882. int generic_writepages(struct address_space *mapping,
  883. struct writeback_control *wbc)
  884. {
  885. /* deal with chardevs and other special file */
  886. if (!mapping->a_ops->writepage)
  887. return 0;
  888. return write_cache_pages(mapping, wbc, __writepage, mapping);
  889. }
  890. EXPORT_SYMBOL(generic_writepages);
  891. int do_writepages(struct address_space *mapping, struct writeback_control *wbc)
  892. {
  893. int ret;
  894. if (wbc->nr_to_write <= 0)
  895. return 0;
  896. wbc->for_writepages = 1;
  897. if (mapping->a_ops->writepages)
  898. ret = mapping->a_ops->writepages(mapping, wbc);
  899. else
  900. ret = generic_writepages(mapping, wbc);
  901. wbc->for_writepages = 0;
  902. return ret;
  903. }
  904. /**
  905. * write_one_page - write out a single page and optionally wait on I/O
  906. * @page: the page to write
  907. * @wait: if true, wait on writeout
  908. *
  909. * The page must be locked by the caller and will be unlocked upon return.
  910. *
  911. * write_one_page() returns a negative error code if I/O failed.
  912. */
  913. int write_one_page(struct page *page, int wait)
  914. {
  915. struct address_space *mapping = page->mapping;
  916. int ret = 0;
  917. struct writeback_control wbc = {
  918. .sync_mode = WB_SYNC_ALL,
  919. .nr_to_write = 1,
  920. };
  921. BUG_ON(!PageLocked(page));
  922. if (wait)
  923. wait_on_page_writeback(page);
  924. if (clear_page_dirty_for_io(page)) {
  925. page_cache_get(page);
  926. ret = mapping->a_ops->writepage(page, &wbc);
  927. if (ret == 0 && wait) {
  928. wait_on_page_writeback(page);
  929. if (PageError(page))
  930. ret = -EIO;
  931. }
  932. page_cache_release(page);
  933. } else {
  934. unlock_page(page);
  935. }
  936. return ret;
  937. }
  938. EXPORT_SYMBOL(write_one_page);
  939. /*
  940. * For address_spaces which do not use buffers nor write back.
  941. */
  942. int __set_page_dirty_no_writeback(struct page *page)
  943. {
  944. if (!PageDirty(page))
  945. SetPageDirty(page);
  946. return 0;
  947. }
  948. /*
  949. * For address_spaces which do not use buffers. Just tag the page as dirty in
  950. * its radix tree.
  951. *
  952. * This is also used when a single buffer is being dirtied: we want to set the
  953. * page dirty in that case, but not all the buffers. This is a "bottom-up"
  954. * dirtying, whereas __set_page_dirty_buffers() is a "top-down" dirtying.
  955. *
  956. * Most callers have locked the page, which pins the address_space in memory.
  957. * But zap_pte_range() does not lock the page, however in that case the
  958. * mapping is pinned by the vma's ->vm_file reference.
  959. *
  960. * We take care to handle the case where the page was truncated from the
  961. * mapping by re-checking page_mapping() inside tree_lock.
  962. */
  963. int __set_page_dirty_nobuffers(struct page *page)
  964. {
  965. if (!TestSetPageDirty(page)) {
  966. struct address_space *mapping = page_mapping(page);
  967. struct address_space *mapping2;
  968. if (!mapping)
  969. return 1;
  970. spin_lock_irq(&mapping->tree_lock);
  971. mapping2 = page_mapping(page);
  972. if (mapping2) { /* Race with truncate? */
  973. BUG_ON(mapping2 != mapping);
  974. WARN_ON_ONCE(!PagePrivate(page) && !PageUptodate(page));
  975. if (mapping_cap_account_dirty(mapping)) {
  976. __inc_zone_page_state(page, NR_FILE_DIRTY);
  977. __inc_bdi_stat(mapping->backing_dev_info,
  978. BDI_RECLAIMABLE);
  979. task_io_account_write(PAGE_CACHE_SIZE);
  980. }
  981. radix_tree_tag_set(&mapping->page_tree,
  982. page_index(page), PAGECACHE_TAG_DIRTY);
  983. }
  984. spin_unlock_irq(&mapping->tree_lock);
  985. if (mapping->host) {
  986. /* !PageAnon && !swapper_space */
  987. __mark_inode_dirty(mapping->host, I_DIRTY_PAGES);
  988. }
  989. return 1;
  990. }
  991. return 0;
  992. }
  993. EXPORT_SYMBOL(__set_page_dirty_nobuffers);
  994. /*
  995. * When a writepage implementation decides that it doesn't want to write this
  996. * page for some reason, it should redirty the locked page via
  997. * redirty_page_for_writepage() and it should then unlock the page and return 0
  998. */
  999. int redirty_page_for_writepage(struct writeback_control *wbc, struct page *page)
  1000. {
  1001. wbc->pages_skipped++;
  1002. return __set_page_dirty_nobuffers(page);
  1003. }
  1004. EXPORT_SYMBOL(redirty_page_for_writepage);
  1005. /*
  1006. * If the mapping doesn't provide a set_page_dirty a_op, then
  1007. * just fall through and assume that it wants buffer_heads.
  1008. */
  1009. static int __set_page_dirty(struct page *page)
  1010. {
  1011. struct address_space *mapping = page_mapping(page);
  1012. if (likely(mapping)) {
  1013. int (*spd)(struct page *) = mapping->a_ops->set_page_dirty;
  1014. #ifdef CONFIG_BLOCK
  1015. if (!spd)
  1016. spd = __set_page_dirty_buffers;
  1017. #endif
  1018. return (*spd)(page);
  1019. }
  1020. if (!PageDirty(page)) {
  1021. if (!TestSetPageDirty(page))
  1022. return 1;
  1023. }
  1024. return 0;
  1025. }
  1026. int set_page_dirty(struct page *page)
  1027. {
  1028. int ret = __set_page_dirty(page);
  1029. if (ret)
  1030. task_dirty_inc(current);
  1031. return ret;
  1032. }
  1033. EXPORT_SYMBOL(set_page_dirty);
  1034. /*
  1035. * set_page_dirty() is racy if the caller has no reference against
  1036. * page->mapping->host, and if the page is unlocked. This is because another
  1037. * CPU could truncate the page off the mapping and then free the mapping.
  1038. *
  1039. * Usually, the page _is_ locked, or the caller is a user-space process which
  1040. * holds a reference on the inode by having an open file.
  1041. *
  1042. * In other cases, the page should be locked before running set_page_dirty().
  1043. */
  1044. int set_page_dirty_lock(struct page *page)
  1045. {
  1046. int ret;
  1047. lock_page_nosync(page);
  1048. ret = set_page_dirty(page);
  1049. unlock_page(page);
  1050. return ret;
  1051. }
  1052. EXPORT_SYMBOL(set_page_dirty_lock);
  1053. /*
  1054. * Clear a page's dirty flag, while caring for dirty memory accounting.
  1055. * Returns true if the page was previously dirty.
  1056. *
  1057. * This is for preparing to put the page under writeout. We leave the page
  1058. * tagged as dirty in the radix tree so that a concurrent write-for-sync
  1059. * can discover it via a PAGECACHE_TAG_DIRTY walk. The ->writepage
  1060. * implementation will run either set_page_writeback() or set_page_dirty(),
  1061. * at which stage we bring the page's dirty flag and radix-tree dirty tag
  1062. * back into sync.
  1063. *
  1064. * This incoherency between the page's dirty flag and radix-tree tag is
  1065. * unfortunate, but it only exists while the page is locked.
  1066. */
  1067. int clear_page_dirty_for_io(struct page *page)
  1068. {
  1069. struct address_space *mapping = page_mapping(page);
  1070. BUG_ON(!PageLocked(page));
  1071. ClearPageReclaim(page);
  1072. if (mapping && mapping_cap_account_dirty(mapping)) {
  1073. /*
  1074. * Yes, Virginia, this is indeed insane.
  1075. *
  1076. * We use this sequence to make sure that
  1077. * (a) we account for dirty stats properly
  1078. * (b) we tell the low-level filesystem to
  1079. * mark the whole page dirty if it was
  1080. * dirty in a pagetable. Only to then
  1081. * (c) clean the page again and return 1 to
  1082. * cause the writeback.
  1083. *
  1084. * This way we avoid all nasty races with the
  1085. * dirty bit in multiple places and clearing
  1086. * them concurrently from different threads.
  1087. *
  1088. * Note! Normally the "set_page_dirty(page)"
  1089. * has no effect on the actual dirty bit - since
  1090. * that will already usually be set. But we
  1091. * need the side effects, and it can help us
  1092. * avoid races.
  1093. *
  1094. * We basically use the page "master dirty bit"
  1095. * as a serialization point for all the different
  1096. * threads doing their things.
  1097. */
  1098. if (page_mkclean(page))
  1099. set_page_dirty(page);
  1100. /*
  1101. * We carefully synchronise fault handlers against
  1102. * installing a dirty pte and marking the page dirty
  1103. * at this point. We do this by having them hold the
  1104. * page lock at some point after installing their
  1105. * pte, but before marking the page dirty.
  1106. * Pages are always locked coming in here, so we get
  1107. * the desired exclusion. See mm/memory.c:do_wp_page()
  1108. * for more comments.
  1109. */
  1110. if (TestClearPageDirty(page)) {
  1111. dec_zone_page_state(page, NR_FILE_DIRTY);
  1112. dec_bdi_stat(mapping->backing_dev_info,
  1113. BDI_RECLAIMABLE);
  1114. return 1;
  1115. }
  1116. return 0;
  1117. }
  1118. return TestClearPageDirty(page);
  1119. }
  1120. EXPORT_SYMBOL(clear_page_dirty_for_io);
  1121. int test_clear_page_writeback(struct page *page)
  1122. {
  1123. struct address_space *mapping = page_mapping(page);
  1124. int ret;
  1125. if (mapping) {
  1126. struct backing_dev_info *bdi = mapping->backing_dev_info;
  1127. unsigned long flags;
  1128. spin_lock_irqsave(&mapping->tree_lock, flags);
  1129. ret = TestClearPageWriteback(page);
  1130. if (ret) {
  1131. radix_tree_tag_clear(&mapping->page_tree,
  1132. page_index(page),
  1133. PAGECACHE_TAG_WRITEBACK);
  1134. if (bdi_cap_account_writeback(bdi)) {
  1135. __dec_bdi_stat(bdi, BDI_WRITEBACK);
  1136. __bdi_writeout_inc(bdi);
  1137. }
  1138. }
  1139. spin_unlock_irqrestore(&mapping->tree_lock, flags);
  1140. } else {
  1141. ret = TestClearPageWriteback(page);
  1142. }
  1143. if (ret)
  1144. dec_zone_page_state(page, NR_WRITEBACK);
  1145. return ret;
  1146. }
  1147. int test_set_page_writeback(struct page *page)
  1148. {
  1149. struct address_space *mapping = page_mapping(page);
  1150. int ret;
  1151. if (mapping) {
  1152. struct backing_dev_info *bdi = mapping->backing_dev_info;
  1153. unsigned long flags;
  1154. spin_lock_irqsave(&mapping->tree_lock, flags);
  1155. ret = TestSetPageWriteback(page);
  1156. if (!ret) {
  1157. radix_tree_tag_set(&mapping->page_tree,
  1158. page_index(page),
  1159. PAGECACHE_TAG_WRITEBACK);
  1160. if (bdi_cap_account_writeback(bdi))
  1161. __inc_bdi_stat(bdi, BDI_WRITEBACK);
  1162. }
  1163. if (!PageDirty(page))
  1164. radix_tree_tag_clear(&mapping->page_tree,
  1165. page_index(page),
  1166. PAGECACHE_TAG_DIRTY);
  1167. spin_unlock_irqrestore(&mapping->tree_lock, flags);
  1168. } else {
  1169. ret = TestSetPageWriteback(page);
  1170. }
  1171. if (!ret)
  1172. inc_zone_page_state(page, NR_WRITEBACK);
  1173. return ret;
  1174. }
  1175. EXPORT_SYMBOL(test_set_page_writeback);
  1176. /*
  1177. * Return true if any of the pages in the mapping are marked with the
  1178. * passed tag.
  1179. */
  1180. int mapping_tagged(struct address_space *mapping, int tag)
  1181. {
  1182. int ret;
  1183. rcu_read_lock();
  1184. ret = radix_tree_tagged(&mapping->page_tree, tag);
  1185. rcu_read_unlock();
  1186. return ret;
  1187. }
  1188. EXPORT_SYMBOL(mapping_tagged);