xfs_log.c 105 KB

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