xfs_log.c 101 KB

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