fs-writeback.c 80 KB

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