xfs_log.c 102 KB

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