xfs_log.c 107 KB

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