page-writeback.c 23 KB

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