xfs_log.c 102 KB

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