xfs_log.c 106 KB

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