page-writeback.c 27 KB

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