page-writeback.c 36 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326
  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 akpm@zip.com.au
  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)
  286. + zone_page_state(z, NR_INACTIVE)
  287. + zone_page_state(z, NR_ACTIVE);
  288. }
  289. /*
  290. * Make sure that the number of highmem pages is never larger
  291. * than the number of the total dirtyable memory. This can only
  292. * occur in very strange VM situations but we want to make sure
  293. * that this does not occur.
  294. */
  295. return min(x, total);
  296. #else
  297. return 0;
  298. #endif
  299. }
  300. /**
  301. * determine_dirtyable_memory - amount of memory that may be used
  302. *
  303. * Returns the numebr of pages that can currently be freed and used
  304. * by the kernel for direct mappings.
  305. */
  306. unsigned long determine_dirtyable_memory(void)
  307. {
  308. unsigned long x;
  309. x = global_page_state(NR_FREE_PAGES)
  310. + global_page_state(NR_INACTIVE)
  311. + global_page_state(NR_ACTIVE);
  312. if (!vm_highmem_is_dirtyable)
  313. x -= highmem_dirtyable_memory(x);
  314. return x + 1; /* Ensure that we never return 0 */
  315. }
  316. void
  317. get_dirty_limits(long *pbackground, long *pdirty, long *pbdi_dirty,
  318. struct backing_dev_info *bdi)
  319. {
  320. int background_ratio; /* Percentages */
  321. int dirty_ratio;
  322. long background;
  323. long dirty;
  324. unsigned long available_memory = determine_dirtyable_memory();
  325. struct task_struct *tsk;
  326. dirty_ratio = vm_dirty_ratio;
  327. if (dirty_ratio < 5)
  328. dirty_ratio = 5;
  329. background_ratio = dirty_background_ratio;
  330. if (background_ratio >= dirty_ratio)
  331. background_ratio = dirty_ratio / 2;
  332. background = (background_ratio * available_memory) / 100;
  333. dirty = (dirty_ratio * available_memory) / 100;
  334. tsk = current;
  335. if (tsk->flags & PF_LESS_THROTTLE || rt_task(tsk)) {
  336. background += background / 4;
  337. dirty += dirty / 4;
  338. }
  339. *pbackground = background;
  340. *pdirty = dirty;
  341. if (bdi) {
  342. u64 bdi_dirty;
  343. long numerator, denominator;
  344. /*
  345. * Calculate this BDI's share of the dirty ratio.
  346. */
  347. bdi_writeout_fraction(bdi, &numerator, &denominator);
  348. bdi_dirty = (dirty * (100 - bdi_min_ratio)) / 100;
  349. bdi_dirty *= numerator;
  350. do_div(bdi_dirty, denominator);
  351. bdi_dirty += (dirty * bdi->min_ratio) / 100;
  352. if (bdi_dirty > (dirty * bdi->max_ratio) / 100)
  353. bdi_dirty = dirty * bdi->max_ratio / 100;
  354. *pbdi_dirty = bdi_dirty;
  355. clip_bdi_dirty_limit(bdi, dirty, pbdi_dirty);
  356. task_dirty_limit(current, pbdi_dirty);
  357. }
  358. }
  359. /*
  360. * balance_dirty_pages() must be called by processes which are generating dirty
  361. * data. It looks at the number of dirty pages in the machine and will force
  362. * the caller to perform writeback if the system is over `vm_dirty_ratio'.
  363. * If we're over `background_thresh' then pdflush is woken to perform some
  364. * writeout.
  365. */
  366. static void balance_dirty_pages(struct address_space *mapping)
  367. {
  368. long nr_reclaimable, bdi_nr_reclaimable;
  369. long nr_writeback, bdi_nr_writeback;
  370. long background_thresh;
  371. long dirty_thresh;
  372. long bdi_thresh;
  373. unsigned long pages_written = 0;
  374. unsigned long write_chunk = sync_writeback_pages();
  375. struct backing_dev_info *bdi = mapping->backing_dev_info;
  376. for (;;) {
  377. struct writeback_control wbc = {
  378. .bdi = bdi,
  379. .sync_mode = WB_SYNC_NONE,
  380. .older_than_this = NULL,
  381. .nr_to_write = write_chunk,
  382. .range_cyclic = 1,
  383. };
  384. get_dirty_limits(&background_thresh, &dirty_thresh,
  385. &bdi_thresh, bdi);
  386. nr_reclaimable = global_page_state(NR_FILE_DIRTY) +
  387. global_page_state(NR_UNSTABLE_NFS);
  388. nr_writeback = global_page_state(NR_WRITEBACK);
  389. bdi_nr_reclaimable = bdi_stat(bdi, BDI_RECLAIMABLE);
  390. bdi_nr_writeback = bdi_stat(bdi, BDI_WRITEBACK);
  391. if (bdi_nr_reclaimable + bdi_nr_writeback <= bdi_thresh)
  392. break;
  393. /*
  394. * Throttle it only when the background writeback cannot
  395. * catch-up. This avoids (excessively) small writeouts
  396. * when the bdi limits are ramping up.
  397. */
  398. if (nr_reclaimable + nr_writeback <
  399. (background_thresh + dirty_thresh) / 2)
  400. break;
  401. if (!bdi->dirty_exceeded)
  402. bdi->dirty_exceeded = 1;
  403. /* Note: nr_reclaimable denotes nr_dirty + nr_unstable.
  404. * Unstable writes are a feature of certain networked
  405. * filesystems (i.e. NFS) in which data may have been
  406. * written to the server's write cache, but has not yet
  407. * been flushed to permanent storage.
  408. */
  409. if (bdi_nr_reclaimable) {
  410. writeback_inodes(&wbc);
  411. pages_written += write_chunk - wbc.nr_to_write;
  412. get_dirty_limits(&background_thresh, &dirty_thresh,
  413. &bdi_thresh, bdi);
  414. }
  415. /*
  416. * In order to avoid the stacked BDI deadlock we need
  417. * to ensure we accurately count the 'dirty' pages when
  418. * the threshold is low.
  419. *
  420. * Otherwise it would be possible to get thresh+n pages
  421. * reported dirty, even though there are thresh-m pages
  422. * actually dirty; with m+n sitting in the percpu
  423. * deltas.
  424. */
  425. if (bdi_thresh < 2*bdi_stat_error(bdi)) {
  426. bdi_nr_reclaimable = bdi_stat_sum(bdi, BDI_RECLAIMABLE);
  427. bdi_nr_writeback = bdi_stat_sum(bdi, BDI_WRITEBACK);
  428. } else if (bdi_nr_reclaimable) {
  429. bdi_nr_reclaimable = bdi_stat(bdi, BDI_RECLAIMABLE);
  430. bdi_nr_writeback = bdi_stat(bdi, BDI_WRITEBACK);
  431. }
  432. if (bdi_nr_reclaimable + bdi_nr_writeback <= bdi_thresh)
  433. break;
  434. if (pages_written >= write_chunk)
  435. break; /* We've done our duty */
  436. congestion_wait(WRITE, HZ/10);
  437. }
  438. if (bdi_nr_reclaimable + bdi_nr_writeback < bdi_thresh &&
  439. bdi->dirty_exceeded)
  440. bdi->dirty_exceeded = 0;
  441. if (writeback_in_progress(bdi))
  442. return; /* pdflush is already working this queue */
  443. /*
  444. * In laptop mode, we wait until hitting the higher threshold before
  445. * starting background writeout, and then write out all the way down
  446. * to the lower threshold. So slow writers cause minimal disk activity.
  447. *
  448. * In normal mode, we start background writeout at the lower
  449. * background_thresh, to keep the amount of dirty memory low.
  450. */
  451. if ((laptop_mode && pages_written) ||
  452. (!laptop_mode && (global_page_state(NR_FILE_DIRTY)
  453. + global_page_state(NR_UNSTABLE_NFS)
  454. > background_thresh)))
  455. pdflush_operation(background_writeout, 0);
  456. }
  457. void set_page_dirty_balance(struct page *page, int page_mkwrite)
  458. {
  459. if (set_page_dirty(page) || page_mkwrite) {
  460. struct address_space *mapping = page_mapping(page);
  461. if (mapping)
  462. balance_dirty_pages_ratelimited(mapping);
  463. }
  464. }
  465. /**
  466. * balance_dirty_pages_ratelimited_nr - balance dirty memory state
  467. * @mapping: address_space which was dirtied
  468. * @nr_pages_dirtied: number of pages which the caller has just dirtied
  469. *
  470. * Processes which are dirtying memory should call in here once for each page
  471. * which was newly dirtied. The function will periodically check the system's
  472. * dirty state and will initiate writeback if needed.
  473. *
  474. * On really big machines, get_writeback_state is expensive, so try to avoid
  475. * calling it too often (ratelimiting). But once we're over the dirty memory
  476. * limit we decrease the ratelimiting by a lot, to prevent individual processes
  477. * from overshooting the limit by (ratelimit_pages) each.
  478. */
  479. void balance_dirty_pages_ratelimited_nr(struct address_space *mapping,
  480. unsigned long nr_pages_dirtied)
  481. {
  482. static DEFINE_PER_CPU(unsigned long, ratelimits) = 0;
  483. unsigned long ratelimit;
  484. unsigned long *p;
  485. ratelimit = ratelimit_pages;
  486. if (mapping->backing_dev_info->dirty_exceeded)
  487. ratelimit = 8;
  488. /*
  489. * Check the rate limiting. Also, we do not want to throttle real-time
  490. * tasks in balance_dirty_pages(). Period.
  491. */
  492. preempt_disable();
  493. p = &__get_cpu_var(ratelimits);
  494. *p += nr_pages_dirtied;
  495. if (unlikely(*p >= ratelimit)) {
  496. *p = 0;
  497. preempt_enable();
  498. balance_dirty_pages(mapping);
  499. return;
  500. }
  501. preempt_enable();
  502. }
  503. EXPORT_SYMBOL(balance_dirty_pages_ratelimited_nr);
  504. void throttle_vm_writeout(gfp_t gfp_mask)
  505. {
  506. long background_thresh;
  507. long dirty_thresh;
  508. for ( ; ; ) {
  509. get_dirty_limits(&background_thresh, &dirty_thresh, NULL, NULL);
  510. /*
  511. * Boost the allowable dirty threshold a bit for page
  512. * allocators so they don't get DoS'ed by heavy writers
  513. */
  514. dirty_thresh += dirty_thresh / 10; /* wheeee... */
  515. if (global_page_state(NR_UNSTABLE_NFS) +
  516. global_page_state(NR_WRITEBACK) <= dirty_thresh)
  517. break;
  518. congestion_wait(WRITE, HZ/10);
  519. /*
  520. * The caller might hold locks which can prevent IO completion
  521. * or progress in the filesystem. So we cannot just sit here
  522. * waiting for IO to complete.
  523. */
  524. if ((gfp_mask & (__GFP_FS|__GFP_IO)) != (__GFP_FS|__GFP_IO))
  525. break;
  526. }
  527. }
  528. /*
  529. * writeback at least _min_pages, and keep writing until the amount of dirty
  530. * memory is less than the background threshold, or until we're all clean.
  531. */
  532. static void background_writeout(unsigned long _min_pages)
  533. {
  534. long min_pages = _min_pages;
  535. struct writeback_control wbc = {
  536. .bdi = NULL,
  537. .sync_mode = WB_SYNC_NONE,
  538. .older_than_this = NULL,
  539. .nr_to_write = 0,
  540. .nonblocking = 1,
  541. .range_cyclic = 1,
  542. };
  543. for ( ; ; ) {
  544. long background_thresh;
  545. long dirty_thresh;
  546. get_dirty_limits(&background_thresh, &dirty_thresh, NULL, NULL);
  547. if (global_page_state(NR_FILE_DIRTY) +
  548. global_page_state(NR_UNSTABLE_NFS) < background_thresh
  549. && min_pages <= 0)
  550. break;
  551. wbc.more_io = 0;
  552. wbc.encountered_congestion = 0;
  553. wbc.nr_to_write = MAX_WRITEBACK_PAGES;
  554. wbc.pages_skipped = 0;
  555. writeback_inodes(&wbc);
  556. min_pages -= MAX_WRITEBACK_PAGES - wbc.nr_to_write;
  557. if (wbc.nr_to_write > 0 || wbc.pages_skipped > 0) {
  558. /* Wrote less than expected */
  559. if (wbc.encountered_congestion || wbc.more_io)
  560. congestion_wait(WRITE, HZ/10);
  561. else
  562. break;
  563. }
  564. }
  565. }
  566. /*
  567. * Start writeback of `nr_pages' pages. If `nr_pages' is zero, write back
  568. * the whole world. Returns 0 if a pdflush thread was dispatched. Returns
  569. * -1 if all pdflush threads were busy.
  570. */
  571. int wakeup_pdflush(long nr_pages)
  572. {
  573. if (nr_pages == 0)
  574. nr_pages = global_page_state(NR_FILE_DIRTY) +
  575. global_page_state(NR_UNSTABLE_NFS);
  576. return pdflush_operation(background_writeout, nr_pages);
  577. }
  578. static void wb_timer_fn(unsigned long unused);
  579. static void laptop_timer_fn(unsigned long unused);
  580. static DEFINE_TIMER(wb_timer, wb_timer_fn, 0, 0);
  581. static DEFINE_TIMER(laptop_mode_wb_timer, laptop_timer_fn, 0, 0);
  582. /*
  583. * Periodic writeback of "old" data.
  584. *
  585. * Define "old": the first time one of an inode's pages is dirtied, we mark the
  586. * dirtying-time in the inode's address_space. So this periodic writeback code
  587. * just walks the superblock inode list, writing back any inodes which are
  588. * older than a specific point in time.
  589. *
  590. * Try to run once per dirty_writeback_interval. But if a writeback event
  591. * takes longer than a dirty_writeback_interval interval, then leave a
  592. * one-second gap.
  593. *
  594. * older_than_this takes precedence over nr_to_write. So we'll only write back
  595. * all dirty pages if they are all attached to "old" mappings.
  596. */
  597. static void wb_kupdate(unsigned long arg)
  598. {
  599. unsigned long oldest_jif;
  600. unsigned long start_jif;
  601. unsigned long next_jif;
  602. long nr_to_write;
  603. struct writeback_control wbc = {
  604. .bdi = NULL,
  605. .sync_mode = WB_SYNC_NONE,
  606. .older_than_this = &oldest_jif,
  607. .nr_to_write = 0,
  608. .nonblocking = 1,
  609. .for_kupdate = 1,
  610. .range_cyclic = 1,
  611. };
  612. sync_supers();
  613. oldest_jif = jiffies - dirty_expire_interval;
  614. start_jif = jiffies;
  615. next_jif = start_jif + dirty_writeback_interval;
  616. nr_to_write = global_page_state(NR_FILE_DIRTY) +
  617. global_page_state(NR_UNSTABLE_NFS) +
  618. (inodes_stat.nr_inodes - inodes_stat.nr_unused);
  619. while (nr_to_write > 0) {
  620. wbc.more_io = 0;
  621. wbc.encountered_congestion = 0;
  622. wbc.nr_to_write = MAX_WRITEBACK_PAGES;
  623. writeback_inodes(&wbc);
  624. if (wbc.nr_to_write > 0) {
  625. if (wbc.encountered_congestion || wbc.more_io)
  626. congestion_wait(WRITE, HZ/10);
  627. else
  628. break; /* All the old data is written */
  629. }
  630. nr_to_write -= MAX_WRITEBACK_PAGES - wbc.nr_to_write;
  631. }
  632. if (time_before(next_jif, jiffies + HZ))
  633. next_jif = jiffies + HZ;
  634. if (dirty_writeback_interval)
  635. mod_timer(&wb_timer, next_jif);
  636. }
  637. /*
  638. * sysctl handler for /proc/sys/vm/dirty_writeback_centisecs
  639. */
  640. int dirty_writeback_centisecs_handler(ctl_table *table, int write,
  641. struct file *file, void __user *buffer, size_t *length, loff_t *ppos)
  642. {
  643. proc_dointvec_userhz_jiffies(table, write, file, buffer, length, ppos);
  644. if (dirty_writeback_interval)
  645. mod_timer(&wb_timer, jiffies + dirty_writeback_interval);
  646. else
  647. del_timer(&wb_timer);
  648. return 0;
  649. }
  650. static void wb_timer_fn(unsigned long unused)
  651. {
  652. if (pdflush_operation(wb_kupdate, 0) < 0)
  653. mod_timer(&wb_timer, jiffies + HZ); /* delay 1 second */
  654. }
  655. static void laptop_flush(unsigned long unused)
  656. {
  657. sys_sync();
  658. }
  659. static void laptop_timer_fn(unsigned long unused)
  660. {
  661. pdflush_operation(laptop_flush, 0);
  662. }
  663. /*
  664. * We've spun up the disk and we're in laptop mode: schedule writeback
  665. * of all dirty data a few seconds from now. If the flush is already scheduled
  666. * then push it back - the user is still using the disk.
  667. */
  668. void laptop_io_completion(void)
  669. {
  670. mod_timer(&laptop_mode_wb_timer, jiffies + laptop_mode);
  671. }
  672. /*
  673. * We're in laptop mode and we've just synced. The sync's writes will have
  674. * caused another writeback to be scheduled by laptop_io_completion.
  675. * Nothing needs to be written back anymore, so we unschedule the writeback.
  676. */
  677. void laptop_sync_completion(void)
  678. {
  679. del_timer(&laptop_mode_wb_timer);
  680. }
  681. /*
  682. * If ratelimit_pages is too high then we can get into dirty-data overload
  683. * if a large number of processes all perform writes at the same time.
  684. * If it is too low then SMP machines will call the (expensive)
  685. * get_writeback_state too often.
  686. *
  687. * Here we set ratelimit_pages to a level which ensures that when all CPUs are
  688. * dirtying in parallel, we cannot go more than 3% (1/32) over the dirty memory
  689. * thresholds before writeback cuts in.
  690. *
  691. * But the limit should not be set too high. Because it also controls the
  692. * amount of memory which the balance_dirty_pages() caller has to write back.
  693. * If this is too large then the caller will block on the IO queue all the
  694. * time. So limit it to four megabytes - the balance_dirty_pages() caller
  695. * will write six megabyte chunks, max.
  696. */
  697. void writeback_set_ratelimit(void)
  698. {
  699. ratelimit_pages = vm_total_pages / (num_online_cpus() * 32);
  700. if (ratelimit_pages < 16)
  701. ratelimit_pages = 16;
  702. if (ratelimit_pages * PAGE_CACHE_SIZE > 4096 * 1024)
  703. ratelimit_pages = (4096 * 1024) / PAGE_CACHE_SIZE;
  704. }
  705. static int __cpuinit
  706. ratelimit_handler(struct notifier_block *self, unsigned long u, void *v)
  707. {
  708. writeback_set_ratelimit();
  709. return NOTIFY_DONE;
  710. }
  711. static struct notifier_block __cpuinitdata ratelimit_nb = {
  712. .notifier_call = ratelimit_handler,
  713. .next = NULL,
  714. };
  715. /*
  716. * Called early on to tune the page writeback dirty limits.
  717. *
  718. * We used to scale dirty pages according to how total memory
  719. * related to pages that could be allocated for buffers (by
  720. * comparing nr_free_buffer_pages() to vm_total_pages.
  721. *
  722. * However, that was when we used "dirty_ratio" to scale with
  723. * all memory, and we don't do that any more. "dirty_ratio"
  724. * is now applied to total non-HIGHPAGE memory (by subtracting
  725. * totalhigh_pages from vm_total_pages), and as such we can't
  726. * get into the old insane situation any more where we had
  727. * large amounts of dirty pages compared to a small amount of
  728. * non-HIGHMEM memory.
  729. *
  730. * But we might still want to scale the dirty_ratio by how
  731. * much memory the box has..
  732. */
  733. void __init page_writeback_init(void)
  734. {
  735. int shift;
  736. mod_timer(&wb_timer, jiffies + dirty_writeback_interval);
  737. writeback_set_ratelimit();
  738. register_cpu_notifier(&ratelimit_nb);
  739. shift = calc_period_shift();
  740. prop_descriptor_init(&vm_completions, shift);
  741. prop_descriptor_init(&vm_dirties, shift);
  742. }
  743. /**
  744. * write_cache_pages - walk the list of dirty pages of the given address space and write all of them.
  745. * @mapping: address space structure to write
  746. * @wbc: subtract the number of written pages from *@wbc->nr_to_write
  747. * @writepage: function called for each page
  748. * @data: data passed to writepage function
  749. *
  750. * If a page is already under I/O, write_cache_pages() skips it, even
  751. * if it's dirty. This is desirable behaviour for memory-cleaning writeback,
  752. * but it is INCORRECT for data-integrity system calls such as fsync(). fsync()
  753. * and msync() need to guarantee that all the data which was dirty at the time
  754. * the call was made get new I/O started against them. If wbc->sync_mode is
  755. * WB_SYNC_ALL then we were called for data integrity and we must wait for
  756. * existing IO to complete.
  757. */
  758. int write_cache_pages(struct address_space *mapping,
  759. struct writeback_control *wbc, writepage_t writepage,
  760. void *data)
  761. {
  762. struct backing_dev_info *bdi = mapping->backing_dev_info;
  763. int ret = 0;
  764. int done = 0;
  765. struct pagevec pvec;
  766. int nr_pages;
  767. pgoff_t index;
  768. pgoff_t end; /* Inclusive */
  769. int scanned = 0;
  770. int range_whole = 0;
  771. if (wbc->nonblocking && bdi_write_congested(bdi)) {
  772. wbc->encountered_congestion = 1;
  773. return 0;
  774. }
  775. pagevec_init(&pvec, 0);
  776. if (wbc->range_cyclic) {
  777. index = mapping->writeback_index; /* Start from prev offset */
  778. end = -1;
  779. } else {
  780. index = wbc->range_start >> PAGE_CACHE_SHIFT;
  781. end = wbc->range_end >> PAGE_CACHE_SHIFT;
  782. if (wbc->range_start == 0 && wbc->range_end == LLONG_MAX)
  783. range_whole = 1;
  784. scanned = 1;
  785. }
  786. retry:
  787. while (!done && (index <= end) &&
  788. (nr_pages = pagevec_lookup_tag(&pvec, mapping, &index,
  789. PAGECACHE_TAG_DIRTY,
  790. min(end - index, (pgoff_t)PAGEVEC_SIZE-1) + 1))) {
  791. unsigned i;
  792. scanned = 1;
  793. for (i = 0; i < nr_pages; i++) {
  794. struct page *page = pvec.pages[i];
  795. /*
  796. * At this point we hold neither mapping->tree_lock nor
  797. * lock on the page itself: the page may be truncated or
  798. * invalidated (changing page->mapping to NULL), or even
  799. * swizzled back from swapper_space to tmpfs file
  800. * mapping
  801. */
  802. lock_page(page);
  803. if (unlikely(page->mapping != mapping)) {
  804. unlock_page(page);
  805. continue;
  806. }
  807. if (!wbc->range_cyclic && page->index > end) {
  808. done = 1;
  809. unlock_page(page);
  810. continue;
  811. }
  812. if (wbc->sync_mode != WB_SYNC_NONE)
  813. wait_on_page_writeback(page);
  814. if (PageWriteback(page) ||
  815. !clear_page_dirty_for_io(page)) {
  816. unlock_page(page);
  817. continue;
  818. }
  819. ret = (*writepage)(page, wbc, data);
  820. if (unlikely(ret == AOP_WRITEPAGE_ACTIVATE)) {
  821. unlock_page(page);
  822. ret = 0;
  823. }
  824. if (ret || (--(wbc->nr_to_write) <= 0))
  825. done = 1;
  826. if (wbc->nonblocking && bdi_write_congested(bdi)) {
  827. wbc->encountered_congestion = 1;
  828. done = 1;
  829. }
  830. }
  831. pagevec_release(&pvec);
  832. cond_resched();
  833. }
  834. if (!scanned && !done) {
  835. /*
  836. * We hit the last page and there is more work to be done: wrap
  837. * back to the start of the file
  838. */
  839. scanned = 1;
  840. index = 0;
  841. goto retry;
  842. }
  843. if (wbc->range_cyclic || (range_whole && wbc->nr_to_write > 0))
  844. mapping->writeback_index = index;
  845. if (wbc->range_cont)
  846. wbc->range_start = index << PAGE_CACHE_SHIFT;
  847. return ret;
  848. }
  849. EXPORT_SYMBOL(write_cache_pages);
  850. /*
  851. * Function used by generic_writepages to call the real writepage
  852. * function and set the mapping flags on error
  853. */
  854. static int __writepage(struct page *page, struct writeback_control *wbc,
  855. void *data)
  856. {
  857. struct address_space *mapping = data;
  858. int ret = mapping->a_ops->writepage(page, wbc);
  859. mapping_set_error(mapping, ret);
  860. return ret;
  861. }
  862. /**
  863. * generic_writepages - walk the list of dirty pages of the given address space and writepage() all of them.
  864. * @mapping: address space structure to write
  865. * @wbc: subtract the number of written pages from *@wbc->nr_to_write
  866. *
  867. * This is a library function, which implements the writepages()
  868. * address_space_operation.
  869. */
  870. int generic_writepages(struct address_space *mapping,
  871. struct writeback_control *wbc)
  872. {
  873. /* deal with chardevs and other special file */
  874. if (!mapping->a_ops->writepage)
  875. return 0;
  876. return write_cache_pages(mapping, wbc, __writepage, mapping);
  877. }
  878. EXPORT_SYMBOL(generic_writepages);
  879. int do_writepages(struct address_space *mapping, struct writeback_control *wbc)
  880. {
  881. int ret;
  882. if (wbc->nr_to_write <= 0)
  883. return 0;
  884. wbc->for_writepages = 1;
  885. if (mapping->a_ops->writepages)
  886. ret = mapping->a_ops->writepages(mapping, wbc);
  887. else
  888. ret = generic_writepages(mapping, wbc);
  889. wbc->for_writepages = 0;
  890. return ret;
  891. }
  892. /**
  893. * write_one_page - write out a single page and optionally wait on I/O
  894. * @page: the page to write
  895. * @wait: if true, wait on writeout
  896. *
  897. * The page must be locked by the caller and will be unlocked upon return.
  898. *
  899. * write_one_page() returns a negative error code if I/O failed.
  900. */
  901. int write_one_page(struct page *page, int wait)
  902. {
  903. struct address_space *mapping = page->mapping;
  904. int ret = 0;
  905. struct writeback_control wbc = {
  906. .sync_mode = WB_SYNC_ALL,
  907. .nr_to_write = 1,
  908. };
  909. BUG_ON(!PageLocked(page));
  910. if (wait)
  911. wait_on_page_writeback(page);
  912. if (clear_page_dirty_for_io(page)) {
  913. page_cache_get(page);
  914. ret = mapping->a_ops->writepage(page, &wbc);
  915. if (ret == 0 && wait) {
  916. wait_on_page_writeback(page);
  917. if (PageError(page))
  918. ret = -EIO;
  919. }
  920. page_cache_release(page);
  921. } else {
  922. unlock_page(page);
  923. }
  924. return ret;
  925. }
  926. EXPORT_SYMBOL(write_one_page);
  927. /*
  928. * For address_spaces which do not use buffers nor write back.
  929. */
  930. int __set_page_dirty_no_writeback(struct page *page)
  931. {
  932. if (!PageDirty(page))
  933. SetPageDirty(page);
  934. return 0;
  935. }
  936. /*
  937. * For address_spaces which do not use buffers. Just tag the page as dirty in
  938. * its radix tree.
  939. *
  940. * This is also used when a single buffer is being dirtied: we want to set the
  941. * page dirty in that case, but not all the buffers. This is a "bottom-up"
  942. * dirtying, whereas __set_page_dirty_buffers() is a "top-down" dirtying.
  943. *
  944. * Most callers have locked the page, which pins the address_space in memory.
  945. * But zap_pte_range() does not lock the page, however in that case the
  946. * mapping is pinned by the vma's ->vm_file reference.
  947. *
  948. * We take care to handle the case where the page was truncated from the
  949. * mapping by re-checking page_mapping() inside tree_lock.
  950. */
  951. int __set_page_dirty_nobuffers(struct page *page)
  952. {
  953. if (!TestSetPageDirty(page)) {
  954. struct address_space *mapping = page_mapping(page);
  955. struct address_space *mapping2;
  956. if (!mapping)
  957. return 1;
  958. spin_lock_irq(&mapping->tree_lock);
  959. mapping2 = page_mapping(page);
  960. if (mapping2) { /* Race with truncate? */
  961. BUG_ON(mapping2 != mapping);
  962. WARN_ON_ONCE(!PagePrivate(page) && !PageUptodate(page));
  963. if (mapping_cap_account_dirty(mapping)) {
  964. __inc_zone_page_state(page, NR_FILE_DIRTY);
  965. __inc_bdi_stat(mapping->backing_dev_info,
  966. BDI_RECLAIMABLE);
  967. task_io_account_write(PAGE_CACHE_SIZE);
  968. }
  969. radix_tree_tag_set(&mapping->page_tree,
  970. page_index(page), PAGECACHE_TAG_DIRTY);
  971. }
  972. spin_unlock_irq(&mapping->tree_lock);
  973. if (mapping->host) {
  974. /* !PageAnon && !swapper_space */
  975. __mark_inode_dirty(mapping->host, I_DIRTY_PAGES);
  976. }
  977. return 1;
  978. }
  979. return 0;
  980. }
  981. EXPORT_SYMBOL(__set_page_dirty_nobuffers);
  982. /*
  983. * When a writepage implementation decides that it doesn't want to write this
  984. * page for some reason, it should redirty the locked page via
  985. * redirty_page_for_writepage() and it should then unlock the page and return 0
  986. */
  987. int redirty_page_for_writepage(struct writeback_control *wbc, struct page *page)
  988. {
  989. wbc->pages_skipped++;
  990. return __set_page_dirty_nobuffers(page);
  991. }
  992. EXPORT_SYMBOL(redirty_page_for_writepage);
  993. /*
  994. * If the mapping doesn't provide a set_page_dirty a_op, then
  995. * just fall through and assume that it wants buffer_heads.
  996. */
  997. static int __set_page_dirty(struct page *page)
  998. {
  999. struct address_space *mapping = page_mapping(page);
  1000. if (likely(mapping)) {
  1001. int (*spd)(struct page *) = mapping->a_ops->set_page_dirty;
  1002. #ifdef CONFIG_BLOCK
  1003. if (!spd)
  1004. spd = __set_page_dirty_buffers;
  1005. #endif
  1006. return (*spd)(page);
  1007. }
  1008. if (!PageDirty(page)) {
  1009. if (!TestSetPageDirty(page))
  1010. return 1;
  1011. }
  1012. return 0;
  1013. }
  1014. int set_page_dirty(struct page *page)
  1015. {
  1016. int ret = __set_page_dirty(page);
  1017. if (ret)
  1018. task_dirty_inc(current);
  1019. return ret;
  1020. }
  1021. EXPORT_SYMBOL(set_page_dirty);
  1022. /*
  1023. * set_page_dirty() is racy if the caller has no reference against
  1024. * page->mapping->host, and if the page is unlocked. This is because another
  1025. * CPU could truncate the page off the mapping and then free the mapping.
  1026. *
  1027. * Usually, the page _is_ locked, or the caller is a user-space process which
  1028. * holds a reference on the inode by having an open file.
  1029. *
  1030. * In other cases, the page should be locked before running set_page_dirty().
  1031. */
  1032. int set_page_dirty_lock(struct page *page)
  1033. {
  1034. int ret;
  1035. lock_page_nosync(page);
  1036. ret = set_page_dirty(page);
  1037. unlock_page(page);
  1038. return ret;
  1039. }
  1040. EXPORT_SYMBOL(set_page_dirty_lock);
  1041. /*
  1042. * Clear a page's dirty flag, while caring for dirty memory accounting.
  1043. * Returns true if the page was previously dirty.
  1044. *
  1045. * This is for preparing to put the page under writeout. We leave the page
  1046. * tagged as dirty in the radix tree so that a concurrent write-for-sync
  1047. * can discover it via a PAGECACHE_TAG_DIRTY walk. The ->writepage
  1048. * implementation will run either set_page_writeback() or set_page_dirty(),
  1049. * at which stage we bring the page's dirty flag and radix-tree dirty tag
  1050. * back into sync.
  1051. *
  1052. * This incoherency between the page's dirty flag and radix-tree tag is
  1053. * unfortunate, but it only exists while the page is locked.
  1054. */
  1055. int clear_page_dirty_for_io(struct page *page)
  1056. {
  1057. struct address_space *mapping = page_mapping(page);
  1058. BUG_ON(!PageLocked(page));
  1059. ClearPageReclaim(page);
  1060. if (mapping && mapping_cap_account_dirty(mapping)) {
  1061. /*
  1062. * Yes, Virginia, this is indeed insane.
  1063. *
  1064. * We use this sequence to make sure that
  1065. * (a) we account for dirty stats properly
  1066. * (b) we tell the low-level filesystem to
  1067. * mark the whole page dirty if it was
  1068. * dirty in a pagetable. Only to then
  1069. * (c) clean the page again and return 1 to
  1070. * cause the writeback.
  1071. *
  1072. * This way we avoid all nasty races with the
  1073. * dirty bit in multiple places and clearing
  1074. * them concurrently from different threads.
  1075. *
  1076. * Note! Normally the "set_page_dirty(page)"
  1077. * has no effect on the actual dirty bit - since
  1078. * that will already usually be set. But we
  1079. * need the side effects, and it can help us
  1080. * avoid races.
  1081. *
  1082. * We basically use the page "master dirty bit"
  1083. * as a serialization point for all the different
  1084. * threads doing their things.
  1085. */
  1086. if (page_mkclean(page))
  1087. set_page_dirty(page);
  1088. /*
  1089. * We carefully synchronise fault handlers against
  1090. * installing a dirty pte and marking the page dirty
  1091. * at this point. We do this by having them hold the
  1092. * page lock at some point after installing their
  1093. * pte, but before marking the page dirty.
  1094. * Pages are always locked coming in here, so we get
  1095. * the desired exclusion. See mm/memory.c:do_wp_page()
  1096. * for more comments.
  1097. */
  1098. if (TestClearPageDirty(page)) {
  1099. dec_zone_page_state(page, NR_FILE_DIRTY);
  1100. dec_bdi_stat(mapping->backing_dev_info,
  1101. BDI_RECLAIMABLE);
  1102. return 1;
  1103. }
  1104. return 0;
  1105. }
  1106. return TestClearPageDirty(page);
  1107. }
  1108. EXPORT_SYMBOL(clear_page_dirty_for_io);
  1109. int test_clear_page_writeback(struct page *page)
  1110. {
  1111. struct address_space *mapping = page_mapping(page);
  1112. int ret;
  1113. if (mapping) {
  1114. struct backing_dev_info *bdi = mapping->backing_dev_info;
  1115. unsigned long flags;
  1116. spin_lock_irqsave(&mapping->tree_lock, flags);
  1117. ret = TestClearPageWriteback(page);
  1118. if (ret) {
  1119. radix_tree_tag_clear(&mapping->page_tree,
  1120. page_index(page),
  1121. PAGECACHE_TAG_WRITEBACK);
  1122. if (bdi_cap_account_writeback(bdi)) {
  1123. __dec_bdi_stat(bdi, BDI_WRITEBACK);
  1124. __bdi_writeout_inc(bdi);
  1125. }
  1126. }
  1127. spin_unlock_irqrestore(&mapping->tree_lock, flags);
  1128. } else {
  1129. ret = TestClearPageWriteback(page);
  1130. }
  1131. if (ret)
  1132. dec_zone_page_state(page, NR_WRITEBACK);
  1133. return ret;
  1134. }
  1135. int test_set_page_writeback(struct page *page)
  1136. {
  1137. struct address_space *mapping = page_mapping(page);
  1138. int ret;
  1139. if (mapping) {
  1140. struct backing_dev_info *bdi = mapping->backing_dev_info;
  1141. unsigned long flags;
  1142. spin_lock_irqsave(&mapping->tree_lock, flags);
  1143. ret = TestSetPageWriteback(page);
  1144. if (!ret) {
  1145. radix_tree_tag_set(&mapping->page_tree,
  1146. page_index(page),
  1147. PAGECACHE_TAG_WRITEBACK);
  1148. if (bdi_cap_account_writeback(bdi))
  1149. __inc_bdi_stat(bdi, BDI_WRITEBACK);
  1150. }
  1151. if (!PageDirty(page))
  1152. radix_tree_tag_clear(&mapping->page_tree,
  1153. page_index(page),
  1154. PAGECACHE_TAG_DIRTY);
  1155. spin_unlock_irqrestore(&mapping->tree_lock, flags);
  1156. } else {
  1157. ret = TestSetPageWriteback(page);
  1158. }
  1159. if (!ret)
  1160. inc_zone_page_state(page, NR_WRITEBACK);
  1161. return ret;
  1162. }
  1163. EXPORT_SYMBOL(test_set_page_writeback);
  1164. /*
  1165. * Return true if any of the pages in the mapping are marked with the
  1166. * passed tag.
  1167. */
  1168. int mapping_tagged(struct address_space *mapping, int tag)
  1169. {
  1170. int ret;
  1171. rcu_read_lock();
  1172. ret = radix_tree_tagged(&mapping->page_tree, tag);
  1173. rcu_read_unlock();
  1174. return ret;
  1175. }
  1176. EXPORT_SYMBOL(mapping_tagged);