xfs_log.c 106 KB

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