xfs_log.c 105 KB

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