xfs_log.c 112 KB

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