xfs_log.c 105 KB

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