xfs_log.c 104 KB

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