xfs_log.c 107 KB

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