page-writeback.c 40 KB

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