xfs_log.c 100 KB

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