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