xfs_log.c 114 KB

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