xfs_log.c 105 KB

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