xfs_log.c 106 KB

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