xfs_log.c 104 KB

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