xfs_log.c 100 KB

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