xfs_log.c 106 KB

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