page-writeback.c 29 KB

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