xfs_log.c 112 KB

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