xfs_log.c 105 KB

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