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