xfs_log.c 100 KB

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