xfs_log.c 100 KB

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