xfs_log.c 107 KB

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