xfs_log.c 106 KB

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