xfs_log.c 107 KB

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