xfs_log.c 105 KB

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