xfs_log.c 102 KB

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