xfs_log.c 100 KB

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