fs-writeback.c 86 KB

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