xfs_log.c 105 KB

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