xfs_log.c 104 KB

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