xfs_log.c 106 KB

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