xfs_log.c 107 KB

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