xfs_log.c 107 KB

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