xfs_log.c 111 KB

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