xfs_log.c 102 KB

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