xfs_log.c 107 KB

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