xfs_log.c 106 KB

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