page-writeback.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844
  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/blkdev.h>
  24. #include <linux/mpage.h>
  25. #include <linux/rmap.h>
  26. #include <linux/percpu.h>
  27. #include <linux/notifier.h>
  28. #include <linux/smp.h>
  29. #include <linux/sysctl.h>
  30. #include <linux/cpu.h>
  31. #include <linux/syscalls.h>
  32. /*
  33. * The maximum number of pages to writeout in a single bdflush/kupdate
  34. * operation. We do this so we don't hold I_LOCK against an inode for
  35. * enormous amounts of time, which would block a userspace task which has
  36. * been forced to throttle against that inode. Also, the code reevaluates
  37. * the dirty each time it has written this many pages.
  38. */
  39. #define MAX_WRITEBACK_PAGES 1024
  40. /*
  41. * After a CPU has dirtied this many pages, balance_dirty_pages_ratelimited
  42. * will look to see if it needs to force writeback or throttling.
  43. */
  44. static long ratelimit_pages = 32;
  45. static int dirty_exceeded __cacheline_aligned_in_smp; /* Dirty mem may be over limit */
  46. /*
  47. * When balance_dirty_pages decides that the caller needs to perform some
  48. * non-background writeback, this is how many pages it will attempt to write.
  49. * It should be somewhat larger than RATELIMIT_PAGES to ensure that reasonably
  50. * large amounts of I/O are submitted.
  51. */
  52. static inline long sync_writeback_pages(void)
  53. {
  54. return ratelimit_pages + ratelimit_pages / 2;
  55. }
  56. /* The following parameters are exported via /proc/sys/vm */
  57. /*
  58. * Start background writeback (via pdflush) at this percentage
  59. */
  60. int dirty_background_ratio = 10;
  61. /*
  62. * The generator of dirty data starts writeback at this percentage
  63. */
  64. int vm_dirty_ratio = 40;
  65. /*
  66. * The interval between `kupdate'-style writebacks, in jiffies
  67. */
  68. int dirty_writeback_interval = 5 * HZ;
  69. /*
  70. * The longest number of jiffies for which data is allowed to remain dirty
  71. */
  72. int dirty_expire_interval = 30 * HZ;
  73. /*
  74. * Flag that makes the machine dump writes/reads and block dirtyings.
  75. */
  76. int block_dump;
  77. /*
  78. * Flag that puts the machine in "laptop mode". Doubles as a timeout in jiffies:
  79. * a full sync is triggered after this time elapses without any disk activity.
  80. */
  81. int laptop_mode;
  82. EXPORT_SYMBOL(laptop_mode);
  83. /* End of sysctl-exported parameters */
  84. static void background_writeout(unsigned long _min_pages);
  85. /*
  86. * Work out the current dirty-memory clamping and background writeout
  87. * thresholds.
  88. *
  89. * The main aim here is to lower them aggressively if there is a lot of mapped
  90. * memory around. To avoid stressing page reclaim with lots of unreclaimable
  91. * pages. It is better to clamp down on writers than to start swapping, and
  92. * performing lots of scanning.
  93. *
  94. * We only allow 1/2 of the currently-unmapped memory to be dirtied.
  95. *
  96. * We don't permit the clamping level to fall below 5% - that is getting rather
  97. * excessive.
  98. *
  99. * We make sure that the background writeout level is below the adjusted
  100. * clamping level.
  101. */
  102. static void
  103. get_dirty_limits(long *pbackground, long *pdirty,
  104. struct address_space *mapping)
  105. {
  106. int background_ratio; /* Percentages */
  107. int dirty_ratio;
  108. int unmapped_ratio;
  109. long background;
  110. long dirty;
  111. unsigned long available_memory = vm_total_pages;
  112. struct task_struct *tsk;
  113. #ifdef CONFIG_HIGHMEM
  114. /*
  115. * If this mapping can only allocate from low memory,
  116. * we exclude high memory from our count.
  117. */
  118. if (mapping && !(mapping_gfp_mask(mapping) & __GFP_HIGHMEM))
  119. available_memory -= totalhigh_pages;
  120. #endif
  121. unmapped_ratio = 100 - ((global_page_state(NR_FILE_MAPPED) +
  122. global_page_state(NR_ANON_PAGES)) * 100) /
  123. vm_total_pages;
  124. dirty_ratio = vm_dirty_ratio;
  125. if (dirty_ratio > unmapped_ratio / 2)
  126. dirty_ratio = unmapped_ratio / 2;
  127. if (dirty_ratio < 5)
  128. dirty_ratio = 5;
  129. background_ratio = dirty_background_ratio;
  130. if (background_ratio >= dirty_ratio)
  131. background_ratio = dirty_ratio / 2;
  132. background = (background_ratio * available_memory) / 100;
  133. dirty = (dirty_ratio * available_memory) / 100;
  134. tsk = current;
  135. if (tsk->flags & PF_LESS_THROTTLE || rt_task(tsk)) {
  136. background += background / 4;
  137. dirty += dirty / 4;
  138. }
  139. *pbackground = background;
  140. *pdirty = dirty;
  141. }
  142. /*
  143. * balance_dirty_pages() must be called by processes which are generating dirty
  144. * data. It looks at the number of dirty pages in the machine and will force
  145. * the caller to perform writeback if the system is over `vm_dirty_ratio'.
  146. * If we're over `background_thresh' then pdflush is woken to perform some
  147. * writeout.
  148. */
  149. static void balance_dirty_pages(struct address_space *mapping)
  150. {
  151. long nr_reclaimable;
  152. long background_thresh;
  153. long dirty_thresh;
  154. unsigned long pages_written = 0;
  155. unsigned long write_chunk = sync_writeback_pages();
  156. struct backing_dev_info *bdi = mapping->backing_dev_info;
  157. for (;;) {
  158. struct writeback_control wbc = {
  159. .bdi = bdi,
  160. .sync_mode = WB_SYNC_NONE,
  161. .older_than_this = NULL,
  162. .nr_to_write = write_chunk,
  163. .range_cyclic = 1,
  164. };
  165. get_dirty_limits(&background_thresh, &dirty_thresh, mapping);
  166. nr_reclaimable = global_page_state(NR_FILE_DIRTY) +
  167. global_page_state(NR_UNSTABLE_NFS);
  168. if (nr_reclaimable + global_page_state(NR_WRITEBACK) <=
  169. dirty_thresh)
  170. break;
  171. if (!dirty_exceeded)
  172. dirty_exceeded = 1;
  173. /* Note: nr_reclaimable denotes nr_dirty + nr_unstable.
  174. * Unstable writes are a feature of certain networked
  175. * filesystems (i.e. NFS) in which data may have been
  176. * written to the server's write cache, but has not yet
  177. * been flushed to permanent storage.
  178. */
  179. if (nr_reclaimable) {
  180. writeback_inodes(&wbc);
  181. get_dirty_limits(&background_thresh,
  182. &dirty_thresh, mapping);
  183. nr_reclaimable = global_page_state(NR_FILE_DIRTY) +
  184. global_page_state(NR_UNSTABLE_NFS);
  185. if (nr_reclaimable +
  186. global_page_state(NR_WRITEBACK)
  187. <= dirty_thresh)
  188. break;
  189. pages_written += write_chunk - wbc.nr_to_write;
  190. if (pages_written >= write_chunk)
  191. break; /* We've done our duty */
  192. }
  193. blk_congestion_wait(WRITE, HZ/10);
  194. }
  195. if (nr_reclaimable + global_page_state(NR_WRITEBACK)
  196. <= dirty_thresh && dirty_exceeded)
  197. dirty_exceeded = 0;
  198. if (writeback_in_progress(bdi))
  199. return; /* pdflush is already working this queue */
  200. /*
  201. * In laptop mode, we wait until hitting the higher threshold before
  202. * starting background writeout, and then write out all the way down
  203. * to the lower threshold. So slow writers cause minimal disk activity.
  204. *
  205. * In normal mode, we start background writeout at the lower
  206. * background_thresh, to keep the amount of dirty memory low.
  207. */
  208. if ((laptop_mode && pages_written) ||
  209. (!laptop_mode && (nr_reclaimable > background_thresh)))
  210. pdflush_operation(background_writeout, 0);
  211. }
  212. void set_page_dirty_balance(struct page *page)
  213. {
  214. if (set_page_dirty(page)) {
  215. struct address_space *mapping = page_mapping(page);
  216. if (mapping)
  217. balance_dirty_pages_ratelimited(mapping);
  218. }
  219. }
  220. /**
  221. * balance_dirty_pages_ratelimited_nr - balance dirty memory state
  222. * @mapping: address_space which was dirtied
  223. * @nr_pages_dirtied: number of pages which the caller has just dirtied
  224. *
  225. * Processes which are dirtying memory should call in here once for each page
  226. * which was newly dirtied. The function will periodically check the system's
  227. * dirty state and will initiate writeback if needed.
  228. *
  229. * On really big machines, get_writeback_state is expensive, so try to avoid
  230. * calling it too often (ratelimiting). But once we're over the dirty memory
  231. * limit we decrease the ratelimiting by a lot, to prevent individual processes
  232. * from overshooting the limit by (ratelimit_pages) each.
  233. */
  234. void balance_dirty_pages_ratelimited_nr(struct address_space *mapping,
  235. unsigned long nr_pages_dirtied)
  236. {
  237. static DEFINE_PER_CPU(unsigned long, ratelimits) = 0;
  238. unsigned long ratelimit;
  239. unsigned long *p;
  240. ratelimit = ratelimit_pages;
  241. if (dirty_exceeded)
  242. ratelimit = 8;
  243. /*
  244. * Check the rate limiting. Also, we do not want to throttle real-time
  245. * tasks in balance_dirty_pages(). Period.
  246. */
  247. preempt_disable();
  248. p = &__get_cpu_var(ratelimits);
  249. *p += nr_pages_dirtied;
  250. if (unlikely(*p >= ratelimit)) {
  251. *p = 0;
  252. preempt_enable();
  253. balance_dirty_pages(mapping);
  254. return;
  255. }
  256. preempt_enable();
  257. }
  258. EXPORT_SYMBOL(balance_dirty_pages_ratelimited_nr);
  259. void throttle_vm_writeout(void)
  260. {
  261. long background_thresh;
  262. long dirty_thresh;
  263. for ( ; ; ) {
  264. get_dirty_limits(&background_thresh, &dirty_thresh, NULL);
  265. /*
  266. * Boost the allowable dirty threshold a bit for page
  267. * allocators so they don't get DoS'ed by heavy writers
  268. */
  269. dirty_thresh += dirty_thresh / 10; /* wheeee... */
  270. if (global_page_state(NR_UNSTABLE_NFS) +
  271. global_page_state(NR_WRITEBACK) <= dirty_thresh)
  272. break;
  273. blk_congestion_wait(WRITE, HZ/10);
  274. }
  275. }
  276. /*
  277. * writeback at least _min_pages, and keep writing until the amount of dirty
  278. * memory is less than the background threshold, or until we're all clean.
  279. */
  280. static void background_writeout(unsigned long _min_pages)
  281. {
  282. long min_pages = _min_pages;
  283. struct writeback_control wbc = {
  284. .bdi = NULL,
  285. .sync_mode = WB_SYNC_NONE,
  286. .older_than_this = NULL,
  287. .nr_to_write = 0,
  288. .nonblocking = 1,
  289. .range_cyclic = 1,
  290. };
  291. for ( ; ; ) {
  292. long background_thresh;
  293. long dirty_thresh;
  294. get_dirty_limits(&background_thresh, &dirty_thresh, NULL);
  295. if (global_page_state(NR_FILE_DIRTY) +
  296. global_page_state(NR_UNSTABLE_NFS) < background_thresh
  297. && min_pages <= 0)
  298. break;
  299. wbc.encountered_congestion = 0;
  300. wbc.nr_to_write = MAX_WRITEBACK_PAGES;
  301. wbc.pages_skipped = 0;
  302. writeback_inodes(&wbc);
  303. min_pages -= MAX_WRITEBACK_PAGES - wbc.nr_to_write;
  304. if (wbc.nr_to_write > 0 || wbc.pages_skipped > 0) {
  305. /* Wrote less than expected */
  306. blk_congestion_wait(WRITE, HZ/10);
  307. if (!wbc.encountered_congestion)
  308. break;
  309. }
  310. }
  311. }
  312. /*
  313. * Start writeback of `nr_pages' pages. If `nr_pages' is zero, write back
  314. * the whole world. Returns 0 if a pdflush thread was dispatched. Returns
  315. * -1 if all pdflush threads were busy.
  316. */
  317. int wakeup_pdflush(long nr_pages)
  318. {
  319. if (nr_pages == 0)
  320. nr_pages = global_page_state(NR_FILE_DIRTY) +
  321. global_page_state(NR_UNSTABLE_NFS);
  322. return pdflush_operation(background_writeout, nr_pages);
  323. }
  324. static void wb_timer_fn(unsigned long unused);
  325. static void laptop_timer_fn(unsigned long unused);
  326. static DEFINE_TIMER(wb_timer, wb_timer_fn, 0, 0);
  327. static DEFINE_TIMER(laptop_mode_wb_timer, laptop_timer_fn, 0, 0);
  328. /*
  329. * Periodic writeback of "old" data.
  330. *
  331. * Define "old": the first time one of an inode's pages is dirtied, we mark the
  332. * dirtying-time in the inode's address_space. So this periodic writeback code
  333. * just walks the superblock inode list, writing back any inodes which are
  334. * older than a specific point in time.
  335. *
  336. * Try to run once per dirty_writeback_interval. But if a writeback event
  337. * takes longer than a dirty_writeback_interval interval, then leave a
  338. * one-second gap.
  339. *
  340. * older_than_this takes precedence over nr_to_write. So we'll only write back
  341. * all dirty pages if they are all attached to "old" mappings.
  342. */
  343. static void wb_kupdate(unsigned long arg)
  344. {
  345. unsigned long oldest_jif;
  346. unsigned long start_jif;
  347. unsigned long next_jif;
  348. long nr_to_write;
  349. struct writeback_control wbc = {
  350. .bdi = NULL,
  351. .sync_mode = WB_SYNC_NONE,
  352. .older_than_this = &oldest_jif,
  353. .nr_to_write = 0,
  354. .nonblocking = 1,
  355. .for_kupdate = 1,
  356. .range_cyclic = 1,
  357. };
  358. sync_supers();
  359. oldest_jif = jiffies - dirty_expire_interval;
  360. start_jif = jiffies;
  361. next_jif = start_jif + dirty_writeback_interval;
  362. nr_to_write = global_page_state(NR_FILE_DIRTY) +
  363. global_page_state(NR_UNSTABLE_NFS) +
  364. (inodes_stat.nr_inodes - inodes_stat.nr_unused);
  365. while (nr_to_write > 0) {
  366. wbc.encountered_congestion = 0;
  367. wbc.nr_to_write = MAX_WRITEBACK_PAGES;
  368. writeback_inodes(&wbc);
  369. if (wbc.nr_to_write > 0) {
  370. if (wbc.encountered_congestion)
  371. blk_congestion_wait(WRITE, HZ/10);
  372. else
  373. break; /* All the old data is written */
  374. }
  375. nr_to_write -= MAX_WRITEBACK_PAGES - wbc.nr_to_write;
  376. }
  377. if (time_before(next_jif, jiffies + HZ))
  378. next_jif = jiffies + HZ;
  379. if (dirty_writeback_interval)
  380. mod_timer(&wb_timer, next_jif);
  381. }
  382. /*
  383. * sysctl handler for /proc/sys/vm/dirty_writeback_centisecs
  384. */
  385. int dirty_writeback_centisecs_handler(ctl_table *table, int write,
  386. struct file *file, void __user *buffer, size_t *length, loff_t *ppos)
  387. {
  388. proc_dointvec_userhz_jiffies(table, write, file, buffer, length, ppos);
  389. if (dirty_writeback_interval) {
  390. mod_timer(&wb_timer,
  391. jiffies + dirty_writeback_interval);
  392. } else {
  393. del_timer(&wb_timer);
  394. }
  395. return 0;
  396. }
  397. static void wb_timer_fn(unsigned long unused)
  398. {
  399. if (pdflush_operation(wb_kupdate, 0) < 0)
  400. mod_timer(&wb_timer, jiffies + HZ); /* delay 1 second */
  401. }
  402. static void laptop_flush(unsigned long unused)
  403. {
  404. sys_sync();
  405. }
  406. static void laptop_timer_fn(unsigned long unused)
  407. {
  408. pdflush_operation(laptop_flush, 0);
  409. }
  410. /*
  411. * We've spun up the disk and we're in laptop mode: schedule writeback
  412. * of all dirty data a few seconds from now. If the flush is already scheduled
  413. * then push it back - the user is still using the disk.
  414. */
  415. void laptop_io_completion(void)
  416. {
  417. mod_timer(&laptop_mode_wb_timer, jiffies + laptop_mode);
  418. }
  419. /*
  420. * We're in laptop mode and we've just synced. The sync's writes will have
  421. * caused another writeback to be scheduled by laptop_io_completion.
  422. * Nothing needs to be written back anymore, so we unschedule the writeback.
  423. */
  424. void laptop_sync_completion(void)
  425. {
  426. del_timer(&laptop_mode_wb_timer);
  427. }
  428. /*
  429. * If ratelimit_pages is too high then we can get into dirty-data overload
  430. * if a large number of processes all perform writes at the same time.
  431. * If it is too low then SMP machines will call the (expensive)
  432. * get_writeback_state too often.
  433. *
  434. * Here we set ratelimit_pages to a level which ensures that when all CPUs are
  435. * dirtying in parallel, we cannot go more than 3% (1/32) over the dirty memory
  436. * thresholds before writeback cuts in.
  437. *
  438. * But the limit should not be set too high. Because it also controls the
  439. * amount of memory which the balance_dirty_pages() caller has to write back.
  440. * If this is too large then the caller will block on the IO queue all the
  441. * time. So limit it to four megabytes - the balance_dirty_pages() caller
  442. * will write six megabyte chunks, max.
  443. */
  444. void writeback_set_ratelimit(void)
  445. {
  446. ratelimit_pages = vm_total_pages / (num_online_cpus() * 32);
  447. if (ratelimit_pages < 16)
  448. ratelimit_pages = 16;
  449. if (ratelimit_pages * PAGE_CACHE_SIZE > 4096 * 1024)
  450. ratelimit_pages = (4096 * 1024) / PAGE_CACHE_SIZE;
  451. }
  452. static int __cpuinit
  453. ratelimit_handler(struct notifier_block *self, unsigned long u, void *v)
  454. {
  455. writeback_set_ratelimit();
  456. return 0;
  457. }
  458. static struct notifier_block __cpuinitdata ratelimit_nb = {
  459. .notifier_call = ratelimit_handler,
  460. .next = NULL,
  461. };
  462. /*
  463. * If the machine has a large highmem:lowmem ratio then scale back the default
  464. * dirty memory thresholds: allowing too much dirty highmem pins an excessive
  465. * number of buffer_heads.
  466. */
  467. void __init page_writeback_init(void)
  468. {
  469. long buffer_pages = nr_free_buffer_pages();
  470. long correction;
  471. correction = (100 * 4 * buffer_pages) / vm_total_pages;
  472. if (correction < 100) {
  473. dirty_background_ratio *= correction;
  474. dirty_background_ratio /= 100;
  475. vm_dirty_ratio *= correction;
  476. vm_dirty_ratio /= 100;
  477. if (dirty_background_ratio <= 0)
  478. dirty_background_ratio = 1;
  479. if (vm_dirty_ratio <= 0)
  480. vm_dirty_ratio = 1;
  481. }
  482. mod_timer(&wb_timer, jiffies + dirty_writeback_interval);
  483. writeback_set_ratelimit();
  484. register_cpu_notifier(&ratelimit_nb);
  485. }
  486. int do_writepages(struct address_space *mapping, struct writeback_control *wbc)
  487. {
  488. int ret;
  489. if (wbc->nr_to_write <= 0)
  490. return 0;
  491. wbc->for_writepages = 1;
  492. if (mapping->a_ops->writepages)
  493. ret = mapping->a_ops->writepages(mapping, wbc);
  494. else
  495. ret = generic_writepages(mapping, wbc);
  496. wbc->for_writepages = 0;
  497. return ret;
  498. }
  499. /**
  500. * write_one_page - write out a single page and optionally wait on I/O
  501. *
  502. * @page: the page to write
  503. * @wait: if true, wait on writeout
  504. *
  505. * The page must be locked by the caller and will be unlocked upon return.
  506. *
  507. * write_one_page() returns a negative error code if I/O failed.
  508. */
  509. int write_one_page(struct page *page, int wait)
  510. {
  511. struct address_space *mapping = page->mapping;
  512. int ret = 0;
  513. struct writeback_control wbc = {
  514. .sync_mode = WB_SYNC_ALL,
  515. .nr_to_write = 1,
  516. };
  517. BUG_ON(!PageLocked(page));
  518. if (wait)
  519. wait_on_page_writeback(page);
  520. if (clear_page_dirty_for_io(page)) {
  521. page_cache_get(page);
  522. ret = mapping->a_ops->writepage(page, &wbc);
  523. if (ret == 0 && wait) {
  524. wait_on_page_writeback(page);
  525. if (PageError(page))
  526. ret = -EIO;
  527. }
  528. page_cache_release(page);
  529. } else {
  530. unlock_page(page);
  531. }
  532. return ret;
  533. }
  534. EXPORT_SYMBOL(write_one_page);
  535. /*
  536. * For address_spaces which do not use buffers. Just tag the page as dirty in
  537. * its radix tree.
  538. *
  539. * This is also used when a single buffer is being dirtied: we want to set the
  540. * page dirty in that case, but not all the buffers. This is a "bottom-up"
  541. * dirtying, whereas __set_page_dirty_buffers() is a "top-down" dirtying.
  542. *
  543. * Most callers have locked the page, which pins the address_space in memory.
  544. * But zap_pte_range() does not lock the page, however in that case the
  545. * mapping is pinned by the vma's ->vm_file reference.
  546. *
  547. * We take care to handle the case where the page was truncated from the
  548. * mapping by re-checking page_mapping() insode tree_lock.
  549. */
  550. int __set_page_dirty_nobuffers(struct page *page)
  551. {
  552. if (!TestSetPageDirty(page)) {
  553. struct address_space *mapping = page_mapping(page);
  554. struct address_space *mapping2;
  555. if (mapping) {
  556. write_lock_irq(&mapping->tree_lock);
  557. mapping2 = page_mapping(page);
  558. if (mapping2) { /* Race with truncate? */
  559. BUG_ON(mapping2 != mapping);
  560. if (mapping_cap_account_dirty(mapping))
  561. __inc_zone_page_state(page,
  562. NR_FILE_DIRTY);
  563. radix_tree_tag_set(&mapping->page_tree,
  564. page_index(page), PAGECACHE_TAG_DIRTY);
  565. }
  566. write_unlock_irq(&mapping->tree_lock);
  567. if (mapping->host) {
  568. /* !PageAnon && !swapper_space */
  569. __mark_inode_dirty(mapping->host,
  570. I_DIRTY_PAGES);
  571. }
  572. }
  573. return 1;
  574. }
  575. return 0;
  576. }
  577. EXPORT_SYMBOL(__set_page_dirty_nobuffers);
  578. /*
  579. * When a writepage implementation decides that it doesn't want to write this
  580. * page for some reason, it should redirty the locked page via
  581. * redirty_page_for_writepage() and it should then unlock the page and return 0
  582. */
  583. int redirty_page_for_writepage(struct writeback_control *wbc, struct page *page)
  584. {
  585. wbc->pages_skipped++;
  586. return __set_page_dirty_nobuffers(page);
  587. }
  588. EXPORT_SYMBOL(redirty_page_for_writepage);
  589. /*
  590. * If the mapping doesn't provide a set_page_dirty a_op, then
  591. * just fall through and assume that it wants buffer_heads.
  592. */
  593. int fastcall set_page_dirty(struct page *page)
  594. {
  595. struct address_space *mapping = page_mapping(page);
  596. if (likely(mapping)) {
  597. int (*spd)(struct page *) = mapping->a_ops->set_page_dirty;
  598. if (spd)
  599. return (*spd)(page);
  600. return __set_page_dirty_buffers(page);
  601. }
  602. if (!PageDirty(page)) {
  603. if (!TestSetPageDirty(page))
  604. return 1;
  605. }
  606. return 0;
  607. }
  608. EXPORT_SYMBOL(set_page_dirty);
  609. /*
  610. * set_page_dirty() is racy if the caller has no reference against
  611. * page->mapping->host, and if the page is unlocked. This is because another
  612. * CPU could truncate the page off the mapping and then free the mapping.
  613. *
  614. * Usually, the page _is_ locked, or the caller is a user-space process which
  615. * holds a reference on the inode by having an open file.
  616. *
  617. * In other cases, the page should be locked before running set_page_dirty().
  618. */
  619. int set_page_dirty_lock(struct page *page)
  620. {
  621. int ret;
  622. lock_page_nosync(page);
  623. ret = set_page_dirty(page);
  624. unlock_page(page);
  625. return ret;
  626. }
  627. EXPORT_SYMBOL(set_page_dirty_lock);
  628. /*
  629. * Clear a page's dirty flag, while caring for dirty memory accounting.
  630. * Returns true if the page was previously dirty.
  631. */
  632. int test_clear_page_dirty(struct page *page)
  633. {
  634. struct address_space *mapping = page_mapping(page);
  635. unsigned long flags;
  636. if (mapping) {
  637. write_lock_irqsave(&mapping->tree_lock, flags);
  638. if (TestClearPageDirty(page)) {
  639. radix_tree_tag_clear(&mapping->page_tree,
  640. page_index(page),
  641. PAGECACHE_TAG_DIRTY);
  642. write_unlock_irqrestore(&mapping->tree_lock, flags);
  643. /*
  644. * We can continue to use `mapping' here because the
  645. * page is locked, which pins the address_space
  646. */
  647. if (mapping_cap_account_dirty(mapping)) {
  648. page_mkclean(page);
  649. dec_zone_page_state(page, NR_FILE_DIRTY);
  650. }
  651. return 1;
  652. }
  653. write_unlock_irqrestore(&mapping->tree_lock, flags);
  654. return 0;
  655. }
  656. return TestClearPageDirty(page);
  657. }
  658. EXPORT_SYMBOL(test_clear_page_dirty);
  659. /*
  660. * Clear a page's dirty flag, while caring for dirty memory accounting.
  661. * Returns true if the page was previously dirty.
  662. *
  663. * This is for preparing to put the page under writeout. We leave the page
  664. * tagged as dirty in the radix tree so that a concurrent write-for-sync
  665. * can discover it via a PAGECACHE_TAG_DIRTY walk. The ->writepage
  666. * implementation will run either set_page_writeback() or set_page_dirty(),
  667. * at which stage we bring the page's dirty flag and radix-tree dirty tag
  668. * back into sync.
  669. *
  670. * This incoherency between the page's dirty flag and radix-tree tag is
  671. * unfortunate, but it only exists while the page is locked.
  672. */
  673. int clear_page_dirty_for_io(struct page *page)
  674. {
  675. struct address_space *mapping = page_mapping(page);
  676. if (mapping) {
  677. if (TestClearPageDirty(page)) {
  678. if (mapping_cap_account_dirty(mapping)) {
  679. page_mkclean(page);
  680. dec_zone_page_state(page, NR_FILE_DIRTY);
  681. }
  682. return 1;
  683. }
  684. return 0;
  685. }
  686. return TestClearPageDirty(page);
  687. }
  688. EXPORT_SYMBOL(clear_page_dirty_for_io);
  689. int test_clear_page_writeback(struct page *page)
  690. {
  691. struct address_space *mapping = page_mapping(page);
  692. int ret;
  693. if (mapping) {
  694. unsigned long flags;
  695. write_lock_irqsave(&mapping->tree_lock, flags);
  696. ret = TestClearPageWriteback(page);
  697. if (ret)
  698. radix_tree_tag_clear(&mapping->page_tree,
  699. page_index(page),
  700. PAGECACHE_TAG_WRITEBACK);
  701. write_unlock_irqrestore(&mapping->tree_lock, flags);
  702. } else {
  703. ret = TestClearPageWriteback(page);
  704. }
  705. return ret;
  706. }
  707. int test_set_page_writeback(struct page *page)
  708. {
  709. struct address_space *mapping = page_mapping(page);
  710. int ret;
  711. if (mapping) {
  712. unsigned long flags;
  713. write_lock_irqsave(&mapping->tree_lock, flags);
  714. ret = TestSetPageWriteback(page);
  715. if (!ret)
  716. radix_tree_tag_set(&mapping->page_tree,
  717. page_index(page),
  718. PAGECACHE_TAG_WRITEBACK);
  719. if (!PageDirty(page))
  720. radix_tree_tag_clear(&mapping->page_tree,
  721. page_index(page),
  722. PAGECACHE_TAG_DIRTY);
  723. write_unlock_irqrestore(&mapping->tree_lock, flags);
  724. } else {
  725. ret = TestSetPageWriteback(page);
  726. }
  727. return ret;
  728. }
  729. EXPORT_SYMBOL(test_set_page_writeback);
  730. /*
  731. * Wakes up tasks that are being throttled due to writeback congestion
  732. */
  733. void writeback_congestion_end(void)
  734. {
  735. blk_congestion_end(WRITE);
  736. }
  737. EXPORT_SYMBOL(writeback_congestion_end);
  738. /*
  739. * Return true if any of the pages in the mapping are marged with the
  740. * passed tag.
  741. */
  742. int mapping_tagged(struct address_space *mapping, int tag)
  743. {
  744. unsigned long flags;
  745. int ret;
  746. read_lock_irqsave(&mapping->tree_lock, flags);
  747. ret = radix_tree_tagged(&mapping->page_tree, tag);
  748. read_unlock_irqrestore(&mapping->tree_lock, flags);
  749. return ret;
  750. }
  751. EXPORT_SYMBOL(mapping_tagged);