xfs_log.c 113 KB

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