xfs_log.c 100 KB

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