xfs_log.c 103 KB

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