xfs_log.c 104 KB

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