xfs_log.c 106 KB

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