xfs_log.c 102 KB

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