xfs_log.c 113 KB

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