page-writeback.c 40 KB

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