xfs_log.c 109 KB

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