xfs_log.c 106 KB

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