xfs_log.c 107 KB

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