xfs_log.c 108 KB

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