xfs_log.c 105 KB

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