xfs_log.c 102 KB

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