xfs_log.c 106 KB

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