xfs_log.c 105 KB

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