xfs_log.c 107 KB

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