xfs_log.c 105 KB

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