xfs_log.c 105 KB

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