xfs_log.c 102 KB

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