xfs_log.c 101 KB

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