fs-writeback.c 72 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543
  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/export.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/pagemap.h>
  23. #include <linux/kthread.h>
  24. #include <linux/writeback.h>
  25. #include <linux/blkdev.h>
  26. #include <linux/backing-dev.h>
  27. #include <linux/tracepoint.h>
  28. #include <linux/device.h>
  29. #include <linux/memcontrol.h>
  30. #include "internal.h"
  31. /*
  32. * 4MB minimal write chunk size
  33. */
  34. #define MIN_WRITEBACK_PAGES (4096UL >> (PAGE_SHIFT - 10))
  35. struct wb_completion {
  36. atomic_t cnt;
  37. };
  38. /*
  39. * Passed into wb_writeback(), essentially a subset of writeback_control
  40. */
  41. struct wb_writeback_work {
  42. long nr_pages;
  43. struct super_block *sb;
  44. unsigned long *older_than_this;
  45. enum writeback_sync_modes sync_mode;
  46. unsigned int tagged_writepages:1;
  47. unsigned int for_kupdate:1;
  48. unsigned int range_cyclic:1;
  49. unsigned int for_background:1;
  50. unsigned int for_sync:1; /* sync(2) WB_SYNC_ALL writeback */
  51. unsigned int auto_free:1; /* free on completion */
  52. enum wb_reason reason; /* why was writeback initiated? */
  53. struct list_head list; /* pending work list */
  54. struct wb_completion *done; /* set if the caller waits */
  55. };
  56. /*
  57. * If one wants to wait for one or more wb_writeback_works, each work's
  58. * ->done should be set to a wb_completion defined using the following
  59. * macro. Once all work items are issued with wb_queue_work(), the caller
  60. * can wait for the completion of all using wb_wait_for_completion(). Work
  61. * items which are waited upon aren't freed automatically on completion.
  62. */
  63. #define DEFINE_WB_COMPLETION_ONSTACK(cmpl) \
  64. struct wb_completion cmpl = { \
  65. .cnt = ATOMIC_INIT(1), \
  66. }
  67. /*
  68. * If an inode is constantly having its pages dirtied, but then the
  69. * updates stop dirtytime_expire_interval seconds in the past, it's
  70. * possible for the worst case time between when an inode has its
  71. * timestamps updated and when they finally get written out to be two
  72. * dirtytime_expire_intervals. We set the default to 12 hours (in
  73. * seconds), which means most of the time inodes will have their
  74. * timestamps written to disk after 12 hours, but in the worst case a
  75. * few inodes might not their timestamps updated for 24 hours.
  76. */
  77. unsigned int dirtytime_expire_interval = 12 * 60 * 60;
  78. static inline struct inode *wb_inode(struct list_head *head)
  79. {
  80. return list_entry(head, struct inode, i_io_list);
  81. }
  82. /*
  83. * Include the creation of the trace points after defining the
  84. * wb_writeback_work structure and inline functions so that the definition
  85. * remains local to this file.
  86. */
  87. #define CREATE_TRACE_POINTS
  88. #include <trace/events/writeback.h>
  89. EXPORT_TRACEPOINT_SYMBOL_GPL(wbc_writepage);
  90. static bool wb_io_lists_populated(struct bdi_writeback *wb)
  91. {
  92. if (wb_has_dirty_io(wb)) {
  93. return false;
  94. } else {
  95. set_bit(WB_has_dirty_io, &wb->state);
  96. WARN_ON_ONCE(!wb->avg_write_bandwidth);
  97. atomic_long_add(wb->avg_write_bandwidth,
  98. &wb->bdi->tot_write_bandwidth);
  99. return true;
  100. }
  101. }
  102. static void wb_io_lists_depopulated(struct bdi_writeback *wb)
  103. {
  104. if (wb_has_dirty_io(wb) && list_empty(&wb->b_dirty) &&
  105. list_empty(&wb->b_io) && list_empty(&wb->b_more_io)) {
  106. clear_bit(WB_has_dirty_io, &wb->state);
  107. WARN_ON_ONCE(atomic_long_sub_return(wb->avg_write_bandwidth,
  108. &wb->bdi->tot_write_bandwidth) < 0);
  109. }
  110. }
  111. /**
  112. * inode_io_list_move_locked - move an inode onto a bdi_writeback IO list
  113. * @inode: inode to be moved
  114. * @wb: target bdi_writeback
  115. * @head: one of @wb->b_{dirty|io|more_io|dirty_time}
  116. *
  117. * Move @inode->i_io_list to @list of @wb and set %WB_has_dirty_io.
  118. * Returns %true if @inode is the first occupant of the !dirty_time IO
  119. * lists; otherwise, %false.
  120. */
  121. static bool inode_io_list_move_locked(struct inode *inode,
  122. struct bdi_writeback *wb,
  123. struct list_head *head)
  124. {
  125. assert_spin_locked(&wb->list_lock);
  126. list_move(&inode->i_io_list, head);
  127. /* dirty_time doesn't count as dirty_io until expiration */
  128. if (head != &wb->b_dirty_time)
  129. return wb_io_lists_populated(wb);
  130. wb_io_lists_depopulated(wb);
  131. return false;
  132. }
  133. /**
  134. * inode_io_list_del_locked - remove an inode from its bdi_writeback IO list
  135. * @inode: inode to be removed
  136. * @wb: bdi_writeback @inode is being removed from
  137. *
  138. * Remove @inode which may be on one of @wb->b_{dirty|io|more_io} lists and
  139. * clear %WB_has_dirty_io if all are empty afterwards.
  140. */
  141. static void inode_io_list_del_locked(struct inode *inode,
  142. struct bdi_writeback *wb)
  143. {
  144. assert_spin_locked(&wb->list_lock);
  145. list_del_init(&inode->i_io_list);
  146. wb_io_lists_depopulated(wb);
  147. }
  148. static void wb_wakeup(struct bdi_writeback *wb)
  149. {
  150. spin_lock_bh(&wb->work_lock);
  151. if (test_bit(WB_registered, &wb->state))
  152. mod_delayed_work(bdi_wq, &wb->dwork, 0);
  153. spin_unlock_bh(&wb->work_lock);
  154. }
  155. static void finish_writeback_work(struct bdi_writeback *wb,
  156. struct wb_writeback_work *work)
  157. {
  158. struct wb_completion *done = work->done;
  159. if (work->auto_free)
  160. kfree(work);
  161. if (done && atomic_dec_and_test(&done->cnt))
  162. wake_up_all(&wb->bdi->wb_waitq);
  163. }
  164. static void wb_queue_work(struct bdi_writeback *wb,
  165. struct wb_writeback_work *work)
  166. {
  167. trace_writeback_queue(wb, work);
  168. if (work->done)
  169. atomic_inc(&work->done->cnt);
  170. spin_lock_bh(&wb->work_lock);
  171. if (test_bit(WB_registered, &wb->state)) {
  172. list_add_tail(&work->list, &wb->work_list);
  173. mod_delayed_work(bdi_wq, &wb->dwork, 0);
  174. } else
  175. finish_writeback_work(wb, work);
  176. spin_unlock_bh(&wb->work_lock);
  177. }
  178. /**
  179. * wb_wait_for_completion - wait for completion of bdi_writeback_works
  180. * @bdi: bdi work items were issued to
  181. * @done: target wb_completion
  182. *
  183. * Wait for one or more work items issued to @bdi with their ->done field
  184. * set to @done, which should have been defined with
  185. * DEFINE_WB_COMPLETION_ONSTACK(). This function returns after all such
  186. * work items are completed. Work items which are waited upon aren't freed
  187. * automatically on completion.
  188. */
  189. static void wb_wait_for_completion(struct backing_dev_info *bdi,
  190. struct wb_completion *done)
  191. {
  192. atomic_dec(&done->cnt); /* put down the initial count */
  193. wait_event(bdi->wb_waitq, !atomic_read(&done->cnt));
  194. }
  195. #ifdef CONFIG_CGROUP_WRITEBACK
  196. /* parameters for foreign inode detection, see wb_detach_inode() */
  197. #define WB_FRN_TIME_SHIFT 13 /* 1s = 2^13, upto 8 secs w/ 16bit */
  198. #define WB_FRN_TIME_AVG_SHIFT 3 /* avg = avg * 7/8 + new * 1/8 */
  199. #define WB_FRN_TIME_CUT_DIV 2 /* ignore rounds < avg / 2 */
  200. #define WB_FRN_TIME_PERIOD (2 * (1 << WB_FRN_TIME_SHIFT)) /* 2s */
  201. #define WB_FRN_HIST_SLOTS 16 /* inode->i_wb_frn_history is 16bit */
  202. #define WB_FRN_HIST_UNIT (WB_FRN_TIME_PERIOD / WB_FRN_HIST_SLOTS)
  203. /* each slot's duration is 2s / 16 */
  204. #define WB_FRN_HIST_THR_SLOTS (WB_FRN_HIST_SLOTS / 2)
  205. /* if foreign slots >= 8, switch */
  206. #define WB_FRN_HIST_MAX_SLOTS (WB_FRN_HIST_THR_SLOTS / 2 + 1)
  207. /* one round can affect upto 5 slots */
  208. static atomic_t isw_nr_in_flight = ATOMIC_INIT(0);
  209. static struct workqueue_struct *isw_wq;
  210. void __inode_attach_wb(struct inode *inode, struct page *page)
  211. {
  212. struct backing_dev_info *bdi = inode_to_bdi(inode);
  213. struct bdi_writeback *wb = NULL;
  214. if (inode_cgwb_enabled(inode)) {
  215. struct cgroup_subsys_state *memcg_css;
  216. if (page) {
  217. memcg_css = mem_cgroup_css_from_page(page);
  218. wb = wb_get_create(bdi, memcg_css, GFP_ATOMIC);
  219. } else {
  220. /* must pin memcg_css, see wb_get_create() */
  221. memcg_css = task_get_css(current, memory_cgrp_id);
  222. wb = wb_get_create(bdi, memcg_css, GFP_ATOMIC);
  223. css_put(memcg_css);
  224. }
  225. }
  226. if (!wb)
  227. wb = &bdi->wb;
  228. /*
  229. * There may be multiple instances of this function racing to
  230. * update the same inode. Use cmpxchg() to tell the winner.
  231. */
  232. if (unlikely(cmpxchg(&inode->i_wb, NULL, wb)))
  233. wb_put(wb);
  234. }
  235. /**
  236. * locked_inode_to_wb_and_lock_list - determine a locked inode's wb and lock it
  237. * @inode: inode of interest with i_lock held
  238. *
  239. * Returns @inode's wb with its list_lock held. @inode->i_lock must be
  240. * held on entry and is released on return. The returned wb is guaranteed
  241. * to stay @inode's associated wb until its list_lock is released.
  242. */
  243. static struct bdi_writeback *
  244. locked_inode_to_wb_and_lock_list(struct inode *inode)
  245. __releases(&inode->i_lock)
  246. __acquires(&wb->list_lock)
  247. {
  248. while (true) {
  249. struct bdi_writeback *wb = inode_to_wb(inode);
  250. /*
  251. * inode_to_wb() association is protected by both
  252. * @inode->i_lock and @wb->list_lock but list_lock nests
  253. * outside i_lock. Drop i_lock and verify that the
  254. * association hasn't changed after acquiring list_lock.
  255. */
  256. wb_get(wb);
  257. spin_unlock(&inode->i_lock);
  258. spin_lock(&wb->list_lock);
  259. /* i_wb may have changed inbetween, can't use inode_to_wb() */
  260. if (likely(wb == inode->i_wb)) {
  261. wb_put(wb); /* @inode already has ref */
  262. return wb;
  263. }
  264. spin_unlock(&wb->list_lock);
  265. wb_put(wb);
  266. cpu_relax();
  267. spin_lock(&inode->i_lock);
  268. }
  269. }
  270. /**
  271. * inode_to_wb_and_lock_list - determine an inode's wb and lock it
  272. * @inode: inode of interest
  273. *
  274. * Same as locked_inode_to_wb_and_lock_list() but @inode->i_lock isn't held
  275. * on entry.
  276. */
  277. static struct bdi_writeback *inode_to_wb_and_lock_list(struct inode *inode)
  278. __acquires(&wb->list_lock)
  279. {
  280. spin_lock(&inode->i_lock);
  281. return locked_inode_to_wb_and_lock_list(inode);
  282. }
  283. struct inode_switch_wbs_context {
  284. struct inode *inode;
  285. struct bdi_writeback *new_wb;
  286. struct rcu_head rcu_head;
  287. struct work_struct work;
  288. };
  289. static void bdi_down_write_wb_switch_rwsem(struct backing_dev_info *bdi)
  290. {
  291. down_write(&bdi->wb_switch_rwsem);
  292. }
  293. static void bdi_up_write_wb_switch_rwsem(struct backing_dev_info *bdi)
  294. {
  295. up_write(&bdi->wb_switch_rwsem);
  296. }
  297. static void inode_switch_wbs_work_fn(struct work_struct *work)
  298. {
  299. struct inode_switch_wbs_context *isw =
  300. container_of(work, struct inode_switch_wbs_context, work);
  301. struct inode *inode = isw->inode;
  302. struct backing_dev_info *bdi = inode_to_bdi(inode);
  303. struct address_space *mapping = inode->i_mapping;
  304. struct bdi_writeback *old_wb = inode->i_wb;
  305. struct bdi_writeback *new_wb = isw->new_wb;
  306. struct radix_tree_iter iter;
  307. bool switched = false;
  308. void **slot;
  309. /*
  310. * If @inode switches cgwb membership while sync_inodes_sb() is
  311. * being issued, sync_inodes_sb() might miss it. Synchronize.
  312. */
  313. down_read(&bdi->wb_switch_rwsem);
  314. /*
  315. * By the time control reaches here, RCU grace period has passed
  316. * since I_WB_SWITCH assertion and all wb stat update transactions
  317. * between unlocked_inode_to_wb_begin/end() are guaranteed to be
  318. * synchronizing against the i_pages lock.
  319. *
  320. * Grabbing old_wb->list_lock, inode->i_lock and the i_pages lock
  321. * gives us exclusion against all wb related operations on @inode
  322. * including IO list manipulations and stat updates.
  323. */
  324. if (old_wb < new_wb) {
  325. spin_lock(&old_wb->list_lock);
  326. spin_lock_nested(&new_wb->list_lock, SINGLE_DEPTH_NESTING);
  327. } else {
  328. spin_lock(&new_wb->list_lock);
  329. spin_lock_nested(&old_wb->list_lock, SINGLE_DEPTH_NESTING);
  330. }
  331. spin_lock(&inode->i_lock);
  332. xa_lock_irq(&mapping->i_pages);
  333. /*
  334. * Once I_FREEING is visible under i_lock, the eviction path owns
  335. * the inode and we shouldn't modify ->i_io_list.
  336. */
  337. if (unlikely(inode->i_state & I_FREEING))
  338. goto skip_switch;
  339. /*
  340. * Count and transfer stats. Note that PAGECACHE_TAG_DIRTY points
  341. * to possibly dirty pages while PAGECACHE_TAG_WRITEBACK points to
  342. * pages actually under writeback.
  343. */
  344. radix_tree_for_each_tagged(slot, &mapping->i_pages, &iter, 0,
  345. PAGECACHE_TAG_DIRTY) {
  346. struct page *page = radix_tree_deref_slot_protected(slot,
  347. &mapping->i_pages.xa_lock);
  348. if (likely(page) && PageDirty(page)) {
  349. dec_wb_stat(old_wb, WB_RECLAIMABLE);
  350. inc_wb_stat(new_wb, WB_RECLAIMABLE);
  351. }
  352. }
  353. radix_tree_for_each_tagged(slot, &mapping->i_pages, &iter, 0,
  354. PAGECACHE_TAG_WRITEBACK) {
  355. struct page *page = radix_tree_deref_slot_protected(slot,
  356. &mapping->i_pages.xa_lock);
  357. if (likely(page)) {
  358. WARN_ON_ONCE(!PageWriteback(page));
  359. dec_wb_stat(old_wb, WB_WRITEBACK);
  360. inc_wb_stat(new_wb, WB_WRITEBACK);
  361. }
  362. }
  363. wb_get(new_wb);
  364. /*
  365. * Transfer to @new_wb's IO list if necessary. The specific list
  366. * @inode was on is ignored and the inode is put on ->b_dirty which
  367. * is always correct including from ->b_dirty_time. The transfer
  368. * preserves @inode->dirtied_when ordering.
  369. */
  370. if (!list_empty(&inode->i_io_list)) {
  371. struct inode *pos;
  372. inode_io_list_del_locked(inode, old_wb);
  373. inode->i_wb = new_wb;
  374. list_for_each_entry(pos, &new_wb->b_dirty, i_io_list)
  375. if (time_after_eq(inode->dirtied_when,
  376. pos->dirtied_when))
  377. break;
  378. inode_io_list_move_locked(inode, new_wb, pos->i_io_list.prev);
  379. } else {
  380. inode->i_wb = new_wb;
  381. }
  382. /* ->i_wb_frn updates may race wbc_detach_inode() but doesn't matter */
  383. inode->i_wb_frn_winner = 0;
  384. inode->i_wb_frn_avg_time = 0;
  385. inode->i_wb_frn_history = 0;
  386. switched = true;
  387. skip_switch:
  388. /*
  389. * Paired with load_acquire in unlocked_inode_to_wb_begin() and
  390. * ensures that the new wb is visible if they see !I_WB_SWITCH.
  391. */
  392. smp_store_release(&inode->i_state, inode->i_state & ~I_WB_SWITCH);
  393. xa_unlock_irq(&mapping->i_pages);
  394. spin_unlock(&inode->i_lock);
  395. spin_unlock(&new_wb->list_lock);
  396. spin_unlock(&old_wb->list_lock);
  397. up_read(&bdi->wb_switch_rwsem);
  398. if (switched) {
  399. wb_wakeup(new_wb);
  400. wb_put(old_wb);
  401. }
  402. wb_put(new_wb);
  403. iput(inode);
  404. kfree(isw);
  405. atomic_dec(&isw_nr_in_flight);
  406. }
  407. static void inode_switch_wbs_rcu_fn(struct rcu_head *rcu_head)
  408. {
  409. struct inode_switch_wbs_context *isw = container_of(rcu_head,
  410. struct inode_switch_wbs_context, rcu_head);
  411. /* needs to grab bh-unsafe locks, bounce to work item */
  412. INIT_WORK(&isw->work, inode_switch_wbs_work_fn);
  413. queue_work(isw_wq, &isw->work);
  414. }
  415. /**
  416. * inode_switch_wbs - change the wb association of an inode
  417. * @inode: target inode
  418. * @new_wb_id: ID of the new wb
  419. *
  420. * Switch @inode's wb association to the wb identified by @new_wb_id. The
  421. * switching is performed asynchronously and may fail silently.
  422. */
  423. static void inode_switch_wbs(struct inode *inode, int new_wb_id)
  424. {
  425. struct backing_dev_info *bdi = inode_to_bdi(inode);
  426. struct cgroup_subsys_state *memcg_css;
  427. struct inode_switch_wbs_context *isw;
  428. /* noop if seems to be already in progress */
  429. if (inode->i_state & I_WB_SWITCH)
  430. return;
  431. /*
  432. * Avoid starting new switches while sync_inodes_sb() is in
  433. * progress. Otherwise, if the down_write protected issue path
  434. * blocks heavily, we might end up starting a large number of
  435. * switches which will block on the rwsem.
  436. */
  437. if (!down_read_trylock(&bdi->wb_switch_rwsem))
  438. return;
  439. isw = kzalloc(sizeof(*isw), GFP_ATOMIC);
  440. if (!isw)
  441. goto out_unlock;
  442. /* find and pin the new wb */
  443. rcu_read_lock();
  444. memcg_css = css_from_id(new_wb_id, &memory_cgrp_subsys);
  445. if (memcg_css)
  446. isw->new_wb = wb_get_create(bdi, memcg_css, GFP_ATOMIC);
  447. rcu_read_unlock();
  448. if (!isw->new_wb)
  449. goto out_free;
  450. /* while holding I_WB_SWITCH, no one else can update the association */
  451. spin_lock(&inode->i_lock);
  452. if (!(inode->i_sb->s_flags & SB_ACTIVE) ||
  453. inode->i_state & (I_WB_SWITCH | I_FREEING) ||
  454. inode_to_wb(inode) == isw->new_wb) {
  455. spin_unlock(&inode->i_lock);
  456. goto out_free;
  457. }
  458. inode->i_state |= I_WB_SWITCH;
  459. __iget(inode);
  460. spin_unlock(&inode->i_lock);
  461. isw->inode = inode;
  462. /*
  463. * In addition to synchronizing among switchers, I_WB_SWITCH tells
  464. * the RCU protected stat update paths to grab the i_page
  465. * lock so that stat transfer can synchronize against them.
  466. * Let's continue after I_WB_SWITCH is guaranteed to be visible.
  467. */
  468. call_rcu(&isw->rcu_head, inode_switch_wbs_rcu_fn);
  469. atomic_inc(&isw_nr_in_flight);
  470. goto out_unlock;
  471. out_free:
  472. if (isw->new_wb)
  473. wb_put(isw->new_wb);
  474. kfree(isw);
  475. out_unlock:
  476. up_read(&bdi->wb_switch_rwsem);
  477. }
  478. /**
  479. * wbc_attach_and_unlock_inode - associate wbc with target inode and unlock it
  480. * @wbc: writeback_control of interest
  481. * @inode: target inode
  482. *
  483. * @inode is locked and about to be written back under the control of @wbc.
  484. * Record @inode's writeback context into @wbc and unlock the i_lock. On
  485. * writeback completion, wbc_detach_inode() should be called. This is used
  486. * to track the cgroup writeback context.
  487. */
  488. void wbc_attach_and_unlock_inode(struct writeback_control *wbc,
  489. struct inode *inode)
  490. {
  491. if (!inode_cgwb_enabled(inode)) {
  492. spin_unlock(&inode->i_lock);
  493. return;
  494. }
  495. wbc->wb = inode_to_wb(inode);
  496. wbc->inode = inode;
  497. wbc->wb_id = wbc->wb->memcg_css->id;
  498. wbc->wb_lcand_id = inode->i_wb_frn_winner;
  499. wbc->wb_tcand_id = 0;
  500. wbc->wb_bytes = 0;
  501. wbc->wb_lcand_bytes = 0;
  502. wbc->wb_tcand_bytes = 0;
  503. wb_get(wbc->wb);
  504. spin_unlock(&inode->i_lock);
  505. /*
  506. * A dying wb indicates that either the blkcg associated with the
  507. * memcg changed or the associated memcg is dying. In the first
  508. * case, a replacement wb should already be available and we should
  509. * refresh the wb immediately. In the second case, trying to
  510. * refresh will keep failing.
  511. */
  512. if (unlikely(wb_dying(wbc->wb) && !css_is_dying(wbc->wb->memcg_css)))
  513. inode_switch_wbs(inode, wbc->wb_id);
  514. }
  515. /**
  516. * wbc_detach_inode - disassociate wbc from inode and perform foreign detection
  517. * @wbc: writeback_control of the just finished writeback
  518. *
  519. * To be called after a writeback attempt of an inode finishes and undoes
  520. * wbc_attach_and_unlock_inode(). Can be called under any context.
  521. *
  522. * As concurrent write sharing of an inode is expected to be very rare and
  523. * memcg only tracks page ownership on first-use basis severely confining
  524. * the usefulness of such sharing, cgroup writeback tracks ownership
  525. * per-inode. While the support for concurrent write sharing of an inode
  526. * is deemed unnecessary, an inode being written to by different cgroups at
  527. * different points in time is a lot more common, and, more importantly,
  528. * charging only by first-use can too readily lead to grossly incorrect
  529. * behaviors (single foreign page can lead to gigabytes of writeback to be
  530. * incorrectly attributed).
  531. *
  532. * To resolve this issue, cgroup writeback detects the majority dirtier of
  533. * an inode and transfers the ownership to it. To avoid unnnecessary
  534. * oscillation, the detection mechanism keeps track of history and gives
  535. * out the switch verdict only if the foreign usage pattern is stable over
  536. * a certain amount of time and/or writeback attempts.
  537. *
  538. * On each writeback attempt, @wbc tries to detect the majority writer
  539. * using Boyer-Moore majority vote algorithm. In addition to the byte
  540. * count from the majority voting, it also counts the bytes written for the
  541. * current wb and the last round's winner wb (max of last round's current
  542. * wb, the winner from two rounds ago, and the last round's majority
  543. * candidate). Keeping track of the historical winner helps the algorithm
  544. * to semi-reliably detect the most active writer even when it's not the
  545. * absolute majority.
  546. *
  547. * Once the winner of the round is determined, whether the winner is
  548. * foreign or not and how much IO time the round consumed is recorded in
  549. * inode->i_wb_frn_history. If the amount of recorded foreign IO time is
  550. * over a certain threshold, the switch verdict is given.
  551. */
  552. void wbc_detach_inode(struct writeback_control *wbc)
  553. {
  554. struct bdi_writeback *wb = wbc->wb;
  555. struct inode *inode = wbc->inode;
  556. unsigned long avg_time, max_bytes, max_time;
  557. u16 history;
  558. int max_id;
  559. if (!wb)
  560. return;
  561. history = inode->i_wb_frn_history;
  562. avg_time = inode->i_wb_frn_avg_time;
  563. /* pick the winner of this round */
  564. if (wbc->wb_bytes >= wbc->wb_lcand_bytes &&
  565. wbc->wb_bytes >= wbc->wb_tcand_bytes) {
  566. max_id = wbc->wb_id;
  567. max_bytes = wbc->wb_bytes;
  568. } else if (wbc->wb_lcand_bytes >= wbc->wb_tcand_bytes) {
  569. max_id = wbc->wb_lcand_id;
  570. max_bytes = wbc->wb_lcand_bytes;
  571. } else {
  572. max_id = wbc->wb_tcand_id;
  573. max_bytes = wbc->wb_tcand_bytes;
  574. }
  575. /*
  576. * Calculate the amount of IO time the winner consumed and fold it
  577. * into the running average kept per inode. If the consumed IO
  578. * time is lower than avag / WB_FRN_TIME_CUT_DIV, ignore it for
  579. * deciding whether to switch or not. This is to prevent one-off
  580. * small dirtiers from skewing the verdict.
  581. */
  582. max_time = DIV_ROUND_UP((max_bytes >> PAGE_SHIFT) << WB_FRN_TIME_SHIFT,
  583. wb->avg_write_bandwidth);
  584. if (avg_time)
  585. avg_time += (max_time >> WB_FRN_TIME_AVG_SHIFT) -
  586. (avg_time >> WB_FRN_TIME_AVG_SHIFT);
  587. else
  588. avg_time = max_time; /* immediate catch up on first run */
  589. if (max_time >= avg_time / WB_FRN_TIME_CUT_DIV) {
  590. int slots;
  591. /*
  592. * The switch verdict is reached if foreign wb's consume
  593. * more than a certain proportion of IO time in a
  594. * WB_FRN_TIME_PERIOD. This is loosely tracked by 16 slot
  595. * history mask where each bit represents one sixteenth of
  596. * the period. Determine the number of slots to shift into
  597. * history from @max_time.
  598. */
  599. slots = min(DIV_ROUND_UP(max_time, WB_FRN_HIST_UNIT),
  600. (unsigned long)WB_FRN_HIST_MAX_SLOTS);
  601. history <<= slots;
  602. if (wbc->wb_id != max_id)
  603. history |= (1U << slots) - 1;
  604. /*
  605. * Switch if the current wb isn't the consistent winner.
  606. * If there are multiple closely competing dirtiers, the
  607. * inode may switch across them repeatedly over time, which
  608. * is okay. The main goal is avoiding keeping an inode on
  609. * the wrong wb for an extended period of time.
  610. */
  611. if (hweight32(history) > WB_FRN_HIST_THR_SLOTS)
  612. inode_switch_wbs(inode, max_id);
  613. }
  614. /*
  615. * Multiple instances of this function may race to update the
  616. * following fields but we don't mind occassional inaccuracies.
  617. */
  618. inode->i_wb_frn_winner = max_id;
  619. inode->i_wb_frn_avg_time = min(avg_time, (unsigned long)U16_MAX);
  620. inode->i_wb_frn_history = history;
  621. wb_put(wbc->wb);
  622. wbc->wb = NULL;
  623. }
  624. /**
  625. * wbc_account_io - account IO issued during writeback
  626. * @wbc: writeback_control of the writeback in progress
  627. * @page: page being written out
  628. * @bytes: number of bytes being written out
  629. *
  630. * @bytes from @page are about to written out during the writeback
  631. * controlled by @wbc. Keep the book for foreign inode detection. See
  632. * wbc_detach_inode().
  633. */
  634. void wbc_account_io(struct writeback_control *wbc, struct page *page,
  635. size_t bytes)
  636. {
  637. struct cgroup_subsys_state *css;
  638. int id;
  639. /*
  640. * pageout() path doesn't attach @wbc to the inode being written
  641. * out. This is intentional as we don't want the function to block
  642. * behind a slow cgroup. Ultimately, we want pageout() to kick off
  643. * regular writeback instead of writing things out itself.
  644. */
  645. if (!wbc->wb)
  646. return;
  647. css = mem_cgroup_css_from_page(page);
  648. /* dead cgroups shouldn't contribute to inode ownership arbitration */
  649. if (!(css->flags & CSS_ONLINE))
  650. return;
  651. id = css->id;
  652. if (id == wbc->wb_id) {
  653. wbc->wb_bytes += bytes;
  654. return;
  655. }
  656. if (id == wbc->wb_lcand_id)
  657. wbc->wb_lcand_bytes += bytes;
  658. /* Boyer-Moore majority vote algorithm */
  659. if (!wbc->wb_tcand_bytes)
  660. wbc->wb_tcand_id = id;
  661. if (id == wbc->wb_tcand_id)
  662. wbc->wb_tcand_bytes += bytes;
  663. else
  664. wbc->wb_tcand_bytes -= min(bytes, wbc->wb_tcand_bytes);
  665. }
  666. EXPORT_SYMBOL_GPL(wbc_account_io);
  667. /**
  668. * inode_congested - test whether an inode is congested
  669. * @inode: inode to test for congestion (may be NULL)
  670. * @cong_bits: mask of WB_[a]sync_congested bits to test
  671. *
  672. * Tests whether @inode is congested. @cong_bits is the mask of congestion
  673. * bits to test and the return value is the mask of set bits.
  674. *
  675. * If cgroup writeback is enabled for @inode, the congestion state is
  676. * determined by whether the cgwb (cgroup bdi_writeback) for the blkcg
  677. * associated with @inode is congested; otherwise, the root wb's congestion
  678. * state is used.
  679. *
  680. * @inode is allowed to be NULL as this function is often called on
  681. * mapping->host which is NULL for the swapper space.
  682. */
  683. int inode_congested(struct inode *inode, int cong_bits)
  684. {
  685. /*
  686. * Once set, ->i_wb never becomes NULL while the inode is alive.
  687. * Start transaction iff ->i_wb is visible.
  688. */
  689. if (inode && inode_to_wb_is_valid(inode)) {
  690. struct bdi_writeback *wb;
  691. struct wb_lock_cookie lock_cookie = {};
  692. bool congested;
  693. wb = unlocked_inode_to_wb_begin(inode, &lock_cookie);
  694. congested = wb_congested(wb, cong_bits);
  695. unlocked_inode_to_wb_end(inode, &lock_cookie);
  696. return congested;
  697. }
  698. return wb_congested(&inode_to_bdi(inode)->wb, cong_bits);
  699. }
  700. EXPORT_SYMBOL_GPL(inode_congested);
  701. /**
  702. * wb_split_bdi_pages - split nr_pages to write according to bandwidth
  703. * @wb: target bdi_writeback to split @nr_pages to
  704. * @nr_pages: number of pages to write for the whole bdi
  705. *
  706. * Split @wb's portion of @nr_pages according to @wb's write bandwidth in
  707. * relation to the total write bandwidth of all wb's w/ dirty inodes on
  708. * @wb->bdi.
  709. */
  710. static long wb_split_bdi_pages(struct bdi_writeback *wb, long nr_pages)
  711. {
  712. unsigned long this_bw = wb->avg_write_bandwidth;
  713. unsigned long tot_bw = atomic_long_read(&wb->bdi->tot_write_bandwidth);
  714. if (nr_pages == LONG_MAX)
  715. return LONG_MAX;
  716. /*
  717. * This may be called on clean wb's and proportional distribution
  718. * may not make sense, just use the original @nr_pages in those
  719. * cases. In general, we wanna err on the side of writing more.
  720. */
  721. if (!tot_bw || this_bw >= tot_bw)
  722. return nr_pages;
  723. else
  724. return DIV_ROUND_UP_ULL((u64)nr_pages * this_bw, tot_bw);
  725. }
  726. /**
  727. * bdi_split_work_to_wbs - split a wb_writeback_work to all wb's of a bdi
  728. * @bdi: target backing_dev_info
  729. * @base_work: wb_writeback_work to issue
  730. * @skip_if_busy: skip wb's which already have writeback in progress
  731. *
  732. * Split and issue @base_work to all wb's (bdi_writeback's) of @bdi which
  733. * have dirty inodes. If @base_work->nr_page isn't %LONG_MAX, it's
  734. * distributed to the busy wbs according to each wb's proportion in the
  735. * total active write bandwidth of @bdi.
  736. */
  737. static void bdi_split_work_to_wbs(struct backing_dev_info *bdi,
  738. struct wb_writeback_work *base_work,
  739. bool skip_if_busy)
  740. {
  741. struct bdi_writeback *last_wb = NULL;
  742. struct bdi_writeback *wb = list_entry(&bdi->wb_list,
  743. struct bdi_writeback, bdi_node);
  744. might_sleep();
  745. restart:
  746. rcu_read_lock();
  747. list_for_each_entry_continue_rcu(wb, &bdi->wb_list, bdi_node) {
  748. DEFINE_WB_COMPLETION_ONSTACK(fallback_work_done);
  749. struct wb_writeback_work fallback_work;
  750. struct wb_writeback_work *work;
  751. long nr_pages;
  752. if (last_wb) {
  753. wb_put(last_wb);
  754. last_wb = NULL;
  755. }
  756. /* SYNC_ALL writes out I_DIRTY_TIME too */
  757. if (!wb_has_dirty_io(wb) &&
  758. (base_work->sync_mode == WB_SYNC_NONE ||
  759. list_empty(&wb->b_dirty_time)))
  760. continue;
  761. if (skip_if_busy && writeback_in_progress(wb))
  762. continue;
  763. nr_pages = wb_split_bdi_pages(wb, base_work->nr_pages);
  764. work = kmalloc(sizeof(*work), GFP_ATOMIC);
  765. if (work) {
  766. *work = *base_work;
  767. work->nr_pages = nr_pages;
  768. work->auto_free = 1;
  769. wb_queue_work(wb, work);
  770. continue;
  771. }
  772. /* alloc failed, execute synchronously using on-stack fallback */
  773. work = &fallback_work;
  774. *work = *base_work;
  775. work->nr_pages = nr_pages;
  776. work->auto_free = 0;
  777. work->done = &fallback_work_done;
  778. wb_queue_work(wb, work);
  779. /*
  780. * Pin @wb so that it stays on @bdi->wb_list. This allows
  781. * continuing iteration from @wb after dropping and
  782. * regrabbing rcu read lock.
  783. */
  784. wb_get(wb);
  785. last_wb = wb;
  786. rcu_read_unlock();
  787. wb_wait_for_completion(bdi, &fallback_work_done);
  788. goto restart;
  789. }
  790. rcu_read_unlock();
  791. if (last_wb)
  792. wb_put(last_wb);
  793. }
  794. /**
  795. * cgroup_writeback_umount - flush inode wb switches for umount
  796. *
  797. * This function is called when a super_block is about to be destroyed and
  798. * flushes in-flight inode wb switches. An inode wb switch goes through
  799. * RCU and then workqueue, so the two need to be flushed in order to ensure
  800. * that all previously scheduled switches are finished. As wb switches are
  801. * rare occurrences and synchronize_rcu() can take a while, perform
  802. * flushing iff wb switches are in flight.
  803. */
  804. void cgroup_writeback_umount(void)
  805. {
  806. if (atomic_read(&isw_nr_in_flight)) {
  807. /*
  808. * Use rcu_barrier() to wait for all pending callbacks to
  809. * ensure that all in-flight wb switches are in the workqueue.
  810. */
  811. rcu_barrier();
  812. flush_workqueue(isw_wq);
  813. }
  814. }
  815. static int __init cgroup_writeback_init(void)
  816. {
  817. isw_wq = alloc_workqueue("inode_switch_wbs", 0, 0);
  818. if (!isw_wq)
  819. return -ENOMEM;
  820. return 0;
  821. }
  822. fs_initcall(cgroup_writeback_init);
  823. #else /* CONFIG_CGROUP_WRITEBACK */
  824. static void bdi_down_write_wb_switch_rwsem(struct backing_dev_info *bdi) { }
  825. static void bdi_up_write_wb_switch_rwsem(struct backing_dev_info *bdi) { }
  826. static struct bdi_writeback *
  827. locked_inode_to_wb_and_lock_list(struct inode *inode)
  828. __releases(&inode->i_lock)
  829. __acquires(&wb->list_lock)
  830. {
  831. struct bdi_writeback *wb = inode_to_wb(inode);
  832. spin_unlock(&inode->i_lock);
  833. spin_lock(&wb->list_lock);
  834. return wb;
  835. }
  836. static struct bdi_writeback *inode_to_wb_and_lock_list(struct inode *inode)
  837. __acquires(&wb->list_lock)
  838. {
  839. struct bdi_writeback *wb = inode_to_wb(inode);
  840. spin_lock(&wb->list_lock);
  841. return wb;
  842. }
  843. static long wb_split_bdi_pages(struct bdi_writeback *wb, long nr_pages)
  844. {
  845. return nr_pages;
  846. }
  847. static void bdi_split_work_to_wbs(struct backing_dev_info *bdi,
  848. struct wb_writeback_work *base_work,
  849. bool skip_if_busy)
  850. {
  851. might_sleep();
  852. if (!skip_if_busy || !writeback_in_progress(&bdi->wb)) {
  853. base_work->auto_free = 0;
  854. wb_queue_work(&bdi->wb, base_work);
  855. }
  856. }
  857. #endif /* CONFIG_CGROUP_WRITEBACK */
  858. /*
  859. * Add in the number of potentially dirty inodes, because each inode
  860. * write can dirty pagecache in the underlying blockdev.
  861. */
  862. static unsigned long get_nr_dirty_pages(void)
  863. {
  864. return global_node_page_state(NR_FILE_DIRTY) +
  865. global_node_page_state(NR_UNSTABLE_NFS) +
  866. get_nr_dirty_inodes();
  867. }
  868. static void wb_start_writeback(struct bdi_writeback *wb, enum wb_reason reason)
  869. {
  870. if (!wb_has_dirty_io(wb))
  871. return;
  872. /*
  873. * All callers of this function want to start writeback of all
  874. * dirty pages. Places like vmscan can call this at a very
  875. * high frequency, causing pointless allocations of tons of
  876. * work items and keeping the flusher threads busy retrieving
  877. * that work. Ensure that we only allow one of them pending and
  878. * inflight at the time.
  879. */
  880. if (test_bit(WB_start_all, &wb->state) ||
  881. test_and_set_bit(WB_start_all, &wb->state))
  882. return;
  883. wb->start_all_reason = reason;
  884. wb_wakeup(wb);
  885. }
  886. /**
  887. * wb_start_background_writeback - start background writeback
  888. * @wb: bdi_writback to write from
  889. *
  890. * Description:
  891. * This makes sure WB_SYNC_NONE background writeback happens. When
  892. * this function returns, it is only guaranteed that for given wb
  893. * some IO is happening if we are over background dirty threshold.
  894. * Caller need not hold sb s_umount semaphore.
  895. */
  896. void wb_start_background_writeback(struct bdi_writeback *wb)
  897. {
  898. /*
  899. * We just wake up the flusher thread. It will perform background
  900. * writeback as soon as there is no other work to do.
  901. */
  902. trace_writeback_wake_background(wb);
  903. wb_wakeup(wb);
  904. }
  905. /*
  906. * Remove the inode from the writeback list it is on.
  907. */
  908. void inode_io_list_del(struct inode *inode)
  909. {
  910. struct bdi_writeback *wb;
  911. wb = inode_to_wb_and_lock_list(inode);
  912. inode_io_list_del_locked(inode, wb);
  913. spin_unlock(&wb->list_lock);
  914. }
  915. /*
  916. * mark an inode as under writeback on the sb
  917. */
  918. void sb_mark_inode_writeback(struct inode *inode)
  919. {
  920. struct super_block *sb = inode->i_sb;
  921. unsigned long flags;
  922. if (list_empty(&inode->i_wb_list)) {
  923. spin_lock_irqsave(&sb->s_inode_wblist_lock, flags);
  924. if (list_empty(&inode->i_wb_list)) {
  925. list_add_tail(&inode->i_wb_list, &sb->s_inodes_wb);
  926. trace_sb_mark_inode_writeback(inode);
  927. }
  928. spin_unlock_irqrestore(&sb->s_inode_wblist_lock, flags);
  929. }
  930. }
  931. /*
  932. * clear an inode as under writeback on the sb
  933. */
  934. void sb_clear_inode_writeback(struct inode *inode)
  935. {
  936. struct super_block *sb = inode->i_sb;
  937. unsigned long flags;
  938. if (!list_empty(&inode->i_wb_list)) {
  939. spin_lock_irqsave(&sb->s_inode_wblist_lock, flags);
  940. if (!list_empty(&inode->i_wb_list)) {
  941. list_del_init(&inode->i_wb_list);
  942. trace_sb_clear_inode_writeback(inode);
  943. }
  944. spin_unlock_irqrestore(&sb->s_inode_wblist_lock, flags);
  945. }
  946. }
  947. /*
  948. * Redirty an inode: set its when-it-was dirtied timestamp and move it to the
  949. * furthest end of its superblock's dirty-inode list.
  950. *
  951. * Before stamping the inode's ->dirtied_when, we check to see whether it is
  952. * already the most-recently-dirtied inode on the b_dirty list. If that is
  953. * the case then the inode must have been redirtied while it was being written
  954. * out and we don't reset its dirtied_when.
  955. */
  956. static void redirty_tail(struct inode *inode, struct bdi_writeback *wb)
  957. {
  958. if (!list_empty(&wb->b_dirty)) {
  959. struct inode *tail;
  960. tail = wb_inode(wb->b_dirty.next);
  961. if (time_before(inode->dirtied_when, tail->dirtied_when))
  962. inode->dirtied_when = jiffies;
  963. }
  964. inode_io_list_move_locked(inode, wb, &wb->b_dirty);
  965. }
  966. /*
  967. * requeue inode for re-scanning after bdi->b_io list is exhausted.
  968. */
  969. static void requeue_io(struct inode *inode, struct bdi_writeback *wb)
  970. {
  971. inode_io_list_move_locked(inode, wb, &wb->b_more_io);
  972. }
  973. static void inode_sync_complete(struct inode *inode)
  974. {
  975. inode->i_state &= ~I_SYNC;
  976. /* If inode is clean an unused, put it into LRU now... */
  977. inode_add_lru(inode);
  978. /* Waiters must see I_SYNC cleared before being woken up */
  979. smp_mb();
  980. wake_up_bit(&inode->i_state, __I_SYNC);
  981. }
  982. static bool inode_dirtied_after(struct inode *inode, unsigned long t)
  983. {
  984. bool ret = time_after(inode->dirtied_when, t);
  985. #ifndef CONFIG_64BIT
  986. /*
  987. * For inodes being constantly redirtied, dirtied_when can get stuck.
  988. * It _appears_ to be in the future, but is actually in distant past.
  989. * This test is necessary to prevent such wrapped-around relative times
  990. * from permanently stopping the whole bdi writeback.
  991. */
  992. ret = ret && time_before_eq(inode->dirtied_when, jiffies);
  993. #endif
  994. return ret;
  995. }
  996. #define EXPIRE_DIRTY_ATIME 0x0001
  997. /*
  998. * Move expired (dirtied before work->older_than_this) dirty inodes from
  999. * @delaying_queue to @dispatch_queue.
  1000. */
  1001. static int move_expired_inodes(struct list_head *delaying_queue,
  1002. struct list_head *dispatch_queue,
  1003. int flags,
  1004. struct wb_writeback_work *work)
  1005. {
  1006. unsigned long *older_than_this = NULL;
  1007. unsigned long expire_time;
  1008. LIST_HEAD(tmp);
  1009. struct list_head *pos, *node;
  1010. struct super_block *sb = NULL;
  1011. struct inode *inode;
  1012. int do_sb_sort = 0;
  1013. int moved = 0;
  1014. if ((flags & EXPIRE_DIRTY_ATIME) == 0)
  1015. older_than_this = work->older_than_this;
  1016. else if (!work->for_sync) {
  1017. expire_time = jiffies - (dirtytime_expire_interval * HZ);
  1018. older_than_this = &expire_time;
  1019. }
  1020. while (!list_empty(delaying_queue)) {
  1021. inode = wb_inode(delaying_queue->prev);
  1022. if (older_than_this &&
  1023. inode_dirtied_after(inode, *older_than_this))
  1024. break;
  1025. list_move(&inode->i_io_list, &tmp);
  1026. moved++;
  1027. if (flags & EXPIRE_DIRTY_ATIME)
  1028. set_bit(__I_DIRTY_TIME_EXPIRED, &inode->i_state);
  1029. if (sb_is_blkdev_sb(inode->i_sb))
  1030. continue;
  1031. if (sb && sb != inode->i_sb)
  1032. do_sb_sort = 1;
  1033. sb = inode->i_sb;
  1034. }
  1035. /* just one sb in list, splice to dispatch_queue and we're done */
  1036. if (!do_sb_sort) {
  1037. list_splice(&tmp, dispatch_queue);
  1038. goto out;
  1039. }
  1040. /* Move inodes from one superblock together */
  1041. while (!list_empty(&tmp)) {
  1042. sb = wb_inode(tmp.prev)->i_sb;
  1043. list_for_each_prev_safe(pos, node, &tmp) {
  1044. inode = wb_inode(pos);
  1045. if (inode->i_sb == sb)
  1046. list_move(&inode->i_io_list, dispatch_queue);
  1047. }
  1048. }
  1049. out:
  1050. return moved;
  1051. }
  1052. /*
  1053. * Queue all expired dirty inodes for io, eldest first.
  1054. * Before
  1055. * newly dirtied b_dirty b_io b_more_io
  1056. * =============> gf edc BA
  1057. * After
  1058. * newly dirtied b_dirty b_io b_more_io
  1059. * =============> g fBAedc
  1060. * |
  1061. * +--> dequeue for IO
  1062. */
  1063. static void queue_io(struct bdi_writeback *wb, struct wb_writeback_work *work)
  1064. {
  1065. int moved;
  1066. assert_spin_locked(&wb->list_lock);
  1067. list_splice_init(&wb->b_more_io, &wb->b_io);
  1068. moved = move_expired_inodes(&wb->b_dirty, &wb->b_io, 0, work);
  1069. moved += move_expired_inodes(&wb->b_dirty_time, &wb->b_io,
  1070. EXPIRE_DIRTY_ATIME, work);
  1071. if (moved)
  1072. wb_io_lists_populated(wb);
  1073. trace_writeback_queue_io(wb, work, moved);
  1074. }
  1075. static int write_inode(struct inode *inode, struct writeback_control *wbc)
  1076. {
  1077. int ret;
  1078. if (inode->i_sb->s_op->write_inode && !is_bad_inode(inode)) {
  1079. trace_writeback_write_inode_start(inode, wbc);
  1080. ret = inode->i_sb->s_op->write_inode(inode, wbc);
  1081. trace_writeback_write_inode(inode, wbc);
  1082. return ret;
  1083. }
  1084. return 0;
  1085. }
  1086. /*
  1087. * Wait for writeback on an inode to complete. Called with i_lock held.
  1088. * Caller must make sure inode cannot go away when we drop i_lock.
  1089. */
  1090. static void __inode_wait_for_writeback(struct inode *inode)
  1091. __releases(inode->i_lock)
  1092. __acquires(inode->i_lock)
  1093. {
  1094. DEFINE_WAIT_BIT(wq, &inode->i_state, __I_SYNC);
  1095. wait_queue_head_t *wqh;
  1096. wqh = bit_waitqueue(&inode->i_state, __I_SYNC);
  1097. while (inode->i_state & I_SYNC) {
  1098. spin_unlock(&inode->i_lock);
  1099. __wait_on_bit(wqh, &wq, bit_wait,
  1100. TASK_UNINTERRUPTIBLE);
  1101. spin_lock(&inode->i_lock);
  1102. }
  1103. }
  1104. /*
  1105. * Wait for writeback on an inode to complete. Caller must have inode pinned.
  1106. */
  1107. void inode_wait_for_writeback(struct inode *inode)
  1108. {
  1109. spin_lock(&inode->i_lock);
  1110. __inode_wait_for_writeback(inode);
  1111. spin_unlock(&inode->i_lock);
  1112. }
  1113. /*
  1114. * Sleep until I_SYNC is cleared. This function must be called with i_lock
  1115. * held and drops it. It is aimed for callers not holding any inode reference
  1116. * so once i_lock is dropped, inode can go away.
  1117. */
  1118. static void inode_sleep_on_writeback(struct inode *inode)
  1119. __releases(inode->i_lock)
  1120. {
  1121. DEFINE_WAIT(wait);
  1122. wait_queue_head_t *wqh = bit_waitqueue(&inode->i_state, __I_SYNC);
  1123. int sleep;
  1124. prepare_to_wait(wqh, &wait, TASK_UNINTERRUPTIBLE);
  1125. sleep = inode->i_state & I_SYNC;
  1126. spin_unlock(&inode->i_lock);
  1127. if (sleep)
  1128. schedule();
  1129. finish_wait(wqh, &wait);
  1130. }
  1131. /*
  1132. * Find proper writeback list for the inode depending on its current state and
  1133. * possibly also change of its state while we were doing writeback. Here we
  1134. * handle things such as livelock prevention or fairness of writeback among
  1135. * inodes. This function can be called only by flusher thread - noone else
  1136. * processes all inodes in writeback lists and requeueing inodes behind flusher
  1137. * thread's back can have unexpected consequences.
  1138. */
  1139. static void requeue_inode(struct inode *inode, struct bdi_writeback *wb,
  1140. struct writeback_control *wbc)
  1141. {
  1142. if (inode->i_state & I_FREEING)
  1143. return;
  1144. /*
  1145. * Sync livelock prevention. Each inode is tagged and synced in one
  1146. * shot. If still dirty, it will be redirty_tail()'ed below. Update
  1147. * the dirty time to prevent enqueue and sync it again.
  1148. */
  1149. if ((inode->i_state & I_DIRTY) &&
  1150. (wbc->sync_mode == WB_SYNC_ALL || wbc->tagged_writepages))
  1151. inode->dirtied_when = jiffies;
  1152. if (wbc->pages_skipped) {
  1153. /*
  1154. * writeback is not making progress due to locked
  1155. * buffers. Skip this inode for now.
  1156. */
  1157. redirty_tail(inode, wb);
  1158. return;
  1159. }
  1160. if (mapping_tagged(inode->i_mapping, PAGECACHE_TAG_DIRTY)) {
  1161. /*
  1162. * We didn't write back all the pages. nfs_writepages()
  1163. * sometimes bales out without doing anything.
  1164. */
  1165. if (wbc->nr_to_write <= 0) {
  1166. /* Slice used up. Queue for next turn. */
  1167. requeue_io(inode, wb);
  1168. } else {
  1169. /*
  1170. * Writeback blocked by something other than
  1171. * congestion. Delay the inode for some time to
  1172. * avoid spinning on the CPU (100% iowait)
  1173. * retrying writeback of the dirty page/inode
  1174. * that cannot be performed immediately.
  1175. */
  1176. redirty_tail(inode, wb);
  1177. }
  1178. } else if (inode->i_state & I_DIRTY) {
  1179. /*
  1180. * Filesystems can dirty the inode during writeback operations,
  1181. * such as delayed allocation during submission or metadata
  1182. * updates after data IO completion.
  1183. */
  1184. redirty_tail(inode, wb);
  1185. } else if (inode->i_state & I_DIRTY_TIME) {
  1186. inode->dirtied_when = jiffies;
  1187. inode_io_list_move_locked(inode, wb, &wb->b_dirty_time);
  1188. } else {
  1189. /* The inode is clean. Remove from writeback lists. */
  1190. inode_io_list_del_locked(inode, wb);
  1191. }
  1192. }
  1193. /*
  1194. * Write out an inode and its dirty pages. Do not update the writeback list
  1195. * linkage. That is left to the caller. The caller is also responsible for
  1196. * setting I_SYNC flag and calling inode_sync_complete() to clear it.
  1197. */
  1198. static int
  1199. __writeback_single_inode(struct inode *inode, struct writeback_control *wbc)
  1200. {
  1201. struct address_space *mapping = inode->i_mapping;
  1202. long nr_to_write = wbc->nr_to_write;
  1203. unsigned dirty;
  1204. int ret;
  1205. WARN_ON(!(inode->i_state & I_SYNC));
  1206. trace_writeback_single_inode_start(inode, wbc, nr_to_write);
  1207. ret = do_writepages(mapping, wbc);
  1208. /*
  1209. * Make sure to wait on the data before writing out the metadata.
  1210. * This is important for filesystems that modify metadata on data
  1211. * I/O completion. We don't do it for sync(2) writeback because it has a
  1212. * separate, external IO completion path and ->sync_fs for guaranteeing
  1213. * inode metadata is written back correctly.
  1214. */
  1215. if (wbc->sync_mode == WB_SYNC_ALL && !wbc->for_sync) {
  1216. int err = filemap_fdatawait(mapping);
  1217. if (ret == 0)
  1218. ret = err;
  1219. }
  1220. /*
  1221. * Some filesystems may redirty the inode during the writeback
  1222. * due to delalloc, clear dirty metadata flags right before
  1223. * write_inode()
  1224. */
  1225. spin_lock(&inode->i_lock);
  1226. dirty = inode->i_state & I_DIRTY;
  1227. if (inode->i_state & I_DIRTY_TIME) {
  1228. if ((dirty & I_DIRTY_INODE) ||
  1229. wbc->sync_mode == WB_SYNC_ALL ||
  1230. unlikely(inode->i_state & I_DIRTY_TIME_EXPIRED) ||
  1231. unlikely(time_after(jiffies,
  1232. (inode->dirtied_time_when +
  1233. dirtytime_expire_interval * HZ)))) {
  1234. dirty |= I_DIRTY_TIME | I_DIRTY_TIME_EXPIRED;
  1235. trace_writeback_lazytime(inode);
  1236. }
  1237. } else
  1238. inode->i_state &= ~I_DIRTY_TIME_EXPIRED;
  1239. inode->i_state &= ~dirty;
  1240. /*
  1241. * Paired with smp_mb() in __mark_inode_dirty(). This allows
  1242. * __mark_inode_dirty() to test i_state without grabbing i_lock -
  1243. * either they see the I_DIRTY bits cleared or we see the dirtied
  1244. * inode.
  1245. *
  1246. * I_DIRTY_PAGES is always cleared together above even if @mapping
  1247. * still has dirty pages. The flag is reinstated after smp_mb() if
  1248. * necessary. This guarantees that either __mark_inode_dirty()
  1249. * sees clear I_DIRTY_PAGES or we see PAGECACHE_TAG_DIRTY.
  1250. */
  1251. smp_mb();
  1252. if (mapping_tagged(mapping, PAGECACHE_TAG_DIRTY))
  1253. inode->i_state |= I_DIRTY_PAGES;
  1254. spin_unlock(&inode->i_lock);
  1255. if (dirty & I_DIRTY_TIME)
  1256. mark_inode_dirty_sync(inode);
  1257. /* Don't write the inode if only I_DIRTY_PAGES was set */
  1258. if (dirty & ~I_DIRTY_PAGES) {
  1259. int err = write_inode(inode, wbc);
  1260. if (ret == 0)
  1261. ret = err;
  1262. }
  1263. trace_writeback_single_inode(inode, wbc, nr_to_write);
  1264. return ret;
  1265. }
  1266. /*
  1267. * Write out an inode's dirty pages. Either the caller has an active reference
  1268. * on the inode or the inode has I_WILL_FREE set.
  1269. *
  1270. * This function is designed to be called for writing back one inode which
  1271. * we go e.g. from filesystem. Flusher thread uses __writeback_single_inode()
  1272. * and does more profound writeback list handling in writeback_sb_inodes().
  1273. */
  1274. static int writeback_single_inode(struct inode *inode,
  1275. struct writeback_control *wbc)
  1276. {
  1277. struct bdi_writeback *wb;
  1278. int ret = 0;
  1279. spin_lock(&inode->i_lock);
  1280. if (!atomic_read(&inode->i_count))
  1281. WARN_ON(!(inode->i_state & (I_WILL_FREE|I_FREEING)));
  1282. else
  1283. WARN_ON(inode->i_state & I_WILL_FREE);
  1284. if (inode->i_state & I_SYNC) {
  1285. if (wbc->sync_mode != WB_SYNC_ALL)
  1286. goto out;
  1287. /*
  1288. * It's a data-integrity sync. We must wait. Since callers hold
  1289. * inode reference or inode has I_WILL_FREE set, it cannot go
  1290. * away under us.
  1291. */
  1292. __inode_wait_for_writeback(inode);
  1293. }
  1294. WARN_ON(inode->i_state & I_SYNC);
  1295. /*
  1296. * Skip inode if it is clean and we have no outstanding writeback in
  1297. * WB_SYNC_ALL mode. We don't want to mess with writeback lists in this
  1298. * function since flusher thread may be doing for example sync in
  1299. * parallel and if we move the inode, it could get skipped. So here we
  1300. * make sure inode is on some writeback list and leave it there unless
  1301. * we have completely cleaned the inode.
  1302. */
  1303. if (!(inode->i_state & I_DIRTY_ALL) &&
  1304. (wbc->sync_mode != WB_SYNC_ALL ||
  1305. !mapping_tagged(inode->i_mapping, PAGECACHE_TAG_WRITEBACK)))
  1306. goto out;
  1307. inode->i_state |= I_SYNC;
  1308. wbc_attach_and_unlock_inode(wbc, inode);
  1309. ret = __writeback_single_inode(inode, wbc);
  1310. wbc_detach_inode(wbc);
  1311. wb = inode_to_wb_and_lock_list(inode);
  1312. spin_lock(&inode->i_lock);
  1313. /*
  1314. * If inode is clean, remove it from writeback lists. Otherwise don't
  1315. * touch it. See comment above for explanation.
  1316. */
  1317. if (!(inode->i_state & I_DIRTY_ALL))
  1318. inode_io_list_del_locked(inode, wb);
  1319. spin_unlock(&wb->list_lock);
  1320. inode_sync_complete(inode);
  1321. out:
  1322. spin_unlock(&inode->i_lock);
  1323. return ret;
  1324. }
  1325. static long writeback_chunk_size(struct bdi_writeback *wb,
  1326. struct wb_writeback_work *work)
  1327. {
  1328. long pages;
  1329. /*
  1330. * WB_SYNC_ALL mode does livelock avoidance by syncing dirty
  1331. * inodes/pages in one big loop. Setting wbc.nr_to_write=LONG_MAX
  1332. * here avoids calling into writeback_inodes_wb() more than once.
  1333. *
  1334. * The intended call sequence for WB_SYNC_ALL writeback is:
  1335. *
  1336. * wb_writeback()
  1337. * writeback_sb_inodes() <== called only once
  1338. * write_cache_pages() <== called once for each inode
  1339. * (quickly) tag currently dirty pages
  1340. * (maybe slowly) sync all tagged pages
  1341. */
  1342. if (work->sync_mode == WB_SYNC_ALL || work->tagged_writepages)
  1343. pages = LONG_MAX;
  1344. else {
  1345. pages = min(wb->avg_write_bandwidth / 2,
  1346. global_wb_domain.dirty_limit / DIRTY_SCOPE);
  1347. pages = min(pages, work->nr_pages);
  1348. pages = round_down(pages + MIN_WRITEBACK_PAGES,
  1349. MIN_WRITEBACK_PAGES);
  1350. }
  1351. return pages;
  1352. }
  1353. /*
  1354. * Write a portion of b_io inodes which belong to @sb.
  1355. *
  1356. * Return the number of pages and/or inodes written.
  1357. *
  1358. * NOTE! This is called with wb->list_lock held, and will
  1359. * unlock and relock that for each inode it ends up doing
  1360. * IO for.
  1361. */
  1362. static long writeback_sb_inodes(struct super_block *sb,
  1363. struct bdi_writeback *wb,
  1364. struct wb_writeback_work *work)
  1365. {
  1366. struct writeback_control wbc = {
  1367. .sync_mode = work->sync_mode,
  1368. .tagged_writepages = work->tagged_writepages,
  1369. .for_kupdate = work->for_kupdate,
  1370. .for_background = work->for_background,
  1371. .for_sync = work->for_sync,
  1372. .range_cyclic = work->range_cyclic,
  1373. .range_start = 0,
  1374. .range_end = LLONG_MAX,
  1375. };
  1376. unsigned long start_time = jiffies;
  1377. long write_chunk;
  1378. long wrote = 0; /* count both pages and inodes */
  1379. while (!list_empty(&wb->b_io)) {
  1380. struct inode *inode = wb_inode(wb->b_io.prev);
  1381. struct bdi_writeback *tmp_wb;
  1382. if (inode->i_sb != sb) {
  1383. if (work->sb) {
  1384. /*
  1385. * We only want to write back data for this
  1386. * superblock, move all inodes not belonging
  1387. * to it back onto the dirty list.
  1388. */
  1389. redirty_tail(inode, wb);
  1390. continue;
  1391. }
  1392. /*
  1393. * The inode belongs to a different superblock.
  1394. * Bounce back to the caller to unpin this and
  1395. * pin the next superblock.
  1396. */
  1397. break;
  1398. }
  1399. /*
  1400. * Don't bother with new inodes or inodes being freed, first
  1401. * kind does not need periodic writeout yet, and for the latter
  1402. * kind writeout is handled by the freer.
  1403. */
  1404. spin_lock(&inode->i_lock);
  1405. if (inode->i_state & (I_NEW | I_FREEING | I_WILL_FREE)) {
  1406. spin_unlock(&inode->i_lock);
  1407. redirty_tail(inode, wb);
  1408. continue;
  1409. }
  1410. if ((inode->i_state & I_SYNC) && wbc.sync_mode != WB_SYNC_ALL) {
  1411. /*
  1412. * If this inode is locked for writeback and we are not
  1413. * doing writeback-for-data-integrity, move it to
  1414. * b_more_io so that writeback can proceed with the
  1415. * other inodes on s_io.
  1416. *
  1417. * We'll have another go at writing back this inode
  1418. * when we completed a full scan of b_io.
  1419. */
  1420. spin_unlock(&inode->i_lock);
  1421. requeue_io(inode, wb);
  1422. trace_writeback_sb_inodes_requeue(inode);
  1423. continue;
  1424. }
  1425. spin_unlock(&wb->list_lock);
  1426. /*
  1427. * We already requeued the inode if it had I_SYNC set and we
  1428. * are doing WB_SYNC_NONE writeback. So this catches only the
  1429. * WB_SYNC_ALL case.
  1430. */
  1431. if (inode->i_state & I_SYNC) {
  1432. /* Wait for I_SYNC. This function drops i_lock... */
  1433. inode_sleep_on_writeback(inode);
  1434. /* Inode may be gone, start again */
  1435. spin_lock(&wb->list_lock);
  1436. continue;
  1437. }
  1438. inode->i_state |= I_SYNC;
  1439. wbc_attach_and_unlock_inode(&wbc, inode);
  1440. write_chunk = writeback_chunk_size(wb, work);
  1441. wbc.nr_to_write = write_chunk;
  1442. wbc.pages_skipped = 0;
  1443. /*
  1444. * We use I_SYNC to pin the inode in memory. While it is set
  1445. * evict_inode() will wait so the inode cannot be freed.
  1446. */
  1447. __writeback_single_inode(inode, &wbc);
  1448. wbc_detach_inode(&wbc);
  1449. work->nr_pages -= write_chunk - wbc.nr_to_write;
  1450. wrote += write_chunk - wbc.nr_to_write;
  1451. if (need_resched()) {
  1452. /*
  1453. * We're trying to balance between building up a nice
  1454. * long list of IOs to improve our merge rate, and
  1455. * getting those IOs out quickly for anyone throttling
  1456. * in balance_dirty_pages(). cond_resched() doesn't
  1457. * unplug, so get our IOs out the door before we
  1458. * give up the CPU.
  1459. */
  1460. blk_flush_plug(current);
  1461. cond_resched();
  1462. }
  1463. /*
  1464. * Requeue @inode if still dirty. Be careful as @inode may
  1465. * have been switched to another wb in the meantime.
  1466. */
  1467. tmp_wb = inode_to_wb_and_lock_list(inode);
  1468. spin_lock(&inode->i_lock);
  1469. if (!(inode->i_state & I_DIRTY_ALL))
  1470. wrote++;
  1471. requeue_inode(inode, tmp_wb, &wbc);
  1472. inode_sync_complete(inode);
  1473. spin_unlock(&inode->i_lock);
  1474. if (unlikely(tmp_wb != wb)) {
  1475. spin_unlock(&tmp_wb->list_lock);
  1476. spin_lock(&wb->list_lock);
  1477. }
  1478. /*
  1479. * bail out to wb_writeback() often enough to check
  1480. * background threshold and other termination conditions.
  1481. */
  1482. if (wrote) {
  1483. if (time_is_before_jiffies(start_time + HZ / 10UL))
  1484. break;
  1485. if (work->nr_pages <= 0)
  1486. break;
  1487. }
  1488. }
  1489. return wrote;
  1490. }
  1491. static long __writeback_inodes_wb(struct bdi_writeback *wb,
  1492. struct wb_writeback_work *work)
  1493. {
  1494. unsigned long start_time = jiffies;
  1495. long wrote = 0;
  1496. while (!list_empty(&wb->b_io)) {
  1497. struct inode *inode = wb_inode(wb->b_io.prev);
  1498. struct super_block *sb = inode->i_sb;
  1499. if (!trylock_super(sb)) {
  1500. /*
  1501. * trylock_super() may fail consistently due to
  1502. * s_umount being grabbed by someone else. Don't use
  1503. * requeue_io() to avoid busy retrying the inode/sb.
  1504. */
  1505. redirty_tail(inode, wb);
  1506. continue;
  1507. }
  1508. wrote += writeback_sb_inodes(sb, wb, work);
  1509. up_read(&sb->s_umount);
  1510. /* refer to the same tests at the end of writeback_sb_inodes */
  1511. if (wrote) {
  1512. if (time_is_before_jiffies(start_time + HZ / 10UL))
  1513. break;
  1514. if (work->nr_pages <= 0)
  1515. break;
  1516. }
  1517. }
  1518. /* Leave any unwritten inodes on b_io */
  1519. return wrote;
  1520. }
  1521. static long writeback_inodes_wb(struct bdi_writeback *wb, long nr_pages,
  1522. enum wb_reason reason)
  1523. {
  1524. struct wb_writeback_work work = {
  1525. .nr_pages = nr_pages,
  1526. .sync_mode = WB_SYNC_NONE,
  1527. .range_cyclic = 1,
  1528. .reason = reason,
  1529. };
  1530. struct blk_plug plug;
  1531. blk_start_plug(&plug);
  1532. spin_lock(&wb->list_lock);
  1533. if (list_empty(&wb->b_io))
  1534. queue_io(wb, &work);
  1535. __writeback_inodes_wb(wb, &work);
  1536. spin_unlock(&wb->list_lock);
  1537. blk_finish_plug(&plug);
  1538. return nr_pages - work.nr_pages;
  1539. }
  1540. /*
  1541. * Explicit flushing or periodic writeback of "old" data.
  1542. *
  1543. * Define "old": the first time one of an inode's pages is dirtied, we mark the
  1544. * dirtying-time in the inode's address_space. So this periodic writeback code
  1545. * just walks the superblock inode list, writing back any inodes which are
  1546. * older than a specific point in time.
  1547. *
  1548. * Try to run once per dirty_writeback_interval. But if a writeback event
  1549. * takes longer than a dirty_writeback_interval interval, then leave a
  1550. * one-second gap.
  1551. *
  1552. * older_than_this takes precedence over nr_to_write. So we'll only write back
  1553. * all dirty pages if they are all attached to "old" mappings.
  1554. */
  1555. static long wb_writeback(struct bdi_writeback *wb,
  1556. struct wb_writeback_work *work)
  1557. {
  1558. unsigned long wb_start = jiffies;
  1559. long nr_pages = work->nr_pages;
  1560. unsigned long oldest_jif;
  1561. struct inode *inode;
  1562. long progress;
  1563. struct blk_plug plug;
  1564. oldest_jif = jiffies;
  1565. work->older_than_this = &oldest_jif;
  1566. blk_start_plug(&plug);
  1567. spin_lock(&wb->list_lock);
  1568. for (;;) {
  1569. /*
  1570. * Stop writeback when nr_pages has been consumed
  1571. */
  1572. if (work->nr_pages <= 0)
  1573. break;
  1574. /*
  1575. * Background writeout and kupdate-style writeback may
  1576. * run forever. Stop them if there is other work to do
  1577. * so that e.g. sync can proceed. They'll be restarted
  1578. * after the other works are all done.
  1579. */
  1580. if ((work->for_background || work->for_kupdate) &&
  1581. !list_empty(&wb->work_list))
  1582. break;
  1583. /*
  1584. * For background writeout, stop when we are below the
  1585. * background dirty threshold
  1586. */
  1587. if (work->for_background && !wb_over_bg_thresh(wb))
  1588. break;
  1589. /*
  1590. * Kupdate and background works are special and we want to
  1591. * include all inodes that need writing. Livelock avoidance is
  1592. * handled by these works yielding to any other work so we are
  1593. * safe.
  1594. */
  1595. if (work->for_kupdate) {
  1596. oldest_jif = jiffies -
  1597. msecs_to_jiffies(dirty_expire_interval * 10);
  1598. } else if (work->for_background)
  1599. oldest_jif = jiffies;
  1600. trace_writeback_start(wb, work);
  1601. if (list_empty(&wb->b_io))
  1602. queue_io(wb, work);
  1603. if (work->sb)
  1604. progress = writeback_sb_inodes(work->sb, wb, work);
  1605. else
  1606. progress = __writeback_inodes_wb(wb, work);
  1607. trace_writeback_written(wb, work);
  1608. wb_update_bandwidth(wb, wb_start);
  1609. /*
  1610. * Did we write something? Try for more
  1611. *
  1612. * Dirty inodes are moved to b_io for writeback in batches.
  1613. * The completion of the current batch does not necessarily
  1614. * mean the overall work is done. So we keep looping as long
  1615. * as made some progress on cleaning pages or inodes.
  1616. */
  1617. if (progress)
  1618. continue;
  1619. /*
  1620. * No more inodes for IO, bail
  1621. */
  1622. if (list_empty(&wb->b_more_io))
  1623. break;
  1624. /*
  1625. * Nothing written. Wait for some inode to
  1626. * become available for writeback. Otherwise
  1627. * we'll just busyloop.
  1628. */
  1629. trace_writeback_wait(wb, work);
  1630. inode = wb_inode(wb->b_more_io.prev);
  1631. spin_lock(&inode->i_lock);
  1632. spin_unlock(&wb->list_lock);
  1633. /* This function drops i_lock... */
  1634. inode_sleep_on_writeback(inode);
  1635. spin_lock(&wb->list_lock);
  1636. }
  1637. spin_unlock(&wb->list_lock);
  1638. blk_finish_plug(&plug);
  1639. return nr_pages - work->nr_pages;
  1640. }
  1641. /*
  1642. * Return the next wb_writeback_work struct that hasn't been processed yet.
  1643. */
  1644. static struct wb_writeback_work *get_next_work_item(struct bdi_writeback *wb)
  1645. {
  1646. struct wb_writeback_work *work = NULL;
  1647. spin_lock_bh(&wb->work_lock);
  1648. if (!list_empty(&wb->work_list)) {
  1649. work = list_entry(wb->work_list.next,
  1650. struct wb_writeback_work, list);
  1651. list_del_init(&work->list);
  1652. }
  1653. spin_unlock_bh(&wb->work_lock);
  1654. return work;
  1655. }
  1656. static long wb_check_background_flush(struct bdi_writeback *wb)
  1657. {
  1658. if (wb_over_bg_thresh(wb)) {
  1659. struct wb_writeback_work work = {
  1660. .nr_pages = LONG_MAX,
  1661. .sync_mode = WB_SYNC_NONE,
  1662. .for_background = 1,
  1663. .range_cyclic = 1,
  1664. .reason = WB_REASON_BACKGROUND,
  1665. };
  1666. return wb_writeback(wb, &work);
  1667. }
  1668. return 0;
  1669. }
  1670. static long wb_check_old_data_flush(struct bdi_writeback *wb)
  1671. {
  1672. unsigned long expired;
  1673. long nr_pages;
  1674. /*
  1675. * When set to zero, disable periodic writeback
  1676. */
  1677. if (!dirty_writeback_interval)
  1678. return 0;
  1679. expired = wb->last_old_flush +
  1680. msecs_to_jiffies(dirty_writeback_interval * 10);
  1681. if (time_before(jiffies, expired))
  1682. return 0;
  1683. wb->last_old_flush = jiffies;
  1684. nr_pages = get_nr_dirty_pages();
  1685. if (nr_pages) {
  1686. struct wb_writeback_work work = {
  1687. .nr_pages = nr_pages,
  1688. .sync_mode = WB_SYNC_NONE,
  1689. .for_kupdate = 1,
  1690. .range_cyclic = 1,
  1691. .reason = WB_REASON_PERIODIC,
  1692. };
  1693. return wb_writeback(wb, &work);
  1694. }
  1695. return 0;
  1696. }
  1697. static long wb_check_start_all(struct bdi_writeback *wb)
  1698. {
  1699. long nr_pages;
  1700. if (!test_bit(WB_start_all, &wb->state))
  1701. return 0;
  1702. nr_pages = get_nr_dirty_pages();
  1703. if (nr_pages) {
  1704. struct wb_writeback_work work = {
  1705. .nr_pages = wb_split_bdi_pages(wb, nr_pages),
  1706. .sync_mode = WB_SYNC_NONE,
  1707. .range_cyclic = 1,
  1708. .reason = wb->start_all_reason,
  1709. };
  1710. nr_pages = wb_writeback(wb, &work);
  1711. }
  1712. clear_bit(WB_start_all, &wb->state);
  1713. return nr_pages;
  1714. }
  1715. /*
  1716. * Retrieve work items and do the writeback they describe
  1717. */
  1718. static long wb_do_writeback(struct bdi_writeback *wb)
  1719. {
  1720. struct wb_writeback_work *work;
  1721. long wrote = 0;
  1722. set_bit(WB_writeback_running, &wb->state);
  1723. while ((work = get_next_work_item(wb)) != NULL) {
  1724. trace_writeback_exec(wb, work);
  1725. wrote += wb_writeback(wb, work);
  1726. finish_writeback_work(wb, work);
  1727. }
  1728. /*
  1729. * Check for a flush-everything request
  1730. */
  1731. wrote += wb_check_start_all(wb);
  1732. /*
  1733. * Check for periodic writeback, kupdated() style
  1734. */
  1735. wrote += wb_check_old_data_flush(wb);
  1736. wrote += wb_check_background_flush(wb);
  1737. clear_bit(WB_writeback_running, &wb->state);
  1738. return wrote;
  1739. }
  1740. /*
  1741. * Handle writeback of dirty data for the device backed by this bdi. Also
  1742. * reschedules periodically and does kupdated style flushing.
  1743. */
  1744. void wb_workfn(struct work_struct *work)
  1745. {
  1746. struct bdi_writeback *wb = container_of(to_delayed_work(work),
  1747. struct bdi_writeback, dwork);
  1748. long pages_written;
  1749. set_worker_desc("flush-%s", dev_name(wb->bdi->dev));
  1750. current->flags |= PF_SWAPWRITE;
  1751. if (likely(!current_is_workqueue_rescuer() ||
  1752. !test_bit(WB_registered, &wb->state))) {
  1753. /*
  1754. * The normal path. Keep writing back @wb until its
  1755. * work_list is empty. Note that this path is also taken
  1756. * if @wb is shutting down even when we're running off the
  1757. * rescuer as work_list needs to be drained.
  1758. */
  1759. do {
  1760. pages_written = wb_do_writeback(wb);
  1761. trace_writeback_pages_written(pages_written);
  1762. } while (!list_empty(&wb->work_list));
  1763. } else {
  1764. /*
  1765. * bdi_wq can't get enough workers and we're running off
  1766. * the emergency worker. Don't hog it. Hopefully, 1024 is
  1767. * enough for efficient IO.
  1768. */
  1769. pages_written = writeback_inodes_wb(wb, 1024,
  1770. WB_REASON_FORKER_THREAD);
  1771. trace_writeback_pages_written(pages_written);
  1772. }
  1773. if (!list_empty(&wb->work_list))
  1774. wb_wakeup(wb);
  1775. else if (wb_has_dirty_io(wb) && dirty_writeback_interval)
  1776. wb_wakeup_delayed(wb);
  1777. current->flags &= ~PF_SWAPWRITE;
  1778. }
  1779. /*
  1780. * Start writeback of `nr_pages' pages on this bdi. If `nr_pages' is zero,
  1781. * write back the whole world.
  1782. */
  1783. static void __wakeup_flusher_threads_bdi(struct backing_dev_info *bdi,
  1784. enum wb_reason reason)
  1785. {
  1786. struct bdi_writeback *wb;
  1787. if (!bdi_has_dirty_io(bdi))
  1788. return;
  1789. list_for_each_entry_rcu(wb, &bdi->wb_list, bdi_node)
  1790. wb_start_writeback(wb, reason);
  1791. }
  1792. void wakeup_flusher_threads_bdi(struct backing_dev_info *bdi,
  1793. enum wb_reason reason)
  1794. {
  1795. rcu_read_lock();
  1796. __wakeup_flusher_threads_bdi(bdi, reason);
  1797. rcu_read_unlock();
  1798. }
  1799. /*
  1800. * Wakeup the flusher threads to start writeback of all currently dirty pages
  1801. */
  1802. void wakeup_flusher_threads(enum wb_reason reason)
  1803. {
  1804. struct backing_dev_info *bdi;
  1805. /*
  1806. * If we are expecting writeback progress we must submit plugged IO.
  1807. */
  1808. if (blk_needs_flush_plug(current))
  1809. blk_schedule_flush_plug(current);
  1810. rcu_read_lock();
  1811. list_for_each_entry_rcu(bdi, &bdi_list, bdi_list)
  1812. __wakeup_flusher_threads_bdi(bdi, reason);
  1813. rcu_read_unlock();
  1814. }
  1815. /*
  1816. * Wake up bdi's periodically to make sure dirtytime inodes gets
  1817. * written back periodically. We deliberately do *not* check the
  1818. * b_dirtytime list in wb_has_dirty_io(), since this would cause the
  1819. * kernel to be constantly waking up once there are any dirtytime
  1820. * inodes on the system. So instead we define a separate delayed work
  1821. * function which gets called much more rarely. (By default, only
  1822. * once every 12 hours.)
  1823. *
  1824. * If there is any other write activity going on in the file system,
  1825. * this function won't be necessary. But if the only thing that has
  1826. * happened on the file system is a dirtytime inode caused by an atime
  1827. * update, we need this infrastructure below to make sure that inode
  1828. * eventually gets pushed out to disk.
  1829. */
  1830. static void wakeup_dirtytime_writeback(struct work_struct *w);
  1831. static DECLARE_DELAYED_WORK(dirtytime_work, wakeup_dirtytime_writeback);
  1832. static void wakeup_dirtytime_writeback(struct work_struct *w)
  1833. {
  1834. struct backing_dev_info *bdi;
  1835. rcu_read_lock();
  1836. list_for_each_entry_rcu(bdi, &bdi_list, bdi_list) {
  1837. struct bdi_writeback *wb;
  1838. list_for_each_entry_rcu(wb, &bdi->wb_list, bdi_node)
  1839. if (!list_empty(&wb->b_dirty_time))
  1840. wb_wakeup(wb);
  1841. }
  1842. rcu_read_unlock();
  1843. schedule_delayed_work(&dirtytime_work, dirtytime_expire_interval * HZ);
  1844. }
  1845. static int __init start_dirtytime_writeback(void)
  1846. {
  1847. schedule_delayed_work(&dirtytime_work, dirtytime_expire_interval * HZ);
  1848. return 0;
  1849. }
  1850. __initcall(start_dirtytime_writeback);
  1851. int dirtytime_interval_handler(struct ctl_table *table, int write,
  1852. void __user *buffer, size_t *lenp, loff_t *ppos)
  1853. {
  1854. int ret;
  1855. ret = proc_dointvec_minmax(table, write, buffer, lenp, ppos);
  1856. if (ret == 0 && write)
  1857. mod_delayed_work(system_wq, &dirtytime_work, 0);
  1858. return ret;
  1859. }
  1860. static noinline void block_dump___mark_inode_dirty(struct inode *inode)
  1861. {
  1862. if (inode->i_ino || strcmp(inode->i_sb->s_id, "bdev")) {
  1863. struct dentry *dentry;
  1864. const char *name = "?";
  1865. dentry = d_find_alias(inode);
  1866. if (dentry) {
  1867. spin_lock(&dentry->d_lock);
  1868. name = (const char *) dentry->d_name.name;
  1869. }
  1870. printk(KERN_DEBUG
  1871. "%s(%d): dirtied inode %lu (%s) on %s\n",
  1872. current->comm, task_pid_nr(current), inode->i_ino,
  1873. name, inode->i_sb->s_id);
  1874. if (dentry) {
  1875. spin_unlock(&dentry->d_lock);
  1876. dput(dentry);
  1877. }
  1878. }
  1879. }
  1880. /**
  1881. * __mark_inode_dirty - internal function
  1882. *
  1883. * @inode: inode to mark
  1884. * @flags: what kind of dirty (i.e. I_DIRTY_SYNC)
  1885. *
  1886. * Mark an inode as dirty. Callers should use mark_inode_dirty or
  1887. * mark_inode_dirty_sync.
  1888. *
  1889. * Put the inode on the super block's dirty list.
  1890. *
  1891. * CAREFUL! We mark it dirty unconditionally, but move it onto the
  1892. * dirty list only if it is hashed or if it refers to a blockdev.
  1893. * If it was not hashed, it will never be added to the dirty list
  1894. * even if it is later hashed, as it will have been marked dirty already.
  1895. *
  1896. * In short, make sure you hash any inodes _before_ you start marking
  1897. * them dirty.
  1898. *
  1899. * Note that for blockdevs, inode->dirtied_when represents the dirtying time of
  1900. * the block-special inode (/dev/hda1) itself. And the ->dirtied_when field of
  1901. * the kernel-internal blockdev inode represents the dirtying time of the
  1902. * blockdev's pages. This is why for I_DIRTY_PAGES we always use
  1903. * page->mapping->host, so the page-dirtying time is recorded in the internal
  1904. * blockdev inode.
  1905. */
  1906. void __mark_inode_dirty(struct inode *inode, int flags)
  1907. {
  1908. struct super_block *sb = inode->i_sb;
  1909. int dirtytime;
  1910. trace_writeback_mark_inode_dirty(inode, flags);
  1911. /*
  1912. * Don't do this for I_DIRTY_PAGES - that doesn't actually
  1913. * dirty the inode itself
  1914. */
  1915. if (flags & (I_DIRTY_INODE | I_DIRTY_TIME)) {
  1916. trace_writeback_dirty_inode_start(inode, flags);
  1917. if (sb->s_op->dirty_inode)
  1918. sb->s_op->dirty_inode(inode, flags);
  1919. trace_writeback_dirty_inode(inode, flags);
  1920. }
  1921. if (flags & I_DIRTY_INODE)
  1922. flags &= ~I_DIRTY_TIME;
  1923. dirtytime = flags & I_DIRTY_TIME;
  1924. /*
  1925. * Paired with smp_mb() in __writeback_single_inode() for the
  1926. * following lockless i_state test. See there for details.
  1927. */
  1928. smp_mb();
  1929. if (((inode->i_state & flags) == flags) ||
  1930. (dirtytime && (inode->i_state & I_DIRTY_INODE)))
  1931. return;
  1932. if (unlikely(block_dump))
  1933. block_dump___mark_inode_dirty(inode);
  1934. spin_lock(&inode->i_lock);
  1935. if (dirtytime && (inode->i_state & I_DIRTY_INODE))
  1936. goto out_unlock_inode;
  1937. if ((inode->i_state & flags) != flags) {
  1938. const int was_dirty = inode->i_state & I_DIRTY;
  1939. inode_attach_wb(inode, NULL);
  1940. if (flags & I_DIRTY_INODE)
  1941. inode->i_state &= ~I_DIRTY_TIME;
  1942. inode->i_state |= flags;
  1943. /*
  1944. * If the inode is being synced, just update its dirty state.
  1945. * The unlocker will place the inode on the appropriate
  1946. * superblock list, based upon its state.
  1947. */
  1948. if (inode->i_state & I_SYNC)
  1949. goto out_unlock_inode;
  1950. /*
  1951. * Only add valid (hashed) inodes to the superblock's
  1952. * dirty list. Add blockdev inodes as well.
  1953. */
  1954. if (!S_ISBLK(inode->i_mode)) {
  1955. if (inode_unhashed(inode))
  1956. goto out_unlock_inode;
  1957. }
  1958. if (inode->i_state & I_FREEING)
  1959. goto out_unlock_inode;
  1960. /*
  1961. * If the inode was already on b_dirty/b_io/b_more_io, don't
  1962. * reposition it (that would break b_dirty time-ordering).
  1963. */
  1964. if (!was_dirty) {
  1965. struct bdi_writeback *wb;
  1966. struct list_head *dirty_list;
  1967. bool wakeup_bdi = false;
  1968. wb = locked_inode_to_wb_and_lock_list(inode);
  1969. WARN(bdi_cap_writeback_dirty(wb->bdi) &&
  1970. !test_bit(WB_registered, &wb->state),
  1971. "bdi-%s not registered\n", wb->bdi->name);
  1972. inode->dirtied_when = jiffies;
  1973. if (dirtytime)
  1974. inode->dirtied_time_when = jiffies;
  1975. if (inode->i_state & I_DIRTY)
  1976. dirty_list = &wb->b_dirty;
  1977. else
  1978. dirty_list = &wb->b_dirty_time;
  1979. wakeup_bdi = inode_io_list_move_locked(inode, wb,
  1980. dirty_list);
  1981. spin_unlock(&wb->list_lock);
  1982. trace_writeback_dirty_inode_enqueue(inode);
  1983. /*
  1984. * If this is the first dirty inode for this bdi,
  1985. * we have to wake-up the corresponding bdi thread
  1986. * to make sure background write-back happens
  1987. * later.
  1988. */
  1989. if (bdi_cap_writeback_dirty(wb->bdi) && wakeup_bdi)
  1990. wb_wakeup_delayed(wb);
  1991. return;
  1992. }
  1993. }
  1994. out_unlock_inode:
  1995. spin_unlock(&inode->i_lock);
  1996. }
  1997. EXPORT_SYMBOL(__mark_inode_dirty);
  1998. /*
  1999. * The @s_sync_lock is used to serialise concurrent sync operations
  2000. * to avoid lock contention problems with concurrent wait_sb_inodes() calls.
  2001. * Concurrent callers will block on the s_sync_lock rather than doing contending
  2002. * walks. The queueing maintains sync(2) required behaviour as all the IO that
  2003. * has been issued up to the time this function is enter is guaranteed to be
  2004. * completed by the time we have gained the lock and waited for all IO that is
  2005. * in progress regardless of the order callers are granted the lock.
  2006. */
  2007. static void wait_sb_inodes(struct super_block *sb)
  2008. {
  2009. LIST_HEAD(sync_list);
  2010. /*
  2011. * We need to be protected against the filesystem going from
  2012. * r/o to r/w or vice versa.
  2013. */
  2014. WARN_ON(!rwsem_is_locked(&sb->s_umount));
  2015. mutex_lock(&sb->s_sync_lock);
  2016. /*
  2017. * Splice the writeback list onto a temporary list to avoid waiting on
  2018. * inodes that have started writeback after this point.
  2019. *
  2020. * Use rcu_read_lock() to keep the inodes around until we have a
  2021. * reference. s_inode_wblist_lock protects sb->s_inodes_wb as well as
  2022. * the local list because inodes can be dropped from either by writeback
  2023. * completion.
  2024. */
  2025. rcu_read_lock();
  2026. spin_lock_irq(&sb->s_inode_wblist_lock);
  2027. list_splice_init(&sb->s_inodes_wb, &sync_list);
  2028. /*
  2029. * Data integrity sync. Must wait for all pages under writeback, because
  2030. * there may have been pages dirtied before our sync call, but which had
  2031. * writeout started before we write it out. In which case, the inode
  2032. * may not be on the dirty list, but we still have to wait for that
  2033. * writeout.
  2034. */
  2035. while (!list_empty(&sync_list)) {
  2036. struct inode *inode = list_first_entry(&sync_list, struct inode,
  2037. i_wb_list);
  2038. struct address_space *mapping = inode->i_mapping;
  2039. /*
  2040. * Move each inode back to the wb list before we drop the lock
  2041. * to preserve consistency between i_wb_list and the mapping
  2042. * writeback tag. Writeback completion is responsible to remove
  2043. * the inode from either list once the writeback tag is cleared.
  2044. */
  2045. list_move_tail(&inode->i_wb_list, &sb->s_inodes_wb);
  2046. /*
  2047. * The mapping can appear untagged while still on-list since we
  2048. * do not have the mapping lock. Skip it here, wb completion
  2049. * will remove it.
  2050. */
  2051. if (!mapping_tagged(mapping, PAGECACHE_TAG_WRITEBACK))
  2052. continue;
  2053. spin_unlock_irq(&sb->s_inode_wblist_lock);
  2054. spin_lock(&inode->i_lock);
  2055. if (inode->i_state & (I_FREEING|I_WILL_FREE|I_NEW)) {
  2056. spin_unlock(&inode->i_lock);
  2057. spin_lock_irq(&sb->s_inode_wblist_lock);
  2058. continue;
  2059. }
  2060. __iget(inode);
  2061. spin_unlock(&inode->i_lock);
  2062. rcu_read_unlock();
  2063. /*
  2064. * We keep the error status of individual mapping so that
  2065. * applications can catch the writeback error using fsync(2).
  2066. * See filemap_fdatawait_keep_errors() for details.
  2067. */
  2068. filemap_fdatawait_keep_errors(mapping);
  2069. cond_resched();
  2070. iput(inode);
  2071. rcu_read_lock();
  2072. spin_lock_irq(&sb->s_inode_wblist_lock);
  2073. }
  2074. spin_unlock_irq(&sb->s_inode_wblist_lock);
  2075. rcu_read_unlock();
  2076. mutex_unlock(&sb->s_sync_lock);
  2077. }
  2078. static void __writeback_inodes_sb_nr(struct super_block *sb, unsigned long nr,
  2079. enum wb_reason reason, bool skip_if_busy)
  2080. {
  2081. DEFINE_WB_COMPLETION_ONSTACK(done);
  2082. struct wb_writeback_work work = {
  2083. .sb = sb,
  2084. .sync_mode = WB_SYNC_NONE,
  2085. .tagged_writepages = 1,
  2086. .done = &done,
  2087. .nr_pages = nr,
  2088. .reason = reason,
  2089. };
  2090. struct backing_dev_info *bdi = sb->s_bdi;
  2091. if (!bdi_has_dirty_io(bdi) || bdi == &noop_backing_dev_info)
  2092. return;
  2093. WARN_ON(!rwsem_is_locked(&sb->s_umount));
  2094. bdi_split_work_to_wbs(sb->s_bdi, &work, skip_if_busy);
  2095. wb_wait_for_completion(bdi, &done);
  2096. }
  2097. /**
  2098. * writeback_inodes_sb_nr - writeback dirty inodes from given super_block
  2099. * @sb: the superblock
  2100. * @nr: the number of pages to write
  2101. * @reason: reason why some writeback work initiated
  2102. *
  2103. * Start writeback on some inodes on this super_block. No guarantees are made
  2104. * on how many (if any) will be written, and this function does not wait
  2105. * for IO completion of submitted IO.
  2106. */
  2107. void writeback_inodes_sb_nr(struct super_block *sb,
  2108. unsigned long nr,
  2109. enum wb_reason reason)
  2110. {
  2111. __writeback_inodes_sb_nr(sb, nr, reason, false);
  2112. }
  2113. EXPORT_SYMBOL(writeback_inodes_sb_nr);
  2114. /**
  2115. * writeback_inodes_sb - writeback dirty inodes from given super_block
  2116. * @sb: the superblock
  2117. * @reason: reason why some writeback work was initiated
  2118. *
  2119. * Start writeback on some inodes on this super_block. No guarantees are made
  2120. * on how many (if any) will be written, and this function does not wait
  2121. * for IO completion of submitted IO.
  2122. */
  2123. void writeback_inodes_sb(struct super_block *sb, enum wb_reason reason)
  2124. {
  2125. return writeback_inodes_sb_nr(sb, get_nr_dirty_pages(), reason);
  2126. }
  2127. EXPORT_SYMBOL(writeback_inodes_sb);
  2128. /**
  2129. * try_to_writeback_inodes_sb - try to start writeback if none underway
  2130. * @sb: the superblock
  2131. * @reason: reason why some writeback work was initiated
  2132. *
  2133. * Invoke __writeback_inodes_sb_nr if no writeback is currently underway.
  2134. */
  2135. void try_to_writeback_inodes_sb(struct super_block *sb, enum wb_reason reason)
  2136. {
  2137. if (!down_read_trylock(&sb->s_umount))
  2138. return;
  2139. __writeback_inodes_sb_nr(sb, get_nr_dirty_pages(), reason, true);
  2140. up_read(&sb->s_umount);
  2141. }
  2142. EXPORT_SYMBOL(try_to_writeback_inodes_sb);
  2143. /**
  2144. * sync_inodes_sb - sync sb inode pages
  2145. * @sb: the superblock
  2146. *
  2147. * This function writes and waits on any dirty inode belonging to this
  2148. * super_block.
  2149. */
  2150. void sync_inodes_sb(struct super_block *sb)
  2151. {
  2152. DEFINE_WB_COMPLETION_ONSTACK(done);
  2153. struct wb_writeback_work work = {
  2154. .sb = sb,
  2155. .sync_mode = WB_SYNC_ALL,
  2156. .nr_pages = LONG_MAX,
  2157. .range_cyclic = 0,
  2158. .done = &done,
  2159. .reason = WB_REASON_SYNC,
  2160. .for_sync = 1,
  2161. };
  2162. struct backing_dev_info *bdi = sb->s_bdi;
  2163. /*
  2164. * Can't skip on !bdi_has_dirty() because we should wait for !dirty
  2165. * inodes under writeback and I_DIRTY_TIME inodes ignored by
  2166. * bdi_has_dirty() need to be written out too.
  2167. */
  2168. if (bdi == &noop_backing_dev_info)
  2169. return;
  2170. WARN_ON(!rwsem_is_locked(&sb->s_umount));
  2171. /* protect against inode wb switch, see inode_switch_wbs_work_fn() */
  2172. bdi_down_write_wb_switch_rwsem(bdi);
  2173. bdi_split_work_to_wbs(bdi, &work, false);
  2174. wb_wait_for_completion(bdi, &done);
  2175. bdi_up_write_wb_switch_rwsem(bdi);
  2176. wait_sb_inodes(sb);
  2177. }
  2178. EXPORT_SYMBOL(sync_inodes_sb);
  2179. /**
  2180. * write_inode_now - write an inode to disk
  2181. * @inode: inode to write to disk
  2182. * @sync: whether the write should be synchronous or not
  2183. *
  2184. * This function commits an inode to disk immediately if it is dirty. This is
  2185. * primarily needed by knfsd.
  2186. *
  2187. * The caller must either have a ref on the inode or must have set I_WILL_FREE.
  2188. */
  2189. int write_inode_now(struct inode *inode, int sync)
  2190. {
  2191. struct writeback_control wbc = {
  2192. .nr_to_write = LONG_MAX,
  2193. .sync_mode = sync ? WB_SYNC_ALL : WB_SYNC_NONE,
  2194. .range_start = 0,
  2195. .range_end = LLONG_MAX,
  2196. };
  2197. if (!mapping_cap_writeback_dirty(inode->i_mapping))
  2198. wbc.nr_to_write = 0;
  2199. might_sleep();
  2200. return writeback_single_inode(inode, &wbc);
  2201. }
  2202. EXPORT_SYMBOL(write_inode_now);
  2203. /**
  2204. * sync_inode - write an inode and its pages to disk.
  2205. * @inode: the inode to sync
  2206. * @wbc: controls the writeback mode
  2207. *
  2208. * sync_inode() will write an inode and its pages to disk. It will also
  2209. * correctly update the inode on its superblock's dirty inode lists and will
  2210. * update inode->i_state.
  2211. *
  2212. * The caller must have a ref on the inode.
  2213. */
  2214. int sync_inode(struct inode *inode, struct writeback_control *wbc)
  2215. {
  2216. return writeback_single_inode(inode, wbc);
  2217. }
  2218. EXPORT_SYMBOL(sync_inode);
  2219. /**
  2220. * sync_inode_metadata - write an inode to disk
  2221. * @inode: the inode to sync
  2222. * @wait: wait for I/O to complete.
  2223. *
  2224. * Write an inode to disk and adjust its dirty state after completion.
  2225. *
  2226. * Note: only writes the actual inode, no associated data or other metadata.
  2227. */
  2228. int sync_inode_metadata(struct inode *inode, int wait)
  2229. {
  2230. struct writeback_control wbc = {
  2231. .sync_mode = wait ? WB_SYNC_ALL : WB_SYNC_NONE,
  2232. .nr_to_write = 0, /* metadata-only */
  2233. };
  2234. return sync_inode(inode, &wbc);
  2235. }
  2236. EXPORT_SYMBOL(sync_inode_metadata);