xfs_log.c 110 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Copyright (c) 2000-2005 Silicon Graphics, Inc.
  4. * All Rights Reserved.
  5. */
  6. #include "xfs.h"
  7. #include "xfs_fs.h"
  8. #include "xfs_shared.h"
  9. #include "xfs_format.h"
  10. #include "xfs_log_format.h"
  11. #include "xfs_trans_resv.h"
  12. #include "xfs_mount.h"
  13. #include "xfs_errortag.h"
  14. #include "xfs_error.h"
  15. #include "xfs_trans.h"
  16. #include "xfs_trans_priv.h"
  17. #include "xfs_log.h"
  18. #include "xfs_log_priv.h"
  19. #include "xfs_trace.h"
  20. #include "xfs_sysfs.h"
  21. #include "xfs_sb.h"
  22. #include "xfs_health.h"
  23. struct kmem_cache *xfs_log_ticket_cache;
  24. /* Local miscellaneous function prototypes */
  25. STATIC struct xlog *
  26. xlog_alloc_log(
  27. struct xfs_mount *mp,
  28. struct xfs_buftarg *log_target,
  29. xfs_daddr_t blk_offset,
  30. int num_bblks);
  31. STATIC int
  32. xlog_space_left(
  33. struct xlog *log,
  34. atomic64_t *head);
  35. STATIC void
  36. xlog_dealloc_log(
  37. struct xlog *log);
  38. /* local state machine functions */
  39. STATIC void xlog_state_done_syncing(
  40. struct xlog_in_core *iclog);
  41. STATIC void xlog_state_do_callback(
  42. struct xlog *log);
  43. STATIC int
  44. xlog_state_get_iclog_space(
  45. struct xlog *log,
  46. int len,
  47. struct xlog_in_core **iclog,
  48. struct xlog_ticket *ticket,
  49. int *logoffsetp);
  50. STATIC void
  51. xlog_grant_push_ail(
  52. struct xlog *log,
  53. int need_bytes);
  54. STATIC void
  55. xlog_sync(
  56. struct xlog *log,
  57. struct xlog_in_core *iclog,
  58. struct xlog_ticket *ticket);
  59. #if defined(DEBUG)
  60. STATIC void
  61. xlog_verify_grant_tail(
  62. struct xlog *log);
  63. STATIC void
  64. xlog_verify_iclog(
  65. struct xlog *log,
  66. struct xlog_in_core *iclog,
  67. int count);
  68. STATIC void
  69. xlog_verify_tail_lsn(
  70. struct xlog *log,
  71. struct xlog_in_core *iclog);
  72. #else
  73. #define xlog_verify_grant_tail(a)
  74. #define xlog_verify_iclog(a,b,c)
  75. #define xlog_verify_tail_lsn(a,b)
  76. #endif
  77. STATIC int
  78. xlog_iclogs_empty(
  79. struct xlog *log);
  80. static int
  81. xfs_log_cover(struct xfs_mount *);
  82. /*
  83. * We need to make sure the buffer pointer returned is naturally aligned for the
  84. * biggest basic data type we put into it. We have already accounted for this
  85. * padding when sizing the buffer.
  86. *
  87. * However, this padding does not get written into the log, and hence we have to
  88. * track the space used by the log vectors separately to prevent log space hangs
  89. * due to inaccurate accounting (i.e. a leak) of the used log space through the
  90. * CIL context ticket.
  91. *
  92. * We also add space for the xlog_op_header that describes this region in the
  93. * log. This prepends the data region we return to the caller to copy their data
  94. * into, so do all the static initialisation of the ophdr now. Because the ophdr
  95. * is not 8 byte aligned, we have to be careful to ensure that we align the
  96. * start of the buffer such that the region we return to the call is 8 byte
  97. * aligned and packed against the tail of the ophdr.
  98. */
  99. void *
  100. xlog_prepare_iovec(
  101. struct xfs_log_vec *lv,
  102. struct xfs_log_iovec **vecp,
  103. uint type)
  104. {
  105. struct xfs_log_iovec *vec = *vecp;
  106. struct xlog_op_header *oph;
  107. uint32_t len;
  108. void *buf;
  109. if (vec) {
  110. ASSERT(vec - lv->lv_iovecp < lv->lv_niovecs);
  111. vec++;
  112. } else {
  113. vec = &lv->lv_iovecp[0];
  114. }
  115. len = lv->lv_buf_len + sizeof(struct xlog_op_header);
  116. if (!IS_ALIGNED(len, sizeof(uint64_t))) {
  117. lv->lv_buf_len = round_up(len, sizeof(uint64_t)) -
  118. sizeof(struct xlog_op_header);
  119. }
  120. vec->i_type = type;
  121. vec->i_addr = lv->lv_buf + lv->lv_buf_len;
  122. oph = vec->i_addr;
  123. oph->oh_clientid = XFS_TRANSACTION;
  124. oph->oh_res2 = 0;
  125. oph->oh_flags = 0;
  126. buf = vec->i_addr + sizeof(struct xlog_op_header);
  127. ASSERT(IS_ALIGNED((unsigned long)buf, sizeof(uint64_t)));
  128. *vecp = vec;
  129. return buf;
  130. }
  131. static void
  132. xlog_grant_sub_space(
  133. struct xlog *log,
  134. atomic64_t *head,
  135. int bytes)
  136. {
  137. int64_t head_val = atomic64_read(head);
  138. int64_t new, old;
  139. do {
  140. int cycle, space;
  141. xlog_crack_grant_head_val(head_val, &cycle, &space);
  142. space -= bytes;
  143. if (space < 0) {
  144. space += log->l_logsize;
  145. cycle--;
  146. }
  147. old = head_val;
  148. new = xlog_assign_grant_head_val(cycle, space);
  149. head_val = atomic64_cmpxchg(head, old, new);
  150. } while (head_val != old);
  151. }
  152. static void
  153. xlog_grant_add_space(
  154. struct xlog *log,
  155. atomic64_t *head,
  156. int bytes)
  157. {
  158. int64_t head_val = atomic64_read(head);
  159. int64_t new, old;
  160. do {
  161. int tmp;
  162. int cycle, space;
  163. xlog_crack_grant_head_val(head_val, &cycle, &space);
  164. tmp = log->l_logsize - space;
  165. if (tmp > bytes)
  166. space += bytes;
  167. else {
  168. space = bytes - tmp;
  169. cycle++;
  170. }
  171. old = head_val;
  172. new = xlog_assign_grant_head_val(cycle, space);
  173. head_val = atomic64_cmpxchg(head, old, new);
  174. } while (head_val != old);
  175. }
  176. STATIC void
  177. xlog_grant_head_init(
  178. struct xlog_grant_head *head)
  179. {
  180. xlog_assign_grant_head(&head->grant, 1, 0);
  181. INIT_LIST_HEAD(&head->waiters);
  182. spin_lock_init(&head->lock);
  183. }
  184. STATIC void
  185. xlog_grant_head_wake_all(
  186. struct xlog_grant_head *head)
  187. {
  188. struct xlog_ticket *tic;
  189. spin_lock(&head->lock);
  190. list_for_each_entry(tic, &head->waiters, t_queue)
  191. wake_up_process(tic->t_task);
  192. spin_unlock(&head->lock);
  193. }
  194. static inline int
  195. xlog_ticket_reservation(
  196. struct xlog *log,
  197. struct xlog_grant_head *head,
  198. struct xlog_ticket *tic)
  199. {
  200. if (head == &log->l_write_head) {
  201. ASSERT(tic->t_flags & XLOG_TIC_PERM_RESERV);
  202. return tic->t_unit_res;
  203. }
  204. if (tic->t_flags & XLOG_TIC_PERM_RESERV)
  205. return tic->t_unit_res * tic->t_cnt;
  206. return tic->t_unit_res;
  207. }
  208. STATIC bool
  209. xlog_grant_head_wake(
  210. struct xlog *log,
  211. struct xlog_grant_head *head,
  212. int *free_bytes)
  213. {
  214. struct xlog_ticket *tic;
  215. int need_bytes;
  216. bool woken_task = false;
  217. list_for_each_entry(tic, &head->waiters, t_queue) {
  218. /*
  219. * There is a chance that the size of the CIL checkpoints in
  220. * progress at the last AIL push target calculation resulted in
  221. * limiting the target to the log head (l_last_sync_lsn) at the
  222. * time. This may not reflect where the log head is now as the
  223. * CIL checkpoints may have completed.
  224. *
  225. * Hence when we are woken here, it may be that the head of the
  226. * log that has moved rather than the tail. As the tail didn't
  227. * move, there still won't be space available for the
  228. * reservation we require. However, if the AIL has already
  229. * pushed to the target defined by the old log head location, we
  230. * will hang here waiting for something else to update the AIL
  231. * push target.
  232. *
  233. * Therefore, if there isn't space to wake the first waiter on
  234. * the grant head, we need to push the AIL again to ensure the
  235. * target reflects both the current log tail and log head
  236. * position before we wait for the tail to move again.
  237. */
  238. need_bytes = xlog_ticket_reservation(log, head, tic);
  239. if (*free_bytes < need_bytes) {
  240. if (!woken_task)
  241. xlog_grant_push_ail(log, need_bytes);
  242. return false;
  243. }
  244. *free_bytes -= need_bytes;
  245. trace_xfs_log_grant_wake_up(log, tic);
  246. wake_up_process(tic->t_task);
  247. woken_task = true;
  248. }
  249. return true;
  250. }
  251. STATIC int
  252. xlog_grant_head_wait(
  253. struct xlog *log,
  254. struct xlog_grant_head *head,
  255. struct xlog_ticket *tic,
  256. int need_bytes) __releases(&head->lock)
  257. __acquires(&head->lock)
  258. {
  259. list_add_tail(&tic->t_queue, &head->waiters);
  260. do {
  261. if (xlog_is_shutdown(log))
  262. goto shutdown;
  263. xlog_grant_push_ail(log, need_bytes);
  264. __set_current_state(TASK_UNINTERRUPTIBLE);
  265. spin_unlock(&head->lock);
  266. XFS_STATS_INC(log->l_mp, xs_sleep_logspace);
  267. trace_xfs_log_grant_sleep(log, tic);
  268. schedule();
  269. trace_xfs_log_grant_wake(log, tic);
  270. spin_lock(&head->lock);
  271. if (xlog_is_shutdown(log))
  272. goto shutdown;
  273. } while (xlog_space_left(log, &head->grant) < need_bytes);
  274. list_del_init(&tic->t_queue);
  275. return 0;
  276. shutdown:
  277. list_del_init(&tic->t_queue);
  278. return -EIO;
  279. }
  280. /*
  281. * Atomically get the log space required for a log ticket.
  282. *
  283. * Once a ticket gets put onto head->waiters, it will only return after the
  284. * needed reservation is satisfied.
  285. *
  286. * This function is structured so that it has a lock free fast path. This is
  287. * necessary because every new transaction reservation will come through this
  288. * path. Hence any lock will be globally hot if we take it unconditionally on
  289. * every pass.
  290. *
  291. * As tickets are only ever moved on and off head->waiters under head->lock, we
  292. * only need to take that lock if we are going to add the ticket to the queue
  293. * and sleep. We can avoid taking the lock if the ticket was never added to
  294. * head->waiters because the t_queue list head will be empty and we hold the
  295. * only reference to it so it can safely be checked unlocked.
  296. */
  297. STATIC int
  298. xlog_grant_head_check(
  299. struct xlog *log,
  300. struct xlog_grant_head *head,
  301. struct xlog_ticket *tic,
  302. int *need_bytes)
  303. {
  304. int free_bytes;
  305. int error = 0;
  306. ASSERT(!xlog_in_recovery(log));
  307. /*
  308. * If there are other waiters on the queue then give them a chance at
  309. * logspace before us. Wake up the first waiters, if we do not wake
  310. * up all the waiters then go to sleep waiting for more free space,
  311. * otherwise try to get some space for this transaction.
  312. */
  313. *need_bytes = xlog_ticket_reservation(log, head, tic);
  314. free_bytes = xlog_space_left(log, &head->grant);
  315. if (!list_empty_careful(&head->waiters)) {
  316. spin_lock(&head->lock);
  317. if (!xlog_grant_head_wake(log, head, &free_bytes) ||
  318. free_bytes < *need_bytes) {
  319. error = xlog_grant_head_wait(log, head, tic,
  320. *need_bytes);
  321. }
  322. spin_unlock(&head->lock);
  323. } else if (free_bytes < *need_bytes) {
  324. spin_lock(&head->lock);
  325. error = xlog_grant_head_wait(log, head, tic, *need_bytes);
  326. spin_unlock(&head->lock);
  327. }
  328. return error;
  329. }
  330. bool
  331. xfs_log_writable(
  332. struct xfs_mount *mp)
  333. {
  334. /*
  335. * Do not write to the log on norecovery mounts, if the data or log
  336. * devices are read-only, or if the filesystem is shutdown. Read-only
  337. * mounts allow internal writes for log recovery and unmount purposes,
  338. * so don't restrict that case.
  339. */
  340. if (xfs_has_norecovery(mp))
  341. return false;
  342. if (xfs_readonly_buftarg(mp->m_ddev_targp))
  343. return false;
  344. if (xfs_readonly_buftarg(mp->m_log->l_targ))
  345. return false;
  346. if (xlog_is_shutdown(mp->m_log))
  347. return false;
  348. return true;
  349. }
  350. /*
  351. * Replenish the byte reservation required by moving the grant write head.
  352. */
  353. int
  354. xfs_log_regrant(
  355. struct xfs_mount *mp,
  356. struct xlog_ticket *tic)
  357. {
  358. struct xlog *log = mp->m_log;
  359. int need_bytes;
  360. int error = 0;
  361. if (xlog_is_shutdown(log))
  362. return -EIO;
  363. XFS_STATS_INC(mp, xs_try_logspace);
  364. /*
  365. * This is a new transaction on the ticket, so we need to change the
  366. * transaction ID so that the next transaction has a different TID in
  367. * the log. Just add one to the existing tid so that we can see chains
  368. * of rolling transactions in the log easily.
  369. */
  370. tic->t_tid++;
  371. xlog_grant_push_ail(log, tic->t_unit_res);
  372. tic->t_curr_res = tic->t_unit_res;
  373. if (tic->t_cnt > 0)
  374. return 0;
  375. trace_xfs_log_regrant(log, tic);
  376. error = xlog_grant_head_check(log, &log->l_write_head, tic,
  377. &need_bytes);
  378. if (error)
  379. goto out_error;
  380. xlog_grant_add_space(log, &log->l_write_head.grant, need_bytes);
  381. trace_xfs_log_regrant_exit(log, tic);
  382. xlog_verify_grant_tail(log);
  383. return 0;
  384. out_error:
  385. /*
  386. * If we are failing, make sure the ticket doesn't have any current
  387. * reservations. We don't want to add this back when the ticket/
  388. * transaction gets cancelled.
  389. */
  390. tic->t_curr_res = 0;
  391. tic->t_cnt = 0; /* ungrant will give back unit_res * t_cnt. */
  392. return error;
  393. }
  394. /*
  395. * Reserve log space and return a ticket corresponding to the reservation.
  396. *
  397. * Each reservation is going to reserve extra space for a log record header.
  398. * When writes happen to the on-disk log, we don't subtract the length of the
  399. * log record header from any reservation. By wasting space in each
  400. * reservation, we prevent over allocation problems.
  401. */
  402. int
  403. xfs_log_reserve(
  404. struct xfs_mount *mp,
  405. int unit_bytes,
  406. int cnt,
  407. struct xlog_ticket **ticp,
  408. bool permanent)
  409. {
  410. struct xlog *log = mp->m_log;
  411. struct xlog_ticket *tic;
  412. int need_bytes;
  413. int error = 0;
  414. if (xlog_is_shutdown(log))
  415. return -EIO;
  416. XFS_STATS_INC(mp, xs_try_logspace);
  417. ASSERT(*ticp == NULL);
  418. tic = xlog_ticket_alloc(log, unit_bytes, cnt, permanent);
  419. *ticp = tic;
  420. xlog_grant_push_ail(log, tic->t_cnt ? tic->t_unit_res * tic->t_cnt
  421. : tic->t_unit_res);
  422. trace_xfs_log_reserve(log, tic);
  423. error = xlog_grant_head_check(log, &log->l_reserve_head, tic,
  424. &need_bytes);
  425. if (error)
  426. goto out_error;
  427. xlog_grant_add_space(log, &log->l_reserve_head.grant, need_bytes);
  428. xlog_grant_add_space(log, &log->l_write_head.grant, need_bytes);
  429. trace_xfs_log_reserve_exit(log, tic);
  430. xlog_verify_grant_tail(log);
  431. return 0;
  432. out_error:
  433. /*
  434. * If we are failing, make sure the ticket doesn't have any current
  435. * reservations. We don't want to add this back when the ticket/
  436. * transaction gets cancelled.
  437. */
  438. tic->t_curr_res = 0;
  439. tic->t_cnt = 0; /* ungrant will give back unit_res * t_cnt. */
  440. return error;
  441. }
  442. /*
  443. * Run all the pending iclog callbacks and wake log force waiters and iclog
  444. * space waiters so they can process the newly set shutdown state. We really
  445. * don't care what order we process callbacks here because the log is shut down
  446. * and so state cannot change on disk anymore. However, we cannot wake waiters
  447. * until the callbacks have been processed because we may be in unmount and
  448. * we must ensure that all AIL operations the callbacks perform have completed
  449. * before we tear down the AIL.
  450. *
  451. * We avoid processing actively referenced iclogs so that we don't run callbacks
  452. * while the iclog owner might still be preparing the iclog for IO submssion.
  453. * These will be caught by xlog_state_iclog_release() and call this function
  454. * again to process any callbacks that may have been added to that iclog.
  455. */
  456. static void
  457. xlog_state_shutdown_callbacks(
  458. struct xlog *log)
  459. {
  460. struct xlog_in_core *iclog;
  461. LIST_HEAD(cb_list);
  462. iclog = log->l_iclog;
  463. do {
  464. if (atomic_read(&iclog->ic_refcnt)) {
  465. /* Reference holder will re-run iclog callbacks. */
  466. continue;
  467. }
  468. list_splice_init(&iclog->ic_callbacks, &cb_list);
  469. spin_unlock(&log->l_icloglock);
  470. xlog_cil_process_committed(&cb_list);
  471. spin_lock(&log->l_icloglock);
  472. wake_up_all(&iclog->ic_write_wait);
  473. wake_up_all(&iclog->ic_force_wait);
  474. } while ((iclog = iclog->ic_next) != log->l_iclog);
  475. wake_up_all(&log->l_flush_wait);
  476. }
  477. /*
  478. * Flush iclog to disk if this is the last reference to the given iclog and the
  479. * it is in the WANT_SYNC state.
  480. *
  481. * If XLOG_ICL_NEED_FUA is already set on the iclog, we need to ensure that the
  482. * log tail is updated correctly. NEED_FUA indicates that the iclog will be
  483. * written to stable storage, and implies that a commit record is contained
  484. * within the iclog. We need to ensure that the log tail does not move beyond
  485. * the tail that the first commit record in the iclog ordered against, otherwise
  486. * correct recovery of that checkpoint becomes dependent on future operations
  487. * performed on this iclog.
  488. *
  489. * Hence if NEED_FUA is set and the current iclog tail lsn is empty, write the
  490. * current tail into iclog. Once the iclog tail is set, future operations must
  491. * not modify it, otherwise they potentially violate ordering constraints for
  492. * the checkpoint commit that wrote the initial tail lsn value. The tail lsn in
  493. * the iclog will get zeroed on activation of the iclog after sync, so we
  494. * always capture the tail lsn on the iclog on the first NEED_FUA release
  495. * regardless of the number of active reference counts on this iclog.
  496. */
  497. int
  498. xlog_state_release_iclog(
  499. struct xlog *log,
  500. struct xlog_in_core *iclog,
  501. struct xlog_ticket *ticket)
  502. {
  503. xfs_lsn_t tail_lsn;
  504. bool last_ref;
  505. lockdep_assert_held(&log->l_icloglock);
  506. trace_xlog_iclog_release(iclog, _RET_IP_);
  507. /*
  508. * Grabbing the current log tail needs to be atomic w.r.t. the writing
  509. * of the tail LSN into the iclog so we guarantee that the log tail does
  510. * not move between the first time we know that the iclog needs to be
  511. * made stable and when we eventually submit it.
  512. */
  513. if ((iclog->ic_state == XLOG_STATE_WANT_SYNC ||
  514. (iclog->ic_flags & XLOG_ICL_NEED_FUA)) &&
  515. !iclog->ic_header.h_tail_lsn) {
  516. tail_lsn = xlog_assign_tail_lsn(log->l_mp);
  517. iclog->ic_header.h_tail_lsn = cpu_to_be64(tail_lsn);
  518. }
  519. last_ref = atomic_dec_and_test(&iclog->ic_refcnt);
  520. if (xlog_is_shutdown(log)) {
  521. /*
  522. * If there are no more references to this iclog, process the
  523. * pending iclog callbacks that were waiting on the release of
  524. * this iclog.
  525. */
  526. if (last_ref)
  527. xlog_state_shutdown_callbacks(log);
  528. return -EIO;
  529. }
  530. if (!last_ref)
  531. return 0;
  532. if (iclog->ic_state != XLOG_STATE_WANT_SYNC) {
  533. ASSERT(iclog->ic_state == XLOG_STATE_ACTIVE);
  534. return 0;
  535. }
  536. iclog->ic_state = XLOG_STATE_SYNCING;
  537. xlog_verify_tail_lsn(log, iclog);
  538. trace_xlog_iclog_syncing(iclog, _RET_IP_);
  539. spin_unlock(&log->l_icloglock);
  540. xlog_sync(log, iclog, ticket);
  541. spin_lock(&log->l_icloglock);
  542. return 0;
  543. }
  544. /*
  545. * Mount a log filesystem
  546. *
  547. * mp - ubiquitous xfs mount point structure
  548. * log_target - buftarg of on-disk log device
  549. * blk_offset - Start block # where block size is 512 bytes (BBSIZE)
  550. * num_bblocks - Number of BBSIZE blocks in on-disk log
  551. *
  552. * Return error or zero.
  553. */
  554. int
  555. xfs_log_mount(
  556. xfs_mount_t *mp,
  557. xfs_buftarg_t *log_target,
  558. xfs_daddr_t blk_offset,
  559. int num_bblks)
  560. {
  561. struct xlog *log;
  562. bool fatal = xfs_has_crc(mp);
  563. int error = 0;
  564. int min_logfsbs;
  565. if (!xfs_has_norecovery(mp)) {
  566. xfs_notice(mp, "Mounting V%d Filesystem",
  567. XFS_SB_VERSION_NUM(&mp->m_sb));
  568. } else {
  569. xfs_notice(mp,
  570. "Mounting V%d filesystem in no-recovery mode. Filesystem will be inconsistent.",
  571. XFS_SB_VERSION_NUM(&mp->m_sb));
  572. ASSERT(xfs_is_readonly(mp));
  573. }
  574. log = xlog_alloc_log(mp, log_target, blk_offset, num_bblks);
  575. if (IS_ERR(log)) {
  576. error = PTR_ERR(log);
  577. goto out;
  578. }
  579. mp->m_log = log;
  580. /*
  581. * Validate the given log space and drop a critical message via syslog
  582. * if the log size is too small that would lead to some unexpected
  583. * situations in transaction log space reservation stage.
  584. *
  585. * Note: we can't just reject the mount if the validation fails. This
  586. * would mean that people would have to downgrade their kernel just to
  587. * remedy the situation as there is no way to grow the log (short of
  588. * black magic surgery with xfs_db).
  589. *
  590. * We can, however, reject mounts for CRC format filesystems, as the
  591. * mkfs binary being used to make the filesystem should never create a
  592. * filesystem with a log that is too small.
  593. */
  594. min_logfsbs = xfs_log_calc_minimum_size(mp);
  595. if (mp->m_sb.sb_logblocks < min_logfsbs) {
  596. xfs_warn(mp,
  597. "Log size %d blocks too small, minimum size is %d blocks",
  598. mp->m_sb.sb_logblocks, min_logfsbs);
  599. error = -EINVAL;
  600. } else if (mp->m_sb.sb_logblocks > XFS_MAX_LOG_BLOCKS) {
  601. xfs_warn(mp,
  602. "Log size %d blocks too large, maximum size is %lld blocks",
  603. mp->m_sb.sb_logblocks, XFS_MAX_LOG_BLOCKS);
  604. error = -EINVAL;
  605. } else if (XFS_FSB_TO_B(mp, mp->m_sb.sb_logblocks) > XFS_MAX_LOG_BYTES) {
  606. xfs_warn(mp,
  607. "log size %lld bytes too large, maximum size is %lld bytes",
  608. XFS_FSB_TO_B(mp, mp->m_sb.sb_logblocks),
  609. XFS_MAX_LOG_BYTES);
  610. error = -EINVAL;
  611. } else if (mp->m_sb.sb_logsunit > 1 &&
  612. mp->m_sb.sb_logsunit % mp->m_sb.sb_blocksize) {
  613. xfs_warn(mp,
  614. "log stripe unit %u bytes must be a multiple of block size",
  615. mp->m_sb.sb_logsunit);
  616. error = -EINVAL;
  617. fatal = true;
  618. }
  619. if (error) {
  620. /*
  621. * Log check errors are always fatal on v5; or whenever bad
  622. * metadata leads to a crash.
  623. */
  624. if (fatal) {
  625. xfs_crit(mp, "AAIEEE! Log failed size checks. Abort!");
  626. ASSERT(0);
  627. goto out_free_log;
  628. }
  629. xfs_crit(mp, "Log size out of supported range.");
  630. xfs_crit(mp,
  631. "Continuing onwards, but if log hangs are experienced then please report this message in the bug report.");
  632. }
  633. /*
  634. * Initialize the AIL now we have a log.
  635. */
  636. error = xfs_trans_ail_init(mp);
  637. if (error) {
  638. xfs_warn(mp, "AIL initialisation failed: error %d", error);
  639. goto out_free_log;
  640. }
  641. log->l_ailp = mp->m_ail;
  642. /*
  643. * skip log recovery on a norecovery mount. pretend it all
  644. * just worked.
  645. */
  646. if (!xfs_has_norecovery(mp)) {
  647. /*
  648. * log recovery ignores readonly state and so we need to clear
  649. * mount-based read only state so it can write to disk.
  650. */
  651. bool readonly = test_and_clear_bit(XFS_OPSTATE_READONLY,
  652. &mp->m_opstate);
  653. error = xlog_recover(log);
  654. if (readonly)
  655. set_bit(XFS_OPSTATE_READONLY, &mp->m_opstate);
  656. if (error) {
  657. xfs_warn(mp, "log mount/recovery failed: error %d",
  658. error);
  659. xlog_recover_cancel(log);
  660. goto out_destroy_ail;
  661. }
  662. }
  663. error = xfs_sysfs_init(&log->l_kobj, &xfs_log_ktype, &mp->m_kobj,
  664. "log");
  665. if (error)
  666. goto out_destroy_ail;
  667. /* Normal transactions can now occur */
  668. clear_bit(XLOG_ACTIVE_RECOVERY, &log->l_opstate);
  669. /*
  670. * Now the log has been fully initialised and we know were our
  671. * space grant counters are, we can initialise the permanent ticket
  672. * needed for delayed logging to work.
  673. */
  674. xlog_cil_init_post_recovery(log);
  675. return 0;
  676. out_destroy_ail:
  677. xfs_trans_ail_destroy(mp);
  678. out_free_log:
  679. xlog_dealloc_log(log);
  680. out:
  681. return error;
  682. }
  683. /*
  684. * Finish the recovery of the file system. This is separate from the
  685. * xfs_log_mount() call, because it depends on the code in xfs_mountfs() to read
  686. * in the root and real-time bitmap inodes between calling xfs_log_mount() and
  687. * here.
  688. *
  689. * If we finish recovery successfully, start the background log work. If we are
  690. * not doing recovery, then we have a RO filesystem and we don't need to start
  691. * it.
  692. */
  693. int
  694. xfs_log_mount_finish(
  695. struct xfs_mount *mp)
  696. {
  697. struct xlog *log = mp->m_log;
  698. bool readonly;
  699. int error = 0;
  700. if (xfs_has_norecovery(mp)) {
  701. ASSERT(xfs_is_readonly(mp));
  702. return 0;
  703. }
  704. /*
  705. * log recovery ignores readonly state and so we need to clear
  706. * mount-based read only state so it can write to disk.
  707. */
  708. readonly = test_and_clear_bit(XFS_OPSTATE_READONLY, &mp->m_opstate);
  709. /*
  710. * During the second phase of log recovery, we need iget and
  711. * iput to behave like they do for an active filesystem.
  712. * xfs_fs_drop_inode needs to be able to prevent the deletion
  713. * of inodes before we're done replaying log items on those
  714. * inodes. Turn it off immediately after recovery finishes
  715. * so that we don't leak the quota inodes if subsequent mount
  716. * activities fail.
  717. *
  718. * We let all inodes involved in redo item processing end up on
  719. * the LRU instead of being evicted immediately so that if we do
  720. * something to an unlinked inode, the irele won't cause
  721. * premature truncation and freeing of the inode, which results
  722. * in log recovery failure. We have to evict the unreferenced
  723. * lru inodes after clearing SB_ACTIVE because we don't
  724. * otherwise clean up the lru if there's a subsequent failure in
  725. * xfs_mountfs, which leads to us leaking the inodes if nothing
  726. * else (e.g. quotacheck) references the inodes before the
  727. * mount failure occurs.
  728. */
  729. mp->m_super->s_flags |= SB_ACTIVE;
  730. xfs_log_work_queue(mp);
  731. if (xlog_recovery_needed(log))
  732. error = xlog_recover_finish(log);
  733. mp->m_super->s_flags &= ~SB_ACTIVE;
  734. evict_inodes(mp->m_super);
  735. /*
  736. * Drain the buffer LRU after log recovery. This is required for v4
  737. * filesystems to avoid leaving around buffers with NULL verifier ops,
  738. * but we do it unconditionally to make sure we're always in a clean
  739. * cache state after mount.
  740. *
  741. * Don't push in the error case because the AIL may have pending intents
  742. * that aren't removed until recovery is cancelled.
  743. */
  744. if (xlog_recovery_needed(log)) {
  745. if (!error) {
  746. xfs_log_force(mp, XFS_LOG_SYNC);
  747. xfs_ail_push_all_sync(mp->m_ail);
  748. }
  749. xfs_notice(mp, "Ending recovery (logdev: %s)",
  750. mp->m_logname ? mp->m_logname : "internal");
  751. } else {
  752. xfs_info(mp, "Ending clean mount");
  753. }
  754. xfs_buftarg_drain(mp->m_ddev_targp);
  755. clear_bit(XLOG_RECOVERY_NEEDED, &log->l_opstate);
  756. if (readonly)
  757. set_bit(XFS_OPSTATE_READONLY, &mp->m_opstate);
  758. /* Make sure the log is dead if we're returning failure. */
  759. ASSERT(!error || xlog_is_shutdown(log));
  760. return error;
  761. }
  762. /*
  763. * The mount has failed. Cancel the recovery if it hasn't completed and destroy
  764. * the log.
  765. */
  766. void
  767. xfs_log_mount_cancel(
  768. struct xfs_mount *mp)
  769. {
  770. xlog_recover_cancel(mp->m_log);
  771. xfs_log_unmount(mp);
  772. }
  773. /*
  774. * Flush out the iclog to disk ensuring that device caches are flushed and
  775. * the iclog hits stable storage before any completion waiters are woken.
  776. */
  777. static inline int
  778. xlog_force_iclog(
  779. struct xlog_in_core *iclog)
  780. {
  781. atomic_inc(&iclog->ic_refcnt);
  782. iclog->ic_flags |= XLOG_ICL_NEED_FLUSH | XLOG_ICL_NEED_FUA;
  783. if (iclog->ic_state == XLOG_STATE_ACTIVE)
  784. xlog_state_switch_iclogs(iclog->ic_log, iclog, 0);
  785. return xlog_state_release_iclog(iclog->ic_log, iclog, NULL);
  786. }
  787. /*
  788. * Wait for the iclog and all prior iclogs to be written disk as required by the
  789. * log force state machine. Waiting on ic_force_wait ensures iclog completions
  790. * have been ordered and callbacks run before we are woken here, hence
  791. * guaranteeing that all the iclogs up to this one are on stable storage.
  792. */
  793. int
  794. xlog_wait_on_iclog(
  795. struct xlog_in_core *iclog)
  796. __releases(iclog->ic_log->l_icloglock)
  797. {
  798. struct xlog *log = iclog->ic_log;
  799. trace_xlog_iclog_wait_on(iclog, _RET_IP_);
  800. if (!xlog_is_shutdown(log) &&
  801. iclog->ic_state != XLOG_STATE_ACTIVE &&
  802. iclog->ic_state != XLOG_STATE_DIRTY) {
  803. XFS_STATS_INC(log->l_mp, xs_log_force_sleep);
  804. xlog_wait(&iclog->ic_force_wait, &log->l_icloglock);
  805. } else {
  806. spin_unlock(&log->l_icloglock);
  807. }
  808. if (xlog_is_shutdown(log))
  809. return -EIO;
  810. return 0;
  811. }
  812. /*
  813. * Write out an unmount record using the ticket provided. We have to account for
  814. * the data space used in the unmount ticket as this write is not done from a
  815. * transaction context that has already done the accounting for us.
  816. */
  817. static int
  818. xlog_write_unmount_record(
  819. struct xlog *log,
  820. struct xlog_ticket *ticket)
  821. {
  822. struct {
  823. struct xlog_op_header ophdr;
  824. struct xfs_unmount_log_format ulf;
  825. } unmount_rec = {
  826. .ophdr = {
  827. .oh_clientid = XFS_LOG,
  828. .oh_tid = cpu_to_be32(ticket->t_tid),
  829. .oh_flags = XLOG_UNMOUNT_TRANS,
  830. },
  831. .ulf = {
  832. .magic = XLOG_UNMOUNT_TYPE,
  833. },
  834. };
  835. struct xfs_log_iovec reg = {
  836. .i_addr = &unmount_rec,
  837. .i_len = sizeof(unmount_rec),
  838. .i_type = XLOG_REG_TYPE_UNMOUNT,
  839. };
  840. struct xfs_log_vec vec = {
  841. .lv_niovecs = 1,
  842. .lv_iovecp = &reg,
  843. };
  844. LIST_HEAD(lv_chain);
  845. list_add(&vec.lv_list, &lv_chain);
  846. BUILD_BUG_ON((sizeof(struct xlog_op_header) +
  847. sizeof(struct xfs_unmount_log_format)) !=
  848. sizeof(unmount_rec));
  849. /* account for space used by record data */
  850. ticket->t_curr_res -= sizeof(unmount_rec);
  851. return xlog_write(log, NULL, &lv_chain, ticket, reg.i_len);
  852. }
  853. /*
  854. * Mark the filesystem clean by writing an unmount record to the head of the
  855. * log.
  856. */
  857. static void
  858. xlog_unmount_write(
  859. struct xlog *log)
  860. {
  861. struct xfs_mount *mp = log->l_mp;
  862. struct xlog_in_core *iclog;
  863. struct xlog_ticket *tic = NULL;
  864. int error;
  865. error = xfs_log_reserve(mp, 600, 1, &tic, 0);
  866. if (error)
  867. goto out_err;
  868. error = xlog_write_unmount_record(log, tic);
  869. /*
  870. * At this point, we're umounting anyway, so there's no point in
  871. * transitioning log state to shutdown. Just continue...
  872. */
  873. out_err:
  874. if (error)
  875. xfs_alert(mp, "%s: unmount record failed", __func__);
  876. spin_lock(&log->l_icloglock);
  877. iclog = log->l_iclog;
  878. error = xlog_force_iclog(iclog);
  879. xlog_wait_on_iclog(iclog);
  880. if (tic) {
  881. trace_xfs_log_umount_write(log, tic);
  882. xfs_log_ticket_ungrant(log, tic);
  883. }
  884. }
  885. static void
  886. xfs_log_unmount_verify_iclog(
  887. struct xlog *log)
  888. {
  889. struct xlog_in_core *iclog = log->l_iclog;
  890. do {
  891. ASSERT(iclog->ic_state == XLOG_STATE_ACTIVE);
  892. ASSERT(iclog->ic_offset == 0);
  893. } while ((iclog = iclog->ic_next) != log->l_iclog);
  894. }
  895. /*
  896. * Unmount record used to have a string "Unmount filesystem--" in the
  897. * data section where the "Un" was really a magic number (XLOG_UNMOUNT_TYPE).
  898. * We just write the magic number now since that particular field isn't
  899. * currently architecture converted and "Unmount" is a bit foo.
  900. * As far as I know, there weren't any dependencies on the old behaviour.
  901. */
  902. static void
  903. xfs_log_unmount_write(
  904. struct xfs_mount *mp)
  905. {
  906. struct xlog *log = mp->m_log;
  907. if (!xfs_log_writable(mp))
  908. return;
  909. xfs_log_force(mp, XFS_LOG_SYNC);
  910. if (xlog_is_shutdown(log))
  911. return;
  912. /*
  913. * If we think the summary counters are bad, avoid writing the unmount
  914. * record to force log recovery at next mount, after which the summary
  915. * counters will be recalculated. Refer to xlog_check_unmount_rec for
  916. * more details.
  917. */
  918. if (XFS_TEST_ERROR(xfs_fs_has_sickness(mp, XFS_SICK_FS_COUNTERS), mp,
  919. XFS_ERRTAG_FORCE_SUMMARY_RECALC)) {
  920. xfs_alert(mp, "%s: will fix summary counters at next mount",
  921. __func__);
  922. return;
  923. }
  924. xfs_log_unmount_verify_iclog(log);
  925. xlog_unmount_write(log);
  926. }
  927. /*
  928. * Empty the log for unmount/freeze.
  929. *
  930. * To do this, we first need to shut down the background log work so it is not
  931. * trying to cover the log as we clean up. We then need to unpin all objects in
  932. * the log so we can then flush them out. Once they have completed their IO and
  933. * run the callbacks removing themselves from the AIL, we can cover the log.
  934. */
  935. int
  936. xfs_log_quiesce(
  937. struct xfs_mount *mp)
  938. {
  939. /*
  940. * Clear log incompat features since we're quiescing the log. Report
  941. * failures, though it's not fatal to have a higher log feature
  942. * protection level than the log contents actually require.
  943. */
  944. if (xfs_clear_incompat_log_features(mp)) {
  945. int error;
  946. error = xfs_sync_sb(mp, false);
  947. if (error)
  948. xfs_warn(mp,
  949. "Failed to clear log incompat features on quiesce");
  950. }
  951. cancel_delayed_work_sync(&mp->m_log->l_work);
  952. xfs_log_force(mp, XFS_LOG_SYNC);
  953. /*
  954. * The superblock buffer is uncached and while xfs_ail_push_all_sync()
  955. * will push it, xfs_buftarg_wait() will not wait for it. Further,
  956. * xfs_buf_iowait() cannot be used because it was pushed with the
  957. * XBF_ASYNC flag set, so we need to use a lock/unlock pair to wait for
  958. * the IO to complete.
  959. */
  960. xfs_ail_push_all_sync(mp->m_ail);
  961. xfs_buftarg_wait(mp->m_ddev_targp);
  962. xfs_buf_lock(mp->m_sb_bp);
  963. xfs_buf_unlock(mp->m_sb_bp);
  964. return xfs_log_cover(mp);
  965. }
  966. void
  967. xfs_log_clean(
  968. struct xfs_mount *mp)
  969. {
  970. xfs_log_quiesce(mp);
  971. xfs_log_unmount_write(mp);
  972. }
  973. /*
  974. * Shut down and release the AIL and Log.
  975. *
  976. * During unmount, we need to ensure we flush all the dirty metadata objects
  977. * from the AIL so that the log is empty before we write the unmount record to
  978. * the log. Once this is done, we can tear down the AIL and the log.
  979. */
  980. void
  981. xfs_log_unmount(
  982. struct xfs_mount *mp)
  983. {
  984. xfs_log_clean(mp);
  985. xfs_buftarg_drain(mp->m_ddev_targp);
  986. xfs_trans_ail_destroy(mp);
  987. xfs_sysfs_del(&mp->m_log->l_kobj);
  988. xlog_dealloc_log(mp->m_log);
  989. }
  990. void
  991. xfs_log_item_init(
  992. struct xfs_mount *mp,
  993. struct xfs_log_item *item,
  994. int type,
  995. const struct xfs_item_ops *ops)
  996. {
  997. item->li_log = mp->m_log;
  998. item->li_ailp = mp->m_ail;
  999. item->li_type = type;
  1000. item->li_ops = ops;
  1001. item->li_lv = NULL;
  1002. INIT_LIST_HEAD(&item->li_ail);
  1003. INIT_LIST_HEAD(&item->li_cil);
  1004. INIT_LIST_HEAD(&item->li_bio_list);
  1005. INIT_LIST_HEAD(&item->li_trans);
  1006. }
  1007. /*
  1008. * Wake up processes waiting for log space after we have moved the log tail.
  1009. */
  1010. void
  1011. xfs_log_space_wake(
  1012. struct xfs_mount *mp)
  1013. {
  1014. struct xlog *log = mp->m_log;
  1015. int free_bytes;
  1016. if (xlog_is_shutdown(log))
  1017. return;
  1018. if (!list_empty_careful(&log->l_write_head.waiters)) {
  1019. ASSERT(!xlog_in_recovery(log));
  1020. spin_lock(&log->l_write_head.lock);
  1021. free_bytes = xlog_space_left(log, &log->l_write_head.grant);
  1022. xlog_grant_head_wake(log, &log->l_write_head, &free_bytes);
  1023. spin_unlock(&log->l_write_head.lock);
  1024. }
  1025. if (!list_empty_careful(&log->l_reserve_head.waiters)) {
  1026. ASSERT(!xlog_in_recovery(log));
  1027. spin_lock(&log->l_reserve_head.lock);
  1028. free_bytes = xlog_space_left(log, &log->l_reserve_head.grant);
  1029. xlog_grant_head_wake(log, &log->l_reserve_head, &free_bytes);
  1030. spin_unlock(&log->l_reserve_head.lock);
  1031. }
  1032. }
  1033. /*
  1034. * Determine if we have a transaction that has gone to disk that needs to be
  1035. * covered. To begin the transition to the idle state firstly the log needs to
  1036. * be idle. That means the CIL, the AIL and the iclogs needs to be empty before
  1037. * we start attempting to cover the log.
  1038. *
  1039. * Only if we are then in a state where covering is needed, the caller is
  1040. * informed that dummy transactions are required to move the log into the idle
  1041. * state.
  1042. *
  1043. * If there are any items in the AIl or CIL, then we do not want to attempt to
  1044. * cover the log as we may be in a situation where there isn't log space
  1045. * available to run a dummy transaction and this can lead to deadlocks when the
  1046. * tail of the log is pinned by an item that is modified in the CIL. Hence
  1047. * there's no point in running a dummy transaction at this point because we
  1048. * can't start trying to idle the log until both the CIL and AIL are empty.
  1049. */
  1050. static bool
  1051. xfs_log_need_covered(
  1052. struct xfs_mount *mp)
  1053. {
  1054. struct xlog *log = mp->m_log;
  1055. bool needed = false;
  1056. if (!xlog_cil_empty(log))
  1057. return false;
  1058. spin_lock(&log->l_icloglock);
  1059. switch (log->l_covered_state) {
  1060. case XLOG_STATE_COVER_DONE:
  1061. case XLOG_STATE_COVER_DONE2:
  1062. case XLOG_STATE_COVER_IDLE:
  1063. break;
  1064. case XLOG_STATE_COVER_NEED:
  1065. case XLOG_STATE_COVER_NEED2:
  1066. if (xfs_ail_min_lsn(log->l_ailp))
  1067. break;
  1068. if (!xlog_iclogs_empty(log))
  1069. break;
  1070. needed = true;
  1071. if (log->l_covered_state == XLOG_STATE_COVER_NEED)
  1072. log->l_covered_state = XLOG_STATE_COVER_DONE;
  1073. else
  1074. log->l_covered_state = XLOG_STATE_COVER_DONE2;
  1075. break;
  1076. default:
  1077. needed = true;
  1078. break;
  1079. }
  1080. spin_unlock(&log->l_icloglock);
  1081. return needed;
  1082. }
  1083. /*
  1084. * Explicitly cover the log. This is similar to background log covering but
  1085. * intended for usage in quiesce codepaths. The caller is responsible to ensure
  1086. * the log is idle and suitable for covering. The CIL, iclog buffers and AIL
  1087. * must all be empty.
  1088. */
  1089. static int
  1090. xfs_log_cover(
  1091. struct xfs_mount *mp)
  1092. {
  1093. int error = 0;
  1094. bool need_covered;
  1095. ASSERT((xlog_cil_empty(mp->m_log) && xlog_iclogs_empty(mp->m_log) &&
  1096. !xfs_ail_min_lsn(mp->m_log->l_ailp)) ||
  1097. xlog_is_shutdown(mp->m_log));
  1098. if (!xfs_log_writable(mp))
  1099. return 0;
  1100. /*
  1101. * xfs_log_need_covered() is not idempotent because it progresses the
  1102. * state machine if the log requires covering. Therefore, we must call
  1103. * this function once and use the result until we've issued an sb sync.
  1104. * Do so first to make that abundantly clear.
  1105. *
  1106. * Fall into the covering sequence if the log needs covering or the
  1107. * mount has lazy superblock accounting to sync to disk. The sb sync
  1108. * used for covering accumulates the in-core counters, so covering
  1109. * handles this for us.
  1110. */
  1111. need_covered = xfs_log_need_covered(mp);
  1112. if (!need_covered && !xfs_has_lazysbcount(mp))
  1113. return 0;
  1114. /*
  1115. * To cover the log, commit the superblock twice (at most) in
  1116. * independent checkpoints. The first serves as a reference for the
  1117. * tail pointer. The sync transaction and AIL push empties the AIL and
  1118. * updates the in-core tail to the LSN of the first checkpoint. The
  1119. * second commit updates the on-disk tail with the in-core LSN,
  1120. * covering the log. Push the AIL one more time to leave it empty, as
  1121. * we found it.
  1122. */
  1123. do {
  1124. error = xfs_sync_sb(mp, true);
  1125. if (error)
  1126. break;
  1127. xfs_ail_push_all_sync(mp->m_ail);
  1128. } while (xfs_log_need_covered(mp));
  1129. return error;
  1130. }
  1131. /*
  1132. * We may be holding the log iclog lock upon entering this routine.
  1133. */
  1134. xfs_lsn_t
  1135. xlog_assign_tail_lsn_locked(
  1136. struct xfs_mount *mp)
  1137. {
  1138. struct xlog *log = mp->m_log;
  1139. struct xfs_log_item *lip;
  1140. xfs_lsn_t tail_lsn;
  1141. assert_spin_locked(&mp->m_ail->ail_lock);
  1142. /*
  1143. * To make sure we always have a valid LSN for the log tail we keep
  1144. * track of the last LSN which was committed in log->l_last_sync_lsn,
  1145. * and use that when the AIL was empty.
  1146. */
  1147. lip = xfs_ail_min(mp->m_ail);
  1148. if (lip)
  1149. tail_lsn = lip->li_lsn;
  1150. else
  1151. tail_lsn = atomic64_read(&log->l_last_sync_lsn);
  1152. trace_xfs_log_assign_tail_lsn(log, tail_lsn);
  1153. atomic64_set(&log->l_tail_lsn, tail_lsn);
  1154. return tail_lsn;
  1155. }
  1156. xfs_lsn_t
  1157. xlog_assign_tail_lsn(
  1158. struct xfs_mount *mp)
  1159. {
  1160. xfs_lsn_t tail_lsn;
  1161. spin_lock(&mp->m_ail->ail_lock);
  1162. tail_lsn = xlog_assign_tail_lsn_locked(mp);
  1163. spin_unlock(&mp->m_ail->ail_lock);
  1164. return tail_lsn;
  1165. }
  1166. /*
  1167. * Return the space in the log between the tail and the head. The head
  1168. * is passed in the cycle/bytes formal parms. In the special case where
  1169. * the reserve head has wrapped passed the tail, this calculation is no
  1170. * longer valid. In this case, just return 0 which means there is no space
  1171. * in the log. This works for all places where this function is called
  1172. * with the reserve head. Of course, if the write head were to ever
  1173. * wrap the tail, we should blow up. Rather than catch this case here,
  1174. * we depend on other ASSERTions in other parts of the code. XXXmiken
  1175. *
  1176. * If reservation head is behind the tail, we have a problem. Warn about it,
  1177. * but then treat it as if the log is empty.
  1178. *
  1179. * If the log is shut down, the head and tail may be invalid or out of whack, so
  1180. * shortcut invalidity asserts in this case so that we don't trigger them
  1181. * falsely.
  1182. */
  1183. STATIC int
  1184. xlog_space_left(
  1185. struct xlog *log,
  1186. atomic64_t *head)
  1187. {
  1188. int tail_bytes;
  1189. int tail_cycle;
  1190. int head_cycle;
  1191. int head_bytes;
  1192. xlog_crack_grant_head(head, &head_cycle, &head_bytes);
  1193. xlog_crack_atomic_lsn(&log->l_tail_lsn, &tail_cycle, &tail_bytes);
  1194. tail_bytes = BBTOB(tail_bytes);
  1195. if (tail_cycle == head_cycle && head_bytes >= tail_bytes)
  1196. return log->l_logsize - (head_bytes - tail_bytes);
  1197. if (tail_cycle + 1 < head_cycle)
  1198. return 0;
  1199. /* Ignore potential inconsistency when shutdown. */
  1200. if (xlog_is_shutdown(log))
  1201. return log->l_logsize;
  1202. if (tail_cycle < head_cycle) {
  1203. ASSERT(tail_cycle == (head_cycle - 1));
  1204. return tail_bytes - head_bytes;
  1205. }
  1206. /*
  1207. * The reservation head is behind the tail. In this case we just want to
  1208. * return the size of the log as the amount of space left.
  1209. */
  1210. xfs_alert(log->l_mp, "xlog_space_left: head behind tail");
  1211. xfs_alert(log->l_mp, " tail_cycle = %d, tail_bytes = %d",
  1212. tail_cycle, tail_bytes);
  1213. xfs_alert(log->l_mp, " GH cycle = %d, GH bytes = %d",
  1214. head_cycle, head_bytes);
  1215. ASSERT(0);
  1216. return log->l_logsize;
  1217. }
  1218. static void
  1219. xlog_ioend_work(
  1220. struct work_struct *work)
  1221. {
  1222. struct xlog_in_core *iclog =
  1223. container_of(work, struct xlog_in_core, ic_end_io_work);
  1224. struct xlog *log = iclog->ic_log;
  1225. int error;
  1226. error = blk_status_to_errno(iclog->ic_bio.bi_status);
  1227. #ifdef DEBUG
  1228. /* treat writes with injected CRC errors as failed */
  1229. if (iclog->ic_fail_crc)
  1230. error = -EIO;
  1231. #endif
  1232. /*
  1233. * Race to shutdown the filesystem if we see an error.
  1234. */
  1235. if (XFS_TEST_ERROR(error, log->l_mp, XFS_ERRTAG_IODONE_IOERR)) {
  1236. xfs_alert(log->l_mp, "log I/O error %d", error);
  1237. xlog_force_shutdown(log, SHUTDOWN_LOG_IO_ERROR);
  1238. }
  1239. xlog_state_done_syncing(iclog);
  1240. bio_uninit(&iclog->ic_bio);
  1241. /*
  1242. * Drop the lock to signal that we are done. Nothing references the
  1243. * iclog after this, so an unmount waiting on this lock can now tear it
  1244. * down safely. As such, it is unsafe to reference the iclog after the
  1245. * unlock as we could race with it being freed.
  1246. */
  1247. up(&iclog->ic_sema);
  1248. }
  1249. /*
  1250. * Return size of each in-core log record buffer.
  1251. *
  1252. * All machines get 8 x 32kB buffers by default, unless tuned otherwise.
  1253. *
  1254. * If the filesystem blocksize is too large, we may need to choose a
  1255. * larger size since the directory code currently logs entire blocks.
  1256. */
  1257. STATIC void
  1258. xlog_get_iclog_buffer_size(
  1259. struct xfs_mount *mp,
  1260. struct xlog *log)
  1261. {
  1262. if (mp->m_logbufs <= 0)
  1263. mp->m_logbufs = XLOG_MAX_ICLOGS;
  1264. if (mp->m_logbsize <= 0)
  1265. mp->m_logbsize = XLOG_BIG_RECORD_BSIZE;
  1266. log->l_iclog_bufs = mp->m_logbufs;
  1267. log->l_iclog_size = mp->m_logbsize;
  1268. /*
  1269. * # headers = size / 32k - one header holds cycles from 32k of data.
  1270. */
  1271. log->l_iclog_heads =
  1272. DIV_ROUND_UP(mp->m_logbsize, XLOG_HEADER_CYCLE_SIZE);
  1273. log->l_iclog_hsize = log->l_iclog_heads << BBSHIFT;
  1274. }
  1275. void
  1276. xfs_log_work_queue(
  1277. struct xfs_mount *mp)
  1278. {
  1279. queue_delayed_work(mp->m_sync_workqueue, &mp->m_log->l_work,
  1280. msecs_to_jiffies(xfs_syncd_centisecs * 10));
  1281. }
  1282. /*
  1283. * Clear the log incompat flags if we have the opportunity.
  1284. *
  1285. * This only happens if we're about to log the second dummy transaction as part
  1286. * of covering the log and we can get the log incompat feature usage lock.
  1287. */
  1288. static inline void
  1289. xlog_clear_incompat(
  1290. struct xlog *log)
  1291. {
  1292. struct xfs_mount *mp = log->l_mp;
  1293. if (!xfs_sb_has_incompat_log_feature(&mp->m_sb,
  1294. XFS_SB_FEAT_INCOMPAT_LOG_ALL))
  1295. return;
  1296. if (log->l_covered_state != XLOG_STATE_COVER_DONE2)
  1297. return;
  1298. if (!down_write_trylock(&log->l_incompat_users))
  1299. return;
  1300. xfs_clear_incompat_log_features(mp);
  1301. up_write(&log->l_incompat_users);
  1302. }
  1303. /*
  1304. * Every sync period we need to unpin all items in the AIL and push them to
  1305. * disk. If there is nothing dirty, then we might need to cover the log to
  1306. * indicate that the filesystem is idle.
  1307. */
  1308. static void
  1309. xfs_log_worker(
  1310. struct work_struct *work)
  1311. {
  1312. struct xlog *log = container_of(to_delayed_work(work),
  1313. struct xlog, l_work);
  1314. struct xfs_mount *mp = log->l_mp;
  1315. /* dgc: errors ignored - not fatal and nowhere to report them */
  1316. if (xfs_fs_writable(mp, SB_FREEZE_WRITE) && xfs_log_need_covered(mp)) {
  1317. /*
  1318. * Dump a transaction into the log that contains no real change.
  1319. * This is needed to stamp the current tail LSN into the log
  1320. * during the covering operation.
  1321. *
  1322. * We cannot use an inode here for this - that will push dirty
  1323. * state back up into the VFS and then periodic inode flushing
  1324. * will prevent log covering from making progress. Hence we
  1325. * synchronously log the superblock instead to ensure the
  1326. * superblock is immediately unpinned and can be written back.
  1327. */
  1328. xlog_clear_incompat(log);
  1329. xfs_sync_sb(mp, true);
  1330. } else
  1331. xfs_log_force(mp, 0);
  1332. /* start pushing all the metadata that is currently dirty */
  1333. xfs_ail_push_all(mp->m_ail);
  1334. /* queue us up again */
  1335. xfs_log_work_queue(mp);
  1336. }
  1337. /*
  1338. * This routine initializes some of the log structure for a given mount point.
  1339. * Its primary purpose is to fill in enough, so recovery can occur. However,
  1340. * some other stuff may be filled in too.
  1341. */
  1342. STATIC struct xlog *
  1343. xlog_alloc_log(
  1344. struct xfs_mount *mp,
  1345. struct xfs_buftarg *log_target,
  1346. xfs_daddr_t blk_offset,
  1347. int num_bblks)
  1348. {
  1349. struct xlog *log;
  1350. xlog_rec_header_t *head;
  1351. xlog_in_core_t **iclogp;
  1352. xlog_in_core_t *iclog, *prev_iclog=NULL;
  1353. int i;
  1354. int error = -ENOMEM;
  1355. uint log2_size = 0;
  1356. log = kmem_zalloc(sizeof(struct xlog), KM_MAYFAIL);
  1357. if (!log) {
  1358. xfs_warn(mp, "Log allocation failed: No memory!");
  1359. goto out;
  1360. }
  1361. log->l_mp = mp;
  1362. log->l_targ = log_target;
  1363. log->l_logsize = BBTOB(num_bblks);
  1364. log->l_logBBstart = blk_offset;
  1365. log->l_logBBsize = num_bblks;
  1366. log->l_covered_state = XLOG_STATE_COVER_IDLE;
  1367. set_bit(XLOG_ACTIVE_RECOVERY, &log->l_opstate);
  1368. INIT_DELAYED_WORK(&log->l_work, xfs_log_worker);
  1369. log->l_prev_block = -1;
  1370. /* log->l_tail_lsn = 0x100000000LL; cycle = 1; current block = 0 */
  1371. xlog_assign_atomic_lsn(&log->l_tail_lsn, 1, 0);
  1372. xlog_assign_atomic_lsn(&log->l_last_sync_lsn, 1, 0);
  1373. log->l_curr_cycle = 1; /* 0 is bad since this is initial value */
  1374. if (xfs_has_logv2(mp) && mp->m_sb.sb_logsunit > 1)
  1375. log->l_iclog_roundoff = mp->m_sb.sb_logsunit;
  1376. else
  1377. log->l_iclog_roundoff = BBSIZE;
  1378. xlog_grant_head_init(&log->l_reserve_head);
  1379. xlog_grant_head_init(&log->l_write_head);
  1380. error = -EFSCORRUPTED;
  1381. if (xfs_has_sector(mp)) {
  1382. log2_size = mp->m_sb.sb_logsectlog;
  1383. if (log2_size < BBSHIFT) {
  1384. xfs_warn(mp, "Log sector size too small (0x%x < 0x%x)",
  1385. log2_size, BBSHIFT);
  1386. goto out_free_log;
  1387. }
  1388. log2_size -= BBSHIFT;
  1389. if (log2_size > mp->m_sectbb_log) {
  1390. xfs_warn(mp, "Log sector size too large (0x%x > 0x%x)",
  1391. log2_size, mp->m_sectbb_log);
  1392. goto out_free_log;
  1393. }
  1394. /* for larger sector sizes, must have v2 or external log */
  1395. if (log2_size && log->l_logBBstart > 0 &&
  1396. !xfs_has_logv2(mp)) {
  1397. xfs_warn(mp,
  1398. "log sector size (0x%x) invalid for configuration.",
  1399. log2_size);
  1400. goto out_free_log;
  1401. }
  1402. }
  1403. log->l_sectBBsize = 1 << log2_size;
  1404. init_rwsem(&log->l_incompat_users);
  1405. xlog_get_iclog_buffer_size(mp, log);
  1406. spin_lock_init(&log->l_icloglock);
  1407. init_waitqueue_head(&log->l_flush_wait);
  1408. iclogp = &log->l_iclog;
  1409. /*
  1410. * The amount of memory to allocate for the iclog structure is
  1411. * rather funky due to the way the structure is defined. It is
  1412. * done this way so that we can use different sizes for machines
  1413. * with different amounts of memory. See the definition of
  1414. * xlog_in_core_t in xfs_log_priv.h for details.
  1415. */
  1416. ASSERT(log->l_iclog_size >= 4096);
  1417. for (i = 0; i < log->l_iclog_bufs; i++) {
  1418. size_t bvec_size = howmany(log->l_iclog_size, PAGE_SIZE) *
  1419. sizeof(struct bio_vec);
  1420. iclog = kmem_zalloc(sizeof(*iclog) + bvec_size, KM_MAYFAIL);
  1421. if (!iclog)
  1422. goto out_free_iclog;
  1423. *iclogp = iclog;
  1424. iclog->ic_prev = prev_iclog;
  1425. prev_iclog = iclog;
  1426. iclog->ic_data = kvzalloc(log->l_iclog_size,
  1427. GFP_KERNEL | __GFP_RETRY_MAYFAIL);
  1428. if (!iclog->ic_data)
  1429. goto out_free_iclog;
  1430. head = &iclog->ic_header;
  1431. memset(head, 0, sizeof(xlog_rec_header_t));
  1432. head->h_magicno = cpu_to_be32(XLOG_HEADER_MAGIC_NUM);
  1433. head->h_version = cpu_to_be32(
  1434. xfs_has_logv2(log->l_mp) ? 2 : 1);
  1435. head->h_size = cpu_to_be32(log->l_iclog_size);
  1436. /* new fields */
  1437. head->h_fmt = cpu_to_be32(XLOG_FMT);
  1438. memcpy(&head->h_fs_uuid, &mp->m_sb.sb_uuid, sizeof(uuid_t));
  1439. iclog->ic_size = log->l_iclog_size - log->l_iclog_hsize;
  1440. iclog->ic_state = XLOG_STATE_ACTIVE;
  1441. iclog->ic_log = log;
  1442. atomic_set(&iclog->ic_refcnt, 0);
  1443. INIT_LIST_HEAD(&iclog->ic_callbacks);
  1444. iclog->ic_datap = (void *)iclog->ic_data + log->l_iclog_hsize;
  1445. init_waitqueue_head(&iclog->ic_force_wait);
  1446. init_waitqueue_head(&iclog->ic_write_wait);
  1447. INIT_WORK(&iclog->ic_end_io_work, xlog_ioend_work);
  1448. sema_init(&iclog->ic_sema, 1);
  1449. iclogp = &iclog->ic_next;
  1450. }
  1451. *iclogp = log->l_iclog; /* complete ring */
  1452. log->l_iclog->ic_prev = prev_iclog; /* re-write 1st prev ptr */
  1453. log->l_ioend_workqueue = alloc_workqueue("xfs-log/%s",
  1454. XFS_WQFLAGS(WQ_FREEZABLE | WQ_MEM_RECLAIM |
  1455. WQ_HIGHPRI),
  1456. 0, mp->m_super->s_id);
  1457. if (!log->l_ioend_workqueue)
  1458. goto out_free_iclog;
  1459. error = xlog_cil_init(log);
  1460. if (error)
  1461. goto out_destroy_workqueue;
  1462. return log;
  1463. out_destroy_workqueue:
  1464. destroy_workqueue(log->l_ioend_workqueue);
  1465. out_free_iclog:
  1466. for (iclog = log->l_iclog; iclog; iclog = prev_iclog) {
  1467. prev_iclog = iclog->ic_next;
  1468. kmem_free(iclog->ic_data);
  1469. kmem_free(iclog);
  1470. if (prev_iclog == log->l_iclog)
  1471. break;
  1472. }
  1473. out_free_log:
  1474. kmem_free(log);
  1475. out:
  1476. return ERR_PTR(error);
  1477. } /* xlog_alloc_log */
  1478. /*
  1479. * Compute the LSN that we'd need to push the log tail towards in order to have
  1480. * (a) enough on-disk log space to log the number of bytes specified, (b) at
  1481. * least 25% of the log space free, and (c) at least 256 blocks free. If the
  1482. * log free space already meets all three thresholds, this function returns
  1483. * NULLCOMMITLSN.
  1484. */
  1485. xfs_lsn_t
  1486. xlog_grant_push_threshold(
  1487. struct xlog *log,
  1488. int need_bytes)
  1489. {
  1490. xfs_lsn_t threshold_lsn = 0;
  1491. xfs_lsn_t last_sync_lsn;
  1492. int free_blocks;
  1493. int free_bytes;
  1494. int threshold_block;
  1495. int threshold_cycle;
  1496. int free_threshold;
  1497. ASSERT(BTOBB(need_bytes) < log->l_logBBsize);
  1498. free_bytes = xlog_space_left(log, &log->l_reserve_head.grant);
  1499. free_blocks = BTOBBT(free_bytes);
  1500. /*
  1501. * Set the threshold for the minimum number of free blocks in the
  1502. * log to the maximum of what the caller needs, one quarter of the
  1503. * log, and 256 blocks.
  1504. */
  1505. free_threshold = BTOBB(need_bytes);
  1506. free_threshold = max(free_threshold, (log->l_logBBsize >> 2));
  1507. free_threshold = max(free_threshold, 256);
  1508. if (free_blocks >= free_threshold)
  1509. return NULLCOMMITLSN;
  1510. xlog_crack_atomic_lsn(&log->l_tail_lsn, &threshold_cycle,
  1511. &threshold_block);
  1512. threshold_block += free_threshold;
  1513. if (threshold_block >= log->l_logBBsize) {
  1514. threshold_block -= log->l_logBBsize;
  1515. threshold_cycle += 1;
  1516. }
  1517. threshold_lsn = xlog_assign_lsn(threshold_cycle,
  1518. threshold_block);
  1519. /*
  1520. * Don't pass in an lsn greater than the lsn of the last
  1521. * log record known to be on disk. Use a snapshot of the last sync lsn
  1522. * so that it doesn't change between the compare and the set.
  1523. */
  1524. last_sync_lsn = atomic64_read(&log->l_last_sync_lsn);
  1525. if (XFS_LSN_CMP(threshold_lsn, last_sync_lsn) > 0)
  1526. threshold_lsn = last_sync_lsn;
  1527. return threshold_lsn;
  1528. }
  1529. /*
  1530. * Push the tail of the log if we need to do so to maintain the free log space
  1531. * thresholds set out by xlog_grant_push_threshold. We may need to adopt a
  1532. * policy which pushes on an lsn which is further along in the log once we
  1533. * reach the high water mark. In this manner, we would be creating a low water
  1534. * mark.
  1535. */
  1536. STATIC void
  1537. xlog_grant_push_ail(
  1538. struct xlog *log,
  1539. int need_bytes)
  1540. {
  1541. xfs_lsn_t threshold_lsn;
  1542. threshold_lsn = xlog_grant_push_threshold(log, need_bytes);
  1543. if (threshold_lsn == NULLCOMMITLSN || xlog_is_shutdown(log))
  1544. return;
  1545. /*
  1546. * Get the transaction layer to kick the dirty buffers out to
  1547. * disk asynchronously. No point in trying to do this if
  1548. * the filesystem is shutting down.
  1549. */
  1550. xfs_ail_push(log->l_ailp, threshold_lsn);
  1551. }
  1552. /*
  1553. * Stamp cycle number in every block
  1554. */
  1555. STATIC void
  1556. xlog_pack_data(
  1557. struct xlog *log,
  1558. struct xlog_in_core *iclog,
  1559. int roundoff)
  1560. {
  1561. int i, j, k;
  1562. int size = iclog->ic_offset + roundoff;
  1563. __be32 cycle_lsn;
  1564. char *dp;
  1565. cycle_lsn = CYCLE_LSN_DISK(iclog->ic_header.h_lsn);
  1566. dp = iclog->ic_datap;
  1567. for (i = 0; i < BTOBB(size); i++) {
  1568. if (i >= (XLOG_HEADER_CYCLE_SIZE / BBSIZE))
  1569. break;
  1570. iclog->ic_header.h_cycle_data[i] = *(__be32 *)dp;
  1571. *(__be32 *)dp = cycle_lsn;
  1572. dp += BBSIZE;
  1573. }
  1574. if (xfs_has_logv2(log->l_mp)) {
  1575. xlog_in_core_2_t *xhdr = iclog->ic_data;
  1576. for ( ; i < BTOBB(size); i++) {
  1577. j = i / (XLOG_HEADER_CYCLE_SIZE / BBSIZE);
  1578. k = i % (XLOG_HEADER_CYCLE_SIZE / BBSIZE);
  1579. xhdr[j].hic_xheader.xh_cycle_data[k] = *(__be32 *)dp;
  1580. *(__be32 *)dp = cycle_lsn;
  1581. dp += BBSIZE;
  1582. }
  1583. for (i = 1; i < log->l_iclog_heads; i++)
  1584. xhdr[i].hic_xheader.xh_cycle = cycle_lsn;
  1585. }
  1586. }
  1587. /*
  1588. * Calculate the checksum for a log buffer.
  1589. *
  1590. * This is a little more complicated than it should be because the various
  1591. * headers and the actual data are non-contiguous.
  1592. */
  1593. __le32
  1594. xlog_cksum(
  1595. struct xlog *log,
  1596. struct xlog_rec_header *rhead,
  1597. char *dp,
  1598. int size)
  1599. {
  1600. uint32_t crc;
  1601. /* first generate the crc for the record header ... */
  1602. crc = xfs_start_cksum_update((char *)rhead,
  1603. sizeof(struct xlog_rec_header),
  1604. offsetof(struct xlog_rec_header, h_crc));
  1605. /* ... then for additional cycle data for v2 logs ... */
  1606. if (xfs_has_logv2(log->l_mp)) {
  1607. union xlog_in_core2 *xhdr = (union xlog_in_core2 *)rhead;
  1608. int i;
  1609. int xheads;
  1610. xheads = DIV_ROUND_UP(size, XLOG_HEADER_CYCLE_SIZE);
  1611. for (i = 1; i < xheads; i++) {
  1612. crc = crc32c(crc, &xhdr[i].hic_xheader,
  1613. sizeof(struct xlog_rec_ext_header));
  1614. }
  1615. }
  1616. /* ... and finally for the payload */
  1617. crc = crc32c(crc, dp, size);
  1618. return xfs_end_cksum(crc);
  1619. }
  1620. static void
  1621. xlog_bio_end_io(
  1622. struct bio *bio)
  1623. {
  1624. struct xlog_in_core *iclog = bio->bi_private;
  1625. queue_work(iclog->ic_log->l_ioend_workqueue,
  1626. &iclog->ic_end_io_work);
  1627. }
  1628. static int
  1629. xlog_map_iclog_data(
  1630. struct bio *bio,
  1631. void *data,
  1632. size_t count)
  1633. {
  1634. do {
  1635. struct page *page = kmem_to_page(data);
  1636. unsigned int off = offset_in_page(data);
  1637. size_t len = min_t(size_t, count, PAGE_SIZE - off);
  1638. if (bio_add_page(bio, page, len, off) != len)
  1639. return -EIO;
  1640. data += len;
  1641. count -= len;
  1642. } while (count);
  1643. return 0;
  1644. }
  1645. STATIC void
  1646. xlog_write_iclog(
  1647. struct xlog *log,
  1648. struct xlog_in_core *iclog,
  1649. uint64_t bno,
  1650. unsigned int count)
  1651. {
  1652. ASSERT(bno < log->l_logBBsize);
  1653. trace_xlog_iclog_write(iclog, _RET_IP_);
  1654. /*
  1655. * We lock the iclogbufs here so that we can serialise against I/O
  1656. * completion during unmount. We might be processing a shutdown
  1657. * triggered during unmount, and that can occur asynchronously to the
  1658. * unmount thread, and hence we need to ensure that completes before
  1659. * tearing down the iclogbufs. Hence we need to hold the buffer lock
  1660. * across the log IO to archieve that.
  1661. */
  1662. down(&iclog->ic_sema);
  1663. if (xlog_is_shutdown(log)) {
  1664. /*
  1665. * It would seem logical to return EIO here, but we rely on
  1666. * the log state machine to propagate I/O errors instead of
  1667. * doing it here. We kick of the state machine and unlock
  1668. * the buffer manually, the code needs to be kept in sync
  1669. * with the I/O completion path.
  1670. */
  1671. xlog_state_done_syncing(iclog);
  1672. up(&iclog->ic_sema);
  1673. return;
  1674. }
  1675. /*
  1676. * We use REQ_SYNC | REQ_IDLE here to tell the block layer the are more
  1677. * IOs coming immediately after this one. This prevents the block layer
  1678. * writeback throttle from throttling log writes behind background
  1679. * metadata writeback and causing priority inversions.
  1680. */
  1681. bio_init(&iclog->ic_bio, log->l_targ->bt_bdev, iclog->ic_bvec,
  1682. howmany(count, PAGE_SIZE),
  1683. REQ_OP_WRITE | REQ_META | REQ_SYNC | REQ_IDLE);
  1684. iclog->ic_bio.bi_iter.bi_sector = log->l_logBBstart + bno;
  1685. iclog->ic_bio.bi_end_io = xlog_bio_end_io;
  1686. iclog->ic_bio.bi_private = iclog;
  1687. if (iclog->ic_flags & XLOG_ICL_NEED_FLUSH) {
  1688. iclog->ic_bio.bi_opf |= REQ_PREFLUSH;
  1689. /*
  1690. * For external log devices, we also need to flush the data
  1691. * device cache first to ensure all metadata writeback covered
  1692. * by the LSN in this iclog is on stable storage. This is slow,
  1693. * but it *must* complete before we issue the external log IO.
  1694. *
  1695. * If the flush fails, we cannot conclude that past metadata
  1696. * writeback from the log succeeded. Repeating the flush is
  1697. * not possible, hence we must shut down with log IO error to
  1698. * avoid shutdown re-entering this path and erroring out again.
  1699. */
  1700. if (log->l_targ != log->l_mp->m_ddev_targp &&
  1701. blkdev_issue_flush(log->l_mp->m_ddev_targp->bt_bdev)) {
  1702. xlog_force_shutdown(log, SHUTDOWN_LOG_IO_ERROR);
  1703. return;
  1704. }
  1705. }
  1706. if (iclog->ic_flags & XLOG_ICL_NEED_FUA)
  1707. iclog->ic_bio.bi_opf |= REQ_FUA;
  1708. iclog->ic_flags &= ~(XLOG_ICL_NEED_FLUSH | XLOG_ICL_NEED_FUA);
  1709. if (xlog_map_iclog_data(&iclog->ic_bio, iclog->ic_data, count)) {
  1710. xlog_force_shutdown(log, SHUTDOWN_LOG_IO_ERROR);
  1711. return;
  1712. }
  1713. if (is_vmalloc_addr(iclog->ic_data))
  1714. flush_kernel_vmap_range(iclog->ic_data, count);
  1715. /*
  1716. * If this log buffer would straddle the end of the log we will have
  1717. * to split it up into two bios, so that we can continue at the start.
  1718. */
  1719. if (bno + BTOBB(count) > log->l_logBBsize) {
  1720. struct bio *split;
  1721. split = bio_split(&iclog->ic_bio, log->l_logBBsize - bno,
  1722. GFP_NOIO, &fs_bio_set);
  1723. bio_chain(split, &iclog->ic_bio);
  1724. submit_bio(split);
  1725. /* restart at logical offset zero for the remainder */
  1726. iclog->ic_bio.bi_iter.bi_sector = log->l_logBBstart;
  1727. }
  1728. submit_bio(&iclog->ic_bio);
  1729. }
  1730. /*
  1731. * We need to bump cycle number for the part of the iclog that is
  1732. * written to the start of the log. Watch out for the header magic
  1733. * number case, though.
  1734. */
  1735. static void
  1736. xlog_split_iclog(
  1737. struct xlog *log,
  1738. void *data,
  1739. uint64_t bno,
  1740. unsigned int count)
  1741. {
  1742. unsigned int split_offset = BBTOB(log->l_logBBsize - bno);
  1743. unsigned int i;
  1744. for (i = split_offset; i < count; i += BBSIZE) {
  1745. uint32_t cycle = get_unaligned_be32(data + i);
  1746. if (++cycle == XLOG_HEADER_MAGIC_NUM)
  1747. cycle++;
  1748. put_unaligned_be32(cycle, data + i);
  1749. }
  1750. }
  1751. static int
  1752. xlog_calc_iclog_size(
  1753. struct xlog *log,
  1754. struct xlog_in_core *iclog,
  1755. uint32_t *roundoff)
  1756. {
  1757. uint32_t count_init, count;
  1758. /* Add for LR header */
  1759. count_init = log->l_iclog_hsize + iclog->ic_offset;
  1760. count = roundup(count_init, log->l_iclog_roundoff);
  1761. *roundoff = count - count_init;
  1762. ASSERT(count >= count_init);
  1763. ASSERT(*roundoff < log->l_iclog_roundoff);
  1764. return count;
  1765. }
  1766. /*
  1767. * Flush out the in-core log (iclog) to the on-disk log in an asynchronous
  1768. * fashion. Previously, we should have moved the current iclog
  1769. * ptr in the log to point to the next available iclog. This allows further
  1770. * write to continue while this code syncs out an iclog ready to go.
  1771. * Before an in-core log can be written out, the data section must be scanned
  1772. * to save away the 1st word of each BBSIZE block into the header. We replace
  1773. * it with the current cycle count. Each BBSIZE block is tagged with the
  1774. * cycle count because there in an implicit assumption that drives will
  1775. * guarantee that entire 512 byte blocks get written at once. In other words,
  1776. * we can't have part of a 512 byte block written and part not written. By
  1777. * tagging each block, we will know which blocks are valid when recovering
  1778. * after an unclean shutdown.
  1779. *
  1780. * This routine is single threaded on the iclog. No other thread can be in
  1781. * this routine with the same iclog. Changing contents of iclog can there-
  1782. * fore be done without grabbing the state machine lock. Updating the global
  1783. * log will require grabbing the lock though.
  1784. *
  1785. * The entire log manager uses a logical block numbering scheme. Only
  1786. * xlog_write_iclog knows about the fact that the log may not start with
  1787. * block zero on a given device.
  1788. */
  1789. STATIC void
  1790. xlog_sync(
  1791. struct xlog *log,
  1792. struct xlog_in_core *iclog,
  1793. struct xlog_ticket *ticket)
  1794. {
  1795. unsigned int count; /* byte count of bwrite */
  1796. unsigned int roundoff; /* roundoff to BB or stripe */
  1797. uint64_t bno;
  1798. unsigned int size;
  1799. ASSERT(atomic_read(&iclog->ic_refcnt) == 0);
  1800. trace_xlog_iclog_sync(iclog, _RET_IP_);
  1801. count = xlog_calc_iclog_size(log, iclog, &roundoff);
  1802. /*
  1803. * If we have a ticket, account for the roundoff via the ticket
  1804. * reservation to avoid touching the hot grant heads needlessly.
  1805. * Otherwise, we have to move grant heads directly.
  1806. */
  1807. if (ticket) {
  1808. ticket->t_curr_res -= roundoff;
  1809. } else {
  1810. xlog_grant_add_space(log, &log->l_reserve_head.grant, roundoff);
  1811. xlog_grant_add_space(log, &log->l_write_head.grant, roundoff);
  1812. }
  1813. /* put cycle number in every block */
  1814. xlog_pack_data(log, iclog, roundoff);
  1815. /* real byte length */
  1816. size = iclog->ic_offset;
  1817. if (xfs_has_logv2(log->l_mp))
  1818. size += roundoff;
  1819. iclog->ic_header.h_len = cpu_to_be32(size);
  1820. XFS_STATS_INC(log->l_mp, xs_log_writes);
  1821. XFS_STATS_ADD(log->l_mp, xs_log_blocks, BTOBB(count));
  1822. bno = BLOCK_LSN(be64_to_cpu(iclog->ic_header.h_lsn));
  1823. /* Do we need to split this write into 2 parts? */
  1824. if (bno + BTOBB(count) > log->l_logBBsize)
  1825. xlog_split_iclog(log, &iclog->ic_header, bno, count);
  1826. /* calculcate the checksum */
  1827. iclog->ic_header.h_crc = xlog_cksum(log, &iclog->ic_header,
  1828. iclog->ic_datap, size);
  1829. /*
  1830. * Intentionally corrupt the log record CRC based on the error injection
  1831. * frequency, if defined. This facilitates testing log recovery in the
  1832. * event of torn writes. Hence, set the IOABORT state to abort the log
  1833. * write on I/O completion and shutdown the fs. The subsequent mount
  1834. * detects the bad CRC and attempts to recover.
  1835. */
  1836. #ifdef DEBUG
  1837. if (XFS_TEST_ERROR(false, log->l_mp, XFS_ERRTAG_LOG_BAD_CRC)) {
  1838. iclog->ic_header.h_crc &= cpu_to_le32(0xAAAAAAAA);
  1839. iclog->ic_fail_crc = true;
  1840. xfs_warn(log->l_mp,
  1841. "Intentionally corrupted log record at LSN 0x%llx. Shutdown imminent.",
  1842. be64_to_cpu(iclog->ic_header.h_lsn));
  1843. }
  1844. #endif
  1845. xlog_verify_iclog(log, iclog, count);
  1846. xlog_write_iclog(log, iclog, bno, count);
  1847. }
  1848. /*
  1849. * Deallocate a log structure
  1850. */
  1851. STATIC void
  1852. xlog_dealloc_log(
  1853. struct xlog *log)
  1854. {
  1855. xlog_in_core_t *iclog, *next_iclog;
  1856. int i;
  1857. /*
  1858. * Cycle all the iclogbuf locks to make sure all log IO completion
  1859. * is done before we tear down these buffers.
  1860. */
  1861. iclog = log->l_iclog;
  1862. for (i = 0; i < log->l_iclog_bufs; i++) {
  1863. down(&iclog->ic_sema);
  1864. up(&iclog->ic_sema);
  1865. iclog = iclog->ic_next;
  1866. }
  1867. /*
  1868. * Destroy the CIL after waiting for iclog IO completion because an
  1869. * iclog EIO error will try to shut down the log, which accesses the
  1870. * CIL to wake up the waiters.
  1871. */
  1872. xlog_cil_destroy(log);
  1873. iclog = log->l_iclog;
  1874. for (i = 0; i < log->l_iclog_bufs; i++) {
  1875. next_iclog = iclog->ic_next;
  1876. kmem_free(iclog->ic_data);
  1877. kmem_free(iclog);
  1878. iclog = next_iclog;
  1879. }
  1880. log->l_mp->m_log = NULL;
  1881. destroy_workqueue(log->l_ioend_workqueue);
  1882. kmem_free(log);
  1883. }
  1884. /*
  1885. * Update counters atomically now that memcpy is done.
  1886. */
  1887. static inline void
  1888. xlog_state_finish_copy(
  1889. struct xlog *log,
  1890. struct xlog_in_core *iclog,
  1891. int record_cnt,
  1892. int copy_bytes)
  1893. {
  1894. lockdep_assert_held(&log->l_icloglock);
  1895. be32_add_cpu(&iclog->ic_header.h_num_logops, record_cnt);
  1896. iclog->ic_offset += copy_bytes;
  1897. }
  1898. /*
  1899. * print out info relating to regions written which consume
  1900. * the reservation
  1901. */
  1902. void
  1903. xlog_print_tic_res(
  1904. struct xfs_mount *mp,
  1905. struct xlog_ticket *ticket)
  1906. {
  1907. xfs_warn(mp, "ticket reservation summary:");
  1908. xfs_warn(mp, " unit res = %d bytes", ticket->t_unit_res);
  1909. xfs_warn(mp, " current res = %d bytes", ticket->t_curr_res);
  1910. xfs_warn(mp, " original count = %d", ticket->t_ocnt);
  1911. xfs_warn(mp, " remaining count = %d", ticket->t_cnt);
  1912. }
  1913. /*
  1914. * Print a summary of the transaction.
  1915. */
  1916. void
  1917. xlog_print_trans(
  1918. struct xfs_trans *tp)
  1919. {
  1920. struct xfs_mount *mp = tp->t_mountp;
  1921. struct xfs_log_item *lip;
  1922. /* dump core transaction and ticket info */
  1923. xfs_warn(mp, "transaction summary:");
  1924. xfs_warn(mp, " log res = %d", tp->t_log_res);
  1925. xfs_warn(mp, " log count = %d", tp->t_log_count);
  1926. xfs_warn(mp, " flags = 0x%x", tp->t_flags);
  1927. xlog_print_tic_res(mp, tp->t_ticket);
  1928. /* dump each log item */
  1929. list_for_each_entry(lip, &tp->t_items, li_trans) {
  1930. struct xfs_log_vec *lv = lip->li_lv;
  1931. struct xfs_log_iovec *vec;
  1932. int i;
  1933. xfs_warn(mp, "log item: ");
  1934. xfs_warn(mp, " type = 0x%x", lip->li_type);
  1935. xfs_warn(mp, " flags = 0x%lx", lip->li_flags);
  1936. if (!lv)
  1937. continue;
  1938. xfs_warn(mp, " niovecs = %d", lv->lv_niovecs);
  1939. xfs_warn(mp, " size = %d", lv->lv_size);
  1940. xfs_warn(mp, " bytes = %d", lv->lv_bytes);
  1941. xfs_warn(mp, " buf len = %d", lv->lv_buf_len);
  1942. /* dump each iovec for the log item */
  1943. vec = lv->lv_iovecp;
  1944. for (i = 0; i < lv->lv_niovecs; i++) {
  1945. int dumplen = min(vec->i_len, 32);
  1946. xfs_warn(mp, " iovec[%d]", i);
  1947. xfs_warn(mp, " type = 0x%x", vec->i_type);
  1948. xfs_warn(mp, " len = %d", vec->i_len);
  1949. xfs_warn(mp, " first %d bytes of iovec[%d]:", dumplen, i);
  1950. xfs_hex_dump(vec->i_addr, dumplen);
  1951. vec++;
  1952. }
  1953. }
  1954. }
  1955. static inline void
  1956. xlog_write_iovec(
  1957. struct xlog_in_core *iclog,
  1958. uint32_t *log_offset,
  1959. void *data,
  1960. uint32_t write_len,
  1961. int *bytes_left,
  1962. uint32_t *record_cnt,
  1963. uint32_t *data_cnt)
  1964. {
  1965. ASSERT(*log_offset < iclog->ic_log->l_iclog_size);
  1966. ASSERT(*log_offset % sizeof(int32_t) == 0);
  1967. ASSERT(write_len % sizeof(int32_t) == 0);
  1968. memcpy(iclog->ic_datap + *log_offset, data, write_len);
  1969. *log_offset += write_len;
  1970. *bytes_left -= write_len;
  1971. (*record_cnt)++;
  1972. *data_cnt += write_len;
  1973. }
  1974. /*
  1975. * Write log vectors into a single iclog which is guaranteed by the caller
  1976. * to have enough space to write the entire log vector into.
  1977. */
  1978. static void
  1979. xlog_write_full(
  1980. struct xfs_log_vec *lv,
  1981. struct xlog_ticket *ticket,
  1982. struct xlog_in_core *iclog,
  1983. uint32_t *log_offset,
  1984. uint32_t *len,
  1985. uint32_t *record_cnt,
  1986. uint32_t *data_cnt)
  1987. {
  1988. int index;
  1989. ASSERT(*log_offset + *len <= iclog->ic_size ||
  1990. iclog->ic_state == XLOG_STATE_WANT_SYNC);
  1991. /*
  1992. * Ordered log vectors have no regions to write so this
  1993. * loop will naturally skip them.
  1994. */
  1995. for (index = 0; index < lv->lv_niovecs; index++) {
  1996. struct xfs_log_iovec *reg = &lv->lv_iovecp[index];
  1997. struct xlog_op_header *ophdr = reg->i_addr;
  1998. ophdr->oh_tid = cpu_to_be32(ticket->t_tid);
  1999. xlog_write_iovec(iclog, log_offset, reg->i_addr,
  2000. reg->i_len, len, record_cnt, data_cnt);
  2001. }
  2002. }
  2003. static int
  2004. xlog_write_get_more_iclog_space(
  2005. struct xlog_ticket *ticket,
  2006. struct xlog_in_core **iclogp,
  2007. uint32_t *log_offset,
  2008. uint32_t len,
  2009. uint32_t *record_cnt,
  2010. uint32_t *data_cnt)
  2011. {
  2012. struct xlog_in_core *iclog = *iclogp;
  2013. struct xlog *log = iclog->ic_log;
  2014. int error;
  2015. spin_lock(&log->l_icloglock);
  2016. ASSERT(iclog->ic_state == XLOG_STATE_WANT_SYNC);
  2017. xlog_state_finish_copy(log, iclog, *record_cnt, *data_cnt);
  2018. error = xlog_state_release_iclog(log, iclog, ticket);
  2019. spin_unlock(&log->l_icloglock);
  2020. if (error)
  2021. return error;
  2022. error = xlog_state_get_iclog_space(log, len, &iclog, ticket,
  2023. log_offset);
  2024. if (error)
  2025. return error;
  2026. *record_cnt = 0;
  2027. *data_cnt = 0;
  2028. *iclogp = iclog;
  2029. return 0;
  2030. }
  2031. /*
  2032. * Write log vectors into a single iclog which is smaller than the current chain
  2033. * length. We write until we cannot fit a full record into the remaining space
  2034. * and then stop. We return the log vector that is to be written that cannot
  2035. * wholly fit in the iclog.
  2036. */
  2037. static int
  2038. xlog_write_partial(
  2039. struct xfs_log_vec *lv,
  2040. struct xlog_ticket *ticket,
  2041. struct xlog_in_core **iclogp,
  2042. uint32_t *log_offset,
  2043. uint32_t *len,
  2044. uint32_t *record_cnt,
  2045. uint32_t *data_cnt)
  2046. {
  2047. struct xlog_in_core *iclog = *iclogp;
  2048. struct xlog_op_header *ophdr;
  2049. int index = 0;
  2050. uint32_t rlen;
  2051. int error;
  2052. /* walk the logvec, copying until we run out of space in the iclog */
  2053. for (index = 0; index < lv->lv_niovecs; index++) {
  2054. struct xfs_log_iovec *reg = &lv->lv_iovecp[index];
  2055. uint32_t reg_offset = 0;
  2056. /*
  2057. * The first region of a continuation must have a non-zero
  2058. * length otherwise log recovery will just skip over it and
  2059. * start recovering from the next opheader it finds. Because we
  2060. * mark the next opheader as a continuation, recovery will then
  2061. * incorrectly add the continuation to the previous region and
  2062. * that breaks stuff.
  2063. *
  2064. * Hence if there isn't space for region data after the
  2065. * opheader, then we need to start afresh with a new iclog.
  2066. */
  2067. if (iclog->ic_size - *log_offset <=
  2068. sizeof(struct xlog_op_header)) {
  2069. error = xlog_write_get_more_iclog_space(ticket,
  2070. &iclog, log_offset, *len, record_cnt,
  2071. data_cnt);
  2072. if (error)
  2073. return error;
  2074. }
  2075. ophdr = reg->i_addr;
  2076. rlen = min_t(uint32_t, reg->i_len, iclog->ic_size - *log_offset);
  2077. ophdr->oh_tid = cpu_to_be32(ticket->t_tid);
  2078. ophdr->oh_len = cpu_to_be32(rlen - sizeof(struct xlog_op_header));
  2079. if (rlen != reg->i_len)
  2080. ophdr->oh_flags |= XLOG_CONTINUE_TRANS;
  2081. xlog_write_iovec(iclog, log_offset, reg->i_addr,
  2082. rlen, len, record_cnt, data_cnt);
  2083. /* If we wrote the whole region, move to the next. */
  2084. if (rlen == reg->i_len)
  2085. continue;
  2086. /*
  2087. * We now have a partially written iovec, but it can span
  2088. * multiple iclogs so we loop here. First we release the iclog
  2089. * we currently have, then we get a new iclog and add a new
  2090. * opheader. Then we continue copying from where we were until
  2091. * we either complete the iovec or fill the iclog. If we
  2092. * complete the iovec, then we increment the index and go right
  2093. * back to the top of the outer loop. if we fill the iclog, we
  2094. * run the inner loop again.
  2095. *
  2096. * This is complicated by the tail of a region using all the
  2097. * space in an iclog and hence requiring us to release the iclog
  2098. * and get a new one before returning to the outer loop. We must
  2099. * always guarantee that we exit this inner loop with at least
  2100. * space for log transaction opheaders left in the current
  2101. * iclog, hence we cannot just terminate the loop at the end
  2102. * of the of the continuation. So we loop while there is no
  2103. * space left in the current iclog, and check for the end of the
  2104. * continuation after getting a new iclog.
  2105. */
  2106. do {
  2107. /*
  2108. * Ensure we include the continuation opheader in the
  2109. * space we need in the new iclog by adding that size
  2110. * to the length we require. This continuation opheader
  2111. * needs to be accounted to the ticket as the space it
  2112. * consumes hasn't been accounted to the lv we are
  2113. * writing.
  2114. */
  2115. error = xlog_write_get_more_iclog_space(ticket,
  2116. &iclog, log_offset,
  2117. *len + sizeof(struct xlog_op_header),
  2118. record_cnt, data_cnt);
  2119. if (error)
  2120. return error;
  2121. ophdr = iclog->ic_datap + *log_offset;
  2122. ophdr->oh_tid = cpu_to_be32(ticket->t_tid);
  2123. ophdr->oh_clientid = XFS_TRANSACTION;
  2124. ophdr->oh_res2 = 0;
  2125. ophdr->oh_flags = XLOG_WAS_CONT_TRANS;
  2126. ticket->t_curr_res -= sizeof(struct xlog_op_header);
  2127. *log_offset += sizeof(struct xlog_op_header);
  2128. *data_cnt += sizeof(struct xlog_op_header);
  2129. /*
  2130. * If rlen fits in the iclog, then end the region
  2131. * continuation. Otherwise we're going around again.
  2132. */
  2133. reg_offset += rlen;
  2134. rlen = reg->i_len - reg_offset;
  2135. if (rlen <= iclog->ic_size - *log_offset)
  2136. ophdr->oh_flags |= XLOG_END_TRANS;
  2137. else
  2138. ophdr->oh_flags |= XLOG_CONTINUE_TRANS;
  2139. rlen = min_t(uint32_t, rlen, iclog->ic_size - *log_offset);
  2140. ophdr->oh_len = cpu_to_be32(rlen);
  2141. xlog_write_iovec(iclog, log_offset,
  2142. reg->i_addr + reg_offset,
  2143. rlen, len, record_cnt, data_cnt);
  2144. } while (ophdr->oh_flags & XLOG_CONTINUE_TRANS);
  2145. }
  2146. /*
  2147. * No more iovecs remain in this logvec so return the next log vec to
  2148. * the caller so it can go back to fast path copying.
  2149. */
  2150. *iclogp = iclog;
  2151. return 0;
  2152. }
  2153. /*
  2154. * Write some region out to in-core log
  2155. *
  2156. * This will be called when writing externally provided regions or when
  2157. * writing out a commit record for a given transaction.
  2158. *
  2159. * General algorithm:
  2160. * 1. Find total length of this write. This may include adding to the
  2161. * lengths passed in.
  2162. * 2. Check whether we violate the tickets reservation.
  2163. * 3. While writing to this iclog
  2164. * A. Reserve as much space in this iclog as can get
  2165. * B. If this is first write, save away start lsn
  2166. * C. While writing this region:
  2167. * 1. If first write of transaction, write start record
  2168. * 2. Write log operation header (header per region)
  2169. * 3. Find out if we can fit entire region into this iclog
  2170. * 4. Potentially, verify destination memcpy ptr
  2171. * 5. Memcpy (partial) region
  2172. * 6. If partial copy, release iclog; otherwise, continue
  2173. * copying more regions into current iclog
  2174. * 4. Mark want sync bit (in simulation mode)
  2175. * 5. Release iclog for potential flush to on-disk log.
  2176. *
  2177. * ERRORS:
  2178. * 1. Panic if reservation is overrun. This should never happen since
  2179. * reservation amounts are generated internal to the filesystem.
  2180. * NOTES:
  2181. * 1. Tickets are single threaded data structures.
  2182. * 2. The XLOG_END_TRANS & XLOG_CONTINUE_TRANS flags are passed down to the
  2183. * syncing routine. When a single log_write region needs to span
  2184. * multiple in-core logs, the XLOG_CONTINUE_TRANS bit should be set
  2185. * on all log operation writes which don't contain the end of the
  2186. * region. The XLOG_END_TRANS bit is used for the in-core log
  2187. * operation which contains the end of the continued log_write region.
  2188. * 3. When xlog_state_get_iclog_space() grabs the rest of the current iclog,
  2189. * we don't really know exactly how much space will be used. As a result,
  2190. * we don't update ic_offset until the end when we know exactly how many
  2191. * bytes have been written out.
  2192. */
  2193. int
  2194. xlog_write(
  2195. struct xlog *log,
  2196. struct xfs_cil_ctx *ctx,
  2197. struct list_head *lv_chain,
  2198. struct xlog_ticket *ticket,
  2199. uint32_t len)
  2200. {
  2201. struct xlog_in_core *iclog = NULL;
  2202. struct xfs_log_vec *lv;
  2203. uint32_t record_cnt = 0;
  2204. uint32_t data_cnt = 0;
  2205. int error = 0;
  2206. int log_offset;
  2207. if (ticket->t_curr_res < 0) {
  2208. xfs_alert_tag(log->l_mp, XFS_PTAG_LOGRES,
  2209. "ctx ticket reservation ran out. Need to up reservation");
  2210. xlog_print_tic_res(log->l_mp, ticket);
  2211. xlog_force_shutdown(log, SHUTDOWN_LOG_IO_ERROR);
  2212. }
  2213. error = xlog_state_get_iclog_space(log, len, &iclog, ticket,
  2214. &log_offset);
  2215. if (error)
  2216. return error;
  2217. ASSERT(log_offset <= iclog->ic_size - 1);
  2218. /*
  2219. * If we have a context pointer, pass it the first iclog we are
  2220. * writing to so it can record state needed for iclog write
  2221. * ordering.
  2222. */
  2223. if (ctx)
  2224. xlog_cil_set_ctx_write_state(ctx, iclog);
  2225. list_for_each_entry(lv, lv_chain, lv_list) {
  2226. /*
  2227. * If the entire log vec does not fit in the iclog, punt it to
  2228. * the partial copy loop which can handle this case.
  2229. */
  2230. if (lv->lv_niovecs &&
  2231. lv->lv_bytes > iclog->ic_size - log_offset) {
  2232. error = xlog_write_partial(lv, ticket, &iclog,
  2233. &log_offset, &len, &record_cnt,
  2234. &data_cnt);
  2235. if (error) {
  2236. /*
  2237. * We have no iclog to release, so just return
  2238. * the error immediately.
  2239. */
  2240. return error;
  2241. }
  2242. } else {
  2243. xlog_write_full(lv, ticket, iclog, &log_offset,
  2244. &len, &record_cnt, &data_cnt);
  2245. }
  2246. }
  2247. ASSERT(len == 0);
  2248. /*
  2249. * We've already been guaranteed that the last writes will fit inside
  2250. * the current iclog, and hence it will already have the space used by
  2251. * those writes accounted to it. Hence we do not need to update the
  2252. * iclog with the number of bytes written here.
  2253. */
  2254. spin_lock(&log->l_icloglock);
  2255. xlog_state_finish_copy(log, iclog, record_cnt, 0);
  2256. error = xlog_state_release_iclog(log, iclog, ticket);
  2257. spin_unlock(&log->l_icloglock);
  2258. return error;
  2259. }
  2260. static void
  2261. xlog_state_activate_iclog(
  2262. struct xlog_in_core *iclog,
  2263. int *iclogs_changed)
  2264. {
  2265. ASSERT(list_empty_careful(&iclog->ic_callbacks));
  2266. trace_xlog_iclog_activate(iclog, _RET_IP_);
  2267. /*
  2268. * If the number of ops in this iclog indicate it just contains the
  2269. * dummy transaction, we can change state into IDLE (the second time
  2270. * around). Otherwise we should change the state into NEED a dummy.
  2271. * We don't need to cover the dummy.
  2272. */
  2273. if (*iclogs_changed == 0 &&
  2274. iclog->ic_header.h_num_logops == cpu_to_be32(XLOG_COVER_OPS)) {
  2275. *iclogs_changed = 1;
  2276. } else {
  2277. /*
  2278. * We have two dirty iclogs so start over. This could also be
  2279. * num of ops indicating this is not the dummy going out.
  2280. */
  2281. *iclogs_changed = 2;
  2282. }
  2283. iclog->ic_state = XLOG_STATE_ACTIVE;
  2284. iclog->ic_offset = 0;
  2285. iclog->ic_header.h_num_logops = 0;
  2286. memset(iclog->ic_header.h_cycle_data, 0,
  2287. sizeof(iclog->ic_header.h_cycle_data));
  2288. iclog->ic_header.h_lsn = 0;
  2289. iclog->ic_header.h_tail_lsn = 0;
  2290. }
  2291. /*
  2292. * Loop through all iclogs and mark all iclogs currently marked DIRTY as
  2293. * ACTIVE after iclog I/O has completed.
  2294. */
  2295. static void
  2296. xlog_state_activate_iclogs(
  2297. struct xlog *log,
  2298. int *iclogs_changed)
  2299. {
  2300. struct xlog_in_core *iclog = log->l_iclog;
  2301. do {
  2302. if (iclog->ic_state == XLOG_STATE_DIRTY)
  2303. xlog_state_activate_iclog(iclog, iclogs_changed);
  2304. /*
  2305. * The ordering of marking iclogs ACTIVE must be maintained, so
  2306. * an iclog doesn't become ACTIVE beyond one that is SYNCING.
  2307. */
  2308. else if (iclog->ic_state != XLOG_STATE_ACTIVE)
  2309. break;
  2310. } while ((iclog = iclog->ic_next) != log->l_iclog);
  2311. }
  2312. static int
  2313. xlog_covered_state(
  2314. int prev_state,
  2315. int iclogs_changed)
  2316. {
  2317. /*
  2318. * We go to NEED for any non-covering writes. We go to NEED2 if we just
  2319. * wrote the first covering record (DONE). We go to IDLE if we just
  2320. * wrote the second covering record (DONE2) and remain in IDLE until a
  2321. * non-covering write occurs.
  2322. */
  2323. switch (prev_state) {
  2324. case XLOG_STATE_COVER_IDLE:
  2325. if (iclogs_changed == 1)
  2326. return XLOG_STATE_COVER_IDLE;
  2327. fallthrough;
  2328. case XLOG_STATE_COVER_NEED:
  2329. case XLOG_STATE_COVER_NEED2:
  2330. break;
  2331. case XLOG_STATE_COVER_DONE:
  2332. if (iclogs_changed == 1)
  2333. return XLOG_STATE_COVER_NEED2;
  2334. break;
  2335. case XLOG_STATE_COVER_DONE2:
  2336. if (iclogs_changed == 1)
  2337. return XLOG_STATE_COVER_IDLE;
  2338. break;
  2339. default:
  2340. ASSERT(0);
  2341. }
  2342. return XLOG_STATE_COVER_NEED;
  2343. }
  2344. STATIC void
  2345. xlog_state_clean_iclog(
  2346. struct xlog *log,
  2347. struct xlog_in_core *dirty_iclog)
  2348. {
  2349. int iclogs_changed = 0;
  2350. trace_xlog_iclog_clean(dirty_iclog, _RET_IP_);
  2351. dirty_iclog->ic_state = XLOG_STATE_DIRTY;
  2352. xlog_state_activate_iclogs(log, &iclogs_changed);
  2353. wake_up_all(&dirty_iclog->ic_force_wait);
  2354. if (iclogs_changed) {
  2355. log->l_covered_state = xlog_covered_state(log->l_covered_state,
  2356. iclogs_changed);
  2357. }
  2358. }
  2359. STATIC xfs_lsn_t
  2360. xlog_get_lowest_lsn(
  2361. struct xlog *log)
  2362. {
  2363. struct xlog_in_core *iclog = log->l_iclog;
  2364. xfs_lsn_t lowest_lsn = 0, lsn;
  2365. do {
  2366. if (iclog->ic_state == XLOG_STATE_ACTIVE ||
  2367. iclog->ic_state == XLOG_STATE_DIRTY)
  2368. continue;
  2369. lsn = be64_to_cpu(iclog->ic_header.h_lsn);
  2370. if ((lsn && !lowest_lsn) || XFS_LSN_CMP(lsn, lowest_lsn) < 0)
  2371. lowest_lsn = lsn;
  2372. } while ((iclog = iclog->ic_next) != log->l_iclog);
  2373. return lowest_lsn;
  2374. }
  2375. /*
  2376. * Completion of a iclog IO does not imply that a transaction has completed, as
  2377. * transactions can be large enough to span many iclogs. We cannot change the
  2378. * tail of the log half way through a transaction as this may be the only
  2379. * transaction in the log and moving the tail to point to the middle of it
  2380. * will prevent recovery from finding the start of the transaction. Hence we
  2381. * should only update the last_sync_lsn if this iclog contains transaction
  2382. * completion callbacks on it.
  2383. *
  2384. * We have to do this before we drop the icloglock to ensure we are the only one
  2385. * that can update it.
  2386. *
  2387. * If we are moving the last_sync_lsn forwards, we also need to ensure we kick
  2388. * the reservation grant head pushing. This is due to the fact that the push
  2389. * target is bound by the current last_sync_lsn value. Hence if we have a large
  2390. * amount of log space bound up in this committing transaction then the
  2391. * last_sync_lsn value may be the limiting factor preventing tail pushing from
  2392. * freeing space in the log. Hence once we've updated the last_sync_lsn we
  2393. * should push the AIL to ensure the push target (and hence the grant head) is
  2394. * no longer bound by the old log head location and can move forwards and make
  2395. * progress again.
  2396. */
  2397. static void
  2398. xlog_state_set_callback(
  2399. struct xlog *log,
  2400. struct xlog_in_core *iclog,
  2401. xfs_lsn_t header_lsn)
  2402. {
  2403. trace_xlog_iclog_callback(iclog, _RET_IP_);
  2404. iclog->ic_state = XLOG_STATE_CALLBACK;
  2405. ASSERT(XFS_LSN_CMP(atomic64_read(&log->l_last_sync_lsn),
  2406. header_lsn) <= 0);
  2407. if (list_empty_careful(&iclog->ic_callbacks))
  2408. return;
  2409. atomic64_set(&log->l_last_sync_lsn, header_lsn);
  2410. xlog_grant_push_ail(log, 0);
  2411. }
  2412. /*
  2413. * Return true if we need to stop processing, false to continue to the next
  2414. * iclog. The caller will need to run callbacks if the iclog is returned in the
  2415. * XLOG_STATE_CALLBACK state.
  2416. */
  2417. static bool
  2418. xlog_state_iodone_process_iclog(
  2419. struct xlog *log,
  2420. struct xlog_in_core *iclog)
  2421. {
  2422. xfs_lsn_t lowest_lsn;
  2423. xfs_lsn_t header_lsn;
  2424. switch (iclog->ic_state) {
  2425. case XLOG_STATE_ACTIVE:
  2426. case XLOG_STATE_DIRTY:
  2427. /*
  2428. * Skip all iclogs in the ACTIVE & DIRTY states:
  2429. */
  2430. return false;
  2431. case XLOG_STATE_DONE_SYNC:
  2432. /*
  2433. * Now that we have an iclog that is in the DONE_SYNC state, do
  2434. * one more check here to see if we have chased our tail around.
  2435. * If this is not the lowest lsn iclog, then we will leave it
  2436. * for another completion to process.
  2437. */
  2438. header_lsn = be64_to_cpu(iclog->ic_header.h_lsn);
  2439. lowest_lsn = xlog_get_lowest_lsn(log);
  2440. if (lowest_lsn && XFS_LSN_CMP(lowest_lsn, header_lsn) < 0)
  2441. return false;
  2442. xlog_state_set_callback(log, iclog, header_lsn);
  2443. return false;
  2444. default:
  2445. /*
  2446. * Can only perform callbacks in order. Since this iclog is not
  2447. * in the DONE_SYNC state, we skip the rest and just try to
  2448. * clean up.
  2449. */
  2450. return true;
  2451. }
  2452. }
  2453. /*
  2454. * Loop over all the iclogs, running attached callbacks on them. Return true if
  2455. * we ran any callbacks, indicating that we dropped the icloglock. We don't need
  2456. * to handle transient shutdown state here at all because
  2457. * xlog_state_shutdown_callbacks() will be run to do the necessary shutdown
  2458. * cleanup of the callbacks.
  2459. */
  2460. static bool
  2461. xlog_state_do_iclog_callbacks(
  2462. struct xlog *log)
  2463. __releases(&log->l_icloglock)
  2464. __acquires(&log->l_icloglock)
  2465. {
  2466. struct xlog_in_core *first_iclog = log->l_iclog;
  2467. struct xlog_in_core *iclog = first_iclog;
  2468. bool ran_callback = false;
  2469. do {
  2470. LIST_HEAD(cb_list);
  2471. if (xlog_state_iodone_process_iclog(log, iclog))
  2472. break;
  2473. if (iclog->ic_state != XLOG_STATE_CALLBACK) {
  2474. iclog = iclog->ic_next;
  2475. continue;
  2476. }
  2477. list_splice_init(&iclog->ic_callbacks, &cb_list);
  2478. spin_unlock(&log->l_icloglock);
  2479. trace_xlog_iclog_callbacks_start(iclog, _RET_IP_);
  2480. xlog_cil_process_committed(&cb_list);
  2481. trace_xlog_iclog_callbacks_done(iclog, _RET_IP_);
  2482. ran_callback = true;
  2483. spin_lock(&log->l_icloglock);
  2484. xlog_state_clean_iclog(log, iclog);
  2485. iclog = iclog->ic_next;
  2486. } while (iclog != first_iclog);
  2487. return ran_callback;
  2488. }
  2489. /*
  2490. * Loop running iclog completion callbacks until there are no more iclogs in a
  2491. * state that can run callbacks.
  2492. */
  2493. STATIC void
  2494. xlog_state_do_callback(
  2495. struct xlog *log)
  2496. {
  2497. int flushcnt = 0;
  2498. int repeats = 0;
  2499. spin_lock(&log->l_icloglock);
  2500. while (xlog_state_do_iclog_callbacks(log)) {
  2501. if (xlog_is_shutdown(log))
  2502. break;
  2503. if (++repeats > 5000) {
  2504. flushcnt += repeats;
  2505. repeats = 0;
  2506. xfs_warn(log->l_mp,
  2507. "%s: possible infinite loop (%d iterations)",
  2508. __func__, flushcnt);
  2509. }
  2510. }
  2511. if (log->l_iclog->ic_state == XLOG_STATE_ACTIVE)
  2512. wake_up_all(&log->l_flush_wait);
  2513. spin_unlock(&log->l_icloglock);
  2514. }
  2515. /*
  2516. * Finish transitioning this iclog to the dirty state.
  2517. *
  2518. * Callbacks could take time, so they are done outside the scope of the
  2519. * global state machine log lock.
  2520. */
  2521. STATIC void
  2522. xlog_state_done_syncing(
  2523. struct xlog_in_core *iclog)
  2524. {
  2525. struct xlog *log = iclog->ic_log;
  2526. spin_lock(&log->l_icloglock);
  2527. ASSERT(atomic_read(&iclog->ic_refcnt) == 0);
  2528. trace_xlog_iclog_sync_done(iclog, _RET_IP_);
  2529. /*
  2530. * If we got an error, either on the first buffer, or in the case of
  2531. * split log writes, on the second, we shut down the file system and
  2532. * no iclogs should ever be attempted to be written to disk again.
  2533. */
  2534. if (!xlog_is_shutdown(log)) {
  2535. ASSERT(iclog->ic_state == XLOG_STATE_SYNCING);
  2536. iclog->ic_state = XLOG_STATE_DONE_SYNC;
  2537. }
  2538. /*
  2539. * Someone could be sleeping prior to writing out the next
  2540. * iclog buffer, we wake them all, one will get to do the
  2541. * I/O, the others get to wait for the result.
  2542. */
  2543. wake_up_all(&iclog->ic_write_wait);
  2544. spin_unlock(&log->l_icloglock);
  2545. xlog_state_do_callback(log);
  2546. }
  2547. /*
  2548. * If the head of the in-core log ring is not (ACTIVE or DIRTY), then we must
  2549. * sleep. We wait on the flush queue on the head iclog as that should be
  2550. * the first iclog to complete flushing. Hence if all iclogs are syncing,
  2551. * we will wait here and all new writes will sleep until a sync completes.
  2552. *
  2553. * The in-core logs are used in a circular fashion. They are not used
  2554. * out-of-order even when an iclog past the head is free.
  2555. *
  2556. * return:
  2557. * * log_offset where xlog_write() can start writing into the in-core
  2558. * log's data space.
  2559. * * in-core log pointer to which xlog_write() should write.
  2560. * * boolean indicating this is a continued write to an in-core log.
  2561. * If this is the last write, then the in-core log's offset field
  2562. * needs to be incremented, depending on the amount of data which
  2563. * is copied.
  2564. */
  2565. STATIC int
  2566. xlog_state_get_iclog_space(
  2567. struct xlog *log,
  2568. int len,
  2569. struct xlog_in_core **iclogp,
  2570. struct xlog_ticket *ticket,
  2571. int *logoffsetp)
  2572. {
  2573. int log_offset;
  2574. xlog_rec_header_t *head;
  2575. xlog_in_core_t *iclog;
  2576. restart:
  2577. spin_lock(&log->l_icloglock);
  2578. if (xlog_is_shutdown(log)) {
  2579. spin_unlock(&log->l_icloglock);
  2580. return -EIO;
  2581. }
  2582. iclog = log->l_iclog;
  2583. if (iclog->ic_state != XLOG_STATE_ACTIVE) {
  2584. XFS_STATS_INC(log->l_mp, xs_log_noiclogs);
  2585. /* Wait for log writes to have flushed */
  2586. xlog_wait(&log->l_flush_wait, &log->l_icloglock);
  2587. goto restart;
  2588. }
  2589. head = &iclog->ic_header;
  2590. atomic_inc(&iclog->ic_refcnt); /* prevents sync */
  2591. log_offset = iclog->ic_offset;
  2592. trace_xlog_iclog_get_space(iclog, _RET_IP_);
  2593. /* On the 1st write to an iclog, figure out lsn. This works
  2594. * if iclogs marked XLOG_STATE_WANT_SYNC always write out what they are
  2595. * committing to. If the offset is set, that's how many blocks
  2596. * must be written.
  2597. */
  2598. if (log_offset == 0) {
  2599. ticket->t_curr_res -= log->l_iclog_hsize;
  2600. head->h_cycle = cpu_to_be32(log->l_curr_cycle);
  2601. head->h_lsn = cpu_to_be64(
  2602. xlog_assign_lsn(log->l_curr_cycle, log->l_curr_block));
  2603. ASSERT(log->l_curr_block >= 0);
  2604. }
  2605. /* If there is enough room to write everything, then do it. Otherwise,
  2606. * claim the rest of the region and make sure the XLOG_STATE_WANT_SYNC
  2607. * bit is on, so this will get flushed out. Don't update ic_offset
  2608. * until you know exactly how many bytes get copied. Therefore, wait
  2609. * until later to update ic_offset.
  2610. *
  2611. * xlog_write() algorithm assumes that at least 2 xlog_op_header_t's
  2612. * can fit into remaining data section.
  2613. */
  2614. if (iclog->ic_size - iclog->ic_offset < 2*sizeof(xlog_op_header_t)) {
  2615. int error = 0;
  2616. xlog_state_switch_iclogs(log, iclog, iclog->ic_size);
  2617. /*
  2618. * If we are the only one writing to this iclog, sync it to
  2619. * disk. We need to do an atomic compare and decrement here to
  2620. * avoid racing with concurrent atomic_dec_and_lock() calls in
  2621. * xlog_state_release_iclog() when there is more than one
  2622. * reference to the iclog.
  2623. */
  2624. if (!atomic_add_unless(&iclog->ic_refcnt, -1, 1))
  2625. error = xlog_state_release_iclog(log, iclog, ticket);
  2626. spin_unlock(&log->l_icloglock);
  2627. if (error)
  2628. return error;
  2629. goto restart;
  2630. }
  2631. /* Do we have enough room to write the full amount in the remainder
  2632. * of this iclog? Or must we continue a write on the next iclog and
  2633. * mark this iclog as completely taken? In the case where we switch
  2634. * iclogs (to mark it taken), this particular iclog will release/sync
  2635. * to disk in xlog_write().
  2636. */
  2637. if (len <= iclog->ic_size - iclog->ic_offset)
  2638. iclog->ic_offset += len;
  2639. else
  2640. xlog_state_switch_iclogs(log, iclog, iclog->ic_size);
  2641. *iclogp = iclog;
  2642. ASSERT(iclog->ic_offset <= iclog->ic_size);
  2643. spin_unlock(&log->l_icloglock);
  2644. *logoffsetp = log_offset;
  2645. return 0;
  2646. }
  2647. /*
  2648. * The first cnt-1 times a ticket goes through here we don't need to move the
  2649. * grant write head because the permanent reservation has reserved cnt times the
  2650. * unit amount. Release part of current permanent unit reservation and reset
  2651. * current reservation to be one units worth. Also move grant reservation head
  2652. * forward.
  2653. */
  2654. void
  2655. xfs_log_ticket_regrant(
  2656. struct xlog *log,
  2657. struct xlog_ticket *ticket)
  2658. {
  2659. trace_xfs_log_ticket_regrant(log, ticket);
  2660. if (ticket->t_cnt > 0)
  2661. ticket->t_cnt--;
  2662. xlog_grant_sub_space(log, &log->l_reserve_head.grant,
  2663. ticket->t_curr_res);
  2664. xlog_grant_sub_space(log, &log->l_write_head.grant,
  2665. ticket->t_curr_res);
  2666. ticket->t_curr_res = ticket->t_unit_res;
  2667. trace_xfs_log_ticket_regrant_sub(log, ticket);
  2668. /* just return if we still have some of the pre-reserved space */
  2669. if (!ticket->t_cnt) {
  2670. xlog_grant_add_space(log, &log->l_reserve_head.grant,
  2671. ticket->t_unit_res);
  2672. trace_xfs_log_ticket_regrant_exit(log, ticket);
  2673. ticket->t_curr_res = ticket->t_unit_res;
  2674. }
  2675. xfs_log_ticket_put(ticket);
  2676. }
  2677. /*
  2678. * Give back the space left from a reservation.
  2679. *
  2680. * All the information we need to make a correct determination of space left
  2681. * is present. For non-permanent reservations, things are quite easy. The
  2682. * count should have been decremented to zero. We only need to deal with the
  2683. * space remaining in the current reservation part of the ticket. If the
  2684. * ticket contains a permanent reservation, there may be left over space which
  2685. * needs to be released. A count of N means that N-1 refills of the current
  2686. * reservation can be done before we need to ask for more space. The first
  2687. * one goes to fill up the first current reservation. Once we run out of
  2688. * space, the count will stay at zero and the only space remaining will be
  2689. * in the current reservation field.
  2690. */
  2691. void
  2692. xfs_log_ticket_ungrant(
  2693. struct xlog *log,
  2694. struct xlog_ticket *ticket)
  2695. {
  2696. int bytes;
  2697. trace_xfs_log_ticket_ungrant(log, ticket);
  2698. if (ticket->t_cnt > 0)
  2699. ticket->t_cnt--;
  2700. trace_xfs_log_ticket_ungrant_sub(log, ticket);
  2701. /*
  2702. * If this is a permanent reservation ticket, we may be able to free
  2703. * up more space based on the remaining count.
  2704. */
  2705. bytes = ticket->t_curr_res;
  2706. if (ticket->t_cnt > 0) {
  2707. ASSERT(ticket->t_flags & XLOG_TIC_PERM_RESERV);
  2708. bytes += ticket->t_unit_res*ticket->t_cnt;
  2709. }
  2710. xlog_grant_sub_space(log, &log->l_reserve_head.grant, bytes);
  2711. xlog_grant_sub_space(log, &log->l_write_head.grant, bytes);
  2712. trace_xfs_log_ticket_ungrant_exit(log, ticket);
  2713. xfs_log_space_wake(log->l_mp);
  2714. xfs_log_ticket_put(ticket);
  2715. }
  2716. /*
  2717. * This routine will mark the current iclog in the ring as WANT_SYNC and move
  2718. * the current iclog pointer to the next iclog in the ring.
  2719. */
  2720. void
  2721. xlog_state_switch_iclogs(
  2722. struct xlog *log,
  2723. struct xlog_in_core *iclog,
  2724. int eventual_size)
  2725. {
  2726. ASSERT(iclog->ic_state == XLOG_STATE_ACTIVE);
  2727. assert_spin_locked(&log->l_icloglock);
  2728. trace_xlog_iclog_switch(iclog, _RET_IP_);
  2729. if (!eventual_size)
  2730. eventual_size = iclog->ic_offset;
  2731. iclog->ic_state = XLOG_STATE_WANT_SYNC;
  2732. iclog->ic_header.h_prev_block = cpu_to_be32(log->l_prev_block);
  2733. log->l_prev_block = log->l_curr_block;
  2734. log->l_prev_cycle = log->l_curr_cycle;
  2735. /* roll log?: ic_offset changed later */
  2736. log->l_curr_block += BTOBB(eventual_size)+BTOBB(log->l_iclog_hsize);
  2737. /* Round up to next log-sunit */
  2738. if (log->l_iclog_roundoff > BBSIZE) {
  2739. uint32_t sunit_bb = BTOBB(log->l_iclog_roundoff);
  2740. log->l_curr_block = roundup(log->l_curr_block, sunit_bb);
  2741. }
  2742. if (log->l_curr_block >= log->l_logBBsize) {
  2743. /*
  2744. * Rewind the current block before the cycle is bumped to make
  2745. * sure that the combined LSN never transiently moves forward
  2746. * when the log wraps to the next cycle. This is to support the
  2747. * unlocked sample of these fields from xlog_valid_lsn(). Most
  2748. * other cases should acquire l_icloglock.
  2749. */
  2750. log->l_curr_block -= log->l_logBBsize;
  2751. ASSERT(log->l_curr_block >= 0);
  2752. smp_wmb();
  2753. log->l_curr_cycle++;
  2754. if (log->l_curr_cycle == XLOG_HEADER_MAGIC_NUM)
  2755. log->l_curr_cycle++;
  2756. }
  2757. ASSERT(iclog == log->l_iclog);
  2758. log->l_iclog = iclog->ic_next;
  2759. }
  2760. /*
  2761. * Force the iclog to disk and check if the iclog has been completed before
  2762. * xlog_force_iclog() returns. This can happen on synchronous (e.g.
  2763. * pmem) or fast async storage because we drop the icloglock to issue the IO.
  2764. * If completion has already occurred, tell the caller so that it can avoid an
  2765. * unnecessary wait on the iclog.
  2766. */
  2767. static int
  2768. xlog_force_and_check_iclog(
  2769. struct xlog_in_core *iclog,
  2770. bool *completed)
  2771. {
  2772. xfs_lsn_t lsn = be64_to_cpu(iclog->ic_header.h_lsn);
  2773. int error;
  2774. *completed = false;
  2775. error = xlog_force_iclog(iclog);
  2776. if (error)
  2777. return error;
  2778. /*
  2779. * If the iclog has already been completed and reused the header LSN
  2780. * will have been rewritten by completion
  2781. */
  2782. if (be64_to_cpu(iclog->ic_header.h_lsn) != lsn)
  2783. *completed = true;
  2784. return 0;
  2785. }
  2786. /*
  2787. * Write out all data in the in-core log as of this exact moment in time.
  2788. *
  2789. * Data may be written to the in-core log during this call. However,
  2790. * we don't guarantee this data will be written out. A change from past
  2791. * implementation means this routine will *not* write out zero length LRs.
  2792. *
  2793. * Basically, we try and perform an intelligent scan of the in-core logs.
  2794. * If we determine there is no flushable data, we just return. There is no
  2795. * flushable data if:
  2796. *
  2797. * 1. the current iclog is active and has no data; the previous iclog
  2798. * is in the active or dirty state.
  2799. * 2. the current iclog is drity, and the previous iclog is in the
  2800. * active or dirty state.
  2801. *
  2802. * We may sleep if:
  2803. *
  2804. * 1. the current iclog is not in the active nor dirty state.
  2805. * 2. the current iclog dirty, and the previous iclog is not in the
  2806. * active nor dirty state.
  2807. * 3. the current iclog is active, and there is another thread writing
  2808. * to this particular iclog.
  2809. * 4. a) the current iclog is active and has no other writers
  2810. * b) when we return from flushing out this iclog, it is still
  2811. * not in the active nor dirty state.
  2812. */
  2813. int
  2814. xfs_log_force(
  2815. struct xfs_mount *mp,
  2816. uint flags)
  2817. {
  2818. struct xlog *log = mp->m_log;
  2819. struct xlog_in_core *iclog;
  2820. XFS_STATS_INC(mp, xs_log_force);
  2821. trace_xfs_log_force(mp, 0, _RET_IP_);
  2822. xlog_cil_force(log);
  2823. spin_lock(&log->l_icloglock);
  2824. if (xlog_is_shutdown(log))
  2825. goto out_error;
  2826. iclog = log->l_iclog;
  2827. trace_xlog_iclog_force(iclog, _RET_IP_);
  2828. if (iclog->ic_state == XLOG_STATE_DIRTY ||
  2829. (iclog->ic_state == XLOG_STATE_ACTIVE &&
  2830. atomic_read(&iclog->ic_refcnt) == 0 && iclog->ic_offset == 0)) {
  2831. /*
  2832. * If the head is dirty or (active and empty), then we need to
  2833. * look at the previous iclog.
  2834. *
  2835. * If the previous iclog is active or dirty we are done. There
  2836. * is nothing to sync out. Otherwise, we attach ourselves to the
  2837. * previous iclog and go to sleep.
  2838. */
  2839. iclog = iclog->ic_prev;
  2840. } else if (iclog->ic_state == XLOG_STATE_ACTIVE) {
  2841. if (atomic_read(&iclog->ic_refcnt) == 0) {
  2842. /* We have exclusive access to this iclog. */
  2843. bool completed;
  2844. if (xlog_force_and_check_iclog(iclog, &completed))
  2845. goto out_error;
  2846. if (completed)
  2847. goto out_unlock;
  2848. } else {
  2849. /*
  2850. * Someone else is still writing to this iclog, so we
  2851. * need to ensure that when they release the iclog it
  2852. * gets synced immediately as we may be waiting on it.
  2853. */
  2854. xlog_state_switch_iclogs(log, iclog, 0);
  2855. }
  2856. }
  2857. /*
  2858. * The iclog we are about to wait on may contain the checkpoint pushed
  2859. * by the above xlog_cil_force() call, but it may not have been pushed
  2860. * to disk yet. Like the ACTIVE case above, we need to make sure caches
  2861. * are flushed when this iclog is written.
  2862. */
  2863. if (iclog->ic_state == XLOG_STATE_WANT_SYNC)
  2864. iclog->ic_flags |= XLOG_ICL_NEED_FLUSH | XLOG_ICL_NEED_FUA;
  2865. if (flags & XFS_LOG_SYNC)
  2866. return xlog_wait_on_iclog(iclog);
  2867. out_unlock:
  2868. spin_unlock(&log->l_icloglock);
  2869. return 0;
  2870. out_error:
  2871. spin_unlock(&log->l_icloglock);
  2872. return -EIO;
  2873. }
  2874. /*
  2875. * Force the log to a specific LSN.
  2876. *
  2877. * If an iclog with that lsn can be found:
  2878. * If it is in the DIRTY state, just return.
  2879. * If it is in the ACTIVE state, move the in-core log into the WANT_SYNC
  2880. * state and go to sleep or return.
  2881. * If it is in any other state, go to sleep or return.
  2882. *
  2883. * Synchronous forces are implemented with a wait queue. All callers trying
  2884. * to force a given lsn to disk must wait on the queue attached to the
  2885. * specific in-core log. When given in-core log finally completes its write
  2886. * to disk, that thread will wake up all threads waiting on the queue.
  2887. */
  2888. static int
  2889. xlog_force_lsn(
  2890. struct xlog *log,
  2891. xfs_lsn_t lsn,
  2892. uint flags,
  2893. int *log_flushed,
  2894. bool already_slept)
  2895. {
  2896. struct xlog_in_core *iclog;
  2897. bool completed;
  2898. spin_lock(&log->l_icloglock);
  2899. if (xlog_is_shutdown(log))
  2900. goto out_error;
  2901. iclog = log->l_iclog;
  2902. while (be64_to_cpu(iclog->ic_header.h_lsn) != lsn) {
  2903. trace_xlog_iclog_force_lsn(iclog, _RET_IP_);
  2904. iclog = iclog->ic_next;
  2905. if (iclog == log->l_iclog)
  2906. goto out_unlock;
  2907. }
  2908. switch (iclog->ic_state) {
  2909. case XLOG_STATE_ACTIVE:
  2910. /*
  2911. * We sleep here if we haven't already slept (e.g. this is the
  2912. * first time we've looked at the correct iclog buf) and the
  2913. * buffer before us is going to be sync'ed. The reason for this
  2914. * is that if we are doing sync transactions here, by waiting
  2915. * for the previous I/O to complete, we can allow a few more
  2916. * transactions into this iclog before we close it down.
  2917. *
  2918. * Otherwise, we mark the buffer WANT_SYNC, and bump up the
  2919. * refcnt so we can release the log (which drops the ref count).
  2920. * The state switch keeps new transaction commits from using
  2921. * this buffer. When the current commits finish writing into
  2922. * the buffer, the refcount will drop to zero and the buffer
  2923. * will go out then.
  2924. */
  2925. if (!already_slept &&
  2926. (iclog->ic_prev->ic_state == XLOG_STATE_WANT_SYNC ||
  2927. iclog->ic_prev->ic_state == XLOG_STATE_SYNCING)) {
  2928. xlog_wait(&iclog->ic_prev->ic_write_wait,
  2929. &log->l_icloglock);
  2930. return -EAGAIN;
  2931. }
  2932. if (xlog_force_and_check_iclog(iclog, &completed))
  2933. goto out_error;
  2934. if (log_flushed)
  2935. *log_flushed = 1;
  2936. if (completed)
  2937. goto out_unlock;
  2938. break;
  2939. case XLOG_STATE_WANT_SYNC:
  2940. /*
  2941. * This iclog may contain the checkpoint pushed by the
  2942. * xlog_cil_force_seq() call, but there are other writers still
  2943. * accessing it so it hasn't been pushed to disk yet. Like the
  2944. * ACTIVE case above, we need to make sure caches are flushed
  2945. * when this iclog is written.
  2946. */
  2947. iclog->ic_flags |= XLOG_ICL_NEED_FLUSH | XLOG_ICL_NEED_FUA;
  2948. break;
  2949. default:
  2950. /*
  2951. * The entire checkpoint was written by the CIL force and is on
  2952. * its way to disk already. It will be stable when it
  2953. * completes, so we don't need to manipulate caches here at all.
  2954. * We just need to wait for completion if necessary.
  2955. */
  2956. break;
  2957. }
  2958. if (flags & XFS_LOG_SYNC)
  2959. return xlog_wait_on_iclog(iclog);
  2960. out_unlock:
  2961. spin_unlock(&log->l_icloglock);
  2962. return 0;
  2963. out_error:
  2964. spin_unlock(&log->l_icloglock);
  2965. return -EIO;
  2966. }
  2967. /*
  2968. * Force the log to a specific checkpoint sequence.
  2969. *
  2970. * First force the CIL so that all the required changes have been flushed to the
  2971. * iclogs. If the CIL force completed it will return a commit LSN that indicates
  2972. * the iclog that needs to be flushed to stable storage. If the caller needs
  2973. * a synchronous log force, we will wait on the iclog with the LSN returned by
  2974. * xlog_cil_force_seq() to be completed.
  2975. */
  2976. int
  2977. xfs_log_force_seq(
  2978. struct xfs_mount *mp,
  2979. xfs_csn_t seq,
  2980. uint flags,
  2981. int *log_flushed)
  2982. {
  2983. struct xlog *log = mp->m_log;
  2984. xfs_lsn_t lsn;
  2985. int ret;
  2986. ASSERT(seq != 0);
  2987. XFS_STATS_INC(mp, xs_log_force);
  2988. trace_xfs_log_force(mp, seq, _RET_IP_);
  2989. lsn = xlog_cil_force_seq(log, seq);
  2990. if (lsn == NULLCOMMITLSN)
  2991. return 0;
  2992. ret = xlog_force_lsn(log, lsn, flags, log_flushed, false);
  2993. if (ret == -EAGAIN) {
  2994. XFS_STATS_INC(mp, xs_log_force_sleep);
  2995. ret = xlog_force_lsn(log, lsn, flags, log_flushed, true);
  2996. }
  2997. return ret;
  2998. }
  2999. /*
  3000. * Free a used ticket when its refcount falls to zero.
  3001. */
  3002. void
  3003. xfs_log_ticket_put(
  3004. xlog_ticket_t *ticket)
  3005. {
  3006. ASSERT(atomic_read(&ticket->t_ref) > 0);
  3007. if (atomic_dec_and_test(&ticket->t_ref))
  3008. kmem_cache_free(xfs_log_ticket_cache, ticket);
  3009. }
  3010. xlog_ticket_t *
  3011. xfs_log_ticket_get(
  3012. xlog_ticket_t *ticket)
  3013. {
  3014. ASSERT(atomic_read(&ticket->t_ref) > 0);
  3015. atomic_inc(&ticket->t_ref);
  3016. return ticket;
  3017. }
  3018. /*
  3019. * Figure out the total log space unit (in bytes) that would be
  3020. * required for a log ticket.
  3021. */
  3022. static int
  3023. xlog_calc_unit_res(
  3024. struct xlog *log,
  3025. int unit_bytes,
  3026. int *niclogs)
  3027. {
  3028. int iclog_space;
  3029. uint num_headers;
  3030. /*
  3031. * Permanent reservations have up to 'cnt'-1 active log operations
  3032. * in the log. A unit in this case is the amount of space for one
  3033. * of these log operations. Normal reservations have a cnt of 1
  3034. * and their unit amount is the total amount of space required.
  3035. *
  3036. * The following lines of code account for non-transaction data
  3037. * which occupy space in the on-disk log.
  3038. *
  3039. * Normal form of a transaction is:
  3040. * <oph><trans-hdr><start-oph><reg1-oph><reg1><reg2-oph>...<commit-oph>
  3041. * and then there are LR hdrs, split-recs and roundoff at end of syncs.
  3042. *
  3043. * We need to account for all the leadup data and trailer data
  3044. * around the transaction data.
  3045. * And then we need to account for the worst case in terms of using
  3046. * more space.
  3047. * The worst case will happen if:
  3048. * - the placement of the transaction happens to be such that the
  3049. * roundoff is at its maximum
  3050. * - the transaction data is synced before the commit record is synced
  3051. * i.e. <transaction-data><roundoff> | <commit-rec><roundoff>
  3052. * Therefore the commit record is in its own Log Record.
  3053. * This can happen as the commit record is called with its
  3054. * own region to xlog_write().
  3055. * This then means that in the worst case, roundoff can happen for
  3056. * the commit-rec as well.
  3057. * The commit-rec is smaller than padding in this scenario and so it is
  3058. * not added separately.
  3059. */
  3060. /* for trans header */
  3061. unit_bytes += sizeof(xlog_op_header_t);
  3062. unit_bytes += sizeof(xfs_trans_header_t);
  3063. /* for start-rec */
  3064. unit_bytes += sizeof(xlog_op_header_t);
  3065. /*
  3066. * for LR headers - the space for data in an iclog is the size minus
  3067. * the space used for the headers. If we use the iclog size, then we
  3068. * undercalculate the number of headers required.
  3069. *
  3070. * Furthermore - the addition of op headers for split-recs might
  3071. * increase the space required enough to require more log and op
  3072. * headers, so take that into account too.
  3073. *
  3074. * IMPORTANT: This reservation makes the assumption that if this
  3075. * transaction is the first in an iclog and hence has the LR headers
  3076. * accounted to it, then the remaining space in the iclog is
  3077. * exclusively for this transaction. i.e. if the transaction is larger
  3078. * than the iclog, it will be the only thing in that iclog.
  3079. * Fundamentally, this means we must pass the entire log vector to
  3080. * xlog_write to guarantee this.
  3081. */
  3082. iclog_space = log->l_iclog_size - log->l_iclog_hsize;
  3083. num_headers = howmany(unit_bytes, iclog_space);
  3084. /* for split-recs - ophdrs added when data split over LRs */
  3085. unit_bytes += sizeof(xlog_op_header_t) * num_headers;
  3086. /* add extra header reservations if we overrun */
  3087. while (!num_headers ||
  3088. howmany(unit_bytes, iclog_space) > num_headers) {
  3089. unit_bytes += sizeof(xlog_op_header_t);
  3090. num_headers++;
  3091. }
  3092. unit_bytes += log->l_iclog_hsize * num_headers;
  3093. /* for commit-rec LR header - note: padding will subsume the ophdr */
  3094. unit_bytes += log->l_iclog_hsize;
  3095. /* roundoff padding for transaction data and one for commit record */
  3096. unit_bytes += 2 * log->l_iclog_roundoff;
  3097. if (niclogs)
  3098. *niclogs = num_headers;
  3099. return unit_bytes;
  3100. }
  3101. int
  3102. xfs_log_calc_unit_res(
  3103. struct xfs_mount *mp,
  3104. int unit_bytes)
  3105. {
  3106. return xlog_calc_unit_res(mp->m_log, unit_bytes, NULL);
  3107. }
  3108. /*
  3109. * Allocate and initialise a new log ticket.
  3110. */
  3111. struct xlog_ticket *
  3112. xlog_ticket_alloc(
  3113. struct xlog *log,
  3114. int unit_bytes,
  3115. int cnt,
  3116. bool permanent)
  3117. {
  3118. struct xlog_ticket *tic;
  3119. int unit_res;
  3120. tic = kmem_cache_zalloc(xfs_log_ticket_cache, GFP_NOFS | __GFP_NOFAIL);
  3121. unit_res = xlog_calc_unit_res(log, unit_bytes, &tic->t_iclog_hdrs);
  3122. atomic_set(&tic->t_ref, 1);
  3123. tic->t_task = current;
  3124. INIT_LIST_HEAD(&tic->t_queue);
  3125. tic->t_unit_res = unit_res;
  3126. tic->t_curr_res = unit_res;
  3127. tic->t_cnt = cnt;
  3128. tic->t_ocnt = cnt;
  3129. tic->t_tid = get_random_u32();
  3130. if (permanent)
  3131. tic->t_flags |= XLOG_TIC_PERM_RESERV;
  3132. return tic;
  3133. }
  3134. #if defined(DEBUG)
  3135. /*
  3136. * Check to make sure the grant write head didn't just over lap the tail. If
  3137. * the cycles are the same, we can't be overlapping. Otherwise, make sure that
  3138. * the cycles differ by exactly one and check the byte count.
  3139. *
  3140. * This check is run unlocked, so can give false positives. Rather than assert
  3141. * on failures, use a warn-once flag and a panic tag to allow the admin to
  3142. * determine if they want to panic the machine when such an error occurs. For
  3143. * debug kernels this will have the same effect as using an assert but, unlinke
  3144. * an assert, it can be turned off at runtime.
  3145. */
  3146. STATIC void
  3147. xlog_verify_grant_tail(
  3148. struct xlog *log)
  3149. {
  3150. int tail_cycle, tail_blocks;
  3151. int cycle, space;
  3152. xlog_crack_grant_head(&log->l_write_head.grant, &cycle, &space);
  3153. xlog_crack_atomic_lsn(&log->l_tail_lsn, &tail_cycle, &tail_blocks);
  3154. if (tail_cycle != cycle) {
  3155. if (cycle - 1 != tail_cycle &&
  3156. !test_and_set_bit(XLOG_TAIL_WARN, &log->l_opstate)) {
  3157. xfs_alert_tag(log->l_mp, XFS_PTAG_LOGRES,
  3158. "%s: cycle - 1 != tail_cycle", __func__);
  3159. }
  3160. if (space > BBTOB(tail_blocks) &&
  3161. !test_and_set_bit(XLOG_TAIL_WARN, &log->l_opstate)) {
  3162. xfs_alert_tag(log->l_mp, XFS_PTAG_LOGRES,
  3163. "%s: space > BBTOB(tail_blocks)", __func__);
  3164. }
  3165. }
  3166. }
  3167. /* check if it will fit */
  3168. STATIC void
  3169. xlog_verify_tail_lsn(
  3170. struct xlog *log,
  3171. struct xlog_in_core *iclog)
  3172. {
  3173. xfs_lsn_t tail_lsn = be64_to_cpu(iclog->ic_header.h_tail_lsn);
  3174. int blocks;
  3175. if (CYCLE_LSN(tail_lsn) == log->l_prev_cycle) {
  3176. blocks =
  3177. log->l_logBBsize - (log->l_prev_block - BLOCK_LSN(tail_lsn));
  3178. if (blocks < BTOBB(iclog->ic_offset)+BTOBB(log->l_iclog_hsize))
  3179. xfs_emerg(log->l_mp, "%s: ran out of log space", __func__);
  3180. } else {
  3181. ASSERT(CYCLE_LSN(tail_lsn)+1 == log->l_prev_cycle);
  3182. if (BLOCK_LSN(tail_lsn) == log->l_prev_block)
  3183. xfs_emerg(log->l_mp, "%s: tail wrapped", __func__);
  3184. blocks = BLOCK_LSN(tail_lsn) - log->l_prev_block;
  3185. if (blocks < BTOBB(iclog->ic_offset) + 1)
  3186. xfs_emerg(log->l_mp, "%s: ran out of log space", __func__);
  3187. }
  3188. }
  3189. /*
  3190. * Perform a number of checks on the iclog before writing to disk.
  3191. *
  3192. * 1. Make sure the iclogs are still circular
  3193. * 2. Make sure we have a good magic number
  3194. * 3. Make sure we don't have magic numbers in the data
  3195. * 4. Check fields of each log operation header for:
  3196. * A. Valid client identifier
  3197. * B. tid ptr value falls in valid ptr space (user space code)
  3198. * C. Length in log record header is correct according to the
  3199. * individual operation headers within record.
  3200. * 5. When a bwrite will occur within 5 blocks of the front of the physical
  3201. * log, check the preceding blocks of the physical log to make sure all
  3202. * the cycle numbers agree with the current cycle number.
  3203. */
  3204. STATIC void
  3205. xlog_verify_iclog(
  3206. struct xlog *log,
  3207. struct xlog_in_core *iclog,
  3208. int count)
  3209. {
  3210. xlog_op_header_t *ophead;
  3211. xlog_in_core_t *icptr;
  3212. xlog_in_core_2_t *xhdr;
  3213. void *base_ptr, *ptr, *p;
  3214. ptrdiff_t field_offset;
  3215. uint8_t clientid;
  3216. int len, i, j, k, op_len;
  3217. int idx;
  3218. /* check validity of iclog pointers */
  3219. spin_lock(&log->l_icloglock);
  3220. icptr = log->l_iclog;
  3221. for (i = 0; i < log->l_iclog_bufs; i++, icptr = icptr->ic_next)
  3222. ASSERT(icptr);
  3223. if (icptr != log->l_iclog)
  3224. xfs_emerg(log->l_mp, "%s: corrupt iclog ring", __func__);
  3225. spin_unlock(&log->l_icloglock);
  3226. /* check log magic numbers */
  3227. if (iclog->ic_header.h_magicno != cpu_to_be32(XLOG_HEADER_MAGIC_NUM))
  3228. xfs_emerg(log->l_mp, "%s: invalid magic num", __func__);
  3229. base_ptr = ptr = &iclog->ic_header;
  3230. p = &iclog->ic_header;
  3231. for (ptr += BBSIZE; ptr < base_ptr + count; ptr += BBSIZE) {
  3232. if (*(__be32 *)ptr == cpu_to_be32(XLOG_HEADER_MAGIC_NUM))
  3233. xfs_emerg(log->l_mp, "%s: unexpected magic num",
  3234. __func__);
  3235. }
  3236. /* check fields */
  3237. len = be32_to_cpu(iclog->ic_header.h_num_logops);
  3238. base_ptr = ptr = iclog->ic_datap;
  3239. ophead = ptr;
  3240. xhdr = iclog->ic_data;
  3241. for (i = 0; i < len; i++) {
  3242. ophead = ptr;
  3243. /* clientid is only 1 byte */
  3244. p = &ophead->oh_clientid;
  3245. field_offset = p - base_ptr;
  3246. if (field_offset & 0x1ff) {
  3247. clientid = ophead->oh_clientid;
  3248. } else {
  3249. idx = BTOBBT((void *)&ophead->oh_clientid - iclog->ic_datap);
  3250. if (idx >= (XLOG_HEADER_CYCLE_SIZE / BBSIZE)) {
  3251. j = idx / (XLOG_HEADER_CYCLE_SIZE / BBSIZE);
  3252. k = idx % (XLOG_HEADER_CYCLE_SIZE / BBSIZE);
  3253. clientid = xlog_get_client_id(
  3254. xhdr[j].hic_xheader.xh_cycle_data[k]);
  3255. } else {
  3256. clientid = xlog_get_client_id(
  3257. iclog->ic_header.h_cycle_data[idx]);
  3258. }
  3259. }
  3260. if (clientid != XFS_TRANSACTION && clientid != XFS_LOG) {
  3261. xfs_warn(log->l_mp,
  3262. "%s: op %d invalid clientid %d op "PTR_FMT" offset 0x%lx",
  3263. __func__, i, clientid, ophead,
  3264. (unsigned long)field_offset);
  3265. }
  3266. /* check length */
  3267. p = &ophead->oh_len;
  3268. field_offset = p - base_ptr;
  3269. if (field_offset & 0x1ff) {
  3270. op_len = be32_to_cpu(ophead->oh_len);
  3271. } else {
  3272. idx = BTOBBT((void *)&ophead->oh_len - iclog->ic_datap);
  3273. if (idx >= (XLOG_HEADER_CYCLE_SIZE / BBSIZE)) {
  3274. j = idx / (XLOG_HEADER_CYCLE_SIZE / BBSIZE);
  3275. k = idx % (XLOG_HEADER_CYCLE_SIZE / BBSIZE);
  3276. op_len = be32_to_cpu(xhdr[j].hic_xheader.xh_cycle_data[k]);
  3277. } else {
  3278. op_len = be32_to_cpu(iclog->ic_header.h_cycle_data[idx]);
  3279. }
  3280. }
  3281. ptr += sizeof(xlog_op_header_t) + op_len;
  3282. }
  3283. }
  3284. #endif
  3285. /*
  3286. * Perform a forced shutdown on the log.
  3287. *
  3288. * This can be called from low level log code to trigger a shutdown, or from the
  3289. * high level mount shutdown code when the mount shuts down.
  3290. *
  3291. * Our main objectives here are to make sure that:
  3292. * a. if the shutdown was not due to a log IO error, flush the logs to
  3293. * disk. Anything modified after this is ignored.
  3294. * b. the log gets atomically marked 'XLOG_IO_ERROR' for all interested
  3295. * parties to find out. Nothing new gets queued after this is done.
  3296. * c. Tasks sleeping on log reservations, pinned objects and
  3297. * other resources get woken up.
  3298. * d. The mount is also marked as shut down so that log triggered shutdowns
  3299. * still behave the same as if they called xfs_forced_shutdown().
  3300. *
  3301. * Return true if the shutdown cause was a log IO error and we actually shut the
  3302. * log down.
  3303. */
  3304. bool
  3305. xlog_force_shutdown(
  3306. struct xlog *log,
  3307. uint32_t shutdown_flags)
  3308. {
  3309. bool log_error = (shutdown_flags & SHUTDOWN_LOG_IO_ERROR);
  3310. if (!log)
  3311. return false;
  3312. /*
  3313. * Flush all the completed transactions to disk before marking the log
  3314. * being shut down. We need to do this first as shutting down the log
  3315. * before the force will prevent the log force from flushing the iclogs
  3316. * to disk.
  3317. *
  3318. * When we are in recovery, there are no transactions to flush, and
  3319. * we don't want to touch the log because we don't want to perturb the
  3320. * current head/tail for future recovery attempts. Hence we need to
  3321. * avoid a log force in this case.
  3322. *
  3323. * If we are shutting down due to a log IO error, then we must avoid
  3324. * trying to write the log as that may just result in more IO errors and
  3325. * an endless shutdown/force loop.
  3326. */
  3327. if (!log_error && !xlog_in_recovery(log))
  3328. xfs_log_force(log->l_mp, XFS_LOG_SYNC);
  3329. /*
  3330. * Atomically set the shutdown state. If the shutdown state is already
  3331. * set, there someone else is performing the shutdown and so we are done
  3332. * here. This should never happen because we should only ever get called
  3333. * once by the first shutdown caller.
  3334. *
  3335. * Much of the log state machine transitions assume that shutdown state
  3336. * cannot change once they hold the log->l_icloglock. Hence we need to
  3337. * hold that lock here, even though we use the atomic test_and_set_bit()
  3338. * operation to set the shutdown state.
  3339. */
  3340. spin_lock(&log->l_icloglock);
  3341. if (test_and_set_bit(XLOG_IO_ERROR, &log->l_opstate)) {
  3342. spin_unlock(&log->l_icloglock);
  3343. return false;
  3344. }
  3345. spin_unlock(&log->l_icloglock);
  3346. /*
  3347. * If this log shutdown also sets the mount shutdown state, issue a
  3348. * shutdown warning message.
  3349. */
  3350. if (!test_and_set_bit(XFS_OPSTATE_SHUTDOWN, &log->l_mp->m_opstate)) {
  3351. xfs_alert_tag(log->l_mp, XFS_PTAG_SHUTDOWN_LOGERROR,
  3352. "Filesystem has been shut down due to log error (0x%x).",
  3353. shutdown_flags);
  3354. xfs_alert(log->l_mp,
  3355. "Please unmount the filesystem and rectify the problem(s).");
  3356. if (xfs_error_level >= XFS_ERRLEVEL_HIGH)
  3357. xfs_stack_trace();
  3358. }
  3359. /*
  3360. * We don't want anybody waiting for log reservations after this. That
  3361. * means we have to wake up everybody queued up on reserveq as well as
  3362. * writeq. In addition, we make sure in xlog_{re}grant_log_space that
  3363. * we don't enqueue anything once the SHUTDOWN flag is set, and this
  3364. * action is protected by the grant locks.
  3365. */
  3366. xlog_grant_head_wake_all(&log->l_reserve_head);
  3367. xlog_grant_head_wake_all(&log->l_write_head);
  3368. /*
  3369. * Wake up everybody waiting on xfs_log_force. Wake the CIL push first
  3370. * as if the log writes were completed. The abort handling in the log
  3371. * item committed callback functions will do this again under lock to
  3372. * avoid races.
  3373. */
  3374. spin_lock(&log->l_cilp->xc_push_lock);
  3375. wake_up_all(&log->l_cilp->xc_start_wait);
  3376. wake_up_all(&log->l_cilp->xc_commit_wait);
  3377. spin_unlock(&log->l_cilp->xc_push_lock);
  3378. spin_lock(&log->l_icloglock);
  3379. xlog_state_shutdown_callbacks(log);
  3380. spin_unlock(&log->l_icloglock);
  3381. wake_up_var(&log->l_opstate);
  3382. return log_error;
  3383. }
  3384. STATIC int
  3385. xlog_iclogs_empty(
  3386. struct xlog *log)
  3387. {
  3388. xlog_in_core_t *iclog;
  3389. iclog = log->l_iclog;
  3390. do {
  3391. /* endianness does not matter here, zero is zero in
  3392. * any language.
  3393. */
  3394. if (iclog->ic_header.h_num_logops)
  3395. return 0;
  3396. iclog = iclog->ic_next;
  3397. } while (iclog != log->l_iclog);
  3398. return 1;
  3399. }
  3400. /*
  3401. * Verify that an LSN stamped into a piece of metadata is valid. This is
  3402. * intended for use in read verifiers on v5 superblocks.
  3403. */
  3404. bool
  3405. xfs_log_check_lsn(
  3406. struct xfs_mount *mp,
  3407. xfs_lsn_t lsn)
  3408. {
  3409. struct xlog *log = mp->m_log;
  3410. bool valid;
  3411. /*
  3412. * norecovery mode skips mount-time log processing and unconditionally
  3413. * resets the in-core LSN. We can't validate in this mode, but
  3414. * modifications are not allowed anyways so just return true.
  3415. */
  3416. if (xfs_has_norecovery(mp))
  3417. return true;
  3418. /*
  3419. * Some metadata LSNs are initialized to NULL (e.g., the agfl). This is
  3420. * handled by recovery and thus safe to ignore here.
  3421. */
  3422. if (lsn == NULLCOMMITLSN)
  3423. return true;
  3424. valid = xlog_valid_lsn(mp->m_log, lsn);
  3425. /* warn the user about what's gone wrong before verifier failure */
  3426. if (!valid) {
  3427. spin_lock(&log->l_icloglock);
  3428. xfs_warn(mp,
  3429. "Corruption warning: Metadata has LSN (%d:%d) ahead of current LSN (%d:%d). "
  3430. "Please unmount and run xfs_repair (>= v4.3) to resolve.",
  3431. CYCLE_LSN(lsn), BLOCK_LSN(lsn),
  3432. log->l_curr_cycle, log->l_curr_block);
  3433. spin_unlock(&log->l_icloglock);
  3434. }
  3435. return valid;
  3436. }
  3437. /*
  3438. * Notify the log that we're about to start using a feature that is protected
  3439. * by a log incompat feature flag. This will prevent log covering from
  3440. * clearing those flags.
  3441. */
  3442. void
  3443. xlog_use_incompat_feat(
  3444. struct xlog *log)
  3445. {
  3446. down_read(&log->l_incompat_users);
  3447. }
  3448. /* Notify the log that we've finished using log incompat features. */
  3449. void
  3450. xlog_drop_incompat_feat(
  3451. struct xlog *log)
  3452. {
  3453. up_read(&log->l_incompat_users);
  3454. }