xfs_log.c 104 KB

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