xfs_log.c 103 KB

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