xfs_log.c 104 KB

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