fs-writeback.c 33 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274
  1. /*
  2. * fs/fs-writeback.c
  3. *
  4. * Copyright (C) 2002, Linus Torvalds.
  5. *
  6. * Contains all the functions related to writing back and waiting
  7. * upon dirty inodes against superblocks, and writing back dirty
  8. * pages against inodes. ie: data writeback. Writeout of the
  9. * inode itself is not handled here.
  10. *
  11. * 10Apr2002 Andrew Morton
  12. * Split out of fs/inode.c
  13. * Additions for address_space-based writeback
  14. */
  15. #include <linux/kernel.h>
  16. #include <linux/module.h>
  17. #include <linux/spinlock.h>
  18. #include <linux/slab.h>
  19. #include <linux/sched.h>
  20. #include <linux/fs.h>
  21. #include <linux/mm.h>
  22. #include <linux/kthread.h>
  23. #include <linux/freezer.h>
  24. #include <linux/writeback.h>
  25. #include <linux/blkdev.h>
  26. #include <linux/backing-dev.h>
  27. #include <linux/buffer_head.h>
  28. #include "internal.h"
  29. #define inode_to_bdi(inode) ((inode)->i_mapping->backing_dev_info)
  30. /*
  31. * We don't actually have pdflush, but this one is exported though /proc...
  32. */
  33. int nr_pdflush_threads;
  34. /*
  35. * Passed into wb_writeback(), essentially a subset of writeback_control
  36. */
  37. struct wb_writeback_args {
  38. long nr_pages;
  39. struct super_block *sb;
  40. enum writeback_sync_modes sync_mode;
  41. unsigned int for_kupdate:1;
  42. unsigned int range_cyclic:1;
  43. unsigned int for_background:1;
  44. };
  45. /*
  46. * Work items for the bdi_writeback threads
  47. */
  48. struct bdi_work {
  49. struct list_head list; /* pending work list */
  50. struct rcu_head rcu_head; /* for RCU free/clear of work */
  51. unsigned long seen; /* threads that have seen this work */
  52. atomic_t pending; /* number of threads still to do work */
  53. struct wb_writeback_args args; /* writeback arguments */
  54. unsigned long state; /* flag bits, see WS_* */
  55. };
  56. enum {
  57. WS_INPROGRESS = 0,
  58. WS_ONSTACK,
  59. };
  60. static inline void bdi_work_init(struct bdi_work *work,
  61. struct wb_writeback_args *args)
  62. {
  63. INIT_RCU_HEAD(&work->rcu_head);
  64. work->args = *args;
  65. __set_bit(WS_INPROGRESS, &work->state);
  66. }
  67. /**
  68. * writeback_in_progress - determine whether there is writeback in progress
  69. * @bdi: the device's backing_dev_info structure.
  70. *
  71. * Determine whether there is writeback waiting to be handled against a
  72. * backing device.
  73. */
  74. int writeback_in_progress(struct backing_dev_info *bdi)
  75. {
  76. return !list_empty(&bdi->work_list);
  77. }
  78. static void bdi_work_free(struct rcu_head *head)
  79. {
  80. struct bdi_work *work = container_of(head, struct bdi_work, rcu_head);
  81. clear_bit(WS_INPROGRESS, &work->state);
  82. smp_mb__after_clear_bit();
  83. wake_up_bit(&work->state, WS_INPROGRESS);
  84. if (!test_bit(WS_ONSTACK, &work->state))
  85. kfree(work);
  86. }
  87. static void wb_clear_pending(struct bdi_writeback *wb, struct bdi_work *work)
  88. {
  89. /*
  90. * The caller has retrieved the work arguments from this work,
  91. * drop our reference. If this is the last ref, delete and free it
  92. */
  93. if (atomic_dec_and_test(&work->pending)) {
  94. struct backing_dev_info *bdi = wb->bdi;
  95. spin_lock(&bdi->wb_lock);
  96. list_del_rcu(&work->list);
  97. spin_unlock(&bdi->wb_lock);
  98. call_rcu(&work->rcu_head, bdi_work_free);
  99. }
  100. }
  101. static void bdi_queue_work(struct backing_dev_info *bdi, struct bdi_work *work)
  102. {
  103. work->seen = bdi->wb_mask;
  104. BUG_ON(!work->seen);
  105. atomic_set(&work->pending, bdi->wb_cnt);
  106. BUG_ON(!bdi->wb_cnt);
  107. /*
  108. * list_add_tail_rcu() contains the necessary barriers to
  109. * make sure the above stores are seen before the item is
  110. * noticed on the list
  111. */
  112. spin_lock(&bdi->wb_lock);
  113. list_add_tail_rcu(&work->list, &bdi->work_list);
  114. spin_unlock(&bdi->wb_lock);
  115. /*
  116. * If the default thread isn't there, make sure we add it. When
  117. * it gets created and wakes up, we'll run this work.
  118. */
  119. if (unlikely(list_empty_careful(&bdi->wb_list)))
  120. wake_up_process(default_backing_dev_info.wb.task);
  121. else {
  122. struct bdi_writeback *wb = &bdi->wb;
  123. if (wb->task)
  124. wake_up_process(wb->task);
  125. }
  126. }
  127. /*
  128. * Used for on-stack allocated work items. The caller needs to wait until
  129. * the wb threads have acked the work before it's safe to continue.
  130. */
  131. static void bdi_wait_on_work_done(struct bdi_work *work)
  132. {
  133. wait_on_bit(&work->state, WS_INPROGRESS, bdi_sched_wait,
  134. TASK_UNINTERRUPTIBLE);
  135. }
  136. static void bdi_alloc_queue_work(struct backing_dev_info *bdi,
  137. struct wb_writeback_args *args)
  138. {
  139. struct bdi_work *work;
  140. /*
  141. * This is WB_SYNC_NONE writeback, so if allocation fails just
  142. * wakeup the thread for old dirty data writeback
  143. */
  144. work = kmalloc(sizeof(*work), GFP_ATOMIC);
  145. if (work) {
  146. bdi_work_init(work, args);
  147. bdi_queue_work(bdi, work);
  148. } else {
  149. struct bdi_writeback *wb = &bdi->wb;
  150. if (wb->task)
  151. wake_up_process(wb->task);
  152. }
  153. }
  154. /**
  155. * bdi_queue_work_onstack - start and wait for writeback
  156. * @sb: write inodes from this super_block
  157. *
  158. * Description:
  159. * This function initiates writeback and waits for the operation to
  160. * complete. Callers must hold the sb s_umount semaphore for
  161. * reading, to avoid having the super disappear before we are done.
  162. */
  163. static void bdi_queue_work_onstack(struct wb_writeback_args *args)
  164. {
  165. struct bdi_work work;
  166. bdi_work_init(&work, args);
  167. __set_bit(WS_ONSTACK, &work.state);
  168. bdi_queue_work(args->sb->s_bdi, &work);
  169. bdi_wait_on_work_done(&work);
  170. }
  171. /**
  172. * bdi_start_writeback - start writeback
  173. * @bdi: the backing device to write from
  174. * @nr_pages: the number of pages to write
  175. *
  176. * Description:
  177. * This does WB_SYNC_NONE opportunistic writeback. The IO is only
  178. * started when this function returns, we make no guarentees on
  179. * completion. Caller need not hold sb s_umount semaphore.
  180. *
  181. */
  182. void bdi_start_writeback(struct backing_dev_info *bdi, long nr_pages)
  183. {
  184. struct wb_writeback_args args = {
  185. .sync_mode = WB_SYNC_NONE,
  186. .nr_pages = nr_pages,
  187. .range_cyclic = 1,
  188. };
  189. bdi_alloc_queue_work(bdi, &args);
  190. }
  191. /**
  192. * bdi_start_background_writeback - start background writeback
  193. * @bdi: the backing device to write from
  194. *
  195. * Description:
  196. * This does WB_SYNC_NONE background writeback. The IO is only
  197. * started when this function returns, we make no guarentees on
  198. * completion. Caller need not hold sb s_umount semaphore.
  199. */
  200. void bdi_start_background_writeback(struct backing_dev_info *bdi)
  201. {
  202. struct wb_writeback_args args = {
  203. .sync_mode = WB_SYNC_NONE,
  204. .nr_pages = LONG_MAX,
  205. .for_background = 1,
  206. .range_cyclic = 1,
  207. };
  208. bdi_alloc_queue_work(bdi, &args);
  209. }
  210. /*
  211. * Redirty an inode: set its when-it-was dirtied timestamp and move it to the
  212. * furthest end of its superblock's dirty-inode list.
  213. *
  214. * Before stamping the inode's ->dirtied_when, we check to see whether it is
  215. * already the most-recently-dirtied inode on the b_dirty list. If that is
  216. * the case then the inode must have been redirtied while it was being written
  217. * out and we don't reset its dirtied_when.
  218. */
  219. static void redirty_tail(struct inode *inode)
  220. {
  221. struct bdi_writeback *wb = &inode_to_bdi(inode)->wb;
  222. if (!list_empty(&wb->b_dirty)) {
  223. struct inode *tail;
  224. tail = list_entry(wb->b_dirty.next, struct inode, i_list);
  225. if (time_before(inode->dirtied_when, tail->dirtied_when))
  226. inode->dirtied_when = jiffies;
  227. }
  228. list_move(&inode->i_list, &wb->b_dirty);
  229. }
  230. /*
  231. * requeue inode for re-scanning after bdi->b_io list is exhausted.
  232. */
  233. static void requeue_io(struct inode *inode)
  234. {
  235. struct bdi_writeback *wb = &inode_to_bdi(inode)->wb;
  236. list_move(&inode->i_list, &wb->b_more_io);
  237. }
  238. static void inode_sync_complete(struct inode *inode)
  239. {
  240. /*
  241. * Prevent speculative execution through spin_unlock(&inode_lock);
  242. */
  243. smp_mb();
  244. wake_up_bit(&inode->i_state, __I_SYNC);
  245. }
  246. static bool inode_dirtied_after(struct inode *inode, unsigned long t)
  247. {
  248. bool ret = time_after(inode->dirtied_when, t);
  249. #ifndef CONFIG_64BIT
  250. /*
  251. * For inodes being constantly redirtied, dirtied_when can get stuck.
  252. * It _appears_ to be in the future, but is actually in distant past.
  253. * This test is necessary to prevent such wrapped-around relative times
  254. * from permanently stopping the whole bdi writeback.
  255. */
  256. ret = ret && time_before_eq(inode->dirtied_when, jiffies);
  257. #endif
  258. return ret;
  259. }
  260. /*
  261. * Move expired dirty inodes from @delaying_queue to @dispatch_queue.
  262. */
  263. static void move_expired_inodes(struct list_head *delaying_queue,
  264. struct list_head *dispatch_queue,
  265. unsigned long *older_than_this)
  266. {
  267. LIST_HEAD(tmp);
  268. struct list_head *pos, *node;
  269. struct super_block *sb = NULL;
  270. struct inode *inode;
  271. int do_sb_sort = 0;
  272. while (!list_empty(delaying_queue)) {
  273. inode = list_entry(delaying_queue->prev, struct inode, i_list);
  274. if (older_than_this &&
  275. inode_dirtied_after(inode, *older_than_this))
  276. break;
  277. if (sb && sb != inode->i_sb)
  278. do_sb_sort = 1;
  279. sb = inode->i_sb;
  280. list_move(&inode->i_list, &tmp);
  281. }
  282. /* just one sb in list, splice to dispatch_queue and we're done */
  283. if (!do_sb_sort) {
  284. list_splice(&tmp, dispatch_queue);
  285. return;
  286. }
  287. /* Move inodes from one superblock together */
  288. while (!list_empty(&tmp)) {
  289. inode = list_entry(tmp.prev, struct inode, i_list);
  290. sb = inode->i_sb;
  291. list_for_each_prev_safe(pos, node, &tmp) {
  292. inode = list_entry(pos, struct inode, i_list);
  293. if (inode->i_sb == sb)
  294. list_move(&inode->i_list, dispatch_queue);
  295. }
  296. }
  297. }
  298. /*
  299. * Queue all expired dirty inodes for io, eldest first.
  300. */
  301. static void queue_io(struct bdi_writeback *wb, unsigned long *older_than_this)
  302. {
  303. list_splice_init(&wb->b_more_io, wb->b_io.prev);
  304. move_expired_inodes(&wb->b_dirty, &wb->b_io, older_than_this);
  305. }
  306. static int write_inode(struct inode *inode, struct writeback_control *wbc)
  307. {
  308. if (inode->i_sb->s_op->write_inode && !is_bad_inode(inode))
  309. return inode->i_sb->s_op->write_inode(inode, wbc);
  310. return 0;
  311. }
  312. /*
  313. * Wait for writeback on an inode to complete.
  314. */
  315. static void inode_wait_for_writeback(struct inode *inode)
  316. {
  317. DEFINE_WAIT_BIT(wq, &inode->i_state, __I_SYNC);
  318. wait_queue_head_t *wqh;
  319. wqh = bit_waitqueue(&inode->i_state, __I_SYNC);
  320. while (inode->i_state & I_SYNC) {
  321. spin_unlock(&inode_lock);
  322. __wait_on_bit(wqh, &wq, inode_wait, TASK_UNINTERRUPTIBLE);
  323. spin_lock(&inode_lock);
  324. }
  325. }
  326. /*
  327. * Write out an inode's dirty pages. Called under inode_lock. Either the
  328. * caller has ref on the inode (either via __iget or via syscall against an fd)
  329. * or the inode has I_WILL_FREE set (via generic_forget_inode)
  330. *
  331. * If `wait' is set, wait on the writeout.
  332. *
  333. * The whole writeout design is quite complex and fragile. We want to avoid
  334. * starvation of particular inodes when others are being redirtied, prevent
  335. * livelocks, etc.
  336. *
  337. * Called under inode_lock.
  338. */
  339. static int
  340. writeback_single_inode(struct inode *inode, struct writeback_control *wbc)
  341. {
  342. struct address_space *mapping = inode->i_mapping;
  343. unsigned dirty;
  344. int ret;
  345. if (!atomic_read(&inode->i_count))
  346. WARN_ON(!(inode->i_state & (I_WILL_FREE|I_FREEING)));
  347. else
  348. WARN_ON(inode->i_state & I_WILL_FREE);
  349. if (inode->i_state & I_SYNC) {
  350. /*
  351. * If this inode is locked for writeback and we are not doing
  352. * writeback-for-data-integrity, move it to b_more_io so that
  353. * writeback can proceed with the other inodes on s_io.
  354. *
  355. * We'll have another go at writing back this inode when we
  356. * completed a full scan of b_io.
  357. */
  358. if (wbc->sync_mode != WB_SYNC_ALL) {
  359. requeue_io(inode);
  360. return 0;
  361. }
  362. /*
  363. * It's a data-integrity sync. We must wait.
  364. */
  365. inode_wait_for_writeback(inode);
  366. }
  367. BUG_ON(inode->i_state & I_SYNC);
  368. /* Set I_SYNC, reset I_DIRTY_PAGES */
  369. inode->i_state |= I_SYNC;
  370. inode->i_state &= ~I_DIRTY_PAGES;
  371. spin_unlock(&inode_lock);
  372. ret = do_writepages(mapping, wbc);
  373. /*
  374. * Make sure to wait on the data before writing out the metadata.
  375. * This is important for filesystems that modify metadata on data
  376. * I/O completion.
  377. */
  378. if (wbc->sync_mode == WB_SYNC_ALL) {
  379. int err = filemap_fdatawait(mapping);
  380. if (ret == 0)
  381. ret = err;
  382. }
  383. /*
  384. * Some filesystems may redirty the inode during the writeback
  385. * due to delalloc, clear dirty metadata flags right before
  386. * write_inode()
  387. */
  388. spin_lock(&inode_lock);
  389. dirty = inode->i_state & I_DIRTY;
  390. inode->i_state &= ~(I_DIRTY_SYNC | I_DIRTY_DATASYNC);
  391. spin_unlock(&inode_lock);
  392. /* Don't write the inode if only I_DIRTY_PAGES was set */
  393. if (dirty & (I_DIRTY_SYNC | I_DIRTY_DATASYNC)) {
  394. int err = write_inode(inode, wbc);
  395. if (ret == 0)
  396. ret = err;
  397. }
  398. spin_lock(&inode_lock);
  399. inode->i_state &= ~I_SYNC;
  400. if (!(inode->i_state & (I_FREEING | I_CLEAR))) {
  401. if ((inode->i_state & I_DIRTY_PAGES) && wbc->for_kupdate) {
  402. /*
  403. * More pages get dirtied by a fast dirtier.
  404. */
  405. goto select_queue;
  406. } else if (inode->i_state & I_DIRTY) {
  407. /*
  408. * At least XFS will redirty the inode during the
  409. * writeback (delalloc) and on io completion (isize).
  410. */
  411. redirty_tail(inode);
  412. } else if (mapping_tagged(mapping, PAGECACHE_TAG_DIRTY)) {
  413. /*
  414. * We didn't write back all the pages. nfs_writepages()
  415. * sometimes bales out without doing anything. Redirty
  416. * the inode; Move it from b_io onto b_more_io/b_dirty.
  417. */
  418. /*
  419. * akpm: if the caller was the kupdate function we put
  420. * this inode at the head of b_dirty so it gets first
  421. * consideration. Otherwise, move it to the tail, for
  422. * the reasons described there. I'm not really sure
  423. * how much sense this makes. Presumably I had a good
  424. * reasons for doing it this way, and I'd rather not
  425. * muck with it at present.
  426. */
  427. if (wbc->for_kupdate) {
  428. /*
  429. * For the kupdate function we move the inode
  430. * to b_more_io so it will get more writeout as
  431. * soon as the queue becomes uncongested.
  432. */
  433. inode->i_state |= I_DIRTY_PAGES;
  434. select_queue:
  435. if (wbc->nr_to_write <= 0) {
  436. /*
  437. * slice used up: queue for next turn
  438. */
  439. requeue_io(inode);
  440. } else {
  441. /*
  442. * somehow blocked: retry later
  443. */
  444. redirty_tail(inode);
  445. }
  446. } else {
  447. /*
  448. * Otherwise fully redirty the inode so that
  449. * other inodes on this superblock will get some
  450. * writeout. Otherwise heavy writing to one
  451. * file would indefinitely suspend writeout of
  452. * all the other files.
  453. */
  454. inode->i_state |= I_DIRTY_PAGES;
  455. redirty_tail(inode);
  456. }
  457. } else if (atomic_read(&inode->i_count)) {
  458. /*
  459. * The inode is clean, inuse
  460. */
  461. list_move(&inode->i_list, &inode_in_use);
  462. } else {
  463. /*
  464. * The inode is clean, unused
  465. */
  466. list_move(&inode->i_list, &inode_unused);
  467. }
  468. }
  469. inode_sync_complete(inode);
  470. return ret;
  471. }
  472. /*
  473. * For background writeback the caller does not have the sb pinned
  474. * before calling writeback. So make sure that we do pin it, so it doesn't
  475. * go away while we are writing inodes from it.
  476. */
  477. static bool pin_sb_for_writeback(struct super_block *sb)
  478. {
  479. spin_lock(&sb_lock);
  480. if (list_empty(&sb->s_instances)) {
  481. spin_unlock(&sb_lock);
  482. return false;
  483. }
  484. sb->s_count++;
  485. spin_unlock(&sb_lock);
  486. if (down_read_trylock(&sb->s_umount)) {
  487. if (sb->s_root)
  488. return true;
  489. up_read(&sb->s_umount);
  490. }
  491. put_super(sb);
  492. return false;
  493. }
  494. /*
  495. * Write a portion of b_io inodes which belong to @sb.
  496. * If @wbc->sb != NULL, then find and write all such
  497. * inodes. Otherwise write only ones which go sequentially
  498. * in reverse order.
  499. * Return 1, if the caller writeback routine should be
  500. * interrupted. Otherwise return 0.
  501. */
  502. static int writeback_sb_inodes(struct super_block *sb,
  503. struct bdi_writeback *wb,
  504. struct writeback_control *wbc)
  505. {
  506. while (!list_empty(&wb->b_io)) {
  507. long pages_skipped;
  508. struct inode *inode = list_entry(wb->b_io.prev,
  509. struct inode, i_list);
  510. if (wbc->sb && sb != inode->i_sb) {
  511. /* super block given and doesn't
  512. match, skip this inode */
  513. redirty_tail(inode);
  514. continue;
  515. }
  516. if (sb != inode->i_sb)
  517. /* finish with this superblock */
  518. return 0;
  519. if (inode->i_state & (I_NEW | I_WILL_FREE)) {
  520. requeue_io(inode);
  521. continue;
  522. }
  523. /*
  524. * Was this inode dirtied after sync_sb_inodes was called?
  525. * This keeps sync from extra jobs and livelock.
  526. */
  527. if (inode_dirtied_after(inode, wbc->wb_start))
  528. return 1;
  529. BUG_ON(inode->i_state & (I_FREEING | I_CLEAR));
  530. __iget(inode);
  531. pages_skipped = wbc->pages_skipped;
  532. writeback_single_inode(inode, wbc);
  533. if (wbc->pages_skipped != pages_skipped) {
  534. /*
  535. * writeback is not making progress due to locked
  536. * buffers. Skip this inode for now.
  537. */
  538. redirty_tail(inode);
  539. }
  540. spin_unlock(&inode_lock);
  541. iput(inode);
  542. cond_resched();
  543. spin_lock(&inode_lock);
  544. if (wbc->nr_to_write <= 0) {
  545. wbc->more_io = 1;
  546. return 1;
  547. }
  548. if (!list_empty(&wb->b_more_io))
  549. wbc->more_io = 1;
  550. }
  551. /* b_io is empty */
  552. return 1;
  553. }
  554. static void writeback_inodes_wb(struct bdi_writeback *wb,
  555. struct writeback_control *wbc)
  556. {
  557. int ret = 0;
  558. wbc->wb_start = jiffies; /* livelock avoidance */
  559. spin_lock(&inode_lock);
  560. if (!wbc->for_kupdate || list_empty(&wb->b_io))
  561. queue_io(wb, wbc->older_than_this);
  562. while (!list_empty(&wb->b_io)) {
  563. struct inode *inode = list_entry(wb->b_io.prev,
  564. struct inode, i_list);
  565. struct super_block *sb = inode->i_sb;
  566. if (wbc->sb) {
  567. /*
  568. * We are requested to write out inodes for a specific
  569. * superblock. This means we already have s_umount
  570. * taken by the caller which also waits for us to
  571. * complete the writeout.
  572. */
  573. if (sb != wbc->sb) {
  574. redirty_tail(inode);
  575. continue;
  576. }
  577. WARN_ON(!rwsem_is_locked(&sb->s_umount));
  578. ret = writeback_sb_inodes(sb, wb, wbc);
  579. } else {
  580. if (!pin_sb_for_writeback(sb)) {
  581. requeue_io(inode);
  582. continue;
  583. }
  584. ret = writeback_sb_inodes(sb, wb, wbc);
  585. drop_super(sb);
  586. }
  587. if (ret)
  588. break;
  589. }
  590. spin_unlock(&inode_lock);
  591. /* Leave any unwritten inodes on b_io */
  592. }
  593. void writeback_inodes_wbc(struct writeback_control *wbc)
  594. {
  595. struct backing_dev_info *bdi = wbc->bdi;
  596. writeback_inodes_wb(&bdi->wb, wbc);
  597. }
  598. /*
  599. * The maximum number of pages to writeout in a single bdi flush/kupdate
  600. * operation. We do this so we don't hold I_SYNC against an inode for
  601. * enormous amounts of time, which would block a userspace task which has
  602. * been forced to throttle against that inode. Also, the code reevaluates
  603. * the dirty each time it has written this many pages.
  604. */
  605. #define MAX_WRITEBACK_PAGES 1024
  606. static inline bool over_bground_thresh(void)
  607. {
  608. unsigned long background_thresh, dirty_thresh;
  609. get_dirty_limits(&background_thresh, &dirty_thresh, NULL, NULL);
  610. return (global_page_state(NR_FILE_DIRTY) +
  611. global_page_state(NR_UNSTABLE_NFS) >= background_thresh);
  612. }
  613. /*
  614. * Explicit flushing or periodic writeback of "old" data.
  615. *
  616. * Define "old": the first time one of an inode's pages is dirtied, we mark the
  617. * dirtying-time in the inode's address_space. So this periodic writeback code
  618. * just walks the superblock inode list, writing back any inodes which are
  619. * older than a specific point in time.
  620. *
  621. * Try to run once per dirty_writeback_interval. But if a writeback event
  622. * takes longer than a dirty_writeback_interval interval, then leave a
  623. * one-second gap.
  624. *
  625. * older_than_this takes precedence over nr_to_write. So we'll only write back
  626. * all dirty pages if they are all attached to "old" mappings.
  627. */
  628. static long wb_writeback(struct bdi_writeback *wb,
  629. struct wb_writeback_args *args)
  630. {
  631. struct writeback_control wbc = {
  632. .bdi = wb->bdi,
  633. .sb = args->sb,
  634. .sync_mode = args->sync_mode,
  635. .older_than_this = NULL,
  636. .for_kupdate = args->for_kupdate,
  637. .for_background = args->for_background,
  638. .range_cyclic = args->range_cyclic,
  639. };
  640. unsigned long oldest_jif;
  641. long wrote = 0;
  642. struct inode *inode;
  643. if (wbc.for_kupdate) {
  644. wbc.older_than_this = &oldest_jif;
  645. oldest_jif = jiffies -
  646. msecs_to_jiffies(dirty_expire_interval * 10);
  647. }
  648. if (!wbc.range_cyclic) {
  649. wbc.range_start = 0;
  650. wbc.range_end = LLONG_MAX;
  651. }
  652. for (;;) {
  653. /*
  654. * Stop writeback when nr_pages has been consumed
  655. */
  656. if (args->nr_pages <= 0)
  657. break;
  658. /*
  659. * For background writeout, stop when we are below the
  660. * background dirty threshold
  661. */
  662. if (args->for_background && !over_bground_thresh())
  663. break;
  664. wbc.more_io = 0;
  665. wbc.nr_to_write = MAX_WRITEBACK_PAGES;
  666. wbc.pages_skipped = 0;
  667. writeback_inodes_wb(wb, &wbc);
  668. args->nr_pages -= MAX_WRITEBACK_PAGES - wbc.nr_to_write;
  669. wrote += MAX_WRITEBACK_PAGES - wbc.nr_to_write;
  670. /*
  671. * If we consumed everything, see if we have more
  672. */
  673. if (wbc.nr_to_write <= 0)
  674. continue;
  675. /*
  676. * Didn't write everything and we don't have more IO, bail
  677. */
  678. if (!wbc.more_io)
  679. break;
  680. /*
  681. * Did we write something? Try for more
  682. */
  683. if (wbc.nr_to_write < MAX_WRITEBACK_PAGES)
  684. continue;
  685. /*
  686. * Nothing written. Wait for some inode to
  687. * become available for writeback. Otherwise
  688. * we'll just busyloop.
  689. */
  690. spin_lock(&inode_lock);
  691. if (!list_empty(&wb->b_more_io)) {
  692. inode = list_entry(wb->b_more_io.prev,
  693. struct inode, i_list);
  694. inode_wait_for_writeback(inode);
  695. }
  696. spin_unlock(&inode_lock);
  697. }
  698. return wrote;
  699. }
  700. /*
  701. * Return the next bdi_work struct that hasn't been processed by this
  702. * wb thread yet. ->seen is initially set for each thread that exists
  703. * for this device, when a thread first notices a piece of work it
  704. * clears its bit. Depending on writeback type, the thread will notify
  705. * completion on either receiving the work (WB_SYNC_NONE) or after
  706. * it is done (WB_SYNC_ALL).
  707. */
  708. static struct bdi_work *get_next_work_item(struct backing_dev_info *bdi,
  709. struct bdi_writeback *wb)
  710. {
  711. struct bdi_work *work, *ret = NULL;
  712. rcu_read_lock();
  713. list_for_each_entry_rcu(work, &bdi->work_list, list) {
  714. if (!test_bit(wb->nr, &work->seen))
  715. continue;
  716. clear_bit(wb->nr, &work->seen);
  717. ret = work;
  718. break;
  719. }
  720. rcu_read_unlock();
  721. return ret;
  722. }
  723. static long wb_check_old_data_flush(struct bdi_writeback *wb)
  724. {
  725. unsigned long expired;
  726. long nr_pages;
  727. /*
  728. * When set to zero, disable periodic writeback
  729. */
  730. if (!dirty_writeback_interval)
  731. return 0;
  732. expired = wb->last_old_flush +
  733. msecs_to_jiffies(dirty_writeback_interval * 10);
  734. if (time_before(jiffies, expired))
  735. return 0;
  736. wb->last_old_flush = jiffies;
  737. nr_pages = global_page_state(NR_FILE_DIRTY) +
  738. global_page_state(NR_UNSTABLE_NFS) +
  739. (inodes_stat.nr_inodes - inodes_stat.nr_unused);
  740. if (nr_pages) {
  741. struct wb_writeback_args args = {
  742. .nr_pages = nr_pages,
  743. .sync_mode = WB_SYNC_NONE,
  744. .for_kupdate = 1,
  745. .range_cyclic = 1,
  746. };
  747. return wb_writeback(wb, &args);
  748. }
  749. return 0;
  750. }
  751. /*
  752. * Retrieve work items and do the writeback they describe
  753. */
  754. long wb_do_writeback(struct bdi_writeback *wb, int force_wait)
  755. {
  756. struct backing_dev_info *bdi = wb->bdi;
  757. struct bdi_work *work;
  758. long wrote = 0;
  759. while ((work = get_next_work_item(bdi, wb)) != NULL) {
  760. struct wb_writeback_args args = work->args;
  761. /*
  762. * Override sync mode, in case we must wait for completion
  763. */
  764. if (force_wait)
  765. work->args.sync_mode = args.sync_mode = WB_SYNC_ALL;
  766. /*
  767. * If this isn't a data integrity operation, just notify
  768. * that we have seen this work and we are now starting it.
  769. */
  770. if (!test_bit(WS_ONSTACK, &work->state))
  771. wb_clear_pending(wb, work);
  772. wrote += wb_writeback(wb, &args);
  773. /*
  774. * This is a data integrity writeback, so only do the
  775. * notification when we have completed the work.
  776. */
  777. if (test_bit(WS_ONSTACK, &work->state))
  778. wb_clear_pending(wb, work);
  779. }
  780. /*
  781. * Check for periodic writeback, kupdated() style
  782. */
  783. wrote += wb_check_old_data_flush(wb);
  784. return wrote;
  785. }
  786. /*
  787. * Handle writeback of dirty data for the device backed by this bdi. Also
  788. * wakes up periodically and does kupdated style flushing.
  789. */
  790. int bdi_writeback_task(struct bdi_writeback *wb)
  791. {
  792. unsigned long last_active = jiffies;
  793. unsigned long wait_jiffies = -1UL;
  794. long pages_written;
  795. while (!kthread_should_stop()) {
  796. pages_written = wb_do_writeback(wb, 0);
  797. if (pages_written)
  798. last_active = jiffies;
  799. else if (wait_jiffies != -1UL) {
  800. unsigned long max_idle;
  801. /*
  802. * Longest period of inactivity that we tolerate. If we
  803. * see dirty data again later, the task will get
  804. * recreated automatically.
  805. */
  806. max_idle = max(5UL * 60 * HZ, wait_jiffies);
  807. if (time_after(jiffies, max_idle + last_active))
  808. break;
  809. }
  810. if (dirty_writeback_interval) {
  811. wait_jiffies = msecs_to_jiffies(dirty_writeback_interval * 10);
  812. schedule_timeout_interruptible(wait_jiffies);
  813. } else {
  814. set_current_state(TASK_INTERRUPTIBLE);
  815. if (list_empty_careful(&wb->bdi->work_list) &&
  816. !kthread_should_stop())
  817. schedule();
  818. __set_current_state(TASK_RUNNING);
  819. }
  820. try_to_freeze();
  821. }
  822. return 0;
  823. }
  824. /*
  825. * Start writeback of `nr_pages' pages. If `nr_pages' is zero, write back
  826. * the whole world.
  827. */
  828. void wakeup_flusher_threads(long nr_pages)
  829. {
  830. struct backing_dev_info *bdi;
  831. struct wb_writeback_args args = {
  832. .sync_mode = WB_SYNC_NONE,
  833. };
  834. if (nr_pages) {
  835. args.nr_pages = nr_pages;
  836. } else {
  837. args.nr_pages = global_page_state(NR_FILE_DIRTY) +
  838. global_page_state(NR_UNSTABLE_NFS);
  839. }
  840. rcu_read_lock();
  841. list_for_each_entry_rcu(bdi, &bdi_list, bdi_list) {
  842. if (!bdi_has_dirty_io(bdi))
  843. continue;
  844. bdi_alloc_queue_work(bdi, &args);
  845. }
  846. rcu_read_unlock();
  847. }
  848. static noinline void block_dump___mark_inode_dirty(struct inode *inode)
  849. {
  850. if (inode->i_ino || strcmp(inode->i_sb->s_id, "bdev")) {
  851. struct dentry *dentry;
  852. const char *name = "?";
  853. dentry = d_find_alias(inode);
  854. if (dentry) {
  855. spin_lock(&dentry->d_lock);
  856. name = (const char *) dentry->d_name.name;
  857. }
  858. printk(KERN_DEBUG
  859. "%s(%d): dirtied inode %lu (%s) on %s\n",
  860. current->comm, task_pid_nr(current), inode->i_ino,
  861. name, inode->i_sb->s_id);
  862. if (dentry) {
  863. spin_unlock(&dentry->d_lock);
  864. dput(dentry);
  865. }
  866. }
  867. }
  868. /**
  869. * __mark_inode_dirty - internal function
  870. * @inode: inode to mark
  871. * @flags: what kind of dirty (i.e. I_DIRTY_SYNC)
  872. * Mark an inode as dirty. Callers should use mark_inode_dirty or
  873. * mark_inode_dirty_sync.
  874. *
  875. * Put the inode on the super block's dirty list.
  876. *
  877. * CAREFUL! We mark it dirty unconditionally, but move it onto the
  878. * dirty list only if it is hashed or if it refers to a blockdev.
  879. * If it was not hashed, it will never be added to the dirty list
  880. * even if it is later hashed, as it will have been marked dirty already.
  881. *
  882. * In short, make sure you hash any inodes _before_ you start marking
  883. * them dirty.
  884. *
  885. * This function *must* be atomic for the I_DIRTY_PAGES case -
  886. * set_page_dirty() is called under spinlock in several places.
  887. *
  888. * Note that for blockdevs, inode->dirtied_when represents the dirtying time of
  889. * the block-special inode (/dev/hda1) itself. And the ->dirtied_when field of
  890. * the kernel-internal blockdev inode represents the dirtying time of the
  891. * blockdev's pages. This is why for I_DIRTY_PAGES we always use
  892. * page->mapping->host, so the page-dirtying time is recorded in the internal
  893. * blockdev inode.
  894. */
  895. void __mark_inode_dirty(struct inode *inode, int flags)
  896. {
  897. struct super_block *sb = inode->i_sb;
  898. /*
  899. * Don't do this for I_DIRTY_PAGES - that doesn't actually
  900. * dirty the inode itself
  901. */
  902. if (flags & (I_DIRTY_SYNC | I_DIRTY_DATASYNC)) {
  903. if (sb->s_op->dirty_inode)
  904. sb->s_op->dirty_inode(inode);
  905. }
  906. /*
  907. * make sure that changes are seen by all cpus before we test i_state
  908. * -- mikulas
  909. */
  910. smp_mb();
  911. /* avoid the locking if we can */
  912. if ((inode->i_state & flags) == flags)
  913. return;
  914. if (unlikely(block_dump))
  915. block_dump___mark_inode_dirty(inode);
  916. spin_lock(&inode_lock);
  917. if ((inode->i_state & flags) != flags) {
  918. const int was_dirty = inode->i_state & I_DIRTY;
  919. inode->i_state |= flags;
  920. /*
  921. * If the inode is being synced, just update its dirty state.
  922. * The unlocker will place the inode on the appropriate
  923. * superblock list, based upon its state.
  924. */
  925. if (inode->i_state & I_SYNC)
  926. goto out;
  927. /*
  928. * Only add valid (hashed) inodes to the superblock's
  929. * dirty list. Add blockdev inodes as well.
  930. */
  931. if (!S_ISBLK(inode->i_mode)) {
  932. if (hlist_unhashed(&inode->i_hash))
  933. goto out;
  934. }
  935. if (inode->i_state & (I_FREEING|I_CLEAR))
  936. goto out;
  937. /*
  938. * If the inode was already on b_dirty/b_io/b_more_io, don't
  939. * reposition it (that would break b_dirty time-ordering).
  940. */
  941. if (!was_dirty) {
  942. struct bdi_writeback *wb = &inode_to_bdi(inode)->wb;
  943. struct backing_dev_info *bdi = wb->bdi;
  944. if (bdi_cap_writeback_dirty(bdi) &&
  945. !test_bit(BDI_registered, &bdi->state)) {
  946. WARN_ON(1);
  947. printk(KERN_ERR "bdi-%s not registered\n",
  948. bdi->name);
  949. }
  950. inode->dirtied_when = jiffies;
  951. list_move(&inode->i_list, &wb->b_dirty);
  952. }
  953. }
  954. out:
  955. spin_unlock(&inode_lock);
  956. }
  957. EXPORT_SYMBOL(__mark_inode_dirty);
  958. /*
  959. * Write out a superblock's list of dirty inodes. A wait will be performed
  960. * upon no inodes, all inodes or the final one, depending upon sync_mode.
  961. *
  962. * If older_than_this is non-NULL, then only write out inodes which
  963. * had their first dirtying at a time earlier than *older_than_this.
  964. *
  965. * If `bdi' is non-zero then we're being asked to writeback a specific queue.
  966. * This function assumes that the blockdev superblock's inodes are backed by
  967. * a variety of queues, so all inodes are searched. For other superblocks,
  968. * assume that all inodes are backed by the same queue.
  969. *
  970. * The inodes to be written are parked on bdi->b_io. They are moved back onto
  971. * bdi->b_dirty as they are selected for writing. This way, none can be missed
  972. * on the writer throttling path, and we get decent balancing between many
  973. * throttled threads: we don't want them all piling up on inode_sync_wait.
  974. */
  975. static void wait_sb_inodes(struct super_block *sb)
  976. {
  977. struct inode *inode, *old_inode = NULL;
  978. /*
  979. * We need to be protected against the filesystem going from
  980. * r/o to r/w or vice versa.
  981. */
  982. WARN_ON(!rwsem_is_locked(&sb->s_umount));
  983. spin_lock(&inode_lock);
  984. /*
  985. * Data integrity sync. Must wait for all pages under writeback,
  986. * because there may have been pages dirtied before our sync
  987. * call, but which had writeout started before we write it out.
  988. * In which case, the inode may not be on the dirty list, but
  989. * we still have to wait for that writeout.
  990. */
  991. list_for_each_entry(inode, &sb->s_inodes, i_sb_list) {
  992. struct address_space *mapping;
  993. if (inode->i_state & (I_FREEING|I_CLEAR|I_WILL_FREE|I_NEW))
  994. continue;
  995. mapping = inode->i_mapping;
  996. if (mapping->nrpages == 0)
  997. continue;
  998. __iget(inode);
  999. spin_unlock(&inode_lock);
  1000. /*
  1001. * We hold a reference to 'inode' so it couldn't have
  1002. * been removed from s_inodes list while we dropped the
  1003. * inode_lock. We cannot iput the inode now as we can
  1004. * be holding the last reference and we cannot iput it
  1005. * under inode_lock. So we keep the reference and iput
  1006. * it later.
  1007. */
  1008. iput(old_inode);
  1009. old_inode = inode;
  1010. filemap_fdatawait(mapping);
  1011. cond_resched();
  1012. spin_lock(&inode_lock);
  1013. }
  1014. spin_unlock(&inode_lock);
  1015. iput(old_inode);
  1016. }
  1017. /**
  1018. * writeback_inodes_sb - writeback dirty inodes from given super_block
  1019. * @sb: the superblock
  1020. *
  1021. * Start writeback on some inodes on this super_block. No guarantees are made
  1022. * on how many (if any) will be written, and this function does not wait
  1023. * for IO completion of submitted IO. The number of pages submitted is
  1024. * returned.
  1025. */
  1026. void writeback_inodes_sb(struct super_block *sb)
  1027. {
  1028. unsigned long nr_dirty = global_page_state(NR_FILE_DIRTY);
  1029. unsigned long nr_unstable = global_page_state(NR_UNSTABLE_NFS);
  1030. struct wb_writeback_args args = {
  1031. .sb = sb,
  1032. .sync_mode = WB_SYNC_NONE,
  1033. };
  1034. WARN_ON(!rwsem_is_locked(&sb->s_umount));
  1035. args.nr_pages = nr_dirty + nr_unstable +
  1036. (inodes_stat.nr_inodes - inodes_stat.nr_unused);
  1037. bdi_queue_work_onstack(&args);
  1038. }
  1039. EXPORT_SYMBOL(writeback_inodes_sb);
  1040. /**
  1041. * writeback_inodes_sb_if_idle - start writeback if none underway
  1042. * @sb: the superblock
  1043. *
  1044. * Invoke writeback_inodes_sb if no writeback is currently underway.
  1045. * Returns 1 if writeback was started, 0 if not.
  1046. */
  1047. int writeback_inodes_sb_if_idle(struct super_block *sb)
  1048. {
  1049. if (!writeback_in_progress(sb->s_bdi)) {
  1050. down_read(&sb->s_umount);
  1051. writeback_inodes_sb(sb);
  1052. up_read(&sb->s_umount);
  1053. return 1;
  1054. } else
  1055. return 0;
  1056. }
  1057. EXPORT_SYMBOL(writeback_inodes_sb_if_idle);
  1058. /**
  1059. * sync_inodes_sb - sync sb inode pages
  1060. * @sb: the superblock
  1061. *
  1062. * This function writes and waits on any dirty inode belonging to this
  1063. * super_block. The number of pages synced is returned.
  1064. */
  1065. void sync_inodes_sb(struct super_block *sb)
  1066. {
  1067. struct wb_writeback_args args = {
  1068. .sb = sb,
  1069. .sync_mode = WB_SYNC_ALL,
  1070. .nr_pages = LONG_MAX,
  1071. .range_cyclic = 0,
  1072. };
  1073. WARN_ON(!rwsem_is_locked(&sb->s_umount));
  1074. bdi_queue_work_onstack(&args);
  1075. wait_sb_inodes(sb);
  1076. }
  1077. EXPORT_SYMBOL(sync_inodes_sb);
  1078. /**
  1079. * write_inode_now - write an inode to disk
  1080. * @inode: inode to write to disk
  1081. * @sync: whether the write should be synchronous or not
  1082. *
  1083. * This function commits an inode to disk immediately if it is dirty. This is
  1084. * primarily needed by knfsd.
  1085. *
  1086. * The caller must either have a ref on the inode or must have set I_WILL_FREE.
  1087. */
  1088. int write_inode_now(struct inode *inode, int sync)
  1089. {
  1090. int ret;
  1091. struct writeback_control wbc = {
  1092. .nr_to_write = LONG_MAX,
  1093. .sync_mode = sync ? WB_SYNC_ALL : WB_SYNC_NONE,
  1094. .range_start = 0,
  1095. .range_end = LLONG_MAX,
  1096. };
  1097. if (!mapping_cap_writeback_dirty(inode->i_mapping))
  1098. wbc.nr_to_write = 0;
  1099. might_sleep();
  1100. spin_lock(&inode_lock);
  1101. ret = writeback_single_inode(inode, &wbc);
  1102. spin_unlock(&inode_lock);
  1103. if (sync)
  1104. inode_sync_wait(inode);
  1105. return ret;
  1106. }
  1107. EXPORT_SYMBOL(write_inode_now);
  1108. /**
  1109. * sync_inode - write an inode and its pages to disk.
  1110. * @inode: the inode to sync
  1111. * @wbc: controls the writeback mode
  1112. *
  1113. * sync_inode() will write an inode and its pages to disk. It will also
  1114. * correctly update the inode on its superblock's dirty inode lists and will
  1115. * update inode->i_state.
  1116. *
  1117. * The caller must have a ref on the inode.
  1118. */
  1119. int sync_inode(struct inode *inode, struct writeback_control *wbc)
  1120. {
  1121. int ret;
  1122. spin_lock(&inode_lock);
  1123. ret = writeback_single_inode(inode, wbc);
  1124. spin_unlock(&inode_lock);
  1125. return ret;
  1126. }
  1127. EXPORT_SYMBOL(sync_inode);