xfs_log.c 102 KB

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