xfs_log.c 106 KB

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