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