xfs_log.c 107 KB

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