xfs_log.c 105 KB

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