xfs_log.c 105 KB

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