xfs_log.c 107 KB

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