xfs_log.c 102 KB

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