xfs_log.c 106 KB

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