page-writeback.c 37 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335
  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. int cycled;
  767. int range_whole = 0;
  768. long nr_to_write = wbc->nr_to_write;
  769. if (wbc->nonblocking && bdi_write_congested(bdi)) {
  770. wbc->encountered_congestion = 1;
  771. return 0;
  772. }
  773. pagevec_init(&pvec, 0);
  774. if (wbc->range_cyclic) {
  775. writeback_index = mapping->writeback_index; /* prev offset */
  776. index = writeback_index;
  777. if (index == 0)
  778. cycled = 1;
  779. else
  780. cycled = 0;
  781. end = -1;
  782. } else {
  783. index = wbc->range_start >> PAGE_CACHE_SHIFT;
  784. end = wbc->range_end >> PAGE_CACHE_SHIFT;
  785. if (wbc->range_start == 0 && wbc->range_end == LLONG_MAX)
  786. range_whole = 1;
  787. cycled = 1; /* ignore range_cyclic tests */
  788. }
  789. retry:
  790. while (!done && (index <= end) &&
  791. (nr_pages = pagevec_lookup_tag(&pvec, mapping, &index,
  792. PAGECACHE_TAG_DIRTY,
  793. min(end - index, (pgoff_t)PAGEVEC_SIZE-1) + 1))) {
  794. unsigned i;
  795. for (i = 0; i < nr_pages; i++) {
  796. struct page *page = pvec.pages[i];
  797. /*
  798. * At this point we hold neither mapping->tree_lock nor
  799. * lock on the page itself: the page may be truncated or
  800. * invalidated (changing page->mapping to NULL), or even
  801. * swizzled back from swapper_space to tmpfs file
  802. * mapping
  803. */
  804. lock_page(page);
  805. if (unlikely(page->mapping != mapping)) {
  806. unlock_page(page);
  807. continue;
  808. }
  809. if (page->index > end) {
  810. /*
  811. * can't be range_cyclic (1st pass) because
  812. * end == -1 in that case.
  813. */
  814. done = 1;
  815. unlock_page(page);
  816. continue;
  817. }
  818. if (wbc->sync_mode != WB_SYNC_NONE)
  819. wait_on_page_writeback(page);
  820. if (PageWriteback(page) ||
  821. !clear_page_dirty_for_io(page)) {
  822. unlock_page(page);
  823. continue;
  824. }
  825. ret = (*writepage)(page, wbc, data);
  826. if (unlikely(ret == AOP_WRITEPAGE_ACTIVATE)) {
  827. unlock_page(page);
  828. ret = 0;
  829. }
  830. if (ret || (--nr_to_write <= 0))
  831. done = 1;
  832. if (wbc->nonblocking && bdi_write_congested(bdi)) {
  833. wbc->encountered_congestion = 1;
  834. done = 1;
  835. }
  836. }
  837. pagevec_release(&pvec);
  838. cond_resched();
  839. }
  840. if (!cycled) {
  841. /*
  842. * range_cyclic:
  843. * We hit the last page and there is more work to be done: wrap
  844. * back to the start of the file
  845. */
  846. cycled = 1;
  847. index = 0;
  848. end = writeback_index - 1;
  849. goto retry;
  850. }
  851. if (!wbc->no_nrwrite_index_update) {
  852. if (wbc->range_cyclic || (range_whole && nr_to_write > 0))
  853. mapping->writeback_index = index;
  854. wbc->nr_to_write = nr_to_write;
  855. }
  856. return ret;
  857. }
  858. EXPORT_SYMBOL(write_cache_pages);
  859. /*
  860. * Function used by generic_writepages to call the real writepage
  861. * function and set the mapping flags on error
  862. */
  863. static int __writepage(struct page *page, struct writeback_control *wbc,
  864. void *data)
  865. {
  866. struct address_space *mapping = data;
  867. int ret = mapping->a_ops->writepage(page, wbc);
  868. mapping_set_error(mapping, ret);
  869. return ret;
  870. }
  871. /**
  872. * generic_writepages - walk the list of dirty pages of the given address space and writepage() all of them.
  873. * @mapping: address space structure to write
  874. * @wbc: subtract the number of written pages from *@wbc->nr_to_write
  875. *
  876. * This is a library function, which implements the writepages()
  877. * address_space_operation.
  878. */
  879. int generic_writepages(struct address_space *mapping,
  880. struct writeback_control *wbc)
  881. {
  882. /* deal with chardevs and other special file */
  883. if (!mapping->a_ops->writepage)
  884. return 0;
  885. return write_cache_pages(mapping, wbc, __writepage, mapping);
  886. }
  887. EXPORT_SYMBOL(generic_writepages);
  888. int do_writepages(struct address_space *mapping, struct writeback_control *wbc)
  889. {
  890. int ret;
  891. if (wbc->nr_to_write <= 0)
  892. return 0;
  893. wbc->for_writepages = 1;
  894. if (mapping->a_ops->writepages)
  895. ret = mapping->a_ops->writepages(mapping, wbc);
  896. else
  897. ret = generic_writepages(mapping, wbc);
  898. wbc->for_writepages = 0;
  899. return ret;
  900. }
  901. /**
  902. * write_one_page - write out a single page and optionally wait on I/O
  903. * @page: the page to write
  904. * @wait: if true, wait on writeout
  905. *
  906. * The page must be locked by the caller and will be unlocked upon return.
  907. *
  908. * write_one_page() returns a negative error code if I/O failed.
  909. */
  910. int write_one_page(struct page *page, int wait)
  911. {
  912. struct address_space *mapping = page->mapping;
  913. int ret = 0;
  914. struct writeback_control wbc = {
  915. .sync_mode = WB_SYNC_ALL,
  916. .nr_to_write = 1,
  917. };
  918. BUG_ON(!PageLocked(page));
  919. if (wait)
  920. wait_on_page_writeback(page);
  921. if (clear_page_dirty_for_io(page)) {
  922. page_cache_get(page);
  923. ret = mapping->a_ops->writepage(page, &wbc);
  924. if (ret == 0 && wait) {
  925. wait_on_page_writeback(page);
  926. if (PageError(page))
  927. ret = -EIO;
  928. }
  929. page_cache_release(page);
  930. } else {
  931. unlock_page(page);
  932. }
  933. return ret;
  934. }
  935. EXPORT_SYMBOL(write_one_page);
  936. /*
  937. * For address_spaces which do not use buffers nor write back.
  938. */
  939. int __set_page_dirty_no_writeback(struct page *page)
  940. {
  941. if (!PageDirty(page))
  942. SetPageDirty(page);
  943. return 0;
  944. }
  945. /*
  946. * For address_spaces which do not use buffers. Just tag the page as dirty in
  947. * its radix tree.
  948. *
  949. * This is also used when a single buffer is being dirtied: we want to set the
  950. * page dirty in that case, but not all the buffers. This is a "bottom-up"
  951. * dirtying, whereas __set_page_dirty_buffers() is a "top-down" dirtying.
  952. *
  953. * Most callers have locked the page, which pins the address_space in memory.
  954. * But zap_pte_range() does not lock the page, however in that case the
  955. * mapping is pinned by the vma's ->vm_file reference.
  956. *
  957. * We take care to handle the case where the page was truncated from the
  958. * mapping by re-checking page_mapping() inside tree_lock.
  959. */
  960. int __set_page_dirty_nobuffers(struct page *page)
  961. {
  962. if (!TestSetPageDirty(page)) {
  963. struct address_space *mapping = page_mapping(page);
  964. struct address_space *mapping2;
  965. if (!mapping)
  966. return 1;
  967. spin_lock_irq(&mapping->tree_lock);
  968. mapping2 = page_mapping(page);
  969. if (mapping2) { /* Race with truncate? */
  970. BUG_ON(mapping2 != mapping);
  971. WARN_ON_ONCE(!PagePrivate(page) && !PageUptodate(page));
  972. if (mapping_cap_account_dirty(mapping)) {
  973. __inc_zone_page_state(page, NR_FILE_DIRTY);
  974. __inc_bdi_stat(mapping->backing_dev_info,
  975. BDI_RECLAIMABLE);
  976. task_io_account_write(PAGE_CACHE_SIZE);
  977. }
  978. radix_tree_tag_set(&mapping->page_tree,
  979. page_index(page), PAGECACHE_TAG_DIRTY);
  980. }
  981. spin_unlock_irq(&mapping->tree_lock);
  982. if (mapping->host) {
  983. /* !PageAnon && !swapper_space */
  984. __mark_inode_dirty(mapping->host, I_DIRTY_PAGES);
  985. }
  986. return 1;
  987. }
  988. return 0;
  989. }
  990. EXPORT_SYMBOL(__set_page_dirty_nobuffers);
  991. /*
  992. * When a writepage implementation decides that it doesn't want to write this
  993. * page for some reason, it should redirty the locked page via
  994. * redirty_page_for_writepage() and it should then unlock the page and return 0
  995. */
  996. int redirty_page_for_writepage(struct writeback_control *wbc, struct page *page)
  997. {
  998. wbc->pages_skipped++;
  999. return __set_page_dirty_nobuffers(page);
  1000. }
  1001. EXPORT_SYMBOL(redirty_page_for_writepage);
  1002. /*
  1003. * If the mapping doesn't provide a set_page_dirty a_op, then
  1004. * just fall through and assume that it wants buffer_heads.
  1005. */
  1006. static int __set_page_dirty(struct page *page)
  1007. {
  1008. struct address_space *mapping = page_mapping(page);
  1009. if (likely(mapping)) {
  1010. int (*spd)(struct page *) = mapping->a_ops->set_page_dirty;
  1011. #ifdef CONFIG_BLOCK
  1012. if (!spd)
  1013. spd = __set_page_dirty_buffers;
  1014. #endif
  1015. return (*spd)(page);
  1016. }
  1017. if (!PageDirty(page)) {
  1018. if (!TestSetPageDirty(page))
  1019. return 1;
  1020. }
  1021. return 0;
  1022. }
  1023. int set_page_dirty(struct page *page)
  1024. {
  1025. int ret = __set_page_dirty(page);
  1026. if (ret)
  1027. task_dirty_inc(current);
  1028. return ret;
  1029. }
  1030. EXPORT_SYMBOL(set_page_dirty);
  1031. /*
  1032. * set_page_dirty() is racy if the caller has no reference against
  1033. * page->mapping->host, and if the page is unlocked. This is because another
  1034. * CPU could truncate the page off the mapping and then free the mapping.
  1035. *
  1036. * Usually, the page _is_ locked, or the caller is a user-space process which
  1037. * holds a reference on the inode by having an open file.
  1038. *
  1039. * In other cases, the page should be locked before running set_page_dirty().
  1040. */
  1041. int set_page_dirty_lock(struct page *page)
  1042. {
  1043. int ret;
  1044. lock_page_nosync(page);
  1045. ret = set_page_dirty(page);
  1046. unlock_page(page);
  1047. return ret;
  1048. }
  1049. EXPORT_SYMBOL(set_page_dirty_lock);
  1050. /*
  1051. * Clear a page's dirty flag, while caring for dirty memory accounting.
  1052. * Returns true if the page was previously dirty.
  1053. *
  1054. * This is for preparing to put the page under writeout. We leave the page
  1055. * tagged as dirty in the radix tree so that a concurrent write-for-sync
  1056. * can discover it via a PAGECACHE_TAG_DIRTY walk. The ->writepage
  1057. * implementation will run either set_page_writeback() or set_page_dirty(),
  1058. * at which stage we bring the page's dirty flag and radix-tree dirty tag
  1059. * back into sync.
  1060. *
  1061. * This incoherency between the page's dirty flag and radix-tree tag is
  1062. * unfortunate, but it only exists while the page is locked.
  1063. */
  1064. int clear_page_dirty_for_io(struct page *page)
  1065. {
  1066. struct address_space *mapping = page_mapping(page);
  1067. BUG_ON(!PageLocked(page));
  1068. ClearPageReclaim(page);
  1069. if (mapping && mapping_cap_account_dirty(mapping)) {
  1070. /*
  1071. * Yes, Virginia, this is indeed insane.
  1072. *
  1073. * We use this sequence to make sure that
  1074. * (a) we account for dirty stats properly
  1075. * (b) we tell the low-level filesystem to
  1076. * mark the whole page dirty if it was
  1077. * dirty in a pagetable. Only to then
  1078. * (c) clean the page again and return 1 to
  1079. * cause the writeback.
  1080. *
  1081. * This way we avoid all nasty races with the
  1082. * dirty bit in multiple places and clearing
  1083. * them concurrently from different threads.
  1084. *
  1085. * Note! Normally the "set_page_dirty(page)"
  1086. * has no effect on the actual dirty bit - since
  1087. * that will already usually be set. But we
  1088. * need the side effects, and it can help us
  1089. * avoid races.
  1090. *
  1091. * We basically use the page "master dirty bit"
  1092. * as a serialization point for all the different
  1093. * threads doing their things.
  1094. */
  1095. if (page_mkclean(page))
  1096. set_page_dirty(page);
  1097. /*
  1098. * We carefully synchronise fault handlers against
  1099. * installing a dirty pte and marking the page dirty
  1100. * at this point. We do this by having them hold the
  1101. * page lock at some point after installing their
  1102. * pte, but before marking the page dirty.
  1103. * Pages are always locked coming in here, so we get
  1104. * the desired exclusion. See mm/memory.c:do_wp_page()
  1105. * for more comments.
  1106. */
  1107. if (TestClearPageDirty(page)) {
  1108. dec_zone_page_state(page, NR_FILE_DIRTY);
  1109. dec_bdi_stat(mapping->backing_dev_info,
  1110. BDI_RECLAIMABLE);
  1111. return 1;
  1112. }
  1113. return 0;
  1114. }
  1115. return TestClearPageDirty(page);
  1116. }
  1117. EXPORT_SYMBOL(clear_page_dirty_for_io);
  1118. int test_clear_page_writeback(struct page *page)
  1119. {
  1120. struct address_space *mapping = page_mapping(page);
  1121. int ret;
  1122. if (mapping) {
  1123. struct backing_dev_info *bdi = mapping->backing_dev_info;
  1124. unsigned long flags;
  1125. spin_lock_irqsave(&mapping->tree_lock, flags);
  1126. ret = TestClearPageWriteback(page);
  1127. if (ret) {
  1128. radix_tree_tag_clear(&mapping->page_tree,
  1129. page_index(page),
  1130. PAGECACHE_TAG_WRITEBACK);
  1131. if (bdi_cap_account_writeback(bdi)) {
  1132. __dec_bdi_stat(bdi, BDI_WRITEBACK);
  1133. __bdi_writeout_inc(bdi);
  1134. }
  1135. }
  1136. spin_unlock_irqrestore(&mapping->tree_lock, flags);
  1137. } else {
  1138. ret = TestClearPageWriteback(page);
  1139. }
  1140. if (ret)
  1141. dec_zone_page_state(page, NR_WRITEBACK);
  1142. return ret;
  1143. }
  1144. int test_set_page_writeback(struct page *page)
  1145. {
  1146. struct address_space *mapping = page_mapping(page);
  1147. int ret;
  1148. if (mapping) {
  1149. struct backing_dev_info *bdi = mapping->backing_dev_info;
  1150. unsigned long flags;
  1151. spin_lock_irqsave(&mapping->tree_lock, flags);
  1152. ret = TestSetPageWriteback(page);
  1153. if (!ret) {
  1154. radix_tree_tag_set(&mapping->page_tree,
  1155. page_index(page),
  1156. PAGECACHE_TAG_WRITEBACK);
  1157. if (bdi_cap_account_writeback(bdi))
  1158. __inc_bdi_stat(bdi, BDI_WRITEBACK);
  1159. }
  1160. if (!PageDirty(page))
  1161. radix_tree_tag_clear(&mapping->page_tree,
  1162. page_index(page),
  1163. PAGECACHE_TAG_DIRTY);
  1164. spin_unlock_irqrestore(&mapping->tree_lock, flags);
  1165. } else {
  1166. ret = TestSetPageWriteback(page);
  1167. }
  1168. if (!ret)
  1169. inc_zone_page_state(page, NR_WRITEBACK);
  1170. return ret;
  1171. }
  1172. EXPORT_SYMBOL(test_set_page_writeback);
  1173. /*
  1174. * Return true if any of the pages in the mapping are marked with the
  1175. * passed tag.
  1176. */
  1177. int mapping_tagged(struct address_space *mapping, int tag)
  1178. {
  1179. int ret;
  1180. rcu_read_lock();
  1181. ret = radix_tree_tagged(&mapping->page_tree, tag);
  1182. rcu_read_unlock();
  1183. return ret;
  1184. }
  1185. EXPORT_SYMBOL(mapping_tagged);