xfs_log.c 107 KB

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