xfs_log.c 106 KB

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