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