xfs_log.c 100 KB

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