xfs_log.c 102 KB

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