xfs_log.c 114 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116
  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. if (prev_iclog == log->l_iclog)
  1394. break;
  1395. }
  1396. spinlock_destroy(&log->l_icloglock);
  1397. xfs_buf_free(log->l_xbuf);
  1398. out_free_log:
  1399. kmem_free(log);
  1400. out:
  1401. return ERR_PTR(error);
  1402. } /* xlog_alloc_log */
  1403. /*
  1404. * Write out the commit record of a transaction associated with the given
  1405. * ticket. Return the lsn of the commit record.
  1406. */
  1407. STATIC int
  1408. xlog_commit_record(
  1409. struct xlog *log,
  1410. struct xlog_ticket *ticket,
  1411. struct xlog_in_core **iclog,
  1412. xfs_lsn_t *commitlsnp)
  1413. {
  1414. struct xfs_mount *mp = log->l_mp;
  1415. int error;
  1416. struct xfs_log_iovec reg = {
  1417. .i_addr = NULL,
  1418. .i_len = 0,
  1419. .i_type = XLOG_REG_TYPE_COMMIT,
  1420. };
  1421. struct xfs_log_vec vec = {
  1422. .lv_niovecs = 1,
  1423. .lv_iovecp = &reg,
  1424. };
  1425. ASSERT_ALWAYS(iclog);
  1426. error = xlog_write(log, &vec, ticket, commitlsnp, iclog,
  1427. XLOG_COMMIT_TRANS);
  1428. if (error)
  1429. xfs_force_shutdown(mp, SHUTDOWN_LOG_IO_ERROR);
  1430. return error;
  1431. }
  1432. /*
  1433. * Push on the buffer cache code if we ever use more than 75% of the on-disk
  1434. * log space. This code pushes on the lsn which would supposedly free up
  1435. * the 25% which we want to leave free. We may need to adopt a policy which
  1436. * pushes on an lsn which is further along in the log once we reach the high
  1437. * water mark. In this manner, we would be creating a low water mark.
  1438. */
  1439. STATIC void
  1440. xlog_grant_push_ail(
  1441. struct xlog *log,
  1442. int need_bytes)
  1443. {
  1444. xfs_lsn_t threshold_lsn = 0;
  1445. xfs_lsn_t last_sync_lsn;
  1446. int free_blocks;
  1447. int free_bytes;
  1448. int threshold_block;
  1449. int threshold_cycle;
  1450. int free_threshold;
  1451. ASSERT(BTOBB(need_bytes) < log->l_logBBsize);
  1452. free_bytes = xlog_space_left(log, &log->l_reserve_head.grant);
  1453. free_blocks = BTOBBT(free_bytes);
  1454. /*
  1455. * Set the threshold for the minimum number of free blocks in the
  1456. * log to the maximum of what the caller needs, one quarter of the
  1457. * log, and 256 blocks.
  1458. */
  1459. free_threshold = BTOBB(need_bytes);
  1460. free_threshold = max(free_threshold, (log->l_logBBsize >> 2));
  1461. free_threshold = max(free_threshold, 256);
  1462. if (free_blocks >= free_threshold)
  1463. return;
  1464. xlog_crack_atomic_lsn(&log->l_tail_lsn, &threshold_cycle,
  1465. &threshold_block);
  1466. threshold_block += free_threshold;
  1467. if (threshold_block >= log->l_logBBsize) {
  1468. threshold_block -= log->l_logBBsize;
  1469. threshold_cycle += 1;
  1470. }
  1471. threshold_lsn = xlog_assign_lsn(threshold_cycle,
  1472. threshold_block);
  1473. /*
  1474. * Don't pass in an lsn greater than the lsn of the last
  1475. * log record known to be on disk. Use a snapshot of the last sync lsn
  1476. * so that it doesn't change between the compare and the set.
  1477. */
  1478. last_sync_lsn = atomic64_read(&log->l_last_sync_lsn);
  1479. if (XFS_LSN_CMP(threshold_lsn, last_sync_lsn) > 0)
  1480. threshold_lsn = last_sync_lsn;
  1481. /*
  1482. * Get the transaction layer to kick the dirty buffers out to
  1483. * disk asynchronously. No point in trying to do this if
  1484. * the filesystem is shutting down.
  1485. */
  1486. if (!XLOG_FORCED_SHUTDOWN(log))
  1487. xfs_ail_push(log->l_ailp, threshold_lsn);
  1488. }
  1489. /*
  1490. * Stamp cycle number in every block
  1491. */
  1492. STATIC void
  1493. xlog_pack_data(
  1494. struct xlog *log,
  1495. struct xlog_in_core *iclog,
  1496. int roundoff)
  1497. {
  1498. int i, j, k;
  1499. int size = iclog->ic_offset + roundoff;
  1500. __be32 cycle_lsn;
  1501. char *dp;
  1502. cycle_lsn = CYCLE_LSN_DISK(iclog->ic_header.h_lsn);
  1503. dp = iclog->ic_datap;
  1504. for (i = 0; i < BTOBB(size); i++) {
  1505. if (i >= (XLOG_HEADER_CYCLE_SIZE / BBSIZE))
  1506. break;
  1507. iclog->ic_header.h_cycle_data[i] = *(__be32 *)dp;
  1508. *(__be32 *)dp = cycle_lsn;
  1509. dp += BBSIZE;
  1510. }
  1511. if (xfs_sb_version_haslogv2(&log->l_mp->m_sb)) {
  1512. xlog_in_core_2_t *xhdr = iclog->ic_data;
  1513. for ( ; i < BTOBB(size); i++) {
  1514. j = i / (XLOG_HEADER_CYCLE_SIZE / BBSIZE);
  1515. k = i % (XLOG_HEADER_CYCLE_SIZE / BBSIZE);
  1516. xhdr[j].hic_xheader.xh_cycle_data[k] = *(__be32 *)dp;
  1517. *(__be32 *)dp = cycle_lsn;
  1518. dp += BBSIZE;
  1519. }
  1520. for (i = 1; i < log->l_iclog_heads; i++)
  1521. xhdr[i].hic_xheader.xh_cycle = cycle_lsn;
  1522. }
  1523. }
  1524. /*
  1525. * Calculate the checksum for a log buffer.
  1526. *
  1527. * This is a little more complicated than it should be because the various
  1528. * headers and the actual data are non-contiguous.
  1529. */
  1530. __le32
  1531. xlog_cksum(
  1532. struct xlog *log,
  1533. struct xlog_rec_header *rhead,
  1534. char *dp,
  1535. int size)
  1536. {
  1537. uint32_t crc;
  1538. /* first generate the crc for the record header ... */
  1539. crc = xfs_start_cksum_update((char *)rhead,
  1540. sizeof(struct xlog_rec_header),
  1541. offsetof(struct xlog_rec_header, h_crc));
  1542. /* ... then for additional cycle data for v2 logs ... */
  1543. if (xfs_sb_version_haslogv2(&log->l_mp->m_sb)) {
  1544. union xlog_in_core2 *xhdr = (union xlog_in_core2 *)rhead;
  1545. int i;
  1546. int xheads;
  1547. xheads = size / XLOG_HEADER_CYCLE_SIZE;
  1548. if (size % XLOG_HEADER_CYCLE_SIZE)
  1549. xheads++;
  1550. for (i = 1; i < xheads; i++) {
  1551. crc = crc32c(crc, &xhdr[i].hic_xheader,
  1552. sizeof(struct xlog_rec_ext_header));
  1553. }
  1554. }
  1555. /* ... and finally for the payload */
  1556. crc = crc32c(crc, dp, size);
  1557. return xfs_end_cksum(crc);
  1558. }
  1559. /*
  1560. * The bdstrat callback function for log bufs. This gives us a central
  1561. * place to trap bufs in case we get hit by a log I/O error and need to
  1562. * shutdown. Actually, in practice, even when we didn't get a log error,
  1563. * we transition the iclogs to IOERROR state *after* flushing all existing
  1564. * iclogs to disk. This is because we don't want anymore new transactions to be
  1565. * started or completed afterwards.
  1566. *
  1567. * We lock the iclogbufs here so that we can serialise against IO completion
  1568. * during unmount. We might be processing a shutdown triggered during unmount,
  1569. * and that can occur asynchronously to the unmount thread, and hence we need to
  1570. * ensure that completes before tearing down the iclogbufs. Hence we need to
  1571. * hold the buffer lock across the log IO to acheive that.
  1572. */
  1573. STATIC int
  1574. xlog_bdstrat(
  1575. struct xfs_buf *bp)
  1576. {
  1577. struct xlog_in_core *iclog = bp->b_log_item;
  1578. xfs_buf_lock(bp);
  1579. if (iclog->ic_state & XLOG_STATE_IOERROR) {
  1580. xfs_buf_ioerror(bp, -EIO);
  1581. xfs_buf_stale(bp);
  1582. xfs_buf_ioend(bp);
  1583. /*
  1584. * It would seem logical to return EIO here, but we rely on
  1585. * the log state machine to propagate I/O errors instead of
  1586. * doing it here. Similarly, IO completion will unlock the
  1587. * buffer, so we don't do it here.
  1588. */
  1589. return 0;
  1590. }
  1591. xfs_buf_submit(bp);
  1592. return 0;
  1593. }
  1594. /*
  1595. * Flush out the in-core log (iclog) to the on-disk log in an asynchronous
  1596. * fashion. Previously, we should have moved the current iclog
  1597. * ptr in the log to point to the next available iclog. This allows further
  1598. * write to continue while this code syncs out an iclog ready to go.
  1599. * Before an in-core log can be written out, the data section must be scanned
  1600. * to save away the 1st word of each BBSIZE block into the header. We replace
  1601. * it with the current cycle count. Each BBSIZE block is tagged with the
  1602. * cycle count because there in an implicit assumption that drives will
  1603. * guarantee that entire 512 byte blocks get written at once. In other words,
  1604. * we can't have part of a 512 byte block written and part not written. By
  1605. * tagging each block, we will know which blocks are valid when recovering
  1606. * after an unclean shutdown.
  1607. *
  1608. * This routine is single threaded on the iclog. No other thread can be in
  1609. * this routine with the same iclog. Changing contents of iclog can there-
  1610. * fore be done without grabbing the state machine lock. Updating the global
  1611. * log will require grabbing the lock though.
  1612. *
  1613. * The entire log manager uses a logical block numbering scheme. Only
  1614. * log_sync (and then only bwrite()) know about the fact that the log may
  1615. * not start with block zero on a given device. The log block start offset
  1616. * is added immediately before calling bwrite().
  1617. */
  1618. STATIC int
  1619. xlog_sync(
  1620. struct xlog *log,
  1621. struct xlog_in_core *iclog)
  1622. {
  1623. xfs_buf_t *bp;
  1624. int i;
  1625. uint count; /* byte count of bwrite */
  1626. uint count_init; /* initial count before roundup */
  1627. int roundoff; /* roundoff to BB or stripe */
  1628. int split = 0; /* split write into two regions */
  1629. int error;
  1630. int v2 = xfs_sb_version_haslogv2(&log->l_mp->m_sb);
  1631. int size;
  1632. XFS_STATS_INC(log->l_mp, xs_log_writes);
  1633. ASSERT(atomic_read(&iclog->ic_refcnt) == 0);
  1634. /* Add for LR header */
  1635. count_init = log->l_iclog_hsize + iclog->ic_offset;
  1636. /* Round out the log write size */
  1637. if (v2 && log->l_mp->m_sb.sb_logsunit > 1) {
  1638. /* we have a v2 stripe unit to use */
  1639. count = XLOG_LSUNITTOB(log, XLOG_BTOLSUNIT(log, count_init));
  1640. } else {
  1641. count = BBTOB(BTOBB(count_init));
  1642. }
  1643. roundoff = count - count_init;
  1644. ASSERT(roundoff >= 0);
  1645. ASSERT((v2 && log->l_mp->m_sb.sb_logsunit > 1 &&
  1646. roundoff < log->l_mp->m_sb.sb_logsunit)
  1647. ||
  1648. (log->l_mp->m_sb.sb_logsunit <= 1 &&
  1649. roundoff < BBTOB(1)));
  1650. /* move grant heads by roundoff in sync */
  1651. xlog_grant_add_space(log, &log->l_reserve_head.grant, roundoff);
  1652. xlog_grant_add_space(log, &log->l_write_head.grant, roundoff);
  1653. /* put cycle number in every block */
  1654. xlog_pack_data(log, iclog, roundoff);
  1655. /* real byte length */
  1656. size = iclog->ic_offset;
  1657. if (v2)
  1658. size += roundoff;
  1659. iclog->ic_header.h_len = cpu_to_be32(size);
  1660. bp = iclog->ic_bp;
  1661. XFS_BUF_SET_ADDR(bp, BLOCK_LSN(be64_to_cpu(iclog->ic_header.h_lsn)));
  1662. XFS_STATS_ADD(log->l_mp, xs_log_blocks, BTOBB(count));
  1663. /* Do we need to split this write into 2 parts? */
  1664. if (XFS_BUF_ADDR(bp) + BTOBB(count) > log->l_logBBsize) {
  1665. char *dptr;
  1666. split = count - (BBTOB(log->l_logBBsize - XFS_BUF_ADDR(bp)));
  1667. count = BBTOB(log->l_logBBsize - XFS_BUF_ADDR(bp));
  1668. iclog->ic_bwritecnt = 2;
  1669. /*
  1670. * Bump the cycle numbers at the start of each block in the
  1671. * part of the iclog that ends up in the buffer that gets
  1672. * written to the start of the log.
  1673. *
  1674. * Watch out for the header magic number case, though.
  1675. */
  1676. dptr = (char *)&iclog->ic_header + count;
  1677. for (i = 0; i < split; i += BBSIZE) {
  1678. uint32_t cycle = be32_to_cpu(*(__be32 *)dptr);
  1679. if (++cycle == XLOG_HEADER_MAGIC_NUM)
  1680. cycle++;
  1681. *(__be32 *)dptr = cpu_to_be32(cycle);
  1682. dptr += BBSIZE;
  1683. }
  1684. } else {
  1685. iclog->ic_bwritecnt = 1;
  1686. }
  1687. /* calculcate the checksum */
  1688. iclog->ic_header.h_crc = xlog_cksum(log, &iclog->ic_header,
  1689. iclog->ic_datap, size);
  1690. /*
  1691. * Intentionally corrupt the log record CRC based on the error injection
  1692. * frequency, if defined. This facilitates testing log recovery in the
  1693. * event of torn writes. Hence, set the IOABORT state to abort the log
  1694. * write on I/O completion and shutdown the fs. The subsequent mount
  1695. * detects the bad CRC and attempts to recover.
  1696. */
  1697. if (XFS_TEST_ERROR(false, log->l_mp, XFS_ERRTAG_LOG_BAD_CRC)) {
  1698. iclog->ic_header.h_crc &= cpu_to_le32(0xAAAAAAAA);
  1699. iclog->ic_state |= XLOG_STATE_IOABORT;
  1700. xfs_warn(log->l_mp,
  1701. "Intentionally corrupted log record at LSN 0x%llx. Shutdown imminent.",
  1702. be64_to_cpu(iclog->ic_header.h_lsn));
  1703. }
  1704. bp->b_io_length = BTOBB(count);
  1705. bp->b_log_item = iclog;
  1706. bp->b_flags &= ~XBF_FLUSH;
  1707. bp->b_flags |= (XBF_ASYNC | XBF_SYNCIO | XBF_WRITE | XBF_FUA);
  1708. /*
  1709. * Flush the data device before flushing the log to make sure all meta
  1710. * data written back from the AIL actually made it to disk before
  1711. * stamping the new log tail LSN into the log buffer. For an external
  1712. * log we need to issue the flush explicitly, and unfortunately
  1713. * synchronously here; for an internal log we can simply use the block
  1714. * layer state machine for preflushes.
  1715. */
  1716. if (log->l_mp->m_logdev_targp != log->l_mp->m_ddev_targp)
  1717. xfs_blkdev_issue_flush(log->l_mp->m_ddev_targp);
  1718. else
  1719. bp->b_flags |= XBF_FLUSH;
  1720. ASSERT(XFS_BUF_ADDR(bp) <= log->l_logBBsize-1);
  1721. ASSERT(XFS_BUF_ADDR(bp) + BTOBB(count) <= log->l_logBBsize);
  1722. xlog_verify_iclog(log, iclog, count, true);
  1723. /* account for log which doesn't start at block #0 */
  1724. XFS_BUF_SET_ADDR(bp, XFS_BUF_ADDR(bp) + log->l_logBBstart);
  1725. /*
  1726. * Don't call xfs_bwrite here. We do log-syncs even when the filesystem
  1727. * is shutting down.
  1728. */
  1729. error = xlog_bdstrat(bp);
  1730. if (error) {
  1731. xfs_buf_ioerror_alert(bp, "xlog_sync");
  1732. return error;
  1733. }
  1734. if (split) {
  1735. bp = iclog->ic_log->l_xbuf;
  1736. XFS_BUF_SET_ADDR(bp, 0); /* logical 0 */
  1737. xfs_buf_associate_memory(bp,
  1738. (char *)&iclog->ic_header + count, split);
  1739. bp->b_log_item = iclog;
  1740. bp->b_flags &= ~XBF_FLUSH;
  1741. bp->b_flags |= (XBF_ASYNC | XBF_SYNCIO | XBF_WRITE | XBF_FUA);
  1742. ASSERT(XFS_BUF_ADDR(bp) <= log->l_logBBsize-1);
  1743. ASSERT(XFS_BUF_ADDR(bp) + BTOBB(count) <= log->l_logBBsize);
  1744. /* account for internal log which doesn't start at block #0 */
  1745. XFS_BUF_SET_ADDR(bp, XFS_BUF_ADDR(bp) + log->l_logBBstart);
  1746. error = xlog_bdstrat(bp);
  1747. if (error) {
  1748. xfs_buf_ioerror_alert(bp, "xlog_sync (split)");
  1749. return error;
  1750. }
  1751. }
  1752. return 0;
  1753. } /* xlog_sync */
  1754. /*
  1755. * Deallocate a log structure
  1756. */
  1757. STATIC void
  1758. xlog_dealloc_log(
  1759. struct xlog *log)
  1760. {
  1761. xlog_in_core_t *iclog, *next_iclog;
  1762. int i;
  1763. xlog_cil_destroy(log);
  1764. /*
  1765. * Cycle all the iclogbuf locks to make sure all log IO completion
  1766. * is done before we tear down these buffers.
  1767. */
  1768. iclog = log->l_iclog;
  1769. for (i = 0; i < log->l_iclog_bufs; i++) {
  1770. xfs_buf_lock(iclog->ic_bp);
  1771. xfs_buf_unlock(iclog->ic_bp);
  1772. iclog = iclog->ic_next;
  1773. }
  1774. /*
  1775. * Always need to ensure that the extra buffer does not point to memory
  1776. * owned by another log buffer before we free it. Also, cycle the lock
  1777. * first to ensure we've completed IO on it.
  1778. */
  1779. xfs_buf_lock(log->l_xbuf);
  1780. xfs_buf_unlock(log->l_xbuf);
  1781. xfs_buf_set_empty(log->l_xbuf, BTOBB(log->l_iclog_size));
  1782. xfs_buf_free(log->l_xbuf);
  1783. iclog = log->l_iclog;
  1784. for (i = 0; i < log->l_iclog_bufs; i++) {
  1785. xfs_buf_free(iclog->ic_bp);
  1786. next_iclog = iclog->ic_next;
  1787. kmem_free(iclog);
  1788. iclog = next_iclog;
  1789. }
  1790. spinlock_destroy(&log->l_icloglock);
  1791. log->l_mp->m_log = NULL;
  1792. kmem_free(log);
  1793. } /* xlog_dealloc_log */
  1794. /*
  1795. * Update counters atomically now that memcpy is done.
  1796. */
  1797. /* ARGSUSED */
  1798. static inline void
  1799. xlog_state_finish_copy(
  1800. struct xlog *log,
  1801. struct xlog_in_core *iclog,
  1802. int record_cnt,
  1803. int copy_bytes)
  1804. {
  1805. spin_lock(&log->l_icloglock);
  1806. be32_add_cpu(&iclog->ic_header.h_num_logops, record_cnt);
  1807. iclog->ic_offset += copy_bytes;
  1808. spin_unlock(&log->l_icloglock);
  1809. } /* xlog_state_finish_copy */
  1810. /*
  1811. * print out info relating to regions written which consume
  1812. * the reservation
  1813. */
  1814. void
  1815. xlog_print_tic_res(
  1816. struct xfs_mount *mp,
  1817. struct xlog_ticket *ticket)
  1818. {
  1819. uint i;
  1820. uint ophdr_spc = ticket->t_res_num_ophdrs * (uint)sizeof(xlog_op_header_t);
  1821. /* match with XLOG_REG_TYPE_* in xfs_log.h */
  1822. #define REG_TYPE_STR(type, str) [XLOG_REG_TYPE_##type] = str
  1823. static char *res_type_str[XLOG_REG_TYPE_MAX + 1] = {
  1824. REG_TYPE_STR(BFORMAT, "bformat"),
  1825. REG_TYPE_STR(BCHUNK, "bchunk"),
  1826. REG_TYPE_STR(EFI_FORMAT, "efi_format"),
  1827. REG_TYPE_STR(EFD_FORMAT, "efd_format"),
  1828. REG_TYPE_STR(IFORMAT, "iformat"),
  1829. REG_TYPE_STR(ICORE, "icore"),
  1830. REG_TYPE_STR(IEXT, "iext"),
  1831. REG_TYPE_STR(IBROOT, "ibroot"),
  1832. REG_TYPE_STR(ILOCAL, "ilocal"),
  1833. REG_TYPE_STR(IATTR_EXT, "iattr_ext"),
  1834. REG_TYPE_STR(IATTR_BROOT, "iattr_broot"),
  1835. REG_TYPE_STR(IATTR_LOCAL, "iattr_local"),
  1836. REG_TYPE_STR(QFORMAT, "qformat"),
  1837. REG_TYPE_STR(DQUOT, "dquot"),
  1838. REG_TYPE_STR(QUOTAOFF, "quotaoff"),
  1839. REG_TYPE_STR(LRHEADER, "LR header"),
  1840. REG_TYPE_STR(UNMOUNT, "unmount"),
  1841. REG_TYPE_STR(COMMIT, "commit"),
  1842. REG_TYPE_STR(TRANSHDR, "trans header"),
  1843. REG_TYPE_STR(ICREATE, "inode create")
  1844. };
  1845. #undef REG_TYPE_STR
  1846. xfs_warn(mp, "ticket reservation summary:");
  1847. xfs_warn(mp, " unit res = %d bytes",
  1848. ticket->t_unit_res);
  1849. xfs_warn(mp, " current res = %d bytes",
  1850. ticket->t_curr_res);
  1851. xfs_warn(mp, " total reg = %u bytes (o/flow = %u bytes)",
  1852. ticket->t_res_arr_sum, ticket->t_res_o_flow);
  1853. xfs_warn(mp, " ophdrs = %u (ophdr space = %u bytes)",
  1854. ticket->t_res_num_ophdrs, ophdr_spc);
  1855. xfs_warn(mp, " ophdr + reg = %u bytes",
  1856. ticket->t_res_arr_sum + ticket->t_res_o_flow + ophdr_spc);
  1857. xfs_warn(mp, " num regions = %u",
  1858. ticket->t_res_num);
  1859. for (i = 0; i < ticket->t_res_num; i++) {
  1860. uint r_type = ticket->t_res_arr[i].r_type;
  1861. xfs_warn(mp, "region[%u]: %s - %u bytes", i,
  1862. ((r_type <= 0 || r_type > XLOG_REG_TYPE_MAX) ?
  1863. "bad-rtype" : res_type_str[r_type]),
  1864. ticket->t_res_arr[i].r_len);
  1865. }
  1866. }
  1867. /*
  1868. * Print a summary of the transaction.
  1869. */
  1870. void
  1871. xlog_print_trans(
  1872. struct xfs_trans *tp)
  1873. {
  1874. struct xfs_mount *mp = tp->t_mountp;
  1875. struct xfs_log_item *lip;
  1876. /* dump core transaction and ticket info */
  1877. xfs_warn(mp, "transaction summary:");
  1878. xfs_warn(mp, " log res = %d", tp->t_log_res);
  1879. xfs_warn(mp, " log count = %d", tp->t_log_count);
  1880. xfs_warn(mp, " flags = 0x%x", tp->t_flags);
  1881. xlog_print_tic_res(mp, tp->t_ticket);
  1882. /* dump each log item */
  1883. list_for_each_entry(lip, &tp->t_items, li_trans) {
  1884. struct xfs_log_vec *lv = lip->li_lv;
  1885. struct xfs_log_iovec *vec;
  1886. int i;
  1887. xfs_warn(mp, "log item: ");
  1888. xfs_warn(mp, " type = 0x%x", lip->li_type);
  1889. xfs_warn(mp, " flags = 0x%lx", lip->li_flags);
  1890. if (!lv)
  1891. continue;
  1892. xfs_warn(mp, " niovecs = %d", lv->lv_niovecs);
  1893. xfs_warn(mp, " size = %d", lv->lv_size);
  1894. xfs_warn(mp, " bytes = %d", lv->lv_bytes);
  1895. xfs_warn(mp, " buf len = %d", lv->lv_buf_len);
  1896. /* dump each iovec for the log item */
  1897. vec = lv->lv_iovecp;
  1898. for (i = 0; i < lv->lv_niovecs; i++) {
  1899. int dumplen = min(vec->i_len, 32);
  1900. xfs_warn(mp, " iovec[%d]", i);
  1901. xfs_warn(mp, " type = 0x%x", vec->i_type);
  1902. xfs_warn(mp, " len = %d", vec->i_len);
  1903. xfs_warn(mp, " first %d bytes of iovec[%d]:", dumplen, i);
  1904. xfs_hex_dump(vec->i_addr, dumplen);
  1905. vec++;
  1906. }
  1907. }
  1908. }
  1909. /*
  1910. * Calculate the potential space needed by the log vector. Each region gets
  1911. * its own xlog_op_header_t and may need to be double word aligned.
  1912. */
  1913. static int
  1914. xlog_write_calc_vec_length(
  1915. struct xlog_ticket *ticket,
  1916. struct xfs_log_vec *log_vector)
  1917. {
  1918. struct xfs_log_vec *lv;
  1919. int headers = 0;
  1920. int len = 0;
  1921. int i;
  1922. /* acct for start rec of xact */
  1923. if (ticket->t_flags & XLOG_TIC_INITED)
  1924. headers++;
  1925. for (lv = log_vector; lv; lv = lv->lv_next) {
  1926. /* we don't write ordered log vectors */
  1927. if (lv->lv_buf_len == XFS_LOG_VEC_ORDERED)
  1928. continue;
  1929. headers += lv->lv_niovecs;
  1930. for (i = 0; i < lv->lv_niovecs; i++) {
  1931. struct xfs_log_iovec *vecp = &lv->lv_iovecp[i];
  1932. len += vecp->i_len;
  1933. xlog_tic_add_region(ticket, vecp->i_len, vecp->i_type);
  1934. }
  1935. }
  1936. ticket->t_res_num_ophdrs += headers;
  1937. len += headers * sizeof(struct xlog_op_header);
  1938. return len;
  1939. }
  1940. /*
  1941. * If first write for transaction, insert start record We can't be trying to
  1942. * commit if we are inited. We can't have any "partial_copy" if we are inited.
  1943. */
  1944. static int
  1945. xlog_write_start_rec(
  1946. struct xlog_op_header *ophdr,
  1947. struct xlog_ticket *ticket)
  1948. {
  1949. if (!(ticket->t_flags & XLOG_TIC_INITED))
  1950. return 0;
  1951. ophdr->oh_tid = cpu_to_be32(ticket->t_tid);
  1952. ophdr->oh_clientid = ticket->t_clientid;
  1953. ophdr->oh_len = 0;
  1954. ophdr->oh_flags = XLOG_START_TRANS;
  1955. ophdr->oh_res2 = 0;
  1956. ticket->t_flags &= ~XLOG_TIC_INITED;
  1957. return sizeof(struct xlog_op_header);
  1958. }
  1959. static xlog_op_header_t *
  1960. xlog_write_setup_ophdr(
  1961. struct xlog *log,
  1962. struct xlog_op_header *ophdr,
  1963. struct xlog_ticket *ticket,
  1964. uint flags)
  1965. {
  1966. ophdr->oh_tid = cpu_to_be32(ticket->t_tid);
  1967. ophdr->oh_clientid = ticket->t_clientid;
  1968. ophdr->oh_res2 = 0;
  1969. /* are we copying a commit or unmount record? */
  1970. ophdr->oh_flags = flags;
  1971. /*
  1972. * We've seen logs corrupted with bad transaction client ids. This
  1973. * makes sure that XFS doesn't generate them on. Turn this into an EIO
  1974. * and shut down the filesystem.
  1975. */
  1976. switch (ophdr->oh_clientid) {
  1977. case XFS_TRANSACTION:
  1978. case XFS_VOLUME:
  1979. case XFS_LOG:
  1980. break;
  1981. default:
  1982. xfs_warn(log->l_mp,
  1983. "Bad XFS transaction clientid 0x%x in ticket "PTR_FMT,
  1984. ophdr->oh_clientid, ticket);
  1985. return NULL;
  1986. }
  1987. return ophdr;
  1988. }
  1989. /*
  1990. * Set up the parameters of the region copy into the log. This has
  1991. * to handle region write split across multiple log buffers - this
  1992. * state is kept external to this function so that this code can
  1993. * be written in an obvious, self documenting manner.
  1994. */
  1995. static int
  1996. xlog_write_setup_copy(
  1997. struct xlog_ticket *ticket,
  1998. struct xlog_op_header *ophdr,
  1999. int space_available,
  2000. int space_required,
  2001. int *copy_off,
  2002. int *copy_len,
  2003. int *last_was_partial_copy,
  2004. int *bytes_consumed)
  2005. {
  2006. int still_to_copy;
  2007. still_to_copy = space_required - *bytes_consumed;
  2008. *copy_off = *bytes_consumed;
  2009. if (still_to_copy <= space_available) {
  2010. /* write of region completes here */
  2011. *copy_len = still_to_copy;
  2012. ophdr->oh_len = cpu_to_be32(*copy_len);
  2013. if (*last_was_partial_copy)
  2014. ophdr->oh_flags |= (XLOG_END_TRANS|XLOG_WAS_CONT_TRANS);
  2015. *last_was_partial_copy = 0;
  2016. *bytes_consumed = 0;
  2017. return 0;
  2018. }
  2019. /* partial write of region, needs extra log op header reservation */
  2020. *copy_len = space_available;
  2021. ophdr->oh_len = cpu_to_be32(*copy_len);
  2022. ophdr->oh_flags |= XLOG_CONTINUE_TRANS;
  2023. if (*last_was_partial_copy)
  2024. ophdr->oh_flags |= XLOG_WAS_CONT_TRANS;
  2025. *bytes_consumed += *copy_len;
  2026. (*last_was_partial_copy)++;
  2027. /* account for new log op header */
  2028. ticket->t_curr_res -= sizeof(struct xlog_op_header);
  2029. ticket->t_res_num_ophdrs++;
  2030. return sizeof(struct xlog_op_header);
  2031. }
  2032. static int
  2033. xlog_write_copy_finish(
  2034. struct xlog *log,
  2035. struct xlog_in_core *iclog,
  2036. uint flags,
  2037. int *record_cnt,
  2038. int *data_cnt,
  2039. int *partial_copy,
  2040. int *partial_copy_len,
  2041. int log_offset,
  2042. struct xlog_in_core **commit_iclog)
  2043. {
  2044. if (*partial_copy) {
  2045. /*
  2046. * This iclog has already been marked WANT_SYNC by
  2047. * xlog_state_get_iclog_space.
  2048. */
  2049. xlog_state_finish_copy(log, iclog, *record_cnt, *data_cnt);
  2050. *record_cnt = 0;
  2051. *data_cnt = 0;
  2052. return xlog_state_release_iclog(log, iclog);
  2053. }
  2054. *partial_copy = 0;
  2055. *partial_copy_len = 0;
  2056. if (iclog->ic_size - log_offset <= sizeof(xlog_op_header_t)) {
  2057. /* no more space in this iclog - push it. */
  2058. xlog_state_finish_copy(log, iclog, *record_cnt, *data_cnt);
  2059. *record_cnt = 0;
  2060. *data_cnt = 0;
  2061. spin_lock(&log->l_icloglock);
  2062. xlog_state_want_sync(log, iclog);
  2063. spin_unlock(&log->l_icloglock);
  2064. if (!commit_iclog)
  2065. return xlog_state_release_iclog(log, iclog);
  2066. ASSERT(flags & XLOG_COMMIT_TRANS);
  2067. *commit_iclog = iclog;
  2068. }
  2069. return 0;
  2070. }
  2071. /*
  2072. * Write some region out to in-core log
  2073. *
  2074. * This will be called when writing externally provided regions or when
  2075. * writing out a commit record for a given transaction.
  2076. *
  2077. * General algorithm:
  2078. * 1. Find total length of this write. This may include adding to the
  2079. * lengths passed in.
  2080. * 2. Check whether we violate the tickets reservation.
  2081. * 3. While writing to this iclog
  2082. * A. Reserve as much space in this iclog as can get
  2083. * B. If this is first write, save away start lsn
  2084. * C. While writing this region:
  2085. * 1. If first write of transaction, write start record
  2086. * 2. Write log operation header (header per region)
  2087. * 3. Find out if we can fit entire region into this iclog
  2088. * 4. Potentially, verify destination memcpy ptr
  2089. * 5. Memcpy (partial) region
  2090. * 6. If partial copy, release iclog; otherwise, continue
  2091. * copying more regions into current iclog
  2092. * 4. Mark want sync bit (in simulation mode)
  2093. * 5. Release iclog for potential flush to on-disk log.
  2094. *
  2095. * ERRORS:
  2096. * 1. Panic if reservation is overrun. This should never happen since
  2097. * reservation amounts are generated internal to the filesystem.
  2098. * NOTES:
  2099. * 1. Tickets are single threaded data structures.
  2100. * 2. The XLOG_END_TRANS & XLOG_CONTINUE_TRANS flags are passed down to the
  2101. * syncing routine. When a single log_write region needs to span
  2102. * multiple in-core logs, the XLOG_CONTINUE_TRANS bit should be set
  2103. * on all log operation writes which don't contain the end of the
  2104. * region. The XLOG_END_TRANS bit is used for the in-core log
  2105. * operation which contains the end of the continued log_write region.
  2106. * 3. When xlog_state_get_iclog_space() grabs the rest of the current iclog,
  2107. * we don't really know exactly how much space will be used. As a result,
  2108. * we don't update ic_offset until the end when we know exactly how many
  2109. * bytes have been written out.
  2110. */
  2111. int
  2112. xlog_write(
  2113. struct xlog *log,
  2114. struct xfs_log_vec *log_vector,
  2115. struct xlog_ticket *ticket,
  2116. xfs_lsn_t *start_lsn,
  2117. struct xlog_in_core **commit_iclog,
  2118. uint flags)
  2119. {
  2120. struct xlog_in_core *iclog = NULL;
  2121. struct xfs_log_iovec *vecp;
  2122. struct xfs_log_vec *lv;
  2123. int len;
  2124. int index;
  2125. int partial_copy = 0;
  2126. int partial_copy_len = 0;
  2127. int contwr = 0;
  2128. int record_cnt = 0;
  2129. int data_cnt = 0;
  2130. int error;
  2131. *start_lsn = 0;
  2132. len = xlog_write_calc_vec_length(ticket, log_vector);
  2133. /*
  2134. * Region headers and bytes are already accounted for.
  2135. * We only need to take into account start records and
  2136. * split regions in this function.
  2137. */
  2138. if (ticket->t_flags & XLOG_TIC_INITED)
  2139. ticket->t_curr_res -= sizeof(xlog_op_header_t);
  2140. /*
  2141. * Commit record headers need to be accounted for. These
  2142. * come in as separate writes so are easy to detect.
  2143. */
  2144. if (flags & (XLOG_COMMIT_TRANS | XLOG_UNMOUNT_TRANS))
  2145. ticket->t_curr_res -= sizeof(xlog_op_header_t);
  2146. if (ticket->t_curr_res < 0) {
  2147. xfs_alert_tag(log->l_mp, XFS_PTAG_LOGRES,
  2148. "ctx ticket reservation ran out. Need to up reservation");
  2149. xlog_print_tic_res(log->l_mp, ticket);
  2150. xfs_force_shutdown(log->l_mp, SHUTDOWN_LOG_IO_ERROR);
  2151. }
  2152. index = 0;
  2153. lv = log_vector;
  2154. vecp = lv->lv_iovecp;
  2155. while (lv && (!lv->lv_niovecs || index < lv->lv_niovecs)) {
  2156. void *ptr;
  2157. int log_offset;
  2158. error = xlog_state_get_iclog_space(log, len, &iclog, ticket,
  2159. &contwr, &log_offset);
  2160. if (error)
  2161. return error;
  2162. ASSERT(log_offset <= iclog->ic_size - 1);
  2163. ptr = iclog->ic_datap + log_offset;
  2164. /* start_lsn is the first lsn written to. That's all we need. */
  2165. if (!*start_lsn)
  2166. *start_lsn = be64_to_cpu(iclog->ic_header.h_lsn);
  2167. /*
  2168. * This loop writes out as many regions as can fit in the amount
  2169. * of space which was allocated by xlog_state_get_iclog_space().
  2170. */
  2171. while (lv && (!lv->lv_niovecs || index < lv->lv_niovecs)) {
  2172. struct xfs_log_iovec *reg;
  2173. struct xlog_op_header *ophdr;
  2174. int start_rec_copy;
  2175. int copy_len;
  2176. int copy_off;
  2177. bool ordered = false;
  2178. /* ordered log vectors have no regions to write */
  2179. if (lv->lv_buf_len == XFS_LOG_VEC_ORDERED) {
  2180. ASSERT(lv->lv_niovecs == 0);
  2181. ordered = true;
  2182. goto next_lv;
  2183. }
  2184. reg = &vecp[index];
  2185. ASSERT(reg->i_len % sizeof(int32_t) == 0);
  2186. ASSERT((unsigned long)ptr % sizeof(int32_t) == 0);
  2187. start_rec_copy = xlog_write_start_rec(ptr, ticket);
  2188. if (start_rec_copy) {
  2189. record_cnt++;
  2190. xlog_write_adv_cnt(&ptr, &len, &log_offset,
  2191. start_rec_copy);
  2192. }
  2193. ophdr = xlog_write_setup_ophdr(log, ptr, ticket, flags);
  2194. if (!ophdr)
  2195. return -EIO;
  2196. xlog_write_adv_cnt(&ptr, &len, &log_offset,
  2197. sizeof(struct xlog_op_header));
  2198. len += xlog_write_setup_copy(ticket, ophdr,
  2199. iclog->ic_size-log_offset,
  2200. reg->i_len,
  2201. &copy_off, &copy_len,
  2202. &partial_copy,
  2203. &partial_copy_len);
  2204. xlog_verify_dest_ptr(log, ptr);
  2205. /*
  2206. * Copy region.
  2207. *
  2208. * Unmount records just log an opheader, so can have
  2209. * empty payloads with no data region to copy. Hence we
  2210. * only copy the payload if the vector says it has data
  2211. * to copy.
  2212. */
  2213. ASSERT(copy_len >= 0);
  2214. if (copy_len > 0) {
  2215. memcpy(ptr, reg->i_addr + copy_off, copy_len);
  2216. xlog_write_adv_cnt(&ptr, &len, &log_offset,
  2217. copy_len);
  2218. }
  2219. copy_len += start_rec_copy + sizeof(xlog_op_header_t);
  2220. record_cnt++;
  2221. data_cnt += contwr ? copy_len : 0;
  2222. error = xlog_write_copy_finish(log, iclog, flags,
  2223. &record_cnt, &data_cnt,
  2224. &partial_copy,
  2225. &partial_copy_len,
  2226. log_offset,
  2227. commit_iclog);
  2228. if (error)
  2229. return error;
  2230. /*
  2231. * if we had a partial copy, we need to get more iclog
  2232. * space but we don't want to increment the region
  2233. * index because there is still more is this region to
  2234. * write.
  2235. *
  2236. * If we completed writing this region, and we flushed
  2237. * the iclog (indicated by resetting of the record
  2238. * count), then we also need to get more log space. If
  2239. * this was the last record, though, we are done and
  2240. * can just return.
  2241. */
  2242. if (partial_copy)
  2243. break;
  2244. if (++index == lv->lv_niovecs) {
  2245. next_lv:
  2246. lv = lv->lv_next;
  2247. index = 0;
  2248. if (lv)
  2249. vecp = lv->lv_iovecp;
  2250. }
  2251. if (record_cnt == 0 && !ordered) {
  2252. if (!lv)
  2253. return 0;
  2254. break;
  2255. }
  2256. }
  2257. }
  2258. ASSERT(len == 0);
  2259. xlog_state_finish_copy(log, iclog, record_cnt, data_cnt);
  2260. if (!commit_iclog)
  2261. return xlog_state_release_iclog(log, iclog);
  2262. ASSERT(flags & XLOG_COMMIT_TRANS);
  2263. *commit_iclog = iclog;
  2264. return 0;
  2265. }
  2266. /*****************************************************************************
  2267. *
  2268. * State Machine functions
  2269. *
  2270. *****************************************************************************
  2271. */
  2272. /* Clean iclogs starting from the head. This ordering must be
  2273. * maintained, so an iclog doesn't become ACTIVE beyond one that
  2274. * is SYNCING. This is also required to maintain the notion that we use
  2275. * a ordered wait queue to hold off would be writers to the log when every
  2276. * iclog is trying to sync to disk.
  2277. *
  2278. * State Change: DIRTY -> ACTIVE
  2279. */
  2280. STATIC void
  2281. xlog_state_clean_log(
  2282. struct xlog *log)
  2283. {
  2284. xlog_in_core_t *iclog;
  2285. int changed = 0;
  2286. iclog = log->l_iclog;
  2287. do {
  2288. if (iclog->ic_state == XLOG_STATE_DIRTY) {
  2289. iclog->ic_state = XLOG_STATE_ACTIVE;
  2290. iclog->ic_offset = 0;
  2291. ASSERT(iclog->ic_callback == NULL);
  2292. /*
  2293. * If the number of ops in this iclog indicate it just
  2294. * contains the dummy transaction, we can
  2295. * change state into IDLE (the second time around).
  2296. * Otherwise we should change the state into
  2297. * NEED a dummy.
  2298. * We don't need to cover the dummy.
  2299. */
  2300. if (!changed &&
  2301. (be32_to_cpu(iclog->ic_header.h_num_logops) ==
  2302. XLOG_COVER_OPS)) {
  2303. changed = 1;
  2304. } else {
  2305. /*
  2306. * We have two dirty iclogs so start over
  2307. * This could also be num of ops indicates
  2308. * this is not the dummy going out.
  2309. */
  2310. changed = 2;
  2311. }
  2312. iclog->ic_header.h_num_logops = 0;
  2313. memset(iclog->ic_header.h_cycle_data, 0,
  2314. sizeof(iclog->ic_header.h_cycle_data));
  2315. iclog->ic_header.h_lsn = 0;
  2316. } else if (iclog->ic_state == XLOG_STATE_ACTIVE)
  2317. /* do nothing */;
  2318. else
  2319. break; /* stop cleaning */
  2320. iclog = iclog->ic_next;
  2321. } while (iclog != log->l_iclog);
  2322. /* log is locked when we are called */
  2323. /*
  2324. * Change state for the dummy log recording.
  2325. * We usually go to NEED. But we go to NEED2 if the changed indicates
  2326. * we are done writing the dummy record.
  2327. * If we are done with the second dummy recored (DONE2), then
  2328. * we go to IDLE.
  2329. */
  2330. if (changed) {
  2331. switch (log->l_covered_state) {
  2332. case XLOG_STATE_COVER_IDLE:
  2333. case XLOG_STATE_COVER_NEED:
  2334. case XLOG_STATE_COVER_NEED2:
  2335. log->l_covered_state = XLOG_STATE_COVER_NEED;
  2336. break;
  2337. case XLOG_STATE_COVER_DONE:
  2338. if (changed == 1)
  2339. log->l_covered_state = XLOG_STATE_COVER_NEED2;
  2340. else
  2341. log->l_covered_state = XLOG_STATE_COVER_NEED;
  2342. break;
  2343. case XLOG_STATE_COVER_DONE2:
  2344. if (changed == 1)
  2345. log->l_covered_state = XLOG_STATE_COVER_IDLE;
  2346. else
  2347. log->l_covered_state = XLOG_STATE_COVER_NEED;
  2348. break;
  2349. default:
  2350. ASSERT(0);
  2351. }
  2352. }
  2353. } /* xlog_state_clean_log */
  2354. STATIC xfs_lsn_t
  2355. xlog_get_lowest_lsn(
  2356. struct xlog *log)
  2357. {
  2358. xlog_in_core_t *lsn_log;
  2359. xfs_lsn_t lowest_lsn, lsn;
  2360. lsn_log = log->l_iclog;
  2361. lowest_lsn = 0;
  2362. do {
  2363. if (!(lsn_log->ic_state & (XLOG_STATE_ACTIVE|XLOG_STATE_DIRTY))) {
  2364. lsn = be64_to_cpu(lsn_log->ic_header.h_lsn);
  2365. if ((lsn && !lowest_lsn) ||
  2366. (XFS_LSN_CMP(lsn, lowest_lsn) < 0)) {
  2367. lowest_lsn = lsn;
  2368. }
  2369. }
  2370. lsn_log = lsn_log->ic_next;
  2371. } while (lsn_log != log->l_iclog);
  2372. return lowest_lsn;
  2373. }
  2374. STATIC void
  2375. xlog_state_do_callback(
  2376. struct xlog *log,
  2377. int aborted,
  2378. struct xlog_in_core *ciclog)
  2379. {
  2380. xlog_in_core_t *iclog;
  2381. xlog_in_core_t *first_iclog; /* used to know when we've
  2382. * processed all iclogs once */
  2383. xfs_log_callback_t *cb, *cb_next;
  2384. int flushcnt = 0;
  2385. xfs_lsn_t lowest_lsn;
  2386. int ioerrors; /* counter: iclogs with errors */
  2387. int loopdidcallbacks; /* flag: inner loop did callbacks*/
  2388. int funcdidcallbacks; /* flag: function did callbacks */
  2389. int repeats; /* for issuing console warnings if
  2390. * looping too many times */
  2391. spin_lock(&log->l_icloglock);
  2392. first_iclog = iclog = log->l_iclog;
  2393. ioerrors = 0;
  2394. funcdidcallbacks = 0;
  2395. repeats = 0;
  2396. do {
  2397. /*
  2398. * Scan all iclogs starting with the one pointed to by the
  2399. * log. Reset this starting point each time the log is
  2400. * unlocked (during callbacks).
  2401. *
  2402. * Keep looping through iclogs until one full pass is made
  2403. * without running any callbacks.
  2404. */
  2405. first_iclog = log->l_iclog;
  2406. iclog = log->l_iclog;
  2407. loopdidcallbacks = 0;
  2408. repeats++;
  2409. do {
  2410. /* skip all iclogs in the ACTIVE & DIRTY states */
  2411. if (iclog->ic_state &
  2412. (XLOG_STATE_ACTIVE|XLOG_STATE_DIRTY)) {
  2413. iclog = iclog->ic_next;
  2414. continue;
  2415. }
  2416. /*
  2417. * Between marking a filesystem SHUTDOWN and stopping
  2418. * the log, we do flush all iclogs to disk (if there
  2419. * wasn't a log I/O error). So, we do want things to
  2420. * go smoothly in case of just a SHUTDOWN w/o a
  2421. * LOG_IO_ERROR.
  2422. */
  2423. if (!(iclog->ic_state & XLOG_STATE_IOERROR)) {
  2424. /*
  2425. * Can only perform callbacks in order. Since
  2426. * this iclog is not in the DONE_SYNC/
  2427. * DO_CALLBACK state, we skip the rest and
  2428. * just try to clean up. If we set our iclog
  2429. * to DO_CALLBACK, we will not process it when
  2430. * we retry since a previous iclog is in the
  2431. * CALLBACK and the state cannot change since
  2432. * we are holding the l_icloglock.
  2433. */
  2434. if (!(iclog->ic_state &
  2435. (XLOG_STATE_DONE_SYNC |
  2436. XLOG_STATE_DO_CALLBACK))) {
  2437. if (ciclog && (ciclog->ic_state ==
  2438. XLOG_STATE_DONE_SYNC)) {
  2439. ciclog->ic_state = XLOG_STATE_DO_CALLBACK;
  2440. }
  2441. break;
  2442. }
  2443. /*
  2444. * We now have an iclog that is in either the
  2445. * DO_CALLBACK or DONE_SYNC states. The other
  2446. * states (WANT_SYNC, SYNCING, or CALLBACK were
  2447. * caught by the above if and are going to
  2448. * clean (i.e. we aren't doing their callbacks)
  2449. * see the above if.
  2450. */
  2451. /*
  2452. * We will do one more check here to see if we
  2453. * have chased our tail around.
  2454. */
  2455. lowest_lsn = xlog_get_lowest_lsn(log);
  2456. if (lowest_lsn &&
  2457. XFS_LSN_CMP(lowest_lsn,
  2458. be64_to_cpu(iclog->ic_header.h_lsn)) < 0) {
  2459. iclog = iclog->ic_next;
  2460. continue; /* Leave this iclog for
  2461. * another thread */
  2462. }
  2463. iclog->ic_state = XLOG_STATE_CALLBACK;
  2464. /*
  2465. * Completion of a iclog IO does not imply that
  2466. * a transaction has completed, as transactions
  2467. * can be large enough to span many iclogs. We
  2468. * cannot change the tail of the log half way
  2469. * through a transaction as this may be the only
  2470. * transaction in the log and moving th etail to
  2471. * point to the middle of it will prevent
  2472. * recovery from finding the start of the
  2473. * transaction. Hence we should only update the
  2474. * last_sync_lsn if this iclog contains
  2475. * transaction completion callbacks on it.
  2476. *
  2477. * We have to do this before we drop the
  2478. * icloglock to ensure we are the only one that
  2479. * can update it.
  2480. */
  2481. ASSERT(XFS_LSN_CMP(atomic64_read(&log->l_last_sync_lsn),
  2482. be64_to_cpu(iclog->ic_header.h_lsn)) <= 0);
  2483. if (iclog->ic_callback)
  2484. atomic64_set(&log->l_last_sync_lsn,
  2485. be64_to_cpu(iclog->ic_header.h_lsn));
  2486. } else
  2487. ioerrors++;
  2488. spin_unlock(&log->l_icloglock);
  2489. /*
  2490. * Keep processing entries in the callback list until
  2491. * we come around and it is empty. We need to
  2492. * atomically see that the list is empty and change the
  2493. * state to DIRTY so that we don't miss any more
  2494. * callbacks being added.
  2495. */
  2496. spin_lock(&iclog->ic_callback_lock);
  2497. cb = iclog->ic_callback;
  2498. while (cb) {
  2499. iclog->ic_callback_tail = &(iclog->ic_callback);
  2500. iclog->ic_callback = NULL;
  2501. spin_unlock(&iclog->ic_callback_lock);
  2502. /* perform callbacks in the order given */
  2503. for (; cb; cb = cb_next) {
  2504. cb_next = cb->cb_next;
  2505. cb->cb_func(cb->cb_arg, aborted);
  2506. }
  2507. spin_lock(&iclog->ic_callback_lock);
  2508. cb = iclog->ic_callback;
  2509. }
  2510. loopdidcallbacks++;
  2511. funcdidcallbacks++;
  2512. spin_lock(&log->l_icloglock);
  2513. ASSERT(iclog->ic_callback == NULL);
  2514. spin_unlock(&iclog->ic_callback_lock);
  2515. if (!(iclog->ic_state & XLOG_STATE_IOERROR))
  2516. iclog->ic_state = XLOG_STATE_DIRTY;
  2517. /*
  2518. * Transition from DIRTY to ACTIVE if applicable.
  2519. * NOP if STATE_IOERROR.
  2520. */
  2521. xlog_state_clean_log(log);
  2522. /* wake up threads waiting in xfs_log_force() */
  2523. wake_up_all(&iclog->ic_force_wait);
  2524. iclog = iclog->ic_next;
  2525. } while (first_iclog != iclog);
  2526. if (repeats > 5000) {
  2527. flushcnt += repeats;
  2528. repeats = 0;
  2529. xfs_warn(log->l_mp,
  2530. "%s: possible infinite loop (%d iterations)",
  2531. __func__, flushcnt);
  2532. }
  2533. } while (!ioerrors && loopdidcallbacks);
  2534. #ifdef DEBUG
  2535. /*
  2536. * Make one last gasp attempt to see if iclogs are being left in limbo.
  2537. * If the above loop finds an iclog earlier than the current iclog and
  2538. * in one of the syncing states, the current iclog is put into
  2539. * DO_CALLBACK and the callbacks are deferred to the completion of the
  2540. * earlier iclog. Walk the iclogs in order and make sure that no iclog
  2541. * is in DO_CALLBACK unless an earlier iclog is in one of the syncing
  2542. * states.
  2543. *
  2544. * Note that SYNCING|IOABORT is a valid state so we cannot just check
  2545. * for ic_state == SYNCING.
  2546. */
  2547. if (funcdidcallbacks) {
  2548. first_iclog = iclog = log->l_iclog;
  2549. do {
  2550. ASSERT(iclog->ic_state != XLOG_STATE_DO_CALLBACK);
  2551. /*
  2552. * Terminate the loop if iclogs are found in states
  2553. * which will cause other threads to clean up iclogs.
  2554. *
  2555. * SYNCING - i/o completion will go through logs
  2556. * DONE_SYNC - interrupt thread should be waiting for
  2557. * l_icloglock
  2558. * IOERROR - give up hope all ye who enter here
  2559. */
  2560. if (iclog->ic_state == XLOG_STATE_WANT_SYNC ||
  2561. iclog->ic_state & XLOG_STATE_SYNCING ||
  2562. iclog->ic_state == XLOG_STATE_DONE_SYNC ||
  2563. iclog->ic_state == XLOG_STATE_IOERROR )
  2564. break;
  2565. iclog = iclog->ic_next;
  2566. } while (first_iclog != iclog);
  2567. }
  2568. #endif
  2569. if (log->l_iclog->ic_state & (XLOG_STATE_ACTIVE|XLOG_STATE_IOERROR))
  2570. wake_up_all(&log->l_flush_wait);
  2571. spin_unlock(&log->l_icloglock);
  2572. }
  2573. /*
  2574. * Finish transitioning this iclog to the dirty state.
  2575. *
  2576. * Make sure that we completely execute this routine only when this is
  2577. * the last call to the iclog. There is a good chance that iclog flushes,
  2578. * when we reach the end of the physical log, get turned into 2 separate
  2579. * calls to bwrite. Hence, one iclog flush could generate two calls to this
  2580. * routine. By using the reference count bwritecnt, we guarantee that only
  2581. * the second completion goes through.
  2582. *
  2583. * Callbacks could take time, so they are done outside the scope of the
  2584. * global state machine log lock.
  2585. */
  2586. STATIC void
  2587. xlog_state_done_syncing(
  2588. xlog_in_core_t *iclog,
  2589. int aborted)
  2590. {
  2591. struct xlog *log = iclog->ic_log;
  2592. spin_lock(&log->l_icloglock);
  2593. ASSERT(iclog->ic_state == XLOG_STATE_SYNCING ||
  2594. iclog->ic_state == XLOG_STATE_IOERROR);
  2595. ASSERT(atomic_read(&iclog->ic_refcnt) == 0);
  2596. ASSERT(iclog->ic_bwritecnt == 1 || iclog->ic_bwritecnt == 2);
  2597. /*
  2598. * If we got an error, either on the first buffer, or in the case of
  2599. * split log writes, on the second, we mark ALL iclogs STATE_IOERROR,
  2600. * and none should ever be attempted to be written to disk
  2601. * again.
  2602. */
  2603. if (iclog->ic_state != XLOG_STATE_IOERROR) {
  2604. if (--iclog->ic_bwritecnt == 1) {
  2605. spin_unlock(&log->l_icloglock);
  2606. return;
  2607. }
  2608. iclog->ic_state = XLOG_STATE_DONE_SYNC;
  2609. }
  2610. /*
  2611. * Someone could be sleeping prior to writing out the next
  2612. * iclog buffer, we wake them all, one will get to do the
  2613. * I/O, the others get to wait for the result.
  2614. */
  2615. wake_up_all(&iclog->ic_write_wait);
  2616. spin_unlock(&log->l_icloglock);
  2617. xlog_state_do_callback(log, aborted, iclog); /* also cleans log */
  2618. } /* xlog_state_done_syncing */
  2619. /*
  2620. * If the head of the in-core log ring is not (ACTIVE or DIRTY), then we must
  2621. * sleep. We wait on the flush queue on the head iclog as that should be
  2622. * the first iclog to complete flushing. Hence if all iclogs are syncing,
  2623. * we will wait here and all new writes will sleep until a sync completes.
  2624. *
  2625. * The in-core logs are used in a circular fashion. They are not used
  2626. * out-of-order even when an iclog past the head is free.
  2627. *
  2628. * return:
  2629. * * log_offset where xlog_write() can start writing into the in-core
  2630. * log's data space.
  2631. * * in-core log pointer to which xlog_write() should write.
  2632. * * boolean indicating this is a continued write to an in-core log.
  2633. * If this is the last write, then the in-core log's offset field
  2634. * needs to be incremented, depending on the amount of data which
  2635. * is copied.
  2636. */
  2637. STATIC int
  2638. xlog_state_get_iclog_space(
  2639. struct xlog *log,
  2640. int len,
  2641. struct xlog_in_core **iclogp,
  2642. struct xlog_ticket *ticket,
  2643. int *continued_write,
  2644. int *logoffsetp)
  2645. {
  2646. int log_offset;
  2647. xlog_rec_header_t *head;
  2648. xlog_in_core_t *iclog;
  2649. int error;
  2650. restart:
  2651. spin_lock(&log->l_icloglock);
  2652. if (XLOG_FORCED_SHUTDOWN(log)) {
  2653. spin_unlock(&log->l_icloglock);
  2654. return -EIO;
  2655. }
  2656. iclog = log->l_iclog;
  2657. if (iclog->ic_state != XLOG_STATE_ACTIVE) {
  2658. XFS_STATS_INC(log->l_mp, xs_log_noiclogs);
  2659. /* Wait for log writes to have flushed */
  2660. xlog_wait(&log->l_flush_wait, &log->l_icloglock);
  2661. goto restart;
  2662. }
  2663. head = &iclog->ic_header;
  2664. atomic_inc(&iclog->ic_refcnt); /* prevents sync */
  2665. log_offset = iclog->ic_offset;
  2666. /* On the 1st write to an iclog, figure out lsn. This works
  2667. * if iclogs marked XLOG_STATE_WANT_SYNC always write out what they are
  2668. * committing to. If the offset is set, that's how many blocks
  2669. * must be written.
  2670. */
  2671. if (log_offset == 0) {
  2672. ticket->t_curr_res -= log->l_iclog_hsize;
  2673. xlog_tic_add_region(ticket,
  2674. log->l_iclog_hsize,
  2675. XLOG_REG_TYPE_LRHEADER);
  2676. head->h_cycle = cpu_to_be32(log->l_curr_cycle);
  2677. head->h_lsn = cpu_to_be64(
  2678. xlog_assign_lsn(log->l_curr_cycle, log->l_curr_block));
  2679. ASSERT(log->l_curr_block >= 0);
  2680. }
  2681. /* If there is enough room to write everything, then do it. Otherwise,
  2682. * claim the rest of the region and make sure the XLOG_STATE_WANT_SYNC
  2683. * bit is on, so this will get flushed out. Don't update ic_offset
  2684. * until you know exactly how many bytes get copied. Therefore, wait
  2685. * until later to update ic_offset.
  2686. *
  2687. * xlog_write() algorithm assumes that at least 2 xlog_op_header_t's
  2688. * can fit into remaining data section.
  2689. */
  2690. if (iclog->ic_size - iclog->ic_offset < 2*sizeof(xlog_op_header_t)) {
  2691. xlog_state_switch_iclogs(log, iclog, iclog->ic_size);
  2692. /*
  2693. * If I'm the only one writing to this iclog, sync it to disk.
  2694. * We need to do an atomic compare and decrement here to avoid
  2695. * racing with concurrent atomic_dec_and_lock() calls in
  2696. * xlog_state_release_iclog() when there is more than one
  2697. * reference to the iclog.
  2698. */
  2699. if (!atomic_add_unless(&iclog->ic_refcnt, -1, 1)) {
  2700. /* we are the only one */
  2701. spin_unlock(&log->l_icloglock);
  2702. error = xlog_state_release_iclog(log, iclog);
  2703. if (error)
  2704. return error;
  2705. } else {
  2706. spin_unlock(&log->l_icloglock);
  2707. }
  2708. goto restart;
  2709. }
  2710. /* Do we have enough room to write the full amount in the remainder
  2711. * of this iclog? Or must we continue a write on the next iclog and
  2712. * mark this iclog as completely taken? In the case where we switch
  2713. * iclogs (to mark it taken), this particular iclog will release/sync
  2714. * to disk in xlog_write().
  2715. */
  2716. if (len <= iclog->ic_size - iclog->ic_offset) {
  2717. *continued_write = 0;
  2718. iclog->ic_offset += len;
  2719. } else {
  2720. *continued_write = 1;
  2721. xlog_state_switch_iclogs(log, iclog, iclog->ic_size);
  2722. }
  2723. *iclogp = iclog;
  2724. ASSERT(iclog->ic_offset <= iclog->ic_size);
  2725. spin_unlock(&log->l_icloglock);
  2726. *logoffsetp = log_offset;
  2727. return 0;
  2728. } /* xlog_state_get_iclog_space */
  2729. /* The first cnt-1 times through here we don't need to
  2730. * move the grant write head because the permanent
  2731. * reservation has reserved cnt times the unit amount.
  2732. * Release part of current permanent unit reservation and
  2733. * reset current reservation to be one units worth. Also
  2734. * move grant reservation head forward.
  2735. */
  2736. STATIC void
  2737. xlog_regrant_reserve_log_space(
  2738. struct xlog *log,
  2739. struct xlog_ticket *ticket)
  2740. {
  2741. trace_xfs_log_regrant_reserve_enter(log, ticket);
  2742. if (ticket->t_cnt > 0)
  2743. ticket->t_cnt--;
  2744. xlog_grant_sub_space(log, &log->l_reserve_head.grant,
  2745. ticket->t_curr_res);
  2746. xlog_grant_sub_space(log, &log->l_write_head.grant,
  2747. ticket->t_curr_res);
  2748. ticket->t_curr_res = ticket->t_unit_res;
  2749. xlog_tic_reset_res(ticket);
  2750. trace_xfs_log_regrant_reserve_sub(log, ticket);
  2751. /* just return if we still have some of the pre-reserved space */
  2752. if (ticket->t_cnt > 0)
  2753. return;
  2754. xlog_grant_add_space(log, &log->l_reserve_head.grant,
  2755. ticket->t_unit_res);
  2756. trace_xfs_log_regrant_reserve_exit(log, ticket);
  2757. ticket->t_curr_res = ticket->t_unit_res;
  2758. xlog_tic_reset_res(ticket);
  2759. } /* xlog_regrant_reserve_log_space */
  2760. /*
  2761. * Give back the space left from a reservation.
  2762. *
  2763. * All the information we need to make a correct determination of space left
  2764. * is present. For non-permanent reservations, things are quite easy. The
  2765. * count should have been decremented to zero. We only need to deal with the
  2766. * space remaining in the current reservation part of the ticket. If the
  2767. * ticket contains a permanent reservation, there may be left over space which
  2768. * needs to be released. A count of N means that N-1 refills of the current
  2769. * reservation can be done before we need to ask for more space. The first
  2770. * one goes to fill up the first current reservation. Once we run out of
  2771. * space, the count will stay at zero and the only space remaining will be
  2772. * in the current reservation field.
  2773. */
  2774. STATIC void
  2775. xlog_ungrant_log_space(
  2776. struct xlog *log,
  2777. struct xlog_ticket *ticket)
  2778. {
  2779. int bytes;
  2780. if (ticket->t_cnt > 0)
  2781. ticket->t_cnt--;
  2782. trace_xfs_log_ungrant_enter(log, ticket);
  2783. trace_xfs_log_ungrant_sub(log, ticket);
  2784. /*
  2785. * If this is a permanent reservation ticket, we may be able to free
  2786. * up more space based on the remaining count.
  2787. */
  2788. bytes = ticket->t_curr_res;
  2789. if (ticket->t_cnt > 0) {
  2790. ASSERT(ticket->t_flags & XLOG_TIC_PERM_RESERV);
  2791. bytes += ticket->t_unit_res*ticket->t_cnt;
  2792. }
  2793. xlog_grant_sub_space(log, &log->l_reserve_head.grant, bytes);
  2794. xlog_grant_sub_space(log, &log->l_write_head.grant, bytes);
  2795. trace_xfs_log_ungrant_exit(log, ticket);
  2796. xfs_log_space_wake(log->l_mp);
  2797. }
  2798. /*
  2799. * Flush iclog to disk if this is the last reference to the given iclog and
  2800. * the WANT_SYNC bit is set.
  2801. *
  2802. * When this function is entered, the iclog is not necessarily in the
  2803. * WANT_SYNC state. It may be sitting around waiting to get filled.
  2804. *
  2805. *
  2806. */
  2807. STATIC int
  2808. xlog_state_release_iclog(
  2809. struct xlog *log,
  2810. struct xlog_in_core *iclog)
  2811. {
  2812. int sync = 0; /* do we sync? */
  2813. if (iclog->ic_state & XLOG_STATE_IOERROR)
  2814. return -EIO;
  2815. ASSERT(atomic_read(&iclog->ic_refcnt) > 0);
  2816. if (!atomic_dec_and_lock(&iclog->ic_refcnt, &log->l_icloglock))
  2817. return 0;
  2818. if (iclog->ic_state & XLOG_STATE_IOERROR) {
  2819. spin_unlock(&log->l_icloglock);
  2820. return -EIO;
  2821. }
  2822. ASSERT(iclog->ic_state == XLOG_STATE_ACTIVE ||
  2823. iclog->ic_state == XLOG_STATE_WANT_SYNC);
  2824. if (iclog->ic_state == XLOG_STATE_WANT_SYNC) {
  2825. /* update tail before writing to iclog */
  2826. xfs_lsn_t tail_lsn = xlog_assign_tail_lsn(log->l_mp);
  2827. sync++;
  2828. iclog->ic_state = XLOG_STATE_SYNCING;
  2829. iclog->ic_header.h_tail_lsn = cpu_to_be64(tail_lsn);
  2830. xlog_verify_tail_lsn(log, iclog, tail_lsn);
  2831. /* cycle incremented when incrementing curr_block */
  2832. }
  2833. spin_unlock(&log->l_icloglock);
  2834. /*
  2835. * We let the log lock go, so it's possible that we hit a log I/O
  2836. * error or some other SHUTDOWN condition that marks the iclog
  2837. * as XLOG_STATE_IOERROR before the bwrite. However, we know that
  2838. * this iclog has consistent data, so we ignore IOERROR
  2839. * flags after this point.
  2840. */
  2841. if (sync)
  2842. return xlog_sync(log, iclog);
  2843. return 0;
  2844. } /* xlog_state_release_iclog */
  2845. /*
  2846. * This routine will mark the current iclog in the ring as WANT_SYNC
  2847. * and move the current iclog pointer to the next iclog in the ring.
  2848. * When this routine is called from xlog_state_get_iclog_space(), the
  2849. * exact size of the iclog has not yet been determined. All we know is
  2850. * that every data block. We have run out of space in this log record.
  2851. */
  2852. STATIC void
  2853. xlog_state_switch_iclogs(
  2854. struct xlog *log,
  2855. struct xlog_in_core *iclog,
  2856. int eventual_size)
  2857. {
  2858. ASSERT(iclog->ic_state == XLOG_STATE_ACTIVE);
  2859. if (!eventual_size)
  2860. eventual_size = iclog->ic_offset;
  2861. iclog->ic_state = XLOG_STATE_WANT_SYNC;
  2862. iclog->ic_header.h_prev_block = cpu_to_be32(log->l_prev_block);
  2863. log->l_prev_block = log->l_curr_block;
  2864. log->l_prev_cycle = log->l_curr_cycle;
  2865. /* roll log?: ic_offset changed later */
  2866. log->l_curr_block += BTOBB(eventual_size)+BTOBB(log->l_iclog_hsize);
  2867. /* Round up to next log-sunit */
  2868. if (xfs_sb_version_haslogv2(&log->l_mp->m_sb) &&
  2869. log->l_mp->m_sb.sb_logsunit > 1) {
  2870. uint32_t sunit_bb = BTOBB(log->l_mp->m_sb.sb_logsunit);
  2871. log->l_curr_block = roundup(log->l_curr_block, sunit_bb);
  2872. }
  2873. if (log->l_curr_block >= log->l_logBBsize) {
  2874. /*
  2875. * Rewind the current block before the cycle is bumped to make
  2876. * sure that the combined LSN never transiently moves forward
  2877. * when the log wraps to the next cycle. This is to support the
  2878. * unlocked sample of these fields from xlog_valid_lsn(). Most
  2879. * other cases should acquire l_icloglock.
  2880. */
  2881. log->l_curr_block -= log->l_logBBsize;
  2882. ASSERT(log->l_curr_block >= 0);
  2883. smp_wmb();
  2884. log->l_curr_cycle++;
  2885. if (log->l_curr_cycle == XLOG_HEADER_MAGIC_NUM)
  2886. log->l_curr_cycle++;
  2887. }
  2888. ASSERT(iclog == log->l_iclog);
  2889. log->l_iclog = iclog->ic_next;
  2890. } /* xlog_state_switch_iclogs */
  2891. /*
  2892. * Write out all data in the in-core log as of this exact moment in time.
  2893. *
  2894. * Data may be written to the in-core log during this call. However,
  2895. * we don't guarantee this data will be written out. A change from past
  2896. * implementation means this routine will *not* write out zero length LRs.
  2897. *
  2898. * Basically, we try and perform an intelligent scan of the in-core logs.
  2899. * If we determine there is no flushable data, we just return. There is no
  2900. * flushable data if:
  2901. *
  2902. * 1. the current iclog is active and has no data; the previous iclog
  2903. * is in the active or dirty state.
  2904. * 2. the current iclog is drity, and the previous iclog is in the
  2905. * active or dirty state.
  2906. *
  2907. * We may sleep if:
  2908. *
  2909. * 1. the current iclog is not in the active nor dirty state.
  2910. * 2. the current iclog dirty, and the previous iclog is not in the
  2911. * active nor dirty state.
  2912. * 3. the current iclog is active, and there is another thread writing
  2913. * to this particular iclog.
  2914. * 4. a) the current iclog is active and has no other writers
  2915. * b) when we return from flushing out this iclog, it is still
  2916. * not in the active nor dirty state.
  2917. */
  2918. int
  2919. xfs_log_force(
  2920. struct xfs_mount *mp,
  2921. uint flags)
  2922. {
  2923. struct xlog *log = mp->m_log;
  2924. struct xlog_in_core *iclog;
  2925. xfs_lsn_t lsn;
  2926. XFS_STATS_INC(mp, xs_log_force);
  2927. trace_xfs_log_force(mp, 0, _RET_IP_);
  2928. xlog_cil_force(log);
  2929. spin_lock(&log->l_icloglock);
  2930. iclog = log->l_iclog;
  2931. if (iclog->ic_state & XLOG_STATE_IOERROR)
  2932. goto out_error;
  2933. if (iclog->ic_state == XLOG_STATE_DIRTY ||
  2934. (iclog->ic_state == XLOG_STATE_ACTIVE &&
  2935. atomic_read(&iclog->ic_refcnt) == 0 && iclog->ic_offset == 0)) {
  2936. /*
  2937. * If the head is dirty or (active and empty), then we need to
  2938. * look at the previous iclog.
  2939. *
  2940. * If the previous iclog is active or dirty we are done. There
  2941. * is nothing to sync out. Otherwise, we attach ourselves to the
  2942. * previous iclog and go to sleep.
  2943. */
  2944. iclog = iclog->ic_prev;
  2945. if (iclog->ic_state == XLOG_STATE_ACTIVE ||
  2946. iclog->ic_state == XLOG_STATE_DIRTY)
  2947. goto out_unlock;
  2948. } else if (iclog->ic_state == XLOG_STATE_ACTIVE) {
  2949. if (atomic_read(&iclog->ic_refcnt) == 0) {
  2950. /*
  2951. * We are the only one with access to this iclog.
  2952. *
  2953. * Flush it out now. There should be a roundoff of zero
  2954. * to show that someone has already taken care of the
  2955. * roundoff from the previous sync.
  2956. */
  2957. atomic_inc(&iclog->ic_refcnt);
  2958. lsn = be64_to_cpu(iclog->ic_header.h_lsn);
  2959. xlog_state_switch_iclogs(log, iclog, 0);
  2960. spin_unlock(&log->l_icloglock);
  2961. if (xlog_state_release_iclog(log, iclog))
  2962. return -EIO;
  2963. spin_lock(&log->l_icloglock);
  2964. if (be64_to_cpu(iclog->ic_header.h_lsn) != lsn ||
  2965. iclog->ic_state == XLOG_STATE_DIRTY)
  2966. goto out_unlock;
  2967. } else {
  2968. /*
  2969. * Someone else is writing to this iclog.
  2970. *
  2971. * Use its call to flush out the data. However, the
  2972. * other thread may not force out this LR, so we mark
  2973. * it WANT_SYNC.
  2974. */
  2975. xlog_state_switch_iclogs(log, iclog, 0);
  2976. }
  2977. } else {
  2978. /*
  2979. * If the head iclog is not active nor dirty, we just attach
  2980. * ourselves to the head and go to sleep if necessary.
  2981. */
  2982. ;
  2983. }
  2984. if (!(flags & XFS_LOG_SYNC))
  2985. goto out_unlock;
  2986. if (iclog->ic_state & XLOG_STATE_IOERROR)
  2987. goto out_error;
  2988. XFS_STATS_INC(mp, xs_log_force_sleep);
  2989. xlog_wait(&iclog->ic_force_wait, &log->l_icloglock);
  2990. if (iclog->ic_state & XLOG_STATE_IOERROR)
  2991. return -EIO;
  2992. return 0;
  2993. out_unlock:
  2994. spin_unlock(&log->l_icloglock);
  2995. return 0;
  2996. out_error:
  2997. spin_unlock(&log->l_icloglock);
  2998. return -EIO;
  2999. }
  3000. static int
  3001. __xfs_log_force_lsn(
  3002. struct xfs_mount *mp,
  3003. xfs_lsn_t lsn,
  3004. uint flags,
  3005. int *log_flushed,
  3006. bool already_slept)
  3007. {
  3008. struct xlog *log = mp->m_log;
  3009. struct xlog_in_core *iclog;
  3010. spin_lock(&log->l_icloglock);
  3011. iclog = log->l_iclog;
  3012. if (iclog->ic_state & XLOG_STATE_IOERROR)
  3013. goto out_error;
  3014. while (be64_to_cpu(iclog->ic_header.h_lsn) != lsn) {
  3015. iclog = iclog->ic_next;
  3016. if (iclog == log->l_iclog)
  3017. goto out_unlock;
  3018. }
  3019. if (iclog->ic_state == XLOG_STATE_DIRTY)
  3020. goto out_unlock;
  3021. if (iclog->ic_state == XLOG_STATE_ACTIVE) {
  3022. /*
  3023. * We sleep here if we haven't already slept (e.g. this is the
  3024. * first time we've looked at the correct iclog buf) and the
  3025. * buffer before us is going to be sync'ed. The reason for this
  3026. * is that if we are doing sync transactions here, by waiting
  3027. * for the previous I/O to complete, we can allow a few more
  3028. * transactions into this iclog before we close it down.
  3029. *
  3030. * Otherwise, we mark the buffer WANT_SYNC, and bump up the
  3031. * refcnt so we can release the log (which drops the ref count).
  3032. * The state switch keeps new transaction commits from using
  3033. * this buffer. When the current commits finish writing into
  3034. * the buffer, the refcount will drop to zero and the buffer
  3035. * will go out then.
  3036. */
  3037. if (!already_slept &&
  3038. (iclog->ic_prev->ic_state &
  3039. (XLOG_STATE_WANT_SYNC | XLOG_STATE_SYNCING))) {
  3040. ASSERT(!(iclog->ic_state & XLOG_STATE_IOERROR));
  3041. XFS_STATS_INC(mp, xs_log_force_sleep);
  3042. xlog_wait(&iclog->ic_prev->ic_write_wait,
  3043. &log->l_icloglock);
  3044. return -EAGAIN;
  3045. }
  3046. atomic_inc(&iclog->ic_refcnt);
  3047. xlog_state_switch_iclogs(log, iclog, 0);
  3048. spin_unlock(&log->l_icloglock);
  3049. if (xlog_state_release_iclog(log, iclog))
  3050. return -EIO;
  3051. if (log_flushed)
  3052. *log_flushed = 1;
  3053. spin_lock(&log->l_icloglock);
  3054. }
  3055. if (!(flags & XFS_LOG_SYNC) ||
  3056. (iclog->ic_state & (XLOG_STATE_ACTIVE | XLOG_STATE_DIRTY)))
  3057. goto out_unlock;
  3058. if (iclog->ic_state & XLOG_STATE_IOERROR)
  3059. goto out_error;
  3060. XFS_STATS_INC(mp, xs_log_force_sleep);
  3061. xlog_wait(&iclog->ic_force_wait, &log->l_icloglock);
  3062. if (iclog->ic_state & XLOG_STATE_IOERROR)
  3063. return -EIO;
  3064. return 0;
  3065. out_unlock:
  3066. spin_unlock(&log->l_icloglock);
  3067. return 0;
  3068. out_error:
  3069. spin_unlock(&log->l_icloglock);
  3070. return -EIO;
  3071. }
  3072. /*
  3073. * Force the in-core log to disk for a specific LSN.
  3074. *
  3075. * Find in-core log with lsn.
  3076. * If it is in the DIRTY state, just return.
  3077. * If it is in the ACTIVE state, move the in-core log into the WANT_SYNC
  3078. * state and go to sleep or return.
  3079. * If it is in any other state, go to sleep or return.
  3080. *
  3081. * Synchronous forces are implemented with a wait queue. All callers trying
  3082. * to force a given lsn to disk must wait on the queue attached to the
  3083. * specific in-core log. When given in-core log finally completes its write
  3084. * to disk, that thread will wake up all threads waiting on the queue.
  3085. */
  3086. int
  3087. xfs_log_force_lsn(
  3088. struct xfs_mount *mp,
  3089. xfs_lsn_t lsn,
  3090. uint flags,
  3091. int *log_flushed)
  3092. {
  3093. int ret;
  3094. ASSERT(lsn != 0);
  3095. XFS_STATS_INC(mp, xs_log_force);
  3096. trace_xfs_log_force(mp, lsn, _RET_IP_);
  3097. lsn = xlog_cil_force_lsn(mp->m_log, lsn);
  3098. if (lsn == NULLCOMMITLSN)
  3099. return 0;
  3100. ret = __xfs_log_force_lsn(mp, lsn, flags, log_flushed, false);
  3101. if (ret == -EAGAIN)
  3102. ret = __xfs_log_force_lsn(mp, lsn, flags, log_flushed, true);
  3103. return ret;
  3104. }
  3105. /*
  3106. * Called when we want to mark the current iclog as being ready to sync to
  3107. * disk.
  3108. */
  3109. STATIC void
  3110. xlog_state_want_sync(
  3111. struct xlog *log,
  3112. struct xlog_in_core *iclog)
  3113. {
  3114. assert_spin_locked(&log->l_icloglock);
  3115. if (iclog->ic_state == XLOG_STATE_ACTIVE) {
  3116. xlog_state_switch_iclogs(log, iclog, 0);
  3117. } else {
  3118. ASSERT(iclog->ic_state &
  3119. (XLOG_STATE_WANT_SYNC|XLOG_STATE_IOERROR));
  3120. }
  3121. }
  3122. /*****************************************************************************
  3123. *
  3124. * TICKET functions
  3125. *
  3126. *****************************************************************************
  3127. */
  3128. /*
  3129. * Free a used ticket when its refcount falls to zero.
  3130. */
  3131. void
  3132. xfs_log_ticket_put(
  3133. xlog_ticket_t *ticket)
  3134. {
  3135. ASSERT(atomic_read(&ticket->t_ref) > 0);
  3136. if (atomic_dec_and_test(&ticket->t_ref))
  3137. kmem_zone_free(xfs_log_ticket_zone, ticket);
  3138. }
  3139. xlog_ticket_t *
  3140. xfs_log_ticket_get(
  3141. xlog_ticket_t *ticket)
  3142. {
  3143. ASSERT(atomic_read(&ticket->t_ref) > 0);
  3144. atomic_inc(&ticket->t_ref);
  3145. return ticket;
  3146. }
  3147. /*
  3148. * Figure out the total log space unit (in bytes) that would be
  3149. * required for a log ticket.
  3150. */
  3151. int
  3152. xfs_log_calc_unit_res(
  3153. struct xfs_mount *mp,
  3154. int unit_bytes)
  3155. {
  3156. struct xlog *log = mp->m_log;
  3157. int iclog_space;
  3158. uint num_headers;
  3159. /*
  3160. * Permanent reservations have up to 'cnt'-1 active log operations
  3161. * in the log. A unit in this case is the amount of space for one
  3162. * of these log operations. Normal reservations have a cnt of 1
  3163. * and their unit amount is the total amount of space required.
  3164. *
  3165. * The following lines of code account for non-transaction data
  3166. * which occupy space in the on-disk log.
  3167. *
  3168. * Normal form of a transaction is:
  3169. * <oph><trans-hdr><start-oph><reg1-oph><reg1><reg2-oph>...<commit-oph>
  3170. * and then there are LR hdrs, split-recs and roundoff at end of syncs.
  3171. *
  3172. * We need to account for all the leadup data and trailer data
  3173. * around the transaction data.
  3174. * And then we need to account for the worst case in terms of using
  3175. * more space.
  3176. * The worst case will happen if:
  3177. * - the placement of the transaction happens to be such that the
  3178. * roundoff is at its maximum
  3179. * - the transaction data is synced before the commit record is synced
  3180. * i.e. <transaction-data><roundoff> | <commit-rec><roundoff>
  3181. * Therefore the commit record is in its own Log Record.
  3182. * This can happen as the commit record is called with its
  3183. * own region to xlog_write().
  3184. * This then means that in the worst case, roundoff can happen for
  3185. * the commit-rec as well.
  3186. * The commit-rec is smaller than padding in this scenario and so it is
  3187. * not added separately.
  3188. */
  3189. /* for trans header */
  3190. unit_bytes += sizeof(xlog_op_header_t);
  3191. unit_bytes += sizeof(xfs_trans_header_t);
  3192. /* for start-rec */
  3193. unit_bytes += sizeof(xlog_op_header_t);
  3194. /*
  3195. * for LR headers - the space for data in an iclog is the size minus
  3196. * the space used for the headers. If we use the iclog size, then we
  3197. * undercalculate the number of headers required.
  3198. *
  3199. * Furthermore - the addition of op headers for split-recs might
  3200. * increase the space required enough to require more log and op
  3201. * headers, so take that into account too.
  3202. *
  3203. * IMPORTANT: This reservation makes the assumption that if this
  3204. * transaction is the first in an iclog and hence has the LR headers
  3205. * accounted to it, then the remaining space in the iclog is
  3206. * exclusively for this transaction. i.e. if the transaction is larger
  3207. * than the iclog, it will be the only thing in that iclog.
  3208. * Fundamentally, this means we must pass the entire log vector to
  3209. * xlog_write to guarantee this.
  3210. */
  3211. iclog_space = log->l_iclog_size - log->l_iclog_hsize;
  3212. num_headers = howmany(unit_bytes, iclog_space);
  3213. /* for split-recs - ophdrs added when data split over LRs */
  3214. unit_bytes += sizeof(xlog_op_header_t) * num_headers;
  3215. /* add extra header reservations if we overrun */
  3216. while (!num_headers ||
  3217. howmany(unit_bytes, iclog_space) > num_headers) {
  3218. unit_bytes += sizeof(xlog_op_header_t);
  3219. num_headers++;
  3220. }
  3221. unit_bytes += log->l_iclog_hsize * num_headers;
  3222. /* for commit-rec LR header - note: padding will subsume the ophdr */
  3223. unit_bytes += log->l_iclog_hsize;
  3224. /* for roundoff padding for transaction data and one for commit record */
  3225. if (xfs_sb_version_haslogv2(&mp->m_sb) && mp->m_sb.sb_logsunit > 1) {
  3226. /* log su roundoff */
  3227. unit_bytes += 2 * mp->m_sb.sb_logsunit;
  3228. } else {
  3229. /* BB roundoff */
  3230. unit_bytes += 2 * BBSIZE;
  3231. }
  3232. return unit_bytes;
  3233. }
  3234. /*
  3235. * Allocate and initialise a new log ticket.
  3236. */
  3237. struct xlog_ticket *
  3238. xlog_ticket_alloc(
  3239. struct xlog *log,
  3240. int unit_bytes,
  3241. int cnt,
  3242. char client,
  3243. bool permanent,
  3244. xfs_km_flags_t alloc_flags)
  3245. {
  3246. struct xlog_ticket *tic;
  3247. int unit_res;
  3248. tic = kmem_zone_zalloc(xfs_log_ticket_zone, alloc_flags);
  3249. if (!tic)
  3250. return NULL;
  3251. unit_res = xfs_log_calc_unit_res(log->l_mp, unit_bytes);
  3252. atomic_set(&tic->t_ref, 1);
  3253. tic->t_task = current;
  3254. INIT_LIST_HEAD(&tic->t_queue);
  3255. tic->t_unit_res = unit_res;
  3256. tic->t_curr_res = unit_res;
  3257. tic->t_cnt = cnt;
  3258. tic->t_ocnt = cnt;
  3259. tic->t_tid = prandom_u32();
  3260. tic->t_clientid = client;
  3261. tic->t_flags = XLOG_TIC_INITED;
  3262. if (permanent)
  3263. tic->t_flags |= XLOG_TIC_PERM_RESERV;
  3264. xlog_tic_reset_res(tic);
  3265. return tic;
  3266. }
  3267. /******************************************************************************
  3268. *
  3269. * Log debug routines
  3270. *
  3271. ******************************************************************************
  3272. */
  3273. #if defined(DEBUG)
  3274. /*
  3275. * Make sure that the destination ptr is within the valid data region of
  3276. * one of the iclogs. This uses backup pointers stored in a different
  3277. * part of the log in case we trash the log structure.
  3278. */
  3279. STATIC void
  3280. xlog_verify_dest_ptr(
  3281. struct xlog *log,
  3282. void *ptr)
  3283. {
  3284. int i;
  3285. int good_ptr = 0;
  3286. for (i = 0; i < log->l_iclog_bufs; i++) {
  3287. if (ptr >= log->l_iclog_bak[i] &&
  3288. ptr <= log->l_iclog_bak[i] + log->l_iclog_size)
  3289. good_ptr++;
  3290. }
  3291. if (!good_ptr)
  3292. xfs_emerg(log->l_mp, "%s: invalid ptr", __func__);
  3293. }
  3294. /*
  3295. * Check to make sure the grant write head didn't just over lap the tail. If
  3296. * the cycles are the same, we can't be overlapping. Otherwise, make sure that
  3297. * the cycles differ by exactly one and check the byte count.
  3298. *
  3299. * This check is run unlocked, so can give false positives. Rather than assert
  3300. * on failures, use a warn-once flag and a panic tag to allow the admin to
  3301. * determine if they want to panic the machine when such an error occurs. For
  3302. * debug kernels this will have the same effect as using an assert but, unlinke
  3303. * an assert, it can be turned off at runtime.
  3304. */
  3305. STATIC void
  3306. xlog_verify_grant_tail(
  3307. struct xlog *log)
  3308. {
  3309. int tail_cycle, tail_blocks;
  3310. int cycle, space;
  3311. xlog_crack_grant_head(&log->l_write_head.grant, &cycle, &space);
  3312. xlog_crack_atomic_lsn(&log->l_tail_lsn, &tail_cycle, &tail_blocks);
  3313. if (tail_cycle != cycle) {
  3314. if (cycle - 1 != tail_cycle &&
  3315. !(log->l_flags & XLOG_TAIL_WARN)) {
  3316. xfs_alert_tag(log->l_mp, XFS_PTAG_LOGRES,
  3317. "%s: cycle - 1 != tail_cycle", __func__);
  3318. log->l_flags |= XLOG_TAIL_WARN;
  3319. }
  3320. if (space > BBTOB(tail_blocks) &&
  3321. !(log->l_flags & XLOG_TAIL_WARN)) {
  3322. xfs_alert_tag(log->l_mp, XFS_PTAG_LOGRES,
  3323. "%s: space > BBTOB(tail_blocks)", __func__);
  3324. log->l_flags |= XLOG_TAIL_WARN;
  3325. }
  3326. }
  3327. }
  3328. /* check if it will fit */
  3329. STATIC void
  3330. xlog_verify_tail_lsn(
  3331. struct xlog *log,
  3332. struct xlog_in_core *iclog,
  3333. xfs_lsn_t tail_lsn)
  3334. {
  3335. int blocks;
  3336. if (CYCLE_LSN(tail_lsn) == log->l_prev_cycle) {
  3337. blocks =
  3338. log->l_logBBsize - (log->l_prev_block - BLOCK_LSN(tail_lsn));
  3339. if (blocks < BTOBB(iclog->ic_offset)+BTOBB(log->l_iclog_hsize))
  3340. xfs_emerg(log->l_mp, "%s: ran out of log space", __func__);
  3341. } else {
  3342. ASSERT(CYCLE_LSN(tail_lsn)+1 == log->l_prev_cycle);
  3343. if (BLOCK_LSN(tail_lsn) == log->l_prev_block)
  3344. xfs_emerg(log->l_mp, "%s: tail wrapped", __func__);
  3345. blocks = BLOCK_LSN(tail_lsn) - log->l_prev_block;
  3346. if (blocks < BTOBB(iclog->ic_offset) + 1)
  3347. xfs_emerg(log->l_mp, "%s: ran out of log space", __func__);
  3348. }
  3349. } /* xlog_verify_tail_lsn */
  3350. /*
  3351. * Perform a number of checks on the iclog before writing to disk.
  3352. *
  3353. * 1. Make sure the iclogs are still circular
  3354. * 2. Make sure we have a good magic number
  3355. * 3. Make sure we don't have magic numbers in the data
  3356. * 4. Check fields of each log operation header for:
  3357. * A. Valid client identifier
  3358. * B. tid ptr value falls in valid ptr space (user space code)
  3359. * C. Length in log record header is correct according to the
  3360. * individual operation headers within record.
  3361. * 5. When a bwrite will occur within 5 blocks of the front of the physical
  3362. * log, check the preceding blocks of the physical log to make sure all
  3363. * the cycle numbers agree with the current cycle number.
  3364. */
  3365. STATIC void
  3366. xlog_verify_iclog(
  3367. struct xlog *log,
  3368. struct xlog_in_core *iclog,
  3369. int count,
  3370. bool syncing)
  3371. {
  3372. xlog_op_header_t *ophead;
  3373. xlog_in_core_t *icptr;
  3374. xlog_in_core_2_t *xhdr;
  3375. void *base_ptr, *ptr, *p;
  3376. ptrdiff_t field_offset;
  3377. uint8_t clientid;
  3378. int len, i, j, k, op_len;
  3379. int idx;
  3380. /* check validity of iclog pointers */
  3381. spin_lock(&log->l_icloglock);
  3382. icptr = log->l_iclog;
  3383. for (i = 0; i < log->l_iclog_bufs; i++, icptr = icptr->ic_next)
  3384. ASSERT(icptr);
  3385. if (icptr != log->l_iclog)
  3386. xfs_emerg(log->l_mp, "%s: corrupt iclog ring", __func__);
  3387. spin_unlock(&log->l_icloglock);
  3388. /* check log magic numbers */
  3389. if (iclog->ic_header.h_magicno != cpu_to_be32(XLOG_HEADER_MAGIC_NUM))
  3390. xfs_emerg(log->l_mp, "%s: invalid magic num", __func__);
  3391. base_ptr = ptr = &iclog->ic_header;
  3392. p = &iclog->ic_header;
  3393. for (ptr += BBSIZE; ptr < base_ptr + count; ptr += BBSIZE) {
  3394. if (*(__be32 *)ptr == cpu_to_be32(XLOG_HEADER_MAGIC_NUM))
  3395. xfs_emerg(log->l_mp, "%s: unexpected magic num",
  3396. __func__);
  3397. }
  3398. /* check fields */
  3399. len = be32_to_cpu(iclog->ic_header.h_num_logops);
  3400. base_ptr = ptr = iclog->ic_datap;
  3401. ophead = ptr;
  3402. xhdr = iclog->ic_data;
  3403. for (i = 0; i < len; i++) {
  3404. ophead = ptr;
  3405. /* clientid is only 1 byte */
  3406. p = &ophead->oh_clientid;
  3407. field_offset = p - base_ptr;
  3408. if (!syncing || (field_offset & 0x1ff)) {
  3409. clientid = ophead->oh_clientid;
  3410. } else {
  3411. idx = BTOBBT((char *)&ophead->oh_clientid - iclog->ic_datap);
  3412. if (idx >= (XLOG_HEADER_CYCLE_SIZE / BBSIZE)) {
  3413. j = idx / (XLOG_HEADER_CYCLE_SIZE / BBSIZE);
  3414. k = idx % (XLOG_HEADER_CYCLE_SIZE / BBSIZE);
  3415. clientid = xlog_get_client_id(
  3416. xhdr[j].hic_xheader.xh_cycle_data[k]);
  3417. } else {
  3418. clientid = xlog_get_client_id(
  3419. iclog->ic_header.h_cycle_data[idx]);
  3420. }
  3421. }
  3422. if (clientid != XFS_TRANSACTION && clientid != XFS_LOG)
  3423. xfs_warn(log->l_mp,
  3424. "%s: invalid clientid %d op "PTR_FMT" offset 0x%lx",
  3425. __func__, clientid, ophead,
  3426. (unsigned long)field_offset);
  3427. /* check length */
  3428. p = &ophead->oh_len;
  3429. field_offset = p - base_ptr;
  3430. if (!syncing || (field_offset & 0x1ff)) {
  3431. op_len = be32_to_cpu(ophead->oh_len);
  3432. } else {
  3433. idx = BTOBBT((uintptr_t)&ophead->oh_len -
  3434. (uintptr_t)iclog->ic_datap);
  3435. if (idx >= (XLOG_HEADER_CYCLE_SIZE / BBSIZE)) {
  3436. j = idx / (XLOG_HEADER_CYCLE_SIZE / BBSIZE);
  3437. k = idx % (XLOG_HEADER_CYCLE_SIZE / BBSIZE);
  3438. op_len = be32_to_cpu(xhdr[j].hic_xheader.xh_cycle_data[k]);
  3439. } else {
  3440. op_len = be32_to_cpu(iclog->ic_header.h_cycle_data[idx]);
  3441. }
  3442. }
  3443. ptr += sizeof(xlog_op_header_t) + op_len;
  3444. }
  3445. } /* xlog_verify_iclog */
  3446. #endif
  3447. /*
  3448. * Mark all iclogs IOERROR. l_icloglock is held by the caller.
  3449. */
  3450. STATIC int
  3451. xlog_state_ioerror(
  3452. struct xlog *log)
  3453. {
  3454. xlog_in_core_t *iclog, *ic;
  3455. iclog = log->l_iclog;
  3456. if (! (iclog->ic_state & XLOG_STATE_IOERROR)) {
  3457. /*
  3458. * Mark all the incore logs IOERROR.
  3459. * From now on, no log flushes will result.
  3460. */
  3461. ic = iclog;
  3462. do {
  3463. ic->ic_state = XLOG_STATE_IOERROR;
  3464. ic = ic->ic_next;
  3465. } while (ic != iclog);
  3466. return 0;
  3467. }
  3468. /*
  3469. * Return non-zero, if state transition has already happened.
  3470. */
  3471. return 1;
  3472. }
  3473. /*
  3474. * This is called from xfs_force_shutdown, when we're forcibly
  3475. * shutting down the filesystem, typically because of an IO error.
  3476. * Our main objectives here are to make sure that:
  3477. * a. if !logerror, flush the logs to disk. Anything modified
  3478. * after this is ignored.
  3479. * b. the filesystem gets marked 'SHUTDOWN' for all interested
  3480. * parties to find out, 'atomically'.
  3481. * c. those who're sleeping on log reservations, pinned objects and
  3482. * other resources get woken up, and be told the bad news.
  3483. * d. nothing new gets queued up after (b) and (c) are done.
  3484. *
  3485. * Note: for the !logerror case we need to flush the regions held in memory out
  3486. * to disk first. This needs to be done before the log is marked as shutdown,
  3487. * otherwise the iclog writes will fail.
  3488. */
  3489. int
  3490. xfs_log_force_umount(
  3491. struct xfs_mount *mp,
  3492. int logerror)
  3493. {
  3494. struct xlog *log;
  3495. int retval;
  3496. log = mp->m_log;
  3497. /*
  3498. * If this happens during log recovery, don't worry about
  3499. * locking; the log isn't open for business yet.
  3500. */
  3501. if (!log ||
  3502. log->l_flags & XLOG_ACTIVE_RECOVERY) {
  3503. mp->m_flags |= XFS_MOUNT_FS_SHUTDOWN;
  3504. if (mp->m_sb_bp)
  3505. mp->m_sb_bp->b_flags |= XBF_DONE;
  3506. return 0;
  3507. }
  3508. /*
  3509. * Somebody could've already done the hard work for us.
  3510. * No need to get locks for this.
  3511. */
  3512. if (logerror && log->l_iclog->ic_state & XLOG_STATE_IOERROR) {
  3513. ASSERT(XLOG_FORCED_SHUTDOWN(log));
  3514. return 1;
  3515. }
  3516. /*
  3517. * Flush all the completed transactions to disk before marking the log
  3518. * being shut down. We need to do it in this order to ensure that
  3519. * completed operations are safely on disk before we shut down, and that
  3520. * we don't have to issue any buffer IO after the shutdown flags are set
  3521. * to guarantee this.
  3522. */
  3523. if (!logerror)
  3524. xfs_log_force(mp, XFS_LOG_SYNC);
  3525. /*
  3526. * mark the filesystem and the as in a shutdown state and wake
  3527. * everybody up to tell them the bad news.
  3528. */
  3529. spin_lock(&log->l_icloglock);
  3530. mp->m_flags |= XFS_MOUNT_FS_SHUTDOWN;
  3531. if (mp->m_sb_bp)
  3532. mp->m_sb_bp->b_flags |= XBF_DONE;
  3533. /*
  3534. * Mark the log and the iclogs with IO error flags to prevent any
  3535. * further log IO from being issued or completed.
  3536. */
  3537. log->l_flags |= XLOG_IO_ERROR;
  3538. retval = xlog_state_ioerror(log);
  3539. spin_unlock(&log->l_icloglock);
  3540. /*
  3541. * We don't want anybody waiting for log reservations after this. That
  3542. * means we have to wake up everybody queued up on reserveq as well as
  3543. * writeq. In addition, we make sure in xlog_{re}grant_log_space that
  3544. * we don't enqueue anything once the SHUTDOWN flag is set, and this
  3545. * action is protected by the grant locks.
  3546. */
  3547. xlog_grant_head_wake_all(&log->l_reserve_head);
  3548. xlog_grant_head_wake_all(&log->l_write_head);
  3549. /*
  3550. * Wake up everybody waiting on xfs_log_force. Wake the CIL push first
  3551. * as if the log writes were completed. The abort handling in the log
  3552. * item committed callback functions will do this again under lock to
  3553. * avoid races.
  3554. */
  3555. spin_lock(&log->l_cilp->xc_push_lock);
  3556. wake_up_all(&log->l_cilp->xc_commit_wait);
  3557. spin_unlock(&log->l_cilp->xc_push_lock);
  3558. xlog_state_do_callback(log, XFS_LI_ABORTED, NULL);
  3559. #ifdef XFSERRORDEBUG
  3560. {
  3561. xlog_in_core_t *iclog;
  3562. spin_lock(&log->l_icloglock);
  3563. iclog = log->l_iclog;
  3564. do {
  3565. ASSERT(iclog->ic_callback == 0);
  3566. iclog = iclog->ic_next;
  3567. } while (iclog != log->l_iclog);
  3568. spin_unlock(&log->l_icloglock);
  3569. }
  3570. #endif
  3571. /* return non-zero if log IOERROR transition had already happened */
  3572. return retval;
  3573. }
  3574. STATIC int
  3575. xlog_iclogs_empty(
  3576. struct xlog *log)
  3577. {
  3578. xlog_in_core_t *iclog;
  3579. iclog = log->l_iclog;
  3580. do {
  3581. /* endianness does not matter here, zero is zero in
  3582. * any language.
  3583. */
  3584. if (iclog->ic_header.h_num_logops)
  3585. return 0;
  3586. iclog = iclog->ic_next;
  3587. } while (iclog != log->l_iclog);
  3588. return 1;
  3589. }
  3590. /*
  3591. * Verify that an LSN stamped into a piece of metadata is valid. This is
  3592. * intended for use in read verifiers on v5 superblocks.
  3593. */
  3594. bool
  3595. xfs_log_check_lsn(
  3596. struct xfs_mount *mp,
  3597. xfs_lsn_t lsn)
  3598. {
  3599. struct xlog *log = mp->m_log;
  3600. bool valid;
  3601. /*
  3602. * norecovery mode skips mount-time log processing and unconditionally
  3603. * resets the in-core LSN. We can't validate in this mode, but
  3604. * modifications are not allowed anyways so just return true.
  3605. */
  3606. if (mp->m_flags & XFS_MOUNT_NORECOVERY)
  3607. return true;
  3608. /*
  3609. * Some metadata LSNs are initialized to NULL (e.g., the agfl). This is
  3610. * handled by recovery and thus safe to ignore here.
  3611. */
  3612. if (lsn == NULLCOMMITLSN)
  3613. return true;
  3614. valid = xlog_valid_lsn(mp->m_log, lsn);
  3615. /* warn the user about what's gone wrong before verifier failure */
  3616. if (!valid) {
  3617. spin_lock(&log->l_icloglock);
  3618. xfs_warn(mp,
  3619. "Corruption warning: Metadata has LSN (%d:%d) ahead of current LSN (%d:%d). "
  3620. "Please unmount and run xfs_repair (>= v4.3) to resolve.",
  3621. CYCLE_LSN(lsn), BLOCK_LSN(lsn),
  3622. log->l_curr_cycle, log->l_curr_block);
  3623. spin_unlock(&log->l_icloglock);
  3624. }
  3625. return valid;
  3626. }
  3627. bool
  3628. xfs_log_in_recovery(
  3629. struct xfs_mount *mp)
  3630. {
  3631. struct xlog *log = mp->m_log;
  3632. return log->l_flags & XLOG_ACTIVE_RECOVERY;
  3633. }