xfs_log.c 106 KB

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