xfs_log.c 107 KB

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