xfs_log.c 105 KB

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