xfs_log.c 106 KB

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