xfs_log.c 105 KB

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