page-writeback.c 27 KB

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