xfs_log.c 106 KB

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