xfs_log.c 106 KB

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