xfs_log.c 102 KB

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