xfs_log.c 107 KB

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